From patchwork Tue Feb 20 15:10:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 203640 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp467593dyc; Tue, 20 Feb 2024 07:15:45 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXLhYP6DSTaYIdqLw+vzu4kU6clmYnsy5GtLjFP2/qGj9lB30MxRQxbXNhjdSq7hzTo1pUWha39+Jf2/3+y0aL9Gv7vpg== X-Google-Smtp-Source: AGHT+IELu9mRN9K/4Qi8I+Z8PZGLiGKMMWngo0kK8yQtODc53Tjzq9QySS7AgjRV4hG41UiO0rls X-Received: by 2002:a05:6a00:228f:b0:6e4:7a32:aa22 with SMTP id f15-20020a056a00228f00b006e47a32aa22mr2426633pfe.20.1708442144822; Tue, 20 Feb 2024 07:15:44 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708442144; cv=pass; d=google.com; s=arc-20160816; b=Tf3Q0hgMOKNEFg9oTuU7NijwYgRaZUzAfC/Jdjq99zC9p+sSXQxSbcQABotdqYwdzS xXEk45qmTGun1525PSX8T8I965Dr8PttCzJE0pan0jTb++a6ANWKtGgA9F2S23zJDYwu 8ggnkbZqOESA0Ckn6BWPP+2laqeTioicmHGe1gXwg36of9Cyz5GVLDioofCwjWOtpPZB h2GPHtWUvXYS1ZZht+WAj4OMiZgyYHBAHwmanug4CsclUmGTZIfsNYAPVNDSsnul4Ppo XEpL6Mgw8jQiUKMiuAPtS6PqYLvhPh7oCVA9BKEa0UAs9u+fBQRGbBidQZIflMs9y0oU 30ug== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=p126ZMPv5yWICgYljgBmVza3JvKiPG+UvgFwItLei+8=; fh=+ELE5T5nKVns0CpmHgc+07NTC3/4llFQS7tli2CyIjM=; b=N5eCA13ivNho4N7Olvtl34aNxES0uN6ZstpizrSNYJE21S1K1oh0dCpPy/HbFwm1lb mRg/EG6TOY5gp3LZukeeWlhYzfTSevcQN8m3vg9XDao6DHjLX+RtJIK9dWtfkl3qVPal A+mrc591G0Y2JB/+oysM7RFsm2SxKeUrwHnO9CWmgA5li9fqSKr6Sw9Iz/SRoSnTtEFJ trohGvqy3T9IyuSqWc469XiNB4ZiSUVnZZrTqJDzb/zpNdKsrUoXELe7xfQRxJ8d1uZq jAf8NyAcKWtzg8p/z+3FegaxXAQ6JmpXpavUJ/urn4VIkuKiXIICHlnjn4cn4bx6lXsQ ep/g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=tCyc6uBw; arc=pass (i=1 spf=pass spfdomain=flex--sebastianene.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-73225-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-73225-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id bn20-20020a056a00325400b006e09499cc7bsi6307734pfb.269.2024.02.20.07.15.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 07:15:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-73225-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=tCyc6uBw; arc=pass (i=1 spf=pass spfdomain=flex--sebastianene.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-73225-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-73225-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id CE49FB22A09 for ; Tue, 20 Feb 2024 15:13:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D7987768E4; Tue, 20 Feb 2024 15:11:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="tCyc6uBw" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8F36A762FB for ; Tue, 20 Feb 2024 15:11:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708441905; cv=none; b=VRy0rpEFLAMkpdWO9dvvAM0FUYwaYINQj6apbXc99RYf2PsbC71UhOhhWVQ0uYNaBhU2PTLfgim6koVL6SeTxgf8JzAH/+TAMNdyU4B/SwRVm1y0XHBs+5CC0Jx/93ryMRU35F2Qqalc9U3VtZRFvJQH0AaQwCV00yD1hGj0Zeg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708441905; c=relaxed/simple; bh=jMavhYRCsq8TrG+5Wn0f9GwDD+Uua09tmUtXiDesIR8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=T7DJ1Vksc33EnD2JPPLFQHMGSqeNbyVSG4UcVp/VMl0r/07V+RD/BWiWo4f/hIcdyNA6RkGqwY7Cll91J8YusVeWyM5FY7X+SvoVjQ9yxTHmfxorqhnZl0b2U17deKgeszZb/ArRyKwsju7SErG5fMksJgM+tY4BRoNjS14qNZ0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=tCyc6uBw; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc6b26783b4so4624610276.0 for ; Tue, 20 Feb 2024 07:11:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708441901; x=1709046701; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=p126ZMPv5yWICgYljgBmVza3JvKiPG+UvgFwItLei+8=; b=tCyc6uBwnGvwKjNwTw0Hfj/mbG6DKrsLL1b4mB+eKgEaJIqgWEivMdBOv6wNZBC3oE UwgQlCNPu6XLaxNwAfe9aZaeYxqT3JMW7AltVoAlGKq//DAqYKP7br7FRuZdJyPO0ajL turDEcTrDeypLwlOo6irez+M3tNctc173Ap3zDHYAQTb1n+zk55z2cFS0SLBikE39xeA FGSIiuQMkcd1g/eKxoQ+9lwLbXarsLoQqU7CCALoiJX4cC1wZ3VP1OzZU7k2M73d5pNn kP9X6Z1Xo0GkYijC+c4VywSgVEVYhtdRpzSBIU+MYlfp6/yXnh7oSr/EuRNrhPJEcOxr A8hA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708441901; x=1709046701; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=p126ZMPv5yWICgYljgBmVza3JvKiPG+UvgFwItLei+8=; b=vS+79bJlSSy6kOWGbpyzj7MH90vDuKdTrsejklFk9RLY2lzrmB0QrC6EdaBID0IJLv Ot/xu2TAwSGrCVgTwKSMyvJx8SlfxhoN/+eLq+d4pRKsUO/noahDZOOA68uh+ehJKysS IEifgo88X/iLvQqccCAQG+QZ5Ya7jGVwY6UiwtBav1GCZ7EmfZUwS8XrkSyUGA1epUBG 1caomQvskGbAV+VVRVzGHH7YcBs3q5XRyr/mQeodrmkNI6fjgac7bAYj8cjmyQ2Iv0xK YUYyVk42iN6QrY8FOlZawu30ITdpaLiwm/pcz77EfngVUpB7w4fODIZdblGowScXdaDi GiMg== X-Forwarded-Encrypted: i=1; AJvYcCXzz3s4m/oLFWWTvYNkKDQENdnooV9PlGCGd1aTjXQAmrsx2FAcRy61p91IgQeldveT04RElaERtF5k2KJteZMzWeid3P03jJdT9Ym/ X-Gm-Message-State: AOJu0YxxDNgb1nW+Tar04eP9ZCtqXKfeqMgwGCY95ogSTuNB7cB4e0mN Jpk9z/8Svb2kG3EqBxzLWLYOtXjP2tCsWXdzwSUzeLDNhYPnV12ikPETGmV5arShwtLsefjw0LV q+qZSZiLDdm6N46P35pTINK6KuQ== X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:6902:100a:b0:dc6:c94e:fb85 with SMTP id w10-20020a056902100a00b00dc6c94efb85mr573440ybt.2.1708441901684; Tue, 20 Feb 2024 07:11:41 -0800 (PST) Date: Tue, 20 Feb 2024 15:10:33 +0000 In-Reply-To: <20240220151035.327199-1-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240220151035.327199-1-sebastianene@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Message-ID: <20240220151035.327199-5-sebastianene@google.com> Subject: [PATCH v6 4/6] KVM: arm64: Register ptdump with debugfs on guest creation From: Sebastian Ene To: catalin.marinas@arm.com, gshan@redhat.com, james.morse@arm.com, mark.rutland@arm.com, maz@kernel.org, oliver.upton@linux.dev, rananta@google.com, ricarkol@google.com, ryan.roberts@arm.com, shahuang@redhat.com, suzuki.poulose@arm.com, will@kernel.org, yuzenghui@huawei.com Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com, vdonnefort@google.com, Sebastian Ene X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791431430397633484 X-GMAIL-MSGID: 1791431430397633484 While arch/*/mem/ptdump handles the kernel pagetable dumping code, introduce KVM/ptdump which shows the guest stage-2 pagetables. The separation is necessary because most of the definitions from the stage-2 pagetable reside in the KVM path and we will be invoking functionality **specific** to KVM. When a guest is created, register a new file entry under the guest debugfs dir which allows userspace to show the contents of the guest stage-2 pagetables when accessed. Signed-off-by: Sebastian Ene --- arch/arm64/kvm/Kconfig | 13 ++++++ arch/arm64/kvm/Makefile | 1 + arch/arm64/kvm/debug.c | 8 ++++ arch/arm64/kvm/kvm_ptdump.h | 20 +++++++++ arch/arm64/kvm/ptdump.c | 88 +++++++++++++++++++++++++++++++++++++ 5 files changed, 130 insertions(+) create mode 100644 arch/arm64/kvm/kvm_ptdump.h create mode 100644 arch/arm64/kvm/ptdump.c diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index 27ca89b628a0..83ca07d16cf4 100644 --- a/arch/arm64/kvm/Kconfig +++ b/arch/arm64/kvm/Kconfig @@ -67,4 +67,17 @@ config PROTECTED_NVHE_STACKTRACE If unsure, or not using protected nVHE (pKVM), say N. +config PTDUMP_STAGE2_DEBUGFS + bool "Present the stage-2 pagetables to debugfs" + depends on PTDUMP_DEBUGFS && KVM + default n + help + Say Y here if you want to show the stage-2 kernel pagetables + layout in a debugfs file. This information is only useful for kernel developers + who are working in architecture specific areas of the kernel. + It is probably not a good idea to enable this feature in a production + kernel. + + If in doubt, say N. + endif # VIRTUALIZATION diff --git a/arch/arm64/kvm/Makefile b/arch/arm64/kvm/Makefile index c0c050e53157..190eac17538c 100644 --- a/arch/arm64/kvm/Makefile +++ b/arch/arm64/kvm/Makefile @@ -23,6 +23,7 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ vgic/vgic-its.o vgic/vgic-debug.o kvm-$(CONFIG_HW_PERF_EVENTS) += pmu-emul.o pmu.o +kvm-$(CONFIG_PTDUMP_STAGE2_DEBUGFS) += ptdump.o always-y := hyp_constants.h hyp-constants.s diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c index 8725291cb00a..ff97af2c0513 100644 --- a/arch/arm64/kvm/debug.c +++ b/arch/arm64/kvm/debug.c @@ -14,6 +14,8 @@ #include #include +#include + #include "trace.h" /* These are the bits of MDSCR_EL1 we may manipulate */ @@ -342,3 +344,9 @@ void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu) vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_SPE); vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRBE); } + +int kvm_arch_create_vm_debugfs(struct kvm *kvm) +{ + kvm_ptdump_guest_register(kvm); + return 0; +} diff --git a/arch/arm64/kvm/kvm_ptdump.h b/arch/arm64/kvm/kvm_ptdump.h new file mode 100644 index 000000000000..5e8a655605f1 --- /dev/null +++ b/arch/arm64/kvm/kvm_ptdump.h @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) Google, 2023 + * Author: Sebastian Ene + */ + +#ifndef __KVM_PTDUMP_H +#define __KVM_PTDUMP_H + +#include +#include + + +#ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS +void kvm_ptdump_guest_register(struct kvm *kvm); +#else +static inline void kvm_ptdump_guest_register(struct kvm *kvm) {} +#endif /* CONFIG_PTDUMP_STAGE2_DEBUGFS */ + +#endif /* __KVM_PTDUMP_H */ diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c new file mode 100644 index 000000000000..9b04c24bb9be --- /dev/null +++ b/arch/arm64/kvm/ptdump.c @@ -0,0 +1,88 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Debug helper used to dump the stage-2 pagetables of the system and their + * associated permissions. + * + * Copyright (C) Google, 2023 + * Author: Sebastian Ene + */ +#include +#include +#include + +#include +#include + + +static int kvm_ptdump_visitor(const struct kvm_pgtable_visit_ctx *ctx, + enum kvm_pgtable_walk_flags visit) +{ + struct pg_state *st = ctx->arg; + struct ptdump_state *pt_st = &st->ptdump; + + note_page(pt_st, ctx->addr, ctx->level, ctx->old); + return 0; +} + +static int kvm_ptdump_show_common(struct seq_file *m, + struct kvm_pgtable *pgtable, + struct pg_state *parser_state) +{ + struct kvm_pgtable_walker walker = (struct kvm_pgtable_walker) { + .cb = kvm_ptdump_visitor, + .arg = parser_state, + .flags = KVM_PGTABLE_WALK_LEAF, + }; + + return kvm_pgtable_walk(pgtable, 0, BIT(pgtable->ia_bits), &walker); +} + +static int kvm_ptdump_guest_show(struct seq_file *m, void *) +{ + struct kvm *kvm = m->private; + struct kvm_s2_mmu *mmu = &kvm->arch.mmu; + struct pg_state parser_state = {0}; + int ret; + + write_lock(&kvm->mmu_lock); + ret = kvm_ptdump_show_common(m, mmu->pgt, &parser_state); + write_unlock(&kvm->mmu_lock); + + return ret; +} + +static int kvm_ptdump_guest_open(struct inode *m, struct file *file) +{ + struct kvm *kvm = m->i_private; + int ret; + + if (!kvm_get_kvm_safe(kvm)) + return -ENOENT; + + ret = single_open(file, kvm_ptdump_guest_show, m->i_private); + if (ret < 0) + kvm_put_kvm(kvm); + + return ret; +} + +static int kvm_ptdump_guest_close(struct inode *m, struct file *file) +{ + struct kvm *kvm = m->i_private; + + kvm_put_kvm(kvm); + return single_release(m, file); +} + +static const struct file_operations kvm_ptdump_guest_fops = { + .open = kvm_ptdump_guest_open, + .read = seq_read, + .llseek = seq_lseek, + .release = kvm_ptdump_guest_close, +}; + +void kvm_ptdump_guest_register(struct kvm *kvm) +{ + debugfs_create_file("stage2_page_tables", 0400, kvm->debugfs_dentry, + kvm, &kvm_ptdump_guest_fops); +}