From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.197]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 692894AD7E0 for ; Wed, 2 Sep 2026 16:41:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.197 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788367306; cv=none; b=EaTCNhTIDVn4hWwQMqLhcLztsVVecFCakRvldHIsVZgCEflopKSww87oElqsxpz6WPq57vxvhLZ/RQg9vQ+NIQgPiT1Fd1qCq86LPOvrKIWpxt61yUoa2AeyVBlbvBqFBNgvtI8SdX6PFfBCWloIKJOHhIS6KPIzJoplJwn1/wE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788367306; c=relaxed/simple; bh=IooTrDugbi730db2Qdv2j1/bfHJilOastYDRCxIfQLA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PkQAYbjXBFjYWYFmdpzvBBncKVBNDtc5vtiMrnHumeuT+HpDh/30G7ACoXlIZ7DaB4q36UP0hFhCVbl0yFqiRIpZr0rRdpP2wXNxG6UNKAzFNLR7K0wjpb9gmF8Z1MnExS1Z4Eij+i/BxUQ+Ptvs35Sd8j5TU6Bpkj/JVKuHhyg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=vBpTxVYW; arc=none smtp.client-ip=209.85.210.197 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vBpTxVYW" Received: by mail-pf1-f197.google.com with SMTP id d2e1a72fcca58-8535cdc2433so37844b3a.0 for ; Wed, 02 Sep 2026 09:41:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1788367299; x=1788972099; darn=lists.linux.dev; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=OWxwiZu3iI1BKMOXY9Ac60kPARcqOSTNs6vsc+7jhyU=; b=vBpTxVYWSYiVIdGTptou7qoH6A0Kmm3cWrwqTpkD18Ipk+bjwEEex7eyh15w5PT6bj W/00mlgOYi2/V1F2FMyYhq8YinYBbkC8iJ93UxbjzV+xCGBLLDwXPKOLQiP1Nbs1vuOE eoAOcyo4oWQ9OX0kB3xjKO2FbQGHi8lgZAREvrzIYe9E2mF5vYyDnmR/psYyXI72iyfh CE0bsHNtCQe/uxhG0sVKkhXIYfJxaV6SEVEoxJoj4UHhXWTIfDI0lAiDz6bmC1F9qwhJ kthSqNUH2r03ao0u7lOyvf3Qho4eUbHpAI0h9lZe/wl7jehdCopjwZI6UuumuFZtkXaE flZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788367299; x=1788972099; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=OWxwiZu3iI1BKMOXY9Ac60kPARcqOSTNs6vsc+7jhyU=; b=g5MCzsz9im9I51TxdlI7gUrGbbOkYFkvCWL/uJKtS07QhlAunfGILq03LjQxFFWDkB 3TAq53QWgaTWl0sKC7Tyq68FeJ8TbSEedETq+nJRgfY0luSRvx5m6XTH1aCkXxYT2Rzi DOp2FUY80vlZuJFr/fwirVw5pT5y0W1JsMmrPE9f93avNaohEb4df5a74debqxirGgcs QN4GGOE+CcvAzusZqk2mqyaXsXu5XWlIaOEhgCfBJPjuK13hBSiv0XBIiErwhS6B9He6 VuuVZUCCJnYMIM15El7XrBQkFunELV3yoKdRv/D7TA3ikT8BR5O6NQwWtp1feehpsQa0 ng2A== X-Forwarded-Encrypted: i=1; AKwUvBzgIbd5zyXG8BldvwWBc7pV91uvUdarxuJR+JgzprS+xNTcFtarXskzA5Ln8+kVAhdlZRRPf9fDhYg=@lists.linux.dev X-Gm-Message-State: AFuF++lk51Q0ZxjWi8gmD1P/GMzLGV/51su3iLqD+3KAW1gSn9uP+pQi xS2dV82BIrVq2XyfJZhPh791Tl2xUvYB3j2CZapIaAC6N1BfkiRD+PzIq6gCSpzSmQh36tjII0i ELWu2PA== X-Received: from pfbbm14.prod.google.com ([2002:a05:6a00:320e:b0:84c:2e88:693d]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3d55:b0:84e:cd14:aeba with SMTP id d2e1a72fcca58-85ffbe960f2mr374778b3a.6.1788367298091; Wed, 02 Sep 2026 09:41:38 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 2 Sep 2026 09:41:14 -0700 In-Reply-To: <20260902164123.2546762-1-seanjc@google.com> Precedence: bulk X-Mailing-List: loongarch@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260902164123.2546762-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.966.g6673acef38-goog Message-ID: <20260902164123.2546762-12-seanjc@google.com> Subject: [PATCH v2 11/20] KVM: selftests: Initialize vm->memslots[] with invalid memslots during creation From: Sean Christopherson To: Marc Zyngier , Oliver Upton , Sean Christopherson , Paolo Bonzini , Tianrui Zhao , Bibo Mao , Huacai Chen , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda Cc: Fuad Tabba , Joey Gouly , Steffen Eiden , Suzuki K Poulose , Zenghui Yu , Atish Patra , Alexandre Ghiti , David Hildenbrand , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm@vger.kernel.org, loongarch@lists.linux.dev, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Itaru Kitayama , Nicholas Piggin , Ritesh Harjani Content-Type: text/plain; charset="UTF-8" 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 Signed-off-by: Sean Christopherson --- 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 c56f342b16fd..304ef6af3268 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.966.g6673acef38-goog