* [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* + lib-prime_numbers-dont-sleep-under-rcu_read_lock-in-with_primes.patch added to mm-nonmm-unstable branch
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 ` Andrew Morton
0 siblings, 0 replies; 2+ messages in thread
From: Andrew Morton @ 2026-11-20 4:27 UTC (permalink / raw)
To: mm-commits, yury.norov, tamird, kees, david, desobei, akpm
The patch titled
Subject: lib/prime_numbers: don't sleep under rcu_read_lock() in with_primes()
has been added to the -mm mm-nonmm-unstable branch. Its filename is
lib-prime_numbers-dont-sleep-under-rcu_read_lock-in-with_primes.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/lib-prime_numbers-dont-sleep-under-rcu_read_lock-in-with_primes.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a
reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various
branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Ilyas N <desobei@outlook.com>
Subject: lib/prime_numbers: don't sleep under rcu_read_lock() in with_primes()
Date: Thu, 19 Nov 2026 17:42:36 -0500
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.
Link: https://patch.msgid.link/PH7PR19MB66581508E6F6733E0C36EF92AF792@PH7PR19MB6658.namprd19.prod.outlook.com
Fixes: 313b38a6ecb4 ("lib/prime_numbers: convert self-test to KUnit")
Signed-off-by: Ilyas N <desobei@outlook.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Tamir Duberstein <tamird@kernel.org>
Cc: Yury Norov <yury.norov@gmail.com>
Cc: Kees Cook <kees@kernel.org>
Cc: David Gow <david@davidgow.net>
lib/math/prime_numbers.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
--- a/lib/math/prime_numbers.c~lib-prime_numbers-dont-sleep-under-rcu_read_lock-in-with_primes
+++ a/lib/math/prime_numbers.c
@@ -59,14 +59,15 @@ static const struct primes __rcu *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);
_
Patches currently in -mm which might be from desobei@outlook.com are
lib-prime_numbers-dont-sleep-under-rcu_read_lock-in-with_primes.patch
^ permalink raw reply [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.