]> git.proxmox.com Git - mirror_spl-debian.git/blame - module/splat/splat-atomic.c
New upstream version 0.7.2
[mirror_spl-debian.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>
10946b02
AX
29#include <sys/mutex.h>
30#include <linux/mm_compat.h>
ec06701b 31#include <linux/wait_compat.h>
80093b6f 32#include <linux/slab.h>
9f4c835a 33#include "splat-internal.h"
34
9f4c835a 35#define SPLAT_ATOMIC_NAME "atomic"
36#define SPLAT_ATOMIC_DESC "Kernel Atomic Tests"
37
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"
41
42#define SPLAT_ATOMIC_TEST_MAGIC 0x43435454UL
43#define SPLAT_ATOMIC_INIT_VALUE 10000000UL
44
45typedef enum {
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
53} atomic_op_t;
54
55typedef struct atomic_priv {
56 unsigned long ap_magic;
57 struct file *ap_file;
10946b02 58 kmutex_t ap_lock;
ec06701b 59 spl_wait_queue_head_t ap_waitq;
9f4c835a 60 volatile uint64_t ap_atomic;
61 volatile uint64_t ap_atomic_exited;
62 atomic_op_t ap_op;
63
64} atomic_priv_t;
65
66static void
67splat_atomic_work(void *priv)
68{
69 atomic_priv_t *ap;
70 atomic_op_t op;
71 int i;
72
73 ap = (atomic_priv_t *)priv;
74 ASSERT(ap->ap_magic == SPLAT_ATOMIC_TEST_MAGIC);
75
10946b02 76 mutex_enter(&ap->ap_lock);
9f4c835a 77 op = ap->ap_op;
78 wake_up(&ap->ap_waitq);
10946b02 79 mutex_exit(&ap->ap_lock);
9f4c835a 80
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);
85
86 for (i = 0; i < SPLAT_ATOMIC_INIT_VALUE / 10; i++) {
87
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);
96 }
97
98 switch (op) {
99 case SPLAT_ATOMIC_INC_64:
100 atomic_inc_64(&ap->ap_atomic);
101 break;
102 case SPLAT_ATOMIC_DEC_64:
103 atomic_dec_64(&ap->ap_atomic);
104 break;
105 case SPLAT_ATOMIC_ADD_64:
106 atomic_add_64(&ap->ap_atomic, 3);
107 break;
108 case SPLAT_ATOMIC_SUB_64:
109 atomic_sub_64(&ap->ap_atomic, 3);
110 break;
111 case SPLAT_ATOMIC_ADD_64_NV:
112 atomic_add_64_nv(&ap->ap_atomic, 5);
113 break;
114 case SPLAT_ATOMIC_SUB_64_NV:
115 atomic_sub_64_nv(&ap->ap_atomic, 5);
116 break;
117 default:
55abb092 118 PANIC("Undefined op %d\n", op);
9f4c835a 119 }
120 }
121
122 atomic_inc_64(&ap->ap_atomic_exited);
123
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);
128
9f4c835a 129 wake_up(&ap->ap_waitq);
728b9dd8 130 thread_exit();
9f4c835a 131}
132
133static int
55c59e61 134splat_atomic_test1_cond(atomic_priv_t *ap, int started)
9f4c835a 135{
55c59e61 136 return (ap->ap_atomic_exited == started);
9f4c835a 137}
138
139static int
140splat_atomic_test1(struct file *file, void *arg)
141{
142 atomic_priv_t ap;
143 DEFINE_WAIT(wait);
144 kthread_t *thr;
55c59e61 145 int i, rc = 0;
9f4c835a 146
147 ap.ap_magic = SPLAT_ATOMIC_TEST_MAGIC;
148 ap.ap_file = file;
10946b02 149 mutex_init(&ap.ap_lock, SPLAT_ATOMIC_TEST1_NAME, MUTEX_DEFAULT, NULL);
9f4c835a 150 init_waitqueue_head(&ap.ap_waitq);
151 ap.ap_atomic = SPLAT_ATOMIC_INIT_VALUE;
152 ap.ap_atomic_exited = 0;
153
154 for (i = 0; i < SPLAT_ATOMIC_COUNT_64; i++) {
10946b02 155 mutex_enter(&ap.ap_lock);
9f4c835a 156 ap.ap_op = i;
157
158 thr = (kthread_t *)thread_create(NULL, 0, splat_atomic_work,
159 &ap, 0, &p0, TS_RUN,
f6188ddd 160 defclsyspri);
55c59e61 161 if (thr == NULL) {
162 rc = -ESRCH;
10946b02 163 mutex_exit(&ap.ap_lock);
55c59e61 164 break;
165 }
9f4c835a 166
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);
10946b02 170 mutex_exit(&ap.ap_lock);
9f4c835a 171 schedule();
172 }
173
8f813bb1 174 wait_event(ap.ap_waitq, splat_atomic_test1_cond(&ap, i));
55c59e61 175
176 if (rc) {
177 splat_vprint(file, SPLAT_ATOMIC_TEST1_NAME, "Only started "
178 "%d/%d test threads\n", i, SPLAT_ATOMIC_COUNT_64);
179 return rc;
180 }
9f4c835a 181
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);
186 return -EINVAL;
187 }
188
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);
192
10946b02
AX
193 mutex_destroy(&ap.ap_lock);
194
9f4c835a 195 return 0;
196}
197
198splat_subsystem_t *
199splat_atomic_init(void)
200{
201 splat_subsystem_t *sub;
202
203 sub = kmalloc(sizeof(*sub), GFP_KERNEL);
204 if (sub == NULL)
205 return NULL;
206
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;
214
ec06701b 215 splat_test_init(sub, SPLAT_ATOMIC_TEST1_NAME, SPLAT_ATOMIC_TEST1_DESC,
9f4c835a 216 SPLAT_ATOMIC_TEST1_ID, splat_atomic_test1);
217
218 return sub;
219}
220
221void
222splat_atomic_fini(splat_subsystem_t *sub)
223{
224 ASSERT(sub);
ec06701b 225 splat_test_fini(sub, SPLAT_ATOMIC_TEST1_ID);
9f4c835a 226
227 kfree(sub);
228}
229
230int
231splat_atomic_id(void) {
232 return SPLAT_SUBSYSTEM_ATOMIC;
233}