2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2001 Julian Elischer <julian@freebsd.org>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice(s), this list of conditions and the following disclaimer as
12 * the first lines of this file unmodified other than the possible
13 * addition of one or more copyright notices.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice(s), this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 #include "opt_witness.h"
32 #include "opt_hwpmc_hooks.h"
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/mutex.h>
43 #include <sys/bitstring.h>
44 #include <sys/epoch.h>
45 #include <sys/rangelock.h>
46 #include <sys/resourcevar.h>
49 #include <sys/sched.h>
50 #include <sys/sleepqueue.h>
51 #include <sys/selinfo.h>
52 #include <sys/syscallsubr.h>
53 #include <sys/dtrace_bsd.h>
54 #include <sys/sysent.h>
55 #include <sys/turnstile.h>
56 #include <sys/taskqueue.h>
58 #include <sys/rwlock.h>
59 #include <sys/umtxvar.h>
60 #include <sys/vmmeter.h>
61 #include <sys/cpuset.h>
63 #include <sys/pmckern.h>
67 #include <security/audit/audit.h>
71 #include <vm/vm_extern.h>
73 #include <vm/vm_phys.h>
74 #include <sys/eventhandler.h>
77 * Asserts below verify the stability of struct thread and struct proc
78 * layout, as exposed by KBI to modules. On head, the KBI is allowed
79 * to drift, change to the structures must be accompanied by the
82 * On the stable branches after KBI freeze, conditions must not be
83 * violated. Typically new fields are moved to the end of the
87 _Static_assert(offsetof(struct thread, td_flags) == 0xfc,
88 "struct thread KBI td_flags");
89 _Static_assert(offsetof(struct thread, td_pflags) == 0x104,
90 "struct thread KBI td_pflags");
91 _Static_assert(offsetof(struct thread, td_frame) == 0x4a0,
92 "struct thread KBI td_frame");
93 _Static_assert(offsetof(struct thread, td_emuldata) == 0x6b0,
94 "struct thread KBI td_emuldata");
95 _Static_assert(offsetof(struct proc, p_flag) == 0xb8,
96 "struct proc KBI p_flag");
97 _Static_assert(offsetof(struct proc, p_pid) == 0xc4,
98 "struct proc KBI p_pid");
99 _Static_assert(offsetof(struct proc, p_filemon) == 0x3b8,
100 "struct proc KBI p_filemon");
101 _Static_assert(offsetof(struct proc, p_comm) == 0x3d0,
102 "struct proc KBI p_comm");
103 _Static_assert(offsetof(struct proc, p_emuldata) == 0x4b8,
104 "struct proc KBI p_emuldata");
107 _Static_assert(offsetof(struct thread, td_flags) == 0x98,
108 "struct thread KBI td_flags");
109 _Static_assert(offsetof(struct thread, td_pflags) == 0xa0,
110 "struct thread KBI td_pflags");
111 _Static_assert(offsetof(struct thread, td_frame) == 0x304,
112 "struct thread KBI td_frame");
113 _Static_assert(offsetof(struct thread, td_emuldata) == 0x348,
114 "struct thread KBI td_emuldata");
115 _Static_assert(offsetof(struct proc, p_flag) == 0x6c,
116 "struct proc KBI p_flag");
117 _Static_assert(offsetof(struct proc, p_pid) == 0x78,
118 "struct proc KBI p_pid");
119 _Static_assert(offsetof(struct proc, p_filemon) == 0x268,
120 "struct proc KBI p_filemon");
121 _Static_assert(offsetof(struct proc, p_comm) == 0x27c,
122 "struct proc KBI p_comm");
123 _Static_assert(offsetof(struct proc, p_emuldata) == 0x308,
124 "struct proc KBI p_emuldata");
127 SDT_PROVIDER_DECLARE(proc);
128 SDT_PROBE_DEFINE(proc, , , lwp__exit);
131 * thread related storage.
133 static uma_zone_t thread_zone;
135 struct thread_domain_data {
136 struct thread *tdd_zombies;
138 } __aligned(CACHE_LINE_SIZE);
140 static struct thread_domain_data thread_domain_data[MAXMEMDOM];
142 static struct task thread_reap_task;
143 static struct callout thread_reap_callout;
145 static void thread_zombie(struct thread *);
146 static void thread_reap(void);
147 static void thread_reap_all(void);
148 static void thread_reap_task_cb(void *, int);
149 static void thread_reap_callout_cb(void *);
150 static int thread_unsuspend_one(struct thread *td, struct proc *p,
152 static void thread_free_batched(struct thread *td);
154 static __exclusive_cache_line struct mtx tid_lock;
155 static bitstr_t *tid_bitmap;
157 static MALLOC_DEFINE(M_TIDHASH, "tidhash", "thread hash");
159 static int maxthread;
160 SYSCTL_INT(_kern, OID_AUTO, maxthread, CTLFLAG_RDTUN,
161 &maxthread, 0, "Maximum number of threads");
163 static __exclusive_cache_line int nthreads;
165 static LIST_HEAD(tidhashhead, thread) *tidhashtbl;
166 static u_long tidhash;
167 static u_long tidhashlock;
168 static struct rwlock *tidhashtbl_lock;
169 #define TIDHASH(tid) (&tidhashtbl[(tid) & tidhash])
170 #define TIDHASHLOCK(tid) (&tidhashtbl_lock[(tid) & tidhashlock])
172 EVENTHANDLER_LIST_DEFINE(thread_ctor);
173 EVENTHANDLER_LIST_DEFINE(thread_dtor);
174 EVENTHANDLER_LIST_DEFINE(thread_init);
175 EVENTHANDLER_LIST_DEFINE(thread_fini);
178 thread_count_inc_try(void)
182 nthreads_new = atomic_fetchadd_int(&nthreads, 1) + 1;
183 if (nthreads_new >= maxthread - 100) {
184 if (priv_check_cred(curthread->td_ucred, PRIV_MAXPROC) != 0 ||
185 nthreads_new >= maxthread) {
186 atomic_subtract_int(&nthreads, 1);
194 thread_count_inc(void)
196 static struct timeval lastfail;
200 if (thread_count_inc_try()) {
205 if (thread_count_inc_try()) {
209 if (ppsratecheck(&lastfail, &curfail, 1)) {
210 printf("maxthread limit exceeded by uid %u "
211 "(pid %d); consider increasing kern.maxthread\n",
212 curthread->td_ucred->cr_ruid, curproc->p_pid);
218 thread_count_sub(int n)
221 atomic_subtract_int(&nthreads, n);
225 thread_count_dec(void)
234 static lwpid_t trytid;
239 * It is an invariant that the bitmap is big enough to hold maxthread
240 * IDs. If we got to this point there has to be at least one free.
242 if (trytid >= maxthread)
244 bit_ffc_at(tid_bitmap, trytid, maxthread, &tid);
246 KASSERT(trytid != 0, ("unexpectedly ran out of IDs"));
248 bit_ffc_at(tid_bitmap, trytid, maxthread, &tid);
249 KASSERT(tid != -1, ("unexpectedly ran out of IDs"));
251 bit_set(tid_bitmap, tid);
253 mtx_unlock(&tid_lock);
254 return (tid + NO_PID);
258 tid_free_locked(lwpid_t rtid)
262 mtx_assert(&tid_lock, MA_OWNED);
263 KASSERT(rtid >= NO_PID,
264 ("%s: invalid tid %d\n", __func__, rtid));
266 KASSERT(bit_test(tid_bitmap, tid) != 0,
267 ("thread ID %d not allocated\n", rtid));
268 bit_clear(tid_bitmap, tid);
272 tid_free(lwpid_t rtid)
276 tid_free_locked(rtid);
277 mtx_unlock(&tid_lock);
281 tid_free_batch(lwpid_t *batch, int n)
286 for (i = 0; i < n; i++) {
287 tid_free_locked(batch[i]);
289 mtx_unlock(&tid_lock);
293 * Batching for thread reapping.
301 tidbatch_prep(struct tidbatch *tb)
308 tidbatch_add(struct tidbatch *tb, struct thread *td)
311 KASSERT(tb->n < nitems(tb->tab),
312 ("%s: count too high %d", __func__, tb->n));
313 tb->tab[tb->n] = td->td_tid;
318 tidbatch_process(struct tidbatch *tb)
321 KASSERT(tb->n <= nitems(tb->tab),
322 ("%s: count too high %d", __func__, tb->n));
323 if (tb->n == nitems(tb->tab)) {
324 tid_free_batch(tb->tab, tb->n);
330 tidbatch_final(struct tidbatch *tb)
333 KASSERT(tb->n <= nitems(tb->tab),
334 ("%s: count too high %d", __func__, tb->n));
336 tid_free_batch(tb->tab, tb->n);
341 * Prepare a thread for use.
344 thread_ctor(void *mem, int size, void *arg, int flags)
348 td = (struct thread *)mem;
349 TD_SET_STATE(td, TDS_INACTIVE);
350 td->td_lastcpu = td->td_oncpu = NOCPU;
353 * Note that td_critnest begins life as 1 because the thread is not
354 * running and is thereby implicitly waiting to be on the receiving
355 * end of a context switch.
358 td->td_lend_user_pri = PRI_MAX;
360 audit_thread_alloc(td);
363 kdtrace_thread_ctor(td);
365 umtx_thread_alloc(td);
366 MPASS(td->td_sel == NULL);
371 * Reclaim a thread after use.
374 thread_dtor(void *mem, int size, void *arg)
378 td = (struct thread *)mem;
381 /* Verify that this thread is in a safe state to free. */
382 switch (TD_GET_STATE(td)) {
388 * We must never unlink a thread that is in one of
389 * these states, because it is currently active.
391 panic("bad state for thread unlinking");
396 panic("bad thread state");
401 audit_thread_free(td);
404 kdtrace_thread_dtor(td);
406 /* Free all OSD associated to this thread. */
408 td_softdep_cleanup(td);
409 MPASS(td->td_su == NULL);
414 * Initialize type-stable parts of a thread (when newly created).
417 thread_init(void *mem, int size, int flags)
421 td = (struct thread *)mem;
423 td->td_allocdomain = vm_phys_domain(vtophys(td));
424 td->td_sleepqueue = sleepq_alloc();
425 td->td_turnstile = turnstile_alloc();
427 EVENTHANDLER_DIRECT_INVOKE(thread_init, td);
428 umtx_thread_init(td);
435 * Tear down type-stable parts of a thread (just before being discarded).
438 thread_fini(void *mem, int size)
442 td = (struct thread *)mem;
443 EVENTHANDLER_DIRECT_INVOKE(thread_fini, td);
444 rlqentry_free(td->td_rlqe);
445 turnstile_free(td->td_turnstile);
446 sleepq_free(td->td_sleepqueue);
447 umtx_thread_fini(td);
448 MPASS(td->td_sel == NULL);
452 * For a newly created process,
453 * link up all the structures and its initial threads etc.
455 * {arch}/{arch}/machdep.c {arch}_init(), init386() etc.
456 * proc_dtor() (should go away)
460 proc_linkup0(struct proc *p, struct thread *td)
462 TAILQ_INIT(&p->p_threads); /* all threads in proc */
467 proc_linkup(struct proc *p, struct thread *td)
470 sigqueue_init(&p->p_sigqueue, p);
471 p->p_ksi = ksiginfo_alloc(1);
472 if (p->p_ksi != NULL) {
473 /* XXX p_ksi may be null if ksiginfo zone is not ready */
474 p->p_ksi->ksi_flags = KSI_EXT | KSI_INS;
476 LIST_INIT(&p->p_mqnotifier);
481 extern int max_threads_per_proc;
484 * Initialize global thread allocation resources.
494 * Place an upper limit on threads which can be allocated.
496 * Note that other factors may make the de facto limit much lower.
498 * Platform limits are somewhat arbitrary but deemed "more than good
499 * enough" for the foreseable future.
501 if (maxthread == 0) {
503 maxthread = MIN(maxproc * max_threads_per_proc, 1000000);
505 maxthread = MIN(maxproc * max_threads_per_proc, 100000);
509 mtx_init(&tid_lock, "TID lock", NULL, MTX_DEF);
510 tid_bitmap = bit_alloc(maxthread, M_TIDHASH, M_WAITOK);
516 if (tid0 != THREAD0_TID)
517 panic("tid0 %d != %d\n", tid0, THREAD0_TID);
519 flags = UMA_ZONE_NOFREE;
522 * Force thread structures to be allocated from the direct map.
523 * Otherwise, superpage promotions and demotions may temporarily
524 * invalidate thread structure mappings. For most dynamically allocated
525 * structures this is not a problem, but translation faults cannot be
526 * handled without accessing curthread.
528 flags |= UMA_ZONE_CONTIG;
530 thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(),
531 thread_ctor, thread_dtor, thread_init, thread_fini,
533 tidhashtbl = hashinit(maxproc / 2, M_TIDHASH, &tidhash);
534 tidhashlock = (tidhash + 1) / 64;
537 tidhashtbl_lock = malloc(sizeof(*tidhashtbl_lock) * (tidhashlock + 1),
538 M_TIDHASH, M_WAITOK | M_ZERO);
539 for (i = 0; i < tidhashlock + 1; i++)
540 rw_init(&tidhashtbl_lock[i], "tidhash");
542 TASK_INIT(&thread_reap_task, 0, thread_reap_task_cb, NULL);
543 callout_init(&thread_reap_callout, 1);
544 callout_reset(&thread_reap_callout, 5 * hz,
545 thread_reap_callout_cb, NULL);
549 * Place an unused thread on the zombie list.
552 thread_zombie(struct thread *td)
554 struct thread_domain_data *tdd;
557 tdd = &thread_domain_data[td->td_allocdomain];
558 ztd = atomic_load_ptr(&tdd->tdd_zombies);
561 if (atomic_fcmpset_rel_ptr((uintptr_t *)&tdd->tdd_zombies,
562 (uintptr_t *)&ztd, (uintptr_t)td))
569 * Release a thread that has exited after cpu_throw().
572 thread_stash(struct thread *td)
574 atomic_subtract_rel_int(&td->td_proc->p_exitthreads, 1);
579 * Reap zombies from passed domain.
582 thread_reap_domain(struct thread_domain_data *tdd)
584 struct thread *itd, *ntd;
585 struct tidbatch tidbatch;
586 struct credbatch credbatch;
592 * Reading upfront is pessimal if followed by concurrent atomic_swap,
593 * but most of the time the list is empty.
595 if (tdd->tdd_zombies == NULL)
598 itd = (struct thread *)atomic_swap_ptr((uintptr_t *)&tdd->tdd_zombies,
604 * Multiple CPUs can get here, the race is fine as ticks is only
607 tdd->tdd_reapticks = ticks;
609 tidbatch_prep(&tidbatch);
610 credbatch_prep(&credbatch);
615 while (itd != NULL) {
616 ntd = itd->td_zombie;
617 EVENTHANDLER_DIRECT_INVOKE(thread_dtor, itd);
618 tidbatch_add(&tidbatch, itd);
619 credbatch_add(&credbatch, itd);
620 MPASS(itd->td_limit != NULL);
621 if (lim != itd->td_limit) {
623 lim_freen(lim, limcount);
629 thread_free_batched(itd);
630 tidbatch_process(&tidbatch);
631 credbatch_process(&credbatch);
634 thread_count_sub(tdcount);
640 tidbatch_final(&tidbatch);
641 credbatch_final(&credbatch);
643 thread_count_sub(tdcount);
645 MPASS(limcount != 0);
646 lim_freen(lim, limcount);
650 * Reap zombies from all domains.
653 thread_reap_all(void)
655 struct thread_domain_data *tdd;
658 domain = PCPU_GET(domain);
659 for (i = 0; i < vm_ndomains; i++) {
660 tdd = &thread_domain_data[(i + domain) % vm_ndomains];
661 thread_reap_domain(tdd);
666 * Reap zombies from local domain.
671 struct thread_domain_data *tdd;
674 domain = PCPU_GET(domain);
675 tdd = &thread_domain_data[domain];
677 thread_reap_domain(tdd);
681 thread_reap_task_cb(void *arg __unused, int pending __unused)
688 thread_reap_callout_cb(void *arg __unused)
690 struct thread_domain_data *tdd;
691 int i, cticks, lticks;
695 cticks = atomic_load_int(&ticks);
696 for (i = 0; i < vm_ndomains; i++) {
697 tdd = &thread_domain_data[i];
698 lticks = tdd->tdd_reapticks;
699 if (tdd->tdd_zombies != NULL &&
700 (u_int)(cticks - lticks) > 5 * hz) {
707 taskqueue_enqueue(taskqueue_thread, &thread_reap_task);
708 callout_reset(&thread_reap_callout, 5 * hz,
709 thread_reap_callout_cb, NULL);
713 * Calling this function guarantees that any thread that exited before
714 * the call is reaped when the function returns. By 'exited' we mean
715 * a thread removed from the process linkage with thread_unlink().
716 * Practically this means that caller must lock/unlock corresponding
717 * process lock before the call, to synchronize with thread_exit().
720 thread_reap_barrier(void)
725 * First do context switches to each CPU to ensure that all
726 * PCPU pc_deadthreads are moved to zombie list.
728 quiesce_all_cpus("", PDROP);
731 * Second, fire the task in the same thread as normal
732 * thread_reap() is done, to serialize reaping.
734 t = malloc(sizeof(*t), M_TEMP, M_WAITOK);
735 TASK_INIT(t, 0, thread_reap_task_cb, t);
736 taskqueue_enqueue(taskqueue_thread, t);
737 taskqueue_drain(taskqueue_thread, t);
745 thread_alloc(int pages)
750 if (!thread_count_inc()) {
755 td = uma_zalloc(thread_zone, M_WAITOK);
756 KASSERT(td->td_kstack == 0, ("thread_alloc got thread with kstack"));
757 if (!vm_thread_new(td, pages)) {
758 uma_zfree(thread_zone, td);
764 cpu_thread_alloc(td);
765 EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td);
770 thread_alloc_stack(struct thread *td, int pages)
773 KASSERT(td->td_kstack == 0,
774 ("thread_alloc_stack called on a thread with kstack"));
775 if (!vm_thread_new(td, pages))
777 cpu_thread_alloc(td);
782 * Deallocate a thread.
785 thread_free_batched(struct thread *td)
788 lock_profile_thread_exit(td);
790 cpuset_rel(td->td_cpuset);
791 td->td_cpuset = NULL;
793 if (td->td_kstack != 0)
794 vm_thread_dispose(td);
795 callout_drain(&td->td_slpcallout);
797 * Freeing handled by the caller.
800 uma_zfree(thread_zone, td);
804 thread_free(struct thread *td)
808 EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td);
810 thread_free_batched(td);
816 thread_cow_get_proc(struct thread *newtd, struct proc *p)
819 PROC_LOCK_ASSERT(p, MA_OWNED);
820 newtd->td_realucred = crcowget(p->p_ucred);
821 newtd->td_ucred = newtd->td_realucred;
822 newtd->td_limit = lim_hold(p->p_limit);
823 newtd->td_cowgen = p->p_cowgen;
827 thread_cow_get(struct thread *newtd, struct thread *td)
830 MPASS(td->td_realucred == td->td_ucred);
831 newtd->td_realucred = crcowget(td->td_realucred);
832 newtd->td_ucred = newtd->td_realucred;
833 newtd->td_limit = lim_hold(td->td_limit);
834 newtd->td_cowgen = td->td_cowgen;
838 thread_cow_free(struct thread *td)
841 if (td->td_realucred != NULL)
843 if (td->td_limit != NULL)
844 lim_free(td->td_limit);
848 thread_cow_update(struct thread *td)
851 struct ucred *oldcred;
852 struct plimit *oldlimit;
857 oldcred = crcowsync();
858 if (td->td_limit != p->p_limit) {
859 oldlimit = td->td_limit;
860 td->td_limit = lim_hold(p->p_limit);
862 td->td_cowgen = p->p_cowgen;
866 if (oldlimit != NULL)
871 * Discard the current thread and exit from its context.
872 * Always called with scheduler locked.
874 * Because we can't free a thread while we're operating under its context,
875 * push the current thread into our CPU's deadthread holder. This means
876 * we needn't worry about someone else grabbing our context before we
882 uint64_t runtime, new_switchtime;
891 PROC_SLOCK_ASSERT(p, MA_OWNED);
892 mtx_assert(&Giant, MA_NOTOWNED);
894 PROC_LOCK_ASSERT(p, MA_OWNED);
895 KASSERT(p != NULL, ("thread exiting without a process"));
896 CTR3(KTR_PROC, "thread_exit: thread %p (pid %ld, %s)", td,
897 (long)p->p_pid, td->td_name);
898 SDT_PROBE0(proc, , , lwp__exit);
899 KASSERT(TAILQ_EMPTY(&td->td_sigqueue.sq_list), ("signal pending"));
900 MPASS(td->td_realucred == td->td_ucred);
903 * drop FPU & debug register state storage, or any other
904 * architecture specific resources that
905 * would not be on a new untouched process.
910 * The last thread is left attached to the process
911 * So that the whole bundle gets recycled. Skip
912 * all this stuff if we never had threads.
913 * EXIT clears all sign of other threads when
914 * it goes to single threading, so the last thread always
915 * takes the short path.
917 if (p->p_flag & P_HADTHREADS) {
918 if (p->p_numthreads > 1) {
919 atomic_add_int(&td->td_proc->p_exitthreads, 1);
921 td2 = FIRST_THREAD_IN_PROC(p);
922 sched_exit_thread(td2, td);
925 * The test below is NOT true if we are the
926 * sole exiting thread. P_STOPPED_SINGLE is unset
927 * in exit1() after it is the only survivor.
929 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
930 if (p->p_numthreads == p->p_suspcount) {
931 thread_lock(p->p_singlethread);
932 wakeup_swapper = thread_unsuspend_one(
933 p->p_singlethread, p, false);
939 PCPU_SET(deadthread, td);
942 * The last thread is exiting.. but not through exit()
944 panic ("thread_exit: Last thread exiting on its own");
949 * If this thread is part of a process that is being tracked by hwpmc(4),
950 * inform the module of the thread's impending exit.
952 if (PMC_PROC_IS_USING_PMCS(td->td_proc)) {
953 PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT);
954 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_EXIT, NULL);
955 } else if (PMC_SYSTEM_SAMPLING_ACTIVE())
956 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_EXIT_LOG, NULL);
963 /* Do the same timestamp bookkeeping that mi_switch() would do. */
964 new_switchtime = cpu_ticks();
965 runtime = new_switchtime - PCPU_GET(switchtime);
966 td->td_runtime += runtime;
967 td->td_incruntime += runtime;
968 PCPU_SET(switchtime, new_switchtime);
969 PCPU_SET(switchticks, ticks);
972 /* Save our resource usage in our process. */
973 td->td_ru.ru_nvcsw++;
974 ruxagg_locked(p, td);
975 rucollect(&p->p_ru, &td->td_ru);
978 TD_SET_STATE(td, TDS_INACTIVE);
980 witness_thread_exit(td);
982 CTR1(KTR_PROC, "thread_exit: cpu_throw() thread %p", td);
984 panic("I'm a teapot!");
989 * Do any thread specific cleanups that may be needed in wait()
990 * called with Giant, proc and schedlock not held.
993 thread_wait(struct proc *p)
997 mtx_assert(&Giant, MA_NOTOWNED);
998 KASSERT(p->p_numthreads == 1, ("multiple threads in thread_wait()"));
999 KASSERT(p->p_exitthreads == 0, ("p_exitthreads leaking"));
1000 td = FIRST_THREAD_IN_PROC(p);
1001 /* Lock the last thread so we spin until it exits cpu_throw(). */
1004 lock_profile_thread_exit(td);
1005 cpuset_rel(td->td_cpuset);
1006 td->td_cpuset = NULL;
1007 cpu_thread_clean(td);
1008 thread_cow_free(td);
1009 callout_drain(&td->td_slpcallout);
1010 thread_reap(); /* check for zombie threads etc. */
1014 * Link a thread to a process.
1015 * set up anything that needs to be initialized for it to
1016 * be used by the process.
1019 thread_link(struct thread *td, struct proc *p)
1023 * XXX This can't be enabled because it's called for proc0 before
1024 * its lock has been created.
1025 * PROC_LOCK_ASSERT(p, MA_OWNED);
1027 TD_SET_STATE(td, TDS_INACTIVE);
1029 td->td_flags = TDF_INMEM;
1031 LIST_INIT(&td->td_contested);
1032 LIST_INIT(&td->td_lprof[0]);
1033 LIST_INIT(&td->td_lprof[1]);
1035 SLIST_INIT(&td->td_epochs);
1037 sigqueue_init(&td->td_sigqueue, p);
1038 callout_init(&td->td_slpcallout, 1);
1039 TAILQ_INSERT_TAIL(&p->p_threads, td, td_plist);
1048 thread_unlink(struct thread *td)
1050 struct proc *p = td->td_proc;
1052 PROC_LOCK_ASSERT(p, MA_OWNED);
1054 MPASS(SLIST_EMPTY(&td->td_epochs));
1057 TAILQ_REMOVE(&p->p_threads, td, td_plist);
1059 /* could clear a few other things here */
1060 /* Must NOT clear links to proc! */
1064 calc_remaining(struct proc *p, int mode)
1068 PROC_LOCK_ASSERT(p, MA_OWNED);
1069 PROC_SLOCK_ASSERT(p, MA_OWNED);
1070 if (mode == SINGLE_EXIT)
1071 remaining = p->p_numthreads;
1072 else if (mode == SINGLE_BOUNDARY)
1073 remaining = p->p_numthreads - p->p_boundary_count;
1074 else if (mode == SINGLE_NO_EXIT || mode == SINGLE_ALLPROC)
1075 remaining = p->p_numthreads - p->p_suspcount;
1077 panic("calc_remaining: wrong mode %d", mode);
1082 remain_for_mode(int mode)
1085 return (mode == SINGLE_ALLPROC ? 0 : 1);
1089 weed_inhib(int mode, struct thread *td2, struct proc *p)
1093 PROC_LOCK_ASSERT(p, MA_OWNED);
1094 PROC_SLOCK_ASSERT(p, MA_OWNED);
1095 THREAD_LOCK_ASSERT(td2, MA_OWNED);
1100 * Since the thread lock is dropped by the scheduler we have
1101 * to retry to check for races.
1106 if (TD_IS_SUSPENDED(td2)) {
1107 wakeup_swapper |= thread_unsuspend_one(td2, p, true);
1111 if (TD_CAN_ABORT(td2)) {
1112 wakeup_swapper |= sleepq_abort(td2, EINTR);
1113 return (wakeup_swapper);
1116 case SINGLE_BOUNDARY:
1117 case SINGLE_NO_EXIT:
1118 if (TD_IS_SUSPENDED(td2) &&
1119 (td2->td_flags & TDF_BOUNDARY) == 0) {
1120 wakeup_swapper |= thread_unsuspend_one(td2, p, false);
1124 if (TD_CAN_ABORT(td2)) {
1125 wakeup_swapper |= sleepq_abort(td2, ERESTART);
1126 return (wakeup_swapper);
1129 case SINGLE_ALLPROC:
1131 * ALLPROC suspend tries to avoid spurious EINTR for
1132 * threads sleeping interruptable, by suspending the
1133 * thread directly, similarly to sig_suspend_threads().
1134 * Since such sleep is not performed at the user
1135 * boundary, TDF_BOUNDARY flag is not set, and TDF_ALLPROCSUSP
1136 * is used to avoid immediate un-suspend.
1138 if (TD_IS_SUSPENDED(td2) && (td2->td_flags & (TDF_BOUNDARY |
1139 TDF_ALLPROCSUSP)) == 0) {
1140 wakeup_swapper |= thread_unsuspend_one(td2, p, false);
1144 if (TD_CAN_ABORT(td2)) {
1145 if ((td2->td_flags & TDF_SBDRY) == 0) {
1146 thread_suspend_one(td2);
1147 td2->td_flags |= TDF_ALLPROCSUSP;
1149 wakeup_swapper |= sleepq_abort(td2, ERESTART);
1150 return (wakeup_swapper);
1158 return (wakeup_swapper);
1162 * Enforce single-threading.
1164 * Returns 1 if the caller must abort (another thread is waiting to
1165 * exit the process or similar). Process is locked!
1166 * Returns 0 when you are successfully the only thread running.
1167 * A process has successfully single threaded in the suspend mode when
1168 * There are no threads in user mode. Threads in the kernel must be
1169 * allowed to continue until they get to the user boundary. They may even
1170 * copy out their return values and data before suspending. They may however be
1171 * accelerated in reaching the user boundary as we will wake up
1172 * any sleeping threads that are interruptable. (PCATCH).
1175 thread_single(struct proc *p, int mode)
1179 int remaining, wakeup_swapper;
1182 KASSERT(mode == SINGLE_EXIT || mode == SINGLE_BOUNDARY ||
1183 mode == SINGLE_ALLPROC || mode == SINGLE_NO_EXIT,
1184 ("invalid mode %d", mode));
1186 * If allowing non-ALLPROC singlethreading for non-curproc
1187 * callers, calc_remaining() and remain_for_mode() should be
1188 * adjusted to also account for td->td_proc != p. For now
1189 * this is not implemented because it is not used.
1191 KASSERT((mode == SINGLE_ALLPROC && td->td_proc != p) ||
1192 (mode != SINGLE_ALLPROC && td->td_proc == p),
1193 ("mode %d proc %p curproc %p", mode, p, td->td_proc));
1194 mtx_assert(&Giant, MA_NOTOWNED);
1195 PROC_LOCK_ASSERT(p, MA_OWNED);
1197 if ((p->p_flag & P_HADTHREADS) == 0 && mode != SINGLE_ALLPROC)
1200 /* Is someone already single threading? */
1201 if (p->p_singlethread != NULL && p->p_singlethread != td)
1204 if (mode == SINGLE_EXIT) {
1205 p->p_flag |= P_SINGLE_EXIT;
1206 p->p_flag &= ~P_SINGLE_BOUNDARY;
1208 p->p_flag &= ~P_SINGLE_EXIT;
1209 if (mode == SINGLE_BOUNDARY)
1210 p->p_flag |= P_SINGLE_BOUNDARY;
1212 p->p_flag &= ~P_SINGLE_BOUNDARY;
1214 if (mode == SINGLE_ALLPROC)
1215 p->p_flag |= P_TOTAL_STOP;
1216 p->p_flag |= P_STOPPED_SINGLE;
1218 p->p_singlethread = td;
1219 remaining = calc_remaining(p, mode);
1220 while (remaining != remain_for_mode(mode)) {
1221 if (P_SHOULDSTOP(p) != P_STOPPED_SINGLE)
1224 FOREACH_THREAD_IN_PROC(p, td2) {
1228 td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
1229 if (TD_IS_INHIBITED(td2)) {
1230 wakeup_swapper |= weed_inhib(mode, td2, p);
1232 } else if (TD_IS_RUNNING(td2) && td != td2) {
1233 forward_signal(td2);
1241 remaining = calc_remaining(p, mode);
1244 * Maybe we suspended some threads.. was it enough?
1246 if (remaining == remain_for_mode(mode))
1251 * Wake us up when everyone else has suspended.
1252 * In the mean time we suspend as well.
1254 thread_suspend_switch(td, p);
1255 remaining = calc_remaining(p, mode);
1257 if (mode == SINGLE_EXIT) {
1259 * Convert the process to an unthreaded process. The
1260 * SINGLE_EXIT is called by exit1() or execve(), in
1261 * both cases other threads must be retired.
1263 KASSERT(p->p_numthreads == 1, ("Unthreading with >1 threads"));
1264 p->p_singlethread = NULL;
1265 p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_HADTHREADS);
1268 * Wait for any remaining threads to exit cpu_throw().
1270 while (p->p_exitthreads != 0) {
1273 sched_relinquish(td);
1277 } else if (mode == SINGLE_BOUNDARY) {
1279 * Wait until all suspended threads are removed from
1280 * the processors. The thread_suspend_check()
1281 * increments p_boundary_count while it is still
1282 * running, which makes it possible for the execve()
1283 * to destroy vmspace while our other threads are
1284 * still using the address space.
1286 * We lock the thread, which is only allowed to
1287 * succeed after context switch code finished using
1288 * the address space.
1290 FOREACH_THREAD_IN_PROC(p, td2) {
1294 KASSERT((td2->td_flags & TDF_BOUNDARY) != 0,
1295 ("td %p not on boundary", td2));
1296 KASSERT(TD_IS_SUSPENDED(td2),
1297 ("td %p is not suspended", td2));
1306 thread_suspend_check_needed(void)
1313 PROC_LOCK_ASSERT(p, MA_OWNED);
1314 return (P_SHOULDSTOP(p) || ((p->p_flag & P_TRACED) != 0 &&
1315 (td->td_dbgflags & TDB_SUSPEND) != 0));
1319 * Called in from locations that can safely check to see
1320 * whether we have to suspend or at least throttle for a
1321 * single-thread event (e.g. fork).
1323 * Such locations include userret().
1324 * If the "return_instead" argument is non zero, the thread must be able to
1325 * accept 0 (caller may continue), or 1 (caller must abort) as a result.
1327 * The 'return_instead' argument tells the function if it may do a
1328 * thread_exit() or suspend, or whether the caller must abort and back
1331 * If the thread that set the single_threading request has set the
1332 * P_SINGLE_EXIT bit in the process flags then this call will never return
1333 * if 'return_instead' is false, but will exit.
1335 * P_SINGLE_EXIT | return_instead == 0| return_instead != 0
1336 *---------------+--------------------+---------------------
1337 * 0 | returns 0 | returns 0 or 1
1338 * | when ST ends | immediately
1339 *---------------+--------------------+---------------------
1340 * 1 | thread exits | returns 1
1342 * 0 = thread_exit() or suspension ok,
1343 * other = return error instead of stopping the thread.
1345 * While a full suspension is under effect, even a single threading
1346 * thread would be suspended if it made this call (but it shouldn't).
1347 * This call should only be made from places where
1348 * thread_exit() would be safe as that may be the outcome unless
1349 * return_instead is set.
1352 thread_suspend_check(int return_instead)
1360 mtx_assert(&Giant, MA_NOTOWNED);
1361 PROC_LOCK_ASSERT(p, MA_OWNED);
1362 while (thread_suspend_check_needed()) {
1363 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
1364 KASSERT(p->p_singlethread != NULL,
1365 ("singlethread not set"));
1367 * The only suspension in action is a
1368 * single-threading. Single threader need not stop.
1369 * It is safe to access p->p_singlethread unlocked
1370 * because it can only be set to our address by us.
1372 if (p->p_singlethread == td)
1373 return (0); /* Exempt from stopping. */
1375 if ((p->p_flag & P_SINGLE_EXIT) && return_instead)
1378 /* Should we goto user boundary if we didn't come from there? */
1379 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE &&
1380 (p->p_flag & P_SINGLE_BOUNDARY) && return_instead)
1384 * Ignore suspend requests if they are deferred.
1386 if ((td->td_flags & TDF_SBDRY) != 0) {
1387 KASSERT(return_instead,
1388 ("TDF_SBDRY set for unsafe thread_suspend_check"));
1389 KASSERT((td->td_flags & (TDF_SEINTR | TDF_SERESTART)) !=
1390 (TDF_SEINTR | TDF_SERESTART),
1391 ("both TDF_SEINTR and TDF_SERESTART"));
1392 return (TD_SBDRY_INTR(td) ? TD_SBDRY_ERRNO(td) : 0);
1396 * If the process is waiting for us to exit,
1397 * this thread should just suicide.
1398 * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE.
1400 if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) {
1404 * Allow Linux emulation layer to do some work
1405 * before thread suicide.
1407 if (__predict_false(p->p_sysent->sv_thread_detach != NULL))
1408 (p->p_sysent->sv_thread_detach)(td);
1409 umtx_thread_exit(td);
1411 panic("stopped thread did not exit");
1416 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
1417 if (p->p_numthreads == p->p_suspcount + 1) {
1418 thread_lock(p->p_singlethread);
1419 wakeup_swapper = thread_unsuspend_one(
1420 p->p_singlethread, p, false);
1428 * When a thread suspends, it just
1429 * gets taken off all queues.
1431 thread_suspend_one(td);
1432 if (return_instead == 0) {
1433 p->p_boundary_count++;
1434 td->td_flags |= TDF_BOUNDARY;
1437 mi_switch(SW_INVOL | SWT_SUSPEND);
1444 * Check for possible stops and suspensions while executing a
1445 * casueword or similar transiently failing operation.
1447 * The sleep argument controls whether the function can handle a stop
1448 * request itself or it should return ERESTART and the request is
1449 * proceed at the kernel/user boundary in ast.
1451 * Typically, when retrying due to casueword(9) failure (rv == 1), we
1452 * should handle the stop requests there, with exception of cases when
1453 * the thread owns a kernel resource, for instance busied the umtx
1454 * key, or when functions return immediately if thread_check_susp()
1455 * returned non-zero. On the other hand, retrying the whole lock
1456 * operation, we better not stop there but delegate the handling to
1459 * If the request is for thread termination P_SINGLE_EXIT, we cannot
1460 * handle it at all, and simply return EINTR.
1463 thread_check_susp(struct thread *td, bool sleep)
1469 * The check for TDF_NEEDSUSPCHK is racy, but it is enough to
1470 * eventually break the lockstep loop.
1472 if ((td->td_flags & TDF_NEEDSUSPCHK) == 0)
1477 if (p->p_flag & P_SINGLE_EXIT)
1479 else if (P_SHOULDSTOP(p) ||
1480 ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_SUSPEND)))
1481 error = sleep ? thread_suspend_check(0) : ERESTART;
1487 thread_suspend_switch(struct thread *td, struct proc *p)
1490 KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
1491 PROC_LOCK_ASSERT(p, MA_OWNED);
1492 PROC_SLOCK_ASSERT(p, MA_OWNED);
1494 * We implement thread_suspend_one in stages here to avoid
1495 * dropping the proc lock while the thread lock is owned.
1497 if (p == td->td_proc) {
1503 td->td_flags &= ~TDF_NEEDSUSPCHK;
1504 TD_SET_SUSPENDED(td);
1508 mi_switch(SW_VOL | SWT_SUSPEND);
1515 thread_suspend_one(struct thread *td)
1520 PROC_SLOCK_ASSERT(p, MA_OWNED);
1521 THREAD_LOCK_ASSERT(td, MA_OWNED);
1522 KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
1524 td->td_flags &= ~TDF_NEEDSUSPCHK;
1525 TD_SET_SUSPENDED(td);
1530 thread_unsuspend_one(struct thread *td, struct proc *p, bool boundary)
1533 THREAD_LOCK_ASSERT(td, MA_OWNED);
1534 KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended"));
1535 TD_CLR_SUSPENDED(td);
1536 td->td_flags &= ~TDF_ALLPROCSUSP;
1537 if (td->td_proc == p) {
1538 PROC_SLOCK_ASSERT(p, MA_OWNED);
1540 if (boundary && (td->td_flags & TDF_BOUNDARY) != 0) {
1541 td->td_flags &= ~TDF_BOUNDARY;
1542 p->p_boundary_count--;
1545 return (setrunnable(td, 0));
1549 thread_run_flash(struct thread *td)
1554 PROC_LOCK_ASSERT(p, MA_OWNED);
1556 if (TD_ON_SLEEPQ(td))
1557 sleepq_remove_nested(td);
1561 THREAD_LOCK_ASSERT(td, MA_OWNED);
1562 KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended"));
1564 TD_CLR_SUSPENDED(td);
1566 MPASS(p->p_suspcount > 0);
1569 if (setrunnable(td, 0))
1574 * Allow all threads blocked by single threading to continue running.
1577 thread_unsuspend(struct proc *p)
1582 PROC_LOCK_ASSERT(p, MA_OWNED);
1583 PROC_SLOCK_ASSERT(p, MA_OWNED);
1585 if (!P_SHOULDSTOP(p)) {
1586 FOREACH_THREAD_IN_PROC(p, td) {
1588 if (TD_IS_SUSPENDED(td)) {
1589 wakeup_swapper |= thread_unsuspend_one(td, p,
1594 } else if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE &&
1595 p->p_numthreads == p->p_suspcount) {
1597 * Stopping everything also did the job for the single
1598 * threading request. Now we've downgraded to single-threaded,
1601 if (p->p_singlethread->td_proc == p) {
1602 thread_lock(p->p_singlethread);
1603 wakeup_swapper = thread_unsuspend_one(
1604 p->p_singlethread, p, false);
1612 * End the single threading mode..
1615 thread_single_end(struct proc *p, int mode)
1620 KASSERT(mode == SINGLE_EXIT || mode == SINGLE_BOUNDARY ||
1621 mode == SINGLE_ALLPROC || mode == SINGLE_NO_EXIT,
1622 ("invalid mode %d", mode));
1623 PROC_LOCK_ASSERT(p, MA_OWNED);
1624 KASSERT((mode == SINGLE_ALLPROC && (p->p_flag & P_TOTAL_STOP) != 0) ||
1625 (mode != SINGLE_ALLPROC && (p->p_flag & P_TOTAL_STOP) == 0),
1626 ("mode %d does not match P_TOTAL_STOP", mode));
1627 KASSERT(mode == SINGLE_ALLPROC || p->p_singlethread == curthread,
1628 ("thread_single_end from other thread %p %p",
1629 curthread, p->p_singlethread));
1630 KASSERT(mode != SINGLE_BOUNDARY ||
1631 (p->p_flag & P_SINGLE_BOUNDARY) != 0,
1632 ("mis-matched SINGLE_BOUNDARY flags %x", p->p_flag));
1633 p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_SINGLE_BOUNDARY |
1636 p->p_singlethread = NULL;
1639 * If there are other threads they may now run,
1640 * unless of course there is a blanket 'stop order'
1641 * on the process. The single threader must be allowed
1642 * to continue however as this is a bad place to stop.
1644 if (p->p_numthreads != remain_for_mode(mode) && !P_SHOULDSTOP(p)) {
1645 FOREACH_THREAD_IN_PROC(p, td) {
1647 if (TD_IS_SUSPENDED(td)) {
1648 wakeup_swapper |= thread_unsuspend_one(td, p,
1649 mode == SINGLE_BOUNDARY);
1654 KASSERT(mode != SINGLE_BOUNDARY || p->p_boundary_count == 0,
1655 ("inconsistent boundary count %d", p->p_boundary_count));
1662 * Locate a thread by number and return with proc lock held.
1664 * thread exit establishes proc -> tidhash lock ordering, but lookup
1665 * takes tidhash first and needs to return locked proc.
1667 * The problem is worked around by relying on type-safety of both
1668 * structures and doing the work in 2 steps:
1669 * - tidhash-locked lookup which saves both thread and proc pointers
1670 * - proc-locked verification that the found thread still matches
1673 tdfind_hash(lwpid_t tid, pid_t pid, struct proc **pp, struct thread **tdp)
1675 #define RUN_THRESH 16
1682 rw_rlock(TIDHASHLOCK(tid));
1684 LIST_FOREACH(td, TIDHASH(tid), td_hash) {
1685 if (td->td_tid != tid) {
1690 if (pid != -1 && p->p_pid != pid) {
1694 if (run > RUN_THRESH) {
1695 if (rw_try_upgrade(TIDHASHLOCK(tid))) {
1696 LIST_REMOVE(td, td_hash);
1697 LIST_INSERT_HEAD(TIDHASH(td->td_tid),
1699 rw_wunlock(TIDHASHLOCK(tid));
1707 rw_runlock(TIDHASHLOCK(tid));
1716 tdfind(lwpid_t tid, pid_t pid)
1722 if (td->td_tid == tid) {
1723 if (pid != -1 && td->td_proc->p_pid != pid)
1725 PROC_LOCK(td->td_proc);
1730 if (!tdfind_hash(tid, pid, &p, &td))
1733 if (td->td_tid != tid) {
1737 if (td->td_proc != p) {
1741 if (p->p_state == PRS_NEW) {
1750 tidhash_add(struct thread *td)
1752 rw_wlock(TIDHASHLOCK(td->td_tid));
1753 LIST_INSERT_HEAD(TIDHASH(td->td_tid), td, td_hash);
1754 rw_wunlock(TIDHASHLOCK(td->td_tid));
1758 tidhash_remove(struct thread *td)
1761 rw_wlock(TIDHASHLOCK(td->td_tid));
1762 LIST_REMOVE(td, td_hash);
1763 rw_wunlock(TIDHASHLOCK(td->td_tid));