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" 303d06b4b3SAttilio Rao 31677b542eSDavid E. O'Brien #include <sys/cdefs.h> 32677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 33677b542eSDavid E. O'Brien 3444990b8cSJulian Elischer #include <sys/param.h> 3544990b8cSJulian Elischer #include <sys/systm.h> 3644990b8cSJulian Elischer #include <sys/kernel.h> 3744990b8cSJulian Elischer #include <sys/lock.h> 3844990b8cSJulian Elischer #include <sys/mutex.h> 3944990b8cSJulian Elischer #include <sys/proc.h> 40e170bfdaSDavid Xu #include <sys/resourcevar.h> 4194e0a4cdSJulian Elischer #include <sys/smp.h> 4244990b8cSJulian Elischer #include <sys/sysctl.h> 43de028f5aSJeff Roberson #include <sys/sched.h> 4444f3b092SJohn Baldwin #include <sys/sleepqueue.h> 45ace8398dSJeff Roberson #include <sys/selinfo.h> 46961a7b24SJohn Baldwin #include <sys/turnstile.h> 4744990b8cSJulian Elischer #include <sys/ktr.h> 48bc8e6d81SDavid Xu #include <sys/umtx.h> 49d7f687fcSJeff Roberson #include <sys/cpuset.h> 5044990b8cSJulian Elischer 51911b84b0SRobert Watson #include <security/audit/audit.h> 52911b84b0SRobert Watson 5344990b8cSJulian Elischer #include <vm/vm.h> 5449a2507bSAlan Cox #include <vm/vm_extern.h> 5544990b8cSJulian Elischer #include <vm/uma.h> 56b209f889SRandall Stewart #include <sys/eventhandler.h> 5702fb42b0SPeter Wemm 588460a577SJohn Birrell /* 598460a577SJohn Birrell * thread related storage. 608460a577SJohn Birrell */ 6144990b8cSJulian Elischer static uma_zone_t thread_zone; 6244990b8cSJulian Elischer 6344990b8cSJulian Elischer SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0, "thread allocation"); 64fdc5ecd2SDavid Xu 65345ad866SJulian Elischer int max_threads_per_proc = 1500; 66fdc5ecd2SDavid Xu SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW, 674f0db5e0SJulian Elischer &max_threads_per_proc, 0, "Limit on threads per proc"); 684f0db5e0SJulian Elischer 69345ad866SJulian Elischer int max_threads_hits; 700252d203SDavid Xu SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD, 710252d203SDavid Xu &max_threads_hits, 0, ""); 720252d203SDavid Xu 735215b187SJeff Roberson TAILQ_HEAD(, thread) zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads); 74c8790f5dSAttilio Rao static struct mtx zombie_lock; 75a54e85fdSJeff Roberson MTX_SYSINIT(zombie_lock, &zombie_lock, "zombie lock", MTX_SPIN); 7644990b8cSJulian Elischer 77ff8fbcffSJeff Roberson static void thread_zombie(struct thread *); 78ff8fbcffSJeff Roberson 79fdcac928SMarcel Moolenaar struct mtx tid_lock; 801ea7a6f8SPoul-Henning Kamp static struct unrhdr *tid_unrhdr; 81fdcac928SMarcel Moolenaar 82fdcac928SMarcel Moolenaar /* 83696058c3SJulian Elischer * Prepare a thread for use. 8444990b8cSJulian Elischer */ 85b23f72e9SBrian Feldman static int 86b23f72e9SBrian Feldman thread_ctor(void *mem, int size, void *arg, int flags) 8744990b8cSJulian Elischer { 8844990b8cSJulian Elischer struct thread *td; 8944990b8cSJulian Elischer 9044990b8cSJulian Elischer td = (struct thread *)mem; 9171fad9fdSJulian Elischer td->td_state = TDS_INACTIVE; 92060563ecSJulian Elischer td->td_oncpu = NOCPU; 936c27c603SJuli Mallett 94773eff9dSPoul-Henning Kamp td->td_tid = alloc_unr(tid_unrhdr); 95f9bb7538SMohan Srinivasan td->td_syscalls = 0; 96773eff9dSPoul-Henning Kamp 976c27c603SJuli Mallett /* 986c27c603SJuli Mallett * Note that td_critnest begins life as 1 because the thread is not 996c27c603SJuli Mallett * running and is thereby implicitly waiting to be on the receiving 100a54e85fdSJeff Roberson * end of a context switch. 1016c27c603SJuli Mallett */ 102139b7550SJohn Baldwin td->td_critnest = 1; 103b209f889SRandall Stewart EVENTHANDLER_INVOKE(thread_ctor, td); 104911b84b0SRobert Watson #ifdef AUDIT 105911b84b0SRobert Watson audit_thread_alloc(td); 106911b84b0SRobert Watson #endif 107d10183d9SDavid Xu umtx_thread_alloc(td); 108b23f72e9SBrian Feldman return (0); 10944990b8cSJulian Elischer } 11044990b8cSJulian Elischer 11144990b8cSJulian Elischer /* 11244990b8cSJulian Elischer * Reclaim a thread after use. 11344990b8cSJulian Elischer */ 11444990b8cSJulian Elischer static void 11544990b8cSJulian Elischer thread_dtor(void *mem, int size, void *arg) 11644990b8cSJulian Elischer { 11744990b8cSJulian Elischer struct thread *td; 11844990b8cSJulian Elischer 11944990b8cSJulian Elischer td = (struct thread *)mem; 12044990b8cSJulian Elischer 12144990b8cSJulian Elischer #ifdef INVARIANTS 12244990b8cSJulian Elischer /* Verify that this thread is in a safe state to free. */ 12344990b8cSJulian Elischer switch (td->td_state) { 12471fad9fdSJulian Elischer case TDS_INHIBITED: 12571fad9fdSJulian Elischer case TDS_RUNNING: 12671fad9fdSJulian Elischer case TDS_CAN_RUN: 12744990b8cSJulian Elischer case TDS_RUNQ: 12844990b8cSJulian Elischer /* 12944990b8cSJulian Elischer * We must never unlink a thread that is in one of 13044990b8cSJulian Elischer * these states, because it is currently active. 13144990b8cSJulian Elischer */ 13244990b8cSJulian Elischer panic("bad state for thread unlinking"); 13344990b8cSJulian Elischer /* NOTREACHED */ 13471fad9fdSJulian Elischer case TDS_INACTIVE: 13544990b8cSJulian Elischer break; 13644990b8cSJulian Elischer default: 13744990b8cSJulian Elischer panic("bad thread state"); 13844990b8cSJulian Elischer /* NOTREACHED */ 13944990b8cSJulian Elischer } 14044990b8cSJulian Elischer #endif 1416e8525ceSRobert Watson #ifdef AUDIT 1426e8525ceSRobert Watson audit_thread_free(td); 1436e8525ceSRobert Watson #endif 1441ba4a712SPawel Jakub Dawidek /* Free all OSD associated to this thread. */ 1451ba4a712SPawel Jakub Dawidek osd_thread_exit(td); 1461ba4a712SPawel Jakub Dawidek 147b209f889SRandall Stewart EVENTHANDLER_INVOKE(thread_dtor, td); 148773eff9dSPoul-Henning Kamp free_unr(tid_unrhdr, td->td_tid); 14944990b8cSJulian Elischer } 15044990b8cSJulian Elischer 15144990b8cSJulian Elischer /* 15244990b8cSJulian Elischer * Initialize type-stable parts of a thread (when newly created). 15344990b8cSJulian Elischer */ 154b23f72e9SBrian Feldman static int 155b23f72e9SBrian Feldman thread_init(void *mem, int size, int flags) 15644990b8cSJulian Elischer { 15744990b8cSJulian Elischer struct thread *td; 15844990b8cSJulian Elischer 15944990b8cSJulian Elischer td = (struct thread *)mem; 160247aba24SMarcel Moolenaar 16144f3b092SJohn Baldwin td->td_sleepqueue = sleepq_alloc(); 162961a7b24SJohn Baldwin td->td_turnstile = turnstile_alloc(); 163b209f889SRandall Stewart EVENTHANDLER_INVOKE(thread_init, td); 164de028f5aSJeff Roberson td->td_sched = (struct td_sched *)&td[1]; 165d10183d9SDavid Xu umtx_thread_init(td); 16689b57fcfSKonstantin Belousov td->td_kstack = 0; 167b23f72e9SBrian Feldman return (0); 16844990b8cSJulian Elischer } 16944990b8cSJulian Elischer 17044990b8cSJulian Elischer /* 17144990b8cSJulian Elischer * Tear down type-stable parts of a thread (just before being discarded). 17244990b8cSJulian Elischer */ 17344990b8cSJulian Elischer static void 17444990b8cSJulian Elischer thread_fini(void *mem, int size) 17544990b8cSJulian Elischer { 17644990b8cSJulian Elischer struct thread *td; 17744990b8cSJulian Elischer 17844990b8cSJulian Elischer td = (struct thread *)mem; 179b209f889SRandall Stewart EVENTHANDLER_INVOKE(thread_fini, td); 180961a7b24SJohn Baldwin turnstile_free(td->td_turnstile); 18144f3b092SJohn Baldwin sleepq_free(td->td_sleepqueue); 182d10183d9SDavid Xu umtx_thread_fini(td); 183ace8398dSJeff Roberson seltdfini(td); 18444990b8cSJulian Elischer } 1855215b187SJeff Roberson 1865c8329edSJulian Elischer /* 1875215b187SJeff Roberson * For a newly created process, 1885215b187SJeff Roberson * link up all the structures and its initial threads etc. 189ed062c8dSJulian Elischer * called from: 190ed062c8dSJulian Elischer * {arch}/{arch}/machdep.c ia64_init(), init386() etc. 191ed062c8dSJulian Elischer * proc_dtor() (should go away) 192ed062c8dSJulian Elischer * proc_init() 1935c8329edSJulian Elischer */ 1945c8329edSJulian Elischer void 19589b57fcfSKonstantin Belousov proc_linkup0(struct proc *p, struct thread *td) 19689b57fcfSKonstantin Belousov { 19789b57fcfSKonstantin Belousov TAILQ_INIT(&p->p_threads); /* all threads in proc */ 19889b57fcfSKonstantin Belousov proc_linkup(p, td); 19989b57fcfSKonstantin Belousov } 20089b57fcfSKonstantin Belousov 20189b57fcfSKonstantin Belousov void 2028460a577SJohn Birrell proc_linkup(struct proc *p, struct thread *td) 2035c8329edSJulian Elischer { 204a54e85fdSJeff Roberson 2059104847fSDavid Xu sigqueue_init(&p->p_sigqueue, p); 206ebceaf6dSDavid Xu p->p_ksi = ksiginfo_alloc(1); 207ebceaf6dSDavid Xu if (p->p_ksi != NULL) { 2085c474517SDavid Xu /* XXX p_ksi may be null if ksiginfo zone is not ready */ 209ebceaf6dSDavid Xu p->p_ksi->ksi_flags = KSI_EXT | KSI_INS; 210ebceaf6dSDavid Xu } 211b2f92ef9SDavid Xu LIST_INIT(&p->p_mqnotifier); 2125c8329edSJulian Elischer p->p_numthreads = 0; 2138460a577SJohn Birrell thread_link(td, p); 2145c8329edSJulian Elischer } 2155c8329edSJulian Elischer 2165c8329edSJulian Elischer /* 21744990b8cSJulian Elischer * Initialize global thread allocation resources. 21844990b8cSJulian Elischer */ 21944990b8cSJulian Elischer void 22044990b8cSJulian Elischer threadinit(void) 22144990b8cSJulian Elischer { 22244990b8cSJulian Elischer 2231ea7a6f8SPoul-Henning Kamp mtx_init(&tid_lock, "TID lock", NULL, MTX_DEF); 2246829a5c5SJulian Elischer /* leave one number for thread0 */ 2256829a5c5SJulian Elischer tid_unrhdr = new_unrhdr(PID_MAX + 2, INT_MAX, &tid_lock); 2261ea7a6f8SPoul-Henning Kamp 227de028f5aSJeff Roberson thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(), 22844990b8cSJulian Elischer thread_ctor, thread_dtor, thread_init, thread_fini, 2294649e92bSJohn Baldwin 16 - 1, 0); 23044990b8cSJulian Elischer } 23144990b8cSJulian Elischer 23244990b8cSJulian Elischer /* 233ff8fbcffSJeff Roberson * Place an unused thread on the zombie list. 234ad1e7d28SJulian Elischer * Use the slpq as that must be unused by now. 23544990b8cSJulian Elischer */ 23644990b8cSJulian Elischer void 237ff8fbcffSJeff Roberson thread_zombie(struct thread *td) 23844990b8cSJulian Elischer { 239a54e85fdSJeff Roberson mtx_lock_spin(&zombie_lock); 240ad1e7d28SJulian Elischer TAILQ_INSERT_HEAD(&zombie_threads, td, td_slpq); 241a54e85fdSJeff Roberson mtx_unlock_spin(&zombie_lock); 24244990b8cSJulian Elischer } 24344990b8cSJulian Elischer 2445c8329edSJulian Elischer /* 245ff8fbcffSJeff Roberson * Release a thread that has exited after cpu_throw(). 246ff8fbcffSJeff Roberson */ 247ff8fbcffSJeff Roberson void 248ff8fbcffSJeff Roberson thread_stash(struct thread *td) 249ff8fbcffSJeff Roberson { 250ff8fbcffSJeff Roberson atomic_subtract_rel_int(&td->td_proc->p_exitthreads, 1); 251ff8fbcffSJeff Roberson thread_zombie(td); 252ff8fbcffSJeff Roberson } 253ff8fbcffSJeff Roberson 254ff8fbcffSJeff Roberson /* 2556617724cSJeff Roberson * Reap zombie resources. 25644990b8cSJulian Elischer */ 25744990b8cSJulian Elischer void 25844990b8cSJulian Elischer thread_reap(void) 25944990b8cSJulian Elischer { 2605c8329edSJulian Elischer struct thread *td_first, *td_next; 26144990b8cSJulian Elischer 26244990b8cSJulian Elischer /* 2635215b187SJeff Roberson * Don't even bother to lock if none at this instant, 2645215b187SJeff Roberson * we really don't care about the next instant.. 26544990b8cSJulian Elischer */ 2668460a577SJohn Birrell if (!TAILQ_EMPTY(&zombie_threads)) { 267a54e85fdSJeff Roberson mtx_lock_spin(&zombie_lock); 2685c8329edSJulian Elischer td_first = TAILQ_FIRST(&zombie_threads); 2695c8329edSJulian Elischer if (td_first) 2705c8329edSJulian Elischer TAILQ_INIT(&zombie_threads); 271a54e85fdSJeff Roberson mtx_unlock_spin(&zombie_lock); 2725c8329edSJulian Elischer while (td_first) { 273ad1e7d28SJulian Elischer td_next = TAILQ_NEXT(td_first, td_slpq); 2745215b187SJeff Roberson if (td_first->td_ucred) 2755215b187SJeff Roberson crfree(td_first->td_ucred); 2765c8329edSJulian Elischer thread_free(td_first); 2775c8329edSJulian Elischer td_first = td_next; 27844990b8cSJulian Elischer } 27944990b8cSJulian Elischer } 280ed062c8dSJulian Elischer } 28144990b8cSJulian Elischer 2824f0db5e0SJulian Elischer /* 28344990b8cSJulian Elischer * Allocate a thread. 28444990b8cSJulian Elischer */ 28544990b8cSJulian Elischer struct thread * 2868a945d10SKonstantin Belousov thread_alloc(int pages) 28744990b8cSJulian Elischer { 28889b57fcfSKonstantin Belousov struct thread *td; 2898460a577SJohn Birrell 29044990b8cSJulian Elischer thread_reap(); /* check if any zombies to get */ 29189b57fcfSKonstantin Belousov 29289b57fcfSKonstantin Belousov td = (struct thread *)uma_zalloc(thread_zone, M_WAITOK); 29389b57fcfSKonstantin Belousov KASSERT(td->td_kstack == 0, ("thread_alloc got thread with kstack")); 2948a945d10SKonstantin Belousov if (!vm_thread_new(td, pages)) { 29589b57fcfSKonstantin Belousov uma_zfree(thread_zone, td); 29689b57fcfSKonstantin Belousov return (NULL); 29789b57fcfSKonstantin Belousov } 2980c3967e7SMarcel Moolenaar cpu_thread_alloc(td); 29989b57fcfSKonstantin Belousov return (td); 30044990b8cSJulian Elischer } 30144990b8cSJulian Elischer 3028a945d10SKonstantin Belousov int 3038a945d10SKonstantin Belousov thread_alloc_stack(struct thread *td, int pages) 3048a945d10SKonstantin Belousov { 3058a945d10SKonstantin Belousov 3068a945d10SKonstantin Belousov KASSERT(td->td_kstack == 0, 3078a945d10SKonstantin Belousov ("thread_alloc_stack called on a thread with kstack")); 3088a945d10SKonstantin Belousov if (!vm_thread_new(td, pages)) 3098a945d10SKonstantin Belousov return (0); 3108a945d10SKonstantin Belousov cpu_thread_alloc(td); 3118a945d10SKonstantin Belousov return (1); 3128a945d10SKonstantin Belousov } 3134f0db5e0SJulian Elischer 3144f0db5e0SJulian Elischer /* 31544990b8cSJulian Elischer * Deallocate a thread. 31644990b8cSJulian Elischer */ 31744990b8cSJulian Elischer void 31844990b8cSJulian Elischer thread_free(struct thread *td) 31944990b8cSJulian Elischer { 3202e6b8de4SJeff Roberson 3212e6b8de4SJeff Roberson lock_profile_thread_exit(td); 32245aea8deSJeff Roberson if (td->td_cpuset) 323d7f687fcSJeff Roberson cpuset_rel(td->td_cpuset); 324d7f687fcSJeff Roberson td->td_cpuset = NULL; 3250c3967e7SMarcel Moolenaar cpu_thread_free(td); 32689b57fcfSKonstantin Belousov if (td->td_kstack != 0) 32789b57fcfSKonstantin Belousov vm_thread_dispose(td); 32844990b8cSJulian Elischer uma_zfree(thread_zone, td); 32944990b8cSJulian Elischer } 33044990b8cSJulian Elischer 33144990b8cSJulian Elischer /* 33244990b8cSJulian Elischer * Discard the current thread and exit from its context. 33394e0a4cdSJulian Elischer * Always called with scheduler locked. 33444990b8cSJulian Elischer * 33544990b8cSJulian Elischer * Because we can't free a thread while we're operating under its context, 336696058c3SJulian Elischer * push the current thread into our CPU's deadthread holder. This means 337696058c3SJulian Elischer * we needn't worry about someone else grabbing our context before we 3386617724cSJeff Roberson * do a cpu_throw(). 33944990b8cSJulian Elischer */ 34044990b8cSJulian Elischer void 34144990b8cSJulian Elischer thread_exit(void) 34244990b8cSJulian Elischer { 343e170bfdaSDavid Xu uint64_t new_switchtime; 34444990b8cSJulian Elischer struct thread *td; 3451c4bcd05SJeff Roberson struct thread *td2; 34644990b8cSJulian Elischer struct proc *p; 3477847a9daSJohn Baldwin int wakeup_swapper; 34844990b8cSJulian Elischer 34944990b8cSJulian Elischer td = curthread; 35044990b8cSJulian Elischer p = td->td_proc; 35144990b8cSJulian Elischer 352a54e85fdSJeff Roberson PROC_SLOCK_ASSERT(p, MA_OWNED); 353ed062c8dSJulian Elischer mtx_assert(&Giant, MA_NOTOWNED); 354a54e85fdSJeff Roberson 35544990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 356ed062c8dSJulian Elischer KASSERT(p != NULL, ("thread exiting without a process")); 357cc701b73SRobert Watson CTR3(KTR_PROC, "thread_exit: thread %p (pid %ld, %s)", td, 358e01eafefSJulian Elischer (long)p->p_pid, td->td_name); 3599104847fSDavid Xu KASSERT(TAILQ_EMPTY(&td->td_sigqueue.sq_list), ("signal pending")); 36044990b8cSJulian Elischer 36189964dd2SRobert Watson #ifdef AUDIT 36289964dd2SRobert Watson AUDIT_SYSCALL_EXIT(0, td); 36389964dd2SRobert Watson #endif 364d10183d9SDavid Xu umtx_thread_exit(td); 365ed062c8dSJulian Elischer /* 366ed062c8dSJulian Elischer * drop FPU & debug register state storage, or any other 367ed062c8dSJulian Elischer * architecture specific resources that 368ed062c8dSJulian Elischer * would not be on a new untouched process. 369ed062c8dSJulian Elischer */ 37044990b8cSJulian Elischer cpu_thread_exit(td); /* XXXSMP */ 37144990b8cSJulian Elischer 372e170bfdaSDavid Xu /* Do the same timestamp bookkeeping that mi_switch() would do. */ 373e170bfdaSDavid Xu new_switchtime = cpu_ticks(); 374e170bfdaSDavid Xu p->p_rux.rux_runtime += (new_switchtime - PCPU_GET(switchtime)); 375e170bfdaSDavid Xu PCPU_SET(switchtime, new_switchtime); 376e170bfdaSDavid Xu PCPU_SET(switchticks, ticks); 377b4b70819SAttilio Rao PCPU_INC(cnt.v_swtch); 378a140976eSAttilio Rao /* Save our resource usage in our process. */ 379a140976eSAttilio Rao td->td_ru.ru_nvcsw++; 380a140976eSAttilio Rao rucollect(&p->p_ru, &td->td_ru); 381ed062c8dSJulian Elischer /* 3821faf202eSJulian Elischer * The last thread is left attached to the process 3831faf202eSJulian Elischer * So that the whole bundle gets recycled. Skip 384ed062c8dSJulian Elischer * all this stuff if we never had threads. 385ed062c8dSJulian Elischer * EXIT clears all sign of other threads when 386ed062c8dSJulian Elischer * it goes to single threading, so the last thread always 387ed062c8dSJulian Elischer * takes the short path. 3881faf202eSJulian Elischer */ 389ed062c8dSJulian Elischer if (p->p_flag & P_HADTHREADS) { 3901faf202eSJulian Elischer if (p->p_numthreads > 1) { 391d3a0bd78SJulian Elischer thread_unlink(td); 3921c4bcd05SJeff Roberson td2 = FIRST_THREAD_IN_PROC(p); 3931c4bcd05SJeff Roberson sched_exit_thread(td2, td); 394ed062c8dSJulian Elischer 395ed062c8dSJulian Elischer /* 39644990b8cSJulian Elischer * The test below is NOT true if we are the 3971faf202eSJulian Elischer * sole exiting thread. P_STOPPED_SNGL is unset 39844990b8cSJulian Elischer * in exit1() after it is the only survivor. 39944990b8cSJulian Elischer */ 4001279572aSDavid Xu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 40144990b8cSJulian Elischer if (p->p_numthreads == p->p_suspcount) { 402a54e85fdSJeff Roberson thread_lock(p->p_singlethread); 4037847a9daSJohn Baldwin wakeup_swapper = thread_unsuspend_one( 4047847a9daSJohn Baldwin p->p_singlethread); 405a54e85fdSJeff Roberson thread_unlock(p->p_singlethread); 4067847a9daSJohn Baldwin if (wakeup_swapper) 4077847a9daSJohn Baldwin kick_proc0(); 40844990b8cSJulian Elischer } 40944990b8cSJulian Elischer } 41048bfcdddSJulian Elischer 411ff8fbcffSJeff Roberson atomic_add_int(&td->td_proc->p_exitthreads, 1); 412696058c3SJulian Elischer PCPU_SET(deadthread, td); 4131faf202eSJulian Elischer } else { 414ed062c8dSJulian Elischer /* 415ed062c8dSJulian Elischer * The last thread is exiting.. but not through exit() 416ed062c8dSJulian Elischer */ 417ed062c8dSJulian Elischer panic ("thread_exit: Last thread exiting on its own"); 418ed062c8dSJulian Elischer } 4191faf202eSJulian Elischer } 420a54e85fdSJeff Roberson PROC_UNLOCK(p); 421a54e85fdSJeff Roberson thread_lock(td); 422a140976eSAttilio Rao /* Save our tick information with both the thread and proc locked */ 423a54e85fdSJeff Roberson ruxagg(&p->p_rux, td); 424a54e85fdSJeff Roberson PROC_SUNLOCK(p); 425dcc9954eSJulian Elischer td->td_state = TDS_INACTIVE; 4263d06b4b3SAttilio Rao #ifdef WITNESS 4273d06b4b3SAttilio Rao witness_thread_exit(td); 4283d06b4b3SAttilio Rao #endif 429732d9528SJulian Elischer CTR1(KTR_PROC, "thread_exit: cpu_throw() thread %p", td); 430a54e85fdSJeff Roberson sched_throw(td); 431cc66ebe2SPeter Wemm panic("I'm a teapot!"); 43244990b8cSJulian Elischer /* NOTREACHED */ 43344990b8cSJulian Elischer } 43444990b8cSJulian Elischer 43544990b8cSJulian Elischer /* 436696058c3SJulian Elischer * Do any thread specific cleanups that may be needed in wait() 43737814395SPeter Wemm * called with Giant, proc and schedlock not held. 438696058c3SJulian Elischer */ 439696058c3SJulian Elischer void 440696058c3SJulian Elischer thread_wait(struct proc *p) 441696058c3SJulian Elischer { 442696058c3SJulian Elischer struct thread *td; 443696058c3SJulian Elischer 44437814395SPeter Wemm mtx_assert(&Giant, MA_NOTOWNED); 44585495c72SJens Schweikhardt KASSERT((p->p_numthreads == 1), ("Multiple threads in wait1()")); 446ff8fbcffSJeff Roberson td = FIRST_THREAD_IN_PROC(p); 447ff8fbcffSJeff Roberson /* Lock the last thread so we spin until it exits cpu_throw(). */ 448ff8fbcffSJeff Roberson thread_lock(td); 449ff8fbcffSJeff Roberson thread_unlock(td); 450ff8fbcffSJeff Roberson /* Wait for any remaining threads to exit cpu_throw(). */ 451ff8fbcffSJeff Roberson while (p->p_exitthreads) 452ff8fbcffSJeff Roberson sched_relinquish(curthread); 4532e6b8de4SJeff Roberson lock_profile_thread_exit(td); 454d7f687fcSJeff Roberson cpuset_rel(td->td_cpuset); 455d7f687fcSJeff Roberson td->td_cpuset = NULL; 456696058c3SJulian Elischer cpu_thread_clean(td); 457ed062c8dSJulian Elischer crfree(td->td_ucred); 458696058c3SJulian Elischer thread_reap(); /* check for zombie threads etc. */ 459696058c3SJulian Elischer } 460696058c3SJulian Elischer 461696058c3SJulian Elischer /* 46244990b8cSJulian Elischer * Link a thread to a process. 4631faf202eSJulian Elischer * set up anything that needs to be initialized for it to 4641faf202eSJulian Elischer * be used by the process. 46544990b8cSJulian Elischer */ 46644990b8cSJulian Elischer void 4678460a577SJohn Birrell thread_link(struct thread *td, struct proc *p) 46844990b8cSJulian Elischer { 46944990b8cSJulian Elischer 470a54e85fdSJeff Roberson /* 471a54e85fdSJeff Roberson * XXX This can't be enabled because it's called for proc0 before 472374ae2a3SJeff Roberson * its lock has been created. 473374ae2a3SJeff Roberson * PROC_LOCK_ASSERT(p, MA_OWNED); 474a54e85fdSJeff Roberson */ 47571fad9fdSJulian Elischer td->td_state = TDS_INACTIVE; 47644990b8cSJulian Elischer td->td_proc = p; 477b61ce5b0SJeff Roberson td->td_flags = TDF_INMEM; 47844990b8cSJulian Elischer 4791faf202eSJulian Elischer LIST_INIT(&td->td_contested); 480eea4f254SJeff Roberson LIST_INIT(&td->td_lprof[0]); 481eea4f254SJeff Roberson LIST_INIT(&td->td_lprof[1]); 4829104847fSDavid Xu sigqueue_init(&td->td_sigqueue, p); 483c06eb4e2SSam Leffler callout_init(&td->td_slpcallout, CALLOUT_MPSAFE); 48444990b8cSJulian Elischer TAILQ_INSERT_HEAD(&p->p_threads, td, td_plist); 48544990b8cSJulian Elischer p->p_numthreads++; 48644990b8cSJulian Elischer } 48744990b8cSJulian Elischer 488ed062c8dSJulian Elischer /* 489e5bedcefSJulian Elischer * Convert a process with one thread to an unthreaded process. 490e5bedcefSJulian Elischer */ 491e5bedcefSJulian Elischer void 492e5bedcefSJulian Elischer thread_unthread(struct thread *td) 493e5bedcefSJulian Elischer { 494e5bedcefSJulian Elischer struct proc *p = td->td_proc; 495e5bedcefSJulian Elischer 496e5bedcefSJulian Elischer KASSERT((p->p_numthreads == 1), ("Unthreading with >1 threads")); 4978460a577SJohn Birrell p->p_flag &= ~P_HADTHREADS; 498e5bedcefSJulian Elischer } 499e5bedcefSJulian Elischer 500e5bedcefSJulian Elischer /* 501ed062c8dSJulian Elischer * Called from: 502ed062c8dSJulian Elischer * thread_exit() 503ed062c8dSJulian Elischer */ 504d3a0bd78SJulian Elischer void 505d3a0bd78SJulian Elischer thread_unlink(struct thread *td) 506d3a0bd78SJulian Elischer { 507d3a0bd78SJulian Elischer struct proc *p = td->td_proc; 508d3a0bd78SJulian Elischer 509374ae2a3SJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 510d3a0bd78SJulian Elischer TAILQ_REMOVE(&p->p_threads, td, td_plist); 511d3a0bd78SJulian Elischer p->p_numthreads--; 512d3a0bd78SJulian Elischer /* could clear a few other things here */ 5138460a577SJohn Birrell /* Must NOT clear links to proc! */ 5145c8329edSJulian Elischer } 5155c8329edSJulian Elischer 51679799053SKonstantin Belousov static int 51779799053SKonstantin Belousov calc_remaining(struct proc *p, int mode) 51879799053SKonstantin Belousov { 51979799053SKonstantin Belousov int remaining; 52079799053SKonstantin Belousov 52179799053SKonstantin Belousov if (mode == SINGLE_EXIT) 52279799053SKonstantin Belousov remaining = p->p_numthreads; 52379799053SKonstantin Belousov else if (mode == SINGLE_BOUNDARY) 52479799053SKonstantin Belousov remaining = p->p_numthreads - p->p_boundary_count; 52579799053SKonstantin Belousov else if (mode == SINGLE_NO_EXIT) 52679799053SKonstantin Belousov remaining = p->p_numthreads - p->p_suspcount; 52779799053SKonstantin Belousov else 52879799053SKonstantin Belousov panic("calc_remaining: wrong mode %d", mode); 52979799053SKonstantin Belousov return (remaining); 53079799053SKonstantin Belousov } 53179799053SKonstantin Belousov 5325215b187SJeff Roberson /* 53344990b8cSJulian Elischer * Enforce single-threading. 53444990b8cSJulian Elischer * 53544990b8cSJulian Elischer * Returns 1 if the caller must abort (another thread is waiting to 53644990b8cSJulian Elischer * exit the process or similar). Process is locked! 53744990b8cSJulian Elischer * Returns 0 when you are successfully the only thread running. 53844990b8cSJulian Elischer * A process has successfully single threaded in the suspend mode when 53944990b8cSJulian Elischer * There are no threads in user mode. Threads in the kernel must be 54044990b8cSJulian Elischer * allowed to continue until they get to the user boundary. They may even 54144990b8cSJulian Elischer * copy out their return values and data before suspending. They may however be 542e2668f55SMaxim Konovalov * accelerated in reaching the user boundary as we will wake up 54344990b8cSJulian Elischer * any sleeping threads that are interruptable. (PCATCH). 54444990b8cSJulian Elischer */ 54544990b8cSJulian Elischer int 546906ac69dSDavid Xu thread_single(int mode) 54744990b8cSJulian Elischer { 54844990b8cSJulian Elischer struct thread *td; 54944990b8cSJulian Elischer struct thread *td2; 55044990b8cSJulian Elischer struct proc *p; 551da7bbd2cSJohn Baldwin int remaining, wakeup_swapper; 55244990b8cSJulian Elischer 55344990b8cSJulian Elischer td = curthread; 55444990b8cSJulian Elischer p = td->td_proc; 55537814395SPeter Wemm mtx_assert(&Giant, MA_NOTOWNED); 55644990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 55744990b8cSJulian Elischer KASSERT((td != NULL), ("curthread is NULL")); 55844990b8cSJulian Elischer 559ed062c8dSJulian Elischer if ((p->p_flag & P_HADTHREADS) == 0) 56044990b8cSJulian Elischer return (0); 56144990b8cSJulian Elischer 562e3b9bf71SJulian Elischer /* Is someone already single threading? */ 563906ac69dSDavid Xu if (p->p_singlethread != NULL && p->p_singlethread != td) 56444990b8cSJulian Elischer return (1); 56544990b8cSJulian Elischer 566906ac69dSDavid Xu if (mode == SINGLE_EXIT) { 567906ac69dSDavid Xu p->p_flag |= P_SINGLE_EXIT; 568906ac69dSDavid Xu p->p_flag &= ~P_SINGLE_BOUNDARY; 569906ac69dSDavid Xu } else { 570906ac69dSDavid Xu p->p_flag &= ~P_SINGLE_EXIT; 571906ac69dSDavid Xu if (mode == SINGLE_BOUNDARY) 572906ac69dSDavid Xu p->p_flag |= P_SINGLE_BOUNDARY; 573906ac69dSDavid Xu else 574906ac69dSDavid Xu p->p_flag &= ~P_SINGLE_BOUNDARY; 575906ac69dSDavid Xu } 5761279572aSDavid Xu p->p_flag |= P_STOPPED_SINGLE; 5777b4a950aSDavid Xu PROC_SLOCK(p); 578112afcb2SJohn Baldwin p->p_singlethread = td; 57979799053SKonstantin Belousov remaining = calc_remaining(p, mode); 580ec008e96SDavid Xu while (remaining != 1) { 581bf1a3220SDavid Xu if (P_SHOULDSTOP(p) != P_STOPPED_SINGLE) 582bf1a3220SDavid Xu goto stopme; 583da7bbd2cSJohn Baldwin wakeup_swapper = 0; 58444990b8cSJulian Elischer FOREACH_THREAD_IN_PROC(p, td2) { 58544990b8cSJulian Elischer if (td2 == td) 58644990b8cSJulian Elischer continue; 587a54e85fdSJeff Roberson thread_lock(td2); 588b7edba77SJeff Roberson td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK; 58971fad9fdSJulian Elischer if (TD_IS_INHIBITED(td2)) { 590906ac69dSDavid Xu switch (mode) { 591906ac69dSDavid Xu case SINGLE_EXIT: 592906ac69dSDavid Xu if (TD_IS_SUSPENDED(td2)) 5937847a9daSJohn Baldwin wakeup_swapper |= 59471fad9fdSJulian Elischer thread_unsuspend_one(td2); 59533862f40SDavid Xu if (TD_ON_SLEEPQ(td2) && 596906ac69dSDavid Xu (td2->td_flags & TDF_SINTR)) 5977847a9daSJohn Baldwin wakeup_swapper |= 59894f0972bSDavid Xu sleepq_abort(td2, EINTR); 599906ac69dSDavid Xu break; 600906ac69dSDavid Xu case SINGLE_BOUNDARY: 601ffdc5a34SDavid Xu if (TD_IS_SUSPENDED(td2) && 602ffdc5a34SDavid Xu !(td2->td_flags & TDF_BOUNDARY)) 603ffdc5a34SDavid Xu wakeup_swapper |= 604ffdc5a34SDavid Xu thread_unsuspend_one(td2); 605ffdc5a34SDavid Xu if (TD_ON_SLEEPQ(td2) && 606ffdc5a34SDavid Xu (td2->td_flags & TDF_SINTR)) 607ffdc5a34SDavid Xu wakeup_swapper |= 608ffdc5a34SDavid Xu sleepq_abort(td2, ERESTART); 609906ac69dSDavid Xu break; 610f33a947bSKonstantin Belousov case SINGLE_NO_EXIT: 611f33a947bSKonstantin Belousov if (TD_IS_SUSPENDED(td2) && 612f33a947bSKonstantin Belousov !(td2->td_flags & TDF_BOUNDARY)) 613f33a947bSKonstantin Belousov wakeup_swapper |= 614f33a947bSKonstantin Belousov thread_unsuspend_one(td2); 615f33a947bSKonstantin Belousov if (TD_ON_SLEEPQ(td2) && 616f33a947bSKonstantin Belousov (td2->td_flags & TDF_SINTR)) 617f33a947bSKonstantin Belousov wakeup_swapper |= 618f33a947bSKonstantin Belousov sleepq_abort(td2, ERESTART); 619f33a947bSKonstantin Belousov break; 620906ac69dSDavid Xu default: 621906ac69dSDavid Xu break; 62244990b8cSJulian Elischer } 62344990b8cSJulian Elischer } 624d8267df7SDavid Xu #ifdef SMP 625d8267df7SDavid Xu else if (TD_IS_RUNNING(td2) && td != td2) { 626d8267df7SDavid Xu forward_signal(td2); 627d8267df7SDavid Xu } 628d8267df7SDavid Xu #endif 629a54e85fdSJeff Roberson thread_unlock(td2); 6309d102777SJulian Elischer } 631da7bbd2cSJohn Baldwin if (wakeup_swapper) 632da7bbd2cSJohn Baldwin kick_proc0(); 63379799053SKonstantin Belousov remaining = calc_remaining(p, mode); 634ec008e96SDavid Xu 6359d102777SJulian Elischer /* 6369d102777SJulian Elischer * Maybe we suspended some threads.. was it enough? 6379d102777SJulian Elischer */ 638ec008e96SDavid Xu if (remaining == 1) 6399d102777SJulian Elischer break; 6409d102777SJulian Elischer 641bf1a3220SDavid Xu stopme: 64244990b8cSJulian Elischer /* 64344990b8cSJulian Elischer * Wake us up when everyone else has suspended. 644e3b9bf71SJulian Elischer * In the mean time we suspend as well. 64544990b8cSJulian Elischer */ 646a54e85fdSJeff Roberson thread_suspend_switch(td); 64779799053SKonstantin Belousov remaining = calc_remaining(p, mode); 64844990b8cSJulian Elischer } 649906ac69dSDavid Xu if (mode == SINGLE_EXIT) { 65091599697SJulian Elischer /* 65191599697SJulian Elischer * We have gotten rid of all the other threads and we 65291599697SJulian Elischer * are about to either exit or exec. In either case, 65391599697SJulian Elischer * we try our utmost to revert to being a non-threaded 65491599697SJulian Elischer * process. 65591599697SJulian Elischer */ 656ed062c8dSJulian Elischer p->p_singlethread = NULL; 65764895117SDavid Xu p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT); 658e5bedcefSJulian Elischer thread_unthread(td); 65991599697SJulian Elischer } 6607b4a950aSDavid Xu PROC_SUNLOCK(p); 66144990b8cSJulian Elischer return (0); 66244990b8cSJulian Elischer } 66344990b8cSJulian Elischer 66444990b8cSJulian Elischer /* 66544990b8cSJulian Elischer * Called in from locations that can safely check to see 66644990b8cSJulian Elischer * whether we have to suspend or at least throttle for a 66744990b8cSJulian Elischer * single-thread event (e.g. fork). 66844990b8cSJulian Elischer * 66944990b8cSJulian Elischer * Such locations include userret(). 67044990b8cSJulian Elischer * If the "return_instead" argument is non zero, the thread must be able to 67144990b8cSJulian Elischer * accept 0 (caller may continue), or 1 (caller must abort) as a result. 67244990b8cSJulian Elischer * 67344990b8cSJulian Elischer * The 'return_instead' argument tells the function if it may do a 67444990b8cSJulian Elischer * thread_exit() or suspend, or whether the caller must abort and back 67544990b8cSJulian Elischer * out instead. 67644990b8cSJulian Elischer * 67744990b8cSJulian Elischer * If the thread that set the single_threading request has set the 67844990b8cSJulian Elischer * P_SINGLE_EXIT bit in the process flags then this call will never return 67944990b8cSJulian Elischer * if 'return_instead' is false, but will exit. 68044990b8cSJulian Elischer * 68144990b8cSJulian Elischer * P_SINGLE_EXIT | return_instead == 0| return_instead != 0 68244990b8cSJulian Elischer *---------------+--------------------+--------------------- 68344990b8cSJulian Elischer * 0 | returns 0 | returns 0 or 1 68444990b8cSJulian Elischer * | when ST ends | immediatly 68544990b8cSJulian Elischer *---------------+--------------------+--------------------- 68644990b8cSJulian Elischer * 1 | thread exits | returns 1 68744990b8cSJulian Elischer * | | immediatly 68844990b8cSJulian Elischer * 0 = thread_exit() or suspension ok, 68944990b8cSJulian Elischer * other = return error instead of stopping the thread. 69044990b8cSJulian Elischer * 69144990b8cSJulian Elischer * While a full suspension is under effect, even a single threading 69244990b8cSJulian Elischer * thread would be suspended if it made this call (but it shouldn't). 69344990b8cSJulian Elischer * This call should only be made from places where 69444990b8cSJulian Elischer * thread_exit() would be safe as that may be the outcome unless 69544990b8cSJulian Elischer * return_instead is set. 69644990b8cSJulian Elischer */ 69744990b8cSJulian Elischer int 69844990b8cSJulian Elischer thread_suspend_check(int return_instead) 69944990b8cSJulian Elischer { 700ecafb24bSJuli Mallett struct thread *td; 701ecafb24bSJuli Mallett struct proc *p; 7027847a9daSJohn Baldwin int wakeup_swapper; 70344990b8cSJulian Elischer 70444990b8cSJulian Elischer td = curthread; 70544990b8cSJulian Elischer p = td->td_proc; 70637814395SPeter Wemm mtx_assert(&Giant, MA_NOTOWNED); 70744990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 708cbf4e354SDavid Xu while (P_SHOULDSTOP(p) || 709904c5ec4SDavid Xu ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_SUSPEND))) { 7101279572aSDavid Xu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 71144990b8cSJulian Elischer KASSERT(p->p_singlethread != NULL, 71244990b8cSJulian Elischer ("singlethread not set")); 71344990b8cSJulian Elischer /* 714e3b9bf71SJulian Elischer * The only suspension in action is a 715e3b9bf71SJulian Elischer * single-threading. Single threader need not stop. 716b6d5995eSJulian Elischer * XXX Should be safe to access unlocked 717b6d5995eSJulian Elischer * as it can only be set to be true by us. 71844990b8cSJulian Elischer */ 719e3b9bf71SJulian Elischer if (p->p_singlethread == td) 72044990b8cSJulian Elischer return (0); /* Exempt from stopping. */ 72144990b8cSJulian Elischer } 72245a4bfa1SDavid Xu if ((p->p_flag & P_SINGLE_EXIT) && return_instead) 72394f0972bSDavid Xu return (EINTR); 72444990b8cSJulian Elischer 725906ac69dSDavid Xu /* Should we goto user boundary if we didn't come from there? */ 726906ac69dSDavid Xu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE && 727906ac69dSDavid Xu (p->p_flag & P_SINGLE_BOUNDARY) && return_instead) 72894f0972bSDavid Xu return (ERESTART); 729906ac69dSDavid Xu 7309104847fSDavid Xu /* If thread will exit, flush its pending signals */ 7319104847fSDavid Xu if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) 7329104847fSDavid Xu sigqueue_flush(&td->td_sigqueue); 7339104847fSDavid Xu 7347b4a950aSDavid Xu PROC_SLOCK(p); 735e574e444SDavid Xu thread_stopped(p); 73644990b8cSJulian Elischer /* 73744990b8cSJulian Elischer * If the process is waiting for us to exit, 73844990b8cSJulian Elischer * this thread should just suicide. 7391279572aSDavid Xu * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE. 74044990b8cSJulian Elischer */ 7417b4a950aSDavid Xu if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) 74244990b8cSJulian Elischer thread_exit(); 743a54e85fdSJeff Roberson if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 744a54e85fdSJeff Roberson if (p->p_numthreads == p->p_suspcount + 1) { 745a54e85fdSJeff Roberson thread_lock(p->p_singlethread); 7467847a9daSJohn Baldwin wakeup_swapper = 747a54e85fdSJeff Roberson thread_unsuspend_one(p->p_singlethread); 748a54e85fdSJeff Roberson thread_unlock(p->p_singlethread); 7497847a9daSJohn Baldwin if (wakeup_swapper) 7507847a9daSJohn Baldwin kick_proc0(); 751a54e85fdSJeff Roberson } 752a54e85fdSJeff Roberson } 7533f9be10eSDavid Xu PROC_UNLOCK(p); 7547b4a950aSDavid Xu thread_lock(td); 75544990b8cSJulian Elischer /* 75644990b8cSJulian Elischer * When a thread suspends, it just 757ad1e7d28SJulian Elischer * gets taken off all queues. 75844990b8cSJulian Elischer */ 75971fad9fdSJulian Elischer thread_suspend_one(td); 760906ac69dSDavid Xu if (return_instead == 0) { 761906ac69dSDavid Xu p->p_boundary_count++; 762906ac69dSDavid Xu td->td_flags |= TDF_BOUNDARY; 763cf19bf91SJulian Elischer } 7647b4a950aSDavid Xu PROC_SUNLOCK(p); 7658df78c41SJeff Roberson mi_switch(SW_INVOL | SWT_SUSPEND, NULL); 766a54e85fdSJeff Roberson if (return_instead == 0) 767906ac69dSDavid Xu td->td_flags &= ~TDF_BOUNDARY; 768a54e85fdSJeff Roberson thread_unlock(td); 76944990b8cSJulian Elischer PROC_LOCK(p); 770a54e85fdSJeff Roberson if (return_instead == 0) 771a54e85fdSJeff Roberson p->p_boundary_count--; 77244990b8cSJulian Elischer } 77344990b8cSJulian Elischer return (0); 77444990b8cSJulian Elischer } 77544990b8cSJulian Elischer 77635c32a76SDavid Xu void 777a54e85fdSJeff Roberson thread_suspend_switch(struct thread *td) 778a54e85fdSJeff Roberson { 779a54e85fdSJeff Roberson struct proc *p; 780a54e85fdSJeff Roberson 781a54e85fdSJeff Roberson p = td->td_proc; 782a54e85fdSJeff Roberson KASSERT(!TD_IS_SUSPENDED(td), ("already suspended")); 783a54e85fdSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 7847b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 785a54e85fdSJeff Roberson /* 786a54e85fdSJeff Roberson * We implement thread_suspend_one in stages here to avoid 787a54e85fdSJeff Roberson * dropping the proc lock while the thread lock is owned. 788a54e85fdSJeff Roberson */ 789a54e85fdSJeff Roberson thread_stopped(p); 790a54e85fdSJeff Roberson p->p_suspcount++; 7913f9be10eSDavid Xu PROC_UNLOCK(p); 7927b4a950aSDavid Xu thread_lock(td); 793b7edba77SJeff Roberson td->td_flags &= ~TDF_NEEDSUSPCHK; 794a54e85fdSJeff Roberson TD_SET_SUSPENDED(td); 795c5aa6b58SJeff Roberson sched_sleep(td, 0); 7967b4a950aSDavid Xu PROC_SUNLOCK(p); 797a54e85fdSJeff Roberson DROP_GIANT(); 7988df78c41SJeff Roberson mi_switch(SW_VOL | SWT_SUSPEND, NULL); 799a54e85fdSJeff Roberson thread_unlock(td); 800a54e85fdSJeff Roberson PICKUP_GIANT(); 801a54e85fdSJeff Roberson PROC_LOCK(p); 8027b4a950aSDavid Xu PROC_SLOCK(p); 803a54e85fdSJeff Roberson } 804a54e85fdSJeff Roberson 805a54e85fdSJeff Roberson void 80635c32a76SDavid Xu thread_suspend_one(struct thread *td) 80735c32a76SDavid Xu { 80835c32a76SDavid Xu struct proc *p = td->td_proc; 80935c32a76SDavid Xu 8107b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 811a54e85fdSJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 812e574e444SDavid Xu KASSERT(!TD_IS_SUSPENDED(td), ("already suspended")); 81335c32a76SDavid Xu p->p_suspcount++; 814b7edba77SJeff Roberson td->td_flags &= ~TDF_NEEDSUSPCHK; 81571fad9fdSJulian Elischer TD_SET_SUSPENDED(td); 816c5aa6b58SJeff Roberson sched_sleep(td, 0); 81735c32a76SDavid Xu } 81835c32a76SDavid Xu 8197847a9daSJohn Baldwin int 82035c32a76SDavid Xu thread_unsuspend_one(struct thread *td) 82135c32a76SDavid Xu { 82235c32a76SDavid Xu struct proc *p = td->td_proc; 82335c32a76SDavid Xu 8247b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 825a54e85fdSJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 826ad1e7d28SJulian Elischer KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended")); 82771fad9fdSJulian Elischer TD_CLR_SUSPENDED(td); 82835c32a76SDavid Xu p->p_suspcount--; 8297847a9daSJohn Baldwin return (setrunnable(td)); 83035c32a76SDavid Xu } 83135c32a76SDavid Xu 83244990b8cSJulian Elischer /* 83344990b8cSJulian Elischer * Allow all threads blocked by single threading to continue running. 83444990b8cSJulian Elischer */ 83544990b8cSJulian Elischer void 83644990b8cSJulian Elischer thread_unsuspend(struct proc *p) 83744990b8cSJulian Elischer { 83844990b8cSJulian Elischer struct thread *td; 8397847a9daSJohn Baldwin int wakeup_swapper; 84044990b8cSJulian Elischer 84144990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 8427b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 8437847a9daSJohn Baldwin wakeup_swapper = 0; 84444990b8cSJulian Elischer if (!P_SHOULDSTOP(p)) { 845ad1e7d28SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 846a54e85fdSJeff Roberson thread_lock(td); 847ad1e7d28SJulian Elischer if (TD_IS_SUSPENDED(td)) { 8487847a9daSJohn Baldwin wakeup_swapper |= thread_unsuspend_one(td); 84944990b8cSJulian Elischer } 850a54e85fdSJeff Roberson thread_unlock(td); 851ad1e7d28SJulian Elischer } 8521279572aSDavid Xu } else if ((P_SHOULDSTOP(p) == P_STOPPED_SINGLE) && 85344990b8cSJulian Elischer (p->p_numthreads == p->p_suspcount)) { 85444990b8cSJulian Elischer /* 85544990b8cSJulian Elischer * Stopping everything also did the job for the single 85644990b8cSJulian Elischer * threading request. Now we've downgraded to single-threaded, 85744990b8cSJulian Elischer * let it continue. 85844990b8cSJulian Elischer */ 859a54e85fdSJeff Roberson thread_lock(p->p_singlethread); 8607847a9daSJohn Baldwin wakeup_swapper = thread_unsuspend_one(p->p_singlethread); 861a54e85fdSJeff Roberson thread_unlock(p->p_singlethread); 86244990b8cSJulian Elischer } 8637847a9daSJohn Baldwin if (wakeup_swapper) 8647847a9daSJohn Baldwin kick_proc0(); 86544990b8cSJulian Elischer } 86644990b8cSJulian Elischer 867ed062c8dSJulian Elischer /* 868ed062c8dSJulian Elischer * End the single threading mode.. 869ed062c8dSJulian Elischer */ 87044990b8cSJulian Elischer void 87144990b8cSJulian Elischer thread_single_end(void) 87244990b8cSJulian Elischer { 87344990b8cSJulian Elischer struct thread *td; 87444990b8cSJulian Elischer struct proc *p; 8757847a9daSJohn Baldwin int wakeup_swapper; 87644990b8cSJulian Elischer 87744990b8cSJulian Elischer td = curthread; 87844990b8cSJulian Elischer p = td->td_proc; 87944990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 880906ac69dSDavid Xu p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_SINGLE_BOUNDARY); 8817b4a950aSDavid Xu PROC_SLOCK(p); 88244990b8cSJulian Elischer p->p_singlethread = NULL; 8837847a9daSJohn Baldwin wakeup_swapper = 0; 88449539972SJulian Elischer /* 8857847a9daSJohn Baldwin * If there are other threads they may now run, 88649539972SJulian Elischer * unless of course there is a blanket 'stop order' 88749539972SJulian Elischer * on the process. The single threader must be allowed 88849539972SJulian Elischer * to continue however as this is a bad place to stop. 88949539972SJulian Elischer */ 89049539972SJulian Elischer if ((p->p_numthreads != 1) && (!P_SHOULDSTOP(p))) { 891ad1e7d28SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 892a54e85fdSJeff Roberson thread_lock(td); 893ad1e7d28SJulian Elischer if (TD_IS_SUSPENDED(td)) { 8947847a9daSJohn Baldwin wakeup_swapper |= thread_unsuspend_one(td); 89544990b8cSJulian Elischer } 896a54e85fdSJeff Roberson thread_unlock(td); 89749539972SJulian Elischer } 898ad1e7d28SJulian Elischer } 8997b4a950aSDavid Xu PROC_SUNLOCK(p); 9007847a9daSJohn Baldwin if (wakeup_swapper) 9017847a9daSJohn Baldwin kick_proc0(); 90249539972SJulian Elischer } 9034fc21c09SDaniel Eischen 90444355392SDavid Xu struct thread * 90544355392SDavid Xu thread_find(struct proc *p, lwpid_t tid) 90644355392SDavid Xu { 90744355392SDavid Xu struct thread *td; 90844355392SDavid Xu 90944355392SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 91044355392SDavid Xu FOREACH_THREAD_IN_PROC(p, td) { 91144355392SDavid Xu if (td->td_tid == tid) 91244355392SDavid Xu break; 91344355392SDavid Xu } 91444355392SDavid Xu return (td); 91544355392SDavid Xu } 916