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
[];
16 static bool static_call_initialized
;
18 /* mutex to protect key modules/sites */
19 static DEFINE_MUTEX(static_call_mutex
);
21 static void static_call_lock(void)
23 mutex_lock(&static_call_mutex
);
26 static void static_call_unlock(void)
28 mutex_unlock(&static_call_mutex
);
31 static inline void *static_call_addr(struct static_call_site
*site
)
33 return (void *)((long)site
->addr
+ (long)&site
->addr
);
37 static inline struct static_call_key
*static_call_key(const struct static_call_site
*site
)
39 return (struct static_call_key
*)
40 (((long)site
->key
+ (long)&site
->key
) & ~STATIC_CALL_SITE_FLAGS
);
43 /* These assume the key is word-aligned. */
44 static inline bool static_call_is_init(struct static_call_site
*site
)
46 return ((long)site
->key
+ (long)&site
->key
) & STATIC_CALL_SITE_INIT
;
49 static inline bool static_call_is_tail(struct static_call_site
*site
)
51 return ((long)site
->key
+ (long)&site
->key
) & STATIC_CALL_SITE_TAIL
;
54 static inline void static_call_set_init(struct static_call_site
*site
)
56 site
->key
= ((long)static_call_key(site
) | STATIC_CALL_SITE_INIT
) -
60 static int static_call_site_cmp(const void *_a
, const void *_b
)
62 const struct static_call_site
*a
= _a
;
63 const struct static_call_site
*b
= _b
;
64 const struct static_call_key
*key_a
= static_call_key(a
);
65 const struct static_call_key
*key_b
= static_call_key(b
);
76 static void static_call_site_swap(void *_a
, void *_b
, int size
)
78 long delta
= (unsigned long)_a
- (unsigned long)_b
;
79 struct static_call_site
*a
= _a
;
80 struct static_call_site
*b
= _b
;
81 struct static_call_site tmp
= *a
;
83 a
->addr
= b
->addr
- delta
;
84 a
->key
= b
->key
- delta
;
86 b
->addr
= tmp
.addr
+ delta
;
87 b
->key
= tmp
.key
+ delta
;
90 static inline void static_call_sort_entries(struct static_call_site
*start
,
91 struct static_call_site
*stop
)
93 sort(start
, stop
- start
, sizeof(struct static_call_site
),
94 static_call_site_cmp
, static_call_site_swap
);
97 static inline bool static_call_key_has_mods(struct static_call_key
*key
)
99 return !(key
->type
& 1);
102 static inline struct static_call_mod
*static_call_key_next(struct static_call_key
*key
)
104 if (!static_call_key_has_mods(key
))
110 static inline struct static_call_site
*static_call_key_sites(struct static_call_key
*key
)
112 if (static_call_key_has_mods(key
))
115 return (struct static_call_site
*)(key
->type
& ~1);
118 void __static_call_update(struct static_call_key
*key
, void *tramp
, void *func
)
120 struct static_call_site
*site
, *stop
;
121 struct static_call_mod
*site_mod
, first
;
126 if (key
->func
== func
)
131 arch_static_call_transform(NULL
, tramp
, func
, false);
134 * If uninitialized, we'll not update the callsites, but they still
135 * point to the trampoline and we just patched that.
137 if (WARN_ON_ONCE(!static_call_initialized
))
140 first
= (struct static_call_mod
){
141 .next
= static_call_key_next(key
),
143 .sites
= static_call_key_sites(key
),
146 for (site_mod
= &first
; site_mod
; site_mod
= site_mod
->next
) {
147 struct module
*mod
= site_mod
->mod
;
149 if (!site_mod
->sites
) {
151 * This can happen if the static call key is defined in
152 * a module which doesn't use it.
154 * It also happens in the has_mods case, where the
155 * 'first' entry has no sites associated with it.
160 stop
= __stop_static_call_sites
;
162 #ifdef CONFIG_MODULES
164 stop
= mod
->static_call_sites
+
165 mod
->num_static_call_sites
;
169 for (site
= site_mod
->sites
;
170 site
< stop
&& static_call_key(site
) == key
; site
++) {
171 void *site_addr
= static_call_addr(site
);
173 if (static_call_is_init(site
)) {
175 * Don't write to call sites which were in
176 * initmem and have since been freed.
178 if (!mod
&& system_state
>= SYSTEM_RUNNING
)
180 if (mod
&& !within_module_init((unsigned long)site_addr
, mod
))
184 if (!kernel_text_address((unsigned long)site_addr
)) {
185 WARN_ONCE(1, "can't patch static call site at %pS",
190 arch_static_call_transform(site_addr
, NULL
, func
,
191 static_call_is_tail(site
));
196 static_call_unlock();
199 EXPORT_SYMBOL_GPL(__static_call_update
);
201 static int __static_call_init(struct module
*mod
,
202 struct static_call_site
*start
,
203 struct static_call_site
*stop
)
205 struct static_call_site
*site
;
206 struct static_call_key
*key
, *prev_key
= NULL
;
207 struct static_call_mod
*site_mod
;
212 static_call_sort_entries(start
, stop
);
214 for (site
= start
; site
< stop
; site
++) {
215 void *site_addr
= static_call_addr(site
);
217 if ((mod
&& within_module_init((unsigned long)site_addr
, mod
)) ||
218 (!mod
&& init_section_contains(site_addr
, 1)))
219 static_call_set_init(site
);
221 key
= static_call_key(site
);
222 if (key
!= prev_key
) {
226 * For vmlinux (!mod) avoid the allocation by storing
227 * the sites pointer in the key itself. Also see
228 * __static_call_update()'s @first.
230 * This allows architectures (eg. x86) to call
231 * static_call_init() before memory allocation works.
239 site_mod
= kzalloc(sizeof(*site_mod
), GFP_KERNEL
);
244 * When the key has a direct sites pointer, extract
245 * that into an explicit struct static_call_mod, so we
246 * can have a list of modules.
248 if (static_call_key_sites(key
)) {
249 site_mod
->mod
= NULL
;
250 site_mod
->next
= NULL
;
251 site_mod
->sites
= static_call_key_sites(key
);
253 key
->mods
= site_mod
;
255 site_mod
= kzalloc(sizeof(*site_mod
), GFP_KERNEL
);
261 site_mod
->sites
= site
;
262 site_mod
->next
= static_call_key_next(key
);
263 key
->mods
= site_mod
;
267 arch_static_call_transform(site_addr
, NULL
, key
->func
,
268 static_call_is_tail(site
));
274 static int addr_conflict(struct static_call_site
*site
, void *start
, void *end
)
276 unsigned long addr
= (unsigned long)static_call_addr(site
);
278 if (addr
<= (unsigned long)end
&&
279 addr
+ CALL_INSN_SIZE
> (unsigned long)start
)
285 static int __static_call_text_reserved(struct static_call_site
*iter_start
,
286 struct static_call_site
*iter_stop
,
287 void *start
, void *end
)
289 struct static_call_site
*iter
= iter_start
;
291 while (iter
< iter_stop
) {
292 if (addr_conflict(iter
, start
, end
))
300 #ifdef CONFIG_MODULES
302 static int __static_call_mod_text_reserved(void *start
, void *end
)
308 mod
= __module_text_address((unsigned long)start
);
309 WARN_ON_ONCE(__module_text_address((unsigned long)end
) != mod
);
310 if (!try_module_get(mod
))
317 ret
= __static_call_text_reserved(mod
->static_call_sites
,
318 mod
->static_call_sites
+ mod
->num_static_call_sites
,
326 static int static_call_add_module(struct module
*mod
)
328 return __static_call_init(mod
, mod
->static_call_sites
,
329 mod
->static_call_sites
+ mod
->num_static_call_sites
);
332 static void static_call_del_module(struct module
*mod
)
334 struct static_call_site
*start
= mod
->static_call_sites
;
335 struct static_call_site
*stop
= mod
->static_call_sites
+
336 mod
->num_static_call_sites
;
337 struct static_call_key
*key
, *prev_key
= NULL
;
338 struct static_call_mod
*site_mod
, **prev
;
339 struct static_call_site
*site
;
341 for (site
= start
; site
< stop
; site
++) {
342 key
= static_call_key(site
);
348 for (prev
= &key
->mods
, site_mod
= key
->mods
;
349 site_mod
&& site_mod
->mod
!= mod
;
350 prev
= &site_mod
->next
, site_mod
= site_mod
->next
)
356 *prev
= site_mod
->next
;
361 static int static_call_module_notify(struct notifier_block
*nb
,
362 unsigned long val
, void *data
)
364 struct module
*mod
= data
;
371 case MODULE_STATE_COMING
:
372 ret
= static_call_add_module(mod
);
374 WARN(1, "Failed to allocate memory for static calls");
375 static_call_del_module(mod
);
378 case MODULE_STATE_GOING
:
379 static_call_del_module(mod
);
383 static_call_unlock();
386 return notifier_from_errno(ret
);
389 static struct notifier_block static_call_module_nb
= {
390 .notifier_call
= static_call_module_notify
,
395 static inline int __static_call_mod_text_reserved(void *start
, void *end
)
400 #endif /* CONFIG_MODULES */
402 int static_call_text_reserved(void *start
, void *end
)
404 int ret
= __static_call_text_reserved(__start_static_call_sites
,
405 __stop_static_call_sites
, start
, end
);
410 return __static_call_mod_text_reserved(start
, end
);
413 int __init
static_call_init(void)
417 if (static_call_initialized
)
422 ret
= __static_call_init(NULL
, __start_static_call_sites
,
423 __stop_static_call_sites
);
424 static_call_unlock();
428 pr_err("Failed to allocate memory for static_call!\n");
432 static_call_initialized
= true;
434 #ifdef CONFIG_MODULES
435 register_module_notifier(&static_call_module_nb
);
439 early_initcall(static_call_init
);
441 #ifdef CONFIG_STATIC_CALL_SELFTEST
443 static int func_a(int x
)
448 static int func_b(int x
)
453 DEFINE_STATIC_CALL(sc_selftest
, func_a
);
455 static struct static_call_data
{
459 } static_call_data
[] __initdata
= {
465 static int __init
test_static_call_init(void)
469 for (i
= 0; i
< ARRAY_SIZE(static_call_data
); i
++ ) {
470 struct static_call_data
*scd
= &static_call_data
[i
];
473 static_call_update(sc_selftest
, scd
->func
);
475 WARN_ON(static_call(sc_selftest
)(scd
->val
) != scd
->expect
);
480 early_initcall(test_static_call_init
);
482 #endif /* CONFIG_STATIC_CALL_SELFTEST */