binder.c 102 KB

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