If a COW fault of the owner of a MAP_PRIVATE hugetlb reservation fails to allocate a folio, the owner keeps the folio, and unmap_ref_private() unmaps it from all other private VMAs that map it. We mark these VMAs with HPAGE_RESV_UNMAPPED, such that future faults in them fail instead of looking like the data was lost. However, when a process whose VMA has HPAGE_RESV_UNMAPPED set forks, hugetlb_dup_vma_private() clears vm_private_data of the child's VMA, including HPAGE_RESV_UNMAPPED. Consequently, faults in the grandchild of the original process can map a page cache folio or a new zeroed folio without any signal, silently losing the data. Reproduced by forking a child after writing to a folio in a private mapping, exhausting the pool such that the COW fault of the owner fails, and letting the child fork a grandchild after freeing up the pool again: the child gets SIGBUS when accessing the folio, but the grandchild reads zeroes. Let's fix it by keeping HPAGE_RESV_UNMAPPED when clearing the private data of the child's VMA. This has low urgency, as it requires a failed COW fault due to an exhausted pool, followed by a fork(). No need to CC stable. Fixes: 04f2cbe35699 ("hugetlb: guarantee that COW faults for a process that called mmap(MAP_PRIVATE) on hugetlbfs will succeed") Reported-by: Chris Mason Signed-off-by: David Hildenbrand (Arm) --- mm/hugetlb.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 68d90c7297921..f64c3293fd109 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1218,7 +1218,7 @@ void hugetlb_dup_vma_private(struct vm_area_struct *vma) { VM_WARN_ON_ONCE_VMA(!vma_is_hugetlb(vma), vma); /* - * Clear vm_private_data + * Clear vm_private_data, except for HPAGE_RESV_UNMAPPED * - For shared mappings this is a per-vma semaphore that may be * allocated in a subsequent call to hugetlb_vm_op_open. * Before clearing, make sure pointer is not associated with vma @@ -1227,7 +1227,10 @@ void hugetlb_dup_vma_private(struct vm_area_struct *vma) * been called to allocate a new structure. * - For MAP_PRIVATE mappings, this is the reserve map which does * not apply to children. Faults generated by the children are - * not guaranteed to succeed, even if read-only. + * not guaranteed to succeed, even if read-only. But keep + * HPAGE_RESV_UNMAPPED: if the reservation owner unmapped folios + * from this VMA when its COW fault failed, faults in children must + * fail as well. */ if (vma->vm_flags & VM_MAYSHARE) { struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; @@ -1235,7 +1238,11 @@ void hugetlb_dup_vma_private(struct vm_area_struct *vma) if (vma_lock && vma_lock->vma != vma) vma->vm_private_data = NULL; } else { + const bool unmapped = is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED); + vma->vm_private_data = NULL; + if (unmapped) + set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED); } } -- 2.56.1