]>
Commit | Line | Data |
---|---|---|
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) Thread Tests. | |
25 | \*****************************************************************************/ | |
715f6251 | 26 | |
df870a69 BB |
27 | #include <sys/thread.h> |
28 | #include <sys/random.h> | |
10946b02 AX |
29 | #include <linux/delay.h> |
30 | #include <linux/mm_compat.h> | |
80093b6f | 31 | #include <linux/slab.h> |
7c50328b | 32 | #include "splat-internal.h" |
f1ca4da6 | 33 | |
7c50328b | 34 | #define SPLAT_THREAD_NAME "thread" |
35 | #define SPLAT_THREAD_DESC "Kernel Thread Tests" | |
f1ca4da6 | 36 | |
7c50328b | 37 | #define SPLAT_THREAD_TEST1_ID 0x0601 |
38 | #define SPLAT_THREAD_TEST1_NAME "create" | |
115aed0d | 39 | #define SPLAT_THREAD_TEST1_DESC "Validate thread creation" |
40 | ||
41 | #define SPLAT_THREAD_TEST2_ID 0x0602 | |
42 | #define SPLAT_THREAD_TEST2_NAME "exit" | |
43 | #define SPLAT_THREAD_TEST2_DESC "Validate thread exit" | |
f1ca4da6 | 44 | |
b7dc3138 BB |
45 | #define SPLAT_THREAD_TEST3_ID 0x6003 |
46 | #define SPLAT_THREAD_TEST3_NAME "tsd" | |
47 | #define SPLAT_THREAD_TEST3_DESC "Validate thread specific data" | |
48 | ||
7c50328b | 49 | #define SPLAT_THREAD_TEST_MAGIC 0x4488CC00UL |
b7dc3138 BB |
50 | #define SPLAT_THREAD_TEST_KEYS 32 |
51 | #define SPLAT_THREAD_TEST_THREADS 16 | |
f1ca4da6 | 52 | |
53 | typedef struct thread_priv { | |
54 | unsigned long tp_magic; | |
55 | struct file *tp_file; | |
56 | spinlock_t tp_lock; | |
57 | wait_queue_head_t tp_waitq; | |
b7dc3138 | 58 | uint_t tp_keys[SPLAT_THREAD_TEST_KEYS]; |
f1ca4da6 | 59 | int tp_rc; |
b7dc3138 BB |
60 | int tp_count; |
61 | int tp_dtor_count; | |
f1ca4da6 | 62 | } thread_priv_t; |
63 | ||
115aed0d | 64 | static int |
65 | splat_thread_rc(thread_priv_t *tp, int rc) | |
66 | { | |
67 | int ret; | |
68 | ||
69 | spin_lock(&tp->tp_lock); | |
70 | ret = (tp->tp_rc == rc); | |
71 | spin_unlock(&tp->tp_lock); | |
72 | ||
73 | return ret; | |
74 | } | |
f1ca4da6 | 75 | |
b7dc3138 BB |
76 | static int |
77 | splat_thread_count(thread_priv_t *tp, int count) | |
78 | { | |
79 | int ret; | |
80 | ||
81 | spin_lock(&tp->tp_lock); | |
82 | ret = (tp->tp_count == count); | |
83 | spin_unlock(&tp->tp_lock); | |
84 | ||
85 | return ret; | |
86 | } | |
87 | ||
f1ca4da6 | 88 | static void |
115aed0d | 89 | splat_thread_work1(void *priv) |
f1ca4da6 | 90 | { |
91 | thread_priv_t *tp = (thread_priv_t *)priv; | |
92 | ||
93 | spin_lock(&tp->tp_lock); | |
7c50328b | 94 | ASSERT(tp->tp_magic == SPLAT_THREAD_TEST_MAGIC); |
f1ca4da6 | 95 | tp->tp_rc = 1; |
616df2dd | 96 | wake_up(&tp->tp_waitq); |
f1ca4da6 | 97 | spin_unlock(&tp->tp_lock); |
f1ca4da6 | 98 | |
99 | thread_exit(); | |
100 | } | |
101 | ||
102 | static int | |
7c50328b | 103 | splat_thread_test1(struct file *file, void *arg) |
f1ca4da6 | 104 | { |
105 | thread_priv_t tp; | |
f1ca4da6 | 106 | kthread_t *thr; |
f1ca4da6 | 107 | |
7c50328b | 108 | tp.tp_magic = SPLAT_THREAD_TEST_MAGIC; |
f1ca4da6 | 109 | tp.tp_file = file; |
110 | spin_lock_init(&tp.tp_lock); | |
111 | init_waitqueue_head(&tp.tp_waitq); | |
112 | tp.tp_rc = 0; | |
113 | ||
115aed0d | 114 | thr = (kthread_t *)thread_create(NULL, 0, splat_thread_work1, &tp, 0, |
8095473b | 115 | &p0, TS_RUN, defclsyspri); |
115aed0d | 116 | /* Must never fail under Solaris, but we check anyway since this |
117 | * can happen in the linux SPL, we may want to change this behavior */ | |
118 | if (thr == NULL) | |
119 | return -ESRCH; | |
f1ca4da6 | 120 | |
115aed0d | 121 | /* Sleep until the thread sets tp.tp_rc == 1 */ |
122 | wait_event(tp.tp_waitq, splat_thread_rc(&tp, 1)); | |
f1ca4da6 | 123 | |
7c50328b | 124 | splat_vprint(file, SPLAT_THREAD_TEST1_NAME, "%s", |
115aed0d | 125 | "Thread successfully started properly\n"); |
126 | return 0; | |
127 | } | |
128 | ||
129 | static void | |
130 | splat_thread_work2(void *priv) | |
131 | { | |
132 | thread_priv_t *tp = (thread_priv_t *)priv; | |
133 | ||
134 | spin_lock(&tp->tp_lock); | |
135 | ASSERT(tp->tp_magic == SPLAT_THREAD_TEST_MAGIC); | |
136 | tp->tp_rc = 1; | |
616df2dd | 137 | wake_up(&tp->tp_waitq); |
115aed0d | 138 | spin_unlock(&tp->tp_lock); |
139 | ||
115aed0d | 140 | thread_exit(); |
141 | ||
142 | /* The following code is unreachable when thread_exit() is | |
143 | * working properly, which is exactly what we're testing */ | |
144 | spin_lock(&tp->tp_lock); | |
145 | tp->tp_rc = 2; | |
115aed0d | 146 | wake_up(&tp->tp_waitq); |
616df2dd | 147 | spin_unlock(&tp->tp_lock); |
115aed0d | 148 | } |
149 | ||
150 | static int | |
151 | splat_thread_test2(struct file *file, void *arg) | |
152 | { | |
153 | thread_priv_t tp; | |
115aed0d | 154 | kthread_t *thr; |
155 | int rc = 0; | |
156 | ||
157 | tp.tp_magic = SPLAT_THREAD_TEST_MAGIC; | |
158 | tp.tp_file = file; | |
159 | spin_lock_init(&tp.tp_lock); | |
160 | init_waitqueue_head(&tp.tp_waitq); | |
161 | tp.tp_rc = 0; | |
162 | ||
163 | thr = (kthread_t *)thread_create(NULL, 0, splat_thread_work2, &tp, 0, | |
8095473b | 164 | &p0, TS_RUN, defclsyspri); |
115aed0d | 165 | /* Must never fail under Solaris, but we check anyway since this |
166 | * can happen in the linux SPL, we may want to change this behavior */ | |
167 | if (thr == NULL) | |
b7dc3138 | 168 | return -ESRCH; |
115aed0d | 169 | |
170 | /* Sleep until the thread sets tp.tp_rc == 1 */ | |
171 | wait_event(tp.tp_waitq, splat_thread_rc(&tp, 1)); | |
172 | ||
173 | /* Sleep until the thread sets tp.tp_rc == 2, or until we hit | |
174 | * the timeout. If thread exit is working properly we should | |
175 | * hit the timeout and never see to.tp_rc == 2. */ | |
176 | rc = wait_event_timeout(tp.tp_waitq, splat_thread_rc(&tp, 2), HZ / 10); | |
177 | if (rc > 0) { | |
178 | rc = -EINVAL; | |
179 | splat_vprint(file, SPLAT_THREAD_TEST2_NAME, "%s", | |
180 | "Thread did not exit properly at thread_exit()\n"); | |
181 | } else { | |
182 | splat_vprint(file, SPLAT_THREAD_TEST2_NAME, "%s", | |
183 | "Thread successfully exited at thread_exit()\n"); | |
184 | } | |
f1ca4da6 | 185 | |
186 | return rc; | |
187 | } | |
188 | ||
b7dc3138 BB |
189 | static void |
190 | splat_thread_work3_common(thread_priv_t *tp) | |
191 | { | |
192 | ulong_t rnd; | |
193 | int i, rc = 0; | |
194 | ||
195 | /* set a unique value for each key using a random value */ | |
196 | get_random_bytes((void *)&rnd, 4); | |
197 | for (i = 0; i < SPLAT_THREAD_TEST_KEYS; i++) | |
198 | tsd_set(tp->tp_keys[i], (void *)(i + rnd)); | |
199 | ||
200 | /* verify the unique value for each key */ | |
201 | for (i = 0; i < SPLAT_THREAD_TEST_KEYS; i++) | |
202 | if (tsd_get(tp->tp_keys[i]) != (void *)(i + rnd)) | |
203 | rc = -EINVAL; | |
204 | ||
205 | /* set the value to thread_priv_t for use by the destructor */ | |
206 | for (i = 0; i < SPLAT_THREAD_TEST_KEYS; i++) | |
207 | tsd_set(tp->tp_keys[i], (void *)tp); | |
208 | ||
209 | spin_lock(&tp->tp_lock); | |
210 | if (rc && !tp->tp_rc) | |
211 | tp->tp_rc = rc; | |
212 | ||
213 | tp->tp_count++; | |
214 | wake_up_all(&tp->tp_waitq); | |
215 | spin_unlock(&tp->tp_lock); | |
216 | } | |
217 | ||
218 | static void | |
219 | splat_thread_work3_wait(void *priv) | |
220 | { | |
221 | thread_priv_t *tp = (thread_priv_t *)priv; | |
222 | ||
223 | ASSERT(tp->tp_magic == SPLAT_THREAD_TEST_MAGIC); | |
224 | splat_thread_work3_common(tp); | |
225 | wait_event(tp->tp_waitq, splat_thread_count(tp, 0)); | |
226 | thread_exit(); | |
227 | } | |
228 | ||
229 | static void | |
230 | splat_thread_work3_exit(void *priv) | |
231 | { | |
232 | thread_priv_t *tp = (thread_priv_t *)priv; | |
233 | ||
234 | ASSERT(tp->tp_magic == SPLAT_THREAD_TEST_MAGIC); | |
235 | splat_thread_work3_common(tp); | |
236 | thread_exit(); | |
237 | } | |
238 | ||
239 | static void | |
240 | splat_thread_dtor3(void *priv) | |
241 | { | |
242 | thread_priv_t *tp = (thread_priv_t *)priv; | |
243 | ||
244 | ASSERT(tp->tp_magic == SPLAT_THREAD_TEST_MAGIC); | |
245 | spin_lock(&tp->tp_lock); | |
246 | tp->tp_dtor_count++; | |
247 | spin_unlock(&tp->tp_lock); | |
248 | } | |
249 | ||
250 | /* | |
251 | * Create threads which set and verify SPLAT_THREAD_TEST_KEYS number of | |
252 | * keys. These threads may then exit by calling thread_exit() which calls | |
253 | * tsd_exit() resulting in all their thread specific data being reclaimed. | |
254 | * Alternately, the thread may block in which case the thread specific | |
255 | * data will be reclaimed as part of tsd_destroy(). In either case all | |
256 | * thread specific data must be reclaimed, this is verified by ensuring | |
257 | * the registered destructor is called the correct number of times. | |
258 | */ | |
259 | static int | |
260 | splat_thread_test3(struct file *file, void *arg) | |
261 | { | |
262 | int i, rc = 0, expected, wait_count = 0, exit_count = 0; | |
263 | thread_priv_t tp; | |
264 | ||
265 | tp.tp_magic = SPLAT_THREAD_TEST_MAGIC; | |
266 | tp.tp_file = file; | |
267 | spin_lock_init(&tp.tp_lock); | |
268 | init_waitqueue_head(&tp.tp_waitq); | |
269 | tp.tp_rc = 0; | |
270 | tp.tp_count = 0; | |
271 | tp.tp_dtor_count = 0; | |
272 | ||
273 | for (i = 0; i < SPLAT_THREAD_TEST_KEYS; i++) { | |
274 | tp.tp_keys[i] = 0; | |
275 | tsd_create(&tp.tp_keys[i], splat_thread_dtor3); | |
276 | } | |
277 | ||
278 | /* Start tsd wait threads */ | |
279 | for (i = 0; i < SPLAT_THREAD_TEST_THREADS; i++) { | |
280 | if (thread_create(NULL, 0, splat_thread_work3_wait, | |
8095473b | 281 | &tp, 0, &p0, TS_RUN, defclsyspri)) |
b7dc3138 BB |
282 | wait_count++; |
283 | } | |
284 | ||
285 | /* All wait threads have setup their tsd and are blocking. */ | |
286 | wait_event(tp.tp_waitq, splat_thread_count(&tp, wait_count)); | |
287 | ||
288 | if (tp.tp_dtor_count != 0) { | |
289 | splat_vprint(file, SPLAT_THREAD_TEST3_NAME, | |
290 | "Prematurely ran %d tsd destructors\n", tp.tp_dtor_count); | |
291 | if (!rc) | |
292 | rc = -ERANGE; | |
293 | } | |
294 | ||
295 | /* Start tsd exit threads */ | |
296 | for (i = 0; i < SPLAT_THREAD_TEST_THREADS; i++) { | |
297 | if (thread_create(NULL, 0, splat_thread_work3_exit, | |
8095473b | 298 | &tp, 0, &p0, TS_RUN, defclsyspri)) |
b7dc3138 BB |
299 | exit_count++; |
300 | } | |
301 | ||
302 | /* All exit threads verified tsd and are in the process of exiting */ | |
303 | wait_event(tp.tp_waitq,splat_thread_count(&tp, wait_count+exit_count)); | |
304 | msleep(500); | |
305 | ||
306 | expected = (SPLAT_THREAD_TEST_KEYS * exit_count); | |
307 | if (tp.tp_dtor_count != expected) { | |
308 | splat_vprint(file, SPLAT_THREAD_TEST3_NAME, | |
309 | "Expected %d exit tsd destructors but saw %d\n", | |
310 | expected, tp.tp_dtor_count); | |
311 | if (!rc) | |
312 | rc = -ERANGE; | |
313 | } | |
314 | ||
315 | /* Destroy all keys and associated tsd in blocked threads */ | |
316 | for (i = 0; i < SPLAT_THREAD_TEST_KEYS; i++) | |
317 | tsd_destroy(&tp.tp_keys[i]); | |
318 | ||
319 | expected = (SPLAT_THREAD_TEST_KEYS * (exit_count + wait_count)); | |
320 | if (tp.tp_dtor_count != expected) { | |
321 | splat_vprint(file, SPLAT_THREAD_TEST3_NAME, | |
322 | "Expected %d wait+exit tsd destructors but saw %d\n", | |
323 | expected, tp.tp_dtor_count); | |
324 | if (!rc) | |
325 | rc = -ERANGE; | |
326 | } | |
327 | ||
328 | /* Release the remaining wait threads, sleep briefly while they exit */ | |
329 | spin_lock(&tp.tp_lock); | |
330 | tp.tp_count = 0; | |
331 | wake_up_all(&tp.tp_waitq); | |
332 | spin_unlock(&tp.tp_lock); | |
333 | msleep(500); | |
334 | ||
335 | if (tp.tp_rc) { | |
336 | splat_vprint(file, SPLAT_THREAD_TEST3_NAME, | |
337 | "Thread tsd_get()/tsd_set() error %d\n", tp.tp_rc); | |
338 | if (!rc) | |
339 | rc = tp.tp_rc; | |
340 | } else if (!rc) { | |
341 | splat_vprint(file, SPLAT_THREAD_TEST3_NAME, "%s", | |
342 | "Thread specific data verified\n"); | |
343 | } | |
344 | ||
345 | return rc; | |
346 | } | |
347 | ||
7c50328b | 348 | splat_subsystem_t * |
349 | splat_thread_init(void) | |
f1ca4da6 | 350 | { |
7c50328b | 351 | splat_subsystem_t *sub; |
f1ca4da6 | 352 | |
353 | sub = kmalloc(sizeof(*sub), GFP_KERNEL); | |
354 | if (sub == NULL) | |
355 | return NULL; | |
356 | ||
357 | memset(sub, 0, sizeof(*sub)); | |
7c50328b | 358 | strncpy(sub->desc.name, SPLAT_THREAD_NAME, SPLAT_NAME_SIZE); |
359 | strncpy(sub->desc.desc, SPLAT_THREAD_DESC, SPLAT_DESC_SIZE); | |
f1ca4da6 | 360 | INIT_LIST_HEAD(&sub->subsystem_list); |
361 | INIT_LIST_HEAD(&sub->test_list); | |
362 | spin_lock_init(&sub->test_lock); | |
7c50328b | 363 | sub->desc.id = SPLAT_SUBSYSTEM_THREAD; |
f1ca4da6 | 364 | |
7c50328b | 365 | SPLAT_TEST_INIT(sub, SPLAT_THREAD_TEST1_NAME, SPLAT_THREAD_TEST1_DESC, |
366 | SPLAT_THREAD_TEST1_ID, splat_thread_test1); | |
115aed0d | 367 | SPLAT_TEST_INIT(sub, SPLAT_THREAD_TEST2_NAME, SPLAT_THREAD_TEST2_DESC, |
368 | SPLAT_THREAD_TEST2_ID, splat_thread_test2); | |
b7dc3138 BB |
369 | SPLAT_TEST_INIT(sub, SPLAT_THREAD_TEST3_NAME, SPLAT_THREAD_TEST3_DESC, |
370 | SPLAT_THREAD_TEST3_ID, splat_thread_test3); | |
f1ca4da6 | 371 | |
372 | return sub; | |
373 | } | |
374 | ||
375 | void | |
7c50328b | 376 | splat_thread_fini(splat_subsystem_t *sub) |
f1ca4da6 | 377 | { |
378 | ASSERT(sub); | |
b7dc3138 | 379 | SPLAT_TEST_FINI(sub, SPLAT_THREAD_TEST3_ID); |
115aed0d | 380 | SPLAT_TEST_FINI(sub, SPLAT_THREAD_TEST2_ID); |
7c50328b | 381 | SPLAT_TEST_FINI(sub, SPLAT_THREAD_TEST1_ID); |
f1ca4da6 | 382 | |
383 | kfree(sub); | |
384 | } | |
385 | ||
386 | int | |
7c50328b | 387 | splat_thread_id(void) { |
388 | return SPLAT_SUBSYSTEM_THREAD; | |
f1ca4da6 | 389 | } |