cgroup: remove unnecessary empty check when enabling threaded mode
[linux/fpc-iii.git] / kernel / jump_label.c
blobd11c506a6ac3db63e9475ee45d3c4bd34aa47a73
1 /*
2 * jump label support
4 * Copyright (C) 2009 Jason Baron <jbaron@redhat.com>
5 * Copyright (C) 2011 Peter Zijlstra
7 */
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)
41 return -1;
43 if (jea->key > jeb->key)
44 return 1;
46 return 0;
49 static void
50 jump_label_sort_entries(struct jump_entry *start, struct jump_entry *stop)
52 unsigned long size;
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_enable(struct static_key *key)
84 int count = static_key_count(key);
86 WARN_ON_ONCE(count < 0 || count > 1);
88 if (!count)
89 static_key_slow_inc(key);
91 EXPORT_SYMBOL_GPL(static_key_enable);
93 void static_key_disable(struct static_key *key)
95 int count = static_key_count(key);
97 WARN_ON_ONCE(count < 0 || count > 1);
99 if (count)
100 static_key_slow_dec(key);
102 EXPORT_SYMBOL_GPL(static_key_disable);
104 void static_key_slow_inc(struct static_key *key)
106 int v, v1;
108 STATIC_KEY_CHECK_USE();
111 * Careful if we get concurrent static_key_slow_inc() calls;
112 * later calls must wait for the first one to _finish_ the
113 * jump_label_update() process. At the same time, however,
114 * the jump_label_update() call below wants to see
115 * static_key_enabled(&key) for jumps to be updated properly.
117 * So give a special meaning to negative key->enabled: it sends
118 * static_key_slow_inc() down the slow path, and it is non-zero
119 * so it counts as "enabled" in jump_label_update(). Note that
120 * atomic_inc_unless_negative() checks >= 0, so roll our own.
122 for (v = atomic_read(&key->enabled); v > 0; v = v1) {
123 v1 = atomic_cmpxchg(&key->enabled, v, v + 1);
124 if (likely(v1 == v))
125 return;
128 cpus_read_lock();
129 jump_label_lock();
130 if (atomic_read(&key->enabled) == 0) {
131 atomic_set(&key->enabled, -1);
132 jump_label_update(key);
133 atomic_set(&key->enabled, 1);
134 } else {
135 atomic_inc(&key->enabled);
137 jump_label_unlock();
138 cpus_read_unlock();
140 EXPORT_SYMBOL_GPL(static_key_slow_inc);
142 static void __static_key_slow_dec(struct static_key *key,
143 unsigned long rate_limit, struct delayed_work *work)
145 cpus_read_lock();
147 * The negative count check is valid even when a negative
148 * key->enabled is in use by static_key_slow_inc(); a
149 * __static_key_slow_dec() before the first static_key_slow_inc()
150 * returns is unbalanced, because all other static_key_slow_inc()
151 * instances block while the update is in progress.
153 if (!atomic_dec_and_mutex_lock(&key->enabled, &jump_label_mutex)) {
154 WARN(atomic_read(&key->enabled) < 0,
155 "jump label: negative count!\n");
156 cpus_read_unlock();
157 return;
160 if (rate_limit) {
161 atomic_inc(&key->enabled);
162 schedule_delayed_work(work, rate_limit);
163 } else {
164 jump_label_update(key);
166 jump_label_unlock();
167 cpus_read_unlock();
170 static void jump_label_update_timeout(struct work_struct *work)
172 struct static_key_deferred *key =
173 container_of(work, struct static_key_deferred, work.work);
174 __static_key_slow_dec(&key->key, 0, NULL);
177 void static_key_slow_dec(struct static_key *key)
179 STATIC_KEY_CHECK_USE();
180 __static_key_slow_dec(key, 0, NULL);
182 EXPORT_SYMBOL_GPL(static_key_slow_dec);
184 void static_key_slow_dec_deferred(struct static_key_deferred *key)
186 STATIC_KEY_CHECK_USE();
187 __static_key_slow_dec(&key->key, key->timeout, &key->work);
189 EXPORT_SYMBOL_GPL(static_key_slow_dec_deferred);
191 void static_key_deferred_flush(struct static_key_deferred *key)
193 STATIC_KEY_CHECK_USE();
194 flush_delayed_work(&key->work);
196 EXPORT_SYMBOL_GPL(static_key_deferred_flush);
198 void jump_label_rate_limit(struct static_key_deferred *key,
199 unsigned long rl)
201 STATIC_KEY_CHECK_USE();
202 key->timeout = rl;
203 INIT_DELAYED_WORK(&key->work, jump_label_update_timeout);
205 EXPORT_SYMBOL_GPL(jump_label_rate_limit);
207 static int addr_conflict(struct jump_entry *entry, void *start, void *end)
209 if (entry->code <= (unsigned long)end &&
210 entry->code + JUMP_LABEL_NOP_SIZE > (unsigned long)start)
211 return 1;
213 return 0;
216 static int __jump_label_text_reserved(struct jump_entry *iter_start,
217 struct jump_entry *iter_stop, void *start, void *end)
219 struct jump_entry *iter;
221 iter = iter_start;
222 while (iter < iter_stop) {
223 if (addr_conflict(iter, start, end))
224 return 1;
225 iter++;
228 return 0;
232 * Update code which is definitely not currently executing.
233 * Architectures which need heavyweight synchronization to modify
234 * running code can override this to make the non-live update case
235 * cheaper.
237 void __weak __init_or_module arch_jump_label_transform_static(struct jump_entry *entry,
238 enum jump_label_type type)
240 arch_jump_label_transform(entry, type);
243 static inline struct jump_entry *static_key_entries(struct static_key *key)
245 WARN_ON_ONCE(key->type & JUMP_TYPE_LINKED);
246 return (struct jump_entry *)(key->type & ~JUMP_TYPE_MASK);
249 static inline bool static_key_type(struct static_key *key)
251 return key->type & JUMP_TYPE_TRUE;
254 static inline bool static_key_linked(struct static_key *key)
256 return key->type & JUMP_TYPE_LINKED;
259 static inline void static_key_clear_linked(struct static_key *key)
261 key->type &= ~JUMP_TYPE_LINKED;
264 static inline void static_key_set_linked(struct static_key *key)
266 key->type |= JUMP_TYPE_LINKED;
269 static inline struct static_key *jump_entry_key(struct jump_entry *entry)
271 return (struct static_key *)((unsigned long)entry->key & ~1UL);
274 static bool jump_entry_branch(struct jump_entry *entry)
276 return (unsigned long)entry->key & 1UL;
279 /***
280 * A 'struct static_key' uses a union such that it either points directly
281 * to a table of 'struct jump_entry' or to a linked list of modules which in
282 * turn point to 'struct jump_entry' tables.
284 * The two lower bits of the pointer are used to keep track of which pointer
285 * type is in use and to store the initial branch direction, we use an access
286 * function which preserves these bits.
288 static void static_key_set_entries(struct static_key *key,
289 struct jump_entry *entries)
291 unsigned long type;
293 WARN_ON_ONCE((unsigned long)entries & JUMP_TYPE_MASK);
294 type = key->type & JUMP_TYPE_MASK;
295 key->entries = entries;
296 key->type |= type;
299 static enum jump_label_type jump_label_type(struct jump_entry *entry)
301 struct static_key *key = jump_entry_key(entry);
302 bool enabled = static_key_enabled(key);
303 bool branch = jump_entry_branch(entry);
305 /* See the comment in linux/jump_label.h */
306 return enabled ^ branch;
309 static void __jump_label_update(struct static_key *key,
310 struct jump_entry *entry,
311 struct jump_entry *stop)
313 for (; (entry < stop) && (jump_entry_key(entry) == key); entry++) {
315 * entry->code set to 0 invalidates module init text sections
316 * kernel_text_address() verifies we are not in core kernel
317 * init code, see jump_label_invalidate_module_init().
319 if (entry->code && kernel_text_address(entry->code))
320 arch_jump_label_transform(entry, jump_label_type(entry));
324 void __init jump_label_init(void)
326 struct jump_entry *iter_start = __start___jump_table;
327 struct jump_entry *iter_stop = __stop___jump_table;
328 struct static_key *key = NULL;
329 struct jump_entry *iter;
332 * Since we are initializing the static_key.enabled field with
333 * with the 'raw' int values (to avoid pulling in atomic.h) in
334 * jump_label.h, let's make sure that is safe. There are only two
335 * cases to check since we initialize to 0 or 1.
337 BUILD_BUG_ON((int)ATOMIC_INIT(0) != 0);
338 BUILD_BUG_ON((int)ATOMIC_INIT(1) != 1);
340 if (static_key_initialized)
341 return;
343 cpus_read_lock();
344 jump_label_lock();
345 jump_label_sort_entries(iter_start, iter_stop);
347 for (iter = iter_start; iter < iter_stop; iter++) {
348 struct static_key *iterk;
350 /* rewrite NOPs */
351 if (jump_label_type(iter) == JUMP_LABEL_NOP)
352 arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
354 iterk = jump_entry_key(iter);
355 if (iterk == key)
356 continue;
358 key = iterk;
359 static_key_set_entries(key, iter);
361 static_key_initialized = true;
362 jump_label_unlock();
363 cpus_read_unlock();
366 #ifdef CONFIG_MODULES
368 static enum jump_label_type jump_label_init_type(struct jump_entry *entry)
370 struct static_key *key = jump_entry_key(entry);
371 bool type = static_key_type(key);
372 bool branch = jump_entry_branch(entry);
374 /* See the comment in linux/jump_label.h */
375 return type ^ branch;
378 struct static_key_mod {
379 struct static_key_mod *next;
380 struct jump_entry *entries;
381 struct module *mod;
384 static inline struct static_key_mod *static_key_mod(struct static_key *key)
386 WARN_ON_ONCE(!(key->type & JUMP_TYPE_LINKED));
387 return (struct static_key_mod *)(key->type & ~JUMP_TYPE_MASK);
390 /***
391 * key->type and key->next are the same via union.
392 * This sets key->next and preserves the type bits.
394 * See additional comments above static_key_set_entries().
396 static void static_key_set_mod(struct static_key *key,
397 struct static_key_mod *mod)
399 unsigned long type;
401 WARN_ON_ONCE((unsigned long)mod & JUMP_TYPE_MASK);
402 type = key->type & JUMP_TYPE_MASK;
403 key->next = mod;
404 key->type |= type;
407 static int __jump_label_mod_text_reserved(void *start, void *end)
409 struct module *mod;
411 preempt_disable();
412 mod = __module_text_address((unsigned long)start);
413 WARN_ON_ONCE(__module_text_address((unsigned long)end) != mod);
414 preempt_enable();
416 if (!mod)
417 return 0;
420 return __jump_label_text_reserved(mod->jump_entries,
421 mod->jump_entries + mod->num_jump_entries,
422 start, end);
425 static void __jump_label_mod_update(struct static_key *key)
427 struct static_key_mod *mod;
429 for (mod = static_key_mod(key); mod; mod = mod->next) {
430 struct jump_entry *stop;
431 struct module *m;
434 * NULL if the static_key is defined in a module
435 * that does not use it
437 if (!mod->entries)
438 continue;
440 m = mod->mod;
441 if (!m)
442 stop = __stop___jump_table;
443 else
444 stop = m->jump_entries + m->num_jump_entries;
445 __jump_label_update(key, mod->entries, stop);
449 /***
450 * apply_jump_label_nops - patch module jump labels with arch_get_jump_label_nop()
451 * @mod: module to patch
453 * Allow for run-time selection of the optimal nops. Before the module
454 * loads patch these with arch_get_jump_label_nop(), which is specified by
455 * the arch specific jump label code.
457 void jump_label_apply_nops(struct module *mod)
459 struct jump_entry *iter_start = mod->jump_entries;
460 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
461 struct jump_entry *iter;
463 /* if the module doesn't have jump label entries, just return */
464 if (iter_start == iter_stop)
465 return;
467 for (iter = iter_start; iter < iter_stop; iter++) {
468 /* Only write NOPs for arch_branch_static(). */
469 if (jump_label_init_type(iter) == JUMP_LABEL_NOP)
470 arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
474 static int jump_label_add_module(struct module *mod)
476 struct jump_entry *iter_start = mod->jump_entries;
477 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
478 struct jump_entry *iter;
479 struct static_key *key = NULL;
480 struct static_key_mod *jlm, *jlm2;
482 /* if the module doesn't have jump label entries, just return */
483 if (iter_start == iter_stop)
484 return 0;
486 jump_label_sort_entries(iter_start, iter_stop);
488 for (iter = iter_start; iter < iter_stop; iter++) {
489 struct static_key *iterk;
491 iterk = jump_entry_key(iter);
492 if (iterk == key)
493 continue;
495 key = iterk;
496 if (within_module(iter->key, mod)) {
497 static_key_set_entries(key, iter);
498 continue;
500 jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL);
501 if (!jlm)
502 return -ENOMEM;
503 if (!static_key_linked(key)) {
504 jlm2 = kzalloc(sizeof(struct static_key_mod),
505 GFP_KERNEL);
506 if (!jlm2) {
507 kfree(jlm);
508 return -ENOMEM;
510 preempt_disable();
511 jlm2->mod = __module_address((unsigned long)key);
512 preempt_enable();
513 jlm2->entries = static_key_entries(key);
514 jlm2->next = NULL;
515 static_key_set_mod(key, jlm2);
516 static_key_set_linked(key);
518 jlm->mod = mod;
519 jlm->entries = iter;
520 jlm->next = static_key_mod(key);
521 static_key_set_mod(key, jlm);
522 static_key_set_linked(key);
524 /* Only update if we've changed from our initial state */
525 if (jump_label_type(iter) != jump_label_init_type(iter))
526 __jump_label_update(key, iter, iter_stop);
529 return 0;
532 static void jump_label_del_module(struct module *mod)
534 struct jump_entry *iter_start = mod->jump_entries;
535 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
536 struct jump_entry *iter;
537 struct static_key *key = NULL;
538 struct static_key_mod *jlm, **prev;
540 for (iter = iter_start; iter < iter_stop; iter++) {
541 if (jump_entry_key(iter) == key)
542 continue;
544 key = jump_entry_key(iter);
546 if (within_module(iter->key, mod))
547 continue;
549 /* No memory during module load */
550 if (WARN_ON(!static_key_linked(key)))
551 continue;
553 prev = &key->next;
554 jlm = static_key_mod(key);
556 while (jlm && jlm->mod != mod) {
557 prev = &jlm->next;
558 jlm = jlm->next;
561 /* No memory during module load */
562 if (WARN_ON(!jlm))
563 continue;
565 if (prev == &key->next)
566 static_key_set_mod(key, jlm->next);
567 else
568 *prev = jlm->next;
570 kfree(jlm);
572 jlm = static_key_mod(key);
573 /* if only one etry is left, fold it back into the static_key */
574 if (jlm->next == NULL) {
575 static_key_set_entries(key, jlm->entries);
576 static_key_clear_linked(key);
577 kfree(jlm);
582 static void jump_label_invalidate_module_init(struct module *mod)
584 struct jump_entry *iter_start = mod->jump_entries;
585 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
586 struct jump_entry *iter;
588 for (iter = iter_start; iter < iter_stop; iter++) {
589 if (within_module_init(iter->code, mod))
590 iter->code = 0;
594 static int
595 jump_label_module_notify(struct notifier_block *self, unsigned long val,
596 void *data)
598 struct module *mod = data;
599 int ret = 0;
601 cpus_read_lock();
602 jump_label_lock();
604 switch (val) {
605 case MODULE_STATE_COMING:
606 ret = jump_label_add_module(mod);
607 if (ret) {
608 WARN(1, "Failed to allocatote memory: jump_label may not work properly.\n");
609 jump_label_del_module(mod);
611 break;
612 case MODULE_STATE_GOING:
613 jump_label_del_module(mod);
614 break;
615 case MODULE_STATE_LIVE:
616 jump_label_invalidate_module_init(mod);
617 break;
620 jump_label_unlock();
621 cpus_read_unlock();
623 return notifier_from_errno(ret);
626 static struct notifier_block jump_label_module_nb = {
627 .notifier_call = jump_label_module_notify,
628 .priority = 1, /* higher than tracepoints */
631 static __init int jump_label_init_module(void)
633 return register_module_notifier(&jump_label_module_nb);
635 early_initcall(jump_label_init_module);
637 #endif /* CONFIG_MODULES */
639 /***
640 * jump_label_text_reserved - check if addr range is reserved
641 * @start: start text addr
642 * @end: end text addr
644 * checks if the text addr located between @start and @end
645 * overlaps with any of the jump label patch addresses. Code
646 * that wants to modify kernel text should first verify that
647 * it does not overlap with any of the jump label addresses.
648 * Caller must hold jump_label_mutex.
650 * returns 1 if there is an overlap, 0 otherwise
652 int jump_label_text_reserved(void *start, void *end)
654 int ret = __jump_label_text_reserved(__start___jump_table,
655 __stop___jump_table, start, end);
657 if (ret)
658 return ret;
660 #ifdef CONFIG_MODULES
661 ret = __jump_label_mod_text_reserved(start, end);
662 #endif
663 return ret;
666 static void jump_label_update(struct static_key *key)
668 struct jump_entry *stop = __stop___jump_table;
669 struct jump_entry *entry;
670 #ifdef CONFIG_MODULES
671 struct module *mod;
673 if (static_key_linked(key)) {
674 __jump_label_mod_update(key);
675 return;
678 preempt_disable();
679 mod = __module_address((unsigned long)key);
680 if (mod)
681 stop = mod->jump_entries + mod->num_jump_entries;
682 preempt_enable();
683 #endif
684 entry = static_key_entries(key);
685 /* if there are no users, entry can be NULL */
686 if (entry)
687 __jump_label_update(key, entry, stop);
690 #ifdef CONFIG_STATIC_KEYS_SELFTEST
691 static DEFINE_STATIC_KEY_TRUE(sk_true);
692 static DEFINE_STATIC_KEY_FALSE(sk_false);
694 static __init int jump_label_test(void)
696 int i;
698 for (i = 0; i < 2; i++) {
699 WARN_ON(static_key_enabled(&sk_true.key) != true);
700 WARN_ON(static_key_enabled(&sk_false.key) != false);
702 WARN_ON(!static_branch_likely(&sk_true));
703 WARN_ON(!static_branch_unlikely(&sk_true));
704 WARN_ON(static_branch_likely(&sk_false));
705 WARN_ON(static_branch_unlikely(&sk_false));
707 static_branch_disable(&sk_true);
708 static_branch_enable(&sk_false);
710 WARN_ON(static_key_enabled(&sk_true.key) == true);
711 WARN_ON(static_key_enabled(&sk_false.key) == false);
713 WARN_ON(static_branch_likely(&sk_true));
714 WARN_ON(static_branch_unlikely(&sk_true));
715 WARN_ON(!static_branch_likely(&sk_false));
716 WARN_ON(!static_branch_unlikely(&sk_false));
718 static_branch_enable(&sk_true);
719 static_branch_disable(&sk_false);
722 return 0;
724 late_initcall(jump_label_test);
725 #endif /* STATIC_KEYS_SELFTEST */
727 #endif /* HAVE_JUMP_LABEL */