Skip to content

Commit b0d66d8

Browse files
lorenzo-stoakesakpm00
authored andcommitted
mm/debug: introduce VM_WARN_ON_VMG() to dump VMA merge state
Patch series "mm/debug: introduce and use VM_WARN_ON_VMG()". We use a number of asserts, enabled only when CONFIG_DEBUG_VM is set, during VMA merge operations to ensure state is as expected. However, when syzkaller or the like encounters these asserts, often the information provided by the report is insufficient to narrow down what the problem is. We noticed this recently in [0], where a non-repro issue resisted debugging due to simply not having sufficient information to go on. This series improves the situation by providing VM_WARN_ON_VMG() which acts like VM_WARN_ON() (i.e. only actually being invoked if CONFIG_DEBUG_VM is set), while dumping significant information about the VMA merge state, the mm_struct describing the virtual address space, all associated VMAs and, if CONFIG_DEBUG_VM_MAPLE_TREE is set, the associated maple tree. [0]:https://lore.kernel.org/all/[email protected]/ This patch (of 2): We use a number of asserts, enabled only when CONFIG_DEBUG_VM is set, during VMA merge operations to ensure state is as expected. However, when syzkaller or the like encounters these asserts, often the information provided by the report is insufficient to narrow down what the problem is. This might not be so much of an issue if the reported problem is reproducible, but if it is a rarely encountered race or some other case which precludes a repro, it is a very big problem (see [0] for the motivating case). It is therefore sensible to provide a means by which we can easily and conveniently dump a lot more information in these circumstances. The aggregation of merge state into a single struct threaded through the operation makes this trivial - we can simply introduce a variant on VM_WARN_ON() which takes the VMA merge state object (vmg) and use that to dump information. This patch therefore introduces VM_WARN_ON_VMG() which provides this functionality. It additionally dumps full mm state, VMA state for each of the three VMAs the vmg contains (prev, next, vma) and if CONFIG_DEBUG_VM_MAPLE_TREE is enabled, dumps the maple tree from the provided VMA iterator if non-NULL. This patch has no functional impact if CONFIG_DEBUG_VM is not set. [0]:https://lore.kernel.org/all/[email protected]/ Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/13b09b52d4d103ee86acaf0ae612539648ae29e0.1735932169.git.lorenzo.stoakes@oracle.com Signed-off-by: Lorenzo Stoakes <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Jann Horn <[email protected]> Cc: Liam R. Howlett <[email protected]> Cc: Matthew Wilcox (Oracle) <[email protected]> Cc: Vlastimil Babka <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 30cee1e commit b0d66d8

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

include/linux/mmdebug.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ struct page;
99
struct vm_area_struct;
1010
struct mm_struct;
1111
struct vma_iterator;
12+
struct vma_merge_struct;
1213

1314
void dump_page(const struct page *page, const char *reason);
1415
void dump_vma(const struct vm_area_struct *vma);
1516
void dump_mm(const struct mm_struct *mm);
17+
void dump_vmg(const struct vma_merge_struct *vmg, const char *reason);
1618
void vma_iter_dump_tree(const struct vma_iterator *vmi);
1719

1820
#ifdef CONFIG_DEBUG_VM
@@ -87,6 +89,15 @@ void vma_iter_dump_tree(const struct vma_iterator *vmi);
8789
} \
8890
unlikely(__ret_warn_once); \
8991
})
92+
#define VM_WARN_ON_VMG(cond, vmg) ({ \
93+
int __ret_warn = !!(cond); \
94+
\
95+
if (unlikely(__ret_warn)) { \
96+
dump_vmg(vmg, "VM_WARN_ON_VMG(" __stringify(cond)")"); \
97+
WARN_ON(1); \
98+
} \
99+
unlikely(__ret_warn); \
100+
})
90101

91102
#define VM_WARN_ON(cond) (void)WARN_ON(cond)
92103
#define VM_WARN_ON_ONCE(cond) (void)WARN_ON_ONCE(cond)
@@ -104,9 +115,10 @@ void vma_iter_dump_tree(const struct vma_iterator *vmi);
104115
#define VM_WARN_ON_FOLIO(cond, folio) BUILD_BUG_ON_INVALID(cond)
105116
#define VM_WARN_ON_ONCE_FOLIO(cond, folio) BUILD_BUG_ON_INVALID(cond)
106117
#define VM_WARN_ON_ONCE_MM(cond, mm) BUILD_BUG_ON_INVALID(cond)
118+
#define VM_WARN_ON_VMG(cond, vmg) BUILD_BUG_ON_INVALID(cond)
107119
#define VM_WARN_ONCE(cond, format...) BUILD_BUG_ON_INVALID(cond)
108120
#define VM_WARN(cond, format...) BUILD_BUG_ON_INVALID(cond)
109-
#endif
121+
#endif /* CONFIG_DEBUG_VM */
110122

111123
#ifdef CONFIG_DEBUG_VM_IRQSOFF
112124
#define VM_WARN_ON_IRQS_ENABLED() WARN_ON_ONCE(!irqs_disabled())

mm/debug.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,77 @@ void dump_mm(const struct mm_struct *mm)
249249
}
250250
EXPORT_SYMBOL(dump_mm);
251251

252+
void dump_vmg(const struct vma_merge_struct *vmg, const char *reason)
253+
{
254+
if (reason)
255+
pr_warn("vmg %px dumped because: %s\n", vmg, reason);
256+
257+
if (!vmg) {
258+
pr_warn("vmg %px state: (NULL)\n", vmg);
259+
return;
260+
}
261+
262+
pr_warn("vmg %px state: mm %px pgoff %lx\n"
263+
"vmi %px [%lx,%lx)\n"
264+
"prev %px next %px vma %px\n"
265+
"start %lx end %lx flags %lx\n"
266+
"file %px anon_vma %px policy %px\n"
267+
"uffd_ctx %px\n"
268+
"anon_name %px\n"
269+
"merge_flags %x state %x\n",
270+
vmg, vmg->mm, vmg->pgoff,
271+
vmg->vmi, vmg->vmi ? vma_iter_addr(vmg->vmi) : 0,
272+
vmg->vmi ? vma_iter_end(vmg->vmi) : 0,
273+
vmg->prev, vmg->next, vmg->vma,
274+
vmg->start, vmg->end, vmg->flags,
275+
vmg->file, vmg->anon_vma, vmg->policy,
276+
#ifdef CONFIG_USERFAULTFD
277+
vmg->uffd_ctx.ctx,
278+
#else
279+
(void *)0,
280+
#endif
281+
vmg->anon_name,
282+
(int)vmg->merge_flags, (int)vmg->state);
283+
284+
if (vmg->mm) {
285+
pr_warn("vmg %px mm:\n", vmg);
286+
dump_mm(vmg->mm);
287+
} else {
288+
pr_warn("vmg %px mm: (NULL)\n", vmg);
289+
}
290+
291+
if (vmg->vma) {
292+
pr_warn("vmg %px vma:\n", vmg);
293+
dump_vma(vmg->vma);
294+
} else {
295+
pr_warn("vmg %px vma: (NULL)\n", vmg);
296+
}
297+
298+
if (vmg->prev) {
299+
pr_warn("vmg %px prev:\n", vmg);
300+
dump_vma(vmg->prev);
301+
} else {
302+
pr_warn("vmg %px prev: (NULL)\n", vmg);
303+
}
304+
305+
if (vmg->next) {
306+
pr_warn("vmg %px next:\n", vmg);
307+
dump_vma(vmg->next);
308+
} else {
309+
pr_warn("vmg %px next: (NULL)\n", vmg);
310+
}
311+
312+
#ifdef CONFIG_DEBUG_VM_MAPLE_TREE
313+
if (vmg->vmi) {
314+
pr_warn("vmg %px vmi:\n", vmg);
315+
vma_iter_dump_tree(vmg->vmi);
316+
} else {
317+
pr_warn("vmg %px vmi: (NULL)\n", vmg);
318+
}
319+
#endif
320+
}
321+
EXPORT_SYMBOL(dump_vmg);
322+
252323
static bool page_init_poisoning __read_mostly = true;
253324

254325
static int __init setup_vm_debug(char *str)

0 commit comments

Comments
 (0)