binder.c 119 KB

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