2 * Copyright (c) 2013 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <kern/kern_types.h>
30 #include <mach/mach_types.h>
31 #include <mach/boolean.h>
33 #include <kern/coalition.h>
34 #include <kern/host.h>
35 #include <kern/kalloc.h>
36 #include <kern/ledger.h>
37 #include <kern/mach_param.h> /* for TASK_CHUNK */
38 #include <kern/task.h>
39 #include <kern/thread_group.h>
40 #include <kern/zalloc.h>
42 #include <libkern/OSAtomic.h>
44 #include <mach/coalition_notification_server.h>
45 #include <mach/host_priv.h>
46 #include <mach/host_special_ports.h>
48 #include <sys/errno.h>
51 * BSD interface functions
53 int coalitions_get_list(int type
, struct procinfo_coalinfo
*coal_list
, int list_sz
);
54 boolean_t
coalition_is_leader(task_t task
, int coal_type
, coalition_t
*coal
);
55 task_t
coalition_get_leader(coalition_t coal
);
56 int coalition_get_task_count(coalition_t coal
);
57 uint64_t coalition_get_page_count(coalition_t coal
, int *ntasks
);
58 int coalition_get_pid_list(coalition_t coal
, uint32_t rolemask
, int sort_order
,
59 int *pid_list
, int list_sz
);
61 /* defined in task.c */
62 extern ledger_template_t task_ledger_template
;
65 * Coalition zone needs limits. We expect there will be as many coalitions as
66 * tasks (same order of magnitude), so use the task zone's limits.
68 #define CONFIG_COALITION_MAX CONFIG_TASK_MAX
69 #define COALITION_CHUNK TASK_CHUNK
71 int unrestrict_coalition_syscalls
;
72 int merge_adaptive_coalitions
;
74 lck_attr_t coalitions_lck_attr
;
75 lck_grp_t coalitions_lck_grp
;
76 lck_grp_attr_t coalitions_lck_grp_attr
;
78 /* coalitions_list_lock protects coalition_count, coalitions queue, next_coalition_id. */
79 decl_lck_mtx_data(static,coalitions_list_lock
);
80 static uint64_t coalition_count
;
81 static uint64_t coalition_next_id
= 1;
82 static queue_head_t coalitions_q
;
84 coalition_t init_coalition
[COALITION_NUM_TYPES
];
85 coalition_t corpse_coalition
[COALITION_NUM_TYPES
];
87 zone_t coalition_zone
;
89 static const char *coal_type_str(int type
)
92 case COALITION_TYPE_RESOURCE
:
94 case COALITION_TYPE_JETSAM
:
101 struct coalition_type
{
106 * pre-condition: coalition just allocated (unlocked), unreferenced,
109 kern_return_t (*init
)(coalition_t coal
, boolean_t privileged
);
113 * pre-condition: coalition unlocked
114 * pre-condition: coalition refcount=0, active_count=0,
115 * termrequested=1, terminated=1, reaped=1
117 void (*dealloc
)(coalition_t coal
);
121 * pre-condition: coalition locked
122 * pre-condition: coalition !repead and !terminated
124 kern_return_t (*adopt_task
)(coalition_t coal
, task_t task
);
128 * pre-condition: coalition locked
129 * pre-condition: task has been removed from coalition's task list
131 kern_return_t (*remove_task
)(coalition_t coal
, task_t task
);
135 * pre-condition: coalition locked
136 * pre-condition: task added to coalition's task list,
137 * active_count >= 1 (at least the given task is active)
139 kern_return_t (*set_taskrole
)(coalition_t coal
, task_t task
, int role
);
143 * pre-condition: coalition locked
144 * pre-condition: task added to coalition's task list,
145 * active_count >= 1 (at least the given task is active)
147 int (*get_taskrole
)(coalition_t coal
, task_t task
);
151 * pre-condition: coalition locked
153 void (*iterate_tasks
)(coalition_t coal
, void *ctx
, void (*callback
)(coalition_t
, void *, task_t
));
157 * COALITION_TYPE_RESOURCE
160 static kern_return_t
i_coal_resource_init(coalition_t coal
, boolean_t privileged
);
161 static void i_coal_resource_dealloc(coalition_t coal
);
162 static kern_return_t
i_coal_resource_adopt_task(coalition_t coal
, task_t task
);
163 static kern_return_t
i_coal_resource_remove_task(coalition_t coal
, task_t task
);
164 static kern_return_t
i_coal_resource_set_taskrole(coalition_t coal
,
165 task_t task
, int role
);
166 static int i_coal_resource_get_taskrole(coalition_t coal
, task_t task
);
167 static void i_coal_resource_iterate_tasks(coalition_t coal
, void *ctx
,
168 void (*callback
)(coalition_t
, void *, task_t
));
170 struct i_resource_coalition
{
173 uint64_t byteswritten
;
176 uint64_t logical_immediate_writes
;
177 uint64_t logical_deferred_writes
;
178 uint64_t logical_invalidated_writes
;
179 uint64_t logical_metadata_writes
;
182 uint64_t task_count
; /* tasks that have started in this coalition */
183 uint64_t dead_task_count
; /* tasks that have exited in this coalition;
184 subtract from task_count to get count
187 * Count the length of time this coalition had at least one active task.
188 * This can be a 'denominator' to turn e.g. cpu_time to %cpu.
190 uint64_t last_became_nonempty_time
;
191 uint64_t time_nonempty
;
193 queue_head_t tasks
; /* List of active tasks in the coalition */
197 * COALITION_TYPE_JETSAM
200 static kern_return_t
i_coal_jetsam_init(coalition_t coal
, boolean_t privileged
);
201 static void i_coal_jetsam_dealloc(coalition_t coal
);
202 static kern_return_t
i_coal_jetsam_adopt_task(coalition_t coal
, task_t task
);
203 static kern_return_t
i_coal_jetsam_remove_task(coalition_t coal
, task_t task
);
204 static kern_return_t
i_coal_jetsam_set_taskrole(coalition_t coal
,
205 task_t task
, int role
);
206 static int i_coal_jetsam_get_taskrole(coalition_t coal
, task_t task
);
207 static void i_coal_jetsam_iterate_tasks(coalition_t coal
, void *ctx
,
208 void (*callback
)(coalition_t
, void *, task_t
));
210 struct i_jetsam_coalition
{
212 queue_head_t extensions
;
213 queue_head_t services
;
215 thread_group_t thread_group
;
220 * main coalition structure
223 uint64_t id
; /* monotonically increasing */
225 uint32_t role
; /* default task role (background, adaptive, interactive, etc) */
226 uint32_t ref_count
; /* Number of references to the memory containing this struct */
227 uint32_t active_count
; /* Number of members of (tasks in) the
228 coalition, plus vouchers referring
230 uint32_t focal_task_count
; /* Number of TASK_FOREGROUND_APPLICATION tasks in the coalition */
231 uint32_t nonfocal_task_count
; /* Number of TASK_BACKGROUND_APPLICATION tasks in the coalition */
233 /* coalition flags */
234 uint32_t privileged
: 1; /* Members of this coalition may create
235 and manage coalitions and may posix_spawn
236 processes into selected coalitions */
239 uint32_t termrequested
: 1; /* launchd has requested termination when coalition becomes empty */
240 uint32_t terminated
: 1; /* coalition became empty and spawns are now forbidden */
241 uint32_t reaped
: 1; /* reaped, invisible to userspace, but waiting for ref_count to go to zero */
242 uint32_t notified
: 1; /* no-more-processes notification was sent via special port */
243 uint32_t efficient
: 1; /* launchd has marked the coalition as efficient */
244 #if DEVELOPMENT || DEBUG
245 uint32_t should_notify
: 1; /* should this coalition send notifications (default: yes) */
248 queue_chain_t coalitions
; /* global list of coalitions */
250 decl_lck_mtx_data(,lock
) /* Coalition lock. */
252 /* put coalition type-specific structures here */
254 struct i_resource_coalition r
;
255 struct i_jetsam_coalition j
;
260 * register different coalition types:
261 * these must be kept in the order specified in coalition.h
263 static const struct coalition_type
264 s_coalition_types
[COALITION_NUM_TYPES
] = {
266 COALITION_TYPE_RESOURCE
,
268 i_coal_resource_init
,
269 i_coal_resource_dealloc
,
270 i_coal_resource_adopt_task
,
271 i_coal_resource_remove_task
,
272 i_coal_resource_set_taskrole
,
273 i_coal_resource_get_taskrole
,
274 i_coal_resource_iterate_tasks
,
277 COALITION_TYPE_JETSAM
,
280 i_coal_jetsam_dealloc
,
281 i_coal_jetsam_adopt_task
,
282 i_coal_jetsam_remove_task
,
283 i_coal_jetsam_set_taskrole
,
284 i_coal_jetsam_get_taskrole
,
285 i_coal_jetsam_iterate_tasks
,
289 #define coal_call(coal, func, ...) \
290 (s_coalition_types[(coal)->type].func)(coal, ## __VA_ARGS__)
293 #define coalition_lock(c) do{ lck_mtx_lock(&c->lock); }while(0)
294 #define coalition_unlock(c) do{ lck_mtx_unlock(&c->lock); }while(0)
297 * Define the coalition type to track focal tasks.
298 * On embedded, track them using jetsam coalitions since they have associated thread
299 * groups which reflect this property as a flag (and pass it down to CLPC).
300 * On non-embedded platforms, since not all coalitions have jetsam coalitions
301 * track focal counts on the resource coalition.
304 #define COALITION_FOCAL_TASKS_ACCOUNTING COALITION_TYPE_JETSAM
305 #else /* CONFIG_EMBEDDED */
306 #define COALITION_FOCAL_TASKS_ACCOUNTING COALITION_TYPE_RESOURCE
307 #endif /* CONFIG_EMBEDDED */
311 coalition_notify_user(uint64_t id
, uint32_t flags
)
313 mach_port_t user_port
;
316 kr
= host_get_coalition_port(host_priv_self(), &user_port
);
317 if ((kr
!= KERN_SUCCESS
) || !IPC_PORT_VALID(user_port
)) {
321 coalition_notification(user_port
, id
, flags
);
322 ipc_port_release_send(user_port
);
327 * COALITION_TYPE_RESOURCE
331 i_coal_resource_init(coalition_t coal
, boolean_t privileged
)
334 assert(coal
&& coal
->type
== COALITION_TYPE_RESOURCE
);
335 coal
->r
.ledger
= ledger_instantiate(task_ledger_template
,
336 LEDGER_CREATE_ACTIVE_ENTRIES
);
337 if (coal
->r
.ledger
== NULL
)
338 return KERN_RESOURCE_SHORTAGE
;
340 queue_init(&coal
->r
.tasks
);
346 i_coal_resource_dealloc(coalition_t coal
)
348 assert(coal
&& coal
->type
== COALITION_TYPE_RESOURCE
);
349 ledger_dereference(coal
->r
.ledger
);
353 i_coal_resource_adopt_task(coalition_t coal
, task_t task
)
355 struct i_resource_coalition
*cr
;
357 assert(coal
&& coal
->type
== COALITION_TYPE_RESOURCE
);
358 assert(queue_empty(&task
->task_coalition
[COALITION_TYPE_RESOURCE
]));
363 if (cr
->task_count
< cr
->dead_task_count
) {
364 panic("%s: coalition %p id:%llu type:%s task_count(%llu) < dead_task_count(%llu)",
365 __func__
, coal
, coal
->id
, coal_type_str(coal
->type
),
366 cr
->task_count
, cr
->dead_task_count
);
369 /* If moving from 0->1 active tasks */
370 if (cr
->task_count
- cr
->dead_task_count
== 1) {
371 cr
->last_became_nonempty_time
= mach_absolute_time();
374 /* put the task on the coalition's list of tasks */
375 enqueue_tail(&cr
->tasks
, &task
->task_coalition
[COALITION_TYPE_RESOURCE
]);
377 coal_dbg("Added PID:%d to id:%llu, task_count:%llu, dead_count:%llu, nonempty_time:%llu",
378 task_pid(task
), coal
->id
, cr
->task_count
, cr
->dead_task_count
,
379 cr
->last_became_nonempty_time
);
385 i_coal_resource_remove_task(coalition_t coal
, task_t task
)
387 struct i_resource_coalition
*cr
;
389 assert(coal
&& coal
->type
== COALITION_TYPE_RESOURCE
);
390 assert(task
->coalition
[COALITION_TYPE_RESOURCE
] == coal
);
391 assert(!queue_empty(&task
->task_coalition
[COALITION_TYPE_RESOURCE
]));
394 * handle resource coalition accounting rollup for dead tasks
398 cr
->dead_task_count
++;
400 if (cr
->task_count
< cr
->dead_task_count
) {
401 panic("%s: coalition %p id:%llu type:%s task_count(%llu) < dead_task_count(%llu)",
402 __func__
, coal
, coal
->id
, coal_type_str(coal
->type
), cr
->task_count
, cr
->dead_task_count
);
405 /* If moving from 1->0 active tasks */
406 if (cr
->task_count
- cr
->dead_task_count
== 0) {
407 uint64_t last_time_nonempty
= mach_absolute_time() - cr
->last_became_nonempty_time
;
408 cr
->last_became_nonempty_time
= 0;
409 cr
->time_nonempty
+= last_time_nonempty
;
412 /* Do not roll up for exec'd task or exec copy task */
413 if (!task_is_exec_copy(task
) && !task_did_exec(task
)) {
414 ledger_rollup(cr
->ledger
, task
->ledger
);
415 cr
->bytesread
+= task
->task_io_stats
->disk_reads
.size
;
416 cr
->byteswritten
+= task
->task_io_stats
->total_io
.size
- task
->task_io_stats
->disk_reads
.size
;
418 cr
->gpu_time
+= task_gpu_utilisation(task
);
420 cr
->energy
+= task_energy(task
);
422 cr
->logical_immediate_writes
+= task
->task_immediate_writes
;
423 cr
->logical_deferred_writes
+= task
->task_deferred_writes
;
424 cr
->logical_invalidated_writes
+= task
->task_invalidated_writes
;
425 cr
->logical_metadata_writes
+= task
->task_metadata_writes
;
426 cr
->cpu_ptime
+= task_cpu_ptime(task
);
429 /* remove the task from the coalition's list */
430 remqueue(&task
->task_coalition
[COALITION_TYPE_RESOURCE
]);
431 queue_chain_init(task
->task_coalition
[COALITION_TYPE_RESOURCE
]);
433 coal_dbg("removed PID:%d from id:%llu, task_count:%llu, dead_count:%llu",
434 task_pid(task
), coal
->id
, cr
->task_count
, cr
->dead_task_count
);
440 i_coal_resource_set_taskrole(__unused coalition_t coal
,
441 __unused task_t task
, __unused
int role
)
447 i_coal_resource_get_taskrole(__unused coalition_t coal
, __unused task_t task
)
451 assert(coal
&& coal
->type
== COALITION_TYPE_RESOURCE
);
453 qe_foreach_element(t
, &coal
->r
.tasks
, task_coalition
[COALITION_TYPE_RESOURCE
]) {
455 return COALITION_TASKROLE_UNDEF
;
462 i_coal_resource_iterate_tasks(coalition_t coal
, void *ctx
, void (*callback
)(coalition_t
, void *, task_t
))
465 assert(coal
&& coal
->type
== COALITION_TYPE_RESOURCE
);
467 qe_foreach_element(t
, &coal
->r
.tasks
, task_coalition
[COALITION_TYPE_RESOURCE
])
468 callback(coal
, ctx
, t
);
472 coalition_resource_usage_internal(coalition_t coal
, struct coalition_resource_usage
*cru_out
)
475 ledger_amount_t credit
, debit
;
478 if (coal
->type
!= COALITION_TYPE_RESOURCE
)
479 return KERN_INVALID_ARGUMENT
;
481 /* Return KERN_INVALID_ARGUMENT for Corpse coalition */
482 for (i
= 0; i
< COALITION_NUM_TYPES
; i
++) {
483 if (coal
== corpse_coalition
[i
]) {
484 return KERN_INVALID_ARGUMENT
;
488 ledger_t sum_ledger
= ledger_instantiate(task_ledger_template
, LEDGER_CREATE_ACTIVE_ENTRIES
);
489 if (sum_ledger
== LEDGER_NULL
)
490 return KERN_RESOURCE_SHORTAGE
;
492 coalition_lock(coal
);
495 * Start with the coalition's ledger, which holds the totals from all
498 ledger_rollup(sum_ledger
, coal
->r
.ledger
);
499 uint64_t bytesread
= coal
->r
.bytesread
;
500 uint64_t byteswritten
= coal
->r
.byteswritten
;
501 uint64_t gpu_time
= coal
->r
.gpu_time
;
502 uint64_t energy
= coal
->r
.energy
;
503 uint64_t logical_immediate_writes
= coal
->r
.logical_immediate_writes
;
504 uint64_t logical_deferred_writes
= coal
->r
.logical_deferred_writes
;
505 uint64_t logical_invalidated_writes
= coal
->r
.logical_invalidated_writes
;
506 uint64_t logical_metadata_writes
= coal
->r
.logical_metadata_writes
;
507 int64_t cpu_time_billed_to_me
= 0;
508 int64_t cpu_time_billed_to_others
= 0;
509 int64_t energy_billed_to_me
= 0;
510 int64_t energy_billed_to_others
= 0;
511 uint64_t cpu_ptime
= coal
->r
.cpu_ptime
;
514 * Add to that all the active tasks' ledgers. Tasks cannot deallocate
515 * out from under us, since we hold the coalition lock.
518 qe_foreach_element(task
, &coal
->r
.tasks
, task_coalition
[COALITION_TYPE_RESOURCE
]) {
520 * Rolling up stats for exec copy task or exec'd task will lead to double accounting.
521 * Cannot take task lock after taking coaliton lock
523 if (task_is_exec_copy(task
) || task_did_exec(task
)) {
527 ledger_rollup(sum_ledger
, task
->ledger
);
528 bytesread
+= task
->task_io_stats
->disk_reads
.size
;
529 byteswritten
+= task
->task_io_stats
->total_io
.size
- task
->task_io_stats
->disk_reads
.size
;
531 gpu_time
+= task_gpu_utilisation(task
);
533 energy
+= task_energy(task
);
535 logical_immediate_writes
+= task
->task_immediate_writes
;
536 logical_deferred_writes
+= task
->task_deferred_writes
;
537 logical_invalidated_writes
+= task
->task_invalidated_writes
;
538 logical_metadata_writes
+= task
->task_metadata_writes
;
539 cpu_ptime
+= task_cpu_ptime(task
);
542 kr
= ledger_get_balance(sum_ledger
, task_ledgers
.cpu_time_billed_to_me
, (int64_t *)&cpu_time_billed_to_me
);
543 if (kr
!= KERN_SUCCESS
|| cpu_time_billed_to_me
< 0) {
544 cpu_time_billed_to_me
= 0;
547 kr
= ledger_get_balance(sum_ledger
, task_ledgers
.cpu_time_billed_to_others
, (int64_t *)&cpu_time_billed_to_others
);
548 if (kr
!= KERN_SUCCESS
|| cpu_time_billed_to_others
< 0) {
549 cpu_time_billed_to_others
= 0;
552 kr
= ledger_get_balance(sum_ledger
, task_ledgers
.energy_billed_to_me
, (int64_t *)&energy_billed_to_me
);
553 if (kr
!= KERN_SUCCESS
|| energy_billed_to_me
< 0) {
554 energy_billed_to_me
= 0;
557 kr
= ledger_get_balance(sum_ledger
, task_ledgers
.energy_billed_to_others
, (int64_t *)&energy_billed_to_others
);
558 if (kr
!= KERN_SUCCESS
|| energy_billed_to_others
< 0) {
559 energy_billed_to_others
= 0;
562 /* collect information from the coalition itself */
563 cru_out
->tasks_started
= coal
->r
.task_count
;
564 cru_out
->tasks_exited
= coal
->r
.dead_task_count
;
566 uint64_t time_nonempty
= coal
->r
.time_nonempty
;
567 uint64_t last_became_nonempty_time
= coal
->r
.last_became_nonempty_time
;
569 coalition_unlock(coal
);
571 /* Copy the totals out of sum_ledger */
572 kr
= ledger_get_entries(sum_ledger
, task_ledgers
.cpu_time
,
574 if (kr
!= KERN_SUCCESS
) {
577 cru_out
->cpu_time
= credit
;
578 cru_out
->cpu_time_billed_to_me
= (uint64_t)cpu_time_billed_to_me
;
579 cru_out
->cpu_time_billed_to_others
= (uint64_t)cpu_time_billed_to_others
;
580 cru_out
->energy_billed_to_me
= (uint64_t)energy_billed_to_me
;
581 cru_out
->energy_billed_to_others
= (uint64_t)energy_billed_to_others
;
583 kr
= ledger_get_entries(sum_ledger
, task_ledgers
.interrupt_wakeups
,
585 if (kr
!= KERN_SUCCESS
) {
588 cru_out
->interrupt_wakeups
= credit
;
590 kr
= ledger_get_entries(sum_ledger
, task_ledgers
.platform_idle_wakeups
,
592 if (kr
!= KERN_SUCCESS
) {
595 cru_out
->platform_idle_wakeups
= credit
;
597 cru_out
->bytesread
= bytesread
;
598 cru_out
->byteswritten
= byteswritten
;
599 cru_out
->gpu_time
= gpu_time
;
600 cru_out
->energy
= energy
;
601 cru_out
->logical_immediate_writes
= logical_immediate_writes
;
602 cru_out
->logical_deferred_writes
= logical_deferred_writes
;
603 cru_out
->logical_invalidated_writes
= logical_invalidated_writes
;
604 cru_out
->logical_metadata_writes
= logical_metadata_writes
;
605 cru_out
->cpu_ptime
= cpu_ptime
;
607 ledger_dereference(sum_ledger
);
608 sum_ledger
= LEDGER_NULL
;
610 if (last_became_nonempty_time
) {
611 time_nonempty
+= mach_absolute_time() - last_became_nonempty_time
;
613 absolutetime_to_nanoseconds(time_nonempty
, &cru_out
->time_nonempty
);
620 * COALITION_TYPE_JETSAM
624 i_coal_jetsam_init(coalition_t coal
, boolean_t privileged
)
626 assert(coal
&& coal
->type
== COALITION_TYPE_JETSAM
);
629 coal
->j
.leader
= TASK_NULL
;
630 queue_head_init(coal
->j
.extensions
);
631 queue_head_init(coal
->j
.services
);
632 queue_head_init(coal
->j
.other
);
638 i_coal_jetsam_dealloc(__unused coalition_t coal
)
640 assert(coal
&& coal
->type
== COALITION_TYPE_JETSAM
);
642 /* the coalition should be completely clear at this point */
643 assert(queue_empty(&coal
->j
.extensions
));
644 assert(queue_empty(&coal
->j
.services
));
645 assert(queue_empty(&coal
->j
.other
));
646 assert(coal
->j
.leader
== TASK_NULL
);
651 i_coal_jetsam_adopt_task(coalition_t coal
, task_t task
)
653 struct i_jetsam_coalition
*cj
;
654 assert(coal
&& coal
->type
== COALITION_TYPE_JETSAM
);
658 assert(queue_empty(&task
->task_coalition
[COALITION_TYPE_JETSAM
]));
660 /* put each task initially in the "other" list */
661 enqueue_tail(&cj
->other
, &task
->task_coalition
[COALITION_TYPE_JETSAM
]);
662 coal_dbg("coalition %lld adopted PID:%d as UNDEF",
663 coal
->id
, task_pid(task
));
669 i_coal_jetsam_remove_task(coalition_t coal
, task_t task
)
671 assert(coal
&& coal
->type
== COALITION_TYPE_JETSAM
);
672 assert(task
->coalition
[COALITION_TYPE_JETSAM
] == coal
);
674 coal_dbg("removing PID:%d from coalition id:%lld",
675 task_pid(task
), coal
->id
);
677 if (task
== coal
->j
.leader
) {
678 coal
->j
.leader
= NULL
;
679 coal_dbg(" PID:%d was the leader!", task_pid(task
));
681 assert(!queue_empty(&task
->task_coalition
[COALITION_TYPE_JETSAM
]));
684 /* remove the task from the specific coalition role queue */
685 remqueue(&task
->task_coalition
[COALITION_TYPE_JETSAM
]);
686 queue_chain_init(task
->task_coalition
[COALITION_TYPE_RESOURCE
]);
692 i_coal_jetsam_set_taskrole(coalition_t coal
, task_t task
, int role
)
694 struct i_jetsam_coalition
*cj
;
696 assert(coal
&& coal
->type
== COALITION_TYPE_JETSAM
);
697 assert(task
->coalition
[COALITION_TYPE_JETSAM
] == coal
);
702 case COALITION_TASKROLE_LEADER
:
703 coal_dbg("setting PID:%d as LEADER of %lld",
704 task_pid(task
), coal
->id
);
705 if (cj
->leader
!= TASK_NULL
) {
706 /* re-queue the exiting leader onto the "other" list */
707 coal_dbg(" re-queue existing leader (%d) as OTHER",
708 task_pid(cj
->leader
));
709 re_queue_tail(&cj
->other
, &cj
->leader
->task_coalition
[COALITION_TYPE_JETSAM
]);
712 * remove the task from the "other" list
713 * (where it was put by default)
715 remqueue(&task
->task_coalition
[COALITION_TYPE_JETSAM
]);
716 queue_chain_init(task
->task_coalition
[COALITION_TYPE_JETSAM
]);
718 /* set the coalition leader */
721 case COALITION_TASKROLE_XPC
:
722 coal_dbg("setting PID:%d as XPC in %lld",
723 task_pid(task
), coal
->id
);
724 q
= (queue_t
)&cj
->services
;
726 case COALITION_TASKROLE_EXT
:
727 coal_dbg("setting PID:%d as EXT in %lld",
728 task_pid(task
), coal
->id
);
729 q
= (queue_t
)&cj
->extensions
;
731 case COALITION_TASKROLE_NONE
:
733 * Tasks with a role of "none" should fall through to an
734 * undefined role so long as the task is currently a member
735 * of the coalition. This scenario can happen if a task is
736 * killed (usually via jetsam) during exec.
738 if (task
->coalition
[COALITION_TYPE_JETSAM
] != coal
) {
739 panic("%s: task %p attempting to set role %d "
740 "in coalition %p to which it does not belong!", __func__
, task
, role
, coal
);
743 case COALITION_TASKROLE_UNDEF
:
744 coal_dbg("setting PID:%d as UNDEF in %lld",
745 task_pid(task
), coal
->id
);
746 q
= (queue_t
)&cj
->other
;
749 panic("%s: invalid role(%d) for task", __func__
, role
);
750 return KERN_INVALID_ARGUMENT
;
754 re_queue_tail(q
, &task
->task_coalition
[COALITION_TYPE_JETSAM
]);
760 i_coal_jetsam_get_taskrole(coalition_t coal
, task_t task
)
762 struct i_jetsam_coalition
*cj
;
765 assert(coal
&& coal
->type
== COALITION_TYPE_JETSAM
);
766 assert(task
->coalition
[COALITION_TYPE_JETSAM
] == coal
);
770 if (task
== cj
->leader
)
771 return COALITION_TASKROLE_LEADER
;
773 qe_foreach_element(t
, &cj
->services
, task_coalition
[COALITION_TYPE_JETSAM
]) {
775 return COALITION_TASKROLE_XPC
;
778 qe_foreach_element(t
, &cj
->extensions
, task_coalition
[COALITION_TYPE_JETSAM
]) {
780 return COALITION_TASKROLE_EXT
;
783 qe_foreach_element(t
, &cj
->other
, task_coalition
[COALITION_TYPE_JETSAM
]) {
785 return COALITION_TASKROLE_UNDEF
;
788 /* task not in the coalition?! */
789 return COALITION_TASKROLE_NONE
;
793 i_coal_jetsam_iterate_tasks(coalition_t coal
, void *ctx
, void (*callback
)(coalition_t
, void *, task_t
))
795 struct i_jetsam_coalition
*cj
;
798 assert(coal
&& coal
->type
== COALITION_TYPE_JETSAM
);
803 callback(coal
, ctx
, cj
->leader
);
805 qe_foreach_element(t
, &cj
->services
, task_coalition
[COALITION_TYPE_JETSAM
])
806 callback(coal
, ctx
, t
);
808 qe_foreach_element(t
, &cj
->extensions
, task_coalition
[COALITION_TYPE_JETSAM
])
809 callback(coal
, ctx
, t
);
811 qe_foreach_element(t
, &cj
->other
, task_coalition
[COALITION_TYPE_JETSAM
])
812 callback(coal
, ctx
, t
);
818 * Main Coalition implementation
823 * coalition_create_internal
824 * Returns: New coalition object, referenced for the caller and unlocked.
825 * Condition: coalitions_list_lock must be UNLOCKED.
828 coalition_create_internal(int type
, int role
, boolean_t privileged
, coalition_t
*out
)
831 struct coalition
*new_coal
;
833 if (type
< 0 || type
> COALITION_TYPE_MAX
)
834 return KERN_INVALID_ARGUMENT
;
836 new_coal
= (struct coalition
*)zalloc(coalition_zone
);
837 if (new_coal
== COALITION_NULL
)
838 return KERN_RESOURCE_SHORTAGE
;
839 bzero(new_coal
, sizeof(*new_coal
));
841 new_coal
->type
= type
;
842 new_coal
->role
= role
;
844 /* initialize type-specific resources */
845 kr
= coal_call(new_coal
, init
, privileged
);
846 if (kr
!= KERN_SUCCESS
) {
847 zfree(coalition_zone
, new_coal
);
851 /* One for caller, one for coalitions list */
852 new_coal
->ref_count
= 2;
854 new_coal
->privileged
= privileged
? TRUE
: FALSE
;
855 #if DEVELOPMENT || DEBUG
856 new_coal
->should_notify
= 1;
859 lck_mtx_init(&new_coal
->lock
, &coalitions_lck_grp
, &coalitions_lck_attr
);
861 lck_mtx_lock(&coalitions_list_lock
);
862 new_coal
->id
= coalition_next_id
++;
864 enqueue_tail(&coalitions_q
, &new_coal
->coalitions
);
866 KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_COALITION
, MACH_COALITION_NEW
),
867 new_coal
->id
, new_coal
->type
);
868 lck_mtx_unlock(&coalitions_list_lock
);
870 coal_dbg("id:%llu, type:%s", new_coal
->id
, coal_type_str(new_coal
->type
));
878 * Condition: coalition must be UNLOCKED.
881 coalition_release(coalition_t coal
)
883 /* TODO: This can be done with atomics. */
884 coalition_lock(coal
);
888 uint32_t rc
= coal
->ref_count
;
889 uint32_t ac
= coal
->active_count
;
890 #endif /* COALITION_DEBUG */
892 coal_dbg("id:%llu type:%s ref_count:%u active_count:%u%s",
893 coal
->id
, coal_type_str(coal
->type
), rc
, ac
,
894 rc
<= 0 ? ", will deallocate now" : "");
896 if (coal
->ref_count
> 0) {
897 coalition_unlock(coal
);
901 assert(coal
->termrequested
);
902 assert(coal
->terminated
);
903 assert(coal
->active_count
== 0);
904 assert(coal
->reaped
);
905 assert(coal
->focal_task_count
== 0);
906 assert(coal
->nonfocal_task_count
== 0);
907 KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_COALITION
, MACH_COALITION_FREE
),
908 coal
->id
, coal
->type
);
910 coal_call(coal
, dealloc
);
912 coalition_unlock(coal
);
914 lck_mtx_destroy(&coal
->lock
, &coalitions_lck_grp
);
916 zfree(coalition_zone
, coal
);
920 * coalition_find_by_id_internal
921 * Returns: Coalition object with specified id, NOT referenced.
922 * If not found, returns COALITION_NULL.
923 * Condition: coalitions_list_lock must be LOCKED.
926 coalition_find_by_id_internal(uint64_t coal_id
)
929 return COALITION_NULL
;
932 lck_mtx_assert(&coalitions_list_lock
, LCK_MTX_ASSERT_OWNED
);
934 qe_foreach_element(coal
, &coalitions_q
, coalitions
) {
935 if (coal
->id
== coal_id
) {
939 return COALITION_NULL
;
943 * coalition_find_by_id
944 * Returns: Coalition object with specified id, referenced.
945 * Condition: coalitions_list_lock must be UNLOCKED.
948 coalition_find_by_id(uint64_t cid
)
951 return COALITION_NULL
;
954 lck_mtx_lock(&coalitions_list_lock
);
956 coalition_t coal
= coalition_find_by_id_internal(cid
);
957 if (coal
== COALITION_NULL
) {
958 lck_mtx_unlock(&coalitions_list_lock
);
959 return COALITION_NULL
;
962 coalition_lock(coal
);
965 coalition_unlock(coal
);
966 lck_mtx_unlock(&coalitions_list_lock
);
967 return COALITION_NULL
;
970 if (coal
->ref_count
== 0) {
971 panic("resurrecting coalition %p id:%llu type:%s, active_count:%u\n",
972 coal
, coal
->id
, coal_type_str(coal
->type
), coal
->active_count
);
976 uint32_t rc
= coal
->ref_count
;
979 coalition_unlock(coal
);
980 lck_mtx_unlock(&coalitions_list_lock
);
982 coal_dbg("id:%llu type:%s ref_count:%u",
983 coal
->id
, coal_type_str(coal
->type
), rc
);
989 * coalition_find_and_activate_by_id
990 * Returns: Coalition object with specified id, referenced, and activated.
991 * Condition: coalitions_list_lock must be UNLOCKED.
992 * This is the function to use when putting a 'new' thing into a coalition,
993 * like posix_spawn of an XPC service by launchd.
994 * See also coalition_extend_active.
997 coalition_find_and_activate_by_id(uint64_t cid
)
1000 return COALITION_NULL
;
1003 lck_mtx_lock(&coalitions_list_lock
);
1005 coalition_t coal
= coalition_find_by_id_internal(cid
);
1006 if (coal
== COALITION_NULL
) {
1007 lck_mtx_unlock(&coalitions_list_lock
);
1008 return COALITION_NULL
;
1011 coalition_lock(coal
);
1013 if (coal
->reaped
|| coal
->terminated
) {
1014 /* Too late to put something new into this coalition, it's
1015 * already on its way out the door */
1016 coalition_unlock(coal
);
1017 lck_mtx_unlock(&coalitions_list_lock
);
1018 return COALITION_NULL
;
1021 if (coal
->ref_count
== 0) {
1022 panic("resurrecting coalition %p id:%llu type:%s, active_count:%u\n",
1023 coal
, coal
->id
, coal_type_str(coal
->type
), coal
->active_count
);
1027 coal
->active_count
++;
1030 uint32_t rc
= coal
->ref_count
;
1031 uint32_t ac
= coal
->active_count
;
1034 coalition_unlock(coal
);
1035 lck_mtx_unlock(&coalitions_list_lock
);
1037 coal_dbg("id:%llu type:%s ref_count:%u, active_count:%u",
1038 coal
->id
, coal_type_str(coal
->type
), rc
, ac
);
1044 coalition_id(coalition_t coal
)
1050 task_coalition_ids(task_t task
, uint64_t ids
[COALITION_NUM_TYPES
])
1053 for (i
= 0; i
< COALITION_NUM_TYPES
; i
++) {
1054 if (task
->coalition
[i
])
1055 ids
[i
] = task
->coalition
[i
]->id
;
1062 task_coalition_roles(task_t task
, int roles
[COALITION_NUM_TYPES
])
1065 memset(roles
, 0, COALITION_NUM_TYPES
* sizeof(roles
[0]));
1067 for (i
= 0; i
< COALITION_NUM_TYPES
; i
++) {
1068 if (task
->coalition
[i
]) {
1069 coalition_lock(task
->coalition
[i
]);
1070 roles
[i
] = coal_call(task
->coalition
[i
],
1071 get_taskrole
, task
);
1072 coalition_unlock(task
->coalition
[i
]);
1074 roles
[i
] = COALITION_TASKROLE_NONE
;
1081 coalition_type(coalition_t coal
)
1087 coalition_term_requested(coalition_t coal
)
1089 return coal
->termrequested
;
1093 coalition_is_terminated(coalition_t coal
)
1095 return coal
->terminated
;
1099 coalition_is_reaped(coalition_t coal
)
1101 return coal
->reaped
;
1105 coalition_is_privileged(coalition_t coal
)
1107 return coal
->privileged
|| unrestrict_coalition_syscalls
;
1111 task_is_in_privileged_coalition(task_t task
, int type
)
1113 if (type
< 0 || type
> COALITION_TYPE_MAX
)
1115 if (unrestrict_coalition_syscalls
)
1117 if (!task
->coalition
[type
])
1119 return task
->coalition
[type
]->privileged
;
1122 void task_coalition_update_gpu_stats(task_t task
, uint64_t gpu_ns_delta
)
1126 assert(task
!= TASK_NULL
);
1127 if (gpu_ns_delta
== 0)
1130 coal
= task
->coalition
[COALITION_TYPE_RESOURCE
];
1131 assert(coal
!= COALITION_NULL
);
1133 coalition_lock(coal
);
1134 coal
->r
.gpu_time
+= gpu_ns_delta
;
1135 coalition_unlock(coal
);
1138 boolean_t
task_coalition_adjust_focal_count(task_t task
, int count
, uint32_t *new_count
)
1140 coalition_t coal
= task
->coalition
[COALITION_FOCAL_TASKS_ACCOUNTING
];
1141 if (coal
== COALITION_NULL
)
1144 *new_count
= hw_atomic_add(&coal
->focal_task_count
, count
);
1145 assert(*new_count
!= UINT32_MAX
);
1149 uint32_t task_coalition_focal_count(task_t task
)
1151 coalition_t coal
= task
->coalition
[COALITION_FOCAL_TASKS_ACCOUNTING
];
1152 if (coal
== COALITION_NULL
)
1155 return coal
->focal_task_count
;
1158 boolean_t
task_coalition_adjust_nonfocal_count(task_t task
, int count
, uint32_t *new_count
)
1160 coalition_t coal
= task
->coalition
[COALITION_FOCAL_TASKS_ACCOUNTING
];
1161 if (coal
== COALITION_NULL
)
1164 *new_count
= hw_atomic_add(&coal
->nonfocal_task_count
, count
);
1165 assert(*new_count
!= UINT32_MAX
);
1169 uint32_t task_coalition_nonfocal_count(task_t task
)
1171 coalition_t coal
= task
->coalition
[COALITION_FOCAL_TASKS_ACCOUNTING
];
1172 if (coal
== COALITION_NULL
)
1175 return coal
->nonfocal_task_count
;
1178 void coalition_set_efficient(coalition_t coal
)
1180 coalition_lock(coal
);
1181 coal
->efficient
= TRUE
;
1182 coalition_unlock(coal
);
1186 void coalition_for_each_task(coalition_t coal
, void *ctx
,
1187 void (*callback
)(coalition_t
, void *, task_t
))
1189 assert(coal
!= COALITION_NULL
);
1191 coal_dbg("iterating tasks in coalition %p id:%llu type:%s, active_count:%u",
1192 coal
, coal
->id
, coal_type_str(coal
->type
), coal
->active_count
);
1194 coalition_lock(coal
);
1196 coal_call(coal
, iterate_tasks
, ctx
, callback
);
1198 coalition_unlock(coal
);
1203 coalition_remove_active(coalition_t coal
)
1205 coalition_lock(coal
);
1207 assert(!coal
->reaped
);
1208 assert(coal
->active_count
> 0);
1210 coal
->active_count
--;
1212 boolean_t do_notify
= FALSE
;
1213 uint64_t notify_id
= 0;
1214 uint32_t notify_flags
= 0;
1215 if (coal
->termrequested
&& coal
->active_count
== 0) {
1216 /* We only notify once, when active_count reaches zero.
1217 * We just decremented, so if it reached zero, we mustn't have
1220 assert(!coal
->terminated
);
1221 coal
->terminated
= TRUE
;
1223 assert(!coal
->notified
);
1225 coal
->notified
= TRUE
;
1226 #if DEVELOPMENT || DEBUG
1227 do_notify
= coal
->should_notify
;
1231 notify_id
= coal
->id
;
1236 uint64_t cid
= coal
->id
;
1237 uint32_t rc
= coal
->ref_count
;
1238 int ac
= coal
->active_count
;
1239 int ct
= coal
->type
;
1241 coalition_unlock(coal
);
1243 coal_dbg("id:%llu type:%s ref_count:%u, active_count:%u,%s",
1244 cid
, coal_type_str(ct
), rc
, ac
, do_notify
? " NOTIFY" : " ");
1247 coalition_notify_user(notify_id
, notify_flags
);
1251 /* Used for kernel_task, launchd, launchd's early boot tasks... */
1253 coalitions_adopt_init_task(task_t task
)
1256 kr
= coalitions_adopt_task(init_coalition
, task
);
1257 if (kr
!= KERN_SUCCESS
) {
1258 panic("failed to adopt task %p into default coalition: %d", task
, kr
);
1263 /* Used for forked corpses. */
1265 coalitions_adopt_corpse_task(task_t task
)
1268 kr
= coalitions_adopt_task(corpse_coalition
, task
);
1269 if (kr
!= KERN_SUCCESS
) {
1270 panic("failed to adopt task %p into corpse coalition: %d", task
, kr
);
1276 * coalition_adopt_task_internal
1277 * Condition: Coalition must be referenced and unlocked. Will fail if coalition
1278 * is already terminated.
1280 static kern_return_t
1281 coalition_adopt_task_internal(coalition_t coal
, task_t task
)
1285 if (task
->coalition
[coal
->type
]) {
1286 return KERN_ALREADY_IN_SET
;
1289 coalition_lock(coal
);
1291 if (coal
->reaped
|| coal
->terminated
) {
1292 coalition_unlock(coal
);
1293 return KERN_TERMINATED
;
1296 kr
= coal_call(coal
, adopt_task
, task
);
1297 if (kr
!= KERN_SUCCESS
)
1300 coal
->active_count
++;
1304 task
->coalition
[coal
->type
] = coal
;
1308 (void)coal
; /* need expression after label */
1309 uint64_t cid
= coal
->id
;
1310 uint32_t rc
= coal
->ref_count
;
1311 uint32_t ct
= coal
->type
;
1313 if (get_task_uniqueid(task
) != UINT64_MAX
) {
1314 /* On 32-bit targets, uniqueid will get truncated to 32 bits */
1315 KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_COALITION
, MACH_COALITION_ADOPT
),
1316 coal
->id
, get_task_uniqueid(task
));
1319 coalition_unlock(coal
);
1321 coal_dbg("task:%d, id:%llu type:%s ref_count:%u, kr=%d",
1322 task_pid(task
), cid
, coal_type_str(ct
), rc
, kr
);
1326 static kern_return_t
1327 coalition_remove_task_internal(task_t task
, int type
)
1331 coalition_t coal
= task
->coalition
[type
];
1334 return KERN_SUCCESS
;
1336 assert(coal
->type
== (uint32_t)type
);
1338 coalition_lock(coal
);
1340 kr
= coal_call(coal
, remove_task
, task
);
1343 uint64_t cid
= coal
->id
;
1344 uint32_t rc
= coal
->ref_count
;
1345 int ac
= coal
->active_count
;
1346 int ct
= coal
->type
;
1348 KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_COALITION
, MACH_COALITION_REMOVE
),
1349 coal
->id
, get_task_uniqueid(task
));
1350 coalition_unlock(coal
);
1352 coal_dbg("id:%llu type:%s ref_count:%u, active_count:%u, kr=%d",
1353 cid
, coal_type_str(ct
), rc
, ac
, kr
);
1355 coalition_remove_active(coal
);
1361 * coalitions_adopt_task
1362 * Condition: All coalitions must be referenced and unlocked.
1363 * Will fail if any coalition is already terminated.
1366 coalitions_adopt_task(coalition_t
*coals
, task_t task
)
1371 if (!coals
|| coals
[COALITION_TYPE_RESOURCE
] == COALITION_NULL
)
1372 return KERN_INVALID_ARGUMENT
;
1374 /* verify that the incoming coalitions are what they say they are */
1375 for (i
= 0; i
< COALITION_NUM_TYPES
; i
++)
1376 if (coals
[i
] && coals
[i
]->type
!= (uint32_t)i
)
1377 return KERN_INVALID_ARGUMENT
;
1379 for (i
= 0; i
< COALITION_NUM_TYPES
; i
++) {
1382 kr
= coalition_adopt_task_internal(coals
[i
], task
);
1383 if (kr
!= KERN_SUCCESS
) {
1384 /* dis-associate any coalitions that just adopted this task */
1386 if (task
->coalition
[i
])
1387 coalition_remove_task_internal(task
, i
);
1396 * coalitions_remove_task
1397 * Condition: task must be referenced and UNLOCKED; all task's coalitions must be UNLOCKED
1400 coalitions_remove_task(task_t task
)
1405 for (i
= 0; i
< COALITION_NUM_TYPES
; i
++) {
1406 kr
= coalition_remove_task_internal(task
, i
);
1407 assert(kr
== KERN_SUCCESS
);
1414 * task_release_coalitions
1415 * helper function to release references to all coalitions in which
1416 * 'task' is a member.
1419 task_release_coalitions(task_t task
)
1422 for (i
= 0; i
< COALITION_NUM_TYPES
; i
++) {
1423 if (task
->coalition
[i
]) {
1424 coalition_release(task
->coalition
[i
]);
1425 } else if (i
== COALITION_TYPE_RESOURCE
) {
1426 panic("deallocating task %p was not a member of a resource coalition", task
);
1432 * coalitions_set_roles
1433 * for each type of coalition, if the task is a member of a coalition of
1434 * that type (given in the coalitions parameter) then set the role of
1435 * the task within that that coalition.
1437 kern_return_t
coalitions_set_roles(coalition_t coalitions
[COALITION_NUM_TYPES
],
1438 task_t task
, int roles
[COALITION_NUM_TYPES
])
1440 kern_return_t kr
= KERN_SUCCESS
;
1443 for (i
= 0; i
< COALITION_NUM_TYPES
; i
++) {
1446 coalition_lock(coalitions
[i
]);
1447 kr
= coal_call(coalitions
[i
], set_taskrole
, task
, roles
[i
]);
1448 coalition_unlock(coalitions
[i
]);
1449 assert(kr
== KERN_SUCCESS
);
1456 * coalition_terminate_internal
1457 * Condition: Coalition must be referenced and UNLOCKED.
1460 coalition_request_terminate_internal(coalition_t coal
)
1462 assert(coal
->type
>= 0 && coal
->type
<= COALITION_TYPE_MAX
);
1464 if (coal
== init_coalition
[coal
->type
]) {
1465 return KERN_DEFAULT_SET
;
1468 coalition_lock(coal
);
1471 coalition_unlock(coal
);
1472 return KERN_INVALID_NAME
;
1475 if (coal
->terminated
|| coal
->termrequested
) {
1476 coalition_unlock(coal
);
1477 return KERN_TERMINATED
;
1480 coal
->termrequested
= TRUE
;
1482 boolean_t do_notify
= FALSE
;
1483 uint64_t note_id
= 0;
1484 uint32_t note_flags
= 0;
1486 if (coal
->active_count
== 0) {
1488 * We only notify once, when active_count reaches zero.
1489 * We just set termrequested to zero. If the active count
1490 * was already at zero (tasks died before we could request
1491 * a termination notification), we should notify.
1493 assert(!coal
->terminated
);
1494 coal
->terminated
= TRUE
;
1496 assert(!coal
->notified
);
1498 coal
->notified
= TRUE
;
1499 #if DEVELOPMENT || DEBUG
1500 do_notify
= coal
->should_notify
;
1508 coalition_unlock(coal
);
1511 coalition_notify_user(note_id
, note_flags
);
1514 return KERN_SUCCESS
;
1518 * coalition_reap_internal
1519 * Condition: Coalition must be referenced and UNLOCKED.
1522 coalition_reap_internal(coalition_t coal
)
1524 assert(coal
->type
<= COALITION_TYPE_MAX
);
1526 if (coal
== init_coalition
[coal
->type
]) {
1527 return KERN_DEFAULT_SET
;
1530 coalition_lock(coal
);
1532 coalition_unlock(coal
);
1533 return KERN_TERMINATED
;
1535 if (!coal
->terminated
) {
1536 coalition_unlock(coal
);
1537 return KERN_FAILURE
;
1539 assert(coal
->termrequested
);
1540 if (coal
->active_count
> 0) {
1541 coalition_unlock(coal
);
1542 return KERN_FAILURE
;
1545 coal
->reaped
= TRUE
;
1547 /* Caller, launchd, and coalitions list should each have a reference */
1548 assert(coal
->ref_count
> 2);
1550 coalition_unlock(coal
);
1552 lck_mtx_lock(&coalitions_list_lock
);
1554 remqueue(&coal
->coalitions
);
1555 lck_mtx_unlock(&coalitions_list_lock
);
1557 /* Release the list's reference and launchd's reference. */
1558 coalition_release(coal
);
1559 coalition_release(coal
);
1561 return KERN_SUCCESS
;
1564 #if DEVELOPMENT || DEBUG
1565 int coalition_should_notify(coalition_t coal
)
1570 coalition_lock(coal
);
1571 should
= coal
->should_notify
;
1572 coalition_unlock(coal
);
1577 void coalition_set_notify(coalition_t coal
, int notify
)
1581 coalition_lock(coal
);
1582 coal
->should_notify
= !!notify
;
1583 coalition_unlock(coal
);
1588 coalitions_init(void)
1592 const struct coalition_type
*ctype
;
1594 coalition_zone
= zinit(
1595 sizeof(struct coalition
),
1596 CONFIG_COALITION_MAX
* sizeof(struct coalition
),
1597 COALITION_CHUNK
* sizeof(struct coalition
),
1599 zone_change(coalition_zone
, Z_NOENCRYPT
, TRUE
);
1600 queue_head_init(coalitions_q
);
1602 if (!PE_parse_boot_argn("unrestrict_coalition_syscalls", &unrestrict_coalition_syscalls
,
1603 sizeof (unrestrict_coalition_syscalls
))) {
1604 unrestrict_coalition_syscalls
= 0;
1607 if (!PE_parse_boot_argn("tg_adaptive", &merge_adaptive_coalitions
,
1608 sizeof (merge_adaptive_coalitions
))) {
1609 merge_adaptive_coalitions
= 0;
1612 lck_grp_attr_setdefault(&coalitions_lck_grp_attr
);
1613 lck_grp_init(&coalitions_lck_grp
, "coalition", &coalitions_lck_grp_attr
);
1614 lck_attr_setdefault(&coalitions_lck_attr
);
1615 lck_mtx_init(&coalitions_list_lock
, &coalitions_lck_grp
, &coalitions_lck_attr
);
1617 init_task_ledgers();
1619 for (i
= 0, ctype
= &s_coalition_types
[0]; i
< COALITION_NUM_TYPES
; ctype
++, i
++) {
1620 /* verify the entry in the global coalition types array */
1621 if (ctype
->type
!= i
||
1624 !ctype
->adopt_task
||
1625 !ctype
->remove_task
) {
1626 panic("%s: Malformed coalition type %s(%d) in slot for type:%s(%d)",
1627 __func__
, coal_type_str(ctype
->type
), ctype
->type
, coal_type_str(i
), i
);
1629 if (!ctype
->has_default
)
1631 kr
= coalition_create_internal(ctype
->type
, COALITION_ROLE_SYSTEM
, TRUE
, &init_coalition
[ctype
->type
]);
1632 if (kr
!= KERN_SUCCESS
)
1633 panic("%s: could not create init %s coalition: kr:%d",
1634 __func__
, coal_type_str(i
), kr
);
1635 kr
= coalition_create_internal(ctype
->type
, COALITION_ROLE_SYSTEM
, FALSE
, &corpse_coalition
[ctype
->type
]);
1636 if (kr
!= KERN_SUCCESS
)
1637 panic("%s: could not create corpse %s coalition: kr:%d",
1638 __func__
, coal_type_str(i
), kr
);
1641 /* "Leak" our reference to the global object */
1645 * BSD Kernel interface functions
1648 static void coalition_fill_procinfo(struct coalition
*coal
,
1649 struct procinfo_coalinfo
*coalinfo
)
1651 coalinfo
->coalition_id
= coal
->id
;
1652 coalinfo
->coalition_type
= coal
->type
;
1653 coalinfo
->coalition_tasks
= coalition_get_task_count(coal
);
1657 int coalitions_get_list(int type
, struct procinfo_coalinfo
*coal_list
, int list_sz
)
1660 struct coalition
*coal
;
1662 lck_mtx_lock(&coalitions_list_lock
);
1663 qe_foreach_element(coal
, &coalitions_q
, coalitions
) {
1664 if (!coal
->reaped
&& (type
< 0 || type
== (int)coal
->type
)) {
1665 if (coal_list
&& ncoals
< list_sz
)
1666 coalition_fill_procinfo(coal
, &coal_list
[ncoals
]);
1670 lck_mtx_unlock(&coalitions_list_lock
);
1676 * Jetsam coalition interface
1679 boolean_t
coalition_is_leader(task_t task
, int coal_type
, coalition_t
*coal
)
1684 if (coal
) /* handle the error cases gracefully */
1685 *coal
= COALITION_NULL
;
1690 if (coal_type
> COALITION_TYPE_MAX
)
1693 c
= task
->coalition
[coal_type
];
1697 assert((int)c
->type
== coal_type
);
1705 if (c
->type
== COALITION_TYPE_JETSAM
&& c
->j
.leader
== task
)
1708 coalition_unlock(c
);
1713 kern_return_t
coalition_iterate_stackshot(coalition_iterate_fn_t callout
, void *arg
, uint32_t coalition_type
)
1718 qe_foreach_element(coal
, &coalitions_q
, coalitions
) {
1719 if (coal
== NULL
|| !ml_validate_nofault((vm_offset_t
)coal
, sizeof(struct coalition
)))
1720 return KERN_FAILURE
;
1722 if (coalition_type
== coal
->type
)
1723 callout(arg
, i
++, coal
);
1726 return KERN_SUCCESS
;
1729 task_t
kdp_coalition_get_leader(coalition_t coal
)
1734 if (coal
->type
== COALITION_TYPE_JETSAM
) {
1735 return coal
->j
.leader
;
1740 task_t
coalition_get_leader(coalition_t coal
)
1742 task_t leader
= TASK_NULL
;
1747 coalition_lock(coal
);
1748 if (coal
->type
!= COALITION_TYPE_JETSAM
)
1751 leader
= coal
->j
.leader
;
1752 if (leader
!= TASK_NULL
)
1753 task_reference(leader
);
1756 coalition_unlock(coal
);
1761 int coalition_get_task_count(coalition_t coal
)
1764 struct queue_entry
*qe
;
1768 coalition_lock(coal
);
1769 switch (coal
->type
) {
1770 case COALITION_TYPE_RESOURCE
:
1771 qe_foreach(qe
, &coal
->r
.tasks
)
1774 case COALITION_TYPE_JETSAM
:
1777 qe_foreach(qe
, &coal
->j
.other
)
1779 qe_foreach(qe
, &coal
->j
.extensions
)
1781 qe_foreach(qe
, &coal
->j
.services
)
1787 coalition_unlock(coal
);
1793 static uint64_t i_get_list_footprint(queue_t list
, int type
, int *ntasks
)
1798 qe_foreach_element(task
, list
, task_coalition
[type
]) {
1799 bytes
+= get_task_phys_footprint(task
);
1800 coal_dbg(" [%d] task_pid:%d, type:%d, footprint:%lld",
1801 *ntasks
, task_pid(task
), type
, bytes
);
1808 uint64_t coalition_get_page_count(coalition_t coal
, int *ntasks
)
1818 coalition_lock(coal
);
1820 switch (coal
->type
) {
1821 case COALITION_TYPE_RESOURCE
:
1822 bytes
+= i_get_list_footprint(&coal
->r
.tasks
, COALITION_TYPE_RESOURCE
, &num_tasks
);
1824 case COALITION_TYPE_JETSAM
:
1825 if (coal
->j
.leader
) {
1826 bytes
+= get_task_phys_footprint(coal
->j
.leader
);
1829 bytes
+= i_get_list_footprint(&coal
->j
.extensions
, COALITION_TYPE_JETSAM
, &num_tasks
);
1830 bytes
+= i_get_list_footprint(&coal
->j
.services
, COALITION_TYPE_JETSAM
, &num_tasks
);
1831 bytes
+= i_get_list_footprint(&coal
->j
.other
, COALITION_TYPE_JETSAM
, &num_tasks
);
1837 coalition_unlock(coal
);
1840 *ntasks
= num_tasks
;
1842 return bytes
/ PAGE_SIZE_64
;
1845 struct coal_sort_s
{
1852 * return < 0 for a < b
1856 typedef int (*cmpfunc_t
)(const void *a
, const void *b
);
1859 qsort(void *a
, size_t n
, size_t es
, cmpfunc_t cmp
);
1861 static int dflt_cmp(const void *a
, const void *b
)
1863 const struct coal_sort_s
*csA
= (const struct coal_sort_s
*)a
;
1864 const struct coal_sort_s
*csB
= (const struct coal_sort_s
*)b
;
1867 * if both A and B are equal, use a memory descending sort
1869 if (csA
->usr_order
== csB
->usr_order
)
1870 return (int)((int64_t)csB
->bytes
- (int64_t)csA
->bytes
);
1872 /* otherwise, return the relationship between user specified orders */
1873 return (csA
->usr_order
- csB
->usr_order
);
1876 static int mem_asc_cmp(const void *a
, const void *b
)
1878 const struct coal_sort_s
*csA
= (const struct coal_sort_s
*)a
;
1879 const struct coal_sort_s
*csB
= (const struct coal_sort_s
*)b
;
1881 return (int)((int64_t)csA
->bytes
- (int64_t)csB
->bytes
);
1884 static int mem_dec_cmp(const void *a
, const void *b
)
1886 const struct coal_sort_s
*csA
= (const struct coal_sort_s
*)a
;
1887 const struct coal_sort_s
*csB
= (const struct coal_sort_s
*)b
;
1889 return (int)((int64_t)csB
->bytes
- (int64_t)csA
->bytes
);
1892 static int usr_asc_cmp(const void *a
, const void *b
)
1894 const struct coal_sort_s
*csA
= (const struct coal_sort_s
*)a
;
1895 const struct coal_sort_s
*csB
= (const struct coal_sort_s
*)b
;
1897 return (csA
->usr_order
- csB
->usr_order
);
1900 static int usr_dec_cmp(const void *a
, const void *b
)
1902 const struct coal_sort_s
*csA
= (const struct coal_sort_s
*)a
;
1903 const struct coal_sort_s
*csB
= (const struct coal_sort_s
*)b
;
1905 return (csB
->usr_order
- csA
->usr_order
);
1908 /* avoid dynamic allocation in this path */
1909 #define MAX_SORTED_PIDS 80
1911 static int coalition_get_sort_list(coalition_t coal
, int sort_order
, queue_t list
,
1912 struct coal_sort_s
*sort_array
, int array_sz
)
1917 assert(sort_array
!= NULL
);
1924 * this function will only be called with a NULL
1925 * list for JETSAM-type coalitions, and is intended
1926 * to investigate the leader process
1928 if (coal
->type
!= COALITION_TYPE_JETSAM
||
1929 coal
->j
.leader
== TASK_NULL
)
1931 sort_array
[0].pid
= task_pid(coal
->j
.leader
);
1932 switch (sort_order
) {
1933 case COALITION_SORT_DEFAULT
:
1934 sort_array
[0].usr_order
= 0;
1936 case COALITION_SORT_MEM_ASC
:
1937 case COALITION_SORT_MEM_DEC
:
1938 sort_array
[0].bytes
= get_task_phys_footprint(coal
->j
.leader
);
1940 case COALITION_SORT_USER_ASC
:
1941 case COALITION_SORT_USER_DEC
:
1942 sort_array
[0].usr_order
= 0;
1950 qe_foreach_element(task
, list
, task_coalition
[coal
->type
]) {
1951 if (ntasks
>= array_sz
) {
1952 printf("WARNING: more than %d pids in coalition %llu\n",
1953 MAX_SORTED_PIDS
, coal
->id
);
1957 sort_array
[ntasks
].pid
= task_pid(task
);
1959 switch (sort_order
) {
1960 case COALITION_SORT_DEFAULT
:
1961 sort_array
[ntasks
].usr_order
= 0;
1963 case COALITION_SORT_MEM_ASC
:
1964 case COALITION_SORT_MEM_DEC
:
1965 sort_array
[ntasks
].bytes
= get_task_phys_footprint(task
);
1967 case COALITION_SORT_USER_ASC
:
1968 case COALITION_SORT_USER_DEC
:
1969 sort_array
[ntasks
].usr_order
= 0;
1981 int coalition_get_pid_list(coalition_t coal
, uint32_t rolemask
, int sort_order
,
1982 int *pid_list
, int list_sz
)
1984 struct i_jetsam_coalition
*cj
;
1986 cmpfunc_t cmp_func
= NULL
;
1987 struct coal_sort_s sort_array
[MAX_SORTED_PIDS
] = { {0,0,0} }; /* keep to < 2k */
1990 !(rolemask
& COALITION_ROLEMASK_ALLROLES
) ||
1991 !pid_list
|| list_sz
< 1) {
1992 coal_dbg("Invalid parameters: coal:%p, type:%d, rolemask:0x%x, "
1993 "pid_list:%p, list_sz:%d", coal
, coal
? coal
->type
: -1,
1994 rolemask
, pid_list
, list_sz
);
1998 switch (sort_order
) {
1999 case COALITION_SORT_NOSORT
:
2002 case COALITION_SORT_DEFAULT
:
2003 cmp_func
= dflt_cmp
;
2005 case COALITION_SORT_MEM_ASC
:
2006 cmp_func
= mem_asc_cmp
;
2008 case COALITION_SORT_MEM_DEC
:
2009 cmp_func
= mem_dec_cmp
;
2011 case COALITION_SORT_USER_ASC
:
2012 cmp_func
= usr_asc_cmp
;
2014 case COALITION_SORT_USER_DEC
:
2015 cmp_func
= usr_dec_cmp
;
2021 coalition_lock(coal
);
2023 if (coal
->type
== COALITION_TYPE_RESOURCE
) {
2024 ntasks
+= coalition_get_sort_list(coal
, sort_order
, &coal
->r
.tasks
,
2025 sort_array
, MAX_SORTED_PIDS
);
2031 if (rolemask
& COALITION_ROLEMASK_UNDEF
)
2032 ntasks
+= coalition_get_sort_list(coal
, sort_order
, &cj
->other
,
2033 sort_array
+ ntasks
,
2034 MAX_SORTED_PIDS
- ntasks
);
2036 if (rolemask
& COALITION_ROLEMASK_XPC
)
2037 ntasks
+= coalition_get_sort_list(coal
, sort_order
, &cj
->services
,
2038 sort_array
+ ntasks
,
2039 MAX_SORTED_PIDS
- ntasks
);
2041 if (rolemask
& COALITION_ROLEMASK_EXT
)
2042 ntasks
+= coalition_get_sort_list(coal
, sort_order
, &cj
->extensions
,
2043 sort_array
+ ntasks
,
2044 MAX_SORTED_PIDS
- ntasks
);
2046 if (rolemask
& COALITION_ROLEMASK_LEADER
)
2047 ntasks
+= coalition_get_sort_list(coal
, sort_order
, NULL
,
2048 sort_array
+ ntasks
,
2049 MAX_SORTED_PIDS
- ntasks
);
2052 coalition_unlock(coal
);
2054 /* sort based on the chosen criterion (no sense sorting 1 item) */
2055 if (cmp_func
&& ntasks
> 1)
2056 qsort(sort_array
, ntasks
, sizeof(struct coal_sort_s
), cmp_func
);
2058 for (int i
= 0; i
< ntasks
; i
++) {
2061 coal_dbg(" [%d] PID:%d, footprint:%lld, usr_order:%d",
2062 i
, sort_array
[i
].pid
, sort_array
[i
].bytes
,
2063 sort_array
[i
].usr_order
);
2064 pid_list
[i
] = sort_array
[i
].pid
;