• David Hildenbrand's avatar
    smaps: use vm_normal_page_pmd() instead of follow_trans_huge_pmd() · 8b9c1cc0
    David Hildenbrand authored
    We shouldn't be using a GUP-internal helper if it can be avoided.
    
    Similar to smaps_pte_entry() that uses vm_normal_page(), let's use
    vm_normal_page_pmd() that similarly refuses to return the huge zeropage.
    
    In contrast to follow_trans_huge_pmd(), vm_normal_page_pmd():
    
    (1) Will always return the head page, not a tail page of a THP.
    
     If we'd ever call smaps_account with a tail page while setting "compound
     = true", we could be in trouble, because smaps_account() would look at
     the memmap of unrelated pages.
    
     If we're unlucky, that memmap does not exist at all. Before we removed
     PG_doublemap, we could have triggered something similar as in
     commit 24d7275c ("fs/proc: task_mmu.c: don't read mapcount for
     migration entry").
    
     This can theoretically happen ever since commit ff9f47f6 ("mm: proc:
     smaps_rollup: do not stall write attempts on mmap_lock"):
    
      (a) We're in show_smaps_rollup() and processed a VMA
      (b) We release the mmap lock in show_smaps_rollup() because it is
          contended
      (c) We merged that VMA with another VMA
      (d) We collapsed a THP in that merged VMA at that position
    
     If the end address of the original VMA falls into the middle of a THP
     area, we would call smap_gather_stats() with a start address that falls
     into a PMD-mapped THP. It's probably very rare to trigger when not
     really forced.
    
    (2) Will succeed on a is_pci_p2pdma_page(), like vm_normal_page()
    
     Treat such PMDs here just like smaps_pte_entry() would treat such PTEs.
     If such pages would be anonymous, we most certainly would want to
     account them.
    
    (3) Will skip over pmd_devmap(), like vm_normal_page() for pte_devmap()
    
     As noted in vm_normal_page(), that is only for handling legacy ZONE_DEVICE
     pages. So just like smaps_pte_entry(), we'll now also ignore such PMD
     entries.
    
     Especially, follow_pmd_mask() never ends up calling
     follow_trans_huge_pmd() on pmd_devmap(). Instead it calls
     follow_devmap_pmd() -- which will fail if neither FOLL_GET nor FOLL_PIN
     is set.
    
     So skipping pmd_devmap() pages seems to be the right thing to do.
    
    (4) Will properly handle VM_MIXEDMAP/VM_PFNMAP, like vm_normal_page()
    
     We won't be returning a memmap that should be ignored by core-mm, or
     worse, a memmap that does not even exist. Note that while
     walk_page_range() will skip VM_PFNMAP mappings, walk_page_vma() won't.
    
     Most probably this case doesn't currently really happen on the PMD level,
     otherwise we'd already be able to trigger kernel crashes when reading
     smaps / smaps_rollup.
    
    So most probably only (1) is relevant in practice as of now, but could only
    cause trouble in extreme corner cases.
    
    Let's move follow_trans_huge_pmd() to mm/internal.h to discourage future
    reuse in wrong context.
    
    Link: https://lkml.kernel.org/r/20230803143208.383663-3-david@redhat.com
    Fixes: ff9f47f6 ("mm: proc: smaps_rollup: do not stall write attempts on mmap_lock")
    Signed-off-by: default avatarDavid Hildenbrand <david@redhat.com>
    Acked-by: default avatarMel Gorman <mgorman@techsingularity.net>
    Cc: Hugh Dickins <hughd@google.com>
    Cc: Jason Gunthorpe <jgg@ziepe.ca>
    Cc: John Hubbard <jhubbard@nvidia.com>
    Cc: Linus Torvalds <torvalds@linux-foundation.org>
    Cc: liubo <liubo254@huawei.com>
    Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
    Cc: Mel Gorman <mgorman@suse.de>
    Cc: Paolo Bonzini <pbonzini@redhat.com>
    Cc: Peter Xu <peterx@redhat.com>
    Cc: Shuah Khan <shuah@kernel.org>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    8b9c1cc0
task_mmu.c 49.1 KB