]>
Commit | Line | Data |
---|---|---|
87d546d8 TG |
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> | |
1681e039 | 31 | #include <linux/wait_compat.h> |
87d546d8 TG |
32 | #include <linux/slab.h> |
33 | #include "splat-internal.h" | |
34 | ||
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 | ||
45 | typedef 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 | ||
55 | typedef struct atomic_priv { | |
56 | unsigned long ap_magic; | |
57 | struct file *ap_file; | |
58 | kmutex_t ap_lock; | |
1681e039 | 59 | spl_wait_queue_head_t ap_waitq; |
87d546d8 TG |
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 | ||
66 | static void | |
67 | splat_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 | ||
76 | mutex_enter(&ap->ap_lock); | |
77 | op = ap->ap_op; | |
78 | wake_up(&ap->ap_waitq); | |
79 | mutex_exit(&ap->ap_lock); | |
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: | |
118 | PANIC("Undefined op %d\n", op); | |
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 | ||
129 | wake_up(&ap->ap_waitq); | |
130 | thread_exit(); | |
131 | } | |
132 | ||
133 | static int | |
134 | splat_atomic_test1_cond(atomic_priv_t *ap, int started) | |
135 | { | |
136 | return (ap->ap_atomic_exited == started); | |
137 | } | |
138 | ||
139 | static int | |
140 | splat_atomic_test1(struct file *file, void *arg) | |
141 | { | |
142 | atomic_priv_t ap; | |
143 | DEFINE_WAIT(wait); | |
144 | kthread_t *thr; | |
145 | int i, rc = 0; | |
146 | ||
147 | ap.ap_magic = SPLAT_ATOMIC_TEST_MAGIC; | |
148 | ap.ap_file = file; | |
149 | mutex_init(&ap.ap_lock, SPLAT_ATOMIC_TEST1_NAME, MUTEX_DEFAULT, NULL); | |
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++) { | |
155 | mutex_enter(&ap.ap_lock); | |
156 | ap.ap_op = i; | |
157 | ||
158 | thr = (kthread_t *)thread_create(NULL, 0, splat_atomic_work, | |
159 | &ap, 0, &p0, TS_RUN, | |
160 | defclsyspri); | |
161 | if (thr == NULL) { | |
162 | rc = -ESRCH; | |
163 | mutex_exit(&ap.ap_lock); | |
164 | break; | |
165 | } | |
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); | |
170 | mutex_exit(&ap.ap_lock); | |
171 | schedule(); | |
172 | } | |
173 | ||
174 | wait_event(ap.ap_waitq, splat_atomic_test1_cond(&ap, i)); | |
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 | } | |
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 | ||
193 | mutex_destroy(&ap.ap_lock); | |
194 | ||
195 | return 0; | |
196 | } | |
197 | ||
198 | splat_subsystem_t * | |
199 | splat_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 | ||
215 | SPLAT_TEST_INIT(sub, SPLAT_ATOMIC_TEST1_NAME, SPLAT_ATOMIC_TEST1_DESC, | |
216 | SPLAT_ATOMIC_TEST1_ID, splat_atomic_test1); | |
217 | ||
218 | return sub; | |
219 | } | |
220 | ||
221 | void | |
222 | splat_atomic_fini(splat_subsystem_t *sub) | |
223 | { | |
224 | ASSERT(sub); | |
225 | SPLAT_TEST_FINI(sub, SPLAT_ATOMIC_TEST1_ID); | |
226 | ||
227 | kfree(sub); | |
228 | } | |
229 | ||
230 | int | |
231 | splat_atomic_id(void) { | |
232 | return SPLAT_SUBSYSTEM_ATOMIC; | |
233 | } |