From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) (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 9F95B4AD7FF for ; Wed, 2 Sep 2026 16:41:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.71 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788367310; cv=none; b=oL0HUiXEnFmYuN1FAbyeYgTckccp7EHETJ0+VOk183rU+8lEB0rNss6tCvl2R+vqxL6yWTGXdsCAxfhWDyFJWvI1mJC+Dpw7BPTS5IUmEmY1Y8GYoaSuf8AgDKCZ9s7ddWlx/aMzt4CzohApqX9ErU303ElAzK5MpHnKQcnEDkI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788367310; c=relaxed/simple; bh=wQtFJanLpI1WY9EHCIE3kdyq790UPQV+51nbUuKlkgo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Cs1OXZmes0UU+Eo7umPV+wDb+IBtoWEMKTLQxM2Eexupq7qLN6yHBhbRYEYDOe/VZGdg/yJhySIYEPHW3tg39+qyUAaGJ3UmzhG3D9fSf5kP1h8zEXsVWFvF+VnnNo/O8q0/D7XDigxQAw0h2rjwINLIbh3iI4CjfYqEFoRWQXI= 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=t1U8Dc/G; arc=none smtp.client-ip=209.85.216.71 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="t1U8Dc/G" Received: by mail-pj1-f71.google.com with SMTP id 98e67ed59e1d1-38ea32e57e2so2605417a91.1 for ; Wed, 02 Sep 2026 09:41:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1788367301; x=1788972101; darn=vger.kernel.org; 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=SeJnYNm/30DDrGtlzlyhX4F8Ff+qodk8muwORsydLHQ=; b=t1U8Dc/G+a6bcBEWuu9tJabeORMoUJlsSNVJ6epE85jFvSTQmTo46TtZKTYPkz55hd FuqEvqhHGLT8d6V6KQmcpOr0XuKaqMIRp0vjtf0yToWhqvGRhaF+y0kenquEvN6/6omg eXmue+8MqRexG2MaFWPDw3CIyzV/X8w05hawzZa54XwZNNt0ItVG8pRD+n415qUZXurY pVlo3GM5z08F5ze2znQooiFBhjgwlsYDsEWDrsYUd7bXiL+iq63xs7nitJAgmN/KJgXZ pn/SrTQ8Ipko8iqgaAijIPT+NMcK/mH3LwGDrMmr1UX7X/zMf3xLXI7nZf4Av18FV36Y 689A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788367301; x=1788972101; 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=SeJnYNm/30DDrGtlzlyhX4F8Ff+qodk8muwORsydLHQ=; b=lGKWuL6j7P7sD+WHwzQAdk/8JLA+c3N+A2TvfdVPMK2bLV/+kFyew8AD9ar+8ZcFBY eSQT5nBj7RwV0eBTgj/pl3NhC9AvGdylHvaakLrrrFP2esNQro792CraZmU6rmGTwwny IZPWUM17bclbGEDhi2HjrsB0LZdlq8/36n8k9sFa2O37VwCMtzgj8KS+g0wYmEQAHLHX SutDofPgUHZM/mwQMZS13Szy/etBSHOL9cBhS/bQ8UqASeJRD4eoh2U+8KaGn9uOxxHl EFgxY1EGmCUfru7wuamSFXxupuplXQxvfKyl+ennaxGzjTcMgj69kP6LF0szr5eDx3We +1rg== X-Forwarded-Encrypted: i=1; AKwUvBynD+T2gm0QAlsal+icvRbIeRWRvGlVZ/EHKX2W59cgbcSvaymGu44KMFi0zX5gDYcV2wU=@vger.kernel.org X-Gm-Message-State: AFuF++lBdkIatYzzuIKAmcrphpLKls8tdm0kCnEk9PXWZqc+awO38UhX 8mzg2y44ioqv61fGKpfwOMZbc9eryHBPwJGTyFXtTpxycfV7UJEcvFfHjDQ0jS1VgDneJ7EXwsD xXNVE1g== X-Received: from pjan2.prod.google.com ([2002:a17:90a:1602:b0:398:9c5e:d8f2]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4ac9:b0:398:9bd5:490c with SMTP id 98e67ed59e1d1-39aee0b498dmr7421235a91.19.1788367300525; Wed, 02 Sep 2026 09:41:40 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 2 Sep 2026 09:41:16 -0700 In-Reply-To: <20260902164123.2546762-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org 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-14-seanjc@google.com> Subject: [PATCH v2 13/20] KVM: selftests: Add TEST_EXTRA memory region type for "special" memslots 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" 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. 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. Tested-by: Itaru Kitayama Signed-off-by: Sean Christopherson --- .../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(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index c8faa8a02b93..b6b96d6978b2 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -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]; }; diff --git a/tools/testing/selftests/kvm/include/x86/smm.h b/tools/testing/selftests/kvm/include/x86/smm.h index 2d1afa09819b..15faaa060126 100644 --- a/tools/testing/selftests/kvm/include/x86/smm.h +++ b/tools/testing/selftests/kvm/include/x86/smm.h @@ -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); diff --git a/tools/testing/selftests/kvm/lib/x86/processor.c b/tools/testing/selftests/kvm/lib/x86/processor.c index ea5fa59888af..b988eea373ad 100644 --- a/tools/testing/selftests/kvm/lib/x86/processor.c +++ b/tools/testing/selftests/kvm/lib/x86/processor.c @@ -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"); -- 2.55.0.966.g6673acef38-goog