From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qt1-f179.google.com (mail-qt1-f179.google.com [209.85.160.179]) (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 B9BC03328FA for ; Wed, 24 Jun 2026 19:27:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=209.85.160.179 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782329226; cv=pass; b=hFpkyLvJTBwm/y1XAqOQWc9utU/QRn3CFgHX2Tre3uu/fZEhMeCnG1ProM9+ITOL7LdL4UpSmUxzypGtgh8KaAUOHchNMjixnfJxp5Z99EsChP1bl6ckDXBRYbn99yk9RwMXTNpXvAD9mamTD8czH4Zduan0BEotiCg4IHcb4DY= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782329226; c=relaxed/simple; bh=ElsN0o6O4qn1GAusSskW5oVYn1nRJxsJndIkIHl3pxw=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=YWCA26pcUt+nx4OycdDPEOKMDLPgoD3HSWCnSasMiNU7TjViletMkArM8NzFHtoyjXhJa2gzGoi88KMaBjF5ncn9gWkXAkmXD4ZXrkIXWCH5iwe6WPIuCrtjMjR3IIcpndEi7/pkQZxSdEHhBT/bvHl1J43V+jC4cOetboVqPg0= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=oCwSxvub; arc=pass smtp.client-ip=209.85.160.179 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=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="oCwSxvub" Received: by mail-qt1-f179.google.com with SMTP id d75a77b69052e-519ed52bcc6so51701cf.0 for ; Wed, 24 Jun 2026 12:27:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1782329224; cv=none; d=google.com; s=arc-20260327; b=UmlQsGk9LtdJm4A6K6ViY9YOwf5X0twYsYk9qkco/P2p0G6LegCTc42NGzun7hAiDp Efm1Anz57q6ZerXtO8LdXwUCP8cOEnLKH+/PQ+RzQdSgZ3BhGtuHtHRHQVEjE3YVD6CJ MbRiKXOHHICP52O0Er9ywXaYUu072SP4p3Xvyde3IOJBT/SgSYOpiIukLFYUF93gVvpD nfKbk9U98g5ibYLg7LiHYaRMhBw6FZZgcEfZzwx0HQfJqyNCE/FWLn0vqzzFEngmAg2O H2nmN1T4dKaJSnZXCk9gxhwG1/Elifhij259YuP2TqhcJS50Z4wan2NK/dHYTMmvke+L BKlA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=iIfO0sWE9plcUue5+b+JHfK+yQSCgxqkYKTUdxso5zE=; fh=G30b8XvKEDdosSMg/9GXtmHiyidCBFJtjAMlS3todtA=; b=s2T1WbmN4cC3F9DNq1nGQvHsXsV739bfAs65mg3o13hUtOA+Vs8KGSQHjWD/4E5H/I SANIhVZev7nGRu2hVmuATmLvgNbZskza0nT7JfuXk1kpg1xfi0SdsFZQC7OBTmhWJBed eZzBVNZlcaNtuOrPk4sLdje79aJkvh0fICOxkvW5GPUIA+jgUI8qsqjwmGoBIp+HyCwa /PwbyL62yhyxY/vpr8OZ/7guwXH6Mu7IWj5RfTNCQFPppI8YryfYrKqtEf0AXD4T7H0D +S8aTy+wZPA1+nOsR4NIuPbBFy/xEPO4HI22hyS3DUe/Cx3oJmyYlGN+NyXn3QGXkkjV AY3w==; darn=vger.kernel.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1782329224; x=1782934024; darn=vger.kernel.org; h=content-type:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:from:to:cc:subject:date:message-id:reply-to :content-type; bh=iIfO0sWE9plcUue5+b+JHfK+yQSCgxqkYKTUdxso5zE=; b=oCwSxvubBKZ/slXdjWYuYTV+2dKZvv7L+Snaajg3h+VCERJ1vyl1EjSNLB1Xejhh6Q lJPujfjtDylOaFJHi54qZLmfB5vNymNckVDbYW1f/3MN7uQq7E+/AV5ljbE2TExexd3m WF71wwjfL+KmLS8AxL8a0TAmjTuASN/u4J9/x0BvMH7DWrgoEg97fnibFq/Kmwnv4XxI ZtuijIaUzsGKfzgfAqwldnVsWi30Tj/pWFqxduDW8tU3yU5BHwfliIlIOSi7omcg0Rf8 cbR70qWHv2KMVtW2/xmsGlnyvgTLi0JjdfgOhw/hKvmTMyCKAg0gaJVruBIQ40BiYiLJ IpSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782329224; x=1782934024; h=content-type:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to:content-type; bh=iIfO0sWE9plcUue5+b+JHfK+yQSCgxqkYKTUdxso5zE=; b=LHhQHIER7n6gZ3iO/4MdUX8mAMeSLj0n/7Z4HdKgZsXN8o0R5OHpYrIsg/MCq9tmLw PIfPE7BTN+Oy+7QEVKKUI00z0Apy+/+Jw4d3eoomnmvxE9oSTduHnOWil+4BYz855Uhx ubQNLabLmPvn4iqlKTn7V3rQz6Zw7ZNHOER5lBITPAp5ZKWc6v8EHXgi6ZQQWH267ZZ7 mHGd0HAXWneE/KlnIRvXvtfBq0MFsGctJoakrhw1BvPQ9jaiksFbV5kMpeAGuBi2OaWx ojp1eugM5R1VI5UEO+Jo6POk8Pqhn2sanzFe0Lh5YtAW1S6eKhJg/MIvgpH0kX3I6d/z wFnw== X-Forwarded-Encrypted: i=1; AFNElJ9TYJ/d49KD8P7qjcXYkl8fuvzF906wCVda1mPeteVWun59PlFU/TTVv+6nunGTnu49FJ8KKC5qUjyx0Ho=@vger.kernel.org X-Gm-Message-State: AOJu0Yx1zmo60EB7iyRA/l/y9nghI47FsaGuh6Q3k9Sslo3ftsQ7wqle M6rpgKrnlBkOs6TXj17RO7CiYhCcc27pi+5TbWYAoqvxYPuaPmCXPdJ0ymLJdxgyA8jwKpH+KxT mkMT6efMgHxtf047z2b+sS5fuYvYJcy9L3YVn2r0s X-Gm-Gg: AfdE7clG0EjkWBv5z23ZH2jePu2OpydUp7EyZ8suikosNAt9TvxjeWZ/hTNVnCpd/sm wOYCzFABjo49KyxawbhwUqUHGLfXurjVCI9xLzmGnb5jGx8hcHO+pPsoR28pBBuEOOVA/+C4IxW GGJuzQM03j5Zg9yltmVvTUIXrVSY9kQ7XAYfh0x994HmpgOwLIOFyg2778x+ueeivZ7isndtSdH OzFpgflwUEA/FnLooZCNFLLF4z6zpO460EBA0cU1D9camDkTALUEGL1QHP9724YSehzn6LHXw== X-Received: by 2002:a05:622a:250c:b0:516:d25f:f038 with SMTP id d75a77b69052e-51a6e9b48c8mr1807551cf.10.1782329222912; Wed, 24 Jun 2026 12:27:02 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20260618-gmem-inplace-conversion-v8-0-9d2959357853@google.com> <20260618-gmem-inplace-conversion-v8-30-9d2959357853@google.com> In-Reply-To: <20260618-gmem-inplace-conversion-v8-30-9d2959357853@google.com> From: Fuad Tabba Date: Wed, 24 Jun 2026 20:26:26 +0100 X-Gm-Features: AVVi8Cek2cuMarnZ-Pi4P7GaLyk3HJGX0nYgRKl7Bk0MKMGsDMyF-sq_kvfaaNA Message-ID: Subject: Re: [PATCH v8 30/46] KVM: selftests: Add helpers for calling ioctls on guest_memfd To: ackerleytng@google.com Cc: aik@amd.com, andrew.jones@linux.dev, binbin.wu@linux.intel.com, brauner@kernel.org, chao.p.peng@linux.intel.com, david@kernel.org, jmattson@google.com, 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, 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, liam@infradead.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 , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Youngjun Park , Qi Zheng , Shakeel Butt , Kiryl Shutsemau , Baoquan He , Jason Gunthorpe , Vlastimil Babka , 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, linux-coco@lists.linux.dev Content-Type: text/plain; charset="UTF-8" On Fri, 19 Jun 2026 at 01:32, Ackerley Tng via B4 Relay wrote: > > 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 > Signed-off-by: Ackerley Tng Reviewed-by: Fuad Tabba Cheers, /fuad > --- > tools/testing/selftests/kvm/include/kvm_util.h | 94 +++++++++++++++++++++++--- > 1 file changed, 86 insertions(+), 8 deletions(-) > > diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h > index 0cacf3698b259..323d06b5699ec 100644 > --- a/tools/testing/selftests/kvm/include/kvm_util.h > +++ b/tools/testing/selftests/kvm/include/kvm_util.h > @@ -392,6 +392,16 @@ static __always_inline void static_assert_is_vcpu(struct kvm_vcpu *vcpu) { } > __TEST_ASSERT_VM_VCPU_IOCTL(!ret, #cmd, ret, (vcpu)->vm); \ > }) > > +#define __gmem_ioctl(gmem_fd, cmd, arg) \ > + kvm_do_ioctl(gmem_fd, cmd, arg) > + > +#define gmem_ioctl(gmem_fd, cmd, arg) \ > +({ \ > + int ret = __gmem_ioctl(gmem_fd, cmd, arg); \ > + \ > + TEST_ASSERT(!ret, __KVM_IOCTL_ERROR(#cmd, ret)); \ > +}) > + > /* > * Looks up and returns the value corresponding to the capability > * (KVM_CAP_*) given by cap. > @@ -418,8 +428,16 @@ static inline void vm_enable_cap(struct kvm_vm *vm, u32 cap, u64 arg0) > vm_ioctl(vm, KVM_ENABLE_CAP, &enable_cap); > } > > +/* > + * KVM_SET_MEMORY_ATTRIBUTES{,2} overwrites _all_ attributes. These > + * flows need significant enhancements to support multiple attributes. > + */ > +#define TEST_ASSERT_SUPPORTED_ATTRIBUTES(attributes) \ > + TEST_ASSERT(!(attributes) || (attributes) == KVM_MEMORY_ATTRIBUTE_PRIVATE, \ > + "Update me to support multiple attributes!") > + > static inline void vm_set_memory_attributes(struct kvm_vm *vm, gpa_t gpa, > - u64 size, u64 attributes) > + size_t size, u64 attributes) > { > struct kvm_memory_attributes attr = { > .attributes = attributes, > @@ -428,17 +446,11 @@ static inline void vm_set_memory_attributes(struct kvm_vm *vm, gpa_t gpa, > .flags = 0, > }; > > - /* > - * KVM_SET_MEMORY_ATTRIBUTES overwrites _all_ attributes. These flows > - * need significant enhancements to support multiple attributes. > - */ > - TEST_ASSERT(!attributes || attributes == KVM_MEMORY_ATTRIBUTE_PRIVATE, > - "Update me to support multiple attributes!"); > + TEST_ASSERT_SUPPORTED_ATTRIBUTES(attributes); > > vm_ioctl(vm, KVM_SET_MEMORY_ATTRIBUTES, &attr); > } > > - > static inline void vm_mem_set_private(struct kvm_vm *vm, gpa_t gpa, > u64 size) > { > @@ -451,6 +463,72 @@ static inline void vm_mem_set_shared(struct kvm_vm *vm, gpa_t gpa, > vm_set_memory_attributes(vm, gpa, size, 0); > } > > +static inline int __gmem_set_memory_attributes(int fd, u64 offset, > + size_t size, u64 attributes, > + u64 *error_offset) > +{ > + struct kvm_memory_attributes2 attr = { > + .attributes = attributes, > + .offset = offset, > + .size = size, > + .flags = 0, > + .error_offset = 0, > + }; > + int r; > + > + r = __gmem_ioctl(fd, KVM_SET_MEMORY_ATTRIBUTES2, &attr); > + > + /* Copy error_offset regardless of r so caller can check. */ > + if (error_offset) > + *error_offset = attr.error_offset; > + > + return r; > +} > + > +static inline int __gmem_set_private(int fd, u64 offset, size_t size, > + u64 *error_offset) > +{ > + return __gmem_set_memory_attributes(fd, offset, size, > + KVM_MEMORY_ATTRIBUTE_PRIVATE, > + error_offset); > +} > + > +static inline int __gmem_set_shared(int fd, u64 offset, size_t size, > + u64 *error_offset) > +{ > + return __gmem_set_memory_attributes(fd, offset, size, 0, > + error_offset); > +} > + > +static inline void gmem_set_memory_attributes(int fd, u64 offset, > + size_t size, u64 attributes) > +{ > + struct kvm_memory_attributes2 attr = { > + .attributes = attributes, > + .offset = offset, > + .size = size, > + .flags = 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, u64 offset, size_t size) > +{ > + gmem_set_memory_attributes(fd, offset, size, > + KVM_MEMORY_ATTRIBUTE_PRIVATE); > +} > + > +static inline void gmem_set_shared(int fd, u64 offset, size_t size) > +{ > + gmem_set_memory_attributes(fd, offset, size, 0); > +} > + > void vm_guest_mem_fallocate(struct kvm_vm *vm, gpa_t gpa, u64 size, > bool punch_hole); > > > -- > 2.55.0.rc0.738.g0c8ab3ebcc-goog > >