]>
Commit | Line | Data |
---|---|---|
bf5438fc JB |
1 | /* |
2 | * jump label support | |
3 | * | |
4 | * Copyright (C) 2009 Jason Baron <jbaron@redhat.com> | |
d430d3d7 | 5 | * Copyright (C) 2011 Peter Zijlstra <pzijlstr@redhat.com> |
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> | |
d430d3d7 | 15 | #include <linux/jump_label.h> |
bf5438fc JB |
16 | |
17 | #ifdef HAVE_JUMP_LABEL | |
18 | ||
bf5438fc JB |
19 | /* mutex to protect coming/going of the the jump_label table */ |
20 | static DEFINE_MUTEX(jump_label_mutex); | |
21 | ||
91bad2f8 JB |
22 | void jump_label_lock(void) |
23 | { | |
24 | mutex_lock(&jump_label_mutex); | |
25 | } | |
26 | ||
27 | void jump_label_unlock(void) | |
28 | { | |
29 | mutex_unlock(&jump_label_mutex); | |
30 | } | |
31 | ||
d430d3d7 JB |
32 | bool jump_label_enabled(struct jump_label_key *key) |
33 | { | |
34 | return !!atomic_read(&key->enabled); | |
35 | } | |
36 | ||
bf5438fc JB |
37 | static 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 | ||
51 | static void | |
d430d3d7 | 52 | jump_label_sort_entries(struct jump_entry *start, struct jump_entry *stop) |
bf5438fc JB |
53 | { |
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 | ||
d430d3d7 | 61 | static void jump_label_update(struct jump_label_key *key, int enable); |
bf5438fc | 62 | |
d430d3d7 | 63 | void jump_label_inc(struct jump_label_key *key) |
bf5438fc | 64 | { |
d430d3d7 JB |
65 | if (atomic_inc_not_zero(&key->enabled)) |
66 | return; | |
bf5438fc | 67 | |
d430d3d7 | 68 | jump_label_lock(); |
bbbf7af4 | 69 | if (atomic_read(&key->enabled) == 0) |
d430d3d7 | 70 | jump_label_update(key, JUMP_LABEL_ENABLE); |
bbbf7af4 | 71 | atomic_inc(&key->enabled); |
d430d3d7 | 72 | jump_label_unlock(); |
bf5438fc | 73 | } |
a65cf518 | 74 | EXPORT_SYMBOL_GPL(jump_label_inc); |
bf5438fc | 75 | |
b2029520 GN |
76 | static void __jump_label_dec(struct jump_label_key *key, |
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); | |
88 | } else | |
89 | jump_label_update(key, JUMP_LABEL_DISABLE); | |
90 | ||
91bad2f8 | 91 | jump_label_unlock(); |
bf5438fc | 92 | } |
a65cf518 | 93 | EXPORT_SYMBOL_GPL(jump_label_dec); |
bf5438fc | 94 | |
b2029520 GN |
95 | static 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 | ||
102 | void jump_label_dec(struct jump_label_key *key) | |
103 | { | |
104 | __jump_label_dec(key, 0, NULL); | |
105 | } | |
106 | ||
107 | void jump_label_dec_deferred(struct jump_label_key_deferred *key) | |
108 | { | |
109 | __jump_label_dec(&key->key, key->timeout, &key->work); | |
110 | } | |
111 | ||
112 | ||
113 | void 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 | ||
4c3ef6d7 JB |
120 | static 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 | ||
d430d3d7 JB |
129 | static int __jump_label_text_reserved(struct jump_entry *iter_start, |
130 | struct jump_entry *iter_stop, void *start, void *end) | |
4c3ef6d7 | 131 | { |
4c3ef6d7 | 132 | struct jump_entry *iter; |
4c3ef6d7 | 133 | |
4c3ef6d7 JB |
134 | iter = iter_start; |
135 | while (iter < iter_stop) { | |
d430d3d7 JB |
136 | if (addr_conflict(iter, start, end)) |
137 | return 1; | |
4c3ef6d7 JB |
138 | iter++; |
139 | } | |
140 | ||
d430d3d7 JB |
141 | return 0; |
142 | } | |
143 | ||
20284aa7 JF |
144 | /* |
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 | */ | |
9cdbe1cb | 150 | void __weak __init_or_module arch_jump_label_transform_static(struct jump_entry *entry, |
20284aa7 JF |
151 | enum jump_label_type type) |
152 | { | |
153 | arch_jump_label_transform(entry, type); | |
154 | } | |
155 | ||
d430d3d7 | 156 | static void __jump_label_update(struct jump_label_key *key, |
7cbc5b8d JO |
157 | struct jump_entry *entry, |
158 | struct jump_entry *stop, int enable) | |
d430d3d7 | 159 | { |
7cbc5b8d JO |
160 | for (; (entry < stop) && |
161 | (entry->key == (jump_label_t)(unsigned long)key); | |
162 | entry++) { | |
d430d3d7 JB |
163 | /* |
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 | } | |
4c3ef6d7 JB |
171 | } |
172 | ||
97ce2c88 | 173 | void __init jump_label_init(void) |
bf5438fc | 174 | { |
bf5438fc JB |
175 | struct jump_entry *iter_start = __start___jump_table; |
176 | struct jump_entry *iter_stop = __stop___jump_table; | |
d430d3d7 | 177 | struct jump_label_key *key = NULL; |
bf5438fc JB |
178 | struct jump_entry *iter; |
179 | ||
91bad2f8 | 180 | jump_label_lock(); |
d430d3d7 JB |
181 | jump_label_sort_entries(iter_start, iter_stop); |
182 | ||
183 | for (iter = iter_start; iter < iter_stop; iter++) { | |
37348804 JF |
184 | struct jump_label_key *iterk; |
185 | ||
186 | iterk = (struct jump_label_key *)(unsigned long)iter->key; | |
20284aa7 JF |
187 | arch_jump_label_transform_static(iter, jump_label_enabled(iterk) ? |
188 | JUMP_LABEL_ENABLE : JUMP_LABEL_DISABLE); | |
37348804 | 189 | if (iterk == key) |
d430d3d7 JB |
190 | continue; |
191 | ||
37348804 | 192 | key = iterk; |
d430d3d7 JB |
193 | key->entries = iter; |
194 | #ifdef CONFIG_MODULES | |
195 | key->next = NULL; | |
196 | #endif | |
bf5438fc | 197 | } |
91bad2f8 | 198 | jump_label_unlock(); |
bf5438fc | 199 | } |
bf5438fc JB |
200 | |
201 | #ifdef CONFIG_MODULES | |
202 | ||
d430d3d7 JB |
203 | struct jump_label_mod { |
204 | struct jump_label_mod *next; | |
205 | struct jump_entry *entries; | |
206 | struct module *mod; | |
207 | }; | |
208 | ||
209 | static 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 | ||
224 | static 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) { | |
7cbc5b8d JO |
229 | struct module *m = mod->mod; |
230 | ||
231 | __jump_label_update(key, mod->entries, | |
232 | m->jump_entries + m->num_jump_entries, | |
233 | enable); | |
d430d3d7 JB |
234 | 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 | */ | |
246 | void jump_label_apply_nops(struct module *mod) | |
bf5438fc | 247 | { |
d430d3d7 JB |
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 | ||
ac99b862 PZ |
256 | 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 | } | |
bf5438fc JB |
263 | } |
264 | ||
d430d3d7 | 265 | static int jump_label_add_module(struct module *mod) |
bf5438fc | 266 | { |
d430d3d7 JB |
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; | |
bf5438fc JB |
272 | |
273 | /* if the module doesn't have jump label entries, just return */ | |
d430d3d7 | 274 | if (iter_start == iter_stop) |
bf5438fc JB |
275 | return 0; |
276 | ||
d430d3d7 JB |
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; | |
bf5438fc | 290 | } |
d430d3d7 JB |
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)) | |
ac99b862 | 302 | __jump_label_update(key, iter, iter_stop, JUMP_LABEL_ENABLE); |
bf5438fc | 303 | } |
d430d3d7 | 304 | |
bf5438fc JB |
305 | return 0; |
306 | } | |
307 | ||
d430d3d7 | 308 | static void jump_label_del_module(struct module *mod) |
bf5438fc | 309 | { |
d430d3d7 JB |
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; | |
bf5438fc | 315 | |
d430d3d7 JB |
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; | |
bf5438fc | 327 | |
d430d3d7 JB |
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); | |
bf5438fc JB |
336 | } |
337 | } | |
338 | } | |
339 | ||
d430d3d7 | 340 | static void jump_label_invalidate_module_init(struct module *mod) |
b842f8fa | 341 | { |
d430d3d7 JB |
342 | struct jump_entry *iter_start = mod->jump_entries; |
343 | struct jump_entry *iter_stop = iter_start + mod->num_jump_entries; | |
b842f8fa | 344 | struct jump_entry *iter; |
b842f8fa | 345 | |
d430d3d7 JB |
346 | for (iter = iter_start; iter < iter_stop; iter++) { |
347 | if (within_module_init(iter->code, mod)) | |
348 | iter->code = 0; | |
b842f8fa JB |
349 | } |
350 | } | |
351 | ||
bf5438fc JB |
352 | static int |
353 | jump_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: | |
91bad2f8 | 361 | jump_label_lock(); |
d430d3d7 | 362 | ret = jump_label_add_module(mod); |
bf5438fc | 363 | if (ret) |
d430d3d7 | 364 | jump_label_del_module(mod); |
91bad2f8 | 365 | jump_label_unlock(); |
bf5438fc JB |
366 | break; |
367 | case MODULE_STATE_GOING: | |
91bad2f8 | 368 | jump_label_lock(); |
d430d3d7 | 369 | jump_label_del_module(mod); |
91bad2f8 | 370 | jump_label_unlock(); |
bf5438fc | 371 | break; |
b842f8fa | 372 | case MODULE_STATE_LIVE: |
91bad2f8 | 373 | jump_label_lock(); |
d430d3d7 | 374 | jump_label_invalidate_module_init(mod); |
91bad2f8 | 375 | jump_label_unlock(); |
b842f8fa | 376 | break; |
bf5438fc | 377 | } |
bf5438fc | 378 | |
d430d3d7 | 379 | return notifier_from_errno(ret); |
bf5438fc JB |
380 | } |
381 | ||
382 | struct notifier_block jump_label_module_nb = { | |
383 | .notifier_call = jump_label_module_notify, | |
d430d3d7 | 384 | .priority = 1, /* higher than tracepoints */ |
bf5438fc JB |
385 | }; |
386 | ||
d430d3d7 | 387 | static __init int jump_label_init_module(void) |
bf5438fc JB |
388 | { |
389 | return register_module_notifier(&jump_label_module_nb); | |
390 | } | |
d430d3d7 | 391 | early_initcall(jump_label_init_module); |
bf5438fc JB |
392 | |
393 | #endif /* CONFIG_MODULES */ | |
394 | ||
d430d3d7 JB |
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. | |
404 | * Caller must hold jump_label_mutex. | |
405 | * | |
406 | * returns 1 if there is an overlap, 0 otherwise | |
407 | */ | |
408 | int jump_label_text_reserved(void *start, void *end) | |
409 | { | |
410 | int ret = __jump_label_text_reserved(__start___jump_table, | |
411 | __stop___jump_table, start, end); | |
412 | ||
413 | if (ret) | |
414 | return ret; | |
415 | ||
416 | #ifdef CONFIG_MODULES | |
417 | ret = __jump_label_mod_text_reserved(start, end); | |
418 | #endif | |
419 | return ret; | |
420 | } | |
421 | ||
422 | static void jump_label_update(struct jump_label_key *key, int enable) | |
423 | { | |
140fe3b1 | 424 | struct jump_entry *entry = key->entries, *stop = __stop___jump_table; |
d430d3d7 JB |
425 | |
426 | #ifdef CONFIG_MODULES | |
a746e3cc | 427 | struct module *mod = __module_address((unsigned long)key); |
140fe3b1 | 428 | |
d430d3d7 | 429 | __jump_label_mod_update(key, enable); |
140fe3b1 XG |
430 | |
431 | if (mod) | |
432 | stop = mod->jump_entries + mod->num_jump_entries; | |
d430d3d7 | 433 | #endif |
140fe3b1 XG |
434 | /* if there are no users, entry can be NULL */ |
435 | if (entry) | |
436 | __jump_label_update(key, entry, stop, enable); | |
d430d3d7 JB |
437 | } |
438 | ||
bf5438fc | 439 | #endif |