[PATCH v3 9/9] mm/userfaultfd: check the hugetlb alignment of every VMA in the pre-scan
From: Tal Zussman
Date: Tue Nov 17 2026 - 23:29:56 EST
UFFDIO_REGISTER and UFFDIO_UNREGISTER both scan the VMAs of the range
before changing anything. For a hugetlb VMA, userfaultfd_register()
rejects a range whose start or end is not huge page aligned there.
However, userfaultfd_unregister() only checks the start.
An unaligned end only fails later, when vma_modify_flags_uffd() cannot
split the VMA there. At that point, userfaultfd_clear_vma() already
cleared the WP or RWP protection. The ioctl returns -EINVAL and the VMA
stays registered, but accesses to its pages are no longer reported. This
requires a caller passing a misaligned range.
To fix it, move the alignment checks into vma_uffd_range_aligned() and
call it for every VMA in the pre-scan of both ioctls. UFFDIO_UNREGISTER
still returns -EINVAL for such a range, but no longer unregisters the
VMAs before the misaligned one first.
Checking every VMA the same way also changes the following:
- UFFDIO_UNREGISTER now returns -EINVAL if the end is not huge page
aligned in a hugetlb VMA that is not registered. It used to skip that
VMA and succeed.
- Both ioctls now accept a start that is not huge page aligned if it is
not inside any VMA and the first VMA of the range is a hugetlb one.
They used to return -EINVAL, although such a range covers that VMA
from its start, which is always huge page aligned.
- UFFDIO_REGISTER now returns -EINVAL rather than -EPERM if the end is
not huge page aligned in a hugetlb VMA without VM_MAYWRITE.
Fixes: cab350afcbc9 ("userfaultfd: hugetlbfs: allow registration of ranges containing huge pages")
Fixes: f369b07c8614 ("mm/uffd: reset write protection when unregister with wp-mode")
Suggested-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
Signed-off-by: Tal Zussman <tz2294@xxxxxxxxxxxx>
---
mm/userfaultfd.c | 54 ++++++++++++++++++------------------------------------
1 file changed, 18 insertions(+), 36 deletions(-)
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index ada841a9cb2a..b80bcc1e03d0 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -3692,6 +3692,20 @@ static __always_inline int validate_range(struct mm_struct *mm,
return validate_unaligned_range(mm, start, len);
}
+static bool vma_uffd_range_aligned(struct vm_area_struct *vma,
+ unsigned long start, unsigned long end)
+{
+ unsigned long size;
+
+ if (!vma_is_hugetlb(vma))
+ return true;
+
+ /* A hugetlb VMA can only be split at a huge page boundary. */
+ size = huge_page_size(hstate_vma(vma));
+ return IS_ALIGNED(max(start, vma->vm_start), size) &&
+ IS_ALIGNED(min(end, vma->vm_end), size);
+}
+
static int userfaultfd_register(struct userfaultfd_ctx *ctx,
unsigned long arg)
{
@@ -3770,17 +3784,6 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
if (!vma)
goto out_unlock;
- /*
- * If the first vma contains huge pages, make sure start address
- * is aligned to huge page size.
- */
- if (vma_is_hugetlb(vma)) {
- unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
-
- if (start & (vma_hpagesize - 1))
- goto out_unlock;
- }
-
/*
* Search for not compatible vmas.
*/
@@ -3797,6 +3800,8 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
ret = -EINVAL;
if (!vma_can_userfault(cur, vm_flags, wp_async))
goto out_unlock;
+ if (!vma_uffd_range_aligned(cur, start, end))
+ goto out_unlock;
/*
* RWP uses protnone as an access-tracking marker. PROT_NONE
@@ -3820,20 +3825,6 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
ret = -EPERM;
if (unlikely(!(cur->vm_flags & VM_MAYWRITE)))
goto out_unlock;
-
- /*
- * If this vma contains ending address, and huge pages
- * check alignment.
- */
- if (vma_is_hugetlb(cur) && end <= cur->vm_end &&
- end > cur->vm_start) {
- unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
-
- ret = -EINVAL;
-
- if (end & (vma_hpagesize - 1))
- goto out_unlock;
- }
if ((vm_flags & VM_UFFD_WP) && !(cur->vm_flags & VM_MAYWRITE))
goto out_unlock;
@@ -3943,17 +3934,6 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
if (!vma)
goto out_unlock;
- /*
- * If the first vma contains huge pages, make sure start address
- * is aligned to huge page size.
- */
- if (vma_is_hugetlb(vma)) {
- unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
-
- if (start & (vma_hpagesize - 1))
- goto out_unlock;
- }
-
/*
* Search for not compatible vmas.
*/
@@ -3982,6 +3962,8 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
*/
if (!vma_can_userfault(cur, cur->vm_flags, wp_async))
goto out_unlock;
+ if (!vma_uffd_range_aligned(cur, start, end))
+ goto out_unlock;
found = true;
} for_each_vma_range(vmi, cur, end);
--
2.39.5