Skip to content

Commit b4cc443

Browse files
mripardgregkh
authored andcommitted
drivers: base: Add basic devm tests for platform devices
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. Reviewed-by: David Gow <[email protected]> Signed-off-by: Maxime Ripard <[email protected]> Link: https://lore.kernel.org/r/20230720-kunit-devm-inconsistencies-test-v3-2-6aa7e074f373@kernel.org Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 06188bc commit b4cc443

File tree

2 files changed

+223
-0
lines changed

2 files changed

+223
-0
lines changed

drivers/base/test/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
obj-$(CONFIG_TEST_ASYNC_DRIVER_PROBE) += test_async_driver_probe.o
33

44
obj-$(CONFIG_DM_KUNIT_TEST) += root-device-test.o
5+
obj-$(CONFIG_DM_KUNIT_TEST) += platform-device-test.o
56

67
obj-$(CONFIG_DRIVER_PE_KUNIT_TEST) += property-entry-test.o
78
CFLAGS_property-entry-test.o += $(DISABLE_STRUCTLEAK_PLUGIN)
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <kunit/resource.h>
4+
5+
#include <linux/device.h>
6+
#include <linux/platform_device.h>
7+
8+
#define DEVICE_NAME "test"
9+
10+
struct test_priv {
11+
bool probe_done;
12+
bool release_done;
13+
wait_queue_head_t probe_wq;
14+
wait_queue_head_t release_wq;
15+
struct device *dev;
16+
};
17+
18+
static int platform_device_devm_init(struct kunit *test)
19+
{
20+
struct test_priv *priv;
21+
22+
priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
23+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv);
24+
init_waitqueue_head(&priv->probe_wq);
25+
init_waitqueue_head(&priv->release_wq);
26+
27+
test->priv = priv;
28+
29+
return 0;
30+
}
31+
32+
static void devm_device_action(void *ptr)
33+
{
34+
struct test_priv *priv = ptr;
35+
36+
priv->release_done = true;
37+
wake_up_interruptible(&priv->release_wq);
38+
}
39+
40+
static void devm_put_device_action(void *ptr)
41+
{
42+
struct test_priv *priv = ptr;
43+
44+
put_device(priv->dev);
45+
priv->release_done = true;
46+
wake_up_interruptible(&priv->release_wq);
47+
}
48+
49+
#define RELEASE_TIMEOUT_MS 100
50+
51+
/*
52+
* Tests that a platform bus, non-probed device will run its
53+
* device-managed actions when unregistered.
54+
*/
55+
static void platform_device_devm_register_unregister_test(struct kunit *test)
56+
{
57+
struct platform_device *pdev;
58+
struct test_priv *priv = test->priv;
59+
int ret;
60+
61+
pdev = platform_device_alloc(DEVICE_NAME, PLATFORM_DEVID_NONE);
62+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
63+
64+
ret = platform_device_add(pdev);
65+
KUNIT_ASSERT_EQ(test, ret, 0);
66+
67+
priv->dev = &pdev->dev;
68+
69+
ret = devm_add_action_or_reset(priv->dev, devm_device_action, priv);
70+
KUNIT_ASSERT_EQ(test, ret, 0);
71+
72+
platform_device_unregister(pdev);
73+
74+
ret = wait_event_interruptible_timeout(priv->release_wq, priv->release_done,
75+
msecs_to_jiffies(RELEASE_TIMEOUT_MS));
76+
KUNIT_EXPECT_GT(test, ret, 0);
77+
}
78+
79+
/*
80+
* Tests that a platform bus, non-probed device will run its
81+
* device-managed actions when unregistered, even if someone still holds
82+
* a reference to it.
83+
*/
84+
static void platform_device_devm_register_get_unregister_with_devm_test(struct kunit *test)
85+
{
86+
struct platform_device *pdev;
87+
struct test_priv *priv = test->priv;
88+
int ret;
89+
90+
kunit_skip(test, "This needs to be fixed in the core.");
91+
92+
pdev = platform_device_alloc(DEVICE_NAME, PLATFORM_DEVID_NONE);
93+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
94+
95+
ret = platform_device_add(pdev);
96+
KUNIT_ASSERT_EQ(test, ret, 0);
97+
98+
priv->dev = &pdev->dev;
99+
100+
get_device(priv->dev);
101+
102+
ret = devm_add_action_or_reset(priv->dev, devm_put_device_action, priv);
103+
KUNIT_ASSERT_EQ(test, ret, 0);
104+
105+
platform_device_unregister(pdev);
106+
107+
ret = wait_event_interruptible_timeout(priv->release_wq, priv->release_done,
108+
msecs_to_jiffies(RELEASE_TIMEOUT_MS));
109+
KUNIT_EXPECT_GT(test, ret, 0);
110+
}
111+
112+
static int fake_probe(struct platform_device *pdev)
113+
{
114+
struct test_priv *priv = platform_get_drvdata(pdev);
115+
116+
priv->probe_done = true;
117+
wake_up_interruptible(&priv->probe_wq);
118+
119+
return 0;
120+
}
121+
122+
static struct platform_driver fake_driver = {
123+
.probe = fake_probe,
124+
.driver = {
125+
.name = DEVICE_NAME,
126+
},
127+
};
128+
129+
/*
130+
* Tests that a platform bus, probed device will run its device-managed
131+
* actions when unregistered.
132+
*/
133+
static void probed_platform_device_devm_register_unregister_test(struct kunit *test)
134+
{
135+
struct platform_device *pdev;
136+
struct test_priv *priv = test->priv;
137+
int ret;
138+
139+
ret = platform_driver_register(&fake_driver);
140+
KUNIT_ASSERT_EQ(test, ret, 0);
141+
142+
pdev = platform_device_alloc(DEVICE_NAME, PLATFORM_DEVID_NONE);
143+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
144+
145+
priv->dev = &pdev->dev;
146+
platform_set_drvdata(pdev, priv);
147+
148+
ret = platform_device_add(pdev);
149+
KUNIT_ASSERT_EQ(test, ret, 0);
150+
151+
ret = wait_event_interruptible_timeout(priv->probe_wq, priv->probe_done,
152+
msecs_to_jiffies(RELEASE_TIMEOUT_MS));
153+
KUNIT_ASSERT_GT(test, ret, 0);
154+
155+
ret = devm_add_action_or_reset(priv->dev, devm_device_action, priv);
156+
KUNIT_ASSERT_EQ(test, ret, 0);
157+
158+
platform_device_unregister(pdev);
159+
160+
ret = wait_event_interruptible_timeout(priv->release_wq, priv->release_done,
161+
msecs_to_jiffies(RELEASE_TIMEOUT_MS));
162+
KUNIT_EXPECT_GT(test, ret, 0);
163+
164+
platform_driver_unregister(&fake_driver);
165+
}
166+
167+
/*
168+
* Tests that a platform bus, probed device will run its device-managed
169+
* actions when unregistered, even if someone still holds a reference to
170+
* it.
171+
*/
172+
static void probed_platform_device_devm_register_get_unregister_with_devm_test(struct kunit *test)
173+
{
174+
struct platform_device *pdev;
175+
struct test_priv *priv = test->priv;
176+
int ret;
177+
178+
ret = platform_driver_register(&fake_driver);
179+
KUNIT_ASSERT_EQ(test, ret, 0);
180+
181+
pdev = platform_device_alloc(DEVICE_NAME, PLATFORM_DEVID_NONE);
182+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
183+
184+
priv->dev = &pdev->dev;
185+
platform_set_drvdata(pdev, priv);
186+
187+
ret = platform_device_add(pdev);
188+
KUNIT_ASSERT_EQ(test, ret, 0);
189+
190+
ret = wait_event_interruptible_timeout(priv->probe_wq, priv->probe_done,
191+
msecs_to_jiffies(RELEASE_TIMEOUT_MS));
192+
KUNIT_ASSERT_GT(test, ret, 0);
193+
194+
get_device(priv->dev);
195+
196+
ret = devm_add_action_or_reset(priv->dev, devm_put_device_action, priv);
197+
KUNIT_ASSERT_EQ(test, ret, 0);
198+
199+
platform_device_unregister(pdev);
200+
201+
ret = wait_event_interruptible_timeout(priv->release_wq, priv->release_done,
202+
msecs_to_jiffies(RELEASE_TIMEOUT_MS));
203+
KUNIT_EXPECT_GT(test, ret, 0);
204+
205+
platform_driver_unregister(&fake_driver);
206+
}
207+
208+
static struct kunit_case platform_device_devm_tests[] = {
209+
KUNIT_CASE(platform_device_devm_register_unregister_test),
210+
KUNIT_CASE(platform_device_devm_register_get_unregister_with_devm_test),
211+
KUNIT_CASE(probed_platform_device_devm_register_unregister_test),
212+
KUNIT_CASE(probed_platform_device_devm_register_get_unregister_with_devm_test),
213+
{}
214+
};
215+
216+
static struct kunit_suite platform_device_devm_test_suite = {
217+
.name = "platform-device-devm",
218+
.init = platform_device_devm_init,
219+
.test_cases = platform_device_devm_tests,
220+
};
221+
222+
kunit_test_suite(platform_device_devm_test_suite);

0 commit comments

Comments
 (0)