Skip to content

Commit db3f363

Browse files
yang-shigregkh
authored andcommitted
fs/proc: task_mmu.c: don't read mapcount for migration entry
commit 24d7275 upstream. The syzbot reported the below BUG: kernel BUG at include/linux/page-flags.h:785! invalid opcode: 0000 [raspberrypi#1] PREEMPT SMP KASAN CPU: 1 PID: 4392 Comm: syz-executor560 Not tainted 5.16.0-rc6-syzkaller #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 RIP: 0010:PageDoubleMap include/linux/page-flags.h:785 [inline] RIP: 0010:__page_mapcount+0x2d2/0x350 mm/util.c:744 Call Trace: page_mapcount include/linux/mm.h:837 [inline] smaps_account+0x470/0xb10 fs/proc/task_mmu.c:466 smaps_pte_entry fs/proc/task_mmu.c:538 [inline] smaps_pte_range+0x611/0x1250 fs/proc/task_mmu.c:601 walk_pmd_range mm/pagewalk.c:128 [inline] walk_pud_range mm/pagewalk.c:205 [inline] walk_p4d_range mm/pagewalk.c:240 [inline] walk_pgd_range mm/pagewalk.c:277 [inline] __walk_page_range+0xe23/0x1ea0 mm/pagewalk.c:379 walk_page_vma+0x277/0x350 mm/pagewalk.c:530 smap_gather_stats.part.0+0x148/0x260 fs/proc/task_mmu.c:768 smap_gather_stats fs/proc/task_mmu.c:741 [inline] show_smap+0xc6/0x440 fs/proc/task_mmu.c:822 seq_read_iter+0xbb0/0x1240 fs/seq_file.c:272 seq_read+0x3e0/0x5b0 fs/seq_file.c:162 vfs_read+0x1b5/0x600 fs/read_write.c:479 ksys_read+0x12d/0x250 fs/read_write.c:619 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x35/0xb0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x44/0xae The reproducer was trying to read /proc/$PID/smaps when calling MADV_FREE at the mean time. MADV_FREE may split THPs if it is called for partial THP. It may trigger the below race: CPU A CPU B ----- ----- smaps walk: MADV_FREE: page_mapcount() PageCompound() split_huge_page() page = compound_head(page) PageDoubleMap(page) When calling PageDoubleMap() this page is not a tail page of THP anymore so the BUG is triggered. This could be fixed by elevated refcount of the page before calling mapcount, but that would prevent it from counting migration entries, and it seems overkilling because the race just could happen when PMD is split so all PTE entries of tail pages are actually migration entries, and smaps_account() does treat migration entries as mapcount == 1 as Kirill pointed out. Add a new parameter for smaps_account() to tell this entry is migration entry then skip calling page_mapcount(). Don't skip getting mapcount for device private entries since they do track references with mapcount. Pagemap also has the similar issue although it was not reported. Fixed it as well. [shy828301@gmail.com: v4] Link: https://lkml.kernel.org/r/20220203182641.824731-1-shy828301@gmail.com [nathan@kernel.org: avoid unused variable warning in pagemap_pmd_range()] Link: https://lkml.kernel.org/r/20220207171049.1102239-1-nathan@kernel.org Link: https://lkml.kernel.org/r/20220120202805.3369-1-shy828301@gmail.com Fixes: e9b61f1 ("thp: reintroduce split_huge_page()") Signed-off-by: Yang Shi <shy828301@gmail.com> Signed-off-by: Nathan Chancellor <nathan@kernel.org> Reported-by: syzbot+1f52b3a18d5633fa7f82@syzkaller.appspotmail.com Acked-by: David Hildenbrand <david@redhat.com> Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com> Cc: Jann Horn <jannh@google.com> Cc: Matthew Wilcox <willy@infradead.org> Cc: Alexey Dobriyan <adobriyan@gmail.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 0849f83 commit db3f363

1 file changed

Lines changed: 32 additions & 11 deletions

File tree

‎fs/proc/task_mmu.c‎

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,8 @@ static void smaps_page_accumulate(struct mem_size_stats *mss,
430430
}
431431

432432
static void smaps_account(struct mem_size_stats *mss, struct page *page,
433-
bool compound, bool young, bool dirty, bool locked)
433+
bool compound, bool young, bool dirty, bool locked,
434+
bool migration)
434435
{
435436
int i, nr = compound ? compound_nr(page) : 1;
436437
unsigned long size = nr * PAGE_SIZE;
@@ -457,8 +458,15 @@ static void smaps_account(struct mem_size_stats *mss, struct page *page,
457458
* page_count(page) == 1 guarantees the page is mapped exactly once.
458459
* If any subpage of the compound page mapped with PTE it would elevate
459460
* page_count().
461+
*
462+
* The page_mapcount() is called to get a snapshot of the mapcount.
463+
* Without holding the page lock this snapshot can be slightly wrong as
464+
* we cannot always read the mapcount atomically. It is not safe to
465+
* call page_mapcount() even with PTL held if the page is not mapped,
466+
* especially for migration entries. Treat regular migration entries
467+
* as mapcount == 1.
460468
*/
461-
if (page_count(page) == 1) {
469+
if ((page_count(page) == 1) || migration) {
462470
smaps_page_accumulate(mss, page, size, size << PSS_SHIFT, dirty,
463471
locked, true);
464472
return;
@@ -495,6 +503,7 @@ static void smaps_pte_entry(pte_t *pte, unsigned long addr,
495503
struct vm_area_struct *vma = walk->vma;
496504
bool locked = !!(vma->vm_flags & VM_LOCKED);
497505
struct page *page = NULL;
506+
bool migration = false;
498507

499508
if (pte_present(*pte)) {
500509
page = vm_normal_page(vma, addr, *pte);
@@ -514,9 +523,10 @@ static void smaps_pte_entry(pte_t *pte, unsigned long addr,
514523
} else {
515524
mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT;
516525
}
517-
} else if (is_migration_entry(swpent))
526+
} else if (is_migration_entry(swpent)) {
527+
migration = true;
518528
page = migration_entry_to_page(swpent);
519-
else if (is_device_private_entry(swpent))
529+
} else if (is_device_private_entry(swpent))
520530
page = device_private_entry_to_page(swpent);
521531
} else if (unlikely(IS_ENABLED(CONFIG_SHMEM) && mss->check_shmem_swap
522532
&& pte_none(*pte))) {
@@ -530,7 +540,8 @@ static void smaps_pte_entry(pte_t *pte, unsigned long addr,
530540
if (!page)
531541
return;
532542

533-
smaps_account(mss, page, false, pte_young(*pte), pte_dirty(*pte), locked);
543+
smaps_account(mss, page, false, pte_young(*pte), pte_dirty(*pte),
544+
locked, migration);
534545
}
535546

536547
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
@@ -541,15 +552,18 @@ static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
541552
struct vm_area_struct *vma = walk->vma;
542553
bool locked = !!(vma->vm_flags & VM_LOCKED);
543554
struct page *page = NULL;
555+
bool migration = false;
544556

545557
if (pmd_present(*pmd)) {
546558
/* FOLL_DUMP will return -EFAULT on huge zero page */
547559
page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP);
548560
} else if (unlikely(thp_migration_supported() && is_swap_pmd(*pmd))) {
549561
swp_entry_t entry = pmd_to_swp_entry(*pmd);
550562

551-
if (is_migration_entry(entry))
563+
if (is_migration_entry(entry)) {
564+
migration = true;
552565
page = migration_entry_to_page(entry);
566+
}
553567
}
554568
if (IS_ERR_OR_NULL(page))
555569
return;
@@ -561,7 +575,9 @@ static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
561575
/* pass */;
562576
else
563577
mss->file_thp += HPAGE_PMD_SIZE;
564-
smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd), locked);
578+
579+
smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd),
580+
locked, migration);
565581
}
566582
#else
567583
static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
@@ -1366,6 +1382,7 @@ static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
13661382
{
13671383
u64 frame = 0, flags = 0;
13681384
struct page *page = NULL;
1385+
bool migration = false;
13691386

13701387
if (pte_present(pte)) {
13711388
if (pm->show_pfn)
@@ -1383,16 +1400,18 @@ static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
13831400
frame = swp_type(entry) |
13841401
(swp_offset(entry) << MAX_SWAPFILES_SHIFT);
13851402
flags |= PM_SWAP;
1386-
if (is_migration_entry(entry))
1403+
if (is_migration_entry(entry)) {
1404+
migration = true;
13871405
page = migration_entry_to_page(entry);
1406+
}
13881407

13891408
if (is_device_private_entry(entry))
13901409
page = device_private_entry_to_page(entry);
13911410
}
13921411

13931412
if (page && !PageAnon(page))
13941413
flags |= PM_FILE;
1395-
if (page && page_mapcount(page) == 1)
1414+
if (page && !migration && page_mapcount(page) == 1)
13961415
flags |= PM_MMAP_EXCLUSIVE;
13971416
if (vma->vm_flags & VM_SOFTDIRTY)
13981417
flags |= PM_SOFT_DIRTY;
@@ -1408,8 +1427,9 @@ static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
14081427
spinlock_t *ptl;
14091428
pte_t *pte, *orig_pte;
14101429
int err = 0;
1411-
14121430
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1431+
bool migration = false;
1432+
14131433
ptl = pmd_trans_huge_lock(pmdp, vma);
14141434
if (ptl) {
14151435
u64 flags = 0, frame = 0;
@@ -1444,11 +1464,12 @@ static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
14441464
if (pmd_swp_soft_dirty(pmd))
14451465
flags |= PM_SOFT_DIRTY;
14461466
VM_BUG_ON(!is_pmd_migration_entry(pmd));
1467+
migration = is_migration_entry(entry);
14471468
page = migration_entry_to_page(entry);
14481469
}
14491470
#endif
14501471

1451-
if (page && page_mapcount(page) == 1)
1472+
if (page && !migration && page_mapcount(page) == 1)
14521473
flags |= PM_MMAP_EXCLUSIVE;
14531474

14541475
for (; addr != end; addr += PAGE_SIZE) {

0 commit comments

Comments
 (0)