xref: /freebsd/sys/tests/framework/kern_testfrwk.c (revision 4bc52338)
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, 0, "Kernel Test Framework");
170 SYSCTL_PROC(_kern_testfrwk, OID_AUTO, runtest, (CTLTYPE_STRUCT | CTLFLAG_RW),
171     0, 0, kerntest_execute, "IU", "Execute a kernel test");
172 
173 int
174 kerntest_execute(SYSCTL_HANDLER_ARGS)
175 {
176 	struct kern_test kt;
177 	struct kern_test_list *li, *te = NULL;
178 	struct kern_test_entry *kte = NULL;
179 	int error = 0;
180 
181 	if (ktest_frwk_inited == 0) {
182 		return (ENOENT);
183 	}
184 	/* Find the entry if possible */
185 	error = SYSCTL_IN(req, &kt, sizeof(struct kern_test));
186 	if (error) {
187 		return (error);
188 	}
189 	if (kt.num_threads <= 0) {
190 		return (EINVAL);
191 	}
192 	/* Grab some memory */
193 	kte = malloc(sizeof(struct kern_test_entry), M_KTFRWK, M_WAITOK);
194 	if (kte == NULL) {
195 		error = ENOMEM;
196 		goto out;
197 	}
198 	KTFRWK_LOCK();
199 	TAILQ_FOREACH(li, &kfrwk.kfrwk_testlist, next) {
200 		if (strcmp(li->name, kt.name) == 0) {
201 			te = li;
202 			break;
203 		}
204 	}
205 	if (te == NULL) {
206 		printf("Can't find the test %s\n", kt.name);
207 		error = ENOENT;
208 		free(kte, M_KTFRWK);
209 		goto out;
210 	}
211 	/* Ok we have a test item to run, can we? */
212 	if (!TAILQ_EMPTY(&kfrwk.kfrwk_testq)) {
213 		/* We don't know if there is enough threads */
214 		error = EAGAIN;
215 		free(kte, M_KTFRWK);
216 		goto out;
217 	}
218 	if (kfrwk.kfrwk_waiting < kt.num_threads) {
219 		error = E2BIG;
220 		free(kte, M_KTFRWK);
221 		goto out;
222 	}
223 	kt.tot_threads_running = kt.num_threads;
224 	/* Ok it looks like we can do it, lets get an entry */
225 	kte->kt_e = li;
226 	memcpy(&kte->kt_data, &kt, sizeof(kt));
227 	TAILQ_INSERT_TAIL(&kfrwk.kfrwk_testq, kte, next);
228 	taskqueue_enqueue(kfrwk.kfrwk_tq, &kfrwk.kfrwk_que);
229 out:
230 	KTFRWK_UNLOCK();
231 	return (error);
232 }
233 
234 int
235 kern_testframework_register(const char *name, kerntfunc func)
236 {
237 	int error = 0;
238 	struct kern_test_list *li, *te = NULL;
239 	int len;
240 
241 	len = strlen(name);
242 	if (len >= TEST_NAME_LEN) {
243 		return (E2BIG);
244 	}
245 	te = malloc(sizeof(struct kern_test_list), M_KTFRWK, M_WAITOK);
246 	if (te == NULL) {
247 		error = ENOMEM;
248 		goto out;
249 	}
250 	KTFRWK_LOCK();
251 	/* First does it already exist? */
252 	TAILQ_FOREACH(li, &kfrwk.kfrwk_testlist, next) {
253 		if (strcmp(li->name, name) == 0) {
254 			error = EALREADY;
255 			free(te, M_KTFRWK);
256 			goto out;
257 		}
258 	}
259 	/* Ok we can do it, lets add it to the list */
260 	te->func = func;
261 	strcpy(te->name, name);
262 	TAILQ_INSERT_TAIL(&kfrwk.kfrwk_testlist, te, next);
263 out:
264 	KTFRWK_UNLOCK();
265 	return (error);
266 }
267 
268 int
269 kern_testframework_deregister(const char *name)
270 {
271 	struct kern_test_list *li, *te = NULL;
272 	u_int ncpus = mp_ncpus ? mp_ncpus : MAXCPU;
273 	int error = 0;
274 
275 	KTFRWK_LOCK();
276 	/* First does it already exist? */
277 	TAILQ_FOREACH(li, &kfrwk.kfrwk_testlist, next) {
278 		if (strcmp(li->name, name) == 0) {
279 			te = li;
280 			break;
281 		}
282 	}
283 	if (te == NULL) {
284 		/* It is not registered so no problem */
285 		goto out;
286 	}
287 	if (ncpus != kfrwk.kfrwk_waiting) {
288 		/* We are busy executing something -- can't unload */
289 		error = EBUSY;
290 		goto out;
291 	}
292 	if (!TAILQ_EMPTY(&kfrwk.kfrwk_testq)) {
293 		/* Something still to execute */
294 		error = EBUSY;
295 		goto out;
296 	}
297 	/* Ok we can remove the dude safely */
298 	TAILQ_REMOVE(&kfrwk.kfrwk_testlist, te, next);
299 	memset(te, 0, sizeof(struct kern_test_list));
300 	free(te, M_KTFRWK);
301 out:
302 	KTFRWK_UNLOCK();
303 	return (error);
304 }
305 
306 static int
307 kerntest_mod_init(module_t mod, int type, void *data)
308 {
309 	int err;
310 
311 	switch (type) {
312 	case MOD_LOAD:
313 		err = kerntest_frwk_init();
314 		break;
315 	case MOD_QUIESCE:
316 		KTFRWK_LOCK();
317 		if (TAILQ_EMPTY(&kfrwk.kfrwk_testlist)) {
318 			err = 0;
319 		} else {
320 			err = EBUSY;
321 		}
322 		KTFRWK_UNLOCK();
323 		break;
324 	case MOD_UNLOAD:
325 		err = kerntest_frwk_fini();
326 		break;
327 	default:
328 		return (EOPNOTSUPP);
329 	}
330 	return (err);
331 }
332 
333 static moduledata_t kern_test_framework = {
334 	.name = "kernel_testfrwk",
335 	.evhand = kerntest_mod_init,
336 	.priv = 0
337 };
338 
339 MODULE_VERSION(kern_testframework, 1);
340 DECLARE_MODULE(kern_testframework, kern_test_framework, SI_SUB_PSEUDO, SI_ORDER_ANY);
341