Skip to content

Commit a945c83

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
static_call: Allow early init
In order to use static_call() to wire up x86_pmu, we need to initialize earlier, specifically before memory allocation works; copy some of the tricks from jump_label to enable this. Primarily we overload key->next to store a sites pointer when there are no modules, this avoids having to use kmalloc() to initialize the sites and allows us to run much earlier. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Ingo Molnar <mingo@kernel.org> Reviewed-by: Steven Rostedt (VMware) <rostedt@goodmis.org> Link: https://lore.kernel.org/r/20200818135805.220737930@infradead.org
1 parent 6c3fce7 commit a945c83

File tree

4 files changed

+85
-7
lines changed

4 files changed

+85
-7
lines changed

arch/x86/kernel/setup.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/hugetlb.h>
2020
#include <linux/tboot.h>
2121
#include <linux/usb/xhci-dbgp.h>
22+
#include <linux/static_call.h>
2223

2324
#include <uapi/linux/mount.h>
2425

@@ -849,6 +850,7 @@ void __init setup_arch(char **cmdline_p)
849850
early_cpu_init();
850851
arch_init_ideal_nops();
851852
jump_label_init();
853+
static_call_init();
852854
early_ioremap_init();
853855

854856
setup_olpc_ofw_pgd();

arch/x86/kernel/static_call.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ enum insn_type {
1111
RET = 3, /* tramp / site cond-tail-call */
1212
};
1313

14-
static void __static_call_transform(void *insn, enum insn_type type, void *func)
14+
static void __ref __static_call_transform(void *insn, enum insn_type type, void *func)
1515
{
1616
int size = CALL_INSN_SIZE;
1717
const void *code;
@@ -38,6 +38,9 @@ static void __static_call_transform(void *insn, enum insn_type type, void *func)
3838
if (memcmp(insn, code, size) == 0)
3939
return;
4040

41+
if (unlikely(system_state == SYSTEM_BOOTING))
42+
return text_poke_early(insn, code, size);
43+
4144
text_poke_bp(insn, code, size, NULL);
4245
}
4346

include/linux/static_call.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ extern void arch_static_call_transform(void *site, void *tramp, void *func, bool
136136

137137
#ifdef CONFIG_HAVE_STATIC_CALL_INLINE
138138

139+
extern void __init static_call_init(void);
140+
139141
struct static_call_mod {
140142
struct static_call_mod *next;
141143
struct module *mod; /* for vmlinux, mod == NULL */
@@ -144,7 +146,12 @@ struct static_call_mod {
144146

145147
struct static_call_key {
146148
void *func;
147-
struct static_call_mod *mods;
149+
union {
150+
/* bit 0: 0 = mods, 1 = sites */
151+
unsigned long type;
152+
struct static_call_mod *mods;
153+
struct static_call_site *sites;
154+
};
148155
};
149156

150157
extern void __static_call_update(struct static_call_key *key, void *tramp, void *func);
@@ -155,7 +162,7 @@ extern int static_call_text_reserved(void *start, void *end);
155162
DECLARE_STATIC_CALL(name, _func); \
156163
struct static_call_key STATIC_CALL_KEY(name) = { \
157164
.func = _func, \
158-
.mods = NULL, \
165+
.type = 1, \
159166
}; \
160167
ARCH_DEFINE_STATIC_CALL_TRAMP(name, _func)
161168

@@ -180,6 +187,8 @@ extern int static_call_text_reserved(void *start, void *end);
180187

181188
#elif defined(CONFIG_HAVE_STATIC_CALL)
182189

190+
static inline void static_call_init(void) { }
191+
183192
struct static_call_key {
184193
void *func;
185194
};
@@ -225,6 +234,8 @@ static inline int static_call_text_reserved(void *start, void *end)
225234

226235
#else /* Generic implementation */
227236

237+
static inline void static_call_init(void) { }
238+
228239
struct static_call_key {
229240
void *func;
230241
};

kernel/static_call.c

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,31 @@ static inline void static_call_sort_entries(struct static_call_site *start,
9494
static_call_site_cmp, static_call_site_swap);
9595
}
9696

97+
static inline bool static_call_key_has_mods(struct static_call_key *key)
98+
{
99+
return !(key->type & 1);
100+
}
101+
102+
static inline struct static_call_mod *static_call_key_next(struct static_call_key *key)
103+
{
104+
if (!static_call_key_has_mods(key))
105+
return NULL;
106+
107+
return key->mods;
108+
}
109+
110+
static inline struct static_call_site *static_call_key_sites(struct static_call_key *key)
111+
{
112+
if (static_call_key_has_mods(key))
113+
return NULL;
114+
115+
return (struct static_call_site *)(key->type & ~1);
116+
}
117+
97118
void __static_call_update(struct static_call_key *key, void *tramp, void *func)
98119
{
99120
struct static_call_site *site, *stop;
100-
struct static_call_mod *site_mod;
121+
struct static_call_mod *site_mod, first;
101122

102123
cpus_read_lock();
103124
static_call_lock();
@@ -116,13 +137,22 @@ void __static_call_update(struct static_call_key *key, void *tramp, void *func)
116137
if (WARN_ON_ONCE(!static_call_initialized))
117138
goto done;
118139

119-
for (site_mod = key->mods; site_mod; site_mod = site_mod->next) {
140+
first = (struct static_call_mod){
141+
.next = static_call_key_next(key),
142+
.mod = NULL,
143+
.sites = static_call_key_sites(key),
144+
};
145+
146+
for (site_mod = &first; site_mod; site_mod = site_mod->next) {
120147
struct module *mod = site_mod->mod;
121148

122149
if (!site_mod->sites) {
123150
/*
124151
* This can happen if the static call key is defined in
125152
* a module which doesn't use it.
153+
*
154+
* It also happens in the has_mods case, where the
155+
* 'first' entry has no sites associated with it.
126156
*/
127157
continue;
128158
}
@@ -192,16 +222,48 @@ static int __static_call_init(struct module *mod,
192222
if (key != prev_key) {
193223
prev_key = key;
194224

225+
/*
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.
229+
*
230+
* This allows architectures (eg. x86) to call
231+
* static_call_init() before memory allocation works.
232+
*/
233+
if (!mod) {
234+
key->sites = site;
235+
key->type |= 1;
236+
goto do_transform;
237+
}
238+
195239
site_mod = kzalloc(sizeof(*site_mod), GFP_KERNEL);
196240
if (!site_mod)
197241
return -ENOMEM;
198242

243+
/*
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.
247+
*/
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);
252+
253+
key->mods = site_mod;
254+
255+
site_mod = kzalloc(sizeof(*site_mod), GFP_KERNEL);
256+
if (!site_mod)
257+
return -ENOMEM;
258+
}
259+
199260
site_mod->mod = mod;
200261
site_mod->sites = site;
201-
site_mod->next = key->mods;
262+
site_mod->next = static_call_key_next(key);
202263
key->mods = site_mod;
203264
}
204265

266+
do_transform:
205267
arch_static_call_transform(site_addr, NULL, key->func,
206268
static_call_is_tail(site));
207269
}
@@ -348,7 +410,7 @@ int static_call_text_reserved(void *start, void *end)
348410
return __static_call_mod_text_reserved(start, end);
349411
}
350412

351-
static void __init static_call_init(void)
413+
void __init static_call_init(void)
352414
{
353415
int ret;
354416

0 commit comments

Comments
 (0)