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