1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/init.h>
3 #include <linux/static_call.h>
6 #include <linux/sort.h>
7 #include <linux/slab.h>
8 #include <linux/module.h>
10 #include <linux/processor.h>
11 #include <asm/sections.h>
13 extern struct static_call_site __start_static_call_sites[],
14 __stop_static_call_sites[];
15 extern struct static_call_tramp_key __start_static_call_tramp_key[],
16 __stop_static_call_tramp_key[];
18 static bool static_call_initialized;
20 /* mutex to protect key modules/sites */
21 static DEFINE_MUTEX(static_call_mutex);
23 static void static_call_lock(void)
25 mutex_lock(&static_call_mutex);
28 static void static_call_unlock(void)
30 mutex_unlock(&static_call_mutex);
33 static inline void *static_call_addr(struct static_call_site *site)
35 return (void *)((long)site->addr + (long)&site->addr);
39 static inline struct static_call_key *static_call_key(const struct static_call_site *site)
41 return (struct static_call_key *)
42 (((long)site->key + (long)&site->key) & ~STATIC_CALL_SITE_FLAGS);
45 /* These assume the key is word-aligned. */
46 static inline bool static_call_is_init(struct static_call_site *site)
48 return ((long)site->key + (long)&site->key) & STATIC_CALL_SITE_INIT;
51 static inline bool static_call_is_tail(struct static_call_site *site)
53 return ((long)site->key + (long)&site->key) & STATIC_CALL_SITE_TAIL;
56 static inline void static_call_set_init(struct static_call_site *site)
58 site->key = ((long)static_call_key(site) | STATIC_CALL_SITE_INIT) -
62 static int static_call_site_cmp(const void *_a, const void *_b)
64 const struct static_call_site *a = _a;
65 const struct static_call_site *b = _b;
66 const struct static_call_key *key_a = static_call_key(a);
67 const struct static_call_key *key_b = static_call_key(b);
78 static void static_call_site_swap(void *_a, void *_b, int size)
80 long delta = (unsigned long)_a - (unsigned long)_b;
81 struct static_call_site *a = _a;
82 struct static_call_site *b = _b;
83 struct static_call_site tmp = *a;
85 a->addr = b->addr - delta;
86 a->key = b->key - delta;
88 b->addr = tmp.addr + delta;
89 b->key = tmp.key + delta;
92 static inline void static_call_sort_entries(struct static_call_site *start,
93 struct static_call_site *stop)
95 sort(start, stop - start, sizeof(struct static_call_site),
96 static_call_site_cmp, static_call_site_swap);
99 static inline bool static_call_key_has_mods(struct static_call_key *key)
101 return !(key->type & 1);
104 static inline struct static_call_mod *static_call_key_next(struct static_call_key *key)
106 if (!static_call_key_has_mods(key))
112 static inline struct static_call_site *static_call_key_sites(struct static_call_key *key)
114 if (static_call_key_has_mods(key))
117 return (struct static_call_site *)(key->type & ~1);
120 void __static_call_update(struct static_call_key *key, void *tramp, void *func)
122 struct static_call_site *site, *stop;
123 struct static_call_mod *site_mod, first;
128 if (key->func == func)
133 arch_static_call_transform(NULL, tramp, func, false);
136 * If uninitialized, we'll not update the callsites, but they still
137 * point to the trampoline and we just patched that.
139 if (WARN_ON_ONCE(!static_call_initialized))
142 first = (struct static_call_mod){
143 .next = static_call_key_next(key),
145 .sites = static_call_key_sites(key),
148 for (site_mod = &first; site_mod; site_mod = site_mod->next) {
149 struct module *mod = site_mod->mod;
151 if (!site_mod->sites) {
153 * This can happen if the static call key is defined in
154 * a module which doesn't use it.
156 * It also happens in the has_mods case, where the
157 * 'first' entry has no sites associated with it.
162 stop = __stop_static_call_sites;
164 #ifdef CONFIG_MODULES
166 stop = mod->static_call_sites +
167 mod->num_static_call_sites;
171 for (site = site_mod->sites;
172 site < stop && static_call_key(site) == key; site++) {
173 void *site_addr = static_call_addr(site);
175 if (static_call_is_init(site)) {
177 * Don't write to call sites which were in
178 * initmem and have since been freed.
180 if (!mod && system_state >= SYSTEM_RUNNING)
182 if (mod && !within_module_init((unsigned long)site_addr, mod))
186 if (!kernel_text_address((unsigned long)site_addr)) {
187 WARN_ONCE(1, "can't patch static call site at %pS",
192 arch_static_call_transform(site_addr, NULL, func,
193 static_call_is_tail(site));
198 static_call_unlock();
201 EXPORT_SYMBOL_GPL(__static_call_update);
203 static int __static_call_init(struct module *mod,
204 struct static_call_site *start,
205 struct static_call_site *stop)
207 struct static_call_site *site;
208 struct static_call_key *key, *prev_key = NULL;
209 struct static_call_mod *site_mod;
214 static_call_sort_entries(start, stop);
216 for (site = start; site < stop; site++) {
217 void *site_addr = static_call_addr(site);
219 if ((mod && within_module_init((unsigned long)site_addr, mod)) ||
220 (!mod && init_section_contains(site_addr, 1)))
221 static_call_set_init(site);
223 key = static_call_key(site);
224 if (key != prev_key) {
228 * For vmlinux (!mod) avoid the allocation by storing
229 * the sites pointer in the key itself. Also see
230 * __static_call_update()'s @first.
232 * This allows architectures (eg. x86) to call
233 * static_call_init() before memory allocation works.
241 site_mod = kzalloc(sizeof(*site_mod), GFP_KERNEL);
246 * When the key has a direct sites pointer, extract
247 * that into an explicit struct static_call_mod, so we
248 * can have a list of modules.
250 if (static_call_key_sites(key)) {
251 site_mod->mod = NULL;
252 site_mod->next = NULL;
253 site_mod->sites = static_call_key_sites(key);
255 key->mods = site_mod;
257 site_mod = kzalloc(sizeof(*site_mod), GFP_KERNEL);
263 site_mod->sites = site;
264 site_mod->next = static_call_key_next(key);
265 key->mods = site_mod;
269 arch_static_call_transform(site_addr, NULL, key->func,
270 static_call_is_tail(site));
276 static int addr_conflict(struct static_call_site *site, void *start, void *end)
278 unsigned long addr = (unsigned long)static_call_addr(site);
280 if (addr <= (unsigned long)end &&
281 addr + CALL_INSN_SIZE > (unsigned long)start)
287 static int __static_call_text_reserved(struct static_call_site *iter_start,
288 struct static_call_site *iter_stop,
289 void *start, void *end)
291 struct static_call_site *iter = iter_start;
293 while (iter < iter_stop) {
294 if (addr_conflict(iter, start, end))
302 #ifdef CONFIG_MODULES
304 static int __static_call_mod_text_reserved(void *start, void *end)
310 mod = __module_text_address((unsigned long)start);
311 WARN_ON_ONCE(__module_text_address((unsigned long)end) != mod);
312 if (!try_module_get(mod))
319 ret = __static_call_text_reserved(mod->static_call_sites,
320 mod->static_call_sites + mod->num_static_call_sites,
328 static unsigned long tramp_key_lookup(unsigned long addr)
330 struct static_call_tramp_key *start = __start_static_call_tramp_key;
331 struct static_call_tramp_key *stop = __stop_static_call_tramp_key;
332 struct static_call_tramp_key *tramp_key;
334 for (tramp_key = start; tramp_key != stop; tramp_key++) {
337 tramp = (long)tramp_key->tramp + (long)&tramp_key->tramp;
339 return (long)tramp_key->key + (long)&tramp_key->key;
345 static int static_call_add_module(struct module *mod)
347 struct static_call_site *start = mod->static_call_sites;
348 struct static_call_site *stop = start + mod->num_static_call_sites;
349 struct static_call_site *site;
351 for (site = start; site != stop; site++) {
352 unsigned long addr = (unsigned long)static_call_key(site);
356 * Is the key is exported, 'addr' points to the key, which
357 * means modules are allowed to call static_call_update() on
360 * Otherwise, the key isn't exported, and 'addr' points to the
361 * trampoline so we need to lookup the key.
363 * We go through this dance to prevent crazy modules from
364 * abusing sensitive static calls.
366 if (!kernel_text_address(addr))
369 key = tramp_key_lookup(addr);
371 pr_warn("Failed to fixup __raw_static_call() usage at: %ps\n",
372 static_call_addr(site));
376 site->key = (key - (long)&site->key) |
377 (site->key & STATIC_CALL_SITE_FLAGS);
380 return __static_call_init(mod, start, stop);
383 static void static_call_del_module(struct module *mod)
385 struct static_call_site *start = mod->static_call_sites;
386 struct static_call_site *stop = mod->static_call_sites +
387 mod->num_static_call_sites;
388 struct static_call_key *key, *prev_key = NULL;
389 struct static_call_mod *site_mod, **prev;
390 struct static_call_site *site;
392 for (site = start; site < stop; site++) {
393 key = static_call_key(site);
399 for (prev = &key->mods, site_mod = key->mods;
400 site_mod && site_mod->mod != mod;
401 prev = &site_mod->next, site_mod = site_mod->next)
407 *prev = site_mod->next;
412 static int static_call_module_notify(struct notifier_block *nb,
413 unsigned long val, void *data)
415 struct module *mod = data;
422 case MODULE_STATE_COMING:
423 ret = static_call_add_module(mod);
425 WARN(1, "Failed to allocate memory for static calls");
426 static_call_del_module(mod);
429 case MODULE_STATE_GOING:
430 static_call_del_module(mod);
434 static_call_unlock();
437 return notifier_from_errno(ret);
440 static struct notifier_block static_call_module_nb = {
441 .notifier_call = static_call_module_notify,
446 static inline int __static_call_mod_text_reserved(void *start, void *end)
451 #endif /* CONFIG_MODULES */
453 int static_call_text_reserved(void *start, void *end)
455 int ret = __static_call_text_reserved(__start_static_call_sites,
456 __stop_static_call_sites, start, end);
461 return __static_call_mod_text_reserved(start, end);
464 int __init static_call_init(void)
468 if (static_call_initialized)
473 ret = __static_call_init(NULL, __start_static_call_sites,
474 __stop_static_call_sites);
475 static_call_unlock();
479 pr_err("Failed to allocate memory for static_call!\n");
483 static_call_initialized = true;
485 #ifdef CONFIG_MODULES
486 register_module_notifier(&static_call_module_nb);
490 early_initcall(static_call_init);
492 long __static_call_return0(void)
497 #ifdef CONFIG_STATIC_CALL_SELFTEST
499 static int func_a(int x)
504 static int func_b(int x)
509 DEFINE_STATIC_CALL(sc_selftest, func_a);
511 static struct static_call_data {
515 } static_call_data [] __initdata = {
521 static int __init test_static_call_init(void)
525 for (i = 0; i < ARRAY_SIZE(static_call_data); i++ ) {
526 struct static_call_data *scd = &static_call_data[i];
529 static_call_update(sc_selftest, scd->func);
531 WARN_ON(static_call(sc_selftest)(scd->val) != scd->expect);
536 early_initcall(test_static_call_init);
538 #endif /* CONFIG_STATIC_CALL_SELFTEST */