[PATCH RFC v2 45/50] samples/kvm: Implement attach and release provider ops in sample provider

From: Ackerley Tng via B4 Relay

Date: Thu Oct 22 2026 - 19:51:34 EST


From: Ackerley Tng <ackerleytng@xxxxxxxxxx>

Implement the guest_memfd provider operations in the sample memory
provider module, providing .attach and .release callbacks.

In .attach, take a reference to the provider file (get_file) to keep the
provider context alive while attached. In .release, release the file
reference via fput.

Attach the provider operations to mps_file_fops.gmem_provider_ops so that
provider file descriptors created by the sample module can be passed as
resource_fd to guest_memfd.

Signed-off-by: Ackerley Tng <ackerleytng@xxxxxxxxxx>
---
samples/kvm/mem_provider_sample.c | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)

diff --git a/samples/kvm/mem_provider_sample.c b/samples/kvm/mem_provider_sample.c
index 397ebb3773f39..d9e57484c3933 100644
--- a/samples/kvm/mem_provider_sample.c
+++ b/samples/kvm/mem_provider_sample.c
@@ -17,6 +17,7 @@
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/gfp.h>
+#include <linux/guest_memfd.h>
#include <linux/io.h>
#include <linux/miscdevice.h>
#include <linux/mm.h>
@@ -118,11 +119,30 @@ static long mps_file_ioctl(struct file *file, unsigned int cmd,
}
}

+static void *mps_gmem_attach(struct inode *gmem_inode,
+ struct file *resource_file)
+{
+ return get_file(resource_file);
+}
+
+static void mps_gmem_release(void *provider)
+{
+ struct file *resource_file = provider;
+
+ fput(resource_file);
+}
+
+static const struct guest_memfd_provider_operations mps_gmem_provider_ops = {
+ .attach = mps_gmem_attach,
+ .release = mps_gmem_release,
+};
+
static const struct file_operations mps_file_fops = {
.owner = THIS_MODULE,
.release = mps_file_release,
.unlocked_ioctl = mps_file_ioctl,
.compat_ioctl = compat_ptr_ioctl,
+ .gmem_provider_ops = &mps_gmem_provider_ops,
};

static long mps_ctl_new_file(void __user *uarg)

--
2.56.0.257.gf7497c7d27-goog