kernel - Fix cumulative nprocs bug
[dragonfly.git] / sys / kern / kern_fork.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)kern_fork.c 8.6 (Berkeley) 4/8/94
5bc7cd8d 39 * $FreeBSD: src/sys/kern/kern_fork.c,v 1.72.2.14 2003/06/26 04:15:10 silby Exp $
f6c36234 40 * $DragonFly: src/sys/kern/kern_fork.c,v 1.77 2008/05/18 20:02:02 nth Exp $
984263bc
MD
41 */
42
43#include "opt_ktrace.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/sysproto.h>
48#include <sys/filedesc.h>
49#include <sys/kernel.h>
50#include <sys/sysctl.h>
51#include <sys/malloc.h>
52#include <sys/proc.h>
53#include <sys/resourcevar.h>
54#include <sys/vnode.h>
55#include <sys/acct.h>
56#include <sys/ktrace.h>
dfc1fc13
EN
57#include <sys/unistd.h>
58#include <sys/jail.h>
59#include <sys/caps.h>
984263bc
MD
60
61#include <vm/vm.h>
62#include <sys/lock.h>
63#include <vm/pmap.h>
64#include <vm/vm_map.h>
65#include <vm/vm_extern.h>
984263bc
MD
66
67#include <sys/vmmeter.h>
19bfc8ab 68#include <sys/refcount.h>
e43a034f 69#include <sys/thread2.h>
b1b4e5a6 70#include <sys/signal2.h>
8f1f6170 71#include <sys/spinlock2.h>
984263bc 72
8c72e3d5
AH
73#include <sys/dsched.h>
74
984263bc
MD
75static MALLOC_DEFINE(M_ATFORK, "atfork", "atfork callback");
76
77/*
78 * These are the stuctures used to create a callout list for things to do
79 * when forking a process
80 */
81struct forklist {
82 forklist_fn function;
83 TAILQ_ENTRY(forklist) next;
84};
85
86TAILQ_HEAD(forklist_head, forklist);
87static struct forklist_head fork_list = TAILQ_HEAD_INITIALIZER(fork_list);
88
13d13d89
SS
89static struct lwp *lwp_fork(struct lwp *, struct proc *, int flags);
90
984263bc
MD
91int forksleep; /* Place for fork1() to sleep on. */
92
3e291793
MD
93/*
94 * Red-Black tree support for LWPs
95 */
96
97static int
98rb_lwp_compare(struct lwp *lp1, struct lwp *lp2)
99{
100 if (lp1->lwp_tid < lp2->lwp_tid)
101 return(-1);
102 if (lp1->lwp_tid > lp2->lwp_tid)
103 return(1);
104 return(0);
105}
106
107RB_GENERATE2(lwp_rb_tree, lwp, u.lwp_rbnode, rb_lwp_compare, lwpid_t, lwp_tid);
108
3919ced0
MD
109/*
110 * Fork system call
111 *
112 * MPALMOSTSAFE
113 */
984263bc 114int
753fd850 115sys_fork(struct fork_args *uap)
984263bc 116{
553ea3c8 117 struct lwp *lp = curthread->td_lwp;
984263bc 118 struct proc *p2;
41c20dac 119 int error;
984263bc 120
167e6ecb 121 error = fork1(lp, RFFDG | RFPROC | RFPGLOCK, &p2);
984263bc 122 if (error == 0) {
553ea3c8 123 start_forked_proc(lp, p2);
c7114eea
MD
124 uap->sysmsg_fds[0] = p2->p_pid;
125 uap->sysmsg_fds[1] = 0;
984263bc
MD
126 }
127 return error;
128}
129
3919ced0
MD
130/*
131 * MPALMOSTSAFE
132 */
984263bc 133int
753fd850 134sys_vfork(struct vfork_args *uap)
984263bc 135{
553ea3c8 136 struct lwp *lp = curthread->td_lwp;
984263bc 137 struct proc *p2;
41c20dac 138 int error;
984263bc 139
167e6ecb 140 error = fork1(lp, RFFDG | RFPROC | RFPPWAIT | RFMEM | RFPGLOCK, &p2);
984263bc 141 if (error == 0) {
553ea3c8 142 start_forked_proc(lp, p2);
c7114eea
MD
143 uap->sysmsg_fds[0] = p2->p_pid;
144 uap->sysmsg_fds[1] = 0;
984263bc
MD
145 }
146 return error;
147}
148
f61c1ff1
MD
149/*
150 * Handle rforks. An rfork may (1) operate on the current process without
151 * creating a new, (2) create a new process that shared the current process's
152 * vmspace, signals, and/or descriptors, or (3) create a new process that does
153 * not share these things (normal fork).
154 *
155 * Note that we only call start_forked_proc() if a new process is actually
156 * created.
157 *
158 * rfork { int flags }
3919ced0
MD
159 *
160 * MPALMOSTSAFE
f61c1ff1 161 */
984263bc 162int
753fd850 163sys_rfork(struct rfork_args *uap)
984263bc 164{
553ea3c8 165 struct lwp *lp = curthread->td_lwp;
984263bc 166 struct proc *p2;
41c20dac 167 int error;
984263bc 168
6654fbcb
MD
169 if ((uap->flags & RFKERNELONLY) != 0)
170 return (EINVAL);
171
167e6ecb 172 error = fork1(lp, uap->flags | RFPGLOCK, &p2);
984263bc 173 if (error == 0) {
f61c1ff1 174 if (p2)
553ea3c8 175 start_forked_proc(lp, p2);
c7114eea
MD
176 uap->sysmsg_fds[0] = p2 ? p2->p_pid : 0;
177 uap->sysmsg_fds[1] = 0;
984263bc
MD
178 }
179 return error;
180}
181
3919ced0
MD
182/*
183 * MPALMOSTSAFE
184 */
91bd9c1e
SS
185int
186sys_lwp_create(struct lwp_create_args *uap)
187{
188 struct proc *p = curproc;
189 struct lwp *lp;
190 struct lwp_params params;
191 int error;
192
193 error = copyin(uap->params, &params, sizeof(params));
194 if (error)
195 goto fail2;
196
b5c4d81f 197 lwkt_gettoken(&p->p_token);
8f1f6170 198 plimit_lwp_fork(p); /* force exclusive access */
91bd9c1e
SS
199 lp = lwp_fork(curthread->td_lwp, p, RFPROC);
200 error = cpu_prepare_lwp(lp, &params);
201 if (params.tid1 != NULL &&
202 (error = copyout(&lp->lwp_tid, params.tid1, sizeof(lp->lwp_tid))))
203 goto fail;
204 if (params.tid2 != NULL &&
205 (error = copyout(&lp->lwp_tid, params.tid2, sizeof(lp->lwp_tid))))
206 goto fail;
207
208 /*
8f1f6170 209 * Now schedule the new lwp.
91bd9c1e
SS
210 */
211 p->p_usched->resetpriority(lp);
212 crit_enter();
213 lp->lwp_stat = LSRUN;
214 p->p_usched->setrunqueue(lp);
215 crit_exit();
b5c4d81f 216 lwkt_reltoken(&p->p_token);
91bd9c1e
SS
217
218 return (0);
219
220fail:
3e291793 221 lwp_rb_tree_RB_REMOVE(&p->p_lwp_tree, lp);
0b26dde3 222 --p->p_nthreads;
e3161323
MD
223 /* lwp_dispose expects an exited lwp, and a held proc */
224 lp->lwp_flag |= LWP_WEXIT;
225 lp->lwp_thread->td_flags |= TDF_EXITING;
226 PHOLD(p);
91bd9c1e 227 lwp_dispose(lp);
b5c4d81f 228 lwkt_reltoken(&p->p_token);
91bd9c1e
SS
229fail2:
230 return (error);
231}
984263bc
MD
232
233int nprocs = 1; /* process 0 */
984263bc
MD
234
235int
553ea3c8 236fork1(struct lwp *lp1, int flags, struct proc **procp)
984263bc 237{
553ea3c8 238 struct proc *p1 = lp1->lwp_proc;
984263bc 239 struct proc *p2, *pptr;
58c2553a
MD
240 struct pgrp *p1grp;
241 struct pgrp *plkgrp;
984263bc 242 uid_t uid;
167e6ecb 243 int ok, error;
51e64ff2 244 static int curfail = 0;
5bc7cd8d 245 static struct timeval lastfail;
984263bc
MD
246 struct forklist *ep;
247 struct filedesc_to_leader *fdtol;
248
249 if ((flags & (RFFDG|RFCFDG)) == (RFFDG|RFCFDG))
250 return (EINVAL);
251
b5c4d81f 252 lwkt_gettoken(&p1->p_token);
58c2553a 253 plkgrp = NULL;
b5c4d81f 254
984263bc
MD
255 /*
256 * Here we don't create a new process, but we divorce
257 * certain parts of a process from itself.
258 */
259 if ((flags & RFPROC) == 0) {
13d13d89
SS
260 /*
261 * This kind of stunt does not work anymore if
262 * there are native threads (lwps) running
263 */
b5c4d81f
MD
264 if (p1->p_nthreads != 1) {
265 error = EINVAL;
266 goto done;
267 }
13d13d89
SS
268
269 vm_fork(p1, 0, flags);
984263bc
MD
270
271 /*
272 * Close all file descriptors.
273 */
274 if (flags & RFCFDG) {
275 struct filedesc *fdtmp;
276 fdtmp = fdinit(p1);
0a4a9c77 277 fdfree(p1, fdtmp);
984263bc
MD
278 }
279
280 /*
281 * Unshare file descriptors (from parent.)
282 */
283 if (flags & RFFDG) {
284 if (p1->p_fd->fd_refcnt > 1) {
285 struct filedesc *newfd;
2994659f
VS
286 error = fdcopy(p1, &newfd);
287 if (error != 0) {
288 error = ENOMEM;
289 goto done;
290 }
0a4a9c77 291 fdfree(p1, newfd);
984263bc
MD
292 }
293 }
294 *procp = NULL;
b5c4d81f
MD
295 error = 0;
296 goto done;
984263bc
MD
297 }
298
167e6ecb
MD
299 /*
300 * Interlock against process group signal delivery. If signals
301 * are pending after the interlock is obtained we have to restart
302 * the system call to process the signals. If we don't the child
303 * can miss a pgsignal (such as ^C) sent during the fork.
304 *
305 * We can't use CURSIG() here because it will process any STOPs
306 * and cause the process group lock to be held indefinitely. If
307 * a STOP occurs, the fork will be restarted after the CONT.
308 */
58c2553a
MD
309 p1grp = p1->p_pgrp;
310 if ((flags & RFPGLOCK) && (plkgrp = p1->p_pgrp) != NULL) {
311 pgref(plkgrp);
312 lockmgr(&plkgrp->pg_lock, LK_SHARED);
f6e73860 313 if (CURSIG_NOBLOCK(lp1)) {
167e6ecb
MD
314 error = ERESTART;
315 goto done;
316 }
317 }
318
984263bc
MD
319 /*
320 * Although process entries are dynamically created, we still keep
321 * a global limit on the maximum number we will create. Don't allow
322 * a nonprivileged user to use the last ten processes; don't let root
323 * exceed the limit. The variable nprocs is the current number of
324 * processes, maxproc is the limit.
325 */
9910d07b 326 uid = lp1->lwp_thread->td_ucred->cr_ruid;
984263bc 327 if ((nprocs >= maxproc - 10 && uid != 0) || nprocs >= maxproc) {
5bc7cd8d 328 if (ppsratecheck(&lastfail, &curfail, 1))
6ea70f76 329 kprintf("maxproc limit exceeded by uid %d, please "
5bc7cd8d 330 "see tuning(7) and login.conf(5).\n", uid);
377d4740 331 tsleep(&forksleep, 0, "fork", hz / 2);
167e6ecb
MD
332 error = EAGAIN;
333 goto done;
984263bc 334 }
8c2bce60 335
984263bc
MD
336 /*
337 * Increment the nprocs resource before blocking can occur. There
338 * are hard-limits as to the number of processes that can run.
339 */
8c2bce60 340 atomic_add_int(&nprocs, 1);
984263bc
MD
341
342 /*
343 * Increment the count of procs running with this uid. Don't allow
344 * a nonprivileged user to exceed their current limit.
345 */
9910d07b 346 ok = chgproccnt(lp1->lwp_thread->td_ucred->cr_ruidinfo, 1,
984263bc
MD
347 (uid != 0) ? p1->p_rlimit[RLIMIT_NPROC].rlim_cur : 0);
348 if (!ok) {
349 /*
350 * Back out the process count
351 */
8c2bce60 352 atomic_add_int(&nprocs, -1);
5bc7cd8d 353 if (ppsratecheck(&lastfail, &curfail, 1))
6ea70f76 354 kprintf("maxproc limit exceeded by uid %d, please "
5bc7cd8d 355 "see tuning(7) and login.conf(5).\n", uid);
377d4740 356 tsleep(&forksleep, 0, "fork", hz / 2);
167e6ecb
MD
357 error = EAGAIN;
358 goto done;
984263bc
MD
359 }
360
361 /* Allocate new proc. */
37733243 362 p2 = kmalloc(sizeof(struct proc), M_PROC, M_WAITOK|M_ZERO);
984263bc
MD
363
364 /*
ef09c3ed 365 * Setup linkage for kernel based threading XXX lwp
984263bc 366 */
cb74210d 367 if (flags & RFTHREAD) {
51e64ff2
MD
368 p2->p_peers = p1->p_peers;
369 p1->p_peers = p2;
370 p2->p_leader = p1->p_leader;
984263bc 371 } else {
51e64ff2 372 p2->p_leader = p2;
984263bc
MD
373 }
374
3e291793 375 RB_INIT(&p2->p_lwp_tree);
8f1f6170 376 spin_init(&p2->p_spin);
8c2bce60 377 lwkt_token_init(&p2->p_token, "proc");
3e291793 378 p2->p_lasttid = -1; /* first tid will be 0 */
ef09c3ed 379
984263bc 380 /*
51e64ff2
MD
381 * Setting the state to SIDL protects the partially initialized
382 * process once it starts getting hooked into the rest of the system.
984263bc 383 */
51e64ff2
MD
384 p2->p_stat = SIDL;
385 proc_add_allproc(p2);
984263bc
MD
386
387 /*
388 * Make a proc table entry for the new process.
f9fe34b6
SS
389 * The whole structure was zeroed above, so copy the section that is
390 * copied directly from the parent.
984263bc 391 */
984263bc
MD
392 bcopy(&p1->p_startcopy, &p2->p_startcopy,
393 (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy));
394
984263bc 395 /*
88072e3b
MD
396 * Duplicate sub-structures as needed. Increase reference counts
397 * on shared objects.
398 *
399 * NOTE: because we are now on the allproc list it is possible for
400 * other consumers to gain temporary references to p2
401 * (p2->p_lock can change).
984263bc 402 */
984263bc
MD
403 if (p1->p_flag & P_PROFIL)
404 startprofclock(p2);
9910d07b 405 p2->p_ucred = crhold(lp1->lwp_thread->td_ucred);
984263bc 406
b40e316c 407 if (jailed(p2->p_ucred))
984263bc 408 p2->p_flag |= P_JAILED;
984263bc
MD
409
410 if (p2->p_args)
19bfc8ab 411 refcount_acquire(&p2->p_args->ar_ref);
984263bc 412
13d13d89 413 p2->p_usched = p1->p_usched;
8c72e3d5
AH
414 /* XXX: verify copy of the secondary iosched stuff */
415 dsched_new_proc(p2);
13d13d89 416
984263bc 417 if (flags & RFSIGSHARE) {
b1b4e5a6 418 p2->p_sigacts = p1->p_sigacts;
6fa9e71a 419 refcount_acquire(&p2->p_sigacts->ps_refcnt);
984263bc 420 } else {
6fa9e71a
MD
421 p2->p_sigacts = kmalloc(sizeof(*p2->p_sigacts),
422 M_SUBPROC, M_WAITOK);
b1b4e5a6 423 bcopy(p1->p_sigacts, p2->p_sigacts, sizeof(*p2->p_sigacts));
6fa9e71a 424 refcount_init(&p2->p_sigacts->ps_refcnt, 1);
984263bc
MD
425 }
426 if (flags & RFLINUXTHPN)
427 p2->p_sigparent = SIGUSR1;
428 else
429 p2->p_sigparent = SIGCHLD;
430
431 /* bump references to the text vnode (for procfs) */
432 p2->p_textvp = p1->p_textvp;
433 if (p2->p_textvp)
597aea93 434 vref(p2->p_textvp);
984263bc 435
8ba5f7ef
AH
436 /* copy namecache handle to the text file */
437 if (p1->p_textnch.mount)
438 cache_copy(&p1->p_textnch, &p2->p_textnch);
439
0daa37a5
MD
440 /*
441 * Handle file descriptors
442 */
984263bc
MD
443 if (flags & RFCFDG) {
444 p2->p_fd = fdinit(p1);
445 fdtol = NULL;
446 } else if (flags & RFFDG) {
2994659f
VS
447 error = fdcopy(p1, &p2->p_fd);
448 if (error != 0) {
449 error = ENOMEM;
450 goto done;
451 }
984263bc
MD
452 fdtol = NULL;
453 } else {
454 p2->p_fd = fdshare(p1);
b5c4d81f
MD
455 if (p1->p_fdtol == NULL) {
456 lwkt_gettoken(&p1->p_token);
984263bc
MD
457 p1->p_fdtol =
458 filedesc_to_leader_alloc(NULL,
459 p1->p_leader);
b5c4d81f
MD
460 lwkt_reltoken(&p1->p_token);
461 }
984263bc
MD
462 if ((flags & RFTHREAD) != 0) {
463 /*
464 * Shared file descriptor table and
465 * shared process leaders.
466 */
467 fdtol = p1->p_fdtol;
468 fdtol->fdl_refcount++;
469 } else {
470 /*
471 * Shared file descriptor table, and
472 * different process leaders
473 */
98a7f915 474 fdtol = filedesc_to_leader_alloc(p1->p_fdtol, p2);
984263bc
MD
475 }
476 }
477 p2->p_fdtol = fdtol;
8f1f6170 478 p2->p_limit = plimit_fork(p1);
984263bc
MD
479
480 /*
481 * Preserve some more flags in subprocess. P_PROFIL has already
482 * been preserved.
483 */
08f2f1bb 484 p2->p_flag |= p1->p_flag & P_SUGID;
984263bc
MD
485 if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT)
486 p2->p_flag |= P_CONTROLT;
487 if (flags & RFPPWAIT)
488 p2->p_flag |= P_PPWAIT;
489
0daa37a5
MD
490 /*
491 * Inherit the virtual kernel structure (allows a virtual kernel
492 * to fork to simulate multiple cpus).
493 */
4a22e893
MD
494 if (p1->p_vkernel)
495 vkernel_inherit(p1, p2);
0daa37a5 496
5fd012e0
MD
497 /*
498 * Once we are on a pglist we may receive signals. XXX we might
499 * race a ^C being sent to the process group by not receiving it
500 * at all prior to this line.
501 */
58c2553a
MD
502 pgref(p1grp);
503 lwkt_gettoken(&p1grp->pg_token);
984263bc 504 LIST_INSERT_AFTER(p1, p2, p_pglist);
58c2553a 505 lwkt_reltoken(&p1grp->pg_token);
984263bc
MD
506
507 /*
508 * Attach the new process to its parent.
509 *
510 * If RFNOWAIT is set, the newly created process becomes a child
511 * of init. This effectively disassociates the child from the
512 * parent.
513 */
514 if (flags & RFNOWAIT)
515 pptr = initproc;
516 else
517 pptr = p1;
518 p2->p_pptr = pptr;
984263bc 519 LIST_INIT(&p2->p_children);
b5c4d81f
MD
520
521 lwkt_gettoken(&pptr->p_token);
522 LIST_INSERT_HEAD(&pptr->p_children, p2, p_sibling);
523 lwkt_reltoken(&pptr->p_token);
524
98a7f915 525 varsymset_init(&p2->p_varsymset, &p1->p_varsymset);
8c2bce60 526 callout_init_mp(&p2->p_ithandle);
984263bc
MD
527
528#ifdef KTRACE
529 /*
530 * Copy traceflag and tracefile if enabled. If not inherited,
531 * these were zeroed above but we still could have a trace race
29f58392 532 * so make sure p2's p_tracenode is NULL.
984263bc 533 */
29f58392 534 if ((p1->p_traceflag & KTRFAC_INHERIT) && p2->p_tracenode == NULL) {
984263bc 535 p2->p_traceflag = p1->p_traceflag;
29f58392 536 p2->p_tracenode = ktrinherit(p1->p_tracenode);
984263bc
MD
537 }
538#endif
539
984263bc
MD
540 /*
541 * This begins the section where we must prevent the parent
542 * from being swapped.
13d13d89
SS
543 *
544 * Gets PRELE'd in the caller in start_forked_proc().
984263bc
MD
545 */
546 PHOLD(p1);
547
13d13d89
SS
548 vm_fork(p1, p2, flags);
549
984263bc 550 /*
13d13d89
SS
551 * Create the first lwp associated with the new proc.
552 * It will return via a different execution path later, directly
553 * into userland, after it was put on the runq by
554 * start_forked_proc().
984263bc 555 */
13d13d89 556 lwp_fork(lp1, p2, flags);
984263bc 557
6b72a0c2 558 if (flags == (RFFDG | RFPROC | RFPGLOCK)) {
12e4aaff
MD
559 mycpu->gd_cnt.v_forks++;
560 mycpu->gd_cnt.v_forkpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize;
6b72a0c2 561 } else if (flags == (RFFDG | RFPROC | RFPPWAIT | RFMEM | RFPGLOCK)) {
12e4aaff
MD
562 mycpu->gd_cnt.v_vforks++;
563 mycpu->gd_cnt.v_vforkpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize;
984263bc 564 } else if (p1 == &proc0) {
12e4aaff
MD
565 mycpu->gd_cnt.v_kthreads++;
566 mycpu->gd_cnt.v_kthreadpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize;
984263bc 567 } else {
12e4aaff
MD
568 mycpu->gd_cnt.v_rforks++;
569 mycpu->gd_cnt.v_rforkpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize;
984263bc
MD
570 }
571
572 /*
573 * Both processes are set up, now check if any loadable modules want
574 * to adjust anything.
575 * What if they have an error? XXX
576 */
577 TAILQ_FOREACH(ep, &fork_list, next) {
578 (*ep->function)(p1, p2, flags);
579 }
580
581 /*
a77ac49d
MD
582 * Set the start time. Note that the process is not runnable. The
583 * caller is responsible for making it runnable.
984263bc 584 */
d9fa5f67 585 microtime(&p2->p_start);
984263bc 586 p2->p_acflag = AFORK;
984263bc
MD
587
588 /*
589 * tell any interested parties about the new process
590 */
591 KNOTE(&p1->p_klist, NOTE_FORK | p2->p_pid);
592
984263bc
MD
593 /*
594 * Return child proc pointer to parent.
595 */
596 *procp = p2;
b5c4d81f 597 error = 0;
167e6ecb 598done:
b5c4d81f 599 lwkt_reltoken(&p1->p_token);
58c2553a
MD
600 if (plkgrp) {
601 lockmgr(&plkgrp->pg_lock, LK_RELEASE);
602 pgrel(plkgrp);
603 }
167e6ecb 604 return (error);
984263bc
MD
605}
606
13d13d89
SS
607static struct lwp *
608lwp_fork(struct lwp *origlp, struct proc *destproc, int flags)
609{
610 struct lwp *lp;
611 struct thread *td;
13d13d89 612
f6c36234 613 lp = kmalloc(sizeof(struct lwp), M_LWP, M_WAITOK|M_ZERO);
3e291793 614
13d13d89 615 lp->lwp_proc = destproc;
287ebb09 616 lp->lwp_vmspace = destproc->p_vmspace;
13d13d89 617 lp->lwp_stat = LSRUN;
13d13d89
SS
618 bcopy(&origlp->lwp_startcopy, &lp->lwp_startcopy,
619 (unsigned) ((caddr_t)&lp->lwp_endcopy -
620 (caddr_t)&lp->lwp_startcopy));
13d13d89
SS
621 lp->lwp_flag |= origlp->lwp_flag & LWP_ALTSTACK;
622 /*
623 * Set cpbase to the last timeout that occured (not the upcoming
624 * timeout).
625 *
626 * A critical section is required since a timer IPI can update
627 * scheduler specific data.
628 */
629 crit_enter();
630 lp->lwp_cpbase = mycpu->gd_schedclock.time -
631 mycpu->gd_schedclock.periodic;
632 destproc->p_usched->heuristic_forking(origlp, lp);
633 crit_exit();
a5ae2446 634 lp->lwp_cpumask &= usched_mastermask;
13d13d89 635
fdce8919 636 td = lwkt_alloc_thread(NULL, LWKT_THREAD_STACK, -1, 0);
13d13d89
SS
637 lp->lwp_thread = td;
638 td->td_proc = destproc;
639 td->td_lwp = lp;
640 td->td_switch = cpu_heavy_switch;
13d13d89
SS
641 lwkt_setpri(td, TDPRI_KERN_USER);
642 lwkt_set_comm(td, "%s", destproc->p_comm);
643
644 /*
645 * cpu_fork will copy and update the pcb, set up the kernel stack,
646 * and make the child ready to run.
647 */
648 cpu_fork(origlp, lp, flags);
649 caps_fork(origlp->lwp_thread, lp->lwp_thread);
a591f597 650 kqueue_init(&lp->lwp_kqueue, destproc->p_fd);
13d13d89 651
0d78b86e
MD
652 /*
653 * Assign a TID to the lp. Loop until the insert succeeds (returns
654 * NULL).
655 */
656 lp->lwp_tid = destproc->p_lasttid;
657 do {
658 if (++lp->lwp_tid < 0)
659 lp->lwp_tid = 1;
660 } while (lwp_rb_tree_RB_INSERT(&destproc->p_lwp_tree, lp) != NULL);
661 destproc->p_lasttid = lp->lwp_tid;
662 destproc->p_nthreads++;
663
664
13d13d89
SS
665 return (lp);
666}
667
984263bc
MD
668/*
669 * The next two functionms are general routines to handle adding/deleting
670 * items on the fork callout list.
671 *
672 * at_fork():
673 * Take the arguments given and put them onto the fork callout list,
674 * However first make sure that it's not already there.
675 * Returns 0 on success or a standard error number.
676 */
984263bc 677int
303c76d5 678at_fork(forklist_fn function)
984263bc
MD
679{
680 struct forklist *ep;
681
682#ifdef INVARIANTS
683 /* let the programmer know if he's been stupid */
303c76d5 684 if (rm_at_fork(function)) {
6ea70f76 685 kprintf("WARNING: fork callout entry (%p) already present\n",
984263bc 686 function);
303c76d5 687 }
984263bc 688#endif
efda3bd0 689 ep = kmalloc(sizeof(*ep), M_ATFORK, M_WAITOK|M_ZERO);
984263bc
MD
690 ep->function = function;
691 TAILQ_INSERT_TAIL(&fork_list, ep, next);
692 return (0);
693}
694
695/*
696 * Scan the exit callout list for the given item and remove it..
697 * Returns the number of items removed (0 or 1)
698 */
984263bc 699int
303c76d5 700rm_at_fork(forklist_fn function)
984263bc
MD
701{
702 struct forklist *ep;
703
704 TAILQ_FOREACH(ep, &fork_list, next) {
705 if (ep->function == function) {
706 TAILQ_REMOVE(&fork_list, ep, next);
efda3bd0 707 kfree(ep, M_ATFORK);
984263bc
MD
708 return(1);
709 }
710 }
711 return (0);
712}
7d0bac62
MD
713
714/*
715 * Add a forked process to the run queue after any remaining setup, such
716 * as setting the fork handler, has been completed.
717 */
7d0bac62 718void
553ea3c8 719start_forked_proc(struct lwp *lp1, struct proc *p2)
7d0bac62 720{
08f2f1bb 721 struct lwp *lp2 = ONLY_LWP_IN_PROC(p2);
553ea3c8 722
7d0bac62 723 /*
26a0694b
MD
724 * Move from SIDL to RUN queue, and activate the process's thread.
725 * Activation of the thread effectively makes the process "a"
726 * current process, so we do not setrunqueue().
8ec60c3f
MD
727 *
728 * YYY setrunqueue works here but we should clean up the trampoline
729 * code so we just schedule the LWKT thread and let the trampoline
730 * deal with the userland scheduler on return to userland.
7d0bac62 731 */
553ea3c8 732 KASSERT(p2->p_stat == SIDL,
7d0bac62 733 ("cannot start forked process, bad status: %p", p2));
553ea3c8 734 p2->p_usched->resetpriority(lp2);
e43a034f 735 crit_enter();
164b8401
SS
736 p2->p_stat = SACTIVE;
737 lp2->lwp_stat = LSRUN;
553ea3c8 738 p2->p_usched->setrunqueue(lp2);
e43a034f 739 crit_exit();
7d0bac62
MD
740
741 /*
742 * Now can be swapped.
743 */
553ea3c8 744 PRELE(lp1->lwp_proc);
7d0bac62
MD
745
746 /*
747 * Preserve synchronization semantics of vfork. If waiting for
748 * child to exec or exit, set P_PPWAIT on child, and sleep on our
ee934fe9
MD
749 * proc (in case of exec or exit).
750 *
751 * We must hold our p_token to interlock the flag/tsleep
7d0bac62 752 */
ee934fe9 753 lwkt_gettoken(&p2->p_token);
7d0bac62 754 while (p2->p_flag & P_PPWAIT)
553ea3c8 755 tsleep(lp1->lwp_proc, 0, "ppwait", 0);
ee934fe9 756 lwkt_reltoken(&p2->p_token);
7d0bac62 757}