X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/5ba3f43ea354af8ad55bea84372a2bc834d8757c..HEAD:/osfmk/kern/task_policy.c diff --git a/osfmk/kern/task_policy.c b/osfmk/kern/task_policy.c index b13186523..b96642803 100644 --- a/osfmk/kern/task_policy.c +++ b/osfmk/kern/task_policy.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000-2016 Apple Computer, Inc. All rights reserved. + * Copyright (c) 2000-2020 Apple Computer, Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * @@ -48,10 +48,10 @@ #if CONFIG_TELEMETRY #include #endif -#if CONFIG_EMBEDDED +#if !defined(XNU_TARGET_OS_OSX) #include #include -#endif /* CONFIG_EMBEDDED */ +#endif /* !defined(XNU_TARGET_OS_OSX) */ #if IMPORTANCE_INHERITANCE #include @@ -139,7 +139,7 @@ static void proc_set_task_policy_locked(task_t task, int category, int flavor, int value, int value2); static void task_policy_update_locked(task_t task, task_pend_token_t pend_token); -static void task_policy_update_internal_locked(task_t task, boolean_t in_create, task_pend_token_t pend_token); +static void task_policy_update_internal_locked(task_t task, bool in_create, task_pend_token_t pend_token); /* For attributes that have two scalars as input/output */ static void proc_set_task_policy2(task_t task, int category, int flavor, int value1, int value2); @@ -167,21 +167,20 @@ static void task_action_cpuusage(thread_call_param_t param0, thread_call_param_t #ifdef MACH_BSD typedef struct proc * proc_t; -int proc_pid(void *proc); +int proc_pid(struct proc *proc); extern int proc_selfpid(void); extern char * proc_name_address(void *p); extern char * proc_best_name(proc_t proc); extern int proc_pidpathinfo_internal(proc_t p, uint64_t arg, - char *buffer, uint32_t buffersize, - int32_t *retval); + char *buffer, uint32_t buffersize, + int32_t *retval); #endif /* MACH_BSD */ -#if CONFIG_EMBEDDED -/* TODO: make CONFIG_TASKWATCH */ +#if CONFIG_TASKWATCH /* Taskwatch related helper functions */ -static void set_thread_appbg(thread_t thread, int setbg,int importance); +static void set_thread_appbg(thread_t thread, int setbg, int importance); static void add_taskwatch_locked(task_t task, task_watch_t * twp); static void remove_taskwatch_locked(task_t task, task_watch_t * twp); static void task_watch_lock(void); @@ -201,7 +200,7 @@ typedef struct thread_watchlist { int importance; /* importance to be restored if thread is being made active */ } thread_watchlist_t; -#endif /* CONFIG_EMBEDDED */ +#endif /* CONFIG_TASKWATCH */ extern int memorystatus_update_priority_for_appnap(proc_t p, boolean_t is_appnap); @@ -248,11 +247,7 @@ int proc_tal_disk_tier = THROTTLE_LEVEL_TIER1; int proc_graphics_timer_qos = (LATENCY_QOS_TIER_0 & 0xFF); -#if CONFIG_EMBEDDED -const int proc_default_bg_iotier = THROTTLE_LEVEL_TIER3; -#else const int proc_default_bg_iotier = THROTTLE_LEVEL_TIER2; -#endif /* Latency/throughput QoS fields remain zeroed, i.e. TIER_UNSPECIFIED at creation */ const struct task_requested_policy default_task_requested_policy = { @@ -269,39 +264,46 @@ const struct task_effective_policy default_task_effective_policy = {}; #define DEFAULT_CPUMON_INTERVAL (3 * 60) uint8_t proc_max_cpumon_percentage; -uint64_t proc_max_cpumon_interval; +uint64_t proc_max_cpumon_interval; kern_return_t -qos_latency_policy_validate(task_latency_qos_t ltier) { +qos_latency_policy_validate(task_latency_qos_t ltier) +{ if ((ltier != LATENCY_QOS_TIER_UNSPECIFIED) && - ((ltier > LATENCY_QOS_TIER_5) || (ltier < LATENCY_QOS_TIER_0))) + ((ltier > LATENCY_QOS_TIER_5) || (ltier < LATENCY_QOS_TIER_0))) { return KERN_INVALID_ARGUMENT; + } return KERN_SUCCESS; } kern_return_t -qos_throughput_policy_validate(task_throughput_qos_t ttier) { +qos_throughput_policy_validate(task_throughput_qos_t ttier) +{ if ((ttier != THROUGHPUT_QOS_TIER_UNSPECIFIED) && - ((ttier > THROUGHPUT_QOS_TIER_5) || (ttier < THROUGHPUT_QOS_TIER_0))) + ((ttier > THROUGHPUT_QOS_TIER_5) || (ttier < THROUGHPUT_QOS_TIER_0))) { return KERN_INVALID_ARGUMENT; + } return KERN_SUCCESS; } static kern_return_t -task_qos_policy_validate(task_qos_policy_t qosinfo, mach_msg_type_number_t count) { - if (count < TASK_QOS_POLICY_COUNT) +task_qos_policy_validate(task_qos_policy_t qosinfo, mach_msg_type_number_t count) +{ + if (count < TASK_QOS_POLICY_COUNT) { return KERN_INVALID_ARGUMENT; + } task_latency_qos_t ltier = qosinfo->task_latency_qos_tier; task_throughput_qos_t ttier = qosinfo->task_throughput_qos_tier; kern_return_t kr = qos_latency_policy_validate(ltier); - if (kr != KERN_SUCCESS) + if (kr != KERN_SUCCESS) { return kr; + } kr = qos_throughput_policy_validate(ttier); @@ -309,79 +311,90 @@ task_qos_policy_validate(task_qos_policy_t qosinfo, mach_msg_type_number_t count } uint32_t -qos_extract(uint32_t qv) { - return (qv & 0xFF); +qos_extract(uint32_t qv) +{ + return qv & 0xFF; } uint32_t -qos_latency_policy_package(uint32_t qv) { +qos_latency_policy_package(uint32_t qv) +{ return (qv == LATENCY_QOS_TIER_UNSPECIFIED) ? LATENCY_QOS_TIER_UNSPECIFIED : ((0xFF << 16) | qv); } uint32_t -qos_throughput_policy_package(uint32_t qv) { +qos_throughput_policy_package(uint32_t qv) +{ return (qv == THROUGHPUT_QOS_TIER_UNSPECIFIED) ? THROUGHPUT_QOS_TIER_UNSPECIFIED : ((0xFE << 16) | qv); } +#define TASK_POLICY_SUPPRESSION_DISABLE 0x1 +#define TASK_POLICY_SUPPRESSION_IOTIER2 0x2 +#define TASK_POLICY_SUPPRESSION_NONDONOR 0x4 /* TEMPORARY boot-arg controlling task_policy suppression (App Nap) */ -static boolean_t task_policy_suppression_disable = FALSE; +static boolean_t task_policy_suppression_flags = TASK_POLICY_SUPPRESSION_IOTIER2 | + TASK_POLICY_SUPPRESSION_NONDONOR; kern_return_t task_policy_set( - task_t task, - task_policy_flavor_t flavor, - task_policy_t policy_info, - mach_msg_type_number_t count) + task_t task, + task_policy_flavor_t flavor, + task_policy_t policy_info, + mach_msg_type_number_t count) { - kern_return_t result = KERN_SUCCESS; + kern_return_t result = KERN_SUCCESS; - if (task == TASK_NULL || task == kernel_task) - return (KERN_INVALID_ARGUMENT); + if (task == TASK_NULL || task == kernel_task) { + return KERN_INVALID_ARGUMENT; + } switch (flavor) { - case TASK_CATEGORY_POLICY: { task_category_policy_t info = (task_category_policy_t)policy_info; - if (count < TASK_CATEGORY_POLICY_COUNT) - return (KERN_INVALID_ARGUMENT); + if (count < TASK_CATEGORY_POLICY_COUNT) { + return KERN_INVALID_ARGUMENT; + } -#if CONFIG_EMBEDDED +#if !defined(XNU_TARGET_OS_OSX) /* On embedded, you can't modify your own role. */ - if (current_task() == task) - return (KERN_INVALID_ARGUMENT); + if (current_task() == task) { + return KERN_INVALID_ARGUMENT; + } #endif - switch(info->role) { - case TASK_FOREGROUND_APPLICATION: - case TASK_BACKGROUND_APPLICATION: - case TASK_DEFAULT_APPLICATION: + switch (info->role) { + case TASK_FOREGROUND_APPLICATION: + case TASK_BACKGROUND_APPLICATION: + case TASK_DEFAULT_APPLICATION: + proc_set_task_policy(task, + TASK_POLICY_ATTRIBUTE, TASK_POLICY_ROLE, + info->role); + break; + + case TASK_CONTROL_APPLICATION: + if (task != current_task() || task->sec_token.val[0] != 0) { + result = KERN_INVALID_ARGUMENT; + } else { proc_set_task_policy(task, - TASK_POLICY_ATTRIBUTE, TASK_POLICY_ROLE, - info->role); - break; - - case TASK_CONTROL_APPLICATION: - if (task != current_task() || task->sec_token.val[0] != 0) - result = KERN_INVALID_ARGUMENT; - else - proc_set_task_policy(task, - TASK_POLICY_ATTRIBUTE, TASK_POLICY_ROLE, - info->role); - break; - - case TASK_GRAPHICS_SERVER: - /* TODO: Restrict this role to FCFS */ - if (task != current_task() || task->sec_token.val[0] != 0) - result = KERN_INVALID_ARGUMENT; - else - proc_set_task_policy(task, - TASK_POLICY_ATTRIBUTE, TASK_POLICY_ROLE, - info->role); - break; - default: + TASK_POLICY_ATTRIBUTE, TASK_POLICY_ROLE, + info->role); + } + break; + + case TASK_GRAPHICS_SERVER: + /* TODO: Restrict this role to FCFS */ + if (task != current_task() || task->sec_token.val[0] != 0) { result = KERN_INVALID_ARGUMENT; - break; + } else { + proc_set_task_policy(task, + TASK_POLICY_ATTRIBUTE, TASK_POLICY_ROLE, + info->role); + } + break; + default: + result = KERN_INVALID_ARGUMENT; + break; } /* switch (info->role) */ break; @@ -394,16 +407,17 @@ task_policy_set( task_qos_policy_t qosinfo = (task_qos_policy_t)policy_info; kern_return_t kr = task_qos_policy_validate(qosinfo, count); - if (kr != KERN_SUCCESS) + if (kr != KERN_SUCCESS) { return kr; + } uint32_t lqos = qos_extract(qosinfo->task_latency_qos_tier); uint32_t tqos = qos_extract(qosinfo->task_throughput_qos_tier); proc_set_task_policy2(task, TASK_POLICY_ATTRIBUTE, - flavor == TASK_BASE_QOS_POLICY ? TASK_POLICY_BASE_LATENCY_AND_THROUGHPUT_QOS : TASK_POLICY_OVERRIDE_LATENCY_AND_THROUGHPUT_QOS, - lqos, tqos); + flavor == TASK_BASE_QOS_POLICY ? TASK_POLICY_BASE_LATENCY_AND_THROUGHPUT_QOS : TASK_POLICY_OVERRIDE_LATENCY_AND_THROUGHPUT_QOS, + lqos, tqos); } break; @@ -412,8 +426,9 @@ task_policy_set( task_qos_policy_t qosinfo = (task_qos_policy_t)policy_info; kern_return_t kr = task_qos_policy_validate(qosinfo, count); - if (kr != KERN_SUCCESS) + if (kr != KERN_SUCCESS) { return kr; + } uint32_t lqos = qos_extract(qosinfo->task_latency_qos_tier); @@ -426,8 +441,9 @@ task_policy_set( task_qos_policy_t qosinfo = (task_qos_policy_t)policy_info; kern_return_t kr = task_qos_policy_validate(qosinfo, count); - if (kr != KERN_SUCCESS) + if (kr != KERN_SUCCESS) { return kr; + } uint32_t tqos = qos_extract(qosinfo->task_throughput_qos_tier); @@ -437,19 +453,20 @@ task_policy_set( case TASK_SUPPRESSION_POLICY: { -#if CONFIG_EMBEDDED +#if !defined(XNU_TARGET_OS_OSX) /* * Suppression policy is not enabled for embedded * because apps aren't marked as denap receivers */ result = KERN_INVALID_ARGUMENT; break; -#else /* CONFIG_EMBEDDED */ +#else /* !defined(XNU_TARGET_OS_OSX) */ task_suppression_policy_t info = (task_suppression_policy_t)policy_info; - if (count < TASK_SUPPRESSION_POLICY_COUNT) - return (KERN_INVALID_ARGUMENT); + if (count < TASK_SUPPRESSION_POLICY_COUNT) { + return KERN_INVALID_ARGUMENT; + } struct task_qos_policy qosinfo; @@ -458,21 +475,24 @@ task_policy_set( kern_return_t kr = task_qos_policy_validate(&qosinfo, TASK_QOS_POLICY_COUNT); - if (kr != KERN_SUCCESS) + if (kr != KERN_SUCCESS) { return kr; + } /* TEMPORARY disablement of task suppression */ - if (task_policy_suppression_disable && info->active) + if (info->active && + (task_policy_suppression_flags & TASK_POLICY_SUPPRESSION_DISABLE)) { return KERN_SUCCESS; + } struct task_pend_token pend_token = {}; task_lock(task); KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, - (IMPORTANCE_CODE(IMP_TASK_SUPPRESSION, info->active)) | DBG_FUNC_START, - proc_selfpid(), task_pid(task), trequested_0(task), - trequested_1(task), 0); + (IMPORTANCE_CODE(IMP_TASK_SUPPRESSION, info->active)) | DBG_FUNC_START, + proc_selfpid(), task_pid(task), trequested_0(task), + trequested_1(task), 0); task->requested_policy.trp_sup_active = (info->active) ? 1 : 0; task->requested_policy.trp_sup_lowpri_cpu = (info->lowpri_cpu) ? 1 : 0; @@ -485,9 +505,9 @@ task_policy_set( task_policy_update_locked(task, &pend_token); KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, - (IMPORTANCE_CODE(IMP_TASK_SUPPRESSION, info->active)) | DBG_FUNC_END, - proc_selfpid(), task_pid(task), trequested_0(task), - trequested_1(task), 0); + (IMPORTANCE_CODE(IMP_TASK_SUPPRESSION, info->active)) | DBG_FUNC_END, + proc_selfpid(), task_pid(task), trequested_0(task), + trequested_1(task), 0); task_unlock(task); @@ -495,7 +515,7 @@ task_policy_set( break; -#endif /* CONFIG_EMBEDDED */ +#endif /* !defined(XNU_TARGET_OS_OSX) */ } default: @@ -503,30 +523,31 @@ task_policy_set( break; } - return (result); + return result; } /* Sets BSD 'nice' value on the task */ kern_return_t task_importance( - task_t task, - integer_t importance) + task_t task, + integer_t importance) { - if (task == TASK_NULL || task == kernel_task) - return (KERN_INVALID_ARGUMENT); + if (task == TASK_NULL || task == kernel_task) { + return KERN_INVALID_ARGUMENT; + } task_lock(task); if (!task->active) { task_unlock(task); - return (KERN_TERMINATED); + return KERN_TERMINATED; } if (proc_get_effective_task_policy(task, TASK_POLICY_ROLE) >= TASK_CONTROL_APPLICATION) { task_unlock(task); - return (KERN_INVALID_ARGUMENT); + return KERN_INVALID_ARGUMENT; } task->importance = importance; @@ -539,33 +560,35 @@ task_importance( task_policy_update_complete_unlocked(task, &pend_token); - return (KERN_SUCCESS); + return KERN_SUCCESS; } kern_return_t task_policy_get( - task_t task, - task_policy_flavor_t flavor, - task_policy_t policy_info, - mach_msg_type_number_t *count, - boolean_t *get_default) + task_t task, + task_policy_flavor_t flavor, + task_policy_t policy_info, + mach_msg_type_number_t *count, + boolean_t *get_default) { - if (task == TASK_NULL || task == kernel_task) - return (KERN_INVALID_ARGUMENT); + if (task == TASK_NULL || task == kernel_task) { + return KERN_INVALID_ARGUMENT; + } switch (flavor) { - case TASK_CATEGORY_POLICY: { - task_category_policy_t info = (task_category_policy_t)policy_info; + task_category_policy_t info = (task_category_policy_t)policy_info; - if (*count < TASK_CATEGORY_POLICY_COUNT) - return (KERN_INVALID_ARGUMENT); + if (*count < TASK_CATEGORY_POLICY_COUNT) { + return KERN_INVALID_ARGUMENT; + } - if (*get_default) + if (*get_default) { info->role = TASK_UNSPECIFIED; - else + } else { info->role = proc_get_task_policy(task, TASK_POLICY_ATTRIBUTE, TASK_POLICY_ROLE); + } break; } @@ -574,8 +597,9 @@ task_policy_get( { task_qos_policy_t info = (task_qos_policy_t)policy_info; - if (*count < TASK_QOS_POLICY_COUNT) - return (KERN_INVALID_ARGUMENT); + if (*count < TASK_QOS_POLICY_COUNT) { + return KERN_INVALID_ARGUMENT; + } if (*get_default) { info->task_latency_qos_tier = LATENCY_QOS_TIER_UNSPECIFIED; @@ -587,7 +611,6 @@ task_policy_get( info->task_latency_qos_tier = qos_latency_policy_package(value1); info->task_throughput_qos_tier = qos_throughput_policy_package(value2); - } else if (flavor == TASK_OVERRIDE_QOS_POLICY) { int value1, value2; @@ -604,12 +627,14 @@ task_policy_get( { task_policy_state_t info = (task_policy_state_t)policy_info; - if (*count < TASK_POLICY_STATE_COUNT) - return (KERN_INVALID_ARGUMENT); + if (*count < TASK_POLICY_STATE_COUNT) { + return KERN_INVALID_ARGUMENT; + } /* Only root can get this info */ - if (current_task()->sec_token.val[0] != 0) + if (current_task()->sec_token.val[0] != 0) { return KERN_PROTECTION_FAILURE; + } if (*get_default) { info->requested = 0; @@ -637,6 +662,7 @@ task_policy_get( info->flags |= (task_is_marked_importance_denap_receiver(task) ? TASK_DENAP_RECEIVER : 0); info->flags |= (task_is_marked_importance_donor(task) ? TASK_IMP_DONOR : 0); info->flags |= (task_is_marked_live_importance_donor(task) ? TASK_IMP_LIVE_DONOR : 0); + info->flags |= (get_task_pidsuspended(task) ? TASK_IS_PIDSUSPENDED : 0); info->imp_transitions = task->task_imp_base->iit_transitions; } else { info->imp_assertcnt = 0; @@ -653,8 +679,9 @@ task_policy_get( { task_suppression_policy_t info = (task_suppression_policy_t)policy_info; - if (*count < TASK_SUPPRESSION_POLICY_COUNT) - return (KERN_INVALID_ARGUMENT); + if (*count < TASK_SUPPRESSION_POLICY_COUNT) { + return KERN_INVALID_ARGUMENT; + } task_lock(task); @@ -684,10 +711,10 @@ task_policy_get( } default: - return (KERN_INVALID_ARGUMENT); + return KERN_INVALID_ARGUMENT; } - return (KERN_SUCCESS); + return KERN_SUCCESS; } /* @@ -722,16 +749,16 @@ task_policy_create(task_t task, task_t parent_task) } KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, - (IMPORTANCE_CODE(IMP_UPDATE, (IMP_UPDATE_TASK_CREATE | TASK_POLICY_TASK))) | DBG_FUNC_START, - task_pid(task), teffective_0(task), - teffective_1(task), task->priority, 0); + (IMPORTANCE_CODE(IMP_UPDATE, (IMP_UPDATE_TASK_CREATE | TASK_POLICY_TASK))) | DBG_FUNC_START, + task_pid(task), teffective_0(task), + teffective_1(task), task->priority, 0); - task_policy_update_internal_locked(task, TRUE, NULL); + task_policy_update_internal_locked(task, true, NULL); KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, - (IMPORTANCE_CODE(IMP_UPDATE, (IMP_UPDATE_TASK_CREATE | TASK_POLICY_TASK))) | DBG_FUNC_END, - task_pid(task), teffective_0(task), - teffective_1(task), task->priority, 0); + (IMPORTANCE_CODE(IMP_UPDATE, (IMP_UPDATE_TASK_CREATE | TASK_POLICY_TASK))) | DBG_FUNC_END, + task_pid(task), teffective_0(task), + teffective_1(task), task->priority, 0); task_importance_update_live_donor(task); } @@ -741,16 +768,16 @@ static void task_policy_update_locked(task_t task, task_pend_token_t pend_token) { KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, - (IMPORTANCE_CODE(IMP_UPDATE, TASK_POLICY_TASK) | DBG_FUNC_START), - task_pid(task), teffective_0(task), - teffective_1(task), task->priority, 0); + (IMPORTANCE_CODE(IMP_UPDATE, TASK_POLICY_TASK) | DBG_FUNC_START), + task_pid(task), teffective_0(task), + teffective_1(task), task->priority, 0); - task_policy_update_internal_locked(task, FALSE, pend_token); + task_policy_update_internal_locked(task, false, pend_token); KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, - (IMPORTANCE_CODE(IMP_UPDATE, TASK_POLICY_TASK)) | DBG_FUNC_END, - task_pid(task), teffective_0(task), - teffective_1(task), task->priority, 0); + (IMPORTANCE_CODE(IMP_UPDATE, TASK_POLICY_TASK)) | DBG_FUNC_END, + task_pid(task), teffective_0(task), + teffective_1(task), task->priority, 0); } /* @@ -766,7 +793,7 @@ task_policy_update_locked(task_t task, task_pend_token_t pend_token) */ static void -task_policy_update_internal_locked(task_t task, boolean_t in_create, task_pend_token_t pend_token) +task_policy_update_internal_locked(task_t task, bool in_create, task_pend_token_t pend_token) { /* * Step 1: @@ -790,58 +817,69 @@ task_policy_update_internal_locked(task_t task, boolean_t in_create, task_pend_t /* Set task qos clamp and ceiling */ next.tep_qos_clamp = requested.trp_qos_clamp; - if (requested.trp_apptype == TASK_APPTYPE_APP_DEFAULT || - requested.trp_apptype == TASK_APPTYPE_APP_TAL) { - + if (requested.trp_apptype == TASK_APPTYPE_APP_DEFAULT) { switch (next.tep_role) { - case TASK_FOREGROUND_APPLICATION: - /* Foreground apps get urgent scheduler priority */ - next.tep_qos_ui_is_urgent = 1; - next.tep_qos_ceiling = THREAD_QOS_UNSPECIFIED; - break; - - case TASK_BACKGROUND_APPLICATION: - /* This is really 'non-focal but on-screen' */ - next.tep_qos_ceiling = THREAD_QOS_UNSPECIFIED; - break; - - case TASK_DEFAULT_APPLICATION: - /* This is 'may render UI but we don't know if it's focal/nonfocal' */ - next.tep_qos_ceiling = THREAD_QOS_UNSPECIFIED; - break; - - case TASK_NONUI_APPLICATION: - /* i.e. 'off-screen' */ - next.tep_qos_ceiling = THREAD_QOS_LEGACY; - break; - - case TASK_CONTROL_APPLICATION: - case TASK_GRAPHICS_SERVER: - next.tep_qos_ui_is_urgent = 1; - next.tep_qos_ceiling = THREAD_QOS_UNSPECIFIED; - break; - - case TASK_THROTTLE_APPLICATION: - /* i.e. 'TAL launch' */ - next.tep_qos_ceiling = THREAD_QOS_UTILITY; - break; - - case TASK_UNSPECIFIED: - default: - /* Apps that don't have an application role get - * USER_INTERACTIVE and USER_INITIATED squashed to LEGACY */ - next.tep_qos_ceiling = THREAD_QOS_LEGACY; - break; + case TASK_FOREGROUND_APPLICATION: + /* Foreground apps get urgent scheduler priority */ + next.tep_qos_ui_is_urgent = 1; + next.tep_qos_ceiling = THREAD_QOS_UNSPECIFIED; + break; + + case TASK_BACKGROUND_APPLICATION: + /* This is really 'non-focal but on-screen' */ + next.tep_qos_ceiling = THREAD_QOS_UNSPECIFIED; + break; + + case TASK_DEFAULT_APPLICATION: + /* This is 'may render UI but we don't know if it's focal/nonfocal' */ + next.tep_qos_ceiling = THREAD_QOS_UNSPECIFIED; + break; + + case TASK_NONUI_APPLICATION: + /* i.e. 'off-screen' */ + next.tep_qos_ceiling = THREAD_QOS_LEGACY; + break; + + case TASK_CONTROL_APPLICATION: + case TASK_GRAPHICS_SERVER: + next.tep_qos_ui_is_urgent = 1; + next.tep_qos_ceiling = THREAD_QOS_UNSPECIFIED; + break; + + case TASK_THROTTLE_APPLICATION: + /* i.e. 'TAL launch' */ + next.tep_qos_ceiling = THREAD_QOS_UTILITY; + break; + + case TASK_DARWINBG_APPLICATION: + /* i.e. 'DARWIN_BG throttled background application' */ + next.tep_qos_ceiling = THREAD_QOS_BACKGROUND; + break; + + case TASK_UNSPECIFIED: + default: + /* Apps that don't have an application role get + * USER_INTERACTIVE and USER_INITIATED squashed to LEGACY */ + next.tep_qos_ceiling = THREAD_QOS_LEGACY; + break; } } else { - /* Daemons get USER_INTERACTIVE squashed to USER_INITIATED */ + /* Daemons and dext get USER_INTERACTIVE squashed to USER_INITIATED */ next.tep_qos_ceiling = THREAD_QOS_USER_INITIATED; } /* Calculate DARWIN_BG */ - boolean_t wants_darwinbg = FALSE; - boolean_t wants_all_sockets_bg = FALSE; /* Do I want my existing sockets to be bg */ - boolean_t wants_watchersbg = FALSE; /* Do I want my pidbound threads to be bg */ + bool wants_darwinbg = false; + bool wants_all_sockets_bg = false; /* Do I want my existing sockets to be bg */ + bool wants_watchersbg = false; /* Do I want my pidbound threads to be bg */ + bool adaptive_bg_only = false; /* This task is BG only because it's adaptive unboosted */ + + /* Adaptive daemons are DARWIN_BG unless boosted, and don't get network throttled. */ + if (requested.trp_apptype == TASK_APPTYPE_DAEMON_ADAPTIVE && + requested.trp_boosted == 0) { + wants_darwinbg = true; + adaptive_bg_only = true; + } /* * If DARWIN_BG has been requested at either level, it's engaged. @@ -849,33 +887,29 @@ task_policy_update_internal_locked(task_t task, boolean_t in_create, task_pend_t * * Backgrounding due to apptype does. */ - if (requested.trp_int_darwinbg || requested.trp_ext_darwinbg) - wants_watchersbg = wants_all_sockets_bg = wants_darwinbg = TRUE; - - /* Background TAL apps are throttled when TAL is enabled */ - if (requested.trp_apptype == TASK_APPTYPE_APP_TAL && - requested.trp_role == TASK_BACKGROUND_APPLICATION && - requested.trp_tal_enabled == 1) { - next.tep_tal_engaged = 1; + if (requested.trp_int_darwinbg || requested.trp_ext_darwinbg || + next.tep_role == TASK_DARWINBG_APPLICATION) { + wants_watchersbg = wants_all_sockets_bg = wants_darwinbg = true; + adaptive_bg_only = false; } - if ((requested.trp_apptype == TASK_APPTYPE_APP_DEFAULT || - requested.trp_apptype == TASK_APPTYPE_APP_TAL) && - requested.trp_role == TASK_THROTTLE_APPLICATION) { + /* Application launching in special Transparent App Lifecycle throttle mode */ + if (requested.trp_apptype == TASK_APPTYPE_APP_DEFAULT && + requested.trp_role == TASK_THROTTLE_APPLICATION) { next.tep_tal_engaged = 1; } - /* Adaptive daemons are DARWIN_BG unless boosted, and don't get network throttled. */ - if (requested.trp_apptype == TASK_APPTYPE_DAEMON_ADAPTIVE && - requested.trp_boosted == 0) - wants_darwinbg = TRUE; - /* Background daemons are always DARWIN_BG, no exceptions, and don't get network throttled. */ - if (requested.trp_apptype == TASK_APPTYPE_DAEMON_BACKGROUND) - wants_darwinbg = TRUE; + if (requested.trp_apptype == TASK_APPTYPE_DAEMON_BACKGROUND) { + wants_darwinbg = true; + adaptive_bg_only = false; + } - if (next.tep_qos_clamp == THREAD_QOS_BACKGROUND || next.tep_qos_clamp == THREAD_QOS_MAINTENANCE) - wants_darwinbg = TRUE; + if (next.tep_qos_clamp == THREAD_QOS_BACKGROUND || + next.tep_qos_clamp == THREAD_QOS_MAINTENANCE) { + wants_darwinbg = true; + adaptive_bg_only = false; + } /* Calculate side effects of DARWIN_BG */ @@ -886,27 +920,35 @@ task_policy_update_internal_locked(task_t task, boolean_t in_create, task_pend_t next.tep_lowpri_cpu = 1; } - if (wants_all_sockets_bg) + if (wants_all_sockets_bg) { next.tep_all_sockets_bg = 1; + } - if (wants_watchersbg) + if (wants_watchersbg) { next.tep_watchers_bg = 1; + } + + next.tep_adaptive_bg = adaptive_bg_only; /* Calculate low CPU priority */ - boolean_t wants_lowpri_cpu = FALSE; + boolean_t wants_lowpri_cpu = false; - if (wants_darwinbg) - wants_lowpri_cpu = TRUE; + if (wants_darwinbg) { + wants_lowpri_cpu = true; + } - if (next.tep_tal_engaged) - wants_lowpri_cpu = TRUE; + if (next.tep_tal_engaged) { + wants_lowpri_cpu = true; + } - if (requested.trp_sup_lowpri_cpu && requested.trp_boosted == 0) - wants_lowpri_cpu = TRUE; + if (requested.trp_sup_lowpri_cpu && requested.trp_boosted == 0) { + wants_lowpri_cpu = true; + } - if (wants_lowpri_cpu) + if (wants_lowpri_cpu) { next.tep_lowpri_cpu = 1; + } /* Calculate IO policy */ @@ -915,20 +957,25 @@ task_policy_update_internal_locked(task_t task, boolean_t in_create, task_pend_t int iopol = THROTTLE_LEVEL_TIER0; - if (wants_darwinbg) + if (wants_darwinbg) { iopol = MAX(iopol, requested.trp_bg_iotier); + } - if (requested.trp_apptype == TASK_APPTYPE_DAEMON_STANDARD) + if (requested.trp_apptype == TASK_APPTYPE_DAEMON_STANDARD) { iopol = MAX(iopol, proc_standard_daemon_tier); + } - if (requested.trp_sup_disk && requested.trp_boosted == 0) + if (requested.trp_sup_disk && requested.trp_boosted == 0) { iopol = MAX(iopol, proc_suppressed_disk_tier); + } - if (next.tep_tal_engaged) + if (next.tep_tal_engaged) { iopol = MAX(iopol, proc_tal_disk_tier); + } - if (next.tep_qos_clamp != THREAD_QOS_UNSPECIFIED) + if (next.tep_qos_clamp != THREAD_QOS_UNSPECIFIED) { iopol = MAX(iopol, thread_qos_policy_params.qos_iotier[next.tep_qos_clamp]); + } iopol = MAX(iopol, requested.trp_int_iotier); iopol = MAX(iopol, requested.trp_ext_iotier); @@ -937,59 +984,70 @@ task_policy_update_internal_locked(task_t task, boolean_t in_create, task_pend_t /* Calculate Passive IO policy */ - if (requested.trp_ext_iopassive || requested.trp_int_iopassive) + if (requested.trp_ext_iopassive || requested.trp_int_iopassive) { next.tep_io_passive = 1; + } /* Calculate suppression-active flag */ - boolean_t memorystatus_appnap_transition = FALSE; + boolean_t appnap_transition = false; - if (requested.trp_sup_active && requested.trp_boosted == 0) + if (requested.trp_sup_active && requested.trp_boosted == 0) { next.tep_sup_active = 1; + } - if (task->effective_policy.tep_sup_active != next.tep_sup_active) - memorystatus_appnap_transition = TRUE; + if (task->effective_policy.tep_sup_active != next.tep_sup_active) { + appnap_transition = true; + } /* Calculate timer QOS */ int latency_qos = requested.trp_base_latency_qos; - if (requested.trp_sup_timer && requested.trp_boosted == 0) + if (requested.trp_sup_timer && requested.trp_boosted == 0) { latency_qos = requested.trp_sup_timer; + } - if (next.tep_qos_clamp != THREAD_QOS_UNSPECIFIED) + if (next.tep_qos_clamp != THREAD_QOS_UNSPECIFIED) { latency_qos = MAX(latency_qos, (int)thread_qos_policy_params.qos_latency_qos[next.tep_qos_clamp]); + } - if (requested.trp_over_latency_qos != 0) + if (requested.trp_over_latency_qos != 0) { latency_qos = requested.trp_over_latency_qos; + } /* Treat the windowserver special */ - if (requested.trp_role == TASK_GRAPHICS_SERVER) + if (requested.trp_role == TASK_GRAPHICS_SERVER) { latency_qos = proc_graphics_timer_qos; + } next.tep_latency_qos = latency_qos; /* Calculate throughput QOS */ int through_qos = requested.trp_base_through_qos; - if (requested.trp_sup_throughput && requested.trp_boosted == 0) + if (requested.trp_sup_throughput && requested.trp_boosted == 0) { through_qos = requested.trp_sup_throughput; + } - if (next.tep_qos_clamp != THREAD_QOS_UNSPECIFIED) + if (next.tep_qos_clamp != THREAD_QOS_UNSPECIFIED) { through_qos = MAX(through_qos, (int)thread_qos_policy_params.qos_through_qos[next.tep_qos_clamp]); + } - if (requested.trp_over_through_qos != 0) + if (requested.trp_over_through_qos != 0) { through_qos = requested.trp_over_through_qos; + } next.tep_through_qos = through_qos; /* Calculate suppressed CPU priority */ - if (requested.trp_sup_cpu && requested.trp_boosted == 0) + if (requested.trp_sup_cpu && requested.trp_boosted == 0) { next.tep_suppressed_cpu = 1; + } /* * Calculate background sockets * Don't take into account boosting to limit transition frequency. */ - if (requested.trp_sup_bg_sockets){ + if (requested.trp_sup_bg_sockets) { next.tep_all_sockets_bg = 1; next.tep_new_sockets_bg = 1; } @@ -999,21 +1057,26 @@ task_policy_update_internal_locked(task_t task, boolean_t in_create, task_pend_t /* Calculate 'live donor' status for live importance */ switch (requested.trp_apptype) { - case TASK_APPTYPE_APP_TAL: - case TASK_APPTYPE_APP_DEFAULT: - if (requested.trp_ext_darwinbg == 0) - next.tep_live_donor = 1; - else - next.tep_live_donor = 0; - break; - - case TASK_APPTYPE_DAEMON_INTERACTIVE: - case TASK_APPTYPE_DAEMON_STANDARD: - case TASK_APPTYPE_DAEMON_ADAPTIVE: - case TASK_APPTYPE_DAEMON_BACKGROUND: - default: + case TASK_APPTYPE_APP_TAL: + case TASK_APPTYPE_APP_DEFAULT: + if (requested.trp_ext_darwinbg == 1 || + (next.tep_sup_active == 1 && + (task_policy_suppression_flags & TASK_POLICY_SUPPRESSION_NONDONOR)) || + next.tep_role == TASK_DARWINBG_APPLICATION) { next.tep_live_donor = 0; - break; + } else { + next.tep_live_donor = 1; + } + break; + + case TASK_APPTYPE_DAEMON_INTERACTIVE: + case TASK_APPTYPE_DAEMON_STANDARD: + case TASK_APPTYPE_DAEMON_ADAPTIVE: + case TASK_APPTYPE_DAEMON_BACKGROUND: + case TASK_APPTYPE_DRIVER: + default: + next.tep_live_donor = 0; + break; } if (requested.trp_terminated) { @@ -1048,76 +1111,86 @@ task_policy_update_internal_locked(task_t task, boolean_t in_create, task_pend_t task->effective_policy = next; /* Don't do anything further to a half-formed task */ - if (in_create) + if (in_create) { return; + } - if (task == kernel_task) + if (task == kernel_task) { panic("Attempting to set task policy on kernel_task"); + } /* * Step 4: * Pend updates that can't be done while holding the task lock */ - if (prev.tep_all_sockets_bg != next.tep_all_sockets_bg) + if (prev.tep_all_sockets_bg != next.tep_all_sockets_bg) { pend_token->tpt_update_sockets = 1; + } /* Only re-scan the timer list if the qos level is getting less strong */ - if (prev.tep_latency_qos > next.tep_latency_qos) + if (prev.tep_latency_qos > next.tep_latency_qos) { pend_token->tpt_update_timers = 1; + } -#if CONFIG_EMBEDDED - if (prev.tep_watchers_bg != next.tep_watchers_bg) +#if CONFIG_TASKWATCH + if (prev.tep_watchers_bg != next.tep_watchers_bg) { pend_token->tpt_update_watchers = 1; -#endif /* CONFIG_EMBEDDED */ + } +#endif /* CONFIG_TASKWATCH */ - if (prev.tep_live_donor != next.tep_live_donor) + if (prev.tep_live_donor != next.tep_live_donor) { pend_token->tpt_update_live_donor = 1; + } /* * Step 5: * Update other subsystems as necessary if something has changed */ - boolean_t update_threads = FALSE, update_sfi = FALSE; + bool update_threads = false, update_sfi = false; /* * Check for the attributes that thread_policy_update_internal_locked() consults, * and trigger thread policy re-evaluation. */ - if (prev.tep_io_tier != next.tep_io_tier || - prev.tep_bg_iotier != next.tep_bg_iotier || - prev.tep_io_passive != next.tep_io_passive || - prev.tep_darwinbg != next.tep_darwinbg || - prev.tep_qos_clamp != next.tep_qos_clamp || - prev.tep_qos_ceiling != next.tep_qos_ceiling || - prev.tep_qos_ui_is_urgent != next.tep_qos_ui_is_urgent || - prev.tep_latency_qos != next.tep_latency_qos || - prev.tep_through_qos != next.tep_through_qos || - prev.tep_lowpri_cpu != next.tep_lowpri_cpu || - prev.tep_new_sockets_bg != next.tep_new_sockets_bg || - prev.tep_terminated != next.tep_terminated ) - update_threads = TRUE; + if (prev.tep_io_tier != next.tep_io_tier || + prev.tep_bg_iotier != next.tep_bg_iotier || + prev.tep_io_passive != next.tep_io_passive || + prev.tep_darwinbg != next.tep_darwinbg || + prev.tep_qos_clamp != next.tep_qos_clamp || + prev.tep_qos_ceiling != next.tep_qos_ceiling || + prev.tep_qos_ui_is_urgent != next.tep_qos_ui_is_urgent || + prev.tep_latency_qos != next.tep_latency_qos || + prev.tep_through_qos != next.tep_through_qos || + prev.tep_lowpri_cpu != next.tep_lowpri_cpu || + prev.tep_new_sockets_bg != next.tep_new_sockets_bg || + prev.tep_terminated != next.tep_terminated || + prev.tep_adaptive_bg != next.tep_adaptive_bg) { + update_threads = true; + } /* * Check for the attributes that sfi_thread_classify() consults, * and trigger SFI re-evaluation. */ if (prev.tep_latency_qos != next.tep_latency_qos || - prev.tep_role != next.tep_role || - prev.tep_sfi_managed != next.tep_sfi_managed ) - update_sfi = TRUE; + prev.tep_role != next.tep_role || + prev.tep_sfi_managed != next.tep_sfi_managed) { + update_sfi = true; + } /* Reflect task role transitions into the coalition role counters */ if (prev.tep_role != next.tep_role) { - if (task_policy_update_coalition_focal_tasks(task, prev.tep_role, next.tep_role, pend_token)) - update_sfi = TRUE; + if (task_policy_update_coalition_focal_tasks(task, prev.tep_role, next.tep_role, pend_token)) { + update_sfi = true; + } } - boolean_t update_priority = FALSE; + bool update_priority = false; - int priority = BASEPRI_DEFAULT; - int max_priority = MAXPRI_USER; + int16_t priority = BASEPRI_DEFAULT; + int16_t max_priority = MAXPRI_USER; if (next.tep_lowpri_cpu) { priority = MAXPRI_THROTTLE; @@ -1127,42 +1200,43 @@ task_policy_update_internal_locked(task_t task, boolean_t in_create, task_pend_t max_priority = MAXPRI_SUPPRESSED; } else { switch (next.tep_role) { - case TASK_CONTROL_APPLICATION: - priority = BASEPRI_CONTROL; - break; - case TASK_GRAPHICS_SERVER: - priority = BASEPRI_GRAPHICS; - max_priority = MAXPRI_RESERVED; - break; - default: - break; + case TASK_CONTROL_APPLICATION: + priority = BASEPRI_CONTROL; + break; + case TASK_GRAPHICS_SERVER: + priority = BASEPRI_GRAPHICS; + max_priority = MAXPRI_RESERVED; + break; + default: + break; } /* factor in 'nice' value */ priority += task->importance; if (task->effective_policy.tep_qos_clamp != THREAD_QOS_UNSPECIFIED) { - int qos_clamp_priority = thread_qos_policy_params.qos_pri[task->effective_policy.tep_qos_clamp]; + int16_t qos_clamp_priority = thread_qos_policy_params.qos_pri[task->effective_policy.tep_qos_clamp]; priority = MIN(priority, qos_clamp_priority); max_priority = MIN(max_priority, qos_clamp_priority); } - if (priority > max_priority) + if (priority > max_priority) { priority = max_priority; - else if (priority < MINPRI) + } else if (priority < MINPRI) { priority = MINPRI; + } } assert(priority <= max_priority); /* avoid extra work if priority isn't changing */ - if (priority != task->priority || - max_priority != task->max_priority ) { + if (priority != task->priority || + max_priority != task->max_priority) { /* update the scheduling priority for the task */ task->max_priority = max_priority; task->priority = priority; - update_priority = TRUE; + update_priority = true; } /* Loop over the threads in the task: @@ -1176,13 +1250,15 @@ task_policy_update_internal_locked(task_t task, boolean_t in_create, task_pend_t queue_iterate(&task->threads, thread, thread_t, task_threads) { struct task_pend_token thread_pend_token = {}; - if (update_sfi) + if (update_sfi) { thread_pend_token.tpt_update_thread_sfi = 1; + } - if (update_priority || update_threads) + if (update_priority || update_threads) { thread_policy_update_tasklocked(thread, - task->priority, task->max_priority, - &thread_pend_token); + task->priority, task->max_priority, + &thread_pend_token); + } assert(!thread_pend_token.tpt_update_sockets); @@ -1193,10 +1269,11 @@ task_policy_update_internal_locked(task_t task, boolean_t in_create, task_pend_t /* * Use the app-nap transitions to influence the - * transition of the process within the jetsam band. + * transition of the process within the jetsam band + * [and optionally its live-donor status] * On macOS only. */ - if (memorystatus_appnap_transition == TRUE) { + if (appnap_transition) { if (task->effective_policy.tep_sup_active == 1) { memorystatus_update_priority_for_appnap(((proc_t) task->bsd_info), TRUE); } else { @@ -1211,13 +1288,13 @@ task_policy_update_internal_locked(task_t task, boolean_t in_create, task_pend_t */ static boolean_t task_policy_update_coalition_focal_tasks(task_t task, - int prev_role, - int next_role, - task_pend_token_t pend_token) + int prev_role, + int next_role, + task_pend_token_t pend_token) { boolean_t sfi_transition = FALSE; uint32_t new_count = 0; - + /* task moving into/out-of the foreground */ if (prev_role != TASK_FOREGROUND_APPLICATION && next_role == TASK_FOREGROUND_APPLICATION) { if (task_coalition_adjust_focal_count(task, 1, &new_count) && (new_count == 1)) { @@ -1233,15 +1310,18 @@ task_policy_update_coalition_focal_tasks(task_t task, /* task moving into/out-of background */ if (prev_role != TASK_BACKGROUND_APPLICATION && next_role == TASK_BACKGROUND_APPLICATION) { - if (task_coalition_adjust_nonfocal_count(task, 1, &new_count) && (new_count == 1)) + if (task_coalition_adjust_nonfocal_count(task, 1, &new_count) && (new_count == 1)) { sfi_transition = TRUE; + } } else if (prev_role == TASK_BACKGROUND_APPLICATION && next_role != TASK_BACKGROUND_APPLICATION) { - if (task_coalition_adjust_nonfocal_count(task, -1, &new_count) && (new_count == 0)) + if (task_coalition_adjust_nonfocal_count(task, -1, &new_count) && (new_count == 0)) { sfi_transition = TRUE; + } } - if (sfi_transition) - pend_token->tpt_update_coal_sfi = 1; + if (sfi_transition) { + pend_token->tpt_update_coal_sfi = 1; + } return sfi_transition; } @@ -1257,8 +1337,9 @@ task_sfi_reevaluate_cb(coalition_t coal, void *ctx, task_t task) (void)coal; /* skip the task we're re-evaluating on behalf of: it's already updated */ - if (task == (task_t)ctx) + if (task == (task_t)ctx) { return; + } task_lock(task); @@ -1277,29 +1358,39 @@ void task_policy_update_complete_unlocked(task_t task, task_pend_token_t pend_token) { #ifdef MACH_BSD - if (pend_token->tpt_update_sockets) + if (pend_token->tpt_update_sockets) { proc_apply_task_networkbg(task->bsd_info, THREAD_NULL); + } #endif /* MACH_BSD */ /* The timer throttle has been removed or reduced, we need to look for expired timers and fire them */ - if (pend_token->tpt_update_timers) + if (pend_token->tpt_update_timers) { ml_timer_evaluate(); + } -#if CONFIG_EMBEDDED - if (pend_token->tpt_update_watchers) +#if CONFIG_TASKWATCH + if (pend_token->tpt_update_watchers) { apply_appstate_watchers(task); -#endif /* CONFIG_EMBEDDED */ + } +#endif /* CONFIG_TASKWATCH */ - if (pend_token->tpt_update_live_donor) + if (pend_token->tpt_update_live_donor) { task_importance_update_live_donor(task); + } #if CONFIG_SCHED_SFI /* use the resource coalition for SFI re-evaluation */ - if (pend_token->tpt_update_coal_sfi) + if (pend_token->tpt_update_coal_sfi) { coalition_for_each_task(task->coalition[COALITION_TYPE_RESOURCE], - (void *)task, task_sfi_reevaluate_cb); + (void *)task, task_sfi_reevaluate_cb); + } #endif /* CONFIG_SCHED_SFI */ +#if CONFIG_THREAD_GROUPS + if (pend_token->tpt_update_tg_ui_flag) { + task_coalition_thread_group_focal_update(task); + } +#endif /* CONFIG_THREAD_GROUPS */ } /* @@ -1314,18 +1405,18 @@ task_policy_update_complete_unlocked(task_t task, task_pend_token_t pend_token) */ void proc_set_task_policy(task_t task, - int category, - int flavor, - int value) + int category, + int flavor, + int value) { struct task_pend_token pend_token = {}; task_lock(task); KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, - (IMPORTANCE_CODE(flavor, (category | TASK_POLICY_TASK))) | DBG_FUNC_START, - task_pid(task), trequested_0(task), - trequested_1(task), value, 0); + (IMPORTANCE_CODE(flavor, (category | TASK_POLICY_TASK))) | DBG_FUNC_START, + task_pid(task), trequested_0(task), + trequested_1(task), value, 0); proc_set_task_policy_locked(task, category, flavor, value, 0); @@ -1333,9 +1424,9 @@ proc_set_task_policy(task_t task, KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, - (IMPORTANCE_CODE(flavor, (category | TASK_POLICY_TASK))) | DBG_FUNC_END, - task_pid(task), trequested_0(task), - trequested_1(task), tpending(&pend_token), 0); + (IMPORTANCE_CODE(flavor, (category | TASK_POLICY_TASK))) | DBG_FUNC_END, + task_pid(task), trequested_0(task), + trequested_1(task), tpending(&pend_token), 0); task_unlock(task); @@ -1348,28 +1439,28 @@ proc_set_task_policy(task_t task, */ void proc_set_task_policy2(task_t task, - int category, - int flavor, - int value, - int value2) + int category, + int flavor, + int value, + int value2) { struct task_pend_token pend_token = {}; task_lock(task); KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, - (IMPORTANCE_CODE(flavor, (category | TASK_POLICY_TASK))) | DBG_FUNC_START, - task_pid(task), trequested_0(task), - trequested_1(task), value, 0); + (IMPORTANCE_CODE(flavor, (category | TASK_POLICY_TASK))) | DBG_FUNC_START, + task_pid(task), trequested_0(task), + trequested_1(task), value, 0); proc_set_task_policy_locked(task, category, flavor, value, value2); task_policy_update_locked(task, &pend_token); KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, - (IMPORTANCE_CODE(flavor, (category | TASK_POLICY_TASK))) | DBG_FUNC_END, - task_pid(task), trequested_0(task), - trequested_1(task), tpending(&pend_token), 0); + (IMPORTANCE_CODE(flavor, (category | TASK_POLICY_TASK))) | DBG_FUNC_END, + task_pid(task), trequested_0(task), + trequested_1(task), tpending(&pend_token), 0); task_unlock(task); @@ -1384,111 +1475,108 @@ proc_set_task_policy2(task_t task, */ static void proc_set_task_policy_locked(task_t task, - int category, - int flavor, - int value, - int value2) + int category, + int flavor, + int value, + int value2) { int tier, passive; struct task_requested_policy requested = task->requested_policy; switch (flavor) { - /* Category: EXTERNAL and INTERNAL */ - case TASK_POLICY_DARWIN_BG: - if (category == TASK_POLICY_EXTERNAL) - requested.trp_ext_darwinbg = value; - else - requested.trp_int_darwinbg = value; - break; + case TASK_POLICY_DARWIN_BG: + if (category == TASK_POLICY_EXTERNAL) { + requested.trp_ext_darwinbg = value; + } else { + requested.trp_int_darwinbg = value; + } + break; - case TASK_POLICY_IOPOL: - proc_iopol_to_tier(value, &tier, &passive); - if (category == TASK_POLICY_EXTERNAL) { - requested.trp_ext_iotier = tier; - requested.trp_ext_iopassive = passive; - } else { - requested.trp_int_iotier = tier; - requested.trp_int_iopassive = passive; - } - break; + case TASK_POLICY_IOPOL: + proc_iopol_to_tier(value, &tier, &passive); + if (category == TASK_POLICY_EXTERNAL) { + requested.trp_ext_iotier = tier; + requested.trp_ext_iopassive = passive; + } else { + requested.trp_int_iotier = tier; + requested.trp_int_iopassive = passive; + } + break; - case TASK_POLICY_IO: - if (category == TASK_POLICY_EXTERNAL) - requested.trp_ext_iotier = value; - else - requested.trp_int_iotier = value; - break; + case TASK_POLICY_IO: + if (category == TASK_POLICY_EXTERNAL) { + requested.trp_ext_iotier = value; + } else { + requested.trp_int_iotier = value; + } + break; - case TASK_POLICY_PASSIVE_IO: - if (category == TASK_POLICY_EXTERNAL) - requested.trp_ext_iopassive = value; - else - requested.trp_int_iopassive = value; - break; + case TASK_POLICY_PASSIVE_IO: + if (category == TASK_POLICY_EXTERNAL) { + requested.trp_ext_iopassive = value; + } else { + requested.trp_int_iopassive = value; + } + break; /* Category: INTERNAL */ - case TASK_POLICY_DARWIN_BG_IOPOL: - assert(category == TASK_POLICY_INTERNAL); - proc_iopol_to_tier(value, &tier, &passive); - requested.trp_bg_iotier = tier; - break; + case TASK_POLICY_DARWIN_BG_IOPOL: + assert(category == TASK_POLICY_INTERNAL); + proc_iopol_to_tier(value, &tier, &passive); + requested.trp_bg_iotier = tier; + break; /* Category: ATTRIBUTE */ - case TASK_POLICY_TAL: - assert(category == TASK_POLICY_ATTRIBUTE); - requested.trp_tal_enabled = value; - break; - - case TASK_POLICY_BOOST: - assert(category == TASK_POLICY_ATTRIBUTE); - requested.trp_boosted = value; - break; + case TASK_POLICY_BOOST: + assert(category == TASK_POLICY_ATTRIBUTE); + requested.trp_boosted = value; + break; - case TASK_POLICY_ROLE: - assert(category == TASK_POLICY_ATTRIBUTE); - requested.trp_role = value; - break; + case TASK_POLICY_ROLE: + assert(category == TASK_POLICY_ATTRIBUTE); + requested.trp_role = value; + break; - case TASK_POLICY_TERMINATED: - assert(category == TASK_POLICY_ATTRIBUTE); - requested.trp_terminated = value; - break; + case TASK_POLICY_TERMINATED: + assert(category == TASK_POLICY_ATTRIBUTE); + requested.trp_terminated = value; + break; - case TASK_BASE_LATENCY_QOS_POLICY: - assert(category == TASK_POLICY_ATTRIBUTE); - requested.trp_base_latency_qos = value; - break; + case TASK_BASE_LATENCY_QOS_POLICY: + assert(category == TASK_POLICY_ATTRIBUTE); + requested.trp_base_latency_qos = value; + break; - case TASK_BASE_THROUGHPUT_QOS_POLICY: - assert(category == TASK_POLICY_ATTRIBUTE); - requested.trp_base_through_qos = value; - break; + case TASK_BASE_THROUGHPUT_QOS_POLICY: + assert(category == TASK_POLICY_ATTRIBUTE); + requested.trp_base_through_qos = value; + break; - case TASK_POLICY_SFI_MANAGED: - assert(category == TASK_POLICY_ATTRIBUTE); - requested.trp_sfi_managed = value; - break; + case TASK_POLICY_SFI_MANAGED: + assert(category == TASK_POLICY_ATTRIBUTE); + requested.trp_sfi_managed = value; + break; - case TASK_POLICY_BASE_LATENCY_AND_THROUGHPUT_QOS: - assert(category == TASK_POLICY_ATTRIBUTE); - requested.trp_base_latency_qos = value; - requested.trp_base_through_qos = value2; - break; + case TASK_POLICY_BASE_LATENCY_AND_THROUGHPUT_QOS: + assert(category == TASK_POLICY_ATTRIBUTE); + requested.trp_base_latency_qos = value; + requested.trp_base_through_qos = value2; + break; - case TASK_POLICY_OVERRIDE_LATENCY_AND_THROUGHPUT_QOS: - assert(category == TASK_POLICY_ATTRIBUTE); - requested.trp_over_latency_qos = value; - requested.trp_over_through_qos = value2; - break; + case TASK_POLICY_OVERRIDE_LATENCY_AND_THROUGHPUT_QOS: + assert(category == TASK_POLICY_ATTRIBUTE); + requested.trp_over_latency_qos = value; + requested.trp_over_through_qos = value2; + break; - default: - panic("unknown task policy: %d %d %d %d", category, flavor, value, value2); - break; + default: + panic("unknown task policy: %d %d %d %d", category, flavor, value, value2); + break; } task->requested_policy = requested; @@ -1499,8 +1587,8 @@ proc_set_task_policy_locked(task_t task, */ int proc_get_task_policy(task_t task, - int category, - int flavor) + int category, + int flavor) { int value = 0; @@ -1509,47 +1597,51 @@ proc_get_task_policy(task_t task, struct task_requested_policy requested = task->requested_policy; switch (flavor) { - case TASK_POLICY_DARWIN_BG: - if (category == TASK_POLICY_EXTERNAL) - value = requested.trp_ext_darwinbg; - else - value = requested.trp_int_darwinbg; - break; - case TASK_POLICY_IOPOL: - if (category == TASK_POLICY_EXTERNAL) - value = proc_tier_to_iopol(requested.trp_ext_iotier, - requested.trp_ext_iopassive); - else - value = proc_tier_to_iopol(requested.trp_int_iotier, - requested.trp_int_iopassive); - break; - case TASK_POLICY_IO: - if (category == TASK_POLICY_EXTERNAL) - value = requested.trp_ext_iotier; - else - value = requested.trp_int_iotier; - break; - case TASK_POLICY_PASSIVE_IO: - if (category == TASK_POLICY_EXTERNAL) - value = requested.trp_ext_iopassive; - else - value = requested.trp_int_iopassive; - break; - case TASK_POLICY_DARWIN_BG_IOPOL: - assert(category == TASK_POLICY_ATTRIBUTE); - value = proc_tier_to_iopol(requested.trp_bg_iotier, 0); - break; - case TASK_POLICY_ROLE: - assert(category == TASK_POLICY_ATTRIBUTE); - value = requested.trp_role; - break; - case TASK_POLICY_SFI_MANAGED: - assert(category == TASK_POLICY_ATTRIBUTE); - value = requested.trp_sfi_managed; - break; - default: - panic("unknown policy_flavor %d", flavor); - break; + case TASK_POLICY_DARWIN_BG: + if (category == TASK_POLICY_EXTERNAL) { + value = requested.trp_ext_darwinbg; + } else { + value = requested.trp_int_darwinbg; + } + break; + case TASK_POLICY_IOPOL: + if (category == TASK_POLICY_EXTERNAL) { + value = proc_tier_to_iopol(requested.trp_ext_iotier, + requested.trp_ext_iopassive); + } else { + value = proc_tier_to_iopol(requested.trp_int_iotier, + requested.trp_int_iopassive); + } + break; + case TASK_POLICY_IO: + if (category == TASK_POLICY_EXTERNAL) { + value = requested.trp_ext_iotier; + } else { + value = requested.trp_int_iotier; + } + break; + case TASK_POLICY_PASSIVE_IO: + if (category == TASK_POLICY_EXTERNAL) { + value = requested.trp_ext_iopassive; + } else { + value = requested.trp_int_iopassive; + } + break; + case TASK_POLICY_DARWIN_BG_IOPOL: + assert(category == TASK_POLICY_INTERNAL); + value = proc_tier_to_iopol(requested.trp_bg_iotier, 0); + break; + case TASK_POLICY_ROLE: + assert(category == TASK_POLICY_ATTRIBUTE); + value = requested.trp_role; + break; + case TASK_POLICY_SFI_MANAGED: + assert(category == TASK_POLICY_ATTRIBUTE); + value = requested.trp_sfi_managed; + break; + default: + panic("unknown policy_flavor %d", flavor); + break; } task_unlock(task); @@ -1562,31 +1654,31 @@ proc_get_task_policy(task_t task, */ void proc_get_task_policy2(task_t task, - __assert_only int category, - int flavor, - int *value1, - int *value2) + __assert_only int category, + int flavor, + int *value1, + int *value2) { task_lock(task); struct task_requested_policy requested = task->requested_policy; switch (flavor) { - case TASK_POLICY_BASE_LATENCY_AND_THROUGHPUT_QOS: - assert(category == TASK_POLICY_ATTRIBUTE); - *value1 = requested.trp_base_latency_qos; - *value2 = requested.trp_base_through_qos; - break; + case TASK_POLICY_BASE_LATENCY_AND_THROUGHPUT_QOS: + assert(category == TASK_POLICY_ATTRIBUTE); + *value1 = requested.trp_base_latency_qos; + *value2 = requested.trp_base_through_qos; + break; - case TASK_POLICY_OVERRIDE_LATENCY_AND_THROUGHPUT_QOS: - assert(category == TASK_POLICY_ATTRIBUTE); - *value1 = requested.trp_over_latency_qos; - *value2 = requested.trp_over_through_qos; - break; + case TASK_POLICY_OVERRIDE_LATENCY_AND_THROUGHPUT_QOS: + assert(category == TASK_POLICY_ATTRIBUTE); + *value1 = requested.trp_over_latency_qos; + *value2 = requested.trp_over_through_qos; + break; - default: - panic("unknown policy_flavor %d", flavor); - break; + default: + panic("unknown policy_flavor %d", flavor); + break; } task_unlock(task); @@ -1606,68 +1698,76 @@ proc_get_task_policy2(task_t task, */ int proc_get_effective_task_policy(task_t task, - int flavor) + int flavor) { int value = 0; switch (flavor) { - case TASK_POLICY_DARWIN_BG: - /* - * This backs the KPI call proc_pidbackgrounded to find - * out if a pid is backgrounded. - * It is used to communicate state to the VM system, as well as - * prioritizing requests to the graphics system. - * Returns 1 for background mode, 0 for normal mode - */ - value = task->effective_policy.tep_darwinbg; - break; - case TASK_POLICY_ALL_SOCKETS_BG: - /* - * do_background_socket() calls this to determine what it should do to the proc's sockets - * Returns 1 for background mode, 0 for normal mode - * - * This consults both thread and task so un-DBGing a thread while the task is BG - * doesn't get you out of the network throttle. - */ - value = task->effective_policy.tep_all_sockets_bg; - break; - case TASK_POLICY_LATENCY_QOS: - /* - * timer arming calls into here to find out the timer coalescing level - * Returns a QoS tier (0-6) - */ - value = task->effective_policy.tep_latency_qos; - break; - case TASK_POLICY_THROUGH_QOS: - /* - * This value is passed into the urgency callout from the scheduler - * to the performance management subsystem. - * Returns a QoS tier (0-6) - */ - value = task->effective_policy.tep_through_qos; - break; - case TASK_POLICY_ROLE: - /* - * This controls various things that ask whether a process is foreground, - * like SFI, VM, access to GPU, etc - */ - value = task->effective_policy.tep_role; - break; - case TASK_POLICY_WATCHERS_BG: - /* - * This controls whether or not a thread watching this process should be BG. - */ - value = task->effective_policy.tep_watchers_bg; - break; - case TASK_POLICY_SFI_MANAGED: - /* - * This controls whether or not a process is targeted for specific control by thermald. - */ - value = task->effective_policy.tep_sfi_managed; - break; - default: - panic("unknown policy_flavor %d", flavor); - break; + case TASK_POLICY_DARWIN_BG: + /* + * This backs the KPI call proc_pidbackgrounded to find + * out if a pid is backgrounded. + * It is used to communicate state to the VM system, as well as + * prioritizing requests to the graphics system. + * Returns 1 for background mode, 0 for normal mode + */ + value = task->effective_policy.tep_darwinbg; + break; + case TASK_POLICY_ALL_SOCKETS_BG: + /* + * do_background_socket() calls this to determine what it should do to the proc's sockets + * Returns 1 for background mode, 0 for normal mode + * + * This consults both thread and task so un-DBGing a thread while the task is BG + * doesn't get you out of the network throttle. + */ + value = task->effective_policy.tep_all_sockets_bg; + break; + case TASK_POLICY_SUP_ACTIVE: + /* + * Is the task in AppNap? This is used to determine the urgency + * that's passed to the performance management subsystem for threads + * that are running at a priority <= MAXPRI_THROTTLE. + */ + value = task->effective_policy.tep_sup_active; + break; + case TASK_POLICY_LATENCY_QOS: + /* + * timer arming calls into here to find out the timer coalescing level + * Returns a QoS tier (0-6) + */ + value = task->effective_policy.tep_latency_qos; + break; + case TASK_POLICY_THROUGH_QOS: + /* + * This value is passed into the urgency callout from the scheduler + * to the performance management subsystem. + * Returns a QoS tier (0-6) + */ + value = task->effective_policy.tep_through_qos; + break; + case TASK_POLICY_ROLE: + /* + * This controls various things that ask whether a process is foreground, + * like SFI, VM, access to GPU, etc + */ + value = task->effective_policy.tep_role; + break; + case TASK_POLICY_WATCHERS_BG: + /* + * This controls whether or not a thread watching this process should be BG. + */ + value = task->effective_policy.tep_watchers_bg; + break; + case TASK_POLICY_SFI_MANAGED: + /* + * This controls whether or not a process is targeted for specific control by thermald. + */ + value = task->effective_policy.tep_sfi_managed; + break; + default: + panic("unknown policy_flavor %d", flavor); + break; } return value; @@ -1686,25 +1786,25 @@ proc_iopol_to_tier(int iopolicy, int *tier, int *passive) *passive = 0; *tier = 0; switch (iopolicy) { - case IOPOL_IMPORTANT: - *tier = THROTTLE_LEVEL_TIER0; - break; - case IOPOL_PASSIVE: - *tier = THROTTLE_LEVEL_TIER0; - *passive = 1; - break; - case IOPOL_STANDARD: - *tier = THROTTLE_LEVEL_TIER1; - break; - case IOPOL_UTILITY: - *tier = THROTTLE_LEVEL_TIER2; - break; - case IOPOL_THROTTLE: - *tier = THROTTLE_LEVEL_TIER3; - break; - default: - panic("unknown I/O policy %d", iopolicy); - break; + case IOPOL_IMPORTANT: + *tier = THROTTLE_LEVEL_TIER0; + break; + case IOPOL_PASSIVE: + *tier = THROTTLE_LEVEL_TIER0; + *passive = 1; + break; + case IOPOL_STANDARD: + *tier = THROTTLE_LEVEL_TIER1; + break; + case IOPOL_UTILITY: + *tier = THROTTLE_LEVEL_TIER2; + break; + case IOPOL_THROTTLE: + *tier = THROTTLE_LEVEL_TIER3; + break; + default: + panic("unknown I/O policy %d", iopolicy); + break; } } @@ -1713,56 +1813,59 @@ proc_tier_to_iopol(int tier, int passive) { if (passive == 1) { switch (tier) { - case THROTTLE_LEVEL_TIER0: - return IOPOL_PASSIVE; - default: - panic("unknown passive tier %d", tier); - return IOPOL_DEFAULT; + case THROTTLE_LEVEL_TIER0: + return IOPOL_PASSIVE; + default: + panic("unknown passive tier %d", tier); + return IOPOL_DEFAULT; } } else { switch (tier) { - case THROTTLE_LEVEL_NONE: - case THROTTLE_LEVEL_TIER0: - return IOPOL_DEFAULT; - case THROTTLE_LEVEL_TIER1: - return IOPOL_STANDARD; - case THROTTLE_LEVEL_TIER2: - return IOPOL_UTILITY; - case THROTTLE_LEVEL_TIER3: - return IOPOL_THROTTLE; - default: - panic("unknown tier %d", tier); - return IOPOL_DEFAULT; + case THROTTLE_LEVEL_NONE: + case THROTTLE_LEVEL_TIER0: + return IOPOL_DEFAULT; + case THROTTLE_LEVEL_TIER1: + return IOPOL_STANDARD; + case THROTTLE_LEVEL_TIER2: + return IOPOL_UTILITY; + case THROTTLE_LEVEL_TIER3: + return IOPOL_THROTTLE; + default: + panic("unknown tier %d", tier); + return IOPOL_DEFAULT; } } } int -proc_darwin_role_to_task_role(int darwin_role, int* task_role) +proc_darwin_role_to_task_role(int darwin_role, task_role_t* task_role) { integer_t role = TASK_UNSPECIFIED; switch (darwin_role) { - case PRIO_DARWIN_ROLE_DEFAULT: - role = TASK_UNSPECIFIED; - break; - case PRIO_DARWIN_ROLE_UI_FOCAL: - role = TASK_FOREGROUND_APPLICATION; - break; - case PRIO_DARWIN_ROLE_UI: - role = TASK_DEFAULT_APPLICATION; - break; - case PRIO_DARWIN_ROLE_NON_UI: - role = TASK_NONUI_APPLICATION; - break; - case PRIO_DARWIN_ROLE_UI_NON_FOCAL: - role = TASK_BACKGROUND_APPLICATION; - break; - case PRIO_DARWIN_ROLE_TAL_LAUNCH: - role = TASK_THROTTLE_APPLICATION; - break; - default: - return EINVAL; + case PRIO_DARWIN_ROLE_DEFAULT: + role = TASK_UNSPECIFIED; + break; + case PRIO_DARWIN_ROLE_UI_FOCAL: + role = TASK_FOREGROUND_APPLICATION; + break; + case PRIO_DARWIN_ROLE_UI: + role = TASK_DEFAULT_APPLICATION; + break; + case PRIO_DARWIN_ROLE_NON_UI: + role = TASK_NONUI_APPLICATION; + break; + case PRIO_DARWIN_ROLE_UI_NON_FOCAL: + role = TASK_BACKGROUND_APPLICATION; + break; + case PRIO_DARWIN_ROLE_TAL_LAUNCH: + role = TASK_THROTTLE_APPLICATION; + break; + case PRIO_DARWIN_ROLE_DARWIN_BG: + role = TASK_DARWINBG_APPLICATION; + break; + default: + return EINVAL; } *task_role = role; @@ -1771,28 +1874,31 @@ proc_darwin_role_to_task_role(int darwin_role, int* task_role) } int -proc_task_role_to_darwin_role(int task_role) +proc_task_role_to_darwin_role(task_role_t task_role) { switch (task_role) { - case TASK_FOREGROUND_APPLICATION: - return PRIO_DARWIN_ROLE_UI_FOCAL; - case TASK_BACKGROUND_APPLICATION: - return PRIO_DARWIN_ROLE_UI_NON_FOCAL; - case TASK_NONUI_APPLICATION: - return PRIO_DARWIN_ROLE_NON_UI; - case TASK_DEFAULT_APPLICATION: - return PRIO_DARWIN_ROLE_UI; - case TASK_THROTTLE_APPLICATION: - return PRIO_DARWIN_ROLE_TAL_LAUNCH; - case TASK_UNSPECIFIED: - default: - return PRIO_DARWIN_ROLE_DEFAULT; + case TASK_FOREGROUND_APPLICATION: + return PRIO_DARWIN_ROLE_UI_FOCAL; + case TASK_BACKGROUND_APPLICATION: + return PRIO_DARWIN_ROLE_UI_NON_FOCAL; + case TASK_NONUI_APPLICATION: + return PRIO_DARWIN_ROLE_NON_UI; + case TASK_DEFAULT_APPLICATION: + return PRIO_DARWIN_ROLE_UI; + case TASK_THROTTLE_APPLICATION: + return PRIO_DARWIN_ROLE_TAL_LAUNCH; + case TASK_DARWINBG_APPLICATION: + return PRIO_DARWIN_ROLE_DARWIN_BG; + case TASK_UNSPECIFIED: + default: + return PRIO_DARWIN_ROLE_DEFAULT; } } /* TODO: remove this variable when interactive daemon audit period is over */ -extern boolean_t ipc_importance_interactive_receiver; +static TUNABLE(bool, ipc_importance_interactive_receiver, + "imp_interactive_receiver", false); /* * Called at process exec to initialize the apptype, qos clamp, and qos seed of a process @@ -1800,78 +1906,84 @@ extern boolean_t ipc_importance_interactive_receiver; * TODO: Make this function more table-driven instead of ad-hoc */ void -proc_set_task_spawnpolicy(task_t task, int apptype, int qos_clamp, int role, - ipc_port_t * portwatch_ports, int portwatch_count) +proc_set_task_spawnpolicy(task_t task, thread_t thread, int apptype, int qos_clamp, task_role_t role, + ipc_port_t * portwatch_ports, uint32_t portwatch_count) { struct task_pend_token pend_token = {}; KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, - (IMPORTANCE_CODE(IMP_TASK_APPTYPE, apptype)) | DBG_FUNC_START, - task_pid(task), trequested_0(task), trequested_1(task), - apptype, 0); + (IMPORTANCE_CODE(IMP_TASK_APPTYPE, apptype)) | DBG_FUNC_START, + task_pid(task), trequested_0(task), trequested_1(task), + apptype, 0); switch (apptype) { - case TASK_APPTYPE_APP_TAL: - case TASK_APPTYPE_APP_DEFAULT: - /* Apps become donors via the 'live-donor' flag instead of the static donor flag */ - task_importance_mark_donor(task, FALSE); - task_importance_mark_live_donor(task, TRUE); - task_importance_mark_receiver(task, FALSE); -#if CONFIG_EMBEDDED - task_importance_mark_denap_receiver(task, FALSE); + case TASK_APPTYPE_APP_DEFAULT: + /* Apps become donors via the 'live-donor' flag instead of the static donor flag */ + task_importance_mark_donor(task, FALSE); + task_importance_mark_live_donor(task, TRUE); + task_importance_mark_receiver(task, FALSE); +#if !defined(XNU_TARGET_OS_OSX) + task_importance_mark_denap_receiver(task, FALSE); #else - /* Apps are de-nap recievers on desktop for suppression behaviors */ - task_importance_mark_denap_receiver(task, TRUE); -#endif /* CONFIG_EMBEDDED */ - break; + /* Apps are de-nap recievers on macOS for suppression behaviors */ + task_importance_mark_denap_receiver(task, TRUE); +#endif /* !defined(XNU_TARGET_OS_OSX) */ + break; - case TASK_APPTYPE_DAEMON_INTERACTIVE: - task_importance_mark_donor(task, TRUE); - task_importance_mark_live_donor(task, FALSE); - - /* - * A boot arg controls whether interactive daemons are importance receivers. - * Normally, they are not. But for testing their behavior as an adaptive - * daemon, the boot-arg can be set. - * - * TODO: remove this when the interactive daemon audit period is over. - */ - task_importance_mark_receiver(task, /* FALSE */ ipc_importance_interactive_receiver); - task_importance_mark_denap_receiver(task, FALSE); - break; + case TASK_APPTYPE_DAEMON_INTERACTIVE: + task_importance_mark_donor(task, TRUE); + task_importance_mark_live_donor(task, FALSE); - case TASK_APPTYPE_DAEMON_STANDARD: - task_importance_mark_donor(task, TRUE); - task_importance_mark_live_donor(task, FALSE); - task_importance_mark_receiver(task, FALSE); - task_importance_mark_denap_receiver(task, FALSE); - break; + /* + * A boot arg controls whether interactive daemons are importance receivers. + * Normally, they are not. But for testing their behavior as an adaptive + * daemon, the boot-arg can be set. + * + * TODO: remove this when the interactive daemon audit period is over. + */ + task_importance_mark_receiver(task, /* FALSE */ ipc_importance_interactive_receiver); + task_importance_mark_denap_receiver(task, FALSE); + break; - case TASK_APPTYPE_DAEMON_ADAPTIVE: - task_importance_mark_donor(task, FALSE); - task_importance_mark_live_donor(task, FALSE); - task_importance_mark_receiver(task, TRUE); - task_importance_mark_denap_receiver(task, FALSE); - break; + case TASK_APPTYPE_DAEMON_STANDARD: + task_importance_mark_donor(task, TRUE); + task_importance_mark_live_donor(task, FALSE); + task_importance_mark_receiver(task, FALSE); + task_importance_mark_denap_receiver(task, FALSE); + break; - case TASK_APPTYPE_DAEMON_BACKGROUND: - task_importance_mark_donor(task, FALSE); - task_importance_mark_live_donor(task, FALSE); - task_importance_mark_receiver(task, FALSE); - task_importance_mark_denap_receiver(task, FALSE); - break; + case TASK_APPTYPE_DAEMON_ADAPTIVE: + task_importance_mark_donor(task, FALSE); + task_importance_mark_live_donor(task, FALSE); + task_importance_mark_receiver(task, TRUE); + task_importance_mark_denap_receiver(task, FALSE); + break; - case TASK_APPTYPE_NONE: - break; + case TASK_APPTYPE_DAEMON_BACKGROUND: + task_importance_mark_donor(task, FALSE); + task_importance_mark_live_donor(task, FALSE); + task_importance_mark_receiver(task, FALSE); + task_importance_mark_denap_receiver(task, FALSE); + break; + + case TASK_APPTYPE_DRIVER: + task_importance_mark_donor(task, FALSE); + task_importance_mark_live_donor(task, FALSE); + task_importance_mark_receiver(task, FALSE); + task_importance_mark_denap_receiver(task, FALSE); + break; + + case TASK_APPTYPE_NONE: + break; } if (portwatch_ports != NULL && apptype == TASK_APPTYPE_DAEMON_ADAPTIVE) { int portwatch_boosts = 0; - for (int i = 0; i < portwatch_count; i++) { + for (uint32_t i = 0; i < portwatch_count; i++) { ipc_port_t port = NULL; - if ((port = portwatch_ports[i]) != NULL) { + if (IP_VALID(port = portwatch_ports[i])) { int boost = 0; task_add_importance_watchport(task, port, &boost); portwatch_boosts += boost; @@ -1883,25 +1995,25 @@ proc_set_task_spawnpolicy(task_t task, int apptype, int qos_clamp, int role, } } - task_lock(task); - - if (apptype == TASK_APPTYPE_APP_TAL) { - /* TAL starts off enabled by default */ - task->requested_policy.trp_tal_enabled = 1; + /* Redirect the turnstile push of watchports to task */ + if (portwatch_count && portwatch_ports != NULL) { + task_add_turnstile_watchports(task, thread, portwatch_ports, portwatch_count); } + task_lock(task); + if (apptype != TASK_APPTYPE_NONE) { task->requested_policy.trp_apptype = apptype; } -#if CONFIG_EMBEDDED +#if !defined(XNU_TARGET_OS_OSX) /* Remove this after launchd starts setting it properly */ if (apptype == TASK_APPTYPE_APP_DEFAULT && role == TASK_UNSPECIFIED) { task->requested_policy.trp_role = TASK_FOREGROUND_APPLICATION; } else #endif if (role != TASK_UNSPECIFIED) { - task->requested_policy.trp_role = role; + task->requested_policy.trp_role = (uint32_t)role; } if (qos_clamp != THREAD_QOS_UNSPECIFIED) { @@ -1918,9 +2030,9 @@ proc_set_task_spawnpolicy(task_t task, int apptype, int qos_clamp, int role, task_policy_update_complete_unlocked(task, &pend_token); KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, - (IMPORTANCE_CODE(IMP_TASK_APPTYPE, apptype)) | DBG_FUNC_END, - task_pid(task), trequested_0(task), trequested_1(task), - task_is_importance_receiver(task), 0); + (IMPORTANCE_CODE(IMP_TASK_APPTYPE, apptype)) | DBG_FUNC_END, + task_pid(task), trequested_0(task), trequested_1(task), + task_is_importance_receiver(task), 0); } /* @@ -1928,7 +2040,7 @@ proc_set_task_spawnpolicy(task_t task, int apptype, int qos_clamp, int role, */ void proc_inherit_task_role(task_t new_task, - task_t old_task) + task_t old_task) { int role; @@ -1937,33 +2049,34 @@ proc_inherit_task_role(task_t new_task, proc_set_task_policy(new_task, TASK_POLICY_ATTRIBUTE, TASK_POLICY_ROLE, role); } -extern void *initproc; +extern void * XNU_PTRAUTH_SIGNED_PTR("initproc") initproc; /* * Compute the default main thread qos for a task */ -int +thread_qos_t task_compute_main_thread_qos(task_t task) { - int primordial_qos = THREAD_QOS_UNSPECIFIED; + thread_qos_t primordial_qos = THREAD_QOS_UNSPECIFIED; - int qos_clamp = task->requested_policy.trp_qos_clamp; + thread_qos_t qos_clamp = task->requested_policy.trp_qos_clamp; switch (task->requested_policy.trp_apptype) { - case TASK_APPTYPE_APP_TAL: - case TASK_APPTYPE_APP_DEFAULT: - primordial_qos = THREAD_QOS_USER_INTERACTIVE; - break; + case TASK_APPTYPE_APP_TAL: + case TASK_APPTYPE_APP_DEFAULT: + primordial_qos = THREAD_QOS_USER_INTERACTIVE; + break; - case TASK_APPTYPE_DAEMON_INTERACTIVE: - case TASK_APPTYPE_DAEMON_STANDARD: - case TASK_APPTYPE_DAEMON_ADAPTIVE: - primordial_qos = THREAD_QOS_LEGACY; - break; + case TASK_APPTYPE_DAEMON_INTERACTIVE: + case TASK_APPTYPE_DAEMON_STANDARD: + case TASK_APPTYPE_DAEMON_ADAPTIVE: + case TASK_APPTYPE_DRIVER: + primordial_qos = THREAD_QOS_LEGACY; + break; - case TASK_APPTYPE_DAEMON_BACKGROUND: - primordial_qos = THREAD_QOS_BACKGROUND; - break; + case TASK_APPTYPE_DAEMON_BACKGROUND: + primordial_qos = THREAD_QOS_BACKGROUND; + break; } if (task->bsd_info == initproc) { @@ -2010,6 +2123,15 @@ task_is_daemon(task_t task) } } +bool +task_is_driver(task_t task) +{ + if (!task) { + return FALSE; + } + return task->requested_policy.trp_apptype == TASK_APPTYPE_DRIVER; +} + boolean_t task_is_app(task_t task) { @@ -2033,38 +2155,47 @@ task_grab_latency_qos(task_t task) int proc_get_darwinbgstate(task_t task, uint32_t * flagsp) { - if (task->requested_policy.trp_ext_darwinbg) + if (task->requested_policy.trp_ext_darwinbg) { *flagsp |= PROC_FLAG_EXT_DARWINBG; + } - if (task->requested_policy.trp_int_darwinbg) + if (task->requested_policy.trp_int_darwinbg) { *flagsp |= PROC_FLAG_DARWINBG; + } -#if CONFIG_EMBEDDED - if (task->requested_policy.trp_apptype == TASK_APPTYPE_DAEMON_BACKGROUND) +#if !defined(XNU_TARGET_OS_OSX) + if (task->requested_policy.trp_apptype == TASK_APPTYPE_DAEMON_BACKGROUND) { *flagsp |= PROC_FLAG_IOS_APPLEDAEMON; + } - if (task->requested_policy.trp_apptype == TASK_APPTYPE_DAEMON_ADAPTIVE) + if (task->requested_policy.trp_apptype == TASK_APPTYPE_DAEMON_ADAPTIVE) { *flagsp |= PROC_FLAG_IOS_IMPPROMOTION; -#endif /* CONFIG_EMBEDDED */ + } +#endif /* !defined(XNU_TARGET_OS_OSX) */ if (task->requested_policy.trp_apptype == TASK_APPTYPE_APP_DEFAULT || - task->requested_policy.trp_apptype == TASK_APPTYPE_APP_TAL) + task->requested_policy.trp_apptype == TASK_APPTYPE_APP_TAL) { *flagsp |= PROC_FLAG_APPLICATION; + } - if (task->requested_policy.trp_apptype == TASK_APPTYPE_DAEMON_ADAPTIVE) + if (task->requested_policy.trp_apptype == TASK_APPTYPE_DAEMON_ADAPTIVE) { *flagsp |= PROC_FLAG_ADAPTIVE; + } if (task->requested_policy.trp_apptype == TASK_APPTYPE_DAEMON_ADAPTIVE && - task->requested_policy.trp_boosted == 1) + task->requested_policy.trp_boosted == 1) { *flagsp |= PROC_FLAG_ADAPTIVE_IMPORTANT; + } - if (task_is_importance_donor(task)) + if (task_is_importance_donor(task)) { *flagsp |= PROC_FLAG_IMPORTANCE_DONOR; + } - if (task->effective_policy.tep_sup_active) + if (task->effective_policy.tep_sup_active) { *flagsp |= PROC_FLAG_SUPPRESSED; + } - return(0); + return 0; } /* @@ -2075,7 +2206,7 @@ proc_get_darwinbgstate(task_t task, uint32_t * flagsp) * given tracepoint will emit either requested or effective data, but not both. * * A tracepoint may emit any of task, thread, or task & thread data. - * + * * The type of data emitted varies with pointer size. Where possible, both * task and thread data are emitted. In LP32 systems, the first and second * halves of either the task or thread data is emitted. @@ -2152,7 +2283,11 @@ teffective_1(task_t task) } /* dump pending for tracepoint */ -uint32_t tpending(task_pend_token_t pend_token) { return *(uint32_t*)(void*)(pend_token); } +uint32_t +tpending(task_pend_token_t pend_token) +{ + return *(uint32_t*)(void*)(pend_token); +} uint64_t task_requested_bitfield(task_t task) @@ -2166,19 +2301,19 @@ task_requested_bitfield(task_t task) bits |= (requested.trp_ext_iotier ? (((uint64_t)requested.trp_ext_iotier) << POLICY_REQ_EXT_IO_TIER_SHIFT) : 0); bits |= (requested.trp_int_iopassive ? POLICY_REQ_INT_PASSIVE_IO : 0); bits |= (requested.trp_ext_iopassive ? POLICY_REQ_EXT_PASSIVE_IO : 0); - bits |= (requested.trp_bg_iotier ? (((uint64_t)requested.trp_bg_iotier) << POLICY_REQ_BG_IOTIER_SHIFT) : 0); + bits |= (requested.trp_bg_iotier ? (((uint64_t)requested.trp_bg_iotier) << POLICY_REQ_BG_IOTIER_SHIFT) : 0); bits |= (requested.trp_terminated ? POLICY_REQ_TERMINATED : 0); bits |= (requested.trp_boosted ? POLICY_REQ_BOOSTED : 0); bits |= (requested.trp_tal_enabled ? POLICY_REQ_TAL_ENABLED : 0); bits |= (requested.trp_apptype ? (((uint64_t)requested.trp_apptype) << POLICY_REQ_APPTYPE_SHIFT) : 0); - bits |= (requested.trp_role ? (((uint64_t)requested.trp_role) << POLICY_REQ_ROLE_SHIFT) : 0); + bits |= (requested.trp_role ? (((uint64_t)requested.trp_role) << POLICY_REQ_ROLE_SHIFT) : 0); bits |= (requested.trp_sup_active ? POLICY_REQ_SUP_ACTIVE : 0); bits |= (requested.trp_sup_lowpri_cpu ? POLICY_REQ_SUP_LOWPRI_CPU : 0); bits |= (requested.trp_sup_cpu ? POLICY_REQ_SUP_CPU : 0); - bits |= (requested.trp_sup_timer ? (((uint64_t)requested.trp_sup_timer) << POLICY_REQ_SUP_TIMER_THROTTLE_SHIFT) : 0); - bits |= (requested.trp_sup_throughput ? (((uint64_t)requested.trp_sup_throughput) << POLICY_REQ_SUP_THROUGHPUT_SHIFT) : 0); + bits |= (requested.trp_sup_timer ? (((uint64_t)requested.trp_sup_timer) << POLICY_REQ_SUP_TIMER_THROTTLE_SHIFT) : 0); + bits |= (requested.trp_sup_throughput ? (((uint64_t)requested.trp_sup_throughput) << POLICY_REQ_SUP_THROUGHPUT_SHIFT) : 0); bits |= (requested.trp_sup_disk ? POLICY_REQ_SUP_DISK_THROTTLE : 0); bits |= (requested.trp_sup_bg_sockets ? POLICY_REQ_SUP_BG_SOCKETS : 0); @@ -2187,7 +2322,7 @@ task_requested_bitfield(task_t task) bits |= (requested.trp_base_through_qos ? (((uint64_t)requested.trp_base_through_qos) << POLICY_REQ_BASE_THROUGH_QOS_SHIFT) : 0); bits |= (requested.trp_over_through_qos ? (((uint64_t)requested.trp_over_through_qos) << POLICY_REQ_OVER_THROUGH_QOS_SHIFT) : 0); bits |= (requested.trp_sfi_managed ? POLICY_REQ_SFI_MANAGED : 0); - bits |= (requested.trp_qos_clamp ? (((uint64_t)requested.trp_qos_clamp) << POLICY_REQ_QOS_CLAMP_SHIFT) : 0); + bits |= (requested.trp_qos_clamp ? (((uint64_t)requested.trp_qos_clamp) << POLICY_REQ_QOS_CLAMP_SHIFT) : 0); return bits; } @@ -2212,7 +2347,7 @@ task_effective_bitfield(task_t task) bits |= (effective.tep_watchers_bg ? POLICY_EFF_WATCHERS_BG : 0); bits |= (effective.tep_sup_active ? POLICY_EFF_SUP_ACTIVE : 0); bits |= (effective.tep_suppressed_cpu ? POLICY_EFF_SUP_CPU : 0); - bits |= (effective.tep_role ? (((uint64_t)effective.tep_role) << POLICY_EFF_ROLE_SHIFT) : 0); + bits |= (effective.tep_role ? (((uint64_t)effective.tep_role) << POLICY_EFF_ROLE_SHIFT) : 0); bits |= (effective.tep_latency_qos ? (((uint64_t)effective.tep_latency_qos) << POLICY_EFF_LATENCY_QOS_SHIFT) : 0); bits |= (effective.tep_through_qos ? (((uint64_t)effective.tep_through_qos) << POLICY_EFF_THROUGH_QOS_SHIFT) : 0); bits |= (effective.tep_sfi_managed ? POLICY_EFF_SFI_MANAGED : 0); @@ -2226,16 +2361,15 @@ task_effective_bitfield(task_t task) * Resource usage and CPU related routines */ -int +int proc_get_task_ruse_cpu(task_t task, uint32_t *policyp, uint8_t *percentagep, uint64_t *intervalp, uint64_t *deadlinep) { - int error = 0; int scope; task_lock(task); - + error = task_get_cpuusage(task, percentagep, intervalp, deadlinep, &scope); task_unlock(task); @@ -2250,7 +2384,7 @@ proc_get_task_ruse_cpu(task_t task, uint32_t *policyp, uint8_t *percentagep, uin *policyp = TASK_POLICY_RESOURCE_ATTRIBUTE_NONE; } - return(error); + return error; } /* @@ -2268,13 +2402,11 @@ proc_init_cpumon_params(void) * device tree. */ if (!PE_parse_boot_argn("max_cpumon_percentage", &proc_max_cpumon_percentage, - sizeof (proc_max_cpumon_percentage))) - { + sizeof(proc_max_cpumon_percentage))) { uint64_t max_percentage = 0ULL; if (!PE_get_default("kern.max_cpumon_percentage", &max_percentage, - sizeof(max_percentage))) - { + sizeof(max_percentage))) { max_percentage = DEFAULT_CPUMON_PERCENTAGE; } @@ -2293,11 +2425,9 @@ proc_init_cpumon_params(void) * via boot-args and the device tree. */ if (!PE_parse_boot_argn("max_cpumon_interval", &proc_max_cpumon_interval, - sizeof (proc_max_cpumon_interval))) - { + sizeof(proc_max_cpumon_interval))) { if (!PE_get_default("kern.max_cpumon_interval", &proc_max_cpumon_interval, - sizeof(proc_max_cpumon_interval))) - { + sizeof(proc_max_cpumon_interval))) { proc_max_cpumon_interval = DEFAULT_CPUMON_INTERVAL; } } @@ -2305,9 +2435,14 @@ proc_init_cpumon_params(void) proc_max_cpumon_interval *= NSEC_PER_SEC; /* TEMPORARY boot arg to control App suppression */ - PE_parse_boot_argn("task_policy_suppression_disable", - &task_policy_suppression_disable, - sizeof(task_policy_suppression_disable)); + PE_parse_boot_argn("task_policy_suppression_flags", + &task_policy_suppression_flags, + sizeof(task_policy_suppression_flags)); + + /* adjust suppression disk policy if called for in boot arg */ + if (task_policy_suppression_flags & TASK_POLICY_SUPPRESSION_IOTIER2) { + proc_suppressed_disk_tier = THROTTLE_LEVEL_TIER2; + } } /* @@ -2337,41 +2472,44 @@ proc_init_cpumon_params(void) * Currently, requesting notification via an exception is the only way to get per-thread scope for a * CPU limit. All other types of notifications force task-wide scope for the limit. */ -int -proc_set_task_ruse_cpu(task_t task, uint32_t policy, uint8_t percentage, uint64_t interval, uint64_t deadline, - int cpumon_entitled) +int +proc_set_task_ruse_cpu(task_t task, uint16_t policy, uint8_t percentage, uint64_t interval, uint64_t deadline, + int cpumon_entitled) { int error = 0; int scope; - /* - * Enforce the matrix of supported configurations for policy, percentage, and deadline. - */ - switch (policy) { - // If no policy is explicitly given, the default is to throttle. - case TASK_POLICY_RESOURCE_ATTRIBUTE_NONE: + /* + * Enforce the matrix of supported configurations for policy, percentage, and deadline. + */ + switch (policy) { + // If no policy is explicitly given, the default is to throttle. + case TASK_POLICY_RESOURCE_ATTRIBUTE_NONE: case TASK_POLICY_RESOURCE_ATTRIBUTE_THROTTLE: - if (deadline != 0) - return (ENOTSUP); + if (deadline != 0) { + return ENOTSUP; + } scope = TASK_RUSECPU_FLAGS_PROC_LIMIT; break; case TASK_POLICY_RESOURCE_ATTRIBUTE_SUSPEND: case TASK_POLICY_RESOURCE_ATTRIBUTE_TERMINATE: case TASK_POLICY_RESOURCE_ATTRIBUTE_NOTIFY_KQ: - if (percentage != 0) - return (ENOTSUP); + if (percentage != 0) { + return ENOTSUP; + } scope = TASK_RUSECPU_FLAGS_DEADLINE; break; - case TASK_POLICY_RESOURCE_ATTRIBUTE_NOTIFY_EXC: - if (deadline != 0) - return (ENOTSUP); + case TASK_POLICY_RESOURCE_ATTRIBUTE_NOTIFY_EXC: + if (deadline != 0) { + return ENOTSUP; + } scope = TASK_RUSECPU_FLAGS_PERTHR_LIMIT; #ifdef CONFIG_NOMONITORS - return (error); + return error; #endif /* CONFIG_NOMONITORS */ break; default: - return (EINVAL); + return EINVAL; } task_lock(task); @@ -2382,7 +2520,7 @@ proc_set_task_ruse_cpu(task_t task, uint32_t policy, uint8_t percentage, uint64_ } error = task_set_cpuusage(task, percentage, interval, deadline, scope, cpumon_entitled); task_unlock(task); - return(error); + return error; } /* TODO: get rid of these */ @@ -2395,7 +2533,7 @@ proc_set_task_ruse_cpu(task_t task, uint32_t policy, uint8_t percentage, uint64_ #define TASK_POLICY_RESOURCE_USAGE_COUNT 6 -int +int proc_clear_task_ruse_cpu(task_t task, int cpumon_entitled) { int error = 0; @@ -2410,8 +2548,9 @@ proc_clear_task_ruse_cpu(task_t task, int cpumon_entitled) } error = task_clear_cpuusage_locked(task, cpumon_entitled); - if (error != 0) - goto out; + if (error != 0) { + goto out; + } action = task->applied_ru_cpu; if (task->applied_ru_cpu_ext != TASK_POLICY_RESOURCE_ATTRIBUTE_NONE) { @@ -2428,8 +2567,7 @@ proc_clear_task_ruse_cpu(task_t task, int cpumon_entitled) out: task_unlock(task); out1: - return(error); - + return error; } /* used to apply resource limit related actions */ @@ -2438,24 +2576,25 @@ task_apply_resource_actions(task_t task, int type) { int action = TASK_POLICY_RESOURCE_ATTRIBUTE_NONE; void * bsdinfo = NULL; - + switch (type) { - case TASK_POLICY_CPU_RESOURCE_USAGE: - break; - case TASK_POLICY_WIREDMEM_RESOURCE_USAGE: - case TASK_POLICY_VIRTUALMEM_RESOURCE_USAGE: - case TASK_POLICY_DISK_RESOURCE_USAGE: - case TASK_POLICY_NETWORK_RESOURCE_USAGE: - case TASK_POLICY_POWER_RESOURCE_USAGE: - return(0); + case TASK_POLICY_CPU_RESOURCE_USAGE: + break; + case TASK_POLICY_WIREDMEM_RESOURCE_USAGE: + case TASK_POLICY_VIRTUALMEM_RESOURCE_USAGE: + case TASK_POLICY_DISK_RESOURCE_USAGE: + case TASK_POLICY_NETWORK_RESOURCE_USAGE: + case TASK_POLICY_POWER_RESOURCE_USAGE: + return 0; - default: - return(1); - }; + default: + return 1; + } + ; /* only cpu actions for now */ task_lock(task); - + if (task->applied_ru_cpu_ext == TASK_POLICY_RESOURCE_ATTRIBUTE_NONE) { /* apply action */ task->applied_ru_cpu_ext = task->policy_ru_cpu_ext; @@ -2468,10 +2607,11 @@ task_apply_resource_actions(task_t task, int type) bsdinfo = task->bsd_info; task_unlock(task); proc_apply_resource_actions(bsdinfo, TASK_POLICY_CPU_RESOURCE_USAGE, action); - } else + } else { task_unlock(task); + } - return(0); + return 0; } /* @@ -2502,7 +2642,7 @@ task_get_cpuusage(task_t task, uint8_t *percentagep, uint64_t *intervalp, uint64 *scope = 0; } - return(0); + return 0; } /* @@ -2513,7 +2653,7 @@ int task_suspend_cpumon(task_t task) { thread_t thread; - + task_lock_assert_owned(task); if ((task->rusage_cpu_flags & TASK_RUSECPU_FLAGS_PERTHR_LIMIT) == 0) { @@ -2531,9 +2671,9 @@ task_suspend_cpumon(task_t task) /* * Suspend monitoring for the task, and propagate that change to each thread. */ - task->rusage_cpu_flags &= ~(TASK_RUSECPU_FLAGS_PERTHR_LIMIT | TASK_RUSECPU_FLAGS_FATAL_CPUMON); + task->rusage_cpu_flags &= ~(TASK_RUSECPU_FLAGS_PERTHR_LIMIT | TASK_RUSECPU_FLAGS_FATAL_CPUMON); queue_iterate(&task->threads, thread, thread_t, task_threads) { - set_astledger(thread); + act_set_astledger(thread); } return KERN_SUCCESS; @@ -2550,13 +2690,15 @@ task_disable_cpumon(task_t task) task_lock_assert_owned(task); kret = task_suspend_cpumon(task); - if (kret) return kret; + if (kret) { + return kret; + } /* Once we clear these values, the monitor can't be resumed */ task->rusage_cpu_perthr_percentage = 0; task->rusage_cpu_perthr_interval = 0; - return (KERN_SUCCESS); + return KERN_SUCCESS; } @@ -2567,13 +2709,13 @@ task_enable_cpumon_locked(task_t task) task_lock_assert_owned(task); if (task->rusage_cpu_perthr_percentage == 0 || - task->rusage_cpu_perthr_interval == 0) { + task->rusage_cpu_perthr_interval == 0) { return KERN_INVALID_ARGUMENT; } task->rusage_cpu_flags |= TASK_RUSECPU_FLAGS_PERTHR_LIMIT; queue_iterate(&task->threads, thread, thread_t, task_threads) { - set_astledger(thread); + act_set_astledger(thread); } return KERN_SUCCESS; @@ -2597,8 +2739,8 @@ task_resume_cpumon(task_t task) /* duplicate values from bsd/sys/process_policy.h */ -#define PROC_POLICY_CPUMON_DISABLE 0xFF -#define PROC_POLICY_CPUMON_DEFAULTS 0xFE +#define PROC_POLICY_CPUMON_DISABLE 0xFF +#define PROC_POLICY_CPUMON_DEFAULTS 0xFE static int task_set_cpuusage(task_t task, uint8_t percentage, uint64_t interval, uint64_t deadline, int scope, int cpumon_entitled) @@ -2609,8 +2751,9 @@ task_set_cpuusage(task_t task, uint8_t percentage, uint64_t interval, uint64_t d lck_mtx_assert(&task->lock, LCK_MTX_ASSERT_OWNED); /* By default, refill once per second */ - if (interval == 0) + if (interval == 0) { interval = NSEC_PER_SEC; + } if (percentage != 0) { if (scope == TASK_RUSECPU_FLAGS_PERTHR_LIMIT) { @@ -2634,7 +2777,7 @@ task_set_cpuusage(task_t task, uint8_t percentage, uint64_t interval, uint64_t d * missing the required entitlement: * com.apple.private.kernel.override-cpumon * - * Instead, treat this as a request to reset its params + * Instead, treat this as a request to reset its params * back to the defaults. */ warn = TRUE; @@ -2655,10 +2798,10 @@ task_set_cpuusage(task_t task, uint8_t percentage, uint64_t interval, uint64_t d * - Leave the interval as-is, if there's already a per-thread * limit configured * - Use the system default. - */ + */ if (interval == -1ULL) { if (task->rusage_cpu_flags & TASK_RUSECPU_FLAGS_PERTHR_LIMIT) { - interval = task->rusage_cpu_perthr_interval; + interval = task->rusage_cpu_perthr_interval; } else { interval = proc_max_cpumon_interval; } @@ -2668,18 +2811,18 @@ task_set_cpuusage(task_t task, uint8_t percentage, uint64_t interval, uint64_t d * Enforce global caps on CPU usage monitor here if the process is not * entitled to escape the global caps. */ - if ((percentage > proc_max_cpumon_percentage) && (cpumon_entitled == 0)) { + if ((percentage > proc_max_cpumon_percentage) && (cpumon_entitled == 0)) { warn = TRUE; - percentage = proc_max_cpumon_percentage; - } + percentage = proc_max_cpumon_percentage; + } - if ((interval > proc_max_cpumon_interval) && (cpumon_entitled == 0)) { + if ((interval > proc_max_cpumon_interval) && (cpumon_entitled == 0)) { warn = TRUE; - interval = proc_max_cpumon_interval; - } + interval = proc_max_cpumon_interval; + } if (warn) { - int pid = 0; + int pid = 0; const char *procname = "unknown"; #ifdef MACH_BSD @@ -2690,7 +2833,7 @@ task_set_cpuusage(task_t task, uint8_t percentage, uint64_t interval, uint64_t d #endif printf("process %s[%d] denied attempt to escape CPU monitor" - " (missing required entitlement).\n", procname, pid); + " (missing required entitlement).\n", procname, pid); } /* configure the limit values */ @@ -2743,7 +2886,7 @@ task_set_cpuusage(task_t task, uint8_t percentage, uint64_t interval, uint64_t d } } - return(0); + return 0; } int @@ -2755,7 +2898,7 @@ task_clear_cpuusage(task_t task, int cpumon_entitled) retval = task_clear_cpuusage_locked(task, cpumon_entitled); task_unlock(task); - return(retval); + return retval; } static int @@ -2765,7 +2908,7 @@ task_clear_cpuusage_locked(task_t task, int cpumon_entitled) /* cancel percentage handling if set */ if (task->rusage_cpu_flags & TASK_RUSECPU_FLAGS_PROC_LIMIT) { - task->rusage_cpu_flags &= ~TASK_RUSECPU_FLAGS_PROC_LIMIT; + task->rusage_cpu_flags &= ~TASK_RUSECPU_FLAGS_PROC_LIMIT; ledger_set_limit(task->ledger, task_ledgers.cpu_time, LEDGER_LIMIT_INFINITY, 0); task->rusage_cpu_percentage = 0; task->rusage_cpu_interval = 0; @@ -2791,7 +2934,7 @@ task_clear_cpuusage_locked(task_t task, int cpumon_entitled) task_lock(task); } } - return(0); + return 0; } /* called by ledger unit to enforce action due to resource usage criteria being met */ @@ -2808,15 +2951,9 @@ task_action_cpuusage(thread_call_param_t param0, __unused thread_call_param_t pa * Routines for taskwatch and pidbind */ -#if CONFIG_EMBEDDED - -lck_mtx_t task_watch_mtx; +#if CONFIG_TASKWATCH -void -task_watch_init(void) -{ - lck_mtx_init(&task_watch_mtx, &task_lck_grp, &task_lck_attr); -} +LCK_MTX_DECLARE_ATTR(task_watch_mtx, &task_lck_grp, &task_lck_attr); static void task_watch_lock(void) @@ -2835,7 +2972,6 @@ add_taskwatch_locked(task_t task, task_watch_t * twp) { queue_enter(&task->task_watchers, twp, task_watch_t *, tw_links); task->num_taskwatchers++; - } static void @@ -2846,7 +2982,7 @@ remove_taskwatch_locked(task_t task, task_watch_t * twp) } -int +int proc_lf_pidbind(task_t curtask, uint64_t tid, task_t target_task, int bind) { thread_t target_thread = NULL; @@ -2855,8 +2991,9 @@ proc_lf_pidbind(task_t curtask, uint64_t tid, task_t target_task, int bind) task_t task = TASK_NULL; target_thread = task_findtid(curtask, tid); - if (target_thread == NULL) + if (target_thread == NULL) { return ESRCH; + } /* holds thread reference */ if (bind != 0) { @@ -2880,7 +3017,7 @@ proc_lf_pidbind(task_t curtask, uint64_t tid, task_t target_task, int bind) task_watch_lock(); - if (target_thread->taskwatch != NULL){ + if (target_thread->taskwatch != NULL) { /* already bound to another task */ task_watch_unlock(); @@ -2893,24 +3030,25 @@ proc_lf_pidbind(task_t curtask, uint64_t tid, task_t target_task, int bind) setbg = proc_get_effective_task_policy(target_task, TASK_POLICY_WATCHERS_BG); - twp->tw_task = target_task; /* holds the task reference */ - twp->tw_thread = target_thread; /* holds the thread reference */ + twp->tw_task = target_task; /* holds the task reference */ + twp->tw_thread = target_thread; /* holds the thread reference */ twp->tw_state = setbg; twp->tw_importance = target_thread->importance; - + add_taskwatch_locked(target_task, twp); target_thread->taskwatch = twp; task_watch_unlock(); - if (setbg) + if (setbg) { set_thread_appbg(target_thread, setbg, INT_MIN); + } /* retain the thread reference as it is in twp */ target_thread = NULL; } else { - /* unbind */ + /* unbind */ task_watch_lock(); if ((twp = target_thread->taskwatch) != NULL) { task = twp->tw_task; @@ -2919,19 +3057,19 @@ proc_lf_pidbind(task_t curtask, uint64_t tid, task_t target_task, int bind) task_watch_unlock(); - task_deallocate(task); /* drop task ref in twp */ + task_deallocate(task); /* drop task ref in twp */ set_thread_appbg(target_thread, 0, twp->tw_importance); - thread_deallocate(target_thread); /* drop thread ref in twp */ + thread_deallocate(target_thread); /* drop thread ref in twp */ kfree(twp, sizeof(task_watch_t)); } else { task_watch_unlock(); - ret = 0; /* return success if it not alredy bound */ + ret = 0; /* return success if it not alredy bound */ goto out; } } out: - thread_deallocate(target_thread); /* drop thread ref acquired in this routine */ - return(ret); + thread_deallocate(target_thread); /* drop thread ref acquired in this routine */ + return ret; } static void @@ -2951,14 +3089,15 @@ apply_appstate_watchers(task_t task) retry: /* if no watchers on the list return */ - if ((numwatchers = task->num_taskwatchers) == 0) + if ((numwatchers = task->num_taskwatchers) == 0) { return; + } - threadlist = (thread_watchlist_t *)kalloc(numwatchers*sizeof(thread_watchlist_t)); - if (threadlist == NULL) + threadlist = kheap_alloc(KHEAP_TEMP, + numwatchers * sizeof(thread_watchlist_t), Z_WAITOK | Z_ZERO); + if (threadlist == NULL) { return; - - bzero(threadlist, numwatchers*sizeof(thread_watchlist_t)); + } task_watch_lock(); /*serialize application of app state changes */ @@ -2966,41 +3105,42 @@ retry: if (task->watchapplying != 0) { lck_mtx_sleep(&task_watch_mtx, LCK_SLEEP_DEFAULT, &task->watchapplying, THREAD_UNINT); task_watch_unlock(); - kfree(threadlist, numwatchers*sizeof(thread_watchlist_t)); + kheap_free(KHEAP_TEMP, threadlist, numwatchers * sizeof(thread_watchlist_t)); goto retry; } if (numwatchers != task->num_taskwatchers) { task_watch_unlock(); - kfree(threadlist, numwatchers*sizeof(thread_watchlist_t)); + kheap_free(KHEAP_TEMP, threadlist, numwatchers * sizeof(thread_watchlist_t)); goto retry; } - + setbg = proc_get_effective_task_policy(task, TASK_POLICY_WATCHERS_BG); task->watchapplying = 1; i = 0; queue_iterate(&task->task_watchers, twp, task_watch_t *, tw_links) { - threadlist[i].thread = twp->tw_thread; thread_reference(threadlist[i].thread); if (setbg != 0) { twp->tw_importance = twp->tw_thread->importance; threadlist[i].importance = INT_MIN; - } else + } else { threadlist[i].importance = twp->tw_importance; + } i++; - if (i > numwatchers) + if (i > numwatchers) { break; + } } task_watch_unlock(); - for (j = 0; j< i; j++) { + for (j = 0; j < i; j++) { set_thread_appbg(threadlist[j].thread, setbg, threadlist[j].importance); thread_deallocate(threadlist[j].thread); } - kfree(threadlist, numwatchers*sizeof(thread_watchlist_t)); + kheap_free(KHEAP_TEMP, threadlist, numwatchers * sizeof(thread_watchlist_t)); task_watch_lock(); @@ -3034,66 +3174,34 @@ thead_remove_taskwatch(thread_t thread) void task_removewatchers(task_t task) { - int numwatchers = 0, i, j; - task_watch_t ** twplist = NULL; - task_watch_t * twp = NULL; - -retry: - if ((numwatchers = task->num_taskwatchers) == 0) - return; - - twplist = (task_watch_t **)kalloc(numwatchers*sizeof(task_watch_t *)); - if (twplist == NULL) - return; - - bzero(twplist, numwatchers*sizeof(task_watch_t *)); + queue_head_t queue; + task_watch_t *twp; task_watch_lock(); - if (task->num_taskwatchers == 0) { - task_watch_unlock(); - goto out; - } - - if (numwatchers != task->num_taskwatchers) { - task_watch_unlock(); - kfree(twplist, numwatchers*sizeof(task_watch_t *)); - numwatchers = 0; - goto retry; - } - - i = 0; - while((twp = (task_watch_t *)dequeue_head(&task->task_watchers)) != NULL) - { - twplist[i] = twp; - task->num_taskwatchers--; + queue_new_head(&task->task_watchers, &queue, task_watch_t *, tw_links); + queue_init(&task->task_watchers); - /* + queue_iterate(&queue, twp, task_watch_t *, tw_links) { + /* * Since the linkage is removed and thead state cleanup is already set up, * remove the refernce from the thread. */ - twp->tw_thread->taskwatch = NULL; /* removed linkage, clear thread holding ref */ - i++; - if ((task->num_taskwatchers == 0) || (i > numwatchers)) - break; + twp->tw_thread->taskwatch = NULL; /* removed linkage, clear thread holding ref */ } + task->num_taskwatchers = 0; task_watch_unlock(); - for (j = 0; j< i; j++) { - - twp = twplist[j]; + while (!queue_empty(&queue)) { + queue_remove_first(&queue, twp, task_watch_t *, tw_links); /* remove thread and network bg */ set_thread_appbg(twp->tw_thread, 0, twp->tw_importance); thread_deallocate(twp->tw_thread); task_deallocate(twp->tw_task); kfree(twp, sizeof(task_watch_t)); } - -out: - kfree(twplist, numwatchers*sizeof(task_watch_t *)); - } -#endif /* CONFIG_EMBEDDED */ +#endif /* CONFIG_TASKWATCH */ /* * Routines for importance donation/inheritance/boosting @@ -3183,8 +3291,53 @@ task_importance_reset(__imp_only task_t task) #endif /* IMPORTANCE_INHERITANCE */ } +void +task_importance_init_from_parent(__imp_only task_t new_task, __imp_only task_t parent_task) +{ #if IMPORTANCE_INHERITANCE + ipc_importance_task_t new_task_imp = IIT_NULL; + new_task->task_imp_base = NULL; + if (!parent_task) { + return; + } + + if (task_is_marked_importance_donor(parent_task)) { + new_task_imp = ipc_importance_for_task(new_task, FALSE); + assert(IIT_NULL != new_task_imp); + ipc_importance_task_mark_donor(new_task_imp, TRUE); + } + if (task_is_marked_live_importance_donor(parent_task)) { + if (IIT_NULL == new_task_imp) { + new_task_imp = ipc_importance_for_task(new_task, FALSE); + } + assert(IIT_NULL != new_task_imp); + ipc_importance_task_mark_live_donor(new_task_imp, TRUE); + } + /* Do not inherit 'receiver' on fork, vfexec or true spawn */ + if (task_is_exec_copy(new_task) && + task_is_marked_importance_receiver(parent_task)) { + if (IIT_NULL == new_task_imp) { + new_task_imp = ipc_importance_for_task(new_task, FALSE); + } + assert(IIT_NULL != new_task_imp); + ipc_importance_task_mark_receiver(new_task_imp, TRUE); + } + if (task_is_marked_importance_denap_receiver(parent_task)) { + if (IIT_NULL == new_task_imp) { + new_task_imp = ipc_importance_for_task(new_task, FALSE); + } + assert(IIT_NULL != new_task_imp); + ipc_importance_task_mark_denap_receiver(new_task_imp, TRUE); + } + if (IIT_NULL != new_task_imp) { + assert(new_task->task_imp_base == new_task_imp); + ipc_importance_task_release(new_task_imp); + } +#endif /* IMPORTANCE_INHERITANCE */ +} + +#if IMPORTANCE_INHERITANCE /* * Sets the task boost bit to the provided value. Does NOT run the update function. * @@ -3195,20 +3348,20 @@ task_set_boost_locked(task_t task, boolean_t boost_active) { #if IMPORTANCE_TRACE KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, (IMPORTANCE_CODE(IMP_BOOST, (boost_active ? IMP_BOOSTED : IMP_UNBOOSTED)) | DBG_FUNC_START), - proc_selfpid(), task_pid(task), trequested_0(task), trequested_1(task), 0); + proc_selfpid(), task_pid(task), trequested_0(task), trequested_1(task), 0); #endif /* IMPORTANCE_TRACE */ task->requested_policy.trp_boosted = boost_active; #if IMPORTANCE_TRACE - if (boost_active == TRUE){ + if (boost_active == TRUE) { DTRACE_BOOST2(boost, task_t, task, int, task_pid(task)); } else { DTRACE_BOOST2(unboost, task_t, task, int, task_pid(task)); } KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, (IMPORTANCE_CODE(IMP_BOOST, (boost_active ? IMP_BOOSTED : IMP_UNBOOSTED)) | DBG_FUNC_END), - proc_selfpid(), task_pid(task), - trequested_0(task), trequested_1(task), 0); + proc_selfpid(), task_pid(task), + trequested_0(task), trequested_1(task), 0); #endif /* IMPORTANCE_TRACE */ } @@ -3234,8 +3387,9 @@ task_update_boost_locked(task_t task, boolean_t boost_active, task_pend_token_t boolean_t task_is_importance_donor(task_t task) { - if (task->task_imp_base == IIT_NULL) + if (task->task_imp_base == IIT_NULL) { return FALSE; + } return ipc_importance_task_is_donor(task->task_imp_base); } @@ -3245,8 +3399,9 @@ task_is_importance_donor(task_t task) boolean_t task_is_marked_importance_donor(task_t task) { - if (task->task_imp_base == IIT_NULL) + if (task->task_imp_base == IIT_NULL) { return FALSE; + } return ipc_importance_task_is_marked_donor(task->task_imp_base); } @@ -3256,8 +3411,9 @@ task_is_marked_importance_donor(task_t task) boolean_t task_is_marked_live_importance_donor(task_t task) { - if (task->task_imp_base == IIT_NULL) + if (task->task_imp_base == IIT_NULL) { return FALSE; + } return ipc_importance_task_is_marked_live_donor(task->task_imp_base); } @@ -3269,8 +3425,9 @@ task_is_marked_live_importance_donor(task_t task) boolean_t task_is_importance_receiver(task_t task) { - if (task->task_imp_base == IIT_NULL) + if (task->task_imp_base == IIT_NULL) { return FALSE; + } return ipc_importance_task_is_marked_receiver(task->task_imp_base); } @@ -3280,8 +3437,9 @@ task_is_importance_receiver(task_t task) boolean_t task_is_marked_importance_receiver(task_t task) { - if (task->task_imp_base == IIT_NULL) + if (task->task_imp_base == IIT_NULL) { return FALSE; + } return ipc_importance_task_is_marked_receiver(task->task_imp_base); } @@ -3292,8 +3450,9 @@ task_is_marked_importance_receiver(task_t task) boolean_t task_is_importance_denap_receiver(task_t task) { - if (task->task_imp_base == IIT_NULL) + if (task->task_imp_base == IIT_NULL) { return FALSE; + } return ipc_importance_task_is_denap_receiver(task->task_imp_base); } @@ -3303,8 +3462,9 @@ task_is_importance_denap_receiver(task_t task) boolean_t task_is_marked_importance_denap_receiver(task_t task) { - if (task->task_imp_base == IIT_NULL) + if (task->task_imp_base == IIT_NULL) { return FALSE; + } return ipc_importance_task_is_marked_denap_receiver(task->task_imp_base); } @@ -3315,10 +3475,11 @@ task_is_marked_importance_denap_receiver(task_t task) boolean_t task_is_importance_receiver_type(task_t task) { - if (task->task_imp_base == IIT_NULL) + if (task->task_imp_base == IIT_NULL) { return FALSE; - return (task_is_importance_receiver(task) || - task_is_importance_denap_receiver(task)); + } + return task_is_importance_receiver(task) || + task_is_importance_denap_receiver(task); } /* @@ -3366,12 +3527,12 @@ task_importance_hold_legacy_external_assertion(task_t target_task, uint32_t coun { ipc_importance_task_t task_imp; kern_return_t ret; - + /* must already have set up an importance */ task_imp = target_task->task_imp_base; if (IIT_NULL == task_imp) { return EOVERFLOW; - } + } ret = ipc_importance_task_hold_legacy_external_assertion(task_imp, count); return (KERN_SUCCESS != ret) ? ENOTSUP : 0; } @@ -3381,7 +3542,7 @@ task_importance_drop_file_lock_assertion(task_t target_task, uint32_t count) { ipc_importance_task_t task_imp; kern_return_t ret; - + /* must already have set up an importance */ task_imp = target_task->task_imp_base; if (IIT_NULL == task_imp) { @@ -3396,7 +3557,7 @@ task_importance_drop_legacy_external_assertion(task_t target_task, uint32_t coun { ipc_importance_task_t task_imp; kern_return_t ret; - + /* must already have set up an importance */ task_imp = target_task->task_imp_base; if (IIT_NULL == task_imp) { @@ -3455,15 +3616,16 @@ task_add_importance_watchport(task_t task, mach_port_t port, int *boostp) } if (IIT_NULL != release_imp_task) { - if (boost > 0) + if (boost > 0) { ipc_importance_task_drop_internal_assertion(release_imp_task, boost); + } // released_pid = task_pid(release_imp_task); /* TODO: Need ref-safe way to get pid */ ipc_importance_task_release(release_imp_task); } #if IMPORTANCE_TRACE KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, (IMPORTANCE_CODE(IMP_WATCHPORT, 0)) | DBG_FUNC_NONE, - proc_selfpid(), pid, boost, released_pid, 0); + proc_selfpid(), pid, boost, released_pid, 0); #endif /* IMPORTANCE_TRACE */ } @@ -3517,13 +3679,14 @@ task_has_been_notified(task_t task, int pressurelevel) if (task == NULL) { return FALSE; } - - if (pressurelevel == kVMPressureWarning) - return (task->low_mem_notified_warn ? TRUE : FALSE); - else if (pressurelevel == kVMPressureCritical) - return (task->low_mem_notified_critical ? TRUE : FALSE); - else + + if (pressurelevel == kVMPressureWarning) { + return task->low_mem_notified_warn ? TRUE : FALSE; + } else if (pressurelevel == kVMPressureCritical) { + return task->low_mem_notified_critical ? TRUE : FALSE; + } else { return TRUE; + } } @@ -3538,19 +3701,20 @@ task_used_for_purging(task_t task, int pressurelevel) if (task == NULL) { return FALSE; } - - if (pressurelevel == kVMPressureWarning) - return (task->purged_memory_warn ? TRUE : FALSE); - else if (pressurelevel == kVMPressureCritical) - return (task->purged_memory_critical ? TRUE : FALSE); - else + + if (pressurelevel == kVMPressureWarning) { + return task->purged_memory_warn ? TRUE : FALSE; + } else if (pressurelevel == kVMPressureCritical) { + return task->purged_memory_critical ? TRUE : FALSE; + } else { return TRUE; + } } /* * Mark the task as notified with memory notification. - * + * * Condition: task lock should be held while calling this function. */ void @@ -3559,11 +3723,12 @@ task_mark_has_been_notified(task_t task, int pressurelevel) if (task == NULL) { return; } - - if (pressurelevel == kVMPressureWarning) + + if (pressurelevel == kVMPressureWarning) { task->low_mem_notified_warn = 1; - else if (pressurelevel == kVMPressureCritical) + } else if (pressurelevel == kVMPressureCritical) { task->low_mem_notified_critical = 1; + } } @@ -3578,17 +3743,18 @@ task_mark_used_for_purging(task_t task, int pressurelevel) if (task == NULL) { return; } - - if (pressurelevel == kVMPressureWarning) + + if (pressurelevel == kVMPressureWarning) { task->purged_memory_warn = 1; - else if (pressurelevel == kVMPressureCritical) + } else if (pressurelevel == kVMPressureCritical) { task->purged_memory_critical = 1; + } } /* * Mark the task eligible for low memory notification. - * + * * Condition: task lock should be held while calling this function. */ void @@ -3597,11 +3763,12 @@ task_clear_has_been_notified(task_t task, int pressurelevel) if (task == NULL) { return; } - - if (pressurelevel == kVMPressureWarning) + + if (pressurelevel == kVMPressureWarning) { task->low_mem_notified_warn = 0; - else if (pressurelevel == kVMPressureCritical) + } else if (pressurelevel == kVMPressureCritical) { task->low_mem_notified_critical = 0; + } } @@ -3616,22 +3783,22 @@ task_clear_used_for_purging(task_t task) if (task == NULL) { return; } - + task->purged_memory_warn = 0; task->purged_memory_critical = 0; } /* - * Estimate task importance for purging its purgeable memory + * Estimate task importance for purging its purgeable memory * and low memory notification. - * + * * Importance is calculated in the following order of criteria: * -Task role : Background vs Foreground * -Boost status: Not boosted vs Boosted * -Darwin BG status. * - * Returns: Estimated task importance. Less important task will have lower + * Returns: Estimated task importance. Less important task will have lower * estimated importance. */ int @@ -3643,27 +3810,29 @@ task_importance_estimate(task_t task) return 0; } - if (proc_get_effective_task_policy(task, TASK_POLICY_ROLE) == TASK_FOREGROUND_APPLICATION) - task_importance += TASK_IMPORTANCE_FOREGROUND; + if (proc_get_effective_task_policy(task, TASK_POLICY_ROLE) == TASK_FOREGROUND_APPLICATION) { + task_importance += TASK_IMPORTANCE_FOREGROUND; + } + + if (proc_get_effective_task_policy(task, TASK_POLICY_DARWIN_BG) == 0) { + task_importance += TASK_IMPORTANCE_NOTDARWINBG; + } - if (proc_get_effective_task_policy(task, TASK_POLICY_DARWIN_BG) == 0) - task_importance += TASK_IMPORTANCE_NOTDARWINBG; - return task_importance; } boolean_t task_has_assertions(task_t task) { - return (task->task_imp_base->iit_assertcnt? TRUE : FALSE); + return task->task_imp_base->iit_assertcnt? TRUE : FALSE; } kern_return_t send_resource_violation(typeof(send_cpu_usage_violation) sendfunc, - task_t violator, - struct ledger_entry_info *linfo, - resource_notify_flags_t flags) + task_t violator, + struct ledger_entry_info *linfo, + resource_notify_flags_t flags) { #ifndef MACH_BSD return KERN_NOT_SUPPORTED; @@ -3693,10 +3862,12 @@ send_resource_violation(typeof(send_cpu_usage_violation) sendfunc, pid = task_pid(violator); if (flags & kRNFatalLimitFlag) { kr = proc_pidpathinfo_internal(proc, 0, proc_path, - sizeof(proc_path), NULL); + sizeof(proc_path), NULL); } task_unlock(violator); - if (kr) goto finish; + if (kr) { + goto finish; + } /* violation time ~ now */ clock_get_calendar_nanotime(&secs, &nsecs); @@ -3706,19 +3877,18 @@ send_resource_violation(typeof(send_cpu_usage_violation) sendfunc, /* send message */ kr = host_get_special_port(host_priv_self(), HOST_LOCAL_NODE, - HOST_RESOURCE_NOTIFY_PORT, &dstport); - if (kr) goto finish; + HOST_RESOURCE_NOTIFY_PORT, &dstport); + if (kr) { + goto finish; + } - /* TH_OPT_HONOR_QLIMIT causes ipc_kmsg_send() to respect the - * queue limit. It also unsets this flag, but this code also - * unsets it for clarity and in case that code changes. */ - curthread->options |= TH_OPT_HONOR_QLIMIT; + thread_set_honor_qlimit(curthread); kr = sendfunc(dstport, - procname, pid, proc_path, timestamp, - linfo->lei_balance, linfo->lei_last_refill, - linfo->lei_limit, linfo->lei_refill_period, - flags); - curthread->options &= (~TH_OPT_HONOR_QLIMIT); + procname, pid, proc_path, timestamp, + linfo->lei_balance, linfo->lei_last_refill, + linfo->lei_limit, linfo->lei_refill_period, + flags); + thread_clear_honor_qlimit(curthread); ipc_port_release_send(dstport); @@ -3736,18 +3906,18 @@ finish: #ifdef __LP64__ void trace_resource_violation(uint16_t code, - struct ledger_entry_info *linfo) + struct ledger_entry_info *linfo) { KERNEL_DBG_IST_SANE(KDBG_CODE(DBG_MACH, DBG_MACH_RESOURCE, code), - linfo->lei_balance, linfo->lei_last_refill, - linfo->lei_limit, linfo->lei_refill_period); + linfo->lei_balance, linfo->lei_last_refill, + linfo->lei_limit, linfo->lei_refill_period); } #else /* K32 */ /* TODO: create/find a trace_two_LLs() for K32 systems */ #define MASK32 0xffffffff void trace_resource_violation(uint16_t code, - struct ledger_entry_info *linfo) + struct ledger_entry_info *linfo) { int8_t lownibble = (code & 0x3) * 2; int16_t codeA = (code & 0xffc) | lownibble; @@ -3764,10 +3934,10 @@ trace_resource_violation(uint16_t code, int32_t refill_period_low = linfo->lei_refill_period & MASK32; KERNEL_DBG_IST_SANE(KDBG_CODE(DBG_MACH, DBG_MACH_RESOURCE, codeA), - balance_high, balance_low, - last_refill_high, last_refill_low); + balance_high, balance_low, + last_refill_high, last_refill_low); KERNEL_DBG_IST_SANE(KDBG_CODE(DBG_MACH, DBG_MACH_RESOURCE, codeB), - limit_high, limit_low, - refill_period_high, refill_period_low); + limit_high, limit_low, + refill_period_high, refill_period_low); } #endif /* K64/K32 */