KVM: arm64: nv: Expose shadow page tables in debugfs
cover KVM: arm64: nv: Expose shadow page tables in debugfs
[sorry for the resend, I generated the cover letter with the wrong commits previously, I should automate this :P]
Hi,
This series exposes shadow page tables in debugfs to improve the debugability and testability of NV. First we teach the KVM ptdump code to handle different s2 mmus, then shadow page tables are exposed. This is gated also behind CONFIG_PTDUMP_STAGE2_DEBUGFS, like the canonical stage-2 page table.
The file names for the shadow page tables are formatted 0x<vttbr>-0x<vtcr>-s2-{en,dis}abled, which is the most intuitive way of identifying the s2 mmus but not terribly clear. For now I don't think of other options that are meaningfully better, any suggestion welcome.
I realized I tend to write long variable and function names, happy to change if anyone finds it too verbose.
Thanks!
Wei-Lin Chang (2):
KVM: arm64: ptdump: Make KVM ptdump code s2 mmu aware
KVM: arm64: nv: Expose shadow page tables in debugfs
arch/arm64/include/asm/kvm_host.h | 7 ++++ arch/arm64/include/asm/kvm_mmu.h | 4 +++ arch/arm64/kvm/nested.c | 6 +++- arch/arm64/kvm/ptdump.c | 60 ++++++++++++++++++++++--------- 4 files changed, 60 insertions(+), 17 deletions(-)
Signature · 1 line
2.43.0
PATCH RESEND 1/2 KVM: arm64: ptdump: Make KVM ptdump code s2 mmu aware
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.
--- arch/arm64/kvm/ptdump.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-)
arch/arm64/kvm/ptdump.c +17 -16
@@ -18,7 +18,7 @@#define KVM_PGTABLE_MAX_LEVELS (KVM_PGTABLE_LAST_LEVEL + 1)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_level *level, u32 start_lvl)return 0 ; } -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 = &kvm->arch.mmu;struct kvm_pgtable *pgtable = mmu->pgt;int ret;
@@ -133,7 +132,7 @@ static struct kvm_ptdump_guest_state *kvm_ptdump_parser_create(struct kvm *kvm) st->ipa_marker[1 ].start_address =BIT (pgtable->ia_bits); st->range[0 ].end =BIT (pgtable->ia_bits); - st->kvm = kvm; + st->mmu = mmu; st->parser_state = (struct ptdump_pg_state ) { .marker = &st->ipa_marker[0 ], .level =-1 ,
@@ -149,8 +148,8 @@ static int kvm_ptdump_guest_show(struct seq_file *m, void *unused) {int ret;struct kvm_ptdump_guest_state *st = m->private; -struct kvm *kvm = st->kvm; -struct kvm_s2_mmu *mmu = &kvm->arch.mmu; +struct kvm_s2_mmu *mmu = st->mmu; +struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu);struct ptdump_pg_state *parser_state = &st->parser_state;struct kvm_pgtable_walker walker = (struct kvm_pgtable_walker ) { .cb = kvm_ptdump_visitor,
@@ -169,14 +168,15 @@ static int kvm_ptdump_guest_show(struct seq_file *m, void *unused)static int kvm_ptdump_guest_open (struct inode *m,struct file *file) { -struct kvm *kvm = m->i_private; +struct kvm_s2_mmu *mmu = m->i_private; +struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu);struct kvm_ptdump_guest_state *st;int ret;if (!kvm_get_kvm_safe (kvm))return -ENOENT ; - st =kvm_ptdump_parser_create (kvm); + st =kvm_ptdump_parser_create (mmu);if (IS_ERR (st)) { ret =PTR_ERR (st); goto err_with_kvm_ref;
@@ -194,7 +194,7 @@ static int kvm_ptdump_guest_open(struct inode *m, struct file *file)static int kvm_ptdump_guest_close (struct inode *m,struct file *file) { -struct kvm *kvm = m->i_private; +struct kvm *kvm =kvm_s2_mmu_to_kvm (m->i_private);void *st = ((struct seq_file *)file->private_data)->private;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 = m->i_private; +struct kvm_s2_mmu *mmu = m->i_private; +struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu);struct kvm_pgtable *pgtable;int ret;if (!kvm_get_kvm_safe (kvm))return -ENOENT ; - pgtable = kvm->arch.mmu.pgt; + pgtable = mmu->pgt; ret =single_open (file, show, pgtable);if (ret <0 )
@@ -256,7 +257,7 @@ static int kvm_pgtable_levels_open(struct inode *m, struct file *file)static int kvm_pgtable_debugfs_close (struct inode *m,struct file *file) { -struct kvm *kvm = m->i_private; +struct kvm *kvm =kvm_s2_mmu_to_kvm (m->i_private);kvm_put_kvm (kvm);return single_release (m, file);
@@ -279,9 +280,9 @@ static const struct file_operations kvm_pgtable_levels_fops = {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); }
Signature · 1 line
2.43.0
PATCH RESEND 2/2 KVM: arm64: nv: Expose shadow page tables in debugfs
In reply to Wei-Lin ChangExposing 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<vttbr>-0x<vtcr>-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.
--- arch/arm64/include/asm/kvm_host.h | 7 +++++++ arch/arm64/include/asm/kvm_mmu.h | 4 ++++ arch/arm64/kvm/nested.c | 6 +++++- arch/arm64/kvm/ptdump.c | 27 +++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_host.h +7 -0
@@ -217,6 +217,10 @@ struct kvm_s2_mmu {*/ bool nested_stage2_enabled; +#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,9 @@ struct kvm_arch {* the associated pKVM instance in the hypervisor. */ struct kvm_protected_vm pkvm; + +/* Nested virtualization info */ +struct dentry *debugfs_nv_dentry; };struct kvm_vcpu_fault_info {
arch/arm64/include/asm/kvm_mmu.h +4 -0
@@ -393,8 +393,12 @@ static inline bool kvm_supports_cacheable_pfnmap(void)#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 */ #endif /* __ASSEMBLER__ */
arch/arm64/kvm/nested.c +5 -1
@@ -730,8 +730,10 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu) kvm->arch.nested_mmus_next = (i +1 ) % kvm->arch.nested_mmus_size;/* 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 = true; + }/* * 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 =vcpu_read_sys_reg (vcpu,VTCR_EL2 ); s2_mmu->nested_stage2_enabled =vcpu_read_sys_reg (vcpu,HCR_EL2 ) &HCR_VM ; +kvm_nested_s2_ptdump_create_debugfs (s2_mmu); + out:atomic_inc (&s2_mmu->refcnt);
arch/arm64/kvm/ptdump.c +27 -0
@@ -10,6 +10,7 @@#include <linux/kvm_host.h> #include <linux/seq_file.h> +#include <asm/cpufeature.h> #include <asm/kvm_mmu.h> #include <asm/kvm_pgtable.h> #include <asm/ptdump.h>
@@ -277,6 +278,29 @@ static const struct file_operations kvm_pgtable_levels_fops = { .release = kvm_pgtable_debugfs_close, }; +void kvm_nested_s2_ptdump_create_debugfs (struct kvm_s2_mmu *mmu) +{ +struct dentry *dent; +/* format: 0x<vttbr>-0x<vtcr>-s2-{en, dis}abled\0 */ +char file_name[2 +16 +1 +2 +16 +4 +3 +6 ]; + +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 =debugfs_create_file (file_name,0400 , + mmu->arch->debugfs_nv_dentry, mmu, + &kvm_ptdump_guest_fops); + + mmu->shadow_pt_debugfs_dentry = 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 = +debugfs_create_dir ("nested" , kvm->debugfs_dentry); }
Signature · 1 line
2.43.0
Joey Gouly
reviewerQuoted 36 lines · Wei-Lin Chang
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<vttbr>-0x<vtcr>-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 <[email protected]>Signed-off-by Wei-Lin Chang <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 7 +++++++ arch/arm64/include/asm/kvm_mmu.h | 4 ++++ arch/arm64/kvm/nested.c | 6 +++++- arch/arm64/kvm/ptdump.c | 27 +++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-)arch/arm64/include/asm/kvm_host.h +4
-0@@ -217,6 +217,10 @@ struct kvm_s2_mmu {*/ bool nested_stage2_enabled; +#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.
arch/arm64/include/asm/kvm_host.h struct kvm_arch { · line 414
Quoted 6 lines
arch/arm64/include/asm/kvm_host.h +3
-0@@ -405,6 +409,9 @@ struct kvm_arch {* the associated pKVM instance in the hypervisor. */ struct kvm_protected_vm pkvm; + +/* Nested virtualization info */ +struct dentry *debugfs_nv_dentry;
Seems like this could be guarded with CONFIG_PTDUMP_STAGE2_DEBUGFS too, or is there a reason you didn't?
Thanks,
Joey
Quoted 98 lines
arch/arm64/include/asm/kvm_host.h +0
-0};struct kvm_vcpu_fault_info {arch/arm64/include/asm/kvm_mmu.h +4
-0@@ -393,8 +393,12 @@ static inline bool kvm_supports_cacheable_pfnmap(void)#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 */ #endif /* __ASSEMBLER__ */ arch/arm64/kvm/nested.c +5
-1@@ -730,8 +730,10 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu) kvm->arch.nested_mmus_next = (i +1 ) % kvm->arch.nested_mmus_size;/* 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 = true; + }/* * 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 =vcpu_read_sys_reg (vcpu,VTCR_EL2 ); s2_mmu->nested_stage2_enabled =vcpu_read_sys_reg (vcpu,HCR_EL2 ) &HCR_VM ; +kvm_nested_s2_ptdump_create_debugfs (s2_mmu); + out:atomic_inc (&s2_mmu->refcnt);arch/arm64/kvm/ptdump.c +27
-0@@ -10,6 +10,7 @@#include <linux/kvm_host.h> #include <linux/seq_file.h> +#include <asm/cpufeature.h> #include <asm/kvm_mmu.h> #include <asm/kvm_pgtable.h> #include <asm/ptdump.h> @@ -277,6 +278,29 @@ static const struct file_operations kvm_pgtable_levels_fops = { .release = kvm_pgtable_debugfs_close, }; +void kvm_nested_s2_ptdump_create_debugfs (struct kvm_s2_mmu *mmu) +{ +struct dentry *dent; +/* format: 0x<vttbr>-0x<vtcr>-s2-{en, dis}abled\0 */ +char file_name[2 +16 +1 +2 +16 +4 +3 +6 ]; + +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 =debugfs_create_file (file_name,0400 , + mmu->arch->debugfs_nv_dentry, mmu, + &kvm_ptdump_guest_fops); + + mmu->shadow_pt_debugfs_dentry = 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 = +debugfs_create_dir ("nested" , kvm->debugfs_dentry); }Signature · 1 line
2.43.0
Quoted 38 lines · Joey Gouly
Quoted 37 lines · Wei-Lin Chang
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<vttbr>-0x<vtcr>-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 <[email protected]>Signed-off-by Wei-Lin Chang <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 7 +++++++ arch/arm64/include/asm/kvm_mmu.h | 4 ++++ arch/arm64/kvm/nested.c | 6 +++++- arch/arm64/kvm/ptdump.c | 27 +++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-)arch/arm64/include/asm/kvm_host.h +4
-0@@ -217,6 +217,10 @@ struct kvm_s2_mmu {*/ bool nested_stage2_enabled; +#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,9 @@ struct kvm_arch {* the associated pKVM instance in the hypervisor.
Quoted 8 lines
arch/arm64/include/asm/kvm_host.h struct kvm_arch { · line 414
Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +3
-0*/ struct kvm_protected_vm pkvm; + +/* Nested virtualization info */ +struct dentry *debugfs_nv_dentry;Seems like this could be guarded with
CONFIG_PTDUMP_STAGE2_DEBUGFStoo, or is there a reason you didn't?
Right, I was thinking this "nested" directory can also hold other NV related information in the future, that's why I didn't keep it behind the option.
Thanks,
Wei-Lin Chang
Quoted 102 lines
Thanks,
JoeyQuoted 98 lines
arch/arm64/include/asm/kvm_host.h +0
-0};struct kvm_vcpu_fault_info {arch/arm64/include/asm/kvm_mmu.h +4
-0@@ -393,8 +393,12 @@ static inline bool kvm_supports_cacheable_pfnmap(void)#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 */ #endif /* __ASSEMBLER__ */ arch/arm64/kvm/nested.c +5
-1@@ -730,8 +730,10 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu) kvm->arch.nested_mmus_next = (i +1 ) % kvm->arch.nested_mmus_size;/* 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 = true; + }/* * 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 =vcpu_read_sys_reg (vcpu,VTCR_EL2 ); s2_mmu->nested_stage2_enabled =vcpu_read_sys_reg (vcpu,HCR_EL2 ) &HCR_VM ; +kvm_nested_s2_ptdump_create_debugfs (s2_mmu); + out:atomic_inc (&s2_mmu->refcnt);arch/arm64/kvm/ptdump.c +27
-0@@ -10,6 +10,7 @@#include <linux/kvm_host.h> #include <linux/seq_file.h> +#include <asm/cpufeature.h> #include <asm/kvm_mmu.h> #include <asm/kvm_pgtable.h> #include <asm/ptdump.h> @@ -277,6 +278,29 @@ static const struct file_operations kvm_pgtable_levels_fops = { .release = kvm_pgtable_debugfs_close, }; +void kvm_nested_s2_ptdump_create_debugfs (struct kvm_s2_mmu *mmu) +{ +struct dentry *dent; +/* format: 0x<vttbr>-0x<vtcr>-s2-{en, dis}abled\0 */ +char file_name[2 +16 +1 +2 +16 +4 +3 +6 ]; + +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 =debugfs_create_file (file_name,0400 , + mmu->arch->debugfs_nv_dentry, mmu, + &kvm_ptdump_guest_fops); + + mmu->shadow_pt_debugfs_dentry = 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 = +debugfs_create_dir ("nested" , kvm->debugfs_dentry); }Signature · 1 line
2.43.0
Marc Zyngier
maintainerQuoted 29 lines · Wei-Lin Chang
Quoted 17 lines · Joey Gouly
Quoted 17 lines
arch/arm64/include/asm/kvm_host.h +4
-0@@ -217,6 +217,10 @@ struct kvm_s2_mmu {*/ bool nested_stage2_enabled; +#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,9 @@ struct kvm_arch {* the associated pKVM instance in the hypervisor. Quoted 8 lines
arch/arm64/include/asm/kvm_host.h struct kvm_arch { · line 414
Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +3
-0*/ struct kvm_protected_vm pkvm; + +/* Nested virtualization info */ +struct dentry *debugfs_nv_dentry;Seems like this could be guarded with
CONFIG_PTDUMP_STAGE2_DEBUGFStoo, or is there a reason you didn't?
Quoted 3 lines
Right, I was thinking this "nested" directory can also hold other NV related information in the future, that's why I didn't keep it behind the option.
We can cross that bridge when we get there. In the meantime, I'd side with Joey and would rather not have a dangling pointer.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Quoted 31 lines · Marc Zyngier
Quoted 29 lines · Wei-Lin Chang
Quoted 25 lines · Joey Gouly
Quoted 16 lines
arch/arm64/include/asm/kvm_host.h +4
-0@@ -217,6 +217,10 @@ struct kvm_s2_mmu {*/ bool nested_stage2_enabled; +#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. arch/arm64/include/asm/kvm_host.h struct kvm_arch { · line 414
Quoted 6 lines
arch/arm64/include/asm/kvm_host.h +3
-0@@ -405,6 +409,9 @@ struct kvm_arch {* the associated pKVM instance in the hypervisor. */ struct kvm_protected_vm pkvm; + +/* Nested virtualization info */ +struct dentry *debugfs_nv_dentry;Seems like this could be guarded with
CONFIG_PTDUMP_STAGE2_DEBUGFStoo, or is there a reason you didn't?
Quoted 6 lines
Quoted 3 lines
Right, I was thinking this "nested" directory can also hold other NV related information in the future, that's why I didn't keep it behind the option.
We can cross that bridge when we get there. In the meantime, I'd side with Joey and would rather not have a dangling pointer.
Sure!
Let me guard it with CONFIG_PTDUMP_STAGE2_DEBUGFS too.
Thanks,
Wei-Lin Chang
Quoted 7 lines
Thanks,
M.Signature · 1 line
Without deviation from the norm, progress is not possible.
Quoted 11 lines · Wei-Lin Chang
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<vttbr>-0x<vtcr>-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.
Quoted 8 lines
Suggested-by Marc Zyngier <[email protected]>Signed-off-by Wei-Lin Chang <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 7 +++++++ arch/arm64/include/asm/kvm_mmu.h | 4 ++++ arch/arm64/kvm/nested.c | 6 +++++- arch/arm64/kvm/ptdump.c | 27 +++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-)
Hi,
Quoted 81 lines
arch/arm64/include/asm/kvm_host.h +7
-0@@ -217,6 +217,10 @@ struct kvm_s2_mmu {*/ bool nested_stage2_enabled; +#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,9 @@ struct kvm_arch {* the associated pKVM instance in the hypervisor. */ struct kvm_protected_vm pkvm; + +/* Nested virtualization info */ +struct dentry *debugfs_nv_dentry; };struct kvm_vcpu_fault_info {arch/arm64/include/asm/kvm_mmu.h +4
-0@@ -393,8 +393,12 @@ static inline bool kvm_supports_cacheable_pfnmap(void)#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 */ #endif /* __ASSEMBLER__ */ arch/arm64/kvm/nested.c +5
-1@@ -730,8 +730,10 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu) kvm->arch.nested_mmus_next = (i +1 ) % kvm->arch.nested_mmus_size;/* 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 = true; + }/* * 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 =vcpu_read_sys_reg (vcpu,VTCR_EL2 ); s2_mmu->nested_stage2_enabled =vcpu_read_sys_reg (vcpu,HCR_EL2 ) &HCR_VM ; +kvm_nested_s2_ptdump_create_debugfs (s2_mmu); + out:atomic_inc (&s2_mmu->refcnt);arch/arm64/kvm/ptdump.c +1
-0@@ -10,6 +10,7 @@#include <linux/kvm_host.h> #include <linux/seq_file.h> +#include <asm/cpufeature.h> #include <asm/kvm_mmu.h> #include <asm/kvm_pgtable.h> #include <asm/ptdump.h>
arch/arm64/kvm/ptdump.c static const struct file_operations kvm_pgtable_levels_fops = { · line 285
Quoted 8 lines
arch/arm64/kvm/ptdump.c +5
-0@@ -277,6 +278,29 @@ static const struct file_operations kvm_pgtable_levels_fops = { .release = kvm_pgtable_debugfs_close, }; +void kvm_nested_s2_ptdump_create_debugfs (struct kvm_s2_mmu *mmu) +{ +struct dentry *dent; +/* format: 0x<vttbr>-0x<vtcr>-s2-{en, dis}abled\0 */ +char file_name[2 +16 +1 +2 +16 +4 +3 +6 ];
Can you hide the size of this definition behind a macro ?
Quoted 22 lines
arch/arm64/kvm/ptdump.c +18
-0+ +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 =debugfs_create_file (file_name,0400 , + mmu->arch->debugfs_nv_dentry, mmu, + &kvm_ptdump_guest_fops); + + mmu->shadow_pt_debugfs_dentry = 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,
arch/arm64/kvm/ptdump.c void kvm_s2_ptdump_create_debugfs(struct kvm *kvm) · line 315
Quoted 7 lines
arch/arm64/kvm/ptdump.c +3
-0@@ -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 = +debugfs_create_dir ("nested" , kvm->debugfs_dentry); }
Other than that this looks good to me, thanks
Quoted 3 lines
Signature · 1 line
2.43.0
Quoted 107 lines · Sebastian Ene
Quoted 11 lines · Wei-Lin Chang
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<vttbr>-0x<vtcr>-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.Quoted 8 lines
Suggested-by Marc Zyngier <[email protected]>Signed-off-by Wei-Lin Chang <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 7 +++++++ arch/arm64/include/asm/kvm_mmu.h | 4 ++++ arch/arm64/kvm/nested.c | 6 +++++- arch/arm64/kvm/ptdump.c | 27 +++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-)Hi,
Quoted 84 lines
arch/arm64/include/asm/kvm_host.h +7
-0@@ -217,6 +217,10 @@ struct kvm_s2_mmu {*/ bool nested_stage2_enabled; +#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,9 @@ struct kvm_arch {* the associated pKVM instance in the hypervisor. */ struct kvm_protected_vm pkvm; + +/* Nested virtualization info */ +struct dentry *debugfs_nv_dentry; };struct kvm_vcpu_fault_info {arch/arm64/include/asm/kvm_mmu.h +4
-0@@ -393,8 +393,12 @@ static inline bool kvm_supports_cacheable_pfnmap(void)#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 */ #endif /* __ASSEMBLER__ */ arch/arm64/kvm/nested.c +5
-1@@ -730,8 +730,10 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu) kvm->arch.nested_mmus_next = (i +1 ) % kvm->arch.nested_mmus_size;/* 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 = true; + }/* * 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 =vcpu_read_sys_reg (vcpu,VTCR_EL2 ); s2_mmu->nested_stage2_enabled =vcpu_read_sys_reg (vcpu,HCR_EL2 ) &HCR_VM ; +kvm_nested_s2_ptdump_create_debugfs (s2_mmu); + out:atomic_inc (&s2_mmu->refcnt);arch/arm64/kvm/ptdump.c +1
-0@@ -10,6 +10,7 @@#include <linux/kvm_host.h> #include <linux/seq_file.h> +#include <asm/cpufeature.h> #include <asm/kvm_mmu.h> #include <asm/kvm_pgtable.h> #include <asm/ptdump.h> @@ -277,6 +278,29 @@ static const struct file_operations kvm_pgtable_levels_fops = { .release = kvm_pgtable_debugfs_close, };
Quoted 7 lines
arch/arm64/kvm/ptdump.c static const struct file_operations kvm_pgtable_levels_fops = { · line 285
Quoted 5 lines
arch/arm64/kvm/ptdump.c +5
-0+void kvm_nested_s2_ptdump_create_debugfs (struct kvm_s2_mmu *mmu) +{ +struct dentry *dent; +/* format: 0x<vttbr>-0x<vtcr>-s2-{en, dis}abled\0 */ +char file_name[2 +16 +1 +2 +16 +4 +3 +6 ];Can you hide the size of this definition behind a macro ?
Sure, no problem.
Quoted 32 lines
Quoted 23 lines
arch/arm64/kvm/ptdump.c +18
-0+ +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 =debugfs_create_file (file_name,0400 , + mmu->arch->debugfs_nv_dentry, mmu, + &kvm_ptdump_guest_fops); + + mmu->shadow_pt_debugfs_dentry = 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,arch/arm64/kvm/ptdump.c void kvm_s2_ptdump_create_debugfs(struct kvm *kvm) · line 315
Quoted 7 lines
arch/arm64/kvm/ptdump.c +3
-0@@ -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 = +debugfs_create_dir ("nested" , kvm->debugfs_dentry); }Other than that this looks good to me, thanks
Quoted 1 line
Reviewed-by Sebastian Ene <[email protected]>
Thanks for the review!
Thanks,
Wei-Lin Chang
Quoted 4 lines
Quoted 4 lines
Signature · 2 lines
-- 2.43.0
Marc Zyngier
maintainerQuoted 3 lines · Wei-Lin Chang
Quoted 1 line · Sebastian Ene
On Sun, Mar 08, 2026 at 11:18:29PM +0000, Wei-Lin Chang wrote:
[...]
Quoted 9 lines
Quoted 7 lines
arch/arm64/kvm/ptdump.c static const struct file_operations kvm_pgtable_levels_fops = { · line 285
Quoted 5 lines
arch/arm64/kvm/ptdump.c +5
-0+void kvm_nested_s2_ptdump_create_debugfs (struct kvm_s2_mmu *mmu) +{ +struct dentry *dent; +/* format: 0x<vttbr>-0x<vtcr>-s2-{en, dis}abled\0 */ +char file_name[2 +16 +1 +2 +16 +4 +3 +6 ];Can you hide the size of this definition behind a macro ?
Sure, no problem.
I don't think a macro helps that much on its own. I'd rather this is made a bit more foolproof by directly expressing the actual string this results into, and use that for the size:
#define S2FNAMESZ sizeof("0x0123456789abcdef-0x0123456789abcdef-s2-disabled")
char file_name[S2FNAME];which is less ambiguous. The compiler will happily optimise this to a nice constant that also accounts for the final '\0'.
Thanks,
M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
Quoted 22 lines · Marc Zyngier
Quoted 3 lines · Wei-Lin Chang
Quoted 1 line · Sebastian Ene
On Sun, Mar 08, 2026 at 11:18:29PM +0000, Wei-Lin Chang wrote:
[...]
Quoted 9 lines
Quoted 7 lines
arch/arm64/kvm/ptdump.c static const struct file_operations kvm_pgtable_levels_fops = { · line 285
Quoted 5 lines
arch/arm64/kvm/ptdump.c +5
-0+void kvm_nested_s2_ptdump_create_debugfs (struct kvm_s2_mmu *mmu) +{ +struct dentry *dent; +/* format: 0x<vttbr>-0x<vtcr>-s2-{en, dis}abled\0 */ +char file_name[2 +16 +1 +2 +16 +4 +3 +6 ];Can you hide the size of this definition behind a macro ?
Sure, no problem.
I don't think a macro helps that much on its own. I'd rather this is made a bit more foolproof by directly expressing the actual string this results into, and use that for the size:
Quoted 6 lines
#define S2FNAMESZ sizeof("0x0123456789abcdef-0x0123456789abcdef-s2-disabled") char file_name[S2FNAME];which is less ambiguous. The compiler will happily optimise this to a nice constant that also accounts for the final '\0'.
Thanks for the advice!
Makes sense to me, I'll change it to that.
Thanks,
Wei-Lin Chang
Quoted 7 lines
Thanks,
M.Signature · 1 line
Jazz isn't dead. It just smells funny.