binder.c 107 KB

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