Skip to contentSkip to conversation

Eliminate task stack trace duplication.

PATCH Superseded Ying Han 3 participants 6 messages May 3, 2011

Subsystems (6)

v1 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Ying Han

author
#0 lore raw

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

Sample output:

[  491.283527] kworker/0:0     S 0000000000000000     0     4      2 0x00000000
[  491.283532]  ffff88082fcf5e80 0000000000000046 ffff88085fc0dfc0 ffff88085f805c00
[  491.283536]  ffff88082fcf4010 ffff88082fce9830 0000000000011940 ffff88082fcf5fd8
[  491.283539]  ffff88082fcf5fd8 0000000000011940 ffff88082fce8000 ffff88082fce9830
[  491.283543] Call Trace:
[  491.283546]  [<ffffffff8108417a>] worker_thread+0x24b/0x250
[  491.283549]  [<ffffffff81083f2f>] ? worker_thread+0x0/0x250
[  491.283552]  [<ffffffff810874dd>] kthread+0x82/0x8a
[  491.283555]  [<ffffffff814120d4>] kernel_thread_helper+0x4/0x10
[  491.283558]  [<ffffffff8108745b>] ? kthread+0x0/0x8a
[  491.283560]  [<ffffffff814120d0>] ? kernel_thread_helper+0x0/0x10
[  491.283630] kworker/1:0     S 0000000000000001     0     8      2 0x00000000
[  491.283636]  ffff88082fd27e80 0000000000000046 ffff88082fd27e20 ffff88082fd16080
[  491.283639]  ffff88082fd26010 ffff88082fceb870 0000000000011940 ffff88082fd27fd8
[  491.283643]  ffff88082fd27fd8 0000000000011940 ffff88082ffbd0a0 ffff88082fceb870
[  491.283647] Call Trace:
[  491.283648] <Same stack as pid 4>
[  491.283710] kworker/2:0     S 0000000000000002     0    12      2 0x00000000
[  491.283715]  ffff88082fd35e80 0000000000000046 ffff88082fd35e20 ffff88082fd16400
[  491.283719]  ffff88082fd34010 ffff88082fcee0c0 0000000000011940 ffff88082fd35fd8
[  491.283723]  ffff88082fd35fd8 0000000000011940 ffff88082ffbc890 ffff88082fcee0c0
[  491.283726] Call Trace:
[  491.283728] <Same stack as pid 4>
---
 arch/x86/Kconfig                  |    3 +
 arch/x86/include/asm/stacktrace.h |    4 +-
 arch/x86/kernel/dumpstack.c       |   18 ++++--
 arch/x86/kernel/dumpstack_32.c    |    6 +-
 arch/x86/kernel/dumpstack_64.c    |   10 +++-
 arch/x86/kernel/pci-gart_64.c     |    2 +-
 arch/x86/kernel/stacktrace.c      |  109 +++++++++++++++++++++++++++++++++++++
 drivers/tty/sysrq.c               |    2 +-
 include/linux/sched.h             |    9 +++-
 init/main.c                       |    1 +
 kernel/debug/kdb/kdb_bt.c         |    8 ++--
 kernel/rtmutex-debug.c            |    2 +-
 kernel/sched.c                    |   24 +++++++-
 13 files changed, 173 insertions(+), 25 deletions(-)
arch/x86/Kconfig +3 -0
index 46d5be2..38597f2 100644
@@ -103,6 +103,9 @@ config LOCKDEP_SUPPORT
 config STACKTRACE_SUPPORT
 	def_bool y
 
+config STACKTRACE
+	def_bool y
+
 config HAVE_LATENCYTOP_SUPPORT
 	def_bool y
 
arch/x86/include/asm/stacktrace.h +2 -2
index 52b5c7e..f78ec95 100644
@@ -86,11 +86,11 @@ stack_frame(struct task_struct *task, struct pt_regs *regs)
 
 extern void
 show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
-		   unsigned long *stack, char *log_lvl);
+		   unsigned long *stack, char *log_lvl, int index);
 
 extern void
 show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
-		   unsigned long *sp, char *log_lvl);
+		   unsigned long *sp, char *log_lvl, int index);
 
 extern unsigned int code_bytes;
 
arch/x86/kernel/dumpstack.c +12 -6
index b3f9a66..c93ac30 100644
@@ -175,21 +175,27 @@ static const struct stacktrace_ops print_trace_ops = {
 
 void
 show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
-		unsigned long *stack, char *log_lvl)
+		unsigned long *stack, char *log_lvl, int index)
 {
-	printk("%sCall Trace:\n", log_lvl);
-	dump_trace(task, regs, stack, &print_trace_ops, log_lvl);
+	if (index) {
+		printk("%sCall Trace:\n", log_lvl);
+		printk("<Same stack as pid %d>\n\n", index);
+	} else {
+		printk("%sCall Trace:\n", log_lvl);
+		dump_trace(task, regs, stack, &print_trace_ops, log_lvl);
+	}
 }
 
 void show_trace(struct task_struct *task, struct pt_regs *regs,
 		unsigned long *stack)
 {
-	show_trace_log_lvl(task, regs, stack, "");
+	show_trace_log_lvl(task, regs, stack, "", 0);
 }
 
-void show_stack(struct task_struct *task, unsigned long *sp)
+void show_stack(struct task_struct *task, unsigned long *sp,
+		int index)
 {
-	show_stack_log_lvl(task, NULL, sp, "");
+	show_stack_log_lvl(task, NULL, sp, "", index);
 }
 
 /*
arch/x86/kernel/dumpstack_32.c +3 -3
index 74cc1ed..b824a13 100644
@@ -55,7 +55,7 @@ EXPORT_SYMBOL(dump_trace);
 
 void
 show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
-		   unsigned long *sp, char *log_lvl)
+		   unsigned long *sp, char *log_lvl, int index)
 {
 	unsigned long *stack;
 	int i;
@@ -77,7 +77,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, log_lvl);
+	show_trace_log_lvl(task, regs, sp, log_lvl, index);
 }
 
 
@@ -102,7 +102,7 @@ void show_registers(struct pt_regs *regs)
 		u8 *ip;
 
 		printk(KERN_EMERG "Stack:\n");
-		show_stack_log_lvl(NULL, regs, &regs->sp, KERN_EMERG);
+		show_stack_log_lvl(NULL, regs, &regs->sp, KERN_EMERG, 0);
 
 		printk(KERN_EMERG "Code: ");
 
arch/x86/kernel/dumpstack_64.c +7 -3
index a6b6fcf..46bef00 100644
@@ -225,7 +225,7 @@ EXPORT_SYMBOL(dump_trace);
 
 void
 show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
-		   unsigned long *sp, char *log_lvl)
+		   unsigned long *sp, char *log_lvl, int index)
 {
 	unsigned long *irq_stack_end;
 	unsigned long *irq_stack;
@@ -269,7 +269,11 @@ 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, log_lvl);
+	if (index) {
+		printk(KERN_CONT "%sCall Trace:\n", log_lvl);
+		printk(KERN_CONT "<Same stack as pid %d>\n\n", index);
+	} else
+		show_trace_log_lvl(task, regs, sp, log_lvl, index);
 }
 
 void show_registers(struct pt_regs *regs)
@@ -298,7 +302,7 @@ void show_registers(struct pt_regs *regs)
 
 		printk(KERN_EMERG "Stack:\n");
 		show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
-				   KERN_EMERG);
+				   KERN_EMERG, 0);
 
 		printk(KERN_EMERG "Code: ");
 
arch/x86/kernel/pci-gart_64.c +1 -1
index c01ffa5..3b8f523 100644
@@ -162,7 +162,7 @@ static void dump_leak(void)
 		return;
 	dump = 1;
 
-	show_stack(NULL, NULL);
+	show_stack(NULL, NULL, 0);
 	debug_dma_dump_mappings(NULL);
 }
 #endif
arch/x86/kernel/stacktrace.c +109 -0

121 lines not on this page · raw message

drivers/tty/sysrq.c +1 -1
index 81f1395..38f4669 100644
@@ -214,7 +214,7 @@ static void showacpu(void *dummy)
 
 	spin_lock_irqsave(&show_lock, flags);
 	printk(KERN_INFO "CPU%d:\n", smp_processor_id());
-	show_stack(NULL, NULL);
+	show_stack(NULL, NULL, 0);
 	spin_unlock_irqrestore(&show_lock, flags);
 }
 
include/linux/sched.h +8 -1
index 98fc7ed..8066c39 100644
@@ -277,6 +277,13 @@ extern int get_nohz_timer_target(void);
 static inline void select_nohz_load_balancer(int stop_tick) { }
 #endif
 
+extern void __init stack_trace_hash_init(void);
+extern void stack_trace_hash_clean(void);
+extern unsigned int save_dup_stack_trace(struct task_struct *tsk);
+
+extern spinlock_t stack_hash_lock;
+extern struct task_stack *stack_hash_table;
+
 /*
  * Only dump TASK_* tasks. (0 for all tasks)
  */
@@ -294,7 +301,7 @@ extern void show_regs(struct pt_regs *);
  * task), SP is the stack pointer of the first frame that should be shown in the back
  * trace (or NULL if the entire call-chain of the task should be shown).
  */
-extern void show_stack(struct task_struct *task, unsigned long *sp);
+extern void show_stack(struct task_struct *task, unsigned long *sp, int index);
 
 void io_schedule(void);
 long io_schedule_timeout(long timeout);
init/main.c +1 -0
index 4a9479e..8255ac5 100644
@@ -614,6 +614,7 @@ asmlinkage void __init start_kernel(void)
 	taskstats_init_early();
 	delayacct_init();
 
+	stack_trace_hash_init();
 	check_bugs();
 
 	acpi_early_init(); /* before LAPIC and SMP init */
kernel/debug/kdb/kdb_bt.c +4 -4
index 2f62fe8..ff8c6ad 100644
@@ -26,15 +26,15 @@ static void kdb_show_stack(struct task_struct *p, void *addr)
 	kdb_trap_printk++;
 	kdb_set_current_task(p);
 	if (addr) {
-		show_stack((struct task_struct *)p, addr);
+		show_stack((struct task_struct *)p, addr, 0);
 	} else if (kdb_current_regs) {
 #ifdef CONFIG_X86
-		show_stack(p, &kdb_current_regs->sp);
+		show_stack(p, &kdb_current_regs->sp, 0);
 #else
-		show_stack(p, NULL);
+		show_stack(p, NULL, 0);
 #endif
 	} else {
-		show_stack(p, NULL);
+		show_stack(p, NULL, 0);
 	}
 	console_loglevel = old_lvl;
 	kdb_trap_printk--;
kernel/rtmutex-debug.c +1 -1
index 3c7cbc2..e636067 100644
@@ -171,7 +171,7 @@ void debug_rt_mutex_print_deadlock(struct rt_mutex_waiter *waiter)
 
 	printk("\n%s/%d's [blocked] stackdump:\n\n",
 		task->comm, task_pid_nr(task));
-	show_stack(task, NULL);
+	show_stack(task, NULL, 0);
 	printk("\n%s/%d's [current] stackdump:\n\n",
 		current->comm, task_pid_nr(current));
 	dump_stack();
kernel/sched.c +21 -3
index f4c2ec2..b7d66ff 100644
@@ -5648,10 +5648,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;
+	int index = 0;
 
 	state = p->state ? __ffs(p->state) + 1 : 0;
 	printk(KERN_INFO "%-15.15s %c", p->comm,
@@ -5674,7 +5675,19 @@ 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 && stack_hash_table)
+		index = save_dup_stack_trace(p);
+	show_stack(p, NULL, index);
+}
+
+void sched_show_task(struct task_struct *p)
+{
+	_sched_show_task(p, 0);
+}
+
+void sched_show_task_dedup(struct task_struct *p)
+{
+	_sched_show_task(p, 1);
 }
 
 void show_state_filter(unsigned long state_filter)
@@ -5689,6 +5702,10 @@ void show_state_filter(unsigned long state_filter)
 		"  task                        PC stack   pid father\n");
 #endif
 	read_lock(&tasklist_lock);
+
+	spin_lock(&stack_hash_lock);
+	stack_trace_hash_clean();
+
 	do_each_thread(g, p) {
 		/*
 		 * reset the NMI-timeout, listing all files on a slow
@@ -5696,9 +5713,10 @@ 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);
 
+	spin_unlock(&stack_hash_lock);
 	touch_all_softlockup_watchdogs();
 
 #ifdef CONFIG_SCHED_DEBUG
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>
#1 lore raw
Quoted 3 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.

I like it. I often overlook information from staring myself blind on way too many duplicate stack traces.

Quoted 3 lines

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.

This changelog doesn't tell the whole story of what the code does.

It appears to store stack traces in the table, and use the hash to look them up. Somehow there's a global pointer, called cur_stack, involved too.

The code looks correct, but somehow I'm not happy with it. Having said that, I also don't have ideas on how to make it better.

If nobody else knows how to make this code better, maybe it should just be merged as is. I hope someone has ideas, though :)

Signature · 8 lines
All rights reversed

--
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>
#3 lore raw
In reply to Ying Han
Quoted 7 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 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.

Nice idea! This makes it a lot more readable too.

Can we compress the register values too? (e.g. by not printing that many 0s and replacing ffff8 with <k> or so)

In fact I don't remember needing the register values for anything. Maybe they could be just not printed by default?

kernel/sched.c void show_state_filter(unsigned long state_filter) · line 5706

Quoted 4 lines

#endif

kernel/sched.c +2 -0
 	read_lock(&tasklist_lock);
+
+	spin_lock(&stack_hash_lock);

The long hold lock scares me a little bit for a unstable system. Could you only hold it while hashing/unhashing?

Also when you can't get it fall back to something else.

-Andi

Signature · 8 lines
[email protected] -- Speaking for myself only

--
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>
author
#4 lore raw
Quoted 10 lines · Andi Kleen
Quoted 7 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 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.

Quoted 7 lines

Nice idea!  This makes it a lot more readable too.

Can we compress the register values too? (e.g. by not printing that many 0s and replacing ffff8 with <k> or so)

In fact I don't remember needing the register values for anything. Maybe they could be just not printed by default?

I can look into that, but i might not have time working on this patch soon. If this is something
nice to have, maybe we can consider merge the first part in and then add on top of it?

Quoted 8 lines
Quoted 5 lines

 #endif
      read_lock(&tasklist_lock);
+
+     spin_lock(&stack_hash_lock);

The long hold lock scares me a little bit for a unstable system. Could you only hold it while hashing/unhashing?

The patch was initially developed on 2.6.26 kernel and I forward ported to the latest kernel this time. We've been running the 2.6.26 kernel with the patch for quite long time, and haven't noticed the problem w/ the locking.

Quoted 2 lines

Also when you can't get it fall back to something else.

Can you clarify that?

Thank you for reviewing :)

--Ying

Quoted 5 lines

-Andi
--
[email protected] -- Speaking for myself only

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

author
#2 lore raw
In reply to Rik van Riel
Quoted 11 lines · Rik van Riel
Quoted 6 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.

I like it.  I often overlook information from staring myself blind on way too many duplicate stack traces.

Quoted 7 lines
Quoted 4 lines

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.

This changelog doesn't tell the whole story of what the code does.

Thanks Rik for reviewing it. I can add more details to the changelog .

--Ying

Quoted 9 lines

It appears to store stack traces in the table, and use the hash to look them up. Somehow there's a global pointer, called cur_stack, involved too.

The code looks correct, but somehow I'm not happy with it. Having said that, I also don't have ideas on how to make it better.

Quoted 7 lines

If nobody else knows how to make this code better, maybe it should just be merged as is. I hope someone has ideas, though :)

--
All rights reversed

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

#5 lore raw
In reply to Ying Han
Quoted 4 lines · Ying Han
Quoted 2 lines

Also when you can't get it fall back to something else.

Can you clarify that?

The debugging paths usually have a lock timeout and fall back to not needing the lock (= not use a hash) when it expires. This way you guarantee output even if the system
is already quite confused.

-Andi

Signature · 8 lines
[email protected] -- Speaking for myself only

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