genirq/debugfs: Reinstate full OF path for domain name
[platform/kernel/linux-rpi.git] / kernel / jump_label.c
1 /*
2  * jump label support
3  *
4  * Copyright (C) 2009 Jason Baron <jbaron@redhat.com>
5  * Copyright (C) 2011 Peter Zijlstra
6  *
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>
19 #include <asm/sections.h>
20
21 /* mutex to protect coming/going of the the jump_label table */
22 static DEFINE_MUTEX(jump_label_mutex);
23
24 void jump_label_lock(void)
25 {
26         mutex_lock(&jump_label_mutex);
27 }
28
29 void jump_label_unlock(void)
30 {
31         mutex_unlock(&jump_label_mutex);
32 }
33
34 static int jump_label_cmp(const void *a, const void *b)
35 {
36         const struct jump_entry *jea = a;
37         const struct jump_entry *jeb = b;
38
39         if (jea->key < jeb->key)
40                 return -1;
41
42         if (jea->key > jeb->key)
43                 return 1;
44
45         return 0;
46 }
47
48 static void
49 jump_label_sort_entries(struct jump_entry *start, struct jump_entry *stop)
50 {
51         unsigned long size;
52
53         size = (((unsigned long)stop - (unsigned long)start)
54                                         / sizeof(struct jump_entry));
55         sort(start, size, sizeof(struct jump_entry), jump_label_cmp, NULL);
56 }
57
58 static void jump_label_update(struct static_key *key);
59
60 /*
61  * There are similar definitions for the !CONFIG_JUMP_LABEL case in jump_label.h.
62  * The use of 'atomic_read()' requires atomic.h and its problematic for some
63  * kernel headers such as kernel.h and others. Since static_key_count() is not
64  * used in the branch statements as it is for the !CONFIG_JUMP_LABEL case its ok
65  * to have it be a function here. Similarly, for 'static_key_enable()' and
66  * 'static_key_disable()', which require bug.h. This should allow jump_label.h
67  * to be included from most/all places for CONFIG_JUMP_LABEL.
68  */
69 int static_key_count(struct static_key *key)
70 {
71         /*
72          * -1 means the first static_key_slow_inc() is in progress.
73          *  static_key_enabled() must return true, so return 1 here.
74          */
75         int n = atomic_read(&key->enabled);
76
77         return n >= 0 ? n : 1;
78 }
79 EXPORT_SYMBOL_GPL(static_key_count);
80
81 void static_key_slow_inc_cpuslocked(struct static_key *key)
82 {
83         int v, v1;
84
85         STATIC_KEY_CHECK_USE(key);
86
87         /*
88          * Careful if we get concurrent static_key_slow_inc() calls;
89          * later calls must wait for the first one to _finish_ the
90          * jump_label_update() process.  At the same time, however,
91          * the jump_label_update() call below wants to see
92          * static_key_enabled(&key) for jumps to be updated properly.
93          *
94          * So give a special meaning to negative key->enabled: it sends
95          * static_key_slow_inc() down the slow path, and it is non-zero
96          * so it counts as "enabled" in jump_label_update().  Note that
97          * atomic_inc_unless_negative() checks >= 0, so roll our own.
98          */
99         for (v = atomic_read(&key->enabled); v > 0; v = v1) {
100                 v1 = atomic_cmpxchg(&key->enabled, v, v + 1);
101                 if (likely(v1 == v))
102                         return;
103         }
104
105         jump_label_lock();
106         if (atomic_read(&key->enabled) == 0) {
107                 atomic_set(&key->enabled, -1);
108                 jump_label_update(key);
109                 /*
110                  * Ensure that if the above cmpxchg loop observes our positive
111                  * value, it must also observe all the text changes.
112                  */
113                 atomic_set_release(&key->enabled, 1);
114         } else {
115                 atomic_inc(&key->enabled);
116         }
117         jump_label_unlock();
118 }
119
120 void static_key_slow_inc(struct static_key *key)
121 {
122         cpus_read_lock();
123         static_key_slow_inc_cpuslocked(key);
124         cpus_read_unlock();
125 }
126 EXPORT_SYMBOL_GPL(static_key_slow_inc);
127
128 void static_key_enable_cpuslocked(struct static_key *key)
129 {
130         STATIC_KEY_CHECK_USE(key);
131
132         if (atomic_read(&key->enabled) > 0) {
133                 WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
134                 return;
135         }
136
137         jump_label_lock();
138         if (atomic_read(&key->enabled) == 0) {
139                 atomic_set(&key->enabled, -1);
140                 jump_label_update(key);
141                 /*
142                  * See static_key_slow_inc().
143                  */
144                 atomic_set_release(&key->enabled, 1);
145         }
146         jump_label_unlock();
147 }
148 EXPORT_SYMBOL_GPL(static_key_enable_cpuslocked);
149
150 void static_key_enable(struct static_key *key)
151 {
152         cpus_read_lock();
153         static_key_enable_cpuslocked(key);
154         cpus_read_unlock();
155 }
156 EXPORT_SYMBOL_GPL(static_key_enable);
157
158 void static_key_disable_cpuslocked(struct static_key *key)
159 {
160         STATIC_KEY_CHECK_USE(key);
161
162         if (atomic_read(&key->enabled) != 1) {
163                 WARN_ON_ONCE(atomic_read(&key->enabled) != 0);
164                 return;
165         }
166
167         jump_label_lock();
168         if (atomic_cmpxchg(&key->enabled, 1, 0))
169                 jump_label_update(key);
170         jump_label_unlock();
171 }
172 EXPORT_SYMBOL_GPL(static_key_disable_cpuslocked);
173
174 void static_key_disable(struct static_key *key)
175 {
176         cpus_read_lock();
177         static_key_disable_cpuslocked(key);
178         cpus_read_unlock();
179 }
180 EXPORT_SYMBOL_GPL(static_key_disable);
181
182 static void __static_key_slow_dec_cpuslocked(struct static_key *key,
183                                            unsigned long rate_limit,
184                                            struct delayed_work *work)
185 {
186         /*
187          * The negative count check is valid even when a negative
188          * key->enabled is in use by static_key_slow_inc(); a
189          * __static_key_slow_dec() before the first static_key_slow_inc()
190          * returns is unbalanced, because all other static_key_slow_inc()
191          * instances block while the update is in progress.
192          */
193         if (!atomic_dec_and_mutex_lock(&key->enabled, &jump_label_mutex)) {
194                 WARN(atomic_read(&key->enabled) < 0,
195                      "jump label: negative count!\n");
196                 return;
197         }
198
199         if (rate_limit) {
200                 atomic_inc(&key->enabled);
201                 schedule_delayed_work(work, rate_limit);
202         } else {
203                 jump_label_update(key);
204         }
205         jump_label_unlock();
206 }
207
208 static void __static_key_slow_dec(struct static_key *key,
209                                   unsigned long rate_limit,
210                                   struct delayed_work *work)
211 {
212         cpus_read_lock();
213         __static_key_slow_dec_cpuslocked(key, rate_limit, work);
214         cpus_read_unlock();
215 }
216
217 static void jump_label_update_timeout(struct work_struct *work)
218 {
219         struct static_key_deferred *key =
220                 container_of(work, struct static_key_deferred, work.work);
221         __static_key_slow_dec(&key->key, 0, NULL);
222 }
223
224 void static_key_slow_dec(struct static_key *key)
225 {
226         STATIC_KEY_CHECK_USE(key);
227         __static_key_slow_dec(key, 0, NULL);
228 }
229 EXPORT_SYMBOL_GPL(static_key_slow_dec);
230
231 void static_key_slow_dec_cpuslocked(struct static_key *key)
232 {
233         STATIC_KEY_CHECK_USE(key);
234         __static_key_slow_dec_cpuslocked(key, 0, NULL);
235 }
236
237 void static_key_slow_dec_deferred(struct static_key_deferred *key)
238 {
239         STATIC_KEY_CHECK_USE(key);
240         __static_key_slow_dec(&key->key, key->timeout, &key->work);
241 }
242 EXPORT_SYMBOL_GPL(static_key_slow_dec_deferred);
243
244 void static_key_deferred_flush(struct static_key_deferred *key)
245 {
246         STATIC_KEY_CHECK_USE(key);
247         flush_delayed_work(&key->work);
248 }
249 EXPORT_SYMBOL_GPL(static_key_deferred_flush);
250
251 void jump_label_rate_limit(struct static_key_deferred *key,
252                 unsigned long rl)
253 {
254         STATIC_KEY_CHECK_USE(key);
255         key->timeout = rl;
256         INIT_DELAYED_WORK(&key->work, jump_label_update_timeout);
257 }
258 EXPORT_SYMBOL_GPL(jump_label_rate_limit);
259
260 static int addr_conflict(struct jump_entry *entry, void *start, void *end)
261 {
262         if (entry->code <= (unsigned long)end &&
263                 entry->code + JUMP_LABEL_NOP_SIZE > (unsigned long)start)
264                 return 1;
265
266         return 0;
267 }
268
269 static int __jump_label_text_reserved(struct jump_entry *iter_start,
270                 struct jump_entry *iter_stop, void *start, void *end)
271 {
272         struct jump_entry *iter;
273
274         iter = iter_start;
275         while (iter < iter_stop) {
276                 if (addr_conflict(iter, start, end))
277                         return 1;
278                 iter++;
279         }
280
281         return 0;
282 }
283
284 /*
285  * Update code which is definitely not currently executing.
286  * Architectures which need heavyweight synchronization to modify
287  * running code can override this to make the non-live update case
288  * cheaper.
289  */
290 void __weak __init_or_module arch_jump_label_transform_static(struct jump_entry *entry,
291                                             enum jump_label_type type)
292 {
293         arch_jump_label_transform(entry, type);
294 }
295
296 static inline struct jump_entry *static_key_entries(struct static_key *key)
297 {
298         WARN_ON_ONCE(key->type & JUMP_TYPE_LINKED);
299         return (struct jump_entry *)(key->type & ~JUMP_TYPE_MASK);
300 }
301
302 static inline bool static_key_type(struct static_key *key)
303 {
304         return key->type & JUMP_TYPE_TRUE;
305 }
306
307 static inline bool static_key_linked(struct static_key *key)
308 {
309         return key->type & JUMP_TYPE_LINKED;
310 }
311
312 static inline void static_key_clear_linked(struct static_key *key)
313 {
314         key->type &= ~JUMP_TYPE_LINKED;
315 }
316
317 static inline void static_key_set_linked(struct static_key *key)
318 {
319         key->type |= JUMP_TYPE_LINKED;
320 }
321
322 static inline struct static_key *jump_entry_key(struct jump_entry *entry)
323 {
324         return (struct static_key *)((unsigned long)entry->key & ~1UL);
325 }
326
327 static bool jump_entry_branch(struct jump_entry *entry)
328 {
329         return (unsigned long)entry->key & 1UL;
330 }
331
332 /***
333  * A 'struct static_key' uses a union such that it either points directly
334  * to a table of 'struct jump_entry' or to a linked list of modules which in
335  * turn point to 'struct jump_entry' tables.
336  *
337  * The two lower bits of the pointer are used to keep track of which pointer
338  * type is in use and to store the initial branch direction, we use an access
339  * function which preserves these bits.
340  */
341 static void static_key_set_entries(struct static_key *key,
342                                    struct jump_entry *entries)
343 {
344         unsigned long type;
345
346         WARN_ON_ONCE((unsigned long)entries & JUMP_TYPE_MASK);
347         type = key->type & JUMP_TYPE_MASK;
348         key->entries = entries;
349         key->type |= type;
350 }
351
352 static enum jump_label_type jump_label_type(struct jump_entry *entry)
353 {
354         struct static_key *key = jump_entry_key(entry);
355         bool enabled = static_key_enabled(key);
356         bool branch = jump_entry_branch(entry);
357
358         /* See the comment in linux/jump_label.h */
359         return enabled ^ branch;
360 }
361
362 static void __jump_label_update(struct static_key *key,
363                                 struct jump_entry *entry,
364                                 struct jump_entry *stop)
365 {
366         for (; (entry < stop) && (jump_entry_key(entry) == key); entry++) {
367                 /*
368                  * An entry->code of 0 indicates an entry which has been
369                  * disabled because it was in an init text area.
370                  */
371                 if (entry->code) {
372                         if (kernel_text_address(entry->code))
373                                 arch_jump_label_transform(entry, jump_label_type(entry));
374                         else
375                                 WARN_ONCE(1, "can't patch jump_label at %pS",
376                                           (void *)(unsigned long)entry->code);
377                 }
378         }
379 }
380
381 void __init jump_label_init(void)
382 {
383         struct jump_entry *iter_start = __start___jump_table;
384         struct jump_entry *iter_stop = __stop___jump_table;
385         struct static_key *key = NULL;
386         struct jump_entry *iter;
387
388         /*
389          * Since we are initializing the static_key.enabled field with
390          * with the 'raw' int values (to avoid pulling in atomic.h) in
391          * jump_label.h, let's make sure that is safe. There are only two
392          * cases to check since we initialize to 0 or 1.
393          */
394         BUILD_BUG_ON((int)ATOMIC_INIT(0) != 0);
395         BUILD_BUG_ON((int)ATOMIC_INIT(1) != 1);
396
397         if (static_key_initialized)
398                 return;
399
400         cpus_read_lock();
401         jump_label_lock();
402         jump_label_sort_entries(iter_start, iter_stop);
403
404         for (iter = iter_start; iter < iter_stop; iter++) {
405                 struct static_key *iterk;
406
407                 /* rewrite NOPs */
408                 if (jump_label_type(iter) == JUMP_LABEL_NOP)
409                         arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
410
411                 iterk = jump_entry_key(iter);
412                 if (iterk == key)
413                         continue;
414
415                 key = iterk;
416                 static_key_set_entries(key, iter);
417         }
418         static_key_initialized = true;
419         jump_label_unlock();
420         cpus_read_unlock();
421 }
422
423 /* Disable any jump label entries in __init/__exit code */
424 void __init jump_label_invalidate_initmem(void)
425 {
426         struct jump_entry *iter_start = __start___jump_table;
427         struct jump_entry *iter_stop = __stop___jump_table;
428         struct jump_entry *iter;
429
430         for (iter = iter_start; iter < iter_stop; iter++) {
431                 if (init_section_contains((void *)(unsigned long)iter->code, 1))
432                         iter->code = 0;
433         }
434 }
435
436 #ifdef CONFIG_MODULES
437
438 static enum jump_label_type jump_label_init_type(struct jump_entry *entry)
439 {
440         struct static_key *key = jump_entry_key(entry);
441         bool type = static_key_type(key);
442         bool branch = jump_entry_branch(entry);
443
444         /* See the comment in linux/jump_label.h */
445         return type ^ branch;
446 }
447
448 struct static_key_mod {
449         struct static_key_mod *next;
450         struct jump_entry *entries;
451         struct module *mod;
452 };
453
454 static inline struct static_key_mod *static_key_mod(struct static_key *key)
455 {
456         WARN_ON_ONCE(!(key->type & JUMP_TYPE_LINKED));
457         return (struct static_key_mod *)(key->type & ~JUMP_TYPE_MASK);
458 }
459
460 /***
461  * key->type and key->next are the same via union.
462  * This sets key->next and preserves the type bits.
463  *
464  * See additional comments above static_key_set_entries().
465  */
466 static void static_key_set_mod(struct static_key *key,
467                                struct static_key_mod *mod)
468 {
469         unsigned long type;
470
471         WARN_ON_ONCE((unsigned long)mod & JUMP_TYPE_MASK);
472         type = key->type & JUMP_TYPE_MASK;
473         key->next = mod;
474         key->type |= type;
475 }
476
477 static int __jump_label_mod_text_reserved(void *start, void *end)
478 {
479         struct module *mod;
480
481         preempt_disable();
482         mod = __module_text_address((unsigned long)start);
483         WARN_ON_ONCE(__module_text_address((unsigned long)end) != mod);
484         preempt_enable();
485
486         if (!mod)
487                 return 0;
488
489
490         return __jump_label_text_reserved(mod->jump_entries,
491                                 mod->jump_entries + mod->num_jump_entries,
492                                 start, end);
493 }
494
495 static void __jump_label_mod_update(struct static_key *key)
496 {
497         struct static_key_mod *mod;
498
499         for (mod = static_key_mod(key); mod; mod = mod->next) {
500                 struct jump_entry *stop;
501                 struct module *m;
502
503                 /*
504                  * NULL if the static_key is defined in a module
505                  * that does not use it
506                  */
507                 if (!mod->entries)
508                         continue;
509
510                 m = mod->mod;
511                 if (!m)
512                         stop = __stop___jump_table;
513                 else
514                         stop = m->jump_entries + m->num_jump_entries;
515                 __jump_label_update(key, mod->entries, stop);
516         }
517 }
518
519 /***
520  * apply_jump_label_nops - patch module jump labels with arch_get_jump_label_nop()
521  * @mod: module to patch
522  *
523  * Allow for run-time selection of the optimal nops. Before the module
524  * loads patch these with arch_get_jump_label_nop(), which is specified by
525  * the arch specific jump label code.
526  */
527 void jump_label_apply_nops(struct module *mod)
528 {
529         struct jump_entry *iter_start = mod->jump_entries;
530         struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
531         struct jump_entry *iter;
532
533         /* if the module doesn't have jump label entries, just return */
534         if (iter_start == iter_stop)
535                 return;
536
537         for (iter = iter_start; iter < iter_stop; iter++) {
538                 /* Only write NOPs for arch_branch_static(). */
539                 if (jump_label_init_type(iter) == JUMP_LABEL_NOP)
540                         arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
541         }
542 }
543
544 static int jump_label_add_module(struct module *mod)
545 {
546         struct jump_entry *iter_start = mod->jump_entries;
547         struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
548         struct jump_entry *iter;
549         struct static_key *key = NULL;
550         struct static_key_mod *jlm, *jlm2;
551
552         /* if the module doesn't have jump label entries, just return */
553         if (iter_start == iter_stop)
554                 return 0;
555
556         jump_label_sort_entries(iter_start, iter_stop);
557
558         for (iter = iter_start; iter < iter_stop; iter++) {
559                 struct static_key *iterk;
560
561                 iterk = jump_entry_key(iter);
562                 if (iterk == key)
563                         continue;
564
565                 key = iterk;
566                 if (within_module(iter->key, mod)) {
567                         static_key_set_entries(key, iter);
568                         continue;
569                 }
570                 jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL);
571                 if (!jlm)
572                         return -ENOMEM;
573                 if (!static_key_linked(key)) {
574                         jlm2 = kzalloc(sizeof(struct static_key_mod),
575                                        GFP_KERNEL);
576                         if (!jlm2) {
577                                 kfree(jlm);
578                                 return -ENOMEM;
579                         }
580                         preempt_disable();
581                         jlm2->mod = __module_address((unsigned long)key);
582                         preempt_enable();
583                         jlm2->entries = static_key_entries(key);
584                         jlm2->next = NULL;
585                         static_key_set_mod(key, jlm2);
586                         static_key_set_linked(key);
587                 }
588                 jlm->mod = mod;
589                 jlm->entries = iter;
590                 jlm->next = static_key_mod(key);
591                 static_key_set_mod(key, jlm);
592                 static_key_set_linked(key);
593
594                 /* Only update if we've changed from our initial state */
595                 if (jump_label_type(iter) != jump_label_init_type(iter))
596                         __jump_label_update(key, iter, iter_stop);
597         }
598
599         return 0;
600 }
601
602 static void jump_label_del_module(struct module *mod)
603 {
604         struct jump_entry *iter_start = mod->jump_entries;
605         struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
606         struct jump_entry *iter;
607         struct static_key *key = NULL;
608         struct static_key_mod *jlm, **prev;
609
610         for (iter = iter_start; iter < iter_stop; iter++) {
611                 if (jump_entry_key(iter) == key)
612                         continue;
613
614                 key = jump_entry_key(iter);
615
616                 if (within_module(iter->key, mod))
617                         continue;
618
619                 /* No memory during module load */
620                 if (WARN_ON(!static_key_linked(key)))
621                         continue;
622
623                 prev = &key->next;
624                 jlm = static_key_mod(key);
625
626                 while (jlm && jlm->mod != mod) {
627                         prev = &jlm->next;
628                         jlm = jlm->next;
629                 }
630
631                 /* No memory during module load */
632                 if (WARN_ON(!jlm))
633                         continue;
634
635                 if (prev == &key->next)
636                         static_key_set_mod(key, jlm->next);
637                 else
638                         *prev = jlm->next;
639
640                 kfree(jlm);
641
642                 jlm = static_key_mod(key);
643                 /* if only one etry is left, fold it back into the static_key */
644                 if (jlm->next == NULL) {
645                         static_key_set_entries(key, jlm->entries);
646                         static_key_clear_linked(key);
647                         kfree(jlm);
648                 }
649         }
650 }
651
652 /* Disable any jump label entries in module init code */
653 static void jump_label_invalidate_module_init(struct module *mod)
654 {
655         struct jump_entry *iter_start = mod->jump_entries;
656         struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
657         struct jump_entry *iter;
658
659         for (iter = iter_start; iter < iter_stop; iter++) {
660                 if (within_module_init(iter->code, mod))
661                         iter->code = 0;
662         }
663 }
664
665 static int
666 jump_label_module_notify(struct notifier_block *self, unsigned long val,
667                          void *data)
668 {
669         struct module *mod = data;
670         int ret = 0;
671
672         cpus_read_lock();
673         jump_label_lock();
674
675         switch (val) {
676         case MODULE_STATE_COMING:
677                 ret = jump_label_add_module(mod);
678                 if (ret) {
679                         WARN(1, "Failed to allocate memory: jump_label may not work properly.\n");
680                         jump_label_del_module(mod);
681                 }
682                 break;
683         case MODULE_STATE_GOING:
684                 jump_label_del_module(mod);
685                 break;
686         case MODULE_STATE_LIVE:
687                 jump_label_invalidate_module_init(mod);
688                 break;
689         }
690
691         jump_label_unlock();
692         cpus_read_unlock();
693
694         return notifier_from_errno(ret);
695 }
696
697 static struct notifier_block jump_label_module_nb = {
698         .notifier_call = jump_label_module_notify,
699         .priority = 1, /* higher than tracepoints */
700 };
701
702 static __init int jump_label_init_module(void)
703 {
704         return register_module_notifier(&jump_label_module_nb);
705 }
706 early_initcall(jump_label_init_module);
707
708 #endif /* CONFIG_MODULES */
709
710 /***
711  * jump_label_text_reserved - check if addr range is reserved
712  * @start: start text addr
713  * @end: end text addr
714  *
715  * checks if the text addr located between @start and @end
716  * overlaps with any of the jump label patch addresses. Code
717  * that wants to modify kernel text should first verify that
718  * it does not overlap with any of the jump label addresses.
719  * Caller must hold jump_label_mutex.
720  *
721  * returns 1 if there is an overlap, 0 otherwise
722  */
723 int jump_label_text_reserved(void *start, void *end)
724 {
725         int ret = __jump_label_text_reserved(__start___jump_table,
726                         __stop___jump_table, start, end);
727
728         if (ret)
729                 return ret;
730
731 #ifdef CONFIG_MODULES
732         ret = __jump_label_mod_text_reserved(start, end);
733 #endif
734         return ret;
735 }
736
737 static void jump_label_update(struct static_key *key)
738 {
739         struct jump_entry *stop = __stop___jump_table;
740         struct jump_entry *entry;
741 #ifdef CONFIG_MODULES
742         struct module *mod;
743
744         if (static_key_linked(key)) {
745                 __jump_label_mod_update(key);
746                 return;
747         }
748
749         preempt_disable();
750         mod = __module_address((unsigned long)key);
751         if (mod)
752                 stop = mod->jump_entries + mod->num_jump_entries;
753         preempt_enable();
754 #endif
755         entry = static_key_entries(key);
756         /* if there are no users, entry can be NULL */
757         if (entry)
758                 __jump_label_update(key, entry, stop);
759 }
760
761 #ifdef CONFIG_STATIC_KEYS_SELFTEST
762 static DEFINE_STATIC_KEY_TRUE(sk_true);
763 static DEFINE_STATIC_KEY_FALSE(sk_false);
764
765 static __init int jump_label_test(void)
766 {
767         int i;
768
769         for (i = 0; i < 2; i++) {
770                 WARN_ON(static_key_enabled(&sk_true.key) != true);
771                 WARN_ON(static_key_enabled(&sk_false.key) != false);
772
773                 WARN_ON(!static_branch_likely(&sk_true));
774                 WARN_ON(!static_branch_unlikely(&sk_true));
775                 WARN_ON(static_branch_likely(&sk_false));
776                 WARN_ON(static_branch_unlikely(&sk_false));
777
778                 static_branch_disable(&sk_true);
779                 static_branch_enable(&sk_false);
780
781                 WARN_ON(static_key_enabled(&sk_true.key) == true);
782                 WARN_ON(static_key_enabled(&sk_false.key) == false);
783
784                 WARN_ON(static_branch_likely(&sk_true));
785                 WARN_ON(static_branch_unlikely(&sk_true));
786                 WARN_ON(!static_branch_likely(&sk_false));
787                 WARN_ON(!static_branch_unlikely(&sk_false));
788
789                 static_branch_enable(&sk_true);
790                 static_branch_disable(&sk_false);
791         }
792
793         return 0;
794 }
795 early_initcall(jump_label_test);
796 #endif /* STATIC_KEYS_SELFTEST */