jump_label: Robustify jump label patching
Subsystems (3)
v2 cover jump_label: Robustify jump label patching
v2:
- Refine the warning so that it doesn't warn about __init entries
- (Do so by explicitly disabling __init entries)
- Drop v1 patches which removed __init tracepoints
Josh Poimboeuf (2):
jump_label: Explicitly disable jump labels in __init code
jump_label: Warn on failed jump_label patch
include/linux/jump_label.h | 3 +++ init/main.c | 2 ++ kernel/jump_label.c | 31 ++++++++++++++++++++++++------- 3 files changed, 29 insertions(+), 7 deletions(-)
Signature · 1 line
2.14.3
PATCH v2 2/2 jump_label: Warn on failed jump_label patch
When the jump label code encounters an address which isn't recognized by kernel_text_address(), it just silently fails.
This can be dangerous because jump labels are used in a variety of places, and are generally expected to work. Convert the silent failure to a warning.
This won't warn about attempted writes to tracepoints in __init code after initmem has been freed, as those are already guarded by the entry->code check.
--- kernel/jump_label.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
kernel/jump_label.c +8 -5
@@ -367,12 +367,15 @@ static void __jump_label_update(struct static_key *key, {for (; (entry < stop) && (jump_entry_key (entry) == key); entry++) {/* -* entry->code set to 0 invalidates module init text sections -* kernel_text_address() verifies we are not in core kernel -* init code, see jump_label_invalidate_module_init(). +* An entry->code of 0 indicates an entry which has been +* disabled because it was in an init text area. */ -if (entry->code &&kernel_text_address (entry->code)) -arch_jump_label_transform (entry,jump_label_type (entry)); +if (entry->code) { +if (kernel_text_address (entry->code)) +arch_jump_label_transform (entry,jump_label_type (entry)); +else +WARN_ONCE (1 ,"can't patch jump_label at %pS" , (void *)entry->code); + } } }
Signature · 1 line
2.14.3
PATCH v2 1/2 jump_label: Explicitly disable jump labels in __init code
In reply to Josh PoimboeufAfter initmem has been freed, any jump label entries in __init code are prevented from being written to by the kernel_text_address() check in __jump_label_update(). However, this check is quite broad. If kernel_text_address() were to return false for any other reason, the jump label write would fail silently with no warning.
For jump label entrieds in module init code, entry->code is set to zero to indicate that the entry is disabled. Do the same thing for core kernel init code. This makes the behavior more consistent, and will also make it more straightforward to detect non-init jump label write failures in the next patch.
--- include/linux/jump_label.h | 3 +++ init/main.c | 2 ++ kernel/jump_label.c | 18 ++++++++++++++++-- 3 files changed, 21 insertions(+), 2 deletions(-)
include/linux/jump_label.h +3 -0
@@ -151,6 +151,7 @@ extern struct jump_entry __start___jump_table[];extern struct jump_entry __stop___jump_table[];extern void jump_label_init (void ); +extern void jump_label_invalidate_init (void );extern void jump_label_lock (void );extern void jump_label_unlock (void ); extern void arch_jump_label_transform(struct jump_entry *entry,
@@ -198,6 +199,8 @@ static __always_inline void jump_label_init(void) static_key_initialized = true; } +static inline void jump_label_invalidate_init (void ) {} + static __always_inline bool static_key_false(struct static_key *key) {if (unlikely (static_key_count (key) >0 ))
init/main.c +2 -0
@@ -89,6 +89,7 @@#include <linux/io.h> #include <linux/cache.h> #include <linux/rodata_test.h> +#include <linux/jump_label.h> #include <asm/io.h> #include <asm/bugs.h>
@@ -1000,6 +1001,7 @@ static int __ref kernel_init(void *unused)/* need to finish all async __init code before freeing the memory */ async_synchronize_full ();ftrace_free_init_mem (); +jump_label_invalidate_init ();free_initmem ();mark_readonly (); system_state =SYSTEM_RUNNING ;
kernel/jump_label.c +16 -2
@@ -16,6 +16,7 @@#include <linux/jump_label_ratelimit.h> #include <linux/bug.h> #include <linux/cpu.h> +#include <asm/sections.h> #ifdef HAVE_JUMP_LABEL
@@ -633,16 +634,29 @@ static void jump_label_del_module(struct module *mod) } } +/* Disable any jump label entries in __init code */ +void __init jump_label_invalidate_init (void ) +{ +struct jump_entry *iter_start = __start___jump_table; +struct jump_entry *iter_stop = __stop___jump_table; +struct jump_entry *iter; + +for (iter = iter_start; iter < iter_stop; iter++) +if (iter->code >= (unsigned long )_sinittext && + iter->code < (unsigned long )_einittext) + iter->code =0 ; +} + +/* Disable any jump label entries in module init code */ static void jump_label_invalidate_module_init (struct module *mod) {struct jump_entry *iter_start = mod->jump_entries;struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;struct jump_entry *iter; -for (iter = iter_start; iter < iter_stop; iter++) { +for (iter = iter_start; iter < iter_stop; iter++)if (within_module_init (iter->code, mod)) iter->code =0 ; - } } static int
Signature · 1 line
2.14.3
Jason Baron
maintainerRe: [PATCH v2 1/2] jump_label: Explicitly disable jump labels in __init code
Quoted 78 lines · Josh Poimboeuf
After initmem has been freed, any jump label entries in
__initcode are prevented from being written to by thekernel_text_address()check in__jump_label_update(). However, this check is quite broad. Ifkernel_text_address()were to return false for any other reason, the jump label write would fail silently with no warning.For jump label entrieds in module init code,
entry->codeis set to zero to indicate that the entry is disabled. Do the same thing for core kernel init code. This makes the behavior more consistent, and will also make it more straightforward to detect non-init jump label write failures in the next patch.Signed-off-by Josh Poimboeuf <[email protected]>--- include/linux/jump_label.h | 3 +++ init/main.c | 2 ++ kernel/jump_label.c | 18 ++++++++++++++++-- 3 files changed, 21 insertions(+), 2 deletions(-)include/linux/jump_label.h +3
-0@@ -151,6 +151,7 @@ extern struct jump_entry __start___jump_table[];extern struct jump_entry __stop___jump_table[];extern void jump_label_init (void ); +extern void jump_label_invalidate_init (void );extern void jump_label_lock (void );extern void jump_label_unlock (void ); extern void arch_jump_label_transform(struct jump_entry *entry,@@ -198,6 +199,8 @@ static __always_inline void jump_label_init(void) static_key_initialized = true; } +static inline void jump_label_invalidate_init (void ) {} + static __always_inline bool static_key_false(struct static_key *key) {if (unlikely (static_key_count (key) >0 ))init/main.c +2
-0@@ -89,6 +89,7 @@#include <linux/io.h> #include <linux/cache.h> #include <linux/rodata_test.h> +#include <linux/jump_label.h> #include <asm/io.h> #include <asm/bugs.h> @@ -1000,6 +1001,7 @@ static int __ref kernel_init(void *unused)/* need to finish all async __init code before freeing the memory */ async_synchronize_full ();ftrace_free_init_mem (); +jump_label_invalidate_init ();free_initmem ();mark_readonly (); system_state =SYSTEM_RUNNING ;kernel/jump_label.c +3
-0@@ -16,6 +16,7 @@#include <linux/jump_label_ratelimit.h> #include <linux/bug.h> #include <linux/cpu.h> +#include <asm/sections.h> #ifdef HAVE_JUMP_LABEL @@ -633,16 +634,29 @@ static void jump_label_del_module(struct module *mod) } } +/* Disable any jump label entries in __init code */ +void __init jump_label_invalidate_init (void )
kernel/jump_label.c static void jump_label_del_module(struct module *mod) · line 648
Quoted 10 lines
kernel/jump_label.c +10
-0+{ +struct jump_entry *iter_start = __start___jump_table; +struct jump_entry *iter_stop = __stop___jump_table; +struct jump_entry *iter; + +for (iter = iter_start; iter < iter_stop; iter++) +if (iter->code >= (unsigned long )_sinittext && + iter->code < (unsigned long )_einittext) + iter->code =0 ; +}
Seems like this wants to use init_kernel_text() but i see its marked 'static', perhaps it can be moved to a header?
Thanks,
-Jason
Quoted 17 lines
kernel/jump_label.c +3
-2+ +/* Disable any jump label entries in module init code */ static void jump_label_invalidate_module_init (struct module *mod) {struct jump_entry *iter_start = mod->jump_entries;struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;struct jump_entry *iter; -for (iter = iter_start; iter < iter_stop; iter++) { +for (iter = iter_start; iter < iter_stop; iter++)if (within_module_init (iter->code, mod)) iter->code =0 ; - } } static int
PATCH 1/1 extable: Make init_kernel_text() global
Quoted 7 lines · Jason Baron
Quoted 7 lines
(quoted diff) +7
-0+/* Disable any jump label entries in __init code */ +void __init jump_label_invalidate_init(void) +{ + struct jump_entry *iter_start = __start___jump_table; + struct jump_entry *iter_stop = __stop___jump_table; + struct jump_entry *iter; +
Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ for (iter = iter_start; iter < iter_stop; iter++) + if (iter->code >= (unsigned long)_sinittext && + iter->code < (unsigned long)_einittext) + iter->code = 0; +}Seems like this wants to use
init_kernel_text()but i see its marked 'static', perhaps it can be moved to a header?
How about this patch on top?
---------
From: Josh Poimboeuf <[email protected]>
Subject: [PATCH] extable: Make init_kernel_text() global
Convert init_kernel_text() to a global function and use it in a few places instead of manually comparing _sinittext and _einittext.
Note that kallsyms.h has a very similar function called is_kernel_inittext(), but its end check is inclusive. I'm not sure whether that's intentional behavior, so I didn't touch it.
--- arch/x86/kernel/unwind_orc.c | 3 +-- include/linux/kernel.h | 1 + kernel/extable.c | 2 +- kernel/jump_label.c | 4 +--- 4 files changed, 4 insertions(+), 6 deletions(-)
arch/x86/kernel/unwind_orc.c +1 -2
@@ -5,7 +5,6 @@#include <asm/unwind.h> #include <asm/orc_types.h> #include <asm/orc_lookup.h> -#include <asm/sections.h> #define orc_warn (fmt, ...) \ printk_deferred_once(KERN_WARNING pr_fmt("WARNING: " fmt), ##__VA_ARGS__)
@@ -148,7 +147,7 @@ static struct orc_entry *orc_find(unsigned long ip) }/* vmlinux .init slow lookup: */ -if (ip >= (unsigned long )_sinittext && ip < (unsigned long )_einittext) +if (init_kernel_text (ip))return __orc_find (__start_orc_unwind_ip, __start_orc_unwind, __stop_orc_unwind_ip - __start_orc_unwind_ip, ip);
include/linux/kernel.h +1 -0
@@ -472,6 +472,7 @@ extern bool parse_option_str(const char *str, const char *option);extern char *next_arg (char *args,char **param,char **val);extern int core_kernel_text (unsigned long addr); +extern int init_kernel_text (unsigned long addr);extern int core_kernel_data (unsigned long addr);extern int __kernel_text_address (unsigned long addr);extern int kernel_text_address (unsigned long addr);
kernel/extable.c +1 -1
@@ -64,7 +64,7 @@ const struct exception_table_entry *search_exception_tables(unsigned long addr)return e; } -static inline int init_kernel_text (unsigned long addr) +int init_kernel_text (unsigned long addr) {if (addr >= (unsigned long )_sinittext && addr < (unsigned long )_einittext)
kernel/jump_label.c +1 -3
@@ -16,7 +16,6 @@#include <linux/jump_label_ratelimit.h> #include <linux/bug.h> #include <linux/cpu.h> -#include <asm/sections.h> #ifdef HAVE_JUMP_LABEL
@@ -645,8 +644,7 @@ void __init jump_label_invalidate_init(void)struct jump_entry *iter;for (iter = iter_start; iter < iter_stop; iter++) -if (iter->code >= (unsigned long )_sinittext && - iter->code < (unsigned long )_einittext) +if (init_kernel_text (iter->code)) iter->code =0 ; }
Signature · 1 line
2.14.3
Re: [PATCH] extable: Make init_kernel_text() global
Quoted 17 lines · On Fri, 16 Feb 2018 11:57:44 -0600 Josh Poimboeuf <[email protected]> wrote:
Quoted 7 lines · Jason Baron
Quoted 7 lines
(quoted diff) +7
-0+/* Disable any jump label entries in __init code */ +void __init jump_label_invalidate_init(void) +{ + struct jump_entry *iter_start = __start___jump_table; + struct jump_entry *iter_stop = __stop___jump_table; + struct jump_entry *iter; +Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ for (iter = iter_start; iter < iter_stop; iter++) + if (iter->code >= (unsigned long)_sinittext && + iter->code < (unsigned long)_einittext) + iter->code = 0; +}Seems like this wants to use
init_kernel_text()but i see its marked 'static', perhaps it can be moved to a header?
Quoted 1 line
How about this patch on top?
Yes please.
Quoted 9 lines
---------
From: Josh Poimboeuf <[email protected]>
Subject: [PATCH] extable: Makeinit_kernel_text()globalConvert
init_kernel_text()to a global function and use it in a few places instead of manually comparing_sinittextand_einittext.
Quoted 7 lines
Note that
kallsyms.hhas a very similar function calledis_kernel_inittext(), but its end check is inclusive. I'm not sure whether that's intentional behavior, so I didn't touch it.Suggested-by Jason Baron <[email protected]>Signed-off-by Josh Poimboeuf <[email protected]>---
-- Steve
Re: [PATCH v2 1/2] jump_label: Explicitly disable jump labels in __init code
In reply to Josh PoimboeufQuoted 15 lines · Josh Poimboeuf
kernel/jump_label.c +14
-0+/* Disable any jump label entries in __init code */ +void __init jump_label_invalidate_init (void ) +{ +struct jump_entry *iter_start = __start___jump_table; +struct jump_entry *iter_stop = __stop___jump_table; +struct jump_entry *iter; + +for (iter = iter_start; iter < iter_stop; iter++) +if (iter->code >= (unsigned long )_sinittext && + iter->code < (unsigned long )_einittext) + iter->code =0 ; +} + +/* Disable any jump label entries in module init code */ static void jump_label_invalidate_module_init (struct module *mod)
kernel/jump_label.c static void jump_label_del_module(struct module *mod) · line 645
Quoted 10 lines
kernel/jump_label.c +1
-2{struct jump_entry *iter_start = mod->jump_entries;struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;struct jump_entry *iter; -for (iter = iter_start; iter < iter_stop; iter++) { +for (iter = iter_start; iter < iter_stop; iter++)if (within_module_init (iter->code, mod)) iter->code =0 ; - }
Why did you remove the curly braces? They are canonical kernel style for multi-line statements.
The new jump_label_invalidate_init() function has that problem too.
Thanks,
Ingo
Re: [PATCH v2 1/2] jump_label: Explicitly disable jump labels in __init code
Quoted 32 lines · Ingo Molnar
Quoted 15 lines · Josh Poimboeuf
kernel/jump_label.c +14
-0+/* Disable any jump label entries in __init code */ +void __init jump_label_invalidate_init (void ) +{ +struct jump_entry *iter_start = __start___jump_table; +struct jump_entry *iter_stop = __stop___jump_table; +struct jump_entry *iter; + +for (iter = iter_start; iter < iter_stop; iter++) +if (iter->code >= (unsigned long )_sinittext && + iter->code < (unsigned long )_einittext) + iter->code =0 ; +} + +/* Disable any jump label entries in module init code */ static void jump_label_invalidate_module_init (struct module *mod)kernel/jump_label.c static void jump_label_del_module(struct module *mod) · line 645
Quoted 10 lines
kernel/jump_label.c +1
-2{struct jump_entry *iter_start = mod->jump_entries;struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;struct jump_entry *iter; -for (iter = iter_start; iter < iter_stop; iter++) { +for (iter = iter_start; iter < iter_stop; iter++)if (within_module_init (iter->code, mod)) iter->code =0 ; - }Why did you remove the curly braces? They are canonical kernel style for multi-line statements.
Quoted 1 line
The new
jump_label_invalidate_init()function has that problem too.
I'd say the jury is still out:
Without braces:
$ find . -name "*.[ch]" | xargs awk '/for \(.*[^{]$/ { line1=$0; f=1; next } f == 1 && /if \(.*[^{]$/ { f=0; line2=$0; i=1; next } i == 1 { i=0; line3=$0; j=1; next } j == 1 && /^$/ {j=0; print line1; print line2; print line3; print; next} { f=0; i=0; j=0; }' |grep 'for (' |wc -l
1389With braces:
$ find . -name "*.[ch]" | xargs awk '/for \(.*{$/ { line1=$0; f=1; next } f == 1 && /if \(.*[^{]$/ { f=0; line2=$0; i=1; next } i == 1 { i=0; line3=$0; j=1; next } j == 1 && /}/ {j=0; print line1; print line2; print line3; print } { f=0; i=0; j=0; }' |grep 'for (' |wc -l
2292And I see no mention of it in coding-style.rst.
Personally I prefer the more compact version, but I have no problem changing it.
Signature · 1 line
Josh
Re: [PATCH v2 1/2] jump_label: Explicitly disable jump labels in __init code
Quoted 33 lines · Josh Poimboeuf
Quoted 23 lines · Ingo Molnar
Quoted 20 lines · Josh Poimboeuf
kernel/jump_label.c +14
-0+/* Disable any jump label entries in __init code */ +void __init jump_label_invalidate_init (void ) +{ +struct jump_entry *iter_start = __start___jump_table; +struct jump_entry *iter_stop = __stop___jump_table; +struct jump_entry *iter; + +for (iter = iter_start; iter < iter_stop; iter++) +if (iter->code >= (unsigned long )_sinittext && + iter->code < (unsigned long )_einittext) + iter->code =0 ; +} + +/* Disable any jump label entries in module init code */ static void jump_label_invalidate_module_init (struct module *mod) {struct jump_entry *iter_start = mod->jump_entries;struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;struct jump_entry *iter;Quoted 8 lines
kernel/jump_label.c static void jump_label_del_module(struct module *mod) · line 645
Quoted 5 lines
kernel/jump_label.c +1
-2-for (iter = iter_start; iter < iter_stop; iter++) { +for (iter = iter_start; iter < iter_stop; iter++)if (within_module_init (iter->code, mod)) iter->code =0 ; - }Why did you remove the curly braces? They are canonical kernel style for multi-line statements.
Quoted 2 lines
Personally I prefer the more compact version, but I have no problem changing it.
Yes, it's certainly a matter of taste. Here is the reason why myself and others prefer the version with braces:
https://marc.info/?l=linux-kernel&m=148467980905537&w=2
Thanks,
tglx
Re: [PATCH v2 1/2] jump_label: Explicitly disable jump labels in __init code
Quoted 34 lines · Thomas Gleixner
Quoted 33 lines · Josh Poimboeuf
Quoted 31 lines · Ingo Molnar
Quoted 15 lines · Josh Poimboeuf
kernel/jump_label.c +14
-0+/* Disable any jump label entries in __init code */ +void __init jump_label_invalidate_init (void ) +{ +struct jump_entry *iter_start = __start___jump_table; +struct jump_entry *iter_stop = __stop___jump_table; +struct jump_entry *iter; + +for (iter = iter_start; iter < iter_stop; iter++) +if (iter->code >= (unsigned long )_sinittext && + iter->code < (unsigned long )_einittext) + iter->code =0 ; +} + +/* Disable any jump label entries in module init code */ static void jump_label_invalidate_module_init (struct module *mod)kernel/jump_label.c static void jump_label_del_module(struct module *mod) · line 645
Quoted 10 lines
kernel/jump_label.c +1
-2{struct jump_entry *iter_start = mod->jump_entries;struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;struct jump_entry *iter; -for (iter = iter_start; iter < iter_stop; iter++) { +for (iter = iter_start; iter < iter_stop; iter++)if (within_module_init (iter->code, mod)) iter->code =0 ; - }Why did you remove the curly braces? They are canonical kernel style for multi-line statements.
Quoted 7 lines
Quoted 2 lines
Personally I prefer the more compact version, but I have no problem changing it.
Yes, it's certainly a matter of taste. Here is the reason why myself and others prefer the version with braces:
https://marc.info/?l=linux-kernel&m=148467980905537&w=2
Easier visual parsing is indeed one of the primary reasons, but there's two other reasons as well:
2) code robustness
For example:
for (i = 0; i < 10; i++)
if (foo)
bar(i);
baz(i);Is probably buggy code, although technically it's valid syntax and will compile just fine.
If all multi-line statements have curly braces then this type of bug cannot occur:
for (i = 0; i < 10; i++) {
if (foo)
bar(i);
baz(i);
}3) style consistency
Nothing is worse than randomly inconsistent coding style, and in arch/x86/ and core kernel code using curly braces is certainly the dominant style:
# multi-line C statements without braces:
$ find arch/x86/ kernel mm -name "*.[ch]" | xargs awk '/for \(.*[^{]$/ { line1=$0;
f=1; next } f == 1 && /if \(.*[^{]$/ { f=0; line2=$0; i=1; next } i == 1 { i=0;
line3=$0; j=1; next } j == 1 && /^$/{j=0; print line1; print line2; print line3;
print; next} { f=0; i=0; j=0; }' |grep 'for (' |wc -l55
# multi-line C statements with braces:
$ find arch/x86 kernel mm -name "*.[ch]" | xargs awk '/for \(.*{$/ { line1=$0; f=1;
next } f == 1 && /if \(.*[^{]$/ { f=0; line2=$0; i=1; next } i == 1 { i=0;
line3=$0; j=1; next } j == 1 && /}/{j=0; print line1; print line2; print line3;
print } { f=0; i=0; j=0; }' |grep 'for (' |wc -l116
Thanks,
Ingo
Re: [PATCH v2 1/2] jump_label: Explicitly disable jump labels in __init code
Quoted 12 lines · Ingo Molnar
2) code robustness
For example:
for (i = 0; i < 10; i++) if (foo) bar(i); baz(i);Is probably buggy code, although technically it's valid syntax and will compile just fine.
Quoted 7 lines
If all multi-line statements have curly braces then this type of bug cannot occur:
for (i = 0; i < 10; i++) { if (foo) bar(i); baz(i); }
Note that newer versions of GCC will warn about this pattern:
warning: this ‘for’ clause does not guard
this statement, but the latter is misleadingly indented as if it were guarded by the ‘for’
But the warning is pretty restrictive and GCC won't warn about slightly more complex patterns like:
for (i = 0; i < 10; i++)
if (foo)
bar(i);
// debug_fn();
baz(i);Thanks,
Ingo