Skip to content

stackdepot, kasan, workqueue: Avoid expanding stackdepot slabs when holding raw_spin_lock

Marco Elver linux-mm lkml 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest6 patches 4 repliesIn review 5 of 6 applied
  2. v16 patches 10 repliesSuperseded

Changes in v2 from the cover letter

* Refer to __stack_depot_save() in comment of stack_depot_save().

Patches in v2 5 of 6 applied

#Subject and filesSizeRepliesApplied
1/6lib/stackdepot: include gfp.hinclude/linux/stackdepot.h+2 -00 replies
2/6lib/stackdepot: remove unused function argumentlib/stackdepot.c+4 -50 repliesApplied
3/6lib/stackdepot: introduce __stack_depot_save()include/linux/stackdepot.h lib/stackdepot.c+41 -60 repliesApplied
4/6kasan: common: provide can_alloc in kasan_save_stack()mm/kasan/common.c mm/kasan/generic.c mm/kasan/kasan.h+5 -50 repliesApplied
5/6kasan: generic: introduce kasan_record_aux_stack_noalloc()include/linux/kasan.h mm/kasan/generic.c+14 -20 repliesApplied
6/6workqueue, kasan: avoid alloc_pages() when recording stackkernel/workqueue.c+1 -13 repliesApplied

Reviews trailers per patch

All 6 patches have a Reviewed-by or Acked-by of their own; the series has one for all of them.

PatchShuah KhanSebastian Andrzej SiewiorTejun HeoAndrey Konovalov
SeriesR-b
1/6T-b A-b
2/6T-b A-b
3/6T-b A-b
4/6T-b A-b
5/6T-b A-b
6/6T-b A-b A-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 4 reviewers.

Read the v2 thread

Cover letter [PATCH v2 0/6] stackdepot, kasan, workqueue: Avoid expanding stackdepot slabs when holding raw_spin_lock

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 ]

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.

[1] https://lkml.kernel.org/r/[email protected]

v2:
* Refer to __stack_depot_save() in comment of stack_depot_save().

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           | 52 ++++++++++++++++++++++++++++++--------
 mm/kasan/common.c          |  6 ++---
 mm/kasan/generic.c         | 14 ++++++++--
 mm/kasan/kasan.h           |  2 +-
 7 files changed, 66 insertions(+), 18 deletions(-)
Signature · 1 line
2.33.0.309.g3052b89438-goog