#include "opt_witness.h"
#include "opt_hwpmc_hooks.h"
#include "opt_hwt_hooks.h"
#include <sys/systm.h>
#include <sys/asan.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/msan.h>
#include <sys/mutex.h>
#include <sys/proc.h>
#include <sys/bitstring.h>
#include <sys/epoch.h>
#include <sys/rangelock.h>
#include <sys/resourcevar.h>
#include <sys/sdt.h>
#include <sys/smp.h>
#include <sys/sched.h>
#include <sys/sleepqueue.h>
#include <sys/selinfo.h>
#include <sys/syscallsubr.h>
#include <sys/dtrace_bsd.h>
#include <sys/sysent.h>
#include <sys/turnstile.h>
#include <sys/taskqueue.h>
#include <sys/ktr.h>
#include <sys/rwlock.h>
#include <sys/umtxvar.h>
#include <sys/vmmeter.h>
#include <sys/cpuset.h>
#ifdef HWPMC_HOOKS
#include <sys/pmckern.h>
#endif
#ifdef HWT_HOOKS
#include <dev/hwt/hwt_hook.h>
#endif
#include <sys/priv.h>
#include <security/audit/audit.h>
#include <vm/pmap.h>
#include <vm/vm.h>
#include <vm/vm_extern.h>
#include <vm/uma.h>
#include <vm/vm_phys.h>
#include <sys/eventhandler.h>
#ifdef __amd64__
_Static_assert(offsetof(struct thread, td_flags) == 0x108,
"struct thread KBI td_flags");
_Static_assert(offsetof(struct thread, td_pflags) == 0x114,
"struct thread KBI td_pflags");
_Static_assert(offsetof(struct thread, td_frame) == 0x4e8,
"struct thread KBI td_frame");
_Static_assert(offsetof(struct thread, td_emuldata) == 0x6f0,
"struct thread KBI td_emuldata");
_Static_assert(offsetof(struct proc, p_flag) == 0xb8,
"struct proc KBI p_flag");
_Static_assert(offsetof(struct proc, p_pid) == 0xc4,
"struct proc KBI p_pid");
_Static_assert(offsetof(struct proc, p_filemon) == 0x3c8,
"struct proc KBI p_filemon");
_Static_assert(offsetof(struct proc, p_comm) == 0x3e0,
"struct proc KBI p_comm");
_Static_assert(offsetof(struct proc, p_emuldata) == 0x4d0,
"struct proc KBI p_emuldata");
#endif
#ifdef __i386__
_Static_assert(offsetof(struct thread, td_flags) == 0x9c,
"struct thread KBI td_flags");
_Static_assert(offsetof(struct thread, td_pflags) == 0xa8,
"struct thread KBI td_pflags");
_Static_assert(offsetof(struct thread, td_frame) == 0x33c,
"struct thread KBI td_frame");
_Static_assert(offsetof(struct thread, td_emuldata) == 0x380,
"struct thread KBI td_emuldata");
_Static_assert(offsetof(struct proc, p_flag) == 0x6c,
"struct proc KBI p_flag");
_Static_assert(offsetof(struct proc, p_pid) == 0x78,
"struct proc KBI p_pid");
_Static_assert(offsetof(struct proc, p_filemon) == 0x270,
"struct proc KBI p_filemon");
_Static_assert(offsetof(struct proc, p_comm) == 0x284,
"struct proc KBI p_comm");
_Static_assert(offsetof(struct proc, p_emuldata) == 0x318,
"struct proc KBI p_emuldata");
#endif
SDT_PROVIDER_DECLARE(proc);
SDT_PROBE_DEFINE(proc, , , lwp__exit);
static uma_zone_t thread_zone;
struct thread_domain_data {
struct thread *tdd_zombies;
int tdd_reapticks;
} __aligned(CACHE_LINE_SIZE);
static struct thread_domain_data thread_domain_data[MAXMEMDOM];
static struct task thread_reap_task;
static struct callout thread_reap_callout;
static void thread_zombie(struct thread *);
static void thread_reap(void);
static void thread_reap_all(void);
static void thread_reap_task_cb(void *, int);
static void thread_reap_callout_cb(void *);
static void thread_unsuspend_one(struct thread *td, struct proc *p,
bool boundary);
static void thread_free_batched(struct thread *td);
static __exclusive_cache_line struct mtx tid_lock;
static bitstr_t *tid_bitmap;
static MALLOC_DEFINE(M_TIDHASH, "tidhash", "thread hash");
static int maxthread;
SYSCTL_INT(_kern, OID_AUTO, maxthread, CTLFLAG_RDTUN,
&maxthread, 0, "Maximum number of threads");
static __exclusive_cache_line int nthreads;
static LIST_HEAD(tidhashhead, thread) *tidhashtbl;
static u_long tidhash;
static u_long tidhashlock;
static struct rwlock *tidhashtbl_lock;
#define TIDHASH(tid) (&tidhashtbl[(tid) & tidhash])
#define TIDHASHLOCK(tid) (&tidhashtbl_lock[(tid) & tidhashlock])
EVENTHANDLER_LIST_DEFINE(thread_ctor);
EVENTHANDLER_LIST_DEFINE(thread_dtor);
EVENTHANDLER_LIST_DEFINE(thread_init);
EVENTHANDLER_LIST_DEFINE(thread_fini);
static bool
thread_count_inc_try(void)
{
int nthreads_new;
nthreads_new = atomic_fetchadd_int(&nthreads, 1) + 1;
if (nthreads_new >= maxthread - 100) {
if (priv_check_cred(curthread->td_ucred, PRIV_MAXPROC) != 0 ||
nthreads_new >= maxthread) {
atomic_subtract_int(&nthreads, 1);
return (false);
}
}
return (true);
}
static bool
thread_count_inc(void)
{
static struct timeval lastfail;
static int curfail;
thread_reap();
if (thread_count_inc_try()) {
return (true);
}
thread_reap_all();
if (thread_count_inc_try()) {
return (true);
}
if (ppsratecheck(&lastfail, &curfail, 1)) {
printf("maxthread limit exceeded by uid %u "
"(pid %d); consider increasing kern.maxthread\n",
curthread->td_ucred->cr_ruid, curproc->p_pid);
}
return (false);
}
static void
thread_count_sub(int n)
{
atomic_subtract_int(&nthreads, n);
}
static void
thread_count_dec(void)
{
thread_count_sub(1);
}
static lwpid_t
tid_alloc(void)
{
static lwpid_t trytid;
lwpid_t tid;
mtx_lock(&tid_lock);
if (trytid >= maxthread)
trytid = 0;
bit_ffc_at(tid_bitmap, trytid, maxthread, &tid);
if (tid == -1) {
KASSERT(trytid != 0, ("unexpectedly ran out of IDs"));
trytid = 0;
bit_ffc_at(tid_bitmap, trytid, maxthread, &tid);
KASSERT(tid != -1, ("unexpectedly ran out of IDs"));
}
bit_set(tid_bitmap, tid);
trytid = tid + 1;
mtx_unlock(&tid_lock);
return (tid + NO_PID);
}
static void
tid_free_locked(lwpid_t rtid)
{
lwpid_t tid;
mtx_assert(&tid_lock, MA_OWNED);
KASSERT(rtid >= NO_PID,
("%s: invalid tid %d\n", __func__, rtid));
tid = rtid - NO_PID;
KASSERT(bit_test(tid_bitmap, tid) != 0,
("thread ID %d not allocated\n", rtid));
bit_clear(tid_bitmap, tid);
}
static void
tid_free(lwpid_t rtid)
{
mtx_lock(&tid_lock);
tid_free_locked(rtid);
mtx_unlock(&tid_lock);
}
static void
tid_free_batch(lwpid_t *batch, int n)
{
int i;
mtx_lock(&tid_lock);
for (i = 0; i < n; i++) {
tid_free_locked(batch[i]);
}
mtx_unlock(&tid_lock);
}
struct tidbatch {
lwpid_t tab[16];
int n;
};
static void
tidbatch_prep(struct tidbatch *tb)
{
tb->n = 0;
}
static void
tidbatch_add(struct tidbatch *tb, struct thread *td)
{
KASSERT(tb->n < nitems(tb->tab),
("%s: count too high %d", __func__, tb->n));
tb->tab[tb->n] = td->td_tid;
tb->n++;
}
static void
tidbatch_process(struct tidbatch *tb)
{
KASSERT(tb->n <= nitems(tb->tab),
("%s: count too high %d", __func__, tb->n));
if (tb->n == nitems(tb->tab)) {
tid_free_batch(tb->tab, tb->n);
tb->n = 0;
}
}
static void
tidbatch_final(struct tidbatch *tb)
{
KASSERT(tb->n <= nitems(tb->tab),
("%s: count too high %d", __func__, tb->n));
if (tb->n != 0) {
tid_free_batch(tb->tab, tb->n);
}
}
struct tdcountbatch {
int n;
};
static void
tdcountbatch_prep(struct tdcountbatch *tb)
{
tb->n = 0;
}
static void
tdcountbatch_add(struct tdcountbatch *tb, struct thread *td __unused)
{
tb->n++;
}
static void
tdcountbatch_process(struct tdcountbatch *tb)
{
if (tb->n == 32) {
thread_count_sub(tb->n);
tb->n = 0;
}
}
static void
tdcountbatch_final(struct tdcountbatch *tb)
{
if (tb->n != 0) {
thread_count_sub(tb->n);
}
}
static int
thread_ctor(void *mem, int size, void *arg, int flags)
{
struct thread *td;
td = (struct thread *)mem;
TD_SET_STATE(td, TDS_INACTIVE);
td->td_lastcpu = td->td_oncpu = NOCPU;
td->td_critnest = 1;
td->td_lend_user_pri = PRI_MAX;
#ifdef AUDIT
audit_thread_alloc(td);
#endif
#ifdef KDTRACE_HOOKS
kdtrace_thread_ctor(td);
#endif
umtx_thread_alloc(td);
MPASS(td->td_sel == NULL);
return (0);
}
static void
thread_dtor(void *mem, int size, void *arg)
{
struct thread *td;
td = (struct thread *)mem;
#ifdef INVARIANTS
switch (TD_GET_STATE(td)) {
case TDS_INHIBITED:
case TDS_RUNNING:
case TDS_CAN_RUN:
case TDS_RUNQ:
panic("bad state for thread unlinking");
case TDS_INACTIVE:
break;
default:
panic("bad thread state");
}
#endif
#ifdef AUDIT
audit_thread_free(td);
#endif
#ifdef KDTRACE_HOOKS
kdtrace_thread_dtor(td);
#endif
osd_thread_exit(td);
ast_kclear(td);
seltdfini(td);
}
static int
thread_init(void *mem, int size, int flags)
{
struct thread *td;
td = (struct thread *)mem;
td->td_allocdomain = vm_phys_domain(vtophys(td));
td->td_sleepqueue = sleepq_alloc();
td->td_turnstile = turnstile_alloc();
EVENTHANDLER_DIRECT_INVOKE(thread_init, td);
umtx_thread_init(td);
td->td_kstack = 0;
td->td_sel = NULL;
return (0);
}
static void
thread_fini(void *mem, int size)
{
struct thread *td;
td = (struct thread *)mem;
EVENTHANDLER_DIRECT_INVOKE(thread_fini, td);
turnstile_free(td->td_turnstile);
sleepq_free(td->td_sleepqueue);
umtx_thread_fini(td);
MPASS(td->td_sel == NULL);
}
void
proc_linkup0(struct proc *p, struct thread *td)
{
TAILQ_INIT(&p->p_threads);
proc_linkup(p, td);
}
void
proc_linkup(struct proc *p, struct thread *td)
{
sigqueue_init(&p->p_sigqueue, p);
p->p_ksi = ksiginfo_alloc(M_WAITOK);
if (p->p_ksi != NULL) {
p->p_ksi->ksi_flags = KSI_EXT | KSI_INS;
}
LIST_INIT(&p->p_mqnotifier);
p->p_numthreads = 0;
thread_link(td, p);
}
static void
ast_suspend(struct thread *td, int tda __unused)
{
struct proc *p;
p = td->td_proc;
PROC_LOCK(p);
thread_suspend_check(0);
PROC_UNLOCK(p);
}
extern int max_threads_per_proc;
void
threadinit(void)
{
u_long i;
lwpid_t tid0;
if (maxthread == 0) {
#ifdef _LP64
maxthread = MIN(maxproc * max_threads_per_proc, 1000000);
#else
maxthread = MIN(maxproc * max_threads_per_proc, 100000);
#endif
}
mtx_init(&tid_lock, "TID lock", NULL, MTX_DEF);
tid_bitmap = bit_alloc(maxthread, M_TIDHASH, M_WAITOK);
thread_count_inc();
tid0 = tid_alloc();
if (tid0 != THREAD0_TID)
panic("tid0 %d != %d\n", tid0, THREAD0_TID);
thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(),
thread_ctor, thread_dtor, thread_init, thread_fini,
UMA_ALIGN_CACHE_AND_MASK(32 - 1), UMA_ZONE_NOFREE);
tidhashtbl = hashinit(maxproc / 2, M_TIDHASH, &tidhash);
tidhashlock = (tidhash + 1) / 64;
if (tidhashlock > 0)
tidhashlock--;
tidhashtbl_lock = malloc(sizeof(*tidhashtbl_lock) * (tidhashlock + 1),
M_TIDHASH, M_WAITOK | M_ZERO);
for (i = 0; i < tidhashlock + 1; i++)
rw_init(&tidhashtbl_lock[i], "tidhash");
TASK_INIT(&thread_reap_task, 0, thread_reap_task_cb, NULL);
callout_init(&thread_reap_callout, 1);
callout_reset(&thread_reap_callout, 5 * hz,
thread_reap_callout_cb, NULL);
ast_register(TDA_SUSPEND, ASTR_ASTF_REQUIRED, 0, ast_suspend);
}
void
thread_zombie(struct thread *td)
{
struct thread_domain_data *tdd;
struct thread *ztd;
tdd = &thread_domain_data[td->td_allocdomain];
ztd = atomic_load_ptr(&tdd->tdd_zombies);
for (;;) {
td->td_zombie = ztd;
if (atomic_fcmpset_rel_ptr((uintptr_t *)&tdd->tdd_zombies,
(uintptr_t *)&ztd, (uintptr_t)td))
break;
continue;
}
}
void
thread_stash(struct thread *td)
{
atomic_subtract_rel_int(&td->td_proc->p_exitthreads, 1);
thread_zombie(td);
}
static void
thread_reap_domain(struct thread_domain_data *tdd)
{
struct thread *itd, *ntd;
struct tidbatch tidbatch;
struct credbatch credbatch;
struct limbatch limbatch;
struct tdcountbatch tdcountbatch;
if (tdd->tdd_zombies == NULL)
return;
itd = (struct thread *)atomic_swap_ptr((uintptr_t *)&tdd->tdd_zombies,
(uintptr_t)NULL);
if (itd == NULL)
return;
tdd->tdd_reapticks = ticks;
tidbatch_prep(&tidbatch);
credbatch_prep(&credbatch);
limbatch_prep(&limbatch);
tdcountbatch_prep(&tdcountbatch);
while (itd != NULL) {
ntd = itd->td_zombie;
EVENTHANDLER_DIRECT_INVOKE(thread_dtor, itd);
tidbatch_add(&tidbatch, itd);
credbatch_add(&credbatch, itd);
limbatch_add(&limbatch, itd);
tdcountbatch_add(&tdcountbatch, itd);
thread_free_batched(itd);
tidbatch_process(&tidbatch);
credbatch_process(&credbatch);
limbatch_process(&limbatch);
tdcountbatch_process(&tdcountbatch);
itd = ntd;
}
tidbatch_final(&tidbatch);
credbatch_final(&credbatch);
limbatch_final(&limbatch);
tdcountbatch_final(&tdcountbatch);
}
static void
thread_reap_all(void)
{
struct thread_domain_data *tdd;
int i, domain;
domain = PCPU_GET(domain);
for (i = 0; i < vm_ndomains; i++) {
tdd = &thread_domain_data[(i + domain) % vm_ndomains];
thread_reap_domain(tdd);
}
}
static void
thread_reap(void)
{
struct thread_domain_data *tdd;
int domain;
domain = PCPU_GET(domain);
tdd = &thread_domain_data[domain];
thread_reap_domain(tdd);
}
static void
thread_reap_task_cb(void *arg __unused, int pending __unused)
{
thread_reap_all();
}
static void
thread_reap_callout_cb(void *arg __unused)
{
struct thread_domain_data *tdd;
int i, cticks, lticks;
bool wantreap;
wantreap = false;
cticks = atomic_load_int(&ticks);
for (i = 0; i < vm_ndomains; i++) {
tdd = &thread_domain_data[i];
lticks = tdd->tdd_reapticks;
if (tdd->tdd_zombies != NULL &&
(u_int)(cticks - lticks) > 5 * hz) {
wantreap = true;
break;
}
}
if (wantreap)
taskqueue_enqueue(taskqueue_thread, &thread_reap_task);
callout_reset(&thread_reap_callout, 5 * hz,
thread_reap_callout_cb, NULL);
}
void
thread_reap_barrier(void)
{
struct task *t;
quiesce_all_cpus("", PDROP);
t = malloc(sizeof(*t), M_TEMP, M_WAITOK);
TASK_INIT(t, 0, thread_reap_task_cb, t);
taskqueue_enqueue(taskqueue_thread, t);
taskqueue_drain(taskqueue_thread, t);
free(t, M_TEMP);
}
struct thread *
thread_alloc(int pages)
{
struct thread *td;
lwpid_t tid;
if (!thread_count_inc()) {
return (NULL);
}
tid = tid_alloc();
td = uma_zalloc(thread_zone, M_WAITOK);
KASSERT(td->td_kstack == 0, ("thread_alloc got thread with kstack"));
if (!vm_thread_new(td, pages)) {
uma_zfree(thread_zone, td);
tid_free(tid);
thread_count_dec();
return (NULL);
}
td->td_tid = tid;
bzero(&td->td_sa.args, sizeof(td->td_sa.args));
kasan_thread_alloc(td);
kmsan_thread_alloc(td);
cpu_thread_alloc(td);
EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td);
return (td);
}
int
thread_recycle(struct thread *td, int pages)
{
if (td->td_kstack == 0 || td->td_kstack_pages != pages) {
if (td->td_kstack != 0)
vm_thread_dispose(td);
if (!vm_thread_new(td, pages))
return (ENOMEM);
cpu_thread_alloc(td);
}
kasan_thread_alloc(td);
kmsan_thread_alloc(td);
return (0);
}
static void
thread_free_batched(struct thread *td)
{
lock_profile_thread_exit(td);
if (td->td_cpuset)
cpuset_rel(td->td_cpuset);
td->td_cpuset = NULL;
cpu_thread_free(td);
if (td->td_kstack != 0)
vm_thread_dispose(td);
callout_drain(&td->td_slpcallout);
td->td_tid = -1;
kmsan_thread_free(td);
uma_zfree(thread_zone, td);
}
void
thread_free(struct thread *td)
{
lwpid_t tid;
EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td);
tid = td->td_tid;
thread_free_batched(td);
tid_free(tid);
thread_count_dec();
}
void
thread_cow_get_proc(struct thread *newtd, struct proc *p)
{
PROC_LOCK_ASSERT(p, MA_OWNED);
newtd->td_realucred = crcowget(p->p_ucred);
newtd->td_ucred = newtd->td_realucred;
newtd->td_limit = lim_hold(p->p_limit);
newtd->td_cowgen = p->p_cowgen;
}
void
thread_cow_get(struct thread *newtd, struct thread *td)
{
MPASS(td->td_realucred == td->td_ucred);
newtd->td_realucred = crcowget(td->td_realucred);
newtd->td_ucred = newtd->td_realucred;
newtd->td_limit = lim_hold(td->td_limit);
newtd->td_cowgen = td->td_cowgen;
}
void
thread_cow_free(struct thread *td)
{
if (td->td_realucred != NULL)
crcowfree(td);
if (td->td_limit != NULL)
lim_free(td->td_limit);
}
void
thread_cow_update(struct thread *td)
{
struct proc *p;
struct ucred *oldcred;
struct plimit *oldlimit;
p = td->td_proc;
PROC_LOCK(p);
oldcred = crcowsync();
oldlimit = lim_cowsync();
td->td_cowgen = p->p_cowgen;
PROC_UNLOCK(p);
if (oldcred != NULL)
crfree(oldcred);
if (oldlimit != NULL)
lim_free(oldlimit);
}
void
thread_cow_synced(struct thread *td)
{
struct proc *p;
p = td->td_proc;
PROC_LOCK_ASSERT(p, MA_OWNED);
MPASS(td->td_cowgen != p->p_cowgen);
MPASS(td->td_ucred == p->p_ucred);
MPASS(td->td_limit == p->p_limit);
td->td_cowgen = p->p_cowgen;
}
void
thread_exit(void)
{
uint64_t runtime, new_switchtime;
struct thread *td;
struct thread *td2;
struct proc *p;
td = curthread;
p = td->td_proc;
PROC_SLOCK_ASSERT(p, MA_OWNED);
mtx_assert(&Giant, MA_NOTOWNED);
PROC_LOCK_ASSERT(p, MA_OWNED);
KASSERT(p != NULL, ("thread exiting without a process"));
CTR3(KTR_PROC, "thread_exit: thread %p (pid %ld, %s)", td,
(long)p->p_pid, td->td_name);
SDT_PROBE0(proc, , , lwp__exit);
KASSERT(TAILQ_EMPTY(&td->td_sigqueue.sq_list), ("signal pending"));
MPASS(td->td_realucred == td->td_ucred);
cpu_thread_exit(td);
if (p->p_flag & P_HADTHREADS) {
if (p->p_numthreads > 1) {
atomic_add_int(&td->td_proc->p_exitthreads, 1);
thread_unlink(td);
td2 = FIRST_THREAD_IN_PROC(p);
sched_exit_thread(td2, td);
if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
if (p->p_numthreads == p->p_suspcount) {
thread_lock(p->p_singlethread);
thread_unsuspend_one(p->p_singlethread,
p, false);
}
}
PCPU_SET(deadthread, td);
} else {
panic ("thread_exit: Last thread exiting on its own");
}
}
#ifdef HWPMC_HOOKS
if (PMC_PROC_IS_USING_PMCS(td->td_proc)) {
PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT);
PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_EXIT, NULL);
} else if (PMC_SYSTEM_SAMPLING_ACTIVE())
PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_EXIT_LOG, NULL);
#endif
#ifdef HWT_HOOKS
HWT_CALL_HOOK(td, HWT_THREAD_EXIT, NULL);
#endif
PROC_UNLOCK(p);
PROC_STATLOCK(p);
thread_lock(td);
PROC_SUNLOCK(p);
new_switchtime = cpu_ticks();
runtime = new_switchtime - PCPU_GET(switchtime);
td->td_runtime += runtime;
td->td_incruntime += runtime;
PCPU_SET(switchtime, new_switchtime);
PCPU_SET(switchticks, ticks);
VM_CNT_INC(v_swtch);
td->td_ru.ru_nvcsw++;
ruxagg_locked(p, td);
rucollect(&p->p_ru, &td->td_ru);
PROC_STATUNLOCK(p);
TD_SET_STATE(td, TDS_INACTIVE);
#ifdef WITNESS
witness_thread_exit(td);
#endif
CTR1(KTR_PROC, "thread_exit: cpu_throw() thread %p", td);
sched_throw(td);
panic("I'm a teapot!");
}
void
thread_wait(struct proc *p)
{
struct thread *td;
mtx_assert(&Giant, MA_NOTOWNED);
KASSERT(p->p_numthreads == 1, ("multiple threads in thread_wait()"));
KASSERT(p->p_exitthreads == 0, ("p_exitthreads leaking"));
td = FIRST_THREAD_IN_PROC(p);
thread_lock(td);
thread_unlock(td);
lock_profile_thread_exit(td);
cpuset_rel(td->td_cpuset);
td->td_cpuset = NULL;
cpu_thread_clean(td);
thread_cow_free(td);
callout_drain(&td->td_slpcallout);
thread_reap();
}
void
thread_link(struct thread *td, struct proc *p)
{
TD_SET_STATE(td, TDS_INACTIVE);
td->td_proc = p;
td->td_flags = TDF_INMEM;
LIST_INIT(&td->td_contested);
LIST_INIT(&td->td_lprof[0]);
LIST_INIT(&td->td_lprof[1]);
#ifdef EPOCH_TRACE
SLIST_INIT(&td->td_epochs);
#endif
sigqueue_init(&td->td_sigqueue, p);
callout_init(&td->td_slpcallout, 1);
TAILQ_INSERT_TAIL(&p->p_threads, td, td_plist);
p->p_numthreads++;
}
void
thread_unlink(struct thread *td)
{
struct proc *p = td->td_proc;
PROC_LOCK_ASSERT(p, MA_OWNED);
#ifdef EPOCH_TRACE
MPASS(SLIST_EMPTY(&td->td_epochs));
#endif
TAILQ_REMOVE(&p->p_threads, td, td_plist);
p->p_numthreads--;
}
static int
calc_remaining(struct proc *p, int mode)
{
int remaining;
PROC_LOCK_ASSERT(p, MA_OWNED);
PROC_SLOCK_ASSERT(p, MA_OWNED);
if (mode == SINGLE_EXIT)
remaining = p->p_numthreads;
else if (mode == SINGLE_BOUNDARY)
remaining = p->p_numthreads - p->p_boundary_count;
else if (mode == SINGLE_NO_EXIT || mode == SINGLE_ALLPROC)
remaining = p->p_numthreads - p->p_suspcount;
else
panic("calc_remaining: wrong mode %d", mode);
return (remaining);
}
static int
remain_for_mode(int mode)
{
return (mode == SINGLE_ALLPROC ? 0 : 1);
}
static void
weed_inhib(int mode, struct thread *td2, struct proc *p)
{
PROC_LOCK_ASSERT(p, MA_OWNED);
PROC_SLOCK_ASSERT(p, MA_OWNED);
THREAD_LOCK_ASSERT(td2, MA_OWNED);
restart:
switch (mode) {
case SINGLE_EXIT:
if (TD_IS_SUSPENDED(td2)) {
thread_unsuspend_one(td2, p, true);
thread_lock(td2);
goto restart;
}
if (TD_CAN_ABORT(td2)) {
sleepq_abort(td2, EINTR);
return;
}
break;
case SINGLE_BOUNDARY:
case SINGLE_NO_EXIT:
if (TD_IS_SUSPENDED(td2) &&
(td2->td_flags & TDF_BOUNDARY) == 0) {
thread_unsuspend_one(td2, p, false);
thread_lock(td2);
goto restart;
}
if (TD_CAN_ABORT(td2)) {
sleepq_abort(td2, ERESTART);
return;
}
break;
case SINGLE_ALLPROC:
if (TD_IS_SUSPENDED(td2) &&
(td2->td_flags & TDF_ALLPROCSUSP) == 0) {
thread_unsuspend_one(td2, p, false);
thread_lock(td2);
goto restart;
}
if (TD_CAN_ABORT(td2)) {
td2->td_flags |= TDF_ALLPROCSUSP;
sleepq_abort(td2, ERESTART);
return;
}
break;
default:
break;
}
thread_unlock(td2);
}
int
thread_single(struct proc *p, int mode)
{
struct thread *td;
struct thread *td2;
int remaining;
td = curthread;
KASSERT(mode == SINGLE_EXIT || mode == SINGLE_BOUNDARY ||
mode == SINGLE_ALLPROC || mode == SINGLE_NO_EXIT,
("invalid mode %d", mode));
KASSERT((mode == SINGLE_ALLPROC && td->td_proc != p) ||
(mode != SINGLE_ALLPROC && td->td_proc == p),
("mode %d proc %p curproc %p", mode, p, td->td_proc));
mtx_assert(&Giant, MA_NOTOWNED);
PROC_LOCK_ASSERT(p, MA_OWNED);
if (mode == SINGLE_ALLPROC) {
while ((p->p_flag & P_STOPPED_SINGLE) != 0) {
if ((p->p_flag2 & P2_WEXIT) != 0)
return (1);
msleep(&p->p_flag, &p->p_mtx, PCATCH, "thrsgl", 0);
}
if ((p->p_flag & (P_STOPPED_SIG | P_TRACED)) != 0 ||
(p->p_flag2 & P2_WEXIT) != 0)
return (1);
} else if ((p->p_flag & P_HADTHREADS) == 0)
return (0);
if (p->p_singlethread != NULL && p->p_singlethread != td)
return (1);
if (mode == SINGLE_EXIT) {
p->p_flag |= P_SINGLE_EXIT;
p->p_flag &= ~P_SINGLE_BOUNDARY;
} else {
p->p_flag &= ~P_SINGLE_EXIT;
if (mode == SINGLE_BOUNDARY)
p->p_flag |= P_SINGLE_BOUNDARY;
else
p->p_flag &= ~P_SINGLE_BOUNDARY;
}
if (mode == SINGLE_ALLPROC)
p->p_flag |= P_TOTAL_STOP;
p->p_flag |= P_STOPPED_SINGLE;
PROC_SLOCK(p);
p->p_singlethread = td;
remaining = calc_remaining(p, mode);
while (remaining != remain_for_mode(mode)) {
if (P_SHOULDSTOP(p) != P_STOPPED_SINGLE)
goto stopme;
FOREACH_THREAD_IN_PROC(p, td2) {
if (td2 == td)
continue;
thread_lock(td2);
ast_sched_locked(td2, TDA_SUSPEND);
if (TD_IS_INHIBITED(td2)) {
weed_inhib(mode, td2, p);
#ifdef SMP
} else if (TD_IS_RUNNING(td2)) {
forward_signal(td2);
thread_unlock(td2);
#endif
} else
thread_unlock(td2);
}
remaining = calc_remaining(p, mode);
if (remaining == remain_for_mode(mode))
break;
stopme:
thread_suspend_switch(td, p);
remaining = calc_remaining(p, mode);
}
if (mode == SINGLE_EXIT) {
KASSERT(p->p_numthreads == 1, ("Unthreading with >1 threads"));
p->p_singlethread = NULL;
p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_HADTHREADS);
while (p->p_exitthreads != 0) {
PROC_SUNLOCK(p);
PROC_UNLOCK(p);
sched_relinquish(td);
PROC_LOCK(p);
PROC_SLOCK(p);
}
} else if (mode == SINGLE_BOUNDARY) {
FOREACH_THREAD_IN_PROC(p, td2) {
if (td2 == td)
continue;
thread_lock(td2);
KASSERT((td2->td_flags & TDF_BOUNDARY) != 0,
("td %p not on boundary", td2));
KASSERT(TD_IS_SUSPENDED(td2),
("td %p is not suspended", td2));
thread_unlock(td2);
}
}
PROC_SUNLOCK(p);
return (0);
}
bool
thread_suspend_check_needed(void)
{
struct proc *p;
struct thread *td;
td = curthread;
p = td->td_proc;
PROC_LOCK_ASSERT(p, MA_OWNED);
return (P_SHOULDSTOP(p) || ((p->p_flag & P_TRACED) != 0 &&
(td->td_dbgflags & TDB_SUSPEND) != 0));
}
int
thread_suspend_check(int return_instead)
{
struct thread *td;
struct proc *p;
td = curthread;
p = td->td_proc;
mtx_assert(&Giant, MA_NOTOWNED);
PROC_LOCK_ASSERT(p, MA_OWNED);
while (thread_suspend_check_needed()) {
if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
KASSERT(p->p_singlethread != NULL,
("singlethread not set"));
if (p->p_singlethread == td)
return (0);
}
if ((p->p_flag & P_SINGLE_EXIT) && return_instead)
return (EINTR);
if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE &&
(p->p_flag & P_SINGLE_BOUNDARY) && return_instead)
return (ERESTART);
if ((td->td_flags & TDF_SBDRY) != 0) {
KASSERT(return_instead,
("TDF_SBDRY set for unsafe thread_suspend_check"));
KASSERT((td->td_flags & (TDF_SEINTR | TDF_SERESTART)) !=
(TDF_SEINTR | TDF_SERESTART),
("both TDF_SEINTR and TDF_SERESTART"));
return (TD_SBDRY_INTR(td) ? TD_SBDRY_ERRNO(td) : 0);
}
if (return_instead)
return (EINTR);
if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) {
PROC_UNLOCK(p);
if (__predict_false(p->p_sysent->sv_thread_detach != NULL))
(p->p_sysent->sv_thread_detach)(td);
umtx_thread_exit(td);
kern_thr_exit(td);
panic("stopped thread did not exit");
}
PROC_SLOCK(p);
thread_stopped(p);
if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
if (p->p_numthreads == p->p_suspcount + 1) {
thread_lock(p->p_singlethread);
thread_unsuspend_one(p->p_singlethread, p,
false);
}
}
PROC_UNLOCK(p);
thread_lock(td);
thread_suspend_one(td);
MPASS(!return_instead);
p->p_boundary_count++;
td->td_flags |= TDF_BOUNDARY;
PROC_SUNLOCK(p);
mi_switch(SW_INVOL | SWT_SUSPEND);
PROC_LOCK(p);
}
return (0);
}
int
thread_check_susp(struct thread *td, bool sleep)
{
struct proc *p;
int error;
if (!td_ast_pending(td, TDA_SUSPEND))
return (0);
error = 0;
p = td->td_proc;
PROC_LOCK(p);
if (p->p_flag & P_SINGLE_EXIT)
error = EINTR;
else if (P_SHOULDSTOP(p) ||
((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_SUSPEND)))
error = sleep ? thread_suspend_check(0) : ERESTART;
PROC_UNLOCK(p);
return (error);
}
void
thread_suspend_switch(struct thread *td, struct proc *p)
{
KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
PROC_LOCK_ASSERT(p, MA_OWNED);
PROC_SLOCK_ASSERT(p, MA_OWNED);
if (p == td->td_proc) {
thread_stopped(p);
p->p_suspcount++;
}
PROC_UNLOCK(p);
thread_lock(td);
ast_unsched_locked(td, TDA_SUSPEND);
TD_SET_SUSPENDED(td);
sched_sleep(td, 0);
PROC_SUNLOCK(p);
DROP_GIANT();
mi_switch(SW_VOL | SWT_SUSPEND);
PICKUP_GIANT();
PROC_LOCK(p);
PROC_SLOCK(p);
}
void
thread_suspend_one(struct thread *td)
{
struct proc *p;
p = td->td_proc;
PROC_SLOCK_ASSERT(p, MA_OWNED);
THREAD_LOCK_ASSERT(td, MA_OWNED);
KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
p->p_suspcount++;
ast_unsched_locked(td, TDA_SUSPEND);
TD_SET_SUSPENDED(td);
sched_sleep(td, 0);
}
static void
thread_unsuspend_one(struct thread *td, struct proc *p, bool boundary)
{
THREAD_LOCK_ASSERT(td, MA_OWNED);
KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended"));
TD_CLR_SUSPENDED(td);
td->td_flags &= ~TDF_ALLPROCSUSP;
if (td->td_proc == p) {
PROC_SLOCK_ASSERT(p, MA_OWNED);
p->p_suspcount--;
if (boundary && (td->td_flags & TDF_BOUNDARY) != 0) {
td->td_flags &= ~TDF_BOUNDARY;
p->p_boundary_count--;
}
}
setrunnable(td, 0);
}
void
thread_run_flash(struct thread *td)
{
struct proc *p;
p = td->td_proc;
PROC_LOCK_ASSERT(p, MA_OWNED);
if (TD_ON_SLEEPQ(td))
sleepq_remove_nested(td);
else
thread_lock(td);
THREAD_LOCK_ASSERT(td, MA_OWNED);
KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended"));
TD_CLR_SUSPENDED(td);
PROC_SLOCK(p);
MPASS(p->p_suspcount > 0);
p->p_suspcount--;
PROC_SUNLOCK(p);
setrunnable(td, 0);
}
void
thread_unsuspend(struct proc *p)
{
struct thread *td;
PROC_LOCK_ASSERT(p, MA_OWNED);
PROC_SLOCK_ASSERT(p, MA_OWNED);
if (!P_SHOULDSTOP(p)) {
FOREACH_THREAD_IN_PROC(p, td) {
thread_lock(td);
if (TD_IS_SUSPENDED(td))
thread_unsuspend_one(td, p, true);
else
thread_unlock(td);
}
} else if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE &&
p->p_numthreads == p->p_suspcount) {
if (p->p_singlethread->td_proc == p) {
thread_lock(p->p_singlethread);
thread_unsuspend_one(p->p_singlethread, p, false);
}
}
}
void
thread_single_end(struct proc *p, int mode)
{
struct thread *td;
KASSERT(mode == SINGLE_EXIT || mode == SINGLE_BOUNDARY ||
mode == SINGLE_ALLPROC || mode == SINGLE_NO_EXIT,
("invalid mode %d", mode));
PROC_LOCK_ASSERT(p, MA_OWNED);
KASSERT((mode == SINGLE_ALLPROC && (p->p_flag & P_TOTAL_STOP) != 0) ||
(mode != SINGLE_ALLPROC && (p->p_flag & P_TOTAL_STOP) == 0),
("mode %d does not match P_TOTAL_STOP", mode));
KASSERT(mode == SINGLE_ALLPROC || p->p_singlethread == curthread,
("thread_single_end from other thread %p %p",
curthread, p->p_singlethread));
KASSERT(mode != SINGLE_BOUNDARY ||
(p->p_flag & P_SINGLE_BOUNDARY) != 0,
("mis-matched SINGLE_BOUNDARY flags %x", p->p_flag));
p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_SINGLE_BOUNDARY |
P_TOTAL_STOP);
PROC_SLOCK(p);
p->p_singlethread = NULL;
if (p->p_numthreads != remain_for_mode(mode) && !P_SHOULDSTOP(p)) {
FOREACH_THREAD_IN_PROC(p, td) {
thread_lock(td);
if (TD_IS_SUSPENDED(td))
thread_unsuspend_one(td, p, true);
else
thread_unlock(td);
}
}
KASSERT(mode != SINGLE_BOUNDARY || P_SHOULDSTOP(p) ||
p->p_boundary_count == 0,
("pid %d proc %p flags %#x inconsistent boundary count %d",
p->p_pid, p, p->p_flag, p->p_boundary_count));
PROC_SUNLOCK(p);
wakeup(&p->p_flag);
}
static bool
tdfind_hash(lwpid_t tid, pid_t pid, struct proc **pp, struct thread **tdp)
{
#define RUN_THRESH 16
struct proc *p;
struct thread *td;
int run;
bool locked;
run = 0;
rw_rlock(TIDHASHLOCK(tid));
locked = true;
LIST_FOREACH(td, TIDHASH(tid), td_hash) {
if (td->td_tid != tid) {
run++;
continue;
}
p = td->td_proc;
if (pid != -1 && p->p_pid != pid) {
td = NULL;
break;
}
if (run > RUN_THRESH) {
if (rw_try_upgrade(TIDHASHLOCK(tid))) {
LIST_REMOVE(td, td_hash);
LIST_INSERT_HEAD(TIDHASH(td->td_tid),
td, td_hash);
rw_wunlock(TIDHASHLOCK(tid));
locked = false;
break;
}
}
break;
}
if (locked)
rw_runlock(TIDHASHLOCK(tid));
if (td == NULL)
return (false);
*pp = p;
*tdp = td;
return (true);
}
struct thread *
tdfind(lwpid_t tid, pid_t pid)
{
struct proc *p;
struct thread *td;
td = curthread;
if (td->td_tid == tid) {
if (pid != -1 && td->td_proc->p_pid != pid)
return (NULL);
PROC_LOCK(td->td_proc);
return (td);
}
for (;;) {
if (!tdfind_hash(tid, pid, &p, &td))
return (NULL);
PROC_LOCK(p);
if (td->td_tid != tid) {
PROC_UNLOCK(p);
continue;
}
if (td->td_proc != p) {
PROC_UNLOCK(p);
continue;
}
if (p->p_state == PRS_NEW) {
PROC_UNLOCK(p);
return (NULL);
}
return (td);
}
}
void
tidhash_add(struct thread *td)
{
rw_wlock(TIDHASHLOCK(td->td_tid));
LIST_INSERT_HEAD(TIDHASH(td->td_tid), td, td_hash);
rw_wunlock(TIDHASHLOCK(td->td_tid));
}
void
tidhash_remove(struct thread *td)
{
rw_wlock(TIDHASHLOCK(td->td_tid));
LIST_REMOVE(td, td_hash);
rw_wunlock(TIDHASHLOCK(td->td_tid));
}