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