Eliminate task stack trace duplication
Subsystems (4)
PATCH v6 1/1 Eliminate task stack trace duplication
The problem with small dmesg ring buffer like 512k is that only limited number of task traces will be logged. Sometimes we lose important information only because of too many duplicated stack traces. This problem occurs when dumping lots of stacks in a single operation, such as sysrq-T.
This patch tries to reduce the duplication of task stack trace in the dump message by hashing the task stack. The hashtable is a 32k pre-allocated buffer during bootup. Each time if we find the identical task trace in the task stack, we dump only the pid of the task which has the task trace dumped. So it is easy to back track to the full stack with the pid.
When we do the hashing, we eliminate garbage entries from stack traces. Those entries are still being printed in the dump to provide more debugging informations.
[ 58.469730] kworker/0:0 S 0000000000000000 0 4 2 0x00000000 [ 58.469735] ffff88082fcfde80 0000000000000046 ffff88082e9d8000 ffff88082fcfc010 [ 58.469739] ffff88082fce9860 0000000000011440 ffff88082fcfdfd8 ffff88082fcfdfd8 [ 58.469743] 0000000000011440 0000000000000000 ffff88082fcee180 ffff88082fce9860 [ 58.469747] Call Trace: [ 58.469751] [<ffffffff8108525a>] worker_thread+0x24b/0x250 [ 58.469754] [<ffffffff8108500f>] ? manage_workers+0x192/0x192 [ 58.469757] [<ffffffff810885bd>] kthread+0x82/0x8a [ 58.469760] [<ffffffff8141aed4>] kernel_thread_helper+0x4/0x10 [ 58.469763] [<ffffffff8108853b>] ? kthread_worker_fn+0x112/0x112 [ 58.469765] [<ffffffff8141aed0>] ? gs_change+0xb/0xb [ 58.469768] kworker/u:0 S 0000000000000004 0 5 2 0x00000000 [ 58.469773] ffff88082fcffe80 0000000000000046 ffff880800000000 ffff88082fcfe010 [ 58.469777] ffff88082fcea080 0000000000011440 ffff88082fcfffd8 ffff88082fcfffd8 [ 58.469781] 0000000000011440 0000000000000000 ffff88082fd4e9a0 ffff88082fcea080 [ 58.469785] Call Trace: [ 58.469786] <Same stack as pid 4> [ 58.470235] kworker/0:1 S 0000000000000000 0 13 2 0x00000000 [ 58.470255] ffff88082fd3fe80 0000000000000046 ffff880800000000 ffff88082fd3e010 [ 58.470279] ffff88082fcee180 0000000000011440 ffff88082fd3ffd8 ffff88082fd3ffd8 [ 58.470301] 0000000000011440 0000000000000000 ffffffff8180b020 ffff88082fcee180 [ 58.470325] Call Trace: [ 58.470332] <Same stack as pid 4>
changelog v6..v5:
1. clear saved stack trace before printing a set of stacks. this ensures the printed stack traces are not omitted messages.
2. add log level in printing duplicate stack.
3. remove the show_stack() API change, and non-x86 arch won't need further change.
4. add more inline documentations.
changelog v5..v4:
1. removed changes to Kconfig file
2. changed hashtable to keep only hash value and length of stack
3. simplified hashtable lookup
changelog v4..v3:
1. improve de-duplication by eliminating garbage entries from stack traces. with this change 793/825 stack traces were recognized as duplicates. in v3 only 482/839 were duplicates.
changelog v3..v2:
1. again better documentation on the patch description.
2. make the stack_hash_table to be allocated at compile time.
3. have better name of variable index
4. move save_dup_stack_trace() in kernel/stacktrace.c
changelog v2..v1:
1. better documentation on the patch description
2. move the spinlock inside the hash lockup, so reducing the holding time.
Note:
1. with pid namespace, we might have same pid number for different processes. i wonder how the stack trace (w/o dedup) handles the case, it uses tsk->pid as well as far as I checked.
2. the core functionality is in x86-specific code, this could be moved out to support other architectures.
3. Andrew made the suggestion of doing appending to stack_hash_table[].
--- arch/x86/include/asm/stacktrace.h | 11 +++- arch/x86/kernel/dumpstack.c | 24 ++++++- arch/x86/kernel/dumpstack_32.c | 7 +- arch/x86/kernel/dumpstack_64.c | 7 +- arch/x86/kernel/stacktrace.c | 123 +++++++++++++++++++++++++++++++++++++ include/linux/sched.h | 3 + include/linux/stacktrace.h | 4 + kernel/sched.c | 32 +++++++++- kernel/stacktrace.c | 15 +++++ 9 files changed, 211 insertions(+), 15 deletions(-)
arch/x86/include/asm/stacktrace.h +9 -2
@@ -81,13 +81,20 @@ stack_frame(struct task_struct *task, struct pt_regs *regs) }#endif +/* +* The parameter dup_stack_pid is used for task stack deduplication. +* The non-zero value of dup_stack_pid indicates the pid of the +* task with the same stack trace. +*/ extern void show_trace_log_lvl (struct task_struct *task,struct pt_regs *regs, -unsigned long *stack,unsigned long bp,char *log_lvl); +unsigned long *stack,unsigned long bp,char *log_lvl, +pid_t dup_stack_pid);extern void show_stack_log_lvl (struct task_struct *task,struct pt_regs *regs, -unsigned long *sp,unsigned long bp,char *log_lvl); +unsigned long *sp,unsigned long bp,char *log_lvl, +pid_t dup_stack_pid);extern unsigned int code_bytes;
arch/x86/kernel/dumpstack.c +20 -4
@@ -159,21 +159,37 @@ static const struct stacktrace_ops print_trace_ops = {void show_trace_log_lvl (struct task_struct *task,struct pt_regs *regs, -unsigned long *stack,unsigned long bp,char *log_lvl) +unsigned long *stack,unsigned long bp,char *log_lvl, +pid_t dup_stack_pid) {printk ("%sCall Trace:\n" , log_lvl); -dump_trace (task, regs, stack, bp, &print_trace_ops, log_lvl); +if (dup_stack_pid) +printk ("%s<Same stack as pid %d>" , log_lvl, dup_stack_pid); +else +dump_trace (task, regs, stack, bp, &print_trace_ops, log_lvl); }void show_trace (struct task_struct *task,struct pt_regs *regs,unsigned long *stack,unsigned long bp) { -show_trace_log_lvl (task, regs, stack, bp,"" ); +show_trace_log_lvl (task, regs, stack, bp,"" ,0 ); }void show_stack (struct task_struct *task,unsigned long *sp) { -show_stack_log_lvl (task,NULL , sp,0 ,"" ); +show_stack_log_lvl (task,NULL , sp,0 ,"" ,0 ); +} + +/* +* Similar to show_stack except accepting the dup_stack_pid parameter. +* The parameter indicates whether or not the caller side tries to do +* a stack dedup, and the non-zero value indicates the pid of the +* task with the same stack trace. +*/ +void show_stack_dedup (struct task_struct *task,unsigned long *sp, +pid_t dup_stack_pid) +{ +show_stack_log_lvl (task,NULL , sp,0 ,"" , dup_stack_pid); }/*
arch/x86/kernel/dumpstack_32.c +4 -3
@@ -56,7 +56,8 @@ EXPORT_SYMBOL(dump_trace);void show_stack_log_lvl (struct task_struct *task,struct pt_regs *regs, -unsigned long *sp,unsigned long bp,char *log_lvl) +unsigned long *sp,unsigned long bp,char *log_lvl, +pid_t dup_stack_pid) {unsigned long *stack;int i;
@@ -78,7 +79,7 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,touch_nmi_watchdog (); }printk (KERN_CONT "\n" ); -show_trace_log_lvl (task, regs, sp, bp, log_lvl); +show_trace_log_lvl (task, regs, sp, bp, log_lvl, dup_stack_pid); }
@@ -103,7 +104,7 @@ void show_registers(struct pt_regs *regs)u8 *ip;printk (KERN_EMERG "Stack:\n" ); -show_stack_log_lvl (NULL , regs, ®s->sp,0 ,KERN_EMERG ); +show_stack_log_lvl (NULL , regs, ®s->sp,0 ,KERN_EMERG ,0 );printk (KERN_EMERG "Code: " );
arch/x86/kernel/dumpstack_64.c +4 -3
@@ -198,7 +198,8 @@ EXPORT_SYMBOL(dump_trace);void show_stack_log_lvl (struct task_struct *task,struct pt_regs *regs, -unsigned long *sp,unsigned long bp,char *log_lvl) +unsigned long *sp,unsigned long bp,char *log_lvl, +pid_t dup_stack_pid) {unsigned long *irq_stack_end;unsigned long *irq_stack;
@@ -242,7 +243,7 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,preempt_enable ();printk (KERN_CONT "\n" ); -show_trace_log_lvl (task, regs, sp, bp, log_lvl); +show_trace_log_lvl (task, regs, sp, bp, log_lvl, dup_stack_pid); }void show_registers (struct pt_regs *regs)
@@ -271,7 +272,7 @@ void show_registers(struct pt_regs *regs)printk (KERN_EMERG "Stack:\n" );show_stack_log_lvl (NULL , regs, (unsigned long *)sp, -0 ,KERN_EMERG ); +0 ,KERN_EMERG ,0 );printk (KERN_EMERG "Code: " );
arch/x86/kernel/stacktrace.c +123 -0
135 lines not on this page · raw message
include/linux/sched.h +3 -0
@@ -297,6 +297,9 @@ extern void show_regs(struct pt_regs *);*/ extern void show_stack (struct task_struct *task,unsigned long *sp); +extern void show_stack_dedup (struct task_struct *task,unsigned long *sp, +pid_t dup_stack_pid); +void io_schedule (void );long io_schedule_timeout (long timeout);
include/linux/stacktrace.h +4 -0
@@ -21,6 +21,8 @@ extern void save_stack_trace_tsk(struct task_struct *tsk,extern void print_stack_trace (struct stack_trace *trace,int spaces); +extern void clear_dup_stack_traces (void ); +extern unsigned int save_dup_stack_trace (struct task_struct *tsk);#ifdef CONFIG_USER_STACKTRACE_SUPPORT extern void save_stack_trace_user (struct stack_trace *trace);#else
@@ -32,6 +34,8 @@ extern void save_stack_trace_user(struct stack_trace *trace);# define save_stack_trace_tsk (tsk, trace) do { } while (0)# define save_stack_trace_user (trace) do { } while (0)# define print_stack_trace (trace, spaces) do { } while (0) +# define clear_dup_stack_traces () do { } while (0) +# define save_dup_stack_trace (tsk) do { } while (0)#endif #endif
kernel/sched.c +29 -3
@@ -71,6 +71,7 @@#include <linux/ctype.h> #include <linux/ftrace.h> #include <linux/slab.h> +#include <linux/stacktrace.h> #include <asm/tlb.h> #include <asm/irq_regs.h>
@@ -5982,10 +5983,11 @@ out_unlock:static const char stat_nam[] =TASK_STATE_TO_CHAR_STR ; -void sched_show_task (struct task_struct *p) +void _sched_show_task (struct task_struct *p,int dedup) {unsigned long free =0 ;unsigned state; +pid_t dup_stack_pid =0 ; state = p->state ?__ffs (p->state) +1 :0 ; printk(KERN_INFO "%-15.15s %c", p->comm,
@@ -6008,13 +6010,37 @@ void sched_show_task(struct task_struct *p)task_pid_nr (p),task_pid_nr (p->real_parent), (unsigned long)task_thread_info(p)->flags); -show_stack (p,NULL ); +if (dedup) { + dup_stack_pid =save_dup_stack_trace (p); +show_stack_dedup (p,NULL , dup_stack_pid); + }else +show_stack (p,NULL ); +} + +void sched_show_task (struct task_struct *p) +{ +_sched_show_task (p,0 ); +} + +/* +* Eliminate task stack trace duplication in multi-task stackdump. +* Note only x86-specific code now implements the feature. +*/ +void sched_show_task_dedup (struct task_struct *p) +{ +_sched_show_task (p,1 ); }void show_state_filter (unsigned long state_filter) {struct task_struct *g, *p; +/* +* Prevent below printed stack traces from referring to previously +* printed ones. +*/ +clear_dup_stack_traces (); +#if BITS_PER_LONG ==32 printk (KERN_INFO " task PC stack pid father\n" );
@@ -6030,7 +6056,7 @@ void show_state_filter(unsigned long state_filter)*/ touch_nmi_watchdog ();if (!state_filter || (p->state & state_filter)) -sched_show_task (p); +sched_show_task_dedup (p); }while_each_thread (g, p);touch_all_softlockup_watchdogs ();
kernel/stacktrace.c +15 -0
@@ -41,3 +41,18 @@ save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace) {WARN_ONCE (1 ,KERN_INFO "save_stack_trace_regs() not implemented yet.\n" ); } + +/* +* Architectures that do not implement the task stack dedup will fallback to +* the default functionality. +*/ +__weak void +clear_dup_stack_traces (void ) +{ +} + +__weak unsigned int +save_dup_stack_trace (struct task_struct *tsk) +{ +return 0 ; +}
Signature · 8 lines
1.7.3.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 76 lines · Ying Han
The problem with small dmesg ring buffer like 512k is that only limited number of task traces will be logged. Sometimes we lose important information only because of too many duplicated stack traces. This problem occurs when dumping lots of stacks in a single operation, such as sysrq-T.
This patch tries to reduce the duplication of task stack trace in the dump message by hashing the task stack. The hashtable is a 32k pre-allocated buffer during bootup. Each time if we find the identical task trace in the task stack, we dump only the pid of the task which has the task trace dumped. So it is easy to back track to the full stack with the pid.
When we do the hashing, we eliminate garbage entries from stack traces. Those entries are still being printed in the dump to provide more debugging informations.
[ 58.469730] kworker/0:0 S 0000000000000000 0 4 2 0x00000000 [ 58.469735] ffff88082fcfde80 0000000000000046 ffff88082e9d8000 ffff88082fcfc010 [ 58.469739] ffff88082fce9860 0000000000011440 ffff88082fcfdfd8 ffff88082fcfdfd8 [ 58.469743] 0000000000011440 0000000000000000 ffff88082fcee180 ffff88082fce9860 [ 58.469747] Call Trace: [ 58.469751] [<ffffffff8108525a>] worker_thread+0x24b/0x250 [ 58.469754] [<ffffffff8108500f>] ? manage_workers+0x192/0x192 [ 58.469757] [<ffffffff810885bd>] kthread+0x82/0x8a [ 58.469760] [<ffffffff8141aed4>] kernel_thread_helper+0x4/0x10 [ 58.469763] [<ffffffff8108853b>] ? kthread_worker_fn+0x112/0x112 [ 58.469765] [<ffffffff8141aed0>] ? gs_change+0xb/0xb [ 58.469768] kworker/u:0 S 0000000000000004 0 5 2 0x00000000 [ 58.469773] ffff88082fcffe80 0000000000000046 ffff880800000000 ffff88082fcfe010 [ 58.469777] ffff88082fcea080 0000000000011440 ffff88082fcfffd8 ffff88082fcfffd8 [ 58.469781] 0000000000011440 0000000000000000 ffff88082fd4e9a0 ffff88082fcea080 [ 58.469785] Call Trace: [ 58.469786] <Same stack as pid 4> [ 58.470235] kworker/0:1 S 0000000000000000 0 13 2 0x00000000 [ 58.470255] ffff88082fd3fe80 0000000000000046 ffff880800000000 ffff88082fd3e010 [ 58.470279] ffff88082fcee180 0000000000011440 ffff88082fd3ffd8 ffff88082fd3ffd8 [ 58.470301] 0000000000011440 0000000000000000 ffffffff8180b020 ffff88082fcee180 [ 58.470325] Call Trace: [ 58.470332] <Same stack as pid 4>changelog v6..v5:
1. clear saved stack trace before printing a set of stacks. this ensures the printed stack traces are not omitted messages.
2. add log level in printing duplicate stack.
3. remove theshow_stack()API change, and non-x86 arch won't need further change.
4. add more inline documentations.changelog v5..v4:
1. removed changes toKconfigfile
2. changed hashtable to keep only hash value and length of stack
3. simplified hashtable lookupchangelog v4..v3:
1. improve de-duplication by eliminating garbage entries from stack traces. with this change 793/825 stack traces were recognized as duplicates. in v3 only 482/839 were duplicates.changelog v3..v2:
1. again better documentation on the patch description.
2. make thestack_hash_tableto be allocated at compile time.
3. have better name of variable index
4. movesave_dup_stack_trace()inkernel/stacktrace.cchangelog v2..v1:
1. better documentation on the patch description
2. move the spinlock inside the hash lockup, so reducing the holding time.Note:
1. with pid namespace, we might have same pid number for different processes. i wonder how the stack trace (w/o dedup) handles the case, it usestsk->pidas well as far as I checked.
2. the core functionality is in x86-specific code, this could be moved out to support other architectures.
3. Andrew made the suggestion of doing appending to stack_hash_table[].Signed-off-by Ying Han <[email protected]>Signed-off-by Greg Thelen <[email protected]>
Quoted 12 lines
--- arch/x86/include/asm/stacktrace.h | 11 +++- arch/x86/kernel/dumpstack.c | 24 ++++++- arch/x86/kernel/dumpstack_32.c | 7 +- arch/x86/kernel/dumpstack_64.c | 7 +- arch/x86/kernel/stacktrace.c | 123 +++++++++++++++++++++++++++++++++++++ include/linux/sched.h | 3 + include/linux/stacktrace.h | 4 + kernel/sched.c | 32 +++++++++- kernel/stacktrace.c | 15 +++++ 9 files changed, 211 insertions(+), 15 deletions(-)
Looks like something that would go through x86/debug? Probably best to cc Ingo, Peter, and Thomas.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 91 lines · David Rientjes
Quoted 86 lines · Ying Han
The problem with small dmesg ring buffer like 512k is that only limited number of task traces will be logged. Sometimes we lose important information only because of too many duplicated stack traces. This problem occurs when dumping lots of stacks in a single operation, such as sysrq-T.
This patch tries to reduce the duplication of task stack trace in the dump message by hashing the task stack. The hashtable is a 32k pre-allocated buffer during bootup. Each time if we find the identical task trace in the task stack, we dump only the pid of the task which has the task trace dumped. So it is easy to back track to the full stack with the pid.
When we do the hashing, we eliminate garbage entries from stack traces. Those entries are still being printed in the dump to provide more debugging informations.
[ 58.469730] kworker/0:0 S 0000000000000000 0 4 2
0x00000000[ 58.469735] ffff88082fcfde80 0000000000000046ffff88082e9d8000ffff88082fcfc010[ 58.469739] ffff88082fce9860 0000000000011440ffff88082fcfdfd8ffff88082fcfdfd8[ 58.469743] 0000000000011440 0000000000000000ffff88082fcee180ffff88082fce9860[ 58.469747] Call Trace:
[ 58.469751] [<ffffffff8108525a>]worker_thread+0x24b/0x250
[ 58.469754] [<ffffffff8108500f>] ?manage_workers+0x192/0x192
[ 58.469757] [<ffffffff810885bd>]kthread+0x82/0x8a
[ 58.469760] [<ffffffff8141aed4>]kernel_thread_helper+0x4/0x10
[ 58.469763] [<ffffffff8108853b>] ?kthread_worker_fn+0x112/0x112
[ 58.469765] [<ffffffff8141aed0>] ?gs_change+0xb/0xb
[ 58.469768] kworker/u:0 S 0000000000000004 0 5 20x00000000[ 58.469773] ffff88082fcffe80 0000000000000046ffff880800000000ffff88082fcfe010[ 58.469777] ffff88082fcea080 0000000000011440ffff88082fcfffd8ffff88082fcfffd8[ 58.469781] 0000000000011440 0000000000000000ffff88082fd4e9a0ffff88082fcea080[ 58.469785] Call Trace:
[ 58.469786] <Same stack as pid 4>
[ 58.470235] kworker/0:1 S 0000000000000000 0 13 20x00000000[ 58.470255] ffff88082fd3fe80 0000000000000046ffff880800000000ffff88082fd3e010[ 58.470279] ffff88082fcee180 0000000000011440ffff88082fd3ffd8ffff88082fd3ffd8[ 58.470301] 0000000000011440 0000000000000000ffffffff8180b020ffff88082fcee180[ 58.470325] Call Trace:
[ 58.470332] <Same stack as pid 4>changelog v6..v5:
1. clear saved stack trace before printing a set of stacks. this ensures the printed stack traces are not omitted messages.
2. add log level in printing duplicate stack.
3. remove theshow_stack()API change, and non-x86 arch won't need further change.
4. add more inline documentations.changelog v5..v4:
1. removed changes toKconfigfile
2. changed hashtable to keep only hash value and length of stack
3. simplified hashtable lookupchangelog v4..v3:
1. improve de-duplication by eliminating garbage entries from stack traces. with this change 793/825 stack traces were recognized as duplicates. in v3 only 482/839 were duplicates.changelog v3..v2:
1. again better documentation on the patch description.
2. make thestack_hash_tableto be allocated at compile time.
3. have better name of variable index
4. movesave_dup_stack_trace()inkernel/stacktrace.cchangelog v2..v1:
1. better documentation on the patch description
2. move the spinlock inside the hash lockup, so reducing the holding time.Note:
1. with pid namespace, we might have same pid number for different processes. i wonder how the stack trace (w/o dedup) handles the case, it usestsk->pidas well as far as I checked.
2. the core functionality is in x86-specific code, this could be moved out to support other architectures.
3. Andrew made the suggestion of doing appending to stack_hash_table[].Signed-off-by Ying Han <[email protected]>Signed-off-by Greg Thelen <[email protected]>---
arch/x86/include/asm/stacktrace.h | 11 +++-arch/x86/kernel/dumpstack.c| 24 ++++++-arch/x86/kernel/dumpstack_32.c| 7 +-arch/x86/kernel/dumpstack_64.c| 7 +-arch/x86/kernel/stacktrace.c| 123 +++++++++++++++++++++++++++++++++++++include/linux/sched.h| 3 +include/linux/stacktrace.h| 4 +
kernel/sched.c | 32 +++++++++-kernel/stacktrace.c| 15 +++++Quoted 2 lines
9 files changed, 211 insertions(+), 15 deletions(-)
Quoted 2 lines
Looks like something that would go through x86/debug? Probably best to cc Ingo, Peter, and Thomas.
Thank you David, I was about to add linux-kernel into the cc list yesterday as well.
--Ying
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>