]> git.proxmox.com Git - mirror_spl-debian.git/blob - module/splat/splat-mutex.c
New upstream version 0.7.2
[mirror_spl-debian.git] / module / splat / splat-mutex.c
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>.
6 * UCRL-CODE-235197
7 *
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
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.
15 *
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
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
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting LAyer Tests (SPLAT) Mutex Tests.
25 \*****************************************************************************/
26
27 #include <sys/mutex.h>
28 #include <sys/taskq.h>
29 #include <linux/delay.h>
30 #include <linux/mm_compat.h>
31 #include "splat-internal.h"
32
33 #define SPLAT_MUTEX_NAME "mutex"
34 #define SPLAT_MUTEX_DESC "Kernel Mutex Tests"
35
36 #define SPLAT_MUTEX_TEST1_ID 0x0401
37 #define SPLAT_MUTEX_TEST1_NAME "tryenter"
38 #define SPLAT_MUTEX_TEST1_DESC "Validate mutex_tryenter() correctness"
39
40 #define SPLAT_MUTEX_TEST2_ID 0x0402
41 #define SPLAT_MUTEX_TEST2_NAME "race"
42 #define SPLAT_MUTEX_TEST2_DESC "Many threads entering/exiting the mutex"
43
44 #define SPLAT_MUTEX_TEST3_ID 0x0403
45 #define SPLAT_MUTEX_TEST3_NAME "owned"
46 #define SPLAT_MUTEX_TEST3_DESC "Validate mutex_owned() correctness"
47
48 #define SPLAT_MUTEX_TEST4_ID 0x0404
49 #define SPLAT_MUTEX_TEST4_NAME "owner"
50 #define SPLAT_MUTEX_TEST4_DESC "Validate mutex_owner() correctness"
51
52 #define SPLAT_MUTEX_TEST_MAGIC 0x115599DDUL
53 #define SPLAT_MUTEX_TEST_NAME "mutex_test"
54 #define SPLAT_MUTEX_TEST_TASKQ "mutex_taskq"
55 #define SPLAT_MUTEX_TEST_COUNT 128
56
57 typedef struct mutex_priv {
58 unsigned long mp_magic;
59 struct file *mp_file;
60 kmutex_t mp_mtx;
61 int mp_rc;
62 int mp_rc2;
63 } mutex_priv_t;
64
65 static void
66 splat_mutex_test1_func(void *arg)
67 {
68 mutex_priv_t *mp = (mutex_priv_t *)arg;
69 ASSERT(mp->mp_magic == SPLAT_MUTEX_TEST_MAGIC);
70
71 if (mutex_tryenter(&mp->mp_mtx)) {
72 mp->mp_rc = 0;
73 mutex_exit(&mp->mp_mtx);
74 } else {
75 mp->mp_rc = -EBUSY;
76 }
77 }
78
79 static int
80 splat_mutex_test1(struct file *file, void *arg)
81 {
82 mutex_priv_t *mp;
83 taskq_t *tq;
84 taskqid_t id;
85 int rc = 0;
86
87 mp = (mutex_priv_t *)kmalloc(sizeof(*mp), GFP_KERNEL);
88 if (mp == NULL)
89 return -ENOMEM;
90
91 tq = taskq_create(SPLAT_MUTEX_TEST_TASKQ, 1, defclsyspri,
92 50, INT_MAX, TASKQ_PREPOPULATE);
93 if (tq == NULL) {
94 rc = -ENOMEM;
95 goto out2;
96 }
97
98 mp->mp_magic = SPLAT_MUTEX_TEST_MAGIC;
99 mp->mp_file = file;
100 mutex_init(&mp->mp_mtx, SPLAT_MUTEX_TEST_NAME, MUTEX_DEFAULT, NULL);
101 mutex_enter(&mp->mp_mtx);
102
103 /*
104 * Schedule a task function which will try and acquire the mutex via
105 * mutex_tryenter() while it's held. This should fail and the task
106 * function will indicate this status in the passed private data.
107 */
108 mp->mp_rc = -EINVAL;
109 id = taskq_dispatch(tq, splat_mutex_test1_func, mp, TQ_SLEEP);
110 if (id == TASKQID_INVALID) {
111 mutex_exit(&mp->mp_mtx);
112 splat_vprint(file, SPLAT_MUTEX_TEST1_NAME, "%s",
113 "taskq_dispatch() failed\n");
114 rc = -EINVAL;
115 goto out;
116 }
117
118 taskq_wait_id(tq, id);
119 mutex_exit(&mp->mp_mtx);
120
121 /* Task function successfully acquired mutex, very bad! */
122 if (mp->mp_rc != -EBUSY) {
123 splat_vprint(file, SPLAT_MUTEX_TEST1_NAME,
124 "mutex_trylock() incorrectly succeeded when "
125 "the mutex was held, %d/%d\n", (int)id, mp->mp_rc);
126 rc = -EINVAL;
127 goto out;
128 } else {
129 splat_vprint(file, SPLAT_MUTEX_TEST1_NAME, "%s",
130 "mutex_trylock() correctly failed when "
131 "the mutex was held\n");
132 }
133
134 /*
135 * Schedule a task function which will try and acquire the mutex via
136 * mutex_tryenter() while it is not held. This should succeed and
137 * can be verified by checking the private data.
138 */
139 mp->mp_rc = -EINVAL;
140 id = taskq_dispatch(tq, splat_mutex_test1_func, mp, TQ_SLEEP);
141 if (id == TASKQID_INVALID) {
142 splat_vprint(file, SPLAT_MUTEX_TEST1_NAME, "%s",
143 "taskq_dispatch() failed\n");
144 rc = -EINVAL;
145 goto out;
146 }
147
148 taskq_wait_id(tq, id);
149
150 /* Task function failed to acquire mutex, very bad! */
151 if (mp->mp_rc != 0) {
152 splat_vprint(file, SPLAT_MUTEX_TEST1_NAME,
153 "mutex_trylock() incorrectly failed when the mutex "
154 "was not held, %d/%d\n", (int)id, mp->mp_rc);
155 rc = -EINVAL;
156 } else {
157 splat_vprint(file, SPLAT_MUTEX_TEST1_NAME, "%s",
158 "mutex_trylock() correctly succeeded "
159 "when the mutex was not held\n");
160 }
161 out:
162 taskq_destroy(tq);
163 mutex_destroy(&(mp->mp_mtx));
164 out2:
165 kfree(mp);
166 return rc;
167 }
168
169 static void
170 splat_mutex_test2_func(void *arg)
171 {
172 mutex_priv_t *mp = (mutex_priv_t *)arg;
173 int rc;
174 ASSERT(mp->mp_magic == SPLAT_MUTEX_TEST_MAGIC);
175
176 /* Read the value before sleeping and write it after we wake up to
177 * maximize the chance of a race if mutexs are not working properly */
178 mutex_enter(&mp->mp_mtx);
179 rc = mp->mp_rc;
180 set_current_state(TASK_INTERRUPTIBLE);
181 schedule_timeout(HZ / 100); /* 1/100 of a second */
182 VERIFY(mp->mp_rc == rc);
183 mp->mp_rc = rc + 1;
184 mutex_exit(&mp->mp_mtx);
185 }
186
187 static int
188 splat_mutex_test2(struct file *file, void *arg)
189 {
190 mutex_priv_t *mp;
191 taskq_t *tq;
192 taskqid_t id;
193 int i, rc = 0;
194
195 mp = (mutex_priv_t *)kmalloc(sizeof(*mp), GFP_KERNEL);
196 if (mp == NULL)
197 return -ENOMEM;
198
199 /* Create several threads allowing tasks to race with each other */
200 tq = taskq_create(SPLAT_MUTEX_TEST_TASKQ, num_online_cpus(),
201 defclsyspri, 50, INT_MAX, TASKQ_PREPOPULATE);
202 if (tq == NULL) {
203 rc = -ENOMEM;
204 goto out;
205 }
206
207 mp->mp_magic = SPLAT_MUTEX_TEST_MAGIC;
208 mp->mp_file = file;
209 mutex_init(&(mp->mp_mtx), SPLAT_MUTEX_TEST_NAME, MUTEX_DEFAULT, NULL);
210 mp->mp_rc = 0;
211
212 /*
213 * Schedule N work items to the work queue each of which enters the
214 * mutex, sleeps briefly, then exits the mutex. On a multiprocessor
215 * box these work items will be handled by all available CPUs. The
216 * task function checks to ensure the tracked shared variable is
217 * always only incremented by one. Additionally, the mutex itself
218 * is instrumented such that if any two processors are in the
219 * critical region at the same time the system will panic. If the
220 * mutex is implemented right this will never happy, that's a pass.
221 */
222 for (i = 0; i < SPLAT_MUTEX_TEST_COUNT; i++) {
223 id = taskq_dispatch(tq, splat_mutex_test2_func, mp, TQ_SLEEP);
224 if (id == TASKQID_INVALID) {
225 splat_vprint(file, SPLAT_MUTEX_TEST2_NAME,
226 "Failed to queue task %d\n", i);
227 rc = -EINVAL;
228 }
229 }
230
231 taskq_wait(tq);
232
233 if (mp->mp_rc == SPLAT_MUTEX_TEST_COUNT) {
234 splat_vprint(file, SPLAT_MUTEX_TEST2_NAME, "%d racing threads "
235 "correctly entered/exited the mutex %d times\n",
236 num_online_cpus(), mp->mp_rc);
237 } else {
238 splat_vprint(file, SPLAT_MUTEX_TEST2_NAME, "%d racing threads "
239 "only processed %d/%d mutex work items\n",
240 num_online_cpus(),mp->mp_rc,SPLAT_MUTEX_TEST_COUNT);
241 rc = -EINVAL;
242 }
243
244 taskq_destroy(tq);
245 mutex_destroy(&(mp->mp_mtx));
246 out:
247 kfree(mp);
248 return rc;
249 }
250
251 static void
252 splat_mutex_owned(void *priv)
253 {
254 mutex_priv_t *mp = (mutex_priv_t *)priv;
255
256 ASSERT(mp->mp_magic == SPLAT_MUTEX_TEST_MAGIC);
257 mp->mp_rc = mutex_owned(&mp->mp_mtx);
258 mp->mp_rc2 = MUTEX_HELD(&mp->mp_mtx);
259 }
260
261 static int
262 splat_mutex_test3(struct file *file, void *arg)
263 {
264 mutex_priv_t mp;
265 taskq_t *tq;
266 taskqid_t id;
267 int rc = 0;
268
269 mp.mp_magic = SPLAT_MUTEX_TEST_MAGIC;
270 mp.mp_file = file;
271 mutex_init(&mp.mp_mtx, SPLAT_MUTEX_TEST_NAME, MUTEX_DEFAULT, NULL);
272
273 if ((tq = taskq_create(SPLAT_MUTEX_TEST_NAME, 1, defclsyspri,
274 50, INT_MAX, TASKQ_PREPOPULATE)) == NULL) {
275 splat_vprint(file, SPLAT_MUTEX_TEST3_NAME, "Taskq '%s' "
276 "create failed\n", SPLAT_MUTEX_TEST3_NAME);
277 return -EINVAL;
278 }
279
280 mutex_enter(&mp.mp_mtx);
281
282 /* Mutex should be owned by current */
283 if (!mutex_owned(&mp.mp_mtx)) {
284 splat_vprint(file, SPLAT_MUTEX_TEST3_NAME, "Unowned mutex "
285 "should be owned by pid %d\n", current->pid);
286 rc = -EINVAL;
287 goto out_exit;
288 }
289
290 id = taskq_dispatch(tq, splat_mutex_owned, &mp, TQ_SLEEP);
291 if (id == TASKQID_INVALID) {
292 splat_vprint(file, SPLAT_MUTEX_TEST3_NAME, "Failed to "
293 "dispatch function '%s' to taskq\n",
294 sym2str(splat_mutex_owned));
295 rc = -EINVAL;
296 goto out_exit;
297 }
298 taskq_wait(tq);
299
300 /* Mutex should not be owned which checked from a different thread */
301 if (mp.mp_rc || mp.mp_rc2) {
302 splat_vprint(file, SPLAT_MUTEX_TEST3_NAME, "Mutex owned by "
303 "pid %d not by taskq\n", current->pid);
304 rc = -EINVAL;
305 goto out_exit;
306 }
307
308 mutex_exit(&mp.mp_mtx);
309
310 /* Mutex should not be owned by current */
311 if (mutex_owned(&mp.mp_mtx)) {
312 splat_vprint(file, SPLAT_MUTEX_TEST3_NAME, "Mutex owned by "
313 "pid %d it should be unowned\b", current->pid);
314 rc = -EINVAL;
315 goto out;
316 }
317
318 id = taskq_dispatch(tq, splat_mutex_owned, &mp, TQ_SLEEP);
319 if (id == TASKQID_INVALID) {
320 splat_vprint(file, SPLAT_MUTEX_TEST3_NAME, "Failed to "
321 "dispatch function '%s' to taskq\n",
322 sym2str(splat_mutex_owned));
323 rc = -EINVAL;
324 goto out;
325 }
326 taskq_wait(tq);
327
328 /* Mutex should be owned by no one */
329 if (mp.mp_rc || mp.mp_rc2) {
330 splat_vprint(file, SPLAT_MUTEX_TEST3_NAME, "Mutex owned by "
331 "no one, %d/%d disagrees\n", mp.mp_rc, mp.mp_rc2);
332 rc = -EINVAL;
333 goto out;
334 }
335
336 splat_vprint(file, SPLAT_MUTEX_TEST3_NAME, "%s",
337 "Correct mutex_owned() behavior\n");
338 goto out;
339 out_exit:
340 mutex_exit(&mp.mp_mtx);
341 out:
342 mutex_destroy(&mp.mp_mtx);
343 taskq_destroy(tq);
344
345 return rc;
346 }
347
348 static int
349 splat_mutex_test4(struct file *file, void *arg)
350 {
351 kmutex_t mtx;
352 kthread_t *owner;
353 int rc = 0;
354
355 mutex_init(&mtx, SPLAT_MUTEX_TEST_NAME, MUTEX_DEFAULT, NULL);
356
357 /*
358 * Verify mutex owner is cleared after being dropped. Depending
359 * on how you build your kernel this behavior changes, ensure the
360 * SPL mutex implementation is properly detecting this.
361 */
362 mutex_enter(&mtx);
363 msleep(100);
364 mutex_exit(&mtx);
365 if (MUTEX_HELD(&mtx)) {
366 splat_vprint(file, SPLAT_MUTEX_TEST4_NAME, "Mutex should "
367 "not be held, bit is by %p\n", mutex_owner(&mtx));
368 rc = -EINVAL;
369 goto out;
370 }
371
372 mutex_enter(&mtx);
373
374 /* Mutex should be owned by current */
375 owner = mutex_owner(&mtx);
376 if (current != owner) {
377 splat_vprint(file, SPLAT_MUTEX_TEST4_NAME, "Mutex should "
378 "be owned by pid %d but is owned by pid %d\n",
379 current->pid, owner ? owner->pid : -1);
380 rc = -EINVAL;
381 goto out;
382 }
383
384 mutex_exit(&mtx);
385
386 /* Mutex should not be owned by any task */
387 owner = mutex_owner(&mtx);
388 if (owner) {
389 splat_vprint(file, SPLAT_MUTEX_TEST4_NAME, "Mutex should not "
390 "be owned but is owned by pid %d\n", owner->pid);
391 rc = -EINVAL;
392 goto out;
393 }
394
395 splat_vprint(file, SPLAT_MUTEX_TEST3_NAME, "%s",
396 "Correct mutex_owner() behavior\n");
397 out:
398 mutex_destroy(&mtx);
399
400 return rc;
401 }
402
403 splat_subsystem_t *
404 splat_mutex_init(void)
405 {
406 splat_subsystem_t *sub;
407
408 sub = kmalloc(sizeof(*sub), GFP_KERNEL);
409 if (sub == NULL)
410 return NULL;
411
412 memset(sub, 0, sizeof(*sub));
413 strncpy(sub->desc.name, SPLAT_MUTEX_NAME, SPLAT_NAME_SIZE);
414 strncpy(sub->desc.desc, SPLAT_MUTEX_DESC, SPLAT_DESC_SIZE);
415 INIT_LIST_HEAD(&sub->subsystem_list);
416 INIT_LIST_HEAD(&sub->test_list);
417 spin_lock_init(&sub->test_lock);
418 sub->desc.id = SPLAT_SUBSYSTEM_MUTEX;
419
420 splat_test_init(sub, SPLAT_MUTEX_TEST1_NAME, SPLAT_MUTEX_TEST1_DESC,
421 SPLAT_MUTEX_TEST1_ID, splat_mutex_test1);
422 splat_test_init(sub, SPLAT_MUTEX_TEST2_NAME, SPLAT_MUTEX_TEST2_DESC,
423 SPLAT_MUTEX_TEST2_ID, splat_mutex_test2);
424 splat_test_init(sub, SPLAT_MUTEX_TEST3_NAME, SPLAT_MUTEX_TEST3_DESC,
425 SPLAT_MUTEX_TEST3_ID, splat_mutex_test3);
426 splat_test_init(sub, SPLAT_MUTEX_TEST4_NAME, SPLAT_MUTEX_TEST4_DESC,
427 SPLAT_MUTEX_TEST4_ID, splat_mutex_test4);
428
429 return sub;
430 }
431
432 void
433 splat_mutex_fini(splat_subsystem_t *sub)
434 {
435 ASSERT(sub);
436 splat_test_fini(sub, SPLAT_MUTEX_TEST4_ID);
437 splat_test_fini(sub, SPLAT_MUTEX_TEST3_ID);
438 splat_test_fini(sub, SPLAT_MUTEX_TEST2_ID);
439 splat_test_fini(sub, SPLAT_MUTEX_TEST1_ID);
440
441 kfree(sub);
442 }
443
444 int
445 splat_mutex_id(void) {
446 return SPLAT_SUBSYSTEM_MUTEX;
447 }