xref: /freebsd/sys/kern/kern_thread.c (revision cd672ca60f7c08a5189078c18579e12b65606f4c)
19454b2d8SWarner Losh /*-
244990b8cSJulian Elischer  * Copyright (C) 2001 Julian Elischer <julian@freebsd.org>.
344990b8cSJulian Elischer  *  All rights reserved.
444990b8cSJulian Elischer  *
544990b8cSJulian Elischer  * Redistribution and use in source and binary forms, with or without
644990b8cSJulian Elischer  * modification, are permitted provided that the following conditions
744990b8cSJulian Elischer  * are met:
844990b8cSJulian Elischer  * 1. Redistributions of source code must retain the above copyright
944990b8cSJulian Elischer  *    notice(s), this list of conditions and the following disclaimer as
1044990b8cSJulian Elischer  *    the first lines of this file unmodified other than the possible
1144990b8cSJulian Elischer  *    addition of one or more copyright notices.
1244990b8cSJulian Elischer  * 2. Redistributions in binary form must reproduce the above copyright
1344990b8cSJulian Elischer  *    notice(s), this list of conditions and the following disclaimer in the
1444990b8cSJulian Elischer  *    documentation and/or other materials provided with the distribution.
1544990b8cSJulian Elischer  *
1644990b8cSJulian Elischer  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
1744990b8cSJulian Elischer  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1844990b8cSJulian Elischer  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1944990b8cSJulian Elischer  * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
2044990b8cSJulian Elischer  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2144990b8cSJulian Elischer  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2244990b8cSJulian Elischer  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
2344990b8cSJulian Elischer  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2444990b8cSJulian Elischer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2544990b8cSJulian Elischer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
2644990b8cSJulian Elischer  * DAMAGE.
2744990b8cSJulian Elischer  */
2844990b8cSJulian Elischer 
293d06b4b3SAttilio Rao #include "opt_witness.h"
3016d95d4fSJoseph Koshy #include "opt_hwpmc_hooks.h"
313d06b4b3SAttilio Rao 
32677b542eSDavid E. O'Brien #include <sys/cdefs.h>
33677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
34677b542eSDavid E. O'Brien 
3544990b8cSJulian Elischer #include <sys/param.h>
3644990b8cSJulian Elischer #include <sys/systm.h>
3744990b8cSJulian Elischer #include <sys/kernel.h>
3844990b8cSJulian Elischer #include <sys/lock.h>
3944990b8cSJulian Elischer #include <sys/mutex.h>
4044990b8cSJulian Elischer #include <sys/proc.h>
418f0e9130SKonstantin Belousov #include <sys/rangelock.h>
42e170bfdaSDavid Xu #include <sys/resourcevar.h>
43b3e9e682SRyan Stone #include <sys/sdt.h>
4494e0a4cdSJulian Elischer #include <sys/smp.h>
45de028f5aSJeff Roberson #include <sys/sched.h>
4644f3b092SJohn Baldwin #include <sys/sleepqueue.h>
47ace8398dSJeff Roberson #include <sys/selinfo.h>
4891d1786fSDmitry Chagin #include <sys/sysent.h>
49961a7b24SJohn Baldwin #include <sys/turnstile.h>
5044990b8cSJulian Elischer #include <sys/ktr.h>
51cf7d9a8cSDavid Xu #include <sys/rwlock.h>
52bc8e6d81SDavid Xu #include <sys/umtx.h>
53d7f687fcSJeff Roberson #include <sys/cpuset.h>
5416d95d4fSJoseph Koshy #ifdef	HWPMC_HOOKS
5516d95d4fSJoseph Koshy #include <sys/pmckern.h>
5616d95d4fSJoseph Koshy #endif
5744990b8cSJulian Elischer 
58911b84b0SRobert Watson #include <security/audit/audit.h>
59911b84b0SRobert Watson 
6044990b8cSJulian Elischer #include <vm/vm.h>
6149a2507bSAlan Cox #include <vm/vm_extern.h>
6244990b8cSJulian Elischer #include <vm/uma.h>
636520495aSAdrian Chadd #include <vm/vm_domain.h>
64b209f889SRandall Stewart #include <sys/eventhandler.h>
6502fb42b0SPeter Wemm 
66b3e9e682SRyan Stone SDT_PROVIDER_DECLARE(proc);
67d9fae5abSAndriy Gapon SDT_PROBE_DEFINE(proc, , , lwp__exit);
68b3e9e682SRyan Stone 
698460a577SJohn Birrell /*
708460a577SJohn Birrell  * thread related storage.
718460a577SJohn Birrell  */
7244990b8cSJulian Elischer static uma_zone_t thread_zone;
7344990b8cSJulian Elischer 
745215b187SJeff Roberson TAILQ_HEAD(, thread) zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads);
75c8790f5dSAttilio Rao static struct mtx zombie_lock;
76a54e85fdSJeff Roberson MTX_SYSINIT(zombie_lock, &zombie_lock, "zombie lock", MTX_SPIN);
7744990b8cSJulian Elischer 
78ff8fbcffSJeff Roberson static void thread_zombie(struct thread *);
7984cdea97SKonstantin Belousov static int thread_unsuspend_one(struct thread *td, struct proc *p,
8084cdea97SKonstantin Belousov     bool boundary);
81ff8fbcffSJeff Roberson 
82ec6ea5e8SDavid Xu #define TID_BUFFER_SIZE	1024
83ec6ea5e8SDavid Xu 
84fdcac928SMarcel Moolenaar struct mtx tid_lock;
851ea7a6f8SPoul-Henning Kamp static struct unrhdr *tid_unrhdr;
86ec6ea5e8SDavid Xu static lwpid_t tid_buffer[TID_BUFFER_SIZE];
87ec6ea5e8SDavid Xu static int tid_head, tid_tail;
88cf7d9a8cSDavid Xu static MALLOC_DEFINE(M_TIDHASH, "tidhash", "thread hash");
89cf7d9a8cSDavid Xu 
90cf7d9a8cSDavid Xu struct	tidhashhead *tidhashtbl;
91cf7d9a8cSDavid Xu u_long	tidhash;
92cf7d9a8cSDavid Xu struct	rwlock tidhash_lock;
93cf7d9a8cSDavid Xu 
94ec6ea5e8SDavid Xu static lwpid_t
95ec6ea5e8SDavid Xu tid_alloc(void)
96ec6ea5e8SDavid Xu {
97ec6ea5e8SDavid Xu 	lwpid_t	tid;
98ec6ea5e8SDavid Xu 
99ec6ea5e8SDavid Xu 	tid = alloc_unr(tid_unrhdr);
100ec6ea5e8SDavid Xu 	if (tid != -1)
101ec6ea5e8SDavid Xu 		return (tid);
102ec6ea5e8SDavid Xu 	mtx_lock(&tid_lock);
103ec6ea5e8SDavid Xu 	if (tid_head == tid_tail) {
104ec6ea5e8SDavid Xu 		mtx_unlock(&tid_lock);
105ec6ea5e8SDavid Xu 		return (-1);
106ec6ea5e8SDavid Xu 	}
10794cb3545SKonstantin Belousov 	tid = tid_buffer[tid_head];
10894cb3545SKonstantin Belousov 	tid_head = (tid_head + 1) % TID_BUFFER_SIZE;
109ec6ea5e8SDavid Xu 	mtx_unlock(&tid_lock);
110ec6ea5e8SDavid Xu 	return (tid);
111ec6ea5e8SDavid Xu }
112ec6ea5e8SDavid Xu 
113ec6ea5e8SDavid Xu static void
114ec6ea5e8SDavid Xu tid_free(lwpid_t tid)
115ec6ea5e8SDavid Xu {
116ec6ea5e8SDavid Xu 	lwpid_t tmp_tid = -1;
117ec6ea5e8SDavid Xu 
118ec6ea5e8SDavid Xu 	mtx_lock(&tid_lock);
119ec6ea5e8SDavid Xu 	if ((tid_tail + 1) % TID_BUFFER_SIZE == tid_head) {
12094cb3545SKonstantin Belousov 		tmp_tid = tid_buffer[tid_head];
12194cb3545SKonstantin Belousov 		tid_head = (tid_head + 1) % TID_BUFFER_SIZE;
122ec6ea5e8SDavid Xu 	}
12394cb3545SKonstantin Belousov 	tid_buffer[tid_tail] = tid;
12494cb3545SKonstantin Belousov 	tid_tail = (tid_tail + 1) % TID_BUFFER_SIZE;
125ec6ea5e8SDavid Xu 	mtx_unlock(&tid_lock);
126ec6ea5e8SDavid Xu 	if (tmp_tid != -1)
127ec6ea5e8SDavid Xu 		free_unr(tid_unrhdr, tmp_tid);
128ec6ea5e8SDavid Xu }
129ec6ea5e8SDavid Xu 
130fdcac928SMarcel Moolenaar /*
131696058c3SJulian Elischer  * Prepare a thread for use.
13244990b8cSJulian Elischer  */
133b23f72e9SBrian Feldman static int
134b23f72e9SBrian Feldman thread_ctor(void *mem, int size, void *arg, int flags)
13544990b8cSJulian Elischer {
13644990b8cSJulian Elischer 	struct thread	*td;
13744990b8cSJulian Elischer 
13844990b8cSJulian Elischer 	td = (struct thread *)mem;
13971fad9fdSJulian Elischer 	td->td_state = TDS_INACTIVE;
140060563ecSJulian Elischer 	td->td_oncpu = NOCPU;
1416c27c603SJuli Mallett 
142ec6ea5e8SDavid Xu 	td->td_tid = tid_alloc();
143773eff9dSPoul-Henning Kamp 
1446c27c603SJuli Mallett 	/*
1456c27c603SJuli Mallett 	 * Note that td_critnest begins life as 1 because the thread is not
1466c27c603SJuli Mallett 	 * running and is thereby implicitly waiting to be on the receiving
147a54e85fdSJeff Roberson 	 * end of a context switch.
1486c27c603SJuli Mallett 	 */
149139b7550SJohn Baldwin 	td->td_critnest = 1;
150acbe332aSDavid Xu 	td->td_lend_user_pri = PRI_MAX;
151b209f889SRandall Stewart 	EVENTHANDLER_INVOKE(thread_ctor, td);
152911b84b0SRobert Watson #ifdef AUDIT
153911b84b0SRobert Watson 	audit_thread_alloc(td);
154911b84b0SRobert Watson #endif
155d10183d9SDavid Xu 	umtx_thread_alloc(td);
156b23f72e9SBrian Feldman 	return (0);
15744990b8cSJulian Elischer }
15844990b8cSJulian Elischer 
15944990b8cSJulian Elischer /*
16044990b8cSJulian Elischer  * Reclaim a thread after use.
16144990b8cSJulian Elischer  */
16244990b8cSJulian Elischer static void
16344990b8cSJulian Elischer thread_dtor(void *mem, int size, void *arg)
16444990b8cSJulian Elischer {
16544990b8cSJulian Elischer 	struct thread *td;
16644990b8cSJulian Elischer 
16744990b8cSJulian Elischer 	td = (struct thread *)mem;
16844990b8cSJulian Elischer 
16944990b8cSJulian Elischer #ifdef INVARIANTS
17044990b8cSJulian Elischer 	/* Verify that this thread is in a safe state to free. */
17144990b8cSJulian Elischer 	switch (td->td_state) {
17271fad9fdSJulian Elischer 	case TDS_INHIBITED:
17371fad9fdSJulian Elischer 	case TDS_RUNNING:
17471fad9fdSJulian Elischer 	case TDS_CAN_RUN:
17544990b8cSJulian Elischer 	case TDS_RUNQ:
17644990b8cSJulian Elischer 		/*
17744990b8cSJulian Elischer 		 * We must never unlink a thread that is in one of
17844990b8cSJulian Elischer 		 * these states, because it is currently active.
17944990b8cSJulian Elischer 		 */
18044990b8cSJulian Elischer 		panic("bad state for thread unlinking");
18144990b8cSJulian Elischer 		/* NOTREACHED */
18271fad9fdSJulian Elischer 	case TDS_INACTIVE:
18344990b8cSJulian Elischer 		break;
18444990b8cSJulian Elischer 	default:
18544990b8cSJulian Elischer 		panic("bad thread state");
18644990b8cSJulian Elischer 		/* NOTREACHED */
18744990b8cSJulian Elischer 	}
18844990b8cSJulian Elischer #endif
1896e8525ceSRobert Watson #ifdef AUDIT
1906e8525ceSRobert Watson 	audit_thread_free(td);
1916e8525ceSRobert Watson #endif
1921ba4a712SPawel Jakub Dawidek 	/* Free all OSD associated to this thread. */
1931ba4a712SPawel Jakub Dawidek 	osd_thread_exit(td);
1941ba4a712SPawel Jakub Dawidek 
195b209f889SRandall Stewart 	EVENTHANDLER_INVOKE(thread_dtor, td);
196ec6ea5e8SDavid Xu 	tid_free(td->td_tid);
19744990b8cSJulian Elischer }
19844990b8cSJulian Elischer 
19944990b8cSJulian Elischer /*
20044990b8cSJulian Elischer  * Initialize type-stable parts of a thread (when newly created).
20144990b8cSJulian Elischer  */
202b23f72e9SBrian Feldman static int
203b23f72e9SBrian Feldman thread_init(void *mem, int size, int flags)
20444990b8cSJulian Elischer {
20544990b8cSJulian Elischer 	struct thread *td;
20644990b8cSJulian Elischer 
20744990b8cSJulian Elischer 	td = (struct thread *)mem;
208247aba24SMarcel Moolenaar 
20944f3b092SJohn Baldwin 	td->td_sleepqueue = sleepq_alloc();
210961a7b24SJohn Baldwin 	td->td_turnstile = turnstile_alloc();
2118f0e9130SKonstantin Belousov 	td->td_rlqe = NULL;
212b209f889SRandall Stewart 	EVENTHANDLER_INVOKE(thread_init, td);
213de028f5aSJeff Roberson 	td->td_sched = (struct td_sched *)&td[1];
214d10183d9SDavid Xu 	umtx_thread_init(td);
21589b57fcfSKonstantin Belousov 	td->td_kstack = 0;
216ad8b1d85SKonstantin Belousov 	td->td_sel = NULL;
217b23f72e9SBrian Feldman 	return (0);
21844990b8cSJulian Elischer }
21944990b8cSJulian Elischer 
22044990b8cSJulian Elischer /*
22144990b8cSJulian Elischer  * Tear down type-stable parts of a thread (just before being discarded).
22244990b8cSJulian Elischer  */
22344990b8cSJulian Elischer static void
22444990b8cSJulian Elischer thread_fini(void *mem, int size)
22544990b8cSJulian Elischer {
22644990b8cSJulian Elischer 	struct thread *td;
22744990b8cSJulian Elischer 
22844990b8cSJulian Elischer 	td = (struct thread *)mem;
229b209f889SRandall Stewart 	EVENTHANDLER_INVOKE(thread_fini, td);
2308f0e9130SKonstantin Belousov 	rlqentry_free(td->td_rlqe);
231961a7b24SJohn Baldwin 	turnstile_free(td->td_turnstile);
23244f3b092SJohn Baldwin 	sleepq_free(td->td_sleepqueue);
233d10183d9SDavid Xu 	umtx_thread_fini(td);
234ace8398dSJeff Roberson 	seltdfini(td);
23544990b8cSJulian Elischer }
2365215b187SJeff Roberson 
2375c8329edSJulian Elischer /*
2385215b187SJeff Roberson  * For a newly created process,
2395215b187SJeff Roberson  * link up all the structures and its initial threads etc.
240ed062c8dSJulian Elischer  * called from:
241e7d939bdSMarcel Moolenaar  * {arch}/{arch}/machdep.c   {arch}_init(), init386() etc.
242ed062c8dSJulian Elischer  * proc_dtor() (should go away)
243ed062c8dSJulian Elischer  * proc_init()
2445c8329edSJulian Elischer  */
2455c8329edSJulian Elischer void
24689b57fcfSKonstantin Belousov proc_linkup0(struct proc *p, struct thread *td)
24789b57fcfSKonstantin Belousov {
24889b57fcfSKonstantin Belousov 	TAILQ_INIT(&p->p_threads);	     /* all threads in proc */
24989b57fcfSKonstantin Belousov 	proc_linkup(p, td);
25089b57fcfSKonstantin Belousov }
25189b57fcfSKonstantin Belousov 
25289b57fcfSKonstantin Belousov void
2538460a577SJohn Birrell proc_linkup(struct proc *p, struct thread *td)
2545c8329edSJulian Elischer {
255a54e85fdSJeff Roberson 
2569104847fSDavid Xu 	sigqueue_init(&p->p_sigqueue, p);
257ebceaf6dSDavid Xu 	p->p_ksi = ksiginfo_alloc(1);
258ebceaf6dSDavid Xu 	if (p->p_ksi != NULL) {
2595c474517SDavid Xu 		/* XXX p_ksi may be null if ksiginfo zone is not ready */
260ebceaf6dSDavid Xu 		p->p_ksi->ksi_flags = KSI_EXT | KSI_INS;
261ebceaf6dSDavid Xu 	}
262b2f92ef9SDavid Xu 	LIST_INIT(&p->p_mqnotifier);
2635c8329edSJulian Elischer 	p->p_numthreads = 0;
2648460a577SJohn Birrell 	thread_link(td, p);
2655c8329edSJulian Elischer }
2665c8329edSJulian Elischer 
2675c8329edSJulian Elischer /*
26844990b8cSJulian Elischer  * Initialize global thread allocation resources.
26944990b8cSJulian Elischer  */
27044990b8cSJulian Elischer void
27144990b8cSJulian Elischer threadinit(void)
27244990b8cSJulian Elischer {
27344990b8cSJulian Elischer 
2741ea7a6f8SPoul-Henning Kamp 	mtx_init(&tid_lock, "TID lock", NULL, MTX_DEF);
27502c6fc21SKonstantin Belousov 
27602c6fc21SKonstantin Belousov 	/*
277abce621cSKonstantin Belousov 	 * pid_max cannot be greater than PID_MAX.
27802c6fc21SKonstantin Belousov 	 * leave one number for thread0.
27902c6fc21SKonstantin Belousov 	 */
2806829a5c5SJulian Elischer 	tid_unrhdr = new_unrhdr(PID_MAX + 2, INT_MAX, &tid_lock);
2811ea7a6f8SPoul-Henning Kamp 
282de028f5aSJeff Roberson 	thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(),
28344990b8cSJulian Elischer 	    thread_ctor, thread_dtor, thread_init, thread_fini,
2844649e92bSJohn Baldwin 	    16 - 1, 0);
285cf7d9a8cSDavid Xu 	tidhashtbl = hashinit(maxproc / 2, M_TIDHASH, &tidhash);
286cf7d9a8cSDavid Xu 	rw_init(&tidhash_lock, "tidhash");
28744990b8cSJulian Elischer }
28844990b8cSJulian Elischer 
28944990b8cSJulian Elischer /*
290ff8fbcffSJeff Roberson  * Place an unused thread on the zombie list.
291ad1e7d28SJulian Elischer  * Use the slpq as that must be unused by now.
29244990b8cSJulian Elischer  */
29344990b8cSJulian Elischer void
294ff8fbcffSJeff Roberson thread_zombie(struct thread *td)
29544990b8cSJulian Elischer {
296a54e85fdSJeff Roberson 	mtx_lock_spin(&zombie_lock);
297ad1e7d28SJulian Elischer 	TAILQ_INSERT_HEAD(&zombie_threads, td, td_slpq);
298a54e85fdSJeff Roberson 	mtx_unlock_spin(&zombie_lock);
29944990b8cSJulian Elischer }
30044990b8cSJulian Elischer 
3015c8329edSJulian Elischer /*
302ff8fbcffSJeff Roberson  * Release a thread that has exited after cpu_throw().
303ff8fbcffSJeff Roberson  */
304ff8fbcffSJeff Roberson void
305ff8fbcffSJeff Roberson thread_stash(struct thread *td)
306ff8fbcffSJeff Roberson {
307ff8fbcffSJeff Roberson 	atomic_subtract_rel_int(&td->td_proc->p_exitthreads, 1);
308ff8fbcffSJeff Roberson 	thread_zombie(td);
309ff8fbcffSJeff Roberson }
310ff8fbcffSJeff Roberson 
311ff8fbcffSJeff Roberson /*
3126617724cSJeff Roberson  * Reap zombie resources.
31344990b8cSJulian Elischer  */
31444990b8cSJulian Elischer void
31544990b8cSJulian Elischer thread_reap(void)
31644990b8cSJulian Elischer {
3175c8329edSJulian Elischer 	struct thread *td_first, *td_next;
31844990b8cSJulian Elischer 
31944990b8cSJulian Elischer 	/*
3205215b187SJeff Roberson 	 * Don't even bother to lock if none at this instant,
3215215b187SJeff Roberson 	 * we really don't care about the next instant..
32244990b8cSJulian Elischer 	 */
3238460a577SJohn Birrell 	if (!TAILQ_EMPTY(&zombie_threads)) {
324a54e85fdSJeff Roberson 		mtx_lock_spin(&zombie_lock);
3255c8329edSJulian Elischer 		td_first = TAILQ_FIRST(&zombie_threads);
3265c8329edSJulian Elischer 		if (td_first)
3275c8329edSJulian Elischer 			TAILQ_INIT(&zombie_threads);
328a54e85fdSJeff Roberson 		mtx_unlock_spin(&zombie_lock);
3295c8329edSJulian Elischer 		while (td_first) {
330ad1e7d28SJulian Elischer 			td_next = TAILQ_NEXT(td_first, td_slpq);
3314ea6a9a2SMateusz Guzik 			thread_cow_free(td_first);
3325c8329edSJulian Elischer 			thread_free(td_first);
3335c8329edSJulian Elischer 			td_first = td_next;
33444990b8cSJulian Elischer 		}
33544990b8cSJulian Elischer 	}
336ed062c8dSJulian Elischer }
33744990b8cSJulian Elischer 
3384f0db5e0SJulian Elischer /*
33944990b8cSJulian Elischer  * Allocate a thread.
34044990b8cSJulian Elischer  */
34144990b8cSJulian Elischer struct thread *
3428a945d10SKonstantin Belousov thread_alloc(int pages)
34344990b8cSJulian Elischer {
34489b57fcfSKonstantin Belousov 	struct thread *td;
3458460a577SJohn Birrell 
34644990b8cSJulian Elischer 	thread_reap(); /* check if any zombies to get */
34789b57fcfSKonstantin Belousov 
34889b57fcfSKonstantin Belousov 	td = (struct thread *)uma_zalloc(thread_zone, M_WAITOK);
34989b57fcfSKonstantin Belousov 	KASSERT(td->td_kstack == 0, ("thread_alloc got thread with kstack"));
3508a945d10SKonstantin Belousov 	if (!vm_thread_new(td, pages)) {
35189b57fcfSKonstantin Belousov 		uma_zfree(thread_zone, td);
35289b57fcfSKonstantin Belousov 		return (NULL);
35389b57fcfSKonstantin Belousov 	}
3540c3967e7SMarcel Moolenaar 	cpu_thread_alloc(td);
3556520495aSAdrian Chadd 	vm_domain_policy_init(&td->td_vm_dom_policy);
35689b57fcfSKonstantin Belousov 	return (td);
35744990b8cSJulian Elischer }
35844990b8cSJulian Elischer 
3598a945d10SKonstantin Belousov int
3608a945d10SKonstantin Belousov thread_alloc_stack(struct thread *td, int pages)
3618a945d10SKonstantin Belousov {
3628a945d10SKonstantin Belousov 
3638a945d10SKonstantin Belousov 	KASSERT(td->td_kstack == 0,
3648a945d10SKonstantin Belousov 	    ("thread_alloc_stack called on a thread with kstack"));
3658a945d10SKonstantin Belousov 	if (!vm_thread_new(td, pages))
3668a945d10SKonstantin Belousov 		return (0);
3678a945d10SKonstantin Belousov 	cpu_thread_alloc(td);
3688a945d10SKonstantin Belousov 	return (1);
3698a945d10SKonstantin Belousov }
3704f0db5e0SJulian Elischer 
3714f0db5e0SJulian Elischer /*
37244990b8cSJulian Elischer  * Deallocate a thread.
37344990b8cSJulian Elischer  */
37444990b8cSJulian Elischer void
37544990b8cSJulian Elischer thread_free(struct thread *td)
37644990b8cSJulian Elischer {
3772e6b8de4SJeff Roberson 
3782e6b8de4SJeff Roberson 	lock_profile_thread_exit(td);
37945aea8deSJeff Roberson 	if (td->td_cpuset)
380d7f687fcSJeff Roberson 		cpuset_rel(td->td_cpuset);
381d7f687fcSJeff Roberson 	td->td_cpuset = NULL;
3820c3967e7SMarcel Moolenaar 	cpu_thread_free(td);
38389b57fcfSKonstantin Belousov 	if (td->td_kstack != 0)
38489b57fcfSKonstantin Belousov 		vm_thread_dispose(td);
3856520495aSAdrian Chadd 	vm_domain_policy_cleanup(&td->td_vm_dom_policy);
38644990b8cSJulian Elischer 	uma_zfree(thread_zone, td);
38744990b8cSJulian Elischer }
38844990b8cSJulian Elischer 
3894ea6a9a2SMateusz Guzik void
3904ea6a9a2SMateusz Guzik thread_cow_get_proc(struct thread *newtd, struct proc *p)
3914ea6a9a2SMateusz Guzik {
3924ea6a9a2SMateusz Guzik 
3934ea6a9a2SMateusz Guzik 	PROC_LOCK_ASSERT(p, MA_OWNED);
3944ea6a9a2SMateusz Guzik 	newtd->td_ucred = crhold(p->p_ucred);
395f6f6d240SMateusz Guzik 	newtd->td_limit = lim_hold(p->p_limit);
3964ea6a9a2SMateusz Guzik 	newtd->td_cowgen = p->p_cowgen;
3974ea6a9a2SMateusz Guzik }
3984ea6a9a2SMateusz Guzik 
3994ea6a9a2SMateusz Guzik void
4004ea6a9a2SMateusz Guzik thread_cow_get(struct thread *newtd, struct thread *td)
4014ea6a9a2SMateusz Guzik {
4024ea6a9a2SMateusz Guzik 
4034ea6a9a2SMateusz Guzik 	newtd->td_ucred = crhold(td->td_ucred);
404f6f6d240SMateusz Guzik 	newtd->td_limit = lim_hold(td->td_limit);
4054ea6a9a2SMateusz Guzik 	newtd->td_cowgen = td->td_cowgen;
4064ea6a9a2SMateusz Guzik }
4074ea6a9a2SMateusz Guzik 
4084ea6a9a2SMateusz Guzik void
4094ea6a9a2SMateusz Guzik thread_cow_free(struct thread *td)
4104ea6a9a2SMateusz Guzik {
4114ea6a9a2SMateusz Guzik 
412*cd672ca6SMateusz Guzik 	if (td->td_ucred != NULL)
4134ea6a9a2SMateusz Guzik 		crfree(td->td_ucred);
414*cd672ca6SMateusz Guzik 	if (td->td_limit != NULL)
415f6f6d240SMateusz Guzik 		lim_free(td->td_limit);
4164ea6a9a2SMateusz Guzik }
4174ea6a9a2SMateusz Guzik 
4184ea6a9a2SMateusz Guzik void
4194ea6a9a2SMateusz Guzik thread_cow_update(struct thread *td)
4204ea6a9a2SMateusz Guzik {
4214ea6a9a2SMateusz Guzik 	struct proc *p;
422*cd672ca6SMateusz Guzik 	struct ucred *oldcred;
423*cd672ca6SMateusz Guzik 	struct plimit *oldlimit;
4244ea6a9a2SMateusz Guzik 
4254ea6a9a2SMateusz Guzik 	p = td->td_proc;
426*cd672ca6SMateusz Guzik 	oldcred = NULL;
427*cd672ca6SMateusz Guzik 	oldlimit = NULL;
4284ea6a9a2SMateusz Guzik 	PROC_LOCK(p);
429*cd672ca6SMateusz Guzik 	if (td->td_ucred != p->p_ucred) {
430*cd672ca6SMateusz Guzik 		oldcred = td->td_ucred;
431*cd672ca6SMateusz Guzik 		td->td_ucred = crhold(p->p_ucred);
432*cd672ca6SMateusz Guzik 	}
433*cd672ca6SMateusz Guzik 	if (td->td_limit != p->p_limit) {
434*cd672ca6SMateusz Guzik 		oldlimit = td->td_limit;
435*cd672ca6SMateusz Guzik 		td->td_limit = lim_hold(p->p_limit);
436*cd672ca6SMateusz Guzik 	}
4374ea6a9a2SMateusz Guzik 	td->td_cowgen = p->p_cowgen;
4384ea6a9a2SMateusz Guzik 	PROC_UNLOCK(p);
439*cd672ca6SMateusz Guzik 	if (oldcred != NULL)
440*cd672ca6SMateusz Guzik 		crfree(oldcred);
441*cd672ca6SMateusz Guzik 	if (oldlimit != NULL)
442*cd672ca6SMateusz Guzik 		lim_free(oldlimit);
4434ea6a9a2SMateusz Guzik }
4444ea6a9a2SMateusz Guzik 
44544990b8cSJulian Elischer /*
44644990b8cSJulian Elischer  * Discard the current thread and exit from its context.
44794e0a4cdSJulian Elischer  * Always called with scheduler locked.
44844990b8cSJulian Elischer  *
44944990b8cSJulian Elischer  * Because we can't free a thread while we're operating under its context,
450696058c3SJulian Elischer  * push the current thread into our CPU's deadthread holder. This means
451696058c3SJulian Elischer  * we needn't worry about someone else grabbing our context before we
4526617724cSJeff Roberson  * do a cpu_throw().
45344990b8cSJulian Elischer  */
45444990b8cSJulian Elischer void
45544990b8cSJulian Elischer thread_exit(void)
45644990b8cSJulian Elischer {
4577e3a96eaSJohn Baldwin 	uint64_t runtime, new_switchtime;
45844990b8cSJulian Elischer 	struct thread *td;
4591c4bcd05SJeff Roberson 	struct thread *td2;
46044990b8cSJulian Elischer 	struct proc *p;
4617847a9daSJohn Baldwin 	int wakeup_swapper;
46244990b8cSJulian Elischer 
46344990b8cSJulian Elischer 	td = curthread;
46444990b8cSJulian Elischer 	p = td->td_proc;
46544990b8cSJulian Elischer 
466a54e85fdSJeff Roberson 	PROC_SLOCK_ASSERT(p, MA_OWNED);
467ed062c8dSJulian Elischer 	mtx_assert(&Giant, MA_NOTOWNED);
468a54e85fdSJeff Roberson 
46944990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
470ed062c8dSJulian Elischer 	KASSERT(p != NULL, ("thread exiting without a process"));
471cc701b73SRobert Watson 	CTR3(KTR_PROC, "thread_exit: thread %p (pid %ld, %s)", td,
472e01eafefSJulian Elischer 	    (long)p->p_pid, td->td_name);
4739104847fSDavid Xu 	KASSERT(TAILQ_EMPTY(&td->td_sigqueue.sq_list), ("signal pending"));
47444990b8cSJulian Elischer 
47589964dd2SRobert Watson #ifdef AUDIT
47689964dd2SRobert Watson 	AUDIT_SYSCALL_EXIT(0, td);
47789964dd2SRobert Watson #endif
478ed062c8dSJulian Elischer 	/*
479ed062c8dSJulian Elischer 	 * drop FPU & debug register state storage, or any other
480ed062c8dSJulian Elischer 	 * architecture specific resources that
481ed062c8dSJulian Elischer 	 * would not be on a new untouched process.
482ed062c8dSJulian Elischer 	 */
48344990b8cSJulian Elischer 	cpu_thread_exit(td);	/* XXXSMP */
48444990b8cSJulian Elischer 
485ed062c8dSJulian Elischer 	/*
4861faf202eSJulian Elischer 	 * The last thread is left attached to the process
4871faf202eSJulian Elischer 	 * So that the whole bundle gets recycled. Skip
488ed062c8dSJulian Elischer 	 * all this stuff if we never had threads.
489ed062c8dSJulian Elischer 	 * EXIT clears all sign of other threads when
490ed062c8dSJulian Elischer 	 * it goes to single threading, so the last thread always
491ed062c8dSJulian Elischer 	 * takes the short path.
4921faf202eSJulian Elischer 	 */
493ed062c8dSJulian Elischer 	if (p->p_flag & P_HADTHREADS) {
4941faf202eSJulian Elischer 		if (p->p_numthreads > 1) {
495fd229b5bSKonstantin Belousov 			atomic_add_int(&td->td_proc->p_exitthreads, 1);
496d3a0bd78SJulian Elischer 			thread_unlink(td);
4971c4bcd05SJeff Roberson 			td2 = FIRST_THREAD_IN_PROC(p);
4981c4bcd05SJeff Roberson 			sched_exit_thread(td2, td);
499ed062c8dSJulian Elischer 
500ed062c8dSJulian Elischer 			/*
50144990b8cSJulian Elischer 			 * The test below is NOT true if we are the
5029182554aSKonstantin Belousov 			 * sole exiting thread. P_STOPPED_SINGLE is unset
50344990b8cSJulian Elischer 			 * in exit1() after it is the only survivor.
50444990b8cSJulian Elischer 			 */
5051279572aSDavid Xu 			if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
50644990b8cSJulian Elischer 				if (p->p_numthreads == p->p_suspcount) {
507a54e85fdSJeff Roberson 					thread_lock(p->p_singlethread);
5087847a9daSJohn Baldwin 					wakeup_swapper = thread_unsuspend_one(
50984cdea97SKonstantin Belousov 						p->p_singlethread, p, false);
510a54e85fdSJeff Roberson 					thread_unlock(p->p_singlethread);
5117847a9daSJohn Baldwin 					if (wakeup_swapper)
5127847a9daSJohn Baldwin 						kick_proc0();
51344990b8cSJulian Elischer 				}
51444990b8cSJulian Elischer 			}
51548bfcdddSJulian Elischer 
516696058c3SJulian Elischer 			PCPU_SET(deadthread, td);
5171faf202eSJulian Elischer 		} else {
518ed062c8dSJulian Elischer 			/*
519ed062c8dSJulian Elischer 			 * The last thread is exiting.. but not through exit()
520ed062c8dSJulian Elischer 			 */
521ed062c8dSJulian Elischer 			panic ("thread_exit: Last thread exiting on its own");
522ed062c8dSJulian Elischer 		}
5231faf202eSJulian Elischer 	}
52416d95d4fSJoseph Koshy #ifdef	HWPMC_HOOKS
52516d95d4fSJoseph Koshy 	/*
52616d95d4fSJoseph Koshy 	 * If this thread is part of a process that is being tracked by hwpmc(4),
52716d95d4fSJoseph Koshy 	 * inform the module of the thread's impending exit.
52816d95d4fSJoseph Koshy 	 */
52916d95d4fSJoseph Koshy 	if (PMC_PROC_IS_USING_PMCS(td->td_proc))
53016d95d4fSJoseph Koshy 		PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT);
53116d95d4fSJoseph Koshy #endif
532a54e85fdSJeff Roberson 	PROC_UNLOCK(p);
5335c7bebf9SKonstantin Belousov 	PROC_STATLOCK(p);
5345c7bebf9SKonstantin Belousov 	thread_lock(td);
5355c7bebf9SKonstantin Belousov 	PROC_SUNLOCK(p);
5367e3a96eaSJohn Baldwin 
5377e3a96eaSJohn Baldwin 	/* Do the same timestamp bookkeeping that mi_switch() would do. */
5387e3a96eaSJohn Baldwin 	new_switchtime = cpu_ticks();
5397e3a96eaSJohn Baldwin 	runtime = new_switchtime - PCPU_GET(switchtime);
5407e3a96eaSJohn Baldwin 	td->td_runtime += runtime;
5417e3a96eaSJohn Baldwin 	td->td_incruntime += runtime;
5427e3a96eaSJohn Baldwin 	PCPU_SET(switchtime, new_switchtime);
5437e3a96eaSJohn Baldwin 	PCPU_SET(switchticks, ticks);
5447e3a96eaSJohn Baldwin 	PCPU_INC(cnt.v_swtch);
5457e3a96eaSJohn Baldwin 
5467e3a96eaSJohn Baldwin 	/* Save our resource usage in our process. */
5477e3a96eaSJohn Baldwin 	td->td_ru.ru_nvcsw++;
54841fd9c63SKonstantin Belousov 	ruxagg(p, td);
5497e3a96eaSJohn Baldwin 	rucollect(&p->p_ru, &td->td_ru);
5505c7bebf9SKonstantin Belousov 	PROC_STATUNLOCK(p);
5517e3a96eaSJohn Baldwin 
552dcc9954eSJulian Elischer 	td->td_state = TDS_INACTIVE;
5533d06b4b3SAttilio Rao #ifdef WITNESS
5543d06b4b3SAttilio Rao 	witness_thread_exit(td);
5553d06b4b3SAttilio Rao #endif
556732d9528SJulian Elischer 	CTR1(KTR_PROC, "thread_exit: cpu_throw() thread %p", td);
557a54e85fdSJeff Roberson 	sched_throw(td);
558cc66ebe2SPeter Wemm 	panic("I'm a teapot!");
55944990b8cSJulian Elischer 	/* NOTREACHED */
56044990b8cSJulian Elischer }
56144990b8cSJulian Elischer 
56244990b8cSJulian Elischer /*
563696058c3SJulian Elischer  * Do any thread specific cleanups that may be needed in wait()
56437814395SPeter Wemm  * called with Giant, proc and schedlock not held.
565696058c3SJulian Elischer  */
566696058c3SJulian Elischer void
567696058c3SJulian Elischer thread_wait(struct proc *p)
568696058c3SJulian Elischer {
569696058c3SJulian Elischer 	struct thread *td;
570696058c3SJulian Elischer 
57137814395SPeter Wemm 	mtx_assert(&Giant, MA_NOTOWNED);
572624bf9e1SKonstantin Belousov 	KASSERT(p->p_numthreads == 1, ("multiple threads in thread_wait()"));
573624bf9e1SKonstantin Belousov 	KASSERT(p->p_exitthreads == 0, ("p_exitthreads leaking"));
574ff8fbcffSJeff Roberson 	td = FIRST_THREAD_IN_PROC(p);
575ff8fbcffSJeff Roberson 	/* Lock the last thread so we spin until it exits cpu_throw(). */
576ff8fbcffSJeff Roberson 	thread_lock(td);
577ff8fbcffSJeff Roberson 	thread_unlock(td);
5782e6b8de4SJeff Roberson 	lock_profile_thread_exit(td);
579d7f687fcSJeff Roberson 	cpuset_rel(td->td_cpuset);
580d7f687fcSJeff Roberson 	td->td_cpuset = NULL;
581696058c3SJulian Elischer 	cpu_thread_clean(td);
5824ea6a9a2SMateusz Guzik 	thread_cow_free(td);
583696058c3SJulian Elischer 	thread_reap();	/* check for zombie threads etc. */
584696058c3SJulian Elischer }
585696058c3SJulian Elischer 
586696058c3SJulian Elischer /*
58744990b8cSJulian Elischer  * Link a thread to a process.
5881faf202eSJulian Elischer  * set up anything that needs to be initialized for it to
5891faf202eSJulian Elischer  * be used by the process.
59044990b8cSJulian Elischer  */
59144990b8cSJulian Elischer void
5928460a577SJohn Birrell thread_link(struct thread *td, struct proc *p)
59344990b8cSJulian Elischer {
59444990b8cSJulian Elischer 
595a54e85fdSJeff Roberson 	/*
596a54e85fdSJeff Roberson 	 * XXX This can't be enabled because it's called for proc0 before
597374ae2a3SJeff Roberson 	 * its lock has been created.
598374ae2a3SJeff Roberson 	 * PROC_LOCK_ASSERT(p, MA_OWNED);
599a54e85fdSJeff Roberson 	 */
60071fad9fdSJulian Elischer 	td->td_state    = TDS_INACTIVE;
60144990b8cSJulian Elischer 	td->td_proc     = p;
602b61ce5b0SJeff Roberson 	td->td_flags    = TDF_INMEM;
60344990b8cSJulian Elischer 
6041faf202eSJulian Elischer 	LIST_INIT(&td->td_contested);
605eea4f254SJeff Roberson 	LIST_INIT(&td->td_lprof[0]);
606eea4f254SJeff Roberson 	LIST_INIT(&td->td_lprof[1]);
6079104847fSDavid Xu 	sigqueue_init(&td->td_sigqueue, p);
608fd90e2edSJung-uk Kim 	callout_init(&td->td_slpcallout, 1);
60966d8df9dSDaniel Eischen 	TAILQ_INSERT_TAIL(&p->p_threads, td, td_plist);
61044990b8cSJulian Elischer 	p->p_numthreads++;
61144990b8cSJulian Elischer }
61244990b8cSJulian Elischer 
613ed062c8dSJulian Elischer /*
614ed062c8dSJulian Elischer  * Called from:
615ed062c8dSJulian Elischer  *  thread_exit()
616ed062c8dSJulian Elischer  */
617d3a0bd78SJulian Elischer void
618d3a0bd78SJulian Elischer thread_unlink(struct thread *td)
619d3a0bd78SJulian Elischer {
620d3a0bd78SJulian Elischer 	struct proc *p = td->td_proc;
621d3a0bd78SJulian Elischer 
622374ae2a3SJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
623d3a0bd78SJulian Elischer 	TAILQ_REMOVE(&p->p_threads, td, td_plist);
624d3a0bd78SJulian Elischer 	p->p_numthreads--;
625d3a0bd78SJulian Elischer 	/* could clear a few other things here */
6268460a577SJohn Birrell 	/* Must  NOT clear links to proc! */
6275c8329edSJulian Elischer }
6285c8329edSJulian Elischer 
62979799053SKonstantin Belousov static int
63079799053SKonstantin Belousov calc_remaining(struct proc *p, int mode)
63179799053SKonstantin Belousov {
63279799053SKonstantin Belousov 	int remaining;
63379799053SKonstantin Belousov 
6347b519077SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
6357b519077SKonstantin Belousov 	PROC_SLOCK_ASSERT(p, MA_OWNED);
63679799053SKonstantin Belousov 	if (mode == SINGLE_EXIT)
63779799053SKonstantin Belousov 		remaining = p->p_numthreads;
63879799053SKonstantin Belousov 	else if (mode == SINGLE_BOUNDARY)
63979799053SKonstantin Belousov 		remaining = p->p_numthreads - p->p_boundary_count;
6406ddcc233SKonstantin Belousov 	else if (mode == SINGLE_NO_EXIT || mode == SINGLE_ALLPROC)
64179799053SKonstantin Belousov 		remaining = p->p_numthreads - p->p_suspcount;
64279799053SKonstantin Belousov 	else
64379799053SKonstantin Belousov 		panic("calc_remaining: wrong mode %d", mode);
64479799053SKonstantin Belousov 	return (remaining);
64579799053SKonstantin Belousov }
64679799053SKonstantin Belousov 
64707a9368aSKonstantin Belousov static int
64807a9368aSKonstantin Belousov remain_for_mode(int mode)
64907a9368aSKonstantin Belousov {
65007a9368aSKonstantin Belousov 
6516ddcc233SKonstantin Belousov 	return (mode == SINGLE_ALLPROC ? 0 : 1);
65207a9368aSKonstantin Belousov }
65307a9368aSKonstantin Belousov 
65407a9368aSKonstantin Belousov static int
65507a9368aSKonstantin Belousov weed_inhib(int mode, struct thread *td2, struct proc *p)
65607a9368aSKonstantin Belousov {
65707a9368aSKonstantin Belousov 	int wakeup_swapper;
65807a9368aSKonstantin Belousov 
65907a9368aSKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
66007a9368aSKonstantin Belousov 	PROC_SLOCK_ASSERT(p, MA_OWNED);
66107a9368aSKonstantin Belousov 	THREAD_LOCK_ASSERT(td2, MA_OWNED);
66207a9368aSKonstantin Belousov 
66307a9368aSKonstantin Belousov 	wakeup_swapper = 0;
66407a9368aSKonstantin Belousov 	switch (mode) {
66507a9368aSKonstantin Belousov 	case SINGLE_EXIT:
66607a9368aSKonstantin Belousov 		if (TD_IS_SUSPENDED(td2))
66784cdea97SKonstantin Belousov 			wakeup_swapper |= thread_unsuspend_one(td2, p, true);
66807a9368aSKonstantin Belousov 		if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR) != 0)
66907a9368aSKonstantin Belousov 			wakeup_swapper |= sleepq_abort(td2, EINTR);
67007a9368aSKonstantin Belousov 		break;
67107a9368aSKonstantin Belousov 	case SINGLE_BOUNDARY:
67207a9368aSKonstantin Belousov 		if (TD_IS_SUSPENDED(td2) && (td2->td_flags & TDF_BOUNDARY) == 0)
67384cdea97SKonstantin Belousov 			wakeup_swapper |= thread_unsuspend_one(td2, p, false);
67407a9368aSKonstantin Belousov 		if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR) != 0)
67507a9368aSKonstantin Belousov 			wakeup_swapper |= sleepq_abort(td2, ERESTART);
67607a9368aSKonstantin Belousov 		break;
67707a9368aSKonstantin Belousov 	case SINGLE_NO_EXIT:
67807a9368aSKonstantin Belousov 		if (TD_IS_SUSPENDED(td2) && (td2->td_flags & TDF_BOUNDARY) == 0)
67984cdea97SKonstantin Belousov 			wakeup_swapper |= thread_unsuspend_one(td2, p, false);
68007a9368aSKonstantin Belousov 		if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR) != 0)
68107a9368aSKonstantin Belousov 			wakeup_swapper |= sleepq_abort(td2, ERESTART);
682917dd390SKonstantin Belousov 		break;
6836ddcc233SKonstantin Belousov 	case SINGLE_ALLPROC:
6846ddcc233SKonstantin Belousov 		/*
6856ddcc233SKonstantin Belousov 		 * ALLPROC suspend tries to avoid spurious EINTR for
6866ddcc233SKonstantin Belousov 		 * threads sleeping interruptable, by suspending the
6876ddcc233SKonstantin Belousov 		 * thread directly, similarly to sig_suspend_threads().
6886ddcc233SKonstantin Belousov 		 * Since such sleep is not performed at the user
6896ddcc233SKonstantin Belousov 		 * boundary, TDF_BOUNDARY flag is not set, and TDF_ALLPROCSUSP
6906ddcc233SKonstantin Belousov 		 * is used to avoid immediate un-suspend.
6916ddcc233SKonstantin Belousov 		 */
6926ddcc233SKonstantin Belousov 		if (TD_IS_SUSPENDED(td2) && (td2->td_flags & (TDF_BOUNDARY |
6936ddcc233SKonstantin Belousov 		    TDF_ALLPROCSUSP)) == 0)
69484cdea97SKonstantin Belousov 			wakeup_swapper |= thread_unsuspend_one(td2, p, false);
6956ddcc233SKonstantin Belousov 		if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR) != 0) {
6966ddcc233SKonstantin Belousov 			if ((td2->td_flags & TDF_SBDRY) == 0) {
6976ddcc233SKonstantin Belousov 				thread_suspend_one(td2);
6986ddcc233SKonstantin Belousov 				td2->td_flags |= TDF_ALLPROCSUSP;
6996ddcc233SKonstantin Belousov 			} else {
7006ddcc233SKonstantin Belousov 				wakeup_swapper |= sleepq_abort(td2, ERESTART);
7016ddcc233SKonstantin Belousov 			}
7026ddcc233SKonstantin Belousov 		}
70307a9368aSKonstantin Belousov 		break;
70407a9368aSKonstantin Belousov 	}
70507a9368aSKonstantin Belousov 	return (wakeup_swapper);
70607a9368aSKonstantin Belousov }
70707a9368aSKonstantin Belousov 
7085215b187SJeff Roberson /*
70944990b8cSJulian Elischer  * Enforce single-threading.
71044990b8cSJulian Elischer  *
71144990b8cSJulian Elischer  * Returns 1 if the caller must abort (another thread is waiting to
71244990b8cSJulian Elischer  * exit the process or similar). Process is locked!
71344990b8cSJulian Elischer  * Returns 0 when you are successfully the only thread running.
71444990b8cSJulian Elischer  * A process has successfully single threaded in the suspend mode when
71544990b8cSJulian Elischer  * There are no threads in user mode. Threads in the kernel must be
71644990b8cSJulian Elischer  * allowed to continue until they get to the user boundary. They may even
71744990b8cSJulian Elischer  * copy out their return values and data before suspending. They may however be
718e2668f55SMaxim Konovalov  * accelerated in reaching the user boundary as we will wake up
71944990b8cSJulian Elischer  * any sleeping threads that are interruptable. (PCATCH).
72044990b8cSJulian Elischer  */
72144990b8cSJulian Elischer int
7226ddcc233SKonstantin Belousov thread_single(struct proc *p, int mode)
72344990b8cSJulian Elischer {
72444990b8cSJulian Elischer 	struct thread *td;
72544990b8cSJulian Elischer 	struct thread *td2;
726da7bbd2cSJohn Baldwin 	int remaining, wakeup_swapper;
72744990b8cSJulian Elischer 
72844990b8cSJulian Elischer 	td = curthread;
7296ddcc233SKonstantin Belousov 	KASSERT(mode == SINGLE_EXIT || mode == SINGLE_BOUNDARY ||
7306ddcc233SKonstantin Belousov 	    mode == SINGLE_ALLPROC || mode == SINGLE_NO_EXIT,
7316ddcc233SKonstantin Belousov 	    ("invalid mode %d", mode));
7326ddcc233SKonstantin Belousov 	/*
7336ddcc233SKonstantin Belousov 	 * If allowing non-ALLPROC singlethreading for non-curproc
7346ddcc233SKonstantin Belousov 	 * callers, calc_remaining() and remain_for_mode() should be
7356ddcc233SKonstantin Belousov 	 * adjusted to also account for td->td_proc != p.  For now
7366ddcc233SKonstantin Belousov 	 * this is not implemented because it is not used.
7376ddcc233SKonstantin Belousov 	 */
7386ddcc233SKonstantin Belousov 	KASSERT((mode == SINGLE_ALLPROC && td->td_proc != p) ||
7396ddcc233SKonstantin Belousov 	    (mode != SINGLE_ALLPROC && td->td_proc == p),
7406ddcc233SKonstantin Belousov 	    ("mode %d proc %p curproc %p", mode, p, td->td_proc));
74137814395SPeter Wemm 	mtx_assert(&Giant, MA_NOTOWNED);
74244990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
74344990b8cSJulian Elischer 
7446ddcc233SKonstantin Belousov 	if ((p->p_flag & P_HADTHREADS) == 0 && mode != SINGLE_ALLPROC)
74544990b8cSJulian Elischer 		return (0);
74644990b8cSJulian Elischer 
747e3b9bf71SJulian Elischer 	/* Is someone already single threading? */
748906ac69dSDavid Xu 	if (p->p_singlethread != NULL && p->p_singlethread != td)
74944990b8cSJulian Elischer 		return (1);
75044990b8cSJulian Elischer 
751906ac69dSDavid Xu 	if (mode == SINGLE_EXIT) {
752906ac69dSDavid Xu 		p->p_flag |= P_SINGLE_EXIT;
753906ac69dSDavid Xu 		p->p_flag &= ~P_SINGLE_BOUNDARY;
754906ac69dSDavid Xu 	} else {
755906ac69dSDavid Xu 		p->p_flag &= ~P_SINGLE_EXIT;
756906ac69dSDavid Xu 		if (mode == SINGLE_BOUNDARY)
757906ac69dSDavid Xu 			p->p_flag |= P_SINGLE_BOUNDARY;
758906ac69dSDavid Xu 		else
759906ac69dSDavid Xu 			p->p_flag &= ~P_SINGLE_BOUNDARY;
760906ac69dSDavid Xu 	}
7616ddcc233SKonstantin Belousov 	if (mode == SINGLE_ALLPROC)
7626ddcc233SKonstantin Belousov 		p->p_flag |= P_TOTAL_STOP;
7631279572aSDavid Xu 	p->p_flag |= P_STOPPED_SINGLE;
7647b4a950aSDavid Xu 	PROC_SLOCK(p);
765112afcb2SJohn Baldwin 	p->p_singlethread = td;
76679799053SKonstantin Belousov 	remaining = calc_remaining(p, mode);
76707a9368aSKonstantin Belousov 	while (remaining != remain_for_mode(mode)) {
768bf1a3220SDavid Xu 		if (P_SHOULDSTOP(p) != P_STOPPED_SINGLE)
769bf1a3220SDavid Xu 			goto stopme;
770da7bbd2cSJohn Baldwin 		wakeup_swapper = 0;
77144990b8cSJulian Elischer 		FOREACH_THREAD_IN_PROC(p, td2) {
77244990b8cSJulian Elischer 			if (td2 == td)
77344990b8cSJulian Elischer 				continue;
774a54e85fdSJeff Roberson 			thread_lock(td2);
775b7edba77SJeff Roberson 			td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
7766ddcc233SKonstantin Belousov 			if (TD_IS_INHIBITED(td2)) {
77707a9368aSKonstantin Belousov 				wakeup_swapper |= weed_inhib(mode, td2, p);
778d8267df7SDavid Xu #ifdef SMP
7796ddcc233SKonstantin Belousov 			} else if (TD_IS_RUNNING(td2) && td != td2) {
780d8267df7SDavid Xu 				forward_signal(td2);
781d8267df7SDavid Xu #endif
7826ddcc233SKonstantin Belousov 			}
783a54e85fdSJeff Roberson 			thread_unlock(td2);
7849d102777SJulian Elischer 		}
785da7bbd2cSJohn Baldwin 		if (wakeup_swapper)
786da7bbd2cSJohn Baldwin 			kick_proc0();
78779799053SKonstantin Belousov 		remaining = calc_remaining(p, mode);
788ec008e96SDavid Xu 
7899d102777SJulian Elischer 		/*
7909d102777SJulian Elischer 		 * Maybe we suspended some threads.. was it enough?
7919d102777SJulian Elischer 		 */
79207a9368aSKonstantin Belousov 		if (remaining == remain_for_mode(mode))
7939d102777SJulian Elischer 			break;
7949d102777SJulian Elischer 
795bf1a3220SDavid Xu stopme:
79644990b8cSJulian Elischer 		/*
79744990b8cSJulian Elischer 		 * Wake us up when everyone else has suspended.
798e3b9bf71SJulian Elischer 		 * In the mean time we suspend as well.
79944990b8cSJulian Elischer 		 */
8006ddcc233SKonstantin Belousov 		thread_suspend_switch(td, p);
80179799053SKonstantin Belousov 		remaining = calc_remaining(p, mode);
80244990b8cSJulian Elischer 	}
803906ac69dSDavid Xu 	if (mode == SINGLE_EXIT) {
80491599697SJulian Elischer 		/*
8058626a0ddSKonstantin Belousov 		 * Convert the process to an unthreaded process.  The
8068626a0ddSKonstantin Belousov 		 * SINGLE_EXIT is called by exit1() or execve(), in
8078626a0ddSKonstantin Belousov 		 * both cases other threads must be retired.
80891599697SJulian Elischer 		 */
8098626a0ddSKonstantin Belousov 		KASSERT(p->p_numthreads == 1, ("Unthreading with >1 threads"));
810ed062c8dSJulian Elischer 		p->p_singlethread = NULL;
8118626a0ddSKonstantin Belousov 		p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_HADTHREADS);
812fd229b5bSKonstantin Belousov 
813fd229b5bSKonstantin Belousov 		/*
814fd229b5bSKonstantin Belousov 		 * Wait for any remaining threads to exit cpu_throw().
815fd229b5bSKonstantin Belousov 		 */
816fd229b5bSKonstantin Belousov 		while (p->p_exitthreads != 0) {
817fd229b5bSKonstantin Belousov 			PROC_SUNLOCK(p);
818fd229b5bSKonstantin Belousov 			PROC_UNLOCK(p);
819fd229b5bSKonstantin Belousov 			sched_relinquish(td);
820fd229b5bSKonstantin Belousov 			PROC_LOCK(p);
821fd229b5bSKonstantin Belousov 			PROC_SLOCK(p);
822fd229b5bSKonstantin Belousov 		}
823ac437c07SKonstantin Belousov 	} else if (mode == SINGLE_BOUNDARY) {
824ac437c07SKonstantin Belousov 		/*
825ac437c07SKonstantin Belousov 		 * Wait until all suspended threads are removed from
826ac437c07SKonstantin Belousov 		 * the processors.  The thread_suspend_check()
827ac437c07SKonstantin Belousov 		 * increments p_boundary_count while it is still
828ac437c07SKonstantin Belousov 		 * running, which makes it possible for the execve()
829ac437c07SKonstantin Belousov 		 * to destroy vmspace while our other threads are
830ac437c07SKonstantin Belousov 		 * still using the address space.
831ac437c07SKonstantin Belousov 		 *
832ac437c07SKonstantin Belousov 		 * We lock the thread, which is only allowed to
833ac437c07SKonstantin Belousov 		 * succeed after context switch code finished using
834ac437c07SKonstantin Belousov 		 * the address space.
835ac437c07SKonstantin Belousov 		 */
836ac437c07SKonstantin Belousov 		FOREACH_THREAD_IN_PROC(p, td2) {
837ac437c07SKonstantin Belousov 			if (td2 == td)
838ac437c07SKonstantin Belousov 				continue;
839ac437c07SKonstantin Belousov 			thread_lock(td2);
840ac437c07SKonstantin Belousov 			KASSERT((td2->td_flags & TDF_BOUNDARY) != 0,
841ac437c07SKonstantin Belousov 			    ("td %p not on boundary", td2));
842ac437c07SKonstantin Belousov 			KASSERT(TD_IS_SUSPENDED(td2),
843ac437c07SKonstantin Belousov 			    ("td %p is not suspended", td2));
844ac437c07SKonstantin Belousov 			thread_unlock(td2);
845ac437c07SKonstantin Belousov 		}
84691599697SJulian Elischer 	}
8477b4a950aSDavid Xu 	PROC_SUNLOCK(p);
84844990b8cSJulian Elischer 	return (0);
84944990b8cSJulian Elischer }
85044990b8cSJulian Elischer 
8518638fe7bSKonstantin Belousov bool
8528638fe7bSKonstantin Belousov thread_suspend_check_needed(void)
8538638fe7bSKonstantin Belousov {
8548638fe7bSKonstantin Belousov 	struct proc *p;
8558638fe7bSKonstantin Belousov 	struct thread *td;
8568638fe7bSKonstantin Belousov 
8578638fe7bSKonstantin Belousov 	td = curthread;
8588638fe7bSKonstantin Belousov 	p = td->td_proc;
8598638fe7bSKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
8608638fe7bSKonstantin Belousov 	return (P_SHOULDSTOP(p) || ((p->p_flag & P_TRACED) != 0 &&
8618638fe7bSKonstantin Belousov 	    (td->td_dbgflags & TDB_SUSPEND) != 0));
8628638fe7bSKonstantin Belousov }
8638638fe7bSKonstantin Belousov 
86444990b8cSJulian Elischer /*
86544990b8cSJulian Elischer  * Called in from locations that can safely check to see
86644990b8cSJulian Elischer  * whether we have to suspend or at least throttle for a
86744990b8cSJulian Elischer  * single-thread event (e.g. fork).
86844990b8cSJulian Elischer  *
86944990b8cSJulian Elischer  * Such locations include userret().
87044990b8cSJulian Elischer  * If the "return_instead" argument is non zero, the thread must be able to
87144990b8cSJulian Elischer  * accept 0 (caller may continue), or 1 (caller must abort) as a result.
87244990b8cSJulian Elischer  *
87344990b8cSJulian Elischer  * The 'return_instead' argument tells the function if it may do a
87444990b8cSJulian Elischer  * thread_exit() or suspend, or whether the caller must abort and back
87544990b8cSJulian Elischer  * out instead.
87644990b8cSJulian Elischer  *
87744990b8cSJulian Elischer  * If the thread that set the single_threading request has set the
87844990b8cSJulian Elischer  * P_SINGLE_EXIT bit in the process flags then this call will never return
87944990b8cSJulian Elischer  * if 'return_instead' is false, but will exit.
88044990b8cSJulian Elischer  *
88144990b8cSJulian Elischer  * P_SINGLE_EXIT | return_instead == 0| return_instead != 0
88244990b8cSJulian Elischer  *---------------+--------------------+---------------------
88344990b8cSJulian Elischer  *       0       | returns 0          |   returns 0 or 1
884353374b5SJohn Baldwin  *               | when ST ends       |   immediately
88544990b8cSJulian Elischer  *---------------+--------------------+---------------------
88644990b8cSJulian Elischer  *       1       | thread exits       |   returns 1
887353374b5SJohn Baldwin  *               |                    |  immediately
88844990b8cSJulian Elischer  * 0 = thread_exit() or suspension ok,
88944990b8cSJulian Elischer  * other = return error instead of stopping the thread.
89044990b8cSJulian Elischer  *
89144990b8cSJulian Elischer  * While a full suspension is under effect, even a single threading
89244990b8cSJulian Elischer  * thread would be suspended if it made this call (but it shouldn't).
89344990b8cSJulian Elischer  * This call should only be made from places where
89444990b8cSJulian Elischer  * thread_exit() would be safe as that may be the outcome unless
89544990b8cSJulian Elischer  * return_instead is set.
89644990b8cSJulian Elischer  */
89744990b8cSJulian Elischer int
89844990b8cSJulian Elischer thread_suspend_check(int return_instead)
89944990b8cSJulian Elischer {
900ecafb24bSJuli Mallett 	struct thread *td;
901ecafb24bSJuli Mallett 	struct proc *p;
9027847a9daSJohn Baldwin 	int wakeup_swapper;
90344990b8cSJulian Elischer 
90444990b8cSJulian Elischer 	td = curthread;
90544990b8cSJulian Elischer 	p = td->td_proc;
90637814395SPeter Wemm 	mtx_assert(&Giant, MA_NOTOWNED);
90744990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
9088638fe7bSKonstantin Belousov 	while (thread_suspend_check_needed()) {
9091279572aSDavid Xu 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
91044990b8cSJulian Elischer 			KASSERT(p->p_singlethread != NULL,
91144990b8cSJulian Elischer 			    ("singlethread not set"));
91244990b8cSJulian Elischer 			/*
913e3b9bf71SJulian Elischer 			 * The only suspension in action is a
914e3b9bf71SJulian Elischer 			 * single-threading. Single threader need not stop.
915b6d5995eSJulian Elischer 			 * XXX Should be safe to access unlocked
916b6d5995eSJulian Elischer 			 * as it can only be set to be true by us.
91744990b8cSJulian Elischer 			 */
918e3b9bf71SJulian Elischer 			if (p->p_singlethread == td)
91944990b8cSJulian Elischer 				return (0);	/* Exempt from stopping. */
92044990b8cSJulian Elischer 		}
92145a4bfa1SDavid Xu 		if ((p->p_flag & P_SINGLE_EXIT) && return_instead)
92294f0972bSDavid Xu 			return (EINTR);
92344990b8cSJulian Elischer 
924906ac69dSDavid Xu 		/* Should we goto user boundary if we didn't come from there? */
925906ac69dSDavid Xu 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE &&
926906ac69dSDavid Xu 		    (p->p_flag & P_SINGLE_BOUNDARY) && return_instead)
92794f0972bSDavid Xu 			return (ERESTART);
928906ac69dSDavid Xu 
92944990b8cSJulian Elischer 		/*
9303077f938SKonstantin Belousov 		 * Ignore suspend requests if they are deferred.
931d071a6faSJohn Baldwin 		 */
9323077f938SKonstantin Belousov 		if ((td->td_flags & TDF_SBDRY) != 0) {
933d071a6faSJohn Baldwin 			KASSERT(return_instead,
934d071a6faSJohn Baldwin 			    ("TDF_SBDRY set for unsafe thread_suspend_check"));
935d071a6faSJohn Baldwin 			return (0);
936d071a6faSJohn Baldwin 		}
937d071a6faSJohn Baldwin 
938d071a6faSJohn Baldwin 		/*
93944990b8cSJulian Elischer 		 * If the process is waiting for us to exit,
94044990b8cSJulian Elischer 		 * this thread should just suicide.
9411279572aSDavid Xu 		 * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE.
94244990b8cSJulian Elischer 		 */
943cf7d9a8cSDavid Xu 		if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) {
944cf7d9a8cSDavid Xu 			PROC_UNLOCK(p);
945cf7d9a8cSDavid Xu 			tidhash_remove(td);
94691d1786fSDmitry Chagin 
94791d1786fSDmitry Chagin 			/*
94891d1786fSDmitry Chagin 			 * Allow Linux emulation layer to do some work
94991d1786fSDmitry Chagin 			 * before thread suicide.
95091d1786fSDmitry Chagin 			 */
95191d1786fSDmitry Chagin 			if (__predict_false(p->p_sysent->sv_thread_detach != NULL))
95291d1786fSDmitry Chagin 				(p->p_sysent->sv_thread_detach)(td);
95391d1786fSDmitry Chagin 
954cf7d9a8cSDavid Xu 			PROC_LOCK(p);
95521ecd1e9SDavid Xu 			tdsigcleanup(td);
95613dad108SKonstantin Belousov 			umtx_thread_exit(td);
957cf7d9a8cSDavid Xu 			PROC_SLOCK(p);
95821ecd1e9SDavid Xu 			thread_stopped(p);
95944990b8cSJulian Elischer 			thread_exit();
960cf7d9a8cSDavid Xu 		}
96121ecd1e9SDavid Xu 
96221ecd1e9SDavid Xu 		PROC_SLOCK(p);
96321ecd1e9SDavid Xu 		thread_stopped(p);
964a54e85fdSJeff Roberson 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
965a54e85fdSJeff Roberson 			if (p->p_numthreads == p->p_suspcount + 1) {
966a54e85fdSJeff Roberson 				thread_lock(p->p_singlethread);
96784cdea97SKonstantin Belousov 				wakeup_swapper = thread_unsuspend_one(
96884cdea97SKonstantin Belousov 				    p->p_singlethread, p, false);
969a54e85fdSJeff Roberson 				thread_unlock(p->p_singlethread);
9707847a9daSJohn Baldwin 				if (wakeup_swapper)
9717847a9daSJohn Baldwin 					kick_proc0();
972a54e85fdSJeff Roberson 			}
973a54e85fdSJeff Roberson 		}
9743f9be10eSDavid Xu 		PROC_UNLOCK(p);
9757b4a950aSDavid Xu 		thread_lock(td);
97644990b8cSJulian Elischer 		/*
97744990b8cSJulian Elischer 		 * When a thread suspends, it just
978ad1e7d28SJulian Elischer 		 * gets taken off all queues.
97944990b8cSJulian Elischer 		 */
98071fad9fdSJulian Elischer 		thread_suspend_one(td);
981906ac69dSDavid Xu 		if (return_instead == 0) {
982906ac69dSDavid Xu 			p->p_boundary_count++;
983906ac69dSDavid Xu 			td->td_flags |= TDF_BOUNDARY;
984cf19bf91SJulian Elischer 		}
9857b4a950aSDavid Xu 		PROC_SUNLOCK(p);
9868df78c41SJeff Roberson 		mi_switch(SW_INVOL | SWT_SUSPEND, NULL);
987a54e85fdSJeff Roberson 		thread_unlock(td);
98844990b8cSJulian Elischer 		PROC_LOCK(p);
98944990b8cSJulian Elischer 	}
99044990b8cSJulian Elischer 	return (0);
99144990b8cSJulian Elischer }
99244990b8cSJulian Elischer 
99335c32a76SDavid Xu void
9946ddcc233SKonstantin Belousov thread_suspend_switch(struct thread *td, struct proc *p)
995a54e85fdSJeff Roberson {
996a54e85fdSJeff Roberson 
997a54e85fdSJeff Roberson 	KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
998a54e85fdSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
9997b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
1000a54e85fdSJeff Roberson 	/*
1001a54e85fdSJeff Roberson 	 * We implement thread_suspend_one in stages here to avoid
1002a54e85fdSJeff Roberson 	 * dropping the proc lock while the thread lock is owned.
1003a54e85fdSJeff Roberson 	 */
10046ddcc233SKonstantin Belousov 	if (p == td->td_proc) {
1005a54e85fdSJeff Roberson 		thread_stopped(p);
1006a54e85fdSJeff Roberson 		p->p_suspcount++;
10076ddcc233SKonstantin Belousov 	}
10083f9be10eSDavid Xu 	PROC_UNLOCK(p);
10097b4a950aSDavid Xu 	thread_lock(td);
1010b7edba77SJeff Roberson 	td->td_flags &= ~TDF_NEEDSUSPCHK;
1011a54e85fdSJeff Roberson 	TD_SET_SUSPENDED(td);
1012c5aa6b58SJeff Roberson 	sched_sleep(td, 0);
10137b4a950aSDavid Xu 	PROC_SUNLOCK(p);
1014a54e85fdSJeff Roberson 	DROP_GIANT();
10158df78c41SJeff Roberson 	mi_switch(SW_VOL | SWT_SUSPEND, NULL);
1016a54e85fdSJeff Roberson 	thread_unlock(td);
1017a54e85fdSJeff Roberson 	PICKUP_GIANT();
1018a54e85fdSJeff Roberson 	PROC_LOCK(p);
10197b4a950aSDavid Xu 	PROC_SLOCK(p);
1020a54e85fdSJeff Roberson }
1021a54e85fdSJeff Roberson 
1022a54e85fdSJeff Roberson void
102335c32a76SDavid Xu thread_suspend_one(struct thread *td)
102435c32a76SDavid Xu {
10256ddcc233SKonstantin Belousov 	struct proc *p;
102635c32a76SDavid Xu 
10276ddcc233SKonstantin Belousov 	p = td->td_proc;
10287b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
1029a54e85fdSJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1030e574e444SDavid Xu 	KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
103135c32a76SDavid Xu 	p->p_suspcount++;
1032b7edba77SJeff Roberson 	td->td_flags &= ~TDF_NEEDSUSPCHK;
103371fad9fdSJulian Elischer 	TD_SET_SUSPENDED(td);
1034c5aa6b58SJeff Roberson 	sched_sleep(td, 0);
103535c32a76SDavid Xu }
103635c32a76SDavid Xu 
103784cdea97SKonstantin Belousov static int
103884cdea97SKonstantin Belousov thread_unsuspend_one(struct thread *td, struct proc *p, bool boundary)
103935c32a76SDavid Xu {
104035c32a76SDavid Xu 
1041a54e85fdSJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1042ad1e7d28SJulian Elischer 	KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended"));
104371fad9fdSJulian Elischer 	TD_CLR_SUSPENDED(td);
10446ddcc233SKonstantin Belousov 	td->td_flags &= ~TDF_ALLPROCSUSP;
10456ddcc233SKonstantin Belousov 	if (td->td_proc == p) {
10466ddcc233SKonstantin Belousov 		PROC_SLOCK_ASSERT(p, MA_OWNED);
104735c32a76SDavid Xu 		p->p_suspcount--;
104884cdea97SKonstantin Belousov 		if (boundary && (td->td_flags & TDF_BOUNDARY) != 0) {
104984cdea97SKonstantin Belousov 			td->td_flags &= ~TDF_BOUNDARY;
105084cdea97SKonstantin Belousov 			p->p_boundary_count--;
105184cdea97SKonstantin Belousov 		}
10526ddcc233SKonstantin Belousov 	}
10537847a9daSJohn Baldwin 	return (setrunnable(td));
105435c32a76SDavid Xu }
105535c32a76SDavid Xu 
105644990b8cSJulian Elischer /*
105744990b8cSJulian Elischer  * Allow all threads blocked by single threading to continue running.
105844990b8cSJulian Elischer  */
105944990b8cSJulian Elischer void
106044990b8cSJulian Elischer thread_unsuspend(struct proc *p)
106144990b8cSJulian Elischer {
106244990b8cSJulian Elischer 	struct thread *td;
10637847a9daSJohn Baldwin 	int wakeup_swapper;
106444990b8cSJulian Elischer 
106544990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
10667b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
10677847a9daSJohn Baldwin 	wakeup_swapper = 0;
106844990b8cSJulian Elischer 	if (!P_SHOULDSTOP(p)) {
1069ad1e7d28SJulian Elischer                 FOREACH_THREAD_IN_PROC(p, td) {
1070a54e85fdSJeff Roberson 			thread_lock(td);
1071ad1e7d28SJulian Elischer 			if (TD_IS_SUSPENDED(td)) {
107284cdea97SKonstantin Belousov 				wakeup_swapper |= thread_unsuspend_one(td, p,
107384cdea97SKonstantin Belousov 				    true);
107444990b8cSJulian Elischer 			}
1075a54e85fdSJeff Roberson 			thread_unlock(td);
1076ad1e7d28SJulian Elischer 		}
107784cdea97SKonstantin Belousov 	} else if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE &&
107884cdea97SKonstantin Belousov 	    p->p_numthreads == p->p_suspcount) {
107944990b8cSJulian Elischer 		/*
108044990b8cSJulian Elischer 		 * Stopping everything also did the job for the single
108144990b8cSJulian Elischer 		 * threading request. Now we've downgraded to single-threaded,
108244990b8cSJulian Elischer 		 * let it continue.
108344990b8cSJulian Elischer 		 */
10846ddcc233SKonstantin Belousov 		if (p->p_singlethread->td_proc == p) {
1085a54e85fdSJeff Roberson 			thread_lock(p->p_singlethread);
10866ddcc233SKonstantin Belousov 			wakeup_swapper = thread_unsuspend_one(
108784cdea97SKonstantin Belousov 			    p->p_singlethread, p, false);
1088a54e85fdSJeff Roberson 			thread_unlock(p->p_singlethread);
108944990b8cSJulian Elischer 		}
10906ddcc233SKonstantin Belousov 	}
10917847a9daSJohn Baldwin 	if (wakeup_swapper)
10927847a9daSJohn Baldwin 		kick_proc0();
109344990b8cSJulian Elischer }
109444990b8cSJulian Elischer 
1095ed062c8dSJulian Elischer /*
1096ed062c8dSJulian Elischer  * End the single threading mode..
1097ed062c8dSJulian Elischer  */
109844990b8cSJulian Elischer void
10996ddcc233SKonstantin Belousov thread_single_end(struct proc *p, int mode)
110044990b8cSJulian Elischer {
110144990b8cSJulian Elischer 	struct thread *td;
11027847a9daSJohn Baldwin 	int wakeup_swapper;
110344990b8cSJulian Elischer 
11046ddcc233SKonstantin Belousov 	KASSERT(mode == SINGLE_EXIT || mode == SINGLE_BOUNDARY ||
11056ddcc233SKonstantin Belousov 	    mode == SINGLE_ALLPROC || mode == SINGLE_NO_EXIT,
11066ddcc233SKonstantin Belousov 	    ("invalid mode %d", mode));
110744990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
11086ddcc233SKonstantin Belousov 	KASSERT((mode == SINGLE_ALLPROC && (p->p_flag & P_TOTAL_STOP) != 0) ||
11096ddcc233SKonstantin Belousov 	    (mode != SINGLE_ALLPROC && (p->p_flag & P_TOTAL_STOP) == 0),
11106ddcc233SKonstantin Belousov 	    ("mode %d does not match P_TOTAL_STOP", mode));
111184cdea97SKonstantin Belousov 	KASSERT(mode == SINGLE_ALLPROC || p->p_singlethread == curthread,
111284cdea97SKonstantin Belousov 	    ("thread_single_end from other thread %p %p",
111384cdea97SKonstantin Belousov 	    curthread, p->p_singlethread));
111484cdea97SKonstantin Belousov 	KASSERT(mode != SINGLE_BOUNDARY ||
111584cdea97SKonstantin Belousov 	    (p->p_flag & P_SINGLE_BOUNDARY) != 0,
111684cdea97SKonstantin Belousov 	    ("mis-matched SINGLE_BOUNDARY flags %x", p->p_flag));
11176ddcc233SKonstantin Belousov 	p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_SINGLE_BOUNDARY |
11186ddcc233SKonstantin Belousov 	    P_TOTAL_STOP);
11197b4a950aSDavid Xu 	PROC_SLOCK(p);
112044990b8cSJulian Elischer 	p->p_singlethread = NULL;
11217847a9daSJohn Baldwin 	wakeup_swapper = 0;
112249539972SJulian Elischer 	/*
11237847a9daSJohn Baldwin 	 * If there are other threads they may now run,
112449539972SJulian Elischer 	 * unless of course there is a blanket 'stop order'
112549539972SJulian Elischer 	 * on the process. The single threader must be allowed
112649539972SJulian Elischer 	 * to continue however as this is a bad place to stop.
112749539972SJulian Elischer 	 */
11286ddcc233SKonstantin Belousov 	if (p->p_numthreads != remain_for_mode(mode) && !P_SHOULDSTOP(p)) {
1129ad1e7d28SJulian Elischer                 FOREACH_THREAD_IN_PROC(p, td) {
1130a54e85fdSJeff Roberson 			thread_lock(td);
1131ad1e7d28SJulian Elischer 			if (TD_IS_SUSPENDED(td)) {
113284cdea97SKonstantin Belousov 				wakeup_swapper |= thread_unsuspend_one(td, p,
113384cdea97SKonstantin Belousov 				    mode == SINGLE_BOUNDARY);
113444990b8cSJulian Elischer 			}
1135a54e85fdSJeff Roberson 			thread_unlock(td);
113649539972SJulian Elischer 		}
1137ad1e7d28SJulian Elischer 	}
113884cdea97SKonstantin Belousov 	KASSERT(mode != SINGLE_BOUNDARY || p->p_boundary_count == 0,
113984cdea97SKonstantin Belousov 	    ("inconsistent boundary count %d", p->p_boundary_count));
11407b4a950aSDavid Xu 	PROC_SUNLOCK(p);
11417847a9daSJohn Baldwin 	if (wakeup_swapper)
11427847a9daSJohn Baldwin 		kick_proc0();
114349539972SJulian Elischer }
11444fc21c09SDaniel Eischen 
114544355392SDavid Xu struct thread *
114644355392SDavid Xu thread_find(struct proc *p, lwpid_t tid)
114744355392SDavid Xu {
114844355392SDavid Xu 	struct thread *td;
114944355392SDavid Xu 
115044355392SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
115144355392SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td) {
115244355392SDavid Xu 		if (td->td_tid == tid)
115344355392SDavid Xu 			break;
115444355392SDavid Xu 	}
115544355392SDavid Xu 	return (td);
115644355392SDavid Xu }
1157cf7d9a8cSDavid Xu 
1158cf7d9a8cSDavid Xu /* Locate a thread by number; return with proc lock held. */
1159cf7d9a8cSDavid Xu struct thread *
1160cf7d9a8cSDavid Xu tdfind(lwpid_t tid, pid_t pid)
1161cf7d9a8cSDavid Xu {
1162cf7d9a8cSDavid Xu #define RUN_THRESH	16
1163cf7d9a8cSDavid Xu 	struct thread *td;
1164cf7d9a8cSDavid Xu 	int run = 0;
1165cf7d9a8cSDavid Xu 
1166cf7d9a8cSDavid Xu 	rw_rlock(&tidhash_lock);
1167cf7d9a8cSDavid Xu 	LIST_FOREACH(td, TIDHASH(tid), td_hash) {
1168cf7d9a8cSDavid Xu 		if (td->td_tid == tid) {
1169cf7d9a8cSDavid Xu 			if (pid != -1 && td->td_proc->p_pid != pid) {
1170cf7d9a8cSDavid Xu 				td = NULL;
1171cf7d9a8cSDavid Xu 				break;
1172cf7d9a8cSDavid Xu 			}
11738e6fa660SJohn Baldwin 			PROC_LOCK(td->td_proc);
1174cf7d9a8cSDavid Xu 			if (td->td_proc->p_state == PRS_NEW) {
11758e6fa660SJohn Baldwin 				PROC_UNLOCK(td->td_proc);
1176cf7d9a8cSDavid Xu 				td = NULL;
1177cf7d9a8cSDavid Xu 				break;
1178cf7d9a8cSDavid Xu 			}
1179cf7d9a8cSDavid Xu 			if (run > RUN_THRESH) {
1180cf7d9a8cSDavid Xu 				if (rw_try_upgrade(&tidhash_lock)) {
1181cf7d9a8cSDavid Xu 					LIST_REMOVE(td, td_hash);
1182cf7d9a8cSDavid Xu 					LIST_INSERT_HEAD(TIDHASH(td->td_tid),
1183cf7d9a8cSDavid Xu 						td, td_hash);
1184cf7d9a8cSDavid Xu 					rw_wunlock(&tidhash_lock);
1185cf7d9a8cSDavid Xu 					return (td);
1186cf7d9a8cSDavid Xu 				}
1187cf7d9a8cSDavid Xu 			}
1188cf7d9a8cSDavid Xu 			break;
1189cf7d9a8cSDavid Xu 		}
1190cf7d9a8cSDavid Xu 		run++;
1191cf7d9a8cSDavid Xu 	}
1192cf7d9a8cSDavid Xu 	rw_runlock(&tidhash_lock);
1193cf7d9a8cSDavid Xu 	return (td);
1194cf7d9a8cSDavid Xu }
1195cf7d9a8cSDavid Xu 
1196cf7d9a8cSDavid Xu void
1197cf7d9a8cSDavid Xu tidhash_add(struct thread *td)
1198cf7d9a8cSDavid Xu {
1199cf7d9a8cSDavid Xu 	rw_wlock(&tidhash_lock);
1200cf7d9a8cSDavid Xu 	LIST_INSERT_HEAD(TIDHASH(td->td_tid), td, td_hash);
1201cf7d9a8cSDavid Xu 	rw_wunlock(&tidhash_lock);
1202cf7d9a8cSDavid Xu }
1203cf7d9a8cSDavid Xu 
1204cf7d9a8cSDavid Xu void
1205cf7d9a8cSDavid Xu tidhash_remove(struct thread *td)
1206cf7d9a8cSDavid Xu {
1207cf7d9a8cSDavid Xu 	rw_wlock(&tidhash_lock);
1208cf7d9a8cSDavid Xu 	LIST_REMOVE(td, td_hash);
1209cf7d9a8cSDavid Xu 	rw_wunlock(&tidhash_lock);
1210cf7d9a8cSDavid Xu }
1211