From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (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 A7DF73E1CF2 for ; Thu, 26 Mar 2026 22:25:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774563951; cv=none; b=b5Tr13J9YbTzB0abLEWOXb8CMfSBku44/tqcxCKmTlp6PKJIZ08ZXg8qLmEqCyMEkG94ZNg2jIWAI45bvPZe245W8JjCUuB/y2WbyYx7pYD2PU5Fh4g4+Z3t+rIrVla0rNY8zkfLQ+A4YKWDvF8iKMz4WRIcgVunORMczyAfMxI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774563951; c=relaxed/simple; bh=atM67lZrb3L/4FSuzE/lIWzYT7CU6pwH5U1B0OOVyHY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UPTYoHlLMZJNaKNwONl9n77Eiy4k+CnT2HlIbYCaIm/2STZRWjogaEFmk69Fujocch0q9y/7wIsrP55hVltZY8OeQ3dCIMEmUrqH+X9RDtnwvNP/COjhxRsRiFpp3TVwUZruPgpdbdWNg5ZT9cxUmcxtqH+QNRiwpZT8eMewwDI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ackerleytng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=vyvdk4GD; arc=none smtp.client-ip=209.85.215.201 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--ackerleytng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vyvdk4GD" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-c737b6686ddso1031034a12.3 for ; Thu, 26 Mar 2026 15:25:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1774563949; x=1775168749; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=YsFx8S2ZIsHTA7mxdUPwMmhoKs9OpIzRTtvg/d03faw=; b=vyvdk4GDEpoTi5tqF13UIRCC6EVzVP5ZrEGvShOwfUs1WNUCmwC0LRtzqDu5sDCYWQ JxfgbpfyRWJz4xlye5x+zSNYxOGQYUx2i0CDMpgVqSBxQjfULyPFwCcfNW8dlTJa80CR JbMRI62iov3MW6Q+1a9BUbIiBv7p4XUP9ngoADhXBh784bpv81Ei8LQOIETDXexz2qC7 rU02g3qIDRlo/wEkldx1MPWCz0Cm/CaN8sszYIATdzV7wCRWz/tt2hTABeAvhsLyTgRk QJkVh9L5OfN+AxzYPKXstArdCy9g+ZIybpHbp4dv2XFyj/DJfQfrxAK2wIcZ608D+fP7 hBkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774563949; x=1775168749; h=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; bh=YsFx8S2ZIsHTA7mxdUPwMmhoKs9OpIzRTtvg/d03faw=; b=ATMYouhLZll4lXQwAQZXh6MsvS2BAn/2zw2fh4QL5iGNgNv7UNKMaMbudnBmqKAOyo OBQw1yBnkehRmWh7fCY0NFxHfP6fV+u5njzWqoruIaiw+tFoD2Bqt5+qY+FK6iFLGdqJ 4CQ0QSIgVs910cCJcUMmWFTkr3A0Tr1XeuJ0IhmiIT8dvZzzAjeyv1kl8bSfl6GX9/du Kz/2PX3hvzqSFfrXYkTgjnBJ4bMxOBmh8x9rauCKSYox9oeqLmcsOFUJge/WDh4g16BU 2YFMsyUjD6z2BWotVgzUt4/RRptB0GT4llnc5bPsEs4KN0LAsLAMEqTsqUTWlG/cvgo5 oE3Q== X-Forwarded-Encrypted: i=1; AJvYcCXDR3RAw13mwzh/asIs3VyhAFtbUcbwy+S3kalQLvaHEUB+zoPJsl5iz+MIh3xUs2zaGb0fkuRQLdo=@vger.kernel.org X-Gm-Message-State: AOJu0Yz43AT32NBRQyUI0PfkIQQQ44txU9iYihC6g3XW7lSgGiXuTYjC 9LlMB2U2jrM7OQgtrkGPY69IZiWjhejNy6KNcxiM6oCzctbsgqv6ZmTqvryWapXd1F7qFvXl1yi I624L4NYgYtfWpQhLNSUzLpO8Eg== X-Received: from pfwy16.prod.google.com ([2002:a05:6a00:1c90:b0:82c:6e7c:ac6d]) (user=ackerleytng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:b488:b0:82a:60ad:874 with SMTP id d2e1a72fcca58-82c95e9ec0amr174041b3a.19.1774563948768; Thu, 26 Mar 2026 15:25:48 -0700 (PDT) Date: Thu, 26 Mar 2026 15:24:50 -0700 In-Reply-To: <20260326-gmem-inplace-conversion-v4-0-e202fe950ffd@google.com> Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260326-gmem-inplace-conversion-v4-0-e202fe950ffd@google.com> X-Developer-Key: i=ackerleytng@google.com; a=ed25519; pk=sAZDYXdm6Iz8FHitpHeFlCMXwabodTm7p8/3/8xUxuU= X-Developer-Signature: v=1; a=ed25519-sha256; t=1774563861; l=5336; i=ackerleytng@google.com; s=20260225; h=from:subject:message-id; bh=atM67lZrb3L/4FSuzE/lIWzYT7CU6pwH5U1B0OOVyHY=; b=5MuqTkb5deqhYAPqYmfVeS6JWQaZBgzlViovjh0d6q6SQRu7C3TxjQOxMfoLP3N43SByS4Jw5 ZnxOUmxggc7B4jsUWt0qDmE7eGPO++o93Lfuw6k6XZIImuFyIUiBC0X X-Mailer: b4 0.14.3 Message-ID: <20260326-gmem-inplace-conversion-v4-41-e202fe950ffd@google.com> Subject: [PATCH RFC v4 41/44] KVM: selftests: Update private_mem_conversions_test to mmap() guest_memfd From: Ackerley Tng To: aik@amd.com, andrew.jones@linux.dev, binbin.wu@linux.intel.com, brauner@kernel.org, chao.p.peng@linux.intel.com, david@kernel.org, ira.weiny@intel.com, jmattson@google.com, jroedel@suse.de, jthoughton@google.com, michael.roth@amd.com, oupton@kernel.org, pankaj.gupta@amd.com, qperret@google.com, rick.p.edgecombe@intel.com, rientjes@google.com, shivankg@amd.com, steven.price@arm.com, tabba@google.com, willy@infradead.org, wyihan@google.com, yan.y.zhao@intel.com, forkloop@google.com, pratyush@kernel.org, suzuki.poulose@arm.com, aneesh.kumar@kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Jonathan Corbet , Shuah Khan , Shuah Khan , Vishal Annapurve , Andrew Morton , Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Jason Gunthorpe , Vlastimil Babka Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, Ackerley Tng Content-Type: text/plain; charset="utf-8" Update the private memory conversions selftest to also test conversions that are done "in-place" via per-guest_memfd memory attributes. In-place conversions require the host to be able to mmap() the guest_memfd so that the host and guest can share the same backing physical memory. This includes several updates, that are conditioned on the system supporting per-guest_memfd attributes (kvm_has_gmem_attributes): 1. Set up guest_memfd requesting MMAP and INIT_SHARED. 2. With in-place conversions, the host's mapping points directly to the guest's memory. When the guest converts a region to private, host access to that region is blocked. Update the test to expect a SIGBUS when attempting to access the host virtual address (HVA) of private memory. 3. Use vm_mem_set_memory_attributes(), which chooses how to set memory attributes based on whether kvm_has_gmem_attributes. Restrict the test to using VM_MEM_SRC_SHMEM because guest_memfd's required mmap() flags and page sizes happens to align with those of VM_MEM_SRC_SHMEM. As long as VM_MEM_SRC_SHMEM is used for src_type, vm_mem_add() works as intended. Signed-off-by: Ackerley Tng Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- .../kvm/x86/private_mem_conversions_test.c | 46 ++++++++++++++++++---- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/kvm/x86/private_mem_conversions_test.c b/tools/testing/selftests/kvm/x86/private_mem_conversions_test.c index 47f1eb9212591..29c3c5b2f538e 100644 --- a/tools/testing/selftests/kvm/x86/private_mem_conversions_test.c +++ b/tools/testing/selftests/kvm/x86/private_mem_conversions_test.c @@ -306,9 +306,14 @@ static void handle_exit_hypercall(struct kvm_vcpu *vcpu) if (do_fallocate) vm_guest_mem_fallocate(vm, gpa, size, map_shared); - if (set_attributes) - vm_set_memory_attributes(vm, gpa, size, - map_shared ? 0 : KVM_MEMORY_ATTRIBUTE_PRIVATE); + if (set_attributes) { + u64 attrs = map_shared ? 0 : KVM_MEMORY_ATTRIBUTE_PRIVATE; + u64 flags = kvm_has_gmem_attributes ? + KVM_SET_MEMORY_ATTRIBUTES2_PRESERVE : 0; + + vm_mem_set_memory_attributes(vm, gpa, size, attrs, flags); + } + run->hypercall.ret = 0; } @@ -352,8 +357,20 @@ static void *__test_mem_conversions(void *__vcpu) size_t nr_bytes = min_t(size_t, vm->page_size, size - i); uint8_t *hva = addr_gpa2hva(vm, gpa + i); - /* In all cases, the host should observe the shared data. */ - memcmp_h(hva, gpa + i, uc.args[3], nr_bytes); + /* + * When using per-guest_memfd memory attributes, + * i.e. in-place conversion, host accesses will + * point at guest memory and should SIGBUS when + * guest memory is private. When using per-VM + * attributes, i.e. separate backing for shared + * vs. private, the host should always observe + * the shared data. + */ + if (kvm_has_gmem_attributes && + uc.args[0] == SYNC_PRIVATE) + TEST_EXPECT_SIGBUS(READ_ONCE(*hva)); + else + memcmp_h(hva, gpa + i, uc.args[3], nr_bytes); /* For shared, write the new pattern to guest memory. */ if (uc.args[0] == SYNC_SHARED) @@ -382,6 +399,7 @@ static void test_mem_conversions(enum vm_mem_backing_src_type src_type, uint32_t const size_t slot_size = memfd_size / nr_memslots; struct kvm_vcpu *vcpus[KVM_MAX_VCPUS]; pthread_t threads[KVM_MAX_VCPUS]; + uint64_t gmem_flags; struct kvm_vm *vm; int memfd, i; @@ -397,12 +415,17 @@ static void test_mem_conversions(enum vm_mem_backing_src_type src_type, uint32_t vm_enable_cap(vm, KVM_CAP_EXIT_HYPERCALL, (1 << KVM_HC_MAP_GPA_RANGE)); - memfd = vm_create_guest_memfd(vm, memfd_size, 0); + if (kvm_has_gmem_attributes) + gmem_flags = GUEST_MEMFD_FLAG_MMAP | GUEST_MEMFD_FLAG_INIT_SHARED; + else + gmem_flags = 0; + + memfd = vm_create_guest_memfd(vm, memfd_size, gmem_flags); for (i = 0; i < nr_memslots; i++) vm_mem_add(vm, src_type, BASE_DATA_GPA + slot_size * i, BASE_DATA_SLOT + i, slot_size / vm->page_size, - KVM_MEM_GUEST_MEMFD, memfd, slot_size * i, 0); + KVM_MEM_GUEST_MEMFD, memfd, slot_size * i, gmem_flags); for (i = 0; i < nr_vcpus; i++) { uint64_t gpa = BASE_DATA_GPA + i * per_cpu_size; @@ -452,17 +475,24 @@ static void usage(const char *cmd) int main(int argc, char *argv[]) { - enum vm_mem_backing_src_type src_type = DEFAULT_VM_MEM_SRC; + enum vm_mem_backing_src_type src_type; uint32_t nr_memslots = 1; uint32_t nr_vcpus = 1; int opt; TEST_REQUIRE(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM)); + src_type = kvm_has_gmem_attributes ? VM_MEM_SRC_SHMEM : + DEFAULT_VM_MEM_SRC; + while ((opt = getopt(argc, argv, "hm:s:n:")) != -1) { switch (opt) { case 's': src_type = parse_backing_src_type(optarg); + TEST_ASSERT(!kvm_has_gmem_attributes || + src_type == VM_MEM_SRC_SHMEM, + "Testing in-place conversions, only %s mem_type supported\n", + vm_mem_backing_src_alias(VM_MEM_SRC_SHMEM)->name); break; case 'n': nr_vcpus = atoi_positive("nr_vcpus", optarg); -- 2.53.0.1018.g2bb0e51243-goog