1
2
3
4
5
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
20#ifdef HAVE_JUMP_LABEL
21
22
23static DEFINE_MUTEX(jump_label_mutex);
24
25void jump_label_lock(void)
26{
27 mutex_lock(&jump_label_mutex);
28}
29
30void jump_label_unlock(void)
31{
32 mutex_unlock(&jump_label_mutex);
33}
34
35static int jump_label_cmp(const void *a, const void *b)
36{
37 const struct jump_entry *jea = a;
38 const struct jump_entry *jeb = b;
39
40 if (jea->key < jeb->key)
41 return -1;
42
43 if (jea->key > jeb->key)
44 return 1;
45
46 return 0;
47}
48
49static void
50jump_label_sort_entries(struct jump_entry *start, struct jump_entry *stop)
51{
52 unsigned long size;
53
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);
57}
58
59static void jump_label_update(struct static_key *key);
60
61
62
63
64
65
66
67
68
69
70int static_key_count(struct static_key *key)
71{
72
73
74
75
76 int n = atomic_read(&key->enabled);
77
78 return n >= 0 ? n : 1;
79}
80EXPORT_SYMBOL_GPL(static_key_count);
81
82static void static_key_slow_inc_cpuslocked(struct static_key *key)
83{
84 int v, v1;
85
86 STATIC_KEY_CHECK_USE();
87
88
89
90
91
92
93
94
95
96
97
98
99
100 for (v = atomic_read(&key->enabled); v > 0; v = v1) {
101 v1 = atomic_cmpxchg(&key->enabled, v, v + 1);
102 if (likely(v1 == v))
103 return;
104 }
105
106 jump_label_lock();
107 if (atomic_read(&key->enabled) == 0) {
108 atomic_set(&key->enabled, -1);
109 jump_label_update(key);
110
111
112
113
114 atomic_set_release(&key->enabled, 1);
115 } else {
116 atomic_inc(&key->enabled);
117 }
118 jump_label_unlock();
119}
120
121void static_key_slow_inc(struct static_key *key)
122{
123 cpus_read_lock();
124 static_key_slow_inc_cpuslocked(key);
125 cpus_read_unlock();
126}
127EXPORT_SYMBOL_GPL(static_key_slow_inc);
128
129void static_key_enable_cpuslocked(struct static_key *key)
130{
131 STATIC_KEY_CHECK_USE();
132
133 if (atomic_read(&key->enabled) > 0) {
134 WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
135 return;
136 }
137
138 jump_label_lock();
139 if (atomic_read(&key->enabled) == 0) {
140 atomic_set(&key->enabled, -1);
141 jump_label_update(key);
142
143
144
145 atomic_set_release(&key->enabled, 1);
146 }
147 jump_label_unlock();
148}
149EXPORT_SYMBOL_GPL(static_key_enable_cpuslocked);
150
151void static_key_enable(struct static_key *key)
152{
153 cpus_read_lock();
154 static_key_enable_cpuslocked(key);
155 cpus_read_unlock();
156}
157EXPORT_SYMBOL_GPL(static_key_enable);
158
159void static_key_disable_cpuslocked(struct static_key *key)
160{
161 STATIC_KEY_CHECK_USE();
162
163 if (atomic_read(&key->enabled) != 1) {
164 WARN_ON_ONCE(atomic_read(&key->enabled) != 0);
165 return;
166 }
167
168 jump_label_lock();
169 if (atomic_cmpxchg(&key->enabled, 1, 0))
170 jump_label_update(key);
171 jump_label_unlock();
172}
173EXPORT_SYMBOL_GPL(static_key_disable_cpuslocked);
174
175void static_key_disable(struct static_key *key)
176{
177 cpus_read_lock();
178 static_key_disable_cpuslocked(key);
179 cpus_read_unlock();
180}
181EXPORT_SYMBOL_GPL(static_key_disable);
182
183static void static_key_slow_dec_cpuslocked(struct static_key *key,
184 unsigned long rate_limit,
185 struct delayed_work *work)
186{
187
188
189
190
191
192
193
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");
197 return;
198 }
199
200 if (rate_limit) {
201 atomic_inc(&key->enabled);
202 schedule_delayed_work(work, rate_limit);
203 } else {
204 jump_label_update(key);
205 }
206 jump_label_unlock();
207}
208
209static void __static_key_slow_dec(struct static_key *key,
210 unsigned long rate_limit,
211 struct delayed_work *work)
212{
213 cpus_read_lock();
214 static_key_slow_dec_cpuslocked(key, rate_limit, work);
215 cpus_read_unlock();
216}
217
218static void jump_label_update_timeout(struct work_struct *work)
219{
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);
223}
224
225void static_key_slow_dec(struct static_key *key)
226{
227 STATIC_KEY_CHECK_USE();
228 __static_key_slow_dec(key, 0, NULL);
229}
230EXPORT_SYMBOL_GPL(static_key_slow_dec);
231
232void static_key_slow_dec_deferred(struct static_key_deferred *key)
233{
234 STATIC_KEY_CHECK_USE();
235 __static_key_slow_dec(&key->key, key->timeout, &key->work);
236}
237EXPORT_SYMBOL_GPL(static_key_slow_dec_deferred);
238
239void static_key_deferred_flush(struct static_key_deferred *key)
240{
241 STATIC_KEY_CHECK_USE();
242 flush_delayed_work(&key->work);
243}
244EXPORT_SYMBOL_GPL(static_key_deferred_flush);
245
246void jump_label_rate_limit(struct static_key_deferred *key,
247 unsigned long rl)
248{
249 STATIC_KEY_CHECK_USE();
250 key->timeout = rl;
251 INIT_DELAYED_WORK(&key->work, jump_label_update_timeout);
252}
253EXPORT_SYMBOL_GPL(jump_label_rate_limit);
254
255static int addr_conflict(struct jump_entry *entry, void *start, void *end)
256{
257 if (entry->code <= (unsigned long)end &&
258 entry->code + JUMP_LABEL_NOP_SIZE > (unsigned long)start)
259 return 1;
260
261 return 0;
262}
263
264static int __jump_label_text_reserved(struct jump_entry *iter_start,
265 struct jump_entry *iter_stop, void *start, void *end)
266{
267 struct jump_entry *iter;
268
269 iter = iter_start;
270 while (iter < iter_stop) {
271 if (addr_conflict(iter, start, end))
272 return 1;
273 iter++;
274 }
275
276 return 0;
277}
278
279
280
281
282
283
284
285void __weak __init_or_module arch_jump_label_transform_static(struct jump_entry *entry,
286 enum jump_label_type type)
287{
288 arch_jump_label_transform(entry, type);
289}
290
291static inline struct jump_entry *static_key_entries(struct static_key *key)
292{
293 WARN_ON_ONCE(key->type & JUMP_TYPE_LINKED);
294 return (struct jump_entry *)(key->type & ~JUMP_TYPE_MASK);
295}
296
297static inline bool static_key_type(struct static_key *key)
298{
299 return key->type & JUMP_TYPE_TRUE;
300}
301
302static inline bool static_key_linked(struct static_key *key)
303{
304 return key->type & JUMP_TYPE_LINKED;
305}
306
307static inline void static_key_clear_linked(struct static_key *key)
308{
309 key->type &= ~JUMP_TYPE_LINKED;
310}
311
312static inline void static_key_set_linked(struct static_key *key)
313{
314 key->type |= JUMP_TYPE_LINKED;
315}
316
317static inline struct static_key *jump_entry_key(struct jump_entry *entry)
318{
319 return (struct static_key *)((unsigned long)entry->key & ~1UL);
320}
321
322static bool jump_entry_branch(struct jump_entry *entry)
323{
324 return (unsigned long)entry->key & 1UL;
325}
326
327
328
329
330
331
332
333
334
335
336static void static_key_set_entries(struct static_key *key,
337 struct jump_entry *entries)
338{
339 unsigned long type;
340
341 WARN_ON_ONCE((unsigned long)entries & JUMP_TYPE_MASK);
342 type = key->type & JUMP_TYPE_MASK;
343 key->entries = entries;
344 key->type |= type;
345}
346
347static enum jump_label_type jump_label_type(struct jump_entry *entry)
348{
349 struct static_key *key = jump_entry_key(entry);
350 bool enabled = static_key_enabled(key);
351 bool branch = jump_entry_branch(entry);
352
353
354 return enabled ^ branch;
355}
356
357static void __jump_label_update(struct static_key *key,
358 struct jump_entry *entry,
359 struct jump_entry *stop)
360{
361 for (; (entry < stop) && (jump_entry_key(entry) == key); entry++) {
362
363
364
365
366
367 if (entry->code && kernel_text_address(entry->code))
368 arch_jump_label_transform(entry, jump_label_type(entry));
369 }
370}
371
372void __init jump_label_init(void)
373{
374 struct jump_entry *iter_start = __start___jump_table;
375 struct jump_entry *iter_stop = __stop___jump_table;
376 struct static_key *key = NULL;
377 struct jump_entry *iter;
378
379
380
381
382
383
384
385 BUILD_BUG_ON((int)ATOMIC_INIT(0) != 0);
386 BUILD_BUG_ON((int)ATOMIC_INIT(1) != 1);
387
388 if (static_key_initialized)
389 return;
390
391 cpus_read_lock();
392 jump_label_lock();
393 jump_label_sort_entries(iter_start, iter_stop);
394
395 for (iter = iter_start; iter < iter_stop; iter++) {
396 struct static_key *iterk;
397
398
399 if (jump_label_type(iter) == JUMP_LABEL_NOP)
400 arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
401
402 iterk = jump_entry_key(iter);
403 if (iterk == key)
404 continue;
405
406 key = iterk;
407 static_key_set_entries(key, iter);
408 }
409 static_key_initialized = true;
410 jump_label_unlock();
411 cpus_read_unlock();
412}
413
414#ifdef CONFIG_MODULES
415
416static enum jump_label_type jump_label_init_type(struct jump_entry *entry)
417{
418 struct static_key *key = jump_entry_key(entry);
419 bool type = static_key_type(key);
420 bool branch = jump_entry_branch(entry);
421
422
423 return type ^ branch;
424}
425
426struct static_key_mod {
427 struct static_key_mod *next;
428 struct jump_entry *entries;
429 struct module *mod;
430};
431
432static inline struct static_key_mod *static_key_mod(struct static_key *key)
433{
434 WARN_ON_ONCE(!(key->type & JUMP_TYPE_LINKED));
435 return (struct static_key_mod *)(key->type & ~JUMP_TYPE_MASK);
436}
437
438
439
440
441
442
443
444static void static_key_set_mod(struct static_key *key,
445 struct static_key_mod *mod)
446{
447 unsigned long type;
448
449 WARN_ON_ONCE((unsigned long)mod & JUMP_TYPE_MASK);
450 type = key->type & JUMP_TYPE_MASK;
451 key->next = mod;
452 key->type |= type;
453}
454
455static int __jump_label_mod_text_reserved(void *start, void *end)
456{
457 struct module *mod;
458
459 preempt_disable();
460 mod = __module_text_address((unsigned long)start);
461 WARN_ON_ONCE(__module_text_address((unsigned long)end) != mod);
462 preempt_enable();
463
464 if (!mod)
465 return 0;
466
467
468 return __jump_label_text_reserved(mod->jump_entries,
469 mod->jump_entries + mod->num_jump_entries,
470 start, end);
471}
472
473static void __jump_label_mod_update(struct static_key *key)
474{
475 struct static_key_mod *mod;
476
477 for (mod = static_key_mod(key); mod; mod = mod->next) {
478 struct jump_entry *stop;
479 struct module *m;
480
481
482
483
484
485 if (!mod->entries)
486 continue;
487
488 m = mod->mod;
489 if (!m)
490 stop = __stop___jump_table;
491 else
492 stop = m->jump_entries + m->num_jump_entries;
493 __jump_label_update(key, mod->entries, stop);
494 }
495}
496
497
498
499
500
501
502
503
504
505void jump_label_apply_nops(struct module *mod)
506{
507 struct jump_entry *iter_start = mod->jump_entries;
508 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
509 struct jump_entry *iter;
510
511
512 if (iter_start == iter_stop)
513 return;
514
515 for (iter = iter_start; iter < iter_stop; iter++) {
516
517 if (jump_label_init_type(iter) == JUMP_LABEL_NOP)
518 arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
519 }
520}
521
522static int jump_label_add_module(struct module *mod)
523{
524 struct jump_entry *iter_start = mod->jump_entries;
525 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
526 struct jump_entry *iter;
527 struct static_key *key = NULL;
528 struct static_key_mod *jlm, *jlm2;
529
530
531 if (iter_start == iter_stop)
532 return 0;
533
534 jump_label_sort_entries(iter_start, iter_stop);
535
536 for (iter = iter_start; iter < iter_stop; iter++) {
537 struct static_key *iterk;
538
539 iterk = jump_entry_key(iter);
540 if (iterk == key)
541 continue;
542
543 key = iterk;
544 if (within_module(iter->key, mod)) {
545 static_key_set_entries(key, iter);
546 continue;
547 }
548 jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL);
549 if (!jlm)
550 return -ENOMEM;
551 if (!static_key_linked(key)) {
552 jlm2 = kzalloc(sizeof(struct static_key_mod),
553 GFP_KERNEL);
554 if (!jlm2) {
555 kfree(jlm);
556 return -ENOMEM;
557 }
558 preempt_disable();
559 jlm2->mod = __module_address((unsigned long)key);
560 preempt_enable();
561 jlm2->entries = static_key_entries(key);
562 jlm2->next = NULL;
563 static_key_set_mod(key, jlm2);
564 static_key_set_linked(key);
565 }
566 jlm->mod = mod;
567 jlm->entries = iter;
568 jlm->next = static_key_mod(key);
569 static_key_set_mod(key, jlm);
570 static_key_set_linked(key);
571
572
573 if (jump_label_type(iter) != jump_label_init_type(iter))
574 __jump_label_update(key, iter, iter_stop);
575 }
576
577 return 0;
578}
579
580static void jump_label_del_module(struct module *mod)
581{
582 struct jump_entry *iter_start = mod->jump_entries;
583 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
584 struct jump_entry *iter;
585 struct static_key *key = NULL;
586 struct static_key_mod *jlm, **prev;
587
588 for (iter = iter_start; iter < iter_stop; iter++) {
589 if (jump_entry_key(iter) == key)
590 continue;
591
592 key = jump_entry_key(iter);
593
594 if (within_module(iter->key, mod))
595 continue;
596
597
598 if (WARN_ON(!static_key_linked(key)))
599 continue;
600
601 prev = &key->next;
602 jlm = static_key_mod(key);
603
604 while (jlm && jlm->mod != mod) {
605 prev = &jlm->next;
606 jlm = jlm->next;
607 }
608
609
610 if (WARN_ON(!jlm))
611 continue;
612
613 if (prev == &key->next)
614 static_key_set_mod(key, jlm->next);
615 else
616 *prev = jlm->next;
617
618 kfree(jlm);
619
620 jlm = static_key_mod(key);
621
622 if (jlm->next == NULL) {
623 static_key_set_entries(key, jlm->entries);
624 static_key_clear_linked(key);
625 kfree(jlm);
626 }
627 }
628}
629
630static void jump_label_invalidate_module_init(struct module *mod)
631{
632 struct jump_entry *iter_start = mod->jump_entries;
633 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
634 struct jump_entry *iter;
635
636 for (iter = iter_start; iter < iter_stop; iter++) {
637 if (within_module_init(iter->code, mod))
638 iter->code = 0;
639 }
640}
641
642static int
643jump_label_module_notify(struct notifier_block *self, unsigned long val,
644 void *data)
645{
646 struct module *mod = data;
647 int ret = 0;
648
649 cpus_read_lock();
650 jump_label_lock();
651
652 switch (val) {
653 case MODULE_STATE_COMING:
654 ret = jump_label_add_module(mod);
655 if (ret) {
656 WARN(1, "Failed to allocatote memory: jump_label may not work properly.\n");
657 jump_label_del_module(mod);
658 }
659 break;
660 case MODULE_STATE_GOING:
661 jump_label_del_module(mod);
662 break;
663 case MODULE_STATE_LIVE:
664 jump_label_invalidate_module_init(mod);
665 break;
666 }
667
668 jump_label_unlock();
669 cpus_read_unlock();
670
671 return notifier_from_errno(ret);
672}
673
674static struct notifier_block jump_label_module_nb = {
675 .notifier_call = jump_label_module_notify,
676 .priority = 1,
677};
678
679static __init int jump_label_init_module(void)
680{
681 return register_module_notifier(&jump_label_module_nb);
682}
683early_initcall(jump_label_init_module);
684
685#endif
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700int jump_label_text_reserved(void *start, void *end)
701{
702 int ret = __jump_label_text_reserved(__start___jump_table,
703 __stop___jump_table, start, end);
704
705 if (ret)
706 return ret;
707
708#ifdef CONFIG_MODULES
709 ret = __jump_label_mod_text_reserved(start, end);
710#endif
711 return ret;
712}
713
714static void jump_label_update(struct static_key *key)
715{
716 struct jump_entry *stop = __stop___jump_table;
717 struct jump_entry *entry;
718#ifdef CONFIG_MODULES
719 struct module *mod;
720
721 if (static_key_linked(key)) {
722 __jump_label_mod_update(key);
723 return;
724 }
725
726 preempt_disable();
727 mod = __module_address((unsigned long)key);
728 if (mod)
729 stop = mod->jump_entries + mod->num_jump_entries;
730 preempt_enable();
731#endif
732 entry = static_key_entries(key);
733
734 if (entry)
735 __jump_label_update(key, entry, stop);
736}
737
738#ifdef CONFIG_STATIC_KEYS_SELFTEST
739static DEFINE_STATIC_KEY_TRUE(sk_true);
740static DEFINE_STATIC_KEY_FALSE(sk_false);
741
742static __init int jump_label_test(void)
743{
744 int i;
745
746 for (i = 0; i < 2; i++) {
747 WARN_ON(static_key_enabled(&sk_true.key) != true);
748 WARN_ON(static_key_enabled(&sk_false.key) != false);
749
750 WARN_ON(!static_branch_likely(&sk_true));
751 WARN_ON(!static_branch_unlikely(&sk_true));
752 WARN_ON(static_branch_likely(&sk_false));
753 WARN_ON(static_branch_unlikely(&sk_false));
754
755 static_branch_disable(&sk_true);
756 static_branch_enable(&sk_false);
757
758 WARN_ON(static_key_enabled(&sk_true.key) == true);
759 WARN_ON(static_key_enabled(&sk_false.key) == false);
760
761 WARN_ON(static_branch_likely(&sk_true));
762 WARN_ON(static_branch_unlikely(&sk_true));
763 WARN_ON(!static_branch_likely(&sk_false));
764 WARN_ON(!static_branch_unlikely(&sk_false));
765
766 static_branch_enable(&sk_true);
767 static_branch_disable(&sk_false);
768 }
769
770 return 0;
771}
772late_initcall(jump_label_test);
773#endif
774
775#endif
776