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