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 130 /* call the processes' main()... */ 131 cpu_fork_kthread_handler(td, func, arg); 132 133 /* Avoid inheriting affinity from a random parent. */ 134 cpuset_setthread(td->td_tid, cpuset_root); 135 thread_lock(td); 136 TD_SET_CAN_RUN(td); 137 sched_prio(td, PVM); 138 sched_user_prio(td, PUSER); 139 140 /* Delay putting it on the run queue until now. */ 141 if (!(flags & RFSTOPPED)) 142 sched_add(td, SRQ_BORING); 143 thread_unlock(td); 144 145 return 0; 146 } 147 148 void 149 kproc_exit(int ecode) 150 { 151 struct thread *td; 152 struct proc *p; 153 154 td = curthread; 155 p = td->td_proc; 156 157 /* 158 * Reparent curthread from proc0 to init so that the zombie 159 * is harvested. 160 */ 161 sx_xlock(&proctree_lock); 162 PROC_LOCK(p); 163 proc_reparent(p, initproc); 164 PROC_UNLOCK(p); 165 sx_xunlock(&proctree_lock); 166 167 /* 168 * Wakeup anyone waiting for us to exit. 169 */ 170 wakeup(p); 171 172 /* Buh-bye! */ 173 exit1(td, ecode, 0); 174 } 175 176 /* 177 * Advise a kernel process to suspend (or resume) in its main loop. 178 * Participation is voluntary. 179 */ 180 int 181 kproc_suspend(struct proc *p, int timo) 182 { 183 /* 184 * Make sure this is indeed a system process and we can safely 185 * use the p_siglist field. 186 */ 187 PROC_LOCK(p); 188 if ((p->p_flag & P_KPROC) == 0) { 189 PROC_UNLOCK(p); 190 return (EINVAL); 191 } 192 SIGADDSET(p->p_siglist, SIGSTOP); 193 wakeup(p); 194 return msleep(&p->p_siglist, &p->p_mtx, PPAUSE | PDROP, "suspkp", timo); 195 } 196 197 int 198 kproc_resume(struct proc *p) 199 { 200 /* 201 * Make sure this is indeed a system process and we can safely 202 * use the p_siglist field. 203 */ 204 PROC_LOCK(p); 205 if ((p->p_flag & P_KPROC) == 0) { 206 PROC_UNLOCK(p); 207 return (EINVAL); 208 } 209 SIGDELSET(p->p_siglist, SIGSTOP); 210 PROC_UNLOCK(p); 211 wakeup(&p->p_siglist); 212 return (0); 213 } 214 215 void 216 kproc_suspend_check(struct proc *p) 217 { 218 PROC_LOCK(p); 219 while (SIGISMEMBER(p->p_siglist, SIGSTOP)) { 220 wakeup(&p->p_siglist); 221 msleep(&p->p_siglist, &p->p_mtx, PPAUSE, "kpsusp", 0); 222 } 223 PROC_UNLOCK(p); 224 } 225 226 227 /* 228 * Start a kernel thread. 229 * 230 * This function is used to start "internal" daemons and intended 231 * to be called from SYSINIT(). 232 */ 233 234 void 235 kthread_start(const void *udata) 236 { 237 const struct kthread_desc *kp = udata; 238 int error; 239 240 error = kthread_add((void (*)(void *))kp->func, NULL, 241 NULL, kp->global_threadpp, 0, 0, "%s", kp->arg0); 242 if (error) 243 panic("kthread_start: %s: error %d", kp->arg0, error); 244 } 245 246 /* 247 * Create a kernel thread. It shares its address space 248 * with proc0 - ie: kernel only. 249 * 250 * func is the function to start. 251 * arg is the parameter to pass to function on first startup. 252 * newtdp is the return value pointing to the thread's struct thread. 253 * ** XXX fix this --> flags are flags to fork1 (in unistd.h) 254 * fmt and following will be *printf'd into (*newtd)->td_name (for ps, etc.). 255 */ 256 int 257 kthread_add(void (*func)(void *), void *arg, struct proc *p, 258 struct thread **newtdp, int flags, int pages, const char *fmt, ...) 259 { 260 va_list ap; 261 struct thread *newtd, *oldtd; 262 263 if (!proc0.p_stats) 264 panic("kthread_add called too soon"); 265 266 /* If no process supplied, put it on proc0 */ 267 if (p == NULL) 268 p = &proc0; 269 270 /* Initialize our new td */ 271 newtd = thread_alloc(pages); 272 if (newtd == NULL) 273 return (ENOMEM); 274 275 PROC_LOCK(p); 276 oldtd = FIRST_THREAD_IN_PROC(p); 277 278 bzero(&newtd->td_startzero, 279 __rangeof(struct thread, td_startzero, td_endzero)); 280 bcopy(&oldtd->td_startcopy, &newtd->td_startcopy, 281 __rangeof(struct thread, td_startcopy, td_endcopy)); 282 283 /* set up arg0 for 'ps', et al */ 284 va_start(ap, fmt); 285 vsnprintf(newtd->td_name, sizeof(newtd->td_name), fmt, ap); 286 va_end(ap); 287 288 TSTHREAD(newtd, newtd->td_name); 289 290 newtd->td_proc = p; /* needed for cpu_copy_thread */ 291 /* might be further optimized for kthread */ 292 cpu_copy_thread(newtd, oldtd); 293 /* put the designated function(arg) as the resume context */ 294 cpu_fork_kthread_handler(newtd, func, arg); 295 296 newtd->td_pflags |= TDP_KTHREAD; 297 thread_cow_get_proc(newtd, p); 298 299 /* this code almost the same as create_thread() in kern_thr.c */ 300 p->p_flag |= P_HADTHREADS; 301 thread_link(newtd, p); 302 thread_lock(oldtd); 303 /* let the scheduler know about these things. */ 304 sched_fork_thread(oldtd, newtd); 305 TD_SET_CAN_RUN(newtd); 306 thread_unlock(oldtd); 307 PROC_UNLOCK(p); 308 309 tidhash_add(newtd); 310 311 /* Avoid inheriting affinity from a random parent. */ 312 cpuset_setthread(newtd->td_tid, cpuset_root); 313 314 /* Delay putting it on the run queue until now. */ 315 if (!(flags & RFSTOPPED)) { 316 thread_lock(newtd); 317 sched_add(newtd, SRQ_BORING); 318 thread_unlock(newtd); 319 } 320 if (newtdp) 321 *newtdp = newtd; 322 return 0; 323 } 324 325 void 326 kthread_exit(void) 327 { 328 struct proc *p; 329 struct thread *td; 330 331 td = curthread; 332 p = td->td_proc; 333 334 /* A module may be waiting for us to exit. */ 335 wakeup(td); 336 337 /* 338 * The last exiting thread in a kernel process must tear down 339 * the whole process. 340 */ 341 rw_wlock(&tidhash_lock); 342 PROC_LOCK(p); 343 if (p->p_numthreads == 1) { 344 PROC_UNLOCK(p); 345 rw_wunlock(&tidhash_lock); 346 kproc_exit(0); 347 } 348 LIST_REMOVE(td, td_hash); 349 rw_wunlock(&tidhash_lock); 350 umtx_thread_exit(td); 351 tdsigcleanup(td); 352 PROC_SLOCK(p); 353 thread_exit(); 354 } 355 356 /* 357 * Advise a kernel process to suspend (or resume) in its main loop. 358 * Participation is voluntary. 359 */ 360 int 361 kthread_suspend(struct thread *td, int timo) 362 { 363 struct proc *p; 364 365 p = td->td_proc; 366 367 /* 368 * td_pflags should not be read by any thread other than 369 * curthread, but as long as this flag is invariant during the 370 * thread's lifetime, it is OK to check its state. 371 */ 372 if ((td->td_pflags & TDP_KTHREAD) == 0) 373 return (EINVAL); 374 375 /* 376 * The caller of the primitive should have already checked that the 377 * thread is up and running, thus not being blocked by other 378 * conditions. 379 */ 380 PROC_LOCK(p); 381 thread_lock(td); 382 td->td_flags |= TDF_KTH_SUSP; 383 thread_unlock(td); 384 return (msleep(&td->td_flags, &p->p_mtx, PPAUSE | PDROP, "suspkt", 385 timo)); 386 } 387 388 /* 389 * Resume a thread previously put asleep with kthread_suspend(). 390 */ 391 int 392 kthread_resume(struct thread *td) 393 { 394 struct proc *p; 395 396 p = td->td_proc; 397 398 /* 399 * td_pflags should not be read by any thread other than 400 * curthread, but as long as this flag is invariant during the 401 * thread's lifetime, it is OK to check its state. 402 */ 403 if ((td->td_pflags & TDP_KTHREAD) == 0) 404 return (EINVAL); 405 406 PROC_LOCK(p); 407 thread_lock(td); 408 td->td_flags &= ~TDF_KTH_SUSP; 409 thread_unlock(td); 410 wakeup(&td->td_flags); 411 PROC_UNLOCK(p); 412 return (0); 413 } 414 415 /* 416 * Used by the thread to poll as to whether it should yield/sleep 417 * and notify the caller that is has happened. 418 */ 419 void 420 kthread_suspend_check(void) 421 { 422 struct proc *p; 423 struct thread *td; 424 425 td = curthread; 426 p = td->td_proc; 427 428 if ((td->td_pflags & TDP_KTHREAD) == 0) 429 panic("%s: curthread is not a valid kthread", __func__); 430 431 /* 432 * As long as the double-lock protection is used when accessing the 433 * TDF_KTH_SUSP flag, synchronizing the read operation via proc mutex 434 * is fine. 435 */ 436 PROC_LOCK(p); 437 while (td->td_flags & TDF_KTH_SUSP) { 438 wakeup(&td->td_flags); 439 msleep(&td->td_flags, &p->p_mtx, PPAUSE, "ktsusp", 0); 440 } 441 PROC_UNLOCK(p); 442 } 443 444 int 445 kproc_kthread_add(void (*func)(void *), void *arg, 446 struct proc **procptr, struct thread **tdptr, 447 int flags, int pages, const char *procname, const char *fmt, ...) 448 { 449 int error; 450 va_list ap; 451 char buf[100]; 452 struct thread *td; 453 454 if (*procptr == NULL) { 455 error = kproc_create(func, arg, 456 procptr, flags, pages, "%s", procname); 457 if (error) 458 return (error); 459 td = FIRST_THREAD_IN_PROC(*procptr); 460 if (tdptr) 461 *tdptr = td; 462 va_start(ap, fmt); 463 vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap); 464 va_end(ap); 465 #ifdef KTR 466 sched_clear_tdname(td); 467 #endif 468 return (0); 469 } 470 va_start(ap, fmt); 471 vsnprintf(buf, sizeof(buf), fmt, ap); 472 va_end(ap); 473 error = kthread_add(func, arg, *procptr, 474 tdptr, flags, pages, "%s", buf); 475 return (error); 476 } 477