Skip to contentSkip to conversation

mm: Refactor KVM guest_memfd to introduce guestmem library

RFC PATCH In review Kalyazin, Nikita 3 participants 5 messages Sep 15, 2025 – Sep 16, 2025

Subsystems (9)
lore
Share & tools
lore
#0 lore raw

v6 cover mm: Refactor KVM guest_memfd to introduce guestmem library

This is a revival of the guestmem library patch series originated from Elliot [1]. The reason I am bringing it up now is it would help implement UserfaultFD support minor mode in guest_memfd.

Background

We are building a Firecracker version that uses guest_memfd to back guest memory [2]. The main objective is to use guest_memfd to remove guest memory from host kernel's direct map to reduce the surface for Spectre-style transient execution issues [3]. Currently, Firecracker supports restoring VMs from snapshots using UserfaultFD [4], which is similar to the postcopy phase of live migration. During restoration, while we rely on a separate mechanism to handle stage-2 faults in guest_memfd [5], UserfaultFD support in guest_memfd is still required to handle faults caused either by the VMM itself or by MMIO access handling on x86.

The major problem in implementing UserfaultFD for guest_memfd is that the MM code (UserfaultFD) needs to call KVM-specific interfaces. Particularly for the minor mode, these are 1) determining the type of the VMA (eg is_vma_guest_memfd()) and 2) obtaining a folio (ie kvm_gmem_get_folio()). Those may not be always available as KVM can be compiled as a module. Peter attempted to approach it via exposing an ops structure where modules (such as KVM) could provide their own callbacks, but it was not deemed to be sufficiently safe as it opens up an unrestricted interface for all modules and may leave MM in an inconsistent state [6].

An alternative way to make these interfaces available to the UserfaultFD code is extracting generic-MM guest_memfd parts into a library (guestmem) under MM where they can be safely consumed by the UserfaultFD code. As far as I know, the original guestmem library series was motivated by adding guest_memfd support in Gunyah hypervisor [7].

This RFC

I took Elliot's v5 (the latest) and rebased it on top of the guest_memfd preview branch [8] because I also wanted to see how it would work with direct map removal [3] and write syscall [9], which are building blocks for the guest_memfd-based Firecracker version. On top of it I added a patch that implements UserfaultFD support for guest_memfd using interfaces provided by the guestmem library to illustrate the complete idea.

I made the following modifications along the way:
- Followed by a comment from Sean, converted invalidate_begin() callback back to void as it cannot fail in KVM, and the related Gunyah requirement is unknown to me
- Extended the guestmem_ops structure with the supports_mmap() callback to provide conditional mmap support in guestmem
- Extended the guestmem library interface with guestmem_allocate(), guestmem_test_no_direct_map(), guestmem_mark_prepared(), guestmem_mmap(), and guestmem_vma_is_guestmem()
- Made (kvm_gmem)/(guestmem)_test_no_direct_map() use mapping_no_direct_map() instead of KVM-specific flag GUEST_MEMFD_FLAG_NO_DIRECT_MAP to make it KVM-independent

Feedback that I would like to receive:
- Is this the right solution to the "UserfaultFD in guest_memfd" problem?
- What requirements from other hypervisors than KVM do we need to consider at this point?
- Does the line between generic-MM and KVM-specific guest_memfd parts look sensible?

Previous iterations of UserfaultFD support in guest_memfd patches:
v3:
- https://lore.kernel.org/kvm/[email protected]
- minor changes to address review comments (James)
v2:
- https://lore.kernel.org/kvm/[email protected]
- implement a full minor trap instead of hybrid missing/minor trap (James/Peter)
- make UFFDIO_CONTINUE implementation generic calling vm_ops->fault()
v1:
- https://lore.kernel.org/kvm/[email protected]

Nikita

[1]: https://lore.kernel.org/kvm/[email protected]
[2]: https://github.com/firecracker-microvm/firecracker/tree/feature/secret-hiding
[3]: https://lore.kernel.org/kvm/[email protected]
[4]: https://github.com/firecracker-microvm/firecracker/blob/main/docs/snapshotting/handling-page-faults-on-snapshot-resume.md
[5]: https://lore.kernel.org/kvm/[email protected]
[6]: https://lore.kernel.org/linux-mm/[email protected]
[7]: https://lore.kernel.org/lkml/[email protected]
[8]: https://git.kernel.org/pub/scm/linux/kernel/git/david/linux.git/log/?h=guestmemfd-preview
[9]: https://lore.kernel.org/kvm/[email protected]

Nikita Kalyazin (2):
mm: guestmem: introduce guestmem library
userfaulfd: add minor mode for guestmem

 Documentation/admin-guide/mm/userfaultfd.rst |   4 +-
 MAINTAINERS                                  |   2 +
 fs/userfaultfd.c                             |   3 +-
 include/linux/guestmem.h                     |  46 +++
 include/linux/userfaultfd_k.h                |   8 +-
 include/uapi/linux/userfaultfd.h             |   8 +-
 mm/Kconfig                                   |   3 +
 mm/Makefile                                  |   1 +
 mm/guestmem.c                                | 380 +++++++++++++++++++
 mm/userfaultfd.c                             |  14 +-
 virt/kvm/Kconfig                             |   1 +
 virt/kvm/guest_memfd.c                       | 303 ++-------------
 12 files changed, 493 insertions(+), 280 deletions(-)
 create mode 100644 include/linux/guestmem.h
 create mode 100644 mm/guestmem.c

base-commit: 911634bac3107b237dcd8fdcb6ac91a22741cbe7

Signature · 1 line
2.50.1
#1 lore raw

RFC PATCH v6 1/2 mm: guestmem: introduce guestmem library

From: Nikita Kalyazin <[email protected]>

Move MM-generic parts of guest_memfd from KVM to MM. This allows other hypervisors to use guestmem code and enables UserfaultFD implementation for guest_memfd [1]. Previously it was not possible because KVM (and guest_memfd code) might be built as a module.

Based on a patch by Elliot Berman <[email protected]> [2].

[1] https://lore.kernel.org/kvm/[email protected]
[2] https://lore.kernel.org/kvm/[email protected]

---
 MAINTAINERS              |   2 +
 include/linux/guestmem.h |  46 +++++
 mm/Kconfig               |   3 +
 mm/Makefile              |   1 +
 mm/guestmem.c            | 380 +++++++++++++++++++++++++++++++++++++++
 virt/kvm/Kconfig         |   1 +
 virt/kvm/guest_memfd.c   | 303 ++++---------------------------
 7 files changed, 465 insertions(+), 271 deletions(-)
 create mode 100644 include/linux/guestmem.h
 create mode 100644 mm/guestmem.c
MAINTAINERS +2 -0
index fed6cd812d79..c468c4847ffd 100644
@@ -15956,6 +15956,7 @@ W:	http://www.linux-mm.org
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
 T:	quilt git://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new
 F:	mm/
+F:	mm/guestmem.c
 F:	tools/mm/
 
 MEMORY MANAGEMENT - CORE
@@ -15973,6 +15974,7 @@ W:	http://www.linux-mm.org
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
 F:	include/linux/gfp.h
 F:	include/linux/gfp_types.h
+F:	include/linux/guestmem.h
 F:	include/linux/highmem.h
 F:	include/linux/memory.h
 F:	include/linux/mm.h
include/linux/guestmem.h new file +46 -0

46 lines not on this page · raw message

mm/Kconfig +3 -0
index e443fe8cd6cf..a3705099601f 100644
@@ -1254,6 +1254,9 @@ config SECRETMEM
 	  memory areas visible only in the context of the owning process and
 	  not mapped to other processes and other kernel page tables.
 
+config GUESTMEM
+	bool
+
 config ANON_VMA_NAME
 	bool "Anonymous VMA name support"
 	depends on PROC_FS && ADVISE_SYSCALLS && MMU
mm/Makefile +1 -0
index ef54aa615d9d..c92892acd819 100644
@@ -138,6 +138,7 @@ obj-$(CONFIG_PERCPU_STATS) += percpu-stats.o
 obj-$(CONFIG_ZONE_DEVICE) += memremap.o
 obj-$(CONFIG_HMM_MIRROR) += hmm.o
 obj-$(CONFIG_MEMFD_CREATE) += memfd.o
+obj-$(CONFIG_GUESTMEM) += guestmem.o
 obj-$(CONFIG_MAPPING_DIRTY_HELPERS) += mapping_dirty_helpers.o
 obj-$(CONFIG_PTDUMP) += ptdump.o
 obj-$(CONFIG_PAGE_REPORTING) += page_reporting.o
mm/guestmem.c new file +380 -0

380 lines not on this page · raw message

virt/kvm/Kconfig +1 -0
index 1b7d5be0b6c4..41e26ad33c1b 100644
@@ -114,6 +114,7 @@ config KVM_GENERIC_MEMORY_ATTRIBUTES
 
 config KVM_GUEST_MEMFD
        select XARRAY_MULTI
+       select GUESTMEM
        bool
 
 config HAVE_KVM_ARCH_GMEM_PREPARE
virt/kvm/guest_memfd.c +32 -271

402 lines not on this page · raw message

Signature · 1 line
2.50.1
#2 lore raw

Re: [RFC PATCH v6 1/2] mm: guestmem: introduce guestmem library

Quoted 6 lines · Nikita

From: Nikita Kalyazin <[email protected]>

Move MM-generic parts of guest_memfd from KVM to MM. This allows other hypervisors to use guestmem code and enables UserfaultFD implementation for guest_memfd [1]. Previously it was not possible because KVM (and guest_memfd code) might be built as a module.

https://lore.kernel.org/kvm/[email protected]/
remains un-addressed.

ie this entire approach is garbage. throw it away and start again.

#3 lore raw

RFC PATCH v6 2/2 userfaulfd: add minor mode for guestmem

In reply to Kalyazin, Nikita

From: Nikita Kalyazin <[email protected]>

UserfaultFD support in guestmem enables use cases like restoring a guest_memfd-backed VM from a memory snapshot in Firecracker [1] where an external process is responsible for supplying the content of the guest memory or live migration of guest_memfd-backed VMs.

[1] https://github.com/firecracker-microvm/firecracker/blob/main/docs/snapshotting/handling-page-faults-on-snapshot-resume.md

---
 Documentation/admin-guide/mm/userfaultfd.rst |  4 +++-
 fs/userfaultfd.c                             |  3 ++-
 include/linux/userfaultfd_k.h                |  8 +++++---
 include/uapi/linux/userfaultfd.h             |  8 +++++++-
 mm/userfaultfd.c                             | 14 +++++++++++---
 5 files changed, 28 insertions(+), 9 deletions(-)
Documentation/admin-guide/mm/userfaultfd.rst +3 -1
index e5cc8848dcb3..ca8c5954ffdb 100644
@@ -111,7 +111,9 @@ events, except page fault notifications, may be generated:
 - ``UFFD_FEATURE_MINOR_HUGETLBFS`` indicates that the kernel supports
   ``UFFDIO_REGISTER_MODE_MINOR`` registration for hugetlbfs virtual memory
   areas. ``UFFD_FEATURE_MINOR_SHMEM`` is the analogous feature indicating
-  support for shmem virtual memory areas.
+  support for shmem virtual memory areas. ``UFFD_FEATURE_MINOR_GUESTMEM``
+  is the analogous feature indicating support for guestmem-backed memory
+  areas.
 
 - ``UFFD_FEATURE_MOVE`` indicates that the kernel supports moving an
   existing page contents from userspace.
fs/userfaultfd.c +2 -1
index 54c6cc7fe9c6..e4e80f1072a6 100644
@@ -1978,7 +1978,8 @@ static int userfaultfd_api(struct userfaultfd_ctx *ctx,
 	uffdio_api.features = UFFD_API_FEATURES;
 #ifndef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR
 	uffdio_api.features &=
-		~(UFFD_FEATURE_MINOR_HUGETLBFS | UFFD_FEATURE_MINOR_SHMEM);
+		~(UFFD_FEATURE_MINOR_HUGETLBFS | UFFD_FEATURE_MINOR_SHMEM |
+		  UFFD_FEATURE_MINOR_GUESTMEM);
 #endif
 #ifndef CONFIG_HAVE_ARCH_USERFAULTFD_WP
 	uffdio_api.features &= ~UFFD_FEATURE_PAGEFAULT_FLAG_WP;
include/linux/userfaultfd_k.h +5 -3
index c0e716aec26a..37bd4e71b611 100644
@@ -14,6 +14,7 @@
 #include <linux/userfaultfd.h> /* linux/include/uapi/linux/userfaultfd.h */
 
 #include <linux/fcntl.h>
+#include <linux/guestmem.h>
 #include <linux/mm.h>
 #include <linux/swap.h>
 #include <linux/swapops.h>
@@ -218,7 +219,8 @@ static inline bool vma_can_userfault(struct vm_area_struct *vma,
 		return false;
 
 	if ((vm_flags & VM_UFFD_MINOR) &&
-	    (!is_vm_hugetlb_page(vma) && !vma_is_shmem(vma)))
+	    (!is_vm_hugetlb_page(vma) && !vma_is_shmem(vma) &&
+		!guestmem_vma_is_guestmem(vma)))
 		return false;
 
 	/*
@@ -238,9 +240,9 @@ static inline bool vma_can_userfault(struct vm_area_struct *vma,
 		return false;
 #endif
 
-	/* By default, allow any of anon|shmem|hugetlb */
+	/* By default, allow any of anon|shmem|hugetlb|guestmem */
 	return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) ||
-	    vma_is_shmem(vma);
+	    vma_is_shmem(vma) || guestmem_vma_is_guestmem(vma);
 }
 
 static inline bool vma_has_uffd_without_event_remap(struct vm_area_struct *vma)
include/uapi/linux/userfaultfd.h +7 -1
index 2841e4ea8f2c..0fe9fbd29772 100644
@@ -42,7 +42,8 @@
 			   UFFD_FEATURE_WP_UNPOPULATED |	\
 			   UFFD_FEATURE_POISON |		\
 			   UFFD_FEATURE_WP_ASYNC |		\
-			   UFFD_FEATURE_MOVE)
+			   UFFD_FEATURE_MOVE |			\
+			   UFFD_FEATURE_MINOR_GUESTMEM)
 #define UFFD_API_IOCTLS				\
 	((__u64)1 << _UFFDIO_REGISTER |		\
 	 (__u64)1 << _UFFDIO_UNREGISTER |	\
@@ -230,6 +231,10 @@ struct uffdio_api {
 	 *
 	 * UFFD_FEATURE_MOVE indicates that the kernel supports moving an
 	 * existing page contents from userspace.
+	 *
+	 * UFFD_FEATURE_MINOR_GUESTMEM indicates the same support as
+	 * UFFD_FEATURE_MINOR_HUGETLBFS, but for guestmem-backed pages
+	 * instead.
 	 */
 #define UFFD_FEATURE_PAGEFAULT_FLAG_WP		(1<<0)
 #define UFFD_FEATURE_EVENT_FORK			(1<<1)
@@ -248,6 +253,7 @@ struct uffdio_api {
 #define UFFD_FEATURE_POISON			(1<<14)
 #define UFFD_FEATURE_WP_ASYNC			(1<<15)
 #define UFFD_FEATURE_MOVE			(1<<16)
+#define UFFD_FEATURE_MINOR_GUESTMEM		(1<<17)
 	__u64 features;
 
 	__u64 ioctls;
mm/userfaultfd.c +11 -3
index 45e6290e2e8b..304e5d7dbb70 100644
@@ -388,7 +388,14 @@ static int mfill_atomic_pte_continue(pmd_t *dst_pmd,
 	struct page *page;
 	int ret;
 
-	ret = shmem_get_folio(inode, pgoff, 0, &folio, SGP_NOALLOC);
+	if (guestmem_vma_is_guestmem(dst_vma)) {
+		ret = 0;
+		folio = guestmem_grab_folio(inode->i_mapping, pgoff);
+		if (IS_ERR(folio))
+			ret = PTR_ERR(folio);
+	} else {
+		ret = shmem_get_folio(inode, pgoff, 0, &folio, SGP_NOALLOC);
+	}
 	/* Our caller expects us to return -EFAULT if we failed to find folio */
 	if (ret == -ENOENT)
 		ret = -EFAULT;
@@ -766,9 +773,10 @@ static __always_inline ssize_t mfill_atomic(struct userfaultfd_ctx *ctx,
 		return  mfill_atomic_hugetlb(ctx, dst_vma, dst_start,
 					     src_start, len, flags);
 
-	if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma))
+	if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma)
+	    && !guestmem_vma_is_guestmem(dst_vma))
 		goto out_unlock;
-	if (!vma_is_shmem(dst_vma) &&
+	if (!vma_is_shmem(dst_vma) && !guestmem_vma_is_guestmem(dst_vma) &&
 	    uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE))
 		goto out_unlock;
 
Signature · 1 line
2.50.1
reviewer
#4 lore raw
In reply to Kalyazin, Nikita

Hello, Nikita,

Quoted 17 lines · Nikita

This is a revival of the guestmem library patch series originated from Elliot [1]. The reason I am bringing it up now is it would help implement UserfaultFD support minor mode in guest_memfd.

Background

We are building a Firecracker version that uses guest_memfd to back guest memory [2]. The main objective is to use guest_memfd to remove guest memory from host kernel's direct map to reduce the surface for Spectre-style transient execution issues [3]. Currently, Firecracker supports restoring VMs from snapshots using UserfaultFD [4], which is similar to the postcopy phase of live migration. During restoration, while we rely on a separate mechanism to handle stage-2 faults in guest_memfd [5], UserfaultFD support in guest_memfd is still required to handle faults caused either by the VMM itself or by MMIO access handling on x86.

Quoted 10 lines

The major problem in implementing UserfaultFD for guest_memfd is that the MM code (UserfaultFD) needs to call KVM-specific interfaces. Particularly for the minor mode, these are 1) determining the type of the VMA (eg is_vma_guest_memfd()) and 2) obtaining a folio (ie kvm_gmem_get_folio()). Those may not be always available as KVM can be compiled as a module. Peter attempted to approach it via exposing an ops structure where modules (such as KVM) could provide their own callbacks, but it was not deemed to be sufficiently safe as it opens up an unrestricted interface for all modules and may leave MM in an inconsistent state [6].

I apologize when I was replying to your offlist email that I'll pick it up, but I didn't.. I moved on with other things after the long off which was more urgent, then I never got the chance to go back.. I will do it this week.

I don't think it's a real safety issue. Frankly, I still think that latest patchset, as-is, is the best we should come up with userfaultfd. If people worry about uffdio_copy(), it's fine, we can drop it. It's not a huge deal at least for now.

Btw, thanks for help pinging that thread, and sorry I didn't yet get back to it. I'll read the discussions (I didn't yet, after back to work for weeks), but I will.

Quoted 9 lines

An alternative way to make these interfaces available to the UserfaultFD code is extracting generic-MM guest_memfd parts into a library (guestmem) under MM where they can be safely consumed by the UserfaultFD code. As far as I know, the original guestmem library series was motivated by adding guest_memfd support in Gunyah hypervisor [7].

This RFC

Quoted 7 lines

I took Elliot's v5 (the latest) and rebased it on top of the guest_memfd preview branch [8] because I also wanted to see how it would work with direct map removal [3] and write syscall [9], which are building blocks for the guest_memfd-based Firecracker version. On top of it I added a patch that implements UserfaultFD support for guest_memfd using interfaces provided by the guestmem library to illustrate the complete idea.

I hope patch 2 exactly illustrated on why the uffd modulization effort is still worthwhile (to not keep attaching "if"s all over the places). Would you agree?

If you agree, we'll need to review the library work as a separate effort from userfaultfd.

Quoted 14 lines

I made the following modifications along the way:
- Followed by a comment from Sean, converted invalidate_begin() callback back to void as it cannot fail in KVM, and the related Gunyah requirement is unknown to me
- Extended the guestmem_ops structure with the supports_mmap() callback to provide conditional mmap support in guestmem
- Extended the guestmem library interface with guestmem_allocate(), guestmem_test_no_direct_map(), guestmem_mark_prepared(), guestmem_mmap(), and guestmem_vma_is_guestmem()
- Made (kvm_gmem)/(guestmem)_test_no_direct_map() use mapping_no_direct_map() instead of KVM-specific flag GUEST_MEMFD_FLAG_NO_DIRECT_MAP to make it KVM-independent

Quoted 3 lines

Feedback that I would like to receive:
- Is this the right solution to the "UserfaultFD in guest_memfd" problem?

Yes it's always a fair question to ask. I shared my two cents above. We can definitely also hear about how others think.

I hope I'll keep my words this time on reposting.

Thanks,

Quoted 56 lines

- What requirements from other hypervisors than KVM do we need to consider at this point?
- Does the line between generic-MM and KVM-specific guest_memfd parts look sensible?

Previous iterations of UserfaultFD support in guest_memfd patches:
v3:
- https://lore.kernel.org/kvm/[email protected]
- minor changes to address review comments (James)
v2:
- https://lore.kernel.org/kvm/[email protected]
- implement a full minor trap instead of hybrid missing/minor trap (James/Peter)
- make UFFDIO_CONTINUE implementation generic calling vm_ops->fault()
v1:
- https://lore.kernel.org/kvm/[email protected]

Nikita

[1]: https://lore.kernel.org/kvm/[email protected]
[2]: https://github.com/firecracker-microvm/firecracker/tree/feature/secret-hiding
[3]: https://lore.kernel.org/kvm/[email protected]
[4]: https://github.com/firecracker-microvm/firecracker/blob/main/docs/snapshotting/handling-page-faults-on-snapshot-resume.md
[5]: https://lore.kernel.org/kvm/[email protected]
[6]: https://lore.kernel.org/linux-mm/[email protected]
[7]: https://lore.kernel.org/lkml/[email protected]
[8]: https://git.kernel.org/pub/scm/linux/kernel/git/david/linux.git/log/?h=guestmemfd-preview
[9]: https://lore.kernel.org/kvm/[email protected]

Nikita Kalyazin (2):
mm: guestmem: introduce guestmem library
userfaulfd: add minor mode for guestmem

 Documentation/admin-guide/mm/userfaultfd.rst |   4 +-
 MAINTAINERS                                  |   2 +
 fs/userfaultfd.c                             |   3 +-
 include/linux/guestmem.h                     |  46 +++
 include/linux/userfaultfd_k.h                |   8 +-
 include/uapi/linux/userfaultfd.h             |   8 +-
 mm/Kconfig                                   |   3 +
 mm/Makefile                                  |   1 +
 mm/guestmem.c                                | 380 +++++++++++++++++++
 mm/userfaultfd.c                             |  14 +-
 virt/kvm/Kconfig                             |   1 +
 virt/kvm/guest_memfd.c                       | 303 ++-------------
 12 files changed, 493 insertions(+), 280 deletions(-)
 create mode 100644 include/linux/guestmem.h
 create mode 100644 mm/guestmem.c

base-commit: 911634bac3107b237dcd8fdcb6ac91a22741cbe7

Signature · 1 line
2.50.1
Signature · 1 line
Peter Xu