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 <linux/slab.h>
30 #include "splat-internal.h"
32 #define SPLAT_ATOMIC_NAME "atomic"
33 #define SPLAT_ATOMIC_DESC "Kernel Atomic Tests"
35 #define SPLAT_ATOMIC_TEST1_ID 0x0b01
36 #define SPLAT_ATOMIC_TEST1_NAME "64-bit"
37 #define SPLAT_ATOMIC_TEST1_DESC "Validate 64-bit atomic ops"
39 #define SPLAT_ATOMIC_TEST_MAGIC 0x43435454UL
40 #define SPLAT_ATOMIC_INIT_VALUE 10000000UL
43 SPLAT_ATOMIC_INC_64
= 0,
44 SPLAT_ATOMIC_DEC_64
= 1,
45 SPLAT_ATOMIC_ADD_64
= 2,
46 SPLAT_ATOMIC_SUB_64
= 3,
47 SPLAT_ATOMIC_ADD_64_NV
= 4,
48 SPLAT_ATOMIC_SUB_64_NV
= 5,
49 SPLAT_ATOMIC_COUNT_64
= 6
52 typedef struct atomic_priv
{
53 unsigned long ap_magic
;
56 wait_queue_head_t ap_waitq
;
57 volatile uint64_t ap_atomic
;
58 volatile uint64_t ap_atomic_exited
;
64 splat_atomic_work(void *priv
)
70 ap
= (atomic_priv_t
*)priv
;
71 ASSERT(ap
->ap_magic
== SPLAT_ATOMIC_TEST_MAGIC
);
73 mutex_lock(&ap
->ap_lock
);
75 wake_up(&ap
->ap_waitq
);
76 mutex_unlock(&ap
->ap_lock
);
78 splat_vprint(ap
->ap_file
, SPLAT_ATOMIC_TEST1_NAME
,
79 "Thread %d successfully started: %lu/%lu\n", op
,
80 (long unsigned)ap
->ap_atomic
,
81 (long unsigned)ap
->ap_atomic_exited
);
83 for (i
= 0; i
< SPLAT_ATOMIC_INIT_VALUE
/ 10; i
++) {
85 /* Periodically sleep to mix up the ordering */
86 if ((i
% (SPLAT_ATOMIC_INIT_VALUE
/ 100)) == 0) {
87 splat_vprint(ap
->ap_file
, SPLAT_ATOMIC_TEST1_NAME
,
88 "Thread %d sleeping: %lu/%lu\n", op
,
89 (long unsigned)ap
->ap_atomic
,
90 (long unsigned)ap
->ap_atomic_exited
);
91 set_current_state(TASK_INTERRUPTIBLE
);
92 schedule_timeout(HZ
/ 100);
96 case SPLAT_ATOMIC_INC_64
:
97 atomic_inc_64(&ap
->ap_atomic
);
99 case SPLAT_ATOMIC_DEC_64
:
100 atomic_dec_64(&ap
->ap_atomic
);
102 case SPLAT_ATOMIC_ADD_64
:
103 atomic_add_64(&ap
->ap_atomic
, 3);
105 case SPLAT_ATOMIC_SUB_64
:
106 atomic_sub_64(&ap
->ap_atomic
, 3);
108 case SPLAT_ATOMIC_ADD_64_NV
:
109 atomic_add_64_nv(&ap
->ap_atomic
, 5);
111 case SPLAT_ATOMIC_SUB_64_NV
:
112 atomic_sub_64_nv(&ap
->ap_atomic
, 5);
115 PANIC("Undefined op %d\n", op
);
119 atomic_inc_64(&ap
->ap_atomic_exited
);
121 splat_vprint(ap
->ap_file
, SPLAT_ATOMIC_TEST1_NAME
,
122 "Thread %d successfully exited: %lu/%lu\n", op
,
123 (long unsigned)ap
->ap_atomic
,
124 (long unsigned)ap
->ap_atomic_exited
);
126 wake_up(&ap
->ap_waitq
);
131 splat_atomic_test1_cond(atomic_priv_t
*ap
, int started
)
133 return (ap
->ap_atomic_exited
== started
);
137 splat_atomic_test1(struct file
*file
, void *arg
)
144 ap
.ap_magic
= SPLAT_ATOMIC_TEST_MAGIC
;
146 mutex_init(&ap
.ap_lock
);
147 init_waitqueue_head(&ap
.ap_waitq
);
148 ap
.ap_atomic
= SPLAT_ATOMIC_INIT_VALUE
;
149 ap
.ap_atomic_exited
= 0;
151 for (i
= 0; i
< SPLAT_ATOMIC_COUNT_64
; i
++) {
152 mutex_lock(&ap
.ap_lock
);
155 thr
= (kthread_t
*)thread_create(NULL
, 0, splat_atomic_work
,
160 mutex_unlock(&ap
.ap_lock
);
164 /* Prepare to wait, the new thread will wake us once it
165 * has made a copy of the unique private passed data */
166 prepare_to_wait(&ap
.ap_waitq
, &wait
, TASK_UNINTERRUPTIBLE
);
167 mutex_unlock(&ap
.ap_lock
);
171 wait_event(ap
.ap_waitq
, splat_atomic_test1_cond(&ap
, i
));
174 splat_vprint(file
, SPLAT_ATOMIC_TEST1_NAME
, "Only started "
175 "%d/%d test threads\n", i
, SPLAT_ATOMIC_COUNT_64
);
179 if (ap
.ap_atomic
!= SPLAT_ATOMIC_INIT_VALUE
) {
180 splat_vprint(file
, SPLAT_ATOMIC_TEST1_NAME
,
181 "Final value %lu does not match initial value %lu\n",
182 (long unsigned)ap
.ap_atomic
, SPLAT_ATOMIC_INIT_VALUE
);
186 splat_vprint(file
, SPLAT_ATOMIC_TEST1_NAME
,
187 "Success initial and final values match, %lu == %lu\n",
188 (long unsigned)ap
.ap_atomic
, SPLAT_ATOMIC_INIT_VALUE
);
194 splat_atomic_init(void)
196 splat_subsystem_t
*sub
;
198 sub
= kmalloc(sizeof(*sub
), GFP_KERNEL
);
202 memset(sub
, 0, sizeof(*sub
));
203 strncpy(sub
->desc
.name
, SPLAT_ATOMIC_NAME
, SPLAT_NAME_SIZE
);
204 strncpy(sub
->desc
.desc
, SPLAT_ATOMIC_DESC
, SPLAT_DESC_SIZE
);
205 INIT_LIST_HEAD(&sub
->subsystem_list
);
206 INIT_LIST_HEAD(&sub
->test_list
);
207 spin_lock_init(&sub
->test_lock
);
208 sub
->desc
.id
= SPLAT_SUBSYSTEM_ATOMIC
;
210 SPLAT_TEST_INIT(sub
, SPLAT_ATOMIC_TEST1_NAME
, SPLAT_ATOMIC_TEST1_DESC
,
211 SPLAT_ATOMIC_TEST1_ID
, splat_atomic_test1
);
217 splat_atomic_fini(splat_subsystem_t
*sub
)
220 SPLAT_TEST_FINI(sub
, SPLAT_ATOMIC_TEST1_ID
);
226 splat_atomic_id(void) {
227 return SPLAT_SUBSYSTEM_ATOMIC
;