Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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: d03c720e03bd ("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>

authored by

David Gow and committed by
Shuah Khan
829388b7 1a9f2c77

+19
+2
lib/kunit/device-impl.h
··· 13 13 14 14 // For internal use only -- registers the kunit_bus. 15 15 int kunit_bus_init(void); 16 + // For internal use only -- unregisters the kunit_bus. 17 + void kunit_bus_shutdown(void); 16 18 17 19 #endif //_KUNIT_DEVICE_IMPL_H
+14
lib/kunit/device.c
··· 54 54 return error; 55 55 } 56 56 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 + 57 71 /* Release a 'fake' KUnit device. */ 58 72 static void kunit_device_release(struct device *d) 59 73 {
+3
lib/kunit/test.c
··· 928 928 #ifdef CONFIG_MODULES 929 929 unregister_module_notifier(&kunit_mod_nb); 930 930 #endif 931 + 932 + kunit_bus_shutdown(); 933 + 931 934 kunit_debugfs_cleanup(); 932 935 } 933 936 module_exit(kunit_exit);