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/stdarg.h>
46 #include <sys/tslog.h>
47 #include <vm/vm.h>
48 #include <vm/vm_extern.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 static int
kproc_create1(void (* func)(void *),void * arg,struct proc ** newpp,int flags,int pages,const char * tdname)80 kproc_create1(void (*func)(void *), void *arg,
81 struct proc **newpp, int flags, int pages, const char *tdname)
82 {
83 struct fork_req fr;
84 int error;
85 struct thread *td;
86 struct proc *p2;
87
88 if (!proc0.p_stats)
89 panic("kproc_create called too soon");
90
91 bzero(&fr, sizeof(fr));
92 fr.fr_flags = RFMEM | RFFDG | RFPROC | RFSTOPPED | flags;
93 fr.fr_flags2 = FR2_KPROC;
94 fr.fr_pages = pages;
95 fr.fr_procp = &p2;
96 error = fork1(&thread0, &fr);
97 if (error != 0)
98 return (error);
99
100 /* save a global descriptor, if desired */
101 if (newpp != NULL)
102 *newpp = p2;
103
104 /* set up arg0 for 'ps', et al */
105 strcpy(p2->p_comm, tdname);
106 td = FIRST_THREAD_IN_PROC(p2);
107 strcpy(td->td_name, tdname);
108 #ifdef KTR
109 sched_clear_tdname(td);
110 #endif
111 TSTHREAD(td, td->td_name);
112 #ifdef HWPMC_HOOKS
113 if (PMC_SYSTEM_SAMPLING_ACTIVE()) {
114 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_PROC_CREATE_LOG, p2);
115 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_CREATE_LOG, NULL);
116 }
117 #endif
118
119 /* call the processes' main()... */
120 cpu_fork_kthread_handler(td, func, arg);
121
122 /* Avoid inheriting affinity from a random parent. */
123 cpuset_kernthread(td);
124 thread_lock(td);
125 TD_SET_CAN_RUN(td);
126 sched_prio(td, PVM);
127 sched_user_prio(td, PUSER);
128
129 /* Delay putting it on the run queue until now. */
130 if ((flags & RFSTOPPED) == 0)
131 sched_add(td, SRQ_BORING);
132 else
133 thread_unlock(td);
134
135 return (0);
136 }
137
138 int
kproc_create(void (* func)(void *),void * arg,struct proc ** newpp,int flags,int pages,const char * fmt,...)139 kproc_create(void (*func)(void *), void *arg,
140 struct proc **newpp, int flags, int pages, const char *fmt, ...)
141 {
142 va_list ap;
143 int error;
144 char tdname[MAXCOMLEN + 1];
145
146 va_start(ap, fmt);
147 vsnprintf(tdname, sizeof(tdname), fmt, ap);
148 va_end(ap);
149 DROP_GIANT();
150 error = kproc_create1(func, arg, newpp, flags, pages, tdname);
151 PICKUP_GIANT();
152 return (error);
153 }
154
155 void
kproc_exit(int ecode)156 kproc_exit(int ecode)
157 {
158 struct thread *td;
159 struct proc *p;
160
161 td = curthread;
162 p = td->td_proc;
163
164 /*
165 * Reparent curthread from proc0 to init so that the zombie
166 * is harvested.
167 */
168 sx_xlock(&proctree_lock);
169 PROC_LOCK(p);
170 proc_reparent(p, initproc, true);
171 PROC_UNLOCK(p);
172 sx_xunlock(&proctree_lock);
173
174 /*
175 * Wakeup anyone waiting for us to exit.
176 */
177 wakeup(p);
178
179 /* Buh-bye! */
180 exit1(td, ecode, 0);
181 }
182
183 /*
184 * Advise a kernel process to suspend (or resume) in its main loop.
185 * Participation is voluntary.
186 */
187 int
kproc_suspend(struct proc * p,int timo)188 kproc_suspend(struct proc *p, int timo)
189 {
190 /*
191 * Make sure this is indeed a system process and we can safely
192 * use the p_siglist field.
193 */
194 PROC_LOCK(p);
195 if ((p->p_flag & P_KPROC) == 0) {
196 PROC_UNLOCK(p);
197 return (EINVAL);
198 }
199 SIGADDSET(p->p_siglist, SIGSTOP);
200 wakeup(p);
201 return (msleep(&p->p_siglist, &p->p_mtx, PPAUSE | PDROP,
202 "suspkp", timo));
203 }
204
205 int
kproc_resume(struct proc * p)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
kproc_suspend_check(struct proc * p)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
kthread_start(const void * udata)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 static int
kthread_add1(void (* func)(void *),void * arg,struct proc * p,struct thread ** newtdp,int flags,int pages,const char * tdname)264 kthread_add1(void (*func)(void *), void *arg, struct proc *p,
265 struct thread **newtdp, int flags, int pages, const char *tdname)
266 {
267 struct thread *newtd, *oldtd;
268
269 if (!proc0.p_stats)
270 panic("kthread_add called too soon");
271
272 /* If no process supplied, put it on proc0 */
273 if (p == NULL)
274 p = &proc0;
275
276 /* Initialize our new td */
277 newtd = thread_alloc(pages);
278 if (newtd == NULL)
279 return (ENOMEM);
280
281 PROC_LOCK(p);
282 if (p->p_state == PRS_ZOMBIE || (p->p_flag2 & P2_WEXIT) != 0) {
283 PROC_UNLOCK(p);
284 return (ESRCH);
285 }
286 oldtd = FIRST_THREAD_IN_PROC(p);
287
288 /*
289 * Set the new thread pointer before the thread starts running: *newtdp
290 * could be a pointer that is referenced by "func".
291 */
292 if (newtdp != NULL)
293 *newtdp = newtd;
294
295 bzero(&newtd->td_startzero,
296 __rangeof(struct thread, td_startzero, td_endzero));
297 bcopy(&oldtd->td_startcopy, &newtd->td_startcopy,
298 __rangeof(struct thread, td_startcopy, td_endcopy));
299
300 /* set up arg0 for 'ps', et al */
301 strcpy(newtd->td_name, tdname);
302
303 TSTHREAD(newtd, newtd->td_name);
304
305 newtd->td_proc = p; /* needed for cpu_copy_thread */
306 newtd->td_pflags |= TDP_KTHREAD;
307
308 /* might be further optimized for kthread */
309 cpu_copy_thread(newtd, oldtd);
310
311 /* put the designated function(arg) as the resume context */
312 cpu_fork_kthread_handler(newtd, func, arg);
313
314 thread_cow_get_proc(newtd, p);
315
316 /* This code is similar to thread_create() in kern_thr.c. */
317 p->p_flag |= P_HADTHREADS;
318 thread_link(newtd, p);
319 thread_lock(oldtd);
320 /* let the scheduler know about these things. */
321 sched_fork_thread(oldtd, newtd);
322 TD_SET_CAN_RUN(newtd);
323 thread_unlock(oldtd);
324 PROC_UNLOCK(p);
325
326 tidhash_add(newtd);
327
328 /* Avoid inheriting affinity from a random parent. */
329 cpuset_kernthread(newtd);
330 #ifdef HWPMC_HOOKS
331 if (PMC_SYSTEM_SAMPLING_ACTIVE())
332 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_CREATE_LOG, NULL);
333 #endif
334 /* Delay putting it on the run queue until now. */
335 if ((flags & RFSTOPPED) == 0) {
336 thread_lock(newtd);
337 sched_add(newtd, SRQ_BORING);
338 }
339 return (0);
340 }
341
342 int
kthread_add(void (* func)(void *),void * arg,struct proc * p,struct thread ** newtdp,int flags,int pages,const char * fmt,...)343 kthread_add(void (*func)(void *), void *arg, struct proc *p,
344 struct thread **newtdp, int flags, int pages, const char *fmt, ...)
345 {
346 va_list ap;
347 int error;
348 char tdname[MAXCOMLEN + 1];
349
350 va_start(ap, fmt);
351 vsnprintf(tdname, sizeof(tdname), fmt, ap);
352 va_end(ap);
353 DROP_GIANT();
354 error = kthread_add1(func, arg, p, newtdp, flags, pages, tdname);
355 PICKUP_GIANT();
356 return (error);
357 }
358
359 void
kthread_exit(void)360 kthread_exit(void)
361 {
362 struct proc *p;
363 struct thread *td;
364
365 td = curthread;
366 p = td->td_proc;
367
368 #ifdef HWPMC_HOOKS
369 if (PMC_SYSTEM_SAMPLING_ACTIVE())
370 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_EXIT_LOG, NULL);
371 #endif
372 /* A module may be waiting for us to exit. */
373 wakeup(td);
374
375 /*
376 * The last exiting thread in a kernel process must tear down
377 * the whole process.
378 */
379 PROC_LOCK(p);
380 if (p->p_numthreads == 1) {
381 PROC_UNLOCK(p);
382 kproc_exit(0);
383 }
384
385 if (p->p_sysent->sv_ontdexit != NULL)
386 p->p_sysent->sv_ontdexit(td);
387
388 tidhash_remove(td);
389 umtx_thread_exit(td);
390 tdsigcleanup(td);
391 PROC_SLOCK(p);
392 thread_exit();
393 }
394
395 /*
396 * Advise a kernel process to suspend (or resume) in its main loop.
397 * Participation is voluntary.
398 */
399 int
kthread_suspend(struct thread * td,int timo)400 kthread_suspend(struct thread *td, int timo)
401 {
402 struct proc *p;
403
404 p = td->td_proc;
405
406 /*
407 * td_pflags should not be read by any thread other than
408 * curthread, but as long as this flag is invariant during the
409 * thread's lifetime, it is OK to check its state.
410 */
411 if ((td->td_pflags & TDP_KTHREAD) == 0)
412 return (EINVAL);
413
414 /*
415 * The caller of the primitive should have already checked that the
416 * thread is up and running, thus not being blocked by other
417 * conditions.
418 */
419 PROC_LOCK(p);
420 thread_lock(td);
421 td->td_flags |= TDF_KTH_SUSP;
422 thread_unlock(td);
423 return (msleep(&td->td_flags, &p->p_mtx, PPAUSE | PDROP, "suspkt",
424 timo));
425 }
426
427 /*
428 * Resume a thread previously put asleep with kthread_suspend().
429 */
430 int
kthread_resume(struct thread * td)431 kthread_resume(struct thread *td)
432 {
433 struct proc *p;
434
435 p = td->td_proc;
436
437 /*
438 * td_pflags should not be read by any thread other than
439 * curthread, but as long as this flag is invariant during the
440 * thread's lifetime, it is OK to check its state.
441 */
442 if ((td->td_pflags & TDP_KTHREAD) == 0)
443 return (EINVAL);
444
445 PROC_LOCK(p);
446 thread_lock(td);
447 td->td_flags &= ~TDF_KTH_SUSP;
448 thread_unlock(td);
449 wakeup(&td->td_flags);
450 PROC_UNLOCK(p);
451 return (0);
452 }
453
454 /*
455 * Used by the thread to poll as to whether it should yield/sleep
456 * and notify the caller that is has happened.
457 */
458 void
kthread_suspend_check(void)459 kthread_suspend_check(void)
460 {
461 struct proc *p;
462 struct thread *td;
463
464 td = curthread;
465 p = td->td_proc;
466
467 if ((td->td_pflags & TDP_KTHREAD) == 0)
468 panic("%s: curthread is not a valid kthread", __func__);
469
470 /*
471 * Setting the TDF_KTH_SUSP flag is protected by process lock.
472 *
473 * Do an unlocked read first to avoid serializing with all other threads
474 * in the common case of not suspending.
475 */
476 if ((td->td_flags & TDF_KTH_SUSP) == 0)
477 return;
478 PROC_LOCK(p);
479 while ((td->td_flags & TDF_KTH_SUSP) != 0) {
480 wakeup(&td->td_flags);
481 msleep(&td->td_flags, &p->p_mtx, PPAUSE, "ktsusp", 0);
482 }
483 PROC_UNLOCK(p);
484 }
485
486 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,...)487 kproc_kthread_add(void (*func)(void *), void *arg,
488 struct proc **procptr, struct thread **tdptr,
489 int flags, int pages, const char *procname, const char *fmt, ...)
490 {
491 int error;
492 va_list ap;
493 char buf[100];
494 struct thread *td;
495
496 if (*procptr == NULL) {
497 /*
498 * Use RFSTOPPED to ensure that *tdptr is initialized before the
499 * thread starts running.
500 */
501 error = kproc_create(func, arg,
502 procptr, flags | RFSTOPPED, pages, "%s", procname);
503 if (error)
504 return (error);
505 td = FIRST_THREAD_IN_PROC(*procptr);
506 if (tdptr)
507 *tdptr = td;
508 if ((flags & RFSTOPPED) == 0) {
509 thread_lock(td);
510 sched_add(td, SRQ_BORING);
511 }
512 va_start(ap, fmt);
513 vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap);
514 va_end(ap);
515 #ifdef KTR
516 sched_clear_tdname(td);
517 #endif
518 return (0);
519 }
520 va_start(ap, fmt);
521 vsnprintf(buf, sizeof(buf), fmt, ap);
522 va_end(ap);
523 error = kthread_add(func, arg, *procptr,
524 tdptr, flags, pages, "%s", buf);
525 return (error);
526 }
527