]>
Commit | Line | Data |
---|---|---|
716154c5 BB |
1 | /*****************************************************************************\ |
2 | * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC. | |
3 | * Copyright (C) 2007 The Regents of the University of California. | |
4 | * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER). | |
5 | * Written by Brian Behlendorf <behlendorf1@llnl.gov>. | |
715f6251 | 6 | * UCRL-CODE-235197 |
7 | * | |
716154c5 BB |
8 | * This file is part of the SPL, Solaris Porting Layer. |
9 | * For details, see <http://github.com/behlendorf/spl/>. | |
10 | * | |
11 | * The SPL is free software; you can redistribute it and/or modify it | |
12 | * under the terms of the GNU General Public License as published by the | |
13 | * Free Software Foundation; either version 2 of the License, or (at your | |
14 | * option) any later version. | |
715f6251 | 15 | * |
716154c5 | 16 | * The SPL is distributed in the hope that it will be useful, but WITHOUT |
715f6251 | 17 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
18 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
19 | * for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License along | |
716154c5 BB |
22 | * with the SPL. If not, see <http://www.gnu.org/licenses/>. |
23 | ***************************************************************************** | |
24 | * Solaris Porting LAyer Tests (SPLAT) Mutex Tests. | |
25 | \*****************************************************************************/ | |
715f6251 | 26 | |
7c50328b | 27 | #include "splat-internal.h" |
f1ca4da6 | 28 | |
4d54fdee BB |
29 | #define SPLAT_MUTEX_NAME "mutex" |
30 | #define SPLAT_MUTEX_DESC "Kernel Mutex Tests" | |
f1ca4da6 | 31 | |
4d54fdee BB |
32 | #define SPLAT_MUTEX_TEST1_ID 0x0401 |
33 | #define SPLAT_MUTEX_TEST1_NAME "tryenter" | |
34 | #define SPLAT_MUTEX_TEST1_DESC "Validate mutex_tryenter() correctness" | |
f1ca4da6 | 35 | |
4d54fdee BB |
36 | #define SPLAT_MUTEX_TEST2_ID 0x0402 |
37 | #define SPLAT_MUTEX_TEST2_NAME "race" | |
38 | #define SPLAT_MUTEX_TEST2_DESC "Many threads entering/exiting the mutex" | |
f1ca4da6 | 39 | |
4d54fdee BB |
40 | #define SPLAT_MUTEX_TEST3_ID 0x0403 |
41 | #define SPLAT_MUTEX_TEST3_NAME "owned" | |
42 | #define SPLAT_MUTEX_TEST3_DESC "Validate mutex_owned() correctness" | |
f1ca4da6 | 43 | |
4d54fdee BB |
44 | #define SPLAT_MUTEX_TEST4_ID 0x0404 |
45 | #define SPLAT_MUTEX_TEST4_NAME "owner" | |
46 | #define SPLAT_MUTEX_TEST4_DESC "Validate mutex_owner() correctness" | |
f1ca4da6 | 47 | |
4d54fdee BB |
48 | #define SPLAT_MUTEX_TEST_MAGIC 0x115599DDUL |
49 | #define SPLAT_MUTEX_TEST_NAME "mutex_test" | |
50 | #define SPLAT_MUTEX_TEST_TASKQ "mutex_taskq" | |
51 | #define SPLAT_MUTEX_TEST_COUNT 128 | |
f1ca4da6 | 52 | |
53 | typedef struct mutex_priv { | |
54 | unsigned long mp_magic; | |
55 | struct file *mp_file; | |
4d54fdee BB |
56 | kmutex_t mp_mtx; |
57 | int mp_rc; | |
f1ca4da6 | 58 | } mutex_priv_t; |
59 | ||
f1ca4da6 | 60 | static void |
5b5f5685 | 61 | splat_mutex_test1_func(void *arg) |
f1ca4da6 | 62 | { |
4d54fdee BB |
63 | mutex_priv_t *mp = (mutex_priv_t *)arg; |
64 | ASSERT(mp->mp_magic == SPLAT_MUTEX_TEST_MAGIC); | |
65 | ||
66 | if (mutex_tryenter(&mp->mp_mtx)) { | |
67 | mp->mp_rc = 0; | |
68 | mutex_exit(&mp->mp_mtx); | |
69 | } else { | |
70 | mp->mp_rc = -EBUSY; | |
71 | } | |
f1ca4da6 | 72 | } |
73 | ||
74 | static int | |
7c50328b | 75 | splat_mutex_test1(struct file *file, void *arg) |
f1ca4da6 | 76 | { |
4d54fdee BB |
77 | mutex_priv_t *mp; |
78 | taskq_t *tq; | |
79 | int id, rc = 0; | |
80 | ||
81 | mp = (mutex_priv_t *)kmalloc(sizeof(*mp), GFP_KERNEL); | |
82 | if (mp == NULL) | |
83 | return -ENOMEM; | |
84 | ||
85 | tq = taskq_create(SPLAT_MUTEX_TEST_TASKQ, 1, maxclsyspri, | |
86 | 50, INT_MAX, TASKQ_PREPOPULATE); | |
87 | if (tq == NULL) { | |
88 | rc = -ENOMEM; | |
89 | goto out2; | |
90 | } | |
91 | ||
92 | mp->mp_magic = SPLAT_MUTEX_TEST_MAGIC; | |
93 | mp->mp_file = file; | |
94 | mutex_init(&mp->mp_mtx, SPLAT_MUTEX_TEST_NAME, MUTEX_DEFAULT, NULL); | |
95 | mutex_enter(&mp->mp_mtx); | |
96 | ||
97 | /* | |
98 | * Schedule a task function which will try and acquire the mutex via | |
99 | * mutex_tryenter() while it's held. This should fail and the task | |
100 | * function will indicate this status in the passed private data. | |
101 | */ | |
102 | mp->mp_rc = -EINVAL; | |
103 | id = taskq_dispatch(tq, splat_mutex_test1_func, mp, TQ_SLEEP); | |
104 | if (id == 0) { | |
105 | mutex_exit(&mp->mp_mtx); | |
106 | splat_vprint(file, SPLAT_MUTEX_TEST1_NAME, "%s", | |
107 | "taskq_dispatch() failed\n"); | |
108 | rc = -EINVAL; | |
109 | goto out; | |
110 | } | |
111 | ||
112 | taskq_wait_id(tq, id); | |
113 | mutex_exit(&mp->mp_mtx); | |
114 | ||
115 | /* Task function successfully acquired mutex, very bad! */ | |
116 | if (mp->mp_rc != -EBUSY) { | |
117 | splat_vprint(file, SPLAT_MUTEX_TEST1_NAME, | |
118 | "mutex_trylock() incorrectly succeeded when " | |
119 | "the mutex was held, %d/%d\n", id, mp->mp_rc); | |
120 | rc = -EINVAL; | |
121 | goto out; | |
122 | } else { | |
123 | splat_vprint(file, SPLAT_MUTEX_TEST1_NAME, "%s", | |
124 | "mutex_trylock() correctly failed when " | |
125 | "the mutex was held\n"); | |
126 | } | |
127 | ||
128 | /* | |
129 | * Schedule a task function which will try and acquire the mutex via | |
130 | * mutex_tryenter() while it is not held. This should succeed and | |
131 | * can be verified by checking the private data. | |
132 | */ | |
133 | mp->mp_rc = -EINVAL; | |
134 | id = taskq_dispatch(tq, splat_mutex_test1_func, mp, TQ_SLEEP); | |
135 | if (id == 0) { | |
136 | splat_vprint(file, SPLAT_MUTEX_TEST1_NAME, "%s", | |
137 | "taskq_dispatch() failed\n"); | |
138 | rc = -EINVAL; | |
139 | goto out; | |
140 | } | |
141 | ||
142 | taskq_wait_id(tq, id); | |
143 | ||
144 | /* Task function failed to acquire mutex, very bad! */ | |
145 | if (mp->mp_rc != 0) { | |
146 | splat_vprint(file, SPLAT_MUTEX_TEST1_NAME, | |
147 | "mutex_trylock() incorrectly failed when " | |
148 | "the mutex was not held, %d/%d\n", id, mp->mp_rc); | |
149 | rc = -EINVAL; | |
150 | } else { | |
151 | splat_vprint(file, SPLAT_MUTEX_TEST1_NAME, "%s", | |
152 | "mutex_trylock() correctly succeeded " | |
153 | "when the mutex was not held\n"); | |
154 | } | |
f1ca4da6 | 155 | out: |
4d54fdee BB |
156 | taskq_destroy(tq); |
157 | mutex_destroy(&(mp->mp_mtx)); | |
f1ca4da6 | 158 | out2: |
4d54fdee BB |
159 | kfree(mp); |
160 | return rc; | |
f1ca4da6 | 161 | } |
162 | ||
163 | static void | |
5b5f5685 | 164 | splat_mutex_test2_func(void *arg) |
f1ca4da6 | 165 | { |
4d54fdee BB |
166 | mutex_priv_t *mp = (mutex_priv_t *)arg; |
167 | int rc; | |
168 | ASSERT(mp->mp_magic == SPLAT_MUTEX_TEST_MAGIC); | |
169 | ||
170 | /* Read the value before sleeping and write it after we wake up to | |
171 | * maximize the chance of a race if mutexs are not working properly */ | |
172 | mutex_enter(&mp->mp_mtx); | |
173 | rc = mp->mp_rc; | |
174 | set_current_state(TASK_INTERRUPTIBLE); | |
175 | schedule_timeout(HZ / 100); /* 1/100 of a second */ | |
176 | VERIFY(mp->mp_rc == rc); | |
177 | mp->mp_rc = rc + 1; | |
178 | mutex_exit(&mp->mp_mtx); | |
f1ca4da6 | 179 | } |
180 | ||
181 | static int | |
7c50328b | 182 | splat_mutex_test2(struct file *file, void *arg) |
f1ca4da6 | 183 | { |
4d54fdee BB |
184 | mutex_priv_t *mp; |
185 | taskq_t *tq; | |
186 | int i, rc = 0; | |
187 | ||
188 | mp = (mutex_priv_t *)kmalloc(sizeof(*mp), GFP_KERNEL); | |
189 | if (mp == NULL) | |
190 | return -ENOMEM; | |
191 | ||
192 | /* Create several threads allowing tasks to race with each other */ | |
193 | tq = taskq_create(SPLAT_MUTEX_TEST_TASKQ, num_online_cpus(), | |
194 | maxclsyspri, 50, INT_MAX, TASKQ_PREPOPULATE); | |
195 | if (tq == NULL) { | |
196 | rc = -ENOMEM; | |
197 | goto out; | |
198 | } | |
199 | ||
200 | mp->mp_magic = SPLAT_MUTEX_TEST_MAGIC; | |
201 | mp->mp_file = file; | |
202 | mutex_init(&(mp->mp_mtx), SPLAT_MUTEX_TEST_NAME, MUTEX_DEFAULT, NULL); | |
203 | mp->mp_rc = 0; | |
204 | ||
205 | /* | |
206 | * Schedule N work items to the work queue each of which enters the | |
207 | * mutex, sleeps briefly, then exits the mutex. On a multiprocessor | |
208 | * box these work items will be handled by all available CPUs. The | |
209 | * task function checks to ensure the tracked shared variable is | |
210 | * always only incremented by one. Additionally, the mutex itself | |
211 | * is instrumented such that if any two processors are in the | |
212 | * critical region at the same time the system will panic. If the | |
213 | * mutex is implemented right this will never happy, that's a pass. | |
214 | */ | |
215 | for (i = 0; i < SPLAT_MUTEX_TEST_COUNT; i++) { | |
216 | if (!taskq_dispatch(tq, splat_mutex_test2_func, mp, TQ_SLEEP)) { | |
217 | splat_vprint(file, SPLAT_MUTEX_TEST2_NAME, | |
218 | "Failed to queue task %d\n", i); | |
219 | rc = -EINVAL; | |
220 | } | |
221 | } | |
222 | ||
223 | taskq_wait(tq); | |
224 | ||
225 | if (mp->mp_rc == SPLAT_MUTEX_TEST_COUNT) { | |
226 | splat_vprint(file, SPLAT_MUTEX_TEST2_NAME, "%d racing threads " | |
227 | "correctly entered/exited the mutex %d times\n", | |
228 | num_online_cpus(), mp->mp_rc); | |
229 | } else { | |
230 | splat_vprint(file, SPLAT_MUTEX_TEST2_NAME, "%d racing threads " | |
231 | "only processed %d/%d mutex work items\n", | |
232 | num_online_cpus(),mp->mp_rc,SPLAT_MUTEX_TEST_COUNT); | |
233 | rc = -EINVAL; | |
234 | } | |
235 | ||
236 | taskq_destroy(tq); | |
237 | mutex_destroy(&(mp->mp_mtx)); | |
f1ca4da6 | 238 | out: |
4d54fdee BB |
239 | kfree(mp); |
240 | return rc; | |
f1ca4da6 | 241 | } |
242 | ||
243 | static int | |
7c50328b | 244 | splat_mutex_test3(struct file *file, void *arg) |
f1ca4da6 | 245 | { |
246 | kmutex_t mtx; | |
4d54fdee | 247 | int rc = 0; |
f1ca4da6 | 248 | |
4d54fdee BB |
249 | mutex_init(&mtx, SPLAT_MUTEX_TEST_NAME, MUTEX_DEFAULT, NULL); |
250 | mutex_enter(&mtx); | |
f1ca4da6 | 251 | |
4d54fdee BB |
252 | /* Mutex should be owned by current */ |
253 | if (!mutex_owned(&mtx)) { | |
254 | splat_vprint(file, SPLAT_MUTEX_TEST3_NAME, "Unowned mutex " | |
255 | "should be owned by pid %d\n", current->pid); | |
256 | rc = -EINVAL; | |
257 | goto out; | |
258 | } | |
f1ca4da6 | 259 | |
4d54fdee | 260 | mutex_exit(&mtx); |
f1ca4da6 | 261 | |
4d54fdee BB |
262 | /* Mutex should not be owned by any task */ |
263 | if (mutex_owned(&mtx)) { | |
264 | splat_vprint(file, SPLAT_MUTEX_TEST3_NAME, "Mutex owned by " | |
265 | "pid %d should be unowned\b", current->pid); | |
266 | rc = -EINVAL; | |
267 | goto out; | |
268 | } | |
f1ca4da6 | 269 | |
7c50328b | 270 | splat_vprint(file, SPLAT_MUTEX_TEST3_NAME, "%s", |
4d54fdee | 271 | "Correct mutex_owned() behavior\n"); |
f1ca4da6 | 272 | out: |
4d54fdee | 273 | mutex_destroy(&mtx); |
f1ca4da6 | 274 | |
4d54fdee | 275 | return rc; |
f1ca4da6 | 276 | } |
277 | ||
278 | static int | |
7c50328b | 279 | splat_mutex_test4(struct file *file, void *arg) |
f1ca4da6 | 280 | { |
281 | kmutex_t mtx; | |
4d54fdee BB |
282 | kthread_t *owner; |
283 | int rc = 0; | |
284 | ||
285 | mutex_init(&mtx, SPLAT_MUTEX_TEST_NAME, MUTEX_DEFAULT, NULL); | |
286 | mutex_enter(&mtx); | |
287 | ||
288 | /* Mutex should be owned by current */ | |
289 | owner = mutex_owner(&mtx); | |
290 | if (current != owner) { | |
291 | splat_vprint(file, SPLAT_MUTEX_TEST3_NAME, "Mutex should " | |
292 | "be owned by pid %d but is owned by pid %d\n", | |
293 | current->pid, owner ? owner->pid : -1); | |
294 | rc = -EINVAL; | |
295 | goto out; | |
296 | } | |
297 | ||
298 | mutex_exit(&mtx); | |
299 | ||
300 | /* Mutex should not be owned by any task */ | |
301 | owner = mutex_owner(&mtx); | |
302 | if (owner) { | |
303 | splat_vprint(file, SPLAT_MUTEX_TEST3_NAME, "Mutex should not " | |
304 | "be owned but is owned by pid %d\n", owner->pid); | |
305 | rc = -EINVAL; | |
306 | goto out; | |
307 | } | |
f1ca4da6 | 308 | |
7c50328b | 309 | splat_vprint(file, SPLAT_MUTEX_TEST3_NAME, "%s", |
4d54fdee | 310 | "Correct mutex_owner() behavior\n"); |
f1ca4da6 | 311 | out: |
4d54fdee | 312 | mutex_destroy(&mtx); |
f1ca4da6 | 313 | |
4d54fdee | 314 | return rc; |
f1ca4da6 | 315 | } |
316 | ||
7c50328b | 317 | splat_subsystem_t * |
318 | splat_mutex_init(void) | |
f1ca4da6 | 319 | { |
7c50328b | 320 | splat_subsystem_t *sub; |
f1ca4da6 | 321 | |
322 | sub = kmalloc(sizeof(*sub), GFP_KERNEL); | |
323 | if (sub == NULL) | |
324 | return NULL; | |
325 | ||
326 | memset(sub, 0, sizeof(*sub)); | |
7c50328b | 327 | strncpy(sub->desc.name, SPLAT_MUTEX_NAME, SPLAT_NAME_SIZE); |
328 | strncpy(sub->desc.desc, SPLAT_MUTEX_DESC, SPLAT_DESC_SIZE); | |
f1ca4da6 | 329 | INIT_LIST_HEAD(&sub->subsystem_list); |
330 | INIT_LIST_HEAD(&sub->test_list); | |
331 | spin_lock_init(&sub->test_lock); | |
7c50328b | 332 | sub->desc.id = SPLAT_SUBSYSTEM_MUTEX; |
f1ca4da6 | 333 | |
7c50328b | 334 | SPLAT_TEST_INIT(sub, SPLAT_MUTEX_TEST1_NAME, SPLAT_MUTEX_TEST1_DESC, |
335 | SPLAT_MUTEX_TEST1_ID, splat_mutex_test1); | |
336 | SPLAT_TEST_INIT(sub, SPLAT_MUTEX_TEST2_NAME, SPLAT_MUTEX_TEST2_DESC, | |
337 | SPLAT_MUTEX_TEST2_ID, splat_mutex_test2); | |
338 | SPLAT_TEST_INIT(sub, SPLAT_MUTEX_TEST3_NAME, SPLAT_MUTEX_TEST3_DESC, | |
339 | SPLAT_MUTEX_TEST3_ID, splat_mutex_test3); | |
340 | SPLAT_TEST_INIT(sub, SPLAT_MUTEX_TEST4_NAME, SPLAT_MUTEX_TEST4_DESC, | |
341 | SPLAT_MUTEX_TEST4_ID, splat_mutex_test4); | |
f1ca4da6 | 342 | |
343 | return sub; | |
344 | } | |
345 | ||
346 | void | |
7c50328b | 347 | splat_mutex_fini(splat_subsystem_t *sub) |
f1ca4da6 | 348 | { |
349 | ASSERT(sub); | |
7c50328b | 350 | SPLAT_TEST_FINI(sub, SPLAT_MUTEX_TEST4_ID); |
351 | SPLAT_TEST_FINI(sub, SPLAT_MUTEX_TEST3_ID); | |
352 | SPLAT_TEST_FINI(sub, SPLAT_MUTEX_TEST2_ID); | |
353 | SPLAT_TEST_FINI(sub, SPLAT_MUTEX_TEST1_ID); | |
f1ca4da6 | 354 | |
355 | kfree(sub); | |
356 | } | |
357 | ||
358 | int | |
7c50328b | 359 | splat_mutex_id(void) { |
360 | return SPLAT_SUBSYSTEM_MUTEX; | |
f1ca4da6 | 361 | } |