Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[linux-2.6-microblaze.git] / include / linux / mmdebug.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef LINUX_MM_DEBUG_H
3 #define LINUX_MM_DEBUG_H 1
4
5 #include <linux/bug.h>
6 #include <linux/stringify.h>
7
8 struct page;
9 struct vm_area_struct;
10 struct mm_struct;
11
12 void dump_page(struct page *page, const char *reason);
13 void dump_vma(const struct vm_area_struct *vma);
14 void dump_mm(const struct mm_struct *mm);
15
16 #ifdef CONFIG_DEBUG_VM
17 #define VM_BUG_ON(cond) BUG_ON(cond)
18 #define VM_BUG_ON_PAGE(cond, page)                                      \
19         do {                                                            \
20                 if (unlikely(cond)) {                                   \
21                         dump_page(page, "VM_BUG_ON_PAGE(" __stringify(cond)")");\
22                         BUG();                                          \
23                 }                                                       \
24         } while (0)
25 #define VM_BUG_ON_FOLIO(cond, folio)                                    \
26         do {                                                            \
27                 if (unlikely(cond)) {                                   \
28                         dump_page(&folio->page, "VM_BUG_ON_FOLIO(" __stringify(cond)")");\
29                         BUG();                                          \
30                 }                                                       \
31         } while (0)
32 #define VM_BUG_ON_VMA(cond, vma)                                        \
33         do {                                                            \
34                 if (unlikely(cond)) {                                   \
35                         dump_vma(vma);                                  \
36                         BUG();                                          \
37                 }                                                       \
38         } while (0)
39 #define VM_BUG_ON_MM(cond, mm)                                          \
40         do {                                                            \
41                 if (unlikely(cond)) {                                   \
42                         dump_mm(mm);                                    \
43                         BUG();                                          \
44                 }                                                       \
45         } while (0)
46 #define VM_WARN_ON_ONCE_PAGE(cond, page)        ({                      \
47         static bool __section(".data.once") __warned;                   \
48         int __ret_warn_once = !!(cond);                                 \
49                                                                         \
50         if (unlikely(__ret_warn_once && !__warned)) {                   \
51                 dump_page(page, "VM_WARN_ON_ONCE_PAGE(" __stringify(cond)")");\
52                 __warned = true;                                        \
53                 WARN_ON(1);                                             \
54         }                                                               \
55         unlikely(__ret_warn_once);                                      \
56 })
57 #define VM_WARN_ON_ONCE_FOLIO(cond, folio)      ({                      \
58         static bool __section(".data.once") __warned;                   \
59         int __ret_warn_once = !!(cond);                                 \
60                                                                         \
61         if (unlikely(__ret_warn_once && !__warned)) {                   \
62                 dump_page(&folio->page, "VM_WARN_ON_ONCE_FOLIO(" __stringify(cond)")");\
63                 __warned = true;                                        \
64                 WARN_ON(1);                                             \
65         }                                                               \
66         unlikely(__ret_warn_once);                                      \
67 })
68
69 #define VM_WARN_ON(cond) (void)WARN_ON(cond)
70 #define VM_WARN_ON_ONCE(cond) (void)WARN_ON_ONCE(cond)
71 #define VM_WARN_ONCE(cond, format...) (void)WARN_ONCE(cond, format)
72 #define VM_WARN(cond, format...) (void)WARN(cond, format)
73 #else
74 #define VM_BUG_ON(cond) BUILD_BUG_ON_INVALID(cond)
75 #define VM_BUG_ON_PAGE(cond, page) VM_BUG_ON(cond)
76 #define VM_BUG_ON_FOLIO(cond, folio) VM_BUG_ON(cond)
77 #define VM_BUG_ON_VMA(cond, vma) VM_BUG_ON(cond)
78 #define VM_BUG_ON_MM(cond, mm) VM_BUG_ON(cond)
79 #define VM_WARN_ON(cond) BUILD_BUG_ON_INVALID(cond)
80 #define VM_WARN_ON_ONCE(cond) BUILD_BUG_ON_INVALID(cond)
81 #define VM_WARN_ON_ONCE_PAGE(cond, page)  BUILD_BUG_ON_INVALID(cond)
82 #define VM_WARN_ON_ONCE_FOLIO(cond, folio)  BUILD_BUG_ON_INVALID(cond)
83 #define VM_WARN_ONCE(cond, format...) BUILD_BUG_ON_INVALID(cond)
84 #define VM_WARN(cond, format...) BUILD_BUG_ON_INVALID(cond)
85 #endif
86
87 #ifdef CONFIG_DEBUG_VIRTUAL
88 #define VIRTUAL_BUG_ON(cond) BUG_ON(cond)
89 #else
90 #define VIRTUAL_BUG_ON(cond) do { } while (0)
91 #endif
92
93 #ifdef CONFIG_DEBUG_VM_PGFLAGS
94 #define VM_BUG_ON_PGFLAGS(cond, page) VM_BUG_ON_PAGE(cond, page)
95 #else
96 #define VM_BUG_ON_PGFLAGS(cond, page) BUILD_BUG_ON_INVALID(cond)
97 #endif
98
99 #endif