Skip to contentSkip to conversation

KVM: arm64: ptdump: Shadow ptdump fixes

PATCH Superseded Wei-Lin Chang 4 participants 25 messages Jun 30 – Jul 17

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover KVM: arm64: ptdump: Shadow ptdump fixes

Hi,

This is v2 of fixing shadow ptdump debugfs files. Unfortunately I couldn't make per mmu ptdump files work after all, mainly because there isn't a clean way to locate the specific nested mmu for each ptdump file as the nested mmus could be freed when the file gets opened. Therefore in this series a single file "shadow_page_tables" is created that dumps all valid mmus' page table information.

An advantage of this is that this new ptdump file have a lifetime identical to other ptdump files i.e. stage2_page_tables, ipa_range, etc., hence avoiding the dentry UAF found last time [1].

With this all ptdump files are only removed when the last kvm reference gets dropped and kvm_destroy_vm_debugfs() is called, in their open(), show() functions the nested mmu array and mmu->pgt are checked with mmu_lock held to prevent UAF.

Patch 1-2: Undo previous shadow ptdump implementation.
Patch 3:   Fix a mmu->pgt UAF that happens when ptdump files are read after
           mmu->pgt is freed.
Patch 4-5: Preparation for the shadow page table dump file.
Patch 6:   Implementation of the shadow page table dump file.

The fixes are tested with CONFIG_PROVE_LOCKING,
CONFIG_DEBUG_ATOMIC_SLEEP, and CONFIG_KASAN.

Thanks!

* Changes from v1 ([2]):

- Move from per mmu ptdump files to one file that will dump all shadow page tables.

[1]: https://lore.kernel.org/kvmarm/ajty6I7ZqodP4ous@sm-arm-grace07/
[2]: https://lore.kernel.org/kvmarm/[email protected]/

Wei-Lin Chang (6):
KVM: arm64: ptdump: Remove shadow ptdump files
KVM: arm64: ptdump: Undo making the ptdump code mmu aware
KVM: arm64: ptdump: Fix UAF when mmu->pgt is freed
KVM: arm64: ptdump: Factor out initialization of
kvm_ptdump_guest_state
KVM: arm64: ptdump: Extract kvm_ptdump_guest_open() from canonical
ptdump path
KVM: arm64: ptdump: Introduce the shadow ptdump file

 arch/arm64/include/asm/kvm_host.h |   5 +-
 arch/arm64/include/asm/kvm_mmu.h  |   4 -
 arch/arm64/kvm/nested.c           |  18 +--
 arch/arm64/kvm/ptdump.c           | 185 ++++++++++++++++++++----------
 4 files changed, 135 insertions(+), 77 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/6 KVM: arm64: ptdump: Remove shadow ptdump files

Previously we exposed shadow page tables by creating a debugfs ptdump file whenever a nested mmu instance gets bound to a new context, and deleting the debugfs file whose context was getting unbound.

This turned out to be buggy, as the instance<->context binding process is done with the mmu_lock held, and debugfs creation/deletion can sleep.

Revert most of commit 19e15dc73f0f ("KVM: arm64: nv: Expose shadow page tables in debugfs"), keep the "nested" debugfs directory for use in a later patch where we'll expose the shadow ptdump in another way.

Fixes 19e15dc73f0f ("KVM: arm64: nv: Expose shadow page tables in debugfs")
---
 arch/arm64/include/asm/kvm_host.h |  5 +----
 arch/arm64/include/asm/kvm_mmu.h  |  4 ----
 arch/arm64/kvm/nested.c           |  6 +-----
 arch/arm64/kvm/ptdump.c           | 23 -----------------------
 4 files changed, 2 insertions(+), 36 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -4
index 2faa60df847d..94bced53a323 100644
@@ -217,10 +217,6 @@ 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.
@@ -424,6 +420,7 @@ struct kvm_arch {
 	/* Nested virtualization info */
 	struct dentry *debugfs_nv_dentry;
 #endif
+
 };
 
 struct kvm_vcpu_fault_info {
arch/arm64/include/asm/kvm_mmu.h +0 -4
index 6eae7e7e2a68..c1e535e3d931 100644
@@ -392,12 +392,8 @@ 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 +1 -5
index faa4a48f265d..6435efd65cb5 100644
@@ -834,10 +834,8 @@ 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)) {
-		kvm_nested_s2_ptdump_remove_debugfs(s2_mmu);
+	if (kvm_s2_mmu_valid(s2_mmu))
 		s2_mmu->pending_unmap = true;
-	}
 
 	/*
 	 * The virtual VMID (modulo CnP) will be used as a key when matching
@@ -851,8 +849,6 @@ 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 +0 -23
index c9140e22abcf..7c32f1f7772c 100644
@@ -17,7 +17,6 @@
 
 #define MARKERS_LEN		2
 #define KVM_PGTABLE_MAX_LEVELS	(KVM_PGTABLE_LAST_LEVEL + 1)
-#define S2FNAMESZ		sizeof("0x0123456789abcdef-0x0123456789abcdef-s2-disabled")
 
 struct kvm_ptdump_guest_state {
 	struct kvm_s2_mmu	*mmu;
@@ -273,28 +272,6 @@ 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;
-	char file_name[S2FNAMESZ];
-
-	snprintf(file_name, sizeof(file_name), "0x%016llx-0x%016llx-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,
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/6 KVM: arm64: ptdump: Undo making the ptdump code mmu aware

In reply to Wei-Lin Chang

Commit 204f7c018d76 ("KVM: arm64: ptdump: Make KVM ptdump code s2 mmu aware") changed the ptdump code from storing the kvm pointer to storing the mmu pointer, in order to reuse code for shadow ptdumps.

This turned out to be buggy as the nested mmus can be freed before the last access to the ptdump files. To prepare for a new implementation of the shadow ptdumps which solves this problem, revert the effects of the commit to avoid this UAF.

---
 arch/arm64/kvm/ptdump.c | 32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)
arch/arm64/kvm/ptdump.c +15 -17
index 7c32f1f7772c..d5aa9eff08d1 100644
@@ -19,7 +19,7 @@
 #define KVM_PGTABLE_MAX_LEVELS	(KVM_PGTABLE_LAST_LEVEL + 1)
 
 struct kvm_ptdump_guest_state {
-	struct kvm_s2_mmu	*mmu;
+	struct kvm		*kvm;
 	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,10 @@ 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_s2_mmu *mmu)
+static struct kvm_ptdump_guest_state *kvm_ptdump_parser_create(struct kvm *kvm)
 {
 	struct kvm_ptdump_guest_state *st;
-	struct kvm_pgtable *pgtable = mmu->pgt;
+	struct kvm_pgtable *pgtable = kvm->arch.mmu.pgt;
 	int ret;
 
 	st = kzalloc_obj(struct kvm_ptdump_guest_state, GFP_KERNEL_ACCOUNT);
@@ -131,7 +131,7 @@ static struct kvm_ptdump_guest_state *kvm_ptdump_parser_create(struct kvm_s2_mmu
 	st->ipa_marker[0].name		= "Guest IPA";
 	st->ipa_marker[1].start_address = BIT(pgtable->ia_bits);
 
-	st->mmu				= mmu;
+	st->kvm				= kvm;
 	return st;
 }
 
@@ -139,8 +139,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_s2_mmu *mmu = st->mmu;
-	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
+	struct kvm *kvm = st->kvm;
+	struct kvm_s2_mmu *mmu = &kvm->arch.mmu;
 	struct kvm_pgtable_walker walker = (struct kvm_pgtable_walker) {
 		.cb	= kvm_ptdump_visitor,
 		.arg	= &st->parser_state,
@@ -163,15 +163,14 @@ 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_s2_mmu *mmu = m->i_private;
-	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
+	struct kvm *kvm = m->i_private;
 	struct kvm_ptdump_guest_state *st;
 	int ret;
 
 	if (!kvm_get_kvm_safe(kvm))
 		return -ENOENT;
 
-	st = kvm_ptdump_parser_create(mmu);
+	st = kvm_ptdump_parser_create(kvm);
 	if (IS_ERR(st)) {
 		ret = PTR_ERR(st);
 		goto err_with_kvm_ref;
@@ -189,7 +188,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 = kvm_s2_mmu_to_kvm(m->i_private);
+	struct kvm *kvm = m->i_private;
 	void *st = ((struct seq_file *)file->private_data)->private;
 
 	kfree(st);
@@ -224,15 +223,14 @@ 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_s2_mmu *mmu = m->i_private;
-	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
+	struct kvm *kvm = m->i_private;
 	struct kvm_pgtable *pgtable;
 	int ret;
 
 	if (!kvm_get_kvm_safe(kvm))
 		return -ENOENT;
 
-	pgtable = mmu->pgt;
+	pgtable = kvm->arch.mmu.pgt;
 
 	ret = single_open(file, show, pgtable);
 	if (ret < 0)
@@ -252,7 +250,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 = kvm_s2_mmu_to_kvm(m->i_private);
+	struct kvm *kvm = m->i_private;
 
 	kvm_put_kvm(kvm);
 	return single_release(m, file);
@@ -275,11 +273,11 @@ 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->arch.mmu, &kvm_ptdump_guest_fops);
+			    kvm, &kvm_ptdump_guest_fops);
 	debugfs_create_file("ipa_range", 0400, kvm->debugfs_dentry,
-			    &kvm->arch.mmu, &kvm_pgtable_range_fops);
+			    kvm, &kvm_pgtable_range_fops);
 	debugfs_create_file("stage2_levels", 0400, kvm->debugfs_dentry,
-			    &kvm->arch.mmu, &kvm_pgtable_levels_fops);
+			    kvm, &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
#3 lore raw

PATCH v2 3/6 KVM: arm64: ptdump: Fix UAF when mmu->pgt is freed

In reply to Wei-Lin Chang

ptdump files can still be read after the pgt of the canonical mmu is freed, if they are opened before the VM debugfs directory is removed. This triggers UAF in places where we cache the pgt pointer or access it without checking its validity.

Check the pgt is still alive under the mmu_lock before accessing the pgt.

---
 arch/arm64/kvm/ptdump.c | 38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)
arch/arm64/kvm/ptdump.c +24 -14
index d5aa9eff08d1..752d8e0cd25c 100644
@@ -115,13 +115,21 @@ static int kvm_ptdump_build_levels(struct ptdump_pg_level *level, u32 start_lvl)
 static struct kvm_ptdump_guest_state *kvm_ptdump_parser_create(struct kvm *kvm)
 {
 	struct kvm_ptdump_guest_state *st;
-	struct kvm_pgtable *pgtable = kvm->arch.mmu.pgt;
+	struct kvm_pgtable *pgtable;
 	int ret;
 
 	st = kzalloc_obj(struct kvm_ptdump_guest_state, GFP_KERNEL_ACCOUNT);
 	if (!st)
 		return ERR_PTR(-ENOMEM);
 
+	guard(write_lock)(&kvm->mmu_lock);
+	if (!kvm->arch.mmu.pgt) {
+		kfree(st);
+		return ERR_PTR(-ENXIO);
+	}
+
+	pgtable = kvm->arch.mmu.pgt;
+
 	ret = kvm_ptdump_build_levels(&st->level[0], pgtable->start_level);
 	if (ret) {
 		kfree(st);
@@ -137,7 +145,6 @@ static struct kvm_ptdump_guest_state *kvm_ptdump_parser_create(struct kvm *kvm)
 
 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;
@@ -154,11 +161,11 @@ static int kvm_ptdump_guest_show(struct seq_file *m, void *unused)
 		.seq		= m,
 	};
 
-	write_lock(&kvm->mmu_lock);
-	ret = kvm_pgtable_walk(mmu->pgt, 0, BIT(mmu->pgt->ia_bits), &walker);
-	write_unlock(&kvm->mmu_lock);
+	guard(write_lock)(&kvm->mmu_lock);
+	if (mmu->pgt)
+		return kvm_pgtable_walk(mmu->pgt, 0, BIT(mmu->pgt->ia_bits), &walker);
 
-	return ret;
+	return 0;
 }
 
 static int kvm_ptdump_guest_open(struct inode *m, struct file *file)
@@ -206,17 +213,23 @@ static const struct file_operations kvm_ptdump_guest_fops = {
 
 static int kvm_pgtable_range_show(struct seq_file *m, void *unused)
 {
-	struct kvm_pgtable *pgtable = m->private;
+	struct kvm *kvm = m->private;
+
+	guard(write_lock)(&kvm->mmu_lock);
+	if (kvm->arch.mmu.pgt)
+		seq_printf(m, "%2u\n", kvm->arch.mmu.pgt->ia_bits);
 
-	seq_printf(m, "%2u\n", pgtable->ia_bits);
 	return 0;
 }
 
 static int kvm_pgtable_levels_show(struct seq_file *m, void *unused)
 {
-	struct kvm_pgtable *pgtable = m->private;
+	struct kvm *kvm = m->private;
+
+	guard(write_lock)(&kvm->mmu_lock);
+	if (kvm->arch.mmu.pgt)
+		seq_printf(m, "%1d\n", KVM_PGTABLE_MAX_LEVELS - kvm->arch.mmu.pgt->start_level);
 
-	seq_printf(m, "%1d\n", KVM_PGTABLE_MAX_LEVELS - pgtable->start_level);
 	return 0;
 }
 
@@ -224,15 +237,12 @@ 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_pgtable *pgtable;
 	int ret;
 
 	if (!kvm_get_kvm_safe(kvm))
 		return -ENOENT;
 
-	pgtable = kvm->arch.mmu.pgt;
-
-	ret = single_open(file, show, pgtable);
+	ret = single_open(file, show, kvm);
 	if (ret < 0)
 		kvm_put_kvm(kvm);
 	return ret;
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v2 3/6] KVM: arm64: ptdump: Fix UAF when mmu->pgt is freed

Hi Wei Lin,

Quoted 51 lines · Wei-Lin Chang

ptdump files can still be read after the pgt of the canonical mmu is freed, if they are opened before the VM debugfs directory is removed. This triggers UAF in places where we cache the pgt pointer or access it without checking its validity.

Check the pgt is still alive under the mmu_lock before accessing the pgt.

---
 arch/arm64/kvm/ptdump.c | 38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)
arch/arm64/kvm/ptdump.c +9 -2
index d5aa9eff08d1..752d8e0cd25c 100644
@@ -115,13 +115,21 @@ static int kvm_ptdump_build_levels(struct ptdump_pg_level *level, u32 start_lvl)
 static struct kvm_ptdump_guest_state *kvm_ptdump_parser_create(struct kvm *kvm)
 {
 	struct kvm_ptdump_guest_state *st;
-	struct kvm_pgtable *pgtable = kvm->arch.mmu.pgt;
+	struct kvm_pgtable *pgtable;
 	int ret;
 
 	st = kzalloc_obj(struct kvm_ptdump_guest_state, GFP_KERNEL_ACCOUNT);
 	if (!st)
 		return ERR_PTR(-ENOMEM);
 
+	guard(write_lock)(&kvm->mmu_lock);
+	if (!kvm->arch.mmu.pgt) {
+		kfree(st);
+		return ERR_PTR(-ENXIO);
+	}
+
+	pgtable = kvm->arch.mmu.pgt;
+
 	ret = kvm_ptdump_build_levels(&st->level[0], pgtable->start_level);
 	if (ret) {
 		kfree(st);
@@ -137,7 +145,6 @@ static struct kvm_ptdump_guest_state *kvm_ptdump_parser_create(struct kvm *kvm)
 
 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;

arch/arm64/kvm/ptdump.c static int kvm_ptdump_guest_show(struct seq_file *m, void *unused) · line 166

Quoted 9 lines
arch/arm64/kvm/ptdump.c +3 -3
@@ -154,11 +161,11 @@ static int kvm_ptdump_guest_show(struct seq_file *m, void *unused)
 		.seq		= m,
 	};
 
-	write_lock(&kvm->mmu_lock);
-	ret = kvm_pgtable_walk(mmu->pgt, 0, BIT(mmu->pgt->ia_bits), &walker);
-	write_unlock(&kvm->mmu_lock);
+	guard(write_lock)(&kvm->mmu_lock);
+	if (mmu->pgt)
+		return kvm_pgtable_walk(mmu->pgt, 0, BIT(mmu->pgt->ia_bits), &walker);

IIUC, that's the same behavior, right?
Just changed to look about the same with the rest of this file?

arch/arm64/kvm/ptdump.c static int kvm_ptdump_guest_show(struct seq_file *m, void *unused) · line 169

Quoted 4 lines
arch/arm64/kvm/ptdump.c +1 -1
-	return ret;
+	return 0;
 }

So if the pgt does not exist anymore, it returns zero. Is that the desired behavior?

I guess it's aligned with the idea of single file mentioned in the cover, right?

Quoted 34 lines
arch/arm64/kvm/ptdump.c +10 -4
 static int kvm_ptdump_guest_open(struct inode *m, struct file *file)
@@ -206,17 +213,23 @@ static const struct file_operations kvm_ptdump_guest_fops = {
 
 static int kvm_pgtable_range_show(struct seq_file *m, void *unused)
 {
-	struct kvm_pgtable *pgtable = m->private;
+	struct kvm *kvm = m->private;
+
+	guard(write_lock)(&kvm->mmu_lock);
+	if (kvm->arch.mmu.pgt)
+		seq_printf(m, "%2u\n", kvm->arch.mmu.pgt->ia_bits);
 
-	seq_printf(m, "%2u\n", pgtable->ia_bits);
 	return 0;
 }
 
 static int kvm_pgtable_levels_show(struct seq_file *m, void *unused)
 {
-	struct kvm_pgtable *pgtable = m->private;
+	struct kvm *kvm = m->private;
+
+	guard(write_lock)(&kvm->mmu_lock);
+	if (kvm->arch.mmu.pgt)
+		seq_printf(m, "%1d\n", KVM_PGTABLE_MAX_LEVELS - kvm->arch.mmu.pgt->start_level);
 
-	seq_printf(m, "%1d\n", KVM_PGTABLE_MAX_LEVELS - pgtable->start_level);
 	return 0;
 }
 
@@ -224,15 +237,12 @@ static int kvm_pgtable_debugfs_open(struct inode *m, struct file *file,
 				    int (*show)(struct seq_file *, void *))
 {
 	struct kvm *kvm = m->i_private;

arch/arm64/kvm/ptdump.c static int kvm_pgtable_debugfs_open(struct inode *m, struct file *file, · line 245

Quoted 10 lines
arch/arm64/kvm/ptdump.c +1 -4
-	struct kvm_pgtable *pgtable;
 	int ret;
 
 	if (!kvm_get_kvm_safe(kvm))
 		return -ENOENT;
 
-	pgtable = kvm->arch.mmu.pgt;
-
-	ret = single_open(file, show, pgtable);
+	ret = single_open(file, show, kvm);

Maybe this change is more related with the previous patch?

Quoted 6 lines
arch/arm64/kvm/ptdump.c +0 -0
 	if (ret < 0)
 		kvm_put_kvm(kvm);
 	return ret;
Signature · 1 line
2.43.0

Thanks!
Leo

#5 lore raw

Re: [PATCH v2 3/6] KVM: arm64: ptdump: Fix UAF when mmu->pgt is freed

Quoted 57 lines · Leonardo Bras

Hi Wei Lin,

Quoted 54 lines · Wei-Lin Chang

ptdump files can still be read after the pgt of the canonical mmu is freed, if they are opened before the VM debugfs directory is removed. This triggers UAF in places where we cache the pgt pointer or access it without checking its validity.

Check the pgt is still alive under the mmu_lock before accessing the pgt.

---
 arch/arm64/kvm/ptdump.c | 38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)
arch/arm64/kvm/ptdump.c +9 -2
index d5aa9eff08d1..752d8e0cd25c 100644
@@ -115,13 +115,21 @@ static int kvm_ptdump_build_levels(struct ptdump_pg_level *level, u32 start_lvl)
 static struct kvm_ptdump_guest_state *kvm_ptdump_parser_create(struct kvm *kvm)
 {
 	struct kvm_ptdump_guest_state *st;
-	struct kvm_pgtable *pgtable = kvm->arch.mmu.pgt;
+	struct kvm_pgtable *pgtable;
 	int ret;
 
 	st = kzalloc_obj(struct kvm_ptdump_guest_state, GFP_KERNEL_ACCOUNT);
 	if (!st)
 		return ERR_PTR(-ENOMEM);
 
+	guard(write_lock)(&kvm->mmu_lock);
+	if (!kvm->arch.mmu.pgt) {
+		kfree(st);
+		return ERR_PTR(-ENXIO);
+	}
+
+	pgtable = kvm->arch.mmu.pgt;
+
 	ret = kvm_ptdump_build_levels(&st->level[0], pgtable->start_level);
 	if (ret) {
 		kfree(st);
@@ -137,7 +145,6 @@ static struct kvm_ptdump_guest_state *kvm_ptdump_parser_create(struct kvm *kvm)
 
 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;
@@ -154,11 +161,11 @@ static int kvm_ptdump_guest_show(struct seq_file *m, void *unused)
 		.seq		= m,
 	};
 
Quoted 9 lines

arch/arm64/kvm/ptdump.c static int kvm_ptdump_guest_show(struct seq_file *m, void *unused) · line 166

Quoted 6 lines
arch/arm64/kvm/ptdump.c +3 -3
-	write_lock(&kvm->mmu_lock);
-	ret = kvm_pgtable_walk(mmu->pgt, 0, BIT(mmu->pgt->ia_bits), &walker);
-	write_unlock(&kvm->mmu_lock);
+	guard(write_lock)(&kvm->mmu_lock);
+	if (mmu->pgt)
+		return kvm_pgtable_walk(mmu->pgt, 0, BIT(mmu->pgt->ia_bits), &walker);

IIUC, that's the same behavior, right?
Just changed to look about the same with the rest of this file?

I'm not too sure what you are referring to, if you mean the write_lock/unlock -> guard(write_lock) change, then yes, mostly. Just also checking mmu->pgt is still not freed.

Quoted 8 lines

arch/arm64/kvm/ptdump.c static int kvm_ptdump_guest_show(struct seq_file *m, void *unused) · line 169

Quoted 5 lines
arch/arm64/kvm/ptdump.c +1 -1
-	return ret;
+	return 0;
 }

So if the pgt does not exist anymore, it returns zero. Is that the desired behavior?

Good question, so the question is what contract between the ptdump and user do we want to make for this case. I guess returning some error like -EIO could make a little more sense than just printing nothing?

Quoted 3 lines

I guess it's aligned with the idea of single file mentioned in the cover, right?

Sorry, I don't get what you are asking here?

Quoted 41 lines
Quoted 41 lines
arch/arm64/kvm/ptdump.c +10 -5
 static int kvm_ptdump_guest_open(struct inode *m, struct file *file)
@@ -206,17 +213,23 @@ static const struct file_operations kvm_ptdump_guest_fops = {
 
 static int kvm_pgtable_range_show(struct seq_file *m, void *unused)
 {
-	struct kvm_pgtable *pgtable = m->private;
+	struct kvm *kvm = m->private;
+
+	guard(write_lock)(&kvm->mmu_lock);
+	if (kvm->arch.mmu.pgt)
+		seq_printf(m, "%2u\n", kvm->arch.mmu.pgt->ia_bits);
 
-	seq_printf(m, "%2u\n", pgtable->ia_bits);
 	return 0;
 }
 
 static int kvm_pgtable_levels_show(struct seq_file *m, void *unused)
 {
-	struct kvm_pgtable *pgtable = m->private;
+	struct kvm *kvm = m->private;
+
+	guard(write_lock)(&kvm->mmu_lock);
+	if (kvm->arch.mmu.pgt)
+		seq_printf(m, "%1d\n", KVM_PGTABLE_MAX_LEVELS - kvm->arch.mmu.pgt->start_level);
 
-	seq_printf(m, "%1d\n", KVM_PGTABLE_MAX_LEVELS - pgtable->start_level);
 	return 0;
 }
 
@@ -224,15 +237,12 @@ 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_pgtable *pgtable;
 	int ret;
 
 	if (!kvm_get_kvm_safe(kvm))
 		return -ENOENT;
 
Quoted 6 lines

arch/arm64/kvm/ptdump.c static int kvm_pgtable_debugfs_open(struct inode *m, struct file *file, · line 245

Quoted 4 lines
arch/arm64/kvm/ptdump.c +1 -3
-	pgtable = kvm->arch.mmu.pgt;
-
-	ret = single_open(file, show, pgtable);
+	ret = single_open(file, show, kvm);

Maybe this change is more related with the previous patch?

I see your point, but I divided it into first fixing mmu UAF, then the pgt UAF, which I also think makes sense.

Thanks,
Wei-Lin Chang

Quoted 10 lines
Quoted 7 lines
arch/arm64/kvm/ptdump.c +0 -0
 	if (ret < 0)
 		kvm_put_kvm(kvm);
 	return ret;
Signature · 1 line
2.43.0

Thanks!
Leo

#6 lore raw

Re: [PATCH v2 3/6] KVM: arm64: ptdump: Fix UAF when mmu->pgt is freed

Quoted 68 lines · Wei-Lin Chang
Quoted 57 lines · Leonardo Bras

Hi Wei Lin,

Quoted 54 lines · Wei-Lin Chang

ptdump files can still be read after the pgt of the canonical mmu is freed, if they are opened before the VM debugfs directory is removed. This triggers UAF in places where we cache the pgt pointer or access it without checking its validity.

Check the pgt is still alive under the mmu_lock before accessing the pgt.

---
 arch/arm64/kvm/ptdump.c | 38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)
arch/arm64/kvm/ptdump.c +9 -2
index d5aa9eff08d1..752d8e0cd25c 100644
@@ -115,13 +115,21 @@ static int kvm_ptdump_build_levels(struct ptdump_pg_level *level, u32 start_lvl)
 static struct kvm_ptdump_guest_state *kvm_ptdump_parser_create(struct kvm *kvm)
 {
 	struct kvm_ptdump_guest_state *st;
-	struct kvm_pgtable *pgtable = kvm->arch.mmu.pgt;
+	struct kvm_pgtable *pgtable;
 	int ret;
 
 	st = kzalloc_obj(struct kvm_ptdump_guest_state, GFP_KERNEL_ACCOUNT);
 	if (!st)
 		return ERR_PTR(-ENOMEM);
 
+	guard(write_lock)(&kvm->mmu_lock);
+	if (!kvm->arch.mmu.pgt) {
+		kfree(st);
+		return ERR_PTR(-ENXIO);
+	}
+
+	pgtable = kvm->arch.mmu.pgt;
+
 	ret = kvm_ptdump_build_levels(&st->level[0], pgtable->start_level);
 	if (ret) {
 		kfree(st);
@@ -137,7 +145,6 @@ static struct kvm_ptdump_guest_state *kvm_ptdump_parser_create(struct kvm *kvm)
 
 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;
@@ -154,11 +161,11 @@ static int kvm_ptdump_guest_show(struct seq_file *m, void *unused)
 		.seq		= m,
 	};
 
Quoted 9 lines

arch/arm64/kvm/ptdump.c static int kvm_ptdump_guest_show(struct seq_file *m, void *unused) · line 166

Quoted 6 lines
arch/arm64/kvm/ptdump.c +3 -3
-	write_lock(&kvm->mmu_lock);
-	ret = kvm_pgtable_walk(mmu->pgt, 0, BIT(mmu->pgt->ia_bits), &walker);
-	write_unlock(&kvm->mmu_lock);
+	guard(write_lock)(&kvm->mmu_lock);
+	if (mmu->pgt)
+		return kvm_pgtable_walk(mmu->pgt, 0, BIT(mmu->pgt->ia_bits), &walker);

IIUC, that's the same behavior, right?
Just changed to look about the same with the rest of this file?

Quoted 3 lines

I'm not too sure what you are referring to, if you mean the write_lock/unlock -> guard(write_lock) change, then yes, mostly. Just also checking mmu->pgt is still not freed.

I was referring just to lock+unlock -> guard() strategy.
IIUC this has the same effect, so I was wondering on why you suggested the change.

I then supposed it was to look the same of the rest of the file.

Quoted 10 lines
Quoted 9 lines

arch/arm64/kvm/ptdump.c static int kvm_ptdump_guest_show(struct seq_file *m, void *unused) · line 169

Quoted 6 lines
arch/arm64/kvm/ptdump.c +1 -1
-	return ret;
+	return 0;
 }

So if the pgt does not exist anymore, it returns zero. Is that the desired behavior?

Quoted 4 lines

Good question, so the question is what contract between the ptdump and user do we want to make for this case. I guess returning some error like -EIO could make a little more sense than just printing nothing?

It depends on what is the behaviour you want to see... see below..

Quoted 5 lines
Quoted 3 lines

I guess it's aligned with the idea of single file mentioned in the cover, right?

Sorry, I don't get what you are asking here?

IIUC you mentioned in the cover letter that you planned to have a file which would, on read, output ptdump for every nested pgtable. Did I get that right?

If so, I imagine that the user has no idea how many nested pgtables are there, and if the ioctl initially finds N, but ends up printing N-1 (because a pgtable was not there anymore), that would not be an error that the user should be worried about.

It could be an issue if you were printing in multiple steps (multiple checks), as a part of that pgtable could be printed while another part could not. Which does not seem to be the case.

Quoted 49 lines
Quoted 42 lines
Quoted 42 lines
arch/arm64/kvm/ptdump.c +10 -5
 static int kvm_ptdump_guest_open(struct inode *m, struct file *file)
@@ -206,17 +213,23 @@ static const struct file_operations kvm_ptdump_guest_fops = {
 
 static int kvm_pgtable_range_show(struct seq_file *m, void *unused)
 {
-	struct kvm_pgtable *pgtable = m->private;
+	struct kvm *kvm = m->private;
+
+	guard(write_lock)(&kvm->mmu_lock);
+	if (kvm->arch.mmu.pgt)
+		seq_printf(m, "%2u\n", kvm->arch.mmu.pgt->ia_bits);
 
-	seq_printf(m, "%2u\n", pgtable->ia_bits);
 	return 0;
 }
 
 static int kvm_pgtable_levels_show(struct seq_file *m, void *unused)
 {
-	struct kvm_pgtable *pgtable = m->private;
+	struct kvm *kvm = m->private;
+
+	guard(write_lock)(&kvm->mmu_lock);
+	if (kvm->arch.mmu.pgt)
+		seq_printf(m, "%1d\n", KVM_PGTABLE_MAX_LEVELS - kvm->arch.mmu.pgt->start_level);
 
-	seq_printf(m, "%1d\n", KVM_PGTABLE_MAX_LEVELS - pgtable->start_level);
 	return 0;
 }
 
@@ -224,15 +237,12 @@ 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_pgtable *pgtable;
 	int ret;
 
 	if (!kvm_get_kvm_safe(kvm))
 		return -ENOENT;
 
Quoted 6 lines

arch/arm64/kvm/ptdump.c static int kvm_pgtable_debugfs_open(struct inode *m, struct file *file, · line 245

Quoted 4 lines
arch/arm64/kvm/ptdump.c +1 -3
-	pgtable = kvm->arch.mmu.pgt;
-
-	ret = single_open(file, show, pgtable);
+	ret = single_open(file, show, kvm);

Maybe this change is more related with the previous patch?

Quoted 3 lines

I see your point, but I divided it into first fixing mmu UAF, then the pgt UAF, which I also think makes sense.

Okay then

Thanks!
Leo

#7 lore raw

Re: [PATCH v2 3/6] KVM: arm64: ptdump: Fix UAF when mmu->pgt is freed

Quoted 73 lines · Leonardo Bras
Quoted 68 lines · Wei-Lin Chang
Quoted 57 lines · Leonardo Bras

Hi Wei Lin,

Quoted 54 lines · Wei-Lin Chang

ptdump files can still be read after the pgt of the canonical mmu is freed, if they are opened before the VM debugfs directory is removed. This triggers UAF in places where we cache the pgt pointer or access it without checking its validity.

Check the pgt is still alive under the mmu_lock before accessing the pgt.

---
 arch/arm64/kvm/ptdump.c | 38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)
arch/arm64/kvm/ptdump.c +9 -2
index d5aa9eff08d1..752d8e0cd25c 100644
@@ -115,13 +115,21 @@ static int kvm_ptdump_build_levels(struct ptdump_pg_level *level, u32 start_lvl)
 static struct kvm_ptdump_guest_state *kvm_ptdump_parser_create(struct kvm *kvm)
 {
 	struct kvm_ptdump_guest_state *st;
-	struct kvm_pgtable *pgtable = kvm->arch.mmu.pgt;
+	struct kvm_pgtable *pgtable;
 	int ret;
 
 	st = kzalloc_obj(struct kvm_ptdump_guest_state, GFP_KERNEL_ACCOUNT);
 	if (!st)
 		return ERR_PTR(-ENOMEM);
 
+	guard(write_lock)(&kvm->mmu_lock);
+	if (!kvm->arch.mmu.pgt) {
+		kfree(st);
+		return ERR_PTR(-ENXIO);
+	}
+
+	pgtable = kvm->arch.mmu.pgt;
+
 	ret = kvm_ptdump_build_levels(&st->level[0], pgtable->start_level);
 	if (ret) {
 		kfree(st);
@@ -137,7 +145,6 @@ static struct kvm_ptdump_guest_state *kvm_ptdump_parser_create(struct kvm *kvm)
 
 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;
@@ -154,11 +161,11 @@ static int kvm_ptdump_guest_show(struct seq_file *m, void *unused)
 		.seq		= m,
 	};
 
Quoted 9 lines

arch/arm64/kvm/ptdump.c static int kvm_ptdump_guest_show(struct seq_file *m, void *unused) · line 166

Quoted 6 lines
arch/arm64/kvm/ptdump.c +3 -3
-	write_lock(&kvm->mmu_lock);
-	ret = kvm_pgtable_walk(mmu->pgt, 0, BIT(mmu->pgt->ia_bits), &walker);
-	write_unlock(&kvm->mmu_lock);
+	guard(write_lock)(&kvm->mmu_lock);
+	if (mmu->pgt)
+		return kvm_pgtable_walk(mmu->pgt, 0, BIT(mmu->pgt->ia_bits), &walker);

IIUC, that's the same behavior, right?
Just changed to look about the same with the rest of this file?

Quoted 3 lines

I'm not too sure what you are referring to, if you mean the write_lock/unlock -> guard(write_lock) change, then yes, mostly. Just also checking mmu->pgt is still not freed.

Quoted 5 lines

I was referring just to lock+unlock -> guard() strategy.
IIUC this has the same effect, so I was wondering on why you suggested the change.

I then supposed it was to look the same of the rest of the file.

Yeah, and I believe using guard() is encouraged, since it's less error prone.

Quoted 19 lines
Quoted 11 lines
Quoted 10 lines

arch/arm64/kvm/ptdump.c static int kvm_ptdump_guest_show(struct seq_file *m, void *unused) · line 169

Quoted 7 lines
arch/arm64/kvm/ptdump.c +1 -1
-	return ret;
+	return 0;
 }

So if the pgt does not exist anymore, it returns zero. Is that the desired behavior?

Quoted 4 lines

Good question, so the question is what contract between the ptdump and user do we want to make for this case. I guess returning some error like -EIO could make a little more sense than just printing nothing?

It depends on what is the behaviour you want to see... see below..

Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

I guess it's aligned with the idea of single file mentioned in the cover, right?

Sorry, I don't get what you are asking here?

IIUC you mentioned in the cover letter that you planned to have a file which would, on read, output ptdump for every nested pgtable. Did I get that right?

Correct.

Quoted 5 lines

If so, I imagine that the user has no idea how many nested pgtables are there, and if the ioctl initially finds N, but ends up printing N-1 (because a pgtable was not there anymore), that would not be an error that the user should be worried about.

In kvm_ptdump_guest_nested_show() I acquire the mmu_lock, therefore "initially finds N but printing N-1" won't happen, the whole operation of looking at the nested mmu array and printing them out is atomic.

Quoted 4 lines

It could be an issue if you were printing in multiple steps (multiple checks), as a part of that pgtable could be printed while another part could not. Which does not seem to be the case.

Yes, that's one of the problems not taking the lock can introduce.

Thanks,
Wei-Lin Chang

Quoted 58 lines
Quoted 50 lines
Quoted 43 lines
Quoted 43 lines
arch/arm64/kvm/ptdump.c +10 -5
 static int kvm_ptdump_guest_open(struct inode *m, struct file *file)
@@ -206,17 +213,23 @@ static const struct file_operations kvm_ptdump_guest_fops = {
 
 static int kvm_pgtable_range_show(struct seq_file *m, void *unused)
 {
-	struct kvm_pgtable *pgtable = m->private;
+	struct kvm *kvm = m->private;
+
+	guard(write_lock)(&kvm->mmu_lock);
+	if (kvm->arch.mmu.pgt)
+		seq_printf(m, "%2u\n", kvm->arch.mmu.pgt->ia_bits);
 
-	seq_printf(m, "%2u\n", pgtable->ia_bits);
 	return 0;
 }
 
 static int kvm_pgtable_levels_show(struct seq_file *m, void *unused)
 {
-	struct kvm_pgtable *pgtable = m->private;
+	struct kvm *kvm = m->private;
+
+	guard(write_lock)(&kvm->mmu_lock);
+	if (kvm->arch.mmu.pgt)
+		seq_printf(m, "%1d\n", KVM_PGTABLE_MAX_LEVELS - kvm->arch.mmu.pgt->start_level);
 
-	seq_printf(m, "%1d\n", KVM_PGTABLE_MAX_LEVELS - pgtable->start_level);
 	return 0;
 }
 
@@ -224,15 +237,12 @@ 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_pgtable *pgtable;
 	int ret;
 
 	if (!kvm_get_kvm_safe(kvm))
 		return -ENOENT;
 
Quoted 6 lines

arch/arm64/kvm/ptdump.c static int kvm_pgtable_debugfs_open(struct inode *m, struct file *file, · line 245

Quoted 4 lines
arch/arm64/kvm/ptdump.c +1 -3
-	pgtable = kvm->arch.mmu.pgt;
-
-	ret = single_open(file, show, pgtable);
+	ret = single_open(file, show, kvm);

Maybe this change is more related with the previous patch?

Quoted 3 lines

I see your point, but I divided it into first fixing mmu UAF, then the pgt UAF, which I also think makes sense.

Okay then

Thanks!
Leo

#8 lore raw

PATCH v2 4/6 KVM: arm64: ptdump: Factor out initialization of kvm_ptdump_guest_state

In reply to Wei-Lin Chang

Extract the code for initializing kvm_ptdump_guest_state to allow reusing the same instance for dumping multiple page tables.

---
 arch/arm64/kvm/ptdump.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
arch/arm64/kvm/ptdump.c +18 -5
index 752d8e0cd25c..0c9647666e65 100644
@@ -112,6 +112,23 @@ static int kvm_ptdump_build_levels(struct ptdump_pg_level *level, u32 start_lvl)
 	return 0;
 }
 
+static int kvm_ptdump_parser_init(struct kvm_ptdump_guest_state *st, struct kvm *kvm,
+				  struct kvm_pgtable *pgt)
+{
+	int ret;
+
+	ret = kvm_ptdump_build_levels(&st->level[0], pgt->start_level);
+	if (ret)
+		return ret;
+
+	st->ipa_marker[0].name          = "Guest IPA";
+	st->ipa_marker[1].start_address = BIT(pgt->ia_bits);
+
+	st->kvm                         = kvm;
+
+	return 0;
+}
+
 static struct kvm_ptdump_guest_state *kvm_ptdump_parser_create(struct kvm *kvm)
 {
 	struct kvm_ptdump_guest_state *st;
@@ -129,17 +146,13 @@ static struct kvm_ptdump_guest_state *kvm_ptdump_parser_create(struct kvm *kvm)
 	}
 
 	pgtable = kvm->arch.mmu.pgt;
+	ret = kvm_ptdump_parser_init(st, kvm, pgtable);
 
-	ret = kvm_ptdump_build_levels(&st->level[0], pgtable->start_level);
 	if (ret) {
 		kfree(st);
 		return ERR_PTR(ret);
 	}
 
-	st->ipa_marker[0].name		= "Guest IPA";
-	st->ipa_marker[1].start_address = BIT(pgtable->ia_bits);
-
-	st->kvm				= kvm;
 	return st;
 }
 
Signature · 1 line
2.43.0
#9 lore raw

PATCH v2 5/6 KVM: arm64: ptdump: Extract kvm_ptdump_guest_open() from canonical ptdump path

In reply to Wei-Lin Chang

Factor out kvm_ptdump_guest_open() so that the shadow ptdump can reuse kvm_ptdump_guest_open().

---
 arch/arm64/kvm/ptdump.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
arch/arm64/kvm/ptdump.c +12 -6
index 0c9647666e65..40f93b7c7ad9 100644
@@ -156,7 +156,7 @@ static struct kvm_ptdump_guest_state *kvm_ptdump_parser_create(struct kvm *kvm)
 	return st;
 }
 
-static int kvm_ptdump_guest_show(struct seq_file *m, void *unused)
+static int kvm_ptdump_guest_canonical_show(struct seq_file *m, void *unused)
 {
 	struct kvm_ptdump_guest_state *st = m->private;
 	struct kvm *kvm = st->kvm;
@@ -181,7 +181,8 @@ static int kvm_ptdump_guest_show(struct seq_file *m, void *unused)
 	return 0;
 }
 
-static int kvm_ptdump_guest_open(struct inode *m, struct file *file)
+static int kvm_ptdump_guest_open(struct inode *m, struct file *file,
+				 int (*show)(struct seq_file *, void *))
 {
 	struct kvm *kvm = m->i_private;
 	struct kvm_ptdump_guest_state *st;
@@ -196,7 +197,7 @@ static int kvm_ptdump_guest_open(struct inode *m, struct file *file)
 		goto err_with_kvm_ref;
 	}
 
-	ret = single_open(file, kvm_ptdump_guest_show, st);
+	ret = single_open(file, show, st);
 	if (!ret)
 		return 0;
 
@@ -206,6 +207,11 @@ static int kvm_ptdump_guest_open(struct inode *m, struct file *file)
 	return ret;
 }
 
+static int kvm_ptdump_guest_canonical_open(struct inode *m, struct file *file)
+{
+	return kvm_ptdump_guest_open(m, file, kvm_ptdump_guest_canonical_show);
+}
+
 static int kvm_ptdump_guest_close(struct inode *m, struct file *file)
 {
 	struct kvm *kvm = m->i_private;
@@ -217,8 +223,8 @@ static int kvm_ptdump_guest_close(struct inode *m, struct file *file)
 	return single_release(m, file);
 }
 
-static const struct file_operations kvm_ptdump_guest_fops = {
-	.open		= kvm_ptdump_guest_open,
+static const struct file_operations kvm_ptdump_guest_canonical_fops = {
+	.open		= kvm_ptdump_guest_canonical_open,
 	.read		= seq_read,
 	.llseek		= seq_lseek,
 	.release	= kvm_ptdump_guest_close,
@@ -296,7 +302,7 @@ 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);
+			    kvm, &kvm_ptdump_guest_canonical_fops);
 	debugfs_create_file("ipa_range", 0400, kvm->debugfs_dentry,
 			    kvm, &kvm_pgtable_range_fops);
 	debugfs_create_file("stage2_levels", 0400, kvm->debugfs_dentry,
Signature · 1 line
2.43.0
#10 lore raw

PATCH v2 6/6 KVM: arm64: ptdump: Introduce the shadow ptdump file

In reply to Wei-Lin Chang

Create a ptdump file for all shadow page tables. It will dump out all valid shadow page tables at the time of request, with the mmu's index, guest VTCR_EL2, VTTBR_EL2, and whether the guest stage-2 is enabled or not.

Also detach the nested mmu array under the mmu_lock in kvm_arch_flush_shadow_all() so readers cannot race with the array being removed, then free the old array after dropping the lock.

---
 arch/arm64/kvm/nested.c | 12 ++++++--
 arch/arm64/kvm/ptdump.c | 61 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 69 insertions(+), 4 deletions(-)
arch/arm64/kvm/nested.c +9 -3
index 6435efd65cb5..17a180ddf6ca 100644
@@ -1283,6 +1283,7 @@ void kvm_nested_s2_flush(struct kvm *kvm)
 
 void kvm_arch_flush_shadow_all(struct kvm *kvm)
 {
+	struct kvm_s2_mmu *mmus;
 	int i;
 
 	for (i = 0; i < kvm->arch.nested_mmus_size; i++) {
@@ -1291,9 +1292,14 @@ void kvm_arch_flush_shadow_all(struct kvm *kvm)
 		if (!WARN_ON(atomic_read(&mmu->refcnt)))
 			kvm_free_stage2_pgd(mmu);
 	}
-	kvfree(kvm->arch.nested_mmus);
-	kvm->arch.nested_mmus = NULL;
-	kvm->arch.nested_mmus_size = 0;
+
+	scoped_guard(write_lock, &kvm->mmu_lock) {
+		mmus = kvm->arch.nested_mmus;
+		kvm->arch.nested_mmus = NULL;
+		kvm->arch.nested_mmus_size = 0;
+	}
+
+	kvfree(mmus);
 	kvm_uninit_stage2_mmu(kvm);
 }
 
arch/arm64/kvm/ptdump.c +60 -1

84 lines not on this page · raw message

Signature · 1 line
2.43.0
#11 lore raw

Re: [PATCH v2 6/6] KVM: arm64: ptdump: Introduce the shadow ptdump file

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

New issues:
- [High] Concurrent reallocation of nested_mmus array leads to Use-After-Free in kvm_ptdump_guest_nested_show().
- [High] If kvm_ptdump_parser_init() fails, st->kvm is left as NULL, causing a kernel panic on subsequent debugfs reads.
- [Medium] Missing note_page_flush() causes the last contiguous block of page table entries to be silently dropped.

Pre-existing issues:
- [Critical] kvm_arch_flush_shadow_all() frees the nested_mmus array even if active VCPUs hold references to its elements.
- [High] Unprotected iteration over nested_mmus in kvm_arch_flush_shadow_all() races with array reallocation. --

commit 4dce12b467bf2d3540834dccee1f3e623af43d42
Author: Wei-Lin Chang <[email protected]>

KVM: arm64: ptdump: Introduce the shadow ptdump file

This commit creates a ptdump file for shadow page tables used in nested virtualization. It dumps all valid shadow page tables, their mmu index, VTCR_EL2, VTTBR_EL2, and stage-2 enablement status. It also modifies kvm_arch_flush_shadow_all() to detach the nested mmu array under mmu_lock.

arch/arm64/kvm/nested.c void kvm_nested_s2_flush(struct kvm *kvm) · line 1190

Quoted 12 lines
arch/arm64/kvm/nested.c +1 -0
index efd5d21c7ac75..cbf7143819669 100644
@@ -1184,6 +1184,7 @@ void kvm_nested_s2_flush(struct kvm *kvm)
 
 void kvm_arch_flush_shadow_all(struct kvm *kvm)
 {
+	struct kvm_s2_mmu *mmus;
 	int i;
 
 	for (i = 0; i < kvm->arch.nested_mmus_size; i++) {

[Severity: High]
This is a pre-existing issue, but can this loop race with array reallocation?

If a concurrent thread calls KVM_ARM_VCPU_INIT to initialize a new nested vCPU, kvm_vcpu_init_nested() expands the array via kvrealloc() holding only kvm->arch.config_lock. Since this loop doesn't hold config_lock or mmu_lock, could it dereference the array while or after it is freed?

Quoted 6 lines
arch/arm64/kvm/nested.c +0 -1
 		struct kvm_s2_mmu *mmu = &kvm->arch.nested_mmus[i];
 
 		if (!WARN_ON(atomic_read(&mmu->refcnt)))
 			kvm_free_stage2_pgd(mmu);
 	}
-	kvfree(kvm->arch.nested_mmus);

arch/arm64/kvm/nested.c void kvm_arch_flush_shadow_all(struct kvm *kvm) · line 1302

Quoted 10 lines
arch/arm64/kvm/nested.c +8 -2
-	kvm->arch.nested_mmus = NULL;
-	kvm->arch.nested_mmus_size = 0;
+
+	scoped_guard(write_lock, &kvm->mmu_lock) {
+		mmus = kvm->arch.nested_mmus;
+		kvm->arch.nested_mmus = NULL;
+		kvm->arch.nested_mmus_size = 0;
+	}
+
+	kvfree(mmus);

[Severity: Critical]
This is a pre-existing issue, but does this prematurely free the array if active VCPUs hold references?

If a running vCPU holds a reference to its nested MMU, where atomic_read shows refcnt > 0, the loop above skips freeing the page table, but the backing array is freed anyway.

When the vCPU exits and calls kvm_vcpu_put_hw_mmu(), it will dereference the freed pointer. Should the array lifetime be tied to the active references?

Quoted 2 lines

kvm_uninit_stage2_mmu(kvm);
}

[ ... ]

Quoted 15 lines
arch/arm64/kvm/ptdump.c +7 -0
index 40f93b7c7ad98..1649eaa75798c 100644
@@ -181,6 +181,50 @@ static int kvm_ptdump_guest_canonical_show(struct seq_file *m, void *unused)
 	return 0;
 }
 
+static int kvm_ptdump_guest_nested_show(struct seq_file *m, void *unused)
+{
+	int ret = 0, i;
+	struct kvm_ptdump_guest_state *st = m->private;
+	struct kvm *kvm = st->kvm;
+	struct kvm_pgtable_walker walker = (struct kvm_pgtable_walker) {
+		.cb	= kvm_ptdump_visitor,

arch/arm64/kvm/ptdump.c static int kvm_ptdump_guest_canonical_show(struct seq_file *m, void *unused) · line 200

Quoted 10 lines
arch/arm64/kvm/ptdump.c +10 -0
+		.arg	= &st->parser_state,
+		.flags	= KVM_PGTABLE_WALK_LEAF,
+	};
+
+	guard(write_lock)(&kvm->mmu_lock);
+
+	if (!kvm->arch.nested_mmus)
+		return 0;
+
+	for (i = 0; i < kvm->arch.nested_mmus_size; i++) {

[Severity: High]
Is it safe to iterate over the nested_mmus array holding only mmu_lock?

If userspace reads the debugfs file while a concurrent thread issues a KVM_ARM_VCPU_INIT ioctl to initialize a new nested vCPU, kvm_vcpu_init_nested() expands the array via kvrealloc() holding only kvm->arch.config_lock:

kvm_vcpu_init_nested()

    tmp = kvrealloc(kvm->arch.nested_mmus, ...);
    swap(kvm->arch.nested_mmus, tmp);

Could the debugfs reader dereference the array while or after it is freed by kvrealloc()?

arch/arm64/kvm/ptdump.c static int kvm_ptdump_guest_canonical_show(struct seq_file *m, void *unused) · line 210

Quoted 10 lines
arch/arm64/kvm/ptdump.c +10 -0
+		struct kvm_s2_mmu *mmu = &kvm->arch.nested_mmus[i];
+
+		if (!mmu->pgt)
+			continue;
+
+		if (kvm_s2_mmu_valid(mmu)) {
+			memset(st, 0, sizeof(*st));
+			ret = kvm_ptdump_parser_init(st, kvm, mmu->pgt);
+			if (ret)
+				return ret;

[Severity: High]
Does an early return here leave st->kvm as NULL?

The memset() clears st->kvm. If kvm_ptdump_parser_init() returns an error, the function propagates it, leaving st->kvm NULL.

If userspace issues another read() on the same file descriptor, seq_read will call the show() callback again, which will dereference the NULL st->kvm at the mmu_lock acquisition:

guard(write_lock)(&kvm->mmu_lock);

Could we restore st->kvm on the error path?

arch/arm64/kvm/ptdump.c static int kvm_ptdump_guest_canonical_show(struct seq_file *m, void *unused) · line 222

Quoted 12 lines
arch/arm64/kvm/ptdump.c +12 -0
+			st->parser_state = (struct ptdump_pg_state) {
+				.marker		= &st->ipa_marker[0],
+				.level		= -1,
+				.pg_level	= &st->level[0],
+				.seq		= m,
+			};
+			seq_printf(m, "nested mmu %d VTCR: 0x%016llx VTTBR: 0x%016llx s2: %s\n",
+				   i, mmu->tlb_vtcr, mmu->tlb_vttbr,
+				   mmu->nested_stage2_enabled ? "enabled" : "disabled");
+			ret = kvm_pgtable_walk(mmu->pgt, 0, BIT(mmu->pgt->ia_bits), &walker);
+			if (ret)
+				return ret;

[Severity: Medium]
Does the page table walk lack a subsequent call to note_page_flush()?

The ptdump API requires a final call to note_page_flush() to emit the final buffered contiguous block of page table attributes. Because this is missing after the kvm_pgtable_walk() completion, does the last block get silently dropped from the debugfs output?

Quoted 4 lines
arch/arm64/kvm/ptdump.c +4 -0
+		}
+	}
+	return ret;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#12 lore raw

Re: [PATCH v2 6/6] KVM: arm64: ptdump: Introduce the shadow ptdump file

Quoted 38 lines · On Tue, Jun 30, 2026 at 12:27:58PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

New issues:
- [High] Concurrent reallocation of nested_mmus array leads to Use-After-Free in kvm_ptdump_guest_nested_show().
- [High] If kvm_ptdump_parser_init() fails, st->kvm is left as NULL, causing a kernel panic on subsequent debugfs reads.
- [Medium] Missing note_page_flush() causes the last contiguous block of page table entries to be silently dropped.

Pre-existing issues:
- [Critical] kvm_arch_flush_shadow_all() frees the nested_mmus array even if active VCPUs hold references to its elements.
- [High] Unprotected iteration over nested_mmus in kvm_arch_flush_shadow_all() races with array reallocation. --

commit 4dce12b467bf2d3540834dccee1f3e623af43d42
Author: Wei-Lin Chang <[email protected]>

KVM: arm64: ptdump: Introduce the shadow ptdump file

This commit creates a ptdump file for shadow page tables used in nested virtualization. It dumps all valid shadow page tables, their mmu index, VTCR_EL2, VTTBR_EL2, and stage-2 enablement status. It also modifies kvm_arch_flush_shadow_all() to detach the nested mmu array under mmu_lock.

arch/arm64/kvm/nested.c void kvm_nested_s2_flush(struct kvm *kvm) · line 1190

Quoted 12 lines
arch/arm64/kvm/nested.c +1 -0
index efd5d21c7ac75..cbf7143819669 100644
@@ -1184,6 +1184,7 @@ void kvm_nested_s2_flush(struct kvm *kvm)
 
 void kvm_arch_flush_shadow_all(struct kvm *kvm)
 {
+	struct kvm_s2_mmu *mmus;
 	int i;
 
 	for (i = 0; i < kvm->arch.nested_mmus_size; i++) {

[Severity: High]
This is a pre-existing issue, but can this loop race with array reallocation?

Quoted 4 lines

If a concurrent thread calls KVM_ARM_VCPU_INIT to initialize a new nested vCPU, kvm_vcpu_init_nested() expands the array via kvrealloc() holding only kvm->arch.config_lock. Since this loop doesn't hold config_lock or mmu_lock, could it dereference the array while or after it is freed?

I don't think so, if a concurrent thread calls KVM_ARM_VCPU_INIT, it means both the kvm structure and mm_struct is still live, hence kvm_arch_flush_shadow_all() won't be called, as it is called when kvm unregisters mmu notifiers (kvm_destroy_vm(), when the last kvm reference goes), or when the last mmput() happens which call the mmu notifier release.

Quoted 22 lines
Quoted 7 lines
arch/arm64/kvm/nested.c +0 -1
 		struct kvm_s2_mmu *mmu = &kvm->arch.nested_mmus[i];
 
 		if (!WARN_ON(atomic_read(&mmu->refcnt)))
 			kvm_free_stage2_pgd(mmu);
 	}
-	kvfree(kvm->arch.nested_mmus);

arch/arm64/kvm/nested.c void kvm_arch_flush_shadow_all(struct kvm *kvm) · line 1302

Quoted 10 lines
arch/arm64/kvm/nested.c +8 -2
-	kvm->arch.nested_mmus = NULL;
-	kvm->arch.nested_mmus_size = 0;
+
+	scoped_guard(write_lock, &kvm->mmu_lock) {
+		mmus = kvm->arch.nested_mmus;
+		kvm->arch.nested_mmus = NULL;
+		kvm->arch.nested_mmus_size = 0;
+	}
+
+	kvfree(mmus);

[Severity: Critical]
This is a pre-existing issue, but does this prematurely free the array if active VCPUs hold references?

Quoted 6 lines

If a running vCPU holds a reference to its nested MMU, where atomic_read shows refcnt > 0, the loop above skips freeing the page table, but the backing array is freed anyway.

When the vCPU exits and calls kvm_vcpu_put_hw_mmu(), it will dereference the freed pointer. Should the array lifetime be tied to the active references?

Similar to my comment above, when kvm_arch_flush_shadow_all() is called, there will be no more mm_struct users, so there can't be any running vCPUs to race against.

Quoted 39 lines
Quoted 3 lines

kvm_uninit_stage2_mmu(kvm);
}

[ ... ]

Quoted 15 lines
arch/arm64/kvm/ptdump.c +7 -0
index 40f93b7c7ad98..1649eaa75798c 100644
@@ -181,6 +181,50 @@ static int kvm_ptdump_guest_canonical_show(struct seq_file *m, void *unused)
 	return 0;
 }
 
+static int kvm_ptdump_guest_nested_show(struct seq_file *m, void *unused)
+{
+	int ret = 0, i;
+	struct kvm_ptdump_guest_state *st = m->private;
+	struct kvm *kvm = st->kvm;
+	struct kvm_pgtable_walker walker = (struct kvm_pgtable_walker) {
+		.cb	= kvm_ptdump_visitor,

arch/arm64/kvm/ptdump.c static int kvm_ptdump_guest_canonical_show(struct seq_file *m, void *unused) · line 200

Quoted 10 lines
arch/arm64/kvm/ptdump.c +10 -0
+		.arg	= &st->parser_state,
+		.flags	= KVM_PGTABLE_WALK_LEAF,
+	};
+
+	guard(write_lock)(&kvm->mmu_lock);
+
+	if (!kvm->arch.nested_mmus)
+		return 0;
+
+	for (i = 0; i < kvm->arch.nested_mmus_size; i++) {

[Severity: High]
Is it safe to iterate over the nested_mmus array holding only mmu_lock?

If userspace reads the debugfs file while a concurrent thread issues a KVM_ARM_VCPU_INIT ioctl to initialize a new nested vCPU, kvm_vcpu_init_nested() expands the array via kvrealloc() holding only kvm->arch.config_lock:

Quoted 6 lines

kvm_vcpu_init_nested()

    tmp = kvrealloc(kvm->arch.nested_mmus, ...);
    swap(kvm->arch.nested_mmus, tmp);

Could the debugfs reader dereference the array while or after it is freed by kvrealloc()?

This should be fixed by ("KVM: arm64: Reassign nested_mmus array behind mmu_lock") [*].

[*]: https://lore.kernel.org/kvmarm/aiKIVVeIr1aAB1yp@v4bel/

Quoted 18 lines

arch/arm64/kvm/ptdump.c static int kvm_ptdump_guest_canonical_show(struct seq_file *m, void *unused) · line 210

Quoted 11 lines
arch/arm64/kvm/ptdump.c +10 -0
+		struct kvm_s2_mmu *mmu = &kvm->arch.nested_mmus[i];
+
+		if (!mmu->pgt)
+			continue;
+
+		if (kvm_s2_mmu_valid(mmu)) {
+			memset(st, 0, sizeof(*st));
+			ret = kvm_ptdump_parser_init(st, kvm, mmu->pgt);
+			if (ret)
+				return ret;

[Severity: High]
Does an early return here leave st->kvm as NULL?

The memset() clears st->kvm. If kvm_ptdump_parser_init() returns an error, the function propagates it, leaving st->kvm NULL.

Quoted 7 lines

If userspace issues another read() on the same file descriptor, seq_read will call the show() callback again, which will dereference the NULL st->kvm at the mmu_lock acquisition:

guard(write_lock)(&kvm->mmu_lock);

Could we restore st->kvm on the error path?

I'll just rearrange kvm_ptdump_parser_init() so that st->kvm is set before the possible error return.

Quoted 17 lines

arch/arm64/kvm/ptdump.c static int kvm_ptdump_guest_canonical_show(struct seq_file *m, void *unused) · line 222

Quoted 13 lines
arch/arm64/kvm/ptdump.c +12 -0
+			st->parser_state = (struct ptdump_pg_state) {
+				.marker		= &st->ipa_marker[0],
+				.level		= -1,
+				.pg_level	= &st->level[0],
+				.seq		= m,
+			};
+			seq_printf(m, "nested mmu %d VTCR: 0x%016llx VTTBR: 0x%016llx s2: %s\n",
+				   i, mmu->tlb_vtcr, mmu->tlb_vttbr,
+				   mmu->nested_stage2_enabled ? "enabled" : "disabled");
+			ret = kvm_pgtable_walk(mmu->pgt, 0, BIT(mmu->pgt->ia_bits), &walker);
+			if (ret)
+				return ret;

[Severity: Medium]
Does the page table walk lack a subsequent call to note_page_flush()?

Quoted 4 lines

The ptdump API requires a final call to note_page_flush() to emit the final buffered contiguous block of page table attributes. Because this is missing after the kvm_pgtable_walk() completion, does the last block get silently dropped from the debugfs output?

Unfortunately yes, we are missing the last row for stage-2 ptdump, however the fix is not a note_page_flush(), as it uses 0 for the current address. For stage-2 ptdump we should supply the last address of the guest IPA space.

However, this is a separate problem and can be fixed after this series.

Thanks,
Wei-Lin Chang

Quoted 8 lines
Quoted 5 lines
arch/arm64/kvm/ptdump.c +4 -0
+		}
+	}
+	return ret;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#13 lore raw

Re: [PATCH v2 6/6] KVM: arm64: ptdump: Introduce the shadow ptdump file

In reply to Wei-Lin Chang
Quoted 8 lines · Wei-Lin Chang

Create a ptdump file for all shadow page tables. It will dump out all valid shadow page tables at the time of request, with the mmu's index, guest VTCR_EL2, VTTBR_EL2, and whether the guest stage-2 is enabled or not.

Also detach the nested mmu array under the mmu_lock in kvm_arch_flush_shadow_all() so readers cannot race with the array being removed, then free the old array after dropping the lock.

Out of curiosity: why drop the lock before kfree'ing ?

Thanks!
Leo

Quoted 132 lines
---
 arch/arm64/kvm/nested.c | 12 ++++++--
 arch/arm64/kvm/ptdump.c | 61 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 69 insertions(+), 4 deletions(-)
arch/arm64/kvm/nested.c +9 -3
index 6435efd65cb5..17a180ddf6ca 100644
@@ -1283,6 +1283,7 @@ void kvm_nested_s2_flush(struct kvm *kvm)
 
 void kvm_arch_flush_shadow_all(struct kvm *kvm)
 {
+	struct kvm_s2_mmu *mmus;
 	int i;
 
 	for (i = 0; i < kvm->arch.nested_mmus_size; i++) {
@@ -1291,9 +1292,14 @@ void kvm_arch_flush_shadow_all(struct kvm *kvm)
 		if (!WARN_ON(atomic_read(&mmu->refcnt)))
 			kvm_free_stage2_pgd(mmu);
 	}
-	kvfree(kvm->arch.nested_mmus);
-	kvm->arch.nested_mmus = NULL;
-	kvm->arch.nested_mmus_size = 0;
+
+	scoped_guard(write_lock, &kvm->mmu_lock) {
+		mmus = kvm->arch.nested_mmus;
+		kvm->arch.nested_mmus = NULL;
+		kvm->arch.nested_mmus_size = 0;
+	}
+
+	kvfree(mmus);
 	kvm_uninit_stage2_mmu(kvm);
 }
 
arch/arm64/kvm/ptdump.c +60 -1
index 40f93b7c7ad9..1649eaa75798 100644
@@ -181,6 +181,50 @@ static int kvm_ptdump_guest_canonical_show(struct seq_file *m, void *unused)
 	return 0;
 }
 
+static int kvm_ptdump_guest_nested_show(struct seq_file *m, void *unused)
+{
+	int ret = 0, i;
+	struct kvm_ptdump_guest_state *st = m->private;
+	struct kvm *kvm = st->kvm;
+	struct kvm_pgtable_walker walker = (struct kvm_pgtable_walker) {
+		.cb	= kvm_ptdump_visitor,
+		.arg	= &st->parser_state,
+		.flags	= KVM_PGTABLE_WALK_LEAF,
+	};
+
+	guard(write_lock)(&kvm->mmu_lock);
+
+	if (!kvm->arch.nested_mmus)
+		return 0;
+
+	for (i = 0; i < kvm->arch.nested_mmus_size; i++) {
+		struct kvm_s2_mmu *mmu = &kvm->arch.nested_mmus[i];
+
+		if (!mmu->pgt)
+			continue;
+
+		if (kvm_s2_mmu_valid(mmu)) {
+			memset(st, 0, sizeof(*st));
+			ret = kvm_ptdump_parser_init(st, kvm, mmu->pgt);
+			if (ret)
+				return ret;
+			st->parser_state = (struct ptdump_pg_state) {
+				.marker		= &st->ipa_marker[0],
+				.level		= -1,
+				.pg_level	= &st->level[0],
+				.seq		= m,
+			};
+			seq_printf(m, "nested mmu %d VTCR: 0x%016llx VTTBR: 0x%016llx s2: %s\n",
+				   i, mmu->tlb_vtcr, mmu->tlb_vttbr,
+				   mmu->nested_stage2_enabled ? "enabled" : "disabled");
+			ret = kvm_pgtable_walk(mmu->pgt, 0, BIT(mmu->pgt->ia_bits), &walker);
+			if (ret)
+				return ret;
+		}
+	}
+	return ret;
+}
+
 static int kvm_ptdump_guest_open(struct inode *m, struct file *file,
 				 int (*show)(struct seq_file *, void *))
 {
@@ -212,6 +256,11 @@ static int kvm_ptdump_guest_canonical_open(struct inode *m, struct file *file)
 	return kvm_ptdump_guest_open(m, file, kvm_ptdump_guest_canonical_show);
 }
 
+static int kvm_ptdump_guest_nested_open(struct inode *m, struct file *file)
+{
+	return kvm_ptdump_guest_open(m, file, kvm_ptdump_guest_nested_show);
+}
+
 static int kvm_ptdump_guest_close(struct inode *m, struct file *file)
 {
 	struct kvm *kvm = m->i_private;
@@ -230,6 +279,13 @@ static const struct file_operations kvm_ptdump_guest_canonical_fops = {
 	.release	= kvm_ptdump_guest_close,
 };
 
+static const struct file_operations kvm_ptdump_guest_nested_fops = {
+	.open		= kvm_ptdump_guest_nested_open,
+	.read		= seq_read,
+	.llseek		= seq_lseek,
+	.release	= kvm_ptdump_guest_close,
+};
+
 static int kvm_pgtable_range_show(struct seq_file *m, void *unused)
 {
 	struct kvm *kvm = m->private;
@@ -307,6 +363,9 @@ void kvm_s2_ptdump_create_debugfs(struct kvm *kvm)
 			    kvm, &kvm_pgtable_range_fops);
 	debugfs_create_file("stage2_levels", 0400, kvm->debugfs_dentry,
 			    kvm, &kvm_pgtable_levels_fops);
-	if (cpus_have_final_cap(ARM64_HAS_NESTED_VIRT))
+	if (cpus_have_final_cap(ARM64_HAS_NESTED_VIRT)) {
 		kvm->arch.debugfs_nv_dentry = debugfs_create_dir("nested", kvm->debugfs_dentry);
+		debugfs_create_file("shadow_page_tables", 0400, kvm->arch.debugfs_nv_dentry,
+				    kvm, &kvm_ptdump_guest_nested_fops);
+	}
 }
Signature · 1 line
2.43.0
#14 lore raw

Re: [PATCH v2 6/6] KVM: arm64: ptdump: Introduce the shadow ptdump file

Quoted 11 lines · Leonardo Bras
Quoted 8 lines · Wei-Lin Chang

Create a ptdump file for all shadow page tables. It will dump out all valid shadow page tables at the time of request, with the mmu's index, guest VTCR_EL2, VTTBR_EL2, and whether the guest stage-2 is enabled or not.

Also detach the nested mmu array under the mmu_lock in kvm_arch_flush_shadow_all() so readers cannot race with the array being removed, then free the old array after dropping the lock.

Out of curiosity: why drop the lock before kfree'ing ?

Because kvfree() can sleep! :)

Thanks,
Wei-Lin Chang

Quoted 136 lines

Thanks!
Leo

Quoted 132 lines
---
 arch/arm64/kvm/nested.c | 12 ++++++--
 arch/arm64/kvm/ptdump.c | 61 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 69 insertions(+), 4 deletions(-)
arch/arm64/kvm/nested.c +9 -3
index 6435efd65cb5..17a180ddf6ca 100644
@@ -1283,6 +1283,7 @@ void kvm_nested_s2_flush(struct kvm *kvm)
 
 void kvm_arch_flush_shadow_all(struct kvm *kvm)
 {
+	struct kvm_s2_mmu *mmus;
 	int i;
 
 	for (i = 0; i < kvm->arch.nested_mmus_size; i++) {
@@ -1291,9 +1292,14 @@ void kvm_arch_flush_shadow_all(struct kvm *kvm)
 		if (!WARN_ON(atomic_read(&mmu->refcnt)))
 			kvm_free_stage2_pgd(mmu);
 	}
-	kvfree(kvm->arch.nested_mmus);
-	kvm->arch.nested_mmus = NULL;
-	kvm->arch.nested_mmus_size = 0;
+
+	scoped_guard(write_lock, &kvm->mmu_lock) {
+		mmus = kvm->arch.nested_mmus;
+		kvm->arch.nested_mmus = NULL;
+		kvm->arch.nested_mmus_size = 0;
+	}
+
+	kvfree(mmus);
 	kvm_uninit_stage2_mmu(kvm);
 }
 
arch/arm64/kvm/ptdump.c +60 -1
index 40f93b7c7ad9..1649eaa75798 100644
@@ -181,6 +181,50 @@ static int kvm_ptdump_guest_canonical_show(struct seq_file *m, void *unused)
 	return 0;
 }
 
+static int kvm_ptdump_guest_nested_show(struct seq_file *m, void *unused)
+{
+	int ret = 0, i;
+	struct kvm_ptdump_guest_state *st = m->private;
+	struct kvm *kvm = st->kvm;
+	struct kvm_pgtable_walker walker = (struct kvm_pgtable_walker) {
+		.cb	= kvm_ptdump_visitor,
+		.arg	= &st->parser_state,
+		.flags	= KVM_PGTABLE_WALK_LEAF,
+	};
+
+	guard(write_lock)(&kvm->mmu_lock);
+
+	if (!kvm->arch.nested_mmus)
+		return 0;
+
+	for (i = 0; i < kvm->arch.nested_mmus_size; i++) {
+		struct kvm_s2_mmu *mmu = &kvm->arch.nested_mmus[i];
+
+		if (!mmu->pgt)
+			continue;
+
+		if (kvm_s2_mmu_valid(mmu)) {
+			memset(st, 0, sizeof(*st));
+			ret = kvm_ptdump_parser_init(st, kvm, mmu->pgt);
+			if (ret)
+				return ret;
+			st->parser_state = (struct ptdump_pg_state) {
+				.marker		= &st->ipa_marker[0],
+				.level		= -1,
+				.pg_level	= &st->level[0],
+				.seq		= m,
+			};
+			seq_printf(m, "nested mmu %d VTCR: 0x%016llx VTTBR: 0x%016llx s2: %s\n",
+				   i, mmu->tlb_vtcr, mmu->tlb_vttbr,
+				   mmu->nested_stage2_enabled ? "enabled" : "disabled");
+			ret = kvm_pgtable_walk(mmu->pgt, 0, BIT(mmu->pgt->ia_bits), &walker);
+			if (ret)
+				return ret;
+		}
+	}
+	return ret;
+}
+
 static int kvm_ptdump_guest_open(struct inode *m, struct file *file,
 				 int (*show)(struct seq_file *, void *))
 {
@@ -212,6 +256,11 @@ static int kvm_ptdump_guest_canonical_open(struct inode *m, struct file *file)
 	return kvm_ptdump_guest_open(m, file, kvm_ptdump_guest_canonical_show);
 }
 
+static int kvm_ptdump_guest_nested_open(struct inode *m, struct file *file)
+{
+	return kvm_ptdump_guest_open(m, file, kvm_ptdump_guest_nested_show);
+}
+
 static int kvm_ptdump_guest_close(struct inode *m, struct file *file)
 {
 	struct kvm *kvm = m->i_private;
@@ -230,6 +279,13 @@ static const struct file_operations kvm_ptdump_guest_canonical_fops = {
 	.release	= kvm_ptdump_guest_close,
 };
 
+static const struct file_operations kvm_ptdump_guest_nested_fops = {
+	.open		= kvm_ptdump_guest_nested_open,
+	.read		= seq_read,
+	.llseek		= seq_lseek,
+	.release	= kvm_ptdump_guest_close,
+};
+
 static int kvm_pgtable_range_show(struct seq_file *m, void *unused)
 {
 	struct kvm *kvm = m->private;
@@ -307,6 +363,9 @@ void kvm_s2_ptdump_create_debugfs(struct kvm *kvm)
 			    kvm, &kvm_pgtable_range_fops);
 	debugfs_create_file("stage2_levels", 0400, kvm->debugfs_dentry,
 			    kvm, &kvm_pgtable_levels_fops);
-	if (cpus_have_final_cap(ARM64_HAS_NESTED_VIRT))
+	if (cpus_have_final_cap(ARM64_HAS_NESTED_VIRT)) {
 		kvm->arch.debugfs_nv_dentry = debugfs_create_dir("nested", kvm->debugfs_dentry);
+		debugfs_create_file("shadow_page_tables", 0400, kvm->arch.debugfs_nv_dentry,
+				    kvm, &kvm_ptdump_guest_nested_fops);
+	}
 }
Signature · 1 line
2.43.0
#15 lore raw

Re: [PATCH v2 6/6] KVM: arm64: ptdump: Introduce the shadow ptdump file

Quoted 7 lines · Wei-Lin Chang
Quoted 6 lines · Leonardo Bras
Quoted 5 lines · Wei-Lin Chang

Create a ptdump file for all shadow page tables. It will dump out all valid shadow page tables at the time of request, with the mmu's index, guest VTCR_EL2, VTTBR_EL2, and whether the guest stage-2 is enabled or not.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Also detach the nested mmu array under the mmu_lock in kvm_arch_flush_shadow_all() so readers cannot race with the array being removed, then free the old array after dropping the lock.

Out of curiosity: why drop the lock before kfree'ing ?

Because kvfree() can sleep! :)

Damn, I was certain to read kfree, not kvfree(), LOL

You are right, then :)

Thanks!
Leoy

Quoted 139 lines

Thanks,
Wei-Lin Chang

Quoted 136 lines

Thanks!
Leo

Quoted 132 lines
---
 arch/arm64/kvm/nested.c | 12 ++++++--
 arch/arm64/kvm/ptdump.c | 61 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 69 insertions(+), 4 deletions(-)
arch/arm64/kvm/nested.c +9 -3
index 6435efd65cb5..17a180ddf6ca 100644
@@ -1283,6 +1283,7 @@ void kvm_nested_s2_flush(struct kvm *kvm)
 
 void kvm_arch_flush_shadow_all(struct kvm *kvm)
 {
+	struct kvm_s2_mmu *mmus;
 	int i;
 
 	for (i = 0; i < kvm->arch.nested_mmus_size; i++) {
@@ -1291,9 +1292,14 @@ void kvm_arch_flush_shadow_all(struct kvm *kvm)
 		if (!WARN_ON(atomic_read(&mmu->refcnt)))
 			kvm_free_stage2_pgd(mmu);
 	}
-	kvfree(kvm->arch.nested_mmus);
-	kvm->arch.nested_mmus = NULL;
-	kvm->arch.nested_mmus_size = 0;
+
+	scoped_guard(write_lock, &kvm->mmu_lock) {
+		mmus = kvm->arch.nested_mmus;
+		kvm->arch.nested_mmus = NULL;
+		kvm->arch.nested_mmus_size = 0;
+	}
+
+	kvfree(mmus);
 	kvm_uninit_stage2_mmu(kvm);
 }
 
arch/arm64/kvm/ptdump.c +60 -1
index 40f93b7c7ad9..1649eaa75798 100644
@@ -181,6 +181,50 @@ static int kvm_ptdump_guest_canonical_show(struct seq_file *m, void *unused)
 	return 0;
 }
 
+static int kvm_ptdump_guest_nested_show(struct seq_file *m, void *unused)
+{
+	int ret = 0, i;
+	struct kvm_ptdump_guest_state *st = m->private;
+	struct kvm *kvm = st->kvm;
+	struct kvm_pgtable_walker walker = (struct kvm_pgtable_walker) {
+		.cb	= kvm_ptdump_visitor,
+		.arg	= &st->parser_state,
+		.flags	= KVM_PGTABLE_WALK_LEAF,
+	};
+
+	guard(write_lock)(&kvm->mmu_lock);
+
+	if (!kvm->arch.nested_mmus)
+		return 0;
+
+	for (i = 0; i < kvm->arch.nested_mmus_size; i++) {
+		struct kvm_s2_mmu *mmu = &kvm->arch.nested_mmus[i];
+
+		if (!mmu->pgt)
+			continue;
+
+		if (kvm_s2_mmu_valid(mmu)) {
+			memset(st, 0, sizeof(*st));
+			ret = kvm_ptdump_parser_init(st, kvm, mmu->pgt);
+			if (ret)
+				return ret;
+			st->parser_state = (struct ptdump_pg_state) {
+				.marker		= &st->ipa_marker[0],
+				.level		= -1,
+				.pg_level	= &st->level[0],
+				.seq		= m,
+			};
+			seq_printf(m, "nested mmu %d VTCR: 0x%016llx VTTBR: 0x%016llx s2: %s\n",
+				   i, mmu->tlb_vtcr, mmu->tlb_vttbr,
+				   mmu->nested_stage2_enabled ? "enabled" : "disabled");
+			ret = kvm_pgtable_walk(mmu->pgt, 0, BIT(mmu->pgt->ia_bits), &walker);
+			if (ret)
+				return ret;
+		}
+	}
+	return ret;
+}
+
 static int kvm_ptdump_guest_open(struct inode *m, struct file *file,
 				 int (*show)(struct seq_file *, void *))
 {
@@ -212,6 +256,11 @@ static int kvm_ptdump_guest_canonical_open(struct inode *m, struct file *file)
 	return kvm_ptdump_guest_open(m, file, kvm_ptdump_guest_canonical_show);
 }
 
+static int kvm_ptdump_guest_nested_open(struct inode *m, struct file *file)
+{
+	return kvm_ptdump_guest_open(m, file, kvm_ptdump_guest_nested_show);
+}
+
 static int kvm_ptdump_guest_close(struct inode *m, struct file *file)
 {
 	struct kvm *kvm = m->i_private;
@@ -230,6 +279,13 @@ static const struct file_operations kvm_ptdump_guest_canonical_fops = {
 	.release	= kvm_ptdump_guest_close,
 };
 
+static const struct file_operations kvm_ptdump_guest_nested_fops = {
+	.open		= kvm_ptdump_guest_nested_open,
+	.read		= seq_read,
+	.llseek		= seq_lseek,
+	.release	= kvm_ptdump_guest_close,
+};
+
 static int kvm_pgtable_range_show(struct seq_file *m, void *unused)
 {
 	struct kvm *kvm = m->private;
@@ -307,6 +363,9 @@ void kvm_s2_ptdump_create_debugfs(struct kvm *kvm)
 			    kvm, &kvm_pgtable_range_fops);
 	debugfs_create_file("stage2_levels", 0400, kvm->debugfs_dentry,
 			    kvm, &kvm_pgtable_levels_fops);
-	if (cpus_have_final_cap(ARM64_HAS_NESTED_VIRT))
+	if (cpus_have_final_cap(ARM64_HAS_NESTED_VIRT)) {
 		kvm->arch.debugfs_nv_dentry = debugfs_create_dir("nested", kvm->debugfs_dentry);
+		debugfs_create_file("shadow_page_tables", 0400, kvm->arch.debugfs_nv_dentry,
+				    kvm, &kvm_ptdump_guest_nested_fops);
+	}
 }
Signature · 1 line
2.43.0
#16 lore raw

Re: [PATCH v2 6/6] KVM: arm64: ptdump: Introduce the shadow ptdump file

In reply to Wei-Lin Chang
Quoted 79 lines · Wei-Lin Chang

Create a ptdump file for all shadow page tables. It will dump out all valid shadow page tables at the time of request, with the mmu's index, guest VTCR_EL2, VTTBR_EL2, and whether the guest stage-2 is enabled or not.

Also detach the nested mmu array under the mmu_lock in kvm_arch_flush_shadow_all() so readers cannot race with the array being removed, then free the old array after dropping the lock.

---
 arch/arm64/kvm/nested.c | 12 ++++++--
 arch/arm64/kvm/ptdump.c | 61 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 69 insertions(+), 4 deletions(-)
arch/arm64/kvm/nested.c +9 -3
index 6435efd65cb5..17a180ddf6ca 100644
@@ -1283,6 +1283,7 @@ void kvm_nested_s2_flush(struct kvm *kvm)
 
 void kvm_arch_flush_shadow_all(struct kvm *kvm)
 {
+	struct kvm_s2_mmu *mmus;
 	int i;
 
 	for (i = 0; i < kvm->arch.nested_mmus_size; i++) {
@@ -1291,9 +1292,14 @@ void kvm_arch_flush_shadow_all(struct kvm *kvm)
 		if (!WARN_ON(atomic_read(&mmu->refcnt)))
 			kvm_free_stage2_pgd(mmu);
 	}
-	kvfree(kvm->arch.nested_mmus);
-	kvm->arch.nested_mmus = NULL;
-	kvm->arch.nested_mmus_size = 0;
+
+	scoped_guard(write_lock, &kvm->mmu_lock) {
+		mmus = kvm->arch.nested_mmus;
+		kvm->arch.nested_mmus = NULL;
+		kvm->arch.nested_mmus_size = 0;
+	}
+
+	kvfree(mmus);
 	kvm_uninit_stage2_mmu(kvm);
 }
 
arch/arm64/kvm/ptdump.c +26 -0
index 40f93b7c7ad9..1649eaa75798 100644
@@ -181,6 +181,50 @@ static int kvm_ptdump_guest_canonical_show(struct seq_file *m, void *unused)
 	return 0;
 }
 
+static int kvm_ptdump_guest_nested_show(struct seq_file *m, void *unused)
+{
+	int ret = 0, i;
+	struct kvm_ptdump_guest_state *st = m->private;
+	struct kvm *kvm = st->kvm;
+	struct kvm_pgtable_walker walker = (struct kvm_pgtable_walker) {
+		.cb	= kvm_ptdump_visitor,
+		.arg	= &st->parser_state,
+		.flags	= KVM_PGTABLE_WALK_LEAF,
+	};
+
+	guard(write_lock)(&kvm->mmu_lock);
+
+	if (!kvm->arch.nested_mmus)
+		return 0;
+
+	for (i = 0; i < kvm->arch.nested_mmus_size; i++) {
+		struct kvm_s2_mmu *mmu = &kvm->arch.nested_mmus[i];
+
+		if (!mmu->pgt)
+			continue;
+
+		if (kvm_s2_mmu_valid(mmu)) {
+			memset(st, 0, sizeof(*st));
+			ret = kvm_ptdump_parser_init(st, kvm, mmu->pgt);
+			if (ret)

arch/arm64/kvm/ptdump.c static int kvm_ptdump_guest_canonical_show(struct seq_file *m, void *unused) · line 219

Quoted 10 lines
arch/arm64/kvm/ptdump.c +10 -0
+				return ret;
+			st->parser_state = (struct ptdump_pg_state) {
+				.marker		= &st->ipa_marker[0],
+				.level		= -1,
+				.pg_level	= &st->level[0],
+				.seq		= m,
+			};
+			seq_printf(m, "nested mmu %d VTCR: 0x%016llx VTTBR: 0x%016llx s2: %s\n",
+				   i, mmu->tlb_vtcr, mmu->tlb_vttbr,
+				   mmu->nested_stage2_enabled ? "enabled" : "disabled");

This header information in the debugfs "shadow_page_tables" file, under the nested directory is showing guest hypervisor's configuration while the file is designed to show nested guest's shadwo stage 2 translation tables layouts owned by Host EL2 hypervisor. Is this intentional?

Thanks,
Itaru.

Quoted 51 lines
arch/arm64/kvm/ptdump.c +24 -1
+			ret = kvm_pgtable_walk(mmu->pgt, 0, BIT(mmu->pgt->ia_bits), &walker);
+			if (ret)
+				return ret;
+		}
+	}
+	return ret;
+}
+
 static int kvm_ptdump_guest_open(struct inode *m, struct file *file,
 				 int (*show)(struct seq_file *, void *))
 {
@@ -212,6 +256,11 @@ static int kvm_ptdump_guest_canonical_open(struct inode *m, struct file *file)
 	return kvm_ptdump_guest_open(m, file, kvm_ptdump_guest_canonical_show);
 }
 
+static int kvm_ptdump_guest_nested_open(struct inode *m, struct file *file)
+{
+	return kvm_ptdump_guest_open(m, file, kvm_ptdump_guest_nested_show);
+}
+
 static int kvm_ptdump_guest_close(struct inode *m, struct file *file)
 {
 	struct kvm *kvm = m->i_private;
@@ -230,6 +279,13 @@ static const struct file_operations kvm_ptdump_guest_canonical_fops = {
 	.release	= kvm_ptdump_guest_close,
 };
 
+static const struct file_operations kvm_ptdump_guest_nested_fops = {
+	.open		= kvm_ptdump_guest_nested_open,
+	.read		= seq_read,
+	.llseek		= seq_lseek,
+	.release	= kvm_ptdump_guest_close,
+};
+
 static int kvm_pgtable_range_show(struct seq_file *m, void *unused)
 {
 	struct kvm *kvm = m->private;
@@ -307,6 +363,9 @@ void kvm_s2_ptdump_create_debugfs(struct kvm *kvm)
 			    kvm, &kvm_pgtable_range_fops);
 	debugfs_create_file("stage2_levels", 0400, kvm->debugfs_dentry,
 			    kvm, &kvm_pgtable_levels_fops);
-	if (cpus_have_final_cap(ARM64_HAS_NESTED_VIRT))
+	if (cpus_have_final_cap(ARM64_HAS_NESTED_VIRT)) {
 		kvm->arch.debugfs_nv_dentry = debugfs_create_dir("nested", kvm->debugfs_dentry);
+		debugfs_create_file("shadow_page_tables", 0400, kvm->arch.debugfs_nv_dentry,
+				    kvm, &kvm_ptdump_guest_nested_fops);
+	}
 }
Signature · 1 line
2.43.0
#17 lore raw

Re: [PATCH v2 6/6] KVM: arm64: ptdump: Introduce the shadow ptdump file

Quoted 87 lines · Itaru Kitayama
Quoted 86 lines · Wei-Lin Chang

Create a ptdump file for all shadow page tables. It will dump out all valid shadow page tables at the time of request, with the mmu's index, guest VTCR_EL2, VTTBR_EL2, and whether the guest stage-2 is enabled or not.

Also detach the nested mmu array under the mmu_lock in kvm_arch_flush_shadow_all() so readers cannot race with the array being removed, then free the old array after dropping the lock.

---
 arch/arm64/kvm/nested.c | 12 ++++++--
 arch/arm64/kvm/ptdump.c | 61 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 69 insertions(+), 4 deletions(-)
arch/arm64/kvm/nested.c +9 -3
index 6435efd65cb5..17a180ddf6ca 100644
@@ -1283,6 +1283,7 @@ void kvm_nested_s2_flush(struct kvm *kvm)
 
 void kvm_arch_flush_shadow_all(struct kvm *kvm)
 {
+	struct kvm_s2_mmu *mmus;
 	int i;
 
 	for (i = 0; i < kvm->arch.nested_mmus_size; i++) {
@@ -1291,9 +1292,14 @@ void kvm_arch_flush_shadow_all(struct kvm *kvm)
 		if (!WARN_ON(atomic_read(&mmu->refcnt)))
 			kvm_free_stage2_pgd(mmu);
 	}
-	kvfree(kvm->arch.nested_mmus);
-	kvm->arch.nested_mmus = NULL;
-	kvm->arch.nested_mmus_size = 0;
+
+	scoped_guard(write_lock, &kvm->mmu_lock) {
+		mmus = kvm->arch.nested_mmus;
+		kvm->arch.nested_mmus = NULL;
+		kvm->arch.nested_mmus_size = 0;
+	}
+
+	kvfree(mmus);
 	kvm_uninit_stage2_mmu(kvm);
 }
 
arch/arm64/kvm/ptdump.c +33 -0
index 40f93b7c7ad9..1649eaa75798 100644
@@ -181,6 +181,50 @@ static int kvm_ptdump_guest_canonical_show(struct seq_file *m, void *unused)
 	return 0;
 }
 
+static int kvm_ptdump_guest_nested_show(struct seq_file *m, void *unused)
+{
+	int ret = 0, i;
+	struct kvm_ptdump_guest_state *st = m->private;
+	struct kvm *kvm = st->kvm;
+	struct kvm_pgtable_walker walker = (struct kvm_pgtable_walker) {
+		.cb	= kvm_ptdump_visitor,
+		.arg	= &st->parser_state,
+		.flags	= KVM_PGTABLE_WALK_LEAF,
+	};
+
+	guard(write_lock)(&kvm->mmu_lock);
+
+	if (!kvm->arch.nested_mmus)
+		return 0;
+
+	for (i = 0; i < kvm->arch.nested_mmus_size; i++) {
+		struct kvm_s2_mmu *mmu = &kvm->arch.nested_mmus[i];
+
+		if (!mmu->pgt)
+			continue;
+
+		if (kvm_s2_mmu_valid(mmu)) {
+			memset(st, 0, sizeof(*st));
+			ret = kvm_ptdump_parser_init(st, kvm, mmu->pgt);
+			if (ret)
+				return ret;
+			st->parser_state = (struct ptdump_pg_state) {
+				.marker		= &st->ipa_marker[0],
+				.level		= -1,
+				.pg_level	= &st->level[0],
+				.seq		= m,
+			};
Quoted 8 lines

arch/arm64/kvm/ptdump.c static int kvm_ptdump_guest_canonical_show(struct seq_file *m, void *unused) · line 219

Quoted 3 lines
arch/arm64/kvm/ptdump.c +3 -0
+			seq_printf(m, "nested mmu %d VTCR: 0x%016llx VTTBR: 0x%016llx s2: %s\n",
+				   i, mmu->tlb_vtcr, mmu->tlb_vttbr,
+				   mmu->nested_stage2_enabled ? "enabled" : "disabled");

This header information in the debugfs "shadow_page_tables" file, under the nested directory is showing guest hypervisor's configuration while the file is designed to show nested guest's shadwo stage 2 translation tables layouts owned by Host EL2 hypervisor. Is this intentional?

Yes, this is intentional. Here we are exposing guest hypervisor VTCR, VTTBR, s2 enabled or not, because that is how the host hypervisor distinguishes between different nested mmus (lookup_s2_mmu()).

Thanks,
Wei-Lin Chang

Quoted 55 lines

Thanks,
Itaru.

Quoted 51 lines
arch/arm64/kvm/ptdump.c +24 -1
+			ret = kvm_pgtable_walk(mmu->pgt, 0, BIT(mmu->pgt->ia_bits), &walker);
+			if (ret)
+				return ret;
+		}
+	}
+	return ret;
+}
+
 static int kvm_ptdump_guest_open(struct inode *m, struct file *file,
 				 int (*show)(struct seq_file *, void *))
 {
@@ -212,6 +256,11 @@ static int kvm_ptdump_guest_canonical_open(struct inode *m, struct file *file)
 	return kvm_ptdump_guest_open(m, file, kvm_ptdump_guest_canonical_show);
 }
 
+static int kvm_ptdump_guest_nested_open(struct inode *m, struct file *file)
+{
+	return kvm_ptdump_guest_open(m, file, kvm_ptdump_guest_nested_show);
+}
+
 static int kvm_ptdump_guest_close(struct inode *m, struct file *file)
 {
 	struct kvm *kvm = m->i_private;
@@ -230,6 +279,13 @@ static const struct file_operations kvm_ptdump_guest_canonical_fops = {
 	.release	= kvm_ptdump_guest_close,
 };
 
+static const struct file_operations kvm_ptdump_guest_nested_fops = {
+	.open		= kvm_ptdump_guest_nested_open,
+	.read		= seq_read,
+	.llseek		= seq_lseek,
+	.release	= kvm_ptdump_guest_close,
+};
+
 static int kvm_pgtable_range_show(struct seq_file *m, void *unused)
 {
 	struct kvm *kvm = m->private;
@@ -307,6 +363,9 @@ void kvm_s2_ptdump_create_debugfs(struct kvm *kvm)
 			    kvm, &kvm_pgtable_range_fops);
 	debugfs_create_file("stage2_levels", 0400, kvm->debugfs_dentry,
 			    kvm, &kvm_pgtable_levels_fops);
-	if (cpus_have_final_cap(ARM64_HAS_NESTED_VIRT))
+	if (cpus_have_final_cap(ARM64_HAS_NESTED_VIRT)) {
 		kvm->arch.debugfs_nv_dentry = debugfs_create_dir("nested", kvm->debugfs_dentry);
+		debugfs_create_file("shadow_page_tables", 0400, kvm->arch.debugfs_nv_dentry,
+				    kvm, &kvm_ptdump_guest_nested_fops);
+	}
 }
Signature · 1 line
2.43.0
#18 lore raw
In reply to Wei-Lin Chang

Hi Wei-Lin,

Quoted 24 lines · Wei-Lin Chang

Hi,

This is v2 of fixing shadow ptdump debugfs files. Unfortunately I couldn't make per mmu ptdump files work after all, mainly because there isn't a clean way to locate the specific nested mmu for each ptdump file as the nested mmus could be freed when the file gets opened. Therefore in this series a single file "shadow_page_tables" is created that dumps all valid mmus' page table information.

An advantage of this is that this new ptdump file have a lifetime identical to other ptdump files i.e. stage2_page_tables, ipa_range, etc., hence avoiding the dentry UAF found last time [1].

With this all ptdump files are only removed when the last kvm reference gets dropped and kvm_destroy_vm_debugfs() is called, in their open(), show() functions the nested mmu array and mmu->pgt are checked with mmu_lock held to prevent UAF.

Patch 1-2: Undo previous shadow ptdump implementation.
Patch 3:   Fix a mmu->pgt UAF that happens when ptdump files are read after
           mmu->pgt is freed.
Patch 4-5: Preparation for the shadow page table dump file.
Patch 6:   Implementation of the shadow page table dump file.
Quoted 4 lines

The fixes are tested with CONFIG_PROVE_LOCKING,
CONFIG_DEBUG_ATOMIC_SLEEP, and CONFIG_KASAN.

Thanks!

Running your shadow stage 2 kselftest with bpftrace shows me that __kvm_pgtable_stage2_init() for shadow stage 2 translation tables are built with ia_bits = 52 and start_level = 0, but the debugfs entry for the active shadow stage 2 tables prints out that's 3 levels. Is this fully expected?

Thanks,
Itaru.

Quoted 28 lines

* Changes from v1 ([2]):

- Move from per mmu ptdump files to one file that will dump all shadow page tables.

[1]: https://lore.kernel.org/kvmarm/ajty6I7ZqodP4ous@sm-arm-grace07/
[2]: https://lore.kernel.org/kvmarm/[email protected]/

Wei-Lin Chang (6):
KVM: arm64: ptdump: Remove shadow ptdump files
KVM: arm64: ptdump: Undo making the ptdump code mmu aware
KVM: arm64: ptdump: Fix UAF when mmu->pgt is freed
KVM: arm64: ptdump: Factor out initialization of
kvm_ptdump_guest_state
KVM: arm64: ptdump: Extract kvm_ptdump_guest_open() from canonical
ptdump path
KVM: arm64: ptdump: Introduce the shadow ptdump file

 arch/arm64/include/asm/kvm_host.h |   5 +-
 arch/arm64/include/asm/kvm_mmu.h  |   4 -
 arch/arm64/kvm/nested.c           |  18 +--
 arch/arm64/kvm/ptdump.c           | 185 ++++++++++++++++++++----------
 4 files changed, 135 insertions(+), 77 deletions(-)
Signature · 1 line
2.43.0
#19 lore raw
Quoted 29 lines · Itaru Kitayama

Hi Wei-Lin,

Quoted 27 lines · Wei-Lin Chang

Hi,

This is v2 of fixing shadow ptdump debugfs files. Unfortunately I couldn't make per mmu ptdump files work after all, mainly because there isn't a clean way to locate the specific nested mmu for each ptdump file as the nested mmus could be freed when the file gets opened. Therefore in this series a single file "shadow_page_tables" is created that dumps all valid mmus' page table information.

An advantage of this is that this new ptdump file have a lifetime identical to other ptdump files i.e. stage2_page_tables, ipa_range, etc., hence avoiding the dentry UAF found last time [1].

With this all ptdump files are only removed when the last kvm reference gets dropped and kvm_destroy_vm_debugfs() is called, in their open(), show() functions the nested mmu array and mmu->pgt are checked with mmu_lock held to prevent UAF.

Patch 1-2: Undo previous shadow ptdump implementation.
Patch 3:   Fix a mmu->pgt UAF that happens when ptdump files are read after
           mmu->pgt is freed.
Patch 4-5: Preparation for the shadow page table dump file.
Patch 6:   Implementation of the shadow page table dump file.

The fixes are tested with CONFIG_PROVE_LOCKING,
CONFIG_DEBUG_ATOMIC_SLEEP, and CONFIG_KASAN.

Quoted 6 lines
Quoted 1 line

Thanks!

Running your shadow stage 2 kselftest with bpftrace shows me that __kvm_pgtable_stage2_init() for shadow stage 2 translation tables are built with ia_bits = 52 and start_level = 0, but the debugfs entry for the active shadow stage 2 tables prints out that's 3 levels. Is this fully expected?

Where is this level information you are seeing from? If it is "stage2_level", that only reports the number of levels for the canonical stage-2 (non nested). For nested mmus only the page tables are dumped in nested/shadow_page_tables.

Thanks,
Wei-Lin Chang

Quoted 32 lines

Thanks,
Itaru.

Quoted 28 lines

* Changes from v1 ([2]):

- Move from per mmu ptdump files to one file that will dump all shadow page tables.

[1]: https://lore.kernel.org/kvmarm/ajty6I7ZqodP4ous@sm-arm-grace07/
[2]: https://lore.kernel.org/kvmarm/[email protected]/

Wei-Lin Chang (6):
KVM: arm64: ptdump: Remove shadow ptdump files
KVM: arm64: ptdump: Undo making the ptdump code mmu aware
KVM: arm64: ptdump: Fix UAF when mmu->pgt is freed
KVM: arm64: ptdump: Factor out initialization of
kvm_ptdump_guest_state
KVM: arm64: ptdump: Extract kvm_ptdump_guest_open() from canonical
ptdump path
KVM: arm64: ptdump: Introduce the shadow ptdump file

 arch/arm64/include/asm/kvm_host.h |   5 +-
 arch/arm64/include/asm/kvm_mmu.h  |   4 -
 arch/arm64/kvm/nested.c           |  18 +--
 arch/arm64/kvm/ptdump.c           | 185 ++++++++++++++++++++----------
 4 files changed, 135 insertions(+), 77 deletions(-)
Signature · 1 line
2.43.0
#20 lore raw
Quoted 37 lines · Wei-Lin Chang
Quoted 29 lines · Itaru Kitayama

Hi Wei-Lin,

Quoted 27 lines · Wei-Lin Chang

Hi,

This is v2 of fixing shadow ptdump debugfs files. Unfortunately I couldn't make per mmu ptdump files work after all, mainly because there isn't a clean way to locate the specific nested mmu for each ptdump file as the nested mmus could be freed when the file gets opened. Therefore in this series a single file "shadow_page_tables" is created that dumps all valid mmus' page table information.

An advantage of this is that this new ptdump file have a lifetime identical to other ptdump files i.e. stage2_page_tables, ipa_range, etc., hence avoiding the dentry UAF found last time [1].

With this all ptdump files are only removed when the last kvm reference gets dropped and kvm_destroy_vm_debugfs() is called, in their open(), show() functions the nested mmu array and mmu->pgt are checked with mmu_lock held to prevent UAF.

Patch 1-2: Undo previous shadow ptdump implementation.
Patch 3:   Fix a mmu->pgt UAF that happens when ptdump files are read after
           mmu->pgt is freed.
Patch 4-5: Preparation for the shadow page table dump file.
Patch 6:   Implementation of the shadow page table dump file.

The fixes are tested with CONFIG_PROVE_LOCKING,
CONFIG_DEBUG_ATOMIC_SLEEP, and CONFIG_KASAN.

Quoted 6 lines
Quoted 1 line

Thanks!

Running your shadow stage 2 kselftest with bpftrace shows me that __kvm_pgtable_stage2_init() for shadow stage 2 translation tables are built with ia_bits = 52 and start_level = 0, but the debugfs entry for the active shadow stage 2 tables prints out that's 3 levels. Is this fully expected?

Quoted 4 lines

Where is this level information you are seeing from? If it is "stage2_level", that only reports the number of levels for the canonical stage-2 (non nested). For nested mmus only the page tables are dumped in nested/shadow_page_tables.

Yes I know. The initial stage 2 translation table structure information is obtained by instrumenting the kernel using eBPF fexit to __kvm_pgtable_stage2_init().

Since you're correctly loopin over nested_mmus array, and the output is correctly shown using the kvm_pgtable information via the kvm_s2_mmu for them, I am confused at this moment.

Thanks,
Itaru.

Quoted 36 lines

Thanks,
Wei-Lin Chang

Quoted 32 lines

Thanks,
Itaru.

Quoted 28 lines

* Changes from v1 ([2]):

- Move from per mmu ptdump files to one file that will dump all shadow page tables.

[1]: https://lore.kernel.org/kvmarm/ajty6I7ZqodP4ous@sm-arm-grace07/
[2]: https://lore.kernel.org/kvmarm/[email protected]/

Wei-Lin Chang (6):
KVM: arm64: ptdump: Remove shadow ptdump files
KVM: arm64: ptdump: Undo making the ptdump code mmu aware
KVM: arm64: ptdump: Fix UAF when mmu->pgt is freed
KVM: arm64: ptdump: Factor out initialization of
kvm_ptdump_guest_state
KVM: arm64: ptdump: Extract kvm_ptdump_guest_open() from canonical
ptdump path
KVM: arm64: ptdump: Introduce the shadow ptdump file

 arch/arm64/include/asm/kvm_host.h |   5 +-
 arch/arm64/include/asm/kvm_mmu.h  |   4 -
 arch/arm64/kvm/nested.c           |  18 +--
 arch/arm64/kvm/ptdump.c           | 185 ++++++++++++++++++++----------
 4 files changed, 135 insertions(+), 77 deletions(-)
Signature · 1 line
2.43.0
#21 lore raw
Quoted 43 lines · Itaru Kitayama
Quoted 37 lines · Wei-Lin Chang
Quoted 29 lines · Itaru Kitayama

Hi Wei-Lin,

Quoted 27 lines · Wei-Lin Chang

Hi,

This is v2 of fixing shadow ptdump debugfs files. Unfortunately I couldn't make per mmu ptdump files work after all, mainly because there isn't a clean way to locate the specific nested mmu for each ptdump file as the nested mmus could be freed when the file gets opened. Therefore in this series a single file "shadow_page_tables" is created that dumps all valid mmus' page table information.

An advantage of this is that this new ptdump file have a lifetime identical to other ptdump files i.e. stage2_page_tables, ipa_range, etc., hence avoiding the dentry UAF found last time [1].

With this all ptdump files are only removed when the last kvm reference gets dropped and kvm_destroy_vm_debugfs() is called, in their open(), show() functions the nested mmu array and mmu->pgt are checked with mmu_lock held to prevent UAF.

Patch 1-2: Undo previous shadow ptdump implementation.
Patch 3:   Fix a mmu->pgt UAF that happens when ptdump files are read after
           mmu->pgt is freed.
Patch 4-5: Preparation for the shadow page table dump file.
Patch 6:   Implementation of the shadow page table dump file.

The fixes are tested with CONFIG_PROVE_LOCKING,
CONFIG_DEBUG_ATOMIC_SLEEP, and CONFIG_KASAN.

Quoted 6 lines
Quoted 1 line

Thanks!

Running your shadow stage 2 kselftest with bpftrace shows me that __kvm_pgtable_stage2_init() for shadow stage 2 translation tables are built with ia_bits = 52 and start_level = 0, but the debugfs entry for the active shadow stage 2 tables prints out that's 3 levels. Is this fully expected?

Quoted 4 lines

Where is this level information you are seeing from? If it is "stage2_level", that only reports the number of levels for the canonical stage-2 (non nested). For nested mmus only the page tables are dumped in nested/shadow_page_tables.

Quoted 6 lines

Yes I know. The initial stage 2 translation table structure information is obtained by instrumenting the kernel using eBPF fexit to __kvm_pgtable_stage2_init().

Since you're correctly loopin over nested_mmus array, and the output is correctly shown using the kvm_pgtable information via the kvm_s2_mmu for them, I am confused at this moment.

Two things here:

Firstly ia_bits being 52 is not expected to me, as I didn't change the VM's IPA size via KVM_VM_TYPE_ARM_IPA_SIZE in the selftest, to me I would expect ia_bits to be 40 (the default). Start level being 0 looks normal.

Second thing is I still can't get what you are confused about, if it's not about the first point. Note that the "3" in ptdump means it is a level 3 mapping, not saying the page tables are using 3 levels.

Happy to discuss more if you clarify. I'll also check the ia_bits when I get to it.

Thanks,
Wei-Lin Chang

Quoted 40 lines

Thanks,
Itaru.

Quoted 36 lines

Thanks,
Wei-Lin Chang

Quoted 32 lines

Thanks,
Itaru.

Quoted 28 lines

* Changes from v1 ([2]):

- Move from per mmu ptdump files to one file that will dump all shadow page tables.

[1]: https://lore.kernel.org/kvmarm/ajty6I7ZqodP4ous@sm-arm-grace07/
[2]: https://lore.kernel.org/kvmarm/[email protected]/

Wei-Lin Chang (6):
KVM: arm64: ptdump: Remove shadow ptdump files
KVM: arm64: ptdump: Undo making the ptdump code mmu aware
KVM: arm64: ptdump: Fix UAF when mmu->pgt is freed
KVM: arm64: ptdump: Factor out initialization of
kvm_ptdump_guest_state
KVM: arm64: ptdump: Extract kvm_ptdump_guest_open() from canonical
ptdump path
KVM: arm64: ptdump: Introduce the shadow ptdump file

 arch/arm64/include/asm/kvm_host.h |   5 +-
 arch/arm64/include/asm/kvm_mmu.h  |   4 -
 arch/arm64/kvm/nested.c           |  18 +--
 arch/arm64/kvm/ptdump.c           | 185 ++++++++++++++++++++----------
 4 files changed, 135 insertions(+), 77 deletions(-)
Signature · 1 line
2.43.0
#22 lore raw
Quoted 51 lines · Wei-Lin Chang
Quoted 43 lines · Itaru Kitayama
Quoted 37 lines · Wei-Lin Chang
Quoted 29 lines · Itaru Kitayama

Hi Wei-Lin,

Quoted 27 lines · Wei-Lin Chang

Hi,

This is v2 of fixing shadow ptdump debugfs files. Unfortunately I couldn't make per mmu ptdump files work after all, mainly because there isn't a clean way to locate the specific nested mmu for each ptdump file as the nested mmus could be freed when the file gets opened. Therefore in this series a single file "shadow_page_tables" is created that dumps all valid mmus' page table information.

An advantage of this is that this new ptdump file have a lifetime identical to other ptdump files i.e. stage2_page_tables, ipa_range, etc., hence avoiding the dentry UAF found last time [1].

With this all ptdump files are only removed when the last kvm reference gets dropped and kvm_destroy_vm_debugfs() is called, in their open(), show() functions the nested mmu array and mmu->pgt are checked with mmu_lock held to prevent UAF.

Patch 1-2: Undo previous shadow ptdump implementation.
Patch 3:   Fix a mmu->pgt UAF that happens when ptdump files are read after
           mmu->pgt is freed.
Patch 4-5: Preparation for the shadow page table dump file.
Patch 6:   Implementation of the shadow page table dump file.

The fixes are tested with CONFIG_PROVE_LOCKING,
CONFIG_DEBUG_ATOMIC_SLEEP, and CONFIG_KASAN.

Quoted 6 lines
Quoted 1 line

Thanks!

Running your shadow stage 2 kselftest with bpftrace shows me that __kvm_pgtable_stage2_init() for shadow stage 2 translation tables are built with ia_bits = 52 and start_level = 0, but the debugfs entry for the active shadow stage 2 tables prints out that's 3 levels. Is this fully expected?

Quoted 4 lines

Where is this level information you are seeing from? If it is "stage2_level", that only reports the number of levels for the canonical stage-2 (non nested). For nested mmus only the page tables are dumped in nested/shadow_page_tables.

Quoted 6 lines

Yes I know. The initial stage 2 translation table structure information is obtained by instrumenting the kernel using eBPF fexit to __kvm_pgtable_stage2_init().

Since you're correctly loopin over nested_mmus array, and the output is correctly shown using the kvm_pgtable information via the kvm_s2_mmu for them, I am confused at this moment.

Quoted 6 lines

Two things here:

Firstly ia_bits being 52 is not expected to me, as I didn't change the VM's IPA size via KVM_VM_TYPE_ARM_IPA_SIZE in the selftest, to me I would expect ia_bits to be 40 (the default). Start level being 0 looks normal.

Instrumenting the ptdump code you're proposing I see the shadow stage 2 translation tables are constructed with the max 52-bit IPA; irrespective of the IPA bits set in the init_s2_mmu() helper for the guest hypervisor setup in the selftest, I am guessing here to me fixed? 52-bit IPA size because it's awkward if nestd guest's IPA size is larger than the shadow stage 2's IPA size.

I confirmed using the __vm_create() helper with various guest modes supported that canonical stage 2's "ipa_range" changes according to the guest (L1) PA size so I wonder if it is a good idea to create something like shadow_ipa_range and shadow_stage2_levels files under nested directoy.

Thanks,
Itaru.

Quoted 51 lines

Second thing is I still can't get what you are confused about, if it's not about the first point. Note that the "3" in ptdump means it is a level 3 mapping, not saying the page tables are using 3 levels.

Happy to discuss more if you clarify. I'll also check the ia_bits when I get to it.

Thanks,
Wei-Lin Chang

Quoted 40 lines

Thanks,
Itaru.

Quoted 36 lines

Thanks,
Wei-Lin Chang

Quoted 32 lines

Thanks,
Itaru.

Quoted 28 lines

* Changes from v1 ([2]):

- Move from per mmu ptdump files to one file that will dump all shadow page tables.

[1]: https://lore.kernel.org/kvmarm/ajty6I7ZqodP4ous@sm-arm-grace07/
[2]: https://lore.kernel.org/kvmarm/[email protected]/

Wei-Lin Chang (6):
KVM: arm64: ptdump: Remove shadow ptdump files
KVM: arm64: ptdump: Undo making the ptdump code mmu aware
KVM: arm64: ptdump: Fix UAF when mmu->pgt is freed
KVM: arm64: ptdump: Factor out initialization of
kvm_ptdump_guest_state
KVM: arm64: ptdump: Extract kvm_ptdump_guest_open() from canonical
ptdump path
KVM: arm64: ptdump: Introduce the shadow ptdump file

 arch/arm64/include/asm/kvm_host.h |   5 +-
 arch/arm64/include/asm/kvm_mmu.h  |   4 -
 arch/arm64/kvm/nested.c           |  18 +--
 arch/arm64/kvm/ptdump.c           | 185 ++++++++++++++++++++----------
 4 files changed, 135 insertions(+), 77 deletions(-)
Signature · 1 line
2.43.0
#23 lore raw
Quoted 59 lines · Itaru Kitayama
Quoted 51 lines · Wei-Lin Chang
Quoted 43 lines · Itaru Kitayama
Quoted 37 lines · Wei-Lin Chang
Quoted 29 lines · Itaru Kitayama

Hi Wei-Lin,

Quoted 27 lines · Wei-Lin Chang

Hi,

This is v2 of fixing shadow ptdump debugfs files. Unfortunately I couldn't make per mmu ptdump files work after all, mainly because there isn't a clean way to locate the specific nested mmu for each ptdump file as the nested mmus could be freed when the file gets opened. Therefore in this series a single file "shadow_page_tables" is created that dumps all valid mmus' page table information.

An advantage of this is that this new ptdump file have a lifetime identical to other ptdump files i.e. stage2_page_tables, ipa_range, etc., hence avoiding the dentry UAF found last time [1].

With this all ptdump files are only removed when the last kvm reference gets dropped and kvm_destroy_vm_debugfs() is called, in their open(), show() functions the nested mmu array and mmu->pgt are checked with mmu_lock held to prevent UAF.

Patch 1-2: Undo previous shadow ptdump implementation.
Patch 3:   Fix a mmu->pgt UAF that happens when ptdump files are read after
           mmu->pgt is freed.
Patch 4-5: Preparation for the shadow page table dump file.
Patch 6:   Implementation of the shadow page table dump file.

The fixes are tested with CONFIG_PROVE_LOCKING,
CONFIG_DEBUG_ATOMIC_SLEEP, and CONFIG_KASAN.

Quoted 6 lines
Quoted 1 line

Thanks!

Running your shadow stage 2 kselftest with bpftrace shows me that __kvm_pgtable_stage2_init() for shadow stage 2 translation tables are built with ia_bits = 52 and start_level = 0, but the debugfs entry for the active shadow stage 2 tables prints out that's 3 levels. Is this fully expected?

Quoted 4 lines

Where is this level information you are seeing from? If it is "stage2_level", that only reports the number of levels for the canonical stage-2 (non nested). For nested mmus only the page tables are dumped in nested/shadow_page_tables.

Quoted 6 lines

Yes I know. The initial stage 2 translation table structure information is obtained by instrumenting the kernel using eBPF fexit to __kvm_pgtable_stage2_init().

Since you're correctly loopin over nested_mmus array, and the output is correctly shown using the kvm_pgtable information via the kvm_s2_mmu for them, I am confused at this moment.

Quoted 6 lines

Two things here:

Firstly ia_bits being 52 is not expected to me, as I didn't change the VM's IPA size via KVM_VM_TYPE_ARM_IPA_SIZE in the selftest, to me I would expect ia_bits to be 40 (the default). Start level being 0 looks normal.

Quoted 6 lines

Instrumenting the ptdump code you're proposing I see the shadow stage 2 translation tables are constructed with the max 52-bit IPA; irrespective of the IPA bits set in the init_s2_mmu() helper for the guest hypervisor setup in the selftest, I am guessing here to me fixed? 52-bit IPA size because it's awkward if nestd guest's IPA size is larger than the shadow stage 2's IPA size.

Sorry, I got it wrong. It is correct that shadow stage-2 translation tables are constructed with 52-bit IPA, irrespective of what L1 sets in init_s2_mmu(). This is because L1 is free to decide what memory map it exposes to its guest, so we only constrain it with the PARange exposed to the guest. Please see the comments in init_nested_s2_mmu().

Quoted 5 lines

I confirmed using the __vm_create() helper with various guest modes supported that canonical stage 2's "ipa_range" changes according to the guest (L1) PA size so I wonder if it is a good idea to create something like shadow_ipa_range and shadow_stage2_levels files under nested directoy.

I guess shadow_ipa_range and shadow_stage2_levels can be added if there are explicit demand for it. Although right now they aren't too difficult to infer from the PARange exposed to the guest.

Thanks,
Wei-Lin Chang

Quoted 55 lines

Thanks,
Itaru.

Quoted 51 lines

Second thing is I still can't get what you are confused about, if it's not about the first point. Note that the "3" in ptdump means it is a level 3 mapping, not saying the page tables are using 3 levels.

Happy to discuss more if you clarify. I'll also check the ia_bits when I get to it.

Thanks,
Wei-Lin Chang

Quoted 40 lines

Thanks,
Itaru.

Quoted 36 lines

Thanks,
Wei-Lin Chang

Quoted 32 lines

Thanks,
Itaru.

Quoted 28 lines

* Changes from v1 ([2]):

- Move from per mmu ptdump files to one file that will dump all shadow page tables.

[1]: https://lore.kernel.org/kvmarm/ajty6I7ZqodP4ous@sm-arm-grace07/
[2]: https://lore.kernel.org/kvmarm/[email protected]/

Wei-Lin Chang (6):
KVM: arm64: ptdump: Remove shadow ptdump files
KVM: arm64: ptdump: Undo making the ptdump code mmu aware
KVM: arm64: ptdump: Fix UAF when mmu->pgt is freed
KVM: arm64: ptdump: Factor out initialization of
kvm_ptdump_guest_state
KVM: arm64: ptdump: Extract kvm_ptdump_guest_open() from canonical
ptdump path
KVM: arm64: ptdump: Introduce the shadow ptdump file

 arch/arm64/include/asm/kvm_host.h |   5 +-
 arch/arm64/include/asm/kvm_mmu.h  |   4 -
 arch/arm64/kvm/nested.c           |  18 +--
 arch/arm64/kvm/ptdump.c           | 185 ++++++++++++++++++++----------
 4 files changed, 135 insertions(+), 77 deletions(-)
Signature · 1 line
2.43.0
Tested-by
#24 lore raw
Quoted 67 lines · Wei-Lin Chang
Quoted 59 lines · Itaru Kitayama
Quoted 51 lines · Wei-Lin Chang
Quoted 43 lines · Itaru Kitayama
Quoted 37 lines · Wei-Lin Chang
Quoted 29 lines · Itaru Kitayama

Hi Wei-Lin,

Quoted 27 lines · Wei-Lin Chang

Hi,

This is v2 of fixing shadow ptdump debugfs files. Unfortunately I couldn't make per mmu ptdump files work after all, mainly because there isn't a clean way to locate the specific nested mmu for each ptdump file as the nested mmus could be freed when the file gets opened. Therefore in this series a single file "shadow_page_tables" is created that dumps all valid mmus' page table information.

An advantage of this is that this new ptdump file have a lifetime identical to other ptdump files i.e. stage2_page_tables, ipa_range, etc., hence avoiding the dentry UAF found last time [1].

With this all ptdump files are only removed when the last kvm reference gets dropped and kvm_destroy_vm_debugfs() is called, in their open(), show() functions the nested mmu array and mmu->pgt are checked with mmu_lock held to prevent UAF.

Patch 1-2: Undo previous shadow ptdump implementation.
Patch 3:   Fix a mmu->pgt UAF that happens when ptdump files are read after
           mmu->pgt is freed.
Patch 4-5: Preparation for the shadow page table dump file.
Patch 6:   Implementation of the shadow page table dump file.

The fixes are tested with CONFIG_PROVE_LOCKING,
CONFIG_DEBUG_ATOMIC_SLEEP, and CONFIG_KASAN.

Quoted 6 lines
Quoted 1 line

Thanks!

Running your shadow stage 2 kselftest with bpftrace shows me that __kvm_pgtable_stage2_init() for shadow stage 2 translation tables are built with ia_bits = 52 and start_level = 0, but the debugfs entry for the active shadow stage 2 tables prints out that's 3 levels. Is this fully expected?

Quoted 4 lines

Where is this level information you are seeing from? If it is "stage2_level", that only reports the number of levels for the canonical stage-2 (non nested). For nested mmus only the page tables are dumped in nested/shadow_page_tables.

Quoted 6 lines

Yes I know. The initial stage 2 translation table structure information is obtained by instrumenting the kernel using eBPF fexit to __kvm_pgtable_stage2_init().

Since you're correctly loopin over nested_mmus array, and the output is correctly shown using the kvm_pgtable information via the kvm_s2_mmu for them, I am confused at this moment.

Quoted 6 lines

Two things here:

Firstly ia_bits being 52 is not expected to me, as I didn't change the VM's IPA size via KVM_VM_TYPE_ARM_IPA_SIZE in the selftest, to me I would expect ia_bits to be 40 (the default). Start level being 0 looks normal.

Quoted 6 lines

Instrumenting the ptdump code you're proposing I see the shadow stage 2 translation tables are constructed with the max 52-bit IPA; irrespective of the IPA bits set in the init_s2_mmu() helper for the guest hypervisor setup in the selftest, I am guessing here to me fixed? 52-bit IPA size because it's awkward if nestd guest's IPA size is larger than the shadow stage 2's IPA size.

Quoted 5 lines

Sorry, I got it wrong. It is correct that shadow stage-2 translation tables are constructed with 52-bit IPA, irrespective of what L1 sets in init_s2_mmu(). This is because L1 is free to decide what memory map it exposes to its guest, so we only constrain it with the PARange exposed to the guest. Please see the comments in init_nested_s2_mmu().

Thanks for the cofirmation, yes on QEMU the "max" CPU model that's what the PARange exposes, bpftrace was showing that too.

Quoted 10 lines
Quoted 6 lines

I confirmed using the __vm_create() helper with various guest modes supported that canonical stage 2's "ipa_range" changes according to the guest (L1) PA size so I wonder if it is a good idea to create something like shadow_ipa_range and shadow_stage2_levels files under nested directoy.

I guess shadow_ipa_range and shadow_stage2_levels can be added if there are explicit demand for it. Although right now they aren't too difficult to infer from the PARange exposed to the guest.

Surely, it's up to you. If you kindly take my Tested-by: tag, that'd be great the kernel I used for this series is the latest kvmarm/fixes, with your series applied I can see the mapping information extracted by walking the shadow stage 2 translation tables.

Thanks,
Itaru.

Quoted 59 lines

Thanks,
Wei-Lin Chang

Quoted 55 lines

Thanks,
Itaru.

Quoted 51 lines

Second thing is I still can't get what you are confused about, if it's not about the first point. Note that the "3" in ptdump means it is a level 3 mapping, not saying the page tables are using 3 levels.

Happy to discuss more if you clarify. I'll also check the ia_bits when I get to it.

Thanks,
Wei-Lin Chang

Quoted 40 lines

Thanks,
Itaru.

Quoted 36 lines

Thanks,
Wei-Lin Chang

Quoted 32 lines

Thanks,
Itaru.

Quoted 28 lines

* Changes from v1 ([2]):

- Move from per mmu ptdump files to one file that will dump all shadow page tables.

[1]: https://lore.kernel.org/kvmarm/ajty6I7ZqodP4ous@sm-arm-grace07/
[2]: https://lore.kernel.org/kvmarm/[email protected]/

Wei-Lin Chang (6):
KVM: arm64: ptdump: Remove shadow ptdump files
KVM: arm64: ptdump: Undo making the ptdump code mmu aware
KVM: arm64: ptdump: Fix UAF when mmu->pgt is freed
KVM: arm64: ptdump: Factor out initialization of
kvm_ptdump_guest_state
KVM: arm64: ptdump: Extract kvm_ptdump_guest_open() from canonical
ptdump path
KVM: arm64: ptdump: Introduce the shadow ptdump file

 arch/arm64/include/asm/kvm_host.h |   5 +-
 arch/arm64/include/asm/kvm_mmu.h  |   4 -
 arch/arm64/kvm/nested.c           |  18 +--
 arch/arm64/kvm/ptdump.c           | 185 ++++++++++++++++++++----------
 4 files changed, 135 insertions(+), 77 deletions(-)
Signature · 1 line
2.43.0