From nobody Thu Apr 2 11:18:35 2026 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 790EE33F385 for ; Thu, 26 Mar 2026 22:25:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774563931; cv=none; b=ceOS8LVnPoHoiQzOUyitYSVARbUyQmia3K4PiN5ryTF/86juVKbkSTsSvO0eNbcJhgmNygV/ixtx/9d8mEYY6zn3vKwjCfSYFwM10VJOnlfSHIMiA8PpENXNYL1pwPkolET6o8Hd6ftIxJW+mo5j2W2Id0nto72MM27+grSb/Nc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774563931; c=relaxed/simple; bh=49y6ENDTk+sGjQthlPC0PI5YjXBfXeC7oiHabUVd5+c=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kkAM/lIZM4j0sVwnVzHPGNRfVKE/A6Grxc2zCV7Z7twuA+hloIUfiGK9wIqDB5ylRLsz8eNEK2GDnieBI08tYo5I5iTvkGr5Yit+y+uyn4y0b+5M7OVSpwzK6PXkaW8yt12NQOj0z2EfmIbYEk+fro0PZ5h888dRldYMv6t/40o= 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=PFYxi+OZ; arc=none smtp.client-ip=209.85.210.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="PFYxi+OZ" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-82a782029b7so1139724b3a.3 for ; Thu, 26 Mar 2026 15:25:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1774563930; x=1775168730; 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=4FDVrh5pBAWxA/8NukVOzbt4j2jYZyIOrjhQG/FDGfA=; b=PFYxi+OZI7E39KcF7SwuJM6KlnAtjVcLnzJrrY3ivJMkF5vKIaAs/RdQS92OU2sprk 5ygz2YS6MEAgjnsRIgpXHJiY1AH7wsDTZSUT8sIn+r0anmFwdTyuMJVjQKEv872OS6Yr kEUIRLFEkPETycsa+ww5BzUfmv6AlOKgFResu4AAdcjBKpzKUSuqwjXdlYwsc8OtP/W4 6HFXPHDnMDQ0EzQNNYPxmxM56jsJmklIe7fWsBZFMW1ri6ay7N/CvQNS+GMbjnKZ6zt9 aFVZ7OD9Dbiv8+YBZI1QDIpBMKKwbyOH+Z4taXowbQ6D/5NwYOoiOKJNm43KC3EAKoHQ WQqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774563930; x=1775168730; 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=4FDVrh5pBAWxA/8NukVOzbt4j2jYZyIOrjhQG/FDGfA=; b=lsw6uytkCtUY+24jKcPhcT0NUikNbpQ1PrLSHjPw7sTIPvfafKJGquo4UDXDkarW3j WNf5UqRYleUHsHeiADsB+msXscBFHjvOUyTY6mi/vsrsH9+Lt34sC9IFqx2JeQrjxHzQ U4bi9kqX9h7Zy+wGAAyuxEDHmMuhX/zaf8b+Wha+EiNB9GEc40BWHr0cgHsjMzDrJXQS EINlnEiFA6WGo0OB+nHj5AG/cDKo1PlyuTg6BT2cSKwW0Oba7P5H2NjlfA5R6iUtixuq 7lMjXRlKyBEF1c06AWQmt1CMmxM2AI2Sb80Y87VwjWKh/fz20vMpJ5mcLi2OSXLMiW9B pKGg== X-Forwarded-Encrypted: i=1; AJvYcCXbp+TzjWfsvgPZurCY3+Ivz7yYnVhbKshxQmdfd5N4u1VG22tWIHoM3vJzDmSVqmjGCrDTiz8jdqABD00=@vger.kernel.org X-Gm-Message-State: AOJu0Yz7VJWG7QeKAb51qLlcQibXXJGKPAWWdgLFNcq5EDJGBS74HKVI uV7NgP/JXB66fa84rRjQcgVs9JW+5Y1IBZhYqmMZ0OY7guaF0nOYb6eMp1TvcesNG1bubkiOwbe 3iiTTzThV8iEGCklC7tOxGEn2MA== X-Received: from pfbmy24-n1.prod.google.com ([2002:a05:6a00:6d58:10b0:82c:85db:66f1]) (user=ackerleytng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:a118:b0:824:188b:c173 with SMTP id d2e1a72fcca58-82c95d35fa3mr154180b3a.5.1774563929625; Thu, 26 Mar 2026 15:25:29 -0700 (PDT) Date: Thu, 26 Mar 2026 15:24:41 -0700 In-Reply-To: <20260326-gmem-inplace-conversion-v4-0-e202fe950ffd@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260326-gmem-inplace-conversion-v4-0-e202fe950ffd@google.com> X-Developer-Key: i=ackerleytng@google.com; a=ed25519; pk=sAZDYXdm6Iz8FHitpHeFlCMXwabodTm7p8/3/8xUxuU= X-Developer-Signature: v=1; a=ed25519-sha256; t=1774563861; l=3818; i=ackerleytng@google.com; s=20260225; h=from:subject:message-id; bh=49y6ENDTk+sGjQthlPC0PI5YjXBfXeC7oiHabUVd5+c=; b=65aFdTZaaBhbx01UOD21wSDI5gmFYvI5UY4Oon4kj35EozZ/PT0v5jtWiriWeTssIZ8LZ5iDi nW+/95/cI0ZBsoUfv6UZ6e3zDpiwjAXZks4pArcDWAYh9DVJGtT36bz X-Mailer: b4 0.14.3 Message-ID: <20260326-gmem-inplace-conversion-v4-32-e202fe950ffd@google.com> Subject: [PATCH RFC v4 32/44] 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 , Andrew Morton , Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , 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, 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 | 80 ++++++++++++++++++= ++++ 1 file changed, 80 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 ed310f26d88a1..4ccd3d515bb26 100644 --- a/tools/testing/selftests/kvm/guest_memfd_conversions_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_conversions_test.c @@ -408,6 +408,86 @@ 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, + KVM_SET_MEMORY_ATTRIBUTES2_PRESERVE); + } 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, + KVM_SET_MEMORY_ATTRIBUTES2_PRESERVE); + + 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.1018.g2bb0e51243-goog