[PATCH RFC v3 20/88] samples/kvm: Introduce memory provider module skeleton

From: Ackerley Tng via B4 Relay

Date: Fri Nov 06 2026 - 19:56:11 EST


From: Fred Griffoul <fgriffo@xxxxxxxxxxxx>

Introduce the memory provider sample module skeleton under samples/kvm/.

The memory provider module will serve as a sample in-tree memory provider
for guest_memfd and iommufd. Register a miscdevice at /dev/memory_provider
upon module initialization and deregister it upon module exit.

Co-developed-by: Ackerley Tng <ackerleytng@xxxxxxxxxx>
Signed-off-by: Ackerley Tng <ackerleytng@xxxxxxxxxx>
Signed-off-by: Fred Griffoul <fgriffo@xxxxxxxxxxxx>
---
samples/Kconfig | 11 +++++++++++
samples/Makefile | 1 +
samples/kvm/Makefile | 2 ++
samples/kvm/memory_provider.c | 35 +++++++++++++++++++++++++++++++++++
4 files changed, 49 insertions(+)

diff --git a/samples/Kconfig b/samples/Kconfig
index a75e8e78330da..d4e79e1a9286b 100644
--- a/samples/Kconfig
+++ b/samples/Kconfig
@@ -326,6 +326,17 @@ source "samples/rust/Kconfig"

source "samples/damon/Kconfig"

+config SAMPLE_KVM_MEMORY_PROVIDER
+ tristate "Build sample memory provider -- loadable module only"
+ depends on CONTIG_ALLOC && X86_64 && m
+ help
+ This builds a sample memory provider.
+ Its files can back a guest_memfd and be mapped by iommufd.
+ It allocates a contiguous region with alloc_contig_pages()
+ and carves out child provider files on the fly.
+
+ If unsure, say N.
+
endif # SAMPLES

config HAVE_SAMPLE_FTRACE_DIRECT
diff --git a/samples/Makefile b/samples/Makefile
index 07641e177bd8b..3933ef6a323ed 100644
--- a/samples/Makefile
+++ b/samples/Makefile
@@ -37,6 +37,7 @@ obj-$(CONFIG_SAMPLE_TPS6594_PFSM) += pfsm/
subdir-$(CONFIG_SAMPLE_WATCHDOG) += watchdog
subdir-$(CONFIG_SAMPLE_WATCH_QUEUE) += watch_queue
obj-$(CONFIG_SAMPLE_KMEMLEAK) += kmemleak/
+obj-$(CONFIG_SAMPLE_KVM_MEMORY_PROVIDER) += kvm/
obj-$(CONFIG_SAMPLE_CORESIGHT_SYSCFG) += coresight/
obj-$(CONFIG_SAMPLE_FPROBE) += fprobe/
obj-$(CONFIG_SAMPLES_RUST) += rust/
diff --git a/samples/kvm/Makefile b/samples/kvm/Makefile
new file mode 100644
index 0000000000000..9668dc57aa458
--- /dev/null
+++ b/samples/kvm/Makefile
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0
+obj-$(CONFIG_SAMPLE_KVM_MEMORY_PROVIDER) += memory_provider.o
diff --git a/samples/kvm/memory_provider.c b/samples/kvm/memory_provider.c
new file mode 100644
index 0000000000000..d5272cb88b758
--- /dev/null
+++ b/samples/kvm/memory_provider.c
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * memory_provider - a sample memory provider.
+ *
+ * Confidential VMs are not supported.
+ */
+
+#include <linux/fs.h>
+#include <linux/miscdevice.h>
+#include <linux/module.h>
+
+static const struct file_operations mp_ctl_fops = {
+ .owner = THIS_MODULE,
+};
+
+static struct miscdevice mp_dev = {
+ .minor = MISC_DYNAMIC_MINOR,
+ .name = "memory_provider",
+ .fops = &mp_ctl_fops,
+};
+
+static int __init mp_init(void)
+{
+ return misc_register(&mp_dev);
+}
+module_init(mp_init);
+
+static void __exit mp_exit(void)
+{
+ misc_deregister(&mp_dev);
+}
+module_exit(mp_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Sample memory provider for guest_memfd and iommufd");

--
2.56.0.455.g6c5a503b38-goog