From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f200.google.com (mail-pf1-f200.google.com [209.85.210.200]) (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 9F6E13FF1A5 for ; Wed, 26 Aug 2026 09:19:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.200 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787735959; cv=none; b=PfXDAgYbmOsR94hO642RM4fWJLsnX/Ui5iV1Q6QIVhLzA09s6tjNw1pbGehGWV8vhC4/eIEtS7jZ8FHJSAa2lGSHF+633O2wZGR5zMbw9a650Lp12MAQUN+5AeOX5qg1ru7XmOpwmjFkmNTZxPsCkkYh/FBTYJw1SsJkP7SeOQk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787735959; c=relaxed/simple; bh=D4kSZoQjn2evbYJeBPXvMiB6VpABDixLKIO9Wkc0q2Y=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=E9jxVS0aQRhxuDf2/J9xfoipG7Dpb80ZOV6NtRRuoV2FX0ni3FUEYVmqOMl1reCY6iJdc+HayasyJ3bKwHnKEDdXAKtUXGvdA+46R56H66U1gqcDApNvTJT8KmcgI6uwpnF26k3qCm8GQivd1I3spPigdNOtHNeaQjyzVD0Ah+Y= 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=sUyb1mZ4; arc=none smtp.client-ip=209.85.210.200 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="sUyb1mZ4" Received: by mail-pf1-f200.google.com with SMTP id d2e1a72fcca58-84842381150so912372b3a.3 for ; Wed, 26 Aug 2026 02:19:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787735953; x=1788340753; 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=Jk/fFiPly+8t/f3A0x8GyZ4oVakKVeks0566IWo6DSM=; b=sUyb1mZ45IDQGZFDnfu0KZp+x/lkLxpn6SfR3ahvQLSzfFVtx4Dg190vxGd0YRrbed IczqGcI2KIGks3i6JjzFetnCyID0fO72BhHSfmShCyHMsmdJOmp2M4jRiILylXwbOexq faiaV8Xi0Mri9VZhC5Q7zbaDUa5dODzsf0XDTaM0KXGI5+5Ugzw13sEHIKDWnAoNCKzD DTDna7E/3rTDRaROGY1dbEkUHQ2gUG43ZWENN2xQr8+BBDhUsQSmvzdtOahqdq180if0 8yvyFwVICJsMBpRnnYJWRcsZ9+kiNJ4wQ4Q4juefDiYgooS4F3wEZQLdVc6buzlmcS6M RnZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787735953; x=1788340753; 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=Jk/fFiPly+8t/f3A0x8GyZ4oVakKVeks0566IWo6DSM=; b=UbVm0np+nToNL1CAdThMoQYzcdSpaIEyqZqPBqKqxyXf5/4/4w+2NTqXYozQNJTGPK 6fY61Lny9YS/NBKWdTZLwTmLhKeDckBDGsx4FmWZW5pXiEj3RYJ0EgAYd6ApMChBiqlr d2Gf8FKXPl5BYDyKUB804SGpubQQNy3QO5tnAy89lZFVGEvHWrLjlVFDvP13JdwRaw5g oIdtWA4+H24iDaNK2xCHPWWRFnUvJfSS1giRKeW1cyVU4/NzwyRaQ9CKYZWGUw3crM+g 0CQx5L1OjgdDLVGkgHkqnGqpVDNf3mEC/sFamrtbCHyDSUpPv4CbUN44/curN9oR9qRt LJMg== X-Gm-Message-State: AFuF++mIOABjmWqlnB2knznwXXIN/+x4BXY2KuZykOa8jetzP9fhuTwi gMzHumGwG6Jx89M2lskvC5IHrcjfyYtMfPjjmUvCbOmuwBAxwWJb7nPTiDd21NWK9UpLsTsciU2 tmcknl1iEgj/1LDsP54WWa/niaQ== X-Received: from pgcn16.prod.google.com ([2002:a63:7210:0:b0:cc1:c6f9:542e]) (user=ackerleytng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:4289:b0:84e:2382:f4f0 with SMTP id d2e1a72fcca58-85372ea2586mr10262808b3a.4.1787735952477; Wed, 26 Aug 2026 02:19:12 -0700 (PDT) Date: Wed, 26 Aug 2026 09:18:36 +0000 In-Reply-To: <20260826-gmem-inplace-conversion-v11-0-0a15d8a799aa@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org 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=3125; i=ackerleytng@google.com; s=20260225; h=from:subject:message-id; bh=D4kSZoQjn2evbYJeBPXvMiB6VpABDixLKIO9Wkc0q2Y=; b=0Xl+Ft2JNeFgHc72cKdPUmlWIaaUKrTcWNaCuE5l4p0QjHUf+PDqPvln09IV5z3p6JgNwwSEQ MgPrgGhkueuDsaZhj/coMGB3BJHJjWnz0bwzPjys8Q0r27VmXRxCpWH X-Mailer: b4 0.16.0 Message-ID: <20260826-gmem-inplace-conversion-v11-38-0a15d8a799aa@google.com> Subject: [PATCH v11 38/46] KVM: selftests: Test conversion with elevated page refcount 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" Add a selftest to verify that converting a shared guest_memfd page to a private page fails if the page has an elevated reference count. When KVM converts a shared page to a private one, it expects the page to have a reference count equal to the reference counts taken by the filemap. If another kernel subsystem holds a reference to the page, the conversion must be aborted. The test asserts that both bulk and single-page conversion attempts correctly fail with EAGAIN for the pinned page. After the page is unpinned, the test verifies that subsequent conversions succeed. Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Reviewed-by: Fuad Tabba Tested-by: Shivank Garg Signed-off-by: Ackerley Tng --- .../kvm/x86/guest_memfd_conversions_test.c | 56 ++++++++++++++++++++++ 1 file changed, 56 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 a972a09c5c6e5..e1c84df0f0596 100644 --- a/tools/testing/selftests/kvm/x86/guest_memfd_conversions_test.c +++ b/tools/testing/selftests/kvm/x86/guest_memfd_conversions_test.c @@ -444,6 +444,62 @@ GMEM_CONVERSION_TEST_INIT_SHARED(forked_accesses) #undef TEST_STATE_AWAIT } +static void test_convert_to_private_fails(test_data_t *t, u64 pgoff, + size_t nr_pages, + u64 expected_error_offset) +{ + /* +1 to make it anything but expected_error_offset. */ + u64 error_offset = expected_error_offset + 1; + u64 offset = pgoff * page_size; + int ret; + + do { + ret = __gmem_set_private(t->gmem_fd, offset, + nr_pages * page_size, &error_offset); + } while (ret == -1 && errno == EINTR); + TEST_ASSERT(ret == -1 && errno == EAGAIN, + "Wanted EAGAIN on page %lu, got %d (ret = %d)", pgoff, + errno, ret); + TEST_ASSERT_EQ(error_offset, expected_error_offset); +} + +GMEM_CONVERSION_MULTIPAGE_TEST_INIT_SHARED(elevated_refcount, 4) +{ + int i; + + pin_pages(t->mem + test_page * page_size, page_size); + + for (i = 0; i < nr_pages; i++) + test_shared(t, i, 0, 'A', 'B'); + + /* + * Converting in bulk should fail as long any page in the range has + * unexpected refcounts. + */ + test_convert_to_private_fails(t, 0, nr_pages, test_page * page_size); + + for (i = 0; i < nr_pages; i++) { + /* + * Converting page-wise should also fail as long any page in the + * range has unexpected refcounts. + */ + if (i == test_page) + test_convert_to_private_fails(t, i, 1, test_page * page_size); + else + test_convert_to_private(t, i, 'B', 'C'); + } + + unpin_pages(); + + gmem_set_private(t->gmem_fd, 0, nr_pages * page_size); + + for (i = 0; i < nr_pages; i++) { + char expected = i == test_page ? 'B' : 'C'; + + test_private(t, i, expected, 'D'); + } +} + 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