Blame view

kernel/jump_label.c 10.2 KB
bf5438fca   Jason Baron   jump label: Base ...
1
2
3
4
  /*
   * jump label support
   *
   * Copyright (C) 2009 Jason Baron <jbaron@redhat.com>
d430d3d7e   Jason Baron   jump label: Intro...
5
   * Copyright (C) 2011 Peter Zijlstra <pzijlstr@redhat.com>
bf5438fca   Jason Baron   jump label: Base ...
6
7
   *
   */
bf5438fca   Jason Baron   jump label: Base ...
8
9
10
11
  #include <linux/memory.h>
  #include <linux/uaccess.h>
  #include <linux/module.h>
  #include <linux/list.h>
bf5438fca   Jason Baron   jump label: Base ...
12
13
14
  #include <linux/slab.h>
  #include <linux/sort.h>
  #include <linux/err.h>
d430d3d7e   Jason Baron   jump label: Intro...
15
  #include <linux/jump_label.h>
bf5438fca   Jason Baron   jump label: Base ...
16
17
  
  #ifdef HAVE_JUMP_LABEL
bf5438fca   Jason Baron   jump label: Base ...
18
19
  /* mutex to protect coming/going of the the jump_label table */
  static DEFINE_MUTEX(jump_label_mutex);
91bad2f8d   Jason Baron   jump label: Fix d...
20
21
22
23
24
25
26
27
28
  void jump_label_lock(void)
  {
  	mutex_lock(&jump_label_mutex);
  }
  
  void jump_label_unlock(void)
  {
  	mutex_unlock(&jump_label_mutex);
  }
d430d3d7e   Jason Baron   jump label: Intro...
29
30
31
32
  bool jump_label_enabled(struct jump_label_key *key)
  {
  	return !!atomic_read(&key->enabled);
  }
bf5438fca   Jason Baron   jump label: Base ...
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  static int jump_label_cmp(const void *a, const void *b)
  {
  	const struct jump_entry *jea = a;
  	const struct jump_entry *jeb = b;
  
  	if (jea->key < jeb->key)
  		return -1;
  
  	if (jea->key > jeb->key)
  		return 1;
  
  	return 0;
  }
  
  static void
d430d3d7e   Jason Baron   jump label: Intro...
48
  jump_label_sort_entries(struct jump_entry *start, struct jump_entry *stop)
bf5438fca   Jason Baron   jump label: Base ...
49
50
51
52
53
54
55
  {
  	unsigned long size;
  
  	size = (((unsigned long)stop - (unsigned long)start)
  					/ sizeof(struct jump_entry));
  	sort(start, size, sizeof(struct jump_entry), jump_label_cmp, NULL);
  }
d430d3d7e   Jason Baron   jump label: Intro...
56
  static void jump_label_update(struct jump_label_key *key, int enable);
bf5438fca   Jason Baron   jump label: Base ...
57

d430d3d7e   Jason Baron   jump label: Intro...
58
  void jump_label_inc(struct jump_label_key *key)
bf5438fca   Jason Baron   jump label: Base ...
59
  {
d430d3d7e   Jason Baron   jump label: Intro...
60
61
  	if (atomic_inc_not_zero(&key->enabled))
  		return;
bf5438fca   Jason Baron   jump label: Base ...
62

d430d3d7e   Jason Baron   jump label: Intro...
63
  	jump_label_lock();
bbbf7af4b   Gleb Natapov   jump_label: jump_...
64
  	if (atomic_read(&key->enabled) == 0)
d430d3d7e   Jason Baron   jump label: Intro...
65
  		jump_label_update(key, JUMP_LABEL_ENABLE);
bbbf7af4b   Gleb Natapov   jump_label: jump_...
66
  	atomic_inc(&key->enabled);
d430d3d7e   Jason Baron   jump label: Intro...
67
  	jump_label_unlock();
bf5438fca   Jason Baron   jump label: Base ...
68
  }
a65cf5181   Xiao Guangrong   jump-label: expor...
69
  EXPORT_SYMBOL_GPL(jump_label_inc);
bf5438fca   Jason Baron   jump label: Base ...
70

b20295207   Gleb Natapov   perf, core: Rate ...
71
72
  static void __jump_label_dec(struct jump_label_key *key,
  		unsigned long rate_limit, struct delayed_work *work)
bf5438fca   Jason Baron   jump label: Base ...
73
  {
d430d3d7e   Jason Baron   jump label: Intro...
74
75
  	if (!atomic_dec_and_mutex_lock(&key->enabled, &jump_label_mutex))
  		return;
bf5438fca   Jason Baron   jump label: Base ...
76

b20295207   Gleb Natapov   perf, core: Rate ...
77
78
79
80
81
  	if (rate_limit) {
  		atomic_inc(&key->enabled);
  		schedule_delayed_work(work, rate_limit);
  	} else
  		jump_label_update(key, JUMP_LABEL_DISABLE);
91bad2f8d   Jason Baron   jump label: Fix d...
82
  	jump_label_unlock();
bf5438fca   Jason Baron   jump label: Base ...
83
  }
a65cf5181   Xiao Guangrong   jump-label: expor...
84
  EXPORT_SYMBOL_GPL(jump_label_dec);
bf5438fca   Jason Baron   jump label: Base ...
85

b20295207   Gleb Natapov   perf, core: Rate ...
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  static void jump_label_update_timeout(struct work_struct *work)
  {
  	struct jump_label_key_deferred *key =
  		container_of(work, struct jump_label_key_deferred, work.work);
  	__jump_label_dec(&key->key, 0, NULL);
  }
  
  void jump_label_dec(struct jump_label_key *key)
  {
  	__jump_label_dec(key, 0, NULL);
  }
  
  void jump_label_dec_deferred(struct jump_label_key_deferred *key)
  {
  	__jump_label_dec(&key->key, key->timeout, &key->work);
  }
  
  
  void jump_label_rate_limit(struct jump_label_key_deferred *key,
  		unsigned long rl)
  {
  	key->timeout = rl;
  	INIT_DELAYED_WORK(&key->work, jump_label_update_timeout);
  }
4c3ef6d79   Jason Baron   jump label: Add j...
110
111
112
113
114
115
116
117
  static int addr_conflict(struct jump_entry *entry, void *start, void *end)
  {
  	if (entry->code <= (unsigned long)end &&
  		entry->code + JUMP_LABEL_NOP_SIZE > (unsigned long)start)
  		return 1;
  
  	return 0;
  }
d430d3d7e   Jason Baron   jump label: Intro...
118
119
  static int __jump_label_text_reserved(struct jump_entry *iter_start,
  		struct jump_entry *iter_stop, void *start, void *end)
4c3ef6d79   Jason Baron   jump label: Add j...
120
  {
4c3ef6d79   Jason Baron   jump label: Add j...
121
  	struct jump_entry *iter;
4c3ef6d79   Jason Baron   jump label: Add j...
122

4c3ef6d79   Jason Baron   jump label: Add j...
123
124
  	iter = iter_start;
  	while (iter < iter_stop) {
d430d3d7e   Jason Baron   jump label: Intro...
125
126
  		if (addr_conflict(iter, start, end))
  			return 1;
4c3ef6d79   Jason Baron   jump label: Add j...
127
128
  		iter++;
  	}
d430d3d7e   Jason Baron   jump label: Intro...
129
130
  	return 0;
  }
20284aa77   Jeremy Fitzhardinge   jump_label: add a...
131
132
133
134
135
136
  /* 
   * Update code which is definitely not currently executing.
   * Architectures which need heavyweight synchronization to modify
   * running code can override this to make the non-live update case
   * cheaper.
   */
9cdbe1cba   Peter Zijlstra   jump_label, x86: ...
137
  void __weak __init_or_module arch_jump_label_transform_static(struct jump_entry *entry,
20284aa77   Jeremy Fitzhardinge   jump_label: add a...
138
139
140
141
  					    enum jump_label_type type)
  {
  	arch_jump_label_transform(entry, type);	
  }
d430d3d7e   Jason Baron   jump label: Intro...
142
  static void __jump_label_update(struct jump_label_key *key,
7cbc5b8d4   Jiri Olsa   jump_label: Check...
143
144
  				struct jump_entry *entry,
  				struct jump_entry *stop, int enable)
d430d3d7e   Jason Baron   jump label: Intro...
145
  {
7cbc5b8d4   Jiri Olsa   jump_label: Check...
146
147
148
  	for (; (entry < stop) &&
  	      (entry->key == (jump_label_t)(unsigned long)key);
  	      entry++) {
d430d3d7e   Jason Baron   jump label: Intro...
149
150
151
152
153
154
155
156
  		/*
  		 * entry->code set to 0 invalidates module init text sections
  		 * kernel_text_address() verifies we are not in core kernel
  		 * init code, see jump_label_invalidate_module_init().
  		 */
  		if (entry->code && kernel_text_address(entry->code))
  			arch_jump_label_transform(entry, enable);
  	}
4c3ef6d79   Jason Baron   jump label: Add j...
157
  }
97ce2c88f   Jeremy Fitzhardinge   jump-label: initi...
158
  void __init jump_label_init(void)
bf5438fca   Jason Baron   jump label: Base ...
159
  {
bf5438fca   Jason Baron   jump label: Base ...
160
161
  	struct jump_entry *iter_start = __start___jump_table;
  	struct jump_entry *iter_stop = __stop___jump_table;
d430d3d7e   Jason Baron   jump label: Intro...
162
  	struct jump_label_key *key = NULL;
bf5438fca   Jason Baron   jump label: Base ...
163
  	struct jump_entry *iter;
91bad2f8d   Jason Baron   jump label: Fix d...
164
  	jump_label_lock();
d430d3d7e   Jason Baron   jump label: Intro...
165
166
167
  	jump_label_sort_entries(iter_start, iter_stop);
  
  	for (iter = iter_start; iter < iter_stop; iter++) {
37348804e   Jeremy Fitzhardinge   jump_label: if a ...
168
169
170
  		struct jump_label_key *iterk;
  
  		iterk = (struct jump_label_key *)(unsigned long)iter->key;
20284aa77   Jeremy Fitzhardinge   jump_label: add a...
171
172
  		arch_jump_label_transform_static(iter, jump_label_enabled(iterk) ?
  						 JUMP_LABEL_ENABLE : JUMP_LABEL_DISABLE);
37348804e   Jeremy Fitzhardinge   jump_label: if a ...
173
  		if (iterk == key)
d430d3d7e   Jason Baron   jump label: Intro...
174
  			continue;
37348804e   Jeremy Fitzhardinge   jump_label: if a ...
175
  		key = iterk;
d430d3d7e   Jason Baron   jump label: Intro...
176
177
178
179
  		key->entries = iter;
  #ifdef CONFIG_MODULES
  		key->next = NULL;
  #endif
bf5438fca   Jason Baron   jump label: Base ...
180
  	}
91bad2f8d   Jason Baron   jump label: Fix d...
181
  	jump_label_unlock();
bf5438fca   Jason Baron   jump label: Base ...
182
  }
bf5438fca   Jason Baron   jump label: Base ...
183
184
  
  #ifdef CONFIG_MODULES
d430d3d7e   Jason Baron   jump label: Intro...
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
  struct jump_label_mod {
  	struct jump_label_mod *next;
  	struct jump_entry *entries;
  	struct module *mod;
  };
  
  static int __jump_label_mod_text_reserved(void *start, void *end)
  {
  	struct module *mod;
  
  	mod = __module_text_address((unsigned long)start);
  	if (!mod)
  		return 0;
  
  	WARN_ON_ONCE(__module_text_address((unsigned long)end) != mod);
  
  	return __jump_label_text_reserved(mod->jump_entries,
  				mod->jump_entries + mod->num_jump_entries,
  				start, end);
  }
  
  static void __jump_label_mod_update(struct jump_label_key *key, int enable)
  {
  	struct jump_label_mod *mod = key->next;
  
  	while (mod) {
7cbc5b8d4   Jiri Olsa   jump_label: Check...
211
212
213
214
215
  		struct module *m = mod->mod;
  
  		__jump_label_update(key, mod->entries,
  				    m->jump_entries + m->num_jump_entries,
  				    enable);
d430d3d7e   Jason Baron   jump label: Intro...
216
217
218
219
220
221
222
223
224
225
226
227
228
  		mod = mod->next;
  	}
  }
  
  /***
   * apply_jump_label_nops - patch module jump labels with arch_get_jump_label_nop()
   * @mod: module to patch
   *
   * Allow for run-time selection of the optimal nops. Before the module
   * loads patch these with arch_get_jump_label_nop(), which is specified by
   * the arch specific jump label code.
   */
  void jump_label_apply_nops(struct module *mod)
bf5438fca   Jason Baron   jump label: Base ...
229
  {
d430d3d7e   Jason Baron   jump label: Intro...
230
231
232
233
234
235
236
  	struct jump_entry *iter_start = mod->jump_entries;
  	struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
  	struct jump_entry *iter;
  
  	/* if the module doesn't have jump label entries, just return */
  	if (iter_start == iter_stop)
  		return;
ac99b862f   Peter Zijlstra   jump_label: Provi...
237
238
239
240
241
242
243
  	for (iter = iter_start; iter < iter_stop; iter++) {
  		struct jump_label_key *iterk;
  
  		iterk = (struct jump_label_key *)(unsigned long)iter->key;
  		arch_jump_label_transform_static(iter, jump_label_enabled(iterk) ?
  				JUMP_LABEL_ENABLE : JUMP_LABEL_DISABLE);
  	}
bf5438fca   Jason Baron   jump label: Base ...
244
  }
d430d3d7e   Jason Baron   jump label: Intro...
245
  static int jump_label_add_module(struct module *mod)
bf5438fca   Jason Baron   jump label: Base ...
246
  {
d430d3d7e   Jason Baron   jump label: Intro...
247
248
249
250
251
  	struct jump_entry *iter_start = mod->jump_entries;
  	struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
  	struct jump_entry *iter;
  	struct jump_label_key *key = NULL;
  	struct jump_label_mod *jlm;
bf5438fca   Jason Baron   jump label: Base ...
252
253
  
  	/* if the module doesn't have jump label entries, just return */
d430d3d7e   Jason Baron   jump label: Intro...
254
  	if (iter_start == iter_stop)
bf5438fca   Jason Baron   jump label: Base ...
255
  		return 0;
d430d3d7e   Jason Baron   jump label: Intro...
256
257
258
259
260
261
262
263
264
265
266
267
268
  	jump_label_sort_entries(iter_start, iter_stop);
  
  	for (iter = iter_start; iter < iter_stop; iter++) {
  		if (iter->key == (jump_label_t)(unsigned long)key)
  			continue;
  
  		key = (struct jump_label_key *)(unsigned long)iter->key;
  
  		if (__module_address(iter->key) == mod) {
  			atomic_set(&key->enabled, 0);
  			key->entries = iter;
  			key->next = NULL;
  			continue;
bf5438fca   Jason Baron   jump label: Base ...
269
  		}
d430d3d7e   Jason Baron   jump label: Intro...
270
271
272
273
274
275
276
277
278
279
280
  
  		jlm = kzalloc(sizeof(struct jump_label_mod), GFP_KERNEL);
  		if (!jlm)
  			return -ENOMEM;
  
  		jlm->mod = mod;
  		jlm->entries = iter;
  		jlm->next = key->next;
  		key->next = jlm;
  
  		if (jump_label_enabled(key))
ac99b862f   Peter Zijlstra   jump_label: Provi...
281
  			__jump_label_update(key, iter, iter_stop, JUMP_LABEL_ENABLE);
bf5438fca   Jason Baron   jump label: Base ...
282
  	}
d430d3d7e   Jason Baron   jump label: Intro...
283

bf5438fca   Jason Baron   jump label: Base ...
284
285
  	return 0;
  }
d430d3d7e   Jason Baron   jump label: Intro...
286
  static void jump_label_del_module(struct module *mod)
bf5438fca   Jason Baron   jump label: Base ...
287
  {
d430d3d7e   Jason Baron   jump label: Intro...
288
289
290
291
292
  	struct jump_entry *iter_start = mod->jump_entries;
  	struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
  	struct jump_entry *iter;
  	struct jump_label_key *key = NULL;
  	struct jump_label_mod *jlm, **prev;
bf5438fca   Jason Baron   jump label: Base ...
293

d430d3d7e   Jason Baron   jump label: Intro...
294
295
296
297
298
299
300
301
302
303
304
  	for (iter = iter_start; iter < iter_stop; iter++) {
  		if (iter->key == (jump_label_t)(unsigned long)key)
  			continue;
  
  		key = (struct jump_label_key *)(unsigned long)iter->key;
  
  		if (__module_address(iter->key) == mod)
  			continue;
  
  		prev = &key->next;
  		jlm = key->next;
bf5438fca   Jason Baron   jump label: Base ...
305

d430d3d7e   Jason Baron   jump label: Intro...
306
307
308
309
310
311
312
313
  		while (jlm && jlm->mod != mod) {
  			prev = &jlm->next;
  			jlm = jlm->next;
  		}
  
  		if (jlm) {
  			*prev = jlm->next;
  			kfree(jlm);
bf5438fca   Jason Baron   jump label: Base ...
314
315
316
  		}
  	}
  }
d430d3d7e   Jason Baron   jump label: Intro...
317
  static void jump_label_invalidate_module_init(struct module *mod)
b842f8faf   Jason Baron   jump label: Fix m...
318
  {
d430d3d7e   Jason Baron   jump label: Intro...
319
320
  	struct jump_entry *iter_start = mod->jump_entries;
  	struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
b842f8faf   Jason Baron   jump label: Fix m...
321
  	struct jump_entry *iter;
b842f8faf   Jason Baron   jump label: Fix m...
322

d430d3d7e   Jason Baron   jump label: Intro...
323
324
325
  	for (iter = iter_start; iter < iter_stop; iter++) {
  		if (within_module_init(iter->code, mod))
  			iter->code = 0;
b842f8faf   Jason Baron   jump label: Fix m...
326
327
  	}
  }
bf5438fca   Jason Baron   jump label: Base ...
328
329
330
331
332
333
334
335
336
  static int
  jump_label_module_notify(struct notifier_block *self, unsigned long val,
  			 void *data)
  {
  	struct module *mod = data;
  	int ret = 0;
  
  	switch (val) {
  	case MODULE_STATE_COMING:
91bad2f8d   Jason Baron   jump label: Fix d...
337
  		jump_label_lock();
d430d3d7e   Jason Baron   jump label: Intro...
338
  		ret = jump_label_add_module(mod);
bf5438fca   Jason Baron   jump label: Base ...
339
  		if (ret)
d430d3d7e   Jason Baron   jump label: Intro...
340
  			jump_label_del_module(mod);
91bad2f8d   Jason Baron   jump label: Fix d...
341
  		jump_label_unlock();
bf5438fca   Jason Baron   jump label: Base ...
342
343
  		break;
  	case MODULE_STATE_GOING:
91bad2f8d   Jason Baron   jump label: Fix d...
344
  		jump_label_lock();
d430d3d7e   Jason Baron   jump label: Intro...
345
  		jump_label_del_module(mod);
91bad2f8d   Jason Baron   jump label: Fix d...
346
  		jump_label_unlock();
bf5438fca   Jason Baron   jump label: Base ...
347
  		break;
b842f8faf   Jason Baron   jump label: Fix m...
348
  	case MODULE_STATE_LIVE:
91bad2f8d   Jason Baron   jump label: Fix d...
349
  		jump_label_lock();
d430d3d7e   Jason Baron   jump label: Intro...
350
  		jump_label_invalidate_module_init(mod);
91bad2f8d   Jason Baron   jump label: Fix d...
351
  		jump_label_unlock();
b842f8faf   Jason Baron   jump label: Fix m...
352
  		break;
bf5438fca   Jason Baron   jump label: Base ...
353
  	}
bf5438fca   Jason Baron   jump label: Base ...
354

d430d3d7e   Jason Baron   jump label: Intro...
355
  	return notifier_from_errno(ret);
bf5438fca   Jason Baron   jump label: Base ...
356
357
358
359
  }
  
  struct notifier_block jump_label_module_nb = {
  	.notifier_call = jump_label_module_notify,
d430d3d7e   Jason Baron   jump label: Intro...
360
  	.priority = 1, /* higher than tracepoints */
bf5438fca   Jason Baron   jump label: Base ...
361
  };
d430d3d7e   Jason Baron   jump label: Intro...
362
  static __init int jump_label_init_module(void)
bf5438fca   Jason Baron   jump label: Base ...
363
364
365
  {
  	return register_module_notifier(&jump_label_module_nb);
  }
d430d3d7e   Jason Baron   jump label: Intro...
366
  early_initcall(jump_label_init_module);
bf5438fca   Jason Baron   jump label: Base ...
367
368
  
  #endif /* CONFIG_MODULES */
d430d3d7e   Jason Baron   jump label: Intro...
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
  /***
   * jump_label_text_reserved - check if addr range is reserved
   * @start: start text addr
   * @end: end text addr
   *
   * checks if the text addr located between @start and @end
   * overlaps with any of the jump label patch addresses. Code
   * that wants to modify kernel text should first verify that
   * it does not overlap with any of the jump label addresses.
   * Caller must hold jump_label_mutex.
   *
   * returns 1 if there is an overlap, 0 otherwise
   */
  int jump_label_text_reserved(void *start, void *end)
  {
  	int ret = __jump_label_text_reserved(__start___jump_table,
  			__stop___jump_table, start, end);
  
  	if (ret)
  		return ret;
  
  #ifdef CONFIG_MODULES
  	ret = __jump_label_mod_text_reserved(start, end);
  #endif
  	return ret;
  }
  
  static void jump_label_update(struct jump_label_key *key, int enable)
  {
140fe3b1a   Xiao Guangrong   jump_label: Fix j...
398
  	struct jump_entry *entry = key->entries, *stop = __stop___jump_table;
d430d3d7e   Jason Baron   jump label: Intro...
399
400
  
  #ifdef CONFIG_MODULES
140fe3b1a   Xiao Guangrong   jump_label: Fix j...
401
  	struct module *mod = __module_address((jump_label_t)key);
d430d3d7e   Jason Baron   jump label: Intro...
402
  	__jump_label_mod_update(key, enable);
140fe3b1a   Xiao Guangrong   jump_label: Fix j...
403
404
405
  
  	if (mod)
  		stop = mod->jump_entries + mod->num_jump_entries;
d430d3d7e   Jason Baron   jump label: Intro...
406
  #endif
140fe3b1a   Xiao Guangrong   jump_label: Fix j...
407
408
409
  	/* if there are no users, entry can be NULL */
  	if (entry)
  		__jump_label_update(key, entry, stop, enable);
d430d3d7e   Jason Baron   jump label: Intro...
410
  }
bf5438fca   Jason Baron   jump label: Base ...
411
  #endif