kunit: Add helpers for creating test-managed devices
Subsystems (7)
v2 cover kunit: Add helpers for creating test-managed devices
KUnit tests often need to provide a struct device, and thus far have mostly been using root_device_register() or platform devices to create a 'fake device' for use with, e.g., code which uses device-managed resources. This has several disadvantages, including not being designed for test use, scattering files in sysfs, and requiring manual teardown on test exit, which may not always be possible in case of failure.
Instead, introduce a set of helper functions which allow devices (internally a struct kunit_device) to be created and managed by KUnit -- i.e., they will be automatically unregistered on test exit. These helpers can either use a user-provided struct device_driver, or have one automatically created and managed by KUnit. In both cases, the device lives on a new kunit_bus.
This is a follow-up to a previous proposal here:
https://lore.kernel.org/linux-kselftest/[email protected]/
(The kunit_defer() function in the first patch there has since been merged as the 'deferred actions' feature.)
My intention is to take this whole series in via the kselftest/kunit branch, but I'm equally okay with splitting up the later patches which use this to go via the various subsystem trees in case there are merge conflicts.
I'd really appreciate any extra scrutiny that can be given to this; particularly around the device refcounts and whether we can guarantee that the device will be released at the correct point in the test cleanup. I've seen a few crashes in kunit_cleanup, but only on some already flaky/fragile UML/clang/alltests setups, which seem to go away if I remove the devm_add_action() call (or if I enable any debugging features / symbols, annoyingly).
Cheers,
-- David
---
Changes in v2:
- Simplify device/driver/bus matching, removing the no-longer-required kunit_bus_match function. (Thanks, Greg)
- The return values are both more consistent (kunit_device_register now returns an explicit error pointer, rather than failing the test), and better documented.
- Add some basic documentation to the implementations as well as the headers. The documentation in the headers is still more complete, and is now properly compiled into the HTML documentation (under dev-tools/kunit/api/resources.html). (Thanks, Matti)
- Moved the internal-only kunit_bus_init() function to a private header, lib/kunit/device-impl.h to avoid polluting the public headers, and match other internal-only headers. (Thanks, Greg)
- Alphabetise KUnit includes in other test modules. (Thanks, Amadeusz.)
- Several code cleanups, particularly around error handling and allocation. (Thanks Greg, Maxime)
- Several const-correctness and casting improvements. (Thanks, Greg)
- Added a new test to verify KUnit cleanup triggers device cleanup. (Thanks, Maxime).
- Improved the user-specified device test to verify that probe/remove hooks are called correctly. (Thanks, Maxime).
- The overflow test no-longer needlessly calls
kunit_device_unregister().
- Several other minor cleanups and documentation improvements, which hopefully make this a bit clearer and more robust.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
David Gow (4):
kunit: Add APIs for managing devices
fortify: test: Use kunit_device
overflow: Replace fake root_device with kunit_device
ASoC: topology: Replace fake root_device with kunit_device in tests
Documentation/dev-tools/kunit/api/resource.rst | 9 ++ Documentation/dev-tools/kunit/usage.rst | 50 +++++++ include/kunit/device.h | 80 +++++++++++ lib/fortify_kunit.c | 5 +- lib/kunit/Makefile | 3 +- lib/kunit/device.c | 181 +++++++++++++++++++++++++ lib/kunit/kunit-test.c | 134 +++++++++++++++++- lib/kunit/test.c | 3 + lib/overflow_kunit.c | 5 +- sound/soc/soc-topology-test.c | 10 +- 10 files changed, 465 insertions(+), 15 deletions(-)
---
base-commit: c8613be119892ccceffbc550b9b9d7d68b995c9e
change-id: 20230718-kunit_bus-ab19c4ef48dc
Best regards,
Signature · 1 line
David Gow <[email protected]>
PATCH v2 1/4 kunit: Add APIs for managing devices
Tests for drivers often require a struct device to pass to other functions. While it's possible to create these with root_device_register(), or to use something like a platform device, this is both a misuse of those APIs, and can be difficult to clean up after, for example, a failed assertion.
Add some KUnit-specific functions for registering and unregistering a struct device:
- kunit_device_register()
- kunit_device_register_with_driver()
- kunit_device_unregister()
These helpers allocate a on a 'kunit' bus which will either probe the driver passed in (kunit_device_register_with_driver), or will create a stub driver (kunit_device_register) which is cleaned up on test shutdown.
Devices are automatically unregistered on test shutdown, but can be manually unregistered earlier with kunit_device_unregister() in order to, for example, test device release code.
--- Documentation/dev-tools/kunit/api/resource.rst | 9 ++ Documentation/dev-tools/kunit/usage.rst | 50 +++++++ include/kunit/device.h | 80 +++++++++++ lib/kunit/Makefile | 3 +- lib/kunit/device.c | 181 +++++++++++++++++++++++++ lib/kunit/kunit-test.c | 134 +++++++++++++++++- lib/kunit/test.c | 3 + 7 files changed, 458 insertions(+), 2 deletions(-)
Documentation/dev-tools/kunit/api/resource.rst +9 -0
@@ -11,3 +11,12 @@ state on a per-test basis, register custom cleanup actions, and more. .. kernel-doc:: include/kunit/resource.h :internal: + +Managed Devices +--------------- + +Functions for using KUnit-managed struct device and struct device_driver. +Include ``kunit/device.h`` to use these. + +.. kernel-doc:: include/kunit/device.h + :internal:
Documentation/dev-tools/kunit/usage.rst +50 -0
54 lines not on this page · raw message
include/kunit/device.h new file +80 -0
80 lines not on this page · raw message
lib/kunit/Makefile +2 -1
@@ -7,7 +7,8 @@ kunit-objs += test.o \ assert.o \ try-catch.o \ executor.o \ - attributes.o + attributes.o \ + device.o ifeq ($(CONFIG_KUNIT_DEBUGFS),y) kunit-objs += debugfs.o
lib/kunit/device.c new file +181 -0
181 lines not on this page · raw message
lib/kunit/kunit-test.c +133 -1
151 lines not on this page · raw message
lib/kunit/test.c +3 -0
@@ -18,6 +18,7 @@#include <linux/sched.h> #include "debugfs.h" +#include "device-impl.h" #include "hooks-impl.h" #include "string-stream.h" #include "try-catch-impl.h"
@@ -840,6 +841,8 @@ static int __init kunit_init(void)kunit_install_hooks ();kunit_debugfs_init (); + +kunit_bus_init ();#ifdef CONFIG_MODULES return register_module_notifier (&kunit_mod_nb);#else
Signature · 1 line
2.43.0.472.g3155946c3a-goog
Re: [PATCH v2 1/4] kunit: Add APIs for managing devices
Quoted 7 lines · On Fri, 8 Dec 2023 18:09:29 +0800, [email protected] wrote:
Tests for drivers often require a struct device to pass to other functions. While it's possible to create these with
root_device_register(), or to use something like a platform device, this is both a misuse of those APIs, and can be difficult to clean up after, for example, a failed assertion.[ ... ]
Thanks!
Maxime
PATCH v2 2/4 fortify: test: Use kunit_device
In reply to [email protected]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.
--- lib/fortify_kunit.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
lib/fortify_kunit.c +3 -2
@@ -15,6 +15,7 @@*/ #define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <kunit/device.h> #include <kunit/test.h> #include <linux/device.h> #include <linux/slab.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)
Signature · 1 line
2.43.0.472.g3155946c3a-goog
PATCH v2 3/4 overflow: Replace fake root_device with kunit_device
In reply to [email protected]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.
--- lib/overflow_kunit.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
lib/overflow_kunit.c +2 -3
@@ -6,6 +6,7 @@*/ #define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <kunit/device.h> #include <kunit/test.h> #include <linux/device.h> #include <linux/kernel.h>
@@ -618,7 +619,7 @@ static void overflow_allocation_test(struct kunit *test) }while (0 )/* Create dummy device for devm_kmalloc()-family tests. */ - dev =root_device_register (device_name); + dev =kunit_device_register (test, device_name);KUNIT_ASSERT_FALSE_MSG (test,IS_ERR (dev),"Cannot register test device\n" );
@@ -634,8 +635,6 @@ static void overflow_allocation_test(struct kunit *test)check_allocation_overflow (devm_kmalloc);check_allocation_overflow (devm_kzalloc); -device_unregister (dev); -kunit_info (test,"%d allocation overflow tests finished\n" , count);#undef check_allocation_overflow }
Signature · 1 line
2.43.0.472.g3155946c3a-goog
PATCH v2 4/4 ASoC: topology: Replace fake root_device with kunit_device in tests
In reply to [email protected]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.
--- sound/soc/soc-topology-test.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)
sound/soc/soc-topology-test.c +2 -8
@@ -9,6 +9,7 @@#include <sound/core.h> #include <sound/soc.h> #include <sound/soc-topology.h> +#include <kunit/device.h> #include <kunit/test.h> /* ===== HELPER FUNCTIONS =================================================== */
@@ -21,26 +22,19 @@*/ static struct device *test_dev; -static struct device_driver test_drv = { - .name ="sound-soc-topology-test-driver" , -}; -static int snd_soc_tplg_test_init (struct kunit *test) { - test_dev =root_device_register ("sound-soc-topology-test" ); + test_dev =kunit_device_register (test,"sound-soc-topology-test" ); test_dev =get_device (test_dev);if (!test_dev)return -ENODEV ; - test_dev->driver = &test_drv; -return 0 ; }static void snd_soc_tplg_test_exit (struct kunit *test) {put_device (test_dev); -root_device_unregister (test_dev); }/*
Signature · 1 line
2.43.0.472.g3155946c3a-goog
Hi David,
Quoted 17 lines · On Fri, Dec 08, 2023 at 06:09:28PM +0800, [email protected] wrote:
KUnit tests often need to provide a struct device, and thus far have mostly been using
root_device_register()or platform devices to create a 'fake device' for use with, e.g., code which uses device-managed resources. This has several disadvantages, including not being designed for test use, scattering files in sysfs, and requiring manual teardown on test exit, which may not always be possible in case of failure.Instead, introduce a set of helper functions which allow devices (internally a struct
kunit_device) to be created and managed by KUnit -- i.e., they will be automatically unregistered on test exit. These helpers can either use a user-provided structdevice_driver, or have one automatically created and managed by KUnit. In both cases, the device lives on a newkunit_bus.This is a follow-up to a previous proposal here:
https://lore.kernel.org/linux-kselftest/[email protected]/
Quoted 7 lines
(The
kunit_defer()function in the first patch there has since been merged as the 'deferred actions' feature.)My intention is to take this whole series in via the kselftest/kunit branch, but I'm equally okay with splitting up the later patches which use this to go via the various subsystem trees in case there are merge conflicts.
Could you take (and apply eventually) that patch as part of your series?
https://lore.kernel.org/linux-kselftest/[email protected]/
Thanks
Maxime
Quoted 24 lines · Maxime Ripard
Hi David,
Quoted 20 lines · On Fri, Dec 08, 2023 at 06:09:28PM +0800, [email protected] wrote:
KUnit tests often need to provide a struct device, and thus far have mostly been using
root_device_register()or platform devices to create a 'fake device' for use with, e.g., code which uses device-managed resources. This has several disadvantages, including not being designed for test use, scattering files in sysfs, and requiring manual teardown on test exit, which may not always be possible in case of failure.Instead, introduce a set of helper functions which allow devices (internally a struct
kunit_device) to be created and managed by KUnit -- i.e., they will be automatically unregistered on test exit. These helpers can either use a user-provided structdevice_driver, or have one automatically created and managed by KUnit. In both cases, the device lives on a newkunit_bus.This is a follow-up to a previous proposal here:
https://lore.kernel.org/linux-kselftest/[email protected]/(The
kunit_defer()function in the first patch there has since been merged as the 'deferred actions' feature.)
Quoted 8 lines
Quoted 4 lines
My intention is to take this whole series in via the kselftest/kunit branch, but I'm equally okay with splitting up the later patches which use this to go via the various subsystem trees in case there are merge conflicts.
Could you take (and apply eventually) that patch as part of your series?
https://lore.kernel.org/linux-kselftest/[email protected]/
Thanks -- I've included it in v3 (which fixes a few other issues), and will take it along with the rest of the series:
https://lore.kernel.org/linux-kselftest/[email protected]/T/
Cheers,
-- David