kunit: device: Unregister the kunit_bus on shutdown

If KUnit is built as a module, and it's unloaded, the kunit_bus is not
unregistered. This causes an error if it's then re-loaded later, as we
try to re-register the bus.

Unregister the bus and root_device on shutdown, if it looks valid.

In addition, be more specific about the value of kunit_bus_device. It
is:
- a valid struct device* if the kunit_bus initialised correctly.
- an ERR_PTR if it failed to initialise.
- NULL before initialisation and after shutdown.

Fixes: d03c720e03 ("kunit: Add APIs for managing devices")
Signed-off-by: David Gow <davidgow@google.com>
Reviewed-by: Rae Moar <rmoar@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
This commit is contained in:
David Gow 2024-02-01 14:04:36 +08:00 committed by Shuah Khan
parent 1a9f2c776d
commit 829388b725
3 changed files with 19 additions and 0 deletions

View File

@ -13,5 +13,7 @@
// For internal use only -- registers the kunit_bus.
int kunit_bus_init(void);
// For internal use only -- unregisters the kunit_bus.
void kunit_bus_shutdown(void);
#endif //_KUNIT_DEVICE_IMPL_H

View File

@ -54,6 +54,20 @@ int kunit_bus_init(void)
return error;
}
/* Unregister the 'kunit_bus' in case the KUnit module is unloaded. */
void kunit_bus_shutdown(void)
{
/* Make sure the bus exists before we unregister it. */
if (IS_ERR_OR_NULL(kunit_bus_device))
return;
bus_unregister(&kunit_bus_type);
root_device_unregister(kunit_bus_device);
kunit_bus_device = NULL;
}
/* Release a 'fake' KUnit device. */
static void kunit_device_release(struct device *d)
{

View File

@ -928,6 +928,9 @@ static void __exit kunit_exit(void)
#ifdef CONFIG_MODULES
unregister_module_notifier(&kunit_mod_nb);
#endif
kunit_bus_shutdown();
kunit_debugfs_cleanup();
}
module_exit(kunit_exit);