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/sx.h> 43 #include <sys/umtx.h> 44 #include <sys/unistd.h> 45 #include <sys/wait.h> 46 #include <sys/sched.h> 47 #include <sys/tslog.h> 48 #include <vm/vm.h> 49 #include <vm/vm_extern.h> 50 51 #include <machine/stdarg.h> 52 53 /* 54 * Start a kernel process. This is called after a fork() call in 55 * mi_startup() in the file kern/init_main.c. 56 * 57 * This function is used to start "internal" daemons and intended 58 * to be called from SYSINIT(). 59 */ 60 void 61 kproc_start(const void *udata) 62 { 63 const struct kproc_desc *kp = udata; 64 int error; 65 66 error = kproc_create((void (*)(void *))kp->func, NULL, 67 kp->global_procpp, 0, 0, "%s", kp->arg0); 68 if (error) 69 panic("kproc_start: %s: error %d", kp->arg0, error); 70 } 71 72 /* 73 * Create a kernel process/thread/whatever. It shares its address space 74 * with proc0 - ie: kernel only. 75 * 76 * func is the function to start. 77 * arg is the parameter to pass to function on first startup. 78 * newpp is the return value pointing to the thread's struct proc. 79 * flags are flags to fork1 (in unistd.h) 80 * fmt and following will be *printf'd into (*newpp)->p_comm (for ps, etc.). 81 */ 82 int 83 kproc_create(void (*func)(void *), void *arg, 84 struct proc **newpp, int flags, int pages, const char *fmt, ...) 85 { 86 struct fork_req fr; 87 int error; 88 va_list ap; 89 struct thread *td; 90 struct proc *p2; 91 92 if (!proc0.p_stats) 93 panic("kproc_create called too soon"); 94 95 bzero(&fr, sizeof(fr)); 96 fr.fr_flags = RFMEM | RFFDG | RFPROC | RFSTOPPED | flags; 97 fr.fr_pages = pages; 98 fr.fr_procp = &p2; 99 error = fork1(&thread0, &fr); 100 if (error) 101 return error; 102 103 /* save a global descriptor, if desired */ 104 if (newpp != NULL) 105 *newpp = p2; 106 107 /* this is a non-swapped system process */ 108 PROC_LOCK(p2); 109 td = FIRST_THREAD_IN_PROC(p2); 110 p2->p_flag |= P_SYSTEM | P_KPROC; 111 td->td_pflags |= TDP_KTHREAD; 112 mtx_lock(&p2->p_sigacts->ps_mtx); 113 p2->p_sigacts->ps_flag |= PS_NOCLDWAIT; 114 mtx_unlock(&p2->p_sigacts->ps_mtx); 115 PROC_UNLOCK(p2); 116 117 /* set up arg0 for 'ps', et al */ 118 va_start(ap, fmt); 119 vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap); 120 va_end(ap); 121 /* set up arg0 for 'ps', et al */ 122 va_start(ap, fmt); 123 vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap); 124 va_end(ap); 125 #ifdef KTR 126 sched_clear_tdname(td); 127 #endif 128 TSTHREAD(td, td->td_name); 129 #ifdef HWPMC_HOOKS 130 if (PMC_SYSTEM_SAMPLING_ACTIVE()) { 131 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_PROC_CREATE_LOG, p2); 132 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_CREATE_LOG, NULL); 133 } 134 #endif 135 136 /* call the processes' main()... */ 137 cpu_fork_kthread_handler(td, func, arg); 138 139 /* Avoid inheriting affinity from a random parent. */ 140 cpuset_kernthread(td); 141 thread_lock(td); 142 TD_SET_CAN_RUN(td); 143 sched_prio(td, PVM); 144 sched_user_prio(td, PUSER); 145 146 /* Delay putting it on the run queue until now. */ 147 if (!(flags & RFSTOPPED)) 148 sched_add(td, SRQ_BORING); 149 else 150 thread_unlock(td); 151 152 return 0; 153 } 154 155 void 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 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, "suspkp", timo); 202 } 203 204 int 205 kproc_resume(struct proc *p) 206 { 207 /* 208 * Make sure this is indeed a system process and we can safely 209 * use the p_siglist field. 210 */ 211 PROC_LOCK(p); 212 if ((p->p_flag & P_KPROC) == 0) { 213 PROC_UNLOCK(p); 214 return (EINVAL); 215 } 216 SIGDELSET(p->p_siglist, SIGSTOP); 217 PROC_UNLOCK(p); 218 wakeup(&p->p_siglist); 219 return (0); 220 } 221 222 void 223 kproc_suspend_check(struct proc *p) 224 { 225 PROC_LOCK(p); 226 while (SIGISMEMBER(p->p_siglist, SIGSTOP)) { 227 wakeup(&p->p_siglist); 228 msleep(&p->p_siglist, &p->p_mtx, PPAUSE, "kpsusp", 0); 229 } 230 PROC_UNLOCK(p); 231 } 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 rw_wlock(&tidhash_lock); 355 PROC_LOCK(p); 356 if (p->p_numthreads == 1) { 357 PROC_UNLOCK(p); 358 rw_wunlock(&tidhash_lock); 359 kproc_exit(0); 360 } 361 LIST_REMOVE(td, td_hash); 362 rw_wunlock(&tidhash_lock); 363 umtx_thread_exit(td); 364 tdsigcleanup(td); 365 PROC_SLOCK(p); 366 thread_exit(); 367 } 368 369 /* 370 * Advise a kernel process to suspend (or resume) in its main loop. 371 * Participation is voluntary. 372 */ 373 int 374 kthread_suspend(struct thread *td, int timo) 375 { 376 struct proc *p; 377 378 p = td->td_proc; 379 380 /* 381 * td_pflags should not be read by any thread other than 382 * curthread, but as long as this flag is invariant during the 383 * thread's lifetime, it is OK to check its state. 384 */ 385 if ((td->td_pflags & TDP_KTHREAD) == 0) 386 return (EINVAL); 387 388 /* 389 * The caller of the primitive should have already checked that the 390 * thread is up and running, thus not being blocked by other 391 * conditions. 392 */ 393 PROC_LOCK(p); 394 thread_lock(td); 395 td->td_flags |= TDF_KTH_SUSP; 396 thread_unlock(td); 397 return (msleep(&td->td_flags, &p->p_mtx, PPAUSE | PDROP, "suspkt", 398 timo)); 399 } 400 401 /* 402 * Resume a thread previously put asleep with kthread_suspend(). 403 */ 404 int 405 kthread_resume(struct thread *td) 406 { 407 struct proc *p; 408 409 p = td->td_proc; 410 411 /* 412 * td_pflags should not be read by any thread other than 413 * curthread, but as long as this flag is invariant during the 414 * thread's lifetime, it is OK to check its state. 415 */ 416 if ((td->td_pflags & TDP_KTHREAD) == 0) 417 return (EINVAL); 418 419 PROC_LOCK(p); 420 thread_lock(td); 421 td->td_flags &= ~TDF_KTH_SUSP; 422 thread_unlock(td); 423 wakeup(&td->td_flags); 424 PROC_UNLOCK(p); 425 return (0); 426 } 427 428 /* 429 * Used by the thread to poll as to whether it should yield/sleep 430 * and notify the caller that is has happened. 431 */ 432 void 433 kthread_suspend_check(void) 434 { 435 struct proc *p; 436 struct thread *td; 437 438 td = curthread; 439 p = td->td_proc; 440 441 if ((td->td_pflags & TDP_KTHREAD) == 0) 442 panic("%s: curthread is not a valid kthread", __func__); 443 444 /* 445 * As long as the double-lock protection is used when accessing the 446 * TDF_KTH_SUSP flag, synchronizing the read operation via proc mutex 447 * is fine. 448 */ 449 PROC_LOCK(p); 450 while (td->td_flags & TDF_KTH_SUSP) { 451 wakeup(&td->td_flags); 452 msleep(&td->td_flags, &p->p_mtx, PPAUSE, "ktsusp", 0); 453 } 454 PROC_UNLOCK(p); 455 } 456 457 int 458 kproc_kthread_add(void (*func)(void *), void *arg, 459 struct proc **procptr, struct thread **tdptr, 460 int flags, int pages, const char *procname, const char *fmt, ...) 461 { 462 int error; 463 va_list ap; 464 char buf[100]; 465 struct thread *td; 466 467 if (*procptr == NULL) { 468 error = kproc_create(func, arg, 469 procptr, flags, pages, "%s", procname); 470 if (error) 471 return (error); 472 td = FIRST_THREAD_IN_PROC(*procptr); 473 if (tdptr) 474 *tdptr = td; 475 va_start(ap, fmt); 476 vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap); 477 va_end(ap); 478 #ifdef KTR 479 sched_clear_tdname(td); 480 #endif 481 return (0); 482 } 483 va_start(ap, fmt); 484 vsnprintf(buf, sizeof(buf), fmt, ap); 485 va_end(ap); 486 error = kthread_add(func, arg, *procptr, 487 tdptr, flags, pages, "%s", buf); 488 return (error); 489 } 490