[PATCH RFC v3 53/88] KVM: guest_memfd: Support check_set_attributes provider operation

From: Ackerley Tng via B4 Relay

Date: Fri Nov 06 2026 - 19:49:51 EST


From: Ackerley Tng <ackerleytng@xxxxxxxxxx>

Allow memory providers to validate or reject attribute changes on a
range of memory offsets before they are committed.

Add an optional .check_set_attributes callback to struct
guest_memfd_provider_operations returning an int error code, and invoke
it via gmem_provider_check_set_attributes() under the exclusive filemap
invalidation lock before preallocating maple tree entries or unmapping
userspace mappings. If the provider rejects the attribute change with an
error, fail the ioctl with that error code and report the error index
back to userspace.

Signed-off-by: Ackerley Tng <ackerleytng@xxxxxxxxxx>
---
include/linux/guest_memfd.h | 12 ++++++++++++
virt/kvm/guest_memfd.c | 16 ++++++++++++++++
2 files changed, 28 insertions(+)

diff --git a/include/linux/guest_memfd.h b/include/linux/guest_memfd.h
index a2af279cd3c34..cd634576c9af8 100644
--- a/include/linux/guest_memfd.h
+++ b/include/linux/guest_memfd.h
@@ -88,6 +88,16 @@ static inline bool kvm_gmem_pfn_is_cacheable(u64 pfn_attrs)
* @mmap: Optional callback to set up user mappings. Called when userspace calls
* mmap() on guest_memfd. The provider can customize VMA flags, or reject
* the mapping.
+ * @check_set_attributes: Optional callback to validate attribute changes on a
+ * range of indices before they are committed. Called
+ * with the exclusive filemap invalidate lock held. The
+ * provider can inspect the requested attributes and
+ * offset range and return 0 to allow the transition or
+ * a negative error code (e.g. -EINVAL or -EPERM) to
+ * reject it. Once @check_set_attributes succeeds, the
+ * provider must ensure this result stays valid and
+ * constant until guest_memfd stops using the range. Do
+ * not take locks held across kvm_gmem_revoke().
*
* Used by filesystems and device drivers that provide memory for guest_memfd.
*/
@@ -103,6 +113,8 @@ struct guest_memfd_provider_operations {
unsigned long *pfn, size_t *nr_pages);
void (*put_pfn_range)(void *provider, pgoff_t index, size_t nr_pages);
int (*mmap)(void *provider, struct vm_area_struct *vma);
+ int (*check_set_attributes)(void *provider, pgoff_t index,
+ size_t nr_pages, u64 attributes);
};

#ifdef CONFIG_KVM_GUEST_MEMFD
diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
index ec908419e5faa..61a3ff7fcd84c 100644
--- a/virt/kvm/guest_memfd.c
+++ b/virt/kvm/guest_memfd.c
@@ -210,6 +210,16 @@ static int gmem_provider_mmap(struct gmem_inode *gi, struct vm_area_struct *vma)
return 0;
}

+static int gmem_provider_check_set_attributes(struct gmem_inode *gi, pgoff_t index,
+ size_t nr_pages, u64 attributes)
+{
+ if (gi->provider_ops && gi->provider_ops->check_set_attributes)
+ return gi->provider_ops->check_set_attributes(gi->provider, index,
+ nr_pages, attributes);
+
+ return 0;
+}
+
#define kvm_gmem_for_each_file(f, inode) \
list_for_each_entry(f, &GMEM_I(inode)->gmem_file_list, entry)

@@ -1083,6 +1093,12 @@ static int __kvm_gmem_set_attributes(struct inode *inode, pgoff_t start,
if (kvm_gmem_range_has_attributes(inode, start, nr_pages, attrs))
goto out;

+ r = gmem_provider_check_set_attributes(gi, start, nr_pages, attrs);
+ if (r) {
+ *err_index = start;
+ goto out;
+ }
+
mas_init(&mas, mt, start);
r = kvm_gmem_mas_preallocate(&mas, attrs, start, nr_pages);
if (r) {

--
2.56.0.455.g6c5a503b38-goog