All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: mm-commits@vger.kernel.org,yury.norov@gmail.com,tamird@kernel.org,
	kees@kernel.org,david@davidgow.net,desobei@outlook.com,
	akpm@linux-foundation.org
Subject: + lib-prime_numbers-dont-sleep-under-rcu_read_lock-in-with_primes.patch added to mm-nonmm-unstable branch
Date: Thu, 19 Nov 2026 20:27:37 -0800	[thread overview]
Message-ID: <20261120042737.C97621F000C6@smtp.kernel.org> (raw)
In-Reply-To: <PH7PR19MB66581508E6F6733E0C36EF92AF792@PH7PR19MB6658.namprd19.prod.outlook.com>


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


      reply	other threads:[~2026-11-20  4:27 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261120042737.C97621F000C6@smtp.kernel.org \
    --to=akpm@linux-foundation.org \
    --cc=david@davidgow.net \
    --cc=desobei@outlook.com \
    --cc=kees@kernel.org \
    --cc=mm-commits@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=yury.norov@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.