.. | .. |
---|
11 | 11 | #include <linux/module.h> |
---|
12 | 12 | #include <linux/platform_device.h> |
---|
13 | 13 | #include <linux/time.h> |
---|
| 14 | +#include <linux/numa.h> |
---|
| 15 | +#include <linux/nodemask.h> |
---|
| 16 | +#include <linux/topology.h> |
---|
14 | 17 | |
---|
15 | 18 | #define TEST_PROBE_DELAY (5 * 1000) /* 5 sec */ |
---|
16 | 19 | #define TEST_PROBE_THRESHOLD (TEST_PROBE_DELAY / 2) |
---|
17 | 20 | |
---|
| 21 | +static atomic_t warnings, errors, timeout, async_completed; |
---|
| 22 | + |
---|
18 | 23 | static int test_probe(struct platform_device *pdev) |
---|
19 | 24 | { |
---|
20 | | - dev_info(&pdev->dev, "sleeping for %d msecs in probe\n", |
---|
21 | | - TEST_PROBE_DELAY); |
---|
22 | | - msleep(TEST_PROBE_DELAY); |
---|
23 | | - dev_info(&pdev->dev, "done sleeping\n"); |
---|
| 25 | + struct device *dev = &pdev->dev; |
---|
| 26 | + |
---|
| 27 | + /* |
---|
| 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. |
---|
| 31 | + */ |
---|
| 32 | + if (atomic_read(&timeout)) { |
---|
| 33 | + dev_err(dev, "async probe took too long\n"); |
---|
| 34 | + atomic_inc(&errors); |
---|
| 35 | + } else { |
---|
| 36 | + dev_dbg(&pdev->dev, "sleeping for %d msecs in probe\n", |
---|
| 37 | + TEST_PROBE_DELAY); |
---|
| 38 | + msleep(TEST_PROBE_DELAY); |
---|
| 39 | + dev_dbg(&pdev->dev, "done sleeping\n"); |
---|
| 40 | + } |
---|
| 41 | + |
---|
| 42 | + /* |
---|
| 43 | + * Report NUMA mismatch if device node is set and we are not |
---|
| 44 | + * performing an async init on that node. |
---|
| 45 | + */ |
---|
| 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); |
---|
| 52 | + } |
---|
| 53 | + |
---|
| 54 | + atomic_inc(&async_completed); |
---|
| 55 | + } |
---|
24 | 56 | |
---|
25 | 57 | return 0; |
---|
26 | 58 | } |
---|
.. | .. |
---|
41 | 73 | .probe = test_probe, |
---|
42 | 74 | }; |
---|
43 | 75 | |
---|
44 | | -static struct platform_device *async_dev_1, *async_dev_2; |
---|
45 | | -static struct platform_device *sync_dev_1; |
---|
| 76 | +static struct platform_device *async_dev[NR_CPUS * 2]; |
---|
| 77 | +static struct platform_device *sync_dev[2]; |
---|
| 78 | + |
---|
| 79 | +static struct platform_device * |
---|
| 80 | +test_platform_device_register_node(char *name, int id, int nid) |
---|
| 81 | +{ |
---|
| 82 | + struct platform_device *pdev; |
---|
| 83 | + int ret; |
---|
| 84 | + |
---|
| 85 | + pdev = platform_device_alloc(name, id); |
---|
| 86 | + if (!pdev) |
---|
| 87 | + return ERR_PTR(-ENOMEM); |
---|
| 88 | + |
---|
| 89 | + if (nid != NUMA_NO_NODE) |
---|
| 90 | + set_dev_node(&pdev->dev, nid); |
---|
| 91 | + |
---|
| 92 | + ret = platform_device_add(pdev); |
---|
| 93 | + if (ret) { |
---|
| 94 | + platform_device_put(pdev); |
---|
| 95 | + return ERR_PTR(ret); |
---|
| 96 | + } |
---|
| 97 | + |
---|
| 98 | + return pdev; |
---|
| 99 | + |
---|
| 100 | +} |
---|
46 | 101 | |
---|
47 | 102 | static int __init test_async_probe_init(void) |
---|
48 | 103 | { |
---|
49 | | - ktime_t calltime, delta; |
---|
| 104 | + struct platform_device **pdev = NULL; |
---|
| 105 | + int async_id = 0, sync_id = 0; |
---|
50 | 106 | unsigned long long duration; |
---|
51 | | - int error; |
---|
| 107 | + ktime_t calltime, delta; |
---|
| 108 | + int err, nid, cpu; |
---|
52 | 109 | |
---|
53 | | - pr_info("registering first asynchronous device...\n"); |
---|
| 110 | + pr_info("registering first set of asynchronous devices...\n"); |
---|
54 | 111 | |
---|
55 | | - async_dev_1 = platform_device_register_simple("test_async_driver", 1, |
---|
56 | | - NULL, 0); |
---|
57 | | - if (IS_ERR(async_dev_1)) { |
---|
58 | | - error = PTR_ERR(async_dev_1); |
---|
59 | | - pr_err("failed to create async_dev_1: %d\n", error); |
---|
60 | | - return error; |
---|
| 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", |
---|
| 116 | + async_id, |
---|
| 117 | + nid); |
---|
| 118 | + if (IS_ERR(*pdev)) { |
---|
| 119 | + err = PTR_ERR(*pdev); |
---|
| 120 | + *pdev = NULL; |
---|
| 121 | + pr_err("failed to create async_dev: %d\n", err); |
---|
| 122 | + goto err_unregister_async_devs; |
---|
| 123 | + } |
---|
| 124 | + |
---|
| 125 | + async_id++; |
---|
61 | 126 | } |
---|
62 | 127 | |
---|
63 | 128 | pr_info("registering asynchronous driver...\n"); |
---|
64 | 129 | calltime = ktime_get(); |
---|
65 | | - error = platform_driver_register(&async_driver); |
---|
66 | | - if (error) { |
---|
67 | | - pr_err("Failed to register async_driver: %d\n", error); |
---|
68 | | - goto err_unregister_async_dev_1; |
---|
| 130 | + err = platform_driver_register(&async_driver); |
---|
| 131 | + if (err) { |
---|
| 132 | + pr_err("Failed to register async_driver: %d\n", err); |
---|
| 133 | + goto err_unregister_async_devs; |
---|
69 | 134 | } |
---|
70 | 135 | |
---|
71 | 136 | delta = ktime_sub(ktime_get(), calltime); |
---|
.. | .. |
---|
73 | 138 | pr_info("registration took %lld msecs\n", duration); |
---|
74 | 139 | if (duration > TEST_PROBE_THRESHOLD) { |
---|
75 | 140 | pr_err("test failed: probe took too long\n"); |
---|
76 | | - error = -ETIMEDOUT; |
---|
| 141 | + err = -ETIMEDOUT; |
---|
77 | 142 | goto err_unregister_async_driver; |
---|
78 | 143 | } |
---|
79 | 144 | |
---|
80 | | - pr_info("registering second asynchronous device...\n"); |
---|
| 145 | + pr_info("registering second set of asynchronous devices...\n"); |
---|
81 | 146 | calltime = ktime_get(); |
---|
82 | | - async_dev_2 = platform_device_register_simple("test_async_driver", 2, |
---|
83 | | - NULL, 0); |
---|
84 | | - if (IS_ERR(async_dev_2)) { |
---|
85 | | - error = PTR_ERR(async_dev_2); |
---|
86 | | - pr_err("failed to create async_dev_2: %d\n", error); |
---|
87 | | - goto err_unregister_async_driver; |
---|
| 147 | + for_each_online_cpu(cpu) { |
---|
| 148 | + nid = cpu_to_node(cpu); |
---|
| 149 | + pdev = &async_dev[async_id]; |
---|
| 150 | + |
---|
| 151 | + *pdev = test_platform_device_register_node("test_async_driver", |
---|
| 152 | + async_id, |
---|
| 153 | + nid); |
---|
| 154 | + if (IS_ERR(*pdev)) { |
---|
| 155 | + err = PTR_ERR(*pdev); |
---|
| 156 | + *pdev = NULL; |
---|
| 157 | + pr_err("failed to create async_dev: %d\n", err); |
---|
| 158 | + goto err_unregister_async_driver; |
---|
| 159 | + } |
---|
| 160 | + |
---|
| 161 | + async_id++; |
---|
88 | 162 | } |
---|
89 | 163 | |
---|
90 | 164 | delta = ktime_sub(ktime_get(), calltime); |
---|
91 | 165 | duration = (unsigned long long) ktime_to_ms(delta); |
---|
92 | | - pr_info("registration took %lld msecs\n", duration); |
---|
| 166 | + dev_info(&(*pdev)->dev, |
---|
| 167 | + "registration took %lld msecs\n", duration); |
---|
93 | 168 | if (duration > TEST_PROBE_THRESHOLD) { |
---|
94 | | - pr_err("test failed: probe took too long\n"); |
---|
95 | | - error = -ETIMEDOUT; |
---|
96 | | - goto err_unregister_async_dev_2; |
---|
| 169 | + dev_err(&(*pdev)->dev, |
---|
| 170 | + "test failed: probe took too long\n"); |
---|
| 171 | + err = -ETIMEDOUT; |
---|
| 172 | + goto err_unregister_async_driver; |
---|
97 | 173 | } |
---|
| 174 | + |
---|
| 175 | + |
---|
| 176 | + pr_info("registering first synchronous device...\n"); |
---|
| 177 | + nid = cpu_to_node(cpu); |
---|
| 178 | + pdev = &sync_dev[sync_id]; |
---|
| 179 | + |
---|
| 180 | + *pdev = test_platform_device_register_node("test_sync_driver", |
---|
| 181 | + sync_id, |
---|
| 182 | + NUMA_NO_NODE); |
---|
| 183 | + if (IS_ERR(*pdev)) { |
---|
| 184 | + err = PTR_ERR(*pdev); |
---|
| 185 | + *pdev = NULL; |
---|
| 186 | + pr_err("failed to create sync_dev: %d\n", err); |
---|
| 187 | + goto err_unregister_async_driver; |
---|
| 188 | + } |
---|
| 189 | + |
---|
| 190 | + sync_id++; |
---|
98 | 191 | |
---|
99 | 192 | pr_info("registering synchronous driver...\n"); |
---|
100 | | - |
---|
101 | | - error = platform_driver_register(&sync_driver); |
---|
102 | | - if (error) { |
---|
103 | | - pr_err("Failed to register async_driver: %d\n", error); |
---|
104 | | - goto err_unregister_async_dev_2; |
---|
105 | | - } |
---|
106 | | - |
---|
107 | | - pr_info("registering synchronous device...\n"); |
---|
108 | 193 | calltime = ktime_get(); |
---|
109 | | - sync_dev_1 = platform_device_register_simple("test_sync_driver", 1, |
---|
110 | | - NULL, 0); |
---|
111 | | - if (IS_ERR(sync_dev_1)) { |
---|
112 | | - error = PTR_ERR(sync_dev_1); |
---|
113 | | - pr_err("failed to create sync_dev_1: %d\n", error); |
---|
114 | | - goto err_unregister_sync_driver; |
---|
| 194 | + err = platform_driver_register(&sync_driver); |
---|
| 195 | + if (err) { |
---|
| 196 | + pr_err("Failed to register async_driver: %d\n", err); |
---|
| 197 | + goto err_unregister_sync_devs; |
---|
115 | 198 | } |
---|
116 | 199 | |
---|
117 | 200 | delta = ktime_sub(ktime_get(), calltime); |
---|
118 | 201 | duration = (unsigned long long) ktime_to_ms(delta); |
---|
119 | 202 | pr_info("registration took %lld msecs\n", duration); |
---|
120 | 203 | if (duration < TEST_PROBE_THRESHOLD) { |
---|
121 | | - pr_err("test failed: probe was too quick\n"); |
---|
122 | | - error = -ETIMEDOUT; |
---|
123 | | - goto err_unregister_sync_dev_1; |
---|
| 204 | + dev_err(&(*pdev)->dev, |
---|
| 205 | + "test failed: probe was too quick\n"); |
---|
| 206 | + err = -ETIMEDOUT; |
---|
| 207 | + goto err_unregister_sync_driver; |
---|
124 | 208 | } |
---|
125 | 209 | |
---|
126 | | - pr_info("completed successfully"); |
---|
| 210 | + pr_info("registering second synchronous device...\n"); |
---|
| 211 | + pdev = &sync_dev[sync_id]; |
---|
| 212 | + calltime = ktime_get(); |
---|
127 | 213 | |
---|
128 | | - return 0; |
---|
| 214 | + *pdev = test_platform_device_register_node("test_sync_driver", |
---|
| 215 | + sync_id, |
---|
| 216 | + NUMA_NO_NODE); |
---|
| 217 | + if (IS_ERR(*pdev)) { |
---|
| 218 | + err = PTR_ERR(*pdev); |
---|
| 219 | + *pdev = NULL; |
---|
| 220 | + pr_err("failed to create sync_dev: %d\n", err); |
---|
| 221 | + goto err_unregister_sync_driver; |
---|
| 222 | + } |
---|
129 | 223 | |
---|
130 | | -err_unregister_sync_dev_1: |
---|
131 | | - platform_device_unregister(sync_dev_1); |
---|
| 224 | + sync_id++; |
---|
| 225 | + |
---|
| 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"); |
---|
| 233 | + err = -ETIMEDOUT; |
---|
| 234 | + goto err_unregister_sync_driver; |
---|
| 235 | + } |
---|
| 236 | + |
---|
| 237 | + /* |
---|
| 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. |
---|
| 243 | + * |
---|
| 244 | + * Otherwise if they completed without errors or warnings then |
---|
| 245 | + * report successful completion. |
---|
| 246 | + */ |
---|
| 247 | + if (atomic_read(&async_completed) != async_id) { |
---|
| 248 | + pr_err("async events still pending, forcing timeout\n"); |
---|
| 249 | + atomic_inc(&timeout); |
---|
| 250 | + err = -ETIMEDOUT; |
---|
| 251 | + } else if (!atomic_read(&errors) && !atomic_read(&warnings)) { |
---|
| 252 | + pr_info("completed successfully\n"); |
---|
| 253 | + return 0; |
---|
| 254 | + } |
---|
132 | 255 | |
---|
133 | 256 | err_unregister_sync_driver: |
---|
134 | 257 | platform_driver_unregister(&sync_driver); |
---|
135 | | - |
---|
136 | | -err_unregister_async_dev_2: |
---|
137 | | - platform_device_unregister(async_dev_2); |
---|
138 | | - |
---|
| 258 | +err_unregister_sync_devs: |
---|
| 259 | + while (sync_id--) |
---|
| 260 | + platform_device_unregister(sync_dev[sync_id]); |
---|
139 | 261 | err_unregister_async_driver: |
---|
140 | 262 | platform_driver_unregister(&async_driver); |
---|
| 263 | +err_unregister_async_devs: |
---|
| 264 | + while (async_id--) |
---|
| 265 | + platform_device_unregister(async_dev[async_id]); |
---|
141 | 266 | |
---|
142 | | -err_unregister_async_dev_1: |
---|
143 | | - platform_device_unregister(async_dev_1); |
---|
| 267 | + /* |
---|
| 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. |
---|
| 272 | + */ |
---|
| 273 | + if (err) |
---|
| 274 | + atomic_inc(&errors); |
---|
| 275 | + else |
---|
| 276 | + err = -EINVAL; |
---|
144 | 277 | |
---|
145 | | - return error; |
---|
| 278 | + pr_err("Test failed with %d errors and %d warnings\n", |
---|
| 279 | + atomic_read(&errors), atomic_read(&warnings)); |
---|
| 280 | + |
---|
| 281 | + return err; |
---|
146 | 282 | } |
---|
147 | 283 | module_init(test_async_probe_init); |
---|
148 | 284 | |
---|
149 | 285 | static void __exit test_async_probe_exit(void) |
---|
150 | 286 | { |
---|
| 287 | + int id = 2; |
---|
| 288 | + |
---|
151 | 289 | platform_driver_unregister(&async_driver); |
---|
152 | 290 | platform_driver_unregister(&sync_driver); |
---|
153 | | - platform_device_unregister(async_dev_1); |
---|
154 | | - platform_device_unregister(async_dev_2); |
---|
155 | | - platform_device_unregister(sync_dev_1); |
---|
| 291 | + |
---|
| 292 | + while (id--) |
---|
| 293 | + platform_device_unregister(sync_dev[id]); |
---|
| 294 | + |
---|
| 295 | + id = NR_CPUS * 2; |
---|
| 296 | + while (id--) |
---|
| 297 | + platform_device_unregister(async_dev[id]); |
---|
156 | 298 | } |
---|
157 | 299 | module_exit(test_async_probe_exit); |
---|
158 | 300 | |
---|