• Mike Kravetz's avatar
    hugetlb: don't delete vma_lock in hugetlb MADV_DONTNEED processing · 04ada095
    Mike Kravetz authored
    madvise(MADV_DONTNEED) ends up calling zap_page_range() to clear page
    tables associated with the address range.  For hugetlb vmas,
    zap_page_range will call __unmap_hugepage_range_final.  However,
    __unmap_hugepage_range_final assumes the passed vma is about to be removed
    and deletes the vma_lock to prevent pmd sharing as the vma is on the way
    out.  In the case of madvise(MADV_DONTNEED) the vma remains, but the
    missing vma_lock prevents pmd sharing and could potentially lead to issues
    with truncation/fault races.
    
    This issue was originally reported here [1] as a BUG triggered in
    page_try_dup_anon_rmap.  Prior to the introduction of the hugetlb
    vma_lock, __unmap_hugepage_range_final cleared the VM_MAYSHARE flag to
    prevent pmd sharing.  Subsequent faults on this vma were confused as
    VM_MAYSHARE indicates a sharable vma, but was not set so page_mapping was
    not set in new pages added to the page table.  This resulted in pages that
    appeared anonymous in a VM_SHARED vma and triggered the BUG.
    
    Address issue by adding a new zap flag ZAP_FLAG_UNMAP to indicate an unmap
    call from unmap_vmas().  This is used to indicate the 'final' unmapping of
    a hugetlb vma.  When called via MADV_DONTNEED, this flag is not set and
    the vm_lock is not deleted.
    
    [1] https://lore.kernel.org/lkml/CAO4mrfdLMXsao9RF4fUE8-Wfde8xmjsKrTNMNC9wjUb6JudD0g@mail.gmail.com/
    
    Link: https://lkml.kernel.org/r/20221114235507.294320-3-mike.kravetz@oracle.com
    Fixes: 90e7e7f5 ("mm: enable MADV_DONTNEED for hugetlb mappings")
    Signed-off-by: default avatarMike Kravetz <mike.kravetz@oracle.com>
    Reported-by: default avatarWei Chen <harperchen1110@gmail.com>
    Cc: Axel Rasmussen <axelrasmussen@google.com>
    Cc: David Hildenbrand <david@redhat.com>
    Cc: Matthew Wilcox <willy@infradead.org>
    Cc: Mina Almasry <almasrymina@google.com>
    Cc: Nadav Amit <nadav.amit@gmail.com>
    Cc: Naoya Horiguchi <naoya.horiguchi@linux.dev>
    Cc: Peter Xu <peterx@redhat.com>
    Cc: Rik van Riel <riel@surriel.com>
    Cc: Vlastimil Babka <vbabka@suse.cz>
    Cc: <stable@vger.kernel.org>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    04ada095
hugetlb.c 208 KB