blob: 297a9247a3b394f8538db111817e8a88d3c345ab [file] [log] [blame]
Jason Baronbf5438fc2010-09-17 11:09:00 -04001/*
2 * jump label support
3 *
4 * Copyright (C) 2009 Jason Baron <jbaron@redhat.com>
Jason Barond430d3d2011-03-16 17:29:47 -04005 * Copyright (C) 2011 Peter Zijlstra <pzijlstr@redhat.com>
Jason Baronbf5438fc2010-09-17 11:09:00 -04006 *
7 */
Jason Baronbf5438fc2010-09-17 11:09:00 -04008#include <linux/memory.h>
9#include <linux/uaccess.h>
10#include <linux/module.h>
11#include <linux/list.h>
Jason Baronbf5438fc2010-09-17 11:09:00 -040012#include <linux/slab.h>
13#include <linux/sort.h>
14#include <linux/err.h>
Ingo Molnarc5905af2012-02-24 08:31:31 +010015#include <linux/static_key.h>
Andrew Jones851cf6e2013-08-09 19:51:57 +053016#include <linux/jump_label_ratelimit.h>
Jason Baronbf5438fc2010-09-17 11:09:00 -040017
18#ifdef HAVE_JUMP_LABEL
19
Jason Baronbf5438fc2010-09-17 11:09:00 -040020/* mutex to protect coming/going of the the jump_label table */
21static DEFINE_MUTEX(jump_label_mutex);
22
Jason Baron91bad2f2010-10-01 17:23:48 -040023void 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
Jason Baronbf5438fc2010-09-17 11:09:00 -040033static 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
Jason Barond430d3d2011-03-16 17:29:47 -040048jump_label_sort_entries(struct jump_entry *start, struct jump_entry *stop)
Jason Baronbf5438fc2010-09-17 11:09:00 -040049{
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
Ingo Molnarc5905af2012-02-24 08:31:31 +010057static void jump_label_update(struct static_key *key, int enable);
Jason Barond430d3d2011-03-16 17:29:47 -040058
Ingo Molnarc5905af2012-02-24 08:31:31 +010059void static_key_slow_inc(struct static_key *key)
Jason Baronbf5438fc2010-09-17 11:09:00 -040060{
Jason Barond430d3d2011-03-16 17:29:47 -040061 if (atomic_inc_not_zero(&key->enabled))
62 return;
Jason Baronbf5438fc2010-09-17 11:09:00 -040063
Jason Baron91bad2f2010-10-01 17:23:48 -040064 jump_label_lock();
Ingo Molnarc5905af2012-02-24 08:31:31 +010065 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 }
Gleb Natapovbbbf7af2011-10-18 19:55:51 +020071 atomic_inc(&key->enabled);
Jason Barond430d3d2011-03-16 17:29:47 -040072 jump_label_unlock();
73}
Ingo Molnarc5905af2012-02-24 08:31:31 +010074EXPORT_SYMBOL_GPL(static_key_slow_inc);
Jason Barond430d3d2011-03-16 17:29:47 -040075
Ingo Molnarc5905af2012-02-24 08:31:31 +010076static void __static_key_slow_dec(struct static_key *key,
Gleb Natapovb2029522011-11-27 17:59:09 +020077 unsigned long rate_limit, struct delayed_work *work)
Jason Barond430d3d2011-03-16 17:29:47 -040078{
Jason Baronfadf0462012-02-21 15:02:53 -050079 if (!atomic_dec_and_mutex_lock(&key->enabled, &jump_label_mutex)) {
80 WARN(atomic_read(&key->enabled) < 0,
81 "jump label: negative count!\n");
Jason Barond430d3d2011-03-16 17:29:47 -040082 return;
Jason Baronfadf0462012-02-21 15:02:53 -050083 }
Jason Barond430d3d2011-03-16 17:29:47 -040084
Gleb Natapovb2029522011-11-27 17:59:09 +020085 if (rate_limit) {
86 atomic_inc(&key->enabled);
87 schedule_delayed_work(work, rate_limit);
Ingo Molnarc5905af2012-02-24 08:31:31 +010088 } 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 }
Jason Baron91bad2f2010-10-01 17:23:48 -040094 jump_label_unlock();
Jason Baronbf5438fc2010-09-17 11:09:00 -040095}
96
Gleb Natapovb2029522011-11-27 17:59:09 +020097static void jump_label_update_timeout(struct work_struct *work)
98{
Ingo Molnarc5905af2012-02-24 08:31:31 +010099 struct static_key_deferred *key =
100 container_of(work, struct static_key_deferred, work.work);
101 __static_key_slow_dec(&key->key, 0, NULL);
Gleb Natapovb2029522011-11-27 17:59:09 +0200102}
103
Ingo Molnarc5905af2012-02-24 08:31:31 +0100104void static_key_slow_dec(struct static_key *key)
Gleb Natapovb2029522011-11-27 17:59:09 +0200105{
Ingo Molnarc5905af2012-02-24 08:31:31 +0100106 __static_key_slow_dec(key, 0, NULL);
Gleb Natapovb2029522011-11-27 17:59:09 +0200107}
Ingo Molnarc5905af2012-02-24 08:31:31 +0100108EXPORT_SYMBOL_GPL(static_key_slow_dec);
Gleb Natapovb2029522011-11-27 17:59:09 +0200109
Ingo Molnarc5905af2012-02-24 08:31:31 +0100110void static_key_slow_dec_deferred(struct static_key_deferred *key)
Gleb Natapovb2029522011-11-27 17:59:09 +0200111{
Ingo Molnarc5905af2012-02-24 08:31:31 +0100112 __static_key_slow_dec(&key->key, key->timeout, &key->work);
Gleb Natapovb2029522011-11-27 17:59:09 +0200113}
Ingo Molnarc5905af2012-02-24 08:31:31 +0100114EXPORT_SYMBOL_GPL(static_key_slow_dec_deferred);
Gleb Natapovb2029522011-11-27 17:59:09 +0200115
Ingo Molnarc5905af2012-02-24 08:31:31 +0100116void jump_label_rate_limit(struct static_key_deferred *key,
Gleb Natapovb2029522011-11-27 17:59:09 +0200117 unsigned long rl)
118{
119 key->timeout = rl;
120 INIT_DELAYED_WORK(&key->work, jump_label_update_timeout);
121}
Gleb Natapova181dc12012-08-05 15:58:29 +0300122EXPORT_SYMBOL_GPL(jump_label_rate_limit);
Gleb Natapovb2029522011-11-27 17:59:09 +0200123
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400124static 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
Jason Barond430d3d2011-03-16 17:29:47 -0400133static int __jump_label_text_reserved(struct jump_entry *iter_start,
134 struct jump_entry *iter_stop, void *start, void *end)
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400135{
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400136 struct jump_entry *iter;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400137
Jason Barond430d3d2011-03-16 17:29:47 -0400138 iter = iter_start;
139 while (iter < iter_stop) {
140 if (addr_conflict(iter, start, end))
141 return 1;
142 iter++;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400143 }
Jason Barond430d3d2011-03-16 17:29:47 -0400144
145 return 0;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400146}
147
Jeremy Fitzhardinge20284aa2011-10-03 11:01:46 -0700148/*
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 */
Peter Zijlstra9cdbe1c2011-12-06 17:27:29 +0100154void __weak __init_or_module arch_jump_label_transform_static(struct jump_entry *entry,
Jeremy Fitzhardinge20284aa2011-10-03 11:01:46 -0700155 enum jump_label_type type)
156{
157 arch_jump_label_transform(entry, type);
158}
159
Ingo Molnarc5905af2012-02-24 08:31:31 +0100160static void __jump_label_update(struct static_key *key,
Jiri Olsa7cbc5b82011-05-10 12:43:46 +0200161 struct jump_entry *entry,
162 struct jump_entry *stop, int enable)
Jason Barond430d3d2011-03-16 17:29:47 -0400163{
Jiri Olsa7cbc5b82011-05-10 12:43:46 +0200164 for (; (entry < stop) &&
165 (entry->key == (jump_label_t)(unsigned long)key);
166 entry++) {
Jason Barond430d3d2011-03-16 17:29:47 -0400167 /*
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 }
175}
176
Ingo Molnarc5905af2012-02-24 08:31:31 +0100177static 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
Jeremy Fitzhardinge97ce2c82011-10-12 16:17:54 -0700188void __init jump_label_init(void)
Jason Barond430d3d2011-03-16 17:29:47 -0400189{
190 struct jump_entry *iter_start = __start___jump_table;
191 struct jump_entry *iter_stop = __stop___jump_table;
Ingo Molnarc5905af2012-02-24 08:31:31 +0100192 struct static_key *key = NULL;
Jason Barond430d3d2011-03-16 17:29:47 -0400193 struct jump_entry *iter;
194
195 jump_label_lock();
196 jump_label_sort_entries(iter_start, iter_stop);
197
198 for (iter = iter_start; iter < iter_stop; iter++) {
Ingo Molnarc5905af2012-02-24 08:31:31 +0100199 struct static_key *iterk;
Jeremy Fitzhardinge37348802011-09-29 11:10:05 -0700200
Ingo Molnarc5905af2012-02-24 08:31:31 +0100201 iterk = (struct static_key *)(unsigned long)iter->key;
202 arch_jump_label_transform_static(iter, jump_label_type(iterk));
Jeremy Fitzhardinge37348802011-09-29 11:10:05 -0700203 if (iterk == key)
Jason Barond430d3d2011-03-16 17:29:47 -0400204 continue;
205
Jeremy Fitzhardinge37348802011-09-29 11:10:05 -0700206 key = iterk;
Ingo Molnarc5905af2012-02-24 08:31:31 +0100207 /*
208 * Set key->entries to iter, but preserve JUMP_LABEL_TRUE_BRANCH.
209 */
210 *((unsigned long *)&key->entries) += (unsigned long)iter;
Jason Barond430d3d2011-03-16 17:29:47 -0400211#ifdef CONFIG_MODULES
212 key->next = NULL;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400213#endif
Jason Barond430d3d2011-03-16 17:29:47 -0400214 }
215 jump_label_unlock();
Jason Barond430d3d2011-03-16 17:29:47 -0400216}
Jason Barond430d3d2011-03-16 17:29:47 -0400217
218#ifdef CONFIG_MODULES
219
Ingo Molnarc5905af2012-02-24 08:31:31 +0100220struct static_key_mod {
221 struct static_key_mod *next;
Jason Barond430d3d2011-03-16 17:29:47 -0400222 struct jump_entry *entries;
223 struct module *mod;
224};
225
226static 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
Ingo Molnarc5905af2012-02-24 08:31:31 +0100241static void __jump_label_mod_update(struct static_key *key, int enable)
Jason Barond430d3d2011-03-16 17:29:47 -0400242{
Ingo Molnarc5905af2012-02-24 08:31:31 +0100243 struct static_key_mod *mod = key->next;
Jason Barond430d3d2011-03-16 17:29:47 -0400244
245 while (mod) {
Jiri Olsa7cbc5b82011-05-10 12:43:46 +0200246 struct module *m = mod->mod;
247
248 __jump_label_update(key, mod->entries,
249 m->jump_entries + m->num_jump_entries,
250 enable);
Jason Barond430d3d2011-03-16 17:29:47 -0400251 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 */
263void jump_label_apply_nops(struct module *mod)
264{
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
Peter Zijlstraac99b862011-07-06 14:20:14 +0200273 for (iter = iter_start; iter < iter_stop; iter++) {
Ingo Molnarc5905af2012-02-24 08:31:31 +0100274 arch_jump_label_transform_static(iter, JUMP_LABEL_DISABLE);
Peter Zijlstraac99b862011-07-06 14:20:14 +0200275 }
Jason Barond430d3d2011-03-16 17:29:47 -0400276}
277
278static int jump_label_add_module(struct module *mod)
279{
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;
Ingo Molnarc5905af2012-02-24 08:31:31 +0100283 struct static_key *key = NULL;
284 struct static_key_mod *jlm;
Jason Barond430d3d2011-03-16 17:29:47 -0400285
286 /* if the module doesn't have jump label entries, just return */
287 if (iter_start == iter_stop)
288 return 0;
289
290 jump_label_sort_entries(iter_start, iter_stop);
291
292 for (iter = iter_start; iter < iter_stop; iter++) {
Ingo Molnarc5905af2012-02-24 08:31:31 +0100293 struct static_key *iterk;
294
295 iterk = (struct static_key *)(unsigned long)iter->key;
296 if (iterk == key)
Jason Barond430d3d2011-03-16 17:29:47 -0400297 continue;
298
Ingo Molnarc5905af2012-02-24 08:31:31 +0100299 key = iterk;
Jason Barond430d3d2011-03-16 17:29:47 -0400300 if (__module_address(iter->key) == mod) {
Ingo Molnarc5905af2012-02-24 08:31:31 +0100301 /*
302 * Set key->entries to iter, but preserve JUMP_LABEL_TRUE_BRANCH.
303 */
304 *((unsigned long *)&key->entries) += (unsigned long)iter;
Jason Barond430d3d2011-03-16 17:29:47 -0400305 key->next = NULL;
306 continue;
307 }
Ingo Molnarc5905af2012-02-24 08:31:31 +0100308 jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL);
Jason Barond430d3d2011-03-16 17:29:47 -0400309 if (!jlm)
310 return -ENOMEM;
Jason Barond430d3d2011-03-16 17:29:47 -0400311 jlm->mod = mod;
312 jlm->entries = iter;
313 jlm->next = key->next;
314 key->next = jlm;
315
Ingo Molnarc5905af2012-02-24 08:31:31 +0100316 if (jump_label_type(key) == JUMP_LABEL_ENABLE)
Peter Zijlstraac99b862011-07-06 14:20:14 +0200317 __jump_label_update(key, iter, iter_stop, JUMP_LABEL_ENABLE);
Jason Barond430d3d2011-03-16 17:29:47 -0400318 }
319
320 return 0;
321}
322
323static void jump_label_del_module(struct module *mod)
324{
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;
Ingo Molnarc5905af2012-02-24 08:31:31 +0100328 struct static_key *key = NULL;
329 struct static_key_mod *jlm, **prev;
Jason Barond430d3d2011-03-16 17:29:47 -0400330
331 for (iter = iter_start; iter < iter_stop; iter++) {
332 if (iter->key == (jump_label_t)(unsigned long)key)
333 continue;
334
Ingo Molnarc5905af2012-02-24 08:31:31 +0100335 key = (struct static_key *)(unsigned long)iter->key;
Jason Barond430d3d2011-03-16 17:29:47 -0400336
337 if (__module_address(iter->key) == mod)
338 continue;
339
340 prev = &key->next;
341 jlm = key->next;
342
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);
351 }
352 }
353}
354
355static void jump_label_invalidate_module_init(struct module *mod)
356{
357 struct jump_entry *iter_start = mod->jump_entries;
358 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
359 struct jump_entry *iter;
360
361 for (iter = iter_start; iter < iter_stop; iter++) {
362 if (within_module_init(iter->code, mod))
363 iter->code = 0;
364 }
365}
366
367static int
368jump_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:
376 jump_label_lock();
377 ret = jump_label_add_module(mod);
378 if (ret)
379 jump_label_del_module(mod);
380 jump_label_unlock();
381 break;
382 case MODULE_STATE_GOING:
383 jump_label_lock();
384 jump_label_del_module(mod);
385 jump_label_unlock();
386 break;
387 case MODULE_STATE_LIVE:
388 jump_label_lock();
389 jump_label_invalidate_module_init(mod);
390 jump_label_unlock();
391 break;
392 }
393
394 return notifier_from_errno(ret);
395}
396
397struct notifier_block jump_label_module_nb = {
398 .notifier_call = jump_label_module_notify,
399 .priority = 1, /* higher than tracepoints */
400};
401
402static __init int jump_label_init_module(void)
403{
404 return register_module_notifier(&jump_label_module_nb);
405}
406early_initcall(jump_label_init_module);
407
408#endif /* CONFIG_MODULES */
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400409
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.
Jason Baron91bad2f2010-10-01 17:23:48 -0400419 * Caller must hold jump_label_mutex.
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400420 *
421 * returns 1 if there is an overlap, 0 otherwise
422 */
423int jump_label_text_reserved(void *start, void *end)
424{
Jason Barond430d3d2011-03-16 17:29:47 -0400425 int ret = __jump_label_text_reserved(__start___jump_table,
426 __stop___jump_table, start, end);
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400427
Jason Barond430d3d2011-03-16 17:29:47 -0400428 if (ret)
429 return ret;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400430
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400431#ifdef CONFIG_MODULES
Jason Barond430d3d2011-03-16 17:29:47 -0400432 ret = __jump_label_mod_text_reserved(start, end);
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400433#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -0400434 return ret;
435}
Jason Barond430d3d2011-03-16 17:29:47 -0400436
Ingo Molnarc5905af2012-02-24 08:31:31 +0100437static void jump_label_update(struct static_key *key, int enable)
Jason Barond430d3d2011-03-16 17:29:47 -0400438{
Ingo Molnarc5905af2012-02-24 08:31:31 +0100439 struct jump_entry *stop = __stop___jump_table;
440 struct jump_entry *entry = jump_label_get_entries(key);
Jason Baronbf5438fc2010-09-17 11:09:00 -0400441
442#ifdef CONFIG_MODULES
Jason Barona746e3c2012-02-21 15:02:57 -0500443 struct module *mod = __module_address((unsigned long)key);
Xiao Guangrong140fe3b2011-06-21 10:35:55 +0800444
Jason Barond430d3d2011-03-16 17:29:47 -0400445 __jump_label_mod_update(key, enable);
Xiao Guangrong140fe3b2011-06-21 10:35:55 +0800446
447 if (mod)
448 stop = mod->jump_entries + mod->num_jump_entries;
Jason Barond430d3d2011-03-16 17:29:47 -0400449#endif
Xiao Guangrong140fe3b2011-06-21 10:35:55 +0800450 /* if there are no users, entry can be NULL */
451 if (entry)
452 __jump_label_update(key, entry, stop, enable);
Jason Baronbf5438fc2010-09-17 11:09:00 -0400453}
454
Jason Baronbf5438fc2010-09-17 11:09:00 -0400455#endif