Skip to content

Commit 80110bb

Browse files
soleentorvalds
authored andcommitted
mm/page_table_check: check entries at pmd levels
syzbot detected a case where the page table counters were not properly updated. syzkaller login: ------------[ cut here ]------------ kernel BUG at mm/page_table_check.c:162! invalid opcode: 0000 [#1] PREEMPT SMP KASAN CPU: 0 PID: 3099 Comm: pasha Not tainted 5.16.0+ #48 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIO4 RIP: 0010:__page_table_check_zero+0x159/0x1a0 Call Trace: free_pcp_prepare+0x3be/0xaa0 free_unref_page+0x1c/0x650 free_compound_page+0xec/0x130 free_transhuge_page+0x1be/0x260 __put_compound_page+0x90/0xd0 release_pages+0x54c/0x1060 __pagevec_release+0x7c/0x110 shmem_undo_range+0x85e/0x1250 ... The repro involved having a huge page that is split due to uprobe event temporarily replacing one of the pages in the huge page. Later the huge page was combined again, but the counters were off, as the PTE level was not properly updated. Make sure that when PMD is cleared and prior to freeing the level the PTEs are updated. Link: https://lkml.kernel.org/r/[email protected] Fixes: df4e817 ("mm: page table check") Signed-off-by: Pasha Tatashin <[email protected]> Acked-by: David Rientjes <[email protected]> Cc: Aneesh Kumar K.V <[email protected]> Cc: Anshuman Khandual <[email protected]> Cc: Dave Hansen <[email protected]> Cc: Greg Thelen <[email protected]> Cc: H. Peter Anvin <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jiri Slaby <[email protected]> Cc: Mike Rapoport <[email protected]> Cc: Muchun Song <[email protected]> Cc: Paul Turner <[email protected]> Cc: Wei Xu <[email protected]> Cc: Will Deacon <[email protected]> Cc: Zi Yan <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent e59a47b commit 80110bb

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

include/linux/page_table_check.h

+19
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ void __page_table_check_pmd_set(struct mm_struct *mm, unsigned long addr,
2626
pmd_t *pmdp, pmd_t pmd);
2727
void __page_table_check_pud_set(struct mm_struct *mm, unsigned long addr,
2828
pud_t *pudp, pud_t pud);
29+
void __page_table_check_pte_clear_range(struct mm_struct *mm,
30+
unsigned long addr,
31+
pmd_t pmd);
2932

3033
static inline void page_table_check_alloc(struct page *page, unsigned int order)
3134
{
@@ -100,6 +103,16 @@ static inline void page_table_check_pud_set(struct mm_struct *mm,
100103
__page_table_check_pud_set(mm, addr, pudp, pud);
101104
}
102105

106+
static inline void page_table_check_pte_clear_range(struct mm_struct *mm,
107+
unsigned long addr,
108+
pmd_t pmd)
109+
{
110+
if (static_branch_likely(&page_table_check_disabled))
111+
return;
112+
113+
__page_table_check_pte_clear_range(mm, addr, pmd);
114+
}
115+
103116
#else
104117

105118
static inline void page_table_check_alloc(struct page *page, unsigned int order)
@@ -143,5 +156,11 @@ static inline void page_table_check_pud_set(struct mm_struct *mm,
143156
{
144157
}
145158

159+
static inline void page_table_check_pte_clear_range(struct mm_struct *mm,
160+
unsigned long addr,
161+
pmd_t pmd)
162+
{
163+
}
164+
146165
#endif /* CONFIG_PAGE_TABLE_CHECK */
147166
#endif /* __LINUX_PAGE_TABLE_CHECK_H */

mm/khugepaged.c

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/hashtable.h>
1717
#include <linux/userfaultfd_k.h>
1818
#include <linux/page_idle.h>
19+
#include <linux/page_table_check.h>
1920
#include <linux/swapops.h>
2021
#include <linux/shmem_fs.h>
2122

@@ -1422,10 +1423,12 @@ static void collapse_and_free_pmd(struct mm_struct *mm, struct vm_area_struct *v
14221423
spinlock_t *ptl;
14231424
pmd_t pmd;
14241425

1426+
mmap_assert_write_locked(mm);
14251427
ptl = pmd_lock(vma->vm_mm, pmdp);
14261428
pmd = pmdp_collapse_flush(vma, addr, pmdp);
14271429
spin_unlock(ptl);
14281430
mm_dec_nr_ptes(mm);
1431+
page_table_check_pte_clear_range(mm, addr, pmd);
14291432
pte_free(mm, pmd_pgtable(pmd));
14301433
}
14311434

mm/page_table_check.c

+20
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,23 @@ void __page_table_check_pud_set(struct mm_struct *mm, unsigned long addr,
247247
}
248248
}
249249
EXPORT_SYMBOL(__page_table_check_pud_set);
250+
251+
void __page_table_check_pte_clear_range(struct mm_struct *mm,
252+
unsigned long addr,
253+
pmd_t pmd)
254+
{
255+
if (&init_mm == mm)
256+
return;
257+
258+
if (!pmd_bad(pmd) && !pmd_leaf(pmd)) {
259+
pte_t *ptep = pte_offset_map(&pmd, addr);
260+
unsigned long i;
261+
262+
pte_unmap(ptep);
263+
for (i = 0; i < PTRS_PER_PTE; i++) {
264+
__page_table_check_pte_clear(mm, addr, *ptep);
265+
addr += PAGE_SIZE;
266+
ptep++;
267+
}
268+
}
269+
}

0 commit comments

Comments
 (0)