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) --- mm/hugetlb.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 105cc2840f988..a23d36e7b8e93 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6559,13 +6559,31 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, bool folio_in_pagecache = false; pte_t dst_ptep; + /* + * Check EOF and keep truncation out until we mapped the new folio or + * installed the poison marker: truncation unmaps or removes it + * afterwards. + */ + if (!is_continue) { + filemap_invalidate_lock_shared(mapping); + if (!hugetlb_pgoff_valid(mapping, idx, /* partial= */ false)) { + if (*foliop) { + folio_put(*foliop); + *foliop = NULL; + } + ret = -EFAULT; + goto out; + } + } + if (uffd_flags_mode_is(flags, MFILL_ATOMIC_POISON)) { ptl = huge_pte_lock(h, dst_mm, dst_pte); /* Don't overwrite any existing PTEs (even markers) */ if (!huge_pte_none(huge_ptep_get(dst_mm, dst_addr, dst_pte))) { spin_unlock(ptl); - return -EEXIST; + ret = -EEXIST; + goto out; } VM_WARN_ON_ONCE(size == PMD_SIZE && hugetlb_pmd_shared(dst_pte)); @@ -6576,19 +6594,8 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, update_mmu_cache(dst_vma, dst_addr, dst_pte); spin_unlock(ptl); - return 0; - } - - if (alloc_pagecache_folio) { - filemap_invalidate_lock_shared(mapping); - if (!hugetlb_pgoff_valid(mapping, idx, /* partial= */ false)) { - if (*foliop) { - folio_put(*foliop); - *foliop = NULL; - } - ret = -EFAULT; - goto out; - } + ret = 0; + goto out; } if (is_continue) { @@ -6757,7 +6764,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, spin_unlock(ptl); ret = 0; out: - if (alloc_pagecache_folio) + if (!is_continue) filemap_invalidate_unlock_shared(mapping); return ret; out_release_unlock: -- 2.56.1