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 (dev_to_node(dev) != numa_node_id()) {
48 dev_warn(dev, "NUMA node mismatch %d != %d\n",
49 dev_to_node(dev), numa_node_id());
50 atomic_inc(&warnings);
53 atomic_inc(&async_completed);
59 static struct platform_driver async_driver = {
61 .name = "test_async_driver",
62 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
67 static struct platform_driver sync_driver = {
69 .name = "test_sync_driver",
70 .probe_type = PROBE_FORCE_SYNCHRONOUS,
75 static struct platform_device *async_dev[NR_CPUS * 2];
76 static struct platform_device *sync_dev[2];
78 static struct platform_device *
79 test_platform_device_register_node(char *name, int id, int nid)
81 struct platform_device *pdev;
84 pdev = platform_device_alloc(name, id);
88 if (nid != NUMA_NO_NODE)
89 set_dev_node(&pdev->dev, nid);
91 ret = platform_device_add(pdev);
93 platform_device_put(pdev);
101 static int __init test_async_probe_init(void)
103 struct platform_device **pdev = NULL;
104 int async_id = 0, sync_id = 0;
105 unsigned long long duration;
106 ktime_t calltime, delta;
109 pr_info("registering first set of asynchronous devices...\n");
111 for_each_online_cpu(cpu) {
112 nid = cpu_to_node(cpu);
113 pdev = &async_dev[async_id];
114 *pdev = test_platform_device_register_node("test_async_driver",
118 err = PTR_ERR(*pdev);
120 pr_err("failed to create async_dev: %d\n", err);
121 goto err_unregister_async_devs;
127 pr_info("registering asynchronous driver...\n");
128 calltime = ktime_get();
129 err = platform_driver_register(&async_driver);
131 pr_err("Failed to register async_driver: %d\n", err);
132 goto err_unregister_async_devs;
135 delta = ktime_sub(ktime_get(), calltime);
136 duration = (unsigned long long) ktime_to_ms(delta);
137 pr_info("registration took %lld msecs\n", duration);
138 if (duration > TEST_PROBE_THRESHOLD) {
139 pr_err("test failed: probe took too long\n");
141 goto err_unregister_async_driver;
144 pr_info("registering second set of asynchronous devices...\n");
145 calltime = ktime_get();
146 for_each_online_cpu(cpu) {
147 nid = cpu_to_node(cpu);
148 pdev = &sync_dev[sync_id];
150 *pdev = test_platform_device_register_node("test_async_driver",
154 err = PTR_ERR(*pdev);
156 pr_err("failed to create async_dev: %d\n", err);
157 goto err_unregister_async_driver;
163 delta = ktime_sub(ktime_get(), calltime);
164 duration = (unsigned long long) ktime_to_ms(delta);
165 dev_info(&(*pdev)->dev,
166 "registration took %lld msecs\n", duration);
167 if (duration > TEST_PROBE_THRESHOLD) {
168 dev_err(&(*pdev)->dev,
169 "test failed: probe took too long\n");
171 goto err_unregister_async_driver;
175 pr_info("registering first synchronous device...\n");
176 nid = cpu_to_node(cpu);
177 pdev = &sync_dev[sync_id];
179 *pdev = test_platform_device_register_node("test_sync_driver",
183 err = PTR_ERR(*pdev);
185 pr_err("failed to create sync_dev: %d\n", err);
186 goto err_unregister_async_driver;
191 pr_info("registering synchronous driver...\n");
192 calltime = ktime_get();
193 err = platform_driver_register(&sync_driver);
195 pr_err("Failed to register async_driver: %d\n", err);
196 goto err_unregister_sync_devs;
199 delta = ktime_sub(ktime_get(), calltime);
200 duration = (unsigned long long) ktime_to_ms(delta);
201 pr_info("registration took %lld msecs\n", duration);
202 if (duration < TEST_PROBE_THRESHOLD) {
203 dev_err(&(*pdev)->dev,
204 "test failed: probe was too quick\n");
206 goto err_unregister_sync_driver;
209 pr_info("registering second synchronous device...\n");
210 pdev = &sync_dev[sync_id];
211 calltime = ktime_get();
213 *pdev = test_platform_device_register_node("test_sync_driver",
217 err = PTR_ERR(*pdev);
219 pr_err("failed to create sync_dev: %d\n", err);
220 goto err_unregister_sync_driver;
225 delta = ktime_sub(ktime_get(), calltime);
226 duration = (unsigned long long) ktime_to_ms(delta);
227 dev_info(&(*pdev)->dev,
228 "registration took %lld msecs\n", duration);
229 if (duration < TEST_PROBE_THRESHOLD) {
230 dev_err(&(*pdev)->dev,
231 "test failed: probe was too quick\n");
233 goto err_unregister_sync_driver;
237 * The async events should have completed while we were taking care
238 * of the synchronous events. We will now terminate any outstanding
239 * asynchronous probe calls remaining by forcing timeout and remove
240 * the driver before we return which should force the flush of the
241 * pending asynchronous probe calls.
243 * Otherwise if they completed without errors or warnings then
244 * report successful completion.
246 if (atomic_read(&async_completed) != async_id) {
247 pr_err("async events still pending, forcing timeout\n");
248 atomic_inc(&timeout);
250 } else if (!atomic_read(&errors) && !atomic_read(&warnings)) {
251 pr_info("completed successfully\n");
255 err_unregister_sync_driver:
256 platform_driver_unregister(&sync_driver);
257 err_unregister_sync_devs:
259 platform_device_unregister(sync_dev[sync_id]);
260 err_unregister_async_driver:
261 platform_driver_unregister(&async_driver);
262 err_unregister_async_devs:
264 platform_device_unregister(async_dev[async_id]);
267 * If err is already set then count that as an additional error for
268 * the test. Otherwise we will report an invalid argument error and
269 * not count that as we should have reached here as a result of
270 * errors or warnings being reported by the probe routine.
277 pr_err("Test failed with %d errors and %d warnings\n",
278 atomic_read(&errors), atomic_read(&warnings));
282 module_init(test_async_probe_init);
284 static void __exit test_async_probe_exit(void)
288 platform_driver_unregister(&async_driver);
289 platform_driver_unregister(&sync_driver);
292 platform_device_unregister(sync_dev[id]);
296 platform_device_unregister(async_dev[id]);
298 module_exit(test_async_probe_exit);
300 MODULE_DESCRIPTION("Test module for asynchronous driver probing");
302 MODULE_LICENSE("GPL");