Skip to contentSkip to conversation

reliable stack trace support

PATCH In review Jan Beulich 4 participants 12 messages May 16, 2006 – May 18, 2006

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

PATCH 1/3 reliable stack trace support

These are the generic bits needed to enable reliable stack traces based on Dwarf2-like (.eh_frame) unwind information. Subsequent patches will enable x86-64 and i386 to make use of this.

include/asm-generic/unwind.h +119 -0
@@ -0,0 +1,119 @@
+#ifndef _ASM_GENERIC_UNWIND_H
+#define _ASM_GENERIC_UNWIND_H
+
+/*
+ * Copyright (C) 2002-2006 Novell, Inc.
+ *	Jan Beulich <[email protected]>
+ *
+ * A simple API for unwinding kernel stacks.  This is used for
+ * debugging and error reporting purposes.  The kernel doesn't need
+ * full-blown stack unwinding with all the bells and whistles, so there
+ * is not much point in implementing the full Dwarf2 unwind API.
+ */
+
+struct module;
+
+#ifdef CONFIG_STACK_UNWIND
+
+#define ARCH_UNWIND_SECTION_NAME ".eh_frame"
+
+/*
+ * Initialize unwind support.
+ */
+extern void unwind_init(void);
+
+extern void *
+unwind_add_table(struct module *,
+                 const void *table_start,
+                 unsigned long table_size);
+
+extern void
+unwind_remove_table(void *handle, int init_only);
+
+extern int
+unwind_init_frame_info(struct unwind_frame_info *,
+                       struct task_struct *,
+                       /*const*/ struct pt_regs *);
+
+/*
+ * Prepare to unwind a blocked task.
+ */
+extern int
+unwind_init_blocked(struct unwind_frame_info *, struct task_struct *);
+
+/*
+ * Prepare to unwind the currently running thread.
+ */
+extern int
+unwind_init_running(struct unwind_frame_info *,
+                    asmlinkage void (*callback)(struct unwind_frame_info *, void *arg),
+                    void *arg);
+
+/*
+ * Unwind to previous to frame.  Returns 0 if successful, negative
+ * number in case of an error.
+ */
+extern int
+unwind(struct unwind_frame_info *);
+
+/*
+ * Unwind until the return pointer is in user-land (or until an error
+ * occurs).  Returns 0 if successful, negative number in case of
+ * error.
+ */
+extern int
+unwind_to_user(struct unwind_frame_info *);
+
+#else
+
+struct unwind_frame_info {};
+
+static inline void unwind_init(void) {}
+
+static inline void *
+unwind_add_table(struct module *mod,
+                 const void *table_start,
+                 unsigned long table_size)
+{
+	return NULL;
+}
+
+static inline void
+unwind_remove_table(void *handle, int init_only)
+{}
+
+static inline int
+unwind_init_frame_info(struct unwind_frame_info *info,
+                       struct task_struct *tsk,
+                       const struct pt_regs *regs)
+{
+	return -ENOSYS;
+}
+
+static inline int
+unwind_init_blocked(struct unwind_frame_info *info, struct task_struct *tsk)
+{
+	return -ENOSYS;
+}
+
+static inline int
+unwind_init_running(struct unwind_frame_info *info,
+                    asmlinkage void (*callback)(struct unwind_frame_info *, void *arg),
+                    void *arg)
+{
+	return -ENOSYS;
+}
+
+static inline int unwind(struct unwind_frame_info *info)
+{
+	return -ENOSYS;
+}
+
+static inline int unwind_to_user(struct unwind_frame_info *info)
+{
+	return -ENOSYS;
+}
+
+#endif
+
+#endif /* _ASM_GENERIC_UNWIND_H */
include/linux/module.h +6 -0
@@ -284,6 +284,12 @@ struct module
 	/* The size of the executable code in each section.  */
 	unsigned long init_text_size, core_text_size;
 
+#if defined(CONFIG_STACK_UNWIND) /* || defined(CONFIG_IA64) || defined(CONFIG_PARISC) */
+# define MODULE_UNWIND_INFO
+	/* The handle returned from unwind_add_table. */
+	void *unwind_info;
+#endif
+
 	/* Arch-specific module values */
 	struct mod_arch_specific arch;
 
init/main.c +7 -0
@@ -54,6 +54,12 @@
 #include <asm/sections.h>
 #include <asm/cacheflush.h>
 
+#ifdef CONFIG_STACK_UNWIND
+#include <asm/unwind.h>
+#else
+#include <asm-generic/unwind.h>
+#endif
+
 #ifdef CONFIG_X86_LOCAL_APIC
 #include <asm/smp.h>
 #endif
@@ -482,6 +488,7 @@ asmlinkage void __init start_kernel(void
 		   __stop___param - __start___param,
 		   &unknown_bootoption);
 	sort_main_extable();
+	unwind_init();
 	trap_init();
 	rcu_init();
 	init_IRQ();
kernel/Makefile +1 -0
@@ -22,6 +22,7 @@ obj-$(CONFIG_UID16) += uid16.o
 obj-$(CONFIG_MODULES) += module.o
 obj-$(CONFIG_OBSOLETE_INTERMODULE) += intermodule.o
 obj-$(CONFIG_KALLSYMS) += kallsyms.o
+obj-$(CONFIG_STACK_UNWIND) += unwind.o
 obj-$(CONFIG_PM) += power/
 obj-$(CONFIG_BSD_PROCESS_ACCT) += acct.o
 obj-$(CONFIG_KEXEC) += kexec.o
kernel/module.c +23 -1
@@ -43,6 +43,9 @@
 #include <asm/uaccess.h>
 #include <asm/semaphore.h>
 #include <asm/cacheflush.h>
+#ifdef MODULE_UNWIND_INFO
+#include <asm/unwind.h>
+#endif
 
 #if 0
 #define DEBUGP printk
@@ -1051,6 +1054,10 @@ static void free_module(struct module *m
 	remove_sect_attrs(mod);
 	mod_kobject_remove(mod);
 
+#ifdef MODULE_UNWIND_INFO
+	unwind_remove_table(mod->unwind_info, 0);
+#endif
+
 	/* Arch-specific cleanup. */
 	module_arch_cleanup(mod);
 
@@ -1412,7 +1419,7 @@ static struct module *load_module(void _
 	unsigned int i, symindex = 0, strindex = 0, setupindex, exindex,
 		exportindex, modindex, obsparmindex, infoindex, gplindex,
 		crcindex, gplcrcindex, versindex, pcpuindex, gplfutureindex,
-		gplfuturecrcindex;
+		gplfuturecrcindex, unwindex = 0;
 	struct module *mod;
 	long err = 0;
 	void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
@@ -1502,6 +1509,9 @@ static struct module *load_module(void _
 	versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
 	infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
 	pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
+#ifdef ARCH_UNWIND_SECTION_NAME
+	unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
+#endif
 
 	/* Don't keep modinfo section */
 	sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
@@ -1510,6 +1520,8 @@ static struct module *load_module(void _
 	sechdrs[symindex].sh_flags |= SHF_ALLOC;
 	sechdrs[strindex].sh_flags |= SHF_ALLOC;
 #endif
+	if (unwindex)
+		sechdrs[unwindex].sh_flags |= SHF_ALLOC;
 
 	/* Check module struct version now, before we try to use module. */
 	if (!check_modstruct_version(sechdrs, versindex, mod)) {
@@ -1738,6 +1750,13 @@ static struct module *load_module(void _
 		goto arch_cleanup;
 	add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
 
+#ifdef MODULE_UNWIND_INFO
+	/* Size of section 0 is 0, so this works well if no unwind info. */
+	mod->unwind_info = unwind_add_table(mod,
+	                                    (void *)sechdrs[unwindex].sh_addr,
+	                                    sechdrs[unwindex].sh_size);
+#endif
+
 	/* Get rid of temporary copy */
 	vfree(hdr);
 
@@ -1836,6 +1855,9 @@ sys_init_module(void __user *umod,
 	mod->state = MODULE_STATE_LIVE;
 	/* Drop initial reference. */
 	module_put(mod);
+#ifdef MODULE_UNWIND_INFO
+	unwind_remove_table(mod->unwind_info, 1);
+#endif
 	module_free(mod, mod->module_init);
 	mod->module_init = NULL;
 	mod->init_size = 0;
kernel/unwind.c +876 -0

876 lines not on this page · raw message

lib/Kconfig.debug +9 -1
@@ -188,7 +188,7 @@ config FRAME_POINTER
 
 config UNWIND_INFO
 	bool "Compile the kernel with frame unwind information"
-	depends on !IA64
+	depends on !IA64 && !PARISC
 	depends on !MODULES || !(MIPS || PARISC || PPC || SUPERH || SPARC64 || V850)
 	help
 	  If you say Y here the resulting kernel image will be slightly larger
@@ -196,6 +196,14 @@ config UNWIND_INFO
 	  If you don't debug the kernel, you can say N, but we may not be able
 	  to solve problems without frame unwind information or frame pointers.
 
+config STACK_UNWIND
+	bool "Stack unwind support"
+	depends on UNWIND_INFO
+	depends on n
+	help
+	  This enables more precise stack traces, omitting all unrelated
+	  occurrences of pointers into kernel code from the dump.
+
 config FORCED_INLINING
 	bool "Force gcc to inline functions marked 'inline'"
 	depends on DEBUG_KERNEL
#1 lore raw
Quoted 3 lines · Jan Beulich

These are the generic bits needed to enable reliable stack traces based on Dwarf2-like (.eh_frame) unwind information. Subsequent patches will enable x86-64 and i386 to make use of this.

really nice!

lib/Kconfig.debug config UNWIND_INFO · line 202

Quoted 4 lines
lib/Kconfig.debug +4 -0
+config STACK_UNWIND
+	bool "Stack unwind support"
+	depends on UNWIND_INFO
+	depends on n

'depends on n' ? Also, i think this should be 'default y'. The code is very clean. Curious: how much testing has it seen?

Ingo

#2 lore raw
Quoted 7 lines

lib/Kconfig.debug config UNWIND_INFO · line 202

Quoted 5 lines
Quoted 1 line

Ingo Molnar <[email protected]> 16.05.06 16:39 >>>

lib/Kconfig.debug +4 -0
+config STACK_UNWIND
+	bool "Stack unwind support"
+	depends on UNWIND_INFO
+	depends on n

'depends on n' ? Also, i think this should be 'default y'. The code is

Subsequent patches then change it to X86_64 and then X86. This is just so the patch can be used standalone.

Quoted 1 line

very clean. Curious: how much testing has it seen?

The code, in slightly different shape, has been in use in NLKD (and its non-Linux ancestor) for several years. The only new code is the hook-up for the stack trace display functions, where I tried to make sure I test all (known to me) possibilities.

Jan

#3 lore raw
Quoted 10 lines · Jan Beulich
Quoted 7 lines

lib/Kconfig.debug config UNWIND_INFO · line 202

Quoted 5 lines
Quoted 1 line

Ingo Molnar <[email protected]> 16.05.06 16:39 >>>

lib/Kconfig.debug +4 -0
+config STACK_UNWIND
+	bool "Stack unwind support"
+	depends on UNWIND_INFO
+	depends on n

'depends on n' ? Also, i think this should be 'default y'. The code is

Subsequent patches then change it to X86_64 and then X86. This is just so the patch can be used standalone.

ok.

Ingo

#4 lore raw
In reply to Jan Beulich
Quoted 3 lines · Jan Beulich

These are the generic bits needed to enable reliable stack traces based on Dwarf2-like (.eh_frame) unwind information. Subsequent patches will enable x86-64 and i386 to make use of this.

Thanks for doing this.

First are you sure this is compiling on its own? People don't like it when their git bisect doesn't work anymore.

I unfortunately have to go through some coding style nits.

include/asm-generic/unwind.h line 26

Quoted 9 lines
include/asm-generic/unwind.h +8 -0
+
+/*
+ * Initialize unwind support.
+ */
+extern void unwind_init(void);
+
+extern void *
+unwind_add_table(struct module *,

The type should be on the same line as the function name. Same further down. Also if you want to comment the functions it would be better to use kerneldoc compliant comments in the function definitions. (Documentation/kernel-doc-nano-HOWTO.txt)

Quoted 1 line

+#if defined(CONFIG_STACK_UNWIND) /* || defined(CONFIG_IA64) || defined(CONFIG_PARISC) */

The comment should be dropped?

Quoted 1 line

+# define MODULE_UNWIND_INFO

Why do you need a second define for this? CONFIG_STACK_UNWIND should be enough, no?

init/main.c line 61

Quoted 5 lines
init/main.c +5 -0
+#ifdef CONFIG_STACK_UNWIND
+#include <asm/unwind.h>
+#else
+#include <asm-generic/unwind.h>
+#endif

Normally the other archs should get stub includes that include asm-generic/unwind.h

init/main.c asmlinkage void __init start_kernel(void · line 491

Quoted 8 lines

#ifdef CONFIG_X86_LOCAL_APIC
#include <asm/smp.h>
#endif

init/main.c +1 -0
@@ -482,6 +488,7 @@ asmlinkage void __init start_kernel(void
 		   __stop___param - __start___param,
 		   &unknown_bootoption);
 	sort_main_extable();
+	unwind_init();

Stupid q. but what happens when we get a crash before unwind_init? Is the failure benign?

kernel/module.c line 48

Quoted 9 lines
kernel/module.c +3 -0
@@ -43,6 +43,9 @@
 #include <asm/uaccess.h>
 #include <asm/semaphore.h>
 #include <asm/cacheflush.h>
+#ifdef MODULE_UNWIND_INFO
+#include <asm/unwind.h>
+#endif

It should be possible to include this without the ifdef, no?

kernel/module.c static void free_module(struct module *m · line 1059

Quoted 10 lines
kernel/module.c +3 -0
 #if 0
 #define DEBUGP printk
@@ -1051,6 +1054,10 @@ static void free_module(struct module *m
 	remove_sect_attrs(mod);
 	mod_kobject_remove(mod);
 
+#ifdef MODULE_UNWIND_INFO
+	unwind_remove_table(mod->unwind_info, 0);
+#endif

Might be better to stub it in the include

kernel/module.c static void free_module(struct module *m · line 1061

Quoted 2 lines
kernel/module.c +2 -0
+#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
+#define VALUE_BUILD_BUG_ON(e) (sizeof(char[1 - 2 * !!(e)]))

Can you move those to kernel.h. Maybe with a short comment for the later.

Quoted 7 lines
kernel/module.c +7 -0
+#define EXTRA_INFO(f) { \
+		VALUE_BUILD_BUG_ON(offsetof(struct unwind_frame_info, f) \
+		                   % FIELD_SIZEOF(struct unwind_frame_info, f)) \
+		* offsetof(struct unwind_frame_info, f) / FIELD_SIZEOF(struct unwind_frame_info, f), \
+		FIELD_SIZEOF(struct unwind_frame_info, f) \
+	}
+#define PTREGS_INFO(f) EXTRA_INFO(regs.f)

kernel/module.c static void free_module(struct module *m

Quoted 10 lines
kernel/module.c +10 -0
+
+static const struct {
+	unsigned offs:BITS_PER_LONG / 2;
+	unsigned width:BITS_PER_LONG / 2;
+} reg_info[] = {
+	UNW_REGISTER_INFO
+};
+
+#undef PTREGS_INFO
+#undef EXTRA_INFO

Where are they actually used? I can't find UNW_REGISTER_INFO in the patch.

In general it looks a bit overcomplicated. Can you just use the values directly in the unwinder code?

kernel/module.c

Quoted 2 lines
kernel/module.c +2 -0
+
+#ifndef REG_INVALID

Who would set it?

kernel/module.c

Quoted 4 lines
kernel/module.c +4 -0
+#define REG_INVALID(r) (reg_info[r].width == 0)
+#endif
+
+#define DW_CFA_nop                          0x00

I guess it would be useful to have them in some include. Maybe linux/dwarf2.h ?

Quoted 28 lines
kernel/module.c +28 -0
+DEFINE_SPINLOCK(table_lock);
+static atomic_t lookups, removals;
+
+static struct unwind_table {
+	struct {
+		unsigned long pc;
+		unsigned long range;
+	} core, init;
+	const void *address;
+	unsigned long size;
+	struct unwind_table *link;
+	atomic_t users;
+	const char *name;
+} root_table, *last_table;
+
+struct unwind_item {
+	enum item_location {
+		Nowhere,
+		Memory,
+		Register,
+		Value
+	} where;
+	uleb128_t value;
+};
+
+struct unwind_state {
+	uleb128_t loc, org;
+	const uint8_t *cieStart, *cieEnd;

kernel/module.c

Quoted 10 lines
kernel/module.c +10 -0
+	uleb128_t codeAlign;
+	sleb128_t dataAlign;
+	struct cfa {
+		uleb128_t reg, offs;
+	} cfa;
+	struct unwind_item regs[ARRAY_SIZE(reg_info)];
+	unsigned stackDepth:8;
+	unsigned version:8;
+	const uint8_t *label;
+	const uint8_t *stack[MAX_STACK_DEPTH];

In general please replace all uintN_t with uN

kernel/module.c

Quoted 3 lines
kernel/module.c +3 -0
+};
+
+static const struct cfa badCFA = {ARRAY_SIZE(reg_info), 1};

White space after {

kernel/module.c

Quoted 3 lines
kernel/module.c +3 -0
+
+static struct unwind_table *
+find_table(unsigned long pc)

Should be on one line. More further down.

kernel/module.c

Quoted 12 lines
kernel/module.c +12 -0
+{
+	int old_removals;
+	struct unwind_table *table = NULL;
+
+	do {
+		if (table)
+				atomic_dec(&table->users);
+		old_removals = atomic_read(&removals);
+		atomic_inc(&lookups);
+		for (table = &root_table; table; table = table->link)
+			if ((pc >= table->core.pc && pc < table->core.pc + table->core.range)
+			    || (pc >= table->init.pc && pc < table->init.pc + table->init.range)) {

And should fit on 80 chars.

kernel/module.c

Quoted 5 lines
kernel/module.c +5 -0
+				atomic_inc(&table->users);
+				break;
+			}
+		atomic_dec(&lookups);
+	} while (atomic_read(&removals) != old_removals);

This looks like a seq lock? Use the real thing?

kernel/module.c

Quoted 12 lines
kernel/module.c +12 -0
+	return table;
+}
+
+static void
+drop_table(struct unwind_table *table)
+{
+	if (table)
+		atomic_dec(&table->users);
+}
+
+static void
+init_unwind_table (struct unwind_table *table, const char *name,

No space before (

kernel/module.c

Quoted 4 lines
kernel/module.c +4 -0
+                   const void *core_start, unsigned long core_size,
+                   const void *init_start, unsigned long init_size,
+                   const void *table_start, unsigned long table_size)
+{

A function with that many arguments scares me. But ok.

kernel/module.c

Quoted 1 line
kernel/module.c +1 -0
+	table = kmalloc(sizeof(*table), GFP_USER);

GFP_KERNEL.

Quoted 44 lines
kernel/module.c +44 -0
+	if (!table)
+		return NULL;
+
+	init_unwind_table(table, module->name,
+	                  module->module_core, module->core_size,
+	                  module->module_init, module->init_size,
+	                  table_start, table_size);
+
+	spin_lock(&table_lock);
+	if (last_table)
+		last_table->link = table;
+	else
+		root_table.link = table;
+	last_table = table;
+	spin_unlock(&table_lock);
+
+	return table;
+}
+
+void
+unwind_remove_table(void *handle, int init_only)
+{
+	struct unwind_table *table = handle, *prev;
+
+	if (!table || table == &root_table)
+		return;
+
+	if (init_only && table == last_table) {
+		table->init.pc = 0;
+		table->init.range = 0;
+		return;
+	}
+
+	spin_lock(&table_lock);
+	for (prev = &root_table; prev->link && prev->link != table; prev = prev->link)
+		;
+	if (prev->link) {
+		if (init_only) {
+			table->init.pc = 0;
+			table->init.range = 0;
+			table = NULL;
+		} else {
+			prev->link = table->link;
+			if (!prev->link)

kernel/module.c

Quoted 10 lines
kernel/module.c +10 -0
+				last_table = prev;
+			atomic_inc(&removals);
+		}
+	} else
+		table = NULL;
+	spin_unlock(&table_lock);
+
+	if (table) {
+		while (atomic_read(&table->users) || atomic_read(&lookups))
+			msleep(1);

Can't this livelock?

I suspect it isn't needed anyways because module unload uses stop_machine() already and that should be enough to stop the lockups which don't block.

Please use /* */ comments

kernel/module.c

Quoted 9 lines
kernel/module.c +9 -0
+		if (((const char *)(cie + 2))[1] == 'z') {
+			uleb128_t augSize = get_uleb128(&ptr, end);
+
+			if ((ptr += augSize) > end)
+				fde = NULL;
+		}
+	}
+	if (cie == NULL || fde == NULL) {
+#ifdef UNW_FP

This should be CONFIG_FRAME_POINTER

kernel/module.c

Quoted 8 lines
kernel/module.c +8 -0
+		unsigned long top, bottom;
+#endif
+
+		drop_table(table);
+#ifdef UNW_FP
+		top = STACK_TOP(frame->task);
+		bottom = STACK_BOTTOM(frame->task);
+# if FRAME_RETADDR_OFFSET < 0

Nasty ifdefs. Can you perhaps isolate that < 0 case in a separate function. Also when does it happen anyways? A little bit cleanup here would be good.

kernel/module.c

Quoted 4 lines
kernel/module.c +4 -0
+			switch(reg_info[state.regs[i].value].width) {
+#define CASE(n) \
+			case sizeof(uint##n##_t): \
+				state.regs[i].value = FRAME_REG(state.regs[i].value, const uint##n##_t); \

80 characters.

Quoted 1 line

+EXPORT_SYMBOL_GPL(unwind_init_frame_info);

I would actually use EXPORT_SYMBOL(). Would be unfair to not give an unwinder to any modules.

kernel/module.c

Quoted 4 lines

config UNWIND_INFO

kernel/module.c +1 -1
 	bool "Compile the kernel with frame unwind information"
-	depends on !IA64
+	depends on !IA64 && !PARISC

Why PARISC?

Quoted 7 lines
kernel/module.c +0 -0
 	depends on !MODULES || !(MIPS || PARISC || PPC || SUPERH || SPARC64 || V850)
 	help
 	  If you say Y here the resulting kernel image will be slightly larger
@@ -196,6 +196,14 @@ config UNWIND_INFO
 	  If you don't debug the kernel, you can say N, but we may not be able
 	  to solve problems without frame unwind information or frame pointers.
 

kernel/module.c config UNWIND_INFO · line 208

Quoted 10 lines
kernel/module.c +7 -0
+config STACK_UNWIND
+	bool "Stack unwind support"
+	depends on UNWIND_INFO
+	depends on n
+	help
+	  This enables more precise stack traces, omitting all unrelated
+	  occurrences of pointers into kernel code from the dump.
 config FORCED_INLINING
 	bool "Force gcc to inline functions marked 'inline'"
 	depends on DEBUG_KERNEL

First again please make sure it's not enabled before it's complete.

Hmm, I assume you tested it, but it's unclear to me where the eh_frame is stored in the x86-64 vmlinux.lds. There is no entry? Hopefully it's not after _end.

-Andi

#5 lore raw
Quoted 7 lines

init/main.c line 61

Quoted 5 lines
init/main.c +5 -0
+#ifdef CONFIG_STACK_UNWIND
+#include <asm/unwind.h>
+#else
+#include <asm-generic/unwind.h>
+#endif

Normally the other archs should get stub includes that include asm-generic/unwind.h

This would make them appear kind of supporting unwind, even though they really don't, wouldn't it?

Quoted 4 lines

init/main.c asmlinkage void __init start_kernel(void · line 491

Quoted 1 line
init/main.c +1 -0
+	unwind_init();

Stupid q. but what happens when we get a crash before unwind_init? Is the failure benign?

You'll get old fashioned stack traces, which I consider benign.

Quoted 5 lines

kernel/module.c line 48

Quoted 3 lines
kernel/module.c +3 -0
+#ifdef MODULE_UNWIND_INFO
+#include <asm/unwind.h>
+#endif

It should be possible to include this without the ifdef, no?

Only if all arch-s get an asm/unwind.h, which I consider ill (see above).

Quoted 5 lines

kernel/module.c static void free_module(struct module *m · line 1059

Quoted 3 lines
kernel/module.c +3 -0
+#ifdef MODULE_UNWIND_INFO
+	unwind_remove_table(mod->unwind_info, 0);
+#endif

Might be better to stub it in the include

It is stubbed, but as above - the include isn't always there.

Quoted 4 lines
Quoted 4 lines
kernel/unwind.c +4 -0
+static const struct {
+	unsigned offs:BITS_PER_LONG / 2;
+	unsigned width:BITS_PER_LONG / 2;
+} reg_info[] = {
Quoted 8 lines

kernel/unwind.c line 42

Quoted 5 lines
kernel/unwind.c +5 -0
+	UNW_REGISTER_INFO
+};
+
+#undef PTREGS_INFO
+#undef EXTRA_INFO

Where are they actually used? I can't find UNW_REGISTER_INFO in the patch.

These are defined per architecture.

Quoted 2 lines

In general it looks a bit overcomplicated. Can you just use the values directly in the unwinder code?

Which values? The offsets into pt_regs are clearly architecture specific, so I don't think it'd be nice to put them into generic code.

Quoted 3 lines
Quoted 1 line

+#ifndef REG_INVALID

Who would set it?

Again, an architecture if the default definition isn't sufficient.

Quoted 4 lines
Quoted 1 line

+#define DW_CFA_nop 0x00

I guess it would be useful to have them in some include. Maybe linux/dwarf2.h ?

Do you think they might be re-used by anyone else? I generally prefer keeping stuff used only in a single place out of sight for anyone else.

Quoted 1 line

In general please replace all uintN_t with uN

Why that? What are these types for then? After all, they're standard mandated, and one more of my preferences is to use standard types where-ever possible.

Quoted 5 lines

kernel/unwind.c line 140

Quoted 3 lines
kernel/unwind.c +3 -0
+
+static struct unwind_table *
+find_table(unsigned long pc)

Should be on one line. More further down.

Make the code uglier in my opinion, especially when the parameter declarations are quite long.

Quoted 7 lines

kernel/unwind.c line 157

Quoted 5 lines
kernel/unwind.c +5 -0
+				atomic_inc(&table->users);
+				break;
+			}
+		atomic_dec(&lookups);
+	} while (atomic_read(&removals) != old_removals);

This looks like a seq lock? Use the real thing?

The code here should get away without taking *any* locks, otherwise you may end up not seeing any backtrace at all when the system dies.

Quoted 3 lines

kernel/unwind.c line 207

Quoted 1 line
kernel/unwind.c +1 -0
+	table = kmalloc(sizeof(*table), GFP_USER);

GFP_KERNEL.

Not sure about the significance - I took this from respective ia64 code.

Quoted 8 lines

kernel/unwind.c line 261

Quoted 3 lines
kernel/unwind.c +3 -0
+	if (table) {
+		while (atomic_read(&table->users) || atomic_read(&lookups))
+			msleep(1);

Can't this livelock?

I suspect it isn't needed anyways because module unload uses stop_machine() already and that should be enough to stop the lockups which don't block.

That is what I wasn't sure of - if these functions are indeed meant to be called only from the module loader (which I think they are), then table_lock isn't needed (serialized by module_mutex).

Quoted 3 lines
Quoted 1 line

+#ifdef UNW_FP

This should be CONFIG_FRAME_POINTER

No - there are arch-s (ia64, while clearly not going to be getting here, immediately comes to mind) where you cannot define what register the frame pointer is in. I rather think x86 is special in that you actually can.

kernel/unwind.c line 697

Quoted 8 lines
kernel/unwind.c +8 -0
+		unsigned long top, bottom;
+#endif
+
+		drop_table(table);
+#ifdef UNW_FP
+		top = STACK_TOP(frame->task);
+		bottom = STACK_BOTTOM(frame->task);
+# if FRAME_RETADDR_OFFSET < 0

Nasty ifdefs. Can you perhaps isolate that < 0 case in a separate function. Also when does it happen anyways? A little bit cleanup here would be good.

Quoted 4 lines
Quoted 1 line

+EXPORT_SYMBOL_GPL(unwind_init_frame_info);

I would actually use EXPORT_SYMBOL(). Would be unfair to not give an unwinder to any modules.

Fine with me - I just followed other people's demands (on other occasions) to only use GPL exports for new symbols.

Quoted 6 lines

lib/Kconfig.debug config FRAME_POINTER · line 191

Quoted 4 lines

config UNWIND_INFO

lib/Kconfig.debug +1 -1
 	bool "Compile the kernel with frame unwind information"
-	depends on !IA64
+	depends on !IA64 && !PARISC

Why PARISC?

Because it, like ia64, has its own unwinding logic.

Jan

#6 lore raw
Quoted 9 lines · Jan Beulich
Quoted 7 lines

init/main.c line 61

Quoted 5 lines
init/main.c +5 -0
+#ifdef CONFIG_STACK_UNWIND
+#include <asm/unwind.h>
+#else
+#include <asm-generic/unwind.h>
+#endif

Normally the other archs should get stub includes that include asm-generic/unwind.h

This would make them appear kind of supporting unwind, even though they really don't, wouldn't it?

Stubs or something calling the normal unwinder are fine.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

+#define DW_CFA_nop 0x00

I guess it would be useful to have them in some include. Maybe linux/dwarf2.h ?

Do you think they might be re-used by anyone else? I generally prefer keeping stuff used only in a single place out of sight for anyone else.

At some point they will probably, but ok - keep them private for now.

Quoted 4 lines
Quoted 1 line

In general please replace all uintN_t with uN

Why that? What are these types for then? After all, they're standard mandated, and one more of my preferences is to use standard types where-ever possible.

Linux is not written in ISO-C, it's Linux C. And in that uN types are used, not uintN_t

Quoted 8 lines
Quoted 6 lines

kernel/unwind.c line 140

Quoted 4 lines
kernel/unwind.c +3 -0
+
+static struct unwind_table *
+find_table(unsigned long pc)

Should be on one line. More further down.

Make the code uglier in my opinion, especially when the parameter declarations are quite long.

Well it's like this all over the kernel. You can run Lindent if you prefer, but usually it takes more work to clean up after it. It's also part of that "Linux C" thing.

Quoted 10 lines
Quoted 7 lines

kernel/unwind.c line 157

Quoted 5 lines
kernel/unwind.c +5 -0
+				atomic_inc(&table->users);
+				break;
+			}
+		atomic_dec(&lookups);
+	} while (atomic_read(&removals) != old_removals);

This looks like a seq lock? Use the real thing?

The code here should get away without taking *any* locks, otherwise you may end up not seeing any backtrace at all when the system dies.

seqlock doesn't really take a look in the classical sense. The only way to make it lock up would be to have another CPU livelocking on this in a loop.

Anyways most likely it isn't needed because of the stop_machine and can be removed

Quoted 6 lines
Quoted 4 lines

kernel/unwind.c line 207

Quoted 2 lines
kernel/unwind.c +1 -0
+	table = kmalloc(sizeof(*table), GFP_USER);

GFP_KERNEL.

Not sure about the significance - I took this from respective ia64 code.

GFP_USER is for user space pages only.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

+#ifdef UNW_FP

This should be CONFIG_FRAME_POINTER

No - there are arch-s (ia64, while clearly not going to be getting here, immediately comes to mind) where you cannot define what register the frame pointer is in. I rather think x86 is special in that you actually can.

Those never set CONFIG_FP

-Andi

#7 lore raw
In reply to Jan Beulich
Quoted 3 lines · Jan Beulich

These are the generic bits needed to enable reliable stack traces based on Dwarf2-like (.eh_frame) unwind information. Subsequent patches will enable x86-64 and i386 to make use of this.

some more detailed review:

init/main.c line 61

Quoted 5 lines
init/main.c +5 -0
+#ifdef CONFIG_STACK_UNWIND
+#include <asm/unwind.h>
+#else
+#include <asm-generic/unwind.h>
+#endif

this wants to become include/linux/unwind.h?

kernel/module.c line 48

Quoted 3 lines
kernel/module.c +3 -0
+#ifdef MODULE_UNWIND_INFO
+#include <asm/unwind.h>
+#endif

this too could then include <linux/unwind.h>

Quoted 1 line

+DEFINE_SPINLOCK(table_lock);

static?

kernel/unwind.c line 148

Quoted 10 lines
kernel/unwind.c +10 -0
+static struct unwind_table *
+find_table(unsigned long pc)
+{
+	int old_removals;
+	struct unwind_table *table = NULL;
+
+	do {
+		if (table)
+				atomic_dec(&table->users);
+		old_removals = atomic_read(&removals);

racy? wants to become rcu?

Review

Quoted 1 line
(quoted diff) +1 -0
+	spin_lock(&table_lock);

spin_lock_irq?

kernel/unwind.c line 239

Quoted 5 lines
kernel/unwind.c +5 -0
+	if (init_only && table == last_table) {
+		table->init.pc = 0;
+		table->init.range = 0;
+		return;
+	}

SMP and PREEMPT unsafe.

Review

Quoted 1 line
(quoted diff) +1 -0
+	spin_lock(&table_lock);

spin_lock_irq().

kernel/unwind.c line 263

Quoted 5 lines
kernel/unwind.c +5 -0
+	if (table) {
+		while (atomic_read(&table->users) || atomic_read(&lookups))
+			msleep(1);
+		kfree(table);
+	}

ugh!

kernel/unwind.c line 544

Quoted 3 lines
kernel/unwind.c +3 -0
+//todo			case DW_CFA_def_cfa_expression:
+//todo			case DW_CFA_expression:
+//todo			case DW_CFA_val_expression:

hm?

kernel/unwind.c line 857

Quoted 4 lines
kernel/unwind.c +4 -0
+{
+	info->task = current;
+	arch_unwind_init_running(info, callback, arg);
+	return 0;

newline before the return. (this happens in a couple of other places too)

Ingo

#8 lore raw
Quoted 9 lines
Quoted 1 line
Quoted 1 line

Ingo Molnar <[email protected]> 16.05.06 17:05 >>>

init/main.c line 61

Quoted 5 lines · Jan Beulich
init/main.c +5 -0
+#ifdef CONFIG_STACK_UNWIND
+#include <asm/unwind.h>
+#else
+#include <asm-generic/unwind.h>
+#endif

this wants to become include/linux/unwind.h?

Not really, at least not until IA64 and PARISC get adopted to the same (architecture independent) interface.

Quoted 5 lines

kernel/module.c line 48

Quoted 3 lines
kernel/module.c +3 -0
+#ifdef MODULE_UNWIND_INFO
+#include <asm/unwind.h>
+#endif

this too could then include <linux/unwind.h>

As above.

Quoted 3 lines
Quoted 1 line

+DEFINE_SPINLOCK(table_lock);

static?

Oh, yes.

Quoted 4 lines
Quoted 4 lines
kernel/unwind.c +4 -0
+static struct unwind_table *
+find_table(unsigned long pc)
+{
+	int old_removals;
Quoted 8 lines

kernel/unwind.c line 148

Quoted 6 lines
kernel/unwind.c +6 -0
+	struct unwind_table *table = NULL;
+
+	do {
+		if (table)
+				atomic_dec(&table->users);
+		old_removals = atomic_read(&removals);

racy? wants to become rcu?

I don't think so. As far as I can tell, this isn't going to be a problem, it may just result in an extra, normally unneeded, re-run of the loop.

Quoted 3 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	spin_lock(&table_lock);

spin_lock_irq?

Why?

Quoted 7 lines

kernel/unwind.c line 239

Quoted 5 lines
kernel/unwind.c +5 -0
+	if (init_only && table == last_table) {
+		table->init.pc = 0;
+		table->init.range = 0;
+		return;
+	}

SMP and PREEMPT unsafe.

I don't think so, given that this can be called only from the module loader. As Andi pointed out elsewhere, it may even be unnecessary to do the locking at all.

Quoted 3 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	spin_lock(&table_lock);

spin_lock_irq().

Again, why?

Quoted 7 lines

kernel/unwind.c line 263

Quoted 5 lines
kernel/unwind.c +5 -0
+	if (table) {
+		while (atomic_read(&table->users) || atomic_read(&lookups))
+			msleep(1);
+		kfree(table);
+	}

ugh!

???

Quoted 5 lines

kernel/unwind.c line 544

Quoted 3 lines
kernel/unwind.c +3 -0
+//todo			case DW_CFA_def_cfa_expression:
+//todo			case DW_CFA_expression:
+//todo			case DW_CFA_val_expression:

hm?

This means what it says - it needs to be done, and I have no clear understanding of how these expressions are to be treated, as I've never seen them in use anywhere.

Quoted 7 lines

kernel/unwind.c line 857

Quoted 4 lines
kernel/unwind.c +4 -0
+{
+	info->task = current;
+	arch_unwind_init_running(info, callback, arg);
+	return 0;

newline before the return. (this happens in a couple of other places too)

Surely can do that, although I don't see why this should be needed in functions this small.

Jan

#9 lore raw
In reply to Jan Beulich
Quoted 10 lines · Jan Beulich

These are the generic bits needed to enable reliable stack traces based on Dwarf2-like (.eh_frame) unwind information. Subsequent patches will enable x86-64 and i386 to make use of this.

include/asm-generic/unwind.h +1 -0
@@ -0,0 +1,119 @@
+#ifndef _ASM_GENERIC_UNWIND_H

include/asm-generic/unwind.h line 11

Quoted 10 lines
include/asm-generic/unwind.h +10 -0
+#define _ASM_GENERIC_UNWIND_H
+
+/*
+ * Copyright (C) 2002-2006 Novell, Inc.
+ *	Jan Beulich <[email protected]>
+ *
+ * A simple API for unwinding kernel stacks.  This is used for
+ * debugging and error reporting purposes.  The kernel doesn't need
+ * full-blown stack unwinding with all the bells and whistles, so there
+ * is not much point in implementing the full Dwarf2 unwind API.

Missing GPL?

kernel/unwind.c line 9

Quoted 12 lines
kernel/unwind.c +9 -0
@@ -0,0 +1,876 @@
+/*
+ * Copyright (C) 2002-2006 Novell, Inc.
+ *	Jan Beulich <[email protected]>
+ *
+ * A simple API for unwinding kernel stacks.  This is used for
+ * debugging and error reporting purposes.  The kernel doesn't need
+ * full-blown stack unwinding with all the bells and whistles, so there
+ * is not much point in implementing the full Dwarf2 unwind API.
+ */

...more than once, I'd say.
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#10 lore raw
Quoted 10 lines
Quoted 8 lines

+/*
+ * Copyright (C) 2002-2006 Novell, Inc.
+ * Jan Beulich <[email protected]>
+ *
+ * A simple API for unwinding kernel stacks. This is used for
+ * debugging and error reporting purposes. The kernel doesn't need
+ * full-blown stack unwinding with all the bells and whistles, so there
+ * is not much point in implementing the full Dwarf2 unwind API.

Missing GPL?

I took include/asm-ia64/unwind.h's header as a template - is there anything wrong with that?

Jan

#11 lore raw
Quoted 9 lines · Jan Beulich
Quoted 9 lines
Quoted 8 lines

+/*
+ * Copyright (C) 2002-2006 Novell, Inc.
+ * Jan Beulich <[email protected]>
+ *
+ * A simple API for unwinding kernel stacks. This is used for
+ * debugging and error reporting purposes. The kernel doesn't need
+ * full-blown stack unwinding with all the bells and whistles, so there
+ * is not much point in implementing the full Dwarf2 unwind API.

Quoted 3 lines
Quoted 1 line

Missing GPL?

I took include/asm-ia64/unwind.h's header as a template - is there anything wrong with that?

Yes, I'd say. That one needs fixing, too.

I guess one line before copyright, saying "Distribute under GPL", "This file is released under the GPLv2", or something like that would be enough.
Pavel
(goes on fixing drivers/block/nbd.c)

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html