xref: /freebsd/sys/kern/kern_kthread.c (revision e63d20b70ee1dbee9b075f29de6f30cdcfe1abe1)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1999 Peter Wemm <peter@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/cpuset.h>
32 #include <sys/kthread.h>
33 #include <sys/lock.h>
34 #include <sys/mutex.h>
35 #include <sys/proc.h>
36 #include <sys/resourcevar.h>
37 #include <sys/rwlock.h>
38 #include <sys/signalvar.h>
39 #include <sys/sysent.h>
40 #include <sys/sx.h>
41 #include <sys/umtxvar.h>
42 #include <sys/unistd.h>
43 #include <sys/wait.h>
44 #include <sys/sched.h>
45 #include <sys/tslog.h>
46 #include <vm/vm.h>
47 #include <vm/vm_extern.h>
48 
49 #include <machine/stdarg.h>
50 
51 /*
52  * Start a kernel process.  This is called after a fork() call in
53  * mi_startup() in the file kern/init_main.c.
54  *
55  * This function is used to start "internal" daemons and intended
56  * to be called from SYSINIT().
57  */
58 void
59 kproc_start(const void *udata)
60 {
61 	const struct kproc_desc	*kp = udata;
62 	int error;
63 
64 	error = kproc_create((void (*)(void *))kp->func, NULL,
65 	    kp->global_procpp, 0, 0, "%s", kp->arg0);
66 	if (error)
67 		panic("kproc_start: %s: error %d", kp->arg0, error);
68 }
69 
70 /*
71  * Create a kernel process/thread/whatever.  It shares its address space
72  * with proc0 - ie: kernel only.
73  *
74  * func is the function to start.
75  * arg is the parameter to pass to function on first startup.
76  * newpp is the return value pointing to the thread's struct proc.
77  * flags are flags to fork1 (in unistd.h)
78  * fmt and following will be *printf'd into (*newpp)->p_comm (for ps, etc.).
79  */
80 static int
81 kproc_create1(void (*func)(void *), void *arg,
82     struct proc **newpp, int flags, int pages, const char *tdname)
83 {
84 	struct fork_req fr;
85 	int error;
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_flags2 = FR2_KPROC;
95 	fr.fr_pages = pages;
96 	fr.fr_procp = &p2;
97 	error = fork1(&thread0, &fr);
98 	if (error != 0)
99 		return (error);
100 
101 	/* save a global descriptor, if desired */
102 	if (newpp != NULL)
103 		*newpp = p2;
104 
105 	/* set up arg0 for 'ps', et al */
106 	strcpy(p2->p_comm, tdname);
107 	td = FIRST_THREAD_IN_PROC(p2);
108 	strcpy(td->td_name, tdname);
109 #ifdef KTR
110 	sched_clear_tdname(td);
111 #endif
112 	TSTHREAD(td, td->td_name);
113 #ifdef HWPMC_HOOKS
114 	if (PMC_SYSTEM_SAMPLING_ACTIVE()) {
115 		PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_PROC_CREATE_LOG, p2);
116 		PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_CREATE_LOG, NULL);
117 	}
118 #endif
119 
120 	/* call the processes' main()... */
121 	cpu_fork_kthread_handler(td, func, arg);
122 
123 	/* Avoid inheriting affinity from a random parent. */
124 	cpuset_kernthread(td);
125 	thread_lock(td);
126 	TD_SET_CAN_RUN(td);
127 	sched_prio(td, PVM);
128 	sched_user_prio(td, PUSER);
129 
130 	/* Delay putting it on the run queue until now. */
131 	if ((flags & RFSTOPPED) == 0)
132 		sched_add(td, SRQ_BORING);
133 	else
134 		thread_unlock(td);
135 
136 	return (0);
137 }
138 
139 int
140 kproc_create(void (*func)(void *), void *arg,
141     struct proc **newpp, int flags, int pages, const char *fmt, ...)
142 {
143 	va_list ap;
144 	int error;
145 	char tdname[MAXCOMLEN + 1];
146 
147 	va_start(ap, fmt);
148 	vsnprintf(tdname, sizeof(tdname), fmt, ap);
149 	va_end(ap);
150 	DROP_GIANT();
151 	error = kproc_create1(func, arg, newpp, flags, pages, tdname);
152 	PICKUP_GIANT();
153 	return (error);
154 }
155 
156 void
157 kproc_exit(int ecode)
158 {
159 	struct thread *td;
160 	struct proc *p;
161 
162 	td = curthread;
163 	p = td->td_proc;
164 
165 	/*
166 	 * Reparent curthread from proc0 to init so that the zombie
167 	 * is harvested.
168 	 */
169 	sx_xlock(&proctree_lock);
170 	PROC_LOCK(p);
171 	proc_reparent(p, initproc, true);
172 	PROC_UNLOCK(p);
173 	sx_xunlock(&proctree_lock);
174 
175 	/*
176 	 * Wakeup anyone waiting for us to exit.
177 	 */
178 	wakeup(p);
179 
180 	/* Buh-bye! */
181 	exit1(td, ecode, 0);
182 }
183 
184 /*
185  * Advise a kernel process to suspend (or resume) in its main loop.
186  * Participation is voluntary.
187  */
188 int
189 kproc_suspend(struct proc *p, int timo)
190 {
191 	/*
192 	 * Make sure this is indeed a system process and we can safely
193 	 * use the p_siglist field.
194 	 */
195 	PROC_LOCK(p);
196 	if ((p->p_flag & P_KPROC) == 0) {
197 		PROC_UNLOCK(p);
198 		return (EINVAL);
199 	}
200 	SIGADDSET(p->p_siglist, SIGSTOP);
201 	wakeup(p);
202 	return (msleep(&p->p_siglist, &p->p_mtx, PPAUSE | PDROP,
203 	    "suspkp", timo));
204 }
205 
206 int
207 kproc_resume(struct proc *p)
208 {
209 	/*
210 	 * Make sure this is indeed a system process and we can safely
211 	 * use the p_siglist field.
212 	 */
213 	PROC_LOCK(p);
214 	if ((p->p_flag & P_KPROC) == 0) {
215 		PROC_UNLOCK(p);
216 		return (EINVAL);
217 	}
218 	SIGDELSET(p->p_siglist, SIGSTOP);
219 	PROC_UNLOCK(p);
220 	wakeup(&p->p_siglist);
221 	return (0);
222 }
223 
224 void
225 kproc_suspend_check(struct proc *p)
226 {
227 	PROC_LOCK(p);
228 	while (SIGISMEMBER(p->p_siglist, SIGSTOP)) {
229 		wakeup(&p->p_siglist);
230 		msleep(&p->p_siglist, &p->p_mtx, PPAUSE, "kpsusp", 0);
231 	}
232 	PROC_UNLOCK(p);
233 }
234 
235 /*
236  * Start a kernel thread.
237  *
238  * This function is used to start "internal" daemons and intended
239  * to be called from SYSINIT().
240  */
241 
242 void
243 kthread_start(const void *udata)
244 {
245 	const struct kthread_desc *kp = udata;
246 	int error;
247 
248 	error = kthread_add((void (*)(void *))kp->func, NULL,
249 	    NULL, kp->global_threadpp, 0, 0, "%s", kp->arg0);
250 	if (error)
251 		panic("kthread_start: %s: error %d", kp->arg0, error);
252 }
253 
254 /*
255  * Create a kernel thread.  It shares its address space
256  * with proc0 - ie: kernel only.
257  *
258  * func is the function to start.
259  * arg is the parameter to pass to function on first startup.
260  * newtdp is the return value pointing to the thread's struct thread.
261  *  ** XXX fix this --> flags are flags to fork1 (in unistd.h)
262  * fmt and following will be *printf'd into (*newtd)->td_name (for ps, etc.).
263  */
264 static int
265 kthread_add1(void (*func)(void *), void *arg, struct proc *p,
266     struct thread **newtdp, int flags, int pages, const char *tdname)
267 {
268 	struct thread *newtd, *oldtd;
269 
270 	if (!proc0.p_stats)
271 		panic("kthread_add called too soon");
272 
273 	/* If no process supplied, put it on proc0 */
274 	if (p == NULL)
275 		p = &proc0;
276 
277 	/* Initialize our new td  */
278 	newtd = thread_alloc(pages);
279 	if (newtd == NULL)
280 		return (ENOMEM);
281 
282 	PROC_LOCK(p);
283 	if (p->p_state == PRS_ZOMBIE || (p->p_flag2 & P2_WEXIT) != 0) {
284 		PROC_UNLOCK(p);
285 		return (ESRCH);
286 	}
287 	oldtd = FIRST_THREAD_IN_PROC(p);
288 
289 	/*
290 	 * Set the new thread pointer before the thread starts running: *newtdp
291 	 * could be a pointer that is referenced by "func".
292 	 */
293 	if (newtdp != NULL)
294 		*newtdp = newtd;
295 
296 	bzero(&newtd->td_startzero,
297 	    __rangeof(struct thread, td_startzero, td_endzero));
298 	bcopy(&oldtd->td_startcopy, &newtd->td_startcopy,
299 	    __rangeof(struct thread, td_startcopy, td_endcopy));
300 
301 	/* set up arg0 for 'ps', et al */
302 	strcpy(newtd->td_name, tdname);
303 
304 	TSTHREAD(newtd, newtd->td_name);
305 
306 	newtd->td_proc = p;  /* needed for cpu_copy_thread */
307 	newtd->td_pflags |= TDP_KTHREAD;
308 
309 	/* might be further optimized for kthread */
310 	cpu_copy_thread(newtd, oldtd);
311 
312 	/* put the designated function(arg) as the resume context */
313 	cpu_fork_kthread_handler(newtd, func, arg);
314 
315 	thread_cow_get_proc(newtd, p);
316 
317 	/* This code is similar to thread_create() in kern_thr.c. */
318 	p->p_flag |= P_HADTHREADS;
319 	thread_link(newtd, p);
320 	thread_lock(oldtd);
321 	/* let the scheduler know about these things. */
322 	sched_fork_thread(oldtd, newtd);
323 	TD_SET_CAN_RUN(newtd);
324 	thread_unlock(oldtd);
325 	PROC_UNLOCK(p);
326 
327 	tidhash_add(newtd);
328 
329 	/* Avoid inheriting affinity from a random parent. */
330 	cpuset_kernthread(newtd);
331 #ifdef HWPMC_HOOKS
332 	if (PMC_SYSTEM_SAMPLING_ACTIVE())
333 		PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_CREATE_LOG, NULL);
334 #endif
335 	/* Delay putting it on the run queue until now. */
336 	if ((flags & RFSTOPPED) == 0) {
337 		thread_lock(newtd);
338 		sched_add(newtd, SRQ_BORING);
339 	}
340 	return (0);
341 }
342 
343 int
344 kthread_add(void (*func)(void *), void *arg, struct proc *p,
345     struct thread **newtdp, int flags, int pages, const char *fmt, ...)
346 {
347 	va_list ap;
348 	int error;
349 	char tdname[MAXCOMLEN + 1];
350 
351 	va_start(ap, fmt);
352 	vsnprintf(tdname, sizeof(tdname), fmt, ap);
353 	va_end(ap);
354 	DROP_GIANT();
355 	error = kthread_add1(func, arg, p, newtdp, flags, pages, tdname);
356 	PICKUP_GIANT();
357 	return (error);
358 }
359 
360 void
361 kthread_exit(void)
362 {
363 	struct proc *p;
364 	struct thread *td;
365 
366 	td = curthread;
367 	p = td->td_proc;
368 
369 #ifdef HWPMC_HOOKS
370 	if (PMC_SYSTEM_SAMPLING_ACTIVE())
371 		PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_EXIT_LOG, NULL);
372 #endif
373 	/* A module may be waiting for us to exit. */
374 	wakeup(td);
375 
376 	/*
377 	 * The last exiting thread in a kernel process must tear down
378 	 * the whole process.
379 	 */
380 	PROC_LOCK(p);
381 	if (p->p_numthreads == 1) {
382 		PROC_UNLOCK(p);
383 		kproc_exit(0);
384 	}
385 
386 	if (p->p_sysent->sv_ontdexit != NULL)
387 		p->p_sysent->sv_ontdexit(td);
388 
389 	tidhash_remove(td);
390 	umtx_thread_exit(td);
391 	tdsigcleanup(td);
392 	PROC_SLOCK(p);
393 	thread_exit();
394 }
395 
396 /*
397  * Advise a kernel process to suspend (or resume) in its main loop.
398  * Participation is voluntary.
399  */
400 int
401 kthread_suspend(struct thread *td, int timo)
402 {
403 	struct proc *p;
404 
405 	p = td->td_proc;
406 
407 	/*
408 	 * td_pflags should not be read by any thread other than
409 	 * curthread, but as long as this flag is invariant during the
410 	 * thread's lifetime, it is OK to check its state.
411 	 */
412 	if ((td->td_pflags & TDP_KTHREAD) == 0)
413 		return (EINVAL);
414 
415 	/*
416 	 * The caller of the primitive should have already checked that the
417 	 * thread is up and running, thus not being blocked by other
418 	 * conditions.
419 	 */
420 	PROC_LOCK(p);
421 	thread_lock(td);
422 	td->td_flags |= TDF_KTH_SUSP;
423 	thread_unlock(td);
424 	return (msleep(&td->td_flags, &p->p_mtx, PPAUSE | PDROP, "suspkt",
425 	    timo));
426 }
427 
428 /*
429  * Resume a thread previously put asleep with kthread_suspend().
430  */
431 int
432 kthread_resume(struct thread *td)
433 {
434 	struct proc *p;
435 
436 	p = td->td_proc;
437 
438 	/*
439 	 * td_pflags should not be read by any thread other than
440 	 * curthread, but as long as this flag is invariant during the
441 	 * thread's lifetime, it is OK to check its state.
442 	 */
443 	if ((td->td_pflags & TDP_KTHREAD) == 0)
444 		return (EINVAL);
445 
446 	PROC_LOCK(p);
447 	thread_lock(td);
448 	td->td_flags &= ~TDF_KTH_SUSP;
449 	thread_unlock(td);
450 	wakeup(&td->td_flags);
451 	PROC_UNLOCK(p);
452 	return (0);
453 }
454 
455 /*
456  * Used by the thread to poll as to whether it should yield/sleep
457  * and notify the caller that is has happened.
458  */
459 void
460 kthread_suspend_check(void)
461 {
462 	struct proc *p;
463 	struct thread *td;
464 
465 	td = curthread;
466 	p = td->td_proc;
467 
468 	if ((td->td_pflags & TDP_KTHREAD) == 0)
469 		panic("%s: curthread is not a valid kthread", __func__);
470 
471 	/*
472 	 * Setting the TDF_KTH_SUSP flag is protected by process lock.
473 	 *
474 	 * Do an unlocked read first to avoid serializing with all other threads
475 	 * in the common case of not suspending.
476 	 */
477 	if ((td->td_flags & TDF_KTH_SUSP) == 0)
478 		return;
479 	PROC_LOCK(p);
480 	while ((td->td_flags & TDF_KTH_SUSP) != 0) {
481 		wakeup(&td->td_flags);
482 		msleep(&td->td_flags, &p->p_mtx, PPAUSE, "ktsusp", 0);
483 	}
484 	PROC_UNLOCK(p);
485 }
486 
487 int
488 kproc_kthread_add(void (*func)(void *), void *arg,
489     struct proc **procptr, struct thread **tdptr,
490     int flags, int pages, const char *procname, const char *fmt, ...)
491 {
492 	int error;
493 	va_list ap;
494 	char buf[100];
495 	struct thread *td;
496 
497 	if (*procptr == NULL) {
498 		/*
499 		 * Use RFSTOPPED to ensure that *tdptr is initialized before the
500 		 * thread starts running.
501 		 */
502 		error = kproc_create(func, arg,
503 		    procptr, flags | RFSTOPPED, pages, "%s", procname);
504 		if (error)
505 			return (error);
506 		td = FIRST_THREAD_IN_PROC(*procptr);
507 		if (tdptr)
508 			*tdptr = td;
509 		if ((flags & RFSTOPPED) == 0) {
510 			thread_lock(td);
511 			sched_add(td, SRQ_BORING);
512 		}
513 		va_start(ap, fmt);
514 		vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap);
515 		va_end(ap);
516 #ifdef KTR
517 		sched_clear_tdname(td);
518 #endif
519 		return (0);
520 	}
521 	va_start(ap, fmt);
522 	vsnprintf(buf, sizeof(buf), fmt, ap);
523 	va_end(ap);
524 	error = kthread_add(func, arg, *procptr,
525 	    tdptr, flags, pages, "%s", buf);
526 	return (error);
527 }
528