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