Skip to content

Commit f03c412

Browse files
Peter Zijlstraingomolnar
authored andcommitted
static_call: Add simple self-test for static calls
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Ingo Molnar <mingo@kernel.org> Link: https://lore.kernel.org/r/20200818135804.922581202@infradead.org
1 parent 1e7e478 commit f03c412

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

arch/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ config STATIC_KEYS_SELFTEST
106106
help
107107
Boot time self-test of the branch patching code.
108108

109+
config STATIC_CALL_SELFTEST
110+
bool "Static call selftest"
111+
depends on HAVE_STATIC_CALL
112+
help
113+
Boot time self-test of the call patching code.
114+
109115
config OPTPROBES
110116
def_bool y
111117
depends on KPROBES && HAVE_OPTPROBES

kernel/static_call.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,3 +369,46 @@ static void __init static_call_init(void)
369369
#endif
370370
}
371371
early_initcall(static_call_init);
372+
373+
#ifdef CONFIG_STATIC_CALL_SELFTEST
374+
375+
static int func_a(int x)
376+
{
377+
return x+1;
378+
}
379+
380+
static int func_b(int x)
381+
{
382+
return x+2;
383+
}
384+
385+
DEFINE_STATIC_CALL(sc_selftest, func_a);
386+
387+
static struct static_call_data {
388+
int (*func)(int);
389+
int val;
390+
int expect;
391+
} static_call_data [] __initdata = {
392+
{ NULL, 2, 3 },
393+
{ func_b, 2, 4 },
394+
{ func_a, 2, 3 }
395+
};
396+
397+
static int __init test_static_call_init(void)
398+
{
399+
int i;
400+
401+
for (i = 0; i < ARRAY_SIZE(static_call_data); i++ ) {
402+
struct static_call_data *scd = &static_call_data[i];
403+
404+
if (scd->func)
405+
static_call_update(sc_selftest, scd->func);
406+
407+
WARN_ON(static_call(sc_selftest)(scd->val) != scd->expect);
408+
}
409+
410+
return 0;
411+
}
412+
early_initcall(test_static_call_init);
413+
414+
#endif /* CONFIG_STATIC_CALL_SELFTEST */

0 commit comments

Comments
 (0)