From nobody Mon Apr 6 21:33:56 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D47963803C4 for ; Tue, 17 Mar 2026 18:27:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773772053; cv=none; b=hgjr/mrgjQPb3dy730JGBff/hCALrDzPnKxvBDWEj1t6CCQyP7BEHLCePTu6tXHQfXkw0uk8SsNQ8tYLlxh7mszioQdq1/lrMU402BbW/SErCFehymCjPPhRe7hzValcn4lG/eu8Rg+NCrBwH3LBgxsuzIOFCLUaoAmaLFeFRpw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773772053; c=relaxed/simple; bh=PKLzSjHdsr85DIWF9Z3dPB8MLO6XnjzBexVFM2uaYO4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=n2gq7c2p2/6DQKHG/leskg9y3F6pfttgUetTHOHGtUh+JR5m51K3zabsvfrhrdA5qzvf0skFQFE9WQKM4NYVSOgFS+S+5ZmsJbcZqmqY1brX6NrpoPz08RA6TFPq80ODIpsVXB8cpQ0uWRd2O8yMRKo9VAinVJAKjwXhk464lkI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 42DEF1477; Tue, 17 Mar 2026 11:27:25 -0700 (PDT) Received: from workstation-e142269.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id BAFD43F7BD; Tue, 17 Mar 2026 11:27:29 -0700 (PDT) From: Wei-Lin Chang To: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org Cc: Marc Zyngier , Oliver Upton , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon Subject: [PATCH v2 1/2] KVM: arm64: ptdump: Make KVM ptdump code s2 mmu aware Date: Tue, 17 Mar 2026 18:26:37 +0000 Message-ID: <20260317182638.1592507-2-weilin.chang@arm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260317182638.1592507-1-weilin.chang@arm.com> References: <20260317182638.1592507-1-weilin.chang@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To reuse the ptdump code for shadow page table dumping later, let's pass s2 mmu as the private data, so we have the freedom to select which page table to print. Signed-off-by: Wei-Lin Chang Reviewed-by: Joey Gouly --- arch/arm64/kvm/ptdump.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c index 6cbe018fd6fd..98763b291956 100644 --- a/arch/arm64/kvm/ptdump.c +++ b/arch/arm64/kvm/ptdump.c @@ -18,7 +18,7 @@ #define KVM_PGTABLE_MAX_LEVELS (KVM_PGTABLE_LAST_LEVEL + 1) =20 struct kvm_ptdump_guest_state { - struct kvm *kvm; + struct kvm_s2_mmu *mmu; struct ptdump_pg_state parser_state; struct addr_marker ipa_marker[MARKERS_LEN]; struct ptdump_pg_level level[KVM_PGTABLE_MAX_LEVELS]; @@ -112,10 +112,9 @@ static int kvm_ptdump_build_levels(struct ptdump_pg_le= vel *level, u32 start_lvl) return 0; } =20 -static struct kvm_ptdump_guest_state *kvm_ptdump_parser_create(struct kvm = *kvm) +static struct kvm_ptdump_guest_state *kvm_ptdump_parser_create(struct kvm_= s2_mmu *mmu) { struct kvm_ptdump_guest_state *st; - struct kvm_s2_mmu *mmu =3D &kvm->arch.mmu; struct kvm_pgtable *pgtable =3D mmu->pgt; int ret; =20 @@ -133,7 +132,7 @@ static struct kvm_ptdump_guest_state *kvm_ptdump_parser= _create(struct kvm *kvm) st->ipa_marker[1].start_address =3D BIT(pgtable->ia_bits); st->range[0].end =3D BIT(pgtable->ia_bits); =20 - st->kvm =3D kvm; + st->mmu =3D mmu; st->parser_state =3D (struct ptdump_pg_state) { .marker =3D &st->ipa_marker[0], .level =3D -1, @@ -149,8 +148,8 @@ static int kvm_ptdump_guest_show(struct seq_file *m, vo= id *unused) { int ret; struct kvm_ptdump_guest_state *st =3D m->private; - struct kvm *kvm =3D st->kvm; - struct kvm_s2_mmu *mmu =3D &kvm->arch.mmu; + struct kvm_s2_mmu *mmu =3D st->mmu; + struct kvm *kvm =3D kvm_s2_mmu_to_kvm(mmu); struct ptdump_pg_state *parser_state =3D &st->parser_state; struct kvm_pgtable_walker walker =3D (struct kvm_pgtable_walker) { .cb =3D kvm_ptdump_visitor, @@ -169,14 +168,15 @@ static int kvm_ptdump_guest_show(struct seq_file *m, = void *unused) =20 static int kvm_ptdump_guest_open(struct inode *m, struct file *file) { - struct kvm *kvm =3D m->i_private; + struct kvm_s2_mmu *mmu =3D m->i_private; + struct kvm *kvm =3D kvm_s2_mmu_to_kvm(mmu); struct kvm_ptdump_guest_state *st; int ret; =20 if (!kvm_get_kvm_safe(kvm)) return -ENOENT; =20 - st =3D kvm_ptdump_parser_create(kvm); + st =3D kvm_ptdump_parser_create(mmu); if (IS_ERR(st)) { ret =3D PTR_ERR(st); goto err_with_kvm_ref; @@ -194,7 +194,7 @@ static int kvm_ptdump_guest_open(struct inode *m, struc= t file *file) =20 static int kvm_ptdump_guest_close(struct inode *m, struct file *file) { - struct kvm *kvm =3D m->i_private; + struct kvm *kvm =3D kvm_s2_mmu_to_kvm(m->i_private); void *st =3D ((struct seq_file *)file->private_data)->private; =20 kfree(st); @@ -229,14 +229,15 @@ static int kvm_pgtable_levels_show(struct seq_file *m= , void *unused) static int kvm_pgtable_debugfs_open(struct inode *m, struct file *file, int (*show)(struct seq_file *, void *)) { - struct kvm *kvm =3D m->i_private; + struct kvm_s2_mmu *mmu =3D m->i_private; + struct kvm *kvm =3D kvm_s2_mmu_to_kvm(mmu); struct kvm_pgtable *pgtable; int ret; =20 if (!kvm_get_kvm_safe(kvm)) return -ENOENT; =20 - pgtable =3D kvm->arch.mmu.pgt; + pgtable =3D mmu->pgt; =20 ret =3D single_open(file, show, pgtable); if (ret < 0) @@ -256,7 +257,7 @@ static int kvm_pgtable_levels_open(struct inode *m, str= uct file *file) =20 static int kvm_pgtable_debugfs_close(struct inode *m, struct file *file) { - struct kvm *kvm =3D m->i_private; + struct kvm *kvm =3D kvm_s2_mmu_to_kvm(m->i_private); =20 kvm_put_kvm(kvm); return single_release(m, file); @@ -279,9 +280,9 @@ static const struct file_operations kvm_pgtable_levels_= fops =3D { void kvm_s2_ptdump_create_debugfs(struct kvm *kvm) { debugfs_create_file("stage2_page_tables", 0400, kvm->debugfs_dentry, - kvm, &kvm_ptdump_guest_fops); - debugfs_create_file("ipa_range", 0400, kvm->debugfs_dentry, kvm, - &kvm_pgtable_range_fops); + &kvm->arch.mmu, &kvm_ptdump_guest_fops); + debugfs_create_file("ipa_range", 0400, kvm->debugfs_dentry, + &kvm->arch.mmu, &kvm_pgtable_range_fops); debugfs_create_file("stage2_levels", 0400, kvm->debugfs_dentry, - kvm, &kvm_pgtable_levels_fops); + &kvm->arch.mmu, &kvm_pgtable_levels_fops); } --=20 2.43.0 From nobody Mon Apr 6 21:33:56 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 96766394483 for ; Tue, 17 Mar 2026 18:27:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773772060; cv=none; b=IVPKSVRvP7/UHRzEf/zfhjzwv1ngWF0MXYl3c8gHl40rjrTcZeC+kMYx5PE1BcSCEa2Wew7kFIYxdnC+ONxxhMiel0eOChGP3ZmE2dX7T5QEtvMgEZDXyscNL9RyuO0r6FrPNt3DcbeIYOVP77YSj6O1SdQ2+hcq++/6e8L+Zl8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773772060; c=relaxed/simple; bh=KLoXmtiChwV85zt+st7WXwM0XYrxNAFXvQYAjOH5sg4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DCxqlxn4HxFjYgoH+i9gSQ3D+kmcl0i5eQbYKXUyt8/qCUsZOYjuFJ+qGfcOPhEBaSUe+dgKKZ6r9f9zI3Cj4f5iIbQHRCvWARP6UUY9rFQDuMSdBcJeeOmG1yGN8ZAZThlai8WxnkFZLmI1xr9KTtr/eSuGZLHCuOh5XQlYHJQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id E30A01477; Tue, 17 Mar 2026 11:27:31 -0700 (PDT) Received: from workstation-e142269.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 64E173F7BD; Tue, 17 Mar 2026 11:27:36 -0700 (PDT) From: Wei-Lin Chang To: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org Cc: Marc Zyngier , Oliver Upton , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon Subject: [PATCH v2 2/2] KVM: arm64: nv: Expose shadow page tables in debugfs Date: Tue, 17 Mar 2026 18:26:38 +0000 Message-ID: <20260317182638.1592507-3-weilin.chang@arm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260317182638.1592507-1-weilin.chang@arm.com> References: <20260317182638.1592507-1-weilin.chang@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Exposing shadow page tables in debugfs improves the debugability and testability of NV. With this patch a new directory "nested" is created for each VM created if the host is NV capable. Within the directory each valid s2 mmu will have its shadow page table exposed as a readable file with the file name formatted as 0x-0x-s2-{en,dis}abled. The creation and removal of the files happen at the points when an s2 mmu becomes valid, or the context it represents change. In the future the "nested" directory can also hold other NV related information. This is gated behind CONFIG_PTDUMP_STAGE2_DEBUGFS. Suggested-by: Marc Zyngier Reviewed-by: Sebastian Ene Signed-off-by: Wei-Lin Chang Reviewed-by: Joey Gouly --- arch/arm64/include/asm/kvm_host.h | 9 +++++++++ arch/arm64/include/asm/kvm_mmu.h | 4 ++++ arch/arm64/kvm/nested.c | 6 +++++- arch/arm64/kvm/ptdump.c | 27 +++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm= _host.h index 5d5a3bbdb95e..52977c9a11c3 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -217,6 +217,10 @@ struct kvm_s2_mmu { */ bool nested_stage2_enabled; =20 +#ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS + struct dentry *shadow_pt_debugfs_dentry; +#endif + /* * true when this MMU needs to be unmapped before being used for a new * purpose. @@ -405,6 +409,11 @@ struct kvm_arch { * the associated pKVM instance in the hypervisor. */ struct kvm_protected_vm pkvm; + +#ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS + /* Nested virtualization info */ + struct dentry *debugfs_nv_dentry; +#endif }; =20 struct kvm_vcpu_fault_info { diff --git a/arch/arm64/include/asm/kvm_mmu.h b/arch/arm64/include/asm/kvm_= mmu.h index d968aca0461a..01e9c72d6aa7 100644 --- a/arch/arm64/include/asm/kvm_mmu.h +++ b/arch/arm64/include/asm/kvm_mmu.h @@ -393,8 +393,12 @@ static inline bool kvm_supports_cacheable_pfnmap(void) =20 #ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS void kvm_s2_ptdump_create_debugfs(struct kvm *kvm); +void kvm_nested_s2_ptdump_create_debugfs(struct kvm_s2_mmu *mmu); +void kvm_nested_s2_ptdump_remove_debugfs(struct kvm_s2_mmu *mmu); #else static inline void kvm_s2_ptdump_create_debugfs(struct kvm *kvm) {} +static inline void kvm_nested_s2_ptdump_create_debugfs(struct kvm_s2_mmu *= mmu) {} +static inline void kvm_nested_s2_ptdump_remove_debugfs(struct kvm_s2_mmu *= mmu) {} #endif /* CONFIG_PTDUMP_STAGE2_DEBUGFS */ =20 #endif /* __ASSEMBLER__ */ diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c index eeea5e692370..31d74ed8449e 100644 --- a/arch/arm64/kvm/nested.c +++ b/arch/arm64/kvm/nested.c @@ -730,8 +730,10 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm= _vcpu *vcpu) kvm->arch.nested_mmus_next =3D (i + 1) % kvm->arch.nested_mmus_size; =20 /* Make sure we don't forget to do the laundry */ - if (kvm_s2_mmu_valid(s2_mmu)) + if (kvm_s2_mmu_valid(s2_mmu)) { + kvm_nested_s2_ptdump_remove_debugfs(s2_mmu); s2_mmu->pending_unmap =3D true; + } =20 /* * The virtual VMID (modulo CnP) will be used as a key when matching @@ -745,6 +747,8 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_= vcpu *vcpu) s2_mmu->tlb_vtcr =3D vcpu_read_sys_reg(vcpu, VTCR_EL2); s2_mmu->nested_stage2_enabled =3D vcpu_read_sys_reg(vcpu, HCR_EL2) & HCR_= VM; =20 + kvm_nested_s2_ptdump_create_debugfs(s2_mmu); + out: atomic_inc(&s2_mmu->refcnt); =20 diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c index 98763b291956..aebbbad85d38 100644 --- a/arch/arm64/kvm/ptdump.c +++ b/arch/arm64/kvm/ptdump.c @@ -10,12 +10,14 @@ #include #include =20 +#include #include #include #include =20 #define MARKERS_LEN 2 #define KVM_PGTABLE_MAX_LEVELS (KVM_PGTABLE_LAST_LEVEL + 1) +#define S2FNAMESZ sizeof("0x0123456789abcdef-0x0123456789abcdef-s2-disable= d") =20 struct kvm_ptdump_guest_state { struct kvm_s2_mmu *mmu; @@ -277,6 +279,28 @@ static const struct file_operations kvm_pgtable_levels= _fops =3D { .release =3D kvm_pgtable_debugfs_close, }; =20 +void kvm_nested_s2_ptdump_create_debugfs(struct kvm_s2_mmu *mmu) +{ + struct dentry *dent; + char file_name[S2FNAMESZ]; + + snprintf(file_name, sizeof(file_name), "0x%llx-0x%llx-s2-%sabled", + mmu->tlb_vttbr, + mmu->tlb_vtcr, + mmu->nested_stage2_enabled ? "en" : "dis"); + + dent =3D debugfs_create_file(file_name, 0400, + mmu->arch->debugfs_nv_dentry, mmu, + &kvm_ptdump_guest_fops); + + mmu->shadow_pt_debugfs_dentry =3D dent; +} + +void kvm_nested_s2_ptdump_remove_debugfs(struct kvm_s2_mmu *mmu) +{ + debugfs_remove(mmu->shadow_pt_debugfs_dentry); +} + void kvm_s2_ptdump_create_debugfs(struct kvm *kvm) { debugfs_create_file("stage2_page_tables", 0400, kvm->debugfs_dentry, @@ -285,4 +309,7 @@ void kvm_s2_ptdump_create_debugfs(struct kvm *kvm) &kvm->arch.mmu, &kvm_pgtable_range_fops); debugfs_create_file("stage2_levels", 0400, kvm->debugfs_dentry, &kvm->arch.mmu, &kvm_pgtable_levels_fops); + if (cpus_have_final_cap(ARM64_HAS_NESTED_VIRT)) + kvm->arch.debugfs_nv_dentry =3D + debugfs_create_dir("nested", kvm->debugfs_dentry); } --=20 2.43.0