blob: 543782e7cdd2c22398127c2a7974956b620047db [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>
Jason Barond430d3d2011-03-16 17:29:47 -040015#include <linux/jump_label.h>
Jason Baronbf5438fc2010-09-17 11:09:00 -040016
17#ifdef HAVE_JUMP_LABEL
18
Jason Baronbf5438fc2010-09-17 11:09:00 -040019/* mutex to protect coming/going of the the jump_label table */
20static DEFINE_MUTEX(jump_label_mutex);
21
Jason Baron91bad2f2010-10-01 17:23:48 -040022void jump_label_lock(void)
23{
24 mutex_lock(&jump_label_mutex);
25}
26
27void jump_label_unlock(void)
28{
29 mutex_unlock(&jump_label_mutex);
30}
31
Jason Barond430d3d2011-03-16 17:29:47 -040032bool jump_label_enabled(struct jump_label_key *key)
33{
34 return !!atomic_read(&key->enabled);
35}
36
Jason Baronbf5438fc2010-09-17 11:09:00 -040037static int jump_label_cmp(const void *a, const void *b)
38{
39 const struct jump_entry *jea = a;
40 const struct jump_entry *jeb = b;
41
42 if (jea->key < jeb->key)
43 return -1;
44
45 if (jea->key > jeb->key)
46 return 1;
47
48 return 0;
49}
50
51static void
Jason Barond430d3d2011-03-16 17:29:47 -040052jump_label_sort_entries(struct jump_entry *start, struct jump_entry *stop)
Jason Baronbf5438fc2010-09-17 11:09:00 -040053{
54 unsigned long size;
55
56 size = (((unsigned long)stop - (unsigned long)start)
57 / sizeof(struct jump_entry));
58 sort(start, size, sizeof(struct jump_entry), jump_label_cmp, NULL);
59}
60
Jason Barond430d3d2011-03-16 17:29:47 -040061static void jump_label_update(struct jump_label_key *key, int enable);
62
63void jump_label_inc(struct jump_label_key *key)
Jason Baronbf5438fc2010-09-17 11:09:00 -040064{
Jason Barond430d3d2011-03-16 17:29:47 -040065 if (atomic_inc_not_zero(&key->enabled))
66 return;
Jason Baronbf5438fc2010-09-17 11:09:00 -040067
Jason Baron91bad2f2010-10-01 17:23:48 -040068 jump_label_lock();
Gleb Natapovbbbf7af2011-10-18 19:55:51 +020069 if (atomic_read(&key->enabled) == 0)
Jason Barond430d3d2011-03-16 17:29:47 -040070 jump_label_update(key, JUMP_LABEL_ENABLE);
Gleb Natapovbbbf7af2011-10-18 19:55:51 +020071 atomic_inc(&key->enabled);
Jason Barond430d3d2011-03-16 17:29:47 -040072 jump_label_unlock();
73}
Xiao Guangronga65cf5182011-11-28 20:39:59 +080074EXPORT_SYMBOL_GPL(jump_label_inc);
Jason Barond430d3d2011-03-16 17:29:47 -040075
Gleb Natapovb2029522011-11-27 17:59:09 +020076static void __jump_label_dec(struct jump_label_key *key,
77 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);
88 } else
89 jump_label_update(key, JUMP_LABEL_DISABLE);
90
Jason Baron91bad2f2010-10-01 17:23:48 -040091 jump_label_unlock();
Jason Baronbf5438fc2010-09-17 11:09:00 -040092}
Xiao Guangronga65cf5182011-11-28 20:39:59 +080093EXPORT_SYMBOL_GPL(jump_label_dec);
Jason Baronbf5438fc2010-09-17 11:09:00 -040094
Gleb Natapovb2029522011-11-27 17:59:09 +020095static void jump_label_update_timeout(struct work_struct *work)
96{
97 struct jump_label_key_deferred *key =
98 container_of(work, struct jump_label_key_deferred, work.work);
99 __jump_label_dec(&key->key, 0, NULL);
100}
101
102void jump_label_dec(struct jump_label_key *key)
103{
104 __jump_label_dec(key, 0, NULL);
105}
106
107void jump_label_dec_deferred(struct jump_label_key_deferred *key)
108{
109 __jump_label_dec(&key->key, key->timeout, &key->work);
110}
111
112
113void jump_label_rate_limit(struct jump_label_key_deferred *key,
114 unsigned long rl)
115{
116 key->timeout = rl;
117 INIT_DELAYED_WORK(&key->work, jump_label_update_timeout);
118}
119
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400120static int addr_conflict(struct jump_entry *entry, void *start, void *end)
121{
122 if (entry->code <= (unsigned long)end &&
123 entry->code + JUMP_LABEL_NOP_SIZE > (unsigned long)start)
124 return 1;
125
126 return 0;
127}
128
Jason Barond430d3d2011-03-16 17:29:47 -0400129static int __jump_label_text_reserved(struct jump_entry *iter_start,
130 struct jump_entry *iter_stop, void *start, void *end)
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400131{
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400132 struct jump_entry *iter;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400133
Jason Barond430d3d2011-03-16 17:29:47 -0400134 iter = iter_start;
135 while (iter < iter_stop) {
136 if (addr_conflict(iter, start, end))
137 return 1;
138 iter++;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400139 }
Jason Barond430d3d2011-03-16 17:29:47 -0400140
141 return 0;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400142}
143
Jeremy Fitzhardinge20284aa2011-10-03 11:01:46 -0700144/*
145 * Update code which is definitely not currently executing.
146 * Architectures which need heavyweight synchronization to modify
147 * running code can override this to make the non-live update case
148 * cheaper.
149 */
Peter Zijlstra9cdbe1c2011-12-06 17:27:29 +0100150void __weak __init_or_module arch_jump_label_transform_static(struct jump_entry *entry,
Jeremy Fitzhardinge20284aa2011-10-03 11:01:46 -0700151 enum jump_label_type type)
152{
153 arch_jump_label_transform(entry, type);
154}
155
Jason Barond430d3d2011-03-16 17:29:47 -0400156static void __jump_label_update(struct jump_label_key *key,
Jiri Olsa7cbc5b82011-05-10 12:43:46 +0200157 struct jump_entry *entry,
158 struct jump_entry *stop, int enable)
Jason Barond430d3d2011-03-16 17:29:47 -0400159{
Jiri Olsa7cbc5b82011-05-10 12:43:46 +0200160 for (; (entry < stop) &&
161 (entry->key == (jump_label_t)(unsigned long)key);
162 entry++) {
Jason Barond430d3d2011-03-16 17:29:47 -0400163 /*
164 * entry->code set to 0 invalidates module init text sections
165 * kernel_text_address() verifies we are not in core kernel
166 * init code, see jump_label_invalidate_module_init().
167 */
168 if (entry->code && kernel_text_address(entry->code))
169 arch_jump_label_transform(entry, enable);
170 }
171}
172
Jeremy Fitzhardinge97ce2c82011-10-12 16:17:54 -0700173void __init jump_label_init(void)
Jason Barond430d3d2011-03-16 17:29:47 -0400174{
175 struct jump_entry *iter_start = __start___jump_table;
176 struct jump_entry *iter_stop = __stop___jump_table;
177 struct jump_label_key *key = NULL;
178 struct jump_entry *iter;
179
180 jump_label_lock();
181 jump_label_sort_entries(iter_start, iter_stop);
182
183 for (iter = iter_start; iter < iter_stop; iter++) {
Jeremy Fitzhardinge37348802011-09-29 11:10:05 -0700184 struct jump_label_key *iterk;
185
186 iterk = (struct jump_label_key *)(unsigned long)iter->key;
Jeremy Fitzhardinge20284aa2011-10-03 11:01:46 -0700187 arch_jump_label_transform_static(iter, jump_label_enabled(iterk) ?
188 JUMP_LABEL_ENABLE : JUMP_LABEL_DISABLE);
Jeremy Fitzhardinge37348802011-09-29 11:10:05 -0700189 if (iterk == key)
Jason Barond430d3d2011-03-16 17:29:47 -0400190 continue;
191
Jeremy Fitzhardinge37348802011-09-29 11:10:05 -0700192 key = iterk;
Jason Barond430d3d2011-03-16 17:29:47 -0400193 key->entries = iter;
194#ifdef CONFIG_MODULES
195 key->next = NULL;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400196#endif
Jason Barond430d3d2011-03-16 17:29:47 -0400197 }
198 jump_label_unlock();
Jason Barond430d3d2011-03-16 17:29:47 -0400199}
Jason Barond430d3d2011-03-16 17:29:47 -0400200
201#ifdef CONFIG_MODULES
202
203struct jump_label_mod {
204 struct jump_label_mod *next;
205 struct jump_entry *entries;
206 struct module *mod;
207};
208
209static int __jump_label_mod_text_reserved(void *start, void *end)
210{
211 struct module *mod;
212
213 mod = __module_text_address((unsigned long)start);
214 if (!mod)
215 return 0;
216
217 WARN_ON_ONCE(__module_text_address((unsigned long)end) != mod);
218
219 return __jump_label_text_reserved(mod->jump_entries,
220 mod->jump_entries + mod->num_jump_entries,
221 start, end);
222}
223
224static void __jump_label_mod_update(struct jump_label_key *key, int enable)
225{
226 struct jump_label_mod *mod = key->next;
227
228 while (mod) {
Jiri Olsa7cbc5b82011-05-10 12:43:46 +0200229 struct module *m = mod->mod;
230
231 __jump_label_update(key, mod->entries,
232 m->jump_entries + m->num_jump_entries,
233 enable);
Jason Barond430d3d2011-03-16 17:29:47 -0400234 mod = mod->next;
235 }
236}
237
238/***
239 * apply_jump_label_nops - patch module jump labels with arch_get_jump_label_nop()
240 * @mod: module to patch
241 *
242 * Allow for run-time selection of the optimal nops. Before the module
243 * loads patch these with arch_get_jump_label_nop(), which is specified by
244 * the arch specific jump label code.
245 */
246void jump_label_apply_nops(struct module *mod)
247{
248 struct jump_entry *iter_start = mod->jump_entries;
249 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
250 struct jump_entry *iter;
251
252 /* if the module doesn't have jump label entries, just return */
253 if (iter_start == iter_stop)
254 return;
255
Peter Zijlstraac99b862011-07-06 14:20:14 +0200256 for (iter = iter_start; iter < iter_stop; iter++) {
257 struct jump_label_key *iterk;
258
259 iterk = (struct jump_label_key *)(unsigned long)iter->key;
260 arch_jump_label_transform_static(iter, jump_label_enabled(iterk) ?
261 JUMP_LABEL_ENABLE : JUMP_LABEL_DISABLE);
262 }
Jason Barond430d3d2011-03-16 17:29:47 -0400263}
264
265static int jump_label_add_module(struct module *mod)
266{
267 struct jump_entry *iter_start = mod->jump_entries;
268 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
269 struct jump_entry *iter;
270 struct jump_label_key *key = NULL;
271 struct jump_label_mod *jlm;
272
273 /* if the module doesn't have jump label entries, just return */
274 if (iter_start == iter_stop)
275 return 0;
276
277 jump_label_sort_entries(iter_start, iter_stop);
278
279 for (iter = iter_start; iter < iter_stop; iter++) {
280 if (iter->key == (jump_label_t)(unsigned long)key)
281 continue;
282
283 key = (struct jump_label_key *)(unsigned long)iter->key;
284
285 if (__module_address(iter->key) == mod) {
286 atomic_set(&key->enabled, 0);
287 key->entries = iter;
288 key->next = NULL;
289 continue;
290 }
291
292 jlm = kzalloc(sizeof(struct jump_label_mod), GFP_KERNEL);
293 if (!jlm)
294 return -ENOMEM;
295
296 jlm->mod = mod;
297 jlm->entries = iter;
298 jlm->next = key->next;
299 key->next = jlm;
300
301 if (jump_label_enabled(key))
Peter Zijlstraac99b862011-07-06 14:20:14 +0200302 __jump_label_update(key, iter, iter_stop, JUMP_LABEL_ENABLE);
Jason Barond430d3d2011-03-16 17:29:47 -0400303 }
304
305 return 0;
306}
307
308static void jump_label_del_module(struct module *mod)
309{
310 struct jump_entry *iter_start = mod->jump_entries;
311 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
312 struct jump_entry *iter;
313 struct jump_label_key *key = NULL;
314 struct jump_label_mod *jlm, **prev;
315
316 for (iter = iter_start; iter < iter_stop; iter++) {
317 if (iter->key == (jump_label_t)(unsigned long)key)
318 continue;
319
320 key = (struct jump_label_key *)(unsigned long)iter->key;
321
322 if (__module_address(iter->key) == mod)
323 continue;
324
325 prev = &key->next;
326 jlm = key->next;
327
328 while (jlm && jlm->mod != mod) {
329 prev = &jlm->next;
330 jlm = jlm->next;
331 }
332
333 if (jlm) {
334 *prev = jlm->next;
335 kfree(jlm);
336 }
337 }
338}
339
340static void jump_label_invalidate_module_init(struct module *mod)
341{
342 struct jump_entry *iter_start = mod->jump_entries;
343 struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
344 struct jump_entry *iter;
345
346 for (iter = iter_start; iter < iter_stop; iter++) {
347 if (within_module_init(iter->code, mod))
348 iter->code = 0;
349 }
350}
351
352static int
353jump_label_module_notify(struct notifier_block *self, unsigned long val,
354 void *data)
355{
356 struct module *mod = data;
357 int ret = 0;
358
359 switch (val) {
360 case MODULE_STATE_COMING:
361 jump_label_lock();
362 ret = jump_label_add_module(mod);
363 if (ret)
364 jump_label_del_module(mod);
365 jump_label_unlock();
366 break;
367 case MODULE_STATE_GOING:
368 jump_label_lock();
369 jump_label_del_module(mod);
370 jump_label_unlock();
371 break;
372 case MODULE_STATE_LIVE:
373 jump_label_lock();
374 jump_label_invalidate_module_init(mod);
375 jump_label_unlock();
376 break;
377 }
378
379 return notifier_from_errno(ret);
380}
381
382struct notifier_block jump_label_module_nb = {
383 .notifier_call = jump_label_module_notify,
384 .priority = 1, /* higher than tracepoints */
385};
386
387static __init int jump_label_init_module(void)
388{
389 return register_module_notifier(&jump_label_module_nb);
390}
391early_initcall(jump_label_init_module);
392
393#endif /* CONFIG_MODULES */
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400394
395/***
396 * jump_label_text_reserved - check if addr range is reserved
397 * @start: start text addr
398 * @end: end text addr
399 *
400 * checks if the text addr located between @start and @end
401 * overlaps with any of the jump label patch addresses. Code
402 * that wants to modify kernel text should first verify that
403 * it does not overlap with any of the jump label addresses.
Jason Baron91bad2f2010-10-01 17:23:48 -0400404 * Caller must hold jump_label_mutex.
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400405 *
406 * returns 1 if there is an overlap, 0 otherwise
407 */
408int jump_label_text_reserved(void *start, void *end)
409{
Jason Barond430d3d2011-03-16 17:29:47 -0400410 int ret = __jump_label_text_reserved(__start___jump_table,
411 __stop___jump_table, start, end);
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400412
Jason Barond430d3d2011-03-16 17:29:47 -0400413 if (ret)
414 return ret;
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400415
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400416#ifdef CONFIG_MODULES
Jason Barond430d3d2011-03-16 17:29:47 -0400417 ret = __jump_label_mod_text_reserved(start, end);
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400418#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -0400419 return ret;
420}
Jason Barond430d3d2011-03-16 17:29:47 -0400421
422static void jump_label_update(struct jump_label_key *key, int enable)
423{
Xiao Guangrong140fe3b2011-06-21 10:35:55 +0800424 struct jump_entry *entry = key->entries, *stop = __stop___jump_table;
Jason Baronbf5438fc2010-09-17 11:09:00 -0400425
426#ifdef CONFIG_MODULES
Jason Barona746e3c2012-02-21 15:02:57 -0500427 struct module *mod = __module_address((unsigned long)key);
Xiao Guangrong140fe3b2011-06-21 10:35:55 +0800428
Jason Barond430d3d2011-03-16 17:29:47 -0400429 __jump_label_mod_update(key, enable);
Xiao Guangrong140fe3b2011-06-21 10:35:55 +0800430
431 if (mod)
432 stop = mod->jump_entries + mod->num_jump_entries;
Jason Barond430d3d2011-03-16 17:29:47 -0400433#endif
Xiao Guangrong140fe3b2011-06-21 10:35:55 +0800434 /* if there are no users, entry can be NULL */
435 if (entry)
436 __jump_label_update(key, entry, stop, enable);
Jason Baronbf5438fc2010-09-17 11:09:00 -0400437}
438
Jason Baronbf5438fc2010-09-17 11:09:00 -0400439#endif