1*19c3b8c2SApple OSS DistributionsTask References 2*19c3b8c2SApple OSS Distributions=============== 3*19c3b8c2SApple OSS Distributions 4*19c3b8c2SApple OSS DistributionsBackground 5*19c3b8c2SApple OSS Distributions---------- 6*19c3b8c2SApple OSS Distributions 7*19c3b8c2SApple OSS DistributionsTasks in XNU are reference counted. When a task is created it starts with two 8*19c3b8c2SApple OSS Distributionsreferences - one for the caller and one for the task itself. Over the lifetime 9*19c3b8c2SApple OSS Distributionsof the task this reference count is modified, for example when a thread is 10*19c3b8c2SApple OSS Distributionscreated it increments the reference count and when it exits that count drops. 11*19c3b8c2SApple OSS DistributionsWhen a reference count reaches zero, the task is freed. 12*19c3b8c2SApple OSS Distributions 13*19c3b8c2SApple OSS DistributionsTo grab a reference: 14*19c3b8c2SApple OSS Distributions```c 15*19c3b8c2SApple OSS Distributionstask_reference() 16*19c3b8c2SApple OSS Distributions``` 17*19c3b8c2SApple OSS Distributions 18*19c3b8c2SApple OSS DistributionsTo release a reference: 19*19c3b8c2SApple OSS Distributions```c 20*19c3b8c2SApple OSS Distributionstask_deallocate() 21*19c3b8c2SApple OSS Distributions``` 22*19c3b8c2SApple OSS Distributions 23*19c3b8c2SApple OSS DistributionsOne of the big problems seen with task references is that difficult to debug 24*19c3b8c2SApple OSS Distributions_leaks_ commonly occur. This happens when a reference is taken but never 25*19c3b8c2SApple OSS Distributionsreleased. The task is kept around indefinitely and eventually the system runs 26*19c3b8c2SApple OSS Distributionsout of a finite resource (for example ASIDs). At this point there is very little 27*19c3b8c2SApple OSS Distributionsinformation to determine what code was responsible for the leak. 28*19c3b8c2SApple OSS Distributions 29*19c3b8c2SApple OSS Distributions 30*19c3b8c2SApple OSS DistributionsTask Reference Groups 31*19c3b8c2SApple OSS Distributions-------------------- 32*19c3b8c2SApple OSS Distributions 33*19c3b8c2SApple OSS DistributionsReference groups are a feature which keep track of statistics (and when 34*19c3b8c2SApple OSS Distributionsconfigured backtrace information) for a set of references. Reference groups are 35*19c3b8c2SApple OSS Distributionshierarchical. To help with debugging the following task reference group 36*19c3b8c2SApple OSS Distributionshierarchy is used: 37*19c3b8c2SApple OSS Distributions 38*19c3b8c2SApple OSS Distributions``` 39*19c3b8c2SApple OSS Distributionstask 40*19c3b8c2SApple OSS Distributions -> task_internal 41*19c3b8c2SApple OSS Distributions -> task_local_internal 42*19c3b8c2SApple OSS Distributions -> task_kernel 43*19c3b8c2SApple OSS Distributions -> task_local_internal 44*19c3b8c2SApple OSS Distributions -> task_mig 45*19c3b8c2SApple OSS Distributions -> task_local_internal 46*19c3b8c2SApple OSS Distributions -> task_external 47*19c3b8c2SApple OSS Distributions -> task_local_external 48*19c3b8c2SApple OSS Distributions -> task_com.apple.security.sandbox 49*19c3b8c2SApple OSS Distributions -> task_com.apple.security.sandbox 50*19c3b8c2SApple OSS Distributions -> task_com.apple.driver.AppleHV 51*19c3b8c2SApple OSS Distributions -> task_com.apple.driver.AppleHV 52*19c3b8c2SApple OSS Distributions ... 53*19c3b8c2SApple OSS Distributions``` 54*19c3b8c2SApple OSS Distributions 55*19c3b8c2SApple OSS DistributionsThe `task` group contains a count of all task references in the system. The 56*19c3b8c2SApple OSS Distributionsfirst-level groups are static and sub-divide task references based on the 57*19c3b8c2SApple OSS Distributionssub-system they come from. `task_external` is used for kext references and each 58*19c3b8c2SApple OSS Distributionskext will be dynamically assigned a reference group as needed (if there's 59*19c3b8c2SApple OSS Distributionsone available). At the bottom level, there's a per-task (local) ref group under 60*19c3b8c2SApple OSS Distributionseach global group. 61*19c3b8c2SApple OSS DistributionsThe exact hierarchy of task references (specifically what per-task reference 62*19c3b8c2SApple OSS Distributionsgroups are created) changes depending on the 'task_refgrp' boot arg. 63*19c3b8c2SApple OSS Distributions 64*19c3b8c2SApple OSS DistributionsTask reference groups can be explored in `lldb` as follows: 65*19c3b8c2SApple OSS Distributions 66*19c3b8c2SApple OSS Distributions``` 67*19c3b8c2SApple OSS Distributions(lldb) showglobaltaskrefgrps 68*19c3b8c2SApple OSS Distributionsos_refgrp name count retain release log 69*19c3b8c2SApple OSS Distributions0xffffff801ace9250 task_kernel 68 367663 367595 0x0 70*19c3b8c2SApple OSS Distributions0xffffff801ace9288 task_internal 974 4953 3979 0x0 71*19c3b8c2SApple OSS Distributions0xffffff801ace92c0 task_mig 0 3670 3670 0x0 72*19c3b8c2SApple OSS Distributions0xffffff801ace9218 task_external 35 108 73 0x0 73*19c3b8c2SApple OSS Distributions0xffffff9369dc7b20 task_com.apple.iokit.IOAcceleratorFamily2 29 77 48 0x0 74*19c3b8c2SApple OSS Distributions0xffffff936a3f0a20 task_com.apple.iokit.CoreAnalyticsFamily 1 1 0 0x0 75*19c3b8c2SApple OSS Distributions0xffffff936a22cb20 task_com.apple.iokit.EndpointSecurity 0 1 1 0x0 76*19c3b8c2SApple OSS Distributions0xffffff936a283f60 task_com.apple.iokit.IOSurface 5 5 0 0x0 77*19c3b8c2SApple OSS Distributions0xffffff936a3f08a0 task_com.apple.security.sandbox 0 24 24 0x0 78*19c3b8c2SApple OSS Distributions 79*19c3b8c2SApple OSS Distributions``` 80*19c3b8c2SApple OSS Distributions 81*19c3b8c2SApple OSS DistributionsDisplay a task's reference groups: 82*19c3b8c2SApple OSS Distributions 83*19c3b8c2SApple OSS Distributions``` 84*19c3b8c2SApple OSS Distributions(lldb) showtaskrefgrps kernel_task 85*19c3b8c2SApple OSS Distributionsos_refgrp name count retain release log 86*19c3b8c2SApple OSS Distributions0xffffff936a4b9200 task_local_kernel 1 6 5 0x0 87*19c3b8c2SApple OSS Distributions0xffffff936a4b9238 task_local_internal 132 619 487 0x0 88*19c3b8c2SApple OSS Distributions``` 89*19c3b8c2SApple OSS Distributions 90*19c3b8c2SApple OSS DistributionsThe reference group hierarchy for a specific group can be displayed as follows: 91*19c3b8c2SApple OSS Distributions 92*19c3b8c2SApple OSS Distributions``` 93*19c3b8c2SApple OSS Distributions(lldb) showosrefgrphierarchy 0xffffff936a3f08a0 94*19c3b8c2SApple OSS Distributions0xffffff801ace9988 all 1121 377740 376619 0x0 95*19c3b8c2SApple OSS Distributions0xffffff801ace91e0 task 1077 376394 375317 0x0 96*19c3b8c2SApple OSS Distributions0xffffff801ace9218 task_external 35 108 73 0x0 97*19c3b8c2SApple OSS Distributions0xffffff936a3f08a0 task_com.apple.security.sandbox 0 24 24 0x0 98*19c3b8c2SApple OSS Distributions``` 99*19c3b8c2SApple OSS Distributions 100*19c3b8c2SApple OSS DistributionsReference groups are normally disabled, but task reference group statistics 101*19c3b8c2SApple OSS Distributions*are* enabled by default (for `RELEASE` builds, reference groups are not available 102*19c3b8c2SApple OSS Distributionsat all). Backtrace logging for all groups is disabled, including task reference 103*19c3b8c2SApple OSS Distributionsgroups. To enable backtrace logging and reference group statistics, the `rlog` 104*19c3b8c2SApple OSS Distributionsboot-arg must be used. Backtrace logging for task reference groups is only 105*19c3b8c2SApple OSS Distributionsenabled when `rlog` has been set to a suitable value. 106*19c3b8c2SApple OSS Distributions 107*19c3b8c2SApple OSS DistributionsFor example 108*19c3b8c2SApple OSS Distributions 109*19c3b8c2SApple OSS DistributionsTo enable statistics for all reference groups and backtrace logging for the 110*19c3b8c2SApple OSS Distributions*task_external* reference group in particular: 111*19c3b8c2SApple OSS Distributions 112*19c3b8c2SApple OSS Distributions``` 113*19c3b8c2SApple OSS Distributionsnvram boot-args="rlog=task_external ..." 114*19c3b8c2SApple OSS Distributions``` 115*19c3b8c2SApple OSS Distributions 116*19c3b8c2SApple OSS Distributions``` 117*19c3b8c2SApple OSS Distributions(lldb) showglobaltaskrefgrps 118*19c3b8c2SApple OSS Distributionsos_refgrp name count retain release log 119*19c3b8c2SApple OSS Distributions0xffffff801e0e9250 task_kernel 1259 132739 131480 0x0 120*19c3b8c2SApple OSS Distributions0xffffff801e0e9218 task_external 35 100 65 0xffffffa05b3fc000 121*19c3b8c2SApple OSS Distributions0xffffff936d117be0 task_com.apple.iokit.IOAcceleratorFamily2 29 77 48 0x0 122*19c3b8c2SApple OSS Distributions0xffffff936db9fa20 task_com.apple.iokit.CoreAnalyticsFamily 1 1 0 0x0 123*19c3b8c2SApple OSS Distributions0xffffff936d9dbb20 task_com.apple.iokit.EndpointSecurity 0 1 1 0x0 124*19c3b8c2SApple OSS Distributions0xffffff936da324e0 task_com.apple.iokit.IOSurface 5 5 0 0x0 125*19c3b8c2SApple OSS Distributions0xffffff936db9f8a0 task_com.apple.security.sandbox 0 16 16 0x0 126*19c3b8c2SApple OSS Distributions 127*19c3b8c2SApple OSS Distributions 128*19c3b8c2SApple OSS Distributions(lldb) showbtlogrecords 0xffffffa05b3fc000 129*19c3b8c2SApple OSS Distributions-------- OP 1 Stack Index 0 with active refs 1 of 165 -------- 130*19c3b8c2SApple OSS Distributions0xffffff801da7c1cb <kernel.development`ref_log_op at refcnt.c:107> 131*19c3b8c2SApple OSS Distributions0xffffff801d27c35d <kernel.development`task_reference_grp at task_ref.c:274> 132*19c3b8c2SApple OSS Distributions0xffffff801ecc014e <EndpointSecurity`VMMap::taskSelf()> 133*19c3b8c2SApple OSS Distributions0xffffff801eccc845 <EndpointSecurity`EndpointSecurityClient::create(ScopedPointer<MachSendWrapper> const&, proc*, ScopedPointer<EndpointSecurityExternalClient> const&, es_client_config_t const&)> 134*19c3b8c2SApple OSS Distributions... 135*19c3b8c2SApple OSS Distributions``` 136