Re: [PATCH v2 23/35] mm/hugetlb: check EOF for UFFDIO_COPY and UFFDIO_POISON
From: Jose A. Perez de Azpillaga
Date: Fri Dec 04 2026 - 06:25:33 EST
On 12/3/26 12:37 PM, David Hildenbrand (Arm) wrote:
> When UFFDIO_COPY places a new folio into a MAP_SHARED hugetlb mapping,
> hugetlb_mfill_atomic_pte() checks EOF under
> filemap_invalidate_lock_shared() and holds it until the folio is mapped,
> such that a concurrent truncation either unmaps the new folio, or we see
> the new i_size. For other file-backed mappings, mfill_file_over_size()
> checks EOF for all userfaultfd operations.
>
> However, we don't check EOF for UFFDIO_COPY into MAP_PRIVATE mappings,
> where the new folio does not go into the page cache, and we don't check
> it for UFFDIO_POISON at all.
>
> Consequently, both can install an anonymous folio or a poison marker
> beyond EOF, even without racing with truncation. Accessing that range
> then maps the stale folio or reports memory poison instead of raising
> SIGBUS, and once the file is extended again, the stale folio or poison
> marker remains instead of a fresh folio getting faulted in.
>
> Reproduced by truncating a file and using UFFDIO_COPY in a private
> mapping or UFFDIO_POISON on the truncated range: both succeed, and after
> extending the file again, accessing the page returns the copied data or
> raises SIGBUS. Further, artificially delaying hugetlb_mfill_atomic_pte()
> before allocating the folio, while another thread truncates the file,
> leaves the page beyond EOF mapped.
>
> Let's fix it by checking EOF under the invalidate lock for all
> UFFDIO_COPY and UFFDIO_POISON, and holding it until we installed the PTE.
> UFFDIO_CONTINUE maps an existing page cache folio under the fault
> mutex, which serializes against its removal on truncation.
>
> This has low urgency, as it requires UFFDIO_COPY into a private mapping
> or UFFDIO_POISON beyond EOF. No need to CC stable.
>
> Fixes: fa27759af4a6 ("hugetlb: clean up code checking for fault/truncation races")
> Fixes: 8a13897fb0da ("mm: userfaultfd: support UFFDIO_POISON for hugetlbfs")
> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
LGTM:
Reviewed-by: Jose A. Perez de Azpillaga <azpijr@xxxxxxxxx>
--
cheers,
jose a. p-a