]> git.proxmox.com Git - mirror_spl.git/blame - module/splat/splat-atomic.c
Update SPLAT to use kmutex_t for portability
[mirror_spl.git] / module / splat / splat-atomic.c
CommitLineData
716154c5
BB
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>.
715f6251 6 * UCRL-CODE-235197
7 *
716154c5 8 * This file is part of the SPL, Solaris Porting Layer.
3d6af2dd 9 * For details, see <http://zfsonlinux.org/>.
716154c5
BB
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.
715f6251 15 *
716154c5 16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
715f6251 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
716154c5
BB
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting LAyer Tests (SPLAT) Atomic Tests.
25\*****************************************************************************/
715f6251 26
df870a69
BB
27#include <sys/atomic.h>
28#include <sys/thread.h>
0cb3dafc 29#include <sys/mutex.h>
79a7ab25 30#include <linux/slab.h>
9f4c835a 31#include "splat-internal.h"
32
9f4c835a 33#define SPLAT_ATOMIC_NAME "atomic"
34#define SPLAT_ATOMIC_DESC "Kernel Atomic Tests"
35
36#define SPLAT_ATOMIC_TEST1_ID 0x0b01
37#define SPLAT_ATOMIC_TEST1_NAME "64-bit"
38#define SPLAT_ATOMIC_TEST1_DESC "Validate 64-bit atomic ops"
39
40#define SPLAT_ATOMIC_TEST_MAGIC 0x43435454UL
41#define SPLAT_ATOMIC_INIT_VALUE 10000000UL
42
43typedef enum {
44 SPLAT_ATOMIC_INC_64 = 0,
45 SPLAT_ATOMIC_DEC_64 = 1,
46 SPLAT_ATOMIC_ADD_64 = 2,
47 SPLAT_ATOMIC_SUB_64 = 3,
48 SPLAT_ATOMIC_ADD_64_NV = 4,
49 SPLAT_ATOMIC_SUB_64_NV = 5,
50 SPLAT_ATOMIC_COUNT_64 = 6
51} atomic_op_t;
52
53typedef struct atomic_priv {
54 unsigned long ap_magic;
55 struct file *ap_file;
0cb3dafc 56 kmutex_t ap_lock;
9f4c835a 57 wait_queue_head_t ap_waitq;
58 volatile uint64_t ap_atomic;
59 volatile uint64_t ap_atomic_exited;
60 atomic_op_t ap_op;
61
62} atomic_priv_t;
63
64static void
65splat_atomic_work(void *priv)
66{
67 atomic_priv_t *ap;
68 atomic_op_t op;
69 int i;
70
71 ap = (atomic_priv_t *)priv;
72 ASSERT(ap->ap_magic == SPLAT_ATOMIC_TEST_MAGIC);
73
0cb3dafc 74 mutex_enter(&ap->ap_lock);
9f4c835a 75 op = ap->ap_op;
76 wake_up(&ap->ap_waitq);
0cb3dafc 77 mutex_exit(&ap->ap_lock);
9f4c835a 78
79 splat_vprint(ap->ap_file, SPLAT_ATOMIC_TEST1_NAME,
80 "Thread %d successfully started: %lu/%lu\n", op,
81 (long unsigned)ap->ap_atomic,
82 (long unsigned)ap->ap_atomic_exited);
83
84 for (i = 0; i < SPLAT_ATOMIC_INIT_VALUE / 10; i++) {
85
86 /* Periodically sleep to mix up the ordering */
87 if ((i % (SPLAT_ATOMIC_INIT_VALUE / 100)) == 0) {
88 splat_vprint(ap->ap_file, SPLAT_ATOMIC_TEST1_NAME,
89 "Thread %d sleeping: %lu/%lu\n", op,
90 (long unsigned)ap->ap_atomic,
91 (long unsigned)ap->ap_atomic_exited);
92 set_current_state(TASK_INTERRUPTIBLE);
93 schedule_timeout(HZ / 100);
94 }
95
96 switch (op) {
97 case SPLAT_ATOMIC_INC_64:
98 atomic_inc_64(&ap->ap_atomic);
99 break;
100 case SPLAT_ATOMIC_DEC_64:
101 atomic_dec_64(&ap->ap_atomic);
102 break;
103 case SPLAT_ATOMIC_ADD_64:
104 atomic_add_64(&ap->ap_atomic, 3);
105 break;
106 case SPLAT_ATOMIC_SUB_64:
107 atomic_sub_64(&ap->ap_atomic, 3);
108 break;
109 case SPLAT_ATOMIC_ADD_64_NV:
110 atomic_add_64_nv(&ap->ap_atomic, 5);
111 break;
112 case SPLAT_ATOMIC_SUB_64_NV:
113 atomic_sub_64_nv(&ap->ap_atomic, 5);
114 break;
115 default:
55abb092 116 PANIC("Undefined op %d\n", op);
9f4c835a 117 }
118 }
119
120 atomic_inc_64(&ap->ap_atomic_exited);
121
122 splat_vprint(ap->ap_file, SPLAT_ATOMIC_TEST1_NAME,
123 "Thread %d successfully exited: %lu/%lu\n", op,
124 (long unsigned)ap->ap_atomic,
125 (long unsigned)ap->ap_atomic_exited);
126
9f4c835a 127 wake_up(&ap->ap_waitq);
728b9dd8 128 thread_exit();
9f4c835a 129}
130
131static int
55c59e61 132splat_atomic_test1_cond(atomic_priv_t *ap, int started)
9f4c835a 133{
55c59e61 134 return (ap->ap_atomic_exited == started);
9f4c835a 135}
136
137static int
138splat_atomic_test1(struct file *file, void *arg)
139{
140 atomic_priv_t ap;
141 DEFINE_WAIT(wait);
142 kthread_t *thr;
55c59e61 143 int i, rc = 0;
9f4c835a 144
145 ap.ap_magic = SPLAT_ATOMIC_TEST_MAGIC;
146 ap.ap_file = file;
0cb3dafc 147 mutex_init(&ap.ap_lock, SPLAT_ATOMIC_TEST1_NAME, NULL, NULL);
9f4c835a 148 init_waitqueue_head(&ap.ap_waitq);
149 ap.ap_atomic = SPLAT_ATOMIC_INIT_VALUE;
150 ap.ap_atomic_exited = 0;
151
152 for (i = 0; i < SPLAT_ATOMIC_COUNT_64; i++) {
0cb3dafc 153 mutex_enter(&ap.ap_lock);
9f4c835a 154 ap.ap_op = i;
155
156 thr = (kthread_t *)thread_create(NULL, 0, splat_atomic_work,
157 &ap, 0, &p0, TS_RUN,
158 minclsyspri);
55c59e61 159 if (thr == NULL) {
160 rc = -ESRCH;
0cb3dafc 161 mutex_exit(&ap.ap_lock);
55c59e61 162 break;
163 }
9f4c835a 164
165 /* Prepare to wait, the new thread will wake us once it
166 * has made a copy of the unique private passed data */
167 prepare_to_wait(&ap.ap_waitq, &wait, TASK_UNINTERRUPTIBLE);
0cb3dafc 168 mutex_exit(&ap.ap_lock);
9f4c835a 169 schedule();
170 }
171
8f813bb1 172 wait_event(ap.ap_waitq, splat_atomic_test1_cond(&ap, i));
55c59e61 173
174 if (rc) {
175 splat_vprint(file, SPLAT_ATOMIC_TEST1_NAME, "Only started "
176 "%d/%d test threads\n", i, SPLAT_ATOMIC_COUNT_64);
177 return rc;
178 }
9f4c835a 179
180 if (ap.ap_atomic != SPLAT_ATOMIC_INIT_VALUE) {
181 splat_vprint(file, SPLAT_ATOMIC_TEST1_NAME,
182 "Final value %lu does not match initial value %lu\n",
183 (long unsigned)ap.ap_atomic, SPLAT_ATOMIC_INIT_VALUE);
184 return -EINVAL;
185 }
186
187 splat_vprint(file, SPLAT_ATOMIC_TEST1_NAME,
188 "Success initial and final values match, %lu == %lu\n",
189 (long unsigned)ap.ap_atomic, SPLAT_ATOMIC_INIT_VALUE);
190
0cb3dafc
BB
191 mutex_destroy(&ap.ap_lock);
192
9f4c835a 193 return 0;
194}
195
196splat_subsystem_t *
197splat_atomic_init(void)
198{
199 splat_subsystem_t *sub;
200
201 sub = kmalloc(sizeof(*sub), GFP_KERNEL);
202 if (sub == NULL)
203 return NULL;
204
205 memset(sub, 0, sizeof(*sub));
206 strncpy(sub->desc.name, SPLAT_ATOMIC_NAME, SPLAT_NAME_SIZE);
207 strncpy(sub->desc.desc, SPLAT_ATOMIC_DESC, SPLAT_DESC_SIZE);
208 INIT_LIST_HEAD(&sub->subsystem_list);
209 INIT_LIST_HEAD(&sub->test_list);
210 spin_lock_init(&sub->test_lock);
211 sub->desc.id = SPLAT_SUBSYSTEM_ATOMIC;
212
213 SPLAT_TEST_INIT(sub, SPLAT_ATOMIC_TEST1_NAME, SPLAT_ATOMIC_TEST1_DESC,
214 SPLAT_ATOMIC_TEST1_ID, splat_atomic_test1);
215
216 return sub;
217}
218
219void
220splat_atomic_fini(splat_subsystem_t *sub)
221{
222 ASSERT(sub);
223 SPLAT_TEST_FINI(sub, SPLAT_ATOMIC_TEST1_ID);
224
225 kfree(sub);
226}
227
228int
229splat_atomic_id(void) {
230 return SPLAT_SUBSYSTEM_ATOMIC;
231}