binder.c 101 KB

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