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