xref: /freebsd/sys/kern/kern_thread.c (revision a9568cd2bcc2939a5640eb55061fd9d9828f5c97)
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>
4335bb59edSMateusz Guzik #include <sys/bitstring.h>
446febf180SGleb Smirnoff #include <sys/epoch.h>
458f0e9130SKonstantin Belousov #include <sys/rangelock.h>
46e170bfdaSDavid Xu #include <sys/resourcevar.h>
47b3e9e682SRyan Stone #include <sys/sdt.h>
4894e0a4cdSJulian Elischer #include <sys/smp.h>
49de028f5aSJeff Roberson #include <sys/sched.h>
5044f3b092SJohn Baldwin #include <sys/sleepqueue.h>
51ace8398dSJeff Roberson #include <sys/selinfo.h>
52d1e7a4a5SJohn Baldwin #include <sys/syscallsubr.h>
5391d1786fSDmitry Chagin #include <sys/sysent.h>
54961a7b24SJohn Baldwin #include <sys/turnstile.h>
55d116b9f1SMateusz Guzik #include <sys/taskqueue.h>
5644990b8cSJulian Elischer #include <sys/ktr.h>
57cf7d9a8cSDavid Xu #include <sys/rwlock.h>
58bc8e6d81SDavid Xu #include <sys/umtx.h>
599ed01c32SGleb Smirnoff #include <sys/vmmeter.h>
60d7f687fcSJeff Roberson #include <sys/cpuset.h>
6116d95d4fSJoseph Koshy #ifdef	HWPMC_HOOKS
6216d95d4fSJoseph Koshy #include <sys/pmckern.h>
6316d95d4fSJoseph Koshy #endif
641bd3cf5dSMateusz Guzik #include <sys/priv.h>
6544990b8cSJulian Elischer 
66911b84b0SRobert Watson #include <security/audit/audit.h>
67911b84b0SRobert Watson 
68d116b9f1SMateusz Guzik #include <vm/pmap.h>
6944990b8cSJulian Elischer #include <vm/vm.h>
7049a2507bSAlan Cox #include <vm/vm_extern.h>
7144990b8cSJulian Elischer #include <vm/uma.h>
72d116b9f1SMateusz Guzik #include <vm/vm_phys.h>
73b209f889SRandall Stewart #include <sys/eventhandler.h>
7402fb42b0SPeter Wemm 
75acd9f517SKonstantin Belousov /*
76acd9f517SKonstantin Belousov  * Asserts below verify the stability of struct thread and struct proc
77acd9f517SKonstantin Belousov  * layout, as exposed by KBI to modules.  On head, the KBI is allowed
78acd9f517SKonstantin Belousov  * to drift, change to the structures must be accompanied by the
79acd9f517SKonstantin Belousov  * assert update.
80acd9f517SKonstantin Belousov  *
81acd9f517SKonstantin Belousov  * On the stable branches after KBI freeze, conditions must not be
82acd9f517SKonstantin Belousov  * violated.  Typically new fields are moved to the end of the
83acd9f517SKonstantin Belousov  * structures.
84acd9f517SKonstantin Belousov  */
85acd9f517SKonstantin Belousov #ifdef __amd64__
863f289c3fSJeff Roberson _Static_assert(offsetof(struct thread, td_flags) == 0xfc,
87acd9f517SKonstantin Belousov     "struct thread KBI td_flags");
883f289c3fSJeff Roberson _Static_assert(offsetof(struct thread, td_pflags) == 0x104,
89acd9f517SKonstantin Belousov     "struct thread KBI td_pflags");
901e2521ffSEdward Tomasz Napierala _Static_assert(offsetof(struct thread, td_frame) == 0x4a0,
91acd9f517SKonstantin Belousov     "struct thread KBI td_frame");
921724c563SMateusz Guzik _Static_assert(offsetof(struct thread, td_emuldata) == 0x6b0,
93acd9f517SKonstantin Belousov     "struct thread KBI td_emuldata");
9485078b85SConrad Meyer _Static_assert(offsetof(struct proc, p_flag) == 0xb8,
95acd9f517SKonstantin Belousov     "struct proc KBI p_flag");
9685078b85SConrad Meyer _Static_assert(offsetof(struct proc, p_pid) == 0xc4,
97acd9f517SKonstantin Belousov     "struct proc KBI p_pid");
9885078b85SConrad Meyer _Static_assert(offsetof(struct proc, p_filemon) == 0x3c0,
99acd9f517SKonstantin Belousov     "struct proc KBI p_filemon");
10085078b85SConrad Meyer _Static_assert(offsetof(struct proc, p_comm) == 0x3d8,
101acd9f517SKonstantin Belousov     "struct proc KBI p_comm");
10285078b85SConrad Meyer _Static_assert(offsetof(struct proc, p_emuldata) == 0x4b8,
103acd9f517SKonstantin Belousov     "struct proc KBI p_emuldata");
104acd9f517SKonstantin Belousov #endif
105acd9f517SKonstantin Belousov #ifdef __i386__
1063f289c3fSJeff Roberson _Static_assert(offsetof(struct thread, td_flags) == 0x98,
107acd9f517SKonstantin Belousov     "struct thread KBI td_flags");
1083f289c3fSJeff Roberson _Static_assert(offsetof(struct thread, td_pflags) == 0xa0,
109acd9f517SKonstantin Belousov     "struct thread KBI td_pflags");
1101e2521ffSEdward Tomasz Napierala _Static_assert(offsetof(struct thread, td_frame) == 0x300,
111acd9f517SKonstantin Belousov     "struct thread KBI td_frame");
1121e2521ffSEdward Tomasz Napierala _Static_assert(offsetof(struct thread, td_emuldata) == 0x344,
113acd9f517SKonstantin Belousov     "struct thread KBI td_emuldata");
11485078b85SConrad Meyer _Static_assert(offsetof(struct proc, p_flag) == 0x6c,
115acd9f517SKonstantin Belousov     "struct proc KBI p_flag");
11685078b85SConrad Meyer _Static_assert(offsetof(struct proc, p_pid) == 0x78,
117acd9f517SKonstantin Belousov     "struct proc KBI p_pid");
11885078b85SConrad Meyer _Static_assert(offsetof(struct proc, p_filemon) == 0x26c,
119acd9f517SKonstantin Belousov     "struct proc KBI p_filemon");
12085078b85SConrad Meyer _Static_assert(offsetof(struct proc, p_comm) == 0x280,
121acd9f517SKonstantin Belousov     "struct proc KBI p_comm");
12285078b85SConrad Meyer _Static_assert(offsetof(struct proc, p_emuldata) == 0x30c,
123acd9f517SKonstantin Belousov     "struct proc KBI p_emuldata");
124acd9f517SKonstantin Belousov #endif
125acd9f517SKonstantin Belousov 
126b3e9e682SRyan Stone SDT_PROVIDER_DECLARE(proc);
127d9fae5abSAndriy Gapon SDT_PROBE_DEFINE(proc, , , lwp__exit);
128b3e9e682SRyan Stone 
1298460a577SJohn Birrell /*
1308460a577SJohn Birrell  * thread related storage.
1318460a577SJohn Birrell  */
13244990b8cSJulian Elischer static uma_zone_t thread_zone;
13344990b8cSJulian Elischer 
134d116b9f1SMateusz Guzik struct thread_domain_data {
135d116b9f1SMateusz Guzik 	struct thread	*tdd_zombies;
136d116b9f1SMateusz Guzik 	int		tdd_reapticks;
137d116b9f1SMateusz Guzik } __aligned(CACHE_LINE_SIZE);
138d116b9f1SMateusz Guzik 
139d116b9f1SMateusz Guzik static struct thread_domain_data thread_domain_data[MAXMEMDOM];
140d116b9f1SMateusz Guzik 
141d116b9f1SMateusz Guzik static struct task	thread_reap_task;
142d116b9f1SMateusz Guzik static struct callout  	thread_reap_callout;
14344990b8cSJulian Elischer 
144ff8fbcffSJeff Roberson static void thread_zombie(struct thread *);
145d116b9f1SMateusz Guzik static void thread_reap_all(void);
146d116b9f1SMateusz Guzik static void thread_reap_task_cb(void *, int);
147d116b9f1SMateusz Guzik static void thread_reap_callout_cb(void *);
14884cdea97SKonstantin Belousov static int thread_unsuspend_one(struct thread *td, struct proc *p,
14984cdea97SKonstantin Belousov     bool boundary);
150755341dfSMateusz Guzik static void thread_free_batched(struct thread *td);
151ff8fbcffSJeff Roberson 
152d1ca25beSMateusz Guzik static __exclusive_cache_line struct mtx tid_lock;
153934e7e5eSMateusz Guzik static bitstr_t *tid_bitmap;
15435bb59edSMateusz Guzik 
155cf7d9a8cSDavid Xu static MALLOC_DEFINE(M_TIDHASH, "tidhash", "thread hash");
156cf7d9a8cSDavid Xu 
1571bd3cf5dSMateusz Guzik static int maxthread;
1581bd3cf5dSMateusz Guzik SYSCTL_INT(_kern, OID_AUTO, maxthread, CTLFLAG_RDTUN,
1591bd3cf5dSMateusz Guzik     &maxthread, 0, "Maximum number of threads");
1601bd3cf5dSMateusz Guzik 
16162dbc992SMateusz Guzik static __exclusive_cache_line int nthreads;
1621bd3cf5dSMateusz Guzik 
163aae3547bSMateusz Guzik static LIST_HEAD(tidhashhead, thread) *tidhashtbl;
164aae3547bSMateusz Guzik static u_long	tidhash;
16526007fe3SMateusz Guzik static u_long	tidhashlock;
16626007fe3SMateusz Guzik static struct	rwlock *tidhashtbl_lock;
167aae3547bSMateusz Guzik #define	TIDHASH(tid)		(&tidhashtbl[(tid) & tidhash])
16826007fe3SMateusz Guzik #define	TIDHASHLOCK(tid)	(&tidhashtbl_lock[(tid) & tidhashlock])
169cf7d9a8cSDavid Xu 
1702ca45184SMatt Joras EVENTHANDLER_LIST_DEFINE(thread_ctor);
1712ca45184SMatt Joras EVENTHANDLER_LIST_DEFINE(thread_dtor);
1722ca45184SMatt Joras EVENTHANDLER_LIST_DEFINE(thread_init);
1732ca45184SMatt Joras EVENTHANDLER_LIST_DEFINE(thread_fini);
1742ca45184SMatt Joras 
17562dbc992SMateusz Guzik static bool
176d116b9f1SMateusz Guzik thread_count_inc_try(void)
177ec6ea5e8SDavid Xu {
17862dbc992SMateusz Guzik 	int nthreads_new;
179ec6ea5e8SDavid Xu 
18062dbc992SMateusz Guzik 	nthreads_new = atomic_fetchadd_int(&nthreads, 1) + 1;
18162dbc992SMateusz Guzik 	if (nthreads_new >= maxthread - 100) {
1821bd3cf5dSMateusz Guzik 		if (priv_check_cred(curthread->td_ucred, PRIV_MAXPROC) != 0 ||
18362dbc992SMateusz Guzik 		    nthreads_new >= maxthread) {
18462dbc992SMateusz Guzik 			atomic_subtract_int(&nthreads, 1);
185d116b9f1SMateusz Guzik 			return (false);
186d116b9f1SMateusz Guzik 		}
187d116b9f1SMateusz Guzik 	}
188d116b9f1SMateusz Guzik 	return (true);
189d116b9f1SMateusz Guzik }
190d116b9f1SMateusz Guzik 
191d116b9f1SMateusz Guzik static bool
192d116b9f1SMateusz Guzik thread_count_inc(void)
193d116b9f1SMateusz Guzik {
194d116b9f1SMateusz Guzik 	static struct timeval lastfail;
195d116b9f1SMateusz Guzik 	static int curfail;
196d116b9f1SMateusz Guzik 
197d116b9f1SMateusz Guzik 	thread_reap();
198d116b9f1SMateusz Guzik 	if (thread_count_inc_try()) {
199d116b9f1SMateusz Guzik 		return (true);
200d116b9f1SMateusz Guzik 	}
201d116b9f1SMateusz Guzik 
202d116b9f1SMateusz Guzik 	thread_reap_all();
203d116b9f1SMateusz Guzik 	if (thread_count_inc_try()) {
204d116b9f1SMateusz Guzik 		return (true);
205d116b9f1SMateusz Guzik 	}
206d116b9f1SMateusz Guzik 
2071bd3cf5dSMateusz Guzik 	if (ppsratecheck(&lastfail, &curfail, 1)) {
2081bd3cf5dSMateusz Guzik 		printf("maxthread limit exceeded by uid %u "
2091bd3cf5dSMateusz Guzik 		    "(pid %d); consider increasing kern.maxthread\n",
2101bd3cf5dSMateusz Guzik 		    curthread->td_ucred->cr_ruid, curproc->p_pid);
2111bd3cf5dSMateusz Guzik 	}
21262dbc992SMateusz Guzik 	return (false);
2131bd3cf5dSMateusz Guzik }
2141bd3cf5dSMateusz Guzik 
21562dbc992SMateusz Guzik static void
21662dbc992SMateusz Guzik thread_count_sub(int n)
21762dbc992SMateusz Guzik {
21862dbc992SMateusz Guzik 
21962dbc992SMateusz Guzik 	atomic_subtract_int(&nthreads, n);
22062dbc992SMateusz Guzik }
22162dbc992SMateusz Guzik 
22262dbc992SMateusz Guzik static void
22362dbc992SMateusz Guzik thread_count_dec(void)
22462dbc992SMateusz Guzik {
22562dbc992SMateusz Guzik 
22662dbc992SMateusz Guzik 	thread_count_sub(1);
22762dbc992SMateusz Guzik }
22862dbc992SMateusz Guzik 
22962dbc992SMateusz Guzik static lwpid_t
23062dbc992SMateusz Guzik tid_alloc(void)
23162dbc992SMateusz Guzik {
23262dbc992SMateusz Guzik 	static lwpid_t trytid;
23362dbc992SMateusz Guzik 	lwpid_t tid;
23462dbc992SMateusz Guzik 
23562dbc992SMateusz Guzik 	mtx_lock(&tid_lock);
23635bb59edSMateusz Guzik 	/*
23735bb59edSMateusz Guzik 	 * It is an invariant that the bitmap is big enough to hold maxthread
23835bb59edSMateusz Guzik 	 * IDs. If we got to this point there has to be at least one free.
23935bb59edSMateusz Guzik 	 */
24035bb59edSMateusz Guzik 	if (trytid >= maxthread)
24135bb59edSMateusz Guzik 		trytid = 0;
24235bb59edSMateusz Guzik 	bit_ffc_at(tid_bitmap, trytid, maxthread, &tid);
24335bb59edSMateusz Guzik 	if (tid == -1) {
24435bb59edSMateusz Guzik 		KASSERT(trytid != 0, ("unexpectedly ran out of IDs"));
24535bb59edSMateusz Guzik 		trytid = 0;
24635bb59edSMateusz Guzik 		bit_ffc_at(tid_bitmap, trytid, maxthread, &tid);
24735bb59edSMateusz Guzik 		KASSERT(tid != -1, ("unexpectedly ran out of IDs"));
248ec6ea5e8SDavid Xu 	}
24935bb59edSMateusz Guzik 	bit_set(tid_bitmap, tid);
250934e7e5eSMateusz Guzik 	trytid = tid + 1;
251ec6ea5e8SDavid Xu 	mtx_unlock(&tid_lock);
25235bb59edSMateusz Guzik 	return (tid + NO_PID);
253ec6ea5e8SDavid Xu }
254ec6ea5e8SDavid Xu 
255ec6ea5e8SDavid Xu static void
256755341dfSMateusz Guzik tid_free_locked(lwpid_t rtid)
257ec6ea5e8SDavid Xu {
25835bb59edSMateusz Guzik 	lwpid_t tid;
259ec6ea5e8SDavid Xu 
260755341dfSMateusz Guzik 	mtx_assert(&tid_lock, MA_OWNED);
26135bb59edSMateusz Guzik 	KASSERT(rtid >= NO_PID,
26235bb59edSMateusz Guzik 	    ("%s: invalid tid %d\n", __func__, rtid));
26335bb59edSMateusz Guzik 	tid = rtid - NO_PID;
26435bb59edSMateusz Guzik 	KASSERT(bit_test(tid_bitmap, tid) != 0,
26535bb59edSMateusz Guzik 	    ("thread ID %d not allocated\n", rtid));
26635bb59edSMateusz Guzik 	bit_clear(tid_bitmap, tid);
267755341dfSMateusz Guzik }
268755341dfSMateusz Guzik 
269755341dfSMateusz Guzik static void
270755341dfSMateusz Guzik tid_free(lwpid_t rtid)
271755341dfSMateusz Guzik {
272755341dfSMateusz Guzik 
273755341dfSMateusz Guzik 	mtx_lock(&tid_lock);
274755341dfSMateusz Guzik 	tid_free_locked(rtid);
275755341dfSMateusz Guzik 	mtx_unlock(&tid_lock);
276755341dfSMateusz Guzik }
277755341dfSMateusz Guzik 
278755341dfSMateusz Guzik static void
279755341dfSMateusz Guzik tid_free_batch(lwpid_t *batch, int n)
280755341dfSMateusz Guzik {
281755341dfSMateusz Guzik 	int i;
282755341dfSMateusz Guzik 
283755341dfSMateusz Guzik 	mtx_lock(&tid_lock);
284755341dfSMateusz Guzik 	for (i = 0; i < n; i++) {
285755341dfSMateusz Guzik 		tid_free_locked(batch[i]);
286755341dfSMateusz Guzik 	}
287ec6ea5e8SDavid Xu 	mtx_unlock(&tid_lock);
288ec6ea5e8SDavid Xu }
289ec6ea5e8SDavid Xu 
290fdcac928SMarcel Moolenaar /*
2915ef7b7a0SMateusz Guzik  * Batching for thread reapping.
2925ef7b7a0SMateusz Guzik  */
2935ef7b7a0SMateusz Guzik struct tidbatch {
2945ef7b7a0SMateusz Guzik 	lwpid_t tab[16];
2955ef7b7a0SMateusz Guzik 	int n;
2965ef7b7a0SMateusz Guzik };
2975ef7b7a0SMateusz Guzik 
2985ef7b7a0SMateusz Guzik static void
2995ef7b7a0SMateusz Guzik tidbatch_prep(struct tidbatch *tb)
3005ef7b7a0SMateusz Guzik {
3015ef7b7a0SMateusz Guzik 
3025ef7b7a0SMateusz Guzik 	tb->n = 0;
3035ef7b7a0SMateusz Guzik }
3045ef7b7a0SMateusz Guzik 
3055ef7b7a0SMateusz Guzik static void
3065ef7b7a0SMateusz Guzik tidbatch_add(struct tidbatch *tb, struct thread *td)
3075ef7b7a0SMateusz Guzik {
3085ef7b7a0SMateusz Guzik 
3095ef7b7a0SMateusz Guzik 	KASSERT(tb->n < nitems(tb->tab),
3105ef7b7a0SMateusz Guzik 	    ("%s: count too high %d", __func__, tb->n));
3115ef7b7a0SMateusz Guzik 	tb->tab[tb->n] = td->td_tid;
3125ef7b7a0SMateusz Guzik 	tb->n++;
3135ef7b7a0SMateusz Guzik }
3145ef7b7a0SMateusz Guzik 
3155ef7b7a0SMateusz Guzik static void
3165ef7b7a0SMateusz Guzik tidbatch_process(struct tidbatch *tb)
3175ef7b7a0SMateusz Guzik {
3185ef7b7a0SMateusz Guzik 
3195ef7b7a0SMateusz Guzik 	KASSERT(tb->n <= nitems(tb->tab),
3205ef7b7a0SMateusz Guzik 	    ("%s: count too high %d", __func__, tb->n));
3215ef7b7a0SMateusz Guzik 	if (tb->n == nitems(tb->tab)) {
3225ef7b7a0SMateusz Guzik 		tid_free_batch(tb->tab, tb->n);
3235ef7b7a0SMateusz Guzik 		tb->n = 0;
3245ef7b7a0SMateusz Guzik 	}
3255ef7b7a0SMateusz Guzik }
3265ef7b7a0SMateusz Guzik 
3275ef7b7a0SMateusz Guzik static void
3285ef7b7a0SMateusz Guzik tidbatch_final(struct tidbatch *tb)
3295ef7b7a0SMateusz Guzik {
3305ef7b7a0SMateusz Guzik 
3315ef7b7a0SMateusz Guzik 	KASSERT(tb->n <= nitems(tb->tab),
3325ef7b7a0SMateusz Guzik 	    ("%s: count too high %d", __func__, tb->n));
3335ef7b7a0SMateusz Guzik 	if (tb->n != 0) {
3345ef7b7a0SMateusz Guzik 		tid_free_batch(tb->tab, tb->n);
3355ef7b7a0SMateusz Guzik 	}
3365ef7b7a0SMateusz Guzik }
3375ef7b7a0SMateusz Guzik 
3385ef7b7a0SMateusz Guzik /*
339696058c3SJulian Elischer  * Prepare a thread for use.
34044990b8cSJulian Elischer  */
341b23f72e9SBrian Feldman static int
342b23f72e9SBrian Feldman thread_ctor(void *mem, int size, void *arg, int flags)
34344990b8cSJulian Elischer {
34444990b8cSJulian Elischer 	struct thread	*td;
34544990b8cSJulian Elischer 
34644990b8cSJulian Elischer 	td = (struct thread *)mem;
34771fad9fdSJulian Elischer 	td->td_state = TDS_INACTIVE;
34894dd54b9SKonstantin Belousov 	td->td_lastcpu = td->td_oncpu = NOCPU;
349*a9568cd2SMateusz Guzik 	td->td_allocdomain = vm_phys_domain(vtophys(td));
3506c27c603SJuli Mallett 
3516c27c603SJuli Mallett 	/*
3526c27c603SJuli Mallett 	 * Note that td_critnest begins life as 1 because the thread is not
3536c27c603SJuli Mallett 	 * running and is thereby implicitly waiting to be on the receiving
354a54e85fdSJeff Roberson 	 * end of a context switch.
3556c27c603SJuli Mallett 	 */
356139b7550SJohn Baldwin 	td->td_critnest = 1;
357acbe332aSDavid Xu 	td->td_lend_user_pri = PRI_MAX;
358911b84b0SRobert Watson #ifdef AUDIT
359911b84b0SRobert Watson 	audit_thread_alloc(td);
360911b84b0SRobert Watson #endif
361d10183d9SDavid Xu 	umtx_thread_alloc(td);
36219d3e47dSMateusz Guzik 	MPASS(td->td_sel == NULL);
363b23f72e9SBrian Feldman 	return (0);
36444990b8cSJulian Elischer }
36544990b8cSJulian Elischer 
36644990b8cSJulian Elischer /*
36744990b8cSJulian Elischer  * Reclaim a thread after use.
36844990b8cSJulian Elischer  */
36944990b8cSJulian Elischer static void
37044990b8cSJulian Elischer thread_dtor(void *mem, int size, void *arg)
37144990b8cSJulian Elischer {
37244990b8cSJulian Elischer 	struct thread *td;
37344990b8cSJulian Elischer 
37444990b8cSJulian Elischer 	td = (struct thread *)mem;
37544990b8cSJulian Elischer 
37644990b8cSJulian Elischer #ifdef INVARIANTS
37744990b8cSJulian Elischer 	/* Verify that this thread is in a safe state to free. */
37844990b8cSJulian Elischer 	switch (td->td_state) {
37971fad9fdSJulian Elischer 	case TDS_INHIBITED:
38071fad9fdSJulian Elischer 	case TDS_RUNNING:
38171fad9fdSJulian Elischer 	case TDS_CAN_RUN:
38244990b8cSJulian Elischer 	case TDS_RUNQ:
38344990b8cSJulian Elischer 		/*
38444990b8cSJulian Elischer 		 * We must never unlink a thread that is in one of
38544990b8cSJulian Elischer 		 * these states, because it is currently active.
38644990b8cSJulian Elischer 		 */
38744990b8cSJulian Elischer 		panic("bad state for thread unlinking");
38844990b8cSJulian Elischer 		/* NOTREACHED */
38971fad9fdSJulian Elischer 	case TDS_INACTIVE:
39044990b8cSJulian Elischer 		break;
39144990b8cSJulian Elischer 	default:
39244990b8cSJulian Elischer 		panic("bad thread state");
39344990b8cSJulian Elischer 		/* NOTREACHED */
39444990b8cSJulian Elischer 	}
39544990b8cSJulian Elischer #endif
3966e8525ceSRobert Watson #ifdef AUDIT
3976e8525ceSRobert Watson 	audit_thread_free(td);
3986e8525ceSRobert Watson #endif
3991ba4a712SPawel Jakub Dawidek 	/* Free all OSD associated to this thread. */
4001ba4a712SPawel Jakub Dawidek 	osd_thread_exit(td);
401aca4bb91SKonstantin Belousov 	td_softdep_cleanup(td);
402aca4bb91SKonstantin Belousov 	MPASS(td->td_su == NULL);
40319d3e47dSMateusz Guzik 	seltdfini(td);
40444990b8cSJulian Elischer }
40544990b8cSJulian Elischer 
40644990b8cSJulian Elischer /*
40744990b8cSJulian Elischer  * Initialize type-stable parts of a thread (when newly created).
40844990b8cSJulian Elischer  */
409b23f72e9SBrian Feldman static int
410b23f72e9SBrian Feldman thread_init(void *mem, int size, int flags)
41144990b8cSJulian Elischer {
41244990b8cSJulian Elischer 	struct thread *td;
41344990b8cSJulian Elischer 
41444990b8cSJulian Elischer 	td = (struct thread *)mem;
415247aba24SMarcel Moolenaar 
41644f3b092SJohn Baldwin 	td->td_sleepqueue = sleepq_alloc();
417961a7b24SJohn Baldwin 	td->td_turnstile = turnstile_alloc();
4188f0e9130SKonstantin Belousov 	td->td_rlqe = NULL;
4192ca45184SMatt Joras 	EVENTHANDLER_DIRECT_INVOKE(thread_init, td);
420d10183d9SDavid Xu 	umtx_thread_init(td);
42189b57fcfSKonstantin Belousov 	td->td_kstack = 0;
422ad8b1d85SKonstantin Belousov 	td->td_sel = NULL;
423b23f72e9SBrian Feldman 	return (0);
42444990b8cSJulian Elischer }
42544990b8cSJulian Elischer 
42644990b8cSJulian Elischer /*
42744990b8cSJulian Elischer  * Tear down type-stable parts of a thread (just before being discarded).
42844990b8cSJulian Elischer  */
42944990b8cSJulian Elischer static void
43044990b8cSJulian Elischer thread_fini(void *mem, int size)
43144990b8cSJulian Elischer {
43244990b8cSJulian Elischer 	struct thread *td;
43344990b8cSJulian Elischer 
43444990b8cSJulian Elischer 	td = (struct thread *)mem;
4352ca45184SMatt Joras 	EVENTHANDLER_DIRECT_INVOKE(thread_fini, td);
4368f0e9130SKonstantin Belousov 	rlqentry_free(td->td_rlqe);
437961a7b24SJohn Baldwin 	turnstile_free(td->td_turnstile);
43844f3b092SJohn Baldwin 	sleepq_free(td->td_sleepqueue);
439d10183d9SDavid Xu 	umtx_thread_fini(td);
44019d3e47dSMateusz Guzik 	MPASS(td->td_sel == NULL);
44144990b8cSJulian Elischer }
4425215b187SJeff Roberson 
4435c8329edSJulian Elischer /*
4445215b187SJeff Roberson  * For a newly created process,
4455215b187SJeff Roberson  * link up all the structures and its initial threads etc.
446ed062c8dSJulian Elischer  * called from:
447e7d939bdSMarcel Moolenaar  * {arch}/{arch}/machdep.c   {arch}_init(), init386() etc.
448ed062c8dSJulian Elischer  * proc_dtor() (should go away)
449ed062c8dSJulian Elischer  * proc_init()
4505c8329edSJulian Elischer  */
4515c8329edSJulian Elischer void
45289b57fcfSKonstantin Belousov proc_linkup0(struct proc *p, struct thread *td)
45389b57fcfSKonstantin Belousov {
45489b57fcfSKonstantin Belousov 	TAILQ_INIT(&p->p_threads);	     /* all threads in proc */
45589b57fcfSKonstantin Belousov 	proc_linkup(p, td);
45689b57fcfSKonstantin Belousov }
45789b57fcfSKonstantin Belousov 
45889b57fcfSKonstantin Belousov void
4598460a577SJohn Birrell proc_linkup(struct proc *p, struct thread *td)
4605c8329edSJulian Elischer {
461a54e85fdSJeff Roberson 
4629104847fSDavid Xu 	sigqueue_init(&p->p_sigqueue, p);
463ebceaf6dSDavid Xu 	p->p_ksi = ksiginfo_alloc(1);
464ebceaf6dSDavid Xu 	if (p->p_ksi != NULL) {
4655c474517SDavid Xu 		/* XXX p_ksi may be null if ksiginfo zone is not ready */
466ebceaf6dSDavid Xu 		p->p_ksi->ksi_flags = KSI_EXT | KSI_INS;
467ebceaf6dSDavid Xu 	}
468b2f92ef9SDavid Xu 	LIST_INIT(&p->p_mqnotifier);
4695c8329edSJulian Elischer 	p->p_numthreads = 0;
4708460a577SJohn Birrell 	thread_link(td, p);
4715c8329edSJulian Elischer }
4725c8329edSJulian Elischer 
4731bd3cf5dSMateusz Guzik extern int max_threads_per_proc;
4741bd3cf5dSMateusz Guzik 
4755c8329edSJulian Elischer /*
47644990b8cSJulian Elischer  * Initialize global thread allocation resources.
47744990b8cSJulian Elischer  */
47844990b8cSJulian Elischer void
47944990b8cSJulian Elischer threadinit(void)
48044990b8cSJulian Elischer {
48126007fe3SMateusz Guzik 	u_long i;
482cf31cadeSMateusz Guzik 	lwpid_t tid0;
4835aa5420fSMark Johnston 	uint32_t flags;
48444990b8cSJulian Elischer 
4851bd3cf5dSMateusz Guzik 	/*
4861bd3cf5dSMateusz Guzik 	 * Place an upper limit on threads which can be allocated.
4871bd3cf5dSMateusz Guzik 	 *
4881bd3cf5dSMateusz Guzik 	 * Note that other factors may make the de facto limit much lower.
4891bd3cf5dSMateusz Guzik 	 *
4901bd3cf5dSMateusz Guzik 	 * Platform limits are somewhat arbitrary but deemed "more than good
4911bd3cf5dSMateusz Guzik 	 * enough" for the foreseable future.
4921bd3cf5dSMateusz Guzik 	 */
4931bd3cf5dSMateusz Guzik 	if (maxthread == 0) {
4941bd3cf5dSMateusz Guzik #ifdef _LP64
4951bd3cf5dSMateusz Guzik 		maxthread = MIN(maxproc * max_threads_per_proc, 1000000);
4961bd3cf5dSMateusz Guzik #else
4971bd3cf5dSMateusz Guzik 		maxthread = MIN(maxproc * max_threads_per_proc, 100000);
4981bd3cf5dSMateusz Guzik #endif
4991bd3cf5dSMateusz Guzik 	}
5001bd3cf5dSMateusz Guzik 
5011ea7a6f8SPoul-Henning Kamp 	mtx_init(&tid_lock, "TID lock", NULL, MTX_DEF);
50235bb59edSMateusz Guzik 	tid_bitmap = bit_alloc(maxthread, M_TIDHASH, M_WAITOK);
50362dbc992SMateusz Guzik 	/*
50462dbc992SMateusz Guzik 	 * Handle thread0.
50562dbc992SMateusz Guzik 	 */
50662dbc992SMateusz Guzik 	thread_count_inc();
507cf31cadeSMateusz Guzik 	tid0 = tid_alloc();
508cf31cadeSMateusz Guzik 	if (tid0 != THREAD0_TID)
509cf31cadeSMateusz Guzik 		panic("tid0 %d != %d\n", tid0, THREAD0_TID);
5101ea7a6f8SPoul-Henning Kamp 
5115aa5420fSMark Johnston 	flags = UMA_ZONE_NOFREE;
5125aa5420fSMark Johnston #ifdef __aarch64__
5135aa5420fSMark Johnston 	/*
5145aa5420fSMark Johnston 	 * Force thread structures to be allocated from the direct map.
5155aa5420fSMark Johnston 	 * Otherwise, superpage promotions and demotions may temporarily
5165aa5420fSMark Johnston 	 * invalidate thread structure mappings.  For most dynamically allocated
5175aa5420fSMark Johnston 	 * structures this is not a problem, but translation faults cannot be
5185aa5420fSMark Johnston 	 * handled without accessing curthread.
5195aa5420fSMark Johnston 	 */
5205aa5420fSMark Johnston 	flags |= UMA_ZONE_CONTIG;
5215aa5420fSMark Johnston #endif
522de028f5aSJeff Roberson 	thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(),
52344990b8cSJulian Elischer 	    thread_ctor, thread_dtor, thread_init, thread_fini,
5245aa5420fSMark Johnston 	    32 - 1, flags);
525cf7d9a8cSDavid Xu 	tidhashtbl = hashinit(maxproc / 2, M_TIDHASH, &tidhash);
52626007fe3SMateusz Guzik 	tidhashlock = (tidhash + 1) / 64;
52726007fe3SMateusz Guzik 	if (tidhashlock > 0)
52826007fe3SMateusz Guzik 		tidhashlock--;
52926007fe3SMateusz Guzik 	tidhashtbl_lock = malloc(sizeof(*tidhashtbl_lock) * (tidhashlock + 1),
53026007fe3SMateusz Guzik 	    M_TIDHASH, M_WAITOK | M_ZERO);
53126007fe3SMateusz Guzik 	for (i = 0; i < tidhashlock + 1; i++)
53226007fe3SMateusz Guzik 		rw_init(&tidhashtbl_lock[i], "tidhash");
533d116b9f1SMateusz Guzik 
534d116b9f1SMateusz Guzik 	TASK_INIT(&thread_reap_task, 0, thread_reap_task_cb, NULL);
535d116b9f1SMateusz Guzik 	callout_init(&thread_reap_callout, 1);
536d116b9f1SMateusz Guzik 	callout_reset(&thread_reap_callout, 5 * hz, thread_reap_callout_cb, NULL);
53744990b8cSJulian Elischer }
53844990b8cSJulian Elischer 
53944990b8cSJulian Elischer /*
540ff8fbcffSJeff Roberson  * Place an unused thread on the zombie list.
54144990b8cSJulian Elischer  */
54244990b8cSJulian Elischer void
543ff8fbcffSJeff Roberson thread_zombie(struct thread *td)
54444990b8cSJulian Elischer {
545d116b9f1SMateusz Guzik 	struct thread_domain_data *tdd;
546c5315f51SMateusz Guzik 	struct thread *ztd;
547c5315f51SMateusz Guzik 
548*a9568cd2SMateusz Guzik 	tdd = &thread_domain_data[td->td_allocdomain];
549d116b9f1SMateusz Guzik 	ztd = atomic_load_ptr(&tdd->tdd_zombies);
550c5315f51SMateusz Guzik 	for (;;) {
551c5315f51SMateusz Guzik 		td->td_zombie = ztd;
552d116b9f1SMateusz Guzik 		if (atomic_fcmpset_rel_ptr((uintptr_t *)&tdd->tdd_zombies,
553c5315f51SMateusz Guzik 		    (uintptr_t *)&ztd, (uintptr_t)td))
554c5315f51SMateusz Guzik 			break;
555c5315f51SMateusz Guzik 		continue;
556c5315f51SMateusz Guzik 	}
55744990b8cSJulian Elischer }
55844990b8cSJulian Elischer 
5595c8329edSJulian Elischer /*
560ff8fbcffSJeff Roberson  * Release a thread that has exited after cpu_throw().
561ff8fbcffSJeff Roberson  */
562ff8fbcffSJeff Roberson void
563ff8fbcffSJeff Roberson thread_stash(struct thread *td)
564ff8fbcffSJeff Roberson {
565ff8fbcffSJeff Roberson 	atomic_subtract_rel_int(&td->td_proc->p_exitthreads, 1);
566ff8fbcffSJeff Roberson 	thread_zombie(td);
567ff8fbcffSJeff Roberson }
568ff8fbcffSJeff Roberson 
569ff8fbcffSJeff Roberson /*
570d116b9f1SMateusz Guzik  * Reap zombies from passed domain.
57144990b8cSJulian Elischer  */
572d116b9f1SMateusz Guzik static void
573d116b9f1SMateusz Guzik thread_reap_domain(struct thread_domain_data *tdd)
57444990b8cSJulian Elischer {
575c5315f51SMateusz Guzik 	struct thread *itd, *ntd;
5765ef7b7a0SMateusz Guzik 	struct tidbatch tidbatch;
577f34a2f56SMateusz Guzik 	struct credbatch credbatch;
5785ef7b7a0SMateusz Guzik 	int tdcount;
579fb8ab680SMateusz Guzik 	struct plimit *lim;
580fb8ab680SMateusz Guzik 	int limcount;
58144990b8cSJulian Elischer 
58244990b8cSJulian Elischer 	/*
583c5315f51SMateusz Guzik 	 * Reading upfront is pessimal if followed by concurrent atomic_swap,
584c5315f51SMateusz Guzik 	 * but most of the time the list is empty.
58544990b8cSJulian Elischer 	 */
586d116b9f1SMateusz Guzik 	if (tdd->tdd_zombies == NULL)
587c5315f51SMateusz Guzik 		return;
588c5315f51SMateusz Guzik 
589d116b9f1SMateusz Guzik 	itd = (struct thread *)atomic_swap_ptr((uintptr_t *)&tdd->tdd_zombies,
590c5315f51SMateusz Guzik 	    (uintptr_t)NULL);
5915ef7b7a0SMateusz Guzik 	if (itd == NULL)
5925ef7b7a0SMateusz Guzik 		return;
5935ef7b7a0SMateusz Guzik 
594d116b9f1SMateusz Guzik 	/*
595d116b9f1SMateusz Guzik 	 * Multiple CPUs can get here, the race is fine as ticks is only
596d116b9f1SMateusz Guzik 	 * advisory.
597d116b9f1SMateusz Guzik 	 */
598d116b9f1SMateusz Guzik 	tdd->tdd_reapticks = ticks;
599d116b9f1SMateusz Guzik 
6005ef7b7a0SMateusz Guzik 	tidbatch_prep(&tidbatch);
601f34a2f56SMateusz Guzik 	credbatch_prep(&credbatch);
6025ef7b7a0SMateusz Guzik 	tdcount = 0;
603fb8ab680SMateusz Guzik 	lim = NULL;
604fb8ab680SMateusz Guzik 	limcount = 0;
605d116b9f1SMateusz Guzik 
606c5315f51SMateusz Guzik 	while (itd != NULL) {
607c5315f51SMateusz Guzik 		ntd = itd->td_zombie;
6085ef7b7a0SMateusz Guzik 		EVENTHANDLER_DIRECT_INVOKE(thread_dtor, itd);
6095ef7b7a0SMateusz Guzik 		tidbatch_add(&tidbatch, itd);
610f34a2f56SMateusz Guzik 		credbatch_add(&credbatch, itd);
611fb8ab680SMateusz Guzik 		MPASS(itd->td_limit != NULL);
612fb8ab680SMateusz Guzik 		if (lim != itd->td_limit) {
613fb8ab680SMateusz Guzik 			if (limcount != 0) {
614fb8ab680SMateusz Guzik 				lim_freen(lim, limcount);
615fb8ab680SMateusz Guzik 				limcount = 0;
616fb8ab680SMateusz Guzik 			}
617fb8ab680SMateusz Guzik 		}
618fb8ab680SMateusz Guzik 		lim = itd->td_limit;
619fb8ab680SMateusz Guzik 		limcount++;
620755341dfSMateusz Guzik 		thread_free_batched(itd);
6215ef7b7a0SMateusz Guzik 		tidbatch_process(&tidbatch);
622f34a2f56SMateusz Guzik 		credbatch_process(&credbatch);
6235ef7b7a0SMateusz Guzik 		tdcount++;
6245ef7b7a0SMateusz Guzik 		if (tdcount == 32) {
6255ef7b7a0SMateusz Guzik 			thread_count_sub(tdcount);
6265ef7b7a0SMateusz Guzik 			tdcount = 0;
627755341dfSMateusz Guzik 		}
628c5315f51SMateusz Guzik 		itd = ntd;
62944990b8cSJulian Elischer 	}
630755341dfSMateusz Guzik 
6315ef7b7a0SMateusz Guzik 	tidbatch_final(&tidbatch);
632f34a2f56SMateusz Guzik 	credbatch_final(&credbatch);
6335ef7b7a0SMateusz Guzik 	if (tdcount != 0) {
6345ef7b7a0SMateusz Guzik 		thread_count_sub(tdcount);
635755341dfSMateusz Guzik 	}
636fb8ab680SMateusz Guzik 	MPASS(limcount != 0);
637fb8ab680SMateusz Guzik 	lim_freen(lim, limcount);
638ed062c8dSJulian Elischer }
63944990b8cSJulian Elischer 
6404f0db5e0SJulian Elischer /*
641d116b9f1SMateusz Guzik  * Reap zombies from all domains.
642d116b9f1SMateusz Guzik  */
643d116b9f1SMateusz Guzik static void
644d116b9f1SMateusz Guzik thread_reap_all(void)
645d116b9f1SMateusz Guzik {
646d116b9f1SMateusz Guzik 	struct thread_domain_data *tdd;
647d116b9f1SMateusz Guzik 	int i, domain;
648d116b9f1SMateusz Guzik 
649d116b9f1SMateusz Guzik 	domain = PCPU_GET(domain);
650d116b9f1SMateusz Guzik 	for (i = 0; i < vm_ndomains; i++) {
651d116b9f1SMateusz Guzik 		tdd = &thread_domain_data[(i + domain) % vm_ndomains];
652d116b9f1SMateusz Guzik 		thread_reap_domain(tdd);
653d116b9f1SMateusz Guzik 	}
654d116b9f1SMateusz Guzik }
655d116b9f1SMateusz Guzik 
656d116b9f1SMateusz Guzik /*
657d116b9f1SMateusz Guzik  * Reap zombies from local domain.
658d116b9f1SMateusz Guzik  */
659d116b9f1SMateusz Guzik void
660d116b9f1SMateusz Guzik thread_reap(void)
661d116b9f1SMateusz Guzik {
662d116b9f1SMateusz Guzik 	struct thread_domain_data *tdd;
663d116b9f1SMateusz Guzik 	int domain;
664d116b9f1SMateusz Guzik 
665d116b9f1SMateusz Guzik 	domain = PCPU_GET(domain);
666d116b9f1SMateusz Guzik 	tdd = &thread_domain_data[domain];
667d116b9f1SMateusz Guzik 
668d116b9f1SMateusz Guzik 	thread_reap_domain(tdd);
669d116b9f1SMateusz Guzik }
670d116b9f1SMateusz Guzik 
671d116b9f1SMateusz Guzik static void
672d116b9f1SMateusz Guzik thread_reap_task_cb(void *arg __unused, int pending __unused)
673d116b9f1SMateusz Guzik {
674d116b9f1SMateusz Guzik 
675d116b9f1SMateusz Guzik 	thread_reap_all();
676d116b9f1SMateusz Guzik }
677d116b9f1SMateusz Guzik 
678d116b9f1SMateusz Guzik static void
679d116b9f1SMateusz Guzik thread_reap_callout_cb(void *arg __unused)
680d116b9f1SMateusz Guzik {
681d116b9f1SMateusz Guzik 	struct thread_domain_data *tdd;
682d116b9f1SMateusz Guzik 	int i, cticks, lticks;
683d116b9f1SMateusz Guzik 	bool wantreap;
684d116b9f1SMateusz Guzik 
685d116b9f1SMateusz Guzik 	wantreap = false;
686d116b9f1SMateusz Guzik 	cticks = atomic_load_int(&ticks);
687d116b9f1SMateusz Guzik 	for (i = 0; i < vm_ndomains; i++) {
688d116b9f1SMateusz Guzik 		tdd = &thread_domain_data[i];
689d116b9f1SMateusz Guzik 		lticks = tdd->tdd_reapticks;
690d116b9f1SMateusz Guzik 		if (tdd->tdd_zombies != NULL &&
691d116b9f1SMateusz Guzik 		    (u_int)(cticks - lticks) > 5 * hz) {
692d116b9f1SMateusz Guzik 			wantreap = true;
693d116b9f1SMateusz Guzik 			break;
694d116b9f1SMateusz Guzik 		}
695d116b9f1SMateusz Guzik 	}
696d116b9f1SMateusz Guzik 
697d116b9f1SMateusz Guzik 	if (wantreap)
698d116b9f1SMateusz Guzik 		taskqueue_enqueue(taskqueue_thread, &thread_reap_task);
699d116b9f1SMateusz Guzik 	callout_reset(&thread_reap_callout, 5 * hz, thread_reap_callout_cb, NULL);
700d116b9f1SMateusz Guzik }
701d116b9f1SMateusz Guzik 
702d116b9f1SMateusz Guzik /*
70344990b8cSJulian Elischer  * Allocate a thread.
70444990b8cSJulian Elischer  */
70544990b8cSJulian Elischer struct thread *
7068a945d10SKonstantin Belousov thread_alloc(int pages)
70744990b8cSJulian Elischer {
70889b57fcfSKonstantin Belousov 	struct thread *td;
7091bd3cf5dSMateusz Guzik 	lwpid_t tid;
7108460a577SJohn Birrell 
71162dbc992SMateusz Guzik 	if (!thread_count_inc()) {
7121bd3cf5dSMateusz Guzik 		return (NULL);
7131bd3cf5dSMateusz Guzik 	}
7141bd3cf5dSMateusz Guzik 
71562dbc992SMateusz Guzik 	tid = tid_alloc();
7161bd3cf5dSMateusz Guzik 	td = uma_zalloc(thread_zone, M_WAITOK);
71789b57fcfSKonstantin Belousov 	KASSERT(td->td_kstack == 0, ("thread_alloc got thread with kstack"));
7188a945d10SKonstantin Belousov 	if (!vm_thread_new(td, pages)) {
71989b57fcfSKonstantin Belousov 		uma_zfree(thread_zone, td);
7201bd3cf5dSMateusz Guzik 		tid_free(tid);
72162dbc992SMateusz Guzik 		thread_count_dec();
72289b57fcfSKonstantin Belousov 		return (NULL);
72389b57fcfSKonstantin Belousov 	}
7241bd3cf5dSMateusz Guzik 	td->td_tid = tid;
7250c3967e7SMarcel Moolenaar 	cpu_thread_alloc(td);
7261bd3cf5dSMateusz Guzik 	EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td);
72789b57fcfSKonstantin Belousov 	return (td);
72844990b8cSJulian Elischer }
72944990b8cSJulian Elischer 
7308a945d10SKonstantin Belousov int
7318a945d10SKonstantin Belousov thread_alloc_stack(struct thread *td, int pages)
7328a945d10SKonstantin Belousov {
7338a945d10SKonstantin Belousov 
7348a945d10SKonstantin Belousov 	KASSERT(td->td_kstack == 0,
7358a945d10SKonstantin Belousov 	    ("thread_alloc_stack called on a thread with kstack"));
7368a945d10SKonstantin Belousov 	if (!vm_thread_new(td, pages))
7378a945d10SKonstantin Belousov 		return (0);
7388a945d10SKonstantin Belousov 	cpu_thread_alloc(td);
7398a945d10SKonstantin Belousov 	return (1);
7408a945d10SKonstantin Belousov }
7414f0db5e0SJulian Elischer 
7424f0db5e0SJulian Elischer /*
74344990b8cSJulian Elischer  * Deallocate a thread.
74444990b8cSJulian Elischer  */
745755341dfSMateusz Guzik static void
746755341dfSMateusz Guzik thread_free_batched(struct thread *td)
74744990b8cSJulian Elischer {
7482e6b8de4SJeff Roberson 
7492e6b8de4SJeff Roberson 	lock_profile_thread_exit(td);
75045aea8deSJeff Roberson 	if (td->td_cpuset)
751d7f687fcSJeff Roberson 		cpuset_rel(td->td_cpuset);
752d7f687fcSJeff Roberson 	td->td_cpuset = NULL;
7530c3967e7SMarcel Moolenaar 	cpu_thread_free(td);
75489b57fcfSKonstantin Belousov 	if (td->td_kstack != 0)
75589b57fcfSKonstantin Belousov 		vm_thread_dispose(td);
7562d19b736SKonstantin Belousov 	callout_drain(&td->td_slpcallout);
757755341dfSMateusz Guzik 	/*
758755341dfSMateusz Guzik 	 * Freeing handled by the caller.
759755341dfSMateusz Guzik 	 */
7601bd3cf5dSMateusz Guzik 	td->td_tid = -1;
76144990b8cSJulian Elischer 	uma_zfree(thread_zone, td);
76244990b8cSJulian Elischer }
76344990b8cSJulian Elischer 
7644ea6a9a2SMateusz Guzik void
765755341dfSMateusz Guzik thread_free(struct thread *td)
766755341dfSMateusz Guzik {
767755341dfSMateusz Guzik 	lwpid_t tid;
768755341dfSMateusz Guzik 
7695ef7b7a0SMateusz Guzik 	EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td);
770755341dfSMateusz Guzik 	tid = td->td_tid;
771755341dfSMateusz Guzik 	thread_free_batched(td);
772755341dfSMateusz Guzik 	tid_free(tid);
77362dbc992SMateusz Guzik 	thread_count_dec();
774755341dfSMateusz Guzik }
775755341dfSMateusz Guzik 
776755341dfSMateusz Guzik void
7774ea6a9a2SMateusz Guzik thread_cow_get_proc(struct thread *newtd, struct proc *p)
7784ea6a9a2SMateusz Guzik {
7794ea6a9a2SMateusz Guzik 
7804ea6a9a2SMateusz Guzik 	PROC_LOCK_ASSERT(p, MA_OWNED);
7811724c563SMateusz Guzik 	newtd->td_realucred = crcowget(p->p_ucred);
7821724c563SMateusz Guzik 	newtd->td_ucred = newtd->td_realucred;
783f6f6d240SMateusz Guzik 	newtd->td_limit = lim_hold(p->p_limit);
7844ea6a9a2SMateusz Guzik 	newtd->td_cowgen = p->p_cowgen;
7854ea6a9a2SMateusz Guzik }
7864ea6a9a2SMateusz Guzik 
7874ea6a9a2SMateusz Guzik void
7884ea6a9a2SMateusz Guzik thread_cow_get(struct thread *newtd, struct thread *td)
7894ea6a9a2SMateusz Guzik {
7904ea6a9a2SMateusz Guzik 
7911724c563SMateusz Guzik 	MPASS(td->td_realucred == td->td_ucred);
7921724c563SMateusz Guzik 	newtd->td_realucred = crcowget(td->td_realucred);
7931724c563SMateusz Guzik 	newtd->td_ucred = newtd->td_realucred;
794f6f6d240SMateusz Guzik 	newtd->td_limit = lim_hold(td->td_limit);
7954ea6a9a2SMateusz Guzik 	newtd->td_cowgen = td->td_cowgen;
7964ea6a9a2SMateusz Guzik }
7974ea6a9a2SMateusz Guzik 
7984ea6a9a2SMateusz Guzik void
7994ea6a9a2SMateusz Guzik thread_cow_free(struct thread *td)
8004ea6a9a2SMateusz Guzik {
8014ea6a9a2SMateusz Guzik 
8021724c563SMateusz Guzik 	if (td->td_realucred != NULL)
8031724c563SMateusz Guzik 		crcowfree(td);
804cd672ca6SMateusz Guzik 	if (td->td_limit != NULL)
805f6f6d240SMateusz Guzik 		lim_free(td->td_limit);
8064ea6a9a2SMateusz Guzik }
8074ea6a9a2SMateusz Guzik 
8084ea6a9a2SMateusz Guzik void
8094ea6a9a2SMateusz Guzik thread_cow_update(struct thread *td)
8104ea6a9a2SMateusz Guzik {
8114ea6a9a2SMateusz Guzik 	struct proc *p;
812cd672ca6SMateusz Guzik 	struct ucred *oldcred;
813cd672ca6SMateusz Guzik 	struct plimit *oldlimit;
8144ea6a9a2SMateusz Guzik 
8154ea6a9a2SMateusz Guzik 	p = td->td_proc;
816cd672ca6SMateusz Guzik 	oldlimit = NULL;
8174ea6a9a2SMateusz Guzik 	PROC_LOCK(p);
8181724c563SMateusz Guzik 	oldcred = crcowsync();
819cd672ca6SMateusz Guzik 	if (td->td_limit != p->p_limit) {
820cd672ca6SMateusz Guzik 		oldlimit = td->td_limit;
821cd672ca6SMateusz Guzik 		td->td_limit = lim_hold(p->p_limit);
822cd672ca6SMateusz Guzik 	}
8234ea6a9a2SMateusz Guzik 	td->td_cowgen = p->p_cowgen;
8244ea6a9a2SMateusz Guzik 	PROC_UNLOCK(p);
825cd672ca6SMateusz Guzik 	if (oldcred != NULL)
826cd672ca6SMateusz Guzik 		crfree(oldcred);
827cd672ca6SMateusz Guzik 	if (oldlimit != NULL)
828cd672ca6SMateusz Guzik 		lim_free(oldlimit);
8294ea6a9a2SMateusz Guzik }
8304ea6a9a2SMateusz Guzik 
83144990b8cSJulian Elischer /*
83244990b8cSJulian Elischer  * Discard the current thread and exit from its context.
83394e0a4cdSJulian Elischer  * Always called with scheduler locked.
83444990b8cSJulian Elischer  *
83544990b8cSJulian Elischer  * Because we can't free a thread while we're operating under its context,
836696058c3SJulian Elischer  * push the current thread into our CPU's deadthread holder. This means
837696058c3SJulian Elischer  * we needn't worry about someone else grabbing our context before we
8386617724cSJeff Roberson  * do a cpu_throw().
83944990b8cSJulian Elischer  */
84044990b8cSJulian Elischer void
84144990b8cSJulian Elischer thread_exit(void)
84244990b8cSJulian Elischer {
8437e3a96eaSJohn Baldwin 	uint64_t runtime, new_switchtime;
84444990b8cSJulian Elischer 	struct thread *td;
8451c4bcd05SJeff Roberson 	struct thread *td2;
84644990b8cSJulian Elischer 	struct proc *p;
8477847a9daSJohn Baldwin 	int wakeup_swapper;
84844990b8cSJulian Elischer 
84944990b8cSJulian Elischer 	td = curthread;
85044990b8cSJulian Elischer 	p = td->td_proc;
85144990b8cSJulian Elischer 
852a54e85fdSJeff Roberson 	PROC_SLOCK_ASSERT(p, MA_OWNED);
853ed062c8dSJulian Elischer 	mtx_assert(&Giant, MA_NOTOWNED);
854a54e85fdSJeff Roberson 
85544990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
856ed062c8dSJulian Elischer 	KASSERT(p != NULL, ("thread exiting without a process"));
857cc701b73SRobert Watson 	CTR3(KTR_PROC, "thread_exit: thread %p (pid %ld, %s)", td,
858e01eafefSJulian Elischer 	    (long)p->p_pid, td->td_name);
8596c9271a9SAndriy Gapon 	SDT_PROBE0(proc, , , lwp__exit);
8609104847fSDavid Xu 	KASSERT(TAILQ_EMPTY(&td->td_sigqueue.sq_list), ("signal pending"));
861936c24faSMateusz Guzik 	MPASS(td->td_realucred == td->td_ucred);
86244990b8cSJulian Elischer 
863ed062c8dSJulian Elischer 	/*
864ed062c8dSJulian Elischer 	 * drop FPU & debug register state storage, or any other
865ed062c8dSJulian Elischer 	 * architecture specific resources that
866ed062c8dSJulian Elischer 	 * would not be on a new untouched process.
867ed062c8dSJulian Elischer 	 */
868bd07998eSKonstantin Belousov 	cpu_thread_exit(td);
86944990b8cSJulian Elischer 
870ed062c8dSJulian Elischer 	/*
8711faf202eSJulian Elischer 	 * The last thread is left attached to the process
8721faf202eSJulian Elischer 	 * So that the whole bundle gets recycled. Skip
873ed062c8dSJulian Elischer 	 * all this stuff if we never had threads.
874ed062c8dSJulian Elischer 	 * EXIT clears all sign of other threads when
875ed062c8dSJulian Elischer 	 * it goes to single threading, so the last thread always
876ed062c8dSJulian Elischer 	 * takes the short path.
8771faf202eSJulian Elischer 	 */
878ed062c8dSJulian Elischer 	if (p->p_flag & P_HADTHREADS) {
8791faf202eSJulian Elischer 		if (p->p_numthreads > 1) {
880fd229b5bSKonstantin Belousov 			atomic_add_int(&td->td_proc->p_exitthreads, 1);
881d3a0bd78SJulian Elischer 			thread_unlink(td);
8821c4bcd05SJeff Roberson 			td2 = FIRST_THREAD_IN_PROC(p);
8831c4bcd05SJeff Roberson 			sched_exit_thread(td2, td);
884ed062c8dSJulian Elischer 
885ed062c8dSJulian Elischer 			/*
88644990b8cSJulian Elischer 			 * The test below is NOT true if we are the
8879182554aSKonstantin Belousov 			 * sole exiting thread. P_STOPPED_SINGLE is unset
88844990b8cSJulian Elischer 			 * in exit1() after it is the only survivor.
88944990b8cSJulian Elischer 			 */
8901279572aSDavid Xu 			if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
89144990b8cSJulian Elischer 				if (p->p_numthreads == p->p_suspcount) {
892a54e85fdSJeff Roberson 					thread_lock(p->p_singlethread);
8937847a9daSJohn Baldwin 					wakeup_swapper = thread_unsuspend_one(
89484cdea97SKonstantin Belousov 						p->p_singlethread, p, false);
8957847a9daSJohn Baldwin 					if (wakeup_swapper)
8967847a9daSJohn Baldwin 						kick_proc0();
89744990b8cSJulian Elischer 				}
89844990b8cSJulian Elischer 			}
89948bfcdddSJulian Elischer 
900696058c3SJulian Elischer 			PCPU_SET(deadthread, td);
9011faf202eSJulian Elischer 		} else {
902ed062c8dSJulian Elischer 			/*
903ed062c8dSJulian Elischer 			 * The last thread is exiting.. but not through exit()
904ed062c8dSJulian Elischer 			 */
905ed062c8dSJulian Elischer 			panic ("thread_exit: Last thread exiting on its own");
906ed062c8dSJulian Elischer 		}
9071faf202eSJulian Elischer 	}
90816d95d4fSJoseph Koshy #ifdef	HWPMC_HOOKS
90916d95d4fSJoseph Koshy 	/*
91016d95d4fSJoseph Koshy 	 * If this thread is part of a process that is being tracked by hwpmc(4),
91116d95d4fSJoseph Koshy 	 * inform the module of the thread's impending exit.
91216d95d4fSJoseph Koshy 	 */
9136161b98cSMatt Macy 	if (PMC_PROC_IS_USING_PMCS(td->td_proc)) {
91416d95d4fSJoseph Koshy 		PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT);
9156161b98cSMatt Macy 		PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_EXIT, NULL);
916ebfaf69cSMatt Macy 	} else if (PMC_SYSTEM_SAMPLING_ACTIVE())
917ebfaf69cSMatt Macy 		PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_EXIT_LOG, NULL);
91816d95d4fSJoseph Koshy #endif
919a54e85fdSJeff Roberson 	PROC_UNLOCK(p);
9205c7bebf9SKonstantin Belousov 	PROC_STATLOCK(p);
9215c7bebf9SKonstantin Belousov 	thread_lock(td);
9225c7bebf9SKonstantin Belousov 	PROC_SUNLOCK(p);
9237e3a96eaSJohn Baldwin 
9247e3a96eaSJohn Baldwin 	/* Do the same timestamp bookkeeping that mi_switch() would do. */
9257e3a96eaSJohn Baldwin 	new_switchtime = cpu_ticks();
9267e3a96eaSJohn Baldwin 	runtime = new_switchtime - PCPU_GET(switchtime);
9277e3a96eaSJohn Baldwin 	td->td_runtime += runtime;
9287e3a96eaSJohn Baldwin 	td->td_incruntime += runtime;
9297e3a96eaSJohn Baldwin 	PCPU_SET(switchtime, new_switchtime);
9307e3a96eaSJohn Baldwin 	PCPU_SET(switchticks, ticks);
93183c9dea1SGleb Smirnoff 	VM_CNT_INC(v_swtch);
9327e3a96eaSJohn Baldwin 
9337e3a96eaSJohn Baldwin 	/* Save our resource usage in our process. */
9347e3a96eaSJohn Baldwin 	td->td_ru.ru_nvcsw++;
93561a74c5cSJeff Roberson 	ruxagg_locked(p, td);
9367e3a96eaSJohn Baldwin 	rucollect(&p->p_ru, &td->td_ru);
9375c7bebf9SKonstantin Belousov 	PROC_STATUNLOCK(p);
9387e3a96eaSJohn Baldwin 
939dcc9954eSJulian Elischer 	td->td_state = TDS_INACTIVE;
9403d06b4b3SAttilio Rao #ifdef WITNESS
9413d06b4b3SAttilio Rao 	witness_thread_exit(td);
9423d06b4b3SAttilio Rao #endif
943732d9528SJulian Elischer 	CTR1(KTR_PROC, "thread_exit: cpu_throw() thread %p", td);
944a54e85fdSJeff Roberson 	sched_throw(td);
945cc66ebe2SPeter Wemm 	panic("I'm a teapot!");
94644990b8cSJulian Elischer 	/* NOTREACHED */
94744990b8cSJulian Elischer }
94844990b8cSJulian Elischer 
94944990b8cSJulian Elischer /*
950696058c3SJulian Elischer  * Do any thread specific cleanups that may be needed in wait()
95137814395SPeter Wemm  * called with Giant, proc and schedlock not held.
952696058c3SJulian Elischer  */
953696058c3SJulian Elischer void
954696058c3SJulian Elischer thread_wait(struct proc *p)
955696058c3SJulian Elischer {
956696058c3SJulian Elischer 	struct thread *td;
957696058c3SJulian Elischer 
95837814395SPeter Wemm 	mtx_assert(&Giant, MA_NOTOWNED);
959624bf9e1SKonstantin Belousov 	KASSERT(p->p_numthreads == 1, ("multiple threads in thread_wait()"));
960624bf9e1SKonstantin Belousov 	KASSERT(p->p_exitthreads == 0, ("p_exitthreads leaking"));
961ff8fbcffSJeff Roberson 	td = FIRST_THREAD_IN_PROC(p);
962ff8fbcffSJeff Roberson 	/* Lock the last thread so we spin until it exits cpu_throw(). */
963ff8fbcffSJeff Roberson 	thread_lock(td);
964ff8fbcffSJeff Roberson 	thread_unlock(td);
9652e6b8de4SJeff Roberson 	lock_profile_thread_exit(td);
966d7f687fcSJeff Roberson 	cpuset_rel(td->td_cpuset);
967d7f687fcSJeff Roberson 	td->td_cpuset = NULL;
968696058c3SJulian Elischer 	cpu_thread_clean(td);
9694ea6a9a2SMateusz Guzik 	thread_cow_free(td);
9702d19b736SKonstantin Belousov 	callout_drain(&td->td_slpcallout);
971696058c3SJulian Elischer 	thread_reap();	/* check for zombie threads etc. */
972696058c3SJulian Elischer }
973696058c3SJulian Elischer 
974696058c3SJulian Elischer /*
97544990b8cSJulian Elischer  * Link a thread to a process.
9761faf202eSJulian Elischer  * set up anything that needs to be initialized for it to
9771faf202eSJulian Elischer  * be used by the process.
97844990b8cSJulian Elischer  */
97944990b8cSJulian Elischer void
9808460a577SJohn Birrell thread_link(struct thread *td, struct proc *p)
98144990b8cSJulian Elischer {
98244990b8cSJulian Elischer 
983a54e85fdSJeff Roberson 	/*
984a54e85fdSJeff Roberson 	 * XXX This can't be enabled because it's called for proc0 before
985374ae2a3SJeff Roberson 	 * its lock has been created.
986374ae2a3SJeff Roberson 	 * PROC_LOCK_ASSERT(p, MA_OWNED);
987a54e85fdSJeff Roberson 	 */
98871fad9fdSJulian Elischer 	td->td_state    = TDS_INACTIVE;
98944990b8cSJulian Elischer 	td->td_proc     = p;
990b61ce5b0SJeff Roberson 	td->td_flags    = TDF_INMEM;
99144990b8cSJulian Elischer 
9921faf202eSJulian Elischer 	LIST_INIT(&td->td_contested);
993eea4f254SJeff Roberson 	LIST_INIT(&td->td_lprof[0]);
994eea4f254SJeff Roberson 	LIST_INIT(&td->td_lprof[1]);
995f6eccf96SGleb Smirnoff #ifdef EPOCH_TRACE
996dd902d01SGleb Smirnoff 	SLIST_INIT(&td->td_epochs);
997f6eccf96SGleb Smirnoff #endif
9989104847fSDavid Xu 	sigqueue_init(&td->td_sigqueue, p);
999fd90e2edSJung-uk Kim 	callout_init(&td->td_slpcallout, 1);
100066d8df9dSDaniel Eischen 	TAILQ_INSERT_TAIL(&p->p_threads, td, td_plist);
100144990b8cSJulian Elischer 	p->p_numthreads++;
100244990b8cSJulian Elischer }
100344990b8cSJulian Elischer 
1004ed062c8dSJulian Elischer /*
1005ed062c8dSJulian Elischer  * Called from:
1006ed062c8dSJulian Elischer  *  thread_exit()
1007ed062c8dSJulian Elischer  */
1008d3a0bd78SJulian Elischer void
1009d3a0bd78SJulian Elischer thread_unlink(struct thread *td)
1010d3a0bd78SJulian Elischer {
1011d3a0bd78SJulian Elischer 	struct proc *p = td->td_proc;
1012d3a0bd78SJulian Elischer 
1013374ae2a3SJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
1014f6eccf96SGleb Smirnoff #ifdef EPOCH_TRACE
1015dd902d01SGleb Smirnoff 	MPASS(SLIST_EMPTY(&td->td_epochs));
1016f6eccf96SGleb Smirnoff #endif
1017dd902d01SGleb Smirnoff 
1018d3a0bd78SJulian Elischer 	TAILQ_REMOVE(&p->p_threads, td, td_plist);
1019d3a0bd78SJulian Elischer 	p->p_numthreads--;
1020d3a0bd78SJulian Elischer 	/* could clear a few other things here */
10218460a577SJohn Birrell 	/* Must  NOT clear links to proc! */
10225c8329edSJulian Elischer }
10235c8329edSJulian Elischer 
102479799053SKonstantin Belousov static int
102579799053SKonstantin Belousov calc_remaining(struct proc *p, int mode)
102679799053SKonstantin Belousov {
102779799053SKonstantin Belousov 	int remaining;
102879799053SKonstantin Belousov 
10297b519077SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
10307b519077SKonstantin Belousov 	PROC_SLOCK_ASSERT(p, MA_OWNED);
103179799053SKonstantin Belousov 	if (mode == SINGLE_EXIT)
103279799053SKonstantin Belousov 		remaining = p->p_numthreads;
103379799053SKonstantin Belousov 	else if (mode == SINGLE_BOUNDARY)
103479799053SKonstantin Belousov 		remaining = p->p_numthreads - p->p_boundary_count;
10356ddcc233SKonstantin Belousov 	else if (mode == SINGLE_NO_EXIT || mode == SINGLE_ALLPROC)
103679799053SKonstantin Belousov 		remaining = p->p_numthreads - p->p_suspcount;
103779799053SKonstantin Belousov 	else
103879799053SKonstantin Belousov 		panic("calc_remaining: wrong mode %d", mode);
103979799053SKonstantin Belousov 	return (remaining);
104079799053SKonstantin Belousov }
104179799053SKonstantin Belousov 
104207a9368aSKonstantin Belousov static int
104307a9368aSKonstantin Belousov remain_for_mode(int mode)
104407a9368aSKonstantin Belousov {
104507a9368aSKonstantin Belousov 
10466ddcc233SKonstantin Belousov 	return (mode == SINGLE_ALLPROC ? 0 : 1);
104707a9368aSKonstantin Belousov }
104807a9368aSKonstantin Belousov 
104907a9368aSKonstantin Belousov static int
105007a9368aSKonstantin Belousov weed_inhib(int mode, struct thread *td2, struct proc *p)
105107a9368aSKonstantin Belousov {
105207a9368aSKonstantin Belousov 	int wakeup_swapper;
105307a9368aSKonstantin Belousov 
105407a9368aSKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
105507a9368aSKonstantin Belousov 	PROC_SLOCK_ASSERT(p, MA_OWNED);
105607a9368aSKonstantin Belousov 	THREAD_LOCK_ASSERT(td2, MA_OWNED);
105707a9368aSKonstantin Belousov 
105807a9368aSKonstantin Belousov 	wakeup_swapper = 0;
105961a74c5cSJeff Roberson 
106061a74c5cSJeff Roberson 	/*
106161a74c5cSJeff Roberson 	 * Since the thread lock is dropped by the scheduler we have
106261a74c5cSJeff Roberson 	 * to retry to check for races.
106361a74c5cSJeff Roberson 	 */
106461a74c5cSJeff Roberson restart:
106507a9368aSKonstantin Belousov 	switch (mode) {
106607a9368aSKonstantin Belousov 	case SINGLE_EXIT:
106761a74c5cSJeff Roberson 		if (TD_IS_SUSPENDED(td2)) {
106884cdea97SKonstantin Belousov 			wakeup_swapper |= thread_unsuspend_one(td2, p, true);
106961a74c5cSJeff Roberson 			thread_lock(td2);
107061a74c5cSJeff Roberson 			goto restart;
107161a74c5cSJeff Roberson 		}
107261a74c5cSJeff Roberson 		if (TD_CAN_ABORT(td2)) {
107307a9368aSKonstantin Belousov 			wakeup_swapper |= sleepq_abort(td2, EINTR);
107461a74c5cSJeff Roberson 			return (wakeup_swapper);
107561a74c5cSJeff Roberson 		}
107607a9368aSKonstantin Belousov 		break;
107707a9368aSKonstantin Belousov 	case SINGLE_BOUNDARY:
107807a9368aSKonstantin Belousov 	case SINGLE_NO_EXIT:
107961a74c5cSJeff Roberson 		if (TD_IS_SUSPENDED(td2) &&
108061a74c5cSJeff Roberson 		    (td2->td_flags & TDF_BOUNDARY) == 0) {
108184cdea97SKonstantin Belousov 			wakeup_swapper |= thread_unsuspend_one(td2, p, false);
108261a74c5cSJeff Roberson 			thread_lock(td2);
108361a74c5cSJeff Roberson 			goto restart;
108461a74c5cSJeff Roberson 		}
108561a74c5cSJeff Roberson 		if (TD_CAN_ABORT(td2)) {
108607a9368aSKonstantin Belousov 			wakeup_swapper |= sleepq_abort(td2, ERESTART);
108761a74c5cSJeff Roberson 			return (wakeup_swapper);
108861a74c5cSJeff Roberson 		}
1089917dd390SKonstantin Belousov 		break;
10906ddcc233SKonstantin Belousov 	case SINGLE_ALLPROC:
10916ddcc233SKonstantin Belousov 		/*
10926ddcc233SKonstantin Belousov 		 * ALLPROC suspend tries to avoid spurious EINTR for
10936ddcc233SKonstantin Belousov 		 * threads sleeping interruptable, by suspending the
10946ddcc233SKonstantin Belousov 		 * thread directly, similarly to sig_suspend_threads().
10956ddcc233SKonstantin Belousov 		 * Since such sleep is not performed at the user
10966ddcc233SKonstantin Belousov 		 * boundary, TDF_BOUNDARY flag is not set, and TDF_ALLPROCSUSP
10976ddcc233SKonstantin Belousov 		 * is used to avoid immediate un-suspend.
10986ddcc233SKonstantin Belousov 		 */
10996ddcc233SKonstantin Belousov 		if (TD_IS_SUSPENDED(td2) && (td2->td_flags & (TDF_BOUNDARY |
110061a74c5cSJeff Roberson 		    TDF_ALLPROCSUSP)) == 0) {
110184cdea97SKonstantin Belousov 			wakeup_swapper |= thread_unsuspend_one(td2, p, false);
110261a74c5cSJeff Roberson 			thread_lock(td2);
110361a74c5cSJeff Roberson 			goto restart;
110461a74c5cSJeff Roberson 		}
110561a74c5cSJeff Roberson 		if (TD_CAN_ABORT(td2)) {
11066ddcc233SKonstantin Belousov 			if ((td2->td_flags & TDF_SBDRY) == 0) {
11076ddcc233SKonstantin Belousov 				thread_suspend_one(td2);
11086ddcc233SKonstantin Belousov 				td2->td_flags |= TDF_ALLPROCSUSP;
11096ddcc233SKonstantin Belousov 			} else {
11106ddcc233SKonstantin Belousov 				wakeup_swapper |= sleepq_abort(td2, ERESTART);
111161a74c5cSJeff Roberson 				return (wakeup_swapper);
11126ddcc233SKonstantin Belousov 			}
11136ddcc233SKonstantin Belousov 		}
111407a9368aSKonstantin Belousov 		break;
111561a74c5cSJeff Roberson 	default:
111661a74c5cSJeff Roberson 		break;
111707a9368aSKonstantin Belousov 	}
111861a74c5cSJeff Roberson 	thread_unlock(td2);
111907a9368aSKonstantin Belousov 	return (wakeup_swapper);
112007a9368aSKonstantin Belousov }
112107a9368aSKonstantin Belousov 
11225215b187SJeff Roberson /*
112344990b8cSJulian Elischer  * Enforce single-threading.
112444990b8cSJulian Elischer  *
112544990b8cSJulian Elischer  * Returns 1 if the caller must abort (another thread is waiting to
112644990b8cSJulian Elischer  * exit the process or similar). Process is locked!
112744990b8cSJulian Elischer  * Returns 0 when you are successfully the only thread running.
112844990b8cSJulian Elischer  * A process has successfully single threaded in the suspend mode when
112944990b8cSJulian Elischer  * There are no threads in user mode. Threads in the kernel must be
113044990b8cSJulian Elischer  * allowed to continue until they get to the user boundary. They may even
113144990b8cSJulian Elischer  * copy out their return values and data before suspending. They may however be
1132e2668f55SMaxim Konovalov  * accelerated in reaching the user boundary as we will wake up
113344990b8cSJulian Elischer  * any sleeping threads that are interruptable. (PCATCH).
113444990b8cSJulian Elischer  */
113544990b8cSJulian Elischer int
11366ddcc233SKonstantin Belousov thread_single(struct proc *p, int mode)
113744990b8cSJulian Elischer {
113844990b8cSJulian Elischer 	struct thread *td;
113944990b8cSJulian Elischer 	struct thread *td2;
1140da7bbd2cSJohn Baldwin 	int remaining, wakeup_swapper;
114144990b8cSJulian Elischer 
114244990b8cSJulian Elischer 	td = curthread;
11436ddcc233SKonstantin Belousov 	KASSERT(mode == SINGLE_EXIT || mode == SINGLE_BOUNDARY ||
11446ddcc233SKonstantin Belousov 	    mode == SINGLE_ALLPROC || mode == SINGLE_NO_EXIT,
11456ddcc233SKonstantin Belousov 	    ("invalid mode %d", mode));
11466ddcc233SKonstantin Belousov 	/*
11476ddcc233SKonstantin Belousov 	 * If allowing non-ALLPROC singlethreading for non-curproc
11486ddcc233SKonstantin Belousov 	 * callers, calc_remaining() and remain_for_mode() should be
11496ddcc233SKonstantin Belousov 	 * adjusted to also account for td->td_proc != p.  For now
11506ddcc233SKonstantin Belousov 	 * this is not implemented because it is not used.
11516ddcc233SKonstantin Belousov 	 */
11526ddcc233SKonstantin Belousov 	KASSERT((mode == SINGLE_ALLPROC && td->td_proc != p) ||
11536ddcc233SKonstantin Belousov 	    (mode != SINGLE_ALLPROC && td->td_proc == p),
11546ddcc233SKonstantin Belousov 	    ("mode %d proc %p curproc %p", mode, p, td->td_proc));
115537814395SPeter Wemm 	mtx_assert(&Giant, MA_NOTOWNED);
115644990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
115744990b8cSJulian Elischer 
11586ddcc233SKonstantin Belousov 	if ((p->p_flag & P_HADTHREADS) == 0 && mode != SINGLE_ALLPROC)
115944990b8cSJulian Elischer 		return (0);
116044990b8cSJulian Elischer 
1161e3b9bf71SJulian Elischer 	/* Is someone already single threading? */
1162906ac69dSDavid Xu 	if (p->p_singlethread != NULL && p->p_singlethread != td)
116344990b8cSJulian Elischer 		return (1);
116444990b8cSJulian Elischer 
1165906ac69dSDavid Xu 	if (mode == SINGLE_EXIT) {
1166906ac69dSDavid Xu 		p->p_flag |= P_SINGLE_EXIT;
1167906ac69dSDavid Xu 		p->p_flag &= ~P_SINGLE_BOUNDARY;
1168906ac69dSDavid Xu 	} else {
1169906ac69dSDavid Xu 		p->p_flag &= ~P_SINGLE_EXIT;
1170906ac69dSDavid Xu 		if (mode == SINGLE_BOUNDARY)
1171906ac69dSDavid Xu 			p->p_flag |= P_SINGLE_BOUNDARY;
1172906ac69dSDavid Xu 		else
1173906ac69dSDavid Xu 			p->p_flag &= ~P_SINGLE_BOUNDARY;
1174906ac69dSDavid Xu 	}
11756ddcc233SKonstantin Belousov 	if (mode == SINGLE_ALLPROC)
11766ddcc233SKonstantin Belousov 		p->p_flag |= P_TOTAL_STOP;
11771279572aSDavid Xu 	p->p_flag |= P_STOPPED_SINGLE;
11787b4a950aSDavid Xu 	PROC_SLOCK(p);
1179112afcb2SJohn Baldwin 	p->p_singlethread = td;
118079799053SKonstantin Belousov 	remaining = calc_remaining(p, mode);
118107a9368aSKonstantin Belousov 	while (remaining != remain_for_mode(mode)) {
1182bf1a3220SDavid Xu 		if (P_SHOULDSTOP(p) != P_STOPPED_SINGLE)
1183bf1a3220SDavid Xu 			goto stopme;
1184da7bbd2cSJohn Baldwin 		wakeup_swapper = 0;
118544990b8cSJulian Elischer 		FOREACH_THREAD_IN_PROC(p, td2) {
118644990b8cSJulian Elischer 			if (td2 == td)
118744990b8cSJulian Elischer 				continue;
1188a54e85fdSJeff Roberson 			thread_lock(td2);
1189b7edba77SJeff Roberson 			td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
11906ddcc233SKonstantin Belousov 			if (TD_IS_INHIBITED(td2)) {
119107a9368aSKonstantin Belousov 				wakeup_swapper |= weed_inhib(mode, td2, p);
1192d8267df7SDavid Xu #ifdef SMP
11936ddcc233SKonstantin Belousov 			} else if (TD_IS_RUNNING(td2) && td != td2) {
1194d8267df7SDavid Xu 				forward_signal(td2);
119561a74c5cSJeff Roberson 				thread_unlock(td2);
1196d8267df7SDavid Xu #endif
119761a74c5cSJeff Roberson 			} else
1198a54e85fdSJeff Roberson 				thread_unlock(td2);
11999d102777SJulian Elischer 		}
1200da7bbd2cSJohn Baldwin 		if (wakeup_swapper)
1201da7bbd2cSJohn Baldwin 			kick_proc0();
120279799053SKonstantin Belousov 		remaining = calc_remaining(p, mode);
1203ec008e96SDavid Xu 
12049d102777SJulian Elischer 		/*
12059d102777SJulian Elischer 		 * Maybe we suspended some threads.. was it enough?
12069d102777SJulian Elischer 		 */
120707a9368aSKonstantin Belousov 		if (remaining == remain_for_mode(mode))
12089d102777SJulian Elischer 			break;
12099d102777SJulian Elischer 
1210bf1a3220SDavid Xu stopme:
121144990b8cSJulian Elischer 		/*
121244990b8cSJulian Elischer 		 * Wake us up when everyone else has suspended.
1213e3b9bf71SJulian Elischer 		 * In the mean time we suspend as well.
121444990b8cSJulian Elischer 		 */
12156ddcc233SKonstantin Belousov 		thread_suspend_switch(td, p);
121679799053SKonstantin Belousov 		remaining = calc_remaining(p, mode);
121744990b8cSJulian Elischer 	}
1218906ac69dSDavid Xu 	if (mode == SINGLE_EXIT) {
121991599697SJulian Elischer 		/*
12208626a0ddSKonstantin Belousov 		 * Convert the process to an unthreaded process.  The
12218626a0ddSKonstantin Belousov 		 * SINGLE_EXIT is called by exit1() or execve(), in
12228626a0ddSKonstantin Belousov 		 * both cases other threads must be retired.
122391599697SJulian Elischer 		 */
12248626a0ddSKonstantin Belousov 		KASSERT(p->p_numthreads == 1, ("Unthreading with >1 threads"));
1225ed062c8dSJulian Elischer 		p->p_singlethread = NULL;
12268626a0ddSKonstantin Belousov 		p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_HADTHREADS);
1227fd229b5bSKonstantin Belousov 
1228fd229b5bSKonstantin Belousov 		/*
1229fd229b5bSKonstantin Belousov 		 * Wait for any remaining threads to exit cpu_throw().
1230fd229b5bSKonstantin Belousov 		 */
1231fd229b5bSKonstantin Belousov 		while (p->p_exitthreads != 0) {
1232fd229b5bSKonstantin Belousov 			PROC_SUNLOCK(p);
1233fd229b5bSKonstantin Belousov 			PROC_UNLOCK(p);
1234fd229b5bSKonstantin Belousov 			sched_relinquish(td);
1235fd229b5bSKonstantin Belousov 			PROC_LOCK(p);
1236fd229b5bSKonstantin Belousov 			PROC_SLOCK(p);
1237fd229b5bSKonstantin Belousov 		}
1238ac437c07SKonstantin Belousov 	} else if (mode == SINGLE_BOUNDARY) {
1239ac437c07SKonstantin Belousov 		/*
1240ac437c07SKonstantin Belousov 		 * Wait until all suspended threads are removed from
1241ac437c07SKonstantin Belousov 		 * the processors.  The thread_suspend_check()
1242ac437c07SKonstantin Belousov 		 * increments p_boundary_count while it is still
1243ac437c07SKonstantin Belousov 		 * running, which makes it possible for the execve()
1244ac437c07SKonstantin Belousov 		 * to destroy vmspace while our other threads are
1245ac437c07SKonstantin Belousov 		 * still using the address space.
1246ac437c07SKonstantin Belousov 		 *
1247ac437c07SKonstantin Belousov 		 * We lock the thread, which is only allowed to
1248ac437c07SKonstantin Belousov 		 * succeed after context switch code finished using
1249ac437c07SKonstantin Belousov 		 * the address space.
1250ac437c07SKonstantin Belousov 		 */
1251ac437c07SKonstantin Belousov 		FOREACH_THREAD_IN_PROC(p, td2) {
1252ac437c07SKonstantin Belousov 			if (td2 == td)
1253ac437c07SKonstantin Belousov 				continue;
1254ac437c07SKonstantin Belousov 			thread_lock(td2);
1255ac437c07SKonstantin Belousov 			KASSERT((td2->td_flags & TDF_BOUNDARY) != 0,
1256ac437c07SKonstantin Belousov 			    ("td %p not on boundary", td2));
1257ac437c07SKonstantin Belousov 			KASSERT(TD_IS_SUSPENDED(td2),
1258ac437c07SKonstantin Belousov 			    ("td %p is not suspended", td2));
1259ac437c07SKonstantin Belousov 			thread_unlock(td2);
1260ac437c07SKonstantin Belousov 		}
126191599697SJulian Elischer 	}
12627b4a950aSDavid Xu 	PROC_SUNLOCK(p);
126344990b8cSJulian Elischer 	return (0);
126444990b8cSJulian Elischer }
126544990b8cSJulian Elischer 
12668638fe7bSKonstantin Belousov bool
12678638fe7bSKonstantin Belousov thread_suspend_check_needed(void)
12688638fe7bSKonstantin Belousov {
12698638fe7bSKonstantin Belousov 	struct proc *p;
12708638fe7bSKonstantin Belousov 	struct thread *td;
12718638fe7bSKonstantin Belousov 
12728638fe7bSKonstantin Belousov 	td = curthread;
12738638fe7bSKonstantin Belousov 	p = td->td_proc;
12748638fe7bSKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
12758638fe7bSKonstantin Belousov 	return (P_SHOULDSTOP(p) || ((p->p_flag & P_TRACED) != 0 &&
12768638fe7bSKonstantin Belousov 	    (td->td_dbgflags & TDB_SUSPEND) != 0));
12778638fe7bSKonstantin Belousov }
12788638fe7bSKonstantin Belousov 
127944990b8cSJulian Elischer /*
128044990b8cSJulian Elischer  * Called in from locations that can safely check to see
128144990b8cSJulian Elischer  * whether we have to suspend or at least throttle for a
128244990b8cSJulian Elischer  * single-thread event (e.g. fork).
128344990b8cSJulian Elischer  *
128444990b8cSJulian Elischer  * Such locations include userret().
128544990b8cSJulian Elischer  * If the "return_instead" argument is non zero, the thread must be able to
128644990b8cSJulian Elischer  * accept 0 (caller may continue), or 1 (caller must abort) as a result.
128744990b8cSJulian Elischer  *
128844990b8cSJulian Elischer  * The 'return_instead' argument tells the function if it may do a
128944990b8cSJulian Elischer  * thread_exit() or suspend, or whether the caller must abort and back
129044990b8cSJulian Elischer  * out instead.
129144990b8cSJulian Elischer  *
129244990b8cSJulian Elischer  * If the thread that set the single_threading request has set the
129344990b8cSJulian Elischer  * P_SINGLE_EXIT bit in the process flags then this call will never return
129444990b8cSJulian Elischer  * if 'return_instead' is false, but will exit.
129544990b8cSJulian Elischer  *
129644990b8cSJulian Elischer  * P_SINGLE_EXIT | return_instead == 0| return_instead != 0
129744990b8cSJulian Elischer  *---------------+--------------------+---------------------
129844990b8cSJulian Elischer  *       0       | returns 0          |   returns 0 or 1
1299353374b5SJohn Baldwin  *               | when ST ends       |   immediately
130044990b8cSJulian Elischer  *---------------+--------------------+---------------------
130144990b8cSJulian Elischer  *       1       | thread exits       |   returns 1
1302353374b5SJohn Baldwin  *               |                    |  immediately
130344990b8cSJulian Elischer  * 0 = thread_exit() or suspension ok,
130444990b8cSJulian Elischer  * other = return error instead of stopping the thread.
130544990b8cSJulian Elischer  *
130644990b8cSJulian Elischer  * While a full suspension is under effect, even a single threading
130744990b8cSJulian Elischer  * thread would be suspended if it made this call (but it shouldn't).
130844990b8cSJulian Elischer  * This call should only be made from places where
130944990b8cSJulian Elischer  * thread_exit() would be safe as that may be the outcome unless
131044990b8cSJulian Elischer  * return_instead is set.
131144990b8cSJulian Elischer  */
131244990b8cSJulian Elischer int
131344990b8cSJulian Elischer thread_suspend_check(int return_instead)
131444990b8cSJulian Elischer {
1315ecafb24bSJuli Mallett 	struct thread *td;
1316ecafb24bSJuli Mallett 	struct proc *p;
131746e47c4fSKonstantin Belousov 	int wakeup_swapper;
131844990b8cSJulian Elischer 
131944990b8cSJulian Elischer 	td = curthread;
132044990b8cSJulian Elischer 	p = td->td_proc;
132137814395SPeter Wemm 	mtx_assert(&Giant, MA_NOTOWNED);
132244990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
13238638fe7bSKonstantin Belousov 	while (thread_suspend_check_needed()) {
13241279572aSDavid Xu 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
132544990b8cSJulian Elischer 			KASSERT(p->p_singlethread != NULL,
132644990b8cSJulian Elischer 			    ("singlethread not set"));
132744990b8cSJulian Elischer 			/*
1328e3b9bf71SJulian Elischer 			 * The only suspension in action is a
1329e3b9bf71SJulian Elischer 			 * single-threading. Single threader need not stop.
1330bd07998eSKonstantin Belousov 			 * It is safe to access p->p_singlethread unlocked
1331bd07998eSKonstantin Belousov 			 * because it can only be set to our address by us.
133244990b8cSJulian Elischer 			 */
1333e3b9bf71SJulian Elischer 			if (p->p_singlethread == td)
133444990b8cSJulian Elischer 				return (0);	/* Exempt from stopping. */
133544990b8cSJulian Elischer 		}
133645a4bfa1SDavid Xu 		if ((p->p_flag & P_SINGLE_EXIT) && return_instead)
133794f0972bSDavid Xu 			return (EINTR);
133844990b8cSJulian Elischer 
1339906ac69dSDavid Xu 		/* Should we goto user boundary if we didn't come from there? */
1340906ac69dSDavid Xu 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE &&
1341906ac69dSDavid Xu 		    (p->p_flag & P_SINGLE_BOUNDARY) && return_instead)
134294f0972bSDavid Xu 			return (ERESTART);
1343906ac69dSDavid Xu 
134444990b8cSJulian Elischer 		/*
13453077f938SKonstantin Belousov 		 * Ignore suspend requests if they are deferred.
1346d071a6faSJohn Baldwin 		 */
13473077f938SKonstantin Belousov 		if ((td->td_flags & TDF_SBDRY) != 0) {
1348d071a6faSJohn Baldwin 			KASSERT(return_instead,
1349d071a6faSJohn Baldwin 			    ("TDF_SBDRY set for unsafe thread_suspend_check"));
135046e47c4fSKonstantin Belousov 			KASSERT((td->td_flags & (TDF_SEINTR | TDF_SERESTART)) !=
135146e47c4fSKonstantin Belousov 			    (TDF_SEINTR | TDF_SERESTART),
135246e47c4fSKonstantin Belousov 			    ("both TDF_SEINTR and TDF_SERESTART"));
135346e47c4fSKonstantin Belousov 			return (TD_SBDRY_INTR(td) ? TD_SBDRY_ERRNO(td) : 0);
1354d071a6faSJohn Baldwin 		}
1355d071a6faSJohn Baldwin 
1356d071a6faSJohn Baldwin 		/*
135744990b8cSJulian Elischer 		 * If the process is waiting for us to exit,
135844990b8cSJulian Elischer 		 * this thread should just suicide.
13591279572aSDavid Xu 		 * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE.
136044990b8cSJulian Elischer 		 */
1361cf7d9a8cSDavid Xu 		if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) {
1362cf7d9a8cSDavid Xu 			PROC_UNLOCK(p);
136391d1786fSDmitry Chagin 
136491d1786fSDmitry Chagin 			/*
136591d1786fSDmitry Chagin 			 * Allow Linux emulation layer to do some work
136691d1786fSDmitry Chagin 			 * before thread suicide.
136791d1786fSDmitry Chagin 			 */
136891d1786fSDmitry Chagin 			if (__predict_false(p->p_sysent->sv_thread_detach != NULL))
136991d1786fSDmitry Chagin 				(p->p_sysent->sv_thread_detach)(td);
13702a339d9eSKonstantin Belousov 			umtx_thread_exit(td);
1371d1e7a4a5SJohn Baldwin 			kern_thr_exit(td);
1372d1e7a4a5SJohn Baldwin 			panic("stopped thread did not exit");
1373cf7d9a8cSDavid Xu 		}
137421ecd1e9SDavid Xu 
137521ecd1e9SDavid Xu 		PROC_SLOCK(p);
137621ecd1e9SDavid Xu 		thread_stopped(p);
1377a54e85fdSJeff Roberson 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
1378a54e85fdSJeff Roberson 			if (p->p_numthreads == p->p_suspcount + 1) {
1379a54e85fdSJeff Roberson 				thread_lock(p->p_singlethread);
138084cdea97SKonstantin Belousov 				wakeup_swapper = thread_unsuspend_one(
138184cdea97SKonstantin Belousov 				    p->p_singlethread, p, false);
13827847a9daSJohn Baldwin 				if (wakeup_swapper)
13837847a9daSJohn Baldwin 					kick_proc0();
1384a54e85fdSJeff Roberson 			}
1385a54e85fdSJeff Roberson 		}
13863f9be10eSDavid Xu 		PROC_UNLOCK(p);
13877b4a950aSDavid Xu 		thread_lock(td);
138844990b8cSJulian Elischer 		/*
138944990b8cSJulian Elischer 		 * When a thread suspends, it just
1390ad1e7d28SJulian Elischer 		 * gets taken off all queues.
139144990b8cSJulian Elischer 		 */
139271fad9fdSJulian Elischer 		thread_suspend_one(td);
1393906ac69dSDavid Xu 		if (return_instead == 0) {
1394906ac69dSDavid Xu 			p->p_boundary_count++;
1395906ac69dSDavid Xu 			td->td_flags |= TDF_BOUNDARY;
1396cf19bf91SJulian Elischer 		}
13977b4a950aSDavid Xu 		PROC_SUNLOCK(p);
1398686bcb5cSJeff Roberson 		mi_switch(SW_INVOL | SWT_SUSPEND);
139944990b8cSJulian Elischer 		PROC_LOCK(p);
140044990b8cSJulian Elischer 	}
140144990b8cSJulian Elischer 	return (0);
140244990b8cSJulian Elischer }
140344990b8cSJulian Elischer 
1404478ca4b0SKonstantin Belousov /*
1405478ca4b0SKonstantin Belousov  * Check for possible stops and suspensions while executing a
1406478ca4b0SKonstantin Belousov  * casueword or similar transiently failing operation.
1407478ca4b0SKonstantin Belousov  *
1408478ca4b0SKonstantin Belousov  * The sleep argument controls whether the function can handle a stop
1409478ca4b0SKonstantin Belousov  * request itself or it should return ERESTART and the request is
1410478ca4b0SKonstantin Belousov  * proceed at the kernel/user boundary in ast.
1411478ca4b0SKonstantin Belousov  *
1412478ca4b0SKonstantin Belousov  * Typically, when retrying due to casueword(9) failure (rv == 1), we
1413478ca4b0SKonstantin Belousov  * should handle the stop requests there, with exception of cases when
1414478ca4b0SKonstantin Belousov  * the thread owns a kernel resource, for instance busied the umtx
1415300b525dSKonstantin Belousov  * key, or when functions return immediately if thread_check_susp()
1416478ca4b0SKonstantin Belousov  * returned non-zero.  On the other hand, retrying the whole lock
1417478ca4b0SKonstantin Belousov  * operation, we better not stop there but delegate the handling to
1418478ca4b0SKonstantin Belousov  * ast.
1419478ca4b0SKonstantin Belousov  *
1420478ca4b0SKonstantin Belousov  * If the request is for thread termination P_SINGLE_EXIT, we cannot
1421478ca4b0SKonstantin Belousov  * handle it at all, and simply return EINTR.
1422478ca4b0SKonstantin Belousov  */
1423478ca4b0SKonstantin Belousov int
1424478ca4b0SKonstantin Belousov thread_check_susp(struct thread *td, bool sleep)
1425478ca4b0SKonstantin Belousov {
1426478ca4b0SKonstantin Belousov 	struct proc *p;
1427478ca4b0SKonstantin Belousov 	int error;
1428478ca4b0SKonstantin Belousov 
1429478ca4b0SKonstantin Belousov 	/*
1430478ca4b0SKonstantin Belousov 	 * The check for TDF_NEEDSUSPCHK is racy, but it is enough to
1431478ca4b0SKonstantin Belousov 	 * eventually break the lockstep loop.
1432478ca4b0SKonstantin Belousov 	 */
1433478ca4b0SKonstantin Belousov 	if ((td->td_flags & TDF_NEEDSUSPCHK) == 0)
1434478ca4b0SKonstantin Belousov 		return (0);
1435478ca4b0SKonstantin Belousov 	error = 0;
1436478ca4b0SKonstantin Belousov 	p = td->td_proc;
1437478ca4b0SKonstantin Belousov 	PROC_LOCK(p);
1438478ca4b0SKonstantin Belousov 	if (p->p_flag & P_SINGLE_EXIT)
1439478ca4b0SKonstantin Belousov 		error = EINTR;
1440478ca4b0SKonstantin Belousov 	else if (P_SHOULDSTOP(p) ||
1441478ca4b0SKonstantin Belousov 	    ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_SUSPEND)))
1442478ca4b0SKonstantin Belousov 		error = sleep ? thread_suspend_check(0) : ERESTART;
1443478ca4b0SKonstantin Belousov 	PROC_UNLOCK(p);
1444478ca4b0SKonstantin Belousov 	return (error);
1445478ca4b0SKonstantin Belousov }
1446478ca4b0SKonstantin Belousov 
144735c32a76SDavid Xu void
14486ddcc233SKonstantin Belousov thread_suspend_switch(struct thread *td, struct proc *p)
1449a54e85fdSJeff Roberson {
1450a54e85fdSJeff Roberson 
1451a54e85fdSJeff Roberson 	KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
1452a54e85fdSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
14537b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
1454a54e85fdSJeff Roberson 	/*
1455a54e85fdSJeff Roberson 	 * We implement thread_suspend_one in stages here to avoid
1456a54e85fdSJeff Roberson 	 * dropping the proc lock while the thread lock is owned.
1457a54e85fdSJeff Roberson 	 */
14586ddcc233SKonstantin Belousov 	if (p == td->td_proc) {
1459a54e85fdSJeff Roberson 		thread_stopped(p);
1460a54e85fdSJeff Roberson 		p->p_suspcount++;
14616ddcc233SKonstantin Belousov 	}
14623f9be10eSDavid Xu 	PROC_UNLOCK(p);
14637b4a950aSDavid Xu 	thread_lock(td);
1464b7edba77SJeff Roberson 	td->td_flags &= ~TDF_NEEDSUSPCHK;
1465a54e85fdSJeff Roberson 	TD_SET_SUSPENDED(td);
1466c5aa6b58SJeff Roberson 	sched_sleep(td, 0);
14677b4a950aSDavid Xu 	PROC_SUNLOCK(p);
1468a54e85fdSJeff Roberson 	DROP_GIANT();
1469686bcb5cSJeff Roberson 	mi_switch(SW_VOL | SWT_SUSPEND);
1470a54e85fdSJeff Roberson 	PICKUP_GIANT();
1471a54e85fdSJeff Roberson 	PROC_LOCK(p);
14727b4a950aSDavid Xu 	PROC_SLOCK(p);
1473a54e85fdSJeff Roberson }
1474a54e85fdSJeff Roberson 
1475a54e85fdSJeff Roberson void
147635c32a76SDavid Xu thread_suspend_one(struct thread *td)
147735c32a76SDavid Xu {
14786ddcc233SKonstantin Belousov 	struct proc *p;
147935c32a76SDavid Xu 
14806ddcc233SKonstantin Belousov 	p = td->td_proc;
14817b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
1482a54e85fdSJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1483e574e444SDavid Xu 	KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
148435c32a76SDavid Xu 	p->p_suspcount++;
1485b7edba77SJeff Roberson 	td->td_flags &= ~TDF_NEEDSUSPCHK;
148671fad9fdSJulian Elischer 	TD_SET_SUSPENDED(td);
1487c5aa6b58SJeff Roberson 	sched_sleep(td, 0);
148835c32a76SDavid Xu }
148935c32a76SDavid Xu 
149084cdea97SKonstantin Belousov static int
149184cdea97SKonstantin Belousov thread_unsuspend_one(struct thread *td, struct proc *p, bool boundary)
149235c32a76SDavid Xu {
149335c32a76SDavid Xu 
1494a54e85fdSJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1495ad1e7d28SJulian Elischer 	KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended"));
149671fad9fdSJulian Elischer 	TD_CLR_SUSPENDED(td);
14976ddcc233SKonstantin Belousov 	td->td_flags &= ~TDF_ALLPROCSUSP;
14986ddcc233SKonstantin Belousov 	if (td->td_proc == p) {
14996ddcc233SKonstantin Belousov 		PROC_SLOCK_ASSERT(p, MA_OWNED);
150035c32a76SDavid Xu 		p->p_suspcount--;
150184cdea97SKonstantin Belousov 		if (boundary && (td->td_flags & TDF_BOUNDARY) != 0) {
150284cdea97SKonstantin Belousov 			td->td_flags &= ~TDF_BOUNDARY;
150384cdea97SKonstantin Belousov 			p->p_boundary_count--;
150484cdea97SKonstantin Belousov 		}
15056ddcc233SKonstantin Belousov 	}
150661a74c5cSJeff Roberson 	return (setrunnable(td, 0));
150735c32a76SDavid Xu }
150835c32a76SDavid Xu 
150944990b8cSJulian Elischer /*
151044990b8cSJulian Elischer  * Allow all threads blocked by single threading to continue running.
151144990b8cSJulian Elischer  */
151244990b8cSJulian Elischer void
151344990b8cSJulian Elischer thread_unsuspend(struct proc *p)
151444990b8cSJulian Elischer {
151544990b8cSJulian Elischer 	struct thread *td;
15167847a9daSJohn Baldwin 	int wakeup_swapper;
151744990b8cSJulian Elischer 
151844990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
15197b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
15207847a9daSJohn Baldwin 	wakeup_swapper = 0;
152144990b8cSJulian Elischer 	if (!P_SHOULDSTOP(p)) {
1522ad1e7d28SJulian Elischer                 FOREACH_THREAD_IN_PROC(p, td) {
1523a54e85fdSJeff Roberson 			thread_lock(td);
1524ad1e7d28SJulian Elischer 			if (TD_IS_SUSPENDED(td)) {
152584cdea97SKonstantin Belousov 				wakeup_swapper |= thread_unsuspend_one(td, p,
152684cdea97SKonstantin Belousov 				    true);
152761a74c5cSJeff Roberson 			} else
1528a54e85fdSJeff Roberson 				thread_unlock(td);
1529ad1e7d28SJulian Elischer 		}
153084cdea97SKonstantin Belousov 	} else if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE &&
153184cdea97SKonstantin Belousov 	    p->p_numthreads == p->p_suspcount) {
153244990b8cSJulian Elischer 		/*
153344990b8cSJulian Elischer 		 * Stopping everything also did the job for the single
153444990b8cSJulian Elischer 		 * threading request. Now we've downgraded to single-threaded,
153544990b8cSJulian Elischer 		 * let it continue.
153644990b8cSJulian Elischer 		 */
15376ddcc233SKonstantin Belousov 		if (p->p_singlethread->td_proc == p) {
1538a54e85fdSJeff Roberson 			thread_lock(p->p_singlethread);
15396ddcc233SKonstantin Belousov 			wakeup_swapper = thread_unsuspend_one(
154084cdea97SKonstantin Belousov 			    p->p_singlethread, p, false);
154144990b8cSJulian Elischer 		}
15426ddcc233SKonstantin Belousov 	}
15437847a9daSJohn Baldwin 	if (wakeup_swapper)
15447847a9daSJohn Baldwin 		kick_proc0();
154544990b8cSJulian Elischer }
154644990b8cSJulian Elischer 
1547ed062c8dSJulian Elischer /*
1548ed062c8dSJulian Elischer  * End the single threading mode..
1549ed062c8dSJulian Elischer  */
155044990b8cSJulian Elischer void
15516ddcc233SKonstantin Belousov thread_single_end(struct proc *p, int mode)
155244990b8cSJulian Elischer {
155344990b8cSJulian Elischer 	struct thread *td;
15547847a9daSJohn Baldwin 	int wakeup_swapper;
155544990b8cSJulian Elischer 
15566ddcc233SKonstantin Belousov 	KASSERT(mode == SINGLE_EXIT || mode == SINGLE_BOUNDARY ||
15576ddcc233SKonstantin Belousov 	    mode == SINGLE_ALLPROC || mode == SINGLE_NO_EXIT,
15586ddcc233SKonstantin Belousov 	    ("invalid mode %d", mode));
155944990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
15606ddcc233SKonstantin Belousov 	KASSERT((mode == SINGLE_ALLPROC && (p->p_flag & P_TOTAL_STOP) != 0) ||
15616ddcc233SKonstantin Belousov 	    (mode != SINGLE_ALLPROC && (p->p_flag & P_TOTAL_STOP) == 0),
15626ddcc233SKonstantin Belousov 	    ("mode %d does not match P_TOTAL_STOP", mode));
156384cdea97SKonstantin Belousov 	KASSERT(mode == SINGLE_ALLPROC || p->p_singlethread == curthread,
156484cdea97SKonstantin Belousov 	    ("thread_single_end from other thread %p %p",
156584cdea97SKonstantin Belousov 	    curthread, p->p_singlethread));
156684cdea97SKonstantin Belousov 	KASSERT(mode != SINGLE_BOUNDARY ||
156784cdea97SKonstantin Belousov 	    (p->p_flag & P_SINGLE_BOUNDARY) != 0,
156884cdea97SKonstantin Belousov 	    ("mis-matched SINGLE_BOUNDARY flags %x", p->p_flag));
15696ddcc233SKonstantin Belousov 	p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_SINGLE_BOUNDARY |
15706ddcc233SKonstantin Belousov 	    P_TOTAL_STOP);
15717b4a950aSDavid Xu 	PROC_SLOCK(p);
157244990b8cSJulian Elischer 	p->p_singlethread = NULL;
15737847a9daSJohn Baldwin 	wakeup_swapper = 0;
157449539972SJulian Elischer 	/*
15757847a9daSJohn Baldwin 	 * If there are other threads they may now run,
157649539972SJulian Elischer 	 * unless of course there is a blanket 'stop order'
157749539972SJulian Elischer 	 * on the process. The single threader must be allowed
157849539972SJulian Elischer 	 * to continue however as this is a bad place to stop.
157949539972SJulian Elischer 	 */
15806ddcc233SKonstantin Belousov 	if (p->p_numthreads != remain_for_mode(mode) && !P_SHOULDSTOP(p)) {
1581ad1e7d28SJulian Elischer                 FOREACH_THREAD_IN_PROC(p, td) {
1582a54e85fdSJeff Roberson 			thread_lock(td);
1583ad1e7d28SJulian Elischer 			if (TD_IS_SUSPENDED(td)) {
158484cdea97SKonstantin Belousov 				wakeup_swapper |= thread_unsuspend_one(td, p,
158584cdea97SKonstantin Belousov 				    mode == SINGLE_BOUNDARY);
158661a74c5cSJeff Roberson 			} else
1587a54e85fdSJeff Roberson 				thread_unlock(td);
158849539972SJulian Elischer 		}
1589ad1e7d28SJulian Elischer 	}
159084cdea97SKonstantin Belousov 	KASSERT(mode != SINGLE_BOUNDARY || p->p_boundary_count == 0,
159184cdea97SKonstantin Belousov 	    ("inconsistent boundary count %d", p->p_boundary_count));
15927b4a950aSDavid Xu 	PROC_SUNLOCK(p);
15937847a9daSJohn Baldwin 	if (wakeup_swapper)
15947847a9daSJohn Baldwin 		kick_proc0();
159549539972SJulian Elischer }
15964fc21c09SDaniel Eischen 
1597aae3547bSMateusz Guzik /*
1598aae3547bSMateusz Guzik  * Locate a thread by number and return with proc lock held.
1599aae3547bSMateusz Guzik  *
1600aae3547bSMateusz Guzik  * thread exit establishes proc -> tidhash lock ordering, but lookup
1601aae3547bSMateusz Guzik  * takes tidhash first and needs to return locked proc.
1602aae3547bSMateusz Guzik  *
1603aae3547bSMateusz Guzik  * The problem is worked around by relying on type-safety of both
1604aae3547bSMateusz Guzik  * structures and doing the work in 2 steps:
1605aae3547bSMateusz Guzik  * - tidhash-locked lookup which saves both thread and proc pointers
1606aae3547bSMateusz Guzik  * - proc-locked verification that the found thread still matches
1607aae3547bSMateusz Guzik  */
1608aae3547bSMateusz Guzik static bool
1609aae3547bSMateusz Guzik tdfind_hash(lwpid_t tid, pid_t pid, struct proc **pp, struct thread **tdp)
1610cf7d9a8cSDavid Xu {
1611cf7d9a8cSDavid Xu #define RUN_THRESH	16
1612aae3547bSMateusz Guzik 	struct proc *p;
1613cf7d9a8cSDavid Xu 	struct thread *td;
1614aae3547bSMateusz Guzik 	int run;
1615aae3547bSMateusz Guzik 	bool locked;
1616cf7d9a8cSDavid Xu 
1617aae3547bSMateusz Guzik 	run = 0;
161826007fe3SMateusz Guzik 	rw_rlock(TIDHASHLOCK(tid));
1619aae3547bSMateusz Guzik 	locked = true;
1620cf7d9a8cSDavid Xu 	LIST_FOREACH(td, TIDHASH(tid), td_hash) {
1621aae3547bSMateusz Guzik 		if (td->td_tid != tid) {
1622aae3547bSMateusz Guzik 			run++;
1623aae3547bSMateusz Guzik 			continue;
1624cf7d9a8cSDavid Xu 		}
1625aae3547bSMateusz Guzik 		p = td->td_proc;
1626aae3547bSMateusz Guzik 		if (pid != -1 && p->p_pid != pid) {
1627cf7d9a8cSDavid Xu 			td = NULL;
1628cf7d9a8cSDavid Xu 			break;
1629cf7d9a8cSDavid Xu 		}
1630cf7d9a8cSDavid Xu 		if (run > RUN_THRESH) {
163126007fe3SMateusz Guzik 			if (rw_try_upgrade(TIDHASHLOCK(tid))) {
1632cf7d9a8cSDavid Xu 				LIST_REMOVE(td, td_hash);
1633cf7d9a8cSDavid Xu 				LIST_INSERT_HEAD(TIDHASH(td->td_tid),
1634cf7d9a8cSDavid Xu 					td, td_hash);
163526007fe3SMateusz Guzik 				rw_wunlock(TIDHASHLOCK(tid));
1636aae3547bSMateusz Guzik 				locked = false;
1637aae3547bSMateusz Guzik 				break;
1638cf7d9a8cSDavid Xu 			}
1639cf7d9a8cSDavid Xu 		}
1640cf7d9a8cSDavid Xu 		break;
1641cf7d9a8cSDavid Xu 	}
1642aae3547bSMateusz Guzik 	if (locked)
164326007fe3SMateusz Guzik 		rw_runlock(TIDHASHLOCK(tid));
1644aae3547bSMateusz Guzik 	if (td == NULL)
1645aae3547bSMateusz Guzik 		return (false);
1646aae3547bSMateusz Guzik 	*pp = p;
1647aae3547bSMateusz Guzik 	*tdp = td;
1648aae3547bSMateusz Guzik 	return (true);
1649aae3547bSMateusz Guzik }
1650aae3547bSMateusz Guzik 
1651aae3547bSMateusz Guzik struct thread *
1652aae3547bSMateusz Guzik tdfind(lwpid_t tid, pid_t pid)
1653aae3547bSMateusz Guzik {
1654aae3547bSMateusz Guzik 	struct proc *p;
1655aae3547bSMateusz Guzik 	struct thread *td;
1656aae3547bSMateusz Guzik 
1657aae3547bSMateusz Guzik 	td = curthread;
1658aae3547bSMateusz Guzik 	if (td->td_tid == tid) {
1659aae3547bSMateusz Guzik 		if (pid != -1 && td->td_proc->p_pid != pid)
1660aae3547bSMateusz Guzik 			return (NULL);
1661aae3547bSMateusz Guzik 		PROC_LOCK(td->td_proc);
1662cf7d9a8cSDavid Xu 		return (td);
1663cf7d9a8cSDavid Xu 	}
1664cf7d9a8cSDavid Xu 
1665aae3547bSMateusz Guzik 	for (;;) {
1666aae3547bSMateusz Guzik 		if (!tdfind_hash(tid, pid, &p, &td))
1667aae3547bSMateusz Guzik 			return (NULL);
1668aae3547bSMateusz Guzik 		PROC_LOCK(p);
1669aae3547bSMateusz Guzik 		if (td->td_tid != tid) {
1670aae3547bSMateusz Guzik 			PROC_UNLOCK(p);
1671aae3547bSMateusz Guzik 			continue;
1672aae3547bSMateusz Guzik 		}
1673aae3547bSMateusz Guzik 		if (td->td_proc != p) {
1674aae3547bSMateusz Guzik 			PROC_UNLOCK(p);
1675aae3547bSMateusz Guzik 			continue;
1676aae3547bSMateusz Guzik 		}
1677aae3547bSMateusz Guzik 		if (p->p_state == PRS_NEW) {
1678aae3547bSMateusz Guzik 			PROC_UNLOCK(p);
1679aae3547bSMateusz Guzik 			return (NULL);
1680aae3547bSMateusz Guzik 		}
1681aae3547bSMateusz Guzik 		return (td);
1682aae3547bSMateusz Guzik 	}
1683aae3547bSMateusz Guzik }
1684aae3547bSMateusz Guzik 
1685cf7d9a8cSDavid Xu void
1686cf7d9a8cSDavid Xu tidhash_add(struct thread *td)
1687cf7d9a8cSDavid Xu {
168826007fe3SMateusz Guzik 	rw_wlock(TIDHASHLOCK(td->td_tid));
1689cf7d9a8cSDavid Xu 	LIST_INSERT_HEAD(TIDHASH(td->td_tid), td, td_hash);
169026007fe3SMateusz Guzik 	rw_wunlock(TIDHASHLOCK(td->td_tid));
1691cf7d9a8cSDavid Xu }
1692cf7d9a8cSDavid Xu 
1693cf7d9a8cSDavid Xu void
1694cf7d9a8cSDavid Xu tidhash_remove(struct thread *td)
1695cf7d9a8cSDavid Xu {
169626007fe3SMateusz Guzik 
169726007fe3SMateusz Guzik 	rw_wlock(TIDHASHLOCK(td->td_tid));
1698cf7d9a8cSDavid Xu 	LIST_REMOVE(td, td_hash);
169926007fe3SMateusz Guzik 	rw_wunlock(TIDHASHLOCK(td->td_tid));
1700cf7d9a8cSDavid Xu }
1701