Skip to content

Commit

Permalink
static_call: Add simple self-test for static calls
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Peter Zijlstra authored and Ingo Molnar committed Sep 1, 2020
1 parent 1e7e478 commit f03c412
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
6 changes: 6 additions & 0 deletions arch/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ config STATIC_KEYS_SELFTEST
help
Boot time self-test of the branch patching code.

config STATIC_CALL_SELFTEST
bool "Static call selftest"
depends on HAVE_STATIC_CALL
help
Boot time self-test of the call patching code.

config OPTPROBES
def_bool y
depends on KPROBES && HAVE_OPTPROBES
Expand Down
43 changes: 43 additions & 0 deletions kernel/static_call.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,46 @@ static void __init static_call_init(void)
#endif
}
early_initcall(static_call_init);

#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 */

0 comments on commit f03c412

Please sign in to comment.