]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - kernel/jump_label.c
powerpc/mm: Convert early cpu/mmu feature check to use the new helpers
[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>
bf5438fc
JB
17
18#ifdef HAVE_JUMP_LABEL
19
bf5438fc
JB
20/* mutex to protect coming/going of the the jump_label table */
21static DEFINE_MUTEX(jump_label_mutex);
22
91bad2f8
JB
23void jump_label_lock(void)
24{
25 mutex_lock(&jump_label_mutex);
26}
27
28void jump_label_unlock(void)
29{
30 mutex_unlock(&jump_label_mutex);
31}
32
bf5438fc
JB
33static 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
47static void
d430d3d7 48jump_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
706249c2 57static void jump_label_update(struct static_key *key);
a1efb01f 58
c5905afb 59void static_key_slow_inc(struct static_key *key)
bf5438fc 60{
4c5ea0a9
PB
61 int v, v1;
62
c4b2c0c5 63 STATIC_KEY_CHECK_USE();
4c5ea0a9
PB
64
65 /*
66 * Careful if we get concurrent static_key_slow_inc() calls;
67 * later calls must wait for the first one to _finish_ the
68 * jump_label_update() process. At the same time, however,
69 * the jump_label_update() call below wants to see
70 * static_key_enabled(&key) for jumps to be updated properly.
71 *
72 * So give a special meaning to negative key->enabled: it sends
73 * static_key_slow_inc() down the slow path, and it is non-zero
74 * so it counts as "enabled" in jump_label_update(). Note that
75 * atomic_inc_unless_negative() checks >= 0, so roll our own.
76 */
77 for (v = atomic_read(&key->enabled); v > 0; v = v1) {
78 v1 = atomic_cmpxchg(&key->enabled, v, v + 1);
79 if (likely(v1 == v))
80 return;
81 }
bf5438fc 82
d430d3d7 83 jump_label_lock();
4c5ea0a9
PB
84 if (atomic_read(&key->enabled) == 0) {
85 atomic_set(&key->enabled, -1);
706249c2 86 jump_label_update(key);
4c5ea0a9
PB
87 atomic_set(&key->enabled, 1);
88 } else {
89 atomic_inc(&key->enabled);
90 }
d430d3d7 91 jump_label_unlock();
bf5438fc 92}
c5905afb 93EXPORT_SYMBOL_GPL(static_key_slow_inc);
bf5438fc 94
c5905afb 95static void __static_key_slow_dec(struct static_key *key,
b2029520 96 unsigned long rate_limit, struct delayed_work *work)
bf5438fc 97{
4c5ea0a9
PB
98 /*
99 * The negative count check is valid even when a negative
100 * key->enabled is in use by static_key_slow_inc(); a
101 * __static_key_slow_dec() before the first static_key_slow_inc()
102 * returns is unbalanced, because all other static_key_slow_inc()
103 * instances block while the update is in progress.
104 */
fadf0464
JB
105 if (!atomic_dec_and_mutex_lock(&key->enabled, &jump_label_mutex)) {
106 WARN(atomic_read(&key->enabled) < 0,
107 "jump label: negative count!\n");
d430d3d7 108 return;
fadf0464 109 }
bf5438fc 110
b2029520
GN
111 if (rate_limit) {
112 atomic_inc(&key->enabled);
113 schedule_delayed_work(work, rate_limit);
c5905afb 114 } else {
706249c2 115 jump_label_update(key);
c5905afb 116 }
91bad2f8 117 jump_label_unlock();
bf5438fc
JB
118}
119
b2029520
GN
120static void jump_label_update_timeout(struct work_struct *work)
121{
c5905afb
IM
122 struct static_key_deferred *key =
123 container_of(work, struct static_key_deferred, work.work);
124 __static_key_slow_dec(&key->key, 0, NULL);
b2029520
GN
125}
126
c5905afb 127void static_key_slow_dec(struct static_key *key)
b2029520 128{
c4b2c0c5 129 STATIC_KEY_CHECK_USE();
c5905afb 130 __static_key_slow_dec(key, 0, NULL);
b2029520 131}
c5905afb 132EXPORT_SYMBOL_GPL(static_key_slow_dec);
b2029520 133
c5905afb 134void static_key_slow_dec_deferred(struct static_key_deferred *key)
b2029520 135{
c4b2c0c5 136 STATIC_KEY_CHECK_USE();
c5905afb 137 __static_key_slow_dec(&key->key, key->timeout, &key->work);
b2029520 138}
c5905afb 139EXPORT_SYMBOL_GPL(static_key_slow_dec_deferred);
b2029520 140
c5905afb 141void jump_label_rate_limit(struct static_key_deferred *key,
b2029520
GN
142 unsigned long rl)
143{
c4b2c0c5 144 STATIC_KEY_CHECK_USE();
b2029520
GN
145 key->timeout = rl;
146 INIT_DELAYED_WORK(&key->work, jump_label_update_timeout);
147}
a181dc14 148EXPORT_SYMBOL_GPL(jump_label_rate_limit);
b2029520 149
4c3ef6d7
JB
150static int addr_conflict(struct jump_entry *entry, void *start, void *end)
151{
152 if (entry->code <= (unsigned long)end &&
153 entry->code + JUMP_LABEL_NOP_SIZE > (unsigned long)start)
154 return 1;
155
156 return 0;
157}
158
d430d3d7
JB
159static int __jump_label_text_reserved(struct jump_entry *iter_start,
160 struct jump_entry *iter_stop, void *start, void *end)
4c3ef6d7 161{
4c3ef6d7 162 struct jump_entry *iter;
4c3ef6d7 163
4c3ef6d7
JB
164 iter = iter_start;
165 while (iter < iter_stop) {
d430d3d7
JB
166 if (addr_conflict(iter, start, end))
167 return 1;
4c3ef6d7
JB
168 iter++;
169 }
170
d430d3d7
JB
171 return 0;
172}
173
706249c2 174/*
20284aa7
JF
175 * Update code which is definitely not currently executing.
176 * Architectures which need heavyweight synchronization to modify
177 * running code can override this to make the non-live update case
178 * cheaper.
179 */
9cdbe1cb 180void __weak __init_or_module arch_jump_label_transform_static(struct jump_entry *entry,
20284aa7
JF
181 enum jump_label_type type)
182{
706249c2 183 arch_jump_label_transform(entry, type);
20284aa7
JF
184}
185
706249c2 186static inline struct jump_entry *static_key_entries(struct static_key *key)
d430d3d7 187{
706249c2 188 return (struct jump_entry *)((unsigned long)key->entries & ~JUMP_TYPE_MASK);
4c3ef6d7
JB
189}
190
706249c2 191static inline bool static_key_type(struct static_key *key)
c5905afb 192{
706249c2 193 return (unsigned long)key->entries & JUMP_TYPE_MASK;
a1efb01f 194}
c5905afb 195
7dcfd915
PZ
196static inline struct static_key *jump_entry_key(struct jump_entry *entry)
197{
11276d53
PZ
198 return (struct static_key *)((unsigned long)entry->key & ~1UL);
199}
200
201static bool jump_entry_branch(struct jump_entry *entry)
202{
203 return (unsigned long)entry->key & 1UL;
7dcfd915
PZ
204}
205
706249c2 206static enum jump_label_type jump_label_type(struct jump_entry *entry)
a1efb01f 207{
706249c2 208 struct static_key *key = jump_entry_key(entry);
a1efb01f 209 bool enabled = static_key_enabled(key);
11276d53 210 bool branch = jump_entry_branch(entry);
c5905afb 211
11276d53
PZ
212 /* See the comment in linux/jump_label.h */
213 return enabled ^ branch;
c5905afb
IM
214}
215
706249c2
PZ
216static void __jump_label_update(struct static_key *key,
217 struct jump_entry *entry,
218 struct jump_entry *stop)
219{
220 for (; (entry < stop) && (jump_entry_key(entry) == key); entry++) {
221 /*
222 * entry->code set to 0 invalidates module init text sections
223 * kernel_text_address() verifies we are not in core kernel
224 * init code, see jump_label_invalidate_module_init().
225 */
226 if (entry->code && kernel_text_address(entry->code))
227 arch_jump_label_transform(entry, jump_label_type(entry));
228 }
229}
230
97ce2c88 231void __init jump_label_init(void)
bf5438fc 232{
bf5438fc
JB
233 struct jump_entry *iter_start = __start___jump_table;
234 struct jump_entry *iter_stop = __stop___jump_table;
c5905afb 235 struct static_key *key = NULL;
bf5438fc
JB
236 struct jump_entry *iter;
237
91bad2f8 238 jump_label_lock();
d430d3d7
JB
239 jump_label_sort_entries(iter_start, iter_stop);
240
241 for (iter = iter_start; iter < iter_stop; iter++) {
c5905afb 242 struct static_key *iterk;
37348804 243
11276d53
PZ
244 /* rewrite NOPs */
245 if (jump_label_type(iter) == JUMP_LABEL_NOP)
246 arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
247
7dcfd915 248 iterk = jump_entry_key(iter);
37348804 249 if (iterk == key)
d430d3d7
JB
250 continue;
251
37348804 252 key = iterk;
c5905afb
IM
253 /*
254 * Set key->entries to iter, but preserve JUMP_LABEL_TRUE_BRANCH.
255 */
256 *((unsigned long *)&key->entries) += (unsigned long)iter;
d430d3d7
JB
257#ifdef CONFIG_MODULES
258 key->next = NULL;
259#endif
bf5438fc 260 }
c4b2c0c5 261 static_key_initialized = true;
91bad2f8 262 jump_label_unlock();
bf5438fc 263}
bf5438fc
JB
264
265#ifdef CONFIG_MODULES
266
11276d53
PZ
267static enum jump_label_type jump_label_init_type(struct jump_entry *entry)
268{
269 struct static_key *key = jump_entry_key(entry);
270 bool type = static_key_type(key);
271 bool branch = jump_entry_branch(entry);
272
273 /* See the comment in linux/jump_label.h */
274 return type ^ branch;
275}
276
c5905afb
IM
277struct static_key_mod {
278 struct static_key_mod *next;
d430d3d7
JB
279 struct jump_entry *entries;
280 struct module *mod;
281};
282
283static int __jump_label_mod_text_reserved(void *start, void *end)
284{
285 struct module *mod;
286
287 mod = __module_text_address((unsigned long)start);
288 if (!mod)
289 return 0;
290
291 WARN_ON_ONCE(__module_text_address((unsigned long)end) != mod);
292
293 return __jump_label_text_reserved(mod->jump_entries,
294 mod->jump_entries + mod->num_jump_entries,
295 start, end);
296}
297
706249c2 298static void __jump_label_mod_update(struct static_key *key)
d430d3d7 299{
706249c2 300 struct static_key_mod *mod;
d430d3d7 301
706249c2 302 for (mod = key->next; mod; mod = mod->next) {
7cbc5b8d
JO
303 struct module *m = mod->mod;
304
305 __jump_label_update(key, mod->entries,
706249c2 306 m->jump_entries + m->num_jump_entries);
d430d3d7
JB
307 }
308}
309
310/***
311 * apply_jump_label_nops - patch module jump labels with arch_get_jump_label_nop()
312 * @mod: module to patch
313 *
314 * Allow for run-time selection of the optimal nops. Before the module
315 * loads patch these with arch_get_jump_label_nop(), which is specified by
316 * the arch specific jump label code.
317 */
318void jump_label_apply_nops(struct module *mod)
bf5438fc 319{
d430d3d7
JB
320 struct jump_entry *iter_start = mod->jump_entries;
321 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
322 struct jump_entry *iter;
323
324 /* if the module doesn't have jump label entries, just return */
325 if (iter_start == iter_stop)
326 return;
327
11276d53
PZ
328 for (iter = iter_start; iter < iter_stop; iter++) {
329 /* Only write NOPs for arch_branch_static(). */
330 if (jump_label_init_type(iter) == JUMP_LABEL_NOP)
331 arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
332 }
bf5438fc
JB
333}
334
d430d3d7 335static int jump_label_add_module(struct module *mod)
bf5438fc 336{
d430d3d7
JB
337 struct jump_entry *iter_start = mod->jump_entries;
338 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
339 struct jump_entry *iter;
c5905afb
IM
340 struct static_key *key = NULL;
341 struct static_key_mod *jlm;
bf5438fc
JB
342
343 /* if the module doesn't have jump label entries, just return */
d430d3d7 344 if (iter_start == iter_stop)
bf5438fc
JB
345 return 0;
346
d430d3d7
JB
347 jump_label_sort_entries(iter_start, iter_stop);
348
349 for (iter = iter_start; iter < iter_stop; iter++) {
c5905afb 350 struct static_key *iterk;
d430d3d7 351
7dcfd915 352 iterk = jump_entry_key(iter);
c5905afb
IM
353 if (iterk == key)
354 continue;
d430d3d7 355
c5905afb 356 key = iterk;
bed831f9 357 if (within_module(iter->key, mod)) {
c5905afb
IM
358 /*
359 * Set key->entries to iter, but preserve JUMP_LABEL_TRUE_BRANCH.
360 */
361 *((unsigned long *)&key->entries) += (unsigned long)iter;
d430d3d7
JB
362 key->next = NULL;
363 continue;
bf5438fc 364 }
c5905afb 365 jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL);
d430d3d7
JB
366 if (!jlm)
367 return -ENOMEM;
d430d3d7
JB
368 jlm->mod = mod;
369 jlm->entries = iter;
370 jlm->next = key->next;
371 key->next = jlm;
372
11276d53
PZ
373 /* Only update if we've changed from our initial state */
374 if (jump_label_type(iter) != jump_label_init_type(iter))
706249c2 375 __jump_label_update(key, iter, iter_stop);
bf5438fc 376 }
d430d3d7 377
bf5438fc
JB
378 return 0;
379}
380
d430d3d7 381static void jump_label_del_module(struct module *mod)
bf5438fc 382{
d430d3d7
JB
383 struct jump_entry *iter_start = mod->jump_entries;
384 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
385 struct jump_entry *iter;
c5905afb
IM
386 struct static_key *key = NULL;
387 struct static_key_mod *jlm, **prev;
bf5438fc 388
d430d3d7 389 for (iter = iter_start; iter < iter_stop; iter++) {
7dcfd915 390 if (jump_entry_key(iter) == key)
d430d3d7
JB
391 continue;
392
7dcfd915 393 key = jump_entry_key(iter);
d430d3d7 394
bed831f9 395 if (within_module(iter->key, mod))
d430d3d7
JB
396 continue;
397
398 prev = &key->next;
399 jlm = key->next;
bf5438fc 400
d430d3d7
JB
401 while (jlm && jlm->mod != mod) {
402 prev = &jlm->next;
403 jlm = jlm->next;
404 }
405
406 if (jlm) {
407 *prev = jlm->next;
408 kfree(jlm);
bf5438fc
JB
409 }
410 }
411}
412
d430d3d7 413static void jump_label_invalidate_module_init(struct module *mod)
b842f8fa 414{
d430d3d7
JB
415 struct jump_entry *iter_start = mod->jump_entries;
416 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
b842f8fa 417 struct jump_entry *iter;
b842f8fa 418
d430d3d7
JB
419 for (iter = iter_start; iter < iter_stop; iter++) {
420 if (within_module_init(iter->code, mod))
421 iter->code = 0;
b842f8fa
JB
422 }
423}
424
bf5438fc
JB
425static int
426jump_label_module_notify(struct notifier_block *self, unsigned long val,
427 void *data)
428{
429 struct module *mod = data;
430 int ret = 0;
431
432 switch (val) {
433 case MODULE_STATE_COMING:
91bad2f8 434 jump_label_lock();
d430d3d7 435 ret = jump_label_add_module(mod);
bf5438fc 436 if (ret)
d430d3d7 437 jump_label_del_module(mod);
91bad2f8 438 jump_label_unlock();
bf5438fc
JB
439 break;
440 case MODULE_STATE_GOING:
91bad2f8 441 jump_label_lock();
d430d3d7 442 jump_label_del_module(mod);
91bad2f8 443 jump_label_unlock();
bf5438fc 444 break;
b842f8fa 445 case MODULE_STATE_LIVE:
91bad2f8 446 jump_label_lock();
d430d3d7 447 jump_label_invalidate_module_init(mod);
91bad2f8 448 jump_label_unlock();
b842f8fa 449 break;
bf5438fc 450 }
bf5438fc 451
d430d3d7 452 return notifier_from_errno(ret);
bf5438fc
JB
453}
454
885885f6 455static struct notifier_block jump_label_module_nb = {
bf5438fc 456 .notifier_call = jump_label_module_notify,
d430d3d7 457 .priority = 1, /* higher than tracepoints */
bf5438fc
JB
458};
459
d430d3d7 460static __init int jump_label_init_module(void)
bf5438fc
JB
461{
462 return register_module_notifier(&jump_label_module_nb);
463}
d430d3d7 464early_initcall(jump_label_init_module);
bf5438fc
JB
465
466#endif /* CONFIG_MODULES */
467
d430d3d7
JB
468/***
469 * jump_label_text_reserved - check if addr range is reserved
470 * @start: start text addr
471 * @end: end text addr
472 *
473 * checks if the text addr located between @start and @end
474 * overlaps with any of the jump label patch addresses. Code
475 * that wants to modify kernel text should first verify that
476 * it does not overlap with any of the jump label addresses.
477 * Caller must hold jump_label_mutex.
478 *
479 * returns 1 if there is an overlap, 0 otherwise
480 */
481int jump_label_text_reserved(void *start, void *end)
482{
483 int ret = __jump_label_text_reserved(__start___jump_table,
484 __stop___jump_table, start, end);
485
486 if (ret)
487 return ret;
488
489#ifdef CONFIG_MODULES
490 ret = __jump_label_mod_text_reserved(start, end);
491#endif
492 return ret;
493}
494
706249c2 495static void jump_label_update(struct static_key *key)
d430d3d7 496{
c5905afb 497 struct jump_entry *stop = __stop___jump_table;
a1efb01f 498 struct jump_entry *entry = static_key_entries(key);
d430d3d7 499#ifdef CONFIG_MODULES
bed831f9 500 struct module *mod;
140fe3b1 501
706249c2 502 __jump_label_mod_update(key);
140fe3b1 503
bed831f9
PZ
504 preempt_disable();
505 mod = __module_address((unsigned long)key);
140fe3b1
XG
506 if (mod)
507 stop = mod->jump_entries + mod->num_jump_entries;
bed831f9 508 preempt_enable();
d430d3d7 509#endif
140fe3b1
XG
510 /* if there are no users, entry can be NULL */
511 if (entry)
706249c2 512 __jump_label_update(key, entry, stop);
d430d3d7
JB
513}
514
1987c947
PZ
515#ifdef CONFIG_STATIC_KEYS_SELFTEST
516static DEFINE_STATIC_KEY_TRUE(sk_true);
517static DEFINE_STATIC_KEY_FALSE(sk_false);
518
519static __init int jump_label_test(void)
520{
521 int i;
522
523 for (i = 0; i < 2; i++) {
524 WARN_ON(static_key_enabled(&sk_true.key) != true);
525 WARN_ON(static_key_enabled(&sk_false.key) != false);
526
527 WARN_ON(!static_branch_likely(&sk_true));
528 WARN_ON(!static_branch_unlikely(&sk_true));
529 WARN_ON(static_branch_likely(&sk_false));
530 WARN_ON(static_branch_unlikely(&sk_false));
531
532 static_branch_disable(&sk_true);
533 static_branch_enable(&sk_false);
534
535 WARN_ON(static_key_enabled(&sk_true.key) == true);
536 WARN_ON(static_key_enabled(&sk_false.key) == false);
537
538 WARN_ON(static_branch_likely(&sk_true));
539 WARN_ON(static_branch_unlikely(&sk_true));
540 WARN_ON(!static_branch_likely(&sk_false));
541 WARN_ON(!static_branch_unlikely(&sk_false));
542
543 static_branch_enable(&sk_true);
544 static_branch_disable(&sk_false);
545 }
546
547 return 0;
548}
549late_initcall(jump_label_test);
550#endif /* STATIC_KEYS_SELFTEST */
551
552#endif /* HAVE_JUMP_LABEL */