Skip to content

Commit cecbca9

Browse files
committed
tracing: Dump either the oops's cpu source or all cpus buffers
The ftrace_dump_on_oops kernel parameter, sysctl and sysrq let one dump every cpu buffers when an oops or panic happens. It's nice when you have few cpus but it may take ages if have many, plus you miss the real origin of the problem in all the cpu traces. Sometimes, all you need is to dump the cpu buffer that triggered the opps, most of the time it is our main interest. This patch modifies ftrace_dump_on_oops to handle this choice. The ftrace_dump_on_oops kernel parameter, when it comes alone, has the same behaviour than before. But ftrace_dump_on_oops=orig_cpu will only dump the buffer of the cpu that oops'ed. Similarly, sysctl kernel.ftrace_dump_on_oops=1 and echo 1 > /proc/sys/kernel/ftrace_dump_on_oops keep their previous behaviour. But setting 2 jumps into cpu origin dump mode. v2: Fix double setup v3: Fix spelling issues reported by Randy Dunlap v4: Also update __ftrace_dump in the selftests Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com> Acked-by: David S. Miller <davem@davemloft.net> Acked-by: Steven Rostedt <rostedt@goodmis.org> Cc: Ingo Molnar <mingo@elte.hu> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Li Zefan <lizf@cn.fujitsu.com> Cc: Lai Jiangshan <laijs@cn.fujitsu.com>
1 parent b15c7b1 commit cecbca9

7 files changed

Lines changed: 64 additions & 21 deletions

File tree

‎Documentation/kernel-parameters.txt‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,8 +789,12 @@ and is between 256 and 4096 characters. It is defined in the file
789789
as early as possible in order to facilitate early
790790
boot debugging.
791791

792-
ftrace_dump_on_oops
792+
ftrace_dump_on_oops[=orig_cpu]
793793
[FTRACE] will dump the trace buffers on oops.
794+
If no parameter is passed, ftrace will dump
795+
buffers of all CPUs, but if you pass orig_cpu, it will
796+
dump only the buffer of the CPU that triggered the
797+
oops.
794798

795799
ftrace_filter=[function-list]
796800
[FTRACE] Limit the functions traced by the function

‎Documentation/trace/ftrace.txt‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,12 +1337,14 @@ ftrace_dump_on_oops must be set. To set ftrace_dump_on_oops, one
13371337
can either use the sysctl function or set it via the proc system
13381338
interface.
13391339

1340-
sysctl kernel.ftrace_dump_on_oops=1
1340+
sysctl kernel.ftrace_dump_on_oops=n
13411341

13421342
or
13431343

1344-
echo 1 > /proc/sys/kernel/ftrace_dump_on_oops
1344+
echo n > /proc/sys/kernel/ftrace_dump_on_oops
13451345

1346+
If n = 1, ftrace will dump buffers of all CPUs, if n = 2 ftrace will
1347+
only dump the buffer of the CPU that triggered the oops.
13461348

13471349
Here's an example of such a dump after a null pointer
13481350
dereference in a kernel module:

‎drivers/char/sysrq.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ static struct sysrq_key_op sysrq_showstate_blocked_op = {
289289

290290
static void sysrq_ftrace_dump(int key, struct tty_struct *tty)
291291
{
292-
ftrace_dump();
292+
ftrace_dump(DUMP_ALL);
293293
}
294294
static struct sysrq_key_op sysrq_ftrace_dump_op = {
295295
.handler = sysrq_ftrace_dump,

‎include/linux/ftrace.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,9 @@ static inline int test_tsk_trace_graph(struct task_struct *tsk)
492492
return tsk->trace & TSK_TRACE_FL_GRAPH;
493493
}
494494

495-
extern int ftrace_dump_on_oops;
495+
enum ftrace_dump_mode;
496+
497+
extern enum ftrace_dump_mode ftrace_dump_on_oops;
496498

497499
#ifdef CONFIG_PREEMPT
498500
#define INIT_TRACE_RECURSION .trace_recursion = 0,

‎include/linux/kernel.h‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,13 @@ static inline void tracing_off(void) { }
490490
static inline void tracing_off_permanent(void) { }
491491
static inline int tracing_is_on(void) { return 0; }
492492
#endif
493+
494+
enum ftrace_dump_mode {
495+
DUMP_NONE,
496+
DUMP_ALL,
497+
DUMP_ORIG,
498+
};
499+
493500
#ifdef CONFIG_TRACING
494501
extern void tracing_start(void);
495502
extern void tracing_stop(void);
@@ -571,7 +578,7 @@ __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
571578
extern int
572579
__ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
573580

574-
extern void ftrace_dump(void);
581+
extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
575582
#else
576583
static inline void
577584
ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3) { }
@@ -592,7 +599,7 @@ ftrace_vprintk(const char *fmt, va_list ap)
592599
{
593600
return 0;
594601
}
595-
static inline void ftrace_dump(void) { }
602+
static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
596603
#endif /* CONFIG_TRACING */
597604

598605
/*

‎kernel/trace/trace.c‎

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,12 @@ static cpumask_var_t __read_mostly tracing_buffer_mask;
117117
*
118118
* It is default off, but you can enable it with either specifying
119119
* "ftrace_dump_on_oops" in the kernel command line, or setting
120-
* /proc/sys/kernel/ftrace_dump_on_oops to true.
120+
* /proc/sys/kernel/ftrace_dump_on_oops
121+
* Set 1 if you want to dump buffers of all CPUs
122+
* Set 2 if you want to dump the buffer of the CPU that triggered oops
121123
*/
122-
int ftrace_dump_on_oops;
124+
125+
enum ftrace_dump_mode ftrace_dump_on_oops;
123126

124127
static int tracing_set_tracer(const char *buf);
125128

@@ -139,8 +142,17 @@ __setup("ftrace=", set_cmdline_ftrace);
139142

140143
static int __init set_ftrace_dump_on_oops(char *str)
141144
{
142-
ftrace_dump_on_oops = 1;
143-
return 1;
145+
if (*str++ != '=' || !*str) {
146+
ftrace_dump_on_oops = DUMP_ALL;
147+
return 1;
148+
}
149+
150+
if (!strcmp("orig_cpu", str)) {
151+
ftrace_dump_on_oops = DUMP_ORIG;
152+
return 1;
153+
}
154+
155+
return 0;
144156
}
145157
__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
146158

@@ -4338,7 +4350,7 @@ static int trace_panic_handler(struct notifier_block *this,
43384350
unsigned long event, void *unused)
43394351
{
43404352
if (ftrace_dump_on_oops)
4341-
ftrace_dump();
4353+
ftrace_dump(ftrace_dump_on_oops);
43424354
return NOTIFY_OK;
43434355
}
43444356

@@ -4355,7 +4367,7 @@ static int trace_die_handler(struct notifier_block *self,
43554367
switch (val) {
43564368
case DIE_OOPS:
43574369
if (ftrace_dump_on_oops)
4358-
ftrace_dump();
4370+
ftrace_dump(ftrace_dump_on_oops);
43594371
break;
43604372
default:
43614373
break;
@@ -4396,7 +4408,8 @@ trace_printk_seq(struct trace_seq *s)
43964408
trace_seq_init(s);
43974409
}
43984410

4399-
static void __ftrace_dump(bool disable_tracing)
4411+
static void
4412+
__ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
44004413
{
44014414
static arch_spinlock_t ftrace_dump_lock =
44024415
(arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
@@ -4429,12 +4442,25 @@ static void __ftrace_dump(bool disable_tracing)
44294442
/* don't look at user memory in panic mode */
44304443
trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
44314444

4432-
printk(KERN_TRACE "Dumping ftrace buffer:\n");
4433-
44344445
/* Simulate the iterator */
44354446
iter.tr = &global_trace;
44364447
iter.trace = current_trace;
4437-
iter.cpu_file = TRACE_PIPE_ALL_CPU;
4448+
4449+
switch (oops_dump_mode) {
4450+
case DUMP_ALL:
4451+
iter.cpu_file = TRACE_PIPE_ALL_CPU;
4452+
break;
4453+
case DUMP_ORIG:
4454+
iter.cpu_file = raw_smp_processor_id();
4455+
break;
4456+
case DUMP_NONE:
4457+
goto out_enable;
4458+
default:
4459+
printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
4460+
iter.cpu_file = TRACE_PIPE_ALL_CPU;
4461+
}
4462+
4463+
printk(KERN_TRACE "Dumping ftrace buffer:\n");
44384464

44394465
/*
44404466
* We need to stop all tracing on all CPUS to read the
@@ -4473,6 +4499,7 @@ static void __ftrace_dump(bool disable_tracing)
44734499
else
44744500
printk(KERN_TRACE "---------------------------------\n");
44754501

4502+
out_enable:
44764503
/* Re-enable tracing if requested */
44774504
if (!disable_tracing) {
44784505
trace_flags |= old_userobj;
@@ -4489,9 +4516,9 @@ static void __ftrace_dump(bool disable_tracing)
44894516
}
44904517

44914518
/* By default: disable tracing after the dump */
4492-
void ftrace_dump(void)
4519+
void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
44934520
{
4494-
__ftrace_dump(true);
4521+
__ftrace_dump(true, oops_dump_mode);
44954522
}
44964523

44974524
__init static int tracer_alloc_buffers(void)

‎kernel/trace/trace_selftest.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
256256
/* Maximum number of functions to trace before diagnosing a hang */
257257
#define GRAPH_MAX_FUNC_TEST 100000000
258258

259-
static void __ftrace_dump(bool disable_tracing);
259+
static void
260+
__ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode);
260261
static unsigned int graph_hang_thresh;
261262

262263
/* Wrap the real function entry probe to avoid possible hanging */
@@ -267,7 +268,7 @@ static int trace_graph_entry_watchdog(struct ftrace_graph_ent *trace)
267268
ftrace_graph_stop();
268269
printk(KERN_WARNING "BUG: Function graph tracer hang!\n");
269270
if (ftrace_dump_on_oops)
270-
__ftrace_dump(false);
271+
__ftrace_dump(false, DUMP_ALL);
271272
return 0;
272273
}
273274

0 commit comments

Comments
 (0)