]>
git.proxmox.com Git - mirror_spl.git/blob - module/splat/splat-mutex.c
909d730cb014453e1bdca80d041ceabc6f3e5896
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
)
86 mp
= (mutex_priv_t
*)kmalloc(sizeof(*mp
), GFP_KERNEL
);
90 tq
= taskq_create(SPLAT_MUTEX_TEST_TASKQ
, 1, maxclsyspri
,
91 50, INT_MAX
, TASKQ_PREPOPULATE
);
97 mp
->mp_magic
= SPLAT_MUTEX_TEST_MAGIC
;
99 mutex_init(&mp
->mp_mtx
, SPLAT_MUTEX_TEST_NAME
, MUTEX_DEFAULT
, NULL
);
100 mutex_enter(&mp
->mp_mtx
);
103 * Schedule a task function which will try and acquire the mutex via
104 * mutex_tryenter() while it's held. This should fail and the task
105 * function will indicate this status in the passed private data.
108 id
= taskq_dispatch(tq
, splat_mutex_test1_func
, mp
, TQ_SLEEP
);
110 mutex_exit(&mp
->mp_mtx
);
111 splat_vprint(file
, SPLAT_MUTEX_TEST1_NAME
, "%s",
112 "taskq_dispatch() failed\n");
117 taskq_wait_id(tq
, id
);
118 mutex_exit(&mp
->mp_mtx
);
120 /* Task function successfully acquired mutex, very bad! */
121 if (mp
->mp_rc
!= -EBUSY
) {
122 splat_vprint(file
, SPLAT_MUTEX_TEST1_NAME
,
123 "mutex_trylock() incorrectly succeeded when "
124 "the mutex was held, %d/%d\n", id
, mp
->mp_rc
);
128 splat_vprint(file
, SPLAT_MUTEX_TEST1_NAME
, "%s",
129 "mutex_trylock() correctly failed when "
130 "the mutex was held\n");
134 * Schedule a task function which will try and acquire the mutex via
135 * mutex_tryenter() while it is not held. This should succeed and
136 * can be verified by checking the private data.
139 id
= taskq_dispatch(tq
, splat_mutex_test1_func
, mp
, TQ_SLEEP
);
141 splat_vprint(file
, SPLAT_MUTEX_TEST1_NAME
, "%s",
142 "taskq_dispatch() failed\n");
147 taskq_wait_id(tq
, id
);
149 /* Task function failed to acquire mutex, very bad! */
150 if (mp
->mp_rc
!= 0) {
151 splat_vprint(file
, SPLAT_MUTEX_TEST1_NAME
,
152 "mutex_trylock() incorrectly failed when "
153 "the mutex was not held, %d/%d\n", id
, mp
->mp_rc
);
156 splat_vprint(file
, SPLAT_MUTEX_TEST1_NAME
, "%s",
157 "mutex_trylock() correctly succeeded "
158 "when the mutex was not held\n");
162 mutex_destroy(&(mp
->mp_mtx
));
169 splat_mutex_test2_func(void *arg
)
171 mutex_priv_t
*mp
= (mutex_priv_t
*)arg
;
173 ASSERT(mp
->mp_magic
== SPLAT_MUTEX_TEST_MAGIC
);
175 /* Read the value before sleeping and write it after we wake up to
176 * maximize the chance of a race if mutexs are not working properly */
177 mutex_enter(&mp
->mp_mtx
);
179 set_current_state(TASK_INTERRUPTIBLE
);
180 schedule_timeout(HZ
/ 100); /* 1/100 of a second */
181 VERIFY(mp
->mp_rc
== rc
);
183 mutex_exit(&mp
->mp_mtx
);
187 splat_mutex_test2(struct file
*file
, void *arg
)
193 mp
= (mutex_priv_t
*)kmalloc(sizeof(*mp
), GFP_KERNEL
);
197 /* Create several threads allowing tasks to race with each other */
198 tq
= taskq_create(SPLAT_MUTEX_TEST_TASKQ
, num_online_cpus(),
199 maxclsyspri
, 50, INT_MAX
, TASKQ_PREPOPULATE
);
205 mp
->mp_magic
= SPLAT_MUTEX_TEST_MAGIC
;
207 mutex_init(&(mp
->mp_mtx
), SPLAT_MUTEX_TEST_NAME
, MUTEX_DEFAULT
, NULL
);
211 * Schedule N work items to the work queue each of which enters the
212 * mutex, sleeps briefly, then exits the mutex. On a multiprocessor
213 * box these work items will be handled by all available CPUs. The
214 * task function checks to ensure the tracked shared variable is
215 * always only incremented by one. Additionally, the mutex itself
216 * is instrumented such that if any two processors are in the
217 * critical region at the same time the system will panic. If the
218 * mutex is implemented right this will never happy, that's a pass.
220 for (i
= 0; i
< SPLAT_MUTEX_TEST_COUNT
; i
++) {
221 if (!taskq_dispatch(tq
, splat_mutex_test2_func
, mp
, TQ_SLEEP
)) {
222 splat_vprint(file
, SPLAT_MUTEX_TEST2_NAME
,
223 "Failed to queue task %d\n", i
);
230 if (mp
->mp_rc
== SPLAT_MUTEX_TEST_COUNT
) {
231 splat_vprint(file
, SPLAT_MUTEX_TEST2_NAME
, "%d racing threads "
232 "correctly entered/exited the mutex %d times\n",
233 num_online_cpus(), mp
->mp_rc
);
235 splat_vprint(file
, SPLAT_MUTEX_TEST2_NAME
, "%d racing threads "
236 "only processed %d/%d mutex work items\n",
237 num_online_cpus(),mp
->mp_rc
,SPLAT_MUTEX_TEST_COUNT
);
242 mutex_destroy(&(mp
->mp_mtx
));
249 splat_mutex_owned(void *priv
)
251 mutex_priv_t
*mp
= (mutex_priv_t
*)priv
;
253 ASSERT(mp
->mp_magic
== SPLAT_MUTEX_TEST_MAGIC
);
254 mp
->mp_rc
= mutex_owned(&mp
->mp_mtx
);
255 mp
->mp_rc2
= MUTEX_HELD(&mp
->mp_mtx
);
259 splat_mutex_test3(struct file
*file
, void *arg
)
265 mp
.mp_magic
= SPLAT_MUTEX_TEST_MAGIC
;
267 mutex_init(&mp
.mp_mtx
, SPLAT_MUTEX_TEST_NAME
, MUTEX_DEFAULT
, NULL
);
269 if ((tq
= taskq_create(SPLAT_MUTEX_TEST_NAME
, 1, maxclsyspri
,
270 50, INT_MAX
, TASKQ_PREPOPULATE
)) == NULL
) {
271 splat_vprint(file
, SPLAT_MUTEX_TEST3_NAME
, "Taskq '%s' "
272 "create failed\n", SPLAT_MUTEX_TEST3_NAME
);
276 mutex_enter(&mp
.mp_mtx
);
278 /* Mutex should be owned by current */
279 if (!mutex_owned(&mp
.mp_mtx
)) {
280 splat_vprint(file
, SPLAT_MUTEX_TEST3_NAME
, "Unowned mutex "
281 "should be owned by pid %d\n", current
->pid
);
286 if (taskq_dispatch(tq
, splat_mutex_owned
, &mp
, TQ_SLEEP
) == 0) {
287 splat_vprint(file
, SPLAT_MUTEX_TEST3_NAME
, "Failed to "
288 "dispatch function '%s' to taskq\n",
289 sym2str(splat_mutex_owned
));
295 /* Mutex should not be owned which checked from a different thread */
296 if (mp
.mp_rc
|| mp
.mp_rc2
) {
297 splat_vprint(file
, SPLAT_MUTEX_TEST3_NAME
, "Mutex owned by "
298 "pid %d not by taskq\n", current
->pid
);
303 mutex_exit(&mp
.mp_mtx
);
305 /* Mutex should not be owned by current */
306 if (mutex_owned(&mp
.mp_mtx
)) {
307 splat_vprint(file
, SPLAT_MUTEX_TEST3_NAME
, "Mutex owned by "
308 "pid %d it should be unowned\b", current
->pid
);
313 if (taskq_dispatch(tq
, splat_mutex_owned
, &mp
, TQ_SLEEP
) == 0) {
314 splat_vprint(file
, SPLAT_MUTEX_TEST3_NAME
, "Failed to "
315 "dispatch function '%s' to taskq\n",
316 sym2str(splat_mutex_owned
));
322 /* Mutex should be owned by no one */
323 if (mp
.mp_rc
|| mp
.mp_rc2
) {
324 splat_vprint(file
, SPLAT_MUTEX_TEST3_NAME
, "Mutex owned by "
325 "no one, %d/%d disagrees\n", mp
.mp_rc
, mp
.mp_rc2
);
330 splat_vprint(file
, SPLAT_MUTEX_TEST3_NAME
, "%s",
331 "Correct mutex_owned() behavior\n");
334 mutex_exit(&mp
.mp_mtx
);
336 mutex_destroy(&mp
.mp_mtx
);
343 splat_mutex_test4(struct file
*file
, void *arg
)
349 mutex_init(&mtx
, SPLAT_MUTEX_TEST_NAME
, MUTEX_DEFAULT
, NULL
);
352 * Verify mutex owner is cleared after being dropped. Depending
353 * on how you build your kernel this behavior changes, ensure the
354 * SPL mutex implementation is properly detecting this.
359 if (MUTEX_HELD(&mtx
)) {
360 splat_vprint(file
, SPLAT_MUTEX_TEST4_NAME
, "Mutex should "
361 "not be held, bit is by %p\n", mutex_owner(&mtx
));
368 /* Mutex should be owned by current */
369 owner
= mutex_owner(&mtx
);
370 if (current
!= owner
) {
371 splat_vprint(file
, SPLAT_MUTEX_TEST4_NAME
, "Mutex should "
372 "be owned by pid %d but is owned by pid %d\n",
373 current
->pid
, owner
? owner
->pid
: -1);
380 /* Mutex should not be owned by any task */
381 owner
= mutex_owner(&mtx
);
383 splat_vprint(file
, SPLAT_MUTEX_TEST4_NAME
, "Mutex should not "
384 "be owned but is owned by pid %d\n", owner
->pid
);
389 splat_vprint(file
, SPLAT_MUTEX_TEST3_NAME
, "%s",
390 "Correct mutex_owner() behavior\n");
398 splat_mutex_init(void)
400 splat_subsystem_t
*sub
;
402 sub
= kmalloc(sizeof(*sub
), GFP_KERNEL
);
406 memset(sub
, 0, sizeof(*sub
));
407 strncpy(sub
->desc
.name
, SPLAT_MUTEX_NAME
, SPLAT_NAME_SIZE
);
408 strncpy(sub
->desc
.desc
, SPLAT_MUTEX_DESC
, SPLAT_DESC_SIZE
);
409 INIT_LIST_HEAD(&sub
->subsystem_list
);
410 INIT_LIST_HEAD(&sub
->test_list
);
411 spin_lock_init(&sub
->test_lock
);
412 sub
->desc
.id
= SPLAT_SUBSYSTEM_MUTEX
;
414 SPLAT_TEST_INIT(sub
, SPLAT_MUTEX_TEST1_NAME
, SPLAT_MUTEX_TEST1_DESC
,
415 SPLAT_MUTEX_TEST1_ID
, splat_mutex_test1
);
416 SPLAT_TEST_INIT(sub
, SPLAT_MUTEX_TEST2_NAME
, SPLAT_MUTEX_TEST2_DESC
,
417 SPLAT_MUTEX_TEST2_ID
, splat_mutex_test2
);
418 SPLAT_TEST_INIT(sub
, SPLAT_MUTEX_TEST3_NAME
, SPLAT_MUTEX_TEST3_DESC
,
419 SPLAT_MUTEX_TEST3_ID
, splat_mutex_test3
);
420 SPLAT_TEST_INIT(sub
, SPLAT_MUTEX_TEST4_NAME
, SPLAT_MUTEX_TEST4_DESC
,
421 SPLAT_MUTEX_TEST4_ID
, splat_mutex_test4
);
427 splat_mutex_fini(splat_subsystem_t
*sub
)
430 SPLAT_TEST_FINI(sub
, SPLAT_MUTEX_TEST4_ID
);
431 SPLAT_TEST_FINI(sub
, SPLAT_MUTEX_TEST3_ID
);
432 SPLAT_TEST_FINI(sub
, SPLAT_MUTEX_TEST2_ID
);
433 SPLAT_TEST_FINI(sub
, SPLAT_MUTEX_TEST1_ID
);
439 splat_mutex_id(void) {
440 return SPLAT_SUBSYSTEM_MUTEX
;