2 * Copyright (c) 2010 Giovanni Trematerra <giovanni.trematerra@gmail.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * This kernel module helped to identify a deadlock in kthread
31 * interface, also pointed out a race in kthread_exit function.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/kthread.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/systm.h>
47 #ifdef TESTPAUSE_DEBUG
48 #define DPRINTF(x) do { \
55 static struct mtx test_global_lock;
56 static int global_condvar;
57 static int test_thrcnt;
61 thr_suspender(void *arg)
63 struct thread *td = (struct thread *) arg;
69 error = kthread_suspend(td, 10*hz);
70 if (error != 0 && QUIT == 0) {
71 if (error == EWOULDBLOCK)
72 panic("Ooops: kthread deadlock\n");
74 panic("kthread_suspend error: %d\n", error);
79 mtx_lock(&test_global_lock);
81 wakeup(&global_condvar);
82 mtx_unlock(&test_global_lock);
88 thr_resumer(void *arg)
90 struct thread *td = (struct thread *) arg;
94 /* must be the last thread to exit */
95 if (QUIT == 1 && test_thrcnt == 1)
97 error = kthread_resume(td);
99 panic("%s: error on kthread_resume. error: %d\n",
103 mtx_lock(&test_global_lock);
105 wakeup(&global_condvar);
106 mtx_unlock(&test_global_lock);
112 thr_getsuspended(void *arg)
117 kthread_suspend_check();
120 mtx_lock(&test_global_lock);
122 wakeup(&global_condvar);
123 mtx_unlock(&test_global_lock);
131 struct proc *testproc;
132 struct thread *newthr;
137 mtx_init(&test_global_lock, "kthrdlk_lock", NULL, MTX_DEF);
139 error = kproc_kthread_add(thr_getsuspended, NULL, &testproc, &newthr,
140 0, 0, "kthrdlk", "thr_getsuspended");
142 panic("cannot start thr_getsuspended error: %d\n", error);
144 error = kproc_kthread_add(thr_resumer, newthr, &testproc, NULL, 0, 0,
145 "kthrdlk", "thr_resumer");
147 panic("cannot start thr_resumer error: %d\n", error);
149 error = kproc_kthread_add(thr_suspender, newthr, &testproc, NULL, 0, 0,
150 "kthrdlk", "thr_suspender");
152 panic("cannot start thr_suspender error: %d\n", error);
159 DPRINTF(("sending QUIT signal to the thrdlk threads\n"));
161 /* wait kernel threads end */
162 mtx_lock(&test_global_lock);
164 while (test_thrcnt != 0) {
165 ret = mtx_sleep(&global_condvar, &test_global_lock, 0, "waiting thrs end", 30 * hz);
166 if (ret == EWOULDBLOCK) {
167 panic("some threads not die! remaining: %d", test_thrcnt);
171 if (test_thrcnt == 0)
172 DPRINTF(("All test_pause threads die\n"));
174 mtx_destroy(&test_global_lock);
178 kthrdlk_handler(module_t mod, int /*modeventtype_t*/ what,
184 uprintf("kthrdlk loaded!\n");
188 uprintf("Bye Bye! kthrdlk unloaded!\n");
195 static moduledata_t mod_data= {
201 MODULE_VERSION(kthrdlk, 1);
203 DECLARE_MODULE(kthrdlk, mod_data, SI_SUB_EXEC, SI_ORDER_ANY);