Skip to content

Commit d430d3d

Browse files
jibaronrostedt
authored andcommitted
jump label: Introduce static_branch() interface
Introduce: static __always_inline bool static_branch(struct jump_label_key *key); instead of the old JUMP_LABEL(key, label) macro. In this way, jump labels become really easy to use: Define: struct jump_label_key jump_key; Can be used as: if (static_branch(&jump_key)) do unlikely code enable/disale via: jump_label_inc(&jump_key); jump_label_dec(&jump_key); that's it! For the jump labels disabled case, the static_branch() becomes an atomic_read(), and jump_label_inc()/dec() are simply atomic_inc(), atomic_dec() operations. We show testing results for this change below. Thanks to H. Peter Anvin for suggesting the 'static_branch()' construct. Since we now require a 'struct jump_label_key *key', we can store a pointer into the jump table addresses. In this way, we can enable/disable jump labels, in basically constant time. This change allows us to completely remove the previous hashtable scheme. Thanks to Peter Zijlstra for this re-write. Testing: I ran a series of 'tbench 20' runs 5 times (with reboots) for 3 configurations, where tracepoints were disabled. jump label configured in avg: 815.6 jump label *not* configured in (using atomic reads) avg: 800.1 jump label *not* configured in (regular reads) avg: 803.4 Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> LKML-Reference: <20110316212947.GA8792@redhat.com> Signed-off-by: Jason Baron <jbaron@redhat.com> Suggested-by: H. Peter Anvin <hpa@linux.intel.com> Tested-by: David Daney <ddaney@caviumnetworks.com> Acked-by: Ralf Baechle <ralf@linux-mips.org> Acked-by: David S. Miller <davem@davemloft.net> Acked-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
1 parent ee5e51f commit d430d3d

15 files changed

Lines changed: 356 additions & 486 deletions

File tree

‎arch/mips/include/asm/jump_label.h‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@
2020
#define WORD_INSN ".word"
2121
#endif
2222

23-
#define JUMP_LABEL(key, label) \
24-
do { \
25-
asm goto("1:\tnop\n\t" \
26-
"nop\n\t" \
27-
".pushsection __jump_table, \"a\"\n\t" \
28-
WORD_INSN " 1b, %l[" #label "], %0\n\t" \
29-
".popsection\n\t" \
30-
: : "i" (key) : : label); \
31-
} while (0)
32-
23+
static __always_inline bool arch_static_branch(struct jump_label_key *key)
24+
{
25+
asm goto("1:\tnop\n\t"
26+
"nop\n\t"
27+
".pushsection __jump_table, \"aw\"\n\t"
28+
WORD_INSN " 1b, %l[l_yes], %0\n\t"
29+
".popsection\n\t"
30+
: : "i" (key) : : l_yes);
31+
return false;
32+
l_yes:
33+
return true;
34+
}
3335

3436
#endif /* __KERNEL__ */
3537

‎arch/sparc/include/asm/jump_label.h‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@
77

88
#define JUMP_LABEL_NOP_SIZE 4
99

10-
#define JUMP_LABEL(key, label) \
11-
do { \
12-
asm goto("1:\n\t" \
13-
"nop\n\t" \
14-
"nop\n\t" \
15-
".pushsection __jump_table, \"a\"\n\t"\
16-
".align 4\n\t" \
17-
".word 1b, %l[" #label "], %c0\n\t" \
18-
".popsection \n\t" \
19-
: : "i" (key) : : label);\
20-
} while (0)
10+
static __always_inline bool arch_static_branch(struct jump_label_key *key)
11+
{
12+
asm goto("1:\n\t"
13+
"nop\n\t"
14+
"nop\n\t"
15+
".pushsection __jump_table, \"aw\"\n\t"
16+
".align 4\n\t"
17+
".word 1b, %l[l_yes], %c0\n\t"
18+
".popsection \n\t"
19+
: : "i" (key) : : l_yes);
20+
return false;
21+
l_yes:
22+
return true;
23+
}
2124

2225
#endif /* __KERNEL__ */
2326

‎arch/x86/include/asm/alternative.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <linux/types.h>
55
#include <linux/stddef.h>
66
#include <linux/stringify.h>
7-
#include <linux/jump_label.h>
87
#include <asm/asm.h>
98

109
/*
@@ -191,7 +190,7 @@ extern void *text_poke(void *addr, const void *opcode, size_t len);
191190
extern void *text_poke_smp(void *addr, const void *opcode, size_t len);
192191
extern void text_poke_smp_batch(struct text_poke_param *params, int n);
193192

194-
#if defined(CONFIG_DYNAMIC_FTRACE) || defined(HAVE_JUMP_LABEL)
193+
#if defined(CONFIG_DYNAMIC_FTRACE) || defined(CONFIG_JUMP_LABEL)
195194
#define IDEAL_NOP_SIZE_5 5
196195
extern unsigned char ideal_nop5[IDEAL_NOP_SIZE_5];
197196
extern void arch_init_ideal_nop5(void);

‎arch/x86/include/asm/jump_label.h‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,24 @@
55

66
#include <linux/types.h>
77
#include <asm/nops.h>
8+
#include <asm/asm.h>
89

910
#define JUMP_LABEL_NOP_SIZE 5
1011

11-
# define JUMP_LABEL_INITIAL_NOP ".byte 0xe9 \n\t .long 0\n\t"
12-
13-
# define JUMP_LABEL(key, label) \
14-
do { \
15-
asm goto("1:" \
16-
JUMP_LABEL_INITIAL_NOP \
17-
".pushsection __jump_table, \"aw\" \n\t"\
18-
_ASM_PTR "1b, %l[" #label "], %c0 \n\t" \
19-
".popsection \n\t" \
20-
: : "i" (key) : : label); \
21-
} while (0)
12+
#define JUMP_LABEL_INITIAL_NOP ".byte 0xe9 \n\t .long 0\n\t"
13+
14+
static __always_inline bool arch_static_branch(struct jump_label_key *key)
15+
{
16+
asm goto("1:"
17+
JUMP_LABEL_INITIAL_NOP
18+
".pushsection __jump_table, \"aw\" \n\t"
19+
_ASM_PTR "1b, %l[l_yes], %c0 \n\t"
20+
".popsection \n\t"
21+
: : "i" (key) : : l_yes);
22+
return false;
23+
l_yes:
24+
return true;
25+
}
2226

2327
#endif /* __KERNEL__ */
2428

‎arch/x86/kernel/alternative.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ void __kprobes text_poke_smp_batch(struct text_poke_param *params, int n)
679679
__stop_machine(stop_machine_text_poke, (void *)&tpp, NULL);
680680
}
681681

682-
#if defined(CONFIG_DYNAMIC_FTRACE) || defined(HAVE_JUMP_LABEL)
682+
#if defined(CONFIG_DYNAMIC_FTRACE) || defined(CONFIG_JUMP_LABEL)
683683

684684
#ifdef CONFIG_X86_64
685685
unsigned char ideal_nop5[5] = { 0x66, 0x66, 0x66, 0x66, 0x90 };

‎arch/x86/kernel/module.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/bug.h>
2525
#include <linux/mm.h>
2626
#include <linux/gfp.h>
27+
#include <linux/jump_label.h>
2728

2829
#include <asm/system.h>
2930
#include <asm/page.h>

‎include/asm-generic/vmlinux.lds.h‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@
170170
STRUCT_ALIGN(); \
171171
*(__tracepoints) \
172172
/* implement dynamic printk debug */ \
173+
. = ALIGN(8); \
174+
VMLINUX_SYMBOL(__start___jump_table) = .; \
175+
*(__jump_table) \
176+
VMLINUX_SYMBOL(__stop___jump_table) = .; \
173177
. = ALIGN(8); \
174178
VMLINUX_SYMBOL(__start___verbose) = .; \
175179
*(__verbose) \
@@ -228,8 +232,6 @@
228232
\
229233
BUG_TABLE \
230234
\
231-
JUMP_TABLE \
232-
\
233235
/* PCI quirks */ \
234236
.pci_fixup : AT(ADDR(.pci_fixup) - LOAD_OFFSET) { \
235237
VMLINUX_SYMBOL(__start_pci_fixups_early) = .; \
@@ -589,14 +591,6 @@
589591
#define BUG_TABLE
590592
#endif
591593

592-
#define JUMP_TABLE \
593-
. = ALIGN(8); \
594-
__jump_table : AT(ADDR(__jump_table) - LOAD_OFFSET) { \
595-
VMLINUX_SYMBOL(__start___jump_table) = .; \
596-
*(__jump_table) \
597-
VMLINUX_SYMBOL(__stop___jump_table) = .; \
598-
}
599-
600594
#ifdef CONFIG_PM_TRACE
601595
#define TRACEDATA \
602596
. = ALIGN(4); \

‎include/linux/dynamic_debug.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#ifndef _DYNAMIC_DEBUG_H
22
#define _DYNAMIC_DEBUG_H
33

4-
#include <linux/jump_label.h>
5-
64
/* dynamic_printk_enabled, and dynamic_printk_enabled2 are bitmasks in which
75
* bit n is set to 1 if any modname hashes into the bucket n, 0 otherwise. They
86
* use independent hash functions, to reduce the chance of false positives.

‎include/linux/jump_label.h‎

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,43 @@
11
#ifndef _LINUX_JUMP_LABEL_H
22
#define _LINUX_JUMP_LABEL_H
33

4+
#include <linux/types.h>
5+
#include <linux/compiler.h>
6+
47
#if defined(CC_HAVE_ASM_GOTO) && defined(CONFIG_JUMP_LABEL)
8+
9+
struct jump_label_key {
10+
atomic_t enabled;
11+
struct jump_entry *entries;
12+
#ifdef CONFIG_MODULES
13+
struct jump_label_mod *next;
14+
#endif
15+
};
16+
517
# include <asm/jump_label.h>
618
# define HAVE_JUMP_LABEL
719
#endif
820

921
enum jump_label_type {
22+
JUMP_LABEL_DISABLE = 0,
1023
JUMP_LABEL_ENABLE,
11-
JUMP_LABEL_DISABLE
1224
};
1325

1426
struct module;
1527

1628
#ifdef HAVE_JUMP_LABEL
1729

30+
#ifdef CONFIG_MODULES
31+
#define JUMP_LABEL_INIT {{ 0 }, NULL, NULL}
32+
#else
33+
#define JUMP_LABEL_INIT {{ 0 }, NULL}
34+
#endif
35+
36+
static __always_inline bool static_branch(struct jump_label_key *key)
37+
{
38+
return arch_static_branch(key);
39+
}
40+
1841
extern struct jump_entry __start___jump_table[];
1942
extern struct jump_entry __stop___jump_table[];
2043

@@ -23,37 +46,37 @@ extern void jump_label_unlock(void);
2346
extern void arch_jump_label_transform(struct jump_entry *entry,
2447
enum jump_label_type type);
2548
extern void arch_jump_label_text_poke_early(jump_label_t addr);
26-
extern void jump_label_update(unsigned long key, enum jump_label_type type);
27-
extern void jump_label_apply_nops(struct module *mod);
2849
extern int jump_label_text_reserved(void *start, void *end);
50+
extern void jump_label_inc(struct jump_label_key *key);
51+
extern void jump_label_dec(struct jump_label_key *key);
52+
extern bool jump_label_enabled(struct jump_label_key *key);
53+
extern void jump_label_apply_nops(struct module *mod);
2954

30-
#define jump_label_enable(key) \
31-
jump_label_update((unsigned long)key, JUMP_LABEL_ENABLE);
55+
#else
3256

33-
#define jump_label_disable(key) \
34-
jump_label_update((unsigned long)key, JUMP_LABEL_DISABLE);
57+
#include <asm/atomic.h>
3558

36-
#else
59+
#define JUMP_LABEL_INIT {ATOMIC_INIT(0)}
3760

38-
#define JUMP_LABEL(key, label) \
39-
do { \
40-
if (unlikely(*key)) \
41-
goto label; \
42-
} while (0)
61+
struct jump_label_key {
62+
atomic_t enabled;
63+
};
4364

44-
#define jump_label_enable(cond_var) \
45-
do { \
46-
*(cond_var) = 1; \
47-
} while (0)
65+
static __always_inline bool static_branch(struct jump_label_key *key)
66+
{
67+
if (unlikely(atomic_read(&key->enabled)))
68+
return true;
69+
return false;
70+
}
4871

49-
#define jump_label_disable(cond_var) \
50-
do { \
51-
*(cond_var) = 0; \
52-
} while (0)
72+
static inline void jump_label_inc(struct jump_label_key *key)
73+
{
74+
atomic_inc(&key->enabled);
75+
}
5376

54-
static inline int jump_label_apply_nops(struct module *mod)
77+
static inline void jump_label_dec(struct jump_label_key *key)
5578
{
56-
return 0;
79+
atomic_dec(&key->enabled);
5780
}
5881

5982
static inline int jump_label_text_reserved(void *start, void *end)
@@ -64,16 +87,16 @@ static inline int jump_label_text_reserved(void *start, void *end)
6487
static inline void jump_label_lock(void) {}
6588
static inline void jump_label_unlock(void) {}
6689

67-
#endif
90+
static inline bool jump_label_enabled(struct jump_label_key *key)
91+
{
92+
return !!atomic_read(&key->enabled);
93+
}
6894

69-
#define COND_STMT(key, stmt) \
70-
do { \
71-
__label__ jl_enabled; \
72-
JUMP_LABEL(key, jl_enabled); \
73-
if (0) { \
74-
jl_enabled: \
75-
stmt; \
76-
} \
77-
} while (0)
95+
static inline int jump_label_apply_nops(struct module *mod)
96+
{
97+
return 0;
98+
}
99+
100+
#endif
78101

79102
#endif

‎include/linux/jump_label_ref.h‎

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)