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