Skip to content

Commit 66f6db7

Browse files
koct9igregkh
authored andcommitted
proc/pagemap: walk page tables under pte lock
commit 05fbf35 upstream. Lockless access to pte in pagemap_pte_range() might race with page migration and trigger BUG_ON(!PageLocked()) in migration_entry_to_page(): CPU A (pagemap) CPU B (migration) lock_page() try_to_unmap(page, TTU_MIGRATION...) make_migration_entry() set_pte_at() <read *pte> pte_to_pagemap_entry() remove_migration_ptes() unlock_page() if(is_migration_entry()) migration_entry_to_page() BUG_ON(!PageLocked(page)) Also lockless read might be non-atomic if pte is larger than wordsize. Other pte walkers (smaps, numa_maps, clear_refs) already lock ptes. Fixes: 052fb0d ("proc: report file/anon bit in /proc/pid/pagemap") Signed-off-by: Konstantin Khlebnikov <[email protected]> Reported-by: Andrey Ryabinin <[email protected]> Reviewed-by: Cyrill Gorcunov <[email protected]> Acked-by: Naoya Horiguchi <[email protected]> Acked-by: Kirill A. Shutemov <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 0c98e7c commit 66f6db7

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

fs/proc/task_mmu.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,7 @@ static int pagemap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
10691069
struct vm_area_struct *vma;
10701070
struct pagemapread *pm = walk->private;
10711071
spinlock_t *ptl;
1072-
pte_t *pte;
1072+
pte_t *pte, *orig_pte;
10731073
int err = 0;
10741074

10751075
/* find the first VMA at or above 'addr' */
@@ -1130,15 +1130,19 @@ static int pagemap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
11301130
BUG_ON(is_vm_hugetlb_page(vma));
11311131

11321132
/* Addresses in the VMA. */
1133-
for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) {
1133+
orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1134+
for (; addr < min(end, vma->vm_end); pte++, addr += PAGE_SIZE) {
11341135
pagemap_entry_t pme;
1135-
pte = pte_offset_map(pmd, addr);
1136+
11361137
pte_to_pagemap_entry(&pme, pm, vma, addr, *pte);
1137-
pte_unmap(pte);
11381138
err = add_to_pagemap(addr, &pme, pm);
11391139
if (err)
1140-
return err;
1140+
break;
11411141
}
1142+
pte_unmap_unlock(orig_pte, ptl);
1143+
1144+
if (err)
1145+
return err;
11421146

11431147
if (addr == end)
11441148
break;

0 commit comments

Comments
 (0)