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