locking/mutex: Improve documentation
[linux-block.git] / kernel / jump_label.c
CommitLineData
bf5438fc
JB
1/*
2 * jump label support
3 *
4 * Copyright (C) 2009 Jason Baron <jbaron@redhat.com>
90eec103 5 * Copyright (C) 2011 Peter Zijlstra
bf5438fc
JB
6 *
7 */
bf5438fc
JB
8#include <linux/memory.h>
9#include <linux/uaccess.h>
10#include <linux/module.h>
11#include <linux/list.h>
bf5438fc
JB
12#include <linux/slab.h>
13#include <linux/sort.h>
14#include <linux/err.h>
c5905afb 15#include <linux/static_key.h>
851cf6e7 16#include <linux/jump_label_ratelimit.h>
1f69bf9c 17#include <linux/bug.h>
f2545b2d 18#include <linux/cpu.h>
bf5438fc
JB
19
20#ifdef HAVE_JUMP_LABEL
21
bf5438fc
JB
22/* mutex to protect coming/going of the the jump_label table */
23static DEFINE_MUTEX(jump_label_mutex);
24
91bad2f8
JB
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
bf5438fc
JB
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
d430d3d7 50jump_label_sort_entries(struct jump_entry *start, struct jump_entry *stop)
bf5438fc
JB
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
706249c2 59static void jump_label_update(struct static_key *key);
a1efb01f 60
1f69bf9c
JB
61/*
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.
69 */
70int static_key_count(struct static_key *key)
71{
72 /*
73 * -1 means the first static_key_slow_inc() is in progress.
74 * static_key_enabled() must return true, so return 1 here.
75 */
76 int n = atomic_read(&key->enabled);
77
78 return n >= 0 ? n : 1;
79}
80EXPORT_SYMBOL_GPL(static_key_count);
81
ce48c146 82void static_key_slow_inc_cpuslocked(struct static_key *key)
bf5438fc 83{
4c5ea0a9
PB
84 int v, v1;
85
5cdda511 86 STATIC_KEY_CHECK_USE(key);
4c5ea0a9
PB
87
88 /*
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.
94 *
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.
99 */
100 for (v = atomic_read(&key->enabled); v > 0; v = v1) {
101 v1 = atomic_cmpxchg(&key->enabled, v, v + 1);
8b7b4128 102 if (likely(v1 == v))
4c5ea0a9
PB
103 return;
104 }
bf5438fc 105
d430d3d7 106 jump_label_lock();
4c5ea0a9
PB
107 if (atomic_read(&key->enabled) == 0) {
108 atomic_set(&key->enabled, -1);
706249c2 109 jump_label_update(key);
d0646a6f
PZ
110 /*
111 * Ensure that if the above cmpxchg loop observes our positive
112 * value, it must also observe all the text changes.
113 */
114 atomic_set_release(&key->enabled, 1);
4c5ea0a9
PB
115 } else {
116 atomic_inc(&key->enabled);
117 }
d430d3d7 118 jump_label_unlock();
8b7b4128
MZ
119}
120
121void static_key_slow_inc(struct static_key *key)
122{
123 cpus_read_lock();
124 static_key_slow_inc_cpuslocked(key);
f2545b2d 125 cpus_read_unlock();
bf5438fc 126}
c5905afb 127EXPORT_SYMBOL_GPL(static_key_slow_inc);
bf5438fc 128
5a40527f 129void static_key_enable_cpuslocked(struct static_key *key)
1dbb6704 130{
5cdda511 131 STATIC_KEY_CHECK_USE(key);
5a40527f 132
1dbb6704
PB
133 if (atomic_read(&key->enabled) > 0) {
134 WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
135 return;
136 }
137
1dbb6704
PB
138 jump_label_lock();
139 if (atomic_read(&key->enabled) == 0) {
140 atomic_set(&key->enabled, -1);
141 jump_label_update(key);
d0646a6f
PZ
142 /*
143 * See static_key_slow_inc().
144 */
145 atomic_set_release(&key->enabled, 1);
1dbb6704
PB
146 }
147 jump_label_unlock();
5a40527f
MZ
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);
1dbb6704
PB
155 cpus_read_unlock();
156}
157EXPORT_SYMBOL_GPL(static_key_enable);
158
5a40527f 159void static_key_disable_cpuslocked(struct static_key *key)
1dbb6704 160{
5cdda511 161 STATIC_KEY_CHECK_USE(key);
5a40527f 162
1dbb6704
PB
163 if (atomic_read(&key->enabled) != 1) {
164 WARN_ON_ONCE(atomic_read(&key->enabled) != 0);
165 return;
166 }
167
1dbb6704
PB
168 jump_label_lock();
169 if (atomic_cmpxchg(&key->enabled, 1, 0))
170 jump_label_update(key);
171 jump_label_unlock();
5a40527f
MZ
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);
1dbb6704
PB
179 cpus_read_unlock();
180}
181EXPORT_SYMBOL_GPL(static_key_disable);
182
ce48c146 183static void __static_key_slow_dec_cpuslocked(struct static_key *key,
8b7b4128
MZ
184 unsigned long rate_limit,
185 struct delayed_work *work)
bf5438fc 186{
4c5ea0a9
PB
187 /*
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.
193 */
fadf0464
JB
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");
d430d3d7 197 return;
fadf0464 198 }
bf5438fc 199
b2029520
GN
200 if (rate_limit) {
201 atomic_inc(&key->enabled);
202 schedule_delayed_work(work, rate_limit);
c5905afb 203 } else {
706249c2 204 jump_label_update(key);
c5905afb 205 }
91bad2f8 206 jump_label_unlock();
8b7b4128
MZ
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();
ce48c146 214 __static_key_slow_dec_cpuslocked(key, rate_limit, work);
f2545b2d 215 cpus_read_unlock();
bf5438fc
JB
216}
217
b2029520
GN
218static void jump_label_update_timeout(struct work_struct *work)
219{
c5905afb
IM
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);
b2029520
GN
223}
224
c5905afb 225void static_key_slow_dec(struct static_key *key)
b2029520 226{
5cdda511 227 STATIC_KEY_CHECK_USE(key);
c5905afb 228 __static_key_slow_dec(key, 0, NULL);
b2029520 229}
c5905afb 230EXPORT_SYMBOL_GPL(static_key_slow_dec);
b2029520 231
ce48c146
PZ
232void static_key_slow_dec_cpuslocked(struct static_key *key)
233{
234 STATIC_KEY_CHECK_USE(key);
235 __static_key_slow_dec_cpuslocked(key, 0, NULL);
236}
237
c5905afb 238void static_key_slow_dec_deferred(struct static_key_deferred *key)
b2029520 239{
5cdda511 240 STATIC_KEY_CHECK_USE(key);
c5905afb 241 __static_key_slow_dec(&key->key, key->timeout, &key->work);
b2029520 242}
c5905afb 243EXPORT_SYMBOL_GPL(static_key_slow_dec_deferred);
b2029520 244
b6416e61
DM
245void static_key_deferred_flush(struct static_key_deferred *key)
246{
5cdda511 247 STATIC_KEY_CHECK_USE(key);
b6416e61
DM
248 flush_delayed_work(&key->work);
249}
250EXPORT_SYMBOL_GPL(static_key_deferred_flush);
251
c5905afb 252void jump_label_rate_limit(struct static_key_deferred *key,
b2029520
GN
253 unsigned long rl)
254{
5cdda511 255 STATIC_KEY_CHECK_USE(key);
b2029520
GN
256 key->timeout = rl;
257 INIT_DELAYED_WORK(&key->work, jump_label_update_timeout);
258}
a181dc14 259EXPORT_SYMBOL_GPL(jump_label_rate_limit);
b2029520 260
4c3ef6d7
JB
261static int addr_conflict(struct jump_entry *entry, void *start, void *end)
262{
263 if (entry->code <= (unsigned long)end &&
264 entry->code + JUMP_LABEL_NOP_SIZE > (unsigned long)start)
265 return 1;
266
267 return 0;
268}
269
d430d3d7
JB
270static int __jump_label_text_reserved(struct jump_entry *iter_start,
271 struct jump_entry *iter_stop, void *start, void *end)
4c3ef6d7 272{
4c3ef6d7 273 struct jump_entry *iter;
4c3ef6d7 274
4c3ef6d7
JB
275 iter = iter_start;
276 while (iter < iter_stop) {
d430d3d7
JB
277 if (addr_conflict(iter, start, end))
278 return 1;
4c3ef6d7
JB
279 iter++;
280 }
281
d430d3d7
JB
282 return 0;
283}
284
706249c2 285/*
20284aa7
JF
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
289 * cheaper.
290 */
9cdbe1cb 291void __weak __init_or_module arch_jump_label_transform_static(struct jump_entry *entry,
20284aa7
JF
292 enum jump_label_type type)
293{
706249c2 294 arch_jump_label_transform(entry, type);
20284aa7
JF
295}
296
706249c2 297static inline struct jump_entry *static_key_entries(struct static_key *key)
d430d3d7 298{
3821fd35
JB
299 WARN_ON_ONCE(key->type & JUMP_TYPE_LINKED);
300 return (struct jump_entry *)(key->type & ~JUMP_TYPE_MASK);
4c3ef6d7
JB
301}
302
706249c2 303static inline bool static_key_type(struct static_key *key)
c5905afb 304{
3821fd35
JB
305 return key->type & JUMP_TYPE_TRUE;
306}
307
308static inline bool static_key_linked(struct static_key *key)
309{
310 return key->type & JUMP_TYPE_LINKED;
311}
312
313static inline void static_key_clear_linked(struct static_key *key)
314{
315 key->type &= ~JUMP_TYPE_LINKED;
316}
317
318static inline void static_key_set_linked(struct static_key *key)
319{
320 key->type |= JUMP_TYPE_LINKED;
a1efb01f 321}
c5905afb 322
7dcfd915
PZ
323static inline struct static_key *jump_entry_key(struct jump_entry *entry)
324{
11276d53
PZ
325 return (struct static_key *)((unsigned long)entry->key & ~1UL);
326}
327
328static bool jump_entry_branch(struct jump_entry *entry)
329{
330 return (unsigned long)entry->key & 1UL;
7dcfd915
PZ
331}
332
3821fd35
JB
333/***
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.
337 *
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.
341 */
342static void static_key_set_entries(struct static_key *key,
343 struct jump_entry *entries)
344{
345 unsigned long type;
346
347 WARN_ON_ONCE((unsigned long)entries & JUMP_TYPE_MASK);
348 type = key->type & JUMP_TYPE_MASK;
349 key->entries = entries;
350 key->type |= type;
351}
352
706249c2 353static enum jump_label_type jump_label_type(struct jump_entry *entry)
a1efb01f 354{
706249c2 355 struct static_key *key = jump_entry_key(entry);
a1efb01f 356 bool enabled = static_key_enabled(key);
11276d53 357 bool branch = jump_entry_branch(entry);
c5905afb 358
11276d53
PZ
359 /* See the comment in linux/jump_label.h */
360 return enabled ^ branch;
c5905afb
IM
361}
362
706249c2
PZ
363static void __jump_label_update(struct static_key *key,
364 struct jump_entry *entry,
365 struct jump_entry *stop)
366{
367 for (; (entry < stop) && (jump_entry_key(entry) == key); entry++) {
368 /*
dc1dd184
JP
369 * An entry->code of 0 indicates an entry which has been
370 * disabled because it was in an init text area.
706249c2 371 */
dc1dd184
JP
372 if (entry->code) {
373 if (kernel_text_address(entry->code))
374 arch_jump_label_transform(entry, jump_label_type(entry));
375 else
af1d830b
JP
376 WARN_ONCE(1, "can't patch jump_label at %pS",
377 (void *)(unsigned long)entry->code);
dc1dd184 378 }
706249c2
PZ
379 }
380}
381
97ce2c88 382void __init jump_label_init(void)
bf5438fc 383{
bf5438fc
JB
384 struct jump_entry *iter_start = __start___jump_table;
385 struct jump_entry *iter_stop = __stop___jump_table;
c5905afb 386 struct static_key *key = NULL;
bf5438fc
JB
387 struct jump_entry *iter;
388
1f69bf9c
JB
389 /*
390 * Since we are initializing the static_key.enabled field with
391 * with the 'raw' int values (to avoid pulling in atomic.h) in
392 * jump_label.h, let's make sure that is safe. There are only two
393 * cases to check since we initialize to 0 or 1.
394 */
395 BUILD_BUG_ON((int)ATOMIC_INIT(0) != 0);
396 BUILD_BUG_ON((int)ATOMIC_INIT(1) != 1);
397
e3f91083
KH
398 if (static_key_initialized)
399 return;
400
f2545b2d 401 cpus_read_lock();
91bad2f8 402 jump_label_lock();
d430d3d7
JB
403 jump_label_sort_entries(iter_start, iter_stop);
404
405 for (iter = iter_start; iter < iter_stop; iter++) {
c5905afb 406 struct static_key *iterk;
37348804 407
11276d53
PZ
408 /* rewrite NOPs */
409 if (jump_label_type(iter) == JUMP_LABEL_NOP)
410 arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
411
7dcfd915 412 iterk = jump_entry_key(iter);
37348804 413 if (iterk == key)
d430d3d7
JB
414 continue;
415
37348804 416 key = iterk;
3821fd35 417 static_key_set_entries(key, iter);
bf5438fc 418 }
c4b2c0c5 419 static_key_initialized = true;
91bad2f8 420 jump_label_unlock();
f2545b2d 421 cpus_read_unlock();
bf5438fc 422}
bf5438fc 423
33352244
JP
424/* Disable any jump label entries in __init code */
425void __init jump_label_invalidate_init(void)
426{
427 struct jump_entry *iter_start = __start___jump_table;
428 struct jump_entry *iter_stop = __stop___jump_table;
429 struct jump_entry *iter;
430
431 for (iter = iter_start; iter < iter_stop; iter++) {
9fbcc57a 432 if (init_kernel_text(iter->code))
33352244
JP
433 iter->code = 0;
434 }
435}
436
bf5438fc
JB
437#ifdef CONFIG_MODULES
438
11276d53
PZ
439static enum jump_label_type jump_label_init_type(struct jump_entry *entry)
440{
441 struct static_key *key = jump_entry_key(entry);
442 bool type = static_key_type(key);
443 bool branch = jump_entry_branch(entry);
444
445 /* See the comment in linux/jump_label.h */
446 return type ^ branch;
447}
448
c5905afb
IM
449struct static_key_mod {
450 struct static_key_mod *next;
d430d3d7
JB
451 struct jump_entry *entries;
452 struct module *mod;
453};
454
3821fd35
JB
455static inline struct static_key_mod *static_key_mod(struct static_key *key)
456{
457 WARN_ON_ONCE(!(key->type & JUMP_TYPE_LINKED));
458 return (struct static_key_mod *)(key->type & ~JUMP_TYPE_MASK);
459}
460
461/***
462 * key->type and key->next are the same via union.
463 * This sets key->next and preserves the type bits.
464 *
465 * See additional comments above static_key_set_entries().
466 */
467static void static_key_set_mod(struct static_key *key,
468 struct static_key_mod *mod)
469{
470 unsigned long type;
471
472 WARN_ON_ONCE((unsigned long)mod & JUMP_TYPE_MASK);
473 type = key->type & JUMP_TYPE_MASK;
474 key->next = mod;
475 key->type |= type;
476}
477
d430d3d7
JB
478static int __jump_label_mod_text_reserved(void *start, void *end)
479{
480 struct module *mod;
481
bdc9f373 482 preempt_disable();
d430d3d7 483 mod = __module_text_address((unsigned long)start);
bdc9f373
RR
484 WARN_ON_ONCE(__module_text_address((unsigned long)end) != mod);
485 preempt_enable();
486
d430d3d7
JB
487 if (!mod)
488 return 0;
489
d430d3d7
JB
490
491 return __jump_label_text_reserved(mod->jump_entries,
492 mod->jump_entries + mod->num_jump_entries,
493 start, end);
494}
495
706249c2 496static void __jump_label_mod_update(struct static_key *key)
d430d3d7 497{
706249c2 498 struct static_key_mod *mod;
d430d3d7 499
3821fd35
JB
500 for (mod = static_key_mod(key); mod; mod = mod->next) {
501 struct jump_entry *stop;
502 struct module *m;
503
504 /*
505 * NULL if the static_key is defined in a module
506 * that does not use it
507 */
508 if (!mod->entries)
509 continue;
7cbc5b8d 510
3821fd35
JB
511 m = mod->mod;
512 if (!m)
513 stop = __stop___jump_table;
514 else
515 stop = m->jump_entries + m->num_jump_entries;
516 __jump_label_update(key, mod->entries, stop);
d430d3d7
JB
517 }
518}
519
520/***
521 * apply_jump_label_nops - patch module jump labels with arch_get_jump_label_nop()
522 * @mod: module to patch
523 *
524 * Allow for run-time selection of the optimal nops. Before the module
525 * loads patch these with arch_get_jump_label_nop(), which is specified by
526 * the arch specific jump label code.
527 */
528void jump_label_apply_nops(struct module *mod)
bf5438fc 529{
d430d3d7
JB
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
534 /* if the module doesn't have jump label entries, just return */
535 if (iter_start == iter_stop)
536 return;
537
11276d53
PZ
538 for (iter = iter_start; iter < iter_stop; iter++) {
539 /* Only write NOPs for arch_branch_static(). */
540 if (jump_label_init_type(iter) == JUMP_LABEL_NOP)
541 arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
542 }
bf5438fc
JB
543}
544
d430d3d7 545static int jump_label_add_module(struct module *mod)
bf5438fc 546{
d430d3d7
JB
547 struct jump_entry *iter_start = mod->jump_entries;
548 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
549 struct jump_entry *iter;
c5905afb 550 struct static_key *key = NULL;
3821fd35 551 struct static_key_mod *jlm, *jlm2;
bf5438fc
JB
552
553 /* if the module doesn't have jump label entries, just return */
d430d3d7 554 if (iter_start == iter_stop)
bf5438fc
JB
555 return 0;
556
d430d3d7
JB
557 jump_label_sort_entries(iter_start, iter_stop);
558
559 for (iter = iter_start; iter < iter_stop; iter++) {
c5905afb 560 struct static_key *iterk;
d430d3d7 561
7dcfd915 562 iterk = jump_entry_key(iter);
c5905afb
IM
563 if (iterk == key)
564 continue;
d430d3d7 565
c5905afb 566 key = iterk;
bed831f9 567 if (within_module(iter->key, mod)) {
3821fd35 568 static_key_set_entries(key, iter);
d430d3d7 569 continue;
bf5438fc 570 }
c5905afb 571 jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL);
d430d3d7
JB
572 if (!jlm)
573 return -ENOMEM;
3821fd35
JB
574 if (!static_key_linked(key)) {
575 jlm2 = kzalloc(sizeof(struct static_key_mod),
576 GFP_KERNEL);
577 if (!jlm2) {
578 kfree(jlm);
579 return -ENOMEM;
580 }
581 preempt_disable();
582 jlm2->mod = __module_address((unsigned long)key);
583 preempt_enable();
584 jlm2->entries = static_key_entries(key);
585 jlm2->next = NULL;
586 static_key_set_mod(key, jlm2);
587 static_key_set_linked(key);
588 }
d430d3d7
JB
589 jlm->mod = mod;
590 jlm->entries = iter;
3821fd35
JB
591 jlm->next = static_key_mod(key);
592 static_key_set_mod(key, jlm);
593 static_key_set_linked(key);
d430d3d7 594
11276d53
PZ
595 /* Only update if we've changed from our initial state */
596 if (jump_label_type(iter) != jump_label_init_type(iter))
706249c2 597 __jump_label_update(key, iter, iter_stop);
bf5438fc 598 }
d430d3d7 599
bf5438fc
JB
600 return 0;
601}
602
d430d3d7 603static void jump_label_del_module(struct module *mod)
bf5438fc 604{
d430d3d7
JB
605 struct jump_entry *iter_start = mod->jump_entries;
606 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
607 struct jump_entry *iter;
c5905afb
IM
608 struct static_key *key = NULL;
609 struct static_key_mod *jlm, **prev;
bf5438fc 610
d430d3d7 611 for (iter = iter_start; iter < iter_stop; iter++) {
7dcfd915 612 if (jump_entry_key(iter) == key)
d430d3d7
JB
613 continue;
614
7dcfd915 615 key = jump_entry_key(iter);
d430d3d7 616
bed831f9 617 if (within_module(iter->key, mod))
d430d3d7
JB
618 continue;
619
3821fd35
JB
620 /* No memory during module load */
621 if (WARN_ON(!static_key_linked(key)))
622 continue;
623
d430d3d7 624 prev = &key->next;
3821fd35 625 jlm = static_key_mod(key);
bf5438fc 626
d430d3d7
JB
627 while (jlm && jlm->mod != mod) {
628 prev = &jlm->next;
629 jlm = jlm->next;
630 }
631
3821fd35
JB
632 /* No memory during module load */
633 if (WARN_ON(!jlm))
634 continue;
635
636 if (prev == &key->next)
637 static_key_set_mod(key, jlm->next);
638 else
d430d3d7 639 *prev = jlm->next;
3821fd35
JB
640
641 kfree(jlm);
642
643 jlm = static_key_mod(key);
644 /* if only one etry is left, fold it back into the static_key */
645 if (jlm->next == NULL) {
646 static_key_set_entries(key, jlm->entries);
647 static_key_clear_linked(key);
d430d3d7 648 kfree(jlm);
bf5438fc
JB
649 }
650 }
651}
652
33352244 653/* Disable any jump label entries in module init code */
d430d3d7 654static void jump_label_invalidate_module_init(struct module *mod)
b842f8fa 655{
d430d3d7
JB
656 struct jump_entry *iter_start = mod->jump_entries;
657 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
b842f8fa 658 struct jump_entry *iter;
b842f8fa 659
d430d3d7
JB
660 for (iter = iter_start; iter < iter_stop; iter++) {
661 if (within_module_init(iter->code, mod))
662 iter->code = 0;
b842f8fa
JB
663 }
664}
665
bf5438fc
JB
666static int
667jump_label_module_notify(struct notifier_block *self, unsigned long val,
668 void *data)
669{
670 struct module *mod = data;
671 int ret = 0;
672
f2545b2d
TG
673 cpus_read_lock();
674 jump_label_lock();
675
bf5438fc
JB
676 switch (val) {
677 case MODULE_STATE_COMING:
d430d3d7 678 ret = jump_label_add_module(mod);
3821fd35
JB
679 if (ret) {
680 WARN(1, "Failed to allocatote memory: jump_label may not work properly.\n");
d430d3d7 681 jump_label_del_module(mod);
3821fd35 682 }
bf5438fc
JB
683 break;
684 case MODULE_STATE_GOING:
d430d3d7 685 jump_label_del_module(mod);
bf5438fc 686 break;
b842f8fa 687 case MODULE_STATE_LIVE:
d430d3d7 688 jump_label_invalidate_module_init(mod);
b842f8fa 689 break;
bf5438fc 690 }
bf5438fc 691
f2545b2d
TG
692 jump_label_unlock();
693 cpus_read_unlock();
694
d430d3d7 695 return notifier_from_errno(ret);
bf5438fc
JB
696}
697
885885f6 698static struct notifier_block jump_label_module_nb = {
bf5438fc 699 .notifier_call = jump_label_module_notify,
d430d3d7 700 .priority = 1, /* higher than tracepoints */
bf5438fc
JB
701};
702
d430d3d7 703static __init int jump_label_init_module(void)
bf5438fc
JB
704{
705 return register_module_notifier(&jump_label_module_nb);
706}
d430d3d7 707early_initcall(jump_label_init_module);
bf5438fc
JB
708
709#endif /* CONFIG_MODULES */
710
d430d3d7
JB
711/***
712 * jump_label_text_reserved - check if addr range is reserved
713 * @start: start text addr
714 * @end: end text addr
715 *
716 * checks if the text addr located between @start and @end
717 * overlaps with any of the jump label patch addresses. Code
718 * that wants to modify kernel text should first verify that
719 * it does not overlap with any of the jump label addresses.
720 * Caller must hold jump_label_mutex.
721 *
722 * returns 1 if there is an overlap, 0 otherwise
723 */
724int jump_label_text_reserved(void *start, void *end)
725{
726 int ret = __jump_label_text_reserved(__start___jump_table,
727 __stop___jump_table, start, end);
728
729 if (ret)
730 return ret;
731
732#ifdef CONFIG_MODULES
733 ret = __jump_label_mod_text_reserved(start, end);
734#endif
735 return ret;
736}
737
706249c2 738static void jump_label_update(struct static_key *key)
d430d3d7 739{
c5905afb 740 struct jump_entry *stop = __stop___jump_table;
3821fd35 741 struct jump_entry *entry;
d430d3d7 742#ifdef CONFIG_MODULES
bed831f9 743 struct module *mod;
140fe3b1 744
3821fd35
JB
745 if (static_key_linked(key)) {
746 __jump_label_mod_update(key);
747 return;
748 }
140fe3b1 749
bed831f9
PZ
750 preempt_disable();
751 mod = __module_address((unsigned long)key);
140fe3b1
XG
752 if (mod)
753 stop = mod->jump_entries + mod->num_jump_entries;
bed831f9 754 preempt_enable();
d430d3d7 755#endif
3821fd35 756 entry = static_key_entries(key);
140fe3b1
XG
757 /* if there are no users, entry can be NULL */
758 if (entry)
706249c2 759 __jump_label_update(key, entry, stop);
d430d3d7
JB
760}
761
1987c947
PZ
762#ifdef CONFIG_STATIC_KEYS_SELFTEST
763static DEFINE_STATIC_KEY_TRUE(sk_true);
764static DEFINE_STATIC_KEY_FALSE(sk_false);
765
766static __init int jump_label_test(void)
767{
768 int i;
769
770 for (i = 0; i < 2; i++) {
771 WARN_ON(static_key_enabled(&sk_true.key) != true);
772 WARN_ON(static_key_enabled(&sk_false.key) != false);
773
774 WARN_ON(!static_branch_likely(&sk_true));
775 WARN_ON(!static_branch_unlikely(&sk_true));
776 WARN_ON(static_branch_likely(&sk_false));
777 WARN_ON(static_branch_unlikely(&sk_false));
778
779 static_branch_disable(&sk_true);
780 static_branch_enable(&sk_false);
781
782 WARN_ON(static_key_enabled(&sk_true.key) == true);
783 WARN_ON(static_key_enabled(&sk_false.key) == false);
784
785 WARN_ON(static_branch_likely(&sk_true));
786 WARN_ON(static_branch_unlikely(&sk_true));
787 WARN_ON(!static_branch_likely(&sk_false));
788 WARN_ON(!static_branch_unlikely(&sk_false));
789
790 static_branch_enable(&sk_true);
791 static_branch_disable(&sk_false);
792 }
793
794 return 0;
795}
92ee46ef 796early_initcall(jump_label_test);
1987c947
PZ
797#endif /* STATIC_KEYS_SELFTEST */
798
799#endif /* HAVE_JUMP_LABEL */