Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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

authored by

Peter Zijlstra and committed by
Ingo Molnar
f03c4129 1e7e4788

+49
+6
arch/Kconfig
··· 106 106 help 107 107 Boot time self-test of the branch patching code. 108 108 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 + 109 115 config OPTPROBES 110 116 def_bool y 111 117 depends on KPROBES && HAVE_OPTPROBES
+43
kernel/static_call.c
··· 369 369 #endif 370 370 } 371 371 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 */