Skip to contentSkip to conversation

KVM: arm64: account pKVM reclaim against the VM mm

PATCH Applied Bradley Morgan 4 participants 13 messages Jun 21 – Jun 23

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: arm64: account pKVM reclaim against the VM mm

Protected guest faults charge long term pins to the VM's mm. Teardown can run later from file release, where current->mm may be unrelated.

Drop the charge from kvm->mm instead.

Fixes 4e6e03f9eadd ("KVM: arm64: Hook up reclaim hypercall to pkvm_pgtable_stage2_destroy()")
---
 arch/arm64/kvm/pkvm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/pkvm.c +1 -1
index 053e4f733e4b..428723b1b0f5 100644
@@ -352,7 +352,7 @@ static int __pkvm_pgtable_stage2_reclaim(struct kvm_pgtable *pgt, u64 start, u64
 		page = pfn_to_page(mapping->pfn);
 		WARN_ON_ONCE(mapping->nr_pages != 1);
 		unpin_user_pages_dirty_lock(&page, 1, true);
-		account_locked_vm(current->mm, 1, false);
+		account_locked_vm(kvm->mm, 1, false);
 		pkvm_mapping_remove(mapping, &pgt->pkvm_mappings);
 		kfree(mapping);
 	}
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 17 lines · Bradley Morgan

Protected guest faults charge long term pins to the VM's mm. Teardown can run later from file release, where current->mm may be unrelated.

Drop the charge from kvm->mm instead.

Fixes 4e6e03f9eadd ("KVM: arm64: Hook up reclaim hypercall to pkvm_pgtable_stage2_destroy()")
---
 arch/arm64/kvm/pkvm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/pkvm.c +0 -0
index 053e4f733e4b..428723b1b0f5 100644

arch/arm64/kvm/pkvm.c static int __pkvm_pgtable_stage2_reclaim(struct kvm_pgtable *pgt, u64 start, u64 · line 358

Quoted 8 lines
arch/arm64/kvm/pkvm.c +1 -1
@@ -352,7 +352,7 @@ static int __pkvm_pgtable_stage2_reclaim(struct kvm_pgtable *pgt, u64 start, u64
 		page = pfn_to_page(mapping->pfn);
 		WARN_ON_ONCE(mapping->nr_pages != 1);
 		unpin_user_pages_dirty_lock(&page, 1, true);
-		account_locked_vm(current->mm, 1, false);
+		account_locked_vm(kvm->mm, 1, false);
 		pkvm_mapping_remove(mapping, &pgt->pkvm_mappings);
 		kfree(mapping);
 	}

Seems correct to me, as the final mmdrop(kvm->mm) occurs after S2 teardown.

Will, what do you think?

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
reviewer Reviewed-by Tested-by on v1 1/1
#5 lore raw
In reply to Bradley Morgan
Quoted 8 lines · Bradley Morgan

Protected guest faults charge long term pins to the VM's mm. Teardown can run later from file release, where current->mm may be unrelated.

Drop the charge from kvm->mm instead.

Fixes 4e6e03f9eadd ("KVM: arm64: Hook up reclaim hypercall to pkvm_pgtable_stage2_destroy()")

Reproduced by creating a protected VM, running the vCPU to fault in a page, then forking and having the child close the last fd reference. Without the fix, the parent's VmLck leaks (the reclaim decrements the child's mm, which is freed on exit). With the fix the parent's VmLck returns to zero.

One minor observation: account_locked_vm() also passes current as the task pointer to __account_locked_vm(), but on the decrement path that is only used in the pr_debug log line, so it is technically wrong but functionally harmless.

Cheers,
/fuad

Quoted 20 lines
---
 arch/arm64/kvm/pkvm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/pkvm.c +1 -2
index 053e4f733e4b..428723b1b0f5 100644
@@ -352,7 +352,7 @@ static int __pkvm_pgtable_stage2_reclaim(struct kvm_pgtable *pgt, u64 start, u64
                page = pfn_to_page(mapping->pfn);
                WARN_ON_ONCE(mapping->nr_pages != 1);
                unpin_user_pages_dirty_lock(&page, 1, true);
-               account_locked_vm(current->mm, 1, false);
+               account_locked_vm(kvm->mm, 1, false);
                pkvm_mapping_remove(mapping, &pgt->pkvm_mappings);
                kfree(mapping);
        }
--

2.53.0

maintainer
#6 lore raw
Quoted 17 lines · Fuad Tabba
Quoted 8 lines · Bradley Morgan

Protected guest faults charge long term pins to the VM's mm. Teardown can run later from file release, where current->mm may be unrelated.

Drop the charge from kvm->mm instead.

Fixes 4e6e03f9eadd ("KVM: arm64: Hook up reclaim hypercall to pkvm_pgtable_stage2_destroy()")

Reproduced by creating a protected VM, running the vCPU to fault in a page, then forking and having the child close the last fd reference. Without the fix, the parent's VmLck leaks (the reclaim decrements the child's mm, which is freed on exit). With the fix the parent's VmLck returns to zero.

Quoted 4 lines

One minor observation: account_locked_vm() also passes current as the task pointer to __account_locked_vm(), but on the decrement path that is only used in the pr_debug log line, so it is technically wrong but functionally harmless.

I don't think this is wrong. Awkward, maybe. It is just that the rlimit check and the accounting may be different contexts, and the pr_debug() call covers both inc and dec.

Quoted 3 lines

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#7 lore raw
Quoted 5 lines · Marc Zyngier
Quoted 3 lines · Fuad Tabba
Quoted 1 line · Bradley Morgan
Quoted 19 lines
Quoted 6 lines
Quoted 6 lines

Protected guest faults charge long term pins to the VM's mm. Teardown can run later from file release, where current->mm may be unrelated.

Drop the charge from kvm->mm instead.

Fixes 4e6e03f9eadd ("KVM: arm64: Hook up reclaim hypercall to

pkvm_pgtable_stage2_destroy()")

Quoted 12 lines
Quoted 1 line

Reproduced by creating a protected VM, running the vCPU to fault in a page, then forking and having the child close the last fd reference. Without the fix, the parent's VmLck leaks (the reclaim decrements the child's mm, which is freed on exit). With the fix the parent's VmLck returns to zero.

One minor observation: account_locked_vm() also passes current as the task pointer to __account_locked_vm(), but on the decrement path that is only used in the pr_debug log line, so it is technically wrong but functionally harmless.

I agree with marc here. Maybe awkward.

I tested it on my pixel 7! :)

Quoted 7 lines

I don't think this is wrong. Awkward, maybe. It is just that the rlimit check and the accounting may be different contexts, and the pr_debug() call covers both inc and dec.

Quoted 3 lines

Thanks for the review! :)

Cheers!

Quoted 5 lines

Thanks,

	M.
maintainer
#2 lore raw
In reply to Marc Zyngier
Quoted 31 lines · Marc Zyngier
Quoted 17 lines · Bradley Morgan

Protected guest faults charge long term pins to the VM's mm. Teardown can run later from file release, where current->mm may be unrelated.

Drop the charge from kvm->mm instead.

Fixes 4e6e03f9eadd ("KVM: arm64: Hook up reclaim hypercall to pkvm_pgtable_stage2_destroy()")
---
 arch/arm64/kvm/pkvm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/pkvm.c +0 -0
index 053e4f733e4b..428723b1b0f5 100644

arch/arm64/kvm/pkvm.c static int __pkvm_pgtable_stage2_reclaim(struct kvm_pgtable *pgt, u64 start, u64 · line 358

Quoted 8 lines
arch/arm64/kvm/pkvm.c +1 -1
@@ -352,7 +352,7 @@ static int __pkvm_pgtable_stage2_reclaim(struct kvm_pgtable *pgt, u64 start, u64
 		page = pfn_to_page(mapping->pfn);
 		WARN_ON_ONCE(mapping->nr_pages != 1);
 		unpin_user_pages_dirty_lock(&page, 1, true);
-		account_locked_vm(current->mm, 1, false);
+		account_locked_vm(kvm->mm, 1, false);
 		pkvm_mapping_remove(mapping, &pgt->pkvm_mappings);
 		kfree(mapping);
 	}

Seems correct to me, as the final mmdrop(kvm->mm) occurs after S2 teardown.

Quoted 1 line

Will, what do you think?

Thanks, this looks correct to me.

While I was thinking about it, I also started looking at the use of 'current->mm' in kvm_arch_prepare_memory_region() in case that should also be 'kvm->mm'. However, I then realised that I don't really grok that code at all because it does a bunch of checking on the VMAs with mmap_read_lock(current->mm) held, but then that lock is dropped immediately after doing the checks so I'm not really sure what they are protected against. Presumably, the address space could be modified as soon as the lock is dropped?

But it's hot, so I'm probably missing something here.

Will

maintainer
#3 lore raw
Quoted 46 lines · Will Deacon
Quoted 31 lines · Marc Zyngier
Quoted 17 lines · Bradley Morgan

Protected guest faults charge long term pins to the VM's mm. Teardown can run later from file release, where current->mm may be unrelated.

Drop the charge from kvm->mm instead.

Fixes 4e6e03f9eadd ("KVM: arm64: Hook up reclaim hypercall to pkvm_pgtable_stage2_destroy()")
---
 arch/arm64/kvm/pkvm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/pkvm.c +0 -0
index 053e4f733e4b..428723b1b0f5 100644

arch/arm64/kvm/pkvm.c static int __pkvm_pgtable_stage2_reclaim(struct kvm_pgtable *pgt, u64 start, u64 · line 358

Quoted 8 lines
arch/arm64/kvm/pkvm.c +1 -1
@@ -352,7 +352,7 @@ static int __pkvm_pgtable_stage2_reclaim(struct kvm_pgtable *pgt, u64 start, u64
 		page = pfn_to_page(mapping->pfn);
 		WARN_ON_ONCE(mapping->nr_pages != 1);
 		unpin_user_pages_dirty_lock(&page, 1, true);
-		account_locked_vm(current->mm, 1, false);
+		account_locked_vm(kvm->mm, 1, false);
 		pkvm_mapping_remove(mapping, &pgt->pkvm_mappings);
 		kfree(mapping);
 	}

Seems correct to me, as the final mmdrop(kvm->mm) occurs after S2 teardown.

Quoted 1 line

Will, what do you think?

Thanks, this looks correct to me.

While I was thinking about it, I also started looking at the use of 'current->mm' in kvm_arch_prepare_memory_region() in case that should also be 'kvm->mm'. However, I then realised that I don't really grok that code at all because it does a bunch of checking on the VMAs with mmap_read_lock(current->mm) held, but then that lock is dropped immediately after doing the checks so I'm not really sure what they are protected against. Presumably, the address space could be modified as soon as the lock is dropped?

Quoted 1 line

But it's hot, so I'm probably missing something here.

I think this is just trying to catch a few obvious issues, such as dirty logging on device memory, but that only works for well behaved userspace that is making "a honest mistake".

For the more trying ones, we end-up doing the same checks again at fault time anyway.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
maintainer Applied
#8 lore raw
In reply to Bradley Morgan
Quoted 6 lines · Bradley Morgan

Protected guest faults charge long term pins to the VM's mm. Teardown can run later from file release, where current->mm may be unrelated.

Drop the charge from kvm->mm instead.

Applied to fixes, thanks!

[1/1] KVM: arm64: account pKVM reclaim against the VM mm

      commit: d098bb75d14fde2f12155f1a95ec0168160867ce

Cheers,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
maintainer
#4 lore raw
In reply to Marc Zyngier
Quoted 48 lines · Marc Zyngier
Quoted 46 lines · Will Deacon
Quoted 31 lines · Marc Zyngier
Quoted 17 lines · Bradley Morgan

Protected guest faults charge long term pins to the VM's mm. Teardown can run later from file release, where current->mm may be unrelated.

Drop the charge from kvm->mm instead.

Fixes 4e6e03f9eadd ("KVM: arm64: Hook up reclaim hypercall to pkvm_pgtable_stage2_destroy()")
---
 arch/arm64/kvm/pkvm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/pkvm.c +0 -0
index 053e4f733e4b..428723b1b0f5 100644

arch/arm64/kvm/pkvm.c static int __pkvm_pgtable_stage2_reclaim(struct kvm_pgtable *pgt, u64 start, u64 · line 358

Quoted 8 lines
arch/arm64/kvm/pkvm.c +1 -1
@@ -352,7 +352,7 @@ static int __pkvm_pgtable_stage2_reclaim(struct kvm_pgtable *pgt, u64 start, u64
 		page = pfn_to_page(mapping->pfn);
 		WARN_ON_ONCE(mapping->nr_pages != 1);
 		unpin_user_pages_dirty_lock(&page, 1, true);
-		account_locked_vm(current->mm, 1, false);
+		account_locked_vm(kvm->mm, 1, false);
 		pkvm_mapping_remove(mapping, &pgt->pkvm_mappings);
 		kfree(mapping);
 	}

Seems correct to me, as the final mmdrop(kvm->mm) occurs after S2 teardown.

Quoted 1 line

Will, what do you think?

Thanks, this looks correct to me.

While I was thinking about it, I also started looking at the use of 'current->mm' in kvm_arch_prepare_memory_region() in case that should also be 'kvm->mm'. However, I then realised that I don't really grok that code at all because it does a bunch of checking on the VMAs with mmap_read_lock(current->mm) held, but then that lock is dropped immediately after doing the checks so I'm not really sure what they are protected against. Presumably, the address space could be modified as soon as the lock is dropped?

Quoted 8 lines
Quoted 1 line

But it's hot, so I'm probably missing something here.

I think this is just trying to catch a few obvious issues, such as dirty logging on device memory, but that only works for well behaved userspace that is making "a honest mistake".

For the more trying ones, we end-up doing the same checks again at fault time anyway.

Got it, so it's a best-effort check. Although it does mean that memslot changes have to be done by the same mm as kvm->mm, otherwise you could get a spurious error back from the kernel.

Will

#9 lore raw
In reply to Marc Zyngier
Quoted 8 lines · Marc Zyngier
Quoted 6 lines · Bradley Morgan

Protected guest faults charge long term pins to the VM's mm. Teardown can run later from file release, where current->mm may be unrelated.

Drop the charge from kvm->mm instead.

Quoted 9 lines

Applied to fixes, thanks!

[1/1] KVM: arm64: account pKVM reclaim against the VM mm

     commit: d098bb75d14fde2f12155f1a95ec0168160867ce

Cheers,

	M.

Hey, which tree did you apply it to?

Thanks!

reviewer
#10 lore raw
Quoted 15 lines · Bradley Morgan
Quoted 13 lines · Marc Zyngier
Quoted 6 lines · Bradley Morgan

Protected guest faults charge long term pins to the VM's mm. Teardown can run later from file release, where current->mm may be unrelated.

Drop the charge from kvm->mm instead.

Applied to fixes, thanks!

[1/1] KVM: arm64: account pKVM reclaim against the VM mm

     commit: d098bb75d14fde2f12155f1a95ec0168160867ce
Quoted 7 lines
Quoted 4 lines

Cheers,

      M.

Hey, which tree did you apply it to?

https://git.kernel.org/pub/scm/linux/kernel/git/kvmarm/kvmarm.git/log/?h=fixes

/fuad

Quoted 3 lines

Thanks!

#11 lore raw
Quoted 23 lines · Fuad Tabba
Quoted 2 lines · Bradley Morgan

On June 23, 2026 2:53:11 PM GMT+01:00, Marc Zyngier <[email protected]>

Quoted 19 lines · wrote:
Quoted 17 lines · Bradley Morgan
Quoted 6 lines

Protected guest faults charge long term pins to the VM's mm. Teardown can run later from file release, where current->mm may be unrelated.

Drop the charge from kvm->mm instead.

Applied to fixes, thanks!

[1/1] KVM: arm64: account pKVM reclaim against the VM mm

     commit: d098bb75d14fde2f12155f1a95ec0168160867ce

Cheers,

      M.
Quoted 10 lines
Quoted 1 line

Hey, which tree did you apply it to?

https://git.kernel.org/pub/scm/linux/kernel/git/kvmarm/kvmarm.git/log/?h=fixes

/fuad

Quoted 3 lines

Thanks!

Thanks fuad.

May I ask if this would be put onto ACK?

Since this affects pixel devices.

(E.g, my pixel 7....)

Thanks!

reviewer
#12 lore raw

Hi Bradley,

Quoted 34 lines · Bradley Morgan
Quoted 31 lines · Fuad Tabba
Quoted 2 lines · Bradley Morgan

On June 23, 2026 2:53:11 PM GMT+01:00, Marc Zyngier <[email protected]>

Quoted 13 lines · wrote:
Quoted 13 lines · Bradley Morgan
Quoted 6 lines

Protected guest faults charge long term pins to the VM's mm. Teardown can run later from file release, where current->mm may be unrelated.

Drop the charge from kvm->mm instead.

Applied to fixes, thanks!

[1/1] KVM: arm64: account pKVM reclaim against the VM mm

     commit: d098bb75d14fde2f12155f1a95ec0168160867ce
Quoted 7 lines
Quoted 4 lines

Cheers,

      M.

Hey, which tree did you apply it to?

https://git.kernel.org/pub/scm/linux/kernel/git/kvmarm/kvmarm.git/log/?h=fixes

/fuad

Quoted 2 lines
Quoted 6 lines
Quoted 2 lines
Quoted 1 line

Thanks!

Thanks fuad.

May I ask if this would be put onto ACK?

Yes, I'll pick it into ACK.

Cheers,
/fuad

Quoted 6 lines

Since this affects pixel devices.

(E.g, my pixel 7....)

Thanks!