Home | History | Annotate | Line # | Download | only in kern
sys_lwp.c revision 1.17
      1 /*	$NetBSD: sys_lwp.c,v 1.17 2007/03/21 18:26:00 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.17 2007/03/21 18:26:00 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 	newlwp(l, p, uaddr, inmem,
    110 	    SCARG(uap, flags) & LWP_DETACHED,
    111 	    NULL, 0, p->p_emul->e_startlwp, newuc, &l2);
    112 
    113 	/*
    114 	 * Set the new LWP running, unless the caller has requested that
    115 	 * it be created in suspended state.  If the process is stopping,
    116 	 * then the LWP is created stopped.
    117 	 */
    118 	mutex_enter(&p->p_smutex);
    119 	lwp_lock(l2);
    120 	lid = l2->l_lid;
    121 	if ((SCARG(uap, flags) & LWP_SUSPENDED) == 0 &&
    122 	    (l->l_flag & (LW_WREBOOT | LW_WSUSPEND | LW_WEXIT)) == 0) {
    123 	    	if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0)
    124 	    		l2->l_stat = LSSTOP;
    125 		else {
    126 			KASSERT(lwp_locked(l2, &sched_mutex));
    127 			p->p_nrlwps++;
    128 			l2->l_stat = LSRUN;
    129 			setrunqueue(l2);
    130 		}
    131 	} else
    132 		l2->l_stat = LSSUSPENDED;
    133 	lwp_unlock(l2);
    134 	mutex_exit(&p->p_smutex);
    135 
    136 	error = copyout(&lid, SCARG(uap, new_lwp), sizeof(lid));
    137 	if (error)
    138 		return error;
    139 
    140 	return 0;
    141 }
    142 
    143 int
    144 sys__lwp_exit(struct lwp *l, void *v, register_t *retval)
    145 {
    146 
    147 	lwp_exit(l);
    148 	return 0;
    149 }
    150 
    151 int
    152 sys__lwp_self(struct lwp *l, void *v, register_t *retval)
    153 {
    154 
    155 	*retval = l->l_lid;
    156 	return 0;
    157 }
    158 
    159 int
    160 sys__lwp_getprivate(struct lwp *l, void *v, register_t *retval)
    161 {
    162 
    163 	*retval = (uintptr_t)l->l_private;
    164 	return 0;
    165 }
    166 
    167 int
    168 sys__lwp_setprivate(struct lwp *l, void *v, register_t *retval)
    169 {
    170 	struct sys__lwp_setprivate_args /* {
    171 		syscallarg(void *) ptr;
    172 	} */ *uap = v;
    173 
    174 	l->l_private = SCARG(uap, ptr);
    175 	return 0;
    176 }
    177 
    178 int
    179 sys__lwp_suspend(struct lwp *l, void *v, register_t *retval)
    180 {
    181 	struct sys__lwp_suspend_args /* {
    182 		syscallarg(lwpid_t) target;
    183 	} */ *uap = v;
    184 	struct proc *p = l->l_proc;
    185 	struct lwp *t;
    186 	int error;
    187 
    188 	mutex_enter(&p->p_smutex);
    189 	if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
    190 		mutex_exit(&p->p_smutex);
    191 		return ESRCH;
    192 	}
    193 
    194 	/*
    195 	 * Check for deadlock, which is only possible when we're suspending
    196 	 * ourself.  XXX There is a short race here, as p_nrlwps is only
    197 	 * incremented when an LWP suspends itself on the kernel/user
    198 	 * boundary.  It's still possible to kill -9 the process so we
    199 	 * don't bother checking further.
    200 	 */
    201 	lwp_lock(t);
    202 	if ((t == l && p->p_nrlwps == 1) ||
    203 	    (l->l_flag & (LW_WCORE | LW_WEXIT)) != 0) {
    204 		lwp_unlock(t);
    205 		mutex_exit(&p->p_smutex);
    206 		return EDEADLK;
    207 	}
    208 
    209 	/*
    210 	 * Suspend the LWP.  XXX If it's on a different CPU, we should wait
    211 	 * for it to be preempted, where it will put itself to sleep.
    212 	 *
    213 	 * Suspension of the current LWP will happen on return to userspace.
    214 	 */
    215 	error = lwp_suspend(l, t);
    216 	mutex_exit(&p->p_smutex);
    217 
    218 	return error;
    219 }
    220 
    221 int
    222 sys__lwp_continue(struct lwp *l, void *v, register_t *retval)
    223 {
    224 	struct sys__lwp_continue_args /* {
    225 		syscallarg(lwpid_t) target;
    226 	} */ *uap = v;
    227 	int error;
    228 	struct proc *p = l->l_proc;
    229 	struct lwp *t;
    230 
    231 	error = 0;
    232 
    233 	mutex_enter(&p->p_smutex);
    234 	if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
    235 		mutex_exit(&p->p_smutex);
    236 		return ESRCH;
    237 	}
    238 
    239 	lwp_lock(t);
    240 	lwp_continue(t);
    241 	mutex_exit(&p->p_smutex);
    242 
    243 	return error;
    244 }
    245 
    246 int
    247 sys__lwp_wakeup(struct lwp *l, void *v, register_t *retval)
    248 {
    249 	struct sys__lwp_wakeup_args /* {
    250 		syscallarg(lwpid_t) target;
    251 	} */ *uap = v;
    252 	struct lwp *t;
    253 	struct proc *p;
    254 	int error;
    255 
    256 	p = l->l_proc;
    257 	mutex_enter(&p->p_smutex);
    258 
    259 	if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
    260 		mutex_exit(&p->p_smutex);
    261 		return ESRCH;
    262 	}
    263 
    264 	lwp_lock(t);
    265 	t->l_flag |= (LW_CANCELLED | LW_UNPARKED);
    266 
    267 	if (t->l_stat != LSSLEEP) {
    268 		lwp_unlock(t);
    269 		error = ENODEV;
    270 	} else if ((t->l_flag & LW_SINTR) == 0) {
    271 		lwp_unlock(t);
    272 		error = EBUSY;
    273 	} else {
    274 		/* Wake it up.  lwp_unsleep() will release the LWP lock. */
    275 		lwp_unsleep(t);
    276 		error = 0;
    277 	}
    278 
    279 	mutex_exit(&p->p_smutex);
    280 
    281 	return error;
    282 }
    283 
    284 int
    285 sys__lwp_wait(struct lwp *l, void *v, register_t *retval)
    286 {
    287 	struct sys__lwp_wait_args /* {
    288 		syscallarg(lwpid_t) wait_for;
    289 		syscallarg(lwpid_t *) departed;
    290 	} */ *uap = v;
    291 	struct proc *p = l->l_proc;
    292 	int error;
    293 	lwpid_t dep;
    294 
    295 	mutex_enter(&p->p_smutex);
    296 	error = lwp_wait1(l, SCARG(uap, wait_for), &dep, 0);
    297 	mutex_exit(&p->p_smutex);
    298 
    299 	if (error)
    300 		return error;
    301 
    302 	if (SCARG(uap, departed)) {
    303 		error = copyout(&dep, SCARG(uap, departed), sizeof(dep));
    304 		if (error)
    305 			return error;
    306 	}
    307 
    308 	return 0;
    309 }
    310 
    311 /* ARGSUSED */
    312 int
    313 sys__lwp_kill(struct lwp *l, void *v, register_t *retval)
    314 {
    315 	struct sys__lwp_kill_args /* {
    316 		syscallarg(lwpid_t)	target;
    317 		syscallarg(int)		signo;
    318 	} */ *uap = v;
    319 	struct proc *p = l->l_proc;
    320 	struct lwp *t;
    321 	ksiginfo_t ksi;
    322 	int signo = SCARG(uap, signo);
    323 	int error = 0;
    324 
    325 	if ((u_int)signo >= NSIG)
    326 		return EINVAL;
    327 
    328 	KSI_INIT(&ksi);
    329 	ksi.ksi_signo = signo;
    330 	ksi.ksi_code = SI_USER;
    331 	ksi.ksi_pid = p->p_pid;
    332 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
    333 	ksi.ksi_lid = SCARG(uap, target);
    334 
    335 	mutex_enter(&proclist_mutex);
    336 	mutex_enter(&p->p_smutex);
    337 	if ((t = lwp_find(p, ksi.ksi_lid)) == NULL)
    338 		error = ESRCH;
    339 	else if (signo != 0)
    340 		kpsignal2(p, &ksi);
    341 	mutex_exit(&p->p_smutex);
    342 	mutex_exit(&proclist_mutex);
    343 
    344 	return error;
    345 }
    346 
    347 int
    348 sys__lwp_detach(struct lwp *l, void *v, register_t *retval)
    349 {
    350 	struct sys__lwp_detach_args /* {
    351 		syscallarg(lwpid_t)	target;
    352 	} */ *uap = v;
    353 	struct proc *p;
    354 	struct lwp *t;
    355 	lwpid_t target;
    356 	int error;
    357 
    358 	target = SCARG(uap, target);
    359 	p = l->l_proc;
    360 
    361 	mutex_enter(&p->p_smutex);
    362 
    363 	if (l->l_lid == target)
    364 		t = l;
    365 	else {
    366 		/*
    367 		 * We can't use lwp_find() here because the target might
    368 		 * be a zombie.
    369 		 */
    370 		LIST_FOREACH(t, &p->p_lwps, l_sibling)
    371 			if (t->l_lid == target)
    372 				break;
    373 	}
    374 
    375 	/*
    376 	 * If the LWP is already detached, there's nothing to do.
    377 	 * If it's a zombie, we need to clean up after it.  LSZOMB
    378 	 * is visible with the proc mutex held.
    379 	 *
    380 	 * After we have detached or released the LWP, kick any
    381 	 * other LWPs that may be sitting in _lwp_wait(), waiting
    382 	 * for the target LWP to exit.
    383 	 */
    384 	if (t != NULL && t->l_stat != LSIDL) {
    385 		if ((t->l_prflag & LPR_DETACHED) == 0) {
    386 			p->p_ndlwps++;
    387 			t->l_prflag |= LPR_DETACHED;
    388 			if (t->l_stat == LSZOMB) {
    389 				/* Releases proc mutex. */
    390 				lwp_free(t, false, false);
    391 				return 0;
    392 			}
    393 			error = 0;
    394 
    395 			/*
    396 			 * Have any LWPs sleeping in lwp_wait() recheck
    397 			 * for deadlock.
    398 			 */
    399 			cv_broadcast(&p->p_lwpcv);
    400 		} else
    401 			error = EINVAL;
    402 	} else
    403 		error = ESRCH;
    404 
    405 	mutex_exit(&p->p_smutex);
    406 
    407 	return error;
    408 }
    409 
    410 static inline wchan_t
    411 lwp_park_wchan(struct proc *p, const void *hint)
    412 {
    413 	return (wchan_t)((uintptr_t)p ^ (uintptr_t)hint);
    414 }
    415 
    416 /*
    417  * 'park' an LWP waiting on a user-level synchronisation object.  The LWP
    418  * will remain parked until another LWP in the same process calls in and
    419  * requests that it be unparked.
    420  */
    421 int
    422 sys__lwp_park(struct lwp *l, void *v, register_t *retval)
    423 {
    424 	struct sys__lwp_park_args /* {
    425 		syscallarg(const struct timespec *)	ts;
    426 		syscallarg(ucontext_t *)		uc;
    427 		syscallarg(const void *)		hint;
    428 	} */ *uap = v;
    429 	const struct timespec *tsp;
    430 	struct timespec ts, tsx;
    431 	struct timeval tv;
    432 	sleepq_t *sq;
    433 	wchan_t wchan;
    434 	int timo, error;
    435 
    436 	/* Fix up the given timeout value. */
    437 	if ((tsp = SCARG(uap, ts)) != NULL) {
    438 		if ((error = copyin(tsp, &ts, sizeof(ts))) != 0)
    439 			return error;
    440 		getnanotime(&tsx);
    441 		timespecsub(&ts, &tsx, &ts);
    442 		tv.tv_sec = ts.tv_sec;
    443 		tv.tv_usec = ts.tv_nsec / 1000;
    444 		if (tv.tv_sec < 0 || (tv.tv_sec == 0 && tv.tv_usec < 0))
    445 			return ETIMEDOUT;
    446 		if ((error = itimerfix(&tv)) != 0)
    447 			return error;
    448 		timo = tvtohz(&tv);
    449 	} else
    450 		timo = 0;
    451 
    452 	/* Find and lock the sleep queue. */
    453 	wchan = lwp_park_wchan(l->l_proc, SCARG(uap, hint));
    454 	sq = sleeptab_lookup(&lwp_park_tab, wchan);
    455 
    456 	/*
    457 	 * Before going the full route and blocking, check to see if an
    458 	 * unpark op is pending.
    459 	 */
    460 	sleepq_lwp_lock(l);
    461 	if ((l->l_flag & (LW_CANCELLED | LW_UNPARKED)) != 0) {
    462 		l->l_flag &= ~(LW_CANCELLED | LW_UNPARKED);
    463 		sleepq_lwp_unlock(l);
    464 		sleepq_unlock(sq);
    465 		return EALREADY;
    466 	}
    467 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
    468 	lwp_unlock_to(l, sq->sq_mutex);
    469 #endif
    470 
    471 	/*
    472 	 * For now we ignore the ucontext argument.  In the future, we may
    473 	 * put our stack up to be recycled.  If it's binned, a trampoline
    474 	 * function could call sleepq_unblock() on our behalf.
    475 	 */
    476 	KERNEL_UNLOCK_ALL(l, &l->l_biglocks); /* XXX for compat32 */
    477 	sleepq_block(sq, sched_kpri(l), wchan, "parked", timo, 1,
    478 	    &lwp_park_sobj);
    479 	error = sleepq_unblock(timo, 1);
    480 	switch (error) {
    481 	case EWOULDBLOCK:
    482 		error = ETIMEDOUT;
    483 		break;
    484 	case ERESTART:
    485 		error = EINTR;
    486 		break;
    487 	default:
    488 		/* nothing */
    489 		break;
    490 	}
    491 	return error;
    492 }
    493 
    494 int
    495 sys__lwp_unpark(struct lwp *l, void *v, register_t *retval)
    496 {
    497 	struct sys__lwp_unpark_args /* {
    498 		syscallarg(lwpid_t)		target;
    499 		syscallarg(const void *)	hint;
    500 	} */ *uap = v;
    501 	struct proc *p;
    502 	struct lwp *t;
    503 	sleepq_t *sq;
    504 	lwpid_t target;
    505 	wchan_t wchan;
    506 	int swapin;
    507 
    508 	p = l->l_proc;
    509 	target = SCARG(uap, target);
    510 
    511 	/*
    512 	 * Easy case: search for the LWP on the sleep queue.  If
    513 	 * it's parked, remove it from the queue and set running.
    514 	 */
    515 	wchan = lwp_park_wchan(p, SCARG(uap, hint));
    516 	sq = sleeptab_lookup(&lwp_park_tab, wchan);
    517 
    518 	TAILQ_FOREACH(t, &sq->sq_queue, l_sleepchain)
    519 		if (t->l_proc == p && t->l_lid == target)
    520 			break;
    521 
    522 	if (__predict_true(t != NULL)) {
    523 		swapin = sleepq_remove(sq, t);
    524 		sleepq_unlock(sq);
    525 		if (swapin)
    526 			uvm_kick_scheduler();
    527 		return 0;
    528 	}
    529 
    530 	/*
    531 	 * The LWP hasn't parked yet.  Take the hit and mark the
    532 	 * operation as pending.
    533 	 */
    534 	sleepq_unlock(sq);
    535 	mutex_enter(&p->p_smutex);
    536 	if ((t = lwp_find(p, target)) == NULL) {
    537 		mutex_exit(&p->p_smutex);
    538 		return ESRCH;
    539 	}
    540 	lwp_lock(t);
    541 
    542 	/*
    543 	 * It may not have parked yet, we may have raced, or it
    544 	 * is parked on a different user sync object.
    545 	 */
    546 	if (t->l_syncobj == &lwp_park_sobj) {
    547 		/* Releases the LWP lock. */
    548 		lwp_unsleep(t);
    549 	} else {
    550 		/*
    551 		 * Set the operation pending.  The next call to _lwp_park
    552 		 * will return early.
    553 		 */
    554 		t->l_flag |= LW_UNPARKED;
    555 		lwp_unlock(t);
    556 	}
    557 
    558 	mutex_exit(&p->p_smutex);
    559 	return 0;
    560 }
    561 
    562 int
    563 sys__lwp_unpark_all(struct lwp *l, void *v, register_t *retval)
    564 {
    565 	struct sys__lwp_unpark_all_args /* {
    566 		syscallarg(const lwpid_t *)	targets;
    567 		syscallarg(size_t)		ntargets;
    568 		syscallarg(const void *)	hint;
    569 	} */ *uap = v;
    570 	struct proc *p;
    571 	struct lwp *t;
    572 	sleepq_t *sq;
    573 	wchan_t wchan;
    574 	lwpid_t targets[32], *tp, *tpp, *tmax, target;
    575 	int swapin, error;
    576 	u_int ntargets;
    577 	size_t sz;
    578 
    579 	p = l->l_proc;
    580 	ntargets = SCARG(uap, ntargets);
    581 
    582 	if (SCARG(uap, targets) == NULL) {
    583 		/*
    584 		 * Let the caller know how much we are willing to do, and
    585 		 * let it unpark the LWPs in blocks.
    586 		 */
    587 		*retval = LWP_UNPARK_MAX;
    588 		return 0;
    589 	}
    590 	if (ntargets > LWP_UNPARK_MAX || ntargets == 0)
    591 		return EINVAL;
    592 
    593 	/*
    594 	 * Copy in the target array.  If it's a small number of LWPs, then
    595 	 * place the numbers on the stack.
    596 	 */
    597 	sz = sizeof(target) * ntargets;
    598 	if (sz <= sizeof(targets))
    599 		tp = targets;
    600 	else {
    601 		KERNEL_LOCK(1, l);		/* XXXSMP */
    602 		tp = kmem_alloc(sz, KM_SLEEP);
    603 		KERNEL_UNLOCK_ONE(l);		/* XXXSMP */
    604 		if (tp == NULL)
    605 			return ENOMEM;
    606 	}
    607 	error = copyin(SCARG(uap, targets), tp, sz);
    608 	if (error != 0) {
    609 		if (tp != targets) {
    610 			KERNEL_LOCK(1, l);	/* XXXSMP */
    611 			kmem_free(tp, sz);
    612 			KERNEL_UNLOCK_ONE(l);	/* XXXSMP */
    613 		}
    614 		return error;
    615 	}
    616 
    617 	swapin = 0;
    618 	wchan = lwp_park_wchan(p, SCARG(uap, hint));
    619 	sq = sleeptab_lookup(&lwp_park_tab, wchan);
    620 
    621 	for (tmax = tp + ntargets, tpp = tp; tpp < tmax; tpp++) {
    622 		target = *tpp;
    623 
    624 		/*
    625 		 * Easy case: search for the LWP on the sleep queue.  If
    626 		 * it's parked, remove it from the queue and set running.
    627 		 */
    628 		TAILQ_FOREACH(t, &sq->sq_queue, l_sleepchain)
    629 			if (t->l_proc == p && t->l_lid == target)
    630 				break;
    631 
    632 		if (t != NULL) {
    633 			swapin |= sleepq_remove(sq, t);
    634 			continue;
    635 		}
    636 
    637 		/*
    638 		 * The LWP hasn't parked yet.  Take the hit and
    639 		 * mark the operation as pending.
    640 		 */
    641 		sleepq_unlock(sq);
    642 		mutex_enter(&p->p_smutex);
    643 		if ((t = lwp_find(p, target)) == NULL) {
    644 			mutex_exit(&p->p_smutex);
    645 			sleepq_lock(sq);
    646 			continue;
    647 		}
    648 		lwp_lock(t);
    649 
    650 		/*
    651 		 * It may not have parked yet, we may have raced, or
    652 		 * it is parked on a different user sync object.
    653 		 */
    654 		if (t->l_syncobj == &lwp_park_sobj) {
    655 			/* Releases the LWP lock. */
    656 			lwp_unsleep(t);
    657 		} else {
    658 			/*
    659 			 * Set the operation pending.  The next call to
    660 			 * _lwp_park will return early.
    661 			 */
    662 			t->l_flag |= LW_UNPARKED;
    663 			lwp_unlock(t);
    664 		}
    665 
    666 		mutex_exit(&p->p_smutex);
    667 		sleepq_lock(sq);
    668 	}
    669 
    670 	sleepq_unlock(sq);
    671 	if (tp != targets) {
    672 		KERNEL_LOCK(1, l);		/* XXXSMP */
    673 		kmem_free(tp, sz);
    674 		KERNEL_UNLOCK_ONE(l);		/* XXXSMP */
    675 	}
    676 	if (swapin)
    677 		uvm_kick_scheduler();
    678 
    679 	return 0;
    680 }
    681