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