Skip to content

Commit 309381f

Browse files
sashalevintorvalds
authored andcommitted
mm: dump page when hitting a VM_BUG_ON using VM_BUG_ON_PAGE
Most of the VM_BUG_ON assertions are performed on a page. Usually, when one of these assertions fails we'll get a BUG_ON with a call stack and the registers. I've recently noticed based on the requests to add a small piece of code that dumps the page to various VM_BUG_ON sites that the page dump is quite useful to people debugging issues in mm. This patch adds a VM_BUG_ON_PAGE(cond, page) which beyond doing what VM_BUG_ON() does, also dumps the page before executing the actual BUG_ON. [akpm@linux-foundation.org: fix up includes] Signed-off-by: Sasha Levin <sasha.levin@oracle.com> Cc: "Kirill A. Shutemov" <kirill@shutemov.name> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent e3bba3c commit 309381f

30 files changed

Lines changed: 181 additions & 170 deletions

‎arch/x86/mm/gup.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ static noinline int gup_pte_range(pmd_t pmd, unsigned long addr,
108108

109109
static inline void get_head_page_multiple(struct page *page, int nr)
110110
{
111-
VM_BUG_ON(page != compound_head(page));
112-
VM_BUG_ON(page_count(page) == 0);
111+
VM_BUG_ON_PAGE(page != compound_head(page), page);
112+
VM_BUG_ON_PAGE(page_count(page) == 0, page);
113113
atomic_add(nr, &page->_count);
114114
SetPageReferenced(page);
115115
}
@@ -135,7 +135,7 @@ static noinline int gup_huge_pmd(pmd_t pmd, unsigned long addr,
135135
head = pte_page(pte);
136136
page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
137137
do {
138-
VM_BUG_ON(compound_head(page) != head);
138+
VM_BUG_ON_PAGE(compound_head(page) != head, page);
139139
pages[*nr] = page;
140140
if (PageTail(page))
141141
get_huge_page_tail(page);
@@ -212,7 +212,7 @@ static noinline int gup_huge_pud(pud_t pud, unsigned long addr,
212212
head = pte_page(pte);
213213
page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
214214
do {
215-
VM_BUG_ON(compound_head(page) != head);
215+
VM_BUG_ON_PAGE(compound_head(page) != head, page);
216216
pages[*nr] = page;
217217
if (PageTail(page))
218218
get_huge_page_tail(page);

‎include/linux/gfp.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef __LINUX_GFP_H
22
#define __LINUX_GFP_H
33

4+
#include <linux/mmdebug.h>
45
#include <linux/mmzone.h>
56
#include <linux/stddef.h>
67
#include <linux/linkage.h>

‎include/linux/hugetlb.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define _LINUX_HUGETLB_H
33

44
#include <linux/mm_types.h>
5+
#include <linux/mmdebug.h>
56
#include <linux/fs.h>
67
#include <linux/hugetlb_inline.h>
78
#include <linux/cgroup.h>
@@ -354,7 +355,7 @@ static inline pte_t arch_make_huge_pte(pte_t entry, struct vm_area_struct *vma,
354355

355356
static inline struct hstate *page_hstate(struct page *page)
356357
{
357-
VM_BUG_ON(!PageHuge(page));
358+
VM_BUG_ON_PAGE(!PageHuge(page), page);
358359
return size_to_hstate(PAGE_SIZE << compound_order(page));
359360
}
360361

‎include/linux/hugetlb_cgroup.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef _LINUX_HUGETLB_CGROUP_H
1616
#define _LINUX_HUGETLB_CGROUP_H
1717

18+
#include <linux/mmdebug.h>
1819
#include <linux/res_counter.h>
1920

2021
struct hugetlb_cgroup;
@@ -28,7 +29,7 @@ struct hugetlb_cgroup;
2829

2930
static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page(struct page *page)
3031
{
31-
VM_BUG_ON(!PageHuge(page));
32+
VM_BUG_ON_PAGE(!PageHuge(page), page);
3233

3334
if (compound_order(page) < HUGETLB_CGROUP_MIN_ORDER)
3435
return NULL;
@@ -38,7 +39,7 @@ static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page(struct page *page)
3839
static inline
3940
int set_hugetlb_cgroup(struct page *page, struct hugetlb_cgroup *h_cg)
4041
{
41-
VM_BUG_ON(!PageHuge(page));
42+
VM_BUG_ON_PAGE(!PageHuge(page), page);
4243

4344
if (compound_order(page) < HUGETLB_CGROUP_MIN_ORDER)
4445
return -1;

‎include/linux/mm.h‎

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#ifdef __KERNEL__
77

8+
#include <linux/mmdebug.h>
89
#include <linux/gfp.h>
910
#include <linux/bug.h>
1011
#include <linux/list.h>
@@ -303,7 +304,7 @@ static inline int get_freepage_migratetype(struct page *page)
303304
*/
304305
static inline int put_page_testzero(struct page *page)
305306
{
306-
VM_BUG_ON(atomic_read(&page->_count) == 0);
307+
VM_BUG_ON_PAGE(atomic_read(&page->_count) == 0, page);
307308
return atomic_dec_and_test(&page->_count);
308309
}
309310

@@ -364,15 +365,15 @@ static inline int is_vmalloc_or_module_addr(const void *x)
364365
static inline void compound_lock(struct page *page)
365366
{
366367
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
367-
VM_BUG_ON(PageSlab(page));
368+
VM_BUG_ON_PAGE(PageSlab(page), page);
368369
bit_spin_lock(PG_compound_lock, &page->flags);
369370
#endif
370371
}
371372

372373
static inline void compound_unlock(struct page *page)
373374
{
374375
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
375-
VM_BUG_ON(PageSlab(page));
376+
VM_BUG_ON_PAGE(PageSlab(page), page);
376377
bit_spin_unlock(PG_compound_lock, &page->flags);
377378
#endif
378379
}
@@ -447,7 +448,7 @@ static inline bool __compound_tail_refcounted(struct page *page)
447448
*/
448449
static inline bool compound_tail_refcounted(struct page *page)
449450
{
450-
VM_BUG_ON(!PageHead(page));
451+
VM_BUG_ON_PAGE(!PageHead(page), page);
451452
return __compound_tail_refcounted(page);
452453
}
453454

@@ -456,9 +457,9 @@ static inline void get_huge_page_tail(struct page *page)
456457
/*
457458
* __split_huge_page_refcount() cannot run from under us.
458459
*/
459-
VM_BUG_ON(!PageTail(page));
460-
VM_BUG_ON(page_mapcount(page) < 0);
461-
VM_BUG_ON(atomic_read(&page->_count) != 0);
460+
VM_BUG_ON_PAGE(!PageTail(page), page);
461+
VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
462+
VM_BUG_ON_PAGE(atomic_read(&page->_count) != 0, page);
462463
if (compound_tail_refcounted(page->first_page))
463464
atomic_inc(&page->_mapcount);
464465
}
@@ -474,7 +475,7 @@ static inline void get_page(struct page *page)
474475
* Getting a normal page or the head of a compound page
475476
* requires to already have an elevated page->_count.
476477
*/
477-
VM_BUG_ON(atomic_read(&page->_count) <= 0);
478+
VM_BUG_ON_PAGE(atomic_read(&page->_count) <= 0, page);
478479
atomic_inc(&page->_count);
479480
}
480481

@@ -511,13 +512,13 @@ static inline int PageBuddy(struct page *page)
511512

512513
static inline void __SetPageBuddy(struct page *page)
513514
{
514-
VM_BUG_ON(atomic_read(&page->_mapcount) != -1);
515+
VM_BUG_ON_PAGE(atomic_read(&page->_mapcount) != -1, page);
515516
atomic_set(&page->_mapcount, PAGE_BUDDY_MAPCOUNT_VALUE);
516517
}
517518

518519
static inline void __ClearPageBuddy(struct page *page)
519520
{
520-
VM_BUG_ON(!PageBuddy(page));
521+
VM_BUG_ON_PAGE(!PageBuddy(page), page);
521522
atomic_set(&page->_mapcount, -1);
522523
}
523524

@@ -1401,7 +1402,7 @@ static inline bool ptlock_init(struct page *page)
14011402
* slab code uses page->slab_cache and page->first_page (for tail
14021403
* pages), which share storage with page->ptl.
14031404
*/
1404-
VM_BUG_ON(*(unsigned long *)&page->ptl);
1405+
VM_BUG_ON_PAGE(*(unsigned long *)&page->ptl, page);
14051406
if (!ptlock_alloc(page))
14061407
return false;
14071408
spin_lock_init(ptlock_ptr(page));
@@ -1492,7 +1493,7 @@ static inline bool pgtable_pmd_page_ctor(struct page *page)
14921493
static inline void pgtable_pmd_page_dtor(struct page *page)
14931494
{
14941495
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1495-
VM_BUG_ON(page->pmd_huge_pte);
1496+
VM_BUG_ON_PAGE(page->pmd_huge_pte, page);
14961497
#endif
14971498
ptlock_free(page);
14981499
}
@@ -2029,10 +2030,6 @@ extern void shake_page(struct page *p, int access);
20292030
extern atomic_long_t num_poisoned_pages;
20302031
extern int soft_offline_page(struct page *page, int flags);
20312032

2032-
extern void dump_page(struct page *page, char *reason);
2033-
extern void dump_page_badflags(struct page *page, char *reason,
2034-
unsigned long badflags);
2035-
20362033
#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
20372034
extern void clear_huge_page(struct page *page,
20382035
unsigned long addr,

‎include/linux/mmdebug.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
#ifndef LINUX_MM_DEBUG_H
22
#define LINUX_MM_DEBUG_H 1
33

4+
struct page;
5+
6+
extern void dump_page(struct page *page, char *reason);
7+
extern void dump_page_badflags(struct page *page, char *reason,
8+
unsigned long badflags);
9+
410
#ifdef CONFIG_DEBUG_VM
511
#define VM_BUG_ON(cond) BUG_ON(cond)
12+
#define VM_BUG_ON_PAGE(cond, page) \
13+
do { if (unlikely(cond)) { dump_page(page, NULL); BUG(); } } while (0)
614
#else
715
#define VM_BUG_ON(cond) BUILD_BUG_ON_INVALID(cond)
16+
#define VM_BUG_ON_PAGE(cond, page) VM_BUG_ON(cond)
817
#endif
918

1019
#ifdef CONFIG_DEBUG_VIRTUAL

‎include/linux/page-flags.h‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ static inline void ClearPageCompound(struct page *page)
412412
*/
413413
static inline int PageTransHuge(struct page *page)
414414
{
415-
VM_BUG_ON(PageTail(page));
415+
VM_BUG_ON_PAGE(PageTail(page), page);
416416
return PageHead(page);
417417
}
418418

@@ -460,25 +460,25 @@ static inline int PageTransTail(struct page *page)
460460
*/
461461
static inline int PageSlabPfmemalloc(struct page *page)
462462
{
463-
VM_BUG_ON(!PageSlab(page));
463+
VM_BUG_ON_PAGE(!PageSlab(page), page);
464464
return PageActive(page);
465465
}
466466

467467
static inline void SetPageSlabPfmemalloc(struct page *page)
468468
{
469-
VM_BUG_ON(!PageSlab(page));
469+
VM_BUG_ON_PAGE(!PageSlab(page), page);
470470
SetPageActive(page);
471471
}
472472

473473
static inline void __ClearPageSlabPfmemalloc(struct page *page)
474474
{
475-
VM_BUG_ON(!PageSlab(page));
475+
VM_BUG_ON_PAGE(!PageSlab(page), page);
476476
__ClearPageActive(page);
477477
}
478478

479479
static inline void ClearPageSlabPfmemalloc(struct page *page)
480480
{
481-
VM_BUG_ON(!PageSlab(page));
481+
VM_BUG_ON_PAGE(!PageSlab(page), page);
482482
ClearPageActive(page);
483483
}
484484

‎include/linux/pagemap.h‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static inline int page_cache_get_speculative(struct page *page)
162162
* disabling preempt, and hence no need for the "speculative get" that
163163
* SMP requires.
164164
*/
165-
VM_BUG_ON(page_count(page) == 0);
165+
VM_BUG_ON_PAGE(page_count(page) == 0, page);
166166
atomic_inc(&page->_count);
167167

168168
#else
@@ -175,7 +175,7 @@ static inline int page_cache_get_speculative(struct page *page)
175175
return 0;
176176
}
177177
#endif
178-
VM_BUG_ON(PageTail(page));
178+
VM_BUG_ON_PAGE(PageTail(page), page);
179179

180180
return 1;
181181
}
@@ -191,14 +191,14 @@ static inline int page_cache_add_speculative(struct page *page, int count)
191191
# ifdef CONFIG_PREEMPT_COUNT
192192
VM_BUG_ON(!in_atomic());
193193
# endif
194-
VM_BUG_ON(page_count(page) == 0);
194+
VM_BUG_ON_PAGE(page_count(page) == 0, page);
195195
atomic_add(count, &page->_count);
196196

197197
#else
198198
if (unlikely(!atomic_add_unless(&page->_count, count, 0)))
199199
return 0;
200200
#endif
201-
VM_BUG_ON(PageCompound(page) && page != compound_head(page));
201+
VM_BUG_ON_PAGE(PageCompound(page) && page != compound_head(page), page);
202202

203203
return 1;
204204
}
@@ -210,7 +210,7 @@ static inline int page_freeze_refs(struct page *page, int count)
210210

211211
static inline void page_unfreeze_refs(struct page *page, int count)
212212
{
213-
VM_BUG_ON(page_count(page) != 0);
213+
VM_BUG_ON_PAGE(page_count(page) != 0, page);
214214
VM_BUG_ON(count == 0);
215215

216216
atomic_set(&page->_count, count);

‎include/linux/percpu.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef __LINUX_PERCPU_H
22
#define __LINUX_PERCPU_H
33

4+
#include <linux/mmdebug.h>
45
#include <linux/preempt.h>
56
#include <linux/smp.h>
67
#include <linux/cpumask.h>

‎mm/cleancache.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ int __cleancache_get_page(struct page *page)
237237
goto out;
238238
}
239239

240-
VM_BUG_ON(!PageLocked(page));
240+
VM_BUG_ON_PAGE(!PageLocked(page), page);
241241
fake_pool_id = page->mapping->host->i_sb->cleancache_poolid;
242242
if (fake_pool_id < 0)
243243
goto out;
@@ -279,7 +279,7 @@ void __cleancache_put_page(struct page *page)
279279
return;
280280
}
281281

282-
VM_BUG_ON(!PageLocked(page));
282+
VM_BUG_ON_PAGE(!PageLocked(page), page);
283283
fake_pool_id = page->mapping->host->i_sb->cleancache_poolid;
284284
if (fake_pool_id < 0)
285285
return;
@@ -318,7 +318,7 @@ void __cleancache_invalidate_page(struct address_space *mapping,
318318
if (pool_id < 0)
319319
return;
320320

321-
VM_BUG_ON(!PageLocked(page));
321+
VM_BUG_ON_PAGE(!PageLocked(page), page);
322322
if (cleancache_get_key(mapping->host, &key) >= 0) {
323323
cleancache_ops->invalidate_page(pool_id,
324324
key, page->index);

0 commit comments

Comments
 (0)