• Rik van Riel's avatar
    hugetlbfs: clear resv_map pointer if mmap fails · 92fe9dcb
    Rik van Riel authored
    Patch series "hugetlbfs: close race between MADV_DONTNEED and page fault", v7.
    
    Malloc libraries, like jemalloc and tcalloc, take decisions on when to
    call madvise independently from the code in the main application.
    
    This sometimes results in the application page faulting on an address,
    right after the malloc library has shot down the backing memory with
    MADV_DONTNEED.
    
    Usually this is harmless, because we always have some 4kB pages sitting
    around to satisfy a page fault.  However, with hugetlbfs systems often
    allocate only the exact number of huge pages that the application wants.
    
    Due to TLB batching, hugetlbfs MADV_DONTNEED will free pages outside of
    any lock taken on the page fault path, which can open up the following
    race condition:
    
           CPU 1                            CPU 2
    
           MADV_DONTNEED
           unmap page
           shoot down TLB entry
                                           page fault
                                           fail to allocate a huge page
                                           killed with SIGBUS
           free page
    
    Fix that race by extending the hugetlb_vma_lock locking scheme to also
    cover private hugetlb mappings (with resv_map), and pulling the locking
    from __unmap_hugepage_final_range into helper functions called from
    zap_page_range_single.  This ensures page faults stay locked out of the
    MADV_DONTNEED VMA until the huge pages have actually been freed.
    
    
    This patch (of 3):
    
    Hugetlbfs leaves a dangling pointer in the VMA if mmap fails.  This has
    not been a problem so far, but other code in this patch series tries to
    follow that pointer.
    
    Link: https://lkml.kernel.org/r/20231006040020.3677377-1-riel@surriel.com
    Link: https://lkml.kernel.org/r/20231006040020.3677377-2-riel@surriel.com
    Fixes: 04ada095 ("hugetlb: don't delete vma_lock in hugetlb MADV_DONTNEED processing")
    Signed-off-by: default avatarMike Kravetz <mike.kravetz@oracle.com>
    Signed-off-by: default avatarRik van Riel <riel@surriel.com>
    Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
    Cc: Muchun Song <muchun.song@linux.dev>
    Cc: <stable@vger.kernel.org>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    92fe9dcb
hugetlb.c 205 KB