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 E2C6C3F3279 for ; Wed, 26 Aug 2026 09:19:05 +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=1787735949; cv=none; b=CDFsTkHstDsJT1gDdE9CJ/4wLqow94CXuTZkPoVzIRwhOVMMuA5mHlmWaDWWsPi/YhNV+j2j7dtWdr7W/XFzk/hGNePJocJstz7jdrUfW+p1FIry9itGmbM1OdU4gHATAcEFXXlcC3sTMz7C0MaXpp+/Mn9CSoax32OnNIGTOGU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787735949; c=relaxed/simple; bh=7w+gDb7QtZnkDWq+h4HUO3ST/UY633rHaNS6m39pYVY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=AprbQJK1Kimlbk0vvp8EUE9TfhnlrQmbny+dn+pUeFGjRLBiKAXW6+6rHUTvcDEG6pXhWCyM9pAy0/4SxbzerQ77hcz9f51Vx87KBtOeHd0TGA9ah/2hRKnPX7dPNp9pWU6LtI8w13Bj1LXraWRQe1L0fMePRNWaied6sGPOTS4= 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=SnJCOWXC; 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--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="SnJCOWXC" Received: by mail-pl1-f198.google.com with SMTP id d9443c01a7336-2cfe48ca1efso12950115ad.0 for ; Wed, 26 Aug 2026 02:19:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787735942; x=1788340742; darn=lists.linux.dev; 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=89L7E0e7HlFcsWgm0DMydejmixwj8/yVJ5vfC8XlboE=; b=SnJCOWXC5/rOMFA/vx/6nF51iWY0lEnNv7RosxVWTD2u9xB2jn1SnHwEJ5AxOIZvXM MoClmBOVrrKc8Ej93mHp8p182DR4o9p6rM3qU9pEGPgQVL7o+qrhaTRS4b+RD0lGWJN2 21i86X2euGhLXnLjKq3nF42t9jlgcs5IgkkhWpMcNm9MNzOrCzj07RI5Ji4I9M9Z6ASC axfS84ILU2w9CQRlTAG8vNxIkzNOe1aIeiNmJdHN6WhVjxLf2PxrFPg+aTZvDRXJOZU5 002bJ2pAcfpUzPOgM8y00Kz9JMThW7WuzZP+aQ8w4r1ltTPA8XoS0WoETLf82F24Tt6E u3aQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787735942; x=1788340742; 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=89L7E0e7HlFcsWgm0DMydejmixwj8/yVJ5vfC8XlboE=; b=tPjaZGSca1Lim8907WBqGXJjtFdRGAXRH0geBW9gBf3Hj1BXko0NH95F0TDRSPDdLN tdNFWR3hEX3jjo67Fss0NQ64Wztb5ItAhabYOGUone/gkXxvlQEVXwVJGsmTAvdHy81k 1XaNakG1kviD3dd7NAuhBLBAH+qejBMS+KOEUGyHygkm/ILoI7FqzQV8Eh5TglCPy6yu DPML613ClC31N3mUzetgSju/uEASnIypnV4G5cvD7kLAw3latl9RRoqha2cpKFYMwtDm 7WsZ5vmdYGCLAqTGwDzt5Onc7cJ17ck8IkR7/KG+j2u+KMJWlZWOnNUGLO333Kpfw0g7 fubA== X-Forwarded-Encrypted: i=1; AHgh+RpffSoQvUkqKRvGcRxpYeJE4CQYlOtCkEpSa2alXT6eWXk7AENwiSK14DO16aXly2M+R+q6nkHMYQth@lists.linux.dev X-Gm-Message-State: AFuF++lqsEvzL2SbGmlSi7pVbjRN5JbdvE5GaWdZGHtwgsJDCgI00JfI ZRXZ6fQWkCtN+6iVbGc13lH4vc8ufkpKpPCHQL58vaLbKJtRoDFCRANIXW0Xpzaa9YuofLRujxd zsUN8ibfdfONau7qU/n3WkXhzJw== X-Received: from plwg14.prod.google.com ([2002:a17:902:f74e:b0:2d5:9dd2:5fc6]) (user=ackerleytng job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:2283:b0:2d6:fed1:7127 with SMTP id d9443c01a7336-2d70779678cmr89516245ad.0.1787735942070; Wed, 26 Aug 2026 02:19:02 -0700 (PDT) Date: Wed, 26 Aug 2026 09:18:30 +0000 In-Reply-To: <20260826-gmem-inplace-conversion-v11-0-0a15d8a799aa@google.com> Precedence: bulk X-Mailing-List: linux-coco@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260826-gmem-inplace-conversion-v11-0-0a15d8a799aa@google.com> X-Developer-Key: i=ackerleytng@google.com; a=ed25519; pk=sAZDYXdm6Iz8FHitpHeFlCMXwabodTm7p8/3/8xUxuU= X-Developer-Signature: v=1; a=ed25519-sha256; t=1787735885; l=4642; i=ackerleytng@google.com; s=20260225; h=from:subject:message-id; bh=7w+gDb7QtZnkDWq+h4HUO3ST/UY633rHaNS6m39pYVY=; b=r4t4S/JNI5hvSCE0TK199jmMLi6mPSfRkQixeoALxuEBJ2SPvehHkS7r4ESdRDawsAmTdhPMz yre4PD9BkhTCsEfVE6Y3A1ngu0fOF/kQZRDMogw2uXeBID6J4beusMe X-Mailer: b4 0.16.0 Message-ID: <20260826-gmem-inplace-conversion-v11-32-0a15d8a799aa@google.com> Subject: [PATCH v11 32/46] KVM: selftests: Test conversion precision in 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, jmattson@google.com, 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, 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, liam@infradead.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 , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Youngjun Park , Qi Zheng , Shakeel Butt , Kiryl Shutsemau , Baoquan He , Jason Gunthorpe , John Hubbard , Peter Xu , tarunsahu@google.com, Fuad Tabba , 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, linux-coco@lists.linux.dev, Ackerley Tng Content-Type: text/plain; charset="utf-8" The existing guest_memfd conversion tests only use single-page memory regions. This provides no coverage for multi-page guest_memfd objects, specifically whether KVM correctly handles the page index for conversion operations. An incorrect implementation could, for example, always operate on the first page regardless of the index provided. Add a new test case to verify that conversions between private and shared memory correctly target the specified page within a multi-page guest_memfd. This test also verifies the precision of memory conversions by converting a single page and then iterating through all other pages to ensure they remain in their original state. To support this test, add a new GMEM_CONVERSION_MULTIPAGE_TEST_INIT_SHARED macro that handles setting up and tearing down the VM for each page iteration. The teardown logic is adjusted to prevent a double-free in this new scenario. Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Reviewed-by: Fuad Tabba Tested-by: Shivank Garg Signed-off-by: Ackerley Tng --- v10: Update macro GMEM_CONVERSION_MULTIPAGE_TEST_INIT_SHARED to avoid potential macro argument reuse --- .../kvm/x86/guest_memfd_conversions_test.c | 67 ++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/tools/testing/selftests/kvm/x86/guest_memfd_conversions_test.c b/tools/testing/selftests/kvm/x86/guest_memfd_conversions_test.c index 4c20d3d8e9349..a8d89df53e46d 100644 --- a/tools/testing/selftests/kvm/x86/guest_memfd_conversions_test.c +++ b/tools/testing/selftests/kvm/x86/guest_memfd_conversions_test.c @@ -61,8 +61,13 @@ static void gmem_conversions_do_setup(test_data_t *t, int nr_pages, static void gmem_conversions_do_teardown(test_data_t *t) { + /* Use NULL to avoid second free in FIXTURE_TEARDOWN (multipage tests). */ + if (!t->vcpu) + return; + /* No need to close gmem_fd, it's owned by the VM structure. */ kvm_vm_free(t->vcpu->vm); + t->vcpu = NULL; } FIXTURE_TEARDOWN(gmem_conversions) @@ -103,6 +108,30 @@ static void __gmem_conversions_##test(test_data_t *t, int nr_pages) \ #define GMEM_CONVERSION_TEST_INIT_SHARED(test) \ __GMEM_CONVERSION_TEST_INIT_SHARED(test, 1) +/* + * Repeats test over nr_pages in a guest_memfd of size nr_pages, providing each + * test iteration with test_page, the index of the page under test in + * guest_memfd. test_page takes values 0..(nr_pages - 1) inclusive. + */ +#define GMEM_CONVERSION_MULTIPAGE_TEST_INIT_SHARED(test, __nr_pages) \ +static void __gmem_conversions_multipage_##test(test_data_t *t, int nr_pages, \ + const int test_page); \ + \ +TEST_F(gmem_conversions, test) \ +{ \ + const u64 flags = GUEST_MEMFD_FLAG_MMAP | GUEST_MEMFD_FLAG_INIT_SHARED; \ + const int nr = (__nr_pages); \ + int i; \ + \ + for (i = 0; i < nr; ++i) { \ + gmem_conversions_do_setup(self, nr, flags); \ + __gmem_conversions_multipage_##test(self, nr, i); \ + gmem_conversions_do_teardown(self); \ + } \ +} \ +static void __gmem_conversions_multipage_##test(test_data_t *t, int nr_pages, \ + const int test_page) + struct guest_check_data { void *mem; char expected_val; @@ -201,6 +230,44 @@ GMEM_CONVERSION_TEST_INIT_SHARED(init_shared) test_convert_to_shared(t, 0, 'C', 'D', 'E'); } +GMEM_CONVERSION_MULTIPAGE_TEST_INIT_SHARED(indexing, 4) +{ + int i; + + /* Get a char that varies with both i and n. */ +#define combine(x, n) (((x) << 4) + (n)) +#define i_(n) (combine(i, n)) +#define t_(n) (combine(test_page, n)) + + /* + * Start with the highest index, to catch any errors when, perhaps, the + * first page is returned even for the last index. + */ + for (i = nr_pages - 1; i >= 0; --i) + test_shared(t, i, 0, i_(0), i_(2)); + + test_convert_to_private(t, test_page, t_(2), t_(3)); + + for (i = 0; i < nr_pages; ++i) { + if (i == test_page) + test_private(t, test_page, t_(3), t_(4)); + else + test_shared(t, i, i_(2), i_(3), i_(4)); + } + + test_convert_to_shared(t, test_page, t_(4), t_(5), t_(6)); + + for (i = 0; i < nr_pages; ++i) { + char expected = i == test_page ? t_(6) : i_(4); + + test_shared(t, i, expected, i_(7), i_(8)); + } + +#undef t_ +#undef i_ +#undef combine +} + int main(int argc, char *argv[]) { TEST_REQUIRE(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM)); -- 2.55.0.887.g758fc8c411-goog