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