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