Blame view

kernel/static_call.c 10.6 KB
9183c3f9e   Josh Poimboeuf   static_call: Add ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  // SPDX-License-Identifier: GPL-2.0
  #include <linux/init.h>
  #include <linux/static_call.h>
  #include <linux/bug.h>
  #include <linux/smp.h>
  #include <linux/sort.h>
  #include <linux/slab.h>
  #include <linux/module.h>
  #include <linux/cpu.h>
  #include <linux/processor.h>
  #include <asm/sections.h>
  
  extern struct static_call_site __start_static_call_sites[],
  			       __stop_static_call_sites[];
  
  static bool static_call_initialized;
9183c3f9e   Josh Poimboeuf   static_call: Add ...
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  /* mutex to protect key modules/sites */
  static DEFINE_MUTEX(static_call_mutex);
  
  static void static_call_lock(void)
  {
  	mutex_lock(&static_call_mutex);
  }
  
  static void static_call_unlock(void)
  {
  	mutex_unlock(&static_call_mutex);
  }
  
  static inline void *static_call_addr(struct static_call_site *site)
  {
  	return (void *)((long)site->addr + (long)&site->addr);
  }
  
  
  static inline struct static_call_key *static_call_key(const struct static_call_site *site)
  {
  	return (struct static_call_key *)
5b06fd3bb   Peter Zijlstra   static_call: Hand...
39
  		(((long)site->key + (long)&site->key) & ~STATIC_CALL_SITE_FLAGS);
9183c3f9e   Josh Poimboeuf   static_call: Add ...
40
41
42
43
44
  }
  
  /* These assume the key is word-aligned. */
  static inline bool static_call_is_init(struct static_call_site *site)
  {
5b06fd3bb   Peter Zijlstra   static_call: Hand...
45
46
47
48
49
50
  	return ((long)site->key + (long)&site->key) & STATIC_CALL_SITE_INIT;
  }
  
  static inline bool static_call_is_tail(struct static_call_site *site)
  {
  	return ((long)site->key + (long)&site->key) & STATIC_CALL_SITE_TAIL;
9183c3f9e   Josh Poimboeuf   static_call: Add ...
51
52
53
54
  }
  
  static inline void static_call_set_init(struct static_call_site *site)
  {
5b06fd3bb   Peter Zijlstra   static_call: Hand...
55
  	site->key = ((long)static_call_key(site) | STATIC_CALL_SITE_INIT) -
9183c3f9e   Josh Poimboeuf   static_call: Add ...
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  		    (long)&site->key;
  }
  
  static int static_call_site_cmp(const void *_a, const void *_b)
  {
  	const struct static_call_site *a = _a;
  	const struct static_call_site *b = _b;
  	const struct static_call_key *key_a = static_call_key(a);
  	const struct static_call_key *key_b = static_call_key(b);
  
  	if (key_a < key_b)
  		return -1;
  
  	if (key_a > key_b)
  		return 1;
  
  	return 0;
  }
  
  static void static_call_site_swap(void *_a, void *_b, int size)
  {
  	long delta = (unsigned long)_a - (unsigned long)_b;
  	struct static_call_site *a = _a;
  	struct static_call_site *b = _b;
  	struct static_call_site tmp = *a;
  
  	a->addr = b->addr  - delta;
  	a->key  = b->key   - delta;
  
  	b->addr = tmp.addr + delta;
  	b->key  = tmp.key  + delta;
  }
  
  static inline void static_call_sort_entries(struct static_call_site *start,
  					    struct static_call_site *stop)
  {
  	sort(start, stop - start, sizeof(struct static_call_site),
  	     static_call_site_cmp, static_call_site_swap);
  }
a945c8345   Peter Zijlstra   static_call: Allo...
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  static inline bool static_call_key_has_mods(struct static_call_key *key)
  {
  	return !(key->type & 1);
  }
  
  static inline struct static_call_mod *static_call_key_next(struct static_call_key *key)
  {
  	if (!static_call_key_has_mods(key))
  		return NULL;
  
  	return key->mods;
  }
  
  static inline struct static_call_site *static_call_key_sites(struct static_call_key *key)
  {
  	if (static_call_key_has_mods(key))
  		return NULL;
  
  	return (struct static_call_site *)(key->type & ~1);
  }
9183c3f9e   Josh Poimboeuf   static_call: Add ...
115
116
117
  void __static_call_update(struct static_call_key *key, void *tramp, void *func)
  {
  	struct static_call_site *site, *stop;
a945c8345   Peter Zijlstra   static_call: Allo...
118
  	struct static_call_mod *site_mod, first;
9183c3f9e   Josh Poimboeuf   static_call: Add ...
119
120
121
122
123
124
125
126
  
  	cpus_read_lock();
  	static_call_lock();
  
  	if (key->func == func)
  		goto done;
  
  	key->func = func;
5b06fd3bb   Peter Zijlstra   static_call: Hand...
127
  	arch_static_call_transform(NULL, tramp, func, false);
9183c3f9e   Josh Poimboeuf   static_call: Add ...
128
129
130
131
132
133
134
  
  	/*
  	 * If uninitialized, we'll not update the callsites, but they still
  	 * point to the trampoline and we just patched that.
  	 */
  	if (WARN_ON_ONCE(!static_call_initialized))
  		goto done;
a945c8345   Peter Zijlstra   static_call: Allo...
135
136
137
138
139
140
141
  	first = (struct static_call_mod){
  		.next = static_call_key_next(key),
  		.mod = NULL,
  		.sites = static_call_key_sites(key),
  	};
  
  	for (site_mod = &first; site_mod; site_mod = site_mod->next) {
9183c3f9e   Josh Poimboeuf   static_call: Add ...
142
143
144
145
146
147
  		struct module *mod = site_mod->mod;
  
  		if (!site_mod->sites) {
  			/*
  			 * This can happen if the static call key is defined in
  			 * a module which doesn't use it.
a945c8345   Peter Zijlstra   static_call: Allo...
148
149
150
  			 *
  			 * It also happens in the has_mods case, where the
  			 * 'first' entry has no sites associated with it.
9183c3f9e   Josh Poimboeuf   static_call: Add ...
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
  			 */
  			continue;
  		}
  
  		stop = __stop_static_call_sites;
  
  #ifdef CONFIG_MODULES
  		if (mod) {
  			stop = mod->static_call_sites +
  			       mod->num_static_call_sites;
  		}
  #endif
  
  		for (site = site_mod->sites;
  		     site < stop && static_call_key(site) == key; site++) {
  			void *site_addr = static_call_addr(site);
  
  			if (static_call_is_init(site)) {
  				/*
  				 * Don't write to call sites which were in
  				 * initmem and have since been freed.
  				 */
  				if (!mod && system_state >= SYSTEM_RUNNING)
  					continue;
  				if (mod && !within_module_init((unsigned long)site_addr, mod))
  					continue;
  			}
  
  			if (!kernel_text_address((unsigned long)site_addr)) {
  				WARN_ONCE(1, "can't patch static call site at %pS",
  					  site_addr);
  				continue;
  			}
5b06fd3bb   Peter Zijlstra   static_call: Hand...
184
185
  			arch_static_call_transform(site_addr, NULL, func,
  				static_call_is_tail(site));
9183c3f9e   Josh Poimboeuf   static_call: Add ...
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
211
212
213
214
215
216
217
  		}
  	}
  
  done:
  	static_call_unlock();
  	cpus_read_unlock();
  }
  EXPORT_SYMBOL_GPL(__static_call_update);
  
  static int __static_call_init(struct module *mod,
  			      struct static_call_site *start,
  			      struct static_call_site *stop)
  {
  	struct static_call_site *site;
  	struct static_call_key *key, *prev_key = NULL;
  	struct static_call_mod *site_mod;
  
  	if (start == stop)
  		return 0;
  
  	static_call_sort_entries(start, stop);
  
  	for (site = start; site < stop; site++) {
  		void *site_addr = static_call_addr(site);
  
  		if ((mod && within_module_init((unsigned long)site_addr, mod)) ||
  		    (!mod && init_section_contains(site_addr, 1)))
  			static_call_set_init(site);
  
  		key = static_call_key(site);
  		if (key != prev_key) {
  			prev_key = key;
a945c8345   Peter Zijlstra   static_call: Allo...
218
219
220
221
222
223
224
225
226
227
228
229
230
  			/*
  			 * For vmlinux (!mod) avoid the allocation by storing
  			 * the sites pointer in the key itself. Also see
  			 * __static_call_update()'s @first.
  			 *
  			 * This allows architectures (eg. x86) to call
  			 * static_call_init() before memory allocation works.
  			 */
  			if (!mod) {
  				key->sites = site;
  				key->type |= 1;
  				goto do_transform;
  			}
9183c3f9e   Josh Poimboeuf   static_call: Add ...
231
232
233
  			site_mod = kzalloc(sizeof(*site_mod), GFP_KERNEL);
  			if (!site_mod)
  				return -ENOMEM;
a945c8345   Peter Zijlstra   static_call: Allo...
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
  			/*
  			 * When the key has a direct sites pointer, extract
  			 * that into an explicit struct static_call_mod, so we
  			 * can have a list of modules.
  			 */
  			if (static_call_key_sites(key)) {
  				site_mod->mod = NULL;
  				site_mod->next = NULL;
  				site_mod->sites = static_call_key_sites(key);
  
  				key->mods = site_mod;
  
  				site_mod = kzalloc(sizeof(*site_mod), GFP_KERNEL);
  				if (!site_mod)
  					return -ENOMEM;
  			}
9183c3f9e   Josh Poimboeuf   static_call: Add ...
250
251
  			site_mod->mod = mod;
  			site_mod->sites = site;
a945c8345   Peter Zijlstra   static_call: Allo...
252
  			site_mod->next = static_call_key_next(key);
9183c3f9e   Josh Poimboeuf   static_call: Add ...
253
254
  			key->mods = site_mod;
  		}
a945c8345   Peter Zijlstra   static_call: Allo...
255
  do_transform:
5b06fd3bb   Peter Zijlstra   static_call: Hand...
256
257
  		arch_static_call_transform(site_addr, NULL, key->func,
  				static_call_is_tail(site));
9183c3f9e   Josh Poimboeuf   static_call: Add ...
258
259
260
261
  	}
  
  	return 0;
  }
6333e8f73   Peter Zijlstra   static_call: Avoi...
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
  static int addr_conflict(struct static_call_site *site, void *start, void *end)
  {
  	unsigned long addr = (unsigned long)static_call_addr(site);
  
  	if (addr <= (unsigned long)end &&
  	    addr + CALL_INSN_SIZE > (unsigned long)start)
  		return 1;
  
  	return 0;
  }
  
  static int __static_call_text_reserved(struct static_call_site *iter_start,
  				       struct static_call_site *iter_stop,
  				       void *start, void *end)
  {
  	struct static_call_site *iter = iter_start;
  
  	while (iter < iter_stop) {
  		if (addr_conflict(iter, start, end))
  			return 1;
  		iter++;
  	}
  
  	return 0;
  }
9183c3f9e   Josh Poimboeuf   static_call: Add ...
287
  #ifdef CONFIG_MODULES
6333e8f73   Peter Zijlstra   static_call: Avoi...
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
  static int __static_call_mod_text_reserved(void *start, void *end)
  {
  	struct module *mod;
  	int ret;
  
  	preempt_disable();
  	mod = __module_text_address((unsigned long)start);
  	WARN_ON_ONCE(__module_text_address((unsigned long)end) != mod);
  	if (!try_module_get(mod))
  		mod = NULL;
  	preempt_enable();
  
  	if (!mod)
  		return 0;
  
  	ret = __static_call_text_reserved(mod->static_call_sites,
  			mod->static_call_sites + mod->num_static_call_sites,
  			start, end);
  
  	module_put(mod);
  
  	return ret;
  }
9183c3f9e   Josh Poimboeuf   static_call: Add ...
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
  static int static_call_add_module(struct module *mod)
  {
  	return __static_call_init(mod, mod->static_call_sites,
  				  mod->static_call_sites + mod->num_static_call_sites);
  }
  
  static void static_call_del_module(struct module *mod)
  {
  	struct static_call_site *start = mod->static_call_sites;
  	struct static_call_site *stop = mod->static_call_sites +
  					mod->num_static_call_sites;
  	struct static_call_key *key, *prev_key = NULL;
  	struct static_call_mod *site_mod, **prev;
  	struct static_call_site *site;
  
  	for (site = start; site < stop; site++) {
  		key = static_call_key(site);
  		if (key == prev_key)
  			continue;
  
  		prev_key = key;
  
  		for (prev = &key->mods, site_mod = key->mods;
  		     site_mod && site_mod->mod != mod;
  		     prev = &site_mod->next, site_mod = site_mod->next)
  			;
  
  		if (!site_mod)
  			continue;
  
  		*prev = site_mod->next;
  		kfree(site_mod);
  	}
  }
  
  static int static_call_module_notify(struct notifier_block *nb,
  				     unsigned long val, void *data)
  {
  	struct module *mod = data;
  	int ret = 0;
  
  	cpus_read_lock();
  	static_call_lock();
  
  	switch (val) {
  	case MODULE_STATE_COMING:
  		ret = static_call_add_module(mod);
  		if (ret) {
  			WARN(1, "Failed to allocate memory for static calls");
  			static_call_del_module(mod);
  		}
  		break;
  	case MODULE_STATE_GOING:
  		static_call_del_module(mod);
  		break;
  	}
  
  	static_call_unlock();
  	cpus_read_unlock();
  
  	return notifier_from_errno(ret);
  }
  
  static struct notifier_block static_call_module_nb = {
  	.notifier_call = static_call_module_notify,
  };
6333e8f73   Peter Zijlstra   static_call: Avoi...
377
378
379
380
381
382
  #else
  
  static inline int __static_call_mod_text_reserved(void *start, void *end)
  {
  	return 0;
  }
9183c3f9e   Josh Poimboeuf   static_call: Add ...
383
  #endif /* CONFIG_MODULES */
6333e8f73   Peter Zijlstra   static_call: Avoi...
384
385
386
387
388
389
390
391
392
393
  int static_call_text_reserved(void *start, void *end)
  {
  	int ret = __static_call_text_reserved(__start_static_call_sites,
  			__stop_static_call_sites, start, end);
  
  	if (ret)
  		return ret;
  
  	return __static_call_mod_text_reserved(start, end);
  }
69e0ad37c   Nathan Chancellor   static_call: Fix ...
394
  int __init static_call_init(void)
9183c3f9e   Josh Poimboeuf   static_call: Add ...
395
396
397
398
  {
  	int ret;
  
  	if (static_call_initialized)
69e0ad37c   Nathan Chancellor   static_call: Fix ...
399
  		return 0;
9183c3f9e   Josh Poimboeuf   static_call: Add ...
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
  
  	cpus_read_lock();
  	static_call_lock();
  	ret = __static_call_init(NULL, __start_static_call_sites,
  				 __stop_static_call_sites);
  	static_call_unlock();
  	cpus_read_unlock();
  
  	if (ret) {
  		pr_err("Failed to allocate memory for static_call!
  ");
  		BUG();
  	}
  
  	static_call_initialized = true;
  
  #ifdef CONFIG_MODULES
  	register_module_notifier(&static_call_module_nb);
  #endif
69e0ad37c   Nathan Chancellor   static_call: Fix ...
419
  	return 0;
9183c3f9e   Josh Poimboeuf   static_call: Add ...
420
421
  }
  early_initcall(static_call_init);
f03c41291   Peter Zijlstra   static_call: Add ...
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
  
  #ifdef CONFIG_STATIC_CALL_SELFTEST
  
  static int func_a(int x)
  {
  	return x+1;
  }
  
  static int func_b(int x)
  {
  	return x+2;
  }
  
  DEFINE_STATIC_CALL(sc_selftest, func_a);
  
  static struct static_call_data {
        int (*func)(int);
        int val;
        int expect;
  } static_call_data [] __initdata = {
        { NULL,   2, 3 },
        { func_b, 2, 4 },
        { func_a, 2, 3 }
  };
  
  static int __init test_static_call_init(void)
  {
        int i;
  
        for (i = 0; i < ARRAY_SIZE(static_call_data); i++ ) {
  	      struct static_call_data *scd = &static_call_data[i];
  
                if (scd->func)
                        static_call_update(sc_selftest, scd->func);
  
                WARN_ON(static_call(sc_selftest)(scd->val) != scd->expect);
        }
  
        return 0;
  }
  early_initcall(test_static_call_init);
  
  #endif /* CONFIG_STATIC_CALL_SELFTEST */