From: Joel Fernandes <joelagnelf@nvidia.com>
To: Puranjay Mohan <puranjay@kernel.org>
Cc: rcu@vger.kernel.org, paulmck@kernel.org
Subject: Possible debug objects issue with: rcu: Make call_rcu() safe to call from any context
Date: Wed, 4 Nov 2026 09:44:08 -0500 [thread overview]
Message-ID: <20261104144408.GA250870@joelbox> (raw)
Hello Puranjay,
My local LLM flagged a debug objects issue with the Make call_rcu() safe to
call patch.
Apparently, it is possible that a double call_rcu can result in corruption
of the deferred list before debug_objects gets a chance to look at it.
Can you take a look at my AI slop patch below that fixes this, and do some
testing on this if it is a real issue and send a proper patch? Thank you so
much.
---8<-----------------------
From 1a550f5c8386761aa9c5c5bf2ec28dcd74ecebec Mon Sep 17 00:00:00 2001
From: Joel Fernandes <joelagnelf@nvidia.com>
Date: Wed, 28 Oct 2026 06:50:58 -0400
Subject: [PATCH] [TBC] rcu: Preserve deferred lists while checking duplicate
callbacks
The deferred call_rcu() path uses the callback head as its lockless list
node. Registering that head twice can self-link the list and lose other
callbacks before rcu_do_enqueue() checks for duplicates. The IRQ-disabled
object_debug test reports a missing valid callback on both Tree and Tiny
RCU, and an invoked duplicate on Tiny RCU.
With DEBUG_OBJECTS_RCU_HEAD, stage callbacks in independent entries so
duplicate heads cannot corrupt the list before the existing checker
runs. Keep the checker in the drain instead of moving its blocking locks
into NMI/reentrant registration. Use kmalloc_nolock()/kfree_nolock() for
the entries, and drain them through the existing irq_work, barrier and
CPU-offline paths. Match Tree RCU's leak callback handling in Tiny RCU.
This costs one small allocation per deferred registration in debug
builds. If metadata cannot be allocated, retain the original intrusive
path rather than dropping valid callbacks. Duplicate containment is
therefore best-effort in that fallback, as with failed debug tracking.
Non-debug builds do not allocate entries.
Fixes: af78eca13b03 ("rcu: Make call_rcu() safe to call from any context")
Fixes: 0ed912d346fd ("rcu: Make Tiny call_rcu() safe to call from any context")
Not-Signed-off-by: Joel Fernandes <joelagnelf@nvidia.com>
---
kernel/rcu/rcu.h | 30 ++++++++++++++++++++++++++++++
kernel/rcu/tiny.c | 28 ++++++++++++++++++++++++++--
kernel/rcu/tree.c | 25 ++++++++++++++++++++++---
kernel/rcu/tree.h | 1 +
4 files changed, 79 insertions(+), 5 deletions(-)
diff --git a/kernel/rcu/rcu.h b/kernel/rcu/rcu.h
index d60444bf3a02..07613693e567 100644
--- a/kernel/rcu/rcu.h
+++ b/kernel/rcu/rcu.h
@@ -10,6 +10,7 @@
#ifndef __LINUX_RCU_H
#define __LINUX_RCU_H
+#include <linux/llist.h>
#include <linux/slab.h>
#include <trace/events/rcu.h>
@@ -583,6 +584,35 @@ static inline bool should_rcu_defer(void)
rcu_scheduler_active != RCU_SCHEDULER_INACTIVE;
}
+/*
+ * Debug checking must run after deferral, where it can take locks. Use a
+ * separate list node so duplicate heads cannot corrupt the deferred list
+ * before debug_rcu_head_queue() gets a chance to reject them.
+ * Allocation failure falls back to the intrusive list to preserve valid
+ * callbacks; duplicate containment is best-effort in that case.
+ */
+struct rcu_defer_entry {
+ struct llist_node node;
+ struct rcu_head *head;
+ rcu_callback_t func;
+};
+
+static inline struct rcu_defer_entry *rcu_defer_entry_alloc(struct rcu_head *head,
+ rcu_callback_t func)
+{
+ struct rcu_defer_entry *entry;
+
+ if (!IS_ENABLED(CONFIG_DEBUG_OBJECTS_RCU_HEAD))
+ return NULL;
+
+ entry = kmalloc_nolock(sizeof(*entry), 0, NUMA_NO_NODE);
+ if (entry) {
+ entry->head = head;
+ entry->func = func;
+ }
+ return entry;
+}
+
/* Drain an outgoing CPU's deferred SRCU callbacks; see rcutree_migrate_callbacks(). */
void srcu_offline_drain(int cpu);
diff --git a/kernel/rcu/tiny.c b/kernel/rcu/tiny.c
index ef0b0474f207..3efd151d30b1 100644
--- a/kernel/rcu/tiny.c
+++ b/kernel/rcu/tiny.c
@@ -48,14 +48,17 @@ static struct rcu_ctrlblk rcu_ctrlblk = {
/*
* The callback list is only accessed with interrupts disabled, so a call_rcu()
* that arrives with interrupts off stages the callback on a lockless list that
- * an irq_work re-issues later. One global list and irq_work suffice, as Tiny
- * RCU is uniprocessor.
+ * an irq_work re-issues later. Tiny RCU is uniprocessor, so the normal and
+ * debug lists share one global irq_work.
*/
static void rcu_defer_drain(struct irq_work *iw);
static LLIST_HEAD(rcu_defer_list);
+static LLIST_HEAD(rcu_defer_debug_list);
static struct irq_work rcu_defer_iw = IRQ_WORK_INIT_HARD(rcu_defer_drain);
static bool rcu_defer_draining;
+static void rcu_leak_callback(struct rcu_head *head) { }
+
/*
* Also called by __rcu_defer_drain() to re-issue a deferred callback, so it
* must not re-check the deferral condition.
@@ -70,6 +73,7 @@ static void rcu_do_enqueue(struct rcu_head *head, rcu_callback_t func)
pr_err("%s(): Double-freed CB %p->%pS()!!! ", __func__, head, head->func);
mem_dump_obj(head);
}
+ WRITE_ONCE(head->func, rcu_leak_callback);
return;
}
@@ -108,6 +112,17 @@ static void __rcu_defer_drain(void)
rcu_do_enqueue(head, head->func);
drained = true;
}
+ if (IS_ENABLED(CONFIG_DEBUG_OBJECTS_RCU_HEAD)) {
+ llist_for_each_safe(node, next, llist_del_all(&rcu_defer_debug_list)) {
+ struct rcu_defer_entry *entry;
+
+ entry = llist_entry(node, struct rcu_defer_entry, node);
+
+ rcu_do_enqueue(entry->head, entry->func);
+ kfree_nolock(entry);
+ drained = true;
+ }
+ }
local_irq_restore(flags);
if (drained)
@@ -124,12 +139,21 @@ static void rcu_defer_drain(struct irq_work *iw)
static void call_rcu_defer(struct rcu_head *head, rcu_callback_t func)
{
+ struct rcu_defer_entry *entry;
+
/* A re-entrant call_rcu() during the drain would livelock it; drop it. */
if (READ_ONCE(rcu_defer_draining) && !in_nmi()) {
WARN_ONCE(IS_ENABLED(CONFIG_PROVE_RCU),
"call_rcu() re-entered during callback drain; leaking callback\n");
return;
}
+ entry = rcu_defer_entry_alloc(head, func);
+ if (entry) {
+ if (llist_add(&entry->node, &rcu_defer_debug_list))
+ irq_work_queue(&rcu_defer_iw);
+ return;
+ }
+ /* Keep valid callbacks working even if debug metadata cannot be allocated. */
head->func = func;
if (llist_add((struct llist_node *)head, &rcu_defer_list))
irq_work_queue(&rcu_defer_iw);
diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c
index a8ede50dbeff..9674cc44618b 100644
--- a/kernel/rcu/tree.c
+++ b/kernel/rcu/tree.c
@@ -3229,6 +3229,16 @@ static void __rcu_defer_drain(struct rcu_data *rdp)
head->next = NULL;
rcu_do_enqueue(head, head->func, false);
}
+ if (IS_ENABLED(CONFIG_DEBUG_OBJECTS_RCU_HEAD)) {
+ llist_for_each_safe(node, next, llist_del_all(&rdp->defer_debug_head)) {
+ struct rcu_defer_entry *entry;
+
+ entry = llist_entry(node, struct rcu_defer_entry, node);
+
+ rcu_do_enqueue(entry->head, entry->func, false);
+ kfree_nolock(entry);
+ }
+ }
raw_spin_unlock_irqrestore(&rdp->defer_lock, flags);
}
@@ -3248,12 +3258,13 @@ static void rcu_defer_drain(struct irq_work *iw)
}
/*
- * Stage @head for this CPU's irq_work to re-issue once interrupts are on. Only
- * the drain side takes a lock, so this stays safe from NMI.
+ * Stage @head for this CPU's irq_work to re-issue once interrupts are on.
+ * Allocation uses nonblocking trylocks, and list insertion is lockless.
*/
static void call_rcu_defer(struct rcu_head *head, rcu_callback_t func)
{
struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
+ struct rcu_defer_entry *entry;
/*
* Instrumentation on the enqueue path can re-enter here from inside the
@@ -3265,6 +3276,13 @@ static void call_rcu_defer(struct rcu_head *head, rcu_callback_t func)
"call_rcu() re-entered during callback drain; leaking callback\n");
return;
}
+ entry = rcu_defer_entry_alloc(head, func);
+ if (entry) {
+ if (llist_add(&entry->node, &rdp->defer_debug_head))
+ irq_work_queue(&rdp->defer_work);
+ return;
+ }
+ /* Keep valid callbacks working even if debug metadata cannot be allocated. */
head->func = func;
if (llist_add((struct llist_node *)head, &rdp->defer_head))
irq_work_queue(&rdp->defer_work);
@@ -4345,6 +4363,7 @@ rcu_boot_init_percpu_data(int cpu)
rdp->last_sched_clock = jiffies;
rdp->cpu = cpu;
init_llist_head(&rdp->defer_head);
+ init_llist_head(&rdp->defer_debug_head);
raw_spin_lock_init(&rdp->defer_lock);
rdp->defer_work = IRQ_WORK_INIT_HARD(rcu_defer_drain);
rcu_boot_init_nocb_percpu_data(rdp);
@@ -4646,7 +4665,7 @@ void rcutree_migrate_callbacks(int cpu)
/*
* Callbacks deferred past the point the outgoing CPU's irq_work can run
- * sit on ->defer_head, which the ->cblist migration below does not
+ * sit on the deferred lists, which the ->cblist migration below does not
* cover. Drain them here, before the early returns.
*/
__rcu_defer_drain(rdp);
diff --git a/kernel/rcu/tree.h b/kernel/rcu/tree.h
index b7cac7a13b4f..da1f29c7c238 100644
--- a/kernel/rcu/tree.h
+++ b/kernel/rcu/tree.h
@@ -231,6 +231,7 @@ struct rcu_data {
/* Deferral of an NMI/reentrant call_rcu(); see __call_rcu_common(). */
struct llist_head defer_head;
+ struct llist_head defer_debug_head;
struct irq_work defer_work;
raw_spinlock_t defer_lock;
bool defer_draining;
--
2.54.0
next reply other threads:[~2026-11-04 14:44 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-04 14:44 Joel Fernandes [this message]
2026-11-05 16:05 ` Possible debug objects issue with: rcu: Make call_rcu() safe to call from any context Puranjay Mohan
2026-11-05 18:54 ` Joel Fernandes
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=20261104144408.GA250870@joelbox \
--to=joelagnelf@nvidia.com \
--cc=paulmck@kernel.org \
--cc=puranjay@kernel.org \
--cc=rcu@vger.kernel.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox