]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - kernel/jump_label.c
UBUNTU: [config] update config for master changes
[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{
945c6e85
PB
61 int v, v1;
62
c4b2c0c5 63 STATIC_KEY_CHECK_USE();
945c6e85
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();
945c6e85
PB
84 if (atomic_read(&key->enabled) == 0) {
85 atomic_set(&key->enabled, -1);
706249c2 86 jump_label_update(key);
945c6e85
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{
945c6e85
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
de4d74e5
DM
141void static_key_deferred_flush(struct static_key_deferred *key)
142{
143 STATIC_KEY_CHECK_USE();
144 flush_delayed_work(&key->work);
145}
146EXPORT_SYMBOL_GPL(static_key_deferred_flush);
147
c5905afb 148void jump_label_rate_limit(struct static_key_deferred *key,
b2029520
GN
149 unsigned long rl)
150{
c4b2c0c5 151 STATIC_KEY_CHECK_USE();
b2029520
GN
152 key->timeout = rl;
153 INIT_DELAYED_WORK(&key->work, jump_label_update_timeout);
154}
a181dc14 155EXPORT_SYMBOL_GPL(jump_label_rate_limit);
b2029520 156
4c3ef6d7
JB
157static int addr_conflict(struct jump_entry *entry, void *start, void *end)
158{
159 if (entry->code <= (unsigned long)end &&
160 entry->code + JUMP_LABEL_NOP_SIZE > (unsigned long)start)
161 return 1;
162
163 return 0;
164}
165
d430d3d7
JB
166static int __jump_label_text_reserved(struct jump_entry *iter_start,
167 struct jump_entry *iter_stop, void *start, void *end)
4c3ef6d7 168{
4c3ef6d7 169 struct jump_entry *iter;
4c3ef6d7 170
4c3ef6d7
JB
171 iter = iter_start;
172 while (iter < iter_stop) {
d430d3d7
JB
173 if (addr_conflict(iter, start, end))
174 return 1;
4c3ef6d7
JB
175 iter++;
176 }
177
d430d3d7
JB
178 return 0;
179}
180
706249c2 181/*
20284aa7
JF
182 * Update code which is definitely not currently executing.
183 * Architectures which need heavyweight synchronization to modify
184 * running code can override this to make the non-live update case
185 * cheaper.
186 */
9cdbe1cb 187void __weak __init_or_module arch_jump_label_transform_static(struct jump_entry *entry,
20284aa7
JF
188 enum jump_label_type type)
189{
706249c2 190 arch_jump_label_transform(entry, type);
20284aa7
JF
191}
192
706249c2 193static inline struct jump_entry *static_key_entries(struct static_key *key)
d430d3d7 194{
706249c2 195 return (struct jump_entry *)((unsigned long)key->entries & ~JUMP_TYPE_MASK);
4c3ef6d7
JB
196}
197
706249c2 198static inline bool static_key_type(struct static_key *key)
c5905afb 199{
706249c2 200 return (unsigned long)key->entries & JUMP_TYPE_MASK;
a1efb01f 201}
c5905afb 202
7dcfd915
PZ
203static inline struct static_key *jump_entry_key(struct jump_entry *entry)
204{
11276d53
PZ
205 return (struct static_key *)((unsigned long)entry->key & ~1UL);
206}
207
208static bool jump_entry_branch(struct jump_entry *entry)
209{
210 return (unsigned long)entry->key & 1UL;
7dcfd915
PZ
211}
212
706249c2 213static enum jump_label_type jump_label_type(struct jump_entry *entry)
a1efb01f 214{
706249c2 215 struct static_key *key = jump_entry_key(entry);
a1efb01f 216 bool enabled = static_key_enabled(key);
11276d53 217 bool branch = jump_entry_branch(entry);
c5905afb 218
11276d53
PZ
219 /* See the comment in linux/jump_label.h */
220 return enabled ^ branch;
c5905afb
IM
221}
222
706249c2
PZ
223static void __jump_label_update(struct static_key *key,
224 struct jump_entry *entry,
225 struct jump_entry *stop)
226{
227 for (; (entry < stop) && (jump_entry_key(entry) == key); entry++) {
228 /*
229 * entry->code set to 0 invalidates module init text sections
230 * kernel_text_address() verifies we are not in core kernel
231 * init code, see jump_label_invalidate_module_init().
232 */
233 if (entry->code && kernel_text_address(entry->code))
234 arch_jump_label_transform(entry, jump_label_type(entry));
235 }
236}
237
97ce2c88 238void __init jump_label_init(void)
bf5438fc 239{
bf5438fc
JB
240 struct jump_entry *iter_start = __start___jump_table;
241 struct jump_entry *iter_stop = __stop___jump_table;
c5905afb 242 struct static_key *key = NULL;
bf5438fc
JB
243 struct jump_entry *iter;
244
91bad2f8 245 jump_label_lock();
d430d3d7
JB
246 jump_label_sort_entries(iter_start, iter_stop);
247
248 for (iter = iter_start; iter < iter_stop; iter++) {
c5905afb 249 struct static_key *iterk;
37348804 250
11276d53
PZ
251 /* rewrite NOPs */
252 if (jump_label_type(iter) == JUMP_LABEL_NOP)
253 arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
254
7dcfd915 255 iterk = jump_entry_key(iter);
37348804 256 if (iterk == key)
d430d3d7
JB
257 continue;
258
37348804 259 key = iterk;
c5905afb
IM
260 /*
261 * Set key->entries to iter, but preserve JUMP_LABEL_TRUE_BRANCH.
262 */
263 *((unsigned long *)&key->entries) += (unsigned long)iter;
d430d3d7
JB
264#ifdef CONFIG_MODULES
265 key->next = NULL;
266#endif
bf5438fc 267 }
c4b2c0c5 268 static_key_initialized = true;
91bad2f8 269 jump_label_unlock();
bf5438fc 270}
bf5438fc
JB
271
272#ifdef CONFIG_MODULES
273
11276d53
PZ
274static enum jump_label_type jump_label_init_type(struct jump_entry *entry)
275{
276 struct static_key *key = jump_entry_key(entry);
277 bool type = static_key_type(key);
278 bool branch = jump_entry_branch(entry);
279
280 /* See the comment in linux/jump_label.h */
281 return type ^ branch;
282}
283
c5905afb
IM
284struct static_key_mod {
285 struct static_key_mod *next;
d430d3d7
JB
286 struct jump_entry *entries;
287 struct module *mod;
288};
289
290static int __jump_label_mod_text_reserved(void *start, void *end)
291{
292 struct module *mod;
293
294 mod = __module_text_address((unsigned long)start);
295 if (!mod)
296 return 0;
297
298 WARN_ON_ONCE(__module_text_address((unsigned long)end) != mod);
299
300 return __jump_label_text_reserved(mod->jump_entries,
301 mod->jump_entries + mod->num_jump_entries,
302 start, end);
303}
304
706249c2 305static void __jump_label_mod_update(struct static_key *key)
d430d3d7 306{
706249c2 307 struct static_key_mod *mod;
d430d3d7 308
706249c2 309 for (mod = key->next; mod; mod = mod->next) {
7cbc5b8d
JO
310 struct module *m = mod->mod;
311
312 __jump_label_update(key, mod->entries,
706249c2 313 m->jump_entries + m->num_jump_entries);
d430d3d7
JB
314 }
315}
316
317/***
318 * apply_jump_label_nops - patch module jump labels with arch_get_jump_label_nop()
319 * @mod: module to patch
320 *
321 * Allow for run-time selection of the optimal nops. Before the module
322 * loads patch these with arch_get_jump_label_nop(), which is specified by
323 * the arch specific jump label code.
324 */
325void jump_label_apply_nops(struct module *mod)
bf5438fc 326{
d430d3d7
JB
327 struct jump_entry *iter_start = mod->jump_entries;
328 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
329 struct jump_entry *iter;
330
331 /* if the module doesn't have jump label entries, just return */
332 if (iter_start == iter_stop)
333 return;
334
11276d53
PZ
335 for (iter = iter_start; iter < iter_stop; iter++) {
336 /* Only write NOPs for arch_branch_static(). */
337 if (jump_label_init_type(iter) == JUMP_LABEL_NOP)
338 arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
339 }
bf5438fc
JB
340}
341
d430d3d7 342static int jump_label_add_module(struct module *mod)
bf5438fc 343{
d430d3d7
JB
344 struct jump_entry *iter_start = mod->jump_entries;
345 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
346 struct jump_entry *iter;
c5905afb
IM
347 struct static_key *key = NULL;
348 struct static_key_mod *jlm;
bf5438fc
JB
349
350 /* if the module doesn't have jump label entries, just return */
d430d3d7 351 if (iter_start == iter_stop)
bf5438fc
JB
352 return 0;
353
d430d3d7
JB
354 jump_label_sort_entries(iter_start, iter_stop);
355
356 for (iter = iter_start; iter < iter_stop; iter++) {
c5905afb 357 struct static_key *iterk;
d430d3d7 358
7dcfd915 359 iterk = jump_entry_key(iter);
c5905afb
IM
360 if (iterk == key)
361 continue;
d430d3d7 362
c5905afb 363 key = iterk;
bed831f9 364 if (within_module(iter->key, mod)) {
c5905afb
IM
365 /*
366 * Set key->entries to iter, but preserve JUMP_LABEL_TRUE_BRANCH.
367 */
368 *((unsigned long *)&key->entries) += (unsigned long)iter;
d430d3d7
JB
369 key->next = NULL;
370 continue;
bf5438fc 371 }
c5905afb 372 jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL);
d430d3d7
JB
373 if (!jlm)
374 return -ENOMEM;
d430d3d7
JB
375 jlm->mod = mod;
376 jlm->entries = iter;
377 jlm->next = key->next;
378 key->next = jlm;
379
11276d53
PZ
380 /* Only update if we've changed from our initial state */
381 if (jump_label_type(iter) != jump_label_init_type(iter))
706249c2 382 __jump_label_update(key, iter, iter_stop);
bf5438fc 383 }
d430d3d7 384
bf5438fc
JB
385 return 0;
386}
387
d430d3d7 388static void jump_label_del_module(struct module *mod)
bf5438fc 389{
d430d3d7
JB
390 struct jump_entry *iter_start = mod->jump_entries;
391 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
392 struct jump_entry *iter;
c5905afb
IM
393 struct static_key *key = NULL;
394 struct static_key_mod *jlm, **prev;
bf5438fc 395
d430d3d7 396 for (iter = iter_start; iter < iter_stop; iter++) {
7dcfd915 397 if (jump_entry_key(iter) == key)
d430d3d7
JB
398 continue;
399
7dcfd915 400 key = jump_entry_key(iter);
d430d3d7 401
bed831f9 402 if (within_module(iter->key, mod))
d430d3d7
JB
403 continue;
404
405 prev = &key->next;
406 jlm = key->next;
bf5438fc 407
d430d3d7
JB
408 while (jlm && jlm->mod != mod) {
409 prev = &jlm->next;
410 jlm = jlm->next;
411 }
412
413 if (jlm) {
414 *prev = jlm->next;
415 kfree(jlm);
bf5438fc
JB
416 }
417 }
418}
419
d430d3d7 420static void jump_label_invalidate_module_init(struct module *mod)
b842f8fa 421{
d430d3d7
JB
422 struct jump_entry *iter_start = mod->jump_entries;
423 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
b842f8fa 424 struct jump_entry *iter;
b842f8fa 425
d430d3d7
JB
426 for (iter = iter_start; iter < iter_stop; iter++) {
427 if (within_module_init(iter->code, mod))
428 iter->code = 0;
b842f8fa
JB
429 }
430}
431
bf5438fc
JB
432static int
433jump_label_module_notify(struct notifier_block *self, unsigned long val,
434 void *data)
435{
436 struct module *mod = data;
437 int ret = 0;
438
439 switch (val) {
440 case MODULE_STATE_COMING:
91bad2f8 441 jump_label_lock();
d430d3d7 442 ret = jump_label_add_module(mod);
bf5438fc 443 if (ret)
d430d3d7 444 jump_label_del_module(mod);
91bad2f8 445 jump_label_unlock();
bf5438fc
JB
446 break;
447 case MODULE_STATE_GOING:
91bad2f8 448 jump_label_lock();
d430d3d7 449 jump_label_del_module(mod);
91bad2f8 450 jump_label_unlock();
bf5438fc 451 break;
b842f8fa 452 case MODULE_STATE_LIVE:
91bad2f8 453 jump_label_lock();
d430d3d7 454 jump_label_invalidate_module_init(mod);
91bad2f8 455 jump_label_unlock();
b842f8fa 456 break;
bf5438fc 457 }
bf5438fc 458
d430d3d7 459 return notifier_from_errno(ret);
bf5438fc
JB
460}
461
462struct notifier_block jump_label_module_nb = {
463 .notifier_call = jump_label_module_notify,
d430d3d7 464 .priority = 1, /* higher than tracepoints */
bf5438fc
JB
465};
466
d430d3d7 467static __init int jump_label_init_module(void)
bf5438fc
JB
468{
469 return register_module_notifier(&jump_label_module_nb);
470}
d430d3d7 471early_initcall(jump_label_init_module);
bf5438fc
JB
472
473#endif /* CONFIG_MODULES */
474
d430d3d7
JB
475/***
476 * jump_label_text_reserved - check if addr range is reserved
477 * @start: start text addr
478 * @end: end text addr
479 *
480 * checks if the text addr located between @start and @end
481 * overlaps with any of the jump label patch addresses. Code
482 * that wants to modify kernel text should first verify that
483 * it does not overlap with any of the jump label addresses.
484 * Caller must hold jump_label_mutex.
485 *
486 * returns 1 if there is an overlap, 0 otherwise
487 */
488int jump_label_text_reserved(void *start, void *end)
489{
490 int ret = __jump_label_text_reserved(__start___jump_table,
491 __stop___jump_table, start, end);
492
493 if (ret)
494 return ret;
495
496#ifdef CONFIG_MODULES
497 ret = __jump_label_mod_text_reserved(start, end);
498#endif
499 return ret;
500}
501
706249c2 502static void jump_label_update(struct static_key *key)
d430d3d7 503{
c5905afb 504 struct jump_entry *stop = __stop___jump_table;
a1efb01f 505 struct jump_entry *entry = static_key_entries(key);
d430d3d7 506#ifdef CONFIG_MODULES
bed831f9 507 struct module *mod;
140fe3b1 508
706249c2 509 __jump_label_mod_update(key);
140fe3b1 510
bed831f9
PZ
511 preempt_disable();
512 mod = __module_address((unsigned long)key);
140fe3b1
XG
513 if (mod)
514 stop = mod->jump_entries + mod->num_jump_entries;
bed831f9 515 preempt_enable();
d430d3d7 516#endif
140fe3b1
XG
517 /* if there are no users, entry can be NULL */
518 if (entry)
706249c2 519 __jump_label_update(key, entry, stop);
d430d3d7
JB
520}
521
1987c947
PZ
522#ifdef CONFIG_STATIC_KEYS_SELFTEST
523static DEFINE_STATIC_KEY_TRUE(sk_true);
524static DEFINE_STATIC_KEY_FALSE(sk_false);
525
526static __init int jump_label_test(void)
527{
528 int i;
529
530 for (i = 0; i < 2; i++) {
531 WARN_ON(static_key_enabled(&sk_true.key) != true);
532 WARN_ON(static_key_enabled(&sk_false.key) != false);
533
534 WARN_ON(!static_branch_likely(&sk_true));
535 WARN_ON(!static_branch_unlikely(&sk_true));
536 WARN_ON(static_branch_likely(&sk_false));
537 WARN_ON(static_branch_unlikely(&sk_false));
538
539 static_branch_disable(&sk_true);
540 static_branch_enable(&sk_false);
541
542 WARN_ON(static_key_enabled(&sk_true.key) == true);
543 WARN_ON(static_key_enabled(&sk_false.key) == false);
544
545 WARN_ON(static_branch_likely(&sk_true));
546 WARN_ON(static_branch_unlikely(&sk_true));
547 WARN_ON(!static_branch_likely(&sk_false));
548 WARN_ON(!static_branch_unlikely(&sk_false));
549
550 static_branch_enable(&sk_true);
551 static_branch_disable(&sk_false);
552 }
553
554 return 0;
555}
de684062 556early_initcall(jump_label_test);
1987c947
PZ
557#endif /* STATIC_KEYS_SELFTEST */
558
559#endif /* HAVE_JUMP_LABEL */