]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
mm/slub: hold cpus_read_lock around flush_rcu_sheaves_on_cache()
authorQing Wang <wangqing7171@gmail.com>
Tue, 12 May 2026 03:50:35 +0000 (11:50 +0800)
committerVlastimil Babka (SUSE) <vbabka@kernel.org>
Thu, 14 May 2026 12:56:58 +0000 (14:56 +0200)
flush_rcu_sheaves_on_cache() calls queue_work_on() in a
for_each_online_cpu() loop, which requires the cpu to stay online.
But cpus_read_lock() is not held in kvfree_rcu_barrier_on_cache() and the
set of "online cpus" is subject to change.

There are two paths that call flush_rcu_sheaves_on_cache():

  // has cpus_read_lock()
  flush_all_rcu_sheaves()
    -> flush_rcu_sheaves_on_cache()

  // no cpus_read_lock()
  kvfree_rcu_barrier_on_cache()
    -> flush_rcu_sheaves_on_cache()

Fix this by holding cpus_read_lock() in kvfree_rcu_barrier_on_cache().

Why not move cpus_read_lock() from flush_all_rcu_sheaves() into
flush_rcu_sheaves_on_cache()? The reason is it would introduce a new lock
order (slab_mutex -> cpu_hotplug_lock). The reverse order
(cpu_hotplug_lock -> slab_mutex) is established by

- cpuhp_setup_state_nocalls(..., slub_cpu_setup, ...)
- kmem_cache_destroy()

The two orders together would form an AB-BA deadlock.

Finally, add lockdep_assert_cpus_held() in flush_rcu_sheaves_on_cache()
to catch the same problem in the future.

Fixes: 0f35040de593 ("mm/slab: introduce kvfree_rcu_barrier_on_cache() for cache destruction")
Cc: <stable@vger.kernel.org>
Signed-off-by: Qing Wang <wangqing7171@gmail.com>
Link: https://patch.msgid.link/20260512035035.762317-1-wangqing7171@gmail.com
Signed-off-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>
mm/slab_common.c
mm/slub.c

index d5a70a831a2a59a82221bf8f82b53a2a0a53e84c..8b661fff5eedb823aa5d286b57ae1890ba1a949b 100644 (file)
@@ -2110,7 +2110,9 @@ EXPORT_SYMBOL_GPL(kvfree_rcu_barrier);
 void kvfree_rcu_barrier_on_cache(struct kmem_cache *s)
 {
        if (cache_has_sheaves(s)) {
+               cpus_read_lock();
                flush_rcu_sheaves_on_cache(s);
+               cpus_read_unlock();
                rcu_barrier();
        }
 
index 0baa906f39ab840fa79bf7f3744d5deac8891ca8..9ad80b7f601a4f59a9497318441b33e95dc01331 100644 (file)
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -4024,6 +4024,7 @@ void flush_rcu_sheaves_on_cache(struct kmem_cache *s)
        struct slub_flush_work *sfw;
        unsigned int cpu;
 
+       lockdep_assert_cpus_held();
        mutex_lock(&flush_lock);
 
        for_each_online_cpu(cpu) {