Skip to content

Commit ac7af1f

Browse files
Waiman-Longakpm00
authored andcommitted
kasan: don't call find_vm_area() in a PREEMPT_RT kernel
The following bug report was found when running a PREEMPT_RT debug kernel. BUG: sleeping function called from invalid context at kernel/locking/spinlock_rt.c:48 in_atomic(): 1, irqs_disabled(): 1, non_block: 0, pid: 140605, name: kunit_try_catch preempt_count: 1, expected: 0 Call trace: rt_spin_lock+0x70/0x140 find_vmap_area+0x84/0x168 find_vm_area+0x1c/0x50 print_address_description.constprop.0+0x2a0/0x320 print_report+0x108/0x1f8 kasan_report+0x90/0xc8 Since commit e30a036 ("kasan: make report_lock a raw spinlock"), report_lock was changed to raw_spinlock_t to fix another similar PREEMPT_RT problem. That alone isn't enough to cover other corner cases. print_address_description() is always invoked under the report_lock. The context under this lock is always atomic even on PREEMPT_RT. find_vm_area() acquires vmap_node::busy.lock which is a spinlock_t, becoming a sleeping lock on PREEMPT_RT and must not be acquired in atomic context. Don't invoke find_vm_area() on PREEMPT_RT and just print the address. Non-PREEMPT_RT builds remain unchanged. Add a DEFINE_WAIT_OVERRIDE_MAP() macro to tell lockdep that this lock nesting is allowed because the PREEMPT_RT part (which is invalid) has been taken care of. This macro was first introduced in commit 0cce06b ("debugobjects,locking: Annotate debug_object_fill_pool() wait type violation"). Link: https://lkml.kernel.org/r/[email protected] Fixes: e30a036 ("kasan: make report_lock a raw spinlock") Signed-off-by: Waiman Long <[email protected]> Suggested-by: Andrey Konovalov <[email protected]> Reviewed-by: Andrey Konovalov <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: Andrey Ryabinin <[email protected]> Cc: Dmitriy Vyukov <[email protected]> Cc: Steven Rostedt <[email protected]> Cc: Mariano Pache <[email protected]> Cc: Sebastian Andrzej Siewior <[email protected]> Cc: Vincenzo Frascino <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 4998a6f commit ac7af1f

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

mm/kasan/report.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,36 @@ static inline bool init_task_stack_addr(const void *addr)
370370
sizeof(init_thread_union.stack));
371371
}
372372

373+
/*
374+
* This function is invoked with report_lock (a raw_spinlock) held. A
375+
* PREEMPT_RT kernel cannot call find_vm_area() as it will acquire a sleeping
376+
* rt_spinlock.
377+
*
378+
* For !RT kernel, the PROVE_RAW_LOCK_NESTING config option will print a
379+
* lockdep warning for this raw_spinlock -> spinlock dependency. This config
380+
* option is enabled by default to ensure better test coverage to expose this
381+
* kind of RT kernel problem. This lockdep splat, however, can be suppressed
382+
* by using DEFINE_WAIT_OVERRIDE_MAP() if it serves a useful purpose and the
383+
* invalid PREEMPT_RT case has been taken care of.
384+
*/
385+
static inline struct vm_struct *kasan_find_vm_area(void *addr)
386+
{
387+
static DEFINE_WAIT_OVERRIDE_MAP(vmalloc_map, LD_WAIT_SLEEP);
388+
struct vm_struct *va;
389+
390+
if (IS_ENABLED(CONFIG_PREEMPT_RT))
391+
return NULL;
392+
393+
/*
394+
* Suppress lockdep warning and fetch vmalloc area of the
395+
* offending address.
396+
*/
397+
lock_map_acquire_try(&vmalloc_map);
398+
va = find_vm_area(addr);
399+
lock_map_release(&vmalloc_map);
400+
return va;
401+
}
402+
373403
static void print_address_description(void *addr, u8 tag,
374404
struct kasan_report_info *info)
375405
{
@@ -399,7 +429,7 @@ static void print_address_description(void *addr, u8 tag,
399429
}
400430

401431
if (is_vmalloc_addr(addr)) {
402-
struct vm_struct *va = find_vm_area(addr);
432+
struct vm_struct *va = kasan_find_vm_area(addr);
403433

404434
if (va) {
405435
pr_err("The buggy address belongs to the virtual mapping at\n"
@@ -409,6 +439,8 @@ static void print_address_description(void *addr, u8 tag,
409439
pr_err("\n");
410440

411441
page = vmalloc_to_page(addr);
442+
} else {
443+
pr_err("The buggy address %px belongs to a vmalloc virtual mapping\n", addr);
412444
}
413445
}
414446

0 commit comments

Comments
 (0)