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 static int jump_label_cmp(const void *a
, const void *b
)
44 const struct jump_entry
*jea
= a
;
45 const struct jump_entry
*jeb
= b
;
47 if (jea
->key
< jeb
->key
)
50 if (jea
->key
> jeb
->key
)
57 sort_jump_label_entries(struct jump_entry
*start
, struct jump_entry
*stop
)
61 size
= (((unsigned long)stop
- (unsigned long)start
)
62 / sizeof(struct jump_entry
));
63 sort(start
, size
, sizeof(struct jump_entry
), jump_label_cmp
, NULL
);
66 static struct jump_label_entry
*get_jump_label_entry(jump_label_t key
)
68 struct hlist_head
*head
;
69 struct hlist_node
*node
;
70 struct jump_label_entry
*e
;
71 u32 hash
= jhash((void *)&key
, sizeof(jump_label_t
), 0);
73 head
= &jump_label_table
[hash
& (JUMP_LABEL_TABLE_SIZE
- 1)];
74 hlist_for_each_entry(e
, node
, head
, hlist
) {
81 static struct jump_label_entry
*
82 add_jump_label_entry(jump_label_t key
, int nr_entries
, struct jump_entry
*table
)
84 struct hlist_head
*head
;
85 struct jump_label_entry
*e
;
88 e
= get_jump_label_entry(key
);
90 return ERR_PTR(-EEXIST
);
92 e
= kmalloc(sizeof(struct jump_label_entry
), GFP_KERNEL
);
94 return ERR_PTR(-ENOMEM
);
96 hash
= jhash((void *)&key
, sizeof(jump_label_t
), 0);
97 head
= &jump_label_table
[hash
& (JUMP_LABEL_TABLE_SIZE
- 1)];
100 e
->nr_entries
= nr_entries
;
101 INIT_HLIST_HEAD(&(e
->modules
));
102 hlist_add_head(&e
->hlist
, head
);
107 build_jump_label_hashtable(struct jump_entry
*start
, struct jump_entry
*stop
)
109 struct jump_entry
*iter
, *iter_begin
;
110 struct jump_label_entry
*entry
;
113 sort_jump_label_entries(start
, stop
);
115 while (iter
< stop
) {
116 entry
= get_jump_label_entry(iter
->key
);
120 while ((iter
< stop
) &&
121 (iter
->key
== iter_begin
->key
)) {
125 entry
= add_jump_label_entry(iter_begin
->key
,
128 return PTR_ERR(entry
);
130 WARN_ONCE(1, KERN_ERR
"build_jump_hashtable: unexpected entry!\n");
138 * jump_label_update - update jump label text
139 * @key - key value associated with a a jump label
140 * @type - enum set to JUMP_LABEL_ENABLE or JUMP_LABEL_DISABLE
142 * Will enable/disable the jump for jump label @key, depending on the
147 void jump_label_update(unsigned long key
, enum jump_label_type type
)
149 struct jump_entry
*iter
;
150 struct jump_label_entry
*entry
;
151 struct hlist_node
*module_node
;
152 struct jump_label_module_entry
*e_module
;
155 mutex_lock(&jump_label_mutex
);
156 entry
= get_jump_label_entry((jump_label_t
)key
);
158 count
= entry
->nr_entries
;
161 if (kernel_text_address(iter
->code
))
162 arch_jump_label_transform(iter
, type
);
165 /* eanble/disable jump labels in modules */
166 hlist_for_each_entry(e_module
, module_node
, &(entry
->modules
),
168 count
= e_module
->nr_entries
;
169 iter
= e_module
->table
;
171 if (kernel_text_address(iter
->code
))
172 arch_jump_label_transform(iter
, type
);
177 mutex_unlock(&jump_label_mutex
);
180 static int addr_conflict(struct jump_entry
*entry
, void *start
, void *end
)
182 if (entry
->code
<= (unsigned long)end
&&
183 entry
->code
+ JUMP_LABEL_NOP_SIZE
> (unsigned long)start
)
189 #ifdef CONFIG_MODULES
191 static int module_conflict(void *start
, void *end
)
193 struct hlist_head
*head
;
194 struct hlist_node
*node
, *node_next
, *module_node
, *module_node_next
;
195 struct jump_label_entry
*e
;
196 struct jump_label_module_entry
*e_module
;
197 struct jump_entry
*iter
;
201 for (i
= 0; i
< JUMP_LABEL_TABLE_SIZE
; i
++) {
202 head
= &jump_label_table
[i
];
203 hlist_for_each_entry_safe(e
, node
, node_next
, head
, hlist
) {
204 hlist_for_each_entry_safe(e_module
, module_node
,
206 &(e
->modules
), hlist
) {
207 count
= e_module
->nr_entries
;
208 iter
= e_module
->table
;
210 if (addr_conflict(iter
, start
, end
)) {
226 * jump_label_text_reserved - check if addr range is reserved
227 * @start: start text addr
228 * @end: end text addr
230 * checks if the text addr located between @start and @end
231 * overlaps with any of the jump label patch addresses. Code
232 * that wants to modify kernel text should first verify that
233 * it does not overlap with any of the jump label addresses.
235 * returns 1 if there is an overlap, 0 otherwise
237 int jump_label_text_reserved(void *start
, void *end
)
239 struct jump_entry
*iter
;
240 struct jump_entry
*iter_start
= __start___jump_table
;
241 struct jump_entry
*iter_stop
= __start___jump_table
;
244 mutex_lock(&jump_label_mutex
);
246 while (iter
< iter_stop
) {
247 if (addr_conflict(iter
, start
, end
)) {
254 /* now check modules */
255 #ifdef CONFIG_MODULES
256 conflict
= module_conflict(start
, end
);
259 mutex_unlock(&jump_label_mutex
);
263 static __init
int init_jump_label(void)
266 struct jump_entry
*iter_start
= __start___jump_table
;
267 struct jump_entry
*iter_stop
= __stop___jump_table
;
268 struct jump_entry
*iter
;
270 mutex_lock(&jump_label_mutex
);
271 ret
= build_jump_label_hashtable(__start___jump_table
,
272 __stop___jump_table
);
274 while (iter
< iter_stop
) {
275 arch_jump_label_text_poke_early(iter
->code
);
278 mutex_unlock(&jump_label_mutex
);
281 early_initcall(init_jump_label
);
283 #ifdef CONFIG_MODULES
285 static struct jump_label_module_entry
*
286 add_jump_label_module_entry(struct jump_label_entry
*entry
,
287 struct jump_entry
*iter_begin
,
288 int count
, struct module
*mod
)
290 struct jump_label_module_entry
*e
;
292 e
= kmalloc(sizeof(struct jump_label_module_entry
), GFP_KERNEL
);
294 return ERR_PTR(-ENOMEM
);
296 e
->nr_entries
= count
;
297 e
->table
= iter_begin
;
298 hlist_add_head(&e
->hlist
, &entry
->modules
);
302 static int add_jump_label_module(struct module
*mod
)
304 struct jump_entry
*iter
, *iter_begin
;
305 struct jump_label_entry
*entry
;
306 struct jump_label_module_entry
*module_entry
;
309 /* if the module doesn't have jump label entries, just return */
310 if (!mod
->num_jump_entries
)
313 sort_jump_label_entries(mod
->jump_entries
,
314 mod
->jump_entries
+ mod
->num_jump_entries
);
315 iter
= mod
->jump_entries
;
316 while (iter
< mod
->jump_entries
+ mod
->num_jump_entries
) {
317 entry
= get_jump_label_entry(iter
->key
);
320 while ((iter
< mod
->jump_entries
+ mod
->num_jump_entries
) &&
321 (iter
->key
== iter_begin
->key
)) {
326 entry
= add_jump_label_entry(iter_begin
->key
, 0, NULL
);
328 return PTR_ERR(entry
);
330 module_entry
= add_jump_label_module_entry(entry
, iter_begin
,
332 if (IS_ERR(module_entry
))
333 return PTR_ERR(module_entry
);
338 static void remove_jump_label_module(struct module
*mod
)
340 struct hlist_head
*head
;
341 struct hlist_node
*node
, *node_next
, *module_node
, *module_node_next
;
342 struct jump_label_entry
*e
;
343 struct jump_label_module_entry
*e_module
;
346 /* if the module doesn't have jump label entries, just return */
347 if (!mod
->num_jump_entries
)
350 for (i
= 0; i
< JUMP_LABEL_TABLE_SIZE
; i
++) {
351 head
= &jump_label_table
[i
];
352 hlist_for_each_entry_safe(e
, node
, node_next
, head
, hlist
) {
353 hlist_for_each_entry_safe(e_module
, module_node
,
355 &(e
->modules
), hlist
) {
356 if (e_module
->mod
== mod
) {
357 hlist_del(&e_module
->hlist
);
361 if (hlist_empty(&e
->modules
) && (e
->nr_entries
== 0)) {
362 hlist_del(&e
->hlist
);
370 jump_label_module_notify(struct notifier_block
*self
, unsigned long val
,
373 struct module
*mod
= data
;
377 case MODULE_STATE_COMING
:
378 mutex_lock(&jump_label_mutex
);
379 ret
= add_jump_label_module(mod
);
381 remove_jump_label_module(mod
);
382 mutex_unlock(&jump_label_mutex
);
384 case MODULE_STATE_GOING
:
385 mutex_lock(&jump_label_mutex
);
386 remove_jump_label_module(mod
);
387 mutex_unlock(&jump_label_mutex
);
394 * apply_jump_label_nops - patch module jump labels with arch_get_jump_label_nop()
395 * @mod: module to patch
397 * Allow for run-time selection of the optimal nops. Before the module
398 * loads patch these with arch_get_jump_label_nop(), which is specified by
399 * the arch specific jump label code.
401 void jump_label_apply_nops(struct module
*mod
)
403 struct jump_entry
*iter
;
405 /* if the module doesn't have jump label entries, just return */
406 if (!mod
->num_jump_entries
)
409 iter
= mod
->jump_entries
;
410 while (iter
< mod
->jump_entries
+ mod
->num_jump_entries
) {
411 arch_jump_label_text_poke_early(iter
->code
);
416 struct notifier_block jump_label_module_nb
= {
417 .notifier_call
= jump_label_module_notify
,
421 static __init
int init_jump_label_module(void)
423 return register_module_notifier(&jump_label_module_nb
);
425 early_initcall(init_jump_label_module
);
427 #endif /* CONFIG_MODULES */