Re: [PATCH v2 9/9] mm/userfaultfd: check hugetlb end alignment in UFFDIO_UNREGISTER pre-scan

From: David Hildenbrand (Arm)

Date: Mon Nov 16 2026 - 15:57:07 EST


On 11/16/26 21:02, Tal Zussman wrote:
> 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, check the end of a registered VMA in the pre-scan as well.
> The ioctl still returns -EINVAL, but no longer unregisters the VMAs
> before the misaligned one first.
>
> Fixes: cab350afcbc9 ("userfaultfd: hugetlbfs: allow registration of ranges containing huge pages")
> Fixes: f369b07c8614 ("mm/uffd: reset write protection when unregister with wp-mode")
> Signed-off-by: Tal Zussman <tz2294@xxxxxxxxxxxx>

Okay, I assume it's not a stable kernel, so we can fix this in a cleaner way.

> ---
> mm/userfaultfd.c | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
> diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
> index bf42fabb5afd..d8abc24353e1 100644
> --- a/mm/userfaultfd.c
> +++ b/mm/userfaultfd.c
> @@ -3960,6 +3960,8 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
> found = false;
> cur = vma;
> do {
> + const struct userfaultfd_ctx *vma_ctx = cur->vm_userfaultfd_ctx.ctx;
> +
> cond_resched();
>
> VM_WARN_ON_ONCE(!!cur->vm_userfaultfd_ctx.ctx ^
> @@ -3983,6 +3985,15 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
> if (!vma_can_userfault(cur, cur->vm_flags, wp_async))
> goto out_unlock;
>
> + /* See the check in userfaultfd_register(). */
> + if (vma_ctx && vma_is_hugetlb(cur) &&

I'm not sure if we really need the vma_ctx. I'd just fail this consistently if
someone passes hugetlb VMAs, independent of if they are registered or not.

> + end <= cur->vm_end && end > cur->vm_start) {
> + const unsigned long vma_hpagesize = vma_kernel_pagesize(cur);

vma_kernel_pagesize(), yuk. :)

> +
> + if (!IS_ALIGNED(end, vma_hpagesize))
> + goto out_unlock;
> + }
> +
> found = true;
> } for_each_vma_range(vmi, cur, end);
> VM_WARN_ON_ONCE(!found);


Can we clean all this up into something simple like:

--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -3669,6 +3669,19 @@ 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;
+
+ 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)
{
@@ -3747,17 +3760,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.
*/
@@ -3774,6 +3776,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
@@ -3797,20 +3801,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;

@@ -3920,17 +3910,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.
*/
@@ -3959,6 +3938,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);

--
Cheers,

David