]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/sched_prim.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / osfmk / kern / sched_prim.c
1 /*
2 * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_FREE_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58 /*
59 * File: sched_prim.c
60 * Author: Avadis Tevanian, Jr.
61 * Date: 1986
62 *
63 * Scheduling primitives
64 *
65 */
66
67 #include <debug.h>
68
69 #include <mach/mach_types.h>
70 #include <mach/machine.h>
71 #include <mach/policy.h>
72 #include <mach/sync_policy.h>
73 #include <mach/thread_act.h>
74
75 #include <machine/machine_routines.h>
76 #include <machine/sched_param.h>
77 #include <machine/machine_cpu.h>
78 #include <machine/limits.h>
79 #include <machine/atomic.h>
80
81 #include <machine/commpage.h>
82
83 #include <kern/kern_types.h>
84 #include <kern/backtrace.h>
85 #include <kern/clock.h>
86 #include <kern/cpu_number.h>
87 #include <kern/cpu_data.h>
88 #include <kern/smp.h>
89 #include <kern/debug.h>
90 #include <kern/macro_help.h>
91 #include <kern/machine.h>
92 #include <kern/misc_protos.h>
93 #if MONOTONIC
94 #include <kern/monotonic.h>
95 #endif /* MONOTONIC */
96 #include <kern/processor.h>
97 #include <kern/queue.h>
98 #include <kern/sched.h>
99 #include <kern/sched_prim.h>
100 #include <kern/sfi.h>
101 #include <kern/syscall_subr.h>
102 #include <kern/task.h>
103 #include <kern/thread.h>
104 #include <kern/ledger.h>
105 #include <kern/timer_queue.h>
106 #include <kern/waitq.h>
107 #include <kern/policy_internal.h>
108 #include <kern/cpu_quiesce.h>
109
110 #include <vm/pmap.h>
111 #include <vm/vm_kern.h>
112 #include <vm/vm_map.h>
113 #include <vm/vm_pageout.h>
114
115 #include <mach/sdt.h>
116 #include <mach/mach_host.h>
117 #include <mach/host_info.h>
118
119 #include <sys/kdebug.h>
120 #include <kperf/kperf.h>
121 #include <kern/kpc.h>
122 #include <san/kasan.h>
123 #include <kern/pms.h>
124 #include <kern/host.h>
125 #include <stdatomic.h>
126
127 struct sched_statistics PERCPU_DATA(sched_stats);
128 bool sched_stats_active;
129
130 int
131 rt_runq_count(processor_set_t pset)
132 {
133 return atomic_load_explicit(&SCHED(rt_runq)(pset)->count, memory_order_relaxed);
134 }
135
136 void
137 rt_runq_count_incr(processor_set_t pset)
138 {
139 atomic_fetch_add_explicit(&SCHED(rt_runq)(pset)->count, 1, memory_order_relaxed);
140 }
141
142 void
143 rt_runq_count_decr(processor_set_t pset)
144 {
145 atomic_fetch_sub_explicit(&SCHED(rt_runq)(pset)->count, 1, memory_order_relaxed);
146 }
147
148 #define DEFAULT_PREEMPTION_RATE 100 /* (1/s) */
149 TUNABLE(int, default_preemption_rate, "preempt", DEFAULT_PREEMPTION_RATE);
150
151 #define DEFAULT_BG_PREEMPTION_RATE 400 /* (1/s) */
152 TUNABLE(int, default_bg_preemption_rate, "bg_preempt", DEFAULT_BG_PREEMPTION_RATE);
153
154 #define MAX_UNSAFE_QUANTA 800
155 TUNABLE(int, max_unsafe_quanta, "unsafe", MAX_UNSAFE_QUANTA);
156
157 #define MAX_POLL_QUANTA 2
158 TUNABLE(int, max_poll_quanta, "poll", MAX_POLL_QUANTA);
159
160 #define SCHED_POLL_YIELD_SHIFT 4 /* 1/16 */
161 int sched_poll_yield_shift = SCHED_POLL_YIELD_SHIFT;
162
163 uint64_t max_poll_computation;
164
165 uint64_t max_unsafe_computation;
166 uint64_t sched_safe_duration;
167
168 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
169
170 uint32_t std_quantum;
171 uint32_t min_std_quantum;
172 uint32_t bg_quantum;
173
174 uint32_t std_quantum_us;
175 uint32_t bg_quantum_us;
176
177 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
178
179 uint32_t thread_depress_time;
180 uint32_t default_timeshare_computation;
181 uint32_t default_timeshare_constraint;
182
183 uint32_t max_rt_quantum;
184 uint32_t min_rt_quantum;
185
186 uint32_t rt_constraint_threshold;
187
188 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
189
190 unsigned sched_tick;
191 uint32_t sched_tick_interval;
192
193 /* Timeshare load calculation interval (15ms) */
194 uint32_t sched_load_compute_interval_us = 15000;
195 uint64_t sched_load_compute_interval_abs;
196 static _Atomic uint64_t sched_load_compute_deadline;
197
198 uint32_t sched_pri_shifts[TH_BUCKET_MAX];
199 uint32_t sched_fixed_shift;
200
201 uint32_t sched_decay_usage_age_factor = 1; /* accelerate 5/8^n usage aging */
202
203 /* Allow foreground to decay past default to resolve inversions */
204 #define DEFAULT_DECAY_BAND_LIMIT ((BASEPRI_FOREGROUND - BASEPRI_DEFAULT) + 2)
205 int sched_pri_decay_band_limit = DEFAULT_DECAY_BAND_LIMIT;
206
207 /* Defaults for timer deadline profiling */
208 #define TIMER_DEADLINE_TRACKING_BIN_1_DEFAULT 2000000 /* Timers with deadlines <=
209 * 2ms */
210 #define TIMER_DEADLINE_TRACKING_BIN_2_DEFAULT 5000000 /* Timers with deadlines
211 * <= 5ms */
212
213 uint64_t timer_deadline_tracking_bin_1;
214 uint64_t timer_deadline_tracking_bin_2;
215
216 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
217
218 thread_t sched_maintenance_thread;
219
220 /* interrupts disabled lock to guard recommended cores state */
221 decl_simple_lock_data(static, sched_recommended_cores_lock);
222 static uint64_t usercontrol_requested_recommended_cores = ALL_CORES_RECOMMENDED;
223 static void sched_update_recommended_cores(uint64_t recommended_cores);
224
225 #if __arm__ || __arm64__
226 static void sched_recommended_cores_maintenance(void);
227 uint64_t perfcontrol_failsafe_starvation_threshold;
228 extern char *proc_name_address(struct proc *p);
229 #endif /* __arm__ || __arm64__ */
230
231 uint64_t sched_one_second_interval;
232 boolean_t allow_direct_handoff = TRUE;
233
234 /* Forwards */
235
236 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
237
238 static void load_shift_init(void);
239 static void preempt_pri_init(void);
240
241 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
242
243 thread_t processor_idle(
244 thread_t thread,
245 processor_t processor);
246
247 static ast_t
248 csw_check_locked(
249 thread_t thread,
250 processor_t processor,
251 processor_set_t pset,
252 ast_t check_reason);
253
254 static void processor_setrun(
255 processor_t processor,
256 thread_t thread,
257 integer_t options);
258
259 static void
260 sched_realtime_timebase_init(void);
261
262 static void
263 sched_timer_deadline_tracking_init(void);
264
265 #if DEBUG
266 extern int debug_task;
267 #define TLOG(a, fmt, args...) if(debug_task & a) kprintf(fmt, ## args)
268 #else
269 #define TLOG(a, fmt, args...) do {} while (0)
270 #endif
271
272 static processor_t
273 thread_bind_internal(
274 thread_t thread,
275 processor_t processor);
276
277 static void
278 sched_vm_group_maintenance(void);
279
280 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
281 int8_t sched_load_shifts[NRQS];
282 bitmap_t sched_preempt_pri[BITMAP_LEN(NRQS_MAX)];
283 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
284
285 /*
286 * Statically allocate a buffer to hold the longest possible
287 * scheduler description string, as currently implemented.
288 * bsd/kern/kern_sysctl.c has a corresponding definition in bsd/
289 * to export to userspace via sysctl(3). If either version
290 * changes, update the other.
291 *
292 * Note that in addition to being an upper bound on the strings
293 * in the kernel, it's also an exact parameter to PE_get_default(),
294 * which interrogates the device tree on some platforms. That
295 * API requires the caller know the exact size of the device tree
296 * property, so we need both a legacy size (32) and the current size
297 * (48) to deal with old and new device trees. The device tree property
298 * is similarly padded to a fixed size so that the same kernel image
299 * can run on multiple devices with different schedulers configured
300 * in the device tree.
301 */
302 char sched_string[SCHED_STRING_MAX_LENGTH];
303
304 uint32_t sched_debug_flags = SCHED_DEBUG_FLAG_CHOOSE_PROCESSOR_TRACEPOINTS;
305
306 /* Global flag which indicates whether Background Stepper Context is enabled */
307 static int cpu_throttle_enabled = 1;
308
309 void
310 sched_init(void)
311 {
312 boolean_t direct_handoff = FALSE;
313 kprintf("Scheduler: Default of %s\n", SCHED(sched_name));
314
315 if (!PE_parse_boot_argn("sched_pri_decay_limit", &sched_pri_decay_band_limit, sizeof(sched_pri_decay_band_limit))) {
316 /* No boot-args, check in device tree */
317 if (!PE_get_default("kern.sched_pri_decay_limit",
318 &sched_pri_decay_band_limit,
319 sizeof(sched_pri_decay_band_limit))) {
320 /* Allow decay all the way to normal limits */
321 sched_pri_decay_band_limit = DEFAULT_DECAY_BAND_LIMIT;
322 }
323 }
324
325 kprintf("Setting scheduler priority decay band limit %d\n", sched_pri_decay_band_limit);
326
327 if (PE_parse_boot_argn("sched_debug", &sched_debug_flags, sizeof(sched_debug_flags))) {
328 kprintf("Scheduler: Debug flags 0x%08x\n", sched_debug_flags);
329 }
330 strlcpy(sched_string, SCHED(sched_name), sizeof(sched_string));
331
332 cpu_quiescent_counter_init();
333
334 SCHED(init)();
335 SCHED(rt_init)(&pset0);
336 sched_timer_deadline_tracking_init();
337
338 SCHED(pset_init)(&pset0);
339 SCHED(processor_init)(master_processor);
340
341 if (PE_parse_boot_argn("direct_handoff", &direct_handoff, sizeof(direct_handoff))) {
342 allow_direct_handoff = direct_handoff;
343 }
344 }
345
346 void
347 sched_timebase_init(void)
348 {
349 uint64_t abstime;
350
351 clock_interval_to_absolutetime_interval(1, NSEC_PER_SEC, &abstime);
352 sched_one_second_interval = abstime;
353
354 SCHED(timebase_init)();
355 sched_realtime_timebase_init();
356 }
357
358 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
359
360 void
361 sched_timeshare_init(void)
362 {
363 /*
364 * Calculate the timeslicing quantum
365 * in us.
366 */
367 if (default_preemption_rate < 1) {
368 default_preemption_rate = DEFAULT_PREEMPTION_RATE;
369 }
370 std_quantum_us = (1000 * 1000) / default_preemption_rate;
371
372 printf("standard timeslicing quantum is %d us\n", std_quantum_us);
373
374 if (default_bg_preemption_rate < 1) {
375 default_bg_preemption_rate = DEFAULT_BG_PREEMPTION_RATE;
376 }
377 bg_quantum_us = (1000 * 1000) / default_bg_preemption_rate;
378
379 printf("standard background quantum is %d us\n", bg_quantum_us);
380
381 load_shift_init();
382 preempt_pri_init();
383 sched_tick = 0;
384 }
385
386 void
387 sched_timeshare_timebase_init(void)
388 {
389 uint64_t abstime;
390 uint32_t shift;
391
392 /* standard timeslicing quantum */
393 clock_interval_to_absolutetime_interval(
394 std_quantum_us, NSEC_PER_USEC, &abstime);
395 assert((abstime >> 32) == 0 && (uint32_t)abstime != 0);
396 std_quantum = (uint32_t)abstime;
397
398 /* smallest remaining quantum (250 us) */
399 clock_interval_to_absolutetime_interval(250, NSEC_PER_USEC, &abstime);
400 assert((abstime >> 32) == 0 && (uint32_t)abstime != 0);
401 min_std_quantum = (uint32_t)abstime;
402
403 /* quantum for background tasks */
404 clock_interval_to_absolutetime_interval(
405 bg_quantum_us, NSEC_PER_USEC, &abstime);
406 assert((abstime >> 32) == 0 && (uint32_t)abstime != 0);
407 bg_quantum = (uint32_t)abstime;
408
409 /* scheduler tick interval */
410 clock_interval_to_absolutetime_interval(USEC_PER_SEC >> SCHED_TICK_SHIFT,
411 NSEC_PER_USEC, &abstime);
412 assert((abstime >> 32) == 0 && (uint32_t)abstime != 0);
413 sched_tick_interval = (uint32_t)abstime;
414
415 /* timeshare load calculation interval & deadline initialization */
416 clock_interval_to_absolutetime_interval(sched_load_compute_interval_us, NSEC_PER_USEC, &sched_load_compute_interval_abs);
417 os_atomic_init(&sched_load_compute_deadline, sched_load_compute_interval_abs);
418
419 /*
420 * Compute conversion factor from usage to
421 * timesharing priorities with 5/8 ** n aging.
422 */
423 abstime = (abstime * 5) / 3;
424 for (shift = 0; abstime > BASEPRI_DEFAULT; ++shift) {
425 abstime >>= 1;
426 }
427 sched_fixed_shift = shift;
428
429 for (uint32_t i = 0; i < TH_BUCKET_MAX; i++) {
430 sched_pri_shifts[i] = INT8_MAX;
431 }
432
433 max_unsafe_computation = ((uint64_t)max_unsafe_quanta) * std_quantum;
434 sched_safe_duration = 2 * ((uint64_t)max_unsafe_quanta) * std_quantum;
435
436 max_poll_computation = ((uint64_t)max_poll_quanta) * std_quantum;
437 thread_depress_time = 1 * std_quantum;
438 default_timeshare_computation = std_quantum / 2;
439 default_timeshare_constraint = std_quantum;
440
441 #if __arm__ || __arm64__
442 perfcontrol_failsafe_starvation_threshold = (2 * sched_tick_interval);
443 #endif /* __arm__ || __arm64__ */
444 }
445
446 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
447
448 void
449 pset_rt_init(processor_set_t pset)
450 {
451 os_atomic_init(&pset->rt_runq.count, 0);
452 queue_init(&pset->rt_runq.queue);
453 memset(&pset->rt_runq.runq_stats, 0, sizeof pset->rt_runq.runq_stats);
454 }
455
456 static void
457 sched_realtime_timebase_init(void)
458 {
459 uint64_t abstime;
460
461 /* smallest rt computaton (50 us) */
462 clock_interval_to_absolutetime_interval(50, NSEC_PER_USEC, &abstime);
463 assert((abstime >> 32) == 0 && (uint32_t)abstime != 0);
464 min_rt_quantum = (uint32_t)abstime;
465
466 /* maximum rt computation (50 ms) */
467 clock_interval_to_absolutetime_interval(
468 50, 1000 * NSEC_PER_USEC, &abstime);
469 assert((abstime >> 32) == 0 && (uint32_t)abstime != 0);
470 max_rt_quantum = (uint32_t)abstime;
471
472 /* constraint threshold for sending backup IPIs (4 ms) */
473 clock_interval_to_absolutetime_interval(4, NSEC_PER_MSEC, &abstime);
474 assert((abstime >> 32) == 0 && (uint32_t)abstime != 0);
475 rt_constraint_threshold = (uint32_t)abstime;
476 }
477
478 void
479 sched_check_spill(processor_set_t pset, thread_t thread)
480 {
481 (void)pset;
482 (void)thread;
483
484 return;
485 }
486
487 bool
488 sched_thread_should_yield(processor_t processor, thread_t thread)
489 {
490 (void)thread;
491
492 return !SCHED(processor_queue_empty)(processor) || rt_runq_count(processor->processor_set) > 0;
493 }
494
495 /* Default implementations of .steal_thread_enabled */
496 bool
497 sched_steal_thread_DISABLED(processor_set_t pset)
498 {
499 (void)pset;
500 return false;
501 }
502
503 bool
504 sched_steal_thread_enabled(processor_set_t pset)
505 {
506 return bit_count(pset->node->pset_map) > 1;
507 }
508
509 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
510
511 /*
512 * Set up values for timeshare
513 * loading factors.
514 */
515 static void
516 load_shift_init(void)
517 {
518 int8_t k, *p = sched_load_shifts;
519 uint32_t i, j;
520
521 uint32_t sched_decay_penalty = 1;
522
523 if (PE_parse_boot_argn("sched_decay_penalty", &sched_decay_penalty, sizeof(sched_decay_penalty))) {
524 kprintf("Overriding scheduler decay penalty %u\n", sched_decay_penalty);
525 }
526
527 if (PE_parse_boot_argn("sched_decay_usage_age_factor", &sched_decay_usage_age_factor, sizeof(sched_decay_usage_age_factor))) {
528 kprintf("Overriding scheduler decay usage age factor %u\n", sched_decay_usage_age_factor);
529 }
530
531 if (sched_decay_penalty == 0) {
532 /*
533 * There is no penalty for timeshare threads for using too much
534 * CPU, so set all load shifts to INT8_MIN. Even under high load,
535 * sched_pri_shift will be >INT8_MAX, and there will be no
536 * penalty applied to threads (nor will sched_usage be updated per
537 * thread).
538 */
539 for (i = 0; i < NRQS; i++) {
540 sched_load_shifts[i] = INT8_MIN;
541 }
542
543 return;
544 }
545
546 *p++ = INT8_MIN; *p++ = 0;
547
548 /*
549 * For a given system load "i", the per-thread priority
550 * penalty per quantum of CPU usage is ~2^k priority
551 * levels. "sched_decay_penalty" can cause more
552 * array entries to be filled with smaller "k" values
553 */
554 for (i = 2, j = 1 << sched_decay_penalty, k = 1; i < NRQS; ++k) {
555 for (j <<= 1; (i < j) && (i < NRQS); ++i) {
556 *p++ = k;
557 }
558 }
559 }
560
561 static void
562 preempt_pri_init(void)
563 {
564 bitmap_t *p = sched_preempt_pri;
565
566 for (int i = BASEPRI_FOREGROUND; i < MINPRI_KERNEL; ++i) {
567 bitmap_set(p, i);
568 }
569
570 for (int i = BASEPRI_PREEMPT; i <= MAXPRI; ++i) {
571 bitmap_set(p, i);
572 }
573 }
574
575 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
576
577 /*
578 * Thread wait timer expiration.
579 */
580 void
581 thread_timer_expire(
582 void *p0,
583 __unused void *p1)
584 {
585 thread_t thread = p0;
586 spl_t s;
587
588 assert_thread_magic(thread);
589
590 s = splsched();
591 thread_lock(thread);
592 if (--thread->wait_timer_active == 0) {
593 if (thread->wait_timer_is_set) {
594 thread->wait_timer_is_set = FALSE;
595 clear_wait_internal(thread, THREAD_TIMED_OUT);
596 }
597 }
598 thread_unlock(thread);
599 splx(s);
600 }
601
602 /*
603 * thread_unblock:
604 *
605 * Unblock thread on wake up.
606 *
607 * Returns TRUE if the thread should now be placed on the runqueue.
608 *
609 * Thread must be locked.
610 *
611 * Called at splsched().
612 */
613 boolean_t
614 thread_unblock(
615 thread_t thread,
616 wait_result_t wresult)
617 {
618 boolean_t ready_for_runq = FALSE;
619 thread_t cthread = current_thread();
620 uint32_t new_run_count;
621 int old_thread_state;
622
623 /*
624 * Set wait_result.
625 */
626 thread->wait_result = wresult;
627
628 /*
629 * Cancel pending wait timer.
630 */
631 if (thread->wait_timer_is_set) {
632 if (timer_call_cancel(&thread->wait_timer)) {
633 thread->wait_timer_active--;
634 }
635 thread->wait_timer_is_set = FALSE;
636 }
637
638 boolean_t aticontext, pidle;
639 ml_get_power_state(&aticontext, &pidle);
640
641 /*
642 * Update scheduling state: not waiting,
643 * set running.
644 */
645 old_thread_state = thread->state;
646 thread->state = (old_thread_state | TH_RUN) &
647 ~(TH_WAIT | TH_UNINT | TH_WAIT_REPORT);
648
649 if ((old_thread_state & TH_RUN) == 0) {
650 uint64_t ctime = mach_approximate_time();
651 thread->last_made_runnable_time = thread->last_basepri_change_time = ctime;
652 timer_start(&thread->runnable_timer, ctime);
653
654 ready_for_runq = TRUE;
655
656 if (old_thread_state & TH_WAIT_REPORT) {
657 (*thread->sched_call)(SCHED_CALL_UNBLOCK, thread);
658 }
659
660 /* Update the runnable thread count */
661 new_run_count = SCHED(run_count_incr)(thread);
662
663 #if CONFIG_SCHED_AUTO_JOIN
664 if (aticontext == FALSE && work_interval_should_propagate(cthread, thread)) {
665 work_interval_auto_join_propagate(cthread, thread);
666 }
667 #endif /*CONFIG_SCHED_AUTO_JOIN */
668 } else {
669 /*
670 * Either the thread is idling in place on another processor,
671 * or it hasn't finished context switching yet.
672 */
673 assert((thread->state & TH_IDLE) == 0);
674 /*
675 * The run count is only dropped after the context switch completes
676 * and the thread is still waiting, so we should not run_incr here
677 */
678 new_run_count = os_atomic_load(&sched_run_buckets[TH_BUCKET_RUN], relaxed);
679 }
680
681 /*
682 * Calculate deadline for real-time threads.
683 */
684 if (thread->sched_mode == TH_MODE_REALTIME) {
685 uint64_t ctime;
686
687 ctime = mach_absolute_time();
688 thread->realtime.deadline = thread->realtime.constraint + ctime;
689 KDBG(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SET_RT_DEADLINE) | DBG_FUNC_NONE,
690 (uintptr_t)thread_tid(thread), thread->realtime.deadline, thread->realtime.computation, 0);
691 }
692
693 /*
694 * Clear old quantum, fail-safe computation, etc.
695 */
696 thread->quantum_remaining = 0;
697 thread->computation_metered = 0;
698 thread->reason = AST_NONE;
699 thread->block_hint = kThreadWaitNone;
700
701 /* Obtain power-relevant interrupt and "platform-idle exit" statistics.
702 * We also account for "double hop" thread signaling via
703 * the thread callout infrastructure.
704 * DRK: consider removing the callout wakeup counters in the future
705 * they're present for verification at the moment.
706 */
707
708 if (__improbable(aticontext && !(thread_get_tag_internal(thread) & THREAD_TAG_CALLOUT))) {
709 DTRACE_SCHED2(iwakeup, struct thread *, thread, struct proc *, thread->task->bsd_info);
710
711 uint64_t ttd = current_processor()->timer_call_ttd;
712
713 if (ttd) {
714 if (ttd <= timer_deadline_tracking_bin_1) {
715 thread->thread_timer_wakeups_bin_1++;
716 } else if (ttd <= timer_deadline_tracking_bin_2) {
717 thread->thread_timer_wakeups_bin_2++;
718 }
719 }
720
721 ledger_credit_thread(thread, thread->t_ledger,
722 task_ledgers.interrupt_wakeups, 1);
723 if (pidle) {
724 ledger_credit_thread(thread, thread->t_ledger,
725 task_ledgers.platform_idle_wakeups, 1);
726 }
727 } else if (thread_get_tag_internal(cthread) & THREAD_TAG_CALLOUT) {
728 /* TODO: what about an interrupt that does a wake taken on a callout thread? */
729 if (cthread->callout_woken_from_icontext) {
730 ledger_credit_thread(thread, thread->t_ledger,
731 task_ledgers.interrupt_wakeups, 1);
732 thread->thread_callout_interrupt_wakeups++;
733
734 if (cthread->callout_woken_from_platform_idle) {
735 ledger_credit_thread(thread, thread->t_ledger,
736 task_ledgers.platform_idle_wakeups, 1);
737 thread->thread_callout_platform_idle_wakeups++;
738 }
739
740 cthread->callout_woke_thread = TRUE;
741 }
742 }
743
744 if (thread_get_tag_internal(thread) & THREAD_TAG_CALLOUT) {
745 thread->callout_woken_from_icontext = !!aticontext;
746 thread->callout_woken_from_platform_idle = !!pidle;
747 thread->callout_woke_thread = FALSE;
748 }
749
750 #if KPERF
751 if (ready_for_runq) {
752 kperf_make_runnable(thread, aticontext);
753 }
754 #endif /* KPERF */
755
756 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
757 MACHDBG_CODE(DBG_MACH_SCHED, MACH_MAKE_RUNNABLE) | DBG_FUNC_NONE,
758 (uintptr_t)thread_tid(thread), thread->sched_pri, thread->wait_result,
759 sched_run_buckets[TH_BUCKET_RUN], 0);
760
761 DTRACE_SCHED2(wakeup, struct thread *, thread, struct proc *, thread->task->bsd_info);
762
763 return ready_for_runq;
764 }
765
766 /*
767 * Routine: thread_allowed_for_handoff
768 * Purpose:
769 * Check if the thread is allowed for handoff operation
770 * Conditions:
771 * thread lock held, IPC locks may be held.
772 * TODO: In future, do not allow handoff if threads have different cluster
773 * recommendations.
774 */
775 boolean_t
776 thread_allowed_for_handoff(
777 thread_t thread)
778 {
779 thread_t self = current_thread();
780
781 if (allow_direct_handoff &&
782 thread->sched_mode == TH_MODE_REALTIME &&
783 self->sched_mode == TH_MODE_REALTIME) {
784 return TRUE;
785 }
786
787 return FALSE;
788 }
789
790 /*
791 * Routine: thread_go
792 * Purpose:
793 * Unblock and dispatch thread.
794 * Conditions:
795 * thread lock held, IPC locks may be held.
796 * thread must have been pulled from wait queue under same lock hold.
797 * thread must have been waiting
798 * Returns:
799 * KERN_SUCCESS - Thread was set running
800 *
801 * TODO: This should return void
802 */
803 kern_return_t
804 thread_go(
805 thread_t thread,
806 wait_result_t wresult,
807 waitq_options_t option)
808 {
809 thread_t self = current_thread();
810
811 assert_thread_magic(thread);
812
813 assert(thread->at_safe_point == FALSE);
814 assert(thread->wait_event == NO_EVENT64);
815 assert(thread->waitq == NULL);
816
817 assert(!(thread->state & (TH_TERMINATE | TH_TERMINATE2)));
818 assert(thread->state & TH_WAIT);
819
820
821 if (thread_unblock(thread, wresult)) {
822 #if SCHED_TRACE_THREAD_WAKEUPS
823 backtrace(&thread->thread_wakeup_bt[0],
824 (sizeof(thread->thread_wakeup_bt) / sizeof(uintptr_t)), NULL);
825 #endif
826 if ((option & WQ_OPTION_HANDOFF) &&
827 thread_allowed_for_handoff(thread)) {
828 thread_reference(thread);
829 assert(self->handoff_thread == NULL);
830 self->handoff_thread = thread;
831 } else {
832 thread_setrun(thread, SCHED_PREEMPT | SCHED_TAILQ);
833 }
834 }
835
836 return KERN_SUCCESS;
837 }
838
839 /*
840 * Routine: thread_mark_wait_locked
841 * Purpose:
842 * Mark a thread as waiting. If, given the circumstances,
843 * it doesn't want to wait (i.e. already aborted), then
844 * indicate that in the return value.
845 * Conditions:
846 * at splsched() and thread is locked.
847 */
848 __private_extern__
849 wait_result_t
850 thread_mark_wait_locked(
851 thread_t thread,
852 wait_interrupt_t interruptible_orig)
853 {
854 boolean_t at_safe_point;
855 wait_interrupt_t interruptible = interruptible_orig;
856
857 if (thread->state & TH_IDLE) {
858 panic("Invalid attempt to wait while running the idle thread");
859 }
860
861 assert(!(thread->state & (TH_WAIT | TH_IDLE | TH_UNINT | TH_TERMINATE2 | TH_WAIT_REPORT)));
862
863 /*
864 * The thread may have certain types of interrupts/aborts masked
865 * off. Even if the wait location says these types of interrupts
866 * are OK, we have to honor mask settings (outer-scoped code may
867 * not be able to handle aborts at the moment).
868 */
869 interruptible &= TH_OPT_INTMASK;
870 if (interruptible > (thread->options & TH_OPT_INTMASK)) {
871 interruptible = thread->options & TH_OPT_INTMASK;
872 }
873
874 at_safe_point = (interruptible == THREAD_ABORTSAFE);
875
876 if (interruptible == THREAD_UNINT ||
877 !(thread->sched_flags & TH_SFLAG_ABORT) ||
878 (!at_safe_point &&
879 (thread->sched_flags & TH_SFLAG_ABORTSAFELY))) {
880 if (!(thread->state & TH_TERMINATE)) {
881 DTRACE_SCHED(sleep);
882 }
883
884 int state_bits = TH_WAIT;
885 if (!interruptible) {
886 state_bits |= TH_UNINT;
887 }
888 if (thread->sched_call) {
889 wait_interrupt_t mask = THREAD_WAIT_NOREPORT_USER;
890 if (is_kerneltask(thread->task)) {
891 mask = THREAD_WAIT_NOREPORT_KERNEL;
892 }
893 if ((interruptible_orig & mask) == 0) {
894 state_bits |= TH_WAIT_REPORT;
895 }
896 }
897 thread->state |= state_bits;
898 thread->at_safe_point = at_safe_point;
899
900 /* TODO: pass this through assert_wait instead, have
901 * assert_wait just take a struct as an argument */
902 assert(!thread->block_hint);
903 thread->block_hint = thread->pending_block_hint;
904 thread->pending_block_hint = kThreadWaitNone;
905
906 return thread->wait_result = THREAD_WAITING;
907 } else {
908 if (thread->sched_flags & TH_SFLAG_ABORTSAFELY) {
909 thread->sched_flags &= ~TH_SFLAG_ABORTED_MASK;
910 }
911 }
912 thread->pending_block_hint = kThreadWaitNone;
913
914 return thread->wait_result = THREAD_INTERRUPTED;
915 }
916
917 /*
918 * Routine: thread_interrupt_level
919 * Purpose:
920 * Set the maximum interruptible state for the
921 * current thread. The effective value of any
922 * interruptible flag passed into assert_wait
923 * will never exceed this.
924 *
925 * Useful for code that must not be interrupted,
926 * but which calls code that doesn't know that.
927 * Returns:
928 * The old interrupt level for the thread.
929 */
930 __private_extern__
931 wait_interrupt_t
932 thread_interrupt_level(
933 wait_interrupt_t new_level)
934 {
935 thread_t thread = current_thread();
936 wait_interrupt_t result = thread->options & TH_OPT_INTMASK;
937
938 thread->options = (thread->options & ~TH_OPT_INTMASK) | (new_level & TH_OPT_INTMASK);
939
940 return result;
941 }
942
943 /*
944 * assert_wait:
945 *
946 * Assert that the current thread is about to go to
947 * sleep until the specified event occurs.
948 */
949 wait_result_t
950 assert_wait(
951 event_t event,
952 wait_interrupt_t interruptible)
953 {
954 if (__improbable(event == NO_EVENT)) {
955 panic("%s() called with NO_EVENT", __func__);
956 }
957
958 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
959 MACHDBG_CODE(DBG_MACH_SCHED, MACH_WAIT) | DBG_FUNC_NONE,
960 VM_KERNEL_UNSLIDE_OR_PERM(event), 0, 0, 0, 0);
961
962 struct waitq *waitq;
963 waitq = global_eventq(event);
964 return waitq_assert_wait64(waitq, CAST_EVENT64_T(event), interruptible, TIMEOUT_WAIT_FOREVER);
965 }
966
967 /*
968 * assert_wait_queue:
969 *
970 * Return the global waitq for the specified event
971 */
972 struct waitq *
973 assert_wait_queue(
974 event_t event)
975 {
976 return global_eventq(event);
977 }
978
979 wait_result_t
980 assert_wait_timeout(
981 event_t event,
982 wait_interrupt_t interruptible,
983 uint32_t interval,
984 uint32_t scale_factor)
985 {
986 thread_t thread = current_thread();
987 wait_result_t wresult;
988 uint64_t deadline;
989 spl_t s;
990
991 if (__improbable(event == NO_EVENT)) {
992 panic("%s() called with NO_EVENT", __func__);
993 }
994
995 struct waitq *waitq;
996 waitq = global_eventq(event);
997
998 s = splsched();
999 waitq_lock(waitq);
1000
1001 clock_interval_to_deadline(interval, scale_factor, &deadline);
1002
1003 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
1004 MACHDBG_CODE(DBG_MACH_SCHED, MACH_WAIT) | DBG_FUNC_NONE,
1005 VM_KERNEL_UNSLIDE_OR_PERM(event), interruptible, deadline, 0, 0);
1006
1007 wresult = waitq_assert_wait64_locked(waitq, CAST_EVENT64_T(event),
1008 interruptible,
1009 TIMEOUT_URGENCY_SYS_NORMAL,
1010 deadline, TIMEOUT_NO_LEEWAY,
1011 thread);
1012
1013 waitq_unlock(waitq);
1014 splx(s);
1015 return wresult;
1016 }
1017
1018 wait_result_t
1019 assert_wait_timeout_with_leeway(
1020 event_t event,
1021 wait_interrupt_t interruptible,
1022 wait_timeout_urgency_t urgency,
1023 uint32_t interval,
1024 uint32_t leeway,
1025 uint32_t scale_factor)
1026 {
1027 thread_t thread = current_thread();
1028 wait_result_t wresult;
1029 uint64_t deadline;
1030 uint64_t abstime;
1031 uint64_t slop;
1032 uint64_t now;
1033 spl_t s;
1034
1035 if (__improbable(event == NO_EVENT)) {
1036 panic("%s() called with NO_EVENT", __func__);
1037 }
1038
1039 now = mach_absolute_time();
1040 clock_interval_to_absolutetime_interval(interval, scale_factor, &abstime);
1041 deadline = now + abstime;
1042
1043 clock_interval_to_absolutetime_interval(leeway, scale_factor, &slop);
1044
1045 struct waitq *waitq;
1046 waitq = global_eventq(event);
1047
1048 s = splsched();
1049 waitq_lock(waitq);
1050
1051 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
1052 MACHDBG_CODE(DBG_MACH_SCHED, MACH_WAIT) | DBG_FUNC_NONE,
1053 VM_KERNEL_UNSLIDE_OR_PERM(event), interruptible, deadline, 0, 0);
1054
1055 wresult = waitq_assert_wait64_locked(waitq, CAST_EVENT64_T(event),
1056 interruptible,
1057 urgency, deadline, slop,
1058 thread);
1059
1060 waitq_unlock(waitq);
1061 splx(s);
1062 return wresult;
1063 }
1064
1065 wait_result_t
1066 assert_wait_deadline(
1067 event_t event,
1068 wait_interrupt_t interruptible,
1069 uint64_t deadline)
1070 {
1071 thread_t thread = current_thread();
1072 wait_result_t wresult;
1073 spl_t s;
1074
1075 if (__improbable(event == NO_EVENT)) {
1076 panic("%s() called with NO_EVENT", __func__);
1077 }
1078
1079 struct waitq *waitq;
1080 waitq = global_eventq(event);
1081
1082 s = splsched();
1083 waitq_lock(waitq);
1084
1085 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
1086 MACHDBG_CODE(DBG_MACH_SCHED, MACH_WAIT) | DBG_FUNC_NONE,
1087 VM_KERNEL_UNSLIDE_OR_PERM(event), interruptible, deadline, 0, 0);
1088
1089 wresult = waitq_assert_wait64_locked(waitq, CAST_EVENT64_T(event),
1090 interruptible,
1091 TIMEOUT_URGENCY_SYS_NORMAL, deadline,
1092 TIMEOUT_NO_LEEWAY, thread);
1093 waitq_unlock(waitq);
1094 splx(s);
1095 return wresult;
1096 }
1097
1098 wait_result_t
1099 assert_wait_deadline_with_leeway(
1100 event_t event,
1101 wait_interrupt_t interruptible,
1102 wait_timeout_urgency_t urgency,
1103 uint64_t deadline,
1104 uint64_t leeway)
1105 {
1106 thread_t thread = current_thread();
1107 wait_result_t wresult;
1108 spl_t s;
1109
1110 if (__improbable(event == NO_EVENT)) {
1111 panic("%s() called with NO_EVENT", __func__);
1112 }
1113
1114 struct waitq *waitq;
1115 waitq = global_eventq(event);
1116
1117 s = splsched();
1118 waitq_lock(waitq);
1119
1120 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
1121 MACHDBG_CODE(DBG_MACH_SCHED, MACH_WAIT) | DBG_FUNC_NONE,
1122 VM_KERNEL_UNSLIDE_OR_PERM(event), interruptible, deadline, 0, 0);
1123
1124 wresult = waitq_assert_wait64_locked(waitq, CAST_EVENT64_T(event),
1125 interruptible,
1126 urgency, deadline, leeway,
1127 thread);
1128 waitq_unlock(waitq);
1129 splx(s);
1130 return wresult;
1131 }
1132
1133 /*
1134 * thread_isoncpu:
1135 *
1136 * Return TRUE if a thread is running on a processor such that an AST
1137 * is needed to pull it out of userspace execution, or if executing in
1138 * the kernel, bring to a context switch boundary that would cause
1139 * thread state to be serialized in the thread PCB.
1140 *
1141 * Thread locked, returns the same way. While locked, fields
1142 * like "state" cannot change. "runq" can change only from set to unset.
1143 */
1144 static inline boolean_t
1145 thread_isoncpu(thread_t thread)
1146 {
1147 /* Not running or runnable */
1148 if (!(thread->state & TH_RUN)) {
1149 return FALSE;
1150 }
1151
1152 /* Waiting on a runqueue, not currently running */
1153 /* TODO: This is invalid - it can get dequeued without thread lock, but not context switched. */
1154 if (thread->runq != PROCESSOR_NULL) {
1155 return FALSE;
1156 }
1157
1158 /*
1159 * Thread does not have a stack yet
1160 * It could be on the stack alloc queue or preparing to be invoked
1161 */
1162 if (!thread->kernel_stack) {
1163 return FALSE;
1164 }
1165
1166 /*
1167 * Thread must be running on a processor, or
1168 * about to run, or just did run. In all these
1169 * cases, an AST to the processor is needed
1170 * to guarantee that the thread is kicked out
1171 * of userspace and the processor has
1172 * context switched (and saved register state).
1173 */
1174 return TRUE;
1175 }
1176
1177 /*
1178 * thread_stop:
1179 *
1180 * Force a preemption point for a thread and wait
1181 * for it to stop running on a CPU. If a stronger
1182 * guarantee is requested, wait until no longer
1183 * runnable. Arbitrates access among
1184 * multiple stop requests. (released by unstop)
1185 *
1186 * The thread must enter a wait state and stop via a
1187 * separate means.
1188 *
1189 * Returns FALSE if interrupted.
1190 */
1191 boolean_t
1192 thread_stop(
1193 thread_t thread,
1194 boolean_t until_not_runnable)
1195 {
1196 wait_result_t wresult;
1197 spl_t s = splsched();
1198 boolean_t oncpu;
1199
1200 wake_lock(thread);
1201 thread_lock(thread);
1202
1203 while (thread->state & TH_SUSP) {
1204 thread->wake_active = TRUE;
1205 thread_unlock(thread);
1206
1207 wresult = assert_wait(&thread->wake_active, THREAD_ABORTSAFE);
1208 wake_unlock(thread);
1209 splx(s);
1210
1211 if (wresult == THREAD_WAITING) {
1212 wresult = thread_block(THREAD_CONTINUE_NULL);
1213 }
1214
1215 if (wresult != THREAD_AWAKENED) {
1216 return FALSE;
1217 }
1218
1219 s = splsched();
1220 wake_lock(thread);
1221 thread_lock(thread);
1222 }
1223
1224 thread->state |= TH_SUSP;
1225
1226 while ((oncpu = thread_isoncpu(thread)) ||
1227 (until_not_runnable && (thread->state & TH_RUN))) {
1228 processor_t processor;
1229
1230 if (oncpu) {
1231 assert(thread->state & TH_RUN);
1232 processor = thread->chosen_processor;
1233 cause_ast_check(processor);
1234 }
1235
1236 thread->wake_active = TRUE;
1237 thread_unlock(thread);
1238
1239 wresult = assert_wait(&thread->wake_active, THREAD_ABORTSAFE);
1240 wake_unlock(thread);
1241 splx(s);
1242
1243 if (wresult == THREAD_WAITING) {
1244 wresult = thread_block(THREAD_CONTINUE_NULL);
1245 }
1246
1247 if (wresult != THREAD_AWAKENED) {
1248 thread_unstop(thread);
1249 return FALSE;
1250 }
1251
1252 s = splsched();
1253 wake_lock(thread);
1254 thread_lock(thread);
1255 }
1256
1257 thread_unlock(thread);
1258 wake_unlock(thread);
1259 splx(s);
1260
1261 /*
1262 * We return with the thread unlocked. To prevent it from
1263 * transitioning to a runnable state (or from TH_RUN to
1264 * being on the CPU), the caller must ensure the thread
1265 * is stopped via an external means (such as an AST)
1266 */
1267
1268 return TRUE;
1269 }
1270
1271 /*
1272 * thread_unstop:
1273 *
1274 * Release a previous stop request and set
1275 * the thread running if appropriate.
1276 *
1277 * Use only after a successful stop operation.
1278 */
1279 void
1280 thread_unstop(
1281 thread_t thread)
1282 {
1283 spl_t s = splsched();
1284
1285 wake_lock(thread);
1286 thread_lock(thread);
1287
1288 assert((thread->state & (TH_RUN | TH_WAIT | TH_SUSP)) != TH_SUSP);
1289
1290 if (thread->state & TH_SUSP) {
1291 thread->state &= ~TH_SUSP;
1292
1293 if (thread->wake_active) {
1294 thread->wake_active = FALSE;
1295 thread_unlock(thread);
1296
1297 thread_wakeup(&thread->wake_active);
1298 wake_unlock(thread);
1299 splx(s);
1300
1301 return;
1302 }
1303 }
1304
1305 thread_unlock(thread);
1306 wake_unlock(thread);
1307 splx(s);
1308 }
1309
1310 /*
1311 * thread_wait:
1312 *
1313 * Wait for a thread to stop running. (non-interruptible)
1314 *
1315 */
1316 void
1317 thread_wait(
1318 thread_t thread,
1319 boolean_t until_not_runnable)
1320 {
1321 wait_result_t wresult;
1322 boolean_t oncpu;
1323 processor_t processor;
1324 spl_t s = splsched();
1325
1326 wake_lock(thread);
1327 thread_lock(thread);
1328
1329 /*
1330 * Wait until not running on a CPU. If stronger requirement
1331 * desired, wait until not runnable. Assumption: if thread is
1332 * on CPU, then TH_RUN is set, so we're not waiting in any case
1333 * where the original, pure "TH_RUN" check would have let us
1334 * finish.
1335 */
1336 while ((oncpu = thread_isoncpu(thread)) ||
1337 (until_not_runnable && (thread->state & TH_RUN))) {
1338 if (oncpu) {
1339 assert(thread->state & TH_RUN);
1340 processor = thread->chosen_processor;
1341 cause_ast_check(processor);
1342 }
1343
1344 thread->wake_active = TRUE;
1345 thread_unlock(thread);
1346
1347 wresult = assert_wait(&thread->wake_active, THREAD_UNINT);
1348 wake_unlock(thread);
1349 splx(s);
1350
1351 if (wresult == THREAD_WAITING) {
1352 thread_block(THREAD_CONTINUE_NULL);
1353 }
1354
1355 s = splsched();
1356 wake_lock(thread);
1357 thread_lock(thread);
1358 }
1359
1360 thread_unlock(thread);
1361 wake_unlock(thread);
1362 splx(s);
1363 }
1364
1365 /*
1366 * Routine: clear_wait_internal
1367 *
1368 * Clear the wait condition for the specified thread.
1369 * Start the thread executing if that is appropriate.
1370 * Arguments:
1371 * thread thread to awaken
1372 * result Wakeup result the thread should see
1373 * Conditions:
1374 * At splsched
1375 * the thread is locked.
1376 * Returns:
1377 * KERN_SUCCESS thread was rousted out a wait
1378 * KERN_FAILURE thread was waiting but could not be rousted
1379 * KERN_NOT_WAITING thread was not waiting
1380 */
1381 __private_extern__ kern_return_t
1382 clear_wait_internal(
1383 thread_t thread,
1384 wait_result_t wresult)
1385 {
1386 uint32_t i = LockTimeOutUsec;
1387 struct waitq *waitq = thread->waitq;
1388
1389 do {
1390 if (wresult == THREAD_INTERRUPTED && (thread->state & TH_UNINT)) {
1391 return KERN_FAILURE;
1392 }
1393
1394 if (waitq != NULL) {
1395 if (!waitq_pull_thread_locked(waitq, thread)) {
1396 thread_unlock(thread);
1397 delay(1);
1398 if (i > 0 && !machine_timeout_suspended()) {
1399 i--;
1400 }
1401 thread_lock(thread);
1402 if (waitq != thread->waitq) {
1403 return KERN_NOT_WAITING;
1404 }
1405 continue;
1406 }
1407 }
1408
1409 /* TODO: Can we instead assert TH_TERMINATE is not set? */
1410 if ((thread->state & (TH_WAIT | TH_TERMINATE)) == TH_WAIT) {
1411 return thread_go(thread, wresult, WQ_OPTION_NONE);
1412 } else {
1413 return KERN_NOT_WAITING;
1414 }
1415 } while (i > 0);
1416
1417 panic("clear_wait_internal: deadlock: thread=%p, wq=%p, cpu=%d\n",
1418 thread, waitq, cpu_number());
1419
1420 return KERN_FAILURE;
1421 }
1422
1423
1424 /*
1425 * clear_wait:
1426 *
1427 * Clear the wait condition for the specified thread. Start the thread
1428 * executing if that is appropriate.
1429 *
1430 * parameters:
1431 * thread thread to awaken
1432 * result Wakeup result the thread should see
1433 */
1434 kern_return_t
1435 clear_wait(
1436 thread_t thread,
1437 wait_result_t result)
1438 {
1439 kern_return_t ret;
1440 spl_t s;
1441
1442 s = splsched();
1443 thread_lock(thread);
1444 ret = clear_wait_internal(thread, result);
1445 thread_unlock(thread);
1446 splx(s);
1447 return ret;
1448 }
1449
1450
1451 /*
1452 * thread_wakeup_prim:
1453 *
1454 * Common routine for thread_wakeup, thread_wakeup_with_result,
1455 * and thread_wakeup_one.
1456 *
1457 */
1458 kern_return_t
1459 thread_wakeup_prim(
1460 event_t event,
1461 boolean_t one_thread,
1462 wait_result_t result)
1463 {
1464 if (__improbable(event == NO_EVENT)) {
1465 panic("%s() called with NO_EVENT", __func__);
1466 }
1467
1468 struct waitq *wq = global_eventq(event);
1469
1470 if (one_thread) {
1471 return waitq_wakeup64_one(wq, CAST_EVENT64_T(event), result, WAITQ_ALL_PRIORITIES);
1472 } else {
1473 return waitq_wakeup64_all(wq, CAST_EVENT64_T(event), result, WAITQ_ALL_PRIORITIES);
1474 }
1475 }
1476
1477 /*
1478 * Wakeup a specified thread if and only if it's waiting for this event
1479 */
1480 kern_return_t
1481 thread_wakeup_thread(
1482 event_t event,
1483 thread_t thread)
1484 {
1485 if (__improbable(event == NO_EVENT)) {
1486 panic("%s() called with NO_EVENT", __func__);
1487 }
1488
1489 if (__improbable(thread == THREAD_NULL)) {
1490 panic("%s() called with THREAD_NULL", __func__);
1491 }
1492
1493 struct waitq *wq = global_eventq(event);
1494
1495 return waitq_wakeup64_thread(wq, CAST_EVENT64_T(event), thread, THREAD_AWAKENED);
1496 }
1497
1498 /*
1499 * Wakeup a thread waiting on an event and promote it to a priority.
1500 *
1501 * Requires woken thread to un-promote itself when done.
1502 */
1503 kern_return_t
1504 thread_wakeup_one_with_pri(
1505 event_t event,
1506 int priority)
1507 {
1508 if (__improbable(event == NO_EVENT)) {
1509 panic("%s() called with NO_EVENT", __func__);
1510 }
1511
1512 struct waitq *wq = global_eventq(event);
1513
1514 return waitq_wakeup64_one(wq, CAST_EVENT64_T(event), THREAD_AWAKENED, priority);
1515 }
1516
1517 /*
1518 * Wakeup a thread waiting on an event,
1519 * promote it to a priority,
1520 * and return a reference to the woken thread.
1521 *
1522 * Requires woken thread to un-promote itself when done.
1523 */
1524 thread_t
1525 thread_wakeup_identify(event_t event,
1526 int priority)
1527 {
1528 if (__improbable(event == NO_EVENT)) {
1529 panic("%s() called with NO_EVENT", __func__);
1530 }
1531
1532 struct waitq *wq = global_eventq(event);
1533
1534 return waitq_wakeup64_identify(wq, CAST_EVENT64_T(event), THREAD_AWAKENED, priority);
1535 }
1536
1537 /*
1538 * thread_bind:
1539 *
1540 * Force the current thread to execute on the specified processor.
1541 * Takes effect after the next thread_block().
1542 *
1543 * Returns the previous binding. PROCESSOR_NULL means
1544 * not bound.
1545 *
1546 * XXX - DO NOT export this to users - XXX
1547 */
1548 processor_t
1549 thread_bind(
1550 processor_t processor)
1551 {
1552 thread_t self = current_thread();
1553 processor_t prev;
1554 spl_t s;
1555
1556 s = splsched();
1557 thread_lock(self);
1558
1559 prev = thread_bind_internal(self, processor);
1560
1561 thread_unlock(self);
1562 splx(s);
1563
1564 return prev;
1565 }
1566
1567 /*
1568 * thread_bind_internal:
1569 *
1570 * If the specified thread is not the current thread, and it is currently
1571 * running on another CPU, a remote AST must be sent to that CPU to cause
1572 * the thread to migrate to its bound processor. Otherwise, the migration
1573 * will occur at the next quantum expiration or blocking point.
1574 *
1575 * When the thread is the current thread, and explicit thread_block() should
1576 * be used to force the current processor to context switch away and
1577 * let the thread migrate to the bound processor.
1578 *
1579 * Thread must be locked, and at splsched.
1580 */
1581
1582 static processor_t
1583 thread_bind_internal(
1584 thread_t thread,
1585 processor_t processor)
1586 {
1587 processor_t prev;
1588
1589 /* <rdar://problem/15102234> */
1590 assert(thread->sched_pri < BASEPRI_RTQUEUES);
1591 /* A thread can't be bound if it's sitting on a (potentially incorrect) runqueue */
1592 assert(thread->runq == PROCESSOR_NULL);
1593
1594 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_THREAD_BIND), thread_tid(thread), processor ? (uintptr_t)processor->cpu_id : (uintptr_t)-1, 0, 0, 0);
1595
1596 prev = thread->bound_processor;
1597 thread->bound_processor = processor;
1598
1599 return prev;
1600 }
1601
1602 /*
1603 * thread_vm_bind_group_add:
1604 *
1605 * The "VM bind group" is a special mechanism to mark a collection
1606 * of threads from the VM subsystem that, in general, should be scheduled
1607 * with only one CPU of parallelism. To accomplish this, we initially
1608 * bind all the threads to the master processor, which has the effect
1609 * that only one of the threads in the group can execute at once, including
1610 * preempting threads in the group that are a lower priority. Future
1611 * mechanisms may use more dynamic mechanisms to prevent the collection
1612 * of VM threads from using more CPU time than desired.
1613 *
1614 * The current implementation can result in priority inversions where
1615 * compute-bound priority 95 or realtime threads that happen to have
1616 * landed on the master processor prevent the VM threads from running.
1617 * When this situation is detected, we unbind the threads for one
1618 * scheduler tick to allow the scheduler to run the threads an
1619 * additional CPUs, before restoring the binding (assuming high latency
1620 * is no longer a problem).
1621 */
1622
1623 /*
1624 * The current max is provisioned for:
1625 * vm_compressor_swap_trigger_thread (92)
1626 * 2 x vm_pageout_iothread_internal (92) when vm_restricted_to_single_processor==TRUE
1627 * vm_pageout_continue (92)
1628 * memorystatus_thread (95)
1629 */
1630 #define MAX_VM_BIND_GROUP_COUNT (5)
1631 decl_simple_lock_data(static, sched_vm_group_list_lock);
1632 static thread_t sched_vm_group_thread_list[MAX_VM_BIND_GROUP_COUNT];
1633 static int sched_vm_group_thread_count;
1634 static boolean_t sched_vm_group_temporarily_unbound = FALSE;
1635
1636 void
1637 thread_vm_bind_group_add(void)
1638 {
1639 thread_t self = current_thread();
1640
1641 thread_reference_internal(self);
1642 self->options |= TH_OPT_SCHED_VM_GROUP;
1643
1644 simple_lock(&sched_vm_group_list_lock, LCK_GRP_NULL);
1645 assert(sched_vm_group_thread_count < MAX_VM_BIND_GROUP_COUNT);
1646 sched_vm_group_thread_list[sched_vm_group_thread_count++] = self;
1647 simple_unlock(&sched_vm_group_list_lock);
1648
1649 thread_bind(master_processor);
1650
1651 /* Switch to bound processor if not already there */
1652 thread_block(THREAD_CONTINUE_NULL);
1653 }
1654
1655 static void
1656 sched_vm_group_maintenance(void)
1657 {
1658 uint64_t ctime = mach_absolute_time();
1659 uint64_t longtime = ctime - sched_tick_interval;
1660 int i;
1661 spl_t s;
1662 boolean_t high_latency_observed = FALSE;
1663 boolean_t runnable_and_not_on_runq_observed = FALSE;
1664 boolean_t bind_target_changed = FALSE;
1665 processor_t bind_target = PROCESSOR_NULL;
1666
1667 /* Make sure nobody attempts to add new threads while we are enumerating them */
1668 simple_lock(&sched_vm_group_list_lock, LCK_GRP_NULL);
1669
1670 s = splsched();
1671
1672 for (i = 0; i < sched_vm_group_thread_count; i++) {
1673 thread_t thread = sched_vm_group_thread_list[i];
1674 assert(thread != THREAD_NULL);
1675 thread_lock(thread);
1676 if ((thread->state & (TH_RUN | TH_WAIT)) == TH_RUN) {
1677 if (thread->runq != PROCESSOR_NULL && thread->last_made_runnable_time < longtime) {
1678 high_latency_observed = TRUE;
1679 } else if (thread->runq == PROCESSOR_NULL) {
1680 /* There are some cases where a thread be transitiong that also fall into this case */
1681 runnable_and_not_on_runq_observed = TRUE;
1682 }
1683 }
1684 thread_unlock(thread);
1685
1686 if (high_latency_observed && runnable_and_not_on_runq_observed) {
1687 /* All the things we are looking for are true, stop looking */
1688 break;
1689 }
1690 }
1691
1692 splx(s);
1693
1694 if (sched_vm_group_temporarily_unbound) {
1695 /* If we turned off binding, make sure everything is OK before rebinding */
1696 if (!high_latency_observed) {
1697 /* rebind */
1698 bind_target_changed = TRUE;
1699 bind_target = master_processor;
1700 sched_vm_group_temporarily_unbound = FALSE; /* might be reset to TRUE if change cannot be completed */
1701 }
1702 } else {
1703 /*
1704 * Check if we're in a bad state, which is defined by high
1705 * latency with no core currently executing a thread. If a
1706 * single thread is making progress on a CPU, that means the
1707 * binding concept to reduce parallelism is working as
1708 * designed.
1709 */
1710 if (high_latency_observed && !runnable_and_not_on_runq_observed) {
1711 /* unbind */
1712 bind_target_changed = TRUE;
1713 bind_target = PROCESSOR_NULL;
1714 sched_vm_group_temporarily_unbound = TRUE;
1715 }
1716 }
1717
1718 if (bind_target_changed) {
1719 s = splsched();
1720 for (i = 0; i < sched_vm_group_thread_count; i++) {
1721 thread_t thread = sched_vm_group_thread_list[i];
1722 boolean_t removed;
1723 assert(thread != THREAD_NULL);
1724
1725 thread_lock(thread);
1726 removed = thread_run_queue_remove(thread);
1727 if (removed || ((thread->state & (TH_RUN | TH_WAIT)) == TH_WAIT)) {
1728 thread_bind_internal(thread, bind_target);
1729 } else {
1730 /*
1731 * Thread was in the middle of being context-switched-to,
1732 * or was in the process of blocking. To avoid switching the bind
1733 * state out mid-flight, defer the change if possible.
1734 */
1735 if (bind_target == PROCESSOR_NULL) {
1736 thread_bind_internal(thread, bind_target);
1737 } else {
1738 sched_vm_group_temporarily_unbound = TRUE; /* next pass will try again */
1739 }
1740 }
1741
1742 if (removed) {
1743 thread_run_queue_reinsert(thread, SCHED_PREEMPT | SCHED_TAILQ);
1744 }
1745 thread_unlock(thread);
1746 }
1747 splx(s);
1748 }
1749
1750 simple_unlock(&sched_vm_group_list_lock);
1751 }
1752
1753 /* Invoked prior to idle entry to determine if, on SMT capable processors, an SMT
1754 * rebalancing opportunity exists when a core is (instantaneously) idle, but
1755 * other SMT-capable cores may be over-committed. TODO: some possible negatives:
1756 * IPI thrash if this core does not remain idle following the load balancing ASTs
1757 * Idle "thrash", when IPI issue is followed by idle entry/core power down
1758 * followed by a wakeup shortly thereafter.
1759 */
1760
1761 #if (DEVELOPMENT || DEBUG)
1762 int sched_smt_balance = 1;
1763 #endif
1764
1765 /* Invoked with pset locked, returns with pset unlocked */
1766 void
1767 sched_SMT_balance(processor_t cprocessor, processor_set_t cpset)
1768 {
1769 processor_t ast_processor = NULL;
1770
1771 #if (DEVELOPMENT || DEBUG)
1772 if (__improbable(sched_smt_balance == 0)) {
1773 goto smt_balance_exit;
1774 }
1775 #endif
1776
1777 assert(cprocessor == current_processor());
1778 if (cprocessor->is_SMT == FALSE) {
1779 goto smt_balance_exit;
1780 }
1781
1782 processor_t sib_processor = cprocessor->processor_secondary ? cprocessor->processor_secondary : cprocessor->processor_primary;
1783
1784 /* Determine if both this processor and its sibling are idle,
1785 * indicating an SMT rebalancing opportunity.
1786 */
1787 if (sib_processor->state != PROCESSOR_IDLE) {
1788 goto smt_balance_exit;
1789 }
1790
1791 processor_t sprocessor;
1792
1793 sched_ipi_type_t ipi_type = SCHED_IPI_NONE;
1794 uint64_t running_secondary_map = (cpset->cpu_state_map[PROCESSOR_RUNNING] &
1795 ~cpset->primary_map);
1796 for (int cpuid = lsb_first(running_secondary_map); cpuid >= 0; cpuid = lsb_next(running_secondary_map, cpuid)) {
1797 sprocessor = processor_array[cpuid];
1798 if ((sprocessor->processor_primary->state == PROCESSOR_RUNNING) &&
1799 (sprocessor->current_pri < BASEPRI_RTQUEUES)) {
1800 ipi_type = sched_ipi_action(sprocessor, NULL, false, SCHED_IPI_EVENT_SMT_REBAL);
1801 if (ipi_type != SCHED_IPI_NONE) {
1802 assert(sprocessor != cprocessor);
1803 ast_processor = sprocessor;
1804 break;
1805 }
1806 }
1807 }
1808
1809 smt_balance_exit:
1810 pset_unlock(cpset);
1811
1812 if (ast_processor) {
1813 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_SMT_BALANCE), ast_processor->cpu_id, ast_processor->state, ast_processor->processor_primary->state, 0, 0);
1814 sched_ipi_perform(ast_processor, ipi_type);
1815 }
1816 }
1817
1818 static cpumap_t
1819 pset_available_cpumap(processor_set_t pset)
1820 {
1821 return (pset->cpu_state_map[PROCESSOR_IDLE] | pset->cpu_state_map[PROCESSOR_DISPATCHING] | pset->cpu_state_map[PROCESSOR_RUNNING]) &
1822 pset->recommended_bitmask;
1823 }
1824
1825 static cpumap_t
1826 pset_available_but_not_running_cpumap(processor_set_t pset)
1827 {
1828 return (pset->cpu_state_map[PROCESSOR_IDLE] | pset->cpu_state_map[PROCESSOR_DISPATCHING]) &
1829 pset->recommended_bitmask;
1830 }
1831
1832 bool
1833 pset_has_stealable_threads(processor_set_t pset)
1834 {
1835 pset_assert_locked(pset);
1836
1837 cpumap_t avail_map = pset_available_but_not_running_cpumap(pset);
1838 /*
1839 * Secondary CPUs never steal, so allow stealing of threads if there are more threads than
1840 * available primary CPUs
1841 */
1842 avail_map &= pset->primary_map;
1843
1844 return (pset->pset_runq.count > 0) && ((pset->pset_runq.count + rt_runq_count(pset)) > bit_count(avail_map));
1845 }
1846
1847 /*
1848 * Called with pset locked, on a processor that is committing to run a new thread
1849 * Will transition an idle or dispatching processor to running as it picks up
1850 * the first new thread from the idle thread.
1851 */
1852 static void
1853 pset_commit_processor_to_new_thread(processor_set_t pset, processor_t processor, thread_t new_thread)
1854 {
1855 pset_assert_locked(pset);
1856
1857 if (processor->state == PROCESSOR_DISPATCHING || processor->state == PROCESSOR_IDLE) {
1858 assert(current_thread() == processor->idle_thread);
1859
1860 /*
1861 * Dispatching processor is now committed to running new_thread,
1862 * so change its state to PROCESSOR_RUNNING.
1863 */
1864 pset_update_processor_state(pset, processor, PROCESSOR_RUNNING);
1865 } else {
1866 assert((processor->state == PROCESSOR_RUNNING) || (processor->state == PROCESSOR_SHUTDOWN));
1867 }
1868
1869 processor_state_update_from_thread(processor, new_thread);
1870
1871 if (new_thread->sched_pri >= BASEPRI_RTQUEUES) {
1872 bit_set(pset->realtime_map, processor->cpu_id);
1873 } else {
1874 bit_clear(pset->realtime_map, processor->cpu_id);
1875 }
1876
1877 pset_node_t node = pset->node;
1878
1879 if (bit_count(node->pset_map) == 1) {
1880 /* Node has only a single pset, so skip node pset map updates */
1881 return;
1882 }
1883
1884 cpumap_t avail_map = pset_available_cpumap(pset);
1885
1886 if (new_thread->sched_pri >= BASEPRI_RTQUEUES) {
1887 if ((avail_map & pset->realtime_map) == avail_map) {
1888 /* No more non-RT CPUs in this pset */
1889 atomic_bit_clear(&node->pset_non_rt_map, pset->pset_id, memory_order_relaxed);
1890 }
1891 avail_map &= pset->primary_map;
1892 if ((avail_map & pset->realtime_map) == avail_map) {
1893 /* No more non-RT primary CPUs in this pset */
1894 atomic_bit_clear(&node->pset_non_rt_primary_map, pset->pset_id, memory_order_relaxed);
1895 }
1896 } else {
1897 if ((avail_map & pset->realtime_map) != avail_map) {
1898 if (!bit_test(atomic_load(&node->pset_non_rt_map), pset->pset_id)) {
1899 atomic_bit_set(&node->pset_non_rt_map, pset->pset_id, memory_order_relaxed);
1900 }
1901 }
1902 avail_map &= pset->primary_map;
1903 if ((avail_map & pset->realtime_map) != avail_map) {
1904 if (!bit_test(atomic_load(&node->pset_non_rt_primary_map), pset->pset_id)) {
1905 atomic_bit_set(&node->pset_non_rt_primary_map, pset->pset_id, memory_order_relaxed);
1906 }
1907 }
1908 }
1909 }
1910
1911 static processor_t choose_processor_for_realtime_thread(processor_set_t pset, processor_t skip_processor, bool consider_secondaries);
1912 static bool all_available_primaries_are_running_realtime_threads(processor_set_t pset);
1913 #if defined(__x86_64__)
1914 static bool these_processors_are_running_realtime_threads(processor_set_t pset, uint64_t these_map);
1915 #endif
1916 static bool sched_ok_to_run_realtime_thread(processor_set_t pset, processor_t processor);
1917 static bool processor_is_fast_track_candidate_for_realtime_thread(processor_set_t pset, processor_t processor);
1918 int sched_allow_rt_smt = 1;
1919 int sched_avoid_cpu0 = 1;
1920
1921 /*
1922 * thread_select:
1923 *
1924 * Select a new thread for the current processor to execute.
1925 *
1926 * May select the current thread, which must be locked.
1927 */
1928 static thread_t
1929 thread_select(thread_t thread,
1930 processor_t processor,
1931 ast_t *reason)
1932 {
1933 processor_set_t pset = processor->processor_set;
1934 thread_t new_thread = THREAD_NULL;
1935
1936 assert(processor == current_processor());
1937 assert((thread->state & (TH_RUN | TH_TERMINATE2)) == TH_RUN);
1938
1939 do {
1940 /*
1941 * Update the priority.
1942 */
1943 if (SCHED(can_update_priority)(thread)) {
1944 SCHED(update_priority)(thread);
1945 }
1946
1947 pset_lock(pset);
1948
1949 processor_state_update_from_thread(processor, thread);
1950
1951 restart:
1952 /* Acknowledge any pending IPIs here with pset lock held */
1953 bit_clear(pset->pending_AST_URGENT_cpu_mask, processor->cpu_id);
1954 bit_clear(pset->pending_AST_PREEMPT_cpu_mask, processor->cpu_id);
1955
1956 #if defined(CONFIG_SCHED_DEFERRED_AST)
1957 bit_clear(pset->pending_deferred_AST_cpu_mask, processor->cpu_id);
1958 #endif
1959
1960 bool secondary_can_only_run_realtime_thread = false;
1961
1962 assert(processor->state != PROCESSOR_OFF_LINE);
1963
1964 if (!processor->is_recommended) {
1965 /*
1966 * The performance controller has provided a hint to not dispatch more threads,
1967 * unless they are bound to us (and thus we are the only option
1968 */
1969 if (!SCHED(processor_bound_count)(processor)) {
1970 goto idle;
1971 }
1972 } else if (processor->processor_primary != processor) {
1973 /*
1974 * Should this secondary SMT processor attempt to find work? For pset runqueue systems,
1975 * we should look for work only under the same conditions that choose_processor()
1976 * would have assigned work, which is when all primary processors have been assigned work.
1977 *
1978 * An exception is that bound threads are dispatched to a processor without going through
1979 * choose_processor(), so in those cases we should continue trying to dequeue work.
1980 */
1981 if (!SCHED(processor_bound_count)(processor)) {
1982 if ((pset->recommended_bitmask & pset->primary_map & pset->cpu_state_map[PROCESSOR_IDLE]) != 0) {
1983 goto idle;
1984 }
1985
1986 /*
1987 * TODO: What if a secondary core beat an idle primary to waking up from an IPI?
1988 * Should it dequeue immediately, or spin waiting for the primary to wake up?
1989 */
1990
1991 /* There are no idle primaries */
1992
1993 if (processor->processor_primary->current_pri >= BASEPRI_RTQUEUES) {
1994 bool secondary_can_run_realtime_thread = sched_allow_rt_smt && rt_runq_count(pset) && all_available_primaries_are_running_realtime_threads(pset);
1995 if (!secondary_can_run_realtime_thread) {
1996 goto idle;
1997 }
1998 secondary_can_only_run_realtime_thread = true;
1999 }
2000 }
2001 }
2002
2003 /*
2004 * Test to see if the current thread should continue
2005 * to run on this processor. Must not be attempting to wait, and not
2006 * bound to a different processor, nor be in the wrong
2007 * processor set, nor be forced to context switch by TH_SUSP.
2008 *
2009 * Note that there are never any RT threads in the regular runqueue.
2010 *
2011 * This code is very insanely tricky.
2012 */
2013
2014 /* i.e. not waiting, not TH_SUSP'ed */
2015 bool still_running = ((thread->state & (TH_TERMINATE | TH_IDLE | TH_WAIT | TH_RUN | TH_SUSP)) == TH_RUN);
2016
2017 /*
2018 * Threads running on SMT processors are forced to context switch. Don't rebalance realtime threads.
2019 * TODO: This should check if it's worth it to rebalance, i.e. 'are there any idle primary processors'
2020 * <rdar://problem/47907700>
2021 *
2022 * A yielding thread shouldn't be forced to context switch.
2023 */
2024
2025 bool is_yielding = (*reason & AST_YIELD) == AST_YIELD;
2026
2027 bool needs_smt_rebalance = !is_yielding && thread->sched_pri < BASEPRI_RTQUEUES && processor->processor_primary != processor;
2028
2029 bool affinity_mismatch = thread->affinity_set != AFFINITY_SET_NULL && thread->affinity_set->aset_pset != pset;
2030
2031 bool bound_elsewhere = thread->bound_processor != PROCESSOR_NULL && thread->bound_processor != processor;
2032
2033 bool avoid_processor = !is_yielding && SCHED(avoid_processor_enabled) && SCHED(thread_avoid_processor)(processor, thread);
2034
2035 if (still_running && !needs_smt_rebalance && !affinity_mismatch && !bound_elsewhere && !avoid_processor) {
2036 /*
2037 * This thread is eligible to keep running on this processor.
2038 *
2039 * RT threads with un-expired quantum stay on processor,
2040 * unless there's a valid RT thread with an earlier deadline.
2041 */
2042 if (thread->sched_pri >= BASEPRI_RTQUEUES && processor->first_timeslice) {
2043 if (rt_runq_count(pset) > 0) {
2044 thread_t next_rt = qe_queue_first(&SCHED(rt_runq)(pset)->queue, struct thread, runq_links);
2045
2046 if (next_rt->realtime.deadline < processor->deadline &&
2047 (next_rt->bound_processor == PROCESSOR_NULL ||
2048 next_rt->bound_processor == processor)) {
2049 /* The next RT thread is better, so pick it off the runqueue. */
2050 goto pick_new_rt_thread;
2051 }
2052 }
2053
2054 /* This is still the best RT thread to run. */
2055 processor->deadline = thread->realtime.deadline;
2056
2057 sched_update_pset_load_average(pset, 0);
2058
2059 processor_t next_rt_processor = PROCESSOR_NULL;
2060 sched_ipi_type_t next_rt_ipi_type = SCHED_IPI_NONE;
2061
2062 if (rt_runq_count(pset) - bit_count(pset->pending_AST_URGENT_cpu_mask) > 0) {
2063 next_rt_processor = choose_processor_for_realtime_thread(pset, processor, true);
2064 if (next_rt_processor) {
2065 SCHED_DEBUG_CHOOSE_PROCESSOR_KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_CHOOSE_PROCESSOR) | DBG_FUNC_NONE,
2066 (uintptr_t)0, (uintptr_t)-4, next_rt_processor->cpu_id, next_rt_processor->state, 0);
2067 if (next_rt_processor->state == PROCESSOR_IDLE) {
2068 pset_update_processor_state(pset, next_rt_processor, PROCESSOR_DISPATCHING);
2069 }
2070 next_rt_ipi_type = sched_ipi_action(next_rt_processor, NULL, false, SCHED_IPI_EVENT_PREEMPT);
2071 }
2072 }
2073 pset_unlock(pset);
2074
2075 if (next_rt_processor) {
2076 sched_ipi_perform(next_rt_processor, next_rt_ipi_type);
2077 }
2078
2079 return thread;
2080 }
2081
2082 if ((rt_runq_count(pset) == 0) &&
2083 SCHED(processor_queue_has_priority)(processor, thread->sched_pri, TRUE) == FALSE) {
2084 /* This thread is still the highest priority runnable (non-idle) thread */
2085 processor->deadline = UINT64_MAX;
2086
2087 sched_update_pset_load_average(pset, 0);
2088 pset_unlock(pset);
2089
2090 return thread;
2091 }
2092 } else {
2093 /*
2094 * This processor must context switch.
2095 * If it's due to a rebalance, we should aggressively find this thread a new home.
2096 */
2097 if (needs_smt_rebalance || affinity_mismatch || bound_elsewhere || avoid_processor) {
2098 *reason |= AST_REBALANCE;
2099 }
2100 }
2101
2102 bool secondary_forced_idle = ((processor->processor_secondary != PROCESSOR_NULL) &&
2103 (thread_no_smt(thread) || (thread->sched_pri >= BASEPRI_RTQUEUES)) &&
2104 (processor->processor_secondary->state == PROCESSOR_IDLE));
2105
2106 /* OK, so we're not going to run the current thread. Look at the RT queue. */
2107 bool ok_to_run_realtime_thread = sched_ok_to_run_realtime_thread(pset, processor);
2108 if ((rt_runq_count(pset) > 0) && ok_to_run_realtime_thread) {
2109 thread_t next_rt = qe_queue_first(&SCHED(rt_runq)(pset)->queue, struct thread, runq_links);
2110
2111 if (__probable((next_rt->bound_processor == PROCESSOR_NULL ||
2112 (next_rt->bound_processor == processor)))) {
2113 pick_new_rt_thread:
2114 new_thread = qe_dequeue_head(&SCHED(rt_runq)(pset)->queue, struct thread, runq_links);
2115
2116 new_thread->runq = PROCESSOR_NULL;
2117 SCHED_STATS_RUNQ_CHANGE(&SCHED(rt_runq)(pset)->runq_stats, rt_runq_count(pset));
2118 rt_runq_count_decr(pset);
2119
2120 processor->deadline = new_thread->realtime.deadline;
2121
2122 pset_commit_processor_to_new_thread(pset, processor, new_thread);
2123
2124 sched_update_pset_load_average(pset, 0);
2125
2126 processor_t ast_processor = PROCESSOR_NULL;
2127 processor_t next_rt_processor = PROCESSOR_NULL;
2128 sched_ipi_type_t ipi_type = SCHED_IPI_NONE;
2129 sched_ipi_type_t next_rt_ipi_type = SCHED_IPI_NONE;
2130
2131 if (processor->processor_secondary != NULL) {
2132 processor_t sprocessor = processor->processor_secondary;
2133 if ((sprocessor->state == PROCESSOR_RUNNING) || (sprocessor->state == PROCESSOR_DISPATCHING)) {
2134 ipi_type = sched_ipi_action(sprocessor, NULL, false, SCHED_IPI_EVENT_SMT_REBAL);
2135 ast_processor = sprocessor;
2136 }
2137 }
2138 if (rt_runq_count(pset) - bit_count(pset->pending_AST_URGENT_cpu_mask) > 0) {
2139 next_rt_processor = choose_processor_for_realtime_thread(pset, processor, true);
2140 if (next_rt_processor) {
2141 SCHED_DEBUG_CHOOSE_PROCESSOR_KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_CHOOSE_PROCESSOR) | DBG_FUNC_NONE,
2142 (uintptr_t)0, (uintptr_t)-5, next_rt_processor->cpu_id, next_rt_processor->state, 0);
2143 if (next_rt_processor->state == PROCESSOR_IDLE) {
2144 pset_update_processor_state(pset, next_rt_processor, PROCESSOR_DISPATCHING);
2145 }
2146 next_rt_ipi_type = sched_ipi_action(next_rt_processor, NULL, false, SCHED_IPI_EVENT_PREEMPT);
2147 }
2148 }
2149 pset_unlock(pset);
2150
2151 if (ast_processor) {
2152 sched_ipi_perform(ast_processor, ipi_type);
2153 }
2154
2155 if (next_rt_processor) {
2156 sched_ipi_perform(next_rt_processor, next_rt_ipi_type);
2157 }
2158
2159 return new_thread;
2160 }
2161 }
2162 if (secondary_can_only_run_realtime_thread) {
2163 goto idle;
2164 }
2165
2166 processor->deadline = UINT64_MAX;
2167
2168 /* No RT threads, so let's look at the regular threads. */
2169 if ((new_thread = SCHED(choose_thread)(processor, MINPRI, *reason)) != THREAD_NULL) {
2170 pset_commit_processor_to_new_thread(pset, processor, new_thread);
2171 sched_update_pset_load_average(pset, 0);
2172
2173 processor_t ast_processor = PROCESSOR_NULL;
2174 sched_ipi_type_t ipi_type = SCHED_IPI_NONE;
2175
2176 processor_t sprocessor = processor->processor_secondary;
2177 if ((sprocessor != NULL) && (sprocessor->state == PROCESSOR_RUNNING)) {
2178 if (thread_no_smt(new_thread)) {
2179 ipi_type = sched_ipi_action(sprocessor, NULL, false, SCHED_IPI_EVENT_SMT_REBAL);
2180 ast_processor = sprocessor;
2181 }
2182 } else if (secondary_forced_idle && !thread_no_smt(new_thread) && pset_has_stealable_threads(pset)) {
2183 pset_update_processor_state(pset, sprocessor, PROCESSOR_DISPATCHING);
2184 ipi_type = sched_ipi_action(sprocessor, NULL, true, SCHED_IPI_EVENT_PREEMPT);
2185 ast_processor = sprocessor;
2186 }
2187 pset_unlock(pset);
2188
2189 if (ast_processor) {
2190 sched_ipi_perform(ast_processor, ipi_type);
2191 }
2192 return new_thread;
2193 }
2194
2195 if (processor->must_idle) {
2196 processor->must_idle = false;
2197 goto idle;
2198 }
2199
2200 if (SCHED(steal_thread_enabled)(pset) && (processor->processor_primary == processor)) {
2201 /*
2202 * No runnable threads, attempt to steal
2203 * from other processors. Returns with pset lock dropped.
2204 */
2205
2206 if ((new_thread = SCHED(steal_thread)(pset)) != THREAD_NULL) {
2207 /*
2208 * Avoid taking the pset_lock unless it is necessary to change state.
2209 * It's safe to read processor->state here, as only the current processor can change state
2210 * from this point (interrupts are disabled and this processor is committed to run new_thread).
2211 */
2212 if (processor->state == PROCESSOR_DISPATCHING || processor->state == PROCESSOR_IDLE) {
2213 pset_lock(pset);
2214 pset_commit_processor_to_new_thread(pset, processor, new_thread);
2215 pset_unlock(pset);
2216 } else {
2217 assert((processor->state == PROCESSOR_RUNNING) || (processor->state == PROCESSOR_SHUTDOWN));
2218 processor_state_update_from_thread(processor, new_thread);
2219 }
2220
2221 return new_thread;
2222 }
2223
2224 /*
2225 * If other threads have appeared, shortcut
2226 * around again.
2227 */
2228 if (!SCHED(processor_queue_empty)(processor) || (ok_to_run_realtime_thread && (rt_runq_count(pset) > 0))) {
2229 continue;
2230 }
2231
2232 pset_lock(pset);
2233
2234 /* Someone selected this processor while we had dropped the lock */
2235 if (bit_test(pset->pending_AST_URGENT_cpu_mask, processor->cpu_id)) {
2236 goto restart;
2237 }
2238 }
2239
2240 idle:
2241 /*
2242 * Nothing is runnable, so set this processor idle if it
2243 * was running.
2244 */
2245 if ((processor->state == PROCESSOR_RUNNING) || (processor->state == PROCESSOR_DISPATCHING)) {
2246 pset_update_processor_state(pset, processor, PROCESSOR_IDLE);
2247 processor_state_update_idle(processor);
2248 }
2249
2250 /* Invoked with pset locked, returns with pset unlocked */
2251 SCHED(processor_balance)(processor, pset);
2252
2253 new_thread = processor->idle_thread;
2254 } while (new_thread == THREAD_NULL);
2255
2256 return new_thread;
2257 }
2258
2259 /*
2260 * thread_invoke
2261 *
2262 * Called at splsched with neither thread locked.
2263 *
2264 * Perform a context switch and start executing the new thread.
2265 *
2266 * Returns FALSE when the context switch didn't happen.
2267 * The reference to the new thread is still consumed.
2268 *
2269 * "self" is what is currently running on the processor,
2270 * "thread" is the new thread to context switch to
2271 * (which may be the same thread in some cases)
2272 */
2273 static boolean_t
2274 thread_invoke(
2275 thread_t self,
2276 thread_t thread,
2277 ast_t reason)
2278 {
2279 if (__improbable(get_preemption_level() != 0)) {
2280 int pl = get_preemption_level();
2281 panic("thread_invoke: preemption_level %d, possible cause: %s",
2282 pl, (pl < 0 ? "unlocking an unlocked mutex or spinlock" :
2283 "blocking while holding a spinlock, or within interrupt context"));
2284 }
2285
2286 thread_continue_t continuation = self->continuation;
2287 void *parameter = self->parameter;
2288 processor_t processor;
2289
2290 uint64_t ctime = mach_absolute_time();
2291
2292 #ifdef CONFIG_MACH_APPROXIMATE_TIME
2293 commpage_update_mach_approximate_time(ctime);
2294 #endif
2295
2296 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
2297 if (!((thread->state & TH_IDLE) != 0 ||
2298 ((reason & AST_HANDOFF) && self->sched_mode == TH_MODE_REALTIME))) {
2299 sched_timeshare_consider_maintenance(ctime);
2300 }
2301 #endif
2302
2303 #if MONOTONIC
2304 mt_sched_update(self);
2305 #endif /* MONOTONIC */
2306
2307 assert_thread_magic(self);
2308 assert(self == current_thread());
2309 assert(self->runq == PROCESSOR_NULL);
2310 assert((self->state & (TH_RUN | TH_TERMINATE2)) == TH_RUN);
2311
2312 thread_lock(thread);
2313
2314 assert_thread_magic(thread);
2315 assert((thread->state & (TH_RUN | TH_WAIT | TH_UNINT | TH_TERMINATE | TH_TERMINATE2)) == TH_RUN);
2316 assert(thread->bound_processor == PROCESSOR_NULL || thread->bound_processor == current_processor());
2317 assert(thread->runq == PROCESSOR_NULL);
2318
2319 /* Reload precise timing global policy to thread-local policy */
2320 thread->precise_user_kernel_time = use_precise_user_kernel_time(thread);
2321
2322 /* Update SFI class based on other factors */
2323 thread->sfi_class = sfi_thread_classify(thread);
2324
2325 /* Update the same_pri_latency for the thread (used by perfcontrol callouts) */
2326 thread->same_pri_latency = ctime - thread->last_basepri_change_time;
2327 /*
2328 * In case a base_pri update happened between the timestamp and
2329 * taking the thread lock
2330 */
2331 if (ctime <= thread->last_basepri_change_time) {
2332 thread->same_pri_latency = ctime - thread->last_made_runnable_time;
2333 }
2334
2335 /* Allow realtime threads to hang onto a stack. */
2336 if ((self->sched_mode == TH_MODE_REALTIME) && !self->reserved_stack) {
2337 self->reserved_stack = self->kernel_stack;
2338 }
2339
2340 /* Prepare for spin debugging */
2341 #if INTERRUPT_MASKED_DEBUG
2342 ml_spin_debug_clear(thread);
2343 #endif
2344
2345 if (continuation != NULL) {
2346 if (!thread->kernel_stack) {
2347 /*
2348 * If we are using a privileged stack,
2349 * check to see whether we can exchange it with
2350 * that of the other thread.
2351 */
2352 if (self->kernel_stack == self->reserved_stack && !thread->reserved_stack) {
2353 goto need_stack;
2354 }
2355
2356 /*
2357 * Context switch by performing a stack handoff.
2358 * Requires both threads to be parked in a continuation.
2359 */
2360 continuation = thread->continuation;
2361 parameter = thread->parameter;
2362
2363 processor = current_processor();
2364 processor->active_thread = thread;
2365 processor_state_update_from_thread(processor, thread);
2366
2367 if (thread->last_processor != processor && thread->last_processor != NULL) {
2368 if (thread->last_processor->processor_set != processor->processor_set) {
2369 thread->ps_switch++;
2370 }
2371 thread->p_switch++;
2372 }
2373 thread->last_processor = processor;
2374 thread->c_switch++;
2375 ast_context(thread);
2376
2377 thread_unlock(thread);
2378
2379 self->reason = reason;
2380
2381 processor->last_dispatch = ctime;
2382 self->last_run_time = ctime;
2383 processor_timer_switch_thread(ctime, &thread->system_timer);
2384 timer_update(&thread->runnable_timer, ctime);
2385 processor->kernel_timer = &thread->system_timer;
2386
2387 /*
2388 * Since non-precise user/kernel time doesn't update the state timer
2389 * during privilege transitions, synthesize an event now.
2390 */
2391 if (!thread->precise_user_kernel_time) {
2392 timer_update(processor->current_state, ctime);
2393 }
2394
2395 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
2396 MACHDBG_CODE(DBG_MACH_SCHED, MACH_STACK_HANDOFF) | DBG_FUNC_NONE,
2397 self->reason, (uintptr_t)thread_tid(thread), self->sched_pri, thread->sched_pri, 0);
2398
2399 if ((thread->chosen_processor != processor) && (thread->chosen_processor != PROCESSOR_NULL)) {
2400 SCHED_DEBUG_CHOOSE_PROCESSOR_KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_MOVED) | DBG_FUNC_NONE,
2401 (uintptr_t)thread_tid(thread), (uintptr_t)thread->chosen_processor->cpu_id, 0, 0, 0);
2402 }
2403
2404 DTRACE_SCHED2(off__cpu, struct thread *, thread, struct proc *, thread->task->bsd_info);
2405
2406 SCHED_STATS_CSW(processor, self->reason, self->sched_pri, thread->sched_pri);
2407
2408 #if KPERF
2409 kperf_off_cpu(self);
2410 #endif /* KPERF */
2411
2412 /*
2413 * This is where we actually switch thread identity,
2414 * and address space if required. However, register
2415 * state is not switched - this routine leaves the
2416 * stack and register state active on the current CPU.
2417 */
2418 TLOG(1, "thread_invoke: calling stack_handoff\n");
2419 stack_handoff(self, thread);
2420
2421 /* 'self' is now off core */
2422 assert(thread == current_thread_volatile());
2423
2424 DTRACE_SCHED(on__cpu);
2425
2426 #if KPERF
2427 kperf_on_cpu(thread, continuation, NULL);
2428 #endif /* KPERF */
2429
2430 thread_dispatch(self, thread);
2431
2432 #if KASAN
2433 /* Old thread's stack has been moved to the new thread, so explicitly
2434 * unpoison it. */
2435 kasan_unpoison_stack(thread->kernel_stack, kernel_stack_size);
2436 #endif
2437
2438 thread->continuation = thread->parameter = NULL;
2439
2440 boolean_t enable_interrupts = TRUE;
2441
2442 /* idle thread needs to stay interrupts-disabled */
2443 if ((thread->state & TH_IDLE)) {
2444 enable_interrupts = FALSE;
2445 }
2446
2447 assert(continuation);
2448 call_continuation(continuation, parameter,
2449 thread->wait_result, enable_interrupts);
2450 /*NOTREACHED*/
2451 } else if (thread == self) {
2452 /* same thread but with continuation */
2453 ast_context(self);
2454
2455 thread_unlock(self);
2456
2457 #if KPERF
2458 kperf_on_cpu(thread, continuation, NULL);
2459 #endif /* KPERF */
2460
2461 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
2462 MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED) | DBG_FUNC_NONE,
2463 self->reason, (uintptr_t)thread_tid(thread), self->sched_pri, thread->sched_pri, 0);
2464
2465 #if KASAN
2466 /* stack handoff to self - no thread_dispatch(), so clear the stack
2467 * and free the fakestack directly */
2468 kasan_fakestack_drop(self);
2469 kasan_fakestack_gc(self);
2470 kasan_unpoison_stack(self->kernel_stack, kernel_stack_size);
2471 #endif
2472
2473 self->continuation = self->parameter = NULL;
2474
2475 boolean_t enable_interrupts = TRUE;
2476
2477 /* idle thread needs to stay interrupts-disabled */
2478 if ((self->state & TH_IDLE)) {
2479 enable_interrupts = FALSE;
2480 }
2481
2482 call_continuation(continuation, parameter,
2483 self->wait_result, enable_interrupts);
2484 /*NOTREACHED*/
2485 }
2486 } else {
2487 /*
2488 * Check that the other thread has a stack
2489 */
2490 if (!thread->kernel_stack) {
2491 need_stack:
2492 if (!stack_alloc_try(thread)) {
2493 thread_unlock(thread);
2494 thread_stack_enqueue(thread);
2495 return FALSE;
2496 }
2497 } else if (thread == self) {
2498 ast_context(self);
2499 thread_unlock(self);
2500
2501 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
2502 MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED) | DBG_FUNC_NONE,
2503 self->reason, (uintptr_t)thread_tid(thread), self->sched_pri, thread->sched_pri, 0);
2504
2505 return TRUE;
2506 }
2507 }
2508
2509 /*
2510 * Context switch by full context save.
2511 */
2512 processor = current_processor();
2513 processor->active_thread = thread;
2514 processor_state_update_from_thread(processor, thread);
2515
2516 if (thread->last_processor != processor && thread->last_processor != NULL) {
2517 if (thread->last_processor->processor_set != processor->processor_set) {
2518 thread->ps_switch++;
2519 }
2520 thread->p_switch++;
2521 }
2522 thread->last_processor = processor;
2523 thread->c_switch++;
2524 ast_context(thread);
2525
2526 thread_unlock(thread);
2527
2528 self->reason = reason;
2529
2530 processor->last_dispatch = ctime;
2531 self->last_run_time = ctime;
2532 processor_timer_switch_thread(ctime, &thread->system_timer);
2533 timer_update(&thread->runnable_timer, ctime);
2534 processor->kernel_timer = &thread->system_timer;
2535
2536 /*
2537 * Since non-precise user/kernel time doesn't update the state timer
2538 * during privilege transitions, synthesize an event now.
2539 */
2540 if (!thread->precise_user_kernel_time) {
2541 timer_update(processor->current_state, ctime);
2542 }
2543
2544 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
2545 MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED) | DBG_FUNC_NONE,
2546 self->reason, (uintptr_t)thread_tid(thread), self->sched_pri, thread->sched_pri, 0);
2547
2548 if ((thread->chosen_processor != processor) && (thread->chosen_processor != NULL)) {
2549 SCHED_DEBUG_CHOOSE_PROCESSOR_KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_MOVED) | DBG_FUNC_NONE,
2550 (uintptr_t)thread_tid(thread), (uintptr_t)thread->chosen_processor->cpu_id, 0, 0, 0);
2551 }
2552
2553 DTRACE_SCHED2(off__cpu, struct thread *, thread, struct proc *, thread->task->bsd_info);
2554
2555 SCHED_STATS_CSW(processor, self->reason, self->sched_pri, thread->sched_pri);
2556
2557 #if KPERF
2558 kperf_off_cpu(self);
2559 #endif /* KPERF */
2560
2561 /*
2562 * This is where we actually switch register context,
2563 * and address space if required. We will next run
2564 * as a result of a subsequent context switch.
2565 *
2566 * Once registers are switched and the processor is running "thread",
2567 * the stack variables and non-volatile registers will contain whatever
2568 * was there the last time that thread blocked. No local variables should
2569 * be used after this point, except for the special case of "thread", which
2570 * the platform layer returns as the previous thread running on the processor
2571 * via the function call ABI as a return register, and "self", which may have
2572 * been stored on the stack or a non-volatile register, but a stale idea of
2573 * what was on the CPU is newly-accurate because that thread is again
2574 * running on the CPU.
2575 *
2576 * If one of the threads is using a continuation, thread_continue
2577 * is used to stitch up its context.
2578 *
2579 * If we are invoking a thread which is resuming from a continuation,
2580 * the CPU will invoke thread_continue next.
2581 *
2582 * If the current thread is parking in a continuation, then its state
2583 * won't be saved and the stack will be discarded. When the stack is
2584 * re-allocated, it will be configured to resume from thread_continue.
2585 */
2586 assert(continuation == self->continuation);
2587 thread = machine_switch_context(self, continuation, thread);
2588 assert(self == current_thread_volatile());
2589 TLOG(1, "thread_invoke: returning machine_switch_context: self %p continuation %p thread %p\n", self, continuation, thread);
2590
2591 assert(continuation == NULL && self->continuation == NULL);
2592
2593 DTRACE_SCHED(on__cpu);
2594
2595 #if KPERF
2596 kperf_on_cpu(self, NULL, __builtin_frame_address(0));
2597 #endif /* KPERF */
2598
2599 /* We have been resumed and are set to run. */
2600 thread_dispatch(thread, self);
2601
2602 return TRUE;
2603 }
2604
2605 #if defined(CONFIG_SCHED_DEFERRED_AST)
2606 /*
2607 * pset_cancel_deferred_dispatch:
2608 *
2609 * Cancels all ASTs that we can cancel for the given processor set
2610 * if the current processor is running the last runnable thread in the
2611 * system.
2612 *
2613 * This function assumes the current thread is runnable. This must
2614 * be called with the pset unlocked.
2615 */
2616 static void
2617 pset_cancel_deferred_dispatch(
2618 processor_set_t pset,
2619 processor_t processor)
2620 {
2621 processor_t active_processor = NULL;
2622 uint32_t sampled_sched_run_count;
2623
2624 pset_lock(pset);
2625 sampled_sched_run_count = os_atomic_load(&sched_run_buckets[TH_BUCKET_RUN], relaxed);
2626
2627 /*
2628 * If we have emptied the run queue, and our current thread is runnable, we
2629 * should tell any processors that are still DISPATCHING that they will
2630 * probably not have any work to do. In the event that there are no
2631 * pending signals that we can cancel, this is also uninteresting.
2632 *
2633 * In the unlikely event that another thread becomes runnable while we are
2634 * doing this (sched_run_count is atomically updated, not guarded), the
2635 * codepath making it runnable SHOULD (a dangerous word) need the pset lock
2636 * in order to dispatch it to a processor in our pset. So, the other
2637 * codepath will wait while we squash all cancelable ASTs, get the pset
2638 * lock, and then dispatch the freshly runnable thread. So this should be
2639 * correct (we won't accidentally have a runnable thread that hasn't been
2640 * dispatched to an idle processor), if not ideal (we may be restarting the
2641 * dispatch process, which could have some overhead).
2642 */
2643
2644 if ((sampled_sched_run_count == 1) && (pset->pending_deferred_AST_cpu_mask)) {
2645 uint64_t dispatching_map = (pset->cpu_state_map[PROCESSOR_DISPATCHING] &
2646 pset->pending_deferred_AST_cpu_mask &
2647 ~pset->pending_AST_URGENT_cpu_mask);
2648 for (int cpuid = lsb_first(dispatching_map); cpuid >= 0; cpuid = lsb_next(dispatching_map, cpuid)) {
2649 active_processor = processor_array[cpuid];
2650 /*
2651 * If a processor is DISPATCHING, it could be because of
2652 * a cancelable signal.
2653 *
2654 * IF the processor is not our
2655 * current processor (the current processor should not
2656 * be DISPATCHING, so this is a bit paranoid), AND there
2657 * is a cancelable signal pending on the processor, AND
2658 * there is no non-cancelable signal pending (as there is
2659 * no point trying to backtrack on bringing the processor
2660 * up if a signal we cannot cancel is outstanding), THEN
2661 * it should make sense to roll back the processor state
2662 * to the IDLE state.
2663 *
2664 * If the racey nature of this approach (as the signal
2665 * will be arbitrated by hardware, and can fire as we
2666 * roll back state) results in the core responding
2667 * despite being pushed back to the IDLE state, it
2668 * should be no different than if the core took some
2669 * interrupt while IDLE.
2670 */
2671 if (active_processor != processor) {
2672 /*
2673 * Squash all of the processor state back to some
2674 * reasonable facsimile of PROCESSOR_IDLE.
2675 */
2676
2677 processor_state_update_idle(active_processor);
2678 active_processor->deadline = UINT64_MAX;
2679 pset_update_processor_state(pset, active_processor, PROCESSOR_IDLE);
2680 bit_clear(pset->pending_deferred_AST_cpu_mask, active_processor->cpu_id);
2681 machine_signal_idle_cancel(active_processor);
2682 }
2683 }
2684 }
2685
2686 pset_unlock(pset);
2687 }
2688 #else
2689 /* We don't support deferred ASTs; everything is candycanes and sunshine. */
2690 #endif
2691
2692 static void
2693 thread_csw_callout(
2694 thread_t old,
2695 thread_t new,
2696 uint64_t timestamp)
2697 {
2698 perfcontrol_event event = (new->state & TH_IDLE) ? IDLE : CONTEXT_SWITCH;
2699 uint64_t same_pri_latency = (new->state & TH_IDLE) ? 0 : new->same_pri_latency;
2700 machine_switch_perfcontrol_context(event, timestamp, 0,
2701 same_pri_latency, old, new);
2702 }
2703
2704
2705 /*
2706 * thread_dispatch:
2707 *
2708 * Handle threads at context switch. Re-dispatch other thread
2709 * if still running, otherwise update run state and perform
2710 * special actions. Update quantum for other thread and begin
2711 * the quantum for ourselves.
2712 *
2713 * "thread" is the old thread that we have switched away from.
2714 * "self" is the new current thread that we have context switched to
2715 *
2716 * Called at splsched.
2717 *
2718 */
2719 void
2720 thread_dispatch(
2721 thread_t thread,
2722 thread_t self)
2723 {
2724 processor_t processor = self->last_processor;
2725 bool was_idle = false;
2726
2727 assert(processor == current_processor());
2728 assert(self == current_thread_volatile());
2729 assert(thread != self);
2730
2731 if (thread != THREAD_NULL) {
2732 /*
2733 * Do the perfcontrol callout for context switch.
2734 * The reason we do this here is:
2735 * - thread_dispatch() is called from various places that are not
2736 * the direct context switch path for eg. processor shutdown etc.
2737 * So adding the callout here covers all those cases.
2738 * - We want this callout as early as possible to be close
2739 * to the timestamp taken in thread_invoke()
2740 * - We want to avoid holding the thread lock while doing the
2741 * callout
2742 * - We do not want to callout if "thread" is NULL.
2743 */
2744 thread_csw_callout(thread, self, processor->last_dispatch);
2745
2746 #if KASAN
2747 if (thread->continuation != NULL) {
2748 /*
2749 * Thread has a continuation and the normal stack is going away.
2750 * Unpoison the stack and mark all fakestack objects as unused.
2751 */
2752 kasan_fakestack_drop(thread);
2753 if (thread->kernel_stack) {
2754 kasan_unpoison_stack(thread->kernel_stack, kernel_stack_size);
2755 }
2756 }
2757
2758 /*
2759 * Free all unused fakestack objects.
2760 */
2761 kasan_fakestack_gc(thread);
2762 #endif
2763
2764 /*
2765 * If blocked at a continuation, discard
2766 * the stack.
2767 */
2768 if (thread->continuation != NULL && thread->kernel_stack != 0) {
2769 stack_free(thread);
2770 }
2771
2772 if (thread->state & TH_IDLE) {
2773 was_idle = true;
2774 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
2775 MACHDBG_CODE(DBG_MACH_SCHED, MACH_DISPATCH) | DBG_FUNC_NONE,
2776 (uintptr_t)thread_tid(thread), 0, thread->state,
2777 sched_run_buckets[TH_BUCKET_RUN], 0);
2778 } else {
2779 int64_t consumed;
2780 int64_t remainder = 0;
2781
2782 if (processor->quantum_end > processor->last_dispatch) {
2783 remainder = processor->quantum_end -
2784 processor->last_dispatch;
2785 }
2786
2787 consumed = thread->quantum_remaining - remainder;
2788
2789 if ((thread->reason & AST_LEDGER) == 0) {
2790 /*
2791 * Bill CPU time to both the task and
2792 * the individual thread.
2793 */
2794 ledger_credit_thread(thread, thread->t_ledger,
2795 task_ledgers.cpu_time, consumed);
2796 ledger_credit_thread(thread, thread->t_threadledger,
2797 thread_ledgers.cpu_time, consumed);
2798 if (thread->t_bankledger) {
2799 ledger_credit_thread(thread, thread->t_bankledger,
2800 bank_ledgers.cpu_time,
2801 (consumed - thread->t_deduct_bank_ledger_time));
2802 }
2803 thread->t_deduct_bank_ledger_time = 0;
2804 if (consumed > 0) {
2805 /*
2806 * This should never be negative, but in traces we are seeing some instances
2807 * of consumed being negative.
2808 * <rdar://problem/57782596> thread_dispatch() thread CPU consumed calculation sometimes results in negative value
2809 */
2810 sched_update_pset_avg_execution_time(current_processor()->processor_set, consumed, processor->last_dispatch, thread->th_sched_bucket);
2811 }
2812 }
2813
2814 wake_lock(thread);
2815 thread_lock(thread);
2816
2817 /*
2818 * Apply a priority floor if the thread holds a kernel resource
2819 * Do this before checking starting_pri to avoid overpenalizing
2820 * repeated rwlock blockers.
2821 */
2822 if (__improbable(thread->rwlock_count != 0)) {
2823 lck_rw_set_promotion_locked(thread);
2824 }
2825
2826 boolean_t keep_quantum = processor->first_timeslice;
2827
2828 /*
2829 * Treat a thread which has dropped priority since it got on core
2830 * as having expired its quantum.
2831 */
2832 if (processor->starting_pri > thread->sched_pri) {
2833 keep_quantum = FALSE;
2834 }
2835
2836 /* Compute remainder of current quantum. */
2837 if (keep_quantum &&
2838 processor->quantum_end > processor->last_dispatch) {
2839 thread->quantum_remaining = (uint32_t)remainder;
2840 } else {
2841 thread->quantum_remaining = 0;
2842 }
2843
2844 if (thread->sched_mode == TH_MODE_REALTIME) {
2845 /*
2846 * Cancel the deadline if the thread has
2847 * consumed the entire quantum.
2848 */
2849 if (thread->quantum_remaining == 0) {
2850 KDBG(MACHDBG_CODE(DBG_MACH_SCHED, MACH_CANCEL_RT_DEADLINE) | DBG_FUNC_NONE,
2851 (uintptr_t)thread_tid(thread), thread->realtime.deadline, thread->realtime.computation, 0);
2852 thread->realtime.deadline = UINT64_MAX;
2853 }
2854 } else {
2855 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
2856 /*
2857 * For non-realtime threads treat a tiny
2858 * remaining quantum as an expired quantum
2859 * but include what's left next time.
2860 */
2861 if (thread->quantum_remaining < min_std_quantum) {
2862 thread->reason |= AST_QUANTUM;
2863 thread->quantum_remaining += SCHED(initial_quantum_size)(thread);
2864 }
2865 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
2866 }
2867
2868 /*
2869 * If we are doing a direct handoff then
2870 * take the remainder of the quantum.
2871 */
2872 if ((thread->reason & (AST_HANDOFF | AST_QUANTUM)) == AST_HANDOFF) {
2873 self->quantum_remaining = thread->quantum_remaining;
2874 thread->reason |= AST_QUANTUM;
2875 thread->quantum_remaining = 0;
2876 } else {
2877 #if defined(CONFIG_SCHED_MULTIQ)
2878 if (SCHED(sched_groups_enabled) &&
2879 thread->sched_group == self->sched_group) {
2880 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
2881 MACHDBG_CODE(DBG_MACH_SCHED, MACH_QUANTUM_HANDOFF),
2882 self->reason, (uintptr_t)thread_tid(thread),
2883 self->quantum_remaining, thread->quantum_remaining, 0);
2884
2885 self->quantum_remaining = thread->quantum_remaining;
2886 thread->quantum_remaining = 0;
2887 /* Don't set AST_QUANTUM here - old thread might still want to preempt someone else */
2888 }
2889 #endif /* defined(CONFIG_SCHED_MULTIQ) */
2890 }
2891
2892 thread->computation_metered += (processor->last_dispatch - thread->computation_epoch);
2893
2894 if (!(thread->state & TH_WAIT)) {
2895 /*
2896 * Still runnable.
2897 */
2898 thread->last_made_runnable_time = thread->last_basepri_change_time = processor->last_dispatch;
2899
2900 machine_thread_going_off_core(thread, FALSE, processor->last_dispatch, TRUE);
2901
2902 ast_t reason = thread->reason;
2903 sched_options_t options = SCHED_NONE;
2904
2905 if (reason & AST_REBALANCE) {
2906 options |= SCHED_REBALANCE;
2907 if (reason & AST_QUANTUM) {
2908 /*
2909 * Having gone to the trouble of forcing this thread off a less preferred core,
2910 * we should force the preferable core to reschedule immediately to give this
2911 * thread a chance to run instead of just sitting on the run queue where
2912 * it may just be stolen back by the idle core we just forced it off.
2913 * But only do this at the end of a quantum to prevent cascading effects.
2914 */
2915 options |= SCHED_PREEMPT;
2916 }
2917 }
2918
2919 if (reason & AST_QUANTUM) {
2920 options |= SCHED_TAILQ;
2921 } else if (reason & AST_PREEMPT) {
2922 options |= SCHED_HEADQ;
2923 } else {
2924 options |= (SCHED_PREEMPT | SCHED_TAILQ);
2925 }
2926
2927 thread_setrun(thread, options);
2928
2929 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
2930 MACHDBG_CODE(DBG_MACH_SCHED, MACH_DISPATCH) | DBG_FUNC_NONE,
2931 (uintptr_t)thread_tid(thread), thread->reason, thread->state,
2932 sched_run_buckets[TH_BUCKET_RUN], 0);
2933
2934 if (thread->wake_active) {
2935 thread->wake_active = FALSE;
2936 thread_unlock(thread);
2937
2938 thread_wakeup(&thread->wake_active);
2939 } else {
2940 thread_unlock(thread);
2941 }
2942
2943 wake_unlock(thread);
2944 } else {
2945 /*
2946 * Waiting.
2947 */
2948 boolean_t should_terminate = FALSE;
2949 uint32_t new_run_count;
2950 int thread_state = thread->state;
2951
2952 /* Only the first call to thread_dispatch
2953 * after explicit termination should add
2954 * the thread to the termination queue
2955 */
2956 if ((thread_state & (TH_TERMINATE | TH_TERMINATE2)) == TH_TERMINATE) {
2957 should_terminate = TRUE;
2958 thread_state |= TH_TERMINATE2;
2959 }
2960
2961 timer_stop(&thread->runnable_timer, processor->last_dispatch);
2962
2963 thread_state &= ~TH_RUN;
2964 thread->state = thread_state;
2965
2966 thread->last_made_runnable_time = thread->last_basepri_change_time = THREAD_NOT_RUNNABLE;
2967 thread->chosen_processor = PROCESSOR_NULL;
2968
2969 new_run_count = SCHED(run_count_decr)(thread);
2970
2971 #if CONFIG_SCHED_AUTO_JOIN
2972 if ((thread->sched_flags & TH_SFLAG_THREAD_GROUP_AUTO_JOIN) != 0) {
2973 work_interval_auto_join_unwind(thread);
2974 }
2975 #endif /* CONFIG_SCHED_AUTO_JOIN */
2976
2977 #if CONFIG_SCHED_SFI
2978 if (thread->reason & AST_SFI) {
2979 thread->wait_sfi_begin_time = processor->last_dispatch;
2980 }
2981 #endif
2982 machine_thread_going_off_core(thread, should_terminate, processor->last_dispatch, FALSE);
2983
2984 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
2985 MACHDBG_CODE(DBG_MACH_SCHED, MACH_DISPATCH) | DBG_FUNC_NONE,
2986 (uintptr_t)thread_tid(thread), thread->reason, thread_state,
2987 new_run_count, 0);
2988
2989 if (thread_state & TH_WAIT_REPORT) {
2990 (*thread->sched_call)(SCHED_CALL_BLOCK, thread);
2991 }
2992
2993 if (thread->wake_active) {
2994 thread->wake_active = FALSE;
2995 thread_unlock(thread);
2996
2997 thread_wakeup(&thread->wake_active);
2998 } else {
2999 thread_unlock(thread);
3000 }
3001
3002 wake_unlock(thread);
3003
3004 if (should_terminate) {
3005 thread_terminate_enqueue(thread);
3006 }
3007 }
3008 }
3009 /*
3010 * The thread could have been added to the termination queue, so it's
3011 * unsafe to use after this point.
3012 */
3013 thread = THREAD_NULL;
3014 }
3015
3016 int urgency = THREAD_URGENCY_NONE;
3017 uint64_t latency = 0;
3018
3019 /* Update (new) current thread and reprogram running timers */
3020 thread_lock(self);
3021
3022 if (!(self->state & TH_IDLE)) {
3023 uint64_t arg1, arg2;
3024
3025 #if CONFIG_SCHED_SFI
3026 ast_t new_ast;
3027
3028 new_ast = sfi_thread_needs_ast(self, NULL);
3029
3030 if (new_ast != AST_NONE) {
3031 ast_on(new_ast);
3032 }
3033 #endif
3034
3035 assertf(processor->last_dispatch >= self->last_made_runnable_time,
3036 "Non-monotonic time? dispatch at 0x%llx, runnable at 0x%llx",
3037 processor->last_dispatch, self->last_made_runnable_time);
3038
3039 assert(self->last_made_runnable_time <= self->last_basepri_change_time);
3040
3041 latency = processor->last_dispatch - self->last_made_runnable_time;
3042 assert(latency >= self->same_pri_latency);
3043
3044 urgency = thread_get_urgency(self, &arg1, &arg2);
3045
3046 thread_tell_urgency(urgency, arg1, arg2, latency, self);
3047
3048 /*
3049 * Get a new quantum if none remaining.
3050 */
3051 if (self->quantum_remaining == 0) {
3052 thread_quantum_init(self);
3053 }
3054
3055 /*
3056 * Set up quantum timer and timeslice.
3057 */
3058 processor->quantum_end = processor->last_dispatch +
3059 self->quantum_remaining;
3060
3061 running_timer_setup(processor, RUNNING_TIMER_QUANTUM, self,
3062 processor->quantum_end, processor->last_dispatch);
3063 if (was_idle) {
3064 /*
3065 * kperf's running timer is active whenever the idle thread for a
3066 * CPU is not running.
3067 */
3068 kperf_running_setup(processor, processor->last_dispatch);
3069 }
3070 running_timers_activate(processor);
3071 processor->first_timeslice = TRUE;
3072 } else {
3073 running_timers_deactivate(processor);
3074 processor->first_timeslice = FALSE;
3075 thread_tell_urgency(THREAD_URGENCY_NONE, 0, 0, 0, self);
3076 }
3077
3078 assert(self->block_hint == kThreadWaitNone);
3079 self->computation_epoch = processor->last_dispatch;
3080 self->reason = AST_NONE;
3081 processor->starting_pri = self->sched_pri;
3082
3083 thread_unlock(self);
3084
3085 machine_thread_going_on_core(self, urgency, latency, self->same_pri_latency,
3086 processor->last_dispatch);
3087
3088 #if defined(CONFIG_SCHED_DEFERRED_AST)
3089 /*
3090 * TODO: Can we state that redispatching our old thread is also
3091 * uninteresting?
3092 */
3093 if ((os_atomic_load(&sched_run_buckets[TH_BUCKET_RUN], relaxed) == 1) && !(self->state & TH_IDLE)) {
3094 pset_cancel_deferred_dispatch(processor->processor_set, processor);
3095 }
3096 #endif
3097 }
3098
3099 /*
3100 * thread_block_reason:
3101 *
3102 * Forces a reschedule, blocking the caller if a wait
3103 * has been asserted.
3104 *
3105 * If a continuation is specified, then thread_invoke will
3106 * attempt to discard the thread's kernel stack. When the
3107 * thread resumes, it will execute the continuation function
3108 * on a new kernel stack.
3109 */
3110 wait_result_t
3111 thread_block_reason(
3112 thread_continue_t continuation,
3113 void *parameter,
3114 ast_t reason)
3115 {
3116 thread_t self = current_thread();
3117 processor_t processor;
3118 thread_t new_thread;
3119 spl_t s;
3120
3121 s = splsched();
3122
3123 processor = current_processor();
3124
3125 /* If we're explicitly yielding, force a subsequent quantum */
3126 if (reason & AST_YIELD) {
3127 processor->first_timeslice = FALSE;
3128 }
3129
3130 /* We're handling all scheduling AST's */
3131 ast_off(AST_SCHEDULING);
3132
3133 #if PROC_REF_DEBUG
3134 if ((continuation != NULL) && (self->task != kernel_task)) {
3135 if (uthread_get_proc_refcount(self->uthread) != 0) {
3136 panic("thread_block_reason with continuation uthread %p with uu_proc_refcount != 0", self->uthread);
3137 }
3138 }
3139 #endif
3140
3141 self->continuation = continuation;
3142 self->parameter = parameter;
3143
3144 if (self->state & ~(TH_RUN | TH_IDLE)) {
3145 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
3146 MACHDBG_CODE(DBG_MACH_SCHED, MACH_BLOCK),
3147 reason, VM_KERNEL_UNSLIDE(continuation), 0, 0, 0);
3148 }
3149
3150 do {
3151 thread_lock(self);
3152 new_thread = thread_select(self, processor, &reason);
3153 thread_unlock(self);
3154 } while (!thread_invoke(self, new_thread, reason));
3155
3156 splx(s);
3157
3158 return self->wait_result;
3159 }
3160
3161 /*
3162 * thread_block:
3163 *
3164 * Block the current thread if a wait has been asserted.
3165 */
3166 wait_result_t
3167 thread_block(
3168 thread_continue_t continuation)
3169 {
3170 return thread_block_reason(continuation, NULL, AST_NONE);
3171 }
3172
3173 wait_result_t
3174 thread_block_parameter(
3175 thread_continue_t continuation,
3176 void *parameter)
3177 {
3178 return thread_block_reason(continuation, parameter, AST_NONE);
3179 }
3180
3181 /*
3182 * thread_run:
3183 *
3184 * Switch directly from the current thread to the
3185 * new thread, handing off our quantum if appropriate.
3186 *
3187 * New thread must be runnable, and not on a run queue.
3188 *
3189 * Called at splsched.
3190 */
3191 int
3192 thread_run(
3193 thread_t self,
3194 thread_continue_t continuation,
3195 void *parameter,
3196 thread_t new_thread)
3197 {
3198 ast_t reason = AST_NONE;
3199
3200 if ((self->state & TH_IDLE) == 0) {
3201 reason = AST_HANDOFF;
3202 }
3203
3204 /*
3205 * If this thread hadn't been setrun'ed, it
3206 * might not have a chosen processor, so give it one
3207 */
3208 if (new_thread->chosen_processor == NULL) {
3209 new_thread->chosen_processor = current_processor();
3210 }
3211
3212 self->continuation = continuation;
3213 self->parameter = parameter;
3214
3215 while (!thread_invoke(self, new_thread, reason)) {
3216 /* the handoff failed, so we have to fall back to the normal block path */
3217 processor_t processor = current_processor();
3218
3219 reason = AST_NONE;
3220
3221 thread_lock(self);
3222 new_thread = thread_select(self, processor, &reason);
3223 thread_unlock(self);
3224 }
3225
3226 return self->wait_result;
3227 }
3228
3229 /*
3230 * thread_continue:
3231 *
3232 * Called at splsched when a thread first receives
3233 * a new stack after a continuation.
3234 *
3235 * Called with THREAD_NULL as the old thread when
3236 * invoked by machine_load_context.
3237 */
3238 void
3239 thread_continue(
3240 thread_t thread)
3241 {
3242 thread_t self = current_thread();
3243 thread_continue_t continuation;
3244 void *parameter;
3245
3246 DTRACE_SCHED(on__cpu);
3247
3248 continuation = self->continuation;
3249 parameter = self->parameter;
3250
3251 assert(continuation != NULL);
3252
3253 #if KPERF
3254 kperf_on_cpu(self, continuation, NULL);
3255 #endif
3256
3257 thread_dispatch(thread, self);
3258
3259 self->continuation = self->parameter = NULL;
3260
3261 #if INTERRUPT_MASKED_DEBUG
3262 /* Reset interrupt-masked spin debugging timeout */
3263 ml_spin_debug_clear(self);
3264 #endif
3265
3266 TLOG(1, "thread_continue: calling call_continuation\n");
3267
3268 boolean_t enable_interrupts = TRUE;
3269
3270 /* bootstrap thread, idle thread need to stay interrupts-disabled */
3271 if (thread == THREAD_NULL || (self->state & TH_IDLE)) {
3272 enable_interrupts = FALSE;
3273 }
3274
3275 call_continuation(continuation, parameter, self->wait_result, enable_interrupts);
3276 /*NOTREACHED*/
3277 }
3278
3279 void
3280 thread_quantum_init(thread_t thread)
3281 {
3282 if (thread->sched_mode == TH_MODE_REALTIME) {
3283 thread->quantum_remaining = thread->realtime.computation;
3284 } else {
3285 thread->quantum_remaining = SCHED(initial_quantum_size)(thread);
3286 }
3287 }
3288
3289 uint32_t
3290 sched_timeshare_initial_quantum_size(thread_t thread)
3291 {
3292 if ((thread != THREAD_NULL) && thread->th_sched_bucket == TH_BUCKET_SHARE_BG) {
3293 return bg_quantum;
3294 } else {
3295 return std_quantum;
3296 }
3297 }
3298
3299 /*
3300 * run_queue_init:
3301 *
3302 * Initialize a run queue before first use.
3303 */
3304 void
3305 run_queue_init(
3306 run_queue_t rq)
3307 {
3308 rq->highq = NOPRI;
3309 for (u_int i = 0; i < BITMAP_LEN(NRQS); i++) {
3310 rq->bitmap[i] = 0;
3311 }
3312 rq->urgency = rq->count = 0;
3313 for (int i = 0; i < NRQS; i++) {
3314 circle_queue_init(&rq->queues[i]);
3315 }
3316 }
3317
3318 /*
3319 * run_queue_dequeue:
3320 *
3321 * Perform a dequeue operation on a run queue,
3322 * and return the resulting thread.
3323 *
3324 * The run queue must be locked (see thread_run_queue_remove()
3325 * for more info), and not empty.
3326 */
3327 thread_t
3328 run_queue_dequeue(
3329 run_queue_t rq,
3330 sched_options_t options)
3331 {
3332 thread_t thread;
3333 circle_queue_t queue = &rq->queues[rq->highq];
3334
3335 if (options & SCHED_HEADQ) {
3336 thread = cqe_dequeue_head(queue, struct thread, runq_links);
3337 } else {
3338 thread = cqe_dequeue_tail(queue, struct thread, runq_links);
3339 }
3340
3341 assert(thread != THREAD_NULL);
3342 assert_thread_magic(thread);
3343
3344 thread->runq = PROCESSOR_NULL;
3345 SCHED_STATS_RUNQ_CHANGE(&rq->runq_stats, rq->count);
3346 rq->count--;
3347 if (SCHED(priority_is_urgent)(rq->highq)) {
3348 rq->urgency--; assert(rq->urgency >= 0);
3349 }
3350 if (circle_queue_empty(queue)) {
3351 bitmap_clear(rq->bitmap, rq->highq);
3352 rq->highq = bitmap_first(rq->bitmap, NRQS);
3353 }
3354
3355 return thread;
3356 }
3357
3358 /*
3359 * run_queue_enqueue:
3360 *
3361 * Perform a enqueue operation on a run queue.
3362 *
3363 * The run queue must be locked (see thread_run_queue_remove()
3364 * for more info).
3365 */
3366 boolean_t
3367 run_queue_enqueue(
3368 run_queue_t rq,
3369 thread_t thread,
3370 sched_options_t options)
3371 {
3372 circle_queue_t queue = &rq->queues[thread->sched_pri];
3373 boolean_t result = FALSE;
3374
3375 assert_thread_magic(thread);
3376
3377 if (circle_queue_empty(queue)) {
3378 circle_enqueue_tail(queue, &thread->runq_links);
3379
3380 rq_bitmap_set(rq->bitmap, thread->sched_pri);
3381 if (thread->sched_pri > rq->highq) {
3382 rq->highq = thread->sched_pri;
3383 result = TRUE;
3384 }
3385 } else {
3386 if (options & SCHED_TAILQ) {
3387 circle_enqueue_tail(queue, &thread->runq_links);
3388 } else {
3389 circle_enqueue_head(queue, &thread->runq_links);
3390 }
3391 }
3392 if (SCHED(priority_is_urgent)(thread->sched_pri)) {
3393 rq->urgency++;
3394 }
3395 SCHED_STATS_RUNQ_CHANGE(&rq->runq_stats, rq->count);
3396 rq->count++;
3397
3398 return result;
3399 }
3400
3401 /*
3402 * run_queue_remove:
3403 *
3404 * Remove a specific thread from a runqueue.
3405 *
3406 * The run queue must be locked.
3407 */
3408 void
3409 run_queue_remove(
3410 run_queue_t rq,
3411 thread_t thread)
3412 {
3413 circle_queue_t queue = &rq->queues[thread->sched_pri];
3414
3415 assert(thread->runq != PROCESSOR_NULL);
3416 assert_thread_magic(thread);
3417
3418 circle_dequeue(queue, &thread->runq_links);
3419 SCHED_STATS_RUNQ_CHANGE(&rq->runq_stats, rq->count);
3420 rq->count--;
3421 if (SCHED(priority_is_urgent)(thread->sched_pri)) {
3422 rq->urgency--; assert(rq->urgency >= 0);
3423 }
3424
3425 if (circle_queue_empty(queue)) {
3426 /* update run queue status */
3427 bitmap_clear(rq->bitmap, thread->sched_pri);
3428 rq->highq = bitmap_first(rq->bitmap, NRQS);
3429 }
3430
3431 thread->runq = PROCESSOR_NULL;
3432 }
3433
3434 /*
3435 * run_queue_peek
3436 *
3437 * Peek at the runq and return the highest
3438 * priority thread from the runq.
3439 *
3440 * The run queue must be locked.
3441 */
3442 thread_t
3443 run_queue_peek(
3444 run_queue_t rq)
3445 {
3446 if (rq->count > 0) {
3447 circle_queue_t queue = &rq->queues[rq->highq];
3448 thread_t thread = cqe_queue_first(queue, struct thread, runq_links);
3449 assert_thread_magic(thread);
3450 return thread;
3451 } else {
3452 return THREAD_NULL;
3453 }
3454 }
3455
3456 rt_queue_t
3457 sched_rtlocal_runq(processor_set_t pset)
3458 {
3459 return &pset->rt_runq;
3460 }
3461
3462 void
3463 sched_rtlocal_init(processor_set_t pset)
3464 {
3465 pset_rt_init(pset);
3466 }
3467
3468 void
3469 sched_rtlocal_queue_shutdown(processor_t processor)
3470 {
3471 processor_set_t pset = processor->processor_set;
3472 thread_t thread;
3473 queue_head_t tqueue;
3474
3475 pset_lock(pset);
3476
3477 /* We only need to migrate threads if this is the last active or last recommended processor in the pset */
3478 if ((pset->online_processor_count > 0) && pset_is_recommended(pset)) {
3479 pset_unlock(pset);
3480 return;
3481 }
3482
3483 queue_init(&tqueue);
3484
3485 while (rt_runq_count(pset) > 0) {
3486 thread = qe_dequeue_head(&pset->rt_runq.queue, struct thread, runq_links);
3487 thread->runq = PROCESSOR_NULL;
3488 SCHED_STATS_RUNQ_CHANGE(&pset->rt_runq.runq_stats, rt_runq_count(pset));
3489 rt_runq_count_decr(pset);
3490 enqueue_tail(&tqueue, &thread->runq_links);
3491 }
3492 sched_update_pset_load_average(pset, 0);
3493 pset_unlock(pset);
3494
3495 qe_foreach_element_safe(thread, &tqueue, runq_links) {
3496 remqueue(&thread->runq_links);
3497
3498 thread_lock(thread);
3499
3500 thread_setrun(thread, SCHED_TAILQ);
3501
3502 thread_unlock(thread);
3503 }
3504 }
3505
3506 /* Assumes RT lock is not held, and acquires splsched/rt_lock itself */
3507 void
3508 sched_rtlocal_runq_scan(sched_update_scan_context_t scan_context)
3509 {
3510 thread_t thread;
3511
3512 pset_node_t node = &pset_node0;
3513 processor_set_t pset = node->psets;
3514
3515 spl_t s = splsched();
3516 do {
3517 while (pset != NULL) {
3518 pset_lock(pset);
3519
3520 qe_foreach_element_safe(thread, &pset->rt_runq.queue, runq_links) {
3521 if (thread->last_made_runnable_time < scan_context->earliest_rt_make_runnable_time) {
3522 scan_context->earliest_rt_make_runnable_time = thread->last_made_runnable_time;
3523 }
3524 }
3525
3526 pset_unlock(pset);
3527
3528 pset = pset->pset_list;
3529 }
3530 } while (((node = node->node_list) != NULL) && ((pset = node->psets) != NULL));
3531 splx(s);
3532 }
3533
3534 int64_t
3535 sched_rtlocal_runq_count_sum(void)
3536 {
3537 pset_node_t node = &pset_node0;
3538 processor_set_t pset = node->psets;
3539 int64_t count = 0;
3540
3541 do {
3542 while (pset != NULL) {
3543 count += pset->rt_runq.runq_stats.count_sum;
3544
3545 pset = pset->pset_list;
3546 }
3547 } while (((node = node->node_list) != NULL) && ((pset = node->psets) != NULL));
3548
3549 return count;
3550 }
3551
3552 /*
3553 * realtime_queue_insert:
3554 *
3555 * Enqueue a thread for realtime execution.
3556 */
3557 static boolean_t
3558 realtime_queue_insert(processor_t processor, processor_set_t pset, thread_t thread)
3559 {
3560 queue_t queue = &SCHED(rt_runq)(pset)->queue;
3561 uint64_t deadline = thread->realtime.deadline;
3562 boolean_t preempt = FALSE;
3563
3564 pset_assert_locked(pset);
3565
3566 if (queue_empty(queue)) {
3567 enqueue_tail(queue, &thread->runq_links);
3568 preempt = TRUE;
3569 } else {
3570 /* Insert into rt_runq in thread deadline order */
3571 queue_entry_t iter;
3572 qe_foreach(iter, queue) {
3573 thread_t iter_thread = qe_element(iter, struct thread, runq_links);
3574 assert_thread_magic(iter_thread);
3575
3576 if (deadline < iter_thread->realtime.deadline) {
3577 if (iter == queue_first(queue)) {
3578 preempt = TRUE;
3579 }
3580 insque(&thread->runq_links, queue_prev(iter));
3581 break;
3582 } else if (iter == queue_last(queue)) {
3583 enqueue_tail(queue, &thread->runq_links);
3584 break;
3585 }
3586 }
3587 }
3588
3589 thread->runq = processor;
3590 SCHED_STATS_RUNQ_CHANGE(&SCHED(rt_runq)(pset)->runq_stats, rt_runq_count(pset));
3591 rt_runq_count_incr(pset);
3592
3593 return preempt;
3594 }
3595
3596 #define MAX_BACKUP_PROCESSORS 7
3597 #if defined(__x86_64__)
3598 #define DEFAULT_BACKUP_PROCESSORS 1
3599 #else
3600 #define DEFAULT_BACKUP_PROCESSORS 0
3601 #endif
3602
3603 int sched_rt_n_backup_processors = DEFAULT_BACKUP_PROCESSORS;
3604
3605 int
3606 sched_get_rt_n_backup_processors(void)
3607 {
3608 return sched_rt_n_backup_processors;
3609 }
3610
3611 void
3612 sched_set_rt_n_backup_processors(int n)
3613 {
3614 if (n < 0) {
3615 n = 0;
3616 } else if (n > MAX_BACKUP_PROCESSORS) {
3617 n = MAX_BACKUP_PROCESSORS;
3618 }
3619
3620 sched_rt_n_backup_processors = n;
3621 }
3622
3623 /*
3624 * realtime_setrun:
3625 *
3626 * Dispatch a thread for realtime execution.
3627 *
3628 * Thread must be locked. Associated pset must
3629 * be locked, and is returned unlocked.
3630 */
3631 static void
3632 realtime_setrun(
3633 processor_t chosen_processor,
3634 thread_t thread)
3635 {
3636 processor_set_t pset = chosen_processor->processor_set;
3637 pset_assert_locked(pset);
3638 ast_t preempt;
3639
3640 int n_backup = 0;
3641
3642 if (thread->realtime.constraint <= rt_constraint_threshold) {
3643 n_backup = sched_rt_n_backup_processors;
3644 }
3645 assert((n_backup >= 0) && (n_backup <= MAX_BACKUP_PROCESSORS));
3646
3647 sched_ipi_type_t ipi_type[MAX_BACKUP_PROCESSORS + 1] = {};
3648 processor_t ipi_processor[MAX_BACKUP_PROCESSORS + 1] = {};
3649
3650 thread->chosen_processor = chosen_processor;
3651
3652 /* <rdar://problem/15102234> */
3653 assert(thread->bound_processor == PROCESSOR_NULL);
3654
3655 realtime_queue_insert(chosen_processor, pset, thread);
3656
3657 processor_t processor = chosen_processor;
3658 bool chosen_process_is_secondary = chosen_processor->processor_primary != chosen_processor;
3659
3660 int count = 0;
3661 for (int i = 0; i <= n_backup; i++) {
3662 if (i > 0) {
3663 processor = choose_processor_for_realtime_thread(pset, chosen_processor, chosen_process_is_secondary);
3664 if ((processor == PROCESSOR_NULL) || (sched_avoid_cpu0 && (processor->cpu_id == 0))) {
3665 break;
3666 }
3667 SCHED_DEBUG_CHOOSE_PROCESSOR_KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_CHOOSE_PROCESSOR) | DBG_FUNC_NONE,
3668 (uintptr_t)thread_tid(thread), (uintptr_t)-3, processor->cpu_id, processor->state, 0);
3669 }
3670 ipi_type[i] = SCHED_IPI_NONE;
3671 ipi_processor[i] = processor;
3672 count++;
3673
3674 if (processor->current_pri < BASEPRI_RTQUEUES) {
3675 preempt = (AST_PREEMPT | AST_URGENT);
3676 } else if (thread->realtime.deadline < processor->deadline) {
3677 preempt = (AST_PREEMPT | AST_URGENT);
3678 } else {
3679 preempt = AST_NONE;
3680 }
3681
3682 if (preempt != AST_NONE) {
3683 if (processor->state == PROCESSOR_IDLE) {
3684 processor_state_update_from_thread(processor, thread);
3685 processor->deadline = thread->realtime.deadline;
3686 pset_update_processor_state(pset, processor, PROCESSOR_DISPATCHING);
3687 if (processor == current_processor()) {
3688 ast_on(preempt);
3689
3690 if ((preempt & AST_URGENT) == AST_URGENT) {
3691 bit_set(pset->pending_AST_URGENT_cpu_mask, processor->cpu_id);
3692 }
3693
3694 if ((preempt & AST_PREEMPT) == AST_PREEMPT) {
3695 bit_set(pset->pending_AST_PREEMPT_cpu_mask, processor->cpu_id);
3696 }
3697 } else {
3698 ipi_type[i] = sched_ipi_action(processor, thread, true, SCHED_IPI_EVENT_PREEMPT);
3699 }
3700 } else if (processor->state == PROCESSOR_DISPATCHING) {
3701 if ((processor->current_pri < thread->sched_pri) || (processor->deadline > thread->realtime.deadline)) {
3702 processor_state_update_from_thread(processor, thread);
3703 processor->deadline = thread->realtime.deadline;
3704 }
3705 } else {
3706 if (processor == current_processor()) {
3707 ast_on(preempt);
3708
3709 if ((preempt & AST_URGENT) == AST_URGENT) {
3710 bit_set(pset->pending_AST_URGENT_cpu_mask, processor->cpu_id);
3711 }
3712
3713 if ((preempt & AST_PREEMPT) == AST_PREEMPT) {
3714 bit_set(pset->pending_AST_PREEMPT_cpu_mask, processor->cpu_id);
3715 }
3716 } else {
3717 ipi_type[i] = sched_ipi_action(processor, thread, false, SCHED_IPI_EVENT_PREEMPT);
3718 }
3719 }
3720 } else {
3721 /* Selected processor was too busy, just keep thread enqueued and let other processors drain it naturally. */
3722 }
3723 }
3724
3725 pset_unlock(pset);
3726
3727 assert((count > 0) && (count <= (n_backup + 1)));
3728 for (int i = 0; i < count; i++) {
3729 assert(ipi_processor[i] != PROCESSOR_NULL);
3730 sched_ipi_perform(ipi_processor[i], ipi_type[i]);
3731 }
3732 }
3733
3734
3735 sched_ipi_type_t
3736 sched_ipi_deferred_policy(processor_set_t pset, processor_t dst,
3737 __unused sched_ipi_event_t event)
3738 {
3739 #if defined(CONFIG_SCHED_DEFERRED_AST)
3740 if (!bit_test(pset->pending_deferred_AST_cpu_mask, dst->cpu_id)) {
3741 return SCHED_IPI_DEFERRED;
3742 }
3743 #else /* CONFIG_SCHED_DEFERRED_AST */
3744 panic("Request for deferred IPI on an unsupported platform; pset: %p CPU: %d", pset, dst->cpu_id);
3745 #endif /* CONFIG_SCHED_DEFERRED_AST */
3746 return SCHED_IPI_NONE;
3747 }
3748
3749 sched_ipi_type_t
3750 sched_ipi_action(processor_t dst, thread_t thread, boolean_t dst_idle, sched_ipi_event_t event)
3751 {
3752 sched_ipi_type_t ipi_type = SCHED_IPI_NONE;
3753 assert(dst != NULL);
3754
3755 processor_set_t pset = dst->processor_set;
3756 if (current_processor() == dst) {
3757 return SCHED_IPI_NONE;
3758 }
3759
3760 if (bit_test(pset->pending_AST_URGENT_cpu_mask, dst->cpu_id)) {
3761 return SCHED_IPI_NONE;
3762 }
3763
3764 ipi_type = SCHED(ipi_policy)(dst, thread, dst_idle, event);
3765 switch (ipi_type) {
3766 case SCHED_IPI_NONE:
3767 return SCHED_IPI_NONE;
3768 #if defined(CONFIG_SCHED_DEFERRED_AST)
3769 case SCHED_IPI_DEFERRED:
3770 bit_set(pset->pending_deferred_AST_cpu_mask, dst->cpu_id);
3771 break;
3772 #endif /* CONFIG_SCHED_DEFERRED_AST */
3773 default:
3774 bit_set(pset->pending_AST_URGENT_cpu_mask, dst->cpu_id);
3775 bit_set(pset->pending_AST_PREEMPT_cpu_mask, dst->cpu_id);
3776 break;
3777 }
3778 return ipi_type;
3779 }
3780
3781 sched_ipi_type_t
3782 sched_ipi_policy(processor_t dst, thread_t thread, boolean_t dst_idle, sched_ipi_event_t event)
3783 {
3784 sched_ipi_type_t ipi_type = SCHED_IPI_NONE;
3785 boolean_t deferred_ipi_supported = false;
3786 processor_set_t pset = dst->processor_set;
3787
3788 #if defined(CONFIG_SCHED_DEFERRED_AST)
3789 deferred_ipi_supported = true;
3790 #endif /* CONFIG_SCHED_DEFERRED_AST */
3791
3792 switch (event) {
3793 case SCHED_IPI_EVENT_SPILL:
3794 case SCHED_IPI_EVENT_SMT_REBAL:
3795 case SCHED_IPI_EVENT_REBALANCE:
3796 case SCHED_IPI_EVENT_BOUND_THR:
3797 /*
3798 * The spill, SMT rebalance, rebalance and the bound thread
3799 * scenarios use immediate IPIs always.
3800 */
3801 ipi_type = dst_idle ? SCHED_IPI_IDLE : SCHED_IPI_IMMEDIATE;
3802 break;
3803 case SCHED_IPI_EVENT_PREEMPT:
3804 /* In the preemption case, use immediate IPIs for RT threads */
3805 if (thread && (thread->sched_pri >= BASEPRI_RTQUEUES)) {
3806 ipi_type = dst_idle ? SCHED_IPI_IDLE : SCHED_IPI_IMMEDIATE;
3807 break;
3808 }
3809
3810 /*
3811 * For Non-RT threads preemption,
3812 * If the core is active, use immediate IPIs.
3813 * If the core is idle, use deferred IPIs if supported; otherwise immediate IPI.
3814 */
3815 if (deferred_ipi_supported && dst_idle) {
3816 return sched_ipi_deferred_policy(pset, dst, event);
3817 }
3818 ipi_type = dst_idle ? SCHED_IPI_IDLE : SCHED_IPI_IMMEDIATE;
3819 break;
3820 default:
3821 panic("Unrecognized scheduler IPI event type %d", event);
3822 }
3823 assert(ipi_type != SCHED_IPI_NONE);
3824 return ipi_type;
3825 }
3826
3827 void
3828 sched_ipi_perform(processor_t dst, sched_ipi_type_t ipi)
3829 {
3830 switch (ipi) {
3831 case SCHED_IPI_NONE:
3832 break;
3833 case SCHED_IPI_IDLE:
3834 machine_signal_idle(dst);
3835 break;
3836 case SCHED_IPI_IMMEDIATE:
3837 cause_ast_check(dst);
3838 break;
3839 case SCHED_IPI_DEFERRED:
3840 machine_signal_idle_deferred(dst);
3841 break;
3842 default:
3843 panic("Unrecognized scheduler IPI type: %d", ipi);
3844 }
3845 }
3846
3847 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
3848
3849 boolean_t
3850 priority_is_urgent(int priority)
3851 {
3852 return bitmap_test(sched_preempt_pri, priority) ? TRUE : FALSE;
3853 }
3854
3855 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
3856
3857 /*
3858 * processor_setrun:
3859 *
3860 * Dispatch a thread for execution on a
3861 * processor.
3862 *
3863 * Thread must be locked. Associated pset must
3864 * be locked, and is returned unlocked.
3865 */
3866 static void
3867 processor_setrun(
3868 processor_t processor,
3869 thread_t thread,
3870 integer_t options)
3871 {
3872 processor_set_t pset = processor->processor_set;
3873 pset_assert_locked(pset);
3874 ast_t preempt;
3875 enum { eExitIdle, eInterruptRunning, eDoNothing } ipi_action = eDoNothing;
3876
3877 sched_ipi_type_t ipi_type = SCHED_IPI_NONE;
3878
3879 thread->chosen_processor = processor;
3880
3881 /*
3882 * Set preemption mode.
3883 */
3884 #if defined(CONFIG_SCHED_DEFERRED_AST)
3885 /* TODO: Do we need to care about urgency (see rdar://problem/20136239)? */
3886 #endif
3887 if (SCHED(priority_is_urgent)(thread->sched_pri) && thread->sched_pri > processor->current_pri) {
3888 preempt = (AST_PREEMPT | AST_URGENT);
3889 } else if (processor->current_is_eagerpreempt) {
3890 preempt = (AST_PREEMPT | AST_URGENT);
3891 } else if ((thread->sched_mode == TH_MODE_TIMESHARE) && (thread->sched_pri < thread->base_pri)) {
3892 if (SCHED(priority_is_urgent)(thread->base_pri) && thread->sched_pri > processor->current_pri) {
3893 preempt = (options & SCHED_PREEMPT)? AST_PREEMPT: AST_NONE;
3894 } else {
3895 preempt = AST_NONE;
3896 }
3897 } else {
3898 preempt = (options & SCHED_PREEMPT)? AST_PREEMPT: AST_NONE;
3899 }
3900
3901 if ((options & (SCHED_PREEMPT | SCHED_REBALANCE)) == (SCHED_PREEMPT | SCHED_REBALANCE)) {
3902 /*
3903 * Having gone to the trouble of forcing this thread off a less preferred core,
3904 * we should force the preferable core to reschedule immediately to give this
3905 * thread a chance to run instead of just sitting on the run queue where
3906 * it may just be stolen back by the idle core we just forced it off.
3907 */
3908 preempt |= AST_PREEMPT;
3909 }
3910
3911 SCHED(processor_enqueue)(processor, thread, options);
3912 sched_update_pset_load_average(pset, 0);
3913
3914 if (preempt != AST_NONE) {
3915 if (processor->state == PROCESSOR_IDLE) {
3916 processor_state_update_from_thread(processor, thread);
3917 processor->deadline = UINT64_MAX;
3918 pset_update_processor_state(pset, processor, PROCESSOR_DISPATCHING);
3919 ipi_action = eExitIdle;
3920 } else if (processor->state == PROCESSOR_DISPATCHING) {
3921 if (processor->current_pri < thread->sched_pri) {
3922 processor_state_update_from_thread(processor, thread);
3923 processor->deadline = UINT64_MAX;
3924 }
3925 } else if ((processor->state == PROCESSOR_RUNNING ||
3926 processor->state == PROCESSOR_SHUTDOWN) &&
3927 (thread->sched_pri >= processor->current_pri)) {
3928 ipi_action = eInterruptRunning;
3929 }
3930 } else {
3931 /*
3932 * New thread is not important enough to preempt what is running, but
3933 * special processor states may need special handling
3934 */
3935 if (processor->state == PROCESSOR_SHUTDOWN &&
3936 thread->sched_pri >= processor->current_pri) {
3937 ipi_action = eInterruptRunning;
3938 } else if (processor->state == PROCESSOR_IDLE) {
3939 processor_state_update_from_thread(processor, thread);
3940 processor->deadline = UINT64_MAX;
3941 pset_update_processor_state(pset, processor, PROCESSOR_DISPATCHING);
3942
3943 ipi_action = eExitIdle;
3944 }
3945 }
3946
3947 if (ipi_action != eDoNothing) {
3948 if (processor == current_processor()) {
3949 if ((preempt = csw_check_locked(processor->active_thread, processor, pset, AST_NONE)) != AST_NONE) {
3950 ast_on(preempt);
3951 }
3952
3953 if ((preempt & AST_URGENT) == AST_URGENT) {
3954 bit_set(pset->pending_AST_URGENT_cpu_mask, processor->cpu_id);
3955 } else {
3956 bit_clear(pset->pending_AST_URGENT_cpu_mask, processor->cpu_id);
3957 }
3958
3959 if ((preempt & AST_PREEMPT) == AST_PREEMPT) {
3960 bit_set(pset->pending_AST_PREEMPT_cpu_mask, processor->cpu_id);
3961 } else {
3962 bit_clear(pset->pending_AST_PREEMPT_cpu_mask, processor->cpu_id);
3963 }
3964 } else {
3965 sched_ipi_event_t event = (options & SCHED_REBALANCE) ? SCHED_IPI_EVENT_REBALANCE : SCHED_IPI_EVENT_PREEMPT;
3966 ipi_type = sched_ipi_action(processor, thread, (ipi_action == eExitIdle), event);
3967 }
3968 }
3969 pset_unlock(pset);
3970 sched_ipi_perform(processor, ipi_type);
3971 }
3972
3973 /*
3974 * choose_next_pset:
3975 *
3976 * Return the next sibling pset containing
3977 * available processors.
3978 *
3979 * Returns the original pset if none other is
3980 * suitable.
3981 */
3982 static processor_set_t
3983 choose_next_pset(
3984 processor_set_t pset)
3985 {
3986 processor_set_t nset = pset;
3987
3988 do {
3989 nset = next_pset(nset);
3990 } while (nset->online_processor_count < 1 && nset != pset);
3991
3992 return nset;
3993 }
3994
3995 inline static processor_set_t
3996 change_locked_pset(processor_set_t current_pset, processor_set_t new_pset)
3997 {
3998 if (current_pset != new_pset) {
3999 pset_unlock(current_pset);
4000 pset_lock(new_pset);
4001 }
4002
4003 return new_pset;
4004 }
4005
4006 /*
4007 * choose_processor:
4008 *
4009 * Choose a processor for the thread, beginning at
4010 * the pset. Accepts an optional processor hint in
4011 * the pset.
4012 *
4013 * Returns a processor, possibly from a different pset.
4014 *
4015 * The thread must be locked. The pset must be locked,
4016 * and the resulting pset is locked on return.
4017 */
4018 processor_t
4019 choose_processor(
4020 processor_set_t starting_pset,
4021 processor_t processor,
4022 thread_t thread)
4023 {
4024 processor_set_t pset = starting_pset;
4025 processor_set_t nset;
4026
4027 assert(thread->sched_pri <= BASEPRI_RTQUEUES);
4028
4029 /*
4030 * Prefer the hinted processor, when appropriate.
4031 */
4032
4033 /* Fold last processor hint from secondary processor to its primary */
4034 if (processor != PROCESSOR_NULL) {
4035 processor = processor->processor_primary;
4036 }
4037
4038 /*
4039 * Only consult platform layer if pset is active, which
4040 * it may not be in some cases when a multi-set system
4041 * is going to sleep.
4042 */
4043 if (pset->online_processor_count) {
4044 if ((processor == PROCESSOR_NULL) || (processor->processor_set == pset && processor->state == PROCESSOR_IDLE)) {
4045 processor_t mc_processor = machine_choose_processor(pset, processor);
4046 if (mc_processor != PROCESSOR_NULL) {
4047 processor = mc_processor->processor_primary;
4048 }
4049 }
4050 }
4051
4052 /*
4053 * At this point, we may have a processor hint, and we may have
4054 * an initial starting pset. If the hint is not in the pset, or
4055 * if the hint is for a processor in an invalid state, discard
4056 * the hint.
4057 */
4058 if (processor != PROCESSOR_NULL) {
4059 if (processor->processor_set != pset) {
4060 processor = PROCESSOR_NULL;
4061 } else if (!processor->is_recommended) {
4062 processor = PROCESSOR_NULL;
4063 } else {
4064 switch (processor->state) {
4065 case PROCESSOR_START:
4066 case PROCESSOR_SHUTDOWN:
4067 case PROCESSOR_OFF_LINE:
4068 /*
4069 * Hint is for a processor that cannot support running new threads.
4070 */
4071 processor = PROCESSOR_NULL;
4072 break;
4073 case PROCESSOR_IDLE:
4074 /*
4075 * Hint is for an idle processor. Assume it is no worse than any other
4076 * idle processor. The platform layer had an opportunity to provide
4077 * the "least cost idle" processor above.
4078 */
4079 if ((thread->sched_pri < BASEPRI_RTQUEUES) || processor_is_fast_track_candidate_for_realtime_thread(pset, processor)) {
4080 return processor;
4081 }
4082 processor = PROCESSOR_NULL;
4083 break;
4084 case PROCESSOR_RUNNING:
4085 case PROCESSOR_DISPATCHING:
4086 /*
4087 * Hint is for an active CPU. This fast-path allows
4088 * realtime threads to preempt non-realtime threads
4089 * to regain their previous executing processor.
4090 */
4091 if ((thread->sched_pri >= BASEPRI_RTQUEUES) &&
4092 processor_is_fast_track_candidate_for_realtime_thread(pset, processor)) {
4093 return processor;
4094 }
4095
4096 /* Otherwise, use hint as part of search below */
4097 break;
4098 default:
4099 processor = PROCESSOR_NULL;
4100 break;
4101 }
4102 }
4103 }
4104
4105 /*
4106 * Iterate through the processor sets to locate
4107 * an appropriate processor. Seed results with
4108 * a last-processor hint, if available, so that
4109 * a search must find something strictly better
4110 * to replace it.
4111 *
4112 * A primary/secondary pair of SMT processors are
4113 * "unpaired" if the primary is busy but its
4114 * corresponding secondary is idle (so the physical
4115 * core has full use of its resources).
4116 */
4117
4118 integer_t lowest_priority = MAXPRI + 1;
4119 integer_t lowest_secondary_priority = MAXPRI + 1;
4120 integer_t lowest_unpaired_primary_priority = MAXPRI + 1;
4121 integer_t lowest_idle_secondary_priority = MAXPRI + 1;
4122 integer_t lowest_count = INT_MAX;
4123 uint64_t furthest_deadline = 1;
4124 processor_t lp_processor = PROCESSOR_NULL;
4125 processor_t lp_unpaired_primary_processor = PROCESSOR_NULL;
4126 processor_t lp_idle_secondary_processor = PROCESSOR_NULL;
4127 processor_t lp_paired_secondary_processor = PROCESSOR_NULL;
4128 processor_t lc_processor = PROCESSOR_NULL;
4129 processor_t fd_processor = PROCESSOR_NULL;
4130
4131 if (processor != PROCESSOR_NULL) {
4132 /* All other states should be enumerated above. */
4133 assert(processor->state == PROCESSOR_RUNNING || processor->state == PROCESSOR_DISPATCHING);
4134
4135 lowest_priority = processor->current_pri;
4136 lp_processor = processor;
4137
4138 if (processor->current_pri >= BASEPRI_RTQUEUES) {
4139 furthest_deadline = processor->deadline;
4140 fd_processor = processor;
4141 }
4142
4143 lowest_count = SCHED(processor_runq_count)(processor);
4144 lc_processor = processor;
4145 }
4146
4147 if (thread->sched_pri >= BASEPRI_RTQUEUES) {
4148 pset_node_t node = pset->node;
4149 int consider_secondaries = (!pset->is_SMT) || (bit_count(node->pset_map) == 1) || (node->pset_non_rt_primary_map == 0);
4150 for (; consider_secondaries < 2; consider_secondaries++) {
4151 pset = change_locked_pset(pset, starting_pset);
4152 do {
4153 processor = choose_processor_for_realtime_thread(pset, PROCESSOR_NULL, consider_secondaries);
4154 if (processor) {
4155 return processor;
4156 }
4157
4158 /* NRG Collect processor stats for furthest deadline etc. here */
4159
4160 nset = next_pset(pset);
4161
4162 if (nset != starting_pset) {
4163 pset = change_locked_pset(pset, nset);
4164 }
4165 } while (nset != starting_pset);
4166 }
4167 /* Or we could just let it change to starting_pset in the loop above */
4168 pset = change_locked_pset(pset, starting_pset);
4169 }
4170
4171 do {
4172 /*
4173 * Choose an idle processor, in pset traversal order
4174 */
4175
4176 uint64_t idle_primary_map = (pset->cpu_state_map[PROCESSOR_IDLE] &
4177 pset->primary_map &
4178 pset->recommended_bitmask);
4179
4180 /* there shouldn't be a pending AST if the processor is idle */
4181 assert((idle_primary_map & pset->pending_AST_URGENT_cpu_mask) == 0);
4182
4183 int cpuid = lsb_first(idle_primary_map);
4184 if (cpuid >= 0) {
4185 processor = processor_array[cpuid];
4186 return processor;
4187 }
4188
4189 /*
4190 * Otherwise, enumerate active and idle processors to find primary candidates
4191 * with lower priority/etc.
4192 */
4193
4194 uint64_t active_map = ((pset->cpu_state_map[PROCESSOR_RUNNING] | pset->cpu_state_map[PROCESSOR_DISPATCHING]) &
4195 pset->recommended_bitmask &
4196 ~pset->pending_AST_URGENT_cpu_mask);
4197
4198 if (SCHED(priority_is_urgent)(thread->sched_pri) == FALSE) {
4199 active_map &= ~pset->pending_AST_PREEMPT_cpu_mask;
4200 }
4201
4202 active_map = bit_ror64(active_map, (pset->last_chosen + 1));
4203 for (int rotid = lsb_first(active_map); rotid >= 0; rotid = lsb_next(active_map, rotid)) {
4204 cpuid = ((rotid + pset->last_chosen + 1) & 63);
4205 processor = processor_array[cpuid];
4206
4207 integer_t cpri = processor->current_pri;
4208 processor_t primary = processor->processor_primary;
4209 if (primary != processor) {
4210 /* If primary is running a NO_SMT thread, don't choose its secondary */
4211 if (!((primary->state == PROCESSOR_RUNNING) && processor_active_thread_no_smt(primary))) {
4212 if (cpri < lowest_secondary_priority) {
4213 lowest_secondary_priority = cpri;
4214 lp_paired_secondary_processor = processor;
4215 }
4216 }
4217 } else {
4218 if (cpri < lowest_priority) {
4219 lowest_priority = cpri;
4220 lp_processor = processor;
4221 }
4222 }
4223
4224 if ((cpri >= BASEPRI_RTQUEUES) && (processor->deadline > furthest_deadline)) {
4225 furthest_deadline = processor->deadline;
4226 fd_processor = processor;
4227 }
4228
4229 integer_t ccount = SCHED(processor_runq_count)(processor);
4230 if (ccount < lowest_count) {
4231 lowest_count = ccount;
4232 lc_processor = processor;
4233 }
4234 }
4235
4236 /*
4237 * For SMT configs, these idle secondary processors must have active primary. Otherwise
4238 * the idle primary would have short-circuited the loop above
4239 */
4240 uint64_t idle_secondary_map = (pset->cpu_state_map[PROCESSOR_IDLE] &
4241 ~pset->primary_map &
4242 pset->recommended_bitmask);
4243
4244 /* there shouldn't be a pending AST if the processor is idle */
4245 assert((idle_secondary_map & pset->pending_AST_URGENT_cpu_mask) == 0);
4246 assert((idle_secondary_map & pset->pending_AST_PREEMPT_cpu_mask) == 0);
4247
4248 for (cpuid = lsb_first(idle_secondary_map); cpuid >= 0; cpuid = lsb_next(idle_secondary_map, cpuid)) {
4249 processor = processor_array[cpuid];
4250
4251 processor_t cprimary = processor->processor_primary;
4252
4253 integer_t primary_pri = cprimary->current_pri;
4254
4255 /*
4256 * TODO: This should also make the same decisions
4257 * as secondary_can_run_realtime_thread
4258 *
4259 * TODO: Keep track of the pending preemption priority
4260 * of the primary to make this more accurate.
4261 */
4262
4263 /* If the primary is running a no-smt thread, then don't choose its secondary */
4264 if (cprimary->state == PROCESSOR_RUNNING &&
4265 processor_active_thread_no_smt(cprimary)) {
4266 continue;
4267 }
4268
4269 /*
4270 * Find the idle secondary processor with the lowest priority primary
4271 *
4272 * We will choose this processor as a fallback if we find no better
4273 * primary to preempt.
4274 */
4275 if (primary_pri < lowest_idle_secondary_priority) {
4276 lp_idle_secondary_processor = processor;
4277 lowest_idle_secondary_priority = primary_pri;
4278 }
4279
4280 /* Find the the lowest priority active primary with idle secondary */
4281 if (primary_pri < lowest_unpaired_primary_priority) {
4282 /* If the primary processor is offline or starting up, it's not a candidate for this path */
4283 if (cprimary->state != PROCESSOR_RUNNING &&
4284 cprimary->state != PROCESSOR_DISPATCHING) {
4285 continue;
4286 }
4287
4288 if (!cprimary->is_recommended) {
4289 continue;
4290 }
4291
4292 /* if the primary is pending preemption, don't try to re-preempt it */
4293 if (bit_test(pset->pending_AST_URGENT_cpu_mask, cprimary->cpu_id)) {
4294 continue;
4295 }
4296
4297 if (SCHED(priority_is_urgent)(thread->sched_pri) == FALSE &&
4298 bit_test(pset->pending_AST_PREEMPT_cpu_mask, cprimary->cpu_id)) {
4299 continue;
4300 }
4301
4302 lowest_unpaired_primary_priority = primary_pri;
4303 lp_unpaired_primary_processor = cprimary;
4304 }
4305 }
4306
4307 /*
4308 * We prefer preempting a primary processor over waking up its secondary.
4309 * The secondary will then be woken up by the preempted thread.
4310 */
4311 if (thread->sched_pri > lowest_unpaired_primary_priority) {
4312 pset->last_chosen = lp_unpaired_primary_processor->cpu_id;
4313 return lp_unpaired_primary_processor;
4314 }
4315
4316 /*
4317 * We prefer preempting a lower priority active processor over directly
4318 * waking up an idle secondary.
4319 * The preempted thread will then find the idle secondary.
4320 */
4321 if (thread->sched_pri > lowest_priority) {
4322 pset->last_chosen = lp_processor->cpu_id;
4323 return lp_processor;
4324 }
4325
4326 if (thread->sched_pri >= BASEPRI_RTQUEUES) {
4327 /*
4328 * For realtime threads, the most important aspect is
4329 * scheduling latency, so we will pick an active
4330 * secondary processor in this pset, or preempt
4331 * another RT thread with a further deadline before
4332 * going to the next pset.
4333 */
4334
4335 if (sched_allow_rt_smt && (thread->sched_pri > lowest_secondary_priority)) {
4336 pset->last_chosen = lp_paired_secondary_processor->cpu_id;
4337 return lp_paired_secondary_processor;
4338 }
4339
4340 if (thread->realtime.deadline < furthest_deadline) {
4341 return fd_processor;
4342 }
4343 }
4344
4345 /*
4346 * lc_processor is used to indicate the best processor set run queue
4347 * on which to enqueue a thread when all available CPUs are busy with
4348 * higher priority threads, so try to make sure it is initialized.
4349 */
4350 if (lc_processor == PROCESSOR_NULL) {
4351 cpumap_t available_map = ((pset->cpu_state_map[PROCESSOR_IDLE] |
4352 pset->cpu_state_map[PROCESSOR_RUNNING] |
4353 pset->cpu_state_map[PROCESSOR_DISPATCHING]) &
4354 pset->recommended_bitmask);
4355 cpuid = lsb_first(available_map);
4356 if (cpuid >= 0) {
4357 lc_processor = processor_array[cpuid];
4358 lowest_count = SCHED(processor_runq_count)(lc_processor);
4359 }
4360 }
4361
4362 /*
4363 * Move onto the next processor set.
4364 *
4365 * If all primary processors in this pset are running a higher
4366 * priority thread, move on to next pset. Only when we have
4367 * exhausted the search for primary processors do we
4368 * fall back to secondaries.
4369 */
4370 nset = next_pset(pset);
4371
4372 if (nset != starting_pset) {
4373 pset = change_locked_pset(pset, nset);
4374 }
4375 } while (nset != starting_pset);
4376
4377 /*
4378 * Make sure that we pick a running processor,
4379 * and that the correct processor set is locked.
4380 * Since we may have unlocked the candidate processor's
4381 * pset, it may have changed state.
4382 *
4383 * All primary processors are running a higher priority
4384 * thread, so the only options left are enqueuing on
4385 * the secondary processor that would perturb the least priority
4386 * primary, or the least busy primary.
4387 */
4388 boolean_t fallback_processor = false;
4389 do {
4390 /* lowest_priority is evaluated in the main loops above */
4391 if (lp_idle_secondary_processor != PROCESSOR_NULL) {
4392 processor = lp_idle_secondary_processor;
4393 lp_idle_secondary_processor = PROCESSOR_NULL;
4394 } else if (lp_paired_secondary_processor != PROCESSOR_NULL) {
4395 processor = lp_paired_secondary_processor;
4396 lp_paired_secondary_processor = PROCESSOR_NULL;
4397 } else if (lc_processor != PROCESSOR_NULL) {
4398 processor = lc_processor;
4399 lc_processor = PROCESSOR_NULL;
4400 } else {
4401 /*
4402 * All processors are executing higher priority threads, and
4403 * the lowest_count candidate was not usable.
4404 *
4405 * For AMP platforms running the clutch scheduler always
4406 * return a processor from the requested pset to allow the
4407 * thread to be enqueued in the correct runq. For non-AMP
4408 * platforms, simply return the master_processor.
4409 */
4410 fallback_processor = true;
4411 #if CONFIG_SCHED_EDGE
4412 processor = processor_array[lsb_first(starting_pset->primary_map)];
4413 #else /* CONFIG_SCHED_EDGE */
4414 processor = master_processor;
4415 #endif /* CONFIG_SCHED_EDGE */
4416 }
4417
4418 /*
4419 * Check that the correct processor set is
4420 * returned locked.
4421 */
4422 pset = change_locked_pset(pset, processor->processor_set);
4423
4424 /*
4425 * We must verify that the chosen processor is still available.
4426 * The cases where we pick the master_processor or the fallback
4427 * processor are execptions, since we may need enqueue a thread
4428 * on its runqueue if this is the last remaining processor
4429 * during pset shutdown.
4430 *
4431 * <rdar://problem/47559304> would really help here since it
4432 * gets rid of the weird last processor SHUTDOWN case where
4433 * the pset is still schedulable.
4434 */
4435 if (processor != master_processor && (fallback_processor == false) && (processor->state == PROCESSOR_SHUTDOWN || processor->state == PROCESSOR_OFF_LINE)) {
4436 processor = PROCESSOR_NULL;
4437 }
4438 } while (processor == PROCESSOR_NULL);
4439
4440 pset->last_chosen = processor->cpu_id;
4441 return processor;
4442 }
4443
4444 /*
4445 * Default implementation of SCHED(choose_node)()
4446 * for single node systems
4447 */
4448 pset_node_t
4449 sched_choose_node(__unused thread_t thread)
4450 {
4451 return &pset_node0;
4452 }
4453
4454 /*
4455 * choose_starting_pset:
4456 *
4457 * Choose a starting processor set for the thread.
4458 * May return a processor hint within the pset.
4459 *
4460 * Returns a starting processor set, to be used by
4461 * choose_processor.
4462 *
4463 * The thread must be locked. The resulting pset is unlocked on return,
4464 * and is chosen without taking any pset locks.
4465 */
4466 processor_set_t
4467 choose_starting_pset(pset_node_t node, thread_t thread, processor_t *processor_hint)
4468 {
4469 processor_set_t pset;
4470 processor_t processor = PROCESSOR_NULL;
4471
4472 if (thread->affinity_set != AFFINITY_SET_NULL) {
4473 /*
4474 * Use affinity set policy hint.
4475 */
4476 pset = thread->affinity_set->aset_pset;
4477 } else if (thread->last_processor != PROCESSOR_NULL) {
4478 /*
4479 * Simple (last processor) affinity case.
4480 */
4481 processor = thread->last_processor;
4482 pset = processor->processor_set;
4483 } else {
4484 /*
4485 * No Affinity case:
4486 *
4487 * Utilitize a per task hint to spread threads
4488 * among the available processor sets.
4489 * NRG this seems like the wrong thing to do.
4490 * See also task->pset_hint = pset in thread_setrun()
4491 */
4492 task_t task = thread->task;
4493
4494 pset = task->pset_hint;
4495 if (pset == PROCESSOR_SET_NULL) {
4496 pset = current_processor()->processor_set;
4497 }
4498
4499 pset = choose_next_pset(pset);
4500 }
4501
4502 if (!bit_test(node->pset_map, pset->pset_id)) {
4503 /* pset is not from this node so choose one that is */
4504 int id = lsb_first(node->pset_map);
4505 assert(id >= 0);
4506 pset = pset_array[id];
4507 }
4508
4509 if (bit_count(node->pset_map) == 1) {
4510 /* Only a single pset in this node */
4511 goto out;
4512 }
4513
4514 bool avoid_cpu0 = false;
4515
4516 #if defined(__x86_64__)
4517 if ((thread->sched_pri >= BASEPRI_RTQUEUES) && sched_avoid_cpu0) {
4518 /* Avoid the pset containing cpu0 */
4519 avoid_cpu0 = true;
4520 /* Assert that cpu0 is in pset0. I expect this to be true on __x86_64__ */
4521 assert(bit_test(pset_array[0]->cpu_bitmask, 0));
4522 }
4523 #endif
4524
4525 if (thread->sched_pri >= BASEPRI_RTQUEUES) {
4526 pset_map_t rt_target_map = atomic_load(&node->pset_non_rt_primary_map);
4527 if ((avoid_cpu0 && pset->pset_id == 0) || !bit_test(rt_target_map, pset->pset_id)) {
4528 if (avoid_cpu0) {
4529 rt_target_map = bit_ror64(rt_target_map, 1);
4530 }
4531 int rotid = lsb_first(rt_target_map);
4532 if (rotid >= 0) {
4533 int id = avoid_cpu0 ? ((rotid + 1) & 63) : rotid;
4534 pset = pset_array[id];
4535 goto out;
4536 }
4537 }
4538 if (!pset->is_SMT || !sched_allow_rt_smt) {
4539 /* All psets are full of RT threads - fall back to choose processor to find the furthest deadline RT thread */
4540 goto out;
4541 }
4542 rt_target_map = atomic_load(&node->pset_non_rt_map);
4543 if ((avoid_cpu0 && pset->pset_id == 0) || !bit_test(rt_target_map, pset->pset_id)) {
4544 if (avoid_cpu0) {
4545 rt_target_map = bit_ror64(rt_target_map, 1);
4546 }
4547 int rotid = lsb_first(rt_target_map);
4548 if (rotid >= 0) {
4549 int id = avoid_cpu0 ? ((rotid + 1) & 63) : rotid;
4550 pset = pset_array[id];
4551 goto out;
4552 }
4553 }
4554 /* All psets are full of RT threads - fall back to choose processor to find the furthest deadline RT thread */
4555 } else {
4556 pset_map_t idle_map = atomic_load(&node->pset_idle_map);
4557 if (!bit_test(idle_map, pset->pset_id)) {
4558 int next_idle_pset_id = lsb_first(idle_map);
4559 if (next_idle_pset_id >= 0) {
4560 pset = pset_array[next_idle_pset_id];
4561 }
4562 }
4563 }
4564
4565 out:
4566 if ((processor != PROCESSOR_NULL) && (processor->processor_set != pset)) {
4567 processor = PROCESSOR_NULL;
4568 }
4569 if (processor != PROCESSOR_NULL) {
4570 *processor_hint = processor;
4571 }
4572
4573 return pset;
4574 }
4575
4576 /*
4577 * thread_setrun:
4578 *
4579 * Dispatch thread for execution, onto an idle
4580 * processor or run queue, and signal a preemption
4581 * as appropriate.
4582 *
4583 * Thread must be locked.
4584 */
4585 void
4586 thread_setrun(
4587 thread_t thread,
4588 sched_options_t options)
4589 {
4590 processor_t processor;
4591 processor_set_t pset;
4592
4593 assert((thread->state & (TH_RUN | TH_WAIT | TH_UNINT | TH_TERMINATE | TH_TERMINATE2)) == TH_RUN);
4594 assert(thread->runq == PROCESSOR_NULL);
4595
4596 /*
4597 * Update priority if needed.
4598 */
4599 if (SCHED(can_update_priority)(thread)) {
4600 SCHED(update_priority)(thread);
4601 }
4602
4603 thread->sfi_class = sfi_thread_classify(thread);
4604
4605 assert(thread->runq == PROCESSOR_NULL);
4606
4607 if (thread->bound_processor == PROCESSOR_NULL) {
4608 /*
4609 * Unbound case.
4610 */
4611 processor_t processor_hint = PROCESSOR_NULL;
4612 pset_node_t node = SCHED(choose_node)(thread);
4613 processor_set_t starting_pset = choose_starting_pset(node, thread, &processor_hint);
4614
4615 pset_lock(starting_pset);
4616
4617 processor = SCHED(choose_processor)(starting_pset, processor_hint, thread);
4618 pset = processor->processor_set;
4619 task_t task = thread->task;
4620 task->pset_hint = pset; /* NRG this is done without holding the task lock */
4621
4622 SCHED_DEBUG_CHOOSE_PROCESSOR_KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_CHOOSE_PROCESSOR) | DBG_FUNC_NONE,
4623 (uintptr_t)thread_tid(thread), (uintptr_t)-1, processor->cpu_id, processor->state, 0);
4624 } else {
4625 /*
4626 * Bound case:
4627 *
4628 * Unconditionally dispatch on the processor.
4629 */
4630 processor = thread->bound_processor;
4631 pset = processor->processor_set;
4632 pset_lock(pset);
4633
4634 SCHED_DEBUG_CHOOSE_PROCESSOR_KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_CHOOSE_PROCESSOR) | DBG_FUNC_NONE,
4635 (uintptr_t)thread_tid(thread), (uintptr_t)-2, processor->cpu_id, processor->state, 0);
4636 }
4637
4638 /*
4639 * Dispatch the thread on the chosen processor.
4640 * TODO: This should be based on sched_mode, not sched_pri
4641 */
4642 if (thread->sched_pri >= BASEPRI_RTQUEUES) {
4643 realtime_setrun(processor, thread);
4644 } else {
4645 processor_setrun(processor, thread, options);
4646 }
4647 /* pset is now unlocked */
4648 if (thread->bound_processor == PROCESSOR_NULL) {
4649 SCHED(check_spill)(pset, thread);
4650 }
4651 }
4652
4653 processor_set_t
4654 task_choose_pset(
4655 task_t task)
4656 {
4657 processor_set_t pset = task->pset_hint;
4658
4659 if (pset != PROCESSOR_SET_NULL) {
4660 pset = choose_next_pset(pset);
4661 }
4662
4663 return pset;
4664 }
4665
4666 /*
4667 * Check for a preemption point in
4668 * the current context.
4669 *
4670 * Called at splsched with thread locked.
4671 */
4672 ast_t
4673 csw_check(
4674 thread_t thread,
4675 processor_t processor,
4676 ast_t check_reason)
4677 {
4678 processor_set_t pset = processor->processor_set;
4679
4680 assert(thread == processor->active_thread);
4681
4682 pset_lock(pset);
4683
4684 processor_state_update_from_thread(processor, thread);
4685
4686 ast_t preempt = csw_check_locked(thread, processor, pset, check_reason);
4687
4688 /* Acknowledge the IPI if we decided not to preempt */
4689
4690 if ((preempt & AST_URGENT) == 0) {
4691 bit_clear(pset->pending_AST_URGENT_cpu_mask, processor->cpu_id);
4692 }
4693
4694 if ((preempt & AST_PREEMPT) == 0) {
4695 bit_clear(pset->pending_AST_PREEMPT_cpu_mask, processor->cpu_id);
4696 }
4697
4698 pset_unlock(pset);
4699
4700 return preempt;
4701 }
4702
4703 /*
4704 * Check for preemption at splsched with
4705 * pset and thread locked
4706 */
4707 ast_t
4708 csw_check_locked(
4709 thread_t thread,
4710 processor_t processor,
4711 processor_set_t pset,
4712 ast_t check_reason)
4713 {
4714 ast_t result;
4715
4716 if (processor->first_timeslice) {
4717 if (rt_runq_count(pset) > 0) {
4718 return check_reason | AST_PREEMPT | AST_URGENT;
4719 }
4720 } else {
4721 if (rt_runq_count(pset) > 0) {
4722 if (BASEPRI_RTQUEUES > processor->current_pri) {
4723 return check_reason | AST_PREEMPT | AST_URGENT;
4724 } else {
4725 return check_reason | AST_PREEMPT;
4726 }
4727 }
4728 }
4729
4730 /*
4731 * If the current thread is running on a processor that is no longer recommended,
4732 * urgently preempt it, at which point thread_select() should
4733 * try to idle the processor and re-dispatch the thread to a recommended processor.
4734 */
4735 if (!processor->is_recommended) {
4736 return check_reason | AST_PREEMPT | AST_URGENT;
4737 }
4738
4739 result = SCHED(processor_csw_check)(processor);
4740 if (result != AST_NONE) {
4741 return check_reason | result | (thread_is_eager_preempt(thread) ? AST_URGENT : AST_NONE);
4742 }
4743
4744 /*
4745 * Same for avoid-processor
4746 *
4747 * TODO: Should these set AST_REBALANCE?
4748 */
4749 if (SCHED(avoid_processor_enabled) && SCHED(thread_avoid_processor)(processor, thread)) {
4750 return check_reason | AST_PREEMPT;
4751 }
4752
4753 /*
4754 * Even though we could continue executing on this processor, a
4755 * secondary SMT core should try to shed load to another primary core.
4756 *
4757 * TODO: Should this do the same check that thread_select does? i.e.
4758 * if no bound threads target this processor, and idle primaries exist, preempt
4759 * The case of RT threads existing is already taken care of above
4760 */
4761
4762 if (processor->current_pri < BASEPRI_RTQUEUES &&
4763 processor->processor_primary != processor) {
4764 return check_reason | AST_PREEMPT;
4765 }
4766
4767 if (thread->state & TH_SUSP) {
4768 return check_reason | AST_PREEMPT;
4769 }
4770
4771 #if CONFIG_SCHED_SFI
4772 /*
4773 * Current thread may not need to be preempted, but maybe needs
4774 * an SFI wait?
4775 */
4776 result = sfi_thread_needs_ast(thread, NULL);
4777 if (result != AST_NONE) {
4778 return check_reason | result;
4779 }
4780 #endif
4781
4782 return AST_NONE;
4783 }
4784
4785 /*
4786 * Handle preemption IPI or IPI in response to setting an AST flag
4787 * Triggered by cause_ast_check
4788 * Called at splsched
4789 */
4790 void
4791 ast_check(processor_t processor)
4792 {
4793 if (processor->state != PROCESSOR_RUNNING &&
4794 processor->state != PROCESSOR_SHUTDOWN) {
4795 return;
4796 }
4797
4798 thread_t thread = processor->active_thread;
4799
4800 assert(thread == current_thread());
4801
4802 thread_lock(thread);
4803
4804 /*
4805 * Propagate thread ast to processor.
4806 * (handles IPI in response to setting AST flag)
4807 */
4808 ast_propagate(thread);
4809
4810 /*
4811 * Stash the old urgency and perfctl values to find out if
4812 * csw_check updates them.
4813 */
4814 thread_urgency_t old_urgency = processor->current_urgency;
4815 perfcontrol_class_t old_perfctl_class = processor->current_perfctl_class;
4816
4817 ast_t preempt;
4818
4819 if ((preempt = csw_check(thread, processor, AST_NONE)) != AST_NONE) {
4820 ast_on(preempt);
4821 }
4822
4823 if (old_urgency != processor->current_urgency) {
4824 /*
4825 * Urgency updates happen with the thread lock held (ugh).
4826 * TODO: This doesn't notice QoS changes...
4827 */
4828 uint64_t urgency_param1, urgency_param2;
4829
4830 thread_urgency_t urgency = thread_get_urgency(thread, &urgency_param1, &urgency_param2);
4831 thread_tell_urgency(urgency, urgency_param1, urgency_param2, 0, thread);
4832 }
4833
4834 thread_unlock(thread);
4835
4836 if (old_perfctl_class != processor->current_perfctl_class) {
4837 /*
4838 * We updated the perfctl class of this thread from another core.
4839 * Let CLPC know that the currently running thread has a new
4840 * class.
4841 */
4842
4843 machine_switch_perfcontrol_state_update(PERFCONTROL_ATTR_UPDATE,
4844 mach_approximate_time(), 0, thread);
4845 }
4846 }
4847
4848
4849 /*
4850 * set_sched_pri:
4851 *
4852 * Set the scheduled priority of the specified thread.
4853 *
4854 * This may cause the thread to change queues.
4855 *
4856 * Thread must be locked.
4857 */
4858 void
4859 set_sched_pri(
4860 thread_t thread,
4861 int16_t new_priority,
4862 set_sched_pri_options_t options)
4863 {
4864 bool is_current_thread = (thread == current_thread());
4865 bool removed_from_runq = false;
4866 bool lazy_update = ((options & SETPRI_LAZY) == SETPRI_LAZY);
4867
4868 int16_t old_priority = thread->sched_pri;
4869
4870 /* If we're already at this priority, no need to mess with the runqueue */
4871 if (new_priority == old_priority) {
4872 #if CONFIG_SCHED_CLUTCH
4873 /* For the first thread in the system, the priority is correct but
4874 * th_sched_bucket is still TH_BUCKET_RUN. Since the clutch
4875 * scheduler relies on the bucket being set for all threads, update
4876 * its bucket here.
4877 */
4878 if (thread->th_sched_bucket == TH_BUCKET_RUN) {
4879 assert(is_current_thread);
4880 SCHED(update_thread_bucket)(thread);
4881 }
4882 #endif /* CONFIG_SCHED_CLUTCH */
4883
4884 return;
4885 }
4886
4887 if (is_current_thread) {
4888 assert(thread->state & TH_RUN);
4889 assert(thread->runq == PROCESSOR_NULL);
4890 } else {
4891 removed_from_runq = thread_run_queue_remove(thread);
4892 }
4893
4894 thread->sched_pri = new_priority;
4895
4896 #if CONFIG_SCHED_CLUTCH
4897 /*
4898 * Since for the clutch scheduler, the thread's bucket determines its runq
4899 * in the hierarchy it is important to update the bucket when the thread
4900 * lock is held and the thread has been removed from the runq hierarchy.
4901 */
4902 SCHED(update_thread_bucket)(thread);
4903
4904 #endif /* CONFIG_SCHED_CLUTCH */
4905
4906 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_CHANGE_PRIORITY),
4907 (uintptr_t)thread_tid(thread),
4908 thread->base_pri,
4909 thread->sched_pri,
4910 thread->sched_usage,
4911 0);
4912
4913 if (removed_from_runq) {
4914 thread_run_queue_reinsert(thread, SCHED_PREEMPT | SCHED_TAILQ);
4915 } else if (is_current_thread) {
4916 processor_t processor = thread->last_processor;
4917 assert(processor == current_processor());
4918
4919 thread_urgency_t old_urgency = processor->current_urgency;
4920
4921 /*
4922 * When dropping in priority, check if the thread no longer belongs on core.
4923 * If a thread raises its own priority, don't aggressively rebalance it.
4924 * <rdar://problem/31699165>
4925 *
4926 * csw_check does a processor_state_update_from_thread, but
4927 * we should do our own if we're being lazy.
4928 */
4929 if (!lazy_update && new_priority < old_priority) {
4930 ast_t preempt;
4931
4932 if ((preempt = csw_check(thread, processor, AST_NONE)) != AST_NONE) {
4933 ast_on(preempt);
4934 }
4935 } else {
4936 processor_state_update_from_thread(processor, thread);
4937 }
4938
4939 /*
4940 * set_sched_pri doesn't alter RT params. We expect direct base priority/QoS
4941 * class alterations from user space to occur relatively infrequently, hence
4942 * those are lazily handled. QoS classes have distinct priority bands, and QoS
4943 * inheritance is expected to involve priority changes.
4944 */
4945 if (processor->current_urgency != old_urgency) {
4946 uint64_t urgency_param1, urgency_param2;
4947
4948 thread_urgency_t new_urgency = thread_get_urgency(thread,
4949 &urgency_param1, &urgency_param2);
4950
4951 thread_tell_urgency(new_urgency, urgency_param1,
4952 urgency_param2, 0, thread);
4953 }
4954
4955 /* TODO: only call this if current_perfctl_class changed */
4956 uint64_t ctime = mach_approximate_time();
4957 machine_thread_going_on_core(thread, processor->current_urgency, 0, 0, ctime);
4958 } else if (thread->state & TH_RUN) {
4959 processor_t processor = thread->last_processor;
4960
4961 if (!lazy_update &&
4962 processor != PROCESSOR_NULL &&
4963 processor != current_processor() &&
4964 processor->active_thread == thread) {
4965 cause_ast_check(processor);
4966 }
4967 }
4968 }
4969
4970 /*
4971 * thread_run_queue_remove_for_handoff
4972 *
4973 * Pull a thread or its (recursive) push target out of the runqueue
4974 * so that it is ready for thread_run()
4975 *
4976 * Called at splsched
4977 *
4978 * Returns the thread that was pulled or THREAD_NULL if no thread could be pulled.
4979 * This may be different than the thread that was passed in.
4980 */
4981 thread_t
4982 thread_run_queue_remove_for_handoff(thread_t thread)
4983 {
4984 thread_t pulled_thread = THREAD_NULL;
4985
4986 thread_lock(thread);
4987
4988 /*
4989 * Check that the thread is not bound to a different processor,
4990 * NO_SMT flag is not set on the thread, cluster type of
4991 * processor matches with thread if the thread is pinned to a
4992 * particular cluster and that realtime is not involved.
4993 *
4994 * Next, pull it off its run queue. If it doesn't come, it's not eligible.
4995 */
4996 processor_t processor = current_processor();
4997 if ((thread->bound_processor == PROCESSOR_NULL || thread->bound_processor == processor)
4998 && (!thread_no_smt(thread))
4999 && (processor->current_pri < BASEPRI_RTQUEUES)
5000 && (thread->sched_pri < BASEPRI_RTQUEUES)
5001 #if __AMP__
5002 && ((!(thread->sched_flags & TH_SFLAG_PCORE_ONLY)) ||
5003 processor->processor_set->pset_cluster_type == PSET_AMP_P)
5004 && ((!(thread->sched_flags & TH_SFLAG_ECORE_ONLY)) ||
5005 processor->processor_set->pset_cluster_type == PSET_AMP_E)
5006 #endif /* __AMP__ */
5007 ) {
5008 if (thread_run_queue_remove(thread)) {
5009 pulled_thread = thread;
5010 }
5011 }
5012
5013 thread_unlock(thread);
5014
5015 return pulled_thread;
5016 }
5017
5018 /*
5019 * thread_prepare_for_handoff
5020 *
5021 * Make the thread ready for handoff.
5022 * If the thread was runnable then pull it off the runq, if the thread could
5023 * not be pulled, return NULL.
5024 *
5025 * If the thread was woken up from wait for handoff, make sure it is not bound to
5026 * different processor.
5027 *
5028 * Called at splsched
5029 *
5030 * Returns the thread that was pulled or THREAD_NULL if no thread could be pulled.
5031 * This may be different than the thread that was passed in.
5032 */
5033 thread_t
5034 thread_prepare_for_handoff(thread_t thread, thread_handoff_option_t option)
5035 {
5036 thread_t pulled_thread = THREAD_NULL;
5037
5038 if (option & THREAD_HANDOFF_SETRUN_NEEDED) {
5039 processor_t processor = current_processor();
5040 thread_lock(thread);
5041
5042 /*
5043 * Check that the thread is not bound to a different processor,
5044 * NO_SMT flag is not set on the thread and cluster type of
5045 * processor matches with thread if the thread is pinned to a
5046 * particular cluster. Call setrun instead if above conditions
5047 * are not satisfied.
5048 */
5049 if ((thread->bound_processor == PROCESSOR_NULL || thread->bound_processor == processor)
5050 && (!thread_no_smt(thread))
5051 #if __AMP__
5052 && ((!(thread->sched_flags & TH_SFLAG_PCORE_ONLY)) ||
5053 processor->processor_set->pset_cluster_type == PSET_AMP_P)
5054 && ((!(thread->sched_flags & TH_SFLAG_ECORE_ONLY)) ||
5055 processor->processor_set->pset_cluster_type == PSET_AMP_E)
5056 #endif /* __AMP__ */
5057 ) {
5058 pulled_thread = thread;
5059 } else {
5060 thread_setrun(thread, SCHED_PREEMPT | SCHED_TAILQ);
5061 }
5062 thread_unlock(thread);
5063 } else {
5064 pulled_thread = thread_run_queue_remove_for_handoff(thread);
5065 }
5066
5067 return pulled_thread;
5068 }
5069
5070 /*
5071 * thread_run_queue_remove:
5072 *
5073 * Remove a thread from its current run queue and
5074 * return TRUE if successful.
5075 *
5076 * Thread must be locked.
5077 *
5078 * If thread->runq is PROCESSOR_NULL, the thread will not re-enter the
5079 * run queues because the caller locked the thread. Otherwise
5080 * the thread is on a run queue, but could be chosen for dispatch
5081 * and removed by another processor under a different lock, which
5082 * will set thread->runq to PROCESSOR_NULL.
5083 *
5084 * Hence the thread select path must not rely on anything that could
5085 * be changed under the thread lock after calling this function,
5086 * most importantly thread->sched_pri.
5087 */
5088 boolean_t
5089 thread_run_queue_remove(
5090 thread_t thread)
5091 {
5092 boolean_t removed = FALSE;
5093 processor_t processor = thread->runq;
5094
5095 if ((thread->state & (TH_RUN | TH_WAIT)) == TH_WAIT) {
5096 /* Thread isn't runnable */
5097 assert(thread->runq == PROCESSOR_NULL);
5098 return FALSE;
5099 }
5100
5101 if (processor == PROCESSOR_NULL) {
5102 /*
5103 * The thread is either not on the runq,
5104 * or is in the midst of being removed from the runq.
5105 *
5106 * runq is set to NULL under the pset lock, not the thread
5107 * lock, so the thread may still be in the process of being dequeued
5108 * from the runq. It will wait in invoke for the thread lock to be
5109 * dropped.
5110 */
5111
5112 return FALSE;
5113 }
5114
5115 if (thread->sched_pri < BASEPRI_RTQUEUES) {
5116 return SCHED(processor_queue_remove)(processor, thread);
5117 }
5118
5119 processor_set_t pset = processor->processor_set;
5120
5121 pset_lock(pset);
5122
5123 if (thread->runq != PROCESSOR_NULL) {
5124 /*
5125 * Thread is on the RT run queue and we have a lock on
5126 * that run queue.
5127 */
5128
5129 remqueue(&thread->runq_links);
5130 SCHED_STATS_RUNQ_CHANGE(&SCHED(rt_runq)(pset)->runq_stats, rt_runq_count(pset));
5131 rt_runq_count_decr(pset);
5132
5133 thread->runq = PROCESSOR_NULL;
5134
5135 removed = TRUE;
5136 }
5137
5138 pset_unlock(pset);
5139
5140 return removed;
5141 }
5142
5143 /*
5144 * Put the thread back where it goes after a thread_run_queue_remove
5145 *
5146 * Thread must have been removed under the same thread lock hold
5147 *
5148 * thread locked, at splsched
5149 */
5150 void
5151 thread_run_queue_reinsert(thread_t thread, sched_options_t options)
5152 {
5153 assert(thread->runq == PROCESSOR_NULL);
5154 assert(thread->state & (TH_RUN));
5155
5156 thread_setrun(thread, options);
5157 }
5158
5159 void
5160 sys_override_cpu_throttle(boolean_t enable_override)
5161 {
5162 if (enable_override) {
5163 cpu_throttle_enabled = 0;
5164 } else {
5165 cpu_throttle_enabled = 1;
5166 }
5167 }
5168
5169 thread_urgency_t
5170 thread_get_urgency(thread_t thread, uint64_t *arg1, uint64_t *arg2)
5171 {
5172 uint64_t urgency_param1 = 0, urgency_param2 = 0;
5173
5174 thread_urgency_t urgency;
5175
5176 if (thread == NULL || (thread->state & TH_IDLE)) {
5177 urgency_param1 = 0;
5178 urgency_param2 = 0;
5179
5180 urgency = THREAD_URGENCY_NONE;
5181 } else if (thread->sched_mode == TH_MODE_REALTIME) {
5182 urgency_param1 = thread->realtime.period;
5183 urgency_param2 = thread->realtime.deadline;
5184
5185 urgency = THREAD_URGENCY_REAL_TIME;
5186 } else if (cpu_throttle_enabled &&
5187 (thread->sched_pri <= MAXPRI_THROTTLE) &&
5188 (thread->base_pri <= MAXPRI_THROTTLE)) {
5189 /*
5190 * Threads that are running at low priority but are not
5191 * tagged with a specific QoS are separated out from
5192 * the "background" urgency. Performance management
5193 * subsystem can decide to either treat these threads
5194 * as normal threads or look at other signals like thermal
5195 * levels for optimal power/perf tradeoffs for a platform.
5196 */
5197 boolean_t thread_lacks_qos = (proc_get_effective_thread_policy(thread, TASK_POLICY_QOS) == THREAD_QOS_UNSPECIFIED); //thread_has_qos_policy(thread);
5198 boolean_t task_is_suppressed = (proc_get_effective_task_policy(thread->task, TASK_POLICY_SUP_ACTIVE) == 0x1);
5199
5200 /*
5201 * Background urgency applied when thread priority is
5202 * MAXPRI_THROTTLE or lower and thread is not promoted
5203 * and thread has a QoS specified
5204 */
5205 urgency_param1 = thread->sched_pri;
5206 urgency_param2 = thread->base_pri;
5207
5208 if (thread_lacks_qos && !task_is_suppressed) {
5209 urgency = THREAD_URGENCY_LOWPRI;
5210 } else {
5211 urgency = THREAD_URGENCY_BACKGROUND;
5212 }
5213 } else {
5214 /* For otherwise unclassified threads, report throughput QoS parameters */
5215 urgency_param1 = proc_get_effective_thread_policy(thread, TASK_POLICY_THROUGH_QOS);
5216 urgency_param2 = proc_get_effective_task_policy(thread->task, TASK_POLICY_THROUGH_QOS);
5217 urgency = THREAD_URGENCY_NORMAL;
5218 }
5219
5220 if (arg1 != NULL) {
5221 *arg1 = urgency_param1;
5222 }
5223 if (arg2 != NULL) {
5224 *arg2 = urgency_param2;
5225 }
5226
5227 return urgency;
5228 }
5229
5230 perfcontrol_class_t
5231 thread_get_perfcontrol_class(thread_t thread)
5232 {
5233 /* Special case handling */
5234 if (thread->state & TH_IDLE) {
5235 return PERFCONTROL_CLASS_IDLE;
5236 }
5237 if (thread->task == kernel_task) {
5238 return PERFCONTROL_CLASS_KERNEL;
5239 }
5240 if (thread->sched_mode == TH_MODE_REALTIME) {
5241 return PERFCONTROL_CLASS_REALTIME;
5242 }
5243
5244 /* perfcontrol_class based on base_pri */
5245 if (thread->base_pri <= MAXPRI_THROTTLE) {
5246 return PERFCONTROL_CLASS_BACKGROUND;
5247 } else if (thread->base_pri <= BASEPRI_UTILITY) {
5248 return PERFCONTROL_CLASS_UTILITY;
5249 } else if (thread->base_pri <= BASEPRI_DEFAULT) {
5250 return PERFCONTROL_CLASS_NONUI;
5251 } else if (thread->base_pri <= BASEPRI_FOREGROUND) {
5252 return PERFCONTROL_CLASS_UI;
5253 } else {
5254 return PERFCONTROL_CLASS_ABOVEUI;
5255 }
5256 }
5257
5258 /*
5259 * This is the processor idle loop, which just looks for other threads
5260 * to execute. Processor idle threads invoke this without supplying a
5261 * current thread to idle without an asserted wait state.
5262 *
5263 * Returns a the next thread to execute if dispatched directly.
5264 */
5265
5266 #if 0
5267 #define IDLE_KERNEL_DEBUG_CONSTANT(...) KERNEL_DEBUG_CONSTANT(__VA_ARGS__)
5268 #else
5269 #define IDLE_KERNEL_DEBUG_CONSTANT(...) do { } while(0)
5270 #endif
5271
5272 thread_t
5273 processor_idle(
5274 thread_t thread,
5275 processor_t processor)
5276 {
5277 processor_set_t pset = processor->processor_set;
5278
5279 (void)splsched();
5280
5281 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
5282 MACHDBG_CODE(DBG_MACH_SCHED, MACH_IDLE) | DBG_FUNC_START,
5283 (uintptr_t)thread_tid(thread), 0, 0, 0, 0);
5284
5285 SCHED_STATS_INC(idle_transitions);
5286 assert(processor->running_timers_active == false);
5287
5288 uint64_t ctime = mach_absolute_time();
5289
5290 timer_switch(&processor->system_state, ctime, &processor->idle_state);
5291 processor->current_state = &processor->idle_state;
5292
5293 cpu_quiescent_counter_leave(ctime);
5294
5295 while (1) {
5296 /*
5297 * Ensure that updates to my processor and pset state,
5298 * made by the IPI source processor before sending the IPI,
5299 * are visible on this processor now (even though we don't
5300 * take the pset lock yet).
5301 */
5302 atomic_thread_fence(memory_order_acquire);
5303
5304 if (processor->state != PROCESSOR_IDLE) {
5305 break;
5306 }
5307 if (bit_test(pset->pending_AST_URGENT_cpu_mask, processor->cpu_id)) {
5308 break;
5309 }
5310 #if defined(CONFIG_SCHED_DEFERRED_AST)
5311 if (bit_test(pset->pending_deferred_AST_cpu_mask, processor->cpu_id)) {
5312 break;
5313 }
5314 #endif
5315 if (processor->is_recommended && (processor->processor_primary == processor)) {
5316 if (rt_runq_count(pset)) {
5317 break;
5318 }
5319 } else {
5320 if (SCHED(processor_bound_count)(processor)) {
5321 break;
5322 }
5323 }
5324
5325 IDLE_KERNEL_DEBUG_CONSTANT(
5326 MACHDBG_CODE(DBG_MACH_SCHED, MACH_IDLE) | DBG_FUNC_NONE, (uintptr_t)thread_tid(thread), rt_runq_count(pset), SCHED(processor_runq_count)(processor), -1, 0);
5327
5328 machine_track_platform_idle(TRUE);
5329
5330 machine_idle();
5331 /* returns with interrupts enabled */
5332
5333 machine_track_platform_idle(FALSE);
5334
5335 (void)splsched();
5336
5337 /*
5338 * Check if we should call sched_timeshare_consider_maintenance() here.
5339 * The CPU was woken out of idle due to an interrupt and we should do the
5340 * call only if the processor is still idle. If the processor is non-idle,
5341 * the threads running on the processor would do the call as part of
5342 * context swithing.
5343 */
5344 if (processor->state == PROCESSOR_IDLE) {
5345 sched_timeshare_consider_maintenance(mach_absolute_time());
5346 }
5347
5348 IDLE_KERNEL_DEBUG_CONSTANT(
5349 MACHDBG_CODE(DBG_MACH_SCHED, MACH_IDLE) | DBG_FUNC_NONE, (uintptr_t)thread_tid(thread), rt_runq_count(pset), SCHED(processor_runq_count)(processor), -2, 0);
5350
5351 if (!SCHED(processor_queue_empty)(processor)) {
5352 /* Secondary SMT processors respond to directed wakeups
5353 * exclusively. Some platforms induce 'spurious' SMT wakeups.
5354 */
5355 if (processor->processor_primary == processor) {
5356 break;
5357 }
5358 }
5359 }
5360
5361 ctime = mach_absolute_time();
5362
5363 timer_switch(&processor->idle_state, ctime, &processor->system_state);
5364 processor->current_state = &processor->system_state;
5365
5366 cpu_quiescent_counter_join(ctime);
5367
5368 ast_t reason = AST_NONE;
5369
5370 /* We're handling all scheduling AST's */
5371 ast_off(AST_SCHEDULING);
5372
5373 /*
5374 * thread_select will move the processor from dispatching to running,
5375 * or put it in idle if there's nothing to do.
5376 */
5377 thread_t current_thread = current_thread();
5378
5379 thread_lock(current_thread);
5380 thread_t new_thread = thread_select(current_thread, processor, &reason);
5381 thread_unlock(current_thread);
5382
5383 assert(processor->running_timers_active == false);
5384
5385 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
5386 MACHDBG_CODE(DBG_MACH_SCHED, MACH_IDLE) | DBG_FUNC_END,
5387 (uintptr_t)thread_tid(thread), processor->state, (uintptr_t)thread_tid(new_thread), reason, 0);
5388
5389 return new_thread;
5390 }
5391
5392 /*
5393 * Each processor has a dedicated thread which
5394 * executes the idle loop when there is no suitable
5395 * previous context.
5396 *
5397 * This continuation is entered with interrupts disabled.
5398 */
5399 void
5400 idle_thread(__assert_only void* parameter,
5401 __unused wait_result_t result)
5402 {
5403 assert(ml_get_interrupts_enabled() == FALSE);
5404 assert(parameter == NULL);
5405
5406 processor_t processor = current_processor();
5407
5408 /*
5409 * Ensure that anything running in idle context triggers
5410 * preemption-disabled checks.
5411 */
5412 disable_preemption();
5413
5414 /*
5415 * Enable interrupts temporarily to handle any pending interrupts
5416 * or IPIs before deciding to sleep
5417 */
5418 spllo();
5419
5420 thread_t new_thread = processor_idle(THREAD_NULL, processor);
5421 /* returns with interrupts disabled */
5422
5423 enable_preemption();
5424
5425 if (new_thread != THREAD_NULL) {
5426 thread_run(processor->idle_thread,
5427 idle_thread, NULL, new_thread);
5428 /*NOTREACHED*/
5429 }
5430
5431 thread_block(idle_thread);
5432 /*NOTREACHED*/
5433 }
5434
5435 kern_return_t
5436 idle_thread_create(
5437 processor_t processor)
5438 {
5439 kern_return_t result;
5440 thread_t thread;
5441 spl_t s;
5442 char name[MAXTHREADNAMESIZE];
5443
5444 result = kernel_thread_create(idle_thread, NULL, MAXPRI_KERNEL, &thread);
5445 if (result != KERN_SUCCESS) {
5446 return result;
5447 }
5448
5449 snprintf(name, sizeof(name), "idle #%d", processor->cpu_id);
5450 thread_set_thread_name(thread, name);
5451
5452 s = splsched();
5453 thread_lock(thread);
5454 thread->bound_processor = processor;
5455 processor->idle_thread = thread;
5456 thread->sched_pri = thread->base_pri = IDLEPRI;
5457 thread->state = (TH_RUN | TH_IDLE);
5458 thread->options |= TH_OPT_IDLE_THREAD;
5459 thread_unlock(thread);
5460 splx(s);
5461
5462 thread_deallocate(thread);
5463
5464 return KERN_SUCCESS;
5465 }
5466
5467 /*
5468 * sched_startup:
5469 *
5470 * Kicks off scheduler services.
5471 *
5472 * Called at splsched.
5473 */
5474 void
5475 sched_startup(void)
5476 {
5477 kern_return_t result;
5478 thread_t thread;
5479
5480 simple_lock_init(&sched_vm_group_list_lock, 0);
5481
5482 #if __arm__ || __arm64__
5483 simple_lock_init(&sched_recommended_cores_lock, 0);
5484 #endif /* __arm__ || __arm64__ */
5485
5486 result = kernel_thread_start_priority((thread_continue_t)sched_init_thread,
5487 NULL, MAXPRI_KERNEL, &thread);
5488 if (result != KERN_SUCCESS) {
5489 panic("sched_startup");
5490 }
5491
5492 thread_deallocate(thread);
5493
5494 assert_thread_magic(thread);
5495
5496 /*
5497 * Yield to the sched_init_thread once, to
5498 * initialize our own thread after being switched
5499 * back to.
5500 *
5501 * The current thread is the only other thread
5502 * active at this point.
5503 */
5504 thread_block(THREAD_CONTINUE_NULL);
5505 }
5506
5507 #if __arm64__
5508 static _Atomic uint64_t sched_perfcontrol_callback_deadline;
5509 #endif /* __arm64__ */
5510
5511
5512 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
5513
5514 static volatile uint64_t sched_maintenance_deadline;
5515 static uint64_t sched_tick_last_abstime;
5516 static uint64_t sched_tick_delta;
5517 uint64_t sched_tick_max_delta;
5518
5519
5520 /*
5521 * sched_init_thread:
5522 *
5523 * Perform periodic bookkeeping functions about ten
5524 * times per second.
5525 */
5526 void
5527 sched_timeshare_maintenance_continue(void)
5528 {
5529 uint64_t sched_tick_ctime, late_time;
5530
5531 struct sched_update_scan_context scan_context = {
5532 .earliest_bg_make_runnable_time = UINT64_MAX,
5533 .earliest_normal_make_runnable_time = UINT64_MAX,
5534 .earliest_rt_make_runnable_time = UINT64_MAX
5535 };
5536
5537 sched_tick_ctime = mach_absolute_time();
5538
5539 if (__improbable(sched_tick_last_abstime == 0)) {
5540 sched_tick_last_abstime = sched_tick_ctime;
5541 late_time = 0;
5542 sched_tick_delta = 1;
5543 } else {
5544 late_time = sched_tick_ctime - sched_tick_last_abstime;
5545 sched_tick_delta = late_time / sched_tick_interval;
5546 /* Ensure a delta of 1, since the interval could be slightly
5547 * smaller than the sched_tick_interval due to dispatch
5548 * latencies.
5549 */
5550 sched_tick_delta = MAX(sched_tick_delta, 1);
5551
5552 /* In the event interrupt latencies or platform
5553 * idle events that advanced the timebase resulted
5554 * in periods where no threads were dispatched,
5555 * cap the maximum "tick delta" at SCHED_TICK_MAX_DELTA
5556 * iterations.
5557 */
5558 sched_tick_delta = MIN(sched_tick_delta, SCHED_TICK_MAX_DELTA);
5559
5560 sched_tick_last_abstime = sched_tick_ctime;
5561 sched_tick_max_delta = MAX(sched_tick_delta, sched_tick_max_delta);
5562 }
5563
5564 scan_context.sched_tick_last_abstime = sched_tick_last_abstime;
5565 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_MAINTENANCE) | DBG_FUNC_START,
5566 sched_tick_delta, late_time, 0, 0, 0);
5567
5568 /* Add a number of pseudo-ticks corresponding to the elapsed interval
5569 * This could be greater than 1 if substantial intervals where
5570 * all processors are idle occur, which rarely occurs in practice.
5571 */
5572
5573 sched_tick += sched_tick_delta;
5574
5575 update_vm_info();
5576
5577 /*
5578 * Compute various averages.
5579 */
5580 compute_averages(sched_tick_delta);
5581
5582 /*
5583 * Scan the run queues for threads which
5584 * may need to be updated, and find the earliest runnable thread on the runqueue
5585 * to report its latency.
5586 */
5587 SCHED(thread_update_scan)(&scan_context);
5588
5589 SCHED(rt_runq_scan)(&scan_context);
5590
5591 uint64_t ctime = mach_absolute_time();
5592
5593 uint64_t bg_max_latency = (ctime > scan_context.earliest_bg_make_runnable_time) ?
5594 ctime - scan_context.earliest_bg_make_runnable_time : 0;
5595
5596 uint64_t default_max_latency = (ctime > scan_context.earliest_normal_make_runnable_time) ?
5597 ctime - scan_context.earliest_normal_make_runnable_time : 0;
5598
5599 uint64_t realtime_max_latency = (ctime > scan_context.earliest_rt_make_runnable_time) ?
5600 ctime - scan_context.earliest_rt_make_runnable_time : 0;
5601
5602 machine_max_runnable_latency(bg_max_latency, default_max_latency, realtime_max_latency);
5603
5604 /*
5605 * Check to see if the special sched VM group needs attention.
5606 */
5607 sched_vm_group_maintenance();
5608
5609 #if __arm__ || __arm64__
5610 /* Check to see if the recommended cores failsafe is active */
5611 sched_recommended_cores_maintenance();
5612 #endif /* __arm__ || __arm64__ */
5613
5614
5615 #if DEBUG || DEVELOPMENT
5616 #if __x86_64__
5617 #include <i386/misc_protos.h>
5618 /* Check for long-duration interrupts */
5619 mp_interrupt_watchdog();
5620 #endif /* __x86_64__ */
5621 #endif /* DEBUG || DEVELOPMENT */
5622
5623 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_MAINTENANCE) | DBG_FUNC_END,
5624 sched_pri_shifts[TH_BUCKET_SHARE_FG], sched_pri_shifts[TH_BUCKET_SHARE_BG],
5625 sched_pri_shifts[TH_BUCKET_SHARE_UT], sched_pri_shifts[TH_BUCKET_SHARE_DF], 0);
5626
5627 assert_wait((event_t)sched_timeshare_maintenance_continue, THREAD_UNINT);
5628 thread_block((thread_continue_t)sched_timeshare_maintenance_continue);
5629 /*NOTREACHED*/
5630 }
5631
5632 static uint64_t sched_maintenance_wakeups;
5633
5634 /*
5635 * Determine if the set of routines formerly driven by a maintenance timer
5636 * must be invoked, based on a deadline comparison. Signals the scheduler
5637 * maintenance thread on deadline expiration. Must be invoked at an interval
5638 * lower than the "sched_tick_interval", currently accomplished by
5639 * invocation via the quantum expiration timer and at context switch time.
5640 * Performance matters: this routine reuses a timestamp approximating the
5641 * current absolute time received from the caller, and should perform
5642 * no more than a comparison against the deadline in the common case.
5643 */
5644 void
5645 sched_timeshare_consider_maintenance(uint64_t ctime)
5646 {
5647 cpu_quiescent_counter_checkin(ctime);
5648
5649 uint64_t deadline = sched_maintenance_deadline;
5650
5651 if (__improbable(ctime >= deadline)) {
5652 if (__improbable(current_thread() == sched_maintenance_thread)) {
5653 return;
5654 }
5655 OSMemoryBarrier();
5656
5657 uint64_t ndeadline = ctime + sched_tick_interval;
5658
5659 if (__probable(os_atomic_cmpxchg(&sched_maintenance_deadline, deadline, ndeadline, seq_cst))) {
5660 thread_wakeup((event_t)sched_timeshare_maintenance_continue);
5661 sched_maintenance_wakeups++;
5662 }
5663 }
5664
5665 #if !CONFIG_SCHED_CLUTCH
5666 /*
5667 * Only non-clutch schedulers use the global load calculation EWMA algorithm. For clutch
5668 * scheduler, the load is maintained at the thread group and bucket level.
5669 */
5670 uint64_t load_compute_deadline = os_atomic_load_wide(&sched_load_compute_deadline, relaxed);
5671
5672 if (__improbable(load_compute_deadline && ctime >= load_compute_deadline)) {
5673 uint64_t new_deadline = 0;
5674 if (os_atomic_cmpxchg(&sched_load_compute_deadline, load_compute_deadline, new_deadline, relaxed)) {
5675 compute_sched_load();
5676 new_deadline = ctime + sched_load_compute_interval_abs;
5677 os_atomic_store_wide(&sched_load_compute_deadline, new_deadline, relaxed);
5678 }
5679 }
5680 #endif /* CONFIG_SCHED_CLUTCH */
5681
5682 #if __arm64__
5683 uint64_t perf_deadline = os_atomic_load(&sched_perfcontrol_callback_deadline, relaxed);
5684
5685 if (__improbable(perf_deadline && ctime >= perf_deadline)) {
5686 /* CAS in 0, if success, make callback. Otherwise let the next context switch check again. */
5687 if (os_atomic_cmpxchg(&sched_perfcontrol_callback_deadline, perf_deadline, 0, relaxed)) {
5688 machine_perfcontrol_deadline_passed(perf_deadline);
5689 }
5690 }
5691 #endif /* __arm64__ */
5692 }
5693
5694 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
5695
5696 void
5697 sched_init_thread(void)
5698 {
5699 thread_block(THREAD_CONTINUE_NULL);
5700
5701 thread_t thread = current_thread();
5702
5703 thread_set_thread_name(thread, "sched_maintenance_thread");
5704
5705 sched_maintenance_thread = thread;
5706
5707 SCHED(maintenance_continuation)();
5708
5709 /*NOTREACHED*/
5710 }
5711
5712 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
5713
5714 /*
5715 * thread_update_scan / runq_scan:
5716 *
5717 * Scan the run queues to account for timesharing threads
5718 * which need to be updated.
5719 *
5720 * Scanner runs in two passes. Pass one squirrels likely
5721 * threads away in an array, pass two does the update.
5722 *
5723 * This is necessary because the run queue is locked for
5724 * the candidate scan, but the thread is locked for the update.
5725 *
5726 * Array should be sized to make forward progress, without
5727 * disabling preemption for long periods.
5728 */
5729
5730 #define THREAD_UPDATE_SIZE 128
5731
5732 static thread_t thread_update_array[THREAD_UPDATE_SIZE];
5733 static uint32_t thread_update_count = 0;
5734
5735 /* Returns TRUE if thread was added, FALSE if thread_update_array is full */
5736 boolean_t
5737 thread_update_add_thread(thread_t thread)
5738 {
5739 if (thread_update_count == THREAD_UPDATE_SIZE) {
5740 return FALSE;
5741 }
5742
5743 thread_update_array[thread_update_count++] = thread;
5744 thread_reference_internal(thread);
5745 return TRUE;
5746 }
5747
5748 void
5749 thread_update_process_threads(void)
5750 {
5751 assert(thread_update_count <= THREAD_UPDATE_SIZE);
5752
5753 for (uint32_t i = 0; i < thread_update_count; i++) {
5754 thread_t thread = thread_update_array[i];
5755 assert_thread_magic(thread);
5756 thread_update_array[i] = THREAD_NULL;
5757
5758 spl_t s = splsched();
5759 thread_lock(thread);
5760 if (!(thread->state & (TH_WAIT)) && thread->sched_stamp != sched_tick) {
5761 SCHED(update_priority)(thread);
5762 }
5763 thread_unlock(thread);
5764 splx(s);
5765
5766 thread_deallocate(thread);
5767 }
5768
5769 thread_update_count = 0;
5770 }
5771
5772 static boolean_t
5773 runq_scan_thread(
5774 thread_t thread,
5775 sched_update_scan_context_t scan_context)
5776 {
5777 assert_thread_magic(thread);
5778
5779 if (thread->sched_stamp != sched_tick &&
5780 thread->sched_mode == TH_MODE_TIMESHARE) {
5781 if (thread_update_add_thread(thread) == FALSE) {
5782 return TRUE;
5783 }
5784 }
5785
5786 if (cpu_throttle_enabled && ((thread->sched_pri <= MAXPRI_THROTTLE) && (thread->base_pri <= MAXPRI_THROTTLE))) {
5787 if (thread->last_made_runnable_time < scan_context->earliest_bg_make_runnable_time) {
5788 scan_context->earliest_bg_make_runnable_time = thread->last_made_runnable_time;
5789 }
5790 } else {
5791 if (thread->last_made_runnable_time < scan_context->earliest_normal_make_runnable_time) {
5792 scan_context->earliest_normal_make_runnable_time = thread->last_made_runnable_time;
5793 }
5794 }
5795
5796 return FALSE;
5797 }
5798
5799 /*
5800 * Scan a runq for candidate threads.
5801 *
5802 * Returns TRUE if retry is needed.
5803 */
5804 boolean_t
5805 runq_scan(
5806 run_queue_t runq,
5807 sched_update_scan_context_t scan_context)
5808 {
5809 int count = runq->count;
5810 int queue_index;
5811
5812 assert(count >= 0);
5813
5814 if (count == 0) {
5815 return FALSE;
5816 }
5817
5818 for (queue_index = bitmap_first(runq->bitmap, NRQS);
5819 queue_index >= 0;
5820 queue_index = bitmap_next(runq->bitmap, queue_index)) {
5821 thread_t thread;
5822 circle_queue_t queue = &runq->queues[queue_index];
5823
5824 cqe_foreach_element(thread, queue, runq_links) {
5825 assert(count > 0);
5826 if (runq_scan_thread(thread, scan_context) == TRUE) {
5827 return TRUE;
5828 }
5829 count--;
5830 }
5831 }
5832
5833 return FALSE;
5834 }
5835
5836 #if CONFIG_SCHED_CLUTCH
5837
5838 boolean_t
5839 sched_clutch_timeshare_scan(
5840 queue_t thread_queue,
5841 uint16_t thread_count,
5842 sched_update_scan_context_t scan_context)
5843 {
5844 if (thread_count == 0) {
5845 return FALSE;
5846 }
5847
5848 thread_t thread;
5849 qe_foreach_element_safe(thread, thread_queue, th_clutch_timeshare_link) {
5850 if (runq_scan_thread(thread, scan_context) == TRUE) {
5851 return TRUE;
5852 }
5853 thread_count--;
5854 }
5855
5856 assert(thread_count == 0);
5857 return FALSE;
5858 }
5859
5860
5861 #endif /* CONFIG_SCHED_CLUTCH */
5862
5863 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
5864
5865 bool
5866 thread_is_eager_preempt(thread_t thread)
5867 {
5868 return thread->sched_flags & TH_SFLAG_EAGERPREEMPT;
5869 }
5870
5871 void
5872 thread_set_eager_preempt(thread_t thread)
5873 {
5874 spl_t s = splsched();
5875 thread_lock(thread);
5876
5877 assert(!thread_is_eager_preempt(thread));
5878
5879 thread->sched_flags |= TH_SFLAG_EAGERPREEMPT;
5880
5881 if (thread == current_thread()) {
5882 /* csw_check updates current_is_eagerpreempt on the processor */
5883 ast_t ast = csw_check(thread, current_processor(), AST_NONE);
5884
5885 thread_unlock(thread);
5886
5887 if (ast != AST_NONE) {
5888 thread_block_reason(THREAD_CONTINUE_NULL, NULL, ast);
5889 }
5890 } else {
5891 processor_t last_processor = thread->last_processor;
5892
5893 if (last_processor != PROCESSOR_NULL &&
5894 last_processor->state == PROCESSOR_RUNNING &&
5895 last_processor->active_thread == thread) {
5896 cause_ast_check(last_processor);
5897 }
5898
5899 thread_unlock(thread);
5900 }
5901
5902 splx(s);
5903 }
5904
5905 void
5906 thread_clear_eager_preempt(thread_t thread)
5907 {
5908 spl_t s = splsched();
5909 thread_lock(thread);
5910
5911 assert(thread_is_eager_preempt(thread));
5912
5913 thread->sched_flags &= ~TH_SFLAG_EAGERPREEMPT;
5914
5915 if (thread == current_thread()) {
5916 current_processor()->current_is_eagerpreempt = false;
5917 }
5918
5919 thread_unlock(thread);
5920 splx(s);
5921 }
5922
5923 /*
5924 * Scheduling statistics
5925 */
5926 void
5927 sched_stats_handle_csw(processor_t processor, int reasons, int selfpri, int otherpri)
5928 {
5929 struct sched_statistics *stats;
5930 boolean_t to_realtime = FALSE;
5931
5932 stats = PERCPU_GET_RELATIVE(sched_stats, processor, processor);
5933 stats->csw_count++;
5934
5935 if (otherpri >= BASEPRI_REALTIME) {
5936 stats->rt_sched_count++;
5937 to_realtime = TRUE;
5938 }
5939
5940 if ((reasons & AST_PREEMPT) != 0) {
5941 stats->preempt_count++;
5942
5943 if (selfpri >= BASEPRI_REALTIME) {
5944 stats->preempted_rt_count++;
5945 }
5946
5947 if (to_realtime) {
5948 stats->preempted_by_rt_count++;
5949 }
5950 }
5951 }
5952
5953 void
5954 sched_stats_handle_runq_change(struct runq_stats *stats, int old_count)
5955 {
5956 uint64_t timestamp = mach_absolute_time();
5957
5958 stats->count_sum += (timestamp - stats->last_change_timestamp) * old_count;
5959 stats->last_change_timestamp = timestamp;
5960 }
5961
5962 /*
5963 * For calls from assembly code
5964 */
5965 #undef thread_wakeup
5966 void
5967 thread_wakeup(
5968 event_t x);
5969
5970 void
5971 thread_wakeup(
5972 event_t x)
5973 {
5974 thread_wakeup_with_result(x, THREAD_AWAKENED);
5975 }
5976
5977 boolean_t
5978 preemption_enabled(void)
5979 {
5980 return get_preemption_level() == 0 && ml_get_interrupts_enabled();
5981 }
5982
5983 static void
5984 sched_timer_deadline_tracking_init(void)
5985 {
5986 nanoseconds_to_absolutetime(TIMER_DEADLINE_TRACKING_BIN_1_DEFAULT, &timer_deadline_tracking_bin_1);
5987 nanoseconds_to_absolutetime(TIMER_DEADLINE_TRACKING_BIN_2_DEFAULT, &timer_deadline_tracking_bin_2);
5988 }
5989
5990 #if __arm__ || __arm64__
5991
5992 uint32_t perfcontrol_requested_recommended_cores = ALL_CORES_RECOMMENDED;
5993 uint32_t perfcontrol_requested_recommended_core_count = MAX_CPUS;
5994 bool perfcontrol_failsafe_active = false;
5995 bool perfcontrol_sleep_override = false;
5996
5997 uint64_t perfcontrol_failsafe_maintenance_runnable_time;
5998 uint64_t perfcontrol_failsafe_activation_time;
5999 uint64_t perfcontrol_failsafe_deactivation_time;
6000
6001 /* data covering who likely caused it and how long they ran */
6002 #define FAILSAFE_NAME_LEN 33 /* (2*MAXCOMLEN)+1 from size of p_name */
6003 char perfcontrol_failsafe_name[FAILSAFE_NAME_LEN];
6004 int perfcontrol_failsafe_pid;
6005 uint64_t perfcontrol_failsafe_tid;
6006 uint64_t perfcontrol_failsafe_thread_timer_at_start;
6007 uint64_t perfcontrol_failsafe_thread_timer_last_seen;
6008 uint32_t perfcontrol_failsafe_recommended_at_trigger;
6009
6010 /*
6011 * Perf controller calls here to update the recommended core bitmask.
6012 * If the failsafe is active, we don't immediately apply the new value.
6013 * Instead, we store the new request and use it after the failsafe deactivates.
6014 *
6015 * If the failsafe is not active, immediately apply the update.
6016 *
6017 * No scheduler locks are held, no other locks are held that scheduler might depend on,
6018 * interrupts are enabled
6019 *
6020 * currently prototype is in osfmk/arm/machine_routines.h
6021 */
6022 void
6023 sched_perfcontrol_update_recommended_cores(uint32_t recommended_cores)
6024 {
6025 assert(preemption_enabled());
6026
6027 spl_t s = splsched();
6028 simple_lock(&sched_recommended_cores_lock, LCK_GRP_NULL);
6029
6030 perfcontrol_requested_recommended_cores = recommended_cores;
6031 perfcontrol_requested_recommended_core_count = __builtin_popcountll(recommended_cores);
6032
6033 if ((perfcontrol_failsafe_active == false) && (perfcontrol_sleep_override == false)) {
6034 sched_update_recommended_cores(perfcontrol_requested_recommended_cores & usercontrol_requested_recommended_cores);
6035 } else {
6036 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
6037 MACHDBG_CODE(DBG_MACH_SCHED, MACH_REC_CORES_FAILSAFE) | DBG_FUNC_NONE,
6038 perfcontrol_requested_recommended_cores,
6039 sched_maintenance_thread->last_made_runnable_time, 0, 0, 0);
6040 }
6041
6042 simple_unlock(&sched_recommended_cores_lock);
6043 splx(s);
6044 }
6045
6046 void
6047 sched_override_recommended_cores_for_sleep(void)
6048 {
6049 spl_t s = splsched();
6050 simple_lock(&sched_recommended_cores_lock, LCK_GRP_NULL);
6051
6052 if (perfcontrol_sleep_override == false) {
6053 perfcontrol_sleep_override = true;
6054 sched_update_recommended_cores(ALL_CORES_RECOMMENDED);
6055 }
6056
6057 simple_unlock(&sched_recommended_cores_lock);
6058 splx(s);
6059 }
6060
6061 void
6062 sched_restore_recommended_cores_after_sleep(void)
6063 {
6064 spl_t s = splsched();
6065 simple_lock(&sched_recommended_cores_lock, LCK_GRP_NULL);
6066
6067 if (perfcontrol_sleep_override == true) {
6068 perfcontrol_sleep_override = false;
6069 sched_update_recommended_cores(perfcontrol_requested_recommended_cores & usercontrol_requested_recommended_cores);
6070 }
6071
6072 simple_unlock(&sched_recommended_cores_lock);
6073 splx(s);
6074 }
6075
6076 /*
6077 * Consider whether we need to activate the recommended cores failsafe
6078 *
6079 * Called from quantum timer interrupt context of a realtime thread
6080 * No scheduler locks are held, interrupts are disabled
6081 */
6082 void
6083 sched_consider_recommended_cores(uint64_t ctime, thread_t cur_thread)
6084 {
6085 /*
6086 * Check if a realtime thread is starving the system
6087 * and bringing up non-recommended cores would help
6088 *
6089 * TODO: Is this the correct check for recommended == possible cores?
6090 * TODO: Validate the checks without the relevant lock are OK.
6091 */
6092
6093 if (__improbable(perfcontrol_failsafe_active == TRUE)) {
6094 /* keep track of how long the responsible thread runs */
6095
6096 simple_lock(&sched_recommended_cores_lock, LCK_GRP_NULL);
6097
6098 if (perfcontrol_failsafe_active == TRUE &&
6099 cur_thread->thread_id == perfcontrol_failsafe_tid) {
6100 perfcontrol_failsafe_thread_timer_last_seen = timer_grab(&cur_thread->user_timer) +
6101 timer_grab(&cur_thread->system_timer);
6102 }
6103
6104 simple_unlock(&sched_recommended_cores_lock);
6105
6106 /* we're already trying to solve the problem, so bail */
6107 return;
6108 }
6109
6110 /* The failsafe won't help if there are no more processors to enable */
6111 if (__probable(perfcontrol_requested_recommended_core_count >= processor_count)) {
6112 return;
6113 }
6114
6115 uint64_t too_long_ago = ctime - perfcontrol_failsafe_starvation_threshold;
6116
6117 /* Use the maintenance thread as our canary in the coal mine */
6118 thread_t m_thread = sched_maintenance_thread;
6119
6120 /* If it doesn't look bad, nothing to see here */
6121 if (__probable(m_thread->last_made_runnable_time >= too_long_ago)) {
6122 return;
6123 }
6124
6125 /* It looks bad, take the lock to be sure */
6126 thread_lock(m_thread);
6127
6128 if (m_thread->runq == PROCESSOR_NULL ||
6129 (m_thread->state & (TH_RUN | TH_WAIT)) != TH_RUN ||
6130 m_thread->last_made_runnable_time >= too_long_ago) {
6131 /*
6132 * Maintenance thread is either on cpu or blocked, and
6133 * therefore wouldn't benefit from more cores
6134 */
6135 thread_unlock(m_thread);
6136 return;
6137 }
6138
6139 uint64_t maintenance_runnable_time = m_thread->last_made_runnable_time;
6140
6141 thread_unlock(m_thread);
6142
6143 /*
6144 * There are cores disabled at perfcontrol's recommendation, but the
6145 * system is so overloaded that the maintenance thread can't run.
6146 * That likely means that perfcontrol can't run either, so it can't fix
6147 * the recommendation. We have to kick in a failsafe to keep from starving.
6148 *
6149 * When the maintenance thread has been starved for too long,
6150 * ignore the recommendation from perfcontrol and light up all the cores.
6151 *
6152 * TODO: Consider weird states like boot, sleep, or debugger
6153 */
6154
6155 simple_lock(&sched_recommended_cores_lock, LCK_GRP_NULL);
6156
6157 if (perfcontrol_failsafe_active == TRUE) {
6158 simple_unlock(&sched_recommended_cores_lock);
6159 return;
6160 }
6161
6162 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
6163 MACHDBG_CODE(DBG_MACH_SCHED, MACH_REC_CORES_FAILSAFE) | DBG_FUNC_START,
6164 perfcontrol_requested_recommended_cores, maintenance_runnable_time, 0, 0, 0);
6165
6166 perfcontrol_failsafe_active = TRUE;
6167 perfcontrol_failsafe_activation_time = mach_absolute_time();
6168 perfcontrol_failsafe_maintenance_runnable_time = maintenance_runnable_time;
6169 perfcontrol_failsafe_recommended_at_trigger = perfcontrol_requested_recommended_cores;
6170
6171 /* Capture some data about who screwed up (assuming that the thread on core is at fault) */
6172 task_t task = cur_thread->task;
6173 perfcontrol_failsafe_pid = task_pid(task);
6174 strlcpy(perfcontrol_failsafe_name, proc_name_address(task->bsd_info), sizeof(perfcontrol_failsafe_name));
6175
6176 perfcontrol_failsafe_tid = cur_thread->thread_id;
6177
6178 /* Blame the thread for time it has run recently */
6179 uint64_t recent_computation = (ctime - cur_thread->computation_epoch) + cur_thread->computation_metered;
6180
6181 uint64_t last_seen = timer_grab(&cur_thread->user_timer) + timer_grab(&cur_thread->system_timer);
6182
6183 /* Compute the start time of the bad behavior in terms of the thread's on core time */
6184 perfcontrol_failsafe_thread_timer_at_start = last_seen - recent_computation;
6185 perfcontrol_failsafe_thread_timer_last_seen = last_seen;
6186
6187 /* Ignore the previously recommended core configuration */
6188 sched_update_recommended_cores(ALL_CORES_RECOMMENDED);
6189
6190 simple_unlock(&sched_recommended_cores_lock);
6191 }
6192
6193 /*
6194 * Now that our bacon has been saved by the failsafe, consider whether to turn it off
6195 *
6196 * Runs in the context of the maintenance thread, no locks held
6197 */
6198 static void
6199 sched_recommended_cores_maintenance(void)
6200 {
6201 /* Common case - no failsafe, nothing to be done here */
6202 if (__probable(perfcontrol_failsafe_active == FALSE)) {
6203 return;
6204 }
6205
6206 uint64_t ctime = mach_absolute_time();
6207
6208 boolean_t print_diagnostic = FALSE;
6209 char p_name[FAILSAFE_NAME_LEN] = "";
6210
6211 spl_t s = splsched();
6212 simple_lock(&sched_recommended_cores_lock, LCK_GRP_NULL);
6213
6214 /* Check again, under the lock, to avoid races */
6215 if (perfcontrol_failsafe_active == FALSE) {
6216 goto out;
6217 }
6218
6219 /*
6220 * Ensure that the other cores get another few ticks to run some threads
6221 * If we don't have this hysteresis, the maintenance thread is the first
6222 * to run, and then it immediately kills the other cores
6223 */
6224 if ((ctime - perfcontrol_failsafe_activation_time) < perfcontrol_failsafe_starvation_threshold) {
6225 goto out;
6226 }
6227
6228 /* Capture some diagnostic state under the lock so we can print it out later */
6229
6230 int pid = perfcontrol_failsafe_pid;
6231 uint64_t tid = perfcontrol_failsafe_tid;
6232
6233 uint64_t thread_usage = perfcontrol_failsafe_thread_timer_last_seen -
6234 perfcontrol_failsafe_thread_timer_at_start;
6235 uint32_t rec_cores_before = perfcontrol_failsafe_recommended_at_trigger;
6236 uint32_t rec_cores_after = perfcontrol_requested_recommended_cores;
6237 uint64_t failsafe_duration = ctime - perfcontrol_failsafe_activation_time;
6238 strlcpy(p_name, perfcontrol_failsafe_name, sizeof(p_name));
6239
6240 print_diagnostic = TRUE;
6241
6242 /* Deactivate the failsafe and reinstate the requested recommendation settings */
6243
6244 perfcontrol_failsafe_deactivation_time = ctime;
6245 perfcontrol_failsafe_active = FALSE;
6246
6247 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
6248 MACHDBG_CODE(DBG_MACH_SCHED, MACH_REC_CORES_FAILSAFE) | DBG_FUNC_END,
6249 perfcontrol_requested_recommended_cores, failsafe_duration, 0, 0, 0);
6250
6251 sched_update_recommended_cores(perfcontrol_requested_recommended_cores & usercontrol_requested_recommended_cores);
6252
6253 out:
6254 simple_unlock(&sched_recommended_cores_lock);
6255 splx(s);
6256
6257 if (print_diagnostic) {
6258 uint64_t failsafe_duration_ms = 0, thread_usage_ms = 0;
6259
6260 absolutetime_to_nanoseconds(failsafe_duration, &failsafe_duration_ms);
6261 failsafe_duration_ms = failsafe_duration_ms / NSEC_PER_MSEC;
6262
6263 absolutetime_to_nanoseconds(thread_usage, &thread_usage_ms);
6264 thread_usage_ms = thread_usage_ms / NSEC_PER_MSEC;
6265
6266 printf("recommended core failsafe kicked in for %lld ms "
6267 "likely due to %s[%d] thread 0x%llx spending "
6268 "%lld ms on cpu at realtime priority - "
6269 "new recommendation: 0x%x -> 0x%x\n",
6270 failsafe_duration_ms, p_name, pid, tid, thread_usage_ms,
6271 rec_cores_before, rec_cores_after);
6272 }
6273 }
6274
6275 #endif /* __arm__ || __arm64__ */
6276
6277 kern_return_t
6278 sched_processor_enable(processor_t processor, boolean_t enable)
6279 {
6280 assert(preemption_enabled());
6281
6282 spl_t s = splsched();
6283 simple_lock(&sched_recommended_cores_lock, LCK_GRP_NULL);
6284
6285 if (enable) {
6286 bit_set(usercontrol_requested_recommended_cores, processor->cpu_id);
6287 } else {
6288 bit_clear(usercontrol_requested_recommended_cores, processor->cpu_id);
6289 }
6290
6291 #if __arm__ || __arm64__
6292 if ((perfcontrol_failsafe_active == false) && (perfcontrol_sleep_override == false)) {
6293 sched_update_recommended_cores(perfcontrol_requested_recommended_cores & usercontrol_requested_recommended_cores);
6294 } else {
6295 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
6296 MACHDBG_CODE(DBG_MACH_SCHED, MACH_REC_CORES_FAILSAFE) | DBG_FUNC_NONE,
6297 perfcontrol_requested_recommended_cores,
6298 sched_maintenance_thread->last_made_runnable_time, 0, 0, 0);
6299 }
6300 #else /* __arm__ || __arm64__ */
6301 sched_update_recommended_cores(usercontrol_requested_recommended_cores);
6302 #endif /* !__arm__ || __arm64__ */
6303
6304 simple_unlock(&sched_recommended_cores_lock);
6305 splx(s);
6306
6307 return KERN_SUCCESS;
6308 }
6309
6310
6311 /*
6312 * Apply a new recommended cores mask to the processors it affects
6313 * Runs after considering failsafes and such
6314 *
6315 * Iterate over processors and update their ->is_recommended field.
6316 * If a processor is running, we let it drain out at its next
6317 * quantum expiration or blocking point. If a processor is idle, there
6318 * may be more work for it to do, so IPI it.
6319 *
6320 * interrupts disabled, sched_recommended_cores_lock is held
6321 */
6322 static void
6323 sched_update_recommended_cores(uint64_t recommended_cores)
6324 {
6325 processor_set_t pset, nset;
6326 processor_t processor;
6327 uint64_t needs_exit_idle_mask = 0x0;
6328 uint32_t avail_count;
6329
6330 processor = processor_list;
6331 pset = processor->processor_set;
6332
6333 KDBG(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_UPDATE_REC_CORES) | DBG_FUNC_START,
6334 recommended_cores,
6335 #if __arm__ || __arm64__
6336 perfcontrol_failsafe_active, 0, 0);
6337 #else /* __arm__ || __arm64__ */
6338 0, 0, 0);
6339 #endif /* ! __arm__ || __arm64__ */
6340
6341 if (__builtin_popcountll(recommended_cores) == 0) {
6342 bit_set(recommended_cores, master_processor->cpu_id); /* add boot processor or we hang */
6343 }
6344
6345 /* First set recommended cores */
6346 pset_lock(pset);
6347 avail_count = 0;
6348 do {
6349 nset = processor->processor_set;
6350 if (nset != pset) {
6351 pset_unlock(pset);
6352 pset = nset;
6353 pset_lock(pset);
6354 }
6355
6356 if (bit_test(recommended_cores, processor->cpu_id)) {
6357 processor->is_recommended = TRUE;
6358 bit_set(pset->recommended_bitmask, processor->cpu_id);
6359
6360 if (processor->state == PROCESSOR_IDLE) {
6361 if (processor != current_processor()) {
6362 bit_set(needs_exit_idle_mask, processor->cpu_id);
6363 }
6364 }
6365 if (processor->state != PROCESSOR_OFF_LINE) {
6366 avail_count++;
6367 SCHED(pset_made_schedulable)(processor, pset, false);
6368 }
6369 }
6370 } while ((processor = processor->processor_list) != NULL);
6371 pset_unlock(pset);
6372
6373 /* Now shutdown not recommended cores */
6374 processor = processor_list;
6375 pset = processor->processor_set;
6376
6377 pset_lock(pset);
6378 do {
6379 nset = processor->processor_set;
6380 if (nset != pset) {
6381 pset_unlock(pset);
6382 pset = nset;
6383 pset_lock(pset);
6384 }
6385
6386 if (!bit_test(recommended_cores, processor->cpu_id)) {
6387 sched_ipi_type_t ipi_type = SCHED_IPI_NONE;
6388
6389 processor->is_recommended = FALSE;
6390 bit_clear(pset->recommended_bitmask, processor->cpu_id);
6391
6392 if ((processor->state == PROCESSOR_RUNNING) || (processor->state == PROCESSOR_DISPATCHING)) {
6393 ipi_type = SCHED_IPI_IMMEDIATE;
6394 }
6395 SCHED(processor_queue_shutdown)(processor);
6396 /* pset unlocked */
6397
6398 SCHED(rt_queue_shutdown)(processor);
6399
6400 if (ipi_type != SCHED_IPI_NONE) {
6401 if (processor == current_processor()) {
6402 ast_on(AST_PREEMPT);
6403 } else {
6404 sched_ipi_perform(processor, ipi_type);
6405 }
6406 }
6407
6408 pset_lock(pset);
6409 }
6410 } while ((processor = processor->processor_list) != NULL);
6411
6412 processor_avail_count_user = avail_count;
6413 #if defined(__x86_64__)
6414 commpage_update_active_cpus();
6415 #endif
6416
6417 pset_unlock(pset);
6418
6419 /* Issue all pending IPIs now that the pset lock has been dropped */
6420 for (int cpuid = lsb_first(needs_exit_idle_mask); cpuid >= 0; cpuid = lsb_next(needs_exit_idle_mask, cpuid)) {
6421 processor = processor_array[cpuid];
6422 machine_signal_idle(processor);
6423 }
6424
6425 KDBG(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_UPDATE_REC_CORES) | DBG_FUNC_END,
6426 needs_exit_idle_mask, 0, 0, 0);
6427 }
6428
6429 void
6430 thread_set_options(uint32_t thopt)
6431 {
6432 spl_t x;
6433 thread_t t = current_thread();
6434
6435 x = splsched();
6436 thread_lock(t);
6437
6438 t->options |= thopt;
6439
6440 thread_unlock(t);
6441 splx(x);
6442 }
6443
6444 void
6445 thread_set_pending_block_hint(thread_t thread, block_hint_t block_hint)
6446 {
6447 thread->pending_block_hint = block_hint;
6448 }
6449
6450 uint32_t
6451 qos_max_parallelism(int qos, uint64_t options)
6452 {
6453 return SCHED(qos_max_parallelism)(qos, options);
6454 }
6455
6456 uint32_t
6457 sched_qos_max_parallelism(__unused int qos, uint64_t options)
6458 {
6459 host_basic_info_data_t hinfo;
6460 mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
6461 /* Query the machine layer for core information */
6462 __assert_only kern_return_t kret = host_info(host_self(), HOST_BASIC_INFO,
6463 (host_info_t)&hinfo, &count);
6464 assert(kret == KERN_SUCCESS);
6465
6466 if (options & QOS_PARALLELISM_COUNT_LOGICAL) {
6467 return hinfo.logical_cpu;
6468 } else {
6469 return hinfo.physical_cpu;
6470 }
6471 }
6472
6473 int sched_allow_NO_SMT_threads = 1;
6474 bool
6475 thread_no_smt(thread_t thread)
6476 {
6477 return sched_allow_NO_SMT_threads && (thread->bound_processor == PROCESSOR_NULL) && ((thread->sched_flags & TH_SFLAG_NO_SMT) || (thread->task->t_flags & TF_NO_SMT));
6478 }
6479
6480 bool
6481 processor_active_thread_no_smt(processor_t processor)
6482 {
6483 return sched_allow_NO_SMT_threads && !processor->current_is_bound && processor->current_is_NO_SMT;
6484 }
6485
6486 #if __arm64__
6487
6488 /*
6489 * Set up or replace old timer with new timer
6490 *
6491 * Returns true if canceled old timer, false if it did not
6492 */
6493 boolean_t
6494 sched_perfcontrol_update_callback_deadline(uint64_t new_deadline)
6495 {
6496 /*
6497 * Exchange deadline for new deadline, if old deadline was nonzero,
6498 * then I cancelled the callback, otherwise I didn't
6499 */
6500
6501 return os_atomic_xchg(&sched_perfcontrol_callback_deadline, new_deadline,
6502 relaxed) != 0;
6503 }
6504
6505 #endif /* __arm64__ */
6506
6507 #if CONFIG_SCHED_EDGE
6508
6509 #define SCHED_PSET_LOAD_EWMA_TC_NSECS 10000000u
6510
6511 /*
6512 * sched_edge_pset_running_higher_bucket()
6513 *
6514 * Routine to calculate cumulative running counts for each scheduling
6515 * bucket. This effectively lets the load calculation calculate if a
6516 * cluster is running any threads at a QoS lower than the thread being
6517 * migrated etc.
6518 */
6519
6520 static void
6521 sched_edge_pset_running_higher_bucket(processor_set_t pset, uint32_t *running_higher)
6522 {
6523 bitmap_t *active_map = &pset->cpu_state_map[PROCESSOR_RUNNING];
6524
6525 /* Edge Scheduler Optimization */
6526 for (int cpu = bitmap_first(active_map, MAX_CPUS); cpu >= 0; cpu = bitmap_next(active_map, cpu)) {
6527 sched_bucket_t cpu_bucket = os_atomic_load(&pset->cpu_running_buckets[cpu], relaxed);
6528 for (sched_bucket_t bucket = cpu_bucket; bucket < TH_BUCKET_SCHED_MAX; bucket++) {
6529 running_higher[bucket]++;
6530 }
6531 }
6532 }
6533
6534 /*
6535 * sched_update_pset_load_average()
6536 *
6537 * Updates the load average for each sched bucket for a cluster.
6538 * This routine must be called with the pset lock held.
6539 */
6540 void
6541 sched_update_pset_load_average(processor_set_t pset, uint64_t curtime)
6542 {
6543 if (pset->online_processor_count == 0) {
6544 /* Looks like the pset is not runnable any more; nothing to do here */
6545 return;
6546 }
6547
6548 /*
6549 * Edge Scheduler Optimization
6550 *
6551 * See if more callers of this routine can pass in timestamps to avoid the
6552 * mach_absolute_time() call here.
6553 */
6554
6555 if (!curtime) {
6556 curtime = mach_absolute_time();
6557 }
6558 uint64_t last_update = os_atomic_load(&pset->pset_load_last_update, relaxed);
6559 int64_t delta_ticks = curtime - last_update;
6560 if (delta_ticks < 0) {
6561 return;
6562 }
6563
6564 uint64_t delta_nsecs = 0;
6565 absolutetime_to_nanoseconds(delta_ticks, &delta_nsecs);
6566
6567 if (__improbable(delta_nsecs > UINT32_MAX)) {
6568 delta_nsecs = UINT32_MAX;
6569 }
6570
6571 uint32_t running_higher[TH_BUCKET_SCHED_MAX] = {0};
6572 sched_edge_pset_running_higher_bucket(pset, running_higher);
6573
6574 for (sched_bucket_t sched_bucket = TH_BUCKET_FIXPRI; sched_bucket < TH_BUCKET_SCHED_MAX; sched_bucket++) {
6575 uint64_t old_load_average = os_atomic_load(&pset->pset_load_average[sched_bucket], relaxed);
6576 uint64_t old_load_average_factor = old_load_average * SCHED_PSET_LOAD_EWMA_TC_NSECS;
6577 uint32_t current_runq_depth = (sched_edge_cluster_cumulative_count(&pset->pset_clutch_root, sched_bucket) + rt_runq_count(pset) + running_higher[sched_bucket]) / pset->online_processor_count;
6578
6579 /*
6580 * For the new load average multiply current_runq_depth by delta_nsecs (which resuts in a 32.0 value).
6581 * Since we want to maintain the load average as a 24.8 fixed arithmetic value for precision, the
6582 * new load averga needs to be shifted before it can be added to the old load average.
6583 */
6584 uint64_t new_load_average_factor = (current_runq_depth * delta_nsecs) << SCHED_PSET_LOAD_EWMA_FRACTION_BITS;
6585
6586 /*
6587 * For extremely parallel workloads, it is important that the load average on a cluster moves zero to non-zero
6588 * instantly to allow threads to be migrated to other (potentially idle) clusters quickly. Hence use the EWMA
6589 * when the system is already loaded; otherwise for an idle system use the latest load average immediately.
6590 */
6591 int old_load_shifted = (int)((old_load_average + SCHED_PSET_LOAD_EWMA_ROUND_BIT) >> SCHED_PSET_LOAD_EWMA_FRACTION_BITS);
6592 boolean_t load_uptick = (old_load_shifted == 0) && (current_runq_depth != 0);
6593 boolean_t load_downtick = (old_load_shifted != 0) && (current_runq_depth == 0);
6594 uint64_t load_average;
6595 if (load_uptick || load_downtick) {
6596 load_average = (current_runq_depth << SCHED_PSET_LOAD_EWMA_FRACTION_BITS);
6597 } else {
6598 /* Indicates a loaded system; use EWMA for load average calculation */
6599 load_average = (old_load_average_factor + new_load_average_factor) / (delta_nsecs + SCHED_PSET_LOAD_EWMA_TC_NSECS);
6600 }
6601 os_atomic_store(&pset->pset_load_average[sched_bucket], load_average, relaxed);
6602 KDBG(MACHDBG_CODE(DBG_MACH_SCHED_CLUTCH, MACH_SCHED_EDGE_LOAD_AVG) | DBG_FUNC_NONE, pset->pset_cluster_id, (load_average >> SCHED_PSET_LOAD_EWMA_FRACTION_BITS), load_average & SCHED_PSET_LOAD_EWMA_FRACTION_MASK, sched_bucket);
6603 }
6604 os_atomic_store(&pset->pset_load_last_update, curtime, relaxed);
6605 }
6606
6607 void
6608 sched_update_pset_avg_execution_time(processor_set_t pset, uint64_t execution_time, uint64_t curtime, sched_bucket_t sched_bucket)
6609 {
6610 pset_execution_time_t old_execution_time_packed, new_execution_time_packed;
6611 uint64_t avg_thread_execution_time = 0;
6612
6613 os_atomic_rmw_loop(&pset->pset_execution_time[sched_bucket].pset_execution_time_packed,
6614 old_execution_time_packed.pset_execution_time_packed,
6615 new_execution_time_packed.pset_execution_time_packed, relaxed, {
6616 uint64_t last_update = old_execution_time_packed.pset_execution_time_last_update;
6617 int64_t delta_ticks = curtime - last_update;
6618 if (delta_ticks < 0) {
6619 /*
6620 * Its possible that another CPU came in and updated the pset_execution_time
6621 * before this CPU could do it. Since the average execution time is meant to
6622 * be an approximate measure per cluster, ignore the older update.
6623 */
6624 os_atomic_rmw_loop_give_up(return );
6625 }
6626 uint64_t delta_nsecs = 0;
6627 absolutetime_to_nanoseconds(delta_ticks, &delta_nsecs);
6628
6629 uint64_t nanotime = 0;
6630 absolutetime_to_nanoseconds(execution_time, &nanotime);
6631 uint64_t execution_time_us = nanotime / NSEC_PER_USEC;
6632
6633 uint64_t old_execution_time = (old_execution_time_packed.pset_avg_thread_execution_time * SCHED_PSET_LOAD_EWMA_TC_NSECS);
6634 uint64_t new_execution_time = (execution_time_us * delta_nsecs);
6635
6636 avg_thread_execution_time = (old_execution_time + new_execution_time) / (delta_nsecs + SCHED_PSET_LOAD_EWMA_TC_NSECS);
6637 new_execution_time_packed.pset_avg_thread_execution_time = avg_thread_execution_time;
6638 new_execution_time_packed.pset_execution_time_last_update = curtime;
6639 });
6640 KDBG(MACHDBG_CODE(DBG_MACH_SCHED, MACH_PSET_AVG_EXEC_TIME) | DBG_FUNC_NONE, pset->pset_cluster_id, avg_thread_execution_time, sched_bucket);
6641 }
6642
6643 #else /* CONFIG_SCHED_EDGE */
6644
6645 void
6646 sched_update_pset_load_average(processor_set_t pset, __unused uint64_t curtime)
6647 {
6648 int non_rt_load = pset->pset_runq.count;
6649 int load = ((bit_count(pset->cpu_state_map[PROCESSOR_RUNNING]) + non_rt_load + rt_runq_count(pset)) << PSET_LOAD_NUMERATOR_SHIFT);
6650 int new_load_average = ((int)pset->load_average + load) >> 1;
6651
6652 pset->load_average = new_load_average;
6653 #if (DEVELOPMENT || DEBUG)
6654 #if __AMP__
6655 if (pset->pset_cluster_type == PSET_AMP_P) {
6656 KDBG(MACHDBG_CODE(DBG_MACH_SCHED, MACH_PSET_LOAD_AVERAGE) | DBG_FUNC_NONE, sched_get_pset_load_average(pset, 0), (bit_count(pset->cpu_state_map[PROCESSOR_RUNNING]) + pset->pset_runq.count + rt_runq_count(pset)));
6657 }
6658 #endif
6659 #endif
6660 }
6661
6662 void
6663 sched_update_pset_avg_execution_time(__unused processor_set_t pset, __unused uint64_t execution_time, __unused uint64_t curtime, __unused sched_bucket_t sched_bucket)
6664 {
6665 }
6666 #endif /* CONFIG_SCHED_EDGE */
6667
6668 /* pset is locked */
6669 static bool
6670 processor_is_fast_track_candidate_for_realtime_thread(processor_set_t pset, processor_t processor)
6671 {
6672 int cpuid = processor->cpu_id;
6673 #if defined(__x86_64__)
6674 if (sched_avoid_cpu0 && (cpuid == 0)) {
6675 return false;
6676 }
6677 #endif
6678
6679 cpumap_t fasttrack_map = pset_available_cpumap(pset) & ~pset->pending_AST_URGENT_cpu_mask & ~pset->realtime_map;
6680
6681 return bit_test(fasttrack_map, cpuid);
6682 }
6683
6684 /* pset is locked */
6685 static processor_t
6686 choose_processor_for_realtime_thread(processor_set_t pset, processor_t skip_processor, bool consider_secondaries)
6687 {
6688 #if defined(__x86_64__)
6689 bool avoid_cpu0 = sched_avoid_cpu0 && bit_test(pset->cpu_bitmask, 0);
6690 #else
6691 const bool avoid_cpu0 = false;
6692 #endif
6693
6694 cpumap_t cpu_map = pset_available_cpumap(pset) & ~pset->pending_AST_URGENT_cpu_mask & ~pset->realtime_map;
6695 if (skip_processor) {
6696 bit_clear(cpu_map, skip_processor->cpu_id);
6697 }
6698
6699 cpumap_t primary_map = cpu_map & pset->primary_map;
6700 if (avoid_cpu0) {
6701 primary_map = bit_ror64(primary_map, 1);
6702 }
6703
6704 int rotid = lsb_first(primary_map);
6705 if (rotid >= 0) {
6706 int cpuid = avoid_cpu0 ? ((rotid + 1) & 63) : rotid;
6707
6708 processor_t processor = processor_array[cpuid];
6709
6710 return processor;
6711 }
6712
6713 if (!pset->is_SMT || !sched_allow_rt_smt || !consider_secondaries) {
6714 goto out;
6715 }
6716
6717 /* Consider secondary processors */
6718 cpumap_t secondary_map = cpu_map & ~pset->primary_map;
6719 if (avoid_cpu0) {
6720 /* Also avoid cpu1 */
6721 secondary_map = bit_ror64(secondary_map, 2);
6722 }
6723 rotid = lsb_first(secondary_map);
6724 if (rotid >= 0) {
6725 int cpuid = avoid_cpu0 ? ((rotid + 2) & 63) : rotid;
6726
6727 processor_t processor = processor_array[cpuid];
6728
6729 return processor;
6730 }
6731
6732 out:
6733 if (skip_processor) {
6734 return PROCESSOR_NULL;
6735 }
6736
6737 /*
6738 * If we didn't find an obvious processor to choose, but there are still more CPUs
6739 * not already running realtime threads than realtime threads in the realtime run queue,
6740 * this thread belongs in this pset, so choose some other processor in this pset
6741 * to ensure the thread is enqueued here.
6742 */
6743 cpumap_t non_realtime_map = pset_available_cpumap(pset) & pset->primary_map & ~pset->realtime_map;
6744 if (bit_count(non_realtime_map) > rt_runq_count(pset)) {
6745 cpu_map = non_realtime_map;
6746 assert(cpu_map != 0);
6747 int cpuid = bit_first(cpu_map);
6748 assert(cpuid >= 0);
6749 return processor_array[cpuid];
6750 }
6751
6752 if (!pset->is_SMT || !sched_allow_rt_smt || !consider_secondaries) {
6753 goto skip_secondaries;
6754 }
6755
6756 non_realtime_map = pset_available_cpumap(pset) & ~pset->realtime_map;
6757 if (bit_count(non_realtime_map) > rt_runq_count(pset)) {
6758 cpu_map = non_realtime_map;
6759 assert(cpu_map != 0);
6760 int cpuid = bit_first(cpu_map);
6761 assert(cpuid >= 0);
6762 return processor_array[cpuid];
6763 }
6764
6765 skip_secondaries:
6766 return PROCESSOR_NULL;
6767 }
6768
6769 /* pset is locked */
6770 static bool
6771 all_available_primaries_are_running_realtime_threads(processor_set_t pset)
6772 {
6773 cpumap_t cpu_map = pset_available_cpumap(pset) & pset->primary_map & ~pset->realtime_map;
6774 return rt_runq_count(pset) > bit_count(cpu_map);
6775 }
6776
6777 #if defined(__x86_64__)
6778 /* pset is locked */
6779 static bool
6780 these_processors_are_running_realtime_threads(processor_set_t pset, uint64_t these_map)
6781 {
6782 cpumap_t cpu_map = pset_available_cpumap(pset) & these_map & ~pset->realtime_map;
6783 return rt_runq_count(pset) > bit_count(cpu_map);
6784 }
6785 #endif
6786
6787 static bool
6788 sched_ok_to_run_realtime_thread(processor_set_t pset, processor_t processor)
6789 {
6790 bool ok_to_run_realtime_thread = true;
6791 #if defined(__x86_64__)
6792 if (sched_avoid_cpu0 && processor->cpu_id == 0) {
6793 ok_to_run_realtime_thread = these_processors_are_running_realtime_threads(pset, pset->primary_map & ~0x1);
6794 } else if (sched_avoid_cpu0 && (processor->cpu_id == 1) && processor->is_SMT) {
6795 ok_to_run_realtime_thread = sched_allow_rt_smt && these_processors_are_running_realtime_threads(pset, ~0x2);
6796 } else if (processor->processor_primary != processor) {
6797 ok_to_run_realtime_thread = (sched_allow_rt_smt && all_available_primaries_are_running_realtime_threads(pset));
6798 }
6799 #else
6800 (void)pset;
6801 (void)processor;
6802 #endif
6803 return ok_to_run_realtime_thread;
6804 }
6805
6806 void
6807 sched_pset_made_schedulable(__unused processor_t processor, processor_set_t pset, boolean_t drop_lock)
6808 {
6809 if (drop_lock) {
6810 pset_unlock(pset);
6811 }
6812 }
6813
6814 void
6815 thread_set_no_smt(bool set)
6816 {
6817 if (!system_is_SMT) {
6818 /* Not a machine that supports SMT */
6819 return;
6820 }
6821
6822 thread_t thread = current_thread();
6823
6824 spl_t s = splsched();
6825 thread_lock(thread);
6826 if (set) {
6827 thread->sched_flags |= TH_SFLAG_NO_SMT;
6828 }
6829 thread_unlock(thread);
6830 splx(s);
6831 }
6832
6833 bool
6834 thread_get_no_smt(void)
6835 {
6836 return current_thread()->sched_flags & TH_SFLAG_NO_SMT;
6837 }
6838
6839 extern void task_set_no_smt(task_t);
6840 void
6841 task_set_no_smt(task_t task)
6842 {
6843 if (!system_is_SMT) {
6844 /* Not a machine that supports SMT */
6845 return;
6846 }
6847
6848 if (task == TASK_NULL) {
6849 task = current_task();
6850 }
6851
6852 task_lock(task);
6853 task->t_flags |= TF_NO_SMT;
6854 task_unlock(task);
6855 }
6856
6857 #if DEBUG || DEVELOPMENT
6858 extern void sysctl_task_set_no_smt(char no_smt);
6859 void
6860 sysctl_task_set_no_smt(char no_smt)
6861 {
6862 if (!system_is_SMT) {
6863 /* Not a machine that supports SMT */
6864 return;
6865 }
6866
6867 task_t task = current_task();
6868
6869 task_lock(task);
6870 if (no_smt == '1') {
6871 task->t_flags |= TF_NO_SMT;
6872 }
6873 task_unlock(task);
6874 }
6875
6876 extern char sysctl_task_get_no_smt(void);
6877 char
6878 sysctl_task_get_no_smt(void)
6879 {
6880 task_t task = current_task();
6881
6882 if (task->t_flags & TF_NO_SMT) {
6883 return '1';
6884 }
6885 return '0';
6886 }
6887 #endif /* DEVELOPMENT || DEBUG */
6888
6889
6890 __private_extern__ void
6891 thread_bind_cluster_type(thread_t thread, char cluster_type, bool soft_bound)
6892 {
6893 #if __AMP__
6894 spl_t s = splsched();
6895 thread_lock(thread);
6896 thread->sched_flags &= ~(TH_SFLAG_ECORE_ONLY | TH_SFLAG_PCORE_ONLY | TH_SFLAG_BOUND_SOFT);
6897 if (soft_bound) {
6898 thread->sched_flags |= TH_SFLAG_BOUND_SOFT;
6899 }
6900 switch (cluster_type) {
6901 case 'e':
6902 case 'E':
6903 thread->sched_flags |= TH_SFLAG_ECORE_ONLY;
6904 break;
6905 case 'p':
6906 case 'P':
6907 thread->sched_flags |= TH_SFLAG_PCORE_ONLY;
6908 break;
6909 default:
6910 break;
6911 }
6912 thread_unlock(thread);
6913 splx(s);
6914
6915 if (thread == current_thread()) {
6916 thread_block(THREAD_CONTINUE_NULL);
6917 }
6918 #else /* __AMP__ */
6919 (void)thread;
6920 (void)cluster_type;
6921 (void)soft_bound;
6922 #endif /* __AMP__ */
6923 }
6924
6925 #if DEVELOPMENT || DEBUG
6926 extern int32_t sysctl_get_bound_cpuid(void);
6927 int32_t
6928 sysctl_get_bound_cpuid(void)
6929 {
6930 int32_t cpuid = -1;
6931 thread_t self = current_thread();
6932
6933 processor_t processor = self->bound_processor;
6934 if (processor == NULL) {
6935 cpuid = -1;
6936 } else {
6937 cpuid = processor->cpu_id;
6938 }
6939
6940 return cpuid;
6941 }
6942
6943 extern kern_return_t sysctl_thread_bind_cpuid(int32_t cpuid);
6944 kern_return_t
6945 sysctl_thread_bind_cpuid(int32_t cpuid)
6946 {
6947 processor_t processor = PROCESSOR_NULL;
6948
6949 if (cpuid == -1) {
6950 goto unbind;
6951 }
6952
6953 if (cpuid < 0 || cpuid >= MAX_SCHED_CPUS) {
6954 return KERN_INVALID_VALUE;
6955 }
6956
6957 processor = processor_array[cpuid];
6958 if (processor == PROCESSOR_NULL) {
6959 return KERN_INVALID_VALUE;
6960 }
6961
6962 #if __AMP__
6963
6964 thread_t thread = current_thread();
6965
6966 if (thread->sched_flags & (TH_SFLAG_ECORE_ONLY | TH_SFLAG_PCORE_ONLY)) {
6967 if ((thread->sched_flags & TH_SFLAG_BOUND_SOFT) == 0) {
6968 /* Cannot hard-bind an already hard-cluster-bound thread */
6969 return KERN_NOT_SUPPORTED;
6970 }
6971 }
6972
6973 #endif /* __AMP__ */
6974
6975 unbind:
6976 thread_bind(processor);
6977
6978 thread_block(THREAD_CONTINUE_NULL);
6979 return KERN_SUCCESS;
6980 }
6981 #endif /* DEVELOPMENT || DEBUG */