Skip to contentSkip to conversation

drivers: base: Add tests showing devm handling inconsistencies

PATCH In review Maxime Ripard 2 participants 9 messages Jul 20, 2023 – Aug 12, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover drivers: base: Add tests showing devm handling inconsistencies

Hi,

This follows the discussion here:
https://lore.kernel.org/linux-kselftest/20230324123157.bbwvfq4gsxnlnfwb@houat/

This shows a couple of inconsistencies with regard to how device-managed resources are cleaned up. Basically, devm resources will only be cleaned up if the device is attached to a bus and bound to a driver. Failing any of these cases, a call to device_unregister will not end up in the devm resources being released.

We had to work around it in DRM to provide helpers to create a device for kunit tests, but the current discussion around creating similar, generic, helpers for kunit resumed interest in fixing this.

This can be tested using the command:
./tools/testing/kunit/kunit.py run --kunitconfig=drivers/base/test/

I added the fix David suggested back in that discussion which does fix the tests. The SoB is missing, since David didn't provide it back then.

Let me know what you think,
Maxime

---
Changes in v3:
- Reworded the commit logs according to David's feedback
- Rebased on current next
- Link to v2: https://lore.kernel.org/r/20230329-kunit-devm-inconsistencies-test-v2-0-19feb71e864b@kernel.org

Changes in v2:
- Use an init function
- Document the tests
- Add a fix for the bugs
- Link to v1: https://lore.kernel.org/r/20230329-kunit-devm-inconsistencies-test-v1-0-c33127048375@cerno.tech

---
David Gow (1):
drivers: base: Free devm resources when unregistering a device

Maxime Ripard (2):
drivers: base: Add basic devm tests for root devices
drivers: base: Add basic devm tests for platform devices

 drivers/base/core.c                      |  11 ++
 drivers/base/test/.kunitconfig           |   2 +
 drivers/base/test/Kconfig                |   4 +
 drivers/base/test/Makefile               |   3 +
 drivers/base/test/platform-device-test.c | 220 +++++++++++++++++++++++++++++++
 drivers/base/test/root-device-test.c     | 108 +++++++++++++++
 6 files changed, 348 insertions(+)

---
base-commit: c58c49dd89324b18a812762a2bfa5a0458e4f252
change-id: 20230329-kunit-devm-inconsistencies-test-5e5a7d01e60d

Best regards,

Signature · 1 line
Maxime Ripard <[email protected]>
#1 lore raw

PATCH v3 1/3 drivers: base: Add basic devm tests for root devices

The root devices show some odd behaviours compared to regular "bus" devices that have been probed through the usual mechanism, so let's create kunit tests to exercise those paths and odd cases.

It's not clear whether root devices are even allowed to use device managed resources, but the fact that it works in some cases but not others like shown in that test suite shouldn't happen either way: we want to make it consistent and documented.

These tests will (after the following patches) ensure that consistency and effectively document that it's allowed.

If it ever turns out to be a bad idea, we can always roll back and modify the tests then.

---
 drivers/base/test/.kunitconfig       |   2 +
 drivers/base/test/Kconfig            |   4 ++
 drivers/base/test/Makefile           |   2 +
 drivers/base/test/root-device-test.c | 110 +++++++++++++++++++++++++++++++++++
 4 files changed, 118 insertions(+)
drivers/base/test/.kunitconfig new file +2 -0
new file mode 100644
index 000000000000..473923f0998b
@@ -0,0 +1,2 @@
+CONFIG_KUNIT=y
+CONFIG_DM_KUNIT_TEST=y
drivers/base/test/Kconfig +4 -0
index 610a1ba7a467..9d42051f8f8e 100644
@@ -9,6 +9,10 @@ config TEST_ASYNC_DRIVER_PROBE
 
 	  If unsure say N.
 
+config DM_KUNIT_TEST
+	tristate "KUnit Tests for the device model" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+
 config DRIVER_PE_KUNIT_TEST
 	bool "KUnit Tests for property entry API" if !KUNIT_ALL_TESTS
 	depends on KUNIT=y
drivers/base/test/Makefile +2 -0
index 7f76fee6f989..d589ca3fa8fc 100644
@@ -1,5 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_TEST_ASYNC_DRIVER_PROBE)	+= test_async_driver_probe.o
 
+obj-$(CONFIG_DM_KUNIT_TEST)	+= root-device-test.o
+
 obj-$(CONFIG_DRIVER_PE_KUNIT_TEST) += property-entry-test.o
 CFLAGS_property-entry-test.o += $(DISABLE_STRUCTLEAK_PLUGIN)
drivers/base/test/root-device-test.c new file +110 -0

110 lines not on this page · raw message

Signature · 1 line
2.41.0
#2 lore raw

PATCH v3 2/3 drivers: base: Add basic devm tests for platform devices

In reply to Maxime Ripard

Platform devices show some inconsistencies with how devm resources are released when the device has been probed and when it hasn't.

In particular, we can register device-managed actions no matter if the device has be bound to a driver or not, but devres_release_all() will only be called if it was bound to a driver or if there's no reference held to it anymore.

If it wasn't bound to a driver and we still have a reference, devres_release_all() will never get called. This is surprising considering that if the driver isn't bound but doesn't have any reference to it anymore, that function will get called, and if it was bound to a driver but still has references, that function will get called as well.

Even if that case is fairly unusual, it can easily lead to memory leaks.

The plan is, with the next patch, to make it consistent and enforce that devres_release_all() is called no matter what situation we're in. For now, it just tests for the current behaviour and skips over the inconsistencies.

---
 drivers/base/test/Makefile               |   1 +
 drivers/base/test/platform-device-test.c | 222 +++++++++++++++++++++++++++++++
 2 files changed, 223 insertions(+)
drivers/base/test/Makefile +1 -0
index d589ca3fa8fc..e321dfc7e922 100644
@@ -2,6 +2,7 @@
 obj-$(CONFIG_TEST_ASYNC_DRIVER_PROBE)	+= test_async_driver_probe.o
 
 obj-$(CONFIG_DM_KUNIT_TEST)	+= root-device-test.o
+obj-$(CONFIG_DM_KUNIT_TEST)	+= platform-device-test.o
 
 obj-$(CONFIG_DRIVER_PE_KUNIT_TEST) += property-entry-test.o
 CFLAGS_property-entry-test.o += $(DISABLE_STRUCTLEAK_PLUGIN)
drivers/base/test/platform-device-test.c new file +222 -0

222 lines not on this page · raw message

Signature · 1 line
2.41.0
#3 lore raw

PATCH v3 3/3 drivers: base: Free devm resources when unregistering a device

In reply to Maxime Ripard

From: David Gow <[email protected]>

In the current code, devres_release_all() only gets called if the device has a bus and has been probed.

This leads to issues when using bus-less or driver-less devices where the device might never get freed if a managed resource holds a reference to the device. This is happening in the DRM framework for example.

We should thus call devres_release_all() in the device_del() function to make sure that the device-managed actions are properly executed when the device is unregistered, even if it has neither a bus nor a driver.

This is effectively the same change than commit 2f8d16a996da ("devres:
release resources on device_del()") that got reverted by commit a525a3ddeaca ("driver core: free devres in device_release") over memory leaks concerns.

This patch effectively combines the two commits mentioned above to release the resources both on device_del() and device_release() and get the best of both worlds.

Fixes a525a3ddeaca ("driver core: free devres in device_release")
---
 drivers/base/core.c                      | 11 +++++++++++
 drivers/base/test/platform-device-test.c |  2 --
 drivers/base/test/root-device-test.c     |  2 --
 3 files changed, 11 insertions(+), 4 deletions(-)
drivers/base/core.c +11 -0
index 3dff5037943e..6ceaf50f5a67 100644
@@ -3817,6 +3817,17 @@ void device_del(struct device *dev)
 	device_platform_notify_remove(dev);
 	device_links_purge(dev);
 
+	/*
+	 * If a device does not have a driver attached, we need to clean
+	 * up any managed resources. We do this in device_release(), but
+	 * it's never called (and we leak the device) if a managed
+	 * resource holds a reference to the device. So release all
+	 * managed resources here, like we do in driver_detach(). We
+	 * still need to do so again in device_release() in case someone
+	 * adds a new resource after this point, though.
+	 */
+	devres_release_all(dev);
+
 	bus_notify(dev, BUS_NOTIFY_REMOVED_DEVICE);
 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
 	glue_dir = get_glue_dir(dev);
drivers/base/test/platform-device-test.c +0 -2
index b6ebf1dcdffb..1ae5ce8bd366 100644
@@ -87,8 +87,6 @@ static void platform_device_devm_register_get_unregister_with_devm_test(struct k
 	struct test_priv *priv = test->priv;
 	int ret;
 
-	kunit_skip(test, "This needs to be fixed in the core.");
-
 	pdev = platform_device_alloc(DEVICE_NAME, PLATFORM_DEVID_NONE);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
 
drivers/base/test/root-device-test.c +0 -2
index 9a3e6cccae13..780d07455f57 100644
@@ -78,8 +78,6 @@ static void root_device_devm_register_get_unregister_with_devm_test(struct kunit
 	struct test_priv *priv = test->priv;
 	int ret;
 
-	kunit_skip(test, "This needs to be fixed in the core.");
-
 	priv->dev = root_device_register(DEVICE_NAME);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->dev);
 
Signature · 1 line
2.41.0
#4 lore raw
In reply to Maxime Ripard
Quoted 18 lines · Maxime Ripard

Hi,

This follows the discussion here:
https://lore.kernel.org/linux-kselftest/20230324123157.bbwvfq4gsxnlnfwb@houat/

This shows a couple of inconsistencies with regard to how device-managed resources are cleaned up. Basically, devm resources will only be cleaned up if the device is attached to a bus and bound to a driver. Failing any of these cases, a call to device_unregister will not end up in the devm resources being released.

We had to work around it in DRM to provide helpers to create a device for kunit tests, but the current discussion around creating similar, generic, helpers for kunit resumed interest in fixing this.

This can be tested using the command:
./tools/testing/kunit/kunit.py run --kunitconfig=drivers/base/test/

Quoted 7 lines

I added the fix David suggested back in that discussion which does fix the tests. The SoB is missing, since David didn't provide it back then.

Let me know what you think,
Maxime

Ping?

Maxime

#5 lore raw
Quoted 22 lines · Maxime Ripard
Quoted 21 lines · Maxime Ripard

Hi,

This follows the discussion here:
https://lore.kernel.org/linux-kselftest/20230324123157.bbwvfq4gsxnlnfwb@houat/

This shows a couple of inconsistencies with regard to how device-managed resources are cleaned up. Basically, devm resources will only be cleaned up if the device is attached to a bus and bound to a driver. Failing any of these cases, a call to device_unregister will not end up in the devm resources being released.

We had to work around it in DRM to provide helpers to create a device for kunit tests, but the current discussion around creating similar, generic, helpers for kunit resumed interest in fixing this.

This can be tested using the command:
./tools/testing/kunit/kunit.py run --kunitconfig=drivers/base/test/

I added the fix David suggested back in that discussion which does fix the tests. The SoB is missing, since David didn't provide it back then.

Quoted 6 lines
Quoted 4 lines

Let me know what you think,
Maxime

Ping?

It's in my review queue, still trying to catch up...

#6 lore raw
Quoted 23 lines · Greg Kroah-Hartman
Quoted 22 lines · Maxime Ripard
Quoted 21 lines · Maxime Ripard

Hi,

This follows the discussion here:
https://lore.kernel.org/linux-kselftest/20230324123157.bbwvfq4gsxnlnfwb@houat/

This shows a couple of inconsistencies with regard to how device-managed resources are cleaned up. Basically, devm resources will only be cleaned up if the device is attached to a bus and bound to a driver. Failing any of these cases, a call to device_unregister will not end up in the devm resources being released.

We had to work around it in DRM to provide helpers to create a device for kunit tests, but the current discussion around creating similar, generic, helpers for kunit resumed interest in fixing this.

This can be tested using the command:
./tools/testing/kunit/kunit.py run --kunitconfig=drivers/base/test/

I added the fix David suggested back in that discussion which does fix the tests. The SoB is missing, since David didn't provide it back then.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

Let me know what you think,
Maxime

Ping?

It's in my review queue, still trying to catch up...

I didn't make it here this week, sorry. I kind of worry about encoding the current "odd" functionality in a test as being the correct thing, but will look at it closer next week.

thanks,

greg k-h

#7 lore raw

Hi,

Quoted 27 lines · Greg Kroah-Hartman
Quoted 26 lines · Greg Kroah-Hartman
Quoted 25 lines · Maxime Ripard
Quoted 24 lines · Maxime Ripard

Hi,

This follows the discussion here:
https://lore.kernel.org/linux-kselftest/20230324123157.bbwvfq4gsxnlnfwb@houat/

This shows a couple of inconsistencies with regard to how device-managed resources are cleaned up. Basically, devm resources will only be cleaned up if the device is attached to a bus and bound to a driver. Failing any of these cases, a call to device_unregister will not end up in the devm resources being released.

We had to work around it in DRM to provide helpers to create a device for kunit tests, but the current discussion around creating similar, generic, helpers for kunit resumed interest in fixing this.

This can be tested using the command:
./tools/testing/kunit/kunit.py run --kunitconfig=drivers/base/test/

I added the fix David suggested back in that discussion which does fix the tests. The SoB is missing, since David didn't provide it back then.

Let me know what you think,
Maxime

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Ping?

It's in my review queue, still trying to catch up...

I didn't make it here this week, sorry.

np, I just don't want that patch to disappear into the ether :)

Quoted 2 lines

I kind of worry about encoding the current "odd" functionality in a test as being the correct thing, but will look at it closer next week.

I don't think I'm doing that? The tests we've added are all how we think it should behave, the broken ones being skipped to avoid any failures.

The last patch drops the kunit_skip() to make sure that it's tested going forward.

So we shouldn't encode the odd behaviour anywhere in this series, unless I got you wrong?

Maxime

#8 lore raw
Quoted 43 lines · Maxime Ripard

Hi,

Quoted 27 lines · Greg Kroah-Hartman
Quoted 26 lines · Greg Kroah-Hartman
Quoted 25 lines · Maxime Ripard
Quoted 24 lines · Maxime Ripard

Hi,

This follows the discussion here:
https://lore.kernel.org/linux-kselftest/20230324123157.bbwvfq4gsxnlnfwb@houat/

This shows a couple of inconsistencies with regard to how device-managed resources are cleaned up. Basically, devm resources will only be cleaned up if the device is attached to a bus and bound to a driver. Failing any of these cases, a call to device_unregister will not end up in the devm resources being released.

We had to work around it in DRM to provide helpers to create a device for kunit tests, but the current discussion around creating similar, generic, helpers for kunit resumed interest in fixing this.

This can be tested using the command:
./tools/testing/kunit/kunit.py run --kunitconfig=drivers/base/test/

I added the fix David suggested back in that discussion which does fix the tests. The SoB is missing, since David didn't provide it back then.

Let me know what you think,
Maxime

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Ping?

It's in my review queue, still trying to catch up...

I didn't make it here this week, sorry.

np, I just don't want that patch to disappear into the ether :)

Quoted 2 lines

I kind of worry about encoding the current "odd" functionality in a test as being the correct thing, but will look at it closer next week.

Quoted 8 lines

I don't think I'm doing that? The tests we've added are all how we think it should behave, the broken ones being skipped to avoid any failures.

The last patch drops the kunit_skip() to make sure that it's tested going forward.

So we shouldn't encode the odd behaviour anywhere in this series, unless I got you wrong?

No you are correct, I was mis-remembering things.

This looks good, thanks for sticking with it, all now applied to my tree.

greg k-h