All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] lib/prime_numbers: don't sleep under rcu_read_lock() in with_primes()
@ 2026-11-19 22:42 Ilyas N
  2026-11-20  4:27 ` + lib-prime_numbers-dont-sleep-under-rcu_read_lock-in-with_primes.patch added to mm-nonmm-unstable branch Andrew Morton
  0 siblings, 1 reply; 2+ messages in thread
From: Ilyas N @ 2026-11-19 22:42 UTC (permalink / raw)
  To: Andrew Morton, Tamir Duberstein
  Cc: Yury Norov, Kees Cook, David Gow, linux-kselftest, kunit-dev,
	linux-kernel, Ilyas N

The KUnit suite exit hook dumps the primes cache through with_primes(),
which calls its callback inside an RCU read-side critical section. The
callback, dump_primes(), logs with kunit_info(). With
CONFIG_KUNIT_DEBUGFS=y that also appends to the suite log, which
allocates with GFP_KERNEL and may sleep. With CONFIG_PREEMPT_RCU=y and
CONFIG_DEBUG_ATOMIC_SLEEP=y, every run of the suite reports:

  BUG: sleeping function called from invalid context at ./include/linux/sched/mm.h:322
  in_atomic(): 0, irqs_disabled(): 0, non_block: 0, pid: 1, name: swapper/0
  preempt_count: 0, expected: 0
  RCU nest depth: 1, expected: 0
  Call Trace:
  __might_resched
  __kmalloc_cache_noprof
  string_stream_vadd
  kunit_log_append
  dump_primes
  with_primes
  kunit_run_tests

All updates of the primes pointer are serialized by the mutex in
prime_numbers.c. Hold that mutex in with_primes() instead of the RCU
read lock. The pointer stays valid for the callback, and the callback
is allowed to sleep. with_primes() is only built for the KUnit test,
and the suite exit hook is its only caller.

Allocating with GFP_KERNEL under the mutex adds no new constraint on
the callers of the library. The only other place that takes the mutex
outside of module exit is expand_to_next_prime(), which does a
GFP_KERNEL allocation right before taking it.

Found by running the KUnit suites on next-20261117 with KASAN, lockdep
and CONFIG_DEBUG_ATOMIC_SLEEP enabled. The problem was found and the
patch was written with the help of an AI coding assistant (Claude Opus
5.5 and Claude Fable 5.2). Tested in x86_64 QEMU/KVM guests with 1 and
8 vCPUs, running only this suite. The splat shows up on every boot
before the patch and is gone with it, lockdep stays quiet, and the dump
is still printed and still lands in the debugfs log of the suite.

Fixes: 313b38a6ecb4 ("lib/prime_numbers: convert self-test to KUnit")
Signed-off-by: Ilyas N <desobei@outlook.com>
---
 lib/math/prime_numbers.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/lib/math/prime_numbers.c b/lib/math/prime_numbers.c
index 95a6f7960db9..2b28bbec8384 100644
--- a/lib/math/prime_numbers.c
+++ b/lib/math/prime_numbers.c
@@ -59,14 +59,15 @@ static const struct primes __rcu *primes = RCU_INITIALIZER(&small_primes);
 
 #if IS_ENABLED(CONFIG_PRIME_NUMBERS_KUNIT_TEST)
 /*
- * Calls the callback under RCU lock. The callback must not retain
- * the primes pointer.
+ * Calls the callback with the lock held, so that the primes cannot be
+ * replaced under it and the callback is allowed to sleep. The callback
+ * must not retain the primes pointer.
  */
 void with_primes(void *ctx, primes_fn fn)
 {
-	rcu_read_lock();
-	fn(ctx, rcu_dereference(primes));
-	rcu_read_unlock();
+	mutex_lock(&lock);
+	fn(ctx, rcu_dereference_protected(primes, lockdep_is_held(&lock)));
+	mutex_unlock(&lock);
 }
 EXPORT_SYMBOL(with_primes);
 
-- 
2.53.0


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

end of thread, other threads:[~2026-11-20  4:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 22:42 [PATCH] lib/prime_numbers: don't sleep under rcu_read_lock() in with_primes() Ilyas N
2026-11-20  4:27 ` + lib-prime_numbers-dont-sleep-under-rcu_read_lock-in-with_primes.patch added to mm-nonmm-unstable branch Andrew Morton

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.