diff mbox series

[v4,linux-kselftest-test,5/6] kunit: allow kunit to be loaded as a module

Message ID 1573812972-10529-6-git-send-email-alan.maguire@oracle.com (mailing list archive)
State New
Headers show
Series kunit: support building core/tests as modules | expand

Commit Message

Alan Maguire Nov. 15, 2019, 10:16 a.m. UTC
Making kunit itself buildable as a module allows for "always-on"
kunit configuration; specifying CONFIG_KUNIT=m means the module
is built but only used when loaded.  Kunit test modules will load
kunit.ko as an implicit dependency, so simply running
"modprobe my-kunit-tests" will load the tests along with the kunit
module and run them.

Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
Signed-off-by: Knut Omang <knut.omang@oracle.com>
---
 lib/kunit/Kconfig  |  2 +-
 lib/kunit/Makefile |  4 +++-
 lib/kunit/test.c   | 13 +++++++++++++
 3 files changed, 17 insertions(+), 2 deletions(-)

Comments

Stephen Boyd Nov. 19, 2019, 9:13 p.m. UTC | #1
Quoting Alan Maguire (2019-11-15 02:16:11)
> diff --git a/lib/kunit/test.c b/lib/kunit/test.c
> index 87b5cf1..41ef71a 100644
> --- a/lib/kunit/test.c
> +++ b/lib/kunit/test.c
> @@ -486,3 +486,16 @@ void kunit_cleanup(struct kunit *test)
>         }
>  }
>  EXPORT_SYMBOL_GPL(kunit_cleanup);
> +
> +static int kunit_init(void)

Missing __init?

> +{
> +       return 0;
> +}
> +late_initcall(kunit_init);

It looks pretty weird that this doesn't do anything in the module init
or exit path. How does it work? And why does it need to be late init if
nothing is called from here?

> +
> +static void __exit kunit_exit(void)
> +{
> +}
> +module_exit(kunit_exit);
> +
> +MODULE_LICENSE("GPL");

I guess should be "GPL v2"?
Alan Maguire Nov. 20, 2019, 1:46 p.m. UTC | #2
On Tue, 19 Nov 2019, Stephen Boyd wrote:

> Quoting Alan Maguire (2019-11-15 02:16:11)
> > diff --git a/lib/kunit/test.c b/lib/kunit/test.c
> > index 87b5cf1..41ef71a 100644
> > --- a/lib/kunit/test.c
> > +++ b/lib/kunit/test.c
> > @@ -486,3 +486,16 @@ void kunit_cleanup(struct kunit *test)
> >         }
> >  }
> >  EXPORT_SYMBOL_GPL(kunit_cleanup);
> > +
> > +static int kunit_init(void)
> 
> Missing __init?
>

Oops, will fix.
 
> > +{
> > +       return 0;
> > +}
> > +late_initcall(kunit_init);
> 
> It looks pretty weird that this doesn't do anything in the module init
> or exit path. How does it work? And why does it need to be late init if
> nothing is called from here?
>

I used a late_initcall() here because if there is initialization code in 
the future (say setting up debugfs, sysctl tables etc), it will get run 
irrespective of whether kunit is built as a module or not.  For the module
case, we need init/exit to ensure the module can be unloaded 
successfully.

> > +
> > +static void __exit kunit_exit(void)
> > +{
> > +}
> > +module_exit(kunit_exit);
> > +
> > +MODULE_LICENSE("GPL");
> 
> I guess should be "GPL v2"?
> 

Good catch, will fix here and elsewhere, thanks!

BTW in replying to your review of patch 1 I made a mistake; we do in fact 
need to add the "#include <linux/kernel.h>" in assert.h as we are removing 
the "#include <kunit/string-stream.h>".  The latter indirectly includes 
linux/kernel.h which allows us to find definition for "struct va_format"
used in the struct kunit_assert definition. Apologies about this.

I'll have a go at generating v5 using -C as requested. Thanks again for 
the careful review!

Alan
diff mbox series

Patch

diff --git a/lib/kunit/Kconfig b/lib/kunit/Kconfig
index 9ebd5e6..065aa16 100644
--- a/lib/kunit/Kconfig
+++ b/lib/kunit/Kconfig
@@ -3,7 +3,7 @@ 
 #
 
 menuconfig KUNIT
-	bool "KUnit - Enable support for unit tests"
+	tristate "KUnit - Enable support for unit tests"
 	help
 	  Enables support for kernel unit tests (KUnit), a lightweight unit
 	  testing and mocking framework for the Linux kernel. These tests are
diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile
index bc6e5e54..fab5564 100644
--- a/lib/kunit/Makefile
+++ b/lib/kunit/Makefile
@@ -1,4 +1,6 @@ 
-obj-$(CONFIG_KUNIT) +=			test.o \
+obj-$(CONFIG_KUNIT) +=			kunit.o
+
+kunit-objs +=				test.o \
 					string-stream.o \
 					assert.o \
 					try-catch.o
diff --git a/lib/kunit/test.c b/lib/kunit/test.c
index 87b5cf1..41ef71a 100644
--- a/lib/kunit/test.c
+++ b/lib/kunit/test.c
@@ -486,3 +486,16 @@  void kunit_cleanup(struct kunit *test)
 	}
 }
 EXPORT_SYMBOL_GPL(kunit_cleanup);
+
+static int kunit_init(void)
+{
+	return 0;
+}
+late_initcall(kunit_init);
+
+static void __exit kunit_exit(void)
+{
+}
+module_exit(kunit_exit);
+
+MODULE_LICENSE("GPL");