binder.c 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742
  1. /* binder.c
  2. *
  3. * Android IPC Subsystem
  4. *
  5. * Copyright (C) 2007-2008 Google, Inc.
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <asm/cacheflush.h>
  19. #include <linux/fdtable.h>
  20. #include <linux/file.h>
  21. #include <linux/freezer.h>
  22. #include <linux/fs.h>
  23. #include <linux/list.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/mm.h>
  26. #include <linux/module.h>
  27. #include <linux/mutex.h>
  28. #include <linux/nsproxy.h>
  29. #include <linux/poll.h>
  30. #include <linux/debugfs.h>
  31. #include <linux/rbtree.h>
  32. #include <linux/sched.h>
  33. #include <linux/seq_file.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/vmalloc.h>
  36. #include <linux/slab.h>
  37. #include <linux/pid_namespace.h>
  38. #include <linux/security.h>
  39. #ifdef CONFIG_ANDROID_BINDER_IPC_32BIT
  40. #define BINDER_IPC_32BIT 1
  41. #endif
  42. #include <uapi/linux/android/binder.h>
  43. #include "binder_trace.h"
  44. static DEFINE_MUTEX(binder_main_lock);
  45. static DEFINE_MUTEX(binder_deferred_lock);
  46. static DEFINE_MUTEX(binder_mmap_lock);
  47. static HLIST_HEAD(binder_procs);
  48. static HLIST_HEAD(binder_deferred_list);
  49. static HLIST_HEAD(binder_dead_nodes);
  50. static struct dentry *binder_debugfs_dir_entry_root;
  51. static struct dentry *binder_debugfs_dir_entry_proc;
  52. static struct binder_node *binder_context_mgr_node;
  53. static kuid_t binder_context_mgr_uid = INVALID_UID;
  54. static int binder_last_id;
  55. #define BINDER_DEBUG_ENTRY(name) \
  56. static int binder_##name##_open(struct inode *inode, struct file *file) \
  57. { \
  58. return single_open(file, binder_##name##_show, inode->i_private); \
  59. } \
  60. \
  61. static const struct file_operations binder_##name##_fops = { \
  62. .owner = THIS_MODULE, \
  63. .open = binder_##name##_open, \
  64. .read = seq_read, \
  65. .llseek = seq_lseek, \
  66. .release = single_release, \
  67. }
  68. static int binder_proc_show(struct seq_file *m, void *unused);
  69. BINDER_DEBUG_ENTRY(proc);
  70. /* This is only defined in include/asm-arm/sizes.h */
  71. #ifndef SZ_1K
  72. #define SZ_1K 0x400
  73. #endif
  74. #ifndef SZ_4M
  75. #define SZ_4M 0x400000
  76. #endif
  77. #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
  78. #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
  79. enum {
  80. BINDER_DEBUG_USER_ERROR = 1U << 0,
  81. BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
  82. BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
  83. BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
  84. BINDER_DEBUG_DEAD_BINDER = 1U << 4,
  85. BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
  86. BINDER_DEBUG_READ_WRITE = 1U << 6,
  87. BINDER_DEBUG_USER_REFS = 1U << 7,
  88. BINDER_DEBUG_THREADS = 1U << 8,
  89. BINDER_DEBUG_TRANSACTION = 1U << 9,
  90. BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
  91. BINDER_DEBUG_FREE_BUFFER = 1U << 11,
  92. BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
  93. BINDER_DEBUG_BUFFER_ALLOC = 1U << 13,
  94. BINDER_DEBUG_PRIORITY_CAP = 1U << 14,
  95. BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 15,
  96. };
  97. static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
  98. BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
  99. module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
  100. static bool binder_debug_no_lock;
  101. module_param_named(proc_no_lock, binder_debug_no_lock, bool, S_IWUSR | S_IRUGO);
  102. static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
  103. static int binder_stop_on_user_error;
  104. static int binder_set_stop_on_user_error(const char *val,
  105. struct kernel_param *kp)
  106. {
  107. int ret;
  108. ret = param_set_int(val, kp);
  109. if (binder_stop_on_user_error < 2)
  110. wake_up(&binder_user_error_wait);
  111. return ret;
  112. }
  113. module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
  114. param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
  115. #define binder_debug(mask, x...) \
  116. do { \
  117. if (binder_debug_mask & mask) \
  118. pr_info(x); \
  119. } while (0)
  120. #define binder_user_error(x...) \
  121. do { \
  122. if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
  123. pr_info(x); \
  124. if (binder_stop_on_user_error) \
  125. binder_stop_on_user_error = 2; \
  126. } while (0)
  127. enum binder_stat_types {
  128. BINDER_STAT_PROC,
  129. BINDER_STAT_THREAD,
  130. BINDER_STAT_NODE,
  131. BINDER_STAT_REF,
  132. BINDER_STAT_DEATH,
  133. BINDER_STAT_TRANSACTION,
  134. BINDER_STAT_TRANSACTION_COMPLETE,
  135. BINDER_STAT_COUNT
  136. };
  137. struct binder_stats {
  138. int br[_IOC_NR(BR_FAILED_REPLY) + 1];
  139. int bc[_IOC_NR(BC_DEAD_BINDER_DONE) + 1];
  140. int obj_created[BINDER_STAT_COUNT];
  141. int obj_deleted[BINDER_STAT_COUNT];
  142. };
  143. static struct binder_stats binder_stats;
  144. static inline void binder_stats_deleted(enum binder_stat_types type)
  145. {
  146. binder_stats.obj_deleted[type]++;
  147. }
  148. static inline void binder_stats_created(enum binder_stat_types type)
  149. {
  150. binder_stats.obj_created[type]++;
  151. }
  152. struct binder_transaction_log_entry {
  153. int debug_id;
  154. int call_type;
  155. int from_proc;
  156. int from_thread;
  157. int target_handle;
  158. int to_proc;
  159. int to_thread;
  160. int to_node;
  161. int data_size;
  162. int offsets_size;
  163. };
  164. struct binder_transaction_log {
  165. int next;
  166. int full;
  167. struct binder_transaction_log_entry entry[32];
  168. };
  169. static struct binder_transaction_log binder_transaction_log;
  170. static struct binder_transaction_log binder_transaction_log_failed;
  171. static struct binder_transaction_log_entry *binder_transaction_log_add(
  172. struct binder_transaction_log *log)
  173. {
  174. struct binder_transaction_log_entry *e;
  175. e = &log->entry[log->next];
  176. memset(e, 0, sizeof(*e));
  177. log->next++;
  178. if (log->next == ARRAY_SIZE(log->entry)) {
  179. log->next = 0;
  180. log->full = 1;
  181. }
  182. return e;
  183. }
  184. struct binder_work {
  185. struct list_head entry;
  186. enum {
  187. BINDER_WORK_TRANSACTION = 1,
  188. BINDER_WORK_TRANSACTION_COMPLETE,
  189. BINDER_WORK_NODE,
  190. BINDER_WORK_DEAD_BINDER,
  191. BINDER_WORK_DEAD_BINDER_AND_CLEAR,
  192. BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
  193. } type;
  194. };
  195. struct binder_node {
  196. int debug_id;
  197. struct binder_work work;
  198. union {
  199. struct rb_node rb_node;
  200. struct hlist_node dead_node;
  201. };
  202. struct binder_proc *proc;
  203. struct hlist_head refs;
  204. int internal_strong_refs;
  205. int local_weak_refs;
  206. int local_strong_refs;
  207. binder_uintptr_t ptr;
  208. binder_uintptr_t cookie;
  209. unsigned has_strong_ref:1;
  210. unsigned pending_strong_ref:1;
  211. unsigned has_weak_ref:1;
  212. unsigned pending_weak_ref:1;
  213. unsigned has_async_transaction:1;
  214. unsigned accept_fds:1;
  215. unsigned min_priority:8;
  216. struct list_head async_todo;
  217. };
  218. struct binder_ref_death {
  219. struct binder_work work;
  220. binder_uintptr_t cookie;
  221. };
  222. struct binder_ref {
  223. /* Lookups needed: */
  224. /* node + proc => ref (transaction) */
  225. /* desc + proc => ref (transaction, inc/dec ref) */
  226. /* node => refs + procs (proc exit) */
  227. int debug_id;
  228. struct rb_node rb_node_desc;
  229. struct rb_node rb_node_node;
  230. struct hlist_node node_entry;
  231. struct binder_proc *proc;
  232. struct binder_node *node;
  233. uint32_t desc;
  234. int strong;
  235. int weak;
  236. struct binder_ref_death *death;
  237. };
  238. struct binder_buffer {
  239. struct list_head entry; /* free and allocated entries by address */
  240. struct rb_node rb_node; /* free entry by size or allocated entry */
  241. /* by address */
  242. unsigned free:1;
  243. unsigned allow_user_free:1;
  244. unsigned async_transaction:1;
  245. unsigned debug_id:29;
  246. struct binder_transaction *transaction;
  247. struct binder_node *target_node;
  248. size_t data_size;
  249. size_t offsets_size;
  250. uint8_t data[0];
  251. };
  252. enum binder_deferred_state {
  253. BINDER_DEFERRED_PUT_FILES = 0x01,
  254. BINDER_DEFERRED_FLUSH = 0x02,
  255. BINDER_DEFERRED_RELEASE = 0x04,
  256. };
  257. struct binder_proc {
  258. struct hlist_node proc_node;
  259. struct rb_root threads;
  260. struct rb_root nodes;
  261. struct rb_root refs_by_desc;
  262. struct rb_root refs_by_node;
  263. int pid;
  264. struct vm_area_struct *vma;
  265. struct mm_struct *vma_vm_mm;
  266. struct task_struct *tsk;
  267. struct files_struct *files;
  268. struct hlist_node deferred_work_node;
  269. int deferred_work;
  270. void *buffer;
  271. ptrdiff_t user_buffer_offset;
  272. struct list_head buffers;
  273. struct rb_root free_buffers;
  274. struct rb_root allocated_buffers;
  275. size_t free_async_space;
  276. struct page **pages;
  277. size_t buffer_size;
  278. uint32_t buffer_free;
  279. struct list_head todo;
  280. wait_queue_head_t wait;
  281. struct binder_stats stats;
  282. struct list_head delivered_death;
  283. int max_threads;
  284. int requested_threads;
  285. int requested_threads_started;
  286. int ready_threads;
  287. long default_priority;
  288. struct dentry *debugfs_entry;
  289. };
  290. enum {
  291. BINDER_LOOPER_STATE_REGISTERED = 0x01,
  292. BINDER_LOOPER_STATE_ENTERED = 0x02,
  293. BINDER_LOOPER_STATE_EXITED = 0x04,
  294. BINDER_LOOPER_STATE_INVALID = 0x08,
  295. BINDER_LOOPER_STATE_WAITING = 0x10,
  296. BINDER_LOOPER_STATE_NEED_RETURN = 0x20
  297. };
  298. struct binder_thread {
  299. struct binder_proc *proc;
  300. struct rb_node rb_node;
  301. int pid;
  302. int looper;
  303. struct binder_transaction *transaction_stack;
  304. struct list_head todo;
  305. uint32_t return_error; /* Write failed, return error code in read buf */
  306. uint32_t return_error2; /* Write failed, return error code in read */
  307. /* buffer. Used when sending a reply to a dead process that */
  308. /* we are also waiting on */
  309. wait_queue_head_t wait;
  310. struct binder_stats stats;
  311. };
  312. struct binder_transaction {
  313. int debug_id;
  314. struct binder_work work;
  315. struct binder_thread *from;
  316. struct binder_transaction *from_parent;
  317. struct binder_proc *to_proc;
  318. struct binder_thread *to_thread;
  319. struct binder_transaction *to_parent;
  320. unsigned need_reply:1;
  321. /* unsigned is_dead:1; */ /* not used at the moment */
  322. struct binder_buffer *buffer;
  323. unsigned int code;
  324. unsigned int flags;
  325. long priority;
  326. long saved_priority;
  327. kuid_t sender_euid;
  328. };
  329. static void
  330. binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
  331. static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
  332. {
  333. struct files_struct *files = proc->files;
  334. unsigned long rlim_cur;
  335. unsigned long irqs;
  336. if (files == NULL)
  337. return -ESRCH;
  338. if (!lock_task_sighand(proc->tsk, &irqs))
  339. return -EMFILE;
  340. rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
  341. unlock_task_sighand(proc->tsk, &irqs);
  342. return __alloc_fd(files, 0, rlim_cur, flags);
  343. }
  344. /*
  345. * copied from fd_install
  346. */
  347. static void task_fd_install(
  348. struct binder_proc *proc, unsigned int fd, struct file *file)
  349. {
  350. if (proc->files)
  351. __fd_install(proc->files, fd, file);
  352. }
  353. /*
  354. * copied from sys_close
  355. */
  356. static long task_close_fd(struct binder_proc *proc, unsigned int fd)
  357. {
  358. int retval;
  359. if (proc->files == NULL)
  360. return -ESRCH;
  361. retval = __close_fd(proc->files, fd);
  362. /* can't restart close syscall because file table entry was cleared */
  363. if (unlikely(retval == -ERESTARTSYS ||
  364. retval == -ERESTARTNOINTR ||
  365. retval == -ERESTARTNOHAND ||
  366. retval == -ERESTART_RESTARTBLOCK))
  367. retval = -EINTR;
  368. return retval;
  369. }
  370. static inline void binder_lock(const char *tag)
  371. {
  372. trace_binder_lock(tag);
  373. mutex_lock(&binder_main_lock);
  374. trace_binder_locked(tag);
  375. }
  376. static inline void binder_unlock(const char *tag)
  377. {
  378. trace_binder_unlock(tag);
  379. mutex_unlock(&binder_main_lock);
  380. }
  381. static void binder_set_nice(long nice)
  382. {
  383. long min_nice;
  384. if (can_nice(current, nice)) {
  385. set_user_nice(current, nice);
  386. return;
  387. }
  388. min_nice = rlimit_to_nice(current->signal->rlim[RLIMIT_NICE].rlim_cur);
  389. binder_debug(BINDER_DEBUG_PRIORITY_CAP,
  390. "%d: nice value %ld not allowed use %ld instead\n",
  391. current->pid, nice, min_nice);
  392. set_user_nice(current, min_nice);
  393. if (min_nice <= MAX_NICE)
  394. return;
  395. binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
  396. }
  397. static size_t binder_buffer_size(struct binder_proc *proc,
  398. struct binder_buffer *buffer)
  399. {
  400. if (list_is_last(&buffer->entry, &proc->buffers))
  401. return proc->buffer + proc->buffer_size - (void *)buffer->data;
  402. return (size_t)list_entry(buffer->entry.next,
  403. struct binder_buffer, entry) - (size_t)buffer->data;
  404. }
  405. static void binder_insert_free_buffer(struct binder_proc *proc,
  406. struct binder_buffer *new_buffer)
  407. {
  408. struct rb_node **p = &proc->free_buffers.rb_node;
  409. struct rb_node *parent = NULL;
  410. struct binder_buffer *buffer;
  411. size_t buffer_size;
  412. size_t new_buffer_size;
  413. BUG_ON(!new_buffer->free);
  414. new_buffer_size = binder_buffer_size(proc, new_buffer);
  415. binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
  416. "%d: add free buffer, size %zd, at %p\n",
  417. proc->pid, new_buffer_size, new_buffer);
  418. while (*p) {
  419. parent = *p;
  420. buffer = rb_entry(parent, struct binder_buffer, rb_node);
  421. BUG_ON(!buffer->free);
  422. buffer_size = binder_buffer_size(proc, buffer);
  423. if (new_buffer_size < buffer_size)
  424. p = &parent->rb_left;
  425. else
  426. p = &parent->rb_right;
  427. }
  428. rb_link_node(&new_buffer->rb_node, parent, p);
  429. rb_insert_color(&new_buffer->rb_node, &proc->free_buffers);
  430. }
  431. static void binder_insert_allocated_buffer(struct binder_proc *proc,
  432. struct binder_buffer *new_buffer)
  433. {
  434. struct rb_node **p = &proc->allocated_buffers.rb_node;
  435. struct rb_node *parent = NULL;
  436. struct binder_buffer *buffer;
  437. BUG_ON(new_buffer->free);
  438. while (*p) {
  439. parent = *p;
  440. buffer = rb_entry(parent, struct binder_buffer, rb_node);
  441. BUG_ON(buffer->free);
  442. if (new_buffer < buffer)
  443. p = &parent->rb_left;
  444. else if (new_buffer > buffer)
  445. p = &parent->rb_right;
  446. else
  447. BUG();
  448. }
  449. rb_link_node(&new_buffer->rb_node, parent, p);
  450. rb_insert_color(&new_buffer->rb_node, &proc->allocated_buffers);
  451. }
  452. static struct binder_buffer *binder_buffer_lookup(struct binder_proc *proc,
  453. uintptr_t user_ptr)
  454. {
  455. struct rb_node *n = proc->allocated_buffers.rb_node;
  456. struct binder_buffer *buffer;
  457. struct binder_buffer *kern_ptr;
  458. kern_ptr = (struct binder_buffer *)(user_ptr - proc->user_buffer_offset
  459. - offsetof(struct binder_buffer, data));
  460. while (n) {
  461. buffer = rb_entry(n, struct binder_buffer, rb_node);
  462. BUG_ON(buffer->free);
  463. if (kern_ptr < buffer)
  464. n = n->rb_left;
  465. else if (kern_ptr > buffer)
  466. n = n->rb_right;
  467. else
  468. return buffer;
  469. }
  470. return NULL;
  471. }
  472. static int binder_update_page_range(struct binder_proc *proc, int allocate,
  473. void *start, void *end,
  474. struct vm_area_struct *vma)
  475. {
  476. void *page_addr;
  477. unsigned long user_page_addr;
  478. struct page **page;
  479. struct mm_struct *mm;
  480. binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
  481. "%d: %s pages %p-%p\n", proc->pid,
  482. allocate ? "allocate" : "free", start, end);
  483. if (end <= start)
  484. return 0;
  485. trace_binder_update_page_range(proc, allocate, start, end);
  486. if (vma)
  487. mm = NULL;
  488. else
  489. mm = get_task_mm(proc->tsk);
  490. if (mm) {
  491. down_write(&mm->mmap_sem);
  492. vma = proc->vma;
  493. if (vma && mm != proc->vma_vm_mm) {
  494. pr_err("%d: vma mm and task mm mismatch\n",
  495. proc->pid);
  496. vma = NULL;
  497. }
  498. }
  499. if (allocate == 0)
  500. goto free_range;
  501. if (vma == NULL) {
  502. pr_err("%d: binder_alloc_buf failed to map pages in userspace, no vma\n",
  503. proc->pid);
  504. goto err_no_vma;
  505. }
  506. for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
  507. int ret;
  508. page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
  509. BUG_ON(*page);
  510. *page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
  511. if (*page == NULL) {
  512. pr_err("%d: binder_alloc_buf failed for page at %p\n",
  513. proc->pid, page_addr);
  514. goto err_alloc_page_failed;
  515. }
  516. ret = map_kernel_range_noflush((unsigned long)page_addr,
  517. PAGE_SIZE, PAGE_KERNEL, page);
  518. flush_cache_vmap((unsigned long)page_addr,
  519. (unsigned long)page_addr + PAGE_SIZE);
  520. if (ret != 1) {
  521. pr_err("%d: binder_alloc_buf failed to map page at %p in kernel\n",
  522. proc->pid, page_addr);
  523. goto err_map_kernel_failed;
  524. }
  525. user_page_addr =
  526. (uintptr_t)page_addr + proc->user_buffer_offset;
  527. ret = vm_insert_page(vma, user_page_addr, page[0]);
  528. if (ret) {
  529. pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
  530. proc->pid, user_page_addr);
  531. goto err_vm_insert_page_failed;
  532. }
  533. /* vm_insert_page does not seem to increment the refcount */
  534. }
  535. if (mm) {
  536. up_write(&mm->mmap_sem);
  537. mmput(mm);
  538. }
  539. return 0;
  540. free_range:
  541. for (page_addr = end - PAGE_SIZE; page_addr >= start;
  542. page_addr -= PAGE_SIZE) {
  543. page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
  544. if (vma)
  545. zap_page_range(vma, (uintptr_t)page_addr +
  546. proc->user_buffer_offset, PAGE_SIZE, NULL);
  547. err_vm_insert_page_failed:
  548. unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
  549. err_map_kernel_failed:
  550. __free_page(*page);
  551. *page = NULL;
  552. err_alloc_page_failed:
  553. ;
  554. }
  555. err_no_vma:
  556. if (mm) {
  557. up_write(&mm->mmap_sem);
  558. mmput(mm);
  559. }
  560. return -ENOMEM;
  561. }
  562. static struct binder_buffer *binder_alloc_buf(struct binder_proc *proc,
  563. size_t data_size,
  564. size_t offsets_size, int is_async)
  565. {
  566. struct rb_node *n = proc->free_buffers.rb_node;
  567. struct binder_buffer *buffer;
  568. size_t buffer_size;
  569. struct rb_node *best_fit = NULL;
  570. void *has_page_addr;
  571. void *end_page_addr;
  572. size_t size;
  573. if (proc->vma == NULL) {
  574. pr_err("%d: binder_alloc_buf, no vma\n",
  575. proc->pid);
  576. return NULL;
  577. }
  578. size = ALIGN(data_size, sizeof(void *)) +
  579. ALIGN(offsets_size, sizeof(void *));
  580. if (size < data_size || size < offsets_size) {
  581. binder_user_error("%d: got transaction with invalid size %zd-%zd\n",
  582. proc->pid, data_size, offsets_size);
  583. return NULL;
  584. }
  585. if (is_async &&
  586. proc->free_async_space < size + sizeof(struct binder_buffer)) {
  587. binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
  588. "%d: binder_alloc_buf size %zd failed, no async space left\n",
  589. proc->pid, size);
  590. return NULL;
  591. }
  592. while (n) {
  593. buffer = rb_entry(n, struct binder_buffer, rb_node);
  594. BUG_ON(!buffer->free);
  595. buffer_size = binder_buffer_size(proc, buffer);
  596. if (size < buffer_size) {
  597. best_fit = n;
  598. n = n->rb_left;
  599. } else if (size > buffer_size)
  600. n = n->rb_right;
  601. else {
  602. best_fit = n;
  603. break;
  604. }
  605. }
  606. if (best_fit == NULL) {
  607. pr_err("%d: binder_alloc_buf size %zd failed, no address space\n",
  608. proc->pid, size);
  609. return NULL;
  610. }
  611. if (n == NULL) {
  612. buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
  613. buffer_size = binder_buffer_size(proc, buffer);
  614. }
  615. binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
  616. "%d: binder_alloc_buf size %zd got buffer %p size %zd\n",
  617. proc->pid, size, buffer, buffer_size);
  618. has_page_addr =
  619. (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK);
  620. if (n == NULL) {
  621. if (size + sizeof(struct binder_buffer) + 4 >= buffer_size)
  622. buffer_size = size; /* no room for other buffers */
  623. else
  624. buffer_size = size + sizeof(struct binder_buffer);
  625. }
  626. end_page_addr =
  627. (void *)PAGE_ALIGN((uintptr_t)buffer->data + buffer_size);
  628. if (end_page_addr > has_page_addr)
  629. end_page_addr = has_page_addr;
  630. if (binder_update_page_range(proc, 1,
  631. (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL))
  632. return NULL;
  633. rb_erase(best_fit, &proc->free_buffers);
  634. buffer->free = 0;
  635. binder_insert_allocated_buffer(proc, buffer);
  636. if (buffer_size != size) {
  637. struct binder_buffer *new_buffer = (void *)buffer->data + size;
  638. list_add(&new_buffer->entry, &buffer->entry);
  639. new_buffer->free = 1;
  640. binder_insert_free_buffer(proc, new_buffer);
  641. }
  642. binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
  643. "%d: binder_alloc_buf size %zd got %p\n",
  644. proc->pid, size, buffer);
  645. buffer->data_size = data_size;
  646. buffer->offsets_size = offsets_size;
  647. buffer->async_transaction = is_async;
  648. if (is_async) {
  649. proc->free_async_space -= size + sizeof(struct binder_buffer);
  650. binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
  651. "%d: binder_alloc_buf size %zd async free %zd\n",
  652. proc->pid, size, proc->free_async_space);
  653. }
  654. return buffer;
  655. }
  656. static void *buffer_start_page(struct binder_buffer *buffer)
  657. {
  658. return (void *)((uintptr_t)buffer & PAGE_MASK);
  659. }
  660. static void *buffer_end_page(struct binder_buffer *buffer)
  661. {
  662. return (void *)(((uintptr_t)(buffer + 1) - 1) & PAGE_MASK);
  663. }
  664. static void binder_delete_free_buffer(struct binder_proc *proc,
  665. struct binder_buffer *buffer)
  666. {
  667. struct binder_buffer *prev, *next = NULL;
  668. int free_page_end = 1;
  669. int free_page_start = 1;
  670. BUG_ON(proc->buffers.next == &buffer->entry);
  671. prev = list_entry(buffer->entry.prev, struct binder_buffer, entry);
  672. BUG_ON(!prev->free);
  673. if (buffer_end_page(prev) == buffer_start_page(buffer)) {
  674. free_page_start = 0;
  675. if (buffer_end_page(prev) == buffer_end_page(buffer))
  676. free_page_end = 0;
  677. binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
  678. "%d: merge free, buffer %p share page with %p\n",
  679. proc->pid, buffer, prev);
  680. }
  681. if (!list_is_last(&buffer->entry, &proc->buffers)) {
  682. next = list_entry(buffer->entry.next,
  683. struct binder_buffer, entry);
  684. if (buffer_start_page(next) == buffer_end_page(buffer)) {
  685. free_page_end = 0;
  686. if (buffer_start_page(next) ==
  687. buffer_start_page(buffer))
  688. free_page_start = 0;
  689. binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
  690. "%d: merge free, buffer %p share page with %p\n",
  691. proc->pid, buffer, prev);
  692. }
  693. }
  694. list_del(&buffer->entry);
  695. if (free_page_start || free_page_end) {
  696. binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
  697. "%d: merge free, buffer %p do not share page%s%s with %p or %p\n",
  698. proc->pid, buffer, free_page_start ? "" : " end",
  699. free_page_end ? "" : " start", prev, next);
  700. binder_update_page_range(proc, 0, free_page_start ?
  701. buffer_start_page(buffer) : buffer_end_page(buffer),
  702. (free_page_end ? buffer_end_page(buffer) :
  703. buffer_start_page(buffer)) + PAGE_SIZE, NULL);
  704. }
  705. }
  706. static void binder_free_buf(struct binder_proc *proc,
  707. struct binder_buffer *buffer)
  708. {
  709. size_t size, buffer_size;
  710. buffer_size = binder_buffer_size(proc, buffer);
  711. size = ALIGN(buffer->data_size, sizeof(void *)) +
  712. ALIGN(buffer->offsets_size, sizeof(void *));
  713. binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
  714. "%d: binder_free_buf %p size %zd buffer_size %zd\n",
  715. proc->pid, buffer, size, buffer_size);
  716. BUG_ON(buffer->free);
  717. BUG_ON(size > buffer_size);
  718. BUG_ON(buffer->transaction != NULL);
  719. BUG_ON((void *)buffer < proc->buffer);
  720. BUG_ON((void *)buffer > proc->buffer + proc->buffer_size);
  721. if (buffer->async_transaction) {
  722. proc->free_async_space += size + sizeof(struct binder_buffer);
  723. binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
  724. "%d: binder_free_buf size %zd async free %zd\n",
  725. proc->pid, size, proc->free_async_space);
  726. }
  727. binder_update_page_range(proc, 0,
  728. (void *)PAGE_ALIGN((uintptr_t)buffer->data),
  729. (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK),
  730. NULL);
  731. rb_erase(&buffer->rb_node, &proc->allocated_buffers);
  732. buffer->free = 1;
  733. if (!list_is_last(&buffer->entry, &proc->buffers)) {
  734. struct binder_buffer *next = list_entry(buffer->entry.next,
  735. struct binder_buffer, entry);
  736. if (next->free) {
  737. rb_erase(&next->rb_node, &proc->free_buffers);
  738. binder_delete_free_buffer(proc, next);
  739. }
  740. }
  741. if (proc->buffers.next != &buffer->entry) {
  742. struct binder_buffer *prev = list_entry(buffer->entry.prev,
  743. struct binder_buffer, entry);
  744. if (prev->free) {
  745. binder_delete_free_buffer(proc, buffer);
  746. rb_erase(&prev->rb_node, &proc->free_buffers);
  747. buffer = prev;
  748. }
  749. }
  750. binder_insert_free_buffer(proc, buffer);
  751. }
  752. static struct binder_node *binder_get_node(struct binder_proc *proc,
  753. binder_uintptr_t ptr)
  754. {
  755. struct rb_node *n = proc->nodes.rb_node;
  756. struct binder_node *node;
  757. while (n) {
  758. node = rb_entry(n, struct binder_node, rb_node);
  759. if (ptr < node->ptr)
  760. n = n->rb_left;
  761. else if (ptr > node->ptr)
  762. n = n->rb_right;
  763. else
  764. return node;
  765. }
  766. return NULL;
  767. }
  768. static struct binder_node *binder_new_node(struct binder_proc *proc,
  769. binder_uintptr_t ptr,
  770. binder_uintptr_t cookie)
  771. {
  772. struct rb_node **p = &proc->nodes.rb_node;
  773. struct rb_node *parent = NULL;
  774. struct binder_node *node;
  775. while (*p) {
  776. parent = *p;
  777. node = rb_entry(parent, struct binder_node, rb_node);
  778. if (ptr < node->ptr)
  779. p = &(*p)->rb_left;
  780. else if (ptr > node->ptr)
  781. p = &(*p)->rb_right;
  782. else
  783. return NULL;
  784. }
  785. node = kzalloc(sizeof(*node), GFP_KERNEL);
  786. if (node == NULL)
  787. return NULL;
  788. binder_stats_created(BINDER_STAT_NODE);
  789. rb_link_node(&node->rb_node, parent, p);
  790. rb_insert_color(&node->rb_node, &proc->nodes);
  791. node->debug_id = ++binder_last_id;
  792. node->proc = proc;
  793. node->ptr = ptr;
  794. node->cookie = cookie;
  795. node->work.type = BINDER_WORK_NODE;
  796. INIT_LIST_HEAD(&node->work.entry);
  797. INIT_LIST_HEAD(&node->async_todo);
  798. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  799. "%d:%d node %d u%016llx c%016llx created\n",
  800. proc->pid, current->pid, node->debug_id,
  801. (u64)node->ptr, (u64)node->cookie);
  802. return node;
  803. }
  804. static int binder_inc_node(struct binder_node *node, int strong, int internal,
  805. struct list_head *target_list)
  806. {
  807. if (strong) {
  808. if (internal) {
  809. if (target_list == NULL &&
  810. node->internal_strong_refs == 0 &&
  811. !(node == binder_context_mgr_node &&
  812. node->has_strong_ref)) {
  813. pr_err("invalid inc strong node for %d\n",
  814. node->debug_id);
  815. return -EINVAL;
  816. }
  817. node->internal_strong_refs++;
  818. } else
  819. node->local_strong_refs++;
  820. if (!node->has_strong_ref && target_list) {
  821. list_del_init(&node->work.entry);
  822. list_add_tail(&node->work.entry, target_list);
  823. }
  824. } else {
  825. if (!internal)
  826. node->local_weak_refs++;
  827. if (!node->has_weak_ref && list_empty(&node->work.entry)) {
  828. if (target_list == NULL) {
  829. pr_err("invalid inc weak node for %d\n",
  830. node->debug_id);
  831. return -EINVAL;
  832. }
  833. list_add_tail(&node->work.entry, target_list);
  834. }
  835. }
  836. return 0;
  837. }
  838. static int binder_dec_node(struct binder_node *node, int strong, int internal)
  839. {
  840. if (strong) {
  841. if (internal)
  842. node->internal_strong_refs--;
  843. else
  844. node->local_strong_refs--;
  845. if (node->local_strong_refs || node->internal_strong_refs)
  846. return 0;
  847. } else {
  848. if (!internal)
  849. node->local_weak_refs--;
  850. if (node->local_weak_refs || !hlist_empty(&node->refs))
  851. return 0;
  852. }
  853. if (node->proc && (node->has_strong_ref || node->has_weak_ref)) {
  854. if (list_empty(&node->work.entry)) {
  855. list_add_tail(&node->work.entry, &node->proc->todo);
  856. wake_up_interruptible(&node->proc->wait);
  857. }
  858. } else {
  859. if (hlist_empty(&node->refs) && !node->local_strong_refs &&
  860. !node->local_weak_refs) {
  861. list_del_init(&node->work.entry);
  862. if (node->proc) {
  863. rb_erase(&node->rb_node, &node->proc->nodes);
  864. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  865. "refless node %d deleted\n",
  866. node->debug_id);
  867. } else {
  868. hlist_del(&node->dead_node);
  869. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  870. "dead node %d deleted\n",
  871. node->debug_id);
  872. }
  873. kfree(node);
  874. binder_stats_deleted(BINDER_STAT_NODE);
  875. }
  876. }
  877. return 0;
  878. }
  879. static struct binder_ref *binder_get_ref(struct binder_proc *proc,
  880. u32 desc, bool need_strong_ref)
  881. {
  882. struct rb_node *n = proc->refs_by_desc.rb_node;
  883. struct binder_ref *ref;
  884. while (n) {
  885. ref = rb_entry(n, struct binder_ref, rb_node_desc);
  886. if (desc < ref->desc) {
  887. n = n->rb_left;
  888. } else if (desc > ref->desc) {
  889. n = n->rb_right;
  890. } else if (need_strong_ref && !ref->strong) {
  891. binder_user_error("tried to use weak ref as strong ref\n");
  892. return NULL;
  893. } else {
  894. return ref;
  895. }
  896. }
  897. return NULL;
  898. }
  899. static struct binder_ref *binder_get_ref_for_node(struct binder_proc *proc,
  900. struct binder_node *node)
  901. {
  902. struct rb_node *n;
  903. struct rb_node **p = &proc->refs_by_node.rb_node;
  904. struct rb_node *parent = NULL;
  905. struct binder_ref *ref, *new_ref;
  906. while (*p) {
  907. parent = *p;
  908. ref = rb_entry(parent, struct binder_ref, rb_node_node);
  909. if (node < ref->node)
  910. p = &(*p)->rb_left;
  911. else if (node > ref->node)
  912. p = &(*p)->rb_right;
  913. else
  914. return ref;
  915. }
  916. new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
  917. if (new_ref == NULL)
  918. return NULL;
  919. binder_stats_created(BINDER_STAT_REF);
  920. new_ref->debug_id = ++binder_last_id;
  921. new_ref->proc = proc;
  922. new_ref->node = node;
  923. rb_link_node(&new_ref->rb_node_node, parent, p);
  924. rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
  925. new_ref->desc = (node == binder_context_mgr_node) ? 0 : 1;
  926. for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
  927. ref = rb_entry(n, struct binder_ref, rb_node_desc);
  928. if (ref->desc > new_ref->desc)
  929. break;
  930. new_ref->desc = ref->desc + 1;
  931. }
  932. p = &proc->refs_by_desc.rb_node;
  933. while (*p) {
  934. parent = *p;
  935. ref = rb_entry(parent, struct binder_ref, rb_node_desc);
  936. if (new_ref->desc < ref->desc)
  937. p = &(*p)->rb_left;
  938. else if (new_ref->desc > ref->desc)
  939. p = &(*p)->rb_right;
  940. else
  941. BUG();
  942. }
  943. rb_link_node(&new_ref->rb_node_desc, parent, p);
  944. rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
  945. if (node) {
  946. hlist_add_head(&new_ref->node_entry, &node->refs);
  947. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  948. "%d new ref %d desc %d for node %d\n",
  949. proc->pid, new_ref->debug_id, new_ref->desc,
  950. node->debug_id);
  951. } else {
  952. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  953. "%d new ref %d desc %d for dead node\n",
  954. proc->pid, new_ref->debug_id, new_ref->desc);
  955. }
  956. return new_ref;
  957. }
  958. static void binder_delete_ref(struct binder_ref *ref)
  959. {
  960. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  961. "%d delete ref %d desc %d for node %d\n",
  962. ref->proc->pid, ref->debug_id, ref->desc,
  963. ref->node->debug_id);
  964. rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
  965. rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
  966. if (ref->strong)
  967. binder_dec_node(ref->node, 1, 1);
  968. hlist_del(&ref->node_entry);
  969. binder_dec_node(ref->node, 0, 1);
  970. if (ref->death) {
  971. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  972. "%d delete ref %d desc %d has death notification\n",
  973. ref->proc->pid, ref->debug_id, ref->desc);
  974. list_del(&ref->death->work.entry);
  975. kfree(ref->death);
  976. binder_stats_deleted(BINDER_STAT_DEATH);
  977. }
  978. kfree(ref);
  979. binder_stats_deleted(BINDER_STAT_REF);
  980. }
  981. static int binder_inc_ref(struct binder_ref *ref, int strong,
  982. struct list_head *target_list)
  983. {
  984. int ret;
  985. if (strong) {
  986. if (ref->strong == 0) {
  987. ret = binder_inc_node(ref->node, 1, 1, target_list);
  988. if (ret)
  989. return ret;
  990. }
  991. ref->strong++;
  992. } else {
  993. if (ref->weak == 0) {
  994. ret = binder_inc_node(ref->node, 0, 1, target_list);
  995. if (ret)
  996. return ret;
  997. }
  998. ref->weak++;
  999. }
  1000. return 0;
  1001. }
  1002. static int binder_dec_ref(struct binder_ref *ref, int strong)
  1003. {
  1004. if (strong) {
  1005. if (ref->strong == 0) {
  1006. binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
  1007. ref->proc->pid, ref->debug_id,
  1008. ref->desc, ref->strong, ref->weak);
  1009. return -EINVAL;
  1010. }
  1011. ref->strong--;
  1012. if (ref->strong == 0) {
  1013. int ret;
  1014. ret = binder_dec_node(ref->node, strong, 1);
  1015. if (ret)
  1016. return ret;
  1017. }
  1018. } else {
  1019. if (ref->weak == 0) {
  1020. binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
  1021. ref->proc->pid, ref->debug_id,
  1022. ref->desc, ref->strong, ref->weak);
  1023. return -EINVAL;
  1024. }
  1025. ref->weak--;
  1026. }
  1027. if (ref->strong == 0 && ref->weak == 0)
  1028. binder_delete_ref(ref);
  1029. return 0;
  1030. }
  1031. static void binder_pop_transaction(struct binder_thread *target_thread,
  1032. struct binder_transaction *t)
  1033. {
  1034. if (target_thread) {
  1035. BUG_ON(target_thread->transaction_stack != t);
  1036. BUG_ON(target_thread->transaction_stack->from != target_thread);
  1037. target_thread->transaction_stack =
  1038. target_thread->transaction_stack->from_parent;
  1039. t->from = NULL;
  1040. }
  1041. t->need_reply = 0;
  1042. if (t->buffer)
  1043. t->buffer->transaction = NULL;
  1044. kfree(t);
  1045. binder_stats_deleted(BINDER_STAT_TRANSACTION);
  1046. }
  1047. static void binder_send_failed_reply(struct binder_transaction *t,
  1048. uint32_t error_code)
  1049. {
  1050. struct binder_thread *target_thread;
  1051. struct binder_transaction *next;
  1052. BUG_ON(t->flags & TF_ONE_WAY);
  1053. while (1) {
  1054. target_thread = t->from;
  1055. if (target_thread) {
  1056. if (target_thread->return_error != BR_OK &&
  1057. target_thread->return_error2 == BR_OK) {
  1058. target_thread->return_error2 =
  1059. target_thread->return_error;
  1060. target_thread->return_error = BR_OK;
  1061. }
  1062. if (target_thread->return_error == BR_OK) {
  1063. binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
  1064. "send failed reply for transaction %d to %d:%d\n",
  1065. t->debug_id,
  1066. target_thread->proc->pid,
  1067. target_thread->pid);
  1068. binder_pop_transaction(target_thread, t);
  1069. target_thread->return_error = error_code;
  1070. wake_up_interruptible(&target_thread->wait);
  1071. } else {
  1072. pr_err("reply failed, target thread, %d:%d, has error code %d already\n",
  1073. target_thread->proc->pid,
  1074. target_thread->pid,
  1075. target_thread->return_error);
  1076. }
  1077. return;
  1078. }
  1079. next = t->from_parent;
  1080. binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
  1081. "send failed reply for transaction %d, target dead\n",
  1082. t->debug_id);
  1083. binder_pop_transaction(target_thread, t);
  1084. if (next == NULL) {
  1085. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  1086. "reply failed, no target thread at root\n");
  1087. return;
  1088. }
  1089. t = next;
  1090. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  1091. "reply failed, no target thread -- retry %d\n",
  1092. t->debug_id);
  1093. }
  1094. }
  1095. static void binder_transaction_buffer_release(struct binder_proc *proc,
  1096. struct binder_buffer *buffer,
  1097. binder_size_t *failed_at)
  1098. {
  1099. binder_size_t *offp, *off_end;
  1100. int debug_id = buffer->debug_id;
  1101. binder_debug(BINDER_DEBUG_TRANSACTION,
  1102. "%d buffer release %d, size %zd-%zd, failed at %p\n",
  1103. proc->pid, buffer->debug_id,
  1104. buffer->data_size, buffer->offsets_size, failed_at);
  1105. if (buffer->target_node)
  1106. binder_dec_node(buffer->target_node, 1, 0);
  1107. offp = (binder_size_t *)(buffer->data +
  1108. ALIGN(buffer->data_size, sizeof(void *)));
  1109. if (failed_at)
  1110. off_end = failed_at;
  1111. else
  1112. off_end = (void *)offp + buffer->offsets_size;
  1113. for (; offp < off_end; offp++) {
  1114. struct flat_binder_object *fp;
  1115. if (*offp > buffer->data_size - sizeof(*fp) ||
  1116. buffer->data_size < sizeof(*fp) ||
  1117. !IS_ALIGNED(*offp, sizeof(u32))) {
  1118. pr_err("transaction release %d bad offset %lld, size %zd\n",
  1119. debug_id, (u64)*offp, buffer->data_size);
  1120. continue;
  1121. }
  1122. fp = (struct flat_binder_object *)(buffer->data + *offp);
  1123. switch (fp->type) {
  1124. case BINDER_TYPE_BINDER:
  1125. case BINDER_TYPE_WEAK_BINDER: {
  1126. struct binder_node *node = binder_get_node(proc, fp->binder);
  1127. if (node == NULL) {
  1128. pr_err("transaction release %d bad node %016llx\n",
  1129. debug_id, (u64)fp->binder);
  1130. break;
  1131. }
  1132. binder_debug(BINDER_DEBUG_TRANSACTION,
  1133. " node %d u%016llx\n",
  1134. node->debug_id, (u64)node->ptr);
  1135. binder_dec_node(node, fp->type == BINDER_TYPE_BINDER, 0);
  1136. } break;
  1137. case BINDER_TYPE_HANDLE:
  1138. case BINDER_TYPE_WEAK_HANDLE: {
  1139. struct binder_ref *ref;
  1140. ref = binder_get_ref(proc, fp->handle,
  1141. fp->type == BINDER_TYPE_HANDLE);
  1142. if (ref == NULL) {
  1143. pr_err("transaction release %d bad handle %d\n",
  1144. debug_id, fp->handle);
  1145. break;
  1146. }
  1147. binder_debug(BINDER_DEBUG_TRANSACTION,
  1148. " ref %d desc %d (node %d)\n",
  1149. ref->debug_id, ref->desc, ref->node->debug_id);
  1150. binder_dec_ref(ref, fp->type == BINDER_TYPE_HANDLE);
  1151. } break;
  1152. case BINDER_TYPE_FD:
  1153. binder_debug(BINDER_DEBUG_TRANSACTION,
  1154. " fd %d\n", fp->handle);
  1155. if (failed_at)
  1156. task_close_fd(proc, fp->handle);
  1157. break;
  1158. default:
  1159. pr_err("transaction release %d bad object type %x\n",
  1160. debug_id, fp->type);
  1161. break;
  1162. }
  1163. }
  1164. }
  1165. static void binder_transaction(struct binder_proc *proc,
  1166. struct binder_thread *thread,
  1167. struct binder_transaction_data *tr, int reply)
  1168. {
  1169. struct binder_transaction *t;
  1170. struct binder_work *tcomplete;
  1171. binder_size_t *offp, *off_end;
  1172. binder_size_t off_min;
  1173. struct binder_proc *target_proc;
  1174. struct binder_thread *target_thread = NULL;
  1175. struct binder_node *target_node = NULL;
  1176. struct list_head *target_list;
  1177. wait_queue_head_t *target_wait;
  1178. struct binder_transaction *in_reply_to = NULL;
  1179. struct binder_transaction_log_entry *e;
  1180. uint32_t return_error;
  1181. e = binder_transaction_log_add(&binder_transaction_log);
  1182. e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
  1183. e->from_proc = proc->pid;
  1184. e->from_thread = thread->pid;
  1185. e->target_handle = tr->target.handle;
  1186. e->data_size = tr->data_size;
  1187. e->offsets_size = tr->offsets_size;
  1188. if (reply) {
  1189. in_reply_to = thread->transaction_stack;
  1190. if (in_reply_to == NULL) {
  1191. binder_user_error("%d:%d got reply transaction with no transaction stack\n",
  1192. proc->pid, thread->pid);
  1193. return_error = BR_FAILED_REPLY;
  1194. goto err_empty_call_stack;
  1195. }
  1196. binder_set_nice(in_reply_to->saved_priority);
  1197. if (in_reply_to->to_thread != thread) {
  1198. binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
  1199. proc->pid, thread->pid, in_reply_to->debug_id,
  1200. in_reply_to->to_proc ?
  1201. in_reply_to->to_proc->pid : 0,
  1202. in_reply_to->to_thread ?
  1203. in_reply_to->to_thread->pid : 0);
  1204. return_error = BR_FAILED_REPLY;
  1205. in_reply_to = NULL;
  1206. goto err_bad_call_stack;
  1207. }
  1208. thread->transaction_stack = in_reply_to->to_parent;
  1209. target_thread = in_reply_to->from;
  1210. if (target_thread == NULL) {
  1211. return_error = BR_DEAD_REPLY;
  1212. goto err_dead_binder;
  1213. }
  1214. if (target_thread->transaction_stack != in_reply_to) {
  1215. binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
  1216. proc->pid, thread->pid,
  1217. target_thread->transaction_stack ?
  1218. target_thread->transaction_stack->debug_id : 0,
  1219. in_reply_to->debug_id);
  1220. return_error = BR_FAILED_REPLY;
  1221. in_reply_to = NULL;
  1222. target_thread = NULL;
  1223. goto err_dead_binder;
  1224. }
  1225. target_proc = target_thread->proc;
  1226. } else {
  1227. if (tr->target.handle) {
  1228. struct binder_ref *ref;
  1229. ref = binder_get_ref(proc, tr->target.handle, true);
  1230. if (ref == NULL) {
  1231. binder_user_error("%d:%d got transaction to invalid handle\n",
  1232. proc->pid, thread->pid);
  1233. return_error = BR_FAILED_REPLY;
  1234. goto err_invalid_target_handle;
  1235. }
  1236. target_node = ref->node;
  1237. } else {
  1238. target_node = binder_context_mgr_node;
  1239. if (target_node == NULL) {
  1240. return_error = BR_DEAD_REPLY;
  1241. goto err_no_context_mgr_node;
  1242. }
  1243. }
  1244. e->to_node = target_node->debug_id;
  1245. target_proc = target_node->proc;
  1246. if (target_proc == NULL) {
  1247. return_error = BR_DEAD_REPLY;
  1248. goto err_dead_binder;
  1249. }
  1250. if (security_binder_transaction(proc->tsk,
  1251. target_proc->tsk) < 0) {
  1252. return_error = BR_FAILED_REPLY;
  1253. goto err_invalid_target_handle;
  1254. }
  1255. if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
  1256. struct binder_transaction *tmp;
  1257. tmp = thread->transaction_stack;
  1258. if (tmp->to_thread != thread) {
  1259. binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
  1260. proc->pid, thread->pid, tmp->debug_id,
  1261. tmp->to_proc ? tmp->to_proc->pid : 0,
  1262. tmp->to_thread ?
  1263. tmp->to_thread->pid : 0);
  1264. return_error = BR_FAILED_REPLY;
  1265. goto err_bad_call_stack;
  1266. }
  1267. while (tmp) {
  1268. if (tmp->from && tmp->from->proc == target_proc)
  1269. target_thread = tmp->from;
  1270. tmp = tmp->from_parent;
  1271. }
  1272. }
  1273. }
  1274. if (target_thread) {
  1275. e->to_thread = target_thread->pid;
  1276. target_list = &target_thread->todo;
  1277. target_wait = &target_thread->wait;
  1278. } else {
  1279. target_list = &target_proc->todo;
  1280. target_wait = &target_proc->wait;
  1281. }
  1282. e->to_proc = target_proc->pid;
  1283. /* TODO: reuse incoming transaction for reply */
  1284. t = kzalloc(sizeof(*t), GFP_KERNEL);
  1285. if (t == NULL) {
  1286. return_error = BR_FAILED_REPLY;
  1287. goto err_alloc_t_failed;
  1288. }
  1289. binder_stats_created(BINDER_STAT_TRANSACTION);
  1290. tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
  1291. if (tcomplete == NULL) {
  1292. return_error = BR_FAILED_REPLY;
  1293. goto err_alloc_tcomplete_failed;
  1294. }
  1295. binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
  1296. t->debug_id = ++binder_last_id;
  1297. e->debug_id = t->debug_id;
  1298. if (reply)
  1299. binder_debug(BINDER_DEBUG_TRANSACTION,
  1300. "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld\n",
  1301. proc->pid, thread->pid, t->debug_id,
  1302. target_proc->pid, target_thread->pid,
  1303. (u64)tr->data.ptr.buffer,
  1304. (u64)tr->data.ptr.offsets,
  1305. (u64)tr->data_size, (u64)tr->offsets_size);
  1306. else
  1307. binder_debug(BINDER_DEBUG_TRANSACTION,
  1308. "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld\n",
  1309. proc->pid, thread->pid, t->debug_id,
  1310. target_proc->pid, target_node->debug_id,
  1311. (u64)tr->data.ptr.buffer,
  1312. (u64)tr->data.ptr.offsets,
  1313. (u64)tr->data_size, (u64)tr->offsets_size);
  1314. if (!reply && !(tr->flags & TF_ONE_WAY))
  1315. t->from = thread;
  1316. else
  1317. t->from = NULL;
  1318. t->sender_euid = task_euid(proc->tsk);
  1319. t->to_proc = target_proc;
  1320. t->to_thread = target_thread;
  1321. t->code = tr->code;
  1322. t->flags = tr->flags;
  1323. t->priority = task_nice(current);
  1324. trace_binder_transaction(reply, t, target_node);
  1325. t->buffer = binder_alloc_buf(target_proc, tr->data_size,
  1326. tr->offsets_size, !reply && (t->flags & TF_ONE_WAY));
  1327. if (t->buffer == NULL) {
  1328. return_error = BR_FAILED_REPLY;
  1329. goto err_binder_alloc_buf_failed;
  1330. }
  1331. t->buffer->allow_user_free = 0;
  1332. t->buffer->debug_id = t->debug_id;
  1333. t->buffer->transaction = t;
  1334. t->buffer->target_node = target_node;
  1335. trace_binder_transaction_alloc_buf(t->buffer);
  1336. if (target_node)
  1337. binder_inc_node(target_node, 1, 0, NULL);
  1338. offp = (binder_size_t *)(t->buffer->data +
  1339. ALIGN(tr->data_size, sizeof(void *)));
  1340. if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
  1341. tr->data.ptr.buffer, tr->data_size)) {
  1342. binder_user_error("%d:%d got transaction with invalid data ptr\n",
  1343. proc->pid, thread->pid);
  1344. return_error = BR_FAILED_REPLY;
  1345. goto err_copy_data_failed;
  1346. }
  1347. if (copy_from_user(offp, (const void __user *)(uintptr_t)
  1348. tr->data.ptr.offsets, tr->offsets_size)) {
  1349. binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
  1350. proc->pid, thread->pid);
  1351. return_error = BR_FAILED_REPLY;
  1352. goto err_copy_data_failed;
  1353. }
  1354. if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
  1355. binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
  1356. proc->pid, thread->pid, (u64)tr->offsets_size);
  1357. return_error = BR_FAILED_REPLY;
  1358. goto err_bad_offset;
  1359. }
  1360. off_end = (void *)offp + tr->offsets_size;
  1361. off_min = 0;
  1362. for (; offp < off_end; offp++) {
  1363. struct flat_binder_object *fp;
  1364. if (*offp > t->buffer->data_size - sizeof(*fp) ||
  1365. *offp < off_min ||
  1366. t->buffer->data_size < sizeof(*fp) ||
  1367. !IS_ALIGNED(*offp, sizeof(u32))) {
  1368. binder_user_error("%d:%d got transaction with invalid offset, %lld (min %lld, max %lld)\n",
  1369. proc->pid, thread->pid, (u64)*offp,
  1370. (u64)off_min,
  1371. (u64)(t->buffer->data_size -
  1372. sizeof(*fp)));
  1373. return_error = BR_FAILED_REPLY;
  1374. goto err_bad_offset;
  1375. }
  1376. fp = (struct flat_binder_object *)(t->buffer->data + *offp);
  1377. off_min = *offp + sizeof(struct flat_binder_object);
  1378. switch (fp->type) {
  1379. case BINDER_TYPE_BINDER:
  1380. case BINDER_TYPE_WEAK_BINDER: {
  1381. struct binder_ref *ref;
  1382. struct binder_node *node = binder_get_node(proc, fp->binder);
  1383. if (node == NULL) {
  1384. node = binder_new_node(proc, fp->binder, fp->cookie);
  1385. if (node == NULL) {
  1386. return_error = BR_FAILED_REPLY;
  1387. goto err_binder_new_node_failed;
  1388. }
  1389. node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
  1390. node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
  1391. }
  1392. if (fp->cookie != node->cookie) {
  1393. binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
  1394. proc->pid, thread->pid,
  1395. (u64)fp->binder, node->debug_id,
  1396. (u64)fp->cookie, (u64)node->cookie);
  1397. return_error = BR_FAILED_REPLY;
  1398. goto err_binder_get_ref_for_node_failed;
  1399. }
  1400. if (security_binder_transfer_binder(proc->tsk,
  1401. target_proc->tsk)) {
  1402. return_error = BR_FAILED_REPLY;
  1403. goto err_binder_get_ref_for_node_failed;
  1404. }
  1405. ref = binder_get_ref_for_node(target_proc, node);
  1406. if (ref == NULL) {
  1407. return_error = BR_FAILED_REPLY;
  1408. goto err_binder_get_ref_for_node_failed;
  1409. }
  1410. if (fp->type == BINDER_TYPE_BINDER)
  1411. fp->type = BINDER_TYPE_HANDLE;
  1412. else
  1413. fp->type = BINDER_TYPE_WEAK_HANDLE;
  1414. fp->binder = 0;
  1415. fp->handle = ref->desc;
  1416. fp->cookie = 0;
  1417. binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE,
  1418. &thread->todo);
  1419. trace_binder_transaction_node_to_ref(t, node, ref);
  1420. binder_debug(BINDER_DEBUG_TRANSACTION,
  1421. " node %d u%016llx -> ref %d desc %d\n",
  1422. node->debug_id, (u64)node->ptr,
  1423. ref->debug_id, ref->desc);
  1424. } break;
  1425. case BINDER_TYPE_HANDLE:
  1426. case BINDER_TYPE_WEAK_HANDLE: {
  1427. struct binder_ref *ref;
  1428. ref = binder_get_ref(proc, fp->handle,
  1429. fp->type == BINDER_TYPE_HANDLE);
  1430. if (ref == NULL) {
  1431. binder_user_error("%d:%d got transaction with invalid handle, %d\n",
  1432. proc->pid,
  1433. thread->pid, fp->handle);
  1434. return_error = BR_FAILED_REPLY;
  1435. goto err_binder_get_ref_failed;
  1436. }
  1437. if (security_binder_transfer_binder(proc->tsk,
  1438. target_proc->tsk)) {
  1439. return_error = BR_FAILED_REPLY;
  1440. goto err_binder_get_ref_failed;
  1441. }
  1442. if (ref->node->proc == target_proc) {
  1443. if (fp->type == BINDER_TYPE_HANDLE)
  1444. fp->type = BINDER_TYPE_BINDER;
  1445. else
  1446. fp->type = BINDER_TYPE_WEAK_BINDER;
  1447. fp->binder = ref->node->ptr;
  1448. fp->cookie = ref->node->cookie;
  1449. binder_inc_node(ref->node, fp->type == BINDER_TYPE_BINDER, 0, NULL);
  1450. trace_binder_transaction_ref_to_node(t, ref);
  1451. binder_debug(BINDER_DEBUG_TRANSACTION,
  1452. " ref %d desc %d -> node %d u%016llx\n",
  1453. ref->debug_id, ref->desc, ref->node->debug_id,
  1454. (u64)ref->node->ptr);
  1455. } else {
  1456. struct binder_ref *new_ref;
  1457. new_ref = binder_get_ref_for_node(target_proc, ref->node);
  1458. if (new_ref == NULL) {
  1459. return_error = BR_FAILED_REPLY;
  1460. goto err_binder_get_ref_for_node_failed;
  1461. }
  1462. fp->binder = 0;
  1463. fp->handle = new_ref->desc;
  1464. fp->cookie = 0;
  1465. binder_inc_ref(new_ref, fp->type == BINDER_TYPE_HANDLE, NULL);
  1466. trace_binder_transaction_ref_to_ref(t, ref,
  1467. new_ref);
  1468. binder_debug(BINDER_DEBUG_TRANSACTION,
  1469. " ref %d desc %d -> ref %d desc %d (node %d)\n",
  1470. ref->debug_id, ref->desc, new_ref->debug_id,
  1471. new_ref->desc, ref->node->debug_id);
  1472. }
  1473. } break;
  1474. case BINDER_TYPE_FD: {
  1475. int target_fd;
  1476. struct file *file;
  1477. if (reply) {
  1478. if (!(in_reply_to->flags & TF_ACCEPT_FDS)) {
  1479. binder_user_error("%d:%d got reply with fd, %d, but target does not allow fds\n",
  1480. proc->pid, thread->pid, fp->handle);
  1481. return_error = BR_FAILED_REPLY;
  1482. goto err_fd_not_allowed;
  1483. }
  1484. } else if (!target_node->accept_fds) {
  1485. binder_user_error("%d:%d got transaction with fd, %d, but target does not allow fds\n",
  1486. proc->pid, thread->pid, fp->handle);
  1487. return_error = BR_FAILED_REPLY;
  1488. goto err_fd_not_allowed;
  1489. }
  1490. file = fget(fp->handle);
  1491. if (file == NULL) {
  1492. binder_user_error("%d:%d got transaction with invalid fd, %d\n",
  1493. proc->pid, thread->pid, fp->handle);
  1494. return_error = BR_FAILED_REPLY;
  1495. goto err_fget_failed;
  1496. }
  1497. if (security_binder_transfer_file(proc->tsk,
  1498. target_proc->tsk,
  1499. file) < 0) {
  1500. fput(file);
  1501. return_error = BR_FAILED_REPLY;
  1502. goto err_get_unused_fd_failed;
  1503. }
  1504. target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
  1505. if (target_fd < 0) {
  1506. fput(file);
  1507. return_error = BR_FAILED_REPLY;
  1508. goto err_get_unused_fd_failed;
  1509. }
  1510. task_fd_install(target_proc, target_fd, file);
  1511. trace_binder_transaction_fd(t, fp->handle, target_fd);
  1512. binder_debug(BINDER_DEBUG_TRANSACTION,
  1513. " fd %d -> %d\n", fp->handle, target_fd);
  1514. /* TODO: fput? */
  1515. fp->binder = 0;
  1516. fp->handle = target_fd;
  1517. } break;
  1518. default:
  1519. binder_user_error("%d:%d got transaction with invalid object type, %x\n",
  1520. proc->pid, thread->pid, fp->type);
  1521. return_error = BR_FAILED_REPLY;
  1522. goto err_bad_object_type;
  1523. }
  1524. }
  1525. if (reply) {
  1526. BUG_ON(t->buffer->async_transaction != 0);
  1527. binder_pop_transaction(target_thread, in_reply_to);
  1528. } else if (!(t->flags & TF_ONE_WAY)) {
  1529. BUG_ON(t->buffer->async_transaction != 0);
  1530. t->need_reply = 1;
  1531. t->from_parent = thread->transaction_stack;
  1532. thread->transaction_stack = t;
  1533. } else {
  1534. BUG_ON(target_node == NULL);
  1535. BUG_ON(t->buffer->async_transaction != 1);
  1536. if (target_node->has_async_transaction) {
  1537. target_list = &target_node->async_todo;
  1538. target_wait = NULL;
  1539. } else
  1540. target_node->has_async_transaction = 1;
  1541. }
  1542. t->work.type = BINDER_WORK_TRANSACTION;
  1543. list_add_tail(&t->work.entry, target_list);
  1544. tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
  1545. list_add_tail(&tcomplete->entry, &thread->todo);
  1546. if (target_wait) {
  1547. if (reply || !(t->flags & TF_ONE_WAY))
  1548. wake_up_interruptible_sync(target_wait);
  1549. else
  1550. wake_up_interruptible(target_wait);
  1551. }
  1552. return;
  1553. err_get_unused_fd_failed:
  1554. err_fget_failed:
  1555. err_fd_not_allowed:
  1556. err_binder_get_ref_for_node_failed:
  1557. err_binder_get_ref_failed:
  1558. err_binder_new_node_failed:
  1559. err_bad_object_type:
  1560. err_bad_offset:
  1561. err_copy_data_failed:
  1562. trace_binder_transaction_failed_buffer_release(t->buffer);
  1563. binder_transaction_buffer_release(target_proc, t->buffer, offp);
  1564. t->buffer->transaction = NULL;
  1565. binder_free_buf(target_proc, t->buffer);
  1566. err_binder_alloc_buf_failed:
  1567. kfree(tcomplete);
  1568. binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
  1569. err_alloc_tcomplete_failed:
  1570. kfree(t);
  1571. binder_stats_deleted(BINDER_STAT_TRANSACTION);
  1572. err_alloc_t_failed:
  1573. err_bad_call_stack:
  1574. err_empty_call_stack:
  1575. err_dead_binder:
  1576. err_invalid_target_handle:
  1577. err_no_context_mgr_node:
  1578. binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
  1579. "%d:%d transaction failed %d, size %lld-%lld\n",
  1580. proc->pid, thread->pid, return_error,
  1581. (u64)tr->data_size, (u64)tr->offsets_size);
  1582. {
  1583. struct binder_transaction_log_entry *fe;
  1584. fe = binder_transaction_log_add(&binder_transaction_log_failed);
  1585. *fe = *e;
  1586. }
  1587. BUG_ON(thread->return_error != BR_OK);
  1588. if (in_reply_to) {
  1589. thread->return_error = BR_TRANSACTION_COMPLETE;
  1590. binder_send_failed_reply(in_reply_to, return_error);
  1591. } else
  1592. thread->return_error = return_error;
  1593. }
  1594. static int binder_thread_write(struct binder_proc *proc,
  1595. struct binder_thread *thread,
  1596. binder_uintptr_t binder_buffer, size_t size,
  1597. binder_size_t *consumed)
  1598. {
  1599. uint32_t cmd;
  1600. void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
  1601. void __user *ptr = buffer + *consumed;
  1602. void __user *end = buffer + size;
  1603. while (ptr < end && thread->return_error == BR_OK) {
  1604. if (get_user(cmd, (uint32_t __user *)ptr))
  1605. return -EFAULT;
  1606. ptr += sizeof(uint32_t);
  1607. trace_binder_command(cmd);
  1608. if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
  1609. binder_stats.bc[_IOC_NR(cmd)]++;
  1610. proc->stats.bc[_IOC_NR(cmd)]++;
  1611. thread->stats.bc[_IOC_NR(cmd)]++;
  1612. }
  1613. switch (cmd) {
  1614. case BC_INCREFS:
  1615. case BC_ACQUIRE:
  1616. case BC_RELEASE:
  1617. case BC_DECREFS: {
  1618. uint32_t target;
  1619. struct binder_ref *ref;
  1620. const char *debug_string;
  1621. if (get_user(target, (uint32_t __user *)ptr))
  1622. return -EFAULT;
  1623. ptr += sizeof(uint32_t);
  1624. if (target == 0 && binder_context_mgr_node &&
  1625. (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
  1626. ref = binder_get_ref_for_node(proc,
  1627. binder_context_mgr_node);
  1628. if (ref->desc != target) {
  1629. binder_user_error("%d:%d tried to acquire reference to desc 0, got %d instead\n",
  1630. proc->pid, thread->pid,
  1631. ref->desc);
  1632. }
  1633. } else
  1634. ref = binder_get_ref(proc, target,
  1635. cmd == BC_ACQUIRE ||
  1636. cmd == BC_RELEASE);
  1637. if (ref == NULL) {
  1638. binder_user_error("%d:%d refcount change on invalid ref %d\n",
  1639. proc->pid, thread->pid, target);
  1640. break;
  1641. }
  1642. switch (cmd) {
  1643. case BC_INCREFS:
  1644. debug_string = "IncRefs";
  1645. binder_inc_ref(ref, 0, NULL);
  1646. break;
  1647. case BC_ACQUIRE:
  1648. debug_string = "Acquire";
  1649. binder_inc_ref(ref, 1, NULL);
  1650. break;
  1651. case BC_RELEASE:
  1652. debug_string = "Release";
  1653. binder_dec_ref(ref, 1);
  1654. break;
  1655. case BC_DECREFS:
  1656. default:
  1657. debug_string = "DecRefs";
  1658. binder_dec_ref(ref, 0);
  1659. break;
  1660. }
  1661. binder_debug(BINDER_DEBUG_USER_REFS,
  1662. "%d:%d %s ref %d desc %d s %d w %d for node %d\n",
  1663. proc->pid, thread->pid, debug_string, ref->debug_id,
  1664. ref->desc, ref->strong, ref->weak, ref->node->debug_id);
  1665. break;
  1666. }
  1667. case BC_INCREFS_DONE:
  1668. case BC_ACQUIRE_DONE: {
  1669. binder_uintptr_t node_ptr;
  1670. binder_uintptr_t cookie;
  1671. struct binder_node *node;
  1672. if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
  1673. return -EFAULT;
  1674. ptr += sizeof(binder_uintptr_t);
  1675. if (get_user(cookie, (binder_uintptr_t __user *)ptr))
  1676. return -EFAULT;
  1677. ptr += sizeof(binder_uintptr_t);
  1678. node = binder_get_node(proc, node_ptr);
  1679. if (node == NULL) {
  1680. binder_user_error("%d:%d %s u%016llx no match\n",
  1681. proc->pid, thread->pid,
  1682. cmd == BC_INCREFS_DONE ?
  1683. "BC_INCREFS_DONE" :
  1684. "BC_ACQUIRE_DONE",
  1685. (u64)node_ptr);
  1686. break;
  1687. }
  1688. if (cookie != node->cookie) {
  1689. binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
  1690. proc->pid, thread->pid,
  1691. cmd == BC_INCREFS_DONE ?
  1692. "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
  1693. (u64)node_ptr, node->debug_id,
  1694. (u64)cookie, (u64)node->cookie);
  1695. break;
  1696. }
  1697. if (cmd == BC_ACQUIRE_DONE) {
  1698. if (node->pending_strong_ref == 0) {
  1699. binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
  1700. proc->pid, thread->pid,
  1701. node->debug_id);
  1702. break;
  1703. }
  1704. node->pending_strong_ref = 0;
  1705. } else {
  1706. if (node->pending_weak_ref == 0) {
  1707. binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
  1708. proc->pid, thread->pid,
  1709. node->debug_id);
  1710. break;
  1711. }
  1712. node->pending_weak_ref = 0;
  1713. }
  1714. binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
  1715. binder_debug(BINDER_DEBUG_USER_REFS,
  1716. "%d:%d %s node %d ls %d lw %d\n",
  1717. proc->pid, thread->pid,
  1718. cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
  1719. node->debug_id, node->local_strong_refs, node->local_weak_refs);
  1720. break;
  1721. }
  1722. case BC_ATTEMPT_ACQUIRE:
  1723. pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
  1724. return -EINVAL;
  1725. case BC_ACQUIRE_RESULT:
  1726. pr_err("BC_ACQUIRE_RESULT not supported\n");
  1727. return -EINVAL;
  1728. case BC_FREE_BUFFER: {
  1729. binder_uintptr_t data_ptr;
  1730. struct binder_buffer *buffer;
  1731. if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
  1732. return -EFAULT;
  1733. ptr += sizeof(binder_uintptr_t);
  1734. buffer = binder_buffer_lookup(proc, data_ptr);
  1735. if (buffer == NULL) {
  1736. binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
  1737. proc->pid, thread->pid, (u64)data_ptr);
  1738. break;
  1739. }
  1740. if (!buffer->allow_user_free) {
  1741. binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
  1742. proc->pid, thread->pid, (u64)data_ptr);
  1743. break;
  1744. }
  1745. binder_debug(BINDER_DEBUG_FREE_BUFFER,
  1746. "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
  1747. proc->pid, thread->pid, (u64)data_ptr,
  1748. buffer->debug_id,
  1749. buffer->transaction ? "active" : "finished");
  1750. if (buffer->transaction) {
  1751. buffer->transaction->buffer = NULL;
  1752. buffer->transaction = NULL;
  1753. }
  1754. if (buffer->async_transaction && buffer->target_node) {
  1755. BUG_ON(!buffer->target_node->has_async_transaction);
  1756. if (list_empty(&buffer->target_node->async_todo))
  1757. buffer->target_node->has_async_transaction = 0;
  1758. else
  1759. list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
  1760. }
  1761. trace_binder_transaction_buffer_release(buffer);
  1762. binder_transaction_buffer_release(proc, buffer, NULL);
  1763. binder_free_buf(proc, buffer);
  1764. break;
  1765. }
  1766. case BC_TRANSACTION:
  1767. case BC_REPLY: {
  1768. struct binder_transaction_data tr;
  1769. if (copy_from_user(&tr, ptr, sizeof(tr)))
  1770. return -EFAULT;
  1771. ptr += sizeof(tr);
  1772. binder_transaction(proc, thread, &tr, cmd == BC_REPLY);
  1773. break;
  1774. }
  1775. case BC_REGISTER_LOOPER:
  1776. binder_debug(BINDER_DEBUG_THREADS,
  1777. "%d:%d BC_REGISTER_LOOPER\n",
  1778. proc->pid, thread->pid);
  1779. if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
  1780. thread->looper |= BINDER_LOOPER_STATE_INVALID;
  1781. binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
  1782. proc->pid, thread->pid);
  1783. } else if (proc->requested_threads == 0) {
  1784. thread->looper |= BINDER_LOOPER_STATE_INVALID;
  1785. binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
  1786. proc->pid, thread->pid);
  1787. } else {
  1788. proc->requested_threads--;
  1789. proc->requested_threads_started++;
  1790. }
  1791. thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
  1792. break;
  1793. case BC_ENTER_LOOPER:
  1794. binder_debug(BINDER_DEBUG_THREADS,
  1795. "%d:%d BC_ENTER_LOOPER\n",
  1796. proc->pid, thread->pid);
  1797. if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
  1798. thread->looper |= BINDER_LOOPER_STATE_INVALID;
  1799. binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
  1800. proc->pid, thread->pid);
  1801. }
  1802. thread->looper |= BINDER_LOOPER_STATE_ENTERED;
  1803. break;
  1804. case BC_EXIT_LOOPER:
  1805. binder_debug(BINDER_DEBUG_THREADS,
  1806. "%d:%d BC_EXIT_LOOPER\n",
  1807. proc->pid, thread->pid);
  1808. thread->looper |= BINDER_LOOPER_STATE_EXITED;
  1809. break;
  1810. case BC_REQUEST_DEATH_NOTIFICATION:
  1811. case BC_CLEAR_DEATH_NOTIFICATION: {
  1812. uint32_t target;
  1813. binder_uintptr_t cookie;
  1814. struct binder_ref *ref;
  1815. struct binder_ref_death *death;
  1816. if (get_user(target, (uint32_t __user *)ptr))
  1817. return -EFAULT;
  1818. ptr += sizeof(uint32_t);
  1819. if (get_user(cookie, (binder_uintptr_t __user *)ptr))
  1820. return -EFAULT;
  1821. ptr += sizeof(binder_uintptr_t);
  1822. ref = binder_get_ref(proc, target, false);
  1823. if (ref == NULL) {
  1824. binder_user_error("%d:%d %s invalid ref %d\n",
  1825. proc->pid, thread->pid,
  1826. cmd == BC_REQUEST_DEATH_NOTIFICATION ?
  1827. "BC_REQUEST_DEATH_NOTIFICATION" :
  1828. "BC_CLEAR_DEATH_NOTIFICATION",
  1829. target);
  1830. break;
  1831. }
  1832. binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
  1833. "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
  1834. proc->pid, thread->pid,
  1835. cmd == BC_REQUEST_DEATH_NOTIFICATION ?
  1836. "BC_REQUEST_DEATH_NOTIFICATION" :
  1837. "BC_CLEAR_DEATH_NOTIFICATION",
  1838. (u64)cookie, ref->debug_id, ref->desc,
  1839. ref->strong, ref->weak, ref->node->debug_id);
  1840. if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
  1841. if (ref->death) {
  1842. binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
  1843. proc->pid, thread->pid);
  1844. break;
  1845. }
  1846. death = kzalloc(sizeof(*death), GFP_KERNEL);
  1847. if (death == NULL) {
  1848. thread->return_error = BR_ERROR;
  1849. binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
  1850. "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
  1851. proc->pid, thread->pid);
  1852. break;
  1853. }
  1854. binder_stats_created(BINDER_STAT_DEATH);
  1855. INIT_LIST_HEAD(&death->work.entry);
  1856. death->cookie = cookie;
  1857. ref->death = death;
  1858. if (ref->node->proc == NULL) {
  1859. ref->death->work.type = BINDER_WORK_DEAD_BINDER;
  1860. if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
  1861. list_add_tail(&ref->death->work.entry, &thread->todo);
  1862. } else {
  1863. list_add_tail(&ref->death->work.entry, &proc->todo);
  1864. wake_up_interruptible(&proc->wait);
  1865. }
  1866. }
  1867. } else {
  1868. if (ref->death == NULL) {
  1869. binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
  1870. proc->pid, thread->pid);
  1871. break;
  1872. }
  1873. death = ref->death;
  1874. if (death->cookie != cookie) {
  1875. binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
  1876. proc->pid, thread->pid,
  1877. (u64)death->cookie,
  1878. (u64)cookie);
  1879. break;
  1880. }
  1881. ref->death = NULL;
  1882. if (list_empty(&death->work.entry)) {
  1883. death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
  1884. if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
  1885. list_add_tail(&death->work.entry, &thread->todo);
  1886. } else {
  1887. list_add_tail(&death->work.entry, &proc->todo);
  1888. wake_up_interruptible(&proc->wait);
  1889. }
  1890. } else {
  1891. BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
  1892. death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
  1893. }
  1894. }
  1895. } break;
  1896. case BC_DEAD_BINDER_DONE: {
  1897. struct binder_work *w;
  1898. binder_uintptr_t cookie;
  1899. struct binder_ref_death *death = NULL;
  1900. if (get_user(cookie, (binder_uintptr_t __user *)ptr))
  1901. return -EFAULT;
  1902. ptr += sizeof(cookie);
  1903. list_for_each_entry(w, &proc->delivered_death, entry) {
  1904. struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
  1905. if (tmp_death->cookie == cookie) {
  1906. death = tmp_death;
  1907. break;
  1908. }
  1909. }
  1910. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  1911. "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
  1912. proc->pid, thread->pid, (u64)cookie,
  1913. death);
  1914. if (death == NULL) {
  1915. binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
  1916. proc->pid, thread->pid, (u64)cookie);
  1917. break;
  1918. }
  1919. list_del_init(&death->work.entry);
  1920. if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
  1921. death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
  1922. if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
  1923. list_add_tail(&death->work.entry, &thread->todo);
  1924. } else {
  1925. list_add_tail(&death->work.entry, &proc->todo);
  1926. wake_up_interruptible(&proc->wait);
  1927. }
  1928. }
  1929. } break;
  1930. default:
  1931. pr_err("%d:%d unknown command %d\n",
  1932. proc->pid, thread->pid, cmd);
  1933. return -EINVAL;
  1934. }
  1935. *consumed = ptr - buffer;
  1936. }
  1937. return 0;
  1938. }
  1939. static void binder_stat_br(struct binder_proc *proc,
  1940. struct binder_thread *thread, uint32_t cmd)
  1941. {
  1942. trace_binder_return(cmd);
  1943. if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
  1944. binder_stats.br[_IOC_NR(cmd)]++;
  1945. proc->stats.br[_IOC_NR(cmd)]++;
  1946. thread->stats.br[_IOC_NR(cmd)]++;
  1947. }
  1948. }
  1949. static int binder_has_proc_work(struct binder_proc *proc,
  1950. struct binder_thread *thread)
  1951. {
  1952. return !list_empty(&proc->todo) ||
  1953. (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
  1954. }
  1955. static int binder_has_thread_work(struct binder_thread *thread)
  1956. {
  1957. return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
  1958. (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
  1959. }
  1960. static int binder_thread_read(struct binder_proc *proc,
  1961. struct binder_thread *thread,
  1962. binder_uintptr_t binder_buffer, size_t size,
  1963. binder_size_t *consumed, int non_block)
  1964. {
  1965. void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
  1966. void __user *ptr = buffer + *consumed;
  1967. void __user *end = buffer + size;
  1968. int ret = 0;
  1969. int wait_for_proc_work;
  1970. if (*consumed == 0) {
  1971. if (put_user(BR_NOOP, (uint32_t __user *)ptr))
  1972. return -EFAULT;
  1973. ptr += sizeof(uint32_t);
  1974. }
  1975. retry:
  1976. wait_for_proc_work = thread->transaction_stack == NULL &&
  1977. list_empty(&thread->todo);
  1978. if (thread->return_error != BR_OK && ptr < end) {
  1979. if (thread->return_error2 != BR_OK) {
  1980. if (put_user(thread->return_error2, (uint32_t __user *)ptr))
  1981. return -EFAULT;
  1982. ptr += sizeof(uint32_t);
  1983. binder_stat_br(proc, thread, thread->return_error2);
  1984. if (ptr == end)
  1985. goto done;
  1986. thread->return_error2 = BR_OK;
  1987. }
  1988. if (put_user(thread->return_error, (uint32_t __user *)ptr))
  1989. return -EFAULT;
  1990. ptr += sizeof(uint32_t);
  1991. binder_stat_br(proc, thread, thread->return_error);
  1992. thread->return_error = BR_OK;
  1993. goto done;
  1994. }
  1995. thread->looper |= BINDER_LOOPER_STATE_WAITING;
  1996. if (wait_for_proc_work)
  1997. proc->ready_threads++;
  1998. binder_unlock(__func__);
  1999. trace_binder_wait_for_work(wait_for_proc_work,
  2000. !!thread->transaction_stack,
  2001. !list_empty(&thread->todo));
  2002. if (wait_for_proc_work) {
  2003. if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
  2004. BINDER_LOOPER_STATE_ENTERED))) {
  2005. binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
  2006. proc->pid, thread->pid, thread->looper);
  2007. wait_event_interruptible(binder_user_error_wait,
  2008. binder_stop_on_user_error < 2);
  2009. }
  2010. binder_set_nice(proc->default_priority);
  2011. if (non_block) {
  2012. if (!binder_has_proc_work(proc, thread))
  2013. ret = -EAGAIN;
  2014. } else
  2015. ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
  2016. } else {
  2017. if (non_block) {
  2018. if (!binder_has_thread_work(thread))
  2019. ret = -EAGAIN;
  2020. } else
  2021. ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
  2022. }
  2023. binder_lock(__func__);
  2024. if (wait_for_proc_work)
  2025. proc->ready_threads--;
  2026. thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
  2027. if (ret)
  2028. return ret;
  2029. while (1) {
  2030. uint32_t cmd;
  2031. struct binder_transaction_data tr;
  2032. struct binder_work *w;
  2033. struct binder_transaction *t = NULL;
  2034. if (!list_empty(&thread->todo)) {
  2035. w = list_first_entry(&thread->todo, struct binder_work,
  2036. entry);
  2037. } else if (!list_empty(&proc->todo) && wait_for_proc_work) {
  2038. w = list_first_entry(&proc->todo, struct binder_work,
  2039. entry);
  2040. } else {
  2041. /* no data added */
  2042. if (ptr - buffer == 4 &&
  2043. !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN))
  2044. goto retry;
  2045. break;
  2046. }
  2047. if (end - ptr < sizeof(tr) + 4)
  2048. break;
  2049. switch (w->type) {
  2050. case BINDER_WORK_TRANSACTION: {
  2051. t = container_of(w, struct binder_transaction, work);
  2052. } break;
  2053. case BINDER_WORK_TRANSACTION_COMPLETE: {
  2054. cmd = BR_TRANSACTION_COMPLETE;
  2055. if (put_user(cmd, (uint32_t __user *)ptr))
  2056. return -EFAULT;
  2057. ptr += sizeof(uint32_t);
  2058. binder_stat_br(proc, thread, cmd);
  2059. binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
  2060. "%d:%d BR_TRANSACTION_COMPLETE\n",
  2061. proc->pid, thread->pid);
  2062. list_del(&w->entry);
  2063. kfree(w);
  2064. binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
  2065. } break;
  2066. case BINDER_WORK_NODE: {
  2067. struct binder_node *node = container_of(w, struct binder_node, work);
  2068. uint32_t cmd = BR_NOOP;
  2069. const char *cmd_name;
  2070. int strong = node->internal_strong_refs || node->local_strong_refs;
  2071. int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong;
  2072. if (weak && !node->has_weak_ref) {
  2073. cmd = BR_INCREFS;
  2074. cmd_name = "BR_INCREFS";
  2075. node->has_weak_ref = 1;
  2076. node->pending_weak_ref = 1;
  2077. node->local_weak_refs++;
  2078. } else if (strong && !node->has_strong_ref) {
  2079. cmd = BR_ACQUIRE;
  2080. cmd_name = "BR_ACQUIRE";
  2081. node->has_strong_ref = 1;
  2082. node->pending_strong_ref = 1;
  2083. node->local_strong_refs++;
  2084. } else if (!strong && node->has_strong_ref) {
  2085. cmd = BR_RELEASE;
  2086. cmd_name = "BR_RELEASE";
  2087. node->has_strong_ref = 0;
  2088. } else if (!weak && node->has_weak_ref) {
  2089. cmd = BR_DECREFS;
  2090. cmd_name = "BR_DECREFS";
  2091. node->has_weak_ref = 0;
  2092. }
  2093. if (cmd != BR_NOOP) {
  2094. if (put_user(cmd, (uint32_t __user *)ptr))
  2095. return -EFAULT;
  2096. ptr += sizeof(uint32_t);
  2097. if (put_user(node->ptr,
  2098. (binder_uintptr_t __user *)ptr))
  2099. return -EFAULT;
  2100. ptr += sizeof(binder_uintptr_t);
  2101. if (put_user(node->cookie,
  2102. (binder_uintptr_t __user *)ptr))
  2103. return -EFAULT;
  2104. ptr += sizeof(binder_uintptr_t);
  2105. binder_stat_br(proc, thread, cmd);
  2106. binder_debug(BINDER_DEBUG_USER_REFS,
  2107. "%d:%d %s %d u%016llx c%016llx\n",
  2108. proc->pid, thread->pid, cmd_name,
  2109. node->debug_id,
  2110. (u64)node->ptr, (u64)node->cookie);
  2111. } else {
  2112. list_del_init(&w->entry);
  2113. if (!weak && !strong) {
  2114. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  2115. "%d:%d node %d u%016llx c%016llx deleted\n",
  2116. proc->pid, thread->pid,
  2117. node->debug_id,
  2118. (u64)node->ptr,
  2119. (u64)node->cookie);
  2120. rb_erase(&node->rb_node, &proc->nodes);
  2121. kfree(node);
  2122. binder_stats_deleted(BINDER_STAT_NODE);
  2123. } else {
  2124. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  2125. "%d:%d node %d u%016llx c%016llx state unchanged\n",
  2126. proc->pid, thread->pid,
  2127. node->debug_id,
  2128. (u64)node->ptr,
  2129. (u64)node->cookie);
  2130. }
  2131. }
  2132. } break;
  2133. case BINDER_WORK_DEAD_BINDER:
  2134. case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
  2135. case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
  2136. struct binder_ref_death *death;
  2137. uint32_t cmd;
  2138. death = container_of(w, struct binder_ref_death, work);
  2139. if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
  2140. cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
  2141. else
  2142. cmd = BR_DEAD_BINDER;
  2143. if (put_user(cmd, (uint32_t __user *)ptr))
  2144. return -EFAULT;
  2145. ptr += sizeof(uint32_t);
  2146. if (put_user(death->cookie,
  2147. (binder_uintptr_t __user *)ptr))
  2148. return -EFAULT;
  2149. ptr += sizeof(binder_uintptr_t);
  2150. binder_stat_br(proc, thread, cmd);
  2151. binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
  2152. "%d:%d %s %016llx\n",
  2153. proc->pid, thread->pid,
  2154. cmd == BR_DEAD_BINDER ?
  2155. "BR_DEAD_BINDER" :
  2156. "BR_CLEAR_DEATH_NOTIFICATION_DONE",
  2157. (u64)death->cookie);
  2158. if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
  2159. list_del(&w->entry);
  2160. kfree(death);
  2161. binder_stats_deleted(BINDER_STAT_DEATH);
  2162. } else
  2163. list_move(&w->entry, &proc->delivered_death);
  2164. if (cmd == BR_DEAD_BINDER)
  2165. goto done; /* DEAD_BINDER notifications can cause transactions */
  2166. } break;
  2167. }
  2168. if (!t)
  2169. continue;
  2170. BUG_ON(t->buffer == NULL);
  2171. if (t->buffer->target_node) {
  2172. struct binder_node *target_node = t->buffer->target_node;
  2173. tr.target.ptr = target_node->ptr;
  2174. tr.cookie = target_node->cookie;
  2175. t->saved_priority = task_nice(current);
  2176. if (t->priority < target_node->min_priority &&
  2177. !(t->flags & TF_ONE_WAY))
  2178. binder_set_nice(t->priority);
  2179. else if (!(t->flags & TF_ONE_WAY) ||
  2180. t->saved_priority > target_node->min_priority)
  2181. binder_set_nice(target_node->min_priority);
  2182. cmd = BR_TRANSACTION;
  2183. } else {
  2184. tr.target.ptr = 0;
  2185. tr.cookie = 0;
  2186. cmd = BR_REPLY;
  2187. }
  2188. tr.code = t->code;
  2189. tr.flags = t->flags;
  2190. tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
  2191. if (t->from) {
  2192. struct task_struct *sender = t->from->proc->tsk;
  2193. tr.sender_pid = task_tgid_nr_ns(sender,
  2194. task_active_pid_ns(current));
  2195. } else {
  2196. tr.sender_pid = 0;
  2197. }
  2198. tr.data_size = t->buffer->data_size;
  2199. tr.offsets_size = t->buffer->offsets_size;
  2200. tr.data.ptr.buffer = (binder_uintptr_t)(
  2201. (uintptr_t)t->buffer->data +
  2202. proc->user_buffer_offset);
  2203. tr.data.ptr.offsets = tr.data.ptr.buffer +
  2204. ALIGN(t->buffer->data_size,
  2205. sizeof(void *));
  2206. if (put_user(cmd, (uint32_t __user *)ptr))
  2207. return -EFAULT;
  2208. ptr += sizeof(uint32_t);
  2209. if (copy_to_user(ptr, &tr, sizeof(tr)))
  2210. return -EFAULT;
  2211. ptr += sizeof(tr);
  2212. trace_binder_transaction_received(t);
  2213. binder_stat_br(proc, thread, cmd);
  2214. binder_debug(BINDER_DEBUG_TRANSACTION,
  2215. "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
  2216. proc->pid, thread->pid,
  2217. (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
  2218. "BR_REPLY",
  2219. t->debug_id, t->from ? t->from->proc->pid : 0,
  2220. t->from ? t->from->pid : 0, cmd,
  2221. t->buffer->data_size, t->buffer->offsets_size,
  2222. (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
  2223. list_del(&t->work.entry);
  2224. t->buffer->allow_user_free = 1;
  2225. if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
  2226. t->to_parent = thread->transaction_stack;
  2227. t->to_thread = thread;
  2228. thread->transaction_stack = t;
  2229. } else {
  2230. t->buffer->transaction = NULL;
  2231. kfree(t);
  2232. binder_stats_deleted(BINDER_STAT_TRANSACTION);
  2233. }
  2234. break;
  2235. }
  2236. done:
  2237. *consumed = ptr - buffer;
  2238. if (proc->requested_threads + proc->ready_threads == 0 &&
  2239. proc->requested_threads_started < proc->max_threads &&
  2240. (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
  2241. BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
  2242. /*spawn a new thread if we leave this out */) {
  2243. proc->requested_threads++;
  2244. binder_debug(BINDER_DEBUG_THREADS,
  2245. "%d:%d BR_SPAWN_LOOPER\n",
  2246. proc->pid, thread->pid);
  2247. if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
  2248. return -EFAULT;
  2249. binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
  2250. }
  2251. return 0;
  2252. }
  2253. static void binder_release_work(struct list_head *list)
  2254. {
  2255. struct binder_work *w;
  2256. while (!list_empty(list)) {
  2257. w = list_first_entry(list, struct binder_work, entry);
  2258. list_del_init(&w->entry);
  2259. switch (w->type) {
  2260. case BINDER_WORK_TRANSACTION: {
  2261. struct binder_transaction *t;
  2262. t = container_of(w, struct binder_transaction, work);
  2263. if (t->buffer->target_node &&
  2264. !(t->flags & TF_ONE_WAY)) {
  2265. binder_send_failed_reply(t, BR_DEAD_REPLY);
  2266. } else {
  2267. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  2268. "undelivered transaction %d\n",
  2269. t->debug_id);
  2270. t->buffer->transaction = NULL;
  2271. kfree(t);
  2272. binder_stats_deleted(BINDER_STAT_TRANSACTION);
  2273. }
  2274. } break;
  2275. case BINDER_WORK_TRANSACTION_COMPLETE: {
  2276. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  2277. "undelivered TRANSACTION_COMPLETE\n");
  2278. kfree(w);
  2279. binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
  2280. } break;
  2281. case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
  2282. case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
  2283. struct binder_ref_death *death;
  2284. death = container_of(w, struct binder_ref_death, work);
  2285. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  2286. "undelivered death notification, %016llx\n",
  2287. (u64)death->cookie);
  2288. kfree(death);
  2289. binder_stats_deleted(BINDER_STAT_DEATH);
  2290. } break;
  2291. default:
  2292. pr_err("unexpected work type, %d, not freed\n",
  2293. w->type);
  2294. break;
  2295. }
  2296. }
  2297. }
  2298. static struct binder_thread *binder_get_thread(struct binder_proc *proc)
  2299. {
  2300. struct binder_thread *thread = NULL;
  2301. struct rb_node *parent = NULL;
  2302. struct rb_node **p = &proc->threads.rb_node;
  2303. while (*p) {
  2304. parent = *p;
  2305. thread = rb_entry(parent, struct binder_thread, rb_node);
  2306. if (current->pid < thread->pid)
  2307. p = &(*p)->rb_left;
  2308. else if (current->pid > thread->pid)
  2309. p = &(*p)->rb_right;
  2310. else
  2311. break;
  2312. }
  2313. if (*p == NULL) {
  2314. thread = kzalloc(sizeof(*thread), GFP_KERNEL);
  2315. if (thread == NULL)
  2316. return NULL;
  2317. binder_stats_created(BINDER_STAT_THREAD);
  2318. thread->proc = proc;
  2319. thread->pid = current->pid;
  2320. init_waitqueue_head(&thread->wait);
  2321. INIT_LIST_HEAD(&thread->todo);
  2322. rb_link_node(&thread->rb_node, parent, p);
  2323. rb_insert_color(&thread->rb_node, &proc->threads);
  2324. thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
  2325. thread->return_error = BR_OK;
  2326. thread->return_error2 = BR_OK;
  2327. }
  2328. return thread;
  2329. }
  2330. static int binder_free_thread(struct binder_proc *proc,
  2331. struct binder_thread *thread)
  2332. {
  2333. struct binder_transaction *t;
  2334. struct binder_transaction *send_reply = NULL;
  2335. int active_transactions = 0;
  2336. rb_erase(&thread->rb_node, &proc->threads);
  2337. t = thread->transaction_stack;
  2338. if (t && t->to_thread == thread)
  2339. send_reply = t;
  2340. while (t) {
  2341. active_transactions++;
  2342. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  2343. "release %d:%d transaction %d %s, still active\n",
  2344. proc->pid, thread->pid,
  2345. t->debug_id,
  2346. (t->to_thread == thread) ? "in" : "out");
  2347. if (t->to_thread == thread) {
  2348. t->to_proc = NULL;
  2349. t->to_thread = NULL;
  2350. if (t->buffer) {
  2351. t->buffer->transaction = NULL;
  2352. t->buffer = NULL;
  2353. }
  2354. t = t->to_parent;
  2355. } else if (t->from == thread) {
  2356. t->from = NULL;
  2357. t = t->from_parent;
  2358. } else
  2359. BUG();
  2360. }
  2361. if (send_reply)
  2362. binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
  2363. binder_release_work(&thread->todo);
  2364. kfree(thread);
  2365. binder_stats_deleted(BINDER_STAT_THREAD);
  2366. return active_transactions;
  2367. }
  2368. static unsigned int binder_poll(struct file *filp,
  2369. struct poll_table_struct *wait)
  2370. {
  2371. struct binder_proc *proc = filp->private_data;
  2372. struct binder_thread *thread = NULL;
  2373. int wait_for_proc_work;
  2374. binder_lock(__func__);
  2375. thread = binder_get_thread(proc);
  2376. if (!thread) {
  2377. binder_unlock(__func__);
  2378. return POLLERR;
  2379. }
  2380. wait_for_proc_work = thread->transaction_stack == NULL &&
  2381. list_empty(&thread->todo) && thread->return_error == BR_OK;
  2382. binder_unlock(__func__);
  2383. if (wait_for_proc_work) {
  2384. if (binder_has_proc_work(proc, thread))
  2385. return POLLIN;
  2386. poll_wait(filp, &proc->wait, wait);
  2387. if (binder_has_proc_work(proc, thread))
  2388. return POLLIN;
  2389. } else {
  2390. if (binder_has_thread_work(thread))
  2391. return POLLIN;
  2392. poll_wait(filp, &thread->wait, wait);
  2393. if (binder_has_thread_work(thread))
  2394. return POLLIN;
  2395. }
  2396. return 0;
  2397. }
  2398. static int binder_ioctl_write_read(struct file *filp,
  2399. unsigned int cmd, unsigned long arg,
  2400. struct binder_thread *thread)
  2401. {
  2402. int ret = 0;
  2403. struct binder_proc *proc = filp->private_data;
  2404. unsigned int size = _IOC_SIZE(cmd);
  2405. void __user *ubuf = (void __user *)arg;
  2406. struct binder_write_read bwr;
  2407. if (size != sizeof(struct binder_write_read)) {
  2408. ret = -EINVAL;
  2409. goto out;
  2410. }
  2411. if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
  2412. ret = -EFAULT;
  2413. goto out;
  2414. }
  2415. binder_debug(BINDER_DEBUG_READ_WRITE,
  2416. "%d:%d write %lld at %016llx, read %lld at %016llx\n",
  2417. proc->pid, thread->pid,
  2418. (u64)bwr.write_size, (u64)bwr.write_buffer,
  2419. (u64)bwr.read_size, (u64)bwr.read_buffer);
  2420. if (bwr.write_size > 0) {
  2421. ret = binder_thread_write(proc, thread,
  2422. bwr.write_buffer,
  2423. bwr.write_size,
  2424. &bwr.write_consumed);
  2425. trace_binder_write_done(ret);
  2426. if (ret < 0) {
  2427. bwr.read_consumed = 0;
  2428. if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
  2429. ret = -EFAULT;
  2430. goto out;
  2431. }
  2432. }
  2433. if (bwr.read_size > 0) {
  2434. ret = binder_thread_read(proc, thread, bwr.read_buffer,
  2435. bwr.read_size,
  2436. &bwr.read_consumed,
  2437. filp->f_flags & O_NONBLOCK);
  2438. trace_binder_read_done(ret);
  2439. if (!list_empty(&proc->todo))
  2440. wake_up_interruptible(&proc->wait);
  2441. if (ret < 0) {
  2442. if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
  2443. ret = -EFAULT;
  2444. goto out;
  2445. }
  2446. }
  2447. binder_debug(BINDER_DEBUG_READ_WRITE,
  2448. "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
  2449. proc->pid, thread->pid,
  2450. (u64)bwr.write_consumed, (u64)bwr.write_size,
  2451. (u64)bwr.read_consumed, (u64)bwr.read_size);
  2452. if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
  2453. ret = -EFAULT;
  2454. goto out;
  2455. }
  2456. out:
  2457. return ret;
  2458. }
  2459. static int binder_ioctl_set_ctx_mgr(struct file *filp)
  2460. {
  2461. int ret = 0;
  2462. struct binder_proc *proc = filp->private_data;
  2463. kuid_t curr_euid = current_euid();
  2464. if (binder_context_mgr_node != NULL) {
  2465. pr_err("BINDER_SET_CONTEXT_MGR already set\n");
  2466. ret = -EBUSY;
  2467. goto out;
  2468. }
  2469. ret = security_binder_set_context_mgr(proc->tsk);
  2470. if (ret < 0)
  2471. goto out;
  2472. if (uid_valid(binder_context_mgr_uid)) {
  2473. if (!uid_eq(binder_context_mgr_uid, curr_euid)) {
  2474. pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
  2475. from_kuid(&init_user_ns, curr_euid),
  2476. from_kuid(&init_user_ns,
  2477. binder_context_mgr_uid));
  2478. ret = -EPERM;
  2479. goto out;
  2480. }
  2481. } else {
  2482. binder_context_mgr_uid = curr_euid;
  2483. }
  2484. binder_context_mgr_node = binder_new_node(proc, 0, 0);
  2485. if (binder_context_mgr_node == NULL) {
  2486. ret = -ENOMEM;
  2487. goto out;
  2488. }
  2489. binder_context_mgr_node->local_weak_refs++;
  2490. binder_context_mgr_node->local_strong_refs++;
  2491. binder_context_mgr_node->has_strong_ref = 1;
  2492. binder_context_mgr_node->has_weak_ref = 1;
  2493. out:
  2494. return ret;
  2495. }
  2496. static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  2497. {
  2498. int ret;
  2499. struct binder_proc *proc = filp->private_data;
  2500. struct binder_thread *thread;
  2501. unsigned int size = _IOC_SIZE(cmd);
  2502. void __user *ubuf = (void __user *)arg;
  2503. /*pr_info("binder_ioctl: %d:%d %x %lx\n",
  2504. proc->pid, current->pid, cmd, arg);*/
  2505. trace_binder_ioctl(cmd, arg);
  2506. ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
  2507. if (ret)
  2508. goto err_unlocked;
  2509. binder_lock(__func__);
  2510. thread = binder_get_thread(proc);
  2511. if (thread == NULL) {
  2512. ret = -ENOMEM;
  2513. goto err;
  2514. }
  2515. switch (cmd) {
  2516. case BINDER_WRITE_READ:
  2517. ret = binder_ioctl_write_read(filp, cmd, arg, thread);
  2518. if (ret)
  2519. goto err;
  2520. break;
  2521. case BINDER_SET_MAX_THREADS:
  2522. if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
  2523. ret = -EINVAL;
  2524. goto err;
  2525. }
  2526. break;
  2527. case BINDER_SET_CONTEXT_MGR:
  2528. ret = binder_ioctl_set_ctx_mgr(filp);
  2529. if (ret)
  2530. goto err;
  2531. break;
  2532. case BINDER_THREAD_EXIT:
  2533. binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
  2534. proc->pid, thread->pid);
  2535. binder_free_thread(proc, thread);
  2536. thread = NULL;
  2537. break;
  2538. case BINDER_VERSION: {
  2539. struct binder_version __user *ver = ubuf;
  2540. if (size != sizeof(struct binder_version)) {
  2541. ret = -EINVAL;
  2542. goto err;
  2543. }
  2544. if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
  2545. &ver->protocol_version)) {
  2546. ret = -EINVAL;
  2547. goto err;
  2548. }
  2549. break;
  2550. }
  2551. default:
  2552. ret = -EINVAL;
  2553. goto err;
  2554. }
  2555. ret = 0;
  2556. err:
  2557. if (thread)
  2558. thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
  2559. binder_unlock(__func__);
  2560. wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
  2561. if (ret && ret != -ERESTARTSYS)
  2562. pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
  2563. err_unlocked:
  2564. trace_binder_ioctl_done(ret);
  2565. return ret;
  2566. }
  2567. static void binder_vma_open(struct vm_area_struct *vma)
  2568. {
  2569. struct binder_proc *proc = vma->vm_private_data;
  2570. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  2571. "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
  2572. proc->pid, vma->vm_start, vma->vm_end,
  2573. (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
  2574. (unsigned long)pgprot_val(vma->vm_page_prot));
  2575. }
  2576. static void binder_vma_close(struct vm_area_struct *vma)
  2577. {
  2578. struct binder_proc *proc = vma->vm_private_data;
  2579. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  2580. "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
  2581. proc->pid, vma->vm_start, vma->vm_end,
  2582. (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
  2583. (unsigned long)pgprot_val(vma->vm_page_prot));
  2584. proc->vma = NULL;
  2585. proc->vma_vm_mm = NULL;
  2586. binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
  2587. }
  2588. static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  2589. {
  2590. return VM_FAULT_SIGBUS;
  2591. }
  2592. static const struct vm_operations_struct binder_vm_ops = {
  2593. .open = binder_vma_open,
  2594. .close = binder_vma_close,
  2595. .fault = binder_vm_fault,
  2596. };
  2597. static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
  2598. {
  2599. int ret;
  2600. struct vm_struct *area;
  2601. struct binder_proc *proc = filp->private_data;
  2602. const char *failure_string;
  2603. struct binder_buffer *buffer;
  2604. if (proc->tsk != current->group_leader)
  2605. return -EINVAL;
  2606. if ((vma->vm_end - vma->vm_start) > SZ_4M)
  2607. vma->vm_end = vma->vm_start + SZ_4M;
  2608. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  2609. "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
  2610. proc->pid, vma->vm_start, vma->vm_end,
  2611. (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
  2612. (unsigned long)pgprot_val(vma->vm_page_prot));
  2613. if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
  2614. ret = -EPERM;
  2615. failure_string = "bad vm_flags";
  2616. goto err_bad_arg;
  2617. }
  2618. vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
  2619. mutex_lock(&binder_mmap_lock);
  2620. if (proc->buffer) {
  2621. ret = -EBUSY;
  2622. failure_string = "already mapped";
  2623. goto err_already_mapped;
  2624. }
  2625. area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
  2626. if (area == NULL) {
  2627. ret = -ENOMEM;
  2628. failure_string = "get_vm_area";
  2629. goto err_get_vm_area_failed;
  2630. }
  2631. proc->buffer = area->addr;
  2632. proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer;
  2633. mutex_unlock(&binder_mmap_lock);
  2634. #ifdef CONFIG_CPU_CACHE_VIPT
  2635. if (cache_is_vipt_aliasing()) {
  2636. while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) {
  2637. pr_info("binder_mmap: %d %lx-%lx maps %p bad alignment\n", proc->pid, vma->vm_start, vma->vm_end, proc->buffer);
  2638. vma->vm_start += PAGE_SIZE;
  2639. }
  2640. }
  2641. #endif
  2642. proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL);
  2643. if (proc->pages == NULL) {
  2644. ret = -ENOMEM;
  2645. failure_string = "alloc page array";
  2646. goto err_alloc_pages_failed;
  2647. }
  2648. proc->buffer_size = vma->vm_end - vma->vm_start;
  2649. vma->vm_ops = &binder_vm_ops;
  2650. vma->vm_private_data = proc;
  2651. if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) {
  2652. ret = -ENOMEM;
  2653. failure_string = "alloc small buf";
  2654. goto err_alloc_small_buf_failed;
  2655. }
  2656. buffer = proc->buffer;
  2657. INIT_LIST_HEAD(&proc->buffers);
  2658. list_add(&buffer->entry, &proc->buffers);
  2659. buffer->free = 1;
  2660. binder_insert_free_buffer(proc, buffer);
  2661. proc->free_async_space = proc->buffer_size / 2;
  2662. barrier();
  2663. proc->files = get_files_struct(current);
  2664. proc->vma = vma;
  2665. proc->vma_vm_mm = vma->vm_mm;
  2666. /*pr_info("binder_mmap: %d %lx-%lx maps %p\n",
  2667. proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
  2668. return 0;
  2669. err_alloc_small_buf_failed:
  2670. kfree(proc->pages);
  2671. proc->pages = NULL;
  2672. err_alloc_pages_failed:
  2673. mutex_lock(&binder_mmap_lock);
  2674. vfree(proc->buffer);
  2675. proc->buffer = NULL;
  2676. err_get_vm_area_failed:
  2677. err_already_mapped:
  2678. mutex_unlock(&binder_mmap_lock);
  2679. err_bad_arg:
  2680. pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
  2681. proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
  2682. return ret;
  2683. }
  2684. static int binder_open(struct inode *nodp, struct file *filp)
  2685. {
  2686. struct binder_proc *proc;
  2687. binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
  2688. current->group_leader->pid, current->pid);
  2689. proc = kzalloc(sizeof(*proc), GFP_KERNEL);
  2690. if (proc == NULL)
  2691. return -ENOMEM;
  2692. get_task_struct(current->group_leader);
  2693. proc->tsk = current->group_leader;
  2694. INIT_LIST_HEAD(&proc->todo);
  2695. init_waitqueue_head(&proc->wait);
  2696. proc->default_priority = task_nice(current);
  2697. binder_lock(__func__);
  2698. binder_stats_created(BINDER_STAT_PROC);
  2699. hlist_add_head(&proc->proc_node, &binder_procs);
  2700. proc->pid = current->group_leader->pid;
  2701. INIT_LIST_HEAD(&proc->delivered_death);
  2702. filp->private_data = proc;
  2703. binder_unlock(__func__);
  2704. if (binder_debugfs_dir_entry_proc) {
  2705. char strbuf[11];
  2706. snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
  2707. proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
  2708. binder_debugfs_dir_entry_proc, proc, &binder_proc_fops);
  2709. }
  2710. return 0;
  2711. }
  2712. static int binder_flush(struct file *filp, fl_owner_t id)
  2713. {
  2714. struct binder_proc *proc = filp->private_data;
  2715. binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
  2716. return 0;
  2717. }
  2718. static void binder_deferred_flush(struct binder_proc *proc)
  2719. {
  2720. struct rb_node *n;
  2721. int wake_count = 0;
  2722. for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
  2723. struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
  2724. thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
  2725. if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
  2726. wake_up_interruptible(&thread->wait);
  2727. wake_count++;
  2728. }
  2729. }
  2730. wake_up_interruptible_all(&proc->wait);
  2731. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  2732. "binder_flush: %d woke %d threads\n", proc->pid,
  2733. wake_count);
  2734. }
  2735. static int binder_release(struct inode *nodp, struct file *filp)
  2736. {
  2737. struct binder_proc *proc = filp->private_data;
  2738. debugfs_remove(proc->debugfs_entry);
  2739. binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
  2740. return 0;
  2741. }
  2742. static int binder_node_release(struct binder_node *node, int refs)
  2743. {
  2744. struct binder_ref *ref;
  2745. int death = 0;
  2746. list_del_init(&node->work.entry);
  2747. binder_release_work(&node->async_todo);
  2748. if (hlist_empty(&node->refs)) {
  2749. kfree(node);
  2750. binder_stats_deleted(BINDER_STAT_NODE);
  2751. return refs;
  2752. }
  2753. node->proc = NULL;
  2754. node->local_strong_refs = 0;
  2755. node->local_weak_refs = 0;
  2756. hlist_add_head(&node->dead_node, &binder_dead_nodes);
  2757. hlist_for_each_entry(ref, &node->refs, node_entry) {
  2758. refs++;
  2759. if (!ref->death)
  2760. continue;
  2761. death++;
  2762. if (list_empty(&ref->death->work.entry)) {
  2763. ref->death->work.type = BINDER_WORK_DEAD_BINDER;
  2764. list_add_tail(&ref->death->work.entry,
  2765. &ref->proc->todo);
  2766. wake_up_interruptible(&ref->proc->wait);
  2767. } else
  2768. BUG();
  2769. }
  2770. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  2771. "node %d now dead, refs %d, death %d\n",
  2772. node->debug_id, refs, death);
  2773. return refs;
  2774. }
  2775. static void binder_deferred_release(struct binder_proc *proc)
  2776. {
  2777. struct binder_transaction *t;
  2778. struct rb_node *n;
  2779. int threads, nodes, incoming_refs, outgoing_refs, buffers,
  2780. active_transactions, page_count;
  2781. BUG_ON(proc->vma);
  2782. BUG_ON(proc->files);
  2783. hlist_del(&proc->proc_node);
  2784. if (binder_context_mgr_node && binder_context_mgr_node->proc == proc) {
  2785. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  2786. "%s: %d context_mgr_node gone\n",
  2787. __func__, proc->pid);
  2788. binder_context_mgr_node = NULL;
  2789. }
  2790. threads = 0;
  2791. active_transactions = 0;
  2792. while ((n = rb_first(&proc->threads))) {
  2793. struct binder_thread *thread;
  2794. thread = rb_entry(n, struct binder_thread, rb_node);
  2795. threads++;
  2796. active_transactions += binder_free_thread(proc, thread);
  2797. }
  2798. nodes = 0;
  2799. incoming_refs = 0;
  2800. while ((n = rb_first(&proc->nodes))) {
  2801. struct binder_node *node;
  2802. node = rb_entry(n, struct binder_node, rb_node);
  2803. nodes++;
  2804. rb_erase(&node->rb_node, &proc->nodes);
  2805. incoming_refs = binder_node_release(node, incoming_refs);
  2806. }
  2807. outgoing_refs = 0;
  2808. while ((n = rb_first(&proc->refs_by_desc))) {
  2809. struct binder_ref *ref;
  2810. ref = rb_entry(n, struct binder_ref, rb_node_desc);
  2811. outgoing_refs++;
  2812. binder_delete_ref(ref);
  2813. }
  2814. binder_release_work(&proc->todo);
  2815. binder_release_work(&proc->delivered_death);
  2816. buffers = 0;
  2817. while ((n = rb_first(&proc->allocated_buffers))) {
  2818. struct binder_buffer *buffer;
  2819. buffer = rb_entry(n, struct binder_buffer, rb_node);
  2820. t = buffer->transaction;
  2821. if (t) {
  2822. t->buffer = NULL;
  2823. buffer->transaction = NULL;
  2824. pr_err("release proc %d, transaction %d, not freed\n",
  2825. proc->pid, t->debug_id);
  2826. /*BUG();*/
  2827. }
  2828. binder_free_buf(proc, buffer);
  2829. buffers++;
  2830. }
  2831. binder_stats_deleted(BINDER_STAT_PROC);
  2832. page_count = 0;
  2833. if (proc->pages) {
  2834. int i;
  2835. for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) {
  2836. void *page_addr;
  2837. if (!proc->pages[i])
  2838. continue;
  2839. page_addr = proc->buffer + i * PAGE_SIZE;
  2840. binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
  2841. "%s: %d: page %d at %p not freed\n",
  2842. __func__, proc->pid, i, page_addr);
  2843. unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
  2844. __free_page(proc->pages[i]);
  2845. page_count++;
  2846. }
  2847. kfree(proc->pages);
  2848. vfree(proc->buffer);
  2849. }
  2850. put_task_struct(proc->tsk);
  2851. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  2852. "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d, buffers %d, pages %d\n",
  2853. __func__, proc->pid, threads, nodes, incoming_refs,
  2854. outgoing_refs, active_transactions, buffers, page_count);
  2855. kfree(proc);
  2856. }
  2857. static void binder_deferred_func(struct work_struct *work)
  2858. {
  2859. struct binder_proc *proc;
  2860. struct files_struct *files;
  2861. int defer;
  2862. do {
  2863. binder_lock(__func__);
  2864. mutex_lock(&binder_deferred_lock);
  2865. if (!hlist_empty(&binder_deferred_list)) {
  2866. proc = hlist_entry(binder_deferred_list.first,
  2867. struct binder_proc, deferred_work_node);
  2868. hlist_del_init(&proc->deferred_work_node);
  2869. defer = proc->deferred_work;
  2870. proc->deferred_work = 0;
  2871. } else {
  2872. proc = NULL;
  2873. defer = 0;
  2874. }
  2875. mutex_unlock(&binder_deferred_lock);
  2876. files = NULL;
  2877. if (defer & BINDER_DEFERRED_PUT_FILES) {
  2878. files = proc->files;
  2879. if (files)
  2880. proc->files = NULL;
  2881. }
  2882. if (defer & BINDER_DEFERRED_FLUSH)
  2883. binder_deferred_flush(proc);
  2884. if (defer & BINDER_DEFERRED_RELEASE)
  2885. binder_deferred_release(proc); /* frees proc */
  2886. binder_unlock(__func__);
  2887. if (files)
  2888. put_files_struct(files);
  2889. } while (proc);
  2890. }
  2891. static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
  2892. static void
  2893. binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
  2894. {
  2895. mutex_lock(&binder_deferred_lock);
  2896. proc->deferred_work |= defer;
  2897. if (hlist_unhashed(&proc->deferred_work_node)) {
  2898. hlist_add_head(&proc->deferred_work_node,
  2899. &binder_deferred_list);
  2900. schedule_work(&binder_deferred_work);
  2901. }
  2902. mutex_unlock(&binder_deferred_lock);
  2903. }
  2904. static void print_binder_transaction(struct seq_file *m, const char *prefix,
  2905. struct binder_transaction *t)
  2906. {
  2907. seq_printf(m,
  2908. "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
  2909. prefix, t->debug_id, t,
  2910. t->from ? t->from->proc->pid : 0,
  2911. t->from ? t->from->pid : 0,
  2912. t->to_proc ? t->to_proc->pid : 0,
  2913. t->to_thread ? t->to_thread->pid : 0,
  2914. t->code, t->flags, t->priority, t->need_reply);
  2915. if (t->buffer == NULL) {
  2916. seq_puts(m, " buffer free\n");
  2917. return;
  2918. }
  2919. if (t->buffer->target_node)
  2920. seq_printf(m, " node %d",
  2921. t->buffer->target_node->debug_id);
  2922. seq_printf(m, " size %zd:%zd data %p\n",
  2923. t->buffer->data_size, t->buffer->offsets_size,
  2924. t->buffer->data);
  2925. }
  2926. static void print_binder_buffer(struct seq_file *m, const char *prefix,
  2927. struct binder_buffer *buffer)
  2928. {
  2929. seq_printf(m, "%s %d: %p size %zd:%zd %s\n",
  2930. prefix, buffer->debug_id, buffer->data,
  2931. buffer->data_size, buffer->offsets_size,
  2932. buffer->transaction ? "active" : "delivered");
  2933. }
  2934. static void print_binder_work(struct seq_file *m, const char *prefix,
  2935. const char *transaction_prefix,
  2936. struct binder_work *w)
  2937. {
  2938. struct binder_node *node;
  2939. struct binder_transaction *t;
  2940. switch (w->type) {
  2941. case BINDER_WORK_TRANSACTION:
  2942. t = container_of(w, struct binder_transaction, work);
  2943. print_binder_transaction(m, transaction_prefix, t);
  2944. break;
  2945. case BINDER_WORK_TRANSACTION_COMPLETE:
  2946. seq_printf(m, "%stransaction complete\n", prefix);
  2947. break;
  2948. case BINDER_WORK_NODE:
  2949. node = container_of(w, struct binder_node, work);
  2950. seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
  2951. prefix, node->debug_id,
  2952. (u64)node->ptr, (u64)node->cookie);
  2953. break;
  2954. case BINDER_WORK_DEAD_BINDER:
  2955. seq_printf(m, "%shas dead binder\n", prefix);
  2956. break;
  2957. case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
  2958. seq_printf(m, "%shas cleared dead binder\n", prefix);
  2959. break;
  2960. case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
  2961. seq_printf(m, "%shas cleared death notification\n", prefix);
  2962. break;
  2963. default:
  2964. seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
  2965. break;
  2966. }
  2967. }
  2968. static void print_binder_thread(struct seq_file *m,
  2969. struct binder_thread *thread,
  2970. int print_always)
  2971. {
  2972. struct binder_transaction *t;
  2973. struct binder_work *w;
  2974. size_t start_pos = m->count;
  2975. size_t header_pos;
  2976. seq_printf(m, " thread %d: l %02x\n", thread->pid, thread->looper);
  2977. header_pos = m->count;
  2978. t = thread->transaction_stack;
  2979. while (t) {
  2980. if (t->from == thread) {
  2981. print_binder_transaction(m,
  2982. " outgoing transaction", t);
  2983. t = t->from_parent;
  2984. } else if (t->to_thread == thread) {
  2985. print_binder_transaction(m,
  2986. " incoming transaction", t);
  2987. t = t->to_parent;
  2988. } else {
  2989. print_binder_transaction(m, " bad transaction", t);
  2990. t = NULL;
  2991. }
  2992. }
  2993. list_for_each_entry(w, &thread->todo, entry) {
  2994. print_binder_work(m, " ", " pending transaction", w);
  2995. }
  2996. if (!print_always && m->count == header_pos)
  2997. m->count = start_pos;
  2998. }
  2999. static void print_binder_node(struct seq_file *m, struct binder_node *node)
  3000. {
  3001. struct binder_ref *ref;
  3002. struct binder_work *w;
  3003. int count;
  3004. count = 0;
  3005. hlist_for_each_entry(ref, &node->refs, node_entry)
  3006. count++;
  3007. seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d",
  3008. node->debug_id, (u64)node->ptr, (u64)node->cookie,
  3009. node->has_strong_ref, node->has_weak_ref,
  3010. node->local_strong_refs, node->local_weak_refs,
  3011. node->internal_strong_refs, count);
  3012. if (count) {
  3013. seq_puts(m, " proc");
  3014. hlist_for_each_entry(ref, &node->refs, node_entry)
  3015. seq_printf(m, " %d", ref->proc->pid);
  3016. }
  3017. seq_puts(m, "\n");
  3018. list_for_each_entry(w, &node->async_todo, entry)
  3019. print_binder_work(m, " ",
  3020. " pending async transaction", w);
  3021. }
  3022. static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
  3023. {
  3024. seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %p\n",
  3025. ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
  3026. ref->node->debug_id, ref->strong, ref->weak, ref->death);
  3027. }
  3028. static void print_binder_proc(struct seq_file *m,
  3029. struct binder_proc *proc, int print_all)
  3030. {
  3031. struct binder_work *w;
  3032. struct rb_node *n;
  3033. size_t start_pos = m->count;
  3034. size_t header_pos;
  3035. seq_printf(m, "proc %d\n", proc->pid);
  3036. header_pos = m->count;
  3037. for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
  3038. print_binder_thread(m, rb_entry(n, struct binder_thread,
  3039. rb_node), print_all);
  3040. for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
  3041. struct binder_node *node = rb_entry(n, struct binder_node,
  3042. rb_node);
  3043. if (print_all || node->has_async_transaction)
  3044. print_binder_node(m, node);
  3045. }
  3046. if (print_all) {
  3047. for (n = rb_first(&proc->refs_by_desc);
  3048. n != NULL;
  3049. n = rb_next(n))
  3050. print_binder_ref(m, rb_entry(n, struct binder_ref,
  3051. rb_node_desc));
  3052. }
  3053. for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
  3054. print_binder_buffer(m, " buffer",
  3055. rb_entry(n, struct binder_buffer, rb_node));
  3056. list_for_each_entry(w, &proc->todo, entry)
  3057. print_binder_work(m, " ", " pending transaction", w);
  3058. list_for_each_entry(w, &proc->delivered_death, entry) {
  3059. seq_puts(m, " has delivered dead binder\n");
  3060. break;
  3061. }
  3062. if (!print_all && m->count == header_pos)
  3063. m->count = start_pos;
  3064. }
  3065. static const char * const binder_return_strings[] = {
  3066. "BR_ERROR",
  3067. "BR_OK",
  3068. "BR_TRANSACTION",
  3069. "BR_REPLY",
  3070. "BR_ACQUIRE_RESULT",
  3071. "BR_DEAD_REPLY",
  3072. "BR_TRANSACTION_COMPLETE",
  3073. "BR_INCREFS",
  3074. "BR_ACQUIRE",
  3075. "BR_RELEASE",
  3076. "BR_DECREFS",
  3077. "BR_ATTEMPT_ACQUIRE",
  3078. "BR_NOOP",
  3079. "BR_SPAWN_LOOPER",
  3080. "BR_FINISHED",
  3081. "BR_DEAD_BINDER",
  3082. "BR_CLEAR_DEATH_NOTIFICATION_DONE",
  3083. "BR_FAILED_REPLY"
  3084. };
  3085. static const char * const binder_command_strings[] = {
  3086. "BC_TRANSACTION",
  3087. "BC_REPLY",
  3088. "BC_ACQUIRE_RESULT",
  3089. "BC_FREE_BUFFER",
  3090. "BC_INCREFS",
  3091. "BC_ACQUIRE",
  3092. "BC_RELEASE",
  3093. "BC_DECREFS",
  3094. "BC_INCREFS_DONE",
  3095. "BC_ACQUIRE_DONE",
  3096. "BC_ATTEMPT_ACQUIRE",
  3097. "BC_REGISTER_LOOPER",
  3098. "BC_ENTER_LOOPER",
  3099. "BC_EXIT_LOOPER",
  3100. "BC_REQUEST_DEATH_NOTIFICATION",
  3101. "BC_CLEAR_DEATH_NOTIFICATION",
  3102. "BC_DEAD_BINDER_DONE"
  3103. };
  3104. static const char * const binder_objstat_strings[] = {
  3105. "proc",
  3106. "thread",
  3107. "node",
  3108. "ref",
  3109. "death",
  3110. "transaction",
  3111. "transaction_complete"
  3112. };
  3113. static void print_binder_stats(struct seq_file *m, const char *prefix,
  3114. struct binder_stats *stats)
  3115. {
  3116. int i;
  3117. BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
  3118. ARRAY_SIZE(binder_command_strings));
  3119. for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
  3120. if (stats->bc[i])
  3121. seq_printf(m, "%s%s: %d\n", prefix,
  3122. binder_command_strings[i], stats->bc[i]);
  3123. }
  3124. BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
  3125. ARRAY_SIZE(binder_return_strings));
  3126. for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
  3127. if (stats->br[i])
  3128. seq_printf(m, "%s%s: %d\n", prefix,
  3129. binder_return_strings[i], stats->br[i]);
  3130. }
  3131. BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
  3132. ARRAY_SIZE(binder_objstat_strings));
  3133. BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
  3134. ARRAY_SIZE(stats->obj_deleted));
  3135. for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
  3136. if (stats->obj_created[i] || stats->obj_deleted[i])
  3137. seq_printf(m, "%s%s: active %d total %d\n", prefix,
  3138. binder_objstat_strings[i],
  3139. stats->obj_created[i] - stats->obj_deleted[i],
  3140. stats->obj_created[i]);
  3141. }
  3142. }
  3143. static void print_binder_proc_stats(struct seq_file *m,
  3144. struct binder_proc *proc)
  3145. {
  3146. struct binder_work *w;
  3147. struct rb_node *n;
  3148. int count, strong, weak;
  3149. seq_printf(m, "proc %d\n", proc->pid);
  3150. count = 0;
  3151. for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
  3152. count++;
  3153. seq_printf(m, " threads: %d\n", count);
  3154. seq_printf(m, " requested threads: %d+%d/%d\n"
  3155. " ready threads %d\n"
  3156. " free async space %zd\n", proc->requested_threads,
  3157. proc->requested_threads_started, proc->max_threads,
  3158. proc->ready_threads, proc->free_async_space);
  3159. count = 0;
  3160. for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
  3161. count++;
  3162. seq_printf(m, " nodes: %d\n", count);
  3163. count = 0;
  3164. strong = 0;
  3165. weak = 0;
  3166. for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
  3167. struct binder_ref *ref = rb_entry(n, struct binder_ref,
  3168. rb_node_desc);
  3169. count++;
  3170. strong += ref->strong;
  3171. weak += ref->weak;
  3172. }
  3173. seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
  3174. count = 0;
  3175. for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
  3176. count++;
  3177. seq_printf(m, " buffers: %d\n", count);
  3178. count = 0;
  3179. list_for_each_entry(w, &proc->todo, entry) {
  3180. switch (w->type) {
  3181. case BINDER_WORK_TRANSACTION:
  3182. count++;
  3183. break;
  3184. default:
  3185. break;
  3186. }
  3187. }
  3188. seq_printf(m, " pending transactions: %d\n", count);
  3189. print_binder_stats(m, " ", &proc->stats);
  3190. }
  3191. static int binder_state_show(struct seq_file *m, void *unused)
  3192. {
  3193. struct binder_proc *proc;
  3194. struct binder_node *node;
  3195. int do_lock = !binder_debug_no_lock;
  3196. if (do_lock)
  3197. binder_lock(__func__);
  3198. seq_puts(m, "binder state:\n");
  3199. if (!hlist_empty(&binder_dead_nodes))
  3200. seq_puts(m, "dead nodes:\n");
  3201. hlist_for_each_entry(node, &binder_dead_nodes, dead_node)
  3202. print_binder_node(m, node);
  3203. hlist_for_each_entry(proc, &binder_procs, proc_node)
  3204. print_binder_proc(m, proc, 1);
  3205. if (do_lock)
  3206. binder_unlock(__func__);
  3207. return 0;
  3208. }
  3209. static int binder_stats_show(struct seq_file *m, void *unused)
  3210. {
  3211. struct binder_proc *proc;
  3212. int do_lock = !binder_debug_no_lock;
  3213. if (do_lock)
  3214. binder_lock(__func__);
  3215. seq_puts(m, "binder stats:\n");
  3216. print_binder_stats(m, "", &binder_stats);
  3217. hlist_for_each_entry(proc, &binder_procs, proc_node)
  3218. print_binder_proc_stats(m, proc);
  3219. if (do_lock)
  3220. binder_unlock(__func__);
  3221. return 0;
  3222. }
  3223. static int binder_transactions_show(struct seq_file *m, void *unused)
  3224. {
  3225. struct binder_proc *proc;
  3226. int do_lock = !binder_debug_no_lock;
  3227. if (do_lock)
  3228. binder_lock(__func__);
  3229. seq_puts(m, "binder transactions:\n");
  3230. hlist_for_each_entry(proc, &binder_procs, proc_node)
  3231. print_binder_proc(m, proc, 0);
  3232. if (do_lock)
  3233. binder_unlock(__func__);
  3234. return 0;
  3235. }
  3236. static int binder_proc_show(struct seq_file *m, void *unused)
  3237. {
  3238. struct binder_proc *itr;
  3239. struct binder_proc *proc = m->private;
  3240. int do_lock = !binder_debug_no_lock;
  3241. bool valid_proc = false;
  3242. if (do_lock)
  3243. binder_lock(__func__);
  3244. hlist_for_each_entry(itr, &binder_procs, proc_node) {
  3245. if (itr == proc) {
  3246. valid_proc = true;
  3247. break;
  3248. }
  3249. }
  3250. if (valid_proc) {
  3251. seq_puts(m, "binder proc state:\n");
  3252. print_binder_proc(m, proc, 1);
  3253. }
  3254. if (do_lock)
  3255. binder_unlock(__func__);
  3256. return 0;
  3257. }
  3258. static void print_binder_transaction_log_entry(struct seq_file *m,
  3259. struct binder_transaction_log_entry *e)
  3260. {
  3261. seq_printf(m,
  3262. "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
  3263. e->debug_id, (e->call_type == 2) ? "reply" :
  3264. ((e->call_type == 1) ? "async" : "call "), e->from_proc,
  3265. e->from_thread, e->to_proc, e->to_thread, e->to_node,
  3266. e->target_handle, e->data_size, e->offsets_size);
  3267. }
  3268. static int binder_transaction_log_show(struct seq_file *m, void *unused)
  3269. {
  3270. struct binder_transaction_log *log = m->private;
  3271. int i;
  3272. if (log->full) {
  3273. for (i = log->next; i < ARRAY_SIZE(log->entry); i++)
  3274. print_binder_transaction_log_entry(m, &log->entry[i]);
  3275. }
  3276. for (i = 0; i < log->next; i++)
  3277. print_binder_transaction_log_entry(m, &log->entry[i]);
  3278. return 0;
  3279. }
  3280. static const struct file_operations binder_fops = {
  3281. .owner = THIS_MODULE,
  3282. .poll = binder_poll,
  3283. .unlocked_ioctl = binder_ioctl,
  3284. .compat_ioctl = binder_ioctl,
  3285. .mmap = binder_mmap,
  3286. .open = binder_open,
  3287. .flush = binder_flush,
  3288. .release = binder_release,
  3289. };
  3290. static struct miscdevice binder_miscdev = {
  3291. .minor = MISC_DYNAMIC_MINOR,
  3292. .name = "binder",
  3293. .fops = &binder_fops
  3294. };
  3295. BINDER_DEBUG_ENTRY(state);
  3296. BINDER_DEBUG_ENTRY(stats);
  3297. BINDER_DEBUG_ENTRY(transactions);
  3298. BINDER_DEBUG_ENTRY(transaction_log);
  3299. static int __init binder_init(void)
  3300. {
  3301. int ret;
  3302. binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
  3303. if (binder_debugfs_dir_entry_root)
  3304. binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
  3305. binder_debugfs_dir_entry_root);
  3306. ret = misc_register(&binder_miscdev);
  3307. if (binder_debugfs_dir_entry_root) {
  3308. debugfs_create_file("state",
  3309. S_IRUGO,
  3310. binder_debugfs_dir_entry_root,
  3311. NULL,
  3312. &binder_state_fops);
  3313. debugfs_create_file("stats",
  3314. S_IRUGO,
  3315. binder_debugfs_dir_entry_root,
  3316. NULL,
  3317. &binder_stats_fops);
  3318. debugfs_create_file("transactions",
  3319. S_IRUGO,
  3320. binder_debugfs_dir_entry_root,
  3321. NULL,
  3322. &binder_transactions_fops);
  3323. debugfs_create_file("transaction_log",
  3324. S_IRUGO,
  3325. binder_debugfs_dir_entry_root,
  3326. &binder_transaction_log,
  3327. &binder_transaction_log_fops);
  3328. debugfs_create_file("failed_transaction_log",
  3329. S_IRUGO,
  3330. binder_debugfs_dir_entry_root,
  3331. &binder_transaction_log_failed,
  3332. &binder_transaction_log_fops);
  3333. }
  3334. return ret;
  3335. }
  3336. device_initcall(binder_init);
  3337. #define CREATE_TRACE_POINTS
  3338. #include "binder_trace.h"
  3339. MODULE_LICENSE("GPL v2");