binder.c 102 KB

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