From nobody Tue Apr 7 14:20:56 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (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 A98FC37EFF5 for ; Fri, 13 Mar 2026 06:13:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773382419; cv=none; b=Mgk+OwHhUnehbS2a3lHeOeKfYX9Nw6qIXLUP8nGVZo8jfxYDU/HOVoCQ+bvoOhrmaaBxuDBK93ipMMoACrD8V8bSLGbnt/MByGOXqo18Fg0gLvvELZyyYVAz+RUsrmjOg0GDlCyRp3sp1aIQETSzWDDaCTXbs6tKHWICbo3fdxA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773382419; c=relaxed/simple; bh=xRKb3bVMcx/uVRBFcKELGtm1TsNEsqcmghIMICAO0wI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=eE5JYuFrOBsekVpEjQ+UvtqqyNzsNiOXwi9hhnmGJAh+i8PSC9oBUjHIiGPdKIyFAZI7irGYJJ55eOE+slhtoxk587jKom3JhTI3cB/BKjNCiXonUqSuWDEikvXuFZbx83Zcl6Ju4v1KD7fqQ/6b14SvlOVC6RLvKHs2terT7oU= 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=qyHt+hsb; arc=none smtp.client-ip=209.85.214.202 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="qyHt+hsb" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2ae59e057f1so24892495ad.1 for ; Thu, 12 Mar 2026 23:13:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773382415; x=1773987215; 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=MtLKd6/akGAIni+0N8+cFKVyOqbU6nOkVaBf+x9Xbcg=; b=qyHt+hsbM3whvSZ0YKRIpGAY1vPC2QssEZuVuYsbYzZTi6ynLVyrx/S9RB9SefRlQY 7jA3sFN1JpDEpTufAC/sw4f3FrnfAsM9c2puKaHeeAqw8f39I96sK0SptIq5afKcj6Rl M+fU0f7QzmSspMUx6FUFz3gr0/sEUlJXmspk+4S6pgQCJqeYlIVAFG/BntMMWKP4iGkr XJCA8aOC2OvNkUjmqkf2rHr21tT/BJR3y+Dy0DoJCZXc8r9uI5vtPoWdUw4Ub+9mTdm8 YsW/jZHqujMRBX2Eb5Ejig68DrcjOcNx3RzKdi4r6q7HhlFd5cWiwMDGtXgKw89VoWzZ JM4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773382415; x=1773987215; 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=MtLKd6/akGAIni+0N8+cFKVyOqbU6nOkVaBf+x9Xbcg=; b=psa/RQp9sVEEcItyuFKLkvdiJbEPGw/T2whhkU/12i14nXKO67jaCdC59Qab3JBh1c XOVs43r0lOL2mqkGH3y1xuqUMPM0wEQg3AQwEiSEhkfc18OUfM3kJr+XpCfqp6vnzClD 7NDypRAzwJJoRRI7U/iiRMgfQrEO0PZXCUKKx3pRsja9Mjetlskdq9/GitBanLVZgH8h VQNMOkmObBlbq74wJHxHhnYFabl0xZahbl6dO3gGayLWbEGwX/Ynz3rAVy3dnwTiZU0x bmg79R+G7PdnLCDGjNbifshYHKhvP7aSBKx0ZGeD8sKHZg0TNN7ctGi1Jv44oEm1AwcI pA3g== X-Forwarded-Encrypted: i=1; AJvYcCXSY1AkybrhmOTuYwVtVr5K9eBIpXbuJU0/xYDAn7DlAXpkE5krnR2k031XIhC+KZFx/Xp5CAT6sCvFLsY=@vger.kernel.org X-Gm-Message-State: AOJu0Yy087bR6+95d8QRMncemL0SBgoOw5Fh9RcbNpkFmfsJiFIFXXCi MvTbg4RNTOlk0JWNDT4HFbDeVycq59ykzO9gPRWGIXqp7lnyQizEIcCxtmhbhA2Ncck3mBufzzH Fv1kCxaqt2jt0XVwQxrNpZEzQjw== X-Received: from pjtz21.prod.google.com ([2002:a17:90a:cb15:b0:359:f37c:e0a5]) (user=ackerleytng job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:540d:b0:359:2d1c:9206 with SMTP id 98e67ed59e1d1-35a220bacbdmr1758060a91.33.1773382414557; Thu, 12 Mar 2026 23:13:34 -0700 (PDT) Date: Fri, 13 Mar 2026 06:13:07 +0000 In-Reply-To: <20260313-gmem-inplace-conversion-v3-0-5fc12a70ec89@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260313-gmem-inplace-conversion-v3-0-5fc12a70ec89@google.com> X-Developer-Key: i=ackerleytng@google.com; a=ed25519; pk=sAZDYXdm6Iz8FHitpHeFlCMXwabodTm7p8/3/8xUxuU= X-Developer-Signature: v=1; a=ed25519-sha256; t=1773382364; l=3729; i=ackerleytng@google.com; s=20260225; h=from:subject:message-id; bh=xRKb3bVMcx/uVRBFcKELGtm1TsNEsqcmghIMICAO0wI=; b=4UlIoexpGM586Ep9gpcgcpdjTd+BgERPWiazIPux/Pti//AcvAjinkDe07IGBccFkXXhQyT3l 2SDQznJ0i3JAz7Lsp03hMRKGOS/RwR/HlpbrT0AR6Z1kG7mJvjjbsx5 X-Mailer: b4 0.14.3 Message-ID: <20260313-gmem-inplace-conversion-v3-28-5fc12a70ec89@google.com> Subject: [PATCH RFC v3 28/43] 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, 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 , 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, Ackerley Tng Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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, for example via pin_user_pages(), the conversion must be aborted. This test uses vmsplice to increment the refcount of a specific page. The reference is kept on the page by not reading data out from vmsplice's destination pipe. It then attempts to convert a range of pages, including the page with elevated refcount, from shared to private. 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. Signed-off-by: Ackerley Tng Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- .../selftests/kvm/guest_memfd_conversions_test.c | 78 ++++++++++++++++++= ++++ 1 file changed, 78 insertions(+) diff --git a/tools/testing/selftests/kvm/guest_memfd_conversions_test.c b/t= ools/testing/selftests/kvm/guest_memfd_conversions_test.c index c1a9cc7c9fae6..8727474325451 100644 --- a/tools/testing/selftests/kvm/guest_memfd_conversions_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_conversions_test.c @@ -396,6 +396,84 @@ GMEM_CONVERSION_TEST_INIT_SHARED(forked_accesses) kvm_munmap(test_state, sizeof(*test_state)); } =20 +static int pin_pipe[2] =3D { -1, -1 }; + +static void pin_pages(void *vaddr, uint64_t size) +{ + struct iovec iov =3D { + .iov_base =3D vaddr, + .iov_len =3D size, + }; + + if (pin_pipe[1] < 0) + TEST_ASSERT_EQ(pipe(pin_pipe), 0); + + TEST_ASSERT_EQ(vmsplice(pin_pipe[1], &iov, 1, 0), size); +} + +static void unpin_pages(void) +{ + close(pin_pipe[1]); + pin_pipe[1] =3D -1; + close(pin_pipe[0]); + pin_pipe[0] =3D -1; +} + +static void test_convert_to_private_fails(test_data_t *t, loff_t pgoff, + size_t nr_pages, + loff_t expected_error_offset) +{ + loff_t offset =3D pgoff * page_size; + loff_t error_offset =3D -1ul; + int ret; + + do { + ret =3D __gmem_set_private(t->gmem_fd, offset, + nr_pages * page_size, &error_offset); + } while (ret =3D=3D -1 && errno =3D=3D EINTR); + TEST_ASSERT(ret =3D=3D -1 && errno =3D=3D EAGAIN, + "Wanted EAGAIN on page %lu, got %d (ret =3D %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 =3D 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 =3D 0; i < nr_pages; i++) { + /* + * Converting page-wise should also fail as long any page in the + * range has unexpected refcounts. + */ + if (i =3D=3D 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 =3D 0; i < nr_pages; i++) { + char expected =3D i =3D=3D 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_V= M)); --=20 2.53.0.851.ga537e3e6e9-goog