linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/6] stackdepot, kasan, workqueue: Avoid expanding stackdepot slabs when holding raw_spin_lock
@ 2021-09-07 14:13 Marco Elver
  2021-09-07 14:13 ` [PATCH 1/6] lib/stackdepot: include gfp.h Marco Elver
                   ` (8 more replies)
  0 siblings, 9 replies; 17+ messages in thread
From: Marco Elver @ 2021-09-07 14:13 UTC (permalink / raw)
  To: elver, Andrew Morton
  Cc: Shuah Khan, Tejun Heo, Lai Jiangshan, Andrey Konovalov,
	Walter Wu, Andrey Ryabinin, Alexander Potapenko, Dmitry Vyukov,
	Vijayanand Jitta, Vinayak Menon, Gustavo A. R. Silva, kasan-dev,
	linux-kernel, linux-mm, Aleksandr Nogikh, Taras Madan

Shuah Khan reported [1]:

 | When CONFIG_PROVE_RAW_LOCK_NESTING=y and CONFIG_KASAN are enabled,
 | kasan_record_aux_stack() runs into "BUG: Invalid wait context" when
 | it tries to allocate memory attempting to acquire spinlock in page
 | allocation code while holding workqueue pool raw_spinlock.
 |
 | There are several instances of this problem when block layer tries
 | to __queue_work(). Call trace from one of these instances is below:
 |
 |     kblockd_mod_delayed_work_on()
 |       mod_delayed_work_on()
 |         __queue_delayed_work()
 |           __queue_work() (rcu_read_lock, raw_spin_lock pool->lock held)
 |             insert_work()
 |               kasan_record_aux_stack()
 |                 kasan_save_stack()
 |                   stack_depot_save()
 |                     alloc_pages()
 |                       __alloc_pages()
 |                         get_page_from_freelist()
 |                           rm_queue()
 |                             rm_queue_pcplist()
 |                               local_lock_irqsave(&pagesets.lock, flags);
 |                               [ BUG: Invalid wait context triggered ]

[1] https://lkml.kernel.org/r/20210902200134.25603-1-skhan@linuxfoundation.org

PROVE_RAW_LOCK_NESTING is pointing out that (on RT kernels) the locking
rules are being violated. More generally, memory is being allocated from
a non-preemptive context (raw_spin_lock'd c-s) where it is not allowed.

To properly fix this, we must prevent stackdepot from replenishing its
"stack slab" pool if memory allocations cannot be done in the current
context: it's a bug to use either GFP_ATOMIC nor GFP_NOWAIT in certain
non-preemptive contexts, including raw_spin_locks (see gfp.h and
ab00db216c9c7).

The only downside is that saving a stack trace may fail if: stackdepot
runs out of space AND the same stack trace has not been recorded before.
I expect this to be unlikely, and a simple experiment (boot the kernel)
didn't result in any failure to record stack trace from insert_work().

The series includes a few minor fixes to stackdepot that I noticed in
preparing the series. It then introduces __stack_depot_save(), which
exposes the option to force stackdepot to not allocate any memory.
Finally, KASAN is changed to use the new stackdepot interface and
provide kasan_record_aux_stack_noalloc(), which is then used by
workqueue code.

Marco Elver (6):
  lib/stackdepot: include gfp.h
  lib/stackdepot: remove unused function argument
  lib/stackdepot: introduce __stack_depot_save()
  kasan: common: provide can_alloc in kasan_save_stack()
  kasan: generic: introduce kasan_record_aux_stack_noalloc()
  workqueue, kasan: avoid alloc_pages() when recording stack

 include/linux/kasan.h      |  2 ++
 include/linux/stackdepot.h |  6 +++++
 kernel/workqueue.c         |  2 +-
 lib/stackdepot.c           | 51 ++++++++++++++++++++++++++++++--------
 mm/kasan/common.c          |  6 ++---
 mm/kasan/generic.c         | 14 +++++++++--
 mm/kasan/kasan.h           |  2 +-
 7 files changed, 65 insertions(+), 18 deletions(-)

-- 
2.33.0.153.gba50c8fa24-goog


^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2021-09-14 12:56 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-07 14:13 [PATCH 0/6] stackdepot, kasan, workqueue: Avoid expanding stackdepot slabs when holding raw_spin_lock Marco Elver
2021-09-07 14:13 ` [PATCH 1/6] lib/stackdepot: include gfp.h Marco Elver
2021-09-14 12:11   ` Alexander Potapenko
2021-09-07 14:13 ` [PATCH 2/6] lib/stackdepot: remove unused function argument Marco Elver
2021-09-14 12:12   ` Alexander Potapenko
2021-09-07 14:13 ` [PATCH 3/6] lib/stackdepot: introduce __stack_depot_save() Marco Elver
2021-09-10 14:08   ` Sebastian Andrzej Siewior
2021-09-07 14:13 ` [PATCH 4/6] kasan: common: provide can_alloc in kasan_save_stack() Marco Elver
2021-09-07 14:13 ` [PATCH 5/6] kasan: generic: introduce kasan_record_aux_stack_noalloc() Marco Elver
2021-09-07 14:13 ` [PATCH 6/6] workqueue, kasan: avoid alloc_pages() when recording stack Marco Elver
2021-09-07 16:39   ` Tejun Heo
2021-09-07 14:17 ` [PATCH 0/6] stackdepot, kasan, workqueue: Avoid expanding stackdepot slabs when holding raw_spin_lock Marco Elver
2021-09-07 20:05 ` Shuah Khan
2021-09-10 10:50   ` Vlastimil Babka
2021-09-10 15:28     ` Sebastian Andrzej Siewior
2021-09-13  6:00       ` Marco Elver
2021-09-14 12:55 ` Alexander Potapenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).