[PATCH 3/4] mm/hugetlb: Support vm_uffd_ops API

From: Peter Xu
Date: Wed Jun 11 2025 - 10:18:40 EST


Add support for the new vm_uffd_ops API for hugetlb. Note that this only
introduces the support, the API is not yet used by core mm.

Cc: Muchun Song <muchun.song@xxxxxxxxx>
Cc: Oscar Salvador <osalvador@xxxxxxx>
Signed-off-by: Peter Xu <peterx@xxxxxxxxxx>
---
mm/hugetlb.c | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 3d61ec17c15a..b9e473fab871 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -5459,6 +5459,22 @@ static vm_fault_t hugetlb_vm_op_fault(struct vm_fault *vmf)
return 0;
}

+#ifdef CONFIG_USERFAULTFD
+static const vm_uffd_ops hugetlb_uffd_ops = {
+ .uffd_features = __VM_UFFD_FLAGS,
+ /* _UFFDIO_ZEROPAGE not supported */
+ .uffd_ioctls = BIT(_UFFDIO_COPY) |
+ BIT(_UFFDIO_WRITEPROTECT) |
+ BIT(_UFFDIO_CONTINUE) |
+ BIT(_UFFDIO_POISON),
+ /*
+ * Hugetlbfs still has its own hard-coded handler in userfaultfd,
+ * due to limitations similar to vm_operations_struct.fault().
+ * TODO: generalize it to use the API functions.
+ */
+};
+#endif
+
/*
* When a new function is introduced to vm_operations_struct and added
* to hugetlb_vm_ops, please consider adding the function to shm_vm_ops.
@@ -5472,6 +5488,9 @@ const struct vm_operations_struct hugetlb_vm_ops = {
.close = hugetlb_vm_op_close,
.may_split = hugetlb_vm_op_split,
.pagesize = hugetlb_vm_op_pagesize,
+#ifdef CONFIG_USERFAULTFD
+ .userfaultfd_ops = &hugetlb_uffd_ops,
+#endif
};

static pte_t make_huge_pte(struct vm_area_struct *vma, struct folio *folio,
--
2.49.0