Skip to contentSkip to conversation

jump label v6

PATCH In review Jason Baron 7 participants 20 messages Apr 9, 2010 – Apr 14, 2010

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

cover jump label v6

Hi,

Refresh of jump labeling patches aginst -tip tree. For bacground see:
http://marc.info/?l=linux-kernel&m=125858436505941&w=2

I believe I've addressed all the reviews from v5.

Changes in v6:

* I've moved Steve Rostedt's 'ftrace_dyn_arch_init()' to alternative.c to put it into a common area for used by both ftrace and jump labels. By default we put a 'jmp 5' in the nop slot. Then, when we detect the best runtime no-op we patch over the 'jmp 5' with the appropriate nop.

* build time sort of the jump label table. The jump label table is more optimally accessed if the entries are continguous. Sorting the table accomplishes this. Do the sort at build-time. Adds a '-j' option to 'modpost' which replaces the vmlinux, with a sorted jump label section vmlinux. I've tested this on x86 with relocatable and it works fine there as well. Note that I have not sorted the jump label table in modules. This is b/c the jump label names can be exported by the core kernel, and thus I don't have them available at buildtime. This could be solved by either finding the correct ones in the vmlinux, or by embedding the name of the jump label in the module tables (and not just a pointer), but the module tables tend to be smaller, and thus there is less value to this kind of change anyway. The kernel continues to do the sort, just in case, but at least for the vmlinux, this is just a verfication b/c the jump label table has already been sorted.

* added jump_label_text_reserved(), so that other routines that want to patch the code, can first verify that they are not stomping on jump label addresses.

thanks,

-Jason

Jason Baron (8):
jump label: base patch
jump label: x86 support
jump label: tracepoint support
jump label: add module support
jump label: move ftrace_dyn_arch_init to common code jump label: sort jump table at build-time
jump label: initialize workqueue tracepoints *before* they are
registered
jump label: jump_label_text_reserved() to reserve our jump points

Mathieu Desnoyers (1):
jump label: notifier atomic call chain notrace

 Makefile                           |    4 +
 arch/x86/include/asm/alternative.h |   14 ++
 arch/x86/include/asm/jump_label.h  |   27 +++
 arch/x86/kernel/Makefile           |    2 +-
 arch/x86/kernel/alternative.c      |   71 ++++++-
 arch/x86/kernel/ftrace.c           |   70 +------
 arch/x86/kernel/jump_label.c       |   42 ++++
 arch/x86/kernel/kprobes.c          |    3 +-
 arch/x86/kernel/module.c           |    3 +
 arch/x86/kernel/ptrace.c           |    1 +
 arch/x86/kernel/setup.c            |    2 +
 include/asm-generic/vmlinux.lds.h  |   22 ++-
 include/linux/jump_label.h         |   76 +++++++
 include/linux/module.h             |    5 +-
 include/linux/tracepoint.h         |   34 ++--
 kernel/Makefile                    |    2 +-
 kernel/jump_label.c                |  428 ++++++++++++++++++++++++++++++++++++
 kernel/kprobes.c                   |    3 +-
 kernel/module.c                    |    7 +
 kernel/notifier.c                  |    6 +-
 kernel/trace/ftrace.c              |   13 +-
 kernel/trace/trace_workqueue.c     |   10 +-
 kernel/tracepoint.c                |    8 +
 scripts/mod/modpost.c              |   69 ++++++-
 scripts/mod/modpost.h              |    9 +
 25 files changed, 816 insertions(+), 115 deletions(-)
 create mode 100644 arch/x86/include/asm/jump_label.h
 create mode 100644 arch/x86/kernel/jump_label.c
 create mode 100644 include/linux/jump_label.h
 create mode 100644 kernel/jump_label.c
#1 lore raw

PATCH 1/9 jump label: notifier atomic call chain notrace

In LTTng, being able to use the atomic notifier from cpu idle entry to ensure the tracer flush the last events in the current subbuffer requires the rcu read-side to be marked "notrace", otherwise it can end up calling back into lockdep and the tracer.

Also apply to the the die notifier.

---
 kernel/notifier.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
kernel/notifier.c +3 -3
index 2488ba7..88453a7 100644
@@ -148,7 +148,7 @@ int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
 	spin_lock_irqsave(&nh->lock, flags);
 	ret = notifier_chain_unregister(&nh->head, n);
 	spin_unlock_irqrestore(&nh->lock, flags);
-	synchronize_rcu();
+	synchronize_sched();
 	return ret;
 }
 EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
@@ -178,9 +178,9 @@ int __kprobes __atomic_notifier_call_chain(struct atomic_notifier_head *nh,
 {
 	int ret;
 
-	rcu_read_lock();
+	rcu_read_lock_sched_notrace();
 	ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
-	rcu_read_unlock();
+	rcu_read_unlock_sched_notrace();
 	return ret;
 }
 EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain);
Signature · 1 line
1.7.0.1
#3 lore raw

PATCH 3/9 jump label: x86 support

In reply to Jason Baron

add x86 support for jump label. I'm keeping this patch separate so its clear to arch maintainers what was required for x86 support this new feature. hopefully, it wouldn't be too painful for other arches.

---
 arch/x86/include/asm/jump_label.h |   31 +++++++++++++++++++++
 arch/x86/kernel/Makefile          |    2 +-
 arch/x86/kernel/jump_label.c      |   53 +++++++++++++++++++++++++++++++++++++
 3 files changed, 85 insertions(+), 1 deletions(-)
 create mode 100644 arch/x86/include/asm/jump_label.h
 create mode 100644 arch/x86/kernel/jump_label.c
arch/x86/include/asm/jump_label.h new file +31 -0
new file mode 100644
index 0000000..b8ebdc8
@@ -0,0 +1,31 @@
+#ifndef _ASM_X86_JUMP_LABEL_H
+#define _ASM_X86_JUMP_LABEL_H
+
+#include <asm/nops.h>
+
+#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
+# define __HAVE_ARCH_JUMP_LABEL
+#endif
+
+#ifdef __HAVE_ARCH_JUMP_LABEL
+
+# ifdef CONFIG_X86_64
+#  define JUMP_LABEL_NOP P6_NOP5
+# else
+#  define JUMP_LABEL_NOP ".byte 0xe9 \n\t .long 0\n\t"
+# endif
+
+# define JUMP_LABEL(tag, label, cond)				\
+	do {							\
+		extern const char __jlstrtab_##tag[];		\
+		asm goto("1:"					\
+			JUMP_LABEL_NOP				\
+			".pushsection __jump_table,  \"a\" \n\t"\
+			_ASM_PTR "1b, %l[" #label "], %c0 \n\t" \
+			".popsection \n\t"			\
+			: :  "i" (__jlstrtab_##tag) :  : label);\
+	} while (0)
+
+# endif
+
+#endif
arch/x86/kernel/Makefile +1 -1
index 4c58352..7cd3bf4 100644
@@ -32,7 +32,7 @@ GCOV_PROFILE_paravirt.o		:= n
 obj-y			:= process_$(BITS).o signal.o entry_$(BITS).o
 obj-y			+= traps.o irq.o irq_$(BITS).o dumpstack_$(BITS).o
 obj-y			+= time.o ioport.o ldt.o dumpstack.o
-obj-y			+= setup.o x86_init.o i8259.o irqinit.o
+obj-y			+= setup.o x86_init.o i8259.o irqinit.o jump_label.o
 obj-$(CONFIG_X86_VISWS)	+= visws_quirks.o
 obj-$(CONFIG_X86_32)	+= probe_roms_32.o
 obj-$(CONFIG_X86_32)	+= sys_i386_32.o i386_ksyms_32.o
arch/x86/kernel/jump_label.c new file +53 -0

53 lines not on this page · raw message

Signature · 1 line
1.7.0.1
#2 lore raw

PATCH 2/9 jump label: base patch

In reply to Jason Baron

base patch to implement 'jump labeling'. Based on a new 'asm goto' inline assembly gcc mechanism, we can now branch to labels from an 'asm goto' statment. This allows us to create a 'no-op' fastpath, which can subsequently be patched with a jump to the slowpath code. This is useful for code which might be rarely used, but which we'd like to be able to call, if needed. Tracepoints are the current usecase that these are being implemented for.

---
 include/asm-generic/vmlinux.lds.h |   10 ++-
 include/linux/jump_label.h        |   59 ++++++++++++
 kernel/Makefile                   |    2 +-
 kernel/jump_label.c               |  176 +++++++++++++++++++++++++++++++++++++
 4 files changed, 245 insertions(+), 2 deletions(-)
 create mode 100644 include/linux/jump_label.h
 create mode 100644 kernel/jump_label.c
include/asm-generic/vmlinux.lds.h +9 -1
index 67e6520..83a469d 100644
@@ -167,7 +167,8 @@
 	BRANCH_PROFILE()						\
 	TRACE_PRINTKS()							\
 	FTRACE_EVENTS()							\
-	TRACE_SYSCALLS()
+	TRACE_SYSCALLS()						\
+	JUMP_TABLE()							\
 
 /*
  * Data section helpers
@@ -206,6 +207,7 @@
 		*(__vermagic)		/* Kernel version magic */	\
 		*(__markers_strings)	/* Markers: strings */		\
 		*(__tracepoints_strings)/* Tracepoints: strings */	\
+		*(__jump_strings)/* Jump: strings */	\
 	}								\
 									\
 	.rodata1          : AT(ADDR(.rodata1) - LOAD_OFFSET) {		\
@@ -557,6 +559,12 @@
 #define BUG_TABLE
 #endif
 
+#define JUMP_TABLE()							\
+	. = ALIGN(64);							\
+		VMLINUX_SYMBOL(__start___jump_table) = .;		\
+		*(__jump_table)						\
+		VMLINUX_SYMBOL(__stop___jump_table) = .;		\
+
 #ifdef CONFIG_PM_TRACE
 #define TRACEDATA							\
 	. = ALIGN(4);							\
include/linux/jump_label.h new file +59 -0

59 lines not on this page · raw message

kernel/Makefile +1 -1
index d5c3006..59ff12e 100644
@@ -10,7 +10,7 @@ obj-y     = sched.o fork.o exec_domain.o panic.o printk.o \
 	    kthread.o wait.o kfifo.o sys_ni.o posix-cpu-timers.o mutex.o \
 	    hrtimer.o rwsem.o nsproxy.o srcu.o semaphore.o \
 	    notifier.o ksysfs.o pm_qos_params.o sched_clock.o cred.o \
-	    async.o range.o
+	    async.o range.o jump_label.o
 obj-$(CONFIG_HAVE_EARLY_RES) += early_res.o
 obj-y += groups.o
 
kernel/jump_label.c new file +176 -0

176 lines not on this page · raw message

Signature · 1 line
1.7.0.1
#6 lore raw

PATCH 6/9 jump label: move ftrace_dyn_arch_init to common code

In reply to Jason Baron

Move Steve's code for finding the best 5-byte no-op from ftrace.c to alternative.c. The idea is that other consumers (in this case jump label) want to make use of that code. I've created a global: 'char ideal_nop[5]', that is setup during setup_arch that can be used.

---
 arch/x86/include/asm/alternative.h |   14 +++++++
 arch/x86/include/asm/jump_label.h  |   10 ++----
 arch/x86/kernel/alternative.c      |   71 +++++++++++++++++++++++++++++++++++-
 arch/x86/kernel/ftrace.c           |   70 +-----------------------------------
 arch/x86/kernel/jump_label.c       |   15 +-------
 arch/x86/kernel/module.c           |    3 ++
 arch/x86/kernel/setup.c            |    2 +
 include/linux/jump_label.h         |    9 +++++
 kernel/jump_label.c                |   33 +++++++++++++++++
 kernel/trace/ftrace.c              |   13 +------
 10 files changed, 137 insertions(+), 103 deletions(-)
arch/x86/include/asm/alternative.h +14 -0
index b09ec55..0218dbd 100644
@@ -5,6 +5,7 @@
 #include <linux/stddef.h>
 #include <linux/stringify.h>
 #include <asm/asm.h>
+#include <asm/jump_label.h>
 
 /*
  * Alternative inline assembly for SMP.
@@ -153,6 +154,8 @@ static inline void apply_paravirt(struct paravirt_patch_site *start,
 #define __parainstructions_end	NULL
 #endif
 
+extern void *text_poke_early(void *addr, const void *opcode, size_t len);
+
 /*
  * Clear and restore the kernel write-protection flag on the local CPU.
  * Allows the kernel to edit read-only pages.
@@ -173,4 +176,15 @@ static inline void apply_paravirt(struct paravirt_patch_site *start,
 extern void *text_poke(void *addr, const void *opcode, size_t len);
 extern void *text_poke_smp(void *addr, const void *opcode, size_t len);
 
+#if defined(CONFIG_DYNAMIC_FTRACE) || defined(__HAVE_ARCH_JUMP_LABEL)
+#define IDEAL_NOP_SIZE_5 5
+extern unsigned char ideal_nop5[IDEAL_NOP_SIZE_5];
+extern int arch_init_ideal_nop5(void);
+#else
+static inline arch_init_ideal_nop5(void)
+{
+	return 0;
+}
+#endif
+
 #endif /* _ASM_X86_ALTERNATIVE_H */
arch/x86/include/asm/jump_label.h +3 -7
index b8ebdc8..e3af6ca 100644
@@ -9,23 +9,19 @@
 
 #ifdef __HAVE_ARCH_JUMP_LABEL
 
-# ifdef CONFIG_X86_64
-#  define JUMP_LABEL_NOP P6_NOP5
-# else
-#  define JUMP_LABEL_NOP ".byte 0xe9 \n\t .long 0\n\t"
-# endif
+# define JUMP_LABEL_INITIAL_NOP ".byte 0xe9 \n\t .long 0\n\t"
 
 # define JUMP_LABEL(tag, label, cond)				\
 	do {							\
 		extern const char __jlstrtab_##tag[];		\
 		asm goto("1:"					\
-			JUMP_LABEL_NOP				\
+			JUMP_LABEL_INITIAL_NOP			\
 			".pushsection __jump_table,  \"a\" \n\t"\
 			_ASM_PTR "1b, %l[" #label "], %c0 \n\t" \
 			".popsection \n\t"			\
 			: :  "i" (__jlstrtab_##tag) :  : label);\
 	} while (0)
 
-# endif
+#endif
 
 #endif
arch/x86/kernel/alternative.c +69 -2

86 lines not on this page · raw message

arch/x86/kernel/ftrace.c +1 -69

84 lines not on this page · raw message

arch/x86/kernel/jump_label.c +2 -13
index 7fc4f84..8eca1b8 100644
@@ -30,19 +30,8 @@ void arch_jump_label_transform(struct jump_entry *entry, enum jump_label_type ty
 	if (type == JUMP_LABEL_ENABLE) {
 		code.jump = 0xe9;
 		code.offset = entry->target - (entry->code + RELATIVEJUMP_SIZE);
-	} else {
-#ifdef CONFIG_X86_64
-		/* opcode for P6_NOP5 */
-		code.code[0] = 0x0f;
-		code.code[1] = 0x1f;
-		code.code[2] = 0x44;
-		code.code[3] = 0x00;
-		code.code[4] = 0x00;
-#else
-		code.jump = 0xe9;
-		code.offset = 0;
-#endif
-	}
+	} else
+		memcpy(&code, ideal_nop5, 5);
 	get_online_cpus();
 	mutex_lock(&text_mutex);
 	text_poke_smp((void *)entry->code, &code, RELATIVEJUMP_SIZE);
arch/x86/kernel/module.c +3 -0
index 89f386f..e47fe49 100644
@@ -238,6 +238,9 @@ int module_finalize(const Elf_Ehdr *hdr,
 		apply_paravirt(pseg, pseg + para->sh_size);
 	}
 
+	/* make jump label nops */
+	apply_jump_label_nops(me);
+
 	return module_bug_finalize(hdr, sechdrs, me);
 }
 
arch/x86/kernel/setup.c +2 -0
index 5d7ba1a..7a4577a 100644
@@ -1040,6 +1040,8 @@ void __init setup_arch(char **cmdline_p)
 	x86_init.oem.banner();
 
 	mcheck_init();
+
+	arch_init_ideal_nop5();
 }
 
 #ifdef CONFIG_X86_32
include/linux/jump_label.h +9 -0
index e0f968d..9868c43 100644
@@ -14,6 +14,8 @@ enum jump_label_type {
 	JUMP_LABEL_DISABLE
 };
 
+struct module;
+
 #ifdef __HAVE_ARCH_JUMP_LABEL
 
 extern struct jump_entry __start___jump_table[];
@@ -29,6 +31,8 @@ extern void arch_jump_label_transform(struct jump_entry *entry,
 
 extern void jump_label_update(const char *name, enum jump_label_type type);
 
+extern void apply_jump_label_nops(struct module *mod);
+
 #define enable_jump_label(name) \
 	jump_label_update(name, JUMP_LABEL_ENABLE);
 
@@ -55,6 +59,11 @@ static inline int disable_jump_label(const char *name)
 	return 0;
 }
 
+static inline int apply_jump_label_nops(struct module *mod)
+{
+	return 0;
+}
+
 #endif
 
 #endif
kernel/jump_label.c +33 -0
index 0714c20..7e7458b 100644
@@ -182,10 +182,19 @@ void jump_label_update(const char *name, enum jump_label_type type)
 static int init_jump_label(void)
 {
 	int ret;
+	struct jump_entry *iter_start = __start___jump_table;
+	struct jump_entry *iter_stop = __start___jump_table;
+	struct jump_entry *iter;
 
 	mutex_lock(&jump_label_mutex);
 	ret = build_jump_label_hashtable(__start___jump_table,
 					 __stop___jump_table);
+	/* update with ideal nop */
+	iter = iter_start;
+	while (iter < iter_stop) {
+		text_poke_early((void *)iter->code, ideal_nop5, IDEAL_NOP_SIZE_5);
+		iter++;
+	}
 	mutex_unlock(&jump_label_mutex);
 	return ret;
 }
@@ -296,6 +305,30 @@ static int jump_label_module_notify(struct notifier_block *self, unsigned long v
 	return ret;
 }
 
+/***
+ * apply_jump_label_nops - patch module jump labels with ideal_nop5
+ * @mod: module to patch
+ *
+ * When a module is intially loaded the code has 'jump 5' instructions
+ * as nops. These are not the most optimal nops, so before the module
+ * loads patch these with the 'ideal_nop5', which was determined boot
+ * time.
+ */
+void apply_jump_label_nops(struct module *mod)
+{
+	struct jump_entry *iter;
+
+	/* if the module doesn't have jump label entries, just return */
+	if (!mod->num_jump_entries)
+		return;
+
+	iter = mod->jump_entries;
+	while (iter < mod->jump_entries + mod->num_jump_entries) {
+		text_poke_early((void *)iter->code, ideal_nop5, IDEAL_NOP_SIZE_5);
+		iter++;
+	}
+}
+
 struct notifier_block jump_label_module_nb = {
 	.notifier_call = jump_label_module_notify,
 	.priority = 0,
kernel/trace/ftrace.c +1 -12
index 8378357..abe6eaf 100644
@@ -2725,20 +2725,9 @@ extern unsigned long __stop_mcount_loc[];
 
 void __init ftrace_init(void)
 {
-	unsigned long count, addr, flags;
+	unsigned long count;
 	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)
-		goto failed;
-
 	count = __stop_mcount_loc - __start_mcount_loc;
 
 	ret = ftrace_dyn_table_alloc(count);
Signature · 1 line
1.7.0.1
#10 lore raw

PATCH 8/9 jump label: initialize workqueue tracepoints *before* they are registered

In reply to Jason Baron

Initialize the workqueue data structures *before* they are registered so that they are ready for callbacks.

---
 kernel/trace/trace_workqueue.c |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)
kernel/trace/trace_workqueue.c +5 -5
index 40cafb0..487d821 100644
@@ -258,6 +258,11 @@ int __init trace_workqueue_early_init(void)
 {
 	int ret, cpu;
 
+	for_each_possible_cpu(cpu) {
+		spin_lock_init(&workqueue_cpu_stat(cpu)->lock);
+		INIT_LIST_HEAD(&workqueue_cpu_stat(cpu)->list);
+	}
+
 	ret = register_trace_workqueue_insertion(probe_workqueue_insertion);
 	if (ret)
 		goto out;
@@ -274,11 +279,6 @@ int __init trace_workqueue_early_init(void)
 	if (ret)
 		goto no_creation;
 
-	for_each_possible_cpu(cpu) {
-		spin_lock_init(&workqueue_cpu_stat(cpu)->lock);
-		INIT_LIST_HEAD(&workqueue_cpu_stat(cpu)->list);
-	}
-
 	return 0;
 
 no_creation:
Signature · 1 line
1.7.0.1
#11 lore raw

PATCH 9/9 jump label: jump_label_text_reserved() to reserve our jump points

In reply to Jason Baron

Add a jump_label_text_reserved(void *start, void *end), so that other pieces of code that want to modify kernel text, can first verify that jump label has not reserved the instruction.

---
 arch/x86/kernel/kprobes.c  |    3 +-
 include/linux/jump_label.h |    6 +++
 kernel/jump_label.c        |   83 ++++++++++++++++++++++++++++++++++++++++++++
 kernel/kprobes.c           |    3 +-
 4 files changed, 93 insertions(+), 2 deletions(-)
arch/x86/kernel/kprobes.c +2 -1
index b43bbae..87bcf63 100644
@@ -1194,7 +1194,8 @@ static int __kprobes copy_optimized_instructions(u8 *dest, u8 *src)
 	}
 	/* Check whether the address range is reserved */
 	if (ftrace_text_reserved(src, src + len - 1) ||
-	    alternatives_text_reserved(src, src + len - 1))
+	    alternatives_text_reserved(src, src + len - 1) ||
+	    jump_label_text_reserved(src, src + len - 1))
 		return -EBUSY;
 
 	return len;
include/linux/jump_label.h +6 -0
index 7238805..90ca4b6 100644
@@ -31,6 +31,7 @@ extern void arch_jump_label_transform(struct jump_entry *entry,
 				 enum jump_label_type type);
 
 extern void jump_label_update(const char *name, enum jump_label_type type);
+extern int jump_label_text_reserved(void *start, void *end);
 
 extern void apply_jump_label_nops(struct module *mod);
 
@@ -65,6 +66,11 @@ static inline int apply_jump_label_nops(struct module *mod)
 	return 0;
 }
 
+static inline int jump_label_text_reserved(void *start, void *end)
+{
+	return 0;
+}
+
 #endif
 
 #endif
kernel/jump_label.c +83 -0

89 lines not on this page · raw message

kernel/kprobes.c +2 -1
index fa034d2..a57755f 100644
@@ -1140,7 +1140,8 @@ int __kprobes register_kprobe(struct kprobe *p)
 	preempt_disable();
 	if (!kernel_text_address((unsigned long) p->addr) ||
 	    in_kprobes_functions((unsigned long) p->addr) ||
-	    ftrace_text_reserved(p->addr, p->addr)) {
+	    ftrace_text_reserved(p->addr, p->addr) ||
+	    jump_label_text_reserved(p->addr, p->addr)) {
 		preempt_enable();
 		return -EINVAL;
 	}
Signature · 1 line
1.7.0.1
#5 lore raw

PATCH 5/9 jump label: add module support

In reply to Jason Baron

Add support for 'jump label' for modules.

---
 arch/x86/kernel/ptrace.c   |    1 +
 include/linux/jump_label.h |    3 +-
 include/linux/module.h     |    5 +-
 kernel/jump_label.c        |  136 ++++++++++++++++++++++++++++++++++++++++++++
 kernel/module.c            |    7 ++
 5 files changed, 150 insertions(+), 2 deletions(-)
arch/x86/kernel/ptrace.c +1 -0
index 2d96aab..21854d2 100644
@@ -24,6 +24,7 @@
 #include <linux/workqueue.h>
 #include <linux/perf_event.h>
 #include <linux/hw_breakpoint.h>
+#include <linux/module.h>
 
 #include <asm/uaccess.h>
 #include <asm/pgtable.h>
include/linux/jump_label.h +2 -1
index 122d441..e0f968d 100644
@@ -21,7 +21,8 @@ extern struct jump_entry __stop___jump_table[];
 
 #define DEFINE_JUMP_LABEL(name)						\
 	const char __jlstrtab_##name[]					\
-	__used __attribute__((section("__jump_strings")))  = #name;
+	__used __attribute__((section("__jump_strings")))  = #name;	\
+	EXPORT_SYMBOL_GPL(__jlstrtab_##name);
 
 extern void arch_jump_label_transform(struct jump_entry *entry,
 				 enum jump_label_type type);
include/linux/module.h +4 -1
index dd618eb..6d7225e 100644
@@ -339,7 +339,10 @@ struct module
 	struct tracepoint *tracepoints;
 	unsigned int num_tracepoints;
 #endif
-
+#ifdef __HAVE_ARCH_JUMP_LABEL
+	struct jump_entry *jump_entries;
+	unsigned int num_jump_entries;
+#endif
 #ifdef CONFIG_TRACING
 	const char **trace_bprintk_fmt_start;
 	unsigned int num_trace_bprintk_fmt;
kernel/jump_label.c +136 -0

152 lines not on this page · raw message

kernel/module.c +7 -0
index c968d36..a8c34a2 100644
@@ -55,6 +55,7 @@
 #include <linux/async.h>
 #include <linux/percpu.h>
 #include <linux/kmemleak.h>
+#include <linux/jump_label.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/module.h>
@@ -2249,6 +2250,12 @@ static noinline struct module *load_module(void __user *umod,
 					sizeof(*mod->tracepoints),
 					&mod->num_tracepoints);
 #endif
+#ifdef __HAVE_ARCH_JUMP_LABEL
+	mod->jump_entries = section_objs(hdr, sechdrs, secstrings,
+					"__jump_table",
+					sizeof(*mod->jump_entries),
+					&mod->num_jump_entries);
+#endif
 #ifdef CONFIG_EVENT_TRACING
 	mod->trace_events = section_objs(hdr, sechdrs, secstrings,
 					 "_ftrace_events",
Signature · 1 line
1.7.0.1
#4 lore raw

PATCH 4/9 jump label: tracepoint support

In reply to Jason Baron

Make use of the jump label infrastructure for tracepoints.

---
 include/linux/tracepoint.h |   34 +++++++++++++++++++---------------
 kernel/tracepoint.c        |    8 ++++++++
 2 files changed, 27 insertions(+), 15 deletions(-)
include/linux/tracepoint.h +19 -15
index f59604e..c18b9c0 100644
@@ -16,6 +16,7 @@
 
 #include <linux/types.h>
 #include <linux/rcupdate.h>
+#include <linux/jump_label.h>
 
 struct module;
 struct tracepoint;
@@ -63,20 +64,22 @@ struct tracepoint {
  * not add unwanted padding between the beginning of the section and the
  * structure. Force alignment to the same alignment as the section start.
  */
-#define DECLARE_TRACE(name, proto, args)				\
-	extern struct tracepoint __tracepoint_##name;			\
-	static inline void trace_##name(proto)				\
-	{								\
-		if (unlikely(__tracepoint_##name.state))		\
-			__DO_TRACE(&__tracepoint_##name,		\
-				TP_PROTO(proto), TP_ARGS(args));	\
-	}								\
-	static inline int register_trace_##name(void (*probe)(proto))	\
-	{								\
-		return tracepoint_probe_register(#name, (void *)probe);	\
-	}								\
-	static inline int unregister_trace_##name(void (*probe)(proto))	\
-	{								\
+#define DECLARE_TRACE(name, proto, args)				 \
+	extern struct tracepoint __tracepoint_##name;			 \
+	static inline void trace_##name(proto)				 \
+	{								 \
+		JUMP_LABEL(name, do_trace, __tracepoint_##name.state);   \
+		return;							 \
+do_trace:								 \
+		__DO_TRACE(&__tracepoint_##name,			 \
+			   TP_PROTO(proto), TP_ARGS(args));		 \
+	}								 \
+	static inline int register_trace_##name(void (*probe)(proto))	 \
+	{								 \
+		return tracepoint_probe_register(#name, (void *)probe);	 \
+	}								 \
+	static inline int unregister_trace_##name(void (*probe)(proto))	 \
+	{								 \
 		return tracepoint_probe_unregister(#name, (void *)probe);\
 	}
 
@@ -86,7 +89,8 @@ struct tracepoint {
 	__attribute__((section("__tracepoints_strings"))) = #name;	\
 	struct tracepoint __tracepoint_##name				\
 	__attribute__((section("__tracepoints"), aligned(32))) =	\
-		{ __tpstrtab_##name, 0, reg, unreg, NULL }
+		{ __tpstrtab_##name, 0, reg, unreg, NULL };		\
+	DEFINE_JUMP_LABEL(name);
 
 #define DEFINE_TRACE(name)						\
 	DEFINE_TRACE_FN(name, NULL, NULL);
kernel/tracepoint.c +8 -0
index cc89be5..8acced8 100644
@@ -25,6 +25,7 @@
 #include <linux/err.h>
 #include <linux/slab.h>
 #include <linux/sched.h>
+#include <linux/jump_label.h>
 
 extern struct tracepoint __start___tracepoints[];
 extern struct tracepoint __stop___tracepoints[];
@@ -256,6 +257,10 @@ static void set_tracepoint(struct tracepoint_entry **entry,
 	 * is used.
 	 */
 	rcu_assign_pointer(elem->funcs, (*entry)->funcs);
+	if (!elem->state && active)
+		enable_jump_label(elem->name);
+	if (elem->state && !active)
+		disable_jump_label(elem->name);
 	elem->state = active;
 }
 
@@ -270,6 +275,9 @@ static void disable_tracepoint(struct tracepoint *elem)
 	if (elem->unregfunc && elem->state)
 		elem->unregfunc();
 
+	if (elem->state)
+		disable_jump_label(elem->name);
+
 	elem->state = 0;
 	rcu_assign_pointer(elem->funcs, NULL);
 }
Signature · 1 line
1.7.0.1
#7 lore raw

PATCH 7/9 jump label: sort jump table at build-time

In reply to Jason Baron

The jump label table is more optimal accessed if the entries are continguous. Sorting the table accomplishes this. Do the sort at build-time. Adds a '-j' option to 'modpost' which replaces the vmlinux, with a sorted jump label section vmlinux. I've tested this on x86 with relocatable and it works fine there as well. Note that I have not sorted the jump label table in modules. This is b/c the jump label names can be exported by the core kernel, and thus I don't have them available at buildtime. This could be solved by either finding the correct ones in the vmlinux, or by embedding the name of the jump label in the module tables (and not just a pointer), but the module tables tend to be smaller, and thus their is less value to this kind of change anyway. The kernel continues to do the sort, just in case, but at least for the vmlinux, this is just a verfication that the jump label table has already been sorted.

---
 Makefile                          |    4 ++
 include/asm-generic/vmlinux.lds.h |   18 ++++++++--
 include/linux/jump_label.h        |    1 +
 scripts/mod/modpost.c             |   69 +++++++++++++++++++++++++++++++++++--
 scripts/mod/modpost.h             |    9 +++++
 5 files changed, 95 insertions(+), 6 deletions(-)
Makefile +4 -0
index 558ddec..f59dd57 100644
@@ -845,6 +845,9 @@ define rule_vmlinux-modpost
 	$(Q)echo 'cmd_$@ := $(cmd_vmlinux-modpost)' > $(dot-target).cmd
 endef
 
+quiet_cmd_sort-jump-label = SORT    $@
+      cmd_sort-jump-label = $(srctree)/scripts/mod/modpost -j $@
+
 # vmlinux image - including updated kernel symbols
 vmlinux: $(vmlinux-lds) $(vmlinux-init) $(vmlinux-main) vmlinux.o $(kallsyms.o) FORCE
 ifdef CONFIG_HEADERS_CHECK
@@ -858,6 +861,7 @@ ifdef CONFIG_BUILD_DOCSRC
 endif
 	$(call vmlinux-modpost)
 	$(call if_changed_rule,vmlinux__)
+	$(call cmd,sort-jump-label)
 	$(Q)rm -f .old_version
 
 # build vmlinux.o first to catch section mismatch errors early
include/asm-generic/vmlinux.lds.h +15 -3
index 83a469d..f9d8188 100644
@@ -168,7 +168,6 @@
 	TRACE_PRINTKS()							\
 	FTRACE_EVENTS()							\
 	TRACE_SYSCALLS()						\
-	JUMP_TABLE()							\
 
 /*
  * Data section helpers
@@ -207,7 +206,6 @@
 		*(__vermagic)		/* Kernel version magic */	\
 		*(__markers_strings)	/* Markers: strings */		\
 		*(__tracepoints_strings)/* Tracepoints: strings */	\
-		*(__jump_strings)/* Jump: strings */	\
 	}								\
 									\
 	.rodata1          : AT(ADDR(.rodata1) - LOAD_OFFSET) {		\
@@ -216,6 +214,10 @@
 									\
 	BUG_TABLE							\
 									\
+	JUMP_TABLE							\
+									\
+	JUMP_STRINGS							\
+									\
 	/* PCI quirks */						\
 	.pci_fixup        : AT(ADDR(.pci_fixup) - LOAD_OFFSET) {	\
 		VMLINUX_SYMBOL(__start_pci_fixups_early) = .;		\
@@ -559,11 +561,21 @@
 #define BUG_TABLE
 #endif
 
-#define JUMP_TABLE()							\
+#define JUMP_TABLE							\
 	. = ALIGN(64);							\
+	__jump_table : AT(ADDR(__jump_table) - LOAD_OFFSET) {		\
 		VMLINUX_SYMBOL(__start___jump_table) = .;		\
 		*(__jump_table)						\
 		VMLINUX_SYMBOL(__stop___jump_table) = .;		\
+	}
+
+#define JUMP_STRINGS							\
+	. = ALIGN(64);							\
+	__jump_strings : AT(ADDR(__jump_strings) - LOAD_OFFSET) {	\
+		VMLINUX_SYMBOL(__start___jump_strings) = .;		\
+		*(__jump_strings)					\
+		VMLINUX_SYMBOL(__stop___jump_strings) = .;		\
+	}
 
 #ifdef CONFIG_PM_TRACE
 #define TRACEDATA							\
include/linux/jump_label.h +1 -0
index 9868c43..7238805 100644
@@ -3,6 +3,7 @@
 
 #include <asm/jump_label.h>
 
+/* struct jump_entry must match scripts/mod/mopost.h */
 struct jump_entry {
 	unsigned long code;
 	unsigned long target;
scripts/mod/modpost.c +66 -3

115 lines not on this page · raw message

scripts/mod/modpost.h +9 -0
index be987a4..312b6db 100644
@@ -126,11 +126,20 @@ struct elf_info {
 	Elf_Section  export_gpl_sec;
 	Elf_Section  export_unused_gpl_sec;
 	Elf_Section  export_gpl_future_sec;
+	Elf_Section  jump_sec;
+	Elf_Section  jump_strings_sec;
 	const char   *strtab;
 	char	     *modinfo;
 	unsigned int modinfo_len;
 };
 
+/* struct jump_entry must match include/linux/jump_labal.h */
+struct jump_entry {
+	unsigned long code;
+	unsigned long target;
+	char *name;
+};
+
 /* file2alias.c */
 extern unsigned int cross_build;
 void handle_moddevtable(struct module *mod, struct elf_info *info,
Signature · 1 line
1.7.0.1
#13 lore raw
In reply to Jason Baron

Hi Jason,

Quoted 28 lines · Jason Baron

Hi,

Refresh of jump labeling patches aginst -tip tree. For bacground see:
http://marc.info/?l=linux-kernel&m=125858436505941&w=2

I believe I've addressed all the reviews from v5.

Changes in v6:

* I've moved Steve Rostedt's 'ftrace_dyn_arch_init()' to alternative.c to put it into a common area for used by both ftrace and jump labels. By default we put a 'jmp 5' in the nop slot. Then, when we detect the best runtime no-op we patch over the 'jmp 5' with the appropriate nop.

* build time sort of the jump label table. The jump label table is more optimally accessed if the entries are continguous. Sorting the table accomplishes this. Do the sort at build-time. Adds a '-j' option to 'modpost' which replaces the vmlinux, with a sorted jump label section vmlinux. I've tested this on x86 with relocatable and it works fine there as well. Note that I have not sorted the jump label table in modules. This is b/c the jump label names can be exported by the core kernel, and thus I don't have them available at buildtime. This could be solved by either finding the correct ones in the vmlinux, or by embedding the name of the jump label in the module tables (and not just a pointer), but the module tables tend to be smaller, and thus there is less value to this kind of change anyway. The kernel continues to do the sort, just in case, but at least for the vmlinux, this is just a verfication b/c the jump label table has already been sorted.

Quoted 2 lines

* added jump_label_text_reserved(), so that other routines that want to patch the code, can first verify that they are not stomping on jump label addresses.

Good!:-)
So now, it might be a good time to integrate those text_reserved() functions.

BTW, how many jumps would you expect modifying at once? Since the text_poke_smp() uses stop_machine() for each modifying text, I found that it can cause a delay issue if it is called so many times... (e.g. a systemtap testcase sets ~5000 probes at once)

Thank you,

Signature · 2 lines
Masami Hiramatsu
e-mail: [email protected]
Acked-by on v1 9/9
#12 lore raw

Re: [PATCH 9/9] jump label: jump_label_text_reserved() to reserve our jump points

In reply to Jason Baron
Quoted 5 lines · Jason Baron

Add a jump_label_text_reserved(void *start, void *end), so that other pieces of code that want to modify kernel text, can first verify that jump label has not reserved the instruction.

At least kprobes parts. :)

Thank you,

Quoted 153 lines
---
 arch/x86/kernel/kprobes.c  |    3 +-
 include/linux/jump_label.h |    6 +++
 kernel/jump_label.c        |   83 ++++++++++++++++++++++++++++++++++++++++++++
 kernel/kprobes.c           |    3 +-
 4 files changed, 93 insertions(+), 2 deletions(-)
arch/x86/kernel/kprobes.c +2 -1
index b43bbae..87bcf63 100644
@@ -1194,7 +1194,8 @@ static int __kprobes copy_optimized_instructions(u8 *dest, u8 *src)
 	}
 	/* Check whether the address range is reserved */
 	if (ftrace_text_reserved(src, src + len - 1) ||
-	    alternatives_text_reserved(src, src + len - 1))
+	    alternatives_text_reserved(src, src + len - 1) ||
+	    jump_label_text_reserved(src, src + len - 1))
 		return -EBUSY;
 
 	return len;
include/linux/jump_label.h +6 -0
index 7238805..90ca4b6 100644
@@ -31,6 +31,7 @@ extern void arch_jump_label_transform(struct jump_entry *entry,
 				 enum jump_label_type type);
 
 extern void jump_label_update(const char *name, enum jump_label_type type);
+extern int jump_label_text_reserved(void *start, void *end);
 
 extern void apply_jump_label_nops(struct module *mod);
 
@@ -65,6 +66,11 @@ static inline int apply_jump_label_nops(struct module *mod)
 	return 0;
 }
 
+static inline int jump_label_text_reserved(void *start, void *end)
+{
+	return 0;
+}
+
 #endif
 
 #endif
kernel/jump_label.c +83 -0
index 7e7458b..24bba61 100644
@@ -179,6 +179,89 @@ void jump_label_update(const char *name, enum jump_label_type type)
 	mutex_unlock(&jump_label_mutex);
 }
 
+static int addr_conflict(struct jump_entry *entry, void *start, void *end)
+{
+	if (entry->code <= (unsigned long)end &&
+		entry->code + IDEAL_NOP_SIZE_5 > (unsigned long)start)
+		return 1;
+
+	return 0;
+}
+
+#ifdef CONFIG_MODULES
+
+static int module_conflict(void *start, void *end)
+{
+	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;
+	int conflict = 0;
+
+	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) {
+				count = e_module->nr_entries;
+				iter = e_module->table;
+				while (count--) {
+					if (addr_conflict(iter, start, end)) {
+						conflict = 1;
+						goto out;
+					}
+					iter++;
+				}
+			}
+		}
+	}
+out:
+	return conflict;
+}
+
+#endif
+
+/***
+ * jump_label_text_reserved - check if addr range is reserved
+ * @start: start text addr
+ * @end: end text addr
+ *
+ * checks if the text addr located between @start and @end
+ * 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.
+ *
+ * returns 1 if there is an overlap, 0 otherwise
+ */
+int jump_label_text_reserved(void *start, void *end)
+{
+	struct jump_entry *iter;
+	struct jump_entry *iter_start = __start___jump_table;
+	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)) {
+			conflict = 1;
+			goto out;
+		}
+		iter++;
+	}
+
+	/* now check modules */
+#ifdef CONFIG_MODULES
+	conflict = module_conflict(start, end);
+#endif
+out:
+	mutex_unlock(&jump_label_mutex);
+	return conflict;
+}
+
 static int init_jump_label(void)
 {
 	int ret;
kernel/kprobes.c +2 -1
index fa034d2..a57755f 100644
@@ -1140,7 +1140,8 @@ int __kprobes register_kprobe(struct kprobe *p)
 	preempt_disable();
 	if (!kernel_text_address((unsigned long) p->addr) ||
 	    in_kprobes_functions((unsigned long) p->addr) ||
-	    ftrace_text_reserved(p->addr, p->addr)) {
+	    ftrace_text_reserved(p->addr, p->addr) ||
+	    jump_label_text_reserved(p->addr, p->addr)) {
 		preempt_enable();
 		return -EINVAL;
 	}
Signature · 2 lines
Masami Hiramatsu
e-mail: [email protected]
#8 lore raw

Re: [PATCH 7/9] jump label: sort jump table at build-time

In reply to Jason Baron
Quoted 14 lines · Jason Baron

The jump label table is more optimal accessed if the entries are continguous. Sorting the table accomplishes this. Do the sort at build-time. Adds a '-j' option to 'modpost' which replaces the vmlinux, with a sorted jump label section vmlinux. I've tested this on x86 with relocatable and it works fine there as well. Note that I have not sorted the jump label table in modules. This is b/c the jump label names can be exported by the core kernel, and thus I don't have them available at buildtime. This could be solved by either finding the correct ones in the vmlinux, or by embedding the name of the jump label in the module tables (and not just a pointer), but the module tables tend to be smaller, and thus their is less value to this kind of change anyway. The kernel continues to do the sort, just in case, but at least for the vmlinux, this is just a verfication that the jump label table has already been sorted.

Quoted 2 lines

---

[ CCing Eric ]

[..]

Quoted 29 lines
scripts/mod/modpost.c +29 -0
+static void swap_jump_label_entries(struct jump_entry *previous, struct jump_entry *next)
+{
+	struct jump_entry tmp;
+
+	tmp = *next;
+	*next = *previous;
+	*previous = tmp;
+}
+
+static void sort_jump_label_table(struct elf_info *info, Elf_Ehdr *hdr)
+{
+	int swapped = 0;
+	struct jump_entry *iter, *iter_next;
+	char *name, *next_name;
+	Elf_Shdr *sechdrs = info->sechdrs;
+	unsigned long jump_table, jump_table_end;
+	unsigned long jump_strings, jump_strings_addr;
+
+	if ((info->jump_sec == 0) && (info->jump_strings_sec == 0))
+		return;
+
+	jump_table = (unsigned long)hdr + sechdrs[info->jump_sec].sh_offset;
+	jump_table_end = jump_table + sechdrs[info->jump_sec].sh_size;
+	jump_strings = (unsigned long)hdr +
+				sechdrs[info->jump_strings_sec].sh_offset;
+	jump_strings_addr = sechdrs[info->jump_strings_sec].sh_addr;
+
+	do {
+		swapped = 0;

scripts/mod/modpost.c void release_file(void *file, unsigned long size) · line 416

Quoted 10 lines
scripts/mod/modpost.c +10 -0
+		iter = iter_next = (struct jump_entry *)jump_table;
+		iter_next++;
+		for (; iter_next < (struct jump_entry *)jump_table_end;
+							iter++, iter_next++) {
+			name = jump_strings + (iter->name - jump_strings_addr);
+			next_name = jump_strings +
+					(iter_next->name - jump_strings_addr);
+			if (strcmp(name, next_name) > 0) {
+				swap_jump_label_entries(iter, iter_next);
+				swapped = 1;

Jason,

As we were chatting about this, it looks like you are modifying vmlinux section outside the knowledge of compiler. So theoritically associated relocation section knowledge is no more valid and it can be a problem during i386 relocatable kernels where we read the section's relocation inforamtion and perform the relocations at runtime.

I know you have tested this on i386 and it works for you. I guess it works because all the entries in the section are same and we apply same relocation offset to all entries so even changing the order of entries is not impacting.

But conceptually, changing the vmlinux section outside knowledge of compiler and assuming that we don't have to change the associated relocation section probably is not the best thing.

I am not sure how to fix it. May be rely back on boot time sorting, or if there is a way to relink sections after sorting etc. I just wanted to raise a concern. May be other people (Eric, hpa) have ideas whether it is a valid concern or not or how to handle it better.

Thanks
Vivek

#9 lore raw

Re: [PATCH 7/9] jump label: sort jump table at build-time

I think it just happens to come out harmless. The relocs.c extraction stuff doesn't actually care about the exact address to be relocated, it only needs to know that a relocated value sits at a given place in the binary. The sorting rearranges the addresses in the text without rearranging their corresponding relocs, but it's still the case that at exactly each of those spots in text there is a relocated address. The boot-time "relocation" is just a blind adjustment to all those spots, without reference to the original relocation details. So it Just Works. I'm not at all sure this is how we want things to be. It's rather nonobvious and fragile if we change any of the related magic. But I think it is entirely reliable in today's code that it will do the right thing.

Thanks,
Roland

#14 lore raw
In reply to Masami Hiramatsu
Quoted 37 lines · Masami Hiramatsu

Hi Jason,

Quoted 28 lines · Jason Baron

Hi,

Refresh of jump labeling patches aginst -tip tree. For bacground see:
http://marc.info/?l=linux-kernel&m=125858436505941&w=2

I believe I've addressed all the reviews from v5.

Changes in v6:

* I've moved Steve Rostedt's 'ftrace_dyn_arch_init()' to alternative.c to put it into a common area for used by both ftrace and jump labels. By default we put a 'jmp 5' in the nop slot. Then, when we detect the best runtime no-op we patch over the 'jmp 5' with the appropriate nop.

* build time sort of the jump label table. The jump label table is more optimally accessed if the entries are continguous. Sorting the table accomplishes this. Do the sort at build-time. Adds a '-j' option to 'modpost' which replaces the vmlinux, with a sorted jump label section vmlinux. I've tested this on x86 with relocatable and it works fine there as well. Note that I have not sorted the jump label table in modules. This is b/c the jump label names can be exported by the core kernel, and thus I don't have them available at buildtime. This could be solved by either finding the correct ones in the vmlinux, or by embedding the name of the jump label in the module tables (and not just a pointer), but the module tables tend to be smaller, and thus there is less value to this kind of change anyway. The kernel continues to do the sort, just in case, but at least for the vmlinux, this is just a verfication b/c the jump label table has already been sorted.

Quoted 2 lines

* added jump_label_text_reserved(), so that other routines that want to patch the code, can first verify that they are not stomping on jump label addresses.

Good!:-)
So now, it might be a good time to integrate those text_reserved() functions.

Quoted 7 lines

BTW, how many jumps would you expect modifying at once? Since the text_poke_smp() uses stop_machine() for each modifying text, I found that it can cause a delay issue if it is called so many times... (e.g. a systemtap testcase sets ~5000 probes at once)

Thank you,

I'm counting 934 jump label locations in the vmlinux, i have compiled, 675 of them being 'kmalloc'. Batch mode for text_poke_smp()?

-Jason

#15 lore raw
Quoted 43 lines · Jason Baron
Quoted 42 lines · Masami Hiramatsu

Hi Jason,

Quoted 28 lines · Jason Baron

Hi,

Refresh of jump labeling patches aginst -tip tree. For bacground see:
http://marc.info/?l=linux-kernel&m=125858436505941&w=2

I believe I've addressed all the reviews from v5.

Changes in v6:

* I've moved Steve Rostedt's 'ftrace_dyn_arch_init()' to alternative.c to put it into a common area for used by both ftrace and jump labels. By default we put a 'jmp 5' in the nop slot. Then, when we detect the best runtime no-op we patch over the 'jmp 5' with the appropriate nop.

* build time sort of the jump label table. The jump label table is more optimally accessed if the entries are continguous. Sorting the table accomplishes this. Do the sort at build-time. Adds a '-j' option to 'modpost' which replaces the vmlinux, with a sorted jump label section vmlinux. I've tested this on x86 with relocatable and it works fine there as well. Note that I have not sorted the jump label table in modules. This is b/c the jump label names can be exported by the core kernel, and thus I don't have them available at buildtime. This could be solved by either finding the correct ones in the vmlinux, or by embedding the name of the jump label in the module tables (and not just a pointer), but the module tables tend to be smaller, and thus there is less value to this kind of change anyway. The kernel continues to do the sort, just in case, but at least for the vmlinux, this is just a verfication b/c the jump label table has already been sorted.

Quoted 2 lines

* added jump_label_text_reserved(), so that other routines that want to patch the code, can first verify that they are not stomping on jump label addresses.

Good!:-)
So now, it might be a good time to integrate those text_reserved() functions.

BTW, how many jumps would you expect modifying at once? Since the text_poke_smp() uses stop_machine() for each modifying text, I found that it can cause a delay issue if it is called so many times... (e.g. a systemtap testcase sets ~5000 probes at once)

Quoted 5 lines
Quoted 2 lines

Thank you,

I'm counting 934 jump label locations in the vmlinux, i have compiled, 675 of them being 'kmalloc'. Batch mode for text_poke_smp()?

Yeah:) I'm now trying to make it with updating kprobes.

Thank you,

Signature · 2 lines
Masami Hiramatsu
e-mail: [email protected]
maintainer
#16 lore raw
In reply to Jason Baron

I took a stab at implementing this on sparc64 but there are just too many x86-specific references in kernel/jump_label.c to even build this on non-x86 currently.

Please abstract out all of that ideal nop business so that whatever values you need can be specified in the asm/jump_label.h header. Probably what you want is the nop sequence size. Fine, call it JUMP_LABEL_NOP_SIZE and define it in asm/jump_label.h

Also, kernel/jump_label.c is built unconditionally, and thus linux/jump_label.h is included unconditionally, but that unconditionally includes asm/jump_label.h which will break the build on every platform that hasn't implemented the header file yet.

Once you sort this out the patch below will probably work, once the nop size defines have been added (sparc will use 2 nops so the sequence is 8 bytes).

Finally, please abstract out the jump label and string address type. Even on 64-bit sparc all of the kernel addresses are 32-bits so we can use "u32" for all of the addresses jump label wants to record. This will decrease the jump label section size. There is at least one other 64-bit platform that can benefit from this, which I think is s390x if I'm not mistaken.

Thanks.

--------------------
sparc64: Add jump_label support.

---
 arch/sparc/include/asm/jump_label.h |   24 ++++++++++++++++++++++++
 arch/sparc/kernel/Makefile          |    2 ++
 arch/sparc/kernel/jump_label.c      |   29 +++++++++++++++++++++++++++++
 arch/sparc/kernel/module.c          |    6 ++++++
 4 files changed, 61 insertions(+), 0 deletions(-)
 create mode 100644 arch/sparc/include/asm/jump_label.h
 create mode 100644 arch/sparc/kernel/jump_label.c
arch/sparc/include/asm/jump_label.h new file +24 -0
new file mode 100644
index 0000000..efcd950
@@ -0,0 +1,24 @@
+#ifndef _ASM_SPARC_JUMP_LABEL_H
+#define _ASM_SPARC_JUMP_LABEL_H
+
+#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
+# define __HAVE_ARCH_JUMP_LABEL
+#endif
+
+#ifdef __HAVE_ARCH_JUMP_LABEL
+
+# define JUMP_LABEL(tag, label, cond)				\
+	do {							\
+		extern const char __jlstrtab_##tag[];		\
+		asm goto("1:\n\t"				\
+			 "nop\n\t"				\
+			 "nop\n\t"				\
+			 ".pushsection __jump_table,  \"a\"\n\t"\
+			 ".xword 1b, %l[" #label "], %c0\n\t"	\
+			 ".popsection \n\t"			\
+			 : :  "i" (__jlstrtab_##tag) :  : label);\
+	} while (0)
+
+# endif
+
+#endif
arch/sparc/kernel/Makefile +2 -0
index 0c2dc1f..599398f 100644
@@ -119,3 +119,5 @@ obj-$(CONFIG_COMPAT)    += $(audit--y)
 
 pc--$(CONFIG_PERF_EVENTS) := perf_event.o
 obj-$(CONFIG_SPARC64)	+= $(pc--y)
+
+obj-$(CONFIG_SPARC64)	+= jump_label.o
arch/sparc/kernel/jump_label.c new file +29 -0
new file mode 100644
index 0000000..c636ccf
@@ -0,0 +1,29 @@
+#include <linux/kernel.h>
+#include <linux/types.h>
+#include <linux/mutex.h>
+#include <linux/cpu.h>
+
+#include <linux/jump_label.h>
+#include <linux/memory.h>
+
+#ifdef __HAVE_ARCH_JUMP_LABEL
+void arch_jump_label_transform(struct jump_entry *entry, enum jump_label_type type)
+{
+	u32 val, *insn = (u32 *) entry->code;
+
+	val = *insn;
+	if (type == JUMP_LABEL_ENABLE) {
+		s32 off = (s32)entry->target - (s32)entry->code;
+		val = 0x40000000 | ((u32) off >> 2);
+	} else {
+		val = 0x01000000;
+	}
+
+	get_online_cpus();
+	mutex_lock(&text_mutex);
+	*insn = val;
+	flushi(insn);
+	mutex_unlock(&text_mutex);
+	put_online_cpus();
+}
+#endif
arch/sparc/kernel/module.c +6 -0
index 0ee642f..469f380 100644
@@ -18,6 +18,9 @@
 #include <asm/spitfire.h>
 
 #ifdef CONFIG_SPARC64
+
+#include <linux/jump_label.h>
+
 static void *module_map(unsigned long size)
 {
 	struct vm_struct *area;
@@ -227,6 +230,9 @@ int module_finalize(const Elf_Ehdr *hdr,
 		    const Elf_Shdr *sechdrs,
 		    struct module *me)
 {
+	/* make jump label nops */
+	apply_jump_label_nops(me);
+
 	/* Cheetah's I-cache is fully coherent.  */
 	if (tlb_type == spitfire) {
 		unsigned long va;
Signature · 1 line
1.6.6.1
#17 lore raw
Quoted 8 lines · David Miller

Finally, please abstract out the jump label and string address type. Even on 64-bit sparc all of the kernel addresses are 32-bits so we can use "u32" for all of the addresses jump label wants to record. This will decrease the jump label section size. There is at least one other 64-bit platform that can benefit from this, which I think is s390x if I'm not mistaken.

Even on x86 they're 32 bits -- but signed.

-hpa

Signature · 2 lines
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.
#18 lore raw
In reply to Jason Baron
Quoted 6 lines · * Jason Baron ([email protected]) wrote:

Hi,

Refresh of jump labeling patches aginst -tip tree. For bacground see:
http://marc.info/?l=linux-kernel&m=125858436505941&w=2

I believe I've addressed all the reviews from v5.

Hi Jason,

I would appreciate if you could add pointers to the Immediate Values benchmarks (or possibly some benchmark information altogether) and a notice that some parts of the design are inspired from Immediate Values in the jump label code.

I recognise that you did great work on getting jump label in shape both at the Linux kernel and gcc level, but it's usually appropriate to acknowledge prior work it is based on. Only then can I justify that Immediate Values have been useful in the whole process.

Thanks,

Mathieu

Quoted 74 lines

Changes in v6:

* I've moved Steve Rostedt's 'ftrace_dyn_arch_init()' to alternative.c to put it into a common area for used by both ftrace and jump labels. By default we put a 'jmp 5' in the nop slot. Then, when we detect the best runtime no-op we patch over the 'jmp 5' with the appropriate nop.

* build time sort of the jump label table. The jump label table is more optimally accessed if the entries are continguous. Sorting the table accomplishes this. Do the sort at build-time. Adds a '-j' option to 'modpost' which replaces the vmlinux, with a sorted jump label section vmlinux. I've tested this on x86 with relocatable and it works fine there as well. Note that I have not sorted the jump label table in modules. This is b/c the jump label names can be exported by the core kernel, and thus I don't have them available at buildtime. This could be solved by either finding the correct ones in the vmlinux, or by embedding the name of the jump label in the module tables (and not just a pointer), but the module tables tend to be smaller, and thus there is less value to this kind of change anyway. The kernel continues to do the sort, just in case, but at least for the vmlinux, this is just a verfication b/c the jump label table has already been sorted.

* added jump_label_text_reserved(), so that other routines that want to patch the code, can first verify that they are not stomping on jump label addresses.

thanks,

-Jason

Jason Baron (8):
jump label: base patch
jump label: x86 support
jump label: tracepoint support
jump label: add module support
jump label: move ftrace_dyn_arch_init to common code jump label: sort jump table at build-time
jump label: initialize workqueue tracepoints *before* they are
registered
jump label: jump_label_text_reserved() to reserve our jump points

Mathieu Desnoyers (1):
jump label: notifier atomic call chain notrace

 Makefile                           |    4 +
 arch/x86/include/asm/alternative.h |   14 ++
 arch/x86/include/asm/jump_label.h  |   27 +++
 arch/x86/kernel/Makefile           |    2 +-
 arch/x86/kernel/alternative.c      |   71 ++++++-
 arch/x86/kernel/ftrace.c           |   70 +------
 arch/x86/kernel/jump_label.c       |   42 ++++
 arch/x86/kernel/kprobes.c          |    3 +-
 arch/x86/kernel/module.c           |    3 +
 arch/x86/kernel/ptrace.c           |    1 +
 arch/x86/kernel/setup.c            |    2 +
 include/asm-generic/vmlinux.lds.h  |   22 ++-
 include/linux/jump_label.h         |   76 +++++++
 include/linux/module.h             |    5 +-
 include/linux/tracepoint.h         |   34 ++--
 kernel/Makefile                    |    2 +-
 kernel/jump_label.c                |  428 ++++++++++++++++++++++++++++++++++++
 kernel/kprobes.c                   |    3 +-
 kernel/module.c                    |    7 +
 kernel/notifier.c                  |    6 +-
 kernel/trace/ftrace.c              |   13 +-
 kernel/trace/trace_workqueue.c     |   10 +-
 kernel/tracepoint.c                |    8 +
 scripts/mod/modpost.c              |   69 ++++++-
 scripts/mod/modpost.h              |    9 +
 25 files changed, 816 insertions(+), 115 deletions(-)
 create mode 100644 arch/x86/include/asm/jump_label.h
 create mode 100644 arch/x86/kernel/jump_label.c
 create mode 100644 include/linux/jump_label.h
 create mode 100644 kernel/jump_label.c
Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
#19 lore raw
Quoted 15 lines · Mathieu Desnoyers
Quoted 6 lines · * Jason Baron ([email protected]) wrote:

Hi,

Refresh of jump labeling patches aginst -tip tree. For bacground see:
http://marc.info/?l=linux-kernel&m=125858436505941&w=2

I believe I've addressed all the reviews from v5.

Hi Jason,

I would appreciate if you could add pointers to the Immediate Values benchmarks (or possibly some benchmark information altogether) and a notice that some parts of the design are inspired from Immediate Values in the jump label code.

Quoted 5 lines

I recognise that you did great work on getting jump label in shape both at the Linux kernel and gcc level, but it's usually appropriate to acknowledge prior work it is based on. Only then can I justify that Immediate Values have been useful in the whole process.

So I've been doing micro-benchmarks measuring the cycles involved when the tracepoints are disabled. As quoted from the above pointer:

"As discussed in pervious mails I've seen an average improvement of 30 cycles per-tracepoint on x86_64 systems that I've tested."

I can post my test harness if you are interested.

If there are any other benchmarks of interest please let me know.

I am planning to add a Docmentation/ file for jump labels, so I can add about the previous Immediate value work, which certainly has been useful.

thanks,

-Jason