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>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
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.
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
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 \*****************************************************************************/
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"
33 #define SPLAT_MUTEX_NAME "mutex"
34 #define SPLAT_MUTEX_DESC "Kernel Mutex Tests"
36 #define SPLAT_MUTEX_TEST1_ID 0x0401
37 #define SPLAT_MUTEX_TEST1_NAME "tryenter"
38 #define SPLAT_MUTEX_TEST1_DESC "Validate mutex_tryenter() correctness"
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"
44 #define SPLAT_MUTEX_TEST3_ID 0x0403
45 #define SPLAT_MUTEX_TEST3_NAME "owned"
46 #define SPLAT_MUTEX_TEST3_DESC "Validate mutex_owned() correctness"
48 #define SPLAT_MUTEX_TEST4_ID 0x0404
49 #define SPLAT_MUTEX_TEST4_NAME "owner"
50 #define SPLAT_MUTEX_TEST4_DESC "Validate mutex_owner() correctness"
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
57 typedef struct mutex_priv {
58 unsigned long mp_magic;
66 splat_mutex_test1_func(void *arg)
68 mutex_priv_t *mp = (mutex_priv_t *)arg;
69 ASSERT(mp->mp_magic == SPLAT_MUTEX_TEST_MAGIC);
71 if (mutex_tryenter(&mp->mp_mtx)) {
73 mutex_exit(&mp->mp_mtx);
80 splat_mutex_test1(struct file *file, void *arg)
87 mp = (mutex_priv_t *)kmalloc(sizeof(*mp), GFP_KERNEL);
91 tq = taskq_create(SPLAT_MUTEX_TEST_TASKQ, 1, defclsyspri,
92 50, INT_MAX, TASKQ_PREPOPULATE);
98 mp->mp_magic = SPLAT_MUTEX_TEST_MAGIC;
100 mutex_init(&mp->mp_mtx, SPLAT_MUTEX_TEST_NAME, MUTEX_DEFAULT, NULL);
101 mutex_enter(&mp->mp_mtx);
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.
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");
118 taskq_wait_id(tq, id);
119 mutex_exit(&mp->mp_mtx);
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);
129 splat_vprint(file, SPLAT_MUTEX_TEST1_NAME, "%s",
130 "mutex_trylock() correctly failed when "
131 "the mutex was held\n");
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.
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");
148 taskq_wait_id(tq, id);
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);
157 splat_vprint(file, SPLAT_MUTEX_TEST1_NAME, "%s",
158 "mutex_trylock() correctly succeeded "
159 "when the mutex was not held\n");
163 mutex_destroy(&(mp->mp_mtx));
170 splat_mutex_test2_func(void *arg)
172 mutex_priv_t *mp = (mutex_priv_t *)arg;
174 ASSERT(mp->mp_magic == SPLAT_MUTEX_TEST_MAGIC);
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);
180 set_current_state(TASK_INTERRUPTIBLE);
181 schedule_timeout(HZ / 100); /* 1/100 of a second */
182 VERIFY(mp->mp_rc == rc);
184 mutex_exit(&mp->mp_mtx);
188 splat_mutex_test2(struct file *file, void *arg)
195 mp = (mutex_priv_t *)kmalloc(sizeof(*mp), GFP_KERNEL);
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);
207 mp->mp_magic = SPLAT_MUTEX_TEST_MAGIC;
209 mutex_init(&(mp->mp_mtx), SPLAT_MUTEX_TEST_NAME, MUTEX_DEFAULT, NULL);
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.
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);
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);
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);
245 mutex_destroy(&(mp->mp_mtx));
252 splat_mutex_owned(void *priv)
254 mutex_priv_t *mp = (mutex_priv_t *)priv;
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);
262 splat_mutex_test3(struct file *file, void *arg)
269 mp.mp_magic = SPLAT_MUTEX_TEST_MAGIC;
271 mutex_init(&mp.mp_mtx, SPLAT_MUTEX_TEST_NAME, MUTEX_DEFAULT, NULL);
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);
280 mutex_enter(&mp.mp_mtx);
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);
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));
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);
308 mutex_exit(&mp.mp_mtx);
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);
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));
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);
336 splat_vprint(file, SPLAT_MUTEX_TEST3_NAME, "%s",
337 "Correct mutex_owned() behavior\n");
340 mutex_exit(&mp.mp_mtx);
342 mutex_destroy(&mp.mp_mtx);
349 splat_mutex_test4(struct file *file, void *arg)
355 mutex_init(&mtx, SPLAT_MUTEX_TEST_NAME, MUTEX_DEFAULT, NULL);
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.
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));
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);
386 /* Mutex should not be owned by any task */
387 owner = mutex_owner(&mtx);
389 splat_vprint(file, SPLAT_MUTEX_TEST4_NAME, "Mutex should not "
390 "be owned but is owned by pid %d\n", owner->pid);
395 splat_vprint(file, SPLAT_MUTEX_TEST3_NAME, "%s",
396 "Correct mutex_owner() behavior\n");
404 splat_mutex_init(void)
406 splat_subsystem_t *sub;
408 sub = kmalloc(sizeof(*sub), GFP_KERNEL);
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;
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);
433 splat_mutex_fini(splat_subsystem_t *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);
445 splat_mutex_id(void) {
446 return SPLAT_SUBSYSTEM_MUTEX;