1 /*- 2 * Copyright (c) 2010 Giovanni Trematerra <giovanni.trematerra@gmail.com> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 */ 26 27 /* 28 * PURPOSE: 29 * 30 * This kernel module helped to identify a deadlock in kthread 31 * interface, also pointed out a race in kthread_exit function. 32 * 33 */ 34 35 #include <sys/cdefs.h> 36 #include <sys/param.h> 37 #include <sys/kernel.h> 38 #include <sys/kthread.h> 39 #include <sys/lock.h> 40 #include <sys/module.h> 41 #include <sys/mutex.h> 42 #include <sys/systm.h> 43 #include <sys/time.h> 44 45 #ifdef TESTPAUSE_DEBUG 46 #define DPRINTF(x) do { \ 47 printf (x); \ 48 } while (0) 49 #else 50 #define DPRINTF(x) 51 #endif 52 53 static struct mtx test_global_lock; 54 static int global_condvar; 55 static int test_thrcnt; 56 volatile int QUIT; 57 58 static void 59 thr_suspender(void *arg) 60 { 61 struct thread *td = (struct thread *) arg; 62 int error; 63 64 for (;;) { 65 if (QUIT == 1) 66 break; 67 error = kthread_suspend(td, 10*hz); 68 if (error != 0 && QUIT == 0) { 69 if (error == EWOULDBLOCK) 70 panic("Ooops: kthread deadlock\n"); 71 else 72 panic("kthread_suspend error: %d\n", error); 73 break; 74 } 75 } 76 77 mtx_lock(&test_global_lock); 78 test_thrcnt--; 79 wakeup(&global_condvar); 80 mtx_unlock(&test_global_lock); 81 82 kthread_exit(); 83 } 84 85 static void 86 thr_resumer(void *arg) 87 { 88 struct thread *td = (struct thread *) arg; 89 int error; 90 91 for (;;) { 92 /* must be the last thread to exit */ 93 if (QUIT == 1 && test_thrcnt == 1) 94 break; 95 error = kthread_resume(td); 96 if (error != 0) 97 panic("%s: error on kthread_resume. error: %d\n", 98 __func__, error); 99 } 100 101 mtx_lock(&test_global_lock); 102 test_thrcnt--; 103 wakeup(&global_condvar); 104 mtx_unlock(&test_global_lock); 105 106 kthread_exit(); 107 } 108 109 static void 110 thr_getsuspended(void *arg) 111 { 112 for (;;) { 113 if (QUIT == 1) 114 break; 115 kthread_suspend_check(); 116 } 117 118 mtx_lock(&test_global_lock); 119 test_thrcnt--; 120 wakeup(&global_condvar); 121 mtx_unlock(&test_global_lock); 122 123 kthread_exit(); 124 } 125 126 static void 127 kthrdlk_init(void) 128 { 129 struct proc *testproc; 130 struct thread *newthr; 131 int error; 132 133 QUIT = 0; 134 test_thrcnt = 3; 135 mtx_init(&test_global_lock, "kthrdlk_lock", NULL, MTX_DEF); 136 testproc = NULL; 137 error = kproc_kthread_add(thr_getsuspended, NULL, &testproc, &newthr, 138 0, 0, "kthrdlk", "thr_getsuspended"); 139 if (error != 0) 140 panic("cannot start thr_getsuspended error: %d\n", error); 141 142 error = kproc_kthread_add(thr_resumer, newthr, &testproc, NULL, 0, 0, 143 "kthrdlk", "thr_resumer"); 144 if (error != 0) 145 panic("cannot start thr_resumer error: %d\n", error); 146 147 error = kproc_kthread_add(thr_suspender, newthr, &testproc, NULL, 0, 0, 148 "kthrdlk", "thr_suspender"); 149 if (error != 0) 150 panic("cannot start thr_suspender error: %d\n", error); 151 } 152 153 static void 154 kthrdlk_done(void) 155 { 156 int ret; 157 DPRINTF(("sending QUIT signal to the thrdlk threads\n")); 158 159 /* wait kernel threads end */ 160 mtx_lock(&test_global_lock); 161 QUIT = 1; 162 while (test_thrcnt != 0) { 163 ret = mtx_sleep(&global_condvar, &test_global_lock, 0, "waiting thrs end", 30 * hz); 164 if (ret == EWOULDBLOCK) { 165 panic("some threads not die! remaining: %d", test_thrcnt); 166 break; 167 } 168 } 169 if (test_thrcnt == 0) 170 DPRINTF(("All test_pause threads die\n")); 171 172 mtx_destroy(&test_global_lock); 173 } 174 175 static int 176 kthrdlk_handler(module_t mod, int /*modeventtype_t*/ what, 177 void *arg) 178 { 179 switch (what) { 180 case MOD_LOAD: 181 kthrdlk_init(); 182 uprintf("kthrdlk loaded!\n"); 183 return (0); 184 case MOD_UNLOAD: 185 kthrdlk_done(); 186 uprintf("Bye Bye! kthrdlk unloaded!\n"); 187 return (0); 188 } 189 190 return (EOPNOTSUPP); 191 } 192 193 static moduledata_t mod_data= { 194 "kthrdlk", 195 kthrdlk_handler, 196 0 197 }; 198 199 MODULE_VERSION(kthrdlk, 1); 200 201 DECLARE_MODULE(kthrdlk, mod_data, SI_SUB_EXEC, SI_ORDER_ANY); 202 203