Re: [PATCH v3 9/9] mm/userfaultfd: check the hugetlb alignment of every VMA in the pre-scan
From: David Hildenbrand (Arm)
Date: Wed Nov 18 2026 - 04:39:04 EST
On 11/18/26 05:28, 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, 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>
> ---
Thanks!
Acked-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
--
Cheers,
David