]>
Commit | Line | Data |
---|---|---|
bf5438fc JB |
1 | /* |
2 | * jump label support | |
3 | * | |
4 | * Copyright (C) 2009 Jason Baron <[email protected]> | |
d430d3d7 | 5 | * Copyright (C) 2011 Peter Zijlstra <[email protected]> |
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> |
bf5438fc JB |
17 | |
18 | #ifdef HAVE_JUMP_LABEL | |
19 | ||
bf5438fc JB |
20 | /* mutex to protect coming/going of the the jump_label table */ |
21 | static DEFINE_MUTEX(jump_label_mutex); | |
22 | ||
91bad2f8 JB |
23 | void jump_label_lock(void) |
24 | { | |
25 | mutex_lock(&jump_label_mutex); | |
26 | } | |
27 | ||
28 | void jump_label_unlock(void) | |
29 | { | |
30 | mutex_unlock(&jump_label_mutex); | |
31 | } | |
32 | ||
bf5438fc JB |
33 | static int jump_label_cmp(const void *a, const void *b) |
34 | { | |
35 | const struct jump_entry *jea = a; | |
36 | const struct jump_entry *jeb = b; | |
37 | ||
38 | if (jea->key < jeb->key) | |
39 | return -1; | |
40 | ||
41 | if (jea->key > jeb->key) | |
42 | return 1; | |
43 | ||
44 | return 0; | |
45 | } | |
46 | ||
47 | static void | |
d430d3d7 | 48 | jump_label_sort_entries(struct jump_entry *start, struct jump_entry *stop) |
bf5438fc JB |
49 | { |
50 | unsigned long size; | |
51 | ||
52 | size = (((unsigned long)stop - (unsigned long)start) | |
53 | / sizeof(struct jump_entry)); | |
54 | sort(start, size, sizeof(struct jump_entry), jump_label_cmp, NULL); | |
55 | } | |
56 | ||
c5905afb | 57 | static void jump_label_update(struct static_key *key, int enable); |
bf5438fc | 58 | |
c5905afb | 59 | void static_key_slow_inc(struct static_key *key) |
bf5438fc | 60 | { |
d430d3d7 JB |
61 | if (atomic_inc_not_zero(&key->enabled)) |
62 | return; | |
bf5438fc | 63 | |
d430d3d7 | 64 | jump_label_lock(); |
c5905afb IM |
65 | if (atomic_read(&key->enabled) == 0) { |
66 | if (!jump_label_get_branch_default(key)) | |
67 | jump_label_update(key, JUMP_LABEL_ENABLE); | |
68 | else | |
69 | jump_label_update(key, JUMP_LABEL_DISABLE); | |
70 | } | |
bbbf7af4 | 71 | atomic_inc(&key->enabled); |
d430d3d7 | 72 | jump_label_unlock(); |
bf5438fc | 73 | } |
c5905afb | 74 | EXPORT_SYMBOL_GPL(static_key_slow_inc); |
bf5438fc | 75 | |
c5905afb | 76 | static void __static_key_slow_dec(struct static_key *key, |
b2029520 | 77 | unsigned long rate_limit, struct delayed_work *work) |
bf5438fc | 78 | { |
fadf0464 JB |
79 | if (!atomic_dec_and_mutex_lock(&key->enabled, &jump_label_mutex)) { |
80 | WARN(atomic_read(&key->enabled) < 0, | |
81 | "jump label: negative count!\n"); | |
d430d3d7 | 82 | return; |
fadf0464 | 83 | } |
bf5438fc | 84 | |
b2029520 GN |
85 | if (rate_limit) { |
86 | atomic_inc(&key->enabled); | |
87 | schedule_delayed_work(work, rate_limit); | |
c5905afb IM |
88 | } else { |
89 | if (!jump_label_get_branch_default(key)) | |
90 | jump_label_update(key, JUMP_LABEL_DISABLE); | |
91 | else | |
92 | jump_label_update(key, JUMP_LABEL_ENABLE); | |
93 | } | |
91bad2f8 | 94 | jump_label_unlock(); |
bf5438fc JB |
95 | } |
96 | ||
b2029520 GN |
97 | static void jump_label_update_timeout(struct work_struct *work) |
98 | { | |
c5905afb IM |
99 | struct static_key_deferred *key = |
100 | container_of(work, struct static_key_deferred, work.work); | |
101 | __static_key_slow_dec(&key->key, 0, NULL); | |
b2029520 GN |
102 | } |
103 | ||
c5905afb | 104 | void static_key_slow_dec(struct static_key *key) |
b2029520 | 105 | { |
c5905afb | 106 | __static_key_slow_dec(key, 0, NULL); |
b2029520 | 107 | } |
c5905afb | 108 | EXPORT_SYMBOL_GPL(static_key_slow_dec); |
b2029520 | 109 | |
c5905afb | 110 | void static_key_slow_dec_deferred(struct static_key_deferred *key) |
b2029520 | 111 | { |
c5905afb | 112 | __static_key_slow_dec(&key->key, key->timeout, &key->work); |
b2029520 | 113 | } |
c5905afb | 114 | EXPORT_SYMBOL_GPL(static_key_slow_dec_deferred); |
b2029520 | 115 | |
c5905afb | 116 | void jump_label_rate_limit(struct static_key_deferred *key, |
b2029520 GN |
117 | unsigned long rl) |
118 | { | |
119 | key->timeout = rl; | |
120 | INIT_DELAYED_WORK(&key->work, jump_label_update_timeout); | |
121 | } | |
a181dc14 | 122 | EXPORT_SYMBOL_GPL(jump_label_rate_limit); |
b2029520 | 123 | |
4c3ef6d7 JB |
124 | static int addr_conflict(struct jump_entry *entry, void *start, void *end) |
125 | { | |
126 | if (entry->code <= (unsigned long)end && | |
127 | entry->code + JUMP_LABEL_NOP_SIZE > (unsigned long)start) | |
128 | return 1; | |
129 | ||
130 | return 0; | |
131 | } | |
132 | ||
d430d3d7 JB |
133 | static int __jump_label_text_reserved(struct jump_entry *iter_start, |
134 | struct jump_entry *iter_stop, void *start, void *end) | |
4c3ef6d7 | 135 | { |
4c3ef6d7 | 136 | struct jump_entry *iter; |
4c3ef6d7 | 137 | |
4c3ef6d7 JB |
138 | iter = iter_start; |
139 | while (iter < iter_stop) { | |
d430d3d7 JB |
140 | if (addr_conflict(iter, start, end)) |
141 | return 1; | |
4c3ef6d7 JB |
142 | iter++; |
143 | } | |
144 | ||
d430d3d7 JB |
145 | return 0; |
146 | } | |
147 | ||
20284aa7 JF |
148 | /* |
149 | * Update code which is definitely not currently executing. | |
150 | * Architectures which need heavyweight synchronization to modify | |
151 | * running code can override this to make the non-live update case | |
152 | * cheaper. | |
153 | */ | |
9cdbe1cb | 154 | void __weak __init_or_module arch_jump_label_transform_static(struct jump_entry *entry, |
20284aa7 JF |
155 | enum jump_label_type type) |
156 | { | |
157 | arch_jump_label_transform(entry, type); | |
158 | } | |
159 | ||
c5905afb | 160 | static void __jump_label_update(struct static_key *key, |
7cbc5b8d JO |
161 | struct jump_entry *entry, |
162 | struct jump_entry *stop, int enable) | |
d430d3d7 | 163 | { |
7cbc5b8d JO |
164 | for (; (entry < stop) && |
165 | (entry->key == (jump_label_t)(unsigned long)key); | |
166 | entry++) { | |
d430d3d7 JB |
167 | /* |
168 | * entry->code set to 0 invalidates module init text sections | |
169 | * kernel_text_address() verifies we are not in core kernel | |
170 | * init code, see jump_label_invalidate_module_init(). | |
171 | */ | |
172 | if (entry->code && kernel_text_address(entry->code)) | |
173 | arch_jump_label_transform(entry, enable); | |
174 | } | |
4c3ef6d7 JB |
175 | } |
176 | ||
c5905afb IM |
177 | static enum jump_label_type jump_label_type(struct static_key *key) |
178 | { | |
179 | bool true_branch = jump_label_get_branch_default(key); | |
180 | bool state = static_key_enabled(key); | |
181 | ||
182 | if ((!true_branch && state) || (true_branch && !state)) | |
183 | return JUMP_LABEL_ENABLE; | |
184 | ||
185 | return JUMP_LABEL_DISABLE; | |
186 | } | |
187 | ||
97ce2c88 | 188 | void __init jump_label_init(void) |
bf5438fc | 189 | { |
bf5438fc JB |
190 | struct jump_entry *iter_start = __start___jump_table; |
191 | struct jump_entry *iter_stop = __stop___jump_table; | |
c5905afb | 192 | struct static_key *key = NULL; |
bf5438fc JB |
193 | struct jump_entry *iter; |
194 | ||
91bad2f8 | 195 | jump_label_lock(); |
d430d3d7 JB |
196 | jump_label_sort_entries(iter_start, iter_stop); |
197 | ||
198 | for (iter = iter_start; iter < iter_stop; iter++) { | |
c5905afb | 199 | struct static_key *iterk; |
37348804 | 200 | |
c5905afb IM |
201 | iterk = (struct static_key *)(unsigned long)iter->key; |
202 | arch_jump_label_transform_static(iter, jump_label_type(iterk)); | |
37348804 | 203 | if (iterk == key) |
d430d3d7 JB |
204 | continue; |
205 | ||
37348804 | 206 | key = iterk; |
c5905afb IM |
207 | /* |
208 | * Set key->entries to iter, but preserve JUMP_LABEL_TRUE_BRANCH. | |
209 | */ | |
210 | *((unsigned long *)&key->entries) += (unsigned long)iter; | |
d430d3d7 JB |
211 | #ifdef CONFIG_MODULES |
212 | key->next = NULL; | |
213 | #endif | |
bf5438fc | 214 | } |
91bad2f8 | 215 | jump_label_unlock(); |
bf5438fc | 216 | } |
bf5438fc JB |
217 | |
218 | #ifdef CONFIG_MODULES | |
219 | ||
c5905afb IM |
220 | struct static_key_mod { |
221 | struct static_key_mod *next; | |
d430d3d7 JB |
222 | struct jump_entry *entries; |
223 | struct module *mod; | |
224 | }; | |
225 | ||
226 | static int __jump_label_mod_text_reserved(void *start, void *end) | |
227 | { | |
228 | struct module *mod; | |
229 | ||
230 | mod = __module_text_address((unsigned long)start); | |
231 | if (!mod) | |
232 | return 0; | |
233 | ||
234 | WARN_ON_ONCE(__module_text_address((unsigned long)end) != mod); | |
235 | ||
236 | return __jump_label_text_reserved(mod->jump_entries, | |
237 | mod->jump_entries + mod->num_jump_entries, | |
238 | start, end); | |
239 | } | |
240 | ||
c5905afb | 241 | static void __jump_label_mod_update(struct static_key *key, int enable) |
d430d3d7 | 242 | { |
c5905afb | 243 | struct static_key_mod *mod = key->next; |
d430d3d7 JB |
244 | |
245 | while (mod) { | |
7cbc5b8d JO |
246 | struct module *m = mod->mod; |
247 | ||
248 | __jump_label_update(key, mod->entries, | |
249 | m->jump_entries + m->num_jump_entries, | |
250 | enable); | |
d430d3d7 JB |
251 | mod = mod->next; |
252 | } | |
253 | } | |
254 | ||
255 | /*** | |
256 | * apply_jump_label_nops - patch module jump labels with arch_get_jump_label_nop() | |
257 | * @mod: module to patch | |
258 | * | |
259 | * Allow for run-time selection of the optimal nops. Before the module | |
260 | * loads patch these with arch_get_jump_label_nop(), which is specified by | |
261 | * the arch specific jump label code. | |
262 | */ | |
263 | void jump_label_apply_nops(struct module *mod) | |
bf5438fc | 264 | { |
d430d3d7 JB |
265 | struct jump_entry *iter_start = mod->jump_entries; |
266 | struct jump_entry *iter_stop = iter_start + mod->num_jump_entries; | |
267 | struct jump_entry *iter; | |
268 | ||
269 | /* if the module doesn't have jump label entries, just return */ | |
270 | if (iter_start == iter_stop) | |
271 | return; | |
272 | ||
ac99b862 | 273 | for (iter = iter_start; iter < iter_stop; iter++) { |
c5905afb | 274 | arch_jump_label_transform_static(iter, JUMP_LABEL_DISABLE); |
ac99b862 | 275 | } |
bf5438fc JB |
276 | } |
277 | ||
d430d3d7 | 278 | static int jump_label_add_module(struct module *mod) |
bf5438fc | 279 | { |
d430d3d7 JB |
280 | struct jump_entry *iter_start = mod->jump_entries; |
281 | struct jump_entry *iter_stop = iter_start + mod->num_jump_entries; | |
282 | struct jump_entry *iter; | |
c5905afb IM |
283 | struct static_key *key = NULL; |
284 | struct static_key_mod *jlm; | |
bf5438fc JB |
285 | |
286 | /* if the module doesn't have jump label entries, just return */ | |
d430d3d7 | 287 | if (iter_start == iter_stop) |
bf5438fc JB |
288 | return 0; |
289 | ||
d430d3d7 JB |
290 | jump_label_sort_entries(iter_start, iter_stop); |
291 | ||
292 | for (iter = iter_start; iter < iter_stop; iter++) { | |
c5905afb | 293 | struct static_key *iterk; |
d430d3d7 | 294 | |
c5905afb IM |
295 | iterk = (struct static_key *)(unsigned long)iter->key; |
296 | if (iterk == key) | |
297 | continue; | |
d430d3d7 | 298 | |
c5905afb | 299 | key = iterk; |
d430d3d7 | 300 | if (__module_address(iter->key) == mod) { |
c5905afb IM |
301 | /* |
302 | * Set key->entries to iter, but preserve JUMP_LABEL_TRUE_BRANCH. | |
303 | */ | |
304 | *((unsigned long *)&key->entries) += (unsigned long)iter; | |
d430d3d7 JB |
305 | key->next = NULL; |
306 | continue; | |
bf5438fc | 307 | } |
c5905afb | 308 | jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL); |
d430d3d7 JB |
309 | if (!jlm) |
310 | return -ENOMEM; | |
d430d3d7 JB |
311 | jlm->mod = mod; |
312 | jlm->entries = iter; | |
313 | jlm->next = key->next; | |
314 | key->next = jlm; | |
315 | ||
c5905afb | 316 | if (jump_label_type(key) == JUMP_LABEL_ENABLE) |
ac99b862 | 317 | __jump_label_update(key, iter, iter_stop, JUMP_LABEL_ENABLE); |
bf5438fc | 318 | } |
d430d3d7 | 319 | |
bf5438fc JB |
320 | return 0; |
321 | } | |
322 | ||
d430d3d7 | 323 | static void jump_label_del_module(struct module *mod) |
bf5438fc | 324 | { |
d430d3d7 JB |
325 | struct jump_entry *iter_start = mod->jump_entries; |
326 | struct jump_entry *iter_stop = iter_start + mod->num_jump_entries; | |
327 | struct jump_entry *iter; | |
c5905afb IM |
328 | struct static_key *key = NULL; |
329 | struct static_key_mod *jlm, **prev; | |
bf5438fc | 330 | |
d430d3d7 JB |
331 | for (iter = iter_start; iter < iter_stop; iter++) { |
332 | if (iter->key == (jump_label_t)(unsigned long)key) | |
333 | continue; | |
334 | ||
c5905afb | 335 | key = (struct static_key *)(unsigned long)iter->key; |
d430d3d7 JB |
336 | |
337 | if (__module_address(iter->key) == mod) | |
338 | continue; | |
339 | ||
340 | prev = &key->next; | |
341 | jlm = key->next; | |
bf5438fc | 342 | |
d430d3d7 JB |
343 | while (jlm && jlm->mod != mod) { |
344 | prev = &jlm->next; | |
345 | jlm = jlm->next; | |
346 | } | |
347 | ||
348 | if (jlm) { | |
349 | *prev = jlm->next; | |
350 | kfree(jlm); | |
bf5438fc JB |
351 | } |
352 | } | |
353 | } | |
354 | ||
d430d3d7 | 355 | static void jump_label_invalidate_module_init(struct module *mod) |
b842f8fa | 356 | { |
d430d3d7 JB |
357 | struct jump_entry *iter_start = mod->jump_entries; |
358 | struct jump_entry *iter_stop = iter_start + mod->num_jump_entries; | |
b842f8fa | 359 | struct jump_entry *iter; |
b842f8fa | 360 | |
d430d3d7 JB |
361 | for (iter = iter_start; iter < iter_stop; iter++) { |
362 | if (within_module_init(iter->code, mod)) | |
363 | iter->code = 0; | |
b842f8fa JB |
364 | } |
365 | } | |
366 | ||
bf5438fc JB |
367 | static int |
368 | jump_label_module_notify(struct notifier_block *self, unsigned long val, | |
369 | void *data) | |
370 | { | |
371 | struct module *mod = data; | |
372 | int ret = 0; | |
373 | ||
374 | switch (val) { | |
375 | case MODULE_STATE_COMING: | |
91bad2f8 | 376 | jump_label_lock(); |
d430d3d7 | 377 | ret = jump_label_add_module(mod); |
bf5438fc | 378 | if (ret) |
d430d3d7 | 379 | jump_label_del_module(mod); |
91bad2f8 | 380 | jump_label_unlock(); |
bf5438fc JB |
381 | break; |
382 | case MODULE_STATE_GOING: | |
91bad2f8 | 383 | jump_label_lock(); |
d430d3d7 | 384 | jump_label_del_module(mod); |
91bad2f8 | 385 | jump_label_unlock(); |
bf5438fc | 386 | break; |
b842f8fa | 387 | case MODULE_STATE_LIVE: |
91bad2f8 | 388 | jump_label_lock(); |
d430d3d7 | 389 | jump_label_invalidate_module_init(mod); |
91bad2f8 | 390 | jump_label_unlock(); |
b842f8fa | 391 | break; |
bf5438fc | 392 | } |
bf5438fc | 393 | |
d430d3d7 | 394 | return notifier_from_errno(ret); |
bf5438fc JB |
395 | } |
396 | ||
397 | struct notifier_block jump_label_module_nb = { | |
398 | .notifier_call = jump_label_module_notify, | |
d430d3d7 | 399 | .priority = 1, /* higher than tracepoints */ |
bf5438fc JB |
400 | }; |
401 | ||
d430d3d7 | 402 | static __init int jump_label_init_module(void) |
bf5438fc JB |
403 | { |
404 | return register_module_notifier(&jump_label_module_nb); | |
405 | } | |
d430d3d7 | 406 | early_initcall(jump_label_init_module); |
bf5438fc JB |
407 | |
408 | #endif /* CONFIG_MODULES */ | |
409 | ||
d430d3d7 JB |
410 | /*** |
411 | * jump_label_text_reserved - check if addr range is reserved | |
412 | * @start: start text addr | |
413 | * @end: end text addr | |
414 | * | |
415 | * checks if the text addr located between @start and @end | |
416 | * overlaps with any of the jump label patch addresses. Code | |
417 | * that wants to modify kernel text should first verify that | |
418 | * it does not overlap with any of the jump label addresses. | |
419 | * Caller must hold jump_label_mutex. | |
420 | * | |
421 | * returns 1 if there is an overlap, 0 otherwise | |
422 | */ | |
423 | int jump_label_text_reserved(void *start, void *end) | |
424 | { | |
425 | int ret = __jump_label_text_reserved(__start___jump_table, | |
426 | __stop___jump_table, start, end); | |
427 | ||
428 | if (ret) | |
429 | return ret; | |
430 | ||
431 | #ifdef CONFIG_MODULES | |
432 | ret = __jump_label_mod_text_reserved(start, end); | |
433 | #endif | |
434 | return ret; | |
435 | } | |
436 | ||
c5905afb | 437 | static void jump_label_update(struct static_key *key, int enable) |
d430d3d7 | 438 | { |
c5905afb IM |
439 | struct jump_entry *stop = __stop___jump_table; |
440 | struct jump_entry *entry = jump_label_get_entries(key); | |
d430d3d7 JB |
441 | |
442 | #ifdef CONFIG_MODULES | |
a746e3cc | 443 | struct module *mod = __module_address((unsigned long)key); |
140fe3b1 | 444 | |
d430d3d7 | 445 | __jump_label_mod_update(key, enable); |
140fe3b1 XG |
446 | |
447 | if (mod) | |
448 | stop = mod->jump_entries + mod->num_jump_entries; | |
d430d3d7 | 449 | #endif |
140fe3b1 XG |
450 | /* if there are no users, entry can be NULL */ |
451 | if (entry) | |
452 | __jump_label_update(key, entry, stop, enable); | |
d430d3d7 JB |
453 | } |
454 | ||
bf5438fc | 455 | #endif |