Home | History | Annotate | Line # | Download | only in kern
sys_lwp.c revision 1.22
      1 /*	$NetBSD: sys_lwp.c,v 1.22 2007/08/01 23:24:26 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001, 2006, 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Nathan J. Williams, and Andrew Doran.
      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 NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Lightweight process (LWP) system calls.  See kern_lwp.c for a description
     41  * of LWPs.
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: sys_lwp.c,v 1.22 2007/08/01 23:24:26 ad Exp $");
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/pool.h>
     50 #include <sys/proc.h>
     51 #include <sys/types.h>
     52 #include <sys/syscallargs.h>
     53 #include <sys/kauth.h>
     54 #include <sys/kmem.h>
     55 #include <sys/sleepq.h>
     56 
     57 #include <uvm/uvm_extern.h>
     58 
     59 #define	LWP_UNPARK_MAX		1024
     60 
     61 syncobj_t lwp_park_sobj = {
     62 	SOBJ_SLEEPQ_FIFO,
     63 	sleepq_unsleep,
     64 	sleepq_changepri,
     65 	sleepq_lendpri,
     66 	syncobj_noowner,
     67 };
     68 
     69 sleeptab_t	lwp_park_tab;
     70 
     71 void
     72 lwp_sys_init(void)
     73 {
     74 	sleeptab_init(&lwp_park_tab);
     75 }
     76 
     77 /* ARGSUSED */
     78 int
     79 sys__lwp_create(struct lwp *l, void *v, register_t *retval)
     80 {
     81 	struct sys__lwp_create_args /* {
     82 		syscallarg(const ucontext_t *) ucp;
     83 		syscallarg(u_long) flags;
     84 		syscallarg(lwpid_t *) new_lwp;
     85 	} */ *uap = v;
     86 	struct proc *p = l->l_proc;
     87 	struct lwp *l2;
     88 	vaddr_t uaddr;
     89 	bool inmem;
     90 	ucontext_t *newuc;
     91 	int error, lid;
     92 
     93 	newuc = pool_get(&lwp_uc_pool, PR_WAITOK);
     94 
     95 	error = copyin(SCARG(uap, ucp), newuc, p->p_emul->e_ucsize);
     96 	if (error) {
     97 		pool_put(&lwp_uc_pool, newuc);
     98 		return error;
     99 	}
    100 
    101 	/* XXX check against resource limits */
    102 
    103 	inmem = uvm_uarea_alloc(&uaddr);
    104 	if (__predict_false(uaddr == 0)) {
    105 		pool_put(&lwp_uc_pool, newuc);
    106 		return ENOMEM;
    107 	}
    108 
    109 	error = newlwp(l, p, uaddr, inmem,
    110 	    SCARG(uap, flags) & LWP_DETACHED,
    111 	    NULL, 0, p->p_emul->e_startlwp, newuc, &l2);
    112 	if (error) {
    113 		uvm_uarea_free(uaddr);
    114 		pool_put(&lwp_uc_pool, newuc);
    115 		return error;
    116 	}
    117 
    118 	lid = l2->l_lid;
    119 	error = copyout(&lid, SCARG(uap, new_lwp), sizeof(lid));
    120 	if (error) {
    121 		lwp_exit(l2);
    122 		pool_put(&lwp_uc_pool, newuc);
    123 		return error;
    124 	}
    125 
    126 	/*
    127 	 * Set the new LWP running, unless the caller has requested that
    128 	 * it be created in suspended state.  If the process is stopping,
    129 	 * then the LWP is created stopped.
    130 	 */
    131 	mutex_enter(&p->p_smutex);
    132 	lwp_lock(l2);
    133 	if ((SCARG(uap, flags) & LWP_SUSPENDED) == 0 &&
    134 	    (l->l_flag & (LW_WREBOOT | LW_WSUSPEND | LW_WEXIT)) == 0) {
    135 	    	if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0)
    136 	    		l2->l_stat = LSSTOP;
    137 		else {
    138 			KASSERT(lwp_locked(l2, l2->l_cpu->ci_schedstate.spc_mutex));
    139 			p->p_nrlwps++;
    140 			l2->l_stat = LSRUN;
    141 			sched_enqueue(l2, false);
    142 		}
    143 	} else
    144 		l2->l_stat = LSSUSPENDED;
    145 	lwp_unlock(l2);
    146 	mutex_exit(&p->p_smutex);
    147 
    148 	return 0;
    149 }
    150 
    151 int
    152 sys__lwp_exit(struct lwp *l, void *v, register_t *retval)
    153 {
    154 
    155 	lwp_exit(l);
    156 	return 0;
    157 }
    158 
    159 int
    160 sys__lwp_self(struct lwp *l, void *v, register_t *retval)
    161 {
    162 
    163 	*retval = l->l_lid;
    164 	return 0;
    165 }
    166 
    167 int
    168 sys__lwp_getprivate(struct lwp *l, void *v, register_t *retval)
    169 {
    170 
    171 	*retval = (uintptr_t)l->l_private;
    172 	return 0;
    173 }
    174 
    175 int
    176 sys__lwp_setprivate(struct lwp *l, void *v, register_t *retval)
    177 {
    178 	struct sys__lwp_setprivate_args /* {
    179 		syscallarg(void *) ptr;
    180 	} */ *uap = v;
    181 
    182 	l->l_private = SCARG(uap, ptr);
    183 	return 0;
    184 }
    185 
    186 int
    187 sys__lwp_suspend(struct lwp *l, void *v, register_t *retval)
    188 {
    189 	struct sys__lwp_suspend_args /* {
    190 		syscallarg(lwpid_t) target;
    191 	} */ *uap = v;
    192 	struct proc *p = l->l_proc;
    193 	struct lwp *t;
    194 	int error;
    195 
    196 	mutex_enter(&p->p_smutex);
    197 	if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
    198 		mutex_exit(&p->p_smutex);
    199 		return ESRCH;
    200 	}
    201 
    202 	/*
    203 	 * Check for deadlock, which is only possible when we're suspending
    204 	 * ourself.  XXX There is a short race here, as p_nrlwps is only
    205 	 * incremented when an LWP suspends itself on the kernel/user
    206 	 * boundary.  It's still possible to kill -9 the process so we
    207 	 * don't bother checking further.
    208 	 */
    209 	lwp_lock(t);
    210 	if ((t == l && p->p_nrlwps == 1) ||
    211 	    (l->l_flag & (LW_WCORE | LW_WEXIT)) != 0) {
    212 		lwp_unlock(t);
    213 		mutex_exit(&p->p_smutex);
    214 		return EDEADLK;
    215 	}
    216 
    217 	/*
    218 	 * Suspend the LWP.  XXX If it's on a different CPU, we should wait
    219 	 * for it to be preempted, where it will put itself to sleep.
    220 	 *
    221 	 * Suspension of the current LWP will happen on return to userspace.
    222 	 */
    223 	error = lwp_suspend(l, t);
    224 	mutex_exit(&p->p_smutex);
    225 
    226 	return error;
    227 }
    228 
    229 int
    230 sys__lwp_continue(struct lwp *l, void *v, register_t *retval)
    231 {
    232 	struct sys__lwp_continue_args /* {
    233 		syscallarg(lwpid_t) target;
    234 	} */ *uap = v;
    235 	int error;
    236 	struct proc *p = l->l_proc;
    237 	struct lwp *t;
    238 
    239 	error = 0;
    240 
    241 	mutex_enter(&p->p_smutex);
    242 	if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
    243 		mutex_exit(&p->p_smutex);
    244 		return ESRCH;
    245 	}
    246 
    247 	lwp_lock(t);
    248 	lwp_continue(t);
    249 	mutex_exit(&p->p_smutex);
    250 
    251 	return error;
    252 }
    253 
    254 int
    255 sys__lwp_wakeup(struct lwp *l, void *v, register_t *retval)
    256 {
    257 	struct sys__lwp_wakeup_args /* {
    258 		syscallarg(lwpid_t) target;
    259 	} */ *uap = v;
    260 	struct lwp *t;
    261 	struct proc *p;
    262 	int error;
    263 
    264 	p = l->l_proc;
    265 	mutex_enter(&p->p_smutex);
    266 
    267 	if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
    268 		mutex_exit(&p->p_smutex);
    269 		return ESRCH;
    270 	}
    271 
    272 	lwp_lock(t);
    273 	t->l_flag |= (LW_CANCELLED | LW_UNPARKED);
    274 
    275 	if (t->l_stat != LSSLEEP) {
    276 		lwp_unlock(t);
    277 		error = ENODEV;
    278 	} else if ((t->l_flag & LW_SINTR) == 0) {
    279 		lwp_unlock(t);
    280 		error = EBUSY;
    281 	} else {
    282 		/* Wake it up.  lwp_unsleep() will release the LWP lock. */
    283 		lwp_unsleep(t);
    284 		error = 0;
    285 	}
    286 
    287 	mutex_exit(&p->p_smutex);
    288 
    289 	return error;
    290 }
    291 
    292 int
    293 sys__lwp_wait(struct lwp *l, void *v, register_t *retval)
    294 {
    295 	struct sys__lwp_wait_args /* {
    296 		syscallarg(lwpid_t) wait_for;
    297 		syscallarg(lwpid_t *) departed;
    298 	} */ *uap = v;
    299 	struct proc *p = l->l_proc;
    300 	int error;
    301 	lwpid_t dep;
    302 
    303 	mutex_enter(&p->p_smutex);
    304 	error = lwp_wait1(l, SCARG(uap, wait_for), &dep, 0);
    305 	mutex_exit(&p->p_smutex);
    306 
    307 	if (error)
    308 		return error;
    309 
    310 	if (SCARG(uap, departed)) {
    311 		error = copyout(&dep, SCARG(uap, departed), sizeof(dep));
    312 		if (error)
    313 			return error;
    314 	}
    315 
    316 	return 0;
    317 }
    318 
    319 /* ARGSUSED */
    320 int
    321 sys__lwp_kill(struct lwp *l, void *v, register_t *retval)
    322 {
    323 	struct sys__lwp_kill_args /* {
    324 		syscallarg(lwpid_t)	target;
    325 		syscallarg(int)		signo;
    326 	} */ *uap = v;
    327 	struct proc *p = l->l_proc;
    328 	struct lwp *t;
    329 	ksiginfo_t ksi;
    330 	int signo = SCARG(uap, signo);
    331 	int error = 0;
    332 
    333 	if ((u_int)signo >= NSIG)
    334 		return EINVAL;
    335 
    336 	KSI_INIT(&ksi);
    337 	ksi.ksi_signo = signo;
    338 	ksi.ksi_code = SI_USER;
    339 	ksi.ksi_pid = p->p_pid;
    340 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
    341 	ksi.ksi_lid = SCARG(uap, target);
    342 
    343 	mutex_enter(&proclist_mutex);
    344 	mutex_enter(&p->p_smutex);
    345 	if ((t = lwp_find(p, ksi.ksi_lid)) == NULL)
    346 		error = ESRCH;
    347 	else if (signo != 0)
    348 		kpsignal2(p, &ksi);
    349 	mutex_exit(&p->p_smutex);
    350 	mutex_exit(&proclist_mutex);
    351 
    352 	return error;
    353 }
    354 
    355 int
    356 sys__lwp_detach(struct lwp *l, void *v, register_t *retval)
    357 {
    358 	struct sys__lwp_detach_args /* {
    359 		syscallarg(lwpid_t)	target;
    360 	} */ *uap = v;
    361 	struct proc *p;
    362 	struct lwp *t;
    363 	lwpid_t target;
    364 	int error;
    365 
    366 	target = SCARG(uap, target);
    367 	p = l->l_proc;
    368 
    369 	mutex_enter(&p->p_smutex);
    370 
    371 	if (l->l_lid == target)
    372 		t = l;
    373 	else {
    374 		/*
    375 		 * We can't use lwp_find() here because the target might
    376 		 * be a zombie.
    377 		 */
    378 		LIST_FOREACH(t, &p->p_lwps, l_sibling)
    379 			if (t->l_lid == target)
    380 				break;
    381 	}
    382 
    383 	/*
    384 	 * If the LWP is already detached, there's nothing to do.
    385 	 * If it's a zombie, we need to clean up after it.  LSZOMB
    386 	 * is visible with the proc mutex held.
    387 	 *
    388 	 * After we have detached or released the LWP, kick any
    389 	 * other LWPs that may be sitting in _lwp_wait(), waiting
    390 	 * for the target LWP to exit.
    391 	 */
    392 	if (t != NULL && t->l_stat != LSIDL) {
    393 		if ((t->l_prflag & LPR_DETACHED) == 0) {
    394 			p->p_ndlwps++;
    395 			t->l_prflag |= LPR_DETACHED;
    396 			if (t->l_stat == LSZOMB) {
    397 				/* Releases proc mutex. */
    398 				lwp_free(t, false, false);
    399 				return 0;
    400 			}
    401 			error = 0;
    402 
    403 			/*
    404 			 * Have any LWPs sleeping in lwp_wait() recheck
    405 			 * for deadlock.
    406 			 */
    407 			cv_broadcast(&p->p_lwpcv);
    408 		} else
    409 			error = EINVAL;
    410 	} else
    411 		error = ESRCH;
    412 
    413 	mutex_exit(&p->p_smutex);
    414 
    415 	return error;
    416 }
    417 
    418 static inline wchan_t
    419 lwp_park_wchan(struct proc *p, const void *hint)
    420 {
    421 
    422 	return (wchan_t)((uintptr_t)p ^ (uintptr_t)hint);
    423 }
    424 
    425 /*
    426  * 'park' an LWP waiting on a user-level synchronisation object.  The LWP
    427  * will remain parked until another LWP in the same process calls in and
    428  * requests that it be unparked.
    429  */
    430 int
    431 sys__lwp_park(struct lwp *l, void *v, register_t *retval)
    432 {
    433 	struct sys__lwp_park_args /* {
    434 		syscallarg(const struct timespec *)	ts;
    435 		syscallarg(ucontext_t *)		ucp;
    436 		syscallarg(const void *)		hint;
    437 	} */ *uap = v;
    438 	struct timespec ts;
    439 	int error;
    440 
    441 	if (SCARG(uap, ts) == NULL)
    442 		return do_sys_lwp_park(l, NULL, SCARG(uap, ucp),
    443 		    SCARG(uap, hint));
    444 
    445 	if ((error = copyin(SCARG(uap, ts), &ts, sizeof(ts))) != 0)
    446 		return error;
    447 
    448 	return do_sys_lwp_park(l, &ts, SCARG(uap, ucp), SCARG(uap, hint));
    449 }
    450 
    451 int
    452 do_sys_lwp_park(struct lwp *l, struct timespec *ts, ucontext_t *uc,
    453 		const void *hint)
    454 {
    455 	struct timespec tsx;
    456 	struct timeval tv;
    457 	sleepq_t *sq;
    458 	wchan_t wchan;
    459 	int timo, error;
    460 
    461 	/* Fix up the given timeout value. */
    462 	if (ts != NULL) {
    463 		getnanotime(&tsx);
    464 		timespecsub(ts, &tsx, ts);
    465 		tv.tv_sec = ts->tv_sec;
    466 		tv.tv_usec = ts->tv_nsec / 1000;
    467 		if (tv.tv_sec < 0 || (tv.tv_sec == 0 && tv.tv_usec < 0))
    468 			return ETIMEDOUT;
    469 		if ((error = itimerfix(&tv)) != 0)
    470 			return error;
    471 		timo = tvtohz(&tv);
    472 	} else
    473 		timo = 0;
    474 
    475 	/* Find and lock the sleep queue. */
    476 	wchan = lwp_park_wchan(l->l_proc, hint);
    477 	sq = sleeptab_lookup(&lwp_park_tab, wchan);
    478 
    479 	/*
    480 	 * Before going the full route and blocking, check to see if an
    481 	 * unpark op is pending.
    482 	 */
    483 	lwp_lock(l);
    484 	if ((l->l_flag & (LW_CANCELLED | LW_UNPARKED)) != 0) {
    485 		l->l_flag &= ~(LW_CANCELLED | LW_UNPARKED);
    486 		lwp_unlock(l);
    487 		sleepq_unlock(sq);
    488 		return EALREADY;
    489 	}
    490 	lwp_unlock_to(l, sq->sq_mutex);
    491 
    492 	KERNEL_UNLOCK_ALL(l, &l->l_biglocks); /* XXX for compat32 */
    493 	sleepq_enqueue(sq, sched_kpri(l), wchan, "parked", &lwp_park_sobj);
    494 	error = sleepq_block(timo, true);
    495 	switch (error) {
    496 	case EWOULDBLOCK:
    497 		error = ETIMEDOUT;
    498 		break;
    499 	case ERESTART:
    500 		error = EINTR;
    501 		break;
    502 	default:
    503 		/* nothing */
    504 		break;
    505 	}
    506 	return error;
    507 }
    508 
    509 int
    510 sys__lwp_unpark(struct lwp *l, void *v, register_t *retval)
    511 {
    512 	struct sys__lwp_unpark_args /* {
    513 		syscallarg(lwpid_t)		target;
    514 		syscallarg(const void *)	hint;
    515 	} */ *uap = v;
    516 	struct proc *p;
    517 	struct lwp *t;
    518 	sleepq_t *sq;
    519 	lwpid_t target;
    520 	wchan_t wchan;
    521 	int swapin;
    522 
    523 	p = l->l_proc;
    524 	target = SCARG(uap, target);
    525 
    526 	/*
    527 	 * Easy case: search for the LWP on the sleep queue.  If
    528 	 * it's parked, remove it from the queue and set running.
    529 	 */
    530 	wchan = lwp_park_wchan(p, SCARG(uap, hint));
    531 	sq = sleeptab_lookup(&lwp_park_tab, wchan);
    532 
    533 	TAILQ_FOREACH(t, &sq->sq_queue, l_sleepchain)
    534 		if (t->l_proc == p && t->l_lid == target)
    535 			break;
    536 
    537 	if (__predict_true(t != NULL)) {
    538 		swapin = sleepq_remove(sq, t);
    539 		sleepq_unlock(sq);
    540 		if (swapin)
    541 			uvm_kick_scheduler();
    542 		return 0;
    543 	}
    544 
    545 	/*
    546 	 * The LWP hasn't parked yet.  Take the hit and mark the
    547 	 * operation as pending.
    548 	 */
    549 	sleepq_unlock(sq);
    550 	mutex_enter(&p->p_smutex);
    551 	if ((t = lwp_find(p, target)) == NULL) {
    552 		mutex_exit(&p->p_smutex);
    553 		return ESRCH;
    554 	}
    555 	lwp_lock(t);
    556 
    557 	/*
    558 	 * It may not have parked yet, we may have raced, or it
    559 	 * is parked on a different user sync object.
    560 	 */
    561 	if (t->l_syncobj == &lwp_park_sobj) {
    562 		/* Releases the LWP lock. */
    563 		lwp_unsleep(t);
    564 	} else {
    565 		/*
    566 		 * Set the operation pending.  The next call to _lwp_park
    567 		 * will return early.
    568 		 */
    569 		t->l_flag |= LW_UNPARKED;
    570 		lwp_unlock(t);
    571 	}
    572 
    573 	mutex_exit(&p->p_smutex);
    574 	return 0;
    575 }
    576 
    577 int
    578 sys__lwp_unpark_all(struct lwp *l, void *v, register_t *retval)
    579 {
    580 	struct sys__lwp_unpark_all_args /* {
    581 		syscallarg(const lwpid_t *)	targets;
    582 		syscallarg(size_t)		ntargets;
    583 		syscallarg(const void *)	hint;
    584 	} */ *uap = v;
    585 	struct proc *p;
    586 	struct lwp *t;
    587 	sleepq_t *sq;
    588 	wchan_t wchan;
    589 	lwpid_t targets[32], *tp, *tpp, *tmax, target;
    590 	int swapin, error;
    591 	u_int ntargets;
    592 	size_t sz;
    593 
    594 	p = l->l_proc;
    595 	ntargets = SCARG(uap, ntargets);
    596 
    597 	if (SCARG(uap, targets) == NULL) {
    598 		/*
    599 		 * Let the caller know how much we are willing to do, and
    600 		 * let it unpark the LWPs in blocks.
    601 		 */
    602 		*retval = LWP_UNPARK_MAX;
    603 		return 0;
    604 	}
    605 	if (ntargets > LWP_UNPARK_MAX || ntargets == 0)
    606 		return EINVAL;
    607 
    608 	/*
    609 	 * Copy in the target array.  If it's a small number of LWPs, then
    610 	 * place the numbers on the stack.
    611 	 */
    612 	sz = sizeof(target) * ntargets;
    613 	if (sz <= sizeof(targets))
    614 		tp = targets;
    615 	else {
    616 		KERNEL_LOCK(1, l);		/* XXXSMP */
    617 		tp = kmem_alloc(sz, KM_SLEEP);
    618 		KERNEL_UNLOCK_ONE(l);		/* XXXSMP */
    619 		if (tp == NULL)
    620 			return ENOMEM;
    621 	}
    622 	error = copyin(SCARG(uap, targets), tp, sz);
    623 	if (error != 0) {
    624 		if (tp != targets) {
    625 			KERNEL_LOCK(1, l);	/* XXXSMP */
    626 			kmem_free(tp, sz);
    627 			KERNEL_UNLOCK_ONE(l);	/* XXXSMP */
    628 		}
    629 		return error;
    630 	}
    631 
    632 	swapin = 0;
    633 	wchan = lwp_park_wchan(p, SCARG(uap, hint));
    634 	sq = sleeptab_lookup(&lwp_park_tab, wchan);
    635 
    636 	for (tmax = tp + ntargets, tpp = tp; tpp < tmax; tpp++) {
    637 		target = *tpp;
    638 
    639 		/*
    640 		 * Easy case: search for the LWP on the sleep queue.  If
    641 		 * it's parked, remove it from the queue and set running.
    642 		 */
    643 		TAILQ_FOREACH(t, &sq->sq_queue, l_sleepchain)
    644 			if (t->l_proc == p && t->l_lid == target)
    645 				break;
    646 
    647 		if (t != NULL) {
    648 			swapin |= sleepq_remove(sq, t);
    649 			continue;
    650 		}
    651 
    652 		/*
    653 		 * The LWP hasn't parked yet.  Take the hit and
    654 		 * mark the operation as pending.
    655 		 */
    656 		sleepq_unlock(sq);
    657 		mutex_enter(&p->p_smutex);
    658 		if ((t = lwp_find(p, target)) == NULL) {
    659 			mutex_exit(&p->p_smutex);
    660 			sleepq_lock(sq);
    661 			continue;
    662 		}
    663 		lwp_lock(t);
    664 
    665 		/*
    666 		 * It may not have parked yet, we may have raced, or
    667 		 * it is parked on a different user sync object.
    668 		 */
    669 		if (t->l_syncobj == &lwp_park_sobj) {
    670 			/* Releases the LWP lock. */
    671 			lwp_unsleep(t);
    672 		} else {
    673 			/*
    674 			 * Set the operation pending.  The next call to
    675 			 * _lwp_park will return early.
    676 			 */
    677 			t->l_flag |= LW_UNPARKED;
    678 			lwp_unlock(t);
    679 		}
    680 
    681 		mutex_exit(&p->p_smutex);
    682 		sleepq_lock(sq);
    683 	}
    684 
    685 	sleepq_unlock(sq);
    686 	if (tp != targets) {
    687 		KERNEL_LOCK(1, l);		/* XXXSMP */
    688 		kmem_free(tp, sz);
    689 		KERNEL_UNLOCK_ONE(l);		/* XXXSMP */
    690 	}
    691 	if (swapin)
    692 		uvm_kick_scheduler();
    693 
    694 	return 0;
    695 }
    696