[PATCH 11/20] KVM: selftests: Initialize vm->memslots[] with invalid memslots during creation

Gautam Menghani gautam at linux.ibm.com
Wed Sep 2 10:52:55 PDT 2026


On Wed, Aug 26, 2026 at 04:05:02PM -0700, Sean Christopherson wrote:
> 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.
> 
> Signed-off-by: Sean Christopherson <seanjc at google.com>
> ---
>  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(-)
> 
> diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
> index 21354434de0a..70e77992dd2f 100644
> --- a/tools/testing/selftests/kvm/include/kvm_util.h
> +++ b/tools/testing/selftests/kvm/include/kvm_util.h
> @@ -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;
> diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
> index ce0d8fba4624..56df5b1d9c79 100644
> --- a/tools/testing/selftests/kvm/lib/kvm_util.c
> +++ b/tools/testing/selftests/kvm/lib/kvm_util.c
> @@ -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)
> diff --git a/tools/testing/selftests/kvm/lib/x86/processor.c b/tools/testing/selftests/kvm/lib/x86/processor.c
> index d31fa81ea075..ea5fa59888af 100644
> --- a/tools/testing/selftests/kvm/lib/x86/processor.c
> +++ b/tools/testing/selftests/kvm/lib/x86/processor.c
> @@ -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);
> -- 
> 2.55.0.887.g758fc8c411-goog
> 

Acked-by: Gautam Menghani <gautam at linux.ibm.com>



More information about the linux-arm-kernel mailing list