Skip to content

Commit 06188bc

Browse files
mripardgregkh
authored andcommitted
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. 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-1-6aa7e074f373@kernel.org Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 0559f63 commit 06188bc

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

drivers/base/test/.kunitconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_KUNIT=y
2+
CONFIG_DM_KUNIT_TEST=y

drivers/base/test/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ config TEST_ASYNC_DRIVER_PROBE
99

1010
If unsure say N.
1111

12+
config DM_KUNIT_TEST
13+
tristate "KUnit Tests for the device model" if !KUNIT_ALL_TESTS
14+
depends on KUNIT
15+
1216
config DRIVER_PE_KUNIT_TEST
1317
bool "KUnit Tests for property entry API" if !KUNIT_ALL_TESTS
1418
depends on KUNIT=y

drivers/base/test/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# SPDX-License-Identifier: GPL-2.0
22
obj-$(CONFIG_TEST_ASYNC_DRIVER_PROBE) += test_async_driver_probe.o
33

4+
obj-$(CONFIG_DM_KUNIT_TEST) += root-device-test.o
5+
46
obj-$(CONFIG_DRIVER_PE_KUNIT_TEST) += property-entry-test.o
57
CFLAGS_property-entry-test.o += $(DISABLE_STRUCTLEAK_PLUGIN)

drivers/base/test/root-device-test.c

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright 2023 Maxime Ripard <[email protected]>
3+
4+
#include <kunit/resource.h>
5+
6+
#include <linux/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 release_wq;
14+
struct device *dev;
15+
};
16+
17+
static int root_device_devm_init(struct kunit *test)
18+
{
19+
struct test_priv *priv;
20+
21+
priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
22+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv);
23+
init_waitqueue_head(&priv->release_wq);
24+
25+
test->priv = priv;
26+
27+
return 0;
28+
}
29+
30+
static void devm_device_action(void *ptr)
31+
{
32+
struct test_priv *priv = ptr;
33+
34+
priv->release_done = true;
35+
wake_up_interruptible(&priv->release_wq);
36+
}
37+
38+
#define RELEASE_TIMEOUT_MS 100
39+
40+
/*
41+
* Tests that a bus-less, non-probed device will run its device-managed
42+
* actions when unregistered.
43+
*/
44+
static void root_device_devm_register_unregister_test(struct kunit *test)
45+
{
46+
struct test_priv *priv = test->priv;
47+
int ret;
48+
49+
priv->dev = root_device_register(DEVICE_NAME);
50+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->dev);
51+
52+
ret = devm_add_action_or_reset(priv->dev, devm_device_action, priv);
53+
KUNIT_ASSERT_EQ(test, ret, 0);
54+
55+
root_device_unregister(priv->dev);
56+
57+
ret = wait_event_interruptible_timeout(priv->release_wq, priv->release_done,
58+
msecs_to_jiffies(RELEASE_TIMEOUT_MS));
59+
KUNIT_EXPECT_GT(test, ret, 0);
60+
}
61+
62+
static void devm_put_device_action(void *ptr)
63+
{
64+
struct test_priv *priv = ptr;
65+
66+
put_device(priv->dev);
67+
priv->release_done = true;
68+
wake_up_interruptible(&priv->release_wq);
69+
}
70+
71+
/*
72+
* Tests that a bus-less, non-probed device will run its device-managed
73+
* actions when unregistered, even if someone still holds a reference to
74+
* it.
75+
*/
76+
static void root_device_devm_register_get_unregister_with_devm_test(struct kunit *test)
77+
{
78+
struct test_priv *priv = test->priv;
79+
int ret;
80+
81+
kunit_skip(test, "This needs to be fixed in the core.");
82+
83+
priv->dev = root_device_register(DEVICE_NAME);
84+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->dev);
85+
86+
get_device(priv->dev);
87+
88+
ret = devm_add_action_or_reset(priv->dev, devm_put_device_action, priv);
89+
KUNIT_ASSERT_EQ(test, ret, 0);
90+
91+
root_device_unregister(priv->dev);
92+
93+
ret = wait_event_interruptible_timeout(priv->release_wq, priv->release_done,
94+
msecs_to_jiffies(RELEASE_TIMEOUT_MS));
95+
KUNIT_EXPECT_GT(test, ret, 0);
96+
}
97+
98+
static struct kunit_case root_device_devm_tests[] = {
99+
KUNIT_CASE(root_device_devm_register_unregister_test),
100+
KUNIT_CASE(root_device_devm_register_get_unregister_with_devm_test),
101+
{}
102+
};
103+
104+
static struct kunit_suite root_device_devm_test_suite = {
105+
.name = "root-device-devm",
106+
.init = root_device_devm_init,
107+
.test_cases = root_device_devm_tests,
108+
};
109+
110+
kunit_test_suite(root_device_devm_test_suite);

0 commit comments

Comments
 (0)