Skip to contentSkip to conversation

RCU debug patches for v7.5

PATCH New Joel Fernandes 1 participant 12 messages Nov 13

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover RCU debug patches for v7.5

This series has the RCU debug patches for the v7.5 merge window.

Kunwu Chan (10):
srcu: Record callback registration stack for KASAN
srcutiny: Record callback registration stack for KASAN
rcu: Record callback registration stack for Tiny RCU
srcu: Validate callback arguments
rcu: Validate callback arguments in Tiny RCU
rcu-tasks: Validate callback arguments
srcu: Detect duplicate callback enqueue at registration
srcutiny: Detect duplicate callback enqueue
rcu-tasks: Detect duplicate callback enqueue
rcu: Improve duplicate callback diagnostics

Paul E. McKenney (1):
rcu: Print CPU that preempted stalled reader wants to run on

 include/linux/sched.h   |  2 ++
 kernel/rcu/srcutiny.c   | 34 ++++++++++++++++++++++++++++++++++
 kernel/rcu/srcutree.c   | 36 ++++++++++++++++++++++++++++++------
 kernel/rcu/tasks.h      | 30 ++++++++++++++++++++++++++++++
 kernel/rcu/tiny.c       | 11 +++++++++++
 kernel/rcu/tree.c       |  9 +++++++--
 kernel/rcu/tree_stall.h | 33 +++++++++++++++++++++++++++------
 kernel/sched/core.c     | 13 +++++++++++++
 8 files changed, 154 insertions(+), 14 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH 1/11 rcu: Print CPU that preempted stalled reader wants to run on

From: "Paul E. McKenney" <[email protected]>

This commit defines a cpu_curr_snapshot_rcu() function that invokes rcu_dereference_all(cpu_curr()) to determine which task is currently running on the specified CPU. The RCU CPU stall warning then uses this new function in order to print the task that has preempted the stalled task (the stalled task being the one that is in an RCU read-side critical section, thus preventing the current RCU grace period from completing). This modified stall-warning message also includes the CPU, along with an exclamation mark ("!") after the CPU number if that CPU is currently offline.

This is a potential step towards allowing people (or LLMs, for that matter) locate any tasks that might be preventing the preempted RCU reader task from running.

Adding a get_task_struct() for the currently running task would be a bug because we are not in a position to prevent that task from exiting. Yes, yes, because interrupts are disabled throughout, the corresponding task_struct structure cannot be freed, but following sequence of events is all too possible:

o The check_slow_task() function places a pointer to the task_struct
structure in its local variable "th".

o The corresponding task exits, reducing its task_struct structure's

	->usage to zero.  The task_struct structure is therefore passed
	to call_rcu().

o The check_slow_task() function invokes get_task_struct(th),

	setting th->usage to one.

o The rcu_print_task_stall() function invokes put_task_struct(),
again reducing ->usage to zero, and again passing the task struct to call_rcu(). This results in either double-call_rcu() or use-after-free, depending on when the RCU grace period for the first call_rcu() completes. Both outcomes are bad.

This commit therefore relies solely on disabled interrupts acting as an implicit RCU read-side critical section that prevents all of the task_struct structures from being freed, thus avoiding the need for get_task_struct().

The original get_task_struct() calls in rcu_print_task_stall() on the tasks on the ->blkd_tasks list are safe because those tasks cannot exit without first acquiring the rcu_node ->lock that this code already holds (because these tasks must remove themselves from the ->blkd_tasks list). But it is better to be consistent and rely on RCU in both cases. These pre-existing get_task_struct() and put_task_struct() calls are therefore removed.

[ paulmck: Apply feedback from K Prateek Nayak and Peter Zijlstra. ] [ paulmck: Apply feedback from Steven Rostedt. ]

---
 include/linux/sched.h   |  2 ++
 kernel/rcu/tree_stall.h | 33 +++++++++++++++++++++++++++------
 kernel/sched/core.c     | 13 +++++++++++++
 3 files changed, 42 insertions(+), 6 deletions(-)
include/linux/sched.h +2 -0
index e4f6323d7855..09e05e2b6c9a 100644
@@ -2529,3 +2529,5 @@ extern void migrate_enable(void);
 DEFINE_LOCK_GUARD_0(migrate, migrate_disable(), migrate_enable())
 
 #endif
+
+struct task_struct *cpu_curr_snapshot_rcu(int cpu);
kernel/rcu/tree_stall.h +27 -6
index 803a56af3259..08699652f51c 100644
@@ -309,6 +309,9 @@ struct rcu_stall_chk_rdr {
 	bool on_blkd_list;
 	bool rcu_rdr_running;
 	int rcu_rdr_boosted;
+	int rcu_rdr_cpu;
+	int rcu_rdr_cpu_offline;
+	struct task_struct *rcu_rdr_cpu_task;
 };
 
 /*
@@ -317,8 +320,10 @@ struct rcu_stall_chk_rdr {
  */
 static int check_slow_task(struct task_struct *t, void *arg)
 {
+	int cpu;
 	struct rcu_node *rnp;
 	struct rcu_stall_chk_rdr *rscrp = arg;
+	struct task_struct *th;
 
 	if (task_curr(t))
 		return -EBUSY; // It is running, so decline to inspect it.
@@ -327,6 +332,13 @@ static int check_slow_task(struct task_struct *t, void *arg)
 	rscrp->on_blkd_list = !list_empty(&t->rcu_node_entry);
 	rscrp->rcu_rdr_running = task_curr(t);
 	rscrp->rcu_rdr_boosted = 0;
+	rscrp->rcu_rdr_cpu = cpu = task_cpu(t);
+	rscrp->rcu_rdr_cpu_offline = cpu_is_offline(cpu);
+	lockdep_assert_irqs_disabled();
+	rscrp->rcu_rdr_cpu_task = th = cpu_curr_snapshot_rcu(cpu);
+	// There is no need for get_task_struct(t) because we are in
+	// an implicit RCU read-side critical section due to interrupts
+	// being disabled.
 	if (rscrp->on_blkd_list) {
 		rnp = READ_ONCE(t->rcu_blocked_node);
 		raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
@@ -352,6 +364,7 @@ static int rcu_print_task_stall(struct rcu_node *rnp, unsigned long flags)
 	int ndetected = 0;
 	struct rcu_stall_chk_rdr rscr;
 	struct task_struct *t;
+	struct task_struct *tc;
 	struct task_struct *ts[8];
 
 	lockdep_assert_irqs_disabled();
@@ -364,7 +377,9 @@ static int rcu_print_task_stall(struct rcu_node *rnp, unsigned long flags)
 	t = list_entry(rnp->gp_tasks->prev,
 		       struct task_struct, rcu_node_entry);
 	list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
-		get_task_struct(t);
+		// There is no need for get_task_struct(t) because we
+		// are in an implicit RCU read-side critical section due
+		// to interrupts being disabled.
 		ts[i++] = t;
 		if (i >= ARRAY_SIZE(ts))
 			break;
@@ -372,19 +387,25 @@ static int rcu_print_task_stall(struct rcu_node *rnp, unsigned long flags)
 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
 	while (i) {
 		t = ts[--i];
-		if (task_call_func(t, check_slow_task, &rscr))
+		if (task_call_func(t, check_slow_task, &rscr)) {
 			pr_cont(" P%d", t->pid);
-		else
-			pr_cont(" P%d/%d:%c%c%c%c%c%c",
+		} else {
+			tc = rscr.rcu_rdr_cpu_task;
+			pr_cont(" P%d/%d:%c%c%c%c%c%c CPU %d%s %s (%d)%s",
 				t->pid, rscr.nesting,
 				".b"[rscr.rs.b.blocked],
 				".q"[rscr.rs.b.need_qs],
 				".e"[rscr.rs.b.exp_hint],
 				".l"[rscr.on_blkd_list],
 				".R"[rscr.rcu_rdr_running],
-				".B?"[rscr.rcu_rdr_boosted]);
+				".B?"[rscr.rcu_rdr_boosted],
+				rscr.rcu_rdr_cpu,
+				rscr.rcu_rdr_cpu_offline ? "!" : "",
+				tc ? tc->comm : "???",
+				tc ? tc->pid : -1,
+				tc ? (is_idle_task(tc) ? " (idle!)" : "") : " (no task)");
+		}
 		lockdep_assert_irqs_disabled();
-		put_task_struct(t);
 		ndetected++;
 	}
 	pr_cont("\n");
kernel/sched/core.c +13 -0
index 1a088d4576f5..7449290264b1 100644
@@ -4605,6 +4605,19 @@ struct task_struct *cpu_curr_snapshot(int cpu)
 	return t;
 }
 
+/**
+ * cpu_curr_snapshot_rcu - Return the task currently running on CPU
+ * @cpu: CPU to query
+ *
+ * The caller must be within an RCU read-side critical section.
+ *
+ * Return: Pointer to task_struct of task running on @cpu.
+ */
+struct task_struct *cpu_curr_snapshot_rcu(int cpu)
+{
+	return rcu_dereference_all(cpu_curr(cpu));
+}
+
 /**
  * wake_up_process - Wake up a specific process
  * @p: The process to be woken up.
Signature · 1 line
2.54.0
#2 lore raw

PATCH 2/11 srcu: Record callback registration stack for KASAN

In reply to Joel Fernandes

From: Kunwu Chan <[email protected]>

Record the callback registration stack in __call_srcu() for KASAN diagnostics involving SRCU callbacks.

---
 kernel/rcu/srcutree.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/rcu/srcutree.c +5 -0
index 2b0e2b2d2c18..46fe2f0cef85 100644
@@ -20,6 +20,7 @@
 #include <linux/percpu.h>
 #include <linux/preempt.h>
 #include <linux/irq_work.h>
+#include <linux/kasan.h>
 #include <linux/llist.h>
 #include <linux/rcupdate_wait.h>
 #include <linux/sched.h>
@@ -1530,6 +1531,10 @@ static void __call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
 {
 	if (WARN_ON_ONCE(ssp->srcu_reader_flavor == SRCU_READ_FLAVOR_ATOMIC))
 		return; // Leak the callback rather than corrupt SRCU state.
+
+	/* Record the caller: the irq_work's stack says nothing about it. */
+	kasan_record_aux_stack(rhp);
+
 	if (should_rcu_defer()) {
 		struct srcu_defer *sndp = this_cpu_ptr(&srcu_defer);
 		struct srcu_data *sdp;
Signature · 1 line
2.54.0
#3 lore raw

PATCH 3/11 srcutiny: Record callback registration stack for KASAN

In reply to Joel Fernandes

From: Kunwu Chan <[email protected]>

Record the callback registration stack in call_srcu() for KASAN diagnostics involving Tiny SRCU callbacks.

---
 kernel/rcu/srcutiny.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/rcu/srcutiny.c +4 -0
index 63a3f0a8bc53..3ee28885b96f 100644
@@ -10,6 +10,7 @@
 
 #include <linux/export.h>
 #include <linux/irq_work.h>
+#include <linux/kasan.h>
 #include <linux/llist.h>
 #include <linux/mutex.h>
 #include <linux/preempt.h>
@@ -290,6 +291,9 @@ EXPORT_SYMBOL_GPL(srcu_defer_drain);
 void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
 	       rcu_callback_t func)
 {
+	/* Record the caller: the irq_work's stack says nothing about it. */
+	kasan_record_aux_stack(rhp);
+
 	if (should_rcu_defer()) {
 		/* A re-entrant call_srcu() during the drain would livelock it. */
 		if (READ_ONCE(srcu_defer_draining) && !in_nmi()) {
Signature · 1 line
2.54.0
#4 lore raw

PATCH 4/11 rcu: Record callback registration stack for Tiny RCU

In reply to Joel Fernandes

From: Kunwu Chan <[email protected]>

Record the callback registration stack in call_rcu() for KASAN diagnostics involving Tiny RCU callbacks.

---
 kernel/rcu/tiny.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/rcu/tiny.c +4 -0
index 656b6a682e31..bd22fa43108d 100644
@@ -12,6 +12,7 @@
 #include <linux/completion.h>
 #include <linux/interrupt.h>
 #include <linux/irq_work.h>
+#include <linux/kasan.h>
 #include <linux/llist.h>
 #include <linux/notifier.h>
 #include <linux/rcupdate_wait.h>
@@ -251,6 +252,9 @@ EXPORT_SYMBOL_GPL(synchronize_rcu);
  */
 void call_rcu(struct rcu_head *head, rcu_callback_t func)
 {
+	/* Record the caller: the irq_work's stack says nothing about it. */
+	kasan_record_aux_stack(head);
+
 	if (should_rcu_defer()) {
 		call_rcu_defer(head, func);
 		return;
Signature · 1 line
2.54.0
#5 lore raw

PATCH 5/11 srcu: Validate callback arguments

In reply to Joel Fernandes

From: Kunwu Chan <[email protected]>

Check callback-head alignment and reject NULL callback functions in the SRCU callback registration paths.

---
 kernel/rcu/srcutiny.c | 7 +++++++
 kernel/rcu/srcutree.c | 7 +++++++
 2 files changed, 14 insertions(+)
kernel/rcu/srcutiny.c +7 -0
index 3ee28885b96f..98864977bed2 100644
@@ -291,6 +291,13 @@ EXPORT_SYMBOL_GPL(srcu_defer_drain);
 void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
 	       rcu_callback_t func)
 {
+	/* Misaligned rcu_head! */
+	WARN_ON_ONCE((unsigned long)rhp & (sizeof(void *) - 1));
+
+	/* Avoid NULL dereference if callback is NULL. */
+	if (WARN_ON_ONCE(!func))
+		return;
+
 	/* Record the caller: the irq_work's stack says nothing about it. */
 	kasan_record_aux_stack(rhp);
 
kernel/rcu/srcutree.c +7 -0
index 46fe2f0cef85..76f074f67299 100644
@@ -1532,6 +1532,13 @@ static void __call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
 	if (WARN_ON_ONCE(ssp->srcu_reader_flavor == SRCU_READ_FLAVOR_ATOMIC))
 		return; // Leak the callback rather than corrupt SRCU state.
 
+	/* Misaligned rcu_head! */
+	WARN_ON_ONCE((unsigned long)rhp & (sizeof(void *) - 1));
+
+	/* Avoid NULL dereference if callback is NULL. */
+	if (WARN_ON_ONCE(!func))
+		return;
+
 	/* Record the caller: the irq_work's stack says nothing about it. */
 	kasan_record_aux_stack(rhp);
 
Signature · 1 line
2.54.0
#6 lore raw

PATCH 6/11 rcu: Validate callback arguments in Tiny RCU

In reply to Joel Fernandes

From: Kunwu Chan <[email protected]>

Check callback-head alignment and reject NULL callback functions in Tiny RCU's call_rcu() path.

---
 kernel/rcu/tiny.c | 7 +++++++
 1 file changed, 7 insertions(+)
kernel/rcu/tiny.c +7 -0
index bd22fa43108d..ef0b0474f207 100644
@@ -252,6 +252,13 @@ EXPORT_SYMBOL_GPL(synchronize_rcu);
  */
 void call_rcu(struct rcu_head *head, rcu_callback_t func)
 {
+	/* Misaligned rcu_head! */
+	WARN_ON_ONCE((unsigned long)head & (sizeof(void *) - 1));
+
+	/* Avoid NULL dereference if callback is NULL. */
+	if (WARN_ON_ONCE(!func))
+		return;
+
 	/* Record the caller: the irq_work's stack says nothing about it. */
 	kasan_record_aux_stack(head);
 
Signature · 1 line
2.54.0
#7 lore raw

PATCH 7/11 rcu-tasks: Validate callback arguments

In reply to Joel Fernandes

From: Kunwu Chan <[email protected]>

Check callback-head alignment and reject NULL callback functions in the generic RCU Tasks callback registration path.

---
 kernel/rcu/tasks.h | 7 +++++++
 1 file changed, 7 insertions(+)
kernel/rcu/tasks.h +7 -0
index 0eec90464d64..a1883cf5b5d1 100644
@@ -349,6 +349,13 @@ static void call_rcu_tasks_generic(struct rcu_head *rhp, rcu_callback_t func,
 	bool needwake;
 	struct rcu_tasks_percpu *rtpcp;
 
+	/* Misaligned rcu_head! */
+	WARN_ON_ONCE((unsigned long)rhp & (sizeof(void *) - 1));
+
+	/* Avoid NULL dereference if callback is NULL. */
+	if (WARN_ON_ONCE(!func))
+		return;
+
 	rhp->next = NULL;
 	rhp->func = func;
 	local_irq_save(flags);
Signature · 1 line
2.54.0
#8 lore raw

PATCH 8/11 srcu: Detect duplicate callback enqueue at registration

In reply to Joel Fernandes

From: Kunwu Chan <[email protected]>

Move the duplicate-callback check from srcu_do_enqueue() to __call_srcu() entry so that detected duplicate registrations are rejected before they reach the deferred-list enqueue path. Use __ratelimit() to limit the diagnostic output (error message, object dump, and stack trace) as a group.

---
 kernel/rcu/srcutree.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
kernel/rcu/srcutree.c +18 -6
index 76f074f67299..778d8507dc80 100644
@@ -23,10 +23,12 @@
 #include <linux/kasan.h>
 #include <linux/llist.h>
 #include <linux/rcupdate_wait.h>
+#include <linux/ratelimit.h>
 #include <linux/sched.h>
 #include <linux/smp.h>
 #include <linux/delay.h>
 #include <linux/module.h>
+#include <linux/mm.h>
 #include <linux/slab.h>
 #include <linux/srcu.h>
 
@@ -1511,12 +1513,6 @@ static unsigned long srcu_gp_start_if_needed(struct srcu_struct *ssp,
 static void srcu_do_enqueue(struct srcu_struct *ssp, struct rcu_head *rhp,
 			    rcu_callback_t func, bool do_norm)
 {
-	if (debug_rcu_head_queue(rhp)) {
-		/* Probable double call_srcu(), so leak the callback. */
-		WRITE_ONCE(rhp->func, srcu_leak_callback);
-		WARN_ONCE(1, "call_srcu(): Leaked duplicate callback\n");
-		return;
-	}
 	rhp->func = func;
 	(void)srcu_gp_start_if_needed(ssp, rhp, do_norm);
 }
@@ -1526,6 +1522,10 @@ static void srcu_do_enqueue(struct srcu_struct *ssp, struct rcu_head *rhp,
  * disabled, so defer when interrupts are already off rather than enqueue into
  * an operation that may be in flight on this CPU.
  */
+static DEFINE_RATELIMIT_STATE(rcu_srcu_dup_rs,
+			      DEFAULT_RATELIMIT_INTERVAL,
+			      DEFAULT_RATELIMIT_BURST);
+
 static void __call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
 			rcu_callback_t func, bool do_norm)
 {
@@ -1539,6 +1539,17 @@ static void __call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
 	if (WARN_ON_ONCE(!func))
 		return;
 
+	if (debug_rcu_head_queue(rhp)) {
+		if (__ratelimit(&rcu_srcu_dup_rs)) {
+			pr_err("%s(): Duplicate call_srcu() CB %p->%pS()\n",
+			       __func__, rhp, rhp->func);
+			mem_dump_obj(rhp);
+			dump_stack();
+		}
+		WRITE_ONCE(rhp->func, srcu_leak_callback);
+		return;
+	}
+
 	/* Record the caller: the irq_work's stack says nothing about it. */
 	kasan_record_aux_stack(rhp);
 
@@ -1554,6 +1565,7 @@ static void __call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
 		if (READ_ONCE(sndp->draining) && !in_nmi()) {
 			WARN_ONCE(IS_ENABLED(CONFIG_PROVE_RCU),
 				  "call_srcu() re-entered during callback drain; leaking callback\n");
+			debug_rcu_head_unqueue(rhp);
 			return;
 		}
 		sdp = this_cpu_ptr(ssp->sda);
Signature · 1 line
2.54.0
#9 lore raw

PATCH 9/11 srcutiny: Detect duplicate callback enqueue

In reply to Joel Fernandes

From: Kunwu Chan <[email protected]>

Detect duplicate callback enqueue attempts at call_srcu() entry, before the deferred list can be corrupted. Use __ratelimit() for the diagnostic output group.

---
 kernel/rcu/srcutiny.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
kernel/rcu/srcutiny.c +23 -0
index 98864977bed2..4c1e8d8595b3 100644
@@ -12,9 +12,11 @@
 #include <linux/irq_work.h>
 #include <linux/kasan.h>
 #include <linux/llist.h>
+#include <linux/mm.h>
 #include <linux/mutex.h>
 #include <linux/preempt.h>
 #include <linux/rcupdate_wait.h>
+#include <linux/ratelimit.h>
 #include <linux/sched.h>
 #include <linux/delay.h>
 #include <linux/srcu.h>
@@ -178,6 +180,7 @@ void srcu_drive_gp(struct work_struct *wp)
 	while (lh) {
 		rhp = lh;
 		lh = lh->next;
+		debug_rcu_head_unqueue(rhp);
 		debug_rcu_head_callback(rhp);
 		local_bh_disable();
 		rhp->func(rhp);
@@ -235,6 +238,8 @@ static void srcu_gp_start_if_needed(struct srcu_struct *ssp)
  * Also called by __srcu_defer_drain() to re-issue a deferred callback, so it
  * must not re-check the deferral condition.
  */
+static void srcu_leak_callback(struct rcu_head *rhp) { }
+
 static void srcu_do_enqueue(struct srcu_struct *ssp, struct rcu_head *rhp,
 			    rcu_callback_t func)
 {
@@ -272,6 +277,8 @@ static void __srcu_defer_drain(struct srcu_struct *ssp)
 	llist_for_each_safe(node, next, llist_del_all(&ssp->defer_cbs)) {
 		struct rcu_head *rhp = (struct rcu_head *)node;
 
+		/* Bounds a node self-linked by a double call_srcu(). */
+		rhp->next = NULL;
 		srcu_do_enqueue(ssp, rhp, rhp->func);
 	}
 	local_irq_restore(flags);
@@ -288,6 +295,10 @@ void srcu_defer_drain(struct irq_work *iw)
 }
 EXPORT_SYMBOL_GPL(srcu_defer_drain);
 
+static DEFINE_RATELIMIT_STATE(rcu_srcutiny_dup_rs,
+			      DEFAULT_RATELIMIT_INTERVAL,
+			      DEFAULT_RATELIMIT_BURST);
+
 void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
 	       rcu_callback_t func)
 {
@@ -298,6 +309,17 @@ void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
 	if (WARN_ON_ONCE(!func))
 		return;
 
+	if (debug_rcu_head_queue(rhp)) {
+		if (__ratelimit(&rcu_srcutiny_dup_rs)) {
+			pr_err("%s(): Duplicate call_srcu() CB %p->%pS()\n",
+			       __func__, rhp, rhp->func);
+			mem_dump_obj(rhp);
+			dump_stack();
+		}
+		WRITE_ONCE(rhp->func, srcu_leak_callback);
+		return;
+	}
+
 	/* Record the caller: the irq_work's stack says nothing about it. */
 	kasan_record_aux_stack(rhp);
 
@@ -306,6 +328,7 @@ void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
 		if (READ_ONCE(srcu_defer_draining) && !in_nmi()) {
 			WARN_ONCE(IS_ENABLED(CONFIG_PROVE_RCU),
 				  "call_srcu() re-entered during callback drain; leaking callback\n");
+			debug_rcu_head_unqueue(rhp);
 			return;
 		}
 		rhp->func = func;
Signature · 1 line
2.54.0
#10 lore raw

PATCH 10/11 rcu-tasks: Detect duplicate callback enqueue

In reply to Joel Fernandes

From: Kunwu Chan <[email protected]>

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. ]

---
 kernel/rcu/tasks.h | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
kernel/rcu/tasks.h +23 -0
index a1883cf5b5d1..c1e456e08e66 100644
@@ -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);
Signature · 1 line
2.54.0
#11 lore raw

PATCH 11/11 rcu: Improve duplicate callback diagnostics

In reply to Joel Fernandes

From: Kunwu Chan <[email protected]>

Use ratelimited diagnostics with dump_stack() for duplicate callback detection in rcu_do_enqueue().

---
 kernel/rcu/tree.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
kernel/rcu/tree.c +7 -2
index aea3aef7397e..a8ede50dbeff 100644
@@ -25,6 +25,7 @@
 #include <linux/rcupdate_wait.h>
 #include <linux/interrupt.h>
 #include <linux/llist.h>
+#include <linux/ratelimit.h>
 #include <linux/sched.h>
 #include <linux/sched/debug.h>
 #include <linux/nmi.h>
@@ -3154,9 +3155,12 @@ static void check_cb_ovld(struct rcu_data *rdp)
  * already disabled, and a drain of a remote CPU re-issues onto the draining
  * CPU.
  */
+static DEFINE_RATELIMIT_STATE(rcu_dup_rs,
+			      DEFAULT_RATELIMIT_INTERVAL,
+			      DEFAULT_RATELIMIT_BURST);
+
 static void rcu_do_enqueue(struct rcu_head *head, rcu_callback_t func, bool lazy_in)
 {
-	static atomic_t doublefrees;
 	unsigned long flags;
 	bool lazy;
 	struct rcu_data *rdp;
@@ -3167,9 +3171,10 @@ static void rcu_do_enqueue(struct rcu_head *head, rcu_callback_t func, bool lazy
 		 * Use rcu:rcu_callback trace event to find the previous
 		 * time callback was passed to call_rcu().
 		 */
-		if (atomic_inc_return(&doublefrees) < 4) {
+		if (__ratelimit(&rcu_dup_rs)) {
 			pr_err("%s(): Double-freed CB %p->%pS()!!!  ", __func__, head, head->func);
 			mem_dump_obj(head);
+			dump_stack();
 		}
 		WRITE_ONCE(head->func, rcu_leak_callback);
 		return;
Signature · 1 line
2.54.0