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