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 293d06b4b3SAttilio Rao #include "opt_witness.h" 3016d95d4fSJoseph Koshy #include "opt_hwpmc_hooks.h" 313d06b4b3SAttilio Rao 32677b542eSDavid E. O'Brien #include <sys/cdefs.h> 33677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 34677b542eSDavid E. O'Brien 3544990b8cSJulian Elischer #include <sys/param.h> 3644990b8cSJulian Elischer #include <sys/systm.h> 3744990b8cSJulian Elischer #include <sys/kernel.h> 3844990b8cSJulian Elischer #include <sys/lock.h> 3944990b8cSJulian Elischer #include <sys/mutex.h> 4044990b8cSJulian Elischer #include <sys/proc.h> 418f0e9130SKonstantin Belousov #include <sys/rangelock.h> 42e170bfdaSDavid Xu #include <sys/resourcevar.h> 43b3e9e682SRyan Stone #include <sys/sdt.h> 4494e0a4cdSJulian Elischer #include <sys/smp.h> 45de028f5aSJeff Roberson #include <sys/sched.h> 4644f3b092SJohn Baldwin #include <sys/sleepqueue.h> 47ace8398dSJeff Roberson #include <sys/selinfo.h> 48961a7b24SJohn Baldwin #include <sys/turnstile.h> 4944990b8cSJulian Elischer #include <sys/ktr.h> 50cf7d9a8cSDavid Xu #include <sys/rwlock.h> 51bc8e6d81SDavid Xu #include <sys/umtx.h> 52d7f687fcSJeff Roberson #include <sys/cpuset.h> 5316d95d4fSJoseph Koshy #ifdef HWPMC_HOOKS 5416d95d4fSJoseph Koshy #include <sys/pmckern.h> 5516d95d4fSJoseph Koshy #endif 5644990b8cSJulian Elischer 57911b84b0SRobert Watson #include <security/audit/audit.h> 58911b84b0SRobert Watson 5944990b8cSJulian Elischer #include <vm/vm.h> 6049a2507bSAlan Cox #include <vm/vm_extern.h> 6144990b8cSJulian Elischer #include <vm/uma.h> 62b209f889SRandall Stewart #include <sys/eventhandler.h> 6302fb42b0SPeter Wemm 64b3e9e682SRyan Stone SDT_PROVIDER_DECLARE(proc); 65d9fae5abSAndriy Gapon SDT_PROBE_DEFINE(proc, , , lwp__exit); 66b3e9e682SRyan Stone 678460a577SJohn Birrell /* 688460a577SJohn Birrell * thread related storage. 698460a577SJohn Birrell */ 7044990b8cSJulian Elischer static uma_zone_t thread_zone; 7144990b8cSJulian Elischer 725215b187SJeff Roberson TAILQ_HEAD(, thread) zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads); 73c8790f5dSAttilio Rao static struct mtx zombie_lock; 74a54e85fdSJeff Roberson MTX_SYSINIT(zombie_lock, &zombie_lock, "zombie lock", MTX_SPIN); 7544990b8cSJulian Elischer 76ff8fbcffSJeff Roberson static void thread_zombie(struct thread *); 7784cdea97SKonstantin Belousov static int thread_unsuspend_one(struct thread *td, struct proc *p, 7884cdea97SKonstantin Belousov bool boundary); 79ff8fbcffSJeff Roberson 80ec6ea5e8SDavid Xu #define TID_BUFFER_SIZE 1024 81ec6ea5e8SDavid Xu 82fdcac928SMarcel Moolenaar struct mtx tid_lock; 831ea7a6f8SPoul-Henning Kamp static struct unrhdr *tid_unrhdr; 84ec6ea5e8SDavid Xu static lwpid_t tid_buffer[TID_BUFFER_SIZE]; 85ec6ea5e8SDavid Xu static int tid_head, tid_tail; 86cf7d9a8cSDavid Xu static MALLOC_DEFINE(M_TIDHASH, "tidhash", "thread hash"); 87cf7d9a8cSDavid Xu 88cf7d9a8cSDavid Xu struct tidhashhead *tidhashtbl; 89cf7d9a8cSDavid Xu u_long tidhash; 90cf7d9a8cSDavid Xu struct rwlock tidhash_lock; 91cf7d9a8cSDavid Xu 92ec6ea5e8SDavid Xu static lwpid_t 93ec6ea5e8SDavid Xu tid_alloc(void) 94ec6ea5e8SDavid Xu { 95ec6ea5e8SDavid Xu lwpid_t tid; 96ec6ea5e8SDavid Xu 97ec6ea5e8SDavid Xu tid = alloc_unr(tid_unrhdr); 98ec6ea5e8SDavid Xu if (tid != -1) 99ec6ea5e8SDavid Xu return (tid); 100ec6ea5e8SDavid Xu mtx_lock(&tid_lock); 101ec6ea5e8SDavid Xu if (tid_head == tid_tail) { 102ec6ea5e8SDavid Xu mtx_unlock(&tid_lock); 103ec6ea5e8SDavid Xu return (-1); 104ec6ea5e8SDavid Xu } 10594cb3545SKonstantin Belousov tid = tid_buffer[tid_head]; 10694cb3545SKonstantin Belousov tid_head = (tid_head + 1) % TID_BUFFER_SIZE; 107ec6ea5e8SDavid Xu mtx_unlock(&tid_lock); 108ec6ea5e8SDavid Xu return (tid); 109ec6ea5e8SDavid Xu } 110ec6ea5e8SDavid Xu 111ec6ea5e8SDavid Xu static void 112ec6ea5e8SDavid Xu tid_free(lwpid_t tid) 113ec6ea5e8SDavid Xu { 114ec6ea5e8SDavid Xu lwpid_t tmp_tid = -1; 115ec6ea5e8SDavid Xu 116ec6ea5e8SDavid Xu mtx_lock(&tid_lock); 117ec6ea5e8SDavid Xu if ((tid_tail + 1) % TID_BUFFER_SIZE == tid_head) { 11894cb3545SKonstantin Belousov tmp_tid = tid_buffer[tid_head]; 11994cb3545SKonstantin Belousov tid_head = (tid_head + 1) % TID_BUFFER_SIZE; 120ec6ea5e8SDavid Xu } 12194cb3545SKonstantin Belousov tid_buffer[tid_tail] = tid; 12294cb3545SKonstantin Belousov tid_tail = (tid_tail + 1) % TID_BUFFER_SIZE; 123ec6ea5e8SDavid Xu mtx_unlock(&tid_lock); 124ec6ea5e8SDavid Xu if (tmp_tid != -1) 125ec6ea5e8SDavid Xu free_unr(tid_unrhdr, tmp_tid); 126ec6ea5e8SDavid Xu } 127ec6ea5e8SDavid Xu 128fdcac928SMarcel Moolenaar /* 129696058c3SJulian Elischer * Prepare a thread for use. 13044990b8cSJulian Elischer */ 131b23f72e9SBrian Feldman static int 132b23f72e9SBrian Feldman thread_ctor(void *mem, int size, void *arg, int flags) 13344990b8cSJulian Elischer { 13444990b8cSJulian Elischer struct thread *td; 13544990b8cSJulian Elischer 13644990b8cSJulian Elischer td = (struct thread *)mem; 13771fad9fdSJulian Elischer td->td_state = TDS_INACTIVE; 138060563ecSJulian Elischer td->td_oncpu = NOCPU; 1396c27c603SJuli Mallett 140ec6ea5e8SDavid Xu td->td_tid = tid_alloc(); 141773eff9dSPoul-Henning Kamp 1426c27c603SJuli Mallett /* 1436c27c603SJuli Mallett * Note that td_critnest begins life as 1 because the thread is not 1446c27c603SJuli Mallett * running and is thereby implicitly waiting to be on the receiving 145a54e85fdSJeff Roberson * end of a context switch. 1466c27c603SJuli Mallett */ 147139b7550SJohn Baldwin td->td_critnest = 1; 148acbe332aSDavid Xu td->td_lend_user_pri = PRI_MAX; 149b209f889SRandall Stewart EVENTHANDLER_INVOKE(thread_ctor, td); 150911b84b0SRobert Watson #ifdef AUDIT 151911b84b0SRobert Watson audit_thread_alloc(td); 152911b84b0SRobert Watson #endif 153d10183d9SDavid Xu umtx_thread_alloc(td); 154b23f72e9SBrian Feldman return (0); 15544990b8cSJulian Elischer } 15644990b8cSJulian Elischer 15744990b8cSJulian Elischer /* 15844990b8cSJulian Elischer * Reclaim a thread after use. 15944990b8cSJulian Elischer */ 16044990b8cSJulian Elischer static void 16144990b8cSJulian Elischer thread_dtor(void *mem, int size, void *arg) 16244990b8cSJulian Elischer { 16344990b8cSJulian Elischer struct thread *td; 16444990b8cSJulian Elischer 16544990b8cSJulian Elischer td = (struct thread *)mem; 16644990b8cSJulian Elischer 16744990b8cSJulian Elischer #ifdef INVARIANTS 16844990b8cSJulian Elischer /* Verify that this thread is in a safe state to free. */ 16944990b8cSJulian Elischer switch (td->td_state) { 17071fad9fdSJulian Elischer case TDS_INHIBITED: 17171fad9fdSJulian Elischer case TDS_RUNNING: 17271fad9fdSJulian Elischer case TDS_CAN_RUN: 17344990b8cSJulian Elischer case TDS_RUNQ: 17444990b8cSJulian Elischer /* 17544990b8cSJulian Elischer * We must never unlink a thread that is in one of 17644990b8cSJulian Elischer * these states, because it is currently active. 17744990b8cSJulian Elischer */ 17844990b8cSJulian Elischer panic("bad state for thread unlinking"); 17944990b8cSJulian Elischer /* NOTREACHED */ 18071fad9fdSJulian Elischer case TDS_INACTIVE: 18144990b8cSJulian Elischer break; 18244990b8cSJulian Elischer default: 18344990b8cSJulian Elischer panic("bad thread state"); 18444990b8cSJulian Elischer /* NOTREACHED */ 18544990b8cSJulian Elischer } 18644990b8cSJulian Elischer #endif 1876e8525ceSRobert Watson #ifdef AUDIT 1886e8525ceSRobert Watson audit_thread_free(td); 1896e8525ceSRobert Watson #endif 1901ba4a712SPawel Jakub Dawidek /* Free all OSD associated to this thread. */ 1911ba4a712SPawel Jakub Dawidek osd_thread_exit(td); 1921ba4a712SPawel Jakub Dawidek 193b209f889SRandall Stewart EVENTHANDLER_INVOKE(thread_dtor, td); 194ec6ea5e8SDavid Xu tid_free(td->td_tid); 19544990b8cSJulian Elischer } 19644990b8cSJulian Elischer 19744990b8cSJulian Elischer /* 19844990b8cSJulian Elischer * Initialize type-stable parts of a thread (when newly created). 19944990b8cSJulian Elischer */ 200b23f72e9SBrian Feldman static int 201b23f72e9SBrian Feldman thread_init(void *mem, int size, int flags) 20244990b8cSJulian Elischer { 20344990b8cSJulian Elischer struct thread *td; 20444990b8cSJulian Elischer 20544990b8cSJulian Elischer td = (struct thread *)mem; 206247aba24SMarcel Moolenaar 20744f3b092SJohn Baldwin td->td_sleepqueue = sleepq_alloc(); 208961a7b24SJohn Baldwin td->td_turnstile = turnstile_alloc(); 2098f0e9130SKonstantin Belousov td->td_rlqe = NULL; 210b209f889SRandall Stewart EVENTHANDLER_INVOKE(thread_init, td); 211de028f5aSJeff Roberson td->td_sched = (struct td_sched *)&td[1]; 212d10183d9SDavid Xu umtx_thread_init(td); 21389b57fcfSKonstantin Belousov td->td_kstack = 0; 214ad8b1d85SKonstantin Belousov td->td_sel = NULL; 215b23f72e9SBrian Feldman return (0); 21644990b8cSJulian Elischer } 21744990b8cSJulian Elischer 21844990b8cSJulian Elischer /* 21944990b8cSJulian Elischer * Tear down type-stable parts of a thread (just before being discarded). 22044990b8cSJulian Elischer */ 22144990b8cSJulian Elischer static void 22244990b8cSJulian Elischer thread_fini(void *mem, int size) 22344990b8cSJulian Elischer { 22444990b8cSJulian Elischer struct thread *td; 22544990b8cSJulian Elischer 22644990b8cSJulian Elischer td = (struct thread *)mem; 227b209f889SRandall Stewart EVENTHANDLER_INVOKE(thread_fini, td); 2288f0e9130SKonstantin Belousov rlqentry_free(td->td_rlqe); 229961a7b24SJohn Baldwin turnstile_free(td->td_turnstile); 23044f3b092SJohn Baldwin sleepq_free(td->td_sleepqueue); 231d10183d9SDavid Xu umtx_thread_fini(td); 232ace8398dSJeff Roberson seltdfini(td); 23344990b8cSJulian Elischer } 2345215b187SJeff Roberson 2355c8329edSJulian Elischer /* 2365215b187SJeff Roberson * For a newly created process, 2375215b187SJeff Roberson * link up all the structures and its initial threads etc. 238ed062c8dSJulian Elischer * called from: 239e7d939bdSMarcel Moolenaar * {arch}/{arch}/machdep.c {arch}_init(), init386() etc. 240ed062c8dSJulian Elischer * proc_dtor() (should go away) 241ed062c8dSJulian Elischer * proc_init() 2425c8329edSJulian Elischer */ 2435c8329edSJulian Elischer void 24489b57fcfSKonstantin Belousov proc_linkup0(struct proc *p, struct thread *td) 24589b57fcfSKonstantin Belousov { 24689b57fcfSKonstantin Belousov TAILQ_INIT(&p->p_threads); /* all threads in proc */ 24789b57fcfSKonstantin Belousov proc_linkup(p, td); 24889b57fcfSKonstantin Belousov } 24989b57fcfSKonstantin Belousov 25089b57fcfSKonstantin Belousov void 2518460a577SJohn Birrell proc_linkup(struct proc *p, struct thread *td) 2525c8329edSJulian Elischer { 253a54e85fdSJeff Roberson 2549104847fSDavid Xu sigqueue_init(&p->p_sigqueue, p); 255ebceaf6dSDavid Xu p->p_ksi = ksiginfo_alloc(1); 256ebceaf6dSDavid Xu if (p->p_ksi != NULL) { 2575c474517SDavid Xu /* XXX p_ksi may be null if ksiginfo zone is not ready */ 258ebceaf6dSDavid Xu p->p_ksi->ksi_flags = KSI_EXT | KSI_INS; 259ebceaf6dSDavid Xu } 260b2f92ef9SDavid Xu LIST_INIT(&p->p_mqnotifier); 2615c8329edSJulian Elischer p->p_numthreads = 0; 2628460a577SJohn Birrell thread_link(td, p); 2635c8329edSJulian Elischer } 2645c8329edSJulian Elischer 2655c8329edSJulian Elischer /* 26644990b8cSJulian Elischer * Initialize global thread allocation resources. 26744990b8cSJulian Elischer */ 26844990b8cSJulian Elischer void 26944990b8cSJulian Elischer threadinit(void) 27044990b8cSJulian Elischer { 27144990b8cSJulian Elischer 2721ea7a6f8SPoul-Henning Kamp mtx_init(&tid_lock, "TID lock", NULL, MTX_DEF); 27302c6fc21SKonstantin Belousov 27402c6fc21SKonstantin Belousov /* 275abce621cSKonstantin Belousov * pid_max cannot be greater than PID_MAX. 27602c6fc21SKonstantin Belousov * leave one number for thread0. 27702c6fc21SKonstantin Belousov */ 2786829a5c5SJulian Elischer tid_unrhdr = new_unrhdr(PID_MAX + 2, INT_MAX, &tid_lock); 2791ea7a6f8SPoul-Henning Kamp 280de028f5aSJeff Roberson thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(), 28144990b8cSJulian Elischer thread_ctor, thread_dtor, thread_init, thread_fini, 2824649e92bSJohn Baldwin 16 - 1, 0); 283cf7d9a8cSDavid Xu tidhashtbl = hashinit(maxproc / 2, M_TIDHASH, &tidhash); 284cf7d9a8cSDavid Xu rw_init(&tidhash_lock, "tidhash"); 28544990b8cSJulian Elischer } 28644990b8cSJulian Elischer 28744990b8cSJulian Elischer /* 288ff8fbcffSJeff Roberson * Place an unused thread on the zombie list. 289ad1e7d28SJulian Elischer * Use the slpq as that must be unused by now. 29044990b8cSJulian Elischer */ 29144990b8cSJulian Elischer void 292ff8fbcffSJeff Roberson thread_zombie(struct thread *td) 29344990b8cSJulian Elischer { 294a54e85fdSJeff Roberson mtx_lock_spin(&zombie_lock); 295ad1e7d28SJulian Elischer TAILQ_INSERT_HEAD(&zombie_threads, td, td_slpq); 296a54e85fdSJeff Roberson mtx_unlock_spin(&zombie_lock); 29744990b8cSJulian Elischer } 29844990b8cSJulian Elischer 2995c8329edSJulian Elischer /* 300ff8fbcffSJeff Roberson * Release a thread that has exited after cpu_throw(). 301ff8fbcffSJeff Roberson */ 302ff8fbcffSJeff Roberson void 303ff8fbcffSJeff Roberson thread_stash(struct thread *td) 304ff8fbcffSJeff Roberson { 305ff8fbcffSJeff Roberson atomic_subtract_rel_int(&td->td_proc->p_exitthreads, 1); 306ff8fbcffSJeff Roberson thread_zombie(td); 307ff8fbcffSJeff Roberson } 308ff8fbcffSJeff Roberson 309ff8fbcffSJeff Roberson /* 3106617724cSJeff Roberson * Reap zombie resources. 31144990b8cSJulian Elischer */ 31244990b8cSJulian Elischer void 31344990b8cSJulian Elischer thread_reap(void) 31444990b8cSJulian Elischer { 3155c8329edSJulian Elischer struct thread *td_first, *td_next; 31644990b8cSJulian Elischer 31744990b8cSJulian Elischer /* 3185215b187SJeff Roberson * Don't even bother to lock if none at this instant, 3195215b187SJeff Roberson * we really don't care about the next instant.. 32044990b8cSJulian Elischer */ 3218460a577SJohn Birrell if (!TAILQ_EMPTY(&zombie_threads)) { 322a54e85fdSJeff Roberson mtx_lock_spin(&zombie_lock); 3235c8329edSJulian Elischer td_first = TAILQ_FIRST(&zombie_threads); 3245c8329edSJulian Elischer if (td_first) 3255c8329edSJulian Elischer TAILQ_INIT(&zombie_threads); 326a54e85fdSJeff Roberson mtx_unlock_spin(&zombie_lock); 3275c8329edSJulian Elischer while (td_first) { 328ad1e7d28SJulian Elischer td_next = TAILQ_NEXT(td_first, td_slpq); 3295215b187SJeff Roberson if (td_first->td_ucred) 3305215b187SJeff Roberson crfree(td_first->td_ucred); 3315c8329edSJulian Elischer thread_free(td_first); 3325c8329edSJulian Elischer td_first = td_next; 33344990b8cSJulian Elischer } 33444990b8cSJulian Elischer } 335ed062c8dSJulian Elischer } 33644990b8cSJulian Elischer 3374f0db5e0SJulian Elischer /* 33844990b8cSJulian Elischer * Allocate a thread. 33944990b8cSJulian Elischer */ 34044990b8cSJulian Elischer struct thread * 3418a945d10SKonstantin Belousov thread_alloc(int pages) 34244990b8cSJulian Elischer { 34389b57fcfSKonstantin Belousov struct thread *td; 3448460a577SJohn Birrell 34544990b8cSJulian Elischer thread_reap(); /* check if any zombies to get */ 34689b57fcfSKonstantin Belousov 34789b57fcfSKonstantin Belousov td = (struct thread *)uma_zalloc(thread_zone, M_WAITOK); 34889b57fcfSKonstantin Belousov KASSERT(td->td_kstack == 0, ("thread_alloc got thread with kstack")); 3498a945d10SKonstantin Belousov if (!vm_thread_new(td, pages)) { 35089b57fcfSKonstantin Belousov uma_zfree(thread_zone, td); 35189b57fcfSKonstantin Belousov return (NULL); 35289b57fcfSKonstantin Belousov } 3530c3967e7SMarcel Moolenaar cpu_thread_alloc(td); 35489b57fcfSKonstantin Belousov return (td); 35544990b8cSJulian Elischer } 35644990b8cSJulian Elischer 3578a945d10SKonstantin Belousov int 3588a945d10SKonstantin Belousov thread_alloc_stack(struct thread *td, int pages) 3598a945d10SKonstantin Belousov { 3608a945d10SKonstantin Belousov 3618a945d10SKonstantin Belousov KASSERT(td->td_kstack == 0, 3628a945d10SKonstantin Belousov ("thread_alloc_stack called on a thread with kstack")); 3638a945d10SKonstantin Belousov if (!vm_thread_new(td, pages)) 3648a945d10SKonstantin Belousov return (0); 3658a945d10SKonstantin Belousov cpu_thread_alloc(td); 3668a945d10SKonstantin Belousov return (1); 3678a945d10SKonstantin Belousov } 3684f0db5e0SJulian Elischer 3694f0db5e0SJulian Elischer /* 37044990b8cSJulian Elischer * Deallocate a thread. 37144990b8cSJulian Elischer */ 37244990b8cSJulian Elischer void 37344990b8cSJulian Elischer thread_free(struct thread *td) 37444990b8cSJulian Elischer { 3752e6b8de4SJeff Roberson 3762e6b8de4SJeff Roberson lock_profile_thread_exit(td); 37745aea8deSJeff Roberson if (td->td_cpuset) 378d7f687fcSJeff Roberson cpuset_rel(td->td_cpuset); 379d7f687fcSJeff Roberson td->td_cpuset = NULL; 3800c3967e7SMarcel Moolenaar cpu_thread_free(td); 38189b57fcfSKonstantin Belousov if (td->td_kstack != 0) 38289b57fcfSKonstantin Belousov vm_thread_dispose(td); 38344990b8cSJulian Elischer uma_zfree(thread_zone, td); 38444990b8cSJulian Elischer } 38544990b8cSJulian Elischer 38644990b8cSJulian Elischer /* 38744990b8cSJulian Elischer * Discard the current thread and exit from its context. 38894e0a4cdSJulian Elischer * Always called with scheduler locked. 38944990b8cSJulian Elischer * 39044990b8cSJulian Elischer * Because we can't free a thread while we're operating under its context, 391696058c3SJulian Elischer * push the current thread into our CPU's deadthread holder. This means 392696058c3SJulian Elischer * we needn't worry about someone else grabbing our context before we 3936617724cSJeff Roberson * do a cpu_throw(). 39444990b8cSJulian Elischer */ 39544990b8cSJulian Elischer void 39644990b8cSJulian Elischer thread_exit(void) 39744990b8cSJulian Elischer { 3987e3a96eaSJohn Baldwin uint64_t runtime, new_switchtime; 39944990b8cSJulian Elischer struct thread *td; 4001c4bcd05SJeff Roberson struct thread *td2; 40144990b8cSJulian Elischer struct proc *p; 4027847a9daSJohn Baldwin int wakeup_swapper; 40344990b8cSJulian Elischer 40444990b8cSJulian Elischer td = curthread; 40544990b8cSJulian Elischer p = td->td_proc; 40644990b8cSJulian Elischer 407a54e85fdSJeff Roberson PROC_SLOCK_ASSERT(p, MA_OWNED); 408ed062c8dSJulian Elischer mtx_assert(&Giant, MA_NOTOWNED); 409a54e85fdSJeff Roberson 41044990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 411ed062c8dSJulian Elischer KASSERT(p != NULL, ("thread exiting without a process")); 412cc701b73SRobert Watson CTR3(KTR_PROC, "thread_exit: thread %p (pid %ld, %s)", td, 413e01eafefSJulian Elischer (long)p->p_pid, td->td_name); 4149104847fSDavid Xu KASSERT(TAILQ_EMPTY(&td->td_sigqueue.sq_list), ("signal pending")); 41544990b8cSJulian Elischer 41689964dd2SRobert Watson #ifdef AUDIT 41789964dd2SRobert Watson AUDIT_SYSCALL_EXIT(0, td); 41889964dd2SRobert Watson #endif 419ed062c8dSJulian Elischer /* 420ed062c8dSJulian Elischer * drop FPU & debug register state storage, or any other 421ed062c8dSJulian Elischer * architecture specific resources that 422ed062c8dSJulian Elischer * would not be on a new untouched process. 423ed062c8dSJulian Elischer */ 42444990b8cSJulian Elischer cpu_thread_exit(td); /* XXXSMP */ 42544990b8cSJulian Elischer 426ed062c8dSJulian Elischer /* 4271faf202eSJulian Elischer * The last thread is left attached to the process 4281faf202eSJulian Elischer * So that the whole bundle gets recycled. Skip 429ed062c8dSJulian Elischer * all this stuff if we never had threads. 430ed062c8dSJulian Elischer * EXIT clears all sign of other threads when 431ed062c8dSJulian Elischer * it goes to single threading, so the last thread always 432ed062c8dSJulian Elischer * takes the short path. 4331faf202eSJulian Elischer */ 434ed062c8dSJulian Elischer if (p->p_flag & P_HADTHREADS) { 4351faf202eSJulian Elischer if (p->p_numthreads > 1) { 436fd229b5bSKonstantin Belousov atomic_add_int(&td->td_proc->p_exitthreads, 1); 437d3a0bd78SJulian Elischer thread_unlink(td); 4381c4bcd05SJeff Roberson td2 = FIRST_THREAD_IN_PROC(p); 4391c4bcd05SJeff Roberson sched_exit_thread(td2, td); 440ed062c8dSJulian Elischer 441ed062c8dSJulian Elischer /* 44244990b8cSJulian Elischer * The test below is NOT true if we are the 4439182554aSKonstantin Belousov * sole exiting thread. P_STOPPED_SINGLE is unset 44444990b8cSJulian Elischer * in exit1() after it is the only survivor. 44544990b8cSJulian Elischer */ 4461279572aSDavid Xu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 44744990b8cSJulian Elischer if (p->p_numthreads == p->p_suspcount) { 448a54e85fdSJeff Roberson thread_lock(p->p_singlethread); 4497847a9daSJohn Baldwin wakeup_swapper = thread_unsuspend_one( 45084cdea97SKonstantin Belousov p->p_singlethread, p, false); 451a54e85fdSJeff Roberson thread_unlock(p->p_singlethread); 4527847a9daSJohn Baldwin if (wakeup_swapper) 4537847a9daSJohn Baldwin kick_proc0(); 45444990b8cSJulian Elischer } 45544990b8cSJulian Elischer } 45648bfcdddSJulian Elischer 457696058c3SJulian Elischer PCPU_SET(deadthread, td); 4581faf202eSJulian Elischer } else { 459ed062c8dSJulian Elischer /* 460ed062c8dSJulian Elischer * The last thread is exiting.. but not through exit() 461ed062c8dSJulian Elischer */ 462ed062c8dSJulian Elischer panic ("thread_exit: Last thread exiting on its own"); 463ed062c8dSJulian Elischer } 4641faf202eSJulian Elischer } 46516d95d4fSJoseph Koshy #ifdef HWPMC_HOOKS 46616d95d4fSJoseph Koshy /* 46716d95d4fSJoseph Koshy * If this thread is part of a process that is being tracked by hwpmc(4), 46816d95d4fSJoseph Koshy * inform the module of the thread's impending exit. 46916d95d4fSJoseph Koshy */ 47016d95d4fSJoseph Koshy if (PMC_PROC_IS_USING_PMCS(td->td_proc)) 47116d95d4fSJoseph Koshy PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT); 47216d95d4fSJoseph Koshy #endif 473a54e85fdSJeff Roberson PROC_UNLOCK(p); 4745c7bebf9SKonstantin Belousov PROC_STATLOCK(p); 4755c7bebf9SKonstantin Belousov thread_lock(td); 4765c7bebf9SKonstantin Belousov PROC_SUNLOCK(p); 4777e3a96eaSJohn Baldwin 4787e3a96eaSJohn Baldwin /* Do the same timestamp bookkeeping that mi_switch() would do. */ 4797e3a96eaSJohn Baldwin new_switchtime = cpu_ticks(); 4807e3a96eaSJohn Baldwin runtime = new_switchtime - PCPU_GET(switchtime); 4817e3a96eaSJohn Baldwin td->td_runtime += runtime; 4827e3a96eaSJohn Baldwin td->td_incruntime += runtime; 4837e3a96eaSJohn Baldwin PCPU_SET(switchtime, new_switchtime); 4847e3a96eaSJohn Baldwin PCPU_SET(switchticks, ticks); 4857e3a96eaSJohn Baldwin PCPU_INC(cnt.v_swtch); 4867e3a96eaSJohn Baldwin 4877e3a96eaSJohn Baldwin /* Save our resource usage in our process. */ 4887e3a96eaSJohn Baldwin td->td_ru.ru_nvcsw++; 48941fd9c63SKonstantin Belousov ruxagg(p, td); 4907e3a96eaSJohn Baldwin rucollect(&p->p_ru, &td->td_ru); 4915c7bebf9SKonstantin Belousov PROC_STATUNLOCK(p); 4927e3a96eaSJohn Baldwin 493dcc9954eSJulian Elischer td->td_state = TDS_INACTIVE; 4943d06b4b3SAttilio Rao #ifdef WITNESS 4953d06b4b3SAttilio Rao witness_thread_exit(td); 4963d06b4b3SAttilio Rao #endif 497732d9528SJulian Elischer CTR1(KTR_PROC, "thread_exit: cpu_throw() thread %p", td); 498a54e85fdSJeff Roberson sched_throw(td); 499cc66ebe2SPeter Wemm panic("I'm a teapot!"); 50044990b8cSJulian Elischer /* NOTREACHED */ 50144990b8cSJulian Elischer } 50244990b8cSJulian Elischer 50344990b8cSJulian Elischer /* 504696058c3SJulian Elischer * Do any thread specific cleanups that may be needed in wait() 50537814395SPeter Wemm * called with Giant, proc and schedlock not held. 506696058c3SJulian Elischer */ 507696058c3SJulian Elischer void 508696058c3SJulian Elischer thread_wait(struct proc *p) 509696058c3SJulian Elischer { 510696058c3SJulian Elischer struct thread *td; 511696058c3SJulian Elischer 51237814395SPeter Wemm mtx_assert(&Giant, MA_NOTOWNED); 513624bf9e1SKonstantin Belousov KASSERT(p->p_numthreads == 1, ("multiple threads in thread_wait()")); 514624bf9e1SKonstantin Belousov KASSERT(p->p_exitthreads == 0, ("p_exitthreads leaking")); 515ff8fbcffSJeff Roberson td = FIRST_THREAD_IN_PROC(p); 516ff8fbcffSJeff Roberson /* Lock the last thread so we spin until it exits cpu_throw(). */ 517ff8fbcffSJeff Roberson thread_lock(td); 518ff8fbcffSJeff Roberson thread_unlock(td); 5192e6b8de4SJeff Roberson lock_profile_thread_exit(td); 520d7f687fcSJeff Roberson cpuset_rel(td->td_cpuset); 521d7f687fcSJeff Roberson td->td_cpuset = NULL; 522696058c3SJulian Elischer cpu_thread_clean(td); 523ed062c8dSJulian Elischer crfree(td->td_ucred); 524696058c3SJulian Elischer thread_reap(); /* check for zombie threads etc. */ 525696058c3SJulian Elischer } 526696058c3SJulian Elischer 527696058c3SJulian Elischer /* 52844990b8cSJulian Elischer * Link a thread to a process. 5291faf202eSJulian Elischer * set up anything that needs to be initialized for it to 5301faf202eSJulian Elischer * be used by the process. 53144990b8cSJulian Elischer */ 53244990b8cSJulian Elischer void 5338460a577SJohn Birrell thread_link(struct thread *td, struct proc *p) 53444990b8cSJulian Elischer { 53544990b8cSJulian Elischer 536a54e85fdSJeff Roberson /* 537a54e85fdSJeff Roberson * XXX This can't be enabled because it's called for proc0 before 538374ae2a3SJeff Roberson * its lock has been created. 539374ae2a3SJeff Roberson * PROC_LOCK_ASSERT(p, MA_OWNED); 540a54e85fdSJeff Roberson */ 54171fad9fdSJulian Elischer td->td_state = TDS_INACTIVE; 54244990b8cSJulian Elischer td->td_proc = p; 543b61ce5b0SJeff Roberson td->td_flags = TDF_INMEM; 54444990b8cSJulian Elischer 5451faf202eSJulian Elischer LIST_INIT(&td->td_contested); 546eea4f254SJeff Roberson LIST_INIT(&td->td_lprof[0]); 547eea4f254SJeff Roberson LIST_INIT(&td->td_lprof[1]); 5489104847fSDavid Xu sigqueue_init(&td->td_sigqueue, p); 549*fd90e2edSJung-uk Kim callout_init(&td->td_slpcallout, 1); 55066d8df9dSDaniel Eischen TAILQ_INSERT_TAIL(&p->p_threads, td, td_plist); 55144990b8cSJulian Elischer p->p_numthreads++; 55244990b8cSJulian Elischer } 55344990b8cSJulian Elischer 554ed062c8dSJulian Elischer /* 555ed062c8dSJulian Elischer * Called from: 556ed062c8dSJulian Elischer * thread_exit() 557ed062c8dSJulian Elischer */ 558d3a0bd78SJulian Elischer void 559d3a0bd78SJulian Elischer thread_unlink(struct thread *td) 560d3a0bd78SJulian Elischer { 561d3a0bd78SJulian Elischer struct proc *p = td->td_proc; 562d3a0bd78SJulian Elischer 563374ae2a3SJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 564d3a0bd78SJulian Elischer TAILQ_REMOVE(&p->p_threads, td, td_plist); 565d3a0bd78SJulian Elischer p->p_numthreads--; 566d3a0bd78SJulian Elischer /* could clear a few other things here */ 5678460a577SJohn Birrell /* Must NOT clear links to proc! */ 5685c8329edSJulian Elischer } 5695c8329edSJulian Elischer 57079799053SKonstantin Belousov static int 57179799053SKonstantin Belousov calc_remaining(struct proc *p, int mode) 57279799053SKonstantin Belousov { 57379799053SKonstantin Belousov int remaining; 57479799053SKonstantin Belousov 5757b519077SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 5767b519077SKonstantin Belousov PROC_SLOCK_ASSERT(p, MA_OWNED); 57779799053SKonstantin Belousov if (mode == SINGLE_EXIT) 57879799053SKonstantin Belousov remaining = p->p_numthreads; 57979799053SKonstantin Belousov else if (mode == SINGLE_BOUNDARY) 58079799053SKonstantin Belousov remaining = p->p_numthreads - p->p_boundary_count; 5816ddcc233SKonstantin Belousov else if (mode == SINGLE_NO_EXIT || mode == SINGLE_ALLPROC) 58279799053SKonstantin Belousov remaining = p->p_numthreads - p->p_suspcount; 58379799053SKonstantin Belousov else 58479799053SKonstantin Belousov panic("calc_remaining: wrong mode %d", mode); 58579799053SKonstantin Belousov return (remaining); 58679799053SKonstantin Belousov } 58779799053SKonstantin Belousov 58807a9368aSKonstantin Belousov static int 58907a9368aSKonstantin Belousov remain_for_mode(int mode) 59007a9368aSKonstantin Belousov { 59107a9368aSKonstantin Belousov 5926ddcc233SKonstantin Belousov return (mode == SINGLE_ALLPROC ? 0 : 1); 59307a9368aSKonstantin Belousov } 59407a9368aSKonstantin Belousov 59507a9368aSKonstantin Belousov static int 59607a9368aSKonstantin Belousov weed_inhib(int mode, struct thread *td2, struct proc *p) 59707a9368aSKonstantin Belousov { 59807a9368aSKonstantin Belousov int wakeup_swapper; 59907a9368aSKonstantin Belousov 60007a9368aSKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 60107a9368aSKonstantin Belousov PROC_SLOCK_ASSERT(p, MA_OWNED); 60207a9368aSKonstantin Belousov THREAD_LOCK_ASSERT(td2, MA_OWNED); 60307a9368aSKonstantin Belousov 60407a9368aSKonstantin Belousov wakeup_swapper = 0; 60507a9368aSKonstantin Belousov switch (mode) { 60607a9368aSKonstantin Belousov case SINGLE_EXIT: 60707a9368aSKonstantin Belousov if (TD_IS_SUSPENDED(td2)) 60884cdea97SKonstantin Belousov wakeup_swapper |= thread_unsuspend_one(td2, p, true); 60907a9368aSKonstantin Belousov if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR) != 0) 61007a9368aSKonstantin Belousov wakeup_swapper |= sleepq_abort(td2, EINTR); 61107a9368aSKonstantin Belousov break; 61207a9368aSKonstantin Belousov case SINGLE_BOUNDARY: 61307a9368aSKonstantin Belousov if (TD_IS_SUSPENDED(td2) && (td2->td_flags & TDF_BOUNDARY) == 0) 61484cdea97SKonstantin Belousov wakeup_swapper |= thread_unsuspend_one(td2, p, false); 61507a9368aSKonstantin Belousov if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR) != 0) 61607a9368aSKonstantin Belousov wakeup_swapper |= sleepq_abort(td2, ERESTART); 61707a9368aSKonstantin Belousov break; 61807a9368aSKonstantin Belousov case SINGLE_NO_EXIT: 61907a9368aSKonstantin Belousov if (TD_IS_SUSPENDED(td2) && (td2->td_flags & TDF_BOUNDARY) == 0) 62084cdea97SKonstantin Belousov wakeup_swapper |= thread_unsuspend_one(td2, p, false); 62107a9368aSKonstantin Belousov if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR) != 0) 62207a9368aSKonstantin Belousov wakeup_swapper |= sleepq_abort(td2, ERESTART); 623917dd390SKonstantin Belousov break; 6246ddcc233SKonstantin Belousov case SINGLE_ALLPROC: 6256ddcc233SKonstantin Belousov /* 6266ddcc233SKonstantin Belousov * ALLPROC suspend tries to avoid spurious EINTR for 6276ddcc233SKonstantin Belousov * threads sleeping interruptable, by suspending the 6286ddcc233SKonstantin Belousov * thread directly, similarly to sig_suspend_threads(). 6296ddcc233SKonstantin Belousov * Since such sleep is not performed at the user 6306ddcc233SKonstantin Belousov * boundary, TDF_BOUNDARY flag is not set, and TDF_ALLPROCSUSP 6316ddcc233SKonstantin Belousov * is used to avoid immediate un-suspend. 6326ddcc233SKonstantin Belousov */ 6336ddcc233SKonstantin Belousov if (TD_IS_SUSPENDED(td2) && (td2->td_flags & (TDF_BOUNDARY | 6346ddcc233SKonstantin Belousov TDF_ALLPROCSUSP)) == 0) 63584cdea97SKonstantin Belousov wakeup_swapper |= thread_unsuspend_one(td2, p, false); 6366ddcc233SKonstantin Belousov if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR) != 0) { 6376ddcc233SKonstantin Belousov if ((td2->td_flags & TDF_SBDRY) == 0) { 6386ddcc233SKonstantin Belousov thread_suspend_one(td2); 6396ddcc233SKonstantin Belousov td2->td_flags |= TDF_ALLPROCSUSP; 6406ddcc233SKonstantin Belousov } else { 6416ddcc233SKonstantin Belousov wakeup_swapper |= sleepq_abort(td2, ERESTART); 6426ddcc233SKonstantin Belousov } 6436ddcc233SKonstantin Belousov } 64407a9368aSKonstantin Belousov break; 64507a9368aSKonstantin Belousov } 64607a9368aSKonstantin Belousov return (wakeup_swapper); 64707a9368aSKonstantin Belousov } 64807a9368aSKonstantin Belousov 6495215b187SJeff Roberson /* 65044990b8cSJulian Elischer * Enforce single-threading. 65144990b8cSJulian Elischer * 65244990b8cSJulian Elischer * Returns 1 if the caller must abort (another thread is waiting to 65344990b8cSJulian Elischer * exit the process or similar). Process is locked! 65444990b8cSJulian Elischer * Returns 0 when you are successfully the only thread running. 65544990b8cSJulian Elischer * A process has successfully single threaded in the suspend mode when 65644990b8cSJulian Elischer * There are no threads in user mode. Threads in the kernel must be 65744990b8cSJulian Elischer * allowed to continue until they get to the user boundary. They may even 65844990b8cSJulian Elischer * copy out their return values and data before suspending. They may however be 659e2668f55SMaxim Konovalov * accelerated in reaching the user boundary as we will wake up 66044990b8cSJulian Elischer * any sleeping threads that are interruptable. (PCATCH). 66144990b8cSJulian Elischer */ 66244990b8cSJulian Elischer int 6636ddcc233SKonstantin Belousov thread_single(struct proc *p, int mode) 66444990b8cSJulian Elischer { 66544990b8cSJulian Elischer struct thread *td; 66644990b8cSJulian Elischer struct thread *td2; 667da7bbd2cSJohn Baldwin int remaining, wakeup_swapper; 66844990b8cSJulian Elischer 66944990b8cSJulian Elischer td = curthread; 6706ddcc233SKonstantin Belousov KASSERT(mode == SINGLE_EXIT || mode == SINGLE_BOUNDARY || 6716ddcc233SKonstantin Belousov mode == SINGLE_ALLPROC || mode == SINGLE_NO_EXIT, 6726ddcc233SKonstantin Belousov ("invalid mode %d", mode)); 6736ddcc233SKonstantin Belousov /* 6746ddcc233SKonstantin Belousov * If allowing non-ALLPROC singlethreading for non-curproc 6756ddcc233SKonstantin Belousov * callers, calc_remaining() and remain_for_mode() should be 6766ddcc233SKonstantin Belousov * adjusted to also account for td->td_proc != p. For now 6776ddcc233SKonstantin Belousov * this is not implemented because it is not used. 6786ddcc233SKonstantin Belousov */ 6796ddcc233SKonstantin Belousov KASSERT((mode == SINGLE_ALLPROC && td->td_proc != p) || 6806ddcc233SKonstantin Belousov (mode != SINGLE_ALLPROC && td->td_proc == p), 6816ddcc233SKonstantin Belousov ("mode %d proc %p curproc %p", mode, p, td->td_proc)); 68237814395SPeter Wemm mtx_assert(&Giant, MA_NOTOWNED); 68344990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 68444990b8cSJulian Elischer 6856ddcc233SKonstantin Belousov if ((p->p_flag & P_HADTHREADS) == 0 && mode != SINGLE_ALLPROC) 68644990b8cSJulian Elischer return (0); 68744990b8cSJulian Elischer 688e3b9bf71SJulian Elischer /* Is someone already single threading? */ 689906ac69dSDavid Xu if (p->p_singlethread != NULL && p->p_singlethread != td) 69044990b8cSJulian Elischer return (1); 69144990b8cSJulian Elischer 692906ac69dSDavid Xu if (mode == SINGLE_EXIT) { 693906ac69dSDavid Xu p->p_flag |= P_SINGLE_EXIT; 694906ac69dSDavid Xu p->p_flag &= ~P_SINGLE_BOUNDARY; 695906ac69dSDavid Xu } else { 696906ac69dSDavid Xu p->p_flag &= ~P_SINGLE_EXIT; 697906ac69dSDavid Xu if (mode == SINGLE_BOUNDARY) 698906ac69dSDavid Xu p->p_flag |= P_SINGLE_BOUNDARY; 699906ac69dSDavid Xu else 700906ac69dSDavid Xu p->p_flag &= ~P_SINGLE_BOUNDARY; 701906ac69dSDavid Xu } 7026ddcc233SKonstantin Belousov if (mode == SINGLE_ALLPROC) 7036ddcc233SKonstantin Belousov p->p_flag |= P_TOTAL_STOP; 7041279572aSDavid Xu p->p_flag |= P_STOPPED_SINGLE; 7057b4a950aSDavid Xu PROC_SLOCK(p); 706112afcb2SJohn Baldwin p->p_singlethread = td; 70779799053SKonstantin Belousov remaining = calc_remaining(p, mode); 70807a9368aSKonstantin Belousov while (remaining != remain_for_mode(mode)) { 709bf1a3220SDavid Xu if (P_SHOULDSTOP(p) != P_STOPPED_SINGLE) 710bf1a3220SDavid Xu goto stopme; 711da7bbd2cSJohn Baldwin wakeup_swapper = 0; 71244990b8cSJulian Elischer FOREACH_THREAD_IN_PROC(p, td2) { 71344990b8cSJulian Elischer if (td2 == td) 71444990b8cSJulian Elischer continue; 715a54e85fdSJeff Roberson thread_lock(td2); 716b7edba77SJeff Roberson td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK; 7176ddcc233SKonstantin Belousov if (TD_IS_INHIBITED(td2)) { 71807a9368aSKonstantin Belousov wakeup_swapper |= weed_inhib(mode, td2, p); 719d8267df7SDavid Xu #ifdef SMP 7206ddcc233SKonstantin Belousov } else if (TD_IS_RUNNING(td2) && td != td2) { 721d8267df7SDavid Xu forward_signal(td2); 722d8267df7SDavid Xu #endif 7236ddcc233SKonstantin Belousov } 724a54e85fdSJeff Roberson thread_unlock(td2); 7259d102777SJulian Elischer } 726da7bbd2cSJohn Baldwin if (wakeup_swapper) 727da7bbd2cSJohn Baldwin kick_proc0(); 72879799053SKonstantin Belousov remaining = calc_remaining(p, mode); 729ec008e96SDavid Xu 7309d102777SJulian Elischer /* 7319d102777SJulian Elischer * Maybe we suspended some threads.. was it enough? 7329d102777SJulian Elischer */ 73307a9368aSKonstantin Belousov if (remaining == remain_for_mode(mode)) 7349d102777SJulian Elischer break; 7359d102777SJulian Elischer 736bf1a3220SDavid Xu stopme: 73744990b8cSJulian Elischer /* 73844990b8cSJulian Elischer * Wake us up when everyone else has suspended. 739e3b9bf71SJulian Elischer * In the mean time we suspend as well. 74044990b8cSJulian Elischer */ 7416ddcc233SKonstantin Belousov thread_suspend_switch(td, p); 74279799053SKonstantin Belousov remaining = calc_remaining(p, mode); 74344990b8cSJulian Elischer } 744906ac69dSDavid Xu if (mode == SINGLE_EXIT) { 74591599697SJulian Elischer /* 7468626a0ddSKonstantin Belousov * Convert the process to an unthreaded process. The 7478626a0ddSKonstantin Belousov * SINGLE_EXIT is called by exit1() or execve(), in 7488626a0ddSKonstantin Belousov * both cases other threads must be retired. 74991599697SJulian Elischer */ 7508626a0ddSKonstantin Belousov KASSERT(p->p_numthreads == 1, ("Unthreading with >1 threads")); 751ed062c8dSJulian Elischer p->p_singlethread = NULL; 7528626a0ddSKonstantin Belousov p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_HADTHREADS); 753fd229b5bSKonstantin Belousov 754fd229b5bSKonstantin Belousov /* 755fd229b5bSKonstantin Belousov * Wait for any remaining threads to exit cpu_throw(). 756fd229b5bSKonstantin Belousov */ 757fd229b5bSKonstantin Belousov while (p->p_exitthreads != 0) { 758fd229b5bSKonstantin Belousov PROC_SUNLOCK(p); 759fd229b5bSKonstantin Belousov PROC_UNLOCK(p); 760fd229b5bSKonstantin Belousov sched_relinquish(td); 761fd229b5bSKonstantin Belousov PROC_LOCK(p); 762fd229b5bSKonstantin Belousov PROC_SLOCK(p); 763fd229b5bSKonstantin Belousov } 764ac437c07SKonstantin Belousov } else if (mode == SINGLE_BOUNDARY) { 765ac437c07SKonstantin Belousov /* 766ac437c07SKonstantin Belousov * Wait until all suspended threads are removed from 767ac437c07SKonstantin Belousov * the processors. The thread_suspend_check() 768ac437c07SKonstantin Belousov * increments p_boundary_count while it is still 769ac437c07SKonstantin Belousov * running, which makes it possible for the execve() 770ac437c07SKonstantin Belousov * to destroy vmspace while our other threads are 771ac437c07SKonstantin Belousov * still using the address space. 772ac437c07SKonstantin Belousov * 773ac437c07SKonstantin Belousov * We lock the thread, which is only allowed to 774ac437c07SKonstantin Belousov * succeed after context switch code finished using 775ac437c07SKonstantin Belousov * the address space. 776ac437c07SKonstantin Belousov */ 777ac437c07SKonstantin Belousov FOREACH_THREAD_IN_PROC(p, td2) { 778ac437c07SKonstantin Belousov if (td2 == td) 779ac437c07SKonstantin Belousov continue; 780ac437c07SKonstantin Belousov thread_lock(td2); 781ac437c07SKonstantin Belousov KASSERT((td2->td_flags & TDF_BOUNDARY) != 0, 782ac437c07SKonstantin Belousov ("td %p not on boundary", td2)); 783ac437c07SKonstantin Belousov KASSERT(TD_IS_SUSPENDED(td2), 784ac437c07SKonstantin Belousov ("td %p is not suspended", td2)); 785ac437c07SKonstantin Belousov thread_unlock(td2); 786ac437c07SKonstantin Belousov } 78791599697SJulian Elischer } 7887b4a950aSDavid Xu PROC_SUNLOCK(p); 78944990b8cSJulian Elischer return (0); 79044990b8cSJulian Elischer } 79144990b8cSJulian Elischer 7928638fe7bSKonstantin Belousov bool 7938638fe7bSKonstantin Belousov thread_suspend_check_needed(void) 7948638fe7bSKonstantin Belousov { 7958638fe7bSKonstantin Belousov struct proc *p; 7968638fe7bSKonstantin Belousov struct thread *td; 7978638fe7bSKonstantin Belousov 7988638fe7bSKonstantin Belousov td = curthread; 7998638fe7bSKonstantin Belousov p = td->td_proc; 8008638fe7bSKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 8018638fe7bSKonstantin Belousov return (P_SHOULDSTOP(p) || ((p->p_flag & P_TRACED) != 0 && 8028638fe7bSKonstantin Belousov (td->td_dbgflags & TDB_SUSPEND) != 0)); 8038638fe7bSKonstantin Belousov } 8048638fe7bSKonstantin Belousov 80544990b8cSJulian Elischer /* 80644990b8cSJulian Elischer * Called in from locations that can safely check to see 80744990b8cSJulian Elischer * whether we have to suspend or at least throttle for a 80844990b8cSJulian Elischer * single-thread event (e.g. fork). 80944990b8cSJulian Elischer * 81044990b8cSJulian Elischer * Such locations include userret(). 81144990b8cSJulian Elischer * If the "return_instead" argument is non zero, the thread must be able to 81244990b8cSJulian Elischer * accept 0 (caller may continue), or 1 (caller must abort) as a result. 81344990b8cSJulian Elischer * 81444990b8cSJulian Elischer * The 'return_instead' argument tells the function if it may do a 81544990b8cSJulian Elischer * thread_exit() or suspend, or whether the caller must abort and back 81644990b8cSJulian Elischer * out instead. 81744990b8cSJulian Elischer * 81844990b8cSJulian Elischer * If the thread that set the single_threading request has set the 81944990b8cSJulian Elischer * P_SINGLE_EXIT bit in the process flags then this call will never return 82044990b8cSJulian Elischer * if 'return_instead' is false, but will exit. 82144990b8cSJulian Elischer * 82244990b8cSJulian Elischer * P_SINGLE_EXIT | return_instead == 0| return_instead != 0 82344990b8cSJulian Elischer *---------------+--------------------+--------------------- 82444990b8cSJulian Elischer * 0 | returns 0 | returns 0 or 1 825353374b5SJohn Baldwin * | when ST ends | immediately 82644990b8cSJulian Elischer *---------------+--------------------+--------------------- 82744990b8cSJulian Elischer * 1 | thread exits | returns 1 828353374b5SJohn Baldwin * | | immediately 82944990b8cSJulian Elischer * 0 = thread_exit() or suspension ok, 83044990b8cSJulian Elischer * other = return error instead of stopping the thread. 83144990b8cSJulian Elischer * 83244990b8cSJulian Elischer * While a full suspension is under effect, even a single threading 83344990b8cSJulian Elischer * thread would be suspended if it made this call (but it shouldn't). 83444990b8cSJulian Elischer * This call should only be made from places where 83544990b8cSJulian Elischer * thread_exit() would be safe as that may be the outcome unless 83644990b8cSJulian Elischer * return_instead is set. 83744990b8cSJulian Elischer */ 83844990b8cSJulian Elischer int 83944990b8cSJulian Elischer thread_suspend_check(int return_instead) 84044990b8cSJulian Elischer { 841ecafb24bSJuli Mallett struct thread *td; 842ecafb24bSJuli Mallett struct proc *p; 8437847a9daSJohn Baldwin int wakeup_swapper; 84444990b8cSJulian Elischer 84544990b8cSJulian Elischer td = curthread; 84644990b8cSJulian Elischer p = td->td_proc; 84737814395SPeter Wemm mtx_assert(&Giant, MA_NOTOWNED); 84844990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 8498638fe7bSKonstantin Belousov while (thread_suspend_check_needed()) { 8501279572aSDavid Xu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 85144990b8cSJulian Elischer KASSERT(p->p_singlethread != NULL, 85244990b8cSJulian Elischer ("singlethread not set")); 85344990b8cSJulian Elischer /* 854e3b9bf71SJulian Elischer * The only suspension in action is a 855e3b9bf71SJulian Elischer * single-threading. Single threader need not stop. 856b6d5995eSJulian Elischer * XXX Should be safe to access unlocked 857b6d5995eSJulian Elischer * as it can only be set to be true by us. 85844990b8cSJulian Elischer */ 859e3b9bf71SJulian Elischer if (p->p_singlethread == td) 86044990b8cSJulian Elischer return (0); /* Exempt from stopping. */ 86144990b8cSJulian Elischer } 86245a4bfa1SDavid Xu if ((p->p_flag & P_SINGLE_EXIT) && return_instead) 86394f0972bSDavid Xu return (EINTR); 86444990b8cSJulian Elischer 865906ac69dSDavid Xu /* Should we goto user boundary if we didn't come from there? */ 866906ac69dSDavid Xu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE && 867906ac69dSDavid Xu (p->p_flag & P_SINGLE_BOUNDARY) && return_instead) 86894f0972bSDavid Xu return (ERESTART); 869906ac69dSDavid Xu 87044990b8cSJulian Elischer /* 871d071a6faSJohn Baldwin * Ignore suspend requests for stop signals if they 872d071a6faSJohn Baldwin * are deferred. 873d071a6faSJohn Baldwin */ 8746ddcc233SKonstantin Belousov if ((P_SHOULDSTOP(p) == P_STOPPED_SIG || 8756ddcc233SKonstantin Belousov (p->p_flag & P_TOTAL_STOP) != 0) && 8766ddcc233SKonstantin Belousov (td->td_flags & TDF_SBDRY) != 0) { 877d071a6faSJohn Baldwin KASSERT(return_instead, 878d071a6faSJohn Baldwin ("TDF_SBDRY set for unsafe thread_suspend_check")); 879d071a6faSJohn Baldwin return (0); 880d071a6faSJohn Baldwin } 881d071a6faSJohn Baldwin 882d071a6faSJohn Baldwin /* 88344990b8cSJulian Elischer * If the process is waiting for us to exit, 88444990b8cSJulian Elischer * this thread should just suicide. 8851279572aSDavid Xu * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE. 88644990b8cSJulian Elischer */ 887cf7d9a8cSDavid Xu if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) { 888cf7d9a8cSDavid Xu PROC_UNLOCK(p); 889cf7d9a8cSDavid Xu tidhash_remove(td); 890cf7d9a8cSDavid Xu PROC_LOCK(p); 89121ecd1e9SDavid Xu tdsigcleanup(td); 89213dad108SKonstantin Belousov umtx_thread_exit(td); 893cf7d9a8cSDavid Xu PROC_SLOCK(p); 89421ecd1e9SDavid Xu thread_stopped(p); 89544990b8cSJulian Elischer thread_exit(); 896cf7d9a8cSDavid Xu } 89721ecd1e9SDavid Xu 89821ecd1e9SDavid Xu PROC_SLOCK(p); 89921ecd1e9SDavid Xu thread_stopped(p); 900a54e85fdSJeff Roberson if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 901a54e85fdSJeff Roberson if (p->p_numthreads == p->p_suspcount + 1) { 902a54e85fdSJeff Roberson thread_lock(p->p_singlethread); 90384cdea97SKonstantin Belousov wakeup_swapper = thread_unsuspend_one( 90484cdea97SKonstantin Belousov p->p_singlethread, p, false); 905a54e85fdSJeff Roberson thread_unlock(p->p_singlethread); 9067847a9daSJohn Baldwin if (wakeup_swapper) 9077847a9daSJohn Baldwin kick_proc0(); 908a54e85fdSJeff Roberson } 909a54e85fdSJeff Roberson } 9103f9be10eSDavid Xu PROC_UNLOCK(p); 9117b4a950aSDavid Xu thread_lock(td); 91244990b8cSJulian Elischer /* 91344990b8cSJulian Elischer * When a thread suspends, it just 914ad1e7d28SJulian Elischer * gets taken off all queues. 91544990b8cSJulian Elischer */ 91671fad9fdSJulian Elischer thread_suspend_one(td); 917906ac69dSDavid Xu if (return_instead == 0) { 918906ac69dSDavid Xu p->p_boundary_count++; 919906ac69dSDavid Xu td->td_flags |= TDF_BOUNDARY; 920cf19bf91SJulian Elischer } 9217b4a950aSDavid Xu PROC_SUNLOCK(p); 9228df78c41SJeff Roberson mi_switch(SW_INVOL | SWT_SUSPEND, NULL); 923a54e85fdSJeff Roberson thread_unlock(td); 92444990b8cSJulian Elischer PROC_LOCK(p); 92544990b8cSJulian Elischer } 92644990b8cSJulian Elischer return (0); 92744990b8cSJulian Elischer } 92844990b8cSJulian Elischer 92935c32a76SDavid Xu void 9306ddcc233SKonstantin Belousov thread_suspend_switch(struct thread *td, struct proc *p) 931a54e85fdSJeff Roberson { 932a54e85fdSJeff Roberson 933a54e85fdSJeff Roberson KASSERT(!TD_IS_SUSPENDED(td), ("already suspended")); 934a54e85fdSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 9357b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 936a54e85fdSJeff Roberson /* 937a54e85fdSJeff Roberson * We implement thread_suspend_one in stages here to avoid 938a54e85fdSJeff Roberson * dropping the proc lock while the thread lock is owned. 939a54e85fdSJeff Roberson */ 9406ddcc233SKonstantin Belousov if (p == td->td_proc) { 941a54e85fdSJeff Roberson thread_stopped(p); 942a54e85fdSJeff Roberson p->p_suspcount++; 9436ddcc233SKonstantin Belousov } 9443f9be10eSDavid Xu PROC_UNLOCK(p); 9457b4a950aSDavid Xu thread_lock(td); 946b7edba77SJeff Roberson td->td_flags &= ~TDF_NEEDSUSPCHK; 947a54e85fdSJeff Roberson TD_SET_SUSPENDED(td); 948c5aa6b58SJeff Roberson sched_sleep(td, 0); 9497b4a950aSDavid Xu PROC_SUNLOCK(p); 950a54e85fdSJeff Roberson DROP_GIANT(); 9518df78c41SJeff Roberson mi_switch(SW_VOL | SWT_SUSPEND, NULL); 952a54e85fdSJeff Roberson thread_unlock(td); 953a54e85fdSJeff Roberson PICKUP_GIANT(); 954a54e85fdSJeff Roberson PROC_LOCK(p); 9557b4a950aSDavid Xu PROC_SLOCK(p); 956a54e85fdSJeff Roberson } 957a54e85fdSJeff Roberson 958a54e85fdSJeff Roberson void 95935c32a76SDavid Xu thread_suspend_one(struct thread *td) 96035c32a76SDavid Xu { 9616ddcc233SKonstantin Belousov struct proc *p; 96235c32a76SDavid Xu 9636ddcc233SKonstantin Belousov p = td->td_proc; 9647b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 965a54e85fdSJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 966e574e444SDavid Xu KASSERT(!TD_IS_SUSPENDED(td), ("already suspended")); 96735c32a76SDavid Xu p->p_suspcount++; 968b7edba77SJeff Roberson td->td_flags &= ~TDF_NEEDSUSPCHK; 96971fad9fdSJulian Elischer TD_SET_SUSPENDED(td); 970c5aa6b58SJeff Roberson sched_sleep(td, 0); 97135c32a76SDavid Xu } 97235c32a76SDavid Xu 97384cdea97SKonstantin Belousov static int 97484cdea97SKonstantin Belousov thread_unsuspend_one(struct thread *td, struct proc *p, bool boundary) 97535c32a76SDavid Xu { 97635c32a76SDavid Xu 977a54e85fdSJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 978ad1e7d28SJulian Elischer KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended")); 97971fad9fdSJulian Elischer TD_CLR_SUSPENDED(td); 9806ddcc233SKonstantin Belousov td->td_flags &= ~TDF_ALLPROCSUSP; 9816ddcc233SKonstantin Belousov if (td->td_proc == p) { 9826ddcc233SKonstantin Belousov PROC_SLOCK_ASSERT(p, MA_OWNED); 98335c32a76SDavid Xu p->p_suspcount--; 98484cdea97SKonstantin Belousov if (boundary && (td->td_flags & TDF_BOUNDARY) != 0) { 98584cdea97SKonstantin Belousov td->td_flags &= ~TDF_BOUNDARY; 98684cdea97SKonstantin Belousov p->p_boundary_count--; 98784cdea97SKonstantin Belousov } 9886ddcc233SKonstantin Belousov } 9897847a9daSJohn Baldwin return (setrunnable(td)); 99035c32a76SDavid Xu } 99135c32a76SDavid Xu 99244990b8cSJulian Elischer /* 99344990b8cSJulian Elischer * Allow all threads blocked by single threading to continue running. 99444990b8cSJulian Elischer */ 99544990b8cSJulian Elischer void 99644990b8cSJulian Elischer thread_unsuspend(struct proc *p) 99744990b8cSJulian Elischer { 99844990b8cSJulian Elischer struct thread *td; 9997847a9daSJohn Baldwin int wakeup_swapper; 100044990b8cSJulian Elischer 100144990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 10027b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 10037847a9daSJohn Baldwin wakeup_swapper = 0; 100444990b8cSJulian Elischer if (!P_SHOULDSTOP(p)) { 1005ad1e7d28SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1006a54e85fdSJeff Roberson thread_lock(td); 1007ad1e7d28SJulian Elischer if (TD_IS_SUSPENDED(td)) { 100884cdea97SKonstantin Belousov wakeup_swapper |= thread_unsuspend_one(td, p, 100984cdea97SKonstantin Belousov true); 101044990b8cSJulian Elischer } 1011a54e85fdSJeff Roberson thread_unlock(td); 1012ad1e7d28SJulian Elischer } 101384cdea97SKonstantin Belousov } else if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE && 101484cdea97SKonstantin Belousov p->p_numthreads == p->p_suspcount) { 101544990b8cSJulian Elischer /* 101644990b8cSJulian Elischer * Stopping everything also did the job for the single 101744990b8cSJulian Elischer * threading request. Now we've downgraded to single-threaded, 101844990b8cSJulian Elischer * let it continue. 101944990b8cSJulian Elischer */ 10206ddcc233SKonstantin Belousov if (p->p_singlethread->td_proc == p) { 1021a54e85fdSJeff Roberson thread_lock(p->p_singlethread); 10226ddcc233SKonstantin Belousov wakeup_swapper = thread_unsuspend_one( 102384cdea97SKonstantin Belousov p->p_singlethread, p, false); 1024a54e85fdSJeff Roberson thread_unlock(p->p_singlethread); 102544990b8cSJulian Elischer } 10266ddcc233SKonstantin Belousov } 10277847a9daSJohn Baldwin if (wakeup_swapper) 10287847a9daSJohn Baldwin kick_proc0(); 102944990b8cSJulian Elischer } 103044990b8cSJulian Elischer 1031ed062c8dSJulian Elischer /* 1032ed062c8dSJulian Elischer * End the single threading mode.. 1033ed062c8dSJulian Elischer */ 103444990b8cSJulian Elischer void 10356ddcc233SKonstantin Belousov thread_single_end(struct proc *p, int mode) 103644990b8cSJulian Elischer { 103744990b8cSJulian Elischer struct thread *td; 10387847a9daSJohn Baldwin int wakeup_swapper; 103944990b8cSJulian Elischer 10406ddcc233SKonstantin Belousov KASSERT(mode == SINGLE_EXIT || mode == SINGLE_BOUNDARY || 10416ddcc233SKonstantin Belousov mode == SINGLE_ALLPROC || mode == SINGLE_NO_EXIT, 10426ddcc233SKonstantin Belousov ("invalid mode %d", mode)); 104344990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 10446ddcc233SKonstantin Belousov KASSERT((mode == SINGLE_ALLPROC && (p->p_flag & P_TOTAL_STOP) != 0) || 10456ddcc233SKonstantin Belousov (mode != SINGLE_ALLPROC && (p->p_flag & P_TOTAL_STOP) == 0), 10466ddcc233SKonstantin Belousov ("mode %d does not match P_TOTAL_STOP", mode)); 104784cdea97SKonstantin Belousov KASSERT(mode == SINGLE_ALLPROC || p->p_singlethread == curthread, 104884cdea97SKonstantin Belousov ("thread_single_end from other thread %p %p", 104984cdea97SKonstantin Belousov curthread, p->p_singlethread)); 105084cdea97SKonstantin Belousov KASSERT(mode != SINGLE_BOUNDARY || 105184cdea97SKonstantin Belousov (p->p_flag & P_SINGLE_BOUNDARY) != 0, 105284cdea97SKonstantin Belousov ("mis-matched SINGLE_BOUNDARY flags %x", p->p_flag)); 10536ddcc233SKonstantin Belousov p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_SINGLE_BOUNDARY | 10546ddcc233SKonstantin Belousov P_TOTAL_STOP); 10557b4a950aSDavid Xu PROC_SLOCK(p); 105644990b8cSJulian Elischer p->p_singlethread = NULL; 10577847a9daSJohn Baldwin wakeup_swapper = 0; 105849539972SJulian Elischer /* 10597847a9daSJohn Baldwin * If there are other threads they may now run, 106049539972SJulian Elischer * unless of course there is a blanket 'stop order' 106149539972SJulian Elischer * on the process. The single threader must be allowed 106249539972SJulian Elischer * to continue however as this is a bad place to stop. 106349539972SJulian Elischer */ 10646ddcc233SKonstantin Belousov if (p->p_numthreads != remain_for_mode(mode) && !P_SHOULDSTOP(p)) { 1065ad1e7d28SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1066a54e85fdSJeff Roberson thread_lock(td); 1067ad1e7d28SJulian Elischer if (TD_IS_SUSPENDED(td)) { 106884cdea97SKonstantin Belousov wakeup_swapper |= thread_unsuspend_one(td, p, 106984cdea97SKonstantin Belousov mode == SINGLE_BOUNDARY); 107044990b8cSJulian Elischer } 1071a54e85fdSJeff Roberson thread_unlock(td); 107249539972SJulian Elischer } 1073ad1e7d28SJulian Elischer } 107484cdea97SKonstantin Belousov KASSERT(mode != SINGLE_BOUNDARY || p->p_boundary_count == 0, 107584cdea97SKonstantin Belousov ("inconsistent boundary count %d", p->p_boundary_count)); 10767b4a950aSDavid Xu PROC_SUNLOCK(p); 10777847a9daSJohn Baldwin if (wakeup_swapper) 10787847a9daSJohn Baldwin kick_proc0(); 107949539972SJulian Elischer } 10804fc21c09SDaniel Eischen 108144355392SDavid Xu struct thread * 108244355392SDavid Xu thread_find(struct proc *p, lwpid_t tid) 108344355392SDavid Xu { 108444355392SDavid Xu struct thread *td; 108544355392SDavid Xu 108644355392SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 108744355392SDavid Xu FOREACH_THREAD_IN_PROC(p, td) { 108844355392SDavid Xu if (td->td_tid == tid) 108944355392SDavid Xu break; 109044355392SDavid Xu } 109144355392SDavid Xu return (td); 109244355392SDavid Xu } 1093cf7d9a8cSDavid Xu 1094cf7d9a8cSDavid Xu /* Locate a thread by number; return with proc lock held. */ 1095cf7d9a8cSDavid Xu struct thread * 1096cf7d9a8cSDavid Xu tdfind(lwpid_t tid, pid_t pid) 1097cf7d9a8cSDavid Xu { 1098cf7d9a8cSDavid Xu #define RUN_THRESH 16 1099cf7d9a8cSDavid Xu struct thread *td; 1100cf7d9a8cSDavid Xu int run = 0; 1101cf7d9a8cSDavid Xu 1102cf7d9a8cSDavid Xu rw_rlock(&tidhash_lock); 1103cf7d9a8cSDavid Xu LIST_FOREACH(td, TIDHASH(tid), td_hash) { 1104cf7d9a8cSDavid Xu if (td->td_tid == tid) { 1105cf7d9a8cSDavid Xu if (pid != -1 && td->td_proc->p_pid != pid) { 1106cf7d9a8cSDavid Xu td = NULL; 1107cf7d9a8cSDavid Xu break; 1108cf7d9a8cSDavid Xu } 11098e6fa660SJohn Baldwin PROC_LOCK(td->td_proc); 1110cf7d9a8cSDavid Xu if (td->td_proc->p_state == PRS_NEW) { 11118e6fa660SJohn Baldwin PROC_UNLOCK(td->td_proc); 1112cf7d9a8cSDavid Xu td = NULL; 1113cf7d9a8cSDavid Xu break; 1114cf7d9a8cSDavid Xu } 1115cf7d9a8cSDavid Xu if (run > RUN_THRESH) { 1116cf7d9a8cSDavid Xu if (rw_try_upgrade(&tidhash_lock)) { 1117cf7d9a8cSDavid Xu LIST_REMOVE(td, td_hash); 1118cf7d9a8cSDavid Xu LIST_INSERT_HEAD(TIDHASH(td->td_tid), 1119cf7d9a8cSDavid Xu td, td_hash); 1120cf7d9a8cSDavid Xu rw_wunlock(&tidhash_lock); 1121cf7d9a8cSDavid Xu return (td); 1122cf7d9a8cSDavid Xu } 1123cf7d9a8cSDavid Xu } 1124cf7d9a8cSDavid Xu break; 1125cf7d9a8cSDavid Xu } 1126cf7d9a8cSDavid Xu run++; 1127cf7d9a8cSDavid Xu } 1128cf7d9a8cSDavid Xu rw_runlock(&tidhash_lock); 1129cf7d9a8cSDavid Xu return (td); 1130cf7d9a8cSDavid Xu } 1131cf7d9a8cSDavid Xu 1132cf7d9a8cSDavid Xu void 1133cf7d9a8cSDavid Xu tidhash_add(struct thread *td) 1134cf7d9a8cSDavid Xu { 1135cf7d9a8cSDavid Xu rw_wlock(&tidhash_lock); 1136cf7d9a8cSDavid Xu LIST_INSERT_HEAD(TIDHASH(td->td_tid), td, td_hash); 1137cf7d9a8cSDavid Xu rw_wunlock(&tidhash_lock); 1138cf7d9a8cSDavid Xu } 1139cf7d9a8cSDavid Xu 1140cf7d9a8cSDavid Xu void 1141cf7d9a8cSDavid Xu tidhash_remove(struct thread *td) 1142cf7d9a8cSDavid Xu { 1143cf7d9a8cSDavid Xu rw_wlock(&tidhash_lock); 1144cf7d9a8cSDavid Xu LIST_REMOVE(td, td_hash); 1145cf7d9a8cSDavid Xu rw_wunlock(&tidhash_lock); 1146cf7d9a8cSDavid Xu } 1147