[PATCH 0/5] KVM: arm64: Handle unaligned memslots in kvm_(test_)_age_gfn()

Oliver Upton oliver.upton at linux.dev
Tue Jan 10 16:02:55 PST 2023


There is no requirement for userspace to align KVM memslots to an
appropriate boundary for the backing memory source. For example,
userspace could choose to use 2M THP and place the memslot at a 4K
offset within the hugepage.

Under these conditions, KVM can only map at the 4K level, effectively
shattering the hugepage into a table of PTEs.

kvm_(test_)_age_gfn() operate on the assumption that the GFN range is of
a single page or hugepage size. Furthermore, this assumption bleeds into
the table walkers, which walk until reaching the first leaf PTE.

This completely falls apart in the aforementioned example where the
memslot is unalgined, as there could be many leaf PTEs that need to be
visited for a single hugepage. Furthermore, this configuration can lead
to the WARN in kvm_age_gfn() firing as well.

This series addresses the issue by rejigging the related page table
walkers to traverse a specified range of memory. I also roped in some
cleanups that I did along the way (i.e. patches 1 and 5), but can easily
respin w/o them if desired.

Tested on Ampere Altra w/ kvmtool and KVM selftests. I was going to
implement a reproducer for the issue in KVM selftests, but there is a
significant amount of idiot-proofing around the memslots helpers that
prevent an unaligned memslot. Might get back to that when I have more
patience for it.

Applies to 6.2-rc3.

Oliver Upton (5):
  KVM: arm64: Hoist S2 PTE definitions into kvm_pgtable.h
  KVM: arm64: Add a mask for all leaf PTE attributes
  KVM: arm64: Only return attributes from stage2_update_leaf_attrs()
  KVM: arm64: Correctly handle page aging notifiers for unaligned
    memlsot
  KVM: arm64: Consistently use KVM's types/helpers in kvm_age_gfn()

 arch/arm64/include/asm/kvm_pgtable.h | 66 +++++++++++++++++++----
 arch/arm64/kvm/hyp/pgtable.c         | 79 ++++++++--------------------
 arch/arm64/kvm/mmu.c                 | 18 +++----
 3 files changed, 88 insertions(+), 75 deletions(-)


base-commit: b7bfaa761d760e72a969d116517eaa12e404c262
-- 
2.39.0.314.g84b9a713c41-goog




More information about the linux-arm-kernel mailing list