xref: /freebsd-11-stable/sys/kern/kern_kthread.c (revision 0010ea385635ce74325d8a8bf5c6397fceb6cefe)
1 /*-
2  * Copyright (c) 1999 Peter Wemm <peter@FreeBSD.org>
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 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/cpuset.h>
33 #include <sys/kthread.h>
34 #include <sys/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/proc.h>
37 #include <sys/resourcevar.h>
38 #include <sys/rwlock.h>
39 #include <sys/signalvar.h>
40 #include <sys/sx.h>
41 #include <sys/umtx.h>
42 #include <sys/unistd.h>
43 #include <sys/wait.h>
44 #include <sys/sched.h>
45 #include <vm/vm.h>
46 #include <vm/vm_extern.h>
47 
48 #include <machine/stdarg.h>
49 
50 /*
51  * Start a kernel process.  This is called after a fork() call in
52  * mi_startup() in the file kern/init_main.c.
53  *
54  * This function is used to start "internal" daemons and intended
55  * to be called from SYSINIT().
56  */
57 void
kproc_start(const void * udata)58 kproc_start(const void *udata)
59 {
60 	const struct kproc_desc	*kp = udata;
61 	int error;
62 
63 	error = kproc_create((void (*)(void *))kp->func, NULL,
64 		    kp->global_procpp, 0, 0, "%s", kp->arg0);
65 	if (error)
66 		panic("kproc_start: %s: error %d", kp->arg0, error);
67 }
68 
69 /*
70  * Create a kernel process/thread/whatever.  It shares its address space
71  * with proc0 - ie: kernel only.
72  *
73  * func is the function to start.
74  * arg is the parameter to pass to function on first startup.
75  * newpp is the return value pointing to the thread's struct proc.
76  * flags are flags to fork1 (in unistd.h)
77  * fmt and following will be *printf'd into (*newpp)->p_comm (for ps, etc.).
78  */
79 int
kproc_create(void (* func)(void *),void * arg,struct proc ** newpp,int flags,int pages,const char * fmt,...)80 kproc_create(void (*func)(void *), void *arg,
81     struct proc **newpp, int flags, int pages, const char *fmt, ...)
82 {
83 	struct fork_req fr;
84 	int error;
85 	va_list ap;
86 	struct thread *td;
87 	struct proc *p2;
88 
89 	if (!proc0.p_stats)
90 		panic("kproc_create called too soon");
91 
92 	bzero(&fr, sizeof(fr));
93 	fr.fr_flags = RFMEM | RFFDG | RFPROC | RFSTOPPED | flags;
94 	fr.fr_pages = pages;
95 	fr.fr_procp = &p2;
96 	error = fork1(&thread0, &fr);
97 	if (error)
98 		return error;
99 
100 	/* save a global descriptor, if desired */
101 	if (newpp != NULL)
102 		*newpp = p2;
103 
104 	/* this is a non-swapped system process */
105 	PROC_LOCK(p2);
106 	td = FIRST_THREAD_IN_PROC(p2);
107 	p2->p_flag |= P_SYSTEM | P_KPROC;
108 	td->td_pflags |= TDP_KTHREAD;
109 	mtx_lock(&p2->p_sigacts->ps_mtx);
110 	p2->p_sigacts->ps_flag |= PS_NOCLDWAIT;
111 	mtx_unlock(&p2->p_sigacts->ps_mtx);
112 	PROC_UNLOCK(p2);
113 
114 	/* set up arg0 for 'ps', et al */
115 	va_start(ap, fmt);
116 	vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap);
117 	va_end(ap);
118 	/* set up arg0 for 'ps', et al */
119 	va_start(ap, fmt);
120 	vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap);
121 	va_end(ap);
122 #ifdef KTR
123 	sched_clear_tdname(td);
124 #endif
125 
126 	/* call the processes' main()... */
127 	cpu_fork_kthread_handler(td, func, arg);
128 
129 	/* Avoid inheriting affinity from a random parent. */
130 	cpuset_setthread(td->td_tid, cpuset_root);
131 	thread_lock(td);
132 	TD_SET_CAN_RUN(td);
133 	sched_prio(td, PVM);
134 	sched_user_prio(td, PUSER);
135 
136 	/* Delay putting it on the run queue until now. */
137 	if (!(flags & RFSTOPPED))
138 		sched_add(td, SRQ_BORING);
139 	thread_unlock(td);
140 
141 	return 0;
142 }
143 
144 void
kproc_exit(int ecode)145 kproc_exit(int ecode)
146 {
147 	struct thread *td;
148 	struct proc *p;
149 
150 	td = curthread;
151 	p = td->td_proc;
152 
153 	/*
154 	 * Reparent curthread from proc0 to init so that the zombie
155 	 * is harvested.
156 	 */
157 	sx_xlock(&proctree_lock);
158 	PROC_LOCK(p);
159 	proc_reparent(p, initproc);
160 	PROC_UNLOCK(p);
161 	sx_xunlock(&proctree_lock);
162 
163 	/*
164 	 * Wakeup anyone waiting for us to exit.
165 	 */
166 	wakeup(p);
167 
168 	/* Buh-bye! */
169 	exit1(td, ecode, 0);
170 }
171 
172 /*
173  * Advise a kernel process to suspend (or resume) in its main loop.
174  * Participation is voluntary.
175  */
176 int
kproc_suspend(struct proc * p,int timo)177 kproc_suspend(struct proc *p, int timo)
178 {
179 	/*
180 	 * Make sure this is indeed a system process and we can safely
181 	 * use the p_siglist field.
182 	 */
183 	PROC_LOCK(p);
184 	if ((p->p_flag & P_KPROC) == 0) {
185 		PROC_UNLOCK(p);
186 		return (EINVAL);
187 	}
188 	SIGADDSET(p->p_siglist, SIGSTOP);
189 	wakeup(p);
190 	return msleep(&p->p_siglist, &p->p_mtx, PPAUSE | PDROP, "suspkp", timo);
191 }
192 
193 int
kproc_resume(struct proc * p)194 kproc_resume(struct proc *p)
195 {
196 	/*
197 	 * Make sure this is indeed a system process and we can safely
198 	 * use the p_siglist field.
199 	 */
200 	PROC_LOCK(p);
201 	if ((p->p_flag & P_KPROC) == 0) {
202 		PROC_UNLOCK(p);
203 		return (EINVAL);
204 	}
205 	SIGDELSET(p->p_siglist, SIGSTOP);
206 	PROC_UNLOCK(p);
207 	wakeup(&p->p_siglist);
208 	return (0);
209 }
210 
211 void
kproc_suspend_check(struct proc * p)212 kproc_suspend_check(struct proc *p)
213 {
214 	PROC_LOCK(p);
215 	while (SIGISMEMBER(p->p_siglist, SIGSTOP)) {
216 		wakeup(&p->p_siglist);
217 		msleep(&p->p_siglist, &p->p_mtx, PPAUSE, "kpsusp", 0);
218 	}
219 	PROC_UNLOCK(p);
220 }
221 
222 
223 /*
224  * Start a kernel thread.
225  *
226  * This function is used to start "internal" daemons and intended
227  * to be called from SYSINIT().
228  */
229 
230 void
kthread_start(const void * udata)231 kthread_start(const void *udata)
232 {
233 	const struct kthread_desc	*kp = udata;
234 	int error;
235 
236 	error = kthread_add((void (*)(void *))kp->func, NULL,
237 		    NULL, kp->global_threadpp, 0, 0, "%s", kp->arg0);
238 	if (error)
239 		panic("kthread_start: %s: error %d", kp->arg0, error);
240 }
241 
242 /*
243  * Create a kernel thread.  It shares its address space
244  * with proc0 - ie: kernel only.
245  *
246  * func is the function to start.
247  * arg is the parameter to pass to function on first startup.
248  * newtdp is the return value pointing to the thread's struct thread.
249  *  ** XXX fix this --> flags are flags to fork1 (in unistd.h)
250  * fmt and following will be *printf'd into (*newtd)->td_name (for ps, etc.).
251  */
252 int
kthread_add(void (* func)(void *),void * arg,struct proc * p,struct thread ** newtdp,int flags,int pages,const char * fmt,...)253 kthread_add(void (*func)(void *), void *arg, struct proc *p,
254     struct thread **newtdp, int flags, int pages, const char *fmt, ...)
255 {
256 	va_list ap;
257 	struct thread *newtd, *oldtd;
258 
259 	if (!proc0.p_stats)
260 		panic("kthread_add called too soon");
261 
262 	/* If no process supplied, put it on proc0 */
263 	if (p == NULL)
264 		p = &proc0;
265 
266 	/* Initialize our new td  */
267 	newtd = thread_alloc(pages);
268 	if (newtd == NULL)
269 		return (ENOMEM);
270 
271 	PROC_LOCK(p);
272 	oldtd = FIRST_THREAD_IN_PROC(p);
273 
274 	bzero(&newtd->td_startzero,
275 	    __rangeof(struct thread, td_startzero, td_endzero));
276 	newtd->td_sleeptimo = 0;
277 	newtd->td_vslock_sz = 0;
278 	bzero(&newtd->td_si, sizeof(newtd->td_si));
279 	bcopy(&oldtd->td_startcopy, &newtd->td_startcopy,
280 	    __rangeof(struct thread, td_startcopy, td_endcopy));
281 	newtd->td_sa = oldtd->td_sa;
282 
283 	/* set up arg0 for 'ps', et al */
284 	va_start(ap, fmt);
285 	vsnprintf(newtd->td_name, sizeof(newtd->td_name), fmt, ap);
286 	va_end(ap);
287 
288 	newtd->td_proc = p;  /* needed for cpu_copy_thread */
289 	/* might be further optimized for kthread */
290 	cpu_copy_thread(newtd, oldtd);
291 	/* put the designated function(arg) as the resume context */
292 	cpu_fork_kthread_handler(newtd, func, arg);
293 
294 	newtd->td_pflags |= TDP_KTHREAD;
295 	thread_cow_get_proc(newtd, p);
296 
297 	/* this code almost the same as create_thread() in kern_thr.c */
298 	p->p_flag |= P_HADTHREADS;
299 	thread_link(newtd, p);
300 	thread_lock(oldtd);
301 	/* let the scheduler know about these things. */
302 	sched_fork_thread(oldtd, newtd);
303 	TD_SET_CAN_RUN(newtd);
304 	thread_unlock(oldtd);
305 	PROC_UNLOCK(p);
306 
307 	tidhash_add(newtd);
308 
309 	/* Avoid inheriting affinity from a random parent. */
310 	cpuset_setthread(newtd->td_tid, cpuset_root);
311 
312 	/* Delay putting it on the run queue until now. */
313 	if (!(flags & RFSTOPPED)) {
314 		thread_lock(newtd);
315 		sched_add(newtd, SRQ_BORING);
316 		thread_unlock(newtd);
317 	}
318 	if (newtdp)
319 		*newtdp = newtd;
320 	return 0;
321 }
322 
323 void
kthread_exit(void)324 kthread_exit(void)
325 {
326 	struct proc *p;
327 	struct thread *td;
328 
329 	td = curthread;
330 	p = td->td_proc;
331 
332 	/* A module may be waiting for us to exit. */
333 	wakeup(td);
334 
335 	/*
336 	 * The last exiting thread in a kernel process must tear down
337 	 * the whole process.
338 	 */
339 	rw_wlock(&tidhash_lock);
340 	PROC_LOCK(p);
341 	if (p->p_numthreads == 1) {
342 		PROC_UNLOCK(p);
343 		rw_wunlock(&tidhash_lock);
344 		kproc_exit(0);
345 	}
346 	LIST_REMOVE(td, td_hash);
347 	rw_wunlock(&tidhash_lock);
348 	umtx_thread_exit(td);
349 	tdsigcleanup(td);
350 	PROC_SLOCK(p);
351 	thread_exit();
352 }
353 
354 /*
355  * Advise a kernel process to suspend (or resume) in its main loop.
356  * Participation is voluntary.
357  */
358 int
kthread_suspend(struct thread * td,int timo)359 kthread_suspend(struct thread *td, int timo)
360 {
361 	struct proc *p;
362 
363 	p = td->td_proc;
364 
365 	/*
366 	 * td_pflags should not be read by any thread other than
367 	 * curthread, but as long as this flag is invariant during the
368 	 * thread's lifetime, it is OK to check its state.
369 	 */
370 	if ((td->td_pflags & TDP_KTHREAD) == 0)
371 		return (EINVAL);
372 
373 	/*
374 	 * The caller of the primitive should have already checked that the
375 	 * thread is up and running, thus not being blocked by other
376 	 * conditions.
377 	 */
378 	PROC_LOCK(p);
379 	thread_lock(td);
380 	td->td_flags |= TDF_KTH_SUSP;
381 	thread_unlock(td);
382 	return (msleep(&td->td_flags, &p->p_mtx, PPAUSE | PDROP, "suspkt",
383 	    timo));
384 }
385 
386 /*
387  * Resume a thread previously put asleep with kthread_suspend().
388  */
389 int
kthread_resume(struct thread * td)390 kthread_resume(struct thread *td)
391 {
392 	struct proc *p;
393 
394 	p = td->td_proc;
395 
396 	/*
397 	 * td_pflags should not be read by any thread other than
398 	 * curthread, but as long as this flag is invariant during the
399 	 * thread's lifetime, it is OK to check its state.
400 	 */
401 	if ((td->td_pflags & TDP_KTHREAD) == 0)
402 		return (EINVAL);
403 
404 	PROC_LOCK(p);
405 	thread_lock(td);
406 	td->td_flags &= ~TDF_KTH_SUSP;
407 	thread_unlock(td);
408 	wakeup(&td->td_flags);
409 	PROC_UNLOCK(p);
410 	return (0);
411 }
412 
413 /*
414  * Used by the thread to poll as to whether it should yield/sleep
415  * and notify the caller that is has happened.
416  */
417 void
kthread_suspend_check(void)418 kthread_suspend_check(void)
419 {
420 	struct proc *p;
421 	struct thread *td;
422 
423 	td = curthread;
424 	p = td->td_proc;
425 
426 	if ((td->td_pflags & TDP_KTHREAD) == 0)
427 		panic("%s: curthread is not a valid kthread", __func__);
428 
429 	/*
430 	 * As long as the double-lock protection is used when accessing the
431 	 * TDF_KTH_SUSP flag, synchronizing the read operation via proc mutex
432 	 * is fine.
433 	 */
434 	PROC_LOCK(p);
435 	while (td->td_flags & TDF_KTH_SUSP) {
436 		wakeup(&td->td_flags);
437 		msleep(&td->td_flags, &p->p_mtx, PPAUSE, "ktsusp", 0);
438 	}
439 	PROC_UNLOCK(p);
440 }
441 
442 int
kproc_kthread_add(void (* func)(void *),void * arg,struct proc ** procptr,struct thread ** tdptr,int flags,int pages,const char * procname,const char * fmt,...)443 kproc_kthread_add(void (*func)(void *), void *arg,
444             struct proc **procptr, struct thread **tdptr,
445             int flags, int pages, const char *procname, const char *fmt, ...)
446 {
447 	int error;
448 	va_list ap;
449 	char buf[100];
450 	struct thread *td;
451 
452 	if (*procptr == NULL) {
453 		error = kproc_create(func, arg,
454 		    	procptr, flags, pages, "%s", procname);
455 		if (error)
456 			return (error);
457 		td = FIRST_THREAD_IN_PROC(*procptr);
458 		if (tdptr)
459 			*tdptr = td;
460 		va_start(ap, fmt);
461 		vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap);
462 		va_end(ap);
463 #ifdef KTR
464 		sched_clear_tdname(td);
465 #endif
466 		return (0);
467 	}
468 	va_start(ap, fmt);
469 	vsnprintf(buf, sizeof(buf), fmt, ap);
470 	va_end(ap);
471 	error = kthread_add(func, arg, *procptr,
472 		    tdptr, flags, pages, "%s", buf);
473 	return (error);
474 }
475