xref: /xnu-10063.121.3/bsd/pthread/pthread_workqueue.c (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions /*
2*2c2f96dcSApple OSS Distributions  * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
3*2c2f96dcSApple OSS Distributions  *
4*2c2f96dcSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*2c2f96dcSApple OSS Distributions  *
6*2c2f96dcSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*2c2f96dcSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*2c2f96dcSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*2c2f96dcSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*2c2f96dcSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*2c2f96dcSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*2c2f96dcSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*2c2f96dcSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*2c2f96dcSApple OSS Distributions  *
15*2c2f96dcSApple OSS Distributions  * Please obtain a copy of the License at
16*2c2f96dcSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*2c2f96dcSApple OSS Distributions  *
18*2c2f96dcSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*2c2f96dcSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*2c2f96dcSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*2c2f96dcSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*2c2f96dcSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*2c2f96dcSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*2c2f96dcSApple OSS Distributions  * limitations under the License.
25*2c2f96dcSApple OSS Distributions  *
26*2c2f96dcSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*2c2f96dcSApple OSS Distributions  */
28*2c2f96dcSApple OSS Distributions /* Copyright (c) 1995-2018 Apple, Inc. All Rights Reserved */
29*2c2f96dcSApple OSS Distributions 
30*2c2f96dcSApple OSS Distributions #include <sys/cdefs.h>
31*2c2f96dcSApple OSS Distributions 
32*2c2f96dcSApple OSS Distributions #include <kern/assert.h>
33*2c2f96dcSApple OSS Distributions #include <kern/ast.h>
34*2c2f96dcSApple OSS Distributions #include <kern/clock.h>
35*2c2f96dcSApple OSS Distributions #include <kern/cpu_data.h>
36*2c2f96dcSApple OSS Distributions #include <kern/kern_types.h>
37*2c2f96dcSApple OSS Distributions #include <kern/policy_internal.h>
38*2c2f96dcSApple OSS Distributions #include <kern/processor.h>
39*2c2f96dcSApple OSS Distributions #include <kern/sched_prim.h>    /* for thread_exception_return */
40*2c2f96dcSApple OSS Distributions #include <kern/task.h>
41*2c2f96dcSApple OSS Distributions #include <kern/thread.h>
42*2c2f96dcSApple OSS Distributions #include <kern/thread_group.h>
43*2c2f96dcSApple OSS Distributions #include <kern/zalloc.h>
44*2c2f96dcSApple OSS Distributions #include <mach/kern_return.h>
45*2c2f96dcSApple OSS Distributions #include <mach/mach_param.h>
46*2c2f96dcSApple OSS Distributions #include <mach/mach_port.h>
47*2c2f96dcSApple OSS Distributions #include <mach/mach_types.h>
48*2c2f96dcSApple OSS Distributions #include <mach/mach_vm.h>
49*2c2f96dcSApple OSS Distributions #include <mach/sync_policy.h>
50*2c2f96dcSApple OSS Distributions #include <mach/task.h>
51*2c2f96dcSApple OSS Distributions #include <mach/thread_act.h> /* for thread_resume */
52*2c2f96dcSApple OSS Distributions #include <mach/thread_policy.h>
53*2c2f96dcSApple OSS Distributions #include <mach/thread_status.h>
54*2c2f96dcSApple OSS Distributions #include <mach/vm_prot.h>
55*2c2f96dcSApple OSS Distributions #include <mach/vm_statistics.h>
56*2c2f96dcSApple OSS Distributions #include <machine/atomic.h>
57*2c2f96dcSApple OSS Distributions #include <machine/machine_routines.h>
58*2c2f96dcSApple OSS Distributions #include <machine/smp.h>
59*2c2f96dcSApple OSS Distributions #include <vm/vm_map.h>
60*2c2f96dcSApple OSS Distributions #include <vm/vm_protos.h>
61*2c2f96dcSApple OSS Distributions 
62*2c2f96dcSApple OSS Distributions #include <sys/eventvar.h>
63*2c2f96dcSApple OSS Distributions #include <sys/kdebug.h>
64*2c2f96dcSApple OSS Distributions #include <sys/kernel.h>
65*2c2f96dcSApple OSS Distributions #include <sys/lock.h>
66*2c2f96dcSApple OSS Distributions #include <sys/param.h>
67*2c2f96dcSApple OSS Distributions #include <sys/proc_info.h>      /* for fill_procworkqueue */
68*2c2f96dcSApple OSS Distributions #include <sys/proc_internal.h>
69*2c2f96dcSApple OSS Distributions #include <sys/pthread_shims.h>
70*2c2f96dcSApple OSS Distributions #include <sys/resourcevar.h>
71*2c2f96dcSApple OSS Distributions #include <sys/signalvar.h>
72*2c2f96dcSApple OSS Distributions #include <sys/sysctl.h>
73*2c2f96dcSApple OSS Distributions #include <sys/sysproto.h>
74*2c2f96dcSApple OSS Distributions #include <sys/systm.h>
75*2c2f96dcSApple OSS Distributions #include <sys/ulock.h> /* for ulock_owner_value_to_port_name */
76*2c2f96dcSApple OSS Distributions 
77*2c2f96dcSApple OSS Distributions #include <pthread/bsdthread_private.h>
78*2c2f96dcSApple OSS Distributions #include <pthread/workqueue_syscalls.h>
79*2c2f96dcSApple OSS Distributions #include <pthread/workqueue_internal.h>
80*2c2f96dcSApple OSS Distributions #include <pthread/workqueue_trace.h>
81*2c2f96dcSApple OSS Distributions 
82*2c2f96dcSApple OSS Distributions #include <os/log.h>
83*2c2f96dcSApple OSS Distributions 
84*2c2f96dcSApple OSS Distributions static void workq_unpark_continue(void *uth, wait_result_t wr) __dead2;
85*2c2f96dcSApple OSS Distributions static void workq_schedule_creator(proc_t p, struct workqueue *wq,
86*2c2f96dcSApple OSS Distributions     workq_kern_threadreq_flags_t flags);
87*2c2f96dcSApple OSS Distributions 
88*2c2f96dcSApple OSS Distributions static bool workq_threadreq_admissible(struct workqueue *wq, struct uthread *uth,
89*2c2f96dcSApple OSS Distributions     workq_threadreq_t req);
90*2c2f96dcSApple OSS Distributions 
91*2c2f96dcSApple OSS Distributions static uint32_t workq_constrained_allowance(struct workqueue *wq,
92*2c2f96dcSApple OSS Distributions     thread_qos_t at_qos, struct uthread *uth, bool may_start_timer);
93*2c2f96dcSApple OSS Distributions 
94*2c2f96dcSApple OSS Distributions static bool _wq_cooperative_queue_refresh_best_req_qos(struct workqueue *wq);
95*2c2f96dcSApple OSS Distributions 
96*2c2f96dcSApple OSS Distributions static bool workq_thread_is_busy(uint64_t cur_ts,
97*2c2f96dcSApple OSS Distributions     _Atomic uint64_t *lastblocked_tsp);
98*2c2f96dcSApple OSS Distributions 
99*2c2f96dcSApple OSS Distributions static int workq_sysctl_handle_usecs SYSCTL_HANDLER_ARGS;
100*2c2f96dcSApple OSS Distributions 
101*2c2f96dcSApple OSS Distributions static bool
102*2c2f96dcSApple OSS Distributions workq_schedule_delayed_thread_creation(struct workqueue *wq, int flags);
103*2c2f96dcSApple OSS Distributions 
104*2c2f96dcSApple OSS Distributions static inline void
105*2c2f96dcSApple OSS Distributions workq_lock_spin(struct workqueue *wq);
106*2c2f96dcSApple OSS Distributions 
107*2c2f96dcSApple OSS Distributions static inline void
108*2c2f96dcSApple OSS Distributions workq_unlock(struct workqueue *wq);
109*2c2f96dcSApple OSS Distributions 
110*2c2f96dcSApple OSS Distributions #pragma mark globals
111*2c2f96dcSApple OSS Distributions 
112*2c2f96dcSApple OSS Distributions struct workq_usec_var {
113*2c2f96dcSApple OSS Distributions 	uint32_t usecs;
114*2c2f96dcSApple OSS Distributions 	uint64_t abstime;
115*2c2f96dcSApple OSS Distributions };
116*2c2f96dcSApple OSS Distributions 
117*2c2f96dcSApple OSS Distributions #define WORKQ_SYSCTL_USECS(var, init) \
118*2c2f96dcSApple OSS Distributions 	        static struct workq_usec_var var = { .usecs = init }; \
119*2c2f96dcSApple OSS Distributions 	        SYSCTL_OID(_kern, OID_AUTO, var##_usecs, \
120*2c2f96dcSApple OSS Distributions 	                        CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &var, 0, \
121*2c2f96dcSApple OSS Distributions 	                        workq_sysctl_handle_usecs, "I", "")
122*2c2f96dcSApple OSS Distributions 
123*2c2f96dcSApple OSS Distributions static LCK_GRP_DECLARE(workq_lck_grp, "workq");
124*2c2f96dcSApple OSS Distributions os_refgrp_decl(static, workq_refgrp, "workq", NULL);
125*2c2f96dcSApple OSS Distributions 
126*2c2f96dcSApple OSS Distributions static ZONE_DEFINE(workq_zone_workqueue, "workq.wq",
127*2c2f96dcSApple OSS Distributions     sizeof(struct workqueue), ZC_NONE);
128*2c2f96dcSApple OSS Distributions static ZONE_DEFINE(workq_zone_threadreq, "workq.threadreq",
129*2c2f96dcSApple OSS Distributions     sizeof(struct workq_threadreq_s), ZC_CACHING);
130*2c2f96dcSApple OSS Distributions 
131*2c2f96dcSApple OSS Distributions static struct mpsc_daemon_queue workq_deallocate_queue;
132*2c2f96dcSApple OSS Distributions 
133*2c2f96dcSApple OSS Distributions WORKQ_SYSCTL_USECS(wq_stalled_window, WQ_STALLED_WINDOW_USECS);
134*2c2f96dcSApple OSS Distributions WORKQ_SYSCTL_USECS(wq_reduce_pool_window, WQ_REDUCE_POOL_WINDOW_USECS);
135*2c2f96dcSApple OSS Distributions WORKQ_SYSCTL_USECS(wq_max_timer_interval, WQ_MAX_TIMER_INTERVAL_USECS);
136*2c2f96dcSApple OSS Distributions static uint32_t wq_max_threads              = WORKQUEUE_MAXTHREADS;
137*2c2f96dcSApple OSS Distributions static uint32_t wq_max_constrained_threads  = WORKQUEUE_MAXTHREADS / 8;
138*2c2f96dcSApple OSS Distributions static uint32_t wq_init_constrained_limit   = 1;
139*2c2f96dcSApple OSS Distributions static uint16_t wq_death_max_load;
140*2c2f96dcSApple OSS Distributions static uint32_t wq_max_parallelism[WORKQ_NUM_QOS_BUCKETS];
141*2c2f96dcSApple OSS Distributions 
142*2c2f96dcSApple OSS Distributions /*
143*2c2f96dcSApple OSS Distributions  * This is not a hard limit but the max size we want to aim to hit across the
144*2c2f96dcSApple OSS Distributions  * entire cooperative pool. We can oversubscribe the pool due to non-cooperative
145*2c2f96dcSApple OSS Distributions  * workers and the max we will oversubscribe the pool by, is a total of
146*2c2f96dcSApple OSS Distributions  * wq_max_cooperative_threads * WORKQ_NUM_QOS_BUCKETS.
147*2c2f96dcSApple OSS Distributions  */
148*2c2f96dcSApple OSS Distributions static uint32_t wq_max_cooperative_threads;
149*2c2f96dcSApple OSS Distributions 
150*2c2f96dcSApple OSS Distributions static inline uint32_t
wq_cooperative_queue_max_size(struct workqueue * wq)151*2c2f96dcSApple OSS Distributions wq_cooperative_queue_max_size(struct workqueue *wq)
152*2c2f96dcSApple OSS Distributions {
153*2c2f96dcSApple OSS Distributions 	return wq->wq_cooperative_queue_has_limited_max_size ? 1 : wq_max_cooperative_threads;
154*2c2f96dcSApple OSS Distributions }
155*2c2f96dcSApple OSS Distributions 
156*2c2f96dcSApple OSS Distributions #pragma mark sysctls
157*2c2f96dcSApple OSS Distributions 
158*2c2f96dcSApple OSS Distributions static int
159*2c2f96dcSApple OSS Distributions workq_sysctl_handle_usecs SYSCTL_HANDLER_ARGS
160*2c2f96dcSApple OSS Distributions {
161*2c2f96dcSApple OSS Distributions #pragma unused(arg2)
162*2c2f96dcSApple OSS Distributions 	struct workq_usec_var *v = arg1;
163*2c2f96dcSApple OSS Distributions 	int error = sysctl_handle_int(oidp, &v->usecs, 0, req);
164*2c2f96dcSApple OSS Distributions 	if (error || !req->newptr) {
165*2c2f96dcSApple OSS Distributions 		return error;
166*2c2f96dcSApple OSS Distributions 	}
167*2c2f96dcSApple OSS Distributions 	clock_interval_to_absolutetime_interval(v->usecs, NSEC_PER_USEC,
168*2c2f96dcSApple OSS Distributions 	    &v->abstime);
169*2c2f96dcSApple OSS Distributions 	return 0;
170*2c2f96dcSApple OSS Distributions }
171*2c2f96dcSApple OSS Distributions 
172*2c2f96dcSApple OSS Distributions SYSCTL_INT(_kern, OID_AUTO, wq_max_threads, CTLFLAG_RW | CTLFLAG_LOCKED,
173*2c2f96dcSApple OSS Distributions     &wq_max_threads, 0, "");
174*2c2f96dcSApple OSS Distributions 
175*2c2f96dcSApple OSS Distributions SYSCTL_INT(_kern, OID_AUTO, wq_max_constrained_threads, CTLFLAG_RW | CTLFLAG_LOCKED,
176*2c2f96dcSApple OSS Distributions     &wq_max_constrained_threads, 0, "");
177*2c2f96dcSApple OSS Distributions 
178*2c2f96dcSApple OSS Distributions static int
179*2c2f96dcSApple OSS Distributions wq_limit_cooperative_threads_for_proc SYSCTL_HANDLER_ARGS
180*2c2f96dcSApple OSS Distributions {
181*2c2f96dcSApple OSS Distributions #pragma unused(arg1, arg2, oidp)
182*2c2f96dcSApple OSS Distributions 	int input_pool_size = 0;
183*2c2f96dcSApple OSS Distributions 	int changed;
184*2c2f96dcSApple OSS Distributions 	int error = 0;
185*2c2f96dcSApple OSS Distributions 
186*2c2f96dcSApple OSS Distributions 	error = sysctl_io_number(req, 0, sizeof(int), &input_pool_size, &changed);
187*2c2f96dcSApple OSS Distributions 	if (error || !changed) {
188*2c2f96dcSApple OSS Distributions 		return error;
189*2c2f96dcSApple OSS Distributions 	}
190*2c2f96dcSApple OSS Distributions 
191*2c2f96dcSApple OSS Distributions #define WQ_COOPERATIVE_POOL_SIZE_DEFAULT 0
192*2c2f96dcSApple OSS Distributions #define WQ_COOPERATIVE_POOL_SIZE_STRICT_PER_QOS -1
193*2c2f96dcSApple OSS Distributions /* Not available currently, but sysctl interface is designed to allow these
194*2c2f96dcSApple OSS Distributions  * extra parameters:
195*2c2f96dcSApple OSS Distributions  *		WQ_COOPERATIVE_POOL_SIZE_STRICT : -2 (across all bucket)
196*2c2f96dcSApple OSS Distributions  *		WQ_COOPERATIVE_POOL_SIZE_CUSTOM : [1, 512]
197*2c2f96dcSApple OSS Distributions  */
198*2c2f96dcSApple OSS Distributions 
199*2c2f96dcSApple OSS Distributions 	if (input_pool_size != WQ_COOPERATIVE_POOL_SIZE_DEFAULT
200*2c2f96dcSApple OSS Distributions 	    && input_pool_size != WQ_COOPERATIVE_POOL_SIZE_STRICT_PER_QOS) {
201*2c2f96dcSApple OSS Distributions 		error = EINVAL;
202*2c2f96dcSApple OSS Distributions 		goto out;
203*2c2f96dcSApple OSS Distributions 	}
204*2c2f96dcSApple OSS Distributions 
205*2c2f96dcSApple OSS Distributions 	proc_t p = req->p;
206*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = proc_get_wqptr(p);
207*2c2f96dcSApple OSS Distributions 
208*2c2f96dcSApple OSS Distributions 	if (wq != NULL) {
209*2c2f96dcSApple OSS Distributions 		workq_lock_spin(wq);
210*2c2f96dcSApple OSS Distributions 		if (wq->wq_reqcount > 0 || wq->wq_nthreads > 0) {
211*2c2f96dcSApple OSS Distributions 			// Hackily enforce that the workqueue is still new (no requests or
212*2c2f96dcSApple OSS Distributions 			// threads)
213*2c2f96dcSApple OSS Distributions 			error = ENOTSUP;
214*2c2f96dcSApple OSS Distributions 		} else {
215*2c2f96dcSApple OSS Distributions 			wq->wq_cooperative_queue_has_limited_max_size = (input_pool_size == WQ_COOPERATIVE_POOL_SIZE_STRICT_PER_QOS);
216*2c2f96dcSApple OSS Distributions 		}
217*2c2f96dcSApple OSS Distributions 		workq_unlock(wq);
218*2c2f96dcSApple OSS Distributions 	} else {
219*2c2f96dcSApple OSS Distributions 		/* This process has no workqueue, calling this syctl makes no sense */
220*2c2f96dcSApple OSS Distributions 		return ENOTSUP;
221*2c2f96dcSApple OSS Distributions 	}
222*2c2f96dcSApple OSS Distributions 
223*2c2f96dcSApple OSS Distributions out:
224*2c2f96dcSApple OSS Distributions 	return error;
225*2c2f96dcSApple OSS Distributions }
226*2c2f96dcSApple OSS Distributions 
227*2c2f96dcSApple OSS Distributions SYSCTL_PROC(_kern, OID_AUTO, wq_limit_cooperative_threads,
228*2c2f96dcSApple OSS Distributions     CTLFLAG_ANYBODY | CTLFLAG_MASKED | CTLFLAG_WR | CTLFLAG_LOCKED | CTLTYPE_INT, 0, 0,
229*2c2f96dcSApple OSS Distributions     wq_limit_cooperative_threads_for_proc,
230*2c2f96dcSApple OSS Distributions     "I", "Modify the max pool size of the cooperative pool");
231*2c2f96dcSApple OSS Distributions 
232*2c2f96dcSApple OSS Distributions #pragma mark p_wqptr
233*2c2f96dcSApple OSS Distributions 
234*2c2f96dcSApple OSS Distributions #define WQPTR_IS_INITING_VALUE ((struct workqueue *)~(uintptr_t)0)
235*2c2f96dcSApple OSS Distributions 
236*2c2f96dcSApple OSS Distributions static struct workqueue *
proc_get_wqptr_fast(struct proc * p)237*2c2f96dcSApple OSS Distributions proc_get_wqptr_fast(struct proc *p)
238*2c2f96dcSApple OSS Distributions {
239*2c2f96dcSApple OSS Distributions 	return os_atomic_load(&p->p_wqptr, relaxed);
240*2c2f96dcSApple OSS Distributions }
241*2c2f96dcSApple OSS Distributions 
242*2c2f96dcSApple OSS Distributions struct workqueue *
proc_get_wqptr(struct proc * p)243*2c2f96dcSApple OSS Distributions proc_get_wqptr(struct proc *p)
244*2c2f96dcSApple OSS Distributions {
245*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = proc_get_wqptr_fast(p);
246*2c2f96dcSApple OSS Distributions 	return wq == WQPTR_IS_INITING_VALUE ? NULL : wq;
247*2c2f96dcSApple OSS Distributions }
248*2c2f96dcSApple OSS Distributions 
249*2c2f96dcSApple OSS Distributions static void
proc_set_wqptr(struct proc * p,struct workqueue * wq)250*2c2f96dcSApple OSS Distributions proc_set_wqptr(struct proc *p, struct workqueue *wq)
251*2c2f96dcSApple OSS Distributions {
252*2c2f96dcSApple OSS Distributions 	wq = os_atomic_xchg(&p->p_wqptr, wq, release);
253*2c2f96dcSApple OSS Distributions 	if (wq == WQPTR_IS_INITING_VALUE) {
254*2c2f96dcSApple OSS Distributions 		proc_lock(p);
255*2c2f96dcSApple OSS Distributions 		thread_wakeup(&p->p_wqptr);
256*2c2f96dcSApple OSS Distributions 		proc_unlock(p);
257*2c2f96dcSApple OSS Distributions 	}
258*2c2f96dcSApple OSS Distributions }
259*2c2f96dcSApple OSS Distributions 
260*2c2f96dcSApple OSS Distributions static bool
proc_init_wqptr_or_wait(struct proc * p)261*2c2f96dcSApple OSS Distributions proc_init_wqptr_or_wait(struct proc *p)
262*2c2f96dcSApple OSS Distributions {
263*2c2f96dcSApple OSS Distributions 	struct workqueue *wq;
264*2c2f96dcSApple OSS Distributions 
265*2c2f96dcSApple OSS Distributions 	proc_lock(p);
266*2c2f96dcSApple OSS Distributions 	wq = os_atomic_load(&p->p_wqptr, relaxed);
267*2c2f96dcSApple OSS Distributions 
268*2c2f96dcSApple OSS Distributions 	if (wq == NULL) {
269*2c2f96dcSApple OSS Distributions 		os_atomic_store(&p->p_wqptr, WQPTR_IS_INITING_VALUE, relaxed);
270*2c2f96dcSApple OSS Distributions 		proc_unlock(p);
271*2c2f96dcSApple OSS Distributions 		return true;
272*2c2f96dcSApple OSS Distributions 	}
273*2c2f96dcSApple OSS Distributions 
274*2c2f96dcSApple OSS Distributions 	if (wq == WQPTR_IS_INITING_VALUE) {
275*2c2f96dcSApple OSS Distributions 		assert_wait(&p->p_wqptr, THREAD_UNINT);
276*2c2f96dcSApple OSS Distributions 		proc_unlock(p);
277*2c2f96dcSApple OSS Distributions 		thread_block(THREAD_CONTINUE_NULL);
278*2c2f96dcSApple OSS Distributions 	} else {
279*2c2f96dcSApple OSS Distributions 		proc_unlock(p);
280*2c2f96dcSApple OSS Distributions 	}
281*2c2f96dcSApple OSS Distributions 	return false;
282*2c2f96dcSApple OSS Distributions }
283*2c2f96dcSApple OSS Distributions 
284*2c2f96dcSApple OSS Distributions static inline event_t
workq_parked_wait_event(struct uthread * uth)285*2c2f96dcSApple OSS Distributions workq_parked_wait_event(struct uthread *uth)
286*2c2f96dcSApple OSS Distributions {
287*2c2f96dcSApple OSS Distributions 	return (event_t)&uth->uu_workq_stackaddr;
288*2c2f96dcSApple OSS Distributions }
289*2c2f96dcSApple OSS Distributions 
290*2c2f96dcSApple OSS Distributions static inline void
workq_thread_wakeup(struct uthread * uth)291*2c2f96dcSApple OSS Distributions workq_thread_wakeup(struct uthread *uth)
292*2c2f96dcSApple OSS Distributions {
293*2c2f96dcSApple OSS Distributions 	thread_wakeup_thread(workq_parked_wait_event(uth), get_machthread(uth));
294*2c2f96dcSApple OSS Distributions }
295*2c2f96dcSApple OSS Distributions 
296*2c2f96dcSApple OSS Distributions #pragma mark wq_thactive
297*2c2f96dcSApple OSS Distributions 
298*2c2f96dcSApple OSS Distributions #if defined(__LP64__)
299*2c2f96dcSApple OSS Distributions // Layout is:
300*2c2f96dcSApple OSS Distributions //   127 - 115 : 13 bits of zeroes
301*2c2f96dcSApple OSS Distributions //   114 - 112 : best QoS among all pending constrained requests
302*2c2f96dcSApple OSS Distributions //   111 -   0 : MGR, AUI, UI, IN, DF, UT, BG+MT buckets every 16 bits
303*2c2f96dcSApple OSS Distributions #define WQ_THACTIVE_BUCKET_WIDTH 16
304*2c2f96dcSApple OSS Distributions #define WQ_THACTIVE_QOS_SHIFT    (7 * WQ_THACTIVE_BUCKET_WIDTH)
305*2c2f96dcSApple OSS Distributions #else
306*2c2f96dcSApple OSS Distributions // Layout is:
307*2c2f96dcSApple OSS Distributions //   63 - 61 : best QoS among all pending constrained requests
308*2c2f96dcSApple OSS Distributions //   60      : Manager bucket (0 or 1)
309*2c2f96dcSApple OSS Distributions //   59 -  0 : AUI, UI, IN, DF, UT, BG+MT buckets every 10 bits
310*2c2f96dcSApple OSS Distributions #define WQ_THACTIVE_BUCKET_WIDTH 10
311*2c2f96dcSApple OSS Distributions #define WQ_THACTIVE_QOS_SHIFT    (6 * WQ_THACTIVE_BUCKET_WIDTH + 1)
312*2c2f96dcSApple OSS Distributions #endif
313*2c2f96dcSApple OSS Distributions #define WQ_THACTIVE_BUCKET_MASK  ((1U << WQ_THACTIVE_BUCKET_WIDTH) - 1)
314*2c2f96dcSApple OSS Distributions #define WQ_THACTIVE_BUCKET_HALF  (1U << (WQ_THACTIVE_BUCKET_WIDTH - 1))
315*2c2f96dcSApple OSS Distributions 
316*2c2f96dcSApple OSS Distributions static_assert(sizeof(wq_thactive_t) * CHAR_BIT - WQ_THACTIVE_QOS_SHIFT >= 3,
317*2c2f96dcSApple OSS Distributions     "Make sure we have space to encode a QoS");
318*2c2f96dcSApple OSS Distributions 
319*2c2f96dcSApple OSS Distributions static inline wq_thactive_t
_wq_thactive(struct workqueue * wq)320*2c2f96dcSApple OSS Distributions _wq_thactive(struct workqueue *wq)
321*2c2f96dcSApple OSS Distributions {
322*2c2f96dcSApple OSS Distributions 	return os_atomic_load_wide(&wq->wq_thactive, relaxed);
323*2c2f96dcSApple OSS Distributions }
324*2c2f96dcSApple OSS Distributions 
325*2c2f96dcSApple OSS Distributions static inline uint8_t
_wq_bucket(thread_qos_t qos)326*2c2f96dcSApple OSS Distributions _wq_bucket(thread_qos_t qos)
327*2c2f96dcSApple OSS Distributions {
328*2c2f96dcSApple OSS Distributions 	// Map both BG and MT to the same bucket by over-shifting down and
329*2c2f96dcSApple OSS Distributions 	// clamping MT and BG together.
330*2c2f96dcSApple OSS Distributions 	switch (qos) {
331*2c2f96dcSApple OSS Distributions 	case THREAD_QOS_MAINTENANCE:
332*2c2f96dcSApple OSS Distributions 		return 0;
333*2c2f96dcSApple OSS Distributions 	default:
334*2c2f96dcSApple OSS Distributions 		return qos - 2;
335*2c2f96dcSApple OSS Distributions 	}
336*2c2f96dcSApple OSS Distributions }
337*2c2f96dcSApple OSS Distributions 
338*2c2f96dcSApple OSS Distributions #define WQ_THACTIVE_BEST_CONSTRAINED_REQ_QOS(tha) \
339*2c2f96dcSApple OSS Distributions 	        ((thread_qos_t)((tha) >> WQ_THACTIVE_QOS_SHIFT))
340*2c2f96dcSApple OSS Distributions 
341*2c2f96dcSApple OSS Distributions static inline thread_qos_t
_wq_thactive_best_constrained_req_qos(struct workqueue * wq)342*2c2f96dcSApple OSS Distributions _wq_thactive_best_constrained_req_qos(struct workqueue *wq)
343*2c2f96dcSApple OSS Distributions {
344*2c2f96dcSApple OSS Distributions 	// Avoid expensive atomic operations: the three bits we're loading are in
345*2c2f96dcSApple OSS Distributions 	// a single byte, and always updated under the workqueue lock
346*2c2f96dcSApple OSS Distributions 	wq_thactive_t v = *(wq_thactive_t *)&wq->wq_thactive;
347*2c2f96dcSApple OSS Distributions 	return WQ_THACTIVE_BEST_CONSTRAINED_REQ_QOS(v);
348*2c2f96dcSApple OSS Distributions }
349*2c2f96dcSApple OSS Distributions 
350*2c2f96dcSApple OSS Distributions static void
_wq_thactive_refresh_best_constrained_req_qos(struct workqueue * wq)351*2c2f96dcSApple OSS Distributions _wq_thactive_refresh_best_constrained_req_qos(struct workqueue *wq)
352*2c2f96dcSApple OSS Distributions {
353*2c2f96dcSApple OSS Distributions 	thread_qos_t old_qos, new_qos;
354*2c2f96dcSApple OSS Distributions 	workq_threadreq_t req;
355*2c2f96dcSApple OSS Distributions 
356*2c2f96dcSApple OSS Distributions 	req = priority_queue_max(&wq->wq_constrained_queue,
357*2c2f96dcSApple OSS Distributions 	    struct workq_threadreq_s, tr_entry);
358*2c2f96dcSApple OSS Distributions 	new_qos = req ? req->tr_qos : THREAD_QOS_UNSPECIFIED;
359*2c2f96dcSApple OSS Distributions 	old_qos = _wq_thactive_best_constrained_req_qos(wq);
360*2c2f96dcSApple OSS Distributions 	if (old_qos != new_qos) {
361*2c2f96dcSApple OSS Distributions 		long delta = (long)new_qos - (long)old_qos;
362*2c2f96dcSApple OSS Distributions 		wq_thactive_t v = (wq_thactive_t)delta << WQ_THACTIVE_QOS_SHIFT;
363*2c2f96dcSApple OSS Distributions 		/*
364*2c2f96dcSApple OSS Distributions 		 * We can do an atomic add relative to the initial load because updates
365*2c2f96dcSApple OSS Distributions 		 * to this qos are always serialized under the workqueue lock.
366*2c2f96dcSApple OSS Distributions 		 */
367*2c2f96dcSApple OSS Distributions 		v = os_atomic_add(&wq->wq_thactive, v, relaxed);
368*2c2f96dcSApple OSS Distributions #ifdef __LP64__
369*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_thactive_update, wq, (uint64_t)v,
370*2c2f96dcSApple OSS Distributions 		    (uint64_t)(v >> 64), 0);
371*2c2f96dcSApple OSS Distributions #else
372*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_thactive_update, wq, v, 0, 0);
373*2c2f96dcSApple OSS Distributions #endif
374*2c2f96dcSApple OSS Distributions 	}
375*2c2f96dcSApple OSS Distributions }
376*2c2f96dcSApple OSS Distributions 
377*2c2f96dcSApple OSS Distributions static inline wq_thactive_t
_wq_thactive_offset_for_qos(thread_qos_t qos)378*2c2f96dcSApple OSS Distributions _wq_thactive_offset_for_qos(thread_qos_t qos)
379*2c2f96dcSApple OSS Distributions {
380*2c2f96dcSApple OSS Distributions 	uint8_t bucket = _wq_bucket(qos);
381*2c2f96dcSApple OSS Distributions 	__builtin_assume(bucket < WORKQ_NUM_BUCKETS);
382*2c2f96dcSApple OSS Distributions 	return (wq_thactive_t)1 << (bucket * WQ_THACTIVE_BUCKET_WIDTH);
383*2c2f96dcSApple OSS Distributions }
384*2c2f96dcSApple OSS Distributions 
385*2c2f96dcSApple OSS Distributions static inline wq_thactive_t
_wq_thactive_inc(struct workqueue * wq,thread_qos_t qos)386*2c2f96dcSApple OSS Distributions _wq_thactive_inc(struct workqueue *wq, thread_qos_t qos)
387*2c2f96dcSApple OSS Distributions {
388*2c2f96dcSApple OSS Distributions 	wq_thactive_t v = _wq_thactive_offset_for_qos(qos);
389*2c2f96dcSApple OSS Distributions 	return os_atomic_add_orig(&wq->wq_thactive, v, relaxed);
390*2c2f96dcSApple OSS Distributions }
391*2c2f96dcSApple OSS Distributions 
392*2c2f96dcSApple OSS Distributions static inline wq_thactive_t
_wq_thactive_dec(struct workqueue * wq,thread_qos_t qos)393*2c2f96dcSApple OSS Distributions _wq_thactive_dec(struct workqueue *wq, thread_qos_t qos)
394*2c2f96dcSApple OSS Distributions {
395*2c2f96dcSApple OSS Distributions 	wq_thactive_t v = _wq_thactive_offset_for_qos(qos);
396*2c2f96dcSApple OSS Distributions 	return os_atomic_sub_orig(&wq->wq_thactive, v, relaxed);
397*2c2f96dcSApple OSS Distributions }
398*2c2f96dcSApple OSS Distributions 
399*2c2f96dcSApple OSS Distributions static inline void
_wq_thactive_move(struct workqueue * wq,thread_qos_t old_qos,thread_qos_t new_qos)400*2c2f96dcSApple OSS Distributions _wq_thactive_move(struct workqueue *wq,
401*2c2f96dcSApple OSS Distributions     thread_qos_t old_qos, thread_qos_t new_qos)
402*2c2f96dcSApple OSS Distributions {
403*2c2f96dcSApple OSS Distributions 	wq_thactive_t v = _wq_thactive_offset_for_qos(new_qos) -
404*2c2f96dcSApple OSS Distributions 	    _wq_thactive_offset_for_qos(old_qos);
405*2c2f96dcSApple OSS Distributions 	os_atomic_add(&wq->wq_thactive, v, relaxed);
406*2c2f96dcSApple OSS Distributions 	wq->wq_thscheduled_count[_wq_bucket(old_qos)]--;
407*2c2f96dcSApple OSS Distributions 	wq->wq_thscheduled_count[_wq_bucket(new_qos)]++;
408*2c2f96dcSApple OSS Distributions }
409*2c2f96dcSApple OSS Distributions 
410*2c2f96dcSApple OSS Distributions static inline uint32_t
_wq_thactive_aggregate_downto_qos(struct workqueue * wq,wq_thactive_t v,thread_qos_t qos,uint32_t * busycount,uint32_t * max_busycount)411*2c2f96dcSApple OSS Distributions _wq_thactive_aggregate_downto_qos(struct workqueue *wq, wq_thactive_t v,
412*2c2f96dcSApple OSS Distributions     thread_qos_t qos, uint32_t *busycount, uint32_t *max_busycount)
413*2c2f96dcSApple OSS Distributions {
414*2c2f96dcSApple OSS Distributions 	uint32_t count = 0, active;
415*2c2f96dcSApple OSS Distributions 	uint64_t curtime;
416*2c2f96dcSApple OSS Distributions 
417*2c2f96dcSApple OSS Distributions 	assert(WORKQ_THREAD_QOS_MIN <= qos && qos <= WORKQ_THREAD_QOS_MAX);
418*2c2f96dcSApple OSS Distributions 
419*2c2f96dcSApple OSS Distributions 	if (busycount) {
420*2c2f96dcSApple OSS Distributions 		curtime = mach_absolute_time();
421*2c2f96dcSApple OSS Distributions 		*busycount = 0;
422*2c2f96dcSApple OSS Distributions 	}
423*2c2f96dcSApple OSS Distributions 	if (max_busycount) {
424*2c2f96dcSApple OSS Distributions 		*max_busycount = THREAD_QOS_LAST - qos;
425*2c2f96dcSApple OSS Distributions 	}
426*2c2f96dcSApple OSS Distributions 
427*2c2f96dcSApple OSS Distributions 	uint8_t i = _wq_bucket(qos);
428*2c2f96dcSApple OSS Distributions 	v >>= i * WQ_THACTIVE_BUCKET_WIDTH;
429*2c2f96dcSApple OSS Distributions 	for (; i < WORKQ_NUM_QOS_BUCKETS; i++, v >>= WQ_THACTIVE_BUCKET_WIDTH) {
430*2c2f96dcSApple OSS Distributions 		active = v & WQ_THACTIVE_BUCKET_MASK;
431*2c2f96dcSApple OSS Distributions 		count += active;
432*2c2f96dcSApple OSS Distributions 
433*2c2f96dcSApple OSS Distributions 		if (busycount && wq->wq_thscheduled_count[i] > active) {
434*2c2f96dcSApple OSS Distributions 			if (workq_thread_is_busy(curtime, &wq->wq_lastblocked_ts[i])) {
435*2c2f96dcSApple OSS Distributions 				/*
436*2c2f96dcSApple OSS Distributions 				 * We only consider the last blocked thread for a given bucket
437*2c2f96dcSApple OSS Distributions 				 * as busy because we don't want to take the list lock in each
438*2c2f96dcSApple OSS Distributions 				 * sched callback. However this is an approximation that could
439*2c2f96dcSApple OSS Distributions 				 * contribute to thread creation storms.
440*2c2f96dcSApple OSS Distributions 				 */
441*2c2f96dcSApple OSS Distributions 				(*busycount)++;
442*2c2f96dcSApple OSS Distributions 			}
443*2c2f96dcSApple OSS Distributions 		}
444*2c2f96dcSApple OSS Distributions 	}
445*2c2f96dcSApple OSS Distributions 
446*2c2f96dcSApple OSS Distributions 	return count;
447*2c2f96dcSApple OSS Distributions }
448*2c2f96dcSApple OSS Distributions 
449*2c2f96dcSApple OSS Distributions /* The input qos here should be the requested QoS of the thread, not accounting
450*2c2f96dcSApple OSS Distributions  * for any overrides */
451*2c2f96dcSApple OSS Distributions static inline void
_wq_cooperative_queue_scheduled_count_dec(struct workqueue * wq,thread_qos_t qos)452*2c2f96dcSApple OSS Distributions _wq_cooperative_queue_scheduled_count_dec(struct workqueue *wq, thread_qos_t qos)
453*2c2f96dcSApple OSS Distributions {
454*2c2f96dcSApple OSS Distributions 	__assert_only uint8_t old_scheduled_count = wq->wq_cooperative_queue_scheduled_count[_wq_bucket(qos)]--;
455*2c2f96dcSApple OSS Distributions 	assert(old_scheduled_count > 0);
456*2c2f96dcSApple OSS Distributions }
457*2c2f96dcSApple OSS Distributions 
458*2c2f96dcSApple OSS Distributions /* The input qos here should be the requested QoS of the thread, not accounting
459*2c2f96dcSApple OSS Distributions  * for any overrides */
460*2c2f96dcSApple OSS Distributions static inline void
_wq_cooperative_queue_scheduled_count_inc(struct workqueue * wq,thread_qos_t qos)461*2c2f96dcSApple OSS Distributions _wq_cooperative_queue_scheduled_count_inc(struct workqueue *wq, thread_qos_t qos)
462*2c2f96dcSApple OSS Distributions {
463*2c2f96dcSApple OSS Distributions 	__assert_only uint8_t old_scheduled_count = wq->wq_cooperative_queue_scheduled_count[_wq_bucket(qos)]++;
464*2c2f96dcSApple OSS Distributions 	assert(old_scheduled_count < UINT8_MAX);
465*2c2f96dcSApple OSS Distributions }
466*2c2f96dcSApple OSS Distributions 
467*2c2f96dcSApple OSS Distributions #pragma mark wq_flags
468*2c2f96dcSApple OSS Distributions 
469*2c2f96dcSApple OSS Distributions static inline uint32_t
_wq_flags(struct workqueue * wq)470*2c2f96dcSApple OSS Distributions _wq_flags(struct workqueue *wq)
471*2c2f96dcSApple OSS Distributions {
472*2c2f96dcSApple OSS Distributions 	return os_atomic_load(&wq->wq_flags, relaxed);
473*2c2f96dcSApple OSS Distributions }
474*2c2f96dcSApple OSS Distributions 
475*2c2f96dcSApple OSS Distributions static inline bool
_wq_exiting(struct workqueue * wq)476*2c2f96dcSApple OSS Distributions _wq_exiting(struct workqueue *wq)
477*2c2f96dcSApple OSS Distributions {
478*2c2f96dcSApple OSS Distributions 	return _wq_flags(wq) & WQ_EXITING;
479*2c2f96dcSApple OSS Distributions }
480*2c2f96dcSApple OSS Distributions 
481*2c2f96dcSApple OSS Distributions bool
workq_is_exiting(struct proc * p)482*2c2f96dcSApple OSS Distributions workq_is_exiting(struct proc *p)
483*2c2f96dcSApple OSS Distributions {
484*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = proc_get_wqptr(p);
485*2c2f96dcSApple OSS Distributions 	return !wq || _wq_exiting(wq);
486*2c2f96dcSApple OSS Distributions }
487*2c2f96dcSApple OSS Distributions 
488*2c2f96dcSApple OSS Distributions 
489*2c2f96dcSApple OSS Distributions #pragma mark workqueue lock
490*2c2f96dcSApple OSS Distributions 
491*2c2f96dcSApple OSS Distributions static bool
workq_lock_is_acquired_kdp(struct workqueue * wq)492*2c2f96dcSApple OSS Distributions workq_lock_is_acquired_kdp(struct workqueue *wq)
493*2c2f96dcSApple OSS Distributions {
494*2c2f96dcSApple OSS Distributions 	return kdp_lck_ticket_is_acquired(&wq->wq_lock);
495*2c2f96dcSApple OSS Distributions }
496*2c2f96dcSApple OSS Distributions 
497*2c2f96dcSApple OSS Distributions static inline void
workq_lock_spin(struct workqueue * wq)498*2c2f96dcSApple OSS Distributions workq_lock_spin(struct workqueue *wq)
499*2c2f96dcSApple OSS Distributions {
500*2c2f96dcSApple OSS Distributions 	lck_ticket_lock(&wq->wq_lock, &workq_lck_grp);
501*2c2f96dcSApple OSS Distributions }
502*2c2f96dcSApple OSS Distributions 
503*2c2f96dcSApple OSS Distributions static inline void
workq_lock_held(struct workqueue * wq)504*2c2f96dcSApple OSS Distributions workq_lock_held(struct workqueue *wq)
505*2c2f96dcSApple OSS Distributions {
506*2c2f96dcSApple OSS Distributions 	LCK_TICKET_ASSERT_OWNED(&wq->wq_lock);
507*2c2f96dcSApple OSS Distributions }
508*2c2f96dcSApple OSS Distributions 
509*2c2f96dcSApple OSS Distributions static inline bool
workq_lock_try(struct workqueue * wq)510*2c2f96dcSApple OSS Distributions workq_lock_try(struct workqueue *wq)
511*2c2f96dcSApple OSS Distributions {
512*2c2f96dcSApple OSS Distributions 	return lck_ticket_lock_try(&wq->wq_lock, &workq_lck_grp);
513*2c2f96dcSApple OSS Distributions }
514*2c2f96dcSApple OSS Distributions 
515*2c2f96dcSApple OSS Distributions static inline void
workq_unlock(struct workqueue * wq)516*2c2f96dcSApple OSS Distributions workq_unlock(struct workqueue *wq)
517*2c2f96dcSApple OSS Distributions {
518*2c2f96dcSApple OSS Distributions 	lck_ticket_unlock(&wq->wq_lock);
519*2c2f96dcSApple OSS Distributions }
520*2c2f96dcSApple OSS Distributions 
521*2c2f96dcSApple OSS Distributions #pragma mark idle thread lists
522*2c2f96dcSApple OSS Distributions 
523*2c2f96dcSApple OSS Distributions #define WORKQ_POLICY_INIT(qos) \
524*2c2f96dcSApple OSS Distributions 	        (struct uu_workq_policy){ .qos_req = qos, .qos_bucket = qos }
525*2c2f96dcSApple OSS Distributions 
526*2c2f96dcSApple OSS Distributions static inline thread_qos_t
workq_pri_bucket(struct uu_workq_policy req)527*2c2f96dcSApple OSS Distributions workq_pri_bucket(struct uu_workq_policy req)
528*2c2f96dcSApple OSS Distributions {
529*2c2f96dcSApple OSS Distributions 	return MAX(MAX(req.qos_req, req.qos_max), req.qos_override);
530*2c2f96dcSApple OSS Distributions }
531*2c2f96dcSApple OSS Distributions 
532*2c2f96dcSApple OSS Distributions static inline thread_qos_t
workq_pri_override(struct uu_workq_policy req)533*2c2f96dcSApple OSS Distributions workq_pri_override(struct uu_workq_policy req)
534*2c2f96dcSApple OSS Distributions {
535*2c2f96dcSApple OSS Distributions 	return MAX(workq_pri_bucket(req), req.qos_bucket);
536*2c2f96dcSApple OSS Distributions }
537*2c2f96dcSApple OSS Distributions 
538*2c2f96dcSApple OSS Distributions static inline bool
workq_thread_needs_params_change(workq_threadreq_t req,struct uthread * uth)539*2c2f96dcSApple OSS Distributions workq_thread_needs_params_change(workq_threadreq_t req, struct uthread *uth)
540*2c2f96dcSApple OSS Distributions {
541*2c2f96dcSApple OSS Distributions 	workq_threadreq_param_t cur_trp, req_trp = { };
542*2c2f96dcSApple OSS Distributions 
543*2c2f96dcSApple OSS Distributions 	cur_trp.trp_value = uth->uu_save.uus_workq_park_data.workloop_params;
544*2c2f96dcSApple OSS Distributions 	if (req->tr_flags & WORKQ_TR_FLAG_WL_PARAMS) {
545*2c2f96dcSApple OSS Distributions 		req_trp = kqueue_threadreq_workloop_param(req);
546*2c2f96dcSApple OSS Distributions 	}
547*2c2f96dcSApple OSS Distributions 
548*2c2f96dcSApple OSS Distributions 	/*
549*2c2f96dcSApple OSS Distributions 	 * CPU percent flags are handled separately to policy changes, so ignore
550*2c2f96dcSApple OSS Distributions 	 * them for all of these checks.
551*2c2f96dcSApple OSS Distributions 	 */
552*2c2f96dcSApple OSS Distributions 	uint16_t cur_flags = (cur_trp.trp_flags & ~TRP_CPUPERCENT);
553*2c2f96dcSApple OSS Distributions 	uint16_t req_flags = (req_trp.trp_flags & ~TRP_CPUPERCENT);
554*2c2f96dcSApple OSS Distributions 
555*2c2f96dcSApple OSS Distributions 	if (!req_flags && !cur_flags) {
556*2c2f96dcSApple OSS Distributions 		return false;
557*2c2f96dcSApple OSS Distributions 	}
558*2c2f96dcSApple OSS Distributions 
559*2c2f96dcSApple OSS Distributions 	if (req_flags != cur_flags) {
560*2c2f96dcSApple OSS Distributions 		return true;
561*2c2f96dcSApple OSS Distributions 	}
562*2c2f96dcSApple OSS Distributions 
563*2c2f96dcSApple OSS Distributions 	if ((req_flags & TRP_PRIORITY) && req_trp.trp_pri != cur_trp.trp_pri) {
564*2c2f96dcSApple OSS Distributions 		return true;
565*2c2f96dcSApple OSS Distributions 	}
566*2c2f96dcSApple OSS Distributions 
567*2c2f96dcSApple OSS Distributions 	if ((req_flags & TRP_POLICY) && req_trp.trp_pol != cur_trp.trp_pol) {
568*2c2f96dcSApple OSS Distributions 		return true;
569*2c2f96dcSApple OSS Distributions 	}
570*2c2f96dcSApple OSS Distributions 
571*2c2f96dcSApple OSS Distributions 	return false;
572*2c2f96dcSApple OSS Distributions }
573*2c2f96dcSApple OSS Distributions 
574*2c2f96dcSApple OSS Distributions static inline bool
workq_thread_needs_priority_change(workq_threadreq_t req,struct uthread * uth)575*2c2f96dcSApple OSS Distributions workq_thread_needs_priority_change(workq_threadreq_t req, struct uthread *uth)
576*2c2f96dcSApple OSS Distributions {
577*2c2f96dcSApple OSS Distributions 	if (workq_thread_needs_params_change(req, uth)) {
578*2c2f96dcSApple OSS Distributions 		return true;
579*2c2f96dcSApple OSS Distributions 	}
580*2c2f96dcSApple OSS Distributions 
581*2c2f96dcSApple OSS Distributions 	if (req->tr_qos != workq_pri_override(uth->uu_workq_pri)) {
582*2c2f96dcSApple OSS Distributions 		return true;
583*2c2f96dcSApple OSS Distributions 	}
584*2c2f96dcSApple OSS Distributions 
585*2c2f96dcSApple OSS Distributions #if CONFIG_PREADOPT_TG
586*2c2f96dcSApple OSS Distributions 	thread_group_qos_t tg = kqr_preadopt_thread_group(req);
587*2c2f96dcSApple OSS Distributions 	if (KQWL_HAS_VALID_PREADOPTED_TG(tg)) {
588*2c2f96dcSApple OSS Distributions 		/*
589*2c2f96dcSApple OSS Distributions 		 * Ideally, we'd add check here to see if thread's preadopt TG is same
590*2c2f96dcSApple OSS Distributions 		 * as the thread requests's thread group and short circuit if that is
591*2c2f96dcSApple OSS Distributions 		 * the case. But in the interest of keeping the code clean and not
592*2c2f96dcSApple OSS Distributions 		 * taking the thread lock here, we're going to skip this. We will
593*2c2f96dcSApple OSS Distributions 		 * eventually shortcircuit once we try to set the preadoption thread
594*2c2f96dcSApple OSS Distributions 		 * group on the thread.
595*2c2f96dcSApple OSS Distributions 		 */
596*2c2f96dcSApple OSS Distributions 		return true;
597*2c2f96dcSApple OSS Distributions 	}
598*2c2f96dcSApple OSS Distributions #endif
599*2c2f96dcSApple OSS Distributions 
600*2c2f96dcSApple OSS Distributions 	return false;
601*2c2f96dcSApple OSS Distributions }
602*2c2f96dcSApple OSS Distributions 
603*2c2f96dcSApple OSS Distributions /* Input thread must be self. Called during self override, resetting overrides
604*2c2f96dcSApple OSS Distributions  * or while processing kevents
605*2c2f96dcSApple OSS Distributions  *
606*2c2f96dcSApple OSS Distributions  * Called with workq lock held. Sometimes also the thread mutex
607*2c2f96dcSApple OSS Distributions  */
608*2c2f96dcSApple OSS Distributions static void
workq_thread_update_bucket(proc_t p,struct workqueue * wq,struct uthread * uth,struct uu_workq_policy old_pri,struct uu_workq_policy new_pri,bool force_run)609*2c2f96dcSApple OSS Distributions workq_thread_update_bucket(proc_t p, struct workqueue *wq, struct uthread *uth,
610*2c2f96dcSApple OSS Distributions     struct uu_workq_policy old_pri, struct uu_workq_policy new_pri,
611*2c2f96dcSApple OSS Distributions     bool force_run)
612*2c2f96dcSApple OSS Distributions {
613*2c2f96dcSApple OSS Distributions 	assert(uth == current_uthread());
614*2c2f96dcSApple OSS Distributions 
615*2c2f96dcSApple OSS Distributions 	thread_qos_t old_bucket = old_pri.qos_bucket;
616*2c2f96dcSApple OSS Distributions 	thread_qos_t new_bucket = workq_pri_bucket(new_pri);
617*2c2f96dcSApple OSS Distributions 
618*2c2f96dcSApple OSS Distributions 	if (old_bucket != new_bucket) {
619*2c2f96dcSApple OSS Distributions 		_wq_thactive_move(wq, old_bucket, new_bucket);
620*2c2f96dcSApple OSS Distributions 	}
621*2c2f96dcSApple OSS Distributions 
622*2c2f96dcSApple OSS Distributions 	new_pri.qos_bucket = new_bucket;
623*2c2f96dcSApple OSS Distributions 	uth->uu_workq_pri = new_pri;
624*2c2f96dcSApple OSS Distributions 
625*2c2f96dcSApple OSS Distributions 	if (old_pri.qos_override != new_pri.qos_override) {
626*2c2f96dcSApple OSS Distributions 		thread_set_workq_override(get_machthread(uth), new_pri.qos_override);
627*2c2f96dcSApple OSS Distributions 	}
628*2c2f96dcSApple OSS Distributions 
629*2c2f96dcSApple OSS Distributions 	if (wq->wq_reqcount && (old_bucket > new_bucket || force_run)) {
630*2c2f96dcSApple OSS Distributions 		int flags = WORKQ_THREADREQ_CAN_CREATE_THREADS;
631*2c2f96dcSApple OSS Distributions 		if (old_bucket > new_bucket) {
632*2c2f96dcSApple OSS Distributions 			/*
633*2c2f96dcSApple OSS Distributions 			 * When lowering our bucket, we may unblock a thread request,
634*2c2f96dcSApple OSS Distributions 			 * but we can't drop our priority before we have evaluated
635*2c2f96dcSApple OSS Distributions 			 * whether this is the case, and if we ever drop the workqueue lock
636*2c2f96dcSApple OSS Distributions 			 * that would cause a priority inversion.
637*2c2f96dcSApple OSS Distributions 			 *
638*2c2f96dcSApple OSS Distributions 			 * We hence have to disallow thread creation in that case.
639*2c2f96dcSApple OSS Distributions 			 */
640*2c2f96dcSApple OSS Distributions 			flags = 0;
641*2c2f96dcSApple OSS Distributions 		}
642*2c2f96dcSApple OSS Distributions 		workq_schedule_creator(p, wq, flags);
643*2c2f96dcSApple OSS Distributions 	}
644*2c2f96dcSApple OSS Distributions }
645*2c2f96dcSApple OSS Distributions 
646*2c2f96dcSApple OSS Distributions /*
647*2c2f96dcSApple OSS Distributions  * Sets/resets the cpu percent limits on the current thread. We can't set
648*2c2f96dcSApple OSS Distributions  * these limits from outside of the current thread, so this function needs
649*2c2f96dcSApple OSS Distributions  * to be called when we're executing on the intended
650*2c2f96dcSApple OSS Distributions  */
651*2c2f96dcSApple OSS Distributions static void
workq_thread_reset_cpupercent(workq_threadreq_t req,struct uthread * uth)652*2c2f96dcSApple OSS Distributions workq_thread_reset_cpupercent(workq_threadreq_t req, struct uthread *uth)
653*2c2f96dcSApple OSS Distributions {
654*2c2f96dcSApple OSS Distributions 	assert(uth == current_uthread());
655*2c2f96dcSApple OSS Distributions 	workq_threadreq_param_t trp = { };
656*2c2f96dcSApple OSS Distributions 
657*2c2f96dcSApple OSS Distributions 	if (req && (req->tr_flags & WORKQ_TR_FLAG_WL_PARAMS)) {
658*2c2f96dcSApple OSS Distributions 		trp = kqueue_threadreq_workloop_param(req);
659*2c2f96dcSApple OSS Distributions 	}
660*2c2f96dcSApple OSS Distributions 
661*2c2f96dcSApple OSS Distributions 	if (uth->uu_workq_flags & UT_WORKQ_CPUPERCENT) {
662*2c2f96dcSApple OSS Distributions 		/*
663*2c2f96dcSApple OSS Distributions 		 * Going through disable when we have an existing CPU percent limit
664*2c2f96dcSApple OSS Distributions 		 * set will force the ledger to refill the token bucket of the current
665*2c2f96dcSApple OSS Distributions 		 * thread. Removing any penalty applied by previous thread use.
666*2c2f96dcSApple OSS Distributions 		 */
667*2c2f96dcSApple OSS Distributions 		thread_set_cpulimit(THREAD_CPULIMIT_DISABLE, 0, 0);
668*2c2f96dcSApple OSS Distributions 		uth->uu_workq_flags &= ~UT_WORKQ_CPUPERCENT;
669*2c2f96dcSApple OSS Distributions 	}
670*2c2f96dcSApple OSS Distributions 
671*2c2f96dcSApple OSS Distributions 	if (trp.trp_flags & TRP_CPUPERCENT) {
672*2c2f96dcSApple OSS Distributions 		thread_set_cpulimit(THREAD_CPULIMIT_BLOCK, trp.trp_cpupercent,
673*2c2f96dcSApple OSS Distributions 		    (uint64_t)trp.trp_refillms * NSEC_PER_SEC);
674*2c2f96dcSApple OSS Distributions 		uth->uu_workq_flags |= UT_WORKQ_CPUPERCENT;
675*2c2f96dcSApple OSS Distributions 	}
676*2c2f96dcSApple OSS Distributions }
677*2c2f96dcSApple OSS Distributions 
678*2c2f96dcSApple OSS Distributions /* Called with the workq lock held */
679*2c2f96dcSApple OSS Distributions static void
workq_thread_reset_pri(struct workqueue * wq,struct uthread * uth,workq_threadreq_t req,bool unpark)680*2c2f96dcSApple OSS Distributions workq_thread_reset_pri(struct workqueue *wq, struct uthread *uth,
681*2c2f96dcSApple OSS Distributions     workq_threadreq_t req, bool unpark)
682*2c2f96dcSApple OSS Distributions {
683*2c2f96dcSApple OSS Distributions 	thread_t th = get_machthread(uth);
684*2c2f96dcSApple OSS Distributions 	thread_qos_t qos = req ? req->tr_qos : WORKQ_THREAD_QOS_CLEANUP;
685*2c2f96dcSApple OSS Distributions 	workq_threadreq_param_t trp = { };
686*2c2f96dcSApple OSS Distributions 	int priority = 31;
687*2c2f96dcSApple OSS Distributions 	int policy = POLICY_TIMESHARE;
688*2c2f96dcSApple OSS Distributions 
689*2c2f96dcSApple OSS Distributions 	if (req && (req->tr_flags & WORKQ_TR_FLAG_WL_PARAMS)) {
690*2c2f96dcSApple OSS Distributions 		trp = kqueue_threadreq_workloop_param(req);
691*2c2f96dcSApple OSS Distributions 	}
692*2c2f96dcSApple OSS Distributions 
693*2c2f96dcSApple OSS Distributions 	uth->uu_workq_pri = WORKQ_POLICY_INIT(qos);
694*2c2f96dcSApple OSS Distributions 	uth->uu_workq_flags &= ~UT_WORKQ_OUTSIDE_QOS;
695*2c2f96dcSApple OSS Distributions 
696*2c2f96dcSApple OSS Distributions 	if (unpark) {
697*2c2f96dcSApple OSS Distributions 		uth->uu_save.uus_workq_park_data.workloop_params = trp.trp_value;
698*2c2f96dcSApple OSS Distributions 		// qos sent out to userspace (may differ from uu_workq_pri on param threads)
699*2c2f96dcSApple OSS Distributions 		uth->uu_save.uus_workq_park_data.qos = qos;
700*2c2f96dcSApple OSS Distributions 	}
701*2c2f96dcSApple OSS Distributions 
702*2c2f96dcSApple OSS Distributions 	if (qos == WORKQ_THREAD_QOS_MANAGER) {
703*2c2f96dcSApple OSS Distributions 		uint32_t mgr_pri = wq->wq_event_manager_priority;
704*2c2f96dcSApple OSS Distributions 		assert(trp.trp_value == 0); // manager qos and thread policy don't mix
705*2c2f96dcSApple OSS Distributions 
706*2c2f96dcSApple OSS Distributions 		if (_pthread_priority_has_sched_pri(mgr_pri)) {
707*2c2f96dcSApple OSS Distributions 			mgr_pri &= _PTHREAD_PRIORITY_SCHED_PRI_MASK;
708*2c2f96dcSApple OSS Distributions 			thread_set_workq_pri(th, THREAD_QOS_UNSPECIFIED, mgr_pri,
709*2c2f96dcSApple OSS Distributions 			    POLICY_TIMESHARE);
710*2c2f96dcSApple OSS Distributions 			return;
711*2c2f96dcSApple OSS Distributions 		}
712*2c2f96dcSApple OSS Distributions 
713*2c2f96dcSApple OSS Distributions 		qos = _pthread_priority_thread_qos(mgr_pri);
714*2c2f96dcSApple OSS Distributions 	} else {
715*2c2f96dcSApple OSS Distributions 		if (trp.trp_flags & TRP_PRIORITY) {
716*2c2f96dcSApple OSS Distributions 			qos = THREAD_QOS_UNSPECIFIED;
717*2c2f96dcSApple OSS Distributions 			priority = trp.trp_pri;
718*2c2f96dcSApple OSS Distributions 			uth->uu_workq_flags |= UT_WORKQ_OUTSIDE_QOS;
719*2c2f96dcSApple OSS Distributions 		}
720*2c2f96dcSApple OSS Distributions 
721*2c2f96dcSApple OSS Distributions 		if (trp.trp_flags & TRP_POLICY) {
722*2c2f96dcSApple OSS Distributions 			policy = trp.trp_pol;
723*2c2f96dcSApple OSS Distributions 		}
724*2c2f96dcSApple OSS Distributions 	}
725*2c2f96dcSApple OSS Distributions 
726*2c2f96dcSApple OSS Distributions #if CONFIG_PREADOPT_TG
727*2c2f96dcSApple OSS Distributions 	if (req && (req->tr_flags & WORKQ_TR_FLAG_WORKLOOP)) {
728*2c2f96dcSApple OSS Distributions 		/*
729*2c2f96dcSApple OSS Distributions 		 * For kqwl permanently configured with a thread group, we can safely borrow
730*2c2f96dcSApple OSS Distributions 		 * +1 ref from kqwl_preadopt_tg. A thread then takes additional +1 ref
731*2c2f96dcSApple OSS Distributions 		 * for itself via thread_set_preadopt_thread_group.
732*2c2f96dcSApple OSS Distributions 		 *
733*2c2f96dcSApple OSS Distributions 		 * In all other cases, we cannot safely read and borrow the reference from the kqwl
734*2c2f96dcSApple OSS Distributions 		 * since it can disappear from under us at any time due to the max-ing logic in
735*2c2f96dcSApple OSS Distributions 		 * kqueue_set_preadopted_thread_group.
736*2c2f96dcSApple OSS Distributions 		 *
737*2c2f96dcSApple OSS Distributions 		 * As such, we do the following dance:
738*2c2f96dcSApple OSS Distributions 		 *
739*2c2f96dcSApple OSS Distributions 		 * 1) cmpxchng and steal the kqwl's preadopt thread group and leave
740*2c2f96dcSApple OSS Distributions 		 * behind with (NULL + QoS). At this point, we have the reference
741*2c2f96dcSApple OSS Distributions 		 * to the thread group from the kqwl.
742*2c2f96dcSApple OSS Distributions 		 * 2) Have the thread set the preadoption thread group on itself.
743*2c2f96dcSApple OSS Distributions 		 * 3) cmpxchng from (NULL + QoS) which we set earlier in (1), back to
744*2c2f96dcSApple OSS Distributions 		 * thread_group + QoS. ie we try to give the reference back to the kqwl.
745*2c2f96dcSApple OSS Distributions 		 * If we fail, that's because a higher QoS thread group was set on the
746*2c2f96dcSApple OSS Distributions 		 * kqwl in kqueue_set_preadopted_thread_group in which case, we need to
747*2c2f96dcSApple OSS Distributions 		 * go back to (1).
748*2c2f96dcSApple OSS Distributions 		 */
749*2c2f96dcSApple OSS Distributions 
750*2c2f96dcSApple OSS Distributions 		_Atomic(struct thread_group *) * tg_loc = kqr_preadopt_thread_group_addr(req);
751*2c2f96dcSApple OSS Distributions 
752*2c2f96dcSApple OSS Distributions 		thread_group_qos_t old_tg, new_tg;
753*2c2f96dcSApple OSS Distributions 		int ret = 0;
754*2c2f96dcSApple OSS Distributions again:
755*2c2f96dcSApple OSS Distributions 		ret = os_atomic_rmw_loop(tg_loc, old_tg, new_tg, relaxed, {
756*2c2f96dcSApple OSS Distributions 			if ((!KQWL_HAS_VALID_PREADOPTED_TG(old_tg)) ||
757*2c2f96dcSApple OSS Distributions 			KQWL_HAS_PERMANENT_PREADOPTED_TG(old_tg)) {
758*2c2f96dcSApple OSS Distributions 			        os_atomic_rmw_loop_give_up(break);
759*2c2f96dcSApple OSS Distributions 			}
760*2c2f96dcSApple OSS Distributions 
761*2c2f96dcSApple OSS Distributions 			/*
762*2c2f96dcSApple OSS Distributions 			 * Leave the QoS behind - kqueue_set_preadopted_thread_group will
763*2c2f96dcSApple OSS Distributions 			 * only modify it if there is a higher QoS thread group to attach
764*2c2f96dcSApple OSS Distributions 			 */
765*2c2f96dcSApple OSS Distributions 			new_tg = (thread_group_qos_t) ((uintptr_t) old_tg & KQWL_PREADOPT_TG_QOS_MASK);
766*2c2f96dcSApple OSS Distributions 		});
767*2c2f96dcSApple OSS Distributions 
768*2c2f96dcSApple OSS Distributions 		if (ret) {
769*2c2f96dcSApple OSS Distributions 			/*
770*2c2f96dcSApple OSS Distributions 			 * We successfully took the ref from the kqwl so set it on the
771*2c2f96dcSApple OSS Distributions 			 * thread now
772*2c2f96dcSApple OSS Distributions 			 */
773*2c2f96dcSApple OSS Distributions 			thread_set_preadopt_thread_group(th, KQWL_GET_PREADOPTED_TG(old_tg));
774*2c2f96dcSApple OSS Distributions 
775*2c2f96dcSApple OSS Distributions 			thread_group_qos_t thread_group_to_expect = new_tg;
776*2c2f96dcSApple OSS Distributions 			thread_group_qos_t thread_group_to_set = old_tg;
777*2c2f96dcSApple OSS Distributions 
778*2c2f96dcSApple OSS Distributions 			os_atomic_rmw_loop(tg_loc, old_tg, new_tg, relaxed, {
779*2c2f96dcSApple OSS Distributions 				if (old_tg != thread_group_to_expect) {
780*2c2f96dcSApple OSS Distributions 				        /*
781*2c2f96dcSApple OSS Distributions 				         * There was an intervening write to the kqwl_preadopt_tg,
782*2c2f96dcSApple OSS Distributions 				         * and it has a higher QoS than what we are working with
783*2c2f96dcSApple OSS Distributions 				         * here. Abandon our current adopted thread group and redo
784*2c2f96dcSApple OSS Distributions 				         * the full dance
785*2c2f96dcSApple OSS Distributions 				         */
786*2c2f96dcSApple OSS Distributions 				        thread_group_deallocate_safe(KQWL_GET_PREADOPTED_TG(thread_group_to_set));
787*2c2f96dcSApple OSS Distributions 				        os_atomic_rmw_loop_give_up(goto again);
788*2c2f96dcSApple OSS Distributions 				}
789*2c2f96dcSApple OSS Distributions 
790*2c2f96dcSApple OSS Distributions 				new_tg = thread_group_to_set;
791*2c2f96dcSApple OSS Distributions 			});
792*2c2f96dcSApple OSS Distributions 		} else {
793*2c2f96dcSApple OSS Distributions 			if (KQWL_HAS_PERMANENT_PREADOPTED_TG(old_tg)) {
794*2c2f96dcSApple OSS Distributions 				thread_set_preadopt_thread_group(th, KQWL_GET_PREADOPTED_TG(old_tg));
795*2c2f96dcSApple OSS Distributions 			} else {
796*2c2f96dcSApple OSS Distributions 				/* Nothing valid on the kqwl, just clear what's on the thread */
797*2c2f96dcSApple OSS Distributions 				thread_set_preadopt_thread_group(th, NULL);
798*2c2f96dcSApple OSS Distributions 			}
799*2c2f96dcSApple OSS Distributions 		}
800*2c2f96dcSApple OSS Distributions 	} else {
801*2c2f96dcSApple OSS Distributions 		/* Not even a kqwl, clear what's on the thread */
802*2c2f96dcSApple OSS Distributions 		thread_set_preadopt_thread_group(th, NULL);
803*2c2f96dcSApple OSS Distributions 	}
804*2c2f96dcSApple OSS Distributions #endif
805*2c2f96dcSApple OSS Distributions 	thread_set_workq_pri(th, qos, priority, policy);
806*2c2f96dcSApple OSS Distributions }
807*2c2f96dcSApple OSS Distributions 
808*2c2f96dcSApple OSS Distributions /*
809*2c2f96dcSApple OSS Distributions  * Called by kevent with the NOTE_WL_THREAD_REQUEST knote lock held,
810*2c2f96dcSApple OSS Distributions  * every time a servicer is being told about a new max QoS.
811*2c2f96dcSApple OSS Distributions  */
812*2c2f96dcSApple OSS Distributions void
workq_thread_set_max_qos(struct proc * p,workq_threadreq_t kqr)813*2c2f96dcSApple OSS Distributions workq_thread_set_max_qos(struct proc *p, workq_threadreq_t kqr)
814*2c2f96dcSApple OSS Distributions {
815*2c2f96dcSApple OSS Distributions 	struct uu_workq_policy old_pri, new_pri;
816*2c2f96dcSApple OSS Distributions 	struct uthread *uth = current_uthread();
817*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = proc_get_wqptr_fast(p);
818*2c2f96dcSApple OSS Distributions 	thread_qos_t qos = kqr->tr_kq_qos_index;
819*2c2f96dcSApple OSS Distributions 
820*2c2f96dcSApple OSS Distributions 	if (uth->uu_workq_pri.qos_max == qos) {
821*2c2f96dcSApple OSS Distributions 		return;
822*2c2f96dcSApple OSS Distributions 	}
823*2c2f96dcSApple OSS Distributions 
824*2c2f96dcSApple OSS Distributions 	workq_lock_spin(wq);
825*2c2f96dcSApple OSS Distributions 	old_pri = new_pri = uth->uu_workq_pri;
826*2c2f96dcSApple OSS Distributions 	new_pri.qos_max = qos;
827*2c2f96dcSApple OSS Distributions 	workq_thread_update_bucket(p, wq, uth, old_pri, new_pri, false);
828*2c2f96dcSApple OSS Distributions 	workq_unlock(wq);
829*2c2f96dcSApple OSS Distributions }
830*2c2f96dcSApple OSS Distributions 
831*2c2f96dcSApple OSS Distributions #pragma mark idle threads accounting and handling
832*2c2f96dcSApple OSS Distributions 
833*2c2f96dcSApple OSS Distributions static inline struct uthread *
workq_oldest_killable_idle_thread(struct workqueue * wq)834*2c2f96dcSApple OSS Distributions workq_oldest_killable_idle_thread(struct workqueue *wq)
835*2c2f96dcSApple OSS Distributions {
836*2c2f96dcSApple OSS Distributions 	struct uthread *uth = TAILQ_LAST(&wq->wq_thidlelist, workq_uthread_head);
837*2c2f96dcSApple OSS Distributions 
838*2c2f96dcSApple OSS Distributions 	if (uth && !uth->uu_save.uus_workq_park_data.has_stack) {
839*2c2f96dcSApple OSS Distributions 		uth = TAILQ_PREV(uth, workq_uthread_head, uu_workq_entry);
840*2c2f96dcSApple OSS Distributions 		if (uth) {
841*2c2f96dcSApple OSS Distributions 			assert(uth->uu_save.uus_workq_park_data.has_stack);
842*2c2f96dcSApple OSS Distributions 		}
843*2c2f96dcSApple OSS Distributions 	}
844*2c2f96dcSApple OSS Distributions 	return uth;
845*2c2f96dcSApple OSS Distributions }
846*2c2f96dcSApple OSS Distributions 
847*2c2f96dcSApple OSS Distributions static inline uint64_t
workq_kill_delay_for_idle_thread(struct workqueue * wq)848*2c2f96dcSApple OSS Distributions workq_kill_delay_for_idle_thread(struct workqueue *wq)
849*2c2f96dcSApple OSS Distributions {
850*2c2f96dcSApple OSS Distributions 	uint64_t delay = wq_reduce_pool_window.abstime;
851*2c2f96dcSApple OSS Distributions 	uint16_t idle = wq->wq_thidlecount;
852*2c2f96dcSApple OSS Distributions 
853*2c2f96dcSApple OSS Distributions 	/*
854*2c2f96dcSApple OSS Distributions 	 * If we have less than wq_death_max_load threads, have a 5s timer.
855*2c2f96dcSApple OSS Distributions 	 *
856*2c2f96dcSApple OSS Distributions 	 * For the next wq_max_constrained_threads ones, decay linearly from
857*2c2f96dcSApple OSS Distributions 	 * from 5s to 50ms.
858*2c2f96dcSApple OSS Distributions 	 */
859*2c2f96dcSApple OSS Distributions 	if (idle <= wq_death_max_load) {
860*2c2f96dcSApple OSS Distributions 		return delay;
861*2c2f96dcSApple OSS Distributions 	}
862*2c2f96dcSApple OSS Distributions 
863*2c2f96dcSApple OSS Distributions 	if (wq_max_constrained_threads > idle - wq_death_max_load) {
864*2c2f96dcSApple OSS Distributions 		delay *= (wq_max_constrained_threads - (idle - wq_death_max_load));
865*2c2f96dcSApple OSS Distributions 	}
866*2c2f96dcSApple OSS Distributions 	return delay / wq_max_constrained_threads;
867*2c2f96dcSApple OSS Distributions }
868*2c2f96dcSApple OSS Distributions 
869*2c2f96dcSApple OSS Distributions static inline bool
workq_should_kill_idle_thread(struct workqueue * wq,struct uthread * uth,uint64_t now)870*2c2f96dcSApple OSS Distributions workq_should_kill_idle_thread(struct workqueue *wq, struct uthread *uth,
871*2c2f96dcSApple OSS Distributions     uint64_t now)
872*2c2f96dcSApple OSS Distributions {
873*2c2f96dcSApple OSS Distributions 	uint64_t delay = workq_kill_delay_for_idle_thread(wq);
874*2c2f96dcSApple OSS Distributions 	return now - uth->uu_save.uus_workq_park_data.idle_stamp > delay;
875*2c2f96dcSApple OSS Distributions }
876*2c2f96dcSApple OSS Distributions 
877*2c2f96dcSApple OSS Distributions static void
workq_death_call_schedule(struct workqueue * wq,uint64_t deadline)878*2c2f96dcSApple OSS Distributions workq_death_call_schedule(struct workqueue *wq, uint64_t deadline)
879*2c2f96dcSApple OSS Distributions {
880*2c2f96dcSApple OSS Distributions 	uint32_t wq_flags = os_atomic_load(&wq->wq_flags, relaxed);
881*2c2f96dcSApple OSS Distributions 
882*2c2f96dcSApple OSS Distributions 	if (wq_flags & (WQ_EXITING | WQ_DEATH_CALL_SCHEDULED)) {
883*2c2f96dcSApple OSS Distributions 		return;
884*2c2f96dcSApple OSS Distributions 	}
885*2c2f96dcSApple OSS Distributions 	os_atomic_or(&wq->wq_flags, WQ_DEATH_CALL_SCHEDULED, relaxed);
886*2c2f96dcSApple OSS Distributions 
887*2c2f96dcSApple OSS Distributions 	WQ_TRACE_WQ(TRACE_wq_death_call | DBG_FUNC_NONE, wq, 1, 0, 0);
888*2c2f96dcSApple OSS Distributions 
889*2c2f96dcSApple OSS Distributions 	/*
890*2c2f96dcSApple OSS Distributions 	 * <rdar://problem/13139182> Due to how long term timers work, the leeway
891*2c2f96dcSApple OSS Distributions 	 * can't be too short, so use 500ms which is long enough that we will not
892*2c2f96dcSApple OSS Distributions 	 * wake up the CPU for killing threads, but short enough that it doesn't
893*2c2f96dcSApple OSS Distributions 	 * fall into long-term timer list shenanigans.
894*2c2f96dcSApple OSS Distributions 	 */
895*2c2f96dcSApple OSS Distributions 	thread_call_enter_delayed_with_leeway(wq->wq_death_call, NULL, deadline,
896*2c2f96dcSApple OSS Distributions 	    wq_reduce_pool_window.abstime / 10,
897*2c2f96dcSApple OSS Distributions 	    THREAD_CALL_DELAY_LEEWAY | THREAD_CALL_DELAY_USER_BACKGROUND);
898*2c2f96dcSApple OSS Distributions }
899*2c2f96dcSApple OSS Distributions 
900*2c2f96dcSApple OSS Distributions /*
901*2c2f96dcSApple OSS Distributions  * `decrement` is set to the number of threads that are no longer dying:
902*2c2f96dcSApple OSS Distributions  * - because they have been resuscitated just in time (workq_pop_idle_thread)
903*2c2f96dcSApple OSS Distributions  * - or have been killed (workq_thread_terminate).
904*2c2f96dcSApple OSS Distributions  */
905*2c2f96dcSApple OSS Distributions static void
workq_death_policy_evaluate(struct workqueue * wq,uint16_t decrement)906*2c2f96dcSApple OSS Distributions workq_death_policy_evaluate(struct workqueue *wq, uint16_t decrement)
907*2c2f96dcSApple OSS Distributions {
908*2c2f96dcSApple OSS Distributions 	struct uthread *uth;
909*2c2f96dcSApple OSS Distributions 
910*2c2f96dcSApple OSS Distributions 	assert(wq->wq_thdying_count >= decrement);
911*2c2f96dcSApple OSS Distributions 	if ((wq->wq_thdying_count -= decrement) > 0) {
912*2c2f96dcSApple OSS Distributions 		return;
913*2c2f96dcSApple OSS Distributions 	}
914*2c2f96dcSApple OSS Distributions 
915*2c2f96dcSApple OSS Distributions 	if (wq->wq_thidlecount <= 1) {
916*2c2f96dcSApple OSS Distributions 		return;
917*2c2f96dcSApple OSS Distributions 	}
918*2c2f96dcSApple OSS Distributions 
919*2c2f96dcSApple OSS Distributions 	if ((uth = workq_oldest_killable_idle_thread(wq)) == NULL) {
920*2c2f96dcSApple OSS Distributions 		return;
921*2c2f96dcSApple OSS Distributions 	}
922*2c2f96dcSApple OSS Distributions 
923*2c2f96dcSApple OSS Distributions 	uint64_t now = mach_absolute_time();
924*2c2f96dcSApple OSS Distributions 	uint64_t delay = workq_kill_delay_for_idle_thread(wq);
925*2c2f96dcSApple OSS Distributions 
926*2c2f96dcSApple OSS Distributions 	if (now - uth->uu_save.uus_workq_park_data.idle_stamp > delay) {
927*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_thread_terminate | DBG_FUNC_START,
928*2c2f96dcSApple OSS Distributions 		    wq, wq->wq_thidlecount, 0, 0);
929*2c2f96dcSApple OSS Distributions 		wq->wq_thdying_count++;
930*2c2f96dcSApple OSS Distributions 		uth->uu_workq_flags |= UT_WORKQ_DYING;
931*2c2f96dcSApple OSS Distributions 		if ((uth->uu_workq_flags & UT_WORKQ_IDLE_CLEANUP) == 0) {
932*2c2f96dcSApple OSS Distributions 			workq_thread_wakeup(uth);
933*2c2f96dcSApple OSS Distributions 		}
934*2c2f96dcSApple OSS Distributions 		return;
935*2c2f96dcSApple OSS Distributions 	}
936*2c2f96dcSApple OSS Distributions 
937*2c2f96dcSApple OSS Distributions 	workq_death_call_schedule(wq,
938*2c2f96dcSApple OSS Distributions 	    uth->uu_save.uus_workq_park_data.idle_stamp + delay);
939*2c2f96dcSApple OSS Distributions }
940*2c2f96dcSApple OSS Distributions 
941*2c2f96dcSApple OSS Distributions void
workq_thread_terminate(struct proc * p,struct uthread * uth)942*2c2f96dcSApple OSS Distributions workq_thread_terminate(struct proc *p, struct uthread *uth)
943*2c2f96dcSApple OSS Distributions {
944*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = proc_get_wqptr_fast(p);
945*2c2f96dcSApple OSS Distributions 
946*2c2f96dcSApple OSS Distributions 	workq_lock_spin(wq);
947*2c2f96dcSApple OSS Distributions 	TAILQ_REMOVE(&wq->wq_thrunlist, uth, uu_workq_entry);
948*2c2f96dcSApple OSS Distributions 	if (uth->uu_workq_flags & UT_WORKQ_DYING) {
949*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_thread_terminate | DBG_FUNC_END,
950*2c2f96dcSApple OSS Distributions 		    wq, wq->wq_thidlecount, 0, 0);
951*2c2f96dcSApple OSS Distributions 		workq_death_policy_evaluate(wq, 1);
952*2c2f96dcSApple OSS Distributions 	}
953*2c2f96dcSApple OSS Distributions 	if (wq->wq_nthreads-- == wq_max_threads) {
954*2c2f96dcSApple OSS Distributions 		/*
955*2c2f96dcSApple OSS Distributions 		 * We got under the thread limit again, which may have prevented
956*2c2f96dcSApple OSS Distributions 		 * thread creation from happening, redrive if there are pending requests
957*2c2f96dcSApple OSS Distributions 		 */
958*2c2f96dcSApple OSS Distributions 		if (wq->wq_reqcount) {
959*2c2f96dcSApple OSS Distributions 			workq_schedule_creator(p, wq, WORKQ_THREADREQ_CAN_CREATE_THREADS);
960*2c2f96dcSApple OSS Distributions 		}
961*2c2f96dcSApple OSS Distributions 	}
962*2c2f96dcSApple OSS Distributions 	workq_unlock(wq);
963*2c2f96dcSApple OSS Distributions 
964*2c2f96dcSApple OSS Distributions 	thread_deallocate(get_machthread(uth));
965*2c2f96dcSApple OSS Distributions }
966*2c2f96dcSApple OSS Distributions 
967*2c2f96dcSApple OSS Distributions static void
workq_kill_old_threads_call(void * param0,void * param1 __unused)968*2c2f96dcSApple OSS Distributions workq_kill_old_threads_call(void *param0, void *param1 __unused)
969*2c2f96dcSApple OSS Distributions {
970*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = param0;
971*2c2f96dcSApple OSS Distributions 
972*2c2f96dcSApple OSS Distributions 	workq_lock_spin(wq);
973*2c2f96dcSApple OSS Distributions 	WQ_TRACE_WQ(TRACE_wq_death_call | DBG_FUNC_START, wq, 0, 0, 0);
974*2c2f96dcSApple OSS Distributions 	os_atomic_andnot(&wq->wq_flags, WQ_DEATH_CALL_SCHEDULED, relaxed);
975*2c2f96dcSApple OSS Distributions 	workq_death_policy_evaluate(wq, 0);
976*2c2f96dcSApple OSS Distributions 	WQ_TRACE_WQ(TRACE_wq_death_call | DBG_FUNC_END, wq, 0, 0, 0);
977*2c2f96dcSApple OSS Distributions 	workq_unlock(wq);
978*2c2f96dcSApple OSS Distributions }
979*2c2f96dcSApple OSS Distributions 
980*2c2f96dcSApple OSS Distributions static struct uthread *
workq_pop_idle_thread(struct workqueue * wq,uint16_t uu_flags,bool * needs_wakeup)981*2c2f96dcSApple OSS Distributions workq_pop_idle_thread(struct workqueue *wq, uint16_t uu_flags,
982*2c2f96dcSApple OSS Distributions     bool *needs_wakeup)
983*2c2f96dcSApple OSS Distributions {
984*2c2f96dcSApple OSS Distributions 	struct uthread *uth;
985*2c2f96dcSApple OSS Distributions 
986*2c2f96dcSApple OSS Distributions 	if ((uth = TAILQ_FIRST(&wq->wq_thidlelist))) {
987*2c2f96dcSApple OSS Distributions 		TAILQ_REMOVE(&wq->wq_thidlelist, uth, uu_workq_entry);
988*2c2f96dcSApple OSS Distributions 	} else {
989*2c2f96dcSApple OSS Distributions 		uth = TAILQ_FIRST(&wq->wq_thnewlist);
990*2c2f96dcSApple OSS Distributions 		TAILQ_REMOVE(&wq->wq_thnewlist, uth, uu_workq_entry);
991*2c2f96dcSApple OSS Distributions 	}
992*2c2f96dcSApple OSS Distributions 	TAILQ_INSERT_TAIL(&wq->wq_thrunlist, uth, uu_workq_entry);
993*2c2f96dcSApple OSS Distributions 
994*2c2f96dcSApple OSS Distributions 	assert((uth->uu_workq_flags & UT_WORKQ_RUNNING) == 0);
995*2c2f96dcSApple OSS Distributions 	uth->uu_workq_flags |= UT_WORKQ_RUNNING | uu_flags;
996*2c2f96dcSApple OSS Distributions 
997*2c2f96dcSApple OSS Distributions 	/* A thread is never woken up as part of the cooperative pool */
998*2c2f96dcSApple OSS Distributions 	assert((uu_flags & UT_WORKQ_COOPERATIVE) == 0);
999*2c2f96dcSApple OSS Distributions 
1000*2c2f96dcSApple OSS Distributions 	if ((uu_flags & UT_WORKQ_OVERCOMMIT) == 0) {
1001*2c2f96dcSApple OSS Distributions 		wq->wq_constrained_threads_scheduled++;
1002*2c2f96dcSApple OSS Distributions 	}
1003*2c2f96dcSApple OSS Distributions 	wq->wq_threads_scheduled++;
1004*2c2f96dcSApple OSS Distributions 	wq->wq_thidlecount--;
1005*2c2f96dcSApple OSS Distributions 
1006*2c2f96dcSApple OSS Distributions 	if (__improbable(uth->uu_workq_flags & UT_WORKQ_DYING)) {
1007*2c2f96dcSApple OSS Distributions 		uth->uu_workq_flags ^= UT_WORKQ_DYING;
1008*2c2f96dcSApple OSS Distributions 		workq_death_policy_evaluate(wq, 1);
1009*2c2f96dcSApple OSS Distributions 		*needs_wakeup = false;
1010*2c2f96dcSApple OSS Distributions 	} else if (uth->uu_workq_flags & UT_WORKQ_IDLE_CLEANUP) {
1011*2c2f96dcSApple OSS Distributions 		*needs_wakeup = false;
1012*2c2f96dcSApple OSS Distributions 	} else {
1013*2c2f96dcSApple OSS Distributions 		*needs_wakeup = true;
1014*2c2f96dcSApple OSS Distributions 	}
1015*2c2f96dcSApple OSS Distributions 	return uth;
1016*2c2f96dcSApple OSS Distributions }
1017*2c2f96dcSApple OSS Distributions 
1018*2c2f96dcSApple OSS Distributions /*
1019*2c2f96dcSApple OSS Distributions  * Called by thread_create_workq_waiting() during thread initialization, before
1020*2c2f96dcSApple OSS Distributions  * assert_wait, before the thread has been started.
1021*2c2f96dcSApple OSS Distributions  */
1022*2c2f96dcSApple OSS Distributions event_t
workq_thread_init_and_wq_lock(task_t task,thread_t th)1023*2c2f96dcSApple OSS Distributions workq_thread_init_and_wq_lock(task_t task, thread_t th)
1024*2c2f96dcSApple OSS Distributions {
1025*2c2f96dcSApple OSS Distributions 	struct uthread *uth = get_bsdthread_info(th);
1026*2c2f96dcSApple OSS Distributions 
1027*2c2f96dcSApple OSS Distributions 	uth->uu_workq_flags = UT_WORKQ_NEW;
1028*2c2f96dcSApple OSS Distributions 	uth->uu_workq_pri = WORKQ_POLICY_INIT(THREAD_QOS_LEGACY);
1029*2c2f96dcSApple OSS Distributions 	uth->uu_workq_thport = MACH_PORT_NULL;
1030*2c2f96dcSApple OSS Distributions 	uth->uu_workq_stackaddr = 0;
1031*2c2f96dcSApple OSS Distributions 	uth->uu_workq_pthread_kill_allowed = 0;
1032*2c2f96dcSApple OSS Distributions 
1033*2c2f96dcSApple OSS Distributions 	thread_set_tag(th, THREAD_TAG_PTHREAD | THREAD_TAG_WORKQUEUE);
1034*2c2f96dcSApple OSS Distributions 	thread_reset_workq_qos(th, THREAD_QOS_LEGACY);
1035*2c2f96dcSApple OSS Distributions 
1036*2c2f96dcSApple OSS Distributions 	workq_lock_spin(proc_get_wqptr_fast(get_bsdtask_info(task)));
1037*2c2f96dcSApple OSS Distributions 	return workq_parked_wait_event(uth);
1038*2c2f96dcSApple OSS Distributions }
1039*2c2f96dcSApple OSS Distributions 
1040*2c2f96dcSApple OSS Distributions /**
1041*2c2f96dcSApple OSS Distributions  * Try to add a new workqueue thread.
1042*2c2f96dcSApple OSS Distributions  *
1043*2c2f96dcSApple OSS Distributions  * - called with workq lock held
1044*2c2f96dcSApple OSS Distributions  * - dropped and retaken around thread creation
1045*2c2f96dcSApple OSS Distributions  * - return with workq lock held
1046*2c2f96dcSApple OSS Distributions  */
1047*2c2f96dcSApple OSS Distributions static bool
workq_add_new_idle_thread(proc_t p,struct workqueue * wq)1048*2c2f96dcSApple OSS Distributions workq_add_new_idle_thread(proc_t p, struct workqueue *wq)
1049*2c2f96dcSApple OSS Distributions {
1050*2c2f96dcSApple OSS Distributions 	mach_vm_offset_t th_stackaddr;
1051*2c2f96dcSApple OSS Distributions 	kern_return_t kret;
1052*2c2f96dcSApple OSS Distributions 	thread_t th;
1053*2c2f96dcSApple OSS Distributions 
1054*2c2f96dcSApple OSS Distributions 	wq->wq_nthreads++;
1055*2c2f96dcSApple OSS Distributions 
1056*2c2f96dcSApple OSS Distributions 	workq_unlock(wq);
1057*2c2f96dcSApple OSS Distributions 
1058*2c2f96dcSApple OSS Distributions 	vm_map_t vmap = get_task_map(proc_task(p));
1059*2c2f96dcSApple OSS Distributions 
1060*2c2f96dcSApple OSS Distributions 	kret = pthread_functions->workq_create_threadstack(p, vmap, &th_stackaddr);
1061*2c2f96dcSApple OSS Distributions 	if (kret != KERN_SUCCESS) {
1062*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_thread_create_failed | DBG_FUNC_NONE, wq,
1063*2c2f96dcSApple OSS Distributions 		    kret, 1, 0);
1064*2c2f96dcSApple OSS Distributions 		goto out;
1065*2c2f96dcSApple OSS Distributions 	}
1066*2c2f96dcSApple OSS Distributions 
1067*2c2f96dcSApple OSS Distributions 	kret = thread_create_workq_waiting(proc_task(p), workq_unpark_continue, &th);
1068*2c2f96dcSApple OSS Distributions 	if (kret != KERN_SUCCESS) {
1069*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_thread_create_failed | DBG_FUNC_NONE, wq,
1070*2c2f96dcSApple OSS Distributions 		    kret, 0, 0);
1071*2c2f96dcSApple OSS Distributions 		pthread_functions->workq_destroy_threadstack(p, vmap, th_stackaddr);
1072*2c2f96dcSApple OSS Distributions 		goto out;
1073*2c2f96dcSApple OSS Distributions 	}
1074*2c2f96dcSApple OSS Distributions 
1075*2c2f96dcSApple OSS Distributions 	// thread_create_workq_waiting() will return with the wq lock held
1076*2c2f96dcSApple OSS Distributions 	// on success, because it calls workq_thread_init_and_wq_lock() above
1077*2c2f96dcSApple OSS Distributions 
1078*2c2f96dcSApple OSS Distributions 	struct uthread *uth = get_bsdthread_info(th);
1079*2c2f96dcSApple OSS Distributions 
1080*2c2f96dcSApple OSS Distributions 	wq->wq_creations++;
1081*2c2f96dcSApple OSS Distributions 	wq->wq_thidlecount++;
1082*2c2f96dcSApple OSS Distributions 	uth->uu_workq_stackaddr = (user_addr_t)th_stackaddr;
1083*2c2f96dcSApple OSS Distributions 	TAILQ_INSERT_TAIL(&wq->wq_thnewlist, uth, uu_workq_entry);
1084*2c2f96dcSApple OSS Distributions 
1085*2c2f96dcSApple OSS Distributions 	WQ_TRACE_WQ(TRACE_wq_thread_create | DBG_FUNC_NONE, wq, 0, 0, 0);
1086*2c2f96dcSApple OSS Distributions 	return true;
1087*2c2f96dcSApple OSS Distributions 
1088*2c2f96dcSApple OSS Distributions out:
1089*2c2f96dcSApple OSS Distributions 	workq_lock_spin(wq);
1090*2c2f96dcSApple OSS Distributions 	/*
1091*2c2f96dcSApple OSS Distributions 	 * Do not redrive here if we went under wq_max_threads again,
1092*2c2f96dcSApple OSS Distributions 	 * it is the responsibility of the callers of this function
1093*2c2f96dcSApple OSS Distributions 	 * to do so when it fails.
1094*2c2f96dcSApple OSS Distributions 	 */
1095*2c2f96dcSApple OSS Distributions 	wq->wq_nthreads--;
1096*2c2f96dcSApple OSS Distributions 	return false;
1097*2c2f96dcSApple OSS Distributions }
1098*2c2f96dcSApple OSS Distributions 
1099*2c2f96dcSApple OSS Distributions static inline bool
workq_thread_is_overcommit(struct uthread * uth)1100*2c2f96dcSApple OSS Distributions workq_thread_is_overcommit(struct uthread *uth)
1101*2c2f96dcSApple OSS Distributions {
1102*2c2f96dcSApple OSS Distributions 	return (uth->uu_workq_flags & UT_WORKQ_OVERCOMMIT) != 0;
1103*2c2f96dcSApple OSS Distributions }
1104*2c2f96dcSApple OSS Distributions 
1105*2c2f96dcSApple OSS Distributions static inline bool
workq_thread_is_nonovercommit(struct uthread * uth)1106*2c2f96dcSApple OSS Distributions workq_thread_is_nonovercommit(struct uthread *uth)
1107*2c2f96dcSApple OSS Distributions {
1108*2c2f96dcSApple OSS Distributions 	return (uth->uu_workq_flags & (UT_WORKQ_OVERCOMMIT | UT_WORKQ_COOPERATIVE)) == 0;
1109*2c2f96dcSApple OSS Distributions }
1110*2c2f96dcSApple OSS Distributions 
1111*2c2f96dcSApple OSS Distributions static inline bool
workq_thread_is_cooperative(struct uthread * uth)1112*2c2f96dcSApple OSS Distributions workq_thread_is_cooperative(struct uthread *uth)
1113*2c2f96dcSApple OSS Distributions {
1114*2c2f96dcSApple OSS Distributions 	return (uth->uu_workq_flags & UT_WORKQ_COOPERATIVE) != 0;
1115*2c2f96dcSApple OSS Distributions }
1116*2c2f96dcSApple OSS Distributions 
1117*2c2f96dcSApple OSS Distributions static inline void
workq_thread_set_type(struct uthread * uth,uint16_t flags)1118*2c2f96dcSApple OSS Distributions workq_thread_set_type(struct uthread *uth, uint16_t flags)
1119*2c2f96dcSApple OSS Distributions {
1120*2c2f96dcSApple OSS Distributions 	uth->uu_workq_flags &= ~(UT_WORKQ_OVERCOMMIT | UT_WORKQ_COOPERATIVE);
1121*2c2f96dcSApple OSS Distributions 	uth->uu_workq_flags |= flags;
1122*2c2f96dcSApple OSS Distributions }
1123*2c2f96dcSApple OSS Distributions 
1124*2c2f96dcSApple OSS Distributions 
1125*2c2f96dcSApple OSS Distributions #define WORKQ_UNPARK_FOR_DEATH_WAS_IDLE 0x1
1126*2c2f96dcSApple OSS Distributions 
1127*2c2f96dcSApple OSS Distributions __attribute__((noreturn, noinline))
1128*2c2f96dcSApple OSS Distributions static void
workq_unpark_for_death_and_unlock(proc_t p,struct workqueue * wq,struct uthread * uth,uint32_t death_flags,uint32_t setup_flags)1129*2c2f96dcSApple OSS Distributions workq_unpark_for_death_and_unlock(proc_t p, struct workqueue *wq,
1130*2c2f96dcSApple OSS Distributions     struct uthread *uth, uint32_t death_flags, uint32_t setup_flags)
1131*2c2f96dcSApple OSS Distributions {
1132*2c2f96dcSApple OSS Distributions 	thread_qos_t qos = workq_pri_override(uth->uu_workq_pri);
1133*2c2f96dcSApple OSS Distributions 	bool first_use = uth->uu_workq_flags & UT_WORKQ_NEW;
1134*2c2f96dcSApple OSS Distributions 
1135*2c2f96dcSApple OSS Distributions 	if (qos > WORKQ_THREAD_QOS_CLEANUP) {
1136*2c2f96dcSApple OSS Distributions 		workq_thread_reset_pri(wq, uth, NULL, /*unpark*/ true);
1137*2c2f96dcSApple OSS Distributions 		qos = WORKQ_THREAD_QOS_CLEANUP;
1138*2c2f96dcSApple OSS Distributions 	}
1139*2c2f96dcSApple OSS Distributions 
1140*2c2f96dcSApple OSS Distributions 	workq_thread_reset_cpupercent(NULL, uth);
1141*2c2f96dcSApple OSS Distributions 
1142*2c2f96dcSApple OSS Distributions 	if (death_flags & WORKQ_UNPARK_FOR_DEATH_WAS_IDLE) {
1143*2c2f96dcSApple OSS Distributions 		wq->wq_thidlecount--;
1144*2c2f96dcSApple OSS Distributions 		if (first_use) {
1145*2c2f96dcSApple OSS Distributions 			TAILQ_REMOVE(&wq->wq_thnewlist, uth, uu_workq_entry);
1146*2c2f96dcSApple OSS Distributions 		} else {
1147*2c2f96dcSApple OSS Distributions 			TAILQ_REMOVE(&wq->wq_thidlelist, uth, uu_workq_entry);
1148*2c2f96dcSApple OSS Distributions 		}
1149*2c2f96dcSApple OSS Distributions 	}
1150*2c2f96dcSApple OSS Distributions 	TAILQ_INSERT_TAIL(&wq->wq_thrunlist, uth, uu_workq_entry);
1151*2c2f96dcSApple OSS Distributions 
1152*2c2f96dcSApple OSS Distributions 	workq_unlock(wq);
1153*2c2f96dcSApple OSS Distributions 
1154*2c2f96dcSApple OSS Distributions 	if (setup_flags & WQ_SETUP_CLEAR_VOUCHER) {
1155*2c2f96dcSApple OSS Distributions 		__assert_only kern_return_t kr;
1156*2c2f96dcSApple OSS Distributions 		kr = thread_set_voucher_name(MACH_PORT_NULL);
1157*2c2f96dcSApple OSS Distributions 		assert(kr == KERN_SUCCESS);
1158*2c2f96dcSApple OSS Distributions 	}
1159*2c2f96dcSApple OSS Distributions 
1160*2c2f96dcSApple OSS Distributions 	uint32_t flags = WQ_FLAG_THREAD_NEWSPI | qos | WQ_FLAG_THREAD_PRIO_QOS;
1161*2c2f96dcSApple OSS Distributions 	thread_t th = get_machthread(uth);
1162*2c2f96dcSApple OSS Distributions 	vm_map_t vmap = get_task_map(proc_task(p));
1163*2c2f96dcSApple OSS Distributions 
1164*2c2f96dcSApple OSS Distributions 	if (!first_use) {
1165*2c2f96dcSApple OSS Distributions 		flags |= WQ_FLAG_THREAD_REUSE;
1166*2c2f96dcSApple OSS Distributions 	}
1167*2c2f96dcSApple OSS Distributions 
1168*2c2f96dcSApple OSS Distributions 	pthread_functions->workq_setup_thread(p, th, vmap, uth->uu_workq_stackaddr,
1169*2c2f96dcSApple OSS Distributions 	    uth->uu_workq_thport, 0, WQ_SETUP_EXIT_THREAD, flags);
1170*2c2f96dcSApple OSS Distributions 	__builtin_unreachable();
1171*2c2f96dcSApple OSS Distributions }
1172*2c2f96dcSApple OSS Distributions 
1173*2c2f96dcSApple OSS Distributions bool
workq_is_current_thread_updating_turnstile(struct workqueue * wq)1174*2c2f96dcSApple OSS Distributions workq_is_current_thread_updating_turnstile(struct workqueue *wq)
1175*2c2f96dcSApple OSS Distributions {
1176*2c2f96dcSApple OSS Distributions 	return wq->wq_turnstile_updater == current_thread();
1177*2c2f96dcSApple OSS Distributions }
1178*2c2f96dcSApple OSS Distributions 
1179*2c2f96dcSApple OSS Distributions __attribute__((always_inline))
1180*2c2f96dcSApple OSS Distributions static inline void
1181*2c2f96dcSApple OSS Distributions workq_perform_turnstile_operation_locked(struct workqueue *wq,
1182*2c2f96dcSApple OSS Distributions     void (^operation)(void))
1183*2c2f96dcSApple OSS Distributions {
1184*2c2f96dcSApple OSS Distributions 	workq_lock_held(wq);
1185*2c2f96dcSApple OSS Distributions 	wq->wq_turnstile_updater = current_thread();
1186*2c2f96dcSApple OSS Distributions 	operation();
1187*2c2f96dcSApple OSS Distributions 	wq->wq_turnstile_updater = THREAD_NULL;
1188*2c2f96dcSApple OSS Distributions }
1189*2c2f96dcSApple OSS Distributions 
1190*2c2f96dcSApple OSS Distributions static void
workq_turnstile_update_inheritor(struct workqueue * wq,turnstile_inheritor_t inheritor,turnstile_update_flags_t flags)1191*2c2f96dcSApple OSS Distributions workq_turnstile_update_inheritor(struct workqueue *wq,
1192*2c2f96dcSApple OSS Distributions     turnstile_inheritor_t inheritor,
1193*2c2f96dcSApple OSS Distributions     turnstile_update_flags_t flags)
1194*2c2f96dcSApple OSS Distributions {
1195*2c2f96dcSApple OSS Distributions 	if (wq->wq_inheritor == inheritor) {
1196*2c2f96dcSApple OSS Distributions 		return;
1197*2c2f96dcSApple OSS Distributions 	}
1198*2c2f96dcSApple OSS Distributions 	wq->wq_inheritor = inheritor;
1199*2c2f96dcSApple OSS Distributions 	workq_perform_turnstile_operation_locked(wq, ^{
1200*2c2f96dcSApple OSS Distributions 		turnstile_update_inheritor(wq->wq_turnstile, inheritor,
1201*2c2f96dcSApple OSS Distributions 		flags | TURNSTILE_IMMEDIATE_UPDATE);
1202*2c2f96dcSApple OSS Distributions 		turnstile_update_inheritor_complete(wq->wq_turnstile,
1203*2c2f96dcSApple OSS Distributions 		TURNSTILE_INTERLOCK_HELD);
1204*2c2f96dcSApple OSS Distributions 	});
1205*2c2f96dcSApple OSS Distributions }
1206*2c2f96dcSApple OSS Distributions 
1207*2c2f96dcSApple OSS Distributions static void
workq_push_idle_thread(proc_t p,struct workqueue * wq,struct uthread * uth,uint32_t setup_flags)1208*2c2f96dcSApple OSS Distributions workq_push_idle_thread(proc_t p, struct workqueue *wq, struct uthread *uth,
1209*2c2f96dcSApple OSS Distributions     uint32_t setup_flags)
1210*2c2f96dcSApple OSS Distributions {
1211*2c2f96dcSApple OSS Distributions 	uint64_t now = mach_absolute_time();
1212*2c2f96dcSApple OSS Distributions 	bool is_creator = (uth == wq->wq_creator);
1213*2c2f96dcSApple OSS Distributions 
1214*2c2f96dcSApple OSS Distributions 	if (workq_thread_is_cooperative(uth)) {
1215*2c2f96dcSApple OSS Distributions 		assert(!is_creator);
1216*2c2f96dcSApple OSS Distributions 
1217*2c2f96dcSApple OSS Distributions 		thread_qos_t thread_qos = uth->uu_workq_pri.qos_req;
1218*2c2f96dcSApple OSS Distributions 		_wq_cooperative_queue_scheduled_count_dec(wq, thread_qos);
1219*2c2f96dcSApple OSS Distributions 
1220*2c2f96dcSApple OSS Distributions 		/* Before we get here, we always go through
1221*2c2f96dcSApple OSS Distributions 		 * workq_select_threadreq_or_park_and_unlock. If we got here, it means
1222*2c2f96dcSApple OSS Distributions 		 * that we went through the logic in workq_threadreq_select which
1223*2c2f96dcSApple OSS Distributions 		 * did the refresh for the next best cooperative qos while
1224*2c2f96dcSApple OSS Distributions 		 * excluding the current thread - we shouldn't need to do it again.
1225*2c2f96dcSApple OSS Distributions 		 */
1226*2c2f96dcSApple OSS Distributions 		assert(_wq_cooperative_queue_refresh_best_req_qos(wq) == false);
1227*2c2f96dcSApple OSS Distributions 	} else if (workq_thread_is_nonovercommit(uth)) {
1228*2c2f96dcSApple OSS Distributions 		assert(!is_creator);
1229*2c2f96dcSApple OSS Distributions 
1230*2c2f96dcSApple OSS Distributions 		wq->wq_constrained_threads_scheduled--;
1231*2c2f96dcSApple OSS Distributions 	}
1232*2c2f96dcSApple OSS Distributions 
1233*2c2f96dcSApple OSS Distributions 	uth->uu_workq_flags &= ~(UT_WORKQ_RUNNING | UT_WORKQ_OVERCOMMIT | UT_WORKQ_COOPERATIVE);
1234*2c2f96dcSApple OSS Distributions 	TAILQ_REMOVE(&wq->wq_thrunlist, uth, uu_workq_entry);
1235*2c2f96dcSApple OSS Distributions 	wq->wq_threads_scheduled--;
1236*2c2f96dcSApple OSS Distributions 
1237*2c2f96dcSApple OSS Distributions 	if (is_creator) {
1238*2c2f96dcSApple OSS Distributions 		wq->wq_creator = NULL;
1239*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_creator_select, wq, 3, 0,
1240*2c2f96dcSApple OSS Distributions 		    uth->uu_save.uus_workq_park_data.yields);
1241*2c2f96dcSApple OSS Distributions 	}
1242*2c2f96dcSApple OSS Distributions 
1243*2c2f96dcSApple OSS Distributions 	if (wq->wq_inheritor == get_machthread(uth)) {
1244*2c2f96dcSApple OSS Distributions 		assert(wq->wq_creator == NULL);
1245*2c2f96dcSApple OSS Distributions 		if (wq->wq_reqcount) {
1246*2c2f96dcSApple OSS Distributions 			workq_turnstile_update_inheritor(wq, wq, TURNSTILE_INHERITOR_WORKQ);
1247*2c2f96dcSApple OSS Distributions 		} else {
1248*2c2f96dcSApple OSS Distributions 			workq_turnstile_update_inheritor(wq, TURNSTILE_INHERITOR_NULL, 0);
1249*2c2f96dcSApple OSS Distributions 		}
1250*2c2f96dcSApple OSS Distributions 	}
1251*2c2f96dcSApple OSS Distributions 
1252*2c2f96dcSApple OSS Distributions 	if (uth->uu_workq_flags & UT_WORKQ_NEW) {
1253*2c2f96dcSApple OSS Distributions 		assert(is_creator || (_wq_flags(wq) & WQ_EXITING));
1254*2c2f96dcSApple OSS Distributions 		TAILQ_INSERT_TAIL(&wq->wq_thnewlist, uth, uu_workq_entry);
1255*2c2f96dcSApple OSS Distributions 		wq->wq_thidlecount++;
1256*2c2f96dcSApple OSS Distributions 		return;
1257*2c2f96dcSApple OSS Distributions 	}
1258*2c2f96dcSApple OSS Distributions 
1259*2c2f96dcSApple OSS Distributions 	if (!is_creator) {
1260*2c2f96dcSApple OSS Distributions 		_wq_thactive_dec(wq, uth->uu_workq_pri.qos_bucket);
1261*2c2f96dcSApple OSS Distributions 		wq->wq_thscheduled_count[_wq_bucket(uth->uu_workq_pri.qos_bucket)]--;
1262*2c2f96dcSApple OSS Distributions 		uth->uu_workq_flags |= UT_WORKQ_IDLE_CLEANUP;
1263*2c2f96dcSApple OSS Distributions 	}
1264*2c2f96dcSApple OSS Distributions 
1265*2c2f96dcSApple OSS Distributions 	uth->uu_save.uus_workq_park_data.idle_stamp = now;
1266*2c2f96dcSApple OSS Distributions 
1267*2c2f96dcSApple OSS Distributions 	struct uthread *oldest = workq_oldest_killable_idle_thread(wq);
1268*2c2f96dcSApple OSS Distributions 	uint16_t cur_idle = wq->wq_thidlecount;
1269*2c2f96dcSApple OSS Distributions 
1270*2c2f96dcSApple OSS Distributions 	if (cur_idle >= wq_max_constrained_threads ||
1271*2c2f96dcSApple OSS Distributions 	    (wq->wq_thdying_count == 0 && oldest &&
1272*2c2f96dcSApple OSS Distributions 	    workq_should_kill_idle_thread(wq, oldest, now))) {
1273*2c2f96dcSApple OSS Distributions 		/*
1274*2c2f96dcSApple OSS Distributions 		 * Immediately kill threads if we have too may of them.
1275*2c2f96dcSApple OSS Distributions 		 *
1276*2c2f96dcSApple OSS Distributions 		 * And swap "place" with the oldest one we'd have woken up.
1277*2c2f96dcSApple OSS Distributions 		 * This is a relatively desperate situation where we really
1278*2c2f96dcSApple OSS Distributions 		 * need to kill threads quickly and it's best to kill
1279*2c2f96dcSApple OSS Distributions 		 * the one that's currently on core than context switching.
1280*2c2f96dcSApple OSS Distributions 		 */
1281*2c2f96dcSApple OSS Distributions 		if (oldest) {
1282*2c2f96dcSApple OSS Distributions 			oldest->uu_save.uus_workq_park_data.idle_stamp = now;
1283*2c2f96dcSApple OSS Distributions 			TAILQ_REMOVE(&wq->wq_thidlelist, oldest, uu_workq_entry);
1284*2c2f96dcSApple OSS Distributions 			TAILQ_INSERT_HEAD(&wq->wq_thidlelist, oldest, uu_workq_entry);
1285*2c2f96dcSApple OSS Distributions 		}
1286*2c2f96dcSApple OSS Distributions 
1287*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_thread_terminate | DBG_FUNC_START,
1288*2c2f96dcSApple OSS Distributions 		    wq, cur_idle, 0, 0);
1289*2c2f96dcSApple OSS Distributions 		wq->wq_thdying_count++;
1290*2c2f96dcSApple OSS Distributions 		uth->uu_workq_flags |= UT_WORKQ_DYING;
1291*2c2f96dcSApple OSS Distributions 		uth->uu_workq_flags &= ~UT_WORKQ_IDLE_CLEANUP;
1292*2c2f96dcSApple OSS Distributions 		workq_unpark_for_death_and_unlock(p, wq, uth, 0, setup_flags);
1293*2c2f96dcSApple OSS Distributions 		__builtin_unreachable();
1294*2c2f96dcSApple OSS Distributions 	}
1295*2c2f96dcSApple OSS Distributions 
1296*2c2f96dcSApple OSS Distributions 	struct uthread *tail = TAILQ_LAST(&wq->wq_thidlelist, workq_uthread_head);
1297*2c2f96dcSApple OSS Distributions 
1298*2c2f96dcSApple OSS Distributions 	cur_idle += 1;
1299*2c2f96dcSApple OSS Distributions 	wq->wq_thidlecount = cur_idle;
1300*2c2f96dcSApple OSS Distributions 
1301*2c2f96dcSApple OSS Distributions 	if (cur_idle >= wq_death_max_load && tail &&
1302*2c2f96dcSApple OSS Distributions 	    tail->uu_save.uus_workq_park_data.has_stack) {
1303*2c2f96dcSApple OSS Distributions 		uth->uu_save.uus_workq_park_data.has_stack = false;
1304*2c2f96dcSApple OSS Distributions 		TAILQ_INSERT_TAIL(&wq->wq_thidlelist, uth, uu_workq_entry);
1305*2c2f96dcSApple OSS Distributions 	} else {
1306*2c2f96dcSApple OSS Distributions 		uth->uu_save.uus_workq_park_data.has_stack = true;
1307*2c2f96dcSApple OSS Distributions 		TAILQ_INSERT_HEAD(&wq->wq_thidlelist, uth, uu_workq_entry);
1308*2c2f96dcSApple OSS Distributions 	}
1309*2c2f96dcSApple OSS Distributions 
1310*2c2f96dcSApple OSS Distributions 	if (!tail) {
1311*2c2f96dcSApple OSS Distributions 		uint64_t delay = workq_kill_delay_for_idle_thread(wq);
1312*2c2f96dcSApple OSS Distributions 		workq_death_call_schedule(wq, now + delay);
1313*2c2f96dcSApple OSS Distributions 	}
1314*2c2f96dcSApple OSS Distributions }
1315*2c2f96dcSApple OSS Distributions 
1316*2c2f96dcSApple OSS Distributions #pragma mark thread requests
1317*2c2f96dcSApple OSS Distributions 
1318*2c2f96dcSApple OSS Distributions static inline bool
workq_tr_is_overcommit(workq_tr_flags_t tr_flags)1319*2c2f96dcSApple OSS Distributions workq_tr_is_overcommit(workq_tr_flags_t tr_flags)
1320*2c2f96dcSApple OSS Distributions {
1321*2c2f96dcSApple OSS Distributions 	return (tr_flags & WORKQ_TR_FLAG_OVERCOMMIT) != 0;
1322*2c2f96dcSApple OSS Distributions }
1323*2c2f96dcSApple OSS Distributions 
1324*2c2f96dcSApple OSS Distributions static inline bool
workq_tr_is_nonovercommit(workq_tr_flags_t tr_flags)1325*2c2f96dcSApple OSS Distributions workq_tr_is_nonovercommit(workq_tr_flags_t tr_flags)
1326*2c2f96dcSApple OSS Distributions {
1327*2c2f96dcSApple OSS Distributions 	return (tr_flags & (WORKQ_TR_FLAG_OVERCOMMIT | WORKQ_TR_FLAG_COOPERATIVE)) == 0;
1328*2c2f96dcSApple OSS Distributions }
1329*2c2f96dcSApple OSS Distributions 
1330*2c2f96dcSApple OSS Distributions static inline bool
workq_tr_is_cooperative(workq_tr_flags_t tr_flags)1331*2c2f96dcSApple OSS Distributions workq_tr_is_cooperative(workq_tr_flags_t tr_flags)
1332*2c2f96dcSApple OSS Distributions {
1333*2c2f96dcSApple OSS Distributions 	return (tr_flags & WORKQ_TR_FLAG_COOPERATIVE) != 0;
1334*2c2f96dcSApple OSS Distributions }
1335*2c2f96dcSApple OSS Distributions 
1336*2c2f96dcSApple OSS Distributions #define workq_threadreq_is_overcommit(req) workq_tr_is_overcommit((req)->tr_flags)
1337*2c2f96dcSApple OSS Distributions #define workq_threadreq_is_nonovercommit(req) workq_tr_is_nonovercommit((req)->tr_flags)
1338*2c2f96dcSApple OSS Distributions #define workq_threadreq_is_cooperative(req) workq_tr_is_cooperative((req)->tr_flags)
1339*2c2f96dcSApple OSS Distributions 
1340*2c2f96dcSApple OSS Distributions static inline int
workq_priority_for_req(workq_threadreq_t req)1341*2c2f96dcSApple OSS Distributions workq_priority_for_req(workq_threadreq_t req)
1342*2c2f96dcSApple OSS Distributions {
1343*2c2f96dcSApple OSS Distributions 	thread_qos_t qos = req->tr_qos;
1344*2c2f96dcSApple OSS Distributions 
1345*2c2f96dcSApple OSS Distributions 	if (req->tr_flags & WORKQ_TR_FLAG_WL_OUTSIDE_QOS) {
1346*2c2f96dcSApple OSS Distributions 		workq_threadreq_param_t trp = kqueue_threadreq_workloop_param(req);
1347*2c2f96dcSApple OSS Distributions 		assert(trp.trp_flags & TRP_PRIORITY);
1348*2c2f96dcSApple OSS Distributions 		return trp.trp_pri;
1349*2c2f96dcSApple OSS Distributions 	}
1350*2c2f96dcSApple OSS Distributions 	return thread_workq_pri_for_qos(qos);
1351*2c2f96dcSApple OSS Distributions }
1352*2c2f96dcSApple OSS Distributions 
1353*2c2f96dcSApple OSS Distributions static inline struct priority_queue_sched_max *
workq_priority_queue_for_req(struct workqueue * wq,workq_threadreq_t req)1354*2c2f96dcSApple OSS Distributions workq_priority_queue_for_req(struct workqueue *wq, workq_threadreq_t req)
1355*2c2f96dcSApple OSS Distributions {
1356*2c2f96dcSApple OSS Distributions 	assert(!workq_tr_is_cooperative(req->tr_flags));
1357*2c2f96dcSApple OSS Distributions 
1358*2c2f96dcSApple OSS Distributions 	if (req->tr_flags & WORKQ_TR_FLAG_WL_OUTSIDE_QOS) {
1359*2c2f96dcSApple OSS Distributions 		return &wq->wq_special_queue;
1360*2c2f96dcSApple OSS Distributions 	} else if (workq_tr_is_overcommit(req->tr_flags)) {
1361*2c2f96dcSApple OSS Distributions 		return &wq->wq_overcommit_queue;
1362*2c2f96dcSApple OSS Distributions 	} else {
1363*2c2f96dcSApple OSS Distributions 		return &wq->wq_constrained_queue;
1364*2c2f96dcSApple OSS Distributions 	}
1365*2c2f96dcSApple OSS Distributions }
1366*2c2f96dcSApple OSS Distributions 
1367*2c2f96dcSApple OSS Distributions 
1368*2c2f96dcSApple OSS Distributions /* Calculates the number of threads scheduled >= the input QoS */
1369*2c2f96dcSApple OSS Distributions static uint64_t
workq_num_cooperative_threads_scheduled_to_qos(struct workqueue * wq,thread_qos_t qos)1370*2c2f96dcSApple OSS Distributions workq_num_cooperative_threads_scheduled_to_qos(struct workqueue *wq, thread_qos_t qos)
1371*2c2f96dcSApple OSS Distributions {
1372*2c2f96dcSApple OSS Distributions 	workq_lock_held(wq);
1373*2c2f96dcSApple OSS Distributions 
1374*2c2f96dcSApple OSS Distributions 	uint64_t num_cooperative_threads = 0;
1375*2c2f96dcSApple OSS Distributions 
1376*2c2f96dcSApple OSS Distributions 	for (thread_qos_t cur_qos = WORKQ_THREAD_QOS_MAX; cur_qos >= qos; cur_qos--) {
1377*2c2f96dcSApple OSS Distributions 		uint8_t bucket = _wq_bucket(cur_qos);
1378*2c2f96dcSApple OSS Distributions 		num_cooperative_threads += wq->wq_cooperative_queue_scheduled_count[bucket];
1379*2c2f96dcSApple OSS Distributions 	}
1380*2c2f96dcSApple OSS Distributions 
1381*2c2f96dcSApple OSS Distributions 	return num_cooperative_threads;
1382*2c2f96dcSApple OSS Distributions }
1383*2c2f96dcSApple OSS Distributions 
1384*2c2f96dcSApple OSS Distributions static uint64_t
workq_num_cooperative_threads_scheduled_total(struct workqueue * wq)1385*2c2f96dcSApple OSS Distributions workq_num_cooperative_threads_scheduled_total(struct workqueue *wq)
1386*2c2f96dcSApple OSS Distributions {
1387*2c2f96dcSApple OSS Distributions 	return workq_num_cooperative_threads_scheduled_to_qos(wq, WORKQ_THREAD_QOS_MIN);
1388*2c2f96dcSApple OSS Distributions }
1389*2c2f96dcSApple OSS Distributions 
1390*2c2f96dcSApple OSS Distributions #if DEBUG || DEVELOPMENT
1391*2c2f96dcSApple OSS Distributions static bool
workq_has_cooperative_thread_requests(struct workqueue * wq)1392*2c2f96dcSApple OSS Distributions workq_has_cooperative_thread_requests(struct workqueue *wq)
1393*2c2f96dcSApple OSS Distributions {
1394*2c2f96dcSApple OSS Distributions 	for (thread_qos_t qos = WORKQ_THREAD_QOS_MAX; qos >= WORKQ_THREAD_QOS_MIN; qos--) {
1395*2c2f96dcSApple OSS Distributions 		uint8_t bucket = _wq_bucket(qos);
1396*2c2f96dcSApple OSS Distributions 		if (!STAILQ_EMPTY(&wq->wq_cooperative_queue[bucket])) {
1397*2c2f96dcSApple OSS Distributions 			return true;
1398*2c2f96dcSApple OSS Distributions 		}
1399*2c2f96dcSApple OSS Distributions 	}
1400*2c2f96dcSApple OSS Distributions 
1401*2c2f96dcSApple OSS Distributions 	return false;
1402*2c2f96dcSApple OSS Distributions }
1403*2c2f96dcSApple OSS Distributions #endif
1404*2c2f96dcSApple OSS Distributions 
1405*2c2f96dcSApple OSS Distributions /*
1406*2c2f96dcSApple OSS Distributions  * Determines the next QoS bucket we should service next in the cooperative
1407*2c2f96dcSApple OSS Distributions  * pool. This function will always return a QoS for cooperative pool as long as
1408*2c2f96dcSApple OSS Distributions  * there are requests to be serviced.
1409*2c2f96dcSApple OSS Distributions  *
1410*2c2f96dcSApple OSS Distributions  * Unlike the other thread pools, for the cooperative thread pool the schedule
1411*2c2f96dcSApple OSS Distributions  * counts for the various buckets in the pool affect the next best request for
1412*2c2f96dcSApple OSS Distributions  * it.
1413*2c2f96dcSApple OSS Distributions  *
1414*2c2f96dcSApple OSS Distributions  * This function is called in the following contexts:
1415*2c2f96dcSApple OSS Distributions  *
1416*2c2f96dcSApple OSS Distributions  * a) When determining the best thread QoS for cooperative bucket for the
1417*2c2f96dcSApple OSS Distributions  * creator/thread reuse
1418*2c2f96dcSApple OSS Distributions  *
1419*2c2f96dcSApple OSS Distributions  * b) Once (a) has happened and thread has bound to a thread request, figuring
1420*2c2f96dcSApple OSS Distributions  * out whether the next best request for this pool has changed so that creator
1421*2c2f96dcSApple OSS Distributions  * can be scheduled.
1422*2c2f96dcSApple OSS Distributions  *
1423*2c2f96dcSApple OSS Distributions  * Returns true if the cooperative queue's best qos changed from previous
1424*2c2f96dcSApple OSS Distributions  * value.
1425*2c2f96dcSApple OSS Distributions  */
1426*2c2f96dcSApple OSS Distributions static bool
_wq_cooperative_queue_refresh_best_req_qos(struct workqueue * wq)1427*2c2f96dcSApple OSS Distributions _wq_cooperative_queue_refresh_best_req_qos(struct workqueue *wq)
1428*2c2f96dcSApple OSS Distributions {
1429*2c2f96dcSApple OSS Distributions 	workq_lock_held(wq);
1430*2c2f96dcSApple OSS Distributions 
1431*2c2f96dcSApple OSS Distributions 	thread_qos_t old_best_req_qos = wq->wq_cooperative_queue_best_req_qos;
1432*2c2f96dcSApple OSS Distributions 
1433*2c2f96dcSApple OSS Distributions 	/* We determine the next best cooperative thread request based on the
1434*2c2f96dcSApple OSS Distributions 	 * following:
1435*2c2f96dcSApple OSS Distributions 	 *
1436*2c2f96dcSApple OSS Distributions 	 * 1. Take the MAX of the following:
1437*2c2f96dcSApple OSS Distributions 	 *		a) Highest qos with pending TRs such that number of scheduled
1438*2c2f96dcSApple OSS Distributions 	 *		threads so far with >= qos is < wq_max_cooperative_threads
1439*2c2f96dcSApple OSS Distributions 	 *		b) Highest qos bucket with pending TRs but no scheduled threads for that bucket
1440*2c2f96dcSApple OSS Distributions 	 *
1441*2c2f96dcSApple OSS Distributions 	 * 2. If the result of (1) is UN, then we pick the highest priority amongst
1442*2c2f96dcSApple OSS Distributions 	 * pending thread requests in the pool.
1443*2c2f96dcSApple OSS Distributions 	 *
1444*2c2f96dcSApple OSS Distributions 	 */
1445*2c2f96dcSApple OSS Distributions 	thread_qos_t highest_qos_with_no_scheduled = THREAD_QOS_UNSPECIFIED;
1446*2c2f96dcSApple OSS Distributions 	thread_qos_t highest_qos_req_with_width = THREAD_QOS_UNSPECIFIED;
1447*2c2f96dcSApple OSS Distributions 
1448*2c2f96dcSApple OSS Distributions 	thread_qos_t highest_qos_req = THREAD_QOS_UNSPECIFIED;
1449*2c2f96dcSApple OSS Distributions 
1450*2c2f96dcSApple OSS Distributions 	int scheduled_count_till_qos = 0;
1451*2c2f96dcSApple OSS Distributions 
1452*2c2f96dcSApple OSS Distributions 	for (thread_qos_t qos = WORKQ_THREAD_QOS_MAX; qos >= WORKQ_THREAD_QOS_MIN; qos--) {
1453*2c2f96dcSApple OSS Distributions 		uint8_t bucket = _wq_bucket(qos);
1454*2c2f96dcSApple OSS Distributions 		uint8_t scheduled_count_for_bucket = wq->wq_cooperative_queue_scheduled_count[bucket];
1455*2c2f96dcSApple OSS Distributions 		scheduled_count_till_qos += scheduled_count_for_bucket;
1456*2c2f96dcSApple OSS Distributions 
1457*2c2f96dcSApple OSS Distributions 		if (!STAILQ_EMPTY(&wq->wq_cooperative_queue[bucket])) {
1458*2c2f96dcSApple OSS Distributions 			if (qos > highest_qos_req) {
1459*2c2f96dcSApple OSS Distributions 				highest_qos_req = qos;
1460*2c2f96dcSApple OSS Distributions 			}
1461*2c2f96dcSApple OSS Distributions 			/*
1462*2c2f96dcSApple OSS Distributions 			 * The pool isn't saturated for threads at and above this QoS, and
1463*2c2f96dcSApple OSS Distributions 			 * this qos bucket has pending requests
1464*2c2f96dcSApple OSS Distributions 			 */
1465*2c2f96dcSApple OSS Distributions 			if (scheduled_count_till_qos < wq_cooperative_queue_max_size(wq)) {
1466*2c2f96dcSApple OSS Distributions 				if (qos > highest_qos_req_with_width) {
1467*2c2f96dcSApple OSS Distributions 					highest_qos_req_with_width = qos;
1468*2c2f96dcSApple OSS Distributions 				}
1469*2c2f96dcSApple OSS Distributions 			}
1470*2c2f96dcSApple OSS Distributions 
1471*2c2f96dcSApple OSS Distributions 			/*
1472*2c2f96dcSApple OSS Distributions 			 * There are no threads scheduled for this bucket but there
1473*2c2f96dcSApple OSS Distributions 			 * is work pending, give it at least 1 thread
1474*2c2f96dcSApple OSS Distributions 			 */
1475*2c2f96dcSApple OSS Distributions 			if (scheduled_count_for_bucket == 0) {
1476*2c2f96dcSApple OSS Distributions 				if (qos > highest_qos_with_no_scheduled) {
1477*2c2f96dcSApple OSS Distributions 					highest_qos_with_no_scheduled = qos;
1478*2c2f96dcSApple OSS Distributions 				}
1479*2c2f96dcSApple OSS Distributions 			}
1480*2c2f96dcSApple OSS Distributions 		}
1481*2c2f96dcSApple OSS Distributions 	}
1482*2c2f96dcSApple OSS Distributions 
1483*2c2f96dcSApple OSS Distributions 	wq->wq_cooperative_queue_best_req_qos = MAX(highest_qos_with_no_scheduled, highest_qos_req_with_width);
1484*2c2f96dcSApple OSS Distributions 	if (wq->wq_cooperative_queue_best_req_qos == THREAD_QOS_UNSPECIFIED) {
1485*2c2f96dcSApple OSS Distributions 		wq->wq_cooperative_queue_best_req_qos = highest_qos_req;
1486*2c2f96dcSApple OSS Distributions 	}
1487*2c2f96dcSApple OSS Distributions 
1488*2c2f96dcSApple OSS Distributions #if DEBUG || DEVELOPMENT
1489*2c2f96dcSApple OSS Distributions 	/* Assert that if we are showing up the next best req as UN, then there
1490*2c2f96dcSApple OSS Distributions 	 * actually is no thread request in the cooperative pool buckets */
1491*2c2f96dcSApple OSS Distributions 	if (wq->wq_cooperative_queue_best_req_qos == THREAD_QOS_UNSPECIFIED) {
1492*2c2f96dcSApple OSS Distributions 		assert(!workq_has_cooperative_thread_requests(wq));
1493*2c2f96dcSApple OSS Distributions 	}
1494*2c2f96dcSApple OSS Distributions #endif
1495*2c2f96dcSApple OSS Distributions 
1496*2c2f96dcSApple OSS Distributions 	return old_best_req_qos != wq->wq_cooperative_queue_best_req_qos;
1497*2c2f96dcSApple OSS Distributions }
1498*2c2f96dcSApple OSS Distributions 
1499*2c2f96dcSApple OSS Distributions /*
1500*2c2f96dcSApple OSS Distributions  * Returns whether or not the input thread (or creator thread if uth is NULL)
1501*2c2f96dcSApple OSS Distributions  * should be allowed to work as part of the cooperative pool for the <input qos>
1502*2c2f96dcSApple OSS Distributions  * bucket.
1503*2c2f96dcSApple OSS Distributions  *
1504*2c2f96dcSApple OSS Distributions  * This function is called in a bunch of places:
1505*2c2f96dcSApple OSS Distributions  *		a) Quantum expires for a thread and it is part of the cooperative pool
1506*2c2f96dcSApple OSS Distributions  *		b) When trying to pick a thread request for the creator thread to
1507*2c2f96dcSApple OSS Distributions  *		represent.
1508*2c2f96dcSApple OSS Distributions  *		c) When a thread is trying to pick a thread request to actually bind to
1509*2c2f96dcSApple OSS Distributions  *		and service.
1510*2c2f96dcSApple OSS Distributions  *
1511*2c2f96dcSApple OSS Distributions  * Called with workq lock held.
1512*2c2f96dcSApple OSS Distributions  */
1513*2c2f96dcSApple OSS Distributions 
1514*2c2f96dcSApple OSS Distributions #define WQ_COOPERATIVE_POOL_UNSATURATED 1
1515*2c2f96dcSApple OSS Distributions #define WQ_COOPERATIVE_BUCKET_UNSERVICED 2
1516*2c2f96dcSApple OSS Distributions #define WQ_COOPERATIVE_POOL_SATURATED_UP_TO_QOS 3
1517*2c2f96dcSApple OSS Distributions 
1518*2c2f96dcSApple OSS Distributions static bool
workq_cooperative_allowance(struct workqueue * wq,thread_qos_t qos,struct uthread * uth,bool may_start_timer)1519*2c2f96dcSApple OSS Distributions workq_cooperative_allowance(struct workqueue *wq, thread_qos_t qos, struct uthread *uth,
1520*2c2f96dcSApple OSS Distributions     bool may_start_timer)
1521*2c2f96dcSApple OSS Distributions {
1522*2c2f96dcSApple OSS Distributions 	workq_lock_held(wq);
1523*2c2f96dcSApple OSS Distributions 
1524*2c2f96dcSApple OSS Distributions 	bool exclude_thread_as_scheduled = false;
1525*2c2f96dcSApple OSS Distributions 	bool passed_admissions = false;
1526*2c2f96dcSApple OSS Distributions 	uint8_t bucket = _wq_bucket(qos);
1527*2c2f96dcSApple OSS Distributions 
1528*2c2f96dcSApple OSS Distributions 	if (uth && workq_thread_is_cooperative(uth)) {
1529*2c2f96dcSApple OSS Distributions 		exclude_thread_as_scheduled = true;
1530*2c2f96dcSApple OSS Distributions 		_wq_cooperative_queue_scheduled_count_dec(wq, uth->uu_workq_pri.qos_req);
1531*2c2f96dcSApple OSS Distributions 	}
1532*2c2f96dcSApple OSS Distributions 
1533*2c2f96dcSApple OSS Distributions 	/*
1534*2c2f96dcSApple OSS Distributions 	 * We have not saturated the pool yet, let this thread continue
1535*2c2f96dcSApple OSS Distributions 	 */
1536*2c2f96dcSApple OSS Distributions 	uint64_t total_cooperative_threads;
1537*2c2f96dcSApple OSS Distributions 	total_cooperative_threads = workq_num_cooperative_threads_scheduled_total(wq);
1538*2c2f96dcSApple OSS Distributions 	if (total_cooperative_threads < wq_cooperative_queue_max_size(wq)) {
1539*2c2f96dcSApple OSS Distributions 		passed_admissions = true;
1540*2c2f96dcSApple OSS Distributions 		WQ_TRACE(TRACE_wq_cooperative_admission | DBG_FUNC_NONE,
1541*2c2f96dcSApple OSS Distributions 		    total_cooperative_threads, qos, passed_admissions,
1542*2c2f96dcSApple OSS Distributions 		    WQ_COOPERATIVE_POOL_UNSATURATED);
1543*2c2f96dcSApple OSS Distributions 		goto out;
1544*2c2f96dcSApple OSS Distributions 	}
1545*2c2f96dcSApple OSS Distributions 
1546*2c2f96dcSApple OSS Distributions 	/*
1547*2c2f96dcSApple OSS Distributions 	 * Without this thread, nothing is servicing the bucket which has pending
1548*2c2f96dcSApple OSS Distributions 	 * work
1549*2c2f96dcSApple OSS Distributions 	 */
1550*2c2f96dcSApple OSS Distributions 	uint64_t bucket_scheduled = wq->wq_cooperative_queue_scheduled_count[bucket];
1551*2c2f96dcSApple OSS Distributions 	if (bucket_scheduled == 0 &&
1552*2c2f96dcSApple OSS Distributions 	    !STAILQ_EMPTY(&wq->wq_cooperative_queue[bucket])) {
1553*2c2f96dcSApple OSS Distributions 		passed_admissions = true;
1554*2c2f96dcSApple OSS Distributions 		WQ_TRACE(TRACE_wq_cooperative_admission | DBG_FUNC_NONE,
1555*2c2f96dcSApple OSS Distributions 		    total_cooperative_threads, qos, passed_admissions,
1556*2c2f96dcSApple OSS Distributions 		    WQ_COOPERATIVE_BUCKET_UNSERVICED);
1557*2c2f96dcSApple OSS Distributions 		goto out;
1558*2c2f96dcSApple OSS Distributions 	}
1559*2c2f96dcSApple OSS Distributions 
1560*2c2f96dcSApple OSS Distributions 	/*
1561*2c2f96dcSApple OSS Distributions 	 * If number of threads at the QoS bucket >= input QoS exceeds the max we want
1562*2c2f96dcSApple OSS Distributions 	 * for the pool, deny this thread
1563*2c2f96dcSApple OSS Distributions 	 */
1564*2c2f96dcSApple OSS Distributions 	uint64_t aggregate_down_to_qos = workq_num_cooperative_threads_scheduled_to_qos(wq, qos);
1565*2c2f96dcSApple OSS Distributions 	passed_admissions = (aggregate_down_to_qos < wq_cooperative_queue_max_size(wq));
1566*2c2f96dcSApple OSS Distributions 	WQ_TRACE(TRACE_wq_cooperative_admission | DBG_FUNC_NONE, aggregate_down_to_qos,
1567*2c2f96dcSApple OSS Distributions 	    qos, passed_admissions, WQ_COOPERATIVE_POOL_SATURATED_UP_TO_QOS);
1568*2c2f96dcSApple OSS Distributions 
1569*2c2f96dcSApple OSS Distributions 	if (!passed_admissions && may_start_timer) {
1570*2c2f96dcSApple OSS Distributions 		workq_schedule_delayed_thread_creation(wq, 0);
1571*2c2f96dcSApple OSS Distributions 	}
1572*2c2f96dcSApple OSS Distributions 
1573*2c2f96dcSApple OSS Distributions out:
1574*2c2f96dcSApple OSS Distributions 	if (exclude_thread_as_scheduled) {
1575*2c2f96dcSApple OSS Distributions 		_wq_cooperative_queue_scheduled_count_inc(wq, uth->uu_workq_pri.qos_req);
1576*2c2f96dcSApple OSS Distributions 	}
1577*2c2f96dcSApple OSS Distributions 	return passed_admissions;
1578*2c2f96dcSApple OSS Distributions }
1579*2c2f96dcSApple OSS Distributions 
1580*2c2f96dcSApple OSS Distributions /*
1581*2c2f96dcSApple OSS Distributions  * returns true if the best request for the pool changed as a result of
1582*2c2f96dcSApple OSS Distributions  * enqueuing this thread request.
1583*2c2f96dcSApple OSS Distributions  */
1584*2c2f96dcSApple OSS Distributions static bool
workq_threadreq_enqueue(struct workqueue * wq,workq_threadreq_t req)1585*2c2f96dcSApple OSS Distributions workq_threadreq_enqueue(struct workqueue *wq, workq_threadreq_t req)
1586*2c2f96dcSApple OSS Distributions {
1587*2c2f96dcSApple OSS Distributions 	assert(req->tr_state == WORKQ_TR_STATE_NEW);
1588*2c2f96dcSApple OSS Distributions 
1589*2c2f96dcSApple OSS Distributions 	req->tr_state = WORKQ_TR_STATE_QUEUED;
1590*2c2f96dcSApple OSS Distributions 	wq->wq_reqcount += req->tr_count;
1591*2c2f96dcSApple OSS Distributions 
1592*2c2f96dcSApple OSS Distributions 	if (req->tr_qos == WORKQ_THREAD_QOS_MANAGER) {
1593*2c2f96dcSApple OSS Distributions 		assert(wq->wq_event_manager_threadreq == NULL);
1594*2c2f96dcSApple OSS Distributions 		assert(req->tr_flags & WORKQ_TR_FLAG_KEVENT);
1595*2c2f96dcSApple OSS Distributions 		assert(req->tr_count == 1);
1596*2c2f96dcSApple OSS Distributions 		wq->wq_event_manager_threadreq = req;
1597*2c2f96dcSApple OSS Distributions 		return true;
1598*2c2f96dcSApple OSS Distributions 	}
1599*2c2f96dcSApple OSS Distributions 
1600*2c2f96dcSApple OSS Distributions 	if (workq_threadreq_is_cooperative(req)) {
1601*2c2f96dcSApple OSS Distributions 		assert(req->tr_qos != WORKQ_THREAD_QOS_MANAGER);
1602*2c2f96dcSApple OSS Distributions 		assert(req->tr_qos != WORKQ_THREAD_QOS_ABOVEUI);
1603*2c2f96dcSApple OSS Distributions 
1604*2c2f96dcSApple OSS Distributions 		struct workq_threadreq_tailq *bucket = &wq->wq_cooperative_queue[_wq_bucket(req->tr_qos)];
1605*2c2f96dcSApple OSS Distributions 		STAILQ_INSERT_TAIL(bucket, req, tr_link);
1606*2c2f96dcSApple OSS Distributions 
1607*2c2f96dcSApple OSS Distributions 		return _wq_cooperative_queue_refresh_best_req_qos(wq);
1608*2c2f96dcSApple OSS Distributions 	}
1609*2c2f96dcSApple OSS Distributions 
1610*2c2f96dcSApple OSS Distributions 	struct priority_queue_sched_max *q = workq_priority_queue_for_req(wq, req);
1611*2c2f96dcSApple OSS Distributions 
1612*2c2f96dcSApple OSS Distributions 	priority_queue_entry_set_sched_pri(q, &req->tr_entry,
1613*2c2f96dcSApple OSS Distributions 	    workq_priority_for_req(req), false);
1614*2c2f96dcSApple OSS Distributions 
1615*2c2f96dcSApple OSS Distributions 	if (priority_queue_insert(q, &req->tr_entry)) {
1616*2c2f96dcSApple OSS Distributions 		if (workq_threadreq_is_nonovercommit(req)) {
1617*2c2f96dcSApple OSS Distributions 			_wq_thactive_refresh_best_constrained_req_qos(wq);
1618*2c2f96dcSApple OSS Distributions 		}
1619*2c2f96dcSApple OSS Distributions 		return true;
1620*2c2f96dcSApple OSS Distributions 	}
1621*2c2f96dcSApple OSS Distributions 	return false;
1622*2c2f96dcSApple OSS Distributions }
1623*2c2f96dcSApple OSS Distributions 
1624*2c2f96dcSApple OSS Distributions /*
1625*2c2f96dcSApple OSS Distributions  * returns true if one of the following is true (so as to update creator if
1626*2c2f96dcSApple OSS Distributions  * needed):
1627*2c2f96dcSApple OSS Distributions  *
1628*2c2f96dcSApple OSS Distributions  * (a) the next highest request of the pool we dequeued the request from changed
1629*2c2f96dcSApple OSS Distributions  * (b) the next highest requests of the pool the current thread used to be a
1630*2c2f96dcSApple OSS Distributions  * part of, changed
1631*2c2f96dcSApple OSS Distributions  *
1632*2c2f96dcSApple OSS Distributions  * For overcommit, special and constrained pools, the next highest QoS for each
1633*2c2f96dcSApple OSS Distributions  * pool just a MAX of pending requests so tracking (a) is sufficient.
1634*2c2f96dcSApple OSS Distributions  *
1635*2c2f96dcSApple OSS Distributions  * But for cooperative thread pool, the next highest QoS for the pool depends on
1636*2c2f96dcSApple OSS Distributions  * schedule counts in the pool as well. So if the current thread used to be
1637*2c2f96dcSApple OSS Distributions  * cooperative in it's previous logical run ie (b), then that can also affect
1638*2c2f96dcSApple OSS Distributions  * cooperative pool's next best QoS requests.
1639*2c2f96dcSApple OSS Distributions  */
1640*2c2f96dcSApple OSS Distributions static bool
workq_threadreq_dequeue(struct workqueue * wq,workq_threadreq_t req,bool cooperative_sched_count_changed)1641*2c2f96dcSApple OSS Distributions workq_threadreq_dequeue(struct workqueue *wq, workq_threadreq_t req,
1642*2c2f96dcSApple OSS Distributions     bool cooperative_sched_count_changed)
1643*2c2f96dcSApple OSS Distributions {
1644*2c2f96dcSApple OSS Distributions 	wq->wq_reqcount--;
1645*2c2f96dcSApple OSS Distributions 
1646*2c2f96dcSApple OSS Distributions 	bool next_highest_request_changed = false;
1647*2c2f96dcSApple OSS Distributions 
1648*2c2f96dcSApple OSS Distributions 	if (--req->tr_count == 0) {
1649*2c2f96dcSApple OSS Distributions 		if (req->tr_qos == WORKQ_THREAD_QOS_MANAGER) {
1650*2c2f96dcSApple OSS Distributions 			assert(wq->wq_event_manager_threadreq == req);
1651*2c2f96dcSApple OSS Distributions 			assert(req->tr_count == 0);
1652*2c2f96dcSApple OSS Distributions 			wq->wq_event_manager_threadreq = NULL;
1653*2c2f96dcSApple OSS Distributions 
1654*2c2f96dcSApple OSS Distributions 			/* If a cooperative thread was the one which picked up the manager
1655*2c2f96dcSApple OSS Distributions 			 * thread request, we need to reevaluate the cooperative pool
1656*2c2f96dcSApple OSS Distributions 			 * anyways.
1657*2c2f96dcSApple OSS Distributions 			 */
1658*2c2f96dcSApple OSS Distributions 			if (cooperative_sched_count_changed) {
1659*2c2f96dcSApple OSS Distributions 				_wq_cooperative_queue_refresh_best_req_qos(wq);
1660*2c2f96dcSApple OSS Distributions 			}
1661*2c2f96dcSApple OSS Distributions 			return true;
1662*2c2f96dcSApple OSS Distributions 		}
1663*2c2f96dcSApple OSS Distributions 
1664*2c2f96dcSApple OSS Distributions 		if (workq_threadreq_is_cooperative(req)) {
1665*2c2f96dcSApple OSS Distributions 			assert(req->tr_qos != WORKQ_THREAD_QOS_MANAGER);
1666*2c2f96dcSApple OSS Distributions 			assert(req->tr_qos != WORKQ_THREAD_QOS_ABOVEUI);
1667*2c2f96dcSApple OSS Distributions 			/* Account for the fact that BG and MT are coalesced when
1668*2c2f96dcSApple OSS Distributions 			 * calculating best request for cooperative pool
1669*2c2f96dcSApple OSS Distributions 			 */
1670*2c2f96dcSApple OSS Distributions 			assert(_wq_bucket(req->tr_qos) == _wq_bucket(wq->wq_cooperative_queue_best_req_qos));
1671*2c2f96dcSApple OSS Distributions 
1672*2c2f96dcSApple OSS Distributions 			struct workq_threadreq_tailq *bucket = &wq->wq_cooperative_queue[_wq_bucket(req->tr_qos)];
1673*2c2f96dcSApple OSS Distributions 			__assert_only workq_threadreq_t head = STAILQ_FIRST(bucket);
1674*2c2f96dcSApple OSS Distributions 
1675*2c2f96dcSApple OSS Distributions 			assert(head == req);
1676*2c2f96dcSApple OSS Distributions 			STAILQ_REMOVE_HEAD(bucket, tr_link);
1677*2c2f96dcSApple OSS Distributions 
1678*2c2f96dcSApple OSS Distributions 			/*
1679*2c2f96dcSApple OSS Distributions 			 * If the request we're dequeueing is cooperative, then the sched
1680*2c2f96dcSApple OSS Distributions 			 * counts definitely changed.
1681*2c2f96dcSApple OSS Distributions 			 */
1682*2c2f96dcSApple OSS Distributions 			assert(cooperative_sched_count_changed);
1683*2c2f96dcSApple OSS Distributions 		}
1684*2c2f96dcSApple OSS Distributions 
1685*2c2f96dcSApple OSS Distributions 		/*
1686*2c2f96dcSApple OSS Distributions 		 * We want to do the cooperative pool refresh after dequeueing a
1687*2c2f96dcSApple OSS Distributions 		 * cooperative thread request if any (to combine both effects into 1
1688*2c2f96dcSApple OSS Distributions 		 * refresh operation)
1689*2c2f96dcSApple OSS Distributions 		 */
1690*2c2f96dcSApple OSS Distributions 		if (cooperative_sched_count_changed) {
1691*2c2f96dcSApple OSS Distributions 			next_highest_request_changed = _wq_cooperative_queue_refresh_best_req_qos(wq);
1692*2c2f96dcSApple OSS Distributions 		}
1693*2c2f96dcSApple OSS Distributions 
1694*2c2f96dcSApple OSS Distributions 		if (!workq_threadreq_is_cooperative(req)) {
1695*2c2f96dcSApple OSS Distributions 			/*
1696*2c2f96dcSApple OSS Distributions 			 * All other types of requests are enqueued in priority queues
1697*2c2f96dcSApple OSS Distributions 			 */
1698*2c2f96dcSApple OSS Distributions 
1699*2c2f96dcSApple OSS Distributions 			if (priority_queue_remove(workq_priority_queue_for_req(wq, req),
1700*2c2f96dcSApple OSS Distributions 			    &req->tr_entry)) {
1701*2c2f96dcSApple OSS Distributions 				next_highest_request_changed |= true;
1702*2c2f96dcSApple OSS Distributions 				if (workq_threadreq_is_nonovercommit(req)) {
1703*2c2f96dcSApple OSS Distributions 					_wq_thactive_refresh_best_constrained_req_qos(wq);
1704*2c2f96dcSApple OSS Distributions 				}
1705*2c2f96dcSApple OSS Distributions 			}
1706*2c2f96dcSApple OSS Distributions 		}
1707*2c2f96dcSApple OSS Distributions 	}
1708*2c2f96dcSApple OSS Distributions 
1709*2c2f96dcSApple OSS Distributions 	return next_highest_request_changed;
1710*2c2f96dcSApple OSS Distributions }
1711*2c2f96dcSApple OSS Distributions 
1712*2c2f96dcSApple OSS Distributions static void
workq_threadreq_destroy(proc_t p,workq_threadreq_t req)1713*2c2f96dcSApple OSS Distributions workq_threadreq_destroy(proc_t p, workq_threadreq_t req)
1714*2c2f96dcSApple OSS Distributions {
1715*2c2f96dcSApple OSS Distributions 	req->tr_state = WORKQ_TR_STATE_CANCELED;
1716*2c2f96dcSApple OSS Distributions 	if (req->tr_flags & (WORKQ_TR_FLAG_WORKLOOP | WORKQ_TR_FLAG_KEVENT)) {
1717*2c2f96dcSApple OSS Distributions 		kqueue_threadreq_cancel(p, req);
1718*2c2f96dcSApple OSS Distributions 	} else {
1719*2c2f96dcSApple OSS Distributions 		zfree(workq_zone_threadreq, req);
1720*2c2f96dcSApple OSS Distributions 	}
1721*2c2f96dcSApple OSS Distributions }
1722*2c2f96dcSApple OSS Distributions 
1723*2c2f96dcSApple OSS Distributions #pragma mark workqueue thread creation thread calls
1724*2c2f96dcSApple OSS Distributions 
1725*2c2f96dcSApple OSS Distributions static inline bool
workq_thread_call_prepost(struct workqueue * wq,uint32_t sched,uint32_t pend,uint32_t fail_mask)1726*2c2f96dcSApple OSS Distributions workq_thread_call_prepost(struct workqueue *wq, uint32_t sched, uint32_t pend,
1727*2c2f96dcSApple OSS Distributions     uint32_t fail_mask)
1728*2c2f96dcSApple OSS Distributions {
1729*2c2f96dcSApple OSS Distributions 	uint32_t old_flags, new_flags;
1730*2c2f96dcSApple OSS Distributions 
1731*2c2f96dcSApple OSS Distributions 	os_atomic_rmw_loop(&wq->wq_flags, old_flags, new_flags, acquire, {
1732*2c2f96dcSApple OSS Distributions 		if (__improbable(old_flags & (WQ_EXITING | sched | pend | fail_mask))) {
1733*2c2f96dcSApple OSS Distributions 		        os_atomic_rmw_loop_give_up(return false);
1734*2c2f96dcSApple OSS Distributions 		}
1735*2c2f96dcSApple OSS Distributions 		if (__improbable(old_flags & WQ_PROC_SUSPENDED)) {
1736*2c2f96dcSApple OSS Distributions 		        new_flags = old_flags | pend;
1737*2c2f96dcSApple OSS Distributions 		} else {
1738*2c2f96dcSApple OSS Distributions 		        new_flags = old_flags | sched;
1739*2c2f96dcSApple OSS Distributions 		}
1740*2c2f96dcSApple OSS Distributions 	});
1741*2c2f96dcSApple OSS Distributions 
1742*2c2f96dcSApple OSS Distributions 	return (old_flags & WQ_PROC_SUSPENDED) == 0;
1743*2c2f96dcSApple OSS Distributions }
1744*2c2f96dcSApple OSS Distributions 
1745*2c2f96dcSApple OSS Distributions #define WORKQ_SCHEDULE_DELAYED_THREAD_CREATION_RESTART 0x1
1746*2c2f96dcSApple OSS Distributions 
1747*2c2f96dcSApple OSS Distributions static bool
workq_schedule_delayed_thread_creation(struct workqueue * wq,int flags)1748*2c2f96dcSApple OSS Distributions workq_schedule_delayed_thread_creation(struct workqueue *wq, int flags)
1749*2c2f96dcSApple OSS Distributions {
1750*2c2f96dcSApple OSS Distributions 	assert(!preemption_enabled());
1751*2c2f96dcSApple OSS Distributions 
1752*2c2f96dcSApple OSS Distributions 	if (!workq_thread_call_prepost(wq, WQ_DELAYED_CALL_SCHEDULED,
1753*2c2f96dcSApple OSS Distributions 	    WQ_DELAYED_CALL_PENDED, WQ_IMMEDIATE_CALL_PENDED |
1754*2c2f96dcSApple OSS Distributions 	    WQ_IMMEDIATE_CALL_SCHEDULED)) {
1755*2c2f96dcSApple OSS Distributions 		return false;
1756*2c2f96dcSApple OSS Distributions 	}
1757*2c2f96dcSApple OSS Distributions 
1758*2c2f96dcSApple OSS Distributions 	uint64_t now = mach_absolute_time();
1759*2c2f96dcSApple OSS Distributions 
1760*2c2f96dcSApple OSS Distributions 	if (flags & WORKQ_SCHEDULE_DELAYED_THREAD_CREATION_RESTART) {
1761*2c2f96dcSApple OSS Distributions 		/* do not change the window */
1762*2c2f96dcSApple OSS Distributions 	} else if (now - wq->wq_thread_call_last_run <= wq->wq_timer_interval) {
1763*2c2f96dcSApple OSS Distributions 		wq->wq_timer_interval *= 2;
1764*2c2f96dcSApple OSS Distributions 		if (wq->wq_timer_interval > wq_max_timer_interval.abstime) {
1765*2c2f96dcSApple OSS Distributions 			wq->wq_timer_interval = (uint32_t)wq_max_timer_interval.abstime;
1766*2c2f96dcSApple OSS Distributions 		}
1767*2c2f96dcSApple OSS Distributions 	} else if (now - wq->wq_thread_call_last_run > 2 * wq->wq_timer_interval) {
1768*2c2f96dcSApple OSS Distributions 		wq->wq_timer_interval /= 2;
1769*2c2f96dcSApple OSS Distributions 		if (wq->wq_timer_interval < wq_stalled_window.abstime) {
1770*2c2f96dcSApple OSS Distributions 			wq->wq_timer_interval = (uint32_t)wq_stalled_window.abstime;
1771*2c2f96dcSApple OSS Distributions 		}
1772*2c2f96dcSApple OSS Distributions 	}
1773*2c2f96dcSApple OSS Distributions 
1774*2c2f96dcSApple OSS Distributions 	WQ_TRACE_WQ(TRACE_wq_start_add_timer, wq, wq->wq_reqcount,
1775*2c2f96dcSApple OSS Distributions 	    _wq_flags(wq), wq->wq_timer_interval);
1776*2c2f96dcSApple OSS Distributions 
1777*2c2f96dcSApple OSS Distributions 	thread_call_t call = wq->wq_delayed_call;
1778*2c2f96dcSApple OSS Distributions 	uintptr_t arg = WQ_DELAYED_CALL_SCHEDULED;
1779*2c2f96dcSApple OSS Distributions 	uint64_t deadline = now + wq->wq_timer_interval;
1780*2c2f96dcSApple OSS Distributions 	if (thread_call_enter1_delayed(call, (void *)arg, deadline)) {
1781*2c2f96dcSApple OSS Distributions 		panic("delayed_call was already enqueued");
1782*2c2f96dcSApple OSS Distributions 	}
1783*2c2f96dcSApple OSS Distributions 	return true;
1784*2c2f96dcSApple OSS Distributions }
1785*2c2f96dcSApple OSS Distributions 
1786*2c2f96dcSApple OSS Distributions static void
workq_schedule_immediate_thread_creation(struct workqueue * wq)1787*2c2f96dcSApple OSS Distributions workq_schedule_immediate_thread_creation(struct workqueue *wq)
1788*2c2f96dcSApple OSS Distributions {
1789*2c2f96dcSApple OSS Distributions 	assert(!preemption_enabled());
1790*2c2f96dcSApple OSS Distributions 
1791*2c2f96dcSApple OSS Distributions 	if (workq_thread_call_prepost(wq, WQ_IMMEDIATE_CALL_SCHEDULED,
1792*2c2f96dcSApple OSS Distributions 	    WQ_IMMEDIATE_CALL_PENDED, 0)) {
1793*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_start_add_timer, wq, wq->wq_reqcount,
1794*2c2f96dcSApple OSS Distributions 		    _wq_flags(wq), 0);
1795*2c2f96dcSApple OSS Distributions 
1796*2c2f96dcSApple OSS Distributions 		uintptr_t arg = WQ_IMMEDIATE_CALL_SCHEDULED;
1797*2c2f96dcSApple OSS Distributions 		if (thread_call_enter1(wq->wq_immediate_call, (void *)arg)) {
1798*2c2f96dcSApple OSS Distributions 			panic("immediate_call was already enqueued");
1799*2c2f96dcSApple OSS Distributions 		}
1800*2c2f96dcSApple OSS Distributions 	}
1801*2c2f96dcSApple OSS Distributions }
1802*2c2f96dcSApple OSS Distributions 
1803*2c2f96dcSApple OSS Distributions void
workq_proc_suspended(struct proc * p)1804*2c2f96dcSApple OSS Distributions workq_proc_suspended(struct proc *p)
1805*2c2f96dcSApple OSS Distributions {
1806*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = proc_get_wqptr(p);
1807*2c2f96dcSApple OSS Distributions 
1808*2c2f96dcSApple OSS Distributions 	if (wq) {
1809*2c2f96dcSApple OSS Distributions 		os_atomic_or(&wq->wq_flags, WQ_PROC_SUSPENDED, relaxed);
1810*2c2f96dcSApple OSS Distributions 	}
1811*2c2f96dcSApple OSS Distributions }
1812*2c2f96dcSApple OSS Distributions 
1813*2c2f96dcSApple OSS Distributions void
workq_proc_resumed(struct proc * p)1814*2c2f96dcSApple OSS Distributions workq_proc_resumed(struct proc *p)
1815*2c2f96dcSApple OSS Distributions {
1816*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = proc_get_wqptr(p);
1817*2c2f96dcSApple OSS Distributions 	uint32_t wq_flags;
1818*2c2f96dcSApple OSS Distributions 
1819*2c2f96dcSApple OSS Distributions 	if (!wq) {
1820*2c2f96dcSApple OSS Distributions 		return;
1821*2c2f96dcSApple OSS Distributions 	}
1822*2c2f96dcSApple OSS Distributions 
1823*2c2f96dcSApple OSS Distributions 	wq_flags = os_atomic_andnot_orig(&wq->wq_flags, WQ_PROC_SUSPENDED |
1824*2c2f96dcSApple OSS Distributions 	    WQ_DELAYED_CALL_PENDED | WQ_IMMEDIATE_CALL_PENDED, relaxed);
1825*2c2f96dcSApple OSS Distributions 	if ((wq_flags & WQ_EXITING) == 0) {
1826*2c2f96dcSApple OSS Distributions 		disable_preemption();
1827*2c2f96dcSApple OSS Distributions 		if (wq_flags & WQ_IMMEDIATE_CALL_PENDED) {
1828*2c2f96dcSApple OSS Distributions 			workq_schedule_immediate_thread_creation(wq);
1829*2c2f96dcSApple OSS Distributions 		} else if (wq_flags & WQ_DELAYED_CALL_PENDED) {
1830*2c2f96dcSApple OSS Distributions 			workq_schedule_delayed_thread_creation(wq,
1831*2c2f96dcSApple OSS Distributions 			    WORKQ_SCHEDULE_DELAYED_THREAD_CREATION_RESTART);
1832*2c2f96dcSApple OSS Distributions 		}
1833*2c2f96dcSApple OSS Distributions 		enable_preemption();
1834*2c2f96dcSApple OSS Distributions 	}
1835*2c2f96dcSApple OSS Distributions }
1836*2c2f96dcSApple OSS Distributions 
1837*2c2f96dcSApple OSS Distributions /**
1838*2c2f96dcSApple OSS Distributions  * returns whether lastblocked_tsp is within wq_stalled_window usecs of now
1839*2c2f96dcSApple OSS Distributions  */
1840*2c2f96dcSApple OSS Distributions static bool
workq_thread_is_busy(uint64_t now,_Atomic uint64_t * lastblocked_tsp)1841*2c2f96dcSApple OSS Distributions workq_thread_is_busy(uint64_t now, _Atomic uint64_t *lastblocked_tsp)
1842*2c2f96dcSApple OSS Distributions {
1843*2c2f96dcSApple OSS Distributions 	uint64_t lastblocked_ts = os_atomic_load_wide(lastblocked_tsp, relaxed);
1844*2c2f96dcSApple OSS Distributions 	if (now <= lastblocked_ts) {
1845*2c2f96dcSApple OSS Distributions 		/*
1846*2c2f96dcSApple OSS Distributions 		 * Because the update of the timestamp when a thread blocks
1847*2c2f96dcSApple OSS Distributions 		 * isn't serialized against us looking at it (i.e. we don't hold
1848*2c2f96dcSApple OSS Distributions 		 * the workq lock), it's possible to have a timestamp that matches
1849*2c2f96dcSApple OSS Distributions 		 * the current time or that even looks to be in the future relative
1850*2c2f96dcSApple OSS Distributions 		 * to when we grabbed the current time...
1851*2c2f96dcSApple OSS Distributions 		 *
1852*2c2f96dcSApple OSS Distributions 		 * Just treat this as a busy thread since it must have just blocked.
1853*2c2f96dcSApple OSS Distributions 		 */
1854*2c2f96dcSApple OSS Distributions 		return true;
1855*2c2f96dcSApple OSS Distributions 	}
1856*2c2f96dcSApple OSS Distributions 	return (now - lastblocked_ts) < wq_stalled_window.abstime;
1857*2c2f96dcSApple OSS Distributions }
1858*2c2f96dcSApple OSS Distributions 
1859*2c2f96dcSApple OSS Distributions static void
workq_add_new_threads_call(void * _p,void * flags)1860*2c2f96dcSApple OSS Distributions workq_add_new_threads_call(void *_p, void *flags)
1861*2c2f96dcSApple OSS Distributions {
1862*2c2f96dcSApple OSS Distributions 	proc_t p = _p;
1863*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = proc_get_wqptr(p);
1864*2c2f96dcSApple OSS Distributions 	uint32_t my_flag = (uint32_t)(uintptr_t)flags;
1865*2c2f96dcSApple OSS Distributions 
1866*2c2f96dcSApple OSS Distributions 	/*
1867*2c2f96dcSApple OSS Distributions 	 * workq_exit() will set the workqueue to NULL before
1868*2c2f96dcSApple OSS Distributions 	 * it cancels thread calls.
1869*2c2f96dcSApple OSS Distributions 	 */
1870*2c2f96dcSApple OSS Distributions 	if (!wq) {
1871*2c2f96dcSApple OSS Distributions 		return;
1872*2c2f96dcSApple OSS Distributions 	}
1873*2c2f96dcSApple OSS Distributions 
1874*2c2f96dcSApple OSS Distributions 	assert((my_flag == WQ_DELAYED_CALL_SCHEDULED) ||
1875*2c2f96dcSApple OSS Distributions 	    (my_flag == WQ_IMMEDIATE_CALL_SCHEDULED));
1876*2c2f96dcSApple OSS Distributions 
1877*2c2f96dcSApple OSS Distributions 	WQ_TRACE_WQ(TRACE_wq_add_timer | DBG_FUNC_START, wq, _wq_flags(wq),
1878*2c2f96dcSApple OSS Distributions 	    wq->wq_nthreads, wq->wq_thidlecount);
1879*2c2f96dcSApple OSS Distributions 
1880*2c2f96dcSApple OSS Distributions 	workq_lock_spin(wq);
1881*2c2f96dcSApple OSS Distributions 
1882*2c2f96dcSApple OSS Distributions 	wq->wq_thread_call_last_run = mach_absolute_time();
1883*2c2f96dcSApple OSS Distributions 	os_atomic_andnot(&wq->wq_flags, my_flag, release);
1884*2c2f96dcSApple OSS Distributions 
1885*2c2f96dcSApple OSS Distributions 	/* This can drop the workqueue lock, and take it again */
1886*2c2f96dcSApple OSS Distributions 	workq_schedule_creator(p, wq, WORKQ_THREADREQ_CAN_CREATE_THREADS);
1887*2c2f96dcSApple OSS Distributions 
1888*2c2f96dcSApple OSS Distributions 	workq_unlock(wq);
1889*2c2f96dcSApple OSS Distributions 
1890*2c2f96dcSApple OSS Distributions 	WQ_TRACE_WQ(TRACE_wq_add_timer | DBG_FUNC_END, wq, 0,
1891*2c2f96dcSApple OSS Distributions 	    wq->wq_nthreads, wq->wq_thidlecount);
1892*2c2f96dcSApple OSS Distributions }
1893*2c2f96dcSApple OSS Distributions 
1894*2c2f96dcSApple OSS Distributions #pragma mark thread state tracking
1895*2c2f96dcSApple OSS Distributions 
1896*2c2f96dcSApple OSS Distributions static void
workq_sched_callback(int type,thread_t thread)1897*2c2f96dcSApple OSS Distributions workq_sched_callback(int type, thread_t thread)
1898*2c2f96dcSApple OSS Distributions {
1899*2c2f96dcSApple OSS Distributions 	thread_ro_t tro = get_thread_ro(thread);
1900*2c2f96dcSApple OSS Distributions 	struct uthread *uth = get_bsdthread_info(thread);
1901*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = proc_get_wqptr(tro->tro_proc);
1902*2c2f96dcSApple OSS Distributions 	thread_qos_t req_qos, qos = uth->uu_workq_pri.qos_bucket;
1903*2c2f96dcSApple OSS Distributions 	wq_thactive_t old_thactive;
1904*2c2f96dcSApple OSS Distributions 	bool start_timer = false;
1905*2c2f96dcSApple OSS Distributions 
1906*2c2f96dcSApple OSS Distributions 	if (qos == WORKQ_THREAD_QOS_MANAGER) {
1907*2c2f96dcSApple OSS Distributions 		return;
1908*2c2f96dcSApple OSS Distributions 	}
1909*2c2f96dcSApple OSS Distributions 
1910*2c2f96dcSApple OSS Distributions 	switch (type) {
1911*2c2f96dcSApple OSS Distributions 	case SCHED_CALL_BLOCK:
1912*2c2f96dcSApple OSS Distributions 		old_thactive = _wq_thactive_dec(wq, qos);
1913*2c2f96dcSApple OSS Distributions 		req_qos = WQ_THACTIVE_BEST_CONSTRAINED_REQ_QOS(old_thactive);
1914*2c2f96dcSApple OSS Distributions 
1915*2c2f96dcSApple OSS Distributions 		/*
1916*2c2f96dcSApple OSS Distributions 		 * Remember the timestamp of the last thread that blocked in this
1917*2c2f96dcSApple OSS Distributions 		 * bucket, it used used by admission checks to ignore one thread
1918*2c2f96dcSApple OSS Distributions 		 * being inactive if this timestamp is recent enough.
1919*2c2f96dcSApple OSS Distributions 		 *
1920*2c2f96dcSApple OSS Distributions 		 * If we collide with another thread trying to update the
1921*2c2f96dcSApple OSS Distributions 		 * last_blocked (really unlikely since another thread would have to
1922*2c2f96dcSApple OSS Distributions 		 * get scheduled and then block after we start down this path), it's
1923*2c2f96dcSApple OSS Distributions 		 * not a problem.  Either timestamp is adequate, so no need to retry
1924*2c2f96dcSApple OSS Distributions 		 */
1925*2c2f96dcSApple OSS Distributions 		os_atomic_store_wide(&wq->wq_lastblocked_ts[_wq_bucket(qos)],
1926*2c2f96dcSApple OSS Distributions 		    thread_last_run_time(thread), relaxed);
1927*2c2f96dcSApple OSS Distributions 
1928*2c2f96dcSApple OSS Distributions 		if (req_qos == THREAD_QOS_UNSPECIFIED) {
1929*2c2f96dcSApple OSS Distributions 			/*
1930*2c2f96dcSApple OSS Distributions 			 * No pending request at the moment we could unblock, move on.
1931*2c2f96dcSApple OSS Distributions 			 */
1932*2c2f96dcSApple OSS Distributions 		} else if (qos < req_qos) {
1933*2c2f96dcSApple OSS Distributions 			/*
1934*2c2f96dcSApple OSS Distributions 			 * The blocking thread is at a lower QoS than the highest currently
1935*2c2f96dcSApple OSS Distributions 			 * pending constrained request, nothing has to be redriven
1936*2c2f96dcSApple OSS Distributions 			 */
1937*2c2f96dcSApple OSS Distributions 		} else {
1938*2c2f96dcSApple OSS Distributions 			uint32_t max_busycount, old_req_count;
1939*2c2f96dcSApple OSS Distributions 			old_req_count = _wq_thactive_aggregate_downto_qos(wq, old_thactive,
1940*2c2f96dcSApple OSS Distributions 			    req_qos, NULL, &max_busycount);
1941*2c2f96dcSApple OSS Distributions 			/*
1942*2c2f96dcSApple OSS Distributions 			 * If it is possible that may_start_constrained_thread had refused
1943*2c2f96dcSApple OSS Distributions 			 * admission due to being over the max concurrency, we may need to
1944*2c2f96dcSApple OSS Distributions 			 * spin up a new thread.
1945*2c2f96dcSApple OSS Distributions 			 *
1946*2c2f96dcSApple OSS Distributions 			 * We take into account the maximum number of busy threads
1947*2c2f96dcSApple OSS Distributions 			 * that can affect may_start_constrained_thread as looking at the
1948*2c2f96dcSApple OSS Distributions 			 * actual number may_start_constrained_thread will see is racy.
1949*2c2f96dcSApple OSS Distributions 			 *
1950*2c2f96dcSApple OSS Distributions 			 * IOW at NCPU = 4, for IN (req_qos = 1), if the old req count is
1951*2c2f96dcSApple OSS Distributions 			 * between NCPU (4) and NCPU - 2 (2) we need to redrive.
1952*2c2f96dcSApple OSS Distributions 			 */
1953*2c2f96dcSApple OSS Distributions 			uint32_t conc = wq_max_parallelism[_wq_bucket(qos)];
1954*2c2f96dcSApple OSS Distributions 			if (old_req_count <= conc && conc <= old_req_count + max_busycount) {
1955*2c2f96dcSApple OSS Distributions 				start_timer = workq_schedule_delayed_thread_creation(wq, 0);
1956*2c2f96dcSApple OSS Distributions 			}
1957*2c2f96dcSApple OSS Distributions 		}
1958*2c2f96dcSApple OSS Distributions 		if (__improbable(kdebug_enable)) {
1959*2c2f96dcSApple OSS Distributions 			__unused uint32_t old = _wq_thactive_aggregate_downto_qos(wq,
1960*2c2f96dcSApple OSS Distributions 			    old_thactive, qos, NULL, NULL);
1961*2c2f96dcSApple OSS Distributions 			WQ_TRACE_WQ(TRACE_wq_thread_block | DBG_FUNC_START, wq,
1962*2c2f96dcSApple OSS Distributions 			    old - 1, qos | (req_qos << 8),
1963*2c2f96dcSApple OSS Distributions 			    wq->wq_reqcount << 1 | start_timer);
1964*2c2f96dcSApple OSS Distributions 		}
1965*2c2f96dcSApple OSS Distributions 		break;
1966*2c2f96dcSApple OSS Distributions 
1967*2c2f96dcSApple OSS Distributions 	case SCHED_CALL_UNBLOCK:
1968*2c2f96dcSApple OSS Distributions 		/*
1969*2c2f96dcSApple OSS Distributions 		 * we cannot take the workqueue_lock here...
1970*2c2f96dcSApple OSS Distributions 		 * an UNBLOCK can occur from a timer event which
1971*2c2f96dcSApple OSS Distributions 		 * is run from an interrupt context... if the workqueue_lock
1972*2c2f96dcSApple OSS Distributions 		 * is already held by this processor, we'll deadlock...
1973*2c2f96dcSApple OSS Distributions 		 * the thread lock for the thread being UNBLOCKED
1974*2c2f96dcSApple OSS Distributions 		 * is also held
1975*2c2f96dcSApple OSS Distributions 		 */
1976*2c2f96dcSApple OSS Distributions 		old_thactive = _wq_thactive_inc(wq, qos);
1977*2c2f96dcSApple OSS Distributions 		if (__improbable(kdebug_enable)) {
1978*2c2f96dcSApple OSS Distributions 			__unused uint32_t old = _wq_thactive_aggregate_downto_qos(wq,
1979*2c2f96dcSApple OSS Distributions 			    old_thactive, qos, NULL, NULL);
1980*2c2f96dcSApple OSS Distributions 			req_qos = WQ_THACTIVE_BEST_CONSTRAINED_REQ_QOS(old_thactive);
1981*2c2f96dcSApple OSS Distributions 			WQ_TRACE_WQ(TRACE_wq_thread_block | DBG_FUNC_END, wq,
1982*2c2f96dcSApple OSS Distributions 			    old + 1, qos | (req_qos << 8),
1983*2c2f96dcSApple OSS Distributions 			    wq->wq_threads_scheduled);
1984*2c2f96dcSApple OSS Distributions 		}
1985*2c2f96dcSApple OSS Distributions 		break;
1986*2c2f96dcSApple OSS Distributions 	}
1987*2c2f96dcSApple OSS Distributions }
1988*2c2f96dcSApple OSS Distributions 
1989*2c2f96dcSApple OSS Distributions #pragma mark workq lifecycle
1990*2c2f96dcSApple OSS Distributions 
1991*2c2f96dcSApple OSS Distributions void
workq_reference(struct workqueue * wq)1992*2c2f96dcSApple OSS Distributions workq_reference(struct workqueue *wq)
1993*2c2f96dcSApple OSS Distributions {
1994*2c2f96dcSApple OSS Distributions 	os_ref_retain(&wq->wq_refcnt);
1995*2c2f96dcSApple OSS Distributions }
1996*2c2f96dcSApple OSS Distributions 
1997*2c2f96dcSApple OSS Distributions static void
workq_deallocate_queue_invoke(mpsc_queue_chain_t e,__assert_only mpsc_daemon_queue_t dq)1998*2c2f96dcSApple OSS Distributions workq_deallocate_queue_invoke(mpsc_queue_chain_t e,
1999*2c2f96dcSApple OSS Distributions     __assert_only mpsc_daemon_queue_t dq)
2000*2c2f96dcSApple OSS Distributions {
2001*2c2f96dcSApple OSS Distributions 	struct workqueue *wq;
2002*2c2f96dcSApple OSS Distributions 	struct turnstile *ts;
2003*2c2f96dcSApple OSS Distributions 
2004*2c2f96dcSApple OSS Distributions 	wq = mpsc_queue_element(e, struct workqueue, wq_destroy_link);
2005*2c2f96dcSApple OSS Distributions 	assert(dq == &workq_deallocate_queue);
2006*2c2f96dcSApple OSS Distributions 
2007*2c2f96dcSApple OSS Distributions 	turnstile_complete((uintptr_t)wq, &wq->wq_turnstile, &ts, TURNSTILE_WORKQS);
2008*2c2f96dcSApple OSS Distributions 	assert(ts);
2009*2c2f96dcSApple OSS Distributions 	turnstile_cleanup();
2010*2c2f96dcSApple OSS Distributions 	turnstile_deallocate(ts);
2011*2c2f96dcSApple OSS Distributions 
2012*2c2f96dcSApple OSS Distributions 	lck_ticket_destroy(&wq->wq_lock, &workq_lck_grp);
2013*2c2f96dcSApple OSS Distributions 	zfree(workq_zone_workqueue, wq);
2014*2c2f96dcSApple OSS Distributions }
2015*2c2f96dcSApple OSS Distributions 
2016*2c2f96dcSApple OSS Distributions static void
workq_deallocate(struct workqueue * wq)2017*2c2f96dcSApple OSS Distributions workq_deallocate(struct workqueue *wq)
2018*2c2f96dcSApple OSS Distributions {
2019*2c2f96dcSApple OSS Distributions 	if (os_ref_release_relaxed(&wq->wq_refcnt) == 0) {
2020*2c2f96dcSApple OSS Distributions 		workq_deallocate_queue_invoke(&wq->wq_destroy_link,
2021*2c2f96dcSApple OSS Distributions 		    &workq_deallocate_queue);
2022*2c2f96dcSApple OSS Distributions 	}
2023*2c2f96dcSApple OSS Distributions }
2024*2c2f96dcSApple OSS Distributions 
2025*2c2f96dcSApple OSS Distributions void
workq_deallocate_safe(struct workqueue * wq)2026*2c2f96dcSApple OSS Distributions workq_deallocate_safe(struct workqueue *wq)
2027*2c2f96dcSApple OSS Distributions {
2028*2c2f96dcSApple OSS Distributions 	if (__improbable(os_ref_release_relaxed(&wq->wq_refcnt) == 0)) {
2029*2c2f96dcSApple OSS Distributions 		mpsc_daemon_enqueue(&workq_deallocate_queue, &wq->wq_destroy_link,
2030*2c2f96dcSApple OSS Distributions 		    MPSC_QUEUE_DISABLE_PREEMPTION);
2031*2c2f96dcSApple OSS Distributions 	}
2032*2c2f96dcSApple OSS Distributions }
2033*2c2f96dcSApple OSS Distributions 
2034*2c2f96dcSApple OSS Distributions /**
2035*2c2f96dcSApple OSS Distributions  * Setup per-process state for the workqueue.
2036*2c2f96dcSApple OSS Distributions  */
2037*2c2f96dcSApple OSS Distributions int
workq_open(struct proc * p,__unused struct workq_open_args * uap,__unused int32_t * retval)2038*2c2f96dcSApple OSS Distributions workq_open(struct proc *p, __unused struct workq_open_args *uap,
2039*2c2f96dcSApple OSS Distributions     __unused int32_t *retval)
2040*2c2f96dcSApple OSS Distributions {
2041*2c2f96dcSApple OSS Distributions 	struct workqueue *wq;
2042*2c2f96dcSApple OSS Distributions 	int error = 0;
2043*2c2f96dcSApple OSS Distributions 
2044*2c2f96dcSApple OSS Distributions 	if ((p->p_lflag & P_LREGISTER) == 0) {
2045*2c2f96dcSApple OSS Distributions 		return EINVAL;
2046*2c2f96dcSApple OSS Distributions 	}
2047*2c2f96dcSApple OSS Distributions 
2048*2c2f96dcSApple OSS Distributions 	if (wq_init_constrained_limit) {
2049*2c2f96dcSApple OSS Distributions 		uint32_t limit, num_cpus = ml_wait_max_cpus();
2050*2c2f96dcSApple OSS Distributions 
2051*2c2f96dcSApple OSS Distributions 		/*
2052*2c2f96dcSApple OSS Distributions 		 * set up the limit for the constrained pool
2053*2c2f96dcSApple OSS Distributions 		 * this is a virtual pool in that we don't
2054*2c2f96dcSApple OSS Distributions 		 * maintain it on a separate idle and run list
2055*2c2f96dcSApple OSS Distributions 		 */
2056*2c2f96dcSApple OSS Distributions 		limit = num_cpus * WORKQUEUE_CONSTRAINED_FACTOR;
2057*2c2f96dcSApple OSS Distributions 
2058*2c2f96dcSApple OSS Distributions 		if (limit > wq_max_constrained_threads) {
2059*2c2f96dcSApple OSS Distributions 			wq_max_constrained_threads = limit;
2060*2c2f96dcSApple OSS Distributions 		}
2061*2c2f96dcSApple OSS Distributions 
2062*2c2f96dcSApple OSS Distributions 		if (wq_max_threads > WQ_THACTIVE_BUCKET_HALF) {
2063*2c2f96dcSApple OSS Distributions 			wq_max_threads = WQ_THACTIVE_BUCKET_HALF;
2064*2c2f96dcSApple OSS Distributions 		}
2065*2c2f96dcSApple OSS Distributions 		if (wq_max_threads > CONFIG_THREAD_MAX - 20) {
2066*2c2f96dcSApple OSS Distributions 			wq_max_threads = CONFIG_THREAD_MAX - 20;
2067*2c2f96dcSApple OSS Distributions 		}
2068*2c2f96dcSApple OSS Distributions 
2069*2c2f96dcSApple OSS Distributions 		wq_death_max_load = (uint16_t)fls(num_cpus) + 1;
2070*2c2f96dcSApple OSS Distributions 
2071*2c2f96dcSApple OSS Distributions 		for (thread_qos_t qos = WORKQ_THREAD_QOS_MIN; qos <= WORKQ_THREAD_QOS_MAX; qos++) {
2072*2c2f96dcSApple OSS Distributions 			wq_max_parallelism[_wq_bucket(qos)] =
2073*2c2f96dcSApple OSS Distributions 			    qos_max_parallelism(qos, QOS_PARALLELISM_COUNT_LOGICAL);
2074*2c2f96dcSApple OSS Distributions 		}
2075*2c2f96dcSApple OSS Distributions 
2076*2c2f96dcSApple OSS Distributions 		wq_max_cooperative_threads = num_cpus;
2077*2c2f96dcSApple OSS Distributions 
2078*2c2f96dcSApple OSS Distributions 		wq_init_constrained_limit = 0;
2079*2c2f96dcSApple OSS Distributions 	}
2080*2c2f96dcSApple OSS Distributions 
2081*2c2f96dcSApple OSS Distributions 	if (proc_get_wqptr(p) == NULL) {
2082*2c2f96dcSApple OSS Distributions 		if (proc_init_wqptr_or_wait(p) == FALSE) {
2083*2c2f96dcSApple OSS Distributions 			assert(proc_get_wqptr(p) != NULL);
2084*2c2f96dcSApple OSS Distributions 			goto out;
2085*2c2f96dcSApple OSS Distributions 		}
2086*2c2f96dcSApple OSS Distributions 
2087*2c2f96dcSApple OSS Distributions 		wq = zalloc_flags(workq_zone_workqueue, Z_WAITOK | Z_ZERO);
2088*2c2f96dcSApple OSS Distributions 
2089*2c2f96dcSApple OSS Distributions 		os_ref_init_count(&wq->wq_refcnt, &workq_refgrp, 1);
2090*2c2f96dcSApple OSS Distributions 
2091*2c2f96dcSApple OSS Distributions 		// Start the event manager at the priority hinted at by the policy engine
2092*2c2f96dcSApple OSS Distributions 		thread_qos_t mgr_priority_hint = task_get_default_manager_qos(current_task());
2093*2c2f96dcSApple OSS Distributions 		pthread_priority_t pp = _pthread_priority_make_from_thread_qos(mgr_priority_hint, 0, 0);
2094*2c2f96dcSApple OSS Distributions 		wq->wq_event_manager_priority = (uint32_t)pp;
2095*2c2f96dcSApple OSS Distributions 		wq->wq_timer_interval = (uint32_t)wq_stalled_window.abstime;
2096*2c2f96dcSApple OSS Distributions 		wq->wq_proc = p;
2097*2c2f96dcSApple OSS Distributions 		turnstile_prepare((uintptr_t)wq, &wq->wq_turnstile, turnstile_alloc(),
2098*2c2f96dcSApple OSS Distributions 		    TURNSTILE_WORKQS);
2099*2c2f96dcSApple OSS Distributions 
2100*2c2f96dcSApple OSS Distributions 		TAILQ_INIT(&wq->wq_thrunlist);
2101*2c2f96dcSApple OSS Distributions 		TAILQ_INIT(&wq->wq_thnewlist);
2102*2c2f96dcSApple OSS Distributions 		TAILQ_INIT(&wq->wq_thidlelist);
2103*2c2f96dcSApple OSS Distributions 		priority_queue_init(&wq->wq_overcommit_queue);
2104*2c2f96dcSApple OSS Distributions 		priority_queue_init(&wq->wq_constrained_queue);
2105*2c2f96dcSApple OSS Distributions 		priority_queue_init(&wq->wq_special_queue);
2106*2c2f96dcSApple OSS Distributions 		for (int bucket = 0; bucket < WORKQ_NUM_QOS_BUCKETS; bucket++) {
2107*2c2f96dcSApple OSS Distributions 			STAILQ_INIT(&wq->wq_cooperative_queue[bucket]);
2108*2c2f96dcSApple OSS Distributions 		}
2109*2c2f96dcSApple OSS Distributions 
2110*2c2f96dcSApple OSS Distributions 		/* We are only using the delayed thread call for the constrained pool
2111*2c2f96dcSApple OSS Distributions 		 * which can't have work at >= UI QoS and so we can be fine with a
2112*2c2f96dcSApple OSS Distributions 		 * UI QoS thread call.
2113*2c2f96dcSApple OSS Distributions 		 */
2114*2c2f96dcSApple OSS Distributions 		wq->wq_delayed_call = thread_call_allocate_with_qos(
2115*2c2f96dcSApple OSS Distributions 			workq_add_new_threads_call, p, THREAD_QOS_USER_INTERACTIVE,
2116*2c2f96dcSApple OSS Distributions 			THREAD_CALL_OPTIONS_ONCE);
2117*2c2f96dcSApple OSS Distributions 		wq->wq_immediate_call = thread_call_allocate_with_options(
2118*2c2f96dcSApple OSS Distributions 			workq_add_new_threads_call, p, THREAD_CALL_PRIORITY_KERNEL,
2119*2c2f96dcSApple OSS Distributions 			THREAD_CALL_OPTIONS_ONCE);
2120*2c2f96dcSApple OSS Distributions 		wq->wq_death_call = thread_call_allocate_with_options(
2121*2c2f96dcSApple OSS Distributions 			workq_kill_old_threads_call, wq,
2122*2c2f96dcSApple OSS Distributions 			THREAD_CALL_PRIORITY_USER, THREAD_CALL_OPTIONS_ONCE);
2123*2c2f96dcSApple OSS Distributions 
2124*2c2f96dcSApple OSS Distributions 		lck_ticket_init(&wq->wq_lock, &workq_lck_grp);
2125*2c2f96dcSApple OSS Distributions 
2126*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_create | DBG_FUNC_NONE, wq,
2127*2c2f96dcSApple OSS Distributions 		    VM_KERNEL_ADDRHIDE(wq), 0, 0);
2128*2c2f96dcSApple OSS Distributions 		proc_set_wqptr(p, wq);
2129*2c2f96dcSApple OSS Distributions 	}
2130*2c2f96dcSApple OSS Distributions out:
2131*2c2f96dcSApple OSS Distributions 
2132*2c2f96dcSApple OSS Distributions 	return error;
2133*2c2f96dcSApple OSS Distributions }
2134*2c2f96dcSApple OSS Distributions 
2135*2c2f96dcSApple OSS Distributions /*
2136*2c2f96dcSApple OSS Distributions  * Routine:	workq_mark_exiting
2137*2c2f96dcSApple OSS Distributions  *
2138*2c2f96dcSApple OSS Distributions  * Function:	Mark the work queue such that new threads will not be added to the
2139*2c2f96dcSApple OSS Distributions  *		work queue after we return.
2140*2c2f96dcSApple OSS Distributions  *
2141*2c2f96dcSApple OSS Distributions  * Conditions:	Called against the current process.
2142*2c2f96dcSApple OSS Distributions  */
2143*2c2f96dcSApple OSS Distributions void
workq_mark_exiting(struct proc * p)2144*2c2f96dcSApple OSS Distributions workq_mark_exiting(struct proc *p)
2145*2c2f96dcSApple OSS Distributions {
2146*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = proc_get_wqptr(p);
2147*2c2f96dcSApple OSS Distributions 	uint32_t wq_flags;
2148*2c2f96dcSApple OSS Distributions 	workq_threadreq_t mgr_req;
2149*2c2f96dcSApple OSS Distributions 
2150*2c2f96dcSApple OSS Distributions 	if (!wq) {
2151*2c2f96dcSApple OSS Distributions 		return;
2152*2c2f96dcSApple OSS Distributions 	}
2153*2c2f96dcSApple OSS Distributions 
2154*2c2f96dcSApple OSS Distributions 	WQ_TRACE_WQ(TRACE_wq_pthread_exit | DBG_FUNC_START, wq, 0, 0, 0);
2155*2c2f96dcSApple OSS Distributions 
2156*2c2f96dcSApple OSS Distributions 	workq_lock_spin(wq);
2157*2c2f96dcSApple OSS Distributions 
2158*2c2f96dcSApple OSS Distributions 	wq_flags = os_atomic_or_orig(&wq->wq_flags, WQ_EXITING, relaxed);
2159*2c2f96dcSApple OSS Distributions 	if (__improbable(wq_flags & WQ_EXITING)) {
2160*2c2f96dcSApple OSS Distributions 		panic("workq_mark_exiting called twice");
2161*2c2f96dcSApple OSS Distributions 	}
2162*2c2f96dcSApple OSS Distributions 
2163*2c2f96dcSApple OSS Distributions 	/*
2164*2c2f96dcSApple OSS Distributions 	 * Opportunistically try to cancel thread calls that are likely in flight.
2165*2c2f96dcSApple OSS Distributions 	 * workq_exit() will do the proper cleanup.
2166*2c2f96dcSApple OSS Distributions 	 */
2167*2c2f96dcSApple OSS Distributions 	if (wq_flags & WQ_IMMEDIATE_CALL_SCHEDULED) {
2168*2c2f96dcSApple OSS Distributions 		thread_call_cancel(wq->wq_immediate_call);
2169*2c2f96dcSApple OSS Distributions 	}
2170*2c2f96dcSApple OSS Distributions 	if (wq_flags & WQ_DELAYED_CALL_SCHEDULED) {
2171*2c2f96dcSApple OSS Distributions 		thread_call_cancel(wq->wq_delayed_call);
2172*2c2f96dcSApple OSS Distributions 	}
2173*2c2f96dcSApple OSS Distributions 	if (wq_flags & WQ_DEATH_CALL_SCHEDULED) {
2174*2c2f96dcSApple OSS Distributions 		thread_call_cancel(wq->wq_death_call);
2175*2c2f96dcSApple OSS Distributions 	}
2176*2c2f96dcSApple OSS Distributions 
2177*2c2f96dcSApple OSS Distributions 	mgr_req = wq->wq_event_manager_threadreq;
2178*2c2f96dcSApple OSS Distributions 	wq->wq_event_manager_threadreq = NULL;
2179*2c2f96dcSApple OSS Distributions 	wq->wq_reqcount = 0; /* workq_schedule_creator must not look at queues */
2180*2c2f96dcSApple OSS Distributions 	wq->wq_creator = NULL;
2181*2c2f96dcSApple OSS Distributions 	workq_turnstile_update_inheritor(wq, TURNSTILE_INHERITOR_NULL, 0);
2182*2c2f96dcSApple OSS Distributions 
2183*2c2f96dcSApple OSS Distributions 	workq_unlock(wq);
2184*2c2f96dcSApple OSS Distributions 
2185*2c2f96dcSApple OSS Distributions 	if (mgr_req) {
2186*2c2f96dcSApple OSS Distributions 		kqueue_threadreq_cancel(p, mgr_req);
2187*2c2f96dcSApple OSS Distributions 	}
2188*2c2f96dcSApple OSS Distributions 	/*
2189*2c2f96dcSApple OSS Distributions 	 * No one touches the priority queues once WQ_EXITING is set.
2190*2c2f96dcSApple OSS Distributions 	 * It is hence safe to do the tear down without holding any lock.
2191*2c2f96dcSApple OSS Distributions 	 */
2192*2c2f96dcSApple OSS Distributions 	priority_queue_destroy(&wq->wq_overcommit_queue,
2193*2c2f96dcSApple OSS Distributions 	    struct workq_threadreq_s, tr_entry, ^(workq_threadreq_t e){
2194*2c2f96dcSApple OSS Distributions 		workq_threadreq_destroy(p, e);
2195*2c2f96dcSApple OSS Distributions 	});
2196*2c2f96dcSApple OSS Distributions 	priority_queue_destroy(&wq->wq_constrained_queue,
2197*2c2f96dcSApple OSS Distributions 	    struct workq_threadreq_s, tr_entry, ^(workq_threadreq_t e){
2198*2c2f96dcSApple OSS Distributions 		workq_threadreq_destroy(p, e);
2199*2c2f96dcSApple OSS Distributions 	});
2200*2c2f96dcSApple OSS Distributions 	priority_queue_destroy(&wq->wq_special_queue,
2201*2c2f96dcSApple OSS Distributions 	    struct workq_threadreq_s, tr_entry, ^(workq_threadreq_t e){
2202*2c2f96dcSApple OSS Distributions 		workq_threadreq_destroy(p, e);
2203*2c2f96dcSApple OSS Distributions 	});
2204*2c2f96dcSApple OSS Distributions 
2205*2c2f96dcSApple OSS Distributions 	WQ_TRACE(TRACE_wq_pthread_exit | DBG_FUNC_END, 0, 0, 0, 0);
2206*2c2f96dcSApple OSS Distributions }
2207*2c2f96dcSApple OSS Distributions 
2208*2c2f96dcSApple OSS Distributions /*
2209*2c2f96dcSApple OSS Distributions  * Routine:	workq_exit
2210*2c2f96dcSApple OSS Distributions  *
2211*2c2f96dcSApple OSS Distributions  * Function:	clean up the work queue structure(s) now that there are no threads
2212*2c2f96dcSApple OSS Distributions  *		left running inside the work queue (except possibly current_thread).
2213*2c2f96dcSApple OSS Distributions  *
2214*2c2f96dcSApple OSS Distributions  * Conditions:	Called by the last thread in the process.
2215*2c2f96dcSApple OSS Distributions  *		Called against current process.
2216*2c2f96dcSApple OSS Distributions  */
2217*2c2f96dcSApple OSS Distributions void
workq_exit(struct proc * p)2218*2c2f96dcSApple OSS Distributions workq_exit(struct proc *p)
2219*2c2f96dcSApple OSS Distributions {
2220*2c2f96dcSApple OSS Distributions 	struct workqueue *wq;
2221*2c2f96dcSApple OSS Distributions 	struct uthread *uth, *tmp;
2222*2c2f96dcSApple OSS Distributions 
2223*2c2f96dcSApple OSS Distributions 	wq = os_atomic_xchg(&p->p_wqptr, NULL, relaxed);
2224*2c2f96dcSApple OSS Distributions 	if (wq != NULL) {
2225*2c2f96dcSApple OSS Distributions 		thread_t th = current_thread();
2226*2c2f96dcSApple OSS Distributions 
2227*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_workqueue_exit | DBG_FUNC_START, wq, 0, 0, 0);
2228*2c2f96dcSApple OSS Distributions 
2229*2c2f96dcSApple OSS Distributions 		if (thread_get_tag(th) & THREAD_TAG_WORKQUEUE) {
2230*2c2f96dcSApple OSS Distributions 			/*
2231*2c2f96dcSApple OSS Distributions 			 * <rdar://problem/40111515> Make sure we will no longer call the
2232*2c2f96dcSApple OSS Distributions 			 * sched call, if we ever block this thread, which the cancel_wait
2233*2c2f96dcSApple OSS Distributions 			 * below can do.
2234*2c2f96dcSApple OSS Distributions 			 */
2235*2c2f96dcSApple OSS Distributions 			thread_sched_call(th, NULL);
2236*2c2f96dcSApple OSS Distributions 		}
2237*2c2f96dcSApple OSS Distributions 
2238*2c2f96dcSApple OSS Distributions 		/*
2239*2c2f96dcSApple OSS Distributions 		 * Thread calls are always scheduled by the proc itself or under the
2240*2c2f96dcSApple OSS Distributions 		 * workqueue spinlock if WQ_EXITING is not yet set.
2241*2c2f96dcSApple OSS Distributions 		 *
2242*2c2f96dcSApple OSS Distributions 		 * Either way, when this runs, the proc has no threads left beside
2243*2c2f96dcSApple OSS Distributions 		 * the one running this very code, so we know no thread call can be
2244*2c2f96dcSApple OSS Distributions 		 * dispatched anymore.
2245*2c2f96dcSApple OSS Distributions 		 */
2246*2c2f96dcSApple OSS Distributions 		thread_call_cancel_wait(wq->wq_delayed_call);
2247*2c2f96dcSApple OSS Distributions 		thread_call_cancel_wait(wq->wq_immediate_call);
2248*2c2f96dcSApple OSS Distributions 		thread_call_cancel_wait(wq->wq_death_call);
2249*2c2f96dcSApple OSS Distributions 		thread_call_free(wq->wq_delayed_call);
2250*2c2f96dcSApple OSS Distributions 		thread_call_free(wq->wq_immediate_call);
2251*2c2f96dcSApple OSS Distributions 		thread_call_free(wq->wq_death_call);
2252*2c2f96dcSApple OSS Distributions 
2253*2c2f96dcSApple OSS Distributions 		/*
2254*2c2f96dcSApple OSS Distributions 		 * Clean up workqueue data structures for threads that exited and
2255*2c2f96dcSApple OSS Distributions 		 * didn't get a chance to clean up after themselves.
2256*2c2f96dcSApple OSS Distributions 		 *
2257*2c2f96dcSApple OSS Distributions 		 * idle/new threads should have been interrupted and died on their own
2258*2c2f96dcSApple OSS Distributions 		 */
2259*2c2f96dcSApple OSS Distributions 		TAILQ_FOREACH_SAFE(uth, &wq->wq_thrunlist, uu_workq_entry, tmp) {
2260*2c2f96dcSApple OSS Distributions 			thread_t mth = get_machthread(uth);
2261*2c2f96dcSApple OSS Distributions 			thread_sched_call(mth, NULL);
2262*2c2f96dcSApple OSS Distributions 			thread_deallocate(mth);
2263*2c2f96dcSApple OSS Distributions 		}
2264*2c2f96dcSApple OSS Distributions 		assert(TAILQ_EMPTY(&wq->wq_thnewlist));
2265*2c2f96dcSApple OSS Distributions 		assert(TAILQ_EMPTY(&wq->wq_thidlelist));
2266*2c2f96dcSApple OSS Distributions 
2267*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_destroy | DBG_FUNC_END, wq,
2268*2c2f96dcSApple OSS Distributions 		    VM_KERNEL_ADDRHIDE(wq), 0, 0);
2269*2c2f96dcSApple OSS Distributions 
2270*2c2f96dcSApple OSS Distributions 		workq_deallocate(wq);
2271*2c2f96dcSApple OSS Distributions 
2272*2c2f96dcSApple OSS Distributions 		WQ_TRACE(TRACE_wq_workqueue_exit | DBG_FUNC_END, 0, 0, 0, 0);
2273*2c2f96dcSApple OSS Distributions 	}
2274*2c2f96dcSApple OSS Distributions }
2275*2c2f96dcSApple OSS Distributions 
2276*2c2f96dcSApple OSS Distributions 
2277*2c2f96dcSApple OSS Distributions #pragma mark bsd thread control
2278*2c2f96dcSApple OSS Distributions 
2279*2c2f96dcSApple OSS Distributions bool
bsdthread_part_of_cooperative_workqueue(struct uthread * uth)2280*2c2f96dcSApple OSS Distributions bsdthread_part_of_cooperative_workqueue(struct uthread *uth)
2281*2c2f96dcSApple OSS Distributions {
2282*2c2f96dcSApple OSS Distributions 	return (workq_thread_is_cooperative(uth) || workq_thread_is_nonovercommit(uth)) &&
2283*2c2f96dcSApple OSS Distributions 	       (uth->uu_workq_pri.qos_bucket != WORKQ_THREAD_QOS_MANAGER);
2284*2c2f96dcSApple OSS Distributions }
2285*2c2f96dcSApple OSS Distributions 
2286*2c2f96dcSApple OSS Distributions static bool
_pthread_priority_to_policy(pthread_priority_t priority,thread_qos_policy_data_t * data)2287*2c2f96dcSApple OSS Distributions _pthread_priority_to_policy(pthread_priority_t priority,
2288*2c2f96dcSApple OSS Distributions     thread_qos_policy_data_t *data)
2289*2c2f96dcSApple OSS Distributions {
2290*2c2f96dcSApple OSS Distributions 	data->qos_tier = _pthread_priority_thread_qos(priority);
2291*2c2f96dcSApple OSS Distributions 	data->tier_importance = _pthread_priority_relpri(priority);
2292*2c2f96dcSApple OSS Distributions 	if (data->qos_tier == THREAD_QOS_UNSPECIFIED || data->tier_importance > 0 ||
2293*2c2f96dcSApple OSS Distributions 	    data->tier_importance < THREAD_QOS_MIN_TIER_IMPORTANCE) {
2294*2c2f96dcSApple OSS Distributions 		return false;
2295*2c2f96dcSApple OSS Distributions 	}
2296*2c2f96dcSApple OSS Distributions 	return true;
2297*2c2f96dcSApple OSS Distributions }
2298*2c2f96dcSApple OSS Distributions 
2299*2c2f96dcSApple OSS Distributions static int
bsdthread_set_self(proc_t p,thread_t th,pthread_priority_t priority,mach_port_name_t voucher,enum workq_set_self_flags flags)2300*2c2f96dcSApple OSS Distributions bsdthread_set_self(proc_t p, thread_t th, pthread_priority_t priority,
2301*2c2f96dcSApple OSS Distributions     mach_port_name_t voucher, enum workq_set_self_flags flags)
2302*2c2f96dcSApple OSS Distributions {
2303*2c2f96dcSApple OSS Distributions 	struct uthread *uth = get_bsdthread_info(th);
2304*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = proc_get_wqptr(p);
2305*2c2f96dcSApple OSS Distributions 
2306*2c2f96dcSApple OSS Distributions 	kern_return_t kr;
2307*2c2f96dcSApple OSS Distributions 	int unbind_rv = 0, qos_rv = 0, voucher_rv = 0, fixedpri_rv = 0;
2308*2c2f96dcSApple OSS Distributions 	bool is_wq_thread = (thread_get_tag(th) & THREAD_TAG_WORKQUEUE);
2309*2c2f96dcSApple OSS Distributions 
2310*2c2f96dcSApple OSS Distributions 	assert(th == current_thread());
2311*2c2f96dcSApple OSS Distributions 	if (flags & WORKQ_SET_SELF_WQ_KEVENT_UNBIND) {
2312*2c2f96dcSApple OSS Distributions 		if (!is_wq_thread) {
2313*2c2f96dcSApple OSS Distributions 			unbind_rv = EINVAL;
2314*2c2f96dcSApple OSS Distributions 			goto qos;
2315*2c2f96dcSApple OSS Distributions 		}
2316*2c2f96dcSApple OSS Distributions 
2317*2c2f96dcSApple OSS Distributions 		if (uth->uu_workq_pri.qos_bucket == WORKQ_THREAD_QOS_MANAGER) {
2318*2c2f96dcSApple OSS Distributions 			unbind_rv = EINVAL;
2319*2c2f96dcSApple OSS Distributions 			goto qos;
2320*2c2f96dcSApple OSS Distributions 		}
2321*2c2f96dcSApple OSS Distributions 
2322*2c2f96dcSApple OSS Distributions 		workq_threadreq_t kqr = uth->uu_kqr_bound;
2323*2c2f96dcSApple OSS Distributions 		if (kqr == NULL) {
2324*2c2f96dcSApple OSS Distributions 			unbind_rv = EALREADY;
2325*2c2f96dcSApple OSS Distributions 			goto qos;
2326*2c2f96dcSApple OSS Distributions 		}
2327*2c2f96dcSApple OSS Distributions 
2328*2c2f96dcSApple OSS Distributions 		if (kqr->tr_flags & WORKQ_TR_FLAG_WORKLOOP) {
2329*2c2f96dcSApple OSS Distributions 			unbind_rv = EINVAL;
2330*2c2f96dcSApple OSS Distributions 			goto qos;
2331*2c2f96dcSApple OSS Distributions 		}
2332*2c2f96dcSApple OSS Distributions 
2333*2c2f96dcSApple OSS Distributions 		kqueue_threadreq_unbind(p, kqr);
2334*2c2f96dcSApple OSS Distributions 	}
2335*2c2f96dcSApple OSS Distributions 
2336*2c2f96dcSApple OSS Distributions qos:
2337*2c2f96dcSApple OSS Distributions 	if (flags & (WORKQ_SET_SELF_QOS_FLAG | WORKQ_SET_SELF_QOS_OVERRIDE_FLAG)) {
2338*2c2f96dcSApple OSS Distributions 		assert(flags & WORKQ_SET_SELF_QOS_FLAG);
2339*2c2f96dcSApple OSS Distributions 
2340*2c2f96dcSApple OSS Distributions 		thread_qos_policy_data_t new_policy;
2341*2c2f96dcSApple OSS Distributions 		thread_qos_t qos_override = THREAD_QOS_UNSPECIFIED;
2342*2c2f96dcSApple OSS Distributions 
2343*2c2f96dcSApple OSS Distributions 		if (!_pthread_priority_to_policy(priority, &new_policy)) {
2344*2c2f96dcSApple OSS Distributions 			qos_rv = EINVAL;
2345*2c2f96dcSApple OSS Distributions 			goto voucher;
2346*2c2f96dcSApple OSS Distributions 		}
2347*2c2f96dcSApple OSS Distributions 
2348*2c2f96dcSApple OSS Distributions 		if (flags & WORKQ_SET_SELF_QOS_OVERRIDE_FLAG) {
2349*2c2f96dcSApple OSS Distributions 			/*
2350*2c2f96dcSApple OSS Distributions 			 * If the WORKQ_SET_SELF_QOS_OVERRIDE_FLAG is set, we definitely
2351*2c2f96dcSApple OSS Distributions 			 * should have an override QoS in the pthread_priority_t and we should
2352*2c2f96dcSApple OSS Distributions 			 * only come into this path for cooperative thread requests
2353*2c2f96dcSApple OSS Distributions 			 */
2354*2c2f96dcSApple OSS Distributions 			if (!_pthread_priority_has_override_qos(priority) ||
2355*2c2f96dcSApple OSS Distributions 			    !_pthread_priority_is_cooperative(priority)) {
2356*2c2f96dcSApple OSS Distributions 				qos_rv = EINVAL;
2357*2c2f96dcSApple OSS Distributions 				goto voucher;
2358*2c2f96dcSApple OSS Distributions 			}
2359*2c2f96dcSApple OSS Distributions 			qos_override = _pthread_priority_thread_override_qos(priority);
2360*2c2f96dcSApple OSS Distributions 		} else {
2361*2c2f96dcSApple OSS Distributions 			/*
2362*2c2f96dcSApple OSS Distributions 			 * If the WORKQ_SET_SELF_QOS_OVERRIDE_FLAG is not set, we definitely
2363*2c2f96dcSApple OSS Distributions 			 * should not have an override QoS in the pthread_priority_t
2364*2c2f96dcSApple OSS Distributions 			 */
2365*2c2f96dcSApple OSS Distributions 			if (_pthread_priority_has_override_qos(priority)) {
2366*2c2f96dcSApple OSS Distributions 				qos_rv = EINVAL;
2367*2c2f96dcSApple OSS Distributions 				goto voucher;
2368*2c2f96dcSApple OSS Distributions 			}
2369*2c2f96dcSApple OSS Distributions 		}
2370*2c2f96dcSApple OSS Distributions 
2371*2c2f96dcSApple OSS Distributions 		if (!is_wq_thread) {
2372*2c2f96dcSApple OSS Distributions 			/*
2373*2c2f96dcSApple OSS Distributions 			 * Threads opted out of QoS can't change QoS
2374*2c2f96dcSApple OSS Distributions 			 */
2375*2c2f96dcSApple OSS Distributions 			if (!thread_has_qos_policy(th)) {
2376*2c2f96dcSApple OSS Distributions 				qos_rv = EPERM;
2377*2c2f96dcSApple OSS Distributions 				goto voucher;
2378*2c2f96dcSApple OSS Distributions 			}
2379*2c2f96dcSApple OSS Distributions 		} else if (uth->uu_workq_pri.qos_bucket == WORKQ_THREAD_QOS_MANAGER ||
2380*2c2f96dcSApple OSS Distributions 		    uth->uu_workq_pri.qos_bucket == WORKQ_THREAD_QOS_ABOVEUI) {
2381*2c2f96dcSApple OSS Distributions 			/*
2382*2c2f96dcSApple OSS Distributions 			 * Workqueue manager threads or threads above UI can't change QoS
2383*2c2f96dcSApple OSS Distributions 			 */
2384*2c2f96dcSApple OSS Distributions 			qos_rv = EINVAL;
2385*2c2f96dcSApple OSS Distributions 			goto voucher;
2386*2c2f96dcSApple OSS Distributions 		} else {
2387*2c2f96dcSApple OSS Distributions 			/*
2388*2c2f96dcSApple OSS Distributions 			 * For workqueue threads, possibly adjust buckets and redrive thread
2389*2c2f96dcSApple OSS Distributions 			 * requests.
2390*2c2f96dcSApple OSS Distributions 			 *
2391*2c2f96dcSApple OSS Distributions 			 * Transitions allowed:
2392*2c2f96dcSApple OSS Distributions 			 *
2393*2c2f96dcSApple OSS Distributions 			 * overcommit --> non-overcommit
2394*2c2f96dcSApple OSS Distributions 			 * overcommit --> overcommit
2395*2c2f96dcSApple OSS Distributions 			 * non-overcommit --> non-overcommit
2396*2c2f96dcSApple OSS Distributions 			 * non-overcommit --> overcommit (to be deprecated later)
2397*2c2f96dcSApple OSS Distributions 			 * cooperative --> cooperative
2398*2c2f96dcSApple OSS Distributions 			 *
2399*2c2f96dcSApple OSS Distributions 			 * All other transitions aren't allowed so reject them.
2400*2c2f96dcSApple OSS Distributions 			 */
2401*2c2f96dcSApple OSS Distributions 			if (workq_thread_is_overcommit(uth) && _pthread_priority_is_cooperative(priority)) {
2402*2c2f96dcSApple OSS Distributions 				qos_rv = EINVAL;
2403*2c2f96dcSApple OSS Distributions 				goto voucher;
2404*2c2f96dcSApple OSS Distributions 			} else if (workq_thread_is_cooperative(uth) && !_pthread_priority_is_cooperative(priority)) {
2405*2c2f96dcSApple OSS Distributions 				qos_rv = EINVAL;
2406*2c2f96dcSApple OSS Distributions 				goto voucher;
2407*2c2f96dcSApple OSS Distributions 			} else if (workq_thread_is_nonovercommit(uth) && _pthread_priority_is_cooperative(priority)) {
2408*2c2f96dcSApple OSS Distributions 				qos_rv = EINVAL;
2409*2c2f96dcSApple OSS Distributions 				goto voucher;
2410*2c2f96dcSApple OSS Distributions 			}
2411*2c2f96dcSApple OSS Distributions 
2412*2c2f96dcSApple OSS Distributions 			struct uu_workq_policy old_pri, new_pri;
2413*2c2f96dcSApple OSS Distributions 			bool force_run = false;
2414*2c2f96dcSApple OSS Distributions 
2415*2c2f96dcSApple OSS Distributions 			if (qos_override) {
2416*2c2f96dcSApple OSS Distributions 				/*
2417*2c2f96dcSApple OSS Distributions 				 * We're in the case of a thread clarifying that it is for eg. not IN
2418*2c2f96dcSApple OSS Distributions 				 * req QoS but rather, UT req QoS with IN override. However, this can
2419*2c2f96dcSApple OSS Distributions 				 * race with a concurrent override happening to the thread via
2420*2c2f96dcSApple OSS Distributions 				 * workq_thread_add_dispatch_override so this needs to be
2421*2c2f96dcSApple OSS Distributions 				 * synchronized with the thread mutex.
2422*2c2f96dcSApple OSS Distributions 				 */
2423*2c2f96dcSApple OSS Distributions 				thread_mtx_lock(th);
2424*2c2f96dcSApple OSS Distributions 			}
2425*2c2f96dcSApple OSS Distributions 
2426*2c2f96dcSApple OSS Distributions 			workq_lock_spin(wq);
2427*2c2f96dcSApple OSS Distributions 
2428*2c2f96dcSApple OSS Distributions 			old_pri = new_pri = uth->uu_workq_pri;
2429*2c2f96dcSApple OSS Distributions 			new_pri.qos_req = (thread_qos_t)new_policy.qos_tier;
2430*2c2f96dcSApple OSS Distributions 
2431*2c2f96dcSApple OSS Distributions 			if (old_pri.qos_override < qos_override) {
2432*2c2f96dcSApple OSS Distributions 				/*
2433*2c2f96dcSApple OSS Distributions 				 * Since this can race with a concurrent override via
2434*2c2f96dcSApple OSS Distributions 				 * workq_thread_add_dispatch_override, only adjust override value if we
2435*2c2f96dcSApple OSS Distributions 				 * are higher - this is a saturating function.
2436*2c2f96dcSApple OSS Distributions 				 *
2437*2c2f96dcSApple OSS Distributions 				 * We should not be changing the final override values, we should simply
2438*2c2f96dcSApple OSS Distributions 				 * be redistributing the current value with a different breakdown of req
2439*2c2f96dcSApple OSS Distributions 				 * vs override QoS - assert to that effect. Therefore, buckets should
2440*2c2f96dcSApple OSS Distributions 				 * not change.
2441*2c2f96dcSApple OSS Distributions 				 */
2442*2c2f96dcSApple OSS Distributions 				new_pri.qos_override = qos_override;
2443*2c2f96dcSApple OSS Distributions 				assert(workq_pri_override(new_pri) == workq_pri_override(old_pri));
2444*2c2f96dcSApple OSS Distributions 				assert(workq_pri_bucket(new_pri) == workq_pri_bucket(old_pri));
2445*2c2f96dcSApple OSS Distributions 			}
2446*2c2f96dcSApple OSS Distributions 
2447*2c2f96dcSApple OSS Distributions 			/* Adjust schedule counts for various types of transitions */
2448*2c2f96dcSApple OSS Distributions 
2449*2c2f96dcSApple OSS Distributions 			/* overcommit -> non-overcommit */
2450*2c2f96dcSApple OSS Distributions 			if (workq_thread_is_overcommit(uth) && _pthread_priority_is_nonovercommit(priority)) {
2451*2c2f96dcSApple OSS Distributions 				workq_thread_set_type(uth, 0);
2452*2c2f96dcSApple OSS Distributions 				wq->wq_constrained_threads_scheduled++;
2453*2c2f96dcSApple OSS Distributions 
2454*2c2f96dcSApple OSS Distributions 				/* non-overcommit -> overcommit */
2455*2c2f96dcSApple OSS Distributions 			} else if (workq_thread_is_nonovercommit(uth) && _pthread_priority_is_overcommit(priority)) {
2456*2c2f96dcSApple OSS Distributions 				workq_thread_set_type(uth, UT_WORKQ_OVERCOMMIT);
2457*2c2f96dcSApple OSS Distributions 				force_run = (wq->wq_constrained_threads_scheduled-- == wq_max_constrained_threads);
2458*2c2f96dcSApple OSS Distributions 
2459*2c2f96dcSApple OSS Distributions 				/* cooperative -> cooperative */
2460*2c2f96dcSApple OSS Distributions 			} else if (workq_thread_is_cooperative(uth)) {
2461*2c2f96dcSApple OSS Distributions 				_wq_cooperative_queue_scheduled_count_dec(wq, old_pri.qos_req);
2462*2c2f96dcSApple OSS Distributions 				_wq_cooperative_queue_scheduled_count_inc(wq, new_pri.qos_req);
2463*2c2f96dcSApple OSS Distributions 
2464*2c2f96dcSApple OSS Distributions 				/* We're changing schedule counts within cooperative pool, we
2465*2c2f96dcSApple OSS Distributions 				 * need to refresh best cooperative QoS logic again */
2466*2c2f96dcSApple OSS Distributions 				force_run = _wq_cooperative_queue_refresh_best_req_qos(wq);
2467*2c2f96dcSApple OSS Distributions 			}
2468*2c2f96dcSApple OSS Distributions 
2469*2c2f96dcSApple OSS Distributions 			/*
2470*2c2f96dcSApple OSS Distributions 			 * This will set up an override on the thread if any and will also call
2471*2c2f96dcSApple OSS Distributions 			 * schedule_creator if needed
2472*2c2f96dcSApple OSS Distributions 			 */
2473*2c2f96dcSApple OSS Distributions 			workq_thread_update_bucket(p, wq, uth, old_pri, new_pri, force_run);
2474*2c2f96dcSApple OSS Distributions 			workq_unlock(wq);
2475*2c2f96dcSApple OSS Distributions 
2476*2c2f96dcSApple OSS Distributions 			if (qos_override) {
2477*2c2f96dcSApple OSS Distributions 				thread_mtx_unlock(th);
2478*2c2f96dcSApple OSS Distributions 			}
2479*2c2f96dcSApple OSS Distributions 
2480*2c2f96dcSApple OSS Distributions 			if (workq_thread_is_overcommit(uth)) {
2481*2c2f96dcSApple OSS Distributions 				thread_disarm_workqueue_quantum(th);
2482*2c2f96dcSApple OSS Distributions 			} else {
2483*2c2f96dcSApple OSS Distributions 				/* If the thread changed QoS buckets, the quantum duration
2484*2c2f96dcSApple OSS Distributions 				 * may have changed too */
2485*2c2f96dcSApple OSS Distributions 				thread_arm_workqueue_quantum(th);
2486*2c2f96dcSApple OSS Distributions 			}
2487*2c2f96dcSApple OSS Distributions 		}
2488*2c2f96dcSApple OSS Distributions 
2489*2c2f96dcSApple OSS Distributions 		kr = thread_policy_set_internal(th, THREAD_QOS_POLICY,
2490*2c2f96dcSApple OSS Distributions 		    (thread_policy_t)&new_policy, THREAD_QOS_POLICY_COUNT);
2491*2c2f96dcSApple OSS Distributions 		if (kr != KERN_SUCCESS) {
2492*2c2f96dcSApple OSS Distributions 			qos_rv = EINVAL;
2493*2c2f96dcSApple OSS Distributions 		}
2494*2c2f96dcSApple OSS Distributions 	}
2495*2c2f96dcSApple OSS Distributions 
2496*2c2f96dcSApple OSS Distributions voucher:
2497*2c2f96dcSApple OSS Distributions 	if (flags & WORKQ_SET_SELF_VOUCHER_FLAG) {
2498*2c2f96dcSApple OSS Distributions 		kr = thread_set_voucher_name(voucher);
2499*2c2f96dcSApple OSS Distributions 		if (kr != KERN_SUCCESS) {
2500*2c2f96dcSApple OSS Distributions 			voucher_rv = ENOENT;
2501*2c2f96dcSApple OSS Distributions 			goto fixedpri;
2502*2c2f96dcSApple OSS Distributions 		}
2503*2c2f96dcSApple OSS Distributions 	}
2504*2c2f96dcSApple OSS Distributions 
2505*2c2f96dcSApple OSS Distributions fixedpri:
2506*2c2f96dcSApple OSS Distributions 	if (qos_rv) {
2507*2c2f96dcSApple OSS Distributions 		goto done;
2508*2c2f96dcSApple OSS Distributions 	}
2509*2c2f96dcSApple OSS Distributions 	if (flags & WORKQ_SET_SELF_FIXEDPRIORITY_FLAG) {
2510*2c2f96dcSApple OSS Distributions 		thread_extended_policy_data_t extpol = {.timeshare = 0};
2511*2c2f96dcSApple OSS Distributions 
2512*2c2f96dcSApple OSS Distributions 		if (is_wq_thread) {
2513*2c2f96dcSApple OSS Distributions 			/* Not allowed on workqueue threads */
2514*2c2f96dcSApple OSS Distributions 			fixedpri_rv = ENOTSUP;
2515*2c2f96dcSApple OSS Distributions 			goto done;
2516*2c2f96dcSApple OSS Distributions 		}
2517*2c2f96dcSApple OSS Distributions 
2518*2c2f96dcSApple OSS Distributions 		kr = thread_policy_set_internal(th, THREAD_EXTENDED_POLICY,
2519*2c2f96dcSApple OSS Distributions 		    (thread_policy_t)&extpol, THREAD_EXTENDED_POLICY_COUNT);
2520*2c2f96dcSApple OSS Distributions 		if (kr != KERN_SUCCESS) {
2521*2c2f96dcSApple OSS Distributions 			fixedpri_rv = EINVAL;
2522*2c2f96dcSApple OSS Distributions 			goto done;
2523*2c2f96dcSApple OSS Distributions 		}
2524*2c2f96dcSApple OSS Distributions 	} else if (flags & WORKQ_SET_SELF_TIMESHARE_FLAG) {
2525*2c2f96dcSApple OSS Distributions 		thread_extended_policy_data_t extpol = {.timeshare = 1};
2526*2c2f96dcSApple OSS Distributions 
2527*2c2f96dcSApple OSS Distributions 		if (is_wq_thread) {
2528*2c2f96dcSApple OSS Distributions 			/* Not allowed on workqueue threads */
2529*2c2f96dcSApple OSS Distributions 			fixedpri_rv = ENOTSUP;
2530*2c2f96dcSApple OSS Distributions 			goto done;
2531*2c2f96dcSApple OSS Distributions 		}
2532*2c2f96dcSApple OSS Distributions 
2533*2c2f96dcSApple OSS Distributions 		kr = thread_policy_set_internal(th, THREAD_EXTENDED_POLICY,
2534*2c2f96dcSApple OSS Distributions 		    (thread_policy_t)&extpol, THREAD_EXTENDED_POLICY_COUNT);
2535*2c2f96dcSApple OSS Distributions 		if (kr != KERN_SUCCESS) {
2536*2c2f96dcSApple OSS Distributions 			fixedpri_rv = EINVAL;
2537*2c2f96dcSApple OSS Distributions 			goto done;
2538*2c2f96dcSApple OSS Distributions 		}
2539*2c2f96dcSApple OSS Distributions 	}
2540*2c2f96dcSApple OSS Distributions 
2541*2c2f96dcSApple OSS Distributions done:
2542*2c2f96dcSApple OSS Distributions 	if (qos_rv && voucher_rv) {
2543*2c2f96dcSApple OSS Distributions 		/* Both failed, give that a unique error. */
2544*2c2f96dcSApple OSS Distributions 		return EBADMSG;
2545*2c2f96dcSApple OSS Distributions 	}
2546*2c2f96dcSApple OSS Distributions 
2547*2c2f96dcSApple OSS Distributions 	if (unbind_rv) {
2548*2c2f96dcSApple OSS Distributions 		return unbind_rv;
2549*2c2f96dcSApple OSS Distributions 	}
2550*2c2f96dcSApple OSS Distributions 
2551*2c2f96dcSApple OSS Distributions 	if (qos_rv) {
2552*2c2f96dcSApple OSS Distributions 		return qos_rv;
2553*2c2f96dcSApple OSS Distributions 	}
2554*2c2f96dcSApple OSS Distributions 
2555*2c2f96dcSApple OSS Distributions 	if (voucher_rv) {
2556*2c2f96dcSApple OSS Distributions 		return voucher_rv;
2557*2c2f96dcSApple OSS Distributions 	}
2558*2c2f96dcSApple OSS Distributions 
2559*2c2f96dcSApple OSS Distributions 	if (fixedpri_rv) {
2560*2c2f96dcSApple OSS Distributions 		return fixedpri_rv;
2561*2c2f96dcSApple OSS Distributions 	}
2562*2c2f96dcSApple OSS Distributions 
2563*2c2f96dcSApple OSS Distributions 
2564*2c2f96dcSApple OSS Distributions 	return 0;
2565*2c2f96dcSApple OSS Distributions }
2566*2c2f96dcSApple OSS Distributions 
2567*2c2f96dcSApple OSS Distributions static int
bsdthread_add_explicit_override(proc_t p,mach_port_name_t kport,pthread_priority_t pp,user_addr_t resource)2568*2c2f96dcSApple OSS Distributions bsdthread_add_explicit_override(proc_t p, mach_port_name_t kport,
2569*2c2f96dcSApple OSS Distributions     pthread_priority_t pp, user_addr_t resource)
2570*2c2f96dcSApple OSS Distributions {
2571*2c2f96dcSApple OSS Distributions 	thread_qos_t qos = _pthread_priority_thread_qos(pp);
2572*2c2f96dcSApple OSS Distributions 	if (qos == THREAD_QOS_UNSPECIFIED) {
2573*2c2f96dcSApple OSS Distributions 		return EINVAL;
2574*2c2f96dcSApple OSS Distributions 	}
2575*2c2f96dcSApple OSS Distributions 
2576*2c2f96dcSApple OSS Distributions 	thread_t th = port_name_to_thread(kport,
2577*2c2f96dcSApple OSS Distributions 	    PORT_INTRANS_THREAD_IN_CURRENT_TASK);
2578*2c2f96dcSApple OSS Distributions 	if (th == THREAD_NULL) {
2579*2c2f96dcSApple OSS Distributions 		return ESRCH;
2580*2c2f96dcSApple OSS Distributions 	}
2581*2c2f96dcSApple OSS Distributions 
2582*2c2f96dcSApple OSS Distributions 	int rv = proc_thread_qos_add_override(proc_task(p), th, 0, qos, TRUE,
2583*2c2f96dcSApple OSS Distributions 	    resource, THREAD_QOS_OVERRIDE_TYPE_PTHREAD_EXPLICIT_OVERRIDE);
2584*2c2f96dcSApple OSS Distributions 
2585*2c2f96dcSApple OSS Distributions 	thread_deallocate(th);
2586*2c2f96dcSApple OSS Distributions 	return rv;
2587*2c2f96dcSApple OSS Distributions }
2588*2c2f96dcSApple OSS Distributions 
2589*2c2f96dcSApple OSS Distributions static int
bsdthread_remove_explicit_override(proc_t p,mach_port_name_t kport,user_addr_t resource)2590*2c2f96dcSApple OSS Distributions bsdthread_remove_explicit_override(proc_t p, mach_port_name_t kport,
2591*2c2f96dcSApple OSS Distributions     user_addr_t resource)
2592*2c2f96dcSApple OSS Distributions {
2593*2c2f96dcSApple OSS Distributions 	thread_t th = port_name_to_thread(kport,
2594*2c2f96dcSApple OSS Distributions 	    PORT_INTRANS_THREAD_IN_CURRENT_TASK);
2595*2c2f96dcSApple OSS Distributions 	if (th == THREAD_NULL) {
2596*2c2f96dcSApple OSS Distributions 		return ESRCH;
2597*2c2f96dcSApple OSS Distributions 	}
2598*2c2f96dcSApple OSS Distributions 
2599*2c2f96dcSApple OSS Distributions 	int rv = proc_thread_qos_remove_override(proc_task(p), th, 0, resource,
2600*2c2f96dcSApple OSS Distributions 	    THREAD_QOS_OVERRIDE_TYPE_PTHREAD_EXPLICIT_OVERRIDE);
2601*2c2f96dcSApple OSS Distributions 
2602*2c2f96dcSApple OSS Distributions 	thread_deallocate(th);
2603*2c2f96dcSApple OSS Distributions 	return rv;
2604*2c2f96dcSApple OSS Distributions }
2605*2c2f96dcSApple OSS Distributions 
2606*2c2f96dcSApple OSS Distributions static int
workq_thread_add_dispatch_override(proc_t p,mach_port_name_t kport,pthread_priority_t pp,user_addr_t ulock_addr)2607*2c2f96dcSApple OSS Distributions workq_thread_add_dispatch_override(proc_t p, mach_port_name_t kport,
2608*2c2f96dcSApple OSS Distributions     pthread_priority_t pp, user_addr_t ulock_addr)
2609*2c2f96dcSApple OSS Distributions {
2610*2c2f96dcSApple OSS Distributions 	struct uu_workq_policy old_pri, new_pri;
2611*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = proc_get_wqptr(p);
2612*2c2f96dcSApple OSS Distributions 
2613*2c2f96dcSApple OSS Distributions 	thread_qos_t qos_override = _pthread_priority_thread_qos(pp);
2614*2c2f96dcSApple OSS Distributions 	if (qos_override == THREAD_QOS_UNSPECIFIED) {
2615*2c2f96dcSApple OSS Distributions 		return EINVAL;
2616*2c2f96dcSApple OSS Distributions 	}
2617*2c2f96dcSApple OSS Distributions 
2618*2c2f96dcSApple OSS Distributions 	thread_t thread = port_name_to_thread(kport,
2619*2c2f96dcSApple OSS Distributions 	    PORT_INTRANS_THREAD_IN_CURRENT_TASK);
2620*2c2f96dcSApple OSS Distributions 	if (thread == THREAD_NULL) {
2621*2c2f96dcSApple OSS Distributions 		return ESRCH;
2622*2c2f96dcSApple OSS Distributions 	}
2623*2c2f96dcSApple OSS Distributions 
2624*2c2f96dcSApple OSS Distributions 	struct uthread *uth = get_bsdthread_info(thread);
2625*2c2f96dcSApple OSS Distributions 	if ((thread_get_tag(thread) & THREAD_TAG_WORKQUEUE) == 0) {
2626*2c2f96dcSApple OSS Distributions 		thread_deallocate(thread);
2627*2c2f96dcSApple OSS Distributions 		return EPERM;
2628*2c2f96dcSApple OSS Distributions 	}
2629*2c2f96dcSApple OSS Distributions 
2630*2c2f96dcSApple OSS Distributions 	WQ_TRACE_WQ(TRACE_wq_override_dispatch | DBG_FUNC_NONE,
2631*2c2f96dcSApple OSS Distributions 	    wq, thread_tid(thread), 1, pp);
2632*2c2f96dcSApple OSS Distributions 
2633*2c2f96dcSApple OSS Distributions 	thread_mtx_lock(thread);
2634*2c2f96dcSApple OSS Distributions 
2635*2c2f96dcSApple OSS Distributions 	if (ulock_addr) {
2636*2c2f96dcSApple OSS Distributions 		uint32_t val;
2637*2c2f96dcSApple OSS Distributions 		int rc;
2638*2c2f96dcSApple OSS Distributions 		/*
2639*2c2f96dcSApple OSS Distributions 		 * Workaround lack of explicit support for 'no-fault copyin'
2640*2c2f96dcSApple OSS Distributions 		 * <rdar://problem/24999882>, as disabling preemption prevents paging in
2641*2c2f96dcSApple OSS Distributions 		 */
2642*2c2f96dcSApple OSS Distributions 		disable_preemption();
2643*2c2f96dcSApple OSS Distributions 		rc = copyin_atomic32(ulock_addr, &val);
2644*2c2f96dcSApple OSS Distributions 		enable_preemption();
2645*2c2f96dcSApple OSS Distributions 		if (rc == 0 && ulock_owner_value_to_port_name(val) != kport) {
2646*2c2f96dcSApple OSS Distributions 			goto out;
2647*2c2f96dcSApple OSS Distributions 		}
2648*2c2f96dcSApple OSS Distributions 	}
2649*2c2f96dcSApple OSS Distributions 
2650*2c2f96dcSApple OSS Distributions 	workq_lock_spin(wq);
2651*2c2f96dcSApple OSS Distributions 
2652*2c2f96dcSApple OSS Distributions 	old_pri = uth->uu_workq_pri;
2653*2c2f96dcSApple OSS Distributions 	if (old_pri.qos_override >= qos_override) {
2654*2c2f96dcSApple OSS Distributions 		/* Nothing to do */
2655*2c2f96dcSApple OSS Distributions 	} else if (thread == current_thread()) {
2656*2c2f96dcSApple OSS Distributions 		new_pri = old_pri;
2657*2c2f96dcSApple OSS Distributions 		new_pri.qos_override = qos_override;
2658*2c2f96dcSApple OSS Distributions 		workq_thread_update_bucket(p, wq, uth, old_pri, new_pri, false);
2659*2c2f96dcSApple OSS Distributions 	} else {
2660*2c2f96dcSApple OSS Distributions 		uth->uu_workq_pri.qos_override = qos_override;
2661*2c2f96dcSApple OSS Distributions 		if (qos_override > workq_pri_override(old_pri)) {
2662*2c2f96dcSApple OSS Distributions 			thread_set_workq_override(thread, qos_override);
2663*2c2f96dcSApple OSS Distributions 		}
2664*2c2f96dcSApple OSS Distributions 	}
2665*2c2f96dcSApple OSS Distributions 
2666*2c2f96dcSApple OSS Distributions 	workq_unlock(wq);
2667*2c2f96dcSApple OSS Distributions 
2668*2c2f96dcSApple OSS Distributions out:
2669*2c2f96dcSApple OSS Distributions 	thread_mtx_unlock(thread);
2670*2c2f96dcSApple OSS Distributions 	thread_deallocate(thread);
2671*2c2f96dcSApple OSS Distributions 	return 0;
2672*2c2f96dcSApple OSS Distributions }
2673*2c2f96dcSApple OSS Distributions 
2674*2c2f96dcSApple OSS Distributions static int
workq_thread_reset_dispatch_override(proc_t p,thread_t thread)2675*2c2f96dcSApple OSS Distributions workq_thread_reset_dispatch_override(proc_t p, thread_t thread)
2676*2c2f96dcSApple OSS Distributions {
2677*2c2f96dcSApple OSS Distributions 	struct uu_workq_policy old_pri, new_pri;
2678*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = proc_get_wqptr(p);
2679*2c2f96dcSApple OSS Distributions 	struct uthread *uth = get_bsdthread_info(thread);
2680*2c2f96dcSApple OSS Distributions 
2681*2c2f96dcSApple OSS Distributions 	if ((thread_get_tag(thread) & THREAD_TAG_WORKQUEUE) == 0) {
2682*2c2f96dcSApple OSS Distributions 		return EPERM;
2683*2c2f96dcSApple OSS Distributions 	}
2684*2c2f96dcSApple OSS Distributions 
2685*2c2f96dcSApple OSS Distributions 	WQ_TRACE_WQ(TRACE_wq_override_reset | DBG_FUNC_NONE, wq, 0, 0, 0);
2686*2c2f96dcSApple OSS Distributions 
2687*2c2f96dcSApple OSS Distributions 	/*
2688*2c2f96dcSApple OSS Distributions 	 * workq_thread_add_dispatch_override takes the thread mutex before doing the
2689*2c2f96dcSApple OSS Distributions 	 * copyin to validate the drainer and apply the override. We need to do the
2690*2c2f96dcSApple OSS Distributions 	 * same here. See rdar://84472518
2691*2c2f96dcSApple OSS Distributions 	 */
2692*2c2f96dcSApple OSS Distributions 	thread_mtx_lock(thread);
2693*2c2f96dcSApple OSS Distributions 
2694*2c2f96dcSApple OSS Distributions 	workq_lock_spin(wq);
2695*2c2f96dcSApple OSS Distributions 	old_pri = new_pri = uth->uu_workq_pri;
2696*2c2f96dcSApple OSS Distributions 	new_pri.qos_override = THREAD_QOS_UNSPECIFIED;
2697*2c2f96dcSApple OSS Distributions 	workq_thread_update_bucket(p, wq, uth, old_pri, new_pri, false);
2698*2c2f96dcSApple OSS Distributions 	workq_unlock(wq);
2699*2c2f96dcSApple OSS Distributions 
2700*2c2f96dcSApple OSS Distributions 	thread_mtx_unlock(thread);
2701*2c2f96dcSApple OSS Distributions 	return 0;
2702*2c2f96dcSApple OSS Distributions }
2703*2c2f96dcSApple OSS Distributions 
2704*2c2f96dcSApple OSS Distributions static int
workq_thread_allow_kill(__unused proc_t p,thread_t thread,bool enable)2705*2c2f96dcSApple OSS Distributions workq_thread_allow_kill(__unused proc_t p, thread_t thread, bool enable)
2706*2c2f96dcSApple OSS Distributions {
2707*2c2f96dcSApple OSS Distributions 	if (!(thread_get_tag(thread) & THREAD_TAG_WORKQUEUE)) {
2708*2c2f96dcSApple OSS Distributions 		// If the thread isn't a workqueue thread, don't set the
2709*2c2f96dcSApple OSS Distributions 		// kill_allowed bit; however, we still need to return 0
2710*2c2f96dcSApple OSS Distributions 		// instead of an error code since this code is executed
2711*2c2f96dcSApple OSS Distributions 		// on the abort path which needs to not depend on the
2712*2c2f96dcSApple OSS Distributions 		// pthread_t (returning an error depends on pthread_t via
2713*2c2f96dcSApple OSS Distributions 		// cerror_nocancel)
2714*2c2f96dcSApple OSS Distributions 		return 0;
2715*2c2f96dcSApple OSS Distributions 	}
2716*2c2f96dcSApple OSS Distributions 	struct uthread *uth = get_bsdthread_info(thread);
2717*2c2f96dcSApple OSS Distributions 	uth->uu_workq_pthread_kill_allowed = enable;
2718*2c2f96dcSApple OSS Distributions 	return 0;
2719*2c2f96dcSApple OSS Distributions }
2720*2c2f96dcSApple OSS Distributions 
2721*2c2f96dcSApple OSS Distributions static int
workq_allow_sigmask(proc_t p,sigset_t mask)2722*2c2f96dcSApple OSS Distributions workq_allow_sigmask(proc_t p, sigset_t mask)
2723*2c2f96dcSApple OSS Distributions {
2724*2c2f96dcSApple OSS Distributions 	if (mask & workq_threadmask) {
2725*2c2f96dcSApple OSS Distributions 		return EINVAL;
2726*2c2f96dcSApple OSS Distributions 	}
2727*2c2f96dcSApple OSS Distributions 
2728*2c2f96dcSApple OSS Distributions 	proc_lock(p);
2729*2c2f96dcSApple OSS Distributions 	p->p_workq_allow_sigmask |= mask;
2730*2c2f96dcSApple OSS Distributions 	proc_unlock(p);
2731*2c2f96dcSApple OSS Distributions 
2732*2c2f96dcSApple OSS Distributions 	return 0;
2733*2c2f96dcSApple OSS Distributions }
2734*2c2f96dcSApple OSS Distributions 
2735*2c2f96dcSApple OSS Distributions static int
bsdthread_get_max_parallelism(thread_qos_t qos,unsigned long flags,int * retval)2736*2c2f96dcSApple OSS Distributions bsdthread_get_max_parallelism(thread_qos_t qos, unsigned long flags,
2737*2c2f96dcSApple OSS Distributions     int *retval)
2738*2c2f96dcSApple OSS Distributions {
2739*2c2f96dcSApple OSS Distributions 	static_assert(QOS_PARALLELISM_COUNT_LOGICAL ==
2740*2c2f96dcSApple OSS Distributions 	    _PTHREAD_QOS_PARALLELISM_COUNT_LOGICAL, "logical");
2741*2c2f96dcSApple OSS Distributions 	static_assert(QOS_PARALLELISM_REALTIME ==
2742*2c2f96dcSApple OSS Distributions 	    _PTHREAD_QOS_PARALLELISM_REALTIME, "realtime");
2743*2c2f96dcSApple OSS Distributions 	static_assert(QOS_PARALLELISM_CLUSTER_SHARED_RESOURCE ==
2744*2c2f96dcSApple OSS Distributions 	    _PTHREAD_QOS_PARALLELISM_CLUSTER_SHARED_RSRC, "cluster shared resource");
2745*2c2f96dcSApple OSS Distributions 
2746*2c2f96dcSApple OSS Distributions 	if (flags & ~(QOS_PARALLELISM_REALTIME | QOS_PARALLELISM_COUNT_LOGICAL | QOS_PARALLELISM_CLUSTER_SHARED_RESOURCE)) {
2747*2c2f96dcSApple OSS Distributions 		return EINVAL;
2748*2c2f96dcSApple OSS Distributions 	}
2749*2c2f96dcSApple OSS Distributions 
2750*2c2f96dcSApple OSS Distributions 	/* No units are present */
2751*2c2f96dcSApple OSS Distributions 	if (flags & QOS_PARALLELISM_CLUSTER_SHARED_RESOURCE) {
2752*2c2f96dcSApple OSS Distributions 		return ENOTSUP;
2753*2c2f96dcSApple OSS Distributions 	}
2754*2c2f96dcSApple OSS Distributions 
2755*2c2f96dcSApple OSS Distributions 	if (flags & QOS_PARALLELISM_REALTIME) {
2756*2c2f96dcSApple OSS Distributions 		if (qos) {
2757*2c2f96dcSApple OSS Distributions 			return EINVAL;
2758*2c2f96dcSApple OSS Distributions 		}
2759*2c2f96dcSApple OSS Distributions 	} else if (qos == THREAD_QOS_UNSPECIFIED || qos >= THREAD_QOS_LAST) {
2760*2c2f96dcSApple OSS Distributions 		return EINVAL;
2761*2c2f96dcSApple OSS Distributions 	}
2762*2c2f96dcSApple OSS Distributions 
2763*2c2f96dcSApple OSS Distributions 	*retval = qos_max_parallelism(qos, flags);
2764*2c2f96dcSApple OSS Distributions 	return 0;
2765*2c2f96dcSApple OSS Distributions }
2766*2c2f96dcSApple OSS Distributions 
2767*2c2f96dcSApple OSS Distributions static int
bsdthread_dispatch_apply_attr(__unused struct proc * p,thread_t thread,unsigned long flags,uint64_t value1,__unused uint64_t value2)2768*2c2f96dcSApple OSS Distributions bsdthread_dispatch_apply_attr(__unused struct proc *p, thread_t thread,
2769*2c2f96dcSApple OSS Distributions     unsigned long flags, uint64_t value1, __unused uint64_t value2)
2770*2c2f96dcSApple OSS Distributions {
2771*2c2f96dcSApple OSS Distributions 	uint32_t apply_worker_index;
2772*2c2f96dcSApple OSS Distributions 	kern_return_t kr;
2773*2c2f96dcSApple OSS Distributions 
2774*2c2f96dcSApple OSS Distributions 	switch (flags) {
2775*2c2f96dcSApple OSS Distributions 	case _PTHREAD_DISPATCH_APPLY_ATTR_CLUSTER_SHARED_RSRC_SET:
2776*2c2f96dcSApple OSS Distributions 		apply_worker_index = (uint32_t)value1;
2777*2c2f96dcSApple OSS Distributions 		kr = thread_shared_rsrc_policy_set(thread, apply_worker_index, CLUSTER_SHARED_RSRC_TYPE_RR, SHARED_RSRC_POLICY_AGENT_DISPATCH);
2778*2c2f96dcSApple OSS Distributions 		/*
2779*2c2f96dcSApple OSS Distributions 		 * KERN_INVALID_POLICY indicates that the thread was trying to bind to a
2780*2c2f96dcSApple OSS Distributions 		 * cluster which it was not eligible to execute on.
2781*2c2f96dcSApple OSS Distributions 		 */
2782*2c2f96dcSApple OSS Distributions 		return (kr == KERN_SUCCESS) ? 0 : ((kr == KERN_INVALID_POLICY) ? ENOTSUP : EINVAL);
2783*2c2f96dcSApple OSS Distributions 	case _PTHREAD_DISPATCH_APPLY_ATTR_CLUSTER_SHARED_RSRC_CLEAR:
2784*2c2f96dcSApple OSS Distributions 		kr = thread_shared_rsrc_policy_clear(thread, CLUSTER_SHARED_RSRC_TYPE_RR, SHARED_RSRC_POLICY_AGENT_DISPATCH);
2785*2c2f96dcSApple OSS Distributions 		return (kr == KERN_SUCCESS) ? 0 : EINVAL;
2786*2c2f96dcSApple OSS Distributions 	default:
2787*2c2f96dcSApple OSS Distributions 		return EINVAL;
2788*2c2f96dcSApple OSS Distributions 	}
2789*2c2f96dcSApple OSS Distributions }
2790*2c2f96dcSApple OSS Distributions 
2791*2c2f96dcSApple OSS Distributions #define ENSURE_UNUSED(arg) \
2792*2c2f96dcSApple OSS Distributions 	        ({ if ((arg) != 0) { return EINVAL; } })
2793*2c2f96dcSApple OSS Distributions 
2794*2c2f96dcSApple OSS Distributions int
bsdthread_ctl(struct proc * p,struct bsdthread_ctl_args * uap,int * retval)2795*2c2f96dcSApple OSS Distributions bsdthread_ctl(struct proc *p, struct bsdthread_ctl_args *uap, int *retval)
2796*2c2f96dcSApple OSS Distributions {
2797*2c2f96dcSApple OSS Distributions 	switch (uap->cmd) {
2798*2c2f96dcSApple OSS Distributions 	case BSDTHREAD_CTL_QOS_OVERRIDE_START:
2799*2c2f96dcSApple OSS Distributions 		return bsdthread_add_explicit_override(p, (mach_port_name_t)uap->arg1,
2800*2c2f96dcSApple OSS Distributions 		           (pthread_priority_t)uap->arg2, uap->arg3);
2801*2c2f96dcSApple OSS Distributions 	case BSDTHREAD_CTL_QOS_OVERRIDE_END:
2802*2c2f96dcSApple OSS Distributions 		ENSURE_UNUSED(uap->arg3);
2803*2c2f96dcSApple OSS Distributions 		return bsdthread_remove_explicit_override(p, (mach_port_name_t)uap->arg1,
2804*2c2f96dcSApple OSS Distributions 		           (user_addr_t)uap->arg2);
2805*2c2f96dcSApple OSS Distributions 
2806*2c2f96dcSApple OSS Distributions 	case BSDTHREAD_CTL_QOS_OVERRIDE_DISPATCH:
2807*2c2f96dcSApple OSS Distributions 		return workq_thread_add_dispatch_override(p, (mach_port_name_t)uap->arg1,
2808*2c2f96dcSApple OSS Distributions 		           (pthread_priority_t)uap->arg2, uap->arg3);
2809*2c2f96dcSApple OSS Distributions 	case BSDTHREAD_CTL_QOS_OVERRIDE_RESET:
2810*2c2f96dcSApple OSS Distributions 		return workq_thread_reset_dispatch_override(p, current_thread());
2811*2c2f96dcSApple OSS Distributions 
2812*2c2f96dcSApple OSS Distributions 	case BSDTHREAD_CTL_SET_SELF:
2813*2c2f96dcSApple OSS Distributions 		return bsdthread_set_self(p, current_thread(),
2814*2c2f96dcSApple OSS Distributions 		           (pthread_priority_t)uap->arg1, (mach_port_name_t)uap->arg2,
2815*2c2f96dcSApple OSS Distributions 		           (enum workq_set_self_flags)uap->arg3);
2816*2c2f96dcSApple OSS Distributions 
2817*2c2f96dcSApple OSS Distributions 	case BSDTHREAD_CTL_QOS_MAX_PARALLELISM:
2818*2c2f96dcSApple OSS Distributions 		ENSURE_UNUSED(uap->arg3);
2819*2c2f96dcSApple OSS Distributions 		return bsdthread_get_max_parallelism((thread_qos_t)uap->arg1,
2820*2c2f96dcSApple OSS Distributions 		           (unsigned long)uap->arg2, retval);
2821*2c2f96dcSApple OSS Distributions 	case BSDTHREAD_CTL_WORKQ_ALLOW_KILL:
2822*2c2f96dcSApple OSS Distributions 		ENSURE_UNUSED(uap->arg2);
2823*2c2f96dcSApple OSS Distributions 		ENSURE_UNUSED(uap->arg3);
2824*2c2f96dcSApple OSS Distributions 		return workq_thread_allow_kill(p, current_thread(), (bool)uap->arg1);
2825*2c2f96dcSApple OSS Distributions 	case BSDTHREAD_CTL_DISPATCH_APPLY_ATTR:
2826*2c2f96dcSApple OSS Distributions 		return bsdthread_dispatch_apply_attr(p, current_thread(),
2827*2c2f96dcSApple OSS Distributions 		           (unsigned long)uap->arg1, (uint64_t)uap->arg2,
2828*2c2f96dcSApple OSS Distributions 		           (uint64_t)uap->arg3);
2829*2c2f96dcSApple OSS Distributions 	case BSDTHREAD_CTL_WORKQ_ALLOW_SIGMASK:
2830*2c2f96dcSApple OSS Distributions 		return workq_allow_sigmask(p, (int)uap->arg1);
2831*2c2f96dcSApple OSS Distributions 	case BSDTHREAD_CTL_SET_QOS:
2832*2c2f96dcSApple OSS Distributions 	case BSDTHREAD_CTL_QOS_DISPATCH_ASYNCHRONOUS_OVERRIDE_ADD:
2833*2c2f96dcSApple OSS Distributions 	case BSDTHREAD_CTL_QOS_DISPATCH_ASYNCHRONOUS_OVERRIDE_RESET:
2834*2c2f96dcSApple OSS Distributions 		/* no longer supported */
2835*2c2f96dcSApple OSS Distributions 		return ENOTSUP;
2836*2c2f96dcSApple OSS Distributions 
2837*2c2f96dcSApple OSS Distributions 	default:
2838*2c2f96dcSApple OSS Distributions 		return EINVAL;
2839*2c2f96dcSApple OSS Distributions 	}
2840*2c2f96dcSApple OSS Distributions }
2841*2c2f96dcSApple OSS Distributions 
2842*2c2f96dcSApple OSS Distributions #pragma mark workqueue thread manipulation
2843*2c2f96dcSApple OSS Distributions 
2844*2c2f96dcSApple OSS Distributions static void __dead2
2845*2c2f96dcSApple OSS Distributions workq_unpark_select_threadreq_or_park_and_unlock(proc_t p, struct workqueue *wq,
2846*2c2f96dcSApple OSS Distributions     struct uthread *uth, uint32_t setup_flags);
2847*2c2f96dcSApple OSS Distributions 
2848*2c2f96dcSApple OSS Distributions static void __dead2
2849*2c2f96dcSApple OSS Distributions workq_select_threadreq_or_park_and_unlock(proc_t p, struct workqueue *wq,
2850*2c2f96dcSApple OSS Distributions     struct uthread *uth, uint32_t setup_flags);
2851*2c2f96dcSApple OSS Distributions 
2852*2c2f96dcSApple OSS Distributions static void workq_setup_and_run(proc_t p, struct uthread *uth, int flags) __dead2;
2853*2c2f96dcSApple OSS Distributions 
2854*2c2f96dcSApple OSS Distributions #if KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD
2855*2c2f96dcSApple OSS Distributions static inline uint64_t
workq_trace_req_id(workq_threadreq_t req)2856*2c2f96dcSApple OSS Distributions workq_trace_req_id(workq_threadreq_t req)
2857*2c2f96dcSApple OSS Distributions {
2858*2c2f96dcSApple OSS Distributions 	struct kqworkloop *kqwl;
2859*2c2f96dcSApple OSS Distributions 	if (req->tr_flags & WORKQ_TR_FLAG_WORKLOOP) {
2860*2c2f96dcSApple OSS Distributions 		kqwl = __container_of(req, struct kqworkloop, kqwl_request);
2861*2c2f96dcSApple OSS Distributions 		return kqwl->kqwl_dynamicid;
2862*2c2f96dcSApple OSS Distributions 	}
2863*2c2f96dcSApple OSS Distributions 
2864*2c2f96dcSApple OSS Distributions 	return VM_KERNEL_ADDRHIDE(req);
2865*2c2f96dcSApple OSS Distributions }
2866*2c2f96dcSApple OSS Distributions #endif
2867*2c2f96dcSApple OSS Distributions 
2868*2c2f96dcSApple OSS Distributions /**
2869*2c2f96dcSApple OSS Distributions  * Entry point for libdispatch to ask for threads
2870*2c2f96dcSApple OSS Distributions  */
2871*2c2f96dcSApple OSS Distributions static int
workq_reqthreads(struct proc * p,uint32_t reqcount,pthread_priority_t pp,bool cooperative)2872*2c2f96dcSApple OSS Distributions workq_reqthreads(struct proc *p, uint32_t reqcount, pthread_priority_t pp, bool cooperative)
2873*2c2f96dcSApple OSS Distributions {
2874*2c2f96dcSApple OSS Distributions 	thread_qos_t qos = _pthread_priority_thread_qos(pp);
2875*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = proc_get_wqptr(p);
2876*2c2f96dcSApple OSS Distributions 	uint32_t unpaced, upcall_flags = WQ_FLAG_THREAD_NEWSPI;
2877*2c2f96dcSApple OSS Distributions 	int ret = 0;
2878*2c2f96dcSApple OSS Distributions 
2879*2c2f96dcSApple OSS Distributions 	if (wq == NULL || reqcount <= 0 || reqcount > UINT16_MAX ||
2880*2c2f96dcSApple OSS Distributions 	    qos == THREAD_QOS_UNSPECIFIED) {
2881*2c2f96dcSApple OSS Distributions 		ret = EINVAL;
2882*2c2f96dcSApple OSS Distributions 		goto exit;
2883*2c2f96dcSApple OSS Distributions 	}
2884*2c2f96dcSApple OSS Distributions 
2885*2c2f96dcSApple OSS Distributions 	WQ_TRACE_WQ(TRACE_wq_wqops_reqthreads | DBG_FUNC_NONE,
2886*2c2f96dcSApple OSS Distributions 	    wq, reqcount, pp, cooperative);
2887*2c2f96dcSApple OSS Distributions 
2888*2c2f96dcSApple OSS Distributions 	workq_threadreq_t req = zalloc(workq_zone_threadreq);
2889*2c2f96dcSApple OSS Distributions 	priority_queue_entry_init(&req->tr_entry);
2890*2c2f96dcSApple OSS Distributions 	req->tr_state = WORKQ_TR_STATE_NEW;
2891*2c2f96dcSApple OSS Distributions 	req->tr_qos   = qos;
2892*2c2f96dcSApple OSS Distributions 	workq_tr_flags_t tr_flags = 0;
2893*2c2f96dcSApple OSS Distributions 
2894*2c2f96dcSApple OSS Distributions 	if (pp & _PTHREAD_PRIORITY_OVERCOMMIT_FLAG) {
2895*2c2f96dcSApple OSS Distributions 		tr_flags |= WORKQ_TR_FLAG_OVERCOMMIT;
2896*2c2f96dcSApple OSS Distributions 		upcall_flags |= WQ_FLAG_THREAD_OVERCOMMIT;
2897*2c2f96dcSApple OSS Distributions 	}
2898*2c2f96dcSApple OSS Distributions 
2899*2c2f96dcSApple OSS Distributions 	if (cooperative) {
2900*2c2f96dcSApple OSS Distributions 		tr_flags |= WORKQ_TR_FLAG_COOPERATIVE;
2901*2c2f96dcSApple OSS Distributions 		upcall_flags |= WQ_FLAG_THREAD_COOPERATIVE;
2902*2c2f96dcSApple OSS Distributions 
2903*2c2f96dcSApple OSS Distributions 		if (reqcount > 1) {
2904*2c2f96dcSApple OSS Distributions 			ret = ENOTSUP;
2905*2c2f96dcSApple OSS Distributions 			goto free_and_exit;
2906*2c2f96dcSApple OSS Distributions 		}
2907*2c2f96dcSApple OSS Distributions 	}
2908*2c2f96dcSApple OSS Distributions 
2909*2c2f96dcSApple OSS Distributions 	/* A thread request cannot be both overcommit and cooperative */
2910*2c2f96dcSApple OSS Distributions 	if (workq_tr_is_cooperative(tr_flags) &&
2911*2c2f96dcSApple OSS Distributions 	    workq_tr_is_overcommit(tr_flags)) {
2912*2c2f96dcSApple OSS Distributions 		ret = EINVAL;
2913*2c2f96dcSApple OSS Distributions 		goto free_and_exit;
2914*2c2f96dcSApple OSS Distributions 	}
2915*2c2f96dcSApple OSS Distributions 	req->tr_flags = tr_flags;
2916*2c2f96dcSApple OSS Distributions 
2917*2c2f96dcSApple OSS Distributions 	WQ_TRACE_WQ(TRACE_wq_thread_request_initiate | DBG_FUNC_NONE,
2918*2c2f96dcSApple OSS Distributions 	    wq, workq_trace_req_id(req), req->tr_qos, reqcount);
2919*2c2f96dcSApple OSS Distributions 
2920*2c2f96dcSApple OSS Distributions 	workq_lock_spin(wq);
2921*2c2f96dcSApple OSS Distributions 	do {
2922*2c2f96dcSApple OSS Distributions 		if (_wq_exiting(wq)) {
2923*2c2f96dcSApple OSS Distributions 			goto unlock_and_exit;
2924*2c2f96dcSApple OSS Distributions 		}
2925*2c2f96dcSApple OSS Distributions 
2926*2c2f96dcSApple OSS Distributions 		/*
2927*2c2f96dcSApple OSS Distributions 		 * When userspace is asking for parallelism, wakeup up to (reqcount - 1)
2928*2c2f96dcSApple OSS Distributions 		 * threads without pacing, to inform the scheduler of that workload.
2929*2c2f96dcSApple OSS Distributions 		 *
2930*2c2f96dcSApple OSS Distributions 		 * The last requests, or the ones that failed the admission checks are
2931*2c2f96dcSApple OSS Distributions 		 * enqueued and go through the regular creator codepath.
2932*2c2f96dcSApple OSS Distributions 		 *
2933*2c2f96dcSApple OSS Distributions 		 * If there aren't enough threads, add one, but re-evaluate everything
2934*2c2f96dcSApple OSS Distributions 		 * as conditions may now have changed.
2935*2c2f96dcSApple OSS Distributions 		 */
2936*2c2f96dcSApple OSS Distributions 		unpaced = reqcount - 1;
2937*2c2f96dcSApple OSS Distributions 
2938*2c2f96dcSApple OSS Distributions 		if (reqcount > 1) {
2939*2c2f96dcSApple OSS Distributions 			/* We don't handle asking for parallelism on the cooperative
2940*2c2f96dcSApple OSS Distributions 			 * workqueue just yet */
2941*2c2f96dcSApple OSS Distributions 			assert(!workq_threadreq_is_cooperative(req));
2942*2c2f96dcSApple OSS Distributions 
2943*2c2f96dcSApple OSS Distributions 			if (workq_threadreq_is_nonovercommit(req)) {
2944*2c2f96dcSApple OSS Distributions 				unpaced = workq_constrained_allowance(wq, qos, NULL, false);
2945*2c2f96dcSApple OSS Distributions 				if (unpaced >= reqcount - 1) {
2946*2c2f96dcSApple OSS Distributions 					unpaced = reqcount - 1;
2947*2c2f96dcSApple OSS Distributions 				}
2948*2c2f96dcSApple OSS Distributions 			}
2949*2c2f96dcSApple OSS Distributions 		}
2950*2c2f96dcSApple OSS Distributions 
2951*2c2f96dcSApple OSS Distributions 		/*
2952*2c2f96dcSApple OSS Distributions 		 * This path does not currently handle custom workloop parameters
2953*2c2f96dcSApple OSS Distributions 		 * when creating threads for parallelism.
2954*2c2f96dcSApple OSS Distributions 		 */
2955*2c2f96dcSApple OSS Distributions 		assert(!(req->tr_flags & WORKQ_TR_FLAG_WL_PARAMS));
2956*2c2f96dcSApple OSS Distributions 
2957*2c2f96dcSApple OSS Distributions 		/*
2958*2c2f96dcSApple OSS Distributions 		 * This is a trimmed down version of workq_threadreq_bind_and_unlock()
2959*2c2f96dcSApple OSS Distributions 		 */
2960*2c2f96dcSApple OSS Distributions 		while (unpaced > 0 && wq->wq_thidlecount) {
2961*2c2f96dcSApple OSS Distributions 			struct uthread *uth;
2962*2c2f96dcSApple OSS Distributions 			bool needs_wakeup;
2963*2c2f96dcSApple OSS Distributions 			uint8_t uu_flags = UT_WORKQ_EARLY_BOUND;
2964*2c2f96dcSApple OSS Distributions 
2965*2c2f96dcSApple OSS Distributions 			if (workq_tr_is_overcommit(req->tr_flags)) {
2966*2c2f96dcSApple OSS Distributions 				uu_flags |= UT_WORKQ_OVERCOMMIT;
2967*2c2f96dcSApple OSS Distributions 			}
2968*2c2f96dcSApple OSS Distributions 
2969*2c2f96dcSApple OSS Distributions 			uth = workq_pop_idle_thread(wq, uu_flags, &needs_wakeup);
2970*2c2f96dcSApple OSS Distributions 
2971*2c2f96dcSApple OSS Distributions 			_wq_thactive_inc(wq, qos);
2972*2c2f96dcSApple OSS Distributions 			wq->wq_thscheduled_count[_wq_bucket(qos)]++;
2973*2c2f96dcSApple OSS Distributions 			workq_thread_reset_pri(wq, uth, req, /*unpark*/ true);
2974*2c2f96dcSApple OSS Distributions 			wq->wq_fulfilled++;
2975*2c2f96dcSApple OSS Distributions 
2976*2c2f96dcSApple OSS Distributions 			uth->uu_save.uus_workq_park_data.upcall_flags = upcall_flags;
2977*2c2f96dcSApple OSS Distributions 			uth->uu_save.uus_workq_park_data.thread_request = req;
2978*2c2f96dcSApple OSS Distributions 			if (needs_wakeup) {
2979*2c2f96dcSApple OSS Distributions 				workq_thread_wakeup(uth);
2980*2c2f96dcSApple OSS Distributions 			}
2981*2c2f96dcSApple OSS Distributions 			unpaced--;
2982*2c2f96dcSApple OSS Distributions 			reqcount--;
2983*2c2f96dcSApple OSS Distributions 		}
2984*2c2f96dcSApple OSS Distributions 	} while (unpaced && wq->wq_nthreads < wq_max_threads &&
2985*2c2f96dcSApple OSS Distributions 	    workq_add_new_idle_thread(p, wq));
2986*2c2f96dcSApple OSS Distributions 
2987*2c2f96dcSApple OSS Distributions 	if (_wq_exiting(wq)) {
2988*2c2f96dcSApple OSS Distributions 		goto unlock_and_exit;
2989*2c2f96dcSApple OSS Distributions 	}
2990*2c2f96dcSApple OSS Distributions 
2991*2c2f96dcSApple OSS Distributions 	req->tr_count = (uint16_t)reqcount;
2992*2c2f96dcSApple OSS Distributions 	if (workq_threadreq_enqueue(wq, req)) {
2993*2c2f96dcSApple OSS Distributions 		/* This can drop the workqueue lock, and take it again */
2994*2c2f96dcSApple OSS Distributions 		workq_schedule_creator(p, wq, WORKQ_THREADREQ_CAN_CREATE_THREADS);
2995*2c2f96dcSApple OSS Distributions 	}
2996*2c2f96dcSApple OSS Distributions 	workq_unlock(wq);
2997*2c2f96dcSApple OSS Distributions 	return 0;
2998*2c2f96dcSApple OSS Distributions 
2999*2c2f96dcSApple OSS Distributions unlock_and_exit:
3000*2c2f96dcSApple OSS Distributions 	workq_unlock(wq);
3001*2c2f96dcSApple OSS Distributions free_and_exit:
3002*2c2f96dcSApple OSS Distributions 	zfree(workq_zone_threadreq, req);
3003*2c2f96dcSApple OSS Distributions exit:
3004*2c2f96dcSApple OSS Distributions 	return ret;
3005*2c2f96dcSApple OSS Distributions }
3006*2c2f96dcSApple OSS Distributions 
3007*2c2f96dcSApple OSS Distributions bool
workq_kern_threadreq_initiate(struct proc * p,workq_threadreq_t req,struct turnstile * workloop_ts,thread_qos_t qos,workq_kern_threadreq_flags_t flags)3008*2c2f96dcSApple OSS Distributions workq_kern_threadreq_initiate(struct proc *p, workq_threadreq_t req,
3009*2c2f96dcSApple OSS Distributions     struct turnstile *workloop_ts, thread_qos_t qos,
3010*2c2f96dcSApple OSS Distributions     workq_kern_threadreq_flags_t flags)
3011*2c2f96dcSApple OSS Distributions {
3012*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = proc_get_wqptr_fast(p);
3013*2c2f96dcSApple OSS Distributions 	struct uthread *uth = NULL;
3014*2c2f96dcSApple OSS Distributions 
3015*2c2f96dcSApple OSS Distributions 	assert(req->tr_flags & (WORKQ_TR_FLAG_WORKLOOP | WORKQ_TR_FLAG_KEVENT));
3016*2c2f96dcSApple OSS Distributions 
3017*2c2f96dcSApple OSS Distributions 	if (req->tr_flags & WORKQ_TR_FLAG_WL_OUTSIDE_QOS) {
3018*2c2f96dcSApple OSS Distributions 		workq_threadreq_param_t trp = kqueue_threadreq_workloop_param(req);
3019*2c2f96dcSApple OSS Distributions 		qos = thread_workq_qos_for_pri(trp.trp_pri);
3020*2c2f96dcSApple OSS Distributions 		if (qos == THREAD_QOS_UNSPECIFIED) {
3021*2c2f96dcSApple OSS Distributions 			qos = WORKQ_THREAD_QOS_ABOVEUI;
3022*2c2f96dcSApple OSS Distributions 		}
3023*2c2f96dcSApple OSS Distributions 	}
3024*2c2f96dcSApple OSS Distributions 
3025*2c2f96dcSApple OSS Distributions 	assert(req->tr_state == WORKQ_TR_STATE_IDLE);
3026*2c2f96dcSApple OSS Distributions 	priority_queue_entry_init(&req->tr_entry);
3027*2c2f96dcSApple OSS Distributions 	req->tr_count = 1;
3028*2c2f96dcSApple OSS Distributions 	req->tr_state = WORKQ_TR_STATE_NEW;
3029*2c2f96dcSApple OSS Distributions 	req->tr_qos   = qos;
3030*2c2f96dcSApple OSS Distributions 
3031*2c2f96dcSApple OSS Distributions 	WQ_TRACE_WQ(TRACE_wq_thread_request_initiate | DBG_FUNC_NONE, wq,
3032*2c2f96dcSApple OSS Distributions 	    workq_trace_req_id(req), qos, 1);
3033*2c2f96dcSApple OSS Distributions 
3034*2c2f96dcSApple OSS Distributions 	if (flags & WORKQ_THREADREQ_ATTEMPT_REBIND) {
3035*2c2f96dcSApple OSS Distributions 		/*
3036*2c2f96dcSApple OSS Distributions 		 * we're called back synchronously from the context of
3037*2c2f96dcSApple OSS Distributions 		 * kqueue_threadreq_unbind from within workq_thread_return()
3038*2c2f96dcSApple OSS Distributions 		 * we can try to match up this thread with this request !
3039*2c2f96dcSApple OSS Distributions 		 */
3040*2c2f96dcSApple OSS Distributions 		uth = current_uthread();
3041*2c2f96dcSApple OSS Distributions 		assert(uth->uu_kqr_bound == NULL);
3042*2c2f96dcSApple OSS Distributions 	}
3043*2c2f96dcSApple OSS Distributions 
3044*2c2f96dcSApple OSS Distributions 	workq_lock_spin(wq);
3045*2c2f96dcSApple OSS Distributions 	if (_wq_exiting(wq)) {
3046*2c2f96dcSApple OSS Distributions 		req->tr_state = WORKQ_TR_STATE_IDLE;
3047*2c2f96dcSApple OSS Distributions 		workq_unlock(wq);
3048*2c2f96dcSApple OSS Distributions 		return false;
3049*2c2f96dcSApple OSS Distributions 	}
3050*2c2f96dcSApple OSS Distributions 
3051*2c2f96dcSApple OSS Distributions 	if (uth && workq_threadreq_admissible(wq, uth, req)) {
3052*2c2f96dcSApple OSS Distributions 		/* This is the case of the rebind - we were about to park and unbind
3053*2c2f96dcSApple OSS Distributions 		 * when more events came so keep the binding.
3054*2c2f96dcSApple OSS Distributions 		 */
3055*2c2f96dcSApple OSS Distributions 		assert(uth != wq->wq_creator);
3056*2c2f96dcSApple OSS Distributions 
3057*2c2f96dcSApple OSS Distributions 		if (uth->uu_workq_pri.qos_bucket != req->tr_qos) {
3058*2c2f96dcSApple OSS Distributions 			_wq_thactive_move(wq, uth->uu_workq_pri.qos_bucket, req->tr_qos);
3059*2c2f96dcSApple OSS Distributions 			workq_thread_reset_pri(wq, uth, req, /*unpark*/ false);
3060*2c2f96dcSApple OSS Distributions 		}
3061*2c2f96dcSApple OSS Distributions 		/*
3062*2c2f96dcSApple OSS Distributions 		 * We're called from workq_kern_threadreq_initiate()
3063*2c2f96dcSApple OSS Distributions 		 * due to an unbind, with the kq req held.
3064*2c2f96dcSApple OSS Distributions 		 */
3065*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_thread_logical_run | DBG_FUNC_START, wq,
3066*2c2f96dcSApple OSS Distributions 		    workq_trace_req_id(req), req->tr_flags, 0);
3067*2c2f96dcSApple OSS Distributions 		wq->wq_fulfilled++;
3068*2c2f96dcSApple OSS Distributions 
3069*2c2f96dcSApple OSS Distributions 		kqueue_threadreq_bind(p, req, get_machthread(uth), 0);
3070*2c2f96dcSApple OSS Distributions 	} else {
3071*2c2f96dcSApple OSS Distributions 		if (workloop_ts) {
3072*2c2f96dcSApple OSS Distributions 			workq_perform_turnstile_operation_locked(wq, ^{
3073*2c2f96dcSApple OSS Distributions 				turnstile_update_inheritor(workloop_ts, wq->wq_turnstile,
3074*2c2f96dcSApple OSS Distributions 				TURNSTILE_IMMEDIATE_UPDATE | TURNSTILE_INHERITOR_TURNSTILE);
3075*2c2f96dcSApple OSS Distributions 				turnstile_update_inheritor_complete(workloop_ts,
3076*2c2f96dcSApple OSS Distributions 				TURNSTILE_INTERLOCK_HELD);
3077*2c2f96dcSApple OSS Distributions 			});
3078*2c2f96dcSApple OSS Distributions 		}
3079*2c2f96dcSApple OSS Distributions 
3080*2c2f96dcSApple OSS Distributions 		bool reevaluate_creator_thread_group = false;
3081*2c2f96dcSApple OSS Distributions #if CONFIG_PREADOPT_TG
3082*2c2f96dcSApple OSS Distributions 		reevaluate_creator_thread_group = (flags & WORKQ_THREADREQ_REEVALUATE_PREADOPT_TG);
3083*2c2f96dcSApple OSS Distributions #endif
3084*2c2f96dcSApple OSS Distributions 		/* We enqueued the highest priority item or we may need to reevaluate if
3085*2c2f96dcSApple OSS Distributions 		 * the creator needs a thread group pre-adoption */
3086*2c2f96dcSApple OSS Distributions 		if (workq_threadreq_enqueue(wq, req) || reevaluate_creator_thread_group) {
3087*2c2f96dcSApple OSS Distributions 			workq_schedule_creator(p, wq, flags);
3088*2c2f96dcSApple OSS Distributions 		}
3089*2c2f96dcSApple OSS Distributions 	}
3090*2c2f96dcSApple OSS Distributions 
3091*2c2f96dcSApple OSS Distributions 	workq_unlock(wq);
3092*2c2f96dcSApple OSS Distributions 
3093*2c2f96dcSApple OSS Distributions 	return true;
3094*2c2f96dcSApple OSS Distributions }
3095*2c2f96dcSApple OSS Distributions 
3096*2c2f96dcSApple OSS Distributions void
workq_kern_threadreq_modify(struct proc * p,workq_threadreq_t req,thread_qos_t qos,workq_kern_threadreq_flags_t flags)3097*2c2f96dcSApple OSS Distributions workq_kern_threadreq_modify(struct proc *p, workq_threadreq_t req,
3098*2c2f96dcSApple OSS Distributions     thread_qos_t qos, workq_kern_threadreq_flags_t flags)
3099*2c2f96dcSApple OSS Distributions {
3100*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = proc_get_wqptr_fast(p);
3101*2c2f96dcSApple OSS Distributions 	bool make_overcommit = false;
3102*2c2f96dcSApple OSS Distributions 
3103*2c2f96dcSApple OSS Distributions 	if (req->tr_flags & WORKQ_TR_FLAG_WL_OUTSIDE_QOS) {
3104*2c2f96dcSApple OSS Distributions 		/* Requests outside-of-QoS shouldn't accept modify operations */
3105*2c2f96dcSApple OSS Distributions 		return;
3106*2c2f96dcSApple OSS Distributions 	}
3107*2c2f96dcSApple OSS Distributions 
3108*2c2f96dcSApple OSS Distributions 	workq_lock_spin(wq);
3109*2c2f96dcSApple OSS Distributions 
3110*2c2f96dcSApple OSS Distributions 	assert(req->tr_qos != WORKQ_THREAD_QOS_MANAGER);
3111*2c2f96dcSApple OSS Distributions 	assert(req->tr_flags & (WORKQ_TR_FLAG_KEVENT | WORKQ_TR_FLAG_WORKLOOP));
3112*2c2f96dcSApple OSS Distributions 
3113*2c2f96dcSApple OSS Distributions 	if (req->tr_state == WORKQ_TR_STATE_BINDING) {
3114*2c2f96dcSApple OSS Distributions 		kqueue_threadreq_bind(p, req, req->tr_thread, 0);
3115*2c2f96dcSApple OSS Distributions 		workq_unlock(wq);
3116*2c2f96dcSApple OSS Distributions 		return;
3117*2c2f96dcSApple OSS Distributions 	}
3118*2c2f96dcSApple OSS Distributions 
3119*2c2f96dcSApple OSS Distributions 	if (flags & WORKQ_THREADREQ_MAKE_OVERCOMMIT) {
3120*2c2f96dcSApple OSS Distributions 		/* TODO (rokhinip): We come into this code path for kqwl thread
3121*2c2f96dcSApple OSS Distributions 		 * requests. kqwl requests cannot be cooperative.
3122*2c2f96dcSApple OSS Distributions 		 */
3123*2c2f96dcSApple OSS Distributions 		assert(!workq_threadreq_is_cooperative(req));
3124*2c2f96dcSApple OSS Distributions 
3125*2c2f96dcSApple OSS Distributions 		make_overcommit = workq_threadreq_is_nonovercommit(req);
3126*2c2f96dcSApple OSS Distributions 	}
3127*2c2f96dcSApple OSS Distributions 
3128*2c2f96dcSApple OSS Distributions 	if (_wq_exiting(wq) || (req->tr_qos == qos && !make_overcommit)) {
3129*2c2f96dcSApple OSS Distributions 		workq_unlock(wq);
3130*2c2f96dcSApple OSS Distributions 		return;
3131*2c2f96dcSApple OSS Distributions 	}
3132*2c2f96dcSApple OSS Distributions 
3133*2c2f96dcSApple OSS Distributions 	assert(req->tr_count == 1);
3134*2c2f96dcSApple OSS Distributions 	if (req->tr_state != WORKQ_TR_STATE_QUEUED) {
3135*2c2f96dcSApple OSS Distributions 		panic("Invalid thread request (%p) state %d", req, req->tr_state);
3136*2c2f96dcSApple OSS Distributions 	}
3137*2c2f96dcSApple OSS Distributions 
3138*2c2f96dcSApple OSS Distributions 	WQ_TRACE_WQ(TRACE_wq_thread_request_modify | DBG_FUNC_NONE, wq,
3139*2c2f96dcSApple OSS Distributions 	    workq_trace_req_id(req), qos, 0);
3140*2c2f96dcSApple OSS Distributions 
3141*2c2f96dcSApple OSS Distributions 	struct priority_queue_sched_max *pq = workq_priority_queue_for_req(wq, req);
3142*2c2f96dcSApple OSS Distributions 	workq_threadreq_t req_max;
3143*2c2f96dcSApple OSS Distributions 
3144*2c2f96dcSApple OSS Distributions 	/*
3145*2c2f96dcSApple OSS Distributions 	 * Stage 1: Dequeue the request from its priority queue.
3146*2c2f96dcSApple OSS Distributions 	 *
3147*2c2f96dcSApple OSS Distributions 	 * If we dequeue the root item of the constrained priority queue,
3148*2c2f96dcSApple OSS Distributions 	 * maintain the best constrained request qos invariant.
3149*2c2f96dcSApple OSS Distributions 	 */
3150*2c2f96dcSApple OSS Distributions 	if (priority_queue_remove(pq, &req->tr_entry)) {
3151*2c2f96dcSApple OSS Distributions 		if (workq_threadreq_is_nonovercommit(req)) {
3152*2c2f96dcSApple OSS Distributions 			_wq_thactive_refresh_best_constrained_req_qos(wq);
3153*2c2f96dcSApple OSS Distributions 		}
3154*2c2f96dcSApple OSS Distributions 	}
3155*2c2f96dcSApple OSS Distributions 
3156*2c2f96dcSApple OSS Distributions 	/*
3157*2c2f96dcSApple OSS Distributions 	 * Stage 2: Apply changes to the thread request
3158*2c2f96dcSApple OSS Distributions 	 *
3159*2c2f96dcSApple OSS Distributions 	 * If the item will not become the root of the priority queue it belongs to,
3160*2c2f96dcSApple OSS Distributions 	 * then we need to wait in line, just enqueue and return quickly.
3161*2c2f96dcSApple OSS Distributions 	 */
3162*2c2f96dcSApple OSS Distributions 	if (__improbable(make_overcommit)) {
3163*2c2f96dcSApple OSS Distributions 		req->tr_flags ^= WORKQ_TR_FLAG_OVERCOMMIT;
3164*2c2f96dcSApple OSS Distributions 		pq = workq_priority_queue_for_req(wq, req);
3165*2c2f96dcSApple OSS Distributions 	}
3166*2c2f96dcSApple OSS Distributions 	req->tr_qos = qos;
3167*2c2f96dcSApple OSS Distributions 
3168*2c2f96dcSApple OSS Distributions 	req_max = priority_queue_max(pq, struct workq_threadreq_s, tr_entry);
3169*2c2f96dcSApple OSS Distributions 	if (req_max && req_max->tr_qos >= qos) {
3170*2c2f96dcSApple OSS Distributions 		priority_queue_entry_set_sched_pri(pq, &req->tr_entry,
3171*2c2f96dcSApple OSS Distributions 		    workq_priority_for_req(req), false);
3172*2c2f96dcSApple OSS Distributions 		priority_queue_insert(pq, &req->tr_entry);
3173*2c2f96dcSApple OSS Distributions 		workq_unlock(wq);
3174*2c2f96dcSApple OSS Distributions 		return;
3175*2c2f96dcSApple OSS Distributions 	}
3176*2c2f96dcSApple OSS Distributions 
3177*2c2f96dcSApple OSS Distributions 	/*
3178*2c2f96dcSApple OSS Distributions 	 * Stage 3: Reevaluate whether we should run the thread request.
3179*2c2f96dcSApple OSS Distributions 	 *
3180*2c2f96dcSApple OSS Distributions 	 * Pretend the thread request is new again:
3181*2c2f96dcSApple OSS Distributions 	 * - adjust wq_reqcount to not count it anymore.
3182*2c2f96dcSApple OSS Distributions 	 * - make its state WORKQ_TR_STATE_NEW (so that workq_threadreq_bind_and_unlock
3183*2c2f96dcSApple OSS Distributions 	 *   properly attempts a synchronous bind)
3184*2c2f96dcSApple OSS Distributions 	 */
3185*2c2f96dcSApple OSS Distributions 	wq->wq_reqcount--;
3186*2c2f96dcSApple OSS Distributions 	req->tr_state = WORKQ_TR_STATE_NEW;
3187*2c2f96dcSApple OSS Distributions 
3188*2c2f96dcSApple OSS Distributions 	/* We enqueued the highest priority item or we may need to reevaluate if
3189*2c2f96dcSApple OSS Distributions 	 * the creator needs a thread group pre-adoption if the request got a new TG */
3190*2c2f96dcSApple OSS Distributions 	bool reevaluate_creator_tg = false;
3191*2c2f96dcSApple OSS Distributions 
3192*2c2f96dcSApple OSS Distributions #if CONFIG_PREADOPT_TG
3193*2c2f96dcSApple OSS Distributions 	reevaluate_creator_tg = (flags & WORKQ_THREADREQ_REEVALUATE_PREADOPT_TG);
3194*2c2f96dcSApple OSS Distributions #endif
3195*2c2f96dcSApple OSS Distributions 
3196*2c2f96dcSApple OSS Distributions 	if (workq_threadreq_enqueue(wq, req) || reevaluate_creator_tg) {
3197*2c2f96dcSApple OSS Distributions 		workq_schedule_creator(p, wq, flags);
3198*2c2f96dcSApple OSS Distributions 	}
3199*2c2f96dcSApple OSS Distributions 	workq_unlock(wq);
3200*2c2f96dcSApple OSS Distributions }
3201*2c2f96dcSApple OSS Distributions 
3202*2c2f96dcSApple OSS Distributions void
workq_kern_threadreq_lock(struct proc * p)3203*2c2f96dcSApple OSS Distributions workq_kern_threadreq_lock(struct proc *p)
3204*2c2f96dcSApple OSS Distributions {
3205*2c2f96dcSApple OSS Distributions 	workq_lock_spin(proc_get_wqptr_fast(p));
3206*2c2f96dcSApple OSS Distributions }
3207*2c2f96dcSApple OSS Distributions 
3208*2c2f96dcSApple OSS Distributions void
workq_kern_threadreq_unlock(struct proc * p)3209*2c2f96dcSApple OSS Distributions workq_kern_threadreq_unlock(struct proc *p)
3210*2c2f96dcSApple OSS Distributions {
3211*2c2f96dcSApple OSS Distributions 	workq_unlock(proc_get_wqptr_fast(p));
3212*2c2f96dcSApple OSS Distributions }
3213*2c2f96dcSApple OSS Distributions 
3214*2c2f96dcSApple OSS Distributions void
workq_kern_threadreq_update_inheritor(struct proc * p,workq_threadreq_t req,thread_t owner,struct turnstile * wl_ts,turnstile_update_flags_t flags)3215*2c2f96dcSApple OSS Distributions workq_kern_threadreq_update_inheritor(struct proc *p, workq_threadreq_t req,
3216*2c2f96dcSApple OSS Distributions     thread_t owner, struct turnstile *wl_ts,
3217*2c2f96dcSApple OSS Distributions     turnstile_update_flags_t flags)
3218*2c2f96dcSApple OSS Distributions {
3219*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = proc_get_wqptr_fast(p);
3220*2c2f96dcSApple OSS Distributions 	turnstile_inheritor_t inheritor;
3221*2c2f96dcSApple OSS Distributions 
3222*2c2f96dcSApple OSS Distributions 	assert(req->tr_qos != WORKQ_THREAD_QOS_MANAGER);
3223*2c2f96dcSApple OSS Distributions 	assert(req->tr_flags & WORKQ_TR_FLAG_WORKLOOP);
3224*2c2f96dcSApple OSS Distributions 	workq_lock_held(wq);
3225*2c2f96dcSApple OSS Distributions 
3226*2c2f96dcSApple OSS Distributions 	if (req->tr_state == WORKQ_TR_STATE_BINDING) {
3227*2c2f96dcSApple OSS Distributions 		kqueue_threadreq_bind(p, req, req->tr_thread,
3228*2c2f96dcSApple OSS Distributions 		    KQUEUE_THREADERQ_BIND_NO_INHERITOR_UPDATE);
3229*2c2f96dcSApple OSS Distributions 		return;
3230*2c2f96dcSApple OSS Distributions 	}
3231*2c2f96dcSApple OSS Distributions 
3232*2c2f96dcSApple OSS Distributions 	if (_wq_exiting(wq)) {
3233*2c2f96dcSApple OSS Distributions 		inheritor = TURNSTILE_INHERITOR_NULL;
3234*2c2f96dcSApple OSS Distributions 	} else {
3235*2c2f96dcSApple OSS Distributions 		if (req->tr_state != WORKQ_TR_STATE_QUEUED) {
3236*2c2f96dcSApple OSS Distributions 			panic("Invalid thread request (%p) state %d", req, req->tr_state);
3237*2c2f96dcSApple OSS Distributions 		}
3238*2c2f96dcSApple OSS Distributions 
3239*2c2f96dcSApple OSS Distributions 		if (owner) {
3240*2c2f96dcSApple OSS Distributions 			inheritor = owner;
3241*2c2f96dcSApple OSS Distributions 			flags |= TURNSTILE_INHERITOR_THREAD;
3242*2c2f96dcSApple OSS Distributions 		} else {
3243*2c2f96dcSApple OSS Distributions 			inheritor = wq->wq_turnstile;
3244*2c2f96dcSApple OSS Distributions 			flags |= TURNSTILE_INHERITOR_TURNSTILE;
3245*2c2f96dcSApple OSS Distributions 		}
3246*2c2f96dcSApple OSS Distributions 	}
3247*2c2f96dcSApple OSS Distributions 
3248*2c2f96dcSApple OSS Distributions 	workq_perform_turnstile_operation_locked(wq, ^{
3249*2c2f96dcSApple OSS Distributions 		turnstile_update_inheritor(wl_ts, inheritor, flags);
3250*2c2f96dcSApple OSS Distributions 	});
3251*2c2f96dcSApple OSS Distributions }
3252*2c2f96dcSApple OSS Distributions 
3253*2c2f96dcSApple OSS Distributions void
workq_kern_threadreq_redrive(struct proc * p,workq_kern_threadreq_flags_t flags)3254*2c2f96dcSApple OSS Distributions workq_kern_threadreq_redrive(struct proc *p, workq_kern_threadreq_flags_t flags)
3255*2c2f96dcSApple OSS Distributions {
3256*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = proc_get_wqptr_fast(p);
3257*2c2f96dcSApple OSS Distributions 
3258*2c2f96dcSApple OSS Distributions 	workq_lock_spin(wq);
3259*2c2f96dcSApple OSS Distributions 	workq_schedule_creator(p, wq, flags);
3260*2c2f96dcSApple OSS Distributions 	workq_unlock(wq);
3261*2c2f96dcSApple OSS Distributions }
3262*2c2f96dcSApple OSS Distributions 
3263*2c2f96dcSApple OSS Distributions /*
3264*2c2f96dcSApple OSS Distributions  * Always called at AST by the thread on itself
3265*2c2f96dcSApple OSS Distributions  *
3266*2c2f96dcSApple OSS Distributions  * Upon quantum expiry, the workqueue subsystem evaluates its state and decides
3267*2c2f96dcSApple OSS Distributions  * on what the thread should do next. The TSD value is always set by the thread
3268*2c2f96dcSApple OSS Distributions  * on itself in the kernel and cleared either by userspace when it acks the TSD
3269*2c2f96dcSApple OSS Distributions  * value and takes action, or by the thread in the kernel when the quantum
3270*2c2f96dcSApple OSS Distributions  * expires again.
3271*2c2f96dcSApple OSS Distributions  */
3272*2c2f96dcSApple OSS Distributions void
workq_kern_quantum_expiry_reevaluate(proc_t proc,thread_t thread)3273*2c2f96dcSApple OSS Distributions workq_kern_quantum_expiry_reevaluate(proc_t proc, thread_t thread)
3274*2c2f96dcSApple OSS Distributions {
3275*2c2f96dcSApple OSS Distributions 	struct uthread *uth = get_bsdthread_info(thread);
3276*2c2f96dcSApple OSS Distributions 
3277*2c2f96dcSApple OSS Distributions 	if (uth->uu_workq_flags & UT_WORKQ_DYING) {
3278*2c2f96dcSApple OSS Distributions 		return;
3279*2c2f96dcSApple OSS Distributions 	}
3280*2c2f96dcSApple OSS Distributions 
3281*2c2f96dcSApple OSS Distributions 	if (!thread_supports_cooperative_workqueue(thread)) {
3282*2c2f96dcSApple OSS Distributions 		panic("Quantum expired for thread that doesn't support cooperative workqueue");
3283*2c2f96dcSApple OSS Distributions 	}
3284*2c2f96dcSApple OSS Distributions 
3285*2c2f96dcSApple OSS Distributions 	thread_qos_t qos = uth->uu_workq_pri.qos_bucket;
3286*2c2f96dcSApple OSS Distributions 	if (qos == THREAD_QOS_UNSPECIFIED) {
3287*2c2f96dcSApple OSS Distributions 		panic("Thread should not have workq bucket of QoS UN");
3288*2c2f96dcSApple OSS Distributions 	}
3289*2c2f96dcSApple OSS Distributions 
3290*2c2f96dcSApple OSS Distributions 	assert(thread_has_expired_workqueue_quantum(thread, false));
3291*2c2f96dcSApple OSS Distributions 
3292*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = proc_get_wqptr(proc);
3293*2c2f96dcSApple OSS Distributions 	assert(wq != NULL);
3294*2c2f96dcSApple OSS Distributions 
3295*2c2f96dcSApple OSS Distributions 	/*
3296*2c2f96dcSApple OSS Distributions 	 * For starters, we're just going to evaluate and see if we need to narrow
3297*2c2f96dcSApple OSS Distributions 	 * the pool and tell this thread to park if needed. In the future, we'll
3298*2c2f96dcSApple OSS Distributions 	 * evaluate and convey other workqueue state information like needing to
3299*2c2f96dcSApple OSS Distributions 	 * pump kevents, etc.
3300*2c2f96dcSApple OSS Distributions 	 */
3301*2c2f96dcSApple OSS Distributions 	uint64_t flags = 0;
3302*2c2f96dcSApple OSS Distributions 
3303*2c2f96dcSApple OSS Distributions 	workq_lock_spin(wq);
3304*2c2f96dcSApple OSS Distributions 
3305*2c2f96dcSApple OSS Distributions 	if (workq_thread_is_cooperative(uth)) {
3306*2c2f96dcSApple OSS Distributions 		if (!workq_cooperative_allowance(wq, qos, uth, false)) {
3307*2c2f96dcSApple OSS Distributions 			flags |= PTHREAD_WQ_QUANTUM_EXPIRY_NARROW;
3308*2c2f96dcSApple OSS Distributions 		} else {
3309*2c2f96dcSApple OSS Distributions 			/* In the future, when we have kevent hookups for the cooperative
3310*2c2f96dcSApple OSS Distributions 			 * pool, we need fancier logic for what userspace should do. But
3311*2c2f96dcSApple OSS Distributions 			 * right now, only userspace thread requests exist - so we'll just
3312*2c2f96dcSApple OSS Distributions 			 * tell userspace to shuffle work items */
3313*2c2f96dcSApple OSS Distributions 			flags |= PTHREAD_WQ_QUANTUM_EXPIRY_SHUFFLE;
3314*2c2f96dcSApple OSS Distributions 		}
3315*2c2f96dcSApple OSS Distributions 	} else if (workq_thread_is_nonovercommit(uth)) {
3316*2c2f96dcSApple OSS Distributions 		if (!workq_constrained_allowance(wq, qos, uth, false)) {
3317*2c2f96dcSApple OSS Distributions 			flags |= PTHREAD_WQ_QUANTUM_EXPIRY_NARROW;
3318*2c2f96dcSApple OSS Distributions 		}
3319*2c2f96dcSApple OSS Distributions 	}
3320*2c2f96dcSApple OSS Distributions 	workq_unlock(wq);
3321*2c2f96dcSApple OSS Distributions 
3322*2c2f96dcSApple OSS Distributions 	WQ_TRACE(TRACE_wq_quantum_expiry_reevaluate, flags, 0, 0, 0);
3323*2c2f96dcSApple OSS Distributions 
3324*2c2f96dcSApple OSS Distributions 	kevent_set_workq_quantum_expiry_user_tsd(proc, thread, flags);
3325*2c2f96dcSApple OSS Distributions 
3326*2c2f96dcSApple OSS Distributions 	/* We have conveyed to userspace about what it needs to do upon quantum
3327*2c2f96dcSApple OSS Distributions 	 * expiry, now rearm the workqueue quantum again */
3328*2c2f96dcSApple OSS Distributions 	thread_arm_workqueue_quantum(get_machthread(uth));
3329*2c2f96dcSApple OSS Distributions }
3330*2c2f96dcSApple OSS Distributions 
3331*2c2f96dcSApple OSS Distributions void
workq_schedule_creator_turnstile_redrive(struct workqueue * wq,bool locked)3332*2c2f96dcSApple OSS Distributions workq_schedule_creator_turnstile_redrive(struct workqueue *wq, bool locked)
3333*2c2f96dcSApple OSS Distributions {
3334*2c2f96dcSApple OSS Distributions 	if (locked) {
3335*2c2f96dcSApple OSS Distributions 		workq_schedule_creator(NULL, wq, WORKQ_THREADREQ_NONE);
3336*2c2f96dcSApple OSS Distributions 	} else {
3337*2c2f96dcSApple OSS Distributions 		workq_schedule_immediate_thread_creation(wq);
3338*2c2f96dcSApple OSS Distributions 	}
3339*2c2f96dcSApple OSS Distributions }
3340*2c2f96dcSApple OSS Distributions 
3341*2c2f96dcSApple OSS Distributions static int
workq_thread_return(struct proc * p,struct workq_kernreturn_args * uap,struct workqueue * wq)3342*2c2f96dcSApple OSS Distributions workq_thread_return(struct proc *p, struct workq_kernreturn_args *uap,
3343*2c2f96dcSApple OSS Distributions     struct workqueue *wq)
3344*2c2f96dcSApple OSS Distributions {
3345*2c2f96dcSApple OSS Distributions 	thread_t th = current_thread();
3346*2c2f96dcSApple OSS Distributions 	struct uthread *uth = get_bsdthread_info(th);
3347*2c2f96dcSApple OSS Distributions 	workq_threadreq_t kqr = uth->uu_kqr_bound;
3348*2c2f96dcSApple OSS Distributions 	workq_threadreq_param_t trp = { };
3349*2c2f96dcSApple OSS Distributions 	int nevents = uap->affinity, error;
3350*2c2f96dcSApple OSS Distributions 	user_addr_t eventlist = uap->item;
3351*2c2f96dcSApple OSS Distributions 
3352*2c2f96dcSApple OSS Distributions 	if (((thread_get_tag(th) & THREAD_TAG_WORKQUEUE) == 0) ||
3353*2c2f96dcSApple OSS Distributions 	    (uth->uu_workq_flags & UT_WORKQ_DYING)) {
3354*2c2f96dcSApple OSS Distributions 		return EINVAL;
3355*2c2f96dcSApple OSS Distributions 	}
3356*2c2f96dcSApple OSS Distributions 
3357*2c2f96dcSApple OSS Distributions 	if (eventlist && nevents && kqr == NULL) {
3358*2c2f96dcSApple OSS Distributions 		return EINVAL;
3359*2c2f96dcSApple OSS Distributions 	}
3360*2c2f96dcSApple OSS Distributions 
3361*2c2f96dcSApple OSS Distributions 	/*
3362*2c2f96dcSApple OSS Distributions 	 * Reset signal mask on the workqueue thread to default state,
3363*2c2f96dcSApple OSS Distributions 	 * but do not touch any signals that are marked for preservation.
3364*2c2f96dcSApple OSS Distributions 	 */
3365*2c2f96dcSApple OSS Distributions 	sigset_t resettable = uth->uu_sigmask & ~p->p_workq_allow_sigmask;
3366*2c2f96dcSApple OSS Distributions 	if (resettable != (sigset_t)~workq_threadmask) {
3367*2c2f96dcSApple OSS Distributions 		proc_lock(p);
3368*2c2f96dcSApple OSS Distributions 		uth->uu_sigmask |= ~workq_threadmask & ~p->p_workq_allow_sigmask;
3369*2c2f96dcSApple OSS Distributions 		proc_unlock(p);
3370*2c2f96dcSApple OSS Distributions 	}
3371*2c2f96dcSApple OSS Distributions 
3372*2c2f96dcSApple OSS Distributions 	if (kqr && kqr->tr_flags & WORKQ_TR_FLAG_WL_PARAMS) {
3373*2c2f96dcSApple OSS Distributions 		/*
3374*2c2f96dcSApple OSS Distributions 		 * Ensure we store the threadreq param before unbinding
3375*2c2f96dcSApple OSS Distributions 		 * the kqr from this thread.
3376*2c2f96dcSApple OSS Distributions 		 */
3377*2c2f96dcSApple OSS Distributions 		trp = kqueue_threadreq_workloop_param(kqr);
3378*2c2f96dcSApple OSS Distributions 	}
3379*2c2f96dcSApple OSS Distributions 
3380*2c2f96dcSApple OSS Distributions 	/*
3381*2c2f96dcSApple OSS Distributions 	 * Freeze the base pri while we decide the fate of this thread.
3382*2c2f96dcSApple OSS Distributions 	 *
3383*2c2f96dcSApple OSS Distributions 	 * Either:
3384*2c2f96dcSApple OSS Distributions 	 * - we return to user and kevent_cleanup will have unfrozen the base pri,
3385*2c2f96dcSApple OSS Distributions 	 * - or we proceed to workq_select_threadreq_or_park_and_unlock() who will.
3386*2c2f96dcSApple OSS Distributions 	 */
3387*2c2f96dcSApple OSS Distributions 	thread_freeze_base_pri(th);
3388*2c2f96dcSApple OSS Distributions 
3389*2c2f96dcSApple OSS Distributions 	if (kqr) {
3390*2c2f96dcSApple OSS Distributions 		uint32_t upcall_flags = WQ_FLAG_THREAD_NEWSPI | WQ_FLAG_THREAD_REUSE;
3391*2c2f96dcSApple OSS Distributions 		if (kqr->tr_flags & WORKQ_TR_FLAG_WORKLOOP) {
3392*2c2f96dcSApple OSS Distributions 			upcall_flags |= WQ_FLAG_THREAD_WORKLOOP | WQ_FLAG_THREAD_KEVENT;
3393*2c2f96dcSApple OSS Distributions 		} else {
3394*2c2f96dcSApple OSS Distributions 			upcall_flags |= WQ_FLAG_THREAD_KEVENT;
3395*2c2f96dcSApple OSS Distributions 		}
3396*2c2f96dcSApple OSS Distributions 		if (uth->uu_workq_pri.qos_bucket == WORKQ_THREAD_QOS_MANAGER) {
3397*2c2f96dcSApple OSS Distributions 			upcall_flags |= WQ_FLAG_THREAD_EVENT_MANAGER;
3398*2c2f96dcSApple OSS Distributions 		} else {
3399*2c2f96dcSApple OSS Distributions 			if (workq_thread_is_overcommit(uth)) {
3400*2c2f96dcSApple OSS Distributions 				upcall_flags |= WQ_FLAG_THREAD_OVERCOMMIT;
3401*2c2f96dcSApple OSS Distributions 			}
3402*2c2f96dcSApple OSS Distributions 			if (uth->uu_workq_flags & UT_WORKQ_OUTSIDE_QOS) {
3403*2c2f96dcSApple OSS Distributions 				upcall_flags |= WQ_FLAG_THREAD_OUTSIDEQOS;
3404*2c2f96dcSApple OSS Distributions 			} else {
3405*2c2f96dcSApple OSS Distributions 				upcall_flags |= uth->uu_workq_pri.qos_req |
3406*2c2f96dcSApple OSS Distributions 				    WQ_FLAG_THREAD_PRIO_QOS;
3407*2c2f96dcSApple OSS Distributions 			}
3408*2c2f96dcSApple OSS Distributions 		}
3409*2c2f96dcSApple OSS Distributions 		error = pthread_functions->workq_handle_stack_events(p, th,
3410*2c2f96dcSApple OSS Distributions 		    get_task_map(proc_task(p)), uth->uu_workq_stackaddr,
3411*2c2f96dcSApple OSS Distributions 		    uth->uu_workq_thport, eventlist, nevents, upcall_flags);
3412*2c2f96dcSApple OSS Distributions 		if (error) {
3413*2c2f96dcSApple OSS Distributions 			assert(uth->uu_kqr_bound == kqr);
3414*2c2f96dcSApple OSS Distributions 			return error;
3415*2c2f96dcSApple OSS Distributions 		}
3416*2c2f96dcSApple OSS Distributions 
3417*2c2f96dcSApple OSS Distributions 		// pthread is supposed to pass KEVENT_FLAG_PARKING here
3418*2c2f96dcSApple OSS Distributions 		// which should cause the above call to either:
3419*2c2f96dcSApple OSS Distributions 		// - not return
3420*2c2f96dcSApple OSS Distributions 		// - return an error
3421*2c2f96dcSApple OSS Distributions 		// - return 0 and have unbound properly
3422*2c2f96dcSApple OSS Distributions 		assert(uth->uu_kqr_bound == NULL);
3423*2c2f96dcSApple OSS Distributions 	}
3424*2c2f96dcSApple OSS Distributions 
3425*2c2f96dcSApple OSS Distributions 	WQ_TRACE_WQ(TRACE_wq_runthread | DBG_FUNC_END, wq, uap->options, 0, 0);
3426*2c2f96dcSApple OSS Distributions 
3427*2c2f96dcSApple OSS Distributions 	thread_sched_call(th, NULL);
3428*2c2f96dcSApple OSS Distributions 	thread_will_park_or_terminate(th);
3429*2c2f96dcSApple OSS Distributions #if CONFIG_WORKLOOP_DEBUG
3430*2c2f96dcSApple OSS Distributions 	UU_KEVENT_HISTORY_WRITE_ENTRY(uth, { .uu_error = -1, });
3431*2c2f96dcSApple OSS Distributions #endif
3432*2c2f96dcSApple OSS Distributions 
3433*2c2f96dcSApple OSS Distributions 	workq_lock_spin(wq);
3434*2c2f96dcSApple OSS Distributions 	WQ_TRACE_WQ(TRACE_wq_thread_logical_run | DBG_FUNC_END, wq, 0, 0, 0);
3435*2c2f96dcSApple OSS Distributions 	uth->uu_save.uus_workq_park_data.workloop_params = trp.trp_value;
3436*2c2f96dcSApple OSS Distributions 	workq_select_threadreq_or_park_and_unlock(p, wq, uth,
3437*2c2f96dcSApple OSS Distributions 	    WQ_SETUP_CLEAR_VOUCHER);
3438*2c2f96dcSApple OSS Distributions 	__builtin_unreachable();
3439*2c2f96dcSApple OSS Distributions }
3440*2c2f96dcSApple OSS Distributions 
3441*2c2f96dcSApple OSS Distributions /**
3442*2c2f96dcSApple OSS Distributions  * Multiplexed call to interact with the workqueue mechanism
3443*2c2f96dcSApple OSS Distributions  */
3444*2c2f96dcSApple OSS Distributions int
workq_kernreturn(struct proc * p,struct workq_kernreturn_args * uap,int32_t * retval)3445*2c2f96dcSApple OSS Distributions workq_kernreturn(struct proc *p, struct workq_kernreturn_args *uap, int32_t *retval)
3446*2c2f96dcSApple OSS Distributions {
3447*2c2f96dcSApple OSS Distributions 	int options = uap->options;
3448*2c2f96dcSApple OSS Distributions 	int arg2 = uap->affinity;
3449*2c2f96dcSApple OSS Distributions 	int arg3 = uap->prio;
3450*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = proc_get_wqptr(p);
3451*2c2f96dcSApple OSS Distributions 	int error = 0;
3452*2c2f96dcSApple OSS Distributions 
3453*2c2f96dcSApple OSS Distributions 	if ((p->p_lflag & P_LREGISTER) == 0) {
3454*2c2f96dcSApple OSS Distributions 		return EINVAL;
3455*2c2f96dcSApple OSS Distributions 	}
3456*2c2f96dcSApple OSS Distributions 
3457*2c2f96dcSApple OSS Distributions 	switch (options) {
3458*2c2f96dcSApple OSS Distributions 	case WQOPS_QUEUE_NEWSPISUPP: {
3459*2c2f96dcSApple OSS Distributions 		/*
3460*2c2f96dcSApple OSS Distributions 		 * arg2 = offset of serialno into dispatch queue
3461*2c2f96dcSApple OSS Distributions 		 * arg3 = kevent support
3462*2c2f96dcSApple OSS Distributions 		 */
3463*2c2f96dcSApple OSS Distributions 		int offset = arg2;
3464*2c2f96dcSApple OSS Distributions 		if (arg3 & 0x01) {
3465*2c2f96dcSApple OSS Distributions 			// If we get here, then userspace has indicated support for kevent delivery.
3466*2c2f96dcSApple OSS Distributions 		}
3467*2c2f96dcSApple OSS Distributions 
3468*2c2f96dcSApple OSS Distributions 		p->p_dispatchqueue_serialno_offset = (uint64_t)offset;
3469*2c2f96dcSApple OSS Distributions 		break;
3470*2c2f96dcSApple OSS Distributions 	}
3471*2c2f96dcSApple OSS Distributions 	case WQOPS_QUEUE_REQTHREADS: {
3472*2c2f96dcSApple OSS Distributions 		/*
3473*2c2f96dcSApple OSS Distributions 		 * arg2 = number of threads to start
3474*2c2f96dcSApple OSS Distributions 		 * arg3 = priority
3475*2c2f96dcSApple OSS Distributions 		 */
3476*2c2f96dcSApple OSS Distributions 		error = workq_reqthreads(p, arg2, arg3, false);
3477*2c2f96dcSApple OSS Distributions 		break;
3478*2c2f96dcSApple OSS Distributions 	}
3479*2c2f96dcSApple OSS Distributions 	/* For requesting threads for the cooperative pool */
3480*2c2f96dcSApple OSS Distributions 	case WQOPS_QUEUE_REQTHREADS2: {
3481*2c2f96dcSApple OSS Distributions 		/*
3482*2c2f96dcSApple OSS Distributions 		 * arg2 = number of threads to start
3483*2c2f96dcSApple OSS Distributions 		 * arg3 = priority
3484*2c2f96dcSApple OSS Distributions 		 */
3485*2c2f96dcSApple OSS Distributions 		error = workq_reqthreads(p, arg2, arg3, true);
3486*2c2f96dcSApple OSS Distributions 		break;
3487*2c2f96dcSApple OSS Distributions 	}
3488*2c2f96dcSApple OSS Distributions 	case WQOPS_SET_EVENT_MANAGER_PRIORITY: {
3489*2c2f96dcSApple OSS Distributions 		/*
3490*2c2f96dcSApple OSS Distributions 		 * arg2 = priority for the manager thread
3491*2c2f96dcSApple OSS Distributions 		 *
3492*2c2f96dcSApple OSS Distributions 		 * if _PTHREAD_PRIORITY_SCHED_PRI_FLAG is set,
3493*2c2f96dcSApple OSS Distributions 		 * the low bits of the value contains a scheduling priority
3494*2c2f96dcSApple OSS Distributions 		 * instead of a QOS value
3495*2c2f96dcSApple OSS Distributions 		 */
3496*2c2f96dcSApple OSS Distributions 		pthread_priority_t pri = arg2;
3497*2c2f96dcSApple OSS Distributions 
3498*2c2f96dcSApple OSS Distributions 		if (wq == NULL) {
3499*2c2f96dcSApple OSS Distributions 			error = EINVAL;
3500*2c2f96dcSApple OSS Distributions 			break;
3501*2c2f96dcSApple OSS Distributions 		}
3502*2c2f96dcSApple OSS Distributions 
3503*2c2f96dcSApple OSS Distributions 		/*
3504*2c2f96dcSApple OSS Distributions 		 * Normalize the incoming priority so that it is ordered numerically.
3505*2c2f96dcSApple OSS Distributions 		 */
3506*2c2f96dcSApple OSS Distributions 		if (_pthread_priority_has_sched_pri(pri)) {
3507*2c2f96dcSApple OSS Distributions 			pri &= (_PTHREAD_PRIORITY_SCHED_PRI_MASK |
3508*2c2f96dcSApple OSS Distributions 			    _PTHREAD_PRIORITY_SCHED_PRI_FLAG);
3509*2c2f96dcSApple OSS Distributions 		} else {
3510*2c2f96dcSApple OSS Distributions 			thread_qos_t qos = _pthread_priority_thread_qos(pri);
3511*2c2f96dcSApple OSS Distributions 			int relpri = _pthread_priority_relpri(pri);
3512*2c2f96dcSApple OSS Distributions 			if (relpri > 0 || relpri < THREAD_QOS_MIN_TIER_IMPORTANCE ||
3513*2c2f96dcSApple OSS Distributions 			    qos == THREAD_QOS_UNSPECIFIED) {
3514*2c2f96dcSApple OSS Distributions 				error = EINVAL;
3515*2c2f96dcSApple OSS Distributions 				break;
3516*2c2f96dcSApple OSS Distributions 			}
3517*2c2f96dcSApple OSS Distributions 			pri &= ~_PTHREAD_PRIORITY_FLAGS_MASK;
3518*2c2f96dcSApple OSS Distributions 		}
3519*2c2f96dcSApple OSS Distributions 
3520*2c2f96dcSApple OSS Distributions 		/*
3521*2c2f96dcSApple OSS Distributions 		 * If userspace passes a scheduling priority, that wins over any QoS.
3522*2c2f96dcSApple OSS Distributions 		 * Userspace should takes care not to lower the priority this way.
3523*2c2f96dcSApple OSS Distributions 		 */
3524*2c2f96dcSApple OSS Distributions 		workq_lock_spin(wq);
3525*2c2f96dcSApple OSS Distributions 		if (wq->wq_event_manager_priority < (uint32_t)pri) {
3526*2c2f96dcSApple OSS Distributions 			wq->wq_event_manager_priority = (uint32_t)pri;
3527*2c2f96dcSApple OSS Distributions 		}
3528*2c2f96dcSApple OSS Distributions 		workq_unlock(wq);
3529*2c2f96dcSApple OSS Distributions 		break;
3530*2c2f96dcSApple OSS Distributions 	}
3531*2c2f96dcSApple OSS Distributions 	case WQOPS_THREAD_KEVENT_RETURN:
3532*2c2f96dcSApple OSS Distributions 	case WQOPS_THREAD_WORKLOOP_RETURN:
3533*2c2f96dcSApple OSS Distributions 	case WQOPS_THREAD_RETURN: {
3534*2c2f96dcSApple OSS Distributions 		error = workq_thread_return(p, uap, wq);
3535*2c2f96dcSApple OSS Distributions 		break;
3536*2c2f96dcSApple OSS Distributions 	}
3537*2c2f96dcSApple OSS Distributions 
3538*2c2f96dcSApple OSS Distributions 	case WQOPS_SHOULD_NARROW: {
3539*2c2f96dcSApple OSS Distributions 		/*
3540*2c2f96dcSApple OSS Distributions 		 * arg2 = priority to test
3541*2c2f96dcSApple OSS Distributions 		 * arg3 = unused
3542*2c2f96dcSApple OSS Distributions 		 */
3543*2c2f96dcSApple OSS Distributions 		thread_t th = current_thread();
3544*2c2f96dcSApple OSS Distributions 		struct uthread *uth = get_bsdthread_info(th);
3545*2c2f96dcSApple OSS Distributions 		if (((thread_get_tag(th) & THREAD_TAG_WORKQUEUE) == 0) ||
3546*2c2f96dcSApple OSS Distributions 		    (uth->uu_workq_flags & (UT_WORKQ_DYING | UT_WORKQ_OVERCOMMIT))) {
3547*2c2f96dcSApple OSS Distributions 			error = EINVAL;
3548*2c2f96dcSApple OSS Distributions 			break;
3549*2c2f96dcSApple OSS Distributions 		}
3550*2c2f96dcSApple OSS Distributions 
3551*2c2f96dcSApple OSS Distributions 		thread_qos_t qos = _pthread_priority_thread_qos(arg2);
3552*2c2f96dcSApple OSS Distributions 		if (qos == THREAD_QOS_UNSPECIFIED) {
3553*2c2f96dcSApple OSS Distributions 			error = EINVAL;
3554*2c2f96dcSApple OSS Distributions 			break;
3555*2c2f96dcSApple OSS Distributions 		}
3556*2c2f96dcSApple OSS Distributions 		workq_lock_spin(wq);
3557*2c2f96dcSApple OSS Distributions 		bool should_narrow = !workq_constrained_allowance(wq, qos, uth, false);
3558*2c2f96dcSApple OSS Distributions 		workq_unlock(wq);
3559*2c2f96dcSApple OSS Distributions 
3560*2c2f96dcSApple OSS Distributions 		*retval = should_narrow;
3561*2c2f96dcSApple OSS Distributions 		break;
3562*2c2f96dcSApple OSS Distributions 	}
3563*2c2f96dcSApple OSS Distributions 	case WQOPS_SETUP_DISPATCH: {
3564*2c2f96dcSApple OSS Distributions 		/*
3565*2c2f96dcSApple OSS Distributions 		 * item = pointer to workq_dispatch_config structure
3566*2c2f96dcSApple OSS Distributions 		 * arg2 = sizeof(item)
3567*2c2f96dcSApple OSS Distributions 		 */
3568*2c2f96dcSApple OSS Distributions 		struct workq_dispatch_config cfg;
3569*2c2f96dcSApple OSS Distributions 		bzero(&cfg, sizeof(cfg));
3570*2c2f96dcSApple OSS Distributions 
3571*2c2f96dcSApple OSS Distributions 		error = copyin(uap->item, &cfg, MIN(sizeof(cfg), (unsigned long) arg2));
3572*2c2f96dcSApple OSS Distributions 		if (error) {
3573*2c2f96dcSApple OSS Distributions 			break;
3574*2c2f96dcSApple OSS Distributions 		}
3575*2c2f96dcSApple OSS Distributions 
3576*2c2f96dcSApple OSS Distributions 		if (cfg.wdc_flags & ~WORKQ_DISPATCH_SUPPORTED_FLAGS ||
3577*2c2f96dcSApple OSS Distributions 		    cfg.wdc_version < WORKQ_DISPATCH_MIN_SUPPORTED_VERSION) {
3578*2c2f96dcSApple OSS Distributions 			error = ENOTSUP;
3579*2c2f96dcSApple OSS Distributions 			break;
3580*2c2f96dcSApple OSS Distributions 		}
3581*2c2f96dcSApple OSS Distributions 
3582*2c2f96dcSApple OSS Distributions 		/* Load fields from version 1 */
3583*2c2f96dcSApple OSS Distributions 		p->p_dispatchqueue_serialno_offset = cfg.wdc_queue_serialno_offs;
3584*2c2f96dcSApple OSS Distributions 
3585*2c2f96dcSApple OSS Distributions 		/* Load fields from version 2 */
3586*2c2f96dcSApple OSS Distributions 		if (cfg.wdc_version >= 2) {
3587*2c2f96dcSApple OSS Distributions 			p->p_dispatchqueue_label_offset = cfg.wdc_queue_label_offs;
3588*2c2f96dcSApple OSS Distributions 		}
3589*2c2f96dcSApple OSS Distributions 
3590*2c2f96dcSApple OSS Distributions 		break;
3591*2c2f96dcSApple OSS Distributions 	}
3592*2c2f96dcSApple OSS Distributions 	default:
3593*2c2f96dcSApple OSS Distributions 		error = EINVAL;
3594*2c2f96dcSApple OSS Distributions 		break;
3595*2c2f96dcSApple OSS Distributions 	}
3596*2c2f96dcSApple OSS Distributions 
3597*2c2f96dcSApple OSS Distributions 	return error;
3598*2c2f96dcSApple OSS Distributions }
3599*2c2f96dcSApple OSS Distributions 
3600*2c2f96dcSApple OSS Distributions /*
3601*2c2f96dcSApple OSS Distributions  * We have no work to do, park ourselves on the idle list.
3602*2c2f96dcSApple OSS Distributions  *
3603*2c2f96dcSApple OSS Distributions  * Consumes the workqueue lock and does not return.
3604*2c2f96dcSApple OSS Distributions  */
3605*2c2f96dcSApple OSS Distributions __attribute__((noreturn, noinline))
3606*2c2f96dcSApple OSS Distributions static void
workq_park_and_unlock(proc_t p,struct workqueue * wq,struct uthread * uth,uint32_t setup_flags)3607*2c2f96dcSApple OSS Distributions workq_park_and_unlock(proc_t p, struct workqueue *wq, struct uthread *uth,
3608*2c2f96dcSApple OSS Distributions     uint32_t setup_flags)
3609*2c2f96dcSApple OSS Distributions {
3610*2c2f96dcSApple OSS Distributions 	assert(uth == current_uthread());
3611*2c2f96dcSApple OSS Distributions 	assert(uth->uu_kqr_bound == NULL);
3612*2c2f96dcSApple OSS Distributions 	workq_push_idle_thread(p, wq, uth, setup_flags); // may not return
3613*2c2f96dcSApple OSS Distributions 
3614*2c2f96dcSApple OSS Distributions 	workq_thread_reset_cpupercent(NULL, uth);
3615*2c2f96dcSApple OSS Distributions 
3616*2c2f96dcSApple OSS Distributions #if CONFIG_PREADOPT_TG
3617*2c2f96dcSApple OSS Distributions 	/* Clear the preadoption thread group on the thread.
3618*2c2f96dcSApple OSS Distributions 	 *
3619*2c2f96dcSApple OSS Distributions 	 * Case 1:
3620*2c2f96dcSApple OSS Distributions 	 *		Creator thread which never picked up a thread request. We set a
3621*2c2f96dcSApple OSS Distributions 	 *		preadoption thread group on creator threads but if it never picked
3622*2c2f96dcSApple OSS Distributions 	 *		up a thread request and didn't go to userspace, then the thread will
3623*2c2f96dcSApple OSS Distributions 	 *		park with a preadoption thread group but no explicitly adopted
3624*2c2f96dcSApple OSS Distributions 	 *		voucher or work interval.
3625*2c2f96dcSApple OSS Distributions 	 *
3626*2c2f96dcSApple OSS Distributions 	 *		We drop the preadoption thread group here before proceeding to park.
3627*2c2f96dcSApple OSS Distributions 	 *		Note - we may get preempted when we drop the workq lock below.
3628*2c2f96dcSApple OSS Distributions 	 *
3629*2c2f96dcSApple OSS Distributions 	 * Case 2:
3630*2c2f96dcSApple OSS Distributions 	 *		Thread picked up a thread request and bound to it and returned back
3631*2c2f96dcSApple OSS Distributions 	 *		from userspace and is parking. At this point, preadoption thread
3632*2c2f96dcSApple OSS Distributions 	 *		group should be NULL since the thread has unbound from the thread
3633*2c2f96dcSApple OSS Distributions 	 *		request. So this operation should be a no-op.
3634*2c2f96dcSApple OSS Distributions 	 */
3635*2c2f96dcSApple OSS Distributions 	thread_set_preadopt_thread_group(get_machthread(uth), NULL);
3636*2c2f96dcSApple OSS Distributions #endif
3637*2c2f96dcSApple OSS Distributions 
3638*2c2f96dcSApple OSS Distributions 	if ((uth->uu_workq_flags & UT_WORKQ_IDLE_CLEANUP) &&
3639*2c2f96dcSApple OSS Distributions 	    !(uth->uu_workq_flags & UT_WORKQ_DYING)) {
3640*2c2f96dcSApple OSS Distributions 		workq_unlock(wq);
3641*2c2f96dcSApple OSS Distributions 
3642*2c2f96dcSApple OSS Distributions 		/*
3643*2c2f96dcSApple OSS Distributions 		 * workq_push_idle_thread() will unset `has_stack`
3644*2c2f96dcSApple OSS Distributions 		 * if it wants us to free the stack before parking.
3645*2c2f96dcSApple OSS Distributions 		 */
3646*2c2f96dcSApple OSS Distributions 		if (!uth->uu_save.uus_workq_park_data.has_stack) {
3647*2c2f96dcSApple OSS Distributions 			pthread_functions->workq_markfree_threadstack(p,
3648*2c2f96dcSApple OSS Distributions 			    get_machthread(uth), get_task_map(proc_task(p)),
3649*2c2f96dcSApple OSS Distributions 			    uth->uu_workq_stackaddr);
3650*2c2f96dcSApple OSS Distributions 		}
3651*2c2f96dcSApple OSS Distributions 
3652*2c2f96dcSApple OSS Distributions 		/*
3653*2c2f96dcSApple OSS Distributions 		 * When we remove the voucher from the thread, we may lose our importance
3654*2c2f96dcSApple OSS Distributions 		 * causing us to get preempted, so we do this after putting the thread on
3655*2c2f96dcSApple OSS Distributions 		 * the idle list.  Then, when we get our importance back we'll be able to
3656*2c2f96dcSApple OSS Distributions 		 * use this thread from e.g. the kevent call out to deliver a boosting
3657*2c2f96dcSApple OSS Distributions 		 * message.
3658*2c2f96dcSApple OSS Distributions 		 *
3659*2c2f96dcSApple OSS Distributions 		 * Note that setting the voucher to NULL will not clear the preadoption
3660*2c2f96dcSApple OSS Distributions 		 * thread since this thread could have become the creator again and
3661*2c2f96dcSApple OSS Distributions 		 * perhaps acquired a preadoption thread group.
3662*2c2f96dcSApple OSS Distributions 		 */
3663*2c2f96dcSApple OSS Distributions 		__assert_only kern_return_t kr;
3664*2c2f96dcSApple OSS Distributions 		kr = thread_set_voucher_name(MACH_PORT_NULL);
3665*2c2f96dcSApple OSS Distributions 		assert(kr == KERN_SUCCESS);
3666*2c2f96dcSApple OSS Distributions 
3667*2c2f96dcSApple OSS Distributions 		workq_lock_spin(wq);
3668*2c2f96dcSApple OSS Distributions 		uth->uu_workq_flags &= ~UT_WORKQ_IDLE_CLEANUP;
3669*2c2f96dcSApple OSS Distributions 		setup_flags &= ~WQ_SETUP_CLEAR_VOUCHER;
3670*2c2f96dcSApple OSS Distributions 	}
3671*2c2f96dcSApple OSS Distributions 
3672*2c2f96dcSApple OSS Distributions 	WQ_TRACE_WQ(TRACE_wq_thread_logical_run | DBG_FUNC_END, wq, 0, 0, 0);
3673*2c2f96dcSApple OSS Distributions 
3674*2c2f96dcSApple OSS Distributions 	if (uth->uu_workq_flags & UT_WORKQ_RUNNING) {
3675*2c2f96dcSApple OSS Distributions 		/*
3676*2c2f96dcSApple OSS Distributions 		 * While we'd dropped the lock to unset our voucher, someone came
3677*2c2f96dcSApple OSS Distributions 		 * around and made us runnable.  But because we weren't waiting on the
3678*2c2f96dcSApple OSS Distributions 		 * event their thread_wakeup() was ineffectual.  To correct for that,
3679*2c2f96dcSApple OSS Distributions 		 * we just run the continuation ourselves.
3680*2c2f96dcSApple OSS Distributions 		 */
3681*2c2f96dcSApple OSS Distributions 		workq_unpark_select_threadreq_or_park_and_unlock(p, wq, uth, setup_flags);
3682*2c2f96dcSApple OSS Distributions 		__builtin_unreachable();
3683*2c2f96dcSApple OSS Distributions 	}
3684*2c2f96dcSApple OSS Distributions 
3685*2c2f96dcSApple OSS Distributions 	if (uth->uu_workq_flags & UT_WORKQ_DYING) {
3686*2c2f96dcSApple OSS Distributions 		workq_unpark_for_death_and_unlock(p, wq, uth,
3687*2c2f96dcSApple OSS Distributions 		    WORKQ_UNPARK_FOR_DEATH_WAS_IDLE, setup_flags);
3688*2c2f96dcSApple OSS Distributions 		__builtin_unreachable();
3689*2c2f96dcSApple OSS Distributions 	}
3690*2c2f96dcSApple OSS Distributions 
3691*2c2f96dcSApple OSS Distributions 	/* Disarm the workqueue quantum since the thread is now idle */
3692*2c2f96dcSApple OSS Distributions 	thread_disarm_workqueue_quantum(get_machthread(uth));
3693*2c2f96dcSApple OSS Distributions 
3694*2c2f96dcSApple OSS Distributions 	thread_set_pending_block_hint(get_machthread(uth), kThreadWaitParkedWorkQueue);
3695*2c2f96dcSApple OSS Distributions 	assert_wait(workq_parked_wait_event(uth), THREAD_INTERRUPTIBLE);
3696*2c2f96dcSApple OSS Distributions 	workq_unlock(wq);
3697*2c2f96dcSApple OSS Distributions 	thread_block(workq_unpark_continue);
3698*2c2f96dcSApple OSS Distributions 	__builtin_unreachable();
3699*2c2f96dcSApple OSS Distributions }
3700*2c2f96dcSApple OSS Distributions 
3701*2c2f96dcSApple OSS Distributions static inline bool
workq_may_start_event_mgr_thread(struct workqueue * wq,struct uthread * uth)3702*2c2f96dcSApple OSS Distributions workq_may_start_event_mgr_thread(struct workqueue *wq, struct uthread *uth)
3703*2c2f96dcSApple OSS Distributions {
3704*2c2f96dcSApple OSS Distributions 	/*
3705*2c2f96dcSApple OSS Distributions 	 * There's an event manager request and either:
3706*2c2f96dcSApple OSS Distributions 	 * - no event manager currently running
3707*2c2f96dcSApple OSS Distributions 	 * - we are re-using the event manager
3708*2c2f96dcSApple OSS Distributions 	 */
3709*2c2f96dcSApple OSS Distributions 	return wq->wq_thscheduled_count[_wq_bucket(WORKQ_THREAD_QOS_MANAGER)] == 0 ||
3710*2c2f96dcSApple OSS Distributions 	       (uth && uth->uu_workq_pri.qos_bucket == WORKQ_THREAD_QOS_MANAGER);
3711*2c2f96dcSApple OSS Distributions }
3712*2c2f96dcSApple OSS Distributions 
3713*2c2f96dcSApple OSS Distributions static uint32_t
workq_constrained_allowance(struct workqueue * wq,thread_qos_t at_qos,struct uthread * uth,bool may_start_timer)3714*2c2f96dcSApple OSS Distributions workq_constrained_allowance(struct workqueue *wq, thread_qos_t at_qos,
3715*2c2f96dcSApple OSS Distributions     struct uthread *uth, bool may_start_timer)
3716*2c2f96dcSApple OSS Distributions {
3717*2c2f96dcSApple OSS Distributions 	assert(at_qos != WORKQ_THREAD_QOS_MANAGER);
3718*2c2f96dcSApple OSS Distributions 	uint32_t count = 0;
3719*2c2f96dcSApple OSS Distributions 
3720*2c2f96dcSApple OSS Distributions 	uint32_t max_count = wq->wq_constrained_threads_scheduled;
3721*2c2f96dcSApple OSS Distributions 	if (uth && workq_thread_is_nonovercommit(uth)) {
3722*2c2f96dcSApple OSS Distributions 		/*
3723*2c2f96dcSApple OSS Distributions 		 * don't count the current thread as scheduled
3724*2c2f96dcSApple OSS Distributions 		 */
3725*2c2f96dcSApple OSS Distributions 		assert(max_count > 0);
3726*2c2f96dcSApple OSS Distributions 		max_count--;
3727*2c2f96dcSApple OSS Distributions 	}
3728*2c2f96dcSApple OSS Distributions 	if (max_count >= wq_max_constrained_threads) {
3729*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_constrained_admission | DBG_FUNC_NONE, wq, 1,
3730*2c2f96dcSApple OSS Distributions 		    wq->wq_constrained_threads_scheduled,
3731*2c2f96dcSApple OSS Distributions 		    wq_max_constrained_threads);
3732*2c2f96dcSApple OSS Distributions 		/*
3733*2c2f96dcSApple OSS Distributions 		 * we need 1 or more constrained threads to return to the kernel before
3734*2c2f96dcSApple OSS Distributions 		 * we can dispatch additional work
3735*2c2f96dcSApple OSS Distributions 		 */
3736*2c2f96dcSApple OSS Distributions 		return 0;
3737*2c2f96dcSApple OSS Distributions 	}
3738*2c2f96dcSApple OSS Distributions 	max_count -= wq_max_constrained_threads;
3739*2c2f96dcSApple OSS Distributions 
3740*2c2f96dcSApple OSS Distributions 	/*
3741*2c2f96dcSApple OSS Distributions 	 * Compute a metric for many how many threads are active.  We find the
3742*2c2f96dcSApple OSS Distributions 	 * highest priority request outstanding and then add up the number of active
3743*2c2f96dcSApple OSS Distributions 	 * threads in that and all higher-priority buckets.  We'll also add any
3744*2c2f96dcSApple OSS Distributions 	 * "busy" threads which are not currently active but blocked recently enough
3745*2c2f96dcSApple OSS Distributions 	 * that we can't be sure that they won't be unblocked soon and start
3746*2c2f96dcSApple OSS Distributions 	 * being active again.
3747*2c2f96dcSApple OSS Distributions 	 *
3748*2c2f96dcSApple OSS Distributions 	 * We'll then compare this metric to our max concurrency to decide whether
3749*2c2f96dcSApple OSS Distributions 	 * to add a new thread.
3750*2c2f96dcSApple OSS Distributions 	 */
3751*2c2f96dcSApple OSS Distributions 
3752*2c2f96dcSApple OSS Distributions 	uint32_t busycount, thactive_count;
3753*2c2f96dcSApple OSS Distributions 
3754*2c2f96dcSApple OSS Distributions 	thactive_count = _wq_thactive_aggregate_downto_qos(wq, _wq_thactive(wq),
3755*2c2f96dcSApple OSS Distributions 	    at_qos, &busycount, NULL);
3756*2c2f96dcSApple OSS Distributions 
3757*2c2f96dcSApple OSS Distributions 	if (uth && uth->uu_workq_pri.qos_bucket != WORKQ_THREAD_QOS_MANAGER &&
3758*2c2f96dcSApple OSS Distributions 	    at_qos <= uth->uu_workq_pri.qos_bucket) {
3759*2c2f96dcSApple OSS Distributions 		/*
3760*2c2f96dcSApple OSS Distributions 		 * Don't count this thread as currently active, but only if it's not
3761*2c2f96dcSApple OSS Distributions 		 * a manager thread, as _wq_thactive_aggregate_downto_qos ignores active
3762*2c2f96dcSApple OSS Distributions 		 * managers.
3763*2c2f96dcSApple OSS Distributions 		 */
3764*2c2f96dcSApple OSS Distributions 		assert(thactive_count > 0);
3765*2c2f96dcSApple OSS Distributions 		thactive_count--;
3766*2c2f96dcSApple OSS Distributions 	}
3767*2c2f96dcSApple OSS Distributions 
3768*2c2f96dcSApple OSS Distributions 	count = wq_max_parallelism[_wq_bucket(at_qos)];
3769*2c2f96dcSApple OSS Distributions 	if (count > thactive_count + busycount) {
3770*2c2f96dcSApple OSS Distributions 		count -= thactive_count + busycount;
3771*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_constrained_admission | DBG_FUNC_NONE, wq, 2,
3772*2c2f96dcSApple OSS Distributions 		    thactive_count, busycount);
3773*2c2f96dcSApple OSS Distributions 		return MIN(count, max_count);
3774*2c2f96dcSApple OSS Distributions 	} else {
3775*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_constrained_admission | DBG_FUNC_NONE, wq, 3,
3776*2c2f96dcSApple OSS Distributions 		    thactive_count, busycount);
3777*2c2f96dcSApple OSS Distributions 	}
3778*2c2f96dcSApple OSS Distributions 
3779*2c2f96dcSApple OSS Distributions 	if (may_start_timer) {
3780*2c2f96dcSApple OSS Distributions 		/*
3781*2c2f96dcSApple OSS Distributions 		 * If this is called from the add timer, we won't have another timer
3782*2c2f96dcSApple OSS Distributions 		 * fire when the thread exits the "busy" state, so rearm the timer.
3783*2c2f96dcSApple OSS Distributions 		 */
3784*2c2f96dcSApple OSS Distributions 		workq_schedule_delayed_thread_creation(wq, 0);
3785*2c2f96dcSApple OSS Distributions 	}
3786*2c2f96dcSApple OSS Distributions 
3787*2c2f96dcSApple OSS Distributions 	return 0;
3788*2c2f96dcSApple OSS Distributions }
3789*2c2f96dcSApple OSS Distributions 
3790*2c2f96dcSApple OSS Distributions static bool
workq_threadreq_admissible(struct workqueue * wq,struct uthread * uth,workq_threadreq_t req)3791*2c2f96dcSApple OSS Distributions workq_threadreq_admissible(struct workqueue *wq, struct uthread *uth,
3792*2c2f96dcSApple OSS Distributions     workq_threadreq_t req)
3793*2c2f96dcSApple OSS Distributions {
3794*2c2f96dcSApple OSS Distributions 	if (req->tr_qos == WORKQ_THREAD_QOS_MANAGER) {
3795*2c2f96dcSApple OSS Distributions 		return workq_may_start_event_mgr_thread(wq, uth);
3796*2c2f96dcSApple OSS Distributions 	}
3797*2c2f96dcSApple OSS Distributions 	if (workq_threadreq_is_cooperative(req)) {
3798*2c2f96dcSApple OSS Distributions 		return workq_cooperative_allowance(wq, req->tr_qos, uth, true);
3799*2c2f96dcSApple OSS Distributions 	}
3800*2c2f96dcSApple OSS Distributions 	if (workq_threadreq_is_nonovercommit(req)) {
3801*2c2f96dcSApple OSS Distributions 		return workq_constrained_allowance(wq, req->tr_qos, uth, true);
3802*2c2f96dcSApple OSS Distributions 	}
3803*2c2f96dcSApple OSS Distributions 
3804*2c2f96dcSApple OSS Distributions 	return true;
3805*2c2f96dcSApple OSS Distributions }
3806*2c2f96dcSApple OSS Distributions 
3807*2c2f96dcSApple OSS Distributions /*
3808*2c2f96dcSApple OSS Distributions  * Called from the context of selecting thread requests for threads returning
3809*2c2f96dcSApple OSS Distributions  * from userspace or creator thread
3810*2c2f96dcSApple OSS Distributions  */
3811*2c2f96dcSApple OSS Distributions static workq_threadreq_t
workq_cooperative_queue_best_req(struct workqueue * wq,struct uthread * uth)3812*2c2f96dcSApple OSS Distributions workq_cooperative_queue_best_req(struct workqueue *wq, struct uthread *uth)
3813*2c2f96dcSApple OSS Distributions {
3814*2c2f96dcSApple OSS Distributions 	workq_lock_held(wq);
3815*2c2f96dcSApple OSS Distributions 
3816*2c2f96dcSApple OSS Distributions 	/*
3817*2c2f96dcSApple OSS Distributions 	 * If the current thread is cooperative, we need to exclude it as part of
3818*2c2f96dcSApple OSS Distributions 	 * cooperative schedule count since this thread is looking for a new
3819*2c2f96dcSApple OSS Distributions 	 * request. Change in the schedule count for cooperative pool therefore
3820*2c2f96dcSApple OSS Distributions 	 * requires us to reeevaluate the next best request for it.
3821*2c2f96dcSApple OSS Distributions 	 */
3822*2c2f96dcSApple OSS Distributions 	if (uth && workq_thread_is_cooperative(uth)) {
3823*2c2f96dcSApple OSS Distributions 		_wq_cooperative_queue_scheduled_count_dec(wq, uth->uu_workq_pri.qos_req);
3824*2c2f96dcSApple OSS Distributions 
3825*2c2f96dcSApple OSS Distributions 		(void) _wq_cooperative_queue_refresh_best_req_qos(wq);
3826*2c2f96dcSApple OSS Distributions 
3827*2c2f96dcSApple OSS Distributions 		_wq_cooperative_queue_scheduled_count_inc(wq, uth->uu_workq_pri.qos_req);
3828*2c2f96dcSApple OSS Distributions 	} else {
3829*2c2f96dcSApple OSS Distributions 		/*
3830*2c2f96dcSApple OSS Distributions 		 * The old value that was already precomputed should be safe to use -
3831*2c2f96dcSApple OSS Distributions 		 * add an assert that asserts that the best req QoS doesn't change in
3832*2c2f96dcSApple OSS Distributions 		 * this case
3833*2c2f96dcSApple OSS Distributions 		 */
3834*2c2f96dcSApple OSS Distributions 		assert(_wq_cooperative_queue_refresh_best_req_qos(wq) == false);
3835*2c2f96dcSApple OSS Distributions 	}
3836*2c2f96dcSApple OSS Distributions 
3837*2c2f96dcSApple OSS Distributions 	thread_qos_t qos = wq->wq_cooperative_queue_best_req_qos;
3838*2c2f96dcSApple OSS Distributions 
3839*2c2f96dcSApple OSS Distributions 	/* There are no eligible requests in the cooperative pool */
3840*2c2f96dcSApple OSS Distributions 	if (qos == THREAD_QOS_UNSPECIFIED) {
3841*2c2f96dcSApple OSS Distributions 		return NULL;
3842*2c2f96dcSApple OSS Distributions 	}
3843*2c2f96dcSApple OSS Distributions 	assert(qos != WORKQ_THREAD_QOS_ABOVEUI);
3844*2c2f96dcSApple OSS Distributions 	assert(qos != WORKQ_THREAD_QOS_MANAGER);
3845*2c2f96dcSApple OSS Distributions 
3846*2c2f96dcSApple OSS Distributions 	uint8_t bucket = _wq_bucket(qos);
3847*2c2f96dcSApple OSS Distributions 	assert(!STAILQ_EMPTY(&wq->wq_cooperative_queue[bucket]));
3848*2c2f96dcSApple OSS Distributions 
3849*2c2f96dcSApple OSS Distributions 	return STAILQ_FIRST(&wq->wq_cooperative_queue[bucket]);
3850*2c2f96dcSApple OSS Distributions }
3851*2c2f96dcSApple OSS Distributions 
3852*2c2f96dcSApple OSS Distributions static workq_threadreq_t
workq_threadreq_select_for_creator(struct workqueue * wq)3853*2c2f96dcSApple OSS Distributions workq_threadreq_select_for_creator(struct workqueue *wq)
3854*2c2f96dcSApple OSS Distributions {
3855*2c2f96dcSApple OSS Distributions 	workq_threadreq_t req_qos, req_pri, req_tmp, req_mgr;
3856*2c2f96dcSApple OSS Distributions 	thread_qos_t qos = THREAD_QOS_UNSPECIFIED;
3857*2c2f96dcSApple OSS Distributions 	uint8_t pri = 0;
3858*2c2f96dcSApple OSS Distributions 
3859*2c2f96dcSApple OSS Distributions 	/*
3860*2c2f96dcSApple OSS Distributions 	 * Compute the best priority request, and ignore the turnstile for now
3861*2c2f96dcSApple OSS Distributions 	 */
3862*2c2f96dcSApple OSS Distributions 
3863*2c2f96dcSApple OSS Distributions 	req_pri = priority_queue_max(&wq->wq_special_queue,
3864*2c2f96dcSApple OSS Distributions 	    struct workq_threadreq_s, tr_entry);
3865*2c2f96dcSApple OSS Distributions 	if (req_pri) {
3866*2c2f96dcSApple OSS Distributions 		pri = (uint8_t)priority_queue_entry_sched_pri(&wq->wq_special_queue,
3867*2c2f96dcSApple OSS Distributions 		    &req_pri->tr_entry);
3868*2c2f96dcSApple OSS Distributions 	}
3869*2c2f96dcSApple OSS Distributions 
3870*2c2f96dcSApple OSS Distributions 	/*
3871*2c2f96dcSApple OSS Distributions 	 * Handle the manager thread request. The special queue might yield
3872*2c2f96dcSApple OSS Distributions 	 * a higher priority, but the manager always beats the QoS world.
3873*2c2f96dcSApple OSS Distributions 	 */
3874*2c2f96dcSApple OSS Distributions 
3875*2c2f96dcSApple OSS Distributions 	req_mgr = wq->wq_event_manager_threadreq;
3876*2c2f96dcSApple OSS Distributions 	if (req_mgr && workq_may_start_event_mgr_thread(wq, NULL)) {
3877*2c2f96dcSApple OSS Distributions 		uint32_t mgr_pri = wq->wq_event_manager_priority;
3878*2c2f96dcSApple OSS Distributions 
3879*2c2f96dcSApple OSS Distributions 		if (mgr_pri & _PTHREAD_PRIORITY_SCHED_PRI_FLAG) {
3880*2c2f96dcSApple OSS Distributions 			mgr_pri &= _PTHREAD_PRIORITY_SCHED_PRI_MASK;
3881*2c2f96dcSApple OSS Distributions 		} else {
3882*2c2f96dcSApple OSS Distributions 			mgr_pri = thread_workq_pri_for_qos(
3883*2c2f96dcSApple OSS Distributions 				_pthread_priority_thread_qos(mgr_pri));
3884*2c2f96dcSApple OSS Distributions 		}
3885*2c2f96dcSApple OSS Distributions 
3886*2c2f96dcSApple OSS Distributions 		return mgr_pri >= pri ? req_mgr : req_pri;
3887*2c2f96dcSApple OSS Distributions 	}
3888*2c2f96dcSApple OSS Distributions 
3889*2c2f96dcSApple OSS Distributions 	/*
3890*2c2f96dcSApple OSS Distributions 	 * Compute the best QoS Request, and check whether it beats the "pri" one
3891*2c2f96dcSApple OSS Distributions 	 *
3892*2c2f96dcSApple OSS Distributions 	 * Start by comparing the overcommit and the cooperative pool
3893*2c2f96dcSApple OSS Distributions 	 */
3894*2c2f96dcSApple OSS Distributions 	req_qos = priority_queue_max(&wq->wq_overcommit_queue,
3895*2c2f96dcSApple OSS Distributions 	    struct workq_threadreq_s, tr_entry);
3896*2c2f96dcSApple OSS Distributions 	if (req_qos) {
3897*2c2f96dcSApple OSS Distributions 		qos = req_qos->tr_qos;
3898*2c2f96dcSApple OSS Distributions 	}
3899*2c2f96dcSApple OSS Distributions 
3900*2c2f96dcSApple OSS Distributions 	req_tmp = workq_cooperative_queue_best_req(wq, NULL);
3901*2c2f96dcSApple OSS Distributions 	if (req_tmp && qos <= req_tmp->tr_qos) {
3902*2c2f96dcSApple OSS Distributions 		/*
3903*2c2f96dcSApple OSS Distributions 		 * Cooperative TR is better between overcommit and cooperative.  Note
3904*2c2f96dcSApple OSS Distributions 		 * that if qos is same between overcommit and cooperative, we choose
3905*2c2f96dcSApple OSS Distributions 		 * cooperative.
3906*2c2f96dcSApple OSS Distributions 		 *
3907*2c2f96dcSApple OSS Distributions 		 * Pick cooperative pool if it passes the admissions check
3908*2c2f96dcSApple OSS Distributions 		 */
3909*2c2f96dcSApple OSS Distributions 		if (workq_cooperative_allowance(wq, req_tmp->tr_qos, NULL, true)) {
3910*2c2f96dcSApple OSS Distributions 			req_qos = req_tmp;
3911*2c2f96dcSApple OSS Distributions 			qos = req_qos->tr_qos;
3912*2c2f96dcSApple OSS Distributions 		}
3913*2c2f96dcSApple OSS Distributions 	}
3914*2c2f96dcSApple OSS Distributions 
3915*2c2f96dcSApple OSS Distributions 	/*
3916*2c2f96dcSApple OSS Distributions 	 * Compare the best QoS so far - either from overcommit or from cooperative
3917*2c2f96dcSApple OSS Distributions 	 * pool - and compare it with the constrained pool
3918*2c2f96dcSApple OSS Distributions 	 */
3919*2c2f96dcSApple OSS Distributions 	req_tmp = priority_queue_max(&wq->wq_constrained_queue,
3920*2c2f96dcSApple OSS Distributions 	    struct workq_threadreq_s, tr_entry);
3921*2c2f96dcSApple OSS Distributions 
3922*2c2f96dcSApple OSS Distributions 	if (req_tmp && qos < req_tmp->tr_qos) {
3923*2c2f96dcSApple OSS Distributions 		/*
3924*2c2f96dcSApple OSS Distributions 		 * Constrained pool is best in QoS between overcommit, cooperative
3925*2c2f96dcSApple OSS Distributions 		 * and constrained. Now check how it fairs against the priority case
3926*2c2f96dcSApple OSS Distributions 		 */
3927*2c2f96dcSApple OSS Distributions 		if (pri && pri >= thread_workq_pri_for_qos(req_tmp->tr_qos)) {
3928*2c2f96dcSApple OSS Distributions 			return req_pri;
3929*2c2f96dcSApple OSS Distributions 		}
3930*2c2f96dcSApple OSS Distributions 
3931*2c2f96dcSApple OSS Distributions 		if (workq_constrained_allowance(wq, req_tmp->tr_qos, NULL, true)) {
3932*2c2f96dcSApple OSS Distributions 			/*
3933*2c2f96dcSApple OSS Distributions 			 * If the constrained thread request is the best one and passes
3934*2c2f96dcSApple OSS Distributions 			 * the admission check, pick it.
3935*2c2f96dcSApple OSS Distributions 			 */
3936*2c2f96dcSApple OSS Distributions 			return req_tmp;
3937*2c2f96dcSApple OSS Distributions 		}
3938*2c2f96dcSApple OSS Distributions 	}
3939*2c2f96dcSApple OSS Distributions 
3940*2c2f96dcSApple OSS Distributions 	/*
3941*2c2f96dcSApple OSS Distributions 	 * Compare the best of the QoS world with the priority
3942*2c2f96dcSApple OSS Distributions 	 */
3943*2c2f96dcSApple OSS Distributions 	if (pri && (!qos || pri >= thread_workq_pri_for_qos(qos))) {
3944*2c2f96dcSApple OSS Distributions 		return req_pri;
3945*2c2f96dcSApple OSS Distributions 	}
3946*2c2f96dcSApple OSS Distributions 
3947*2c2f96dcSApple OSS Distributions 	if (req_qos) {
3948*2c2f96dcSApple OSS Distributions 		return req_qos;
3949*2c2f96dcSApple OSS Distributions 	}
3950*2c2f96dcSApple OSS Distributions 
3951*2c2f96dcSApple OSS Distributions 	/*
3952*2c2f96dcSApple OSS Distributions 	 * If we had no eligible request but we have a turnstile push,
3953*2c2f96dcSApple OSS Distributions 	 * it must be a non overcommit thread request that failed
3954*2c2f96dcSApple OSS Distributions 	 * the admission check.
3955*2c2f96dcSApple OSS Distributions 	 *
3956*2c2f96dcSApple OSS Distributions 	 * Just fake a BG thread request so that if the push stops the creator
3957*2c2f96dcSApple OSS Distributions 	 * priority just drops to 4.
3958*2c2f96dcSApple OSS Distributions 	 */
3959*2c2f96dcSApple OSS Distributions 	if (turnstile_workq_proprietor_of_max_turnstile(wq->wq_turnstile, NULL)) {
3960*2c2f96dcSApple OSS Distributions 		static struct workq_threadreq_s workq_sync_push_fake_req = {
3961*2c2f96dcSApple OSS Distributions 			.tr_qos = THREAD_QOS_BACKGROUND,
3962*2c2f96dcSApple OSS Distributions 		};
3963*2c2f96dcSApple OSS Distributions 
3964*2c2f96dcSApple OSS Distributions 		return &workq_sync_push_fake_req;
3965*2c2f96dcSApple OSS Distributions 	}
3966*2c2f96dcSApple OSS Distributions 
3967*2c2f96dcSApple OSS Distributions 	return NULL;
3968*2c2f96dcSApple OSS Distributions }
3969*2c2f96dcSApple OSS Distributions 
3970*2c2f96dcSApple OSS Distributions /*
3971*2c2f96dcSApple OSS Distributions  * Returns true if this caused a change in the schedule counts of the
3972*2c2f96dcSApple OSS Distributions  * cooperative pool
3973*2c2f96dcSApple OSS Distributions  */
3974*2c2f96dcSApple OSS Distributions static bool
workq_adjust_cooperative_constrained_schedule_counts(struct workqueue * wq,struct uthread * uth,thread_qos_t old_thread_qos,workq_tr_flags_t tr_flags)3975*2c2f96dcSApple OSS Distributions workq_adjust_cooperative_constrained_schedule_counts(struct workqueue *wq,
3976*2c2f96dcSApple OSS Distributions     struct uthread *uth, thread_qos_t old_thread_qos, workq_tr_flags_t tr_flags)
3977*2c2f96dcSApple OSS Distributions {
3978*2c2f96dcSApple OSS Distributions 	workq_lock_held(wq);
3979*2c2f96dcSApple OSS Distributions 
3980*2c2f96dcSApple OSS Distributions 	/*
3981*2c2f96dcSApple OSS Distributions 	 * Row: thread type
3982*2c2f96dcSApple OSS Distributions 	 * Column: Request type
3983*2c2f96dcSApple OSS Distributions 	 *
3984*2c2f96dcSApple OSS Distributions 	 *					overcommit		non-overcommit		cooperative
3985*2c2f96dcSApple OSS Distributions 	 * overcommit			X				case 1				case 2
3986*2c2f96dcSApple OSS Distributions 	 * cooperative		case 3				case 4				case 5
3987*2c2f96dcSApple OSS Distributions 	 * non-overcommit	case 6					X				case 7
3988*2c2f96dcSApple OSS Distributions 	 *
3989*2c2f96dcSApple OSS Distributions 	 * Move the thread to the right bucket depending on what state it currently
3990*2c2f96dcSApple OSS Distributions 	 * has and what state the thread req it picks, is going to have.
3991*2c2f96dcSApple OSS Distributions 	 *
3992*2c2f96dcSApple OSS Distributions 	 * Note that the creator thread is an overcommit thread.
3993*2c2f96dcSApple OSS Distributions 	 */
3994*2c2f96dcSApple OSS Distributions 	thread_qos_t new_thread_qos = uth->uu_workq_pri.qos_req;
3995*2c2f96dcSApple OSS Distributions 
3996*2c2f96dcSApple OSS Distributions 	/*
3997*2c2f96dcSApple OSS Distributions 	 * Anytime a cooperative bucket's schedule count changes, we need to
3998*2c2f96dcSApple OSS Distributions 	 * potentially refresh the next best QoS for that pool when we determine
3999*2c2f96dcSApple OSS Distributions 	 * the next request for the creator
4000*2c2f96dcSApple OSS Distributions 	 */
4001*2c2f96dcSApple OSS Distributions 	bool cooperative_pool_sched_count_changed = false;
4002*2c2f96dcSApple OSS Distributions 
4003*2c2f96dcSApple OSS Distributions 	if (workq_thread_is_overcommit(uth)) {
4004*2c2f96dcSApple OSS Distributions 		if (workq_tr_is_nonovercommit(tr_flags)) {
4005*2c2f96dcSApple OSS Distributions 			// Case 1: thread is overcommit, req is non-overcommit
4006*2c2f96dcSApple OSS Distributions 			wq->wq_constrained_threads_scheduled++;
4007*2c2f96dcSApple OSS Distributions 		} else if (workq_tr_is_cooperative(tr_flags)) {
4008*2c2f96dcSApple OSS Distributions 			// Case 2: thread is overcommit, req is cooperative
4009*2c2f96dcSApple OSS Distributions 			_wq_cooperative_queue_scheduled_count_inc(wq, new_thread_qos);
4010*2c2f96dcSApple OSS Distributions 			cooperative_pool_sched_count_changed = true;
4011*2c2f96dcSApple OSS Distributions 		}
4012*2c2f96dcSApple OSS Distributions 	} else if (workq_thread_is_cooperative(uth)) {
4013*2c2f96dcSApple OSS Distributions 		if (workq_tr_is_overcommit(tr_flags)) {
4014*2c2f96dcSApple OSS Distributions 			// Case 3: thread is cooperative, req is overcommit
4015*2c2f96dcSApple OSS Distributions 			_wq_cooperative_queue_scheduled_count_dec(wq, old_thread_qos);
4016*2c2f96dcSApple OSS Distributions 		} else if (workq_tr_is_nonovercommit(tr_flags)) {
4017*2c2f96dcSApple OSS Distributions 			// Case 4: thread is cooperative, req is non-overcommit
4018*2c2f96dcSApple OSS Distributions 			_wq_cooperative_queue_scheduled_count_dec(wq, old_thread_qos);
4019*2c2f96dcSApple OSS Distributions 			wq->wq_constrained_threads_scheduled++;
4020*2c2f96dcSApple OSS Distributions 		} else {
4021*2c2f96dcSApple OSS Distributions 			// Case 5: thread is cooperative, req is also cooperative
4022*2c2f96dcSApple OSS Distributions 			assert(workq_tr_is_cooperative(tr_flags));
4023*2c2f96dcSApple OSS Distributions 			_wq_cooperative_queue_scheduled_count_dec(wq, old_thread_qos);
4024*2c2f96dcSApple OSS Distributions 			_wq_cooperative_queue_scheduled_count_inc(wq, new_thread_qos);
4025*2c2f96dcSApple OSS Distributions 		}
4026*2c2f96dcSApple OSS Distributions 		cooperative_pool_sched_count_changed = true;
4027*2c2f96dcSApple OSS Distributions 	} else {
4028*2c2f96dcSApple OSS Distributions 		if (workq_tr_is_overcommit(tr_flags)) {
4029*2c2f96dcSApple OSS Distributions 			// Case 6: Thread is non-overcommit, req is overcommit
4030*2c2f96dcSApple OSS Distributions 			wq->wq_constrained_threads_scheduled--;
4031*2c2f96dcSApple OSS Distributions 		} else if (workq_tr_is_cooperative(tr_flags)) {
4032*2c2f96dcSApple OSS Distributions 			// Case 7: Thread is non-overcommit, req is cooperative
4033*2c2f96dcSApple OSS Distributions 			wq->wq_constrained_threads_scheduled--;
4034*2c2f96dcSApple OSS Distributions 			_wq_cooperative_queue_scheduled_count_inc(wq, new_thread_qos);
4035*2c2f96dcSApple OSS Distributions 			cooperative_pool_sched_count_changed = true;
4036*2c2f96dcSApple OSS Distributions 		}
4037*2c2f96dcSApple OSS Distributions 	}
4038*2c2f96dcSApple OSS Distributions 
4039*2c2f96dcSApple OSS Distributions 	return cooperative_pool_sched_count_changed;
4040*2c2f96dcSApple OSS Distributions }
4041*2c2f96dcSApple OSS Distributions 
4042*2c2f96dcSApple OSS Distributions static workq_threadreq_t
workq_threadreq_select(struct workqueue * wq,struct uthread * uth)4043*2c2f96dcSApple OSS Distributions workq_threadreq_select(struct workqueue *wq, struct uthread *uth)
4044*2c2f96dcSApple OSS Distributions {
4045*2c2f96dcSApple OSS Distributions 	workq_threadreq_t req_qos, req_pri, req_tmp, req_mgr;
4046*2c2f96dcSApple OSS Distributions 	uintptr_t proprietor;
4047*2c2f96dcSApple OSS Distributions 	thread_qos_t qos = THREAD_QOS_UNSPECIFIED;
4048*2c2f96dcSApple OSS Distributions 	uint8_t pri = 0;
4049*2c2f96dcSApple OSS Distributions 
4050*2c2f96dcSApple OSS Distributions 	if (uth == wq->wq_creator) {
4051*2c2f96dcSApple OSS Distributions 		uth = NULL;
4052*2c2f96dcSApple OSS Distributions 	}
4053*2c2f96dcSApple OSS Distributions 
4054*2c2f96dcSApple OSS Distributions 	/*
4055*2c2f96dcSApple OSS Distributions 	 * Compute the best priority request (special or turnstile)
4056*2c2f96dcSApple OSS Distributions 	 */
4057*2c2f96dcSApple OSS Distributions 
4058*2c2f96dcSApple OSS Distributions 	pri = (uint8_t)turnstile_workq_proprietor_of_max_turnstile(wq->wq_turnstile,
4059*2c2f96dcSApple OSS Distributions 	    &proprietor);
4060*2c2f96dcSApple OSS Distributions 	if (pri) {
4061*2c2f96dcSApple OSS Distributions 		struct kqworkloop *kqwl = (struct kqworkloop *)proprietor;
4062*2c2f96dcSApple OSS Distributions 		req_pri = &kqwl->kqwl_request;
4063*2c2f96dcSApple OSS Distributions 		if (req_pri->tr_state != WORKQ_TR_STATE_QUEUED) {
4064*2c2f96dcSApple OSS Distributions 			panic("Invalid thread request (%p) state %d",
4065*2c2f96dcSApple OSS Distributions 			    req_pri, req_pri->tr_state);
4066*2c2f96dcSApple OSS Distributions 		}
4067*2c2f96dcSApple OSS Distributions 	} else {
4068*2c2f96dcSApple OSS Distributions 		req_pri = NULL;
4069*2c2f96dcSApple OSS Distributions 	}
4070*2c2f96dcSApple OSS Distributions 
4071*2c2f96dcSApple OSS Distributions 	req_tmp = priority_queue_max(&wq->wq_special_queue,
4072*2c2f96dcSApple OSS Distributions 	    struct workq_threadreq_s, tr_entry);
4073*2c2f96dcSApple OSS Distributions 	if (req_tmp && pri < priority_queue_entry_sched_pri(&wq->wq_special_queue,
4074*2c2f96dcSApple OSS Distributions 	    &req_tmp->tr_entry)) {
4075*2c2f96dcSApple OSS Distributions 		req_pri = req_tmp;
4076*2c2f96dcSApple OSS Distributions 		pri = (uint8_t)priority_queue_entry_sched_pri(&wq->wq_special_queue,
4077*2c2f96dcSApple OSS Distributions 		    &req_tmp->tr_entry);
4078*2c2f96dcSApple OSS Distributions 	}
4079*2c2f96dcSApple OSS Distributions 
4080*2c2f96dcSApple OSS Distributions 	/*
4081*2c2f96dcSApple OSS Distributions 	 * Handle the manager thread request. The special queue might yield
4082*2c2f96dcSApple OSS Distributions 	 * a higher priority, but the manager always beats the QoS world.
4083*2c2f96dcSApple OSS Distributions 	 */
4084*2c2f96dcSApple OSS Distributions 
4085*2c2f96dcSApple OSS Distributions 	req_mgr = wq->wq_event_manager_threadreq;
4086*2c2f96dcSApple OSS Distributions 	if (req_mgr && workq_may_start_event_mgr_thread(wq, uth)) {
4087*2c2f96dcSApple OSS Distributions 		uint32_t mgr_pri = wq->wq_event_manager_priority;
4088*2c2f96dcSApple OSS Distributions 
4089*2c2f96dcSApple OSS Distributions 		if (mgr_pri & _PTHREAD_PRIORITY_SCHED_PRI_FLAG) {
4090*2c2f96dcSApple OSS Distributions 			mgr_pri &= _PTHREAD_PRIORITY_SCHED_PRI_MASK;
4091*2c2f96dcSApple OSS Distributions 		} else {
4092*2c2f96dcSApple OSS Distributions 			mgr_pri = thread_workq_pri_for_qos(
4093*2c2f96dcSApple OSS Distributions 				_pthread_priority_thread_qos(mgr_pri));
4094*2c2f96dcSApple OSS Distributions 		}
4095*2c2f96dcSApple OSS Distributions 
4096*2c2f96dcSApple OSS Distributions 		return mgr_pri >= pri ? req_mgr : req_pri;
4097*2c2f96dcSApple OSS Distributions 	}
4098*2c2f96dcSApple OSS Distributions 
4099*2c2f96dcSApple OSS Distributions 	/*
4100*2c2f96dcSApple OSS Distributions 	 * Compute the best QoS Request, and check whether it beats the "pri" one
4101*2c2f96dcSApple OSS Distributions 	 */
4102*2c2f96dcSApple OSS Distributions 
4103*2c2f96dcSApple OSS Distributions 	req_qos = priority_queue_max(&wq->wq_overcommit_queue,
4104*2c2f96dcSApple OSS Distributions 	    struct workq_threadreq_s, tr_entry);
4105*2c2f96dcSApple OSS Distributions 	if (req_qos) {
4106*2c2f96dcSApple OSS Distributions 		qos = req_qos->tr_qos;
4107*2c2f96dcSApple OSS Distributions 	}
4108*2c2f96dcSApple OSS Distributions 
4109*2c2f96dcSApple OSS Distributions 	req_tmp = workq_cooperative_queue_best_req(wq, uth);
4110*2c2f96dcSApple OSS Distributions 	if (req_tmp && qos <= req_tmp->tr_qos) {
4111*2c2f96dcSApple OSS Distributions 		/*
4112*2c2f96dcSApple OSS Distributions 		 * Cooperative TR is better between overcommit and cooperative.  Note
4113*2c2f96dcSApple OSS Distributions 		 * that if qos is same between overcommit and cooperative, we choose
4114*2c2f96dcSApple OSS Distributions 		 * cooperative.
4115*2c2f96dcSApple OSS Distributions 		 *
4116*2c2f96dcSApple OSS Distributions 		 * Pick cooperative pool if it passes the admissions check
4117*2c2f96dcSApple OSS Distributions 		 */
4118*2c2f96dcSApple OSS Distributions 		if (workq_cooperative_allowance(wq, req_tmp->tr_qos, uth, true)) {
4119*2c2f96dcSApple OSS Distributions 			req_qos = req_tmp;
4120*2c2f96dcSApple OSS Distributions 			qos = req_qos->tr_qos;
4121*2c2f96dcSApple OSS Distributions 		}
4122*2c2f96dcSApple OSS Distributions 	}
4123*2c2f96dcSApple OSS Distributions 
4124*2c2f96dcSApple OSS Distributions 	/*
4125*2c2f96dcSApple OSS Distributions 	 * Compare the best QoS so far - either from overcommit or from cooperative
4126*2c2f96dcSApple OSS Distributions 	 * pool - and compare it with the constrained pool
4127*2c2f96dcSApple OSS Distributions 	 */
4128*2c2f96dcSApple OSS Distributions 	req_tmp = priority_queue_max(&wq->wq_constrained_queue,
4129*2c2f96dcSApple OSS Distributions 	    struct workq_threadreq_s, tr_entry);
4130*2c2f96dcSApple OSS Distributions 
4131*2c2f96dcSApple OSS Distributions 	if (req_tmp && qos < req_tmp->tr_qos) {
4132*2c2f96dcSApple OSS Distributions 		/*
4133*2c2f96dcSApple OSS Distributions 		 * Constrained pool is best in QoS between overcommit, cooperative
4134*2c2f96dcSApple OSS Distributions 		 * and constrained. Now check how it fairs against the priority case
4135*2c2f96dcSApple OSS Distributions 		 */
4136*2c2f96dcSApple OSS Distributions 		if (pri && pri >= thread_workq_pri_for_qos(req_tmp->tr_qos)) {
4137*2c2f96dcSApple OSS Distributions 			return req_pri;
4138*2c2f96dcSApple OSS Distributions 		}
4139*2c2f96dcSApple OSS Distributions 
4140*2c2f96dcSApple OSS Distributions 		if (workq_constrained_allowance(wq, req_tmp->tr_qos, uth, true)) {
4141*2c2f96dcSApple OSS Distributions 			/*
4142*2c2f96dcSApple OSS Distributions 			 * If the constrained thread request is the best one and passes
4143*2c2f96dcSApple OSS Distributions 			 * the admission check, pick it.
4144*2c2f96dcSApple OSS Distributions 			 */
4145*2c2f96dcSApple OSS Distributions 			return req_tmp;
4146*2c2f96dcSApple OSS Distributions 		}
4147*2c2f96dcSApple OSS Distributions 	}
4148*2c2f96dcSApple OSS Distributions 
4149*2c2f96dcSApple OSS Distributions 	if (req_pri && (!qos || pri >= thread_workq_pri_for_qos(qos))) {
4150*2c2f96dcSApple OSS Distributions 		return req_pri;
4151*2c2f96dcSApple OSS Distributions 	}
4152*2c2f96dcSApple OSS Distributions 
4153*2c2f96dcSApple OSS Distributions 	return req_qos;
4154*2c2f96dcSApple OSS Distributions }
4155*2c2f96dcSApple OSS Distributions 
4156*2c2f96dcSApple OSS Distributions /*
4157*2c2f96dcSApple OSS Distributions  * The creator is an anonymous thread that is counted as scheduled,
4158*2c2f96dcSApple OSS Distributions  * but otherwise without its scheduler callback set or tracked as active
4159*2c2f96dcSApple OSS Distributions  * that is used to make other threads.
4160*2c2f96dcSApple OSS Distributions  *
4161*2c2f96dcSApple OSS Distributions  * When more requests are added or an existing one is hurried along,
4162*2c2f96dcSApple OSS Distributions  * a creator is elected and setup, or the existing one overridden accordingly.
4163*2c2f96dcSApple OSS Distributions  *
4164*2c2f96dcSApple OSS Distributions  * While this creator is in flight, because no request has been dequeued,
4165*2c2f96dcSApple OSS Distributions  * already running threads have a chance at stealing thread requests avoiding
4166*2c2f96dcSApple OSS Distributions  * useless context switches, and the creator once scheduled may not find any
4167*2c2f96dcSApple OSS Distributions  * work to do and will then just park again.
4168*2c2f96dcSApple OSS Distributions  *
4169*2c2f96dcSApple OSS Distributions  * The creator serves the dual purpose of informing the scheduler of work that
4170*2c2f96dcSApple OSS Distributions  * hasn't be materialized as threads yet, and also as a natural pacing mechanism
4171*2c2f96dcSApple OSS Distributions  * for thread creation.
4172*2c2f96dcSApple OSS Distributions  *
4173*2c2f96dcSApple OSS Distributions  * By being anonymous (and not bound to anything) it means that thread requests
4174*2c2f96dcSApple OSS Distributions  * can be stolen from this creator by threads already on core yielding more
4175*2c2f96dcSApple OSS Distributions  * efficient scheduling and reduced context switches.
4176*2c2f96dcSApple OSS Distributions  */
4177*2c2f96dcSApple OSS Distributions static void
workq_schedule_creator(proc_t p,struct workqueue * wq,workq_kern_threadreq_flags_t flags)4178*2c2f96dcSApple OSS Distributions workq_schedule_creator(proc_t p, struct workqueue *wq,
4179*2c2f96dcSApple OSS Distributions     workq_kern_threadreq_flags_t flags)
4180*2c2f96dcSApple OSS Distributions {
4181*2c2f96dcSApple OSS Distributions 	workq_threadreq_t req;
4182*2c2f96dcSApple OSS Distributions 	struct uthread *uth;
4183*2c2f96dcSApple OSS Distributions 	bool needs_wakeup;
4184*2c2f96dcSApple OSS Distributions 
4185*2c2f96dcSApple OSS Distributions 	workq_lock_held(wq);
4186*2c2f96dcSApple OSS Distributions 	assert(p || (flags & WORKQ_THREADREQ_CAN_CREATE_THREADS) == 0);
4187*2c2f96dcSApple OSS Distributions 
4188*2c2f96dcSApple OSS Distributions again:
4189*2c2f96dcSApple OSS Distributions 	uth = wq->wq_creator;
4190*2c2f96dcSApple OSS Distributions 
4191*2c2f96dcSApple OSS Distributions 	if (!wq->wq_reqcount) {
4192*2c2f96dcSApple OSS Distributions 		/*
4193*2c2f96dcSApple OSS Distributions 		 * There is no thread request left.
4194*2c2f96dcSApple OSS Distributions 		 *
4195*2c2f96dcSApple OSS Distributions 		 * If there is a creator, leave everything in place, so that it cleans
4196*2c2f96dcSApple OSS Distributions 		 * up itself in workq_push_idle_thread().
4197*2c2f96dcSApple OSS Distributions 		 *
4198*2c2f96dcSApple OSS Distributions 		 * Else, make sure the turnstile state is reset to no inheritor.
4199*2c2f96dcSApple OSS Distributions 		 */
4200*2c2f96dcSApple OSS Distributions 		if (uth == NULL) {
4201*2c2f96dcSApple OSS Distributions 			workq_turnstile_update_inheritor(wq, TURNSTILE_INHERITOR_NULL, 0);
4202*2c2f96dcSApple OSS Distributions 		}
4203*2c2f96dcSApple OSS Distributions 		return;
4204*2c2f96dcSApple OSS Distributions 	}
4205*2c2f96dcSApple OSS Distributions 
4206*2c2f96dcSApple OSS Distributions 	req = workq_threadreq_select_for_creator(wq);
4207*2c2f96dcSApple OSS Distributions 	if (req == NULL) {
4208*2c2f96dcSApple OSS Distributions 		/*
4209*2c2f96dcSApple OSS Distributions 		 * There isn't a thread request that passes the admission check.
4210*2c2f96dcSApple OSS Distributions 		 *
4211*2c2f96dcSApple OSS Distributions 		 * If there is a creator, do not touch anything, the creator will sort
4212*2c2f96dcSApple OSS Distributions 		 * it out when it runs.
4213*2c2f96dcSApple OSS Distributions 		 *
4214*2c2f96dcSApple OSS Distributions 		 * Else, set the inheritor to "WORKQ" so that the turnstile propagation
4215*2c2f96dcSApple OSS Distributions 		 * code calls us if anything changes.
4216*2c2f96dcSApple OSS Distributions 		 */
4217*2c2f96dcSApple OSS Distributions 		if (uth == NULL) {
4218*2c2f96dcSApple OSS Distributions 			workq_turnstile_update_inheritor(wq, wq, TURNSTILE_INHERITOR_WORKQ);
4219*2c2f96dcSApple OSS Distributions 		}
4220*2c2f96dcSApple OSS Distributions 		return;
4221*2c2f96dcSApple OSS Distributions 	}
4222*2c2f96dcSApple OSS Distributions 
4223*2c2f96dcSApple OSS Distributions 
4224*2c2f96dcSApple OSS Distributions 	if (uth) {
4225*2c2f96dcSApple OSS Distributions 		/*
4226*2c2f96dcSApple OSS Distributions 		 * We need to maybe override the creator we already have
4227*2c2f96dcSApple OSS Distributions 		 */
4228*2c2f96dcSApple OSS Distributions 		if (workq_thread_needs_priority_change(req, uth)) {
4229*2c2f96dcSApple OSS Distributions 			WQ_TRACE_WQ(TRACE_wq_creator_select | DBG_FUNC_NONE,
4230*2c2f96dcSApple OSS Distributions 			    wq, 1, uthread_tid(uth), req->tr_qos);
4231*2c2f96dcSApple OSS Distributions 			workq_thread_reset_pri(wq, uth, req, /*unpark*/ true);
4232*2c2f96dcSApple OSS Distributions 		}
4233*2c2f96dcSApple OSS Distributions 		assert(wq->wq_inheritor == get_machthread(uth));
4234*2c2f96dcSApple OSS Distributions 	} else if (wq->wq_thidlecount) {
4235*2c2f96dcSApple OSS Distributions 		/*
4236*2c2f96dcSApple OSS Distributions 		 * We need to unpark a creator thread
4237*2c2f96dcSApple OSS Distributions 		 */
4238*2c2f96dcSApple OSS Distributions 		wq->wq_creator = uth = workq_pop_idle_thread(wq, UT_WORKQ_OVERCOMMIT,
4239*2c2f96dcSApple OSS Distributions 		    &needs_wakeup);
4240*2c2f96dcSApple OSS Distributions 		/* Always reset the priorities on the newly chosen creator */
4241*2c2f96dcSApple OSS Distributions 		workq_thread_reset_pri(wq, uth, req, /*unpark*/ true);
4242*2c2f96dcSApple OSS Distributions 		workq_turnstile_update_inheritor(wq, get_machthread(uth),
4243*2c2f96dcSApple OSS Distributions 		    TURNSTILE_INHERITOR_THREAD);
4244*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_creator_select | DBG_FUNC_NONE,
4245*2c2f96dcSApple OSS Distributions 		    wq, 2, uthread_tid(uth), req->tr_qos);
4246*2c2f96dcSApple OSS Distributions 		uth->uu_save.uus_workq_park_data.fulfilled_snapshot = wq->wq_fulfilled;
4247*2c2f96dcSApple OSS Distributions 		uth->uu_save.uus_workq_park_data.yields = 0;
4248*2c2f96dcSApple OSS Distributions 		if (needs_wakeup) {
4249*2c2f96dcSApple OSS Distributions 			workq_thread_wakeup(uth);
4250*2c2f96dcSApple OSS Distributions 		}
4251*2c2f96dcSApple OSS Distributions 	} else {
4252*2c2f96dcSApple OSS Distributions 		/*
4253*2c2f96dcSApple OSS Distributions 		 * We need to allocate a thread...
4254*2c2f96dcSApple OSS Distributions 		 */
4255*2c2f96dcSApple OSS Distributions 		if (__improbable(wq->wq_nthreads >= wq_max_threads)) {
4256*2c2f96dcSApple OSS Distributions 			/* out of threads, just go away */
4257*2c2f96dcSApple OSS Distributions 			flags = WORKQ_THREADREQ_NONE;
4258*2c2f96dcSApple OSS Distributions 		} else if (flags & WORKQ_THREADREQ_SET_AST_ON_FAILURE) {
4259*2c2f96dcSApple OSS Distributions 			act_set_astkevent(current_thread(), AST_KEVENT_REDRIVE_THREADREQ);
4260*2c2f96dcSApple OSS Distributions 		} else if (!(flags & WORKQ_THREADREQ_CAN_CREATE_THREADS)) {
4261*2c2f96dcSApple OSS Distributions 			/* This can drop the workqueue lock, and take it again */
4262*2c2f96dcSApple OSS Distributions 			workq_schedule_immediate_thread_creation(wq);
4263*2c2f96dcSApple OSS Distributions 		} else if (workq_add_new_idle_thread(p, wq)) {
4264*2c2f96dcSApple OSS Distributions 			goto again;
4265*2c2f96dcSApple OSS Distributions 		} else {
4266*2c2f96dcSApple OSS Distributions 			workq_schedule_delayed_thread_creation(wq, 0);
4267*2c2f96dcSApple OSS Distributions 		}
4268*2c2f96dcSApple OSS Distributions 
4269*2c2f96dcSApple OSS Distributions 		/*
4270*2c2f96dcSApple OSS Distributions 		 * If the current thread is the inheritor:
4271*2c2f96dcSApple OSS Distributions 		 *
4272*2c2f96dcSApple OSS Distributions 		 * If we set the AST, then the thread will stay the inheritor until
4273*2c2f96dcSApple OSS Distributions 		 * either the AST calls workq_kern_threadreq_redrive(), or it parks
4274*2c2f96dcSApple OSS Distributions 		 * and calls workq_push_idle_thread().
4275*2c2f96dcSApple OSS Distributions 		 *
4276*2c2f96dcSApple OSS Distributions 		 * Else, the responsibility of the thread creation is with a thread-call
4277*2c2f96dcSApple OSS Distributions 		 * and we need to clear the inheritor.
4278*2c2f96dcSApple OSS Distributions 		 */
4279*2c2f96dcSApple OSS Distributions 		if ((flags & WORKQ_THREADREQ_SET_AST_ON_FAILURE) == 0 &&
4280*2c2f96dcSApple OSS Distributions 		    wq->wq_inheritor == current_thread()) {
4281*2c2f96dcSApple OSS Distributions 			workq_turnstile_update_inheritor(wq, TURNSTILE_INHERITOR_NULL, 0);
4282*2c2f96dcSApple OSS Distributions 		}
4283*2c2f96dcSApple OSS Distributions 	}
4284*2c2f96dcSApple OSS Distributions }
4285*2c2f96dcSApple OSS Distributions 
4286*2c2f96dcSApple OSS Distributions /**
4287*2c2f96dcSApple OSS Distributions  * Same as workq_unpark_select_threadreq_or_park_and_unlock,
4288*2c2f96dcSApple OSS Distributions  * but do not allow early binds.
4289*2c2f96dcSApple OSS Distributions  *
4290*2c2f96dcSApple OSS Distributions  * Called with the base pri frozen, will unfreeze it.
4291*2c2f96dcSApple OSS Distributions  */
4292*2c2f96dcSApple OSS Distributions __attribute__((noreturn, noinline))
4293*2c2f96dcSApple OSS Distributions static void
workq_select_threadreq_or_park_and_unlock(proc_t p,struct workqueue * wq,struct uthread * uth,uint32_t setup_flags)4294*2c2f96dcSApple OSS Distributions workq_select_threadreq_or_park_and_unlock(proc_t p, struct workqueue *wq,
4295*2c2f96dcSApple OSS Distributions     struct uthread *uth, uint32_t setup_flags)
4296*2c2f96dcSApple OSS Distributions {
4297*2c2f96dcSApple OSS Distributions 	workq_threadreq_t req = NULL;
4298*2c2f96dcSApple OSS Distributions 	bool is_creator = (wq->wq_creator == uth);
4299*2c2f96dcSApple OSS Distributions 	bool schedule_creator = false;
4300*2c2f96dcSApple OSS Distributions 
4301*2c2f96dcSApple OSS Distributions 	if (__improbable(_wq_exiting(wq))) {
4302*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_select_threadreq | DBG_FUNC_NONE, wq, 0, 0, 0);
4303*2c2f96dcSApple OSS Distributions 		goto park;
4304*2c2f96dcSApple OSS Distributions 	}
4305*2c2f96dcSApple OSS Distributions 
4306*2c2f96dcSApple OSS Distributions 	if (wq->wq_reqcount == 0) {
4307*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_select_threadreq | DBG_FUNC_NONE, wq, 1, 0, 0);
4308*2c2f96dcSApple OSS Distributions 		goto park;
4309*2c2f96dcSApple OSS Distributions 	}
4310*2c2f96dcSApple OSS Distributions 
4311*2c2f96dcSApple OSS Distributions 	req = workq_threadreq_select(wq, uth);
4312*2c2f96dcSApple OSS Distributions 	if (__improbable(req == NULL)) {
4313*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_select_threadreq | DBG_FUNC_NONE, wq, 2, 0, 0);
4314*2c2f96dcSApple OSS Distributions 		goto park;
4315*2c2f96dcSApple OSS Distributions 	}
4316*2c2f96dcSApple OSS Distributions 
4317*2c2f96dcSApple OSS Distributions 	struct uu_workq_policy old_pri = uth->uu_workq_pri;
4318*2c2f96dcSApple OSS Distributions 	uint8_t tr_flags = req->tr_flags;
4319*2c2f96dcSApple OSS Distributions 	struct turnstile *req_ts = kqueue_threadreq_get_turnstile(req);
4320*2c2f96dcSApple OSS Distributions 
4321*2c2f96dcSApple OSS Distributions 	/*
4322*2c2f96dcSApple OSS Distributions 	 * Attempt to setup ourselves as the new thing to run, moving all priority
4323*2c2f96dcSApple OSS Distributions 	 * pushes to ourselves.
4324*2c2f96dcSApple OSS Distributions 	 *
4325*2c2f96dcSApple OSS Distributions 	 * If the current thread is the creator, then the fact that we are presently
4326*2c2f96dcSApple OSS Distributions 	 * running is proof that we'll do something useful, so keep going.
4327*2c2f96dcSApple OSS Distributions 	 *
4328*2c2f96dcSApple OSS Distributions 	 * For other cases, peek at the AST to know whether the scheduler wants
4329*2c2f96dcSApple OSS Distributions 	 * to preempt us, if yes, park instead, and move the thread request
4330*2c2f96dcSApple OSS Distributions 	 * turnstile back to the workqueue.
4331*2c2f96dcSApple OSS Distributions 	 */
4332*2c2f96dcSApple OSS Distributions 	if (req_ts) {
4333*2c2f96dcSApple OSS Distributions 		workq_perform_turnstile_operation_locked(wq, ^{
4334*2c2f96dcSApple OSS Distributions 			turnstile_update_inheritor(req_ts, get_machthread(uth),
4335*2c2f96dcSApple OSS Distributions 			TURNSTILE_IMMEDIATE_UPDATE | TURNSTILE_INHERITOR_THREAD);
4336*2c2f96dcSApple OSS Distributions 			turnstile_update_inheritor_complete(req_ts,
4337*2c2f96dcSApple OSS Distributions 			TURNSTILE_INTERLOCK_HELD);
4338*2c2f96dcSApple OSS Distributions 		});
4339*2c2f96dcSApple OSS Distributions 	}
4340*2c2f96dcSApple OSS Distributions 
4341*2c2f96dcSApple OSS Distributions 	/* accounting changes of aggregate thscheduled_count and thactive which has
4342*2c2f96dcSApple OSS Distributions 	 * to be paired with the workq_thread_reset_pri below so that we have
4343*2c2f96dcSApple OSS Distributions 	 * uth->uu_workq_pri match with thactive.
4344*2c2f96dcSApple OSS Distributions 	 *
4345*2c2f96dcSApple OSS Distributions 	 * This is undone when the thread parks */
4346*2c2f96dcSApple OSS Distributions 	if (is_creator) {
4347*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_creator_select, wq, 4, 0,
4348*2c2f96dcSApple OSS Distributions 		    uth->uu_save.uus_workq_park_data.yields);
4349*2c2f96dcSApple OSS Distributions 		wq->wq_creator = NULL;
4350*2c2f96dcSApple OSS Distributions 		_wq_thactive_inc(wq, req->tr_qos);
4351*2c2f96dcSApple OSS Distributions 		wq->wq_thscheduled_count[_wq_bucket(req->tr_qos)]++;
4352*2c2f96dcSApple OSS Distributions 	} else if (old_pri.qos_bucket != req->tr_qos) {
4353*2c2f96dcSApple OSS Distributions 		_wq_thactive_move(wq, old_pri.qos_bucket, req->tr_qos);
4354*2c2f96dcSApple OSS Distributions 	}
4355*2c2f96dcSApple OSS Distributions 	workq_thread_reset_pri(wq, uth, req, /*unpark*/ true);
4356*2c2f96dcSApple OSS Distributions 
4357*2c2f96dcSApple OSS Distributions 	/*
4358*2c2f96dcSApple OSS Distributions 	 * Make relevant accounting changes for pool specific counts.
4359*2c2f96dcSApple OSS Distributions 	 *
4360*2c2f96dcSApple OSS Distributions 	 * The schedule counts changing can affect what the next best request
4361*2c2f96dcSApple OSS Distributions 	 * for cooperative thread pool is if this request is dequeued.
4362*2c2f96dcSApple OSS Distributions 	 */
4363*2c2f96dcSApple OSS Distributions 	bool cooperative_sched_count_changed =
4364*2c2f96dcSApple OSS Distributions 	    workq_adjust_cooperative_constrained_schedule_counts(wq, uth,
4365*2c2f96dcSApple OSS Distributions 	    old_pri.qos_req, tr_flags);
4366*2c2f96dcSApple OSS Distributions 
4367*2c2f96dcSApple OSS Distributions 	if (workq_tr_is_overcommit(tr_flags)) {
4368*2c2f96dcSApple OSS Distributions 		workq_thread_set_type(uth, UT_WORKQ_OVERCOMMIT);
4369*2c2f96dcSApple OSS Distributions 	} else if (workq_tr_is_cooperative(tr_flags)) {
4370*2c2f96dcSApple OSS Distributions 		workq_thread_set_type(uth, UT_WORKQ_COOPERATIVE);
4371*2c2f96dcSApple OSS Distributions 	} else {
4372*2c2f96dcSApple OSS Distributions 		workq_thread_set_type(uth, 0);
4373*2c2f96dcSApple OSS Distributions 	}
4374*2c2f96dcSApple OSS Distributions 
4375*2c2f96dcSApple OSS Distributions 	if (__improbable(thread_unfreeze_base_pri(get_machthread(uth)) && !is_creator)) {
4376*2c2f96dcSApple OSS Distributions 		if (req_ts) {
4377*2c2f96dcSApple OSS Distributions 			workq_perform_turnstile_operation_locked(wq, ^{
4378*2c2f96dcSApple OSS Distributions 				turnstile_update_inheritor(req_ts, wq->wq_turnstile,
4379*2c2f96dcSApple OSS Distributions 				TURNSTILE_IMMEDIATE_UPDATE | TURNSTILE_INHERITOR_TURNSTILE);
4380*2c2f96dcSApple OSS Distributions 				turnstile_update_inheritor_complete(req_ts,
4381*2c2f96dcSApple OSS Distributions 				TURNSTILE_INTERLOCK_HELD);
4382*2c2f96dcSApple OSS Distributions 			});
4383*2c2f96dcSApple OSS Distributions 		}
4384*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_select_threadreq | DBG_FUNC_NONE, wq, 3, 0, 0);
4385*2c2f96dcSApple OSS Distributions 		goto park_thawed;
4386*2c2f96dcSApple OSS Distributions 	}
4387*2c2f96dcSApple OSS Distributions 
4388*2c2f96dcSApple OSS Distributions 	/*
4389*2c2f96dcSApple OSS Distributions 	 * We passed all checks, dequeue the request, bind to it, and set it up
4390*2c2f96dcSApple OSS Distributions 	 * to return to user.
4391*2c2f96dcSApple OSS Distributions 	 */
4392*2c2f96dcSApple OSS Distributions 	WQ_TRACE_WQ(TRACE_wq_thread_logical_run | DBG_FUNC_START, wq,
4393*2c2f96dcSApple OSS Distributions 	    workq_trace_req_id(req), tr_flags, 0);
4394*2c2f96dcSApple OSS Distributions 	wq->wq_fulfilled++;
4395*2c2f96dcSApple OSS Distributions 	schedule_creator = workq_threadreq_dequeue(wq, req,
4396*2c2f96dcSApple OSS Distributions 	    cooperative_sched_count_changed);
4397*2c2f96dcSApple OSS Distributions 
4398*2c2f96dcSApple OSS Distributions 	workq_thread_reset_cpupercent(req, uth);
4399*2c2f96dcSApple OSS Distributions 
4400*2c2f96dcSApple OSS Distributions 	if (tr_flags & (WORKQ_TR_FLAG_KEVENT | WORKQ_TR_FLAG_WORKLOOP)) {
4401*2c2f96dcSApple OSS Distributions 		kqueue_threadreq_bind_prepost(p, req, uth);
4402*2c2f96dcSApple OSS Distributions 		req = NULL;
4403*2c2f96dcSApple OSS Distributions 	} else if (req->tr_count > 0) {
4404*2c2f96dcSApple OSS Distributions 		req = NULL;
4405*2c2f96dcSApple OSS Distributions 	}
4406*2c2f96dcSApple OSS Distributions 
4407*2c2f96dcSApple OSS Distributions 	if (uth->uu_workq_flags & UT_WORKQ_NEW) {
4408*2c2f96dcSApple OSS Distributions 		uth->uu_workq_flags ^= UT_WORKQ_NEW;
4409*2c2f96dcSApple OSS Distributions 		setup_flags |= WQ_SETUP_FIRST_USE;
4410*2c2f96dcSApple OSS Distributions 	}
4411*2c2f96dcSApple OSS Distributions 
4412*2c2f96dcSApple OSS Distributions 	/* If one of the following is true, call workq_schedule_creator (which also
4413*2c2f96dcSApple OSS Distributions 	 * adjusts priority of existing creator):
4414*2c2f96dcSApple OSS Distributions 	 *
4415*2c2f96dcSApple OSS Distributions 	 *	  - We are the creator currently so the wq may need a new creator
4416*2c2f96dcSApple OSS Distributions 	 *	  - The request we're binding to is the highest priority one, existing
4417*2c2f96dcSApple OSS Distributions 	 *	  creator's priority might need to be adjusted to reflect the next
4418*2c2f96dcSApple OSS Distributions 	 *	  highest TR
4419*2c2f96dcSApple OSS Distributions 	 */
4420*2c2f96dcSApple OSS Distributions 	if (is_creator || schedule_creator) {
4421*2c2f96dcSApple OSS Distributions 		/* This can drop the workqueue lock, and take it again */
4422*2c2f96dcSApple OSS Distributions 		workq_schedule_creator(p, wq, WORKQ_THREADREQ_CAN_CREATE_THREADS);
4423*2c2f96dcSApple OSS Distributions 	}
4424*2c2f96dcSApple OSS Distributions 
4425*2c2f96dcSApple OSS Distributions 	workq_unlock(wq);
4426*2c2f96dcSApple OSS Distributions 
4427*2c2f96dcSApple OSS Distributions 	if (req) {
4428*2c2f96dcSApple OSS Distributions 		zfree(workq_zone_threadreq, req);
4429*2c2f96dcSApple OSS Distributions 	}
4430*2c2f96dcSApple OSS Distributions 
4431*2c2f96dcSApple OSS Distributions 	/*
4432*2c2f96dcSApple OSS Distributions 	 * Run Thread, Run!
4433*2c2f96dcSApple OSS Distributions 	 */
4434*2c2f96dcSApple OSS Distributions 	uint32_t upcall_flags = WQ_FLAG_THREAD_NEWSPI;
4435*2c2f96dcSApple OSS Distributions 	if (uth->uu_workq_pri.qos_bucket == WORKQ_THREAD_QOS_MANAGER) {
4436*2c2f96dcSApple OSS Distributions 		upcall_flags |= WQ_FLAG_THREAD_EVENT_MANAGER;
4437*2c2f96dcSApple OSS Distributions 	} else if (workq_tr_is_overcommit(tr_flags)) {
4438*2c2f96dcSApple OSS Distributions 		upcall_flags |= WQ_FLAG_THREAD_OVERCOMMIT;
4439*2c2f96dcSApple OSS Distributions 	} else if (workq_tr_is_cooperative(tr_flags)) {
4440*2c2f96dcSApple OSS Distributions 		upcall_flags |= WQ_FLAG_THREAD_COOPERATIVE;
4441*2c2f96dcSApple OSS Distributions 	}
4442*2c2f96dcSApple OSS Distributions 	if (tr_flags & WORKQ_TR_FLAG_KEVENT) {
4443*2c2f96dcSApple OSS Distributions 		upcall_flags |= WQ_FLAG_THREAD_KEVENT;
4444*2c2f96dcSApple OSS Distributions 		assert((upcall_flags & WQ_FLAG_THREAD_COOPERATIVE) == 0);
4445*2c2f96dcSApple OSS Distributions 	}
4446*2c2f96dcSApple OSS Distributions 
4447*2c2f96dcSApple OSS Distributions 	if (tr_flags & WORKQ_TR_FLAG_WORKLOOP) {
4448*2c2f96dcSApple OSS Distributions 		upcall_flags |= WQ_FLAG_THREAD_WORKLOOP | WQ_FLAG_THREAD_KEVENT;
4449*2c2f96dcSApple OSS Distributions 	}
4450*2c2f96dcSApple OSS Distributions 	uth->uu_save.uus_workq_park_data.upcall_flags = upcall_flags;
4451*2c2f96dcSApple OSS Distributions 
4452*2c2f96dcSApple OSS Distributions 	if (tr_flags & (WORKQ_TR_FLAG_KEVENT | WORKQ_TR_FLAG_WORKLOOP)) {
4453*2c2f96dcSApple OSS Distributions 		kqueue_threadreq_bind_commit(p, get_machthread(uth));
4454*2c2f96dcSApple OSS Distributions 	} else {
4455*2c2f96dcSApple OSS Distributions #if CONFIG_PREADOPT_TG
4456*2c2f96dcSApple OSS Distributions 		/*
4457*2c2f96dcSApple OSS Distributions 		 * The thread may have a preadopt thread group on it already because it
4458*2c2f96dcSApple OSS Distributions 		 * got tagged with it as a creator thread. So we need to make sure to
4459*2c2f96dcSApple OSS Distributions 		 * clear that since we don't have preadoption for anonymous thread
4460*2c2f96dcSApple OSS Distributions 		 * requests
4461*2c2f96dcSApple OSS Distributions 		 */
4462*2c2f96dcSApple OSS Distributions 		thread_set_preadopt_thread_group(get_machthread(uth), NULL);
4463*2c2f96dcSApple OSS Distributions #endif
4464*2c2f96dcSApple OSS Distributions 	}
4465*2c2f96dcSApple OSS Distributions 
4466*2c2f96dcSApple OSS Distributions 	workq_setup_and_run(p, uth, setup_flags);
4467*2c2f96dcSApple OSS Distributions 	__builtin_unreachable();
4468*2c2f96dcSApple OSS Distributions 
4469*2c2f96dcSApple OSS Distributions park:
4470*2c2f96dcSApple OSS Distributions 	thread_unfreeze_base_pri(get_machthread(uth));
4471*2c2f96dcSApple OSS Distributions park_thawed:
4472*2c2f96dcSApple OSS Distributions 	workq_park_and_unlock(p, wq, uth, setup_flags);
4473*2c2f96dcSApple OSS Distributions }
4474*2c2f96dcSApple OSS Distributions 
4475*2c2f96dcSApple OSS Distributions /**
4476*2c2f96dcSApple OSS Distributions  * Runs a thread request on a thread
4477*2c2f96dcSApple OSS Distributions  *
4478*2c2f96dcSApple OSS Distributions  * - if thread is THREAD_NULL, will find a thread and run the request there.
4479*2c2f96dcSApple OSS Distributions  *   Otherwise, the thread must be the current thread.
4480*2c2f96dcSApple OSS Distributions  *
4481*2c2f96dcSApple OSS Distributions  * - if req is NULL, will find the highest priority request and run that.  If
4482*2c2f96dcSApple OSS Distributions  *   it is not NULL, it must be a threadreq object in state NEW.  If it can not
4483*2c2f96dcSApple OSS Distributions  *   be run immediately, it will be enqueued and moved to state QUEUED.
4484*2c2f96dcSApple OSS Distributions  *
4485*2c2f96dcSApple OSS Distributions  *   Either way, the thread request object serviced will be moved to state
4486*2c2f96dcSApple OSS Distributions  *   BINDING and attached to the uthread.
4487*2c2f96dcSApple OSS Distributions  *
4488*2c2f96dcSApple OSS Distributions  * Should be called with the workqueue lock held.  Will drop it.
4489*2c2f96dcSApple OSS Distributions  * Should be called with the base pri not frozen.
4490*2c2f96dcSApple OSS Distributions  */
4491*2c2f96dcSApple OSS Distributions __attribute__((noreturn, noinline))
4492*2c2f96dcSApple OSS Distributions static void
workq_unpark_select_threadreq_or_park_and_unlock(proc_t p,struct workqueue * wq,struct uthread * uth,uint32_t setup_flags)4493*2c2f96dcSApple OSS Distributions workq_unpark_select_threadreq_or_park_and_unlock(proc_t p, struct workqueue *wq,
4494*2c2f96dcSApple OSS Distributions     struct uthread *uth, uint32_t setup_flags)
4495*2c2f96dcSApple OSS Distributions {
4496*2c2f96dcSApple OSS Distributions 	if (uth->uu_workq_flags & UT_WORKQ_EARLY_BOUND) {
4497*2c2f96dcSApple OSS Distributions 		if (uth->uu_workq_flags & UT_WORKQ_NEW) {
4498*2c2f96dcSApple OSS Distributions 			setup_flags |= WQ_SETUP_FIRST_USE;
4499*2c2f96dcSApple OSS Distributions 		}
4500*2c2f96dcSApple OSS Distributions 		uth->uu_workq_flags &= ~(UT_WORKQ_NEW | UT_WORKQ_EARLY_BOUND);
4501*2c2f96dcSApple OSS Distributions 		/*
4502*2c2f96dcSApple OSS Distributions 		 * This pointer is possibly freed and only used for tracing purposes.
4503*2c2f96dcSApple OSS Distributions 		 */
4504*2c2f96dcSApple OSS Distributions 		workq_threadreq_t req = uth->uu_save.uus_workq_park_data.thread_request;
4505*2c2f96dcSApple OSS Distributions 		workq_unlock(wq);
4506*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_thread_logical_run | DBG_FUNC_START, wq,
4507*2c2f96dcSApple OSS Distributions 		    VM_KERNEL_ADDRHIDE(req), 0, 0);
4508*2c2f96dcSApple OSS Distributions 		(void)req;
4509*2c2f96dcSApple OSS Distributions 
4510*2c2f96dcSApple OSS Distributions 		workq_setup_and_run(p, uth, setup_flags);
4511*2c2f96dcSApple OSS Distributions 		__builtin_unreachable();
4512*2c2f96dcSApple OSS Distributions 	}
4513*2c2f96dcSApple OSS Distributions 
4514*2c2f96dcSApple OSS Distributions 	thread_freeze_base_pri(get_machthread(uth));
4515*2c2f96dcSApple OSS Distributions 	workq_select_threadreq_or_park_and_unlock(p, wq, uth, setup_flags);
4516*2c2f96dcSApple OSS Distributions }
4517*2c2f96dcSApple OSS Distributions 
4518*2c2f96dcSApple OSS Distributions static bool
workq_creator_should_yield(struct workqueue * wq,struct uthread * uth)4519*2c2f96dcSApple OSS Distributions workq_creator_should_yield(struct workqueue *wq, struct uthread *uth)
4520*2c2f96dcSApple OSS Distributions {
4521*2c2f96dcSApple OSS Distributions 	thread_qos_t qos = workq_pri_override(uth->uu_workq_pri);
4522*2c2f96dcSApple OSS Distributions 
4523*2c2f96dcSApple OSS Distributions 	if (qos >= THREAD_QOS_USER_INTERACTIVE) {
4524*2c2f96dcSApple OSS Distributions 		return false;
4525*2c2f96dcSApple OSS Distributions 	}
4526*2c2f96dcSApple OSS Distributions 
4527*2c2f96dcSApple OSS Distributions 	uint32_t snapshot = uth->uu_save.uus_workq_park_data.fulfilled_snapshot;
4528*2c2f96dcSApple OSS Distributions 	if (wq->wq_fulfilled == snapshot) {
4529*2c2f96dcSApple OSS Distributions 		return false;
4530*2c2f96dcSApple OSS Distributions 	}
4531*2c2f96dcSApple OSS Distributions 
4532*2c2f96dcSApple OSS Distributions 	uint32_t cnt = 0, conc = wq_max_parallelism[_wq_bucket(qos)];
4533*2c2f96dcSApple OSS Distributions 	if (wq->wq_fulfilled - snapshot > conc) {
4534*2c2f96dcSApple OSS Distributions 		/* we fulfilled more than NCPU requests since being dispatched */
4535*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_creator_yield, wq, 1,
4536*2c2f96dcSApple OSS Distributions 		    wq->wq_fulfilled, snapshot);
4537*2c2f96dcSApple OSS Distributions 		return true;
4538*2c2f96dcSApple OSS Distributions 	}
4539*2c2f96dcSApple OSS Distributions 
4540*2c2f96dcSApple OSS Distributions 	for (uint8_t i = _wq_bucket(qos); i < WORKQ_NUM_QOS_BUCKETS; i++) {
4541*2c2f96dcSApple OSS Distributions 		cnt += wq->wq_thscheduled_count[i];
4542*2c2f96dcSApple OSS Distributions 	}
4543*2c2f96dcSApple OSS Distributions 	if (conc <= cnt) {
4544*2c2f96dcSApple OSS Distributions 		/* We fulfilled requests and have more than NCPU scheduled threads */
4545*2c2f96dcSApple OSS Distributions 		WQ_TRACE_WQ(TRACE_wq_creator_yield, wq, 2,
4546*2c2f96dcSApple OSS Distributions 		    wq->wq_fulfilled, snapshot);
4547*2c2f96dcSApple OSS Distributions 		return true;
4548*2c2f96dcSApple OSS Distributions 	}
4549*2c2f96dcSApple OSS Distributions 
4550*2c2f96dcSApple OSS Distributions 	return false;
4551*2c2f96dcSApple OSS Distributions }
4552*2c2f96dcSApple OSS Distributions 
4553*2c2f96dcSApple OSS Distributions /**
4554*2c2f96dcSApple OSS Distributions  * parked thread wakes up
4555*2c2f96dcSApple OSS Distributions  */
4556*2c2f96dcSApple OSS Distributions __attribute__((noreturn, noinline))
4557*2c2f96dcSApple OSS Distributions static void
workq_unpark_continue(void * parameter __unused,wait_result_t wr __unused)4558*2c2f96dcSApple OSS Distributions workq_unpark_continue(void *parameter __unused, wait_result_t wr __unused)
4559*2c2f96dcSApple OSS Distributions {
4560*2c2f96dcSApple OSS Distributions 	thread_t th = current_thread();
4561*2c2f96dcSApple OSS Distributions 	struct uthread *uth = get_bsdthread_info(th);
4562*2c2f96dcSApple OSS Distributions 	proc_t p = current_proc();
4563*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = proc_get_wqptr_fast(p);
4564*2c2f96dcSApple OSS Distributions 
4565*2c2f96dcSApple OSS Distributions 	workq_lock_spin(wq);
4566*2c2f96dcSApple OSS Distributions 
4567*2c2f96dcSApple OSS Distributions 	if (wq->wq_creator == uth && workq_creator_should_yield(wq, uth)) {
4568*2c2f96dcSApple OSS Distributions 		/*
4569*2c2f96dcSApple OSS Distributions 		 * If the number of threads we have out are able to keep up with the
4570*2c2f96dcSApple OSS Distributions 		 * demand, then we should avoid sending this creator thread to
4571*2c2f96dcSApple OSS Distributions 		 * userspace.
4572*2c2f96dcSApple OSS Distributions 		 */
4573*2c2f96dcSApple OSS Distributions 		uth->uu_save.uus_workq_park_data.fulfilled_snapshot = wq->wq_fulfilled;
4574*2c2f96dcSApple OSS Distributions 		uth->uu_save.uus_workq_park_data.yields++;
4575*2c2f96dcSApple OSS Distributions 		workq_unlock(wq);
4576*2c2f96dcSApple OSS Distributions 		thread_yield_with_continuation(workq_unpark_continue, NULL);
4577*2c2f96dcSApple OSS Distributions 		__builtin_unreachable();
4578*2c2f96dcSApple OSS Distributions 	}
4579*2c2f96dcSApple OSS Distributions 
4580*2c2f96dcSApple OSS Distributions 	if (__probable(uth->uu_workq_flags & UT_WORKQ_RUNNING)) {
4581*2c2f96dcSApple OSS Distributions 		workq_unpark_select_threadreq_or_park_and_unlock(p, wq, uth, WQ_SETUP_NONE);
4582*2c2f96dcSApple OSS Distributions 		__builtin_unreachable();
4583*2c2f96dcSApple OSS Distributions 	}
4584*2c2f96dcSApple OSS Distributions 
4585*2c2f96dcSApple OSS Distributions 	if (__probable(wr == THREAD_AWAKENED)) {
4586*2c2f96dcSApple OSS Distributions 		/*
4587*2c2f96dcSApple OSS Distributions 		 * We were set running, but for the purposes of dying.
4588*2c2f96dcSApple OSS Distributions 		 */
4589*2c2f96dcSApple OSS Distributions 		assert(uth->uu_workq_flags & UT_WORKQ_DYING);
4590*2c2f96dcSApple OSS Distributions 		assert((uth->uu_workq_flags & UT_WORKQ_NEW) == 0);
4591*2c2f96dcSApple OSS Distributions 	} else {
4592*2c2f96dcSApple OSS Distributions 		/*
4593*2c2f96dcSApple OSS Distributions 		 * workaround for <rdar://problem/38647347>,
4594*2c2f96dcSApple OSS Distributions 		 * in case we do hit userspace, make sure calling
4595*2c2f96dcSApple OSS Distributions 		 * workq_thread_terminate() does the right thing here,
4596*2c2f96dcSApple OSS Distributions 		 * and if we never call it, that workq_exit() will too because it sees
4597*2c2f96dcSApple OSS Distributions 		 * this thread on the runlist.
4598*2c2f96dcSApple OSS Distributions 		 */
4599*2c2f96dcSApple OSS Distributions 		assert(wr == THREAD_INTERRUPTED);
4600*2c2f96dcSApple OSS Distributions 		wq->wq_thdying_count++;
4601*2c2f96dcSApple OSS Distributions 		uth->uu_workq_flags |= UT_WORKQ_DYING;
4602*2c2f96dcSApple OSS Distributions 	}
4603*2c2f96dcSApple OSS Distributions 
4604*2c2f96dcSApple OSS Distributions 	workq_unpark_for_death_and_unlock(p, wq, uth,
4605*2c2f96dcSApple OSS Distributions 	    WORKQ_UNPARK_FOR_DEATH_WAS_IDLE, WQ_SETUP_NONE);
4606*2c2f96dcSApple OSS Distributions 	__builtin_unreachable();
4607*2c2f96dcSApple OSS Distributions }
4608*2c2f96dcSApple OSS Distributions 
4609*2c2f96dcSApple OSS Distributions __attribute__((noreturn, noinline))
4610*2c2f96dcSApple OSS Distributions static void
workq_setup_and_run(proc_t p,struct uthread * uth,int setup_flags)4611*2c2f96dcSApple OSS Distributions workq_setup_and_run(proc_t p, struct uthread *uth, int setup_flags)
4612*2c2f96dcSApple OSS Distributions {
4613*2c2f96dcSApple OSS Distributions 	thread_t th = get_machthread(uth);
4614*2c2f96dcSApple OSS Distributions 	vm_map_t vmap = get_task_map(proc_task(p));
4615*2c2f96dcSApple OSS Distributions 
4616*2c2f96dcSApple OSS Distributions 	if (setup_flags & WQ_SETUP_CLEAR_VOUCHER) {
4617*2c2f96dcSApple OSS Distributions 		/*
4618*2c2f96dcSApple OSS Distributions 		 * For preemption reasons, we want to reset the voucher as late as
4619*2c2f96dcSApple OSS Distributions 		 * possible, so we do it in two places:
4620*2c2f96dcSApple OSS Distributions 		 *   - Just before parking (i.e. in workq_park_and_unlock())
4621*2c2f96dcSApple OSS Distributions 		 *   - Prior to doing the setup for the next workitem (i.e. here)
4622*2c2f96dcSApple OSS Distributions 		 *
4623*2c2f96dcSApple OSS Distributions 		 * Those two places are sufficient to ensure we always reset it before
4624*2c2f96dcSApple OSS Distributions 		 * it goes back out to user space, but be careful to not break that
4625*2c2f96dcSApple OSS Distributions 		 * guarantee.
4626*2c2f96dcSApple OSS Distributions 		 *
4627*2c2f96dcSApple OSS Distributions 		 * Note that setting the voucher to NULL will not clear the preadoption
4628*2c2f96dcSApple OSS Distributions 		 * thread group on this thread
4629*2c2f96dcSApple OSS Distributions 		 */
4630*2c2f96dcSApple OSS Distributions 		__assert_only kern_return_t kr;
4631*2c2f96dcSApple OSS Distributions 		kr = thread_set_voucher_name(MACH_PORT_NULL);
4632*2c2f96dcSApple OSS Distributions 		assert(kr == KERN_SUCCESS);
4633*2c2f96dcSApple OSS Distributions 	}
4634*2c2f96dcSApple OSS Distributions 
4635*2c2f96dcSApple OSS Distributions 	uint32_t upcall_flags = uth->uu_save.uus_workq_park_data.upcall_flags;
4636*2c2f96dcSApple OSS Distributions 	if (!(setup_flags & WQ_SETUP_FIRST_USE)) {
4637*2c2f96dcSApple OSS Distributions 		upcall_flags |= WQ_FLAG_THREAD_REUSE;
4638*2c2f96dcSApple OSS Distributions 	}
4639*2c2f96dcSApple OSS Distributions 
4640*2c2f96dcSApple OSS Distributions 	if (uth->uu_workq_flags & UT_WORKQ_OUTSIDE_QOS) {
4641*2c2f96dcSApple OSS Distributions 		/*
4642*2c2f96dcSApple OSS Distributions 		 * For threads that have an outside-of-QoS thread priority, indicate
4643*2c2f96dcSApple OSS Distributions 		 * to userspace that setting QoS should only affect the TSD and not
4644*2c2f96dcSApple OSS Distributions 		 * change QOS in the kernel.
4645*2c2f96dcSApple OSS Distributions 		 */
4646*2c2f96dcSApple OSS Distributions 		upcall_flags |= WQ_FLAG_THREAD_OUTSIDEQOS;
4647*2c2f96dcSApple OSS Distributions 	} else {
4648*2c2f96dcSApple OSS Distributions 		/*
4649*2c2f96dcSApple OSS Distributions 		 * Put the QoS class value into the lower bits of the reuse_thread
4650*2c2f96dcSApple OSS Distributions 		 * register, this is where the thread priority used to be stored
4651*2c2f96dcSApple OSS Distributions 		 * anyway.
4652*2c2f96dcSApple OSS Distributions 		 */
4653*2c2f96dcSApple OSS Distributions 		upcall_flags |= uth->uu_save.uus_workq_park_data.qos |
4654*2c2f96dcSApple OSS Distributions 		    WQ_FLAG_THREAD_PRIO_QOS;
4655*2c2f96dcSApple OSS Distributions 	}
4656*2c2f96dcSApple OSS Distributions 
4657*2c2f96dcSApple OSS Distributions 	if (uth->uu_workq_thport == MACH_PORT_NULL) {
4658*2c2f96dcSApple OSS Distributions 		/* convert_thread_to_port_pinned() consumes a reference */
4659*2c2f96dcSApple OSS Distributions 		thread_reference(th);
4660*2c2f96dcSApple OSS Distributions 		/* Convert to immovable/pinned thread port, but port is not pinned yet */
4661*2c2f96dcSApple OSS Distributions 		ipc_port_t port = convert_thread_to_port_pinned(th);
4662*2c2f96dcSApple OSS Distributions 		/* Atomically, pin and copy out the port */
4663*2c2f96dcSApple OSS Distributions 		uth->uu_workq_thport = ipc_port_copyout_send_pinned(port, get_task_ipcspace(proc_task(p)));
4664*2c2f96dcSApple OSS Distributions 	}
4665*2c2f96dcSApple OSS Distributions 
4666*2c2f96dcSApple OSS Distributions 	/* Thread has been set up to run, arm its next workqueue quantum or disarm
4667*2c2f96dcSApple OSS Distributions 	 * if it is no longer supporting that */
4668*2c2f96dcSApple OSS Distributions 	if (thread_supports_cooperative_workqueue(th)) {
4669*2c2f96dcSApple OSS Distributions 		thread_arm_workqueue_quantum(th);
4670*2c2f96dcSApple OSS Distributions 	} else {
4671*2c2f96dcSApple OSS Distributions 		thread_disarm_workqueue_quantum(th);
4672*2c2f96dcSApple OSS Distributions 	}
4673*2c2f96dcSApple OSS Distributions 
4674*2c2f96dcSApple OSS Distributions 	/*
4675*2c2f96dcSApple OSS Distributions 	 * Call out to pthread, this sets up the thread, pulls in kevent structs
4676*2c2f96dcSApple OSS Distributions 	 * onto the stack, sets up the thread state and then returns to userspace.
4677*2c2f96dcSApple OSS Distributions 	 */
4678*2c2f96dcSApple OSS Distributions 	WQ_TRACE_WQ(TRACE_wq_runthread | DBG_FUNC_START,
4679*2c2f96dcSApple OSS Distributions 	    proc_get_wqptr_fast(p), 0, 0, 0);
4680*2c2f96dcSApple OSS Distributions 
4681*2c2f96dcSApple OSS Distributions 	if (workq_thread_is_cooperative(uth)) {
4682*2c2f96dcSApple OSS Distributions 		thread_sched_call(th, NULL);
4683*2c2f96dcSApple OSS Distributions 	} else {
4684*2c2f96dcSApple OSS Distributions 		thread_sched_call(th, workq_sched_callback);
4685*2c2f96dcSApple OSS Distributions 	}
4686*2c2f96dcSApple OSS Distributions 
4687*2c2f96dcSApple OSS Distributions 	pthread_functions->workq_setup_thread(p, th, vmap, uth->uu_workq_stackaddr,
4688*2c2f96dcSApple OSS Distributions 	    uth->uu_workq_thport, 0, setup_flags, upcall_flags);
4689*2c2f96dcSApple OSS Distributions 
4690*2c2f96dcSApple OSS Distributions 	__builtin_unreachable();
4691*2c2f96dcSApple OSS Distributions }
4692*2c2f96dcSApple OSS Distributions 
4693*2c2f96dcSApple OSS Distributions #pragma mark misc
4694*2c2f96dcSApple OSS Distributions 
4695*2c2f96dcSApple OSS Distributions int
fill_procworkqueue(proc_t p,struct proc_workqueueinfo * pwqinfo)4696*2c2f96dcSApple OSS Distributions fill_procworkqueue(proc_t p, struct proc_workqueueinfo * pwqinfo)
4697*2c2f96dcSApple OSS Distributions {
4698*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = proc_get_wqptr(p);
4699*2c2f96dcSApple OSS Distributions 	int error = 0;
4700*2c2f96dcSApple OSS Distributions 	int     activecount;
4701*2c2f96dcSApple OSS Distributions 
4702*2c2f96dcSApple OSS Distributions 	if (wq == NULL) {
4703*2c2f96dcSApple OSS Distributions 		return EINVAL;
4704*2c2f96dcSApple OSS Distributions 	}
4705*2c2f96dcSApple OSS Distributions 
4706*2c2f96dcSApple OSS Distributions 	/*
4707*2c2f96dcSApple OSS Distributions 	 * This is sometimes called from interrupt context by the kperf sampler.
4708*2c2f96dcSApple OSS Distributions 	 * In that case, it's not safe to spin trying to take the lock since we
4709*2c2f96dcSApple OSS Distributions 	 * might already hold it.  So, we just try-lock it and error out if it's
4710*2c2f96dcSApple OSS Distributions 	 * already held.  Since this is just a debugging aid, and all our callers
4711*2c2f96dcSApple OSS Distributions 	 * are able to handle an error, that's fine.
4712*2c2f96dcSApple OSS Distributions 	 */
4713*2c2f96dcSApple OSS Distributions 	bool locked = workq_lock_try(wq);
4714*2c2f96dcSApple OSS Distributions 	if (!locked) {
4715*2c2f96dcSApple OSS Distributions 		return EBUSY;
4716*2c2f96dcSApple OSS Distributions 	}
4717*2c2f96dcSApple OSS Distributions 
4718*2c2f96dcSApple OSS Distributions 	wq_thactive_t act = _wq_thactive(wq);
4719*2c2f96dcSApple OSS Distributions 	activecount = _wq_thactive_aggregate_downto_qos(wq, act,
4720*2c2f96dcSApple OSS Distributions 	    WORKQ_THREAD_QOS_MIN, NULL, NULL);
4721*2c2f96dcSApple OSS Distributions 	if (act & _wq_thactive_offset_for_qos(WORKQ_THREAD_QOS_MANAGER)) {
4722*2c2f96dcSApple OSS Distributions 		activecount++;
4723*2c2f96dcSApple OSS Distributions 	}
4724*2c2f96dcSApple OSS Distributions 	pwqinfo->pwq_nthreads = wq->wq_nthreads;
4725*2c2f96dcSApple OSS Distributions 	pwqinfo->pwq_runthreads = activecount;
4726*2c2f96dcSApple OSS Distributions 	pwqinfo->pwq_blockedthreads = wq->wq_threads_scheduled - activecount;
4727*2c2f96dcSApple OSS Distributions 	pwqinfo->pwq_state = 0;
4728*2c2f96dcSApple OSS Distributions 
4729*2c2f96dcSApple OSS Distributions 	if (wq->wq_constrained_threads_scheduled >= wq_max_constrained_threads) {
4730*2c2f96dcSApple OSS Distributions 		pwqinfo->pwq_state |= WQ_EXCEEDED_CONSTRAINED_THREAD_LIMIT;
4731*2c2f96dcSApple OSS Distributions 	}
4732*2c2f96dcSApple OSS Distributions 
4733*2c2f96dcSApple OSS Distributions 	if (wq->wq_nthreads >= wq_max_threads) {
4734*2c2f96dcSApple OSS Distributions 		pwqinfo->pwq_state |= WQ_EXCEEDED_TOTAL_THREAD_LIMIT;
4735*2c2f96dcSApple OSS Distributions 	}
4736*2c2f96dcSApple OSS Distributions 
4737*2c2f96dcSApple OSS Distributions 	workq_unlock(wq);
4738*2c2f96dcSApple OSS Distributions 	return error;
4739*2c2f96dcSApple OSS Distributions }
4740*2c2f96dcSApple OSS Distributions 
4741*2c2f96dcSApple OSS Distributions boolean_t
workqueue_get_pwq_exceeded(void * v,boolean_t * exceeded_total,boolean_t * exceeded_constrained)4742*2c2f96dcSApple OSS Distributions workqueue_get_pwq_exceeded(void *v, boolean_t *exceeded_total,
4743*2c2f96dcSApple OSS Distributions     boolean_t *exceeded_constrained)
4744*2c2f96dcSApple OSS Distributions {
4745*2c2f96dcSApple OSS Distributions 	proc_t p = v;
4746*2c2f96dcSApple OSS Distributions 	struct proc_workqueueinfo pwqinfo;
4747*2c2f96dcSApple OSS Distributions 	int err;
4748*2c2f96dcSApple OSS Distributions 
4749*2c2f96dcSApple OSS Distributions 	assert(p != NULL);
4750*2c2f96dcSApple OSS Distributions 	assert(exceeded_total != NULL);
4751*2c2f96dcSApple OSS Distributions 	assert(exceeded_constrained != NULL);
4752*2c2f96dcSApple OSS Distributions 
4753*2c2f96dcSApple OSS Distributions 	err = fill_procworkqueue(p, &pwqinfo);
4754*2c2f96dcSApple OSS Distributions 	if (err) {
4755*2c2f96dcSApple OSS Distributions 		return FALSE;
4756*2c2f96dcSApple OSS Distributions 	}
4757*2c2f96dcSApple OSS Distributions 	if (!(pwqinfo.pwq_state & WQ_FLAGS_AVAILABLE)) {
4758*2c2f96dcSApple OSS Distributions 		return FALSE;
4759*2c2f96dcSApple OSS Distributions 	}
4760*2c2f96dcSApple OSS Distributions 
4761*2c2f96dcSApple OSS Distributions 	*exceeded_total = (pwqinfo.pwq_state & WQ_EXCEEDED_TOTAL_THREAD_LIMIT);
4762*2c2f96dcSApple OSS Distributions 	*exceeded_constrained = (pwqinfo.pwq_state & WQ_EXCEEDED_CONSTRAINED_THREAD_LIMIT);
4763*2c2f96dcSApple OSS Distributions 
4764*2c2f96dcSApple OSS Distributions 	return TRUE;
4765*2c2f96dcSApple OSS Distributions }
4766*2c2f96dcSApple OSS Distributions 
4767*2c2f96dcSApple OSS Distributions uint32_t
workqueue_get_pwq_state_kdp(void * v)4768*2c2f96dcSApple OSS Distributions workqueue_get_pwq_state_kdp(void * v)
4769*2c2f96dcSApple OSS Distributions {
4770*2c2f96dcSApple OSS Distributions 	static_assert((WQ_EXCEEDED_CONSTRAINED_THREAD_LIMIT << 17) ==
4771*2c2f96dcSApple OSS Distributions 	    kTaskWqExceededConstrainedThreadLimit);
4772*2c2f96dcSApple OSS Distributions 	static_assert((WQ_EXCEEDED_TOTAL_THREAD_LIMIT << 17) ==
4773*2c2f96dcSApple OSS Distributions 	    kTaskWqExceededTotalThreadLimit);
4774*2c2f96dcSApple OSS Distributions 	static_assert((WQ_FLAGS_AVAILABLE << 17) == kTaskWqFlagsAvailable);
4775*2c2f96dcSApple OSS Distributions 	static_assert((WQ_FLAGS_AVAILABLE | WQ_EXCEEDED_TOTAL_THREAD_LIMIT |
4776*2c2f96dcSApple OSS Distributions 	    WQ_EXCEEDED_CONSTRAINED_THREAD_LIMIT) == 0x7);
4777*2c2f96dcSApple OSS Distributions 
4778*2c2f96dcSApple OSS Distributions 	if (v == NULL) {
4779*2c2f96dcSApple OSS Distributions 		return 0;
4780*2c2f96dcSApple OSS Distributions 	}
4781*2c2f96dcSApple OSS Distributions 
4782*2c2f96dcSApple OSS Distributions 	proc_t p = v;
4783*2c2f96dcSApple OSS Distributions 	struct workqueue *wq = proc_get_wqptr(p);
4784*2c2f96dcSApple OSS Distributions 
4785*2c2f96dcSApple OSS Distributions 	if (wq == NULL || workq_lock_is_acquired_kdp(wq)) {
4786*2c2f96dcSApple OSS Distributions 		return 0;
4787*2c2f96dcSApple OSS Distributions 	}
4788*2c2f96dcSApple OSS Distributions 
4789*2c2f96dcSApple OSS Distributions 	uint32_t pwq_state = WQ_FLAGS_AVAILABLE;
4790*2c2f96dcSApple OSS Distributions 
4791*2c2f96dcSApple OSS Distributions 	if (wq->wq_constrained_threads_scheduled >= wq_max_constrained_threads) {
4792*2c2f96dcSApple OSS Distributions 		pwq_state |= WQ_EXCEEDED_CONSTRAINED_THREAD_LIMIT;
4793*2c2f96dcSApple OSS Distributions 	}
4794*2c2f96dcSApple OSS Distributions 
4795*2c2f96dcSApple OSS Distributions 	if (wq->wq_nthreads >= wq_max_threads) {
4796*2c2f96dcSApple OSS Distributions 		pwq_state |= WQ_EXCEEDED_TOTAL_THREAD_LIMIT;
4797*2c2f96dcSApple OSS Distributions 	}
4798*2c2f96dcSApple OSS Distributions 
4799*2c2f96dcSApple OSS Distributions 	return pwq_state;
4800*2c2f96dcSApple OSS Distributions }
4801*2c2f96dcSApple OSS Distributions 
4802*2c2f96dcSApple OSS Distributions void
workq_init(void)4803*2c2f96dcSApple OSS Distributions workq_init(void)
4804*2c2f96dcSApple OSS Distributions {
4805*2c2f96dcSApple OSS Distributions 	clock_interval_to_absolutetime_interval(wq_stalled_window.usecs,
4806*2c2f96dcSApple OSS Distributions 	    NSEC_PER_USEC, &wq_stalled_window.abstime);
4807*2c2f96dcSApple OSS Distributions 	clock_interval_to_absolutetime_interval(wq_reduce_pool_window.usecs,
4808*2c2f96dcSApple OSS Distributions 	    NSEC_PER_USEC, &wq_reduce_pool_window.abstime);
4809*2c2f96dcSApple OSS Distributions 	clock_interval_to_absolutetime_interval(wq_max_timer_interval.usecs,
4810*2c2f96dcSApple OSS Distributions 	    NSEC_PER_USEC, &wq_max_timer_interval.abstime);
4811*2c2f96dcSApple OSS Distributions 
4812*2c2f96dcSApple OSS Distributions 	thread_deallocate_daemon_register_queue(&workq_deallocate_queue,
4813*2c2f96dcSApple OSS Distributions 	    workq_deallocate_queue_invoke);
4814*2c2f96dcSApple OSS Distributions }
4815