]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/task_policy.c
xnu-3789.41.3.tar.gz
[apple/xnu.git] / osfmk / kern / task_policy.c
1 /*
2 * Copyright (c) 2000-2016 Apple Computer, 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 #include <kern/policy_internal.h>
30 #include <mach/task_policy.h>
31
32 #include <mach/mach_types.h>
33 #include <mach/task_server.h>
34
35 #include <kern/host.h> /* host_priv_self() */
36 #include <mach/host_priv.h> /* host_get_special_port() */
37 #include <mach/host_special_ports.h> /* RESOURCE_NOTIFY_PORT */
38 #include <kern/sched.h>
39 #include <kern/task.h>
40 #include <mach/thread_policy.h>
41 #include <sys/errno.h>
42 #include <sys/resource.h>
43 #include <machine/limits.h>
44 #include <kern/ledger.h>
45 #include <kern/thread_call.h>
46 #include <kern/sfi.h>
47 #include <kern/coalition.h>
48 #if CONFIG_TELEMETRY
49 #include <kern/telemetry.h>
50 #endif
51
52 #if IMPORTANCE_INHERITANCE
53 #include <ipc/ipc_importance.h>
54 #if IMPORTANCE_DEBUG
55 #include <mach/machine/sdt.h>
56 #endif /* IMPORTANCE_DEBUG */
57 #endif /* IMPORTANCE_INHERITACE */
58
59 #include <sys/kdebug.h>
60
61 /*
62 * Task Policy
63 *
64 * This subsystem manages task and thread IO priority and backgrounding,
65 * as well as importance inheritance, process suppression, task QoS, and apptype.
66 * These properties have a suprising number of complex interactions, so they are
67 * centralized here in one state machine to simplify the implementation of those interactions.
68 *
69 * Architecture:
70 * Threads and tasks have two policy fields: requested, effective.
71 * Requested represents the wishes of each interface that influences task policy.
72 * Effective represents the distillation of that policy into a set of behaviors.
73 *
74 * Each thread making a modification in the policy system passes a 'pending' struct,
75 * which tracks updates that will be applied after dropping the policy engine lock.
76 *
77 * Each interface that has an input into the task policy state machine controls a field in requested.
78 * If the interface has a getter, it returns what is in the field in requested, but that is
79 * not necessarily what is actually in effect.
80 *
81 * All kernel subsystems that behave differently based on task policy call into
82 * the proc_get_effective_(task|thread)_policy functions, which return the decision of the task policy state machine
83 * for that subsystem by querying only the 'effective' field.
84 *
85 * Policy change operations:
86 * Here are the steps to change a policy on a task or thread:
87 * 1) Lock task
88 * 2) Change requested field for the relevant policy
89 * 3) Run a task policy update, which recalculates effective based on requested,
90 * then takes a diff between the old and new versions of requested and calls the relevant
91 * other subsystems to apply these changes, and updates the pending field.
92 * 4) Unlock task
93 * 5) Run task policy update complete, which looks at the pending field to update
94 * subsystems which cannot be touched while holding the task lock.
95 *
96 * To add a new requested policy, add the field in the requested struct, the flavor in task.h,
97 * the setter and getter in proc_(set|get)_task_policy*,
98 * then set up the effects of that behavior in task_policy_update*. If the policy manifests
99 * itself as a distinct effective policy, add it to the effective struct and add it to the
100 * proc_get_effective_task_policy accessor.
101 *
102 * Most policies are set via proc_set_task_policy, but policies that don't fit that interface
103 * roll their own lock/set/update/unlock/complete code inside this file.
104 *
105 *
106 * Suppression policy
107 *
108 * These are a set of behaviors that can be requested for a task. They currently have specific
109 * implied actions when they're enabled, but they may be made customizable in the future.
110 *
111 * When the affected task is boosted, we temporarily disable the suppression behaviors
112 * so that the affected process has a chance to run so it can call the API to permanently
113 * disable the suppression behaviors.
114 *
115 * Locking
116 *
117 * Changing task policy on a task takes the task lock.
118 * Changing task policy on a thread takes the thread mutex.
119 * Task policy changes that affect threads will take each thread's mutex to update it if necessary.
120 *
121 * Querying the effective policy does not take a lock, because callers
122 * may run in interrupt context or other place where locks are not OK.
123 *
124 * This means that any notification of state change needs to be externally synchronized.
125 * We do this by idempotent callouts after the state has changed to ask
126 * other subsystems to update their view of the world.
127 *
128 * TODO: Move all cpu/wakes/io monitor code into a separate file
129 * TODO: Move all importance code over to importance subsystem
130 * TODO: Move all taskwatch code into a separate file
131 * TODO: Move all VM importance code into a separate file
132 */
133
134 /* Task policy related helper functions */
135 static void proc_set_task_policy_locked(task_t task, int category, int flavor, int value, int value2);
136
137 static void task_policy_update_locked(task_t task, task_pend_token_t pend_token);
138 static void task_policy_update_internal_locked(task_t task, boolean_t in_create, task_pend_token_t pend_token);
139
140 /* For attributes that have two scalars as input/output */
141 static void proc_set_task_policy2(task_t task, int category, int flavor, int value1, int value2);
142 static void proc_get_task_policy2(task_t task, int category, int flavor, int *value1, int *value2);
143
144 #if CONFIG_SCHED_SFI
145 static boolean_t task_policy_update_coalition_focal_tasks(task_t task, int prev_role, int next_role);
146 #endif
147
148 static uint64_t task_requested_bitfield(task_t task);
149 static uint64_t task_effective_bitfield(task_t task);
150
151 /* Convenience functions for munging a policy bitfield into a tracepoint */
152 static uintptr_t trequested_0(task_t task);
153 static uintptr_t trequested_1(task_t task);
154 static uintptr_t teffective_0(task_t task);
155 static uintptr_t teffective_1(task_t task);
156
157 /* CPU limits helper functions */
158 static int task_set_cpuusage(task_t task, uint8_t percentage, uint64_t interval, uint64_t deadline, int scope, int entitled);
159 static int task_get_cpuusage(task_t task, uint8_t *percentagep, uint64_t *intervalp, uint64_t *deadlinep, int *scope);
160 static int task_enable_cpumon_locked(task_t task);
161 static int task_disable_cpumon(task_t task);
162 static int task_clear_cpuusage_locked(task_t task, int cpumon_entitled);
163 static int task_apply_resource_actions(task_t task, int type);
164 static void task_action_cpuusage(thread_call_param_t param0, thread_call_param_t param1);
165
166 #ifdef MACH_BSD
167 typedef struct proc * proc_t;
168 int proc_pid(void *proc);
169 extern int proc_selfpid(void);
170 extern char * proc_name_address(void *p);
171 extern char * proc_best_name(proc_t proc);
172
173 extern int proc_pidpathinfo_internal(proc_t p, uint64_t arg,
174 char *buffer, uint32_t buffersize,
175 int32_t *retval);
176 #endif /* MACH_BSD */
177
178
179
180 /* Importance Inheritance related helper functions */
181
182 #if IMPORTANCE_INHERITANCE
183
184 static void task_importance_mark_live_donor(task_t task, boolean_t donating);
185 static void task_importance_mark_receiver(task_t task, boolean_t receiving);
186 static void task_importance_mark_denap_receiver(task_t task, boolean_t denap);
187
188 static boolean_t task_is_marked_live_importance_donor(task_t task);
189 static boolean_t task_is_importance_receiver(task_t task);
190 static boolean_t task_is_importance_denap_receiver(task_t task);
191
192 static int task_importance_hold_internal_assertion(task_t target_task, uint32_t count);
193
194 static void task_add_importance_watchport(task_t task, mach_port_t port, int *boostp);
195 static void task_importance_update_live_donor(task_t target_task);
196
197 static void task_set_boost_locked(task_t task, boolean_t boost_active);
198
199 #endif /* IMPORTANCE_INHERITANCE */
200
201 #if IMPORTANCE_DEBUG
202 #define __impdebug_only
203 #else
204 #define __impdebug_only __unused
205 #endif
206
207 #if IMPORTANCE_INHERITANCE
208 #define __imp_only
209 #else
210 #define __imp_only __unused
211 #endif
212
213 /*
214 * Default parameters for certain policies
215 */
216
217 int proc_standard_daemon_tier = THROTTLE_LEVEL_TIER1;
218 int proc_suppressed_disk_tier = THROTTLE_LEVEL_TIER1;
219 int proc_tal_disk_tier = THROTTLE_LEVEL_TIER1;
220
221 int proc_graphics_timer_qos = (LATENCY_QOS_TIER_0 & 0xFF);
222
223 const int proc_default_bg_iotier = THROTTLE_LEVEL_TIER2;
224
225 /* Latency/throughput QoS fields remain zeroed, i.e. TIER_UNSPECIFIED at creation */
226 const struct task_requested_policy default_task_requested_policy = {
227 .trp_bg_iotier = proc_default_bg_iotier
228 };
229 const struct task_effective_policy default_task_effective_policy = {};
230
231 /*
232 * Default parameters for CPU usage monitor.
233 *
234 * Default setting is 50% over 3 minutes.
235 */
236 #define DEFAULT_CPUMON_PERCENTAGE 50
237 #define DEFAULT_CPUMON_INTERVAL (3 * 60)
238
239 uint8_t proc_max_cpumon_percentage;
240 uint64_t proc_max_cpumon_interval;
241
242
243 kern_return_t
244 qos_latency_policy_validate(task_latency_qos_t ltier) {
245 if ((ltier != LATENCY_QOS_TIER_UNSPECIFIED) &&
246 ((ltier > LATENCY_QOS_TIER_5) || (ltier < LATENCY_QOS_TIER_0)))
247 return KERN_INVALID_ARGUMENT;
248
249 return KERN_SUCCESS;
250 }
251
252 kern_return_t
253 qos_throughput_policy_validate(task_throughput_qos_t ttier) {
254 if ((ttier != THROUGHPUT_QOS_TIER_UNSPECIFIED) &&
255 ((ttier > THROUGHPUT_QOS_TIER_5) || (ttier < THROUGHPUT_QOS_TIER_0)))
256 return KERN_INVALID_ARGUMENT;
257
258 return KERN_SUCCESS;
259 }
260
261 static kern_return_t
262 task_qos_policy_validate(task_qos_policy_t qosinfo, mach_msg_type_number_t count) {
263 if (count < TASK_QOS_POLICY_COUNT)
264 return KERN_INVALID_ARGUMENT;
265
266 task_latency_qos_t ltier = qosinfo->task_latency_qos_tier;
267 task_throughput_qos_t ttier = qosinfo->task_throughput_qos_tier;
268
269 kern_return_t kr = qos_latency_policy_validate(ltier);
270
271 if (kr != KERN_SUCCESS)
272 return kr;
273
274 kr = qos_throughput_policy_validate(ttier);
275
276 return kr;
277 }
278
279 uint32_t
280 qos_extract(uint32_t qv) {
281 return (qv & 0xFF);
282 }
283
284 uint32_t
285 qos_latency_policy_package(uint32_t qv) {
286 return (qv == LATENCY_QOS_TIER_UNSPECIFIED) ? LATENCY_QOS_TIER_UNSPECIFIED : ((0xFF << 16) | qv);
287 }
288
289 uint32_t
290 qos_throughput_policy_package(uint32_t qv) {
291 return (qv == THROUGHPUT_QOS_TIER_UNSPECIFIED) ? THROUGHPUT_QOS_TIER_UNSPECIFIED : ((0xFE << 16) | qv);
292 }
293
294 /* TEMPORARY boot-arg controlling task_policy suppression (App Nap) */
295 static boolean_t task_policy_suppression_disable = FALSE;
296
297 kern_return_t
298 task_policy_set(
299 task_t task,
300 task_policy_flavor_t flavor,
301 task_policy_t policy_info,
302 mach_msg_type_number_t count)
303 {
304 kern_return_t result = KERN_SUCCESS;
305
306 if (task == TASK_NULL || task == kernel_task)
307 return (KERN_INVALID_ARGUMENT);
308
309 switch (flavor) {
310
311 case TASK_CATEGORY_POLICY: {
312 task_category_policy_t info = (task_category_policy_t)policy_info;
313
314 if (count < TASK_CATEGORY_POLICY_COUNT)
315 return (KERN_INVALID_ARGUMENT);
316
317
318 switch(info->role) {
319 case TASK_FOREGROUND_APPLICATION:
320 case TASK_BACKGROUND_APPLICATION:
321 case TASK_DEFAULT_APPLICATION:
322 proc_set_task_policy(task,
323 TASK_POLICY_ATTRIBUTE, TASK_POLICY_ROLE,
324 info->role);
325 break;
326
327 case TASK_CONTROL_APPLICATION:
328 if (task != current_task() || task->sec_token.val[0] != 0)
329 result = KERN_INVALID_ARGUMENT;
330 else
331 proc_set_task_policy(task,
332 TASK_POLICY_ATTRIBUTE, TASK_POLICY_ROLE,
333 info->role);
334 break;
335
336 case TASK_GRAPHICS_SERVER:
337 /* TODO: Restrict this role to FCFS <rdar://problem/12552788> */
338 if (task != current_task() || task->sec_token.val[0] != 0)
339 result = KERN_INVALID_ARGUMENT;
340 else
341 proc_set_task_policy(task,
342 TASK_POLICY_ATTRIBUTE, TASK_POLICY_ROLE,
343 info->role);
344 break;
345 default:
346 result = KERN_INVALID_ARGUMENT;
347 break;
348 } /* switch (info->role) */
349
350 break;
351 }
352
353 /* Desired energy-efficiency/performance "quality-of-service" */
354 case TASK_BASE_QOS_POLICY:
355 case TASK_OVERRIDE_QOS_POLICY:
356 {
357 task_qos_policy_t qosinfo = (task_qos_policy_t)policy_info;
358 kern_return_t kr = task_qos_policy_validate(qosinfo, count);
359
360 if (kr != KERN_SUCCESS)
361 return kr;
362
363
364 uint32_t lqos = qos_extract(qosinfo->task_latency_qos_tier);
365 uint32_t tqos = qos_extract(qosinfo->task_throughput_qos_tier);
366
367 proc_set_task_policy2(task, TASK_POLICY_ATTRIBUTE,
368 flavor == TASK_BASE_QOS_POLICY ? TASK_POLICY_BASE_LATENCY_AND_THROUGHPUT_QOS : TASK_POLICY_OVERRIDE_LATENCY_AND_THROUGHPUT_QOS,
369 lqos, tqos);
370 }
371 break;
372
373 case TASK_BASE_LATENCY_QOS_POLICY:
374 {
375 task_qos_policy_t qosinfo = (task_qos_policy_t)policy_info;
376 kern_return_t kr = task_qos_policy_validate(qosinfo, count);
377
378 if (kr != KERN_SUCCESS)
379 return kr;
380
381 uint32_t lqos = qos_extract(qosinfo->task_latency_qos_tier);
382
383 proc_set_task_policy(task, TASK_POLICY_ATTRIBUTE, TASK_BASE_LATENCY_QOS_POLICY, lqos);
384 }
385 break;
386
387 case TASK_BASE_THROUGHPUT_QOS_POLICY:
388 {
389 task_qos_policy_t qosinfo = (task_qos_policy_t)policy_info;
390 kern_return_t kr = task_qos_policy_validate(qosinfo, count);
391
392 if (kr != KERN_SUCCESS)
393 return kr;
394
395 uint32_t tqos = qos_extract(qosinfo->task_throughput_qos_tier);
396
397 proc_set_task_policy(task, TASK_POLICY_ATTRIBUTE, TASK_BASE_THROUGHPUT_QOS_POLICY, tqos);
398 }
399 break;
400
401 case TASK_SUPPRESSION_POLICY:
402 {
403
404 task_suppression_policy_t info = (task_suppression_policy_t)policy_info;
405
406 if (count < TASK_SUPPRESSION_POLICY_COUNT)
407 return (KERN_INVALID_ARGUMENT);
408
409 struct task_qos_policy qosinfo;
410
411 qosinfo.task_latency_qos_tier = info->timer_throttle;
412 qosinfo.task_throughput_qos_tier = info->throughput_qos;
413
414 kern_return_t kr = task_qos_policy_validate(&qosinfo, TASK_QOS_POLICY_COUNT);
415
416 if (kr != KERN_SUCCESS)
417 return kr;
418
419 /* TEMPORARY disablement of task suppression */
420 if (task_policy_suppression_disable && info->active)
421 return KERN_SUCCESS;
422
423 struct task_pend_token pend_token = {};
424
425 task_lock(task);
426
427 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
428 (IMPORTANCE_CODE(IMP_TASK_SUPPRESSION, info->active)) | DBG_FUNC_START,
429 proc_selfpid(), task_pid(task), trequested_0(task),
430 trequested_1(task), 0);
431
432 task->requested_policy.trp_sup_active = (info->active) ? 1 : 0;
433 task->requested_policy.trp_sup_lowpri_cpu = (info->lowpri_cpu) ? 1 : 0;
434 task->requested_policy.trp_sup_timer = qos_extract(info->timer_throttle);
435 task->requested_policy.trp_sup_disk = (info->disk_throttle) ? 1 : 0;
436 task->requested_policy.trp_sup_throughput = qos_extract(info->throughput_qos);
437 task->requested_policy.trp_sup_cpu = (info->suppressed_cpu) ? 1 : 0;
438 task->requested_policy.trp_sup_bg_sockets = (info->background_sockets) ? 1 : 0;
439
440 task_policy_update_locked(task, &pend_token);
441
442 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
443 (IMPORTANCE_CODE(IMP_TASK_SUPPRESSION, info->active)) | DBG_FUNC_END,
444 proc_selfpid(), task_pid(task), trequested_0(task),
445 trequested_1(task), 0);
446
447 task_unlock(task);
448
449 task_policy_update_complete_unlocked(task, &pend_token);
450
451 break;
452
453 }
454
455 default:
456 result = KERN_INVALID_ARGUMENT;
457 break;
458 }
459
460 return (result);
461 }
462
463 /* Sets BSD 'nice' value on the task */
464 kern_return_t
465 task_importance(
466 task_t task,
467 integer_t importance)
468 {
469 if (task == TASK_NULL || task == kernel_task)
470 return (KERN_INVALID_ARGUMENT);
471
472 task_lock(task);
473
474 if (!task->active) {
475 task_unlock(task);
476
477 return (KERN_TERMINATED);
478 }
479
480 if (proc_get_effective_task_policy(task, TASK_POLICY_ROLE) >= TASK_CONTROL_APPLICATION) {
481 task_unlock(task);
482
483 return (KERN_INVALID_ARGUMENT);
484 }
485
486 task->importance = importance;
487
488 struct task_pend_token pend_token = {};
489
490 task_policy_update_locked(task, &pend_token);
491
492 task_unlock(task);
493
494 task_policy_update_complete_unlocked(task, &pend_token);
495
496 return (KERN_SUCCESS);
497 }
498
499 kern_return_t
500 task_policy_get(
501 task_t task,
502 task_policy_flavor_t flavor,
503 task_policy_t policy_info,
504 mach_msg_type_number_t *count,
505 boolean_t *get_default)
506 {
507 if (task == TASK_NULL || task == kernel_task)
508 return (KERN_INVALID_ARGUMENT);
509
510 switch (flavor) {
511
512 case TASK_CATEGORY_POLICY:
513 {
514 task_category_policy_t info = (task_category_policy_t)policy_info;
515
516 if (*count < TASK_CATEGORY_POLICY_COUNT)
517 return (KERN_INVALID_ARGUMENT);
518
519 if (*get_default)
520 info->role = TASK_UNSPECIFIED;
521 else
522 info->role = proc_get_task_policy(task, TASK_POLICY_ATTRIBUTE, TASK_POLICY_ROLE);
523 break;
524 }
525
526 case TASK_BASE_QOS_POLICY: /* FALLTHRU */
527 case TASK_OVERRIDE_QOS_POLICY:
528 {
529 task_qos_policy_t info = (task_qos_policy_t)policy_info;
530
531 if (*count < TASK_QOS_POLICY_COUNT)
532 return (KERN_INVALID_ARGUMENT);
533
534 if (*get_default) {
535 info->task_latency_qos_tier = LATENCY_QOS_TIER_UNSPECIFIED;
536 info->task_throughput_qos_tier = THROUGHPUT_QOS_TIER_UNSPECIFIED;
537 } else if (flavor == TASK_BASE_QOS_POLICY) {
538 int value1, value2;
539
540 proc_get_task_policy2(task, TASK_POLICY_ATTRIBUTE, TASK_POLICY_BASE_LATENCY_AND_THROUGHPUT_QOS, &value1, &value2);
541
542 info->task_latency_qos_tier = qos_latency_policy_package(value1);
543 info->task_throughput_qos_tier = qos_throughput_policy_package(value2);
544
545 } else if (flavor == TASK_OVERRIDE_QOS_POLICY) {
546 int value1, value2;
547
548 proc_get_task_policy2(task, TASK_POLICY_ATTRIBUTE, TASK_POLICY_OVERRIDE_LATENCY_AND_THROUGHPUT_QOS, &value1, &value2);
549
550 info->task_latency_qos_tier = qos_latency_policy_package(value1);
551 info->task_throughput_qos_tier = qos_throughput_policy_package(value2);
552 }
553
554 break;
555 }
556
557 case TASK_POLICY_STATE:
558 {
559 task_policy_state_t info = (task_policy_state_t)policy_info;
560
561 if (*count < TASK_POLICY_STATE_COUNT)
562 return (KERN_INVALID_ARGUMENT);
563
564 /* Only root can get this info */
565 if (current_task()->sec_token.val[0] != 0)
566 return KERN_PROTECTION_FAILURE;
567
568 if (*get_default) {
569 info->requested = 0;
570 info->effective = 0;
571 info->pending = 0;
572 info->imp_assertcnt = 0;
573 info->imp_externcnt = 0;
574 info->flags = 0;
575 info->imp_transitions = 0;
576 } else {
577 task_lock(task);
578
579 info->requested = task_requested_bitfield(task);
580 info->effective = task_effective_bitfield(task);
581 info->pending = 0;
582
583 info->tps_requested_policy = *(uint64_t*)(&task->requested_policy);
584 info->tps_effective_policy = *(uint64_t*)(&task->effective_policy);
585
586 info->flags = 0;
587 if (task->task_imp_base != NULL) {
588 info->imp_assertcnt = task->task_imp_base->iit_assertcnt;
589 info->imp_externcnt = IIT_EXTERN(task->task_imp_base);
590 info->flags |= (task_is_marked_importance_receiver(task) ? TASK_IMP_RECEIVER : 0);
591 info->flags |= (task_is_marked_importance_denap_receiver(task) ? TASK_DENAP_RECEIVER : 0);
592 info->flags |= (task_is_marked_importance_donor(task) ? TASK_IMP_DONOR : 0);
593 info->flags |= (task_is_marked_live_importance_donor(task) ? TASK_IMP_LIVE_DONOR : 0);
594 info->imp_transitions = task->task_imp_base->iit_transitions;
595 } else {
596 info->imp_assertcnt = 0;
597 info->imp_externcnt = 0;
598 info->imp_transitions = 0;
599 }
600 task_unlock(task);
601 }
602
603 break;
604 }
605
606 case TASK_SUPPRESSION_POLICY:
607 {
608 task_suppression_policy_t info = (task_suppression_policy_t)policy_info;
609
610 if (*count < TASK_SUPPRESSION_POLICY_COUNT)
611 return (KERN_INVALID_ARGUMENT);
612
613 task_lock(task);
614
615 if (*get_default) {
616 info->active = 0;
617 info->lowpri_cpu = 0;
618 info->timer_throttle = LATENCY_QOS_TIER_UNSPECIFIED;
619 info->disk_throttle = 0;
620 info->cpu_limit = 0;
621 info->suspend = 0;
622 info->throughput_qos = 0;
623 info->suppressed_cpu = 0;
624 } else {
625 info->active = task->requested_policy.trp_sup_active;
626 info->lowpri_cpu = task->requested_policy.trp_sup_lowpri_cpu;
627 info->timer_throttle = qos_latency_policy_package(task->requested_policy.trp_sup_timer);
628 info->disk_throttle = task->requested_policy.trp_sup_disk;
629 info->cpu_limit = 0;
630 info->suspend = 0;
631 info->throughput_qos = qos_throughput_policy_package(task->requested_policy.trp_sup_throughput);
632 info->suppressed_cpu = task->requested_policy.trp_sup_cpu;
633 info->background_sockets = task->requested_policy.trp_sup_bg_sockets;
634 }
635
636 task_unlock(task);
637 break;
638 }
639
640 default:
641 return (KERN_INVALID_ARGUMENT);
642 }
643
644 return (KERN_SUCCESS);
645 }
646
647 /*
648 * Called at task creation
649 * We calculate the correct effective but don't apply it to anything yet.
650 * The threads, etc will inherit from the task as they get created.
651 */
652 void
653 task_policy_create(task_t task, task_t parent_task)
654 {
655 task->requested_policy.trp_apptype = parent_task->requested_policy.trp_apptype;
656
657 task->requested_policy.trp_int_darwinbg = parent_task->requested_policy.trp_int_darwinbg;
658 task->requested_policy.trp_ext_darwinbg = parent_task->requested_policy.trp_ext_darwinbg;
659 task->requested_policy.trp_int_iotier = parent_task->requested_policy.trp_int_iotier;
660 task->requested_policy.trp_ext_iotier = parent_task->requested_policy.trp_ext_iotier;
661 task->requested_policy.trp_int_iopassive = parent_task->requested_policy.trp_int_iopassive;
662 task->requested_policy.trp_ext_iopassive = parent_task->requested_policy.trp_ext_iopassive;
663 task->requested_policy.trp_bg_iotier = parent_task->requested_policy.trp_bg_iotier;
664 task->requested_policy.trp_terminated = parent_task->requested_policy.trp_terminated;
665 task->requested_policy.trp_qos_clamp = parent_task->requested_policy.trp_qos_clamp;
666
667 if (task->requested_policy.trp_apptype == TASK_APPTYPE_DAEMON_ADAPTIVE && !task_is_exec_copy(task)) {
668 /* Do not update the apptype for exec copy task */
669 if (parent_task->requested_policy.trp_boosted) {
670 task->requested_policy.trp_apptype = TASK_APPTYPE_DAEMON_INTERACTIVE;
671 task_importance_mark_donor(task, TRUE);
672 } else {
673 task->requested_policy.trp_apptype = TASK_APPTYPE_DAEMON_BACKGROUND;
674 task_importance_mark_receiver(task, FALSE);
675 }
676 }
677
678 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
679 (IMPORTANCE_CODE(IMP_UPDATE, (IMP_UPDATE_TASK_CREATE | TASK_POLICY_TASK))) | DBG_FUNC_START,
680 task_pid(task), teffective_0(task),
681 teffective_1(task), task->priority, 0);
682
683 task_policy_update_internal_locked(task, TRUE, NULL);
684
685 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
686 (IMPORTANCE_CODE(IMP_UPDATE, (IMP_UPDATE_TASK_CREATE | TASK_POLICY_TASK))) | DBG_FUNC_END,
687 task_pid(task), teffective_0(task),
688 teffective_1(task), task->priority, 0);
689
690 task_importance_update_live_donor(task);
691 }
692
693
694 static void
695 task_policy_update_locked(task_t task, task_pend_token_t pend_token)
696 {
697 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
698 (IMPORTANCE_CODE(IMP_UPDATE, TASK_POLICY_TASK) | DBG_FUNC_START),
699 task_pid(task), teffective_0(task),
700 teffective_1(task), task->priority, 0);
701
702 task_policy_update_internal_locked(task, FALSE, pend_token);
703
704 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
705 (IMPORTANCE_CODE(IMP_UPDATE, TASK_POLICY_TASK)) | DBG_FUNC_END,
706 task_pid(task), teffective_0(task),
707 teffective_1(task), task->priority, 0);
708 }
709
710 /*
711 * One state update function TO RULE THEM ALL
712 *
713 * This function updates the task or thread effective policy fields
714 * and pushes the results to the relevant subsystems.
715 *
716 * Must call update_complete after unlocking the task,
717 * as some subsystems cannot be updated while holding the task lock.
718 *
719 * Called with task locked, not thread
720 */
721
722 static void
723 task_policy_update_internal_locked(task_t task, boolean_t in_create, task_pend_token_t pend_token)
724 {
725 /*
726 * Step 1:
727 * Gather requested policy
728 */
729
730 struct task_requested_policy requested = task->requested_policy;
731
732 /*
733 * Step 2:
734 * Calculate new effective policies from requested policy and task state
735 * Rules:
736 * Don't change requested, it won't take effect
737 */
738
739 struct task_effective_policy next = {};
740
741 /* Update task role */
742 next.tep_role = requested.trp_role;
743
744 /* Set task qos clamp and ceiling */
745 next.tep_qos_clamp = requested.trp_qos_clamp;
746
747 if (requested.trp_apptype == TASK_APPTYPE_APP_DEFAULT ||
748 requested.trp_apptype == TASK_APPTYPE_APP_TAL) {
749
750 switch (next.tep_role) {
751 case TASK_FOREGROUND_APPLICATION:
752 /* Foreground apps get urgent scheduler priority */
753 next.tep_qos_ui_is_urgent = 1;
754 next.tep_qos_ceiling = THREAD_QOS_UNSPECIFIED;
755 break;
756
757 case TASK_BACKGROUND_APPLICATION:
758 /* This is really 'non-focal but on-screen' */
759 next.tep_qos_ceiling = THREAD_QOS_UNSPECIFIED;
760 break;
761
762 case TASK_DEFAULT_APPLICATION:
763 /* This is 'may render UI but we don't know if it's focal/nonfocal' */
764 next.tep_qos_ceiling = THREAD_QOS_UNSPECIFIED;
765 break;
766
767 case TASK_NONUI_APPLICATION:
768 /* i.e. 'off-screen' */
769 next.tep_qos_ceiling = THREAD_QOS_LEGACY;
770 break;
771
772 case TASK_CONTROL_APPLICATION:
773 case TASK_GRAPHICS_SERVER:
774 next.tep_qos_ui_is_urgent = 1;
775 next.tep_qos_ceiling = THREAD_QOS_UNSPECIFIED;
776 break;
777
778 case TASK_THROTTLE_APPLICATION:
779 /* i.e. 'TAL launch' */
780 next.tep_qos_ceiling = THREAD_QOS_UTILITY;
781 break;
782
783 case TASK_UNSPECIFIED:
784 default:
785 /* Apps that don't have an application role get
786 * USER_INTERACTIVE and USER_INITIATED squashed to LEGACY */
787 next.tep_qos_ceiling = THREAD_QOS_LEGACY;
788 break;
789 }
790 } else {
791 /* Daemons get USER_INTERACTIVE squashed to USER_INITIATED */
792 next.tep_qos_ceiling = THREAD_QOS_USER_INITIATED;
793 }
794
795 /* Calculate DARWIN_BG */
796 boolean_t wants_darwinbg = FALSE;
797 boolean_t wants_all_sockets_bg = FALSE; /* Do I want my existing sockets to be bg */
798 boolean_t wants_watchersbg = FALSE; /* Do I want my pidbound threads to be bg */
799
800 /*
801 * If DARWIN_BG has been requested at either level, it's engaged.
802 * Only true DARWIN_BG changes cause watchers to transition.
803 *
804 * Backgrounding due to apptype does.
805 */
806 if (requested.trp_int_darwinbg || requested.trp_ext_darwinbg)
807 wants_watchersbg = wants_all_sockets_bg = wants_darwinbg = TRUE;
808
809 /* Background TAL apps are throttled when TAL is enabled */
810 if (requested.trp_apptype == TASK_APPTYPE_APP_TAL &&
811 requested.trp_role == TASK_BACKGROUND_APPLICATION &&
812 requested.trp_tal_enabled == 1) {
813 next.tep_tal_engaged = 1;
814 }
815
816 if ((requested.trp_apptype == TASK_APPTYPE_APP_DEFAULT ||
817 requested.trp_apptype == TASK_APPTYPE_APP_TAL) &&
818 requested.trp_role == TASK_THROTTLE_APPLICATION) {
819 next.tep_tal_engaged = 1;
820 }
821
822 /* Adaptive daemons are DARWIN_BG unless boosted, and don't get network throttled. */
823 if (requested.trp_apptype == TASK_APPTYPE_DAEMON_ADAPTIVE &&
824 requested.trp_boosted == 0)
825 wants_darwinbg = TRUE;
826
827 /* Background daemons are always DARWIN_BG, no exceptions, and don't get network throttled. */
828 if (requested.trp_apptype == TASK_APPTYPE_DAEMON_BACKGROUND)
829 wants_darwinbg = TRUE;
830
831 if (next.tep_qos_clamp == THREAD_QOS_BACKGROUND || next.tep_qos_clamp == THREAD_QOS_MAINTENANCE)
832 wants_darwinbg = TRUE;
833
834 /* Calculate side effects of DARWIN_BG */
835
836 if (wants_darwinbg) {
837 next.tep_darwinbg = 1;
838 /* darwinbg tasks always create bg sockets, but we don't always loop over all sockets */
839 next.tep_new_sockets_bg = 1;
840 next.tep_lowpri_cpu = 1;
841 }
842
843 if (wants_all_sockets_bg)
844 next.tep_all_sockets_bg = 1;
845
846 if (wants_watchersbg)
847 next.tep_watchers_bg = 1;
848
849 /* Calculate low CPU priority */
850
851 boolean_t wants_lowpri_cpu = FALSE;
852
853 if (wants_darwinbg)
854 wants_lowpri_cpu = TRUE;
855
856 if (next.tep_tal_engaged)
857 wants_lowpri_cpu = TRUE;
858
859 if (requested.trp_sup_lowpri_cpu && requested.trp_boosted == 0)
860 wants_lowpri_cpu = TRUE;
861
862 if (wants_lowpri_cpu)
863 next.tep_lowpri_cpu = 1;
864
865 /* Calculate IO policy */
866
867 /* Update BG IO policy (so we can see if it has changed) */
868 next.tep_bg_iotier = requested.trp_bg_iotier;
869
870 int iopol = THROTTLE_LEVEL_TIER0;
871
872 if (wants_darwinbg)
873 iopol = MAX(iopol, requested.trp_bg_iotier);
874
875 if (requested.trp_apptype == TASK_APPTYPE_DAEMON_STANDARD)
876 iopol = MAX(iopol, proc_standard_daemon_tier);
877
878 if (requested.trp_sup_disk && requested.trp_boosted == 0)
879 iopol = MAX(iopol, proc_suppressed_disk_tier);
880
881 if (next.tep_tal_engaged)
882 iopol = MAX(iopol, proc_tal_disk_tier);
883
884 if (next.tep_qos_clamp != THREAD_QOS_UNSPECIFIED)
885 iopol = MAX(iopol, thread_qos_policy_params.qos_iotier[next.tep_qos_clamp]);
886
887 iopol = MAX(iopol, requested.trp_int_iotier);
888 iopol = MAX(iopol, requested.trp_ext_iotier);
889
890 next.tep_io_tier = iopol;
891
892 /* Calculate Passive IO policy */
893
894 if (requested.trp_ext_iopassive || requested.trp_int_iopassive)
895 next.tep_io_passive = 1;
896
897 /* Calculate suppression-active flag */
898 if (requested.trp_sup_active && requested.trp_boosted == 0)
899 next.tep_sup_active = 1;
900
901 /* Calculate timer QOS */
902 int latency_qos = requested.trp_base_latency_qos;
903
904 if (requested.trp_sup_timer && requested.trp_boosted == 0)
905 latency_qos = requested.trp_sup_timer;
906
907 if (next.tep_qos_clamp != THREAD_QOS_UNSPECIFIED)
908 latency_qos = MAX(latency_qos, (int)thread_qos_policy_params.qos_latency_qos[next.tep_qos_clamp]);
909
910 if (requested.trp_over_latency_qos != 0)
911 latency_qos = requested.trp_over_latency_qos;
912
913 /* Treat the windowserver special */
914 if (requested.trp_role == TASK_GRAPHICS_SERVER)
915 latency_qos = proc_graphics_timer_qos;
916
917 next.tep_latency_qos = latency_qos;
918
919 /* Calculate throughput QOS */
920 int through_qos = requested.trp_base_through_qos;
921
922 if (requested.trp_sup_throughput && requested.trp_boosted == 0)
923 through_qos = requested.trp_sup_throughput;
924
925 if (next.tep_qos_clamp != THREAD_QOS_UNSPECIFIED)
926 through_qos = MAX(through_qos, (int)thread_qos_policy_params.qos_through_qos[next.tep_qos_clamp]);
927
928 if (requested.trp_over_through_qos != 0)
929 through_qos = requested.trp_over_through_qos;
930
931 next.tep_through_qos = through_qos;
932
933 /* Calculate suppressed CPU priority */
934 if (requested.trp_sup_cpu && requested.trp_boosted == 0)
935 next.tep_suppressed_cpu = 1;
936
937 /*
938 * Calculate background sockets
939 * Don't take into account boosting to limit transition frequency.
940 */
941 if (requested.trp_sup_bg_sockets){
942 next.tep_all_sockets_bg = 1;
943 next.tep_new_sockets_bg = 1;
944 }
945
946 /* Apply SFI Managed class bit */
947 next.tep_sfi_managed = requested.trp_sfi_managed;
948
949 /* Calculate 'live donor' status for live importance */
950 switch (requested.trp_apptype) {
951 case TASK_APPTYPE_APP_TAL:
952 case TASK_APPTYPE_APP_DEFAULT:
953 if (requested.trp_ext_darwinbg == 0)
954 next.tep_live_donor = 1;
955 else
956 next.tep_live_donor = 0;
957 break;
958
959 case TASK_APPTYPE_DAEMON_INTERACTIVE:
960 case TASK_APPTYPE_DAEMON_STANDARD:
961 case TASK_APPTYPE_DAEMON_ADAPTIVE:
962 case TASK_APPTYPE_DAEMON_BACKGROUND:
963 default:
964 next.tep_live_donor = 0;
965 break;
966 }
967
968 if (requested.trp_terminated) {
969 /*
970 * Shoot down the throttles that slow down exit or response to SIGTERM
971 * We don't need to shoot down:
972 * passive (don't want to cause others to throttle)
973 * all_sockets_bg (don't need to iterate FDs on every exit)
974 * new_sockets_bg (doesn't matter for exiting process)
975 * pidsuspend (jetsam-ed BG process shouldn't run again)
976 * watchers_bg (watcher threads don't need to be unthrottled)
977 * latency_qos (affects userspace timers only)
978 */
979
980 next.tep_terminated = 1;
981 next.tep_darwinbg = 0;
982 next.tep_lowpri_cpu = 0;
983 next.tep_io_tier = THROTTLE_LEVEL_TIER0;
984 next.tep_tal_engaged = 0;
985 next.tep_role = TASK_UNSPECIFIED;
986 next.tep_suppressed_cpu = 0;
987 }
988
989 /*
990 * Step 3:
991 * Swap out old policy for new policy
992 */
993
994 struct task_effective_policy prev = task->effective_policy;
995
996 /* This is the point where the new values become visible to other threads */
997 task->effective_policy = next;
998
999 /* Don't do anything further to a half-formed task */
1000 if (in_create)
1001 return;
1002
1003 if (task == kernel_task)
1004 panic("Attempting to set task policy on kernel_task");
1005
1006 /*
1007 * Step 4:
1008 * Pend updates that can't be done while holding the task lock
1009 */
1010
1011 if (prev.tep_all_sockets_bg != next.tep_all_sockets_bg)
1012 pend_token->tpt_update_sockets = 1;
1013
1014 /* Only re-scan the timer list if the qos level is getting less strong */
1015 if (prev.tep_latency_qos > next.tep_latency_qos)
1016 pend_token->tpt_update_timers = 1;
1017
1018
1019 if (prev.tep_live_donor != next.tep_live_donor)
1020 pend_token->tpt_update_live_donor = 1;
1021
1022 /*
1023 * Step 5:
1024 * Update other subsystems as necessary if something has changed
1025 */
1026
1027 boolean_t update_threads = FALSE, update_sfi = FALSE;
1028
1029 /*
1030 * Check for the attributes that thread_policy_update_internal_locked() consults,
1031 * and trigger thread policy re-evaluation.
1032 */
1033 if (prev.tep_io_tier != next.tep_io_tier ||
1034 prev.tep_bg_iotier != next.tep_bg_iotier ||
1035 prev.tep_io_passive != next.tep_io_passive ||
1036 prev.tep_darwinbg != next.tep_darwinbg ||
1037 prev.tep_qos_clamp != next.tep_qos_clamp ||
1038 prev.tep_qos_ceiling != next.tep_qos_ceiling ||
1039 prev.tep_qos_ui_is_urgent != next.tep_qos_ui_is_urgent ||
1040 prev.tep_latency_qos != next.tep_latency_qos ||
1041 prev.tep_through_qos != next.tep_through_qos ||
1042 prev.tep_lowpri_cpu != next.tep_lowpri_cpu ||
1043 prev.tep_new_sockets_bg != next.tep_new_sockets_bg ||
1044 prev.tep_terminated != next.tep_terminated )
1045 update_threads = TRUE;
1046
1047 /*
1048 * Check for the attributes that sfi_thread_classify() consults,
1049 * and trigger SFI re-evaluation.
1050 */
1051 if (prev.tep_latency_qos != next.tep_latency_qos ||
1052 prev.tep_role != next.tep_role ||
1053 prev.tep_sfi_managed != next.tep_sfi_managed )
1054 update_sfi = TRUE;
1055
1056 #if CONFIG_SCHED_SFI
1057 /* Reflect task role transitions into the coalition role counters */
1058 if (prev.tep_role != next.tep_role) {
1059 if (task_policy_update_coalition_focal_tasks(task, prev.tep_role, next.tep_role)) {
1060 update_sfi = TRUE;
1061 pend_token->tpt_update_coal_sfi = 1;
1062 }
1063 }
1064 #endif /* !CONFIG_SCHED_SFI */
1065
1066 boolean_t update_priority = FALSE;
1067
1068 int priority = BASEPRI_DEFAULT;
1069 int max_priority = MAXPRI_USER;
1070
1071 if (next.tep_lowpri_cpu) {
1072 priority = MAXPRI_THROTTLE;
1073 max_priority = MAXPRI_THROTTLE;
1074 } else if (next.tep_suppressed_cpu) {
1075 priority = MAXPRI_SUPPRESSED;
1076 max_priority = MAXPRI_SUPPRESSED;
1077 } else {
1078 switch (next.tep_role) {
1079 case TASK_CONTROL_APPLICATION:
1080 priority = BASEPRI_CONTROL;
1081 break;
1082 case TASK_GRAPHICS_SERVER:
1083 priority = BASEPRI_GRAPHICS;
1084 max_priority = MAXPRI_RESERVED;
1085 break;
1086 default:
1087 break;
1088 }
1089
1090 /* factor in 'nice' value */
1091 priority += task->importance;
1092
1093 if (task->effective_policy.tep_qos_clamp != THREAD_QOS_UNSPECIFIED) {
1094 int qos_clamp_priority = thread_qos_policy_params.qos_pri[task->effective_policy.tep_qos_clamp];
1095
1096 priority = MIN(priority, qos_clamp_priority);
1097 max_priority = MIN(max_priority, qos_clamp_priority);
1098 }
1099
1100 if (priority > max_priority)
1101 priority = max_priority;
1102 else if (priority < MINPRI)
1103 priority = MINPRI;
1104 }
1105
1106 assert(priority <= max_priority);
1107
1108 /* avoid extra work if priority isn't changing */
1109 if (priority != task->priority ||
1110 max_priority != task->max_priority ) {
1111 /* update the scheduling priority for the task */
1112 task->max_priority = max_priority;
1113 task->priority = priority;
1114 update_priority = TRUE;
1115 }
1116
1117 /* Loop over the threads in the task:
1118 * only once
1119 * only if necessary
1120 * with one thread mutex hold per thread
1121 */
1122 if (update_threads || update_priority || update_sfi) {
1123 thread_t thread;
1124
1125 queue_iterate(&task->threads, thread, thread_t, task_threads) {
1126 struct task_pend_token thread_pend_token = {};
1127
1128 if (update_sfi)
1129 thread_pend_token.tpt_update_thread_sfi = 1;
1130
1131 if (update_priority || update_threads)
1132 thread_policy_update_tasklocked(thread,
1133 task->priority, task->max_priority,
1134 &thread_pend_token);
1135
1136 assert(!thread_pend_token.tpt_update_sockets);
1137
1138 // Slightly risky, as we still hold the task lock...
1139 thread_policy_update_complete_unlocked(thread, &thread_pend_token);
1140 }
1141 }
1142 }
1143
1144
1145 #if CONFIG_SCHED_SFI
1146 /*
1147 * Yet another layering violation. We reach out and bang on the coalition directly.
1148 */
1149 static boolean_t
1150 task_policy_update_coalition_focal_tasks(task_t task,
1151 int prev_role,
1152 int next_role)
1153 {
1154 boolean_t sfi_transition = FALSE;
1155
1156 /* task moving into/out-of the foreground */
1157 if (prev_role != TASK_FOREGROUND_APPLICATION && next_role == TASK_FOREGROUND_APPLICATION) {
1158 if (task_coalition_adjust_focal_count(task, 1) == 1)
1159 sfi_transition = TRUE;
1160 } else if (prev_role == TASK_FOREGROUND_APPLICATION && next_role != TASK_FOREGROUND_APPLICATION) {
1161 if (task_coalition_adjust_focal_count(task, -1) == 0)
1162 sfi_transition = TRUE;
1163 }
1164
1165 /* task moving into/out-of background */
1166 if (prev_role != TASK_BACKGROUND_APPLICATION && next_role == TASK_BACKGROUND_APPLICATION) {
1167 if (task_coalition_adjust_nonfocal_count(task, 1) == 1)
1168 sfi_transition = TRUE;
1169 } else if (prev_role == TASK_BACKGROUND_APPLICATION && next_role != TASK_BACKGROUND_APPLICATION) {
1170 if (task_coalition_adjust_nonfocal_count(task, -1) == 0)
1171 sfi_transition = TRUE;
1172 }
1173
1174 return sfi_transition;
1175 }
1176
1177 /* coalition object is locked */
1178 static void
1179 task_sfi_reevaluate_cb(coalition_t coal, void *ctx, task_t task)
1180 {
1181 thread_t thread;
1182
1183 /* unused for now */
1184 (void)coal;
1185
1186 /* skip the task we're re-evaluating on behalf of: it's already updated */
1187 if (task == (task_t)ctx)
1188 return;
1189
1190 task_lock(task);
1191
1192 queue_iterate(&task->threads, thread, thread_t, task_threads) {
1193 sfi_reevaluate(thread);
1194 }
1195
1196 task_unlock(task);
1197 }
1198 #endif /* CONFIG_SCHED_SFI */
1199
1200 /*
1201 * Called with task unlocked to do things that can't be done while holding the task lock
1202 */
1203 void
1204 task_policy_update_complete_unlocked(task_t task, task_pend_token_t pend_token)
1205 {
1206 #ifdef MACH_BSD
1207 if (pend_token->tpt_update_sockets)
1208 proc_apply_task_networkbg(task->bsd_info, THREAD_NULL);
1209 #endif /* MACH_BSD */
1210
1211 /* The timer throttle has been removed or reduced, we need to look for expired timers and fire them */
1212 if (pend_token->tpt_update_timers)
1213 ml_timer_evaluate();
1214
1215
1216 if (pend_token->tpt_update_live_donor)
1217 task_importance_update_live_donor(task);
1218
1219 #if CONFIG_SCHED_SFI
1220 /* use the resource coalition for SFI re-evaluation */
1221 if (pend_token->tpt_update_coal_sfi)
1222 coalition_for_each_task(task->coalition[COALITION_TYPE_RESOURCE],
1223 (void *)task, task_sfi_reevaluate_cb);
1224 #endif /* CONFIG_SCHED_SFI */
1225 }
1226
1227 /*
1228 * Initiate a task policy state transition
1229 *
1230 * Everything that modifies requested except functions that need to hold the task lock
1231 * should use this function
1232 *
1233 * Argument validation should be performed before reaching this point.
1234 *
1235 * TODO: Do we need to check task->active?
1236 */
1237 void
1238 proc_set_task_policy(task_t task,
1239 int category,
1240 int flavor,
1241 int value)
1242 {
1243 struct task_pend_token pend_token = {};
1244
1245 task_lock(task);
1246
1247 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
1248 (IMPORTANCE_CODE(flavor, (category | TASK_POLICY_TASK))) | DBG_FUNC_START,
1249 task_pid(task), trequested_0(task),
1250 trequested_1(task), value, 0);
1251
1252 proc_set_task_policy_locked(task, category, flavor, value, 0);
1253
1254 task_policy_update_locked(task, &pend_token);
1255
1256
1257 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
1258 (IMPORTANCE_CODE(flavor, (category | TASK_POLICY_TASK))) | DBG_FUNC_END,
1259 task_pid(task), trequested_0(task),
1260 trequested_1(task), tpending(&pend_token), 0);
1261
1262 task_unlock(task);
1263
1264 task_policy_update_complete_unlocked(task, &pend_token);
1265 }
1266
1267 /*
1268 * Variant of proc_set_task_policy() that sets two scalars in the requested policy structure.
1269 * Same locking rules apply.
1270 */
1271 void
1272 proc_set_task_policy2(task_t task,
1273 int category,
1274 int flavor,
1275 int value,
1276 int value2)
1277 {
1278 struct task_pend_token pend_token = {};
1279
1280 task_lock(task);
1281
1282 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
1283 (IMPORTANCE_CODE(flavor, (category | TASK_POLICY_TASK))) | DBG_FUNC_START,
1284 task_pid(task), trequested_0(task),
1285 trequested_1(task), value, 0);
1286
1287 proc_set_task_policy_locked(task, category, flavor, value, value2);
1288
1289 task_policy_update_locked(task, &pend_token);
1290
1291 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
1292 (IMPORTANCE_CODE(flavor, (category | TASK_POLICY_TASK))) | DBG_FUNC_END,
1293 task_pid(task), trequested_0(task),
1294 trequested_1(task), tpending(&pend_token), 0);
1295
1296 task_unlock(task);
1297
1298 task_policy_update_complete_unlocked(task, &pend_token);
1299 }
1300
1301 /*
1302 * Set the requested state for a specific flavor to a specific value.
1303 *
1304 * TODO:
1305 * Verify that arguments to non iopol things are 1 or 0
1306 */
1307 static void
1308 proc_set_task_policy_locked(task_t task,
1309 int category,
1310 int flavor,
1311 int value,
1312 int value2)
1313 {
1314 int tier, passive;
1315
1316 struct task_requested_policy requested = task->requested_policy;
1317
1318 switch (flavor) {
1319
1320 /* Category: EXTERNAL and INTERNAL */
1321
1322 case TASK_POLICY_DARWIN_BG:
1323 if (category == TASK_POLICY_EXTERNAL)
1324 requested.trp_ext_darwinbg = value;
1325 else
1326 requested.trp_int_darwinbg = value;
1327 break;
1328
1329 case TASK_POLICY_IOPOL:
1330 proc_iopol_to_tier(value, &tier, &passive);
1331 if (category == TASK_POLICY_EXTERNAL) {
1332 requested.trp_ext_iotier = tier;
1333 requested.trp_ext_iopassive = passive;
1334 } else {
1335 requested.trp_int_iotier = tier;
1336 requested.trp_int_iopassive = passive;
1337 }
1338 break;
1339
1340 case TASK_POLICY_IO:
1341 if (category == TASK_POLICY_EXTERNAL)
1342 requested.trp_ext_iotier = value;
1343 else
1344 requested.trp_int_iotier = value;
1345 break;
1346
1347 case TASK_POLICY_PASSIVE_IO:
1348 if (category == TASK_POLICY_EXTERNAL)
1349 requested.trp_ext_iopassive = value;
1350 else
1351 requested.trp_int_iopassive = value;
1352 break;
1353
1354 /* Category: INTERNAL */
1355
1356 case TASK_POLICY_DARWIN_BG_IOPOL:
1357 assert(category == TASK_POLICY_INTERNAL);
1358 proc_iopol_to_tier(value, &tier, &passive);
1359 requested.trp_bg_iotier = tier;
1360 break;
1361
1362 /* Category: ATTRIBUTE */
1363
1364 case TASK_POLICY_TAL:
1365 assert(category == TASK_POLICY_ATTRIBUTE);
1366 requested.trp_tal_enabled = value;
1367 break;
1368
1369 case TASK_POLICY_BOOST:
1370 assert(category == TASK_POLICY_ATTRIBUTE);
1371 requested.trp_boosted = value;
1372 break;
1373
1374 case TASK_POLICY_ROLE:
1375 assert(category == TASK_POLICY_ATTRIBUTE);
1376 requested.trp_role = value;
1377 break;
1378
1379 case TASK_POLICY_TERMINATED:
1380 assert(category == TASK_POLICY_ATTRIBUTE);
1381 requested.trp_terminated = value;
1382 break;
1383
1384 case TASK_BASE_LATENCY_QOS_POLICY:
1385 assert(category == TASK_POLICY_ATTRIBUTE);
1386 requested.trp_base_latency_qos = value;
1387 break;
1388
1389 case TASK_BASE_THROUGHPUT_QOS_POLICY:
1390 assert(category == TASK_POLICY_ATTRIBUTE);
1391 requested.trp_base_through_qos = value;
1392 break;
1393
1394 case TASK_POLICY_SFI_MANAGED:
1395 assert(category == TASK_POLICY_ATTRIBUTE);
1396 requested.trp_sfi_managed = value;
1397 break;
1398
1399 case TASK_POLICY_BASE_LATENCY_AND_THROUGHPUT_QOS:
1400 assert(category == TASK_POLICY_ATTRIBUTE);
1401 requested.trp_base_latency_qos = value;
1402 requested.trp_base_through_qos = value2;
1403 break;
1404
1405 case TASK_POLICY_OVERRIDE_LATENCY_AND_THROUGHPUT_QOS:
1406 assert(category == TASK_POLICY_ATTRIBUTE);
1407 requested.trp_over_latency_qos = value;
1408 requested.trp_over_through_qos = value2;
1409 break;
1410
1411 default:
1412 panic("unknown task policy: %d %d %d %d", category, flavor, value, value2);
1413 break;
1414 }
1415
1416 task->requested_policy = requested;
1417 }
1418
1419 /*
1420 * Gets what you set. Effective values may be different.
1421 */
1422 int
1423 proc_get_task_policy(task_t task,
1424 int category,
1425 int flavor)
1426 {
1427 int value = 0;
1428
1429 task_lock(task);
1430
1431 struct task_requested_policy requested = task->requested_policy;
1432
1433 switch (flavor) {
1434 case TASK_POLICY_DARWIN_BG:
1435 if (category == TASK_POLICY_EXTERNAL)
1436 value = requested.trp_ext_darwinbg;
1437 else
1438 value = requested.trp_int_darwinbg;
1439 break;
1440 case TASK_POLICY_IOPOL:
1441 if (category == TASK_POLICY_EXTERNAL)
1442 value = proc_tier_to_iopol(requested.trp_ext_iotier,
1443 requested.trp_ext_iopassive);
1444 else
1445 value = proc_tier_to_iopol(requested.trp_int_iotier,
1446 requested.trp_int_iopassive);
1447 break;
1448 case TASK_POLICY_IO:
1449 if (category == TASK_POLICY_EXTERNAL)
1450 value = requested.trp_ext_iotier;
1451 else
1452 value = requested.trp_int_iotier;
1453 break;
1454 case TASK_POLICY_PASSIVE_IO:
1455 if (category == TASK_POLICY_EXTERNAL)
1456 value = requested.trp_ext_iopassive;
1457 else
1458 value = requested.trp_int_iopassive;
1459 break;
1460 case TASK_POLICY_DARWIN_BG_IOPOL:
1461 assert(category == TASK_POLICY_ATTRIBUTE);
1462 value = proc_tier_to_iopol(requested.trp_bg_iotier, 0);
1463 break;
1464 case TASK_POLICY_ROLE:
1465 assert(category == TASK_POLICY_ATTRIBUTE);
1466 value = requested.trp_role;
1467 break;
1468 case TASK_POLICY_SFI_MANAGED:
1469 assert(category == TASK_POLICY_ATTRIBUTE);
1470 value = requested.trp_sfi_managed;
1471 break;
1472 default:
1473 panic("unknown policy_flavor %d", flavor);
1474 break;
1475 }
1476
1477 task_unlock(task);
1478
1479 return value;
1480 }
1481
1482 /*
1483 * Variant of proc_get_task_policy() that returns two scalar outputs.
1484 */
1485 void
1486 proc_get_task_policy2(task_t task,
1487 __assert_only int category,
1488 int flavor,
1489 int *value1,
1490 int *value2)
1491 {
1492 task_lock(task);
1493
1494 struct task_requested_policy requested = task->requested_policy;
1495
1496 switch (flavor) {
1497 case TASK_POLICY_BASE_LATENCY_AND_THROUGHPUT_QOS:
1498 assert(category == TASK_POLICY_ATTRIBUTE);
1499 *value1 = requested.trp_base_latency_qos;
1500 *value2 = requested.trp_base_through_qos;
1501 break;
1502
1503 case TASK_POLICY_OVERRIDE_LATENCY_AND_THROUGHPUT_QOS:
1504 assert(category == TASK_POLICY_ATTRIBUTE);
1505 *value1 = requested.trp_over_latency_qos;
1506 *value2 = requested.trp_over_through_qos;
1507 break;
1508
1509 default:
1510 panic("unknown policy_flavor %d", flavor);
1511 break;
1512 }
1513
1514 task_unlock(task);
1515 }
1516
1517 /*
1518 * Function for querying effective state for relevant subsystems
1519 * Gets what is actually in effect, for subsystems which pull policy instead of receive updates.
1520 *
1521 * ONLY the relevant subsystem should query this.
1522 * NEVER take a value from the 'effective' function and stuff it into a setter.
1523 *
1524 * NOTE: This accessor does not take the task lock.
1525 * Notifications of state updates need to be externally synchronized with state queries.
1526 * This routine *MUST* remain interrupt safe, as it is potentially invoked
1527 * within the context of a timer interrupt. It is also called in KDP context for stackshot.
1528 */
1529 int
1530 proc_get_effective_task_policy(task_t task,
1531 int flavor)
1532 {
1533 int value = 0;
1534
1535 switch (flavor) {
1536 case TASK_POLICY_DARWIN_BG:
1537 /*
1538 * This backs the KPI call proc_pidbackgrounded to find
1539 * out if a pid is backgrounded.
1540 * It is used to communicate state to the VM system, as well as
1541 * prioritizing requests to the graphics system.
1542 * Returns 1 for background mode, 0 for normal mode
1543 */
1544 value = task->effective_policy.tep_darwinbg;
1545 break;
1546 case TASK_POLICY_ALL_SOCKETS_BG:
1547 /*
1548 * do_background_socket() calls this to determine what it should do to the proc's sockets
1549 * Returns 1 for background mode, 0 for normal mode
1550 *
1551 * This consults both thread and task so un-DBGing a thread while the task is BG
1552 * doesn't get you out of the network throttle.
1553 */
1554 value = task->effective_policy.tep_all_sockets_bg;
1555 break;
1556 case TASK_POLICY_LATENCY_QOS:
1557 /*
1558 * timer arming calls into here to find out the timer coalescing level
1559 * Returns a QoS tier (0-6)
1560 */
1561 value = task->effective_policy.tep_latency_qos;
1562 break;
1563 case TASK_POLICY_THROUGH_QOS:
1564 /*
1565 * This value is passed into the urgency callout from the scheduler
1566 * to the performance management subsystem.
1567 * Returns a QoS tier (0-6)
1568 */
1569 value = task->effective_policy.tep_through_qos;
1570 break;
1571 case TASK_POLICY_ROLE:
1572 /*
1573 * This controls various things that ask whether a process is foreground,
1574 * like SFI, VM, access to GPU, etc
1575 */
1576 value = task->effective_policy.tep_role;
1577 break;
1578 case TASK_POLICY_WATCHERS_BG:
1579 /*
1580 * This controls whether or not a thread watching this process should be BG.
1581 */
1582 value = task->effective_policy.tep_watchers_bg;
1583 break;
1584 case TASK_POLICY_SFI_MANAGED:
1585 /*
1586 * This controls whether or not a process is targeted for specific control by thermald.
1587 */
1588 value = task->effective_policy.tep_sfi_managed;
1589 break;
1590 default:
1591 panic("unknown policy_flavor %d", flavor);
1592 break;
1593 }
1594
1595 return value;
1596 }
1597
1598 /*
1599 * Convert from IOPOL_* values to throttle tiers.
1600 *
1601 * TODO: Can this be made more compact, like an array lookup
1602 * Note that it is possible to support e.g. IOPOL_PASSIVE_STANDARD in the future
1603 */
1604
1605 void
1606 proc_iopol_to_tier(int iopolicy, int *tier, int *passive)
1607 {
1608 *passive = 0;
1609 *tier = 0;
1610 switch (iopolicy) {
1611 case IOPOL_IMPORTANT:
1612 *tier = THROTTLE_LEVEL_TIER0;
1613 break;
1614 case IOPOL_PASSIVE:
1615 *tier = THROTTLE_LEVEL_TIER0;
1616 *passive = 1;
1617 break;
1618 case IOPOL_STANDARD:
1619 *tier = THROTTLE_LEVEL_TIER1;
1620 break;
1621 case IOPOL_UTILITY:
1622 *tier = THROTTLE_LEVEL_TIER2;
1623 break;
1624 case IOPOL_THROTTLE:
1625 *tier = THROTTLE_LEVEL_TIER3;
1626 break;
1627 default:
1628 panic("unknown I/O policy %d", iopolicy);
1629 break;
1630 }
1631 }
1632
1633 int
1634 proc_tier_to_iopol(int tier, int passive)
1635 {
1636 if (passive == 1) {
1637 switch (tier) {
1638 case THROTTLE_LEVEL_TIER0:
1639 return IOPOL_PASSIVE;
1640 default:
1641 panic("unknown passive tier %d", tier);
1642 return IOPOL_DEFAULT;
1643 }
1644 } else {
1645 switch (tier) {
1646 case THROTTLE_LEVEL_NONE:
1647 case THROTTLE_LEVEL_TIER0:
1648 return IOPOL_DEFAULT;
1649 case THROTTLE_LEVEL_TIER1:
1650 return IOPOL_STANDARD;
1651 case THROTTLE_LEVEL_TIER2:
1652 return IOPOL_UTILITY;
1653 case THROTTLE_LEVEL_TIER3:
1654 return IOPOL_THROTTLE;
1655 default:
1656 panic("unknown tier %d", tier);
1657 return IOPOL_DEFAULT;
1658 }
1659 }
1660 }
1661
1662 int
1663 proc_darwin_role_to_task_role(int darwin_role, int* task_role)
1664 {
1665 integer_t role = TASK_UNSPECIFIED;
1666
1667 switch (darwin_role) {
1668 case PRIO_DARWIN_ROLE_DEFAULT:
1669 role = TASK_UNSPECIFIED;
1670 break;
1671 case PRIO_DARWIN_ROLE_UI_FOCAL:
1672 role = TASK_FOREGROUND_APPLICATION;
1673 break;
1674 case PRIO_DARWIN_ROLE_UI:
1675 role = TASK_DEFAULT_APPLICATION;
1676 break;
1677 case PRIO_DARWIN_ROLE_NON_UI:
1678 role = TASK_NONUI_APPLICATION;
1679 break;
1680 case PRIO_DARWIN_ROLE_UI_NON_FOCAL:
1681 role = TASK_BACKGROUND_APPLICATION;
1682 break;
1683 case PRIO_DARWIN_ROLE_TAL_LAUNCH:
1684 role = TASK_THROTTLE_APPLICATION;
1685 break;
1686 default:
1687 return EINVAL;
1688 }
1689
1690 *task_role = role;
1691
1692 return 0;
1693 }
1694
1695 int
1696 proc_task_role_to_darwin_role(int task_role)
1697 {
1698 switch (task_role) {
1699 case TASK_FOREGROUND_APPLICATION:
1700 return PRIO_DARWIN_ROLE_UI_FOCAL;
1701 case TASK_BACKGROUND_APPLICATION:
1702 return PRIO_DARWIN_ROLE_UI_NON_FOCAL;
1703 case TASK_NONUI_APPLICATION:
1704 return PRIO_DARWIN_ROLE_NON_UI;
1705 case TASK_DEFAULT_APPLICATION:
1706 return PRIO_DARWIN_ROLE_UI;
1707 case TASK_THROTTLE_APPLICATION:
1708 return PRIO_DARWIN_ROLE_TAL_LAUNCH;
1709 case TASK_UNSPECIFIED:
1710 default:
1711 return PRIO_DARWIN_ROLE_DEFAULT;
1712 }
1713 }
1714
1715
1716 /* TODO: remove this variable when interactive daemon audit period is over */
1717 extern boolean_t ipc_importance_interactive_receiver;
1718
1719 /*
1720 * Called at process exec to initialize the apptype, qos clamp, and qos seed of a process
1721 *
1722 * TODO: Make this function more table-driven instead of ad-hoc
1723 */
1724 void
1725 proc_set_task_spawnpolicy(task_t task, int apptype, int qos_clamp, int role,
1726 ipc_port_t * portwatch_ports, int portwatch_count)
1727 {
1728 struct task_pend_token pend_token = {};
1729
1730 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
1731 (IMPORTANCE_CODE(IMP_TASK_APPTYPE, apptype)) | DBG_FUNC_START,
1732 task_pid(task), trequested_0(task), trequested_1(task),
1733 apptype, 0);
1734
1735 switch (apptype) {
1736 case TASK_APPTYPE_APP_TAL:
1737 case TASK_APPTYPE_APP_DEFAULT:
1738 /* Apps become donors via the 'live-donor' flag instead of the static donor flag */
1739 task_importance_mark_donor(task, FALSE);
1740 task_importance_mark_live_donor(task, TRUE);
1741 task_importance_mark_receiver(task, FALSE);
1742 /* Apps are de-nap recievers on desktop for suppression behaviors */
1743 task_importance_mark_denap_receiver(task, TRUE);
1744 break;
1745
1746 case TASK_APPTYPE_DAEMON_INTERACTIVE:
1747 task_importance_mark_donor(task, TRUE);
1748 task_importance_mark_live_donor(task, FALSE);
1749
1750 /*
1751 * A boot arg controls whether interactive daemons are importance receivers.
1752 * Normally, they are not. But for testing their behavior as an adaptive
1753 * daemon, the boot-arg can be set.
1754 *
1755 * TODO: remove this when the interactive daemon audit period is over.
1756 */
1757 task_importance_mark_receiver(task, /* FALSE */ ipc_importance_interactive_receiver);
1758 task_importance_mark_denap_receiver(task, FALSE);
1759 break;
1760
1761 case TASK_APPTYPE_DAEMON_STANDARD:
1762 task_importance_mark_donor(task, TRUE);
1763 task_importance_mark_live_donor(task, FALSE);
1764 task_importance_mark_receiver(task, FALSE);
1765 task_importance_mark_denap_receiver(task, FALSE);
1766 break;
1767
1768 case TASK_APPTYPE_DAEMON_ADAPTIVE:
1769 task_importance_mark_donor(task, FALSE);
1770 task_importance_mark_live_donor(task, FALSE);
1771 task_importance_mark_receiver(task, TRUE);
1772 task_importance_mark_denap_receiver(task, FALSE);
1773 break;
1774
1775 case TASK_APPTYPE_DAEMON_BACKGROUND:
1776 task_importance_mark_donor(task, FALSE);
1777 task_importance_mark_live_donor(task, FALSE);
1778 task_importance_mark_receiver(task, FALSE);
1779 task_importance_mark_denap_receiver(task, FALSE);
1780 break;
1781
1782 case TASK_APPTYPE_NONE:
1783 break;
1784 }
1785
1786 if (portwatch_ports != NULL && apptype == TASK_APPTYPE_DAEMON_ADAPTIVE) {
1787 int portwatch_boosts = 0;
1788
1789 for (int i = 0; i < portwatch_count; i++) {
1790 ipc_port_t port = NULL;
1791
1792 if ((port = portwatch_ports[i]) != NULL) {
1793 int boost = 0;
1794 task_add_importance_watchport(task, port, &boost);
1795 portwatch_boosts += boost;
1796 }
1797 }
1798
1799 if (portwatch_boosts > 0) {
1800 task_importance_hold_internal_assertion(task, portwatch_boosts);
1801 }
1802 }
1803
1804 task_lock(task);
1805
1806 if (apptype == TASK_APPTYPE_APP_TAL) {
1807 /* TAL starts off enabled by default */
1808 task->requested_policy.trp_tal_enabled = 1;
1809 }
1810
1811 if (apptype != TASK_APPTYPE_NONE) {
1812 task->requested_policy.trp_apptype = apptype;
1813 }
1814
1815 if (role != TASK_UNSPECIFIED) {
1816 task->requested_policy.trp_role = role;
1817 }
1818
1819 if (qos_clamp != THREAD_QOS_UNSPECIFIED) {
1820 task->requested_policy.trp_qos_clamp = qos_clamp;
1821 }
1822
1823 task_policy_update_locked(task, &pend_token);
1824
1825 task_unlock(task);
1826
1827 /* Ensure the donor bit is updated to be in sync with the new live donor status */
1828 pend_token.tpt_update_live_donor = 1;
1829
1830 task_policy_update_complete_unlocked(task, &pend_token);
1831
1832 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
1833 (IMPORTANCE_CODE(IMP_TASK_APPTYPE, apptype)) | DBG_FUNC_END,
1834 task_pid(task), trequested_0(task), trequested_1(task),
1835 task_is_importance_receiver(task), 0);
1836 }
1837
1838 /*
1839 * Inherit task role across exec
1840 */
1841 void
1842 proc_inherit_task_role(task_t new_task,
1843 task_t old_task)
1844 {
1845 int role;
1846
1847 /* inherit the role from old task to new task */
1848 role = proc_get_task_policy(old_task, TASK_POLICY_ATTRIBUTE, TASK_POLICY_ROLE);
1849 proc_set_task_policy(new_task, TASK_POLICY_ATTRIBUTE, TASK_POLICY_ROLE, role);
1850 }
1851
1852 extern task_t bsd_init_task;
1853
1854 /*
1855 * Compute the default main thread qos for a task
1856 */
1857 int
1858 task_compute_main_thread_qos(task_t task)
1859 {
1860 int primordial_qos = THREAD_QOS_UNSPECIFIED;
1861
1862 int qos_clamp = task->requested_policy.trp_qos_clamp;
1863
1864 switch (task->requested_policy.trp_apptype) {
1865 case TASK_APPTYPE_APP_TAL:
1866 case TASK_APPTYPE_APP_DEFAULT:
1867 primordial_qos = THREAD_QOS_USER_INTERACTIVE;
1868 break;
1869
1870 case TASK_APPTYPE_DAEMON_INTERACTIVE:
1871 case TASK_APPTYPE_DAEMON_STANDARD:
1872 case TASK_APPTYPE_DAEMON_ADAPTIVE:
1873 primordial_qos = THREAD_QOS_LEGACY;
1874 break;
1875
1876 case TASK_APPTYPE_DAEMON_BACKGROUND:
1877 primordial_qos = THREAD_QOS_BACKGROUND;
1878 break;
1879 }
1880
1881 if (task == bsd_init_task) {
1882 /* PID 1 gets a special case */
1883 primordial_qos = MAX(primordial_qos, THREAD_QOS_USER_INITIATED);
1884 }
1885
1886 if (qos_clamp != THREAD_QOS_UNSPECIFIED) {
1887 if (primordial_qos != THREAD_QOS_UNSPECIFIED) {
1888 primordial_qos = MIN(qos_clamp, primordial_qos);
1889 } else {
1890 primordial_qos = qos_clamp;
1891 }
1892 }
1893
1894 return primordial_qos;
1895 }
1896
1897
1898 /* for process_policy to check before attempting to set */
1899 boolean_t
1900 proc_task_is_tal(task_t task)
1901 {
1902 return (task->requested_policy.trp_apptype == TASK_APPTYPE_APP_TAL) ? TRUE : FALSE;
1903 }
1904
1905 int
1906 task_get_apptype(task_t task)
1907 {
1908 return task->requested_policy.trp_apptype;
1909 }
1910
1911 boolean_t
1912 task_is_daemon(task_t task)
1913 {
1914 switch (task->requested_policy.trp_apptype) {
1915 case TASK_APPTYPE_DAEMON_INTERACTIVE:
1916 case TASK_APPTYPE_DAEMON_STANDARD:
1917 case TASK_APPTYPE_DAEMON_ADAPTIVE:
1918 case TASK_APPTYPE_DAEMON_BACKGROUND:
1919 return TRUE;
1920 default:
1921 return FALSE;
1922 }
1923 }
1924
1925 boolean_t
1926 task_is_app(task_t task)
1927 {
1928 switch (task->requested_policy.trp_apptype) {
1929 case TASK_APPTYPE_APP_DEFAULT:
1930 case TASK_APPTYPE_APP_TAL:
1931 return TRUE;
1932 default:
1933 return FALSE;
1934 }
1935 }
1936
1937 /* for telemetry */
1938 integer_t
1939 task_grab_latency_qos(task_t task)
1940 {
1941 return qos_latency_policy_package(proc_get_effective_task_policy(task, TASK_POLICY_LATENCY_QOS));
1942 }
1943
1944 /* update the darwin background action state in the flags field for libproc */
1945 int
1946 proc_get_darwinbgstate(task_t task, uint32_t * flagsp)
1947 {
1948 if (task->requested_policy.trp_ext_darwinbg)
1949 *flagsp |= PROC_FLAG_EXT_DARWINBG;
1950
1951 if (task->requested_policy.trp_int_darwinbg)
1952 *flagsp |= PROC_FLAG_DARWINBG;
1953
1954
1955 if (task->requested_policy.trp_apptype == TASK_APPTYPE_APP_DEFAULT ||
1956 task->requested_policy.trp_apptype == TASK_APPTYPE_APP_TAL)
1957 *flagsp |= PROC_FLAG_APPLICATION;
1958
1959 if (task->requested_policy.trp_apptype == TASK_APPTYPE_DAEMON_ADAPTIVE)
1960 *flagsp |= PROC_FLAG_ADAPTIVE;
1961
1962 if (task->requested_policy.trp_apptype == TASK_APPTYPE_DAEMON_ADAPTIVE &&
1963 task->requested_policy.trp_boosted == 1)
1964 *flagsp |= PROC_FLAG_ADAPTIVE_IMPORTANT;
1965
1966 if (task_is_importance_donor(task))
1967 *flagsp |= PROC_FLAG_IMPORTANCE_DONOR;
1968
1969 if (task->effective_policy.tep_sup_active)
1970 *flagsp |= PROC_FLAG_SUPPRESSED;
1971
1972 return(0);
1973 }
1974
1975 /*
1976 * Tracepoint data... Reading the tracepoint data can be somewhat complicated.
1977 * The current scheme packs as much data into a single tracepoint as it can.
1978 *
1979 * Each task/thread requested/effective structure is 64 bits in size. Any
1980 * given tracepoint will emit either requested or effective data, but not both.
1981 *
1982 * A tracepoint may emit any of task, thread, or task & thread data.
1983 *
1984 * The type of data emitted varies with pointer size. Where possible, both
1985 * task and thread data are emitted. In LP32 systems, the first and second
1986 * halves of either the task or thread data is emitted.
1987 *
1988 * The code uses uintptr_t array indexes instead of high/low to avoid
1989 * confusion WRT big vs little endian.
1990 *
1991 * The truth table for the tracepoint data functions is below, and has the
1992 * following invariants:
1993 *
1994 * 1) task and thread are uintptr_t*
1995 * 2) task may never be NULL
1996 *
1997 *
1998 * LP32 LP64
1999 * trequested_0(task, NULL) task[0] task[0]
2000 * trequested_1(task, NULL) task[1] NULL
2001 * trequested_0(task, thread) thread[0] task[0]
2002 * trequested_1(task, thread) thread[1] thread[0]
2003 *
2004 * Basically, you get a full task or thread on LP32, and both on LP64.
2005 *
2006 * The uintptr_t munging here is squicky enough to deserve a comment.
2007 *
2008 * The variables we are accessing are laid out in memory like this:
2009 *
2010 * [ LP64 uintptr_t 0 ]
2011 * [ LP32 uintptr_t 0 ] [ LP32 uintptr_t 1 ]
2012 *
2013 * 1 2 3 4 5 6 7 8
2014 *
2015 */
2016
2017 static uintptr_t
2018 trequested_0(task_t task)
2019 {
2020 static_assert(sizeof(struct task_requested_policy) == sizeof(uint64_t), "size invariant violated");
2021
2022 uintptr_t* raw = (uintptr_t*)&task->requested_policy;
2023
2024 return raw[0];
2025 }
2026
2027 static uintptr_t
2028 trequested_1(task_t task)
2029 {
2030 #if defined __LP64__
2031 (void)task;
2032 return 0;
2033 #else
2034 uintptr_t* raw = (uintptr_t*)(&task->requested_policy);
2035 return raw[1];
2036 #endif
2037 }
2038
2039 static uintptr_t
2040 teffective_0(task_t task)
2041 {
2042 uintptr_t* raw = (uintptr_t*)&task->effective_policy;
2043
2044 return raw[0];
2045 }
2046
2047 static uintptr_t
2048 teffective_1(task_t task)
2049 {
2050 #if defined __LP64__
2051 (void)task;
2052 return 0;
2053 #else
2054 uintptr_t* raw = (uintptr_t*)(&task->effective_policy);
2055 return raw[1];
2056 #endif
2057 }
2058
2059 /* dump pending for tracepoint */
2060 uint32_t tpending(task_pend_token_t pend_token) { return *(uint32_t*)(void*)(pend_token); }
2061
2062 uint64_t
2063 task_requested_bitfield(task_t task)
2064 {
2065 uint64_t bits = 0;
2066 struct task_requested_policy requested = task->requested_policy;
2067
2068 bits |= (requested.trp_int_darwinbg ? POLICY_REQ_INT_DARWIN_BG : 0);
2069 bits |= (requested.trp_ext_darwinbg ? POLICY_REQ_EXT_DARWIN_BG : 0);
2070 bits |= (requested.trp_int_iotier ? (((uint64_t)requested.trp_int_iotier) << POLICY_REQ_INT_IO_TIER_SHIFT) : 0);
2071 bits |= (requested.trp_ext_iotier ? (((uint64_t)requested.trp_ext_iotier) << POLICY_REQ_EXT_IO_TIER_SHIFT) : 0);
2072 bits |= (requested.trp_int_iopassive ? POLICY_REQ_INT_PASSIVE_IO : 0);
2073 bits |= (requested.trp_ext_iopassive ? POLICY_REQ_EXT_PASSIVE_IO : 0);
2074 bits |= (requested.trp_bg_iotier ? (((uint64_t)requested.trp_bg_iotier) << POLICY_REQ_BG_IOTIER_SHIFT) : 0);
2075 bits |= (requested.trp_terminated ? POLICY_REQ_TERMINATED : 0);
2076
2077 bits |= (requested.trp_boosted ? POLICY_REQ_BOOSTED : 0);
2078 bits |= (requested.trp_tal_enabled ? POLICY_REQ_TAL_ENABLED : 0);
2079 bits |= (requested.trp_apptype ? (((uint64_t)requested.trp_apptype) << POLICY_REQ_APPTYPE_SHIFT) : 0);
2080 bits |= (requested.trp_role ? (((uint64_t)requested.trp_role) << POLICY_REQ_ROLE_SHIFT) : 0);
2081
2082 bits |= (requested.trp_sup_active ? POLICY_REQ_SUP_ACTIVE : 0);
2083 bits |= (requested.trp_sup_lowpri_cpu ? POLICY_REQ_SUP_LOWPRI_CPU : 0);
2084 bits |= (requested.trp_sup_cpu ? POLICY_REQ_SUP_CPU : 0);
2085 bits |= (requested.trp_sup_timer ? (((uint64_t)requested.trp_sup_timer) << POLICY_REQ_SUP_TIMER_THROTTLE_SHIFT) : 0);
2086 bits |= (requested.trp_sup_throughput ? (((uint64_t)requested.trp_sup_throughput) << POLICY_REQ_SUP_THROUGHPUT_SHIFT) : 0);
2087 bits |= (requested.trp_sup_disk ? POLICY_REQ_SUP_DISK_THROTTLE : 0);
2088 bits |= (requested.trp_sup_bg_sockets ? POLICY_REQ_SUP_BG_SOCKETS : 0);
2089
2090 bits |= (requested.trp_base_latency_qos ? (((uint64_t)requested.trp_base_latency_qos) << POLICY_REQ_BASE_LATENCY_QOS_SHIFT) : 0);
2091 bits |= (requested.trp_over_latency_qos ? (((uint64_t)requested.trp_over_latency_qos) << POLICY_REQ_OVER_LATENCY_QOS_SHIFT) : 0);
2092 bits |= (requested.trp_base_through_qos ? (((uint64_t)requested.trp_base_through_qos) << POLICY_REQ_BASE_THROUGH_QOS_SHIFT) : 0);
2093 bits |= (requested.trp_over_through_qos ? (((uint64_t)requested.trp_over_through_qos) << POLICY_REQ_OVER_THROUGH_QOS_SHIFT) : 0);
2094 bits |= (requested.trp_sfi_managed ? POLICY_REQ_SFI_MANAGED : 0);
2095 bits |= (requested.trp_qos_clamp ? (((uint64_t)requested.trp_qos_clamp) << POLICY_REQ_QOS_CLAMP_SHIFT) : 0);
2096
2097 return bits;
2098 }
2099
2100 uint64_t
2101 task_effective_bitfield(task_t task)
2102 {
2103 uint64_t bits = 0;
2104 struct task_effective_policy effective = task->effective_policy;
2105
2106 bits |= (effective.tep_io_tier ? (((uint64_t)effective.tep_io_tier) << POLICY_EFF_IO_TIER_SHIFT) : 0);
2107 bits |= (effective.tep_io_passive ? POLICY_EFF_IO_PASSIVE : 0);
2108 bits |= (effective.tep_darwinbg ? POLICY_EFF_DARWIN_BG : 0);
2109 bits |= (effective.tep_lowpri_cpu ? POLICY_EFF_LOWPRI_CPU : 0);
2110 bits |= (effective.tep_terminated ? POLICY_EFF_TERMINATED : 0);
2111 bits |= (effective.tep_all_sockets_bg ? POLICY_EFF_ALL_SOCKETS_BG : 0);
2112 bits |= (effective.tep_new_sockets_bg ? POLICY_EFF_NEW_SOCKETS_BG : 0);
2113 bits |= (effective.tep_bg_iotier ? (((uint64_t)effective.tep_bg_iotier) << POLICY_EFF_BG_IOTIER_SHIFT) : 0);
2114 bits |= (effective.tep_qos_ui_is_urgent ? POLICY_EFF_QOS_UI_IS_URGENT : 0);
2115
2116 bits |= (effective.tep_tal_engaged ? POLICY_EFF_TAL_ENGAGED : 0);
2117 bits |= (effective.tep_watchers_bg ? POLICY_EFF_WATCHERS_BG : 0);
2118 bits |= (effective.tep_sup_active ? POLICY_EFF_SUP_ACTIVE : 0);
2119 bits |= (effective.tep_suppressed_cpu ? POLICY_EFF_SUP_CPU : 0);
2120 bits |= (effective.tep_role ? (((uint64_t)effective.tep_role) << POLICY_EFF_ROLE_SHIFT) : 0);
2121 bits |= (effective.tep_latency_qos ? (((uint64_t)effective.tep_latency_qos) << POLICY_EFF_LATENCY_QOS_SHIFT) : 0);
2122 bits |= (effective.tep_through_qos ? (((uint64_t)effective.tep_through_qos) << POLICY_EFF_THROUGH_QOS_SHIFT) : 0);
2123 bits |= (effective.tep_sfi_managed ? POLICY_EFF_SFI_MANAGED : 0);
2124 bits |= (effective.tep_qos_ceiling ? (((uint64_t)effective.tep_qos_ceiling) << POLICY_EFF_QOS_CEILING_SHIFT) : 0);
2125
2126 return bits;
2127 }
2128
2129
2130 /*
2131 * Resource usage and CPU related routines
2132 */
2133
2134 int
2135 proc_get_task_ruse_cpu(task_t task, uint32_t *policyp, uint8_t *percentagep, uint64_t *intervalp, uint64_t *deadlinep)
2136 {
2137
2138 int error = 0;
2139 int scope;
2140
2141 task_lock(task);
2142
2143
2144 error = task_get_cpuusage(task, percentagep, intervalp, deadlinep, &scope);
2145 task_unlock(task);
2146
2147 /*
2148 * Reverse-map from CPU resource limit scopes back to policies (see comment below).
2149 */
2150 if (scope == TASK_RUSECPU_FLAGS_PERTHR_LIMIT) {
2151 *policyp = TASK_POLICY_RESOURCE_ATTRIBUTE_NOTIFY_EXC;
2152 } else if (scope == TASK_RUSECPU_FLAGS_PROC_LIMIT) {
2153 *policyp = TASK_POLICY_RESOURCE_ATTRIBUTE_THROTTLE;
2154 } else if (scope == TASK_RUSECPU_FLAGS_DEADLINE) {
2155 *policyp = TASK_POLICY_RESOURCE_ATTRIBUTE_NONE;
2156 }
2157
2158 return(error);
2159 }
2160
2161 /*
2162 * Configure the default CPU usage monitor parameters.
2163 *
2164 * For tasks which have this mechanism activated: if any thread in the
2165 * process consumes more CPU than this, an EXC_RESOURCE exception will be generated.
2166 */
2167 void
2168 proc_init_cpumon_params(void)
2169 {
2170 /*
2171 * The max CPU percentage can be configured via the boot-args and
2172 * a key in the device tree. The boot-args are honored first, then the
2173 * device tree.
2174 */
2175 if (!PE_parse_boot_argn("max_cpumon_percentage", &proc_max_cpumon_percentage,
2176 sizeof (proc_max_cpumon_percentage)))
2177 {
2178 uint64_t max_percentage = 0ULL;
2179
2180 if (!PE_get_default("kern.max_cpumon_percentage", &max_percentage,
2181 sizeof(max_percentage)))
2182 {
2183 max_percentage = DEFAULT_CPUMON_PERCENTAGE;
2184 }
2185
2186 assert(max_percentage <= UINT8_MAX);
2187 proc_max_cpumon_percentage = (uint8_t) max_percentage;
2188 }
2189
2190 if (proc_max_cpumon_percentage > 100) {
2191 proc_max_cpumon_percentage = 100;
2192 }
2193
2194 /*
2195 * The interval should be specified in seconds.
2196 *
2197 * Like the max CPU percentage, the max CPU interval can be configured
2198 * via boot-args and the device tree.
2199 */
2200 if (!PE_parse_boot_argn("max_cpumon_interval", &proc_max_cpumon_interval,
2201 sizeof (proc_max_cpumon_interval)))
2202 {
2203 if (!PE_get_default("kern.max_cpumon_interval", &proc_max_cpumon_interval,
2204 sizeof(proc_max_cpumon_interval)))
2205 {
2206 proc_max_cpumon_interval = DEFAULT_CPUMON_INTERVAL;
2207 }
2208 }
2209
2210 proc_max_cpumon_interval *= NSEC_PER_SEC;
2211
2212 /* TEMPORARY boot arg to control App suppression */
2213 PE_parse_boot_argn("task_policy_suppression_disable",
2214 &task_policy_suppression_disable,
2215 sizeof(task_policy_suppression_disable));
2216 }
2217
2218 /*
2219 * Currently supported configurations for CPU limits.
2220 *
2221 * Policy | Deadline-based CPU limit | Percentage-based CPU limit
2222 * -------------------------------------+--------------------------+------------------------------
2223 * PROC_POLICY_RSRCACT_THROTTLE | ENOTSUP | Task-wide scope only
2224 * PROC_POLICY_RSRCACT_SUSPEND | Task-wide scope only | ENOTSUP
2225 * PROC_POLICY_RSRCACT_TERMINATE | Task-wide scope only | ENOTSUP
2226 * PROC_POLICY_RSRCACT_NOTIFY_KQ | Task-wide scope only | ENOTSUP
2227 * PROC_POLICY_RSRCACT_NOTIFY_EXC | ENOTSUP | Per-thread scope only
2228 *
2229 * A deadline-based CPU limit is actually a simple wallclock timer - the requested action is performed
2230 * after the specified amount of wallclock time has elapsed.
2231 *
2232 * A percentage-based CPU limit performs the requested action after the specified amount of actual CPU time
2233 * has been consumed -- regardless of how much wallclock time has elapsed -- by either the task as an
2234 * aggregate entity (so-called "Task-wide" or "Proc-wide" scope, whereby the CPU time consumed by all threads
2235 * in the task are added together), or by any one thread in the task (so-called "per-thread" scope).
2236 *
2237 * We support either deadline != 0 OR percentage != 0, but not both. The original intention in having them
2238 * share an API was to use actual CPU time as the basis of the deadline-based limit (as in: perform an action
2239 * after I have used some amount of CPU time; this is different than the recurring percentage/interval model)
2240 * but the potential consumer of the API at the time was insisting on wallclock time instead.
2241 *
2242 * Currently, requesting notification via an exception is the only way to get per-thread scope for a
2243 * CPU limit. All other types of notifications force task-wide scope for the limit.
2244 */
2245 int
2246 proc_set_task_ruse_cpu(task_t task, uint32_t policy, uint8_t percentage, uint64_t interval, uint64_t deadline,
2247 int cpumon_entitled)
2248 {
2249 int error = 0;
2250 int scope;
2251
2252 /*
2253 * Enforce the matrix of supported configurations for policy, percentage, and deadline.
2254 */
2255 switch (policy) {
2256 // If no policy is explicitly given, the default is to throttle.
2257 case TASK_POLICY_RESOURCE_ATTRIBUTE_NONE:
2258 case TASK_POLICY_RESOURCE_ATTRIBUTE_THROTTLE:
2259 if (deadline != 0)
2260 return (ENOTSUP);
2261 scope = TASK_RUSECPU_FLAGS_PROC_LIMIT;
2262 break;
2263 case TASK_POLICY_RESOURCE_ATTRIBUTE_SUSPEND:
2264 case TASK_POLICY_RESOURCE_ATTRIBUTE_TERMINATE:
2265 case TASK_POLICY_RESOURCE_ATTRIBUTE_NOTIFY_KQ:
2266 if (percentage != 0)
2267 return (ENOTSUP);
2268 scope = TASK_RUSECPU_FLAGS_DEADLINE;
2269 break;
2270 case TASK_POLICY_RESOURCE_ATTRIBUTE_NOTIFY_EXC:
2271 if (deadline != 0)
2272 return (ENOTSUP);
2273 scope = TASK_RUSECPU_FLAGS_PERTHR_LIMIT;
2274 #ifdef CONFIG_NOMONITORS
2275 return (error);
2276 #endif /* CONFIG_NOMONITORS */
2277 break;
2278 default:
2279 return (EINVAL);
2280 }
2281
2282 task_lock(task);
2283 if (task != current_task()) {
2284 task->policy_ru_cpu_ext = policy;
2285 } else {
2286 task->policy_ru_cpu = policy;
2287 }
2288 error = task_set_cpuusage(task, percentage, interval, deadline, scope, cpumon_entitled);
2289 task_unlock(task);
2290 return(error);
2291 }
2292
2293 /* TODO: get rid of these */
2294 #define TASK_POLICY_CPU_RESOURCE_USAGE 0
2295 #define TASK_POLICY_WIREDMEM_RESOURCE_USAGE 1
2296 #define TASK_POLICY_VIRTUALMEM_RESOURCE_USAGE 2
2297 #define TASK_POLICY_DISK_RESOURCE_USAGE 3
2298 #define TASK_POLICY_NETWORK_RESOURCE_USAGE 4
2299 #define TASK_POLICY_POWER_RESOURCE_USAGE 5
2300
2301 #define TASK_POLICY_RESOURCE_USAGE_COUNT 6
2302
2303 int
2304 proc_clear_task_ruse_cpu(task_t task, int cpumon_entitled)
2305 {
2306 int error = 0;
2307 int action;
2308 void * bsdinfo = NULL;
2309
2310 task_lock(task);
2311 if (task != current_task()) {
2312 task->policy_ru_cpu_ext = TASK_POLICY_RESOURCE_ATTRIBUTE_DEFAULT;
2313 } else {
2314 task->policy_ru_cpu = TASK_POLICY_RESOURCE_ATTRIBUTE_DEFAULT;
2315 }
2316
2317 error = task_clear_cpuusage_locked(task, cpumon_entitled);
2318 if (error != 0)
2319 goto out;
2320
2321 action = task->applied_ru_cpu;
2322 if (task->applied_ru_cpu_ext != TASK_POLICY_RESOURCE_ATTRIBUTE_NONE) {
2323 /* reset action */
2324 task->applied_ru_cpu_ext = TASK_POLICY_RESOURCE_ATTRIBUTE_NONE;
2325 }
2326 if (action != TASK_POLICY_RESOURCE_ATTRIBUTE_NONE) {
2327 bsdinfo = task->bsd_info;
2328 task_unlock(task);
2329 proc_restore_resource_actions(bsdinfo, TASK_POLICY_CPU_RESOURCE_USAGE, action);
2330 goto out1;
2331 }
2332
2333 out:
2334 task_unlock(task);
2335 out1:
2336 return(error);
2337
2338 }
2339
2340 /* used to apply resource limit related actions */
2341 static int
2342 task_apply_resource_actions(task_t task, int type)
2343 {
2344 int action = TASK_POLICY_RESOURCE_ATTRIBUTE_NONE;
2345 void * bsdinfo = NULL;
2346
2347 switch (type) {
2348 case TASK_POLICY_CPU_RESOURCE_USAGE:
2349 break;
2350 case TASK_POLICY_WIREDMEM_RESOURCE_USAGE:
2351 case TASK_POLICY_VIRTUALMEM_RESOURCE_USAGE:
2352 case TASK_POLICY_DISK_RESOURCE_USAGE:
2353 case TASK_POLICY_NETWORK_RESOURCE_USAGE:
2354 case TASK_POLICY_POWER_RESOURCE_USAGE:
2355 return(0);
2356
2357 default:
2358 return(1);
2359 };
2360
2361 /* only cpu actions for now */
2362 task_lock(task);
2363
2364 if (task->applied_ru_cpu_ext == TASK_POLICY_RESOURCE_ATTRIBUTE_NONE) {
2365 /* apply action */
2366 task->applied_ru_cpu_ext = task->policy_ru_cpu_ext;
2367 action = task->applied_ru_cpu_ext;
2368 } else {
2369 action = task->applied_ru_cpu_ext;
2370 }
2371
2372 if (action != TASK_POLICY_RESOURCE_ATTRIBUTE_NONE) {
2373 bsdinfo = task->bsd_info;
2374 task_unlock(task);
2375 proc_apply_resource_actions(bsdinfo, TASK_POLICY_CPU_RESOURCE_USAGE, action);
2376 } else
2377 task_unlock(task);
2378
2379 return(0);
2380 }
2381
2382 /*
2383 * XXX This API is somewhat broken; we support multiple simultaneous CPU limits, but the get/set API
2384 * only allows for one at a time. This means that if there is a per-thread limit active, the other
2385 * "scopes" will not be accessible via this API. We could change it to pass in the scope of interest
2386 * to the caller, and prefer that, but there's no need for that at the moment.
2387 */
2388 static int
2389 task_get_cpuusage(task_t task, uint8_t *percentagep, uint64_t *intervalp, uint64_t *deadlinep, int *scope)
2390 {
2391 *percentagep = 0;
2392 *intervalp = 0;
2393 *deadlinep = 0;
2394
2395 if ((task->rusage_cpu_flags & TASK_RUSECPU_FLAGS_PERTHR_LIMIT) != 0) {
2396 *scope = TASK_RUSECPU_FLAGS_PERTHR_LIMIT;
2397 *percentagep = task->rusage_cpu_perthr_percentage;
2398 *intervalp = task->rusage_cpu_perthr_interval;
2399 } else if ((task->rusage_cpu_flags & TASK_RUSECPU_FLAGS_PROC_LIMIT) != 0) {
2400 *scope = TASK_RUSECPU_FLAGS_PROC_LIMIT;
2401 *percentagep = task->rusage_cpu_percentage;
2402 *intervalp = task->rusage_cpu_interval;
2403 } else if ((task->rusage_cpu_flags & TASK_RUSECPU_FLAGS_DEADLINE) != 0) {
2404 *scope = TASK_RUSECPU_FLAGS_DEADLINE;
2405 *deadlinep = task->rusage_cpu_deadline;
2406 } else {
2407 *scope = 0;
2408 }
2409
2410 return(0);
2411 }
2412
2413 /*
2414 * Suspend the CPU usage monitor for the task. Return value indicates
2415 * if the mechanism was actually enabled.
2416 */
2417 int
2418 task_suspend_cpumon(task_t task)
2419 {
2420 thread_t thread;
2421
2422 task_lock_assert_owned(task);
2423
2424 if ((task->rusage_cpu_flags & TASK_RUSECPU_FLAGS_PERTHR_LIMIT) == 0) {
2425 return KERN_INVALID_ARGUMENT;
2426 }
2427
2428 #if CONFIG_TELEMETRY
2429 /*
2430 * Disable task-wide telemetry if it was ever enabled by the CPU usage
2431 * monitor's warning zone.
2432 */
2433 telemetry_task_ctl_locked(task, TF_CPUMON_WARNING, 0);
2434 #endif
2435
2436 /*
2437 * Suspend monitoring for the task, and propagate that change to each thread.
2438 */
2439 task->rusage_cpu_flags &= ~(TASK_RUSECPU_FLAGS_PERTHR_LIMIT | TASK_RUSECPU_FLAGS_FATAL_CPUMON);
2440 queue_iterate(&task->threads, thread, thread_t, task_threads) {
2441 set_astledger(thread);
2442 }
2443
2444 return KERN_SUCCESS;
2445 }
2446
2447 /*
2448 * Remove all traces of the CPU monitor.
2449 */
2450 int
2451 task_disable_cpumon(task_t task)
2452 {
2453 int kret;
2454
2455 task_lock_assert_owned(task);
2456
2457 kret = task_suspend_cpumon(task);
2458 if (kret) return kret;
2459
2460 /* Once we clear these values, the monitor can't be resumed */
2461 task->rusage_cpu_perthr_percentage = 0;
2462 task->rusage_cpu_perthr_interval = 0;
2463
2464 return (KERN_SUCCESS);
2465 }
2466
2467
2468 static int
2469 task_enable_cpumon_locked(task_t task)
2470 {
2471 thread_t thread;
2472 task_lock_assert_owned(task);
2473
2474 if (task->rusage_cpu_perthr_percentage == 0 ||
2475 task->rusage_cpu_perthr_interval == 0) {
2476 return KERN_INVALID_ARGUMENT;
2477 }
2478
2479 task->rusage_cpu_flags |= TASK_RUSECPU_FLAGS_PERTHR_LIMIT;
2480 queue_iterate(&task->threads, thread, thread_t, task_threads) {
2481 set_astledger(thread);
2482 }
2483
2484 return KERN_SUCCESS;
2485 }
2486
2487 int
2488 task_resume_cpumon(task_t task)
2489 {
2490 kern_return_t kret;
2491
2492 if (!task) {
2493 return EINVAL;
2494 }
2495
2496 task_lock(task);
2497 kret = task_enable_cpumon_locked(task);
2498 task_unlock(task);
2499
2500 return kret;
2501 }
2502
2503
2504 /* duplicate values from bsd/sys/process_policy.h */
2505 #define PROC_POLICY_CPUMON_DISABLE 0xFF
2506 #define PROC_POLICY_CPUMON_DEFAULTS 0xFE
2507
2508 static int
2509 task_set_cpuusage(task_t task, uint8_t percentage, uint64_t interval, uint64_t deadline, int scope, int cpumon_entitled)
2510 {
2511 uint64_t abstime = 0;
2512 uint64_t limittime = 0;
2513
2514 lck_mtx_assert(&task->lock, LCK_MTX_ASSERT_OWNED);
2515
2516 /* By default, refill once per second */
2517 if (interval == 0)
2518 interval = NSEC_PER_SEC;
2519
2520 if (percentage != 0) {
2521 if (scope == TASK_RUSECPU_FLAGS_PERTHR_LIMIT) {
2522 boolean_t warn = FALSE;
2523
2524 /*
2525 * A per-thread CPU limit on a task generates an exception
2526 * (LEDGER_ACTION_EXCEPTION) if any one thread in the task
2527 * exceeds the limit.
2528 */
2529
2530 if (percentage == PROC_POLICY_CPUMON_DISABLE) {
2531 if (cpumon_entitled) {
2532 /* 25095698 - task_disable_cpumon() should be reliable */
2533 task_disable_cpumon(task);
2534 return 0;
2535 }
2536
2537 /*
2538 * This task wishes to disable the CPU usage monitor, but it's
2539 * missing the required entitlement:
2540 * com.apple.private.kernel.override-cpumon
2541 *
2542 * Instead, treat this as a request to reset its params
2543 * back to the defaults.
2544 */
2545 warn = TRUE;
2546 percentage = PROC_POLICY_CPUMON_DEFAULTS;
2547 }
2548
2549 if (percentage == PROC_POLICY_CPUMON_DEFAULTS) {
2550 percentage = proc_max_cpumon_percentage;
2551 interval = proc_max_cpumon_interval;
2552 }
2553
2554 if (percentage > 100) {
2555 percentage = 100;
2556 }
2557
2558 /*
2559 * Passing in an interval of -1 means either:
2560 * - Leave the interval as-is, if there's already a per-thread
2561 * limit configured
2562 * - Use the system default.
2563 */
2564 if (interval == -1ULL) {
2565 if (task->rusage_cpu_flags & TASK_RUSECPU_FLAGS_PERTHR_LIMIT) {
2566 interval = task->rusage_cpu_perthr_interval;
2567 } else {
2568 interval = proc_max_cpumon_interval;
2569 }
2570 }
2571
2572 /*
2573 * Enforce global caps on CPU usage monitor here if the process is not
2574 * entitled to escape the global caps.
2575 */
2576 if ((percentage > proc_max_cpumon_percentage) && (cpumon_entitled == 0)) {
2577 warn = TRUE;
2578 percentage = proc_max_cpumon_percentage;
2579 }
2580
2581 if ((interval > proc_max_cpumon_interval) && (cpumon_entitled == 0)) {
2582 warn = TRUE;
2583 interval = proc_max_cpumon_interval;
2584 }
2585
2586 if (warn) {
2587 int pid = 0;
2588 const char *procname = "unknown";
2589
2590 #ifdef MACH_BSD
2591 pid = proc_selfpid();
2592 if (current_task()->bsd_info != NULL) {
2593 procname = proc_name_address(current_task()->bsd_info);
2594 }
2595 #endif
2596
2597 printf("process %s[%d] denied attempt to escape CPU monitor"
2598 " (missing required entitlement).\n", procname, pid);
2599 }
2600
2601 /* configure the limit values */
2602 task->rusage_cpu_perthr_percentage = percentage;
2603 task->rusage_cpu_perthr_interval = interval;
2604
2605 /* and enable the CPU monitor */
2606 (void)task_enable_cpumon_locked(task);
2607 } else if (scope == TASK_RUSECPU_FLAGS_PROC_LIMIT) {
2608 /*
2609 * Currently, a proc-wide CPU limit always blocks if the limit is
2610 * exceeded (LEDGER_ACTION_BLOCK).
2611 */
2612 task->rusage_cpu_flags |= TASK_RUSECPU_FLAGS_PROC_LIMIT;
2613 task->rusage_cpu_percentage = percentage;
2614 task->rusage_cpu_interval = interval;
2615
2616 limittime = (interval * percentage) / 100;
2617 nanoseconds_to_absolutetime(limittime, &abstime);
2618
2619 ledger_set_limit(task->ledger, task_ledgers.cpu_time, abstime, 0);
2620 ledger_set_period(task->ledger, task_ledgers.cpu_time, interval);
2621 ledger_set_action(task->ledger, task_ledgers.cpu_time, LEDGER_ACTION_BLOCK);
2622 }
2623 }
2624
2625 if (deadline != 0) {
2626 assert(scope == TASK_RUSECPU_FLAGS_DEADLINE);
2627
2628 /* if already in use, cancel and wait for it to cleanout */
2629 if (task->rusage_cpu_callt != NULL) {
2630 task_unlock(task);
2631 thread_call_cancel_wait(task->rusage_cpu_callt);
2632 task_lock(task);
2633 }
2634 if (task->rusage_cpu_callt == NULL) {
2635 task->rusage_cpu_callt = thread_call_allocate_with_priority(task_action_cpuusage, (thread_call_param_t)task, THREAD_CALL_PRIORITY_KERNEL);
2636 }
2637 /* setup callout */
2638 if (task->rusage_cpu_callt != 0) {
2639 uint64_t save_abstime = 0;
2640
2641 task->rusage_cpu_flags |= TASK_RUSECPU_FLAGS_DEADLINE;
2642 task->rusage_cpu_deadline = deadline;
2643
2644 nanoseconds_to_absolutetime(deadline, &abstime);
2645 save_abstime = abstime;
2646 clock_absolutetime_interval_to_deadline(save_abstime, &abstime);
2647 thread_call_enter_delayed(task->rusage_cpu_callt, abstime);
2648 }
2649 }
2650
2651 return(0);
2652 }
2653
2654 int
2655 task_clear_cpuusage(task_t task, int cpumon_entitled)
2656 {
2657 int retval = 0;
2658
2659 task_lock(task);
2660 retval = task_clear_cpuusage_locked(task, cpumon_entitled);
2661 task_unlock(task);
2662
2663 return(retval);
2664 }
2665
2666 static int
2667 task_clear_cpuusage_locked(task_t task, int cpumon_entitled)
2668 {
2669 thread_call_t savecallt;
2670
2671 /* cancel percentage handling if set */
2672 if (task->rusage_cpu_flags & TASK_RUSECPU_FLAGS_PROC_LIMIT) {
2673 task->rusage_cpu_flags &= ~TASK_RUSECPU_FLAGS_PROC_LIMIT;
2674 ledger_set_limit(task->ledger, task_ledgers.cpu_time, LEDGER_LIMIT_INFINITY, 0);
2675 task->rusage_cpu_percentage = 0;
2676 task->rusage_cpu_interval = 0;
2677 }
2678
2679 /*
2680 * Disable the CPU usage monitor.
2681 */
2682 if (cpumon_entitled) {
2683 task_disable_cpumon(task);
2684 }
2685
2686 /* cancel deadline handling if set */
2687 if (task->rusage_cpu_flags & TASK_RUSECPU_FLAGS_DEADLINE) {
2688 task->rusage_cpu_flags &= ~TASK_RUSECPU_FLAGS_DEADLINE;
2689 if (task->rusage_cpu_callt != 0) {
2690 savecallt = task->rusage_cpu_callt;
2691 task->rusage_cpu_callt = NULL;
2692 task->rusage_cpu_deadline = 0;
2693 task_unlock(task);
2694 thread_call_cancel_wait(savecallt);
2695 thread_call_free(savecallt);
2696 task_lock(task);
2697 }
2698 }
2699 return(0);
2700 }
2701
2702 /* called by ledger unit to enforce action due to resource usage criteria being met */
2703 static void
2704 task_action_cpuusage(thread_call_param_t param0, __unused thread_call_param_t param1)
2705 {
2706 task_t task = (task_t)param0;
2707 (void)task_apply_resource_actions(task, TASK_POLICY_CPU_RESOURCE_USAGE);
2708 return;
2709 }
2710
2711
2712 /*
2713 * Routines for taskwatch and pidbind
2714 */
2715
2716
2717 /*
2718 * Routines for importance donation/inheritance/boosting
2719 */
2720
2721 static void
2722 task_importance_update_live_donor(task_t target_task)
2723 {
2724 #if IMPORTANCE_INHERITANCE
2725
2726 ipc_importance_task_t task_imp;
2727
2728 task_imp = ipc_importance_for_task(target_task, FALSE);
2729 if (IIT_NULL != task_imp) {
2730 ipc_importance_task_update_live_donor(task_imp);
2731 ipc_importance_task_release(task_imp);
2732 }
2733 #endif /* IMPORTANCE_INHERITANCE */
2734 }
2735
2736 void
2737 task_importance_mark_donor(task_t task, boolean_t donating)
2738 {
2739 #if IMPORTANCE_INHERITANCE
2740 ipc_importance_task_t task_imp;
2741
2742 task_imp = ipc_importance_for_task(task, FALSE);
2743 if (IIT_NULL != task_imp) {
2744 ipc_importance_task_mark_donor(task_imp, donating);
2745 ipc_importance_task_release(task_imp);
2746 }
2747 #endif /* IMPORTANCE_INHERITANCE */
2748 }
2749
2750 void
2751 task_importance_mark_live_donor(task_t task, boolean_t live_donating)
2752 {
2753 #if IMPORTANCE_INHERITANCE
2754 ipc_importance_task_t task_imp;
2755
2756 task_imp = ipc_importance_for_task(task, FALSE);
2757 if (IIT_NULL != task_imp) {
2758 ipc_importance_task_mark_live_donor(task_imp, live_donating);
2759 ipc_importance_task_release(task_imp);
2760 }
2761 #endif /* IMPORTANCE_INHERITANCE */
2762 }
2763
2764 void
2765 task_importance_mark_receiver(task_t task, boolean_t receiving)
2766 {
2767 #if IMPORTANCE_INHERITANCE
2768 ipc_importance_task_t task_imp;
2769
2770 task_imp = ipc_importance_for_task(task, FALSE);
2771 if (IIT_NULL != task_imp) {
2772 ipc_importance_task_mark_receiver(task_imp, receiving);
2773 ipc_importance_task_release(task_imp);
2774 }
2775 #endif /* IMPORTANCE_INHERITANCE */
2776 }
2777
2778 void
2779 task_importance_mark_denap_receiver(task_t task, boolean_t denap)
2780 {
2781 #if IMPORTANCE_INHERITANCE
2782 ipc_importance_task_t task_imp;
2783
2784 task_imp = ipc_importance_for_task(task, FALSE);
2785 if (IIT_NULL != task_imp) {
2786 ipc_importance_task_mark_denap_receiver(task_imp, denap);
2787 ipc_importance_task_release(task_imp);
2788 }
2789 #endif /* IMPORTANCE_INHERITANCE */
2790 }
2791
2792 void
2793 task_importance_reset(__imp_only task_t task)
2794 {
2795 #if IMPORTANCE_INHERITANCE
2796 ipc_importance_task_t task_imp;
2797
2798 /* TODO: Lower importance downstream before disconnect */
2799 task_imp = task->task_imp_base;
2800 ipc_importance_reset(task_imp, FALSE);
2801 task_importance_update_live_donor(task);
2802 #endif /* IMPORTANCE_INHERITANCE */
2803 }
2804
2805 #if IMPORTANCE_INHERITANCE
2806
2807 /*
2808 * Sets the task boost bit to the provided value. Does NOT run the update function.
2809 *
2810 * Task lock must be held.
2811 */
2812 static void
2813 task_set_boost_locked(task_t task, boolean_t boost_active)
2814 {
2815 #if IMPORTANCE_DEBUG
2816 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, (IMPORTANCE_CODE(IMP_BOOST, (boost_active ? IMP_BOOSTED : IMP_UNBOOSTED)) | DBG_FUNC_START),
2817 proc_selfpid(), task_pid(task), trequested_0(task), trequested_1(task), 0);
2818 #endif
2819
2820 task->requested_policy.trp_boosted = boost_active;
2821
2822 #if IMPORTANCE_DEBUG
2823 if (boost_active == TRUE){
2824 DTRACE_BOOST2(boost, task_t, task, int, task_pid(task));
2825 } else {
2826 DTRACE_BOOST2(unboost, task_t, task, int, task_pid(task));
2827 }
2828 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, (IMPORTANCE_CODE(IMP_BOOST, (boost_active ? IMP_BOOSTED : IMP_UNBOOSTED)) | DBG_FUNC_END),
2829 proc_selfpid(), task_pid(task),
2830 trequested_0(task), trequested_1(task), 0);
2831 #endif
2832 }
2833
2834 /*
2835 * Sets the task boost bit to the provided value and applies the update.
2836 *
2837 * Task lock must be held. Must call update complete after unlocking the task.
2838 */
2839 void
2840 task_update_boost_locked(task_t task, boolean_t boost_active, task_pend_token_t pend_token)
2841 {
2842 task_set_boost_locked(task, boost_active);
2843
2844 task_policy_update_locked(task, pend_token);
2845 }
2846
2847 /*
2848 * Check if this task should donate importance.
2849 *
2850 * May be called without taking the task lock. In that case, donor status can change
2851 * so you must check only once for each donation event.
2852 */
2853 boolean_t
2854 task_is_importance_donor(task_t task)
2855 {
2856 if (task->task_imp_base == IIT_NULL)
2857 return FALSE;
2858 return ipc_importance_task_is_donor(task->task_imp_base);
2859 }
2860
2861 /*
2862 * Query the status of the task's donor mark.
2863 */
2864 boolean_t
2865 task_is_marked_importance_donor(task_t task)
2866 {
2867 if (task->task_imp_base == IIT_NULL)
2868 return FALSE;
2869 return ipc_importance_task_is_marked_donor(task->task_imp_base);
2870 }
2871
2872 /*
2873 * Query the status of the task's live donor and donor mark.
2874 */
2875 boolean_t
2876 task_is_marked_live_importance_donor(task_t task)
2877 {
2878 if (task->task_imp_base == IIT_NULL)
2879 return FALSE;
2880 return ipc_importance_task_is_marked_live_donor(task->task_imp_base);
2881 }
2882
2883
2884 /*
2885 * This routine may be called without holding task lock
2886 * since the value of imp_receiver can never be unset.
2887 */
2888 boolean_t
2889 task_is_importance_receiver(task_t task)
2890 {
2891 if (task->task_imp_base == IIT_NULL)
2892 return FALSE;
2893 return ipc_importance_task_is_marked_receiver(task->task_imp_base);
2894 }
2895
2896 /*
2897 * Query the task's receiver mark.
2898 */
2899 boolean_t
2900 task_is_marked_importance_receiver(task_t task)
2901 {
2902 if (task->task_imp_base == IIT_NULL)
2903 return FALSE;
2904 return ipc_importance_task_is_marked_receiver(task->task_imp_base);
2905 }
2906
2907 /*
2908 * This routine may be called without holding task lock
2909 * since the value of de-nap receiver can never be unset.
2910 */
2911 boolean_t
2912 task_is_importance_denap_receiver(task_t task)
2913 {
2914 if (task->task_imp_base == IIT_NULL)
2915 return FALSE;
2916 return ipc_importance_task_is_denap_receiver(task->task_imp_base);
2917 }
2918
2919 /*
2920 * Query the task's de-nap receiver mark.
2921 */
2922 boolean_t
2923 task_is_marked_importance_denap_receiver(task_t task)
2924 {
2925 if (task->task_imp_base == IIT_NULL)
2926 return FALSE;
2927 return ipc_importance_task_is_marked_denap_receiver(task->task_imp_base);
2928 }
2929
2930 /*
2931 * This routine may be called without holding task lock
2932 * since the value of imp_receiver can never be unset.
2933 */
2934 boolean_t
2935 task_is_importance_receiver_type(task_t task)
2936 {
2937 if (task->task_imp_base == IIT_NULL)
2938 return FALSE;
2939 return (task_is_importance_receiver(task) ||
2940 task_is_importance_denap_receiver(task));
2941 }
2942
2943 /*
2944 * External importance assertions are managed by the process in userspace
2945 * Internal importance assertions are the responsibility of the kernel
2946 * Assertions are changed from internal to external via task_importance_externalize_assertion
2947 */
2948
2949 int
2950 task_importance_hold_internal_assertion(task_t target_task, uint32_t count)
2951 {
2952 ipc_importance_task_t task_imp;
2953 kern_return_t ret;
2954
2955 /* may be first time, so allow for possible importance setup */
2956 task_imp = ipc_importance_for_task(target_task, FALSE);
2957 if (IIT_NULL == task_imp) {
2958 return EOVERFLOW;
2959 }
2960 ret = ipc_importance_task_hold_internal_assertion(task_imp, count);
2961 ipc_importance_task_release(task_imp);
2962
2963 return (KERN_SUCCESS != ret) ? ENOTSUP : 0;
2964 }
2965
2966 int
2967 task_importance_hold_file_lock_assertion(task_t target_task, uint32_t count)
2968 {
2969 ipc_importance_task_t task_imp;
2970 kern_return_t ret;
2971
2972 /* may be first time, so allow for possible importance setup */
2973 task_imp = ipc_importance_for_task(target_task, FALSE);
2974 if (IIT_NULL == task_imp) {
2975 return EOVERFLOW;
2976 }
2977 ret = ipc_importance_task_hold_file_lock_assertion(task_imp, count);
2978 ipc_importance_task_release(task_imp);
2979
2980 return (KERN_SUCCESS != ret) ? ENOTSUP : 0;
2981 }
2982
2983 int
2984 task_importance_hold_legacy_external_assertion(task_t target_task, uint32_t count)
2985 {
2986 ipc_importance_task_t task_imp;
2987 kern_return_t ret;
2988
2989 /* must already have set up an importance */
2990 task_imp = target_task->task_imp_base;
2991 if (IIT_NULL == task_imp) {
2992 return EOVERFLOW;
2993 }
2994 ret = ipc_importance_task_hold_legacy_external_assertion(task_imp, count);
2995 return (KERN_SUCCESS != ret) ? ENOTSUP : 0;
2996 }
2997
2998 int
2999 task_importance_drop_file_lock_assertion(task_t target_task, uint32_t count)
3000 {
3001 ipc_importance_task_t task_imp;
3002 kern_return_t ret;
3003
3004 /* must already have set up an importance */
3005 task_imp = target_task->task_imp_base;
3006 if (IIT_NULL == task_imp) {
3007 return EOVERFLOW;
3008 }
3009 ret = ipc_importance_task_drop_file_lock_assertion(target_task->task_imp_base, count);
3010 return (KERN_SUCCESS != ret) ? EOVERFLOW : 0;
3011 }
3012
3013 int
3014 task_importance_drop_legacy_external_assertion(task_t target_task, uint32_t count)
3015 {
3016 ipc_importance_task_t task_imp;
3017 kern_return_t ret;
3018
3019 /* must already have set up an importance */
3020 task_imp = target_task->task_imp_base;
3021 if (IIT_NULL == task_imp) {
3022 return EOVERFLOW;
3023 }
3024 ret = ipc_importance_task_drop_legacy_external_assertion(task_imp, count);
3025 return (KERN_SUCCESS != ret) ? EOVERFLOW : 0;
3026 }
3027
3028 static void
3029 task_add_importance_watchport(task_t task, mach_port_t port, int *boostp)
3030 {
3031 int boost = 0;
3032
3033 __impdebug_only int released_pid = 0;
3034 __impdebug_only int pid = task_pid(task);
3035
3036 ipc_importance_task_t release_imp_task = IIT_NULL;
3037
3038 if (IP_VALID(port) != 0) {
3039 ipc_importance_task_t new_imp_task = ipc_importance_for_task(task, FALSE);
3040
3041 ip_lock(port);
3042
3043 /*
3044 * The port must have been marked tempowner already.
3045 * This also filters out ports whose receive rights
3046 * are already enqueued in a message, as you can't
3047 * change the right's destination once it's already
3048 * on its way.
3049 */
3050 if (port->ip_tempowner != 0) {
3051 assert(port->ip_impdonation != 0);
3052
3053 boost = port->ip_impcount;
3054 if (IIT_NULL != port->ip_imp_task) {
3055 /*
3056 * if this port is already bound to a task,
3057 * release the task reference and drop any
3058 * watchport-forwarded boosts
3059 */
3060 release_imp_task = port->ip_imp_task;
3061 port->ip_imp_task = IIT_NULL;
3062 }
3063
3064 /* mark the port is watching another task (reference held in port->ip_imp_task) */
3065 if (ipc_importance_task_is_marked_receiver(new_imp_task)) {
3066 port->ip_imp_task = new_imp_task;
3067 new_imp_task = IIT_NULL;
3068 }
3069 }
3070 ip_unlock(port);
3071
3072 if (IIT_NULL != new_imp_task) {
3073 ipc_importance_task_release(new_imp_task);
3074 }
3075
3076 if (IIT_NULL != release_imp_task) {
3077 if (boost > 0)
3078 ipc_importance_task_drop_internal_assertion(release_imp_task, boost);
3079
3080 // released_pid = task_pid(release_imp_task); /* TODO: Need ref-safe way to get pid */
3081 ipc_importance_task_release(release_imp_task);
3082 }
3083 #if IMPORTANCE_DEBUG
3084 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, (IMPORTANCE_CODE(IMP_WATCHPORT, 0)) | DBG_FUNC_NONE,
3085 proc_selfpid(), pid, boost, released_pid, 0);
3086 #endif /* IMPORTANCE_DEBUG */
3087 }
3088
3089 *boostp = boost;
3090 return;
3091 }
3092
3093 #endif /* IMPORTANCE_INHERITANCE */
3094
3095 /*
3096 * Routines for VM to query task importance
3097 */
3098
3099
3100 /*
3101 * Order to be considered while estimating importance
3102 * for low memory notification and purging purgeable memory.
3103 */
3104 #define TASK_IMPORTANCE_FOREGROUND 4
3105 #define TASK_IMPORTANCE_NOTDARWINBG 1
3106
3107
3108 /*
3109 * (Un)Mark the task as a privileged listener for memory notifications.
3110 * if marked, this task will be among the first to be notified amongst
3111 * the bulk of all other tasks when the system enters a pressure level
3112 * of interest to this task.
3113 */
3114 int
3115 task_low_mem_privileged_listener(task_t task, boolean_t new_value, boolean_t *old_value)
3116 {
3117 if (old_value != NULL) {
3118 *old_value = (boolean_t)task->low_mem_privileged_listener;
3119 } else {
3120 task_lock(task);
3121 task->low_mem_privileged_listener = (uint32_t)new_value;
3122 task_unlock(task);
3123 }
3124
3125 return 0;
3126 }
3127
3128 /*
3129 * Checks if the task is already notified.
3130 *
3131 * Condition: task lock should be held while calling this function.
3132 */
3133 boolean_t
3134 task_has_been_notified(task_t task, int pressurelevel)
3135 {
3136 if (task == NULL) {
3137 return FALSE;
3138 }
3139
3140 if (pressurelevel == kVMPressureWarning)
3141 return (task->low_mem_notified_warn ? TRUE : FALSE);
3142 else if (pressurelevel == kVMPressureCritical)
3143 return (task->low_mem_notified_critical ? TRUE : FALSE);
3144 else
3145 return TRUE;
3146 }
3147
3148
3149 /*
3150 * Checks if the task is used for purging.
3151 *
3152 * Condition: task lock should be held while calling this function.
3153 */
3154 boolean_t
3155 task_used_for_purging(task_t task, int pressurelevel)
3156 {
3157 if (task == NULL) {
3158 return FALSE;
3159 }
3160
3161 if (pressurelevel == kVMPressureWarning)
3162 return (task->purged_memory_warn ? TRUE : FALSE);
3163 else if (pressurelevel == kVMPressureCritical)
3164 return (task->purged_memory_critical ? TRUE : FALSE);
3165 else
3166 return TRUE;
3167 }
3168
3169
3170 /*
3171 * Mark the task as notified with memory notification.
3172 *
3173 * Condition: task lock should be held while calling this function.
3174 */
3175 void
3176 task_mark_has_been_notified(task_t task, int pressurelevel)
3177 {
3178 if (task == NULL) {
3179 return;
3180 }
3181
3182 if (pressurelevel == kVMPressureWarning)
3183 task->low_mem_notified_warn = 1;
3184 else if (pressurelevel == kVMPressureCritical)
3185 task->low_mem_notified_critical = 1;
3186 }
3187
3188
3189 /*
3190 * Mark the task as purged.
3191 *
3192 * Condition: task lock should be held while calling this function.
3193 */
3194 void
3195 task_mark_used_for_purging(task_t task, int pressurelevel)
3196 {
3197 if (task == NULL) {
3198 return;
3199 }
3200
3201 if (pressurelevel == kVMPressureWarning)
3202 task->purged_memory_warn = 1;
3203 else if (pressurelevel == kVMPressureCritical)
3204 task->purged_memory_critical = 1;
3205 }
3206
3207
3208 /*
3209 * Mark the task eligible for low memory notification.
3210 *
3211 * Condition: task lock should be held while calling this function.
3212 */
3213 void
3214 task_clear_has_been_notified(task_t task, int pressurelevel)
3215 {
3216 if (task == NULL) {
3217 return;
3218 }
3219
3220 if (pressurelevel == kVMPressureWarning)
3221 task->low_mem_notified_warn = 0;
3222 else if (pressurelevel == kVMPressureCritical)
3223 task->low_mem_notified_critical = 0;
3224 }
3225
3226
3227 /*
3228 * Mark the task eligible for purging its purgeable memory.
3229 *
3230 * Condition: task lock should be held while calling this function.
3231 */
3232 void
3233 task_clear_used_for_purging(task_t task)
3234 {
3235 if (task == NULL) {
3236 return;
3237 }
3238
3239 task->purged_memory_warn = 0;
3240 task->purged_memory_critical = 0;
3241 }
3242
3243
3244 /*
3245 * Estimate task importance for purging its purgeable memory
3246 * and low memory notification.
3247 *
3248 * Importance is calculated in the following order of criteria:
3249 * -Task role : Background vs Foreground
3250 * -Boost status: Not boosted vs Boosted
3251 * -Darwin BG status.
3252 *
3253 * Returns: Estimated task importance. Less important task will have lower
3254 * estimated importance.
3255 */
3256 int
3257 task_importance_estimate(task_t task)
3258 {
3259 int task_importance = 0;
3260
3261 if (task == NULL) {
3262 return 0;
3263 }
3264
3265 if (proc_get_effective_task_policy(task, TASK_POLICY_ROLE) == TASK_FOREGROUND_APPLICATION)
3266 task_importance += TASK_IMPORTANCE_FOREGROUND;
3267
3268 if (proc_get_effective_task_policy(task, TASK_POLICY_DARWIN_BG) == 0)
3269 task_importance += TASK_IMPORTANCE_NOTDARWINBG;
3270
3271 return task_importance;
3272 }
3273
3274 boolean_t
3275 task_has_assertions(task_t task)
3276 {
3277 return (task->task_imp_base->iit_assertcnt? TRUE : FALSE);
3278 }
3279
3280
3281 kern_return_t
3282 send_resource_violation(typeof(send_cpu_usage_violation) sendfunc,
3283 task_t violator,
3284 struct ledger_entry_info *linfo,
3285 resource_notify_flags_t flags)
3286 {
3287 #ifndef MACH_BSD
3288 return KERN_NOT_SUPPORTED;
3289 #else
3290 kern_return_t kr = KERN_SUCCESS;
3291 proc_t proc = NULL;
3292 posix_path_t proc_path = "";
3293 proc_name_t procname = "<unknown>";
3294 int pid = -1;
3295 clock_sec_t secs;
3296 clock_nsec_t nsecs;
3297 mach_timespec_t timestamp;
3298 thread_t curthread = current_thread();
3299 ipc_port_t dstport = MACH_PORT_NULL;
3300
3301 if (!violator) {
3302 kr = KERN_INVALID_ARGUMENT; goto finish;
3303 }
3304
3305 /* extract violator information */
3306 task_lock(violator);
3307 if (!(proc = get_bsdtask_info(violator))) {
3308 task_unlock(violator);
3309 kr = KERN_INVALID_ARGUMENT; goto finish;
3310 }
3311 (void)mig_strncpy(procname, proc_best_name(proc), sizeof(procname));
3312 pid = task_pid(violator);
3313 if (flags & kRNFatalLimitFlag) {
3314 kr = proc_pidpathinfo_internal(proc, 0, proc_path,
3315 sizeof(proc_path), NULL);
3316 }
3317 task_unlock(violator);
3318 if (kr) goto finish;
3319
3320 /* violation time ~ now */
3321 clock_get_calendar_nanotime(&secs, &nsecs);
3322 timestamp.tv_sec = (int32_t)secs;
3323 timestamp.tv_nsec = (int32_t)nsecs;
3324 /* 25567702 tracks widening mach_timespec_t */
3325
3326 /* send message */
3327 kr = host_get_special_port(host_priv_self(), HOST_LOCAL_NODE,
3328 HOST_RESOURCE_NOTIFY_PORT, &dstport);
3329 if (kr) goto finish;
3330
3331 /* TH_OPT_HONOR_QLIMIT causes ipc_kmsg_send() to respect the
3332 * queue limit. It also unsets this flag, but this code also
3333 * unsets it for clarity and in case that code changes. */
3334 curthread->options |= TH_OPT_HONOR_QLIMIT;
3335 kr = sendfunc(dstport,
3336 procname, pid, proc_path, timestamp,
3337 linfo->lei_balance, linfo->lei_last_refill,
3338 linfo->lei_limit, linfo->lei_refill_period,
3339 flags);
3340 curthread->options &= (~TH_OPT_HONOR_QLIMIT);
3341
3342 ipc_port_release_send(dstport);
3343
3344 finish:
3345 return kr;
3346 #endif /* MACH_BSD */
3347 }
3348
3349
3350 /*
3351 * Resource violations trace four 64-bit integers. For K32, two additional
3352 * codes are allocated, the first with the low nibble doubled. So if the K64
3353 * code is 0x042, the K32 codes would be 0x044 and 0x45.
3354 */
3355 #ifdef __LP64__
3356 void
3357 trace_resource_violation(uint16_t code,
3358 struct ledger_entry_info *linfo)
3359 {
3360 KERNEL_DBG_IST_SANE(KDBG_CODE(DBG_MACH, DBG_MACH_RESOURCE, code),
3361 linfo->lei_balance, linfo->lei_last_refill,
3362 linfo->lei_limit, linfo->lei_refill_period);
3363 }
3364 #else /* K32 */
3365 /* TODO: create/find a trace_two_LLs() for K32 systems */
3366 #define MASK32 0xffffffff
3367 void
3368 trace_resource_violation(uint16_t code,
3369 struct ledger_entry_info *linfo)
3370 {
3371 int8_t lownibble = (code & 0x3) * 2;
3372 int16_t codeA = (code & 0xffc) | lownibble;
3373 int16_t codeB = codeA + 1;
3374
3375 int32_t balance_high = (linfo->lei_balance >> 32) & MASK32;
3376 int32_t balance_low = linfo->lei_balance & MASK32;
3377 int32_t last_refill_high = (linfo->lei_last_refill >> 32) & MASK32;
3378 int32_t last_refill_low = linfo->lei_last_refill & MASK32;
3379
3380 int32_t limit_high = (linfo->lei_limit >> 32) & MASK32;
3381 int32_t limit_low = linfo->lei_limit & MASK32;
3382 int32_t refill_period_high = (linfo->lei_refill_period >> 32) & MASK32;
3383 int32_t refill_period_low = linfo->lei_refill_period & MASK32;
3384
3385 KERNEL_DBG_IST_SANE(KDBG_CODE(DBG_MACH, DBG_MACH_RESOURCE, codeA),
3386 balance_high, balance_low,
3387 last_refill_high, last_refill_low);
3388 KERNEL_DBG_IST_SANE(KDBG_CODE(DBG_MACH, DBG_MACH_RESOURCE, codeB),
3389 limit_high, limit_low,
3390 refill_period_high, refill_period_low);
3391 }
3392 #endif /* K64/K32 */