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) Atomic Tests.
25 \*****************************************************************************/
27 #include <sys/atomic.h>
28 #include <sys/thread.h>
29 #include <sys/mutex.h>
30 #include <linux/mm_compat.h>
31 #include <linux/wait_compat.h>
32 #include <linux/slab.h>
33 #include "splat-internal.h"
35 #define SPLAT_ATOMIC_NAME "atomic"
36 #define SPLAT_ATOMIC_DESC "Kernel Atomic Tests"
38 #define SPLAT_ATOMIC_TEST1_ID 0x0b01
39 #define SPLAT_ATOMIC_TEST1_NAME "64-bit"
40 #define SPLAT_ATOMIC_TEST1_DESC "Validate 64-bit atomic ops"
42 #define SPLAT_ATOMIC_TEST_MAGIC 0x43435454UL
43 #define SPLAT_ATOMIC_INIT_VALUE 10000000UL
46 SPLAT_ATOMIC_INC_64
= 0,
47 SPLAT_ATOMIC_DEC_64
= 1,
48 SPLAT_ATOMIC_ADD_64
= 2,
49 SPLAT_ATOMIC_SUB_64
= 3,
50 SPLAT_ATOMIC_ADD_64_NV
= 4,
51 SPLAT_ATOMIC_SUB_64_NV
= 5,
52 SPLAT_ATOMIC_COUNT_64
= 6
55 typedef struct atomic_priv
{
56 unsigned long ap_magic
;
59 spl_wait_queue_head_t ap_waitq
;
60 volatile uint64_t ap_atomic
;
61 volatile uint64_t ap_atomic_exited
;
67 splat_atomic_work(void *priv
)
73 ap
= (atomic_priv_t
*)priv
;
74 ASSERT(ap
->ap_magic
== SPLAT_ATOMIC_TEST_MAGIC
);
76 mutex_enter(&ap
->ap_lock
);
78 wake_up(&ap
->ap_waitq
);
79 mutex_exit(&ap
->ap_lock
);
81 splat_vprint(ap
->ap_file
, SPLAT_ATOMIC_TEST1_NAME
,
82 "Thread %d successfully started: %lu/%lu\n", op
,
83 (long unsigned)ap
->ap_atomic
,
84 (long unsigned)ap
->ap_atomic_exited
);
86 for (i
= 0; i
< SPLAT_ATOMIC_INIT_VALUE
/ 10; i
++) {
88 /* Periodically sleep to mix up the ordering */
89 if ((i
% (SPLAT_ATOMIC_INIT_VALUE
/ 100)) == 0) {
90 splat_vprint(ap
->ap_file
, SPLAT_ATOMIC_TEST1_NAME
,
91 "Thread %d sleeping: %lu/%lu\n", op
,
92 (long unsigned)ap
->ap_atomic
,
93 (long unsigned)ap
->ap_atomic_exited
);
94 set_current_state(TASK_INTERRUPTIBLE
);
95 schedule_timeout(HZ
/ 100);
99 case SPLAT_ATOMIC_INC_64
:
100 atomic_inc_64(&ap
->ap_atomic
);
102 case SPLAT_ATOMIC_DEC_64
:
103 atomic_dec_64(&ap
->ap_atomic
);
105 case SPLAT_ATOMIC_ADD_64
:
106 atomic_add_64(&ap
->ap_atomic
, 3);
108 case SPLAT_ATOMIC_SUB_64
:
109 atomic_sub_64(&ap
->ap_atomic
, 3);
111 case SPLAT_ATOMIC_ADD_64_NV
:
112 atomic_add_64_nv(&ap
->ap_atomic
, 5);
114 case SPLAT_ATOMIC_SUB_64_NV
:
115 atomic_sub_64_nv(&ap
->ap_atomic
, 5);
118 PANIC("Undefined op %d\n", op
);
122 atomic_inc_64(&ap
->ap_atomic_exited
);
124 splat_vprint(ap
->ap_file
, SPLAT_ATOMIC_TEST1_NAME
,
125 "Thread %d successfully exited: %lu/%lu\n", op
,
126 (long unsigned)ap
->ap_atomic
,
127 (long unsigned)ap
->ap_atomic_exited
);
129 wake_up(&ap
->ap_waitq
);
134 splat_atomic_test1_cond(atomic_priv_t
*ap
, int started
)
136 return (ap
->ap_atomic_exited
== started
);
140 splat_atomic_test1(struct file
*file
, void *arg
)
147 ap
.ap_magic
= SPLAT_ATOMIC_TEST_MAGIC
;
149 mutex_init(&ap
.ap_lock
, SPLAT_ATOMIC_TEST1_NAME
, MUTEX_DEFAULT
, NULL
);
150 init_waitqueue_head(&ap
.ap_waitq
);
151 ap
.ap_atomic
= SPLAT_ATOMIC_INIT_VALUE
;
152 ap
.ap_atomic_exited
= 0;
154 for (i
= 0; i
< SPLAT_ATOMIC_COUNT_64
; i
++) {
155 mutex_enter(&ap
.ap_lock
);
158 thr
= (kthread_t
*)thread_create(NULL
, 0, splat_atomic_work
,
163 mutex_exit(&ap
.ap_lock
);
167 /* Prepare to wait, the new thread will wake us once it
168 * has made a copy of the unique private passed data */
169 prepare_to_wait(&ap
.ap_waitq
, &wait
, TASK_UNINTERRUPTIBLE
);
170 mutex_exit(&ap
.ap_lock
);
174 wait_event(ap
.ap_waitq
, splat_atomic_test1_cond(&ap
, i
));
177 splat_vprint(file
, SPLAT_ATOMIC_TEST1_NAME
, "Only started "
178 "%d/%d test threads\n", i
, SPLAT_ATOMIC_COUNT_64
);
182 if (ap
.ap_atomic
!= SPLAT_ATOMIC_INIT_VALUE
) {
183 splat_vprint(file
, SPLAT_ATOMIC_TEST1_NAME
,
184 "Final value %lu does not match initial value %lu\n",
185 (long unsigned)ap
.ap_atomic
, SPLAT_ATOMIC_INIT_VALUE
);
189 splat_vprint(file
, SPLAT_ATOMIC_TEST1_NAME
,
190 "Success initial and final values match, %lu == %lu\n",
191 (long unsigned)ap
.ap_atomic
, SPLAT_ATOMIC_INIT_VALUE
);
193 mutex_destroy(&ap
.ap_lock
);
199 splat_atomic_init(void)
201 splat_subsystem_t
*sub
;
203 sub
= kmalloc(sizeof(*sub
), GFP_KERNEL
);
207 memset(sub
, 0, sizeof(*sub
));
208 strncpy(sub
->desc
.name
, SPLAT_ATOMIC_NAME
, SPLAT_NAME_SIZE
);
209 strncpy(sub
->desc
.desc
, SPLAT_ATOMIC_DESC
, SPLAT_DESC_SIZE
);
210 INIT_LIST_HEAD(&sub
->subsystem_list
);
211 INIT_LIST_HEAD(&sub
->test_list
);
212 spin_lock_init(&sub
->test_lock
);
213 sub
->desc
.id
= SPLAT_SUBSYSTEM_ATOMIC
;
215 splat_test_init(sub
, SPLAT_ATOMIC_TEST1_NAME
, SPLAT_ATOMIC_TEST1_DESC
,
216 SPLAT_ATOMIC_TEST1_ID
, splat_atomic_test1
);
222 splat_atomic_fini(splat_subsystem_t
*sub
)
225 splat_test_fini(sub
, SPLAT_ATOMIC_TEST1_ID
);
231 splat_atomic_id(void) {
232 return SPLAT_SUBSYSTEM_ATOMIC
;