drivers: base: Add tests showing devm handling inconsistencies
Subsystems (2)
v2 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 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: 53cdf865f90ba922a854c65ed05b519f9d728424
change-id: 20230329-kunit-devm-inconsistencies-test-5e5a7d01e60d
Best regards,
Signature · 1 line
Maxime Ripard <[email protected]>
PATCH v2 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.
--- 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
@@ -0,0 +1,2 @@ +CONFIG_KUNIT=y +CONFIG_DM_KUNIT_TEST=y
drivers/base/test/Kconfig +4 -0
@@ -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
@@ -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.40.0
Re: [PATCH v2 1/3] drivers: base: Add basic devm tests for root devices
Quoted 7 lines · Maxime Ripard
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.
Signed-off-by Maxime Ripard <[email protected]>---
There's definitely an argument that root devices are not supposed to be like regular devices, and so devm_ managed resources aren't supposed to work with them. Either way:
- It needs to be documented somewhere (and this test makes for good documentation, IMO).
- It should be consistent: if devm_ isn't to be used with root devices, it should fail everywhere, and if it is, it should work in all the cases below.
So I'm all in favour of including this test and making root devices work.
That being said, I am planning to send out a patchset adding a struct kunit_device for use in tests, which will be attached to a "kunit" bus. I think the combination of "fix devm_ with root devices" and "don't recommend root devices as a 'fake' device for testing" is probably the longer-term solution everyone can agree upon?
Cheers,
-- David
Quoted 19 lines
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@@ -0,0 +1,2 @@ +CONFIG_KUNIT=y +CONFIG_DM_KUNIT_TEST=ydrivers/base/test/Kconfig +0
-0
drivers/base/test/Kconfig config TEST_ASYNC_DRIVER_PROBE · line 14
Quoted 6 lines
drivers/base/test/Kconfig +3
-0@@ -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
Could we add "default KUNIT_ALL_TESTS" here? Or if there's a good reason to not make this run by default, remove the "if !KUNIT_ALL_TESTS" condition above.
Quoted 136 lines
drivers/base/test/Kconfig +1
-0+ config DRIVER_PE_KUNIT_TEST bool "KUnit Tests for property entry API" if !KUNIT_ALL_TESTS depends on KUNIT=ydrivers/base/test/Makefile +2
-0@@ -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
-1@@ -0,0 +1,110 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright 2023 Maxime Ripard <[email protected]> + +#include <kunit/resource.h> + +#include <linux/device.h> + +#define DEVICE_NAME "test" + +struct test_priv { +bool probe_done; +bool release_done; +wait_queue_head_t release_wq; +struct device *dev; +}; + +static int root_device_devm_init (struct kunit *test) +{ +struct test_priv *priv; + + priv =kunit_kzalloc (test,sizeof (*priv),GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, priv); +init_waitqueue_head (&priv->release_wq); + + test->priv = priv; + +return 0 ; +} + +static void devm_device_action (void *ptr) +{ +struct test_priv *priv = ptr; + + priv->release_done = true; +wake_up_interruptible (&priv->release_wq); +} + +#define RELEASE_TIMEOUT_MS 100 + +/* +* Tests that a bus-less, non-probed device will run its device-managed +* actions when unregistered. +*/ +static void root_device_devm_register_unregister_test (struct kunit *test) +{ +struct test_priv *priv = test->priv; +int ret; + + priv->dev =root_device_register (DEVICE_NAME ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, priv->dev); + + ret =devm_add_action_or_reset (priv->dev, devm_device_action, priv); +KUNIT_ASSERT_EQ (test, ret,0 ); + +root_device_unregister (priv->dev); + + ret =wait_event_interruptible_timeout (priv->release_wq, priv->release_done, +msecs_to_jiffies (RELEASE_TIMEOUT_MS )); +KUNIT_EXPECT_GT (test, ret,0 ); +} + +static void devm_put_device_action (void *ptr) +{ +struct test_priv *priv = ptr; + +put_device (priv->dev); + priv->release_done = true; +wake_up_interruptible (&priv->release_wq); +} + +/* +* Tests that a bus-less, non-probed device will run its device-managed +* actions when unregistered, even if someone still holds a reference to +* it. +*/ +static void root_device_devm_register_get_unregister_with_devm_test (struct kunit *test) +{ +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); + +get_device (priv->dev); + + ret =devm_add_action_or_reset (priv->dev, devm_put_device_action, priv); +KUNIT_ASSERT_EQ (test, ret,0 ); + +root_device_unregister (priv->dev); + + ret =wait_event_interruptible_timeout (priv->release_wq, priv->release_done, +msecs_to_jiffies (RELEASE_TIMEOUT_MS )); +KUNIT_EXPECT_GT (test, ret,0 ); +} + +static struct kunit_case root_device_devm_tests[] = { +KUNIT_CASE (root_device_devm_register_unregister_test), +KUNIT_CASE (root_device_devm_register_get_unregister_with_devm_test), + {} +}; + +static struct kunit_suite root_device_devm_test_suite = { + .name ="root-device-devm" , + .init = root_device_devm_init, + .test_cases = root_device_devm_tests, +}; + +kunit_test_suite (root_device_devm_test_suite); --2.40.0
Re: [PATCH v2 1/3] drivers: base: Add basic devm tests for root devices
Hi David,
Quoted 10 lines · David Gow
Quoted 7 lines · Maxime Ripard
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.
Signed-off-by Maxime Ripard <[email protected]>---
Reviewed-by David Gow <[email protected]>
Thanks!
Quoted 10 lines
There's definitely an argument that root devices are not supposed to be like regular devices, and so
devm_managed resources aren't supposed to work with them. Either way:
- It needs to be documented somewhere (and this test makes for good documentation, IMO).
- It should be consistent: ifdevm_isn't to be used with root devices, it should fail everywhere, and if it is, it should work in all the cases below.So I'm all in favour of including this test and making root devices work.
I agree 100%. I've reworded the commit log a bit to make it clearer that's what we should strive for, and that this is what this patch is doing.
Quoted 5 lines
That being said, I am planning to send out a patchset adding a struct
kunit_devicefor use in tests, which will be attached to a "kunit" bus. I think the combination of "fixdevm_with root devices" and "don't recommend root devices as a 'fake' device for testing" is probably the longer-term solution everyone can agree upon?
Yeah, that sounds reasonable to me
Maxime
PATCH v2 2/3 drivers: base: Add basic devm tests for platform devices
In reply to Maxime RipardPlatform devices show some inconsistencies with how devm resources are released when the device has been probed and when it hasn't. Let's add a few tests to exercise thos paths and odd cases.
--- drivers/base/test/Makefile | 1 + drivers/base/test/platform-device-test.c | 222 +++++++++++++++++++++++++++++++ 2 files changed, 223 insertions(+)
drivers/base/test/Makefile +1 -0
@@ -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.40.0
Re: [PATCH v2 2/3] drivers: base: Add basic devm tests for platform devices
Quoted 4 lines · Maxime Ripard
Platform devices show some inconsistencies with how devm resources are released when the device has been probed and when it hasn't. Let's add a few tests to exercise thos paths and odd cases.
Nit: "these".
Also, it'd be nice to call out the case that fails explicitly in the commit message here, so it's obvious what the "inconsistency" is.
Quoted 3 lines
Signed-off-by Maxime Ripard <[email protected]>---
This looks good to me. I think this is, if anything, even more obviously important than the root device issues, so we definitely need to fix or document it.
Quoted 248 lines
drivers/base/test/Makefile | 1 + drivers/base/test/platform-device-test.c | 222 +++++++++++++++++++++++++++++++ 2 files changed, 223 insertions(+)drivers/base/test/Makefile +1
-0@@ -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
-1@@ -0,0 +1,222 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <kunit/resource.h> + +#include <linux/device.h> +#include <linux/platform_device.h> + +#define DEVICE_NAME "test" + +struct test_priv { +bool probe_done; +bool release_done; +wait_queue_head_t probe_wq; +wait_queue_head_t release_wq; +struct device *dev; +}; + +static int platform_device_devm_init (struct kunit *test) +{ +struct test_priv *priv; + + priv =kunit_kzalloc (test,sizeof (*priv),GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, priv); +init_waitqueue_head (&priv->probe_wq); +init_waitqueue_head (&priv->release_wq); + + test->priv = priv; + +return 0 ; +} + +static void devm_device_action (void *ptr) +{ +struct test_priv *priv = ptr; + + priv->release_done = true; +wake_up_interruptible (&priv->release_wq); +} + +static void devm_put_device_action (void *ptr) +{ +struct test_priv *priv = ptr; + +put_device (priv->dev); + priv->release_done = true; +wake_up_interruptible (&priv->release_wq); +} + +#define RELEASE_TIMEOUT_MS 100 + +/* +* Tests that a platform bus, non-probed device will run its +* device-managed actions when unregistered. +*/ +static void platform_device_devm_register_unregister_test (struct kunit *test) +{ +struct platform_device *pdev; +struct test_priv *priv = test->priv; +int ret; + + pdev =platform_device_alloc (DEVICE_NAME ,PLATFORM_DEVID_NONE ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, pdev); + + ret =platform_device_add (pdev); +KUNIT_ASSERT_EQ (test, ret,0 ); + + priv->dev = &pdev->dev; + + ret =devm_add_action_or_reset (priv->dev, devm_device_action, priv); +KUNIT_ASSERT_EQ (test, ret,0 ); + +platform_device_unregister (pdev); + + ret =wait_event_interruptible_timeout (priv->release_wq, priv->release_done, +msecs_to_jiffies (RELEASE_TIMEOUT_MS )); +KUNIT_EXPECT_GT (test, ret,0 ); +} + +/* +* Tests that a platform bus, non-probed device will run its +* device-managed actions when unregistered, even if someone still holds +* a reference to it. +*/ +static void platform_device_devm_register_get_unregister_with_devm_test (struct kunit *test) +{ +struct platform_device *pdev; +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); + + ret =platform_device_add (pdev); +KUNIT_ASSERT_EQ (test, ret,0 ); + + priv->dev = &pdev->dev; + +get_device (priv->dev); + + ret =devm_add_action_or_reset (priv->dev, devm_put_device_action, priv); +KUNIT_ASSERT_EQ (test, ret,0 ); + +platform_device_unregister (pdev); + + ret =wait_event_interruptible_timeout (priv->release_wq, priv->release_done, +msecs_to_jiffies (RELEASE_TIMEOUT_MS )); +KUNIT_EXPECT_GT (test, ret,0 ); +} + +static int fake_probe (struct platform_device *pdev) +{ +struct test_priv *priv =platform_get_drvdata (pdev); + + priv->probe_done = true; +wake_up_interruptible (&priv->probe_wq); + +return 0 ; +} + +static struct platform_driver fake_driver = { + .probe = fake_probe, + .driver = { + .name =DEVICE_NAME , + }, +}; + +/* +* Tests that a platform bus, probed device will run its device-managed +* actions when unregistered. +*/ +static void probed_platform_device_devm_register_unregister_test (struct kunit *test) +{ +struct platform_device *pdev; +struct test_priv *priv = test->priv; +int ret; + + ret =platform_driver_register (&fake_driver); +KUNIT_ASSERT_EQ (test, ret,0 ); + + pdev =platform_device_alloc (DEVICE_NAME ,PLATFORM_DEVID_NONE ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, pdev); + + priv->dev = &pdev->dev; +platform_set_drvdata (pdev, priv); + + ret =platform_device_add (pdev); +KUNIT_ASSERT_EQ (test, ret,0 ); + + ret =wait_event_interruptible_timeout (priv->probe_wq, priv->probe_done, +msecs_to_jiffies (RELEASE_TIMEOUT_MS )); +KUNIT_ASSERT_GT (test, ret,0 ); + + ret =devm_add_action_or_reset (priv->dev, devm_device_action, priv); +KUNIT_ASSERT_EQ (test, ret,0 ); + +platform_device_unregister (pdev); + + ret =wait_event_interruptible_timeout (priv->release_wq, priv->release_done, +msecs_to_jiffies (RELEASE_TIMEOUT_MS )); +KUNIT_EXPECT_GT (test, ret,0 ); + +platform_driver_unregister (&fake_driver); +} + +/* +* Tests that a platform bus, probed device will run its device-managed +* actions when unregistered, even if someone still holds a reference to +* it. +*/ +static void probed_platform_device_devm_register_get_unregister_with_devm_test (struct kunit *test) +{ +struct platform_device *pdev; +struct test_priv *priv = test->priv; +int ret; + + ret =platform_driver_register (&fake_driver); +KUNIT_ASSERT_EQ (test, ret,0 ); + + pdev =platform_device_alloc (DEVICE_NAME ,PLATFORM_DEVID_NONE ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, pdev); + + priv->dev = &pdev->dev; +platform_set_drvdata (pdev, priv); + + ret =platform_device_add (pdev); +KUNIT_ASSERT_EQ (test, ret,0 ); + + ret =wait_event_interruptible_timeout (priv->probe_wq, priv->probe_done, +msecs_to_jiffies (RELEASE_TIMEOUT_MS )); +KUNIT_ASSERT_GT (test, ret,0 ); + +get_device (priv->dev); + + ret =devm_add_action_or_reset (priv->dev, devm_put_device_action, priv); +KUNIT_ASSERT_EQ (test, ret,0 ); + +platform_device_unregister (pdev); + + ret =wait_event_interruptible_timeout (priv->release_wq, priv->release_done, +msecs_to_jiffies (RELEASE_TIMEOUT_MS )); +KUNIT_EXPECT_GT (test, ret,0 ); + +platform_driver_unregister (&fake_driver); +} + +static struct kunit_case platform_device_devm_tests[] = { +KUNIT_CASE (platform_device_devm_register_unregister_test), +KUNIT_CASE (platform_device_devm_register_get_unregister_with_devm_test), +KUNIT_CASE (probed_platform_device_devm_register_unregister_test), +KUNIT_CASE (probed_platform_device_devm_register_get_unregister_with_devm_test), + {} +}; + +static struct kunit_suite platform_device_devm_test_suite = { + .name ="platform-device-devm" , + .init = platform_device_devm_init, + .test_cases = platform_device_devm_tests, +}; + +kunit_test_suite (platform_device_devm_test_suite); --2.40.0
Re: [PATCH v2 2/3] drivers: base: Add basic devm tests for platform devices
Hi
Quoted 10 lines · David Gow
Quoted 4 lines · Maxime Ripard
Platform devices show some inconsistencies with how devm resources are released when the device has been probed and when it hasn't. Let's add a few tests to exercise thos paths and odd cases.
Nit: "these".
Also, it'd be nice to call out the case that fails explicitly in the commit message here, so it's obvious what the "inconsistency" is.
I've reworded the commit message.
Quoted 9 lines
Quoted 3 lines
Signed-off-by Maxime Ripard <[email protected]>---
This looks good to me. I think this is, if anything, even more obviously important than the root device issues, so we definitely need to fix or document it.
Reviewed-by David Gow <[email protected]>
Thanks!
Maxime
PATCH v2 3/3 drivers: base: Free devm resources when unregistering a device
In reply to Maxime RipardFrom: 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 use-after-free concerns.
It's not clear whether those concerns are legitimate though, but I would expect drivers not to register new resources in their device-managed actions.
--- 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
@@ -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
@@ -87,8 +87,6 @@ static void platform_device_devm_register_get_unregister_with_devm_test(struct kstruct 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
@@ -78,8 +78,6 @@ static void root_device_devm_register_get_unregister_with_devm_test(struct kunitstruct 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.40.0
Re: [PATCH v2 3/3] drivers: base: Free devm resources when unregistering a device
Quoted 14 lines · 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 thedevice_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.
Quoted 8 lines
This is effectively the same change than commit
2f8d16a996da("devres:
release resources ondevice_del()") that got reverted by commita525a3ddeaca("driver core: free devres indevice_release") over use-after-free concerns.It's not clear whether those concerns are legitimate though, but I would expect drivers not to register new resources in their device-managed actions.
It might be clearer to notice that this patch effectively combines the two patches above, freeing _both_ on device_del() and device_release(). This should give us the best of both worlds. I'm not aware of a use-after-free issue that could result here, though it's possible there's a double free I'm missing now that we are freeing things twice. My understanding is that commit a525a3ddeaca ("driver
core: free devres in device_release") was more to avoid a leak than a use-after-free, but I could be wrong.
Quoted 3 lines
Fixes a525a3ddeaca ("driver core: free devres in device_release")Signed-off-by Maxime Ripard <[email protected]>---
Personally, I feel that this is the right way to go, but I'm definitely not an expert, so I'll let someone else review it in case there's something I'm missing.
Cheers,
-- David
Quoted 57 lines
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@@ -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@@ -87,8 +87,6 @@ static void platform_device_devm_register_get_unregister_with_devm_test(struct kstruct 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
-3@@ -78,8 +78,6 @@ static void root_device_devm_register_get_unregister_with_devm_test(struct kunitstruct 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); --2.40.0
Re: [PATCH v2 3/3] drivers: base: Free devm resources when unregistering a device
Quoted 20 lines · David Gow
Quoted 19 lines · 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 thedevice_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 ondevice_del()") that got reverted by commita525a3ddeaca("driver core: free devres indevice_release") over use-after-free concerns.
Quoted 7 lines
Quoted 3 lines
It's not clear whether those concerns are legitimate though, but I would expect drivers not to register new resources in their device-managed actions.
It might be clearer to notice that this patch effectively combines the two patches above, freeing _both_ on
device_del()anddevice_release(). This should give us the best of both worlds.
You're right I'll add that part to the commit log.
Quoted 5 lines
I'm not aware of a use-after-free issue that could result here, though it's possible there's a double free I'm missing now that we are freeing things twice. My understanding is that commit
a525a3ddeaca("driver core: free devres indevice_release") was more to avoid a leak than a use-after-free, but I could be wrong.
Yeah, I'm not sure where I got the UAF from. I probably misread/misremembered.
Maxime
Quoted 15 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_unregisterwill 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.
Quoted 5 lines
This can be tested using the command:
./tools/testing/kunit/kunit.pyrun --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.
Ping?
Maxime
Quoted 19 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_unregisterwill 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.pyrun --kunitconfig=drivers/base/test/
Quoted 8 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,
MaximeSigned-off-by Maxime Ripard <[email protected]>---
Whoops, sorry. This managed to hide in my inbox. I think it looks good, and am happy for you to add by SoB to the patch.
I've sent out reviews to the others with some small nitpicky things.
Thanks a bunch,
-- David
Quoted 29 lines
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 deviceMaxime Ripard (2):
drivers: base: Add basic devm tests for root devices
drivers: base: Add basic devm tests for platform devicesdrivers/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:53cdf865f90ba922a854c65ed05b519f9d728424
change-id: 20230329-kunit-devm-inconsistencies-test-5e5a7d01e60dBest regards,
--
Maxime Ripard <[email protected]>