From nobody Wed Feb 11 07:50:15 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 C90D938BF9F for ; Mon, 2 Feb 2026 22:31:08 +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=1770071470; cv=none; b=JC3yXMaRKgreji3M4y8oYY40dJK+EGxQtlpvDN2Zb6jCi6pXHp4/4391fI7J8099/8iQbKR2OuyTau+OpDyB/yJgV0avKsYef6GgsfmDVr0ni9MUYBGdmQNCglCQ2Df5GIL+E7MS6MHluCU3DwPxJGmVgDi3QW+q/oVHLFR7IL0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770071470; c=relaxed/simple; bh=qRnEczXlduOW3C0O9spHmofImFcnn7rXn4Gw2hh/jro=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KHEQ4JGgsSmsMdfTzWJ1c46qGLb0TXBUIpJt6EoZ58GdC5T6e8o5t7lav2BoNm8D5ifmMJwr+FciI0L9NhMc1WZgYuMnQ6AT8McC9gN1n+4ixVgXq/6jTfVfYd5toFrfmwEefjdQqx5xbs2OIdeTzKWa7M6LBLhtiubi3FmzMlM= 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=2Cpg0OSb; 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="2Cpg0OSb" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-353a5c295e4so158769a91.0 for ; Mon, 02 Feb 2026 14:31:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770071468; x=1770676268; 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=I3H0YZPf5vKYzMrAOphWj1h1SEeefAlujnu5HOJZAJU=; b=2Cpg0OSbqEjlLcQIAkSI4PgNVskzNa+RHb0MpjW6XtILcYda6B+1na2wzvX7awou+V QHt8GZDTBaJV5UCEMKw9+KsRkfNGipKJ7c6NA4CXHng7yy2Kemncwa/zbHjfcRHFuA/U AzSNnvUQtDeyLUXpz1fVi1uyNUbbsRZaee4lMU+VTDw6/kYLnmuVpY3XYZSo8k6GYOLx mziOhQz/5cOclT5Wl/3q0sSYRl4ZvP91qGEM0djVMBD+Dd2cPvdeTFZJ0Bmj8JbvIdIv Vj8LK58FQL16lx4mLv85JrxSEJEe+wOMtpZ47Ra1H9av36LFI1rWacNqCGz6zUmOCDxB nZjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770071468; x=1770676268; 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=I3H0YZPf5vKYzMrAOphWj1h1SEeefAlujnu5HOJZAJU=; b=ZVWMTWrjHx2UJs3ssNRY5WFq8PGx9aG2r+6j/DacD+5RlzrqBohj7yGrTW4Ov1kcWs //Zv+o9ipVU7ea6gvpxhxX5IIcHZRkgNnu/1v1mB4t78QdzsTX8YRC1DR4JIbpp92Yg8 ZooP3lP2kkEVyeUZcNWmphRw7ad4OZ+SRmFADsN7z41M8Kq/YKaz4tp+SCViSIFQ0tnn I22bdrhM7wnS8Z1KV0/NT88D5cDpdmvLTQ1AjKi+mjvZ3TNbPYxO+Kd+a80ts/x4LmE9 Cr2fwGI5ihrntnTT6QzGYjAwj0kayqO95TnVBtCJfJce/blSs6pPuj4vWrRlxr/7olrJ /vMg== X-Forwarded-Encrypted: i=1; AJvYcCU+8v50buWGRjfqGsIK4/fjp7gdqvaGZPgm3x5uZxSVFZBIJ/+XxoMaN+CU+vx8okkZsVGMcFBBGf9ODIA=@vger.kernel.org X-Gm-Message-State: AOJu0YzDxHSQYSMMtYiI6+XN/o7THtssqXvD4CDDQMpwsEG9bgdjetrJ wAjCbALsrHutifk/mbCVJs9CzXGtG00lHVhofGLGhwyHzu2JEeeH5Jxc3tgRoBB3FbjNGNN5nu3 tWJAyOwF5PsrW9y0zmJUt0gNbmA== X-Received: from plho3.prod.google.com ([2002:a17:903:23c3:b0:2a0:a0e0:a9c3]) (user=ackerleytng job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:c94a:b0:2a7:a87a:423 with SMTP id d9443c01a7336-2a92465cb91mr7857185ad.19.1770071467885; Mon, 02 Feb 2026 14:31:07 -0800 (PST) Date: Mon, 2 Feb 2026 14:30:06 -0800 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.53.0.rc1.225.gd81095ad13-goog Message-ID: <69033e02150097aeb2b261ead30cd37c20d007c7.1770071243.git.ackerleytng@google.com> Subject: [RFC PATCH v2 28/37] KVM: selftests: Test conversion with elevated page refcount From: Ackerley Tng To: kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org, x86@kernel.org Cc: aik@amd.com, andrew.jones@linux.dev, binbin.wu@linux.intel.com, bp@alien8.de, brauner@kernel.org, chao.p.peng@intel.com, chao.p.peng@linux.intel.com, chenhuacai@kernel.org, corbet@lwn.net, dave.hansen@linux.intel.com, david@kernel.org, hpa@zytor.com, ira.weiny@intel.com, jgg@nvidia.com, jmattson@google.com, jroedel@suse.de, jthoughton@google.com, maobibo@loongson.cn, mathieu.desnoyers@efficios.com, maz@kernel.org, mhiramat@kernel.org, michael.roth@amd.com, mingo@redhat.com, mlevitsk@redhat.com, oupton@kernel.org, pankaj.gupta@amd.com, pbonzini@redhat.com, prsampat@amd.com, qperret@google.com, ricarkol@google.com, rick.p.edgecombe@intel.com, rientjes@google.com, rostedt@goodmis.org, seanjc@google.com, shivankg@amd.com, shuah@kernel.org, steven.price@arm.com, tabba@google.com, tglx@linutronix.de, vannapurve@google.com, vbabka@suse.cz, willy@infradead.org, wyihan@google.com, yan.y.zhao@intel.com, Ackerley Tng 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 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 --- .../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 c1a9cc7c9fae..872747432545 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.rc1.225.gd81095ad13-goog