KVM: selftests: PPC pre-enabling
Sean Christopherson
maintainerv2 cover KVM: selftests: PPC pre-enabling
This is a big pile of cleanups and refactoring to prepare for PPC support in KVM selftests. PPC has paging modes that require multi-page page tables, and those page tables need to be naturally aligned. The original proposal was to plumb in an @align param to the page allocators[*], but I didn't love the idea of adding another rarely used param to the allocators.
v2:
- Collect tags. [Itaru, Bibo]
- Use 0x180000 as the min GPA for page table pages on LoongArch, instead of changing every other arch to use 0x200000. [Sashiko, Bibo]
- Opportunistically move the vGIC LPI stress test's global "gpa_base" variable into setup_memslot(). [Sashiko]
v1: https://lore.kernel.org/all/[email protected]
Nicholas Piggin (1):
KVM: selftests: Use goto instead of do-while to retry finding unused
physical pages
Sean Christopherson (19):
KVM: selftests: Use MEM_REGION_PT memslot instead of '0' for s390
regions/segments
KVM: selftests: Use the common minimum GPA for page tables on
LoongArch
KVM: selftests: Use vm_alloc_page_table() to allocate LoongArch page
tables
KVM: selftests: Rename "num" param to "nr_pages" for physical page
allocators
KVM: selftests: Extend page allocator to support naturally aligned
allocations
KVM: selftests: Make the single-page allocator APIs static inline
KVM: selftests: Use the innermost page allocator API in the memslot
perf test
KVM: selftests: Use the innermost page allocator API in s390's IRQ
routing test
KVM: selftests: Add a wrapper API to allocate multiple page table
pages
KVM: selftests: Initialize vm->memslots[] with invalid memslots during
creation
KVM: selftests: Add APIs to override memory region types with custom
memslots
KVM: selftests: Add TEST_EXTRA memory region type for "special"
memslots
KVM: selftests: Use TEST_EXTRA region in arm64's vGIC LPI stress test
KVM: selftests: Use TEST_EXTRA region in x86's smaller MAXPHYADDR test
KVM: selftests: Use TEST_EXTRA region in set memory region test
KVM: selftests: Take the memory region type, not memslot, in page
allocators
KVM: selftests: Use TEST_ASSERT(), not assert(), in
vm_get_mem_region()
KVM: selftests: Automatically pick min_gpa for allocations based on
region type
KVM: selftests: Add arch hook to force page tables to be naturally
aligned
.../selftests/kvm/arm64/page_fault_test.c | 18 ++- .../selftests/kvm/arm64/vgic_lpi_stress.c | 21 ++- .../testing/selftests/kvm/include/kvm_util.h | 106 +++++++++++++-- tools/testing/selftests/kvm/include/x86/smm.h | 2 +- .../selftests/kvm/lib/arm64/processor.c | 4 +- tools/testing/selftests/kvm/lib/kvm_util.c | 127 +++++++++++------- .../selftests/kvm/lib/loongarch/processor.c | 5 +- .../selftests/kvm/lib/riscv/processor.c | 4 +- .../selftests/kvm/lib/s390/processor.c | 7 +- .../testing/selftests/kvm/lib/x86/processor.c | 15 ++- .../testing/selftests/kvm/memslot_perf_test.c | 3 +- tools/testing/selftests/kvm/s390/cmma_test.c | 19 +-- .../testing/selftests/kvm/s390/irq_routing.c | 2 +- .../selftests/kvm/set_memory_region_test.c | 8 +- .../x86/smaller_maxphyaddr_emulation_test.c | 9 +- 15 files changed, 221 insertions(+), 129 deletions(-)
base-commit: 76671054f9a1ff6abb976583cd8da37650acdc97
Signature · 1 line
2.55.0.966.g6673acef38-goog
Sean Christopherson
maintainerPATCH v2 1/20 KVM: selftests: Use MEM_REGION_PT memslot instead of '0' for s390 regions/segments
Use MEM_REGION_PT to indirectly specify memslot '0' instead of hardcoding the memslot in s390's virtual memory allocator, as a first step towards taking the allocation type instead of the exact, raw memslot in the physical memory allocator.
Note, the only tests that set MEM_REGION_PT to a memslot other than '0' are arm64-only, i.e. s390 always uses memslot '0' for page tables.
No functional change intended.
--- tools/testing/selftests/kvm/lib/s390/processor.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/lib/s390/processor.c +2 -1
@@ -39,7 +39,8 @@ static u64 virt_alloc_region(struct kvm_vm *vm, int ri)u64 taddr; taddr =vm_phy_pages_alloc (vm, ri <4 ?PAGES_PER_REGION :1 , -KVM_GUEST_PAGE_TABLE_MIN_PADDR ,0 ); +KVM_GUEST_PAGE_TABLE_MIN_PADDR , + vm->memslots[MEM_REGION_PT ]);memset (addr_gpa2hva (vm, taddr),0xff ,PAGES_PER_REGION * vm->page_size);return (taddr ®ION_ENTRY_ORIGIN )
Signature · 1 line
2.55.0.966.g6673acef38-goog
[email protected]
AI reviewRe: [PATCH v2 01/20] KVM: selftests: Use MEM_REGION_PT memslot instead of '0' for s390 regions/segments
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [Medium] Out-of-bounds memory write in s390 virt_alloc_region due to mismatched allocation and initialization sizes when setting up a page table. --
commit 6a3a77e3cef84a1f8a83db6921ea5e3f5b11c1ed
Author: Sean Christopherson <[email protected]>
KVM: selftests: Use MEM_REGION_PT memslot instead of '0' for s390 regions/segments
This commit updates s390's virtual memory allocator to use MEM_REGION_PT instead of hardcoding memslot '0'. This acts as a preparatory step for allowing the physical memory allocator to take the allocation type rather than the raw memslot.
tools/testing/selftests/kvm/lib/s390/processor.c static u64 virt_alloc_region(struct kvm_vm *vm, int ri) · line 44
Quoted 12 lines
tools/testing/selftests/kvm/lib/s390/processor.c +2
-1@@ -39,7 +39,8 @@ static u64 virt_alloc_region(struct kvm_vm *vm, int ri)u64 taddr; taddr =vm_phy_pages_alloc (vm, ri <4 ?PAGES_PER_REGION :1 , -KVM_GUEST_PAGE_TABLE_MIN_PADDR ,0 ); +KVM_GUEST_PAGE_TABLE_MIN_PADDR , + vm->memslots[MEM_REGION_PT ]);memset (addr_gpa2hva (vm, taddr),0xff ,PAGES_PER_REGION * vm->page_size);
[Severity: Medium]
This is a pre-existing issue, but does this memset cause an out-of-bounds write when allocating a page table?
When ri == 4, exactly 1 page is allocated by vm_phy_pages_alloc(), but this memset() unconditionally clears PAGES_PER_REGION (4) pages. This overwrites 3 adjacent physical pages, which may corrupt other page tables, guest code, or test data during s390 guest page table setup in virt_arch_pg_map().
Quoted 2 lines
tools/testing/selftests/kvm/lib/s390/processor.c +0
-0return (taddr ®ION_ENTRY_ORIGIN )
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v2 01/20] KVM: selftests: Use MEM_REGION_PT memslot instead of '0' for s390 regions/segments
In reply to Sean ChristophersonQuoted 5 lines · On Wed, 2 Sep 2026 09:41:04 -0700 Sean Christopherson <[email protected]>…
Use
MEM_REGION_PTto indirectly specify memslot '0' instead of hardcoding the memslot in s390's virtual memory allocator, as a first step towards taking the allocation type instead of the exact, raw memslot in the physical memory allocator.
Quoted 7 lines
Note, the only tests that set
MEM_REGION_PTto a memslot other than '0' are arm64-only, i.e. s390 always uses memslot '0' for page tables.No functional change intended.
Tested-by Itaru Kitayama <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Quoted 18 lines
--- tools/testing/selftests/kvm/lib/s390/processor.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)tools/testing/selftests/kvm/lib/s390/processor.c +2
-1@@ -39,7 +39,8 @@ static u64 virt_alloc_region(struct kvm_vm *vm, int ri)u64 taddr; taddr =vm_phy_pages_alloc (vm, ri <4 ?PAGES_PER_REGION :1 , -KVM_GUEST_PAGE_TABLE_MIN_PADDR ,0 ); +KVM_GUEST_PAGE_TABLE_MIN_PADDR , + vm->memslots[MEM_REGION_PT ]);memset (addr_gpa2hva (vm, taddr),0xff ,PAGES_PER_REGION * vm->page_size);return (taddr ®ION_ENTRY_ORIGIN )
Sean Christopherson
maintainerPATCH v2 2/20 KVM: selftests: Use the common minimum GPA for page tables on LoongArch
In reply to Sean ChristophersonUse the common minimum GPA for page tables (0x180000) on LoongArch instead of LoongArch's custom minimum of 0x200000, so that LoongArch can use vm_alloc_page_table() instead of open coding a rough equivalent. There are no known reasons why LoongArch can't use a lower, less-aligned minimum. The more-aligned value was originally selected to support huge pages for the guest, but that only requires the address of the final allocation to be aligned, the addresses of page table pages is irrelevant on that front.
--- tools/testing/selftests/kvm/lib/loongarch/processor.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
tools/testing/selftests/kvm/lib/loongarch/processor.c +1 -2
@@ -9,7 +9,6 @@#include "processor.h" #include "ucall_common.h" -#define LOONGARCH_PAGE_TABLE_PHYS_MIN 0x200000#define LOONGARCH_GUEST_STACK_VADDR_MIN 0x200000static gpa_t invalid_pgtable[4 ];
@@ -57,7 +56,7 @@ void virt_arch_pgd_alloc(struct kvm_vm *vm) child = table =0 ;for (i =0 ; i < vm->mmu.pgtable_levels; i++) { invalid_pgtable[i] = child; - table =vm_phy_page_alloc (vm,LOONGARCH_PAGE_TABLE_PHYS_MIN , + table =vm_phy_page_alloc (vm,KVM_GUEST_PAGE_TABLE_MIN_PADDR , vm->memslots[MEM_REGION_PT ]);TEST_ASSERT (table,"Fail to allocate page tale at level %d\n" , i);virt_set_pgtable (vm, table, child);
Signature · 1 line
2.55.0.966.g6673acef38-goog
Re: [PATCH v2 02/20] KVM: selftests: Use the common minimum GPA for page tables on LoongArch
Quoted 27 lines · Sean Christopherson
Use the common minimum GPA for page tables (
0x180000) on LoongArch instead of LoongArch's custom minimum of0x200000, so that LoongArch can usevm_alloc_page_table()instead of open coding a rough equivalent. There are no known reasons why LoongArch can't use a lower, less-aligned minimum. The more-aligned value was originally selected to support huge pages for the guest, but that only requires the address of the final allocation to be aligned, the addresses of page table pages is irrelevant on that front.Signed-off-by Sean Christopherson <[email protected]>--- tools/testing/selftests/kvm/lib/loongarch/processor.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)tools/testing/selftests/kvm/lib/loongarch/processor.c +0
-1@@ -9,7 +9,6 @@#include "processor.h" #include "ucall_common.h" -#define LOONGARCH_PAGE_TABLE_PHYS_MIN 0x200000#define LOONGARCH_GUEST_STACK_VADDR_MIN 0x200000static gpa_t invalid_pgtable[4 ];
tools/testing/selftests/kvm/lib/loongarch/processor.c void virt_arch_pgd_alloc(struct kvm_vm *vm) · line 62
Quoted 8 lines
tools/testing/selftests/kvm/lib/loongarch/processor.c +1
-1@@ -57,7 +56,7 @@ void virt_arch_pgd_alloc(struct kvm_vm *vm) child = table =0 ;for (i =0 ; i < vm->mmu.pgtable_levels; i++) { invalid_pgtable[i] = child; - table =vm_phy_page_alloc (vm,LOONGARCH_PAGE_TABLE_PHYS_MIN , + table =vm_phy_page_alloc (vm,KVM_GUEST_PAGE_TABLE_MIN_PADDR , vm->memslots[MEM_REGION_PT ]);TEST_ASSERT (table,"Fail to allocate page tale at level %d\n" , i);virt_set_pgtable (vm, table, child);
Sean Christopherson
maintainerPATCH v2 3/20 KVM: selftests: Use vm_alloc_page_table() to allocate LoongArch page tables
In reply to Sean ChristophersonNow that LoongArch uses KVM_GUEST_PAGE_TABLE_MIN_PADDR for its page table page allocations, use the common vm_alloc_page_table() instead of open coding the same.
Opportunistically drop the assert that the allocation succeeded, as the allocator itself guarantees success.
No functional change intended.
--- tools/testing/selftests/kvm/lib/loongarch/processor.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
tools/testing/selftests/kvm/lib/loongarch/processor.c +1 -3
@@ -56,9 +56,7 @@ void virt_arch_pgd_alloc(struct kvm_vm *vm) child = table =0 ;for (i =0 ; i < vm->mmu.pgtable_levels; i++) { invalid_pgtable[i] = child; - table =vm_phy_page_alloc (vm,KVM_GUEST_PAGE_TABLE_MIN_PADDR , - vm->memslots[MEM_REGION_PT ]); -TEST_ASSERT (table,"Fail to allocate page tale at level %d\n" , i); + table =vm_alloc_page_table (vm);virt_set_pgtable (vm, table, child); child = table; }
Signature · 1 line
2.55.0.966.g6673acef38-goog
Sean Christopherson
maintainerPATCH v2 4/20 KVM: selftests: Rename "num" param to "nr_pages" for physical page allocators
In reply to Sean ChristophersonRename the "num" param in the physical page allocator APIs to "nr_pages" to make the unit and role of the param more obvious.
Opportunistically rework the function comment to drop the superfluous kerneldoc style, and to very explicitly call out that success is guaranteed.
No functional change intended.
--- .../testing/selftests/kvm/include/kvm_util.h | 6 ++-- tools/testing/selftests/kvm/lib/kvm_util.c | 35 ++++++------------- 2 files changed, 14 insertions(+), 27 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +3 -3
@@ -991,11 +991,11 @@ void kvm_gsi_routing_write(struct kvm_vm *vm, struct kvm_irq_routing *routing);const char *exit_reason_str (unsigned int exit_reason);gpa_t vm_phy_page_alloc (struct kvm_vm *vm,gpa_t min_gpa,u32 memslot); -gpa_t __vm_phy_pages_alloc (struct kvm_vm *vm,size_t num,gpa_t min_gpa, +gpa_t __vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,gpa_t min_gpa,u32 memslot,bool protected);gpa_t vm_alloc_page_table (struct kvm_vm *vm); -static inline gpa_t vm_phy_pages_alloc (struct kvm_vm *vm,size_t num, +static inline gpa_t vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,gpa_t min_gpa,u32 memslot) {/*
@@ -1003,7 +1003,7 @@ static inline gpa_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num,* protected memory, as the majority of memory for such VMs is * protected, i.e. using shared memory is effectively opt-in. */ -return __vm_phy_pages_alloc (vm, num, min_gpa, memslot, +return __vm_phy_pages_alloc (vm, nr_pages, min_gpa, memslot,vm_arch_has_protected_memory (vm)); }
tools/testing/selftests/kvm/lib/kvm_util.c +11 -24
@@ -2025,33 +2025,20 @@ const char *exit_reason_str(unsigned int exit_reason) }/* -* Physical Contiguous Page Allocator +* Allocate contiguous (guest) physical pages in a given memory region, at or +* the minimum specific GPA. If the memory is protected/private, also add the +* allocate pages to the region's set of protected pages, e.g. so that arch +* code knows which pages need to be encrypted when launching the VM. * -* Input Args: -* vm - Virtual Machine -* num - number of pages -* min_gpa - Physical address minimum -* memslot - Memory region to allocate page from -* protected - True if the pages will be used as protected/private memory -* -* Output Args: None -* -* Return: -* Starting physical address -* -* Within the VM specified by vm, locates a range of available physical -* pages at or above min_gpa. If found, the pages are marked as in use -* and their base address is returned. A TEST_ASSERT failure occurs if -* not enough pages are available at or above min_gpa. +* Note, success is guaranteed! */ -gpa_t __vm_phy_pages_alloc (struct kvm_vm *vm,size_t num, -gpa_t min_gpa,u32 memslot, -bool protected) +gpa_t __vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,gpa_t min_gpa, +u32 memslot,bool protected) {struct userspace_mem_region *region;sparsebit_idx_t pg, base; -TEST_ASSERT (num >0 ,"Must allocate at least one page" ); +TEST_ASSERT (nr_pages,"Must allocate at least one page" );TEST_ASSERT ((min_gpa % vm->page_size) ==0 ,"Min physical address " "not divisible by page size.\n"
@@ -2064,13 +2051,13 @@ gpa_t __vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, base = pg = min_gpa >> vm->page_shift;do { -for (; pg < base + num; ++pg) { +for (; pg < base + nr_pages; ++pg) {if (!sparsebit_is_set (region->unused_phy_pages, pg)) { base = pg =sparsebit_next_set (region->unused_phy_pages, pg);break ; } } - }while (pg && pg != base + num); + }while (pg && pg != base + nr_pages);if (pg ==0 ) { fprintf(stderr, "No guest physical page available, "
@@ -2081,7 +2068,7 @@ gpa_t __vm_phy_pages_alloc(struct kvm_vm *vm, size_t num,abort (); } -for (pg = base; pg < base + num; ++pg) { +for (pg = base; pg < base + nr_pages; ++pg) {sparsebit_clear (region->unused_phy_pages, pg);if (protected)sparsebit_set (region->protected_phy_pages, pg);
Signature · 1 line
2.55.0.966.g6673acef38-goog
[email protected]
AI reviewRe: [PATCH v2 04/20] KVM: selftests: Rename "num" param to "nr_pages" for physical page allocators
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Grammatical and missing word errors in the updated function comment. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Grammatical and missing word errors in the updated function comment. --
commit fcd1f5cf5e36c06f5f1aaed2058d2615dea81241
Author: Sean Christopherson <[email protected]>
KVM: selftests: Rename "num" param to "nr_pages" for physical page allocators
This commit renames the "num" parameter to "nr_pages" in the physical page allocator APIs to clarify its unit and role. It also reworks the function comment to drop the kerneldoc style and explicitly state that success is guaranteed.
tools/testing/selftests/kvm/lib/kvm_util.c const char *exit_reason_str(unsigned int exit_reason) · line 2030
Quoted 12 lines
tools/testing/selftests/kvm/lib/kvm_util.c +3
-1@@ -2025,33 +2025,20 @@ const char *exit_reason_str(unsigned int exit_reason) }/* -* Physical Contiguous Page Allocator +* Allocate contiguous (guest) physical pages in a given memory region, at or +* the minimum specific GPA. If the memory is protected/private, also add the +* allocate pages to the region's set of protected pages, e.g. so that arch
[Severity: Low]
This isn't a bug, but there appear to be a couple of typos in the updated comment block.
Should "at or the minimum specific GPA" be "at or above the minimum specified GPA"?
Also, should "add the allocate pages" be "add the allocated pages"?
Quoted 9 lines
tools/testing/selftests/kvm/lib/kvm_util.c +5
-3+* code knows which pages need to be encrypted when launching the VM. +* +* Note, success is guaranteed! */ -gpa_t __vm_phy_pages_alloc (struct kvm_vm *vm,size_t num, -gpa_t min_gpa,u32 memslot, -bool protected) +gpa_t __vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,gpa_t min_gpa, +u32 memslot,bool protected)
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH v2 04/20] KVM: selftests: Rename "num" param to "nr_pages" for physical page allocators
In reply to Sean ChristophersonQuoted 47 lines · Sean Christopherson
Rename the "num" param in the physical page allocator APIs to "
nr_pages" to make the unit and role of the param more obvious.Opportunistically rework the function comment to drop the superfluous kerneldoc style, and to very explicitly call out that success is guaranteed.
No functional change intended.
Tested-by Itaru Kitayama <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- .../testing/selftests/kvm/include/kvm_util.h | 6 ++-- tools/testing/selftests/kvm/lib/kvm_util.c | 35 ++++++------------- 2 files changed, 14 insertions(+), 27 deletions(-)tools/testing/selftests/kvm/include/kvm_util.h +3
-3@@ -991,11 +991,11 @@ void kvm_gsi_routing_write(struct kvm_vm *vm, struct kvm_irq_routing *routing);const char *exit_reason_str (unsigned int exit_reason);gpa_t vm_phy_page_alloc (struct kvm_vm *vm,gpa_t min_gpa,u32 memslot); -gpa_t __vm_phy_pages_alloc (struct kvm_vm *vm,size_t num,gpa_t min_gpa, +gpa_t __vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,gpa_t min_gpa,u32 memslot,bool protected);gpa_t vm_alloc_page_table (struct kvm_vm *vm); -static inline gpa_t vm_phy_pages_alloc (struct kvm_vm *vm,size_t num, +static inline gpa_t vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,gpa_t min_gpa,u32 memslot) {/* @@ -1003,7 +1003,7 @@ static inline gpa_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num,* protected memory, as the majority of memory for such VMs is * protected, i.e. using shared memory is effectively opt-in. */ -return __vm_phy_pages_alloc (vm, num, min_gpa, memslot, +return __vm_phy_pages_alloc (vm, nr_pages, min_gpa, memslot,vm_arch_has_protected_memory (vm)); }tools/testing/selftests/kvm/lib/kvm_util.c +0
-0
tools/testing/selftests/kvm/lib/kvm_util.c const char *exit_reason_str(unsigned int exit_reason) · line 2029
Quoted 6 lines
tools/testing/selftests/kvm/lib/kvm_util.c +2
-1@@ -2025,33 +2025,20 @@ const char *exit_reason_str(unsigned int exit_reason) }/* -* Physical Contiguous Page Allocator +* Allocate contiguous (guest) physical pages in a given memory region, at or +* the minimum specific GPA. If the memory is protected/private, also add the
at or "above" the minimum ...
Otherwise looks good. Feel free to add:
Sean Christopherson
maintainerPATCH v2 5/20 KVM: selftests: Use goto instead of do-while to retry finding unused physical pages
In reply to Sean ChristophersonFrom: Nicholas Piggin <[email protected]>
Rework the core loop of the physical page allocator to use a goto instead of do-while loop when retrying with a new base address. The do-while loop obviously works, but it obfuscates the success vs. failure paths, and in general is convoluted.
For all intents and purposes, no functional change intended.
--- tools/testing/selftests/kvm/lib/kvm_util.c | 32 +++++++++++----------- 1 file changed, 16 insertions(+), 16 deletions(-)
tools/testing/selftests/kvm/lib/kvm_util.c +16 -16
@@ -2049,23 +2049,15 @@ gpa_t __vm_phy_pages_alloc(struct kvm_vm *vm, size_t nr_pages, gpa_t min_gpa,TEST_ASSERT (!protected || region->protected_phy_pages,"Region doesn't support protected memory" ); - base = pg = min_gpa >> vm->page_shift; -do { -for (; pg < base + nr_pages; ++pg) { -if (!sparsebit_is_set (region->unused_phy_pages, pg)) { - base = pg =sparsebit_next_set (region->unused_phy_pages, pg); -break ; - } + base = min_gpa >> vm->page_shift; +again: +for (pg = base; pg < base + nr_pages; ++pg) { +if (!sparsebit_is_set (region->unused_phy_pages, pg)) { + base =sparsebit_next_set (region->unused_phy_pages, pg); +if (!base) + goto enomem; + goto again; } - }while (pg && pg != base + nr_pages); - -if (pg ==0 ) { -fprintf (stderr,"No guest physical page available, " -"min_gpa: 0x%lx page_size: 0x%x memslot: %u\n" , - min_gpa, vm->page_size, memslot); -fputs ("---- vm dump ----\n" , stderr); -vm_dump (stderr, vm,2 ); -abort (); }for (pg = base; pg < base + nr_pages; ++pg) {
@@ -2075,6 +2067,14 @@ gpa_t __vm_phy_pages_alloc(struct kvm_vm *vm, size_t nr_pages, gpa_t min_gpa, }return base * vm->page_size; + +enomem: +fprintf (stderr,"No guest physical page available, min_gpa: 0x%lx page_size: 0x%x memslot: %u\n" , + min_gpa, vm->page_size, memslot); +fputs ("---- vm dump ----\n" , stderr); +vm_dump (stderr, vm,2 ); +abort (); +__builtin_unreachable (); }gpa_t vm_phy_page_alloc (struct kvm_vm *vm,gpa_t min_gpa,u32 memslot)
Signature · 1 line
2.55.0.966.g6673acef38-goog
Re: [PATCH v2 05/20] KVM: selftests: Use goto instead of do-while to retry finding unused physical pages
Quoted 73 lines · Sean Christopherson
From: Nicholas Piggin <[email protected]>
Rework the core loop of the physical page allocator to use a goto instead of do-while loop when retrying with a new base address. The do-while loop obviously works, but it obfuscates the success vs. failure paths, and in general is convoluted.
For all intents and purposes, no functional change intended.
Signed-off-by Nicholas Piggin <[email protected]>[Rebased to latest mainline tree]Signed-off-by Ritesh Harjani (IBM) <[email protected]>[sean: separate from alignment change, use enomem label, write changelog]Tested-by Itaru Kitayama <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- tools/testing/selftests/kvm/lib/kvm_util.c | 32 +++++++++++----------- 1 file changed, 16 insertions(+), 16 deletions(-)tools/testing/selftests/kvm/lib/kvm_util.c +16
-16@@ -2049,23 +2049,15 @@ gpa_t __vm_phy_pages_alloc(struct kvm_vm *vm, size_t nr_pages, gpa_t min_gpa,TEST_ASSERT (!protected || region->protected_phy_pages,"Region doesn't support protected memory" ); - base = pg = min_gpa >> vm->page_shift; -do { -for (; pg < base + nr_pages; ++pg) { -if (!sparsebit_is_set (region->unused_phy_pages, pg)) { - base = pg =sparsebit_next_set (region->unused_phy_pages, pg); -break ; - } + base = min_gpa >> vm->page_shift; +again: +for (pg = base; pg < base + nr_pages; ++pg) { +if (!sparsebit_is_set (region->unused_phy_pages, pg)) { + base =sparsebit_next_set (region->unused_phy_pages, pg); +if (!base) + goto enomem; + goto again; } - }while (pg && pg != base + nr_pages); - -if (pg ==0 ) { -fprintf (stderr,"No guest physical page available, " -"min_gpa: 0x%lx page_size: 0x%x memslot: %u\n" , - min_gpa, vm->page_size, memslot); -fputs ("---- vm dump ----\n" , stderr); -vm_dump (stderr, vm,2 ); -abort (); }for (pg = base; pg < base + nr_pages; ++pg) {@@ -2075,6 +2067,14 @@ gpa_t __vm_phy_pages_alloc(struct kvm_vm *vm, size_t nr_pages, gpa_t min_gpa, }return base * vm->page_size; + +enomem: +fprintf (stderr,"No guest physical page available, min_gpa: 0x%lx page_size: 0x%x memslot: %u\n" , + min_gpa, vm->page_size, memslot); +fputs ("---- vm dump ----\n" , stderr); +vm_dump (stderr, vm,2 ); +abort (); +__builtin_unreachable (); }gpa_t vm_phy_page_alloc (struct kvm_vm *vm,gpa_t min_gpa,u32 memslot)Signature · 1 line
2.55.0.966.g6673acef38-goog
Sean Christopherson
maintainerPATCH v2 6/20 KVM: selftests: Extend page allocator to support naturally aligned allocations
In reply to Sean ChristophersonExtend the page allocator to support naturally aligned allocations, as some flavors of PowerPC page tables require multiple guest pages per table, and those tables need to be naturally aligned.
Take care to align the potential base address *inside* the retry loop, i.e. the base needs to be (re)aligned on every attempt.
--- tools/testing/selftests/kvm/include/kvm_util.h | 2 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 13 +++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +2 -0
@@ -991,6 +991,8 @@ void kvm_gsi_routing_write(struct kvm_vm *vm, struct kvm_irq_routing *routing);const char *exit_reason_str (unsigned int exit_reason);gpa_t vm_phy_page_alloc (struct kvm_vm *vm,gpa_t min_gpa,u32 memslot); +gpa_t ____vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,gpa_t min_gpa, +u32 memslot,bool protected,bool naturally_aligned);gpa_t __vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,gpa_t min_gpa,u32 memslot,bool protected);gpa_t vm_alloc_page_table (struct kvm_vm *vm);
tools/testing/selftests/kvm/lib/kvm_util.c +11 -2
@@ -17,6 +17,7 @@#include <sys/stat.h> #include <time.h> #include <unistd.h> +#include <linux/align.h> #include <linux/kernel.h> #define KVM_UTIL_MIN_PFN 2
@@ -2032,9 +2033,10 @@ const char *exit_reason_str(unsigned int exit_reason)* * Note, success is guaranteed! */ -gpa_t __vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,gpa_t min_gpa, -u32 memslot,bool protected) +gpa_t ____vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,gpa_t min_gpa, +u32 memslot,bool protected,bool naturally_aligned) { +size_t alignment = naturally_aligned ? nr_pages :1 ;struct userspace_mem_region *region;sparsebit_idx_t pg, base;
@@ -2051,6 +2053,7 @@ gpa_t __vm_phy_pages_alloc(struct kvm_vm *vm, size_t nr_pages, gpa_t min_gpa, base = min_gpa >> vm->page_shift; again: + base =ALIGN (base, alignment);for (pg = base; pg < base + nr_pages; ++pg) {if (!sparsebit_is_set (region->unused_phy_pages, pg)) { base =sparsebit_next_set (region->unused_phy_pages, pg);
@@ -2077,6 +2080,12 @@ gpa_t __vm_phy_pages_alloc(struct kvm_vm *vm, size_t nr_pages, gpa_t min_gpa,__builtin_unreachable (); } +gpa_t __vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,gpa_t min_gpa, +u32 memslot,bool protected) +{ +return ____vm_phy_pages_alloc (vm, nr_pages, min_gpa, memslot, protected, false); +} +gpa_t vm_phy_page_alloc (struct kvm_vm *vm,gpa_t min_gpa,u32 memslot) {return vm_phy_pages_alloc (vm,1 , min_gpa, memslot);
Signature · 1 line
2.55.0.966.g6673acef38-goog
[email protected]
AI reviewRe: [PATCH v2 06/20] KVM: selftests: Extend page allocator to support naturally aligned allocations
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The `ALIGN()` macro is used with `nr_pages` as the alignment constraint, but there is no…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The ALIGN() macro is used with nr_pages as the alignment constraint, but there is no validation to ensure nr_pages is a power of two, which ALIGN() strictly requires. --
commit 1b8052ce0034b1eff1950b660c7716fd2dffde7a
Author: Sean Christopherson <[email protected]>
KVM: selftests: Extend page allocator to support naturally aligned allocations
This commit extends the KVM selftests page allocator to support naturally aligned allocations. This is primarily for some flavors of PowerPC page tables that require multiple guest pages per table, and those tables need to be naturally aligned.
Quoted 4 lines
tools/testing/selftests/kvm/lib/kvm_util.c +0
-0
[ ... ]
Quoted 13 lines
tools/testing/selftests/kvm/lib/kvm_util.c +3
-2@@ -2032,9 +2033,10 @@ const char *exit_reason_str(unsigned int exit_reason)* * Note, success is guaranteed! */ -gpa_t __vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,gpa_t min_gpa, -u32 memslot,bool protected) +gpa_t ____vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,gpa_t min_gpa, +u32 memslot,bool protected,bool naturally_aligned) { +size_t alignment = naturally_aligned ? nr_pages :1 ;struct userspace_mem_region *region;sparsebit_idx_t pg, base;
[ ... ]
tools/testing/selftests/kvm/lib/kvm_util.c gpa_t __vm_phy_pages_alloc(struct kvm_vm *vm, size_t nr_pages, gpa_t min_gpa, · line 2056
Quoted 5 lines
tools/testing/selftests/kvm/lib/kvm_util.c +1
-0@@ -2051,6 +2053,7 @@ gpa_t __vm_phy_pages_alloc(struct kvm_vm *vm, size_t nr_pages, gpa_t min_gpa, base = min_gpa >> vm->page_shift; again: + base =ALIGN (base, alignment);
[Severity: Medium]
Are there any scenarios where ____vm_phy_pages_alloc() is called with naturally_aligned set to true but nr_pages is not a power of two?
The ALIGN() macro uses bitwise arithmetic and mathematically requires the alignment constraint to be a power of two. If nr_pages is not a power of two, ALIGN() will silently compute an incorrect base address.
Would it be safer to add a check to ensure nr_pages is a power of two when naturally_aligned is requested?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Re: [PATCH v2 06/20] KVM: selftests: Extend page allocator to support naturally aligned allocations
In reply to Sean ChristophersonQuoted 4 lines · Sean Christopherson
Extend the page allocator to support naturally aligned allocations, as some flavors of PowerPC page tables require multiple guest pages per table, and those tables need to be naturally aligned.
That's true.
Quoted 5 lines
Take care to align the potential base address *inside* the retry loop, i.e. the base needs to be (re)aligned on every attempt.
Tested-by Itaru Kitayama <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Thanks for sharing the changes. Looks good to me.
Feel free to add:
Sean Christopherson
maintainerPATCH v2 7/20 KVM: selftests: Make the single-page allocator APIs static inline
In reply to Sean ChristophersonMove the single-page allocator APIs to kvm_util.h as inline functions, as the are trivial wrappers, and unlike __vm_phy_pages_alloc(), are expected to remain that way for the foreseeable future.
No functional change intended.
--- tools/testing/selftests/kvm/include/kvm_util.h | 14 ++++++++++++-- tools/testing/selftests/kvm/lib/kvm_util.c | 11 ----------- 2 files changed, 12 insertions(+), 13 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +12 -2
@@ -990,12 +990,10 @@ void kvm_gsi_routing_write(struct kvm_vm *vm, struct kvm_irq_routing *routing);const char *exit_reason_str (unsigned int exit_reason); -gpa_t vm_phy_page_alloc (struct kvm_vm *vm,gpa_t min_gpa,u32 memslot);gpa_t ____vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,gpa_t min_gpa,u32 memslot,bool protected,bool naturally_aligned);gpa_t __vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,gpa_t min_gpa,u32 memslot,bool protected); -gpa_t vm_alloc_page_table (struct kvm_vm *vm);static inline gpa_t vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,gpa_t min_gpa,u32 memslot)
@@ -1009,6 +1007,18 @@ static inline gpa_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t nr_pages, vm_arch_has_protected_memory(vm)); } +static inline gpa_t vm_phy_page_alloc (struct kvm_vm *vm,gpa_t min_gpa, +u32 memslot) +{ +return vm_phy_pages_alloc (vm,1 , min_gpa, memslot); +} + +static inline gpa_t vm_alloc_page_table (struct kvm_vm *vm) +{ +return vm_phy_page_alloc (vm,KVM_GUEST_PAGE_TABLE_MIN_PADDR , + vm->memslots[MEM_REGION_PT ]); +} +/* * ____vm_create() does KVM_CREATE_VM and little else. __vm_create() also * loads the test binary into guest memory and creates an IRQ chip (x86 only).
tools/testing/selftests/kvm/lib/kvm_util.c +0 -11
@@ -2086,17 +2086,6 @@ gpa_t __vm_phy_pages_alloc(struct kvm_vm *vm, size_t nr_pages, gpa_t min_gpa,return ____vm_phy_pages_alloc (vm, nr_pages, min_gpa, memslot, protected, false); } -gpa_t vm_phy_page_alloc (struct kvm_vm *vm,gpa_t min_gpa,u32 memslot) -{ -return vm_phy_pages_alloc (vm,1 , min_gpa, memslot); -} - -gpa_t vm_alloc_page_table (struct kvm_vm *vm) -{ -return vm_phy_page_alloc (vm,KVM_GUEST_PAGE_TABLE_MIN_PADDR , - vm->memslots[MEM_REGION_PT ]); -} -/* * Address Guest Virtual to Host Virtual *
Signature · 1 line
2.55.0.966.g6673acef38-goog
Re: [PATCH v2 07/20] KVM: selftests: Make the single-page allocator APIs static inline
Quoted 8 lines · Sean Christopherson
Move the single-page allocator APIs to
kvm_util.has inline functions, as the are trivial wrappers, and unlike__vm_phy_pages_alloc(), are expected to remain that way for the foreseeable future.No functional change intended.
Tested-by Itaru Kitayama <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
LGTM. Feel free to add:
Sean Christopherson
maintainerPATCH v2 8/20 KVM: selftests: Use the innermost page allocator API in the memslot perf test
In reply to Sean ChristophersonUse the quad-underscores physical page allocator API in the memslot perf test in anticipation of converting the outer versions to take the memory region type instead of the raw memslot. Stating the obvious, the memslot perf test creates and allocates from multiple memslots, and so can't use MEM_REGION_TEST_DATA to communicate which region/slot to allocate from.
No functional change intended.
--- tools/testing/selftests/kvm/memslot_perf_test.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/memslot_perf_test.c +2 -1
@@ -339,7 +339,8 @@ static bool prepare_vm(struct vm_data *data, int nslots, u64 *maxslots,if (slot == data->nslots) npages += rempages; - gpa =vm_phy_pages_alloc (data->vm, npages, guest_addr, slot); + gpa =____vm_phy_pages_alloc (data->vm, npages, guest_addr, slot, + false, false);TEST_ASSERT (gpa == guest_addr,"vm_phy_pages_alloc() failed" );
Signature · 1 line
2.55.0.966.g6673acef38-goog
Re: [PATCH v2 08/20] KVM: selftests: Use the innermost page allocator API in the memslot perf test
Quoted 8 lines · Sean Christopherson
Use the quad-underscores physical page allocator API in the memslot perf test in anticipation of converting the outer versions to take the memory region type instead of the raw memslot. Stating the obvious, the memslot perf test creates and allocates from multiple memslots, and so can't use
MEM_REGION_TEST_DATAto communicate which region/slot to allocate from.No functional change intended.
Agreed with the reasoning. LGTM.
Please feel free to add:
Re: [PATCH v2 08/20] KVM: selftests: Use the innermost page allocator API in the memslot perf test
In reply to Sean ChristophersonQuoted 8 lines · Sean Christopherson
Use the quad-underscores physical page allocator API in the memslot perf test in anticipation of converting the outer versions to take the memory region type instead of the raw memslot. Stating the obvious, the memslot perf test creates and allocates from multiple memslots, and so can't use
MEM_REGION_TEST_DATAto communicate which region/slot to allocate from.No functional change intended.
yup. Indeed the later APIs will drop the memslot argument from the main/outer page allocator APIs, so we need to use the ____vm_phy_pages_alloc() variant here. The change looks good to me.
Please feel free to add:
Sean Christopherson
maintainerPATCH v2 9/20 KVM: selftests: Use the innermost page allocator API in s390's IRQ routing test
In reply to Sean ChristophersonUse the quad-underscores page allocator in s390's IRQ routing test in anticipation of dropping the @min_gpa param from the primary APIs.
Note, it's not entirely clear that the test *needs* to use a very magical @min_gpa, i.e. it's possible a more elegant solution exists. But that's a problem for a different day.
No functional change intended.
--- tools/testing/selftests/kvm/s390/irq_routing.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kvm/s390/irq_routing.c +1 -1
@@ -36,7 +36,7 @@ static void test(void) }; vm =vm_create_with_one_vcpu (&vcpu, guest_code); - mem =vm_phy_pages_alloc (vm,2 ,4096 *42 ,0 ); + mem =____vm_phy_pages_alloc (vm,2 ,4096 *42 ,0 , false, false); routing =kvm_gsi_routing_create (); routing->nr =1 ;
Signature · 1 line
2.55.0.966.g6673acef38-goog
Janosch Frank
maintainerRe: [PATCH v2 09/20] KVM: selftests: Use the innermost page allocator API in s390's IRQ routing test
Quoted 7 lines · Sean Christopherson
Use the quad-underscores page allocator in s390's IRQ routing test in anticipation of dropping the @min_gpa param from the primary APIs.
Note, it's not entirely clear that the test *needs* to use a very magical @min_gpa, i.e. it's possible a more elegant solution exists. But that's a problem for a different day.
No, it does not need a magic gpa I just wanted to have two gpa consecutive pages.
Quoted 21 lines
No functional change intended.
Tested-by Itaru Kitayama <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- tools/testing/selftests/kvm/s390/irq_routing.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/kvm/s390/irq_routing.c +1
-1@@ -36,7 +36,7 @@ static void test(void) }; vm =vm_create_with_one_vcpu (&vcpu, guest_code); - mem =vm_phy_pages_alloc (vm,2 ,4096 *42 ,0 ); + mem =____vm_phy_pages_alloc (vm,2 ,4096 *42 ,0 , false, false); routing =kvm_gsi_routing_create (); routing->nr =1 ;
Signature · 3 lines
kvm-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kvm-riscv
Re: [PATCH v2 09/20] KVM: selftests: Use the innermost page allocator API in s390's IRQ routing test
In reply to Sean ChristophersonQuoted 11 lines · On Wed, 2 Sep 2026 09:41:12 -0700 Sean Christopherson <[email protected]>…
Use the quad-underscores page allocator in s390's IRQ routing test in anticipation of dropping the @min_gpa param from the primary APIs.
Note, it's not entirely clear that the test *needs* to use a very magical @min_gpa, i.e. it's possible a more elegant solution exists. But that's a problem for a different day.
No functional change intended.
Tested-by Itaru Kitayama <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Quoted 17 lines
--- tools/testing/selftests/kvm/s390/irq_routing.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/kvm/s390/irq_routing.c +1
-1@@ -36,7 +36,7 @@ static void test(void) }; vm =vm_create_with_one_vcpu (&vcpu, guest_code); - mem =vm_phy_pages_alloc (vm,2 ,4096 *42 ,0 ); + mem =____vm_phy_pages_alloc (vm,2 ,4096 *42 ,0 , false, false); routing =kvm_gsi_routing_create (); routing->nr =1 ;
Sean Christopherson
maintainerPATCH v2 10/20 KVM: selftests: Add a wrapper API to allocate multiple page table pages
In reply to Sean ChristophersonAdd vm_alloc_page_table_pages() and use it throughout arch code to replace open coded equivalents.
No functional change intended.
--- tools/testing/selftests/kvm/include/kvm_util.h | 9 +++++++-- tools/testing/selftests/kvm/lib/arm64/processor.c | 4 +--- tools/testing/selftests/kvm/lib/riscv/processor.c | 4 +--- tools/testing/selftests/kvm/lib/s390/processor.c | 8 ++------ 4 files changed, 11 insertions(+), 14 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +7 -2
@@ -1013,10 +1013,15 @@ static inline gpa_t vm_phy_page_alloc(struct kvm_vm *vm, gpa_t min_gpa,return vm_phy_pages_alloc (vm,1 , min_gpa, memslot); } +static inline gpa_t vm_alloc_page_table_pages (struct kvm_vm *vm,size_t nr_pages) +{ +return vm_phy_pages_alloc (vm, nr_pages,KVM_GUEST_PAGE_TABLE_MIN_PADDR , + vm->memslots[MEM_REGION_PT ]); +} +static inline gpa_t vm_alloc_page_table (struct kvm_vm *vm) { -return vm_phy_page_alloc (vm,KVM_GUEST_PAGE_TABLE_MIN_PADDR , - vm->memslots[MEM_REGION_PT ]); +return vm_alloc_page_table_pages (vm,1 ); }/*
tools/testing/selftests/kvm/lib/arm64/processor.c +1 -3
@@ -115,9 +115,7 @@ void virt_arch_pgd_alloc(struct kvm_vm *vm)if (vm->mmu.pgd_created)return ; - vm->mmu.pgd =vm_phy_pages_alloc (vm, nr_pages, -KVM_GUEST_PAGE_TABLE_MIN_PADDR , - vm->memslots[MEM_REGION_PT ]); + vm->mmu.pgd =vm_alloc_page_table_pages (vm, nr_pages); vm->mmu.pgd_created = true; }
tools/testing/selftests/kvm/lib/riscv/processor.c +1 -3
@@ -69,9 +69,7 @@ void virt_arch_pgd_alloc(struct kvm_vm *vm)if (vm->mmu.pgd_created)return ; - vm->mmu.pgd =vm_phy_pages_alloc (vm, nr_pages, -KVM_GUEST_PAGE_TABLE_MIN_PADDR , - vm->memslots[MEM_REGION_PT ]); + vm->mmu.pgd =vm_alloc_page_table_pages (vm, nr_pages); vm->mmu.pgd_created = true; }
tools/testing/selftests/kvm/lib/s390/processor.c +2 -6
@@ -20,9 +20,7 @@ void virt_arch_pgd_alloc(struct kvm_vm *vm)if (vm->mmu.pgd_created)return ; - gpa =vm_phy_pages_alloc (vm,PAGES_PER_REGION , -KVM_GUEST_PAGE_TABLE_MIN_PADDR , - vm->memslots[MEM_REGION_PT ]); + gpa =vm_alloc_page_table_pages (vm,PAGES_PER_REGION );memset (addr_gpa2hva (vm, gpa),0xff ,PAGES_PER_REGION * vm->page_size); vm->mmu.pgd = gpa;
@@ -38,9 +36,7 @@ static u64 virt_alloc_region(struct kvm_vm *vm, int ri) {u64 taddr; - taddr =vm_phy_pages_alloc (vm, ri <4 ?PAGES_PER_REGION :1 , -KVM_GUEST_PAGE_TABLE_MIN_PADDR , - vm->memslots[MEM_REGION_PT ]); + taddr =vm_alloc_page_table_pages (vm, ri <4 ?PAGES_PER_REGION :1 );memset (addr_gpa2hva (vm, taddr),0xff ,PAGES_PER_REGION * vm->page_size);return (taddr ®ION_ENTRY_ORIGIN )
Signature · 1 line
2.55.0.966.g6673acef38-goog
Re: [PATCH v2 10/20] KVM: selftests: Add a wrapper API to allocate multiple page table pages
Quoted 96 lines · Sean Christopherson
Add
vm_alloc_page_table_pages()and use it throughout arch code to replace open coded equivalents.No functional change intended.
Reviewed-by Itaru Kitayama <[email protected]>Tested-by Itaru Kitayama <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- tools/testing/selftests/kvm/include/kvm_util.h | 9 +++++++-- tools/testing/selftests/kvm/lib/arm64/processor.c | 4 +--- tools/testing/selftests/kvm/lib/riscv/processor.c | 4 +--- tools/testing/selftests/kvm/lib/s390/processor.c | 8 ++------ 4 files changed, 11 insertions(+), 14 deletions(-)tools/testing/selftests/kvm/include/kvm_util.h +7
-2@@ -1013,10 +1013,15 @@ static inline gpa_t vm_phy_page_alloc(struct kvm_vm *vm, gpa_t min_gpa,return vm_phy_pages_alloc (vm,1 , min_gpa, memslot); } +static inline gpa_t vm_alloc_page_table_pages (struct kvm_vm *vm,size_t nr_pages) +{ +return vm_phy_pages_alloc (vm, nr_pages,KVM_GUEST_PAGE_TABLE_MIN_PADDR , + vm->memslots[MEM_REGION_PT ]); +} +static inline gpa_t vm_alloc_page_table (struct kvm_vm *vm) { -return vm_phy_page_alloc (vm,KVM_GUEST_PAGE_TABLE_MIN_PADDR , - vm->memslots[MEM_REGION_PT ]); +return vm_alloc_page_table_pages (vm,1 ); }/* tools/testing/selftests/kvm/lib/arm64/processor.c +1
-3@@ -115,9 +115,7 @@ void virt_arch_pgd_alloc(struct kvm_vm *vm)if (vm->mmu.pgd_created)return ; - vm->mmu.pgd =vm_phy_pages_alloc (vm, nr_pages, -KVM_GUEST_PAGE_TABLE_MIN_PADDR , - vm->memslots[MEM_REGION_PT ]); + vm->mmu.pgd =vm_alloc_page_table_pages (vm, nr_pages); vm->mmu.pgd_created = true; }tools/testing/selftests/kvm/lib/riscv/processor.c +1
-3@@ -69,9 +69,7 @@ void virt_arch_pgd_alloc(struct kvm_vm *vm)if (vm->mmu.pgd_created)return ; - vm->mmu.pgd =vm_phy_pages_alloc (vm, nr_pages, -KVM_GUEST_PAGE_TABLE_MIN_PADDR , - vm->memslots[MEM_REGION_PT ]); + vm->mmu.pgd =vm_alloc_page_table_pages (vm, nr_pages); vm->mmu.pgd_created = true; }tools/testing/selftests/kvm/lib/s390/processor.c +2
-6@@ -20,9 +20,7 @@ void virt_arch_pgd_alloc(struct kvm_vm *vm)if (vm->mmu.pgd_created)return ; - gpa =vm_phy_pages_alloc (vm,PAGES_PER_REGION , -KVM_GUEST_PAGE_TABLE_MIN_PADDR , - vm->memslots[MEM_REGION_PT ]); + gpa =vm_alloc_page_table_pages (vm,PAGES_PER_REGION );memset (addr_gpa2hva (vm, gpa),0xff ,PAGES_PER_REGION * vm->page_size); vm->mmu.pgd = gpa;@@ -38,9 +36,7 @@ static u64 virt_alloc_region(struct kvm_vm *vm, int ri) {u64 taddr; - taddr =vm_phy_pages_alloc (vm, ri <4 ?PAGES_PER_REGION :1 , -KVM_GUEST_PAGE_TABLE_MIN_PADDR , - vm->memslots[MEM_REGION_PT ]); + taddr =vm_alloc_page_table_pages (vm, ri <4 ?PAGES_PER_REGION :1 );memset (addr_gpa2hva (vm, taddr),0xff ,PAGES_PER_REGION * vm->page_size);return (taddr ®ION_ENTRY_ORIGIN )Signature · 1 line
2.55.0.966.g6673acef38-goog
Re: [PATCH v2 10/20] KVM: selftests: Add a wrapper API to allocate multiple page table pages
In reply to Sean ChristophersonQuoted 9 lines · Sean Christopherson
Add
vm_alloc_page_table_pages()and use it throughout arch code to replace open coded equivalents.No functional change intended.
Reviewed-by Itaru Kitayama <[email protected]>Tested-by Itaru Kitayama <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
LGTM.
For KVM RISC-V:
Regards,
Anup
Quoted 88 lines
--- tools/testing/selftests/kvm/include/kvm_util.h | 9 +++++++-- tools/testing/selftests/kvm/lib/arm64/processor.c | 4 +--- tools/testing/selftests/kvm/lib/riscv/processor.c | 4 +--- tools/testing/selftests/kvm/lib/s390/processor.c | 8 ++------ 4 files changed, 11 insertions(+), 14 deletions(-)tools/testing/selftests/kvm/include/kvm_util.h +7
-2@@ -1013,10 +1013,15 @@ static inline gpa_t vm_phy_page_alloc(struct kvm_vm *vm, gpa_t min_gpa,return vm_phy_pages_alloc (vm,1 , min_gpa, memslot); } +static inline gpa_t vm_alloc_page_table_pages (struct kvm_vm *vm,size_t nr_pages) +{ +return vm_phy_pages_alloc (vm, nr_pages,KVM_GUEST_PAGE_TABLE_MIN_PADDR , + vm->memslots[MEM_REGION_PT ]); +} +static inline gpa_t vm_alloc_page_table (struct kvm_vm *vm) { -return vm_phy_page_alloc (vm,KVM_GUEST_PAGE_TABLE_MIN_PADDR , - vm->memslots[MEM_REGION_PT ]); +return vm_alloc_page_table_pages (vm,1 ); }/* tools/testing/selftests/kvm/lib/arm64/processor.c +1
-3@@ -115,9 +115,7 @@ void virt_arch_pgd_alloc(struct kvm_vm *vm)if (vm->mmu.pgd_created)return ; - vm->mmu.pgd =vm_phy_pages_alloc (vm, nr_pages, -KVM_GUEST_PAGE_TABLE_MIN_PADDR , - vm->memslots[MEM_REGION_PT ]); + vm->mmu.pgd =vm_alloc_page_table_pages (vm, nr_pages); vm->mmu.pgd_created = true; }tools/testing/selftests/kvm/lib/riscv/processor.c +1
-3@@ -69,9 +69,7 @@ void virt_arch_pgd_alloc(struct kvm_vm *vm)if (vm->mmu.pgd_created)return ; - vm->mmu.pgd =vm_phy_pages_alloc (vm, nr_pages, -KVM_GUEST_PAGE_TABLE_MIN_PADDR , - vm->memslots[MEM_REGION_PT ]); + vm->mmu.pgd =vm_alloc_page_table_pages (vm, nr_pages); vm->mmu.pgd_created = true; }tools/testing/selftests/kvm/lib/s390/processor.c +2
-7@@ -20,9 +20,7 @@ void virt_arch_pgd_alloc(struct kvm_vm *vm)if (vm->mmu.pgd_created)return ; - gpa =vm_phy_pages_alloc (vm,PAGES_PER_REGION , -KVM_GUEST_PAGE_TABLE_MIN_PADDR , - vm->memslots[MEM_REGION_PT ]); + gpa =vm_alloc_page_table_pages (vm,PAGES_PER_REGION );memset (addr_gpa2hva (vm, gpa),0xff ,PAGES_PER_REGION * vm->page_size); vm->mmu.pgd = gpa;@@ -38,9 +36,7 @@ static u64 virt_alloc_region(struct kvm_vm *vm, int ri) {u64 taddr; - taddr =vm_phy_pages_alloc (vm, ri <4 ?PAGES_PER_REGION :1 , -KVM_GUEST_PAGE_TABLE_MIN_PADDR , - vm->memslots[MEM_REGION_PT ]); + taddr =vm_alloc_page_table_pages (vm, ri <4 ?PAGES_PER_REGION :1 );memset (addr_gpa2hva (vm, taddr),0xff ,PAGES_PER_REGION * vm->page_size);return (taddr ®ION_ENTRY_ORIGIN ) --2.55.0.966.g6673acef38-goog
Re: [PATCH v2 10/20] KVM: selftests: Add a wrapper API to allocate multiple page table pages
In reply to Sean ChristophersonQuoted 8 lines · On Wed, 2 Sep 2026 09:41:13 -0700 Sean Christopherson <[email protected]>…
Add
vm_alloc_page_table_pages()and use it throughout arch code to replace open coded equivalents.No functional change intended.
Reviewed-by Itaru Kitayama <[email protected]>Tested-by Itaru Kitayama <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Quoted 85 lines
--- tools/testing/selftests/kvm/include/kvm_util.h | 9 +++++++-- tools/testing/selftests/kvm/lib/arm64/processor.c | 4 +--- tools/testing/selftests/kvm/lib/riscv/processor.c | 4 +--- tools/testing/selftests/kvm/lib/s390/processor.c | 8 ++------ 4 files changed, 11 insertions(+), 14 deletions(-)tools/testing/selftests/kvm/include/kvm_util.h +7
-2@@ -1013,10 +1013,15 @@ static inline gpa_t vm_phy_page_alloc(struct kvm_vm *vm, gpa_t min_gpa,return vm_phy_pages_alloc (vm,1 , min_gpa, memslot); } +static inline gpa_t vm_alloc_page_table_pages (struct kvm_vm *vm,size_t nr_pages) +{ +return vm_phy_pages_alloc (vm, nr_pages,KVM_GUEST_PAGE_TABLE_MIN_PADDR , + vm->memslots[MEM_REGION_PT ]); +} +static inline gpa_t vm_alloc_page_table (struct kvm_vm *vm) { -return vm_phy_page_alloc (vm,KVM_GUEST_PAGE_TABLE_MIN_PADDR , - vm->memslots[MEM_REGION_PT ]); +return vm_alloc_page_table_pages (vm,1 ); }/* tools/testing/selftests/kvm/lib/arm64/processor.c +1
-3@@ -115,9 +115,7 @@ void virt_arch_pgd_alloc(struct kvm_vm *vm)if (vm->mmu.pgd_created)return ; - vm->mmu.pgd =vm_phy_pages_alloc (vm, nr_pages, -KVM_GUEST_PAGE_TABLE_MIN_PADDR , - vm->memslots[MEM_REGION_PT ]); + vm->mmu.pgd =vm_alloc_page_table_pages (vm, nr_pages); vm->mmu.pgd_created = true; }tools/testing/selftests/kvm/lib/riscv/processor.c +1
-3@@ -69,9 +69,7 @@ void virt_arch_pgd_alloc(struct kvm_vm *vm)if (vm->mmu.pgd_created)return ; - vm->mmu.pgd =vm_phy_pages_alloc (vm, nr_pages, -KVM_GUEST_PAGE_TABLE_MIN_PADDR , - vm->memslots[MEM_REGION_PT ]); + vm->mmu.pgd =vm_alloc_page_table_pages (vm, nr_pages); vm->mmu.pgd_created = true; }tools/testing/selftests/kvm/lib/s390/processor.c +2
-6@@ -20,9 +20,7 @@ void virt_arch_pgd_alloc(struct kvm_vm *vm)if (vm->mmu.pgd_created)return ; - gpa =vm_phy_pages_alloc (vm,PAGES_PER_REGION , -KVM_GUEST_PAGE_TABLE_MIN_PADDR , - vm->memslots[MEM_REGION_PT ]); + gpa =vm_alloc_page_table_pages (vm,PAGES_PER_REGION );memset (addr_gpa2hva (vm, gpa),0xff ,PAGES_PER_REGION * vm->page_size); vm->mmu.pgd = gpa;@@ -38,9 +36,7 @@ static u64 virt_alloc_region(struct kvm_vm *vm, int ri) {u64 taddr; - taddr =vm_phy_pages_alloc (vm, ri <4 ?PAGES_PER_REGION :1 , -KVM_GUEST_PAGE_TABLE_MIN_PADDR , - vm->memslots[MEM_REGION_PT ]); + taddr =vm_alloc_page_table_pages (vm, ri <4 ?PAGES_PER_REGION :1 );memset (addr_gpa2hva (vm, taddr),0xff ,PAGES_PER_REGION * vm->page_size);return (taddr ®ION_ENTRY_ORIGIN )
Signature · 3 lines
kvm-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kvm-riscv
Re: [PATCH v2 10/20] KVM: selftests: Add a wrapper API to allocate multiple page table pages
In reply to Sean ChristophersonQuoted 5 lines · Sean Christopherson
Add
vm_alloc_page_table_pages()and use it throughout arch code to replace open coded equivalents.No functional change intended.
Straight forward change. LGTM.
Sean Christopherson
maintainerPATCH v2 11/20 KVM: selftests: Initialize vm->memslots[] with invalid memslots during creation
In reply to Sean ChristophersonInitialize vm->memslots[] with an invalid memslots value during very initial VM creation so that failure to configure the core memory regions generates an immediate assert instead of potential weirdness, e.g. if slot0 is coincidentally valid. This will also allow hardening the region APIs to guard overriding a region's memslot multiple times.
--- tools/testing/selftests/kvm/include/kvm_util.h | 2 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 5 +++++ tools/testing/selftests/kvm/lib/x86/processor.c | 5 ++++- 3 files changed, 11 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/include/kvm_util.h +2 -0
@@ -33,6 +33,8 @@#define NSEC_PER_SEC 1000000000L +#define KVM_INVALID_MEMSLOT UINT_MAX +struct userspace_mem_region {struct kvm_userspace_memory_region2 region;struct sparsebit *unused_phy_pages;
tools/testing/selftests/kvm/lib/kvm_util.c +5 -0
@@ -288,6 +288,7 @@ __weak void vm_populate_gva_bitmap(struct kvm_vm *vm)struct kvm_vm *____vm_create (struct vm_shape shape) {struct kvm_vm *vm; +int i; vm =calloc (1 ,sizeof (*vm));TEST_ASSERT (vm !=NULL ,"Insufficient Memory" );
@@ -296,6 +297,8 @@ struct kvm_vm *____vm_create(struct vm_shape shape) vm->regions.gpa_tree =RB_ROOT ; vm->regions.hva_tree =RB_ROOT ;hash_init (vm->regions.slot_hash); +for (i =0 ; i <NR_MEM_REGIONS ; i++) + vm->memslots[i] =KVM_INVALID_MEMSLOT ; vm->mode = shape.mode; vm->type = shape.type;
@@ -1190,6 +1193,8 @@ memslot2region(struct kvm_vm *vm, u32 memslot) {struct userspace_mem_region *region; +TEST_ASSERT (memslot !=KVM_INVALID_MEMSLOT ,"vm->memslots[] unpopulated?" ); +hash_for_each_possible (vm->regions.slot_hash, region, slot_node, memslot)if (region->region.slot == memslot)
tools/testing/selftests/kvm/lib/x86/processor.c +4 -1
@@ -529,7 +529,10 @@ void tdp_identity_map_default_memslots(struct kvm_vm *vm)/* Only memslot 0 is mapped here, ensure it's the only one being used */ for (s =0 ; s <NR_MEM_REGIONS ; s++) -TEST_ASSERT_EQ (vm->memslots[s],0 ); +TEST_ASSERT (!vm->memslots[s] || + vm->memslots[s] ==KVM_INVALID_MEMSLOT , +"Unhandled memslot '%u' for type '%u'" , + vm->memslots[s], s); i = (region->region.guest_phys_addr >> vm->page_shift) -1 ; last = i + (region->region.memory_size >> vm->page_shift);
Signature · 1 line
2.55.0.966.g6673acef38-goog
Re: [PATCH v2 11/20] KVM: selftests: Initialize vm->memslots[] with invalid memslots during creation
Quoted 76 lines · Sean Christopherson
Initialize vm->memslots[] with an invalid memslots value during very initial VM creation so that failure to configure the core memory regions generates an immediate assert instead of potential weirdness, e.g. if slot0 is coincidentally valid. This will also allow hardening the region APIs to guard overriding a region's memslot multiple times.
Tested-by Itaru Kitayama <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- tools/testing/selftests/kvm/include/kvm_util.h | 2 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 5 +++++ tools/testing/selftests/kvm/lib/x86/processor.c | 5 ++++- 3 files changed, 11 insertions(+), 1 deletion(-)tools/testing/selftests/kvm/include/kvm_util.h +2
-0@@ -33,6 +33,8 @@#define NSEC_PER_SEC 1000000000L +#define KVM_INVALID_MEMSLOT UINT_MAX +struct userspace_mem_region {struct kvm_userspace_memory_region2 region;struct sparsebit *unused_phy_pages;tools/testing/selftests/kvm/lib/kvm_util.c +5
-0@@ -288,6 +288,7 @@ __weak void vm_populate_gva_bitmap(struct kvm_vm *vm)struct kvm_vm *____vm_create (struct vm_shape shape) {struct kvm_vm *vm; +int i; vm =calloc (1 ,sizeof (*vm));TEST_ASSERT (vm !=NULL ,"Insufficient Memory" );@@ -296,6 +297,8 @@ struct kvm_vm *____vm_create(struct vm_shape shape) vm->regions.gpa_tree =RB_ROOT ; vm->regions.hva_tree =RB_ROOT ;hash_init (vm->regions.slot_hash); +for (i =0 ; i <NR_MEM_REGIONS ; i++) + vm->memslots[i] =KVM_INVALID_MEMSLOT ; vm->mode = shape.mode; vm->type = shape.type;@@ -1190,6 +1193,8 @@ memslot2region(struct kvm_vm *vm, u32 memslot) {struct userspace_mem_region *region; +TEST_ASSERT (memslot !=KVM_INVALID_MEMSLOT ,"vm->memslots[] unpopulated?" ); +hash_for_each_possible (vm->regions.slot_hash, region, slot_node, memslot)if (region->region.slot == memslot)tools/testing/selftests/kvm/lib/x86/processor.c +4
-1@@ -529,7 +529,10 @@ void tdp_identity_map_default_memslots(struct kvm_vm *vm)/* Only memslot 0 is mapped here, ensure it's the only one being used */ for (s =0 ; s <NR_MEM_REGIONS ; s++) -TEST_ASSERT_EQ (vm->memslots[s],0 ); +TEST_ASSERT (!vm->memslots[s] || + vm->memslots[s] ==KVM_INVALID_MEMSLOT , +"Unhandled memslot '%u' for type '%u'" , + vm->memslots[s], s); i = (region->region.guest_phys_addr >> vm->page_shift) -1 ; last = i + (region->region.memory_size >> vm->page_shift);Signature · 1 line
2.55.0.966.g6673acef38-goog
Re: [PATCH v2 11/20] KVM: selftests: Initialize vm->memslots[] with invalid memslots during creation
In reply to Sean ChristophersonQuoted 6 lines · Sean Christopherson
Initialize vm->memslots[] with an invalid memslots value during very initial VM creation so that failure to configure the core memory regions generates an immediate assert instead of potential weirdness, e.g. if slot0 is coincidentally valid. This will also allow hardening the region APIs to guard overriding a region's memslot multiple times.
Sure. Looks good overall. Please feel free to add:
Sean Christopherson
maintainerPATCH v2 12/20 KVM: selftests: Add APIs to override memory region types with custom memslots
In reply to Sean ChristophersonAdd 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.
--- .../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(-)
tools/testing/selftests/kvm/arm64/page_fault_test.c +8 -10
@@ -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)
tools/testing/selftests/kvm/include/kvm_util.h +30 -0
@@ -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_memorystatic inline bool vm_arch_has_protected_memory (struct kvm_vm *vm) {
tools/testing/selftests/kvm/lib/kvm_util.c +5 -3
@@ -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);
tools/testing/selftests/kvm/s390/cmma_test.c +6 -13
@@ -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)
Signature · 1 line
2.55.0.966.g6673acef38-goog
Re: [PATCH v2 12/20] KVM: selftests: Add APIs to override memory region types with custom memslots
Quoted 11 lines · On Wed, 2 Sep 2026 09:41:15 -0700 Sean Christopherson <[email protected]>…
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'screate_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.
Quoted 5 lines
Converting tests to overriding memory regions will allow taking the region type instead of the raw memslot in the physical page allocator APIs.
Tested-by Itaru Kitayama <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Quoted 140 lines
--- .../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(-)tools/testing/selftests/kvm/arm64/page_fault_test.c +8
-10@@ -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)tools/testing/selftests/kvm/include/kvm_util.h +30
-0@@ -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_memorystatic inline bool vm_arch_has_protected_memory (struct kvm_vm *vm) {tools/testing/selftests/kvm/lib/kvm_util.c +5
-3@@ -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);tools/testing/selftests/kvm/s390/cmma_test.c +6
-13@@ -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)
Re: [PATCH v2 12/20] KVM: selftests: Add APIs to override memory region types with custom memslots
In reply to Sean ChristophersonQuoted 6 lines · Sean Christopherson
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.
Quoted 8 lines
Deliberately "unroll" the loops in
__vm_create()and the CMMA test'screate_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.
May I suggest few changes in the naming of these APIs:
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_add_mem_region_flags(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_add_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_add_mem_region_flags(vm, type, src_type, gpa, slot, npages, 0);
}Those "_add_" and "_flags" in the function names easily gives away the difference in the APIs, rather than differentiating via "__".
However, logically the change looks good - so feel free to add:
Sean Christopherson
maintainerRe: [PATCH v2 12/20] KVM: selftests: Add APIs to override memory region types with custom memslots
Quoted 44 lines · Ritesh Harjani
Quoted 6 lines · Sean Christopherson
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.
Quoted 8 lines
Deliberately "unroll" the loops in
__vm_create()and the CMMA test'screate_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.
May I suggest few changes in the naming of these APIs:
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_add_mem_region_flags(struct
kvm_vm*vm,
enumkvm_mem_region_typetype, enumvm_mem_backing_src_typesrc_type,gpa_tgpa, 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_add_mem_region(struct
kvm_vm*vm,
enumkvm_mem_region_typetype, enumvm_mem_backing_src_typesrc_type,gpa_tgpa, u32 slot, u64 npages)
Quoted 6 lines
{ vm_override_add_mem_region_flags(vm, type, src_type, gpa, slot, npages, 0); }Those "_add_" and "
_flags" in the function names easily gives away the difference in the APIs, rather than differentiating via "__".
I am strongly against postfixes like "_flags". We do use postfixes for the VM creation APIs, e.g. vm_create_barebones(), vm_create_with_vcpus(), etc., but only because there are so many possible combinations that differentiating through underscores is completely infeasible (unless we forced callers to regurgitate huge amounts of boilerplace and/or had an absurd number of params), and because the collection of APIs is tree-like, as opposed to a single linear chain of APIs.
For this, there is a much more finite set of possibilities, and the set of APIs is a direct liner chain (no underscores => __ => ____).
I agree that not capturing that {,__}vm_override_mem_region() adds a userspace memory region isn't ideal, but due to the term "memory region" already being somewhat overloaded, I don't want to have "add" in the name as I think that will make it harder to differentiate between the "enum kvm_mem_region_type" APIs and the "userspace memory region" APIs.
And IMO it's totally fine for a function name to express what the API does at a higher level, without capturing the exact operations in explicit detail. E.g. __vm_create() (and even ____vm_create()) obviously does a lot more than literally KVM_VM_CREATE.
Re: [PATCH v2 12/20] KVM: selftests: Add APIs to override memory region types with custom memslots
Quoted 1 line · Sean Christopherson
On Thu, Sep 10, 2026, Ritesh Harjani wrote:
[...]
Quoted 50 lines
Quoted 27 lines
May I suggest few changes in the naming of these APIs:
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_add_mem_region_flags(struct
kvm_vm*vm,
enumkvm_mem_region_typetype, enumvm_mem_backing_src_typesrc_type,gpa_tgpa, 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_add_mem_region(struct
kvm_vm*vm,
enumkvm_mem_region_typetype, enumvm_mem_backing_src_typesrc_type,gpa_tgpa, u32 slot, u64 npages)Quoted 6 lines
{ vm_override_add_mem_region_flags(vm, type, src_type, gpa, slot, npages, 0); }Those "_add_" and "
_flags" in the function names easily gives away the difference in the APIs, rather than differentiating via "__".I am strongly against postfixes like "
_flags". We do use postfixes for the VM creation APIs, e.g.vm_create_barebones(),vm_create_with_vcpus(), etc., but only because there are so many possible combinations that differentiating through underscores is completely infeasible (unless we forced callers to regurgitate huge amounts of boilerplace and/or had an absurd number of params), and because the collection of APIs is tree-like, as opposed to a single linear chain of APIs.For this, there is a much more finite set of possibilities, and the set of APIs is a direct liner chain (no underscores => __ => ____).
I agree that not capturing that {,__}vm_override_mem_region() adds a userspace memory region isn't ideal, but due to the term "memory region" already being somewhat overloaded, I don't want to have "add" in the name as I think that will make it harder to differentiate between the "enum
kvm_mem_region_type" APIs and the "userspace memory region" APIs.
Quoted 4 lines
And IMO it's totally fine for a function name to express what the API does at a higher level, without capturing the exact operations in explicit detail. E.g.
__vm_create()(and even____vm_create()) obviously does a lot more than literallyKVM_VM_CREATE.
As I have been looking at the direct liner chain of vm_create() for some time, I agree with Sean. To create with a non-default guest mode ID, we have to start with those underscored functions, but I still prefer short function names.
Thanks,
Itaru.
Signature · 3 lines
kvm-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kvm-riscv
Sean Christopherson
maintainerPATCH v2 13/20 KVM: selftests: Add TEST_EXTRA memory region type for "special" memslots
In reply to Sean ChristophersonAnd another memory region type to deal with extra, one-off memory regions, and use the new type to manage x86's SMRAM memslot, as another step towards taking the region type instead of the raw memslot in the physical page allocator APIs.
Alternatively, SMRAM setup could simply use the quad-underscore API to continue passing in the memslot, but a surprising number of tests use an "extra" memslot for a variety of reasons. I.e. allocating memory from one (and exactly one) extra memslot isn't all that rare, and so should be treated as normal behavior, not as something extraordinary, as quad-underscore functions typically suggest.
Opportunistically add comments to document the intended usage of the types, as the difference between DATA, TEST_DATA, and TEST_EXTRA in particular isn't exactly obvious.
--- .../testing/selftests/kvm/include/kvm_util.h | 39 ++++++++++++++++--- tools/testing/selftests/kvm/include/x86/smm.h | 2 +- .../testing/selftests/kvm/lib/x86/processor.c | 7 ++-- 3 files changed, 39 insertions(+), 9 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +34 -5
@@ -82,11 +82,45 @@ struct userspace_mem_regions {DECLARE_HASHTABLE (slot_hash,9 ); }; +/* +* Memory region types are passed to various page allocators to communicate +* various properties and metadata related to the allocation. Note, the +* descriptions below described the primary usage of each type. Individual +* tests may allocate memory for other purposes. +* +* By default, all regions except TEST_EXTRA are mapped to memslot '0'. The +* TEXT_EXTRA region is left unmapped as it's intended to be used only for test +* specific allocations, i.e. should never be used by core/infrastructure code. +* Tests can override the memslot for any or all types, e.g. so that all test +* data is allocated from a curated memslot. +*/ enum kvm_mem_region_type { +/* +* The CODE region is used by lib/elf when loading the test's code into +* guest memory. +*/ MEM_REGION_CODE , +/* +* The DATA region is used to allocate core data structures, e.g. vCPU +* stacks, VM exception tables, x86's TSS, etc. +*/ MEM_REGION_DATA , +/* +* The PT region, a.k.a. Page Table region, is used to allocate page +* table pages. +*/ MEM_REGION_PT , +/* +* The TEST_DATA region is used for allocating test data that is either +* test specific, and/or isn't considered a "core" data structure. +*/ MEM_REGION_TEST_DATA , +/* +* The TEST_EXTRA region is for special snowflakes, where a test wants +* to create and use a one-off memslot, without impacting "normal" test +* data allocations. +*/ +MEM_REGION_TEST_EXTRA ,NR_MEM_REGIONS , };
@@ -129,11 +163,6 @@ struct kvm_vm {struct kvm_binary_stats stats; -/* -* KVM region slots. These are the default memslots used by page -* allocators, e.g., lib/elf uses the memslots[MEM_REGION_CODE] -* memslot. -*/ u32 memslots[NR_MEM_REGIONS ]; };
tools/testing/selftests/kvm/include/x86/smm.h +1 -1
@@ -8,7 +8,7 @@#define SMRAM_MEMSLOT ((1 << 16) | 1)#define SMRAM_PAGES (SMRAM_SIZE / PAGE_SIZE) -void setup_smram (struct kvm_vm *vm,struct kvm_vcpu *vcpu,u64 smram_gpa, +void setup_smram (struct kvm_vm *vm,struct kvm_vcpu *vcpu,gpa_t smram_gpa,const void *smi_handler,size_t handler_size);void inject_smi (struct kvm_vcpu *vcpu);
tools/testing/selftests/kvm/lib/x86/processor.c +4 -3
@@ -1468,11 +1468,12 @@ bool kvm_arch_has_default_irqchip(void)return true; } -void setup_smram (struct kvm_vm *vm,struct kvm_vcpu *vcpu,u64 smram_gpa, +void setup_smram (struct kvm_vm *vm,struct kvm_vcpu *vcpu,gpa_t smram_gpa,const void *smi_handler,size_t handler_size) { -vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS , smram_gpa, -SMRAM_MEMSLOT ,SMRAM_PAGES ,0 ); +vm_override_mem_region (vm,MEM_REGION_TEST_EXTRA ,VM_MEM_SRC_ANONYMOUS , + smram_gpa,SMRAM_MEMSLOT ,SMRAM_PAGES ); +TEST_ASSERT (vm_phy_pages_alloc (vm,SMRAM_PAGES , smram_gpa,SMRAM_MEMSLOT ) == smram_gpa,"Could not allocate guest physical addresses for SMRAM" );
Signature · 1 line
2.55.0.966.g6673acef38-goog
[email protected]
AI reviewRe: [PATCH v2 13/20] KVM: selftests: Add TEST_EXTRA memory region type for "special" memslots
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Typo in the documentation comment for memory region types ('TEXT_EXTRA' instead of 'TEST_EXTRA'). --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Typo in the documentation comment for memory region types ('TEXT_EXTRA' instead of 'TEST_EXTRA'). --
commit 72083c9d1df29879d635314499b7e5b9848895ef
Author: Sean Christopherson <[email protected]>
KVM: selftests: Add TEST_EXTRA memory region type for "special" memslots
And another memory region type to deal with extra, one-off memory regions, and use the new type to manage x86's SMRAM memslot, as another step towards taking the region type instead of the raw memslot in the physical page allocator APIs.
Quoted 6 lines
tools/testing/selftests/kvm/include/kvm_util.h +0
-0@@ -82,11 +82,45 @@ struct userspace_mem_regions {DECLARE_HASHTABLE (slot_hash,9 );
tools/testing/selftests/kvm/include/kvm_util.h struct userspace_mem_regions { · line 92
Quoted 10 lines
tools/testing/selftests/kvm/include/kvm_util.h +8
-0}; +/* +* Memory region types are passed to various page allocators to communicate +* various properties and metadata related to the allocation. Note, the +* descriptions below described the primary usage of each type. Individual +* tests may allocate memory for other purposes. +* +* By default, all regions except TEST_EXTRA are mapped to memslot '0'. The +* TEXT_EXTRA region is left unmapped as it's intended to be used only for test
^^^^^^^^^^
[Severity: Low]
Is there a typo here ("TEXT_EXTRA" instead of "TEST_EXTRA")?
Quoted 5 lines
tools/testing/selftests/kvm/include/kvm_util.h +4
-0+* specific allocations, i.e. should never be used by core/infrastructure code. +* Tests can override the memslot for any or all types, e.g. so that all test +* data is allocated from a curated memslot. +*/ enum kvm_mem_region_type {
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
Sean Christopherson
maintainerPATCH v2 14/20 KVM: selftests: Use TEST_EXTRA region in arm64's vGIC LPI stress test
In reply to Sean ChristophersonUse the TEST_EXTRA region/memslot for arm64's vGIC LPI stress test instead of a custom memslot, so that the primary vm_phy_pages_alloc() APIs can be reworked to take a region type instead of a raw memslot.
For all intents and purposes, no functional change intended, as vm_override_mem_region() is barely more than a wrapper for vm_userspace_mem_region_add().
--- tools/testing/selftests/kvm/arm64/vgic_lpi_stress.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/arm64/vgic_lpi_stress.c +2 -2
@@ -164,8 +164,8 @@ static void setup_memslot(void) pages = sz / vm->page_size; gpa_base = ((vm_compute_max_gfn (vm) +1 ) * vm->page_size) - sz; -vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS , gpa_base, -TEST_MEMSLOT_INDEX , pages,0 ); +vm_override_mem_region (vm,MEM_REGION_TEST_EXTRA ,VM_MEM_SRC_ANONYMOUS , + gpa_base,TEST_MEMSLOT_INDEX , pages); }#define LPI_PROP_DEFAULT_PRIO 0xa0
Signature · 1 line
2.55.0.966.g6673acef38-goog
Sean Christopherson
maintainerPATCH v2 15/20 KVM: selftests: Use TEST_EXTRA region in x86's smaller MAXPHYADDR test
In reply to Sean ChristophersonUse the TEST_EXTRA region/memslot for x86's smaller MAXPHYADDR test instead of a custom memslot, so that the primary vm_phy_pages_alloc() APIs can be reworked to take a region type instead of a raw memslot.
For all intents and purposes, no functional change intended, as vm_add_extra_mem_region() is barely more than a wrapper for vm_userspace_mem_region_add().
--- .../selftests/kvm/x86/smaller_maxphyaddr_emulation_test.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/x86/smaller_maxphyaddr_emulation_test.c +4 -3
@@ -62,9 +62,10 @@ int main(int argc, char *argv[])TEST_ASSERT (rc,"KVM_CAP_EXIT_ON_EMULATION_FAILURE is unavailable" );vm_enable_cap (vm,KVM_CAP_EXIT_ON_EMULATION_FAILURE ,1 ); -vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS , -MEM_REGION_GPA ,MEM_REGION_SLOT , -MEM_REGION_SIZE /PAGE_SIZE ,0 ); +vm_override_mem_region (vm,MEM_REGION_TEST_EXTRA ,VM_MEM_SRC_ANONYMOUS , +MEM_REGION_GPA ,MEM_REGION_SLOT , +MEM_REGION_SIZE /PAGE_SIZE ); + gpa =vm_phy_pages_alloc (vm,MEM_REGION_SIZE /PAGE_SIZE ,MEM_REGION_GPA ,MEM_REGION_SLOT );TEST_ASSERT (gpa ==MEM_REGION_GPA ,"Failed vm_phy_pages_alloc" );
Signature · 1 line
2.55.0.966.g6673acef38-goog
Sean Christopherson
maintainerPATCH v2 16/20 KVM: selftests: Use TEST_EXTRA region in set memory region test
In reply to Sean ChristophersonUse the TEST_EXTRA region/memslot in the "set memory region" test instead of a custom memslot, so that the primary vm_phy_pages_alloc() APIs can be reworked to take a region type instead of a raw memslot.
For all intents and purposes, no functional change intended, as vm_override_mem_region() is barely more than a wrapper for vm_userspace_mem_region_add().
--- tools/testing/selftests/kvm/set_memory_region_test.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/set_memory_region_test.c +3 -3
@@ -116,9 +116,9 @@ static struct kvm_vm *spawn_vm(struct kvm_vcpu **vcpu, pthread_t *vcpu_thread, vm =vm_create_with_one_vcpu (vcpu, guest_code); -vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS_THP , -MEM_REGION_GPA ,MEM_REGION_SLOT , -MEM_REGION_SIZE /getpagesize (),0 ); +vm_override_mem_region (vm,MEM_REGION_TEST_EXTRA ,VM_MEM_SRC_ANONYMOUS_THP , +MEM_REGION_GPA ,MEM_REGION_SLOT , +MEM_REGION_SIZE /getpagesize ());/* * Allocate and map two pages so that the GPA accessed by guest_code()
Signature · 1 line
2.55.0.966.g6673acef38-goog
Sean Christopherson
maintainerPATCH v2 17/20 KVM: selftests: Take the memory region type, not memslot, in page allocators
In reply to Sean ChristophersonTake the memory region type instead of the memslot in all page allocator APIs, except for the innermost quad-underscores function. This will allow automatically selecting the minimum GPA based on the allocation type, which can't be done using the memslot due to all regions sharing a single memslot by default. PowerPC support will also leverage the type to ensure that page table allocations are naturally aligned.
No functional change intended.
--- tools/testing/selftests/kvm/arm64/vgic_lpi_stress.c | 12 ++++++------ tools/testing/selftests/kvm/include/kvm_util.h | 13 +++++++------ tools/testing/selftests/kvm/lib/kvm_util.c | 10 +++++++--- tools/testing/selftests/kvm/lib/x86/processor.c | 2 +- .../testing/selftests/kvm/set_memory_region_test.c | 2 +- .../kvm/x86/smaller_maxphyaddr_emulation_test.c | 2 +- 6 files changed, 23 insertions(+), 18 deletions(-)
tools/testing/selftests/kvm/arm64/vgic_lpi_stress.c +6 -6
@@ -192,27 +192,27 @@ static void setup_test_data(void) test_data.device_table =vm_phy_pages_alloc (vm, pages_per_64k, gpa_base, -TEST_MEMSLOT_INDEX ); +MEM_REGION_TEST_EXTRA ); test_data.collection_table =vm_phy_pages_alloc (vm, pages_per_64k, gpa_base, -TEST_MEMSLOT_INDEX ); +MEM_REGION_TEST_EXTRA ); cmdq_base =vm_phy_pages_alloc (vm, pages_per_64k, gpa_base, -TEST_MEMSLOT_INDEX ); +MEM_REGION_TEST_EXTRA );virt_map (vm, cmdq_base, cmdq_base, pages_per_64k); test_data.cmdq_base = cmdq_base; test_data.cmdq_base_va = (void *)cmdq_base; test_data.itt_tables =vm_phy_pages_alloc (vm, pages_per_64k * nr_devices, - gpa_base,TEST_MEMSLOT_INDEX ); + gpa_base,MEM_REGION_TEST_EXTRA ); test_data.lpi_prop_table =vm_phy_pages_alloc (vm, pages_per_64k, - gpa_base,TEST_MEMSLOT_INDEX ); + gpa_base,MEM_REGION_TEST_EXTRA );configure_lpis (); test_data.lpi_pend_tables =vm_phy_pages_alloc (vm, pages_per_64k * nr_cpus, - gpa_base,TEST_MEMSLOT_INDEX ); + gpa_base,MEM_REGION_TEST_EXTRA );sync_global_to_guest (vm, test_data); }
tools/testing/selftests/kvm/include/kvm_util.h +7 -6
@@ -1054,30 +1054,31 @@ const char *exit_reason_str(unsigned int exit_reason);gpa_t ____vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,gpa_t min_gpa,u32 memslot,bool protected,bool naturally_aligned);gpa_t __vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,gpa_t min_gpa, -u32 memslot,bool protected); +enum kvm_mem_region_type type,bool protected);static inline gpa_t vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages, -gpa_t min_gpa,u32 memslot) +gpa_t min_gpa, +enum kvm_mem_region_type type) {/* * By default, allocate memory as protected for VMs that support * protected memory, as the majority of memory for such VMs is * protected, i.e. using shared memory is effectively opt-in. */ -return __vm_phy_pages_alloc (vm, nr_pages, min_gpa, memslot, +return __vm_phy_pages_alloc (vm, nr_pages, min_gpa, type,vm_arch_has_protected_memory (vm)); }static inline gpa_t vm_phy_page_alloc (struct kvm_vm *vm,gpa_t min_gpa, -u32 memslot) +enum kvm_mem_region_type type) { -return vm_phy_pages_alloc (vm,1 , min_gpa, memslot); +return vm_phy_pages_alloc (vm,1 , min_gpa, type); }static inline gpa_t vm_alloc_page_table_pages (struct kvm_vm *vm,size_t nr_pages) {return vm_phy_pages_alloc (vm, nr_pages,KVM_GUEST_PAGE_TABLE_MIN_PADDR , - vm->memslots[MEM_REGION_PT ]); +MEM_REGION_PT ); }static inline gpa_t vm_alloc_page_table (struct kvm_vm *vm)
tools/testing/selftests/kvm/lib/kvm_util.c +7 -3
@@ -1475,7 +1475,7 @@ static gva_t ____vm_alloc(struct kvm_vm *vm, size_t sz, gva_t min_gva,virt_pgd_alloc (vm);gpa_t gpa =__vm_phy_pages_alloc (vm, pages,KVM_UTIL_MIN_PFN * vm->page_size, - vm->memslots[type], protected); + type, protected);/* * Find an unused range of virtual page addresses of at least
@@ -2088,9 +2088,13 @@ gpa_t ____vm_phy_pages_alloc(struct kvm_vm *vm, size_t nr_pages, gpa_t min_gpa, }gpa_t __vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,gpa_t min_gpa, -u32 memslot,bool protected) +enum kvm_mem_region_type type,bool protected) { -return ____vm_phy_pages_alloc (vm, nr_pages, min_gpa, memslot, protected, false); +TEST_ASSERT (type <NR_MEM_REGIONS , +"Invalid memory region type '%u'" , type); + +return ____vm_phy_pages_alloc (vm, nr_pages, min_gpa, vm->memslots[type], + protected, false); }/*
tools/testing/selftests/kvm/lib/x86/processor.c +1 -1
@@ -1475,7 +1475,7 @@ void setup_smram(struct kvm_vm *vm, struct kvm_vcpu *vcpu, gpa_t smram_gpa, smram_gpa, SMRAM_MEMSLOT, SMRAM_PAGES);TEST_ASSERT (vm_phy_pages_alloc (vm,SMRAM_PAGES , smram_gpa, -SMRAM_MEMSLOT ) == smram_gpa, +MEM_REGION_TEST_EXTRA ) == smram_gpa,"Could not allocate guest physical addresses for SMRAM" );memset (addr_gpa2hva (vm, smram_gpa),0x0 ,SMRAM_SIZE );
tools/testing/selftests/kvm/set_memory_region_test.c +1 -1
@@ -124,7 +124,7 @@ static struct kvm_vm *spawn_vm(struct kvm_vcpu **vcpu, pthread_t *vcpu_thread,* Allocate and map two pages so that the GPA accessed by guest_code() * stays valid across the memslot move. */ - gpa =vm_phy_pages_alloc (vm,2 ,MEM_REGION_GPA ,MEM_REGION_SLOT ); + gpa =vm_phy_pages_alloc (vm,2 ,MEM_REGION_GPA ,MEM_REGION_TEST_EXTRA );TEST_ASSERT (gpa ==MEM_REGION_GPA ,"Failed vm_phy_pages_alloc\n" );virt_map (vm,MEM_REGION_GPA ,MEM_REGION_GPA ,2 );
tools/testing/selftests/kvm/x86/smaller_maxphyaddr_emulation_test.c +1 -1
@@ -67,7 +67,7 @@ int main(int argc, char *argv[]) MEM_REGION_SIZE / PAGE_SIZE); gpa =vm_phy_pages_alloc (vm,MEM_REGION_SIZE /PAGE_SIZE , -MEM_REGION_GPA ,MEM_REGION_SLOT ); +MEM_REGION_GPA ,MEM_REGION_TEST_EXTRA );TEST_ASSERT (gpa ==MEM_REGION_GPA ,"Failed vm_phy_pages_alloc" );virt_map (vm,MEM_REGION_GVA ,MEM_REGION_GPA ,1 ); hva =addr_gpa2hva (vm,MEM_REGION_GPA );
Signature · 1 line
2.55.0.966.g6673acef38-goog
Re: [PATCH v2 17/20] KVM: selftests: Take the memory region type, not memslot, in page allocators
Quoted 7 lines · Sean Christopherson
Take the memory region type instead of the memslot in all page allocator APIs, except for the innermost quad-underscores function. This will allow automatically selecting the minimum GPA based on the allocation type, which can't be done using the memslot due to all regions sharing a single memslot by default. PowerPC support will also leverage the type to ensure that page table allocations are naturally aligned.
yup, PowerPC can leverage this.
Quoted 2 lines
No functional change intended.
Sure, even though the callers are changed to pass type instead of slot, the inner page allocator is moved to pass vm->memslots[type] to ____vm_phy_pages_alloc(). So no functional change.
LGTM.
Sean Christopherson
maintainerPATCH v2 18/20 KVM: selftests: Use TEST_ASSERT(), not assert(), in vm_get_mem_region()
In reply to Sean ChristophersonUse a proper selftests TEST_ASSERT() in vm_get_mem_region() so that bad input is guaranteed to fail the test, and with the standard error message format.
--- tools/testing/selftests/kvm/include/kvm_util.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/include/kvm_util.h +2 -1
@@ -200,7 +200,8 @@ memslot2region(struct kvm_vm *vm, u32 memslot);static inline struct userspace_mem_region *vm_get_mem_region (struct kvm_vm *vm,enum kvm_mem_region_type type) { -assert (type <NR_MEM_REGIONS ); +TEST_ASSERT (type <NR_MEM_REGIONS , +"Invalid memory region type '%u'" , type);return memslot2region (vm, vm->memslots[type]); }
Signature · 1 line
2.55.0.966.g6673acef38-goog
Re: [PATCH v2 18/20] KVM: selftests: Use TEST_ASSERT(), not assert(), in vm_get_mem_region()
Quoted 4 lines · Sean Christopherson
Use a proper selftests
TEST_ASSERT()invm_get_mem_region()so that bad input is guaranteed to fail the test, and with the standard error message format.
LGTM. Feel free to add:
Sean Christopherson
maintainerPATCH v2 19/20 KVM: selftests: Automatically pick min_gpa for allocations based on region type
In reply to Sean ChristophersonAutomatically choose the minimum GPA for physical page allocations based on the region type instead of sprinkling the logic over various wrappers and tests. All usage falls into three categories: (a) don't care, just use the bare minimum GPA, (b) page tables, use a slightly higher min to keep low memory available, (c) custom memslot, use the base of the memslot. I.e. there isn't a strong need to allow completely custom minimums.
Opportunistically move the vGIC LPI stress test's "gpa_base" variable into setup_memslot() as the only reason it was a global variable was so that it could be passed as the minimum GPA for allocations from the extra memslot.
--- .../selftests/kvm/arm64/vgic_lpi_stress.c | 13 +++---- .../testing/selftests/kvm/include/kvm_util.h | 12 +++--- tools/testing/selftests/kvm/lib/kvm_util.c | 38 ++++++++++++++++--- .../testing/selftests/kvm/lib/x86/processor.c | 3 +- .../selftests/kvm/set_memory_region_test.c | 2 +- .../x86/smaller_maxphyaddr_emulation_test.c | 2 +- 6 files changed, 45 insertions(+), 25 deletions(-)
tools/testing/selftests/kvm/arm64/vgic_lpi_stress.c +5 -8
@@ -23,7 +23,6 @@#define GIC_LPI_OFFSET 8192static size_t nr_iterations =1000 ; -static gpa_t gpa_base;static struct kvm_vm *vm;static struct kvm_vcpu **vcpus;
@@ -143,6 +142,7 @@ static void guest_code(size_t nr_lpis)static void setup_memslot (void ) { +gpa_t gpa_base;size_t pages;size_t sz;
@@ -191,28 +191,25 @@ static void setup_test_data(void)gpa_t cmdq_base; test_data.device_table =vm_phy_pages_alloc (vm, pages_per_64k, - gpa_base,MEM_REGION_TEST_EXTRA ); test_data.collection_table =vm_phy_pages_alloc (vm, pages_per_64k, - gpa_base,MEM_REGION_TEST_EXTRA ); - cmdq_base =vm_phy_pages_alloc (vm, pages_per_64k, gpa_base, -MEM_REGION_TEST_EXTRA ); + cmdq_base =vm_phy_pages_alloc (vm, pages_per_64k,MEM_REGION_TEST_EXTRA );virt_map (vm, cmdq_base, cmdq_base, pages_per_64k); test_data.cmdq_base = cmdq_base; test_data.cmdq_base_va = (void *)cmdq_base; test_data.itt_tables =vm_phy_pages_alloc (vm, pages_per_64k * nr_devices, - gpa_base,MEM_REGION_TEST_EXTRA ); +MEM_REGION_TEST_EXTRA ); test_data.lpi_prop_table =vm_phy_pages_alloc (vm, pages_per_64k, - gpa_base,MEM_REGION_TEST_EXTRA ); +MEM_REGION_TEST_EXTRA );configure_lpis (); test_data.lpi_pend_tables =vm_phy_pages_alloc (vm, pages_per_64k * nr_cpus, - gpa_base,MEM_REGION_TEST_EXTRA ); +MEM_REGION_TEST_EXTRA );sync_global_to_guest (vm, test_data); }
tools/testing/selftests/kvm/include/kvm_util.h +5 -7
@@ -1054,11 +1054,10 @@ const char *exit_reason_str(unsigned int exit_reason);gpa_t ____vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,gpa_t min_gpa,u32 memslot,bool protected,bool naturally_aligned); -gpa_t __vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,gpa_t min_gpa, +gpa_t __vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,enum kvm_mem_region_type type,bool protected);static inline gpa_t vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages, -gpa_t min_gpa,enum kvm_mem_region_type type) {/*
@@ -1066,20 +1065,19 @@ static inline gpa_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t nr_pages,* protected memory, as the majority of memory for such VMs is * protected, i.e. using shared memory is effectively opt-in. */ -return __vm_phy_pages_alloc (vm, nr_pages, min_gpa, type, +return __vm_phy_pages_alloc (vm, nr_pages, type,vm_arch_has_protected_memory (vm)); } -static inline gpa_t vm_phy_page_alloc (struct kvm_vm *vm,gpa_t min_gpa, +static inline gpa_t vm_phy_page_alloc (struct kvm_vm *vm,enum kvm_mem_region_type type) { -return vm_phy_pages_alloc (vm,1 , min_gpa, type); +return vm_phy_pages_alloc (vm,1 , type); }static inline gpa_t vm_alloc_page_table_pages (struct kvm_vm *vm,size_t nr_pages) { -return vm_phy_pages_alloc (vm, nr_pages,KVM_GUEST_PAGE_TABLE_MIN_PADDR , -MEM_REGION_PT ); +return vm_phy_pages_alloc (vm, nr_pages,MEM_REGION_PT ); }static inline gpa_t vm_alloc_page_table (struct kvm_vm *vm)
tools/testing/selftests/kvm/lib/kvm_util.c +32 -6
@@ -1473,9 +1473,7 @@ static gva_t ____vm_alloc(struct kvm_vm *vm, size_t sz, gva_t min_gva,u64 pages = (sz >> vm->page_shift) + ((sz % vm->page_size) !=0 );virt_pgd_alloc (vm); -gpa_t gpa =__vm_phy_pages_alloc (vm, pages, -KVM_UTIL_MIN_PFN * vm->page_size, - type, protected); +gpa_t gpa =__vm_phy_pages_alloc (vm, pages, type, protected);/* * Find an unused range of virtual page addresses of at least
@@ -2087,11 +2085,39 @@ gpa_t ____vm_phy_pages_alloc(struct kvm_vm *vm, size_t nr_pages, gpa_t min_gpa,__builtin_unreachable (); } -gpa_t __vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,gpa_t min_gpa, +gpa_t __vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,enum kvm_mem_region_type type,bool protected) { -TEST_ASSERT (type <NR_MEM_REGIONS , -"Invalid memory region type '%u'" , type); +struct userspace_mem_region *region =vm_get_mem_region (vm, type); +gpa_t min_gpa; + +TEST_ASSERT (region,"No region for type '%u', memslot '%u'" , + type, vm->memslots[type]); + +switch (type) { +case MEM_REGION_CODE : +case MEM_REGION_DATA : +case MEM_REGION_TEST_DATA : +/* +* If the region is backed by the default memslot (id=0), use +* selftests' hardcoded minimum PFN, otherwise use the base of +* the custom memory slot that backs the region. +*/ +if (!vm->memslots[type]) + min_gpa =KVM_UTIL_MIN_PFN * vm->page_size; +else + min_gpa = region->region.guest_phys_addr; +break ; +case MEM_REGION_PT : + min_gpa =KVM_GUEST_PAGE_TABLE_MIN_PADDR ; +break ; +case MEM_REGION_TEST_EXTRA : + min_gpa = region->region.guest_phys_addr; +break ; +default : +TEST_FAIL ("Invalid memory region type '%u'" , type); +break ; + }return ____vm_phy_pages_alloc (vm, nr_pages, min_gpa, vm->memslots[type], protected, false);
tools/testing/selftests/kvm/lib/x86/processor.c +1 -2
@@ -1474,8 +1474,7 @@ void setup_smram(struct kvm_vm *vm, struct kvm_vcpu *vcpu, gpa_t smram_gpa,vm_override_mem_region (vm,MEM_REGION_TEST_EXTRA ,VM_MEM_SRC_ANONYMOUS , smram_gpa,SMRAM_MEMSLOT ,SMRAM_PAGES ); -TEST_ASSERT (vm_phy_pages_alloc (vm,SMRAM_PAGES , smram_gpa, -MEM_REGION_TEST_EXTRA ) == smram_gpa, +TEST_ASSERT (vm_phy_pages_alloc (vm,SMRAM_PAGES ,MEM_REGION_TEST_EXTRA ) == smram_gpa,"Could not allocate guest physical addresses for SMRAM" );memset (addr_gpa2hva (vm, smram_gpa),0x0 ,SMRAM_SIZE );
tools/testing/selftests/kvm/set_memory_region_test.c +1 -1
@@ -124,7 +124,7 @@ static struct kvm_vm *spawn_vm(struct kvm_vcpu **vcpu, pthread_t *vcpu_thread,* Allocate and map two pages so that the GPA accessed by guest_code() * stays valid across the memslot move. */ - gpa =vm_phy_pages_alloc (vm,2 ,MEM_REGION_GPA ,MEM_REGION_TEST_EXTRA ); + gpa =vm_phy_pages_alloc (vm,2 ,MEM_REGION_TEST_EXTRA );TEST_ASSERT (gpa ==MEM_REGION_GPA ,"Failed vm_phy_pages_alloc\n" );virt_map (vm,MEM_REGION_GPA ,MEM_REGION_GPA ,2 );
tools/testing/selftests/kvm/x86/smaller_maxphyaddr_emulation_test.c +1 -1
@@ -67,7 +67,7 @@ int main(int argc, char *argv[]) MEM_REGION_SIZE / PAGE_SIZE); gpa =vm_phy_pages_alloc (vm,MEM_REGION_SIZE /PAGE_SIZE , -MEM_REGION_GPA ,MEM_REGION_TEST_EXTRA ); +MEM_REGION_TEST_EXTRA );TEST_ASSERT (gpa ==MEM_REGION_GPA ,"Failed vm_phy_pages_alloc" );virt_map (vm,MEM_REGION_GVA ,MEM_REGION_GPA ,1 ); hva =addr_gpa2hva (vm,MEM_REGION_GPA );
Signature · 1 line
2.55.0.966.g6673acef38-goog
Sean Christopherson
maintainerPATCH v2 20/20 KVM: selftests: Add arch hook to force page tables to be naturally aligned
In reply to Sean ChristophersonAdd an arch hook to communicate to the core physical page allocator that page table allocations need to be naturally aligned. The hook will be used for upcoming PowerPC support, which has MMU tables that are larger than the guest base page size and need to be naturally aligned.
Bury the hook in the physical page allocator to avoid having to plumb a rarely used param into the main APIs.
--- tools/testing/selftests/kvm/include/kvm_util.h | 2 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/include/kvm_util.h +2 -0
@@ -1052,6 +1052,8 @@ void kvm_gsi_routing_write(struct kvm_vm *vm, struct kvm_irq_routing *routing);const char *exit_reason_str (unsigned int exit_reason); +bool kvm_arch_needs_naturally_aligned_page_tables (void ); +gpa_t ____vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,gpa_t min_gpa,u32 memslot,bool protected,bool naturally_aligned); gpa_t __vm_phy_pages_alloc(struct kvm_vm *vm, size_t nr_pages,
tools/testing/selftests/kvm/lib/kvm_util.c +8 -1
@@ -2085,10 +2085,16 @@ gpa_t ____vm_phy_pages_alloc(struct kvm_vm *vm, size_t nr_pages, gpa_t min_gpa,__builtin_unreachable (); } +__weak bool kvm_arch_needs_naturally_aligned_page_tables (void ) +{ +return false; +} +gpa_t __vm_phy_pages_alloc (struct kvm_vm *vm,size_t nr_pages,enum kvm_mem_region_type type,bool protected) {struct userspace_mem_region *region =vm_get_mem_region (vm, type); +bool naturally_aligned = false;gpa_t min_gpa; TEST_ASSERT(region, "No region for type '%u', memslot '%u'",
@@ -2110,6 +2116,7 @@ gpa_t __vm_phy_pages_alloc(struct kvm_vm *vm, size_t nr_pages,break ;case MEM_REGION_PT : min_gpa =KVM_GUEST_PAGE_TABLE_MIN_PADDR ; + naturally_aligned =kvm_arch_needs_naturally_aligned_page_tables ();break ;case MEM_REGION_TEST_EXTRA : min_gpa = region->region.guest_phys_addr;
@@ -2120,7 +2127,7 @@ gpa_t __vm_phy_pages_alloc(struct kvm_vm *vm, size_t nr_pages, }return ____vm_phy_pages_alloc (vm, nr_pages, min_gpa, vm->memslots[type], - protected, false); + protected, naturally_aligned); }/*
Signature · 1 line
2.55.0.966.g6673acef38-goog
[email protected]
AI reviewRe: [PATCH v2 20/20] KVM: selftests: Add arch hook to force page tables to be naturally aligned
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Using the ALIGN() macro with a potentially non-power-of-2 alignment value leads to incorrect…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Using the ALIGN() macro with a potentially non-power-of-2 alignment value leads to incorrect alignment calculations and silent memory corruption. --
commit ff7ec91da1b3e52d1f5f08d02d9a0f7729040242
Author: Sean Christopherson <[email protected]>
KVM: selftests: Add arch hook to force page tables to be naturally aligned
Add an arch hook to communicate to the core physical page allocator that page table allocations need to be naturally aligned. The hook will be used for upcoming PowerPC support, which has MMU tables that are larger than the guest base page size and need to be naturally aligned.
Quoted 4 lines
tools/testing/selftests/kvm/lib/kvm_util.c +0
-0
[ ... ]
tools/testing/selftests/kvm/lib/kvm_util.c gpa_t __vm_phy_pages_alloc(struct kvm_vm *vm, size_t nr_pages, · line 2130
Quoted 6 lines
tools/testing/selftests/kvm/lib/kvm_util.c +1
-1@@ -2120,7 +2127,7 @@ gpa_t __vm_phy_pages_alloc(struct kvm_vm *vm, size_t nr_pages, }return ____vm_phy_pages_alloc (vm, nr_pages, min_gpa, vm->memslots[type], - protected, false); + protected, naturally_aligned);
[Severity: Medium]
When naturally_aligned is true, ____vm_phy_pages_alloc() sets the alignment to nr_pages:
size_t alignment = naturally_aligned ? nr_pages : 1; ... base = ALIGN(base, alignment);
Since ALIGN(x, a) mathematically requires the alignment 'a' to be a power of 2, could this result in an incorrect base physical address calculation if a caller requests a page table allocation with a non-power-of-2 nr_pages? This might silently fail to provide the requested alignment and lead to overlapping allocations or hardware MMU faults.
Quoted 1 line
tools/testing/selftests/kvm/lib/kvm_util.c +0
-0}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=20
Re: [PATCH v2 20/20] KVM: selftests: Add arch hook to force page tables to be naturally aligned
In reply to Sean ChristophersonQuoted 5 lines · Sean Christopherson
Add an arch hook to communicate to the core physical page allocator that page table allocations need to be naturally aligned. The hook will be used for upcoming PowerPC support, which has MMU tables that are larger than the guest base page size and need to be naturally aligned.
Thanks Sean, for these patches. I have addressed your review comments on v3 of PPC KVM selftests support and ported those on top of this series. I am still in the process of running the entire series on a real HW (and some todos/minor changes to be done).
Will soon post v4 of PPC KVM selftests on top of this change series.
Quoted 4 lines
Bury the hook in the physical page allocator to avoid having to plumb a rarely used param into the main APIs.
Cc Ritesh Harjani (IBM) <[email protected]>
As for this change. LGTM. Feel free to add:
Quoted 8 lines · Sean Christopherson
This is a big pile of cleanups and refactoring to prepare for PPC support in KVM selftests. PPC has paging modes that require multi-page page tables, and those page tables need to be naturally aligned. The original proposal was to plumb in an @align param to the page allocators[*], but I didn't love the idea of adding another rarely used param to the allocators.
v2:
- Collect tags. [Itaru, Bibo]
I ran kvm_page_table_test for arm64 again it went ok on HW.
Thanks,
Itaru.
Quoted 71 lines
- Use
0x180000as the min GPA for page table pages on LoongArch, instead of changing every other arch to use0x200000. [Sashiko, Bibo]
- Opportunistically move the vGIC LPI stress test's global "gpa_base" variable intosetup_memslot(). [Sashiko]v1: https://lore.kernel.org/all/[email protected]
Nicholas Piggin (1):
KVM: selftests: Use goto instead of do-while to retry finding unused
physical pagesSean Christopherson (19):
KVM: selftests: UseMEM_REGION_PTmemslot instead of '0' for s390
regions/segments
KVM: selftests: Use the common minimum GPA for page tables on
LoongArch
KVM: selftests: Usevm_alloc_page_table()to allocate LoongArch page
tables
KVM: selftests: Rename "num" param to "nr_pages" for physical page
allocators
KVM: selftests: Extend page allocator to support naturally aligned
allocations
KVM: selftests: Make the single-page allocator APIs static inline
KVM: selftests: Use the innermost page allocator API in the memslot
perf test
KVM: selftests: Use the innermost page allocator API in s390's IRQ
routing test
KVM: selftests: Add a wrapper API to allocate multiple page table
pages
KVM: selftests: Initialize vm->memslots[] with invalid memslots during
creation
KVM: selftests: Add APIs to override memory region types with custom
memslots
KVM: selftests: AddTEST_EXTRAmemory region type for "special"
memslots
KVM: selftests: UseTEST_EXTRAregion in arm64's vGIC LPI stress test
KVM: selftests: UseTEST_EXTRAregion in x86's smaller MAXPHYADDR test
KVM: selftests: UseTEST_EXTRAregion in set memory region test
KVM: selftests: Take the memory region type, not memslot, in page
allocators
KVM: selftests: UseTEST_ASSERT(), notassert(), invm_get_mem_region()KVM: selftests: Automatically pick
min_gpafor allocations based on
region type
KVM: selftests: Add arch hook to force page tables to be naturally
aligned.../selftests/kvm/arm64/page_fault_test.c | 18 ++- .../selftests/kvm/arm64/vgic_lpi_stress.c | 21 ++- .../testing/selftests/kvm/include/kvm_util.h | 106 +++++++++++++-- tools/testing/selftests/kvm/include/x86/smm.h | 2 +- .../selftests/kvm/lib/arm64/processor.c | 4 +- tools/testing/selftests/kvm/lib/kvm_util.c | 127 +++++++++++------- .../selftests/kvm/lib/loongarch/processor.c | 5 +- .../selftests/kvm/lib/riscv/processor.c | 4 +- .../selftests/kvm/lib/s390/processor.c | 7 +- .../testing/selftests/kvm/lib/x86/processor.c | 15 ++- .../testing/selftests/kvm/memslot_perf_test.c | 3 +- tools/testing/selftests/kvm/s390/cmma_test.c | 19 +-- .../testing/selftests/kvm/s390/irq_routing.c | 2 +- .../selftests/kvm/set_memory_region_test.c | 8 +- .../x86/smaller_maxphyaddr_emulation_test.c | 9 +- 15 files changed, 221 insertions(+), 129 deletions(-)base-commit:
76671054f9a1ff6abb976583cd8da37650acdc97Signature · 1 line
2.55.0.966.g6673acef38-goog
Sean Christopherson
maintainerQuoted 6 lines · Sean Christopherson
This is a big pile of cleanups and refactoring to prepare for PPC support in KVM selftests. PPC has paging modes that require multi-page page tables, and those page tables need to be naturally aligned. The original proposal was to plumb in an @align param to the page allocators[*], but I didn't love the idea of adding another rarely used param to the allocators.
Quoted 8 lines
v2:
- Collect tags. [Itaru, Bibo]
- Use0x180000as the min GPA for page table pages on LoongArch, instead of changing every other arch to use0x200000. [Sashiko, Bibo]
- Opportunistically move the vGIC LPI stress test's global "gpa_base" variable intosetup_memslot(). [Sashiko][...]
Applied to kvm-x86 selftests, thanks!
[01/20] KVM: selftests: Use MEM_REGION_PT memslot instead of '0' for s390 regions/segments
https://github.com/kvm-x86/linux/commit/73ac07325851 [02/20] KVM: selftests: Use the common minimum GPA for page tables on LoongArch https://github.com/kvm-x86/linux/commit/9886413e0466 [03/20] KVM: selftests: Use vm_alloc_page_table() to allocate LoongArch page tables https://github.com/kvm-x86/linux/commit/befd94805906 [04/20] KVM: selftests: Rename "num" param to "nr_pages" for physical page allocators https://github.com/kvm-x86/linux/commit/d220e33516fa [05/20] KVM: selftests: Use goto instead of do-while to retry finding unused physical pages https://github.com/kvm-x86/linux/commit/2c0555c51e7c [06/20] KVM: selftests: Extend page allocator to support naturally aligned allocations https://github.com/kvm-x86/linux/commit/2429ecf7ccd5 [07/20] KVM: selftests: Make the single-page allocator APIs static inline https://github.com/kvm-x86/linux/commit/54e83f729385 [08/20] KVM: selftests: Use the innermost page allocator API in the memslot perf test https://github.com/kvm-x86/linux/commit/b8d9e6ec747a [09/20] KVM: selftests: Use the innermost page allocator API in s390's IRQ routing test https://github.com/kvm-x86/linux/commit/9c0061d2e7cd [10/20] KVM: selftests: Add a wrapper API to allocate multiple page table pages https://github.com/kvm-x86/linux/commit/7f477ba0878e [11/20] KVM: selftests: Initialize vm->memslots[] with invalid memslots during creation https://github.com/kvm-x86/linux/commit/9e4e2281df12 [12/20] KVM: selftests: Add APIs to override memory region types with custom memslots https://github.com/kvm-x86/linux/commit/d69b3e714265 [13/20] KVM: selftests: Add TEST_EXTRA memory region type for "special" memslots https://github.com/kvm-x86/linux/commit/9f035aca8765 [14/20] KVM: selftests: Use TEST_EXTRA region in arm64's vGIC LPI stress test https://github.com/kvm-x86/linux/commit/5d81014d3868 [15/20] KVM: selftests: Use TEST_EXTRA region in x86's smaller MAXPHYADDR test https://github.com/kvm-x86/linux/commit/00c37bfc5b4e [16/20] KVM: selftests: Use TEST_EXTRA region in set memory region test https://github.com/kvm-x86/linux/commit/5ed16822bc45 [17/20] KVM: selftests: Take the memory region type, not memslot, in page allocators https://github.com/kvm-x86/linux/commit/36a062568ea8 [18/20] KVM: selftests: Use TEST_ASSERT(), not assert(), in vm_get_mem_region() https://github.com/kvm-x86/linux/commit/1856064afc9d [19/20] KVM: selftests: Automatically pick min_gpa for allocations based on region type https://github.com/kvm-x86/linux/commit/6c28a0d7cf12 [20/20] KVM: selftests: Add arch hook to force page tables to be naturally aligned https://github.com/kvm-x86/linux/commit/1e59e5a3bdbe
Quoted 75 lines · Sean Christopherson
This is a big pile of cleanups and refactoring to prepare for PPC support in KVM selftests. PPC has paging modes that require multi-page page tables, and those page tables need to be naturally aligned. The original proposal was to plumb in an @align param to the page allocators[*], but I didn't love the idea of adding another rarely used param to the allocators.
v2:
- Collect tags. [Itaru, Bibo]
- Use0x180000as the min GPA for page table pages on LoongArch, instead ofchanging every other arch to use 0x200000. [Sashiko, Bibo]- Opportunistically move the vGIC LPI stress test's global "
gpa_base" variable intosetup_memslot(). [Sashiko]v1: https://lore.kernel.org/all/[email protected]
Nicholas Piggin (1):
KVM: selftests: Use goto instead of do-while to retry finding unused
physical pagesSean Christopherson (19):
KVM: selftests: UseMEM_REGION_PTmemslot instead of '0' for s390
regions/segments
KVM: selftests: Use the common minimum GPA for page tables on
LoongArch
KVM: selftests: Usevm_alloc_page_table()to allocate LoongArch page
tables
KVM: selftests: Rename "num" param to "nr_pages" for physical page
allocators
KVM: selftests: Extend page allocator to support naturally aligned
allocations
KVM: selftests: Make the single-page allocator APIs static inline
KVM: selftests: Use the innermost page allocator API in the memslot
perf test
KVM: selftests: Use the innermost page allocator API in s390's IRQ
routing test
KVM: selftests: Add a wrapper API to allocate multiple page table
pages
KVM: selftests: Initialize vm->memslots[] with invalid memslots during
creation
KVM: selftests: Add APIs to override memory region types with custom
memslots
KVM: selftests: AddTEST_EXTRAmemory region type for "special"
memslots
KVM: selftests: UseTEST_EXTRAregion in arm64's vGIC LPI stress test
KVM: selftests: UseTEST_EXTRAregion in x86's smaller MAXPHYADDR test
KVM: selftests: UseTEST_EXTRAregion in set memory region test
KVM: selftests: Take the memory region type, not memslot, in page
allocators
KVM: selftests: UseTEST_ASSERT(), notassert(), invm_get_mem_region()KVM: selftests: Automatically pick
min_gpafor allocations based on
region type
KVM: selftests: Add arch hook to force page tables to be naturally
aligned.../selftests/kvm/arm64/page_fault_test.c | 18 ++- .../selftests/kvm/arm64/vgic_lpi_stress.c | 21 ++- .../testing/selftests/kvm/include/kvm_util.h | 106 +++++++++++++-- tools/testing/selftests/kvm/include/x86/smm.h | 2 +- .../selftests/kvm/lib/arm64/processor.c | 4 +- tools/testing/selftests/kvm/lib/kvm_util.c | 127 +++++++++++------- .../selftests/kvm/lib/loongarch/processor.c | 5 +- .../selftests/kvm/lib/riscv/processor.c | 4 +- .../selftests/kvm/lib/s390/processor.c | 7 +- .../testing/selftests/kvm/lib/x86/processor.c | 15 ++- .../testing/selftests/kvm/memslot_perf_test.c | 3 +- tools/testing/selftests/kvm/s390/cmma_test.c | 19 +-- .../testing/selftests/kvm/s390/irq_routing.c | 2 +- .../selftests/kvm/set_memory_region_test.c | 8 +- .../x86/smaller_maxphyaddr_emulation_test.c | 9 +- 15 files changed, 221 insertions(+), 129 deletions(-)
Quoted 1 line
base-commit:
76671054f9a1ff6abb976583cd8da37650acdc97
Hi Sean,
Thanks for working on this patch series. I tested this series (v2) as a base for Ritesh's PowerPC KVM selftest series (v5) [1] on two real PowerPC(ppc64le) PowerVM & PowerNV systems. Your patches applied cleanly and the core infrastructure changes worked correctly on PowerPC — exactly the foundation Ritesh's series needed.
Ritesh's patch series : https://lore.kernel.org/linuxppc-dev/[email protected]/
Full test suite summary (make run_tests):
-----------------------------------------
LPAR bare-metal demand_paging_test PASS PASS dirty_log_test PASS PASS dirty_log_perf_test PASS PASS guest_print_test PASS PASS hardware_disable_test PASS PASS kvm_binary_stats_test PASS PASS kvm_create_max_vcpus PASS PASS kvm_page_table_test PASS PASS set_memory_region_test PASS PASS memslot_modification_stress_test PASS PASS memslot_perf_test PASS PASS access_tracking_perf_test PASS PASS irqfd_test SKIP SKIP vm_types_test SKIP SKIP
--------------------------------------------------
Total 12 PASS 12 PASS 2 SKIP 2 SKIP
My Detailed testing analysis for your patches on PowerPC (ppc64le):
-------------------------------------------------
Patches 1-5 [Page allocator and memslot refactoring]
Core page allocator and memslot infrastructure exercised by all 14 tests on both systems — no regressions observed.
Patches 6-10 [Extended page allocator APIs]
Exercised via kvm_create_max_vcpus and memslot_perf_test on both systems — APIs work correctly on PowerPC.
Patches 11-13, 16 [Memslot initialization and TEST_EXTRA region] set_memory_region_test and memslot_modification_stress_test ran successfully on both systems — memslot init, TEST_EXTRA region, and its usage in set_memory_region_test all working correctly.
Patch 17 [Memory region type API]
Exercised by set_memory_region_test — PASS on both systems.
Patch 18 [Use TEST_ASSERT() in kvm_util.c] Exercised by all tests — no assertion failures observed.
Patch 19 [Auto-pick min_gpa for PowerPC]
This is the critical patch for PPC — guest physical test memory was correctly placed above 256M on both systems:
LPAR: guest physical test memory: [0xfffffbfff0000, 0xfffffffff0000)
bare-metal: guest physical test memory: [0xfffffbfff0000, 0xfffffffff0000)
Patch 20 [Add arch hook to force page table setup] lib/powerpc/processor.c correctly hooks into this — guest page tables set up correctly on both systems.
Note: Patches 14 and 15 (arm64/x86-specific TEST_EXTRA usage) were not exercised as those tests don't build on PowerPC — as expected.
Please feel free to add :
Thank you,
Anushree Mathur