* [PATCH v4 1/5] KVM: arm64: selftests: Disable unused TTBR1_EL1 translations
2026-01-09 8:22 [PATCH v4 0/5] KVM: selftests: Alignment fixes and arm64 MMU cleanup Fuad Tabba
@ 2026-01-09 8:22 ` Fuad Tabba
2026-01-09 8:22 ` [PATCH v4 2/5] KVM: arm64: selftests: Fix incorrect rounding in page_align() Fuad Tabba
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Fuad Tabba @ 2026-01-09 8:22 UTC (permalink / raw)
To: kvm, kvm-riscv, kvmarm, linux-arm-kernel
Cc: maz, oliver.upton, joey.gouly, suzuki.poulose, yuzenghui, will,
pbonzini, shuah, anup, atish.patra, itaru.kitayama, andrew.jones,
seanjc, tabba
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`.
Suggested-by: Will Deacon <will@kernel.org>
Reviewed-by: Itaru Kitayama <itaru.kitayama@fujitsu.com>
Signed-off-by: Fuad Tabba <tabba@google.com>
---
tools/testing/selftests/kvm/include/arm64/processor.h | 4 ++++
tools/testing/selftests/kvm/lib/arm64/processor.c | 2 ++
2 files changed, 6 insertions(+)
diff --git a/tools/testing/selftests/kvm/include/arm64/processor.h b/tools/testing/selftests/kvm/include/arm64/processor.h
index ff928716574d..ac97a1c436fc 100644
--- a/tools/testing/selftests/kvm/include/arm64/processor.h
+++ b/tools/testing/selftests/kvm/include/arm64/processor.h
@@ -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)
diff --git a/tools/testing/selftests/kvm/lib/arm64/processor.c b/tools/testing/selftests/kvm/lib/arm64/processor.c
index d46e4b13b92c..5b379da8cb90 100644
--- a/tools/testing/selftests/kvm/lib/arm64/processor.c
+++ b/tools/testing/selftests/kvm/lib/arm64/processor.c
@@ -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;
--
2.52.0.457.g6b5491de43-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH v4 2/5] KVM: arm64: selftests: Fix incorrect rounding in page_align()
2026-01-09 8:22 [PATCH v4 0/5] KVM: selftests: Alignment fixes and arm64 MMU cleanup Fuad Tabba
2026-01-09 8:22 ` [PATCH v4 1/5] KVM: arm64: selftests: Disable unused TTBR1_EL1 translations Fuad Tabba
@ 2026-01-09 8:22 ` Fuad Tabba
2026-01-09 8:22 ` [PATCH v4 3/5] KVM: riscv: " Fuad Tabba
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Fuad Tabba @ 2026-01-09 8:22 UTC (permalink / raw)
To: kvm, kvm-riscv, kvmarm, linux-arm-kernel
Cc: maz, oliver.upton, joey.gouly, suzuki.poulose, yuzenghui, will,
pbonzini, shuah, anup, atish.patra, itaru.kitayama, andrew.jones,
seanjc, 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")
Reviewed-by: Andrew Jones <andrew.jones@linux.dev>
Signed-off-by: Fuad Tabba <tabba@google.com>
---
tools/testing/selftests/kvm/lib/arm64/processor.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/testing/selftests/kvm/lib/arm64/processor.c b/tools/testing/selftests/kvm/lib/arm64/processor.c
index 5b379da8cb90..607a4e462984 100644
--- a/tools/testing/selftests/kvm/lib/arm64/processor.c
+++ b/tools/testing/selftests/kvm/lib/arm64/processor.c
@@ -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)
--
2.52.0.457.g6b5491de43-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH v4 3/5] KVM: riscv: selftests: Fix incorrect rounding in page_align()
2026-01-09 8:22 [PATCH v4 0/5] KVM: selftests: Alignment fixes and arm64 MMU cleanup Fuad Tabba
2026-01-09 8:22 ` [PATCH v4 1/5] KVM: arm64: selftests: Disable unused TTBR1_EL1 translations Fuad Tabba
2026-01-09 8:22 ` [PATCH v4 2/5] KVM: arm64: selftests: Fix incorrect rounding in page_align() Fuad Tabba
@ 2026-01-09 8:22 ` Fuad Tabba
2026-01-09 8:22 ` [PATCH v4 4/5] KVM: selftests: Move page_align() to shared header Fuad Tabba
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Fuad Tabba @ 2026-01-09 8:22 UTC (permalink / raw)
To: kvm, kvm-riscv, kvmarm, linux-arm-kernel
Cc: maz, oliver.upton, joey.gouly, suzuki.poulose, yuzenghui, will,
pbonzini, shuah, anup, atish.patra, itaru.kitayama, andrew.jones,
seanjc, 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")
Reviewed-by: Andrew Jones <andrew.jones@linux.dev>
Signed-off-by: Fuad Tabba <tabba@google.com>
---
tools/testing/selftests/kvm/lib/riscv/processor.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/testing/selftests/kvm/lib/riscv/processor.c b/tools/testing/selftests/kvm/lib/riscv/processor.c
index 2eac7d4b59e9..d5e8747b5e69 100644
--- a/tools/testing/selftests/kvm/lib/riscv/processor.c
+++ b/tools/testing/selftests/kvm/lib/riscv/processor.c
@@ -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)
--
2.52.0.457.g6b5491de43-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH v4 4/5] KVM: selftests: Move page_align() to shared header
2026-01-09 8:22 [PATCH v4 0/5] KVM: selftests: Alignment fixes and arm64 MMU cleanup Fuad Tabba
` (2 preceding siblings ...)
2026-01-09 8:22 ` [PATCH v4 3/5] KVM: riscv: " Fuad Tabba
@ 2026-01-09 8:22 ` Fuad Tabba
2026-01-09 8:22 ` [PATCH v4 5/5] KVM: selftests: Fix typos and stale comments in kvm_util Fuad Tabba
2026-01-15 13:44 ` [PATCH v4 0/5] KVM: selftests: Alignment fixes and arm64 MMU cleanup Marc Zyngier
5 siblings, 0 replies; 7+ messages in thread
From: Fuad Tabba @ 2026-01-09 8:22 UTC (permalink / raw)
To: kvm, kvm-riscv, kvmarm, linux-arm-kernel
Cc: maz, oliver.upton, joey.gouly, suzuki.poulose, yuzenghui, will,
pbonzini, shuah, anup, atish.patra, itaru.kitayama, andrew.jones,
seanjc, tabba
To avoid code duplication, move page_align() to the shared `kvm_util.h`
header file. Rename it to vm_page_align(), to make it clear that the
alignment is done with respect to the guest's base page size.
No functional change intended.
Reviewed-by: Andrew Jones <andrew.jones@linux.dev>
Signed-off-by: Fuad Tabba <tabba@google.com>
---
tools/testing/selftests/kvm/include/kvm_util.h | 5 +++++
tools/testing/selftests/kvm/lib/arm64/processor.c | 7 +------
tools/testing/selftests/kvm/lib/riscv/processor.c | 7 +------
3 files changed, 7 insertions(+), 12 deletions(-)
diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
index 81f4355ff28a..747effa614f1 100644
--- a/tools/testing/selftests/kvm/include/kvm_util.h
+++ b/tools/testing/selftests/kvm/include/kvm_util.h
@@ -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 vm_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
diff --git a/tools/testing/selftests/kvm/lib/arm64/processor.c b/tools/testing/selftests/kvm/lib/arm64/processor.c
index 607a4e462984..1605dc740d1e 100644
--- a/tools/testing/selftests/kvm/lib/arm64/processor.c
+++ b/tools/testing/selftests/kvm/lib/arm64/processor.c
@@ -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;
@@ -115,7 +110,7 @@ static uint64_t __maybe_unused ptrs_per_pte(struct kvm_vm *vm)
void virt_arch_pgd_alloc(struct kvm_vm *vm)
{
- size_t nr_pages = page_align(vm, ptrs_per_pgd(vm) * 8) / vm->page_size;
+ size_t nr_pages = vm_page_align(vm, ptrs_per_pgd(vm) * 8) / vm->page_size;
if (vm->pgd_created)
return;
diff --git a/tools/testing/selftests/kvm/lib/riscv/processor.c b/tools/testing/selftests/kvm/lib/riscv/processor.c
index d5e8747b5e69..401245fe31db 100644
--- a/tools/testing/selftests/kvm/lib/riscv/processor.c
+++ b/tools/testing/selftests/kvm/lib/riscv/processor.c
@@ -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) <<
@@ -68,7 +63,7 @@ static uint64_t pte_index(struct kvm_vm *vm, vm_vaddr_t gva, int level)
void virt_arch_pgd_alloc(struct kvm_vm *vm)
{
- size_t nr_pages = page_align(vm, ptrs_per_pte(vm) * 8) / vm->page_size;
+ size_t nr_pages = vm_page_align(vm, ptrs_per_pte(vm) * 8) / vm->page_size;
if (vm->pgd_created)
return;
--
2.52.0.457.g6b5491de43-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH v4 5/5] KVM: selftests: Fix typos and stale comments in kvm_util
2026-01-09 8:22 [PATCH v4 0/5] KVM: selftests: Alignment fixes and arm64 MMU cleanup Fuad Tabba
` (3 preceding siblings ...)
2026-01-09 8:22 ` [PATCH v4 4/5] KVM: selftests: Move page_align() to shared header Fuad Tabba
@ 2026-01-09 8:22 ` Fuad Tabba
2026-01-15 13:44 ` [PATCH v4 0/5] KVM: selftests: Alignment fixes and arm64 MMU cleanup Marc Zyngier
5 siblings, 0 replies; 7+ messages in thread
From: Fuad Tabba @ 2026-01-09 8:22 UTC (permalink / raw)
To: kvm, kvm-riscv, kvmarm, linux-arm-kernel
Cc: maz, oliver.upton, joey.gouly, suzuki.poulose, yuzenghui, will,
pbonzini, shuah, anup, atish.patra, itaru.kitayama, andrew.jones,
seanjc, 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.
Reviewed-by: Andrew Jones <andrew.jones@linux.dev>
Signed-off-by: Fuad Tabba <tabba@google.com>
---
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(-)
diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
index 747effa614f1..97f9251eb073 100644
--- a/tools/testing/selftests/kvm/include/kvm_util.h
+++ b/tools/testing/selftests/kvm/include/kvm_util.h
@@ -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 vm_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".
*/
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index 8279b6ced8d2..fab6b62d7810 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -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.
*
--
2.52.0.457.g6b5491de43-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH v4 0/5] KVM: selftests: Alignment fixes and arm64 MMU cleanup
2026-01-09 8:22 [PATCH v4 0/5] KVM: selftests: Alignment fixes and arm64 MMU cleanup Fuad Tabba
` (4 preceding siblings ...)
2026-01-09 8:22 ` [PATCH v4 5/5] KVM: selftests: Fix typos and stale comments in kvm_util Fuad Tabba
@ 2026-01-15 13:44 ` Marc Zyngier
5 siblings, 0 replies; 7+ messages in thread
From: Marc Zyngier @ 2026-01-15 13:44 UTC (permalink / raw)
To: kvm, kvm-riscv, kvmarm, linux-arm-kernel, Fuad Tabba
Cc: joey.gouly, suzuki.poulose, yuzenghui, will, pbonzini, shuah,
anup, atish.patra, itaru.kitayama, andrew.jones, seanjc,
Oliver Upton
On Fri, 09 Jan 2026 08:22:13 +0000, Fuad Tabba wrote:
> Changes from v3 [1]:
> - Renamed page_align() to vm_page_align() (Sean)
>
> 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.
>
> [...]
Applied to next, thanks!
[1/5] KVM: arm64: selftests: Disable unused TTBR1_EL1 translations
commit: 7e03d07d03a486c66d5c084c7185b1bef29049e9
[2/5] KVM: arm64: selftests: Fix incorrect rounding in page_align()
commit: dd0c5d04d13cae8ff2694ef83d1ae5804d6d9798
[3/5] KVM: riscv: selftests: Fix incorrect rounding in page_align()
commit: 582b39463f1c0774e0b3cb5be2118e8564b7941e
[4/5] KVM: selftests: Move page_align() to shared header
commit: de00d07321cf3f182762de2308c08062d5b824c0
[5/5] KVM: selftests: Fix typos and stale comments in kvm_util
commit: e0a99a2b72f3c6365d9f4d6943ed45f7fc286b70
Cheers,
M.
--
Without deviation from the norm, progress is not possible.
^ permalink raw reply [flat|nested] 7+ messages in thread