diff mbox series

[2/4] fortify: test: Use kunit_device

Message ID 20231205-kunit_bus-v1-2-635036d3bc13@google.com (mailing list archive)
State Superseded
Headers show
Series kunit: Add helpers for creating test-managed devices | expand

Commit Message

David Gow Dec. 5, 2023, 7:31 a.m. UTC
Using struct root_device to create fake devices for tests is something
of a hack. The new struct kunit_device is meant for this purpose, so use
it instead.

Signed-off-by: David Gow <davidgow@google.com>
---
 lib/fortify_kunit.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Comments

Matti Vaittinen Dec. 5, 2023, 8:39 a.m. UTC | #1
On 12/5/23 09:31, davidgow@google.com wrote:
> Using struct root_device to create fake devices for tests is something
> of a hack. The new struct kunit_device is meant for this purpose, so use
> it instead.
> 
> Signed-off-by: David Gow <davidgow@google.com>

Reviewed-by: Matti Vaittinen <mazziesaccount@gmail.com>
Kees Cook Dec. 6, 2023, 9:07 p.m. UTC | #2
On Tue, Dec 05, 2023 at 03:31:34PM +0800, davidgow@google.com wrote:
> Using struct root_device to create fake devices for tests is something
> of a hack. The new struct kunit_device is meant for this purpose, so use
> it instead.
> 
> Signed-off-by: David Gow <davidgow@google.com>
> ---
>  lib/fortify_kunit.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/lib/fortify_kunit.c b/lib/fortify_kunit.c
> index c8c33cbaae9e..f7a1fce8849b 100644
> --- a/lib/fortify_kunit.c
> +++ b/lib/fortify_kunit.c
> @@ -16,6 +16,7 @@
>  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
>  
>  #include <kunit/test.h>
> +#include <kunit/device.h>
>  #include <linux/device.h>
>  #include <linux/slab.h>
>  #include <linux/string.h>
> @@ -269,7 +270,7 @@ DEFINE_ALLOC_SIZE_TEST_PAIR(kvmalloc)
>  	size_t len;							\
>  									\
>  	/* Create dummy device for devm_kmalloc()-family tests. */	\
> -	dev = root_device_register(dev_name);				\
> +	dev = kunit_device_register(test, dev_name);			\
>  	KUNIT_ASSERT_FALSE_MSG(test, IS_ERR(dev),			\
>  			       "Cannot register test device\n");	\
>  									\
> @@ -303,7 +304,7 @@ DEFINE_ALLOC_SIZE_TEST_PAIR(kvmalloc)
>  	checker(len, devm_kmemdup(dev, "Ohai", len, gfp),		\
>  		devm_kfree(dev, p));					\
>  									\
> -	device_unregister(dev);						\
> +	kunit_device_unregister(test, dev);				\
>  } while (0)
>  DEFINE_ALLOC_SIZE_TEST_PAIR(devm_kmalloc)

Acked-by: Kees Cook <keescook@chromium.org>

(As an aside; shouldn't this get automatically cleaned up like other
kunit resources, though?)
David Gow Dec. 8, 2023, 7:38 a.m. UTC | #3
On Thu, 7 Dec 2023 at 05:07, Kees Cook <keescook@chromium.org> wrote:
>
> On Tue, Dec 05, 2023 at 03:31:34PM +0800, davidgow@google.com wrote:
> > Using struct root_device to create fake devices for tests is something
> > of a hack. The new struct kunit_device is meant for this purpose, so use
> > it instead.
> >
> > Signed-off-by: David Gow <davidgow@google.com>
> > ---
> >  lib/fortify_kunit.c | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/lib/fortify_kunit.c b/lib/fortify_kunit.c
> > index c8c33cbaae9e..f7a1fce8849b 100644
> > --- a/lib/fortify_kunit.c
> > +++ b/lib/fortify_kunit.c
> > @@ -16,6 +16,7 @@
> >  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
> >
> >  #include <kunit/test.h>
> > +#include <kunit/device.h>
> >  #include <linux/device.h>
> >  #include <linux/slab.h>
> >  #include <linux/string.h>
> > @@ -269,7 +270,7 @@ DEFINE_ALLOC_SIZE_TEST_PAIR(kvmalloc)
> >       size_t len;                                                     \
> >                                                                       \
> >       /* Create dummy device for devm_kmalloc()-family tests. */      \
> > -     dev = root_device_register(dev_name);                           \
> > +     dev = kunit_device_register(test, dev_name);                    \
> >       KUNIT_ASSERT_FALSE_MSG(test, IS_ERR(dev),                       \
> >                              "Cannot register test device\n");        \
> >                                                                       \
> > @@ -303,7 +304,7 @@ DEFINE_ALLOC_SIZE_TEST_PAIR(kvmalloc)
> >       checker(len, devm_kmemdup(dev, "Ohai", len, gfp),               \
> >               devm_kfree(dev, p));                                    \
> >                                                                       \
> > -     device_unregister(dev);                                         \
> > +     kunit_device_unregister(test, dev);                             \
> >  } while (0)
> >  DEFINE_ALLOC_SIZE_TEST_PAIR(devm_kmalloc)
>
> Acked-by: Kees Cook <keescook@chromium.org>
>
> (As an aside; shouldn't this get automatically cleaned up like other
> kunit resources, though?)
>

We can't just get rid of the {kunit_,}device_unregister() here,
because otherwise we'd have several devices with the same name during
the test.

So, yes, these get automatically cleaned up, but the test would have
to be restructured to either give each device a different name, or
split the tests up further.

-- David
diff mbox series

Patch

diff --git a/lib/fortify_kunit.c b/lib/fortify_kunit.c
index c8c33cbaae9e..f7a1fce8849b 100644
--- a/lib/fortify_kunit.c
+++ b/lib/fortify_kunit.c
@@ -16,6 +16,7 @@ 
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 #include <kunit/test.h>
+#include <kunit/device.h>
 #include <linux/device.h>
 #include <linux/slab.h>
 #include <linux/string.h>
@@ -269,7 +270,7 @@  DEFINE_ALLOC_SIZE_TEST_PAIR(kvmalloc)
 	size_t len;							\
 									\
 	/* Create dummy device for devm_kmalloc()-family tests. */	\
-	dev = root_device_register(dev_name);				\
+	dev = kunit_device_register(test, dev_name);			\
 	KUNIT_ASSERT_FALSE_MSG(test, IS_ERR(dev),			\
 			       "Cannot register test device\n");	\
 									\
@@ -303,7 +304,7 @@  DEFINE_ALLOC_SIZE_TEST_PAIR(kvmalloc)
 	checker(len, devm_kmemdup(dev, "Ohai", len, gfp),		\
 		devm_kfree(dev, p));					\
 									\
-	device_unregister(dev);						\
+	kunit_device_unregister(test, dev);				\
 } while (0)
 DEFINE_ALLOC_SIZE_TEST_PAIR(devm_kmalloc)