sys/kern/kern_plimit.c
void
plimit_modify(struct proc *p, int index, struct rlimit *rlim)
{
	struct plimit *olimit;
	struct plimit *nlimit;
	struct plimit *rlimit;

	/*
	 * Shortcut.  If we are not threaded we may be able to trivially
	 * set the structure to exclusive access without needing to acquire
	 * any spinlocks.   The p_limit structure will be stable.
	 */
	olimit = p->p_limit;
	if (p->p_nthreads == 1) {
		if (olimit->p_exclusive == 0 && olimit->p_refcnt == 1)
			olimit->p_exclusive = 1;
		if (olimit->p_exclusive) {
			if (index >= 0)
				p->p_limit->pl_rlimit[index] = *rlim;
			return;
		}
	}

	/*
	 * Full-blown code-up.  Make a copy if we aren't exclusive.  If
	 * we have only one ref we can safely convert the structure to
	 * exclusive without copying.
	 */
	nlimit = NULL;
	spin_lock(&olimit->p_spin);

	for (;;) {
		if (olimit->p_refcnt == 1) {
			if (olimit->p_exclusive == 0)
				olimit->p_exclusive = 1;
			rlimit = olimit;
			break;
		}
		KKASSERT(olimit->p_exclusive == 0);
		if (nlimit) {
			plimit_copy(olimit, nlimit);
			nlimit->p_exclusive = 1;
			p->p_limit = nlimit;
			rlimit = nlimit;
			nlimit = NULL;
			break;
		}
		spin_unlock(&olimit->p_spin);
		nlimit = kmalloc(sizeof(*nlimit), M_SUBPROC, M_WAITOK);
		spin_lock(&olimit->p_spin);
	}
	if (index >= 0)
		rlimit->pl_rlimit[index] = *rlim;
	spin_unlock(&olimit->p_spin);
	if (nlimit)
		kfree(nlimit, M_SUBPROC);
}
/*
 * This routine is called to fixup a proces's p_limit structure prior
 * to it being modified.  If index >= 0 the specified modification is also
 * made.
 *
 * This routine must make the limit structure exclusive.  A later fork
 * will convert it back to copy-on-write if possible.
 *
 * We can count on p->p_limit being stable since if we had created any
 * threads it will have already been made exclusive (=2).
 *
 * MPSAFE
 */
This routine is called to fixup a proces's p_limit structure prior
to it being modified.  If index >= 0 the specified modification is also
made.

This routine must make the limit structure exclusive.  A later fork
will convert it back to copy-on-write if possible.

We can count on p->p_limit being stable since if we had created any
threads it will have already been made exclusive (=2).

MPSAFE
This routine is called to fixup a proces ' s p_limit structure prior to it being modified . If index > = 0 the specified modification is also made . This routine must make the limit structure exclusive . A later fork will convert it back to copy - on - write if possible . We can count on p - > p_limit being stable since if we had created any threads it will have already been made exclusive ( = 2 ) . MPSAFE
void
plimit_modify(struct proc *p, int index, struct rlimit *rlim)
{
	struct plimit *olimit;
	struct plimit *nlimit;
	struct plimit *rlimit;
	olimit = p->p_limit;
	if (p->p_nthreads == 1) {
		if (olimit->p_exclusive == 0 && olimit->p_refcnt == 1)
			olimit->p_exclusive = 1;
		if (olimit->p_exclusive) {
			if (index >= 0)
				p->p_limit->pl_rlimit[index] = *rlim;
			return;
		}
	}
	nlimit = NULL;
	spin_lock(&olimit->p_spin);
	for (;;) {
		if (olimit->p_refcnt == 1) {
			if (olimit->p_exclusive == 0)
				olimit->p_exclusive = 1;
			rlimit = olimit;
			break;
		}
		KKASSERT(olimit->p_exclusive == 0);
		if (nlimit) {
			plimit_copy(olimit, nlimit);
			nlimit->p_exclusive = 1;
			p->p_limit = nlimit;
			rlimit = nlimit;
			nlimit = NULL;
			break;
		}
		spin_unlock(&olimit->p_spin);
		nlimit = kmalloc(sizeof(*nlimit), M_SUBPROC, M_WAITOK);
		spin_lock(&olimit->p_spin);
	}
	if (index >= 0)
		rlimit->pl_rlimit[index] = *rlim;
	spin_unlock(&olimit->p_spin);
	if (nlimit)
		kfree(nlimit, M_SUBPROC);
}
void plimit_modify ( struct proc * p , int index , struct rlimit * rlim ) { struct plimit * olimit ; struct plimit * nlimit ; struct plimit * rlimit ; olimit = p -> p_limit ; if ( p -> p_nthreads == 1 ) { if ( olimit -> p_exclusive == 0 && olimit -> p_refcnt == 1 ) olimit -> p_exclusive = 1 ; if ( olimit -> p_exclusive ) { if ( index >= 0 ) p -> p_limit -> pl_rlimit [ index ] = * rlim ; return ; } } nlimit = NULL ; spin_lock ( & olimit -> p_spin ) ; for ( ; ; ) { if ( olimit -> p_refcnt == 1 ) { if ( olimit -> p_exclusive == 0 ) olimit -> p_exclusive = 1 ; rlimit = olimit ; break ; } KKASSERT ( olimit -> p_exclusive == 0 ) ; if ( nlimit ) { plimit_copy ( olimit , nlimit ) ; nlimit -> p_exclusive = 1 ; p -> p_limit = nlimit ; rlimit = nlimit ; nlimit = NULL ; break ; } spin_unlock ( & olimit -> p_spin ) ; nlimit = kmalloc ( sizeof ( * nlimit ) , M_SUBPROC , M_WAITOK ) ; spin_lock ( & olimit -> p_spin ) ; } if ( index >= 0 ) rlimit -> pl_rlimit [ index ] = * rlim ; spin_unlock ( & olimit -> p_spin ) ; if ( nlimit ) kfree ( nlimit , M_SUBPROC ) ; }
This routine is called to fixup a proces's p_limit structure prior
 to it being modified.
This routine is called to fixup a proces ' s p_limit structure prior to it being modified .
/*
	 * Shortcut.  If we are not threaded we may be able to trivially
	 * set the structure to exclusive access without needing to acquire
	 * any spinlocks.   The p_limit structure will be stable.
	 */ /*
	 * Full-blown code-up.  Make a copy if we aren't exclusive.  If
	 * we have only one ref we can safely convert the structure to
	 * exclusive without copying.
	 */