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