jump label: Add MIPS architecture support.
Subsystems (11)
v2 cover jump label: Add MIPS architecture support.
v2: Make arch_jump_label_text_poke_early() optional. As pointed out by Rabin Vincent, the MIPS NOP is already optimal and does not need to be replaced at boot time. It is possible that SPARC should leave arch_jump_label_text_poke_early() unimplementd, but I leave that work for others as I cannot test it.
v1: Add MIPS jump label support.
David Daney (2):
jump label: Make arch_jump_label_text_poke_early() optional jump label: Add MIPS support.
arch/mips/Kconfig | 1 + arch/mips/include/asm/jump_label.h | 48 +++++++++++++++++++++++++++++++++ arch/mips/kernel/Makefile | 3 +- arch/mips/kernel/jump_label.c | 50 +++++++++++++++++++++++++++++++++++ arch/mips/kernel/module.c | 5 +++ arch/sparc/include/asm/jump_label.h | 1 + arch/x86/include/asm/jump_label.h | 1 + include/linux/jump_label.h | 6 ++++ 8 files changed, 114 insertions(+), 1 deletions(-) create mode 100644 arch/mips/include/asm/jump_label.h create mode 100644 arch/mips/kernel/jump_label.c
Signature · 1 line
1.7.2.2
PATCH v2 1/2 jump label: Make arch_jump_label_text_poke_early() optional
For the forthcoming MIPS jump label support, arch_jump_label_text_poke_early() is unneeded as the MIPS NOP instruction is already optimal.
Supply a default implementation that does nothing. Flag x86 and SPARC as having arch_jump_label_text_poke_early().
--- arch/sparc/include/asm/jump_label.h | 1 + arch/x86/include/asm/jump_label.h | 1 + include/linux/jump_label.h | 6 ++++++ 3 files changed, 8 insertions(+), 0 deletions(-)
arch/sparc/include/asm/jump_label.h +1 -0
@@ -6,6 +6,7 @@#include <linux/types.h> #include <asm/system.h> +#define HAVE_ARCH_JUMP_LABEL_TEXT_POKE_EARLY #define JUMP_LABEL_NOP_SIZE 4#define JUMP_LABEL (key, label) \
arch/x86/include/asm/jump_label.h +1 -0
@@ -6,6 +6,7 @@#include <linux/types.h> #include <asm/nops.h> +#define HAVE_ARCH_JUMP_LABEL_TEXT_POKE_EARLY #define JUMP_LABEL_NOP_SIZE 5# define JUMP_LABEL_INITIAL_NOP ".byte 0xe9 \n\t .long 0\n\t"
include/linux/jump_label.h +6 -0
@@ -20,7 +20,13 @@ extern struct jump_entry __stop___jump_table[];extern void arch_jump_label_transform (struct jump_entry *entry,enum jump_label_type type); + +#ifdef HAVE_ARCH_JUMP_LABEL_TEXT_POKE_EARLY extern void arch_jump_label_text_poke_early (jump_label_t addr); +#else +static inline void arch_jump_label_text_poke_early (jump_label_t addr) {} +#endif +extern void jump_label_update (unsigned long key,enum jump_label_type type);extern void jump_label_apply_nops (struct module *mod);extern int jump_label_text_reserved (void *start,void *end);
Signature · 1 line
1.7.2.2
PATCH v2 2/2 jump label: Add MIPS support.
In reply to David DaneyWhen in Rome...
In order not to be left behind, we add jump label support for MIPS.
Tested on 64-bit big endian (Octeon), and 32-bit little endian (malta/qemu).
--- arch/mips/Kconfig | 1 + arch/mips/include/asm/jump_label.h | 48 ++++++++++++++++++++++++++++++++++ arch/mips/kernel/Makefile | 3 +- arch/mips/kernel/jump_label.c | 50 ++++++++++++++++++++++++++++++++++++ arch/mips/kernel/module.c | 5 +++ 5 files changed, 106 insertions(+), 1 deletions(-) create mode 100644 arch/mips/include/asm/jump_label.h create mode 100644 arch/mips/kernel/jump_label.c
arch/mips/Kconfig +1 -0
@@ -13,6 +13,7 @@ config MIPS select HAVE_KPROBES select HAVE_KRETPROBES select RTC_LIB if !MACH_LOONGSON + select HAVE_ARCH_JUMP_LABEL mainmenu "Linux/MIPS Kernel Configuration"
arch/mips/include/asm/jump_label.h new file +48 -0
48 lines not on this page · raw message
arch/mips/kernel/Makefile +2 -1
@@ -6,7 +6,8 @@ extra-y := head.o init_task.o vmlinux.lds obj-y += cpu-probe.o branch.o entry.o genex.o irq.o process.o \ ptrace.o reset.o setup.o signal.o syscall.o \ - time.o topology.o traps.o unaligned.o watch.o vdso.o + time.o topology.o traps.o unaligned.o watch.o vdso.o \ + jump_label.o ifdef CONFIG_FUNCTION_TRACER CFLAGS_REMOVE_ftrace.o = -pg
arch/mips/kernel/jump_label.c new file +50 -0
50 lines not on this page · raw message
arch/mips/kernel/module.c +5 -0
@@ -30,6 +30,8 @@#include <linux/kernel.h> #include <linux/module.h> #include <linux/spinlock.h> +#include <linux/jump_label.h> +#include <asm/pgtable.h> /* MODULE_START */ struct mips_hi16 {
@@ -390,6 +392,9 @@ int module_finalize(const Elf_Ehdr *hdr,const Elf_Shdr *s;char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; +/* Make jump label nops. */ +jump_label_apply_nops (me); +INIT_LIST_HEAD (&me->arch.dbe_list);for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {if (strcmp ("__dbe_table" , secstrings + s->sh_name) !=0 )
Signature · 1 line
1.7.2.2
Re: [PATCH v2 2/2] jump label: Add MIPS support.
Ralf
Re: [PATCH v2 2/2] jump label: Add MIPS support.
Quoted 4 lines · Ralf Baechle
Acked-by Ralf Baechle<[email protected]>Ralf
That should complete the Acked-by prerequisites for this set. I hope Steven and Jason can get them merged for 2.6.37 (hint, hint :-))
Thanks,
David Daney
Steven Rostedt
maintainer[PATCH 0/9] [GIT PULL] jump label: various updates
Ingo,
Please pull the latest tip/perf/jump-label tree, which can be found at:
git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-2.6-trace.git
tip/perf/jump-label
David Daney (2):
jump label: Make arch_jump_label_text_poke_early() optional jump label: Add MIPS support.
Jason Baron (6):
jump label/x86: Move arch_init_ideal_nop5 later
jump label: Fix module __init section race
jump label: Fix deadlock b/w jump_label_mutex vs. text_mutex jump label: Add register_jump_label_key/unregister_jump_label_key jump label: Move jump table to r/w section
jump label: Add docs
Steven Rostedt (1):
tracing/x86: No need to disable interrupts when calling arch_init_ideal_nop5
----
Documentation/jump-label.txt | 142 ++++++++++++++++++++++ arch/mips/Kconfig | 1 + arch/mips/include/asm/jump_label.h | 48 ++++++++ arch/mips/kernel/Makefile | 3 +- arch/mips/kernel/jump_label.c | 50 ++++++++ arch/mips/kernel/module.c | 5 + arch/sparc/include/asm/jump_label.h | 1 + arch/x86/include/asm/jump_label.h | 1 + arch/x86/kernel/alternative.c | 5 + arch/x86/kernel/setup.c | 6 - include/asm-generic/vmlinux.lds.h | 14 +-- include/linux/jump_label.h | 15 +++ kernel/jump_label.c | 222 ++++++++++++++++++++++++++++++----- kernel/kprobes.c | 6 + kernel/trace/ftrace.c | 4 +- kernel/tracepoint.c | 34 ++++++ lib/dynamic_debug.c | 20 +++ 17 files changed, 526 insertions(+), 51 deletions(-)
Steven Rostedt
maintainerPATCH 1/9 jump label/x86: Move arch_init_ideal_nop5 later
From: Jason Baron <[email protected]>
arch_init_ideal_nop5() was being called from setup_arch() before the exception table was setup. Move it later into alternative_instructions().
Fixes a boot hang on OLPC's XO-1 laptop based on Geode LX processor.
LKML-Reference: <[email protected]>
--- arch/x86/kernel/alternative.c | 5 +++++ arch/x86/kernel/setup.c | 6 ------ 2 files changed, 5 insertions(+), 6 deletions(-)
arch/x86/kernel/alternative.c +5 -0
@@ -454,6 +454,7 @@ extern struct paravirt_patch_site __start_parainstructions[],void __init alternative_instructions (void ) { +unsigned long flags;/* The patching is not fully atomic, so try to avoid local interruptions that might execute the to be patched code. Other CPUs are not running. */
@@ -508,6 +509,10 @@ void __init alternative_instructions(void) (unsigned long)__smp_locks_end);restart_nmi (); + +local_irq_save (flags); +arch_init_ideal_nop5 (); +local_irq_restore (flags); }/**
arch/x86/kernel/setup.c +0 -6
@@ -112,7 +112,6 @@#include <asm/numa_64.h> #endif #include <asm/mce.h> -#include <asm/alternative.h> /* * end_pfn only includes RAM, while max_pfn_mapped includes all e820 entries.
@@ -727,7 +726,6 @@ void __init setup_arch(char **cmdline_p) {int acpi =0 ;int k8 =0 ; -unsigned long flags;#ifdef CONFIG_X86_32 memcpy (&boot_cpu_data, &new_cpu_data,sizeof (new_cpu_data));
@@ -1073,10 +1071,6 @@ void __init setup_arch(char **cmdline_p) x86_init.oem.banner ();mcheck_init (); - -local_irq_save (flags); -arch_init_ideal_nop5 (); -local_irq_restore (flags); }#ifdef CONFIG_X86_32
Signature · 1 line
1.7.1
Steven Rostedt
maintainerPATCH 2/9 tracing/x86: No need to disable interrupts when calling arch_init_ideal_nop5
In reply to Steven RostedtFrom: Steven Rostedt <[email protected]>
The function arch_init_ideal_nop5() is taken from the original ftrace_dyn_arch_init() that originally had interrupts disabled when calling it. This was due to the old way that mcounts were converted to nops. The disabling of interrupts is no longer needed for the work that is done.
--- kernel/trace/ftrace.c | 4 +--- 1 files changed, 1 insertions(+), 3 deletions(-)
kernel/trace/ftrace.c +1 -3
@@ -2795,15 +2795,13 @@ extern unsigned long __stop_mcount_loc[];void __init ftrace_init (void ) { -unsigned long count, addr, flags; +unsigned long count, addr;int ret;/* Keep the ftrace pointer to the stub */ addr = (unsigned long )ftrace_stub; -local_irq_save (flags);ftrace_dyn_arch_init (&addr); -local_irq_restore (flags);/* ftrace_dyn_arch_init places the return code in addr */ if (addr)
Signature · 1 line
1.7.1
Steven Rostedt
maintainerPATCH 3/9 jump label: Fix module __init section race
In reply to Steven RostedtFrom: Jason Baron <[email protected]>
Jump label uses is_module_text_address() to ensure that the module __init sections are valid before updating them. However, between the check for a valid module __init section and the subsequent jump label update, the module's __init section could be freed out from under us.
We fix this potential race by adding a notifier callback to the MODULE_STATE_LIVE state. This notifier is called *after* the __init section has been run but before it is going to be freed. In the callback, the jump label code zeros the key value for any __init jump code within the module, and we add a check for a non-zero key value when we update jump labels. In this way we require no additional data structures.
Thanks to Mathieu Desnoyers for pointing out this race condition.
LKML-Reference: <c6f037b7598777668025ceedd9294212fd95fa34.1285965957.git.jbaron@redhat.com>
[ Renamed remove_module_init() to remove_jump_label_module_init()
as suggested by Masami Hiramatsu. ]
--- kernel/jump_label.c | 41 ++++++++++++++++++++++++++++++++++++++++- 1 files changed, 40 insertions(+), 1 deletions(-)
kernel/jump_label.c +40 -1
@@ -168,7 +168,8 @@ void jump_label_update(unsigned long key, enum jump_label_type type) count = e_module->nr_entries; iter = e_module->table;while (count--) { -if (kernel_text_address (iter->code)) +if (iter->key && +kernel_text_address (iter->code))arch_jump_label_transform (iter, type); iter++; }
@@ -366,6 +367,39 @@ static void remove_jump_label_module(struct module *mod) } } +static void remove_jump_label_module_init (struct module *mod) +{ +struct hlist_head *head; +struct hlist_node *node, *node_next, *module_node, *module_node_next; +struct jump_label_entry *e; +struct jump_label_module_entry *e_module; +struct jump_entry *iter; +int i, count; + +/* if the module doesn't have jump label entries, just return */ +if (!mod->num_jump_entries) +return ; + +for (i =0 ; i <JUMP_LABEL_TABLE_SIZE ; i++) { + head = &jump_label_table[i]; +hlist_for_each_entry_safe (e, node, node_next, head, hlist) { +hlist_for_each_entry_safe (e_module, module_node, + module_node_next, + &(e->modules), hlist) { +if (e_module->mod != mod) +continue ; + count = e_module->nr_entries; + iter = e_module->table; +while (count--) { +if (within_module_init (iter->code, mod)) + iter->key =0 ; + iter++; + } + } + } + } +} +static int jump_label_module_notify (struct notifier_block *self,unsigned long val,void *data)
@@ -386,6 +420,11 @@ jump_label_module_notify(struct notifier_block *self, unsigned long val,remove_jump_label_module (mod);mutex_unlock (&jump_label_mutex);break ; +case MODULE_STATE_LIVE : +mutex_lock (&jump_label_mutex); +remove_jump_label_module_init (mod); +mutex_unlock (&jump_label_mutex); +break ; }return ret; }
Signature · 1 line
1.7.1
Steven Rostedt
maintainerRe: [PATCH 3/9] jump label: Fix module __init section race
kernel/jump_label.c void jump_label_update(unsigned long key, enum jump_label_type type) · line 172
Quoted 9 lines · Steven Rostedt
kernel/jump_label.c +2
-1@@ -168,7 +168,8 @@ void jump_label_update(unsigned long key, enum jump_label_type type) count = e_module->nr_entries; iter = e_module->table;while (count--) { -if (kernel_text_address (iter->code)) +if (iter->key && +kernel_text_address (iter->code))
Peter, I know you hated this, but the alternative is either:
if (iter->key && kernel_text_address(iter->code))
or break 80 chars. All three seem bad (although I don't mind breaking 80 chars for this). But I'll leave it for now.
-- Steve
Quoted 6 lines
kernel/jump_label.c +0
-0arch_jump_label_transform (iter, type); iter++; }@@ -366,6 +367,39 @@ static void remove_jump_label_module(struct module *mod) } }
Re: [PATCH 3/9] jump label: Fix module __init section race
Quoted 14 lines · Steven Rostedt
kernel/jump_label.c void jump_label_update(unsigned long key, enum jump_label_type type) · line 172
Quoted 9 lines · Steven Rostedt
kernel/jump_label.c +2
-1@@ -168,7 +168,8 @@ void jump_label_update(unsigned long key, enum jump_label_type type) count = e_module->nr_entries; iter = e_module->table;while (count--) { -if (kernel_text_address (iter->code)) +if (iter->key && +kernel_text_address (iter->code))Peter, I know you hated this, but the alternative is either:
Quoted 5 lines
if (iter->key && kernel_text_address(iter->code))or break 80 chars. All three seem bad (although I don't mind breaking 80 chars for this). But I'll leave it for now.
No, the proper alternative is to move the body of the iteration into a jump_label_update_entry() inline function ...
Nobody is forcing you to start yet another iteration 4 indentations deep. We have functions for a reason.
Thanks,
Ingo
Steven Rostedt
maintainerRe: [PATCH 3/9] jump label: Fix module __init section race
Quoted 19 lines · Ingo Molnar
Quoted 17 lines · Steven Rostedt
kernel/jump_label.c void jump_label_update(unsigned long key, enum jump_label_type type) · line 172
Quoted 9 lines · Steven Rostedt
kernel/jump_label.c +2
-1@@ -168,7 +168,8 @@ void jump_label_update(unsigned long key, enum jump_label_type type) count = e_module->nr_entries; iter = e_module->table;while (count--) { -if (kernel_text_address (iter->code)) +if (iter->key && +kernel_text_address (iter->code))Peter, I know you hated this, but the alternative is either:
if (iter->key && kernel_text_address(iter->code))
Quoted 8 lines
Quoted 2 lines
or break 80 chars. All three seem bad (although I don't mind breaking 80 chars for this). But I'll leave it for now.
No, the proper alternative is to move the body of the iteration into a
jump_label_update_entry()inline function ...Nobody is forcing you to start yet another iteration 4 indentations deep. We have functions for a reason.
Jason,
Could you send me another version of this patch with the second iteration wrapped in a function.
Thanks,
-- Steve
Re: [PATCH 3/9] jump label: Fix module __init section race
Quoted 29 lines · Steven Rostedt
Quoted 19 lines · Ingo Molnar
Quoted 17 lines · Steven Rostedt
kernel/jump_label.c void jump_label_update(unsigned long key, enum jump_label_type type) · line 172
Quoted 9 lines · Steven Rostedt
kernel/jump_label.c +2
-1@@ -168,7 +168,8 @@ void jump_label_update(unsigned long key, enum jump_label_type type) count = e_module->nr_entries; iter = e_module->table;while (count--) { -if (kernel_text_address (iter->code)) +if (iter->key && +kernel_text_address (iter->code))Peter, I know you hated this, but the alternative is either:
if (iter->key && kernel_text_address(iter->code))Quoted 8 lines
Quoted 2 lines
or break 80 chars. All three seem bad (although I don't mind breaking 80 chars for this). But I'll leave it for now.
No, the proper alternative is to move the body of the iteration into a
jump_label_update_entry()inline function ...Nobody is forcing you to start yet another iteration 4 indentations deep. We have functions for a reason.
Quoted 10 lines
Jason,
Could you send me another version of this patch with the second iteration wrapped in a function.
Thanks,
-- Steve
sure. I going to re-spin this whole series - there are a bunch of changes. Hopefully, I can drop the whole register/unregister notion as Peter suggested.
thanks,
-Jason
Steven Rostedt
maintainerPATCH 4/9 jump label: Fix deadlock b/w jump_label_mutex vs. text_mutex
In reply to Steven RostedtFrom: Jason Baron <[email protected]>
register_kprobe() downs the 'text_mutex' and then calls jump_label_text_reserved(), which downs the 'jump_label_mutex'. However, the jump label code takes those mutexes in the reverse order.
Fix by requiring the caller of jump_label_text_reserved() to do the jump label locking via the newly added: jump_label_lock(), jump_label_unlock(). Currently, kprobes is the only user of jump_label_text_reserved().
LKML-Reference: <759032c48d5e30c27f0bba003d09bffa8e9f28bb.1285965957.git.jbaron@redhat.com>
--- include/linux/jump_label.h | 5 +++++ kernel/jump_label.c | 33 +++++++++++++++++++++------------ kernel/kprobes.c | 6 ++++++ 3 files changed, 32 insertions(+), 12 deletions(-)
include/linux/jump_label.h +5 -0
@@ -18,6 +18,8 @@ struct module;extern struct jump_entry __start___jump_table[];extern struct jump_entry __stop___jump_table[]; +extern void jump_label_lock (void ); +extern void jump_label_unlock (void );extern void arch_jump_label_transform (struct jump_entry *entry,enum jump_label_type type);extern void arch_jump_label_text_poke_early (jump_label_t addr);
@@ -59,6 +61,9 @@ static inline int jump_label_text_reserved(void *start, void *end)return 0 ; } +static inline void jump_label_lock (void ) {} +static inline void jump_label_unlock (void ) {} +#endif #endif
kernel/jump_label.c +21 -12
@@ -39,6 +39,16 @@ struct jump_label_module_entry {struct module *mod; }; +void jump_label_lock (void ) +{ +mutex_lock (&jump_label_mutex); +} + +void jump_label_unlock (void ) +{ +mutex_unlock (&jump_label_mutex); +} +static int jump_label_cmp (const void *a,const void *b) {const struct jump_entry *jea = a;
@@ -152,7 +162,7 @@ void jump_label_update(unsigned long key, enum jump_label_type type)struct jump_label_module_entry *e_module;int count; -mutex_lock (&jump_label_mutex); +jump_label_lock (); entry =get_jump_label_entry ((jump_label_t )key);if (entry) { count = entry->nr_entries;
@@ -175,7 +185,7 @@ void jump_label_update(unsigned long key, enum jump_label_type type) } } } -mutex_unlock (&jump_label_mutex); +jump_label_unlock (); }static int addr_conflict (struct jump_entry *entry,void *start,void *end)
@@ -232,6 +242,7 @@ out:* overlaps with any of the jump label patch addresses. Code * that wants to modify kernel text should first verify that * it does not overlap with any of the jump label addresses. +* Caller must hold jump_label_mutex. * * returns 1 if there is an overlap, 0 otherwise */
@@ -242,7 +253,6 @@ int jump_label_text_reserved(void *start, void *end)struct jump_entry *iter_stop = __start___jump_table;int conflict =0 ; -mutex_lock (&jump_label_mutex); iter = iter_start;while (iter < iter_stop) {if (addr_conflict (iter, start, end)) {
@@ -257,7 +267,6 @@ int jump_label_text_reserved(void *start, void *end) conflict =module_conflict (start, end);#endif out: -mutex_unlock (&jump_label_mutex);return conflict; }
@@ -268,7 +277,7 @@ static __init int init_jump_label(void)struct jump_entry *iter_stop = __stop___jump_table;struct jump_entry *iter; -mutex_lock (&jump_label_mutex); +jump_label_lock (); ret =build_jump_label_hashtable (__start___jump_table, __stop___jump_table); iter = iter_start;
@@ -276,7 +285,7 @@ static __init int init_jump_label(void)arch_jump_label_text_poke_early (iter->code); iter++; } -mutex_unlock (&jump_label_mutex); +jump_label_unlock ();return ret; }early_initcall (init_jump_label);
@@ -409,21 +418,21 @@ jump_label_module_notify(struct notifier_block *self, unsigned long val,switch (val) {case MODULE_STATE_COMING : -mutex_lock (&jump_label_mutex); +jump_label_lock (); ret =add_jump_label_module (mod);if (ret)remove_jump_label_module (mod); -mutex_unlock (&jump_label_mutex); +jump_label_unlock ();break ;case MODULE_STATE_GOING : -mutex_lock (&jump_label_mutex); +jump_label_lock ();remove_jump_label_module (mod); -mutex_unlock (&jump_label_mutex); +jump_label_unlock ();break ;case MODULE_STATE_LIVE : -mutex_lock (&jump_label_mutex); +jump_label_lock ();remove_jump_label_module_init (mod); -mutex_unlock (&jump_label_mutex); +jump_label_unlock ();break ; }return ret;
kernel/kprobes.c +6 -0
@@ -1145,13 +1145,16 @@ int __kprobes register_kprobe(struct kprobe *p)return ret;preempt_disable (); +jump_label_lock ();if (!kernel_text_address ((unsigned long ) p->addr) ||in_kprobes_functions ((unsigned long ) p->addr) ||ftrace_text_reserved (p->addr, p->addr) ||jump_label_text_reserved (p->addr, p->addr)) {preempt_enable (); +jump_label_unlock ();return -EINVAL ; } +jump_label_unlock ();/* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */ p->flags &=KPROBE_FLAG_DISABLED ;
@@ -1186,6 +1189,8 @@ int __kprobes register_kprobe(struct kprobe *p)INIT_LIST_HEAD (&p->list);mutex_lock (&kprobe_mutex); +jump_label_lock ();/* needed to call jump_label_text_reserved() */ +get_online_cpus ();/* For avoiding text_mutex deadlock. */ mutex_lock (&text_mutex);
@@ -1213,6 +1218,7 @@ int __kprobes register_kprobe(struct kprobe *p) out:mutex_unlock (&text_mutex);put_online_cpus (); +jump_label_unlock ();mutex_unlock (&kprobe_mutex);if (probed_mod)
Signature · 1 line
1.7.1
Peter Zijlstra
maintainerRe: [PATCH 4/9] jump label: Fix deadlock b/w jump_label_mutex vs. text_mutex
kernel/jump_label.c int __kprobes register_kprobe(struct kprobe *p) · line 1148
Quoted 12 lines · Steven Rostedt
kernel/jump_label.c +7
-0+void jump_label_lock (void ) +{ +mutex_lock (&jump_label_mutex); +} + +++ b/kernel/kprobes.c@@ -1145,13 +1145,16 @@ int __kprobes register_kprobe(struct kprobe *p)return ret;preempt_disable (); +jump_label_lock ();
How exactly does that work?
Quoted 9 lines
kernel/kprobes.c +2
-0if (!kernel_text_address ((unsigned long ) p->addr) ||in_kprobes_functions ((unsigned long ) p->addr) ||ftrace_text_reserved (p->addr, p->addr) ||jump_label_text_reserved (p->addr, p->addr)) {preempt_enable (); +jump_label_unlock ();return -EINVAL ; } +jump_label_unlock ();
Steven Rostedt
maintainerRe: [PATCH 4/9] jump label: Fix deadlock b/w jump_label_mutex vs. text_mutex
Quoted 7 lines · Peter Zijlstra
Quoted 5 lines · Steven Rostedt
kernel/jump_label.c +5
-0+void jump_label_lock (void ) +{ +mutex_lock (&jump_label_mutex); +} +
Quoted 8 lines
kernel/kprobes.c int __kprobes register_kprobe(struct kprobe *p) · line 1148
Quoted 6 lines
+++
b/kernel/kprobes.ckernel/kprobes.c +1
-0@@ -1145,13 +1145,16 @@ int __kprobes register_kprobe(struct kprobe *p)return ret;preempt_disable (); +jump_label_lock ();How exactly does that work?
Ouch! Good catch!
I'll do the James Bottomley and blame jet lag ;-)
-- Steve
Quoted 10 lines
Quoted 10 lines
kernel/kprobes.c +2
-0if (!kernel_text_address ((unsigned long ) p->addr) ||in_kprobes_functions ((unsigned long ) p->addr) ||ftrace_text_reserved (p->addr, p->addr) ||jump_label_text_reserved (p->addr, p->addr)) {preempt_enable (); +jump_label_unlock ();return -EINVAL ; } +jump_label_unlock ();
Steven Rostedt
maintainerRe: [PATCH 4/9] jump label: Fix deadlock b/w jump_label_mutex vs. text_mutex
In reply to Peter ZijlstraQuoted 7 lines · Peter Zijlstra
Quoted 5 lines · Steven Rostedt
(quoted diff) +5
-0+void jump_label_lock(void) +{ + mutex_lock(&jump_label_mutex); +} +
Quoted 9 lines
Review int __kprobes register_kprobe(struct kprobe *p) · line 1148
Quoted 6 lines
+++
b/kernel/kprobes.c(quoted diff) +1
-0@@ -1145,13 +1145,16 @@ int __kprobes register_kprobe(struct kprobe *p) return ret; preempt_disable(); + jump_label_lock();How exactly does that work?
This patch should fix it (compiled tested only). If all agree, I'll add it to the series.
-- Steve
kernel/kprobes.c +13 -13
@@ -1144,17 +1144,13 @@ int __kprobes register_kprobe(struct kprobe *p)if (ret)return ret; -preempt_disable ();jump_label_lock (); +preempt_disable ();if (!kernel_text_address ((unsigned long ) p->addr) ||in_kprobes_functions ((unsigned long ) p->addr) ||ftrace_text_reserved (p->addr, p->addr) || -jump_label_text_reserved (p->addr, p->addr)) { -preempt_enable (); -jump_label_unlock (); -return -EINVAL ; - } -jump_label_unlock (); +jump_label_text_reserved (p->addr, p->addr)) + goto fail_with_jump_label;/* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */ p->flags &=KPROBE_FLAG_DISABLED ;
@@ -1168,10 +1164,9 @@ int __kprobes register_kprobe(struct kprobe *p)* We must hold a refcount of the probed module while updating * its code to prohibit unexpected unloading. */ -if (unlikely (!try_module_get (probed_mod))) { -preempt_enable (); -return -EINVAL ; - } +if (unlikely (!try_module_get (probed_mod))) + goto fail_with_jump_label; +/* * If the module freed .init.text, we couldn't insert * kprobes in there.
@@ -1179,11 +1174,11 @@ int __kprobes register_kprobe(struct kprobe *p)if (within_module_init ((unsigned long )p->addr, probed_mod) && probed_mod->state !=MODULE_STATE_COMING ) {module_put (probed_mod); -preempt_enable (); -return -EINVAL ; + goto fail_with_jump_label; } }preempt_enable (); +jump_label_unlock (); p->nmissed =0 ;INIT_LIST_HEAD (&p->list);
@@ -1225,6 +1220,11 @@ out:module_put (probed_mod);return ret; + +fail_with_jump_label: +preempt_enable (); +jump_label_unlock (); +return -EINVAL ; }EXPORT_SYMBOL_GPL (register_kprobe);
Steven Rostedt
maintainerRe: [PATCH 4/9] jump label: Fix deadlock b/w jump_label_mutex vs. text_mutex
Quoted 12 lines · Steven Rostedt
Quoted 11 lines · Peter Zijlstra
Quoted 10 lines · Steven Rostedt
(quoted diff) +6
-0+void jump_label_lock(void) +{ + mutex_lock(&jump_label_mutex); +} + +++ b/kernel/kprobes.c@@ -1145,13 +1145,16 @@ int __kprobes register_kprobe(struct kprobe *p) return ret;
Quoted 8 lines
Quoted 5 lines
Review int __kprobes register_kprobe(struct kprobe *p) · line 1148
Quoted 2 lines
(quoted diff) +1
-0preempt_disable(); + jump_label_lock();How exactly does that work?
This patch should fix it (compiled tested only). If all agree, I'll add it to the series.
Well, I just did more than compile test it. I booted first without this patch, and added a kprobe and lockdep spewed its guts about this condition. Then I applied the patch, compiled and rebooted, tried the probe again, and lockdep stayed quiet :-)
-- Steve
Quoted 66 lines
kernel/kprobes.c +13
-13@@ -1144,17 +1144,13 @@ int __kprobes register_kprobe(struct kprobe *p)if (ret)return ret; -preempt_disable ();jump_label_lock (); +preempt_disable ();if (!kernel_text_address ((unsigned long ) p->addr) ||in_kprobes_functions ((unsigned long ) p->addr) ||ftrace_text_reserved (p->addr, p->addr) || -jump_label_text_reserved (p->addr, p->addr)) { -preempt_enable (); -jump_label_unlock (); -return -EINVAL ; - } -jump_label_unlock (); +jump_label_text_reserved (p->addr, p->addr)) + goto fail_with_jump_label;/* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */ p->flags &=KPROBE_FLAG_DISABLED ;@@ -1168,10 +1164,9 @@ int __kprobes register_kprobe(struct kprobe *p)* We must hold a refcount of the probed module while updating * its code to prohibit unexpected unloading. */ -if (unlikely (!try_module_get (probed_mod))) { -preempt_enable (); -return -EINVAL ; - } +if (unlikely (!try_module_get (probed_mod))) + goto fail_with_jump_label; +/* * If the module freed .init.text, we couldn't insert * kprobes in there. @@ -1179,11 +1174,11 @@ int __kprobes register_kprobe(struct kprobe *p)if (within_module_init ((unsigned long )p->addr, probed_mod) && probed_mod->state !=MODULE_STATE_COMING ) {module_put (probed_mod); -preempt_enable (); -return -EINVAL ; + goto fail_with_jump_label; } }preempt_enable (); +jump_label_unlock (); p->nmissed =0 ;INIT_LIST_HEAD (&p->list);@@ -1225,6 +1220,11 @@ out:module_put (probed_mod);return ret; + +fail_with_jump_label: +preempt_enable (); +jump_label_unlock (); +return -EINVAL ; }EXPORT_SYMBOL_GPL (register_kprobe);
Steven Rostedt
maintainerPATCH 5/9 jump label: Add register_jump_label_key/unregister_jump_label_key
In reply to Steven RostedtFrom: Jason Baron <[email protected]>
There are two cases where jump labels didn't match the state transitions of tracepoints.
1)
A tracepoint is enabled in the core kernel for say kmalloc and then a module is loaded which has a kmalloc call. We currently would miss enabling the jump label for the kmalloc in the module. This is addressed by associating an 'enabled' field with each jump label. Now, when a new module is loaded we check if any of the jump labels in that module need to be enabled. If so, we enable them.
2)
If a tracepoint is defined in the core kernel code, but the usage of the tracepoint is confined to a module, the current jump label code does not create a entry in its table until the module is loaded. Thus, if the tracepoint is enabled before the module is loaded, we would miss the enablement of the jump label.
I'm not sure if there any tracepoints which currently fall into this category (the bkl could fall into this category at some point if its only used in modules). However, I do think its an important case to address to make sure that jump label behave in a consistent way with how consumers of the tracepoints might expect.
This case is implemented by introducing:
void register_jump_label_key(unsigned long key); void unregister_jump_label_key(unsigned long key);
So basically any jump label that we want to use in the system must first be registered, then it can be enabled/disabled, and then finally it can be unregistered. For core kernel jump labels, I would only expect them to be registered and never unregistered. However, a jump label may be unregistred when modules are removed.
Although, this introduces some more work for consumers wanting to use jump labels, the tracepoint and dynamic debug consumer code seems fairly contained, at least to me.
LKML-Reference: <12fafc1958abf192532a25aedbe6b50a2587c7a3.1285965957.git.jbaron@redhat.com>
--- include/linux/jump_label.h | 4 + kernel/jump_label.c | 158 +++++++++++++++++++++++++++++++++++++------- kernel/tracepoint.c | 34 ++++++++++ lib/dynamic_debug.c | 20 ++++++ 4 files changed, 193 insertions(+), 23 deletions(-)
include/linux/jump_label.h +4 -0
@@ -26,6 +26,8 @@ extern void arch_jump_label_text_poke_early(jump_label_t addr);extern void jump_label_update (unsigned long key,enum jump_label_type type);extern void jump_label_apply_nops (struct module *mod);extern int jump_label_text_reserved (void *start,void *end); +void register_jump_label_key (unsigned long key); +void unregister_jump_label_key (unsigned long key);#define enable_jump_label (key) \ jump_label_update((unsigned long)key, JUMP_LABEL_ENABLE);
@@ -63,6 +65,8 @@ static inline int jump_label_text_reserved(void *start, void *end)static inline void jump_label_lock (void ) {}static inline void jump_label_unlock (void ) {} +static inline void register_jump_label_key (unsigned long key) {} +static inline void unregister_jump_label_key (unsigned long key) {}#endif
kernel/jump_label.c +135 -23
233 lines not on this page · raw message
kernel/tracepoint.c +34 -0
@@ -575,6 +575,33 @@ void tracepoint_iter_reset(struct tracepoint_iter *iter) }EXPORT_SYMBOL_GPL (tracepoint_iter_reset); + +static void jump_label_reg_unreg (struct tracepoint *begin, +struct tracepoint *end,int reg) +{ +struct tracepoint *iter; +if (!begin) +return ; + +mutex_lock (&tracepoints_mutex); +for (iter = begin; iter < end; iter++) { +if (reg) +register_jump_label_key ((unsigned long )&iter->state); +else +unregister_jump_label_key ((unsigned long )&iter->state); + } +mutex_unlock (&tracepoints_mutex); +} +#define jump_label_register (begin, end) jump_label_reg_unreg(begin, end, 1); +#define jump_label_unregister (begin, end) jump_label_reg_unreg(begin, end, 0); + +static int __init init_jump_label_register (void ) +{ +jump_label_register (__start___tracepoints, __stop___tracepoints); +return 0 ; +} +core_initcall (init_jump_label_register); +#ifdef CONFIG_MODULES int tracepoint_module_notify(struct notifier_block *self,
@@ -584,9 +611,16 @@ int tracepoint_module_notify(struct notifier_block *self,switch (val) {case MODULE_STATE_COMING : +jump_label_register (mod->tracepoints, + mod->tracepoints + mod->num_tracepoints); +tracepoint_update_probe_range (mod->tracepoints, + mod->tracepoints + mod->num_tracepoints); +break ;case MODULE_STATE_GOING :tracepoint_update_probe_range (mod->tracepoints, mod->tracepoints + mod->num_tracepoints); +jump_label_unregister (mod->tracepoints, + mod->tracepoints + mod->num_tracepoints);break ; }return 0 ;
lib/dynamic_debug.c +20 -0
@@ -79,6 +79,24 @@ static char *ddebug_describe_flags(struct _ddebug *dp, char *buf,return buf; } +static void jump_label_reg_unreg (struct ddebug_table *dt,int reg) +{ +int i; +if (!dt) +return ; + +for (i =0 ; i < dt->num_ddebugs ; i++) { +struct _ddebug *dp = &dt->ddebugs[i]; +if (reg) +register_jump_label_key ((unsigned long )&dp->enabled); +else +unregister_jump_label_key ((unsigned long )&dp->enabled); + } +} + +#define jump_label_register (dt) jump_label_reg_unreg(dt, 1); +#define jump_label_unregister (dt) jump_label_reg_unreg(dt, 0); +/* * Search the tables for _ddebug's which match the given * `query' and apply the `flags' and `mask' to them. Tells
@@ -636,6 +654,7 @@ int ddebug_add_module(struct _ddebug *tab, unsigned int n,mutex_lock (&ddebug_lock);list_add_tail (&dt->link, &ddebug_tables); +jump_label_register (dt);mutex_unlock (&ddebug_lock);if (verbose)
@@ -647,6 +666,7 @@ EXPORT_SYMBOL_GPL(ddebug_add_module);static void ddebug_table_free (struct ddebug_table *dt) { +jump_label_unregister (dt);list_del_init (&dt->link);kfree (dt->mod_name);kfree (dt);
Signature · 1 line
1.7.1
Peter Zijlstra
maintainerRe: [PATCH 5/9] jump label: Add register_jump_label_key/unregister_jump_label_key
include/linux/jump_label.h static inline int jump_label_text_reserved(void *start, void *end) · line 69
Quoted 6 lines · Steven Rostedt
static inline void
jump_label_lock(void){}
static inline voidjump_label_unlock(void){}include/linux/jump_label.h +2
-0+static inline void register_jump_label_key (unsigned long key) {} +static inline void unregister_jump_label_key (unsigned long key) {}
shouldn't we call that jump_label_register_key() and co?
Steven Rostedt
maintainerRe: [PATCH 5/9] jump label: Add register_jump_label_key/unregister_jump_label_key
Quoted 8 lines · Peter Zijlstra
include/linux/jump_label.h static inline int jump_label_text_reserved(void *start, void *end) · line 69
Quoted 6 lines · Steven Rostedt
static inline void
jump_label_lock(void){}
static inline voidjump_label_unlock(void){}include/linux/jump_label.h +2
-0+static inline void register_jump_label_key (unsigned long key) {} +static inline void unregister_jump_label_key (unsigned long key) {}shouldn't we call that
jump_label_register_key()and co?
I believe a lot of code has register/unregister first in its name.
-- Steve
Peter Zijlstra
maintainerRe: [PATCH 5/9] jump label: Add register_jump_label_key/unregister_jump_label_key
In reply to Steven RostedtQuoted 26 lines · Steven Rostedt
2)
If a tracepoint is defined in the core kernel code, but the usage of the tracepoint is confined to a module, the current jump label code does not create a entry in its table until the module is loaded. Thus, if the tracepoint is enabled before the module is loaded, we would miss the enablement of the jump label.
I'm not sure if there any tracepoints which currently fall into this category (the bkl could fall into this category at some point if its only used in modules). However, I do think its an important case to address to make sure that jump label behave in a consistent way with how consumers of the tracepoints might expect.
This case is implemented by introducing:
void
register_jump_label_key(unsigned long key); voidunregister_jump_label_key(unsigned long key);So basically any jump label that we want to use in the system must first be registered, then it can be enabled/disabled, and then finally it can be unregistered. For core kernel jump labels, I would only expect them to be registered and never unregistered. However, a jump label may be unregistred when modules are removed.
Quoted 3 lines
Although, this introduces some more work for consumers wanting to use jump labels, the tracepoint and dynamic debug consumer code seems fairly contained, at least to me.
Urgh, this sucks.. :-(
So now we have to actually track all JUMP_LABEL() sites and call register muck on them.. even though we already track them through the special data section.
Is there really no way around this?
Steven Rostedt
maintainerRe: [PATCH 5/9] jump label: Add register_jump_label_key/unregister_jump_label_key
Quoted 7 lines · Peter Zijlstra
Urgh, this sucks.. :-(
So now we have to actually track all
JUMP_LABEL()sites and call register muck on them.. even though we already track them through the special data section.Is there really no way around this?
I'll take a look to see if we can monkey with magic and automate it.
-- Steve "pulling out his macro voodoo handlers" Rostedt
Peter Zijlstra
maintainerRe: [PATCH 5/9] jump label: Add register_jump_label_key/unregister_jump_label_key
Quoted 11 lines · Steven Rostedt
Quoted 7 lines · Peter Zijlstra
Urgh, this sucks.. :-(
So now we have to actually track all
JUMP_LABEL()sites and call register muck on them.. even though we already track them through the special data section.Is there really no way around this?
I'll take a look to see if we can monkey with magic and automate it.
So the problem is something like:
core kernel:
jump_label_enable()
module:
JUMP_LABEL()
And then because we don't have a proper __jump_table section, the jump_label_enable() won't properly work?
Why not let jump_label_enable() add a dummy entry with the enabled bit and once you load the module merge the real entry into it.
Peter Zijlstra
maintainerRe: [PATCH 5/9] jump label: Add register_jump_label_key/unregister_jump_label_key
Quoted 21 lines · Peter Zijlstra
Quoted 11 lines · Steven Rostedt
Quoted 7 lines · Peter Zijlstra
Urgh, this sucks.. :-(
So now we have to actually track all
JUMP_LABEL()sites and call register muck on them.. even though we already track them through the special data section.Is there really no way around this?
I'll take a look to see if we can monkey with magic and automate it.
So the problem is something like:
core kernel:
jump_label_enable()module:
Quoted 7 lines
JUMP_LABEL()And then because we don't have a proper
__jump_tablesection, thejump_label_enable()won't properly work?Why not let
jump_label_enable()add a dummy entry with the enabled bit and once you load the module merge the real entry into it.
Or actually use the value of the key pointer.. it would mean either standardizing the size (int/atomic_t would work), or using a version of the fallback JUMP_LABEL implementation to sort out the type issues.
Re: [PATCH 5/9] jump label: Add register_jump_label_key/unregister_jump_label_key
Quoted 27 lines · Peter Zijlstra
Quoted 26 lines · Peter Zijlstra
Quoted 11 lines · Steven Rostedt
Quoted 7 lines · Peter Zijlstra
Urgh, this sucks.. :-(
So now we have to actually track all
JUMP_LABEL()sites and call register muck on them.. even though we already track them through the special data section.Is there really no way around this?
I'll take a look to see if we can monkey with magic and automate it.
So the problem is something like:
core kernel:
jump_label_enable()module:
JUMP_LABEL()And then because we don't have a proper
__jump_tablesection, thejump_label_enable()won't properly work?
Quoted 6 lines
Quoted 2 lines
Why not let
jump_label_enable()add a dummy entry with the enabled bit and once you load the module merge the real entry into it.Or actually use the value of the key pointer.. it would mean either standardizing the size (int/atomic_t would work), or using a version of the fallback
JUMP_LABELimplementation to sort out the type issues.
So I initially implmented this as 'jump_label_enable()' would add a new entry for the key, if it didn't already exist. However, I was concerned about the case where module 'a' defined the key variable, and then module 'b' did the enable/disable, and then module 'a' was removed and thus the key value could be re-used, and module's 'b' key would mean something different.
However, I'm not sure that is possible - since module 'b' would have symbol dependency on module 'a', and thus module 'a' could not be unloaded before module 'b'.
Thus, when a module is freed, I think we can scan all the keys and check if any key is contained within the text section of the module that is about to be freed. If so, we simply remove that key entry. does this make sense?
thanks,
-Jason
Peter Zijlstra
maintainerRe: [PATCH 5/9] jump label: Add register_jump_label_key/unregister_jump_label_key
Quoted 43 lines · Jason Baron
Quoted 27 lines · Peter Zijlstra
Quoted 26 lines · Peter Zijlstra
Quoted 11 lines · Steven Rostedt
Quoted 7 lines · Peter Zijlstra
Urgh, this sucks.. :-(
So now we have to actually track all
JUMP_LABEL()sites and call register muck on them.. even though we already track them through the special data section.Is there really no way around this?
I'll take a look to see if we can monkey with magic and automate it.
So the problem is something like:
core kernel:
jump_label_enable()module:
JUMP_LABEL()And then because we don't have a proper
__jump_tablesection, thejump_label_enable()won't properly work?Quoted 6 lines
Quoted 2 lines
Why not let
jump_label_enable()add a dummy entry with the enabled bit and once you load the module merge the real entry into it.Or actually use the value of the key pointer.. it would mean either standardizing the size (int/atomic_t would work), or using a version of the fallback
JUMP_LABELimplementation to sort out the type issues.So I initially implmented this as '
jump_label_enable()' would add a new entry for the key, if it didn't already exist. However, I was concerned about the case where module 'a' defined the key variable, and then module 'b' did the enable/disable, and then module 'a' was removed and thus the key value could be re-used, and module's 'b' key would mean something different.
Quoted 3 lines
However, I'm not sure that is possible - since module 'b' would have symbol dependency on module 'a', and thus module 'a' could not be unloaded before module 'b'.
Right, if the key variable is part of A and B uses it, then A should not be allowed to be unloaded.
Quoted 4 lines
Thus, when a module is freed, I think we can scan all the keys and check if any key is contained within the text section of the module that is about to be freed. If so, we simply remove that key entry. does this make sense?
Yep.
Steven Rostedt
maintainerPATCH 6/9 jump label: Move jump table to r/w section
In reply to Steven RostedtFrom: Jason Baron <[email protected]>
Since we are writing the jump table it should be be in R/W kernel section. Move it to DATA_DATA
LKML-Reference: <e37e4c270ba893644ef3c2f0365e2448b8b3981b.1285965957.git.jbaron@redhat.com>
--- include/asm-generic/vmlinux.lds.h | 14 ++++---------- 1 files changed, 4 insertions(+), 10 deletions(-)
include/asm-generic/vmlinux.lds.h +4 -10
@@ -160,6 +160,10 @@ VMLINUX_SYMBOL(__start___tracepoints) = .; \ *(__tracepoints ) \ VMLINUX_SYMBOL(__stop___tracepoints) = .; \ + . = ALIGN(8); \ + VMLINUX_SYMBOL(__start___jump_table) = .; \ + *(__jump_table ) \ + VMLINUX_SYMBOL(__stop___jump_table) = .; \/* implement dynamic printk debug */ \ . = ALIGN(8); \ VMLINUX_SYMBOL(__start___verbose) = .; \
@@ -220,8 +224,6 @@ \ BUG_TABLE \ \ - JUMP_TABLE \ - \ /* PCI quirks */ \ .pci_fixup : AT(ADDR(.pci_fixup) - LOAD_OFFSET) { \ VMLINUX_SYMBOL(__start_pci_fixups_early) = .; \
@@ -565,14 +567,6 @@#define BUG_TABLE #endif -#define JUMP_TABLE \ - . = ALIGN(8); \ - __jump_table : AT(ADDR(__jump_table) - LOAD_OFFSET) { \ - VMLINUX_SYMBOL(__start___jump_table) = .; \ - *(__jump_table) \ - VMLINUX_SYMBOL(__stop___jump_table) = .; \ - } -#ifdef CONFIG_PM_TRACE #define TRACEDATA \ . = ALIGN(4); \
Signature · 1 line
1.7.1
Steven Rostedt
maintainerPATCH 7/9 jump label: Add docs
In reply to Steven RostedtFrom: Jason Baron <[email protected]>
Add jump label docs as: Documentation/jump-label.txt
Also, fix documentation spelling error in jump_label.c
LKML-Reference: <9a71c059f86d432ea468728281a8e570556c8a38.1285965957.git.jbaron@redhat.com>
--- Documentation/jump-label.txt | 142 ++++++++++++++++++++++++++++++++++++++++++ kernel/jump_label.c | 2 +- 2 files changed, 143 insertions(+), 1 deletions(-) create mode 100644 Documentation/jump-label.txt
Documentation/jump-label.txt new file +142 -0
@@ -0,0 +1,142 @@ + Jump Label + ---------- + +By: Jason Baron <[email protected]> + + +1) Motivation + + +Currently, tracepoints are implemented using a conditional. The conditional +check requires checking a global variable for each tracepoint. Although +the overhead of this check is small, it increases when the memory cache comes +under pressure (memory cache lines for these global variables may be shared +with other memory accesses). As we increase the number of tracepoints in the +kernel this overhead may become more of an issue. In addition, tracepoints are +often dormant (disabled) and provide no direct kernel functionality. Thus, it +is highly desirable to reduce their impact as much as possible. Although +tracepoints are the original motivation for this work, other kernel code paths +should be able to make use of the jump label optimization. + + +2) Jump label description/usage + + +gcc (v4.5) adds a new 'asm goto' statement that allows branching to a label. +http://gcc.gnu.org/ml/gcc-patches/2009-07/msg01556.html + +Thus, this patch set introduces an architecture specific 'JUMP_LABEL()' macro as +follows (x86): + +# define JUMP_LABEL_INITIAL_NOP ".byte 0xe9 \n\t .long 0\n\t" + +# define JUMP_LABEL(key, label) \ + do { \ + asm goto("1:" \ + JUMP_LABEL_INITIAL_NOP \ + ".pushsection __jump_table, \"a\" \n\t"\ + _ASM_PTR "1b, %l[" #label "], %c0 \n\t" \ + ".popsection \n\t" \ + : : "i" (key) : : label); \ + } while (0) + + +For architectures that have not yet introduced jump label support it's simply: + +#define JUMP_LABEL(key, label) \ + if (unlikely(*key)) \ + goto label; + +which then can be used as: + + .... + JUMP_LABEL(key, trace_label); + printk("not doing tracing\n"); + return; +trace_label: + printk("doing tracing: %d\n", file); + .... + +The 'key' argument is thus a pointer to a conditional argument that can be used +if the optimization is not enabled. Otherwise, this address serves as a unique +key to identify the particular instance of the jump label. + +Thus, when tracing is disabled, we simply have a no-op followed by a jump around +the dormant (disabled) tracing code. The 'JUMP_LABEL()' macro produces a +'jump_table', which has the following format: + +[instruction address] [jump target] [key] + +Thus, to enable a tracepoint, we simply patch the 'instruction address' with +a jump to the 'jump target.' + +The calls to enable and disable a jump label are: enable_jump_label(key) and +disable_jump_label(key). + + +3) Architecture interface + + +There are a few functions and macros that architectures must implement in order +to take advantage of this optimization. As previously mentioned, if there is no +architecture support, we simply fall back to a traditional, load, test, and +jump sequence. + +* add "HAVE_ARCH_JUMP_LABEL" to arch/<arch>/Kconfig to indicate support + +* #define JUMP_LABEL_NOP_SIZE, arch/x86/include/asm/jump_label.h + +* #define "JUMP_LABEL(key, label)", arch/x86/include/asm/jump_label.h + +* void arch_jump_label_transform(struct jump_entry *entry, enum jump_label_type type) + see: arch/x86/kernel/jump_label.c + +* void arch_jump_label_text_poke_early(jump_label_t addr) + see: arch/x86/kernel/jump_label.c + +* finally add a definition for "struct jump_entry". + see: arch/x86/include/asm/jump_label.h + + +4) Jump label analysis (x86) + + +I've tested the performance of using 'get_cycles()' calls around the +tracepoint call sites. For an Intel Core 2 Quad cpu (in cycles, averages): + + idle after tbench run + ---- ---------------- +old code 32 88 +new code 2 4 + + +The performance improvement can be reproduced reliably on both Intel and AMD +hardware. + +In terms of code analysis, the current code for the disabled case is a 'cmpl' +followed by a 'je' around the tracepoint code. So: + +cmpl - 83 3d 0e 77 87 00 00 - 7 bytes +je - 74 3e - 2 bytes + +total of 9 instruction bytes. + +The new code is a 'nopl' followed by a 'jmp'. Thus: + +nopl - 0f 1f 44 00 00 - 5 bytes +jmp - eb 3e - 2 bytes + +total of 7 instruction bytes. + +So, the new code also accounts for 2 less bytes in the instruction cache per tracepoint. + + +5) Acknowledgements + + +Thanks to Roland McGrath and Richard Henderson for helping come up with the +initial 'asm goto' and jump label design. + +Thanks to Mathieu Desnoyers and H. Peter Anvin for calling attention to this +issue, and outlining the requirements of a solution. Mathieu also implemented a +solution in the form of the "Immediate Values" work.
kernel/jump_label.c +1 -1
@@ -193,7 +193,7 @@ void jump_label_update(unsigned long key, enum jump_label_type type)arch_jump_label_transform (iter, type); iter++; } -/* eanble/disable jump labels in modules */ +/* enable/disable jump labels in modules */ hlist_for_each_entry (e_module, module_node, &(entry->modules), hlist) { count = e_module->nr_entries;
Signature · 1 line
1.7.1
Steven Rostedt
maintainerPATCH 8/9 jump label: Make arch_jump_label_text_poke_early() optional
In reply to Steven RostedtFrom: David Daney <[email protected]>
For the forthcoming MIPS jump label support, arch_jump_label_text_poke_early() is unneeded as the MIPS NOP instruction is already optimal.
Supply a default implementation that does nothing. Flag x86 and SPARC as having arch_jump_label_text_poke_early().
LKML-Reference: <[email protected]>
--- arch/sparc/include/asm/jump_label.h | 1 + arch/x86/include/asm/jump_label.h | 1 + include/linux/jump_label.h | 6 ++++++ 3 files changed, 8 insertions(+), 0 deletions(-)
arch/sparc/include/asm/jump_label.h +1 -0
@@ -6,6 +6,7 @@#include <linux/types.h> #include <asm/system.h> +#define HAVE_ARCH_JUMP_LABEL_TEXT_POKE_EARLY #define JUMP_LABEL_NOP_SIZE 4#define JUMP_LABEL (key, label) \
arch/x86/include/asm/jump_label.h +1 -0
@@ -6,6 +6,7 @@#include <linux/types.h> #include <asm/nops.h> +#define HAVE_ARCH_JUMP_LABEL_TEXT_POKE_EARLY #define JUMP_LABEL_NOP_SIZE 5# define JUMP_LABEL_INITIAL_NOP ".byte 0xe9 \n\t .long 0\n\t"
include/linux/jump_label.h +6 -0
@@ -22,7 +22,13 @@ extern void jump_label_lock(void);extern void jump_label_unlock (void );extern void arch_jump_label_transform (struct jump_entry *entry,enum jump_label_type type); + +#ifdef HAVE_ARCH_JUMP_LABEL_TEXT_POKE_EARLY extern void arch_jump_label_text_poke_early (jump_label_t addr); +#else +static inline void arch_jump_label_text_poke_early (jump_label_t addr) {} +#endif +extern void jump_label_update (unsigned long key,enum jump_label_type type);extern void jump_label_apply_nops (struct module *mod);extern int jump_label_text_reserved (void *start,void *end);
Signature · 1 line
1.7.1
Peter Zijlstra
maintainerRe: [PATCH 8/9] jump label: Make arch_jump_label_text_poke_early() optional
include/linux/jump_label.h extern void jump_label_lock(void); · line 31
Quoted 7 lines · Steven Rostedt
include/linux/jump_label.h +6
-0+ +#ifdef HAVE_ARCH_JUMP_LABEL_TEXT_POKE_EARLY extern void arch_jump_label_text_poke_early (jump_label_t addr); +#else +static inline void arch_jump_label_text_poke_early (jump_label_t addr) {} +#endif +
How often is this called, do we really need to avoid that call? It looks like init code.
Steven Rostedt
maintainerRe: [PATCH 8/9] jump label: Make arch_jump_label_text_poke_early() optional
Quoted 11 lines · Peter Zijlstra
include/linux/jump_label.h extern void jump_label_lock(void); · line 31
Quoted 7 lines · Steven Rostedt
include/linux/jump_label.h +6
-0+ +#ifdef HAVE_ARCH_JUMP_LABEL_TEXT_POKE_EARLY extern void arch_jump_label_text_poke_early (jump_label_t addr); +#else +static inline void arch_jump_label_text_poke_early (jump_label_t addr) {} +#endif +How often is this called, do we really need to avoid that call? It looks like init code.
Yeah, that's a good point.
What's the harm in doing a stub in the mips code here?
-- Steve
Re: [PATCH 8/9] jump label: Make arch_jump_label_text_poke_early() optional
Quoted 13 lines · Steven Rostedt
Quoted 11 lines · Peter Zijlstra
include/linux/jump_label.h extern void jump_label_lock(void); · line 31
Quoted 7 lines · Steven Rostedt
include/linux/jump_label.h +6
-0+ +#ifdef HAVE_ARCH_JUMP_LABEL_TEXT_POKE_EARLY extern void arch_jump_label_text_poke_early (jump_label_t addr); +#else +static inline void arch_jump_label_text_poke_early (jump_label_t addr) {} +#endif +How often is this called, do we really need to avoid that call? It looks like init code.
Quoted 4 lines
Yeah, that's a good point.
What's the harm in doing a stub in the mips code here?
Really the SPARC code (and any architectures added in the future with a well defined optimal NOP) should be doing nothing here as well.
So I could turn the question around and ask: How many empty definitions of this thing do you want in the tree?
I will defer to Steven. If he wants to push the empty stubs into all the architectures, I will update the MIPS patch and he can remove/revert the 'jump label: Make arch_jump_label_text_poke_early() optional' patch.
Let me know which option you would prefer.
David Daney
Steven Rostedt
maintainerRe: [PATCH 8/9] jump label: Make arch_jump_label_text_poke_early() optional
Quoted 11 lines · David Daney
Really the SPARC code (and any architectures added in the future with a well defined optimal NOP) should be doing nothing here as well.
So I could turn the question around and ask: How many empty definitions of this thing do you want in the tree?
I will defer to Steven. If he wants to push the empty stubs into all the architectures, I will update the MIPS patch and he can remove/revert the 'jump label: Make
arch_jump_label_text_poke_early()optional' patch.Let me know which option you would prefer.
On IRC, Thomas Gleixner mentioned a better solution (something I've done with ftrace). Just add a "weak" stub function. No need to get Kconfig involved.
-- Steve
Re: [PATCH 8/9] jump label: Make arch_jump_label_text_poke_early() optional
Quoted 12 lines · Steven Rostedt
Quoted 10 lines · David Daney
Really the SPARC code (and any architectures added in the future with a well defined optimal NOP) should be doing nothing here as well.
So I could turn the question around and ask: How many empty definitions of this thing do you want in the tree?
I will defer to Steven. If he wants to push the empty stubs into all the architectures, I will update the MIPS patch and he can remove/revert the 'jump label: Make
arch_jump_label_text_poke_early()optional' patch.
Quoted 6 lines
Quoted 1 line
Let me know which option you would prefer.
On IRC, Thomas Gleixner mentioned a better solution (something I've done with ftrace). Just add a "weak" stub function. No need to get
Kconfiginvolved.
Do you want to write that patch, or shall I?
David Daney
Steven Rostedt
maintainerRe: [PATCH 8/9] jump label: Make arch_jump_label_text_poke_early() optional
Quoted 13 lines · David Daney
Quoted 12 lines · Steven Rostedt
Quoted 10 lines · David Daney
Really the SPARC code (and any architectures added in the future with a well defined optimal NOP) should be doing nothing here as well.
So I could turn the question around and ask: How many empty definitions of this thing do you want in the tree?
I will defer to Steven. If he wants to push the empty stubs into all the architectures, I will update the MIPS patch and he can remove/revert the 'jump label: Make
arch_jump_label_text_poke_early()optional' patch.
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
Let me know which option you would prefer.
On IRC, Thomas Gleixner mentioned a better solution (something I've done with ftrace). Just add a "weak" stub function. No need to get
Kconfiginvolved.Do you want to write that patch, or shall I?
I can do it since I need to clean up some of the patch set anyway.
-- Steve
[tip:perf/urgent] jump label: Make arch_jump_label_text_poke_early() optional
In reply to Steven RostedtCommit-ID: 95bcd683fb694a3e2d0538bf486430a0dfbb4111 Gitweb: http://git.kernel.org/tip/95bcd683fb694a3e2d0538bf486430a0dfbb4111 Author: Steven Rostedt <[email protected]> AuthorDate: Fri, 29 Oct 2010 11:02:43 -0400 Committer: Steven Rostedt <[email protected]> CommitDate: Fri, 29 Oct 2010 12:56:13 -0400
jump label: Make arch_jump_label_text_poke_early() optional
Some archs do not need to do anything special for jump labels on startup (like MIPS). This patch adds a weak function stub for arch_jump_label_text_poke_early();
LKML-Reference: <[email protected]>
LKML-Reference: <[email protected]>
--- kernel/jump_label.c | 7 +++++++ 1 files changed, 7 insertions(+), 0 deletions(-)
kernel/jump_label.c +7 -0
@@ -270,6 +270,13 @@ out:return conflict; } +/* +* Not all archs need this. +*/ +void __weak arch_jump_label_text_poke_early (jump_label_t addr) +{ +} +static __init int init_jump_label (void ) {int ret;
Steven Rostedt
maintainerPATCH 9/9 jump label: Add MIPS support.
In reply to Steven RostedtFrom: David Daney <[email protected]>
When in Rome...
In order not to be left behind, we add jump label support for MIPS.
Tested on 64-bit big endian (Octeon), and 32-bit little endian (malta/qemu).
LKML-Reference: <[email protected]>
--- arch/mips/Kconfig | 1 + arch/mips/include/asm/jump_label.h | 48 ++++++++++++++++++++++++++++++++++ arch/mips/kernel/Makefile | 3 +- arch/mips/kernel/jump_label.c | 50 ++++++++++++++++++++++++++++++++++++ arch/mips/kernel/module.c | 5 +++ 5 files changed, 106 insertions(+), 1 deletions(-) create mode 100644 arch/mips/include/asm/jump_label.h create mode 100644 arch/mips/kernel/jump_label.c
arch/mips/Kconfig +1 -0
@@ -12,6 +12,7 @@ config MIPS select HAVE_FUNCTION_GRAPH_TRACER select HAVE_KPROBES select HAVE_KRETPROBES + select HAVE_ARCH_JUMP_LABEL select RTC_LIB if !MACH_LOONGSON select GENERIC_ATOMIC64 if !64BIT
arch/mips/include/asm/jump_label.h new file +48 -0
48 lines not on this page · raw message
arch/mips/kernel/Makefile +2 -1
@@ -6,7 +6,8 @@ extra-y := head.o init_task.o vmlinux.lds obj-y += cpu-probe.o branch.o entry.o genex.o irq.o process.o \ ptrace.o reset.o setup.o signal.o syscall.o \ - time.o topology.o traps.o unaligned.o watch.o vdso.o + time.o topology.o traps.o unaligned.o watch.o vdso.o \ + jump_label.o ifdef CONFIG_FUNCTION_TRACER CFLAGS_REMOVE_ftrace.o = -pg
arch/mips/kernel/jump_label.c new file +50 -0
50 lines not on this page · raw message
arch/mips/kernel/module.c +5 -0
@@ -30,6 +30,8 @@#include <linux/kernel.h> #include <linux/module.h> #include <linux/spinlock.h> +#include <linux/jump_label.h> +#include <asm/pgtable.h> /* MODULE_START */ struct mips_hi16 {
@@ -390,6 +392,9 @@ int module_finalize(const Elf_Ehdr *hdr,const Elf_Shdr *s;char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; +/* Make jump label nops. */ +jump_label_apply_nops (me); +INIT_LIST_HEAD (&me->arch.dbe_list);for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {if (strcmp ("__dbe_table" , secstrings + s->sh_name) !=0 )
Signature · 1 line
1.7.1
Steven Rostedt
maintainerRe: [PATCH 0/9] [GIT PULL] jump label: various updates
In reply to Steven RostedtQuoted 3 lines · Steven Rostedt
Ingo,
Please pull the latest tip/perf/jump-label tree, which can be found at:
Hold on, I forgot to add Jason Baron's fix for compiling against < 4.5.
I'll add that and push it out as jump-label-2
-- Steve
Quoted 39 lines
git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-2.6-trace.git
tip/perf/jump-labelDavid Daney (2):
jump label: Makearch_jump_label_text_poke_early()optional jump label: Add MIPS support.Jason Baron (6):
jump label/x86: Movearch_init_ideal_nop5later
jump label: Fix module__initsection race
jump label: Fix deadlock b/wjump_label_mutexvs.text_mutexjump label: Add register_jump_label_key/unregister_jump_label_key jump label: Move jump table to r/w section
jump label: Add docsSteven Rostedt (1):
tracing/x86: No need to disable interrupts when callingarch_init_ideal_nop5----
Documentation/jump-label.txt | 142 ++++++++++++++++++++++ arch/mips/Kconfig | 1 + arch/mips/include/asm/jump_label.h | 48 ++++++++ arch/mips/kernel/Makefile | 3 +- arch/mips/kernel/jump_label.c | 50 ++++++++ arch/mips/kernel/module.c | 5 + arch/sparc/include/asm/jump_label.h | 1 + arch/x86/include/asm/jump_label.h | 1 + arch/x86/kernel/alternative.c | 5 + arch/x86/kernel/setup.c | 6 - include/asm-generic/vmlinux.lds.h | 14 +-- include/linux/jump_label.h | 15 +++ kernel/jump_label.c | 222 ++++++++++++++++++++++++++++++----- kernel/kprobes.c | 6 + kernel/trace/ftrace.c | 4 +- kernel/tracepoint.c | 34 ++++++ lib/dynamic_debug.c | 20 +++ 17 files changed, 526 insertions(+), 51 deletions(-)
Steven Rostedt
maintainerRe: [PATCH 0/9] [GIT PULL] jump label: various updates
In reply to Steven RostedtQuoted 3 lines · Steven Rostedt
Ingo,
Please pull the latest tip/perf/jump-label tree, which can be found at:
OK, don't pull. With feedback from Peter, I'm going to do some clean ups and such.
He was still in Japan when the original patch set went out, and this is the first time he's seen them.
-- Steve