From nobody Wed Feb 11 05:18:00 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 5AEA33446CE for ; Fri, 17 Oct 2025 20:13:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760732004; cv=none; b=LJzojrau6k55Uue7llmhrLkJ1Mc5Vt4Y6Epr9woqTwLujq/5bsD1Ep58iD0q6RdjXSO0p2UNGvNNKB7pUx6XmrLoGlTkdLoEZ2W/BDVI5Sb6VnSJv2bh6P6JiUSOD5kzWFMnOC9lRJJCkt+nDv/8Qds7AbYNSDY+p8Toa+Zu1mo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760732004; c=relaxed/simple; bh=htWfv+Kz/+8mCGgXv5yxc0WFOgcnBeuLeS5xitlSAOU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RGxer6Nvo4Cw23giMzTvDXFZ9TkXD7m/7aq4vzbC97k2gELEXe9uAH2Kx0vHD2cxPEgCF+SSzCvlpsmJBbfET0LeGub5MIAU9JIo0GyxmpIGVIrwjWWD4o7WEabvOaeoHvwDcq6dXMDA81Klubyzf2OM+1PvztnWuvqWh8JaeUQ= 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=FRLCDZHy; arc=none smtp.client-ip=209.85.216.74 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="FRLCDZHy" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-33ba9047881so3096603a91.1 for ; Fri, 17 Oct 2025 13:13:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1760731994; x=1761336794; 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=9/dkg7bO1Q/m68vFvjbiJ5siG3cxkpNKvttryWm72LM=; b=FRLCDZHyT2uekXSfWvnAflP80+xnxJ1XgSXoDgyAg/BDqm13tT1h4yXW2coiw359qF JAx/nGV7tCqYor1nJdLIeKiresbmQzRjHtFMlD79YG7dqwYlTctXSEAgmrYyTqvjBN9B 3LrxBLchJgHioDHSBJRsKfqaxSTH5h2FWDBOkRNO/Ct6i35I4gptSeJlYsfh46KLavO5 E4KSlPUCb0UQVqdEvJEIT4oTa3dlgAOo0OXYFcAd6hxYr4vHFf52SSWodljdybSIP6DT cUIoZp72HObXtnbfAQly8/B9vgiLZGr9wSXZmoXadTLxLbmJKdDS1jd+U0cPx5So2en7 jCKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760731994; x=1761336794; 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=9/dkg7bO1Q/m68vFvjbiJ5siG3cxkpNKvttryWm72LM=; b=iA7T0xvCEOjh/xpax4gX5SZFN2e19pIzovLfKSscgwHIgMqrie/MvC+R19IWVkrH1m M2ZJmbcPUIfaSRU4jWuJvc5evJlYllK+gWtSg8PpEJFrVLtRNi0lZIUolgBnMCQJCwNR qhZ7hKlxd7sNfhv3vX6ybLHnBediPmgLoT35a+9UOsf3lH9wDWjZM/C8OFl74wnNt+uK Ec4UyIUszfy6muuvXBVE1L3wsfS9ylZUIllk8HIXBDqXE2VZmUd4S+VlKwYwWwIxzXx5 kHClQU1teB4ofhlTXvfeUfDO/MFGBhqRl3tjz82In670ym1iwQvHctBP+70tv4xchfPC P7+w== X-Forwarded-Encrypted: i=1; AJvYcCUtvadHDVWE8T1m8R8rS88VhFXtqUTnDV5ajdqeVp95WDk83EEvCaCfsuYFutXl5fqE/Yy9Jq7lQmt6Qvo=@vger.kernel.org X-Gm-Message-State: AOJu0YzPq4/EwuO3pvLqC1RwF6FwL3MkghZ0meLmCDwrP5UXBWwG6Yyz Inqvb2hvgFO6rP0rJk5EjcYm2kkwGymzx695BEEvA1KoYQmRoJrGXD9BcNSo5XzewOgXBo5lcqI cLaCEsRd1ZDxnw0e9gUS/ficiyg== X-Google-Smtp-Source: AGHT+IHVYfbeJHjmJQIxf3lnI6xML61YGFIRkStopZSXJJvTgLI8jTgXjo0mAYsj6xfs9PsRIMTiStrABfsfvN8xtQ== X-Received: from pjbqx13.prod.google.com ([2002:a17:90b:3e4d:b0:32d:e4c6:7410]) (user=ackerleytng job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:28c5:b0:33b:c5c7:511a with SMTP id 98e67ed59e1d1-33bc5c75327mr6623268a91.15.1760731993183; Fri, 17 Oct 2025 13:13:13 -0700 (PDT) Date: Fri, 17 Oct 2025 13:12:08 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.51.0.858.gf9c4a03a3a-goog Message-ID: Subject: [RFC PATCH v1 27/37] KVM: selftests: guest_memfd: Test conversion with elevated page refcount From: Ackerley Tng To: cgroups@vger.kernel.org, kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org, x86@kernel.org Cc: ackerleytng@google.com, akpm@linux-foundation.org, binbin.wu@linux.intel.com, bp@alien8.de, brauner@kernel.org, chao.p.peng@intel.com, chenhuacai@kernel.org, corbet@lwn.net, dave.hansen@intel.com, dave.hansen@linux.intel.com, david@redhat.com, dmatlack@google.com, erdemaktas@google.com, fan.du@intel.com, fvdl@google.com, haibo1.xu@intel.com, hannes@cmpxchg.org, hch@infradead.org, hpa@zytor.com, hughd@google.com, ira.weiny@intel.com, isaku.yamahata@intel.com, jack@suse.cz, james.morse@arm.com, jarkko@kernel.org, jgg@ziepe.ca, jgowans@amazon.com, jhubbard@nvidia.com, jroedel@suse.de, jthoughton@google.com, jun.miao@intel.com, kai.huang@intel.com, keirf@google.com, kent.overstreet@linux.dev, liam.merwick@oracle.com, maciej.wieczor-retman@intel.com, mail@maciej.szmigiero.name, maobibo@loongson.cn, mathieu.desnoyers@efficios.com, maz@kernel.org, mhiramat@kernel.org, mhocko@kernel.org, mic@digikod.net, michael.roth@amd.com, mingo@redhat.com, mlevitsk@redhat.com, mpe@ellerman.id.au, muchun.song@linux.dev, nikunj@amd.com, nsaenz@amazon.es, oliver.upton@linux.dev, palmer@dabbelt.com, pankaj.gupta@amd.com, paul.walmsley@sifive.com, pbonzini@redhat.com, peterx@redhat.com, pgonda@google.com, prsampat@amd.com, pvorel@suse.cz, qperret@google.com, richard.weiyang@gmail.com, rick.p.edgecombe@intel.com, rientjes@google.com, rostedt@goodmis.org, roypat@amazon.co.uk, rppt@kernel.org, seanjc@google.com, shakeel.butt@linux.dev, shuah@kernel.org, steven.price@arm.com, steven.sistare@oracle.com, suzuki.poulose@arm.com, tabba@google.com, tglx@linutronix.de, thomas.lendacky@amd.com, vannapurve@google.com, vbabka@suse.cz, viro@zeniv.linux.org.uk, vkuznets@redhat.com, wei.w.wang@intel.com, will@kernel.org, willy@infradead.org, wyihan@google.com, xiaoyao.li@intel.com, yan.y.zhao@intel.com, yilun.xu@intel.com, yuzenghui@huawei.com, zhiquan1.li@intel.com Content-Transfer-Encoding: quoted-printable 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, for example via pin_user_pages(), the conversion must be aborted. This test uses the gup_test debugfs interface (which requires CONFIG_GUP_TEST) to call pin_user_pages() on a specific page, artificially increasing its reference count. It then attempts to convert a range of pages, including the pinned page, 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 --- .../kvm/guest_memfd_conversions_test.c | 82 +++++++++++++++++++ 1 file changed, 82 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 e6abf2d30c62d..856166f1b1dfc 100644 --- a/tools/testing/selftests/kvm/guest_memfd_conversions_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_conversions_test.c @@ -14,6 +14,7 @@ #include "kselftest_harness.h" #include "test_util.h" #include "ucall_common.h" +#include "../../../../mm/gup_test.h" =20 FIXTURE(gmem_conversions) { struct kvm_vcpu *vcpu; @@ -404,6 +405,87 @@ GMEM_CONVERSION_TEST_INIT_SHARED(forked_accesses) kvm_munmap(test_state, sizeof(*test_state)); } =20 +static int gup_test_fd; + +static void pin_pages(void *vaddr, uint64_t size) +{ + const struct pin_longterm_test args =3D { + .addr =3D (uint64_t)vaddr, + .size =3D size, + .flags =3D PIN_LONGTERM_TEST_FLAG_USE_WRITE, + }; + + gup_test_fd =3D open("/sys/kernel/debug/gup_test", O_RDWR); + TEST_REQUIRE(gup_test_fd >=3D 0); + + TEST_ASSERT_EQ(ioctl(gup_test_fd, PIN_LONGTERM_TEST_START, &args), 0); +} + +static void unpin_pages(void) +{ + if (gup_test_fd > 0) + TEST_ASSERT_EQ(ioctl(gup_test_fd, PIN_LONGTERM_TEST_STOP), 0); +} + +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); +} + +/* + * This test depends on CONFIG_GUP_TEST to provide a kernel module that ex= poses + * pin_user_pages() to userspace. + */ +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.51.0.858.gf9c4a03a3a-goog