Skip to contentSkip to conversation

KVM: arm64: ptdump: Shadow ptdump fixes

PATCH Superseded Wei-Lin Chang 4 participants 8 messages Sep 16 – Sep 18

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover KVM: arm64: ptdump: Shadow ptdump fixes

Hi,

This is v3 of fixing the sleep-in-atomic problem of the shadow ptdump. The approach taken this time is simply creating a file for each nested mmu.

The ptdump files are created alongside the nested mmus in kvm_vcpu_init_nested(), in a sleepable context, and removed when the VM is destroyed (kvm_destroy_vm_debugfs(), before the nested mmus are freed in kvm_destroy_nested()). This allows us to store the kvm_s2_mmu pointers in the ptdump code, after Marc's nested mmu lifecycle fixes [1].

For the shadow ptdump, for valid nested mmus the first line will show the nested context's VTTBR and VTCR values, and whether s2 is enabled or not. The ptdump then follows.

The first patch fixes mmu->pgt accesses after the VM MMU teardown in ptdump.c, the second patch switches from per nested context ptdumps to per nested mmu ptdumps.

Tested with CONFIG_PROVE_LOCKING, CONFIG_DEBUG_ATOMIC_SLEEP, and CONFIG_KASAN.

Series based on kvmarm/fixes.

* Changes from v2 [2]:

- Changed from creating a file that contains all nested mmus to creating one file for each nested mmu.

Thanks!

[1]: https://lore.kernel.org/kvmarm/[email protected]/
[2]: https://lore.kernel.org/kvmarm/[email protected]/

Wei-Lin Chang (2):
KVM: arm64: ptdump: Check the page tables aren't freed when accessing
KVM: arm64: ptdump: Fix shadow ptdump sleep-in-atomic-context problem

 arch/arm64/include/asm/kvm_host.h |  4 --
 arch/arm64/include/asm/kvm_mmu.h  |  6 +--
 arch/arm64/kvm/nested.c           |  9 ++--
 arch/arm64/kvm/ptdump.c           | 75 ++++++++++++++++++++-----------
 4 files changed, 54 insertions(+), 40 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/2 KVM: arm64: ptdump: Check the page tables aren't freed when accessing

An open debugfs file keeps the KVM structure alive, but does not prevent mmu notifier release from freeing the stage-2 page tables when the VMM’s address space is torn down. Therefore page tables belonging to the mmus could have been freed when a thread opens or reads the ptdump files. Take the mmu_lock and check mmu->pgt is still alive before accessing the page tables.

Also for the ipa_range file and stage2_levels file, switch from keeping the pointer to the page tables to keeping the pointer to the mmu.

Fixes 7c4f73548ed1 ("KVM: arm64: Register ptdump with debugfs on guest creation")
---
 arch/arm64/kvm/ptdump.c | 43 +++++++++++++++++++++++++++++++----------
 1 file changed, 33 insertions(+), 10 deletions(-)
arch/arm64/kvm/ptdump.c +33 -10
index 69899797dbad..4ead856c181a 100644
@@ -115,15 +115,28 @@ 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_s2_mmu *mmu)
 {
+	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
 	struct kvm_ptdump_guest_state *st;
-	struct kvm_pgtable *pgtable = mmu->pgt;
+	s8 start_level;
 	int ret;
 
+	/*
+	 * We only need the pgt start level to initialize the ptdump, get it
+	 * while holding the mmu_lock. It's fine if the pgt gets freed
+	 * afterwards, we'll check again when doing the actual dump.
+	 */
+	scoped_guard(write_lock, &kvm->mmu_lock) {
+		if (mmu->pgt)
+			start_level = mmu->pgt->start_level;
+		else
+			return ERR_PTR(-ENOENT);
+	}
+
 	st = kzalloc_obj(struct kvm_ptdump_guest_state, GFP_KERNEL_ACCOUNT);
 	if (!st)
 		return ERR_PTR(-ENOMEM);
 
-	ret = kvm_ptdump_build_levels(&st->level[0], pgtable->start_level);
+	ret = kvm_ptdump_build_levels(&st->level[0], start_level);
 	if (ret) {
 		kfree(st);
 		return ERR_PTR(ret);
@@ -149,6 +162,9 @@ static int kvm_ptdump_guest_show(struct seq_file *m, void *unused)
 	};
 
 	guard(write_lock)(&kvm->mmu_lock);
+	if (!mmu->pgt)
+		return 0;
+
 	st->parser_state = (struct ptdump_pg_state) {
 		.marker		= &st->ipa_marker[0],
 		.end_address	= BIT(mmu->pgt->ia_bits),
@@ -211,17 +227,27 @@ 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_s2_mmu *mmu = m->private;
+	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
+
+	guard(write_lock)(&kvm->mmu_lock);
+
+	if (mmu->pgt)
+		seq_printf(m, "%2u\n", 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_s2_mmu *mmu = m->private;
+	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
+
+	guard(write_lock)(&kvm->mmu_lock);
+
+	if (mmu->pgt)
+		seq_printf(m, "%1d\n", KVM_PGTABLE_MAX_LEVELS - mmu->pgt->start_level);
 
-	seq_printf(m, "%1d\n", KVM_PGTABLE_MAX_LEVELS - pgtable->start_level);
 	return 0;
 }
 
@@ -230,15 +256,12 @@ static int kvm_pgtable_debugfs_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_pgtable *pgtable;
 	int ret;
 
 	if (!kvm_get_kvm_safe(kvm))
 		return -ENOENT;
 
-	pgtable = mmu->pgt;
-
-	ret = single_open(file, show, pgtable);
+	ret = single_open(file, show, mmu);
 	if (ret < 0)
 		kvm_put_kvm(kvm);
 	return ret;
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v3 1/2] KVM: arm64: ptdump: Check the page tables aren't freed when accessing

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] kvm: arm64: ptdump: prolonged write_lock on mmu_lock causes lock contention --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] kvm: arm64: ptdump: prolonged write_lock on mmu_lock causes lock contention --

commit 6e17983aae35d9aa5807fc7910feb63951dae7e2
Author: Wei-Lin Chang <[email protected]>
KVM: arm64: ptdump: Check the page tables aren't freed when accessing

This patch adds checks to ensure that stage-2 page tables are not freed during access when debugfs ptdump files are opened or read. It uses mmu_lock to protect the existence of the page tables and switches related handlers to keep a pointer to the mmu instead.

Quoted 4 lines
arch/arm64/kvm/ptdump.c +0 -0
index 69899797dbad8..4ead856c181a0 100644

[ ... ]

Quoted 4 lines

static struct kvm_ptdump_guest_state *kvm_ptdump_parser_create(struct kvm_s2_mmu *mmu) {

arch/arm64/kvm/ptdump.c +1 -0
+	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
 	struct kvm_ptdump_guest_state *st;

arch/arm64/kvm/ptdump.c static int kvm_ptdump_build_levels(struct ptdump_pg_level *level, u32 start_lvl) · line 128

Quoted 10 lines
arch/arm64/kvm/ptdump.c +7 -1
-	struct kvm_pgtable *pgtable = mmu->pgt;
+	s8 start_level;
 	int ret;
 
+	/*
+	 * We only need the pgt start level to initialize the ptdump, get it
+	 * while holding the mmu_lock. It's fine if the pgt gets freed
+	 * afterwards, we'll check again when doing the actual dump.
+	 */
+	scoped_guard(write_lock, &kvm->mmu_lock) {

[Severity: Medium]
Does this code unnecessarily acquire the exclusive write lock just to read the start_level in kvm_ptdump_parser_create()?

Since this is a read-only field check, acquiring the write lock could contend with VM page faults. Could read_lock be used here instead?

Quoted 5 lines
arch/arm64/kvm/ptdump.c +5 -0
+		if (mmu->pgt)
+			start_level = mmu->pgt->start_level;
+		else
+			return ERR_PTR(-ENOENT);
+	}

[ ... ]

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

Quoted 4 lines
arch/arm64/kvm/ptdump.c +0 -0
@@ -149,6 +162,9 @@ static int kvm_ptdump_guest_show(struct seq_file *m, void *unused)
 	};
 
 	guard(write_lock)(&kvm->mmu_lock);

[Severity: Medium]
Does this sequence create a denial of service regression by holding the exclusive write lock for the entire duration of a heavy page table walk?

The full page table walk is performed synchronously while the exclusive lock is held later in kvm_ptdump_guest_show():

    ret = kvm_pgtable_walk(mmu->pgt, 0, BIT(mmu->pgt->ia_bits), &walker);

This prolonged read-only walk blocks all VM page faults, causing severe lock contention.

Since a user reading the ptdump file from debugfs can trigger this path, it may allow host userspace to stall all vCPUs. Is it possible to perform this walk using read_lock instead?

Quoted 4 lines
arch/arm64/kvm/ptdump.c +3 -0
+	if (!mmu->pgt)
+		return 0;
+
 	st->parser_state = (struct ptdump_pg_state) {

[ ... ]

arch/arm64/kvm/ptdump.c static const struct file_operations kvm_ptdump_guest_fops = { · line 233

Quoted 9 lines
arch/arm64/kvm/ptdump.c +4 -1
@@ -211,17 +227,27 @@ 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_s2_mmu *mmu = m->private;
+	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
+
+	guard(write_lock)(&kvm->mmu_lock);

[Severity: Medium]
Does this code unnecessarily acquire the exclusive write lock for a read-only field check in kvm_pgtable_range_show()?

Quoted 5 lines
arch/arm64/kvm/ptdump.c +3 -1
+
+	if (mmu->pgt)
+		seq_printf(m, "%2u\n", mmu->pgt->ia_bits);
 
-	seq_printf(m, "%2u\n", pgtable->ia_bits);

arch/arm64/kvm/ptdump.c static const struct file_operations kvm_ptdump_guest_fops = { · line 246

Quoted 10 lines
arch/arm64/kvm/ptdump.c +4 -1
 	return 0;
 }
 
 static int kvm_pgtable_levels_show(struct seq_file *m, void *unused)
 {
-	struct kvm_pgtable *pgtable = m->private;
+	struct kvm_s2_mmu *mmu = m->private;
+	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
+
+	guard(write_lock)(&kvm->mmu_lock);

[Severity: Medium]
Similarly, does this code unnecessarily acquire the exclusive write lock for a read-only field check in kvm_pgtable_levels_show()?

Quoted 7 lines
arch/arm64/kvm/ptdump.c +3 -1
+
+	if (mmu->pgt)
+		seq_printf(m, "%1d\n", KVM_PGTABLE_MAX_LEVELS - mmu->pgt->start_level);
 
-	seq_printf(m, "%1d\n", KVM_PGTABLE_MAX_LEVELS - pgtable->start_level);
 	return 0;
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by Tested-by on v3 1/2
#3 lore raw

Re: [PATCH v3 1/2] KVM: arm64: ptdump: Check the page tables aren't freed when accessing

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

An open debugfs file keeps the KVM structure alive, but does not prevent mmu notifier release from freeing the stage-2 page tables when the VMM’s address space is torn down. Therefore page tables belonging to the mmus could have been freed when a thread opens or reads the ptdump files. Take the mmu_lock and check mmu->pgt is still alive before accessing the page tables.

As Sashiko said, the read_lock is probably enough, including the existing one in kvm_ptdump_guest_show()

With that change:

Quoted 110 lines

Also for the ipa_range file and stage2_levels file, switch from keeping the pointer to the page tables to keeping the pointer to the mmu.

Fixes 7c4f73548ed1 ("KVM: arm64: Register ptdump with debugfs on guest creation")
---
 arch/arm64/kvm/ptdump.c | 43 +++++++++++++++++++++++++++++++----------
 1 file changed, 33 insertions(+), 10 deletions(-)
arch/arm64/kvm/ptdump.c +33 -10
index 69899797dbad..4ead856c181a 100644
@@ -115,15 +115,28 @@ 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_s2_mmu *mmu)
 {
+	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
 	struct kvm_ptdump_guest_state *st;
-	struct kvm_pgtable *pgtable = mmu->pgt;
+	s8 start_level;
 	int ret;
 
+	/*
+	 * We only need the pgt start level to initialize the ptdump, get it
+	 * while holding the mmu_lock. It's fine if the pgt gets freed
+	 * afterwards, we'll check again when doing the actual dump.
+	 */
+	scoped_guard(write_lock, &kvm->mmu_lock) {
+		if (mmu->pgt)
+			start_level = mmu->pgt->start_level;
+		else
+			return ERR_PTR(-ENOENT);
+	}
+
 	st = kzalloc_obj(struct kvm_ptdump_guest_state, GFP_KERNEL_ACCOUNT);
 	if (!st)
 		return ERR_PTR(-ENOMEM);
 
-	ret = kvm_ptdump_build_levels(&st->level[0], pgtable->start_level);
+	ret = kvm_ptdump_build_levels(&st->level[0], start_level);
 	if (ret) {
 		kfree(st);
 		return ERR_PTR(ret);
@@ -149,6 +162,9 @@ static int kvm_ptdump_guest_show(struct seq_file *m, void *unused)
 	};
 
 	guard(write_lock)(&kvm->mmu_lock);
+	if (!mmu->pgt)
+		return 0;
+
 	st->parser_state = (struct ptdump_pg_state) {
 		.marker		= &st->ipa_marker[0],
 		.end_address	= BIT(mmu->pgt->ia_bits),
@@ -211,17 +227,27 @@ 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_s2_mmu *mmu = m->private;
+	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
+
+	guard(write_lock)(&kvm->mmu_lock);
+
+	if (mmu->pgt)
+		seq_printf(m, "%2u\n", 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_s2_mmu *mmu = m->private;
+	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
+
+	guard(write_lock)(&kvm->mmu_lock);
+
+	if (mmu->pgt)
+		seq_printf(m, "%1d\n", KVM_PGTABLE_MAX_LEVELS - mmu->pgt->start_level);
 
-	seq_printf(m, "%1d\n", KVM_PGTABLE_MAX_LEVELS - pgtable->start_level);
 	return 0;
 }
 
@@ -230,15 +256,12 @@ static int kvm_pgtable_debugfs_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_pgtable *pgtable;
 	int ret;
 
 	if (!kvm_get_kvm_safe(kvm))
 		return -ENOENT;
 
-	pgtable = mmu->pgt;
-
-	ret = single_open(file, show, pgtable);
+	ret = single_open(file, show, mmu);
 	if (ret < 0)
 		kvm_put_kvm(kvm);
 	return ret;
Signature · 1 line
2.43.0
Signature · 1 line
Vincent
#4 lore raw

Re: [PATCH v3 1/2] KVM: arm64: ptdump: Check the page tables aren't freed when accessing

Quoted 8 lines · Vincent Donnefort
Quoted 6 lines · Wei-Lin Chang

An open debugfs file keeps the KVM structure alive, but does not prevent mmu notifier release from freeing the stage-2 page tables when the VMM’s address space is torn down. Therefore page tables belonging to the mmus could have been freed when a thread opens or reads the ptdump files. Take the mmu_lock and check mmu->pgt is still alive before accessing the page tables.

Quoted 7 lines

As Sashiko said, the read_lock is probably enough, including the existing one in kvm_ptdump_guest_show()

With that change:

Thanks for the review and testing!

I agree taking read_lock is enough for most of these, but changing kvm_ptdump_guest_show() to read_lock could result in a dump showing weird output e.g. 0-sized ranges. This happens when the dump reads a block, and a parallel fault turns that block into a table, and the dump descends into the table later.

This is debugfs afterall so I think it isn't a dealbreaker, but it adds another purpose to this patch. Maybe we can change kvm_ptdump_guest_show() into taking a read_lock when someone reports a scalability problem when dumping the page tables.

I'll stick to changing the other ones into taking the read_lock now.

For future reference: KVM_PGTABLE_WALK_SHARED is required if we want to change kvm_ptdump_guest_show() into taking a read_lock.

Thanks,
Wei-Lin Chang

Quoted 1 line
#5 lore raw

Re: [PATCH v3 1/2] KVM: arm64: ptdump: Check the page tables aren't freed when accessing

Quoted 17 lines · Wei-Lin Chang
Quoted 8 lines · Vincent Donnefort
Quoted 6 lines · Wei-Lin Chang

An open debugfs file keeps the KVM structure alive, but does not prevent mmu notifier release from freeing the stage-2 page tables when the VMM’s address space is torn down. Therefore page tables belonging to the mmus could have been freed when a thread opens or reads the ptdump files. Take the mmu_lock and check mmu->pgt is still alive before accessing the page tables.

Quoted 7 lines

As Sashiko said, the read_lock is probably enough, including the existing one in kvm_ptdump_guest_show()

With that change:

Quoted 7 lines

Thanks for the review and testing!

I agree taking read_lock is enough for most of these, but changing kvm_ptdump_guest_show() to read_lock could result in a dump showing weird output e.g. 0-sized ranges. This happens when the dump reads a block, and a parallel fault turns that block into a table, and the dump descends into the table later.

Now you say it, I remember discussing that with Sebastian when he wrote the patches.

Quoted 15 lines

This is debugfs afterall so I think it isn't a dealbreaker, but it adds another purpose to this patch. Maybe we can change kvm_ptdump_guest_show() into taking a read_lock when someone reports a scalability problem when dumping the page tables.

I'll stick to changing the other ones into taking the read_lock now.

For future reference: KVM_PGTABLE_WALK_SHARED is required if we want to change kvm_ptdump_guest_show() into taking a read_lock.

Thanks,
Wei-Lin Chang

Quoted 1 line
Signature · 1 line
Vincent
#6 lore raw

PATCH v3 2/2 KVM: arm64: ptdump: Fix shadow ptdump sleep-in-atomic-context problem

In reply to Wei-Lin Chang

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.

Change the approach and create a shadow ptdump file for each nested mmu instead during kvm_vcpu_init_nested(), in a sleepable context. The files will be named nested_mmu<index>, and reading it will return the nested context's VTTBR, VTCR, and s2 enabled or not before dumping the shadow page tables, given the nested mmu is valid.

Fixes 19e15dc73f0f ("KVM: arm64: nv: Expose shadow page tables in debugfs")
---
 arch/arm64/include/asm/kvm_host.h |  4 ----
 arch/arm64/include/asm/kvm_mmu.h  |  6 ++----
 arch/arm64/kvm/nested.c           |  9 ++++-----
 arch/arm64/kvm/ptdump.c           | 32 +++++++++++++++----------------
 4 files changed, 21 insertions(+), 30 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -4
index cd9b9d2462f9..db177af9149f 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.
arch/arm64/include/asm/kvm_mmu.h +2 -4
index 6eae7e7e2a68..ac2b0637692a 100644
@@ -392,12 +392,10 @@ 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);
+void kvm_nested_s2_ptdump_create_debugfs(struct kvm_s2_mmu *mmu, int idx);
 #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) {}
+static inline void kvm_nested_s2_ptdump_create_debugfs(struct kvm_s2_mmu *mmu, int idx) {}
 #endif /* CONFIG_PTDUMP_STAGE2_DEBUGFS */
 
 #endif /* __ASSEMBLER__ */
arch/arm64/kvm/nested.c +4 -5
index b191365d97cc..41253fe45941 100644
@@ -124,6 +124,9 @@ int kvm_vcpu_init_nested(struct kvm_vcpu *vcpu)
 			return ret;
 		}
 
+		for (i = 0; i < S2_MMU_PER_VCPU; i++)
+			kvm_nested_s2_ptdump_create_debugfs(&tmp[i], i + kvm->arch.nested_mmus_size);
+
 		guard(write_lock)(&kvm->mmu_lock);
 
 		for (i = 0; i < S2_MMU_PER_VCPU; i++)
@@ -837,10 +840,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
@@ -854,8 +855,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 +15 -17
index 4ead856c181a..9411d6c2dafb 100644
@@ -17,7 +17,7 @@
 
 #define MARKERS_LEN		2
 #define KVM_PGTABLE_MAX_LEVELS	(KVM_PGTABLE_LAST_LEVEL + 1)
-#define S2FNAMESZ		sizeof("0x0123456789abcdef-0x0123456789abcdef-s2-disabled")
+#define S2FNAMESZ		sizeof("nested_mmu0000")
 
 struct kvm_ptdump_guest_state {
 	struct kvm_s2_mmu	*mmu;
@@ -173,6 +173,15 @@ static int kvm_ptdump_guest_show(struct seq_file *m, void *unused)
 		.seq		= m,
 	};
 
+	if (kvm_is_nested_s2_mmu(kvm, mmu)) {
+		if (kvm_s2_mmu_valid(mmu))
+			seq_printf(m, "VTCR: 0x%016llx VTTBR: 0x%016llx s2: %s\n",
+				   mmu->tlb_vtcr, mmu->tlb_vttbr,
+				   mmu->nested_stage2_enabled ? "enabled" : "disabled");
+		else
+			return 0;
+	}
+
 	ret = kvm_pgtable_walk(mmu->pgt, 0, BIT(mmu->pgt->ia_bits), &walker);
 	if (ret)
 		return ret;
@@ -299,26 +308,15 @@ 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)
+void kvm_nested_s2_ptdump_create_debugfs(struct kvm_s2_mmu *mmu, int idx)
 {
-	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);
+	snprintf(file_name, sizeof(file_name), "nested_mmu%d", idx);
 
-	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);
+	debugfs_create_file(file_name, 0400,
+			    mmu->arch->debugfs_nv_dentry, mmu,
+			    &kvm_ptdump_guest_fops);
 }
 
 void kvm_s2_ptdump_create_debugfs(struct kvm *kvm)
Signature · 1 line
2.43.0
Tested-by on the v3 series
#7 lore raw
In reply to Wei-Lin Chang

Hi Wei-Lin,

Quoted 23 lines · Wei-Lin Chang

Hi,

This is v3 of fixing the sleep-in-atomic problem of the shadow ptdump. The approach taken this time is simply creating a file for each nested mmu.

The ptdump files are created alongside the nested mmus in kvm_vcpu_init_nested(), in a sleepable context, and removed when the VM is destroyed (kvm_destroy_vm_debugfs(), before the nested mmus are freed in kvm_destroy_nested()). This allows us to store the kvm_s2_mmu pointers in the ptdump code, after Marc's nested mmu lifecycle fixes [1].

For the shadow ptdump, for valid nested mmus the first line will show the nested context's VTTBR and VTCR values, and whether s2 is enabled or not. The ptdump then follows.

The first patch fixes mmu->pgt accesses after the VM MMU teardown in ptdump.c, the second patch switches from per nested context ptdumps to per nested mmu ptdumps.

Tested with CONFIG_PROVE_LOCKING, CONFIG_DEBUG_ATOMIC_SLEEP, and CONFIG_KASAN.

Quoted 6 lines

Series based on kvmarm/fixes.

* Changes from v2 [2]:

- Changed from creating a file that contains all nested mmus to creating one file for each nested mmu.

I've booted your kernel built only CONFIG_DEBUG_ATOMIC_SLEEP enabled mentioned above kernel configs; on Grace CPUs shadow_stage2 test did not trigger the sleep in atomic message.

Thanks,
Itaru.

Quoted 19 lines

Thanks!

[1]: https://lore.kernel.org/kvmarm/[email protected]/
[2]: https://lore.kernel.org/kvmarm/[email protected]/

Wei-Lin Chang (2):
KVM: arm64: ptdump: Check the page tables aren't freed when accessing
KVM: arm64: ptdump: Fix shadow ptdump sleep-in-atomic-context problem

 arch/arm64/include/asm/kvm_host.h |  4 --
 arch/arm64/include/asm/kvm_mmu.h  |  6 +--
 arch/arm64/kvm/nested.c           |  9 ++--
 arch/arm64/kvm/ptdump.c           | 75 ++++++++++++++++++++-----------
 4 files changed, 54 insertions(+), 40 deletions(-)
Signature · 1 line
2.43.0