Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm/memory: fix PMD/PUD checks in follow_pfnmap_start()

follow_pfnmap_start() suffers from two problems:

(1) We are not re-fetching the pmd/pud after taking the PTL

Therefore, we are not properly stabilizing what the lock actually
protects. If there is concurrent zapping, we would indicate to the
caller that we found an entry, however, that entry might already have
been invalidated, or contain a different PFN after taking the lock.

Properly use pmdp_get() / pudp_get() after taking the lock.

(2) pmd_leaf() / pud_leaf() are not well defined on non-present entries

pmd_leaf()/pud_leaf() could wrongly trigger on non-present entries.

There is no real guarantee that pmd_leaf()/pud_leaf() returns something
reasonable on non-present entries. Most architectures indeed either
perform a present check or make it work by smart use of flags.

However, for example loongarch checks the _PAGE_HUGE flag in pmd_leaf(),
and always sets the _PAGE_HUGE flag in __swp_entry_to_pmd(). Whereby
pmd_trans_huge() explicitly checks pmd_present(), pmd_leaf() does not do
that.

Let's check pmd_present()/pud_present() before assuming "the is a present
PMD leaf" when spotting pmd_leaf()/pud_leaf(), like other page table
handling code that traverses user page tables does.

Given that non-present PMD entries are likely rare in VM_IO|VM_PFNMAP, (1)
is likely more relevant than (2). It is questionable how often (1) would
actually trigger, but let's CC stable to be sure.

This was found by code inspection.

Link: https://lkml.kernel.org/r/20260323-follow_pfnmap_fix-v1-1-5b0ec10872b3@kernel.org
Fixes: 6da8e9634bb7 ("mm: new follow_pfnmap API")
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
Acked-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Reviewed-by: Lorenzo Stoakes (Oracle) <ljs@kernel.org>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Vlastimil Babka <vbabka@kernel.org>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

David Hildenbrand (Arm) and committed by
Andrew Morton
26e7888a d0bde8e2

+15 -3
+15 -3
mm/memory.c
··· 6824 6824 6825 6825 pudp = pud_offset(p4dp, address); 6826 6826 pud = pudp_get(pudp); 6827 - if (pud_none(pud)) 6827 + if (!pud_present(pud)) 6828 6828 goto out; 6829 6829 if (pud_leaf(pud)) { 6830 6830 lock = pud_lock(mm, pudp); 6831 - if (!unlikely(pud_leaf(pud))) { 6831 + pud = pudp_get(pudp); 6832 + 6833 + if (unlikely(!pud_present(pud))) { 6834 + spin_unlock(lock); 6835 + goto out; 6836 + } else if (unlikely(!pud_leaf(pud))) { 6832 6837 spin_unlock(lock); 6833 6838 goto retry; 6834 6839 } ··· 6845 6840 6846 6841 pmdp = pmd_offset(pudp, address); 6847 6842 pmd = pmdp_get_lockless(pmdp); 6843 + if (!pmd_present(pmd)) 6844 + goto out; 6848 6845 if (pmd_leaf(pmd)) { 6849 6846 lock = pmd_lock(mm, pmdp); 6850 - if (!unlikely(pmd_leaf(pmd))) { 6847 + pmd = pmdp_get(pmdp); 6848 + 6849 + if (unlikely(!pmd_present(pmd))) { 6850 + spin_unlock(lock); 6851 + goto out; 6852 + } else if (unlikely(!pmd_leaf(pmd))) { 6851 6853 spin_unlock(lock); 6852 6854 goto retry; 6853 6855 }