jump label: Fix deadlock b/w jump_label_mutex vs. text_mutex
[linux-2.6.git] / kernel / jump_label.c
1 /*
2  * jump label support
3  *
4  * Copyright (C) 2009 Jason Baron <jbaron@redhat.com>
5  *
6  */
7 #include <linux/jump_label.h>
8 #include <linux/memory.h>
9 #include <linux/uaccess.h>
10 #include <linux/module.h>
11 #include <linux/list.h>
12 #include <linux/jhash.h>
13 #include <linux/slab.h>
14 #include <linux/sort.h>
15 #include <linux/err.h>
16
17 #ifdef HAVE_JUMP_LABEL
18
19 #define JUMP_LABEL_HASH_BITS 6
20 #define JUMP_LABEL_TABLE_SIZE (1 << JUMP_LABEL_HASH_BITS)
21 static struct hlist_head jump_label_table[JUMP_LABEL_TABLE_SIZE];
22
23 /* mutex to protect coming/going of the the jump_label table */
24 static DEFINE_MUTEX(jump_label_mutex);
25
26 struct jump_label_entry {
27         struct hlist_node hlist;
28         struct jump_entry *table;
29         int nr_entries;
30         /* hang modules off here */
31         struct hlist_head modules;
32         unsigned long key;
33 };
34
35 struct jump_label_module_entry {
36         struct hlist_node hlist;
37         struct jump_entry *table;
38         int nr_entries;
39         struct module *mod;
40 };
41
42 void jump_label_lock(void)
43 {
44         mutex_lock(&jump_label_mutex);
45 }
46
47 void jump_label_unlock(void)
48 {
49         mutex_unlock(&jump_label_mutex);
50 }
51
52 static int jump_label_cmp(const void *a, const void *b)
53 {
54         const struct jump_entry *jea = a;
55         const struct jump_entry *jeb = b;
56
57         if (jea->key < jeb->key)
58                 return -1;
59
60         if (jea->key > jeb->key)
61                 return 1;
62
63         return 0;
64 }
65
66 static void
67 sort_jump_label_entries(struct jump_entry *start, struct jump_entry *stop)
68 {
69         unsigned long size;
70
71         size = (((unsigned long)stop - (unsigned long)start)
72                                         / sizeof(struct jump_entry));
73         sort(start, size, sizeof(struct jump_entry), jump_label_cmp, NULL);
74 }
75
76 static struct jump_label_entry *get_jump_label_entry(jump_label_t key)
77 {
78         struct hlist_head *head;
79         struct hlist_node *node;
80         struct jump_label_entry *e;
81         u32 hash = jhash((void *)&key, sizeof(jump_label_t), 0);
82
83         head = &jump_label_table[hash & (JUMP_LABEL_TABLE_SIZE - 1)];
84         hlist_for_each_entry(e, node, head, hlist) {
85                 if (key == e->key)
86                         return e;
87         }
88         return NULL;
89 }
90
91 static struct jump_label_entry *
92 add_jump_label_entry(jump_label_t key, int nr_entries, struct jump_entry *table)
93 {
94         struct hlist_head *head;
95         struct jump_label_entry *e;
96         u32 hash;
97
98         e = get_jump_label_entry(key);
99         if (e)
100                 return ERR_PTR(-EEXIST);
101
102         e = kmalloc(sizeof(struct jump_label_entry), GFP_KERNEL);
103         if (!e)
104                 return ERR_PTR(-ENOMEM);
105
106         hash = jhash((void *)&key, sizeof(jump_label_t), 0);
107         head = &jump_label_table[hash & (JUMP_LABEL_TABLE_SIZE - 1)];
108         e->key = key;
109         e->table = table;
110         e->nr_entries = nr_entries;
111         INIT_HLIST_HEAD(&(e->modules));
112         hlist_add_head(&e->hlist, head);
113         return e;
114 }
115
116 static int
117 build_jump_label_hashtable(struct jump_entry *start, struct jump_entry *stop)
118 {
119         struct jump_entry *iter, *iter_begin;
120         struct jump_label_entry *entry;
121         int count;
122
123         sort_jump_label_entries(start, stop);
124         iter = start;
125         while (iter < stop) {
126                 entry = get_jump_label_entry(iter->key);
127                 if (!entry) {
128                         iter_begin = iter;
129                         count = 0;
130                         while ((iter < stop) &&
131                                 (iter->key == iter_begin->key)) {
132                                 iter++;
133                                 count++;
134                         }
135                         entry = add_jump_label_entry(iter_begin->key,
136                                                         count, iter_begin);
137                         if (IS_ERR(entry))
138                                 return PTR_ERR(entry);
139                  } else {
140                         WARN_ONCE(1, KERN_ERR "build_jump_hashtable: unexpected entry!\n");
141                         return -1;
142                 }
143         }
144         return 0;
145 }
146
147 /***
148  * jump_label_update - update jump label text
149  * @key -  key value associated with a a jump label
150  * @type - enum set to JUMP_LABEL_ENABLE or JUMP_LABEL_DISABLE
151  *
152  * Will enable/disable the jump for jump label @key, depending on the
153  * value of @type.
154  *
155  */
156
157 void jump_label_update(unsigned long key, enum jump_label_type type)
158 {
159         struct jump_entry *iter;
160         struct jump_label_entry *entry;
161         struct hlist_node *module_node;
162         struct jump_label_module_entry *e_module;
163         int count;
164
165         jump_label_lock();
166         entry = get_jump_label_entry((jump_label_t)key);
167         if (entry) {
168                 count = entry->nr_entries;
169                 iter = entry->table;
170                 while (count--) {
171                         if (kernel_text_address(iter->code))
172                                 arch_jump_label_transform(iter, type);
173                         iter++;
174                 }
175                 /* eanble/disable jump labels in modules */
176                 hlist_for_each_entry(e_module, module_node, &(entry->modules),
177                                                         hlist) {
178                         count = e_module->nr_entries;
179                         iter = e_module->table;
180                         while (count--) {
181                                 if (iter->key &&
182                                                 kernel_text_address(iter->code))
183                                         arch_jump_label_transform(iter, type);
184                                 iter++;
185                         }
186                 }
187         }
188         jump_label_unlock();
189 }
190
191 static int addr_conflict(struct jump_entry *entry, void *start, void *end)
192 {
193         if (entry->code <= (unsigned long)end &&
194                 entry->code + JUMP_LABEL_NOP_SIZE > (unsigned long)start)
195                 return 1;
196
197         return 0;
198 }
199
200 #ifdef CONFIG_MODULES
201
202 static int module_conflict(void *start, void *end)
203 {
204         struct hlist_head *head;
205         struct hlist_node *node, *node_next, *module_node, *module_node_next;
206         struct jump_label_entry *e;
207         struct jump_label_module_entry *e_module;
208         struct jump_entry *iter;
209         int i, count;
210         int conflict = 0;
211
212         for (i = 0; i < JUMP_LABEL_TABLE_SIZE; i++) {
213                 head = &jump_label_table[i];
214                 hlist_for_each_entry_safe(e, node, node_next, head, hlist) {
215                         hlist_for_each_entry_safe(e_module, module_node,
216                                                         module_node_next,
217                                                         &(e->modules), hlist) {
218                                 count = e_module->nr_entries;
219                                 iter = e_module->table;
220                                 while (count--) {
221                                         if (addr_conflict(iter, start, end)) {
222                                                 conflict = 1;
223                                                 goto out;
224                                         }
225                                         iter++;
226                                 }
227                         }
228                 }
229         }
230 out:
231         return conflict;
232 }
233
234 #endif
235
236 /***
237  * jump_label_text_reserved - check if addr range is reserved
238  * @start: start text addr
239  * @end: end text addr
240  *
241  * checks if the text addr located between @start and @end
242  * overlaps with any of the jump label patch addresses. Code
243  * that wants to modify kernel text should first verify that
244  * it does not overlap with any of the jump label addresses.
245  * Caller must hold jump_label_mutex.
246  *
247  * returns 1 if there is an overlap, 0 otherwise
248  */
249 int jump_label_text_reserved(void *start, void *end)
250 {
251         struct jump_entry *iter;
252         struct jump_entry *iter_start = __start___jump_table;
253         struct jump_entry *iter_stop = __start___jump_table;
254         int conflict = 0;
255
256         iter = iter_start;
257         while (iter < iter_stop) {
258                 if (addr_conflict(iter, start, end)) {
259                         conflict = 1;
260                         goto out;
261                 }
262                 iter++;
263         }
264
265         /* now check modules */
266 #ifdef CONFIG_MODULES
267         conflict = module_conflict(start, end);
268 #endif
269 out:
270         return conflict;
271 }
272
273 static __init int init_jump_label(void)
274 {
275         int ret;
276         struct jump_entry *iter_start = __start___jump_table;
277         struct jump_entry *iter_stop = __stop___jump_table;
278         struct jump_entry *iter;
279
280         jump_label_lock();
281         ret = build_jump_label_hashtable(__start___jump_table,
282                                          __stop___jump_table);
283         iter = iter_start;
284         while (iter < iter_stop) {
285                 arch_jump_label_text_poke_early(iter->code);
286                 iter++;
287         }
288         jump_label_unlock();
289         return ret;
290 }
291 early_initcall(init_jump_label);
292
293 #ifdef CONFIG_MODULES
294
295 static struct jump_label_module_entry *
296 add_jump_label_module_entry(struct jump_label_entry *entry,
297                             struct jump_entry *iter_begin,
298                             int count, struct module *mod)
299 {
300         struct jump_label_module_entry *e;
301
302         e = kmalloc(sizeof(struct jump_label_module_entry), GFP_KERNEL);
303         if (!e)
304                 return ERR_PTR(-ENOMEM);
305         e->mod = mod;
306         e->nr_entries = count;
307         e->table = iter_begin;
308         hlist_add_head(&e->hlist, &entry->modules);
309         return e;
310 }
311
312 static int add_jump_label_module(struct module *mod)
313 {
314         struct jump_entry *iter, *iter_begin;
315         struct jump_label_entry *entry;
316         struct jump_label_module_entry *module_entry;
317         int count;
318
319         /* if the module doesn't have jump label entries, just return */
320         if (!mod->num_jump_entries)
321                 return 0;
322
323         sort_jump_label_entries(mod->jump_entries,
324                                 mod->jump_entries + mod->num_jump_entries);
325         iter = mod->jump_entries;
326         while (iter < mod->jump_entries + mod->num_jump_entries) {
327                 entry = get_jump_label_entry(iter->key);
328                 iter_begin = iter;
329                 count = 0;
330                 while ((iter < mod->jump_entries + mod->num_jump_entries) &&
331                         (iter->key == iter_begin->key)) {
332                                 iter++;
333                                 count++;
334                 }
335                 if (!entry) {
336                         entry = add_jump_label_entry(iter_begin->key, 0, NULL);
337                         if (IS_ERR(entry))
338                                 return PTR_ERR(entry);
339                 }
340                 module_entry = add_jump_label_module_entry(entry, iter_begin,
341                                                            count, mod);
342                 if (IS_ERR(module_entry))
343                         return PTR_ERR(module_entry);
344         }
345         return 0;
346 }
347
348 static void remove_jump_label_module(struct module *mod)
349 {
350         struct hlist_head *head;
351         struct hlist_node *node, *node_next, *module_node, *module_node_next;
352         struct jump_label_entry *e;
353         struct jump_label_module_entry *e_module;
354         int i;
355
356         /* if the module doesn't have jump label entries, just return */
357         if (!mod->num_jump_entries)
358                 return;
359
360         for (i = 0; i < JUMP_LABEL_TABLE_SIZE; i++) {
361                 head = &jump_label_table[i];
362                 hlist_for_each_entry_safe(e, node, node_next, head, hlist) {
363                         hlist_for_each_entry_safe(e_module, module_node,
364                                                   module_node_next,
365                                                   &(e->modules), hlist) {
366                                 if (e_module->mod == mod) {
367                                         hlist_del(&e_module->hlist);
368                                         kfree(e_module);
369                                 }
370                         }
371                         if (hlist_empty(&e->modules) && (e->nr_entries == 0)) {
372                                 hlist_del(&e->hlist);
373                                 kfree(e);
374                         }
375                 }
376         }
377 }
378
379 static void remove_jump_label_module_init(struct module *mod)
380 {
381         struct hlist_head *head;
382         struct hlist_node *node, *node_next, *module_node, *module_node_next;
383         struct jump_label_entry *e;
384         struct jump_label_module_entry *e_module;
385         struct jump_entry *iter;
386         int i, count;
387
388         /* if the module doesn't have jump label entries, just return */
389         if (!mod->num_jump_entries)
390                 return;
391
392         for (i = 0; i < JUMP_LABEL_TABLE_SIZE; i++) {
393                 head = &jump_label_table[i];
394                 hlist_for_each_entry_safe(e, node, node_next, head, hlist) {
395                         hlist_for_each_entry_safe(e_module, module_node,
396                                                   module_node_next,
397                                                   &(e->modules), hlist) {
398                                 if (e_module->mod != mod)
399                                         continue;
400                                 count = e_module->nr_entries;
401                                 iter = e_module->table;
402                                 while (count--) {
403                                         if (within_module_init(iter->code, mod))
404                                                 iter->key = 0;
405                                         iter++;
406                                 }
407                         }
408                 }
409         }
410 }
411
412 static int
413 jump_label_module_notify(struct notifier_block *self, unsigned long val,
414                          void *data)
415 {
416         struct module *mod = data;
417         int ret = 0;
418
419         switch (val) {
420         case MODULE_STATE_COMING:
421                 jump_label_lock();
422                 ret = add_jump_label_module(mod);
423                 if (ret)
424                         remove_jump_label_module(mod);
425                 jump_label_unlock();
426                 break;
427         case MODULE_STATE_GOING:
428                 jump_label_lock();
429                 remove_jump_label_module(mod);
430                 jump_label_unlock();
431                 break;
432         case MODULE_STATE_LIVE:
433                 jump_label_lock();
434                 remove_jump_label_module_init(mod);
435                 jump_label_unlock();
436                 break;
437         }
438         return ret;
439 }
440
441 /***
442  * apply_jump_label_nops - patch module jump labels with arch_get_jump_label_nop()
443  * @mod: module to patch
444  *
445  * Allow for run-time selection of the optimal nops. Before the module
446  * loads patch these with arch_get_jump_label_nop(), which is specified by
447  * the arch specific jump label code.
448  */
449 void jump_label_apply_nops(struct module *mod)
450 {
451         struct jump_entry *iter;
452
453         /* if the module doesn't have jump label entries, just return */
454         if (!mod->num_jump_entries)
455                 return;
456
457         iter = mod->jump_entries;
458         while (iter < mod->jump_entries + mod->num_jump_entries) {
459                 arch_jump_label_text_poke_early(iter->code);
460                 iter++;
461         }
462 }
463
464 struct notifier_block jump_label_module_nb = {
465         .notifier_call = jump_label_module_notify,
466         .priority = 0,
467 };
468
469 static __init int init_jump_label_module(void)
470 {
471         return register_module_notifier(&jump_label_module_nb);
472 }
473 early_initcall(init_jump_label_module);
474
475 #endif /* CONFIG_MODULES */
476
477 #endif