]> git.proxmox.com Git - mirror_spl-debian.git/blob - modules/splat/splat-atomic.c
Go through and add a header with the proper UCRL number.
[mirror_spl-debian.git] / modules / splat / splat-atomic.c
1 /*
2 * This file is part of the SPL: Solaris Porting Layer.
3 *
4 * Copyright (c) 2008 Lawrence Livermore National Security, LLC.
5 * Produced at Lawrence Livermore National Laboratory
6 * Written by:
7 * Brian Behlendorf <behlendorf1@llnl.gov>,
8 * Herb Wartens <wartens2@llnl.gov>,
9 * Jim Garlick <garlick@llnl.gov>
10 * UCRL-CODE-235197
11 *
12 * This is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
27 #include "splat-internal.h"
28
29 #define SPLAT_SUBSYSTEM_ATOMIC 0x0b00
30 #define SPLAT_ATOMIC_NAME "atomic"
31 #define SPLAT_ATOMIC_DESC "Kernel Atomic Tests"
32
33 #define SPLAT_ATOMIC_TEST1_ID 0x0b01
34 #define SPLAT_ATOMIC_TEST1_NAME "64-bit"
35 #define SPLAT_ATOMIC_TEST1_DESC "Validate 64-bit atomic ops"
36
37 #define SPLAT_ATOMIC_TEST_MAGIC 0x43435454UL
38 #define SPLAT_ATOMIC_INIT_VALUE 10000000UL
39
40 typedef enum {
41 SPLAT_ATOMIC_INC_64 = 0,
42 SPLAT_ATOMIC_DEC_64 = 1,
43 SPLAT_ATOMIC_ADD_64 = 2,
44 SPLAT_ATOMIC_SUB_64 = 3,
45 SPLAT_ATOMIC_ADD_64_NV = 4,
46 SPLAT_ATOMIC_SUB_64_NV = 5,
47 SPLAT_ATOMIC_COUNT_64 = 6
48 } atomic_op_t;
49
50 typedef struct atomic_priv {
51 unsigned long ap_magic;
52 struct file *ap_file;
53 spinlock_t ap_lock;
54 wait_queue_head_t ap_waitq;
55 volatile uint64_t ap_atomic;
56 volatile uint64_t ap_atomic_exited;
57 atomic_op_t ap_op;
58
59 } atomic_priv_t;
60
61 static void
62 splat_atomic_work(void *priv)
63 {
64 atomic_priv_t *ap;
65 atomic_op_t op;
66 int i;
67
68 ap = (atomic_priv_t *)priv;
69 ASSERT(ap->ap_magic == SPLAT_ATOMIC_TEST_MAGIC);
70
71 spin_lock(&ap->ap_lock);
72 op = ap->ap_op;
73 wake_up(&ap->ap_waitq);
74 spin_unlock(&ap->ap_lock);
75
76 splat_vprint(ap->ap_file, SPLAT_ATOMIC_TEST1_NAME,
77 "Thread %d successfully started: %lu/%lu\n", op,
78 (long unsigned)ap->ap_atomic,
79 (long unsigned)ap->ap_atomic_exited);
80
81 for (i = 0; i < SPLAT_ATOMIC_INIT_VALUE / 10; i++) {
82
83 /* Periodically sleep to mix up the ordering */
84 if ((i % (SPLAT_ATOMIC_INIT_VALUE / 100)) == 0) {
85 splat_vprint(ap->ap_file, SPLAT_ATOMIC_TEST1_NAME,
86 "Thread %d sleeping: %lu/%lu\n", op,
87 (long unsigned)ap->ap_atomic,
88 (long unsigned)ap->ap_atomic_exited);
89 set_current_state(TASK_INTERRUPTIBLE);
90 schedule_timeout(HZ / 100);
91 }
92
93 switch (op) {
94 case SPLAT_ATOMIC_INC_64:
95 atomic_inc_64(&ap->ap_atomic);
96 break;
97 case SPLAT_ATOMIC_DEC_64:
98 atomic_dec_64(&ap->ap_atomic);
99 break;
100 case SPLAT_ATOMIC_ADD_64:
101 atomic_add_64(&ap->ap_atomic, 3);
102 break;
103 case SPLAT_ATOMIC_SUB_64:
104 atomic_sub_64(&ap->ap_atomic, 3);
105 break;
106 case SPLAT_ATOMIC_ADD_64_NV:
107 atomic_add_64_nv(&ap->ap_atomic, 5);
108 break;
109 case SPLAT_ATOMIC_SUB_64_NV:
110 atomic_sub_64_nv(&ap->ap_atomic, 5);
111 break;
112 default:
113 BUG_ON(1);
114 }
115 }
116
117 atomic_inc_64(&ap->ap_atomic_exited);
118
119 splat_vprint(ap->ap_file, SPLAT_ATOMIC_TEST1_NAME,
120 "Thread %d successfully exited: %lu/%lu\n", op,
121 (long unsigned)ap->ap_atomic,
122 (long unsigned)ap->ap_atomic_exited);
123
124 wake_up(&ap->ap_waitq);
125 thread_exit();
126 }
127
128 static int
129 splat_atomic_test1_cond(atomic_priv_t *ap)
130 {
131 return (ap->ap_atomic_exited == SPLAT_ATOMIC_COUNT_64);
132 }
133
134 static int
135 splat_atomic_test1(struct file *file, void *arg)
136 {
137 atomic_priv_t ap;
138 DEFINE_WAIT(wait);
139 kthread_t *thr;
140 int i;
141
142 ap.ap_magic = SPLAT_ATOMIC_TEST_MAGIC;
143 ap.ap_file = file;
144 spin_lock_init(&ap.ap_lock);
145 init_waitqueue_head(&ap.ap_waitq);
146 ap.ap_atomic = SPLAT_ATOMIC_INIT_VALUE;
147 ap.ap_atomic_exited = 0;
148
149 for (i = 0; i < SPLAT_ATOMIC_COUNT_64; i++) {
150 spin_lock(&ap.ap_lock);
151 ap.ap_op = i;
152
153 thr = (kthread_t *)thread_create(NULL, 0, splat_atomic_work,
154 &ap, 0, &p0, TS_RUN,
155 minclsyspri);
156 BUG_ON(thr == NULL);
157
158 /* Prepare to wait, the new thread will wake us once it
159 * has made a copy of the unique private passed data */
160 prepare_to_wait(&ap.ap_waitq, &wait, TASK_UNINTERRUPTIBLE);
161 spin_unlock(&ap.ap_lock);
162 schedule();
163 }
164
165 wait_event_interruptible(ap.ap_waitq, splat_atomic_test1_cond(&ap));
166
167 if (ap.ap_atomic != SPLAT_ATOMIC_INIT_VALUE) {
168 splat_vprint(file, SPLAT_ATOMIC_TEST1_NAME,
169 "Final value %lu does not match initial value %lu\n",
170 (long unsigned)ap.ap_atomic, SPLAT_ATOMIC_INIT_VALUE);
171 return -EINVAL;
172 }
173
174 splat_vprint(file, SPLAT_ATOMIC_TEST1_NAME,
175 "Success initial and final values match, %lu == %lu\n",
176 (long unsigned)ap.ap_atomic, SPLAT_ATOMIC_INIT_VALUE);
177
178 return 0;
179 }
180
181 splat_subsystem_t *
182 splat_atomic_init(void)
183 {
184 splat_subsystem_t *sub;
185
186 sub = kmalloc(sizeof(*sub), GFP_KERNEL);
187 if (sub == NULL)
188 return NULL;
189
190 memset(sub, 0, sizeof(*sub));
191 strncpy(sub->desc.name, SPLAT_ATOMIC_NAME, SPLAT_NAME_SIZE);
192 strncpy(sub->desc.desc, SPLAT_ATOMIC_DESC, SPLAT_DESC_SIZE);
193 INIT_LIST_HEAD(&sub->subsystem_list);
194 INIT_LIST_HEAD(&sub->test_list);
195 spin_lock_init(&sub->test_lock);
196 sub->desc.id = SPLAT_SUBSYSTEM_ATOMIC;
197
198 SPLAT_TEST_INIT(sub, SPLAT_ATOMIC_TEST1_NAME, SPLAT_ATOMIC_TEST1_DESC,
199 SPLAT_ATOMIC_TEST1_ID, splat_atomic_test1);
200
201 return sub;
202 }
203
204 void
205 splat_atomic_fini(splat_subsystem_t *sub)
206 {
207 ASSERT(sub);
208 SPLAT_TEST_FINI(sub, SPLAT_ATOMIC_TEST1_ID);
209
210 kfree(sub);
211 }
212
213 int
214 splat_atomic_id(void) {
215 return SPLAT_SUBSYSTEM_ATOMIC;
216 }