1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2014 Google, Inc.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/delay.h>
9 #include <linux/init.h>
10 #include <linux/hrtimer.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/time.h>
14 #include <linux/numa.h>
15 #include <linux/nodemask.h>
16 #include <linux/topology.h>
18 #define TEST_PROBE_DELAY (5 * 1000) /* 5 sec */
19 #define TEST_PROBE_THRESHOLD (TEST_PROBE_DELAY / 2)
21 static atomic_t warnings
, errors
, timeout
, async_completed
;
23 static int test_probe(struct platform_device
*pdev
)
25 struct device
*dev
= &pdev
->dev
;
28 * Determine if we have hit the "timeout" limit for the test if we
29 * have then report it as an error, otherwise we wil sleep for the
30 * required amount of time and then report completion.
32 if (atomic_read(&timeout
)) {
33 dev_err(dev
, "async probe took too long\n");
36 dev_dbg(&pdev
->dev
, "sleeping for %d msecs in probe\n",
38 msleep(TEST_PROBE_DELAY
);
39 dev_dbg(&pdev
->dev
, "done sleeping\n");
43 * Report NUMA mismatch if device node is set and we are not
44 * performing an async init on that node.
46 if (dev
->driver
->probe_type
== PROBE_PREFER_ASYNCHRONOUS
) {
47 if (IS_ENABLED(CONFIG_NUMA
) &&
48 dev_to_node(dev
) != numa_node_id()) {
49 dev_warn(dev
, "NUMA node mismatch %d != %d\n",
50 dev_to_node(dev
), numa_node_id());
51 atomic_inc(&warnings
);
54 atomic_inc(&async_completed
);
60 static struct platform_driver async_driver
= {
62 .name
= "test_async_driver",
63 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
68 static struct platform_driver sync_driver
= {
70 .name
= "test_sync_driver",
71 .probe_type
= PROBE_FORCE_SYNCHRONOUS
,
76 static struct platform_device
*async_dev
[NR_CPUS
* 2];
77 static struct platform_device
*sync_dev
[2];
79 static struct platform_device
*
80 test_platform_device_register_node(char *name
, int id
, int nid
)
82 struct platform_device
*pdev
;
85 pdev
= platform_device_alloc(name
, id
);
89 if (nid
!= NUMA_NO_NODE
)
90 set_dev_node(&pdev
->dev
, nid
);
92 ret
= platform_device_add(pdev
);
94 platform_device_put(pdev
);
102 static int __init
test_async_probe_init(void)
104 struct platform_device
**pdev
= NULL
;
105 int async_id
= 0, sync_id
= 0;
106 unsigned long long duration
;
107 ktime_t calltime
, delta
;
110 pr_info("registering first set of asynchronous devices...\n");
112 for_each_online_cpu(cpu
) {
113 nid
= cpu_to_node(cpu
);
114 pdev
= &async_dev
[async_id
];
115 *pdev
= test_platform_device_register_node("test_async_driver",
119 err
= PTR_ERR(*pdev
);
121 pr_err("failed to create async_dev: %d\n", err
);
122 goto err_unregister_async_devs
;
128 pr_info("registering asynchronous driver...\n");
129 calltime
= ktime_get();
130 err
= platform_driver_register(&async_driver
);
132 pr_err("Failed to register async_driver: %d\n", err
);
133 goto err_unregister_async_devs
;
136 delta
= ktime_sub(ktime_get(), calltime
);
137 duration
= (unsigned long long) ktime_to_ms(delta
);
138 pr_info("registration took %lld msecs\n", duration
);
139 if (duration
> TEST_PROBE_THRESHOLD
) {
140 pr_err("test failed: probe took too long\n");
142 goto err_unregister_async_driver
;
145 pr_info("registering second set of asynchronous devices...\n");
146 calltime
= ktime_get();
147 for_each_online_cpu(cpu
) {
148 nid
= cpu_to_node(cpu
);
149 pdev
= &sync_dev
[sync_id
];
151 *pdev
= test_platform_device_register_node("test_async_driver",
155 err
= PTR_ERR(*pdev
);
157 pr_err("failed to create async_dev: %d\n", err
);
158 goto err_unregister_async_driver
;
164 delta
= ktime_sub(ktime_get(), calltime
);
165 duration
= (unsigned long long) ktime_to_ms(delta
);
166 dev_info(&(*pdev
)->dev
,
167 "registration took %lld msecs\n", duration
);
168 if (duration
> TEST_PROBE_THRESHOLD
) {
169 dev_err(&(*pdev
)->dev
,
170 "test failed: probe took too long\n");
172 goto err_unregister_async_driver
;
176 pr_info("registering first synchronous device...\n");
177 nid
= cpu_to_node(cpu
);
178 pdev
= &sync_dev
[sync_id
];
180 *pdev
= test_platform_device_register_node("test_sync_driver",
184 err
= PTR_ERR(*pdev
);
186 pr_err("failed to create sync_dev: %d\n", err
);
187 goto err_unregister_async_driver
;
192 pr_info("registering synchronous driver...\n");
193 calltime
= ktime_get();
194 err
= platform_driver_register(&sync_driver
);
196 pr_err("Failed to register async_driver: %d\n", err
);
197 goto err_unregister_sync_devs
;
200 delta
= ktime_sub(ktime_get(), calltime
);
201 duration
= (unsigned long long) ktime_to_ms(delta
);
202 pr_info("registration took %lld msecs\n", duration
);
203 if (duration
< TEST_PROBE_THRESHOLD
) {
204 dev_err(&(*pdev
)->dev
,
205 "test failed: probe was too quick\n");
207 goto err_unregister_sync_driver
;
210 pr_info("registering second synchronous device...\n");
211 pdev
= &sync_dev
[sync_id
];
212 calltime
= ktime_get();
214 *pdev
= test_platform_device_register_node("test_sync_driver",
218 err
= PTR_ERR(*pdev
);
220 pr_err("failed to create sync_dev: %d\n", err
);
221 goto err_unregister_sync_driver
;
226 delta
= ktime_sub(ktime_get(), calltime
);
227 duration
= (unsigned long long) ktime_to_ms(delta
);
228 dev_info(&(*pdev
)->dev
,
229 "registration took %lld msecs\n", duration
);
230 if (duration
< TEST_PROBE_THRESHOLD
) {
231 dev_err(&(*pdev
)->dev
,
232 "test failed: probe was too quick\n");
234 goto err_unregister_sync_driver
;
238 * The async events should have completed while we were taking care
239 * of the synchronous events. We will now terminate any outstanding
240 * asynchronous probe calls remaining by forcing timeout and remove
241 * the driver before we return which should force the flush of the
242 * pending asynchronous probe calls.
244 * Otherwise if they completed without errors or warnings then
245 * report successful completion.
247 if (atomic_read(&async_completed
) != async_id
) {
248 pr_err("async events still pending, forcing timeout\n");
249 atomic_inc(&timeout
);
251 } else if (!atomic_read(&errors
) && !atomic_read(&warnings
)) {
252 pr_info("completed successfully\n");
256 err_unregister_sync_driver
:
257 platform_driver_unregister(&sync_driver
);
258 err_unregister_sync_devs
:
260 platform_device_unregister(sync_dev
[sync_id
]);
261 err_unregister_async_driver
:
262 platform_driver_unregister(&async_driver
);
263 err_unregister_async_devs
:
265 platform_device_unregister(async_dev
[async_id
]);
268 * If err is already set then count that as an additional error for
269 * the test. Otherwise we will report an invalid argument error and
270 * not count that as we should have reached here as a result of
271 * errors or warnings being reported by the probe routine.
278 pr_err("Test failed with %d errors and %d warnings\n",
279 atomic_read(&errors
), atomic_read(&warnings
));
283 module_init(test_async_probe_init
);
285 static void __exit
test_async_probe_exit(void)
289 platform_driver_unregister(&async_driver
);
290 platform_driver_unregister(&sync_driver
);
293 platform_device_unregister(sync_dev
[id
]);
297 platform_device_unregister(async_dev
[id
]);
299 module_exit(test_async_probe_exit
);
301 MODULE_DESCRIPTION("Test module for asynchronous driver probing");
302 MODULE_AUTHOR("Dmitry Torokhov <dtor@chromium.org>");
303 MODULE_LICENSE("GPL");