From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9CB0330FC1B; Sat, 23 May 2026 00:18:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779495486; cv=none; b=naXe2cBHr+UBDYHyw7iY5H9TvTMNF6U2K92Or+RX/SPUvLJ5wdo2i7eHT2N/G2tZYHNQzUqEoj/fp9fFs6gZNxHIy+Mjom06Hlgt/SX/f6K7g9xkxiyK00PKiJkkiMoRN8obYZ3jbLLFmb1q9UEhcVTaKKJ35QZ0uGN9r4giYRA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779495486; c=relaxed/simple; bh=E0i/Ob4PUht8m/+FvYsQmtkYc36JgnYgiL4WTdoKOdE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=DVgXZNA1FteDPags90NTG7TUbtnQR65T/8cSBEOBkdLC2uNY1fgH24ZVkXIPWV4gwm4aO3SGt2rmbbR59n4lIDw08NKy7b0cdqMdUaeREk6jUApw49KGbWe7o6DbL27+Em8lsGudsCpCPP6oAkzGofnq5Y1+TGERYNLROoxmZrI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=i4VZMuD7; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="i4VZMuD7" Received: by smtp.kernel.org (Postfix) with ESMTPS id 06876C4E676; Sat, 23 May 2026 00:18:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1779495486; bh=E0i/Ob4PUht8m/+FvYsQmtkYc36JgnYgiL4WTdoKOdE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=i4VZMuD7IbHxYn0vTEc3UBRTF8kslFd0PNTdjnFfKWHZydxN7uhWiKXRlx/WWc7cQ 7xKEwfSZ0vHrUiYPmzHjY5ijfXNtu1V+aDxzafhXqZhXUO51WW2W3qYo6R3OYi50OQ c+D1KTw/3eI4uxfJsmgcYogb4Qh3aTxgLpf0bNZqd5D6aNKDHt54znNDPzB5TzXa/s 96f/M4G74TEPDPXrAu087xEXR06uaap4Ju4kWNtAU4BB1OzDbilqliqV5m4wf2aDnd 12HYFPn9c272ihIMeclCMntXu5digyI7OnizLMH1FcFlL4gt3Yr+nEEm6GFIp2MdqT rAPGG3caM8Dlw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id E5412CD5BC0; Sat, 23 May 2026 00:18:05 +0000 (UTC) From: Ackerley Tng via B4 Relay Date: Fri, 22 May 2026 17:18:13 -0700 Subject: [PATCH v7 31/42] KVM: selftests: Convert with allocated folios in different layouts Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260522-gmem-inplace-conversion-v7-31-2f0fae496530@google.com> References: <20260522-gmem-inplace-conversion-v7-0-2f0fae496530@google.com> In-Reply-To: <20260522-gmem-inplace-conversion-v7-0-2f0fae496530@google.com> 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, 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, 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 , Baoquan He , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Youngjun Park , Qi Zheng , Shakeel Butt , Kiryl Shutsemau , 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, linux-coco@lists.linux.dev, Ackerley Tng X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1779495480; l=2229; i=ackerleytng@google.com; s=20260225; h=from:subject:message-id; bh=7qaC2ZARmSx5/GbZ2zCm8aFOw0W5K8P2rPxoe2X7dWE=; b=CNl+zq9vG8Y4XPhfpSNG4v1Da9a3yZKQ9V88Ne+bDfM5kK27U46YxFrxvdWdRC7UHnqCqDrgl viyxPND3xCfBIl8+e9KmN/69chiqwkjdrE0bglgWI8EKW0YdCdJeAB9 X-Developer-Key: i=ackerleytng@google.com; a=ed25519; pk=sAZDYXdm6Iz8FHitpHeFlCMXwabodTm7p8/3/8xUxuU= X-Endpoint-Received: by B4 Relay for ackerleytng@google.com/20260225 with auth_id=649 X-Original-From: Ackerley Tng Reply-To: ackerleytng@google.com From: Ackerley Tng Add a guest_memfd selftest to verify that memory conversions work correctly with allocated folios in different layouts. By iterating through which pages are initially faulted, the test covers various layouts of contiguous allocated and unallocated regions, exercising conversion with different range layouts. Signed-off-by: Ackerley Tng Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- .../kvm/x86/guest_memfd_conversions_test.c | 30 ++++++++++++++++++++++ 1 file changed, 30 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 b43ac196330f1..0b024fb7227f0 100644 --- a/tools/testing/selftests/kvm/x86/guest_memfd_conversions_test.c +++ b/tools/testing/selftests/kvm/x86/guest_memfd_conversions_test.c @@ -279,6 +279,36 @@ GMEM_CONVERSION_TEST_INIT_PRIVATE(before_allocation_private) test_convert_to_shared(t, 0, 0, 'A', 'B'); } +/* + * Test that when some of the folios in the conversion range are allocated, + * conversion requests are handled correctly in guest_memfd. Vary the ranges + * allocated before conversion, using test_page, to cover various layouts of + * contiguous allocated and unallocated regions. + */ +GMEM_CONVERSION_MULTIPAGE_TEST_INIT_SHARED(unallocated_folios, 8) +{ + const int second_page_to_fault = 4; + int i; + + /* + * Fault 2 of the pages to test filemap range operations except when + * test_page == second_page_to_fault. + */ + host_do_rmw(t->mem, test_page, 0, 'A'); + if (test_page != second_page_to_fault) + host_do_rmw(t->mem, second_page_to_fault, 0, 'A'); + + gmem_set_private(t->gmem_fd, 0, nr_pages * page_size); + for (i = 0; i < nr_pages; ++i) { + char expected = (i == test_page || i == second_page_to_fault) ? 'A' : 0; + + test_private(t, i, expected, 'B'); + } + + for (i = 0; i < nr_pages; ++i) + test_convert_to_shared(t, i, 'B', 'C', 'D'); +} + int main(int argc, char *argv[]) { TEST_REQUIRE(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM)); -- 2.54.0.794.g4f17f83d09-goog