4 * Copyright (C) 2009 Jason Baron <jbaron@redhat.com>
5 * Copyright (C) 2011 Peter Zijlstra
8 #include <linux/memory.h>
9 #include <linux/uaccess.h>
10 #include <linux/module.h>
11 #include <linux/list.h>
12 #include <linux/slab.h>
13 #include <linux/sort.h>
14 #include <linux/err.h>
15 #include <linux/static_key.h>
16 #include <linux/jump_label_ratelimit.h>
17 #include <linux/bug.h>
18 #include <linux/cpu.h>
20 #ifdef HAVE_JUMP_LABEL
22 /* mutex to protect coming/going of the the jump_label table */
23 static DEFINE_MUTEX(jump_label_mutex
);
25 void jump_label_lock(void)
27 mutex_lock(&jump_label_mutex
);
30 void jump_label_unlock(void)
32 mutex_unlock(&jump_label_mutex
);
35 static int jump_label_cmp(const void *a
, const void *b
)
37 const struct jump_entry
*jea
= a
;
38 const struct jump_entry
*jeb
= b
;
40 if (jea
->key
< jeb
->key
)
43 if (jea
->key
> jeb
->key
)
50 jump_label_sort_entries(struct jump_entry
*start
, struct jump_entry
*stop
)
54 size
= (((unsigned long)stop
- (unsigned long)start
)
55 / sizeof(struct jump_entry
));
56 sort(start
, size
, sizeof(struct jump_entry
), jump_label_cmp
, NULL
);
59 static void jump_label_update(struct static_key
*key
);
62 * There are similar definitions for the !HAVE_JUMP_LABEL case in jump_label.h.
63 * The use of 'atomic_read()' requires atomic.h and its problematic for some
64 * kernel headers such as kernel.h and others. Since static_key_count() is not
65 * used in the branch statements as it is for the !HAVE_JUMP_LABEL case its ok
66 * to have it be a function here. Similarly, for 'static_key_enable()' and
67 * 'static_key_disable()', which require bug.h. This should allow jump_label.h
68 * to be included from most/all places for HAVE_JUMP_LABEL.
70 int static_key_count(struct static_key
*key
)
73 * -1 means the first static_key_slow_inc() is in progress.
74 * static_key_enabled() must return true, so return 1 here.
76 int n
= atomic_read(&key
->enabled
);
78 return n
>= 0 ? n
: 1;
80 EXPORT_SYMBOL_GPL(static_key_count
);
82 void static_key_slow_inc_cpuslocked(struct static_key
*key
)
86 STATIC_KEY_CHECK_USE();
89 * Careful if we get concurrent static_key_slow_inc() calls;
90 * later calls must wait for the first one to _finish_ the
91 * jump_label_update() process. At the same time, however,
92 * the jump_label_update() call below wants to see
93 * static_key_enabled(&key) for jumps to be updated properly.
95 * So give a special meaning to negative key->enabled: it sends
96 * static_key_slow_inc() down the slow path, and it is non-zero
97 * so it counts as "enabled" in jump_label_update(). Note that
98 * atomic_inc_unless_negative() checks >= 0, so roll our own.
100 for (v
= atomic_read(&key
->enabled
); v
> 0; v
= v1
) {
101 v1
= atomic_cmpxchg(&key
->enabled
, v
, v
+ 1);
107 if (atomic_read(&key
->enabled
) == 0) {
108 atomic_set(&key
->enabled
, -1);
109 jump_label_update(key
);
111 * Ensure that if the above cmpxchg loop observes our positive
112 * value, it must also observe all the text changes.
114 atomic_set_release(&key
->enabled
, 1);
116 atomic_inc(&key
->enabled
);
121 void static_key_slow_inc(struct static_key
*key
)
124 static_key_slow_inc_cpuslocked(key
);
127 EXPORT_SYMBOL_GPL(static_key_slow_inc
);
129 void static_key_enable_cpuslocked(struct static_key
*key
)
131 STATIC_KEY_CHECK_USE();
133 if (atomic_read(&key
->enabled
) > 0) {
134 WARN_ON_ONCE(atomic_read(&key
->enabled
) != 1);
139 if (atomic_read(&key
->enabled
) == 0) {
140 atomic_set(&key
->enabled
, -1);
141 jump_label_update(key
);
143 * See static_key_slow_inc().
145 atomic_set_release(&key
->enabled
, 1);
149 EXPORT_SYMBOL_GPL(static_key_enable_cpuslocked
);
151 void static_key_enable(struct static_key
*key
)
154 static_key_enable_cpuslocked(key
);
157 EXPORT_SYMBOL_GPL(static_key_enable
);
159 void static_key_disable_cpuslocked(struct static_key
*key
)
161 STATIC_KEY_CHECK_USE();
163 if (atomic_read(&key
->enabled
) != 1) {
164 WARN_ON_ONCE(atomic_read(&key
->enabled
) != 0);
169 if (atomic_cmpxchg(&key
->enabled
, 1, 0))
170 jump_label_update(key
);
173 EXPORT_SYMBOL_GPL(static_key_disable_cpuslocked
);
175 void static_key_disable(struct static_key
*key
)
178 static_key_disable_cpuslocked(key
);
181 EXPORT_SYMBOL_GPL(static_key_disable
);
183 static void __static_key_slow_dec_cpuslocked(struct static_key
*key
,
184 unsigned long rate_limit
,
185 struct delayed_work
*work
)
188 * The negative count check is valid even when a negative
189 * key->enabled is in use by static_key_slow_inc(); a
190 * __static_key_slow_dec() before the first static_key_slow_inc()
191 * returns is unbalanced, because all other static_key_slow_inc()
192 * instances block while the update is in progress.
194 if (!atomic_dec_and_mutex_lock(&key
->enabled
, &jump_label_mutex
)) {
195 WARN(atomic_read(&key
->enabled
) < 0,
196 "jump label: negative count!\n");
201 atomic_inc(&key
->enabled
);
202 schedule_delayed_work(work
, rate_limit
);
204 jump_label_update(key
);
209 static void __static_key_slow_dec(struct static_key
*key
,
210 unsigned long rate_limit
,
211 struct delayed_work
*work
)
214 __static_key_slow_dec_cpuslocked(key
, rate_limit
, work
);
218 static void jump_label_update_timeout(struct work_struct
*work
)
220 struct static_key_deferred
*key
=
221 container_of(work
, struct static_key_deferred
, work
.work
);
222 __static_key_slow_dec(&key
->key
, 0, NULL
);
225 void static_key_slow_dec(struct static_key
*key
)
227 STATIC_KEY_CHECK_USE();
228 __static_key_slow_dec(key
, 0, NULL
);
230 EXPORT_SYMBOL_GPL(static_key_slow_dec
);
232 void static_key_slow_dec_cpuslocked(struct static_key
*key
)
234 STATIC_KEY_CHECK_USE();
235 __static_key_slow_dec_cpuslocked(key
, 0, NULL
);
238 void static_key_slow_dec_deferred(struct static_key_deferred
*key
)
240 STATIC_KEY_CHECK_USE();
241 __static_key_slow_dec(&key
->key
, key
->timeout
, &key
->work
);
243 EXPORT_SYMBOL_GPL(static_key_slow_dec_deferred
);
245 void static_key_deferred_flush(struct static_key_deferred
*key
)
247 STATIC_KEY_CHECK_USE();
248 flush_delayed_work(&key
->work
);
250 EXPORT_SYMBOL_GPL(static_key_deferred_flush
);
252 void jump_label_rate_limit(struct static_key_deferred
*key
,
255 STATIC_KEY_CHECK_USE();
257 INIT_DELAYED_WORK(&key
->work
, jump_label_update_timeout
);
259 EXPORT_SYMBOL_GPL(jump_label_rate_limit
);
261 static int addr_conflict(struct jump_entry
*entry
, void *start
, void *end
)
263 if (entry
->code
<= (unsigned long)end
&&
264 entry
->code
+ JUMP_LABEL_NOP_SIZE
> (unsigned long)start
)
270 static int __jump_label_text_reserved(struct jump_entry
*iter_start
,
271 struct jump_entry
*iter_stop
, void *start
, void *end
)
273 struct jump_entry
*iter
;
276 while (iter
< iter_stop
) {
277 if (addr_conflict(iter
, start
, end
))
286 * Update code which is definitely not currently executing.
287 * Architectures which need heavyweight synchronization to modify
288 * running code can override this to make the non-live update case
291 void __weak __init_or_module
arch_jump_label_transform_static(struct jump_entry
*entry
,
292 enum jump_label_type type
)
294 arch_jump_label_transform(entry
, type
);
297 static inline struct jump_entry
*static_key_entries(struct static_key
*key
)
299 WARN_ON_ONCE(key
->type
& JUMP_TYPE_LINKED
);
300 return (struct jump_entry
*)(key
->type
& ~JUMP_TYPE_MASK
);
303 static inline bool static_key_type(struct static_key
*key
)
305 return key
->type
& JUMP_TYPE_TRUE
;
308 static inline bool static_key_linked(struct static_key
*key
)
310 return key
->type
& JUMP_TYPE_LINKED
;
313 static inline void static_key_clear_linked(struct static_key
*key
)
315 key
->type
&= ~JUMP_TYPE_LINKED
;
318 static inline void static_key_set_linked(struct static_key
*key
)
320 key
->type
|= JUMP_TYPE_LINKED
;
323 static inline struct static_key
*jump_entry_key(struct jump_entry
*entry
)
325 return (struct static_key
*)((unsigned long)entry
->key
& ~1UL);
328 static bool jump_entry_branch(struct jump_entry
*entry
)
330 return (unsigned long)entry
->key
& 1UL;
334 * A 'struct static_key' uses a union such that it either points directly
335 * to a table of 'struct jump_entry' or to a linked list of modules which in
336 * turn point to 'struct jump_entry' tables.
338 * The two lower bits of the pointer are used to keep track of which pointer
339 * type is in use and to store the initial branch direction, we use an access
340 * function which preserves these bits.
342 static void static_key_set_entries(struct static_key
*key
,
343 struct jump_entry
*entries
)
347 WARN_ON_ONCE((unsigned long)entries
& JUMP_TYPE_MASK
);
348 type
= key
->type
& JUMP_TYPE_MASK
;
349 key
->entries
= entries
;
353 static enum jump_label_type
jump_label_type(struct jump_entry
*entry
)
355 struct static_key
*key
= jump_entry_key(entry
);
356 bool enabled
= static_key_enabled(key
);
357 bool branch
= jump_entry_branch(entry
);
359 /* See the comment in linux/jump_label.h */
360 return enabled
^ branch
;
363 static void __jump_label_update(struct static_key
*key
,
364 struct jump_entry
*entry
,
365 struct jump_entry
*stop
)
367 for (; (entry
< stop
) && (jump_entry_key(entry
) == key
); entry
++) {
369 * entry->code set to 0 invalidates module init text sections
370 * kernel_text_address() verifies we are not in core kernel
371 * init code, see jump_label_invalidate_module_init().
373 if (entry
->code
&& kernel_text_address(entry
->code
))
374 arch_jump_label_transform(entry
, jump_label_type(entry
));
378 void __init
jump_label_init(void)
380 struct jump_entry
*iter_start
= __start___jump_table
;
381 struct jump_entry
*iter_stop
= __stop___jump_table
;
382 struct static_key
*key
= NULL
;
383 struct jump_entry
*iter
;
386 * Since we are initializing the static_key.enabled field with
387 * with the 'raw' int values (to avoid pulling in atomic.h) in
388 * jump_label.h, let's make sure that is safe. There are only two
389 * cases to check since we initialize to 0 or 1.
391 BUILD_BUG_ON((int)ATOMIC_INIT(0) != 0);
392 BUILD_BUG_ON((int)ATOMIC_INIT(1) != 1);
394 if (static_key_initialized
)
399 jump_label_sort_entries(iter_start
, iter_stop
);
401 for (iter
= iter_start
; iter
< iter_stop
; iter
++) {
402 struct static_key
*iterk
;
405 if (jump_label_type(iter
) == JUMP_LABEL_NOP
)
406 arch_jump_label_transform_static(iter
, JUMP_LABEL_NOP
);
408 iterk
= jump_entry_key(iter
);
413 static_key_set_entries(key
, iter
);
415 static_key_initialized
= true;
420 #ifdef CONFIG_MODULES
422 static enum jump_label_type
jump_label_init_type(struct jump_entry
*entry
)
424 struct static_key
*key
= jump_entry_key(entry
);
425 bool type
= static_key_type(key
);
426 bool branch
= jump_entry_branch(entry
);
428 /* See the comment in linux/jump_label.h */
429 return type
^ branch
;
432 struct static_key_mod
{
433 struct static_key_mod
*next
;
434 struct jump_entry
*entries
;
438 static inline struct static_key_mod
*static_key_mod(struct static_key
*key
)
440 WARN_ON_ONCE(!(key
->type
& JUMP_TYPE_LINKED
));
441 return (struct static_key_mod
*)(key
->type
& ~JUMP_TYPE_MASK
);
445 * key->type and key->next are the same via union.
446 * This sets key->next and preserves the type bits.
448 * See additional comments above static_key_set_entries().
450 static void static_key_set_mod(struct static_key
*key
,
451 struct static_key_mod
*mod
)
455 WARN_ON_ONCE((unsigned long)mod
& JUMP_TYPE_MASK
);
456 type
= key
->type
& JUMP_TYPE_MASK
;
461 static int __jump_label_mod_text_reserved(void *start
, void *end
)
466 mod
= __module_text_address((unsigned long)start
);
467 WARN_ON_ONCE(__module_text_address((unsigned long)end
) != mod
);
474 return __jump_label_text_reserved(mod
->jump_entries
,
475 mod
->jump_entries
+ mod
->num_jump_entries
,
479 static void __jump_label_mod_update(struct static_key
*key
)
481 struct static_key_mod
*mod
;
483 for (mod
= static_key_mod(key
); mod
; mod
= mod
->next
) {
484 struct jump_entry
*stop
;
488 * NULL if the static_key is defined in a module
489 * that does not use it
496 stop
= __stop___jump_table
;
498 stop
= m
->jump_entries
+ m
->num_jump_entries
;
499 __jump_label_update(key
, mod
->entries
, stop
);
504 * apply_jump_label_nops - patch module jump labels with arch_get_jump_label_nop()
505 * @mod: module to patch
507 * Allow for run-time selection of the optimal nops. Before the module
508 * loads patch these with arch_get_jump_label_nop(), which is specified by
509 * the arch specific jump label code.
511 void jump_label_apply_nops(struct module
*mod
)
513 struct jump_entry
*iter_start
= mod
->jump_entries
;
514 struct jump_entry
*iter_stop
= iter_start
+ mod
->num_jump_entries
;
515 struct jump_entry
*iter
;
517 /* if the module doesn't have jump label entries, just return */
518 if (iter_start
== iter_stop
)
521 for (iter
= iter_start
; iter
< iter_stop
; iter
++) {
522 /* Only write NOPs for arch_branch_static(). */
523 if (jump_label_init_type(iter
) == JUMP_LABEL_NOP
)
524 arch_jump_label_transform_static(iter
, JUMP_LABEL_NOP
);
528 static int jump_label_add_module(struct module
*mod
)
530 struct jump_entry
*iter_start
= mod
->jump_entries
;
531 struct jump_entry
*iter_stop
= iter_start
+ mod
->num_jump_entries
;
532 struct jump_entry
*iter
;
533 struct static_key
*key
= NULL
;
534 struct static_key_mod
*jlm
, *jlm2
;
536 /* if the module doesn't have jump label entries, just return */
537 if (iter_start
== iter_stop
)
540 jump_label_sort_entries(iter_start
, iter_stop
);
542 for (iter
= iter_start
; iter
< iter_stop
; iter
++) {
543 struct static_key
*iterk
;
545 iterk
= jump_entry_key(iter
);
550 if (within_module(iter
->key
, mod
)) {
551 static_key_set_entries(key
, iter
);
554 jlm
= kzalloc(sizeof(struct static_key_mod
), GFP_KERNEL
);
557 if (!static_key_linked(key
)) {
558 jlm2
= kzalloc(sizeof(struct static_key_mod
),
565 jlm2
->mod
= __module_address((unsigned long)key
);
567 jlm2
->entries
= static_key_entries(key
);
569 static_key_set_mod(key
, jlm2
);
570 static_key_set_linked(key
);
574 jlm
->next
= static_key_mod(key
);
575 static_key_set_mod(key
, jlm
);
576 static_key_set_linked(key
);
578 /* Only update if we've changed from our initial state */
579 if (jump_label_type(iter
) != jump_label_init_type(iter
))
580 __jump_label_update(key
, iter
, iter_stop
);
586 static void jump_label_del_module(struct module
*mod
)
588 struct jump_entry
*iter_start
= mod
->jump_entries
;
589 struct jump_entry
*iter_stop
= iter_start
+ mod
->num_jump_entries
;
590 struct jump_entry
*iter
;
591 struct static_key
*key
= NULL
;
592 struct static_key_mod
*jlm
, **prev
;
594 for (iter
= iter_start
; iter
< iter_stop
; iter
++) {
595 if (jump_entry_key(iter
) == key
)
598 key
= jump_entry_key(iter
);
600 if (within_module(iter
->key
, mod
))
603 /* No memory during module load */
604 if (WARN_ON(!static_key_linked(key
)))
608 jlm
= static_key_mod(key
);
610 while (jlm
&& jlm
->mod
!= mod
) {
615 /* No memory during module load */
619 if (prev
== &key
->next
)
620 static_key_set_mod(key
, jlm
->next
);
626 jlm
= static_key_mod(key
);
627 /* if only one etry is left, fold it back into the static_key */
628 if (jlm
->next
== NULL
) {
629 static_key_set_entries(key
, jlm
->entries
);
630 static_key_clear_linked(key
);
636 static void jump_label_invalidate_module_init(struct module
*mod
)
638 struct jump_entry
*iter_start
= mod
->jump_entries
;
639 struct jump_entry
*iter_stop
= iter_start
+ mod
->num_jump_entries
;
640 struct jump_entry
*iter
;
642 for (iter
= iter_start
; iter
< iter_stop
; iter
++) {
643 if (within_module_init(iter
->code
, mod
))
649 jump_label_module_notify(struct notifier_block
*self
, unsigned long val
,
652 struct module
*mod
= data
;
659 case MODULE_STATE_COMING
:
660 ret
= jump_label_add_module(mod
);
662 WARN(1, "Failed to allocatote memory: jump_label may not work properly.\n");
663 jump_label_del_module(mod
);
666 case MODULE_STATE_GOING
:
667 jump_label_del_module(mod
);
669 case MODULE_STATE_LIVE
:
670 jump_label_invalidate_module_init(mod
);
677 return notifier_from_errno(ret
);
680 static struct notifier_block jump_label_module_nb
= {
681 .notifier_call
= jump_label_module_notify
,
682 .priority
= 1, /* higher than tracepoints */
685 static __init
int jump_label_init_module(void)
687 return register_module_notifier(&jump_label_module_nb
);
689 early_initcall(jump_label_init_module
);
691 #endif /* CONFIG_MODULES */
694 * jump_label_text_reserved - check if addr range is reserved
695 * @start: start text addr
696 * @end: end text addr
698 * checks if the text addr located between @start and @end
699 * overlaps with any of the jump label patch addresses. Code
700 * that wants to modify kernel text should first verify that
701 * it does not overlap with any of the jump label addresses.
702 * Caller must hold jump_label_mutex.
704 * returns 1 if there is an overlap, 0 otherwise
706 int jump_label_text_reserved(void *start
, void *end
)
708 int ret
= __jump_label_text_reserved(__start___jump_table
,
709 __stop___jump_table
, start
, end
);
714 #ifdef CONFIG_MODULES
715 ret
= __jump_label_mod_text_reserved(start
, end
);
720 static void jump_label_update(struct static_key
*key
)
722 struct jump_entry
*stop
= __stop___jump_table
;
723 struct jump_entry
*entry
;
724 #ifdef CONFIG_MODULES
727 if (static_key_linked(key
)) {
728 __jump_label_mod_update(key
);
733 mod
= __module_address((unsigned long)key
);
735 stop
= mod
->jump_entries
+ mod
->num_jump_entries
;
738 entry
= static_key_entries(key
);
739 /* if there are no users, entry can be NULL */
741 __jump_label_update(key
, entry
, stop
);
744 #ifdef CONFIG_STATIC_KEYS_SELFTEST
745 static DEFINE_STATIC_KEY_TRUE(sk_true
);
746 static DEFINE_STATIC_KEY_FALSE(sk_false
);
748 static __init
int jump_label_test(void)
752 for (i
= 0; i
< 2; i
++) {
753 WARN_ON(static_key_enabled(&sk_true
.key
) != true);
754 WARN_ON(static_key_enabled(&sk_false
.key
) != false);
756 WARN_ON(!static_branch_likely(&sk_true
));
757 WARN_ON(!static_branch_unlikely(&sk_true
));
758 WARN_ON(static_branch_likely(&sk_false
));
759 WARN_ON(static_branch_unlikely(&sk_false
));
761 static_branch_disable(&sk_true
);
762 static_branch_enable(&sk_false
);
764 WARN_ON(static_key_enabled(&sk_true
.key
) == true);
765 WARN_ON(static_key_enabled(&sk_false
.key
) == false);
767 WARN_ON(static_branch_likely(&sk_true
));
768 WARN_ON(static_branch_unlikely(&sk_true
));
769 WARN_ON(!static_branch_likely(&sk_false
));
770 WARN_ON(!static_branch_unlikely(&sk_false
));
772 static_branch_enable(&sk_true
);
773 static_branch_disable(&sk_false
);
778 early_initcall(jump_label_test
);
779 #endif /* STATIC_KEYS_SELFTEST */
781 #endif /* HAVE_JUMP_LABEL */