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/zalloc.h>
41 #include <libkern/OSAtomic.h>
43 #include <mach/coalition_notification_server.h>
44 #include <mach/host_priv.h>
45 #include <mach/host_special_ports.h>
47 #include <sys/errno.h>
50 * BSD interface functions
52 int coalitions_get_list(int type
, struct procinfo_coalinfo
*coal_list
, int list_sz
);
53 boolean_t
coalition_is_leader(task_t task
, int coal_type
, coalition_t
*coal
);
54 task_t
coalition_get_leader(coalition_t coal
);
55 int coalition_get_task_count(coalition_t coal
);
56 uint64_t coalition_get_page_count(coalition_t coal
, int *ntasks
);
57 int coalition_get_pid_list(coalition_t coal
, uint32_t rolemask
, int sort_order
,
58 int *pid_list
, int list_sz
);
60 /* defined in task.c */
61 extern ledger_template_t task_ledger_template
;
64 * Coalition zone needs limits. We expect there will be as many coalitions as
65 * tasks (same order of magnitude), so use the task zone's limits.
67 #define CONFIG_COALITION_MAX CONFIG_TASK_MAX
68 #define COALITION_CHUNK TASK_CHUNK
70 int unrestrict_coalition_syscalls
;
72 lck_attr_t coalitions_lck_attr
;
73 lck_grp_t coalitions_lck_grp
;
74 lck_grp_attr_t coalitions_lck_grp_attr
;
76 /* coalitions_list_lock protects coalition_count, coalitions queue, next_coalition_id. */
77 decl_lck_mtx_data(static,coalitions_list_lock
);
78 static uint64_t coalition_count
;
79 static uint64_t coalition_next_id
= 1;
80 static queue_head_t coalitions_q
;
82 coalition_t init_coalition
[COALITION_NUM_TYPES
];
84 zone_t coalition_zone
;
86 static const char *coal_type_str(int type
)
89 case COALITION_TYPE_RESOURCE
:
91 case COALITION_TYPE_JETSAM
:
98 struct coalition_type
{
103 * pre-condition: coalition just allocated (unlocked), unreferenced,
106 kern_return_t (*init
)(coalition_t coal
, boolean_t privileged
);
110 * pre-condition: coalition unlocked
111 * pre-condition: coalition refcount=0, active_count=0,
112 * termrequested=1, terminated=1, reaped=1
114 void (*dealloc
)(coalition_t coal
);
118 * pre-condition: coalition locked
119 * pre-condition: coalition !repead and !terminated
121 kern_return_t (*adopt_task
)(coalition_t coal
, task_t task
);
125 * pre-condition: coalition locked
126 * pre-condition: task has been removed from coalition's task list
128 kern_return_t (*remove_task
)(coalition_t coal
, task_t task
);
132 * pre-condition: coalition locked
133 * pre-condition: task added to coalition's task list,
134 * active_count >= 1 (at least the given task is active)
136 kern_return_t (*set_taskrole
)(coalition_t coal
, task_t task
, int role
);
140 * pre-condition: coalition locked
141 * pre-condition: task added to coalition's task list,
142 * active_count >= 1 (at least the given task is active)
144 int (*get_taskrole
)(coalition_t coal
, task_t task
);
148 * pre-condition: coalition locked
150 void (*iterate_tasks
)(coalition_t coal
, void *ctx
, void (*callback
)(coalition_t
, void *, task_t
));
154 * COALITION_TYPE_RESOURCE
157 static kern_return_t
i_coal_resource_init(coalition_t coal
, boolean_t privileged
);
158 static void i_coal_resource_dealloc(coalition_t coal
);
159 static kern_return_t
i_coal_resource_adopt_task(coalition_t coal
, task_t task
);
160 static kern_return_t
i_coal_resource_remove_task(coalition_t coal
, task_t task
);
161 static kern_return_t
i_coal_resource_set_taskrole(coalition_t coal
,
162 task_t task
, int role
);
163 static int i_coal_resource_get_taskrole(coalition_t coal
, task_t task
);
164 static void i_coal_resource_iterate_tasks(coalition_t coal
, void *ctx
,
165 void (*callback
)(coalition_t
, void *, task_t
));
167 struct i_resource_coalition
{
170 uint64_t byteswritten
;
172 uint64_t logical_immediate_writes
;
173 uint64_t logical_deferred_writes
;
174 uint64_t logical_invalidated_writes
;
175 uint64_t logical_metadata_writes
;
177 uint64_t task_count
; /* tasks that have started in this coalition */
178 uint64_t dead_task_count
; /* tasks that have exited in this coalition;
179 subtract from task_count to get count
182 * Count the length of time this coalition had at least one active task.
183 * This can be a 'denominator' to turn e.g. cpu_time to %cpu.
185 uint64_t last_became_nonempty_time
;
186 uint64_t time_nonempty
;
188 queue_head_t tasks
; /* List of active tasks in the coalition */
192 * COALITION_TYPE_JETSAM
195 static kern_return_t
i_coal_jetsam_init(coalition_t coal
, boolean_t privileged
);
196 static void i_coal_jetsam_dealloc(coalition_t coal
);
197 static kern_return_t
i_coal_jetsam_adopt_task(coalition_t coal
, task_t task
);
198 static kern_return_t
i_coal_jetsam_remove_task(coalition_t coal
, task_t task
);
199 static kern_return_t
i_coal_jetsam_set_taskrole(coalition_t coal
,
200 task_t task
, int role
);
201 static int i_coal_jetsam_get_taskrole(coalition_t coal
, task_t task
);
202 static void i_coal_jetsam_iterate_tasks(coalition_t coal
, void *ctx
,
203 void (*callback
)(coalition_t
, void *, task_t
));
205 struct i_jetsam_coalition
{
207 queue_head_t extensions
;
208 queue_head_t services
;
214 * main coalition structure
217 uint64_t id
; /* monotonically increasing */
219 uint32_t ref_count
; /* Number of references to the memory containing this struct */
220 uint32_t active_count
; /* Number of members of (tasks in) the
221 coalition, plus vouchers referring
223 uint32_t focal_task_count
; /* Number of TASK_FOREGROUND_APPLICATION tasks in the coalition */
224 uint32_t nonfocal_task_count
; /* Number of TASK_BACKGROUND_APPLICATION tasks in the coalition */
226 /* coalition flags */
227 uint32_t privileged
: 1; /* Members of this coalition may create
228 and manage coalitions and may posix_spawn
229 processes into selected coalitions */
232 uint32_t termrequested
: 1; /* launchd has requested termination when coalition becomes empty */
233 uint32_t terminated
: 1; /* coalition became empty and spawns are now forbidden */
234 uint32_t reaped
: 1; /* reaped, invisible to userspace, but waiting for ref_count to go to zero */
235 uint32_t notified
: 1; /* no-more-processes notification was sent via special port */
236 #if defined(DEVELOPMENT) || defined(DEBUG)
237 uint32_t should_notify
: 1; /* should this coalition send notifications (default: yes) */
240 queue_chain_t coalitions
; /* global list of coalitions */
242 decl_lck_mtx_data(,lock
) /* Coalition lock. */
244 /* put coalition type-specific structures here */
246 struct i_resource_coalition r
;
247 struct i_jetsam_coalition j
;
252 * register different coalition types:
253 * these must be kept in the order specified in coalition.h
255 static const struct coalition_type
256 s_coalition_types
[COALITION_NUM_TYPES
] = {
258 COALITION_TYPE_RESOURCE
,
260 i_coal_resource_init
,
261 i_coal_resource_dealloc
,
262 i_coal_resource_adopt_task
,
263 i_coal_resource_remove_task
,
264 i_coal_resource_set_taskrole
,
265 i_coal_resource_get_taskrole
,
266 i_coal_resource_iterate_tasks
,
269 COALITION_TYPE_JETSAM
,
272 i_coal_jetsam_dealloc
,
273 i_coal_jetsam_adopt_task
,
274 i_coal_jetsam_remove_task
,
275 i_coal_jetsam_set_taskrole
,
276 i_coal_jetsam_get_taskrole
,
277 i_coal_jetsam_iterate_tasks
,
281 #define coal_call(coal, func, ...) \
282 (s_coalition_types[(coal)->type].func)(coal, ## __VA_ARGS__)
285 #define coalition_lock(c) do{ lck_mtx_lock(&c->lock); }while(0)
286 #define coalition_unlock(c) do{ lck_mtx_unlock(&c->lock); }while(0)
289 coalition_notify_user(uint64_t id
, uint32_t flags
)
291 mach_port_t user_port
;
294 kr
= host_get_coalition_port(host_priv_self(), &user_port
);
295 if ((kr
!= KERN_SUCCESS
) || !IPC_PORT_VALID(user_port
)) {
299 coalition_notification(user_port
, id
, flags
);
304 * COALITION_TYPE_RESOURCE
308 i_coal_resource_init(coalition_t coal
, boolean_t privileged
)
311 assert(coal
&& coal
->type
== COALITION_TYPE_RESOURCE
);
312 coal
->r
.ledger
= ledger_instantiate(task_ledger_template
,
313 LEDGER_CREATE_ACTIVE_ENTRIES
);
314 if (coal
->r
.ledger
== NULL
)
315 return KERN_RESOURCE_SHORTAGE
;
317 queue_init(&coal
->r
.tasks
);
323 i_coal_resource_dealloc(coalition_t coal
)
325 assert(coal
&& coal
->type
== COALITION_TYPE_RESOURCE
);
326 ledger_dereference(coal
->r
.ledger
);
330 i_coal_resource_adopt_task(coalition_t coal
, task_t task
)
332 struct i_resource_coalition
*cr
;
334 assert(coal
&& coal
->type
== COALITION_TYPE_RESOURCE
);
335 assert(queue_empty(&task
->task_coalition
[COALITION_TYPE_RESOURCE
]));
340 if (cr
->task_count
< cr
->dead_task_count
) {
341 panic("%s: coalition %p id:%llu type:%s task_count(%llu) < dead_task_count(%llu)",
342 __func__
, coal
, coal
->id
, coal_type_str(coal
->type
),
343 cr
->task_count
, cr
->dead_task_count
);
346 /* If moving from 0->1 active tasks */
347 if (cr
->task_count
- cr
->dead_task_count
== 1) {
348 cr
->last_became_nonempty_time
= mach_absolute_time();
351 /* put the task on the coalition's list of tasks */
352 enqueue_tail(&cr
->tasks
, &task
->task_coalition
[COALITION_TYPE_RESOURCE
]);
354 coal_dbg("Added PID:%d to id:%llu, task_count:%llu, dead_count:%llu, nonempty_time:%llu",
355 task_pid(task
), coal
->id
, cr
->task_count
, cr
->dead_task_count
,
356 cr
->last_became_nonempty_time
);
362 i_coal_resource_remove_task(coalition_t coal
, task_t task
)
364 struct i_resource_coalition
*cr
;
366 assert(coal
&& coal
->type
== COALITION_TYPE_RESOURCE
);
367 assert(task
->coalition
[COALITION_TYPE_RESOURCE
] == coal
);
368 assert(!queue_empty(&task
->task_coalition
[COALITION_TYPE_RESOURCE
]));
371 * handle resource coalition accounting rollup for dead tasks
375 cr
->dead_task_count
++;
377 if (cr
->task_count
< cr
->dead_task_count
) {
378 panic("%s: coalition %p id:%llu type:%s task_count(%llu) < dead_task_count(%llu)",
379 __func__
, coal
, coal
->id
, coal_type_str(coal
->type
), cr
->task_count
, cr
->dead_task_count
);
382 /* If moving from 1->0 active tasks */
383 if (cr
->task_count
- cr
->dead_task_count
== 0) {
384 uint64_t last_time_nonempty
= mach_absolute_time() - cr
->last_became_nonempty_time
;
385 cr
->last_became_nonempty_time
= 0;
386 cr
->time_nonempty
+= last_time_nonempty
;
389 ledger_rollup(cr
->ledger
, task
->ledger
);
390 cr
->bytesread
+= task
->task_io_stats
->disk_reads
.size
;
391 cr
->byteswritten
+= task
->task_io_stats
->total_io
.size
- task
->task_io_stats
->disk_reads
.size
;
392 cr
->gpu_time
+= task_gpu_utilisation(task
);
393 cr
->logical_immediate_writes
+= task
->task_immediate_writes
;
394 cr
->logical_deferred_writes
+= task
->task_deferred_writes
;
395 cr
->logical_invalidated_writes
+= task
->task_invalidated_writes
;
396 cr
->logical_metadata_writes
+= task
->task_metadata_writes
;
398 /* remove the task from the coalition's list */
399 remqueue(&task
->task_coalition
[COALITION_TYPE_RESOURCE
]);
400 queue_chain_init(task
->task_coalition
[COALITION_TYPE_RESOURCE
]);
402 coal_dbg("removed PID:%d from id:%llu, task_count:%llu, dead_count:%llu",
403 task_pid(task
), coal
->id
, cr
->task_count
, cr
->dead_task_count
);
409 i_coal_resource_set_taskrole(__unused coalition_t coal
,
410 __unused task_t task
, __unused
int role
)
416 i_coal_resource_get_taskrole(__unused coalition_t coal
, __unused task_t task
)
420 assert(coal
&& coal
->type
== COALITION_TYPE_RESOURCE
);
422 qe_foreach_element(t
, &coal
->r
.tasks
, task_coalition
[COALITION_TYPE_RESOURCE
]) {
424 return COALITION_TASKROLE_UNDEF
;
431 i_coal_resource_iterate_tasks(coalition_t coal
, void *ctx
, void (*callback
)(coalition_t
, void *, task_t
))
434 assert(coal
&& coal
->type
== COALITION_TYPE_RESOURCE
);
436 qe_foreach_element(t
, &coal
->r
.tasks
, task_coalition
[COALITION_TYPE_RESOURCE
])
437 callback(coal
, ctx
, t
);
441 coalition_resource_usage_internal(coalition_t coal
, struct coalition_resource_usage
*cru_out
)
444 ledger_amount_t credit
, debit
;
446 if (coal
->type
!= COALITION_TYPE_RESOURCE
)
447 return KERN_INVALID_ARGUMENT
;
449 ledger_t sum_ledger
= ledger_instantiate(task_ledger_template
, LEDGER_CREATE_ACTIVE_ENTRIES
);
450 if (sum_ledger
== LEDGER_NULL
)
451 return KERN_RESOURCE_SHORTAGE
;
453 coalition_lock(coal
);
456 * Start with the coalition's ledger, which holds the totals from all
459 ledger_rollup(sum_ledger
, coal
->r
.ledger
);
460 uint64_t bytesread
= coal
->r
.bytesread
;
461 uint64_t byteswritten
= coal
->r
.byteswritten
;
462 uint64_t gpu_time
= coal
->r
.gpu_time
;
463 uint64_t logical_immediate_writes
= coal
->r
.logical_immediate_writes
;
464 uint64_t logical_deferred_writes
= coal
->r
.logical_deferred_writes
;
465 uint64_t logical_invalidated_writes
= coal
->r
.logical_invalidated_writes
;
466 uint64_t logical_metadata_writes
= coal
->r
.logical_metadata_writes
;
467 int64_t cpu_time_billed_to_me
= 0;
468 int64_t cpu_time_billed_to_others
= 0;
470 kr
= ledger_get_balance(sum_ledger
, task_ledgers
.cpu_time_billed_to_me
, (int64_t *)&cpu_time_billed_to_me
);
471 if (kr
!= KERN_SUCCESS
|| cpu_time_billed_to_me
< 0) {
472 #if DEVELOPMENT || DEBUG
473 printf("ledger_get_balance failed or ledger negative in coalition_resource_usage_internal: %lld\n", cpu_time_billed_to_me
);
474 #endif /* DEVELOPMENT || DEBUG */
475 cpu_time_billed_to_me
= 0;
478 kr
= ledger_get_balance(sum_ledger
, task_ledgers
.cpu_time_billed_to_others
, (int64_t *)&cpu_time_billed_to_others
);
479 if (kr
!= KERN_SUCCESS
|| cpu_time_billed_to_others
< 0) {
480 #if DEVELOPMENT || DEBUG
481 printf("ledger_get_balance failed or ledger negative in coalition_resource_usage_internal: %lld\n", cpu_time_billed_to_others
);
482 #endif /* DEVELOPMENT || DEBUG */
483 cpu_time_billed_to_others
= 0;
487 * Add to that all the active tasks' ledgers. Tasks cannot deallocate
488 * out from under us, since we hold the coalition lock.
489 * Do not use the on-behalf of cpu time from ledger for live tasks, since
490 * it will not have cpu time for active linkages between tasks.
493 qe_foreach_element(task
, &coal
->r
.tasks
, task_coalition
[COALITION_TYPE_RESOURCE
]) {
494 ledger_rollup(sum_ledger
, task
->ledger
);
495 bytesread
+= task
->task_io_stats
->disk_reads
.size
;
496 byteswritten
+= task
->task_io_stats
->total_io
.size
- task
->task_io_stats
->disk_reads
.size
;
497 gpu_time
+= task_gpu_utilisation(task
);
498 logical_immediate_writes
+= task
->task_immediate_writes
;
499 logical_deferred_writes
+= task
->task_deferred_writes
;
500 logical_invalidated_writes
+= task
->task_invalidated_writes
;
501 logical_metadata_writes
+= task
->task_metadata_writes
;
502 cpu_time_billed_to_me
+= (int64_t)bank_billed_time_safe(task
);
503 cpu_time_billed_to_others
+= (int64_t)bank_serviced_time_safe(task
);
506 /* collect information from the coalition itself */
507 cru_out
->tasks_started
= coal
->r
.task_count
;
508 cru_out
->tasks_exited
= coal
->r
.dead_task_count
;
510 uint64_t time_nonempty
= coal
->r
.time_nonempty
;
511 uint64_t last_became_nonempty_time
= coal
->r
.last_became_nonempty_time
;
513 coalition_unlock(coal
);
515 /* Copy the totals out of sum_ledger */
516 kr
= ledger_get_entries(sum_ledger
, task_ledgers
.cpu_time
,
518 if (kr
!= KERN_SUCCESS
) {
521 cru_out
->cpu_time
= credit
;
522 cru_out
->cpu_time_billed_to_me
= (uint64_t)cpu_time_billed_to_me
;
523 cru_out
->cpu_time_billed_to_others
= (uint64_t)cpu_time_billed_to_others
;
525 kr
= ledger_get_entries(sum_ledger
, task_ledgers
.interrupt_wakeups
,
527 if (kr
!= KERN_SUCCESS
) {
530 cru_out
->interrupt_wakeups
= credit
;
532 kr
= ledger_get_entries(sum_ledger
, task_ledgers
.platform_idle_wakeups
,
534 if (kr
!= KERN_SUCCESS
) {
537 cru_out
->platform_idle_wakeups
= credit
;
539 cru_out
->bytesread
= bytesread
;
540 cru_out
->byteswritten
= byteswritten
;
541 cru_out
->gpu_time
= gpu_time
;
542 cru_out
->logical_immediate_writes
= logical_immediate_writes
;
543 cru_out
->logical_deferred_writes
= logical_deferred_writes
;
544 cru_out
->logical_invalidated_writes
= logical_invalidated_writes
;
545 cru_out
->logical_metadata_writes
= logical_metadata_writes
;
547 ledger_dereference(sum_ledger
);
548 sum_ledger
= LEDGER_NULL
;
550 if (last_became_nonempty_time
) {
551 time_nonempty
+= mach_absolute_time() - last_became_nonempty_time
;
553 absolutetime_to_nanoseconds(time_nonempty
, &cru_out
->time_nonempty
);
560 * COALITION_TYPE_JETSAM
564 i_coal_jetsam_init(coalition_t coal
, boolean_t privileged
)
566 assert(coal
&& coal
->type
== COALITION_TYPE_JETSAM
);
569 coal
->j
.leader
= TASK_NULL
;
570 queue_head_init(coal
->j
.extensions
);
571 queue_head_init(coal
->j
.services
);
572 queue_head_init(coal
->j
.other
);
578 i_coal_jetsam_dealloc(__unused coalition_t coal
)
580 assert(coal
&& coal
->type
== COALITION_TYPE_JETSAM
);
582 /* the coalition should be completely clear at this point */
583 assert(queue_empty(&coal
->j
.extensions
));
584 assert(queue_empty(&coal
->j
.services
));
585 assert(queue_empty(&coal
->j
.other
));
586 assert(coal
->j
.leader
== TASK_NULL
);
590 i_coal_jetsam_adopt_task(coalition_t coal
, task_t task
)
592 struct i_jetsam_coalition
*cj
;
593 assert(coal
&& coal
->type
== COALITION_TYPE_JETSAM
);
597 assert(queue_empty(&task
->task_coalition
[COALITION_TYPE_JETSAM
]));
599 /* put each task initially in the "other" list */
600 enqueue_tail(&cj
->other
, &task
->task_coalition
[COALITION_TYPE_JETSAM
]);
601 coal_dbg("coalition %lld adopted PID:%d as UNDEF",
602 coal
->id
, task_pid(task
));
608 i_coal_jetsam_remove_task(coalition_t coal
, task_t task
)
610 assert(coal
&& coal
->type
== COALITION_TYPE_JETSAM
);
611 assert(task
->coalition
[COALITION_TYPE_JETSAM
] == coal
);
613 coal_dbg("removing PID:%d from coalition id:%lld",
614 task_pid(task
), coal
->id
);
616 if (task
== coal
->j
.leader
) {
617 coal
->j
.leader
= NULL
;
618 coal_dbg(" PID:%d was the leader!", task_pid(task
));
620 assert(!queue_empty(&task
->task_coalition
[COALITION_TYPE_JETSAM
]));
623 /* remove the task from the specific coalition role queue */
624 remqueue(&task
->task_coalition
[COALITION_TYPE_JETSAM
]);
625 queue_chain_init(task
->task_coalition
[COALITION_TYPE_RESOURCE
]);
631 i_coal_jetsam_set_taskrole(coalition_t coal
, task_t task
, int role
)
633 struct i_jetsam_coalition
*cj
;
635 assert(coal
&& coal
->type
== COALITION_TYPE_JETSAM
);
636 assert(task
->coalition
[COALITION_TYPE_JETSAM
] == coal
);
641 case COALITION_TASKROLE_LEADER
:
642 coal_dbg("setting PID:%d as LEADER of %lld",
643 task_pid(task
), coal
->id
);
644 if (cj
->leader
!= TASK_NULL
) {
645 /* re-queue the exiting leader onto the "other" list */
646 coal_dbg(" re-queue existing leader (%d) as OTHER",
647 task_pid(cj
->leader
));
648 re_queue_tail(&cj
->other
, &cj
->leader
->task_coalition
[COALITION_TYPE_JETSAM
]);
651 * remove the task from the "other" list
652 * (where it was put by default)
654 remqueue(&task
->task_coalition
[COALITION_TYPE_JETSAM
]);
655 queue_chain_init(task
->task_coalition
[COALITION_TYPE_JETSAM
]);
657 /* set the coalition leader */
660 case COALITION_TASKROLE_UNDEF
:
661 coal_dbg("setting PID:%d as UNDEF in %lld",
662 task_pid(task
), coal
->id
);
663 q
= (queue_t
)&cj
->other
;
665 case COALITION_TASKROLE_XPC
:
666 coal_dbg("setting PID:%d as XPC in %lld",
667 task_pid(task
), coal
->id
);
668 q
= (queue_t
)&cj
->services
;
670 case COALITION_TASKROLE_EXT
:
671 coal_dbg("setting PID:%d as EXT in %lld",
672 task_pid(task
), coal
->id
);
673 q
= (queue_t
)&cj
->extensions
;
676 panic("%s: invalid role(%d) for task", __func__
, role
);
677 return KERN_INVALID_ARGUMENT
;
681 re_queue_tail(q
, &task
->task_coalition
[COALITION_TYPE_JETSAM
]);
687 i_coal_jetsam_get_taskrole(coalition_t coal
, task_t task
)
689 struct i_jetsam_coalition
*cj
;
692 assert(coal
&& coal
->type
== COALITION_TYPE_JETSAM
);
693 assert(task
->coalition
[COALITION_TYPE_JETSAM
] == coal
);
697 if (task
== cj
->leader
)
698 return COALITION_TASKROLE_LEADER
;
700 qe_foreach_element(t
, &cj
->services
, task_coalition
[COALITION_TYPE_JETSAM
]) {
702 return COALITION_TASKROLE_XPC
;
705 qe_foreach_element(t
, &cj
->extensions
, task_coalition
[COALITION_TYPE_JETSAM
]) {
707 return COALITION_TASKROLE_EXT
;
710 qe_foreach_element(t
, &cj
->other
, task_coalition
[COALITION_TYPE_JETSAM
]) {
712 return COALITION_TASKROLE_UNDEF
;
715 /* task not in the coalition?! */
720 i_coal_jetsam_iterate_tasks(coalition_t coal
, void *ctx
, void (*callback
)(coalition_t
, void *, task_t
))
722 struct i_jetsam_coalition
*cj
;
725 assert(coal
&& coal
->type
== COALITION_TYPE_JETSAM
);
730 callback(coal
, ctx
, cj
->leader
);
732 qe_foreach_element(t
, &cj
->services
, task_coalition
[COALITION_TYPE_JETSAM
])
733 callback(coal
, ctx
, t
);
735 qe_foreach_element(t
, &cj
->extensions
, task_coalition
[COALITION_TYPE_JETSAM
])
736 callback(coal
, ctx
, t
);
738 qe_foreach_element(t
, &cj
->other
, task_coalition
[COALITION_TYPE_JETSAM
])
739 callback(coal
, ctx
, t
);
745 * Main Coalition implementation
750 * coalition_create_internal
751 * Returns: New coalition object, referenced for the caller and unlocked.
752 * Condition: coalitions_list_lock must be UNLOCKED.
755 coalition_create_internal(int type
, boolean_t privileged
, coalition_t
*out
)
758 struct coalition
*new_coal
;
760 if (type
< 0 || type
> COALITION_TYPE_MAX
)
761 return KERN_INVALID_ARGUMENT
;
763 new_coal
= (struct coalition
*)zalloc(coalition_zone
);
764 if (new_coal
== COALITION_NULL
)
765 return KERN_RESOURCE_SHORTAGE
;
766 bzero(new_coal
, sizeof(*new_coal
));
768 new_coal
->type
= type
;
770 /* initialize type-specific resources */
771 kr
= coal_call(new_coal
, init
, privileged
);
772 if (kr
!= KERN_SUCCESS
) {
773 zfree(coalition_zone
, new_coal
);
777 /* One for caller, one for coalitions list */
778 new_coal
->ref_count
= 2;
780 new_coal
->privileged
= privileged
? TRUE
: FALSE
;
781 #if defined(DEVELOPMENT) || defined(DEBUG)
782 new_coal
->should_notify
= 1;
785 lck_mtx_init(&new_coal
->lock
, &coalitions_lck_grp
, &coalitions_lck_attr
);
787 lck_mtx_lock(&coalitions_list_lock
);
788 new_coal
->id
= coalition_next_id
++;
790 enqueue_tail(&coalitions_q
, &new_coal
->coalitions
);
791 lck_mtx_unlock(&coalitions_list_lock
);
793 coal_dbg("id:%llu, type:%s", new_coal
->id
, coal_type_str(new_coal
->type
));
801 * Condition: coalition must be UNLOCKED.
804 coalition_release(coalition_t coal
)
806 /* TODO: This can be done with atomics. */
807 coalition_lock(coal
);
811 uint32_t rc
= coal
->ref_count
;
812 uint32_t ac
= coal
->active_count
;
813 #endif /* COALITION_DEBUG */
815 coal_dbg("id:%llu type:%s ref_count:%u active_count:%u%s",
816 coal
->id
, coal_type_str(coal
->type
), rc
, ac
,
817 rc
<= 0 ? ", will deallocate now" : "");
819 if (coal
->ref_count
> 0) {
820 coalition_unlock(coal
);
824 assert(coal
->termrequested
);
825 assert(coal
->terminated
);
826 assert(coal
->active_count
== 0);
827 assert(coal
->reaped
);
828 assert(coal
->focal_task_count
== 0);
829 assert(coal
->nonfocal_task_count
== 0);
831 coal_call(coal
, dealloc
);
833 coalition_unlock(coal
);
835 lck_mtx_destroy(&coal
->lock
, &coalitions_lck_grp
);
837 zfree(coalition_zone
, coal
);
841 * coalition_find_by_id_internal
842 * Returns: Coalition object with specified id, NOT referenced.
843 * If not found, returns COALITION_NULL.
844 * Condition: coalitions_list_lock must be LOCKED.
847 coalition_find_by_id_internal(uint64_t coal_id
)
850 return COALITION_NULL
;
853 lck_mtx_assert(&coalitions_list_lock
, LCK_MTX_ASSERT_OWNED
);
855 qe_foreach_element(coal
, &coalitions_q
, coalitions
) {
856 if (coal
->id
== coal_id
) {
860 return COALITION_NULL
;
864 * coalition_find_by_id
865 * Returns: Coalition object with specified id, referenced.
866 * Condition: coalitions_list_lock must be UNLOCKED.
869 coalition_find_by_id(uint64_t cid
)
872 return COALITION_NULL
;
875 lck_mtx_lock(&coalitions_list_lock
);
877 coalition_t coal
= coalition_find_by_id_internal(cid
);
878 if (coal
== COALITION_NULL
) {
879 lck_mtx_unlock(&coalitions_list_lock
);
880 return COALITION_NULL
;
883 coalition_lock(coal
);
886 coalition_unlock(coal
);
887 lck_mtx_unlock(&coalitions_list_lock
);
888 return COALITION_NULL
;
891 if (coal
->ref_count
== 0) {
892 panic("resurrecting coalition %p id:%llu type:%s, active_count:%u\n",
893 coal
, coal
->id
, coal_type_str(coal
->type
), coal
->active_count
);
897 uint32_t rc
= coal
->ref_count
;
900 coalition_unlock(coal
);
901 lck_mtx_unlock(&coalitions_list_lock
);
903 coal_dbg("id:%llu type:%s ref_count:%u",
904 coal
->id
, coal_type_str(coal
->type
), rc
);
910 * coalition_find_and_activate_by_id
911 * Returns: Coalition object with specified id, referenced, and activated.
912 * Condition: coalitions_list_lock must be UNLOCKED.
913 * This is the function to use when putting a 'new' thing into a coalition,
914 * like posix_spawn of an XPC service by launchd.
915 * See also coalition_extend_active.
918 coalition_find_and_activate_by_id(uint64_t cid
)
921 return COALITION_NULL
;
924 lck_mtx_lock(&coalitions_list_lock
);
926 coalition_t coal
= coalition_find_by_id_internal(cid
);
927 if (coal
== COALITION_NULL
) {
928 lck_mtx_unlock(&coalitions_list_lock
);
929 return COALITION_NULL
;
932 coalition_lock(coal
);
934 if (coal
->reaped
|| coal
->terminated
) {
935 /* Too late to put something new into this coalition, it's
936 * already on its way out the door */
937 coalition_unlock(coal
);
938 lck_mtx_unlock(&coalitions_list_lock
);
939 return COALITION_NULL
;
942 if (coal
->ref_count
== 0) {
943 panic("resurrecting coalition %p id:%llu type:%s, active_count:%u\n",
944 coal
, coal
->id
, coal_type_str(coal
->type
), coal
->active_count
);
948 coal
->active_count
++;
951 uint32_t rc
= coal
->ref_count
;
952 uint32_t ac
= coal
->active_count
;
955 coalition_unlock(coal
);
956 lck_mtx_unlock(&coalitions_list_lock
);
958 coal_dbg("id:%llu type:%s ref_count:%u, active_count:%u",
959 coal
->id
, coal_type_str(coal
->type
), rc
, ac
);
965 coalition_id(coalition_t coal
)
971 task_coalition_ids(task_t task
, uint64_t ids
[COALITION_NUM_TYPES
])
974 for (i
= 0; i
< COALITION_NUM_TYPES
; i
++) {
975 if (task
->coalition
[i
])
976 ids
[i
] = task
->coalition
[i
]->id
;
983 task_coalition_roles(task_t task
, int roles
[COALITION_NUM_TYPES
])
986 memset(roles
, 0, COALITION_NUM_TYPES
* sizeof(roles
[0]));
988 for (i
= 0; i
< COALITION_NUM_TYPES
; i
++) {
989 if (task
->coalition
[i
]) {
990 coalition_lock(task
->coalition
[i
]);
991 roles
[i
] = coal_call(task
->coalition
[i
],
993 coalition_unlock(task
->coalition
[i
]);
1002 coalition_type(coalition_t coal
)
1008 coalition_is_privileged(coalition_t coal
)
1010 return coal
->privileged
|| unrestrict_coalition_syscalls
;
1014 task_is_in_privileged_coalition(task_t task
, int type
)
1016 if (type
< 0 || type
> COALITION_TYPE_MAX
)
1018 if (unrestrict_coalition_syscalls
)
1020 if (!task
->coalition
[type
])
1022 return task
->coalition
[type
]->privileged
;
1025 void task_coalition_update_gpu_stats(task_t task
, uint64_t gpu_ns_delta
)
1029 assert(task
!= TASK_NULL
);
1030 if (gpu_ns_delta
== 0)
1033 coal
= task
->coalition
[COALITION_TYPE_RESOURCE
];
1034 assert(coal
!= COALITION_NULL
);
1036 coalition_lock(coal
);
1037 coal
->r
.gpu_time
+= gpu_ns_delta
;
1038 coalition_unlock(coal
);
1041 uint32_t task_coalition_adjust_focal_count(task_t task
, int count
)
1047 * For now: only use the resource coalition. Perhaps in the
1048 * future we may combine all coalition types, or even make
1049 * a special coalition type just for this.
1051 coal
= task
->coalition
[COALITION_TYPE_RESOURCE
];
1052 assert(coal
!= COALITION_NULL
);
1054 ret
= hw_atomic_add(&coal
->focal_task_count
, count
);
1056 /* catch underflow */
1057 assert(ret
!= UINT32_MAX
);
1061 uint32_t task_coalition_focal_count(task_t task
)
1064 coal
= task
->coalition
[COALITION_TYPE_RESOURCE
];
1065 assert(coal
!= COALITION_NULL
);
1067 return coal
->focal_task_count
;
1070 uint32_t task_coalition_adjust_nonfocal_count(task_t task
, int count
)
1076 * For now: only use the resource coalition. Perhaps in the
1077 * future we may combine all coalition types, or even make
1078 * a special coalition type just for this.
1080 coal
= task
->coalition
[COALITION_TYPE_RESOURCE
];
1081 assert(coal
!= COALITION_NULL
);
1083 ret
= hw_atomic_add(&coal
->nonfocal_task_count
, count
);
1085 /* catch underflow */
1086 assert(ret
!= UINT32_MAX
);
1090 uint32_t task_coalition_nonfocal_count(task_t task
)
1093 coal
= task
->coalition
[COALITION_TYPE_RESOURCE
];
1094 assert(coal
!= COALITION_NULL
);
1096 return coal
->nonfocal_task_count
;
1099 void coalition_for_each_task(coalition_t coal
, void *ctx
,
1100 void (*callback
)(coalition_t
, void *, task_t
))
1102 assert(coal
!= COALITION_NULL
);
1104 coal_dbg("iterating tasks in coalition %p id:%llu type:%s, active_count:%u",
1105 coal
, coal
->id
, coal_type_str(coal
->type
), coal
->active_count
);
1107 coalition_lock(coal
);
1109 coal_call(coal
, iterate_tasks
, ctx
, callback
);
1111 coalition_unlock(coal
);
1116 coalition_remove_active(coalition_t coal
)
1118 coalition_lock(coal
);
1120 assert(!coal
->reaped
);
1121 assert(coal
->active_count
> 0);
1123 coal
->active_count
--;
1125 boolean_t do_notify
= FALSE
;
1126 uint64_t notify_id
= 0;
1127 uint32_t notify_flags
= 0;
1128 if (coal
->termrequested
&& coal
->active_count
== 0) {
1129 /* We only notify once, when active_count reaches zero.
1130 * We just decremented, so if it reached zero, we mustn't have
1133 assert(!coal
->terminated
);
1134 coal
->terminated
= TRUE
;
1136 assert(!coal
->notified
);
1138 coal
->notified
= TRUE
;
1139 #if defined(DEVELOPMENT) || defined(DEBUG)
1140 do_notify
= coal
->should_notify
;
1144 notify_id
= coal
->id
;
1149 uint64_t cid
= coal
->id
;
1150 uint32_t rc
= coal
->ref_count
;
1151 int ac
= coal
->active_count
;
1152 int ct
= coal
->type
;
1154 coalition_unlock(coal
);
1156 coal_dbg("id:%llu type:%s ref_count:%u, active_count:%u,%s",
1157 cid
, coal_type_str(ct
), rc
, ac
, do_notify
? " NOTIFY" : " ");
1160 coalition_notify_user(notify_id
, notify_flags
);
1164 /* Used for kernel_task, launchd, launchd's early boot tasks... */
1166 coalitions_adopt_init_task(task_t task
)
1169 kr
= coalitions_adopt_task(init_coalition
, task
);
1170 if (kr
!= KERN_SUCCESS
) {
1171 panic("failed to adopt task %p into default coalition: %d", task
, kr
);
1177 * coalition_adopt_task_internal
1178 * Condition: Coalition must be referenced and unlocked. Will fail if coalition
1179 * is already terminated.
1181 static kern_return_t
1182 coalition_adopt_task_internal(coalition_t coal
, task_t task
)
1186 if (task
->coalition
[coal
->type
]) {
1187 return KERN_ALREADY_IN_SET
;
1190 coalition_lock(coal
);
1192 if (coal
->reaped
|| coal
->terminated
) {
1193 coalition_unlock(coal
);
1194 return KERN_TERMINATED
;
1197 kr
= coal_call(coal
, adopt_task
, task
);
1198 if (kr
!= KERN_SUCCESS
)
1201 coal
->active_count
++;
1205 task
->coalition
[coal
->type
] = coal
;
1209 (void)coal
; /* need expression after label */
1210 uint64_t cid
= coal
->id
;
1211 uint32_t rc
= coal
->ref_count
;
1212 uint32_t ct
= coal
->type
;
1214 coalition_unlock(coal
);
1216 coal_dbg("task:%d, id:%llu type:%s ref_count:%u, kr=%d",
1217 task_pid(task
), cid
, coal_type_str(ct
), rc
, kr
);
1221 static kern_return_t
1222 coalition_remove_task_internal(task_t task
, int type
)
1226 coalition_t coal
= task
->coalition
[type
];
1229 return KERN_SUCCESS
;
1231 assert(coal
->type
== (uint32_t)type
);
1233 coalition_lock(coal
);
1235 kr
= coal_call(coal
, remove_task
, task
);
1238 uint64_t cid
= coal
->id
;
1239 uint32_t rc
= coal
->ref_count
;
1240 int ac
= coal
->active_count
;
1241 int ct
= coal
->type
;
1243 coalition_unlock(coal
);
1245 coal_dbg("id:%llu type:%s ref_count:%u, active_count:%u, kr=%d",
1246 cid
, coal_type_str(ct
), rc
, ac
, kr
);
1248 coalition_remove_active(coal
);
1254 * coalitions_adopt_task
1255 * Condition: All coalitions must be referenced and unlocked.
1256 * Will fail if any coalition is already terminated.
1259 coalitions_adopt_task(coalition_t
*coals
, task_t task
)
1264 if (!coals
|| coals
[COALITION_TYPE_RESOURCE
] == COALITION_NULL
)
1265 return KERN_INVALID_ARGUMENT
;
1267 /* verify that the incoming coalitions are what they say they are */
1268 for (i
= 0; i
< COALITION_NUM_TYPES
; i
++)
1269 if (coals
[i
] && coals
[i
]->type
!= (uint32_t)i
)
1270 return KERN_INVALID_ARGUMENT
;
1272 for (i
= 0; i
< COALITION_NUM_TYPES
; i
++) {
1275 kr
= coalition_adopt_task_internal(coals
[i
], task
);
1276 if (kr
!= KERN_SUCCESS
) {
1277 /* dis-associate any coalitions that just adopted this task */
1279 if (task
->coalition
[i
])
1280 coalition_remove_task_internal(task
, i
);
1289 * coalitions_remove_task
1290 * Condition: task must be referenced and UNLOCKED; all task's coalitions must be UNLOCKED
1293 coalitions_remove_task(task_t task
)
1298 for (i
= 0; i
< COALITION_NUM_TYPES
; i
++) {
1299 kr
= coalition_remove_task_internal(task
, i
);
1300 assert(kr
== KERN_SUCCESS
);
1307 * task_release_coalitions
1308 * helper function to release references to all coalitions in which
1309 * 'task' is a member.
1312 task_release_coalitions(task_t task
)
1315 for (i
= 0; i
< COALITION_NUM_TYPES
; i
++) {
1316 if (task
->coalition
[i
])
1317 coalition_release(task
->coalition
[i
]);
1322 * coalitions_set_roles
1323 * for each type of coalition, if the task is a member of a coalition of
1324 * that type (given in the coalitions parameter) then set the role of
1325 * the task within that that coalition.
1327 kern_return_t
coalitions_set_roles(coalition_t coalitions
[COALITION_NUM_TYPES
],
1328 task_t task
, int roles
[COALITION_NUM_TYPES
])
1330 kern_return_t kr
= KERN_SUCCESS
;
1333 for (i
= 0; i
< COALITION_NUM_TYPES
; i
++) {
1336 coalition_lock(coalitions
[i
]);
1337 kr
= coal_call(coalitions
[i
], set_taskrole
, task
, roles
[i
]);
1338 coalition_unlock(coalitions
[i
]);
1339 assert(kr
== KERN_SUCCESS
);
1346 * coalition_terminate_internal
1347 * Condition: Coalition must be referenced and UNLOCKED.
1350 coalition_request_terminate_internal(coalition_t coal
)
1352 assert(coal
->type
>= 0 && coal
->type
<= COALITION_TYPE_MAX
);
1354 if (coal
== init_coalition
[coal
->type
]) {
1355 return KERN_DEFAULT_SET
;
1358 coalition_lock(coal
);
1361 coalition_unlock(coal
);
1362 return KERN_INVALID_NAME
;
1365 if (coal
->terminated
|| coal
->termrequested
) {
1366 coalition_unlock(coal
);
1367 return KERN_TERMINATED
;
1370 coal
->termrequested
= TRUE
;
1372 boolean_t do_notify
= FALSE
;
1373 uint64_t note_id
= 0;
1374 uint32_t note_flags
= 0;
1376 if (coal
->active_count
== 0) {
1378 * We only notify once, when active_count reaches zero.
1379 * We just set termrequested to zero. If the active count
1380 * was already at zero (tasks died before we could request
1381 * a termination notification), we should notify.
1383 assert(!coal
->terminated
);
1384 coal
->terminated
= TRUE
;
1386 assert(!coal
->notified
);
1388 coal
->notified
= TRUE
;
1389 #if defined(DEVELOPMENT) || defined(DEBUG)
1390 do_notify
= coal
->should_notify
;
1398 coalition_unlock(coal
);
1401 coalition_notify_user(note_id
, note_flags
);
1404 return KERN_SUCCESS
;
1408 * coalition_reap_internal
1409 * Condition: Coalition must be referenced and UNLOCKED.
1412 coalition_reap_internal(coalition_t coal
)
1414 assert(coal
->type
<= COALITION_TYPE_MAX
);
1416 if (coal
== init_coalition
[coal
->type
]) {
1417 return KERN_DEFAULT_SET
;
1420 coalition_lock(coal
);
1422 coalition_unlock(coal
);
1423 return KERN_TERMINATED
;
1425 if (!coal
->terminated
) {
1426 coalition_unlock(coal
);
1427 return KERN_FAILURE
;
1429 assert(coal
->termrequested
);
1430 if (coal
->active_count
> 0) {
1431 coalition_unlock(coal
);
1432 return KERN_FAILURE
;
1435 coal
->reaped
= TRUE
;
1437 /* Caller, launchd, and coalitions list should each have a reference */
1438 assert(coal
->ref_count
> 2);
1440 coalition_unlock(coal
);
1442 lck_mtx_lock(&coalitions_list_lock
);
1444 remqueue(&coal
->coalitions
);
1445 lck_mtx_unlock(&coalitions_list_lock
);
1447 /* Release the list's reference and launchd's reference. */
1448 coalition_release(coal
);
1449 coalition_release(coal
);
1451 return KERN_SUCCESS
;
1454 #if defined(DEVELOPMENT) || defined(DEBUG)
1455 int coalition_should_notify(coalition_t coal
)
1460 coalition_lock(coal
);
1461 should
= coal
->should_notify
;
1462 coalition_unlock(coal
);
1467 void coalition_set_notify(coalition_t coal
, int notify
)
1471 coalition_lock(coal
);
1472 coal
->should_notify
= !!notify
;
1473 coalition_unlock(coal
);
1478 coalitions_init(void)
1482 const struct coalition_type
*ctype
;
1484 coalition_zone
= zinit(
1485 sizeof(struct coalition
),
1486 CONFIG_COALITION_MAX
* sizeof(struct coalition
),
1487 COALITION_CHUNK
* sizeof(struct coalition
),
1489 zone_change(coalition_zone
, Z_NOENCRYPT
, TRUE
);
1490 queue_head_init(coalitions_q
);
1492 if (!PE_parse_boot_argn("unrestrict_coalition_syscalls", &unrestrict_coalition_syscalls
,
1493 sizeof (unrestrict_coalition_syscalls
))) {
1494 unrestrict_coalition_syscalls
= 0;
1497 lck_grp_attr_setdefault(&coalitions_lck_grp_attr
);
1498 lck_grp_init(&coalitions_lck_grp
, "coalition", &coalitions_lck_grp_attr
);
1499 lck_attr_setdefault(&coalitions_lck_attr
);
1500 lck_mtx_init(&coalitions_list_lock
, &coalitions_lck_grp
, &coalitions_lck_attr
);
1502 init_task_ledgers();
1504 for (i
= 0, ctype
= &s_coalition_types
[0]; i
< COALITION_NUM_TYPES
; ctype
++, i
++) {
1505 /* verify the entry in the global coalition types array */
1506 if (ctype
->type
!= i
||
1509 !ctype
->adopt_task
||
1510 !ctype
->remove_task
) {
1511 panic("%s: Malformed coalition type %s(%d) in slot for type:%s(%d)",
1512 __func__
, coal_type_str(ctype
->type
), ctype
->type
, coal_type_str(i
), i
);
1514 if (!ctype
->has_default
)
1516 kr
= coalition_create_internal(ctype
->type
, TRUE
, &init_coalition
[ctype
->type
]);
1517 if (kr
!= KERN_SUCCESS
)
1518 panic("%s: could not create init %s coalition: kr:%d",
1519 __func__
, coal_type_str(i
), kr
);
1522 /* "Leak" our reference to the global object */
1526 * BSD Kernel interface functions
1529 static void coalition_fill_procinfo(struct coalition
*coal
,
1530 struct procinfo_coalinfo
*coalinfo
)
1532 coalinfo
->coalition_id
= coal
->id
;
1533 coalinfo
->coalition_type
= coal
->type
;
1534 coalinfo
->coalition_tasks
= coalition_get_task_count(coal
);
1538 int coalitions_get_list(int type
, struct procinfo_coalinfo
*coal_list
, int list_sz
)
1541 struct coalition
*coal
;
1543 lck_mtx_lock(&coalitions_list_lock
);
1544 qe_foreach_element(coal
, &coalitions_q
, coalitions
) {
1545 if (!coal
->reaped
&& (type
< 0 || type
== (int)coal
->type
)) {
1546 if (coal_list
&& ncoals
< list_sz
)
1547 coalition_fill_procinfo(coal
, &coal_list
[ncoals
]);
1551 lck_mtx_unlock(&coalitions_list_lock
);
1557 * Jetsam coalition interface
1560 boolean_t
coalition_is_leader(task_t task
, int coal_type
, coalition_t
*coal
)
1565 if (coal
) /* handle the error cases gracefully */
1566 *coal
= COALITION_NULL
;
1571 if (coal_type
> COALITION_TYPE_MAX
)
1574 c
= task
->coalition
[coal_type
];
1578 assert((int)c
->type
== coal_type
);
1586 if (c
->type
== COALITION_TYPE_JETSAM
&& c
->j
.leader
== task
)
1589 coalition_unlock(c
);
1595 task_t
coalition_get_leader(coalition_t coal
)
1597 task_t leader
= TASK_NULL
;
1602 coalition_lock(coal
);
1603 if (coal
->type
!= COALITION_TYPE_JETSAM
)
1606 leader
= coal
->j
.leader
;
1607 if (leader
!= TASK_NULL
)
1608 task_reference(leader
);
1611 coalition_unlock(coal
);
1616 int coalition_get_task_count(coalition_t coal
)
1619 struct queue_entry
*qe
;
1623 coalition_lock(coal
);
1624 switch (coal
->type
) {
1625 case COALITION_TYPE_RESOURCE
:
1626 qe_foreach(qe
, &coal
->r
.tasks
)
1629 case COALITION_TYPE_JETSAM
:
1632 qe_foreach(qe
, &coal
->j
.other
)
1634 qe_foreach(qe
, &coal
->j
.extensions
)
1636 qe_foreach(qe
, &coal
->j
.services
)
1642 coalition_unlock(coal
);
1648 static uint64_t i_get_list_footprint(queue_t list
, int type
, int *ntasks
)
1653 qe_foreach_element(task
, list
, task_coalition
[type
]) {
1654 bytes
+= get_task_phys_footprint(task
);
1655 coal_dbg(" [%d] task_pid:%d, type:%d, footprint:%lld",
1656 *ntasks
, task_pid(task
), type
, bytes
);
1663 uint64_t coalition_get_page_count(coalition_t coal
, int *ntasks
)
1673 coalition_lock(coal
);
1675 switch (coal
->type
) {
1676 case COALITION_TYPE_RESOURCE
:
1677 bytes
+= i_get_list_footprint(&coal
->r
.tasks
, COALITION_TYPE_RESOURCE
, &num_tasks
);
1679 case COALITION_TYPE_JETSAM
:
1680 if (coal
->j
.leader
) {
1681 bytes
+= get_task_phys_footprint(coal
->j
.leader
);
1684 bytes
+= i_get_list_footprint(&coal
->j
.extensions
, COALITION_TYPE_JETSAM
, &num_tasks
);
1685 bytes
+= i_get_list_footprint(&coal
->j
.services
, COALITION_TYPE_JETSAM
, &num_tasks
);
1686 bytes
+= i_get_list_footprint(&coal
->j
.other
, COALITION_TYPE_JETSAM
, &num_tasks
);
1692 coalition_unlock(coal
);
1695 *ntasks
= num_tasks
;
1697 return bytes
/ PAGE_SIZE_64
;
1700 struct coal_sort_s
{
1707 * return < 0 for a < b
1711 typedef int (*cmpfunc_t
)(const void *a
, const void *b
);
1714 qsort(void *a
, size_t n
, size_t es
, cmpfunc_t cmp
);
1716 static int dflt_cmp(const void *a
, const void *b
)
1718 const struct coal_sort_s
*csA
= (const struct coal_sort_s
*)a
;
1719 const struct coal_sort_s
*csB
= (const struct coal_sort_s
*)b
;
1722 * if both A and B are equal, use a memory descending sort
1724 if (csA
->usr_order
== csB
->usr_order
)
1725 return (int)((int64_t)csB
->bytes
- (int64_t)csA
->bytes
);
1727 /* otherwise, return the relationship between user specified orders */
1728 return (csA
->usr_order
- csB
->usr_order
);
1731 static int mem_asc_cmp(const void *a
, const void *b
)
1733 const struct coal_sort_s
*csA
= (const struct coal_sort_s
*)a
;
1734 const struct coal_sort_s
*csB
= (const struct coal_sort_s
*)b
;
1736 return (int)((int64_t)csA
->bytes
- (int64_t)csB
->bytes
);
1739 static int mem_dec_cmp(const void *a
, const void *b
)
1741 const struct coal_sort_s
*csA
= (const struct coal_sort_s
*)a
;
1742 const struct coal_sort_s
*csB
= (const struct coal_sort_s
*)b
;
1744 return (int)((int64_t)csB
->bytes
- (int64_t)csA
->bytes
);
1747 static int usr_asc_cmp(const void *a
, const void *b
)
1749 const struct coal_sort_s
*csA
= (const struct coal_sort_s
*)a
;
1750 const struct coal_sort_s
*csB
= (const struct coal_sort_s
*)b
;
1752 return (csA
->usr_order
- csB
->usr_order
);
1755 static int usr_dec_cmp(const void *a
, const void *b
)
1757 const struct coal_sort_s
*csA
= (const struct coal_sort_s
*)a
;
1758 const struct coal_sort_s
*csB
= (const struct coal_sort_s
*)b
;
1760 return (csB
->usr_order
- csA
->usr_order
);
1763 /* avoid dynamic allocation in this path */
1764 #define MAX_SORTED_PIDS 80
1766 static int coalition_get_sort_list(coalition_t coal
, int sort_order
, queue_t list
,
1767 struct coal_sort_s
*sort_array
, int array_sz
)
1772 assert(sort_array
!= NULL
);
1779 * this function will only be called with a NULL
1780 * list for JETSAM-type coalitions, and is intended
1781 * to investigate the leader process
1783 if (coal
->type
!= COALITION_TYPE_JETSAM
||
1784 coal
->j
.leader
== TASK_NULL
)
1786 sort_array
[0].pid
= task_pid(coal
->j
.leader
);
1787 switch (sort_order
) {
1788 case COALITION_SORT_DEFAULT
:
1789 sort_array
[0].usr_order
= 0;
1791 case COALITION_SORT_MEM_ASC
:
1792 case COALITION_SORT_MEM_DEC
:
1793 sort_array
[0].bytes
= get_task_phys_footprint(coal
->j
.leader
);
1795 case COALITION_SORT_USER_ASC
:
1796 case COALITION_SORT_USER_DEC
:
1797 sort_array
[0].usr_order
= 0;
1805 qe_foreach_element(task
, list
, task_coalition
[coal
->type
]) {
1806 if (ntasks
>= array_sz
) {
1807 printf("WARNING: more than %d pids in coalition %llu\n",
1808 MAX_SORTED_PIDS
, coal
->id
);
1812 sort_array
[ntasks
].pid
= task_pid(task
);
1814 switch (sort_order
) {
1815 case COALITION_SORT_DEFAULT
:
1816 sort_array
[ntasks
].usr_order
= 0;
1818 case COALITION_SORT_MEM_ASC
:
1819 case COALITION_SORT_MEM_DEC
:
1820 sort_array
[ntasks
].bytes
= get_task_phys_footprint(task
);
1822 case COALITION_SORT_USER_ASC
:
1823 case COALITION_SORT_USER_DEC
:
1824 sort_array
[ntasks
].usr_order
= 0;
1836 int coalition_get_pid_list(coalition_t coal
, uint32_t rolemask
, int sort_order
,
1837 int *pid_list
, int list_sz
)
1839 struct i_jetsam_coalition
*cj
;
1841 cmpfunc_t cmp_func
= NULL
;
1842 struct coal_sort_s sort_array
[MAX_SORTED_PIDS
] = { {0,0,0} }; /* keep to < 2k */
1845 !(rolemask
& COALITION_ROLEMASK_ALLROLES
) ||
1846 !pid_list
|| list_sz
< 1) {
1847 coal_dbg("Invalid parameters: coal:%p, type:%d, rolemask:0x%x, "
1848 "pid_list:%p, list_sz:%d", coal
, coal
? coal
->type
: -1,
1849 rolemask
, pid_list
, list_sz
);
1853 switch (sort_order
) {
1854 case COALITION_SORT_NOSORT
:
1857 case COALITION_SORT_DEFAULT
:
1858 cmp_func
= dflt_cmp
;
1860 case COALITION_SORT_MEM_ASC
:
1861 cmp_func
= mem_asc_cmp
;
1863 case COALITION_SORT_MEM_DEC
:
1864 cmp_func
= mem_dec_cmp
;
1866 case COALITION_SORT_USER_ASC
:
1867 cmp_func
= usr_asc_cmp
;
1869 case COALITION_SORT_USER_DEC
:
1870 cmp_func
= usr_dec_cmp
;
1876 coalition_lock(coal
);
1878 if (coal
->type
== COALITION_TYPE_RESOURCE
) {
1879 ntasks
+= coalition_get_sort_list(coal
, sort_order
, &coal
->r
.tasks
,
1880 sort_array
, MAX_SORTED_PIDS
);
1886 if (rolemask
& COALITION_ROLEMASK_UNDEF
)
1887 ntasks
+= coalition_get_sort_list(coal
, sort_order
, &cj
->other
,
1888 sort_array
+ ntasks
,
1889 MAX_SORTED_PIDS
- ntasks
);
1891 if (rolemask
& COALITION_ROLEMASK_XPC
)
1892 ntasks
+= coalition_get_sort_list(coal
, sort_order
, &cj
->services
,
1893 sort_array
+ ntasks
,
1894 MAX_SORTED_PIDS
- ntasks
);
1896 if (rolemask
& COALITION_ROLEMASK_EXT
)
1897 ntasks
+= coalition_get_sort_list(coal
, sort_order
, &cj
->extensions
,
1898 sort_array
+ ntasks
,
1899 MAX_SORTED_PIDS
- ntasks
);
1901 if (rolemask
& COALITION_ROLEMASK_LEADER
)
1902 ntasks
+= coalition_get_sort_list(coal
, sort_order
, NULL
,
1903 sort_array
+ ntasks
,
1904 MAX_SORTED_PIDS
- ntasks
);
1907 coalition_unlock(coal
);
1909 /* sort based on the chosen criterion (no sense sorting 1 item) */
1910 if (cmp_func
&& ntasks
> 1)
1911 qsort(sort_array
, ntasks
, sizeof(struct coal_sort_s
), cmp_func
);
1913 for (int i
= 0; i
< ntasks
; i
++) {
1916 coal_dbg(" [%d] PID:%d, footprint:%lld, usr_order:%d",
1917 i
, sort_array
[i
].pid
, sort_array
[i
].bytes
,
1918 sort_array
[i
].usr_order
);
1919 pid_list
[i
] = sort_array
[i
].pid
;