xref: /freebsd/sys/kern/kern_thread.c (revision 478ca4b004a09b7f0cea986469ff794ccb4a1292)
19454b2d8SWarner Losh /*-
28a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
38a36da99SPedro F. Giffuni  *
444990b8cSJulian Elischer  * Copyright (C) 2001 Julian Elischer <julian@freebsd.org>.
544990b8cSJulian Elischer  *  All rights reserved.
644990b8cSJulian Elischer  *
744990b8cSJulian Elischer  * Redistribution and use in source and binary forms, with or without
844990b8cSJulian Elischer  * modification, are permitted provided that the following conditions
944990b8cSJulian Elischer  * are met:
1044990b8cSJulian Elischer  * 1. Redistributions of source code must retain the above copyright
1144990b8cSJulian Elischer  *    notice(s), this list of conditions and the following disclaimer as
1244990b8cSJulian Elischer  *    the first lines of this file unmodified other than the possible
1344990b8cSJulian Elischer  *    addition of one or more copyright notices.
1444990b8cSJulian Elischer  * 2. Redistributions in binary form must reproduce the above copyright
1544990b8cSJulian Elischer  *    notice(s), this list of conditions and the following disclaimer in the
1644990b8cSJulian Elischer  *    documentation and/or other materials provided with the distribution.
1744990b8cSJulian Elischer  *
1844990b8cSJulian Elischer  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
1944990b8cSJulian Elischer  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2044990b8cSJulian Elischer  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2144990b8cSJulian Elischer  * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
2244990b8cSJulian Elischer  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2344990b8cSJulian Elischer  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2444990b8cSJulian Elischer  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
2544990b8cSJulian Elischer  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2644990b8cSJulian Elischer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2744990b8cSJulian Elischer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
2844990b8cSJulian Elischer  * DAMAGE.
2944990b8cSJulian Elischer  */
3044990b8cSJulian Elischer 
313d06b4b3SAttilio Rao #include "opt_witness.h"
3216d95d4fSJoseph Koshy #include "opt_hwpmc_hooks.h"
333d06b4b3SAttilio Rao 
34677b542eSDavid E. O'Brien #include <sys/cdefs.h>
35677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
36677b542eSDavid E. O'Brien 
3744990b8cSJulian Elischer #include <sys/param.h>
3844990b8cSJulian Elischer #include <sys/systm.h>
3944990b8cSJulian Elischer #include <sys/kernel.h>
4044990b8cSJulian Elischer #include <sys/lock.h>
4144990b8cSJulian Elischer #include <sys/mutex.h>
4244990b8cSJulian Elischer #include <sys/proc.h>
436febf180SGleb Smirnoff #include <sys/epoch.h>
448f0e9130SKonstantin Belousov #include <sys/rangelock.h>
45e170bfdaSDavid Xu #include <sys/resourcevar.h>
46b3e9e682SRyan Stone #include <sys/sdt.h>
4794e0a4cdSJulian Elischer #include <sys/smp.h>
48de028f5aSJeff Roberson #include <sys/sched.h>
4944f3b092SJohn Baldwin #include <sys/sleepqueue.h>
50ace8398dSJeff Roberson #include <sys/selinfo.h>
51d1e7a4a5SJohn Baldwin #include <sys/syscallsubr.h>
5291d1786fSDmitry Chagin #include <sys/sysent.h>
53961a7b24SJohn Baldwin #include <sys/turnstile.h>
5444990b8cSJulian Elischer #include <sys/ktr.h>
55cf7d9a8cSDavid Xu #include <sys/rwlock.h>
56bc8e6d81SDavid Xu #include <sys/umtx.h>
579ed01c32SGleb Smirnoff #include <sys/vmmeter.h>
58d7f687fcSJeff Roberson #include <sys/cpuset.h>
5916d95d4fSJoseph Koshy #ifdef	HWPMC_HOOKS
6016d95d4fSJoseph Koshy #include <sys/pmckern.h>
6116d95d4fSJoseph Koshy #endif
6244990b8cSJulian Elischer 
63911b84b0SRobert Watson #include <security/audit/audit.h>
64911b84b0SRobert Watson 
6544990b8cSJulian Elischer #include <vm/vm.h>
6649a2507bSAlan Cox #include <vm/vm_extern.h>
6744990b8cSJulian Elischer #include <vm/uma.h>
68b209f889SRandall Stewart #include <sys/eventhandler.h>
6902fb42b0SPeter Wemm 
70acd9f517SKonstantin Belousov /*
71acd9f517SKonstantin Belousov  * Asserts below verify the stability of struct thread and struct proc
72acd9f517SKonstantin Belousov  * layout, as exposed by KBI to modules.  On head, the KBI is allowed
73acd9f517SKonstantin Belousov  * to drift, change to the structures must be accompanied by the
74acd9f517SKonstantin Belousov  * assert update.
75acd9f517SKonstantin Belousov  *
76acd9f517SKonstantin Belousov  * On the stable branches after KBI freeze, conditions must not be
77acd9f517SKonstantin Belousov  * violated.  Typically new fields are moved to the end of the
78acd9f517SKonstantin Belousov  * structures.
79acd9f517SKonstantin Belousov  */
80acd9f517SKonstantin Belousov #ifdef __amd64__
813f289c3fSJeff Roberson _Static_assert(offsetof(struct thread, td_flags) == 0xfc,
82acd9f517SKonstantin Belousov     "struct thread KBI td_flags");
833f289c3fSJeff Roberson _Static_assert(offsetof(struct thread, td_pflags) == 0x104,
84acd9f517SKonstantin Belousov     "struct thread KBI td_pflags");
85b3c0d957SAndrew Turner _Static_assert(offsetof(struct thread, td_frame) == 0x478,
86acd9f517SKonstantin Belousov     "struct thread KBI td_frame");
875e921ff4SKonstantin Belousov _Static_assert(offsetof(struct thread, td_emuldata) == 0x690,
88acd9f517SKonstantin Belousov     "struct thread KBI td_emuldata");
89acd9f517SKonstantin Belousov _Static_assert(offsetof(struct proc, p_flag) == 0xb0,
90acd9f517SKonstantin Belousov     "struct proc KBI p_flag");
91acd9f517SKonstantin Belousov _Static_assert(offsetof(struct proc, p_pid) == 0xbc,
92acd9f517SKonstantin Belousov     "struct proc KBI p_pid");
9383bf5ec3SJohn Baldwin _Static_assert(offsetof(struct proc, p_filemon) == 0x3c8,
94acd9f517SKonstantin Belousov     "struct proc KBI p_filemon");
9583bf5ec3SJohn Baldwin _Static_assert(offsetof(struct proc, p_comm) == 0x3e0,
96acd9f517SKonstantin Belousov     "struct proc KBI p_comm");
9783bf5ec3SJohn Baldwin _Static_assert(offsetof(struct proc, p_emuldata) == 0x4c0,
98acd9f517SKonstantin Belousov     "struct proc KBI p_emuldata");
99acd9f517SKonstantin Belousov #endif
100acd9f517SKonstantin Belousov #ifdef __i386__
1013f289c3fSJeff Roberson _Static_assert(offsetof(struct thread, td_flags) == 0x98,
102acd9f517SKonstantin Belousov     "struct thread KBI td_flags");
1033f289c3fSJeff Roberson _Static_assert(offsetof(struct thread, td_pflags) == 0xa0,
104acd9f517SKonstantin Belousov     "struct thread KBI td_pflags");
1051af9474bSJohn Baldwin _Static_assert(offsetof(struct thread, td_frame) == 0x2f0,
106acd9f517SKonstantin Belousov     "struct thread KBI td_frame");
107be860eaeSAndrew Turner _Static_assert(offsetof(struct thread, td_emuldata) == 0x338,
108acd9f517SKonstantin Belousov     "struct thread KBI td_emuldata");
109acd9f517SKonstantin Belousov _Static_assert(offsetof(struct proc, p_flag) == 0x68,
110acd9f517SKonstantin Belousov     "struct proc KBI p_flag");
111acd9f517SKonstantin Belousov _Static_assert(offsetof(struct proc, p_pid) == 0x74,
112acd9f517SKonstantin Belousov     "struct proc KBI p_pid");
11383bf5ec3SJohn Baldwin _Static_assert(offsetof(struct proc, p_filemon) == 0x278,
114acd9f517SKonstantin Belousov     "struct proc KBI p_filemon");
11583bf5ec3SJohn Baldwin _Static_assert(offsetof(struct proc, p_comm) == 0x28c,
116acd9f517SKonstantin Belousov     "struct proc KBI p_comm");
11783bf5ec3SJohn Baldwin _Static_assert(offsetof(struct proc, p_emuldata) == 0x318,
118acd9f517SKonstantin Belousov     "struct proc KBI p_emuldata");
119acd9f517SKonstantin Belousov #endif
120acd9f517SKonstantin Belousov 
121b3e9e682SRyan Stone SDT_PROVIDER_DECLARE(proc);
122d9fae5abSAndriy Gapon SDT_PROBE_DEFINE(proc, , , lwp__exit);
123b3e9e682SRyan Stone 
1248460a577SJohn Birrell /*
1258460a577SJohn Birrell  * thread related storage.
1268460a577SJohn Birrell  */
12744990b8cSJulian Elischer static uma_zone_t thread_zone;
12844990b8cSJulian Elischer 
1295215b187SJeff Roberson TAILQ_HEAD(, thread) zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads);
130c8790f5dSAttilio Rao static struct mtx zombie_lock;
131a54e85fdSJeff Roberson MTX_SYSINIT(zombie_lock, &zombie_lock, "zombie lock", MTX_SPIN);
13244990b8cSJulian Elischer 
133ff8fbcffSJeff Roberson static void thread_zombie(struct thread *);
13484cdea97SKonstantin Belousov static int thread_unsuspend_one(struct thread *td, struct proc *p,
13584cdea97SKonstantin Belousov     bool boundary);
136ff8fbcffSJeff Roberson 
137ec6ea5e8SDavid Xu #define TID_BUFFER_SIZE	1024
138ec6ea5e8SDavid Xu 
139fdcac928SMarcel Moolenaar struct mtx tid_lock;
1401ea7a6f8SPoul-Henning Kamp static struct unrhdr *tid_unrhdr;
141ec6ea5e8SDavid Xu static lwpid_t tid_buffer[TID_BUFFER_SIZE];
142ec6ea5e8SDavid Xu static int tid_head, tid_tail;
143cf7d9a8cSDavid Xu static MALLOC_DEFINE(M_TIDHASH, "tidhash", "thread hash");
144cf7d9a8cSDavid Xu 
145cf7d9a8cSDavid Xu struct	tidhashhead *tidhashtbl;
146cf7d9a8cSDavid Xu u_long	tidhash;
147cf7d9a8cSDavid Xu struct	rwlock tidhash_lock;
148cf7d9a8cSDavid Xu 
1492ca45184SMatt Joras EVENTHANDLER_LIST_DEFINE(thread_ctor);
1502ca45184SMatt Joras EVENTHANDLER_LIST_DEFINE(thread_dtor);
1512ca45184SMatt Joras EVENTHANDLER_LIST_DEFINE(thread_init);
1522ca45184SMatt Joras EVENTHANDLER_LIST_DEFINE(thread_fini);
1532ca45184SMatt Joras 
154ec6ea5e8SDavid Xu static lwpid_t
155ec6ea5e8SDavid Xu tid_alloc(void)
156ec6ea5e8SDavid Xu {
157ec6ea5e8SDavid Xu 	lwpid_t	tid;
158ec6ea5e8SDavid Xu 
159ec6ea5e8SDavid Xu 	tid = alloc_unr(tid_unrhdr);
160ec6ea5e8SDavid Xu 	if (tid != -1)
161ec6ea5e8SDavid Xu 		return (tid);
162ec6ea5e8SDavid Xu 	mtx_lock(&tid_lock);
163ec6ea5e8SDavid Xu 	if (tid_head == tid_tail) {
164ec6ea5e8SDavid Xu 		mtx_unlock(&tid_lock);
165ec6ea5e8SDavid Xu 		return (-1);
166ec6ea5e8SDavid Xu 	}
16794cb3545SKonstantin Belousov 	tid = tid_buffer[tid_head];
16894cb3545SKonstantin Belousov 	tid_head = (tid_head + 1) % TID_BUFFER_SIZE;
169ec6ea5e8SDavid Xu 	mtx_unlock(&tid_lock);
170ec6ea5e8SDavid Xu 	return (tid);
171ec6ea5e8SDavid Xu }
172ec6ea5e8SDavid Xu 
173ec6ea5e8SDavid Xu static void
174ec6ea5e8SDavid Xu tid_free(lwpid_t tid)
175ec6ea5e8SDavid Xu {
176ec6ea5e8SDavid Xu 	lwpid_t tmp_tid = -1;
177ec6ea5e8SDavid Xu 
178ec6ea5e8SDavid Xu 	mtx_lock(&tid_lock);
179ec6ea5e8SDavid Xu 	if ((tid_tail + 1) % TID_BUFFER_SIZE == tid_head) {
18094cb3545SKonstantin Belousov 		tmp_tid = tid_buffer[tid_head];
18194cb3545SKonstantin Belousov 		tid_head = (tid_head + 1) % TID_BUFFER_SIZE;
182ec6ea5e8SDavid Xu 	}
18394cb3545SKonstantin Belousov 	tid_buffer[tid_tail] = tid;
18494cb3545SKonstantin Belousov 	tid_tail = (tid_tail + 1) % TID_BUFFER_SIZE;
185ec6ea5e8SDavid Xu 	mtx_unlock(&tid_lock);
186ec6ea5e8SDavid Xu 	if (tmp_tid != -1)
187ec6ea5e8SDavid Xu 		free_unr(tid_unrhdr, tmp_tid);
188ec6ea5e8SDavid Xu }
189ec6ea5e8SDavid Xu 
190fdcac928SMarcel Moolenaar /*
191696058c3SJulian Elischer  * Prepare a thread for use.
19244990b8cSJulian Elischer  */
193b23f72e9SBrian Feldman static int
194b23f72e9SBrian Feldman thread_ctor(void *mem, int size, void *arg, int flags)
19544990b8cSJulian Elischer {
19644990b8cSJulian Elischer 	struct thread	*td;
19744990b8cSJulian Elischer 
19844990b8cSJulian Elischer 	td = (struct thread *)mem;
19971fad9fdSJulian Elischer 	td->td_state = TDS_INACTIVE;
20094dd54b9SKonstantin Belousov 	td->td_lastcpu = td->td_oncpu = NOCPU;
2016c27c603SJuli Mallett 
202ec6ea5e8SDavid Xu 	td->td_tid = tid_alloc();
203773eff9dSPoul-Henning Kamp 
2046c27c603SJuli Mallett 	/*
2056c27c603SJuli Mallett 	 * Note that td_critnest begins life as 1 because the thread is not
2066c27c603SJuli Mallett 	 * running and is thereby implicitly waiting to be on the receiving
207a54e85fdSJeff Roberson 	 * end of a context switch.
2086c27c603SJuli Mallett 	 */
209139b7550SJohn Baldwin 	td->td_critnest = 1;
210acbe332aSDavid Xu 	td->td_lend_user_pri = PRI_MAX;
2112ca45184SMatt Joras 	EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td);
212911b84b0SRobert Watson #ifdef AUDIT
213911b84b0SRobert Watson 	audit_thread_alloc(td);
214911b84b0SRobert Watson #endif
215d10183d9SDavid Xu 	umtx_thread_alloc(td);
216b23f72e9SBrian Feldman 	return (0);
21744990b8cSJulian Elischer }
21844990b8cSJulian Elischer 
21944990b8cSJulian Elischer /*
22044990b8cSJulian Elischer  * Reclaim a thread after use.
22144990b8cSJulian Elischer  */
22244990b8cSJulian Elischer static void
22344990b8cSJulian Elischer thread_dtor(void *mem, int size, void *arg)
22444990b8cSJulian Elischer {
22544990b8cSJulian Elischer 	struct thread *td;
22644990b8cSJulian Elischer 
22744990b8cSJulian Elischer 	td = (struct thread *)mem;
22844990b8cSJulian Elischer 
22944990b8cSJulian Elischer #ifdef INVARIANTS
23044990b8cSJulian Elischer 	/* Verify that this thread is in a safe state to free. */
23144990b8cSJulian Elischer 	switch (td->td_state) {
23271fad9fdSJulian Elischer 	case TDS_INHIBITED:
23371fad9fdSJulian Elischer 	case TDS_RUNNING:
23471fad9fdSJulian Elischer 	case TDS_CAN_RUN:
23544990b8cSJulian Elischer 	case TDS_RUNQ:
23644990b8cSJulian Elischer 		/*
23744990b8cSJulian Elischer 		 * We must never unlink a thread that is in one of
23844990b8cSJulian Elischer 		 * these states, because it is currently active.
23944990b8cSJulian Elischer 		 */
24044990b8cSJulian Elischer 		panic("bad state for thread unlinking");
24144990b8cSJulian Elischer 		/* NOTREACHED */
24271fad9fdSJulian Elischer 	case TDS_INACTIVE:
24344990b8cSJulian Elischer 		break;
24444990b8cSJulian Elischer 	default:
24544990b8cSJulian Elischer 		panic("bad thread state");
24644990b8cSJulian Elischer 		/* NOTREACHED */
24744990b8cSJulian Elischer 	}
24844990b8cSJulian Elischer #endif
2496e8525ceSRobert Watson #ifdef AUDIT
2506e8525ceSRobert Watson 	audit_thread_free(td);
2516e8525ceSRobert Watson #endif
2521ba4a712SPawel Jakub Dawidek 	/* Free all OSD associated to this thread. */
2531ba4a712SPawel Jakub Dawidek 	osd_thread_exit(td);
254aca4bb91SKonstantin Belousov 	td_softdep_cleanup(td);
255aca4bb91SKonstantin Belousov 	MPASS(td->td_su == NULL);
2561ba4a712SPawel Jakub Dawidek 
2572ca45184SMatt Joras 	EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td);
258ec6ea5e8SDavid Xu 	tid_free(td->td_tid);
25944990b8cSJulian Elischer }
26044990b8cSJulian Elischer 
26144990b8cSJulian Elischer /*
26244990b8cSJulian Elischer  * Initialize type-stable parts of a thread (when newly created).
26344990b8cSJulian Elischer  */
264b23f72e9SBrian Feldman static int
265b23f72e9SBrian Feldman thread_init(void *mem, int size, int flags)
26644990b8cSJulian Elischer {
26744990b8cSJulian Elischer 	struct thread *td;
26844990b8cSJulian Elischer 
26944990b8cSJulian Elischer 	td = (struct thread *)mem;
270247aba24SMarcel Moolenaar 
27144f3b092SJohn Baldwin 	td->td_sleepqueue = sleepq_alloc();
272961a7b24SJohn Baldwin 	td->td_turnstile = turnstile_alloc();
2738f0e9130SKonstantin Belousov 	td->td_rlqe = NULL;
2742ca45184SMatt Joras 	EVENTHANDLER_DIRECT_INVOKE(thread_init, td);
275d10183d9SDavid Xu 	umtx_thread_init(td);
27689b57fcfSKonstantin Belousov 	td->td_kstack = 0;
277ad8b1d85SKonstantin Belousov 	td->td_sel = NULL;
278b23f72e9SBrian Feldman 	return (0);
27944990b8cSJulian Elischer }
28044990b8cSJulian Elischer 
28144990b8cSJulian Elischer /*
28244990b8cSJulian Elischer  * Tear down type-stable parts of a thread (just before being discarded).
28344990b8cSJulian Elischer  */
28444990b8cSJulian Elischer static void
28544990b8cSJulian Elischer thread_fini(void *mem, int size)
28644990b8cSJulian Elischer {
28744990b8cSJulian Elischer 	struct thread *td;
28844990b8cSJulian Elischer 
28944990b8cSJulian Elischer 	td = (struct thread *)mem;
2902ca45184SMatt Joras 	EVENTHANDLER_DIRECT_INVOKE(thread_fini, td);
2918f0e9130SKonstantin Belousov 	rlqentry_free(td->td_rlqe);
292961a7b24SJohn Baldwin 	turnstile_free(td->td_turnstile);
29344f3b092SJohn Baldwin 	sleepq_free(td->td_sleepqueue);
294d10183d9SDavid Xu 	umtx_thread_fini(td);
295ace8398dSJeff Roberson 	seltdfini(td);
29644990b8cSJulian Elischer }
2975215b187SJeff Roberson 
2985c8329edSJulian Elischer /*
2995215b187SJeff Roberson  * For a newly created process,
3005215b187SJeff Roberson  * link up all the structures and its initial threads etc.
301ed062c8dSJulian Elischer  * called from:
302e7d939bdSMarcel Moolenaar  * {arch}/{arch}/machdep.c   {arch}_init(), init386() etc.
303ed062c8dSJulian Elischer  * proc_dtor() (should go away)
304ed062c8dSJulian Elischer  * proc_init()
3055c8329edSJulian Elischer  */
3065c8329edSJulian Elischer void
30789b57fcfSKonstantin Belousov proc_linkup0(struct proc *p, struct thread *td)
30889b57fcfSKonstantin Belousov {
30989b57fcfSKonstantin Belousov 	TAILQ_INIT(&p->p_threads);	     /* all threads in proc */
31089b57fcfSKonstantin Belousov 	proc_linkup(p, td);
31189b57fcfSKonstantin Belousov }
31289b57fcfSKonstantin Belousov 
31389b57fcfSKonstantin Belousov void
3148460a577SJohn Birrell proc_linkup(struct proc *p, struct thread *td)
3155c8329edSJulian Elischer {
316a54e85fdSJeff Roberson 
3179104847fSDavid Xu 	sigqueue_init(&p->p_sigqueue, p);
318ebceaf6dSDavid Xu 	p->p_ksi = ksiginfo_alloc(1);
319ebceaf6dSDavid Xu 	if (p->p_ksi != NULL) {
3205c474517SDavid Xu 		/* XXX p_ksi may be null if ksiginfo zone is not ready */
321ebceaf6dSDavid Xu 		p->p_ksi->ksi_flags = KSI_EXT | KSI_INS;
322ebceaf6dSDavid Xu 	}
323b2f92ef9SDavid Xu 	LIST_INIT(&p->p_mqnotifier);
3245c8329edSJulian Elischer 	p->p_numthreads = 0;
3258460a577SJohn Birrell 	thread_link(td, p);
3265c8329edSJulian Elischer }
3275c8329edSJulian Elischer 
3285c8329edSJulian Elischer /*
32944990b8cSJulian Elischer  * Initialize global thread allocation resources.
33044990b8cSJulian Elischer  */
33144990b8cSJulian Elischer void
33244990b8cSJulian Elischer threadinit(void)
33344990b8cSJulian Elischer {
33444990b8cSJulian Elischer 
3351ea7a6f8SPoul-Henning Kamp 	mtx_init(&tid_lock, "TID lock", NULL, MTX_DEF);
33602c6fc21SKonstantin Belousov 
33702c6fc21SKonstantin Belousov 	/*
338abce621cSKonstantin Belousov 	 * pid_max cannot be greater than PID_MAX.
33902c6fc21SKonstantin Belousov 	 * leave one number for thread0.
34002c6fc21SKonstantin Belousov 	 */
3416829a5c5SJulian Elischer 	tid_unrhdr = new_unrhdr(PID_MAX + 2, INT_MAX, &tid_lock);
3421ea7a6f8SPoul-Henning Kamp 
343de028f5aSJeff Roberson 	thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(),
34444990b8cSJulian Elischer 	    thread_ctor, thread_dtor, thread_init, thread_fini,
345f743ea96SMateusz Guzik 	    32 - 1, UMA_ZONE_NOFREE);
346cf7d9a8cSDavid Xu 	tidhashtbl = hashinit(maxproc / 2, M_TIDHASH, &tidhash);
347cf7d9a8cSDavid Xu 	rw_init(&tidhash_lock, "tidhash");
34844990b8cSJulian Elischer }
34944990b8cSJulian Elischer 
35044990b8cSJulian Elischer /*
351ff8fbcffSJeff Roberson  * Place an unused thread on the zombie list.
352ad1e7d28SJulian Elischer  * Use the slpq as that must be unused by now.
35344990b8cSJulian Elischer  */
35444990b8cSJulian Elischer void
355ff8fbcffSJeff Roberson thread_zombie(struct thread *td)
35644990b8cSJulian Elischer {
357a54e85fdSJeff Roberson 	mtx_lock_spin(&zombie_lock);
358ad1e7d28SJulian Elischer 	TAILQ_INSERT_HEAD(&zombie_threads, td, td_slpq);
359a54e85fdSJeff Roberson 	mtx_unlock_spin(&zombie_lock);
36044990b8cSJulian Elischer }
36144990b8cSJulian Elischer 
3625c8329edSJulian Elischer /*
363ff8fbcffSJeff Roberson  * Release a thread that has exited after cpu_throw().
364ff8fbcffSJeff Roberson  */
365ff8fbcffSJeff Roberson void
366ff8fbcffSJeff Roberson thread_stash(struct thread *td)
367ff8fbcffSJeff Roberson {
368ff8fbcffSJeff Roberson 	atomic_subtract_rel_int(&td->td_proc->p_exitthreads, 1);
369ff8fbcffSJeff Roberson 	thread_zombie(td);
370ff8fbcffSJeff Roberson }
371ff8fbcffSJeff Roberson 
372ff8fbcffSJeff Roberson /*
3736617724cSJeff Roberson  * Reap zombie resources.
37444990b8cSJulian Elischer  */
37544990b8cSJulian Elischer void
37644990b8cSJulian Elischer thread_reap(void)
37744990b8cSJulian Elischer {
3785c8329edSJulian Elischer 	struct thread *td_first, *td_next;
37944990b8cSJulian Elischer 
38044990b8cSJulian Elischer 	/*
3815215b187SJeff Roberson 	 * Don't even bother to lock if none at this instant,
3822d19b736SKonstantin Belousov 	 * we really don't care about the next instant.
38344990b8cSJulian Elischer 	 */
3848460a577SJohn Birrell 	if (!TAILQ_EMPTY(&zombie_threads)) {
385a54e85fdSJeff Roberson 		mtx_lock_spin(&zombie_lock);
3865c8329edSJulian Elischer 		td_first = TAILQ_FIRST(&zombie_threads);
3875c8329edSJulian Elischer 		if (td_first)
3885c8329edSJulian Elischer 			TAILQ_INIT(&zombie_threads);
389a54e85fdSJeff Roberson 		mtx_unlock_spin(&zombie_lock);
3905c8329edSJulian Elischer 		while (td_first) {
391ad1e7d28SJulian Elischer 			td_next = TAILQ_NEXT(td_first, td_slpq);
3924ea6a9a2SMateusz Guzik 			thread_cow_free(td_first);
3935c8329edSJulian Elischer 			thread_free(td_first);
3945c8329edSJulian Elischer 			td_first = td_next;
39544990b8cSJulian Elischer 		}
39644990b8cSJulian Elischer 	}
397ed062c8dSJulian Elischer }
39844990b8cSJulian Elischer 
3994f0db5e0SJulian Elischer /*
40044990b8cSJulian Elischer  * Allocate a thread.
40144990b8cSJulian Elischer  */
40244990b8cSJulian Elischer struct thread *
4038a945d10SKonstantin Belousov thread_alloc(int pages)
40444990b8cSJulian Elischer {
40589b57fcfSKonstantin Belousov 	struct thread *td;
4068460a577SJohn Birrell 
40744990b8cSJulian Elischer 	thread_reap(); /* check if any zombies to get */
40889b57fcfSKonstantin Belousov 
40989b57fcfSKonstantin Belousov 	td = (struct thread *)uma_zalloc(thread_zone, M_WAITOK);
41089b57fcfSKonstantin Belousov 	KASSERT(td->td_kstack == 0, ("thread_alloc got thread with kstack"));
4118a945d10SKonstantin Belousov 	if (!vm_thread_new(td, pages)) {
41289b57fcfSKonstantin Belousov 		uma_zfree(thread_zone, td);
41389b57fcfSKonstantin Belousov 		return (NULL);
41489b57fcfSKonstantin Belousov 	}
4150c3967e7SMarcel Moolenaar 	cpu_thread_alloc(td);
41689b57fcfSKonstantin Belousov 	return (td);
41744990b8cSJulian Elischer }
41844990b8cSJulian Elischer 
4198a945d10SKonstantin Belousov int
4208a945d10SKonstantin Belousov thread_alloc_stack(struct thread *td, int pages)
4218a945d10SKonstantin Belousov {
4228a945d10SKonstantin Belousov 
4238a945d10SKonstantin Belousov 	KASSERT(td->td_kstack == 0,
4248a945d10SKonstantin Belousov 	    ("thread_alloc_stack called on a thread with kstack"));
4258a945d10SKonstantin Belousov 	if (!vm_thread_new(td, pages))
4268a945d10SKonstantin Belousov 		return (0);
4278a945d10SKonstantin Belousov 	cpu_thread_alloc(td);
4288a945d10SKonstantin Belousov 	return (1);
4298a945d10SKonstantin Belousov }
4304f0db5e0SJulian Elischer 
4314f0db5e0SJulian Elischer /*
43244990b8cSJulian Elischer  * Deallocate a thread.
43344990b8cSJulian Elischer  */
43444990b8cSJulian Elischer void
43544990b8cSJulian Elischer thread_free(struct thread *td)
43644990b8cSJulian Elischer {
4372e6b8de4SJeff Roberson 
4382e6b8de4SJeff Roberson 	lock_profile_thread_exit(td);
43945aea8deSJeff Roberson 	if (td->td_cpuset)
440d7f687fcSJeff Roberson 		cpuset_rel(td->td_cpuset);
441d7f687fcSJeff Roberson 	td->td_cpuset = NULL;
4420c3967e7SMarcel Moolenaar 	cpu_thread_free(td);
44389b57fcfSKonstantin Belousov 	if (td->td_kstack != 0)
44489b57fcfSKonstantin Belousov 		vm_thread_dispose(td);
4452d19b736SKonstantin Belousov 	callout_drain(&td->td_slpcallout);
44644990b8cSJulian Elischer 	uma_zfree(thread_zone, td);
44744990b8cSJulian Elischer }
44844990b8cSJulian Elischer 
4494ea6a9a2SMateusz Guzik void
4504ea6a9a2SMateusz Guzik thread_cow_get_proc(struct thread *newtd, struct proc *p)
4514ea6a9a2SMateusz Guzik {
4524ea6a9a2SMateusz Guzik 
4534ea6a9a2SMateusz Guzik 	PROC_LOCK_ASSERT(p, MA_OWNED);
4544ea6a9a2SMateusz Guzik 	newtd->td_ucred = crhold(p->p_ucred);
455f6f6d240SMateusz Guzik 	newtd->td_limit = lim_hold(p->p_limit);
4564ea6a9a2SMateusz Guzik 	newtd->td_cowgen = p->p_cowgen;
4574ea6a9a2SMateusz Guzik }
4584ea6a9a2SMateusz Guzik 
4594ea6a9a2SMateusz Guzik void
4604ea6a9a2SMateusz Guzik thread_cow_get(struct thread *newtd, struct thread *td)
4614ea6a9a2SMateusz Guzik {
4624ea6a9a2SMateusz Guzik 
4634ea6a9a2SMateusz Guzik 	newtd->td_ucred = crhold(td->td_ucred);
464f6f6d240SMateusz Guzik 	newtd->td_limit = lim_hold(td->td_limit);
4654ea6a9a2SMateusz Guzik 	newtd->td_cowgen = td->td_cowgen;
4664ea6a9a2SMateusz Guzik }
4674ea6a9a2SMateusz Guzik 
4684ea6a9a2SMateusz Guzik void
4694ea6a9a2SMateusz Guzik thread_cow_free(struct thread *td)
4704ea6a9a2SMateusz Guzik {
4714ea6a9a2SMateusz Guzik 
472cd672ca6SMateusz Guzik 	if (td->td_ucred != NULL)
4734ea6a9a2SMateusz Guzik 		crfree(td->td_ucred);
474cd672ca6SMateusz Guzik 	if (td->td_limit != NULL)
475f6f6d240SMateusz Guzik 		lim_free(td->td_limit);
4764ea6a9a2SMateusz Guzik }
4774ea6a9a2SMateusz Guzik 
4784ea6a9a2SMateusz Guzik void
4794ea6a9a2SMateusz Guzik thread_cow_update(struct thread *td)
4804ea6a9a2SMateusz Guzik {
4814ea6a9a2SMateusz Guzik 	struct proc *p;
482cd672ca6SMateusz Guzik 	struct ucred *oldcred;
483cd672ca6SMateusz Guzik 	struct plimit *oldlimit;
4844ea6a9a2SMateusz Guzik 
4854ea6a9a2SMateusz Guzik 	p = td->td_proc;
486cd672ca6SMateusz Guzik 	oldcred = NULL;
487cd672ca6SMateusz Guzik 	oldlimit = NULL;
4884ea6a9a2SMateusz Guzik 	PROC_LOCK(p);
489cd672ca6SMateusz Guzik 	if (td->td_ucred != p->p_ucred) {
490cd672ca6SMateusz Guzik 		oldcred = td->td_ucred;
491cd672ca6SMateusz Guzik 		td->td_ucred = crhold(p->p_ucred);
492cd672ca6SMateusz Guzik 	}
493cd672ca6SMateusz Guzik 	if (td->td_limit != p->p_limit) {
494cd672ca6SMateusz Guzik 		oldlimit = td->td_limit;
495cd672ca6SMateusz Guzik 		td->td_limit = lim_hold(p->p_limit);
496cd672ca6SMateusz Guzik 	}
4974ea6a9a2SMateusz Guzik 	td->td_cowgen = p->p_cowgen;
4984ea6a9a2SMateusz Guzik 	PROC_UNLOCK(p);
499cd672ca6SMateusz Guzik 	if (oldcred != NULL)
500cd672ca6SMateusz Guzik 		crfree(oldcred);
501cd672ca6SMateusz Guzik 	if (oldlimit != NULL)
502cd672ca6SMateusz Guzik 		lim_free(oldlimit);
5034ea6a9a2SMateusz Guzik }
5044ea6a9a2SMateusz Guzik 
50544990b8cSJulian Elischer /*
50644990b8cSJulian Elischer  * Discard the current thread and exit from its context.
50794e0a4cdSJulian Elischer  * Always called with scheduler locked.
50844990b8cSJulian Elischer  *
50944990b8cSJulian Elischer  * Because we can't free a thread while we're operating under its context,
510696058c3SJulian Elischer  * push the current thread into our CPU's deadthread holder. This means
511696058c3SJulian Elischer  * we needn't worry about someone else grabbing our context before we
5126617724cSJeff Roberson  * do a cpu_throw().
51344990b8cSJulian Elischer  */
51444990b8cSJulian Elischer void
51544990b8cSJulian Elischer thread_exit(void)
51644990b8cSJulian Elischer {
5177e3a96eaSJohn Baldwin 	uint64_t runtime, new_switchtime;
51844990b8cSJulian Elischer 	struct thread *td;
5191c4bcd05SJeff Roberson 	struct thread *td2;
52044990b8cSJulian Elischer 	struct proc *p;
5217847a9daSJohn Baldwin 	int wakeup_swapper;
52244990b8cSJulian Elischer 
52344990b8cSJulian Elischer 	td = curthread;
52444990b8cSJulian Elischer 	p = td->td_proc;
52544990b8cSJulian Elischer 
526a54e85fdSJeff Roberson 	PROC_SLOCK_ASSERT(p, MA_OWNED);
527ed062c8dSJulian Elischer 	mtx_assert(&Giant, MA_NOTOWNED);
528a54e85fdSJeff Roberson 
52944990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
530ed062c8dSJulian Elischer 	KASSERT(p != NULL, ("thread exiting without a process"));
531cc701b73SRobert Watson 	CTR3(KTR_PROC, "thread_exit: thread %p (pid %ld, %s)", td,
532e01eafefSJulian Elischer 	    (long)p->p_pid, td->td_name);
5336c9271a9SAndriy Gapon 	SDT_PROBE0(proc, , , lwp__exit);
5349104847fSDavid Xu 	KASSERT(TAILQ_EMPTY(&td->td_sigqueue.sq_list), ("signal pending"));
53544990b8cSJulian Elischer 
536ed062c8dSJulian Elischer 	/*
537ed062c8dSJulian Elischer 	 * drop FPU & debug register state storage, or any other
538ed062c8dSJulian Elischer 	 * architecture specific resources that
539ed062c8dSJulian Elischer 	 * would not be on a new untouched process.
540ed062c8dSJulian Elischer 	 */
541bd07998eSKonstantin Belousov 	cpu_thread_exit(td);
54244990b8cSJulian Elischer 
543ed062c8dSJulian Elischer 	/*
5441faf202eSJulian Elischer 	 * The last thread is left attached to the process
5451faf202eSJulian Elischer 	 * So that the whole bundle gets recycled. Skip
546ed062c8dSJulian Elischer 	 * all this stuff if we never had threads.
547ed062c8dSJulian Elischer 	 * EXIT clears all sign of other threads when
548ed062c8dSJulian Elischer 	 * it goes to single threading, so the last thread always
549ed062c8dSJulian Elischer 	 * takes the short path.
5501faf202eSJulian Elischer 	 */
551ed062c8dSJulian Elischer 	if (p->p_flag & P_HADTHREADS) {
5521faf202eSJulian Elischer 		if (p->p_numthreads > 1) {
553fd229b5bSKonstantin Belousov 			atomic_add_int(&td->td_proc->p_exitthreads, 1);
554d3a0bd78SJulian Elischer 			thread_unlink(td);
5551c4bcd05SJeff Roberson 			td2 = FIRST_THREAD_IN_PROC(p);
5561c4bcd05SJeff Roberson 			sched_exit_thread(td2, td);
557ed062c8dSJulian Elischer 
558ed062c8dSJulian Elischer 			/*
55944990b8cSJulian Elischer 			 * The test below is NOT true if we are the
5609182554aSKonstantin Belousov 			 * sole exiting thread. P_STOPPED_SINGLE is unset
56144990b8cSJulian Elischer 			 * in exit1() after it is the only survivor.
56244990b8cSJulian Elischer 			 */
5631279572aSDavid Xu 			if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
56444990b8cSJulian Elischer 				if (p->p_numthreads == p->p_suspcount) {
565a54e85fdSJeff Roberson 					thread_lock(p->p_singlethread);
5667847a9daSJohn Baldwin 					wakeup_swapper = thread_unsuspend_one(
56784cdea97SKonstantin Belousov 						p->p_singlethread, p, false);
5687847a9daSJohn Baldwin 					if (wakeup_swapper)
5697847a9daSJohn Baldwin 						kick_proc0();
57044990b8cSJulian Elischer 				}
57144990b8cSJulian Elischer 			}
57248bfcdddSJulian Elischer 
573696058c3SJulian Elischer 			PCPU_SET(deadthread, td);
5741faf202eSJulian Elischer 		} else {
575ed062c8dSJulian Elischer 			/*
576ed062c8dSJulian Elischer 			 * The last thread is exiting.. but not through exit()
577ed062c8dSJulian Elischer 			 */
578ed062c8dSJulian Elischer 			panic ("thread_exit: Last thread exiting on its own");
579ed062c8dSJulian Elischer 		}
5801faf202eSJulian Elischer 	}
58116d95d4fSJoseph Koshy #ifdef	HWPMC_HOOKS
58216d95d4fSJoseph Koshy 	/*
58316d95d4fSJoseph Koshy 	 * If this thread is part of a process that is being tracked by hwpmc(4),
58416d95d4fSJoseph Koshy 	 * inform the module of the thread's impending exit.
58516d95d4fSJoseph Koshy 	 */
5866161b98cSMatt Macy 	if (PMC_PROC_IS_USING_PMCS(td->td_proc)) {
58716d95d4fSJoseph Koshy 		PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT);
5886161b98cSMatt Macy 		PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_EXIT, NULL);
589ebfaf69cSMatt Macy 	} else if (PMC_SYSTEM_SAMPLING_ACTIVE())
590ebfaf69cSMatt Macy 		PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_EXIT_LOG, NULL);
59116d95d4fSJoseph Koshy #endif
592a54e85fdSJeff Roberson 	PROC_UNLOCK(p);
5935c7bebf9SKonstantin Belousov 	PROC_STATLOCK(p);
5945c7bebf9SKonstantin Belousov 	thread_lock(td);
5955c7bebf9SKonstantin Belousov 	PROC_SUNLOCK(p);
5967e3a96eaSJohn Baldwin 
5977e3a96eaSJohn Baldwin 	/* Do the same timestamp bookkeeping that mi_switch() would do. */
5987e3a96eaSJohn Baldwin 	new_switchtime = cpu_ticks();
5997e3a96eaSJohn Baldwin 	runtime = new_switchtime - PCPU_GET(switchtime);
6007e3a96eaSJohn Baldwin 	td->td_runtime += runtime;
6017e3a96eaSJohn Baldwin 	td->td_incruntime += runtime;
6027e3a96eaSJohn Baldwin 	PCPU_SET(switchtime, new_switchtime);
6037e3a96eaSJohn Baldwin 	PCPU_SET(switchticks, ticks);
60483c9dea1SGleb Smirnoff 	VM_CNT_INC(v_swtch);
6057e3a96eaSJohn Baldwin 
6067e3a96eaSJohn Baldwin 	/* Save our resource usage in our process. */
6077e3a96eaSJohn Baldwin 	td->td_ru.ru_nvcsw++;
60861a74c5cSJeff Roberson 	ruxagg_locked(p, td);
6097e3a96eaSJohn Baldwin 	rucollect(&p->p_ru, &td->td_ru);
6105c7bebf9SKonstantin Belousov 	PROC_STATUNLOCK(p);
6117e3a96eaSJohn Baldwin 
612dcc9954eSJulian Elischer 	td->td_state = TDS_INACTIVE;
6133d06b4b3SAttilio Rao #ifdef WITNESS
6143d06b4b3SAttilio Rao 	witness_thread_exit(td);
6153d06b4b3SAttilio Rao #endif
616732d9528SJulian Elischer 	CTR1(KTR_PROC, "thread_exit: cpu_throw() thread %p", td);
617a54e85fdSJeff Roberson 	sched_throw(td);
618cc66ebe2SPeter Wemm 	panic("I'm a teapot!");
61944990b8cSJulian Elischer 	/* NOTREACHED */
62044990b8cSJulian Elischer }
62144990b8cSJulian Elischer 
62244990b8cSJulian Elischer /*
623696058c3SJulian Elischer  * Do any thread specific cleanups that may be needed in wait()
62437814395SPeter Wemm  * called with Giant, proc and schedlock not held.
625696058c3SJulian Elischer  */
626696058c3SJulian Elischer void
627696058c3SJulian Elischer thread_wait(struct proc *p)
628696058c3SJulian Elischer {
629696058c3SJulian Elischer 	struct thread *td;
630696058c3SJulian Elischer 
63137814395SPeter Wemm 	mtx_assert(&Giant, MA_NOTOWNED);
632624bf9e1SKonstantin Belousov 	KASSERT(p->p_numthreads == 1, ("multiple threads in thread_wait()"));
633624bf9e1SKonstantin Belousov 	KASSERT(p->p_exitthreads == 0, ("p_exitthreads leaking"));
634ff8fbcffSJeff Roberson 	td = FIRST_THREAD_IN_PROC(p);
635ff8fbcffSJeff Roberson 	/* Lock the last thread so we spin until it exits cpu_throw(). */
636ff8fbcffSJeff Roberson 	thread_lock(td);
637ff8fbcffSJeff Roberson 	thread_unlock(td);
6382e6b8de4SJeff Roberson 	lock_profile_thread_exit(td);
639d7f687fcSJeff Roberson 	cpuset_rel(td->td_cpuset);
640d7f687fcSJeff Roberson 	td->td_cpuset = NULL;
641696058c3SJulian Elischer 	cpu_thread_clean(td);
6424ea6a9a2SMateusz Guzik 	thread_cow_free(td);
6432d19b736SKonstantin Belousov 	callout_drain(&td->td_slpcallout);
644696058c3SJulian Elischer 	thread_reap();	/* check for zombie threads etc. */
645696058c3SJulian Elischer }
646696058c3SJulian Elischer 
647696058c3SJulian Elischer /*
64844990b8cSJulian Elischer  * Link a thread to a process.
6491faf202eSJulian Elischer  * set up anything that needs to be initialized for it to
6501faf202eSJulian Elischer  * be used by the process.
65144990b8cSJulian Elischer  */
65244990b8cSJulian Elischer void
6538460a577SJohn Birrell thread_link(struct thread *td, struct proc *p)
65444990b8cSJulian Elischer {
65544990b8cSJulian Elischer 
656a54e85fdSJeff Roberson 	/*
657a54e85fdSJeff Roberson 	 * XXX This can't be enabled because it's called for proc0 before
658374ae2a3SJeff Roberson 	 * its lock has been created.
659374ae2a3SJeff Roberson 	 * PROC_LOCK_ASSERT(p, MA_OWNED);
660a54e85fdSJeff Roberson 	 */
66171fad9fdSJulian Elischer 	td->td_state    = TDS_INACTIVE;
66244990b8cSJulian Elischer 	td->td_proc     = p;
663b61ce5b0SJeff Roberson 	td->td_flags    = TDF_INMEM;
66444990b8cSJulian Elischer 
6651faf202eSJulian Elischer 	LIST_INIT(&td->td_contested);
666eea4f254SJeff Roberson 	LIST_INIT(&td->td_lprof[0]);
667eea4f254SJeff Roberson 	LIST_INIT(&td->td_lprof[1]);
668f6eccf96SGleb Smirnoff #ifdef EPOCH_TRACE
669dd902d01SGleb Smirnoff 	SLIST_INIT(&td->td_epochs);
670f6eccf96SGleb Smirnoff #endif
6719104847fSDavid Xu 	sigqueue_init(&td->td_sigqueue, p);
672fd90e2edSJung-uk Kim 	callout_init(&td->td_slpcallout, 1);
67366d8df9dSDaniel Eischen 	TAILQ_INSERT_TAIL(&p->p_threads, td, td_plist);
67444990b8cSJulian Elischer 	p->p_numthreads++;
67544990b8cSJulian Elischer }
67644990b8cSJulian Elischer 
677ed062c8dSJulian Elischer /*
678ed062c8dSJulian Elischer  * Called from:
679ed062c8dSJulian Elischer  *  thread_exit()
680ed062c8dSJulian Elischer  */
681d3a0bd78SJulian Elischer void
682d3a0bd78SJulian Elischer thread_unlink(struct thread *td)
683d3a0bd78SJulian Elischer {
684d3a0bd78SJulian Elischer 	struct proc *p = td->td_proc;
685d3a0bd78SJulian Elischer 
686374ae2a3SJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
687f6eccf96SGleb Smirnoff #ifdef EPOCH_TRACE
688dd902d01SGleb Smirnoff 	MPASS(SLIST_EMPTY(&td->td_epochs));
689f6eccf96SGleb Smirnoff #endif
690dd902d01SGleb Smirnoff 
691d3a0bd78SJulian Elischer 	TAILQ_REMOVE(&p->p_threads, td, td_plist);
692d3a0bd78SJulian Elischer 	p->p_numthreads--;
693d3a0bd78SJulian Elischer 	/* could clear a few other things here */
6948460a577SJohn Birrell 	/* Must  NOT clear links to proc! */
6955c8329edSJulian Elischer }
6965c8329edSJulian Elischer 
69779799053SKonstantin Belousov static int
69879799053SKonstantin Belousov calc_remaining(struct proc *p, int mode)
69979799053SKonstantin Belousov {
70079799053SKonstantin Belousov 	int remaining;
70179799053SKonstantin Belousov 
7027b519077SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
7037b519077SKonstantin Belousov 	PROC_SLOCK_ASSERT(p, MA_OWNED);
70479799053SKonstantin Belousov 	if (mode == SINGLE_EXIT)
70579799053SKonstantin Belousov 		remaining = p->p_numthreads;
70679799053SKonstantin Belousov 	else if (mode == SINGLE_BOUNDARY)
70779799053SKonstantin Belousov 		remaining = p->p_numthreads - p->p_boundary_count;
7086ddcc233SKonstantin Belousov 	else if (mode == SINGLE_NO_EXIT || mode == SINGLE_ALLPROC)
70979799053SKonstantin Belousov 		remaining = p->p_numthreads - p->p_suspcount;
71079799053SKonstantin Belousov 	else
71179799053SKonstantin Belousov 		panic("calc_remaining: wrong mode %d", mode);
71279799053SKonstantin Belousov 	return (remaining);
71379799053SKonstantin Belousov }
71479799053SKonstantin Belousov 
71507a9368aSKonstantin Belousov static int
71607a9368aSKonstantin Belousov remain_for_mode(int mode)
71707a9368aSKonstantin Belousov {
71807a9368aSKonstantin Belousov 
7196ddcc233SKonstantin Belousov 	return (mode == SINGLE_ALLPROC ? 0 : 1);
72007a9368aSKonstantin Belousov }
72107a9368aSKonstantin Belousov 
72207a9368aSKonstantin Belousov static int
72307a9368aSKonstantin Belousov weed_inhib(int mode, struct thread *td2, struct proc *p)
72407a9368aSKonstantin Belousov {
72507a9368aSKonstantin Belousov 	int wakeup_swapper;
72607a9368aSKonstantin Belousov 
72707a9368aSKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
72807a9368aSKonstantin Belousov 	PROC_SLOCK_ASSERT(p, MA_OWNED);
72907a9368aSKonstantin Belousov 	THREAD_LOCK_ASSERT(td2, MA_OWNED);
73007a9368aSKonstantin Belousov 
73107a9368aSKonstantin Belousov 	wakeup_swapper = 0;
73261a74c5cSJeff Roberson 
73361a74c5cSJeff Roberson 	/*
73461a74c5cSJeff Roberson 	 * Since the thread lock is dropped by the scheduler we have
73561a74c5cSJeff Roberson 	 * to retry to check for races.
73661a74c5cSJeff Roberson 	 */
73761a74c5cSJeff Roberson restart:
73807a9368aSKonstantin Belousov 	switch (mode) {
73907a9368aSKonstantin Belousov 	case SINGLE_EXIT:
74061a74c5cSJeff Roberson 		if (TD_IS_SUSPENDED(td2)) {
74184cdea97SKonstantin Belousov 			wakeup_swapper |= thread_unsuspend_one(td2, p, true);
74261a74c5cSJeff Roberson 			thread_lock(td2);
74361a74c5cSJeff Roberson 			goto restart;
74461a74c5cSJeff Roberson 		}
74561a74c5cSJeff Roberson 		if (TD_CAN_ABORT(td2)) {
74607a9368aSKonstantin Belousov 			wakeup_swapper |= sleepq_abort(td2, EINTR);
74761a74c5cSJeff Roberson 			return (wakeup_swapper);
74861a74c5cSJeff Roberson 		}
74907a9368aSKonstantin Belousov 		break;
75007a9368aSKonstantin Belousov 	case SINGLE_BOUNDARY:
75107a9368aSKonstantin Belousov 	case SINGLE_NO_EXIT:
75261a74c5cSJeff Roberson 		if (TD_IS_SUSPENDED(td2) &&
75361a74c5cSJeff Roberson 		    (td2->td_flags & TDF_BOUNDARY) == 0) {
75484cdea97SKonstantin Belousov 			wakeup_swapper |= thread_unsuspend_one(td2, p, false);
75561a74c5cSJeff Roberson 			thread_lock(td2);
75661a74c5cSJeff Roberson 			goto restart;
75761a74c5cSJeff Roberson 		}
75861a74c5cSJeff Roberson 		if (TD_CAN_ABORT(td2)) {
75907a9368aSKonstantin Belousov 			wakeup_swapper |= sleepq_abort(td2, ERESTART);
76061a74c5cSJeff Roberson 			return (wakeup_swapper);
76161a74c5cSJeff Roberson 		}
762917dd390SKonstantin Belousov 		break;
7636ddcc233SKonstantin Belousov 	case SINGLE_ALLPROC:
7646ddcc233SKonstantin Belousov 		/*
7656ddcc233SKonstantin Belousov 		 * ALLPROC suspend tries to avoid spurious EINTR for
7666ddcc233SKonstantin Belousov 		 * threads sleeping interruptable, by suspending the
7676ddcc233SKonstantin Belousov 		 * thread directly, similarly to sig_suspend_threads().
7686ddcc233SKonstantin Belousov 		 * Since such sleep is not performed at the user
7696ddcc233SKonstantin Belousov 		 * boundary, TDF_BOUNDARY flag is not set, and TDF_ALLPROCSUSP
7706ddcc233SKonstantin Belousov 		 * is used to avoid immediate un-suspend.
7716ddcc233SKonstantin Belousov 		 */
7726ddcc233SKonstantin Belousov 		if (TD_IS_SUSPENDED(td2) && (td2->td_flags & (TDF_BOUNDARY |
77361a74c5cSJeff Roberson 		    TDF_ALLPROCSUSP)) == 0) {
77484cdea97SKonstantin Belousov 			wakeup_swapper |= thread_unsuspend_one(td2, p, false);
77561a74c5cSJeff Roberson 			thread_lock(td2);
77661a74c5cSJeff Roberson 			goto restart;
77761a74c5cSJeff Roberson 		}
77861a74c5cSJeff Roberson 		if (TD_CAN_ABORT(td2)) {
7796ddcc233SKonstantin Belousov 			if ((td2->td_flags & TDF_SBDRY) == 0) {
7806ddcc233SKonstantin Belousov 				thread_suspend_one(td2);
7816ddcc233SKonstantin Belousov 				td2->td_flags |= TDF_ALLPROCSUSP;
7826ddcc233SKonstantin Belousov 			} else {
7836ddcc233SKonstantin Belousov 				wakeup_swapper |= sleepq_abort(td2, ERESTART);
78461a74c5cSJeff Roberson 				return (wakeup_swapper);
7856ddcc233SKonstantin Belousov 			}
7866ddcc233SKonstantin Belousov 		}
78707a9368aSKonstantin Belousov 		break;
78861a74c5cSJeff Roberson 	default:
78961a74c5cSJeff Roberson 		break;
79007a9368aSKonstantin Belousov 	}
79161a74c5cSJeff Roberson 	thread_unlock(td2);
79207a9368aSKonstantin Belousov 	return (wakeup_swapper);
79307a9368aSKonstantin Belousov }
79407a9368aSKonstantin Belousov 
7955215b187SJeff Roberson /*
79644990b8cSJulian Elischer  * Enforce single-threading.
79744990b8cSJulian Elischer  *
79844990b8cSJulian Elischer  * Returns 1 if the caller must abort (another thread is waiting to
79944990b8cSJulian Elischer  * exit the process or similar). Process is locked!
80044990b8cSJulian Elischer  * Returns 0 when you are successfully the only thread running.
80144990b8cSJulian Elischer  * A process has successfully single threaded in the suspend mode when
80244990b8cSJulian Elischer  * There are no threads in user mode. Threads in the kernel must be
80344990b8cSJulian Elischer  * allowed to continue until they get to the user boundary. They may even
80444990b8cSJulian Elischer  * copy out their return values and data before suspending. They may however be
805e2668f55SMaxim Konovalov  * accelerated in reaching the user boundary as we will wake up
80644990b8cSJulian Elischer  * any sleeping threads that are interruptable. (PCATCH).
80744990b8cSJulian Elischer  */
80844990b8cSJulian Elischer int
8096ddcc233SKonstantin Belousov thread_single(struct proc *p, int mode)
81044990b8cSJulian Elischer {
81144990b8cSJulian Elischer 	struct thread *td;
81244990b8cSJulian Elischer 	struct thread *td2;
813da7bbd2cSJohn Baldwin 	int remaining, wakeup_swapper;
81444990b8cSJulian Elischer 
81544990b8cSJulian Elischer 	td = curthread;
8166ddcc233SKonstantin Belousov 	KASSERT(mode == SINGLE_EXIT || mode == SINGLE_BOUNDARY ||
8176ddcc233SKonstantin Belousov 	    mode == SINGLE_ALLPROC || mode == SINGLE_NO_EXIT,
8186ddcc233SKonstantin Belousov 	    ("invalid mode %d", mode));
8196ddcc233SKonstantin Belousov 	/*
8206ddcc233SKonstantin Belousov 	 * If allowing non-ALLPROC singlethreading for non-curproc
8216ddcc233SKonstantin Belousov 	 * callers, calc_remaining() and remain_for_mode() should be
8226ddcc233SKonstantin Belousov 	 * adjusted to also account for td->td_proc != p.  For now
8236ddcc233SKonstantin Belousov 	 * this is not implemented because it is not used.
8246ddcc233SKonstantin Belousov 	 */
8256ddcc233SKonstantin Belousov 	KASSERT((mode == SINGLE_ALLPROC && td->td_proc != p) ||
8266ddcc233SKonstantin Belousov 	    (mode != SINGLE_ALLPROC && td->td_proc == p),
8276ddcc233SKonstantin Belousov 	    ("mode %d proc %p curproc %p", mode, p, td->td_proc));
82837814395SPeter Wemm 	mtx_assert(&Giant, MA_NOTOWNED);
82944990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
83044990b8cSJulian Elischer 
8316ddcc233SKonstantin Belousov 	if ((p->p_flag & P_HADTHREADS) == 0 && mode != SINGLE_ALLPROC)
83244990b8cSJulian Elischer 		return (0);
83344990b8cSJulian Elischer 
834e3b9bf71SJulian Elischer 	/* Is someone already single threading? */
835906ac69dSDavid Xu 	if (p->p_singlethread != NULL && p->p_singlethread != td)
83644990b8cSJulian Elischer 		return (1);
83744990b8cSJulian Elischer 
838906ac69dSDavid Xu 	if (mode == SINGLE_EXIT) {
839906ac69dSDavid Xu 		p->p_flag |= P_SINGLE_EXIT;
840906ac69dSDavid Xu 		p->p_flag &= ~P_SINGLE_BOUNDARY;
841906ac69dSDavid Xu 	} else {
842906ac69dSDavid Xu 		p->p_flag &= ~P_SINGLE_EXIT;
843906ac69dSDavid Xu 		if (mode == SINGLE_BOUNDARY)
844906ac69dSDavid Xu 			p->p_flag |= P_SINGLE_BOUNDARY;
845906ac69dSDavid Xu 		else
846906ac69dSDavid Xu 			p->p_flag &= ~P_SINGLE_BOUNDARY;
847906ac69dSDavid Xu 	}
8486ddcc233SKonstantin Belousov 	if (mode == SINGLE_ALLPROC)
8496ddcc233SKonstantin Belousov 		p->p_flag |= P_TOTAL_STOP;
8501279572aSDavid Xu 	p->p_flag |= P_STOPPED_SINGLE;
8517b4a950aSDavid Xu 	PROC_SLOCK(p);
852112afcb2SJohn Baldwin 	p->p_singlethread = td;
85379799053SKonstantin Belousov 	remaining = calc_remaining(p, mode);
85407a9368aSKonstantin Belousov 	while (remaining != remain_for_mode(mode)) {
855bf1a3220SDavid Xu 		if (P_SHOULDSTOP(p) != P_STOPPED_SINGLE)
856bf1a3220SDavid Xu 			goto stopme;
857da7bbd2cSJohn Baldwin 		wakeup_swapper = 0;
85844990b8cSJulian Elischer 		FOREACH_THREAD_IN_PROC(p, td2) {
85944990b8cSJulian Elischer 			if (td2 == td)
86044990b8cSJulian Elischer 				continue;
861a54e85fdSJeff Roberson 			thread_lock(td2);
862b7edba77SJeff Roberson 			td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
8636ddcc233SKonstantin Belousov 			if (TD_IS_INHIBITED(td2)) {
86407a9368aSKonstantin Belousov 				wakeup_swapper |= weed_inhib(mode, td2, p);
865d8267df7SDavid Xu #ifdef SMP
8666ddcc233SKonstantin Belousov 			} else if (TD_IS_RUNNING(td2) && td != td2) {
867d8267df7SDavid Xu 				forward_signal(td2);
86861a74c5cSJeff Roberson 				thread_unlock(td2);
869d8267df7SDavid Xu #endif
87061a74c5cSJeff Roberson 			} else
871a54e85fdSJeff Roberson 				thread_unlock(td2);
8729d102777SJulian Elischer 		}
873da7bbd2cSJohn Baldwin 		if (wakeup_swapper)
874da7bbd2cSJohn Baldwin 			kick_proc0();
87579799053SKonstantin Belousov 		remaining = calc_remaining(p, mode);
876ec008e96SDavid Xu 
8779d102777SJulian Elischer 		/*
8789d102777SJulian Elischer 		 * Maybe we suspended some threads.. was it enough?
8799d102777SJulian Elischer 		 */
88007a9368aSKonstantin Belousov 		if (remaining == remain_for_mode(mode))
8819d102777SJulian Elischer 			break;
8829d102777SJulian Elischer 
883bf1a3220SDavid Xu stopme:
88444990b8cSJulian Elischer 		/*
88544990b8cSJulian Elischer 		 * Wake us up when everyone else has suspended.
886e3b9bf71SJulian Elischer 		 * In the mean time we suspend as well.
88744990b8cSJulian Elischer 		 */
8886ddcc233SKonstantin Belousov 		thread_suspend_switch(td, p);
88979799053SKonstantin Belousov 		remaining = calc_remaining(p, mode);
89044990b8cSJulian Elischer 	}
891906ac69dSDavid Xu 	if (mode == SINGLE_EXIT) {
89291599697SJulian Elischer 		/*
8938626a0ddSKonstantin Belousov 		 * Convert the process to an unthreaded process.  The
8948626a0ddSKonstantin Belousov 		 * SINGLE_EXIT is called by exit1() or execve(), in
8958626a0ddSKonstantin Belousov 		 * both cases other threads must be retired.
89691599697SJulian Elischer 		 */
8978626a0ddSKonstantin Belousov 		KASSERT(p->p_numthreads == 1, ("Unthreading with >1 threads"));
898ed062c8dSJulian Elischer 		p->p_singlethread = NULL;
8998626a0ddSKonstantin Belousov 		p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_HADTHREADS);
900fd229b5bSKonstantin Belousov 
901fd229b5bSKonstantin Belousov 		/*
902fd229b5bSKonstantin Belousov 		 * Wait for any remaining threads to exit cpu_throw().
903fd229b5bSKonstantin Belousov 		 */
904fd229b5bSKonstantin Belousov 		while (p->p_exitthreads != 0) {
905fd229b5bSKonstantin Belousov 			PROC_SUNLOCK(p);
906fd229b5bSKonstantin Belousov 			PROC_UNLOCK(p);
907fd229b5bSKonstantin Belousov 			sched_relinquish(td);
908fd229b5bSKonstantin Belousov 			PROC_LOCK(p);
909fd229b5bSKonstantin Belousov 			PROC_SLOCK(p);
910fd229b5bSKonstantin Belousov 		}
911ac437c07SKonstantin Belousov 	} else if (mode == SINGLE_BOUNDARY) {
912ac437c07SKonstantin Belousov 		/*
913ac437c07SKonstantin Belousov 		 * Wait until all suspended threads are removed from
914ac437c07SKonstantin Belousov 		 * the processors.  The thread_suspend_check()
915ac437c07SKonstantin Belousov 		 * increments p_boundary_count while it is still
916ac437c07SKonstantin Belousov 		 * running, which makes it possible for the execve()
917ac437c07SKonstantin Belousov 		 * to destroy vmspace while our other threads are
918ac437c07SKonstantin Belousov 		 * still using the address space.
919ac437c07SKonstantin Belousov 		 *
920ac437c07SKonstantin Belousov 		 * We lock the thread, which is only allowed to
921ac437c07SKonstantin Belousov 		 * succeed after context switch code finished using
922ac437c07SKonstantin Belousov 		 * the address space.
923ac437c07SKonstantin Belousov 		 */
924ac437c07SKonstantin Belousov 		FOREACH_THREAD_IN_PROC(p, td2) {
925ac437c07SKonstantin Belousov 			if (td2 == td)
926ac437c07SKonstantin Belousov 				continue;
927ac437c07SKonstantin Belousov 			thread_lock(td2);
928ac437c07SKonstantin Belousov 			KASSERT((td2->td_flags & TDF_BOUNDARY) != 0,
929ac437c07SKonstantin Belousov 			    ("td %p not on boundary", td2));
930ac437c07SKonstantin Belousov 			KASSERT(TD_IS_SUSPENDED(td2),
931ac437c07SKonstantin Belousov 			    ("td %p is not suspended", td2));
932ac437c07SKonstantin Belousov 			thread_unlock(td2);
933ac437c07SKonstantin Belousov 		}
93491599697SJulian Elischer 	}
9357b4a950aSDavid Xu 	PROC_SUNLOCK(p);
93644990b8cSJulian Elischer 	return (0);
93744990b8cSJulian Elischer }
93844990b8cSJulian Elischer 
9398638fe7bSKonstantin Belousov bool
9408638fe7bSKonstantin Belousov thread_suspend_check_needed(void)
9418638fe7bSKonstantin Belousov {
9428638fe7bSKonstantin Belousov 	struct proc *p;
9438638fe7bSKonstantin Belousov 	struct thread *td;
9448638fe7bSKonstantin Belousov 
9458638fe7bSKonstantin Belousov 	td = curthread;
9468638fe7bSKonstantin Belousov 	p = td->td_proc;
9478638fe7bSKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
9488638fe7bSKonstantin Belousov 	return (P_SHOULDSTOP(p) || ((p->p_flag & P_TRACED) != 0 &&
9498638fe7bSKonstantin Belousov 	    (td->td_dbgflags & TDB_SUSPEND) != 0));
9508638fe7bSKonstantin Belousov }
9518638fe7bSKonstantin Belousov 
95244990b8cSJulian Elischer /*
95344990b8cSJulian Elischer  * Called in from locations that can safely check to see
95444990b8cSJulian Elischer  * whether we have to suspend or at least throttle for a
95544990b8cSJulian Elischer  * single-thread event (e.g. fork).
95644990b8cSJulian Elischer  *
95744990b8cSJulian Elischer  * Such locations include userret().
95844990b8cSJulian Elischer  * If the "return_instead" argument is non zero, the thread must be able to
95944990b8cSJulian Elischer  * accept 0 (caller may continue), or 1 (caller must abort) as a result.
96044990b8cSJulian Elischer  *
96144990b8cSJulian Elischer  * The 'return_instead' argument tells the function if it may do a
96244990b8cSJulian Elischer  * thread_exit() or suspend, or whether the caller must abort and back
96344990b8cSJulian Elischer  * out instead.
96444990b8cSJulian Elischer  *
96544990b8cSJulian Elischer  * If the thread that set the single_threading request has set the
96644990b8cSJulian Elischer  * P_SINGLE_EXIT bit in the process flags then this call will never return
96744990b8cSJulian Elischer  * if 'return_instead' is false, but will exit.
96844990b8cSJulian Elischer  *
96944990b8cSJulian Elischer  * P_SINGLE_EXIT | return_instead == 0| return_instead != 0
97044990b8cSJulian Elischer  *---------------+--------------------+---------------------
97144990b8cSJulian Elischer  *       0       | returns 0          |   returns 0 or 1
972353374b5SJohn Baldwin  *               | when ST ends       |   immediately
97344990b8cSJulian Elischer  *---------------+--------------------+---------------------
97444990b8cSJulian Elischer  *       1       | thread exits       |   returns 1
975353374b5SJohn Baldwin  *               |                    |  immediately
97644990b8cSJulian Elischer  * 0 = thread_exit() or suspension ok,
97744990b8cSJulian Elischer  * other = return error instead of stopping the thread.
97844990b8cSJulian Elischer  *
97944990b8cSJulian Elischer  * While a full suspension is under effect, even a single threading
98044990b8cSJulian Elischer  * thread would be suspended if it made this call (but it shouldn't).
98144990b8cSJulian Elischer  * This call should only be made from places where
98244990b8cSJulian Elischer  * thread_exit() would be safe as that may be the outcome unless
98344990b8cSJulian Elischer  * return_instead is set.
98444990b8cSJulian Elischer  */
98544990b8cSJulian Elischer int
98644990b8cSJulian Elischer thread_suspend_check(int return_instead)
98744990b8cSJulian Elischer {
988ecafb24bSJuli Mallett 	struct thread *td;
989ecafb24bSJuli Mallett 	struct proc *p;
99046e47c4fSKonstantin Belousov 	int wakeup_swapper;
99144990b8cSJulian Elischer 
99244990b8cSJulian Elischer 	td = curthread;
99344990b8cSJulian Elischer 	p = td->td_proc;
99437814395SPeter Wemm 	mtx_assert(&Giant, MA_NOTOWNED);
99544990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
9968638fe7bSKonstantin Belousov 	while (thread_suspend_check_needed()) {
9971279572aSDavid Xu 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
99844990b8cSJulian Elischer 			KASSERT(p->p_singlethread != NULL,
99944990b8cSJulian Elischer 			    ("singlethread not set"));
100044990b8cSJulian Elischer 			/*
1001e3b9bf71SJulian Elischer 			 * The only suspension in action is a
1002e3b9bf71SJulian Elischer 			 * single-threading. Single threader need not stop.
1003bd07998eSKonstantin Belousov 			 * It is safe to access p->p_singlethread unlocked
1004bd07998eSKonstantin Belousov 			 * because it can only be set to our address by us.
100544990b8cSJulian Elischer 			 */
1006e3b9bf71SJulian Elischer 			if (p->p_singlethread == td)
100744990b8cSJulian Elischer 				return (0);	/* Exempt from stopping. */
100844990b8cSJulian Elischer 		}
100945a4bfa1SDavid Xu 		if ((p->p_flag & P_SINGLE_EXIT) && return_instead)
101094f0972bSDavid Xu 			return (EINTR);
101144990b8cSJulian Elischer 
1012906ac69dSDavid Xu 		/* Should we goto user boundary if we didn't come from there? */
1013906ac69dSDavid Xu 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE &&
1014906ac69dSDavid Xu 		    (p->p_flag & P_SINGLE_BOUNDARY) && return_instead)
101594f0972bSDavid Xu 			return (ERESTART);
1016906ac69dSDavid Xu 
101744990b8cSJulian Elischer 		/*
10183077f938SKonstantin Belousov 		 * Ignore suspend requests if they are deferred.
1019d071a6faSJohn Baldwin 		 */
10203077f938SKonstantin Belousov 		if ((td->td_flags & TDF_SBDRY) != 0) {
1021d071a6faSJohn Baldwin 			KASSERT(return_instead,
1022d071a6faSJohn Baldwin 			    ("TDF_SBDRY set for unsafe thread_suspend_check"));
102346e47c4fSKonstantin Belousov 			KASSERT((td->td_flags & (TDF_SEINTR | TDF_SERESTART)) !=
102446e47c4fSKonstantin Belousov 			    (TDF_SEINTR | TDF_SERESTART),
102546e47c4fSKonstantin Belousov 			    ("both TDF_SEINTR and TDF_SERESTART"));
102646e47c4fSKonstantin Belousov 			return (TD_SBDRY_INTR(td) ? TD_SBDRY_ERRNO(td) : 0);
1027d071a6faSJohn Baldwin 		}
1028d071a6faSJohn Baldwin 
1029d071a6faSJohn Baldwin 		/*
103044990b8cSJulian Elischer 		 * If the process is waiting for us to exit,
103144990b8cSJulian Elischer 		 * this thread should just suicide.
10321279572aSDavid Xu 		 * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE.
103344990b8cSJulian Elischer 		 */
1034cf7d9a8cSDavid Xu 		if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) {
1035cf7d9a8cSDavid Xu 			PROC_UNLOCK(p);
103691d1786fSDmitry Chagin 
103791d1786fSDmitry Chagin 			/*
103891d1786fSDmitry Chagin 			 * Allow Linux emulation layer to do some work
103991d1786fSDmitry Chagin 			 * before thread suicide.
104091d1786fSDmitry Chagin 			 */
104191d1786fSDmitry Chagin 			if (__predict_false(p->p_sysent->sv_thread_detach != NULL))
104291d1786fSDmitry Chagin 				(p->p_sysent->sv_thread_detach)(td);
10432a339d9eSKonstantin Belousov 			umtx_thread_exit(td);
1044d1e7a4a5SJohn Baldwin 			kern_thr_exit(td);
1045d1e7a4a5SJohn Baldwin 			panic("stopped thread did not exit");
1046cf7d9a8cSDavid Xu 		}
104721ecd1e9SDavid Xu 
104821ecd1e9SDavid Xu 		PROC_SLOCK(p);
104921ecd1e9SDavid Xu 		thread_stopped(p);
1050a54e85fdSJeff Roberson 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
1051a54e85fdSJeff Roberson 			if (p->p_numthreads == p->p_suspcount + 1) {
1052a54e85fdSJeff Roberson 				thread_lock(p->p_singlethread);
105384cdea97SKonstantin Belousov 				wakeup_swapper = thread_unsuspend_one(
105484cdea97SKonstantin Belousov 				    p->p_singlethread, p, false);
10557847a9daSJohn Baldwin 				if (wakeup_swapper)
10567847a9daSJohn Baldwin 					kick_proc0();
1057a54e85fdSJeff Roberson 			}
1058a54e85fdSJeff Roberson 		}
10593f9be10eSDavid Xu 		PROC_UNLOCK(p);
10607b4a950aSDavid Xu 		thread_lock(td);
106144990b8cSJulian Elischer 		/*
106244990b8cSJulian Elischer 		 * When a thread suspends, it just
1063ad1e7d28SJulian Elischer 		 * gets taken off all queues.
106444990b8cSJulian Elischer 		 */
106571fad9fdSJulian Elischer 		thread_suspend_one(td);
1066906ac69dSDavid Xu 		if (return_instead == 0) {
1067906ac69dSDavid Xu 			p->p_boundary_count++;
1068906ac69dSDavid Xu 			td->td_flags |= TDF_BOUNDARY;
1069cf19bf91SJulian Elischer 		}
10707b4a950aSDavid Xu 		PROC_SUNLOCK(p);
1071686bcb5cSJeff Roberson 		mi_switch(SW_INVOL | SWT_SUSPEND);
107244990b8cSJulian Elischer 		PROC_LOCK(p);
107344990b8cSJulian Elischer 	}
107444990b8cSJulian Elischer 	return (0);
107544990b8cSJulian Elischer }
107644990b8cSJulian Elischer 
1077*478ca4b0SKonstantin Belousov /*
1078*478ca4b0SKonstantin Belousov  * Check for possible stops and suspensions while executing a
1079*478ca4b0SKonstantin Belousov  * casueword or similar transiently failing operation.
1080*478ca4b0SKonstantin Belousov  *
1081*478ca4b0SKonstantin Belousov  * The sleep argument controls whether the function can handle a stop
1082*478ca4b0SKonstantin Belousov  * request itself or it should return ERESTART and the request is
1083*478ca4b0SKonstantin Belousov  * proceed at the kernel/user boundary in ast.
1084*478ca4b0SKonstantin Belousov  *
1085*478ca4b0SKonstantin Belousov  * Typically, when retrying due to casueword(9) failure (rv == 1), we
1086*478ca4b0SKonstantin Belousov  * should handle the stop requests there, with exception of cases when
1087*478ca4b0SKonstantin Belousov  * the thread owns a kernel resource, for instance busied the umtx
1088*478ca4b0SKonstantin Belousov  * key, or when functions return immediately if casueword_check_susp()
1089*478ca4b0SKonstantin Belousov  * returned non-zero.  On the other hand, retrying the whole lock
1090*478ca4b0SKonstantin Belousov  * operation, we better not stop there but delegate the handling to
1091*478ca4b0SKonstantin Belousov  * ast.
1092*478ca4b0SKonstantin Belousov  *
1093*478ca4b0SKonstantin Belousov  * If the request is for thread termination P_SINGLE_EXIT, we cannot
1094*478ca4b0SKonstantin Belousov  * handle it at all, and simply return EINTR.
1095*478ca4b0SKonstantin Belousov  */
1096*478ca4b0SKonstantin Belousov int
1097*478ca4b0SKonstantin Belousov thread_check_susp(struct thread *td, bool sleep)
1098*478ca4b0SKonstantin Belousov {
1099*478ca4b0SKonstantin Belousov 	struct proc *p;
1100*478ca4b0SKonstantin Belousov 	int error;
1101*478ca4b0SKonstantin Belousov 
1102*478ca4b0SKonstantin Belousov 	/*
1103*478ca4b0SKonstantin Belousov 	 * The check for TDF_NEEDSUSPCHK is racy, but it is enough to
1104*478ca4b0SKonstantin Belousov 	 * eventually break the lockstep loop.
1105*478ca4b0SKonstantin Belousov 	 */
1106*478ca4b0SKonstantin Belousov 	if ((td->td_flags & TDF_NEEDSUSPCHK) == 0)
1107*478ca4b0SKonstantin Belousov 		return (0);
1108*478ca4b0SKonstantin Belousov 	error = 0;
1109*478ca4b0SKonstantin Belousov 	p = td->td_proc;
1110*478ca4b0SKonstantin Belousov 	PROC_LOCK(p);
1111*478ca4b0SKonstantin Belousov 	if (p->p_flag & P_SINGLE_EXIT)
1112*478ca4b0SKonstantin Belousov 		error = EINTR;
1113*478ca4b0SKonstantin Belousov 	else if (P_SHOULDSTOP(p) ||
1114*478ca4b0SKonstantin Belousov 	    ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_SUSPEND)))
1115*478ca4b0SKonstantin Belousov 		error = sleep ? thread_suspend_check(0) : ERESTART;
1116*478ca4b0SKonstantin Belousov 	PROC_UNLOCK(p);
1117*478ca4b0SKonstantin Belousov 	return (error);
1118*478ca4b0SKonstantin Belousov }
1119*478ca4b0SKonstantin Belousov 
112035c32a76SDavid Xu void
11216ddcc233SKonstantin Belousov thread_suspend_switch(struct thread *td, struct proc *p)
1122a54e85fdSJeff Roberson {
1123a54e85fdSJeff Roberson 
1124a54e85fdSJeff Roberson 	KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
1125a54e85fdSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
11267b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
1127a54e85fdSJeff Roberson 	/*
1128a54e85fdSJeff Roberson 	 * We implement thread_suspend_one in stages here to avoid
1129a54e85fdSJeff Roberson 	 * dropping the proc lock while the thread lock is owned.
1130a54e85fdSJeff Roberson 	 */
11316ddcc233SKonstantin Belousov 	if (p == td->td_proc) {
1132a54e85fdSJeff Roberson 		thread_stopped(p);
1133a54e85fdSJeff Roberson 		p->p_suspcount++;
11346ddcc233SKonstantin Belousov 	}
11353f9be10eSDavid Xu 	PROC_UNLOCK(p);
11367b4a950aSDavid Xu 	thread_lock(td);
1137b7edba77SJeff Roberson 	td->td_flags &= ~TDF_NEEDSUSPCHK;
1138a54e85fdSJeff Roberson 	TD_SET_SUSPENDED(td);
1139c5aa6b58SJeff Roberson 	sched_sleep(td, 0);
11407b4a950aSDavid Xu 	PROC_SUNLOCK(p);
1141a54e85fdSJeff Roberson 	DROP_GIANT();
1142686bcb5cSJeff Roberson 	mi_switch(SW_VOL | SWT_SUSPEND);
1143a54e85fdSJeff Roberson 	PICKUP_GIANT();
1144a54e85fdSJeff Roberson 	PROC_LOCK(p);
11457b4a950aSDavid Xu 	PROC_SLOCK(p);
1146a54e85fdSJeff Roberson }
1147a54e85fdSJeff Roberson 
1148a54e85fdSJeff Roberson void
114935c32a76SDavid Xu thread_suspend_one(struct thread *td)
115035c32a76SDavid Xu {
11516ddcc233SKonstantin Belousov 	struct proc *p;
115235c32a76SDavid Xu 
11536ddcc233SKonstantin Belousov 	p = td->td_proc;
11547b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
1155a54e85fdSJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1156e574e444SDavid Xu 	KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
115735c32a76SDavid Xu 	p->p_suspcount++;
1158b7edba77SJeff Roberson 	td->td_flags &= ~TDF_NEEDSUSPCHK;
115971fad9fdSJulian Elischer 	TD_SET_SUSPENDED(td);
1160c5aa6b58SJeff Roberson 	sched_sleep(td, 0);
116135c32a76SDavid Xu }
116235c32a76SDavid Xu 
116384cdea97SKonstantin Belousov static int
116484cdea97SKonstantin Belousov thread_unsuspend_one(struct thread *td, struct proc *p, bool boundary)
116535c32a76SDavid Xu {
116635c32a76SDavid Xu 
1167a54e85fdSJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1168ad1e7d28SJulian Elischer 	KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended"));
116971fad9fdSJulian Elischer 	TD_CLR_SUSPENDED(td);
11706ddcc233SKonstantin Belousov 	td->td_flags &= ~TDF_ALLPROCSUSP;
11716ddcc233SKonstantin Belousov 	if (td->td_proc == p) {
11726ddcc233SKonstantin Belousov 		PROC_SLOCK_ASSERT(p, MA_OWNED);
117335c32a76SDavid Xu 		p->p_suspcount--;
117484cdea97SKonstantin Belousov 		if (boundary && (td->td_flags & TDF_BOUNDARY) != 0) {
117584cdea97SKonstantin Belousov 			td->td_flags &= ~TDF_BOUNDARY;
117684cdea97SKonstantin Belousov 			p->p_boundary_count--;
117784cdea97SKonstantin Belousov 		}
11786ddcc233SKonstantin Belousov 	}
117961a74c5cSJeff Roberson 	return (setrunnable(td, 0));
118035c32a76SDavid Xu }
118135c32a76SDavid Xu 
118244990b8cSJulian Elischer /*
118344990b8cSJulian Elischer  * Allow all threads blocked by single threading to continue running.
118444990b8cSJulian Elischer  */
118544990b8cSJulian Elischer void
118644990b8cSJulian Elischer thread_unsuspend(struct proc *p)
118744990b8cSJulian Elischer {
118844990b8cSJulian Elischer 	struct thread *td;
11897847a9daSJohn Baldwin 	int wakeup_swapper;
119044990b8cSJulian Elischer 
119144990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
11927b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
11937847a9daSJohn Baldwin 	wakeup_swapper = 0;
119444990b8cSJulian Elischer 	if (!P_SHOULDSTOP(p)) {
1195ad1e7d28SJulian Elischer                 FOREACH_THREAD_IN_PROC(p, td) {
1196a54e85fdSJeff Roberson 			thread_lock(td);
1197ad1e7d28SJulian Elischer 			if (TD_IS_SUSPENDED(td)) {
119884cdea97SKonstantin Belousov 				wakeup_swapper |= thread_unsuspend_one(td, p,
119984cdea97SKonstantin Belousov 				    true);
120061a74c5cSJeff Roberson 			} else
1201a54e85fdSJeff Roberson 				thread_unlock(td);
1202ad1e7d28SJulian Elischer 		}
120384cdea97SKonstantin Belousov 	} else if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE &&
120484cdea97SKonstantin Belousov 	    p->p_numthreads == p->p_suspcount) {
120544990b8cSJulian Elischer 		/*
120644990b8cSJulian Elischer 		 * Stopping everything also did the job for the single
120744990b8cSJulian Elischer 		 * threading request. Now we've downgraded to single-threaded,
120844990b8cSJulian Elischer 		 * let it continue.
120944990b8cSJulian Elischer 		 */
12106ddcc233SKonstantin Belousov 		if (p->p_singlethread->td_proc == p) {
1211a54e85fdSJeff Roberson 			thread_lock(p->p_singlethread);
12126ddcc233SKonstantin Belousov 			wakeup_swapper = thread_unsuspend_one(
121384cdea97SKonstantin Belousov 			    p->p_singlethread, p, false);
121444990b8cSJulian Elischer 		}
12156ddcc233SKonstantin Belousov 	}
12167847a9daSJohn Baldwin 	if (wakeup_swapper)
12177847a9daSJohn Baldwin 		kick_proc0();
121844990b8cSJulian Elischer }
121944990b8cSJulian Elischer 
1220ed062c8dSJulian Elischer /*
1221ed062c8dSJulian Elischer  * End the single threading mode..
1222ed062c8dSJulian Elischer  */
122344990b8cSJulian Elischer void
12246ddcc233SKonstantin Belousov thread_single_end(struct proc *p, int mode)
122544990b8cSJulian Elischer {
122644990b8cSJulian Elischer 	struct thread *td;
12277847a9daSJohn Baldwin 	int wakeup_swapper;
122844990b8cSJulian Elischer 
12296ddcc233SKonstantin Belousov 	KASSERT(mode == SINGLE_EXIT || mode == SINGLE_BOUNDARY ||
12306ddcc233SKonstantin Belousov 	    mode == SINGLE_ALLPROC || mode == SINGLE_NO_EXIT,
12316ddcc233SKonstantin Belousov 	    ("invalid mode %d", mode));
123244990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
12336ddcc233SKonstantin Belousov 	KASSERT((mode == SINGLE_ALLPROC && (p->p_flag & P_TOTAL_STOP) != 0) ||
12346ddcc233SKonstantin Belousov 	    (mode != SINGLE_ALLPROC && (p->p_flag & P_TOTAL_STOP) == 0),
12356ddcc233SKonstantin Belousov 	    ("mode %d does not match P_TOTAL_STOP", mode));
123684cdea97SKonstantin Belousov 	KASSERT(mode == SINGLE_ALLPROC || p->p_singlethread == curthread,
123784cdea97SKonstantin Belousov 	    ("thread_single_end from other thread %p %p",
123884cdea97SKonstantin Belousov 	    curthread, p->p_singlethread));
123984cdea97SKonstantin Belousov 	KASSERT(mode != SINGLE_BOUNDARY ||
124084cdea97SKonstantin Belousov 	    (p->p_flag & P_SINGLE_BOUNDARY) != 0,
124184cdea97SKonstantin Belousov 	    ("mis-matched SINGLE_BOUNDARY flags %x", p->p_flag));
12426ddcc233SKonstantin Belousov 	p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_SINGLE_BOUNDARY |
12436ddcc233SKonstantin Belousov 	    P_TOTAL_STOP);
12447b4a950aSDavid Xu 	PROC_SLOCK(p);
124544990b8cSJulian Elischer 	p->p_singlethread = NULL;
12467847a9daSJohn Baldwin 	wakeup_swapper = 0;
124749539972SJulian Elischer 	/*
12487847a9daSJohn Baldwin 	 * If there are other threads they may now run,
124949539972SJulian Elischer 	 * unless of course there is a blanket 'stop order'
125049539972SJulian Elischer 	 * on the process. The single threader must be allowed
125149539972SJulian Elischer 	 * to continue however as this is a bad place to stop.
125249539972SJulian Elischer 	 */
12536ddcc233SKonstantin Belousov 	if (p->p_numthreads != remain_for_mode(mode) && !P_SHOULDSTOP(p)) {
1254ad1e7d28SJulian Elischer                 FOREACH_THREAD_IN_PROC(p, td) {
1255a54e85fdSJeff Roberson 			thread_lock(td);
1256ad1e7d28SJulian Elischer 			if (TD_IS_SUSPENDED(td)) {
125784cdea97SKonstantin Belousov 				wakeup_swapper |= thread_unsuspend_one(td, p,
125884cdea97SKonstantin Belousov 				    mode == SINGLE_BOUNDARY);
125961a74c5cSJeff Roberson 			} else
1260a54e85fdSJeff Roberson 				thread_unlock(td);
126149539972SJulian Elischer 		}
1262ad1e7d28SJulian Elischer 	}
126384cdea97SKonstantin Belousov 	KASSERT(mode != SINGLE_BOUNDARY || p->p_boundary_count == 0,
126484cdea97SKonstantin Belousov 	    ("inconsistent boundary count %d", p->p_boundary_count));
12657b4a950aSDavid Xu 	PROC_SUNLOCK(p);
12667847a9daSJohn Baldwin 	if (wakeup_swapper)
12677847a9daSJohn Baldwin 		kick_proc0();
126849539972SJulian Elischer }
12694fc21c09SDaniel Eischen 
127044355392SDavid Xu struct thread *
127144355392SDavid Xu thread_find(struct proc *p, lwpid_t tid)
127244355392SDavid Xu {
127344355392SDavid Xu 	struct thread *td;
127444355392SDavid Xu 
127544355392SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
127644355392SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td) {
127744355392SDavid Xu 		if (td->td_tid == tid)
127844355392SDavid Xu 			break;
127944355392SDavid Xu 	}
128044355392SDavid Xu 	return (td);
128144355392SDavid Xu }
1282cf7d9a8cSDavid Xu 
1283cf7d9a8cSDavid Xu /* Locate a thread by number; return with proc lock held. */
1284cf7d9a8cSDavid Xu struct thread *
1285cf7d9a8cSDavid Xu tdfind(lwpid_t tid, pid_t pid)
1286cf7d9a8cSDavid Xu {
1287cf7d9a8cSDavid Xu #define RUN_THRESH	16
1288cf7d9a8cSDavid Xu 	struct thread *td;
1289cf7d9a8cSDavid Xu 	int run = 0;
1290cf7d9a8cSDavid Xu 
1291cf7d9a8cSDavid Xu 	rw_rlock(&tidhash_lock);
1292cf7d9a8cSDavid Xu 	LIST_FOREACH(td, TIDHASH(tid), td_hash) {
1293cf7d9a8cSDavid Xu 		if (td->td_tid == tid) {
1294cf7d9a8cSDavid Xu 			if (pid != -1 && td->td_proc->p_pid != pid) {
1295cf7d9a8cSDavid Xu 				td = NULL;
1296cf7d9a8cSDavid Xu 				break;
1297cf7d9a8cSDavid Xu 			}
12988e6fa660SJohn Baldwin 			PROC_LOCK(td->td_proc);
1299cf7d9a8cSDavid Xu 			if (td->td_proc->p_state == PRS_NEW) {
13008e6fa660SJohn Baldwin 				PROC_UNLOCK(td->td_proc);
1301cf7d9a8cSDavid Xu 				td = NULL;
1302cf7d9a8cSDavid Xu 				break;
1303cf7d9a8cSDavid Xu 			}
1304cf7d9a8cSDavid Xu 			if (run > RUN_THRESH) {
1305cf7d9a8cSDavid Xu 				if (rw_try_upgrade(&tidhash_lock)) {
1306cf7d9a8cSDavid Xu 					LIST_REMOVE(td, td_hash);
1307cf7d9a8cSDavid Xu 					LIST_INSERT_HEAD(TIDHASH(td->td_tid),
1308cf7d9a8cSDavid Xu 						td, td_hash);
1309cf7d9a8cSDavid Xu 					rw_wunlock(&tidhash_lock);
1310cf7d9a8cSDavid Xu 					return (td);
1311cf7d9a8cSDavid Xu 				}
1312cf7d9a8cSDavid Xu 			}
1313cf7d9a8cSDavid Xu 			break;
1314cf7d9a8cSDavid Xu 		}
1315cf7d9a8cSDavid Xu 		run++;
1316cf7d9a8cSDavid Xu 	}
1317cf7d9a8cSDavid Xu 	rw_runlock(&tidhash_lock);
1318cf7d9a8cSDavid Xu 	return (td);
1319cf7d9a8cSDavid Xu }
1320cf7d9a8cSDavid Xu 
1321cf7d9a8cSDavid Xu void
1322cf7d9a8cSDavid Xu tidhash_add(struct thread *td)
1323cf7d9a8cSDavid Xu {
1324cf7d9a8cSDavid Xu 	rw_wlock(&tidhash_lock);
1325cf7d9a8cSDavid Xu 	LIST_INSERT_HEAD(TIDHASH(td->td_tid), td, td_hash);
1326cf7d9a8cSDavid Xu 	rw_wunlock(&tidhash_lock);
1327cf7d9a8cSDavid Xu }
1328cf7d9a8cSDavid Xu 
1329cf7d9a8cSDavid Xu void
1330cf7d9a8cSDavid Xu tidhash_remove(struct thread *td)
1331cf7d9a8cSDavid Xu {
1332cf7d9a8cSDavid Xu 	rw_wlock(&tidhash_lock);
1333cf7d9a8cSDavid Xu 	LIST_REMOVE(td, td_hash);
1334cf7d9a8cSDavid Xu 	rw_wunlock(&tidhash_lock);
1335cf7d9a8cSDavid Xu }
1336