kern_lwp.c revision 1.239 1 /* $NetBSD: kern_lwp.c,v 1.239 2020/05/23 23:42:43 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2001, 2006, 2007, 2008, 2009, 2019, 2020
5 * The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Nathan J. Williams, and Andrew Doran.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Overview
35 *
36 * Lightweight processes (LWPs) are the basic unit or thread of
37 * execution within the kernel. The core state of an LWP is described
38 * by "struct lwp", also known as lwp_t.
39 *
40 * Each LWP is contained within a process (described by "struct proc"),
41 * Every process contains at least one LWP, but may contain more. The
42 * process describes attributes shared among all of its LWPs such as a
43 * private address space, global execution state (stopped, active,
44 * zombie, ...), signal disposition and so on. On a multiprocessor
45 * machine, multiple LWPs be executing concurrently in the kernel.
46 *
47 * Execution states
48 *
49 * At any given time, an LWP has overall state that is described by
50 * lwp::l_stat. The states are broken into two sets below. The first
51 * set is guaranteed to represent the absolute, current state of the
52 * LWP:
53 *
54 * LSONPROC
55 *
56 * On processor: the LWP is executing on a CPU, either in the
57 * kernel or in user space.
58 *
59 * LSRUN
60 *
61 * Runnable: the LWP is parked on a run queue, and may soon be
62 * chosen to run by an idle processor, or by a processor that
63 * has been asked to preempt a currently runnning but lower
64 * priority LWP.
65 *
66 * LSIDL
67 *
68 * Idle: the LWP has been created but has not yet executed, or
69 * it has ceased executing a unit of work and is waiting to be
70 * started again. This state exists so that the LWP can occupy
71 * a slot in the process & PID table, but without having to
72 * worry about being touched; lookups of the LWP by ID will
73 * fail while in this state. The LWP will become visible for
74 * lookup once its state transitions further. Some special
75 * kernel threads also (ab)use this state to indicate that they
76 * are idle (soft interrupts and idle LWPs).
77 *
78 * LSSUSPENDED:
79 *
80 * Suspended: the LWP has had its execution suspended by
81 * another LWP in the same process using the _lwp_suspend()
82 * system call. User-level LWPs also enter the suspended
83 * state when the system is shutting down.
84 *
85 * The second set represent a "statement of intent" on behalf of the
86 * LWP. The LWP may in fact be executing on a processor, may be
87 * sleeping or idle. It is expected to take the necessary action to
88 * stop executing or become "running" again within a short timeframe.
89 * The LP_RUNNING flag in lwp::l_pflag indicates that an LWP is running.
90 * Importantly, it indicates that its state is tied to a CPU.
91 *
92 * LSZOMB:
93 *
94 * Dead or dying: the LWP has released most of its resources
95 * and is about to switch away into oblivion, or has already
96 * switched away. When it switches away, its few remaining
97 * resources can be collected.
98 *
99 * LSSLEEP:
100 *
101 * Sleeping: the LWP has entered itself onto a sleep queue, and
102 * has switched away or will switch away shortly to allow other
103 * LWPs to run on the CPU.
104 *
105 * LSSTOP:
106 *
107 * Stopped: the LWP has been stopped as a result of a job
108 * control signal, or as a result of the ptrace() interface.
109 *
110 * Stopped LWPs may run briefly within the kernel to handle
111 * signals that they receive, but will not return to user space
112 * until their process' state is changed away from stopped.
113 *
114 * Single LWPs within a process can not be set stopped
115 * selectively: all actions that can stop or continue LWPs
116 * occur at the process level.
117 *
118 * State transitions
119 *
120 * Note that the LSSTOP state may only be set when returning to
121 * user space in userret(), or when sleeping interruptably. The
122 * LSSUSPENDED state may only be set in userret(). Before setting
123 * those states, we try to ensure that the LWPs will release all
124 * locks that they hold, and at a minimum try to ensure that the
125 * LWP can be set runnable again by a signal.
126 *
127 * LWPs may transition states in the following ways:
128 *
129 * RUN -------> ONPROC ONPROC -----> RUN
130 * > SLEEP
131 * > STOPPED
132 * > SUSPENDED
133 * > ZOMB
134 * > IDL (special cases)
135 *
136 * STOPPED ---> RUN SUSPENDED --> RUN
137 * > SLEEP
138 *
139 * SLEEP -----> ONPROC IDL --------> RUN
140 * > RUN > SUSPENDED
141 * > STOPPED > STOPPED
142 * > ONPROC (special cases)
143 *
144 * Some state transitions are only possible with kernel threads (eg
145 * ONPROC -> IDL) and happen under tightly controlled circumstances
146 * free of unwanted side effects.
147 *
148 * Migration
149 *
150 * Migration of threads from one CPU to another could be performed
151 * internally by the scheduler via sched_takecpu() or sched_catchlwp()
152 * functions. The universal lwp_migrate() function should be used for
153 * any other cases. Subsystems in the kernel must be aware that CPU
154 * of LWP may change, while it is not locked.
155 *
156 * Locking
157 *
158 * The majority of fields in 'struct lwp' are covered by a single,
159 * general spin lock pointed to by lwp::l_mutex. The locks covering
160 * each field are documented in sys/lwp.h.
161 *
162 * State transitions must be made with the LWP's general lock held,
163 * and may cause the LWP's lock pointer to change. Manipulation of
164 * the general lock is not performed directly, but through calls to
165 * lwp_lock(), lwp_unlock() and others. It should be noted that the
166 * adaptive locks are not allowed to be released while the LWP's lock
167 * is being held (unlike for other spin-locks).
168 *
169 * States and their associated locks:
170 *
171 * LSIDL, LSONPROC, LSZOMB, LSSUPENDED:
172 *
173 * Always covered by spc_lwplock, which protects LWPs not
174 * associated with any other sync object. This is a per-CPU
175 * lock and matches lwp::l_cpu.
176 *
177 * LSRUN:
178 *
179 * Always covered by spc_mutex, which protects the run queues.
180 * This is a per-CPU lock and matches lwp::l_cpu.
181 *
182 * LSSLEEP:
183 *
184 * Covered by a lock associated with the sleep queue (sometimes
185 * a turnstile sleep queue) that the LWP resides on. This can
186 * be spc_lwplock for SOBJ_SLEEPQ_NULL (an "untracked" sleep).
187 *
188 * LSSTOP:
189 *
190 * If the LWP was previously sleeping (l_wchan != NULL), then
191 * l_mutex references the sleep queue lock. If the LWP was
192 * runnable or on the CPU when halted, or has been removed from
193 * the sleep queue since halted, then the lock is spc_lwplock.
194 *
195 * The lock order is as follows:
196 *
197 * sleepq -> turnstile -> spc_lwplock -> spc_mutex
198 *
199 * Each process has an scheduler state lock (proc::p_lock), and a
200 * number of counters on LWPs and their states: p_nzlwps, p_nrlwps, and
201 * so on. When an LWP is to be entered into or removed from one of the
202 * following states, p_lock must be held and the process wide counters
203 * adjusted:
204 *
205 * LSIDL, LSZOMB, LSSTOP, LSSUSPENDED
206 *
207 * (But not always for kernel threads. There are some special cases
208 * as mentioned above: soft interrupts, and the idle loops.)
209 *
210 * Note that an LWP is considered running or likely to run soon if in
211 * one of the following states. This affects the value of p_nrlwps:
212 *
213 * LSRUN, LSONPROC, LSSLEEP
214 *
215 * p_lock does not need to be held when transitioning among these
216 * three states, hence p_lock is rarely taken for state transitions.
217 */
218
219 #include <sys/cdefs.h>
220 __KERNEL_RCSID(0, "$NetBSD: kern_lwp.c,v 1.239 2020/05/23 23:42:43 ad Exp $");
221
222 #include "opt_ddb.h"
223 #include "opt_lockdebug.h"
224 #include "opt_dtrace.h"
225
226 #define _LWP_API_PRIVATE
227
228 #include <sys/param.h>
229 #include <sys/systm.h>
230 #include <sys/cpu.h>
231 #include <sys/pool.h>
232 #include <sys/proc.h>
233 #include <sys/syscallargs.h>
234 #include <sys/syscall_stats.h>
235 #include <sys/kauth.h>
236 #include <sys/sleepq.h>
237 #include <sys/lockdebug.h>
238 #include <sys/kmem.h>
239 #include <sys/pset.h>
240 #include <sys/intr.h>
241 #include <sys/lwpctl.h>
242 #include <sys/atomic.h>
243 #include <sys/filedesc.h>
244 #include <sys/fstrans.h>
245 #include <sys/dtrace_bsd.h>
246 #include <sys/sdt.h>
247 #include <sys/ptrace.h>
248 #include <sys/xcall.h>
249 #include <sys/uidinfo.h>
250 #include <sys/sysctl.h>
251 #include <sys/psref.h>
252 #include <sys/msan.h>
253 #include <sys/kcov.h>
254 #include <sys/cprng.h>
255 #include <sys/futex.h>
256
257 #include <uvm/uvm_extern.h>
258 #include <uvm/uvm_object.h>
259
260 static pool_cache_t lwp_cache __read_mostly;
261 struct lwplist alllwp __cacheline_aligned;
262
263 static int lwp_ctor(void *, void *, int);
264 static void lwp_dtor(void *, void *);
265
266 /* DTrace proc provider probes */
267 SDT_PROVIDER_DEFINE(proc);
268
269 SDT_PROBE_DEFINE1(proc, kernel, , lwp__create, "struct lwp *");
270 SDT_PROBE_DEFINE1(proc, kernel, , lwp__start, "struct lwp *");
271 SDT_PROBE_DEFINE1(proc, kernel, , lwp__exit, "struct lwp *");
272
273 struct turnstile turnstile0 __cacheline_aligned;
274 struct lwp lwp0 __aligned(MIN_LWP_ALIGNMENT) = {
275 #ifdef LWP0_CPU_INFO
276 .l_cpu = LWP0_CPU_INFO,
277 #endif
278 #ifdef LWP0_MD_INITIALIZER
279 .l_md = LWP0_MD_INITIALIZER,
280 #endif
281 .l_proc = &proc0,
282 .l_lid = 0, /* we own proc0's slot in the pid table */
283 .l_flag = LW_SYSTEM,
284 .l_stat = LSONPROC,
285 .l_ts = &turnstile0,
286 .l_syncobj = &sched_syncobj,
287 .l_refcnt = 0,
288 .l_priority = PRI_USER + NPRI_USER - 1,
289 .l_inheritedprio = -1,
290 .l_class = SCHED_OTHER,
291 .l_psid = PS_NONE,
292 .l_pi_lenders = SLIST_HEAD_INITIALIZER(&lwp0.l_pi_lenders),
293 .l_name = __UNCONST("swapper"),
294 .l_fd = &filedesc0,
295 };
296
297 static int sysctl_kern_maxlwp(SYSCTLFN_PROTO);
298
299 /*
300 * sysctl helper routine for kern.maxlwp. Ensures that the new
301 * values are not too low or too high.
302 */
303 static int
304 sysctl_kern_maxlwp(SYSCTLFN_ARGS)
305 {
306 int error, nmaxlwp;
307 struct sysctlnode node;
308
309 nmaxlwp = maxlwp;
310 node = *rnode;
311 node.sysctl_data = &nmaxlwp;
312 error = sysctl_lookup(SYSCTLFN_CALL(&node));
313 if (error || newp == NULL)
314 return error;
315
316 if (nmaxlwp < 0 || nmaxlwp >= 65536)
317 return EINVAL;
318 if (nmaxlwp > cpu_maxlwp())
319 return EINVAL;
320 maxlwp = nmaxlwp;
321
322 return 0;
323 }
324
325 static void
326 sysctl_kern_lwp_setup(void)
327 {
328 struct sysctllog *clog = NULL;
329
330 sysctl_createv(&clog, 0, NULL, NULL,
331 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
332 CTLTYPE_INT, "maxlwp",
333 SYSCTL_DESCR("Maximum number of simultaneous threads"),
334 sysctl_kern_maxlwp, 0, NULL, 0,
335 CTL_KERN, CTL_CREATE, CTL_EOL);
336 }
337
338 void
339 lwpinit(void)
340 {
341
342 LIST_INIT(&alllwp);
343 lwpinit_specificdata();
344 lwp_cache = pool_cache_init(sizeof(lwp_t), MIN_LWP_ALIGNMENT, 0, 0,
345 "lwppl", NULL, IPL_NONE, lwp_ctor, lwp_dtor, NULL);
346
347 maxlwp = cpu_maxlwp();
348 sysctl_kern_lwp_setup();
349 }
350
351 void
352 lwp0_init(void)
353 {
354 struct lwp *l = &lwp0;
355
356 KASSERT((void *)uvm_lwp_getuarea(l) != NULL);
357
358 LIST_INSERT_HEAD(&alllwp, l, l_list);
359
360 callout_init(&l->l_timeout_ch, CALLOUT_MPSAFE);
361 callout_setfunc(&l->l_timeout_ch, sleepq_timeout, l);
362 cv_init(&l->l_sigcv, "sigwait");
363 cv_init(&l->l_waitcv, "vfork");
364
365 kauth_cred_hold(proc0.p_cred);
366 l->l_cred = proc0.p_cred;
367
368 kdtrace_thread_ctor(NULL, l);
369 lwp_initspecific(l);
370
371 SYSCALL_TIME_LWP_INIT(l);
372 }
373
374 /*
375 * Initialize the non-zeroed portion of an lwp_t.
376 */
377 static int
378 lwp_ctor(void *arg, void *obj, int flags)
379 {
380 lwp_t *l = obj;
381
382 l->l_stat = LSIDL;
383 l->l_cpu = curcpu();
384 l->l_mutex = l->l_cpu->ci_schedstate.spc_lwplock;
385 l->l_ts = pool_get(&turnstile_pool, flags);
386
387 if (l->l_ts == NULL) {
388 return ENOMEM;
389 } else {
390 turnstile_ctor(l->l_ts);
391 return 0;
392 }
393 }
394
395 static void
396 lwp_dtor(void *arg, void *obj)
397 {
398 lwp_t *l = obj;
399 (void)l;
400
401 /*
402 * Provide a barrier to ensure that all mutex_oncpu() and rw_oncpu()
403 * calls will exit before memory of LWP is returned to the pool, where
404 * KVA of LWP structure might be freed and re-used for other purposes.
405 * Kernel preemption is disabled around mutex_oncpu() and rw_oncpu()
406 * callers, therefore cross-call to all CPUs will do the job. Also,
407 * the value of l->l_cpu must be still valid at this point.
408 *
409 * XXX should use epoch based reclamation.
410 */
411 KASSERT(l->l_cpu != NULL);
412 xc_barrier(0);
413
414 /*
415 * We can't return turnstile0 to the pool (it didn't come from it),
416 * so if it comes up just drop it quietly and move on.
417 */
418 if (l->l_ts != &turnstile0)
419 pool_put(&turnstile_pool, l->l_ts);
420 }
421
422 /*
423 * Set an LWP suspended.
424 *
425 * Must be called with p_lock held, and the LWP locked. Will unlock the
426 * LWP before return.
427 */
428 int
429 lwp_suspend(struct lwp *curl, struct lwp *t)
430 {
431 int error;
432
433 KASSERT(mutex_owned(t->l_proc->p_lock));
434 KASSERT(lwp_locked(t, NULL));
435
436 KASSERT(curl != t || curl->l_stat == LSONPROC);
437
438 /*
439 * If the current LWP has been told to exit, we must not suspend anyone
440 * else or deadlock could occur. We won't return to userspace.
441 */
442 if ((curl->l_flag & (LW_WEXIT | LW_WCORE)) != 0) {
443 lwp_unlock(t);
444 return (EDEADLK);
445 }
446
447 if ((t->l_flag & LW_DBGSUSPEND) != 0) {
448 lwp_unlock(t);
449 return 0;
450 }
451
452 error = 0;
453
454 switch (t->l_stat) {
455 case LSRUN:
456 case LSONPROC:
457 t->l_flag |= LW_WSUSPEND;
458 lwp_need_userret(t);
459 lwp_unlock(t);
460 break;
461
462 case LSSLEEP:
463 t->l_flag |= LW_WSUSPEND;
464
465 /*
466 * Kick the LWP and try to get it to the kernel boundary
467 * so that it will release any locks that it holds.
468 * setrunnable() will release the lock.
469 */
470 if ((t->l_flag & LW_SINTR) != 0)
471 setrunnable(t);
472 else
473 lwp_unlock(t);
474 break;
475
476 case LSSUSPENDED:
477 lwp_unlock(t);
478 break;
479
480 case LSSTOP:
481 t->l_flag |= LW_WSUSPEND;
482 setrunnable(t);
483 break;
484
485 case LSIDL:
486 case LSZOMB:
487 error = EINTR; /* It's what Solaris does..... */
488 lwp_unlock(t);
489 break;
490 }
491
492 return (error);
493 }
494
495 /*
496 * Restart a suspended LWP.
497 *
498 * Must be called with p_lock held, and the LWP locked. Will unlock the
499 * LWP before return.
500 */
501 void
502 lwp_continue(struct lwp *l)
503 {
504
505 KASSERT(mutex_owned(l->l_proc->p_lock));
506 KASSERT(lwp_locked(l, NULL));
507
508 /* If rebooting or not suspended, then just bail out. */
509 if ((l->l_flag & LW_WREBOOT) != 0) {
510 lwp_unlock(l);
511 return;
512 }
513
514 l->l_flag &= ~LW_WSUSPEND;
515
516 if (l->l_stat != LSSUSPENDED || (l->l_flag & LW_DBGSUSPEND) != 0) {
517 lwp_unlock(l);
518 return;
519 }
520
521 /* setrunnable() will release the lock. */
522 setrunnable(l);
523 }
524
525 /*
526 * Restart a stopped LWP.
527 *
528 * Must be called with p_lock held, and the LWP NOT locked. Will unlock the
529 * LWP before return.
530 */
531 void
532 lwp_unstop(struct lwp *l)
533 {
534 struct proc *p = l->l_proc;
535
536 KASSERT(mutex_owned(&proc_lock));
537 KASSERT(mutex_owned(p->p_lock));
538
539 lwp_lock(l);
540
541 KASSERT((l->l_flag & LW_DBGSUSPEND) == 0);
542
543 /* If not stopped, then just bail out. */
544 if (l->l_stat != LSSTOP) {
545 lwp_unlock(l);
546 return;
547 }
548
549 p->p_stat = SACTIVE;
550 p->p_sflag &= ~PS_STOPPING;
551
552 if (!p->p_waited)
553 p->p_pptr->p_nstopchild--;
554
555 if (l->l_wchan == NULL) {
556 /* setrunnable() will release the lock. */
557 setrunnable(l);
558 } else if (p->p_xsig && (l->l_flag & LW_SINTR) != 0) {
559 /* setrunnable() so we can receive the signal */
560 setrunnable(l);
561 } else {
562 l->l_stat = LSSLEEP;
563 p->p_nrlwps++;
564 lwp_unlock(l);
565 }
566 }
567
568 /*
569 * Wait for an LWP within the current process to exit. If 'lid' is
570 * non-zero, we are waiting for a specific LWP.
571 *
572 * Must be called with p->p_lock held.
573 */
574 int
575 lwp_wait(struct lwp *l, lwpid_t lid, lwpid_t *departed, bool exiting)
576 {
577 const lwpid_t curlid = l->l_lid;
578 proc_t *p = l->l_proc;
579 lwp_t *l2, *next;
580 int error;
581
582 KASSERT(mutex_owned(p->p_lock));
583
584 p->p_nlwpwait++;
585 l->l_waitingfor = lid;
586
587 for (;;) {
588 int nfound;
589
590 /*
591 * Avoid a race between exit1() and sigexit(): if the
592 * process is dumping core, then we need to bail out: call
593 * into lwp_userret() where we will be suspended until the
594 * deed is done.
595 */
596 if ((p->p_sflag & PS_WCORE) != 0) {
597 mutex_exit(p->p_lock);
598 lwp_userret(l);
599 KASSERT(false);
600 }
601
602 /*
603 * First off, drain any detached LWP that is waiting to be
604 * reaped.
605 */
606 while ((l2 = p->p_zomblwp) != NULL) {
607 p->p_zomblwp = NULL;
608 lwp_free(l2, false, false);/* releases proc mutex */
609 mutex_enter(p->p_lock);
610 }
611
612 /*
613 * Now look for an LWP to collect. If the whole process is
614 * exiting, count detached LWPs as eligible to be collected,
615 * but don't drain them here.
616 */
617 nfound = 0;
618 error = 0;
619
620 /*
621 * If given a specific LID, go via pid_table and make sure
622 * it's not detached.
623 */
624 if (lid != 0) {
625 l2 = proc_find_lwp(p, lid);
626 if (l2 == NULL) {
627 error = ESRCH;
628 break;
629 }
630 KASSERT(l2->l_lid == lid);
631 if ((l2->l_prflag & LPR_DETACHED) != 0) {
632 error = EINVAL;
633 break;
634 }
635 } else {
636 l2 = LIST_FIRST(&p->p_lwps);
637 }
638 for (; l2 != NULL; l2 = next) {
639 next = (lid != 0 ? NULL : LIST_NEXT(l2, l_sibling));
640
641 /*
642 * If a specific wait and the target is waiting on
643 * us, then avoid deadlock. This also traps LWPs
644 * that try to wait on themselves.
645 *
646 * Note that this does not handle more complicated
647 * cycles, like: t1 -> t2 -> t3 -> t1. The process
648 * can still be killed so it is not a major problem.
649 */
650 if (l2->l_lid == lid && l2->l_waitingfor == curlid) {
651 error = EDEADLK;
652 break;
653 }
654 if (l2 == l)
655 continue;
656 if ((l2->l_prflag & LPR_DETACHED) != 0) {
657 nfound += exiting;
658 continue;
659 }
660 if (lid != 0) {
661 /*
662 * Mark this LWP as the first waiter, if there
663 * is no other.
664 */
665 if (l2->l_waiter == 0)
666 l2->l_waiter = curlid;
667 } else if (l2->l_waiter != 0) {
668 /*
669 * It already has a waiter - so don't
670 * collect it. If the waiter doesn't
671 * grab it we'll get another chance
672 * later.
673 */
674 nfound++;
675 continue;
676 }
677 nfound++;
678
679 /* No need to lock the LWP in order to see LSZOMB. */
680 if (l2->l_stat != LSZOMB)
681 continue;
682
683 /*
684 * We're no longer waiting. Reset the "first waiter"
685 * pointer on the target, in case it was us.
686 */
687 l->l_waitingfor = 0;
688 l2->l_waiter = 0;
689 p->p_nlwpwait--;
690 if (departed)
691 *departed = l2->l_lid;
692 sched_lwp_collect(l2);
693
694 /* lwp_free() releases the proc lock. */
695 lwp_free(l2, false, false);
696 mutex_enter(p->p_lock);
697 return 0;
698 }
699
700 if (error != 0)
701 break;
702 if (nfound == 0) {
703 error = ESRCH;
704 break;
705 }
706
707 /*
708 * Note: since the lock will be dropped, need to restart on
709 * wakeup to run all LWPs again, e.g. there may be new LWPs.
710 */
711 if (exiting) {
712 KASSERT(p->p_nlwps > 1);
713 error = cv_timedwait(&p->p_lwpcv, p->p_lock, 1);
714 break;
715 }
716
717 /*
718 * Break out if all LWPs are in _lwp_wait(). There are
719 * other ways to hang the process with _lwp_wait(), but the
720 * sleep is interruptable so little point checking for them.
721 */
722 if (p->p_nlwpwait == p->p_nlwps) {
723 error = EDEADLK;
724 break;
725 }
726
727 /*
728 * Sit around and wait for something to happen. We'll be
729 * awoken if any of the conditions examined change: if an
730 * LWP exits, is collected, or is detached.
731 */
732 if ((error = cv_wait_sig(&p->p_lwpcv, p->p_lock)) != 0)
733 break;
734 }
735
736 /*
737 * We didn't find any LWPs to collect, we may have received a
738 * signal, or some other condition has caused us to bail out.
739 *
740 * If waiting on a specific LWP, clear the waiters marker: some
741 * other LWP may want it. Then, kick all the remaining waiters
742 * so that they can re-check for zombies and for deadlock.
743 */
744 if (lid != 0) {
745 l2 = proc_find_lwp(p, lid);
746 KASSERT(l2 == NULL || l2->l_lid == lid);
747
748 if (l2 != NULL && l2->l_waiter == curlid)
749 l2->l_waiter = 0;
750 }
751 p->p_nlwpwait--;
752 l->l_waitingfor = 0;
753 cv_broadcast(&p->p_lwpcv);
754
755 return error;
756 }
757
758 /*
759 * Create a new LWP within process 'p2', using LWP 'l1' as a template.
760 * The new LWP is created in state LSIDL and must be set running,
761 * suspended, or stopped by the caller.
762 */
763 int
764 lwp_create(lwp_t *l1, proc_t *p2, vaddr_t uaddr, int flags,
765 void *stack, size_t stacksize, void (*func)(void *), void *arg,
766 lwp_t **rnewlwpp, int sclass, const sigset_t *sigmask,
767 const stack_t *sigstk)
768 {
769 struct lwp *l2;
770
771 KASSERT(l1 == curlwp || l1->l_proc == &proc0);
772
773 /*
774 * Enforce limits, excluding the first lwp and kthreads. We must
775 * use the process credentials here when adjusting the limit, as
776 * they are what's tied to the accounting entity. However for
777 * authorizing the action, we'll use the LWP's credentials.
778 */
779 mutex_enter(p2->p_lock);
780 if (p2->p_nlwps != 0 && p2 != &proc0) {
781 uid_t uid = kauth_cred_getuid(p2->p_cred);
782 int count = chglwpcnt(uid, 1);
783 if (__predict_false(count >
784 p2->p_rlimit[RLIMIT_NTHR].rlim_cur)) {
785 if (kauth_authorize_process(l1->l_cred,
786 KAUTH_PROCESS_RLIMIT, p2,
787 KAUTH_ARG(KAUTH_REQ_PROCESS_RLIMIT_BYPASS),
788 &p2->p_rlimit[RLIMIT_NTHR], KAUTH_ARG(RLIMIT_NTHR))
789 != 0) {
790 (void)chglwpcnt(uid, -1);
791 mutex_exit(p2->p_lock);
792 return EAGAIN;
793 }
794 }
795 }
796
797 /*
798 * First off, reap any detached LWP waiting to be collected.
799 * We can re-use its LWP structure and turnstile.
800 */
801 if ((l2 = p2->p_zomblwp) != NULL) {
802 p2->p_zomblwp = NULL;
803 lwp_free(l2, true, false);
804 /* p2 now unlocked by lwp_free() */
805 KASSERT(l2->l_ts != NULL);
806 KASSERT(l2->l_inheritedprio == -1);
807 KASSERT(SLIST_EMPTY(&l2->l_pi_lenders));
808 memset(&l2->l_startzero, 0, sizeof(*l2) -
809 offsetof(lwp_t, l_startzero));
810 } else {
811 mutex_exit(p2->p_lock);
812 l2 = pool_cache_get(lwp_cache, PR_WAITOK);
813 memset(&l2->l_startzero, 0, sizeof(*l2) -
814 offsetof(lwp_t, l_startzero));
815 SLIST_INIT(&l2->l_pi_lenders);
816 }
817
818 /*
819 * Because of lockless lookup via pid_table, the LWP can be locked
820 * and inspected briefly even after it's freed, so a few fields are
821 * kept stable.
822 */
823 KASSERT(l2->l_stat == LSIDL);
824 KASSERT(l2->l_cpu != NULL);
825 KASSERT(l2->l_ts != NULL);
826 KASSERT(l2->l_mutex == l2->l_cpu->ci_schedstate.spc_lwplock);
827
828 l2->l_proc = p2;
829 l2->l_refcnt = 0;
830 l2->l_class = sclass;
831
832 /*
833 * Allocate a process ID for this LWP. We need to do this now
834 * while we can still unwind if it fails. Beacuse we're marked
835 * as LSIDL, no lookups by the ID will succeed.
836 *
837 * N.B. this will always succeed for the first LWP in a process,
838 * because proc_alloc_lwpid() will usurp the slot. Also note
839 * that l2->l_proc MUST be valid so that lookups of the proc
840 * will succeed, even if the LWP itself is not visible.
841 */
842 if (__predict_false(proc_alloc_lwpid(p2, l2) == -1)) {
843 pool_cache_put(lwp_cache, l2);
844 return EAGAIN;
845 }
846
847 /*
848 * If vfork(), we want the LWP to run fast and on the same CPU
849 * as its parent, so that it can reuse the VM context and cache
850 * footprint on the local CPU.
851 */
852 l2->l_kpriority = ((flags & LWP_VFORK) ? true : false);
853 l2->l_kpribase = PRI_KERNEL;
854 l2->l_priority = l1->l_priority;
855 l2->l_inheritedprio = -1;
856 l2->l_protectprio = -1;
857 l2->l_auxprio = -1;
858 l2->l_flag = 0;
859 l2->l_pflag = LP_MPSAFE;
860 TAILQ_INIT(&l2->l_ld_locks);
861 l2->l_psrefs = 0;
862 kmsan_lwp_alloc(l2);
863
864 /*
865 * For vfork, borrow parent's lwpctl context if it exists.
866 * This also causes us to return via lwp_userret.
867 */
868 if (flags & LWP_VFORK && l1->l_lwpctl) {
869 l2->l_lwpctl = l1->l_lwpctl;
870 l2->l_flag |= LW_LWPCTL;
871 }
872
873 /*
874 * If not the first LWP in the process, grab a reference to the
875 * descriptor table.
876 */
877 l2->l_fd = p2->p_fd;
878 if (p2->p_nlwps != 0) {
879 KASSERT(l1->l_proc == p2);
880 fd_hold(l2);
881 } else {
882 KASSERT(l1->l_proc != p2);
883 }
884
885 if (p2->p_flag & PK_SYSTEM) {
886 /* Mark it as a system LWP. */
887 l2->l_flag |= LW_SYSTEM;
888 }
889
890 kdtrace_thread_ctor(NULL, l2);
891 lwp_initspecific(l2);
892 sched_lwp_fork(l1, l2);
893 lwp_update_creds(l2);
894 callout_init(&l2->l_timeout_ch, CALLOUT_MPSAFE);
895 callout_setfunc(&l2->l_timeout_ch, sleepq_timeout, l2);
896 cv_init(&l2->l_sigcv, "sigwait");
897 cv_init(&l2->l_waitcv, "vfork");
898 l2->l_syncobj = &sched_syncobj;
899 PSREF_DEBUG_INIT_LWP(l2);
900
901 if (rnewlwpp != NULL)
902 *rnewlwpp = l2;
903
904 /*
905 * PCU state needs to be saved before calling uvm_lwp_fork() so that
906 * the MD cpu_lwp_fork() can copy the saved state to the new LWP.
907 */
908 pcu_save_all(l1);
909 #if PCU_UNIT_COUNT > 0
910 l2->l_pcu_valid = l1->l_pcu_valid;
911 #endif
912
913 uvm_lwp_setuarea(l2, uaddr);
914 uvm_lwp_fork(l1, l2, stack, stacksize, func, (arg != NULL) ? arg : l2);
915
916 mutex_enter(p2->p_lock);
917 if ((flags & LWP_DETACHED) != 0) {
918 l2->l_prflag = LPR_DETACHED;
919 p2->p_ndlwps++;
920 } else
921 l2->l_prflag = 0;
922
923 if (l1->l_proc == p2) {
924 /*
925 * These flags are set while p_lock is held. Copy with
926 * p_lock held too, so the LWP doesn't sneak into the
927 * process without them being set.
928 */
929 l2->l_flag |= (l1->l_flag & (LW_WEXIT | LW_WREBOOT | LW_WCORE));
930 } else {
931 /* fork(): pending core/exit doesn't apply to child. */
932 l2->l_flag |= (l1->l_flag & LW_WREBOOT);
933 }
934
935 l2->l_sigstk = *sigstk;
936 l2->l_sigmask = *sigmask;
937 TAILQ_INIT(&l2->l_sigpend.sp_info);
938 sigemptyset(&l2->l_sigpend.sp_set);
939 LIST_INSERT_HEAD(&p2->p_lwps, l2, l_sibling);
940 p2->p_nlwps++;
941 p2->p_nrlwps++;
942
943 KASSERT(l2->l_affinity == NULL);
944
945 /* Inherit the affinity mask. */
946 if (l1->l_affinity) {
947 /*
948 * Note that we hold the state lock while inheriting
949 * the affinity to avoid race with sched_setaffinity().
950 */
951 lwp_lock(l1);
952 if (l1->l_affinity) {
953 kcpuset_use(l1->l_affinity);
954 l2->l_affinity = l1->l_affinity;
955 }
956 lwp_unlock(l1);
957 }
958
959 /* This marks the end of the "must be atomic" section. */
960 mutex_exit(p2->p_lock);
961
962 SDT_PROBE(proc, kernel, , lwp__create, l2, 0, 0, 0, 0);
963
964 mutex_enter(&proc_lock);
965 LIST_INSERT_HEAD(&alllwp, l2, l_list);
966 /* Inherit a processor-set */
967 l2->l_psid = l1->l_psid;
968 mutex_exit(&proc_lock);
969
970 SYSCALL_TIME_LWP_INIT(l2);
971
972 if (p2->p_emul->e_lwp_fork)
973 (*p2->p_emul->e_lwp_fork)(l1, l2);
974
975 return (0);
976 }
977
978 /*
979 * Set a new LWP running. If the process is stopping, then the LWP is
980 * created stopped.
981 */
982 void
983 lwp_start(lwp_t *l, int flags)
984 {
985 proc_t *p = l->l_proc;
986
987 mutex_enter(p->p_lock);
988 lwp_lock(l);
989 KASSERT(l->l_stat == LSIDL);
990 if ((flags & LWP_SUSPENDED) != 0) {
991 /* It'll suspend itself in lwp_userret(). */
992 l->l_flag |= LW_WSUSPEND;
993 }
994 if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
995 KASSERT(l->l_wchan == NULL);
996 l->l_stat = LSSTOP;
997 p->p_nrlwps--;
998 lwp_unlock(l);
999 } else {
1000 setrunnable(l);
1001 /* LWP now unlocked */
1002 }
1003 mutex_exit(p->p_lock);
1004 }
1005
1006 /*
1007 * Called by MD code when a new LWP begins execution. Must be called
1008 * with the previous LWP locked (so at splsched), or if there is no
1009 * previous LWP, at splsched.
1010 */
1011 void
1012 lwp_startup(struct lwp *prev, struct lwp *new_lwp)
1013 {
1014 kmutex_t *lock;
1015
1016 KASSERTMSG(new_lwp == curlwp, "l %p curlwp %p prevlwp %p", new_lwp, curlwp, prev);
1017 KASSERT(kpreempt_disabled());
1018 KASSERT(prev != NULL);
1019 KASSERT((prev->l_pflag & LP_RUNNING) != 0);
1020 KASSERT(curcpu()->ci_mtx_count == -2);
1021
1022 /*
1023 * Immediately mark the previous LWP as no longer running and unlock
1024 * (to keep lock wait times short as possible). If a zombie, don't
1025 * touch after clearing LP_RUNNING as it could be reaped by another
1026 * CPU. Issue a memory barrier to ensure this.
1027 */
1028 lock = prev->l_mutex;
1029 if (__predict_false(prev->l_stat == LSZOMB)) {
1030 membar_sync();
1031 }
1032 prev->l_pflag &= ~LP_RUNNING;
1033 mutex_spin_exit(lock);
1034
1035 /* Correct spin mutex count after mi_switch(). */
1036 curcpu()->ci_mtx_count = 0;
1037
1038 /* Install new VM context. */
1039 if (__predict_true(new_lwp->l_proc->p_vmspace)) {
1040 pmap_activate(new_lwp);
1041 }
1042
1043 /* We remain at IPL_SCHED from mi_switch() - reset it. */
1044 spl0();
1045
1046 LOCKDEBUG_BARRIER(NULL, 0);
1047 SDT_PROBE(proc, kernel, , lwp__start, new_lwp, 0, 0, 0, 0);
1048
1049 /* For kthreads, acquire kernel lock if not MPSAFE. */
1050 if (__predict_false((new_lwp->l_pflag & LP_MPSAFE) == 0)) {
1051 KERNEL_LOCK(1, new_lwp);
1052 }
1053 }
1054
1055 /*
1056 * Exit an LWP.
1057 */
1058 void
1059 lwp_exit(struct lwp *l)
1060 {
1061 struct proc *p = l->l_proc;
1062 struct lwp *l2;
1063 bool current;
1064
1065 current = (l == curlwp);
1066
1067 KASSERT(current || (l->l_stat == LSIDL && l->l_target_cpu == NULL));
1068 KASSERT(p == curproc);
1069
1070 SDT_PROBE(proc, kernel, , lwp__exit, l, 0, 0, 0, 0);
1071
1072 /* Verify that we hold no locks; for DIAGNOSTIC check kernel_lock. */
1073 LOCKDEBUG_BARRIER(NULL, 0);
1074 KASSERTMSG(curcpu()->ci_biglock_count == 0, "kernel_lock leaked");
1075
1076 /*
1077 * If we are the last live LWP in a process, we need to exit the
1078 * entire process. We do so with an exit status of zero, because
1079 * it's a "controlled" exit, and because that's what Solaris does.
1080 *
1081 * We are not quite a zombie yet, but for accounting purposes we
1082 * must increment the count of zombies here.
1083 *
1084 * Note: the last LWP's specificdata will be deleted here.
1085 */
1086 mutex_enter(p->p_lock);
1087 if (p->p_nlwps - p->p_nzlwps == 1) {
1088 KASSERT(current == true);
1089 KASSERT(p != &proc0);
1090 exit1(l, 0, 0);
1091 /* NOTREACHED */
1092 }
1093 p->p_nzlwps++;
1094
1095 /*
1096 * Perform any required thread cleanup. Do this early so
1097 * anyone wanting to look us up with lwp_getref_lwpid() will
1098 * fail to find us before we become a zombie.
1099 *
1100 * N.B. this will unlock p->p_lock on our behalf.
1101 */
1102 lwp_thread_cleanup(l);
1103
1104 if (p->p_emul->e_lwp_exit)
1105 (*p->p_emul->e_lwp_exit)(l);
1106
1107 /* Drop filedesc reference. */
1108 fd_free();
1109
1110 /* Release fstrans private data. */
1111 fstrans_lwp_dtor(l);
1112
1113 /* Delete the specificdata while it's still safe to sleep. */
1114 lwp_finispecific(l);
1115
1116 /*
1117 * Release our cached credentials.
1118 */
1119 kauth_cred_free(l->l_cred);
1120 callout_destroy(&l->l_timeout_ch);
1121
1122 /*
1123 * If traced, report LWP exit event to the debugger.
1124 *
1125 * Remove the LWP from the global list.
1126 * Free its LID from the PID namespace if needed.
1127 */
1128 mutex_enter(&proc_lock);
1129
1130 if ((p->p_slflag & (PSL_TRACED|PSL_TRACELWP_EXIT)) ==
1131 (PSL_TRACED|PSL_TRACELWP_EXIT)) {
1132 mutex_enter(p->p_lock);
1133 if (ISSET(p->p_sflag, PS_WEXIT)) {
1134 mutex_exit(p->p_lock);
1135 /*
1136 * We are exiting, bail out without informing parent
1137 * about a terminating LWP as it would deadlock.
1138 */
1139 } else {
1140 eventswitch(TRAP_LWP, PTRACE_LWP_EXIT, l->l_lid);
1141 mutex_enter(&proc_lock);
1142 }
1143 }
1144
1145 LIST_REMOVE(l, l_list);
1146 mutex_exit(&proc_lock);
1147
1148 /*
1149 * Get rid of all references to the LWP that others (e.g. procfs)
1150 * may have, and mark the LWP as a zombie. If the LWP is detached,
1151 * mark it waiting for collection in the proc structure. Note that
1152 * before we can do that, we need to free any other dead, deatched
1153 * LWP waiting to meet its maker.
1154 *
1155 * All conditions need to be observed upon under the same hold of
1156 * p_lock, because if the lock is dropped any of them can change.
1157 */
1158 mutex_enter(p->p_lock);
1159 for (;;) {
1160 if (lwp_drainrefs(l))
1161 continue;
1162 if ((l->l_prflag & LPR_DETACHED) != 0) {
1163 if ((l2 = p->p_zomblwp) != NULL) {
1164 p->p_zomblwp = NULL;
1165 lwp_free(l2, false, false);
1166 /* proc now unlocked */
1167 mutex_enter(p->p_lock);
1168 continue;
1169 }
1170 p->p_zomblwp = l;
1171 }
1172 break;
1173 }
1174
1175 /*
1176 * If we find a pending signal for the process and we have been
1177 * asked to check for signals, then we lose: arrange to have
1178 * all other LWPs in the process check for signals.
1179 */
1180 if ((l->l_flag & LW_PENDSIG) != 0 &&
1181 firstsig(&p->p_sigpend.sp_set) != 0) {
1182 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
1183 lwp_lock(l2);
1184 signotify(l2);
1185 lwp_unlock(l2);
1186 }
1187 }
1188
1189 /*
1190 * Release any PCU resources before becoming a zombie.
1191 */
1192 pcu_discard_all(l);
1193
1194 lwp_lock(l);
1195 l->l_stat = LSZOMB;
1196 if (l->l_name != NULL) {
1197 strcpy(l->l_name, "(zombie)");
1198 }
1199 lwp_unlock(l);
1200 p->p_nrlwps--;
1201 cv_broadcast(&p->p_lwpcv);
1202 if (l->l_lwpctl != NULL)
1203 l->l_lwpctl->lc_curcpu = LWPCTL_CPU_EXITED;
1204 mutex_exit(p->p_lock);
1205
1206 /*
1207 * We can no longer block. At this point, lwp_free() may already
1208 * be gunning for us. On a multi-CPU system, we may be off p_lwps.
1209 *
1210 * Free MD LWP resources.
1211 */
1212 cpu_lwp_free(l, 0);
1213
1214 if (current) {
1215 /* Switch away into oblivion. */
1216 lwp_lock(l);
1217 spc_lock(l->l_cpu);
1218 mi_switch(l);
1219 panic("lwp_exit");
1220 }
1221 }
1222
1223 /*
1224 * Free a dead LWP's remaining resources.
1225 *
1226 * XXXLWP limits.
1227 */
1228 void
1229 lwp_free(struct lwp *l, bool recycle, bool last)
1230 {
1231 struct proc *p = l->l_proc;
1232 struct rusage *ru;
1233 ksiginfoq_t kq;
1234
1235 KASSERT(l != curlwp);
1236 KASSERT(last || mutex_owned(p->p_lock));
1237
1238 /*
1239 * We use the process credentials instead of the lwp credentials here
1240 * because the lwp credentials maybe cached (just after a setuid call)
1241 * and we don't want pay for syncing, since the lwp is going away
1242 * anyway
1243 */
1244 if (p != &proc0 && p->p_nlwps != 1)
1245 (void)chglwpcnt(kauth_cred_getuid(p->p_cred), -1);
1246
1247 /*
1248 * In the unlikely event that the LWP is still on the CPU,
1249 * then spin until it has switched away.
1250 */
1251 membar_consumer();
1252 while (__predict_false((l->l_pflag & LP_RUNNING) != 0)) {
1253 SPINLOCK_BACKOFF_HOOK;
1254 }
1255
1256 /*
1257 * Now that the LWP's known off the CPU, reset its state back to
1258 * LSIDL, which defeats anything that might have gotten a hold on
1259 * the LWP via pid_table before the ID was freed. It's important
1260 * to do this with both the LWP locked and p_lock held.
1261 *
1262 * Also reset the CPU and lock pointer back to curcpu(), since the
1263 * LWP will in all likelyhood be cached with the current CPU in
1264 * lwp_cache when we free it and later allocated from there again
1265 * (avoid incidental lock contention).
1266 */
1267 lwp_lock(l);
1268 l->l_stat = LSIDL;
1269 l->l_cpu = curcpu();
1270 lwp_unlock_to(l, l->l_cpu->ci_schedstate.spc_lwplock);
1271
1272 /*
1273 * If this was not the last LWP in the process, then adjust counters
1274 * and unlock. This is done differently for the last LWP in exit1().
1275 */
1276 if (!last) {
1277 /*
1278 * Add the LWP's run time to the process' base value.
1279 * This needs to co-incide with coming off p_lwps.
1280 */
1281 bintime_add(&p->p_rtime, &l->l_rtime);
1282 p->p_pctcpu += l->l_pctcpu;
1283 ru = &p->p_stats->p_ru;
1284 ruadd(ru, &l->l_ru);
1285 ru->ru_nvcsw += (l->l_ncsw - l->l_nivcsw);
1286 ru->ru_nivcsw += l->l_nivcsw;
1287 LIST_REMOVE(l, l_sibling);
1288 p->p_nlwps--;
1289 p->p_nzlwps--;
1290 if ((l->l_prflag & LPR_DETACHED) != 0)
1291 p->p_ndlwps--;
1292
1293 /*
1294 * Have any LWPs sleeping in lwp_wait() recheck for
1295 * deadlock.
1296 */
1297 cv_broadcast(&p->p_lwpcv);
1298 mutex_exit(p->p_lock);
1299
1300 /* Free the LWP ID. */
1301 mutex_enter(&proc_lock);
1302 proc_free_lwpid(p, l->l_lid);
1303 mutex_exit(&proc_lock);
1304 }
1305
1306 /*
1307 * Destroy the LWP's remaining signal information.
1308 */
1309 ksiginfo_queue_init(&kq);
1310 sigclear(&l->l_sigpend, NULL, &kq);
1311 ksiginfo_queue_drain(&kq);
1312 cv_destroy(&l->l_sigcv);
1313 cv_destroy(&l->l_waitcv);
1314
1315 /*
1316 * Free lwpctl structure and affinity.
1317 */
1318 if (l->l_lwpctl) {
1319 lwp_ctl_free(l);
1320 }
1321 if (l->l_affinity) {
1322 kcpuset_unuse(l->l_affinity, NULL);
1323 l->l_affinity = NULL;
1324 }
1325
1326 /*
1327 * Free remaining data structures and the LWP itself unless the
1328 * caller wants to recycle.
1329 */
1330 if (l->l_name != NULL)
1331 kmem_free(l->l_name, MAXCOMLEN);
1332
1333 kmsan_lwp_free(l);
1334 kcov_lwp_free(l);
1335 cpu_lwp_free2(l);
1336 uvm_lwp_exit(l);
1337
1338 KASSERT(SLIST_EMPTY(&l->l_pi_lenders));
1339 KASSERT(l->l_inheritedprio == -1);
1340 KASSERT(l->l_blcnt == 0);
1341 kdtrace_thread_dtor(NULL, l);
1342 if (!recycle)
1343 pool_cache_put(lwp_cache, l);
1344 }
1345
1346 /*
1347 * Migrate the LWP to the another CPU. Unlocks the LWP.
1348 */
1349 void
1350 lwp_migrate(lwp_t *l, struct cpu_info *tci)
1351 {
1352 struct schedstate_percpu *tspc;
1353 int lstat = l->l_stat;
1354
1355 KASSERT(lwp_locked(l, NULL));
1356 KASSERT(tci != NULL);
1357
1358 /* If LWP is still on the CPU, it must be handled like LSONPROC */
1359 if ((l->l_pflag & LP_RUNNING) != 0) {
1360 lstat = LSONPROC;
1361 }
1362
1363 /*
1364 * The destination CPU could be changed while previous migration
1365 * was not finished.
1366 */
1367 if (l->l_target_cpu != NULL) {
1368 l->l_target_cpu = tci;
1369 lwp_unlock(l);
1370 return;
1371 }
1372
1373 /* Nothing to do if trying to migrate to the same CPU */
1374 if (l->l_cpu == tci) {
1375 lwp_unlock(l);
1376 return;
1377 }
1378
1379 KASSERT(l->l_target_cpu == NULL);
1380 tspc = &tci->ci_schedstate;
1381 switch (lstat) {
1382 case LSRUN:
1383 l->l_target_cpu = tci;
1384 break;
1385 case LSSLEEP:
1386 l->l_cpu = tci;
1387 break;
1388 case LSIDL:
1389 case LSSTOP:
1390 case LSSUSPENDED:
1391 l->l_cpu = tci;
1392 if (l->l_wchan == NULL) {
1393 lwp_unlock_to(l, tspc->spc_lwplock);
1394 return;
1395 }
1396 break;
1397 case LSONPROC:
1398 l->l_target_cpu = tci;
1399 spc_lock(l->l_cpu);
1400 sched_resched_cpu(l->l_cpu, PRI_USER_RT, true);
1401 /* spc now unlocked */
1402 break;
1403 }
1404 lwp_unlock(l);
1405 }
1406
1407 #define lwp_find_exclude(l) \
1408 ((l)->l_stat == LSIDL || (l)->l_stat == LSZOMB)
1409
1410 /*
1411 * Find the LWP in the process. Arguments may be zero, in such case,
1412 * the calling process and first LWP in the list will be used.
1413 * On success - returns proc locked.
1414 *
1415 * => pid == 0 -> look in curproc.
1416 * => pid == -1 -> match any proc.
1417 * => otherwise look up the proc.
1418 *
1419 * => lid == 0 -> first LWP in the proc
1420 * => otherwise specific LWP
1421 */
1422 struct lwp *
1423 lwp_find2(pid_t pid, lwpid_t lid)
1424 {
1425 proc_t *p;
1426 lwp_t *l;
1427
1428 /* First LWP of specified proc. */
1429 if (lid == 0) {
1430 switch (pid) {
1431 case -1:
1432 /* No lookup keys. */
1433 return NULL;
1434 case 0:
1435 p = curproc;
1436 mutex_enter(p->p_lock);
1437 break;
1438 default:
1439 mutex_enter(&proc_lock);
1440 p = proc_find(pid);
1441 if (__predict_false(p == NULL)) {
1442 mutex_exit(&proc_lock);
1443 return NULL;
1444 }
1445 mutex_enter(p->p_lock);
1446 mutex_exit(&proc_lock);
1447 break;
1448 }
1449 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1450 if (__predict_true(!lwp_find_exclude(l)))
1451 break;
1452 }
1453 goto out;
1454 }
1455
1456 l = proc_find_lwp_acquire_proc(lid, &p);
1457 if (l == NULL)
1458 return NULL;
1459 KASSERT(p != NULL);
1460 KASSERT(mutex_owned(p->p_lock));
1461
1462 if (__predict_false(lwp_find_exclude(l))) {
1463 l = NULL;
1464 goto out;
1465 }
1466
1467 /* Apply proc filter, if applicable. */
1468 switch (pid) {
1469 case -1:
1470 /* Match anything. */
1471 break;
1472 case 0:
1473 if (p != curproc)
1474 l = NULL;
1475 break;
1476 default:
1477 if (p->p_pid != pid)
1478 l = NULL;
1479 break;
1480 }
1481
1482 out:
1483 if (__predict_false(l == NULL)) {
1484 mutex_exit(p->p_lock);
1485 }
1486 return l;
1487 }
1488
1489 /*
1490 * Look up a live LWP within the specified process.
1491 *
1492 * Must be called with p->p_lock held (as it looks at the radix tree,
1493 * and also wants to exclude idle and zombie LWPs).
1494 */
1495 struct lwp *
1496 lwp_find(struct proc *p, lwpid_t id)
1497 {
1498 struct lwp *l;
1499
1500 KASSERT(mutex_owned(p->p_lock));
1501
1502 l = proc_find_lwp(p, id);
1503 KASSERT(l == NULL || l->l_lid == id);
1504
1505 /*
1506 * No need to lock - all of these conditions will
1507 * be visible with the process level mutex held.
1508 */
1509 if (__predict_false(l != NULL && lwp_find_exclude(l)))
1510 l = NULL;
1511
1512 return l;
1513 }
1514
1515 /*
1516 * Update an LWP's cached credentials to mirror the process' master copy.
1517 *
1518 * This happens early in the syscall path, on user trap, and on LWP
1519 * creation. A long-running LWP can also voluntarily choose to update
1520 * its credentials by calling this routine. This may be called from
1521 * LWP_CACHE_CREDS(), which checks l->l_cred != p->p_cred beforehand.
1522 */
1523 void
1524 lwp_update_creds(struct lwp *l)
1525 {
1526 kauth_cred_t oc;
1527 struct proc *p;
1528
1529 p = l->l_proc;
1530 oc = l->l_cred;
1531
1532 mutex_enter(p->p_lock);
1533 kauth_cred_hold(p->p_cred);
1534 l->l_cred = p->p_cred;
1535 l->l_prflag &= ~LPR_CRMOD;
1536 mutex_exit(p->p_lock);
1537 if (oc != NULL)
1538 kauth_cred_free(oc);
1539 }
1540
1541 /*
1542 * Verify that an LWP is locked, and optionally verify that the lock matches
1543 * one we specify.
1544 */
1545 int
1546 lwp_locked(struct lwp *l, kmutex_t *mtx)
1547 {
1548 kmutex_t *cur = l->l_mutex;
1549
1550 return mutex_owned(cur) && (mtx == cur || mtx == NULL);
1551 }
1552
1553 /*
1554 * Lend a new mutex to an LWP. The old mutex must be held.
1555 */
1556 kmutex_t *
1557 lwp_setlock(struct lwp *l, kmutex_t *mtx)
1558 {
1559 kmutex_t *oldmtx = l->l_mutex;
1560
1561 KASSERT(mutex_owned(oldmtx));
1562
1563 membar_exit();
1564 l->l_mutex = mtx;
1565 return oldmtx;
1566 }
1567
1568 /*
1569 * Lend a new mutex to an LWP, and release the old mutex. The old mutex
1570 * must be held.
1571 */
1572 void
1573 lwp_unlock_to(struct lwp *l, kmutex_t *mtx)
1574 {
1575 kmutex_t *old;
1576
1577 KASSERT(lwp_locked(l, NULL));
1578
1579 old = l->l_mutex;
1580 membar_exit();
1581 l->l_mutex = mtx;
1582 mutex_spin_exit(old);
1583 }
1584
1585 int
1586 lwp_trylock(struct lwp *l)
1587 {
1588 kmutex_t *old;
1589
1590 for (;;) {
1591 if (!mutex_tryenter(old = l->l_mutex))
1592 return 0;
1593 if (__predict_true(l->l_mutex == old))
1594 return 1;
1595 mutex_spin_exit(old);
1596 }
1597 }
1598
1599 void
1600 lwp_unsleep(lwp_t *l, bool unlock)
1601 {
1602
1603 KASSERT(mutex_owned(l->l_mutex));
1604 (*l->l_syncobj->sobj_unsleep)(l, unlock);
1605 }
1606
1607 /*
1608 * Handle exceptions for mi_userret(). Called if a member of LW_USERRET is
1609 * set.
1610 */
1611 void
1612 lwp_userret(struct lwp *l)
1613 {
1614 struct proc *p;
1615 int sig;
1616
1617 KASSERT(l == curlwp);
1618 KASSERT(l->l_stat == LSONPROC);
1619 p = l->l_proc;
1620
1621 /*
1622 * It is safe to do this read unlocked on a MP system..
1623 */
1624 while ((l->l_flag & LW_USERRET) != 0) {
1625 /*
1626 * Process pending signals first, unless the process
1627 * is dumping core or exiting, where we will instead
1628 * enter the LW_WSUSPEND case below.
1629 */
1630 if ((l->l_flag & (LW_PENDSIG | LW_WCORE | LW_WEXIT)) ==
1631 LW_PENDSIG) {
1632 mutex_enter(p->p_lock);
1633 while ((sig = issignal(l)) != 0)
1634 postsig(sig);
1635 mutex_exit(p->p_lock);
1636 }
1637
1638 /*
1639 * Core-dump or suspend pending.
1640 *
1641 * In case of core dump, suspend ourselves, so that the kernel
1642 * stack and therefore the userland registers saved in the
1643 * trapframe are around for coredump() to write them out.
1644 * We also need to save any PCU resources that we have so that
1645 * they accessible for coredump(). We issue a wakeup on
1646 * p->p_lwpcv so that sigexit() will write the core file out
1647 * once all other LWPs are suspended.
1648 */
1649 if ((l->l_flag & LW_WSUSPEND) != 0) {
1650 pcu_save_all(l);
1651 mutex_enter(p->p_lock);
1652 p->p_nrlwps--;
1653 cv_broadcast(&p->p_lwpcv);
1654 lwp_lock(l);
1655 l->l_stat = LSSUSPENDED;
1656 lwp_unlock(l);
1657 mutex_exit(p->p_lock);
1658 lwp_lock(l);
1659 spc_lock(l->l_cpu);
1660 mi_switch(l);
1661 }
1662
1663 /* Process is exiting. */
1664 if ((l->l_flag & LW_WEXIT) != 0) {
1665 lwp_exit(l);
1666 KASSERT(0);
1667 /* NOTREACHED */
1668 }
1669
1670 /* update lwpctl processor (for vfork child_return) */
1671 if (l->l_flag & LW_LWPCTL) {
1672 lwp_lock(l);
1673 KASSERT(kpreempt_disabled());
1674 l->l_lwpctl->lc_curcpu = (int)cpu_index(l->l_cpu);
1675 l->l_lwpctl->lc_pctr++;
1676 l->l_flag &= ~LW_LWPCTL;
1677 lwp_unlock(l);
1678 }
1679 }
1680 }
1681
1682 /*
1683 * Force an LWP to enter the kernel, to take a trip through lwp_userret().
1684 */
1685 void
1686 lwp_need_userret(struct lwp *l)
1687 {
1688
1689 KASSERT(!cpu_intr_p());
1690 KASSERT(lwp_locked(l, NULL));
1691
1692 /*
1693 * If the LWP is in any state other than LSONPROC, we know that it
1694 * is executing in-kernel and will hit userret() on the way out.
1695 *
1696 * If the LWP is curlwp, then we know we'll be back out to userspace
1697 * soon (can't be called from a hardware interrupt here).
1698 *
1699 * Otherwise, we can't be sure what the LWP is doing, so first make
1700 * sure the update to l_flag will be globally visible, and then
1701 * force the LWP to take a trip through trap() where it will do
1702 * userret().
1703 */
1704 if (l->l_stat == LSONPROC && l != curlwp) {
1705 membar_producer();
1706 cpu_signotify(l);
1707 }
1708 }
1709
1710 /*
1711 * Add one reference to an LWP. This will prevent the LWP from
1712 * exiting, thus keep the lwp structure and PCB around to inspect.
1713 */
1714 void
1715 lwp_addref(struct lwp *l)
1716 {
1717 KASSERT(mutex_owned(l->l_proc->p_lock));
1718 KASSERT(l->l_stat != LSZOMB);
1719 l->l_refcnt++;
1720 }
1721
1722 /*
1723 * Remove one reference to an LWP. If this is the last reference,
1724 * then we must finalize the LWP's death.
1725 */
1726 void
1727 lwp_delref(struct lwp *l)
1728 {
1729 struct proc *p = l->l_proc;
1730
1731 mutex_enter(p->p_lock);
1732 lwp_delref2(l);
1733 mutex_exit(p->p_lock);
1734 }
1735
1736 /*
1737 * Remove one reference to an LWP. If this is the last reference,
1738 * then we must finalize the LWP's death. The proc mutex is held
1739 * on entry.
1740 */
1741 void
1742 lwp_delref2(struct lwp *l)
1743 {
1744 struct proc *p = l->l_proc;
1745
1746 KASSERT(mutex_owned(p->p_lock));
1747 KASSERT(l->l_stat != LSZOMB);
1748 KASSERT(l->l_refcnt > 0);
1749
1750 if (--l->l_refcnt == 0)
1751 cv_broadcast(&p->p_lwpcv);
1752 }
1753
1754 /*
1755 * Drain all references to the current LWP. Returns true if
1756 * we blocked.
1757 */
1758 bool
1759 lwp_drainrefs(struct lwp *l)
1760 {
1761 struct proc *p = l->l_proc;
1762 bool rv = false;
1763
1764 KASSERT(mutex_owned(p->p_lock));
1765
1766 l->l_prflag |= LPR_DRAINING;
1767
1768 while (l->l_refcnt > 0) {
1769 rv = true;
1770 cv_wait(&p->p_lwpcv, p->p_lock);
1771 }
1772 return rv;
1773 }
1774
1775 /*
1776 * Return true if the specified LWP is 'alive'. Only p->p_lock need
1777 * be held.
1778 */
1779 bool
1780 lwp_alive(lwp_t *l)
1781 {
1782
1783 KASSERT(mutex_owned(l->l_proc->p_lock));
1784
1785 switch (l->l_stat) {
1786 case LSSLEEP:
1787 case LSRUN:
1788 case LSONPROC:
1789 case LSSTOP:
1790 case LSSUSPENDED:
1791 return true;
1792 default:
1793 return false;
1794 }
1795 }
1796
1797 /*
1798 * Return first live LWP in the process.
1799 */
1800 lwp_t *
1801 lwp_find_first(proc_t *p)
1802 {
1803 lwp_t *l;
1804
1805 KASSERT(mutex_owned(p->p_lock));
1806
1807 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1808 if (lwp_alive(l)) {
1809 return l;
1810 }
1811 }
1812
1813 return NULL;
1814 }
1815
1816 /*
1817 * Allocate a new lwpctl structure for a user LWP.
1818 */
1819 int
1820 lwp_ctl_alloc(vaddr_t *uaddr)
1821 {
1822 lcproc_t *lp;
1823 u_int bit, i, offset;
1824 struct uvm_object *uao;
1825 int error;
1826 lcpage_t *lcp;
1827 proc_t *p;
1828 lwp_t *l;
1829
1830 l = curlwp;
1831 p = l->l_proc;
1832
1833 /* don't allow a vforked process to create lwp ctls */
1834 if (p->p_lflag & PL_PPWAIT)
1835 return EBUSY;
1836
1837 if (l->l_lcpage != NULL) {
1838 lcp = l->l_lcpage;
1839 *uaddr = lcp->lcp_uaddr + (vaddr_t)l->l_lwpctl - lcp->lcp_kaddr;
1840 return 0;
1841 }
1842
1843 /* First time around, allocate header structure for the process. */
1844 if ((lp = p->p_lwpctl) == NULL) {
1845 lp = kmem_alloc(sizeof(*lp), KM_SLEEP);
1846 mutex_init(&lp->lp_lock, MUTEX_DEFAULT, IPL_NONE);
1847 lp->lp_uao = NULL;
1848 TAILQ_INIT(&lp->lp_pages);
1849 mutex_enter(p->p_lock);
1850 if (p->p_lwpctl == NULL) {
1851 p->p_lwpctl = lp;
1852 mutex_exit(p->p_lock);
1853 } else {
1854 mutex_exit(p->p_lock);
1855 mutex_destroy(&lp->lp_lock);
1856 kmem_free(lp, sizeof(*lp));
1857 lp = p->p_lwpctl;
1858 }
1859 }
1860
1861 /*
1862 * Set up an anonymous memory region to hold the shared pages.
1863 * Map them into the process' address space. The user vmspace
1864 * gets the first reference on the UAO.
1865 */
1866 mutex_enter(&lp->lp_lock);
1867 if (lp->lp_uao == NULL) {
1868 lp->lp_uao = uao_create(LWPCTL_UAREA_SZ, 0);
1869 lp->lp_cur = 0;
1870 lp->lp_max = LWPCTL_UAREA_SZ;
1871 lp->lp_uva = p->p_emul->e_vm_default_addr(p,
1872 (vaddr_t)p->p_vmspace->vm_daddr, LWPCTL_UAREA_SZ,
1873 p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN);
1874 error = uvm_map(&p->p_vmspace->vm_map, &lp->lp_uva,
1875 LWPCTL_UAREA_SZ, lp->lp_uao, 0, 0, UVM_MAPFLAG(UVM_PROT_RW,
1876 UVM_PROT_RW, UVM_INH_NONE, UVM_ADV_NORMAL, 0));
1877 if (error != 0) {
1878 uao_detach(lp->lp_uao);
1879 lp->lp_uao = NULL;
1880 mutex_exit(&lp->lp_lock);
1881 return error;
1882 }
1883 }
1884
1885 /* Get a free block and allocate for this LWP. */
1886 TAILQ_FOREACH(lcp, &lp->lp_pages, lcp_chain) {
1887 if (lcp->lcp_nfree != 0)
1888 break;
1889 }
1890 if (lcp == NULL) {
1891 /* Nothing available - try to set up a free page. */
1892 if (lp->lp_cur == lp->lp_max) {
1893 mutex_exit(&lp->lp_lock);
1894 return ENOMEM;
1895 }
1896 lcp = kmem_alloc(LWPCTL_LCPAGE_SZ, KM_SLEEP);
1897
1898 /*
1899 * Wire the next page down in kernel space. Since this
1900 * is a new mapping, we must add a reference.
1901 */
1902 uao = lp->lp_uao;
1903 (*uao->pgops->pgo_reference)(uao);
1904 lcp->lcp_kaddr = vm_map_min(kernel_map);
1905 error = uvm_map(kernel_map, &lcp->lcp_kaddr, PAGE_SIZE,
1906 uao, lp->lp_cur, PAGE_SIZE,
1907 UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW,
1908 UVM_INH_NONE, UVM_ADV_RANDOM, 0));
1909 if (error != 0) {
1910 mutex_exit(&lp->lp_lock);
1911 kmem_free(lcp, LWPCTL_LCPAGE_SZ);
1912 (*uao->pgops->pgo_detach)(uao);
1913 return error;
1914 }
1915 error = uvm_map_pageable(kernel_map, lcp->lcp_kaddr,
1916 lcp->lcp_kaddr + PAGE_SIZE, FALSE, 0);
1917 if (error != 0) {
1918 mutex_exit(&lp->lp_lock);
1919 uvm_unmap(kernel_map, lcp->lcp_kaddr,
1920 lcp->lcp_kaddr + PAGE_SIZE);
1921 kmem_free(lcp, LWPCTL_LCPAGE_SZ);
1922 return error;
1923 }
1924 /* Prepare the page descriptor and link into the list. */
1925 lcp->lcp_uaddr = lp->lp_uva + lp->lp_cur;
1926 lp->lp_cur += PAGE_SIZE;
1927 lcp->lcp_nfree = LWPCTL_PER_PAGE;
1928 lcp->lcp_rotor = 0;
1929 memset(lcp->lcp_bitmap, 0xff, LWPCTL_BITMAP_SZ);
1930 TAILQ_INSERT_HEAD(&lp->lp_pages, lcp, lcp_chain);
1931 }
1932 for (i = lcp->lcp_rotor; lcp->lcp_bitmap[i] == 0;) {
1933 if (++i >= LWPCTL_BITMAP_ENTRIES)
1934 i = 0;
1935 }
1936 bit = ffs(lcp->lcp_bitmap[i]) - 1;
1937 lcp->lcp_bitmap[i] ^= (1U << bit);
1938 lcp->lcp_rotor = i;
1939 lcp->lcp_nfree--;
1940 l->l_lcpage = lcp;
1941 offset = (i << 5) + bit;
1942 l->l_lwpctl = (lwpctl_t *)lcp->lcp_kaddr + offset;
1943 *uaddr = lcp->lcp_uaddr + offset * sizeof(lwpctl_t);
1944 mutex_exit(&lp->lp_lock);
1945
1946 KPREEMPT_DISABLE(l);
1947 l->l_lwpctl->lc_curcpu = (int)cpu_index(curcpu());
1948 KPREEMPT_ENABLE(l);
1949
1950 return 0;
1951 }
1952
1953 /*
1954 * Free an lwpctl structure back to the per-process list.
1955 */
1956 void
1957 lwp_ctl_free(lwp_t *l)
1958 {
1959 struct proc *p = l->l_proc;
1960 lcproc_t *lp;
1961 lcpage_t *lcp;
1962 u_int map, offset;
1963
1964 /* don't free a lwp context we borrowed for vfork */
1965 if (p->p_lflag & PL_PPWAIT) {
1966 l->l_lwpctl = NULL;
1967 return;
1968 }
1969
1970 lp = p->p_lwpctl;
1971 KASSERT(lp != NULL);
1972
1973 lcp = l->l_lcpage;
1974 offset = (u_int)((lwpctl_t *)l->l_lwpctl - (lwpctl_t *)lcp->lcp_kaddr);
1975 KASSERT(offset < LWPCTL_PER_PAGE);
1976
1977 mutex_enter(&lp->lp_lock);
1978 lcp->lcp_nfree++;
1979 map = offset >> 5;
1980 lcp->lcp_bitmap[map] |= (1U << (offset & 31));
1981 if (lcp->lcp_bitmap[lcp->lcp_rotor] == 0)
1982 lcp->lcp_rotor = map;
1983 if (TAILQ_FIRST(&lp->lp_pages)->lcp_nfree == 0) {
1984 TAILQ_REMOVE(&lp->lp_pages, lcp, lcp_chain);
1985 TAILQ_INSERT_HEAD(&lp->lp_pages, lcp, lcp_chain);
1986 }
1987 mutex_exit(&lp->lp_lock);
1988 }
1989
1990 /*
1991 * Process is exiting; tear down lwpctl state. This can only be safely
1992 * called by the last LWP in the process.
1993 */
1994 void
1995 lwp_ctl_exit(void)
1996 {
1997 lcpage_t *lcp, *next;
1998 lcproc_t *lp;
1999 proc_t *p;
2000 lwp_t *l;
2001
2002 l = curlwp;
2003 l->l_lwpctl = NULL;
2004 l->l_lcpage = NULL;
2005 p = l->l_proc;
2006 lp = p->p_lwpctl;
2007
2008 KASSERT(lp != NULL);
2009 KASSERT(p->p_nlwps == 1);
2010
2011 for (lcp = TAILQ_FIRST(&lp->lp_pages); lcp != NULL; lcp = next) {
2012 next = TAILQ_NEXT(lcp, lcp_chain);
2013 uvm_unmap(kernel_map, lcp->lcp_kaddr,
2014 lcp->lcp_kaddr + PAGE_SIZE);
2015 kmem_free(lcp, LWPCTL_LCPAGE_SZ);
2016 }
2017
2018 if (lp->lp_uao != NULL) {
2019 uvm_unmap(&p->p_vmspace->vm_map, lp->lp_uva,
2020 lp->lp_uva + LWPCTL_UAREA_SZ);
2021 }
2022
2023 mutex_destroy(&lp->lp_lock);
2024 kmem_free(lp, sizeof(*lp));
2025 p->p_lwpctl = NULL;
2026 }
2027
2028 /*
2029 * Return the current LWP's "preemption counter". Used to detect
2030 * preemption across operations that can tolerate preemption without
2031 * crashing, but which may generate incorrect results if preempted.
2032 */
2033 uint64_t
2034 lwp_pctr(void)
2035 {
2036
2037 return curlwp->l_ncsw;
2038 }
2039
2040 /*
2041 * Set an LWP's private data pointer.
2042 */
2043 int
2044 lwp_setprivate(struct lwp *l, void *ptr)
2045 {
2046 int error = 0;
2047
2048 l->l_private = ptr;
2049 #ifdef __HAVE_CPU_LWP_SETPRIVATE
2050 error = cpu_lwp_setprivate(l, ptr);
2051 #endif
2052 return error;
2053 }
2054
2055 /*
2056 * Perform any thread-related cleanup on LWP exit.
2057 * N.B. l->l_proc->p_lock must be HELD on entry but will
2058 * be released before returning!
2059 */
2060 void
2061 lwp_thread_cleanup(struct lwp *l)
2062 {
2063 KASSERT(l == curlwp);
2064 const lwpid_t tid = l->l_lid;
2065
2066 KASSERT((tid & FUTEX_TID_MASK) == tid);
2067 KASSERT(mutex_owned(l->l_proc->p_lock));
2068
2069 mutex_exit(l->l_proc->p_lock);
2070
2071 /*
2072 * If the LWP has robust futexes, release them all
2073 * now.
2074 */
2075 if (__predict_false(l->l_robust_head != 0)) {
2076 futex_release_all_lwp(l, tid);
2077 }
2078 }
2079
2080 #if defined(DDB)
2081 #include <machine/pcb.h>
2082
2083 void
2084 lwp_whatis(uintptr_t addr, void (*pr)(const char *, ...))
2085 {
2086 lwp_t *l;
2087
2088 LIST_FOREACH(l, &alllwp, l_list) {
2089 uintptr_t stack = (uintptr_t)KSTACK_LOWEST_ADDR(l);
2090
2091 if (addr < stack || stack + KSTACK_SIZE <= addr) {
2092 continue;
2093 }
2094 (*pr)("%p is %p+%zu, LWP %p's stack\n",
2095 (void *)addr, (void *)stack,
2096 (size_t)(addr - stack), l);
2097 }
2098 }
2099 #endif /* defined(DDB) */
2100