1 /*- 2 * Copyright (c) 2015 Netflix, Inc. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 * 25 */ 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/bus.h> 32 #include <sys/callout.h> 33 #include <sys/kernel.h> 34 #include <sys/ktr.h> 35 #include <sys/lock.h> 36 #include <sys/malloc.h> 37 #include <sys/module.h> 38 #include <sys/mutex.h> 39 #include <sys/sdt.h> 40 #include <sys/smp.h> 41 #include <sys/sysctl.h> 42 #include <sys/taskqueue.h> 43 #include <sys/queue.h> 44 #include <tests/kern_testfrwk.h> 45 #ifdef SMP 46 #include <machine/cpu.h> 47 #endif 48 49 struct kern_test_list { 50 TAILQ_ENTRY(kern_test_list) next; 51 char name[TEST_NAME_LEN]; 52 kerntfunc func; 53 }; 54 55 TAILQ_HEAD(ktestlist, kern_test_list); 56 57 struct kern_test_entry { 58 TAILQ_ENTRY(kern_test_entry) next; 59 struct kern_test_list *kt_e; 60 struct kern_test kt_data; 61 }; 62 63 TAILQ_HEAD(ktestqueue, kern_test_entry); 64 65 MALLOC_DEFINE(M_KTFRWK, "kern_tfrwk", "Kernel Test Framework"); 66 struct kern_totfrwk { 67 struct taskqueue *kfrwk_tq; 68 struct task kfrwk_que; 69 struct ktestlist kfrwk_testlist; 70 struct ktestqueue kfrwk_testq; 71 struct mtx kfrwk_mtx; 72 int kfrwk_waiting; 73 }; 74 75 struct kern_totfrwk kfrwk; 76 static int ktest_frwk_inited = 0; 77 78 #define KTFRWK_MUTEX_INIT() mtx_init(&kfrwk.kfrwk_mtx, "kern_test_frwk", "tfrwk", MTX_DEF) 79 80 #define KTFRWK_DESTROY() mtx_destroy(&kfrwk.kfrwk_mtx) 81 82 #define KTFRWK_LOCK() mtx_lock(&kfrwk.kfrwk_mtx) 83 84 #define KTFRWK_UNLOCK() mtx_unlock(&kfrwk.kfrwk_mtx) 85 86 static void 87 kfrwk_task(void *context, int pending) 88 { 89 struct kern_totfrwk *tf; 90 struct kern_test_entry *wk; 91 int free_mem = 0; 92 struct kern_test kt_data; 93 kerntfunc ktf; 94 95 memset(&kt_data, 0, sizeof(kt_data)); 96 ktf = NULL; 97 tf = (struct kern_totfrwk *)context; 98 KTFRWK_LOCK(); 99 wk = TAILQ_FIRST(&tf->kfrwk_testq); 100 if (wk) { 101 wk->kt_data.tot_threads_running--; 102 tf->kfrwk_waiting--; 103 memcpy(&kt_data, &wk->kt_data, sizeof(kt_data)); 104 if (wk->kt_data.tot_threads_running == 0) { 105 TAILQ_REMOVE(&tf->kfrwk_testq, wk, next); 106 free_mem = 1; 107 } else { 108 /* Wake one of my colleages up to help too */ 109 taskqueue_enqueue(tf->kfrwk_tq, &tf->kfrwk_que); 110 } 111 if (wk->kt_e) { 112 ktf = wk->kt_e->func; 113 } 114 } 115 KTFRWK_UNLOCK(); 116 if (wk && free_mem) { 117 free(wk, M_KTFRWK); 118 } 119 /* Execute the test */ 120 if (ktf) { 121 (*ktf) (&kt_data); 122 } 123 /* We are done */ 124 atomic_add_int(&tf->kfrwk_waiting, 1); 125 } 126 127 static int 128 kerntest_frwk_init(void) 129 { 130 u_int ncpus = mp_ncpus ? mp_ncpus : MAXCPU; 131 132 KTFRWK_MUTEX_INIT(); 133 TAILQ_INIT(&kfrwk.kfrwk_testq); 134 TAILQ_INIT(&kfrwk.kfrwk_testlist); 135 /* Now lets start up a number of tasks to do the work */ 136 TASK_INIT(&kfrwk.kfrwk_que, 0, kfrwk_task, &kfrwk); 137 kfrwk.kfrwk_tq = taskqueue_create_fast("sbtls_task", M_NOWAIT, 138 taskqueue_thread_enqueue, &kfrwk.kfrwk_tq); 139 if (kfrwk.kfrwk_tq == NULL) { 140 printf("Can't start taskqueue for Kernel Test Framework\n"); 141 panic("Taskqueue init fails for kfrwk"); 142 } 143 taskqueue_start_threads(&kfrwk.kfrwk_tq, ncpus, PI_NET, "[kt_frwk task]"); 144 kfrwk.kfrwk_waiting = ncpus; 145 ktest_frwk_inited = 1; 146 return (0); 147 } 148 149 static int 150 kerntest_frwk_fini(void) 151 { 152 KTFRWK_LOCK(); 153 if (!TAILQ_EMPTY(&kfrwk.kfrwk_testlist)) { 154 /* Still modules registered */ 155 KTFRWK_UNLOCK(); 156 return (EBUSY); 157 } 158 ktest_frwk_inited = 0; 159 KTFRWK_UNLOCK(); 160 taskqueue_free(kfrwk.kfrwk_tq); 161 /* Ok lets destroy the mutex on the way outs */ 162 KTFRWK_DESTROY(); 163 return (0); 164 } 165 166 167 static int kerntest_execute(SYSCTL_HANDLER_ARGS); 168 169 SYSCTL_NODE(_kern, OID_AUTO, testfrwk, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 170 "Kernel Test Framework"); 171 SYSCTL_PROC(_kern_testfrwk, OID_AUTO, runtest, 172 CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 173 0, 0, kerntest_execute, "IU", 174 "Execute a kernel test"); 175 176 int 177 kerntest_execute(SYSCTL_HANDLER_ARGS) 178 { 179 struct kern_test kt; 180 struct kern_test_list *li, *te = NULL; 181 struct kern_test_entry *kte = NULL; 182 int error = 0; 183 184 if (ktest_frwk_inited == 0) { 185 return (ENOENT); 186 } 187 /* Find the entry if possible */ 188 error = SYSCTL_IN(req, &kt, sizeof(struct kern_test)); 189 if (error) { 190 return (error); 191 } 192 if (kt.num_threads <= 0) { 193 return (EINVAL); 194 } 195 /* Grab some memory */ 196 kte = malloc(sizeof(struct kern_test_entry), M_KTFRWK, M_WAITOK); 197 if (kte == NULL) { 198 error = ENOMEM; 199 goto out; 200 } 201 KTFRWK_LOCK(); 202 TAILQ_FOREACH(li, &kfrwk.kfrwk_testlist, next) { 203 if (strcmp(li->name, kt.name) == 0) { 204 te = li; 205 break; 206 } 207 } 208 if (te == NULL) { 209 printf("Can't find the test %s\n", kt.name); 210 error = ENOENT; 211 free(kte, M_KTFRWK); 212 goto out; 213 } 214 /* Ok we have a test item to run, can we? */ 215 if (!TAILQ_EMPTY(&kfrwk.kfrwk_testq)) { 216 /* We don't know if there is enough threads */ 217 error = EAGAIN; 218 free(kte, M_KTFRWK); 219 goto out; 220 } 221 if (kfrwk.kfrwk_waiting < kt.num_threads) { 222 error = E2BIG; 223 free(kte, M_KTFRWK); 224 goto out; 225 } 226 kt.tot_threads_running = kt.num_threads; 227 /* Ok it looks like we can do it, lets get an entry */ 228 kte->kt_e = li; 229 memcpy(&kte->kt_data, &kt, sizeof(kt)); 230 TAILQ_INSERT_TAIL(&kfrwk.kfrwk_testq, kte, next); 231 taskqueue_enqueue(kfrwk.kfrwk_tq, &kfrwk.kfrwk_que); 232 out: 233 KTFRWK_UNLOCK(); 234 return (error); 235 } 236 237 int 238 kern_testframework_register(const char *name, kerntfunc func) 239 { 240 int error = 0; 241 struct kern_test_list *li, *te = NULL; 242 int len; 243 244 len = strlen(name); 245 if (len >= TEST_NAME_LEN) { 246 return (E2BIG); 247 } 248 te = malloc(sizeof(struct kern_test_list), M_KTFRWK, M_WAITOK); 249 if (te == NULL) { 250 error = ENOMEM; 251 goto out; 252 } 253 KTFRWK_LOCK(); 254 /* First does it already exist? */ 255 TAILQ_FOREACH(li, &kfrwk.kfrwk_testlist, next) { 256 if (strcmp(li->name, name) == 0) { 257 error = EALREADY; 258 free(te, M_KTFRWK); 259 goto out; 260 } 261 } 262 /* Ok we can do it, lets add it to the list */ 263 te->func = func; 264 strcpy(te->name, name); 265 TAILQ_INSERT_TAIL(&kfrwk.kfrwk_testlist, te, next); 266 out: 267 KTFRWK_UNLOCK(); 268 return (error); 269 } 270 271 int 272 kern_testframework_deregister(const char *name) 273 { 274 struct kern_test_list *li, *te = NULL; 275 u_int ncpus = mp_ncpus ? mp_ncpus : MAXCPU; 276 int error = 0; 277 278 KTFRWK_LOCK(); 279 /* First does it already exist? */ 280 TAILQ_FOREACH(li, &kfrwk.kfrwk_testlist, next) { 281 if (strcmp(li->name, name) == 0) { 282 te = li; 283 break; 284 } 285 } 286 if (te == NULL) { 287 /* It is not registered so no problem */ 288 goto out; 289 } 290 if (ncpus != kfrwk.kfrwk_waiting) { 291 /* We are busy executing something -- can't unload */ 292 error = EBUSY; 293 goto out; 294 } 295 if (!TAILQ_EMPTY(&kfrwk.kfrwk_testq)) { 296 /* Something still to execute */ 297 error = EBUSY; 298 goto out; 299 } 300 /* Ok we can remove the dude safely */ 301 TAILQ_REMOVE(&kfrwk.kfrwk_testlist, te, next); 302 memset(te, 0, sizeof(struct kern_test_list)); 303 free(te, M_KTFRWK); 304 out: 305 KTFRWK_UNLOCK(); 306 return (error); 307 } 308 309 static int 310 kerntest_mod_init(module_t mod, int type, void *data) 311 { 312 int err; 313 314 switch (type) { 315 case MOD_LOAD: 316 err = kerntest_frwk_init(); 317 break; 318 case MOD_QUIESCE: 319 KTFRWK_LOCK(); 320 if (TAILQ_EMPTY(&kfrwk.kfrwk_testlist)) { 321 err = 0; 322 } else { 323 err = EBUSY; 324 } 325 KTFRWK_UNLOCK(); 326 break; 327 case MOD_UNLOAD: 328 err = kerntest_frwk_fini(); 329 break; 330 default: 331 return (EOPNOTSUPP); 332 } 333 return (err); 334 } 335 336 static moduledata_t kern_test_framework = { 337 .name = "kernel_testfrwk", 338 .evhand = kerntest_mod_init, 339 .priv = 0 340 }; 341 342 MODULE_VERSION(kern_testframework, 1); 343 DECLARE_MODULE(kern_testframework, kern_test_framework, SI_SUB_PSEUDO, SI_ORDER_ANY); 344