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