4 * Copyright (C) 2009 Jason Baron <jbaron@redhat.com>
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>
17 #ifdef HAVE_JUMP_LABEL
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
];
23 /* mutex to protect coming/going of the the jump_label table */
24 static DEFINE_MUTEX(jump_label_mutex
);
26 struct jump_label_entry
{
27 struct hlist_node hlist
;
28 struct jump_entry
*table
;
30 /* hang modules off here */
31 struct hlist_head modules
;
35 struct jump_label_module_entry
{
36 struct hlist_node hlist
;
37 struct jump_entry
*table
;
42 void jump_label_lock(void)
44 mutex_lock(&jump_label_mutex
);
47 void jump_label_unlock(void)
49 mutex_unlock(&jump_label_mutex
);
52 static int jump_label_cmp(const void *a
, const void *b
)
54 const struct jump_entry
*jea
= a
;
55 const struct jump_entry
*jeb
= b
;
57 if (jea
->key
< jeb
->key
)
60 if (jea
->key
> jeb
->key
)
67 sort_jump_label_entries(struct jump_entry
*start
, struct jump_entry
*stop
)
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
);
76 static struct jump_label_entry
*get_jump_label_entry(jump_label_t key
)
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);
83 head
= &jump_label_table
[hash
& (JUMP_LABEL_TABLE_SIZE
- 1)];
84 hlist_for_each_entry(e
, node
, head
, hlist
) {
91 static struct jump_label_entry
*
92 add_jump_label_entry(jump_label_t key
, int nr_entries
, struct jump_entry
*table
)
94 struct hlist_head
*head
;
95 struct jump_label_entry
*e
;
98 e
= get_jump_label_entry(key
);
100 return ERR_PTR(-EEXIST
);
102 e
= kmalloc(sizeof(struct jump_label_entry
), GFP_KERNEL
);
104 return ERR_PTR(-ENOMEM
);
106 hash
= jhash((void *)&key
, sizeof(jump_label_t
), 0);
107 head
= &jump_label_table
[hash
& (JUMP_LABEL_TABLE_SIZE
- 1)];
110 e
->nr_entries
= nr_entries
;
111 INIT_HLIST_HEAD(&(e
->modules
));
112 hlist_add_head(&e
->hlist
, head
);
117 build_jump_label_hashtable(struct jump_entry
*start
, struct jump_entry
*stop
)
119 struct jump_entry
*iter
, *iter_begin
;
120 struct jump_label_entry
*entry
;
123 sort_jump_label_entries(start
, stop
);
125 while (iter
< stop
) {
126 entry
= get_jump_label_entry(iter
->key
);
130 while ((iter
< stop
) &&
131 (iter
->key
== iter_begin
->key
)) {
135 entry
= add_jump_label_entry(iter_begin
->key
,
138 return PTR_ERR(entry
);
140 WARN_ONCE(1, KERN_ERR
"build_jump_hashtable: unexpected entry!\n");
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
152 * Will enable/disable the jump for jump label @key, depending on the
157 void jump_label_update(unsigned long key
, enum jump_label_type type
)
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
;
166 entry
= get_jump_label_entry((jump_label_t
)key
);
168 count
= entry
->nr_entries
;
171 if (kernel_text_address(iter
->code
))
172 arch_jump_label_transform(iter
, type
);
175 /* eanble/disable jump labels in modules */
176 hlist_for_each_entry(e_module
, module_node
, &(entry
->modules
),
178 count
= e_module
->nr_entries
;
179 iter
= e_module
->table
;
182 kernel_text_address(iter
->code
))
183 arch_jump_label_transform(iter
, type
);
191 static int addr_conflict(struct jump_entry
*entry
, void *start
, void *end
)
193 if (entry
->code
<= (unsigned long)end
&&
194 entry
->code
+ JUMP_LABEL_NOP_SIZE
> (unsigned long)start
)
200 #ifdef CONFIG_MODULES
202 static int module_conflict(void *start
, void *end
)
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
;
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
,
217 &(e
->modules
), hlist
) {
218 count
= e_module
->nr_entries
;
219 iter
= e_module
->table
;
221 if (addr_conflict(iter
, start
, end
)) {
237 * jump_label_text_reserved - check if addr range is reserved
238 * @start: start text addr
239 * @end: end text addr
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.
247 * returns 1 if there is an overlap, 0 otherwise
249 int jump_label_text_reserved(void *start
, void *end
)
251 struct jump_entry
*iter
;
252 struct jump_entry
*iter_start
= __start___jump_table
;
253 struct jump_entry
*iter_stop
= __start___jump_table
;
257 while (iter
< iter_stop
) {
258 if (addr_conflict(iter
, start
, end
)) {
265 /* now check modules */
266 #ifdef CONFIG_MODULES
267 conflict
= module_conflict(start
, end
);
274 * Not all archs need this.
276 void __weak
arch_jump_label_text_poke_early(jump_label_t addr
)
280 static __init
int init_jump_label(void)
283 struct jump_entry
*iter_start
= __start___jump_table
;
284 struct jump_entry
*iter_stop
= __stop___jump_table
;
285 struct jump_entry
*iter
;
288 ret
= build_jump_label_hashtable(__start___jump_table
,
289 __stop___jump_table
);
291 while (iter
< iter_stop
) {
292 arch_jump_label_text_poke_early(iter
->code
);
298 early_initcall(init_jump_label
);
300 #ifdef CONFIG_MODULES
302 static struct jump_label_module_entry
*
303 add_jump_label_module_entry(struct jump_label_entry
*entry
,
304 struct jump_entry
*iter_begin
,
305 int count
, struct module
*mod
)
307 struct jump_label_module_entry
*e
;
309 e
= kmalloc(sizeof(struct jump_label_module_entry
), GFP_KERNEL
);
311 return ERR_PTR(-ENOMEM
);
313 e
->nr_entries
= count
;
314 e
->table
= iter_begin
;
315 hlist_add_head(&e
->hlist
, &entry
->modules
);
319 static int add_jump_label_module(struct module
*mod
)
321 struct jump_entry
*iter
, *iter_begin
;
322 struct jump_label_entry
*entry
;
323 struct jump_label_module_entry
*module_entry
;
326 /* if the module doesn't have jump label entries, just return */
327 if (!mod
->num_jump_entries
)
330 sort_jump_label_entries(mod
->jump_entries
,
331 mod
->jump_entries
+ mod
->num_jump_entries
);
332 iter
= mod
->jump_entries
;
333 while (iter
< mod
->jump_entries
+ mod
->num_jump_entries
) {
334 entry
= get_jump_label_entry(iter
->key
);
337 while ((iter
< mod
->jump_entries
+ mod
->num_jump_entries
) &&
338 (iter
->key
== iter_begin
->key
)) {
343 entry
= add_jump_label_entry(iter_begin
->key
, 0, NULL
);
345 return PTR_ERR(entry
);
347 module_entry
= add_jump_label_module_entry(entry
, iter_begin
,
349 if (IS_ERR(module_entry
))
350 return PTR_ERR(module_entry
);
355 static void remove_jump_label_module(struct module
*mod
)
357 struct hlist_head
*head
;
358 struct hlist_node
*node
, *node_next
, *module_node
, *module_node_next
;
359 struct jump_label_entry
*e
;
360 struct jump_label_module_entry
*e_module
;
363 /* if the module doesn't have jump label entries, just return */
364 if (!mod
->num_jump_entries
)
367 for (i
= 0; i
< JUMP_LABEL_TABLE_SIZE
; i
++) {
368 head
= &jump_label_table
[i
];
369 hlist_for_each_entry_safe(e
, node
, node_next
, head
, hlist
) {
370 hlist_for_each_entry_safe(e_module
, module_node
,
372 &(e
->modules
), hlist
) {
373 if (e_module
->mod
== mod
) {
374 hlist_del(&e_module
->hlist
);
378 if (hlist_empty(&e
->modules
) && (e
->nr_entries
== 0)) {
379 hlist_del(&e
->hlist
);
386 static void remove_jump_label_module_init(struct module
*mod
)
388 struct hlist_head
*head
;
389 struct hlist_node
*node
, *node_next
, *module_node
, *module_node_next
;
390 struct jump_label_entry
*e
;
391 struct jump_label_module_entry
*e_module
;
392 struct jump_entry
*iter
;
395 /* if the module doesn't have jump label entries, just return */
396 if (!mod
->num_jump_entries
)
399 for (i
= 0; i
< JUMP_LABEL_TABLE_SIZE
; i
++) {
400 head
= &jump_label_table
[i
];
401 hlist_for_each_entry_safe(e
, node
, node_next
, head
, hlist
) {
402 hlist_for_each_entry_safe(e_module
, module_node
,
404 &(e
->modules
), hlist
) {
405 if (e_module
->mod
!= mod
)
407 count
= e_module
->nr_entries
;
408 iter
= e_module
->table
;
410 if (within_module_init(iter
->code
, mod
))
420 jump_label_module_notify(struct notifier_block
*self
, unsigned long val
,
423 struct module
*mod
= data
;
427 case MODULE_STATE_COMING
:
429 ret
= add_jump_label_module(mod
);
431 remove_jump_label_module(mod
);
434 case MODULE_STATE_GOING
:
436 remove_jump_label_module(mod
);
439 case MODULE_STATE_LIVE
:
441 remove_jump_label_module_init(mod
);
449 * apply_jump_label_nops - patch module jump labels with arch_get_jump_label_nop()
450 * @mod: module to patch
452 * Allow for run-time selection of the optimal nops. Before the module
453 * loads patch these with arch_get_jump_label_nop(), which is specified by
454 * the arch specific jump label code.
456 void jump_label_apply_nops(struct module
*mod
)
458 struct jump_entry
*iter
;
460 /* if the module doesn't have jump label entries, just return */
461 if (!mod
->num_jump_entries
)
464 iter
= mod
->jump_entries
;
465 while (iter
< mod
->jump_entries
+ mod
->num_jump_entries
) {
466 arch_jump_label_text_poke_early(iter
->code
);
471 struct notifier_block jump_label_module_nb
= {
472 .notifier_call
= jump_label_module_notify
,
476 static __init
int init_jump_label_module(void)
478 return register_module_notifier(&jump_label_module_nb
);
480 early_initcall(init_jump_label_module
);
482 #endif /* CONFIG_MODULES */