[PATCH for-v7.5 10/11] rcu-tasks: Detect duplicate callback enqueue

From: Joel Fernandes

Date: Fri Nov 13 2026 - 01:33:28 EST


From: Kunwu Chan <kunwu.chan@xxxxxxxxx>

Detect duplicate callback enqueue attempts in the generic
RCU Tasks callback registration path. Use __ratelimit()
for the diagnostic output group.

[ joel: Activate each rcu_barrier_tasks_generic() barrier head in
debugobjects before entrainment, and undo that activation when nothing
is entrained, so that barrier callbacks no longer trigger active_state
and deactivate warnings for untracked objects. ]

Suggested-by: Paul E. McKenney <paulmck@xxxxxxxxxx>
Signed-off-by: Kunwu Chan <chentao@xxxxxxxxxx>
Reviewed-by: Joel Fernandes <jfern@xxxxxxxxxx>
Signed-off-by: Joel Fernandes <jfern@xxxxxxxxxx>
---
kernel/rcu/tasks.h | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)

diff --git a/kernel/rcu/tasks.h b/kernel/rcu/tasks.h
index a1883cf5b5d1..c1e456e08e66 100644
--- a/kernel/rcu/tasks.h
+++ b/kernel/rcu/tasks.h
@@ -6,6 +6,8 @@
*/

#ifdef CONFIG_TASKS_RCU_GENERIC
+#include <linux/mm.h>
+#include <linux/ratelimit.h>
#include "rcu_segcblist.h"

////////////////////////////////////////////////////////////////////////
@@ -337,6 +339,12 @@ static void call_rcu_tasks_iw_wakeup(struct irq_work *iwp)
}

// Enqueue a callback for the specified flavor of Tasks RCU.
+static void rcu_tasks_leak_callback(struct rcu_head *rhp) { }
+
+static DEFINE_RATELIMIT_STATE(rcu_tasks_dup_rs,
+ DEFAULT_RATELIMIT_INTERVAL,
+ DEFAULT_RATELIMIT_BURST);
+
static void call_rcu_tasks_generic(struct rcu_head *rhp, rcu_callback_t func,
struct rcu_tasks *rtp)
{
@@ -356,6 +364,17 @@ static void call_rcu_tasks_generic(struct rcu_head *rhp, rcu_callback_t func,
if (WARN_ON_ONCE(!func))
return;

+ if (debug_rcu_head_queue(rhp)) {
+ if (__ratelimit(&rcu_tasks_dup_rs)) {
+ pr_err("%s(): Duplicate CB for %s, %p->%pS()\n",
+ __func__, rtp->name, rhp, rhp->func);
+ mem_dump_obj(rhp);
+ dump_stack();
+ }
+ WRITE_ONCE(rhp->func, rcu_tasks_leak_callback);
+ return;
+ }
+
rhp->next = NULL;
rhp->func = func;
local_irq_save(flags);
@@ -448,9 +467,12 @@ static void __maybe_unused rcu_barrier_tasks_generic(struct rcu_tasks *rtp)
break;
rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
rtpcp->barrier_q_head.func = rcu_barrier_tasks_generic_cb;
+ debug_rcu_head_queue(&rtpcp->barrier_q_head);
raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
if (rcu_segcblist_entrain(&rtpcp->cblist, &rtpcp->barrier_q_head))
atomic_inc(&rtp->barrier_q_count);
+ else
+ debug_rcu_head_unqueue(&rtpcp->barrier_q_head);
raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
}
if (atomic_sub_and_test(2, &rtp->barrier_q_count))
@@ -579,6 +601,7 @@ static void rcu_tasks_invoke_cbs(struct rcu_tasks *rtp, struct rcu_tasks_percpu
raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
len = rcl.len;
for (rhp = rcu_cblist_dequeue(&rcl); rhp; rhp = rcu_cblist_dequeue(&rcl)) {
+ debug_rcu_head_unqueue(rhp);
debug_rcu_head_callback(rhp);
local_bh_disable();
rhp->func(rhp);
--
2.54.0