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