From: Sean Christopherson <seanjc@google.com>
To: Marc Zyngier <maz@kernel.org>, Oliver Upton <oupton@kernel.org>,
Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Tianrui Zhao <zhaotianrui@loongson.cn>,
Bibo Mao <maobibo@loongson.cn>,
Huacai Chen <chenhuacai@kernel.org>,
Anup Patel <anup@brainfault.org>, Paul Walmsley <pjw@kernel.org>,
Palmer Dabbelt <palmer@dabbelt.com>,
Albert Ou <aou@eecs.berkeley.edu>,
Christian Borntraeger <borntraeger@linux.ibm.com>,
Janosch Frank <frankja@linux.ibm.com>,
Claudio Imbrenda <imbrenda@linux.ibm.com>
Cc: Fuad Tabba <fuad.tabba@linux.dev>,
Joey Gouly <joey.gouly@arm.com>,
Steffen Eiden <seiden@linux.ibm.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
Zenghui Yu <yuzenghui@huawei.com>,
Atish Patra <atish.patra@linux.dev>,
Alexandre Ghiti <alex@ghiti.fr>,
David Hildenbrand <david@kernel.org>,
linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev,
kvm@vger.kernel.org, loongarch@lists.linux.dev,
kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org,
linux-kernel@vger.kernel.org,
Nicholas Piggin <npiggin@gmail.com>,
Ritesh Harjani <ritesh.list@gmail.com>
Subject: [PATCH 12/20] KVM: selftests: Add APIs to override memory region types with custom memslots
Date: Wed, 26 Aug 2026 16:05:03 -0700 [thread overview]
Message-ID: <20260826230511.972824-13-seanjc@google.com> (raw)
In-Reply-To: <20260826230511.972824-1-seanjc@google.com>
Add and use APIs to override memory region types instead of open coding the
logic in tests, partly to dedup test code, but mostly to add some amount of
protection against conflicting overrides. This will allow converting more
tests to override memory regions without taking on too much risk of bugs in
the long term, e.g. due to silently overriding a region multiple times.
Deliberately "unroll" the loops in __vm_create() and the CMMA test's
create_main_memslot() to avoid filling all memslots in anticipation of
adding an "extra" memslot that is for test use and *only* test use, i.e.
shouldn't be configured by core library code.
Converting tests to overriding memory regions will allow taking the region
type instead of the raw memslot in the physical page allocator APIs.
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
.../selftests/kvm/arm64/page_fault_test.c | 18 +++++------
.../testing/selftests/kvm/include/kvm_util.h | 30 +++++++++++++++++++
tools/testing/selftests/kvm/lib/kvm_util.c | 8 +++--
tools/testing/selftests/kvm/s390/cmma_test.c | 19 ++++--------
4 files changed, 49 insertions(+), 26 deletions(-)
diff --git a/tools/testing/selftests/kvm/arm64/page_fault_test.c b/tools/testing/selftests/kvm/arm64/page_fault_test.c
index 6bb3d82906b2..b0e3fc8cd76a 100644
--- a/tools/testing/selftests/kvm/arm64/page_fault_test.c
+++ b/tools/testing/selftests/kvm/arm64/page_fault_test.c
@@ -597,18 +597,16 @@ static void setup_memslots(struct kvm_vm *vm, struct test_params *p)
vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, 0,
CODE_AND_DATA_MEMSLOT, code_npages, 0);
- vm->memslots[MEM_REGION_CODE] = CODE_AND_DATA_MEMSLOT;
- vm->memslots[MEM_REGION_DATA] = CODE_AND_DATA_MEMSLOT;
+ ____vm_override_mem_region(vm, MEM_REGION_CODE, CODE_AND_DATA_MEMSLOT);
+ ____vm_override_mem_region(vm, MEM_REGION_DATA, CODE_AND_DATA_MEMSLOT);
- vm_userspace_mem_region_add(vm, p->src_type, data_gpa - pt_size,
- PAGE_TABLE_MEMSLOT, pt_size / guest_page_size,
- p->test_desc->pt_memslot_flags);
- vm->memslots[MEM_REGION_PT] = PAGE_TABLE_MEMSLOT;
+ __vm_override_mem_region(vm, MEM_REGION_PT, p->src_type, data_gpa - pt_size,
+ PAGE_TABLE_MEMSLOT, pt_size / guest_page_size,
+ p->test_desc->pt_memslot_flags);
- vm_userspace_mem_region_add(vm, p->src_type, data_gpa, TEST_DATA_MEMSLOT,
- data_size / guest_page_size,
- p->test_desc->data_memslot_flags);
- vm->memslots[MEM_REGION_TEST_DATA] = TEST_DATA_MEMSLOT;
+ __vm_override_mem_region(vm, MEM_REGION_TEST_DATA, p->src_type, data_gpa,
+ TEST_DATA_MEMSLOT, data_size / guest_page_size,
+ p->test_desc->data_memslot_flags);
}
static void setup_ucall(struct kvm_vm *vm)
diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
index 70e77992dd2f..14f87c8a00ec 100644
--- a/tools/testing/selftests/kvm/include/kvm_util.h
+++ b/tools/testing/selftests/kvm/include/kvm_util.h
@@ -704,6 +704,36 @@ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type,
gpa_t gpa, u32 slot, u64 npages, u32 flags,
int guest_memfd_fd, u64 guest_memfd_offset);
+
+static inline void ____vm_override_mem_region(struct kvm_vm *vm,
+ enum kvm_mem_region_type type,
+ u32 slot)
+{
+ TEST_ASSERT(vm->memslots[type] == KVM_INVALID_MEMSLOT,
+ "Memory region type '%u' was already overridden with slot=%u",
+ type, vm->memslots[type]);
+
+ vm->memslots[type] = slot;
+}
+
+static inline void __vm_override_mem_region(struct kvm_vm *vm,
+ enum kvm_mem_region_type type,
+ enum vm_mem_backing_src_type src_type,
+ gpa_t gpa, u32 slot, u64 npages,
+ u32 flags)
+{
+ ____vm_override_mem_region(vm, type, slot);
+ vm_userspace_mem_region_add(vm, src_type, gpa, slot, npages, flags);
+}
+
+static inline void vm_override_mem_region(struct kvm_vm *vm,
+ enum kvm_mem_region_type type,
+ enum vm_mem_backing_src_type src_type,
+ gpa_t gpa, u32 slot, u64 npages)
+{
+ __vm_override_mem_region(vm, type, src_type, gpa, slot, npages, 0);
+}
+
#ifndef vm_arch_has_protected_memory
static inline bool vm_arch_has_protected_memory(struct kvm_vm *vm)
{
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index 56df5b1d9c79..781af2928d04 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -495,7 +495,7 @@ struct kvm_vm *__vm_create(struct vm_shape shape, u32 nr_runnable_vcpus,
nr_extra_pages);
struct userspace_mem_region *slot0;
struct kvm_vm *vm;
- int i, flags;
+ int flags;
kvm_set_files_rlimit(nr_runnable_vcpus);
@@ -513,8 +513,10 @@ struct kvm_vm *__vm_create(struct vm_shape shape, u32 nr_runnable_vcpus,
flags |= KVM_MEM_GUEST_MEMFD;
vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, 0, 0, nr_pages, flags);
- for (i = 0; i < NR_MEM_REGIONS; i++)
- vm->memslots[i] = 0;
+ ____vm_override_mem_region(vm, MEM_REGION_CODE, 0);
+ ____vm_override_mem_region(vm, MEM_REGION_PT, 0);
+ ____vm_override_mem_region(vm, MEM_REGION_DATA, 0);
+ ____vm_override_mem_region(vm, MEM_REGION_TEST_DATA, 0);
kvm_vm_elf_load(vm, program_invocation_name);
diff --git a/tools/testing/selftests/kvm/s390/cmma_test.c b/tools/testing/selftests/kvm/s390/cmma_test.c
index 15d81b2ed7ad..73387cc3bca7 100644
--- a/tools/testing/selftests/kvm/s390/cmma_test.c
+++ b/tools/testing/selftests/kvm/s390/cmma_test.c
@@ -102,24 +102,17 @@ static void guest_dirty_test_data(void)
static void create_main_memslot(struct kvm_vm *vm)
{
- int i;
-
vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, 0, 0, MAIN_PAGE_COUNT, 0);
- /* set the array of memslots to zero like __vm_create does */
- for (i = 0; i < NR_MEM_REGIONS; i++)
- vm->memslots[i] = 0;
+ ____vm_override_mem_region(vm, MEM_REGION_CODE, 0);
+ ____vm_override_mem_region(vm, MEM_REGION_PT, 0);
+ ____vm_override_mem_region(vm, MEM_REGION_DATA, 0);
}
static void create_test_memslot(struct kvm_vm *vm)
{
- vm_userspace_mem_region_add(vm,
- VM_MEM_SRC_ANONYMOUS,
- TEST_DATA_START_GFN << vm->page_shift,
- TEST_DATA_MEMSLOT,
- TEST_DATA_PAGE_COUNT,
- 0
- );
- vm->memslots[MEM_REGION_TEST_DATA] = TEST_DATA_MEMSLOT;
+ vm_override_mem_region(vm, MEM_REGION_TEST_DATA, VM_MEM_SRC_ANONYMOUS,
+ TEST_DATA_START_GFN << vm->page_shift,
+ TEST_DATA_MEMSLOT, TEST_DATA_PAGE_COUNT);
}
static void create_memslots(struct kvm_vm *vm)
--
2.55.0.887.g758fc8c411-goog
next prev parent reply other threads:[~2026-08-26 23:06 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-26 23:04 [PATCH 00/20] KVM: selftests: PPC pre-enabling Sean Christopherson
2026-08-26 23:04 ` [PATCH 01/20] KVM: selftests: Use MEM_REGION_PT memslot instead of '0' for s390 regions/segments Sean Christopherson
2026-08-26 23:04 ` [PATCH 02/20] KVM: selftests: Bump the minimum GPA for page tables to 0x200000 Sean Christopherson
2026-08-26 23:04 ` [PATCH 03/20] KVM: selftests: Use vm_alloc_page_table() to allocate LoongArch page tables Sean Christopherson
2026-08-27 6:43 ` Bibo Mao
2026-08-26 23:04 ` [PATCH 04/20] KVM: selftests: Rename "num" param to "nr_pages" for physical page allocators Sean Christopherson
2026-08-26 23:04 ` [PATCH 05/20] KVM: selftests: Use goto instead of do-while to retry finding unused physical pages Sean Christopherson
2026-08-26 23:04 ` [PATCH 06/20] KVM: selftests: Extend page allocator to support naturally aligned allocations Sean Christopherson
2026-08-26 23:04 ` [PATCH 07/20] KVM: selftests: Make the single-page allocator APIs static inline Sean Christopherson
2026-08-26 23:04 ` [PATCH 08/20] KVM: selftests: Use the innermost page allocator API in the memslot perf test Sean Christopherson
2026-08-26 23:05 ` [PATCH 09/20] KVM: selftests: Use the innermost page allocator API in s390's IRQ routing test Sean Christopherson
2026-08-26 23:05 ` [PATCH 10/20] KVM: selftests: Add a wrapper API to allocate multiple page table pages Sean Christopherson
2026-08-27 5:27 ` Itaru Kitayama
2026-08-26 23:05 ` [PATCH 11/20] KVM: selftests: Initialize vm->memslots[] with invalid memslots during creation Sean Christopherson
2026-08-26 23:05 ` Sean Christopherson [this message]
2026-08-26 23:05 ` [PATCH 13/20] KVM: selftests: Add TEST_EXTRA memory region type for "special" memslots Sean Christopherson
2026-08-27 4:37 ` Itaru Kitayama
2026-08-26 23:05 ` [PATCH 14/20] KVM: selftests: Use TEST_EXTRA region in arm64's vGIC LPI stress test Sean Christopherson
2026-08-26 23:05 ` [PATCH 15/20] KVM: selftests: Use TEST_EXTRA region in x86's smaller MAXPHYADDR test Sean Christopherson
2026-08-26 23:05 ` [PATCH 16/20] KVM: selftests: Use TEST_EXTRA region in set memory region test Sean Christopherson
2026-08-26 23:05 ` [PATCH 17/20] KVM: selftests: Take the memory region type, not memslot, in page allocators Sean Christopherson
2026-08-26 23:05 ` [PATCH 18/20] KVM: selftests: Use TEST_ASSERT(), not assert(), in vm_get_mem_region() Sean Christopherson
2026-08-26 23:05 ` [PATCH 19/20] KVM: selftests: Automatically pick min_gpa for allocations based on region type Sean Christopherson
2026-08-26 23:05 ` [PATCH 20/20] KVM: selftests: Add arch hook to force page tables to be naturally aligned Sean Christopherson
2026-08-27 8:07 ` [PATCH 00/20] KVM: selftests: PPC pre-enabling Itaru Kitayama
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260826230511.972824-13-seanjc@google.com \
--to=seanjc@google.com \
--cc=alex@ghiti.fr \
--cc=anup@brainfault.org \
--cc=aou@eecs.berkeley.edu \
--cc=atish.patra@linux.dev \
--cc=borntraeger@linux.ibm.com \
--cc=chenhuacai@kernel.org \
--cc=david@kernel.org \
--cc=frankja@linux.ibm.com \
--cc=fuad.tabba@linux.dev \
--cc=imbrenda@linux.ibm.com \
--cc=joey.gouly@arm.com \
--cc=kvm-riscv@lists.infradead.org \
--cc=kvm@vger.kernel.org \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=loongarch@lists.linux.dev \
--cc=maobibo@loongson.cn \
--cc=maz@kernel.org \
--cc=npiggin@gmail.com \
--cc=oupton@kernel.org \
--cc=palmer@dabbelt.com \
--cc=pbonzini@redhat.com \
--cc=pjw@kernel.org \
--cc=ritesh.list@gmail.com \
--cc=seiden@linux.ibm.com \
--cc=suzuki.poulose@arm.com \
--cc=yuzenghui@huawei.com \
--cc=zhaotianrui@loongson.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox