xref: /freebsd/sys/kern/kern_thread.c (revision 4ea6a9a28fd7ae3cc6e8697bc93f9ce5ea6b6262)
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>
63b209f889SRandall Stewart #include <sys/eventhandler.h>
6402fb42b0SPeter Wemm 
65b3e9e682SRyan Stone SDT_PROVIDER_DECLARE(proc);
66d9fae5abSAndriy Gapon SDT_PROBE_DEFINE(proc, , , lwp__exit);
67b3e9e682SRyan Stone 
688460a577SJohn Birrell /*
698460a577SJohn Birrell  * thread related storage.
708460a577SJohn Birrell  */
7144990b8cSJulian Elischer static uma_zone_t thread_zone;
7244990b8cSJulian Elischer 
735215b187SJeff Roberson TAILQ_HEAD(, thread) zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads);
74c8790f5dSAttilio Rao static struct mtx zombie_lock;
75a54e85fdSJeff Roberson MTX_SYSINIT(zombie_lock, &zombie_lock, "zombie lock", MTX_SPIN);
7644990b8cSJulian Elischer 
77ff8fbcffSJeff Roberson static void thread_zombie(struct thread *);
7884cdea97SKonstantin Belousov static int thread_unsuspend_one(struct thread *td, struct proc *p,
7984cdea97SKonstantin Belousov     bool boundary);
80ff8fbcffSJeff Roberson 
81ec6ea5e8SDavid Xu #define TID_BUFFER_SIZE	1024
82ec6ea5e8SDavid Xu 
83fdcac928SMarcel Moolenaar struct mtx tid_lock;
841ea7a6f8SPoul-Henning Kamp static struct unrhdr *tid_unrhdr;
85ec6ea5e8SDavid Xu static lwpid_t tid_buffer[TID_BUFFER_SIZE];
86ec6ea5e8SDavid Xu static int tid_head, tid_tail;
87cf7d9a8cSDavid Xu static MALLOC_DEFINE(M_TIDHASH, "tidhash", "thread hash");
88cf7d9a8cSDavid Xu 
89cf7d9a8cSDavid Xu struct	tidhashhead *tidhashtbl;
90cf7d9a8cSDavid Xu u_long	tidhash;
91cf7d9a8cSDavid Xu struct	rwlock tidhash_lock;
92cf7d9a8cSDavid Xu 
93ec6ea5e8SDavid Xu static lwpid_t
94ec6ea5e8SDavid Xu tid_alloc(void)
95ec6ea5e8SDavid Xu {
96ec6ea5e8SDavid Xu 	lwpid_t	tid;
97ec6ea5e8SDavid Xu 
98ec6ea5e8SDavid Xu 	tid = alloc_unr(tid_unrhdr);
99ec6ea5e8SDavid Xu 	if (tid != -1)
100ec6ea5e8SDavid Xu 		return (tid);
101ec6ea5e8SDavid Xu 	mtx_lock(&tid_lock);
102ec6ea5e8SDavid Xu 	if (tid_head == tid_tail) {
103ec6ea5e8SDavid Xu 		mtx_unlock(&tid_lock);
104ec6ea5e8SDavid Xu 		return (-1);
105ec6ea5e8SDavid Xu 	}
10694cb3545SKonstantin Belousov 	tid = tid_buffer[tid_head];
10794cb3545SKonstantin Belousov 	tid_head = (tid_head + 1) % TID_BUFFER_SIZE;
108ec6ea5e8SDavid Xu 	mtx_unlock(&tid_lock);
109ec6ea5e8SDavid Xu 	return (tid);
110ec6ea5e8SDavid Xu }
111ec6ea5e8SDavid Xu 
112ec6ea5e8SDavid Xu static void
113ec6ea5e8SDavid Xu tid_free(lwpid_t tid)
114ec6ea5e8SDavid Xu {
115ec6ea5e8SDavid Xu 	lwpid_t tmp_tid = -1;
116ec6ea5e8SDavid Xu 
117ec6ea5e8SDavid Xu 	mtx_lock(&tid_lock);
118ec6ea5e8SDavid Xu 	if ((tid_tail + 1) % TID_BUFFER_SIZE == tid_head) {
11994cb3545SKonstantin Belousov 		tmp_tid = tid_buffer[tid_head];
12094cb3545SKonstantin Belousov 		tid_head = (tid_head + 1) % TID_BUFFER_SIZE;
121ec6ea5e8SDavid Xu 	}
12294cb3545SKonstantin Belousov 	tid_buffer[tid_tail] = tid;
12394cb3545SKonstantin Belousov 	tid_tail = (tid_tail + 1) % TID_BUFFER_SIZE;
124ec6ea5e8SDavid Xu 	mtx_unlock(&tid_lock);
125ec6ea5e8SDavid Xu 	if (tmp_tid != -1)
126ec6ea5e8SDavid Xu 		free_unr(tid_unrhdr, tmp_tid);
127ec6ea5e8SDavid Xu }
128ec6ea5e8SDavid Xu 
129fdcac928SMarcel Moolenaar /*
130696058c3SJulian Elischer  * Prepare a thread for use.
13144990b8cSJulian Elischer  */
132b23f72e9SBrian Feldman static int
133b23f72e9SBrian Feldman thread_ctor(void *mem, int size, void *arg, int flags)
13444990b8cSJulian Elischer {
13544990b8cSJulian Elischer 	struct thread	*td;
13644990b8cSJulian Elischer 
13744990b8cSJulian Elischer 	td = (struct thread *)mem;
13871fad9fdSJulian Elischer 	td->td_state = TDS_INACTIVE;
139060563ecSJulian Elischer 	td->td_oncpu = NOCPU;
1406c27c603SJuli Mallett 
141ec6ea5e8SDavid Xu 	td->td_tid = tid_alloc();
142773eff9dSPoul-Henning Kamp 
1436c27c603SJuli Mallett 	/*
1446c27c603SJuli Mallett 	 * Note that td_critnest begins life as 1 because the thread is not
1456c27c603SJuli Mallett 	 * running and is thereby implicitly waiting to be on the receiving
146a54e85fdSJeff Roberson 	 * end of a context switch.
1476c27c603SJuli Mallett 	 */
148139b7550SJohn Baldwin 	td->td_critnest = 1;
149acbe332aSDavid Xu 	td->td_lend_user_pri = PRI_MAX;
150b209f889SRandall Stewart 	EVENTHANDLER_INVOKE(thread_ctor, td);
151911b84b0SRobert Watson #ifdef AUDIT
152911b84b0SRobert Watson 	audit_thread_alloc(td);
153911b84b0SRobert Watson #endif
154d10183d9SDavid Xu 	umtx_thread_alloc(td);
155b23f72e9SBrian Feldman 	return (0);
15644990b8cSJulian Elischer }
15744990b8cSJulian Elischer 
15844990b8cSJulian Elischer /*
15944990b8cSJulian Elischer  * Reclaim a thread after use.
16044990b8cSJulian Elischer  */
16144990b8cSJulian Elischer static void
16244990b8cSJulian Elischer thread_dtor(void *mem, int size, void *arg)
16344990b8cSJulian Elischer {
16444990b8cSJulian Elischer 	struct thread *td;
16544990b8cSJulian Elischer 
16644990b8cSJulian Elischer 	td = (struct thread *)mem;
16744990b8cSJulian Elischer 
16844990b8cSJulian Elischer #ifdef INVARIANTS
16944990b8cSJulian Elischer 	/* Verify that this thread is in a safe state to free. */
17044990b8cSJulian Elischer 	switch (td->td_state) {
17171fad9fdSJulian Elischer 	case TDS_INHIBITED:
17271fad9fdSJulian Elischer 	case TDS_RUNNING:
17371fad9fdSJulian Elischer 	case TDS_CAN_RUN:
17444990b8cSJulian Elischer 	case TDS_RUNQ:
17544990b8cSJulian Elischer 		/*
17644990b8cSJulian Elischer 		 * We must never unlink a thread that is in one of
17744990b8cSJulian Elischer 		 * these states, because it is currently active.
17844990b8cSJulian Elischer 		 */
17944990b8cSJulian Elischer 		panic("bad state for thread unlinking");
18044990b8cSJulian Elischer 		/* NOTREACHED */
18171fad9fdSJulian Elischer 	case TDS_INACTIVE:
18244990b8cSJulian Elischer 		break;
18344990b8cSJulian Elischer 	default:
18444990b8cSJulian Elischer 		panic("bad thread state");
18544990b8cSJulian Elischer 		/* NOTREACHED */
18644990b8cSJulian Elischer 	}
18744990b8cSJulian Elischer #endif
1886e8525ceSRobert Watson #ifdef AUDIT
1896e8525ceSRobert Watson 	audit_thread_free(td);
1906e8525ceSRobert Watson #endif
1911ba4a712SPawel Jakub Dawidek 	/* Free all OSD associated to this thread. */
1921ba4a712SPawel Jakub Dawidek 	osd_thread_exit(td);
1931ba4a712SPawel Jakub Dawidek 
194b209f889SRandall Stewart 	EVENTHANDLER_INVOKE(thread_dtor, td);
195ec6ea5e8SDavid Xu 	tid_free(td->td_tid);
19644990b8cSJulian Elischer }
19744990b8cSJulian Elischer 
19844990b8cSJulian Elischer /*
19944990b8cSJulian Elischer  * Initialize type-stable parts of a thread (when newly created).
20044990b8cSJulian Elischer  */
201b23f72e9SBrian Feldman static int
202b23f72e9SBrian Feldman thread_init(void *mem, int size, int flags)
20344990b8cSJulian Elischer {
20444990b8cSJulian Elischer 	struct thread *td;
20544990b8cSJulian Elischer 
20644990b8cSJulian Elischer 	td = (struct thread *)mem;
207247aba24SMarcel Moolenaar 
20844f3b092SJohn Baldwin 	td->td_sleepqueue = sleepq_alloc();
209961a7b24SJohn Baldwin 	td->td_turnstile = turnstile_alloc();
2108f0e9130SKonstantin Belousov 	td->td_rlqe = NULL;
211b209f889SRandall Stewart 	EVENTHANDLER_INVOKE(thread_init, td);
212de028f5aSJeff Roberson 	td->td_sched = (struct td_sched *)&td[1];
213d10183d9SDavid Xu 	umtx_thread_init(td);
21489b57fcfSKonstantin Belousov 	td->td_kstack = 0;
215ad8b1d85SKonstantin Belousov 	td->td_sel = NULL;
216b23f72e9SBrian Feldman 	return (0);
21744990b8cSJulian Elischer }
21844990b8cSJulian Elischer 
21944990b8cSJulian Elischer /*
22044990b8cSJulian Elischer  * Tear down type-stable parts of a thread (just before being discarded).
22144990b8cSJulian Elischer  */
22244990b8cSJulian Elischer static void
22344990b8cSJulian Elischer thread_fini(void *mem, int size)
22444990b8cSJulian Elischer {
22544990b8cSJulian Elischer 	struct thread *td;
22644990b8cSJulian Elischer 
22744990b8cSJulian Elischer 	td = (struct thread *)mem;
228b209f889SRandall Stewart 	EVENTHANDLER_INVOKE(thread_fini, td);
2298f0e9130SKonstantin Belousov 	rlqentry_free(td->td_rlqe);
230961a7b24SJohn Baldwin 	turnstile_free(td->td_turnstile);
23144f3b092SJohn Baldwin 	sleepq_free(td->td_sleepqueue);
232d10183d9SDavid Xu 	umtx_thread_fini(td);
233ace8398dSJeff Roberson 	seltdfini(td);
23444990b8cSJulian Elischer }
2355215b187SJeff Roberson 
2365c8329edSJulian Elischer /*
2375215b187SJeff Roberson  * For a newly created process,
2385215b187SJeff Roberson  * link up all the structures and its initial threads etc.
239ed062c8dSJulian Elischer  * called from:
240e7d939bdSMarcel Moolenaar  * {arch}/{arch}/machdep.c   {arch}_init(), init386() etc.
241ed062c8dSJulian Elischer  * proc_dtor() (should go away)
242ed062c8dSJulian Elischer  * proc_init()
2435c8329edSJulian Elischer  */
2445c8329edSJulian Elischer void
24589b57fcfSKonstantin Belousov proc_linkup0(struct proc *p, struct thread *td)
24689b57fcfSKonstantin Belousov {
24789b57fcfSKonstantin Belousov 	TAILQ_INIT(&p->p_threads);	     /* all threads in proc */
24889b57fcfSKonstantin Belousov 	proc_linkup(p, td);
24989b57fcfSKonstantin Belousov }
25089b57fcfSKonstantin Belousov 
25189b57fcfSKonstantin Belousov void
2528460a577SJohn Birrell proc_linkup(struct proc *p, struct thread *td)
2535c8329edSJulian Elischer {
254a54e85fdSJeff Roberson 
2559104847fSDavid Xu 	sigqueue_init(&p->p_sigqueue, p);
256ebceaf6dSDavid Xu 	p->p_ksi = ksiginfo_alloc(1);
257ebceaf6dSDavid Xu 	if (p->p_ksi != NULL) {
2585c474517SDavid Xu 		/* XXX p_ksi may be null if ksiginfo zone is not ready */
259ebceaf6dSDavid Xu 		p->p_ksi->ksi_flags = KSI_EXT | KSI_INS;
260ebceaf6dSDavid Xu 	}
261b2f92ef9SDavid Xu 	LIST_INIT(&p->p_mqnotifier);
2625c8329edSJulian Elischer 	p->p_numthreads = 0;
2638460a577SJohn Birrell 	thread_link(td, p);
2645c8329edSJulian Elischer }
2655c8329edSJulian Elischer 
2665c8329edSJulian Elischer /*
26744990b8cSJulian Elischer  * Initialize global thread allocation resources.
26844990b8cSJulian Elischer  */
26944990b8cSJulian Elischer void
27044990b8cSJulian Elischer threadinit(void)
27144990b8cSJulian Elischer {
27244990b8cSJulian Elischer 
2731ea7a6f8SPoul-Henning Kamp 	mtx_init(&tid_lock, "TID lock", NULL, MTX_DEF);
27402c6fc21SKonstantin Belousov 
27502c6fc21SKonstantin Belousov 	/*
276abce621cSKonstantin Belousov 	 * pid_max cannot be greater than PID_MAX.
27702c6fc21SKonstantin Belousov 	 * leave one number for thread0.
27802c6fc21SKonstantin Belousov 	 */
2796829a5c5SJulian Elischer 	tid_unrhdr = new_unrhdr(PID_MAX + 2, INT_MAX, &tid_lock);
2801ea7a6f8SPoul-Henning Kamp 
281de028f5aSJeff Roberson 	thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(),
28244990b8cSJulian Elischer 	    thread_ctor, thread_dtor, thread_init, thread_fini,
2834649e92bSJohn Baldwin 	    16 - 1, 0);
284cf7d9a8cSDavid Xu 	tidhashtbl = hashinit(maxproc / 2, M_TIDHASH, &tidhash);
285cf7d9a8cSDavid Xu 	rw_init(&tidhash_lock, "tidhash");
28644990b8cSJulian Elischer }
28744990b8cSJulian Elischer 
28844990b8cSJulian Elischer /*
289ff8fbcffSJeff Roberson  * Place an unused thread on the zombie list.
290ad1e7d28SJulian Elischer  * Use the slpq as that must be unused by now.
29144990b8cSJulian Elischer  */
29244990b8cSJulian Elischer void
293ff8fbcffSJeff Roberson thread_zombie(struct thread *td)
29444990b8cSJulian Elischer {
295a54e85fdSJeff Roberson 	mtx_lock_spin(&zombie_lock);
296ad1e7d28SJulian Elischer 	TAILQ_INSERT_HEAD(&zombie_threads, td, td_slpq);
297a54e85fdSJeff Roberson 	mtx_unlock_spin(&zombie_lock);
29844990b8cSJulian Elischer }
29944990b8cSJulian Elischer 
3005c8329edSJulian Elischer /*
301ff8fbcffSJeff Roberson  * Release a thread that has exited after cpu_throw().
302ff8fbcffSJeff Roberson  */
303ff8fbcffSJeff Roberson void
304ff8fbcffSJeff Roberson thread_stash(struct thread *td)
305ff8fbcffSJeff Roberson {
306ff8fbcffSJeff Roberson 	atomic_subtract_rel_int(&td->td_proc->p_exitthreads, 1);
307ff8fbcffSJeff Roberson 	thread_zombie(td);
308ff8fbcffSJeff Roberson }
309ff8fbcffSJeff Roberson 
310ff8fbcffSJeff Roberson /*
3116617724cSJeff Roberson  * Reap zombie resources.
31244990b8cSJulian Elischer  */
31344990b8cSJulian Elischer void
31444990b8cSJulian Elischer thread_reap(void)
31544990b8cSJulian Elischer {
3165c8329edSJulian Elischer 	struct thread *td_first, *td_next;
31744990b8cSJulian Elischer 
31844990b8cSJulian Elischer 	/*
3195215b187SJeff Roberson 	 * Don't even bother to lock if none at this instant,
3205215b187SJeff Roberson 	 * we really don't care about the next instant..
32144990b8cSJulian Elischer 	 */
3228460a577SJohn Birrell 	if (!TAILQ_EMPTY(&zombie_threads)) {
323a54e85fdSJeff Roberson 		mtx_lock_spin(&zombie_lock);
3245c8329edSJulian Elischer 		td_first = TAILQ_FIRST(&zombie_threads);
3255c8329edSJulian Elischer 		if (td_first)
3265c8329edSJulian Elischer 			TAILQ_INIT(&zombie_threads);
327a54e85fdSJeff Roberson 		mtx_unlock_spin(&zombie_lock);
3285c8329edSJulian Elischer 		while (td_first) {
329ad1e7d28SJulian Elischer 			td_next = TAILQ_NEXT(td_first, td_slpq);
330*4ea6a9a2SMateusz Guzik 			thread_cow_free(td_first);
3315c8329edSJulian Elischer 			thread_free(td_first);
3325c8329edSJulian Elischer 			td_first = td_next;
33344990b8cSJulian Elischer 		}
33444990b8cSJulian Elischer 	}
335ed062c8dSJulian Elischer }
33644990b8cSJulian Elischer 
3374f0db5e0SJulian Elischer /*
33844990b8cSJulian Elischer  * Allocate a thread.
33944990b8cSJulian Elischer  */
34044990b8cSJulian Elischer struct thread *
3418a945d10SKonstantin Belousov thread_alloc(int pages)
34244990b8cSJulian Elischer {
34389b57fcfSKonstantin Belousov 	struct thread *td;
3448460a577SJohn Birrell 
34544990b8cSJulian Elischer 	thread_reap(); /* check if any zombies to get */
34689b57fcfSKonstantin Belousov 
34789b57fcfSKonstantin Belousov 	td = (struct thread *)uma_zalloc(thread_zone, M_WAITOK);
34889b57fcfSKonstantin Belousov 	KASSERT(td->td_kstack == 0, ("thread_alloc got thread with kstack"));
3498a945d10SKonstantin Belousov 	if (!vm_thread_new(td, pages)) {
35089b57fcfSKonstantin Belousov 		uma_zfree(thread_zone, td);
35189b57fcfSKonstantin Belousov 		return (NULL);
35289b57fcfSKonstantin Belousov 	}
3530c3967e7SMarcel Moolenaar 	cpu_thread_alloc(td);
35489b57fcfSKonstantin Belousov 	return (td);
35544990b8cSJulian Elischer }
35644990b8cSJulian Elischer 
3578a945d10SKonstantin Belousov int
3588a945d10SKonstantin Belousov thread_alloc_stack(struct thread *td, int pages)
3598a945d10SKonstantin Belousov {
3608a945d10SKonstantin Belousov 
3618a945d10SKonstantin Belousov 	KASSERT(td->td_kstack == 0,
3628a945d10SKonstantin Belousov 	    ("thread_alloc_stack called on a thread with kstack"));
3638a945d10SKonstantin Belousov 	if (!vm_thread_new(td, pages))
3648a945d10SKonstantin Belousov 		return (0);
3658a945d10SKonstantin Belousov 	cpu_thread_alloc(td);
3668a945d10SKonstantin Belousov 	return (1);
3678a945d10SKonstantin Belousov }
3684f0db5e0SJulian Elischer 
3694f0db5e0SJulian Elischer /*
37044990b8cSJulian Elischer  * Deallocate a thread.
37144990b8cSJulian Elischer  */
37244990b8cSJulian Elischer void
37344990b8cSJulian Elischer thread_free(struct thread *td)
37444990b8cSJulian Elischer {
3752e6b8de4SJeff Roberson 
3762e6b8de4SJeff Roberson 	lock_profile_thread_exit(td);
37745aea8deSJeff Roberson 	if (td->td_cpuset)
378d7f687fcSJeff Roberson 		cpuset_rel(td->td_cpuset);
379d7f687fcSJeff Roberson 	td->td_cpuset = NULL;
3800c3967e7SMarcel Moolenaar 	cpu_thread_free(td);
38189b57fcfSKonstantin Belousov 	if (td->td_kstack != 0)
38289b57fcfSKonstantin Belousov 		vm_thread_dispose(td);
38344990b8cSJulian Elischer 	uma_zfree(thread_zone, td);
38444990b8cSJulian Elischer }
38544990b8cSJulian Elischer 
386*4ea6a9a2SMateusz Guzik void
387*4ea6a9a2SMateusz Guzik thread_cow_get_proc(struct thread *newtd, struct proc *p)
388*4ea6a9a2SMateusz Guzik {
389*4ea6a9a2SMateusz Guzik 
390*4ea6a9a2SMateusz Guzik 	PROC_LOCK_ASSERT(p, MA_OWNED);
391*4ea6a9a2SMateusz Guzik 	newtd->td_ucred = crhold(p->p_ucred);
392*4ea6a9a2SMateusz Guzik 	newtd->td_cowgen = p->p_cowgen;
393*4ea6a9a2SMateusz Guzik }
394*4ea6a9a2SMateusz Guzik 
395*4ea6a9a2SMateusz Guzik void
396*4ea6a9a2SMateusz Guzik thread_cow_get(struct thread *newtd, struct thread *td)
397*4ea6a9a2SMateusz Guzik {
398*4ea6a9a2SMateusz Guzik 
399*4ea6a9a2SMateusz Guzik 	newtd->td_ucred = crhold(td->td_ucred);
400*4ea6a9a2SMateusz Guzik 	newtd->td_cowgen = td->td_cowgen;
401*4ea6a9a2SMateusz Guzik }
402*4ea6a9a2SMateusz Guzik 
403*4ea6a9a2SMateusz Guzik void
404*4ea6a9a2SMateusz Guzik thread_cow_free(struct thread *td)
405*4ea6a9a2SMateusz Guzik {
406*4ea6a9a2SMateusz Guzik 
407*4ea6a9a2SMateusz Guzik 	if (td->td_ucred)
408*4ea6a9a2SMateusz Guzik 		crfree(td->td_ucred);
409*4ea6a9a2SMateusz Guzik }
410*4ea6a9a2SMateusz Guzik 
411*4ea6a9a2SMateusz Guzik void
412*4ea6a9a2SMateusz Guzik thread_cow_update(struct thread *td)
413*4ea6a9a2SMateusz Guzik {
414*4ea6a9a2SMateusz Guzik 	struct proc *p;
415*4ea6a9a2SMateusz Guzik 
416*4ea6a9a2SMateusz Guzik 	p = td->td_proc;
417*4ea6a9a2SMateusz Guzik 	PROC_LOCK(p);
418*4ea6a9a2SMateusz Guzik 	if (td->td_ucred != p->p_ucred)
419*4ea6a9a2SMateusz Guzik 		cred_update_thread(td);
420*4ea6a9a2SMateusz Guzik 	td->td_cowgen = p->p_cowgen;
421*4ea6a9a2SMateusz Guzik 	PROC_UNLOCK(p);
422*4ea6a9a2SMateusz Guzik }
423*4ea6a9a2SMateusz Guzik 
42444990b8cSJulian Elischer /*
42544990b8cSJulian Elischer  * Discard the current thread and exit from its context.
42694e0a4cdSJulian Elischer  * Always called with scheduler locked.
42744990b8cSJulian Elischer  *
42844990b8cSJulian Elischer  * Because we can't free a thread while we're operating under its context,
429696058c3SJulian Elischer  * push the current thread into our CPU's deadthread holder. This means
430696058c3SJulian Elischer  * we needn't worry about someone else grabbing our context before we
4316617724cSJeff Roberson  * do a cpu_throw().
43244990b8cSJulian Elischer  */
43344990b8cSJulian Elischer void
43444990b8cSJulian Elischer thread_exit(void)
43544990b8cSJulian Elischer {
4367e3a96eaSJohn Baldwin 	uint64_t runtime, new_switchtime;
43744990b8cSJulian Elischer 	struct thread *td;
4381c4bcd05SJeff Roberson 	struct thread *td2;
43944990b8cSJulian Elischer 	struct proc *p;
4407847a9daSJohn Baldwin 	int wakeup_swapper;
44144990b8cSJulian Elischer 
44244990b8cSJulian Elischer 	td = curthread;
44344990b8cSJulian Elischer 	p = td->td_proc;
44444990b8cSJulian Elischer 
445a54e85fdSJeff Roberson 	PROC_SLOCK_ASSERT(p, MA_OWNED);
446ed062c8dSJulian Elischer 	mtx_assert(&Giant, MA_NOTOWNED);
447a54e85fdSJeff Roberson 
44844990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
449ed062c8dSJulian Elischer 	KASSERT(p != NULL, ("thread exiting without a process"));
450cc701b73SRobert Watson 	CTR3(KTR_PROC, "thread_exit: thread %p (pid %ld, %s)", td,
451e01eafefSJulian Elischer 	    (long)p->p_pid, td->td_name);
4529104847fSDavid Xu 	KASSERT(TAILQ_EMPTY(&td->td_sigqueue.sq_list), ("signal pending"));
45344990b8cSJulian Elischer 
45489964dd2SRobert Watson #ifdef AUDIT
45589964dd2SRobert Watson 	AUDIT_SYSCALL_EXIT(0, td);
45689964dd2SRobert Watson #endif
457ed062c8dSJulian Elischer 	/*
458ed062c8dSJulian Elischer 	 * drop FPU & debug register state storage, or any other
459ed062c8dSJulian Elischer 	 * architecture specific resources that
460ed062c8dSJulian Elischer 	 * would not be on a new untouched process.
461ed062c8dSJulian Elischer 	 */
46244990b8cSJulian Elischer 	cpu_thread_exit(td);	/* XXXSMP */
46344990b8cSJulian Elischer 
464ed062c8dSJulian Elischer 	/*
4651faf202eSJulian Elischer 	 * The last thread is left attached to the process
4661faf202eSJulian Elischer 	 * So that the whole bundle gets recycled. Skip
467ed062c8dSJulian Elischer 	 * all this stuff if we never had threads.
468ed062c8dSJulian Elischer 	 * EXIT clears all sign of other threads when
469ed062c8dSJulian Elischer 	 * it goes to single threading, so the last thread always
470ed062c8dSJulian Elischer 	 * takes the short path.
4711faf202eSJulian Elischer 	 */
472ed062c8dSJulian Elischer 	if (p->p_flag & P_HADTHREADS) {
4731faf202eSJulian Elischer 		if (p->p_numthreads > 1) {
474fd229b5bSKonstantin Belousov 			atomic_add_int(&td->td_proc->p_exitthreads, 1);
475d3a0bd78SJulian Elischer 			thread_unlink(td);
4761c4bcd05SJeff Roberson 			td2 = FIRST_THREAD_IN_PROC(p);
4771c4bcd05SJeff Roberson 			sched_exit_thread(td2, td);
478ed062c8dSJulian Elischer 
479ed062c8dSJulian Elischer 			/*
48044990b8cSJulian Elischer 			 * The test below is NOT true if we are the
4819182554aSKonstantin Belousov 			 * sole exiting thread. P_STOPPED_SINGLE is unset
48244990b8cSJulian Elischer 			 * in exit1() after it is the only survivor.
48344990b8cSJulian Elischer 			 */
4841279572aSDavid Xu 			if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
48544990b8cSJulian Elischer 				if (p->p_numthreads == p->p_suspcount) {
486a54e85fdSJeff Roberson 					thread_lock(p->p_singlethread);
4877847a9daSJohn Baldwin 					wakeup_swapper = thread_unsuspend_one(
48884cdea97SKonstantin Belousov 						p->p_singlethread, p, false);
489a54e85fdSJeff Roberson 					thread_unlock(p->p_singlethread);
4907847a9daSJohn Baldwin 					if (wakeup_swapper)
4917847a9daSJohn Baldwin 						kick_proc0();
49244990b8cSJulian Elischer 				}
49344990b8cSJulian Elischer 			}
49448bfcdddSJulian Elischer 
495696058c3SJulian Elischer 			PCPU_SET(deadthread, td);
4961faf202eSJulian Elischer 		} else {
497ed062c8dSJulian Elischer 			/*
498ed062c8dSJulian Elischer 			 * The last thread is exiting.. but not through exit()
499ed062c8dSJulian Elischer 			 */
500ed062c8dSJulian Elischer 			panic ("thread_exit: Last thread exiting on its own");
501ed062c8dSJulian Elischer 		}
5021faf202eSJulian Elischer 	}
50316d95d4fSJoseph Koshy #ifdef	HWPMC_HOOKS
50416d95d4fSJoseph Koshy 	/*
50516d95d4fSJoseph Koshy 	 * If this thread is part of a process that is being tracked by hwpmc(4),
50616d95d4fSJoseph Koshy 	 * inform the module of the thread's impending exit.
50716d95d4fSJoseph Koshy 	 */
50816d95d4fSJoseph Koshy 	if (PMC_PROC_IS_USING_PMCS(td->td_proc))
50916d95d4fSJoseph Koshy 		PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT);
51016d95d4fSJoseph Koshy #endif
511a54e85fdSJeff Roberson 	PROC_UNLOCK(p);
5125c7bebf9SKonstantin Belousov 	PROC_STATLOCK(p);
5135c7bebf9SKonstantin Belousov 	thread_lock(td);
5145c7bebf9SKonstantin Belousov 	PROC_SUNLOCK(p);
5157e3a96eaSJohn Baldwin 
5167e3a96eaSJohn Baldwin 	/* Do the same timestamp bookkeeping that mi_switch() would do. */
5177e3a96eaSJohn Baldwin 	new_switchtime = cpu_ticks();
5187e3a96eaSJohn Baldwin 	runtime = new_switchtime - PCPU_GET(switchtime);
5197e3a96eaSJohn Baldwin 	td->td_runtime += runtime;
5207e3a96eaSJohn Baldwin 	td->td_incruntime += runtime;
5217e3a96eaSJohn Baldwin 	PCPU_SET(switchtime, new_switchtime);
5227e3a96eaSJohn Baldwin 	PCPU_SET(switchticks, ticks);
5237e3a96eaSJohn Baldwin 	PCPU_INC(cnt.v_swtch);
5247e3a96eaSJohn Baldwin 
5257e3a96eaSJohn Baldwin 	/* Save our resource usage in our process. */
5267e3a96eaSJohn Baldwin 	td->td_ru.ru_nvcsw++;
52741fd9c63SKonstantin Belousov 	ruxagg(p, td);
5287e3a96eaSJohn Baldwin 	rucollect(&p->p_ru, &td->td_ru);
5295c7bebf9SKonstantin Belousov 	PROC_STATUNLOCK(p);
5307e3a96eaSJohn Baldwin 
531dcc9954eSJulian Elischer 	td->td_state = TDS_INACTIVE;
5323d06b4b3SAttilio Rao #ifdef WITNESS
5333d06b4b3SAttilio Rao 	witness_thread_exit(td);
5343d06b4b3SAttilio Rao #endif
535732d9528SJulian Elischer 	CTR1(KTR_PROC, "thread_exit: cpu_throw() thread %p", td);
536a54e85fdSJeff Roberson 	sched_throw(td);
537cc66ebe2SPeter Wemm 	panic("I'm a teapot!");
53844990b8cSJulian Elischer 	/* NOTREACHED */
53944990b8cSJulian Elischer }
54044990b8cSJulian Elischer 
54144990b8cSJulian Elischer /*
542696058c3SJulian Elischer  * Do any thread specific cleanups that may be needed in wait()
54337814395SPeter Wemm  * called with Giant, proc and schedlock not held.
544696058c3SJulian Elischer  */
545696058c3SJulian Elischer void
546696058c3SJulian Elischer thread_wait(struct proc *p)
547696058c3SJulian Elischer {
548696058c3SJulian Elischer 	struct thread *td;
549696058c3SJulian Elischer 
55037814395SPeter Wemm 	mtx_assert(&Giant, MA_NOTOWNED);
551624bf9e1SKonstantin Belousov 	KASSERT(p->p_numthreads == 1, ("multiple threads in thread_wait()"));
552624bf9e1SKonstantin Belousov 	KASSERT(p->p_exitthreads == 0, ("p_exitthreads leaking"));
553ff8fbcffSJeff Roberson 	td = FIRST_THREAD_IN_PROC(p);
554ff8fbcffSJeff Roberson 	/* Lock the last thread so we spin until it exits cpu_throw(). */
555ff8fbcffSJeff Roberson 	thread_lock(td);
556ff8fbcffSJeff Roberson 	thread_unlock(td);
5572e6b8de4SJeff Roberson 	lock_profile_thread_exit(td);
558d7f687fcSJeff Roberson 	cpuset_rel(td->td_cpuset);
559d7f687fcSJeff Roberson 	td->td_cpuset = NULL;
560696058c3SJulian Elischer 	cpu_thread_clean(td);
561*4ea6a9a2SMateusz Guzik 	thread_cow_free(td);
562696058c3SJulian Elischer 	thread_reap();	/* check for zombie threads etc. */
563696058c3SJulian Elischer }
564696058c3SJulian Elischer 
565696058c3SJulian Elischer /*
56644990b8cSJulian Elischer  * Link a thread to a process.
5671faf202eSJulian Elischer  * set up anything that needs to be initialized for it to
5681faf202eSJulian Elischer  * be used by the process.
56944990b8cSJulian Elischer  */
57044990b8cSJulian Elischer void
5718460a577SJohn Birrell thread_link(struct thread *td, struct proc *p)
57244990b8cSJulian Elischer {
57344990b8cSJulian Elischer 
574a54e85fdSJeff Roberson 	/*
575a54e85fdSJeff Roberson 	 * XXX This can't be enabled because it's called for proc0 before
576374ae2a3SJeff Roberson 	 * its lock has been created.
577374ae2a3SJeff Roberson 	 * PROC_LOCK_ASSERT(p, MA_OWNED);
578a54e85fdSJeff Roberson 	 */
57971fad9fdSJulian Elischer 	td->td_state    = TDS_INACTIVE;
58044990b8cSJulian Elischer 	td->td_proc     = p;
581b61ce5b0SJeff Roberson 	td->td_flags    = TDF_INMEM;
58244990b8cSJulian Elischer 
5831faf202eSJulian Elischer 	LIST_INIT(&td->td_contested);
584eea4f254SJeff Roberson 	LIST_INIT(&td->td_lprof[0]);
585eea4f254SJeff Roberson 	LIST_INIT(&td->td_lprof[1]);
5869104847fSDavid Xu 	sigqueue_init(&td->td_sigqueue, p);
587fd90e2edSJung-uk Kim 	callout_init(&td->td_slpcallout, 1);
58866d8df9dSDaniel Eischen 	TAILQ_INSERT_TAIL(&p->p_threads, td, td_plist);
58944990b8cSJulian Elischer 	p->p_numthreads++;
59044990b8cSJulian Elischer }
59144990b8cSJulian Elischer 
592ed062c8dSJulian Elischer /*
593ed062c8dSJulian Elischer  * Called from:
594ed062c8dSJulian Elischer  *  thread_exit()
595ed062c8dSJulian Elischer  */
596d3a0bd78SJulian Elischer void
597d3a0bd78SJulian Elischer thread_unlink(struct thread *td)
598d3a0bd78SJulian Elischer {
599d3a0bd78SJulian Elischer 	struct proc *p = td->td_proc;
600d3a0bd78SJulian Elischer 
601374ae2a3SJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
602d3a0bd78SJulian Elischer 	TAILQ_REMOVE(&p->p_threads, td, td_plist);
603d3a0bd78SJulian Elischer 	p->p_numthreads--;
604d3a0bd78SJulian Elischer 	/* could clear a few other things here */
6058460a577SJohn Birrell 	/* Must  NOT clear links to proc! */
6065c8329edSJulian Elischer }
6075c8329edSJulian Elischer 
60879799053SKonstantin Belousov static int
60979799053SKonstantin Belousov calc_remaining(struct proc *p, int mode)
61079799053SKonstantin Belousov {
61179799053SKonstantin Belousov 	int remaining;
61279799053SKonstantin Belousov 
6137b519077SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
6147b519077SKonstantin Belousov 	PROC_SLOCK_ASSERT(p, MA_OWNED);
61579799053SKonstantin Belousov 	if (mode == SINGLE_EXIT)
61679799053SKonstantin Belousov 		remaining = p->p_numthreads;
61779799053SKonstantin Belousov 	else if (mode == SINGLE_BOUNDARY)
61879799053SKonstantin Belousov 		remaining = p->p_numthreads - p->p_boundary_count;
6196ddcc233SKonstantin Belousov 	else if (mode == SINGLE_NO_EXIT || mode == SINGLE_ALLPROC)
62079799053SKonstantin Belousov 		remaining = p->p_numthreads - p->p_suspcount;
62179799053SKonstantin Belousov 	else
62279799053SKonstantin Belousov 		panic("calc_remaining: wrong mode %d", mode);
62379799053SKonstantin Belousov 	return (remaining);
62479799053SKonstantin Belousov }
62579799053SKonstantin Belousov 
62607a9368aSKonstantin Belousov static int
62707a9368aSKonstantin Belousov remain_for_mode(int mode)
62807a9368aSKonstantin Belousov {
62907a9368aSKonstantin Belousov 
6306ddcc233SKonstantin Belousov 	return (mode == SINGLE_ALLPROC ? 0 : 1);
63107a9368aSKonstantin Belousov }
63207a9368aSKonstantin Belousov 
63307a9368aSKonstantin Belousov static int
63407a9368aSKonstantin Belousov weed_inhib(int mode, struct thread *td2, struct proc *p)
63507a9368aSKonstantin Belousov {
63607a9368aSKonstantin Belousov 	int wakeup_swapper;
63707a9368aSKonstantin Belousov 
63807a9368aSKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
63907a9368aSKonstantin Belousov 	PROC_SLOCK_ASSERT(p, MA_OWNED);
64007a9368aSKonstantin Belousov 	THREAD_LOCK_ASSERT(td2, MA_OWNED);
64107a9368aSKonstantin Belousov 
64207a9368aSKonstantin Belousov 	wakeup_swapper = 0;
64307a9368aSKonstantin Belousov 	switch (mode) {
64407a9368aSKonstantin Belousov 	case SINGLE_EXIT:
64507a9368aSKonstantin Belousov 		if (TD_IS_SUSPENDED(td2))
64684cdea97SKonstantin Belousov 			wakeup_swapper |= thread_unsuspend_one(td2, p, true);
64707a9368aSKonstantin Belousov 		if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR) != 0)
64807a9368aSKonstantin Belousov 			wakeup_swapper |= sleepq_abort(td2, EINTR);
64907a9368aSKonstantin Belousov 		break;
65007a9368aSKonstantin Belousov 	case SINGLE_BOUNDARY:
65107a9368aSKonstantin Belousov 		if (TD_IS_SUSPENDED(td2) && (td2->td_flags & TDF_BOUNDARY) == 0)
65284cdea97SKonstantin Belousov 			wakeup_swapper |= thread_unsuspend_one(td2, p, false);
65307a9368aSKonstantin Belousov 		if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR) != 0)
65407a9368aSKonstantin Belousov 			wakeup_swapper |= sleepq_abort(td2, ERESTART);
65507a9368aSKonstantin Belousov 		break;
65607a9368aSKonstantin Belousov 	case SINGLE_NO_EXIT:
65707a9368aSKonstantin Belousov 		if (TD_IS_SUSPENDED(td2) && (td2->td_flags & TDF_BOUNDARY) == 0)
65884cdea97SKonstantin Belousov 			wakeup_swapper |= thread_unsuspend_one(td2, p, false);
65907a9368aSKonstantin Belousov 		if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR) != 0)
66007a9368aSKonstantin Belousov 			wakeup_swapper |= sleepq_abort(td2, ERESTART);
661917dd390SKonstantin Belousov 		break;
6626ddcc233SKonstantin Belousov 	case SINGLE_ALLPROC:
6636ddcc233SKonstantin Belousov 		/*
6646ddcc233SKonstantin Belousov 		 * ALLPROC suspend tries to avoid spurious EINTR for
6656ddcc233SKonstantin Belousov 		 * threads sleeping interruptable, by suspending the
6666ddcc233SKonstantin Belousov 		 * thread directly, similarly to sig_suspend_threads().
6676ddcc233SKonstantin Belousov 		 * Since such sleep is not performed at the user
6686ddcc233SKonstantin Belousov 		 * boundary, TDF_BOUNDARY flag is not set, and TDF_ALLPROCSUSP
6696ddcc233SKonstantin Belousov 		 * is used to avoid immediate un-suspend.
6706ddcc233SKonstantin Belousov 		 */
6716ddcc233SKonstantin Belousov 		if (TD_IS_SUSPENDED(td2) && (td2->td_flags & (TDF_BOUNDARY |
6726ddcc233SKonstantin Belousov 		    TDF_ALLPROCSUSP)) == 0)
67384cdea97SKonstantin Belousov 			wakeup_swapper |= thread_unsuspend_one(td2, p, false);
6746ddcc233SKonstantin Belousov 		if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR) != 0) {
6756ddcc233SKonstantin Belousov 			if ((td2->td_flags & TDF_SBDRY) == 0) {
6766ddcc233SKonstantin Belousov 				thread_suspend_one(td2);
6776ddcc233SKonstantin Belousov 				td2->td_flags |= TDF_ALLPROCSUSP;
6786ddcc233SKonstantin Belousov 			} else {
6796ddcc233SKonstantin Belousov 				wakeup_swapper |= sleepq_abort(td2, ERESTART);
6806ddcc233SKonstantin Belousov 			}
6816ddcc233SKonstantin Belousov 		}
68207a9368aSKonstantin Belousov 		break;
68307a9368aSKonstantin Belousov 	}
68407a9368aSKonstantin Belousov 	return (wakeup_swapper);
68507a9368aSKonstantin Belousov }
68607a9368aSKonstantin Belousov 
6875215b187SJeff Roberson /*
68844990b8cSJulian Elischer  * Enforce single-threading.
68944990b8cSJulian Elischer  *
69044990b8cSJulian Elischer  * Returns 1 if the caller must abort (another thread is waiting to
69144990b8cSJulian Elischer  * exit the process or similar). Process is locked!
69244990b8cSJulian Elischer  * Returns 0 when you are successfully the only thread running.
69344990b8cSJulian Elischer  * A process has successfully single threaded in the suspend mode when
69444990b8cSJulian Elischer  * There are no threads in user mode. Threads in the kernel must be
69544990b8cSJulian Elischer  * allowed to continue until they get to the user boundary. They may even
69644990b8cSJulian Elischer  * copy out their return values and data before suspending. They may however be
697e2668f55SMaxim Konovalov  * accelerated in reaching the user boundary as we will wake up
69844990b8cSJulian Elischer  * any sleeping threads that are interruptable. (PCATCH).
69944990b8cSJulian Elischer  */
70044990b8cSJulian Elischer int
7016ddcc233SKonstantin Belousov thread_single(struct proc *p, int mode)
70244990b8cSJulian Elischer {
70344990b8cSJulian Elischer 	struct thread *td;
70444990b8cSJulian Elischer 	struct thread *td2;
705da7bbd2cSJohn Baldwin 	int remaining, wakeup_swapper;
70644990b8cSJulian Elischer 
70744990b8cSJulian Elischer 	td = curthread;
7086ddcc233SKonstantin Belousov 	KASSERT(mode == SINGLE_EXIT || mode == SINGLE_BOUNDARY ||
7096ddcc233SKonstantin Belousov 	    mode == SINGLE_ALLPROC || mode == SINGLE_NO_EXIT,
7106ddcc233SKonstantin Belousov 	    ("invalid mode %d", mode));
7116ddcc233SKonstantin Belousov 	/*
7126ddcc233SKonstantin Belousov 	 * If allowing non-ALLPROC singlethreading for non-curproc
7136ddcc233SKonstantin Belousov 	 * callers, calc_remaining() and remain_for_mode() should be
7146ddcc233SKonstantin Belousov 	 * adjusted to also account for td->td_proc != p.  For now
7156ddcc233SKonstantin Belousov 	 * this is not implemented because it is not used.
7166ddcc233SKonstantin Belousov 	 */
7176ddcc233SKonstantin Belousov 	KASSERT((mode == SINGLE_ALLPROC && td->td_proc != p) ||
7186ddcc233SKonstantin Belousov 	    (mode != SINGLE_ALLPROC && td->td_proc == p),
7196ddcc233SKonstantin Belousov 	    ("mode %d proc %p curproc %p", mode, p, td->td_proc));
72037814395SPeter Wemm 	mtx_assert(&Giant, MA_NOTOWNED);
72144990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
72244990b8cSJulian Elischer 
7236ddcc233SKonstantin Belousov 	if ((p->p_flag & P_HADTHREADS) == 0 && mode != SINGLE_ALLPROC)
72444990b8cSJulian Elischer 		return (0);
72544990b8cSJulian Elischer 
726e3b9bf71SJulian Elischer 	/* Is someone already single threading? */
727906ac69dSDavid Xu 	if (p->p_singlethread != NULL && p->p_singlethread != td)
72844990b8cSJulian Elischer 		return (1);
72944990b8cSJulian Elischer 
730906ac69dSDavid Xu 	if (mode == SINGLE_EXIT) {
731906ac69dSDavid Xu 		p->p_flag |= P_SINGLE_EXIT;
732906ac69dSDavid Xu 		p->p_flag &= ~P_SINGLE_BOUNDARY;
733906ac69dSDavid Xu 	} else {
734906ac69dSDavid Xu 		p->p_flag &= ~P_SINGLE_EXIT;
735906ac69dSDavid Xu 		if (mode == SINGLE_BOUNDARY)
736906ac69dSDavid Xu 			p->p_flag |= P_SINGLE_BOUNDARY;
737906ac69dSDavid Xu 		else
738906ac69dSDavid Xu 			p->p_flag &= ~P_SINGLE_BOUNDARY;
739906ac69dSDavid Xu 	}
7406ddcc233SKonstantin Belousov 	if (mode == SINGLE_ALLPROC)
7416ddcc233SKonstantin Belousov 		p->p_flag |= P_TOTAL_STOP;
7421279572aSDavid Xu 	p->p_flag |= P_STOPPED_SINGLE;
7437b4a950aSDavid Xu 	PROC_SLOCK(p);
744112afcb2SJohn Baldwin 	p->p_singlethread = td;
74579799053SKonstantin Belousov 	remaining = calc_remaining(p, mode);
74607a9368aSKonstantin Belousov 	while (remaining != remain_for_mode(mode)) {
747bf1a3220SDavid Xu 		if (P_SHOULDSTOP(p) != P_STOPPED_SINGLE)
748bf1a3220SDavid Xu 			goto stopme;
749da7bbd2cSJohn Baldwin 		wakeup_swapper = 0;
75044990b8cSJulian Elischer 		FOREACH_THREAD_IN_PROC(p, td2) {
75144990b8cSJulian Elischer 			if (td2 == td)
75244990b8cSJulian Elischer 				continue;
753a54e85fdSJeff Roberson 			thread_lock(td2);
754b7edba77SJeff Roberson 			td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
7556ddcc233SKonstantin Belousov 			if (TD_IS_INHIBITED(td2)) {
75607a9368aSKonstantin Belousov 				wakeup_swapper |= weed_inhib(mode, td2, p);
757d8267df7SDavid Xu #ifdef SMP
7586ddcc233SKonstantin Belousov 			} else if (TD_IS_RUNNING(td2) && td != td2) {
759d8267df7SDavid Xu 				forward_signal(td2);
760d8267df7SDavid Xu #endif
7616ddcc233SKonstantin Belousov 			}
762a54e85fdSJeff Roberson 			thread_unlock(td2);
7639d102777SJulian Elischer 		}
764da7bbd2cSJohn Baldwin 		if (wakeup_swapper)
765da7bbd2cSJohn Baldwin 			kick_proc0();
76679799053SKonstantin Belousov 		remaining = calc_remaining(p, mode);
767ec008e96SDavid Xu 
7689d102777SJulian Elischer 		/*
7699d102777SJulian Elischer 		 * Maybe we suspended some threads.. was it enough?
7709d102777SJulian Elischer 		 */
77107a9368aSKonstantin Belousov 		if (remaining == remain_for_mode(mode))
7729d102777SJulian Elischer 			break;
7739d102777SJulian Elischer 
774bf1a3220SDavid Xu stopme:
77544990b8cSJulian Elischer 		/*
77644990b8cSJulian Elischer 		 * Wake us up when everyone else has suspended.
777e3b9bf71SJulian Elischer 		 * In the mean time we suspend as well.
77844990b8cSJulian Elischer 		 */
7796ddcc233SKonstantin Belousov 		thread_suspend_switch(td, p);
78079799053SKonstantin Belousov 		remaining = calc_remaining(p, mode);
78144990b8cSJulian Elischer 	}
782906ac69dSDavid Xu 	if (mode == SINGLE_EXIT) {
78391599697SJulian Elischer 		/*
7848626a0ddSKonstantin Belousov 		 * Convert the process to an unthreaded process.  The
7858626a0ddSKonstantin Belousov 		 * SINGLE_EXIT is called by exit1() or execve(), in
7868626a0ddSKonstantin Belousov 		 * both cases other threads must be retired.
78791599697SJulian Elischer 		 */
7888626a0ddSKonstantin Belousov 		KASSERT(p->p_numthreads == 1, ("Unthreading with >1 threads"));
789ed062c8dSJulian Elischer 		p->p_singlethread = NULL;
7908626a0ddSKonstantin Belousov 		p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_HADTHREADS);
791fd229b5bSKonstantin Belousov 
792fd229b5bSKonstantin Belousov 		/*
793fd229b5bSKonstantin Belousov 		 * Wait for any remaining threads to exit cpu_throw().
794fd229b5bSKonstantin Belousov 		 */
795fd229b5bSKonstantin Belousov 		while (p->p_exitthreads != 0) {
796fd229b5bSKonstantin Belousov 			PROC_SUNLOCK(p);
797fd229b5bSKonstantin Belousov 			PROC_UNLOCK(p);
798fd229b5bSKonstantin Belousov 			sched_relinquish(td);
799fd229b5bSKonstantin Belousov 			PROC_LOCK(p);
800fd229b5bSKonstantin Belousov 			PROC_SLOCK(p);
801fd229b5bSKonstantin Belousov 		}
802ac437c07SKonstantin Belousov 	} else if (mode == SINGLE_BOUNDARY) {
803ac437c07SKonstantin Belousov 		/*
804ac437c07SKonstantin Belousov 		 * Wait until all suspended threads are removed from
805ac437c07SKonstantin Belousov 		 * the processors.  The thread_suspend_check()
806ac437c07SKonstantin Belousov 		 * increments p_boundary_count while it is still
807ac437c07SKonstantin Belousov 		 * running, which makes it possible for the execve()
808ac437c07SKonstantin Belousov 		 * to destroy vmspace while our other threads are
809ac437c07SKonstantin Belousov 		 * still using the address space.
810ac437c07SKonstantin Belousov 		 *
811ac437c07SKonstantin Belousov 		 * We lock the thread, which is only allowed to
812ac437c07SKonstantin Belousov 		 * succeed after context switch code finished using
813ac437c07SKonstantin Belousov 		 * the address space.
814ac437c07SKonstantin Belousov 		 */
815ac437c07SKonstantin Belousov 		FOREACH_THREAD_IN_PROC(p, td2) {
816ac437c07SKonstantin Belousov 			if (td2 == td)
817ac437c07SKonstantin Belousov 				continue;
818ac437c07SKonstantin Belousov 			thread_lock(td2);
819ac437c07SKonstantin Belousov 			KASSERT((td2->td_flags & TDF_BOUNDARY) != 0,
820ac437c07SKonstantin Belousov 			    ("td %p not on boundary", td2));
821ac437c07SKonstantin Belousov 			KASSERT(TD_IS_SUSPENDED(td2),
822ac437c07SKonstantin Belousov 			    ("td %p is not suspended", td2));
823ac437c07SKonstantin Belousov 			thread_unlock(td2);
824ac437c07SKonstantin Belousov 		}
82591599697SJulian Elischer 	}
8267b4a950aSDavid Xu 	PROC_SUNLOCK(p);
82744990b8cSJulian Elischer 	return (0);
82844990b8cSJulian Elischer }
82944990b8cSJulian Elischer 
8308638fe7bSKonstantin Belousov bool
8318638fe7bSKonstantin Belousov thread_suspend_check_needed(void)
8328638fe7bSKonstantin Belousov {
8338638fe7bSKonstantin Belousov 	struct proc *p;
8348638fe7bSKonstantin Belousov 	struct thread *td;
8358638fe7bSKonstantin Belousov 
8368638fe7bSKonstantin Belousov 	td = curthread;
8378638fe7bSKonstantin Belousov 	p = td->td_proc;
8388638fe7bSKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
8398638fe7bSKonstantin Belousov 	return (P_SHOULDSTOP(p) || ((p->p_flag & P_TRACED) != 0 &&
8408638fe7bSKonstantin Belousov 	    (td->td_dbgflags & TDB_SUSPEND) != 0));
8418638fe7bSKonstantin Belousov }
8428638fe7bSKonstantin Belousov 
84344990b8cSJulian Elischer /*
84444990b8cSJulian Elischer  * Called in from locations that can safely check to see
84544990b8cSJulian Elischer  * whether we have to suspend or at least throttle for a
84644990b8cSJulian Elischer  * single-thread event (e.g. fork).
84744990b8cSJulian Elischer  *
84844990b8cSJulian Elischer  * Such locations include userret().
84944990b8cSJulian Elischer  * If the "return_instead" argument is non zero, the thread must be able to
85044990b8cSJulian Elischer  * accept 0 (caller may continue), or 1 (caller must abort) as a result.
85144990b8cSJulian Elischer  *
85244990b8cSJulian Elischer  * The 'return_instead' argument tells the function if it may do a
85344990b8cSJulian Elischer  * thread_exit() or suspend, or whether the caller must abort and back
85444990b8cSJulian Elischer  * out instead.
85544990b8cSJulian Elischer  *
85644990b8cSJulian Elischer  * If the thread that set the single_threading request has set the
85744990b8cSJulian Elischer  * P_SINGLE_EXIT bit in the process flags then this call will never return
85844990b8cSJulian Elischer  * if 'return_instead' is false, but will exit.
85944990b8cSJulian Elischer  *
86044990b8cSJulian Elischer  * P_SINGLE_EXIT | return_instead == 0| return_instead != 0
86144990b8cSJulian Elischer  *---------------+--------------------+---------------------
86244990b8cSJulian Elischer  *       0       | returns 0          |   returns 0 or 1
863353374b5SJohn Baldwin  *               | when ST ends       |   immediately
86444990b8cSJulian Elischer  *---------------+--------------------+---------------------
86544990b8cSJulian Elischer  *       1       | thread exits       |   returns 1
866353374b5SJohn Baldwin  *               |                    |  immediately
86744990b8cSJulian Elischer  * 0 = thread_exit() or suspension ok,
86844990b8cSJulian Elischer  * other = return error instead of stopping the thread.
86944990b8cSJulian Elischer  *
87044990b8cSJulian Elischer  * While a full suspension is under effect, even a single threading
87144990b8cSJulian Elischer  * thread would be suspended if it made this call (but it shouldn't).
87244990b8cSJulian Elischer  * This call should only be made from places where
87344990b8cSJulian Elischer  * thread_exit() would be safe as that may be the outcome unless
87444990b8cSJulian Elischer  * return_instead is set.
87544990b8cSJulian Elischer  */
87644990b8cSJulian Elischer int
87744990b8cSJulian Elischer thread_suspend_check(int return_instead)
87844990b8cSJulian Elischer {
879ecafb24bSJuli Mallett 	struct thread *td;
880ecafb24bSJuli Mallett 	struct proc *p;
8817847a9daSJohn Baldwin 	int wakeup_swapper;
88244990b8cSJulian Elischer 
88344990b8cSJulian Elischer 	td = curthread;
88444990b8cSJulian Elischer 	p = td->td_proc;
88537814395SPeter Wemm 	mtx_assert(&Giant, MA_NOTOWNED);
88644990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
8878638fe7bSKonstantin Belousov 	while (thread_suspend_check_needed()) {
8881279572aSDavid Xu 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
88944990b8cSJulian Elischer 			KASSERT(p->p_singlethread != NULL,
89044990b8cSJulian Elischer 			    ("singlethread not set"));
89144990b8cSJulian Elischer 			/*
892e3b9bf71SJulian Elischer 			 * The only suspension in action is a
893e3b9bf71SJulian Elischer 			 * single-threading. Single threader need not stop.
894b6d5995eSJulian Elischer 			 * XXX Should be safe to access unlocked
895b6d5995eSJulian Elischer 			 * as it can only be set to be true by us.
89644990b8cSJulian Elischer 			 */
897e3b9bf71SJulian Elischer 			if (p->p_singlethread == td)
89844990b8cSJulian Elischer 				return (0);	/* Exempt from stopping. */
89944990b8cSJulian Elischer 		}
90045a4bfa1SDavid Xu 		if ((p->p_flag & P_SINGLE_EXIT) && return_instead)
90194f0972bSDavid Xu 			return (EINTR);
90244990b8cSJulian Elischer 
903906ac69dSDavid Xu 		/* Should we goto user boundary if we didn't come from there? */
904906ac69dSDavid Xu 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE &&
905906ac69dSDavid Xu 		    (p->p_flag & P_SINGLE_BOUNDARY) && return_instead)
90694f0972bSDavid Xu 			return (ERESTART);
907906ac69dSDavid Xu 
90844990b8cSJulian Elischer 		/*
9093077f938SKonstantin Belousov 		 * Ignore suspend requests if they are deferred.
910d071a6faSJohn Baldwin 		 */
9113077f938SKonstantin Belousov 		if ((td->td_flags & TDF_SBDRY) != 0) {
912d071a6faSJohn Baldwin 			KASSERT(return_instead,
913d071a6faSJohn Baldwin 			    ("TDF_SBDRY set for unsafe thread_suspend_check"));
914d071a6faSJohn Baldwin 			return (0);
915d071a6faSJohn Baldwin 		}
916d071a6faSJohn Baldwin 
917d071a6faSJohn Baldwin 		/*
91844990b8cSJulian Elischer 		 * If the process is waiting for us to exit,
91944990b8cSJulian Elischer 		 * this thread should just suicide.
9201279572aSDavid Xu 		 * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE.
92144990b8cSJulian Elischer 		 */
922cf7d9a8cSDavid Xu 		if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) {
923cf7d9a8cSDavid Xu 			PROC_UNLOCK(p);
924cf7d9a8cSDavid Xu 			tidhash_remove(td);
92591d1786fSDmitry Chagin 
92691d1786fSDmitry Chagin 			/*
92791d1786fSDmitry Chagin 			 * Allow Linux emulation layer to do some work
92891d1786fSDmitry Chagin 			 * before thread suicide.
92991d1786fSDmitry Chagin 			 */
93091d1786fSDmitry Chagin 			if (__predict_false(p->p_sysent->sv_thread_detach != NULL))
93191d1786fSDmitry Chagin 				(p->p_sysent->sv_thread_detach)(td);
93291d1786fSDmitry Chagin 
933cf7d9a8cSDavid Xu 			PROC_LOCK(p);
93421ecd1e9SDavid Xu 			tdsigcleanup(td);
93513dad108SKonstantin Belousov 			umtx_thread_exit(td);
936cf7d9a8cSDavid Xu 			PROC_SLOCK(p);
93721ecd1e9SDavid Xu 			thread_stopped(p);
93844990b8cSJulian Elischer 			thread_exit();
939cf7d9a8cSDavid Xu 		}
94021ecd1e9SDavid Xu 
94121ecd1e9SDavid Xu 		PROC_SLOCK(p);
94221ecd1e9SDavid Xu 		thread_stopped(p);
943a54e85fdSJeff Roberson 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
944a54e85fdSJeff Roberson 			if (p->p_numthreads == p->p_suspcount + 1) {
945a54e85fdSJeff Roberson 				thread_lock(p->p_singlethread);
94684cdea97SKonstantin Belousov 				wakeup_swapper = thread_unsuspend_one(
94784cdea97SKonstantin Belousov 				    p->p_singlethread, p, false);
948a54e85fdSJeff Roberson 				thread_unlock(p->p_singlethread);
9497847a9daSJohn Baldwin 				if (wakeup_swapper)
9507847a9daSJohn Baldwin 					kick_proc0();
951a54e85fdSJeff Roberson 			}
952a54e85fdSJeff Roberson 		}
9533f9be10eSDavid Xu 		PROC_UNLOCK(p);
9547b4a950aSDavid Xu 		thread_lock(td);
95544990b8cSJulian Elischer 		/*
95644990b8cSJulian Elischer 		 * When a thread suspends, it just
957ad1e7d28SJulian Elischer 		 * gets taken off all queues.
95844990b8cSJulian Elischer 		 */
95971fad9fdSJulian Elischer 		thread_suspend_one(td);
960906ac69dSDavid Xu 		if (return_instead == 0) {
961906ac69dSDavid Xu 			p->p_boundary_count++;
962906ac69dSDavid Xu 			td->td_flags |= TDF_BOUNDARY;
963cf19bf91SJulian Elischer 		}
9647b4a950aSDavid Xu 		PROC_SUNLOCK(p);
9658df78c41SJeff Roberson 		mi_switch(SW_INVOL | SWT_SUSPEND, NULL);
966a54e85fdSJeff Roberson 		thread_unlock(td);
96744990b8cSJulian Elischer 		PROC_LOCK(p);
96844990b8cSJulian Elischer 	}
96944990b8cSJulian Elischer 	return (0);
97044990b8cSJulian Elischer }
97144990b8cSJulian Elischer 
97235c32a76SDavid Xu void
9736ddcc233SKonstantin Belousov thread_suspend_switch(struct thread *td, struct proc *p)
974a54e85fdSJeff Roberson {
975a54e85fdSJeff Roberson 
976a54e85fdSJeff Roberson 	KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
977a54e85fdSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
9787b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
979a54e85fdSJeff Roberson 	/*
980a54e85fdSJeff Roberson 	 * We implement thread_suspend_one in stages here to avoid
981a54e85fdSJeff Roberson 	 * dropping the proc lock while the thread lock is owned.
982a54e85fdSJeff Roberson 	 */
9836ddcc233SKonstantin Belousov 	if (p == td->td_proc) {
984a54e85fdSJeff Roberson 		thread_stopped(p);
985a54e85fdSJeff Roberson 		p->p_suspcount++;
9866ddcc233SKonstantin Belousov 	}
9873f9be10eSDavid Xu 	PROC_UNLOCK(p);
9887b4a950aSDavid Xu 	thread_lock(td);
989b7edba77SJeff Roberson 	td->td_flags &= ~TDF_NEEDSUSPCHK;
990a54e85fdSJeff Roberson 	TD_SET_SUSPENDED(td);
991c5aa6b58SJeff Roberson 	sched_sleep(td, 0);
9927b4a950aSDavid Xu 	PROC_SUNLOCK(p);
993a54e85fdSJeff Roberson 	DROP_GIANT();
9948df78c41SJeff Roberson 	mi_switch(SW_VOL | SWT_SUSPEND, NULL);
995a54e85fdSJeff Roberson 	thread_unlock(td);
996a54e85fdSJeff Roberson 	PICKUP_GIANT();
997a54e85fdSJeff Roberson 	PROC_LOCK(p);
9987b4a950aSDavid Xu 	PROC_SLOCK(p);
999a54e85fdSJeff Roberson }
1000a54e85fdSJeff Roberson 
1001a54e85fdSJeff Roberson void
100235c32a76SDavid Xu thread_suspend_one(struct thread *td)
100335c32a76SDavid Xu {
10046ddcc233SKonstantin Belousov 	struct proc *p;
100535c32a76SDavid Xu 
10066ddcc233SKonstantin Belousov 	p = td->td_proc;
10077b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
1008a54e85fdSJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1009e574e444SDavid Xu 	KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
101035c32a76SDavid Xu 	p->p_suspcount++;
1011b7edba77SJeff Roberson 	td->td_flags &= ~TDF_NEEDSUSPCHK;
101271fad9fdSJulian Elischer 	TD_SET_SUSPENDED(td);
1013c5aa6b58SJeff Roberson 	sched_sleep(td, 0);
101435c32a76SDavid Xu }
101535c32a76SDavid Xu 
101684cdea97SKonstantin Belousov static int
101784cdea97SKonstantin Belousov thread_unsuspend_one(struct thread *td, struct proc *p, bool boundary)
101835c32a76SDavid Xu {
101935c32a76SDavid Xu 
1020a54e85fdSJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1021ad1e7d28SJulian Elischer 	KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended"));
102271fad9fdSJulian Elischer 	TD_CLR_SUSPENDED(td);
10236ddcc233SKonstantin Belousov 	td->td_flags &= ~TDF_ALLPROCSUSP;
10246ddcc233SKonstantin Belousov 	if (td->td_proc == p) {
10256ddcc233SKonstantin Belousov 		PROC_SLOCK_ASSERT(p, MA_OWNED);
102635c32a76SDavid Xu 		p->p_suspcount--;
102784cdea97SKonstantin Belousov 		if (boundary && (td->td_flags & TDF_BOUNDARY) != 0) {
102884cdea97SKonstantin Belousov 			td->td_flags &= ~TDF_BOUNDARY;
102984cdea97SKonstantin Belousov 			p->p_boundary_count--;
103084cdea97SKonstantin Belousov 		}
10316ddcc233SKonstantin Belousov 	}
10327847a9daSJohn Baldwin 	return (setrunnable(td));
103335c32a76SDavid Xu }
103435c32a76SDavid Xu 
103544990b8cSJulian Elischer /*
103644990b8cSJulian Elischer  * Allow all threads blocked by single threading to continue running.
103744990b8cSJulian Elischer  */
103844990b8cSJulian Elischer void
103944990b8cSJulian Elischer thread_unsuspend(struct proc *p)
104044990b8cSJulian Elischer {
104144990b8cSJulian Elischer 	struct thread *td;
10427847a9daSJohn Baldwin 	int wakeup_swapper;
104344990b8cSJulian Elischer 
104444990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
10457b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
10467847a9daSJohn Baldwin 	wakeup_swapper = 0;
104744990b8cSJulian Elischer 	if (!P_SHOULDSTOP(p)) {
1048ad1e7d28SJulian Elischer                 FOREACH_THREAD_IN_PROC(p, td) {
1049a54e85fdSJeff Roberson 			thread_lock(td);
1050ad1e7d28SJulian Elischer 			if (TD_IS_SUSPENDED(td)) {
105184cdea97SKonstantin Belousov 				wakeup_swapper |= thread_unsuspend_one(td, p,
105284cdea97SKonstantin Belousov 				    true);
105344990b8cSJulian Elischer 			}
1054a54e85fdSJeff Roberson 			thread_unlock(td);
1055ad1e7d28SJulian Elischer 		}
105684cdea97SKonstantin Belousov 	} else if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE &&
105784cdea97SKonstantin Belousov 	    p->p_numthreads == p->p_suspcount) {
105844990b8cSJulian Elischer 		/*
105944990b8cSJulian Elischer 		 * Stopping everything also did the job for the single
106044990b8cSJulian Elischer 		 * threading request. Now we've downgraded to single-threaded,
106144990b8cSJulian Elischer 		 * let it continue.
106244990b8cSJulian Elischer 		 */
10636ddcc233SKonstantin Belousov 		if (p->p_singlethread->td_proc == p) {
1064a54e85fdSJeff Roberson 			thread_lock(p->p_singlethread);
10656ddcc233SKonstantin Belousov 			wakeup_swapper = thread_unsuspend_one(
106684cdea97SKonstantin Belousov 			    p->p_singlethread, p, false);
1067a54e85fdSJeff Roberson 			thread_unlock(p->p_singlethread);
106844990b8cSJulian Elischer 		}
10696ddcc233SKonstantin Belousov 	}
10707847a9daSJohn Baldwin 	if (wakeup_swapper)
10717847a9daSJohn Baldwin 		kick_proc0();
107244990b8cSJulian Elischer }
107344990b8cSJulian Elischer 
1074ed062c8dSJulian Elischer /*
1075ed062c8dSJulian Elischer  * End the single threading mode..
1076ed062c8dSJulian Elischer  */
107744990b8cSJulian Elischer void
10786ddcc233SKonstantin Belousov thread_single_end(struct proc *p, int mode)
107944990b8cSJulian Elischer {
108044990b8cSJulian Elischer 	struct thread *td;
10817847a9daSJohn Baldwin 	int wakeup_swapper;
108244990b8cSJulian Elischer 
10836ddcc233SKonstantin Belousov 	KASSERT(mode == SINGLE_EXIT || mode == SINGLE_BOUNDARY ||
10846ddcc233SKonstantin Belousov 	    mode == SINGLE_ALLPROC || mode == SINGLE_NO_EXIT,
10856ddcc233SKonstantin Belousov 	    ("invalid mode %d", mode));
108644990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
10876ddcc233SKonstantin Belousov 	KASSERT((mode == SINGLE_ALLPROC && (p->p_flag & P_TOTAL_STOP) != 0) ||
10886ddcc233SKonstantin Belousov 	    (mode != SINGLE_ALLPROC && (p->p_flag & P_TOTAL_STOP) == 0),
10896ddcc233SKonstantin Belousov 	    ("mode %d does not match P_TOTAL_STOP", mode));
109084cdea97SKonstantin Belousov 	KASSERT(mode == SINGLE_ALLPROC || p->p_singlethread == curthread,
109184cdea97SKonstantin Belousov 	    ("thread_single_end from other thread %p %p",
109284cdea97SKonstantin Belousov 	    curthread, p->p_singlethread));
109384cdea97SKonstantin Belousov 	KASSERT(mode != SINGLE_BOUNDARY ||
109484cdea97SKonstantin Belousov 	    (p->p_flag & P_SINGLE_BOUNDARY) != 0,
109584cdea97SKonstantin Belousov 	    ("mis-matched SINGLE_BOUNDARY flags %x", p->p_flag));
10966ddcc233SKonstantin Belousov 	p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_SINGLE_BOUNDARY |
10976ddcc233SKonstantin Belousov 	    P_TOTAL_STOP);
10987b4a950aSDavid Xu 	PROC_SLOCK(p);
109944990b8cSJulian Elischer 	p->p_singlethread = NULL;
11007847a9daSJohn Baldwin 	wakeup_swapper = 0;
110149539972SJulian Elischer 	/*
11027847a9daSJohn Baldwin 	 * If there are other threads they may now run,
110349539972SJulian Elischer 	 * unless of course there is a blanket 'stop order'
110449539972SJulian Elischer 	 * on the process. The single threader must be allowed
110549539972SJulian Elischer 	 * to continue however as this is a bad place to stop.
110649539972SJulian Elischer 	 */
11076ddcc233SKonstantin Belousov 	if (p->p_numthreads != remain_for_mode(mode) && !P_SHOULDSTOP(p)) {
1108ad1e7d28SJulian Elischer                 FOREACH_THREAD_IN_PROC(p, td) {
1109a54e85fdSJeff Roberson 			thread_lock(td);
1110ad1e7d28SJulian Elischer 			if (TD_IS_SUSPENDED(td)) {
111184cdea97SKonstantin Belousov 				wakeup_swapper |= thread_unsuspend_one(td, p,
111284cdea97SKonstantin Belousov 				    mode == SINGLE_BOUNDARY);
111344990b8cSJulian Elischer 			}
1114a54e85fdSJeff Roberson 			thread_unlock(td);
111549539972SJulian Elischer 		}
1116ad1e7d28SJulian Elischer 	}
111784cdea97SKonstantin Belousov 	KASSERT(mode != SINGLE_BOUNDARY || p->p_boundary_count == 0,
111884cdea97SKonstantin Belousov 	    ("inconsistent boundary count %d", p->p_boundary_count));
11197b4a950aSDavid Xu 	PROC_SUNLOCK(p);
11207847a9daSJohn Baldwin 	if (wakeup_swapper)
11217847a9daSJohn Baldwin 		kick_proc0();
112249539972SJulian Elischer }
11234fc21c09SDaniel Eischen 
112444355392SDavid Xu struct thread *
112544355392SDavid Xu thread_find(struct proc *p, lwpid_t tid)
112644355392SDavid Xu {
112744355392SDavid Xu 	struct thread *td;
112844355392SDavid Xu 
112944355392SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
113044355392SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td) {
113144355392SDavid Xu 		if (td->td_tid == tid)
113244355392SDavid Xu 			break;
113344355392SDavid Xu 	}
113444355392SDavid Xu 	return (td);
113544355392SDavid Xu }
1136cf7d9a8cSDavid Xu 
1137cf7d9a8cSDavid Xu /* Locate a thread by number; return with proc lock held. */
1138cf7d9a8cSDavid Xu struct thread *
1139cf7d9a8cSDavid Xu tdfind(lwpid_t tid, pid_t pid)
1140cf7d9a8cSDavid Xu {
1141cf7d9a8cSDavid Xu #define RUN_THRESH	16
1142cf7d9a8cSDavid Xu 	struct thread *td;
1143cf7d9a8cSDavid Xu 	int run = 0;
1144cf7d9a8cSDavid Xu 
1145cf7d9a8cSDavid Xu 	rw_rlock(&tidhash_lock);
1146cf7d9a8cSDavid Xu 	LIST_FOREACH(td, TIDHASH(tid), td_hash) {
1147cf7d9a8cSDavid Xu 		if (td->td_tid == tid) {
1148cf7d9a8cSDavid Xu 			if (pid != -1 && td->td_proc->p_pid != pid) {
1149cf7d9a8cSDavid Xu 				td = NULL;
1150cf7d9a8cSDavid Xu 				break;
1151cf7d9a8cSDavid Xu 			}
11528e6fa660SJohn Baldwin 			PROC_LOCK(td->td_proc);
1153cf7d9a8cSDavid Xu 			if (td->td_proc->p_state == PRS_NEW) {
11548e6fa660SJohn Baldwin 				PROC_UNLOCK(td->td_proc);
1155cf7d9a8cSDavid Xu 				td = NULL;
1156cf7d9a8cSDavid Xu 				break;
1157cf7d9a8cSDavid Xu 			}
1158cf7d9a8cSDavid Xu 			if (run > RUN_THRESH) {
1159cf7d9a8cSDavid Xu 				if (rw_try_upgrade(&tidhash_lock)) {
1160cf7d9a8cSDavid Xu 					LIST_REMOVE(td, td_hash);
1161cf7d9a8cSDavid Xu 					LIST_INSERT_HEAD(TIDHASH(td->td_tid),
1162cf7d9a8cSDavid Xu 						td, td_hash);
1163cf7d9a8cSDavid Xu 					rw_wunlock(&tidhash_lock);
1164cf7d9a8cSDavid Xu 					return (td);
1165cf7d9a8cSDavid Xu 				}
1166cf7d9a8cSDavid Xu 			}
1167cf7d9a8cSDavid Xu 			break;
1168cf7d9a8cSDavid Xu 		}
1169cf7d9a8cSDavid Xu 		run++;
1170cf7d9a8cSDavid Xu 	}
1171cf7d9a8cSDavid Xu 	rw_runlock(&tidhash_lock);
1172cf7d9a8cSDavid Xu 	return (td);
1173cf7d9a8cSDavid Xu }
1174cf7d9a8cSDavid Xu 
1175cf7d9a8cSDavid Xu void
1176cf7d9a8cSDavid Xu tidhash_add(struct thread *td)
1177cf7d9a8cSDavid Xu {
1178cf7d9a8cSDavid Xu 	rw_wlock(&tidhash_lock);
1179cf7d9a8cSDavid Xu 	LIST_INSERT_HEAD(TIDHASH(td->td_tid), td, td_hash);
1180cf7d9a8cSDavid Xu 	rw_wunlock(&tidhash_lock);
1181cf7d9a8cSDavid Xu }
1182cf7d9a8cSDavid Xu 
1183cf7d9a8cSDavid Xu void
1184cf7d9a8cSDavid Xu tidhash_remove(struct thread *td)
1185cf7d9a8cSDavid Xu {
1186cf7d9a8cSDavid Xu 	rw_wlock(&tidhash_lock);
1187cf7d9a8cSDavid Xu 	LIST_REMOVE(td, td_hash);
1188cf7d9a8cSDavid Xu 	rw_wunlock(&tidhash_lock);
1189cf7d9a8cSDavid Xu }
1190