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> 48d1e7a4a5SJohn Baldwin #include <sys/syscallsubr.h> 4991d1786fSDmitry Chagin #include <sys/sysent.h> 50961a7b24SJohn Baldwin #include <sys/turnstile.h> 5144990b8cSJulian Elischer #include <sys/ktr.h> 52cf7d9a8cSDavid Xu #include <sys/rwlock.h> 53bc8e6d81SDavid Xu #include <sys/umtx.h> 549ed01c32SGleb Smirnoff #include <sys/vmmeter.h> 55d7f687fcSJeff Roberson #include <sys/cpuset.h> 5616d95d4fSJoseph Koshy #ifdef HWPMC_HOOKS 5716d95d4fSJoseph Koshy #include <sys/pmckern.h> 5816d95d4fSJoseph Koshy #endif 5944990b8cSJulian Elischer 60911b84b0SRobert Watson #include <security/audit/audit.h> 61911b84b0SRobert Watson 6244990b8cSJulian Elischer #include <vm/vm.h> 6349a2507bSAlan Cox #include <vm/vm_extern.h> 6444990b8cSJulian Elischer #include <vm/uma.h> 656520495aSAdrian Chadd #include <vm/vm_domain.h> 66b209f889SRandall Stewart #include <sys/eventhandler.h> 6702fb42b0SPeter Wemm 68b3e9e682SRyan Stone SDT_PROVIDER_DECLARE(proc); 69d9fae5abSAndriy Gapon SDT_PROBE_DEFINE(proc, , , lwp__exit); 70b3e9e682SRyan Stone 718460a577SJohn Birrell /* 728460a577SJohn Birrell * thread related storage. 738460a577SJohn Birrell */ 7444990b8cSJulian Elischer static uma_zone_t thread_zone; 7544990b8cSJulian Elischer 765215b187SJeff Roberson TAILQ_HEAD(, thread) zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads); 77c8790f5dSAttilio Rao static struct mtx zombie_lock; 78a54e85fdSJeff Roberson MTX_SYSINIT(zombie_lock, &zombie_lock, "zombie lock", MTX_SPIN); 7944990b8cSJulian Elischer 80ff8fbcffSJeff Roberson static void thread_zombie(struct thread *); 8184cdea97SKonstantin Belousov static int thread_unsuspend_one(struct thread *td, struct proc *p, 8284cdea97SKonstantin Belousov bool boundary); 83ff8fbcffSJeff Roberson 84ec6ea5e8SDavid Xu #define TID_BUFFER_SIZE 1024 85ec6ea5e8SDavid Xu 86fdcac928SMarcel Moolenaar struct mtx tid_lock; 871ea7a6f8SPoul-Henning Kamp static struct unrhdr *tid_unrhdr; 88ec6ea5e8SDavid Xu static lwpid_t tid_buffer[TID_BUFFER_SIZE]; 89ec6ea5e8SDavid Xu static int tid_head, tid_tail; 90cf7d9a8cSDavid Xu static MALLOC_DEFINE(M_TIDHASH, "tidhash", "thread hash"); 91cf7d9a8cSDavid Xu 92cf7d9a8cSDavid Xu struct tidhashhead *tidhashtbl; 93cf7d9a8cSDavid Xu u_long tidhash; 94cf7d9a8cSDavid Xu struct rwlock tidhash_lock; 95cf7d9a8cSDavid Xu 96ec6ea5e8SDavid Xu static lwpid_t 97ec6ea5e8SDavid Xu tid_alloc(void) 98ec6ea5e8SDavid Xu { 99ec6ea5e8SDavid Xu lwpid_t tid; 100ec6ea5e8SDavid Xu 101ec6ea5e8SDavid Xu tid = alloc_unr(tid_unrhdr); 102ec6ea5e8SDavid Xu if (tid != -1) 103ec6ea5e8SDavid Xu return (tid); 104ec6ea5e8SDavid Xu mtx_lock(&tid_lock); 105ec6ea5e8SDavid Xu if (tid_head == tid_tail) { 106ec6ea5e8SDavid Xu mtx_unlock(&tid_lock); 107ec6ea5e8SDavid Xu return (-1); 108ec6ea5e8SDavid Xu } 10994cb3545SKonstantin Belousov tid = tid_buffer[tid_head]; 11094cb3545SKonstantin Belousov tid_head = (tid_head + 1) % TID_BUFFER_SIZE; 111ec6ea5e8SDavid Xu mtx_unlock(&tid_lock); 112ec6ea5e8SDavid Xu return (tid); 113ec6ea5e8SDavid Xu } 114ec6ea5e8SDavid Xu 115ec6ea5e8SDavid Xu static void 116ec6ea5e8SDavid Xu tid_free(lwpid_t tid) 117ec6ea5e8SDavid Xu { 118ec6ea5e8SDavid Xu lwpid_t tmp_tid = -1; 119ec6ea5e8SDavid Xu 120ec6ea5e8SDavid Xu mtx_lock(&tid_lock); 121ec6ea5e8SDavid Xu if ((tid_tail + 1) % TID_BUFFER_SIZE == tid_head) { 12294cb3545SKonstantin Belousov tmp_tid = tid_buffer[tid_head]; 12394cb3545SKonstantin Belousov tid_head = (tid_head + 1) % TID_BUFFER_SIZE; 124ec6ea5e8SDavid Xu } 12594cb3545SKonstantin Belousov tid_buffer[tid_tail] = tid; 12694cb3545SKonstantin Belousov tid_tail = (tid_tail + 1) % TID_BUFFER_SIZE; 127ec6ea5e8SDavid Xu mtx_unlock(&tid_lock); 128ec6ea5e8SDavid Xu if (tmp_tid != -1) 129ec6ea5e8SDavid Xu free_unr(tid_unrhdr, tmp_tid); 130ec6ea5e8SDavid Xu } 131ec6ea5e8SDavid Xu 132fdcac928SMarcel Moolenaar /* 133696058c3SJulian Elischer * Prepare a thread for use. 13444990b8cSJulian Elischer */ 135b23f72e9SBrian Feldman static int 136b23f72e9SBrian Feldman thread_ctor(void *mem, int size, void *arg, int flags) 13744990b8cSJulian Elischer { 13844990b8cSJulian Elischer struct thread *td; 13944990b8cSJulian Elischer 14044990b8cSJulian Elischer td = (struct thread *)mem; 14171fad9fdSJulian Elischer td->td_state = TDS_INACTIVE; 142060563ecSJulian Elischer td->td_oncpu = NOCPU; 1436c27c603SJuli Mallett 144ec6ea5e8SDavid Xu td->td_tid = tid_alloc(); 145773eff9dSPoul-Henning Kamp 1466c27c603SJuli Mallett /* 1476c27c603SJuli Mallett * Note that td_critnest begins life as 1 because the thread is not 1486c27c603SJuli Mallett * running and is thereby implicitly waiting to be on the receiving 149a54e85fdSJeff Roberson * end of a context switch. 1506c27c603SJuli Mallett */ 151139b7550SJohn Baldwin td->td_critnest = 1; 152acbe332aSDavid Xu td->td_lend_user_pri = PRI_MAX; 153b209f889SRandall Stewart EVENTHANDLER_INVOKE(thread_ctor, td); 154911b84b0SRobert Watson #ifdef AUDIT 155911b84b0SRobert Watson audit_thread_alloc(td); 156911b84b0SRobert Watson #endif 157d10183d9SDavid Xu umtx_thread_alloc(td); 158b23f72e9SBrian Feldman return (0); 15944990b8cSJulian Elischer } 16044990b8cSJulian Elischer 16144990b8cSJulian Elischer /* 16244990b8cSJulian Elischer * Reclaim a thread after use. 16344990b8cSJulian Elischer */ 16444990b8cSJulian Elischer static void 16544990b8cSJulian Elischer thread_dtor(void *mem, int size, void *arg) 16644990b8cSJulian Elischer { 16744990b8cSJulian Elischer struct thread *td; 16844990b8cSJulian Elischer 16944990b8cSJulian Elischer td = (struct thread *)mem; 17044990b8cSJulian Elischer 17144990b8cSJulian Elischer #ifdef INVARIANTS 17244990b8cSJulian Elischer /* Verify that this thread is in a safe state to free. */ 17344990b8cSJulian Elischer switch (td->td_state) { 17471fad9fdSJulian Elischer case TDS_INHIBITED: 17571fad9fdSJulian Elischer case TDS_RUNNING: 17671fad9fdSJulian Elischer case TDS_CAN_RUN: 17744990b8cSJulian Elischer case TDS_RUNQ: 17844990b8cSJulian Elischer /* 17944990b8cSJulian Elischer * We must never unlink a thread that is in one of 18044990b8cSJulian Elischer * these states, because it is currently active. 18144990b8cSJulian Elischer */ 18244990b8cSJulian Elischer panic("bad state for thread unlinking"); 18344990b8cSJulian Elischer /* NOTREACHED */ 18471fad9fdSJulian Elischer case TDS_INACTIVE: 18544990b8cSJulian Elischer break; 18644990b8cSJulian Elischer default: 18744990b8cSJulian Elischer panic("bad thread state"); 18844990b8cSJulian Elischer /* NOTREACHED */ 18944990b8cSJulian Elischer } 19044990b8cSJulian Elischer #endif 1916e8525ceSRobert Watson #ifdef AUDIT 1926e8525ceSRobert Watson audit_thread_free(td); 1936e8525ceSRobert Watson #endif 1941ba4a712SPawel Jakub Dawidek /* Free all OSD associated to this thread. */ 1951ba4a712SPawel Jakub Dawidek osd_thread_exit(td); 196aca4bb91SKonstantin Belousov td_softdep_cleanup(td); 197aca4bb91SKonstantin Belousov MPASS(td->td_su == NULL); 1981ba4a712SPawel Jakub Dawidek 199b209f889SRandall Stewart EVENTHANDLER_INVOKE(thread_dtor, td); 200ec6ea5e8SDavid Xu tid_free(td->td_tid); 20144990b8cSJulian Elischer } 20244990b8cSJulian Elischer 20344990b8cSJulian Elischer /* 20444990b8cSJulian Elischer * Initialize type-stable parts of a thread (when newly created). 20544990b8cSJulian Elischer */ 206b23f72e9SBrian Feldman static int 207b23f72e9SBrian Feldman thread_init(void *mem, int size, int flags) 20844990b8cSJulian Elischer { 20944990b8cSJulian Elischer struct thread *td; 21044990b8cSJulian Elischer 21144990b8cSJulian Elischer td = (struct thread *)mem; 212247aba24SMarcel Moolenaar 21344f3b092SJohn Baldwin td->td_sleepqueue = sleepq_alloc(); 214961a7b24SJohn Baldwin td->td_turnstile = turnstile_alloc(); 2158f0e9130SKonstantin Belousov td->td_rlqe = NULL; 216b209f889SRandall Stewart EVENTHANDLER_INVOKE(thread_init, td); 217d10183d9SDavid Xu umtx_thread_init(td); 21889b57fcfSKonstantin Belousov td->td_kstack = 0; 219ad8b1d85SKonstantin Belousov td->td_sel = NULL; 220b23f72e9SBrian Feldman return (0); 22144990b8cSJulian Elischer } 22244990b8cSJulian Elischer 22344990b8cSJulian Elischer /* 22444990b8cSJulian Elischer * Tear down type-stable parts of a thread (just before being discarded). 22544990b8cSJulian Elischer */ 22644990b8cSJulian Elischer static void 22744990b8cSJulian Elischer thread_fini(void *mem, int size) 22844990b8cSJulian Elischer { 22944990b8cSJulian Elischer struct thread *td; 23044990b8cSJulian Elischer 23144990b8cSJulian Elischer td = (struct thread *)mem; 232b209f889SRandall Stewart EVENTHANDLER_INVOKE(thread_fini, td); 2338f0e9130SKonstantin Belousov rlqentry_free(td->td_rlqe); 234961a7b24SJohn Baldwin turnstile_free(td->td_turnstile); 23544f3b092SJohn Baldwin sleepq_free(td->td_sleepqueue); 236d10183d9SDavid Xu umtx_thread_fini(td); 237ace8398dSJeff Roberson seltdfini(td); 23844990b8cSJulian Elischer } 2395215b187SJeff Roberson 2405c8329edSJulian Elischer /* 2415215b187SJeff Roberson * For a newly created process, 2425215b187SJeff Roberson * link up all the structures and its initial threads etc. 243ed062c8dSJulian Elischer * called from: 244e7d939bdSMarcel Moolenaar * {arch}/{arch}/machdep.c {arch}_init(), init386() etc. 245ed062c8dSJulian Elischer * proc_dtor() (should go away) 246ed062c8dSJulian Elischer * proc_init() 2475c8329edSJulian Elischer */ 2485c8329edSJulian Elischer void 24989b57fcfSKonstantin Belousov proc_linkup0(struct proc *p, struct thread *td) 25089b57fcfSKonstantin Belousov { 25189b57fcfSKonstantin Belousov TAILQ_INIT(&p->p_threads); /* all threads in proc */ 25289b57fcfSKonstantin Belousov proc_linkup(p, td); 25389b57fcfSKonstantin Belousov } 25489b57fcfSKonstantin Belousov 25589b57fcfSKonstantin Belousov void 2568460a577SJohn Birrell proc_linkup(struct proc *p, struct thread *td) 2575c8329edSJulian Elischer { 258a54e85fdSJeff Roberson 2599104847fSDavid Xu sigqueue_init(&p->p_sigqueue, p); 260ebceaf6dSDavid Xu p->p_ksi = ksiginfo_alloc(1); 261ebceaf6dSDavid Xu if (p->p_ksi != NULL) { 2625c474517SDavid Xu /* XXX p_ksi may be null if ksiginfo zone is not ready */ 263ebceaf6dSDavid Xu p->p_ksi->ksi_flags = KSI_EXT | KSI_INS; 264ebceaf6dSDavid Xu } 265b2f92ef9SDavid Xu LIST_INIT(&p->p_mqnotifier); 2665c8329edSJulian Elischer p->p_numthreads = 0; 2678460a577SJohn Birrell thread_link(td, p); 2685c8329edSJulian Elischer } 2695c8329edSJulian Elischer 2705c8329edSJulian Elischer /* 27144990b8cSJulian Elischer * Initialize global thread allocation resources. 27244990b8cSJulian Elischer */ 27344990b8cSJulian Elischer void 27444990b8cSJulian Elischer threadinit(void) 27544990b8cSJulian Elischer { 27644990b8cSJulian Elischer 2771ea7a6f8SPoul-Henning Kamp mtx_init(&tid_lock, "TID lock", NULL, MTX_DEF); 27802c6fc21SKonstantin Belousov 27902c6fc21SKonstantin Belousov /* 280abce621cSKonstantin Belousov * pid_max cannot be greater than PID_MAX. 28102c6fc21SKonstantin Belousov * leave one number for thread0. 28202c6fc21SKonstantin Belousov */ 2836829a5c5SJulian Elischer tid_unrhdr = new_unrhdr(PID_MAX + 2, INT_MAX, &tid_lock); 2841ea7a6f8SPoul-Henning Kamp 285de028f5aSJeff Roberson thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(), 28644990b8cSJulian Elischer thread_ctor, thread_dtor, thread_init, thread_fini, 287f743ea96SMateusz Guzik 32 - 1, UMA_ZONE_NOFREE); 288cf7d9a8cSDavid Xu tidhashtbl = hashinit(maxproc / 2, M_TIDHASH, &tidhash); 289cf7d9a8cSDavid Xu rw_init(&tidhash_lock, "tidhash"); 29044990b8cSJulian Elischer } 29144990b8cSJulian Elischer 29244990b8cSJulian Elischer /* 293ff8fbcffSJeff Roberson * Place an unused thread on the zombie list. 294ad1e7d28SJulian Elischer * Use the slpq as that must be unused by now. 29544990b8cSJulian Elischer */ 29644990b8cSJulian Elischer void 297ff8fbcffSJeff Roberson thread_zombie(struct thread *td) 29844990b8cSJulian Elischer { 299a54e85fdSJeff Roberson mtx_lock_spin(&zombie_lock); 300ad1e7d28SJulian Elischer TAILQ_INSERT_HEAD(&zombie_threads, td, td_slpq); 301a54e85fdSJeff Roberson mtx_unlock_spin(&zombie_lock); 30244990b8cSJulian Elischer } 30344990b8cSJulian Elischer 3045c8329edSJulian Elischer /* 305ff8fbcffSJeff Roberson * Release a thread that has exited after cpu_throw(). 306ff8fbcffSJeff Roberson */ 307ff8fbcffSJeff Roberson void 308ff8fbcffSJeff Roberson thread_stash(struct thread *td) 309ff8fbcffSJeff Roberson { 310ff8fbcffSJeff Roberson atomic_subtract_rel_int(&td->td_proc->p_exitthreads, 1); 311ff8fbcffSJeff Roberson thread_zombie(td); 312ff8fbcffSJeff Roberson } 313ff8fbcffSJeff Roberson 314ff8fbcffSJeff Roberson /* 3156617724cSJeff Roberson * Reap zombie resources. 31644990b8cSJulian Elischer */ 31744990b8cSJulian Elischer void 31844990b8cSJulian Elischer thread_reap(void) 31944990b8cSJulian Elischer { 3205c8329edSJulian Elischer struct thread *td_first, *td_next; 32144990b8cSJulian Elischer 32244990b8cSJulian Elischer /* 3235215b187SJeff Roberson * Don't even bother to lock if none at this instant, 3242d19b736SKonstantin Belousov * we really don't care about the next instant. 32544990b8cSJulian Elischer */ 3268460a577SJohn Birrell if (!TAILQ_EMPTY(&zombie_threads)) { 327a54e85fdSJeff Roberson mtx_lock_spin(&zombie_lock); 3285c8329edSJulian Elischer td_first = TAILQ_FIRST(&zombie_threads); 3295c8329edSJulian Elischer if (td_first) 3305c8329edSJulian Elischer TAILQ_INIT(&zombie_threads); 331a54e85fdSJeff Roberson mtx_unlock_spin(&zombie_lock); 3325c8329edSJulian Elischer while (td_first) { 333ad1e7d28SJulian Elischer td_next = TAILQ_NEXT(td_first, td_slpq); 3344ea6a9a2SMateusz Guzik thread_cow_free(td_first); 3355c8329edSJulian Elischer thread_free(td_first); 3365c8329edSJulian Elischer td_first = td_next; 33744990b8cSJulian Elischer } 33844990b8cSJulian Elischer } 339ed062c8dSJulian Elischer } 34044990b8cSJulian Elischer 3414f0db5e0SJulian Elischer /* 34244990b8cSJulian Elischer * Allocate a thread. 34344990b8cSJulian Elischer */ 34444990b8cSJulian Elischer struct thread * 3458a945d10SKonstantin Belousov thread_alloc(int pages) 34644990b8cSJulian Elischer { 34789b57fcfSKonstantin Belousov struct thread *td; 3488460a577SJohn Birrell 34944990b8cSJulian Elischer thread_reap(); /* check if any zombies to get */ 35089b57fcfSKonstantin Belousov 35189b57fcfSKonstantin Belousov td = (struct thread *)uma_zalloc(thread_zone, M_WAITOK); 35289b57fcfSKonstantin Belousov KASSERT(td->td_kstack == 0, ("thread_alloc got thread with kstack")); 3538a945d10SKonstantin Belousov if (!vm_thread_new(td, pages)) { 35489b57fcfSKonstantin Belousov uma_zfree(thread_zone, td); 35589b57fcfSKonstantin Belousov return (NULL); 35689b57fcfSKonstantin Belousov } 3570c3967e7SMarcel Moolenaar cpu_thread_alloc(td); 3586520495aSAdrian Chadd vm_domain_policy_init(&td->td_vm_dom_policy); 35989b57fcfSKonstantin Belousov return (td); 36044990b8cSJulian Elischer } 36144990b8cSJulian Elischer 3628a945d10SKonstantin Belousov int 3638a945d10SKonstantin Belousov thread_alloc_stack(struct thread *td, int pages) 3648a945d10SKonstantin Belousov { 3658a945d10SKonstantin Belousov 3668a945d10SKonstantin Belousov KASSERT(td->td_kstack == 0, 3678a945d10SKonstantin Belousov ("thread_alloc_stack called on a thread with kstack")); 3688a945d10SKonstantin Belousov if (!vm_thread_new(td, pages)) 3698a945d10SKonstantin Belousov return (0); 3708a945d10SKonstantin Belousov cpu_thread_alloc(td); 3718a945d10SKonstantin Belousov return (1); 3728a945d10SKonstantin Belousov } 3734f0db5e0SJulian Elischer 3744f0db5e0SJulian Elischer /* 37544990b8cSJulian Elischer * Deallocate a thread. 37644990b8cSJulian Elischer */ 37744990b8cSJulian Elischer void 37844990b8cSJulian Elischer thread_free(struct thread *td) 37944990b8cSJulian Elischer { 3802e6b8de4SJeff Roberson 3812e6b8de4SJeff Roberson lock_profile_thread_exit(td); 38245aea8deSJeff Roberson if (td->td_cpuset) 383d7f687fcSJeff Roberson cpuset_rel(td->td_cpuset); 384d7f687fcSJeff Roberson td->td_cpuset = NULL; 3850c3967e7SMarcel Moolenaar cpu_thread_free(td); 38689b57fcfSKonstantin Belousov if (td->td_kstack != 0) 38789b57fcfSKonstantin Belousov vm_thread_dispose(td); 3886520495aSAdrian Chadd vm_domain_policy_cleanup(&td->td_vm_dom_policy); 3892d19b736SKonstantin Belousov callout_drain(&td->td_slpcallout); 39044990b8cSJulian Elischer uma_zfree(thread_zone, td); 39144990b8cSJulian Elischer } 39244990b8cSJulian Elischer 3934ea6a9a2SMateusz Guzik void 3944ea6a9a2SMateusz Guzik thread_cow_get_proc(struct thread *newtd, struct proc *p) 3954ea6a9a2SMateusz Guzik { 3964ea6a9a2SMateusz Guzik 3974ea6a9a2SMateusz Guzik PROC_LOCK_ASSERT(p, MA_OWNED); 3984ea6a9a2SMateusz Guzik newtd->td_ucred = crhold(p->p_ucred); 399f6f6d240SMateusz Guzik newtd->td_limit = lim_hold(p->p_limit); 4004ea6a9a2SMateusz Guzik newtd->td_cowgen = p->p_cowgen; 4014ea6a9a2SMateusz Guzik } 4024ea6a9a2SMateusz Guzik 4034ea6a9a2SMateusz Guzik void 4044ea6a9a2SMateusz Guzik thread_cow_get(struct thread *newtd, struct thread *td) 4054ea6a9a2SMateusz Guzik { 4064ea6a9a2SMateusz Guzik 4074ea6a9a2SMateusz Guzik newtd->td_ucred = crhold(td->td_ucred); 408f6f6d240SMateusz Guzik newtd->td_limit = lim_hold(td->td_limit); 4094ea6a9a2SMateusz Guzik newtd->td_cowgen = td->td_cowgen; 4104ea6a9a2SMateusz Guzik } 4114ea6a9a2SMateusz Guzik 4124ea6a9a2SMateusz Guzik void 4134ea6a9a2SMateusz Guzik thread_cow_free(struct thread *td) 4144ea6a9a2SMateusz Guzik { 4154ea6a9a2SMateusz Guzik 416cd672ca6SMateusz Guzik if (td->td_ucred != NULL) 4174ea6a9a2SMateusz Guzik crfree(td->td_ucred); 418cd672ca6SMateusz Guzik if (td->td_limit != NULL) 419f6f6d240SMateusz Guzik lim_free(td->td_limit); 4204ea6a9a2SMateusz Guzik } 4214ea6a9a2SMateusz Guzik 4224ea6a9a2SMateusz Guzik void 4234ea6a9a2SMateusz Guzik thread_cow_update(struct thread *td) 4244ea6a9a2SMateusz Guzik { 4254ea6a9a2SMateusz Guzik struct proc *p; 426cd672ca6SMateusz Guzik struct ucred *oldcred; 427cd672ca6SMateusz Guzik struct plimit *oldlimit; 4284ea6a9a2SMateusz Guzik 4294ea6a9a2SMateusz Guzik p = td->td_proc; 430cd672ca6SMateusz Guzik oldcred = NULL; 431cd672ca6SMateusz Guzik oldlimit = NULL; 4324ea6a9a2SMateusz Guzik PROC_LOCK(p); 433cd672ca6SMateusz Guzik if (td->td_ucred != p->p_ucred) { 434cd672ca6SMateusz Guzik oldcred = td->td_ucred; 435cd672ca6SMateusz Guzik td->td_ucred = crhold(p->p_ucred); 436cd672ca6SMateusz Guzik } 437cd672ca6SMateusz Guzik if (td->td_limit != p->p_limit) { 438cd672ca6SMateusz Guzik oldlimit = td->td_limit; 439cd672ca6SMateusz Guzik td->td_limit = lim_hold(p->p_limit); 440cd672ca6SMateusz Guzik } 4414ea6a9a2SMateusz Guzik td->td_cowgen = p->p_cowgen; 4424ea6a9a2SMateusz Guzik PROC_UNLOCK(p); 443cd672ca6SMateusz Guzik if (oldcred != NULL) 444cd672ca6SMateusz Guzik crfree(oldcred); 445cd672ca6SMateusz Guzik if (oldlimit != NULL) 446cd672ca6SMateusz Guzik lim_free(oldlimit); 4474ea6a9a2SMateusz Guzik } 4484ea6a9a2SMateusz Guzik 44944990b8cSJulian Elischer /* 45044990b8cSJulian Elischer * Discard the current thread and exit from its context. 45194e0a4cdSJulian Elischer * Always called with scheduler locked. 45244990b8cSJulian Elischer * 45344990b8cSJulian Elischer * Because we can't free a thread while we're operating under its context, 454696058c3SJulian Elischer * push the current thread into our CPU's deadthread holder. This means 455696058c3SJulian Elischer * we needn't worry about someone else grabbing our context before we 4566617724cSJeff Roberson * do a cpu_throw(). 45744990b8cSJulian Elischer */ 45844990b8cSJulian Elischer void 45944990b8cSJulian Elischer thread_exit(void) 46044990b8cSJulian Elischer { 4617e3a96eaSJohn Baldwin uint64_t runtime, new_switchtime; 46244990b8cSJulian Elischer struct thread *td; 4631c4bcd05SJeff Roberson struct thread *td2; 46444990b8cSJulian Elischer struct proc *p; 4657847a9daSJohn Baldwin int wakeup_swapper; 46644990b8cSJulian Elischer 46744990b8cSJulian Elischer td = curthread; 46844990b8cSJulian Elischer p = td->td_proc; 46944990b8cSJulian Elischer 470a54e85fdSJeff Roberson PROC_SLOCK_ASSERT(p, MA_OWNED); 471ed062c8dSJulian Elischer mtx_assert(&Giant, MA_NOTOWNED); 472a54e85fdSJeff Roberson 47344990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 474ed062c8dSJulian Elischer KASSERT(p != NULL, ("thread exiting without a process")); 475cc701b73SRobert Watson CTR3(KTR_PROC, "thread_exit: thread %p (pid %ld, %s)", td, 476e01eafefSJulian Elischer (long)p->p_pid, td->td_name); 4776c9271a9SAndriy Gapon SDT_PROBE0(proc, , , lwp__exit); 4789104847fSDavid Xu KASSERT(TAILQ_EMPTY(&td->td_sigqueue.sq_list), ("signal pending")); 47944990b8cSJulian Elischer 48089964dd2SRobert Watson #ifdef AUDIT 48189964dd2SRobert Watson AUDIT_SYSCALL_EXIT(0, td); 48289964dd2SRobert Watson #endif 483ed062c8dSJulian Elischer /* 484ed062c8dSJulian Elischer * drop FPU & debug register state storage, or any other 485ed062c8dSJulian Elischer * architecture specific resources that 486ed062c8dSJulian Elischer * would not be on a new untouched process. 487ed062c8dSJulian Elischer */ 488bd07998eSKonstantin Belousov cpu_thread_exit(td); 48944990b8cSJulian Elischer 490ed062c8dSJulian Elischer /* 4911faf202eSJulian Elischer * The last thread is left attached to the process 4921faf202eSJulian Elischer * So that the whole bundle gets recycled. Skip 493ed062c8dSJulian Elischer * all this stuff if we never had threads. 494ed062c8dSJulian Elischer * EXIT clears all sign of other threads when 495ed062c8dSJulian Elischer * it goes to single threading, so the last thread always 496ed062c8dSJulian Elischer * takes the short path. 4971faf202eSJulian Elischer */ 498ed062c8dSJulian Elischer if (p->p_flag & P_HADTHREADS) { 4991faf202eSJulian Elischer if (p->p_numthreads > 1) { 500fd229b5bSKonstantin Belousov atomic_add_int(&td->td_proc->p_exitthreads, 1); 501d3a0bd78SJulian Elischer thread_unlink(td); 5021c4bcd05SJeff Roberson td2 = FIRST_THREAD_IN_PROC(p); 5031c4bcd05SJeff Roberson sched_exit_thread(td2, td); 504ed062c8dSJulian Elischer 505ed062c8dSJulian Elischer /* 50644990b8cSJulian Elischer * The test below is NOT true if we are the 5079182554aSKonstantin Belousov * sole exiting thread. P_STOPPED_SINGLE is unset 50844990b8cSJulian Elischer * in exit1() after it is the only survivor. 50944990b8cSJulian Elischer */ 5101279572aSDavid Xu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 51144990b8cSJulian Elischer if (p->p_numthreads == p->p_suspcount) { 512a54e85fdSJeff Roberson thread_lock(p->p_singlethread); 5137847a9daSJohn Baldwin wakeup_swapper = thread_unsuspend_one( 51484cdea97SKonstantin Belousov p->p_singlethread, p, false); 515a54e85fdSJeff Roberson thread_unlock(p->p_singlethread); 5167847a9daSJohn Baldwin if (wakeup_swapper) 5177847a9daSJohn Baldwin kick_proc0(); 51844990b8cSJulian Elischer } 51944990b8cSJulian Elischer } 52048bfcdddSJulian Elischer 521696058c3SJulian Elischer PCPU_SET(deadthread, td); 5221faf202eSJulian Elischer } else { 523ed062c8dSJulian Elischer /* 524ed062c8dSJulian Elischer * The last thread is exiting.. but not through exit() 525ed062c8dSJulian Elischer */ 526ed062c8dSJulian Elischer panic ("thread_exit: Last thread exiting on its own"); 527ed062c8dSJulian Elischer } 5281faf202eSJulian Elischer } 52916d95d4fSJoseph Koshy #ifdef HWPMC_HOOKS 53016d95d4fSJoseph Koshy /* 53116d95d4fSJoseph Koshy * If this thread is part of a process that is being tracked by hwpmc(4), 53216d95d4fSJoseph Koshy * inform the module of the thread's impending exit. 53316d95d4fSJoseph Koshy */ 53416d95d4fSJoseph Koshy if (PMC_PROC_IS_USING_PMCS(td->td_proc)) 53516d95d4fSJoseph Koshy PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT); 53616d95d4fSJoseph Koshy #endif 537a54e85fdSJeff Roberson PROC_UNLOCK(p); 5385c7bebf9SKonstantin Belousov PROC_STATLOCK(p); 5395c7bebf9SKonstantin Belousov thread_lock(td); 5405c7bebf9SKonstantin Belousov PROC_SUNLOCK(p); 5417e3a96eaSJohn Baldwin 5427e3a96eaSJohn Baldwin /* Do the same timestamp bookkeeping that mi_switch() would do. */ 5437e3a96eaSJohn Baldwin new_switchtime = cpu_ticks(); 5447e3a96eaSJohn Baldwin runtime = new_switchtime - PCPU_GET(switchtime); 5457e3a96eaSJohn Baldwin td->td_runtime += runtime; 5467e3a96eaSJohn Baldwin td->td_incruntime += runtime; 5477e3a96eaSJohn Baldwin PCPU_SET(switchtime, new_switchtime); 5487e3a96eaSJohn Baldwin PCPU_SET(switchticks, ticks); 549*83c9dea1SGleb Smirnoff VM_CNT_INC(v_swtch); 5507e3a96eaSJohn Baldwin 5517e3a96eaSJohn Baldwin /* Save our resource usage in our process. */ 5527e3a96eaSJohn Baldwin td->td_ru.ru_nvcsw++; 55341fd9c63SKonstantin Belousov ruxagg(p, td); 5547e3a96eaSJohn Baldwin rucollect(&p->p_ru, &td->td_ru); 5555c7bebf9SKonstantin Belousov PROC_STATUNLOCK(p); 5567e3a96eaSJohn Baldwin 557dcc9954eSJulian Elischer td->td_state = TDS_INACTIVE; 5583d06b4b3SAttilio Rao #ifdef WITNESS 5593d06b4b3SAttilio Rao witness_thread_exit(td); 5603d06b4b3SAttilio Rao #endif 561732d9528SJulian Elischer CTR1(KTR_PROC, "thread_exit: cpu_throw() thread %p", td); 562a54e85fdSJeff Roberson sched_throw(td); 563cc66ebe2SPeter Wemm panic("I'm a teapot!"); 56444990b8cSJulian Elischer /* NOTREACHED */ 56544990b8cSJulian Elischer } 56644990b8cSJulian Elischer 56744990b8cSJulian Elischer /* 568696058c3SJulian Elischer * Do any thread specific cleanups that may be needed in wait() 56937814395SPeter Wemm * called with Giant, proc and schedlock not held. 570696058c3SJulian Elischer */ 571696058c3SJulian Elischer void 572696058c3SJulian Elischer thread_wait(struct proc *p) 573696058c3SJulian Elischer { 574696058c3SJulian Elischer struct thread *td; 575696058c3SJulian Elischer 57637814395SPeter Wemm mtx_assert(&Giant, MA_NOTOWNED); 577624bf9e1SKonstantin Belousov KASSERT(p->p_numthreads == 1, ("multiple threads in thread_wait()")); 578624bf9e1SKonstantin Belousov KASSERT(p->p_exitthreads == 0, ("p_exitthreads leaking")); 579ff8fbcffSJeff Roberson td = FIRST_THREAD_IN_PROC(p); 580ff8fbcffSJeff Roberson /* Lock the last thread so we spin until it exits cpu_throw(). */ 581ff8fbcffSJeff Roberson thread_lock(td); 582ff8fbcffSJeff Roberson thread_unlock(td); 5832e6b8de4SJeff Roberson lock_profile_thread_exit(td); 584d7f687fcSJeff Roberson cpuset_rel(td->td_cpuset); 585d7f687fcSJeff Roberson td->td_cpuset = NULL; 586696058c3SJulian Elischer cpu_thread_clean(td); 5874ea6a9a2SMateusz Guzik thread_cow_free(td); 5882d19b736SKonstantin Belousov callout_drain(&td->td_slpcallout); 589696058c3SJulian Elischer thread_reap(); /* check for zombie threads etc. */ 590696058c3SJulian Elischer } 591696058c3SJulian Elischer 592696058c3SJulian Elischer /* 59344990b8cSJulian Elischer * Link a thread to a process. 5941faf202eSJulian Elischer * set up anything that needs to be initialized for it to 5951faf202eSJulian Elischer * be used by the process. 59644990b8cSJulian Elischer */ 59744990b8cSJulian Elischer void 5988460a577SJohn Birrell thread_link(struct thread *td, struct proc *p) 59944990b8cSJulian Elischer { 60044990b8cSJulian Elischer 601a54e85fdSJeff Roberson /* 602a54e85fdSJeff Roberson * XXX This can't be enabled because it's called for proc0 before 603374ae2a3SJeff Roberson * its lock has been created. 604374ae2a3SJeff Roberson * PROC_LOCK_ASSERT(p, MA_OWNED); 605a54e85fdSJeff Roberson */ 60671fad9fdSJulian Elischer td->td_state = TDS_INACTIVE; 60744990b8cSJulian Elischer td->td_proc = p; 608b61ce5b0SJeff Roberson td->td_flags = TDF_INMEM; 60944990b8cSJulian Elischer 6101faf202eSJulian Elischer LIST_INIT(&td->td_contested); 611eea4f254SJeff Roberson LIST_INIT(&td->td_lprof[0]); 612eea4f254SJeff Roberson LIST_INIT(&td->td_lprof[1]); 6139104847fSDavid Xu sigqueue_init(&td->td_sigqueue, p); 614fd90e2edSJung-uk Kim callout_init(&td->td_slpcallout, 1); 61566d8df9dSDaniel Eischen TAILQ_INSERT_TAIL(&p->p_threads, td, td_plist); 61644990b8cSJulian Elischer p->p_numthreads++; 61744990b8cSJulian Elischer } 61844990b8cSJulian Elischer 619ed062c8dSJulian Elischer /* 620ed062c8dSJulian Elischer * Called from: 621ed062c8dSJulian Elischer * thread_exit() 622ed062c8dSJulian Elischer */ 623d3a0bd78SJulian Elischer void 624d3a0bd78SJulian Elischer thread_unlink(struct thread *td) 625d3a0bd78SJulian Elischer { 626d3a0bd78SJulian Elischer struct proc *p = td->td_proc; 627d3a0bd78SJulian Elischer 628374ae2a3SJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 629d3a0bd78SJulian Elischer TAILQ_REMOVE(&p->p_threads, td, td_plist); 630d3a0bd78SJulian Elischer p->p_numthreads--; 631d3a0bd78SJulian Elischer /* could clear a few other things here */ 6328460a577SJohn Birrell /* Must NOT clear links to proc! */ 6335c8329edSJulian Elischer } 6345c8329edSJulian Elischer 63579799053SKonstantin Belousov static int 63679799053SKonstantin Belousov calc_remaining(struct proc *p, int mode) 63779799053SKonstantin Belousov { 63879799053SKonstantin Belousov int remaining; 63979799053SKonstantin Belousov 6407b519077SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 6417b519077SKonstantin Belousov PROC_SLOCK_ASSERT(p, MA_OWNED); 64279799053SKonstantin Belousov if (mode == SINGLE_EXIT) 64379799053SKonstantin Belousov remaining = p->p_numthreads; 64479799053SKonstantin Belousov else if (mode == SINGLE_BOUNDARY) 64579799053SKonstantin Belousov remaining = p->p_numthreads - p->p_boundary_count; 6466ddcc233SKonstantin Belousov else if (mode == SINGLE_NO_EXIT || mode == SINGLE_ALLPROC) 64779799053SKonstantin Belousov remaining = p->p_numthreads - p->p_suspcount; 64879799053SKonstantin Belousov else 64979799053SKonstantin Belousov panic("calc_remaining: wrong mode %d", mode); 65079799053SKonstantin Belousov return (remaining); 65179799053SKonstantin Belousov } 65279799053SKonstantin Belousov 65307a9368aSKonstantin Belousov static int 65407a9368aSKonstantin Belousov remain_for_mode(int mode) 65507a9368aSKonstantin Belousov { 65607a9368aSKonstantin Belousov 6576ddcc233SKonstantin Belousov return (mode == SINGLE_ALLPROC ? 0 : 1); 65807a9368aSKonstantin Belousov } 65907a9368aSKonstantin Belousov 66007a9368aSKonstantin Belousov static int 66107a9368aSKonstantin Belousov weed_inhib(int mode, struct thread *td2, struct proc *p) 66207a9368aSKonstantin Belousov { 66307a9368aSKonstantin Belousov int wakeup_swapper; 66407a9368aSKonstantin Belousov 66507a9368aSKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 66607a9368aSKonstantin Belousov PROC_SLOCK_ASSERT(p, MA_OWNED); 66707a9368aSKonstantin Belousov THREAD_LOCK_ASSERT(td2, MA_OWNED); 66807a9368aSKonstantin Belousov 66907a9368aSKonstantin Belousov wakeup_swapper = 0; 67007a9368aSKonstantin Belousov switch (mode) { 67107a9368aSKonstantin Belousov case SINGLE_EXIT: 67207a9368aSKonstantin Belousov if (TD_IS_SUSPENDED(td2)) 67384cdea97SKonstantin Belousov wakeup_swapper |= thread_unsuspend_one(td2, p, true); 67407a9368aSKonstantin Belousov if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR) != 0) 67507a9368aSKonstantin Belousov wakeup_swapper |= sleepq_abort(td2, EINTR); 67607a9368aSKonstantin Belousov break; 67707a9368aSKonstantin Belousov case SINGLE_BOUNDARY: 67807a9368aSKonstantin Belousov case SINGLE_NO_EXIT: 67907a9368aSKonstantin Belousov if (TD_IS_SUSPENDED(td2) && (td2->td_flags & TDF_BOUNDARY) == 0) 68084cdea97SKonstantin Belousov wakeup_swapper |= thread_unsuspend_one(td2, p, false); 68107a9368aSKonstantin Belousov if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR) != 0) 68207a9368aSKonstantin Belousov wakeup_swapper |= sleepq_abort(td2, ERESTART); 683917dd390SKonstantin Belousov break; 6846ddcc233SKonstantin Belousov case SINGLE_ALLPROC: 6856ddcc233SKonstantin Belousov /* 6866ddcc233SKonstantin Belousov * ALLPROC suspend tries to avoid spurious EINTR for 6876ddcc233SKonstantin Belousov * threads sleeping interruptable, by suspending the 6886ddcc233SKonstantin Belousov * thread directly, similarly to sig_suspend_threads(). 6896ddcc233SKonstantin Belousov * Since such sleep is not performed at the user 6906ddcc233SKonstantin Belousov * boundary, TDF_BOUNDARY flag is not set, and TDF_ALLPROCSUSP 6916ddcc233SKonstantin Belousov * is used to avoid immediate un-suspend. 6926ddcc233SKonstantin Belousov */ 6936ddcc233SKonstantin Belousov if (TD_IS_SUSPENDED(td2) && (td2->td_flags & (TDF_BOUNDARY | 6946ddcc233SKonstantin Belousov TDF_ALLPROCSUSP)) == 0) 69584cdea97SKonstantin Belousov wakeup_swapper |= thread_unsuspend_one(td2, p, false); 6966ddcc233SKonstantin Belousov if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR) != 0) { 6976ddcc233SKonstantin Belousov if ((td2->td_flags & TDF_SBDRY) == 0) { 6986ddcc233SKonstantin Belousov thread_suspend_one(td2); 6996ddcc233SKonstantin Belousov td2->td_flags |= TDF_ALLPROCSUSP; 7006ddcc233SKonstantin Belousov } else { 7016ddcc233SKonstantin Belousov wakeup_swapper |= sleepq_abort(td2, ERESTART); 7026ddcc233SKonstantin Belousov } 7036ddcc233SKonstantin Belousov } 70407a9368aSKonstantin Belousov break; 70507a9368aSKonstantin Belousov } 70607a9368aSKonstantin Belousov return (wakeup_swapper); 70707a9368aSKonstantin Belousov } 70807a9368aSKonstantin Belousov 7095215b187SJeff Roberson /* 71044990b8cSJulian Elischer * Enforce single-threading. 71144990b8cSJulian Elischer * 71244990b8cSJulian Elischer * Returns 1 if the caller must abort (another thread is waiting to 71344990b8cSJulian Elischer * exit the process or similar). Process is locked! 71444990b8cSJulian Elischer * Returns 0 when you are successfully the only thread running. 71544990b8cSJulian Elischer * A process has successfully single threaded in the suspend mode when 71644990b8cSJulian Elischer * There are no threads in user mode. Threads in the kernel must be 71744990b8cSJulian Elischer * allowed to continue until they get to the user boundary. They may even 71844990b8cSJulian Elischer * copy out their return values and data before suspending. They may however be 719e2668f55SMaxim Konovalov * accelerated in reaching the user boundary as we will wake up 72044990b8cSJulian Elischer * any sleeping threads that are interruptable. (PCATCH). 72144990b8cSJulian Elischer */ 72244990b8cSJulian Elischer int 7236ddcc233SKonstantin Belousov thread_single(struct proc *p, int mode) 72444990b8cSJulian Elischer { 72544990b8cSJulian Elischer struct thread *td; 72644990b8cSJulian Elischer struct thread *td2; 727da7bbd2cSJohn Baldwin int remaining, wakeup_swapper; 72844990b8cSJulian Elischer 72944990b8cSJulian Elischer td = curthread; 7306ddcc233SKonstantin Belousov KASSERT(mode == SINGLE_EXIT || mode == SINGLE_BOUNDARY || 7316ddcc233SKonstantin Belousov mode == SINGLE_ALLPROC || mode == SINGLE_NO_EXIT, 7326ddcc233SKonstantin Belousov ("invalid mode %d", mode)); 7336ddcc233SKonstantin Belousov /* 7346ddcc233SKonstantin Belousov * If allowing non-ALLPROC singlethreading for non-curproc 7356ddcc233SKonstantin Belousov * callers, calc_remaining() and remain_for_mode() should be 7366ddcc233SKonstantin Belousov * adjusted to also account for td->td_proc != p. For now 7376ddcc233SKonstantin Belousov * this is not implemented because it is not used. 7386ddcc233SKonstantin Belousov */ 7396ddcc233SKonstantin Belousov KASSERT((mode == SINGLE_ALLPROC && td->td_proc != p) || 7406ddcc233SKonstantin Belousov (mode != SINGLE_ALLPROC && td->td_proc == p), 7416ddcc233SKonstantin Belousov ("mode %d proc %p curproc %p", mode, p, td->td_proc)); 74237814395SPeter Wemm mtx_assert(&Giant, MA_NOTOWNED); 74344990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 74444990b8cSJulian Elischer 7456ddcc233SKonstantin Belousov if ((p->p_flag & P_HADTHREADS) == 0 && mode != SINGLE_ALLPROC) 74644990b8cSJulian Elischer return (0); 74744990b8cSJulian Elischer 748e3b9bf71SJulian Elischer /* Is someone already single threading? */ 749906ac69dSDavid Xu if (p->p_singlethread != NULL && p->p_singlethread != td) 75044990b8cSJulian Elischer return (1); 75144990b8cSJulian Elischer 752906ac69dSDavid Xu if (mode == SINGLE_EXIT) { 753906ac69dSDavid Xu p->p_flag |= P_SINGLE_EXIT; 754906ac69dSDavid Xu p->p_flag &= ~P_SINGLE_BOUNDARY; 755906ac69dSDavid Xu } else { 756906ac69dSDavid Xu p->p_flag &= ~P_SINGLE_EXIT; 757906ac69dSDavid Xu if (mode == SINGLE_BOUNDARY) 758906ac69dSDavid Xu p->p_flag |= P_SINGLE_BOUNDARY; 759906ac69dSDavid Xu else 760906ac69dSDavid Xu p->p_flag &= ~P_SINGLE_BOUNDARY; 761906ac69dSDavid Xu } 7626ddcc233SKonstantin Belousov if (mode == SINGLE_ALLPROC) 7636ddcc233SKonstantin Belousov p->p_flag |= P_TOTAL_STOP; 7641279572aSDavid Xu p->p_flag |= P_STOPPED_SINGLE; 7657b4a950aSDavid Xu PROC_SLOCK(p); 766112afcb2SJohn Baldwin p->p_singlethread = td; 76779799053SKonstantin Belousov remaining = calc_remaining(p, mode); 76807a9368aSKonstantin Belousov while (remaining != remain_for_mode(mode)) { 769bf1a3220SDavid Xu if (P_SHOULDSTOP(p) != P_STOPPED_SINGLE) 770bf1a3220SDavid Xu goto stopme; 771da7bbd2cSJohn Baldwin wakeup_swapper = 0; 77244990b8cSJulian Elischer FOREACH_THREAD_IN_PROC(p, td2) { 77344990b8cSJulian Elischer if (td2 == td) 77444990b8cSJulian Elischer continue; 775a54e85fdSJeff Roberson thread_lock(td2); 776b7edba77SJeff Roberson td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK; 7776ddcc233SKonstantin Belousov if (TD_IS_INHIBITED(td2)) { 77807a9368aSKonstantin Belousov wakeup_swapper |= weed_inhib(mode, td2, p); 779d8267df7SDavid Xu #ifdef SMP 7806ddcc233SKonstantin Belousov } else if (TD_IS_RUNNING(td2) && td != td2) { 781d8267df7SDavid Xu forward_signal(td2); 782d8267df7SDavid Xu #endif 7836ddcc233SKonstantin Belousov } 784a54e85fdSJeff Roberson thread_unlock(td2); 7859d102777SJulian Elischer } 786da7bbd2cSJohn Baldwin if (wakeup_swapper) 787da7bbd2cSJohn Baldwin kick_proc0(); 78879799053SKonstantin Belousov remaining = calc_remaining(p, mode); 789ec008e96SDavid Xu 7909d102777SJulian Elischer /* 7919d102777SJulian Elischer * Maybe we suspended some threads.. was it enough? 7929d102777SJulian Elischer */ 79307a9368aSKonstantin Belousov if (remaining == remain_for_mode(mode)) 7949d102777SJulian Elischer break; 7959d102777SJulian Elischer 796bf1a3220SDavid Xu stopme: 79744990b8cSJulian Elischer /* 79844990b8cSJulian Elischer * Wake us up when everyone else has suspended. 799e3b9bf71SJulian Elischer * In the mean time we suspend as well. 80044990b8cSJulian Elischer */ 8016ddcc233SKonstantin Belousov thread_suspend_switch(td, p); 80279799053SKonstantin Belousov remaining = calc_remaining(p, mode); 80344990b8cSJulian Elischer } 804906ac69dSDavid Xu if (mode == SINGLE_EXIT) { 80591599697SJulian Elischer /* 8068626a0ddSKonstantin Belousov * Convert the process to an unthreaded process. The 8078626a0ddSKonstantin Belousov * SINGLE_EXIT is called by exit1() or execve(), in 8088626a0ddSKonstantin Belousov * both cases other threads must be retired. 80991599697SJulian Elischer */ 8108626a0ddSKonstantin Belousov KASSERT(p->p_numthreads == 1, ("Unthreading with >1 threads")); 811ed062c8dSJulian Elischer p->p_singlethread = NULL; 8128626a0ddSKonstantin Belousov p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_HADTHREADS); 813fd229b5bSKonstantin Belousov 814fd229b5bSKonstantin Belousov /* 815fd229b5bSKonstantin Belousov * Wait for any remaining threads to exit cpu_throw(). 816fd229b5bSKonstantin Belousov */ 817fd229b5bSKonstantin Belousov while (p->p_exitthreads != 0) { 818fd229b5bSKonstantin Belousov PROC_SUNLOCK(p); 819fd229b5bSKonstantin Belousov PROC_UNLOCK(p); 820fd229b5bSKonstantin Belousov sched_relinquish(td); 821fd229b5bSKonstantin Belousov PROC_LOCK(p); 822fd229b5bSKonstantin Belousov PROC_SLOCK(p); 823fd229b5bSKonstantin Belousov } 824ac437c07SKonstantin Belousov } else if (mode == SINGLE_BOUNDARY) { 825ac437c07SKonstantin Belousov /* 826ac437c07SKonstantin Belousov * Wait until all suspended threads are removed from 827ac437c07SKonstantin Belousov * the processors. The thread_suspend_check() 828ac437c07SKonstantin Belousov * increments p_boundary_count while it is still 829ac437c07SKonstantin Belousov * running, which makes it possible for the execve() 830ac437c07SKonstantin Belousov * to destroy vmspace while our other threads are 831ac437c07SKonstantin Belousov * still using the address space. 832ac437c07SKonstantin Belousov * 833ac437c07SKonstantin Belousov * We lock the thread, which is only allowed to 834ac437c07SKonstantin Belousov * succeed after context switch code finished using 835ac437c07SKonstantin Belousov * the address space. 836ac437c07SKonstantin Belousov */ 837ac437c07SKonstantin Belousov FOREACH_THREAD_IN_PROC(p, td2) { 838ac437c07SKonstantin Belousov if (td2 == td) 839ac437c07SKonstantin Belousov continue; 840ac437c07SKonstantin Belousov thread_lock(td2); 841ac437c07SKonstantin Belousov KASSERT((td2->td_flags & TDF_BOUNDARY) != 0, 842ac437c07SKonstantin Belousov ("td %p not on boundary", td2)); 843ac437c07SKonstantin Belousov KASSERT(TD_IS_SUSPENDED(td2), 844ac437c07SKonstantin Belousov ("td %p is not suspended", td2)); 845ac437c07SKonstantin Belousov thread_unlock(td2); 846ac437c07SKonstantin Belousov } 84791599697SJulian Elischer } 8487b4a950aSDavid Xu PROC_SUNLOCK(p); 84944990b8cSJulian Elischer return (0); 85044990b8cSJulian Elischer } 85144990b8cSJulian Elischer 8528638fe7bSKonstantin Belousov bool 8538638fe7bSKonstantin Belousov thread_suspend_check_needed(void) 8548638fe7bSKonstantin Belousov { 8558638fe7bSKonstantin Belousov struct proc *p; 8568638fe7bSKonstantin Belousov struct thread *td; 8578638fe7bSKonstantin Belousov 8588638fe7bSKonstantin Belousov td = curthread; 8598638fe7bSKonstantin Belousov p = td->td_proc; 8608638fe7bSKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 8618638fe7bSKonstantin Belousov return (P_SHOULDSTOP(p) || ((p->p_flag & P_TRACED) != 0 && 8628638fe7bSKonstantin Belousov (td->td_dbgflags & TDB_SUSPEND) != 0)); 8638638fe7bSKonstantin Belousov } 8648638fe7bSKonstantin Belousov 86544990b8cSJulian Elischer /* 86644990b8cSJulian Elischer * Called in from locations that can safely check to see 86744990b8cSJulian Elischer * whether we have to suspend or at least throttle for a 86844990b8cSJulian Elischer * single-thread event (e.g. fork). 86944990b8cSJulian Elischer * 87044990b8cSJulian Elischer * Such locations include userret(). 87144990b8cSJulian Elischer * If the "return_instead" argument is non zero, the thread must be able to 87244990b8cSJulian Elischer * accept 0 (caller may continue), or 1 (caller must abort) as a result. 87344990b8cSJulian Elischer * 87444990b8cSJulian Elischer * The 'return_instead' argument tells the function if it may do a 87544990b8cSJulian Elischer * thread_exit() or suspend, or whether the caller must abort and back 87644990b8cSJulian Elischer * out instead. 87744990b8cSJulian Elischer * 87844990b8cSJulian Elischer * If the thread that set the single_threading request has set the 87944990b8cSJulian Elischer * P_SINGLE_EXIT bit in the process flags then this call will never return 88044990b8cSJulian Elischer * if 'return_instead' is false, but will exit. 88144990b8cSJulian Elischer * 88244990b8cSJulian Elischer * P_SINGLE_EXIT | return_instead == 0| return_instead != 0 88344990b8cSJulian Elischer *---------------+--------------------+--------------------- 88444990b8cSJulian Elischer * 0 | returns 0 | returns 0 or 1 885353374b5SJohn Baldwin * | when ST ends | immediately 88644990b8cSJulian Elischer *---------------+--------------------+--------------------- 88744990b8cSJulian Elischer * 1 | thread exits | returns 1 888353374b5SJohn Baldwin * | | immediately 88944990b8cSJulian Elischer * 0 = thread_exit() or suspension ok, 89044990b8cSJulian Elischer * other = return error instead of stopping the thread. 89144990b8cSJulian Elischer * 89244990b8cSJulian Elischer * While a full suspension is under effect, even a single threading 89344990b8cSJulian Elischer * thread would be suspended if it made this call (but it shouldn't). 89444990b8cSJulian Elischer * This call should only be made from places where 89544990b8cSJulian Elischer * thread_exit() would be safe as that may be the outcome unless 89644990b8cSJulian Elischer * return_instead is set. 89744990b8cSJulian Elischer */ 89844990b8cSJulian Elischer int 89944990b8cSJulian Elischer thread_suspend_check(int return_instead) 90044990b8cSJulian Elischer { 901ecafb24bSJuli Mallett struct thread *td; 902ecafb24bSJuli Mallett struct proc *p; 90346e47c4fSKonstantin Belousov int wakeup_swapper; 90444990b8cSJulian Elischer 90544990b8cSJulian Elischer td = curthread; 90644990b8cSJulian Elischer p = td->td_proc; 90737814395SPeter Wemm mtx_assert(&Giant, MA_NOTOWNED); 90844990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 9098638fe7bSKonstantin Belousov while (thread_suspend_check_needed()) { 9101279572aSDavid Xu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 91144990b8cSJulian Elischer KASSERT(p->p_singlethread != NULL, 91244990b8cSJulian Elischer ("singlethread not set")); 91344990b8cSJulian Elischer /* 914e3b9bf71SJulian Elischer * The only suspension in action is a 915e3b9bf71SJulian Elischer * single-threading. Single threader need not stop. 916bd07998eSKonstantin Belousov * It is safe to access p->p_singlethread unlocked 917bd07998eSKonstantin Belousov * because it can only be set to our address by us. 91844990b8cSJulian Elischer */ 919e3b9bf71SJulian Elischer if (p->p_singlethread == td) 92044990b8cSJulian Elischer return (0); /* Exempt from stopping. */ 92144990b8cSJulian Elischer } 92245a4bfa1SDavid Xu if ((p->p_flag & P_SINGLE_EXIT) && return_instead) 92394f0972bSDavid Xu return (EINTR); 92444990b8cSJulian Elischer 925906ac69dSDavid Xu /* Should we goto user boundary if we didn't come from there? */ 926906ac69dSDavid Xu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE && 927906ac69dSDavid Xu (p->p_flag & P_SINGLE_BOUNDARY) && return_instead) 92894f0972bSDavid Xu return (ERESTART); 929906ac69dSDavid Xu 93044990b8cSJulian Elischer /* 9313077f938SKonstantin Belousov * Ignore suspend requests if they are deferred. 932d071a6faSJohn Baldwin */ 9333077f938SKonstantin Belousov if ((td->td_flags & TDF_SBDRY) != 0) { 934d071a6faSJohn Baldwin KASSERT(return_instead, 935d071a6faSJohn Baldwin ("TDF_SBDRY set for unsafe thread_suspend_check")); 93646e47c4fSKonstantin Belousov KASSERT((td->td_flags & (TDF_SEINTR | TDF_SERESTART)) != 93746e47c4fSKonstantin Belousov (TDF_SEINTR | TDF_SERESTART), 93846e47c4fSKonstantin Belousov ("both TDF_SEINTR and TDF_SERESTART")); 93946e47c4fSKonstantin Belousov return (TD_SBDRY_INTR(td) ? TD_SBDRY_ERRNO(td) : 0); 940d071a6faSJohn Baldwin } 941d071a6faSJohn Baldwin 942d071a6faSJohn Baldwin /* 94344990b8cSJulian Elischer * If the process is waiting for us to exit, 94444990b8cSJulian Elischer * this thread should just suicide. 9451279572aSDavid Xu * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE. 94644990b8cSJulian Elischer */ 947cf7d9a8cSDavid Xu if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) { 948cf7d9a8cSDavid Xu PROC_UNLOCK(p); 94991d1786fSDmitry Chagin 95091d1786fSDmitry Chagin /* 95191d1786fSDmitry Chagin * Allow Linux emulation layer to do some work 95291d1786fSDmitry Chagin * before thread suicide. 95391d1786fSDmitry Chagin */ 95491d1786fSDmitry Chagin if (__predict_false(p->p_sysent->sv_thread_detach != NULL)) 95591d1786fSDmitry Chagin (p->p_sysent->sv_thread_detach)(td); 9562a339d9eSKonstantin Belousov umtx_thread_exit(td); 957d1e7a4a5SJohn Baldwin kern_thr_exit(td); 958d1e7a4a5SJohn Baldwin panic("stopped thread did not exit"); 959cf7d9a8cSDavid Xu } 96021ecd1e9SDavid Xu 96121ecd1e9SDavid Xu PROC_SLOCK(p); 96221ecd1e9SDavid Xu thread_stopped(p); 963a54e85fdSJeff Roberson if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 964a54e85fdSJeff Roberson if (p->p_numthreads == p->p_suspcount + 1) { 965a54e85fdSJeff Roberson thread_lock(p->p_singlethread); 96684cdea97SKonstantin Belousov wakeup_swapper = thread_unsuspend_one( 96784cdea97SKonstantin Belousov p->p_singlethread, p, false); 968a54e85fdSJeff Roberson thread_unlock(p->p_singlethread); 9697847a9daSJohn Baldwin if (wakeup_swapper) 9707847a9daSJohn Baldwin kick_proc0(); 971a54e85fdSJeff Roberson } 972a54e85fdSJeff Roberson } 9733f9be10eSDavid Xu PROC_UNLOCK(p); 9747b4a950aSDavid Xu thread_lock(td); 97544990b8cSJulian Elischer /* 97644990b8cSJulian Elischer * When a thread suspends, it just 977ad1e7d28SJulian Elischer * gets taken off all queues. 97844990b8cSJulian Elischer */ 97971fad9fdSJulian Elischer thread_suspend_one(td); 980906ac69dSDavid Xu if (return_instead == 0) { 981906ac69dSDavid Xu p->p_boundary_count++; 982906ac69dSDavid Xu td->td_flags |= TDF_BOUNDARY; 983cf19bf91SJulian Elischer } 9847b4a950aSDavid Xu PROC_SUNLOCK(p); 9858df78c41SJeff Roberson mi_switch(SW_INVOL | SWT_SUSPEND, NULL); 986a54e85fdSJeff Roberson thread_unlock(td); 98744990b8cSJulian Elischer PROC_LOCK(p); 98844990b8cSJulian Elischer } 98944990b8cSJulian Elischer return (0); 99044990b8cSJulian Elischer } 99144990b8cSJulian Elischer 99235c32a76SDavid Xu void 9936ddcc233SKonstantin Belousov thread_suspend_switch(struct thread *td, struct proc *p) 994a54e85fdSJeff Roberson { 995a54e85fdSJeff Roberson 996a54e85fdSJeff Roberson KASSERT(!TD_IS_SUSPENDED(td), ("already suspended")); 997a54e85fdSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 9987b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 999a54e85fdSJeff Roberson /* 1000a54e85fdSJeff Roberson * We implement thread_suspend_one in stages here to avoid 1001a54e85fdSJeff Roberson * dropping the proc lock while the thread lock is owned. 1002a54e85fdSJeff Roberson */ 10036ddcc233SKonstantin Belousov if (p == td->td_proc) { 1004a54e85fdSJeff Roberson thread_stopped(p); 1005a54e85fdSJeff Roberson p->p_suspcount++; 10066ddcc233SKonstantin Belousov } 10073f9be10eSDavid Xu PROC_UNLOCK(p); 10087b4a950aSDavid Xu thread_lock(td); 1009b7edba77SJeff Roberson td->td_flags &= ~TDF_NEEDSUSPCHK; 1010a54e85fdSJeff Roberson TD_SET_SUSPENDED(td); 1011c5aa6b58SJeff Roberson sched_sleep(td, 0); 10127b4a950aSDavid Xu PROC_SUNLOCK(p); 1013a54e85fdSJeff Roberson DROP_GIANT(); 10148df78c41SJeff Roberson mi_switch(SW_VOL | SWT_SUSPEND, NULL); 1015a54e85fdSJeff Roberson thread_unlock(td); 1016a54e85fdSJeff Roberson PICKUP_GIANT(); 1017a54e85fdSJeff Roberson PROC_LOCK(p); 10187b4a950aSDavid Xu PROC_SLOCK(p); 1019a54e85fdSJeff Roberson } 1020a54e85fdSJeff Roberson 1021a54e85fdSJeff Roberson void 102235c32a76SDavid Xu thread_suspend_one(struct thread *td) 102335c32a76SDavid Xu { 10246ddcc233SKonstantin Belousov struct proc *p; 102535c32a76SDavid Xu 10266ddcc233SKonstantin Belousov p = td->td_proc; 10277b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 1028a54e85fdSJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 1029e574e444SDavid Xu KASSERT(!TD_IS_SUSPENDED(td), ("already suspended")); 103035c32a76SDavid Xu p->p_suspcount++; 1031b7edba77SJeff Roberson td->td_flags &= ~TDF_NEEDSUSPCHK; 103271fad9fdSJulian Elischer TD_SET_SUSPENDED(td); 1033c5aa6b58SJeff Roberson sched_sleep(td, 0); 103435c32a76SDavid Xu } 103535c32a76SDavid Xu 103684cdea97SKonstantin Belousov static int 103784cdea97SKonstantin Belousov thread_unsuspend_one(struct thread *td, struct proc *p, bool boundary) 103835c32a76SDavid Xu { 103935c32a76SDavid Xu 1040a54e85fdSJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 1041ad1e7d28SJulian Elischer KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended")); 104271fad9fdSJulian Elischer TD_CLR_SUSPENDED(td); 10436ddcc233SKonstantin Belousov td->td_flags &= ~TDF_ALLPROCSUSP; 10446ddcc233SKonstantin Belousov if (td->td_proc == p) { 10456ddcc233SKonstantin Belousov PROC_SLOCK_ASSERT(p, MA_OWNED); 104635c32a76SDavid Xu p->p_suspcount--; 104784cdea97SKonstantin Belousov if (boundary && (td->td_flags & TDF_BOUNDARY) != 0) { 104884cdea97SKonstantin Belousov td->td_flags &= ~TDF_BOUNDARY; 104984cdea97SKonstantin Belousov p->p_boundary_count--; 105084cdea97SKonstantin Belousov } 10516ddcc233SKonstantin Belousov } 10527847a9daSJohn Baldwin return (setrunnable(td)); 105335c32a76SDavid Xu } 105435c32a76SDavid Xu 105544990b8cSJulian Elischer /* 105644990b8cSJulian Elischer * Allow all threads blocked by single threading to continue running. 105744990b8cSJulian Elischer */ 105844990b8cSJulian Elischer void 105944990b8cSJulian Elischer thread_unsuspend(struct proc *p) 106044990b8cSJulian Elischer { 106144990b8cSJulian Elischer struct thread *td; 10627847a9daSJohn Baldwin int wakeup_swapper; 106344990b8cSJulian Elischer 106444990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 10657b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 10667847a9daSJohn Baldwin wakeup_swapper = 0; 106744990b8cSJulian Elischer if (!P_SHOULDSTOP(p)) { 1068ad1e7d28SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1069a54e85fdSJeff Roberson thread_lock(td); 1070ad1e7d28SJulian Elischer if (TD_IS_SUSPENDED(td)) { 107184cdea97SKonstantin Belousov wakeup_swapper |= thread_unsuspend_one(td, p, 107284cdea97SKonstantin Belousov true); 107344990b8cSJulian Elischer } 1074a54e85fdSJeff Roberson thread_unlock(td); 1075ad1e7d28SJulian Elischer } 107684cdea97SKonstantin Belousov } else if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE && 107784cdea97SKonstantin Belousov p->p_numthreads == p->p_suspcount) { 107844990b8cSJulian Elischer /* 107944990b8cSJulian Elischer * Stopping everything also did the job for the single 108044990b8cSJulian Elischer * threading request. Now we've downgraded to single-threaded, 108144990b8cSJulian Elischer * let it continue. 108244990b8cSJulian Elischer */ 10836ddcc233SKonstantin Belousov if (p->p_singlethread->td_proc == p) { 1084a54e85fdSJeff Roberson thread_lock(p->p_singlethread); 10856ddcc233SKonstantin Belousov wakeup_swapper = thread_unsuspend_one( 108684cdea97SKonstantin Belousov p->p_singlethread, p, false); 1087a54e85fdSJeff Roberson thread_unlock(p->p_singlethread); 108844990b8cSJulian Elischer } 10896ddcc233SKonstantin Belousov } 10907847a9daSJohn Baldwin if (wakeup_swapper) 10917847a9daSJohn Baldwin kick_proc0(); 109244990b8cSJulian Elischer } 109344990b8cSJulian Elischer 1094ed062c8dSJulian Elischer /* 1095ed062c8dSJulian Elischer * End the single threading mode.. 1096ed062c8dSJulian Elischer */ 109744990b8cSJulian Elischer void 10986ddcc233SKonstantin Belousov thread_single_end(struct proc *p, int mode) 109944990b8cSJulian Elischer { 110044990b8cSJulian Elischer struct thread *td; 11017847a9daSJohn Baldwin int wakeup_swapper; 110244990b8cSJulian Elischer 11036ddcc233SKonstantin Belousov KASSERT(mode == SINGLE_EXIT || mode == SINGLE_BOUNDARY || 11046ddcc233SKonstantin Belousov mode == SINGLE_ALLPROC || mode == SINGLE_NO_EXIT, 11056ddcc233SKonstantin Belousov ("invalid mode %d", mode)); 110644990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 11076ddcc233SKonstantin Belousov KASSERT((mode == SINGLE_ALLPROC && (p->p_flag & P_TOTAL_STOP) != 0) || 11086ddcc233SKonstantin Belousov (mode != SINGLE_ALLPROC && (p->p_flag & P_TOTAL_STOP) == 0), 11096ddcc233SKonstantin Belousov ("mode %d does not match P_TOTAL_STOP", mode)); 111084cdea97SKonstantin Belousov KASSERT(mode == SINGLE_ALLPROC || p->p_singlethread == curthread, 111184cdea97SKonstantin Belousov ("thread_single_end from other thread %p %p", 111284cdea97SKonstantin Belousov curthread, p->p_singlethread)); 111384cdea97SKonstantin Belousov KASSERT(mode != SINGLE_BOUNDARY || 111484cdea97SKonstantin Belousov (p->p_flag & P_SINGLE_BOUNDARY) != 0, 111584cdea97SKonstantin Belousov ("mis-matched SINGLE_BOUNDARY flags %x", p->p_flag)); 11166ddcc233SKonstantin Belousov p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_SINGLE_BOUNDARY | 11176ddcc233SKonstantin Belousov P_TOTAL_STOP); 11187b4a950aSDavid Xu PROC_SLOCK(p); 111944990b8cSJulian Elischer p->p_singlethread = NULL; 11207847a9daSJohn Baldwin wakeup_swapper = 0; 112149539972SJulian Elischer /* 11227847a9daSJohn Baldwin * If there are other threads they may now run, 112349539972SJulian Elischer * unless of course there is a blanket 'stop order' 112449539972SJulian Elischer * on the process. The single threader must be allowed 112549539972SJulian Elischer * to continue however as this is a bad place to stop. 112649539972SJulian Elischer */ 11276ddcc233SKonstantin Belousov if (p->p_numthreads != remain_for_mode(mode) && !P_SHOULDSTOP(p)) { 1128ad1e7d28SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1129a54e85fdSJeff Roberson thread_lock(td); 1130ad1e7d28SJulian Elischer if (TD_IS_SUSPENDED(td)) { 113184cdea97SKonstantin Belousov wakeup_swapper |= thread_unsuspend_one(td, p, 113284cdea97SKonstantin Belousov mode == SINGLE_BOUNDARY); 113344990b8cSJulian Elischer } 1134a54e85fdSJeff Roberson thread_unlock(td); 113549539972SJulian Elischer } 1136ad1e7d28SJulian Elischer } 113784cdea97SKonstantin Belousov KASSERT(mode != SINGLE_BOUNDARY || p->p_boundary_count == 0, 113884cdea97SKonstantin Belousov ("inconsistent boundary count %d", p->p_boundary_count)); 11397b4a950aSDavid Xu PROC_SUNLOCK(p); 11407847a9daSJohn Baldwin if (wakeup_swapper) 11417847a9daSJohn Baldwin kick_proc0(); 114249539972SJulian Elischer } 11434fc21c09SDaniel Eischen 114444355392SDavid Xu struct thread * 114544355392SDavid Xu thread_find(struct proc *p, lwpid_t tid) 114644355392SDavid Xu { 114744355392SDavid Xu struct thread *td; 114844355392SDavid Xu 114944355392SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 115044355392SDavid Xu FOREACH_THREAD_IN_PROC(p, td) { 115144355392SDavid Xu if (td->td_tid == tid) 115244355392SDavid Xu break; 115344355392SDavid Xu } 115444355392SDavid Xu return (td); 115544355392SDavid Xu } 1156cf7d9a8cSDavid Xu 1157cf7d9a8cSDavid Xu /* Locate a thread by number; return with proc lock held. */ 1158cf7d9a8cSDavid Xu struct thread * 1159cf7d9a8cSDavid Xu tdfind(lwpid_t tid, pid_t pid) 1160cf7d9a8cSDavid Xu { 1161cf7d9a8cSDavid Xu #define RUN_THRESH 16 1162cf7d9a8cSDavid Xu struct thread *td; 1163cf7d9a8cSDavid Xu int run = 0; 1164cf7d9a8cSDavid Xu 1165cf7d9a8cSDavid Xu rw_rlock(&tidhash_lock); 1166cf7d9a8cSDavid Xu LIST_FOREACH(td, TIDHASH(tid), td_hash) { 1167cf7d9a8cSDavid Xu if (td->td_tid == tid) { 1168cf7d9a8cSDavid Xu if (pid != -1 && td->td_proc->p_pid != pid) { 1169cf7d9a8cSDavid Xu td = NULL; 1170cf7d9a8cSDavid Xu break; 1171cf7d9a8cSDavid Xu } 11728e6fa660SJohn Baldwin PROC_LOCK(td->td_proc); 1173cf7d9a8cSDavid Xu if (td->td_proc->p_state == PRS_NEW) { 11748e6fa660SJohn Baldwin PROC_UNLOCK(td->td_proc); 1175cf7d9a8cSDavid Xu td = NULL; 1176cf7d9a8cSDavid Xu break; 1177cf7d9a8cSDavid Xu } 1178cf7d9a8cSDavid Xu if (run > RUN_THRESH) { 1179cf7d9a8cSDavid Xu if (rw_try_upgrade(&tidhash_lock)) { 1180cf7d9a8cSDavid Xu LIST_REMOVE(td, td_hash); 1181cf7d9a8cSDavid Xu LIST_INSERT_HEAD(TIDHASH(td->td_tid), 1182cf7d9a8cSDavid Xu td, td_hash); 1183cf7d9a8cSDavid Xu rw_wunlock(&tidhash_lock); 1184cf7d9a8cSDavid Xu return (td); 1185cf7d9a8cSDavid Xu } 1186cf7d9a8cSDavid Xu } 1187cf7d9a8cSDavid Xu break; 1188cf7d9a8cSDavid Xu } 1189cf7d9a8cSDavid Xu run++; 1190cf7d9a8cSDavid Xu } 1191cf7d9a8cSDavid Xu rw_runlock(&tidhash_lock); 1192cf7d9a8cSDavid Xu return (td); 1193cf7d9a8cSDavid Xu } 1194cf7d9a8cSDavid Xu 1195cf7d9a8cSDavid Xu void 1196cf7d9a8cSDavid Xu tidhash_add(struct thread *td) 1197cf7d9a8cSDavid Xu { 1198cf7d9a8cSDavid Xu rw_wlock(&tidhash_lock); 1199cf7d9a8cSDavid Xu LIST_INSERT_HEAD(TIDHASH(td->td_tid), td, td_hash); 1200cf7d9a8cSDavid Xu rw_wunlock(&tidhash_lock); 1201cf7d9a8cSDavid Xu } 1202cf7d9a8cSDavid Xu 1203cf7d9a8cSDavid Xu void 1204cf7d9a8cSDavid Xu tidhash_remove(struct thread *td) 1205cf7d9a8cSDavid Xu { 1206cf7d9a8cSDavid Xu rw_wlock(&tidhash_lock); 1207cf7d9a8cSDavid Xu LIST_REMOVE(td, td_hash); 1208cf7d9a8cSDavid Xu rw_wunlock(&tidhash_lock); 1209cf7d9a8cSDavid Xu } 1210