Skip to content

Commit 4655e5e

Browse files
liu-song-6torvalds
authored andcommitted
mm,thp: recheck each page before collapsing file THP
In collapse_file(), for !is_shmem case, current check cannot guarantee the locked page is up-to-date. Specifically, xas_unlock_irq() should not be called before lock_page() and get_page(); and it is necessary to recheck PageUptodate() after locking the page. With this bug and CONFIG_READ_ONLY_THP_FOR_FS=y, madvise(HUGE)'ed .text may contain corrupted data. This is because khugepaged mistakenly collapses some not up-to-date sub pages into a huge page, and assumes the huge page is up-to-date. This will NOT corrupt data in the disk, because the page is read-only and never written back. Fix this by properly checking PageUptodate() after locking the page. This check replaces "VM_BUG_ON_PAGE(!PageUptodate(page), page);". Also, move PageDirty() check after locking the page. Current khugepaged should not try to collapse dirty file THP, because it is limited to read-only .text. The only case we hit a dirty page here is when the page hasn't been written since write. Bail out and retry when this happens. syzbot reported bug on previous version of this patch. Link: http://lkml.kernel.org/r/[email protected] Fixes: 99cb0db ("mm,thp: add read-only THP support for (non-shmem) FS") Signed-off-by: Song Liu <[email protected]> Reported-by: [email protected] Cc: Johannes Weiner <[email protected]> Cc: Kirill A. Shutemov <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: William Kucharski <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent aea4df4 commit 4655e5e

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

mm/khugepaged.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,17 +1602,6 @@ static void collapse_file(struct mm_struct *mm,
16021602
result = SCAN_FAIL;
16031603
goto xa_unlocked;
16041604
}
1605-
} else if (!PageUptodate(page)) {
1606-
xas_unlock_irq(&xas);
1607-
wait_on_page_locked(page);
1608-
if (!trylock_page(page)) {
1609-
result = SCAN_PAGE_LOCK;
1610-
goto xa_unlocked;
1611-
}
1612-
get_page(page);
1613-
} else if (PageDirty(page)) {
1614-
result = SCAN_FAIL;
1615-
goto xa_locked;
16161605
} else if (trylock_page(page)) {
16171606
get_page(page);
16181607
xas_unlock_irq(&xas);
@@ -1627,7 +1616,12 @@ static void collapse_file(struct mm_struct *mm,
16271616
* without racing with truncate.
16281617
*/
16291618
VM_BUG_ON_PAGE(!PageLocked(page), page);
1630-
VM_BUG_ON_PAGE(!PageUptodate(page), page);
1619+
1620+
/* make sure the page is up to date */
1621+
if (unlikely(!PageUptodate(page))) {
1622+
result = SCAN_FAIL;
1623+
goto out_unlock;
1624+
}
16311625

16321626
/*
16331627
* If file was truncated then extended, or hole-punched, before
@@ -1643,6 +1637,16 @@ static void collapse_file(struct mm_struct *mm,
16431637
goto out_unlock;
16441638
}
16451639

1640+
if (!is_shmem && PageDirty(page)) {
1641+
/*
1642+
* khugepaged only works on read-only fd, so this
1643+
* page is dirty because it hasn't been flushed
1644+
* since first write.
1645+
*/
1646+
result = SCAN_FAIL;
1647+
goto out_unlock;
1648+
}
1649+
16461650
if (isolate_lru_page(page)) {
16471651
result = SCAN_DEL_PAGE_LRU;
16481652
goto out_unlock;

0 commit comments

Comments
 (0)