binder.c 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738
  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. wake_up_interruptible(target_wait);
  1548. return;
  1549. err_get_unused_fd_failed:
  1550. err_fget_failed:
  1551. err_fd_not_allowed:
  1552. err_binder_get_ref_for_node_failed:
  1553. err_binder_get_ref_failed:
  1554. err_binder_new_node_failed:
  1555. err_bad_object_type:
  1556. err_bad_offset:
  1557. err_copy_data_failed:
  1558. trace_binder_transaction_failed_buffer_release(t->buffer);
  1559. binder_transaction_buffer_release(target_proc, t->buffer, offp);
  1560. t->buffer->transaction = NULL;
  1561. binder_free_buf(target_proc, t->buffer);
  1562. err_binder_alloc_buf_failed:
  1563. kfree(tcomplete);
  1564. binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
  1565. err_alloc_tcomplete_failed:
  1566. kfree(t);
  1567. binder_stats_deleted(BINDER_STAT_TRANSACTION);
  1568. err_alloc_t_failed:
  1569. err_bad_call_stack:
  1570. err_empty_call_stack:
  1571. err_dead_binder:
  1572. err_invalid_target_handle:
  1573. err_no_context_mgr_node:
  1574. binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
  1575. "%d:%d transaction failed %d, size %lld-%lld\n",
  1576. proc->pid, thread->pid, return_error,
  1577. (u64)tr->data_size, (u64)tr->offsets_size);
  1578. {
  1579. struct binder_transaction_log_entry *fe;
  1580. fe = binder_transaction_log_add(&binder_transaction_log_failed);
  1581. *fe = *e;
  1582. }
  1583. BUG_ON(thread->return_error != BR_OK);
  1584. if (in_reply_to) {
  1585. thread->return_error = BR_TRANSACTION_COMPLETE;
  1586. binder_send_failed_reply(in_reply_to, return_error);
  1587. } else
  1588. thread->return_error = return_error;
  1589. }
  1590. static int binder_thread_write(struct binder_proc *proc,
  1591. struct binder_thread *thread,
  1592. binder_uintptr_t binder_buffer, size_t size,
  1593. binder_size_t *consumed)
  1594. {
  1595. uint32_t cmd;
  1596. void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
  1597. void __user *ptr = buffer + *consumed;
  1598. void __user *end = buffer + size;
  1599. while (ptr < end && thread->return_error == BR_OK) {
  1600. if (get_user(cmd, (uint32_t __user *)ptr))
  1601. return -EFAULT;
  1602. ptr += sizeof(uint32_t);
  1603. trace_binder_command(cmd);
  1604. if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
  1605. binder_stats.bc[_IOC_NR(cmd)]++;
  1606. proc->stats.bc[_IOC_NR(cmd)]++;
  1607. thread->stats.bc[_IOC_NR(cmd)]++;
  1608. }
  1609. switch (cmd) {
  1610. case BC_INCREFS:
  1611. case BC_ACQUIRE:
  1612. case BC_RELEASE:
  1613. case BC_DECREFS: {
  1614. uint32_t target;
  1615. struct binder_ref *ref;
  1616. const char *debug_string;
  1617. if (get_user(target, (uint32_t __user *)ptr))
  1618. return -EFAULT;
  1619. ptr += sizeof(uint32_t);
  1620. if (target == 0 && binder_context_mgr_node &&
  1621. (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
  1622. ref = binder_get_ref_for_node(proc,
  1623. binder_context_mgr_node);
  1624. if (ref->desc != target) {
  1625. binder_user_error("%d:%d tried to acquire reference to desc 0, got %d instead\n",
  1626. proc->pid, thread->pid,
  1627. ref->desc);
  1628. }
  1629. } else
  1630. ref = binder_get_ref(proc, target,
  1631. cmd == BC_ACQUIRE ||
  1632. cmd == BC_RELEASE);
  1633. if (ref == NULL) {
  1634. binder_user_error("%d:%d refcount change on invalid ref %d\n",
  1635. proc->pid, thread->pid, target);
  1636. break;
  1637. }
  1638. switch (cmd) {
  1639. case BC_INCREFS:
  1640. debug_string = "IncRefs";
  1641. binder_inc_ref(ref, 0, NULL);
  1642. break;
  1643. case BC_ACQUIRE:
  1644. debug_string = "Acquire";
  1645. binder_inc_ref(ref, 1, NULL);
  1646. break;
  1647. case BC_RELEASE:
  1648. debug_string = "Release";
  1649. binder_dec_ref(ref, 1);
  1650. break;
  1651. case BC_DECREFS:
  1652. default:
  1653. debug_string = "DecRefs";
  1654. binder_dec_ref(ref, 0);
  1655. break;
  1656. }
  1657. binder_debug(BINDER_DEBUG_USER_REFS,
  1658. "%d:%d %s ref %d desc %d s %d w %d for node %d\n",
  1659. proc->pid, thread->pid, debug_string, ref->debug_id,
  1660. ref->desc, ref->strong, ref->weak, ref->node->debug_id);
  1661. break;
  1662. }
  1663. case BC_INCREFS_DONE:
  1664. case BC_ACQUIRE_DONE: {
  1665. binder_uintptr_t node_ptr;
  1666. binder_uintptr_t cookie;
  1667. struct binder_node *node;
  1668. if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
  1669. return -EFAULT;
  1670. ptr += sizeof(binder_uintptr_t);
  1671. if (get_user(cookie, (binder_uintptr_t __user *)ptr))
  1672. return -EFAULT;
  1673. ptr += sizeof(binder_uintptr_t);
  1674. node = binder_get_node(proc, node_ptr);
  1675. if (node == NULL) {
  1676. binder_user_error("%d:%d %s u%016llx no match\n",
  1677. proc->pid, thread->pid,
  1678. cmd == BC_INCREFS_DONE ?
  1679. "BC_INCREFS_DONE" :
  1680. "BC_ACQUIRE_DONE",
  1681. (u64)node_ptr);
  1682. break;
  1683. }
  1684. if (cookie != node->cookie) {
  1685. binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
  1686. proc->pid, thread->pid,
  1687. cmd == BC_INCREFS_DONE ?
  1688. "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
  1689. (u64)node_ptr, node->debug_id,
  1690. (u64)cookie, (u64)node->cookie);
  1691. break;
  1692. }
  1693. if (cmd == BC_ACQUIRE_DONE) {
  1694. if (node->pending_strong_ref == 0) {
  1695. binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
  1696. proc->pid, thread->pid,
  1697. node->debug_id);
  1698. break;
  1699. }
  1700. node->pending_strong_ref = 0;
  1701. } else {
  1702. if (node->pending_weak_ref == 0) {
  1703. binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
  1704. proc->pid, thread->pid,
  1705. node->debug_id);
  1706. break;
  1707. }
  1708. node->pending_weak_ref = 0;
  1709. }
  1710. binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
  1711. binder_debug(BINDER_DEBUG_USER_REFS,
  1712. "%d:%d %s node %d ls %d lw %d\n",
  1713. proc->pid, thread->pid,
  1714. cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
  1715. node->debug_id, node->local_strong_refs, node->local_weak_refs);
  1716. break;
  1717. }
  1718. case BC_ATTEMPT_ACQUIRE:
  1719. pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
  1720. return -EINVAL;
  1721. case BC_ACQUIRE_RESULT:
  1722. pr_err("BC_ACQUIRE_RESULT not supported\n");
  1723. return -EINVAL;
  1724. case BC_FREE_BUFFER: {
  1725. binder_uintptr_t data_ptr;
  1726. struct binder_buffer *buffer;
  1727. if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
  1728. return -EFAULT;
  1729. ptr += sizeof(binder_uintptr_t);
  1730. buffer = binder_buffer_lookup(proc, data_ptr);
  1731. if (buffer == NULL) {
  1732. binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
  1733. proc->pid, thread->pid, (u64)data_ptr);
  1734. break;
  1735. }
  1736. if (!buffer->allow_user_free) {
  1737. binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
  1738. proc->pid, thread->pid, (u64)data_ptr);
  1739. break;
  1740. }
  1741. binder_debug(BINDER_DEBUG_FREE_BUFFER,
  1742. "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
  1743. proc->pid, thread->pid, (u64)data_ptr,
  1744. buffer->debug_id,
  1745. buffer->transaction ? "active" : "finished");
  1746. if (buffer->transaction) {
  1747. buffer->transaction->buffer = NULL;
  1748. buffer->transaction = NULL;
  1749. }
  1750. if (buffer->async_transaction && buffer->target_node) {
  1751. BUG_ON(!buffer->target_node->has_async_transaction);
  1752. if (list_empty(&buffer->target_node->async_todo))
  1753. buffer->target_node->has_async_transaction = 0;
  1754. else
  1755. list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
  1756. }
  1757. trace_binder_transaction_buffer_release(buffer);
  1758. binder_transaction_buffer_release(proc, buffer, NULL);
  1759. binder_free_buf(proc, buffer);
  1760. break;
  1761. }
  1762. case BC_TRANSACTION:
  1763. case BC_REPLY: {
  1764. struct binder_transaction_data tr;
  1765. if (copy_from_user(&tr, ptr, sizeof(tr)))
  1766. return -EFAULT;
  1767. ptr += sizeof(tr);
  1768. binder_transaction(proc, thread, &tr, cmd == BC_REPLY);
  1769. break;
  1770. }
  1771. case BC_REGISTER_LOOPER:
  1772. binder_debug(BINDER_DEBUG_THREADS,
  1773. "%d:%d BC_REGISTER_LOOPER\n",
  1774. proc->pid, thread->pid);
  1775. if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
  1776. thread->looper |= BINDER_LOOPER_STATE_INVALID;
  1777. binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
  1778. proc->pid, thread->pid);
  1779. } else if (proc->requested_threads == 0) {
  1780. thread->looper |= BINDER_LOOPER_STATE_INVALID;
  1781. binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
  1782. proc->pid, thread->pid);
  1783. } else {
  1784. proc->requested_threads--;
  1785. proc->requested_threads_started++;
  1786. }
  1787. thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
  1788. break;
  1789. case BC_ENTER_LOOPER:
  1790. binder_debug(BINDER_DEBUG_THREADS,
  1791. "%d:%d BC_ENTER_LOOPER\n",
  1792. proc->pid, thread->pid);
  1793. if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
  1794. thread->looper |= BINDER_LOOPER_STATE_INVALID;
  1795. binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
  1796. proc->pid, thread->pid);
  1797. }
  1798. thread->looper |= BINDER_LOOPER_STATE_ENTERED;
  1799. break;
  1800. case BC_EXIT_LOOPER:
  1801. binder_debug(BINDER_DEBUG_THREADS,
  1802. "%d:%d BC_EXIT_LOOPER\n",
  1803. proc->pid, thread->pid);
  1804. thread->looper |= BINDER_LOOPER_STATE_EXITED;
  1805. break;
  1806. case BC_REQUEST_DEATH_NOTIFICATION:
  1807. case BC_CLEAR_DEATH_NOTIFICATION: {
  1808. uint32_t target;
  1809. binder_uintptr_t cookie;
  1810. struct binder_ref *ref;
  1811. struct binder_ref_death *death;
  1812. if (get_user(target, (uint32_t __user *)ptr))
  1813. return -EFAULT;
  1814. ptr += sizeof(uint32_t);
  1815. if (get_user(cookie, (binder_uintptr_t __user *)ptr))
  1816. return -EFAULT;
  1817. ptr += sizeof(binder_uintptr_t);
  1818. ref = binder_get_ref(proc, target, false);
  1819. if (ref == NULL) {
  1820. binder_user_error("%d:%d %s invalid ref %d\n",
  1821. proc->pid, thread->pid,
  1822. cmd == BC_REQUEST_DEATH_NOTIFICATION ?
  1823. "BC_REQUEST_DEATH_NOTIFICATION" :
  1824. "BC_CLEAR_DEATH_NOTIFICATION",
  1825. target);
  1826. break;
  1827. }
  1828. binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
  1829. "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
  1830. proc->pid, thread->pid,
  1831. cmd == BC_REQUEST_DEATH_NOTIFICATION ?
  1832. "BC_REQUEST_DEATH_NOTIFICATION" :
  1833. "BC_CLEAR_DEATH_NOTIFICATION",
  1834. (u64)cookie, ref->debug_id, ref->desc,
  1835. ref->strong, ref->weak, ref->node->debug_id);
  1836. if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
  1837. if (ref->death) {
  1838. binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
  1839. proc->pid, thread->pid);
  1840. break;
  1841. }
  1842. death = kzalloc(sizeof(*death), GFP_KERNEL);
  1843. if (death == NULL) {
  1844. thread->return_error = BR_ERROR;
  1845. binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
  1846. "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
  1847. proc->pid, thread->pid);
  1848. break;
  1849. }
  1850. binder_stats_created(BINDER_STAT_DEATH);
  1851. INIT_LIST_HEAD(&death->work.entry);
  1852. death->cookie = cookie;
  1853. ref->death = death;
  1854. if (ref->node->proc == NULL) {
  1855. ref->death->work.type = BINDER_WORK_DEAD_BINDER;
  1856. if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
  1857. list_add_tail(&ref->death->work.entry, &thread->todo);
  1858. } else {
  1859. list_add_tail(&ref->death->work.entry, &proc->todo);
  1860. wake_up_interruptible(&proc->wait);
  1861. }
  1862. }
  1863. } else {
  1864. if (ref->death == NULL) {
  1865. binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
  1866. proc->pid, thread->pid);
  1867. break;
  1868. }
  1869. death = ref->death;
  1870. if (death->cookie != cookie) {
  1871. binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
  1872. proc->pid, thread->pid,
  1873. (u64)death->cookie,
  1874. (u64)cookie);
  1875. break;
  1876. }
  1877. ref->death = NULL;
  1878. if (list_empty(&death->work.entry)) {
  1879. death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
  1880. if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
  1881. list_add_tail(&death->work.entry, &thread->todo);
  1882. } else {
  1883. list_add_tail(&death->work.entry, &proc->todo);
  1884. wake_up_interruptible(&proc->wait);
  1885. }
  1886. } else {
  1887. BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
  1888. death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
  1889. }
  1890. }
  1891. } break;
  1892. case BC_DEAD_BINDER_DONE: {
  1893. struct binder_work *w;
  1894. binder_uintptr_t cookie;
  1895. struct binder_ref_death *death = NULL;
  1896. if (get_user(cookie, (binder_uintptr_t __user *)ptr))
  1897. return -EFAULT;
  1898. ptr += sizeof(cookie);
  1899. list_for_each_entry(w, &proc->delivered_death, entry) {
  1900. struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
  1901. if (tmp_death->cookie == cookie) {
  1902. death = tmp_death;
  1903. break;
  1904. }
  1905. }
  1906. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  1907. "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
  1908. proc->pid, thread->pid, (u64)cookie,
  1909. death);
  1910. if (death == NULL) {
  1911. binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
  1912. proc->pid, thread->pid, (u64)cookie);
  1913. break;
  1914. }
  1915. list_del_init(&death->work.entry);
  1916. if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
  1917. death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
  1918. if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
  1919. list_add_tail(&death->work.entry, &thread->todo);
  1920. } else {
  1921. list_add_tail(&death->work.entry, &proc->todo);
  1922. wake_up_interruptible(&proc->wait);
  1923. }
  1924. }
  1925. } break;
  1926. default:
  1927. pr_err("%d:%d unknown command %d\n",
  1928. proc->pid, thread->pid, cmd);
  1929. return -EINVAL;
  1930. }
  1931. *consumed = ptr - buffer;
  1932. }
  1933. return 0;
  1934. }
  1935. static void binder_stat_br(struct binder_proc *proc,
  1936. struct binder_thread *thread, uint32_t cmd)
  1937. {
  1938. trace_binder_return(cmd);
  1939. if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
  1940. binder_stats.br[_IOC_NR(cmd)]++;
  1941. proc->stats.br[_IOC_NR(cmd)]++;
  1942. thread->stats.br[_IOC_NR(cmd)]++;
  1943. }
  1944. }
  1945. static int binder_has_proc_work(struct binder_proc *proc,
  1946. struct binder_thread *thread)
  1947. {
  1948. return !list_empty(&proc->todo) ||
  1949. (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
  1950. }
  1951. static int binder_has_thread_work(struct binder_thread *thread)
  1952. {
  1953. return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
  1954. (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
  1955. }
  1956. static int binder_thread_read(struct binder_proc *proc,
  1957. struct binder_thread *thread,
  1958. binder_uintptr_t binder_buffer, size_t size,
  1959. binder_size_t *consumed, int non_block)
  1960. {
  1961. void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
  1962. void __user *ptr = buffer + *consumed;
  1963. void __user *end = buffer + size;
  1964. int ret = 0;
  1965. int wait_for_proc_work;
  1966. if (*consumed == 0) {
  1967. if (put_user(BR_NOOP, (uint32_t __user *)ptr))
  1968. return -EFAULT;
  1969. ptr += sizeof(uint32_t);
  1970. }
  1971. retry:
  1972. wait_for_proc_work = thread->transaction_stack == NULL &&
  1973. list_empty(&thread->todo);
  1974. if (thread->return_error != BR_OK && ptr < end) {
  1975. if (thread->return_error2 != BR_OK) {
  1976. if (put_user(thread->return_error2, (uint32_t __user *)ptr))
  1977. return -EFAULT;
  1978. ptr += sizeof(uint32_t);
  1979. binder_stat_br(proc, thread, thread->return_error2);
  1980. if (ptr == end)
  1981. goto done;
  1982. thread->return_error2 = BR_OK;
  1983. }
  1984. if (put_user(thread->return_error, (uint32_t __user *)ptr))
  1985. return -EFAULT;
  1986. ptr += sizeof(uint32_t);
  1987. binder_stat_br(proc, thread, thread->return_error);
  1988. thread->return_error = BR_OK;
  1989. goto done;
  1990. }
  1991. thread->looper |= BINDER_LOOPER_STATE_WAITING;
  1992. if (wait_for_proc_work)
  1993. proc->ready_threads++;
  1994. binder_unlock(__func__);
  1995. trace_binder_wait_for_work(wait_for_proc_work,
  1996. !!thread->transaction_stack,
  1997. !list_empty(&thread->todo));
  1998. if (wait_for_proc_work) {
  1999. if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
  2000. BINDER_LOOPER_STATE_ENTERED))) {
  2001. binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
  2002. proc->pid, thread->pid, thread->looper);
  2003. wait_event_interruptible(binder_user_error_wait,
  2004. binder_stop_on_user_error < 2);
  2005. }
  2006. binder_set_nice(proc->default_priority);
  2007. if (non_block) {
  2008. if (!binder_has_proc_work(proc, thread))
  2009. ret = -EAGAIN;
  2010. } else
  2011. ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
  2012. } else {
  2013. if (non_block) {
  2014. if (!binder_has_thread_work(thread))
  2015. ret = -EAGAIN;
  2016. } else
  2017. ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
  2018. }
  2019. binder_lock(__func__);
  2020. if (wait_for_proc_work)
  2021. proc->ready_threads--;
  2022. thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
  2023. if (ret)
  2024. return ret;
  2025. while (1) {
  2026. uint32_t cmd;
  2027. struct binder_transaction_data tr;
  2028. struct binder_work *w;
  2029. struct binder_transaction *t = NULL;
  2030. if (!list_empty(&thread->todo)) {
  2031. w = list_first_entry(&thread->todo, struct binder_work,
  2032. entry);
  2033. } else if (!list_empty(&proc->todo) && wait_for_proc_work) {
  2034. w = list_first_entry(&proc->todo, struct binder_work,
  2035. entry);
  2036. } else {
  2037. /* no data added */
  2038. if (ptr - buffer == 4 &&
  2039. !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN))
  2040. goto retry;
  2041. break;
  2042. }
  2043. if (end - ptr < sizeof(tr) + 4)
  2044. break;
  2045. switch (w->type) {
  2046. case BINDER_WORK_TRANSACTION: {
  2047. t = container_of(w, struct binder_transaction, work);
  2048. } break;
  2049. case BINDER_WORK_TRANSACTION_COMPLETE: {
  2050. cmd = BR_TRANSACTION_COMPLETE;
  2051. if (put_user(cmd, (uint32_t __user *)ptr))
  2052. return -EFAULT;
  2053. ptr += sizeof(uint32_t);
  2054. binder_stat_br(proc, thread, cmd);
  2055. binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
  2056. "%d:%d BR_TRANSACTION_COMPLETE\n",
  2057. proc->pid, thread->pid);
  2058. list_del(&w->entry);
  2059. kfree(w);
  2060. binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
  2061. } break;
  2062. case BINDER_WORK_NODE: {
  2063. struct binder_node *node = container_of(w, struct binder_node, work);
  2064. uint32_t cmd = BR_NOOP;
  2065. const char *cmd_name;
  2066. int strong = node->internal_strong_refs || node->local_strong_refs;
  2067. int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong;
  2068. if (weak && !node->has_weak_ref) {
  2069. cmd = BR_INCREFS;
  2070. cmd_name = "BR_INCREFS";
  2071. node->has_weak_ref = 1;
  2072. node->pending_weak_ref = 1;
  2073. node->local_weak_refs++;
  2074. } else if (strong && !node->has_strong_ref) {
  2075. cmd = BR_ACQUIRE;
  2076. cmd_name = "BR_ACQUIRE";
  2077. node->has_strong_ref = 1;
  2078. node->pending_strong_ref = 1;
  2079. node->local_strong_refs++;
  2080. } else if (!strong && node->has_strong_ref) {
  2081. cmd = BR_RELEASE;
  2082. cmd_name = "BR_RELEASE";
  2083. node->has_strong_ref = 0;
  2084. } else if (!weak && node->has_weak_ref) {
  2085. cmd = BR_DECREFS;
  2086. cmd_name = "BR_DECREFS";
  2087. node->has_weak_ref = 0;
  2088. }
  2089. if (cmd != BR_NOOP) {
  2090. if (put_user(cmd, (uint32_t __user *)ptr))
  2091. return -EFAULT;
  2092. ptr += sizeof(uint32_t);
  2093. if (put_user(node->ptr,
  2094. (binder_uintptr_t __user *)ptr))
  2095. return -EFAULT;
  2096. ptr += sizeof(binder_uintptr_t);
  2097. if (put_user(node->cookie,
  2098. (binder_uintptr_t __user *)ptr))
  2099. return -EFAULT;
  2100. ptr += sizeof(binder_uintptr_t);
  2101. binder_stat_br(proc, thread, cmd);
  2102. binder_debug(BINDER_DEBUG_USER_REFS,
  2103. "%d:%d %s %d u%016llx c%016llx\n",
  2104. proc->pid, thread->pid, cmd_name,
  2105. node->debug_id,
  2106. (u64)node->ptr, (u64)node->cookie);
  2107. } else {
  2108. list_del_init(&w->entry);
  2109. if (!weak && !strong) {
  2110. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  2111. "%d:%d node %d u%016llx c%016llx deleted\n",
  2112. proc->pid, thread->pid,
  2113. node->debug_id,
  2114. (u64)node->ptr,
  2115. (u64)node->cookie);
  2116. rb_erase(&node->rb_node, &proc->nodes);
  2117. kfree(node);
  2118. binder_stats_deleted(BINDER_STAT_NODE);
  2119. } else {
  2120. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  2121. "%d:%d node %d u%016llx c%016llx state unchanged\n",
  2122. proc->pid, thread->pid,
  2123. node->debug_id,
  2124. (u64)node->ptr,
  2125. (u64)node->cookie);
  2126. }
  2127. }
  2128. } break;
  2129. case BINDER_WORK_DEAD_BINDER:
  2130. case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
  2131. case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
  2132. struct binder_ref_death *death;
  2133. uint32_t cmd;
  2134. death = container_of(w, struct binder_ref_death, work);
  2135. if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
  2136. cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
  2137. else
  2138. cmd = BR_DEAD_BINDER;
  2139. if (put_user(cmd, (uint32_t __user *)ptr))
  2140. return -EFAULT;
  2141. ptr += sizeof(uint32_t);
  2142. if (put_user(death->cookie,
  2143. (binder_uintptr_t __user *)ptr))
  2144. return -EFAULT;
  2145. ptr += sizeof(binder_uintptr_t);
  2146. binder_stat_br(proc, thread, cmd);
  2147. binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
  2148. "%d:%d %s %016llx\n",
  2149. proc->pid, thread->pid,
  2150. cmd == BR_DEAD_BINDER ?
  2151. "BR_DEAD_BINDER" :
  2152. "BR_CLEAR_DEATH_NOTIFICATION_DONE",
  2153. (u64)death->cookie);
  2154. if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
  2155. list_del(&w->entry);
  2156. kfree(death);
  2157. binder_stats_deleted(BINDER_STAT_DEATH);
  2158. } else
  2159. list_move(&w->entry, &proc->delivered_death);
  2160. if (cmd == BR_DEAD_BINDER)
  2161. goto done; /* DEAD_BINDER notifications can cause transactions */
  2162. } break;
  2163. }
  2164. if (!t)
  2165. continue;
  2166. BUG_ON(t->buffer == NULL);
  2167. if (t->buffer->target_node) {
  2168. struct binder_node *target_node = t->buffer->target_node;
  2169. tr.target.ptr = target_node->ptr;
  2170. tr.cookie = target_node->cookie;
  2171. t->saved_priority = task_nice(current);
  2172. if (t->priority < target_node->min_priority &&
  2173. !(t->flags & TF_ONE_WAY))
  2174. binder_set_nice(t->priority);
  2175. else if (!(t->flags & TF_ONE_WAY) ||
  2176. t->saved_priority > target_node->min_priority)
  2177. binder_set_nice(target_node->min_priority);
  2178. cmd = BR_TRANSACTION;
  2179. } else {
  2180. tr.target.ptr = 0;
  2181. tr.cookie = 0;
  2182. cmd = BR_REPLY;
  2183. }
  2184. tr.code = t->code;
  2185. tr.flags = t->flags;
  2186. tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
  2187. if (t->from) {
  2188. struct task_struct *sender = t->from->proc->tsk;
  2189. tr.sender_pid = task_tgid_nr_ns(sender,
  2190. task_active_pid_ns(current));
  2191. } else {
  2192. tr.sender_pid = 0;
  2193. }
  2194. tr.data_size = t->buffer->data_size;
  2195. tr.offsets_size = t->buffer->offsets_size;
  2196. tr.data.ptr.buffer = (binder_uintptr_t)(
  2197. (uintptr_t)t->buffer->data +
  2198. proc->user_buffer_offset);
  2199. tr.data.ptr.offsets = tr.data.ptr.buffer +
  2200. ALIGN(t->buffer->data_size,
  2201. sizeof(void *));
  2202. if (put_user(cmd, (uint32_t __user *)ptr))
  2203. return -EFAULT;
  2204. ptr += sizeof(uint32_t);
  2205. if (copy_to_user(ptr, &tr, sizeof(tr)))
  2206. return -EFAULT;
  2207. ptr += sizeof(tr);
  2208. trace_binder_transaction_received(t);
  2209. binder_stat_br(proc, thread, cmd);
  2210. binder_debug(BINDER_DEBUG_TRANSACTION,
  2211. "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
  2212. proc->pid, thread->pid,
  2213. (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
  2214. "BR_REPLY",
  2215. t->debug_id, t->from ? t->from->proc->pid : 0,
  2216. t->from ? t->from->pid : 0, cmd,
  2217. t->buffer->data_size, t->buffer->offsets_size,
  2218. (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
  2219. list_del(&t->work.entry);
  2220. t->buffer->allow_user_free = 1;
  2221. if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
  2222. t->to_parent = thread->transaction_stack;
  2223. t->to_thread = thread;
  2224. thread->transaction_stack = t;
  2225. } else {
  2226. t->buffer->transaction = NULL;
  2227. kfree(t);
  2228. binder_stats_deleted(BINDER_STAT_TRANSACTION);
  2229. }
  2230. break;
  2231. }
  2232. done:
  2233. *consumed = ptr - buffer;
  2234. if (proc->requested_threads + proc->ready_threads == 0 &&
  2235. proc->requested_threads_started < proc->max_threads &&
  2236. (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
  2237. BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
  2238. /*spawn a new thread if we leave this out */) {
  2239. proc->requested_threads++;
  2240. binder_debug(BINDER_DEBUG_THREADS,
  2241. "%d:%d BR_SPAWN_LOOPER\n",
  2242. proc->pid, thread->pid);
  2243. if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
  2244. return -EFAULT;
  2245. binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
  2246. }
  2247. return 0;
  2248. }
  2249. static void binder_release_work(struct list_head *list)
  2250. {
  2251. struct binder_work *w;
  2252. while (!list_empty(list)) {
  2253. w = list_first_entry(list, struct binder_work, entry);
  2254. list_del_init(&w->entry);
  2255. switch (w->type) {
  2256. case BINDER_WORK_TRANSACTION: {
  2257. struct binder_transaction *t;
  2258. t = container_of(w, struct binder_transaction, work);
  2259. if (t->buffer->target_node &&
  2260. !(t->flags & TF_ONE_WAY)) {
  2261. binder_send_failed_reply(t, BR_DEAD_REPLY);
  2262. } else {
  2263. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  2264. "undelivered transaction %d\n",
  2265. t->debug_id);
  2266. t->buffer->transaction = NULL;
  2267. kfree(t);
  2268. binder_stats_deleted(BINDER_STAT_TRANSACTION);
  2269. }
  2270. } break;
  2271. case BINDER_WORK_TRANSACTION_COMPLETE: {
  2272. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  2273. "undelivered TRANSACTION_COMPLETE\n");
  2274. kfree(w);
  2275. binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
  2276. } break;
  2277. case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
  2278. case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
  2279. struct binder_ref_death *death;
  2280. death = container_of(w, struct binder_ref_death, work);
  2281. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  2282. "undelivered death notification, %016llx\n",
  2283. (u64)death->cookie);
  2284. kfree(death);
  2285. binder_stats_deleted(BINDER_STAT_DEATH);
  2286. } break;
  2287. default:
  2288. pr_err("unexpected work type, %d, not freed\n",
  2289. w->type);
  2290. break;
  2291. }
  2292. }
  2293. }
  2294. static struct binder_thread *binder_get_thread(struct binder_proc *proc)
  2295. {
  2296. struct binder_thread *thread = NULL;
  2297. struct rb_node *parent = NULL;
  2298. struct rb_node **p = &proc->threads.rb_node;
  2299. while (*p) {
  2300. parent = *p;
  2301. thread = rb_entry(parent, struct binder_thread, rb_node);
  2302. if (current->pid < thread->pid)
  2303. p = &(*p)->rb_left;
  2304. else if (current->pid > thread->pid)
  2305. p = &(*p)->rb_right;
  2306. else
  2307. break;
  2308. }
  2309. if (*p == NULL) {
  2310. thread = kzalloc(sizeof(*thread), GFP_KERNEL);
  2311. if (thread == NULL)
  2312. return NULL;
  2313. binder_stats_created(BINDER_STAT_THREAD);
  2314. thread->proc = proc;
  2315. thread->pid = current->pid;
  2316. init_waitqueue_head(&thread->wait);
  2317. INIT_LIST_HEAD(&thread->todo);
  2318. rb_link_node(&thread->rb_node, parent, p);
  2319. rb_insert_color(&thread->rb_node, &proc->threads);
  2320. thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
  2321. thread->return_error = BR_OK;
  2322. thread->return_error2 = BR_OK;
  2323. }
  2324. return thread;
  2325. }
  2326. static int binder_free_thread(struct binder_proc *proc,
  2327. struct binder_thread *thread)
  2328. {
  2329. struct binder_transaction *t;
  2330. struct binder_transaction *send_reply = NULL;
  2331. int active_transactions = 0;
  2332. rb_erase(&thread->rb_node, &proc->threads);
  2333. t = thread->transaction_stack;
  2334. if (t && t->to_thread == thread)
  2335. send_reply = t;
  2336. while (t) {
  2337. active_transactions++;
  2338. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  2339. "release %d:%d transaction %d %s, still active\n",
  2340. proc->pid, thread->pid,
  2341. t->debug_id,
  2342. (t->to_thread == thread) ? "in" : "out");
  2343. if (t->to_thread == thread) {
  2344. t->to_proc = NULL;
  2345. t->to_thread = NULL;
  2346. if (t->buffer) {
  2347. t->buffer->transaction = NULL;
  2348. t->buffer = NULL;
  2349. }
  2350. t = t->to_parent;
  2351. } else if (t->from == thread) {
  2352. t->from = NULL;
  2353. t = t->from_parent;
  2354. } else
  2355. BUG();
  2356. }
  2357. if (send_reply)
  2358. binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
  2359. binder_release_work(&thread->todo);
  2360. kfree(thread);
  2361. binder_stats_deleted(BINDER_STAT_THREAD);
  2362. return active_transactions;
  2363. }
  2364. static unsigned int binder_poll(struct file *filp,
  2365. struct poll_table_struct *wait)
  2366. {
  2367. struct binder_proc *proc = filp->private_data;
  2368. struct binder_thread *thread = NULL;
  2369. int wait_for_proc_work;
  2370. binder_lock(__func__);
  2371. thread = binder_get_thread(proc);
  2372. wait_for_proc_work = thread->transaction_stack == NULL &&
  2373. list_empty(&thread->todo) && thread->return_error == BR_OK;
  2374. binder_unlock(__func__);
  2375. if (wait_for_proc_work) {
  2376. if (binder_has_proc_work(proc, thread))
  2377. return POLLIN;
  2378. poll_wait(filp, &proc->wait, wait);
  2379. if (binder_has_proc_work(proc, thread))
  2380. return POLLIN;
  2381. } else {
  2382. if (binder_has_thread_work(thread))
  2383. return POLLIN;
  2384. poll_wait(filp, &thread->wait, wait);
  2385. if (binder_has_thread_work(thread))
  2386. return POLLIN;
  2387. }
  2388. return 0;
  2389. }
  2390. static int binder_ioctl_write_read(struct file *filp,
  2391. unsigned int cmd, unsigned long arg,
  2392. struct binder_thread *thread)
  2393. {
  2394. int ret = 0;
  2395. struct binder_proc *proc = filp->private_data;
  2396. unsigned int size = _IOC_SIZE(cmd);
  2397. void __user *ubuf = (void __user *)arg;
  2398. struct binder_write_read bwr;
  2399. if (size != sizeof(struct binder_write_read)) {
  2400. ret = -EINVAL;
  2401. goto out;
  2402. }
  2403. if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
  2404. ret = -EFAULT;
  2405. goto out;
  2406. }
  2407. binder_debug(BINDER_DEBUG_READ_WRITE,
  2408. "%d:%d write %lld at %016llx, read %lld at %016llx\n",
  2409. proc->pid, thread->pid,
  2410. (u64)bwr.write_size, (u64)bwr.write_buffer,
  2411. (u64)bwr.read_size, (u64)bwr.read_buffer);
  2412. if (bwr.write_size > 0) {
  2413. ret = binder_thread_write(proc, thread,
  2414. bwr.write_buffer,
  2415. bwr.write_size,
  2416. &bwr.write_consumed);
  2417. trace_binder_write_done(ret);
  2418. if (ret < 0) {
  2419. bwr.read_consumed = 0;
  2420. if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
  2421. ret = -EFAULT;
  2422. goto out;
  2423. }
  2424. }
  2425. if (bwr.read_size > 0) {
  2426. ret = binder_thread_read(proc, thread, bwr.read_buffer,
  2427. bwr.read_size,
  2428. &bwr.read_consumed,
  2429. filp->f_flags & O_NONBLOCK);
  2430. trace_binder_read_done(ret);
  2431. if (!list_empty(&proc->todo))
  2432. wake_up_interruptible(&proc->wait);
  2433. if (ret < 0) {
  2434. if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
  2435. ret = -EFAULT;
  2436. goto out;
  2437. }
  2438. }
  2439. binder_debug(BINDER_DEBUG_READ_WRITE,
  2440. "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
  2441. proc->pid, thread->pid,
  2442. (u64)bwr.write_consumed, (u64)bwr.write_size,
  2443. (u64)bwr.read_consumed, (u64)bwr.read_size);
  2444. if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
  2445. ret = -EFAULT;
  2446. goto out;
  2447. }
  2448. out:
  2449. return ret;
  2450. }
  2451. static int binder_ioctl_set_ctx_mgr(struct file *filp)
  2452. {
  2453. int ret = 0;
  2454. struct binder_proc *proc = filp->private_data;
  2455. kuid_t curr_euid = current_euid();
  2456. if (binder_context_mgr_node != NULL) {
  2457. pr_err("BINDER_SET_CONTEXT_MGR already set\n");
  2458. ret = -EBUSY;
  2459. goto out;
  2460. }
  2461. ret = security_binder_set_context_mgr(proc->tsk);
  2462. if (ret < 0)
  2463. goto out;
  2464. if (uid_valid(binder_context_mgr_uid)) {
  2465. if (!uid_eq(binder_context_mgr_uid, curr_euid)) {
  2466. pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
  2467. from_kuid(&init_user_ns, curr_euid),
  2468. from_kuid(&init_user_ns,
  2469. binder_context_mgr_uid));
  2470. ret = -EPERM;
  2471. goto out;
  2472. }
  2473. } else {
  2474. binder_context_mgr_uid = curr_euid;
  2475. }
  2476. binder_context_mgr_node = binder_new_node(proc, 0, 0);
  2477. if (binder_context_mgr_node == NULL) {
  2478. ret = -ENOMEM;
  2479. goto out;
  2480. }
  2481. binder_context_mgr_node->local_weak_refs++;
  2482. binder_context_mgr_node->local_strong_refs++;
  2483. binder_context_mgr_node->has_strong_ref = 1;
  2484. binder_context_mgr_node->has_weak_ref = 1;
  2485. out:
  2486. return ret;
  2487. }
  2488. static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  2489. {
  2490. int ret;
  2491. struct binder_proc *proc = filp->private_data;
  2492. struct binder_thread *thread;
  2493. unsigned int size = _IOC_SIZE(cmd);
  2494. void __user *ubuf = (void __user *)arg;
  2495. /*pr_info("binder_ioctl: %d:%d %x %lx\n",
  2496. proc->pid, current->pid, cmd, arg);*/
  2497. if (unlikely(current->mm != proc->vma_vm_mm)) {
  2498. pr_err("current mm mismatch proc mm\n");
  2499. return -EINVAL;
  2500. }
  2501. trace_binder_ioctl(cmd, arg);
  2502. ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
  2503. if (ret)
  2504. goto err_unlocked;
  2505. binder_lock(__func__);
  2506. thread = binder_get_thread(proc);
  2507. if (thread == NULL) {
  2508. ret = -ENOMEM;
  2509. goto err;
  2510. }
  2511. switch (cmd) {
  2512. case BINDER_WRITE_READ:
  2513. ret = binder_ioctl_write_read(filp, cmd, arg, thread);
  2514. if (ret)
  2515. goto err;
  2516. break;
  2517. case BINDER_SET_MAX_THREADS:
  2518. if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
  2519. ret = -EINVAL;
  2520. goto err;
  2521. }
  2522. break;
  2523. case BINDER_SET_CONTEXT_MGR:
  2524. ret = binder_ioctl_set_ctx_mgr(filp);
  2525. if (ret)
  2526. goto err;
  2527. break;
  2528. case BINDER_THREAD_EXIT:
  2529. binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
  2530. proc->pid, thread->pid);
  2531. binder_free_thread(proc, thread);
  2532. thread = NULL;
  2533. break;
  2534. case BINDER_VERSION: {
  2535. struct binder_version __user *ver = ubuf;
  2536. if (size != sizeof(struct binder_version)) {
  2537. ret = -EINVAL;
  2538. goto err;
  2539. }
  2540. if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
  2541. &ver->protocol_version)) {
  2542. ret = -EINVAL;
  2543. goto err;
  2544. }
  2545. break;
  2546. }
  2547. default:
  2548. ret = -EINVAL;
  2549. goto err;
  2550. }
  2551. ret = 0;
  2552. err:
  2553. if (thread)
  2554. thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
  2555. binder_unlock(__func__);
  2556. wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
  2557. if (ret && ret != -ERESTARTSYS)
  2558. pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
  2559. err_unlocked:
  2560. trace_binder_ioctl_done(ret);
  2561. return ret;
  2562. }
  2563. static void binder_vma_open(struct vm_area_struct *vma)
  2564. {
  2565. struct binder_proc *proc = vma->vm_private_data;
  2566. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  2567. "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
  2568. proc->pid, vma->vm_start, vma->vm_end,
  2569. (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
  2570. (unsigned long)pgprot_val(vma->vm_page_prot));
  2571. }
  2572. static void binder_vma_close(struct vm_area_struct *vma)
  2573. {
  2574. struct binder_proc *proc = vma->vm_private_data;
  2575. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  2576. "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
  2577. proc->pid, vma->vm_start, vma->vm_end,
  2578. (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
  2579. (unsigned long)pgprot_val(vma->vm_page_prot));
  2580. proc->vma = NULL;
  2581. proc->vma_vm_mm = NULL;
  2582. binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
  2583. }
  2584. static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  2585. {
  2586. return VM_FAULT_SIGBUS;
  2587. }
  2588. static const struct vm_operations_struct binder_vm_ops = {
  2589. .open = binder_vma_open,
  2590. .close = binder_vma_close,
  2591. .fault = binder_vm_fault,
  2592. };
  2593. static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
  2594. {
  2595. int ret;
  2596. struct vm_struct *area;
  2597. struct binder_proc *proc = filp->private_data;
  2598. const char *failure_string;
  2599. struct binder_buffer *buffer;
  2600. if (proc->tsk != current)
  2601. return -EINVAL;
  2602. if ((vma->vm_end - vma->vm_start) > SZ_4M)
  2603. vma->vm_end = vma->vm_start + SZ_4M;
  2604. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  2605. "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
  2606. proc->pid, vma->vm_start, vma->vm_end,
  2607. (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
  2608. (unsigned long)pgprot_val(vma->vm_page_prot));
  2609. if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
  2610. ret = -EPERM;
  2611. failure_string = "bad vm_flags";
  2612. goto err_bad_arg;
  2613. }
  2614. vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
  2615. mutex_lock(&binder_mmap_lock);
  2616. if (proc->buffer) {
  2617. ret = -EBUSY;
  2618. failure_string = "already mapped";
  2619. goto err_already_mapped;
  2620. }
  2621. area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
  2622. if (area == NULL) {
  2623. ret = -ENOMEM;
  2624. failure_string = "get_vm_area";
  2625. goto err_get_vm_area_failed;
  2626. }
  2627. proc->buffer = area->addr;
  2628. proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer;
  2629. mutex_unlock(&binder_mmap_lock);
  2630. #ifdef CONFIG_CPU_CACHE_VIPT
  2631. if (cache_is_vipt_aliasing()) {
  2632. while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) {
  2633. pr_info("binder_mmap: %d %lx-%lx maps %p bad alignment\n", proc->pid, vma->vm_start, vma->vm_end, proc->buffer);
  2634. vma->vm_start += PAGE_SIZE;
  2635. }
  2636. }
  2637. #endif
  2638. proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL);
  2639. if (proc->pages == NULL) {
  2640. ret = -ENOMEM;
  2641. failure_string = "alloc page array";
  2642. goto err_alloc_pages_failed;
  2643. }
  2644. proc->buffer_size = vma->vm_end - vma->vm_start;
  2645. vma->vm_ops = &binder_vm_ops;
  2646. vma->vm_private_data = proc;
  2647. if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) {
  2648. ret = -ENOMEM;
  2649. failure_string = "alloc small buf";
  2650. goto err_alloc_small_buf_failed;
  2651. }
  2652. buffer = proc->buffer;
  2653. INIT_LIST_HEAD(&proc->buffers);
  2654. list_add(&buffer->entry, &proc->buffers);
  2655. buffer->free = 1;
  2656. binder_insert_free_buffer(proc, buffer);
  2657. proc->free_async_space = proc->buffer_size / 2;
  2658. barrier();
  2659. proc->files = get_files_struct(current);
  2660. proc->vma = vma;
  2661. proc->vma_vm_mm = vma->vm_mm;
  2662. /*pr_info("binder_mmap: %d %lx-%lx maps %p\n",
  2663. proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
  2664. return 0;
  2665. err_alloc_small_buf_failed:
  2666. kfree(proc->pages);
  2667. proc->pages = NULL;
  2668. err_alloc_pages_failed:
  2669. mutex_lock(&binder_mmap_lock);
  2670. vfree(proc->buffer);
  2671. proc->buffer = NULL;
  2672. err_get_vm_area_failed:
  2673. err_already_mapped:
  2674. mutex_unlock(&binder_mmap_lock);
  2675. err_bad_arg:
  2676. pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
  2677. proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
  2678. return ret;
  2679. }
  2680. static int binder_open(struct inode *nodp, struct file *filp)
  2681. {
  2682. struct binder_proc *proc;
  2683. binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
  2684. current->group_leader->pid, current->pid);
  2685. proc = kzalloc(sizeof(*proc), GFP_KERNEL);
  2686. if (proc == NULL)
  2687. return -ENOMEM;
  2688. get_task_struct(current);
  2689. proc->tsk = current;
  2690. proc->vma_vm_mm = current->mm;
  2691. INIT_LIST_HEAD(&proc->todo);
  2692. init_waitqueue_head(&proc->wait);
  2693. proc->default_priority = task_nice(current);
  2694. binder_lock(__func__);
  2695. binder_stats_created(BINDER_STAT_PROC);
  2696. hlist_add_head(&proc->proc_node, &binder_procs);
  2697. proc->pid = current->group_leader->pid;
  2698. INIT_LIST_HEAD(&proc->delivered_death);
  2699. filp->private_data = proc;
  2700. binder_unlock(__func__);
  2701. if (binder_debugfs_dir_entry_proc) {
  2702. char strbuf[11];
  2703. snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
  2704. proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
  2705. binder_debugfs_dir_entry_proc, proc, &binder_proc_fops);
  2706. }
  2707. return 0;
  2708. }
  2709. static int binder_flush(struct file *filp, fl_owner_t id)
  2710. {
  2711. struct binder_proc *proc = filp->private_data;
  2712. binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
  2713. return 0;
  2714. }
  2715. static void binder_deferred_flush(struct binder_proc *proc)
  2716. {
  2717. struct rb_node *n;
  2718. int wake_count = 0;
  2719. for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
  2720. struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
  2721. thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
  2722. if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
  2723. wake_up_interruptible(&thread->wait);
  2724. wake_count++;
  2725. }
  2726. }
  2727. wake_up_interruptible_all(&proc->wait);
  2728. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  2729. "binder_flush: %d woke %d threads\n", proc->pid,
  2730. wake_count);
  2731. }
  2732. static int binder_release(struct inode *nodp, struct file *filp)
  2733. {
  2734. struct binder_proc *proc = filp->private_data;
  2735. debugfs_remove(proc->debugfs_entry);
  2736. binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
  2737. return 0;
  2738. }
  2739. static int binder_node_release(struct binder_node *node, int refs)
  2740. {
  2741. struct binder_ref *ref;
  2742. int death = 0;
  2743. list_del_init(&node->work.entry);
  2744. binder_release_work(&node->async_todo);
  2745. if (hlist_empty(&node->refs)) {
  2746. kfree(node);
  2747. binder_stats_deleted(BINDER_STAT_NODE);
  2748. return refs;
  2749. }
  2750. node->proc = NULL;
  2751. node->local_strong_refs = 0;
  2752. node->local_weak_refs = 0;
  2753. hlist_add_head(&node->dead_node, &binder_dead_nodes);
  2754. hlist_for_each_entry(ref, &node->refs, node_entry) {
  2755. refs++;
  2756. if (!ref->death)
  2757. continue;
  2758. death++;
  2759. if (list_empty(&ref->death->work.entry)) {
  2760. ref->death->work.type = BINDER_WORK_DEAD_BINDER;
  2761. list_add_tail(&ref->death->work.entry,
  2762. &ref->proc->todo);
  2763. wake_up_interruptible(&ref->proc->wait);
  2764. } else
  2765. BUG();
  2766. }
  2767. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  2768. "node %d now dead, refs %d, death %d\n",
  2769. node->debug_id, refs, death);
  2770. return refs;
  2771. }
  2772. static void binder_deferred_release(struct binder_proc *proc)
  2773. {
  2774. struct binder_transaction *t;
  2775. struct rb_node *n;
  2776. int threads, nodes, incoming_refs, outgoing_refs, buffers,
  2777. active_transactions, page_count;
  2778. BUG_ON(proc->vma);
  2779. BUG_ON(proc->files);
  2780. hlist_del(&proc->proc_node);
  2781. if (binder_context_mgr_node && binder_context_mgr_node->proc == proc) {
  2782. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  2783. "%s: %d context_mgr_node gone\n",
  2784. __func__, proc->pid);
  2785. binder_context_mgr_node = NULL;
  2786. }
  2787. threads = 0;
  2788. active_transactions = 0;
  2789. while ((n = rb_first(&proc->threads))) {
  2790. struct binder_thread *thread;
  2791. thread = rb_entry(n, struct binder_thread, rb_node);
  2792. threads++;
  2793. active_transactions += binder_free_thread(proc, thread);
  2794. }
  2795. nodes = 0;
  2796. incoming_refs = 0;
  2797. while ((n = rb_first(&proc->nodes))) {
  2798. struct binder_node *node;
  2799. node = rb_entry(n, struct binder_node, rb_node);
  2800. nodes++;
  2801. rb_erase(&node->rb_node, &proc->nodes);
  2802. incoming_refs = binder_node_release(node, incoming_refs);
  2803. }
  2804. outgoing_refs = 0;
  2805. while ((n = rb_first(&proc->refs_by_desc))) {
  2806. struct binder_ref *ref;
  2807. ref = rb_entry(n, struct binder_ref, rb_node_desc);
  2808. outgoing_refs++;
  2809. binder_delete_ref(ref);
  2810. }
  2811. binder_release_work(&proc->todo);
  2812. binder_release_work(&proc->delivered_death);
  2813. buffers = 0;
  2814. while ((n = rb_first(&proc->allocated_buffers))) {
  2815. struct binder_buffer *buffer;
  2816. buffer = rb_entry(n, struct binder_buffer, rb_node);
  2817. t = buffer->transaction;
  2818. if (t) {
  2819. t->buffer = NULL;
  2820. buffer->transaction = NULL;
  2821. pr_err("release proc %d, transaction %d, not freed\n",
  2822. proc->pid, t->debug_id);
  2823. /*BUG();*/
  2824. }
  2825. binder_free_buf(proc, buffer);
  2826. buffers++;
  2827. }
  2828. binder_stats_deleted(BINDER_STAT_PROC);
  2829. page_count = 0;
  2830. if (proc->pages) {
  2831. int i;
  2832. for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) {
  2833. void *page_addr;
  2834. if (!proc->pages[i])
  2835. continue;
  2836. page_addr = proc->buffer + i * PAGE_SIZE;
  2837. binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
  2838. "%s: %d: page %d at %p not freed\n",
  2839. __func__, proc->pid, i, page_addr);
  2840. unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
  2841. __free_page(proc->pages[i]);
  2842. page_count++;
  2843. }
  2844. kfree(proc->pages);
  2845. vfree(proc->buffer);
  2846. }
  2847. put_task_struct(proc->tsk);
  2848. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  2849. "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d, buffers %d, pages %d\n",
  2850. __func__, proc->pid, threads, nodes, incoming_refs,
  2851. outgoing_refs, active_transactions, buffers, page_count);
  2852. kfree(proc);
  2853. }
  2854. static void binder_deferred_func(struct work_struct *work)
  2855. {
  2856. struct binder_proc *proc;
  2857. struct files_struct *files;
  2858. int defer;
  2859. do {
  2860. binder_lock(__func__);
  2861. mutex_lock(&binder_deferred_lock);
  2862. if (!hlist_empty(&binder_deferred_list)) {
  2863. proc = hlist_entry(binder_deferred_list.first,
  2864. struct binder_proc, deferred_work_node);
  2865. hlist_del_init(&proc->deferred_work_node);
  2866. defer = proc->deferred_work;
  2867. proc->deferred_work = 0;
  2868. } else {
  2869. proc = NULL;
  2870. defer = 0;
  2871. }
  2872. mutex_unlock(&binder_deferred_lock);
  2873. files = NULL;
  2874. if (defer & BINDER_DEFERRED_PUT_FILES) {
  2875. files = proc->files;
  2876. if (files)
  2877. proc->files = NULL;
  2878. }
  2879. if (defer & BINDER_DEFERRED_FLUSH)
  2880. binder_deferred_flush(proc);
  2881. if (defer & BINDER_DEFERRED_RELEASE)
  2882. binder_deferred_release(proc); /* frees proc */
  2883. binder_unlock(__func__);
  2884. if (files)
  2885. put_files_struct(files);
  2886. } while (proc);
  2887. }
  2888. static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
  2889. static void
  2890. binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
  2891. {
  2892. mutex_lock(&binder_deferred_lock);
  2893. proc->deferred_work |= defer;
  2894. if (hlist_unhashed(&proc->deferred_work_node)) {
  2895. hlist_add_head(&proc->deferred_work_node,
  2896. &binder_deferred_list);
  2897. schedule_work(&binder_deferred_work);
  2898. }
  2899. mutex_unlock(&binder_deferred_lock);
  2900. }
  2901. static void print_binder_transaction(struct seq_file *m, const char *prefix,
  2902. struct binder_transaction *t)
  2903. {
  2904. seq_printf(m,
  2905. "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
  2906. prefix, t->debug_id, t,
  2907. t->from ? t->from->proc->pid : 0,
  2908. t->from ? t->from->pid : 0,
  2909. t->to_proc ? t->to_proc->pid : 0,
  2910. t->to_thread ? t->to_thread->pid : 0,
  2911. t->code, t->flags, t->priority, t->need_reply);
  2912. if (t->buffer == NULL) {
  2913. seq_puts(m, " buffer free\n");
  2914. return;
  2915. }
  2916. if (t->buffer->target_node)
  2917. seq_printf(m, " node %d",
  2918. t->buffer->target_node->debug_id);
  2919. seq_printf(m, " size %zd:%zd data %p\n",
  2920. t->buffer->data_size, t->buffer->offsets_size,
  2921. t->buffer->data);
  2922. }
  2923. static void print_binder_buffer(struct seq_file *m, const char *prefix,
  2924. struct binder_buffer *buffer)
  2925. {
  2926. seq_printf(m, "%s %d: %p size %zd:%zd %s\n",
  2927. prefix, buffer->debug_id, buffer->data,
  2928. buffer->data_size, buffer->offsets_size,
  2929. buffer->transaction ? "active" : "delivered");
  2930. }
  2931. static void print_binder_work(struct seq_file *m, const char *prefix,
  2932. const char *transaction_prefix,
  2933. struct binder_work *w)
  2934. {
  2935. struct binder_node *node;
  2936. struct binder_transaction *t;
  2937. switch (w->type) {
  2938. case BINDER_WORK_TRANSACTION:
  2939. t = container_of(w, struct binder_transaction, work);
  2940. print_binder_transaction(m, transaction_prefix, t);
  2941. break;
  2942. case BINDER_WORK_TRANSACTION_COMPLETE:
  2943. seq_printf(m, "%stransaction complete\n", prefix);
  2944. break;
  2945. case BINDER_WORK_NODE:
  2946. node = container_of(w, struct binder_node, work);
  2947. seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
  2948. prefix, node->debug_id,
  2949. (u64)node->ptr, (u64)node->cookie);
  2950. break;
  2951. case BINDER_WORK_DEAD_BINDER:
  2952. seq_printf(m, "%shas dead binder\n", prefix);
  2953. break;
  2954. case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
  2955. seq_printf(m, "%shas cleared dead binder\n", prefix);
  2956. break;
  2957. case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
  2958. seq_printf(m, "%shas cleared death notification\n", prefix);
  2959. break;
  2960. default:
  2961. seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
  2962. break;
  2963. }
  2964. }
  2965. static void print_binder_thread(struct seq_file *m,
  2966. struct binder_thread *thread,
  2967. int print_always)
  2968. {
  2969. struct binder_transaction *t;
  2970. struct binder_work *w;
  2971. size_t start_pos = m->count;
  2972. size_t header_pos;
  2973. seq_printf(m, " thread %d: l %02x\n", thread->pid, thread->looper);
  2974. header_pos = m->count;
  2975. t = thread->transaction_stack;
  2976. while (t) {
  2977. if (t->from == thread) {
  2978. print_binder_transaction(m,
  2979. " outgoing transaction", t);
  2980. t = t->from_parent;
  2981. } else if (t->to_thread == thread) {
  2982. print_binder_transaction(m,
  2983. " incoming transaction", t);
  2984. t = t->to_parent;
  2985. } else {
  2986. print_binder_transaction(m, " bad transaction", t);
  2987. t = NULL;
  2988. }
  2989. }
  2990. list_for_each_entry(w, &thread->todo, entry) {
  2991. print_binder_work(m, " ", " pending transaction", w);
  2992. }
  2993. if (!print_always && m->count == header_pos)
  2994. m->count = start_pos;
  2995. }
  2996. static void print_binder_node(struct seq_file *m, struct binder_node *node)
  2997. {
  2998. struct binder_ref *ref;
  2999. struct binder_work *w;
  3000. int count;
  3001. count = 0;
  3002. hlist_for_each_entry(ref, &node->refs, node_entry)
  3003. count++;
  3004. seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d",
  3005. node->debug_id, (u64)node->ptr, (u64)node->cookie,
  3006. node->has_strong_ref, node->has_weak_ref,
  3007. node->local_strong_refs, node->local_weak_refs,
  3008. node->internal_strong_refs, count);
  3009. if (count) {
  3010. seq_puts(m, " proc");
  3011. hlist_for_each_entry(ref, &node->refs, node_entry)
  3012. seq_printf(m, " %d", ref->proc->pid);
  3013. }
  3014. seq_puts(m, "\n");
  3015. list_for_each_entry(w, &node->async_todo, entry)
  3016. print_binder_work(m, " ",
  3017. " pending async transaction", w);
  3018. }
  3019. static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
  3020. {
  3021. seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %p\n",
  3022. ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
  3023. ref->node->debug_id, ref->strong, ref->weak, ref->death);
  3024. }
  3025. static void print_binder_proc(struct seq_file *m,
  3026. struct binder_proc *proc, int print_all)
  3027. {
  3028. struct binder_work *w;
  3029. struct rb_node *n;
  3030. size_t start_pos = m->count;
  3031. size_t header_pos;
  3032. seq_printf(m, "proc %d\n", proc->pid);
  3033. header_pos = m->count;
  3034. for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
  3035. print_binder_thread(m, rb_entry(n, struct binder_thread,
  3036. rb_node), print_all);
  3037. for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
  3038. struct binder_node *node = rb_entry(n, struct binder_node,
  3039. rb_node);
  3040. if (print_all || node->has_async_transaction)
  3041. print_binder_node(m, node);
  3042. }
  3043. if (print_all) {
  3044. for (n = rb_first(&proc->refs_by_desc);
  3045. n != NULL;
  3046. n = rb_next(n))
  3047. print_binder_ref(m, rb_entry(n, struct binder_ref,
  3048. rb_node_desc));
  3049. }
  3050. for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
  3051. print_binder_buffer(m, " buffer",
  3052. rb_entry(n, struct binder_buffer, rb_node));
  3053. list_for_each_entry(w, &proc->todo, entry)
  3054. print_binder_work(m, " ", " pending transaction", w);
  3055. list_for_each_entry(w, &proc->delivered_death, entry) {
  3056. seq_puts(m, " has delivered dead binder\n");
  3057. break;
  3058. }
  3059. if (!print_all && m->count == header_pos)
  3060. m->count = start_pos;
  3061. }
  3062. static const char * const binder_return_strings[] = {
  3063. "BR_ERROR",
  3064. "BR_OK",
  3065. "BR_TRANSACTION",
  3066. "BR_REPLY",
  3067. "BR_ACQUIRE_RESULT",
  3068. "BR_DEAD_REPLY",
  3069. "BR_TRANSACTION_COMPLETE",
  3070. "BR_INCREFS",
  3071. "BR_ACQUIRE",
  3072. "BR_RELEASE",
  3073. "BR_DECREFS",
  3074. "BR_ATTEMPT_ACQUIRE",
  3075. "BR_NOOP",
  3076. "BR_SPAWN_LOOPER",
  3077. "BR_FINISHED",
  3078. "BR_DEAD_BINDER",
  3079. "BR_CLEAR_DEATH_NOTIFICATION_DONE",
  3080. "BR_FAILED_REPLY"
  3081. };
  3082. static const char * const binder_command_strings[] = {
  3083. "BC_TRANSACTION",
  3084. "BC_REPLY",
  3085. "BC_ACQUIRE_RESULT",
  3086. "BC_FREE_BUFFER",
  3087. "BC_INCREFS",
  3088. "BC_ACQUIRE",
  3089. "BC_RELEASE",
  3090. "BC_DECREFS",
  3091. "BC_INCREFS_DONE",
  3092. "BC_ACQUIRE_DONE",
  3093. "BC_ATTEMPT_ACQUIRE",
  3094. "BC_REGISTER_LOOPER",
  3095. "BC_ENTER_LOOPER",
  3096. "BC_EXIT_LOOPER",
  3097. "BC_REQUEST_DEATH_NOTIFICATION",
  3098. "BC_CLEAR_DEATH_NOTIFICATION",
  3099. "BC_DEAD_BINDER_DONE"
  3100. };
  3101. static const char * const binder_objstat_strings[] = {
  3102. "proc",
  3103. "thread",
  3104. "node",
  3105. "ref",
  3106. "death",
  3107. "transaction",
  3108. "transaction_complete"
  3109. };
  3110. static void print_binder_stats(struct seq_file *m, const char *prefix,
  3111. struct binder_stats *stats)
  3112. {
  3113. int i;
  3114. BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
  3115. ARRAY_SIZE(binder_command_strings));
  3116. for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
  3117. if (stats->bc[i])
  3118. seq_printf(m, "%s%s: %d\n", prefix,
  3119. binder_command_strings[i], stats->bc[i]);
  3120. }
  3121. BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
  3122. ARRAY_SIZE(binder_return_strings));
  3123. for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
  3124. if (stats->br[i])
  3125. seq_printf(m, "%s%s: %d\n", prefix,
  3126. binder_return_strings[i], stats->br[i]);
  3127. }
  3128. BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
  3129. ARRAY_SIZE(binder_objstat_strings));
  3130. BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
  3131. ARRAY_SIZE(stats->obj_deleted));
  3132. for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
  3133. if (stats->obj_created[i] || stats->obj_deleted[i])
  3134. seq_printf(m, "%s%s: active %d total %d\n", prefix,
  3135. binder_objstat_strings[i],
  3136. stats->obj_created[i] - stats->obj_deleted[i],
  3137. stats->obj_created[i]);
  3138. }
  3139. }
  3140. static void print_binder_proc_stats(struct seq_file *m,
  3141. struct binder_proc *proc)
  3142. {
  3143. struct binder_work *w;
  3144. struct rb_node *n;
  3145. int count, strong, weak;
  3146. seq_printf(m, "proc %d\n", proc->pid);
  3147. count = 0;
  3148. for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
  3149. count++;
  3150. seq_printf(m, " threads: %d\n", count);
  3151. seq_printf(m, " requested threads: %d+%d/%d\n"
  3152. " ready threads %d\n"
  3153. " free async space %zd\n", proc->requested_threads,
  3154. proc->requested_threads_started, proc->max_threads,
  3155. proc->ready_threads, proc->free_async_space);
  3156. count = 0;
  3157. for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
  3158. count++;
  3159. seq_printf(m, " nodes: %d\n", count);
  3160. count = 0;
  3161. strong = 0;
  3162. weak = 0;
  3163. for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
  3164. struct binder_ref *ref = rb_entry(n, struct binder_ref,
  3165. rb_node_desc);
  3166. count++;
  3167. strong += ref->strong;
  3168. weak += ref->weak;
  3169. }
  3170. seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
  3171. count = 0;
  3172. for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
  3173. count++;
  3174. seq_printf(m, " buffers: %d\n", count);
  3175. count = 0;
  3176. list_for_each_entry(w, &proc->todo, entry) {
  3177. switch (w->type) {
  3178. case BINDER_WORK_TRANSACTION:
  3179. count++;
  3180. break;
  3181. default:
  3182. break;
  3183. }
  3184. }
  3185. seq_printf(m, " pending transactions: %d\n", count);
  3186. print_binder_stats(m, " ", &proc->stats);
  3187. }
  3188. static int binder_state_show(struct seq_file *m, void *unused)
  3189. {
  3190. struct binder_proc *proc;
  3191. struct binder_node *node;
  3192. int do_lock = !binder_debug_no_lock;
  3193. if (do_lock)
  3194. binder_lock(__func__);
  3195. seq_puts(m, "binder state:\n");
  3196. if (!hlist_empty(&binder_dead_nodes))
  3197. seq_puts(m, "dead nodes:\n");
  3198. hlist_for_each_entry(node, &binder_dead_nodes, dead_node)
  3199. print_binder_node(m, node);
  3200. hlist_for_each_entry(proc, &binder_procs, proc_node)
  3201. print_binder_proc(m, proc, 1);
  3202. if (do_lock)
  3203. binder_unlock(__func__);
  3204. return 0;
  3205. }
  3206. static int binder_stats_show(struct seq_file *m, void *unused)
  3207. {
  3208. struct binder_proc *proc;
  3209. int do_lock = !binder_debug_no_lock;
  3210. if (do_lock)
  3211. binder_lock(__func__);
  3212. seq_puts(m, "binder stats:\n");
  3213. print_binder_stats(m, "", &binder_stats);
  3214. hlist_for_each_entry(proc, &binder_procs, proc_node)
  3215. print_binder_proc_stats(m, proc);
  3216. if (do_lock)
  3217. binder_unlock(__func__);
  3218. return 0;
  3219. }
  3220. static int binder_transactions_show(struct seq_file *m, void *unused)
  3221. {
  3222. struct binder_proc *proc;
  3223. int do_lock = !binder_debug_no_lock;
  3224. if (do_lock)
  3225. binder_lock(__func__);
  3226. seq_puts(m, "binder transactions:\n");
  3227. hlist_for_each_entry(proc, &binder_procs, proc_node)
  3228. print_binder_proc(m, proc, 0);
  3229. if (do_lock)
  3230. binder_unlock(__func__);
  3231. return 0;
  3232. }
  3233. static int binder_proc_show(struct seq_file *m, void *unused)
  3234. {
  3235. struct binder_proc *itr;
  3236. struct binder_proc *proc = m->private;
  3237. int do_lock = !binder_debug_no_lock;
  3238. bool valid_proc = false;
  3239. if (do_lock)
  3240. binder_lock(__func__);
  3241. hlist_for_each_entry(itr, &binder_procs, proc_node) {
  3242. if (itr == proc) {
  3243. valid_proc = true;
  3244. break;
  3245. }
  3246. }
  3247. if (valid_proc) {
  3248. seq_puts(m, "binder proc state:\n");
  3249. print_binder_proc(m, proc, 1);
  3250. }
  3251. if (do_lock)
  3252. binder_unlock(__func__);
  3253. return 0;
  3254. }
  3255. static void print_binder_transaction_log_entry(struct seq_file *m,
  3256. struct binder_transaction_log_entry *e)
  3257. {
  3258. seq_printf(m,
  3259. "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
  3260. e->debug_id, (e->call_type == 2) ? "reply" :
  3261. ((e->call_type == 1) ? "async" : "call "), e->from_proc,
  3262. e->from_thread, e->to_proc, e->to_thread, e->to_node,
  3263. e->target_handle, e->data_size, e->offsets_size);
  3264. }
  3265. static int binder_transaction_log_show(struct seq_file *m, void *unused)
  3266. {
  3267. struct binder_transaction_log *log = m->private;
  3268. int i;
  3269. if (log->full) {
  3270. for (i = log->next; i < ARRAY_SIZE(log->entry); i++)
  3271. print_binder_transaction_log_entry(m, &log->entry[i]);
  3272. }
  3273. for (i = 0; i < log->next; i++)
  3274. print_binder_transaction_log_entry(m, &log->entry[i]);
  3275. return 0;
  3276. }
  3277. static const struct file_operations binder_fops = {
  3278. .owner = THIS_MODULE,
  3279. .poll = binder_poll,
  3280. .unlocked_ioctl = binder_ioctl,
  3281. .compat_ioctl = binder_ioctl,
  3282. .mmap = binder_mmap,
  3283. .open = binder_open,
  3284. .flush = binder_flush,
  3285. .release = binder_release,
  3286. };
  3287. static struct miscdevice binder_miscdev = {
  3288. .minor = MISC_DYNAMIC_MINOR,
  3289. .name = "binder",
  3290. .fops = &binder_fops
  3291. };
  3292. BINDER_DEBUG_ENTRY(state);
  3293. BINDER_DEBUG_ENTRY(stats);
  3294. BINDER_DEBUG_ENTRY(transactions);
  3295. BINDER_DEBUG_ENTRY(transaction_log);
  3296. static int __init binder_init(void)
  3297. {
  3298. int ret;
  3299. binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
  3300. if (binder_debugfs_dir_entry_root)
  3301. binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
  3302. binder_debugfs_dir_entry_root);
  3303. ret = misc_register(&binder_miscdev);
  3304. if (binder_debugfs_dir_entry_root) {
  3305. debugfs_create_file("state",
  3306. S_IRUGO,
  3307. binder_debugfs_dir_entry_root,
  3308. NULL,
  3309. &binder_state_fops);
  3310. debugfs_create_file("stats",
  3311. S_IRUGO,
  3312. binder_debugfs_dir_entry_root,
  3313. NULL,
  3314. &binder_stats_fops);
  3315. debugfs_create_file("transactions",
  3316. S_IRUGO,
  3317. binder_debugfs_dir_entry_root,
  3318. NULL,
  3319. &binder_transactions_fops);
  3320. debugfs_create_file("transaction_log",
  3321. S_IRUGO,
  3322. binder_debugfs_dir_entry_root,
  3323. &binder_transaction_log,
  3324. &binder_transaction_log_fops);
  3325. debugfs_create_file("failed_transaction_log",
  3326. S_IRUGO,
  3327. binder_debugfs_dir_entry_root,
  3328. &binder_transaction_log_failed,
  3329. &binder_transaction_log_fops);
  3330. }
  3331. return ret;
  3332. }
  3333. device_initcall(binder_init);
  3334. #define CREATE_TRACE_POINTS
  3335. #include "binder_trace.h"
  3336. MODULE_LICENSE("GPL v2");