1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 30 31 #pragma ident "%Z%%M% %I% %E% SMI" 32 33 #include <sys/types.h> 34 #include <sys/param.h> 35 #include <sys/sysmacros.h> 36 #include <sys/signal.h> 37 #include <sys/cred.h> 38 #include <sys/policy.h> 39 #include <sys/user.h> 40 #include <sys/systm.h> 41 #include <sys/cpuvar.h> 42 #include <sys/vfs.h> 43 #include <sys/vnode.h> 44 #include <sys/file.h> 45 #include <sys/errno.h> 46 #include <sys/time.h> 47 #include <sys/proc.h> 48 #include <sys/cmn_err.h> 49 #include <sys/acct.h> 50 #include <sys/tuneable.h> 51 #include <sys/class.h> 52 #include <sys/kmem.h> 53 #include <sys/session.h> 54 #include <sys/ucontext.h> 55 #include <sys/stack.h> 56 #include <sys/procfs.h> 57 #include <sys/prsystm.h> 58 #include <sys/vmsystm.h> 59 #include <sys/vtrace.h> 60 #include <sys/debug.h> 61 #include <sys/shm_impl.h> 62 #include <sys/door_data.h> 63 #include <vm/as.h> 64 #include <vm/rm.h> 65 #include <c2/audit.h> 66 #include <sys/var.h> 67 #include <sys/schedctl.h> 68 #include <sys/utrap.h> 69 #include <sys/task.h> 70 #include <sys/resource.h> 71 #include <sys/cyclic.h> 72 #include <sys/lgrp.h> 73 #include <sys/rctl.h> 74 #include <sys/contract_impl.h> 75 #include <sys/contract/process_impl.h> 76 #include <sys/list.h> 77 #include <sys/dtrace.h> 78 #include <sys/pool.h> 79 #include <sys/zone.h> 80 #include <sys/sdt.h> 81 #include <sys/class.h> 82 #include <sys/corectl.h> 83 84 static int64_t cfork(int, int); 85 static int getproc(proc_t **, int); 86 static void fork_fail(proc_t *); 87 static void forklwp_fail(proc_t *); 88 89 int fork_fail_pending; 90 91 extern struct kmem_cache *process_cache; 92 93 /* 94 * forkall system call. 95 */ 96 int64_t 97 forkall(void) 98 { 99 return (cfork(0, 0)); 100 } 101 102 /* 103 * The parent is stopped until the child invokes relvm(). 104 */ 105 int64_t 106 vfork(void) 107 { 108 curthread->t_post_sys = 1; /* so vfwait() will be called */ 109 return (cfork(1, 1)); 110 } 111 112 /* 113 * fork1 system call 114 */ 115 int64_t 116 fork1(void) 117 { 118 return (cfork(0, 1)); 119 } 120 121 /* ARGSUSED */ 122 static int64_t 123 cfork(int isvfork, int isfork1) 124 { 125 proc_t *p = ttoproc(curthread); 126 struct as *as; 127 proc_t *cp, **orphpp; 128 klwp_t *clone; 129 kthread_t *t; 130 task_t *tk; 131 rval_t r; 132 int error; 133 int i; 134 rctl_set_t *dup_set; 135 rctl_alloc_gp_t *dup_gp; 136 rctl_entity_p_t e; 137 lwpdir_t *ldp; 138 lwpent_t *lep; 139 lwpent_t *clep; 140 141 /* 142 * fork is not supported for the /proc agent lwp. 143 */ 144 if (curthread == p->p_agenttp) { 145 error = ENOTSUP; 146 goto forkerr; 147 } 148 149 if ((error = secpolicy_basic_fork(CRED())) != 0) 150 goto forkerr; 151 152 /* 153 * If the calling lwp is doing a fork1() then the 154 * other lwps in this process are not duplicated and 155 * don't need to be held where their kernel stacks can be 156 * cloned. If doing forkall(), the process is held with 157 * SHOLDFORK, so that the lwps are at a point where their 158 * stacks can be copied which is on entry or exit from 159 * the kernel. 160 */ 161 if (!holdlwps(isfork1 ? SHOLDFORK1 : SHOLDFORK)) { 162 aston(curthread); 163 error = EINTR; 164 goto forkerr; 165 } 166 167 #if defined(__sparc) 168 /* 169 * Ensure that the user stack is fully constructed 170 * before creating the child process structure. 171 */ 172 (void) flush_user_windows_to_stack(NULL); 173 #endif 174 175 mutex_enter(&p->p_lock); 176 /* 177 * If this is vfork(), cancel any suspend request we might 178 * have gotten from some other thread via lwp_suspend(). 179 * Otherwise we could end up with a deadlock on return 180 * from the vfork() in both the parent and the child. 181 */ 182 if (isvfork) 183 curthread->t_proc_flag &= ~TP_HOLDLWP; 184 /* 185 * Prevent our resource set associations from being changed during fork. 186 */ 187 pool_barrier_enter(); 188 mutex_exit(&p->p_lock); 189 190 /* 191 * Create a child proc struct. Place a VN_HOLD on appropriate vnodes. 192 */ 193 if (getproc(&cp, 0) < 0) { 194 mutex_enter(&p->p_lock); 195 pool_barrier_exit(); 196 continuelwps(p); 197 mutex_exit(&p->p_lock); 198 error = EAGAIN; 199 goto forkerr; 200 } 201 202 TRACE_2(TR_FAC_PROC, TR_PROC_FORK, "proc_fork:cp %p p %p", cp, p); 203 204 /* 205 * Assign an address space to child 206 */ 207 if (isvfork) { 208 /* 209 * Clear any watched areas and remember the 210 * watched pages for restoring in vfwait(). 211 */ 212 as = p->p_as; 213 if (avl_numnodes(&as->a_wpage) != 0) { 214 AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 215 as_clearwatch(as); 216 p->p_wpage = as->a_wpage; 217 avl_create(&as->a_wpage, wp_compare, 218 sizeof (struct watched_page), 219 offsetof(struct watched_page, wp_link)); 220 AS_LOCK_EXIT(as, &as->a_lock); 221 } 222 cp->p_as = as; 223 cp->p_flag |= SVFORK; 224 } else { 225 /* 226 * We need to hold P_PR_LOCK until the address space has 227 * been duplicated and we've had a chance to remove from the 228 * child any DTrace probes that were in the parent. Holding 229 * P_PR_LOCK prevents any new probes from being added and any 230 * extant probes from being removed. 231 */ 232 mutex_enter(&p->p_lock); 233 sprlock_proc(p); 234 p->p_flag |= SFORKING; 235 mutex_exit(&p->p_lock); 236 237 error = as_dup(p->p_as, &cp->p_as); 238 if (error != 0) { 239 fork_fail(cp); 240 mutex_enter(&pidlock); 241 orphpp = &p->p_orphan; 242 while (*orphpp != cp) 243 orphpp = &(*orphpp)->p_nextorph; 244 *orphpp = cp->p_nextorph; 245 if (p->p_child == cp) 246 p->p_child = cp->p_sibling; 247 if (cp->p_sibling) 248 cp->p_sibling->p_psibling = cp->p_psibling; 249 if (cp->p_psibling) 250 cp->p_psibling->p_sibling = cp->p_sibling; 251 mutex_enter(&cp->p_lock); 252 tk = cp->p_task; 253 task_detach(cp); 254 ASSERT(cp->p_pool->pool_ref > 0); 255 atomic_add_32(&cp->p_pool->pool_ref, -1); 256 mutex_exit(&cp->p_lock); 257 pid_exit(cp); 258 mutex_exit(&pidlock); 259 task_rele(tk); 260 261 mutex_enter(&p->p_lock); 262 p->p_flag &= ~SFORKING; 263 pool_barrier_exit(); 264 continuelwps(p); 265 sprunlock(p); 266 /* 267 * Preserve ENOMEM error condition but 268 * map all others to EAGAIN. 269 */ 270 error = (error == ENOMEM) ? ENOMEM : EAGAIN; 271 goto forkerr; 272 } 273 /* Duplicate parent's shared memory */ 274 if (p->p_segacct) 275 shmfork(p, cp); 276 277 /* 278 * Remove all DTrace tracepoints from the child process. We 279 * need to do this _before_ duplicating USDT providers since 280 * any associated probes may be immediately enabled. 281 */ 282 if (p->p_dtrace_count > 0) 283 dtrace_fasttrap_fork(p, cp); 284 285 /* 286 * Duplicate any helper actions and providers. The SFORKING 287 * we set above informs the code to enable USDT probes that 288 * sprlock() may fail because the child is being forked. 289 */ 290 if (p->p_dtrace_helpers != NULL) { 291 ASSERT(dtrace_helpers_fork != NULL); 292 (*dtrace_helpers_fork)(p, cp); 293 } 294 295 mutex_enter(&p->p_lock); 296 p->p_flag &= ~SFORKING; 297 sprunlock(p); 298 } 299 300 /* 301 * Duplicate parent's resource controls. 302 */ 303 dup_set = rctl_set_create(); 304 for (;;) { 305 dup_gp = rctl_set_dup_prealloc(p->p_rctls); 306 mutex_enter(&p->p_rctls->rcs_lock); 307 if (rctl_set_dup_ready(p->p_rctls, dup_gp)) 308 break; 309 mutex_exit(&p->p_rctls->rcs_lock); 310 rctl_prealloc_destroy(dup_gp); 311 } 312 e.rcep_p.proc = cp; 313 e.rcep_t = RCENTITY_PROCESS; 314 cp->p_rctls = rctl_set_dup(p->p_rctls, p, cp, &e, dup_set, dup_gp, 315 RCD_DUP | RCD_CALLBACK); 316 mutex_exit(&p->p_rctls->rcs_lock); 317 318 rctl_prealloc_destroy(dup_gp); 319 320 /* 321 * Allocate the child's lwp directory and lwpid hash table. 322 */ 323 if (isfork1) 324 cp->p_lwpdir_sz = 2; 325 else 326 cp->p_lwpdir_sz = p->p_lwpdir_sz; 327 cp->p_lwpdir = cp->p_lwpfree = ldp = 328 kmem_zalloc(cp->p_lwpdir_sz * sizeof (lwpdir_t), KM_SLEEP); 329 for (i = 1; i < cp->p_lwpdir_sz; i++, ldp++) 330 ldp->ld_next = ldp + 1; 331 cp->p_tidhash_sz = (cp->p_lwpdir_sz + 2) / 2; 332 cp->p_tidhash = 333 kmem_zalloc(cp->p_tidhash_sz * sizeof (lwpdir_t *), KM_SLEEP); 334 335 /* 336 * Duplicate parent's lwps. 337 * Mutual exclusion is not needed because the process is 338 * in the hold state and only the current lwp is running. 339 */ 340 klgrpset_clear(cp->p_lgrpset); 341 if (isfork1) { 342 clone = forklwp(ttolwp(curthread), cp, curthread->t_tid); 343 if (clone == NULL) 344 goto forklwperr; 345 /* 346 * Inherit only the lwp_wait()able flag, 347 * Daemon threads should not call fork1(), but oh well... 348 */ 349 lwptot(clone)->t_proc_flag |= 350 (curthread->t_proc_flag & TP_TWAIT); 351 } else { 352 /* this is forkall(), no one can be in lwp_wait() */ 353 ASSERT(p->p_lwpwait == 0 && p->p_lwpdwait == 0); 354 /* for each entry in the parent's lwp directory... */ 355 for (i = 0, ldp = p->p_lwpdir; i < p->p_lwpdir_sz; i++, ldp++) { 356 klwp_t *clwp; 357 kthread_t *ct; 358 359 if ((lep = ldp->ld_entry) == NULL) 360 continue; 361 362 if ((t = lep->le_thread) != NULL) { 363 clwp = forklwp(ttolwp(t), cp, t->t_tid); 364 if (clwp == NULL) 365 goto forklwperr; 366 ct = lwptot(clwp); 367 /* 368 * Inherit lwp_wait()able and daemon flags. 369 */ 370 ct->t_proc_flag |= 371 (t->t_proc_flag & (TP_TWAIT|TP_DAEMON)); 372 /* 373 * Keep track of the clone of curthread to 374 * post return values through lwp_setrval(). 375 * Mark other threads for special treatment 376 * by lwp_rtt() / post_syscall(). 377 */ 378 if (t == curthread) 379 clone = clwp; 380 else 381 ct->t_flag |= T_FORKALL; 382 } else { 383 /* 384 * Replicate zombie lwps in the child. 385 */ 386 clep = kmem_zalloc(sizeof (*clep), KM_SLEEP); 387 clep->le_lwpid = lep->le_lwpid; 388 clep->le_start = lep->le_start; 389 lwp_hash_in(cp, clep); 390 } 391 } 392 } 393 394 /* 395 * Put new process in the parent's process contract, or put it 396 * in a new one if there is an active process template. Send a 397 * fork event (if requested) to whatever contract the child is 398 * a member of. Fails if the parent has been SIGKILLed. 399 */ 400 if (contract_process_fork(NULL, cp, p, B_TRUE) == NULL) 401 goto forklwperr; 402 403 /* 404 * No fork failures occur beyond this point. 405 */ 406 407 cp->p_lwpid = p->p_lwpid; 408 if (!isfork1) { 409 cp->p_lwpdaemon = p->p_lwpdaemon; 410 cp->p_zombcnt = p->p_zombcnt; 411 /* 412 * If the parent's lwp ids have wrapped around, so have the 413 * child's. 414 */ 415 cp->p_flag |= p->p_flag & SLWPWRAP; 416 } 417 418 corectl_path_hold(cp->p_corefile = p->p_corefile); 419 corectl_content_hold(cp->p_content = p->p_content); 420 421 /* 422 * Duplicate process context ops, if any. 423 */ 424 if (p->p_pctx) 425 forkpctx(p, cp); 426 427 #ifdef __sparc 428 utrap_dup(p, cp); 429 #endif 430 /* 431 * If the child process has been marked to stop on exit 432 * from this fork, arrange for all other lwps to stop in 433 * sympathy with the active lwp. 434 */ 435 if (PTOU(cp)->u_systrap && 436 prismember(&PTOU(cp)->u_exitmask, curthread->t_sysnum)) { 437 mutex_enter(&cp->p_lock); 438 t = cp->p_tlist; 439 do { 440 t->t_proc_flag |= TP_PRSTOP; 441 aston(t); /* so TP_PRSTOP will be seen */ 442 } while ((t = t->t_forw) != cp->p_tlist); 443 mutex_exit(&cp->p_lock); 444 } 445 /* 446 * If the parent process has been marked to stop on exit 447 * from this fork, and its asynchronous-stop flag has not 448 * been set, arrange for all other lwps to stop before 449 * they return back to user level. 450 */ 451 if (!(p->p_proc_flag & P_PR_ASYNC) && PTOU(p)->u_systrap && 452 prismember(&PTOU(p)->u_exitmask, curthread->t_sysnum)) { 453 mutex_enter(&p->p_lock); 454 t = p->p_tlist; 455 do { 456 t->t_proc_flag |= TP_PRSTOP; 457 aston(t); /* so TP_PRSTOP will be seen */ 458 } while ((t = t->t_forw) != p->p_tlist); 459 mutex_exit(&p->p_lock); 460 } 461 462 /* set return values for child */ 463 lwp_setrval(clone, p->p_pid, 1); 464 465 /* set return values for parent */ 466 r.r_val1 = (int)cp->p_pid; 467 r.r_val2 = 0; 468 469 /* 470 * pool_barrier_exit() can now be called because the child process has: 471 * - all identifying features cloned or set (p_pid, p_task, p_pool) 472 * - all resource sets associated (p_tlist->*->t_cpupart, p_as->a_mset) 473 * - any other fields set which are used in resource set binding. 474 */ 475 mutex_enter(&p->p_lock); 476 pool_barrier_exit(); 477 mutex_exit(&p->p_lock); 478 479 mutex_enter(&pidlock); 480 mutex_enter(&cp->p_lock); 481 482 /* 483 * Now that there are lwps and threads attached, add the new 484 * process to the process group. 485 */ 486 pgjoin(cp, p->p_pgidp); 487 cp->p_stat = SRUN; 488 /* 489 * We are now done with all the lwps in the child process. 490 */ 491 t = cp->p_tlist; 492 do { 493 /* 494 * Set the lwp_suspend()ed lwps running. 495 * They will suspend properly at syscall exit. 496 */ 497 if (t->t_proc_flag & TP_HOLDLWP) 498 lwp_create_done(t); 499 else { 500 /* set TS_CREATE to allow continuelwps() to work */ 501 thread_lock(t); 502 ASSERT(t->t_state == TS_STOPPED && 503 !(t->t_schedflag & (TS_CREATE|TS_CSTART))); 504 t->t_schedflag |= TS_CREATE; 505 thread_unlock(t); 506 } 507 } while ((t = t->t_forw) != cp->p_tlist); 508 mutex_exit(&cp->p_lock); 509 510 if (isvfork) { 511 CPU_STATS_ADDQ(CPU, sys, sysvfork, 1); 512 mutex_enter(&p->p_lock); 513 p->p_flag |= SVFWAIT; 514 DTRACE_PROC1(create, proc_t *, cp); 515 cv_broadcast(&pr_pid_cv[p->p_slot]); /* inform /proc */ 516 mutex_exit(&p->p_lock); 517 /* 518 * Grab child's p_lock before dropping pidlock to ensure 519 * the process will not disappear before we set it running. 520 */ 521 mutex_enter(&cp->p_lock); 522 mutex_exit(&pidlock); 523 sigdefault(cp); 524 continuelwps(cp); 525 mutex_exit(&cp->p_lock); 526 } else { 527 CPU_STATS_ADDQ(CPU, sys, sysfork, 1); 528 DTRACE_PROC1(create, proc_t *, cp); 529 /* 530 * It is CL_FORKRET's job to drop pidlock. 531 * If we do it here, the process could be set running 532 * and disappear before CL_FORKRET() is called. 533 */ 534 CL_FORKRET(curthread, cp->p_tlist); 535 ASSERT(MUTEX_NOT_HELD(&pidlock)); 536 } 537 538 return (r.r_vals); 539 540 forklwperr: 541 if (isvfork) { 542 if (avl_numnodes(&p->p_wpage) != 0) { 543 /* restore watchpoints to parent */ 544 as = p->p_as; 545 AS_LOCK_ENTER(as, &as->a_lock, 546 RW_WRITER); 547 as->a_wpage = p->p_wpage; 548 avl_create(&p->p_wpage, wp_compare, 549 sizeof (struct watched_page), 550 offsetof(struct watched_page, wp_link)); 551 as_setwatch(as); 552 AS_LOCK_EXIT(as, &as->a_lock); 553 } 554 } else { 555 if (cp->p_segacct) 556 shmexit(cp); 557 as = cp->p_as; 558 cp->p_as = &kas; 559 as_free(as); 560 } 561 562 if (cp->p_lwpdir) { 563 for (i = 0, ldp = cp->p_lwpdir; i < cp->p_lwpdir_sz; i++, ldp++) 564 if ((lep = ldp->ld_entry) != NULL) 565 kmem_free(lep, sizeof (*lep)); 566 kmem_free(cp->p_lwpdir, 567 cp->p_lwpdir_sz * sizeof (*cp->p_lwpdir)); 568 } 569 cp->p_lwpdir = NULL; 570 cp->p_lwpfree = NULL; 571 cp->p_lwpdir_sz = 0; 572 573 if (cp->p_tidhash) 574 kmem_free(cp->p_tidhash, 575 cp->p_tidhash_sz * sizeof (*cp->p_tidhash)); 576 cp->p_tidhash = NULL; 577 cp->p_tidhash_sz = 0; 578 579 forklwp_fail(cp); 580 fork_fail(cp); 581 rctl_set_free(cp->p_rctls); 582 mutex_enter(&pidlock); 583 584 /* 585 * Detach failed child from task. 586 */ 587 mutex_enter(&cp->p_lock); 588 tk = cp->p_task; 589 task_detach(cp); 590 ASSERT(cp->p_pool->pool_ref > 0); 591 atomic_add_32(&cp->p_pool->pool_ref, -1); 592 mutex_exit(&cp->p_lock); 593 594 orphpp = &p->p_orphan; 595 while (*orphpp != cp) 596 orphpp = &(*orphpp)->p_nextorph; 597 *orphpp = cp->p_nextorph; 598 if (p->p_child == cp) 599 p->p_child = cp->p_sibling; 600 if (cp->p_sibling) 601 cp->p_sibling->p_psibling = cp->p_psibling; 602 if (cp->p_psibling) 603 cp->p_psibling->p_sibling = cp->p_sibling; 604 pid_exit(cp); 605 mutex_exit(&pidlock); 606 607 task_rele(tk); 608 609 mutex_enter(&p->p_lock); 610 pool_barrier_exit(); 611 continuelwps(p); 612 mutex_exit(&p->p_lock); 613 error = EAGAIN; 614 forkerr: 615 return ((int64_t)set_errno(error)); 616 } 617 618 /* 619 * Free allocated resources from getproc() if a fork failed. 620 */ 621 static void 622 fork_fail(proc_t *cp) 623 { 624 uf_info_t *fip = P_FINFO(cp); 625 626 fcnt_add(fip, -1); 627 sigdelq(cp, NULL, 0); 628 629 mutex_enter(&pidlock); 630 upcount_dec(crgetruid(cp->p_cred), crgetzoneid(cp->p_cred)); 631 mutex_exit(&pidlock); 632 633 /* 634 * single threaded, so no locking needed here 635 */ 636 crfree(cp->p_cred); 637 638 kmem_free(fip->fi_list, fip->fi_nfiles * sizeof (uf_entry_t)); 639 640 VN_RELE(u.u_cdir); 641 if (u.u_rdir) 642 VN_RELE(u.u_rdir); 643 if (cp->p_exec) 644 VN_RELE(cp->p_exec); 645 if (cp->p_execdir) 646 VN_RELE(cp->p_execdir); 647 if (u.u_cwd) 648 refstr_rele(u.u_cwd); 649 } 650 651 /* 652 * Clean up the lwps already created for this child process. 653 * The fork failed while duplicating all the lwps of the parent 654 * and those lwps already created must be freed. 655 * This process is invisible to the rest of the system, 656 * so we don't need to hold p->p_lock to protect the list. 657 */ 658 static void 659 forklwp_fail(proc_t *p) 660 { 661 kthread_t *t; 662 task_t *tk; 663 664 while ((t = p->p_tlist) != NULL) { 665 /* 666 * First remove the lwp from the process's p_tlist. 667 */ 668 if (t != t->t_forw) 669 p->p_tlist = t->t_forw; 670 else 671 p->p_tlist = NULL; 672 p->p_lwpcnt--; 673 t->t_forw->t_back = t->t_back; 674 t->t_back->t_forw = t->t_forw; 675 676 tk = p->p_task; 677 mutex_enter(&p->p_zone->zone_nlwps_lock); 678 tk->tk_nlwps--; 679 tk->tk_proj->kpj_nlwps--; 680 p->p_zone->zone_nlwps--; 681 mutex_exit(&p->p_zone->zone_nlwps_lock); 682 683 ASSERT(t->t_schedctl == NULL); 684 685 if (t->t_door != NULL) { 686 kmem_free(t->t_door, sizeof (door_data_t)); 687 t->t_door = NULL; 688 } 689 lwp_ctmpl_clear(ttolwp(t)); 690 691 /* 692 * Remove the thread from the all threads list. 693 * We need to hold pidlock for this. 694 */ 695 mutex_enter(&pidlock); 696 t->t_next->t_prev = t->t_prev; 697 t->t_prev->t_next = t->t_next; 698 CL_EXIT(t); /* tell the scheduler that we're exiting */ 699 cv_broadcast(&t->t_joincv); /* tell anyone in thread_join */ 700 mutex_exit(&pidlock); 701 702 /* 703 * Let the lgroup load averages know that this thread isn't 704 * going to show up (i.e. un-do what was done on behalf of 705 * this thread by the earlier lgrp_move_thread()). 706 */ 707 kpreempt_disable(); 708 lgrp_move_thread(t, NULL, 1); 709 kpreempt_enable(); 710 711 /* 712 * The thread was created TS_STOPPED. 713 * We change it to TS_FREE to avoid an 714 * ASSERT() panic in thread_free(). 715 */ 716 t->t_state = TS_FREE; 717 thread_rele(t); 718 thread_free(t); 719 } 720 } 721 722 extern struct as kas; 723 724 /* 725 * fork a kernel process. 726 */ 727 int 728 newproc(void (*pc)(), caddr_t arg, id_t cid, int pri, struct contract **ct) 729 { 730 proc_t *p; 731 struct user *up; 732 klwp_t *lwp; 733 cont_process_t *ctp = NULL; 734 rctl_entity_p_t e; 735 736 ASSERT(!(cid == syscid && ct != NULL)); 737 if (cid == syscid) { 738 rctl_alloc_gp_t *init_gp; 739 rctl_set_t *init_set; 740 741 if (getproc(&p, 1) < 0) 742 return (EAGAIN); 743 744 p->p_flag |= SNOWAIT; 745 p->p_exec = NULL; 746 p->p_execdir = NULL; 747 748 init_set = rctl_set_create(); 749 init_gp = rctl_set_init_prealloc(RCENTITY_PROCESS); 750 751 /* 752 * kernel processes do not inherit /proc tracing flags. 753 */ 754 sigemptyset(&p->p_sigmask); 755 premptyset(&p->p_fltmask); 756 up = PTOU(p); 757 up->u_systrap = 0; 758 premptyset(&(up->u_entrymask)); 759 premptyset(&(up->u_exitmask)); 760 mutex_enter(&p->p_lock); 761 e.rcep_p.proc = p; 762 e.rcep_t = RCENTITY_PROCESS; 763 p->p_rctls = rctl_set_init(RCENTITY_PROCESS, p, &e, init_set, 764 init_gp); 765 mutex_exit(&p->p_lock); 766 767 rctl_prealloc_destroy(init_gp); 768 } else { 769 rctl_alloc_gp_t *init_gp, *default_gp; 770 rctl_set_t *init_set; 771 task_t *tk, *tk_old; 772 773 if (getproc(&p, 0) < 0) 774 return (EAGAIN); 775 /* 776 * init creates a new task, distinct from the task 777 * containing kernel "processes". 778 */ 779 tk = task_create(0, p->p_zone); 780 mutex_enter(&tk->tk_zone->zone_nlwps_lock); 781 tk->tk_proj->kpj_ntasks++; 782 mutex_exit(&tk->tk_zone->zone_nlwps_lock); 783 784 default_gp = rctl_rlimit_set_prealloc(RLIM_NLIMITS); 785 init_gp = rctl_set_init_prealloc(RCENTITY_PROCESS); 786 init_set = rctl_set_create(); 787 788 mutex_enter(&pidlock); 789 mutex_enter(&p->p_lock); 790 tk_old = p->p_task; /* switch to new task */ 791 792 task_detach(p); 793 task_begin(tk, p); 794 mutex_exit(&pidlock); 795 796 e.rcep_p.proc = p; 797 e.rcep_t = RCENTITY_PROCESS; 798 p->p_rctls = rctl_set_init(RCENTITY_PROCESS, p, &e, init_set, 799 init_gp); 800 rctlproc_default_init(p, default_gp); 801 mutex_exit(&p->p_lock); 802 803 task_rele(tk_old); 804 rctl_prealloc_destroy(default_gp); 805 rctl_prealloc_destroy(init_gp); 806 } 807 808 p->p_as = &kas; 809 810 if ((lwp = lwp_create(pc, arg, 0, p, TS_STOPPED, pri, 811 &curthread->t_hold, cid, 1)) == NULL) { 812 task_t *tk; 813 fork_fail(p); 814 mutex_enter(&pidlock); 815 mutex_enter(&p->p_lock); 816 tk = p->p_task; 817 task_detach(p); 818 ASSERT(p->p_pool->pool_ref > 0); 819 atomic_add_32(&p->p_pool->pool_ref, -1); 820 mutex_exit(&p->p_lock); 821 pid_exit(p); 822 mutex_exit(&pidlock); 823 task_rele(tk); 824 825 return (EAGAIN); 826 } 827 828 if (cid != syscid) { 829 ctp = contract_process_fork(sys_process_tmpl, p, curproc, 830 B_FALSE); 831 ASSERT(ctp != NULL); 832 if (ct != NULL) 833 *ct = &ctp->conp_contract; 834 } 835 836 p->p_lwpid = 1; 837 mutex_enter(&pidlock); 838 pgjoin(p, curproc->p_pgidp); 839 p->p_stat = SRUN; 840 mutex_enter(&p->p_lock); 841 lwptot(lwp)->t_proc_flag &= ~TP_HOLDLWP; 842 lwp_create_done(lwptot(lwp)); 843 mutex_exit(&p->p_lock); 844 mutex_exit(&pidlock); 845 return (0); 846 } 847 848 /* 849 * create a child proc struct. 850 */ 851 static int 852 getproc(proc_t **cpp, int kernel) 853 { 854 proc_t *pp, *cp; 855 pid_t newpid; 856 struct user *uarea; 857 extern uint_t nproc; 858 struct cred *cr; 859 uid_t ruid; 860 zoneid_t zoneid; 861 862 if (!page_mem_avail(tune.t_minarmem)) 863 return (-1); 864 if (zone_status_get(curproc->p_zone) >= ZONE_IS_SHUTTING_DOWN) 865 return (-1); /* no point in starting new processes */ 866 867 pp = curproc; 868 cp = kmem_cache_alloc(process_cache, KM_SLEEP); 869 bzero(cp, sizeof (proc_t)); 870 871 /* 872 * Make proc entry for child process 873 */ 874 mutex_init(&cp->p_crlock, NULL, MUTEX_DEFAULT, NULL); 875 mutex_init(&cp->p_pflock, NULL, MUTEX_DEFAULT, NULL); 876 #if defined(__x86) 877 mutex_init(&cp->p_ldtlock, NULL, MUTEX_DEFAULT, NULL); 878 #endif 879 mutex_init(&cp->p_maplock, NULL, MUTEX_DEFAULT, NULL); 880 cp->p_stat = SIDL; 881 cp->p_mstart = gethrtime(); 882 883 if ((newpid = pid_assign(cp)) == -1) { 884 if (nproc == v.v_proc) { 885 CPU_STATS_ADDQ(CPU, sys, procovf, 1); 886 cmn_err(CE_WARN, "out of processes"); 887 } 888 goto bad; 889 } 890 891 /* 892 * If not privileged make sure that this user hasn't exceeded 893 * v.v_maxup processes, and that users collectively haven't 894 * exceeded v.v_maxupttl processes. 895 */ 896 mutex_enter(&pidlock); 897 ASSERT(nproc < v.v_proc); /* otherwise how'd we get our pid? */ 898 cr = CRED(); 899 ruid = crgetruid(cr); 900 zoneid = crgetzoneid(cr); 901 if (nproc >= v.v_maxup && /* short-circuit; usually false */ 902 (nproc >= v.v_maxupttl || 903 upcount_get(ruid, zoneid) >= v.v_maxup) && 904 secpolicy_newproc(cr) != 0) { 905 mutex_exit(&pidlock); 906 zcmn_err(zoneid, CE_NOTE, 907 "out of per-user processes for uid %d", ruid); 908 goto bad; 909 } 910 911 /* 912 * Everything is cool, put the new proc on the active process list. 913 * It is already on the pid list and in /proc. 914 * Increment the per uid process count (upcount). 915 */ 916 nproc++; 917 upcount_inc(ruid, zoneid); 918 919 cp->p_next = practive; 920 practive->p_prev = cp; 921 practive = cp; 922 923 cp->p_ignore = pp->p_ignore; 924 cp->p_siginfo = pp->p_siginfo; 925 cp->p_flag = pp->p_flag & (SJCTL|SNOWAIT|SNOCD); 926 cp->p_sessp = pp->p_sessp; 927 SESS_HOLD(pp->p_sessp); 928 cp->p_exec = pp->p_exec; 929 cp->p_execdir = pp->p_execdir; 930 cp->p_zone = pp->p_zone; 931 932 cp->p_bssbase = pp->p_bssbase; 933 cp->p_brkbase = pp->p_brkbase; 934 cp->p_brksize = pp->p_brksize; 935 cp->p_brkpageszc = pp->p_brkpageszc; 936 cp->p_stksize = pp->p_stksize; 937 cp->p_stkpageszc = pp->p_stkpageszc; 938 cp->p_stkprot = pp->p_stkprot; 939 cp->p_datprot = pp->p_datprot; 940 cp->p_usrstack = pp->p_usrstack; 941 cp->p_model = pp->p_model; 942 cp->p_ppid = pp->p_pid; 943 cp->p_ancpid = pp->p_pid; 944 cp->p_portcnt = pp->p_portcnt; 945 946 /* 947 * Initialize watchpoint structures 948 */ 949 avl_create(&cp->p_warea, wa_compare, sizeof (struct watched_area), 950 offsetof(struct watched_area, wa_link)); 951 952 /* 953 * Initialize immediate resource control values. 954 */ 955 cp->p_stk_ctl = pp->p_stk_ctl; 956 cp->p_fsz_ctl = pp->p_fsz_ctl; 957 cp->p_vmem_ctl = pp->p_vmem_ctl; 958 cp->p_fno_ctl = pp->p_fno_ctl; 959 960 /* 961 * Link up to parent-child-sibling chain. No need to lock 962 * in general since only a call to freeproc() (done by the 963 * same parent as newproc()) diddles with the child chain. 964 */ 965 cp->p_sibling = pp->p_child; 966 if (pp->p_child) 967 pp->p_child->p_psibling = cp; 968 969 cp->p_parent = pp; 970 pp->p_child = cp; 971 972 cp->p_child_ns = NULL; 973 cp->p_sibling_ns = NULL; 974 975 cp->p_nextorph = pp->p_orphan; 976 cp->p_nextofkin = pp; 977 pp->p_orphan = cp; 978 979 /* 980 * Inherit profiling state; do not inherit REALPROF profiling state. 981 */ 982 cp->p_prof = pp->p_prof; 983 cp->p_rprof_cyclic = CYCLIC_NONE; 984 985 /* 986 * Inherit pool pointer from the parent. Kernel processes are 987 * always bound to the default pool. 988 */ 989 mutex_enter(&pp->p_lock); 990 if (kernel) { 991 cp->p_pool = pool_default; 992 cp->p_flag |= SSYS; 993 } else { 994 cp->p_pool = pp->p_pool; 995 } 996 atomic_add_32(&cp->p_pool->pool_ref, 1); 997 mutex_exit(&pp->p_lock); 998 999 /* 1000 * Add the child process to the current task. Kernel processes 1001 * are always attached to task0. 1002 */ 1003 mutex_enter(&cp->p_lock); 1004 if (kernel) 1005 task_attach(task0p, cp); 1006 else 1007 task_attach(pp->p_task, cp); 1008 mutex_exit(&cp->p_lock); 1009 mutex_exit(&pidlock); 1010 1011 avl_create(&cp->p_ct_held, contract_compar, sizeof (contract_t), 1012 offsetof(contract_t, ct_ctlist)); 1013 1014 /* 1015 * Duplicate any audit information kept in the process table 1016 */ 1017 #ifdef C2_AUDIT 1018 if (audit_active) /* copy audit data to cp */ 1019 audit_newproc(cp); 1020 #endif 1021 1022 crhold(cp->p_cred = cr); 1023 1024 /* 1025 * Bump up the counts on the file structures pointed at by the 1026 * parent's file table since the child will point at them too. 1027 */ 1028 fcnt_add(P_FINFO(pp), 1); 1029 1030 VN_HOLD(u.u_cdir); 1031 if (u.u_rdir) 1032 VN_HOLD(u.u_rdir); 1033 if (u.u_cwd) 1034 refstr_hold(u.u_cwd); 1035 1036 /* 1037 * copy the parent's uarea. 1038 */ 1039 uarea = PTOU(cp); 1040 bcopy(PTOU(pp), uarea, sizeof (user_t)); 1041 flist_fork(P_FINFO(pp), P_FINFO(cp)); 1042 1043 gethrestime(&uarea->u_start); 1044 uarea->u_ticks = lbolt; 1045 uarea->u_mem = rm_asrss(pp->p_as); 1046 uarea->u_acflag = AFORK; 1047 1048 /* 1049 * If inherit-on-fork, copy /proc tracing flags to child. 1050 */ 1051 if ((pp->p_proc_flag & P_PR_FORK) != 0) { 1052 cp->p_proc_flag |= pp->p_proc_flag & (P_PR_TRACE|P_PR_FORK); 1053 cp->p_sigmask = pp->p_sigmask; 1054 cp->p_fltmask = pp->p_fltmask; 1055 } else { 1056 sigemptyset(&cp->p_sigmask); 1057 premptyset(&cp->p_fltmask); 1058 uarea->u_systrap = 0; 1059 premptyset(&uarea->u_entrymask); 1060 premptyset(&uarea->u_exitmask); 1061 } 1062 /* 1063 * If microstate accounting is being inherited, mark child 1064 */ 1065 if ((pp->p_flag & SMSFORK) != 0) 1066 cp->p_flag |= pp->p_flag & (SMSFORK|SMSACCT); 1067 1068 /* 1069 * Inherit fixalignment flag from the parent 1070 */ 1071 cp->p_fixalignment = pp->p_fixalignment; 1072 1073 if (cp->p_exec) 1074 VN_HOLD(cp->p_exec); 1075 if (cp->p_execdir) 1076 VN_HOLD(cp->p_execdir); 1077 *cpp = cp; 1078 return (0); 1079 1080 bad: 1081 ASSERT(MUTEX_NOT_HELD(&pidlock)); 1082 1083 mutex_destroy(&cp->p_crlock); 1084 mutex_destroy(&cp->p_pflock); 1085 #if defined(__x86) 1086 mutex_destroy(&cp->p_ldtlock); 1087 #endif 1088 if (newpid != -1) { 1089 proc_entry_free(cp->p_pidp); 1090 (void) pid_rele(cp->p_pidp); 1091 } 1092 kmem_cache_free(process_cache, cp); 1093 1094 /* 1095 * We most likely got into this situation because some process is 1096 * forking out of control. As punishment, put it to sleep for a 1097 * bit so it can't eat the machine alive. Sleep interval is chosen 1098 * to allow no more than one fork failure per cpu per clock tick 1099 * on average (yes, I just made this up). This has two desirable 1100 * properties: (1) it sets a constant limit on the fork failure 1101 * rate, and (2) the busier the system is, the harsher the penalty 1102 * for abusing it becomes. 1103 */ 1104 INCR_COUNT(&fork_fail_pending, &pidlock); 1105 delay(fork_fail_pending / ncpus + 1); 1106 DECR_COUNT(&fork_fail_pending, &pidlock); 1107 1108 return (-1); /* out of memory or proc slots */ 1109 } 1110 1111 /* 1112 * Release virtual memory. 1113 * In the case of vfork(), the child was given exclusive access to its 1114 * parent's address space. The parent is waiting in vfwait() for the 1115 * child to release its exclusive claim via relvm(). 1116 */ 1117 void 1118 relvm() 1119 { 1120 proc_t *p = curproc; 1121 1122 ASSERT((unsigned)p->p_lwpcnt <= 1); 1123 1124 prrelvm(); /* inform /proc */ 1125 1126 if (p->p_flag & SVFORK) { 1127 proc_t *pp = p->p_parent; 1128 /* 1129 * The child process is either exec'ing or exit'ing. 1130 * The child is now separated from the parent's address 1131 * space. The parent process is made dispatchable. 1132 * 1133 * This is a delicate locking maneuver, involving 1134 * both the parent's p_lock and the child's p_lock. 1135 * As soon as the SVFORK flag is turned off, the 1136 * parent is free to run, but it must not run until 1137 * we wake it up using its p_cv because it might 1138 * exit and we would be referencing invalid memory. 1139 * Therefore, we hold the parent with its p_lock 1140 * while protecting our p_flags with our own p_lock. 1141 */ 1142 try_again: 1143 mutex_enter(&p->p_lock); /* grab child's lock first */ 1144 prbarrier(p); /* make sure /proc is blocked out */ 1145 mutex_enter(&pp->p_lock); 1146 1147 /* 1148 * Check if parent is locked by /proc. 1149 */ 1150 if (pp->p_proc_flag & P_PR_LOCK) { 1151 /* 1152 * Delay until /proc is done with the parent. 1153 * We must drop our (the child's) p->p_lock, wait 1154 * via prbarrier() on the parent, then start over. 1155 */ 1156 mutex_exit(&p->p_lock); 1157 prbarrier(pp); 1158 mutex_exit(&pp->p_lock); 1159 goto try_again; 1160 } 1161 p->p_flag &= ~SVFORK; 1162 kpreempt_disable(); 1163 p->p_as = &kas; 1164 1165 /* 1166 * notify hat of change in thread's address space 1167 */ 1168 hat_thread_exit(curthread); 1169 kpreempt_enable(); 1170 1171 /* 1172 * child sizes are copied back to parent because 1173 * child may have grown. 1174 */ 1175 pp->p_brkbase = p->p_brkbase; 1176 pp->p_brksize = p->p_brksize; 1177 pp->p_stksize = p->p_stksize; 1178 /* 1179 * The parent is no longer waiting for the vfork()d child. 1180 * Restore the parent's watched pages, if any. This is 1181 * safe because we know the parent is not locked by /proc 1182 */ 1183 pp->p_flag &= ~SVFWAIT; 1184 if (avl_numnodes(&pp->p_wpage) != 0) { 1185 pp->p_as->a_wpage = pp->p_wpage; 1186 avl_create(&pp->p_wpage, wp_compare, 1187 sizeof (struct watched_page), 1188 offsetof(struct watched_page, wp_link)); 1189 } 1190 cv_signal(&pp->p_cv); 1191 mutex_exit(&pp->p_lock); 1192 mutex_exit(&p->p_lock); 1193 } else { 1194 if (p->p_as != &kas) { 1195 struct as *as; 1196 1197 if (p->p_segacct) 1198 shmexit(p); 1199 /* 1200 * We grab p_lock for the benefit of /proc 1201 */ 1202 kpreempt_disable(); 1203 mutex_enter(&p->p_lock); 1204 prbarrier(p); /* make sure /proc is blocked out */ 1205 as = p->p_as; 1206 p->p_as = &kas; 1207 mutex_exit(&p->p_lock); 1208 1209 /* 1210 * notify hat of change in thread's address space 1211 */ 1212 hat_thread_exit(curthread); 1213 kpreempt_enable(); 1214 1215 as_free(as); 1216 } 1217 } 1218 } 1219 1220 /* 1221 * Wait for child to exec or exit. 1222 * Called by parent of vfork'ed process. 1223 * See important comments in relvm(), above. 1224 */ 1225 void 1226 vfwait(pid_t pid) 1227 { 1228 int signalled = 0; 1229 proc_t *pp = ttoproc(curthread); 1230 proc_t *cp; 1231 1232 /* 1233 * Wait for child to exec or exit. 1234 */ 1235 for (;;) { 1236 mutex_enter(&pidlock); 1237 cp = prfind(pid); 1238 if (cp == NULL || cp->p_parent != pp) { 1239 /* 1240 * Child has exit()ed. 1241 */ 1242 mutex_exit(&pidlock); 1243 break; 1244 } 1245 /* 1246 * Grab the child's p_lock before releasing pidlock. 1247 * Otherwise, the child could exit and we would be 1248 * referencing invalid memory. 1249 */ 1250 mutex_enter(&cp->p_lock); 1251 mutex_exit(&pidlock); 1252 if (!(cp->p_flag & SVFORK)) { 1253 /* 1254 * Child has exec()ed or is exit()ing. 1255 */ 1256 mutex_exit(&cp->p_lock); 1257 break; 1258 } 1259 mutex_enter(&pp->p_lock); 1260 mutex_exit(&cp->p_lock); 1261 /* 1262 * We might be waked up spuriously from the cv_wait(). 1263 * We have to do the whole operation over again to be 1264 * sure the child's SVFORK flag really is turned off. 1265 * We cannot make reference to the child because it can 1266 * exit before we return and we would be referencing 1267 * invalid memory. 1268 * 1269 * Because this is potentially a very long-term wait, 1270 * we call cv_wait_sig() (for its jobcontrol and /proc 1271 * side-effects) unless there is a current signal, in 1272 * which case we use cv_wait() because we cannot return 1273 * from this function until the child has released the 1274 * address space. Calling cv_wait_sig() with a current 1275 * signal would lead to an indefinite loop here because 1276 * cv_wait_sig() returns immediately in this case. 1277 */ 1278 if (signalled) 1279 cv_wait(&pp->p_cv, &pp->p_lock); 1280 else 1281 signalled = !cv_wait_sig(&pp->p_cv, &pp->p_lock); 1282 mutex_exit(&pp->p_lock); 1283 } 1284 1285 /* restore watchpoints to parent */ 1286 if (pr_watch_active(pp)) { 1287 struct as *as = pp->p_as; 1288 AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 1289 as_setwatch(as); 1290 AS_LOCK_EXIT(as, &as->a_lock); 1291 } 1292 1293 mutex_enter(&pp->p_lock); 1294 prbarrier(pp); /* barrier against /proc locking */ 1295 continuelwps(pp); 1296 mutex_exit(&pp->p_lock); 1297 } 1298