From nobody Tue Apr 7 14:25:05 2026 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 8688F375AA2 for ; Fri, 13 Mar 2026 06:13:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773382401; cv=none; b=ASlW2haYP87yFvKZpdK+BPyptrmrrVwsL0NEV0zrfdF3wFNguRZic5FGux+wyXb3cnXaFsOAeZv/ho1xm+teWZpLPJ7/nKl2E+tiAXTC4tzqwjP0Beg5/Xomx893kBOJl+i2lE2yvMXuk0JN/82Lz2aci/OTFXkqlezFDsHrYNE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773382401; c=relaxed/simple; bh=bMuIvbANV6whsHe/9Uf4i1Iz6OP8YGDNiGzY3Lul/Ts=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UYulfg/PU6eDcZbw1X/A6ZdoHLDKG2WimDH1W+qlAGE/EYgSdWuccCh0qhkKwwuJd0rPX5HpAyV0wm9Hy4/Z2XGD3ifT7PSNPocVvAYdyQqIo0f3PrueyAP+GfiTxQbpk9bear+q3BK1xXm5pZcex2KPNisxFt0dkbX63TzBwqw= 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=Adlp9YqF; arc=none smtp.client-ip=209.85.210.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="Adlp9YqF" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-829ba0e63e8so789541b3a.2 for ; Thu, 12 Mar 2026 23:13:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773382399; x=1773987199; 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=QknoZXBKlPCGwx1irT4+gYrnFv4rWV1IvMUMC4bd+4I=; b=Adlp9YqFVZ/0kZVl1gOaiLeaP02UyV+gwAzakx1CwddnL7kBVC/leVdTcm6c1LxJTJ MQXYiTPquZFkfpLGQc8q+4dgoT3paEPBtRSO6T9/DJFs/Wt2tS09WvrMeY56DdJWlEMo 6Ykr2b6QgWBgwhj2gi+fmz+mR1hC5rbmeeEF5nxdjJ72eiLtITraskG5TTLyD+EOgHZG RicR8o6AY1nL9abTP2kDX8b3ZUaf7+Ad4zEI3Pv9Z9XGErADj/gxPo88rvI/9fSak64d VPAQ/9GETTt6G2JGSfrBSkqKZm0SvNvc46PAQ6iGaF0O87LcBEGlEtj3SyM1lib7wkX5 pxng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773382399; x=1773987199; 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=QknoZXBKlPCGwx1irT4+gYrnFv4rWV1IvMUMC4bd+4I=; b=VolraLqm/6bTImayK+BLusBXadKeViB6cFINWllwE6cdi5ctTv0XlK+13MfV9gmtB7 bvDBzLJ0xNDvtejH3PtffZ66zTbLSYf7N62o200BViGmIlpMw+jVmp7G9uKqz28Rm507 FBpSf/AnHETldDvIEgGHAO2ZdObxU78uB1QlFb27XCIakHx5oTfIp//P0V1ovZF2QyN7 Auam96M943J1nskmSowYvKNcSDOlirqrpoNxINg5GgWpFnUaQXg3iBkpCD9pszUvI8tt GFLBbYqCmJjOTYj0FMhrZ5UQPapMTMpnWqYpbKUmIO/9hqj04x2l9JJLKRGpOyeeEcQf 7npA== X-Forwarded-Encrypted: i=1; AJvYcCU6BO1UD7Lvpw1Pdx2BJ0C3BpeQCTtqJha2KdcIDjSlIgt7wlKusTFnLip6ePUMLgaHWIDBT/ZOn2La3V0=@vger.kernel.org X-Gm-Message-State: AOJu0YxX3iIBkpcuA9/iYEvG5bH133uwBzE7uKP+txJDwIykusRhX+j7 7c9DcRF2WcsR+ndnLTw/6pIBryatO9enrZYIDvya6uLh5GhceCQ3mkSY6xVfE4F3UAOHhOWDeZ5 KSes3Ci6bWQjBNLb87r1oCpwtYw== X-Received: from pfld5.prod.google.com ([2002:a05:6a00:1985:b0:7e8:b7f1:f957]) (user=ackerleytng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:23d2:b0:829:6f28:1d6 with SMTP id d2e1a72fcca58-82a196f5730mr1947629b3a.13.1773382398356; Thu, 12 Mar 2026 23:13:18 -0700 (PDT) Date: Fri, 13 Mar 2026 06:12:57 +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=5329; i=ackerleytng@google.com; s=20260225; h=from:subject:message-id; bh=3sSrOJAlZS2IXh3VCeWaAhxTVEFs3kS8NsgUAhaH5Jc=; b=AN1YnbEfb1F2TkaQQpPX1DyWGYGoMBr3dXAuDgbZOWFOOvW8Ejji0gH/z7UKRx+wEouyz10+t GVh+DUFSNPcAaHLANfsWx2imPLwzue1iOqejIRs/eDerfBkVTX/eb7Y X-Mailer: b4 0.14.3 Message-ID: <20260313-gmem-inplace-conversion-v3-18-5fc12a70ec89@google.com> Subject: [PATCH RFC v3 18/43] KVM: selftests: Add helpers for calling ioctls on guest_memfd 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 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 --- tools/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 fc55ad3c67485..1c0f41bf847d0 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -333,6 +333,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) \ @@ -417,6 +427,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) { @@ -427,12 +445,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!"); @@ -440,7 +453,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) { @@ -453,6 +465,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.851.ga537e3e6e9-goog