forked from opensource-apple/xnu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipc_importance.c
3922 lines (3370 loc) · 116 KB
/
ipc_importance.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2013 Apple Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. The rights granted to you under the License
* may not be used to create, or enable the creation or redistribution of,
* unlawful or unlicensed copies of an Apple operating system, or to
* circumvent, violate, or enable the circumvention or violation of, any
* terms of an Apple operating system software license agreement.
*
* Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
#include <mach/mach_types.h>
#include <mach/notify.h>
#include <ipc/ipc_types.h>
#include <ipc/ipc_importance.h>
#include <ipc/ipc_port.h>
#include <ipc/ipc_voucher.h>
#include <kern/ipc_kobject.h>
#include <kern/ipc_tt.h>
#include <kern/mach_param.h>
#include <kern/misc_protos.h>
#include <kern/kalloc.h>
#include <kern/zalloc.h>
#include <kern/queue.h>
#include <kern/task.h>
#include <kern/policy_internal.h>
#include <sys/kdebug.h>
#include <mach/mach_voucher_attr_control.h>
#include <mach/machine/sdt.h>
extern int proc_pid(void *);
extern int proc_selfpid(void);
extern uint64_t proc_uniqueid(void *p);
extern char *proc_name_address(void *p);
/*
* Globals for delayed boost drop processing.
*/
static queue_head_t ipc_importance_delayed_drop_queue;
static thread_call_t ipc_importance_delayed_drop_call;
static uint64_t ipc_importance_delayed_drop_timestamp;
static boolean_t ipc_importance_delayed_drop_call_requested = FALSE;
#define DENAP_DROP_TARGET (1000 * NSEC_PER_MSEC) /* optimum denap delay */
#define DENAP_DROP_SKEW (100 * NSEC_PER_MSEC) /* request skew for wakeup */
#define DENAP_DROP_LEEWAY (2 * DENAP_DROP_SKEW) /* specified wakeup leeway */
#define DENAP_DROP_DELAY (DENAP_DROP_TARGET + DENAP_DROP_SKEW)
#define DENAP_DROP_FLAGS (THREAD_CALL_DELAY_SYS_NORMAL | THREAD_CALL_DELAY_LEEWAY)
/*
* Importance Voucher Attribute Manager
*/
static lck_spin_t ipc_importance_lock_data; /* single lock for now */
#define ipc_importance_lock_init() \
lck_spin_init(&ipc_importance_lock_data, &ipc_lck_grp, &ipc_lck_attr)
#define ipc_importance_lock_destroy() \
lck_spin_destroy(&ipc_importance_lock_data, &ipc_lck_grp)
#define ipc_importance_lock() \
lck_spin_lock(&ipc_importance_lock_data)
#define ipc_importance_lock_try() \
lck_spin_try_lock(&ipc_importance_lock_data)
#define ipc_importance_unlock() \
lck_spin_unlock(&ipc_importance_lock_data)
#define ipc_importance_assert_held() \
lck_spin_assert(&ipc_importance_lock_data, LCK_ASSERT_OWNED)
#define ipc_importance_sleep(elem) lck_spin_sleep(&ipc_importance_lock_data, \
LCK_SLEEP_DEFAULT, \
(event_t)(elem), \
THREAD_UNINT)
#define ipc_importance_wakeup(elem) thread_wakeup((event_t)(elem))
#if IIE_REF_DEBUG
#define incr_ref_counter(x) (hw_atomic_add(&(x), 1))
static inline
uint32_t ipc_importance_reference_internal(ipc_importance_elem_t elem)
{
incr_ref_counter(elem->iie_refs_added);
return (hw_atomic_add(&elem->iie_bits, 1) & IIE_REFS_MASK);
}
static inline
uint32_t ipc_importance_release_internal(ipc_importance_elem_t elem)
{
incr_ref_counter(elem->iie_refs_dropped);
return (hw_atomic_sub(&elem->iie_bits, 1) & IIE_REFS_MASK);
}
static inline
uint32_t ipc_importance_task_reference_internal(ipc_importance_task_t task_imp)
{
uint32_t out;
out = ipc_importance_reference_internal(&task_imp->iit_elem);
incr_ref_counter(task_imp->iit_elem.iie_task_refs_added);
return out;
}
static inline
uint32_t ipc_importance_task_release_internal(ipc_importance_task_t task_imp)
{
uint32_t out;
assert(1 < IIT_REFS(task_imp));
incr_ref_counter(task_imp->iit_elem.iie_task_refs_dropped);
out = ipc_importance_release_internal(&task_imp->iit_elem);
return out;
}
static inline
void ipc_importance_counter_init(ipc_importance_elem_t elem)
{
elem->iie_refs_added = 0;
elem->iie_refs_dropped = 0;
elem->iie_kmsg_refs_added = 0;
elem->iie_kmsg_refs_inherited = 0;
elem->iie_kmsg_refs_coalesced = 0;
elem->iie_kmsg_refs_dropped = 0;
elem->iie_task_refs_added = 0;
elem->iie_task_refs_added_inherit_from = 0;
elem->iie_task_refs_added_transition = 0;
elem->iie_task_refs_self_added = 0;
elem->iie_task_refs_inherited = 0;
elem->iie_task_refs_coalesced = 0;
elem->iie_task_refs_dropped = 0;
}
#else
#define incr_ref_counter(x)
#endif
#if DEVELOPMENT || DEBUG
static queue_head_t global_iit_alloc_queue;
#endif
/* TODO: remove this varibale when interactive daemon audit is complete */
boolean_t ipc_importance_interactive_receiver = FALSE;
static zone_t ipc_importance_task_zone;
static zone_t ipc_importance_inherit_zone;
static ipc_voucher_attr_control_t ipc_importance_control;
static boolean_t ipc_importance_task_check_transition(ipc_importance_task_t task_imp,
iit_update_type_t type, uint32_t delta);
static void ipc_importance_task_propagate_assertion_locked(ipc_importance_task_t task_imp,
iit_update_type_t type, boolean_t update_task_imp);
static ipc_importance_inherit_t ipc_importance_inherit_from_task(task_t from_task, task_t to_task);
/*
* Routine: ipc_importance_kmsg_link
* Purpose:
* Link the kmsg onto the appropriate propagation chain.
* If the element is a task importance, we link directly
* on its propagation chain. Otherwise, we link onto the
* destination task of the inherit.
* Conditions:
* Importance lock held.
* Caller is donating an importance elem reference to the kmsg.
*/
static void
ipc_importance_kmsg_link(
ipc_kmsg_t kmsg,
ipc_importance_elem_t elem)
{
ipc_importance_elem_t link_elem;
assert(IIE_NULL == kmsg->ikm_importance);
link_elem = (IIE_TYPE_INHERIT == IIE_TYPE(elem)) ?
(ipc_importance_elem_t)((ipc_importance_inherit_t)elem)->iii_to_task :
elem;
queue_enter(&link_elem->iie_kmsgs, kmsg, ipc_kmsg_t, ikm_inheritance);
kmsg->ikm_importance = elem;
}
/*
* Routine: ipc_importance_kmsg_unlink
* Purpose:
* Unlink the kmsg from its current propagation chain.
* If the element is a task importance, we unlink directly
* from its propagation chain. Otherwise, we unlink from the
* destination task of the inherit.
* Returns:
* The reference to the importance element it was linked on.
* Conditions:
* Importance lock held.
* Caller is responsible for dropping reference on returned elem.
*/
static ipc_importance_elem_t
ipc_importance_kmsg_unlink(
ipc_kmsg_t kmsg)
{
ipc_importance_elem_t elem = kmsg->ikm_importance;
if (IIE_NULL != elem) {
ipc_importance_elem_t unlink_elem;
unlink_elem = (IIE_TYPE_INHERIT == IIE_TYPE(elem)) ?
(ipc_importance_elem_t)((ipc_importance_inherit_t)elem)->iii_to_task :
elem;
queue_remove(&unlink_elem->iie_kmsgs, kmsg, ipc_kmsg_t, ikm_inheritance);
kmsg->ikm_importance = IIE_NULL;
}
return elem;
}
/*
* Routine: ipc_importance_inherit_link
* Purpose:
* Link the inherit onto the appropriate propagation chain.
* If the element is a task importance, we link directly
* on its propagation chain. Otherwise, we link onto the
* destination task of the inherit.
* Conditions:
* Importance lock held.
* Caller is donating an elem importance reference to the inherit.
*/
static void
ipc_importance_inherit_link(
ipc_importance_inherit_t inherit,
ipc_importance_elem_t elem)
{
ipc_importance_task_t link_task;
assert(IIE_NULL == inherit->iii_from_elem);
link_task = (IIE_TYPE_INHERIT == IIE_TYPE(elem)) ?
((ipc_importance_inherit_t)elem)->iii_to_task :
(ipc_importance_task_t)elem;
queue_enter(&link_task->iit_inherits, inherit,
ipc_importance_inherit_t, iii_inheritance);
inherit->iii_from_elem = elem;
}
/*
* Routine: ipc_importance_inherit_find
* Purpose:
* Find an existing inherit that links the from element to the
* to_task at a given nesting depth. As inherits from other
* inherits are actually linked off the original inherit's donation
* receiving task, we have to conduct our search from there if
* the from element is an inherit.
* Returns:
* A pointer (not a reference) to the matching inherit.
* Conditions:
* Importance lock held.
*/
static ipc_importance_inherit_t
ipc_importance_inherit_find(
ipc_importance_elem_t from,
ipc_importance_task_t to_task,
unsigned int depth)
{
ipc_importance_task_t link_task;
ipc_importance_inherit_t inherit;
link_task = (IIE_TYPE_INHERIT == IIE_TYPE(from)) ?
((ipc_importance_inherit_t)from)->iii_to_task :
(ipc_importance_task_t)from;
queue_iterate(&link_task->iit_inherits, inherit,
ipc_importance_inherit_t, iii_inheritance) {
if (inherit->iii_to_task == to_task && inherit->iii_depth == depth) {
return inherit;
}
}
return III_NULL;
}
/*
* Routine: ipc_importance_inherit_unlink
* Purpose:
* Unlink the inherit from its current propagation chain.
* If the element is a task importance, we unlink directly
* from its propagation chain. Otherwise, we unlink from the
* destination task of the inherit.
* Returns:
* The reference to the importance element it was linked on.
* Conditions:
* Importance lock held.
* Caller is responsible for dropping reference on returned elem.
*/
static ipc_importance_elem_t
ipc_importance_inherit_unlink(
ipc_importance_inherit_t inherit)
{
ipc_importance_elem_t elem = inherit->iii_from_elem;
if (IIE_NULL != elem) {
ipc_importance_task_t unlink_task;
unlink_task = (IIE_TYPE_INHERIT == IIE_TYPE(elem)) ?
((ipc_importance_inherit_t)elem)->iii_to_task :
(ipc_importance_task_t)elem;
queue_remove(&unlink_task->iit_inherits, inherit,
ipc_importance_inherit_t, iii_inheritance);
inherit->iii_from_elem = IIE_NULL;
}
return elem;
}
/*
* Routine: ipc_importance_reference
* Purpose:
* Add a reference to the importance element.
* Conditions:
* Caller must hold a reference on the element.
*/
void
ipc_importance_reference(ipc_importance_elem_t elem)
{
assert(0 < IIE_REFS(elem));
ipc_importance_reference_internal(elem);
}
/*
* Routine: ipc_importance_release_locked
* Purpose:
* Release a reference on an importance attribute value,
* unlinking and deallocating the attribute if the last reference.
* Conditions:
* Entered with importance lock held, leaves with it unlocked.
*/
static void
ipc_importance_release_locked(ipc_importance_elem_t elem)
{
assert(0 < IIE_REFS(elem));
#if IMPORTANCE_DEBUG
ipc_importance_inherit_t temp_inherit;
ipc_importance_task_t link_task;
ipc_kmsg_t temp_kmsg;
uint32_t expected = 0;
if (0 < elem->iie_made)
expected++;
link_task = (IIE_TYPE_INHERIT == IIE_TYPE(elem)) ?
((ipc_importance_inherit_t)elem)->iii_to_task :
(ipc_importance_task_t)elem;
queue_iterate(&link_task->iit_kmsgs, temp_kmsg, ipc_kmsg_t, ikm_inheritance)
if (temp_kmsg->ikm_importance == elem)
expected++;
queue_iterate(&link_task->iit_inherits, temp_inherit,
ipc_importance_inherit_t, iii_inheritance)
if (temp_inherit->iii_from_elem == elem)
expected++;
if (IIE_REFS(elem) < expected + 1)
panic("ipc_importance_release_locked (%p)", elem);
#endif /* IMPORTANCE_DEBUG */
if (0 < ipc_importance_release_internal(elem)) {
ipc_importance_unlock();
return;
}
/* last ref */
switch (IIE_TYPE(elem)) {
/* just a "from" task reference to drop */
case IIE_TYPE_TASK:
{
ipc_importance_task_t task_elem;
task_elem = (ipc_importance_task_t)elem;
/* the task can't still hold a reference on the task importance */
assert(TASK_NULL == task_elem->iit_task);
#if DEVELOPMENT || DEBUG
queue_remove(&global_iit_alloc_queue, task_elem, ipc_importance_task_t, iit_allocation);
#endif
ipc_importance_unlock();
zfree(ipc_importance_task_zone, task_elem);
break;
}
/* dropping an inherit element */
case IIE_TYPE_INHERIT:
{
ipc_importance_inherit_t inherit = (ipc_importance_inherit_t)elem;
ipc_importance_task_t to_task = inherit->iii_to_task;
ipc_importance_elem_t from_elem;
assert(IIT_NULL != to_task);
assert(ipc_importance_task_is_any_receiver_type(to_task));
/* unlink the inherit from its source element */
from_elem = ipc_importance_inherit_unlink(inherit);
assert(IIE_NULL != from_elem);
/*
* The attribute might have pending external boosts if the attribute
* was given out during exec, drop them from the appropriate destination
* task.
*
* The attribute will not have any pending external boosts if the
* attribute was given out to voucher system since it would have been
* dropped by ipc_importance_release_value, but there is not way to
* detect that, thus if the attribute has a pending external boost,
* drop them from the appropriate destination task.
*
* The inherit attribute from exec and voucher system would not
* get deduped to each other, thus dropping the external boost
* from destination task at two different places will not have
* any unintended side effects.
*/
assert(inherit->iii_externcnt >= inherit->iii_externdrop);
if (inherit->iii_donating) {
uint32_t assertcnt = III_EXTERN(inherit);
assert(ipc_importance_task_is_any_receiver_type(to_task));
assert(to_task->iit_externcnt >= inherit->iii_externcnt);
assert(to_task->iit_externdrop >= inherit->iii_externdrop);
to_task->iit_externcnt -= inherit->iii_externcnt;
to_task->iit_externdrop -= inherit->iii_externdrop;
inherit->iii_externcnt = 0;
inherit->iii_externdrop = 0;
inherit->iii_donating = FALSE;
/* adjust the internal assertions - and propagate as needed */
if (ipc_importance_task_check_transition(to_task, IIT_UPDATE_DROP, assertcnt)) {
ipc_importance_task_propagate_assertion_locked(to_task, IIT_UPDATE_DROP, TRUE);
}
} else {
inherit->iii_externcnt = 0;
inherit->iii_externdrop = 0;
}
/* release the reference on the source element */
ipc_importance_release_locked(from_elem);
/* unlocked on return */
/* release the reference on the destination task */
ipc_importance_task_release(to_task);
/* free the inherit */
zfree(ipc_importance_inherit_zone, inherit);
break;
}
}
}
/*
* Routine: ipc_importance_release
* Purpose:
* Release a reference on an importance attribute value,
* unlinking and deallocating the attribute if the last reference.
* Conditions:
* nothing locked on entrance, nothing locked on exit.
* May block.
*/
void
ipc_importance_release(ipc_importance_elem_t elem)
{
if (IIE_NULL == elem)
return;
ipc_importance_lock();
ipc_importance_release_locked(elem);
/* unlocked */
}
/*
* Routine: ipc_importance_task_reference
* Purpose:
* Retain a reference on a task importance attribute value.
* Conditions:
* nothing locked on entrance, nothing locked on exit.
* caller holds a reference already.
*/
void
ipc_importance_task_reference(ipc_importance_task_t task_elem)
{
if (IIT_NULL == task_elem)
return;
#if IIE_REF_DEBUG
incr_ref_counter(task_elem->iit_elem.iie_task_refs_added);
#endif
ipc_importance_reference(&task_elem->iit_elem);
}
/*
* Routine: ipc_importance_task_release
* Purpose:
* Release a reference on a task importance attribute value,
* unlinking and deallocating the attribute if the last reference.
* Conditions:
* nothing locked on entrance, nothing locked on exit.
* May block.
*/
void
ipc_importance_task_release(ipc_importance_task_t task_elem)
{
if (IIT_NULL == task_elem)
return;
ipc_importance_lock();
#if IIE_REF_DEBUG
incr_ref_counter(task_elem->iit_elem.iie_task_refs_dropped);
#endif
ipc_importance_release_locked(&task_elem->iit_elem);
/* unlocked */
}
/*
* Routine: ipc_importance_task_release_locked
* Purpose:
* Release a reference on a task importance attribute value,
* unlinking and deallocating the attribute if the last reference.
* Conditions:
* importance lock held on entry, nothing locked on exit.
* May block.
*/
static void
ipc_importance_task_release_locked(ipc_importance_task_t task_elem)
{
if (IIT_NULL == task_elem) {
ipc_importance_unlock();
return;
}
#if IIE_REF_DEBUG
incr_ref_counter(task_elem->iit_elem.iie_task_refs_dropped);
#endif
ipc_importance_release_locked(&task_elem->iit_elem);
/* unlocked */
}
/*
* Routines for importance donation/inheritance/boosting
*/
/*
* External importance assertions are managed by the process in userspace
* Internal importance assertions are the responsibility of the kernel
* Assertions are changed from internal to external via task_importance_externalize_assertion
*/
/*
* Routine: ipc_importance_task_check_transition
* Purpose:
* Increase or decrement the internal task importance counter of the
* specified task and determine if propagation and a task policy
* update is required.
*
* If it is already enqueued for a policy update, steal it from that queue
* (as we are reversing that update before it happens).
*
* Conditions:
* Called with the importance lock held.
* It is the caller's responsibility to perform the propagation of the
* transition and/or policy changes by checking the return value.
*/
static boolean_t
ipc_importance_task_check_transition(
ipc_importance_task_t task_imp,
iit_update_type_t type,
uint32_t delta)
{
#if IMPORTANCE_TRACE
task_t target_task = task_imp->iit_task;
#endif
boolean_t boost = (IIT_UPDATE_HOLD == type);
boolean_t before_boosted, after_boosted;
ipc_importance_assert_held();
if (!ipc_importance_task_is_any_receiver_type(task_imp))
return FALSE;
#if IMPORTANCE_TRACE
int target_pid = task_pid(target_task);
KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, (IMPORTANCE_CODE(IMP_ASSERTION, (((boost) ? IMP_HOLD : IMP_DROP) | TASK_POLICY_INTERNAL))) | DBG_FUNC_START,
proc_selfpid(), target_pid, task_imp->iit_assertcnt, IIT_EXTERN(task_imp), 0);
#endif
/* snapshot the effective boosting status before making any changes */
before_boosted = (task_imp->iit_assertcnt > 0);
/* Adjust the assertcnt appropriately */
if (boost) {
task_imp->iit_assertcnt += delta;
#if IMPORTANCE_TRACE
DTRACE_BOOST6(send_boost, task_t, target_task, int, target_pid,
task_t, current_task(), int, proc_selfpid(), int, delta, int, task_imp->iit_assertcnt);
#endif
} else {
// assert(delta <= task_imp->iit_assertcnt);
if (task_imp->iit_assertcnt < delta + IIT_EXTERN(task_imp)) {
/* TODO: Turn this back into a panic <rdar://problem/12592649> */
task_imp->iit_assertcnt = IIT_EXTERN(task_imp);
} else {
task_imp->iit_assertcnt -= delta;
}
#if IMPORTANCE_TRACE
// This convers both legacy and voucher-based importance.
DTRACE_BOOST4(drop_boost, task_t, target_task, int, target_pid, int, delta, int, task_imp->iit_assertcnt);
#endif
}
#if IMPORTANCE_TRACE
KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, (IMPORTANCE_CODE(IMP_ASSERTION, (((boost) ? IMP_HOLD : IMP_DROP) | TASK_POLICY_INTERNAL))) | DBG_FUNC_END,
proc_selfpid(), target_pid, task_imp->iit_assertcnt, IIT_EXTERN(task_imp), 0);
#endif
/* did the change result in an effective donor status change? */
after_boosted = (task_imp->iit_assertcnt > 0);
if (after_boosted != before_boosted) {
/*
* If the task importance is already on an update queue, we just reversed the need for a
* pending policy update. If the queue is any other than the delayed-drop-queue, pull it
* off that queue and release the reference it got going onto the update queue. If it is
* the delayed-drop-queue we leave it in place in case it comes back into the drop state
* before its time delay is up.
*
* We still need to propagate the change downstream to reverse the assertcnt effects,
* but we no longer need to update this task's boost policy state.
*
* Otherwise, mark it as needing a policy update.
*/
assert(0 == task_imp->iit_updatepolicy);
if (NULL != task_imp->iit_updateq) {
if (&ipc_importance_delayed_drop_queue != task_imp->iit_updateq) {
queue_remove(task_imp->iit_updateq, task_imp, ipc_importance_task_t, iit_updates);
task_imp->iit_updateq = NULL;
ipc_importance_task_release_internal(task_imp); /* can't be last ref */
}
} else {
task_imp->iit_updatepolicy = 1;
}
return TRUE;
}
return FALSE;
}
/*
* Routine: ipc_importance_task_propagate_helper
* Purpose:
* Increase or decrement the internal task importance counter of all
* importance tasks inheriting from the specified one. If this causes
* that importance task to change state, add it to the list of tasks
* to do a policy update against.
* Conditions:
* Called with the importance lock held.
* It is the caller's responsibility to iterate down the generated list
* and propagate any subsequent assertion changes from there.
*/
static void
ipc_importance_task_propagate_helper(
ipc_importance_task_t task_imp,
iit_update_type_t type,
queue_t propagation)
{
ipc_importance_task_t temp_task_imp;
/*
* iterate the downstream kmsgs, adjust their boosts,
* and capture the next task to adjust for each message
*/
ipc_kmsg_t temp_kmsg;
queue_iterate(&task_imp->iit_kmsgs, temp_kmsg, ipc_kmsg_t, ikm_inheritance) {
mach_msg_header_t *hdr = temp_kmsg->ikm_header;
mach_port_delta_t delta;
ipc_port_t port;
/* toggle the kmsg importance bit as a barrier to parallel adjusts */
if (IIT_UPDATE_HOLD == type) {
if (MACH_MSGH_BITS_RAISED_IMPORTANCE(hdr->msgh_bits)) {
continue;
}
/* mark the message as now carrying importance */
hdr->msgh_bits |= MACH_MSGH_BITS_RAISEIMP;
delta = 1;
} else {
if (!MACH_MSGH_BITS_RAISED_IMPORTANCE(hdr->msgh_bits)) {
continue;
}
/* clear the message as now carrying importance */
hdr->msgh_bits &= ~MACH_MSGH_BITS_RAISEIMP;
delta = -1;
}
/* determine the task importance to adjust as result (if any) */
port = (ipc_port_t) hdr->msgh_remote_port;
assert(IP_VALID(port));
ip_lock(port);
temp_task_imp = IIT_NULL;
if (!ipc_port_importance_delta_internal(port, IPID_OPTION_NORMAL, &delta, &temp_task_imp)) {
ip_unlock(port);
}
/* no task importance to adjust associated with the port? */
if (IIT_NULL == temp_task_imp) {
continue;
}
/* hold a reference on temp_task_imp */
/* Adjust the task assertions and determine if an edge was crossed */
if (ipc_importance_task_check_transition(temp_task_imp, type, 1)) {
incr_ref_counter(temp_task_imp->iit_elem.iie_task_refs_added_transition);
queue_enter(propagation, temp_task_imp, ipc_importance_task_t, iit_props);
/* reference donated */
} else {
ipc_importance_task_release_internal(temp_task_imp);
}
}
/*
* iterate the downstream importance inherits
* and capture the next task importance to boost for each
*/
ipc_importance_inherit_t temp_inherit;
queue_iterate(&task_imp->iit_inherits, temp_inherit, ipc_importance_inherit_t, iii_inheritance) {
uint32_t assertcnt = III_EXTERN(temp_inherit);
temp_task_imp = temp_inherit->iii_to_task;
assert(IIT_NULL != temp_task_imp);
if (IIT_UPDATE_HOLD == type) {
/* if no undropped externcnts in the inherit, nothing to do */
if (0 == assertcnt) {
assert(temp_inherit->iii_donating == FALSE);
continue;
}
/* nothing to do if the inherit is already donating (forced donation) */
if (temp_inherit->iii_donating) {
continue;
}
/* mark it donating and contribute to the task externcnts */
temp_inherit->iii_donating = TRUE;
temp_task_imp->iit_externcnt += temp_inherit->iii_externcnt;
temp_task_imp->iit_externdrop += temp_inherit->iii_externdrop;
} else {
/* if no contributing assertions, move on */
if (0 == assertcnt) {
assert(temp_inherit->iii_donating == FALSE);
continue;
}
/* nothing to do if the inherit is not donating */
if (!temp_inherit->iii_donating) {
continue;
}
/* mark it no longer donating */
temp_inherit->iii_donating = FALSE;
/* remove the contribution the inherit made to the to-task */
assert(IIT_EXTERN(temp_task_imp) >= III_EXTERN(temp_inherit));
assert(temp_task_imp->iit_externcnt >= temp_inherit->iii_externcnt);
assert(temp_task_imp->iit_externdrop >= temp_inherit->iii_externdrop);
temp_task_imp->iit_externcnt -= temp_inherit->iii_externcnt;
temp_task_imp->iit_externdrop -= temp_inherit->iii_externdrop;
}
/* Adjust the task assertions and determine if an edge was crossed */
assert(ipc_importance_task_is_any_receiver_type(temp_task_imp));
if (ipc_importance_task_check_transition(temp_task_imp, type, assertcnt)) {
ipc_importance_task_reference(temp_task_imp);
incr_ref_counter(temp_task_imp->iit_elem.iie_task_refs_added_transition);
queue_enter(propagation, temp_task_imp, ipc_importance_task_t, iit_props);
}
}
}
/*
* Routine: ipc_importance_task_process_updates
* Purpose:
* Process the queue of task importances and apply the policy
* update called for. Only process tasks in the queue with an
* update timestamp less than the supplied max.
* Conditions:
* Called and returns with importance locked.
* May drop importance lock and block temporarily.
*/
static void
ipc_importance_task_process_updates(
queue_t supplied_queue,
boolean_t boost,
uint64_t max_timestamp)
{
ipc_importance_task_t task_imp;
queue_head_t second_chance;
queue_t queue = supplied_queue;
/*
* This queue will hold the task's we couldn't trylock on first pass.
* By using a second (private) queue, we guarantee all tasks that get
* entered on this queue have a timestamp under the maximum.
*/
queue_init(&second_chance);
/* process any resulting policy updates */
retry:
while(!queue_empty(queue)) {
task_t target_task;
struct task_pend_token pend_token = {};
task_imp = (ipc_importance_task_t)queue_first(queue);
assert(0 == task_imp->iit_updatepolicy);
assert(queue == task_imp->iit_updateq);
/* if timestamp is too big, we're done */
if (task_imp->iit_updatetime > max_timestamp) {
break;
}
/* we were given a reference on each task in the queue */
/* remove it from the supplied queue */
queue_remove(queue, task_imp, ipc_importance_task_t, iit_updates);
task_imp->iit_updateq = NULL;
target_task = task_imp->iit_task;
/* Is it well on the way to exiting? */
if (TASK_NULL == target_task) {
ipc_importance_task_release_locked(task_imp);
/* importance unlocked */
ipc_importance_lock();
continue;
}
/* Has the update been reversed on the hysteresis queue? */
if (0 < task_imp->iit_assertcnt &&
queue == &ipc_importance_delayed_drop_queue) {
ipc_importance_task_release_locked(task_imp);
/* importance unlocked */
ipc_importance_lock();
continue;
}
/*
* Can we get the task lock out-of-order?
* If not, stick this back on the second-chance queue.
*/
if (!task_lock_try(target_task)) {
boolean_t should_wait_lock = (queue == &second_chance);
task_imp->iit_updateq = &second_chance;
/*
* If we're already processing second-chances on
* tasks, keep this task on the front of the queue.
* We will wait for the task lock before coming
* back and trying again, and we have a better
* chance of re-acquiring the lock if we come back
* to it right away.
*/
if (should_wait_lock){
task_reference(target_task);
queue_enter_first(&second_chance, task_imp,
ipc_importance_task_t, iit_updates);
} else {
queue_enter(&second_chance, task_imp,
ipc_importance_task_t, iit_updates);
}
ipc_importance_unlock();
if (should_wait_lock) {
task_lock(target_task);
task_unlock(target_task);
task_deallocate(target_task);
}
ipc_importance_lock();
continue;
}
/* is it going away? */
if (!target_task->active) {
task_unlock(target_task);
ipc_importance_task_release_locked(task_imp);
/* importance unlocked */
ipc_importance_lock();
continue;
}
/* take a task reference for while we don't have the importance lock */
task_reference(target_task);
/* count the transition */
if (boost)
task_imp->iit_transitions++;
ipc_importance_unlock();
/* apply the policy adjust to the target task (while it is still locked) */
task_update_boost_locked(target_task, boost, &pend_token);
/* complete the policy update with the task unlocked */
ipc_importance_task_release(task_imp);
task_unlock(target_task);
task_policy_update_complete_unlocked(target_task, &pend_token);
task_deallocate(target_task);
ipc_importance_lock();
}
/* If there are tasks we couldn't update the first time, try again */
if (!queue_empty(&second_chance)) {
queue = &second_chance;
goto retry;
}
}
/*
* Routine: ipc_importance_task_delayed_drop_scan
* Purpose:
* The thread call routine to scan the delayed drop queue,
* requesting all updates with a deadline up to the last target
* for the thread-call (which is DENAP_DROP_SKEW beyond the first
* thread's optimum delay).
* update to drop its boost.
* Conditions:
* Nothing locked
*/
static void
ipc_importance_task_delayed_drop_scan(
__unused void *arg1,
__unused void *arg2)
{
ipc_importance_lock();
/* process all queued task drops with timestamps up to TARGET(first)+SKEW */
ipc_importance_task_process_updates(&ipc_importance_delayed_drop_queue,
FALSE,
ipc_importance_delayed_drop_timestamp);
/* importance lock may have been temporarily dropped */
/* If there are any entries left in the queue, re-arm the call here */
if (!queue_empty(&ipc_importance_delayed_drop_queue)) {
ipc_importance_task_t task_imp;
uint64_t deadline;
uint64_t leeway;
task_imp = (ipc_importance_task_t)queue_first(&ipc_importance_delayed_drop_queue);
nanoseconds_to_absolutetime(DENAP_DROP_DELAY, &deadline);
deadline += task_imp->iit_updatetime;
ipc_importance_delayed_drop_timestamp = deadline;
nanoseconds_to_absolutetime(DENAP_DROP_LEEWAY, &leeway);
thread_call_enter_delayed_with_leeway(
ipc_importance_delayed_drop_call,
NULL,
deadline,
leeway,