Skip to content

Commit 2cbc482

Browse files
Zhen LeiFrederic Weisbecker
authored andcommitted
rcu: Dump memory object info if callback function is invalid
When a structure containing an RCU callback rhp is (incorrectly) freed and reallocated after rhp is passed to call_rcu(), it is not unusual for rhp->func to be set to NULL. This defeats the debugging prints used by __call_rcu_common() in kernels built with CONFIG_DEBUG_OBJECTS_RCU_HEAD=y, which expect to identify the offending code using the identity of this function. And in kernels build without CONFIG_DEBUG_OBJECTS_RCU_HEAD=y, things are even worse, as can be seen from this splat: Unable to handle kernel NULL pointer dereference at virtual address 0 ... ... PC is at 0x0 LR is at rcu_do_batch+0x1c0/0x3b8 ... ... (rcu_do_batch) from (rcu_core+0x1d4/0x284) (rcu_core) from (__do_softirq+0x24c/0x344) (__do_softirq) from (__irq_exit_rcu+0x64/0x108) (__irq_exit_rcu) from (irq_exit+0x8/0x10) (irq_exit) from (__handle_domain_irq+0x74/0x9c) (__handle_domain_irq) from (gic_handle_irq+0x8c/0x98) (gic_handle_irq) from (__irq_svc+0x5c/0x94) (__irq_svc) from (arch_cpu_idle+0x20/0x3c) (arch_cpu_idle) from (default_idle_call+0x4c/0x78) (default_idle_call) from (do_idle+0xf8/0x150) (do_idle) from (cpu_startup_entry+0x18/0x20) (cpu_startup_entry) from (0xc01530) This commit therefore adds calls to mem_dump_obj(rhp) to output some information, for example: slab kmalloc-256 start ffff410c45019900 pointer offset 0 size 256 This provides the rough size of the memory block and the offset of the rcu_head structure, which as least provides at least a few clues to help locate the problem. If the problem is reproducible, additional slab debugging can be enabled, for example, CONFIG_DEBUG_SLAB=y, which can provide significantly more information. Signed-off-by: Zhen Lei <[email protected]> Signed-off-by: Paul E. McKenney <[email protected]> Signed-off-by: Frederic Weisbecker <[email protected]>
1 parent 6e284c5 commit 2cbc482

File tree

6 files changed

+12
-0
lines changed

6 files changed

+12
-0
lines changed

kernel/rcu/rcu.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#ifndef __LINUX_RCU_H
1111
#define __LINUX_RCU_H
1212

13+
#include <linux/slab.h>
1314
#include <trace/events/rcu.h>
1415

1516
/*
@@ -248,6 +249,12 @@ static inline void debug_rcu_head_unqueue(struct rcu_head *head)
248249
}
249250
#endif /* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
250251

252+
static inline void debug_rcu_head_callback(struct rcu_head *rhp)
253+
{
254+
if (unlikely(!rhp->func))
255+
kmem_dump_obj(rhp);
256+
}
257+
251258
extern int rcu_cpu_stall_suppress_at_boot;
252259

253260
static inline bool rcu_stall_is_suppressed_at_boot(void)

kernel/rcu/srcutiny.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ void srcu_drive_gp(struct work_struct *wp)
138138
while (lh) {
139139
rhp = lh;
140140
lh = lh->next;
141+
debug_rcu_head_callback(rhp);
141142
local_bh_disable();
142143
rhp->func(rhp);
143144
local_bh_enable();

kernel/rcu/srcutree.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,7 @@ static void srcu_invoke_callbacks(struct work_struct *work)
17101710
rhp = rcu_cblist_dequeue(&ready_cbs);
17111711
for (; rhp != NULL; rhp = rcu_cblist_dequeue(&ready_cbs)) {
17121712
debug_rcu_head_unqueue(rhp);
1713+
debug_rcu_head_callback(rhp);
17131714
local_bh_disable();
17141715
rhp->func(rhp);
17151716
local_bh_enable();

kernel/rcu/tasks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ static void rcu_tasks_invoke_cbs(struct rcu_tasks *rtp, struct rcu_tasks_percpu
538538
raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
539539
len = rcl.len;
540540
for (rhp = rcu_cblist_dequeue(&rcl); rhp; rhp = rcu_cblist_dequeue(&rcl)) {
541+
debug_rcu_head_callback(rhp);
541542
local_bh_disable();
542543
rhp->func(rhp);
543544
local_bh_enable();

kernel/rcu/tiny.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ static inline bool rcu_reclaim_tiny(struct rcu_head *head)
9797

9898
trace_rcu_invoke_callback("", head);
9999
f = head->func;
100+
debug_rcu_head_callback(head);
100101
WRITE_ONCE(head->func, (rcu_callback_t)0L);
101102
f(head);
102103
rcu_lock_release(&rcu_callback_map);

kernel/rcu/tree.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,6 +2135,7 @@ static void rcu_do_batch(struct rcu_data *rdp)
21352135
trace_rcu_invoke_callback(rcu_state.name, rhp);
21362136

21372137
f = rhp->func;
2138+
debug_rcu_head_callback(rhp);
21382139
WRITE_ONCE(rhp->func, (rcu_callback_t)0L);
21392140
f(rhp);
21402141

0 commit comments

Comments
 (0)