xref: /freebsd/sys/kern/kern_thread.c (revision 9104847f216771dfda5d78a6dd4974882f1a55cd)
19454b2d8SWarner Losh /*-
244990b8cSJulian Elischer  * Copyright (C) 2001 Julian Elischer <julian@freebsd.org>.
344990b8cSJulian Elischer  *  All rights reserved.
444990b8cSJulian Elischer  *
544990b8cSJulian Elischer  * Redistribution and use in source and binary forms, with or without
644990b8cSJulian Elischer  * modification, are permitted provided that the following conditions
744990b8cSJulian Elischer  * are met:
844990b8cSJulian Elischer  * 1. Redistributions of source code must retain the above copyright
944990b8cSJulian Elischer  *    notice(s), this list of conditions and the following disclaimer as
1044990b8cSJulian Elischer  *    the first lines of this file unmodified other than the possible
1144990b8cSJulian Elischer  *    addition of one or more copyright notices.
1244990b8cSJulian Elischer  * 2. Redistributions in binary form must reproduce the above copyright
1344990b8cSJulian Elischer  *    notice(s), this list of conditions and the following disclaimer in the
1444990b8cSJulian Elischer  *    documentation and/or other materials provided with the distribution.
1544990b8cSJulian Elischer  *
1644990b8cSJulian Elischer  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
1744990b8cSJulian Elischer  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1844990b8cSJulian Elischer  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1944990b8cSJulian Elischer  * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
2044990b8cSJulian Elischer  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2144990b8cSJulian Elischer  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2244990b8cSJulian Elischer  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
2344990b8cSJulian Elischer  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2444990b8cSJulian Elischer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2544990b8cSJulian Elischer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
2644990b8cSJulian Elischer  * DAMAGE.
2744990b8cSJulian Elischer  */
2844990b8cSJulian Elischer 
29677b542eSDavid E. O'Brien #include <sys/cdefs.h>
30677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
31677b542eSDavid E. O'Brien 
3244990b8cSJulian Elischer #include <sys/param.h>
3344990b8cSJulian Elischer #include <sys/systm.h>
3444990b8cSJulian Elischer #include <sys/kernel.h>
3544990b8cSJulian Elischer #include <sys/lock.h>
3644990b8cSJulian Elischer #include <sys/mutex.h>
3744990b8cSJulian Elischer #include <sys/proc.h>
3894e0a4cdSJulian Elischer #include <sys/smp.h>
3944990b8cSJulian Elischer #include <sys/sysctl.h>
40de028f5aSJeff Roberson #include <sys/sched.h>
4144f3b092SJohn Baldwin #include <sys/sleepqueue.h>
42961a7b24SJohn Baldwin #include <sys/turnstile.h>
4344990b8cSJulian Elischer #include <sys/ktr.h>
44bc8e6d81SDavid Xu #include <sys/umtx.h>
4544990b8cSJulian Elischer 
4644990b8cSJulian Elischer #include <vm/vm.h>
4749a2507bSAlan Cox #include <vm/vm_extern.h>
4844990b8cSJulian Elischer #include <vm/uma.h>
4902fb42b0SPeter Wemm 
5044990b8cSJulian Elischer /*
514f0db5e0SJulian Elischer  * KSEGRP related storage.
5244990b8cSJulian Elischer  */
534f0db5e0SJulian Elischer static uma_zone_t ksegrp_zone;
5444990b8cSJulian Elischer static uma_zone_t thread_zone;
5544990b8cSJulian Elischer 
564f0db5e0SJulian Elischer /* DEBUG ONLY */
5744990b8cSJulian Elischer SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0, "thread allocation");
58696058c3SJulian Elischer static int thread_debug = 0;
59696058c3SJulian Elischer SYSCTL_INT(_kern_threads, OID_AUTO, debug, CTLFLAG_RW,
60696058c3SJulian Elischer 	&thread_debug, 0, "thread debug");
61fdc5ecd2SDavid Xu 
62345ad866SJulian Elischer int max_threads_per_proc = 1500;
63fdc5ecd2SDavid Xu SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW,
644f0db5e0SJulian Elischer 	&max_threads_per_proc, 0, "Limit on threads per proc");
654f0db5e0SJulian Elischer 
66ed062c8dSJulian Elischer int max_groups_per_proc = 1500;
67fdc5ecd2SDavid Xu SYSCTL_INT(_kern_threads, OID_AUTO, max_groups_per_proc, CTLFLAG_RW,
68fdc5ecd2SDavid Xu 	&max_groups_per_proc, 0, "Limit on thread groups per proc");
69fdc5ecd2SDavid Xu 
70345ad866SJulian Elischer int max_threads_hits;
710252d203SDavid Xu SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD,
720252d203SDavid Xu 	&max_threads_hits, 0, "");
730252d203SDavid Xu 
7494e0a4cdSJulian Elischer int virtual_cpu;
7594e0a4cdSJulian Elischer 
765215b187SJeff Roberson TAILQ_HEAD(, thread) zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads);
775c8329edSJulian Elischer TAILQ_HEAD(, ksegrp) zombie_ksegrps = TAILQ_HEAD_INITIALIZER(zombie_ksegrps);
785215b187SJeff Roberson struct mtx kse_zombie_lock;
795215b187SJeff Roberson MTX_SYSINIT(kse_zombie_lock, &kse_zombie_lock, "kse zombie lock", MTX_SPIN);
8044990b8cSJulian Elischer 
8194e0a4cdSJulian Elischer static int
8294e0a4cdSJulian Elischer sysctl_kse_virtual_cpu(SYSCTL_HANDLER_ARGS)
8394e0a4cdSJulian Elischer {
8494e0a4cdSJulian Elischer 	int error, new_val;
8594e0a4cdSJulian Elischer 	int def_val;
8694e0a4cdSJulian Elischer 
8794e0a4cdSJulian Elischer 	def_val = mp_ncpus;
8894e0a4cdSJulian Elischer 	if (virtual_cpu == 0)
8994e0a4cdSJulian Elischer 		new_val = def_val;
9094e0a4cdSJulian Elischer 	else
9194e0a4cdSJulian Elischer 		new_val = virtual_cpu;
9294e0a4cdSJulian Elischer 	error = sysctl_handle_int(oidp, &new_val, 0, req);
9394e0a4cdSJulian Elischer 	if (error != 0 || req->newptr == NULL)
9494e0a4cdSJulian Elischer 		return (error);
9594e0a4cdSJulian Elischer 	if (new_val < 0)
9694e0a4cdSJulian Elischer 		return (EINVAL);
9794e0a4cdSJulian Elischer 	virtual_cpu = new_val;
9894e0a4cdSJulian Elischer 	return (0);
9994e0a4cdSJulian Elischer }
10094e0a4cdSJulian Elischer 
10194e0a4cdSJulian Elischer /* DEBUG ONLY */
10294e0a4cdSJulian Elischer SYSCTL_PROC(_kern_threads, OID_AUTO, virtual_cpu, CTLTYPE_INT|CTLFLAG_RW,
10394e0a4cdSJulian Elischer 	0, sizeof(virtual_cpu), sysctl_kse_virtual_cpu, "I",
10494e0a4cdSJulian Elischer 	"debug virtual cpus");
1055c8329edSJulian Elischer 
106fdcac928SMarcel Moolenaar struct mtx tid_lock;
1071ea7a6f8SPoul-Henning Kamp static struct unrhdr *tid_unrhdr;
108fdcac928SMarcel Moolenaar 
109fdcac928SMarcel Moolenaar /*
110696058c3SJulian Elischer  * Prepare a thread for use.
11144990b8cSJulian Elischer  */
112b23f72e9SBrian Feldman static int
113b23f72e9SBrian Feldman thread_ctor(void *mem, int size, void *arg, int flags)
11444990b8cSJulian Elischer {
11544990b8cSJulian Elischer 	struct thread	*td;
11644990b8cSJulian Elischer 
11744990b8cSJulian Elischer 	td = (struct thread *)mem;
11871fad9fdSJulian Elischer 	td->td_state = TDS_INACTIVE;
119060563ecSJulian Elischer 	td->td_oncpu = NOCPU;
1206c27c603SJuli Mallett 
121773eff9dSPoul-Henning Kamp 	td->td_tid = alloc_unr(tid_unrhdr);
122773eff9dSPoul-Henning Kamp 
1236c27c603SJuli Mallett 	/*
1246c27c603SJuli Mallett 	 * Note that td_critnest begins life as 1 because the thread is not
1256c27c603SJuli Mallett 	 * running and is thereby implicitly waiting to be on the receiving
1266c27c603SJuli Mallett 	 * end of a context switch.  A context switch must occur inside a
1276c27c603SJuli Mallett 	 * critical section, and in fact, includes hand-off of the sched_lock.
1286c27c603SJuli Mallett 	 * After a context switch to a newly created thread, it will release
1296c27c603SJuli Mallett 	 * sched_lock for the first time, and its td_critnest will hit 0 for
1306c27c603SJuli Mallett 	 * the first time.  This happens on the far end of a context switch,
1316c27c603SJuli Mallett 	 * and when it context switches away from itself, it will in fact go
1326c27c603SJuli Mallett 	 * back into a critical section, and hand off the sched lock to the
1336c27c603SJuli Mallett 	 * next thread.
1346c27c603SJuli Mallett 	 */
135139b7550SJohn Baldwin 	td->td_critnest = 1;
136b23f72e9SBrian Feldman 	return (0);
13744990b8cSJulian Elischer }
13844990b8cSJulian Elischer 
13944990b8cSJulian Elischer /*
14044990b8cSJulian Elischer  * Reclaim a thread after use.
14144990b8cSJulian Elischer  */
14244990b8cSJulian Elischer static void
14344990b8cSJulian Elischer thread_dtor(void *mem, int size, void *arg)
14444990b8cSJulian Elischer {
14544990b8cSJulian Elischer 	struct thread *td;
14644990b8cSJulian Elischer 
14744990b8cSJulian Elischer 	td = (struct thread *)mem;
14844990b8cSJulian Elischer 
14944990b8cSJulian Elischer #ifdef INVARIANTS
15044990b8cSJulian Elischer 	/* Verify that this thread is in a safe state to free. */
15144990b8cSJulian Elischer 	switch (td->td_state) {
15271fad9fdSJulian Elischer 	case TDS_INHIBITED:
15371fad9fdSJulian Elischer 	case TDS_RUNNING:
15471fad9fdSJulian Elischer 	case TDS_CAN_RUN:
15544990b8cSJulian Elischer 	case TDS_RUNQ:
15644990b8cSJulian Elischer 		/*
15744990b8cSJulian Elischer 		 * We must never unlink a thread that is in one of
15844990b8cSJulian Elischer 		 * these states, because it is currently active.
15944990b8cSJulian Elischer 		 */
16044990b8cSJulian Elischer 		panic("bad state for thread unlinking");
16144990b8cSJulian Elischer 		/* NOTREACHED */
16271fad9fdSJulian Elischer 	case TDS_INACTIVE:
16344990b8cSJulian Elischer 		break;
16444990b8cSJulian Elischer 	default:
16544990b8cSJulian Elischer 		panic("bad thread state");
16644990b8cSJulian Elischer 		/* NOTREACHED */
16744990b8cSJulian Elischer 	}
16844990b8cSJulian Elischer #endif
169773eff9dSPoul-Henning Kamp 
170773eff9dSPoul-Henning Kamp 	free_unr(tid_unrhdr, td->td_tid);
171ed062c8dSJulian Elischer 	sched_newthread(td);
17244990b8cSJulian Elischer }
17344990b8cSJulian Elischer 
17444990b8cSJulian Elischer /*
17544990b8cSJulian Elischer  * Initialize type-stable parts of a thread (when newly created).
17644990b8cSJulian Elischer  */
177b23f72e9SBrian Feldman static int
178b23f72e9SBrian Feldman thread_init(void *mem, int size, int flags)
17944990b8cSJulian Elischer {
18044990b8cSJulian Elischer 	struct thread *td;
18144990b8cSJulian Elischer 
18244990b8cSJulian Elischer 	td = (struct thread *)mem;
183247aba24SMarcel Moolenaar 
18449a2507bSAlan Cox 	vm_thread_new(td, 0);
18544990b8cSJulian Elischer 	cpu_thread_setup(td);
18644f3b092SJohn Baldwin 	td->td_sleepqueue = sleepq_alloc();
187961a7b24SJohn Baldwin 	td->td_turnstile = turnstile_alloc();
188bc8e6d81SDavid Xu 	td->td_umtxq = umtxq_alloc();
189de028f5aSJeff Roberson 	td->td_sched = (struct td_sched *)&td[1];
190ed062c8dSJulian Elischer 	sched_newthread(td);
191b23f72e9SBrian Feldman 	return (0);
19244990b8cSJulian Elischer }
19344990b8cSJulian Elischer 
19444990b8cSJulian Elischer /*
19544990b8cSJulian Elischer  * Tear down type-stable parts of a thread (just before being discarded).
19644990b8cSJulian Elischer  */
19744990b8cSJulian Elischer static void
19844990b8cSJulian Elischer thread_fini(void *mem, int size)
19944990b8cSJulian Elischer {
20044990b8cSJulian Elischer 	struct thread *td;
20144990b8cSJulian Elischer 
20244990b8cSJulian Elischer 	td = (struct thread *)mem;
203961a7b24SJohn Baldwin 	turnstile_free(td->td_turnstile);
20444f3b092SJohn Baldwin 	sleepq_free(td->td_sleepqueue);
205bc8e6d81SDavid Xu 	umtxq_free(td->td_umtxq);
20649a2507bSAlan Cox 	vm_thread_dispose(td);
20744990b8cSJulian Elischer }
2085215b187SJeff Roberson 
209de028f5aSJeff Roberson /*
210de028f5aSJeff Roberson  * Initialize type-stable parts of a ksegrp (when newly created).
211de028f5aSJeff Roberson  */
212b23f72e9SBrian Feldman static int
213a9b5dc7dSJulian Elischer ksegrp_ctor(void *mem, int size, void *arg, int flags)
214de028f5aSJeff Roberson {
215de028f5aSJeff Roberson 	struct ksegrp	*kg;
216de028f5aSJeff Roberson 
217de028f5aSJeff Roberson 	kg = (struct ksegrp *)mem;
218a9b5dc7dSJulian Elischer 	bzero(mem, size);
219de028f5aSJeff Roberson 	kg->kg_sched = (struct kg_sched *)&kg[1];
220b23f72e9SBrian Feldman 	return (0);
221de028f5aSJeff Roberson }
22244990b8cSJulian Elischer 
2235c8329edSJulian Elischer void
2245c8329edSJulian Elischer ksegrp_link(struct ksegrp *kg, struct proc *p)
2255c8329edSJulian Elischer {
2265c8329edSJulian Elischer 
2275c8329edSJulian Elischer 	TAILQ_INIT(&kg->kg_threads);
2285c8329edSJulian Elischer 	TAILQ_INIT(&kg->kg_runq);	/* links with td_runq */
2295215b187SJeff Roberson 	TAILQ_INIT(&kg->kg_upcalls);	/* all upcall structure in ksegrp */
2305c8329edSJulian Elischer 	kg->kg_proc = p;
2315215b187SJeff Roberson 	/*
2325215b187SJeff Roberson 	 * the following counters are in the -zero- section
2335215b187SJeff Roberson 	 * and may not need clearing
2345215b187SJeff Roberson 	 */
2355c8329edSJulian Elischer 	kg->kg_numthreads = 0;
2365215b187SJeff Roberson 	kg->kg_numupcalls = 0;
2375c8329edSJulian Elischer 	/* link it in now that it's consistent */
2385c8329edSJulian Elischer 	p->p_numksegrps++;
2395c8329edSJulian Elischer 	TAILQ_INSERT_HEAD(&p->p_ksegrps, kg, kg_ksegrp);
2405c8329edSJulian Elischer }
2415c8329edSJulian Elischer 
242ed062c8dSJulian Elischer /*
243ed062c8dSJulian Elischer  * Called from:
244ed062c8dSJulian Elischer  *   thread-exit()
245ed062c8dSJulian Elischer  */
2465c8329edSJulian Elischer void
2475c8329edSJulian Elischer ksegrp_unlink(struct ksegrp *kg)
2485c8329edSJulian Elischer {
2495c8329edSJulian Elischer 	struct proc *p;
2505c8329edSJulian Elischer 
2515c8329edSJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
2525215b187SJeff Roberson 	KASSERT((kg->kg_numthreads == 0), ("ksegrp_unlink: residual threads"));
2535215b187SJeff Roberson 	KASSERT((kg->kg_numupcalls == 0), ("ksegrp_unlink: residual upcalls"));
2545215b187SJeff Roberson 
2555c8329edSJulian Elischer 	p = kg->kg_proc;
2565c8329edSJulian Elischer 	TAILQ_REMOVE(&p->p_ksegrps, kg, kg_ksegrp);
2575c8329edSJulian Elischer 	p->p_numksegrps--;
2585c8329edSJulian Elischer 	/*
2595c8329edSJulian Elischer 	 * Aggregate stats from the KSE
2605c8329edSJulian Elischer 	 */
26121fc3164SDavid Xu 	if (p->p_procscopegrp == kg)
26221fc3164SDavid Xu 		p->p_procscopegrp = NULL;
2635c8329edSJulian Elischer }
2645c8329edSJulian Elischer 
2655c8329edSJulian Elischer /*
2665215b187SJeff Roberson  * For a newly created process,
2675215b187SJeff Roberson  * link up all the structures and its initial threads etc.
268ed062c8dSJulian Elischer  * called from:
269ed062c8dSJulian Elischer  * {arch}/{arch}/machdep.c   ia64_init(), init386() etc.
270ed062c8dSJulian Elischer  * proc_dtor() (should go away)
271ed062c8dSJulian Elischer  * proc_init()
2725c8329edSJulian Elischer  */
2735c8329edSJulian Elischer void
274ed062c8dSJulian Elischer proc_linkup(struct proc *p, struct ksegrp *kg, struct thread *td)
2755c8329edSJulian Elischer {
2765c8329edSJulian Elischer 
2775c8329edSJulian Elischer 	TAILQ_INIT(&p->p_ksegrps);	     /* all ksegrps in proc */
2785c8329edSJulian Elischer 	TAILQ_INIT(&p->p_threads);	     /* all threads in proc */
2795c8329edSJulian Elischer 	TAILQ_INIT(&p->p_suspended);	     /* Threads suspended */
2809104847fSDavid Xu 	sigqueue_init(&p->p_sigqueue, p);
2815c8329edSJulian Elischer 	p->p_numksegrps = 0;
2825c8329edSJulian Elischer 	p->p_numthreads = 0;
2835c8329edSJulian Elischer 
2845c8329edSJulian Elischer 	ksegrp_link(kg, p);
2855c8329edSJulian Elischer 	thread_link(td, kg);
2865c8329edSJulian Elischer }
2875c8329edSJulian Elischer 
2885c8329edSJulian Elischer /*
28944990b8cSJulian Elischer  * Initialize global thread allocation resources.
29044990b8cSJulian Elischer  */
29144990b8cSJulian Elischer void
29244990b8cSJulian Elischer threadinit(void)
29344990b8cSJulian Elischer {
29444990b8cSJulian Elischer 
2951ea7a6f8SPoul-Henning Kamp 	mtx_init(&tid_lock, "TID lock", NULL, MTX_DEF);
2961ea7a6f8SPoul-Henning Kamp 	tid_unrhdr = new_unrhdr(PID_MAX + 1, INT_MAX, &tid_lock);
2971ea7a6f8SPoul-Henning Kamp 
298de028f5aSJeff Roberson 	thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(),
29944990b8cSJulian Elischer 	    thread_ctor, thread_dtor, thread_init, thread_fini,
30044990b8cSJulian Elischer 	    UMA_ALIGN_CACHE, 0);
301de028f5aSJeff Roberson 	ksegrp_zone = uma_zcreate("KSEGRP", sched_sizeof_ksegrp(),
302a9b5dc7dSJulian Elischer 	    ksegrp_ctor, NULL, NULL, NULL,
3034f0db5e0SJulian Elischer 	    UMA_ALIGN_CACHE, 0);
304ed062c8dSJulian Elischer 	kseinit();	/* set up kse specific stuff  e.g. upcall zone*/
30544990b8cSJulian Elischer }
30644990b8cSJulian Elischer 
30744990b8cSJulian Elischer /*
3081faf202eSJulian Elischer  * Stash an embarasingly extra thread into the zombie thread queue.
30944990b8cSJulian Elischer  */
31044990b8cSJulian Elischer void
31144990b8cSJulian Elischer thread_stash(struct thread *td)
31244990b8cSJulian Elischer {
3135215b187SJeff Roberson 	mtx_lock_spin(&kse_zombie_lock);
31444990b8cSJulian Elischer 	TAILQ_INSERT_HEAD(&zombie_threads, td, td_runq);
3155215b187SJeff Roberson 	mtx_unlock_spin(&kse_zombie_lock);
31644990b8cSJulian Elischer }
31744990b8cSJulian Elischer 
31844990b8cSJulian Elischer /*
3195c8329edSJulian Elischer  * Stash an embarasingly extra ksegrp into the zombie ksegrp queue.
3205c8329edSJulian Elischer  */
3215c8329edSJulian Elischer void
3225c8329edSJulian Elischer ksegrp_stash(struct ksegrp *kg)
3235c8329edSJulian Elischer {
3245215b187SJeff Roberson 	mtx_lock_spin(&kse_zombie_lock);
3255c8329edSJulian Elischer 	TAILQ_INSERT_HEAD(&zombie_ksegrps, kg, kg_ksegrp);
3265215b187SJeff Roberson 	mtx_unlock_spin(&kse_zombie_lock);
3275c8329edSJulian Elischer }
3285c8329edSJulian Elischer 
3295c8329edSJulian Elischer /*
3305215b187SJeff Roberson  * Reap zombie kse resource.
33144990b8cSJulian Elischer  */
33244990b8cSJulian Elischer void
33344990b8cSJulian Elischer thread_reap(void)
33444990b8cSJulian Elischer {
3355c8329edSJulian Elischer 	struct thread *td_first, *td_next;
3365c8329edSJulian Elischer 	struct ksegrp *kg_first, * kg_next;
33744990b8cSJulian Elischer 
33844990b8cSJulian Elischer 	/*
3395215b187SJeff Roberson 	 * Don't even bother to lock if none at this instant,
3405215b187SJeff Roberson 	 * we really don't care about the next instant..
34144990b8cSJulian Elischer 	 */
3425c8329edSJulian Elischer 	if ((!TAILQ_EMPTY(&zombie_threads))
343345ad866SJulian Elischer 	    || (!TAILQ_EMPTY(&zombie_ksegrps))) {
3445215b187SJeff Roberson 		mtx_lock_spin(&kse_zombie_lock);
3455c8329edSJulian Elischer 		td_first = TAILQ_FIRST(&zombie_threads);
3465c8329edSJulian Elischer 		kg_first = TAILQ_FIRST(&zombie_ksegrps);
3475c8329edSJulian Elischer 		if (td_first)
3485c8329edSJulian Elischer 			TAILQ_INIT(&zombie_threads);
3495c8329edSJulian Elischer 		if (kg_first)
3505c8329edSJulian Elischer 			TAILQ_INIT(&zombie_ksegrps);
3515215b187SJeff Roberson 		mtx_unlock_spin(&kse_zombie_lock);
3525c8329edSJulian Elischer 		while (td_first) {
3535c8329edSJulian Elischer 			td_next = TAILQ_NEXT(td_first, td_runq);
3545215b187SJeff Roberson 			if (td_first->td_ucred)
3555215b187SJeff Roberson 				crfree(td_first->td_ucred);
3565c8329edSJulian Elischer 			thread_free(td_first);
3575c8329edSJulian Elischer 			td_first = td_next;
35844990b8cSJulian Elischer 		}
3595c8329edSJulian Elischer 		while (kg_first) {
3605c8329edSJulian Elischer 			kg_next = TAILQ_NEXT(kg_first, kg_ksegrp);
3615c8329edSJulian Elischer 			ksegrp_free(kg_first);
3625c8329edSJulian Elischer 			kg_first = kg_next;
3635c8329edSJulian Elischer 		}
364ed062c8dSJulian Elischer 		/*
365ed062c8dSJulian Elischer 		 * there will always be a thread on the list if one of these
366ed062c8dSJulian Elischer 		 * is there.
367ed062c8dSJulian Elischer 		 */
368345ad866SJulian Elischer 		kse_GC();
36944990b8cSJulian Elischer 	}
370ed062c8dSJulian Elischer }
37144990b8cSJulian Elischer 
37244990b8cSJulian Elischer /*
3734f0db5e0SJulian Elischer  * Allocate a ksegrp.
3744f0db5e0SJulian Elischer  */
3754f0db5e0SJulian Elischer struct ksegrp *
3764f0db5e0SJulian Elischer ksegrp_alloc(void)
3774f0db5e0SJulian Elischer {
378a163d034SWarner Losh 	return (uma_zalloc(ksegrp_zone, M_WAITOK));
3794f0db5e0SJulian Elischer }
3804f0db5e0SJulian Elischer 
3814f0db5e0SJulian Elischer /*
38244990b8cSJulian Elischer  * Allocate a thread.
38344990b8cSJulian Elischer  */
38444990b8cSJulian Elischer struct thread *
38544990b8cSJulian Elischer thread_alloc(void)
38644990b8cSJulian Elischer {
38744990b8cSJulian Elischer 	thread_reap(); /* check if any zombies to get */
388a163d034SWarner Losh 	return (uma_zalloc(thread_zone, M_WAITOK));
38944990b8cSJulian Elischer }
39044990b8cSJulian Elischer 
39144990b8cSJulian Elischer /*
3924f0db5e0SJulian Elischer  * Deallocate a ksegrp.
3934f0db5e0SJulian Elischer  */
3944f0db5e0SJulian Elischer void
3954f0db5e0SJulian Elischer ksegrp_free(struct ksegrp *td)
3964f0db5e0SJulian Elischer {
3974f0db5e0SJulian Elischer 	uma_zfree(ksegrp_zone, td);
3984f0db5e0SJulian Elischer }
3994f0db5e0SJulian Elischer 
4004f0db5e0SJulian Elischer /*
40144990b8cSJulian Elischer  * Deallocate a thread.
40244990b8cSJulian Elischer  */
40344990b8cSJulian Elischer void
40444990b8cSJulian Elischer thread_free(struct thread *td)
40544990b8cSJulian Elischer {
406696058c3SJulian Elischer 
407696058c3SJulian Elischer 	cpu_thread_clean(td);
40844990b8cSJulian Elischer 	uma_zfree(thread_zone, td);
40944990b8cSJulian Elischer }
41044990b8cSJulian Elischer 
41144990b8cSJulian Elischer /*
41244990b8cSJulian Elischer  * Discard the current thread and exit from its context.
41394e0a4cdSJulian Elischer  * Always called with scheduler locked.
41444990b8cSJulian Elischer  *
41544990b8cSJulian Elischer  * Because we can't free a thread while we're operating under its context,
416696058c3SJulian Elischer  * push the current thread into our CPU's deadthread holder. This means
417696058c3SJulian Elischer  * we needn't worry about someone else grabbing our context before we
41894e0a4cdSJulian Elischer  * do a cpu_throw().  This may not be needed now as we are under schedlock.
41994e0a4cdSJulian Elischer  * Maybe we can just do a thread_stash() as thr_exit1 does.
42094e0a4cdSJulian Elischer  */
42194e0a4cdSJulian Elischer /*  XXX
42294e0a4cdSJulian Elischer  * libthr expects its thread exit to return for the last
42394e0a4cdSJulian Elischer  * thread, meaning that the program is back to non-threaded
42494e0a4cdSJulian Elischer  * mode I guess. Because we do this (cpu_throw) unconditionally
42594e0a4cdSJulian Elischer  * here, they have their own version of it. (thr_exit1())
42694e0a4cdSJulian Elischer  * that doesn't do it all if this was the last thread.
42794e0a4cdSJulian Elischer  * It is also called from thread_suspend_check().
42894e0a4cdSJulian Elischer  * Of course in the end, they end up coming here through exit1
42994e0a4cdSJulian Elischer  * anyhow..  After fixing 'thr' to play by the rules we should be able
43094e0a4cdSJulian Elischer  * to merge these two functions together.
431ed062c8dSJulian Elischer  *
432ed062c8dSJulian Elischer  * called from:
433ed062c8dSJulian Elischer  * exit1()
434ed062c8dSJulian Elischer  * kse_exit()
435ed062c8dSJulian Elischer  * thr_exit()
436ed062c8dSJulian Elischer  * thread_user_enter()
437ed062c8dSJulian Elischer  * thread_userret()
438ed062c8dSJulian Elischer  * thread_suspend_check()
43944990b8cSJulian Elischer  */
44044990b8cSJulian Elischer void
44144990b8cSJulian Elischer thread_exit(void)
44244990b8cSJulian Elischer {
44344990b8cSJulian Elischer 	struct thread *td;
44444990b8cSJulian Elischer 	struct proc *p;
44544990b8cSJulian Elischer 	struct ksegrp	*kg;
44644990b8cSJulian Elischer 
44744990b8cSJulian Elischer 	td = curthread;
44844990b8cSJulian Elischer 	kg = td->td_ksegrp;
44944990b8cSJulian Elischer 	p = td->td_proc;
45044990b8cSJulian Elischer 
45144990b8cSJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
452ed062c8dSJulian Elischer 	mtx_assert(&Giant, MA_NOTOWNED);
45344990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
454ed062c8dSJulian Elischer 	KASSERT(p != NULL, ("thread exiting without a process"));
455ed062c8dSJulian Elischer 	KASSERT(kg != NULL, ("thread exiting without a kse group"));
456cc701b73SRobert Watson 	CTR3(KTR_PROC, "thread_exit: thread %p (pid %ld, %s)", td,
457cc701b73SRobert Watson 	    (long)p->p_pid, p->p_comm);
4589104847fSDavid Xu 	KASSERT(TAILQ_EMPTY(&td->td_sigqueue.sq_list), ("signal pending"));
45944990b8cSJulian Elischer 
46048bfcdddSJulian Elischer 	if (td->td_standin != NULL) {
461ed062c8dSJulian Elischer 		/*
462ed062c8dSJulian Elischer 		 * Note that we don't need to free the cred here as it
463ed062c8dSJulian Elischer 		 * is done in thread_reap().
464ed062c8dSJulian Elischer 		 */
46548bfcdddSJulian Elischer 		thread_stash(td->td_standin);
46648bfcdddSJulian Elischer 		td->td_standin = NULL;
46748bfcdddSJulian Elischer 	}
46848bfcdddSJulian Elischer 
469ed062c8dSJulian Elischer 	/*
470ed062c8dSJulian Elischer 	 * drop FPU & debug register state storage, or any other
471ed062c8dSJulian Elischer 	 * architecture specific resources that
472ed062c8dSJulian Elischer 	 * would not be on a new untouched process.
473ed062c8dSJulian Elischer 	 */
47444990b8cSJulian Elischer 	cpu_thread_exit(td);	/* XXXSMP */
47544990b8cSJulian Elischer 
4761faf202eSJulian Elischer 	/*
477ed062c8dSJulian Elischer 	 * The thread is exiting. scheduler can release its stuff
478ed062c8dSJulian Elischer 	 * and collect stats etc.
479ed062c8dSJulian Elischer 	 */
480ed062c8dSJulian Elischer 	sched_thread_exit(td);
481ed062c8dSJulian Elischer 
482ed062c8dSJulian Elischer 	/*
4831faf202eSJulian Elischer 	 * The last thread is left attached to the process
4841faf202eSJulian Elischer 	 * So that the whole bundle gets recycled. Skip
485ed062c8dSJulian Elischer 	 * all this stuff if we never had threads.
486ed062c8dSJulian Elischer 	 * EXIT clears all sign of other threads when
487ed062c8dSJulian Elischer 	 * it goes to single threading, so the last thread always
488ed062c8dSJulian Elischer 	 * takes the short path.
4891faf202eSJulian Elischer 	 */
490ed062c8dSJulian Elischer 	if (p->p_flag & P_HADTHREADS) {
4911faf202eSJulian Elischer 		if (p->p_numthreads > 1) {
492d3a0bd78SJulian Elischer 			thread_unlink(td);
493ed062c8dSJulian Elischer 
494ed062c8dSJulian Elischer 			/* XXX first arg not used in 4BSD or ULE */
495ed062c8dSJulian Elischer 			sched_exit_thread(FIRST_THREAD_IN_PROC(p), td);
496ed062c8dSJulian Elischer 
497ed062c8dSJulian Elischer 			/*
49844990b8cSJulian Elischer 			 * The test below is NOT true if we are the
4991faf202eSJulian Elischer 			 * sole exiting thread. P_STOPPED_SNGL is unset
50044990b8cSJulian Elischer 			 * in exit1() after it is the only survivor.
50144990b8cSJulian Elischer 			 */
5021279572aSDavid Xu 			if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
50344990b8cSJulian Elischer 				if (p->p_numthreads == p->p_suspcount) {
50471fad9fdSJulian Elischer 					thread_unsuspend_one(p->p_singlethread);
50544990b8cSJulian Elischer 				}
50644990b8cSJulian Elischer 			}
50748bfcdddSJulian Elischer 
5085215b187SJeff Roberson 			/*
5095215b187SJeff Roberson 			 * Because each upcall structure has an owner thread,
5105215b187SJeff Roberson 			 * owner thread exits only when process is in exiting
5115215b187SJeff Roberson 			 * state, so upcall to userland is no longer needed,
5125215b187SJeff Roberson 			 * deleting upcall structure is safe here.
5135215b187SJeff Roberson 			 * So when all threads in a group is exited, all upcalls
5145215b187SJeff Roberson 			 * in the group should be automatically freed.
515ed062c8dSJulian Elischer 			 *  XXXKSE This is a KSE thing and should be exported
516ed062c8dSJulian Elischer 			 * there somehow.
5175215b187SJeff Roberson 			 */
5185215b187SJeff Roberson 			upcall_remove(td);
5196f8132a8SJulian Elischer 
52048bfcdddSJulian Elischer 			/*
521ed062c8dSJulian Elischer 			 * If the thread we unlinked above was the last one,
522ed062c8dSJulian Elischer 			 * then this ksegrp should go away too.
52348bfcdddSJulian Elischer 			 */
524ed062c8dSJulian Elischer 			if (kg->kg_numthreads == 0) {
525ed062c8dSJulian Elischer 				/*
526ed062c8dSJulian Elischer 				 * let the scheduler know about this in case
527ed062c8dSJulian Elischer 				 * it needs to recover stats or resources.
528ed062c8dSJulian Elischer 				 * Theoretically we could let
529ed062c8dSJulian Elischer 				 * sched_exit_ksegrp()  do the equivalent of
530ed062c8dSJulian Elischer 				 * setting the concurrency to 0
531ed062c8dSJulian Elischer 				 * but don't do it yet to avoid changing
532ed062c8dSJulian Elischer 				 * the existing scheduler code until we
533ed062c8dSJulian Elischer 				 * are ready.
534ed062c8dSJulian Elischer 				 * We supply a random other ksegrp
535ed062c8dSJulian Elischer 				 * as the recipient of any built up
536ed062c8dSJulian Elischer 				 * cpu usage etc. (If the scheduler wants it).
537ed062c8dSJulian Elischer 				 * XXXKSE
538ed062c8dSJulian Elischer 				 * This is probably not fair so think of
539ed062c8dSJulian Elischer  				 * a better answer.
540ed062c8dSJulian Elischer 				 */
54155d44f79SJulian Elischer 				sched_exit_ksegrp(FIRST_KSEGRP_IN_PROC(p), td);
542ed062c8dSJulian Elischer 				sched_set_concurrency(kg, 0); /* XXX TEMP */
543ab2baa72SDavid Xu 				ksegrp_unlink(kg);
544ed062c8dSJulian Elischer 				ksegrp_stash(kg);
545ab2baa72SDavid Xu 			}
5466f8132a8SJulian Elischer 			PROC_UNLOCK(p);
5475c8329edSJulian Elischer 			td->td_ksegrp	= NULL;
548696058c3SJulian Elischer 			PCPU_SET(deadthread, td);
5491faf202eSJulian Elischer 		} else {
550ed062c8dSJulian Elischer 			/*
551ed062c8dSJulian Elischer 			 * The last thread is exiting.. but not through exit()
552ed062c8dSJulian Elischer 			 * what should we do?
553ed062c8dSJulian Elischer 			 * Theoretically this can't happen
554ed062c8dSJulian Elischer  			 * exit1() - clears threading flags before coming here
555ed062c8dSJulian Elischer  			 * kse_exit() - treats last thread specially
556ed062c8dSJulian Elischer  			 * thr_exit() - treats last thread specially
557ed062c8dSJulian Elischer  			 * thread_user_enter() - only if more exist
558ed062c8dSJulian Elischer  			 * thread_userret() - only if more exist
559ed062c8dSJulian Elischer  			 * thread_suspend_check() - only if more exist
560ed062c8dSJulian Elischer 			 */
561ed062c8dSJulian Elischer 			panic ("thread_exit: Last thread exiting on its own");
562ed062c8dSJulian Elischer 		}
563ed062c8dSJulian Elischer 	} else {
564ed062c8dSJulian Elischer 		/*
565ed062c8dSJulian Elischer 		 * non threaded process comes here.
566ed062c8dSJulian Elischer 		 * This includes an EX threaded process that is coming
567ed062c8dSJulian Elischer 		 * here via exit1(). (exit1 dethreads the proc first).
568ed062c8dSJulian Elischer 		 */
5691faf202eSJulian Elischer 		PROC_UNLOCK(p);
5701faf202eSJulian Elischer 	}
571dcc9954eSJulian Elischer 	td->td_state = TDS_INACTIVE;
572732d9528SJulian Elischer 	CTR1(KTR_PROC, "thread_exit: cpu_throw() thread %p", td);
573cc66ebe2SPeter Wemm 	cpu_throw(td, choosethread());
574cc66ebe2SPeter Wemm 	panic("I'm a teapot!");
57544990b8cSJulian Elischer 	/* NOTREACHED */
57644990b8cSJulian Elischer }
57744990b8cSJulian Elischer 
57844990b8cSJulian Elischer /*
579696058c3SJulian Elischer  * Do any thread specific cleanups that may be needed in wait()
58037814395SPeter Wemm  * called with Giant, proc and schedlock not held.
581696058c3SJulian Elischer  */
582696058c3SJulian Elischer void
583696058c3SJulian Elischer thread_wait(struct proc *p)
584696058c3SJulian Elischer {
585696058c3SJulian Elischer 	struct thread *td;
586696058c3SJulian Elischer 
58737814395SPeter Wemm 	mtx_assert(&Giant, MA_NOTOWNED);
58885495c72SJens Schweikhardt 	KASSERT((p->p_numthreads == 1), ("Multiple threads in wait1()"));
58985495c72SJens Schweikhardt 	KASSERT((p->p_numksegrps == 1), ("Multiple ksegrps in wait1()"));
590696058c3SJulian Elischer 	FOREACH_THREAD_IN_PROC(p, td) {
591696058c3SJulian Elischer 		if (td->td_standin != NULL) {
592b75b0311SJulian Elischer 			if (td->td_standin->td_ucred != NULL) {
593b75b0311SJulian Elischer 				crfree(td->td_standin->td_ucred);
594b75b0311SJulian Elischer 				td->td_standin->td_ucred = NULL;
595b75b0311SJulian Elischer 			}
596696058c3SJulian Elischer 			thread_free(td->td_standin);
597696058c3SJulian Elischer 			td->td_standin = NULL;
598696058c3SJulian Elischer 		}
599696058c3SJulian Elischer 		cpu_thread_clean(td);
600ed062c8dSJulian Elischer 		crfree(td->td_ucred);
601696058c3SJulian Elischer 	}
602696058c3SJulian Elischer 	thread_reap();	/* check for zombie threads etc. */
603696058c3SJulian Elischer }
604696058c3SJulian Elischer 
605696058c3SJulian Elischer /*
60644990b8cSJulian Elischer  * Link a thread to a process.
6071faf202eSJulian Elischer  * set up anything that needs to be initialized for it to
6081faf202eSJulian Elischer  * be used by the process.
60944990b8cSJulian Elischer  *
61044990b8cSJulian Elischer  * Note that we do not link to the proc's ucred here.
61144990b8cSJulian Elischer  * The thread is linked as if running but no KSE assigned.
612ed062c8dSJulian Elischer  * Called from:
613ed062c8dSJulian Elischer  *  proc_linkup()
614ed062c8dSJulian Elischer  *  thread_schedule_upcall()
615ed062c8dSJulian Elischer  *  thr_create()
61644990b8cSJulian Elischer  */
61744990b8cSJulian Elischer void
61844990b8cSJulian Elischer thread_link(struct thread *td, struct ksegrp *kg)
61944990b8cSJulian Elischer {
62044990b8cSJulian Elischer 	struct proc *p;
62144990b8cSJulian Elischer 
62244990b8cSJulian Elischer 	p = kg->kg_proc;
62371fad9fdSJulian Elischer 	td->td_state    = TDS_INACTIVE;
62444990b8cSJulian Elischer 	td->td_proc     = p;
62544990b8cSJulian Elischer 	td->td_ksegrp   = kg;
6265215b187SJeff Roberson 	td->td_flags    = 0;
6274fc21c09SDaniel Eischen 	td->td_kflags	= 0;
62844990b8cSJulian Elischer 
6291faf202eSJulian Elischer 	LIST_INIT(&td->td_contested);
6309104847fSDavid Xu 	sigqueue_init(&td->td_sigqueue, p);
631c06eb4e2SSam Leffler 	callout_init(&td->td_slpcallout, CALLOUT_MPSAFE);
63244990b8cSJulian Elischer 	TAILQ_INSERT_HEAD(&p->p_threads, td, td_plist);
63344990b8cSJulian Elischer 	TAILQ_INSERT_HEAD(&kg->kg_threads, td, td_kglist);
63444990b8cSJulian Elischer 	p->p_numthreads++;
63544990b8cSJulian Elischer 	kg->kg_numthreads++;
63644990b8cSJulian Elischer }
63744990b8cSJulian Elischer 
638ed062c8dSJulian Elischer /*
639e5bedcefSJulian Elischer  * Convert a process with one thread to an unthreaded process.
640e5bedcefSJulian Elischer  * Called from:
641e5bedcefSJulian Elischer  *  thread_single(exit)  (called from execve and exit)
642e5bedcefSJulian Elischer  *  kse_exit()		XXX may need cleaning up wrt KSE stuff
643e5bedcefSJulian Elischer  */
644e5bedcefSJulian Elischer void
645e5bedcefSJulian Elischer thread_unthread(struct thread *td)
646e5bedcefSJulian Elischer {
647e5bedcefSJulian Elischer 	struct proc *p = td->td_proc;
648e5bedcefSJulian Elischer 
649e5bedcefSJulian Elischer 	KASSERT((p->p_numthreads == 1), ("Unthreading with >1 threads"));
650e5bedcefSJulian Elischer 	upcall_remove(td);
651e5bedcefSJulian Elischer 	p->p_flag &= ~(P_SA|P_HADTHREADS);
652e5bedcefSJulian Elischer 	td->td_mailbox = NULL;
653e5bedcefSJulian Elischer 	td->td_pflags &= ~(TDP_SA | TDP_CAN_UNBIND);
654e5bedcefSJulian Elischer 	if (td->td_standin != NULL) {
655e5bedcefSJulian Elischer 		thread_stash(td->td_standin);
656e5bedcefSJulian Elischer 		td->td_standin = NULL;
657e5bedcefSJulian Elischer 	}
658e5bedcefSJulian Elischer 	sched_set_concurrency(td->td_ksegrp, 1);
659e5bedcefSJulian Elischer }
660e5bedcefSJulian Elischer 
661e5bedcefSJulian Elischer /*
662ed062c8dSJulian Elischer  * Called from:
663ed062c8dSJulian Elischer  *  thread_exit()
664ed062c8dSJulian Elischer  */
665d3a0bd78SJulian Elischer void
666d3a0bd78SJulian Elischer thread_unlink(struct thread *td)
667d3a0bd78SJulian Elischer {
668d3a0bd78SJulian Elischer 	struct proc *p = td->td_proc;
669d3a0bd78SJulian Elischer 	struct ksegrp *kg = td->td_ksegrp;
670d3a0bd78SJulian Elischer 
671112afcb2SJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED);
672d3a0bd78SJulian Elischer 	TAILQ_REMOVE(&p->p_threads, td, td_plist);
673d3a0bd78SJulian Elischer 	p->p_numthreads--;
674d3a0bd78SJulian Elischer 	TAILQ_REMOVE(&kg->kg_threads, td, td_kglist);
675d3a0bd78SJulian Elischer 	kg->kg_numthreads--;
676d3a0bd78SJulian Elischer 	/* could clear a few other things here */
677ed062c8dSJulian Elischer 	/* Must  NOT clear links to proc and ksegrp! */
6785c8329edSJulian Elischer }
6795c8329edSJulian Elischer 
6805215b187SJeff Roberson /*
68144990b8cSJulian Elischer  * Enforce single-threading.
68244990b8cSJulian Elischer  *
68344990b8cSJulian Elischer  * Returns 1 if the caller must abort (another thread is waiting to
68444990b8cSJulian Elischer  * exit the process or similar). Process is locked!
68544990b8cSJulian Elischer  * Returns 0 when you are successfully the only thread running.
68644990b8cSJulian Elischer  * A process has successfully single threaded in the suspend mode when
68744990b8cSJulian Elischer  * There are no threads in user mode. Threads in the kernel must be
68844990b8cSJulian Elischer  * allowed to continue until they get to the user boundary. They may even
68944990b8cSJulian Elischer  * copy out their return values and data before suspending. They may however be
69044990b8cSJulian Elischer  * accellerated in reaching the user boundary as we will wake up
69144990b8cSJulian Elischer  * any sleeping threads that are interruptable. (PCATCH).
69244990b8cSJulian Elischer  */
69344990b8cSJulian Elischer int
694906ac69dSDavid Xu thread_single(int mode)
69544990b8cSJulian Elischer {
69644990b8cSJulian Elischer 	struct thread *td;
69744990b8cSJulian Elischer 	struct thread *td2;
69844990b8cSJulian Elischer 	struct proc *p;
699ec008e96SDavid Xu 	int remaining;
70044990b8cSJulian Elischer 
70144990b8cSJulian Elischer 	td = curthread;
70244990b8cSJulian Elischer 	p = td->td_proc;
70337814395SPeter Wemm 	mtx_assert(&Giant, MA_NOTOWNED);
70444990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
70544990b8cSJulian Elischer 	KASSERT((td != NULL), ("curthread is NULL"));
70644990b8cSJulian Elischer 
707ed062c8dSJulian Elischer 	if ((p->p_flag & P_HADTHREADS) == 0)
70844990b8cSJulian Elischer 		return (0);
70944990b8cSJulian Elischer 
710e3b9bf71SJulian Elischer 	/* Is someone already single threading? */
711906ac69dSDavid Xu 	if (p->p_singlethread != NULL && p->p_singlethread != td)
71244990b8cSJulian Elischer 		return (1);
71344990b8cSJulian Elischer 
714906ac69dSDavid Xu 	if (mode == SINGLE_EXIT) {
715906ac69dSDavid Xu 		p->p_flag |= P_SINGLE_EXIT;
716906ac69dSDavid Xu 		p->p_flag &= ~P_SINGLE_BOUNDARY;
717906ac69dSDavid Xu 	} else {
718906ac69dSDavid Xu 		p->p_flag &= ~P_SINGLE_EXIT;
719906ac69dSDavid Xu 		if (mode == SINGLE_BOUNDARY)
720906ac69dSDavid Xu 			p->p_flag |= P_SINGLE_BOUNDARY;
721906ac69dSDavid Xu 		else
722906ac69dSDavid Xu 			p->p_flag &= ~P_SINGLE_BOUNDARY;
723906ac69dSDavid Xu 	}
7241279572aSDavid Xu 	p->p_flag |= P_STOPPED_SINGLE;
72571fad9fdSJulian Elischer 	mtx_lock_spin(&sched_lock);
726112afcb2SJohn Baldwin 	p->p_singlethread = td;
727906ac69dSDavid Xu 	if (mode == SINGLE_EXIT)
728ec008e96SDavid Xu 		remaining = p->p_numthreads;
729906ac69dSDavid Xu 	else if (mode == SINGLE_BOUNDARY)
730906ac69dSDavid Xu 		remaining = p->p_numthreads - p->p_boundary_count;
731906ac69dSDavid Xu 	else
732ec008e96SDavid Xu 		remaining = p->p_numthreads - p->p_suspcount;
733ec008e96SDavid Xu 	while (remaining != 1) {
73444990b8cSJulian Elischer 		FOREACH_THREAD_IN_PROC(p, td2) {
73544990b8cSJulian Elischer 			if (td2 == td)
73644990b8cSJulian Elischer 				continue;
737588257e8SDavid Xu 			td2->td_flags |= TDF_ASTPENDING;
73871fad9fdSJulian Elischer 			if (TD_IS_INHIBITED(td2)) {
739906ac69dSDavid Xu 				switch (mode) {
740906ac69dSDavid Xu 				case SINGLE_EXIT:
741cbf4e354SDavid Xu 					if (td->td_flags & TDF_DBSUSPEND)
742cbf4e354SDavid Xu 						td->td_flags &= ~TDF_DBSUSPEND;
743906ac69dSDavid Xu 					if (TD_IS_SUSPENDED(td2))
74471fad9fdSJulian Elischer 						thread_unsuspend_one(td2);
74533862f40SDavid Xu 					if (TD_ON_SLEEPQ(td2) &&
746906ac69dSDavid Xu 					    (td2->td_flags & TDF_SINTR))
74744f3b092SJohn Baldwin 						sleepq_abort(td2);
748906ac69dSDavid Xu 					break;
749906ac69dSDavid Xu 				case SINGLE_BOUNDARY:
750906ac69dSDavid Xu 					if (TD_IS_SUSPENDED(td2) &&
751906ac69dSDavid Xu 					    !(td2->td_flags & TDF_BOUNDARY))
752906ac69dSDavid Xu 						thread_unsuspend_one(td2);
753906ac69dSDavid Xu 					if (TD_ON_SLEEPQ(td2) &&
754906ac69dSDavid Xu 					    (td2->td_flags & TDF_SINTR))
755906ac69dSDavid Xu 						sleepq_abort(td2);
756906ac69dSDavid Xu 					break;
757906ac69dSDavid Xu 				default:
7589d102777SJulian Elischer 					if (TD_IS_SUSPENDED(td2))
7599d102777SJulian Elischer 						continue;
7605215b187SJeff Roberson 					/*
7615215b187SJeff Roberson 					 * maybe other inhibitted states too?
7625215b187SJeff Roberson 					 */
7638acf6057SDavid Xu 					if ((td2->td_flags & TDF_SINTR) &&
7648acf6057SDavid Xu 					    (td2->td_inhibitors &
7658acf6057SDavid Xu 					    (TDI_SLEEPING | TDI_SWAPPED)))
7669d102777SJulian Elischer 						thread_suspend_one(td2);
767906ac69dSDavid Xu 					break;
76844990b8cSJulian Elischer 				}
76944990b8cSJulian Elischer 			}
7709d102777SJulian Elischer 		}
771906ac69dSDavid Xu 		if (mode == SINGLE_EXIT)
772ec008e96SDavid Xu 			remaining = p->p_numthreads;
773906ac69dSDavid Xu 		else if (mode == SINGLE_BOUNDARY)
774906ac69dSDavid Xu 			remaining = p->p_numthreads - p->p_boundary_count;
775ec008e96SDavid Xu 		else
776ec008e96SDavid Xu 			remaining = p->p_numthreads - p->p_suspcount;
777ec008e96SDavid Xu 
7789d102777SJulian Elischer 		/*
7799d102777SJulian Elischer 		 * Maybe we suspended some threads.. was it enough?
7809d102777SJulian Elischer 		 */
781ec008e96SDavid Xu 		if (remaining == 1)
7829d102777SJulian Elischer 			break;
7839d102777SJulian Elischer 
78444990b8cSJulian Elischer 		/*
78544990b8cSJulian Elischer 		 * Wake us up when everyone else has suspended.
786e3b9bf71SJulian Elischer 		 * In the mean time we suspend as well.
78744990b8cSJulian Elischer 		 */
78871fad9fdSJulian Elischer 		thread_suspend_one(td);
78944990b8cSJulian Elischer 		PROC_UNLOCK(p);
790bf0acc27SJohn Baldwin 		mi_switch(SW_VOL, NULL);
79144990b8cSJulian Elischer 		mtx_unlock_spin(&sched_lock);
79244990b8cSJulian Elischer 		PROC_LOCK(p);
793112afcb2SJohn Baldwin 		mtx_lock_spin(&sched_lock);
794906ac69dSDavid Xu 		if (mode == SINGLE_EXIT)
795ec008e96SDavid Xu 			remaining = p->p_numthreads;
796906ac69dSDavid Xu 		else if (mode == SINGLE_BOUNDARY)
797906ac69dSDavid Xu 			remaining = p->p_numthreads - p->p_boundary_count;
798ec008e96SDavid Xu 		else
799ec008e96SDavid Xu 			remaining = p->p_numthreads - p->p_suspcount;
80044990b8cSJulian Elischer 	}
801906ac69dSDavid Xu 	if (mode == SINGLE_EXIT) {
80291599697SJulian Elischer 		/*
80391599697SJulian Elischer 		 * We have gotten rid of all the other threads and we
80491599697SJulian Elischer 		 * are about to either exit or exec. In either case,
80591599697SJulian Elischer 		 * we try our utmost  to revert to being a non-threaded
80691599697SJulian Elischer 		 * process.
80791599697SJulian Elischer 		 */
808ed062c8dSJulian Elischer 		p->p_singlethread = NULL;
80964895117SDavid Xu 		p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT);
810e5bedcefSJulian Elischer 		thread_unthread(td);
81191599697SJulian Elischer 	}
812112afcb2SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
81344990b8cSJulian Elischer 	return (0);
81444990b8cSJulian Elischer }
81544990b8cSJulian Elischer 
81644990b8cSJulian Elischer /*
81744990b8cSJulian Elischer  * Called in from locations that can safely check to see
81844990b8cSJulian Elischer  * whether we have to suspend or at least throttle for a
81944990b8cSJulian Elischer  * single-thread event (e.g. fork).
82044990b8cSJulian Elischer  *
82144990b8cSJulian Elischer  * Such locations include userret().
82244990b8cSJulian Elischer  * If the "return_instead" argument is non zero, the thread must be able to
82344990b8cSJulian Elischer  * accept 0 (caller may continue), or 1 (caller must abort) as a result.
82444990b8cSJulian Elischer  *
82544990b8cSJulian Elischer  * The 'return_instead' argument tells the function if it may do a
82644990b8cSJulian Elischer  * thread_exit() or suspend, or whether the caller must abort and back
82744990b8cSJulian Elischer  * out instead.
82844990b8cSJulian Elischer  *
82944990b8cSJulian Elischer  * If the thread that set the single_threading request has set the
83044990b8cSJulian Elischer  * P_SINGLE_EXIT bit in the process flags then this call will never return
83144990b8cSJulian Elischer  * if 'return_instead' is false, but will exit.
83244990b8cSJulian Elischer  *
83344990b8cSJulian Elischer  * P_SINGLE_EXIT | return_instead == 0| return_instead != 0
83444990b8cSJulian Elischer  *---------------+--------------------+---------------------
83544990b8cSJulian Elischer  *       0       | returns 0          |   returns 0 or 1
83644990b8cSJulian Elischer  *               | when ST ends       |   immediatly
83744990b8cSJulian Elischer  *---------------+--------------------+---------------------
83844990b8cSJulian Elischer  *       1       | thread exits       |   returns 1
83944990b8cSJulian Elischer  *               |                    |  immediatly
84044990b8cSJulian Elischer  * 0 = thread_exit() or suspension ok,
84144990b8cSJulian Elischer  * other = return error instead of stopping the thread.
84244990b8cSJulian Elischer  *
84344990b8cSJulian Elischer  * While a full suspension is under effect, even a single threading
84444990b8cSJulian Elischer  * thread would be suspended if it made this call (but it shouldn't).
84544990b8cSJulian Elischer  * This call should only be made from places where
84644990b8cSJulian Elischer  * thread_exit() would be safe as that may be the outcome unless
84744990b8cSJulian Elischer  * return_instead is set.
84844990b8cSJulian Elischer  */
84944990b8cSJulian Elischer int
85044990b8cSJulian Elischer thread_suspend_check(int return_instead)
85144990b8cSJulian Elischer {
852ecafb24bSJuli Mallett 	struct thread *td;
853ecafb24bSJuli Mallett 	struct proc *p;
85444990b8cSJulian Elischer 
85544990b8cSJulian Elischer 	td = curthread;
85644990b8cSJulian Elischer 	p = td->td_proc;
85737814395SPeter Wemm 	mtx_assert(&Giant, MA_NOTOWNED);
85844990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
859cbf4e354SDavid Xu 	while (P_SHOULDSTOP(p) ||
860cbf4e354SDavid Xu 	      ((p->p_flag & P_TRACED) && (td->td_flags & TDF_DBSUSPEND))) {
8611279572aSDavid Xu 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
86244990b8cSJulian Elischer 			KASSERT(p->p_singlethread != NULL,
86344990b8cSJulian Elischer 			    ("singlethread not set"));
86444990b8cSJulian Elischer 			/*
865e3b9bf71SJulian Elischer 			 * The only suspension in action is a
866e3b9bf71SJulian Elischer 			 * single-threading. Single threader need not stop.
867b6d5995eSJulian Elischer 			 * XXX Should be safe to access unlocked
868b6d5995eSJulian Elischer 			 * as it can only be set to be true by us.
86944990b8cSJulian Elischer 			 */
870e3b9bf71SJulian Elischer 			if (p->p_singlethread == td)
87144990b8cSJulian Elischer 				return (0);	/* Exempt from stopping. */
87244990b8cSJulian Elischer 		}
87345a4bfa1SDavid Xu 		if ((p->p_flag & P_SINGLE_EXIT) && return_instead)
87444990b8cSJulian Elischer 			return (1);
87544990b8cSJulian Elischer 
876906ac69dSDavid Xu 		/* Should we goto user boundary if we didn't come from there? */
877906ac69dSDavid Xu 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE &&
878906ac69dSDavid Xu 		    (p->p_flag & P_SINGLE_BOUNDARY) && return_instead)
879906ac69dSDavid Xu 			return (1);
880906ac69dSDavid Xu 
8819104847fSDavid Xu 		/* If thread will exit, flush its pending signals */
8829104847fSDavid Xu 		if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td))
8839104847fSDavid Xu 			sigqueue_flush(&td->td_sigqueue);
8849104847fSDavid Xu 
885e574e444SDavid Xu 		mtx_lock_spin(&sched_lock);
886e574e444SDavid Xu 		thread_stopped(p);
88744990b8cSJulian Elischer 		/*
88844990b8cSJulian Elischer 		 * If the process is waiting for us to exit,
88944990b8cSJulian Elischer 		 * this thread should just suicide.
8901279572aSDavid Xu 		 * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE.
89144990b8cSJulian Elischer 		 */
892906ac69dSDavid Xu 		if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td))
89344990b8cSJulian Elischer 			thread_exit();
89444990b8cSJulian Elischer 
89544990b8cSJulian Elischer 		/*
89644990b8cSJulian Elischer 		 * When a thread suspends, it just
89744990b8cSJulian Elischer 		 * moves to the processes's suspend queue
89844990b8cSJulian Elischer 		 * and stays there.
89944990b8cSJulian Elischer 		 */
90071fad9fdSJulian Elischer 		thread_suspend_one(td);
901906ac69dSDavid Xu 		if (return_instead == 0) {
902906ac69dSDavid Xu 			p->p_boundary_count++;
903906ac69dSDavid Xu 			td->td_flags |= TDF_BOUNDARY;
904cf19bf91SJulian Elischer 		}
905906ac69dSDavid Xu 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
906906ac69dSDavid Xu 			if (p->p_numthreads == p->p_suspcount)
907906ac69dSDavid Xu 				thread_unsuspend_one(p->p_singlethread);
908cf19bf91SJulian Elischer 		}
909a6f37ac9SJohn Baldwin 		PROC_UNLOCK(p);
910bf0acc27SJohn Baldwin 		mi_switch(SW_INVOL, NULL);
911906ac69dSDavid Xu 		if (return_instead == 0) {
912906ac69dSDavid Xu 			p->p_boundary_count--;
913906ac69dSDavid Xu 			td->td_flags &= ~TDF_BOUNDARY;
914906ac69dSDavid Xu 		}
91544990b8cSJulian Elischer 		mtx_unlock_spin(&sched_lock);
91644990b8cSJulian Elischer 		PROC_LOCK(p);
91744990b8cSJulian Elischer 	}
91844990b8cSJulian Elischer 	return (0);
91944990b8cSJulian Elischer }
92044990b8cSJulian Elischer 
92135c32a76SDavid Xu void
92235c32a76SDavid Xu thread_suspend_one(struct thread *td)
92335c32a76SDavid Xu {
92435c32a76SDavid Xu 	struct proc *p = td->td_proc;
92535c32a76SDavid Xu 
92635c32a76SDavid Xu 	mtx_assert(&sched_lock, MA_OWNED);
927112afcb2SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
928e574e444SDavid Xu 	KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
92935c32a76SDavid Xu 	p->p_suspcount++;
93071fad9fdSJulian Elischer 	TD_SET_SUSPENDED(td);
93135c32a76SDavid Xu 	TAILQ_INSERT_TAIL(&p->p_suspended, td, td_runq);
93235c32a76SDavid Xu }
93335c32a76SDavid Xu 
93435c32a76SDavid Xu void
93535c32a76SDavid Xu thread_unsuspend_one(struct thread *td)
93635c32a76SDavid Xu {
93735c32a76SDavid Xu 	struct proc *p = td->td_proc;
93835c32a76SDavid Xu 
93935c32a76SDavid Xu 	mtx_assert(&sched_lock, MA_OWNED);
940112afcb2SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
94135c32a76SDavid Xu 	TAILQ_REMOVE(&p->p_suspended, td, td_runq);
94271fad9fdSJulian Elischer 	TD_CLR_SUSPENDED(td);
94335c32a76SDavid Xu 	p->p_suspcount--;
94471fad9fdSJulian Elischer 	setrunnable(td);
94535c32a76SDavid Xu }
94635c32a76SDavid Xu 
94744990b8cSJulian Elischer /*
94844990b8cSJulian Elischer  * Allow all threads blocked by single threading to continue running.
94944990b8cSJulian Elischer  */
95044990b8cSJulian Elischer void
95144990b8cSJulian Elischer thread_unsuspend(struct proc *p)
95244990b8cSJulian Elischer {
95344990b8cSJulian Elischer 	struct thread *td;
95444990b8cSJulian Elischer 
955b6d5995eSJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
95644990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
95744990b8cSJulian Elischer 	if (!P_SHOULDSTOP(p)) {
95844990b8cSJulian Elischer 		while ((td = TAILQ_FIRST(&p->p_suspended))) {
95935c32a76SDavid Xu 			thread_unsuspend_one(td);
96044990b8cSJulian Elischer 		}
9611279572aSDavid Xu 	} else if ((P_SHOULDSTOP(p) == P_STOPPED_SINGLE) &&
96244990b8cSJulian Elischer 	    (p->p_numthreads == p->p_suspcount)) {
96344990b8cSJulian Elischer 		/*
96444990b8cSJulian Elischer 		 * Stopping everything also did the job for the single
96544990b8cSJulian Elischer 		 * threading request. Now we've downgraded to single-threaded,
96644990b8cSJulian Elischer 		 * let it continue.
96744990b8cSJulian Elischer 		 */
96835c32a76SDavid Xu 		thread_unsuspend_one(p->p_singlethread);
96944990b8cSJulian Elischer 	}
97044990b8cSJulian Elischer }
97144990b8cSJulian Elischer 
972ed062c8dSJulian Elischer /*
973ed062c8dSJulian Elischer  * End the single threading mode..
974ed062c8dSJulian Elischer  */
97544990b8cSJulian Elischer void
97644990b8cSJulian Elischer thread_single_end(void)
97744990b8cSJulian Elischer {
97844990b8cSJulian Elischer 	struct thread *td;
97944990b8cSJulian Elischer 	struct proc *p;
98044990b8cSJulian Elischer 
98144990b8cSJulian Elischer 	td = curthread;
98244990b8cSJulian Elischer 	p = td->td_proc;
98344990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
984906ac69dSDavid Xu 	p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_SINGLE_BOUNDARY);
985112afcb2SJohn Baldwin 	mtx_lock_spin(&sched_lock);
98644990b8cSJulian Elischer 	p->p_singlethread = NULL;
98721fc3164SDavid Xu 	p->p_procscopegrp = NULL;
98849539972SJulian Elischer 	/*
98949539972SJulian Elischer 	 * If there are other threads they mey now run,
99049539972SJulian Elischer 	 * unless of course there is a blanket 'stop order'
99149539972SJulian Elischer 	 * on the process. The single threader must be allowed
99249539972SJulian Elischer 	 * to continue however as this is a bad place to stop.
99349539972SJulian Elischer 	 */
99449539972SJulian Elischer 	if ((p->p_numthreads != 1) && (!P_SHOULDSTOP(p))) {
99549539972SJulian Elischer 		while ((td = TAILQ_FIRST(&p->p_suspended))) {
99671fad9fdSJulian Elischer 			thread_unsuspend_one(td);
99744990b8cSJulian Elischer 		}
99849539972SJulian Elischer 	}
999112afcb2SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
100049539972SJulian Elischer }
10014fc21c09SDaniel Eischen 
1002007ddf7eSJohn Baldwin /*
1003007ddf7eSJohn Baldwin  * Called before going into an interruptible sleep to see if we have been
1004007ddf7eSJohn Baldwin  * interrupted or requested to exit.
1005007ddf7eSJohn Baldwin  */
1006007ddf7eSJohn Baldwin int
1007007ddf7eSJohn Baldwin thread_sleep_check(struct thread *td)
1008007ddf7eSJohn Baldwin {
1009007ddf7eSJohn Baldwin 	struct proc *p;
1010007ddf7eSJohn Baldwin 
1011007ddf7eSJohn Baldwin 	p = td->td_proc;
1012007ddf7eSJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED);
10132179a22cSJulian Elischer 	if (p->p_flag & P_HADTHREADS) {
1014906ac69dSDavid Xu 		if (p->p_singlethread != td) {
1015906ac69dSDavid Xu 			if (p->p_flag & P_SINGLE_EXIT)
1016007ddf7eSJohn Baldwin 				return (EINTR);
1017906ac69dSDavid Xu 			if (p->p_flag & P_SINGLE_BOUNDARY)
1018906ac69dSDavid Xu 				return (ERESTART);
1019906ac69dSDavid Xu 		}
1020007ddf7eSJohn Baldwin 		if (td->td_flags & TDF_INTERRUPT)
1021007ddf7eSJohn Baldwin 			return (td->td_intrval);
1022007ddf7eSJohn Baldwin 	}
1023007ddf7eSJohn Baldwin 	return (0);
1024007ddf7eSJohn Baldwin }
1025