Skip to contentSkip to conversation

KVM: selftests: Alignment fixes and arm64 MMU cleanup

PATCH Superseded Fuad Tabba 2 participants 8 messages Jan 6

Subsystems (5)
KVM Kselftest RISC-V KVM arm64 +1 more subsystems

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Fuad Tabba

author
#0 lore raw

v3 cover KVM: selftests: Alignment fixes and arm64 MMU cleanup

Changes from v2 [1]:
- Collected Reviewed-by tags from the previous version (thanks!)
- Rebased on 6.19-rc4
- No code changes

This series tidies up a few things in the KVM selftests. It addresses an error in memory alignment, hardens the arm64 MMU configuration for selftests, and fixes minor documentation issues.

First, for arm64, the series explicitly disables translation table walks for the unused upper virtual address range (TTBR1). Since selftests run entirely in the lower range (TTBR0), leaving TTBR1 uninitialized but active could lead to unpredictable behavior if guest code accesses high addresses. We set EPD1 (and TBI1) to ensure such accesses deterministically generate translation faults.

Second, the series fixes the page_align() implementation in both arm64 and riscv. The previous version incorrectly rounded up already-aligned addresses to the *next* page, potentially wasting memory or causing unexpected gaps. After fixing the logic in the arch-specific files, the function is moved to the common kvm_util.h header to eliminate code duplication.

Finally, a few comments and argument descriptions in kvm_util are updated to match the actual code implementation.

Based on Linux 6.19-rc4.

Cheers,
/fuad

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

Fuad Tabba (5):
KVM: arm64: selftests: Disable unused TTBR1_EL1 translations
KVM: arm64: selftests: Fix incorrect rounding in page_align()
KVM: riscv: selftests: Fix incorrect rounding in page_align()
KVM: selftests: Move page_align() to shared header
KVM: selftests: Fix typos and stale comments in kvm_util

 tools/testing/selftests/kvm/include/arm64/processor.h | 4 ++++
 tools/testing/selftests/kvm/include/kvm_util.h        | 9 +++++++--
 tools/testing/selftests/kvm/lib/arm64/processor.c     | 7 ++-----
 tools/testing/selftests/kvm/lib/kvm_util.c            | 2 +-
 tools/testing/selftests/kvm/lib/riscv/processor.c     | 5 -----
 5 files changed, 14 insertions(+), 13 deletions(-)

base-commit: 9ace4753a5202b02191d54e9fdf7f9e3d02b85eb

Signature · 1 line
2.52.0.351.gbe84eed79e-goog
author
#1 lore raw

PATCH v3 1/5 KVM: arm64: selftests: Disable unused TTBR1_EL1 translations

KVM selftests map all guest code and data into the lower virtual address range (0x0000...) managed by TTBR0_EL1. The upper range (0xFFFF...) managed by TTBR1_EL1 is unused and uninitialized.

If a guest accesses the upper range, the MMU attempts a translation table walk using uninitialized registers, leading to unpredictable behavior.

Set TCR_EL1.EPD1 to disable translation table walks for TTBR1_EL1, ensuring that any access to the upper range generates an immediate Translation Fault. Additionally, set TCR_EL1.TBI1 (Top Byte Ignore) to ensure that tagged pointers in the upper range also deterministically trigger a Translation Fault via EPD1.

Define TCR_EPD1_MASK, TCR_EPD1_SHIFT, and TCR_TBI1 in processor.h to support this configuration. These are based on their definitions in arch/arm64/include/asm/pgtable-hwdef.h.

---
 tools/testing/selftests/kvm/include/arm64/processor.h | 4 ++++
 tools/testing/selftests/kvm/lib/arm64/processor.c     | 2 ++
 2 files changed, 6 insertions(+)
tools/testing/selftests/kvm/include/arm64/processor.h +4 -0
index ff928716574d..ac97a1c436fc 100644
@@ -90,6 +90,9 @@
 #define TCR_TG0_64K		(UL(1) << TCR_TG0_SHIFT)
 #define TCR_TG0_16K		(UL(2) << TCR_TG0_SHIFT)
 
+#define TCR_EPD1_SHIFT		23
+#define TCR_EPD1_MASK		(UL(1) << TCR_EPD1_SHIFT)
+
 #define TCR_IPS_SHIFT		32
 #define TCR_IPS_MASK		(UL(7) << TCR_IPS_SHIFT)
 #define TCR_IPS_52_BITS	(UL(6) << TCR_IPS_SHIFT)
@@ -97,6 +100,7 @@
 #define TCR_IPS_40_BITS	(UL(2) << TCR_IPS_SHIFT)
 #define TCR_IPS_36_BITS	(UL(1) << TCR_IPS_SHIFT)
 
+#define TCR_TBI1		(UL(1) << 38)
 #define TCR_HA			(UL(1) << 39)
 #define TCR_DS			(UL(1) << 59)
 
tools/testing/selftests/kvm/lib/arm64/processor.c +2 -0
index d46e4b13b92c..5b379da8cb90 100644
@@ -384,6 +384,8 @@ void aarch64_vcpu_setup(struct kvm_vcpu *vcpu, struct kvm_vcpu_init *init)
 
 	tcr_el1 |= TCR_IRGN0_WBWA | TCR_ORGN0_WBWA | TCR_SH0_INNER;
 	tcr_el1 |= TCR_T0SZ(vm->va_bits);
+	tcr_el1 |= TCR_TBI1;
+	tcr_el1 |= TCR_EPD1_MASK;
 	if (use_lpa2_pte_format(vm))
 		tcr_el1 |= TCR_DS;
 
Signature · 1 line
2.52.0.351.gbe84eed79e-goog
author
#2 lore raw

PATCH v3 2/5 KVM: arm64: selftests: Fix incorrect rounding in page_align()

In reply to Fuad Tabba

The implementation of page_align() in processor.c calculates alignment incorrectly for values that are already aligned. Specifically, (v + vm->page_size) & ~(vm->page_size - 1) aligns to the *next* page boundary even if v is already page-aligned, potentially wasting a page of memory.

Fix the calculation to use standard alignment logic: `(v + vm->page_size
- 1) & ~(vm->page_size - 1)`.

Fixes 7a6629ef746d ("kvm: selftests: add virt mem support for aarch64")
---
 tools/testing/selftests/kvm/lib/arm64/processor.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kvm/lib/arm64/processor.c +1 -1
index 5b379da8cb90..607a4e462984 100644
@@ -23,7 +23,7 @@ static vm_vaddr_t exception_handlers;
 
 static uint64_t page_align(struct kvm_vm *vm, uint64_t v)
 {
-	return (v + vm->page_size) & ~(vm->page_size - 1);
+	return (v + vm->page_size - 1) & ~(vm->page_size - 1);
 }
 
 static uint64_t pgd_index(struct kvm_vm *vm, vm_vaddr_t gva)
Signature · 1 line
2.52.0.351.gbe84eed79e-goog
author
#3 lore raw

PATCH v3 3/5 KVM: riscv: selftests: Fix incorrect rounding in page_align()

In reply to Fuad Tabba

The implementation of page_align() in processor.c calculates alignment incorrectly for values that are already aligned. Specifically, (v + vm->page_size) & ~(vm->page_size - 1) aligns to the *next* page boundary even if v is already page-aligned, potentially wasting a page of memory.

Fix the calculation to use standard alignment logic: `(v + vm->page_size
- 1) & ~(vm->page_size - 1)`.

Fixes 3e06cdf10520 ("KVM: selftests: Add initial support for RISC-V 64-bit")
---
 tools/testing/selftests/kvm/lib/riscv/processor.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kvm/lib/riscv/processor.c +1 -1
index 2eac7d4b59e9..d5e8747b5e69 100644
@@ -28,7 +28,7 @@ bool __vcpu_has_ext(struct kvm_vcpu *vcpu, uint64_t ext)
 
 static uint64_t page_align(struct kvm_vm *vm, uint64_t v)
 {
-	return (v + vm->page_size) & ~(vm->page_size - 1);
+	return (v + vm->page_size - 1) & ~(vm->page_size - 1);
 }
 
 static uint64_t pte_addr(struct kvm_vm *vm, uint64_t entry)
Signature · 1 line
2.52.0.351.gbe84eed79e-goog
author
#4 lore raw

PATCH v3 4/5 KVM: selftests: Move page_align() to shared header

In reply to Fuad Tabba

To avoid code duplication, move page_align() to the shared kvm_util.h header file.

No functional change intended.

---
 tools/testing/selftests/kvm/include/kvm_util.h    | 5 +++++
 tools/testing/selftests/kvm/lib/arm64/processor.c | 5 -----
 tools/testing/selftests/kvm/lib/riscv/processor.c | 5 -----
 3 files changed, 5 insertions(+), 10 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +5 -0
index 81f4355ff28a..dabbe4c3b93f 100644
@@ -1258,6 +1258,11 @@ static inline int __vm_disable_nx_huge_pages(struct kvm_vm *vm)
 	return __vm_enable_cap(vm, KVM_CAP_VM_DISABLE_NX_HUGE_PAGES, 0);
 }
 
+static inline uint64_t page_align(struct kvm_vm *vm, uint64_t v)
+{
+	return (v + vm->page_size - 1) & ~(vm->page_size - 1);
+}
+
 /*
  * Arch hook that is invoked via a constructor, i.e. before exeucting main(),
  * to allow for arch-specific setup that is common to all tests, e.g. computing
tools/testing/selftests/kvm/lib/arm64/processor.c +0 -5
index 607a4e462984..143632917766 100644
@@ -21,11 +21,6 @@
 
 static vm_vaddr_t exception_handlers;
 
-static uint64_t page_align(struct kvm_vm *vm, uint64_t v)
-{
-	return (v + vm->page_size - 1) & ~(vm->page_size - 1);
-}
-
 static uint64_t pgd_index(struct kvm_vm *vm, vm_vaddr_t gva)
 {
 	unsigned int shift = (vm->pgtable_levels - 1) * (vm->page_shift - 3) + vm->page_shift;
tools/testing/selftests/kvm/lib/riscv/processor.c +0 -5
index d5e8747b5e69..f8ff4bf938d9 100644
@@ -26,11 +26,6 @@ bool __vcpu_has_ext(struct kvm_vcpu *vcpu, uint64_t ext)
 	return !ret && !!value;
 }
 
-static uint64_t page_align(struct kvm_vm *vm, uint64_t v)
-{
-	return (v + vm->page_size - 1) & ~(vm->page_size - 1);
-}
-
 static uint64_t pte_addr(struct kvm_vm *vm, uint64_t entry)
 {
 	return ((entry & PGTBL_PTE_ADDR_MASK) >> PGTBL_PTE_ADDR_SHIFT) <<
Signature · 1 line
2.52.0.351.gbe84eed79e-goog
author
#7 lore raw

PATCH v3 5/5 KVM: selftests: Fix typos and stale comments in kvm_util

In reply to Fuad Tabba

Fix minor documentation errors in kvm_util.h and kvm_util.c.

- Correct the argument description for vcpu_args_set in kvm_util.h, which incorrectly listed vm instead of vcpu.
- Fix a typo in the comment for kvm_selftest_arch_init ("exeucting" -> "executing").
- Correct the return value description for vm_vaddr_unused_gap in kvm_util.c to match the implementation, which returns an address "at or above" vaddr_min, not "at or below".

No functional change intended.

---
 tools/testing/selftests/kvm/include/kvm_util.h | 4 ++--
 tools/testing/selftests/kvm/lib/kvm_util.c     | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +2 -2
index dabbe4c3b93f..e29b2cbcb82b 100644
@@ -939,7 +939,7 @@ void *vcpu_map_dirty_ring(struct kvm_vcpu *vcpu);
  * VM VCPU Args Set
  *
  * Input Args:
- *   vm - Virtual Machine
+ *   vcpu - vCPU
  *   num - number of arguments
  *   ... - arguments, each of type uint64_t
  *
@@ -1264,7 +1264,7 @@ static inline uint64_t page_align(struct kvm_vm *vm, uint64_t v)
 }
 
 /*
- * Arch hook that is invoked via a constructor, i.e. before exeucting main(),
+ * Arch hook that is invoked via a constructor, i.e. before executing main(),
  * to allow for arch-specific setup that is common to all tests, e.g. computing
  * the default guest "mode".
  */
tools/testing/selftests/kvm/lib/kvm_util.c +1 -1
index 8279b6ced8d2..fab6b62d7810 100644
@@ -1351,7 +1351,7 @@ struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)
  * Output Args: None
  *
  * Return:
- *   Lowest virtual address at or below vaddr_min, with at least
+ *   Lowest virtual address at or above vaddr_min, with at least
  *   sz unused bytes.  TEST_ASSERT failure if no area of at least
  *   size sz is available.
  *
Signature · 1 line
2.52.0.351.gbe84eed79e-goog
#5 lore raw

Re: [PATCH v3 4/5] KVM: selftests: Move page_align() to shared header

In reply to Fuad Tabba
Quoted 18 lines · Fuad Tabba

To avoid code duplication, move page_align() to the shared kvm_util.h header file.

No functional change intended.

---
 tools/testing/selftests/kvm/include/kvm_util.h    | 5 +++++
 tools/testing/selftests/kvm/lib/arm64/processor.c | 5 -----
 tools/testing/selftests/kvm/lib/riscv/processor.c | 5 -----
 3 files changed, 5 insertions(+), 10 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +0 -0
index 81f4355ff28a..dabbe4c3b93f 100644

tools/testing/selftests/kvm/include/kvm_util.h static inline int __vm_disable_nx_huge_pages(struct kvm_vm *vm) · line 1261

Quoted 4 lines
tools/testing/selftests/kvm/include/kvm_util.h +1 -0
@@ -1258,6 +1258,11 @@ static inline int __vm_disable_nx_huge_pages(struct kvm_vm *vm)
 	return __vm_enable_cap(vm, KVM_CAP_VM_DISABLE_NX_HUGE_PAGES, 0);
 }
 
+static inline uint64_t page_align(struct kvm_vm *vm, uint64_t v)

Maybe vm_page_align()? So that it's a bit more obvious when reading call sites that the alignment is done with respect to the guest's "base" page size, not the host's page size.

Quoted 42 lines
tools/testing/selftests/kvm/include/kvm_util.h +4 -0
+{
+	return (v + vm->page_size - 1) & ~(vm->page_size - 1);
+}
+
 /*
  * Arch hook that is invoked via a constructor, i.e. before exeucting main(),
  * to allow for arch-specific setup that is common to all tests, e.g. computing
tools/testing/selftests/kvm/lib/arm64/processor.c +0 -5
index 607a4e462984..143632917766 100644
@@ -21,11 +21,6 @@
 
 static vm_vaddr_t exception_handlers;
 
-static uint64_t page_align(struct kvm_vm *vm, uint64_t v)
-{
-	return (v + vm->page_size - 1) & ~(vm->page_size - 1);
-}
-
 static uint64_t pgd_index(struct kvm_vm *vm, vm_vaddr_t gva)
 {
 	unsigned int shift = (vm->pgtable_levels - 1) * (vm->page_shift - 3) + vm->page_shift;
tools/testing/selftests/kvm/lib/riscv/processor.c +0 -5
index d5e8747b5e69..f8ff4bf938d9 100644
@@ -26,11 +26,6 @@ bool __vcpu_has_ext(struct kvm_vcpu *vcpu, uint64_t ext)
 	return !ret && !!value;
 }
 
-static uint64_t page_align(struct kvm_vm *vm, uint64_t v)
-{
-	return (v + vm->page_size - 1) & ~(vm->page_size - 1);
-}
-
 static uint64_t pte_addr(struct kvm_vm *vm, uint64_t entry)
 {
 	return ((entry & PGTBL_PTE_ADDR_MASK) >> PGTBL_PTE_ADDR_SHIFT) <<
Signature · 1 line
2.52.0.351.gbe84eed79e-goog
author
#6 lore raw

Re: [PATCH v3 4/5] KVM: selftests: Move page_align() to shared header

Quoted 23 lines · Sean Christopherson
Quoted 21 lines · Fuad Tabba

To avoid code duplication, move page_align() to the shared kvm_util.h header file.

No functional change intended.

---
 tools/testing/selftests/kvm/include/kvm_util.h    | 5 +++++
 tools/testing/selftests/kvm/lib/arm64/processor.c | 5 -----
 tools/testing/selftests/kvm/lib/riscv/processor.c | 5 -----
 3 files changed, 5 insertions(+), 10 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +0 -0
index 81f4355ff28a..dabbe4c3b93f 100644
@@ -1258,6 +1258,11 @@ static inline int __vm_disable_nx_huge_pages(struct kvm_vm *vm)
      return __vm_enable_cap(vm, KVM_CAP_VM_DISABLE_NX_HUGE_PAGES, 0);
 }
 
Quoted 5 lines

tools/testing/selftests/kvm/include/kvm_util.h static inline int __vm_disable_nx_huge_pages(struct kvm_vm *vm) · line 1261

Quoted 1 line
tools/testing/selftests/kvm/include/kvm_util.h +1 -0
+static inline uint64_t page_align(struct kvm_vm *vm, uint64_t v)

Maybe vm_page_align()? So that it's a bit more obvious when reading call sites that the alignment is done with respect to the guest's "base" page size, not the host's page size.

Yes, that's clearer. I'll fix this.

Thanks,
/fuad

Quoted 42 lines
Quoted 42 lines
tools/testing/selftests/kvm/include/kvm_util.h +4 -0
+{
+     return (v + vm->page_size - 1) & ~(vm->page_size - 1);
+}
+
 /*
  * Arch hook that is invoked via a constructor, i.e. before exeucting main(),
  * to allow for arch-specific setup that is common to all tests, e.g. computing
tools/testing/selftests/kvm/lib/arm64/processor.c +0 -5
index 607a4e462984..143632917766 100644
@@ -21,11 +21,6 @@
 
 static vm_vaddr_t exception_handlers;
 
-static uint64_t page_align(struct kvm_vm *vm, uint64_t v)
-{
-     return (v + vm->page_size - 1) & ~(vm->page_size - 1);
-}
-
 static uint64_t pgd_index(struct kvm_vm *vm, vm_vaddr_t gva)
 {
      unsigned int shift = (vm->pgtable_levels - 1) * (vm->page_shift - 3) + vm->page_shift;
tools/testing/selftests/kvm/lib/riscv/processor.c +0 -6
index d5e8747b5e69..f8ff4bf938d9 100644
@@ -26,11 +26,6 @@ bool __vcpu_has_ext(struct kvm_vcpu *vcpu, uint64_t ext)
      return !ret && !!value;
 }
 
-static uint64_t page_align(struct kvm_vm *vm, uint64_t v)
-{
-     return (v + vm->page_size - 1) & ~(vm->page_size - 1);
-}
-
 static uint64_t pte_addr(struct kvm_vm *vm, uint64_t entry)
 {
      return ((entry & PGTBL_PTE_ADDR_MASK) >> PGTBL_PTE_ADDR_SHIFT) <<
--

2.52.0.351.gbe84eed79e-goog