[v6,11/17] static_call: Simple self-test
diff mbox series

Message ID 20200710134336.798619415@infradead.org
State New
Headers show
Series
  • Add static_call
Related show

Commit Message

Peter Zijlstra July 10, 2020, 1:38 p.m. UTC
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
 arch/Kconfig         |    6 ++++++
 kernel/static_call.c |   28 ++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)

Comments

Steven Rostedt July 10, 2020, 10:42 p.m. UTC | #1
On Fri, 10 Jul 2020 15:38:42 +0200
Peter Zijlstra <peterz@infradead.org> wrote:

> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> ---
>  arch/Kconfig         |    6 ++++++
>  kernel/static_call.c |   28 ++++++++++++++++++++++++++++
>  2 files changed, 34 insertions(+)
> 
> --- a/arch/Kconfig
> +++ b/arch/Kconfig
> @@ -103,6 +103,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
> --- a/kernel/static_call.c
> +++ b/kernel/static_call.c
> @@ -364,3 +364,31 @@ 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 int __init test_static_call_init(void)
> +{
> +	WARN_ON(static_call(sc_selftest)(2) != 3);
> +	static_call_update(sc_selftest, &func_b);
> +	WARN_ON(static_call(sc_selftest)(2) != 4);
> +	static_call_update(sc_selftest, &func_a);
> +	WARN_ON(static_call(sc_selftest)(2) != 3);
> +
> +	return 0;
> +}

I wonder if this would be better if we were testing the same static call each time?

static int __init run_static_call(int val)
{
	return static_call(sc_selftest)(val);
}

static struct {
	int (*func)(int);
	int val;
	int expect;
} static_call_data [] = {
	{ NULL, 2, 3 }
	( func_b, 2 , 4},
	{ func_a, 2, 3}
} __initdata;

static int __init test_static_call_init(void)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(static_call_data); i++ ) {
		if (static_call_data[i].func)
			static_call_update(sc_selftest, static_call_data[i].func);
		WARN_ON(run_static_call(static_call_data[i].val) != static_call_data[i].expect);
	}

	return 0;
}

-- Steve


> +early_initcall(test_static_call_init);
> +
> +#endif /* CONFIG_STATIC_CALL_SELFTEST */
>
Peter Zijlstra July 11, 2020, 10:27 a.m. UTC | #2
On Fri, Jul 10, 2020 at 06:42:29PM -0400, Steven Rostedt wrote:
> On Fri, 10 Jul 2020 15:38:42 +0200
> Peter Zijlstra <peterz@infradead.org> wrote:

> > +static int __init test_static_call_init(void)
> > +{
> > +	WARN_ON(static_call(sc_selftest)(2) != 3);
> > +	static_call_update(sc_selftest, &func_b);
> > +	WARN_ON(static_call(sc_selftest)(2) != 4);
> > +	static_call_update(sc_selftest, &func_a);
> > +	WARN_ON(static_call(sc_selftest)(2) != 3);
> > +
> > +	return 0;
> > +}
> 
> I wonder if this would be better if we were testing the same static call each time?

Makes sense, I suppose.

> static int __init run_static_call(int val)
> {
> 	return static_call(sc_selftest)(val);
> }

Don't think we need this, or are you afraid of loop unrolling, in which
case you also want a noinline here I suppose.

> 
> static struct {
> 	int (*func)(int);
> 	int val;
> 	int expect;
> } static_call_data [] = {
> 	{ NULL, 2, 3 }
> 	( func_b, 2 , 4},
> 	{ func_a, 2, 3}
> } __initdata;
> 
> static int __init test_static_call_init(void)
> {
> 	int i;
> 
> 	for (i = 0; i < ARRAY_SIZE(static_call_data); i++ ) {
> 		if (static_call_data[i].func)
> 			static_call_update(sc_selftest, static_call_data[i].func);
> 		WARN_ON(run_static_call(static_call_data[i].val) != static_call_data[i].expect);
> 	}
> 
> 	return 0;
> }

Lots of compile errors with that, fixed them all :-)
Steven Rostedt July 13, 2020, 8:26 p.m. UTC | #3
On Sat, 11 Jul 2020 12:27:02 +0200
Peter Zijlstra <peterz@infradead.org> wrote:

> > static int __init test_static_call_init(void)
> > {
> > 	int i;
> > 
> > 	for (i = 0; i < ARRAY_SIZE(static_call_data); i++ ) {
> > 		if (static_call_data[i].func)
> > 			static_call_update(sc_selftest, static_call_data[i].func);
> > 		WARN_ON(run_static_call(static_call_data[i].val) != static_call_data[i].expect);
> > 	}
> > 
> > 	return 0;
> > }  
> 
> Lots of compile errors with that, fixed them all :-)

Hey, I wrote that all without compiling it. I would have been surprised
if it was clean.

-- Steve

Patch
diff mbox series

--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -103,6 +103,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
--- a/kernel/static_call.c
+++ b/kernel/static_call.c
@@ -364,3 +364,31 @@  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 int __init test_static_call_init(void)
+{
+	WARN_ON(static_call(sc_selftest)(2) != 3);
+	static_call_update(sc_selftest, &func_b);
+	WARN_ON(static_call(sc_selftest)(2) != 4);
+	static_call_update(sc_selftest, &func_a);
+	WARN_ON(static_call(sc_selftest)(2) != 3);
+
+	return 0;
+}
+early_initcall(test_static_call_init);
+
+#endif /* CONFIG_STATIC_CALL_SELFTEST */