Home | History | Annotate | Line # | Download | only in kern
sys_lwp.c revision 1.23
      1 /*	$NetBSD: sys_lwp.c,v 1.23 2007/08/02 01:48:45 rmind 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.23 2007/08/02 01:48:45 rmind 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 	if (error) {
    225 		mutex_exit(&p->p_smutex);
    226 		return error;
    227 	}
    228 
    229 	/*
    230 	 * Wait for:
    231 	 *  o process exiting
    232 	 *  o target LWP suspended
    233 	 *  o target LWP not suspended and L_WSUSPEND clear
    234 	 *  o target LWP exited
    235 	 */
    236 	for (;;) {
    237 		error = cv_wait_sig(&p->p_lwpcv, &p->p_smutex);
    238 		if (error) {
    239 			error = ERESTART;
    240 			break;
    241 		}
    242 		if ((l->l_flag | t->l_flag) & (LW_WCORE | LW_WEXIT)) {
    243 			error = ERESTART;
    244 			break;
    245 		}
    246 		if (t->l_stat == LSSUSPENDED ||
    247 		    (t->l_flag & LW_WSUSPEND) == 0)
    248 			break;
    249 	}
    250 	mutex_exit(&p->p_smutex);
    251 
    252 	return error;
    253 }
    254 
    255 int
    256 sys__lwp_continue(struct lwp *l, void *v, register_t *retval)
    257 {
    258 	struct sys__lwp_continue_args /* {
    259 		syscallarg(lwpid_t) target;
    260 	} */ *uap = v;
    261 	int error;
    262 	struct proc *p = l->l_proc;
    263 	struct lwp *t;
    264 
    265 	error = 0;
    266 
    267 	mutex_enter(&p->p_smutex);
    268 	if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
    269 		mutex_exit(&p->p_smutex);
    270 		return ESRCH;
    271 	}
    272 
    273 	lwp_lock(t);
    274 	lwp_continue(t);
    275 	mutex_exit(&p->p_smutex);
    276 
    277 	return error;
    278 }
    279 
    280 int
    281 sys__lwp_wakeup(struct lwp *l, void *v, register_t *retval)
    282 {
    283 	struct sys__lwp_wakeup_args /* {
    284 		syscallarg(lwpid_t) target;
    285 	} */ *uap = v;
    286 	struct lwp *t;
    287 	struct proc *p;
    288 	int error;
    289 
    290 	p = l->l_proc;
    291 	mutex_enter(&p->p_smutex);
    292 
    293 	if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
    294 		mutex_exit(&p->p_smutex);
    295 		return ESRCH;
    296 	}
    297 
    298 	lwp_lock(t);
    299 	t->l_flag |= (LW_CANCELLED | LW_UNPARKED);
    300 
    301 	if (t->l_stat != LSSLEEP) {
    302 		lwp_unlock(t);
    303 		error = ENODEV;
    304 	} else if ((t->l_flag & LW_SINTR) == 0) {
    305 		lwp_unlock(t);
    306 		error = EBUSY;
    307 	} else {
    308 		/* Wake it up.  lwp_unsleep() will release the LWP lock. */
    309 		lwp_unsleep(t);
    310 		error = 0;
    311 	}
    312 
    313 	mutex_exit(&p->p_smutex);
    314 
    315 	return error;
    316 }
    317 
    318 int
    319 sys__lwp_wait(struct lwp *l, void *v, register_t *retval)
    320 {
    321 	struct sys__lwp_wait_args /* {
    322 		syscallarg(lwpid_t) wait_for;
    323 		syscallarg(lwpid_t *) departed;
    324 	} */ *uap = v;
    325 	struct proc *p = l->l_proc;
    326 	int error;
    327 	lwpid_t dep;
    328 
    329 	mutex_enter(&p->p_smutex);
    330 	error = lwp_wait1(l, SCARG(uap, wait_for), &dep, 0);
    331 	mutex_exit(&p->p_smutex);
    332 
    333 	if (error)
    334 		return error;
    335 
    336 	if (SCARG(uap, departed)) {
    337 		error = copyout(&dep, SCARG(uap, departed), sizeof(dep));
    338 		if (error)
    339 			return error;
    340 	}
    341 
    342 	return 0;
    343 }
    344 
    345 /* ARGSUSED */
    346 int
    347 sys__lwp_kill(struct lwp *l, void *v, register_t *retval)
    348 {
    349 	struct sys__lwp_kill_args /* {
    350 		syscallarg(lwpid_t)	target;
    351 		syscallarg(int)		signo;
    352 	} */ *uap = v;
    353 	struct proc *p = l->l_proc;
    354 	struct lwp *t;
    355 	ksiginfo_t ksi;
    356 	int signo = SCARG(uap, signo);
    357 	int error = 0;
    358 
    359 	if ((u_int)signo >= NSIG)
    360 		return EINVAL;
    361 
    362 	KSI_INIT(&ksi);
    363 	ksi.ksi_signo = signo;
    364 	ksi.ksi_code = SI_USER;
    365 	ksi.ksi_pid = p->p_pid;
    366 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
    367 	ksi.ksi_lid = SCARG(uap, target);
    368 
    369 	mutex_enter(&proclist_mutex);
    370 	mutex_enter(&p->p_smutex);
    371 	if ((t = lwp_find(p, ksi.ksi_lid)) == NULL)
    372 		error = ESRCH;
    373 	else if (signo != 0)
    374 		kpsignal2(p, &ksi);
    375 	mutex_exit(&p->p_smutex);
    376 	mutex_exit(&proclist_mutex);
    377 
    378 	return error;
    379 }
    380 
    381 int
    382 sys__lwp_detach(struct lwp *l, void *v, register_t *retval)
    383 {
    384 	struct sys__lwp_detach_args /* {
    385 		syscallarg(lwpid_t)	target;
    386 	} */ *uap = v;
    387 	struct proc *p;
    388 	struct lwp *t;
    389 	lwpid_t target;
    390 	int error;
    391 
    392 	target = SCARG(uap, target);
    393 	p = l->l_proc;
    394 
    395 	mutex_enter(&p->p_smutex);
    396 
    397 	if (l->l_lid == target)
    398 		t = l;
    399 	else {
    400 		/*
    401 		 * We can't use lwp_find() here because the target might
    402 		 * be a zombie.
    403 		 */
    404 		LIST_FOREACH(t, &p->p_lwps, l_sibling)
    405 			if (t->l_lid == target)
    406 				break;
    407 	}
    408 
    409 	/*
    410 	 * If the LWP is already detached, there's nothing to do.
    411 	 * If it's a zombie, we need to clean up after it.  LSZOMB
    412 	 * is visible with the proc mutex held.
    413 	 *
    414 	 * After we have detached or released the LWP, kick any
    415 	 * other LWPs that may be sitting in _lwp_wait(), waiting
    416 	 * for the target LWP to exit.
    417 	 */
    418 	if (t != NULL && t->l_stat != LSIDL) {
    419 		if ((t->l_prflag & LPR_DETACHED) == 0) {
    420 			p->p_ndlwps++;
    421 			t->l_prflag |= LPR_DETACHED;
    422 			if (t->l_stat == LSZOMB) {
    423 				/* Releases proc mutex. */
    424 				lwp_free(t, false, false);
    425 				return 0;
    426 			}
    427 			error = 0;
    428 
    429 			/*
    430 			 * Have any LWPs sleeping in lwp_wait() recheck
    431 			 * for deadlock.
    432 			 */
    433 			cv_broadcast(&p->p_lwpcv);
    434 		} else
    435 			error = EINVAL;
    436 	} else
    437 		error = ESRCH;
    438 
    439 	mutex_exit(&p->p_smutex);
    440 
    441 	return error;
    442 }
    443 
    444 static inline wchan_t
    445 lwp_park_wchan(struct proc *p, const void *hint)
    446 {
    447 
    448 	return (wchan_t)((uintptr_t)p ^ (uintptr_t)hint);
    449 }
    450 
    451 /*
    452  * 'park' an LWP waiting on a user-level synchronisation object.  The LWP
    453  * will remain parked until another LWP in the same process calls in and
    454  * requests that it be unparked.
    455  */
    456 int
    457 sys__lwp_park(struct lwp *l, void *v, register_t *retval)
    458 {
    459 	struct sys__lwp_park_args /* {
    460 		syscallarg(const struct timespec *)	ts;
    461 		syscallarg(ucontext_t *)		ucp;
    462 		syscallarg(const void *)		hint;
    463 	} */ *uap = v;
    464 	struct timespec ts;
    465 	int error;
    466 
    467 	if (SCARG(uap, ts) == NULL)
    468 		return do_sys_lwp_park(l, NULL, SCARG(uap, ucp),
    469 		    SCARG(uap, hint));
    470 
    471 	if ((error = copyin(SCARG(uap, ts), &ts, sizeof(ts))) != 0)
    472 		return error;
    473 
    474 	return do_sys_lwp_park(l, &ts, SCARG(uap, ucp), SCARG(uap, hint));
    475 }
    476 
    477 int
    478 do_sys_lwp_park(struct lwp *l, struct timespec *ts, ucontext_t *uc,
    479 		const void *hint)
    480 {
    481 	struct timespec tsx;
    482 	struct timeval tv;
    483 	sleepq_t *sq;
    484 	wchan_t wchan;
    485 	int timo, error;
    486 
    487 	/* Fix up the given timeout value. */
    488 	if (ts != NULL) {
    489 		getnanotime(&tsx);
    490 		timespecsub(ts, &tsx, ts);
    491 		tv.tv_sec = ts->tv_sec;
    492 		tv.tv_usec = ts->tv_nsec / 1000;
    493 		if (tv.tv_sec < 0 || (tv.tv_sec == 0 && tv.tv_usec < 0))
    494 			return ETIMEDOUT;
    495 		if ((error = itimerfix(&tv)) != 0)
    496 			return error;
    497 		timo = tvtohz(&tv);
    498 	} else
    499 		timo = 0;
    500 
    501 	/* Find and lock the sleep queue. */
    502 	wchan = lwp_park_wchan(l->l_proc, hint);
    503 	sq = sleeptab_lookup(&lwp_park_tab, wchan);
    504 
    505 	/*
    506 	 * Before going the full route and blocking, check to see if an
    507 	 * unpark op is pending.
    508 	 */
    509 	lwp_lock(l);
    510 	if ((l->l_flag & (LW_CANCELLED | LW_UNPARKED)) != 0) {
    511 		l->l_flag &= ~(LW_CANCELLED | LW_UNPARKED);
    512 		lwp_unlock(l);
    513 		sleepq_unlock(sq);
    514 		return EALREADY;
    515 	}
    516 	lwp_unlock_to(l, sq->sq_mutex);
    517 
    518 	KERNEL_UNLOCK_ALL(l, &l->l_biglocks); /* XXX for compat32 */
    519 	sleepq_enqueue(sq, sched_kpri(l), wchan, "parked", &lwp_park_sobj);
    520 	error = sleepq_block(timo, true);
    521 	switch (error) {
    522 	case EWOULDBLOCK:
    523 		error = ETIMEDOUT;
    524 		break;
    525 	case ERESTART:
    526 		error = EINTR;
    527 		break;
    528 	default:
    529 		/* nothing */
    530 		break;
    531 	}
    532 	return error;
    533 }
    534 
    535 int
    536 sys__lwp_unpark(struct lwp *l, void *v, register_t *retval)
    537 {
    538 	struct sys__lwp_unpark_args /* {
    539 		syscallarg(lwpid_t)		target;
    540 		syscallarg(const void *)	hint;
    541 	} */ *uap = v;
    542 	struct proc *p;
    543 	struct lwp *t;
    544 	sleepq_t *sq;
    545 	lwpid_t target;
    546 	wchan_t wchan;
    547 	int swapin;
    548 
    549 	p = l->l_proc;
    550 	target = SCARG(uap, target);
    551 
    552 	/*
    553 	 * Easy case: search for the LWP on the sleep queue.  If
    554 	 * it's parked, remove it from the queue and set running.
    555 	 */
    556 	wchan = lwp_park_wchan(p, SCARG(uap, hint));
    557 	sq = sleeptab_lookup(&lwp_park_tab, wchan);
    558 
    559 	TAILQ_FOREACH(t, &sq->sq_queue, l_sleepchain)
    560 		if (t->l_proc == p && t->l_lid == target)
    561 			break;
    562 
    563 	if (__predict_true(t != NULL)) {
    564 		swapin = sleepq_remove(sq, t);
    565 		sleepq_unlock(sq);
    566 		if (swapin)
    567 			uvm_kick_scheduler();
    568 		return 0;
    569 	}
    570 
    571 	/*
    572 	 * The LWP hasn't parked yet.  Take the hit and mark the
    573 	 * operation as pending.
    574 	 */
    575 	sleepq_unlock(sq);
    576 	mutex_enter(&p->p_smutex);
    577 	if ((t = lwp_find(p, target)) == NULL) {
    578 		mutex_exit(&p->p_smutex);
    579 		return ESRCH;
    580 	}
    581 	lwp_lock(t);
    582 
    583 	/*
    584 	 * It may not have parked yet, we may have raced, or it
    585 	 * is parked on a different user sync object.
    586 	 */
    587 	if (t->l_syncobj == &lwp_park_sobj) {
    588 		/* Releases the LWP lock. */
    589 		lwp_unsleep(t);
    590 	} else {
    591 		/*
    592 		 * Set the operation pending.  The next call to _lwp_park
    593 		 * will return early.
    594 		 */
    595 		t->l_flag |= LW_UNPARKED;
    596 		lwp_unlock(t);
    597 	}
    598 
    599 	mutex_exit(&p->p_smutex);
    600 	return 0;
    601 }
    602 
    603 int
    604 sys__lwp_unpark_all(struct lwp *l, void *v, register_t *retval)
    605 {
    606 	struct sys__lwp_unpark_all_args /* {
    607 		syscallarg(const lwpid_t *)	targets;
    608 		syscallarg(size_t)		ntargets;
    609 		syscallarg(const void *)	hint;
    610 	} */ *uap = v;
    611 	struct proc *p;
    612 	struct lwp *t;
    613 	sleepq_t *sq;
    614 	wchan_t wchan;
    615 	lwpid_t targets[32], *tp, *tpp, *tmax, target;
    616 	int swapin, error;
    617 	u_int ntargets;
    618 	size_t sz;
    619 
    620 	p = l->l_proc;
    621 	ntargets = SCARG(uap, ntargets);
    622 
    623 	if (SCARG(uap, targets) == NULL) {
    624 		/*
    625 		 * Let the caller know how much we are willing to do, and
    626 		 * let it unpark the LWPs in blocks.
    627 		 */
    628 		*retval = LWP_UNPARK_MAX;
    629 		return 0;
    630 	}
    631 	if (ntargets > LWP_UNPARK_MAX || ntargets == 0)
    632 		return EINVAL;
    633 
    634 	/*
    635 	 * Copy in the target array.  If it's a small number of LWPs, then
    636 	 * place the numbers on the stack.
    637 	 */
    638 	sz = sizeof(target) * ntargets;
    639 	if (sz <= sizeof(targets))
    640 		tp = targets;
    641 	else {
    642 		KERNEL_LOCK(1, l);		/* XXXSMP */
    643 		tp = kmem_alloc(sz, KM_SLEEP);
    644 		KERNEL_UNLOCK_ONE(l);		/* XXXSMP */
    645 		if (tp == NULL)
    646 			return ENOMEM;
    647 	}
    648 	error = copyin(SCARG(uap, targets), tp, sz);
    649 	if (error != 0) {
    650 		if (tp != targets) {
    651 			KERNEL_LOCK(1, l);	/* XXXSMP */
    652 			kmem_free(tp, sz);
    653 			KERNEL_UNLOCK_ONE(l);	/* XXXSMP */
    654 		}
    655 		return error;
    656 	}
    657 
    658 	swapin = 0;
    659 	wchan = lwp_park_wchan(p, SCARG(uap, hint));
    660 	sq = sleeptab_lookup(&lwp_park_tab, wchan);
    661 
    662 	for (tmax = tp + ntargets, tpp = tp; tpp < tmax; tpp++) {
    663 		target = *tpp;
    664 
    665 		/*
    666 		 * Easy case: search for the LWP on the sleep queue.  If
    667 		 * it's parked, remove it from the queue and set running.
    668 		 */
    669 		TAILQ_FOREACH(t, &sq->sq_queue, l_sleepchain)
    670 			if (t->l_proc == p && t->l_lid == target)
    671 				break;
    672 
    673 		if (t != NULL) {
    674 			swapin |= sleepq_remove(sq, t);
    675 			continue;
    676 		}
    677 
    678 		/*
    679 		 * The LWP hasn't parked yet.  Take the hit and
    680 		 * mark the operation as pending.
    681 		 */
    682 		sleepq_unlock(sq);
    683 		mutex_enter(&p->p_smutex);
    684 		if ((t = lwp_find(p, target)) == NULL) {
    685 			mutex_exit(&p->p_smutex);
    686 			sleepq_lock(sq);
    687 			continue;
    688 		}
    689 		lwp_lock(t);
    690 
    691 		/*
    692 		 * It may not have parked yet, we may have raced, or
    693 		 * it is parked on a different user sync object.
    694 		 */
    695 		if (t->l_syncobj == &lwp_park_sobj) {
    696 			/* Releases the LWP lock. */
    697 			lwp_unsleep(t);
    698 		} else {
    699 			/*
    700 			 * Set the operation pending.  The next call to
    701 			 * _lwp_park will return early.
    702 			 */
    703 			t->l_flag |= LW_UNPARKED;
    704 			lwp_unlock(t);
    705 		}
    706 
    707 		mutex_exit(&p->p_smutex);
    708 		sleepq_lock(sq);
    709 	}
    710 
    711 	sleepq_unlock(sq);
    712 	if (tp != targets) {
    713 		KERNEL_LOCK(1, l);		/* XXXSMP */
    714 		kmem_free(tp, sz);
    715 		KERNEL_UNLOCK_ONE(l);		/* XXXSMP */
    716 	}
    717 	if (swapin)
    718 		uvm_kick_scheduler();
    719 
    720 	return 0;
    721 }
    722