Skip to content

Commit 829388b

Browse files
sulixshuahkh
authored andcommitted
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: d03c720 ("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>
1 parent 1a9f2c7 commit 829388b

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

lib/kunit/device-impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@
1313

1414
// For internal use only -- registers the kunit_bus.
1515
int kunit_bus_init(void);
16+
// For internal use only -- unregisters the kunit_bus.
17+
void kunit_bus_shutdown(void);
1618

1719
#endif //_KUNIT_DEVICE_IMPL_H

lib/kunit/device.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ int kunit_bus_init(void)
5454
return error;
5555
}
5656

57+
/* Unregister the 'kunit_bus' in case the KUnit module is unloaded. */
58+
void kunit_bus_shutdown(void)
59+
{
60+
/* Make sure the bus exists before we unregister it. */
61+
if (IS_ERR_OR_NULL(kunit_bus_device))
62+
return;
63+
64+
bus_unregister(&kunit_bus_type);
65+
66+
root_device_unregister(kunit_bus_device);
67+
68+
kunit_bus_device = NULL;
69+
}
70+
5771
/* Release a 'fake' KUnit device. */
5872
static void kunit_device_release(struct device *d)
5973
{

lib/kunit/test.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,9 @@ static void __exit kunit_exit(void)
928928
#ifdef CONFIG_MODULES
929929
unregister_module_notifier(&kunit_mod_nb);
930930
#endif
931+
932+
kunit_bus_shutdown();
933+
931934
kunit_debugfs_cleanup();
932935
}
933936
module_exit(kunit_exit);

0 commit comments

Comments
 (0)