PATCH] debug: add notifier chain debugging
Subsystems (2)
From: Arjan van de Ven <[email protected]>
Subject: [PATCH] debug: add notifier chain debugging
during some development we suspected a case where we left something in a notifier chain that was from a module that was unloaded already... and that sort of thing is rather hard to track down.
This patch adds a very simple sanity check (which isn't all that expensive) to make sure the notifier we're about to call is actually from either the kernel itself of from a still-loaded module, avoiding a hard-to-chase-down crash.
--- kernel/notifier.c | 16 ++++++++++++++++ lib/Kconfig.debug | 10 ++++++++++ 2 files changed, 26 insertions(+), 0 deletions(-)
kernel/notifier.c +16 -0
@@ -21,6 +21,10 @@ BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);static int notifier_chain_register (struct notifier_block **nl,struct notifier_block *n) { +if (!kernel_text_address ((unsigned long )n->notifier_call)) { +WARN (1 ,"Invalid notifier registered!" ); +return 0 ; + }while ((*nl) !=NULL ) {if (n->priority > (*nl)->priority)break ;
@@ -34,6 +38,10 @@ static int notifier_chain_register(struct notifier_block **nl,static int notifier_chain_cond_register (struct notifier_block **nl,struct notifier_block *n) { +if (!kernel_text_address ((unsigned long )n->notifier_call)) { +WARN (1 ,"Invalid notifier registered!" ); +return 0 ; + }while ((*nl) !=NULL ) {if ((*nl) == n)return 0 ;
@@ -82,6 +90,14 @@ static int __kprobes notifier_call_chain(struct notifier_block **nl,while (nb && nr_to_call) { next_nb =rcu_dereference (nb->next); + +#ifdef CONFIG_DEBUG_NOTIFIERS +if (!kernel_text_address ((unsigned long )nb->notifier_call)) { +WARN (1 ,"Invalid notifier called!" ); + nb = next_nb; +continue ; + } +#endif ret = nb->notifier_call (nb, val, v);if (nr_calls)
lib/Kconfig.debug +10 -0
@@ -536,6 +536,16 @@ config DEBUG_SG If unsure, say N. +config DEBUG_NOTIFIERS + bool "Debug notifier call chains" + depends on DEBUG_KERNEL + help + Enable this to turn on sanity checking for notifier call chains. + This is most useful for kernel developers to make sure that + modules properly unregister themselves from notifier chains. + This is a relatively cheap check but if you care about maximum + performance, say N. + config FRAME_POINTER bool "Compile the kernel with frame pointers" depends on DEBUG_KERNEL && \
Signature · 1 line
1.5.5.1
Quoted 11 lines · Arjan van de Ven
From: Arjan van de Ven <[email protected]>
Subject: [PATCH] debug: add notifier chain debuggingduring some development we suspected a case where we left something in a notifier chain that was from a module that was unloaded already... and that sort of thing is rather hard to track down.
This patch adds a very simple sanity check (which isn't all that expensive) to make sure the notifier we're about to call is actually from either the kernel itself of from a still-loaded module, avoiding a hard-to-chase-down crash.
applied for testing, thanks Arjan.
Ingo
Andrew Morton
maintainerQuoted 34 lines · On Fri, 15 Aug 2008 15:29:38 -0700 Arjan van de Ven <[email protected]> wrote:
From: Arjan van de Ven <[email protected]>
Subject: [PATCH] debug: add notifier chain debuggingduring some development we suspected a case where we left something in a notifier chain that was from a module that was unloaded already... and that sort of thing is rather hard to track down.
This patch adds a very simple sanity check (which isn't all that expensive) to make sure the notifier we're about to call is actually from either the kernel itself of from a still-loaded module, avoiding a hard-to-chase-down crash.
Signed-off-by Arjan van de Ven <[email protected]>--- kernel/notifier.c | 16 ++++++++++++++++ lib/Kconfig.debug | 10 ++++++++++ 2 files changed, 26 insertions(+), 0 deletions(-)kernel/notifier.c +4
-0@@ -21,6 +21,10 @@ BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);static int notifier_chain_register (struct notifier_block **nl,struct notifier_block *n) { +if (!kernel_text_address ((unsigned long )n->notifier_call)) { +WARN (1 ,"Invalid notifier registered!" ); +return 0 ; + }while ((*nl) !=NULL ) {if (n->priority > (*nl)->priority)break ;
kernel/notifier.c static int notifier_chain_register(struct notifier_block **nl, · line 44
Quoted 7 lines
kernel/notifier.c +4
-0@@ -34,6 +38,10 @@ static int notifier_chain_register(struct notifier_block **nl,static int notifier_chain_cond_register (struct notifier_block **nl,struct notifier_block *n) { +if (!kernel_text_address ((unsigned long )n->notifier_call)) { +WARN (1 ,"Invalid notifier registered!" ); +return 0 ; + }
Seems strange to add checks to the registration functions. What could be that broken?
Quoted 26 lines
kernel/notifier.c +8
-0while ((*nl) !=NULL ) {if ((*nl) == n)return 0 ;@@ -82,6 +90,14 @@ static int __kprobes notifier_call_chain(struct notifier_block **nl,while (nb && nr_to_call) { next_nb =rcu_dereference (nb->next); + +#ifdef CONFIG_DEBUG_NOTIFIERS +if (!kernel_text_address ((unsigned long )nb->notifier_call)) { +WARN (1 ,"Invalid notifier called!" ); + nb = next_nb; +continue ; + } +#endif ret = nb->notifier_call (nb, val, v);if (nr_calls)lib/Kconfig.debug +0
-0@@ -536,6 +536,16 @@ config DEBUG_SG If unsure, say N.
lib/Kconfig.debug config DEBUG_SG · line 548
Quoted 10 lines
lib/Kconfig.debug +10
-0+config DEBUG_NOTIFIERS + bool "Debug notifier call chains" + depends on DEBUG_KERNEL + help + Enable this to turn on sanity checking for notifier call chains. + This is most useful for kernel developers to make sure that + modules properly unregister themselves from notifier chains. + This is a relatively cheap check but if you care about maximum + performance, say N. +
If we remove the first two checks then surely we can afford to add the remaining check unconditionally and lose the new config option and its ifdef.
Quoted 6 lines · Andrew Morton
Quoted 6 lines
lib/Kconfig.debug +6
-0+config DEBUG_NOTIFIERS + bool "Debug notifier call chains" + depends on DEBUG_KERNEL + help + Enable this to turn on sanity checking for notifier call chains. + This is most useful for kernel developers to make sure that
Quoted 8 lines
lib/Kconfig.debug config DEBUG_SG · line 548
Quoted 4 lines
lib/Kconfig.debug +4
-0+ modules properly unregister themselves from notifier chains. + This is a relatively cheap check but if you care about maximum + performance, say N. +If we remove the first two checks then surely we can afford to add the remaining check unconditionally and lose the new config option and its ifdef.
yeah, that's sensible. Arjan?
Ingo
Quoted 5 lines · On Tue, 19 Aug 2008 21:09:32 -0700 Andrew Morton <[email protected]>…
If we remove the first two checks then surely we can afford to add the remaining check unconditionally and lose the new config option and its ifdef.
Andi told me that this notifier is a hotpath for kprobes (systemtap) use models; at which point making this a config option makes sort of sense to get the last bit of performance out of that. Personally I would always want this option on for anything I run, and I suspect (enterprise) distros will just always turn it on as well since it's a good sanity check at low cost.
I'd be happy to lose the config (I think we have way too many unneeded ones of those already); I'll make a patch.
Signature · 3 lines
If you want to reach me at my work email, use [email protected] For development, discussion and tips for power savings, visit http://www.lesswatts.org
Quoted 16 lines · Arjan van de Ven
Quoted 5 lines · On Tue, 19 Aug 2008 21:09:32 -0700 Andrew Morton <[email protected]>…
If we remove the first two checks then surely we can afford to add the remaining check unconditionally and lose the new config option and its ifdef.
Andi told me that this notifier is a hotpath for kprobes (systemtap) use models; at which point making this a config option makes sort of sense to get the last bit of performance out of that. Personally I would always want this option on for anything I run, and I suspect (enterprise) distros will just always turn it on as well since it's a good sanity check at low cost.
Quoted 2 lines
I'd be happy to lose the config (I think we have way too many unneeded ones of those already); I'll make a patch.
What about checking yourself? At least the braindead use of notiiers in the pagefault path for kprobes (even unconditional on x86_64) is gone now and replaced with proper kprobes hooks. The die notifier that's also used by kprobes shouldn't be in a performance critical path.
kernel/notifier.c BLOCKING_NOTIFIER_HEAD(reboot_notifier_list); · line 27
Quoted 4 lines · Arjan van de Ven
kernel/notifier.c +4
-0+if (!kernel_text_address ((unsigned long )n->notifier_call)) { +WARN (1 ,"Invalid notifier registered!" ); +return 0 ; + }
This breaks on ia64 (and pa-risc I think) where function pointers don't point directly at the code, they point to a {code,data} structure which is itself located in data space, not text space.
-Tony
Quoted 10 lines · On Mon, 25 Aug 2008 15:39:13 -0700 "Tony Luck" <[email protected]> wrote:
kernel/notifier.c BLOCKING_NOTIFIER_HEAD(reboot_notifier_list); · line 27
Quoted 4 lines · Arjan van de Ven
kernel/notifier.c +4
-0+if (!kernel_text_address ((unsigned long )n->notifier_call)) { +WARN (1 ,"Invalid notifier registered!" ); +return 0 ; + }This breaks on ia64 (and pa-risc I think) where function pointers don't point directly at the code, they point to a {code,data} structure which is itself located in data space, not text space.
is there a way to go to the actual address? I'm sure this is a bit more common.... (like kallsyms!)
Signature · 3 lines
If you want to reach me at my work email, use [email protected] For development, discussion and tips for power savings, visit http://www.lesswatts.org
Quoted 6 lines
Quoted 3 lines
This breaks on ia64 (and pa-risc I think) where function pointers don't point directly at the code, they point to a {code,data} structure which is itself located in data space, not text space.
is there a way to go to the actual address? I'm sure this is a bit more common.... (like kallsyms!)
See dereference_function_descriptor() in lib/vsprintf.c (where it will be clear that my memory was wrong and that PPC64 is the other architecture that needs this).
Perhaps this needs to be moved to some place in asm-generic so that it can be used by code like your sanity check?
-Tony
Quoted 7 lines · On Mon, 25 Aug 2008 22:08:00 -0700 "Tony Luck" <[email protected]> wrote:
Quoted 6 lines
Quoted 3 lines
This breaks on ia64 (and pa-risc I think) where function pointers don't point directly at the code, they point to a {code,data} structure which is itself located in data space, not text space.
is there a way to go to the actual address? I'm sure this is a bit more common.... (like kallsyms!)
Quoted 6 lines
See
dereference_function_descriptor()inlib/vsprintf.c(where it will be clear that my memory was wrong and that PPC64 is the other architecture that needs this).Perhaps this needs to be moved to some place in asm-generic so that it can be used by code like your sanity check?
or we make a func_is_kernel_text() thast maps underneath... after all that'll be a common use of this function ;)
Quoted 2 lines
-Tony
Signature · 3 lines
If you want to reach me at my work email, use [email protected] For development, discussion and tips for power savings, visit http://www.lesswatts.org
Arjan van de Ven
Acked-by Tony LuckQuoted 11 lines · On Mon, 25 Aug 2008 22:08:00 -0700 "Tony Luck" <[email protected]> wrote:
Quoted 6 lines
Quoted 3 lines
This breaks on ia64 (and pa-risc I think) where function pointers don't point directly at the code, they point to a {code,data} structure which is itself located in data space, not text space.
is there a way to go to the actual address? I'm sure this is a bit more common.... (like kallsyms!)
See
dereference_function_descriptor()inlib/vsprintf.c(where it will be clear that my memory was wrong and that PPC64 is the other architecture that needs this).
The patch below fixes this;
Ingo, please replace the patch with this one.
From: Arjan van de Ven <[email protected]>
Subject: [PATCH] debug: add notifier chain debugging
during some development we suspected a case where we left something in a notifier chain that was from a module that was unloaded already... and that sort of thing is rather hard to track down.
This patch adds a very simple sanity check (which isn't all that expensive) to make sure the notifier we're about to call is actually from either the kernel itself of from a still-loaded module, avoiding a hard-to-chase-down crash.
--- include/linux/kernel.h | 3 +++ kernel/extable.c | 16 ++++++++++++++++ kernel/notifier.c | 6 ++++++ lib/vsprintf.c | 2 +- 4 files changed, 26 insertions(+), 1 deletions(-)
include/linux/kernel.h +3 -0
@@ -187,6 +187,9 @@ extern unsigned long long memparse(char *ptr, char **retptr);extern int core_kernel_text (unsigned long addr);extern int __kernel_text_address (unsigned long addr);extern int kernel_text_address (unsigned long addr); +extern int func_ptr_is_kernel_text (void *ptr); +extern void *dereference_function_descriptor (void *ptr); +struct pid ;extern struct pid *session_of_pgrp (struct pid *pgrp);
kernel/extable.c +16 -0
@@ -66,3 +66,19 @@ int kernel_text_address(unsigned long addr)return 1 ;return module_text_address (addr) !=NULL ; } + +/* +* On some architectures (PPC64, IA64) function pointers +* are actually only tokens to some data that then holds the +* real function address. As a result, to find if a function +* pointer is part of the kernel text, we need to do some +* special dereferencing first. +*/ +int func_ptr_is_kernel_text (void *ptr) +{ +unsigned long addr; + addr = (unsigned long )dereference_function_descriptor (ptr); +if (core_kernel_text (addr)) +return 1 ; +return module_text_address (addr) !=NULL ; +}
kernel/notifier.c +6 -0
@@ -82,6 +82,12 @@ static int __kprobes notifier_call_chain(struct notifier_block **nl,while (nb && nr_to_call) { next_nb =rcu_dereference (nb->next); +if (!func_ptr_is_kernel_text (nb->notifier_call)) { +WARN (1 ,"Invalid notifier called!" ); + nb = next_nb; +continue ; + } + ret = nb->notifier_call (nb, val, v);if (nr_calls)
lib/vsprintf.c +1 -1
@@ -513,7 +513,7 @@ static char *string(char *buf, char *end, char *s, int field_width, int precisioreturn buf; } -static inline void *dereference_function_descriptor (void *ptr) +void *dereference_function_descriptor (void *ptr) {#if defined(CONFIG_IA64 ) || defined(CONFIG_PPC64 )void *p;
Signature · 1 line
1.5.5.1
Quoted 2 lines · Arjan van de Ven
The patch below fixes this;
Ingo, please replace the patch with this one.
well, this one is now less than cheap on x86:
Review
Quoted 8 lines
(quoted diff) +8
-0+int func_ptr_is_kernel_text(void *ptr) +{ + unsigned long addr; + addr = (unsigned long) dereference_function_descriptor(ptr); + if (core_kernel_text(addr)) + return 1; + return module_text_address(addr) != NULL; +}
as it's rather large. So i kept the config option, and it defaults to off.
Review
Quoted 5 lines
(quoted diff) +5
-0+ if (!func_ptr_is_kernel_text(nb->notifier_call)) { + WARN(1, "Invalid notifier called!"); + nb = next_nb; + continue; + }
and that should be an unlikely() too i guess.
i've applied v2 as a delta patch to -tip, with these changes - see the commit below.
Ingo
-------------------->
Quoted 1 line
From
e0f789bde8bda8ee6cf084197b6f3fc951ad241aMon Sep 17 00:00:00 2001
From: Arjan van de Ven <[email protected]>
Date: Fri, 15 Aug 2008 15:29:38 -0700
Subject: [PATCH] debug: add notifier chain debugging, v2
- unbreak ia64 (and powerpc) where function pointers dont point at code but at data (reported by Tony Luck)
[ [email protected]: various cleanups ]
--- include/linux/kernel.h | 3 +++ kernel/extable.c | 16 ++++++++++++++++ kernel/notifier.c | 10 +--------- lib/vsprintf.c | 2 +- 4 files changed, 21 insertions(+), 10 deletions(-)
include/linux/kernel.h +3 -0
@@ -187,6 +187,9 @@ extern unsigned long long memparse(char *ptr, char **retptr);extern int core_kernel_text (unsigned long addr);extern int __kernel_text_address (unsigned long addr);extern int kernel_text_address (unsigned long addr); +extern int func_ptr_is_kernel_text (void *ptr); +extern void *dereference_function_descriptor (void *ptr); +struct pid ;extern struct pid *session_of_pgrp (struct pid *pgrp);
kernel/extable.c +16 -0
@@ -66,3 +66,19 @@ int kernel_text_address(unsigned long addr)return 1 ;return module_text_address (addr) !=NULL ; } + +/* +* On some architectures (PPC64, IA64) function pointers +* are actually only tokens to some data that then holds the +* real function address. As a result, to find if a function +* pointer is part of the kernel text, we need to do some +* special dereferencing first. +*/ +int func_ptr_is_kernel_text (void *ptr) +{ +unsigned long addr; + addr = (unsigned long )dereference_function_descriptor (ptr); +if (core_kernel_text (addr)) +return 1 ; +return module_text_address (addr) !=NULL ; +}
kernel/notifier.c +1 -9
@@ -21,10 +21,6 @@ BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);static int notifier_chain_register (struct notifier_block **nl,struct notifier_block *n) { -if (!kernel_text_address ((unsigned long )n->notifier_call)) { -WARN (1 ,"Invalid notifier registered!" ); -return 0 ; - }while ((*nl) !=NULL ) {if (n->priority > (*nl)->priority)break ;
@@ -38,10 +34,6 @@ static int notifier_chain_register(struct notifier_block **nl,static int notifier_chain_cond_register (struct notifier_block **nl,struct notifier_block *n) { -if (!kernel_text_address ((unsigned long )n->notifier_call)) { -WARN (1 ,"Invalid notifier registered!" ); -return 0 ; - }while ((*nl) !=NULL ) {if ((*nl) == n)return 0 ;
@@ -92,7 +84,7 @@ static int __kprobes notifier_call_chain(struct notifier_block **nl, next_nb =rcu_dereference (nb->next);#ifdef CONFIG_DEBUG_NOTIFIERS -if (!kernel_text_address ((unsigned long )nb->notifier_call)) { +if (unlikely (!func_ptr_is_kernel_text (nb->notifier_call))) {WARN (1 ,"Invalid notifier called!" ); nb = next_nb;continue ;
lib/vsprintf.c +1 -1
@@ -513,7 +513,7 @@ static char *string(char *buf, char *end, char *s, int field_width, int precisioreturn buf; } -static inline void *dereference_function_descriptor (void *ptr) +void *dereference_function_descriptor (void *ptr) {#if defined(CONFIG_IA64 ) || defined(CONFIG_PPC64 )void *p;
Quoted 7 lines · On Wed, 27 Aug 2008 08:39:15 +0200 Ingo Molnar <[email protected]> wrote:
Quoted 2 lines · Arjan van de Ven
The patch below fixes this;
Ingo, please replace the patch with this one.well, this one is now less than cheap on x86:
really it isn't; dereference_function_descriptor is a fall through and basically free.
Quoted 3 lines
as it's rather large. So i kept the config option, and it defaults to off.
huh? the consensus was that the config needed to go ... nothing changed on that front. I agree absolutely with not introducing config options for things that don't need it; we have too many already anyway.
Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +5
-0+ if (!func_ptr_is_kernel_text(nb->notifier_call)) { + WARN(1, "Invalid notifier called!"); + nb = next_nb; + continue; + }and that should be an
unlikely()too i guess.
please do not add new unlikely()'s to the kernel.. not on non-hotpaths at least