From nobody Wed Feb 11 06:31:28 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (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 63F79392806 for ; Mon, 2 Feb 2026 22:30:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770071453; cv=none; b=MyChK2LtPmVSqMk/LEUhBLx3PDAd7Lc6j3+HU8X+M2uaTbBsoqLAd/t+tWIDE3bxW46GBqtBLNxHfLafRbb3eGSy6FLc8aWpqzPS9jTrHGaLVBNsQxGHvUuF5UHoiHMsQ7vmOJqVsrAY4qpJerfOij5NPnSkEuxIj9jKvGxuKCE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770071453; c=relaxed/simple; bh=Uvsd6bdAGEnmGmQd4Ry1auq6/yFlAXHgE1LCGfhg7pw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=duWRb+UWDhZMR9D0LUotZnTFEuyr2esEE/kTwrAFLhBgMOIDNT0KdA7ZaZ/ND1RarXcWilNtpTvxx35qmcfOADNAwG2HMN+G7bxxUGUx4SrKOLnwS2gIZyLvFl3msYcnSxBRW12JEBkqkDs4kw5fi3HO/SWBoexma1P4gEBUzfo= 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=d4BL/RcN; arc=none smtp.client-ip=209.85.214.201 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="d4BL/RcN" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2a7d7b87977so46415985ad.0 for ; Mon, 02 Feb 2026 14:30:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770071452; x=1770676252; 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=7RNUoiBoMC2uoky/O8FKXZwuVtbvGjbMSNbY2NDZ1UM=; b=d4BL/RcNLEmmqdCryKOs2zZ872MLQLfC0QDT3brKbHUvvUN5ne3pajotchiDlPqrvI LPvKkEoCV7WQPNRwPqTTPnN8+KuolTnOrZhdqT6wnwoD8Z9nGr3bAhWyAeEI/DenPhhD kFvIa9QSTerWlK0FhaV/AiQPLt3hPfbU6IPOiXAcesAwy0oCAVgL69nH+XIY7+1M5QYm 2PskjDRQi2WDt8CE9VRkvzWiLCmhvHxDVBCJo1VOMhueFFaDLNO9aAMj75GBh9mdadVv fowPOIke2OnHFmge3fVlE3ZsCVIuv5OF9ZB8I0rnn2MTPNhpCv3rMG8sx5thsuTGynEL 6slQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770071452; x=1770676252; 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=7RNUoiBoMC2uoky/O8FKXZwuVtbvGjbMSNbY2NDZ1UM=; b=S1rhWPqzHkYWMjUbMlrb+NWLAImPxnz79E8DxMpAh4kdtST8oFd2Dcww43FhCaRZXf 1bwslUNFQr23SL11oUueXGQr88/cYhNbezBbxQBc2DqK6nE/aMjTdSa4PXup/fm8fUqj ch9iQww0K8G2gSTEO2vVOXbdyaJV5kDOZ1H7PeD4Wxn/5SbvS9I1IpIhqwN2kdbp3rCl pLyeFdO9b07+vGaclzY25RJrAJuIS3As53M+fb00WnpaEyx6pjuE19YnjFrYGxeSRDyy TFbGuxuhiDAMNDU3QtNnE6OmjHrNDli4Ev+4JCHZR+A2+YMs+DDxlQnW9i5tPEdI8P1l jomQ== X-Forwarded-Encrypted: i=1; AJvYcCWP1bUZxHCA4q1oxyMDJM1DH/2hr9DeWOQy8M3fuBeWi+2ivIJBnm5aR1ITi5AErJ96f/IBZPMfKSZMrB4=@vger.kernel.org X-Gm-Message-State: AOJu0YwBK2tXV9i6s/UqQ3m9ABL6hTSMo9k1DrFmZBY7B/yVG+pzIO6y OgY9nYvP2D+BgD/bItjic4uykw9bAAOnL1TkZy7z5oOQJRHEgfx4s5lMXqK/Xo+HqSBEW4mXIPn res4Qrl4jieAVpSm5KqC3Fv8Mxw== X-Received: from pjtg1.prod.google.com ([2002:a17:90a:c301:b0:34f:8d56:3f70]) (user=ackerleytng job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2b8e:b0:34c:c514:ee1f with SMTP id 98e67ed59e1d1-3543b310da1mr12317430a91.11.1770071451845; Mon, 02 Feb 2026 14:30:51 -0800 (PST) Date: Mon, 2 Feb 2026 14:29:56 -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: <48396066e5d65920166b09cc9b02ec8a376ef619.1770071243.git.ackerleytng@google.com> Subject: [RFC PATCH v2 18/37] KVM: selftests: Add helpers for calling ioctls on guest_memfd 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Sean Christopherson Add helper functions to kvm_util.h to support calling ioctls, specifically KVM_SET_MEMORY_ATTRIBUTES2, on a guest_memfd file descriptor. Introduce gmem_ioctl() and __gmem_ioctl() macros, modeled after the existing vm_ioctl() helpers, to provide a standard way to call ioctls on a guest_memfd. Add gmem_set_memory_attributes() and its derivatives (gmem_set_private(), gmem_set_shared()) to set memory attributes on a guest_memfd region. Also provide "__" variants that return the ioctl error code instead of aborting the test. These helpers will be used by upcoming guest_memfd tests. To avoid code duplication, factor out the check for supported memory attributes into a new macro, TEST_ASSERT_SUPPORTED_ATTRIBUTES, and use it in both the existing vm_set_memory_attributes() and the new gmem_set_memory_attributes() helpers. Signed-off-by: Sean Christopherson --- .../testing/selftests/kvm/include/kvm_util.h | 89 +++++++++++++++++-- 1 file changed, 82 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing= /selftests/kvm/include/kvm_util.h index 872988197a5c..e767f9a99a7b 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -310,6 +310,16 @@ static inline bool kvm_has_cap(long cap) TEST_ASSERT(!ret, __KVM_IOCTL_ERROR(#cmd, ret)); \ }) =20 +#define __gmem_ioctl(gmem_fd, cmd, arg) \ + kvm_do_ioctl(gmem_fd, cmd, arg) + +#define gmem_ioctl(gmem_fd, cmd, arg) \ +({ \ + int ret =3D __gmem_ioctl(gmem_fd, cmd, arg); \ + \ + TEST_ASSERT(!ret, __KVM_IOCTL_ERROR(#cmd, ret)); \ +}) + static __always_inline void static_assert_is_vm(struct kvm_vm *vm) { } =20 #define __vm_ioctl(vm, cmd, arg) \ @@ -394,6 +404,14 @@ static inline void vm_enable_cap(struct kvm_vm *vm, ui= nt32_t cap, uint64_t arg0) vm_ioctl(vm, KVM_ENABLE_CAP, &enable_cap); } =20 +/* + * KVM_SET_MEMORY_ATTRIBUTES overwrites _all_ attributes. These flows need + * significant enhancements to support multiple attributes. + */ +#define TEST_ASSERT_SUPPORTED_ATTRIBUTES(attributes) \ + TEST_ASSERT(!attributes || attributes =3D=3D KVM_MEMORY_ATTRIBUTE_PRIVATE= , \ + "Update me to support multiple attributes!") + static inline void vm_set_memory_attributes(struct kvm_vm *vm, uint64_t gp= a, uint64_t size, uint64_t attributes) { @@ -404,12 +422,7 @@ static inline void vm_set_memory_attributes(struct kvm= _vm *vm, uint64_t gpa, .flags =3D 0, }; =20 - /* - * KVM_SET_MEMORY_ATTRIBUTES2 overwrites _all_ attributes. These flows - * need significant enhancements to support multiple attributes. - */ - TEST_ASSERT(!attributes || attributes =3D=3D KVM_MEMORY_ATTRIBUTE_PRIVATE, - "Update me to support multiple attributes!"); + TEST_ASSERT_SUPPORTED_ATTRIBUTES(attributes); =20 __TEST_REQUIRE(kvm_check_cap(KVM_CAP_MEMORY_ATTRIBUTES2) > 0, "No valid attributes for VM fd ioctl!"); @@ -417,7 +430,6 @@ static inline void vm_set_memory_attributes(struct kvm_= vm *vm, uint64_t gpa, vm_ioctl(vm, KVM_SET_MEMORY_ATTRIBUTES2, &attr); } =20 - static inline void vm_mem_set_private(struct kvm_vm *vm, uint64_t gpa, uint64_t size) { @@ -430,6 +442,69 @@ static inline void vm_mem_set_shared(struct kvm_vm *vm= , uint64_t gpa, vm_set_memory_attributes(vm, gpa, size, 0); } =20 +static inline int __gmem_set_memory_attributes(int fd, loff_t offset, + uint64_t size, + uint64_t attributes, + loff_t *error_offset) +{ + struct kvm_memory_attributes2 attr =3D { + .attributes =3D attributes, + .offset =3D offset, + .size =3D size, + .flags =3D 0, + }; + int r; + + TEST_ASSERT_SUPPORTED_ATTRIBUTES(attributes); + + r =3D __gmem_ioctl(fd, KVM_SET_MEMORY_ATTRIBUTES2, &attr); + if (r) + *error_offset =3D attr.error_offset; + return r; +} + +static inline int __gmem_set_private(int fd, loff_t offset, uint64_t size, + loff_t *error_offset) +{ + return __gmem_set_memory_attributes(fd, offset, size, + KVM_MEMORY_ATTRIBUTE_PRIVATE, + error_offset); +} + +static inline int __gmem_set_shared(int fd, loff_t offset, uint64_t size, + loff_t *error_offset) +{ + return __gmem_set_memory_attributes(fd, offset, size, 0, error_offset); +} + +static inline void gmem_set_memory_attributes(int fd, loff_t offset, + uint64_t size, uint64_t attributes) +{ + struct kvm_memory_attributes2 attr =3D { + .attributes =3D attributes, + .offset =3D offset, + .size =3D size, + .flags =3D 0, + }; + + TEST_ASSERT_SUPPORTED_ATTRIBUTES(attributes); + + __TEST_REQUIRE(kvm_check_cap(KVM_CAP_GUEST_MEMFD_MEMORY_ATTRIBUTES) > 0, + "No valid attributes for guest_memfd ioctl!"); + + gmem_ioctl(fd, KVM_SET_MEMORY_ATTRIBUTES2, &attr); +} + +static inline void gmem_set_private(int fd, loff_t offset, uint64_t size) +{ + gmem_set_memory_attributes(fd, offset, size, KVM_MEMORY_ATTRIBUTE_PRIVATE= ); +} + +static inline void gmem_set_shared(int fd, loff_t offset, uint64_t size) +{ + gmem_set_memory_attributes(fd, offset, size, 0); +} + void vm_guest_mem_fallocate(struct kvm_vm *vm, uint64_t gpa, uint64_t size, bool punch_hole); =20 --=20 2.53.0.rc1.225.gd81095ad13-goog