From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f198.google.com (mail-pl1-f198.google.com [209.85.214.198]) (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 A84E2535FB5 for ; Thu, 1 Oct 2026 19:38:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.198 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790883538; cv=none; b=OqVcnHGvsVFAwnvqiAl+SXCPgSImuiuaRRZn4lfz1V7DeKVkdtYu2EiTEa4IbQLLYSXQj1T2rw3axjbzpdMmXHv1JY4vYrTk9jDEjJ+Z1U8+ViKnwEd03CoLo80CysYTLtiyXTYrpEZ8siYSavmTKe2h4VKFjW8YUrqReYuyiXA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790883538; c=relaxed/simple; bh=sZyyJs/KlSsWRD9sQg89mVg98goUj8UDv1HtfnjzwME=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fIZrXZm81DD1HP1hB9lUjbl8cw/zt32YwVT/7Aqbsi1qRZIODBJoRylclajhAzOEXTrrj5XLFZMFWrjsDnOeIdx5G6hnQofVbS7bYl0oVGGtXPy3Dlt6p8Ep53C9OhA+wH5hZ3NZWIzgXlfOs7dXWub24OFhjU96udBWtbKG3mo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--wyihan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=MCHzQag/; arc=none smtp.client-ip=209.85.214.198 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--wyihan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="MCHzQag/" Received: by mail-pl1-f198.google.com with SMTP id d9443c01a7336-2df8484f12cso65782535ad.3 for ; Thu, 01 Oct 2026 12:38:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790883534; x=1791488334; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=Q+jGeioH3xBzsz+B9rJvX4YPTjLXl5pfC6kAHkC+4bE=; b=MCHzQag/DZVbzWEby/qDBxcuBcoAc+tVE5Qm+pYfupJlwaatAMkrEgEprKe2dKaNPZ 6lV3hbXwKwqCwSytgMkNb28AoxPVbPvGUv/LsHDLn5wUWW3mTJHhJ0eVpf1d0mabm4gt FsdlZk96RdB4LgQ98m1QCgE8dKKlIOUxbtBTZwNBDnNvxdZj5dshU3xgi0R5yYxoy3Sr 74/d5KI84XIE6Z/yr2MtXQRmR+EjulpdOjK3IcdpQvjKoSGELeMKX5yPZMqwdHv8l7rS fooy4Rm26MuVD1FEspq2a6aOB9r1ByKq4eSUsU72DC/ooPN7Zwx3n8A23pC3J7iBvXq9 7Psg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790883534; x=1791488334; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Q+jGeioH3xBzsz+B9rJvX4YPTjLXl5pfC6kAHkC+4bE=; b=wKvKBjI4akWfgAJDytmAmEe+gqlu/z9JSzmj0YG25xfefxRcmT1OsFqCYnI8KIE0Tf 9B50lqkBf0VGmh4cc1AZ8lduruuQC8sFto7QH9oqmCZ/Il0NYKANgrwZhJuk2AmQpHIf D9BGGdN2DVvFAlJzg+L5voBW5llnmEBnDTMiFlOqhR1Aeqh7EicAaBH/GXPMQhEOxypW RZ/FH7o37Bavstu6W4ft+5V4m2jff4OpXt3Z6yAi/rYifOWzAav4zVPfOX25rWdny4x4 ekV+GAEiMhWaIppYShb85qZHW5W2KeGPcMm3laQWz+voKMrhVy1kZcgldnPoU52BRKnH HYTw== X-Forwarded-Encrypted: i=1; AKwUvByc1B8U2JLSip/oc12eCLzJ4dB/DXxiGHm/C/rJq5d+VLX1L22X7iXF1yxapCKWSoFR9xiIyI/mOyXZkIA=@vger.kernel.org X-Gm-Message-State: AFq9FYIwGnQhQ11c/W9P93U3gFE3g6aLSSwWLfafTmc7qT+jM//CFjeu M7tCeCbVPTBUiQOdEMCmeOllLF+hVHLjhUNBUIZtZHWF1roDIHqNn4Blm7ixfkhVGEgoDkdE9uk TeQwtBw== X-Received: from plap1.prod.google.com ([2002:a17:902:f081:b0:2df:9edc:303f]) (user=wyihan job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:186:b0:2df:ab36:f145 with SMTP id d9443c01a7336-2e49b685f37mr4147425ad.68.1790883533313; Thu, 01 Oct 2026 12:38:53 -0700 (PDT) Date: Thu, 01 Oct 2026 19:37:40 +0000 In-Reply-To: <20261001-tdx-selftests-v15-0-7c62a5d8a992@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20261001-tdx-selftests-v15-0-7c62a5d8a992@google.com> X-Developer-Key: i=wyihan@google.com; a=ed25519; pk=cRi0fKzS5BMxlHyHY2pJv3w/1zcgfYKr6EYGYppdMYc= X-Developer-Signature: v=1; a=ed25519-sha256; t=1790883521; l=3515; i=wyihan@google.com; s=20260319; h=from:subject:message-id; bh=sZyyJs/KlSsWRD9sQg89mVg98goUj8UDv1HtfnjzwME=; b=O95g8bknqdRknghTtLqM3Z47uJTLV1rIB0wGM6T628Yfa65tkZlZ+mNz7Y+7PyuYycoq14Rsb L/WWSkzNI3iDFwRxhnitj/S/Jwpuy/HU1djFdGF3Vfc53bobmxENV8/ X-Mailer: b4 0.14.3 Message-ID: <20261001-tdx-selftests-v15-13-7c62a5d8a992@google.com> Subject: [PATCH v15 13/23] KVM: selftests: Support guest_memfd in-place conversion From: Lisa Wang To: Andrew Jones , Ackerley Tng , Binbin Wu , Chao Gao , Chenyi Qiang , Dave Hansen , Erdem Aktas , Ira Weiny , Isaku Yamahata , Kiryl Shutsemau , linux-kselftest@vger.kernel.org, Paolo Bonzini , "Pratik R. Sampat" , Reinette Chatre , Rick Edgecombe , Roger Wang , Ryan Afranji , Sagi Shahar , Sean Christopherson , Shuah Khan , Xiaoyao Li , Oliver Upton Cc: Jeremiah McReynolds , kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, x86@kernel.org, Lisa Wang Content-Type: text/plain; charset="utf-8" With guest_memfd in-place conversion, the memory region can be set as shared, eliminating the need for dual-backed memory. By setting the region to shared initially, the KVM selftest framework can populate the mmap()-ed guest_memfd memory just like any standard memory provider. For CoCo VMs, pages that need to be private are explicitly set to private before executing the VM. Signed-off-by: Lisa Wang --- tools/testing/selftests/kvm/lib/kvm_util.c | 20 ++++++++++++++------ tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c | 6 +++++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 7625908caf21..f474664b5d92 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -494,8 +494,10 @@ struct kvm_vm *__vm_create(struct vm_shape shape, u32 nr_runnable_vcpus, u64 nr_pages = vm_nr_pages_required(shape.mode, nr_runnable_vcpus, nr_extra_pages); struct userspace_mem_region *slot0; + u64 gmem_flags = 0; struct kvm_vm *vm; - int i, flags; + int flags = 0; + int i; kvm_set_files_rlimit(nr_runnable_vcpus); @@ -505,14 +507,20 @@ struct kvm_vm *__vm_create(struct vm_shape shape, u32 nr_runnable_vcpus, vm = ____vm_create(shape); /* - * Force GUEST_MEMFD for the primary memory region if necessary, e.g. - * for CoCo VMs that require GUEST_MEMFD backed private memory. + * Force GUEST_MEMFD for the primary memory region if necessary, and + * initialize it as shared if in-place conversion is supported, so the + * selftest framework can populate it exactly like other memory + * providers. */ - flags = 0; - if (is_guest_memfd_required(shape)) + if (is_guest_memfd_required(shape)) { flags |= KVM_MEM_GUEST_MEMFD; + if (kvm_has_gmem_attributes) + gmem_flags |= GUEST_MEMFD_FLAG_INIT_SHARED | + GUEST_MEMFD_FLAG_MMAP; + } - vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, 0, 0, nr_pages, flags); + vm_mem_add(vm, VM_MEM_SRC_ANONYMOUS, 0, 0, nr_pages, flags, + -1, 0, gmem_flags); for (i = 0; i < NR_MEM_REGIONS; i++) vm->memslots[i] = 0; diff --git a/tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c b/tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c index e348507a6be8..40ee9b1951c2 100644 --- a/tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +++ b/tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c @@ -23,6 +23,8 @@ void tdx_vm_setup_boot_code_region(struct kvm_vm *vm) gpa_t gpa; u8 *hva; + if (kvm_has_gmem_attributes) + gmem_flags |= GUEST_MEMFD_FLAG_INIT_SHARED | GUEST_MEMFD_FLAG_MMAP; vm_mem_add(vm, VM_MEM_SRC_SHMEM, alloc_gpa, TD_BOOT_CODE_SLOT, nr_pages, KVM_MEM_GUEST_MEMFD, -1, 0, gmem_flags); @@ -44,9 +46,11 @@ void tdx_vm_setup_boot_parameters_region(struct kvm_vm *vm, u32 nr_runnable_vcpu size_t boot_params_size = sizeof(struct td_boot_parameters) + nr_runnable_vcpus * sizeof(struct td_per_vcpu_parameters); int npages = DIV_ROUND_UP(boot_params_size, PAGE_SIZE); - const u64 gmem_flags = 0; + u64 gmem_flags = 0; gpa_t gpa; + if (kvm_has_gmem_attributes) + gmem_flags |= GUEST_MEMFD_FLAG_INIT_SHARED | GUEST_MEMFD_FLAG_MMAP; vm_mem_add(vm, VM_MEM_SRC_SHMEM, TD_BOOT_PARAMETERS_GPA, TD_BOOT_PARAMETERS_SLOT, npages, KVM_MEM_GUEST_MEMFD, -1, 0, gmem_flags); -- 2.56.0.rc1.315.gc6ed9934b7-goog