29 #include "opt_witness.h"
30 #include "opt_kdtrace.h"
31 #include "opt_hwpmc_hooks.h"
33 #include <sys/cdefs.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/mutex.h>
42 #include <sys/rangelock.h>
43 #include <sys/resourcevar.h>
46 #include <sys/sched.h>
47 #include <sys/sleepqueue.h>
48 #include <sys/selinfo.h>
49 #include <sys/turnstile.h>
51 #include <sys/rwlock.h>
53 #include <sys/cpuset.h>
55 #include <sys/pmckern.h>
58 #include <security/audit/audit.h>
61 #include <vm/vm_extern.h>
63 #include <sys/eventhandler.h>
74 TAILQ_HEAD(, thread) zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads);
75 static struct mtx zombie_lock;
76 MTX_SYSINIT(zombie_lock, &zombie_lock, "zombie lock", MTX_SPIN);
80 #define TID_BUFFER_SIZE 1024
83 static struct unrhdr *tid_unrhdr;
85 static int tid_head, tid_tail;
88 struct tidhashhead *tidhashtbl;
90 struct rwlock tidhash_lock;
101 if (tid_head == tid_tail) {
102 mtx_unlock(&tid_lock);
105 tid = tid_buffer[tid_head];
107 mtx_unlock(&tid_lock);
114 lwpid_t tmp_tid = -1;
118 tmp_tid = tid_buffer[tid_head];
121 tid_buffer[tid_tail] = tid;
123 mtx_unlock(&tid_lock);
136 td = (
struct thread *)mem;
137 td->td_state = TDS_INACTIVE;
138 td->td_oncpu = NOCPU;
140 td->td_tid = tid_alloc();
148 td->td_lend_user_pri = PRI_MAX;
151 audit_thread_alloc(td);
165 td = (
struct thread *)mem;
169 switch (td->td_state) {
178 panic(
"bad state for thread unlinking");
183 panic(
"bad thread state");
188 audit_thread_free(td);
205 td = (
struct thread *)mem;
210 td->td_vp_reserv = 0;
212 td->td_sched = (
struct td_sched *)&td[1];
227 td = (
struct thread *)mem;
247 TAILQ_INIT(&p->p_threads);
257 if (p->p_ksi != NULL) {
259 p->p_ksi->ksi_flags = KSI_EXT | KSI_INS;
261 LIST_INIT(&p->p_mqnotifier);
273 mtx_init(&tid_lock,
"TID lock", NULL, MTX_DEF);
279 tid_unrhdr =
new_unrhdr(PID_MAX + 2, INT_MAX, &tid_lock);
285 rw_init(&tidhash_lock,
"tidhash");
295 mtx_lock_spin(&zombie_lock);
296 TAILQ_INSERT_HEAD(&zombie_threads, td, td_slpq);
297 mtx_unlock_spin(&zombie_lock);
306 atomic_subtract_rel_int(&td->td_proc->p_exitthreads, 1);
316 struct thread *td_first, *td_next;
322 if (!TAILQ_EMPTY(&zombie_threads)) {
323 mtx_lock_spin(&zombie_lock);
324 td_first = TAILQ_FIRST(&zombie_threads);
326 TAILQ_INIT(&zombie_threads);
327 mtx_unlock_spin(&zombie_lock);
329 td_next = TAILQ_NEXT(td_first, td_slpq);
330 if (td_first->td_ucred)
331 crfree(td_first->td_ucred);
348 td = (
struct thread *)uma_zalloc(
thread_zone, M_WAITOK);
349 KASSERT(td->td_kstack == 0, (
"thread_alloc got thread with kstack"));
350 if (!vm_thread_new(td, pages)) {
354 cpu_thread_alloc(td);
362 KASSERT(td->td_kstack == 0,
363 (
"thread_alloc_stack called on a thread with kstack"));
364 if (!vm_thread_new(td, pages))
366 cpu_thread_alloc(td);
377 lock_profile_thread_exit(td);
380 td->td_cpuset = NULL;
382 if (td->td_kstack != 0)
383 vm_thread_dispose(td);
399 uint64_t runtime, new_switchtime;
408 PROC_SLOCK_ASSERT(p, MA_OWNED);
409 mtx_assert(&
Giant, MA_NOTOWNED);
411 PROC_LOCK_ASSERT(p, MA_OWNED);
412 KASSERT(p != NULL, (
"thread exiting without a process"));
413 CTR3(KTR_PROC,
"thread_exit: thread %p (pid %ld, %s)", td,
414 (
long)p->p_pid, td->td_name);
415 KASSERT(TAILQ_EMPTY(&td->td_sigqueue.sq_list), (
"signal pending"));
418 AUDIT_SYSCALL_EXIT(0, td);
436 if (p->p_flag & P_HADTHREADS) {
437 if (p->p_numthreads > 1) {
439 td2 = FIRST_THREAD_IN_PROC(p);
447 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
448 if (p->p_numthreads == p->p_suspcount) {
449 thread_lock(p->p_singlethread);
452 thread_unlock(p->p_singlethread);
458 atomic_add_int(&td->td_proc->p_exitthreads, 1);
459 PCPU_SET(deadthread, td);
464 panic (
"thread_exit: Last thread exiting on its own");
472 if (PMC_PROC_IS_USING_PMCS(td->td_proc))
473 PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT);
479 runtime = new_switchtime - PCPU_GET(switchtime);
480 td->td_runtime += runtime;
481 td->td_incruntime += runtime;
482 PCPU_SET(switchtime, new_switchtime);
483 PCPU_SET(switchticks,
ticks);
484 PCPU_INC(cnt.v_swtch);
487 td->td_ru.ru_nvcsw++;
493 td->td_state = TDS_INACTIVE;
497 CTR1(KTR_PROC,
"thread_exit: cpu_throw() thread %p", td);
499 panic(
"I'm a teapot!");
512 mtx_assert(&
Giant, MA_NOTOWNED);
513 KASSERT((p->p_numthreads == 1), (
"Multiple threads in wait1()"));
514 td = FIRST_THREAD_IN_PROC(p);
519 while (p->p_exitthreads)
521 lock_profile_thread_exit(td);
523 td->td_cpuset = NULL;
524 cpu_thread_clean(td);
543 td->td_state = TDS_INACTIVE;
545 td->td_flags = TDF_INMEM;
547 LIST_INIT(&td->td_contested);
548 LIST_INIT(&td->td_lprof[0]);
549 LIST_INIT(&td->td_lprof[1]);
552 TAILQ_INSERT_HEAD(&p->p_threads, td, td_plist);
562 struct proc *p = td->td_proc;
564 KASSERT((p->p_numthreads == 1), (
"Unthreading with >1 threads"));
565 p->p_flag &= ~P_HADTHREADS;
575 struct proc *p = td->td_proc;
577 PROC_LOCK_ASSERT(p, MA_OWNED);
578 TAILQ_REMOVE(&p->p_threads, td, td_plist);
589 PROC_LOCK_ASSERT(p, MA_OWNED);
590 PROC_SLOCK_ASSERT(p, MA_OWNED);
591 if (mode == SINGLE_EXIT)
592 remaining = p->p_numthreads;
593 else if (mode == SINGLE_BOUNDARY)
594 remaining = p->p_numthreads - p->p_boundary_count;
595 else if (mode == SINGLE_NO_EXIT)
596 remaining = p->p_numthreads - p->p_suspcount;
598 panic(
"calc_remaining: wrong mode %d", mode);
621 int remaining, wakeup_swapper;
625 mtx_assert(&
Giant, MA_NOTOWNED);
626 PROC_LOCK_ASSERT(p, MA_OWNED);
628 if ((p->p_flag & P_HADTHREADS) == 0)
632 if (p->p_singlethread != NULL && p->p_singlethread != td)
635 if (mode == SINGLE_EXIT) {
636 p->p_flag |= P_SINGLE_EXIT;
637 p->p_flag &= ~P_SINGLE_BOUNDARY;
639 p->p_flag &= ~P_SINGLE_EXIT;
640 if (mode == SINGLE_BOUNDARY)
641 p->p_flag |= P_SINGLE_BOUNDARY;
643 p->p_flag &= ~P_SINGLE_BOUNDARY;
645 p->p_flag |= P_STOPPED_SINGLE;
647 p->p_singlethread = td;
649 while (remaining != 1) {
650 if (P_SHOULDSTOP(p) != P_STOPPED_SINGLE)
653 FOREACH_THREAD_IN_PROC(p, td2) {
657 td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
658 if (TD_IS_INHIBITED(td2)) {
661 if (TD_IS_SUSPENDED(td2))
664 if (TD_ON_SLEEPQ(td2) &&
665 (td2->td_flags & TDF_SINTR))
669 case SINGLE_BOUNDARY:
670 if (TD_IS_SUSPENDED(td2) &&
671 !(td2->td_flags & TDF_BOUNDARY))
674 if (TD_ON_SLEEPQ(td2) &&
675 (td2->td_flags & TDF_SINTR))
680 if (TD_IS_SUSPENDED(td2) &&
681 !(td2->td_flags & TDF_BOUNDARY))
684 if (TD_ON_SLEEPQ(td2) &&
685 (td2->td_flags & TDF_SINTR))
694 else if (TD_IS_RUNNING(td2) && td != td2) {
718 if (mode == SINGLE_EXIT) {
725 p->p_singlethread = NULL;
726 p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT);
775 mtx_assert(&
Giant, MA_NOTOWNED);
776 PROC_LOCK_ASSERT(p, MA_OWNED);
777 while (P_SHOULDSTOP(p) ||
778 ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_SUSPEND))) {
779 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
780 KASSERT(p->p_singlethread != NULL,
781 (
"singlethread not set"));
788 if (p->p_singlethread == td)
791 if ((p->p_flag & P_SINGLE_EXIT) && return_instead)
795 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE &&
796 (p->p_flag & P_SINGLE_BOUNDARY) && return_instead)
803 if (P_SHOULDSTOP(p) == P_STOPPED_SIG &&
804 td->td_flags & TDF_SBDRY) {
805 KASSERT(return_instead,
806 (
"TDF_SBDRY set for unsafe thread_suspend_check"));
815 if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) {
827 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
828 if (p->p_numthreads == p->p_suspcount + 1) {
829 thread_lock(p->p_singlethread);
832 thread_unlock(p->p_singlethread);
844 if (return_instead == 0) {
845 p->p_boundary_count++;
846 td->td_flags |= TDF_BOUNDARY;
850 if (return_instead == 0)
851 td->td_flags &= ~TDF_BOUNDARY;
854 if (return_instead == 0) {
856 p->p_boundary_count--;
869 KASSERT(!TD_IS_SUSPENDED(td), (
"already suspended"));
870 PROC_LOCK_ASSERT(p, MA_OWNED);
871 PROC_SLOCK_ASSERT(p, MA_OWNED);
880 td->td_flags &= ~TDF_NEEDSUSPCHK;
881 TD_SET_SUSPENDED(td);
895 struct proc *p = td->td_proc;
897 PROC_SLOCK_ASSERT(p, MA_OWNED);
898 THREAD_LOCK_ASSERT(td, MA_OWNED);
899 KASSERT(!TD_IS_SUSPENDED(td), (
"already suspended"));
901 td->td_flags &= ~TDF_NEEDSUSPCHK;
902 TD_SET_SUSPENDED(td);
909 struct proc *p = td->td_proc;
911 PROC_SLOCK_ASSERT(p, MA_OWNED);
912 THREAD_LOCK_ASSERT(td, MA_OWNED);
913 KASSERT(TD_IS_SUSPENDED(td), (
"Thread not suspended"));
914 TD_CLR_SUSPENDED(td);
928 PROC_LOCK_ASSERT(p, MA_OWNED);
929 PROC_SLOCK_ASSERT(p, MA_OWNED);
931 if (!P_SHOULDSTOP(p)) {
932 FOREACH_THREAD_IN_PROC(p, td) {
934 if (TD_IS_SUSPENDED(td)) {
939 }
else if ((P_SHOULDSTOP(p) == P_STOPPED_SINGLE) &&
940 (p->p_numthreads == p->p_suspcount)) {
946 thread_lock(p->p_singlethread);
948 thread_unlock(p->p_singlethread);
966 PROC_LOCK_ASSERT(p, MA_OWNED);
967 p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_SINGLE_BOUNDARY);
969 p->p_singlethread = NULL;
977 if ((p->p_numthreads != 1) && (!P_SHOULDSTOP(p))) {
978 FOREACH_THREAD_IN_PROC(p, td) {
980 if (TD_IS_SUSPENDED(td)) {
996 PROC_LOCK_ASSERT(p, MA_OWNED);
997 FOREACH_THREAD_IN_PROC(p, td) {
998 if (td->td_tid == tid)
1008 #define RUN_THRESH 16
1012 rw_rlock(&tidhash_lock);
1013 LIST_FOREACH(td, TIDHASH(tid), td_hash) {
1014 if (td->td_tid == tid) {
1015 if (pid != -1 && td->td_proc->p_pid != pid) {
1019 PROC_LOCK(td->td_proc);
1020 if (td->td_proc->p_state == PRS_NEW) {
1021 PROC_UNLOCK(td->td_proc);
1026 if (rw_try_upgrade(&tidhash_lock)) {
1027 LIST_REMOVE(td, td_hash);
1028 LIST_INSERT_HEAD(TIDHASH(td->td_tid),
1030 rw_wunlock(&tidhash_lock);
1038 rw_runlock(&tidhash_lock);
1045 rw_wlock(&tidhash_lock);
1046 LIST_INSERT_HEAD(TIDHASH(td->td_tid), td, td_hash);
1047 rw_wunlock(&tidhash_lock);
1053 rw_wlock(&tidhash_lock);
1054 LIST_REMOVE(td, td_hash);
1055 rw_wunlock(&tidhash_lock);
void thread_link(struct thread *td, struct proc *p)
void umtx_thread_fini(struct thread *td)
void umtx_thread_exit(struct thread *td)
static void thread_fini(void *mem, int size)
void umtx_thread_alloc(struct thread *td)
void thread_single_end(void)
void sched_relinquish(struct thread *td)
void * hashinit(int elements, struct malloc_type *type, u_long *hashmask)
MTX_SYSINIT(et_eventtimers_init,&et_eventtimers_mtx,"et_mtx", MTX_DEF)
static void thread_dtor(void *mem, int size, void *arg)
void tdsigcleanup(struct thread *td)
void panic(const char *fmt,...)
void thread_stopped(struct proc *p)
int sched_sizeof_thread(void)
void witness_thread_exit(struct thread *td)
void thread_unsuspend(struct proc *p)
void proc_linkup(struct proc *p, struct thread *td)
int sleepq_abort(struct thread *td, int intrval)
int alloc_unr(struct unrhdr *uh)
struct turnstile * turnstile_alloc(void)
void mi_switch(int flags, struct thread *newtd)
void thread_free(struct thread *td)
void turnstile_free(struct turnstile *ts)
void rlqentry_free(struct rl_q_entry *rleq)
void sleepq_free(struct sleepqueue *sq)
struct thread * thread_alloc(int pages)
void proc_linkup0(struct proc *p, struct thread *td)
void thread_zombie(struct thread *td)
static MALLOC_DEFINE(M_GZIP,"gzip_trees","Gzip trees")
void sched_exit_thread(struct thread *td, struct thread *child)
void rucollect(struct rusage *ru, struct rusage *ru2)
void cpuset_rel(struct cpuset *set)
SDT_PROBE_DEFINE(proc,,, lwp__exit)
int setrunnable(struct thread *td)
static int thread_init(void *mem, int size, int flags)
void crfree(struct ucred *cr)
void ruxagg(struct proc *p, struct thread *td)
int thread_alloc_stack(struct thread *td, int pages)
static uma_zone_t thread_zone
static int thread_ctor(void *mem, int size, void *arg, int flags)
struct unrhdr * new_unrhdr(int low, int high, struct mtx *mutex)
void tidhash_add(struct thread *td)
int thread_single(int mode)
int thread_unsuspend_one(struct thread *td)
ksiginfo_t * ksiginfo_alloc(int wait)
static int calc_remaining(struct proc *p, int mode)
void umtx_thread_init(struct thread *td)
void sched_throw(struct thread *td)
void callout_init(struct callout *c, int mpsafe)
void thread_suspend_one(struct thread *td)
void mtx_init(struct mtx *m, const char *name, const char *type, int opts)
struct sleepqueue * sleepq_alloc(void)
void sigqueue_init(sigqueue_t *list, struct proc *p)
struct thread * thread_find(struct proc *p, lwpid_t tid)
void thread_stash(struct thread *td)
void seltdfini(struct thread *td)
void tidhash_remove(struct thread *td)
struct thread * tdfind(lwpid_t tid, pid_t pid)
void thread_unthread(struct thread *td)
SDT_PROVIDER_DECLARE(proc)
void free_unr(struct unrhdr *uh, u_int item)
static void tid_free(lwpid_t tid)
void thread_wait(struct proc *p)
int thread_suspend_check(int return_instead)
void thread_suspend_switch(struct thread *td)
void sched_sleep(struct thread *td, int pri)
void thread_unlink(struct thread *td)