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> 54d7f687fcSJeff Roberson #include <sys/cpuset.h> 5516d95d4fSJoseph Koshy #ifdef HWPMC_HOOKS 5616d95d4fSJoseph Koshy #include <sys/pmckern.h> 5716d95d4fSJoseph Koshy #endif 5844990b8cSJulian Elischer 59911b84b0SRobert Watson #include <security/audit/audit.h> 60911b84b0SRobert Watson 6144990b8cSJulian Elischer #include <vm/vm.h> 6249a2507bSAlan Cox #include <vm/vm_extern.h> 6344990b8cSJulian Elischer #include <vm/uma.h> 646520495aSAdrian Chadd #include <vm/vm_domain.h> 65b209f889SRandall Stewart #include <sys/eventhandler.h> 6602fb42b0SPeter Wemm 67b3e9e682SRyan Stone SDT_PROVIDER_DECLARE(proc); 68d9fae5abSAndriy Gapon SDT_PROBE_DEFINE(proc, , , lwp__exit); 69b3e9e682SRyan Stone 708460a577SJohn Birrell /* 718460a577SJohn Birrell * thread related storage. 728460a577SJohn Birrell */ 7344990b8cSJulian Elischer static uma_zone_t thread_zone; 7444990b8cSJulian Elischer 755215b187SJeff Roberson TAILQ_HEAD(, thread) zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads); 76c8790f5dSAttilio Rao static struct mtx zombie_lock; 77a54e85fdSJeff Roberson MTX_SYSINIT(zombie_lock, &zombie_lock, "zombie lock", MTX_SPIN); 7844990b8cSJulian Elischer 79ff8fbcffSJeff Roberson static void thread_zombie(struct thread *); 8084cdea97SKonstantin Belousov static int thread_unsuspend_one(struct thread *td, struct proc *p, 8184cdea97SKonstantin Belousov bool boundary); 82ff8fbcffSJeff Roberson 83ec6ea5e8SDavid Xu #define TID_BUFFER_SIZE 1024 84ec6ea5e8SDavid Xu 85fdcac928SMarcel Moolenaar struct mtx tid_lock; 861ea7a6f8SPoul-Henning Kamp static struct unrhdr *tid_unrhdr; 87ec6ea5e8SDavid Xu static lwpid_t tid_buffer[TID_BUFFER_SIZE]; 88ec6ea5e8SDavid Xu static int tid_head, tid_tail; 89cf7d9a8cSDavid Xu static MALLOC_DEFINE(M_TIDHASH, "tidhash", "thread hash"); 90cf7d9a8cSDavid Xu 91cf7d9a8cSDavid Xu struct tidhashhead *tidhashtbl; 92cf7d9a8cSDavid Xu u_long tidhash; 93cf7d9a8cSDavid Xu struct rwlock tidhash_lock; 94cf7d9a8cSDavid Xu 95ec6ea5e8SDavid Xu static lwpid_t 96ec6ea5e8SDavid Xu tid_alloc(void) 97ec6ea5e8SDavid Xu { 98ec6ea5e8SDavid Xu lwpid_t tid; 99ec6ea5e8SDavid Xu 100ec6ea5e8SDavid Xu tid = alloc_unr(tid_unrhdr); 101ec6ea5e8SDavid Xu if (tid != -1) 102ec6ea5e8SDavid Xu return (tid); 103ec6ea5e8SDavid Xu mtx_lock(&tid_lock); 104ec6ea5e8SDavid Xu if (tid_head == tid_tail) { 105ec6ea5e8SDavid Xu mtx_unlock(&tid_lock); 106ec6ea5e8SDavid Xu return (-1); 107ec6ea5e8SDavid Xu } 10894cb3545SKonstantin Belousov tid = tid_buffer[tid_head]; 10994cb3545SKonstantin Belousov tid_head = (tid_head + 1) % TID_BUFFER_SIZE; 110ec6ea5e8SDavid Xu mtx_unlock(&tid_lock); 111ec6ea5e8SDavid Xu return (tid); 112ec6ea5e8SDavid Xu } 113ec6ea5e8SDavid Xu 114ec6ea5e8SDavid Xu static void 115ec6ea5e8SDavid Xu tid_free(lwpid_t tid) 116ec6ea5e8SDavid Xu { 117ec6ea5e8SDavid Xu lwpid_t tmp_tid = -1; 118ec6ea5e8SDavid Xu 119ec6ea5e8SDavid Xu mtx_lock(&tid_lock); 120ec6ea5e8SDavid Xu if ((tid_tail + 1) % TID_BUFFER_SIZE == tid_head) { 12194cb3545SKonstantin Belousov tmp_tid = tid_buffer[tid_head]; 12294cb3545SKonstantin Belousov tid_head = (tid_head + 1) % TID_BUFFER_SIZE; 123ec6ea5e8SDavid Xu } 12494cb3545SKonstantin Belousov tid_buffer[tid_tail] = tid; 12594cb3545SKonstantin Belousov tid_tail = (tid_tail + 1) % TID_BUFFER_SIZE; 126ec6ea5e8SDavid Xu mtx_unlock(&tid_lock); 127ec6ea5e8SDavid Xu if (tmp_tid != -1) 128ec6ea5e8SDavid Xu free_unr(tid_unrhdr, tmp_tid); 129ec6ea5e8SDavid Xu } 130ec6ea5e8SDavid Xu 131fdcac928SMarcel Moolenaar /* 132696058c3SJulian Elischer * Prepare a thread for use. 13344990b8cSJulian Elischer */ 134b23f72e9SBrian Feldman static int 135b23f72e9SBrian Feldman thread_ctor(void *mem, int size, void *arg, int flags) 13644990b8cSJulian Elischer { 13744990b8cSJulian Elischer struct thread *td; 13844990b8cSJulian Elischer 13944990b8cSJulian Elischer td = (struct thread *)mem; 14071fad9fdSJulian Elischer td->td_state = TDS_INACTIVE; 141060563ecSJulian Elischer td->td_oncpu = NOCPU; 1426c27c603SJuli Mallett 143ec6ea5e8SDavid Xu td->td_tid = tid_alloc(); 144773eff9dSPoul-Henning Kamp 1456c27c603SJuli Mallett /* 1466c27c603SJuli Mallett * Note that td_critnest begins life as 1 because the thread is not 1476c27c603SJuli Mallett * running and is thereby implicitly waiting to be on the receiving 148a54e85fdSJeff Roberson * end of a context switch. 1496c27c603SJuli Mallett */ 150139b7550SJohn Baldwin td->td_critnest = 1; 151acbe332aSDavid Xu td->td_lend_user_pri = PRI_MAX; 152b209f889SRandall Stewart EVENTHANDLER_INVOKE(thread_ctor, td); 153911b84b0SRobert Watson #ifdef AUDIT 154911b84b0SRobert Watson audit_thread_alloc(td); 155911b84b0SRobert Watson #endif 156d10183d9SDavid Xu umtx_thread_alloc(td); 157b23f72e9SBrian Feldman return (0); 15844990b8cSJulian Elischer } 15944990b8cSJulian Elischer 16044990b8cSJulian Elischer /* 16144990b8cSJulian Elischer * Reclaim a thread after use. 16244990b8cSJulian Elischer */ 16344990b8cSJulian Elischer static void 16444990b8cSJulian Elischer thread_dtor(void *mem, int size, void *arg) 16544990b8cSJulian Elischer { 16644990b8cSJulian Elischer struct thread *td; 16744990b8cSJulian Elischer 16844990b8cSJulian Elischer td = (struct thread *)mem; 16944990b8cSJulian Elischer 17044990b8cSJulian Elischer #ifdef INVARIANTS 17144990b8cSJulian Elischer /* Verify that this thread is in a safe state to free. */ 17244990b8cSJulian Elischer switch (td->td_state) { 17371fad9fdSJulian Elischer case TDS_INHIBITED: 17471fad9fdSJulian Elischer case TDS_RUNNING: 17571fad9fdSJulian Elischer case TDS_CAN_RUN: 17644990b8cSJulian Elischer case TDS_RUNQ: 17744990b8cSJulian Elischer /* 17844990b8cSJulian Elischer * We must never unlink a thread that is in one of 17944990b8cSJulian Elischer * these states, because it is currently active. 18044990b8cSJulian Elischer */ 18144990b8cSJulian Elischer panic("bad state for thread unlinking"); 18244990b8cSJulian Elischer /* NOTREACHED */ 18371fad9fdSJulian Elischer case TDS_INACTIVE: 18444990b8cSJulian Elischer break; 18544990b8cSJulian Elischer default: 18644990b8cSJulian Elischer panic("bad thread state"); 18744990b8cSJulian Elischer /* NOTREACHED */ 18844990b8cSJulian Elischer } 18944990b8cSJulian Elischer #endif 1906e8525ceSRobert Watson #ifdef AUDIT 1916e8525ceSRobert Watson audit_thread_free(td); 1926e8525ceSRobert Watson #endif 1931ba4a712SPawel Jakub Dawidek /* Free all OSD associated to this thread. */ 1941ba4a712SPawel Jakub Dawidek osd_thread_exit(td); 1951ba4a712SPawel Jakub Dawidek 196b209f889SRandall Stewart EVENTHANDLER_INVOKE(thread_dtor, td); 197ec6ea5e8SDavid Xu tid_free(td->td_tid); 19844990b8cSJulian Elischer } 19944990b8cSJulian Elischer 20044990b8cSJulian Elischer /* 20144990b8cSJulian Elischer * Initialize type-stable parts of a thread (when newly created). 20244990b8cSJulian Elischer */ 203b23f72e9SBrian Feldman static int 204b23f72e9SBrian Feldman thread_init(void *mem, int size, int flags) 20544990b8cSJulian Elischer { 20644990b8cSJulian Elischer struct thread *td; 20744990b8cSJulian Elischer 20844990b8cSJulian Elischer td = (struct thread *)mem; 209247aba24SMarcel Moolenaar 21044f3b092SJohn Baldwin td->td_sleepqueue = sleepq_alloc(); 211961a7b24SJohn Baldwin td->td_turnstile = turnstile_alloc(); 2128f0e9130SKonstantin Belousov td->td_rlqe = NULL; 213b209f889SRandall Stewart EVENTHANDLER_INVOKE(thread_init, td); 214de028f5aSJeff Roberson td->td_sched = (struct td_sched *)&td[1]; 215d10183d9SDavid Xu umtx_thread_init(td); 21689b57fcfSKonstantin Belousov td->td_kstack = 0; 217ad8b1d85SKonstantin Belousov td->td_sel = NULL; 218b23f72e9SBrian Feldman return (0); 21944990b8cSJulian Elischer } 22044990b8cSJulian Elischer 22144990b8cSJulian Elischer /* 22244990b8cSJulian Elischer * Tear down type-stable parts of a thread (just before being discarded). 22344990b8cSJulian Elischer */ 22444990b8cSJulian Elischer static void 22544990b8cSJulian Elischer thread_fini(void *mem, int size) 22644990b8cSJulian Elischer { 22744990b8cSJulian Elischer struct thread *td; 22844990b8cSJulian Elischer 22944990b8cSJulian Elischer td = (struct thread *)mem; 230b209f889SRandall Stewart EVENTHANDLER_INVOKE(thread_fini, td); 2318f0e9130SKonstantin Belousov rlqentry_free(td->td_rlqe); 232961a7b24SJohn Baldwin turnstile_free(td->td_turnstile); 23344f3b092SJohn Baldwin sleepq_free(td->td_sleepqueue); 234d10183d9SDavid Xu umtx_thread_fini(td); 235ace8398dSJeff Roberson seltdfini(td); 23644990b8cSJulian Elischer } 2375215b187SJeff Roberson 2385c8329edSJulian Elischer /* 2395215b187SJeff Roberson * For a newly created process, 2405215b187SJeff Roberson * link up all the structures and its initial threads etc. 241ed062c8dSJulian Elischer * called from: 242e7d939bdSMarcel Moolenaar * {arch}/{arch}/machdep.c {arch}_init(), init386() etc. 243ed062c8dSJulian Elischer * proc_dtor() (should go away) 244ed062c8dSJulian Elischer * proc_init() 2455c8329edSJulian Elischer */ 2465c8329edSJulian Elischer void 24789b57fcfSKonstantin Belousov proc_linkup0(struct proc *p, struct thread *td) 24889b57fcfSKonstantin Belousov { 24989b57fcfSKonstantin Belousov TAILQ_INIT(&p->p_threads); /* all threads in proc */ 25089b57fcfSKonstantin Belousov proc_linkup(p, td); 25189b57fcfSKonstantin Belousov } 25289b57fcfSKonstantin Belousov 25389b57fcfSKonstantin Belousov void 2548460a577SJohn Birrell proc_linkup(struct proc *p, struct thread *td) 2555c8329edSJulian Elischer { 256a54e85fdSJeff Roberson 2579104847fSDavid Xu sigqueue_init(&p->p_sigqueue, p); 258ebceaf6dSDavid Xu p->p_ksi = ksiginfo_alloc(1); 259ebceaf6dSDavid Xu if (p->p_ksi != NULL) { 2605c474517SDavid Xu /* XXX p_ksi may be null if ksiginfo zone is not ready */ 261ebceaf6dSDavid Xu p->p_ksi->ksi_flags = KSI_EXT | KSI_INS; 262ebceaf6dSDavid Xu } 263b2f92ef9SDavid Xu LIST_INIT(&p->p_mqnotifier); 2645c8329edSJulian Elischer p->p_numthreads = 0; 2658460a577SJohn Birrell thread_link(td, p); 2665c8329edSJulian Elischer } 2675c8329edSJulian Elischer 2685c8329edSJulian Elischer /* 26944990b8cSJulian Elischer * Initialize global thread allocation resources. 27044990b8cSJulian Elischer */ 27144990b8cSJulian Elischer void 27244990b8cSJulian Elischer threadinit(void) 27344990b8cSJulian Elischer { 27444990b8cSJulian Elischer 2751ea7a6f8SPoul-Henning Kamp mtx_init(&tid_lock, "TID lock", NULL, MTX_DEF); 27602c6fc21SKonstantin Belousov 27702c6fc21SKonstantin Belousov /* 278abce621cSKonstantin Belousov * pid_max cannot be greater than PID_MAX. 27902c6fc21SKonstantin Belousov * leave one number for thread0. 28002c6fc21SKonstantin Belousov */ 2816829a5c5SJulian Elischer tid_unrhdr = new_unrhdr(PID_MAX + 2, INT_MAX, &tid_lock); 2821ea7a6f8SPoul-Henning Kamp 283de028f5aSJeff Roberson thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(), 28444990b8cSJulian Elischer thread_ctor, thread_dtor, thread_init, thread_fini, 2851b253694SKonstantin Belousov 16 - 1, UMA_ZONE_NOFREE); 286cf7d9a8cSDavid Xu tidhashtbl = hashinit(maxproc / 2, M_TIDHASH, &tidhash); 287cf7d9a8cSDavid Xu rw_init(&tidhash_lock, "tidhash"); 28844990b8cSJulian Elischer } 28944990b8cSJulian Elischer 29044990b8cSJulian Elischer /* 291ff8fbcffSJeff Roberson * Place an unused thread on the zombie list. 292ad1e7d28SJulian Elischer * Use the slpq as that must be unused by now. 29344990b8cSJulian Elischer */ 29444990b8cSJulian Elischer void 295ff8fbcffSJeff Roberson thread_zombie(struct thread *td) 29644990b8cSJulian Elischer { 297a54e85fdSJeff Roberson mtx_lock_spin(&zombie_lock); 298ad1e7d28SJulian Elischer TAILQ_INSERT_HEAD(&zombie_threads, td, td_slpq); 299a54e85fdSJeff Roberson mtx_unlock_spin(&zombie_lock); 30044990b8cSJulian Elischer } 30144990b8cSJulian Elischer 3025c8329edSJulian Elischer /* 303ff8fbcffSJeff Roberson * Release a thread that has exited after cpu_throw(). 304ff8fbcffSJeff Roberson */ 305ff8fbcffSJeff Roberson void 306ff8fbcffSJeff Roberson thread_stash(struct thread *td) 307ff8fbcffSJeff Roberson { 308ff8fbcffSJeff Roberson atomic_subtract_rel_int(&td->td_proc->p_exitthreads, 1); 309ff8fbcffSJeff Roberson thread_zombie(td); 310ff8fbcffSJeff Roberson } 311ff8fbcffSJeff Roberson 312ff8fbcffSJeff Roberson /* 3136617724cSJeff Roberson * Reap zombie resources. 31444990b8cSJulian Elischer */ 31544990b8cSJulian Elischer void 31644990b8cSJulian Elischer thread_reap(void) 31744990b8cSJulian Elischer { 3185c8329edSJulian Elischer struct thread *td_first, *td_next; 31944990b8cSJulian Elischer 32044990b8cSJulian Elischer /* 3215215b187SJeff Roberson * Don't even bother to lock if none at this instant, 3225215b187SJeff Roberson * we really don't care about the next instant.. 32344990b8cSJulian Elischer */ 3248460a577SJohn Birrell if (!TAILQ_EMPTY(&zombie_threads)) { 325a54e85fdSJeff Roberson mtx_lock_spin(&zombie_lock); 3265c8329edSJulian Elischer td_first = TAILQ_FIRST(&zombie_threads); 3275c8329edSJulian Elischer if (td_first) 3285c8329edSJulian Elischer TAILQ_INIT(&zombie_threads); 329a54e85fdSJeff Roberson mtx_unlock_spin(&zombie_lock); 3305c8329edSJulian Elischer while (td_first) { 331ad1e7d28SJulian Elischer td_next = TAILQ_NEXT(td_first, td_slpq); 3324ea6a9a2SMateusz Guzik thread_cow_free(td_first); 3335c8329edSJulian Elischer thread_free(td_first); 3345c8329edSJulian Elischer td_first = td_next; 33544990b8cSJulian Elischer } 33644990b8cSJulian Elischer } 337ed062c8dSJulian Elischer } 33844990b8cSJulian Elischer 3394f0db5e0SJulian Elischer /* 34044990b8cSJulian Elischer * Allocate a thread. 34144990b8cSJulian Elischer */ 34244990b8cSJulian Elischer struct thread * 3438a945d10SKonstantin Belousov thread_alloc(int pages) 34444990b8cSJulian Elischer { 34589b57fcfSKonstantin Belousov struct thread *td; 3468460a577SJohn Birrell 34744990b8cSJulian Elischer thread_reap(); /* check if any zombies to get */ 34889b57fcfSKonstantin Belousov 34989b57fcfSKonstantin Belousov td = (struct thread *)uma_zalloc(thread_zone, M_WAITOK); 35089b57fcfSKonstantin Belousov KASSERT(td->td_kstack == 0, ("thread_alloc got thread with kstack")); 3518a945d10SKonstantin Belousov if (!vm_thread_new(td, pages)) { 35289b57fcfSKonstantin Belousov uma_zfree(thread_zone, td); 35389b57fcfSKonstantin Belousov return (NULL); 35489b57fcfSKonstantin Belousov } 3550c3967e7SMarcel Moolenaar cpu_thread_alloc(td); 3566520495aSAdrian Chadd vm_domain_policy_init(&td->td_vm_dom_policy); 35789b57fcfSKonstantin Belousov return (td); 35844990b8cSJulian Elischer } 35944990b8cSJulian Elischer 3608a945d10SKonstantin Belousov int 3618a945d10SKonstantin Belousov thread_alloc_stack(struct thread *td, int pages) 3628a945d10SKonstantin Belousov { 3638a945d10SKonstantin Belousov 3648a945d10SKonstantin Belousov KASSERT(td->td_kstack == 0, 3658a945d10SKonstantin Belousov ("thread_alloc_stack called on a thread with kstack")); 3668a945d10SKonstantin Belousov if (!vm_thread_new(td, pages)) 3678a945d10SKonstantin Belousov return (0); 3688a945d10SKonstantin Belousov cpu_thread_alloc(td); 3698a945d10SKonstantin Belousov return (1); 3708a945d10SKonstantin Belousov } 3714f0db5e0SJulian Elischer 3724f0db5e0SJulian Elischer /* 37344990b8cSJulian Elischer * Deallocate a thread. 37444990b8cSJulian Elischer */ 37544990b8cSJulian Elischer void 37644990b8cSJulian Elischer thread_free(struct thread *td) 37744990b8cSJulian Elischer { 3782e6b8de4SJeff Roberson 3792e6b8de4SJeff Roberson lock_profile_thread_exit(td); 38045aea8deSJeff Roberson if (td->td_cpuset) 381d7f687fcSJeff Roberson cpuset_rel(td->td_cpuset); 382d7f687fcSJeff Roberson td->td_cpuset = NULL; 3830c3967e7SMarcel Moolenaar cpu_thread_free(td); 38489b57fcfSKonstantin Belousov if (td->td_kstack != 0) 38589b57fcfSKonstantin Belousov vm_thread_dispose(td); 3866520495aSAdrian Chadd vm_domain_policy_cleanup(&td->td_vm_dom_policy); 38744990b8cSJulian Elischer uma_zfree(thread_zone, td); 38844990b8cSJulian Elischer } 38944990b8cSJulian Elischer 3904ea6a9a2SMateusz Guzik void 3914ea6a9a2SMateusz Guzik thread_cow_get_proc(struct thread *newtd, struct proc *p) 3924ea6a9a2SMateusz Guzik { 3934ea6a9a2SMateusz Guzik 3944ea6a9a2SMateusz Guzik PROC_LOCK_ASSERT(p, MA_OWNED); 3954ea6a9a2SMateusz Guzik newtd->td_ucred = crhold(p->p_ucred); 396f6f6d240SMateusz Guzik newtd->td_limit = lim_hold(p->p_limit); 3974ea6a9a2SMateusz Guzik newtd->td_cowgen = p->p_cowgen; 3984ea6a9a2SMateusz Guzik } 3994ea6a9a2SMateusz Guzik 4004ea6a9a2SMateusz Guzik void 4014ea6a9a2SMateusz Guzik thread_cow_get(struct thread *newtd, struct thread *td) 4024ea6a9a2SMateusz Guzik { 4034ea6a9a2SMateusz Guzik 4044ea6a9a2SMateusz Guzik newtd->td_ucred = crhold(td->td_ucred); 405f6f6d240SMateusz Guzik newtd->td_limit = lim_hold(td->td_limit); 4064ea6a9a2SMateusz Guzik newtd->td_cowgen = td->td_cowgen; 4074ea6a9a2SMateusz Guzik } 4084ea6a9a2SMateusz Guzik 4094ea6a9a2SMateusz Guzik void 4104ea6a9a2SMateusz Guzik thread_cow_free(struct thread *td) 4114ea6a9a2SMateusz Guzik { 4124ea6a9a2SMateusz Guzik 413cd672ca6SMateusz Guzik if (td->td_ucred != NULL) 4144ea6a9a2SMateusz Guzik crfree(td->td_ucred); 415cd672ca6SMateusz Guzik if (td->td_limit != NULL) 416f6f6d240SMateusz Guzik lim_free(td->td_limit); 4174ea6a9a2SMateusz Guzik } 4184ea6a9a2SMateusz Guzik 4194ea6a9a2SMateusz Guzik void 4204ea6a9a2SMateusz Guzik thread_cow_update(struct thread *td) 4214ea6a9a2SMateusz Guzik { 4224ea6a9a2SMateusz Guzik struct proc *p; 423cd672ca6SMateusz Guzik struct ucred *oldcred; 424cd672ca6SMateusz Guzik struct plimit *oldlimit; 4254ea6a9a2SMateusz Guzik 4264ea6a9a2SMateusz Guzik p = td->td_proc; 427cd672ca6SMateusz Guzik oldcred = NULL; 428cd672ca6SMateusz Guzik oldlimit = NULL; 4294ea6a9a2SMateusz Guzik PROC_LOCK(p); 430cd672ca6SMateusz Guzik if (td->td_ucred != p->p_ucred) { 431cd672ca6SMateusz Guzik oldcred = td->td_ucred; 432cd672ca6SMateusz Guzik td->td_ucred = crhold(p->p_ucred); 433cd672ca6SMateusz Guzik } 434cd672ca6SMateusz Guzik if (td->td_limit != p->p_limit) { 435cd672ca6SMateusz Guzik oldlimit = td->td_limit; 436cd672ca6SMateusz Guzik td->td_limit = lim_hold(p->p_limit); 437cd672ca6SMateusz Guzik } 4384ea6a9a2SMateusz Guzik td->td_cowgen = p->p_cowgen; 4394ea6a9a2SMateusz Guzik PROC_UNLOCK(p); 440cd672ca6SMateusz Guzik if (oldcred != NULL) 441cd672ca6SMateusz Guzik crfree(oldcred); 442cd672ca6SMateusz Guzik if (oldlimit != NULL) 443cd672ca6SMateusz Guzik lim_free(oldlimit); 4444ea6a9a2SMateusz Guzik } 4454ea6a9a2SMateusz Guzik 44644990b8cSJulian Elischer /* 44744990b8cSJulian Elischer * Discard the current thread and exit from its context. 44894e0a4cdSJulian Elischer * Always called with scheduler locked. 44944990b8cSJulian Elischer * 45044990b8cSJulian Elischer * Because we can't free a thread while we're operating under its context, 451696058c3SJulian Elischer * push the current thread into our CPU's deadthread holder. This means 452696058c3SJulian Elischer * we needn't worry about someone else grabbing our context before we 4536617724cSJeff Roberson * do a cpu_throw(). 45444990b8cSJulian Elischer */ 45544990b8cSJulian Elischer void 45644990b8cSJulian Elischer thread_exit(void) 45744990b8cSJulian Elischer { 4587e3a96eaSJohn Baldwin uint64_t runtime, new_switchtime; 45944990b8cSJulian Elischer struct thread *td; 4601c4bcd05SJeff Roberson struct thread *td2; 46144990b8cSJulian Elischer struct proc *p; 4627847a9daSJohn Baldwin int wakeup_swapper; 46344990b8cSJulian Elischer 46444990b8cSJulian Elischer td = curthread; 46544990b8cSJulian Elischer p = td->td_proc; 46644990b8cSJulian Elischer 467a54e85fdSJeff Roberson PROC_SLOCK_ASSERT(p, MA_OWNED); 468ed062c8dSJulian Elischer mtx_assert(&Giant, MA_NOTOWNED); 469a54e85fdSJeff Roberson 47044990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 471ed062c8dSJulian Elischer KASSERT(p != NULL, ("thread exiting without a process")); 472cc701b73SRobert Watson CTR3(KTR_PROC, "thread_exit: thread %p (pid %ld, %s)", td, 473e01eafefSJulian Elischer (long)p->p_pid, td->td_name); 4749104847fSDavid Xu KASSERT(TAILQ_EMPTY(&td->td_sigqueue.sq_list), ("signal pending")); 47544990b8cSJulian Elischer 47689964dd2SRobert Watson #ifdef AUDIT 47789964dd2SRobert Watson AUDIT_SYSCALL_EXIT(0, td); 47889964dd2SRobert Watson #endif 479ed062c8dSJulian Elischer /* 480ed062c8dSJulian Elischer * drop FPU & debug register state storage, or any other 481ed062c8dSJulian Elischer * architecture specific resources that 482ed062c8dSJulian Elischer * would not be on a new untouched process. 483ed062c8dSJulian Elischer */ 48444990b8cSJulian Elischer cpu_thread_exit(td); /* XXXSMP */ 48544990b8cSJulian Elischer 486ed062c8dSJulian Elischer /* 4871faf202eSJulian Elischer * The last thread is left attached to the process 4881faf202eSJulian Elischer * So that the whole bundle gets recycled. Skip 489ed062c8dSJulian Elischer * all this stuff if we never had threads. 490ed062c8dSJulian Elischer * EXIT clears all sign of other threads when 491ed062c8dSJulian Elischer * it goes to single threading, so the last thread always 492ed062c8dSJulian Elischer * takes the short path. 4931faf202eSJulian Elischer */ 494ed062c8dSJulian Elischer if (p->p_flag & P_HADTHREADS) { 4951faf202eSJulian Elischer if (p->p_numthreads > 1) { 496fd229b5bSKonstantin Belousov atomic_add_int(&td->td_proc->p_exitthreads, 1); 497d3a0bd78SJulian Elischer thread_unlink(td); 4981c4bcd05SJeff Roberson td2 = FIRST_THREAD_IN_PROC(p); 4991c4bcd05SJeff Roberson sched_exit_thread(td2, td); 500ed062c8dSJulian Elischer 501ed062c8dSJulian Elischer /* 50244990b8cSJulian Elischer * The test below is NOT true if we are the 5039182554aSKonstantin Belousov * sole exiting thread. P_STOPPED_SINGLE is unset 50444990b8cSJulian Elischer * in exit1() after it is the only survivor. 50544990b8cSJulian Elischer */ 5061279572aSDavid Xu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 50744990b8cSJulian Elischer if (p->p_numthreads == p->p_suspcount) { 508a54e85fdSJeff Roberson thread_lock(p->p_singlethread); 5097847a9daSJohn Baldwin wakeup_swapper = thread_unsuspend_one( 51084cdea97SKonstantin Belousov p->p_singlethread, p, false); 511a54e85fdSJeff Roberson thread_unlock(p->p_singlethread); 5127847a9daSJohn Baldwin if (wakeup_swapper) 5137847a9daSJohn Baldwin kick_proc0(); 51444990b8cSJulian Elischer } 51544990b8cSJulian Elischer } 51648bfcdddSJulian Elischer 517696058c3SJulian Elischer PCPU_SET(deadthread, td); 5181faf202eSJulian Elischer } else { 519ed062c8dSJulian Elischer /* 520ed062c8dSJulian Elischer * The last thread is exiting.. but not through exit() 521ed062c8dSJulian Elischer */ 522ed062c8dSJulian Elischer panic ("thread_exit: Last thread exiting on its own"); 523ed062c8dSJulian Elischer } 5241faf202eSJulian Elischer } 52516d95d4fSJoseph Koshy #ifdef HWPMC_HOOKS 52616d95d4fSJoseph Koshy /* 52716d95d4fSJoseph Koshy * If this thread is part of a process that is being tracked by hwpmc(4), 52816d95d4fSJoseph Koshy * inform the module of the thread's impending exit. 52916d95d4fSJoseph Koshy */ 53016d95d4fSJoseph Koshy if (PMC_PROC_IS_USING_PMCS(td->td_proc)) 53116d95d4fSJoseph Koshy PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT); 53216d95d4fSJoseph Koshy #endif 533a54e85fdSJeff Roberson PROC_UNLOCK(p); 5345c7bebf9SKonstantin Belousov PROC_STATLOCK(p); 5355c7bebf9SKonstantin Belousov thread_lock(td); 5365c7bebf9SKonstantin Belousov PROC_SUNLOCK(p); 5377e3a96eaSJohn Baldwin 5387e3a96eaSJohn Baldwin /* Do the same timestamp bookkeeping that mi_switch() would do. */ 5397e3a96eaSJohn Baldwin new_switchtime = cpu_ticks(); 5407e3a96eaSJohn Baldwin runtime = new_switchtime - PCPU_GET(switchtime); 5417e3a96eaSJohn Baldwin td->td_runtime += runtime; 5427e3a96eaSJohn Baldwin td->td_incruntime += runtime; 5437e3a96eaSJohn Baldwin PCPU_SET(switchtime, new_switchtime); 5447e3a96eaSJohn Baldwin PCPU_SET(switchticks, ticks); 5457e3a96eaSJohn Baldwin PCPU_INC(cnt.v_swtch); 5467e3a96eaSJohn Baldwin 5477e3a96eaSJohn Baldwin /* Save our resource usage in our process. */ 5487e3a96eaSJohn Baldwin td->td_ru.ru_nvcsw++; 54941fd9c63SKonstantin Belousov ruxagg(p, td); 5507e3a96eaSJohn Baldwin rucollect(&p->p_ru, &td->td_ru); 5515c7bebf9SKonstantin Belousov PROC_STATUNLOCK(p); 5527e3a96eaSJohn Baldwin 553dcc9954eSJulian Elischer td->td_state = TDS_INACTIVE; 5543d06b4b3SAttilio Rao #ifdef WITNESS 5553d06b4b3SAttilio Rao witness_thread_exit(td); 5563d06b4b3SAttilio Rao #endif 557732d9528SJulian Elischer CTR1(KTR_PROC, "thread_exit: cpu_throw() thread %p", td); 558a54e85fdSJeff Roberson sched_throw(td); 559cc66ebe2SPeter Wemm panic("I'm a teapot!"); 56044990b8cSJulian Elischer /* NOTREACHED */ 56144990b8cSJulian Elischer } 56244990b8cSJulian Elischer 56344990b8cSJulian Elischer /* 564696058c3SJulian Elischer * Do any thread specific cleanups that may be needed in wait() 56537814395SPeter Wemm * called with Giant, proc and schedlock not held. 566696058c3SJulian Elischer */ 567696058c3SJulian Elischer void 568696058c3SJulian Elischer thread_wait(struct proc *p) 569696058c3SJulian Elischer { 570696058c3SJulian Elischer struct thread *td; 571696058c3SJulian Elischer 57237814395SPeter Wemm mtx_assert(&Giant, MA_NOTOWNED); 573624bf9e1SKonstantin Belousov KASSERT(p->p_numthreads == 1, ("multiple threads in thread_wait()")); 574624bf9e1SKonstantin Belousov KASSERT(p->p_exitthreads == 0, ("p_exitthreads leaking")); 575ff8fbcffSJeff Roberson td = FIRST_THREAD_IN_PROC(p); 576ff8fbcffSJeff Roberson /* Lock the last thread so we spin until it exits cpu_throw(). */ 577ff8fbcffSJeff Roberson thread_lock(td); 578ff8fbcffSJeff Roberson thread_unlock(td); 5792e6b8de4SJeff Roberson lock_profile_thread_exit(td); 580d7f687fcSJeff Roberson cpuset_rel(td->td_cpuset); 581d7f687fcSJeff Roberson td->td_cpuset = NULL; 582696058c3SJulian Elischer cpu_thread_clean(td); 5834ea6a9a2SMateusz Guzik thread_cow_free(td); 584696058c3SJulian Elischer thread_reap(); /* check for zombie threads etc. */ 585696058c3SJulian Elischer } 586696058c3SJulian Elischer 587696058c3SJulian Elischer /* 58844990b8cSJulian Elischer * Link a thread to a process. 5891faf202eSJulian Elischer * set up anything that needs to be initialized for it to 5901faf202eSJulian Elischer * be used by the process. 59144990b8cSJulian Elischer */ 59244990b8cSJulian Elischer void 5938460a577SJohn Birrell thread_link(struct thread *td, struct proc *p) 59444990b8cSJulian Elischer { 59544990b8cSJulian Elischer 596a54e85fdSJeff Roberson /* 597a54e85fdSJeff Roberson * XXX This can't be enabled because it's called for proc0 before 598374ae2a3SJeff Roberson * its lock has been created. 599374ae2a3SJeff Roberson * PROC_LOCK_ASSERT(p, MA_OWNED); 600a54e85fdSJeff Roberson */ 60171fad9fdSJulian Elischer td->td_state = TDS_INACTIVE; 60244990b8cSJulian Elischer td->td_proc = p; 603b61ce5b0SJeff Roberson td->td_flags = TDF_INMEM; 60444990b8cSJulian Elischer 6051faf202eSJulian Elischer LIST_INIT(&td->td_contested); 606eea4f254SJeff Roberson LIST_INIT(&td->td_lprof[0]); 607eea4f254SJeff Roberson LIST_INIT(&td->td_lprof[1]); 6089104847fSDavid Xu sigqueue_init(&td->td_sigqueue, p); 609fd90e2edSJung-uk Kim callout_init(&td->td_slpcallout, 1); 61066d8df9dSDaniel Eischen TAILQ_INSERT_TAIL(&p->p_threads, td, td_plist); 61144990b8cSJulian Elischer p->p_numthreads++; 61244990b8cSJulian Elischer } 61344990b8cSJulian Elischer 614ed062c8dSJulian Elischer /* 615ed062c8dSJulian Elischer * Called from: 616ed062c8dSJulian Elischer * thread_exit() 617ed062c8dSJulian Elischer */ 618d3a0bd78SJulian Elischer void 619d3a0bd78SJulian Elischer thread_unlink(struct thread *td) 620d3a0bd78SJulian Elischer { 621d3a0bd78SJulian Elischer struct proc *p = td->td_proc; 622d3a0bd78SJulian Elischer 623374ae2a3SJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 624d3a0bd78SJulian Elischer TAILQ_REMOVE(&p->p_threads, td, td_plist); 625d3a0bd78SJulian Elischer p->p_numthreads--; 626d3a0bd78SJulian Elischer /* could clear a few other things here */ 6278460a577SJohn Birrell /* Must NOT clear links to proc! */ 6285c8329edSJulian Elischer } 6295c8329edSJulian Elischer 63079799053SKonstantin Belousov static int 63179799053SKonstantin Belousov calc_remaining(struct proc *p, int mode) 63279799053SKonstantin Belousov { 63379799053SKonstantin Belousov int remaining; 63479799053SKonstantin Belousov 6357b519077SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 6367b519077SKonstantin Belousov PROC_SLOCK_ASSERT(p, MA_OWNED); 63779799053SKonstantin Belousov if (mode == SINGLE_EXIT) 63879799053SKonstantin Belousov remaining = p->p_numthreads; 63979799053SKonstantin Belousov else if (mode == SINGLE_BOUNDARY) 64079799053SKonstantin Belousov remaining = p->p_numthreads - p->p_boundary_count; 6416ddcc233SKonstantin Belousov else if (mode == SINGLE_NO_EXIT || mode == SINGLE_ALLPROC) 64279799053SKonstantin Belousov remaining = p->p_numthreads - p->p_suspcount; 64379799053SKonstantin Belousov else 64479799053SKonstantin Belousov panic("calc_remaining: wrong mode %d", mode); 64579799053SKonstantin Belousov return (remaining); 64679799053SKonstantin Belousov } 64779799053SKonstantin Belousov 64807a9368aSKonstantin Belousov static int 64907a9368aSKonstantin Belousov remain_for_mode(int mode) 65007a9368aSKonstantin Belousov { 65107a9368aSKonstantin Belousov 6526ddcc233SKonstantin Belousov return (mode == SINGLE_ALLPROC ? 0 : 1); 65307a9368aSKonstantin Belousov } 65407a9368aSKonstantin Belousov 65507a9368aSKonstantin Belousov static int 65607a9368aSKonstantin Belousov weed_inhib(int mode, struct thread *td2, struct proc *p) 65707a9368aSKonstantin Belousov { 65807a9368aSKonstantin Belousov int wakeup_swapper; 65907a9368aSKonstantin Belousov 66007a9368aSKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 66107a9368aSKonstantin Belousov PROC_SLOCK_ASSERT(p, MA_OWNED); 66207a9368aSKonstantin Belousov THREAD_LOCK_ASSERT(td2, MA_OWNED); 66307a9368aSKonstantin Belousov 66407a9368aSKonstantin Belousov wakeup_swapper = 0; 66507a9368aSKonstantin Belousov switch (mode) { 66607a9368aSKonstantin Belousov case SINGLE_EXIT: 66707a9368aSKonstantin Belousov if (TD_IS_SUSPENDED(td2)) 66884cdea97SKonstantin Belousov wakeup_swapper |= thread_unsuspend_one(td2, p, true); 66907a9368aSKonstantin Belousov if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR) != 0) 67007a9368aSKonstantin Belousov wakeup_swapper |= sleepq_abort(td2, EINTR); 67107a9368aSKonstantin Belousov break; 67207a9368aSKonstantin Belousov case SINGLE_BOUNDARY: 67307a9368aSKonstantin Belousov if (TD_IS_SUSPENDED(td2) && (td2->td_flags & TDF_BOUNDARY) == 0) 67484cdea97SKonstantin Belousov wakeup_swapper |= thread_unsuspend_one(td2, p, false); 67507a9368aSKonstantin Belousov if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR) != 0) 67607a9368aSKonstantin Belousov wakeup_swapper |= sleepq_abort(td2, ERESTART); 67707a9368aSKonstantin Belousov break; 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; 9037847a9daSJohn Baldwin 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. 916b6d5995eSJulian Elischer * XXX Should be safe to access unlocked 917b6d5995eSJulian Elischer * as it can only be set to be true 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")); 936d071a6faSJohn Baldwin return (0); 937d071a6faSJohn Baldwin } 938d071a6faSJohn Baldwin 939d071a6faSJohn Baldwin /* 94044990b8cSJulian Elischer * If the process is waiting for us to exit, 94144990b8cSJulian Elischer * this thread should just suicide. 9421279572aSDavid Xu * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE. 94344990b8cSJulian Elischer */ 944cf7d9a8cSDavid Xu if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) { 945cf7d9a8cSDavid Xu PROC_UNLOCK(p); 94691d1786fSDmitry Chagin 94791d1786fSDmitry Chagin /* 94891d1786fSDmitry Chagin * Allow Linux emulation layer to do some work 94991d1786fSDmitry Chagin * before thread suicide. 95091d1786fSDmitry Chagin */ 95191d1786fSDmitry Chagin if (__predict_false(p->p_sysent->sv_thread_detach != NULL)) 95291d1786fSDmitry Chagin (p->p_sysent->sv_thread_detach)(td); 953*2a339d9eSKonstantin Belousov umtx_thread_exit(td); 954d1e7a4a5SJohn Baldwin kern_thr_exit(td); 955d1e7a4a5SJohn Baldwin panic("stopped thread did not exit"); 956cf7d9a8cSDavid Xu } 95721ecd1e9SDavid Xu 95821ecd1e9SDavid Xu PROC_SLOCK(p); 95921ecd1e9SDavid Xu thread_stopped(p); 960a54e85fdSJeff Roberson if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 961a54e85fdSJeff Roberson if (p->p_numthreads == p->p_suspcount + 1) { 962a54e85fdSJeff Roberson thread_lock(p->p_singlethread); 96384cdea97SKonstantin Belousov wakeup_swapper = thread_unsuspend_one( 96484cdea97SKonstantin Belousov p->p_singlethread, p, false); 965a54e85fdSJeff Roberson thread_unlock(p->p_singlethread); 9667847a9daSJohn Baldwin if (wakeup_swapper) 9677847a9daSJohn Baldwin kick_proc0(); 968a54e85fdSJeff Roberson } 969a54e85fdSJeff Roberson } 9703f9be10eSDavid Xu PROC_UNLOCK(p); 9717b4a950aSDavid Xu thread_lock(td); 97244990b8cSJulian Elischer /* 97344990b8cSJulian Elischer * When a thread suspends, it just 974ad1e7d28SJulian Elischer * gets taken off all queues. 97544990b8cSJulian Elischer */ 97671fad9fdSJulian Elischer thread_suspend_one(td); 977906ac69dSDavid Xu if (return_instead == 0) { 978906ac69dSDavid Xu p->p_boundary_count++; 979906ac69dSDavid Xu td->td_flags |= TDF_BOUNDARY; 980cf19bf91SJulian Elischer } 9817b4a950aSDavid Xu PROC_SUNLOCK(p); 9828df78c41SJeff Roberson mi_switch(SW_INVOL | SWT_SUSPEND, NULL); 983a54e85fdSJeff Roberson thread_unlock(td); 98444990b8cSJulian Elischer PROC_LOCK(p); 98544990b8cSJulian Elischer } 98644990b8cSJulian Elischer return (0); 98744990b8cSJulian Elischer } 98844990b8cSJulian Elischer 98935c32a76SDavid Xu void 9906ddcc233SKonstantin Belousov thread_suspend_switch(struct thread *td, struct proc *p) 991a54e85fdSJeff Roberson { 992a54e85fdSJeff Roberson 993a54e85fdSJeff Roberson KASSERT(!TD_IS_SUSPENDED(td), ("already suspended")); 994a54e85fdSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 9957b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 996a54e85fdSJeff Roberson /* 997a54e85fdSJeff Roberson * We implement thread_suspend_one in stages here to avoid 998a54e85fdSJeff Roberson * dropping the proc lock while the thread lock is owned. 999a54e85fdSJeff Roberson */ 10006ddcc233SKonstantin Belousov if (p == td->td_proc) { 1001a54e85fdSJeff Roberson thread_stopped(p); 1002a54e85fdSJeff Roberson p->p_suspcount++; 10036ddcc233SKonstantin Belousov } 10043f9be10eSDavid Xu PROC_UNLOCK(p); 10057b4a950aSDavid Xu thread_lock(td); 1006b7edba77SJeff Roberson td->td_flags &= ~TDF_NEEDSUSPCHK; 1007a54e85fdSJeff Roberson TD_SET_SUSPENDED(td); 1008c5aa6b58SJeff Roberson sched_sleep(td, 0); 10097b4a950aSDavid Xu PROC_SUNLOCK(p); 1010a54e85fdSJeff Roberson DROP_GIANT(); 10118df78c41SJeff Roberson mi_switch(SW_VOL | SWT_SUSPEND, NULL); 1012a54e85fdSJeff Roberson thread_unlock(td); 1013a54e85fdSJeff Roberson PICKUP_GIANT(); 1014a54e85fdSJeff Roberson PROC_LOCK(p); 10157b4a950aSDavid Xu PROC_SLOCK(p); 1016a54e85fdSJeff Roberson } 1017a54e85fdSJeff Roberson 1018a54e85fdSJeff Roberson void 101935c32a76SDavid Xu thread_suspend_one(struct thread *td) 102035c32a76SDavid Xu { 10216ddcc233SKonstantin Belousov struct proc *p; 102235c32a76SDavid Xu 10236ddcc233SKonstantin Belousov p = td->td_proc; 10247b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 1025a54e85fdSJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 1026e574e444SDavid Xu KASSERT(!TD_IS_SUSPENDED(td), ("already suspended")); 102735c32a76SDavid Xu p->p_suspcount++; 1028b7edba77SJeff Roberson td->td_flags &= ~TDF_NEEDSUSPCHK; 102971fad9fdSJulian Elischer TD_SET_SUSPENDED(td); 1030c5aa6b58SJeff Roberson sched_sleep(td, 0); 103135c32a76SDavid Xu } 103235c32a76SDavid Xu 103384cdea97SKonstantin Belousov static int 103484cdea97SKonstantin Belousov thread_unsuspend_one(struct thread *td, struct proc *p, bool boundary) 103535c32a76SDavid Xu { 103635c32a76SDavid Xu 1037a54e85fdSJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 1038ad1e7d28SJulian Elischer KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended")); 103971fad9fdSJulian Elischer TD_CLR_SUSPENDED(td); 10406ddcc233SKonstantin Belousov td->td_flags &= ~TDF_ALLPROCSUSP; 10416ddcc233SKonstantin Belousov if (td->td_proc == p) { 10426ddcc233SKonstantin Belousov PROC_SLOCK_ASSERT(p, MA_OWNED); 104335c32a76SDavid Xu p->p_suspcount--; 104484cdea97SKonstantin Belousov if (boundary && (td->td_flags & TDF_BOUNDARY) != 0) { 104584cdea97SKonstantin Belousov td->td_flags &= ~TDF_BOUNDARY; 104684cdea97SKonstantin Belousov p->p_boundary_count--; 104784cdea97SKonstantin Belousov } 10486ddcc233SKonstantin Belousov } 10497847a9daSJohn Baldwin return (setrunnable(td)); 105035c32a76SDavid Xu } 105135c32a76SDavid Xu 105244990b8cSJulian Elischer /* 105344990b8cSJulian Elischer * Allow all threads blocked by single threading to continue running. 105444990b8cSJulian Elischer */ 105544990b8cSJulian Elischer void 105644990b8cSJulian Elischer thread_unsuspend(struct proc *p) 105744990b8cSJulian Elischer { 105844990b8cSJulian Elischer struct thread *td; 10597847a9daSJohn Baldwin int wakeup_swapper; 106044990b8cSJulian Elischer 106144990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 10627b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 10637847a9daSJohn Baldwin wakeup_swapper = 0; 106444990b8cSJulian Elischer if (!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 true); 107044990b8cSJulian Elischer } 1071a54e85fdSJeff Roberson thread_unlock(td); 1072ad1e7d28SJulian Elischer } 107384cdea97SKonstantin Belousov } else if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE && 107484cdea97SKonstantin Belousov p->p_numthreads == p->p_suspcount) { 107544990b8cSJulian Elischer /* 107644990b8cSJulian Elischer * Stopping everything also did the job for the single 107744990b8cSJulian Elischer * threading request. Now we've downgraded to single-threaded, 107844990b8cSJulian Elischer * let it continue. 107944990b8cSJulian Elischer */ 10806ddcc233SKonstantin Belousov if (p->p_singlethread->td_proc == p) { 1081a54e85fdSJeff Roberson thread_lock(p->p_singlethread); 10826ddcc233SKonstantin Belousov wakeup_swapper = thread_unsuspend_one( 108384cdea97SKonstantin Belousov p->p_singlethread, p, false); 1084a54e85fdSJeff Roberson thread_unlock(p->p_singlethread); 108544990b8cSJulian Elischer } 10866ddcc233SKonstantin Belousov } 10877847a9daSJohn Baldwin if (wakeup_swapper) 10887847a9daSJohn Baldwin kick_proc0(); 108944990b8cSJulian Elischer } 109044990b8cSJulian Elischer 1091ed062c8dSJulian Elischer /* 1092ed062c8dSJulian Elischer * End the single threading mode.. 1093ed062c8dSJulian Elischer */ 109444990b8cSJulian Elischer void 10956ddcc233SKonstantin Belousov thread_single_end(struct proc *p, int mode) 109644990b8cSJulian Elischer { 109744990b8cSJulian Elischer struct thread *td; 10987847a9daSJohn Baldwin int wakeup_swapper; 109944990b8cSJulian Elischer 11006ddcc233SKonstantin Belousov KASSERT(mode == SINGLE_EXIT || mode == SINGLE_BOUNDARY || 11016ddcc233SKonstantin Belousov mode == SINGLE_ALLPROC || mode == SINGLE_NO_EXIT, 11026ddcc233SKonstantin Belousov ("invalid mode %d", mode)); 110344990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 11046ddcc233SKonstantin Belousov KASSERT((mode == SINGLE_ALLPROC && (p->p_flag & P_TOTAL_STOP) != 0) || 11056ddcc233SKonstantin Belousov (mode != SINGLE_ALLPROC && (p->p_flag & P_TOTAL_STOP) == 0), 11066ddcc233SKonstantin Belousov ("mode %d does not match P_TOTAL_STOP", mode)); 110784cdea97SKonstantin Belousov KASSERT(mode == SINGLE_ALLPROC || p->p_singlethread == curthread, 110884cdea97SKonstantin Belousov ("thread_single_end from other thread %p %p", 110984cdea97SKonstantin Belousov curthread, p->p_singlethread)); 111084cdea97SKonstantin Belousov KASSERT(mode != SINGLE_BOUNDARY || 111184cdea97SKonstantin Belousov (p->p_flag & P_SINGLE_BOUNDARY) != 0, 111284cdea97SKonstantin Belousov ("mis-matched SINGLE_BOUNDARY flags %x", p->p_flag)); 11136ddcc233SKonstantin Belousov p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_SINGLE_BOUNDARY | 11146ddcc233SKonstantin Belousov P_TOTAL_STOP); 11157b4a950aSDavid Xu PROC_SLOCK(p); 111644990b8cSJulian Elischer p->p_singlethread = NULL; 11177847a9daSJohn Baldwin wakeup_swapper = 0; 111849539972SJulian Elischer /* 11197847a9daSJohn Baldwin * If there are other threads they may now run, 112049539972SJulian Elischer * unless of course there is a blanket 'stop order' 112149539972SJulian Elischer * on the process. The single threader must be allowed 112249539972SJulian Elischer * to continue however as this is a bad place to stop. 112349539972SJulian Elischer */ 11246ddcc233SKonstantin Belousov if (p->p_numthreads != remain_for_mode(mode) && !P_SHOULDSTOP(p)) { 1125ad1e7d28SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1126a54e85fdSJeff Roberson thread_lock(td); 1127ad1e7d28SJulian Elischer if (TD_IS_SUSPENDED(td)) { 112884cdea97SKonstantin Belousov wakeup_swapper |= thread_unsuspend_one(td, p, 112984cdea97SKonstantin Belousov mode == SINGLE_BOUNDARY); 113044990b8cSJulian Elischer } 1131a54e85fdSJeff Roberson thread_unlock(td); 113249539972SJulian Elischer } 1133ad1e7d28SJulian Elischer } 113484cdea97SKonstantin Belousov KASSERT(mode != SINGLE_BOUNDARY || p->p_boundary_count == 0, 113584cdea97SKonstantin Belousov ("inconsistent boundary count %d", p->p_boundary_count)); 11367b4a950aSDavid Xu PROC_SUNLOCK(p); 11377847a9daSJohn Baldwin if (wakeup_swapper) 11387847a9daSJohn Baldwin kick_proc0(); 113949539972SJulian Elischer } 11404fc21c09SDaniel Eischen 114144355392SDavid Xu struct thread * 114244355392SDavid Xu thread_find(struct proc *p, lwpid_t tid) 114344355392SDavid Xu { 114444355392SDavid Xu struct thread *td; 114544355392SDavid Xu 114644355392SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 114744355392SDavid Xu FOREACH_THREAD_IN_PROC(p, td) { 114844355392SDavid Xu if (td->td_tid == tid) 114944355392SDavid Xu break; 115044355392SDavid Xu } 115144355392SDavid Xu return (td); 115244355392SDavid Xu } 1153cf7d9a8cSDavid Xu 1154cf7d9a8cSDavid Xu /* Locate a thread by number; return with proc lock held. */ 1155cf7d9a8cSDavid Xu struct thread * 1156cf7d9a8cSDavid Xu tdfind(lwpid_t tid, pid_t pid) 1157cf7d9a8cSDavid Xu { 1158cf7d9a8cSDavid Xu #define RUN_THRESH 16 1159cf7d9a8cSDavid Xu struct thread *td; 1160cf7d9a8cSDavid Xu int run = 0; 1161cf7d9a8cSDavid Xu 1162cf7d9a8cSDavid Xu rw_rlock(&tidhash_lock); 1163cf7d9a8cSDavid Xu LIST_FOREACH(td, TIDHASH(tid), td_hash) { 1164cf7d9a8cSDavid Xu if (td->td_tid == tid) { 1165cf7d9a8cSDavid Xu if (pid != -1 && td->td_proc->p_pid != pid) { 1166cf7d9a8cSDavid Xu td = NULL; 1167cf7d9a8cSDavid Xu break; 1168cf7d9a8cSDavid Xu } 11698e6fa660SJohn Baldwin PROC_LOCK(td->td_proc); 1170cf7d9a8cSDavid Xu if (td->td_proc->p_state == PRS_NEW) { 11718e6fa660SJohn Baldwin PROC_UNLOCK(td->td_proc); 1172cf7d9a8cSDavid Xu td = NULL; 1173cf7d9a8cSDavid Xu break; 1174cf7d9a8cSDavid Xu } 1175cf7d9a8cSDavid Xu if (run > RUN_THRESH) { 1176cf7d9a8cSDavid Xu if (rw_try_upgrade(&tidhash_lock)) { 1177cf7d9a8cSDavid Xu LIST_REMOVE(td, td_hash); 1178cf7d9a8cSDavid Xu LIST_INSERT_HEAD(TIDHASH(td->td_tid), 1179cf7d9a8cSDavid Xu td, td_hash); 1180cf7d9a8cSDavid Xu rw_wunlock(&tidhash_lock); 1181cf7d9a8cSDavid Xu return (td); 1182cf7d9a8cSDavid Xu } 1183cf7d9a8cSDavid Xu } 1184cf7d9a8cSDavid Xu break; 1185cf7d9a8cSDavid Xu } 1186cf7d9a8cSDavid Xu run++; 1187cf7d9a8cSDavid Xu } 1188cf7d9a8cSDavid Xu rw_runlock(&tidhash_lock); 1189cf7d9a8cSDavid Xu return (td); 1190cf7d9a8cSDavid Xu } 1191cf7d9a8cSDavid Xu 1192cf7d9a8cSDavid Xu void 1193cf7d9a8cSDavid Xu tidhash_add(struct thread *td) 1194cf7d9a8cSDavid Xu { 1195cf7d9a8cSDavid Xu rw_wlock(&tidhash_lock); 1196cf7d9a8cSDavid Xu LIST_INSERT_HEAD(TIDHASH(td->td_tid), td, td_hash); 1197cf7d9a8cSDavid Xu rw_wunlock(&tidhash_lock); 1198cf7d9a8cSDavid Xu } 1199cf7d9a8cSDavid Xu 1200cf7d9a8cSDavid Xu void 1201cf7d9a8cSDavid Xu tidhash_remove(struct thread *td) 1202cf7d9a8cSDavid Xu { 1203cf7d9a8cSDavid Xu rw_wlock(&tidhash_lock); 1204cf7d9a8cSDavid Xu LIST_REMOVE(td, td_hash); 1205cf7d9a8cSDavid Xu rw_wunlock(&tidhash_lock); 1206cf7d9a8cSDavid Xu } 1207