xref: /freebsd/sys/kern/kern_thread.c (revision 85078b8573332c2c83a79adea8a61b519fb3b6af)
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>
5544990b8cSJulian Elischer #include <sys/ktr.h>
56cf7d9a8cSDavid Xu #include <sys/rwlock.h>
57bc8e6d81SDavid Xu #include <sys/umtx.h>
589ed01c32SGleb Smirnoff #include <sys/vmmeter.h>
59d7f687fcSJeff Roberson #include <sys/cpuset.h>
6016d95d4fSJoseph Koshy #ifdef	HWPMC_HOOKS
6116d95d4fSJoseph Koshy #include <sys/pmckern.h>
6216d95d4fSJoseph Koshy #endif
631bd3cf5dSMateusz Guzik #include <sys/priv.h>
6444990b8cSJulian Elischer 
65911b84b0SRobert Watson #include <security/audit/audit.h>
66911b84b0SRobert Watson 
6744990b8cSJulian Elischer #include <vm/vm.h>
6849a2507bSAlan Cox #include <vm/vm_extern.h>
6944990b8cSJulian Elischer #include <vm/uma.h>
70b209f889SRandall Stewart #include <sys/eventhandler.h>
7102fb42b0SPeter Wemm 
72acd9f517SKonstantin Belousov /*
73acd9f517SKonstantin Belousov  * Asserts below verify the stability of struct thread and struct proc
74acd9f517SKonstantin Belousov  * layout, as exposed by KBI to modules.  On head, the KBI is allowed
75acd9f517SKonstantin Belousov  * to drift, change to the structures must be accompanied by the
76acd9f517SKonstantin Belousov  * assert update.
77acd9f517SKonstantin Belousov  *
78acd9f517SKonstantin Belousov  * On the stable branches after KBI freeze, conditions must not be
79acd9f517SKonstantin Belousov  * violated.  Typically new fields are moved to the end of the
80acd9f517SKonstantin Belousov  * structures.
81acd9f517SKonstantin Belousov  */
82acd9f517SKonstantin Belousov #ifdef __amd64__
833f289c3fSJeff Roberson _Static_assert(offsetof(struct thread, td_flags) == 0xfc,
84acd9f517SKonstantin Belousov     "struct thread KBI td_flags");
853f289c3fSJeff Roberson _Static_assert(offsetof(struct thread, td_pflags) == 0x104,
86acd9f517SKonstantin Belousov     "struct thread KBI td_pflags");
871e2521ffSEdward Tomasz Napierala _Static_assert(offsetof(struct thread, td_frame) == 0x4a0,
88acd9f517SKonstantin Belousov     "struct thread KBI td_frame");
891724c563SMateusz Guzik _Static_assert(offsetof(struct thread, td_emuldata) == 0x6b0,
90acd9f517SKonstantin Belousov     "struct thread KBI td_emuldata");
91*85078b85SConrad Meyer _Static_assert(offsetof(struct proc, p_flag) == 0xb8,
92acd9f517SKonstantin Belousov     "struct proc KBI p_flag");
93*85078b85SConrad Meyer _Static_assert(offsetof(struct proc, p_pid) == 0xc4,
94acd9f517SKonstantin Belousov     "struct proc KBI p_pid");
95*85078b85SConrad Meyer _Static_assert(offsetof(struct proc, p_filemon) == 0x3c0,
96acd9f517SKonstantin Belousov     "struct proc KBI p_filemon");
97*85078b85SConrad Meyer _Static_assert(offsetof(struct proc, p_comm) == 0x3d8,
98acd9f517SKonstantin Belousov     "struct proc KBI p_comm");
99*85078b85SConrad Meyer _Static_assert(offsetof(struct proc, p_emuldata) == 0x4b8,
100acd9f517SKonstantin Belousov     "struct proc KBI p_emuldata");
101acd9f517SKonstantin Belousov #endif
102acd9f517SKonstantin Belousov #ifdef __i386__
1033f289c3fSJeff Roberson _Static_assert(offsetof(struct thread, td_flags) == 0x98,
104acd9f517SKonstantin Belousov     "struct thread KBI td_flags");
1053f289c3fSJeff Roberson _Static_assert(offsetof(struct thread, td_pflags) == 0xa0,
106acd9f517SKonstantin Belousov     "struct thread KBI td_pflags");
1071e2521ffSEdward Tomasz Napierala _Static_assert(offsetof(struct thread, td_frame) == 0x300,
108acd9f517SKonstantin Belousov     "struct thread KBI td_frame");
1091e2521ffSEdward Tomasz Napierala _Static_assert(offsetof(struct thread, td_emuldata) == 0x344,
110acd9f517SKonstantin Belousov     "struct thread KBI td_emuldata");
111*85078b85SConrad Meyer _Static_assert(offsetof(struct proc, p_flag) == 0x6c,
112acd9f517SKonstantin Belousov     "struct proc KBI p_flag");
113*85078b85SConrad Meyer _Static_assert(offsetof(struct proc, p_pid) == 0x78,
114acd9f517SKonstantin Belousov     "struct proc KBI p_pid");
115*85078b85SConrad Meyer _Static_assert(offsetof(struct proc, p_filemon) == 0x26c,
116acd9f517SKonstantin Belousov     "struct proc KBI p_filemon");
117*85078b85SConrad Meyer _Static_assert(offsetof(struct proc, p_comm) == 0x280,
118acd9f517SKonstantin Belousov     "struct proc KBI p_comm");
119*85078b85SConrad Meyer _Static_assert(offsetof(struct proc, p_emuldata) == 0x30c,
120acd9f517SKonstantin Belousov     "struct proc KBI p_emuldata");
121acd9f517SKonstantin Belousov #endif
122acd9f517SKonstantin Belousov 
123b3e9e682SRyan Stone SDT_PROVIDER_DECLARE(proc);
124d9fae5abSAndriy Gapon SDT_PROBE_DEFINE(proc, , , lwp__exit);
125b3e9e682SRyan Stone 
1268460a577SJohn Birrell /*
1278460a577SJohn Birrell  * thread related storage.
1288460a577SJohn Birrell  */
12944990b8cSJulian Elischer static uma_zone_t thread_zone;
13044990b8cSJulian Elischer 
131c5315f51SMateusz Guzik static __exclusive_cache_line struct thread *thread_zombies;
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);
136755341dfSMateusz Guzik static void thread_free_batched(struct thread *td);
137ff8fbcffSJeff Roberson 
138d1ca25beSMateusz Guzik static __exclusive_cache_line struct mtx tid_lock;
139934e7e5eSMateusz Guzik static bitstr_t *tid_bitmap;
14035bb59edSMateusz Guzik 
141cf7d9a8cSDavid Xu static MALLOC_DEFINE(M_TIDHASH, "tidhash", "thread hash");
142cf7d9a8cSDavid Xu 
1431bd3cf5dSMateusz Guzik static int maxthread;
1441bd3cf5dSMateusz Guzik SYSCTL_INT(_kern, OID_AUTO, maxthread, CTLFLAG_RDTUN,
1451bd3cf5dSMateusz Guzik     &maxthread, 0, "Maximum number of threads");
1461bd3cf5dSMateusz Guzik 
14762dbc992SMateusz Guzik static __exclusive_cache_line int nthreads;
1481bd3cf5dSMateusz Guzik 
149aae3547bSMateusz Guzik static LIST_HEAD(tidhashhead, thread) *tidhashtbl;
150aae3547bSMateusz Guzik static u_long	tidhash;
15126007fe3SMateusz Guzik static u_long	tidhashlock;
15226007fe3SMateusz Guzik static struct	rwlock *tidhashtbl_lock;
153aae3547bSMateusz Guzik #define	TIDHASH(tid)		(&tidhashtbl[(tid) & tidhash])
15426007fe3SMateusz Guzik #define	TIDHASHLOCK(tid)	(&tidhashtbl_lock[(tid) & tidhashlock])
155cf7d9a8cSDavid Xu 
1562ca45184SMatt Joras EVENTHANDLER_LIST_DEFINE(thread_ctor);
1572ca45184SMatt Joras EVENTHANDLER_LIST_DEFINE(thread_dtor);
1582ca45184SMatt Joras EVENTHANDLER_LIST_DEFINE(thread_init);
1592ca45184SMatt Joras EVENTHANDLER_LIST_DEFINE(thread_fini);
1602ca45184SMatt Joras 
16162dbc992SMateusz Guzik static bool
16262dbc992SMateusz Guzik thread_count_inc(void)
163ec6ea5e8SDavid Xu {
1641bd3cf5dSMateusz Guzik 	static struct timeval lastfail;
1651bd3cf5dSMateusz Guzik 	static int curfail;
16662dbc992SMateusz Guzik 	int nthreads_new;
167ec6ea5e8SDavid Xu 
16862dbc992SMateusz Guzik 	thread_reap();
16962dbc992SMateusz Guzik 
17062dbc992SMateusz Guzik 	nthreads_new = atomic_fetchadd_int(&nthreads, 1) + 1;
17162dbc992SMateusz Guzik 	if (nthreads_new >= maxthread - 100) {
1721bd3cf5dSMateusz Guzik 		if (priv_check_cred(curthread->td_ucred, PRIV_MAXPROC) != 0 ||
17362dbc992SMateusz Guzik 		    nthreads_new >= maxthread) {
17462dbc992SMateusz Guzik 			atomic_subtract_int(&nthreads, 1);
1751bd3cf5dSMateusz Guzik 			if (ppsratecheck(&lastfail, &curfail, 1)) {
1761bd3cf5dSMateusz Guzik 				printf("maxthread limit exceeded by uid %u "
1771bd3cf5dSMateusz Guzik 				"(pid %d); consider increasing kern.maxthread\n",
1781bd3cf5dSMateusz Guzik 				curthread->td_ucred->cr_ruid, curproc->p_pid);
1791bd3cf5dSMateusz Guzik 			}
18062dbc992SMateusz Guzik 			return (false);
1811bd3cf5dSMateusz Guzik 		}
1821bd3cf5dSMateusz Guzik 	}
18362dbc992SMateusz Guzik 	return (true);
18462dbc992SMateusz Guzik }
1851bd3cf5dSMateusz Guzik 
18662dbc992SMateusz Guzik static void
18762dbc992SMateusz Guzik thread_count_sub(int n)
18862dbc992SMateusz Guzik {
18962dbc992SMateusz Guzik 
19062dbc992SMateusz Guzik 	atomic_subtract_int(&nthreads, n);
19162dbc992SMateusz Guzik }
19262dbc992SMateusz Guzik 
19362dbc992SMateusz Guzik static void
19462dbc992SMateusz Guzik thread_count_dec(void)
19562dbc992SMateusz Guzik {
19662dbc992SMateusz Guzik 
19762dbc992SMateusz Guzik 	thread_count_sub(1);
19862dbc992SMateusz Guzik }
19962dbc992SMateusz Guzik 
20062dbc992SMateusz Guzik static lwpid_t
20162dbc992SMateusz Guzik tid_alloc(void)
20262dbc992SMateusz Guzik {
20362dbc992SMateusz Guzik 	static lwpid_t trytid;
20462dbc992SMateusz Guzik 	lwpid_t tid;
20562dbc992SMateusz Guzik 
20662dbc992SMateusz Guzik 	mtx_lock(&tid_lock);
20735bb59edSMateusz Guzik 	/*
20835bb59edSMateusz Guzik 	 * It is an invariant that the bitmap is big enough to hold maxthread
20935bb59edSMateusz Guzik 	 * IDs. If we got to this point there has to be at least one free.
21035bb59edSMateusz Guzik 	 */
21135bb59edSMateusz Guzik 	if (trytid >= maxthread)
21235bb59edSMateusz Guzik 		trytid = 0;
21335bb59edSMateusz Guzik 	bit_ffc_at(tid_bitmap, trytid, maxthread, &tid);
21435bb59edSMateusz Guzik 	if (tid == -1) {
21535bb59edSMateusz Guzik 		KASSERT(trytid != 0, ("unexpectedly ran out of IDs"));
21635bb59edSMateusz Guzik 		trytid = 0;
21735bb59edSMateusz Guzik 		bit_ffc_at(tid_bitmap, trytid, maxthread, &tid);
21835bb59edSMateusz Guzik 		KASSERT(tid != -1, ("unexpectedly ran out of IDs"));
219ec6ea5e8SDavid Xu 	}
22035bb59edSMateusz Guzik 	bit_set(tid_bitmap, tid);
221934e7e5eSMateusz Guzik 	trytid = tid + 1;
222ec6ea5e8SDavid Xu 	mtx_unlock(&tid_lock);
22335bb59edSMateusz Guzik 	return (tid + NO_PID);
224ec6ea5e8SDavid Xu }
225ec6ea5e8SDavid Xu 
226ec6ea5e8SDavid Xu static void
227755341dfSMateusz Guzik tid_free_locked(lwpid_t rtid)
228ec6ea5e8SDavid Xu {
22935bb59edSMateusz Guzik 	lwpid_t tid;
230ec6ea5e8SDavid Xu 
231755341dfSMateusz Guzik 	mtx_assert(&tid_lock, MA_OWNED);
23235bb59edSMateusz Guzik 	KASSERT(rtid >= NO_PID,
23335bb59edSMateusz Guzik 	    ("%s: invalid tid %d\n", __func__, rtid));
23435bb59edSMateusz Guzik 	tid = rtid - NO_PID;
23535bb59edSMateusz Guzik 	KASSERT(bit_test(tid_bitmap, tid) != 0,
23635bb59edSMateusz Guzik 	    ("thread ID %d not allocated\n", rtid));
23735bb59edSMateusz Guzik 	bit_clear(tid_bitmap, tid);
238755341dfSMateusz Guzik }
239755341dfSMateusz Guzik 
240755341dfSMateusz Guzik static void
241755341dfSMateusz Guzik tid_free(lwpid_t rtid)
242755341dfSMateusz Guzik {
243755341dfSMateusz Guzik 
244755341dfSMateusz Guzik 	mtx_lock(&tid_lock);
245755341dfSMateusz Guzik 	tid_free_locked(rtid);
246755341dfSMateusz Guzik 	mtx_unlock(&tid_lock);
247755341dfSMateusz Guzik }
248755341dfSMateusz Guzik 
249755341dfSMateusz Guzik static void
250755341dfSMateusz Guzik tid_free_batch(lwpid_t *batch, int n)
251755341dfSMateusz Guzik {
252755341dfSMateusz Guzik 	int i;
253755341dfSMateusz Guzik 
254755341dfSMateusz Guzik 	mtx_lock(&tid_lock);
255755341dfSMateusz Guzik 	for (i = 0; i < n; i++) {
256755341dfSMateusz Guzik 		tid_free_locked(batch[i]);
257755341dfSMateusz Guzik 	}
258ec6ea5e8SDavid Xu 	mtx_unlock(&tid_lock);
259ec6ea5e8SDavid Xu }
260ec6ea5e8SDavid Xu 
261fdcac928SMarcel Moolenaar /*
2625ef7b7a0SMateusz Guzik  * Batching for thread reapping.
2635ef7b7a0SMateusz Guzik  */
2645ef7b7a0SMateusz Guzik struct tidbatch {
2655ef7b7a0SMateusz Guzik 	lwpid_t tab[16];
2665ef7b7a0SMateusz Guzik 	int n;
2675ef7b7a0SMateusz Guzik };
2685ef7b7a0SMateusz Guzik 
2695ef7b7a0SMateusz Guzik static void
2705ef7b7a0SMateusz Guzik tidbatch_prep(struct tidbatch *tb)
2715ef7b7a0SMateusz Guzik {
2725ef7b7a0SMateusz Guzik 
2735ef7b7a0SMateusz Guzik 	tb->n = 0;
2745ef7b7a0SMateusz Guzik }
2755ef7b7a0SMateusz Guzik 
2765ef7b7a0SMateusz Guzik static void
2775ef7b7a0SMateusz Guzik tidbatch_add(struct tidbatch *tb, struct thread *td)
2785ef7b7a0SMateusz Guzik {
2795ef7b7a0SMateusz Guzik 
2805ef7b7a0SMateusz Guzik 	KASSERT(tb->n < nitems(tb->tab),
2815ef7b7a0SMateusz Guzik 	    ("%s: count too high %d", __func__, tb->n));
2825ef7b7a0SMateusz Guzik 	tb->tab[tb->n] = td->td_tid;
2835ef7b7a0SMateusz Guzik 	tb->n++;
2845ef7b7a0SMateusz Guzik }
2855ef7b7a0SMateusz Guzik 
2865ef7b7a0SMateusz Guzik static void
2875ef7b7a0SMateusz Guzik tidbatch_process(struct tidbatch *tb)
2885ef7b7a0SMateusz Guzik {
2895ef7b7a0SMateusz Guzik 
2905ef7b7a0SMateusz Guzik 	KASSERT(tb->n <= nitems(tb->tab),
2915ef7b7a0SMateusz Guzik 	    ("%s: count too high %d", __func__, tb->n));
2925ef7b7a0SMateusz Guzik 	if (tb->n == nitems(tb->tab)) {
2935ef7b7a0SMateusz Guzik 		tid_free_batch(tb->tab, tb->n);
2945ef7b7a0SMateusz Guzik 		tb->n = 0;
2955ef7b7a0SMateusz Guzik 	}
2965ef7b7a0SMateusz Guzik }
2975ef7b7a0SMateusz Guzik 
2985ef7b7a0SMateusz Guzik static void
2995ef7b7a0SMateusz Guzik tidbatch_final(struct tidbatch *tb)
3005ef7b7a0SMateusz Guzik {
3015ef7b7a0SMateusz Guzik 
3025ef7b7a0SMateusz Guzik 	KASSERT(tb->n <= nitems(tb->tab),
3035ef7b7a0SMateusz Guzik 	    ("%s: count too high %d", __func__, tb->n));
3045ef7b7a0SMateusz Guzik 	if (tb->n != 0) {
3055ef7b7a0SMateusz Guzik 		tid_free_batch(tb->tab, tb->n);
3065ef7b7a0SMateusz Guzik 	}
3075ef7b7a0SMateusz Guzik }
3085ef7b7a0SMateusz Guzik 
3095ef7b7a0SMateusz Guzik /*
310696058c3SJulian Elischer  * Prepare a thread for use.
31144990b8cSJulian Elischer  */
312b23f72e9SBrian Feldman static int
313b23f72e9SBrian Feldman thread_ctor(void *mem, int size, void *arg, int flags)
31444990b8cSJulian Elischer {
31544990b8cSJulian Elischer 	struct thread	*td;
31644990b8cSJulian Elischer 
31744990b8cSJulian Elischer 	td = (struct thread *)mem;
31871fad9fdSJulian Elischer 	td->td_state = TDS_INACTIVE;
31994dd54b9SKonstantin Belousov 	td->td_lastcpu = td->td_oncpu = NOCPU;
3206c27c603SJuli Mallett 
3216c27c603SJuli Mallett 	/*
3226c27c603SJuli Mallett 	 * Note that td_critnest begins life as 1 because the thread is not
3236c27c603SJuli Mallett 	 * running and is thereby implicitly waiting to be on the receiving
324a54e85fdSJeff Roberson 	 * end of a context switch.
3256c27c603SJuli Mallett 	 */
326139b7550SJohn Baldwin 	td->td_critnest = 1;
327acbe332aSDavid Xu 	td->td_lend_user_pri = PRI_MAX;
328911b84b0SRobert Watson #ifdef AUDIT
329911b84b0SRobert Watson 	audit_thread_alloc(td);
330911b84b0SRobert Watson #endif
331d10183d9SDavid Xu 	umtx_thread_alloc(td);
33219d3e47dSMateusz Guzik 	MPASS(td->td_sel == NULL);
333b23f72e9SBrian Feldman 	return (0);
33444990b8cSJulian Elischer }
33544990b8cSJulian Elischer 
33644990b8cSJulian Elischer /*
33744990b8cSJulian Elischer  * Reclaim a thread after use.
33844990b8cSJulian Elischer  */
33944990b8cSJulian Elischer static void
34044990b8cSJulian Elischer thread_dtor(void *mem, int size, void *arg)
34144990b8cSJulian Elischer {
34244990b8cSJulian Elischer 	struct thread *td;
34344990b8cSJulian Elischer 
34444990b8cSJulian Elischer 	td = (struct thread *)mem;
34544990b8cSJulian Elischer 
34644990b8cSJulian Elischer #ifdef INVARIANTS
34744990b8cSJulian Elischer 	/* Verify that this thread is in a safe state to free. */
34844990b8cSJulian Elischer 	switch (td->td_state) {
34971fad9fdSJulian Elischer 	case TDS_INHIBITED:
35071fad9fdSJulian Elischer 	case TDS_RUNNING:
35171fad9fdSJulian Elischer 	case TDS_CAN_RUN:
35244990b8cSJulian Elischer 	case TDS_RUNQ:
35344990b8cSJulian Elischer 		/*
35444990b8cSJulian Elischer 		 * We must never unlink a thread that is in one of
35544990b8cSJulian Elischer 		 * these states, because it is currently active.
35644990b8cSJulian Elischer 		 */
35744990b8cSJulian Elischer 		panic("bad state for thread unlinking");
35844990b8cSJulian Elischer 		/* NOTREACHED */
35971fad9fdSJulian Elischer 	case TDS_INACTIVE:
36044990b8cSJulian Elischer 		break;
36144990b8cSJulian Elischer 	default:
36244990b8cSJulian Elischer 		panic("bad thread state");
36344990b8cSJulian Elischer 		/* NOTREACHED */
36444990b8cSJulian Elischer 	}
36544990b8cSJulian Elischer #endif
3666e8525ceSRobert Watson #ifdef AUDIT
3676e8525ceSRobert Watson 	audit_thread_free(td);
3686e8525ceSRobert Watson #endif
3691ba4a712SPawel Jakub Dawidek 	/* Free all OSD associated to this thread. */
3701ba4a712SPawel Jakub Dawidek 	osd_thread_exit(td);
371aca4bb91SKonstantin Belousov 	td_softdep_cleanup(td);
372aca4bb91SKonstantin Belousov 	MPASS(td->td_su == NULL);
37319d3e47dSMateusz Guzik 	seltdfini(td);
37444990b8cSJulian Elischer }
37544990b8cSJulian Elischer 
37644990b8cSJulian Elischer /*
37744990b8cSJulian Elischer  * Initialize type-stable parts of a thread (when newly created).
37844990b8cSJulian Elischer  */
379b23f72e9SBrian Feldman static int
380b23f72e9SBrian Feldman thread_init(void *mem, int size, int flags)
38144990b8cSJulian Elischer {
38244990b8cSJulian Elischer 	struct thread *td;
38344990b8cSJulian Elischer 
38444990b8cSJulian Elischer 	td = (struct thread *)mem;
385247aba24SMarcel Moolenaar 
38644f3b092SJohn Baldwin 	td->td_sleepqueue = sleepq_alloc();
387961a7b24SJohn Baldwin 	td->td_turnstile = turnstile_alloc();
3888f0e9130SKonstantin Belousov 	td->td_rlqe = NULL;
3892ca45184SMatt Joras 	EVENTHANDLER_DIRECT_INVOKE(thread_init, td);
390d10183d9SDavid Xu 	umtx_thread_init(td);
39189b57fcfSKonstantin Belousov 	td->td_kstack = 0;
392ad8b1d85SKonstantin Belousov 	td->td_sel = NULL;
393b23f72e9SBrian Feldman 	return (0);
39444990b8cSJulian Elischer }
39544990b8cSJulian Elischer 
39644990b8cSJulian Elischer /*
39744990b8cSJulian Elischer  * Tear down type-stable parts of a thread (just before being discarded).
39844990b8cSJulian Elischer  */
39944990b8cSJulian Elischer static void
40044990b8cSJulian Elischer thread_fini(void *mem, int size)
40144990b8cSJulian Elischer {
40244990b8cSJulian Elischer 	struct thread *td;
40344990b8cSJulian Elischer 
40444990b8cSJulian Elischer 	td = (struct thread *)mem;
4052ca45184SMatt Joras 	EVENTHANDLER_DIRECT_INVOKE(thread_fini, td);
4068f0e9130SKonstantin Belousov 	rlqentry_free(td->td_rlqe);
407961a7b24SJohn Baldwin 	turnstile_free(td->td_turnstile);
40844f3b092SJohn Baldwin 	sleepq_free(td->td_sleepqueue);
409d10183d9SDavid Xu 	umtx_thread_fini(td);
41019d3e47dSMateusz Guzik 	MPASS(td->td_sel == NULL);
41144990b8cSJulian Elischer }
4125215b187SJeff Roberson 
4135c8329edSJulian Elischer /*
4145215b187SJeff Roberson  * For a newly created process,
4155215b187SJeff Roberson  * link up all the structures and its initial threads etc.
416ed062c8dSJulian Elischer  * called from:
417e7d939bdSMarcel Moolenaar  * {arch}/{arch}/machdep.c   {arch}_init(), init386() etc.
418ed062c8dSJulian Elischer  * proc_dtor() (should go away)
419ed062c8dSJulian Elischer  * proc_init()
4205c8329edSJulian Elischer  */
4215c8329edSJulian Elischer void
42289b57fcfSKonstantin Belousov proc_linkup0(struct proc *p, struct thread *td)
42389b57fcfSKonstantin Belousov {
42489b57fcfSKonstantin Belousov 	TAILQ_INIT(&p->p_threads);	     /* all threads in proc */
42589b57fcfSKonstantin Belousov 	proc_linkup(p, td);
42689b57fcfSKonstantin Belousov }
42789b57fcfSKonstantin Belousov 
42889b57fcfSKonstantin Belousov void
4298460a577SJohn Birrell proc_linkup(struct proc *p, struct thread *td)
4305c8329edSJulian Elischer {
431a54e85fdSJeff Roberson 
4329104847fSDavid Xu 	sigqueue_init(&p->p_sigqueue, p);
433ebceaf6dSDavid Xu 	p->p_ksi = ksiginfo_alloc(1);
434ebceaf6dSDavid Xu 	if (p->p_ksi != NULL) {
4355c474517SDavid Xu 		/* XXX p_ksi may be null if ksiginfo zone is not ready */
436ebceaf6dSDavid Xu 		p->p_ksi->ksi_flags = KSI_EXT | KSI_INS;
437ebceaf6dSDavid Xu 	}
438b2f92ef9SDavid Xu 	LIST_INIT(&p->p_mqnotifier);
4395c8329edSJulian Elischer 	p->p_numthreads = 0;
4408460a577SJohn Birrell 	thread_link(td, p);
4415c8329edSJulian Elischer }
4425c8329edSJulian Elischer 
4431bd3cf5dSMateusz Guzik extern int max_threads_per_proc;
4441bd3cf5dSMateusz Guzik 
4455c8329edSJulian Elischer /*
44644990b8cSJulian Elischer  * Initialize global thread allocation resources.
44744990b8cSJulian Elischer  */
44844990b8cSJulian Elischer void
44944990b8cSJulian Elischer threadinit(void)
45044990b8cSJulian Elischer {
45126007fe3SMateusz Guzik 	u_long i;
452cf31cadeSMateusz Guzik 	lwpid_t tid0;
4535aa5420fSMark Johnston 	uint32_t flags;
45444990b8cSJulian Elischer 
4551bd3cf5dSMateusz Guzik 	/*
4561bd3cf5dSMateusz Guzik 	 * Place an upper limit on threads which can be allocated.
4571bd3cf5dSMateusz Guzik 	 *
4581bd3cf5dSMateusz Guzik 	 * Note that other factors may make the de facto limit much lower.
4591bd3cf5dSMateusz Guzik 	 *
4601bd3cf5dSMateusz Guzik 	 * Platform limits are somewhat arbitrary but deemed "more than good
4611bd3cf5dSMateusz Guzik 	 * enough" for the foreseable future.
4621bd3cf5dSMateusz Guzik 	 */
4631bd3cf5dSMateusz Guzik 	if (maxthread == 0) {
4641bd3cf5dSMateusz Guzik #ifdef _LP64
4651bd3cf5dSMateusz Guzik 		maxthread = MIN(maxproc * max_threads_per_proc, 1000000);
4661bd3cf5dSMateusz Guzik #else
4671bd3cf5dSMateusz Guzik 		maxthread = MIN(maxproc * max_threads_per_proc, 100000);
4681bd3cf5dSMateusz Guzik #endif
4691bd3cf5dSMateusz Guzik 	}
4701bd3cf5dSMateusz Guzik 
4711ea7a6f8SPoul-Henning Kamp 	mtx_init(&tid_lock, "TID lock", NULL, MTX_DEF);
47235bb59edSMateusz Guzik 	tid_bitmap = bit_alloc(maxthread, M_TIDHASH, M_WAITOK);
47362dbc992SMateusz Guzik 	/*
47462dbc992SMateusz Guzik 	 * Handle thread0.
47562dbc992SMateusz Guzik 	 */
47662dbc992SMateusz Guzik 	thread_count_inc();
477cf31cadeSMateusz Guzik 	tid0 = tid_alloc();
478cf31cadeSMateusz Guzik 	if (tid0 != THREAD0_TID)
479cf31cadeSMateusz Guzik 		panic("tid0 %d != %d\n", tid0, THREAD0_TID);
4801ea7a6f8SPoul-Henning Kamp 
4815aa5420fSMark Johnston 	flags = UMA_ZONE_NOFREE;
4825aa5420fSMark Johnston #ifdef __aarch64__
4835aa5420fSMark Johnston 	/*
4845aa5420fSMark Johnston 	 * Force thread structures to be allocated from the direct map.
4855aa5420fSMark Johnston 	 * Otherwise, superpage promotions and demotions may temporarily
4865aa5420fSMark Johnston 	 * invalidate thread structure mappings.  For most dynamically allocated
4875aa5420fSMark Johnston 	 * structures this is not a problem, but translation faults cannot be
4885aa5420fSMark Johnston 	 * handled without accessing curthread.
4895aa5420fSMark Johnston 	 */
4905aa5420fSMark Johnston 	flags |= UMA_ZONE_CONTIG;
4915aa5420fSMark Johnston #endif
492de028f5aSJeff Roberson 	thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(),
49344990b8cSJulian Elischer 	    thread_ctor, thread_dtor, thread_init, thread_fini,
4945aa5420fSMark Johnston 	    32 - 1, flags);
495cf7d9a8cSDavid Xu 	tidhashtbl = hashinit(maxproc / 2, M_TIDHASH, &tidhash);
49626007fe3SMateusz Guzik 	tidhashlock = (tidhash + 1) / 64;
49726007fe3SMateusz Guzik 	if (tidhashlock > 0)
49826007fe3SMateusz Guzik 		tidhashlock--;
49926007fe3SMateusz Guzik 	tidhashtbl_lock = malloc(sizeof(*tidhashtbl_lock) * (tidhashlock + 1),
50026007fe3SMateusz Guzik 	    M_TIDHASH, M_WAITOK | M_ZERO);
50126007fe3SMateusz Guzik 	for (i = 0; i < tidhashlock + 1; i++)
50226007fe3SMateusz Guzik 		rw_init(&tidhashtbl_lock[i], "tidhash");
50344990b8cSJulian Elischer }
50444990b8cSJulian Elischer 
50544990b8cSJulian Elischer /*
506ff8fbcffSJeff Roberson  * Place an unused thread on the zombie list.
50744990b8cSJulian Elischer  */
50844990b8cSJulian Elischer void
509ff8fbcffSJeff Roberson thread_zombie(struct thread *td)
51044990b8cSJulian Elischer {
511c5315f51SMateusz Guzik 	struct thread *ztd;
512c5315f51SMateusz Guzik 
513c5315f51SMateusz Guzik 	ztd = atomic_load_ptr(&thread_zombies);
514c5315f51SMateusz Guzik 	for (;;) {
515c5315f51SMateusz Guzik 		td->td_zombie = ztd;
516c5315f51SMateusz Guzik 		if (atomic_fcmpset_rel_ptr((uintptr_t *)&thread_zombies,
517c5315f51SMateusz Guzik 		    (uintptr_t *)&ztd, (uintptr_t)td))
518c5315f51SMateusz Guzik 			break;
519c5315f51SMateusz Guzik 		continue;
520c5315f51SMateusz Guzik 	}
52144990b8cSJulian Elischer }
52244990b8cSJulian Elischer 
5235c8329edSJulian Elischer /*
524ff8fbcffSJeff Roberson  * Release a thread that has exited after cpu_throw().
525ff8fbcffSJeff Roberson  */
526ff8fbcffSJeff Roberson void
527ff8fbcffSJeff Roberson thread_stash(struct thread *td)
528ff8fbcffSJeff Roberson {
529ff8fbcffSJeff Roberson 	atomic_subtract_rel_int(&td->td_proc->p_exitthreads, 1);
530ff8fbcffSJeff Roberson 	thread_zombie(td);
531ff8fbcffSJeff Roberson }
532ff8fbcffSJeff Roberson 
533ff8fbcffSJeff Roberson /*
534c5315f51SMateusz Guzik  * Reap zombie threads.
53544990b8cSJulian Elischer  */
53644990b8cSJulian Elischer void
53744990b8cSJulian Elischer thread_reap(void)
53844990b8cSJulian Elischer {
539c5315f51SMateusz Guzik 	struct thread *itd, *ntd;
5405ef7b7a0SMateusz Guzik 	struct tidbatch tidbatch;
541f34a2f56SMateusz Guzik 	struct credbatch credbatch;
5425ef7b7a0SMateusz Guzik 	int tdcount;
543fb8ab680SMateusz Guzik 	struct plimit *lim;
544fb8ab680SMateusz Guzik 	int limcount;
54544990b8cSJulian Elischer 
54644990b8cSJulian Elischer 	/*
547c5315f51SMateusz Guzik 	 * Reading upfront is pessimal if followed by concurrent atomic_swap,
548c5315f51SMateusz Guzik 	 * but most of the time the list is empty.
54944990b8cSJulian Elischer 	 */
550c5315f51SMateusz Guzik 	if (thread_zombies == NULL)
551c5315f51SMateusz Guzik 		return;
552c5315f51SMateusz Guzik 
553c5315f51SMateusz Guzik 	itd = (struct thread *)atomic_swap_ptr((uintptr_t *)&thread_zombies,
554c5315f51SMateusz Guzik 	    (uintptr_t)NULL);
5555ef7b7a0SMateusz Guzik 	if (itd == NULL)
5565ef7b7a0SMateusz Guzik 		return;
5575ef7b7a0SMateusz Guzik 
5585ef7b7a0SMateusz Guzik 	tidbatch_prep(&tidbatch);
559f34a2f56SMateusz Guzik 	credbatch_prep(&credbatch);
5605ef7b7a0SMateusz Guzik 	tdcount = 0;
561fb8ab680SMateusz Guzik 	lim = NULL;
562fb8ab680SMateusz Guzik 	limcount = 0;
563c5315f51SMateusz Guzik 	while (itd != NULL) {
564c5315f51SMateusz Guzik 		ntd = itd->td_zombie;
5655ef7b7a0SMateusz Guzik 		EVENTHANDLER_DIRECT_INVOKE(thread_dtor, itd);
5665ef7b7a0SMateusz Guzik 		tidbatch_add(&tidbatch, itd);
567f34a2f56SMateusz Guzik 		credbatch_add(&credbatch, itd);
568fb8ab680SMateusz Guzik 		MPASS(itd->td_limit != NULL);
569fb8ab680SMateusz Guzik 		if (lim != itd->td_limit) {
570fb8ab680SMateusz Guzik 			if (limcount != 0) {
571fb8ab680SMateusz Guzik 				lim_freen(lim, limcount);
572fb8ab680SMateusz Guzik 				limcount = 0;
573fb8ab680SMateusz Guzik 			}
574fb8ab680SMateusz Guzik 		}
575fb8ab680SMateusz Guzik 		lim = itd->td_limit;
576fb8ab680SMateusz Guzik 		limcount++;
577755341dfSMateusz Guzik 		thread_free_batched(itd);
5785ef7b7a0SMateusz Guzik 		tidbatch_process(&tidbatch);
579f34a2f56SMateusz Guzik 		credbatch_process(&credbatch);
5805ef7b7a0SMateusz Guzik 		tdcount++;
5815ef7b7a0SMateusz Guzik 		if (tdcount == 32) {
5825ef7b7a0SMateusz Guzik 			thread_count_sub(tdcount);
5835ef7b7a0SMateusz Guzik 			tdcount = 0;
584755341dfSMateusz Guzik 		}
585c5315f51SMateusz Guzik 		itd = ntd;
58644990b8cSJulian Elischer 	}
587755341dfSMateusz Guzik 
5885ef7b7a0SMateusz Guzik 	tidbatch_final(&tidbatch);
589f34a2f56SMateusz Guzik 	credbatch_final(&credbatch);
5905ef7b7a0SMateusz Guzik 	if (tdcount != 0) {
5915ef7b7a0SMateusz Guzik 		thread_count_sub(tdcount);
592755341dfSMateusz Guzik 	}
593fb8ab680SMateusz Guzik 	MPASS(limcount != 0);
594fb8ab680SMateusz Guzik 	lim_freen(lim, limcount);
595ed062c8dSJulian Elischer }
59644990b8cSJulian Elischer 
5974f0db5e0SJulian Elischer /*
59844990b8cSJulian Elischer  * Allocate a thread.
59944990b8cSJulian Elischer  */
60044990b8cSJulian Elischer struct thread *
6018a945d10SKonstantin Belousov thread_alloc(int pages)
60244990b8cSJulian Elischer {
60389b57fcfSKonstantin Belousov 	struct thread *td;
6041bd3cf5dSMateusz Guzik 	lwpid_t tid;
6058460a577SJohn Birrell 
60662dbc992SMateusz Guzik 	if (!thread_count_inc()) {
6071bd3cf5dSMateusz Guzik 		return (NULL);
6081bd3cf5dSMateusz Guzik 	}
6091bd3cf5dSMateusz Guzik 
61062dbc992SMateusz Guzik 	tid = tid_alloc();
6111bd3cf5dSMateusz Guzik 	td = uma_zalloc(thread_zone, M_WAITOK);
61289b57fcfSKonstantin Belousov 	KASSERT(td->td_kstack == 0, ("thread_alloc got thread with kstack"));
6138a945d10SKonstantin Belousov 	if (!vm_thread_new(td, pages)) {
61489b57fcfSKonstantin Belousov 		uma_zfree(thread_zone, td);
6151bd3cf5dSMateusz Guzik 		tid_free(tid);
61662dbc992SMateusz Guzik 		thread_count_dec();
61789b57fcfSKonstantin Belousov 		return (NULL);
61889b57fcfSKonstantin Belousov 	}
6191bd3cf5dSMateusz Guzik 	td->td_tid = tid;
6200c3967e7SMarcel Moolenaar 	cpu_thread_alloc(td);
6211bd3cf5dSMateusz Guzik 	EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td);
62289b57fcfSKonstantin Belousov 	return (td);
62344990b8cSJulian Elischer }
62444990b8cSJulian Elischer 
6258a945d10SKonstantin Belousov int
6268a945d10SKonstantin Belousov thread_alloc_stack(struct thread *td, int pages)
6278a945d10SKonstantin Belousov {
6288a945d10SKonstantin Belousov 
6298a945d10SKonstantin Belousov 	KASSERT(td->td_kstack == 0,
6308a945d10SKonstantin Belousov 	    ("thread_alloc_stack called on a thread with kstack"));
6318a945d10SKonstantin Belousov 	if (!vm_thread_new(td, pages))
6328a945d10SKonstantin Belousov 		return (0);
6338a945d10SKonstantin Belousov 	cpu_thread_alloc(td);
6348a945d10SKonstantin Belousov 	return (1);
6358a945d10SKonstantin Belousov }
6364f0db5e0SJulian Elischer 
6374f0db5e0SJulian Elischer /*
63844990b8cSJulian Elischer  * Deallocate a thread.
63944990b8cSJulian Elischer  */
640755341dfSMateusz Guzik static void
641755341dfSMateusz Guzik thread_free_batched(struct thread *td)
64244990b8cSJulian Elischer {
6432e6b8de4SJeff Roberson 
6442e6b8de4SJeff Roberson 	lock_profile_thread_exit(td);
64545aea8deSJeff Roberson 	if (td->td_cpuset)
646d7f687fcSJeff Roberson 		cpuset_rel(td->td_cpuset);
647d7f687fcSJeff Roberson 	td->td_cpuset = NULL;
6480c3967e7SMarcel Moolenaar 	cpu_thread_free(td);
64989b57fcfSKonstantin Belousov 	if (td->td_kstack != 0)
65089b57fcfSKonstantin Belousov 		vm_thread_dispose(td);
6512d19b736SKonstantin Belousov 	callout_drain(&td->td_slpcallout);
652755341dfSMateusz Guzik 	/*
653755341dfSMateusz Guzik 	 * Freeing handled by the caller.
654755341dfSMateusz Guzik 	 */
6551bd3cf5dSMateusz Guzik 	td->td_tid = -1;
65644990b8cSJulian Elischer 	uma_zfree(thread_zone, td);
65744990b8cSJulian Elischer }
65844990b8cSJulian Elischer 
6594ea6a9a2SMateusz Guzik void
660755341dfSMateusz Guzik thread_free(struct thread *td)
661755341dfSMateusz Guzik {
662755341dfSMateusz Guzik 	lwpid_t tid;
663755341dfSMateusz Guzik 
6645ef7b7a0SMateusz Guzik 	EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td);
665755341dfSMateusz Guzik 	tid = td->td_tid;
666755341dfSMateusz Guzik 	thread_free_batched(td);
667755341dfSMateusz Guzik 	tid_free(tid);
66862dbc992SMateusz Guzik 	thread_count_dec();
669755341dfSMateusz Guzik }
670755341dfSMateusz Guzik 
671755341dfSMateusz Guzik void
6724ea6a9a2SMateusz Guzik thread_cow_get_proc(struct thread *newtd, struct proc *p)
6734ea6a9a2SMateusz Guzik {
6744ea6a9a2SMateusz Guzik 
6754ea6a9a2SMateusz Guzik 	PROC_LOCK_ASSERT(p, MA_OWNED);
6761724c563SMateusz Guzik 	newtd->td_realucred = crcowget(p->p_ucred);
6771724c563SMateusz Guzik 	newtd->td_ucred = newtd->td_realucred;
678f6f6d240SMateusz Guzik 	newtd->td_limit = lim_hold(p->p_limit);
6794ea6a9a2SMateusz Guzik 	newtd->td_cowgen = p->p_cowgen;
6804ea6a9a2SMateusz Guzik }
6814ea6a9a2SMateusz Guzik 
6824ea6a9a2SMateusz Guzik void
6834ea6a9a2SMateusz Guzik thread_cow_get(struct thread *newtd, struct thread *td)
6844ea6a9a2SMateusz Guzik {
6854ea6a9a2SMateusz Guzik 
6861724c563SMateusz Guzik 	MPASS(td->td_realucred == td->td_ucred);
6871724c563SMateusz Guzik 	newtd->td_realucred = crcowget(td->td_realucred);
6881724c563SMateusz Guzik 	newtd->td_ucred = newtd->td_realucred;
689f6f6d240SMateusz Guzik 	newtd->td_limit = lim_hold(td->td_limit);
6904ea6a9a2SMateusz Guzik 	newtd->td_cowgen = td->td_cowgen;
6914ea6a9a2SMateusz Guzik }
6924ea6a9a2SMateusz Guzik 
6934ea6a9a2SMateusz Guzik void
6944ea6a9a2SMateusz Guzik thread_cow_free(struct thread *td)
6954ea6a9a2SMateusz Guzik {
6964ea6a9a2SMateusz Guzik 
6971724c563SMateusz Guzik 	if (td->td_realucred != NULL)
6981724c563SMateusz Guzik 		crcowfree(td);
699cd672ca6SMateusz Guzik 	if (td->td_limit != NULL)
700f6f6d240SMateusz Guzik 		lim_free(td->td_limit);
7014ea6a9a2SMateusz Guzik }
7024ea6a9a2SMateusz Guzik 
7034ea6a9a2SMateusz Guzik void
7044ea6a9a2SMateusz Guzik thread_cow_update(struct thread *td)
7054ea6a9a2SMateusz Guzik {
7064ea6a9a2SMateusz Guzik 	struct proc *p;
707cd672ca6SMateusz Guzik 	struct ucred *oldcred;
708cd672ca6SMateusz Guzik 	struct plimit *oldlimit;
7094ea6a9a2SMateusz Guzik 
7104ea6a9a2SMateusz Guzik 	p = td->td_proc;
711cd672ca6SMateusz Guzik 	oldlimit = NULL;
7124ea6a9a2SMateusz Guzik 	PROC_LOCK(p);
7131724c563SMateusz Guzik 	oldcred = crcowsync();
714cd672ca6SMateusz Guzik 	if (td->td_limit != p->p_limit) {
715cd672ca6SMateusz Guzik 		oldlimit = td->td_limit;
716cd672ca6SMateusz Guzik 		td->td_limit = lim_hold(p->p_limit);
717cd672ca6SMateusz Guzik 	}
7184ea6a9a2SMateusz Guzik 	td->td_cowgen = p->p_cowgen;
7194ea6a9a2SMateusz Guzik 	PROC_UNLOCK(p);
720cd672ca6SMateusz Guzik 	if (oldcred != NULL)
721cd672ca6SMateusz Guzik 		crfree(oldcred);
722cd672ca6SMateusz Guzik 	if (oldlimit != NULL)
723cd672ca6SMateusz Guzik 		lim_free(oldlimit);
7244ea6a9a2SMateusz Guzik }
7254ea6a9a2SMateusz Guzik 
72644990b8cSJulian Elischer /*
72744990b8cSJulian Elischer  * Discard the current thread and exit from its context.
72894e0a4cdSJulian Elischer  * Always called with scheduler locked.
72944990b8cSJulian Elischer  *
73044990b8cSJulian Elischer  * Because we can't free a thread while we're operating under its context,
731696058c3SJulian Elischer  * push the current thread into our CPU's deadthread holder. This means
732696058c3SJulian Elischer  * we needn't worry about someone else grabbing our context before we
7336617724cSJeff Roberson  * do a cpu_throw().
73444990b8cSJulian Elischer  */
73544990b8cSJulian Elischer void
73644990b8cSJulian Elischer thread_exit(void)
73744990b8cSJulian Elischer {
7387e3a96eaSJohn Baldwin 	uint64_t runtime, new_switchtime;
73944990b8cSJulian Elischer 	struct thread *td;
7401c4bcd05SJeff Roberson 	struct thread *td2;
74144990b8cSJulian Elischer 	struct proc *p;
7427847a9daSJohn Baldwin 	int wakeup_swapper;
74344990b8cSJulian Elischer 
74444990b8cSJulian Elischer 	td = curthread;
74544990b8cSJulian Elischer 	p = td->td_proc;
74644990b8cSJulian Elischer 
747a54e85fdSJeff Roberson 	PROC_SLOCK_ASSERT(p, MA_OWNED);
748ed062c8dSJulian Elischer 	mtx_assert(&Giant, MA_NOTOWNED);
749a54e85fdSJeff Roberson 
75044990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
751ed062c8dSJulian Elischer 	KASSERT(p != NULL, ("thread exiting without a process"));
752cc701b73SRobert Watson 	CTR3(KTR_PROC, "thread_exit: thread %p (pid %ld, %s)", td,
753e01eafefSJulian Elischer 	    (long)p->p_pid, td->td_name);
7546c9271a9SAndriy Gapon 	SDT_PROBE0(proc, , , lwp__exit);
7559104847fSDavid Xu 	KASSERT(TAILQ_EMPTY(&td->td_sigqueue.sq_list), ("signal pending"));
756936c24faSMateusz Guzik 	MPASS(td->td_realucred == td->td_ucred);
75744990b8cSJulian Elischer 
758ed062c8dSJulian Elischer 	/*
759ed062c8dSJulian Elischer 	 * drop FPU & debug register state storage, or any other
760ed062c8dSJulian Elischer 	 * architecture specific resources that
761ed062c8dSJulian Elischer 	 * would not be on a new untouched process.
762ed062c8dSJulian Elischer 	 */
763bd07998eSKonstantin Belousov 	cpu_thread_exit(td);
76444990b8cSJulian Elischer 
765ed062c8dSJulian Elischer 	/*
7661faf202eSJulian Elischer 	 * The last thread is left attached to the process
7671faf202eSJulian Elischer 	 * So that the whole bundle gets recycled. Skip
768ed062c8dSJulian Elischer 	 * all this stuff if we never had threads.
769ed062c8dSJulian Elischer 	 * EXIT clears all sign of other threads when
770ed062c8dSJulian Elischer 	 * it goes to single threading, so the last thread always
771ed062c8dSJulian Elischer 	 * takes the short path.
7721faf202eSJulian Elischer 	 */
773ed062c8dSJulian Elischer 	if (p->p_flag & P_HADTHREADS) {
7741faf202eSJulian Elischer 		if (p->p_numthreads > 1) {
775fd229b5bSKonstantin Belousov 			atomic_add_int(&td->td_proc->p_exitthreads, 1);
776d3a0bd78SJulian Elischer 			thread_unlink(td);
7771c4bcd05SJeff Roberson 			td2 = FIRST_THREAD_IN_PROC(p);
7781c4bcd05SJeff Roberson 			sched_exit_thread(td2, td);
779ed062c8dSJulian Elischer 
780ed062c8dSJulian Elischer 			/*
78144990b8cSJulian Elischer 			 * The test below is NOT true if we are the
7829182554aSKonstantin Belousov 			 * sole exiting thread. P_STOPPED_SINGLE is unset
78344990b8cSJulian Elischer 			 * in exit1() after it is the only survivor.
78444990b8cSJulian Elischer 			 */
7851279572aSDavid Xu 			if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
78644990b8cSJulian Elischer 				if (p->p_numthreads == p->p_suspcount) {
787a54e85fdSJeff Roberson 					thread_lock(p->p_singlethread);
7887847a9daSJohn Baldwin 					wakeup_swapper = thread_unsuspend_one(
78984cdea97SKonstantin Belousov 						p->p_singlethread, p, false);
7907847a9daSJohn Baldwin 					if (wakeup_swapper)
7917847a9daSJohn Baldwin 						kick_proc0();
79244990b8cSJulian Elischer 				}
79344990b8cSJulian Elischer 			}
79448bfcdddSJulian Elischer 
795696058c3SJulian Elischer 			PCPU_SET(deadthread, td);
7961faf202eSJulian Elischer 		} else {
797ed062c8dSJulian Elischer 			/*
798ed062c8dSJulian Elischer 			 * The last thread is exiting.. but not through exit()
799ed062c8dSJulian Elischer 			 */
800ed062c8dSJulian Elischer 			panic ("thread_exit: Last thread exiting on its own");
801ed062c8dSJulian Elischer 		}
8021faf202eSJulian Elischer 	}
80316d95d4fSJoseph Koshy #ifdef	HWPMC_HOOKS
80416d95d4fSJoseph Koshy 	/*
80516d95d4fSJoseph Koshy 	 * If this thread is part of a process that is being tracked by hwpmc(4),
80616d95d4fSJoseph Koshy 	 * inform the module of the thread's impending exit.
80716d95d4fSJoseph Koshy 	 */
8086161b98cSMatt Macy 	if (PMC_PROC_IS_USING_PMCS(td->td_proc)) {
80916d95d4fSJoseph Koshy 		PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT);
8106161b98cSMatt Macy 		PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_EXIT, NULL);
811ebfaf69cSMatt Macy 	} else if (PMC_SYSTEM_SAMPLING_ACTIVE())
812ebfaf69cSMatt Macy 		PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_EXIT_LOG, NULL);
81316d95d4fSJoseph Koshy #endif
814a54e85fdSJeff Roberson 	PROC_UNLOCK(p);
8155c7bebf9SKonstantin Belousov 	PROC_STATLOCK(p);
8165c7bebf9SKonstantin Belousov 	thread_lock(td);
8175c7bebf9SKonstantin Belousov 	PROC_SUNLOCK(p);
8187e3a96eaSJohn Baldwin 
8197e3a96eaSJohn Baldwin 	/* Do the same timestamp bookkeeping that mi_switch() would do. */
8207e3a96eaSJohn Baldwin 	new_switchtime = cpu_ticks();
8217e3a96eaSJohn Baldwin 	runtime = new_switchtime - PCPU_GET(switchtime);
8227e3a96eaSJohn Baldwin 	td->td_runtime += runtime;
8237e3a96eaSJohn Baldwin 	td->td_incruntime += runtime;
8247e3a96eaSJohn Baldwin 	PCPU_SET(switchtime, new_switchtime);
8257e3a96eaSJohn Baldwin 	PCPU_SET(switchticks, ticks);
82683c9dea1SGleb Smirnoff 	VM_CNT_INC(v_swtch);
8277e3a96eaSJohn Baldwin 
8287e3a96eaSJohn Baldwin 	/* Save our resource usage in our process. */
8297e3a96eaSJohn Baldwin 	td->td_ru.ru_nvcsw++;
83061a74c5cSJeff Roberson 	ruxagg_locked(p, td);
8317e3a96eaSJohn Baldwin 	rucollect(&p->p_ru, &td->td_ru);
8325c7bebf9SKonstantin Belousov 	PROC_STATUNLOCK(p);
8337e3a96eaSJohn Baldwin 
834dcc9954eSJulian Elischer 	td->td_state = TDS_INACTIVE;
8353d06b4b3SAttilio Rao #ifdef WITNESS
8363d06b4b3SAttilio Rao 	witness_thread_exit(td);
8373d06b4b3SAttilio Rao #endif
838732d9528SJulian Elischer 	CTR1(KTR_PROC, "thread_exit: cpu_throw() thread %p", td);
839a54e85fdSJeff Roberson 	sched_throw(td);
840cc66ebe2SPeter Wemm 	panic("I'm a teapot!");
84144990b8cSJulian Elischer 	/* NOTREACHED */
84244990b8cSJulian Elischer }
84344990b8cSJulian Elischer 
84444990b8cSJulian Elischer /*
845696058c3SJulian Elischer  * Do any thread specific cleanups that may be needed in wait()
84637814395SPeter Wemm  * called with Giant, proc and schedlock not held.
847696058c3SJulian Elischer  */
848696058c3SJulian Elischer void
849696058c3SJulian Elischer thread_wait(struct proc *p)
850696058c3SJulian Elischer {
851696058c3SJulian Elischer 	struct thread *td;
852696058c3SJulian Elischer 
85337814395SPeter Wemm 	mtx_assert(&Giant, MA_NOTOWNED);
854624bf9e1SKonstantin Belousov 	KASSERT(p->p_numthreads == 1, ("multiple threads in thread_wait()"));
855624bf9e1SKonstantin Belousov 	KASSERT(p->p_exitthreads == 0, ("p_exitthreads leaking"));
856ff8fbcffSJeff Roberson 	td = FIRST_THREAD_IN_PROC(p);
857ff8fbcffSJeff Roberson 	/* Lock the last thread so we spin until it exits cpu_throw(). */
858ff8fbcffSJeff Roberson 	thread_lock(td);
859ff8fbcffSJeff Roberson 	thread_unlock(td);
8602e6b8de4SJeff Roberson 	lock_profile_thread_exit(td);
861d7f687fcSJeff Roberson 	cpuset_rel(td->td_cpuset);
862d7f687fcSJeff Roberson 	td->td_cpuset = NULL;
863696058c3SJulian Elischer 	cpu_thread_clean(td);
8644ea6a9a2SMateusz Guzik 	thread_cow_free(td);
8652d19b736SKonstantin Belousov 	callout_drain(&td->td_slpcallout);
866696058c3SJulian Elischer 	thread_reap();	/* check for zombie threads etc. */
867696058c3SJulian Elischer }
868696058c3SJulian Elischer 
869696058c3SJulian Elischer /*
87044990b8cSJulian Elischer  * Link a thread to a process.
8711faf202eSJulian Elischer  * set up anything that needs to be initialized for it to
8721faf202eSJulian Elischer  * be used by the process.
87344990b8cSJulian Elischer  */
87444990b8cSJulian Elischer void
8758460a577SJohn Birrell thread_link(struct thread *td, struct proc *p)
87644990b8cSJulian Elischer {
87744990b8cSJulian Elischer 
878a54e85fdSJeff Roberson 	/*
879a54e85fdSJeff Roberson 	 * XXX This can't be enabled because it's called for proc0 before
880374ae2a3SJeff Roberson 	 * its lock has been created.
881374ae2a3SJeff Roberson 	 * PROC_LOCK_ASSERT(p, MA_OWNED);
882a54e85fdSJeff Roberson 	 */
88371fad9fdSJulian Elischer 	td->td_state    = TDS_INACTIVE;
88444990b8cSJulian Elischer 	td->td_proc     = p;
885b61ce5b0SJeff Roberson 	td->td_flags    = TDF_INMEM;
88644990b8cSJulian Elischer 
8871faf202eSJulian Elischer 	LIST_INIT(&td->td_contested);
888eea4f254SJeff Roberson 	LIST_INIT(&td->td_lprof[0]);
889eea4f254SJeff Roberson 	LIST_INIT(&td->td_lprof[1]);
890f6eccf96SGleb Smirnoff #ifdef EPOCH_TRACE
891dd902d01SGleb Smirnoff 	SLIST_INIT(&td->td_epochs);
892f6eccf96SGleb Smirnoff #endif
8939104847fSDavid Xu 	sigqueue_init(&td->td_sigqueue, p);
894fd90e2edSJung-uk Kim 	callout_init(&td->td_slpcallout, 1);
89566d8df9dSDaniel Eischen 	TAILQ_INSERT_TAIL(&p->p_threads, td, td_plist);
89644990b8cSJulian Elischer 	p->p_numthreads++;
89744990b8cSJulian Elischer }
89844990b8cSJulian Elischer 
899ed062c8dSJulian Elischer /*
900ed062c8dSJulian Elischer  * Called from:
901ed062c8dSJulian Elischer  *  thread_exit()
902ed062c8dSJulian Elischer  */
903d3a0bd78SJulian Elischer void
904d3a0bd78SJulian Elischer thread_unlink(struct thread *td)
905d3a0bd78SJulian Elischer {
906d3a0bd78SJulian Elischer 	struct proc *p = td->td_proc;
907d3a0bd78SJulian Elischer 
908374ae2a3SJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
909f6eccf96SGleb Smirnoff #ifdef EPOCH_TRACE
910dd902d01SGleb Smirnoff 	MPASS(SLIST_EMPTY(&td->td_epochs));
911f6eccf96SGleb Smirnoff #endif
912dd902d01SGleb Smirnoff 
913d3a0bd78SJulian Elischer 	TAILQ_REMOVE(&p->p_threads, td, td_plist);
914d3a0bd78SJulian Elischer 	p->p_numthreads--;
915d3a0bd78SJulian Elischer 	/* could clear a few other things here */
9168460a577SJohn Birrell 	/* Must  NOT clear links to proc! */
9175c8329edSJulian Elischer }
9185c8329edSJulian Elischer 
91979799053SKonstantin Belousov static int
92079799053SKonstantin Belousov calc_remaining(struct proc *p, int mode)
92179799053SKonstantin Belousov {
92279799053SKonstantin Belousov 	int remaining;
92379799053SKonstantin Belousov 
9247b519077SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
9257b519077SKonstantin Belousov 	PROC_SLOCK_ASSERT(p, MA_OWNED);
92679799053SKonstantin Belousov 	if (mode == SINGLE_EXIT)
92779799053SKonstantin Belousov 		remaining = p->p_numthreads;
92879799053SKonstantin Belousov 	else if (mode == SINGLE_BOUNDARY)
92979799053SKonstantin Belousov 		remaining = p->p_numthreads - p->p_boundary_count;
9306ddcc233SKonstantin Belousov 	else if (mode == SINGLE_NO_EXIT || mode == SINGLE_ALLPROC)
93179799053SKonstantin Belousov 		remaining = p->p_numthreads - p->p_suspcount;
93279799053SKonstantin Belousov 	else
93379799053SKonstantin Belousov 		panic("calc_remaining: wrong mode %d", mode);
93479799053SKonstantin Belousov 	return (remaining);
93579799053SKonstantin Belousov }
93679799053SKonstantin Belousov 
93707a9368aSKonstantin Belousov static int
93807a9368aSKonstantin Belousov remain_for_mode(int mode)
93907a9368aSKonstantin Belousov {
94007a9368aSKonstantin Belousov 
9416ddcc233SKonstantin Belousov 	return (mode == SINGLE_ALLPROC ? 0 : 1);
94207a9368aSKonstantin Belousov }
94307a9368aSKonstantin Belousov 
94407a9368aSKonstantin Belousov static int
94507a9368aSKonstantin Belousov weed_inhib(int mode, struct thread *td2, struct proc *p)
94607a9368aSKonstantin Belousov {
94707a9368aSKonstantin Belousov 	int wakeup_swapper;
94807a9368aSKonstantin Belousov 
94907a9368aSKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
95007a9368aSKonstantin Belousov 	PROC_SLOCK_ASSERT(p, MA_OWNED);
95107a9368aSKonstantin Belousov 	THREAD_LOCK_ASSERT(td2, MA_OWNED);
95207a9368aSKonstantin Belousov 
95307a9368aSKonstantin Belousov 	wakeup_swapper = 0;
95461a74c5cSJeff Roberson 
95561a74c5cSJeff Roberson 	/*
95661a74c5cSJeff Roberson 	 * Since the thread lock is dropped by the scheduler we have
95761a74c5cSJeff Roberson 	 * to retry to check for races.
95861a74c5cSJeff Roberson 	 */
95961a74c5cSJeff Roberson restart:
96007a9368aSKonstantin Belousov 	switch (mode) {
96107a9368aSKonstantin Belousov 	case SINGLE_EXIT:
96261a74c5cSJeff Roberson 		if (TD_IS_SUSPENDED(td2)) {
96384cdea97SKonstantin Belousov 			wakeup_swapper |= thread_unsuspend_one(td2, p, true);
96461a74c5cSJeff Roberson 			thread_lock(td2);
96561a74c5cSJeff Roberson 			goto restart;
96661a74c5cSJeff Roberson 		}
96761a74c5cSJeff Roberson 		if (TD_CAN_ABORT(td2)) {
96807a9368aSKonstantin Belousov 			wakeup_swapper |= sleepq_abort(td2, EINTR);
96961a74c5cSJeff Roberson 			return (wakeup_swapper);
97061a74c5cSJeff Roberson 		}
97107a9368aSKonstantin Belousov 		break;
97207a9368aSKonstantin Belousov 	case SINGLE_BOUNDARY:
97307a9368aSKonstantin Belousov 	case SINGLE_NO_EXIT:
97461a74c5cSJeff Roberson 		if (TD_IS_SUSPENDED(td2) &&
97561a74c5cSJeff Roberson 		    (td2->td_flags & TDF_BOUNDARY) == 0) {
97684cdea97SKonstantin Belousov 			wakeup_swapper |= thread_unsuspend_one(td2, p, false);
97761a74c5cSJeff Roberson 			thread_lock(td2);
97861a74c5cSJeff Roberson 			goto restart;
97961a74c5cSJeff Roberson 		}
98061a74c5cSJeff Roberson 		if (TD_CAN_ABORT(td2)) {
98107a9368aSKonstantin Belousov 			wakeup_swapper |= sleepq_abort(td2, ERESTART);
98261a74c5cSJeff Roberson 			return (wakeup_swapper);
98361a74c5cSJeff Roberson 		}
984917dd390SKonstantin Belousov 		break;
9856ddcc233SKonstantin Belousov 	case SINGLE_ALLPROC:
9866ddcc233SKonstantin Belousov 		/*
9876ddcc233SKonstantin Belousov 		 * ALLPROC suspend tries to avoid spurious EINTR for
9886ddcc233SKonstantin Belousov 		 * threads sleeping interruptable, by suspending the
9896ddcc233SKonstantin Belousov 		 * thread directly, similarly to sig_suspend_threads().
9906ddcc233SKonstantin Belousov 		 * Since such sleep is not performed at the user
9916ddcc233SKonstantin Belousov 		 * boundary, TDF_BOUNDARY flag is not set, and TDF_ALLPROCSUSP
9926ddcc233SKonstantin Belousov 		 * is used to avoid immediate un-suspend.
9936ddcc233SKonstantin Belousov 		 */
9946ddcc233SKonstantin Belousov 		if (TD_IS_SUSPENDED(td2) && (td2->td_flags & (TDF_BOUNDARY |
99561a74c5cSJeff Roberson 		    TDF_ALLPROCSUSP)) == 0) {
99684cdea97SKonstantin Belousov 			wakeup_swapper |= thread_unsuspend_one(td2, p, false);
99761a74c5cSJeff Roberson 			thread_lock(td2);
99861a74c5cSJeff Roberson 			goto restart;
99961a74c5cSJeff Roberson 		}
100061a74c5cSJeff Roberson 		if (TD_CAN_ABORT(td2)) {
10016ddcc233SKonstantin Belousov 			if ((td2->td_flags & TDF_SBDRY) == 0) {
10026ddcc233SKonstantin Belousov 				thread_suspend_one(td2);
10036ddcc233SKonstantin Belousov 				td2->td_flags |= TDF_ALLPROCSUSP;
10046ddcc233SKonstantin Belousov 			} else {
10056ddcc233SKonstantin Belousov 				wakeup_swapper |= sleepq_abort(td2, ERESTART);
100661a74c5cSJeff Roberson 				return (wakeup_swapper);
10076ddcc233SKonstantin Belousov 			}
10086ddcc233SKonstantin Belousov 		}
100907a9368aSKonstantin Belousov 		break;
101061a74c5cSJeff Roberson 	default:
101161a74c5cSJeff Roberson 		break;
101207a9368aSKonstantin Belousov 	}
101361a74c5cSJeff Roberson 	thread_unlock(td2);
101407a9368aSKonstantin Belousov 	return (wakeup_swapper);
101507a9368aSKonstantin Belousov }
101607a9368aSKonstantin Belousov 
10175215b187SJeff Roberson /*
101844990b8cSJulian Elischer  * Enforce single-threading.
101944990b8cSJulian Elischer  *
102044990b8cSJulian Elischer  * Returns 1 if the caller must abort (another thread is waiting to
102144990b8cSJulian Elischer  * exit the process or similar). Process is locked!
102244990b8cSJulian Elischer  * Returns 0 when you are successfully the only thread running.
102344990b8cSJulian Elischer  * A process has successfully single threaded in the suspend mode when
102444990b8cSJulian Elischer  * There are no threads in user mode. Threads in the kernel must be
102544990b8cSJulian Elischer  * allowed to continue until they get to the user boundary. They may even
102644990b8cSJulian Elischer  * copy out their return values and data before suspending. They may however be
1027e2668f55SMaxim Konovalov  * accelerated in reaching the user boundary as we will wake up
102844990b8cSJulian Elischer  * any sleeping threads that are interruptable. (PCATCH).
102944990b8cSJulian Elischer  */
103044990b8cSJulian Elischer int
10316ddcc233SKonstantin Belousov thread_single(struct proc *p, int mode)
103244990b8cSJulian Elischer {
103344990b8cSJulian Elischer 	struct thread *td;
103444990b8cSJulian Elischer 	struct thread *td2;
1035da7bbd2cSJohn Baldwin 	int remaining, wakeup_swapper;
103644990b8cSJulian Elischer 
103744990b8cSJulian Elischer 	td = curthread;
10386ddcc233SKonstantin Belousov 	KASSERT(mode == SINGLE_EXIT || mode == SINGLE_BOUNDARY ||
10396ddcc233SKonstantin Belousov 	    mode == SINGLE_ALLPROC || mode == SINGLE_NO_EXIT,
10406ddcc233SKonstantin Belousov 	    ("invalid mode %d", mode));
10416ddcc233SKonstantin Belousov 	/*
10426ddcc233SKonstantin Belousov 	 * If allowing non-ALLPROC singlethreading for non-curproc
10436ddcc233SKonstantin Belousov 	 * callers, calc_remaining() and remain_for_mode() should be
10446ddcc233SKonstantin Belousov 	 * adjusted to also account for td->td_proc != p.  For now
10456ddcc233SKonstantin Belousov 	 * this is not implemented because it is not used.
10466ddcc233SKonstantin Belousov 	 */
10476ddcc233SKonstantin Belousov 	KASSERT((mode == SINGLE_ALLPROC && td->td_proc != p) ||
10486ddcc233SKonstantin Belousov 	    (mode != SINGLE_ALLPROC && td->td_proc == p),
10496ddcc233SKonstantin Belousov 	    ("mode %d proc %p curproc %p", mode, p, td->td_proc));
105037814395SPeter Wemm 	mtx_assert(&Giant, MA_NOTOWNED);
105144990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
105244990b8cSJulian Elischer 
10536ddcc233SKonstantin Belousov 	if ((p->p_flag & P_HADTHREADS) == 0 && mode != SINGLE_ALLPROC)
105444990b8cSJulian Elischer 		return (0);
105544990b8cSJulian Elischer 
1056e3b9bf71SJulian Elischer 	/* Is someone already single threading? */
1057906ac69dSDavid Xu 	if (p->p_singlethread != NULL && p->p_singlethread != td)
105844990b8cSJulian Elischer 		return (1);
105944990b8cSJulian Elischer 
1060906ac69dSDavid Xu 	if (mode == SINGLE_EXIT) {
1061906ac69dSDavid Xu 		p->p_flag |= P_SINGLE_EXIT;
1062906ac69dSDavid Xu 		p->p_flag &= ~P_SINGLE_BOUNDARY;
1063906ac69dSDavid Xu 	} else {
1064906ac69dSDavid Xu 		p->p_flag &= ~P_SINGLE_EXIT;
1065906ac69dSDavid Xu 		if (mode == SINGLE_BOUNDARY)
1066906ac69dSDavid Xu 			p->p_flag |= P_SINGLE_BOUNDARY;
1067906ac69dSDavid Xu 		else
1068906ac69dSDavid Xu 			p->p_flag &= ~P_SINGLE_BOUNDARY;
1069906ac69dSDavid Xu 	}
10706ddcc233SKonstantin Belousov 	if (mode == SINGLE_ALLPROC)
10716ddcc233SKonstantin Belousov 		p->p_flag |= P_TOTAL_STOP;
10721279572aSDavid Xu 	p->p_flag |= P_STOPPED_SINGLE;
10737b4a950aSDavid Xu 	PROC_SLOCK(p);
1074112afcb2SJohn Baldwin 	p->p_singlethread = td;
107579799053SKonstantin Belousov 	remaining = calc_remaining(p, mode);
107607a9368aSKonstantin Belousov 	while (remaining != remain_for_mode(mode)) {
1077bf1a3220SDavid Xu 		if (P_SHOULDSTOP(p) != P_STOPPED_SINGLE)
1078bf1a3220SDavid Xu 			goto stopme;
1079da7bbd2cSJohn Baldwin 		wakeup_swapper = 0;
108044990b8cSJulian Elischer 		FOREACH_THREAD_IN_PROC(p, td2) {
108144990b8cSJulian Elischer 			if (td2 == td)
108244990b8cSJulian Elischer 				continue;
1083a54e85fdSJeff Roberson 			thread_lock(td2);
1084b7edba77SJeff Roberson 			td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
10856ddcc233SKonstantin Belousov 			if (TD_IS_INHIBITED(td2)) {
108607a9368aSKonstantin Belousov 				wakeup_swapper |= weed_inhib(mode, td2, p);
1087d8267df7SDavid Xu #ifdef SMP
10886ddcc233SKonstantin Belousov 			} else if (TD_IS_RUNNING(td2) && td != td2) {
1089d8267df7SDavid Xu 				forward_signal(td2);
109061a74c5cSJeff Roberson 				thread_unlock(td2);
1091d8267df7SDavid Xu #endif
109261a74c5cSJeff Roberson 			} else
1093a54e85fdSJeff Roberson 				thread_unlock(td2);
10949d102777SJulian Elischer 		}
1095da7bbd2cSJohn Baldwin 		if (wakeup_swapper)
1096da7bbd2cSJohn Baldwin 			kick_proc0();
109779799053SKonstantin Belousov 		remaining = calc_remaining(p, mode);
1098ec008e96SDavid Xu 
10999d102777SJulian Elischer 		/*
11009d102777SJulian Elischer 		 * Maybe we suspended some threads.. was it enough?
11019d102777SJulian Elischer 		 */
110207a9368aSKonstantin Belousov 		if (remaining == remain_for_mode(mode))
11039d102777SJulian Elischer 			break;
11049d102777SJulian Elischer 
1105bf1a3220SDavid Xu stopme:
110644990b8cSJulian Elischer 		/*
110744990b8cSJulian Elischer 		 * Wake us up when everyone else has suspended.
1108e3b9bf71SJulian Elischer 		 * In the mean time we suspend as well.
110944990b8cSJulian Elischer 		 */
11106ddcc233SKonstantin Belousov 		thread_suspend_switch(td, p);
111179799053SKonstantin Belousov 		remaining = calc_remaining(p, mode);
111244990b8cSJulian Elischer 	}
1113906ac69dSDavid Xu 	if (mode == SINGLE_EXIT) {
111491599697SJulian Elischer 		/*
11158626a0ddSKonstantin Belousov 		 * Convert the process to an unthreaded process.  The
11168626a0ddSKonstantin Belousov 		 * SINGLE_EXIT is called by exit1() or execve(), in
11178626a0ddSKonstantin Belousov 		 * both cases other threads must be retired.
111891599697SJulian Elischer 		 */
11198626a0ddSKonstantin Belousov 		KASSERT(p->p_numthreads == 1, ("Unthreading with >1 threads"));
1120ed062c8dSJulian Elischer 		p->p_singlethread = NULL;
11218626a0ddSKonstantin Belousov 		p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_HADTHREADS);
1122fd229b5bSKonstantin Belousov 
1123fd229b5bSKonstantin Belousov 		/*
1124fd229b5bSKonstantin Belousov 		 * Wait for any remaining threads to exit cpu_throw().
1125fd229b5bSKonstantin Belousov 		 */
1126fd229b5bSKonstantin Belousov 		while (p->p_exitthreads != 0) {
1127fd229b5bSKonstantin Belousov 			PROC_SUNLOCK(p);
1128fd229b5bSKonstantin Belousov 			PROC_UNLOCK(p);
1129fd229b5bSKonstantin Belousov 			sched_relinquish(td);
1130fd229b5bSKonstantin Belousov 			PROC_LOCK(p);
1131fd229b5bSKonstantin Belousov 			PROC_SLOCK(p);
1132fd229b5bSKonstantin Belousov 		}
1133ac437c07SKonstantin Belousov 	} else if (mode == SINGLE_BOUNDARY) {
1134ac437c07SKonstantin Belousov 		/*
1135ac437c07SKonstantin Belousov 		 * Wait until all suspended threads are removed from
1136ac437c07SKonstantin Belousov 		 * the processors.  The thread_suspend_check()
1137ac437c07SKonstantin Belousov 		 * increments p_boundary_count while it is still
1138ac437c07SKonstantin Belousov 		 * running, which makes it possible for the execve()
1139ac437c07SKonstantin Belousov 		 * to destroy vmspace while our other threads are
1140ac437c07SKonstantin Belousov 		 * still using the address space.
1141ac437c07SKonstantin Belousov 		 *
1142ac437c07SKonstantin Belousov 		 * We lock the thread, which is only allowed to
1143ac437c07SKonstantin Belousov 		 * succeed after context switch code finished using
1144ac437c07SKonstantin Belousov 		 * the address space.
1145ac437c07SKonstantin Belousov 		 */
1146ac437c07SKonstantin Belousov 		FOREACH_THREAD_IN_PROC(p, td2) {
1147ac437c07SKonstantin Belousov 			if (td2 == td)
1148ac437c07SKonstantin Belousov 				continue;
1149ac437c07SKonstantin Belousov 			thread_lock(td2);
1150ac437c07SKonstantin Belousov 			KASSERT((td2->td_flags & TDF_BOUNDARY) != 0,
1151ac437c07SKonstantin Belousov 			    ("td %p not on boundary", td2));
1152ac437c07SKonstantin Belousov 			KASSERT(TD_IS_SUSPENDED(td2),
1153ac437c07SKonstantin Belousov 			    ("td %p is not suspended", td2));
1154ac437c07SKonstantin Belousov 			thread_unlock(td2);
1155ac437c07SKonstantin Belousov 		}
115691599697SJulian Elischer 	}
11577b4a950aSDavid Xu 	PROC_SUNLOCK(p);
115844990b8cSJulian Elischer 	return (0);
115944990b8cSJulian Elischer }
116044990b8cSJulian Elischer 
11618638fe7bSKonstantin Belousov bool
11628638fe7bSKonstantin Belousov thread_suspend_check_needed(void)
11638638fe7bSKonstantin Belousov {
11648638fe7bSKonstantin Belousov 	struct proc *p;
11658638fe7bSKonstantin Belousov 	struct thread *td;
11668638fe7bSKonstantin Belousov 
11678638fe7bSKonstantin Belousov 	td = curthread;
11688638fe7bSKonstantin Belousov 	p = td->td_proc;
11698638fe7bSKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
11708638fe7bSKonstantin Belousov 	return (P_SHOULDSTOP(p) || ((p->p_flag & P_TRACED) != 0 &&
11718638fe7bSKonstantin Belousov 	    (td->td_dbgflags & TDB_SUSPEND) != 0));
11728638fe7bSKonstantin Belousov }
11738638fe7bSKonstantin Belousov 
117444990b8cSJulian Elischer /*
117544990b8cSJulian Elischer  * Called in from locations that can safely check to see
117644990b8cSJulian Elischer  * whether we have to suspend or at least throttle for a
117744990b8cSJulian Elischer  * single-thread event (e.g. fork).
117844990b8cSJulian Elischer  *
117944990b8cSJulian Elischer  * Such locations include userret().
118044990b8cSJulian Elischer  * If the "return_instead" argument is non zero, the thread must be able to
118144990b8cSJulian Elischer  * accept 0 (caller may continue), or 1 (caller must abort) as a result.
118244990b8cSJulian Elischer  *
118344990b8cSJulian Elischer  * The 'return_instead' argument tells the function if it may do a
118444990b8cSJulian Elischer  * thread_exit() or suspend, or whether the caller must abort and back
118544990b8cSJulian Elischer  * out instead.
118644990b8cSJulian Elischer  *
118744990b8cSJulian Elischer  * If the thread that set the single_threading request has set the
118844990b8cSJulian Elischer  * P_SINGLE_EXIT bit in the process flags then this call will never return
118944990b8cSJulian Elischer  * if 'return_instead' is false, but will exit.
119044990b8cSJulian Elischer  *
119144990b8cSJulian Elischer  * P_SINGLE_EXIT | return_instead == 0| return_instead != 0
119244990b8cSJulian Elischer  *---------------+--------------------+---------------------
119344990b8cSJulian Elischer  *       0       | returns 0          |   returns 0 or 1
1194353374b5SJohn Baldwin  *               | when ST ends       |   immediately
119544990b8cSJulian Elischer  *---------------+--------------------+---------------------
119644990b8cSJulian Elischer  *       1       | thread exits       |   returns 1
1197353374b5SJohn Baldwin  *               |                    |  immediately
119844990b8cSJulian Elischer  * 0 = thread_exit() or suspension ok,
119944990b8cSJulian Elischer  * other = return error instead of stopping the thread.
120044990b8cSJulian Elischer  *
120144990b8cSJulian Elischer  * While a full suspension is under effect, even a single threading
120244990b8cSJulian Elischer  * thread would be suspended if it made this call (but it shouldn't).
120344990b8cSJulian Elischer  * This call should only be made from places where
120444990b8cSJulian Elischer  * thread_exit() would be safe as that may be the outcome unless
120544990b8cSJulian Elischer  * return_instead is set.
120644990b8cSJulian Elischer  */
120744990b8cSJulian Elischer int
120844990b8cSJulian Elischer thread_suspend_check(int return_instead)
120944990b8cSJulian Elischer {
1210ecafb24bSJuli Mallett 	struct thread *td;
1211ecafb24bSJuli Mallett 	struct proc *p;
121246e47c4fSKonstantin Belousov 	int wakeup_swapper;
121344990b8cSJulian Elischer 
121444990b8cSJulian Elischer 	td = curthread;
121544990b8cSJulian Elischer 	p = td->td_proc;
121637814395SPeter Wemm 	mtx_assert(&Giant, MA_NOTOWNED);
121744990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
12188638fe7bSKonstantin Belousov 	while (thread_suspend_check_needed()) {
12191279572aSDavid Xu 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
122044990b8cSJulian Elischer 			KASSERT(p->p_singlethread != NULL,
122144990b8cSJulian Elischer 			    ("singlethread not set"));
122244990b8cSJulian Elischer 			/*
1223e3b9bf71SJulian Elischer 			 * The only suspension in action is a
1224e3b9bf71SJulian Elischer 			 * single-threading. Single threader need not stop.
1225bd07998eSKonstantin Belousov 			 * It is safe to access p->p_singlethread unlocked
1226bd07998eSKonstantin Belousov 			 * because it can only be set to our address by us.
122744990b8cSJulian Elischer 			 */
1228e3b9bf71SJulian Elischer 			if (p->p_singlethread == td)
122944990b8cSJulian Elischer 				return (0);	/* Exempt from stopping. */
123044990b8cSJulian Elischer 		}
123145a4bfa1SDavid Xu 		if ((p->p_flag & P_SINGLE_EXIT) && return_instead)
123294f0972bSDavid Xu 			return (EINTR);
123344990b8cSJulian Elischer 
1234906ac69dSDavid Xu 		/* Should we goto user boundary if we didn't come from there? */
1235906ac69dSDavid Xu 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE &&
1236906ac69dSDavid Xu 		    (p->p_flag & P_SINGLE_BOUNDARY) && return_instead)
123794f0972bSDavid Xu 			return (ERESTART);
1238906ac69dSDavid Xu 
123944990b8cSJulian Elischer 		/*
12403077f938SKonstantin Belousov 		 * Ignore suspend requests if they are deferred.
1241d071a6faSJohn Baldwin 		 */
12423077f938SKonstantin Belousov 		if ((td->td_flags & TDF_SBDRY) != 0) {
1243d071a6faSJohn Baldwin 			KASSERT(return_instead,
1244d071a6faSJohn Baldwin 			    ("TDF_SBDRY set for unsafe thread_suspend_check"));
124546e47c4fSKonstantin Belousov 			KASSERT((td->td_flags & (TDF_SEINTR | TDF_SERESTART)) !=
124646e47c4fSKonstantin Belousov 			    (TDF_SEINTR | TDF_SERESTART),
124746e47c4fSKonstantin Belousov 			    ("both TDF_SEINTR and TDF_SERESTART"));
124846e47c4fSKonstantin Belousov 			return (TD_SBDRY_INTR(td) ? TD_SBDRY_ERRNO(td) : 0);
1249d071a6faSJohn Baldwin 		}
1250d071a6faSJohn Baldwin 
1251d071a6faSJohn Baldwin 		/*
125244990b8cSJulian Elischer 		 * If the process is waiting for us to exit,
125344990b8cSJulian Elischer 		 * this thread should just suicide.
12541279572aSDavid Xu 		 * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE.
125544990b8cSJulian Elischer 		 */
1256cf7d9a8cSDavid Xu 		if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) {
1257cf7d9a8cSDavid Xu 			PROC_UNLOCK(p);
125891d1786fSDmitry Chagin 
125991d1786fSDmitry Chagin 			/*
126091d1786fSDmitry Chagin 			 * Allow Linux emulation layer to do some work
126191d1786fSDmitry Chagin 			 * before thread suicide.
126291d1786fSDmitry Chagin 			 */
126391d1786fSDmitry Chagin 			if (__predict_false(p->p_sysent->sv_thread_detach != NULL))
126491d1786fSDmitry Chagin 				(p->p_sysent->sv_thread_detach)(td);
12652a339d9eSKonstantin Belousov 			umtx_thread_exit(td);
1266d1e7a4a5SJohn Baldwin 			kern_thr_exit(td);
1267d1e7a4a5SJohn Baldwin 			panic("stopped thread did not exit");
1268cf7d9a8cSDavid Xu 		}
126921ecd1e9SDavid Xu 
127021ecd1e9SDavid Xu 		PROC_SLOCK(p);
127121ecd1e9SDavid Xu 		thread_stopped(p);
1272a54e85fdSJeff Roberson 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
1273a54e85fdSJeff Roberson 			if (p->p_numthreads == p->p_suspcount + 1) {
1274a54e85fdSJeff Roberson 				thread_lock(p->p_singlethread);
127584cdea97SKonstantin Belousov 				wakeup_swapper = thread_unsuspend_one(
127684cdea97SKonstantin Belousov 				    p->p_singlethread, p, false);
12777847a9daSJohn Baldwin 				if (wakeup_swapper)
12787847a9daSJohn Baldwin 					kick_proc0();
1279a54e85fdSJeff Roberson 			}
1280a54e85fdSJeff Roberson 		}
12813f9be10eSDavid Xu 		PROC_UNLOCK(p);
12827b4a950aSDavid Xu 		thread_lock(td);
128344990b8cSJulian Elischer 		/*
128444990b8cSJulian Elischer 		 * When a thread suspends, it just
1285ad1e7d28SJulian Elischer 		 * gets taken off all queues.
128644990b8cSJulian Elischer 		 */
128771fad9fdSJulian Elischer 		thread_suspend_one(td);
1288906ac69dSDavid Xu 		if (return_instead == 0) {
1289906ac69dSDavid Xu 			p->p_boundary_count++;
1290906ac69dSDavid Xu 			td->td_flags |= TDF_BOUNDARY;
1291cf19bf91SJulian Elischer 		}
12927b4a950aSDavid Xu 		PROC_SUNLOCK(p);
1293686bcb5cSJeff Roberson 		mi_switch(SW_INVOL | SWT_SUSPEND);
129444990b8cSJulian Elischer 		PROC_LOCK(p);
129544990b8cSJulian Elischer 	}
129644990b8cSJulian Elischer 	return (0);
129744990b8cSJulian Elischer }
129844990b8cSJulian Elischer 
1299478ca4b0SKonstantin Belousov /*
1300478ca4b0SKonstantin Belousov  * Check for possible stops and suspensions while executing a
1301478ca4b0SKonstantin Belousov  * casueword or similar transiently failing operation.
1302478ca4b0SKonstantin Belousov  *
1303478ca4b0SKonstantin Belousov  * The sleep argument controls whether the function can handle a stop
1304478ca4b0SKonstantin Belousov  * request itself or it should return ERESTART and the request is
1305478ca4b0SKonstantin Belousov  * proceed at the kernel/user boundary in ast.
1306478ca4b0SKonstantin Belousov  *
1307478ca4b0SKonstantin Belousov  * Typically, when retrying due to casueword(9) failure (rv == 1), we
1308478ca4b0SKonstantin Belousov  * should handle the stop requests there, with exception of cases when
1309478ca4b0SKonstantin Belousov  * the thread owns a kernel resource, for instance busied the umtx
1310300b525dSKonstantin Belousov  * key, or when functions return immediately if thread_check_susp()
1311478ca4b0SKonstantin Belousov  * returned non-zero.  On the other hand, retrying the whole lock
1312478ca4b0SKonstantin Belousov  * operation, we better not stop there but delegate the handling to
1313478ca4b0SKonstantin Belousov  * ast.
1314478ca4b0SKonstantin Belousov  *
1315478ca4b0SKonstantin Belousov  * If the request is for thread termination P_SINGLE_EXIT, we cannot
1316478ca4b0SKonstantin Belousov  * handle it at all, and simply return EINTR.
1317478ca4b0SKonstantin Belousov  */
1318478ca4b0SKonstantin Belousov int
1319478ca4b0SKonstantin Belousov thread_check_susp(struct thread *td, bool sleep)
1320478ca4b0SKonstantin Belousov {
1321478ca4b0SKonstantin Belousov 	struct proc *p;
1322478ca4b0SKonstantin Belousov 	int error;
1323478ca4b0SKonstantin Belousov 
1324478ca4b0SKonstantin Belousov 	/*
1325478ca4b0SKonstantin Belousov 	 * The check for TDF_NEEDSUSPCHK is racy, but it is enough to
1326478ca4b0SKonstantin Belousov 	 * eventually break the lockstep loop.
1327478ca4b0SKonstantin Belousov 	 */
1328478ca4b0SKonstantin Belousov 	if ((td->td_flags & TDF_NEEDSUSPCHK) == 0)
1329478ca4b0SKonstantin Belousov 		return (0);
1330478ca4b0SKonstantin Belousov 	error = 0;
1331478ca4b0SKonstantin Belousov 	p = td->td_proc;
1332478ca4b0SKonstantin Belousov 	PROC_LOCK(p);
1333478ca4b0SKonstantin Belousov 	if (p->p_flag & P_SINGLE_EXIT)
1334478ca4b0SKonstantin Belousov 		error = EINTR;
1335478ca4b0SKonstantin Belousov 	else if (P_SHOULDSTOP(p) ||
1336478ca4b0SKonstantin Belousov 	    ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_SUSPEND)))
1337478ca4b0SKonstantin Belousov 		error = sleep ? thread_suspend_check(0) : ERESTART;
1338478ca4b0SKonstantin Belousov 	PROC_UNLOCK(p);
1339478ca4b0SKonstantin Belousov 	return (error);
1340478ca4b0SKonstantin Belousov }
1341478ca4b0SKonstantin Belousov 
134235c32a76SDavid Xu void
13436ddcc233SKonstantin Belousov thread_suspend_switch(struct thread *td, struct proc *p)
1344a54e85fdSJeff Roberson {
1345a54e85fdSJeff Roberson 
1346a54e85fdSJeff Roberson 	KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
1347a54e85fdSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
13487b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
1349a54e85fdSJeff Roberson 	/*
1350a54e85fdSJeff Roberson 	 * We implement thread_suspend_one in stages here to avoid
1351a54e85fdSJeff Roberson 	 * dropping the proc lock while the thread lock is owned.
1352a54e85fdSJeff Roberson 	 */
13536ddcc233SKonstantin Belousov 	if (p == td->td_proc) {
1354a54e85fdSJeff Roberson 		thread_stopped(p);
1355a54e85fdSJeff Roberson 		p->p_suspcount++;
13566ddcc233SKonstantin Belousov 	}
13573f9be10eSDavid Xu 	PROC_UNLOCK(p);
13587b4a950aSDavid Xu 	thread_lock(td);
1359b7edba77SJeff Roberson 	td->td_flags &= ~TDF_NEEDSUSPCHK;
1360a54e85fdSJeff Roberson 	TD_SET_SUSPENDED(td);
1361c5aa6b58SJeff Roberson 	sched_sleep(td, 0);
13627b4a950aSDavid Xu 	PROC_SUNLOCK(p);
1363a54e85fdSJeff Roberson 	DROP_GIANT();
1364686bcb5cSJeff Roberson 	mi_switch(SW_VOL | SWT_SUSPEND);
1365a54e85fdSJeff Roberson 	PICKUP_GIANT();
1366a54e85fdSJeff Roberson 	PROC_LOCK(p);
13677b4a950aSDavid Xu 	PROC_SLOCK(p);
1368a54e85fdSJeff Roberson }
1369a54e85fdSJeff Roberson 
1370a54e85fdSJeff Roberson void
137135c32a76SDavid Xu thread_suspend_one(struct thread *td)
137235c32a76SDavid Xu {
13736ddcc233SKonstantin Belousov 	struct proc *p;
137435c32a76SDavid Xu 
13756ddcc233SKonstantin Belousov 	p = td->td_proc;
13767b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
1377a54e85fdSJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1378e574e444SDavid Xu 	KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
137935c32a76SDavid Xu 	p->p_suspcount++;
1380b7edba77SJeff Roberson 	td->td_flags &= ~TDF_NEEDSUSPCHK;
138171fad9fdSJulian Elischer 	TD_SET_SUSPENDED(td);
1382c5aa6b58SJeff Roberson 	sched_sleep(td, 0);
138335c32a76SDavid Xu }
138435c32a76SDavid Xu 
138584cdea97SKonstantin Belousov static int
138684cdea97SKonstantin Belousov thread_unsuspend_one(struct thread *td, struct proc *p, bool boundary)
138735c32a76SDavid Xu {
138835c32a76SDavid Xu 
1389a54e85fdSJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1390ad1e7d28SJulian Elischer 	KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended"));
139171fad9fdSJulian Elischer 	TD_CLR_SUSPENDED(td);
13926ddcc233SKonstantin Belousov 	td->td_flags &= ~TDF_ALLPROCSUSP;
13936ddcc233SKonstantin Belousov 	if (td->td_proc == p) {
13946ddcc233SKonstantin Belousov 		PROC_SLOCK_ASSERT(p, MA_OWNED);
139535c32a76SDavid Xu 		p->p_suspcount--;
139684cdea97SKonstantin Belousov 		if (boundary && (td->td_flags & TDF_BOUNDARY) != 0) {
139784cdea97SKonstantin Belousov 			td->td_flags &= ~TDF_BOUNDARY;
139884cdea97SKonstantin Belousov 			p->p_boundary_count--;
139984cdea97SKonstantin Belousov 		}
14006ddcc233SKonstantin Belousov 	}
140161a74c5cSJeff Roberson 	return (setrunnable(td, 0));
140235c32a76SDavid Xu }
140335c32a76SDavid Xu 
140444990b8cSJulian Elischer /*
140544990b8cSJulian Elischer  * Allow all threads blocked by single threading to continue running.
140644990b8cSJulian Elischer  */
140744990b8cSJulian Elischer void
140844990b8cSJulian Elischer thread_unsuspend(struct proc *p)
140944990b8cSJulian Elischer {
141044990b8cSJulian Elischer 	struct thread *td;
14117847a9daSJohn Baldwin 	int wakeup_swapper;
141244990b8cSJulian Elischer 
141344990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
14147b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
14157847a9daSJohn Baldwin 	wakeup_swapper = 0;
141644990b8cSJulian Elischer 	if (!P_SHOULDSTOP(p)) {
1417ad1e7d28SJulian Elischer                 FOREACH_THREAD_IN_PROC(p, td) {
1418a54e85fdSJeff Roberson 			thread_lock(td);
1419ad1e7d28SJulian Elischer 			if (TD_IS_SUSPENDED(td)) {
142084cdea97SKonstantin Belousov 				wakeup_swapper |= thread_unsuspend_one(td, p,
142184cdea97SKonstantin Belousov 				    true);
142261a74c5cSJeff Roberson 			} else
1423a54e85fdSJeff Roberson 				thread_unlock(td);
1424ad1e7d28SJulian Elischer 		}
142584cdea97SKonstantin Belousov 	} else if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE &&
142684cdea97SKonstantin Belousov 	    p->p_numthreads == p->p_suspcount) {
142744990b8cSJulian Elischer 		/*
142844990b8cSJulian Elischer 		 * Stopping everything also did the job for the single
142944990b8cSJulian Elischer 		 * threading request. Now we've downgraded to single-threaded,
143044990b8cSJulian Elischer 		 * let it continue.
143144990b8cSJulian Elischer 		 */
14326ddcc233SKonstantin Belousov 		if (p->p_singlethread->td_proc == p) {
1433a54e85fdSJeff Roberson 			thread_lock(p->p_singlethread);
14346ddcc233SKonstantin Belousov 			wakeup_swapper = thread_unsuspend_one(
143584cdea97SKonstantin Belousov 			    p->p_singlethread, p, false);
143644990b8cSJulian Elischer 		}
14376ddcc233SKonstantin Belousov 	}
14387847a9daSJohn Baldwin 	if (wakeup_swapper)
14397847a9daSJohn Baldwin 		kick_proc0();
144044990b8cSJulian Elischer }
144144990b8cSJulian Elischer 
1442ed062c8dSJulian Elischer /*
1443ed062c8dSJulian Elischer  * End the single threading mode..
1444ed062c8dSJulian Elischer  */
144544990b8cSJulian Elischer void
14466ddcc233SKonstantin Belousov thread_single_end(struct proc *p, int mode)
144744990b8cSJulian Elischer {
144844990b8cSJulian Elischer 	struct thread *td;
14497847a9daSJohn Baldwin 	int wakeup_swapper;
145044990b8cSJulian Elischer 
14516ddcc233SKonstantin Belousov 	KASSERT(mode == SINGLE_EXIT || mode == SINGLE_BOUNDARY ||
14526ddcc233SKonstantin Belousov 	    mode == SINGLE_ALLPROC || mode == SINGLE_NO_EXIT,
14536ddcc233SKonstantin Belousov 	    ("invalid mode %d", mode));
145444990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
14556ddcc233SKonstantin Belousov 	KASSERT((mode == SINGLE_ALLPROC && (p->p_flag & P_TOTAL_STOP) != 0) ||
14566ddcc233SKonstantin Belousov 	    (mode != SINGLE_ALLPROC && (p->p_flag & P_TOTAL_STOP) == 0),
14576ddcc233SKonstantin Belousov 	    ("mode %d does not match P_TOTAL_STOP", mode));
145884cdea97SKonstantin Belousov 	KASSERT(mode == SINGLE_ALLPROC || p->p_singlethread == curthread,
145984cdea97SKonstantin Belousov 	    ("thread_single_end from other thread %p %p",
146084cdea97SKonstantin Belousov 	    curthread, p->p_singlethread));
146184cdea97SKonstantin Belousov 	KASSERT(mode != SINGLE_BOUNDARY ||
146284cdea97SKonstantin Belousov 	    (p->p_flag & P_SINGLE_BOUNDARY) != 0,
146384cdea97SKonstantin Belousov 	    ("mis-matched SINGLE_BOUNDARY flags %x", p->p_flag));
14646ddcc233SKonstantin Belousov 	p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_SINGLE_BOUNDARY |
14656ddcc233SKonstantin Belousov 	    P_TOTAL_STOP);
14667b4a950aSDavid Xu 	PROC_SLOCK(p);
146744990b8cSJulian Elischer 	p->p_singlethread = NULL;
14687847a9daSJohn Baldwin 	wakeup_swapper = 0;
146949539972SJulian Elischer 	/*
14707847a9daSJohn Baldwin 	 * If there are other threads they may now run,
147149539972SJulian Elischer 	 * unless of course there is a blanket 'stop order'
147249539972SJulian Elischer 	 * on the process. The single threader must be allowed
147349539972SJulian Elischer 	 * to continue however as this is a bad place to stop.
147449539972SJulian Elischer 	 */
14756ddcc233SKonstantin Belousov 	if (p->p_numthreads != remain_for_mode(mode) && !P_SHOULDSTOP(p)) {
1476ad1e7d28SJulian Elischer                 FOREACH_THREAD_IN_PROC(p, td) {
1477a54e85fdSJeff Roberson 			thread_lock(td);
1478ad1e7d28SJulian Elischer 			if (TD_IS_SUSPENDED(td)) {
147984cdea97SKonstantin Belousov 				wakeup_swapper |= thread_unsuspend_one(td, p,
148084cdea97SKonstantin Belousov 				    mode == SINGLE_BOUNDARY);
148161a74c5cSJeff Roberson 			} else
1482a54e85fdSJeff Roberson 				thread_unlock(td);
148349539972SJulian Elischer 		}
1484ad1e7d28SJulian Elischer 	}
148584cdea97SKonstantin Belousov 	KASSERT(mode != SINGLE_BOUNDARY || p->p_boundary_count == 0,
148684cdea97SKonstantin Belousov 	    ("inconsistent boundary count %d", p->p_boundary_count));
14877b4a950aSDavid Xu 	PROC_SUNLOCK(p);
14887847a9daSJohn Baldwin 	if (wakeup_swapper)
14897847a9daSJohn Baldwin 		kick_proc0();
149049539972SJulian Elischer }
14914fc21c09SDaniel Eischen 
1492aae3547bSMateusz Guzik /*
1493aae3547bSMateusz Guzik  * Locate a thread by number and return with proc lock held.
1494aae3547bSMateusz Guzik  *
1495aae3547bSMateusz Guzik  * thread exit establishes proc -> tidhash lock ordering, but lookup
1496aae3547bSMateusz Guzik  * takes tidhash first and needs to return locked proc.
1497aae3547bSMateusz Guzik  *
1498aae3547bSMateusz Guzik  * The problem is worked around by relying on type-safety of both
1499aae3547bSMateusz Guzik  * structures and doing the work in 2 steps:
1500aae3547bSMateusz Guzik  * - tidhash-locked lookup which saves both thread and proc pointers
1501aae3547bSMateusz Guzik  * - proc-locked verification that the found thread still matches
1502aae3547bSMateusz Guzik  */
1503aae3547bSMateusz Guzik static bool
1504aae3547bSMateusz Guzik tdfind_hash(lwpid_t tid, pid_t pid, struct proc **pp, struct thread **tdp)
1505cf7d9a8cSDavid Xu {
1506cf7d9a8cSDavid Xu #define RUN_THRESH	16
1507aae3547bSMateusz Guzik 	struct proc *p;
1508cf7d9a8cSDavid Xu 	struct thread *td;
1509aae3547bSMateusz Guzik 	int run;
1510aae3547bSMateusz Guzik 	bool locked;
1511cf7d9a8cSDavid Xu 
1512aae3547bSMateusz Guzik 	run = 0;
151326007fe3SMateusz Guzik 	rw_rlock(TIDHASHLOCK(tid));
1514aae3547bSMateusz Guzik 	locked = true;
1515cf7d9a8cSDavid Xu 	LIST_FOREACH(td, TIDHASH(tid), td_hash) {
1516aae3547bSMateusz Guzik 		if (td->td_tid != tid) {
1517aae3547bSMateusz Guzik 			run++;
1518aae3547bSMateusz Guzik 			continue;
1519cf7d9a8cSDavid Xu 		}
1520aae3547bSMateusz Guzik 		p = td->td_proc;
1521aae3547bSMateusz Guzik 		if (pid != -1 && p->p_pid != pid) {
1522cf7d9a8cSDavid Xu 			td = NULL;
1523cf7d9a8cSDavid Xu 			break;
1524cf7d9a8cSDavid Xu 		}
1525cf7d9a8cSDavid Xu 		if (run > RUN_THRESH) {
152626007fe3SMateusz Guzik 			if (rw_try_upgrade(TIDHASHLOCK(tid))) {
1527cf7d9a8cSDavid Xu 				LIST_REMOVE(td, td_hash);
1528cf7d9a8cSDavid Xu 				LIST_INSERT_HEAD(TIDHASH(td->td_tid),
1529cf7d9a8cSDavid Xu 					td, td_hash);
153026007fe3SMateusz Guzik 				rw_wunlock(TIDHASHLOCK(tid));
1531aae3547bSMateusz Guzik 				locked = false;
1532aae3547bSMateusz Guzik 				break;
1533cf7d9a8cSDavid Xu 			}
1534cf7d9a8cSDavid Xu 		}
1535cf7d9a8cSDavid Xu 		break;
1536cf7d9a8cSDavid Xu 	}
1537aae3547bSMateusz Guzik 	if (locked)
153826007fe3SMateusz Guzik 		rw_runlock(TIDHASHLOCK(tid));
1539aae3547bSMateusz Guzik 	if (td == NULL)
1540aae3547bSMateusz Guzik 		return (false);
1541aae3547bSMateusz Guzik 	*pp = p;
1542aae3547bSMateusz Guzik 	*tdp = td;
1543aae3547bSMateusz Guzik 	return (true);
1544aae3547bSMateusz Guzik }
1545aae3547bSMateusz Guzik 
1546aae3547bSMateusz Guzik struct thread *
1547aae3547bSMateusz Guzik tdfind(lwpid_t tid, pid_t pid)
1548aae3547bSMateusz Guzik {
1549aae3547bSMateusz Guzik 	struct proc *p;
1550aae3547bSMateusz Guzik 	struct thread *td;
1551aae3547bSMateusz Guzik 
1552aae3547bSMateusz Guzik 	td = curthread;
1553aae3547bSMateusz Guzik 	if (td->td_tid == tid) {
1554aae3547bSMateusz Guzik 		if (pid != -1 && td->td_proc->p_pid != pid)
1555aae3547bSMateusz Guzik 			return (NULL);
1556aae3547bSMateusz Guzik 		PROC_LOCK(td->td_proc);
1557cf7d9a8cSDavid Xu 		return (td);
1558cf7d9a8cSDavid Xu 	}
1559cf7d9a8cSDavid Xu 
1560aae3547bSMateusz Guzik 	for (;;) {
1561aae3547bSMateusz Guzik 		if (!tdfind_hash(tid, pid, &p, &td))
1562aae3547bSMateusz Guzik 			return (NULL);
1563aae3547bSMateusz Guzik 		PROC_LOCK(p);
1564aae3547bSMateusz Guzik 		if (td->td_tid != tid) {
1565aae3547bSMateusz Guzik 			PROC_UNLOCK(p);
1566aae3547bSMateusz Guzik 			continue;
1567aae3547bSMateusz Guzik 		}
1568aae3547bSMateusz Guzik 		if (td->td_proc != p) {
1569aae3547bSMateusz Guzik 			PROC_UNLOCK(p);
1570aae3547bSMateusz Guzik 			continue;
1571aae3547bSMateusz Guzik 		}
1572aae3547bSMateusz Guzik 		if (p->p_state == PRS_NEW) {
1573aae3547bSMateusz Guzik 			PROC_UNLOCK(p);
1574aae3547bSMateusz Guzik 			return (NULL);
1575aae3547bSMateusz Guzik 		}
1576aae3547bSMateusz Guzik 		return (td);
1577aae3547bSMateusz Guzik 	}
1578aae3547bSMateusz Guzik }
1579aae3547bSMateusz Guzik 
1580cf7d9a8cSDavid Xu void
1581cf7d9a8cSDavid Xu tidhash_add(struct thread *td)
1582cf7d9a8cSDavid Xu {
158326007fe3SMateusz Guzik 	rw_wlock(TIDHASHLOCK(td->td_tid));
1584cf7d9a8cSDavid Xu 	LIST_INSERT_HEAD(TIDHASH(td->td_tid), td, td_hash);
158526007fe3SMateusz Guzik 	rw_wunlock(TIDHASHLOCK(td->td_tid));
1586cf7d9a8cSDavid Xu }
1587cf7d9a8cSDavid Xu 
1588cf7d9a8cSDavid Xu void
1589cf7d9a8cSDavid Xu tidhash_remove(struct thread *td)
1590cf7d9a8cSDavid Xu {
159126007fe3SMateusz Guzik 
159226007fe3SMateusz Guzik 	rw_wlock(TIDHASHLOCK(td->td_tid));
1593cf7d9a8cSDavid Xu 	LIST_REMOVE(td, td_hash);
159426007fe3SMateusz Guzik 	rw_wunlock(TIDHASHLOCK(td->td_tid));
1595cf7d9a8cSDavid Xu }
1596