binder.c 158 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565
  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. /*
  18. * Locking overview
  19. *
  20. * There are 3 main spinlocks which must be acquired in the
  21. * order shown:
  22. *
  23. * 1) proc->outer_lock : protects binder_ref
  24. * binder_proc_lock() and binder_proc_unlock() are
  25. * used to acq/rel.
  26. * 2) node->lock : protects most fields of binder_node.
  27. * binder_node_lock() and binder_node_unlock() are
  28. * used to acq/rel
  29. * 3) proc->inner_lock : protects the thread and node lists
  30. * (proc->threads, proc->waiting_threads, proc->nodes)
  31. * and all todo lists associated with the binder_proc
  32. * (proc->todo, thread->todo, proc->delivered_death and
  33. * node->async_todo), as well as thread->transaction_stack
  34. * binder_inner_proc_lock() and binder_inner_proc_unlock()
  35. * are used to acq/rel
  36. *
  37. * Any lock under procA must never be nested under any lock at the same
  38. * level or below on procB.
  39. *
  40. * Functions that require a lock held on entry indicate which lock
  41. * in the suffix of the function name:
  42. *
  43. * foo_olocked() : requires node->outer_lock
  44. * foo_nlocked() : requires node->lock
  45. * foo_ilocked() : requires proc->inner_lock
  46. * foo_oilocked(): requires proc->outer_lock and proc->inner_lock
  47. * foo_nilocked(): requires node->lock and proc->inner_lock
  48. * ...
  49. */
  50. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  51. #include <asm/cacheflush.h>
  52. #include <linux/fdtable.h>
  53. #include <linux/file.h>
  54. #include <linux/freezer.h>
  55. #include <linux/fs.h>
  56. #include <linux/list.h>
  57. #include <linux/miscdevice.h>
  58. #include <linux/module.h>
  59. #include <linux/mutex.h>
  60. #include <linux/nsproxy.h>
  61. #include <linux/poll.h>
  62. #include <linux/debugfs.h>
  63. #include <linux/rbtree.h>
  64. #include <linux/sched/signal.h>
  65. #include <linux/sched/mm.h>
  66. #include <linux/seq_file.h>
  67. #include <linux/uaccess.h>
  68. #include <linux/pid_namespace.h>
  69. #include <linux/security.h>
  70. #include <linux/spinlock.h>
  71. #ifdef CONFIG_ANDROID_BINDER_IPC_32BIT
  72. #define BINDER_IPC_32BIT 1
  73. #endif
  74. #include <uapi/linux/android/binder.h>
  75. #include "binder_alloc.h"
  76. #include "binder_trace.h"
  77. static HLIST_HEAD(binder_deferred_list);
  78. static DEFINE_MUTEX(binder_deferred_lock);
  79. static HLIST_HEAD(binder_devices);
  80. static HLIST_HEAD(binder_procs);
  81. static DEFINE_MUTEX(binder_procs_lock);
  82. static HLIST_HEAD(binder_dead_nodes);
  83. static DEFINE_SPINLOCK(binder_dead_nodes_lock);
  84. static struct dentry *binder_debugfs_dir_entry_root;
  85. static struct dentry *binder_debugfs_dir_entry_proc;
  86. static atomic_t binder_last_id;
  87. #define BINDER_DEBUG_ENTRY(name) \
  88. static int binder_##name##_open(struct inode *inode, struct file *file) \
  89. { \
  90. return single_open(file, binder_##name##_show, inode->i_private); \
  91. } \
  92. \
  93. static const struct file_operations binder_##name##_fops = { \
  94. .owner = THIS_MODULE, \
  95. .open = binder_##name##_open, \
  96. .read = seq_read, \
  97. .llseek = seq_lseek, \
  98. .release = single_release, \
  99. }
  100. static int binder_proc_show(struct seq_file *m, void *unused);
  101. BINDER_DEBUG_ENTRY(proc);
  102. /* This is only defined in include/asm-arm/sizes.h */
  103. #ifndef SZ_1K
  104. #define SZ_1K 0x400
  105. #endif
  106. #ifndef SZ_4M
  107. #define SZ_4M 0x400000
  108. #endif
  109. #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
  110. enum {
  111. BINDER_DEBUG_USER_ERROR = 1U << 0,
  112. BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
  113. BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
  114. BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
  115. BINDER_DEBUG_DEAD_BINDER = 1U << 4,
  116. BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
  117. BINDER_DEBUG_READ_WRITE = 1U << 6,
  118. BINDER_DEBUG_USER_REFS = 1U << 7,
  119. BINDER_DEBUG_THREADS = 1U << 8,
  120. BINDER_DEBUG_TRANSACTION = 1U << 9,
  121. BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
  122. BINDER_DEBUG_FREE_BUFFER = 1U << 11,
  123. BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
  124. BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
  125. BINDER_DEBUG_SPINLOCKS = 1U << 14,
  126. };
  127. static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
  128. BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
  129. module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
  130. static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
  131. module_param_named(devices, binder_devices_param, charp, 0444);
  132. static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
  133. static int binder_stop_on_user_error;
  134. static int binder_set_stop_on_user_error(const char *val,
  135. struct kernel_param *kp)
  136. {
  137. int ret;
  138. ret = param_set_int(val, kp);
  139. if (binder_stop_on_user_error < 2)
  140. wake_up(&binder_user_error_wait);
  141. return ret;
  142. }
  143. module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
  144. param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
  145. #define binder_debug(mask, x...) \
  146. do { \
  147. if (binder_debug_mask & mask) \
  148. pr_info(x); \
  149. } while (0)
  150. #define binder_user_error(x...) \
  151. do { \
  152. if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
  153. pr_info(x); \
  154. if (binder_stop_on_user_error) \
  155. binder_stop_on_user_error = 2; \
  156. } while (0)
  157. #define to_flat_binder_object(hdr) \
  158. container_of(hdr, struct flat_binder_object, hdr)
  159. #define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
  160. #define to_binder_buffer_object(hdr) \
  161. container_of(hdr, struct binder_buffer_object, hdr)
  162. #define to_binder_fd_array_object(hdr) \
  163. container_of(hdr, struct binder_fd_array_object, hdr)
  164. enum binder_stat_types {
  165. BINDER_STAT_PROC,
  166. BINDER_STAT_THREAD,
  167. BINDER_STAT_NODE,
  168. BINDER_STAT_REF,
  169. BINDER_STAT_DEATH,
  170. BINDER_STAT_TRANSACTION,
  171. BINDER_STAT_TRANSACTION_COMPLETE,
  172. BINDER_STAT_COUNT
  173. };
  174. struct binder_stats {
  175. atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1];
  176. atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
  177. atomic_t obj_created[BINDER_STAT_COUNT];
  178. atomic_t obj_deleted[BINDER_STAT_COUNT];
  179. };
  180. static struct binder_stats binder_stats;
  181. static inline void binder_stats_deleted(enum binder_stat_types type)
  182. {
  183. atomic_inc(&binder_stats.obj_deleted[type]);
  184. }
  185. static inline void binder_stats_created(enum binder_stat_types type)
  186. {
  187. atomic_inc(&binder_stats.obj_created[type]);
  188. }
  189. struct binder_transaction_log_entry {
  190. int debug_id;
  191. int debug_id_done;
  192. int call_type;
  193. int from_proc;
  194. int from_thread;
  195. int target_handle;
  196. int to_proc;
  197. int to_thread;
  198. int to_node;
  199. int data_size;
  200. int offsets_size;
  201. int return_error_line;
  202. uint32_t return_error;
  203. uint32_t return_error_param;
  204. const char *context_name;
  205. };
  206. struct binder_transaction_log {
  207. atomic_t cur;
  208. bool full;
  209. struct binder_transaction_log_entry entry[32];
  210. };
  211. static struct binder_transaction_log binder_transaction_log;
  212. static struct binder_transaction_log binder_transaction_log_failed;
  213. static struct binder_transaction_log_entry *binder_transaction_log_add(
  214. struct binder_transaction_log *log)
  215. {
  216. struct binder_transaction_log_entry *e;
  217. unsigned int cur = atomic_inc_return(&log->cur);
  218. if (cur >= ARRAY_SIZE(log->entry))
  219. log->full = 1;
  220. e = &log->entry[cur % ARRAY_SIZE(log->entry)];
  221. WRITE_ONCE(e->debug_id_done, 0);
  222. /*
  223. * write-barrier to synchronize access to e->debug_id_done.
  224. * We make sure the initialized 0 value is seen before
  225. * memset() other fields are zeroed by memset.
  226. */
  227. smp_wmb();
  228. memset(e, 0, sizeof(*e));
  229. return e;
  230. }
  231. struct binder_context {
  232. struct binder_node *binder_context_mgr_node;
  233. struct mutex context_mgr_node_lock;
  234. kuid_t binder_context_mgr_uid;
  235. const char *name;
  236. };
  237. struct binder_device {
  238. struct hlist_node hlist;
  239. struct miscdevice miscdev;
  240. struct binder_context context;
  241. };
  242. /**
  243. * struct binder_work - work enqueued on a worklist
  244. * @entry: node enqueued on list
  245. * @type: type of work to be performed
  246. *
  247. * There are separate work lists for proc, thread, and node (async).
  248. */
  249. struct binder_work {
  250. struct list_head entry;
  251. enum {
  252. BINDER_WORK_TRANSACTION = 1,
  253. BINDER_WORK_TRANSACTION_COMPLETE,
  254. BINDER_WORK_RETURN_ERROR,
  255. BINDER_WORK_NODE,
  256. BINDER_WORK_DEAD_BINDER,
  257. BINDER_WORK_DEAD_BINDER_AND_CLEAR,
  258. BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
  259. } type;
  260. };
  261. struct binder_error {
  262. struct binder_work work;
  263. uint32_t cmd;
  264. };
  265. /**
  266. * struct binder_node - binder node bookkeeping
  267. * @debug_id: unique ID for debugging
  268. * (invariant after initialized)
  269. * @lock: lock for node fields
  270. * @work: worklist element for node work
  271. * (protected by @proc->inner_lock)
  272. * @rb_node: element for proc->nodes tree
  273. * (protected by @proc->inner_lock)
  274. * @dead_node: element for binder_dead_nodes list
  275. * (protected by binder_dead_nodes_lock)
  276. * @proc: binder_proc that owns this node
  277. * (invariant after initialized)
  278. * @refs: list of references on this node
  279. * (protected by @lock)
  280. * @internal_strong_refs: used to take strong references when
  281. * initiating a transaction
  282. * (protected by @proc->inner_lock if @proc
  283. * and by @lock)
  284. * @local_weak_refs: weak user refs from local process
  285. * (protected by @proc->inner_lock if @proc
  286. * and by @lock)
  287. * @local_strong_refs: strong user refs from local process
  288. * (protected by @proc->inner_lock if @proc
  289. * and by @lock)
  290. * @tmp_refs: temporary kernel refs
  291. * (protected by @proc->inner_lock while @proc
  292. * is valid, and by binder_dead_nodes_lock
  293. * if @proc is NULL. During inc/dec and node release
  294. * it is also protected by @lock to provide safety
  295. * as the node dies and @proc becomes NULL)
  296. * @ptr: userspace pointer for node
  297. * (invariant, no lock needed)
  298. * @cookie: userspace cookie for node
  299. * (invariant, no lock needed)
  300. * @has_strong_ref: userspace notified of strong ref
  301. * (protected by @proc->inner_lock if @proc
  302. * and by @lock)
  303. * @pending_strong_ref: userspace has acked notification of strong ref
  304. * (protected by @proc->inner_lock if @proc
  305. * and by @lock)
  306. * @has_weak_ref: userspace notified of weak ref
  307. * (protected by @proc->inner_lock if @proc
  308. * and by @lock)
  309. * @pending_weak_ref: userspace has acked notification of weak ref
  310. * (protected by @proc->inner_lock if @proc
  311. * and by @lock)
  312. * @has_async_transaction: async transaction to node in progress
  313. * (protected by @lock)
  314. * @accept_fds: file descriptor operations supported for node
  315. * (invariant after initialized)
  316. * @min_priority: minimum scheduling priority
  317. * (invariant after initialized)
  318. * @async_todo: list of async work items
  319. * (protected by @proc->inner_lock)
  320. *
  321. * Bookkeeping structure for binder nodes.
  322. */
  323. struct binder_node {
  324. int debug_id;
  325. spinlock_t lock;
  326. struct binder_work work;
  327. union {
  328. struct rb_node rb_node;
  329. struct hlist_node dead_node;
  330. };
  331. struct binder_proc *proc;
  332. struct hlist_head refs;
  333. int internal_strong_refs;
  334. int local_weak_refs;
  335. int local_strong_refs;
  336. int tmp_refs;
  337. binder_uintptr_t ptr;
  338. binder_uintptr_t cookie;
  339. struct {
  340. /*
  341. * bitfield elements protected by
  342. * proc inner_lock
  343. */
  344. u8 has_strong_ref:1;
  345. u8 pending_strong_ref:1;
  346. u8 has_weak_ref:1;
  347. u8 pending_weak_ref:1;
  348. };
  349. struct {
  350. /*
  351. * invariant after initialization
  352. */
  353. u8 accept_fds:1;
  354. u8 min_priority;
  355. };
  356. bool has_async_transaction;
  357. struct list_head async_todo;
  358. };
  359. struct binder_ref_death {
  360. /**
  361. * @work: worklist element for death notifications
  362. * (protected by inner_lock of the proc that
  363. * this ref belongs to)
  364. */
  365. struct binder_work work;
  366. binder_uintptr_t cookie;
  367. };
  368. /**
  369. * struct binder_ref_data - binder_ref counts and id
  370. * @debug_id: unique ID for the ref
  371. * @desc: unique userspace handle for ref
  372. * @strong: strong ref count (debugging only if not locked)
  373. * @weak: weak ref count (debugging only if not locked)
  374. *
  375. * Structure to hold ref count and ref id information. Since
  376. * the actual ref can only be accessed with a lock, this structure
  377. * is used to return information about the ref to callers of
  378. * ref inc/dec functions.
  379. */
  380. struct binder_ref_data {
  381. int debug_id;
  382. uint32_t desc;
  383. int strong;
  384. int weak;
  385. };
  386. /**
  387. * struct binder_ref - struct to track references on nodes
  388. * @data: binder_ref_data containing id, handle, and current refcounts
  389. * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
  390. * @rb_node_node: node for lookup by @node in proc's rb_tree
  391. * @node_entry: list entry for node->refs list in target node
  392. * (protected by @node->lock)
  393. * @proc: binder_proc containing ref
  394. * @node: binder_node of target node. When cleaning up a
  395. * ref for deletion in binder_cleanup_ref, a non-NULL
  396. * @node indicates the node must be freed
  397. * @death: pointer to death notification (ref_death) if requested
  398. * (protected by @node->lock)
  399. *
  400. * Structure to track references from procA to target node (on procB). This
  401. * structure is unsafe to access without holding @proc->outer_lock.
  402. */
  403. struct binder_ref {
  404. /* Lookups needed: */
  405. /* node + proc => ref (transaction) */
  406. /* desc + proc => ref (transaction, inc/dec ref) */
  407. /* node => refs + procs (proc exit) */
  408. struct binder_ref_data data;
  409. struct rb_node rb_node_desc;
  410. struct rb_node rb_node_node;
  411. struct hlist_node node_entry;
  412. struct binder_proc *proc;
  413. struct binder_node *node;
  414. struct binder_ref_death *death;
  415. };
  416. enum binder_deferred_state {
  417. BINDER_DEFERRED_PUT_FILES = 0x01,
  418. BINDER_DEFERRED_FLUSH = 0x02,
  419. BINDER_DEFERRED_RELEASE = 0x04,
  420. };
  421. /**
  422. * struct binder_proc - binder process bookkeeping
  423. * @proc_node: element for binder_procs list
  424. * @threads: rbtree of binder_threads in this proc
  425. * (protected by @inner_lock)
  426. * @nodes: rbtree of binder nodes associated with
  427. * this proc ordered by node->ptr
  428. * (protected by @inner_lock)
  429. * @refs_by_desc: rbtree of refs ordered by ref->desc
  430. * (protected by @outer_lock)
  431. * @refs_by_node: rbtree of refs ordered by ref->node
  432. * (protected by @outer_lock)
  433. * @waiting_threads: threads currently waiting for proc work
  434. * (protected by @inner_lock)
  435. * @pid PID of group_leader of process
  436. * (invariant after initialized)
  437. * @tsk task_struct for group_leader of process
  438. * (invariant after initialized)
  439. * @files files_struct for process
  440. * (invariant after initialized)
  441. * @deferred_work_node: element for binder_deferred_list
  442. * (protected by binder_deferred_lock)
  443. * @deferred_work: bitmap of deferred work to perform
  444. * (protected by binder_deferred_lock)
  445. * @is_dead: process is dead and awaiting free
  446. * when outstanding transactions are cleaned up
  447. * (protected by @inner_lock)
  448. * @todo: list of work for this process
  449. * (protected by @inner_lock)
  450. * @wait: wait queue head to wait for proc work
  451. * (invariant after initialized)
  452. * @stats: per-process binder statistics
  453. * (atomics, no lock needed)
  454. * @delivered_death: list of delivered death notification
  455. * (protected by @inner_lock)
  456. * @max_threads: cap on number of binder threads
  457. * (protected by @inner_lock)
  458. * @requested_threads: number of binder threads requested but not
  459. * yet started. In current implementation, can
  460. * only be 0 or 1.
  461. * (protected by @inner_lock)
  462. * @requested_threads_started: number binder threads started
  463. * (protected by @inner_lock)
  464. * @tmp_ref: temporary reference to indicate proc is in use
  465. * (protected by @inner_lock)
  466. * @default_priority: default scheduler priority
  467. * (invariant after initialized)
  468. * @debugfs_entry: debugfs node
  469. * @alloc: binder allocator bookkeeping
  470. * @context: binder_context for this proc
  471. * (invariant after initialized)
  472. * @inner_lock: can nest under outer_lock and/or node lock
  473. * @outer_lock: no nesting under innor or node lock
  474. * Lock order: 1) outer, 2) node, 3) inner
  475. *
  476. * Bookkeeping structure for binder processes
  477. */
  478. struct binder_proc {
  479. struct hlist_node proc_node;
  480. struct rb_root threads;
  481. struct rb_root nodes;
  482. struct rb_root refs_by_desc;
  483. struct rb_root refs_by_node;
  484. struct list_head waiting_threads;
  485. int pid;
  486. struct task_struct *tsk;
  487. struct files_struct *files;
  488. struct hlist_node deferred_work_node;
  489. int deferred_work;
  490. bool is_dead;
  491. struct list_head todo;
  492. wait_queue_head_t wait;
  493. struct binder_stats stats;
  494. struct list_head delivered_death;
  495. int max_threads;
  496. int requested_threads;
  497. int requested_threads_started;
  498. int tmp_ref;
  499. long default_priority;
  500. struct dentry *debugfs_entry;
  501. struct binder_alloc alloc;
  502. struct binder_context *context;
  503. spinlock_t inner_lock;
  504. spinlock_t outer_lock;
  505. };
  506. enum {
  507. BINDER_LOOPER_STATE_REGISTERED = 0x01,
  508. BINDER_LOOPER_STATE_ENTERED = 0x02,
  509. BINDER_LOOPER_STATE_EXITED = 0x04,
  510. BINDER_LOOPER_STATE_INVALID = 0x08,
  511. BINDER_LOOPER_STATE_WAITING = 0x10,
  512. BINDER_LOOPER_STATE_POLL = 0x20,
  513. };
  514. /**
  515. * struct binder_thread - binder thread bookkeeping
  516. * @proc: binder process for this thread
  517. * (invariant after initialization)
  518. * @rb_node: element for proc->threads rbtree
  519. * (protected by @proc->inner_lock)
  520. * @waiting_thread_node: element for @proc->waiting_threads list
  521. * (protected by @proc->inner_lock)
  522. * @pid: PID for this thread
  523. * (invariant after initialization)
  524. * @looper: bitmap of looping state
  525. * (only accessed by this thread)
  526. * @looper_needs_return: looping thread needs to exit driver
  527. * (no lock needed)
  528. * @transaction_stack: stack of in-progress transactions for this thread
  529. * (protected by @proc->inner_lock)
  530. * @todo: list of work to do for this thread
  531. * (protected by @proc->inner_lock)
  532. * @return_error: transaction errors reported by this thread
  533. * (only accessed by this thread)
  534. * @reply_error: transaction errors reported by target thread
  535. * (protected by @proc->inner_lock)
  536. * @wait: wait queue for thread work
  537. * @stats: per-thread statistics
  538. * (atomics, no lock needed)
  539. * @tmp_ref: temporary reference to indicate thread is in use
  540. * (atomic since @proc->inner_lock cannot
  541. * always be acquired)
  542. * @is_dead: thread is dead and awaiting free
  543. * when outstanding transactions are cleaned up
  544. * (protected by @proc->inner_lock)
  545. *
  546. * Bookkeeping structure for binder threads.
  547. */
  548. struct binder_thread {
  549. struct binder_proc *proc;
  550. struct rb_node rb_node;
  551. struct list_head waiting_thread_node;
  552. int pid;
  553. int looper; /* only modified by this thread */
  554. bool looper_need_return; /* can be written by other thread */
  555. struct binder_transaction *transaction_stack;
  556. struct list_head todo;
  557. struct binder_error return_error;
  558. struct binder_error reply_error;
  559. wait_queue_head_t wait;
  560. struct binder_stats stats;
  561. atomic_t tmp_ref;
  562. bool is_dead;
  563. };
  564. struct binder_transaction {
  565. int debug_id;
  566. struct binder_work work;
  567. struct binder_thread *from;
  568. struct binder_transaction *from_parent;
  569. struct binder_proc *to_proc;
  570. struct binder_thread *to_thread;
  571. struct binder_transaction *to_parent;
  572. unsigned need_reply:1;
  573. /* unsigned is_dead:1; */ /* not used at the moment */
  574. struct binder_buffer *buffer;
  575. unsigned int code;
  576. unsigned int flags;
  577. long priority;
  578. long saved_priority;
  579. kuid_t sender_euid;
  580. /**
  581. * @lock: protects @from, @to_proc, and @to_thread
  582. *
  583. * @from, @to_proc, and @to_thread can be set to NULL
  584. * during thread teardown
  585. */
  586. spinlock_t lock;
  587. };
  588. /**
  589. * binder_proc_lock() - Acquire outer lock for given binder_proc
  590. * @proc: struct binder_proc to acquire
  591. *
  592. * Acquires proc->outer_lock. Used to protect binder_ref
  593. * structures associated with the given proc.
  594. */
  595. #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
  596. static void
  597. _binder_proc_lock(struct binder_proc *proc, int line)
  598. {
  599. binder_debug(BINDER_DEBUG_SPINLOCKS,
  600. "%s: line=%d\n", __func__, line);
  601. spin_lock(&proc->outer_lock);
  602. }
  603. /**
  604. * binder_proc_unlock() - Release spinlock for given binder_proc
  605. * @proc: struct binder_proc to acquire
  606. *
  607. * Release lock acquired via binder_proc_lock()
  608. */
  609. #define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
  610. static void
  611. _binder_proc_unlock(struct binder_proc *proc, int line)
  612. {
  613. binder_debug(BINDER_DEBUG_SPINLOCKS,
  614. "%s: line=%d\n", __func__, line);
  615. spin_unlock(&proc->outer_lock);
  616. }
  617. /**
  618. * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
  619. * @proc: struct binder_proc to acquire
  620. *
  621. * Acquires proc->inner_lock. Used to protect todo lists
  622. */
  623. #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
  624. static void
  625. _binder_inner_proc_lock(struct binder_proc *proc, int line)
  626. {
  627. binder_debug(BINDER_DEBUG_SPINLOCKS,
  628. "%s: line=%d\n", __func__, line);
  629. spin_lock(&proc->inner_lock);
  630. }
  631. /**
  632. * binder_inner_proc_unlock() - Release inner lock for given binder_proc
  633. * @proc: struct binder_proc to acquire
  634. *
  635. * Release lock acquired via binder_inner_proc_lock()
  636. */
  637. #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
  638. static void
  639. _binder_inner_proc_unlock(struct binder_proc *proc, int line)
  640. {
  641. binder_debug(BINDER_DEBUG_SPINLOCKS,
  642. "%s: line=%d\n", __func__, line);
  643. spin_unlock(&proc->inner_lock);
  644. }
  645. /**
  646. * binder_node_lock() - Acquire spinlock for given binder_node
  647. * @node: struct binder_node to acquire
  648. *
  649. * Acquires node->lock. Used to protect binder_node fields
  650. */
  651. #define binder_node_lock(node) _binder_node_lock(node, __LINE__)
  652. static void
  653. _binder_node_lock(struct binder_node *node, int line)
  654. {
  655. binder_debug(BINDER_DEBUG_SPINLOCKS,
  656. "%s: line=%d\n", __func__, line);
  657. spin_lock(&node->lock);
  658. }
  659. /**
  660. * binder_node_unlock() - Release spinlock for given binder_proc
  661. * @node: struct binder_node to acquire
  662. *
  663. * Release lock acquired via binder_node_lock()
  664. */
  665. #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
  666. static void
  667. _binder_node_unlock(struct binder_node *node, int line)
  668. {
  669. binder_debug(BINDER_DEBUG_SPINLOCKS,
  670. "%s: line=%d\n", __func__, line);
  671. spin_unlock(&node->lock);
  672. }
  673. /**
  674. * binder_node_inner_lock() - Acquire node and inner locks
  675. * @node: struct binder_node to acquire
  676. *
  677. * Acquires node->lock. If node->proc also acquires
  678. * proc->inner_lock. Used to protect binder_node fields
  679. */
  680. #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
  681. static void
  682. _binder_node_inner_lock(struct binder_node *node, int line)
  683. {
  684. binder_debug(BINDER_DEBUG_SPINLOCKS,
  685. "%s: line=%d\n", __func__, line);
  686. spin_lock(&node->lock);
  687. if (node->proc)
  688. binder_inner_proc_lock(node->proc);
  689. }
  690. /**
  691. * binder_node_unlock() - Release node and inner locks
  692. * @node: struct binder_node to acquire
  693. *
  694. * Release lock acquired via binder_node_lock()
  695. */
  696. #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
  697. static void
  698. _binder_node_inner_unlock(struct binder_node *node, int line)
  699. {
  700. struct binder_proc *proc = node->proc;
  701. binder_debug(BINDER_DEBUG_SPINLOCKS,
  702. "%s: line=%d\n", __func__, line);
  703. if (proc)
  704. binder_inner_proc_unlock(proc);
  705. spin_unlock(&node->lock);
  706. }
  707. static bool binder_worklist_empty_ilocked(struct list_head *list)
  708. {
  709. return list_empty(list);
  710. }
  711. /**
  712. * binder_worklist_empty() - Check if no items on the work list
  713. * @proc: binder_proc associated with list
  714. * @list: list to check
  715. *
  716. * Return: true if there are no items on list, else false
  717. */
  718. static bool binder_worklist_empty(struct binder_proc *proc,
  719. struct list_head *list)
  720. {
  721. bool ret;
  722. binder_inner_proc_lock(proc);
  723. ret = binder_worklist_empty_ilocked(list);
  724. binder_inner_proc_unlock(proc);
  725. return ret;
  726. }
  727. static void
  728. binder_enqueue_work_ilocked(struct binder_work *work,
  729. struct list_head *target_list)
  730. {
  731. BUG_ON(target_list == NULL);
  732. BUG_ON(work->entry.next && !list_empty(&work->entry));
  733. list_add_tail(&work->entry, target_list);
  734. }
  735. /**
  736. * binder_enqueue_work() - Add an item to the work list
  737. * @proc: binder_proc associated with list
  738. * @work: struct binder_work to add to list
  739. * @target_list: list to add work to
  740. *
  741. * Adds the work to the specified list. Asserts that work
  742. * is not already on a list.
  743. */
  744. static void
  745. binder_enqueue_work(struct binder_proc *proc,
  746. struct binder_work *work,
  747. struct list_head *target_list)
  748. {
  749. binder_inner_proc_lock(proc);
  750. binder_enqueue_work_ilocked(work, target_list);
  751. binder_inner_proc_unlock(proc);
  752. }
  753. static void
  754. binder_dequeue_work_ilocked(struct binder_work *work)
  755. {
  756. list_del_init(&work->entry);
  757. }
  758. /**
  759. * binder_dequeue_work() - Removes an item from the work list
  760. * @proc: binder_proc associated with list
  761. * @work: struct binder_work to remove from list
  762. *
  763. * Removes the specified work item from whatever list it is on.
  764. * Can safely be called if work is not on any list.
  765. */
  766. static void
  767. binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
  768. {
  769. binder_inner_proc_lock(proc);
  770. binder_dequeue_work_ilocked(work);
  771. binder_inner_proc_unlock(proc);
  772. }
  773. static struct binder_work *binder_dequeue_work_head_ilocked(
  774. struct list_head *list)
  775. {
  776. struct binder_work *w;
  777. w = list_first_entry_or_null(list, struct binder_work, entry);
  778. if (w)
  779. list_del_init(&w->entry);
  780. return w;
  781. }
  782. /**
  783. * binder_dequeue_work_head() - Dequeues the item at head of list
  784. * @proc: binder_proc associated with list
  785. * @list: list to dequeue head
  786. *
  787. * Removes the head of the list if there are items on the list
  788. *
  789. * Return: pointer dequeued binder_work, NULL if list was empty
  790. */
  791. static struct binder_work *binder_dequeue_work_head(
  792. struct binder_proc *proc,
  793. struct list_head *list)
  794. {
  795. struct binder_work *w;
  796. binder_inner_proc_lock(proc);
  797. w = binder_dequeue_work_head_ilocked(list);
  798. binder_inner_proc_unlock(proc);
  799. return w;
  800. }
  801. static void
  802. binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
  803. static void binder_free_thread(struct binder_thread *thread);
  804. static void binder_free_proc(struct binder_proc *proc);
  805. static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
  806. static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
  807. {
  808. struct files_struct *files = proc->files;
  809. unsigned long rlim_cur;
  810. unsigned long irqs;
  811. if (files == NULL)
  812. return -ESRCH;
  813. if (!lock_task_sighand(proc->tsk, &irqs))
  814. return -EMFILE;
  815. rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
  816. unlock_task_sighand(proc->tsk, &irqs);
  817. return __alloc_fd(files, 0, rlim_cur, flags);
  818. }
  819. /*
  820. * copied from fd_install
  821. */
  822. static void task_fd_install(
  823. struct binder_proc *proc, unsigned int fd, struct file *file)
  824. {
  825. if (proc->files)
  826. __fd_install(proc->files, fd, file);
  827. }
  828. /*
  829. * copied from sys_close
  830. */
  831. static long task_close_fd(struct binder_proc *proc, unsigned int fd)
  832. {
  833. int retval;
  834. if (proc->files == NULL)
  835. return -ESRCH;
  836. retval = __close_fd(proc->files, fd);
  837. /* can't restart close syscall because file table entry was cleared */
  838. if (unlikely(retval == -ERESTARTSYS ||
  839. retval == -ERESTARTNOINTR ||
  840. retval == -ERESTARTNOHAND ||
  841. retval == -ERESTART_RESTARTBLOCK))
  842. retval = -EINTR;
  843. return retval;
  844. }
  845. static bool binder_has_work_ilocked(struct binder_thread *thread,
  846. bool do_proc_work)
  847. {
  848. return !binder_worklist_empty_ilocked(&thread->todo) ||
  849. thread->looper_need_return ||
  850. (do_proc_work &&
  851. !binder_worklist_empty_ilocked(&thread->proc->todo));
  852. }
  853. static bool binder_has_work(struct binder_thread *thread, bool do_proc_work)
  854. {
  855. bool has_work;
  856. binder_inner_proc_lock(thread->proc);
  857. has_work = binder_has_work_ilocked(thread, do_proc_work);
  858. binder_inner_proc_unlock(thread->proc);
  859. return has_work;
  860. }
  861. static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread)
  862. {
  863. return !thread->transaction_stack &&
  864. binder_worklist_empty_ilocked(&thread->todo) &&
  865. (thread->looper & (BINDER_LOOPER_STATE_ENTERED |
  866. BINDER_LOOPER_STATE_REGISTERED));
  867. }
  868. static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc,
  869. bool sync)
  870. {
  871. struct rb_node *n;
  872. struct binder_thread *thread;
  873. for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
  874. thread = rb_entry(n, struct binder_thread, rb_node);
  875. if (thread->looper & BINDER_LOOPER_STATE_POLL &&
  876. binder_available_for_proc_work_ilocked(thread)) {
  877. if (sync)
  878. wake_up_interruptible_sync(&thread->wait);
  879. else
  880. wake_up_interruptible(&thread->wait);
  881. }
  882. }
  883. }
  884. /**
  885. * binder_select_thread_ilocked() - selects a thread for doing proc work.
  886. * @proc: process to select a thread from
  887. *
  888. * Note that calling this function moves the thread off the waiting_threads
  889. * list, so it can only be woken up by the caller of this function, or a
  890. * signal. Therefore, callers *should* always wake up the thread this function
  891. * returns.
  892. *
  893. * Return: If there's a thread currently waiting for process work,
  894. * returns that thread. Otherwise returns NULL.
  895. */
  896. static struct binder_thread *
  897. binder_select_thread_ilocked(struct binder_proc *proc)
  898. {
  899. struct binder_thread *thread;
  900. assert_spin_locked(&proc->inner_lock);
  901. thread = list_first_entry_or_null(&proc->waiting_threads,
  902. struct binder_thread,
  903. waiting_thread_node);
  904. if (thread)
  905. list_del_init(&thread->waiting_thread_node);
  906. return thread;
  907. }
  908. /**
  909. * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
  910. * @proc: process to wake up a thread in
  911. * @thread: specific thread to wake-up (may be NULL)
  912. * @sync: whether to do a synchronous wake-up
  913. *
  914. * This function wakes up a thread in the @proc process.
  915. * The caller may provide a specific thread to wake-up in
  916. * the @thread parameter. If @thread is NULL, this function
  917. * will wake up threads that have called poll().
  918. *
  919. * Note that for this function to work as expected, callers
  920. * should first call binder_select_thread() to find a thread
  921. * to handle the work (if they don't have a thread already),
  922. * and pass the result into the @thread parameter.
  923. */
  924. static void binder_wakeup_thread_ilocked(struct binder_proc *proc,
  925. struct binder_thread *thread,
  926. bool sync)
  927. {
  928. assert_spin_locked(&proc->inner_lock);
  929. if (thread) {
  930. if (sync)
  931. wake_up_interruptible_sync(&thread->wait);
  932. else
  933. wake_up_interruptible(&thread->wait);
  934. return;
  935. }
  936. /* Didn't find a thread waiting for proc work; this can happen
  937. * in two scenarios:
  938. * 1. All threads are busy handling transactions
  939. * In that case, one of those threads should call back into
  940. * the kernel driver soon and pick up this work.
  941. * 2. Threads are using the (e)poll interface, in which case
  942. * they may be blocked on the waitqueue without having been
  943. * added to waiting_threads. For this case, we just iterate
  944. * over all threads not handling transaction work, and
  945. * wake them all up. We wake all because we don't know whether
  946. * a thread that called into (e)poll is handling non-binder
  947. * work currently.
  948. */
  949. binder_wakeup_poll_threads_ilocked(proc, sync);
  950. }
  951. static void binder_wakeup_proc_ilocked(struct binder_proc *proc)
  952. {
  953. struct binder_thread *thread = binder_select_thread_ilocked(proc);
  954. binder_wakeup_thread_ilocked(proc, thread, /* sync = */false);
  955. }
  956. static void binder_set_nice(long nice)
  957. {
  958. long min_nice;
  959. if (can_nice(current, nice)) {
  960. set_user_nice(current, nice);
  961. return;
  962. }
  963. min_nice = rlimit_to_nice(rlimit(RLIMIT_NICE));
  964. binder_debug(BINDER_DEBUG_PRIORITY_CAP,
  965. "%d: nice value %ld not allowed use %ld instead\n",
  966. current->pid, nice, min_nice);
  967. set_user_nice(current, min_nice);
  968. if (min_nice <= MAX_NICE)
  969. return;
  970. binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
  971. }
  972. static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
  973. binder_uintptr_t ptr)
  974. {
  975. struct rb_node *n = proc->nodes.rb_node;
  976. struct binder_node *node;
  977. assert_spin_locked(&proc->inner_lock);
  978. while (n) {
  979. node = rb_entry(n, struct binder_node, rb_node);
  980. if (ptr < node->ptr)
  981. n = n->rb_left;
  982. else if (ptr > node->ptr)
  983. n = n->rb_right;
  984. else {
  985. /*
  986. * take an implicit weak reference
  987. * to ensure node stays alive until
  988. * call to binder_put_node()
  989. */
  990. binder_inc_node_tmpref_ilocked(node);
  991. return node;
  992. }
  993. }
  994. return NULL;
  995. }
  996. static struct binder_node *binder_get_node(struct binder_proc *proc,
  997. binder_uintptr_t ptr)
  998. {
  999. struct binder_node *node;
  1000. binder_inner_proc_lock(proc);
  1001. node = binder_get_node_ilocked(proc, ptr);
  1002. binder_inner_proc_unlock(proc);
  1003. return node;
  1004. }
  1005. static struct binder_node *binder_init_node_ilocked(
  1006. struct binder_proc *proc,
  1007. struct binder_node *new_node,
  1008. struct flat_binder_object *fp)
  1009. {
  1010. struct rb_node **p = &proc->nodes.rb_node;
  1011. struct rb_node *parent = NULL;
  1012. struct binder_node *node;
  1013. binder_uintptr_t ptr = fp ? fp->binder : 0;
  1014. binder_uintptr_t cookie = fp ? fp->cookie : 0;
  1015. __u32 flags = fp ? fp->flags : 0;
  1016. assert_spin_locked(&proc->inner_lock);
  1017. while (*p) {
  1018. parent = *p;
  1019. node = rb_entry(parent, struct binder_node, rb_node);
  1020. if (ptr < node->ptr)
  1021. p = &(*p)->rb_left;
  1022. else if (ptr > node->ptr)
  1023. p = &(*p)->rb_right;
  1024. else {
  1025. /*
  1026. * A matching node is already in
  1027. * the rb tree. Abandon the init
  1028. * and return it.
  1029. */
  1030. binder_inc_node_tmpref_ilocked(node);
  1031. return node;
  1032. }
  1033. }
  1034. node = new_node;
  1035. binder_stats_created(BINDER_STAT_NODE);
  1036. node->tmp_refs++;
  1037. rb_link_node(&node->rb_node, parent, p);
  1038. rb_insert_color(&node->rb_node, &proc->nodes);
  1039. node->debug_id = atomic_inc_return(&binder_last_id);
  1040. node->proc = proc;
  1041. node->ptr = ptr;
  1042. node->cookie = cookie;
  1043. node->work.type = BINDER_WORK_NODE;
  1044. node->min_priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
  1045. node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
  1046. spin_lock_init(&node->lock);
  1047. INIT_LIST_HEAD(&node->work.entry);
  1048. INIT_LIST_HEAD(&node->async_todo);
  1049. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  1050. "%d:%d node %d u%016llx c%016llx created\n",
  1051. proc->pid, current->pid, node->debug_id,
  1052. (u64)node->ptr, (u64)node->cookie);
  1053. return node;
  1054. }
  1055. static struct binder_node *binder_new_node(struct binder_proc *proc,
  1056. struct flat_binder_object *fp)
  1057. {
  1058. struct binder_node *node;
  1059. struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
  1060. if (!new_node)
  1061. return NULL;
  1062. binder_inner_proc_lock(proc);
  1063. node = binder_init_node_ilocked(proc, new_node, fp);
  1064. binder_inner_proc_unlock(proc);
  1065. if (node != new_node)
  1066. /*
  1067. * The node was already added by another thread
  1068. */
  1069. kfree(new_node);
  1070. return node;
  1071. }
  1072. static void binder_free_node(struct binder_node *node)
  1073. {
  1074. kfree(node);
  1075. binder_stats_deleted(BINDER_STAT_NODE);
  1076. }
  1077. static int binder_inc_node_nilocked(struct binder_node *node, int strong,
  1078. int internal,
  1079. struct list_head *target_list)
  1080. {
  1081. struct binder_proc *proc = node->proc;
  1082. assert_spin_locked(&node->lock);
  1083. if (proc)
  1084. assert_spin_locked(&proc->inner_lock);
  1085. if (strong) {
  1086. if (internal) {
  1087. if (target_list == NULL &&
  1088. node->internal_strong_refs == 0 &&
  1089. !(node->proc &&
  1090. node == node->proc->context->binder_context_mgr_node &&
  1091. node->has_strong_ref)) {
  1092. pr_err("invalid inc strong node for %d\n",
  1093. node->debug_id);
  1094. return -EINVAL;
  1095. }
  1096. node->internal_strong_refs++;
  1097. } else
  1098. node->local_strong_refs++;
  1099. if (!node->has_strong_ref && target_list) {
  1100. binder_dequeue_work_ilocked(&node->work);
  1101. binder_enqueue_work_ilocked(&node->work, target_list);
  1102. }
  1103. } else {
  1104. if (!internal)
  1105. node->local_weak_refs++;
  1106. if (!node->has_weak_ref && list_empty(&node->work.entry)) {
  1107. if (target_list == NULL) {
  1108. pr_err("invalid inc weak node for %d\n",
  1109. node->debug_id);
  1110. return -EINVAL;
  1111. }
  1112. binder_enqueue_work_ilocked(&node->work, target_list);
  1113. }
  1114. }
  1115. return 0;
  1116. }
  1117. static int binder_inc_node(struct binder_node *node, int strong, int internal,
  1118. struct list_head *target_list)
  1119. {
  1120. int ret;
  1121. binder_node_inner_lock(node);
  1122. ret = binder_inc_node_nilocked(node, strong, internal, target_list);
  1123. binder_node_inner_unlock(node);
  1124. return ret;
  1125. }
  1126. static bool binder_dec_node_nilocked(struct binder_node *node,
  1127. int strong, int internal)
  1128. {
  1129. struct binder_proc *proc = node->proc;
  1130. assert_spin_locked(&node->lock);
  1131. if (proc)
  1132. assert_spin_locked(&proc->inner_lock);
  1133. if (strong) {
  1134. if (internal)
  1135. node->internal_strong_refs--;
  1136. else
  1137. node->local_strong_refs--;
  1138. if (node->local_strong_refs || node->internal_strong_refs)
  1139. return false;
  1140. } else {
  1141. if (!internal)
  1142. node->local_weak_refs--;
  1143. if (node->local_weak_refs || node->tmp_refs ||
  1144. !hlist_empty(&node->refs))
  1145. return false;
  1146. }
  1147. if (proc && (node->has_strong_ref || node->has_weak_ref)) {
  1148. if (list_empty(&node->work.entry)) {
  1149. binder_enqueue_work_ilocked(&node->work, &proc->todo);
  1150. binder_wakeup_proc_ilocked(proc);
  1151. }
  1152. } else {
  1153. if (hlist_empty(&node->refs) && !node->local_strong_refs &&
  1154. !node->local_weak_refs && !node->tmp_refs) {
  1155. if (proc) {
  1156. binder_dequeue_work_ilocked(&node->work);
  1157. rb_erase(&node->rb_node, &proc->nodes);
  1158. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  1159. "refless node %d deleted\n",
  1160. node->debug_id);
  1161. } else {
  1162. BUG_ON(!list_empty(&node->work.entry));
  1163. spin_lock(&binder_dead_nodes_lock);
  1164. /*
  1165. * tmp_refs could have changed so
  1166. * check it again
  1167. */
  1168. if (node->tmp_refs) {
  1169. spin_unlock(&binder_dead_nodes_lock);
  1170. return false;
  1171. }
  1172. hlist_del(&node->dead_node);
  1173. spin_unlock(&binder_dead_nodes_lock);
  1174. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  1175. "dead node %d deleted\n",
  1176. node->debug_id);
  1177. }
  1178. return true;
  1179. }
  1180. }
  1181. return false;
  1182. }
  1183. static void binder_dec_node(struct binder_node *node, int strong, int internal)
  1184. {
  1185. bool free_node;
  1186. binder_node_inner_lock(node);
  1187. free_node = binder_dec_node_nilocked(node, strong, internal);
  1188. binder_node_inner_unlock(node);
  1189. if (free_node)
  1190. binder_free_node(node);
  1191. }
  1192. static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
  1193. {
  1194. /*
  1195. * No call to binder_inc_node() is needed since we
  1196. * don't need to inform userspace of any changes to
  1197. * tmp_refs
  1198. */
  1199. node->tmp_refs++;
  1200. }
  1201. /**
  1202. * binder_inc_node_tmpref() - take a temporary reference on node
  1203. * @node: node to reference
  1204. *
  1205. * Take reference on node to prevent the node from being freed
  1206. * while referenced only by a local variable. The inner lock is
  1207. * needed to serialize with the node work on the queue (which
  1208. * isn't needed after the node is dead). If the node is dead
  1209. * (node->proc is NULL), use binder_dead_nodes_lock to protect
  1210. * node->tmp_refs against dead-node-only cases where the node
  1211. * lock cannot be acquired (eg traversing the dead node list to
  1212. * print nodes)
  1213. */
  1214. static void binder_inc_node_tmpref(struct binder_node *node)
  1215. {
  1216. binder_node_lock(node);
  1217. if (node->proc)
  1218. binder_inner_proc_lock(node->proc);
  1219. else
  1220. spin_lock(&binder_dead_nodes_lock);
  1221. binder_inc_node_tmpref_ilocked(node);
  1222. if (node->proc)
  1223. binder_inner_proc_unlock(node->proc);
  1224. else
  1225. spin_unlock(&binder_dead_nodes_lock);
  1226. binder_node_unlock(node);
  1227. }
  1228. /**
  1229. * binder_dec_node_tmpref() - remove a temporary reference on node
  1230. * @node: node to reference
  1231. *
  1232. * Release temporary reference on node taken via binder_inc_node_tmpref()
  1233. */
  1234. static void binder_dec_node_tmpref(struct binder_node *node)
  1235. {
  1236. bool free_node;
  1237. binder_node_inner_lock(node);
  1238. if (!node->proc)
  1239. spin_lock(&binder_dead_nodes_lock);
  1240. node->tmp_refs--;
  1241. BUG_ON(node->tmp_refs < 0);
  1242. if (!node->proc)
  1243. spin_unlock(&binder_dead_nodes_lock);
  1244. /*
  1245. * Call binder_dec_node() to check if all refcounts are 0
  1246. * and cleanup is needed. Calling with strong=0 and internal=1
  1247. * causes no actual reference to be released in binder_dec_node().
  1248. * If that changes, a change is needed here too.
  1249. */
  1250. free_node = binder_dec_node_nilocked(node, 0, 1);
  1251. binder_node_inner_unlock(node);
  1252. if (free_node)
  1253. binder_free_node(node);
  1254. }
  1255. static void binder_put_node(struct binder_node *node)
  1256. {
  1257. binder_dec_node_tmpref(node);
  1258. }
  1259. static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
  1260. u32 desc, bool need_strong_ref)
  1261. {
  1262. struct rb_node *n = proc->refs_by_desc.rb_node;
  1263. struct binder_ref *ref;
  1264. while (n) {
  1265. ref = rb_entry(n, struct binder_ref, rb_node_desc);
  1266. if (desc < ref->data.desc) {
  1267. n = n->rb_left;
  1268. } else if (desc > ref->data.desc) {
  1269. n = n->rb_right;
  1270. } else if (need_strong_ref && !ref->data.strong) {
  1271. binder_user_error("tried to use weak ref as strong ref\n");
  1272. return NULL;
  1273. } else {
  1274. return ref;
  1275. }
  1276. }
  1277. return NULL;
  1278. }
  1279. /**
  1280. * binder_get_ref_for_node_olocked() - get the ref associated with given node
  1281. * @proc: binder_proc that owns the ref
  1282. * @node: binder_node of target
  1283. * @new_ref: newly allocated binder_ref to be initialized or %NULL
  1284. *
  1285. * Look up the ref for the given node and return it if it exists
  1286. *
  1287. * If it doesn't exist and the caller provides a newly allocated
  1288. * ref, initialize the fields of the newly allocated ref and insert
  1289. * into the given proc rb_trees and node refs list.
  1290. *
  1291. * Return: the ref for node. It is possible that another thread
  1292. * allocated/initialized the ref first in which case the
  1293. * returned ref would be different than the passed-in
  1294. * new_ref. new_ref must be kfree'd by the caller in
  1295. * this case.
  1296. */
  1297. static struct binder_ref *binder_get_ref_for_node_olocked(
  1298. struct binder_proc *proc,
  1299. struct binder_node *node,
  1300. struct binder_ref *new_ref)
  1301. {
  1302. struct binder_context *context = proc->context;
  1303. struct rb_node **p = &proc->refs_by_node.rb_node;
  1304. struct rb_node *parent = NULL;
  1305. struct binder_ref *ref;
  1306. struct rb_node *n;
  1307. while (*p) {
  1308. parent = *p;
  1309. ref = rb_entry(parent, struct binder_ref, rb_node_node);
  1310. if (node < ref->node)
  1311. p = &(*p)->rb_left;
  1312. else if (node > ref->node)
  1313. p = &(*p)->rb_right;
  1314. else
  1315. return ref;
  1316. }
  1317. if (!new_ref)
  1318. return NULL;
  1319. binder_stats_created(BINDER_STAT_REF);
  1320. new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
  1321. new_ref->proc = proc;
  1322. new_ref->node = node;
  1323. rb_link_node(&new_ref->rb_node_node, parent, p);
  1324. rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
  1325. new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
  1326. for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
  1327. ref = rb_entry(n, struct binder_ref, rb_node_desc);
  1328. if (ref->data.desc > new_ref->data.desc)
  1329. break;
  1330. new_ref->data.desc = ref->data.desc + 1;
  1331. }
  1332. p = &proc->refs_by_desc.rb_node;
  1333. while (*p) {
  1334. parent = *p;
  1335. ref = rb_entry(parent, struct binder_ref, rb_node_desc);
  1336. if (new_ref->data.desc < ref->data.desc)
  1337. p = &(*p)->rb_left;
  1338. else if (new_ref->data.desc > ref->data.desc)
  1339. p = &(*p)->rb_right;
  1340. else
  1341. BUG();
  1342. }
  1343. rb_link_node(&new_ref->rb_node_desc, parent, p);
  1344. rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
  1345. binder_node_lock(node);
  1346. hlist_add_head(&new_ref->node_entry, &node->refs);
  1347. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  1348. "%d new ref %d desc %d for node %d\n",
  1349. proc->pid, new_ref->data.debug_id, new_ref->data.desc,
  1350. node->debug_id);
  1351. binder_node_unlock(node);
  1352. return new_ref;
  1353. }
  1354. static void binder_cleanup_ref_olocked(struct binder_ref *ref)
  1355. {
  1356. bool delete_node = false;
  1357. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  1358. "%d delete ref %d desc %d for node %d\n",
  1359. ref->proc->pid, ref->data.debug_id, ref->data.desc,
  1360. ref->node->debug_id);
  1361. rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
  1362. rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
  1363. binder_node_inner_lock(ref->node);
  1364. if (ref->data.strong)
  1365. binder_dec_node_nilocked(ref->node, 1, 1);
  1366. hlist_del(&ref->node_entry);
  1367. delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
  1368. binder_node_inner_unlock(ref->node);
  1369. /*
  1370. * Clear ref->node unless we want the caller to free the node
  1371. */
  1372. if (!delete_node) {
  1373. /*
  1374. * The caller uses ref->node to determine
  1375. * whether the node needs to be freed. Clear
  1376. * it since the node is still alive.
  1377. */
  1378. ref->node = NULL;
  1379. }
  1380. if (ref->death) {
  1381. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  1382. "%d delete ref %d desc %d has death notification\n",
  1383. ref->proc->pid, ref->data.debug_id,
  1384. ref->data.desc);
  1385. binder_dequeue_work(ref->proc, &ref->death->work);
  1386. binder_stats_deleted(BINDER_STAT_DEATH);
  1387. }
  1388. binder_stats_deleted(BINDER_STAT_REF);
  1389. }
  1390. /**
  1391. * binder_inc_ref_olocked() - increment the ref for given handle
  1392. * @ref: ref to be incremented
  1393. * @strong: if true, strong increment, else weak
  1394. * @target_list: list to queue node work on
  1395. *
  1396. * Increment the ref. @ref->proc->outer_lock must be held on entry
  1397. *
  1398. * Return: 0, if successful, else errno
  1399. */
  1400. static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
  1401. struct list_head *target_list)
  1402. {
  1403. int ret;
  1404. if (strong) {
  1405. if (ref->data.strong == 0) {
  1406. ret = binder_inc_node(ref->node, 1, 1, target_list);
  1407. if (ret)
  1408. return ret;
  1409. }
  1410. ref->data.strong++;
  1411. } else {
  1412. if (ref->data.weak == 0) {
  1413. ret = binder_inc_node(ref->node, 0, 1, target_list);
  1414. if (ret)
  1415. return ret;
  1416. }
  1417. ref->data.weak++;
  1418. }
  1419. return 0;
  1420. }
  1421. /**
  1422. * binder_dec_ref() - dec the ref for given handle
  1423. * @ref: ref to be decremented
  1424. * @strong: if true, strong decrement, else weak
  1425. *
  1426. * Decrement the ref.
  1427. *
  1428. * Return: true if ref is cleaned up and ready to be freed
  1429. */
  1430. static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
  1431. {
  1432. if (strong) {
  1433. if (ref->data.strong == 0) {
  1434. binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
  1435. ref->proc->pid, ref->data.debug_id,
  1436. ref->data.desc, ref->data.strong,
  1437. ref->data.weak);
  1438. return false;
  1439. }
  1440. ref->data.strong--;
  1441. if (ref->data.strong == 0)
  1442. binder_dec_node(ref->node, strong, 1);
  1443. } else {
  1444. if (ref->data.weak == 0) {
  1445. binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
  1446. ref->proc->pid, ref->data.debug_id,
  1447. ref->data.desc, ref->data.strong,
  1448. ref->data.weak);
  1449. return false;
  1450. }
  1451. ref->data.weak--;
  1452. }
  1453. if (ref->data.strong == 0 && ref->data.weak == 0) {
  1454. binder_cleanup_ref_olocked(ref);
  1455. return true;
  1456. }
  1457. return false;
  1458. }
  1459. /**
  1460. * binder_get_node_from_ref() - get the node from the given proc/desc
  1461. * @proc: proc containing the ref
  1462. * @desc: the handle associated with the ref
  1463. * @need_strong_ref: if true, only return node if ref is strong
  1464. * @rdata: the id/refcount data for the ref
  1465. *
  1466. * Given a proc and ref handle, return the associated binder_node
  1467. *
  1468. * Return: a binder_node or NULL if not found or not strong when strong required
  1469. */
  1470. static struct binder_node *binder_get_node_from_ref(
  1471. struct binder_proc *proc,
  1472. u32 desc, bool need_strong_ref,
  1473. struct binder_ref_data *rdata)
  1474. {
  1475. struct binder_node *node;
  1476. struct binder_ref *ref;
  1477. binder_proc_lock(proc);
  1478. ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
  1479. if (!ref)
  1480. goto err_no_ref;
  1481. node = ref->node;
  1482. /*
  1483. * Take an implicit reference on the node to ensure
  1484. * it stays alive until the call to binder_put_node()
  1485. */
  1486. binder_inc_node_tmpref(node);
  1487. if (rdata)
  1488. *rdata = ref->data;
  1489. binder_proc_unlock(proc);
  1490. return node;
  1491. err_no_ref:
  1492. binder_proc_unlock(proc);
  1493. return NULL;
  1494. }
  1495. /**
  1496. * binder_free_ref() - free the binder_ref
  1497. * @ref: ref to free
  1498. *
  1499. * Free the binder_ref. Free the binder_node indicated by ref->node
  1500. * (if non-NULL) and the binder_ref_death indicated by ref->death.
  1501. */
  1502. static void binder_free_ref(struct binder_ref *ref)
  1503. {
  1504. if (ref->node)
  1505. binder_free_node(ref->node);
  1506. kfree(ref->death);
  1507. kfree(ref);
  1508. }
  1509. /**
  1510. * binder_update_ref_for_handle() - inc/dec the ref for given handle
  1511. * @proc: proc containing the ref
  1512. * @desc: the handle associated with the ref
  1513. * @increment: true=inc reference, false=dec reference
  1514. * @strong: true=strong reference, false=weak reference
  1515. * @rdata: the id/refcount data for the ref
  1516. *
  1517. * Given a proc and ref handle, increment or decrement the ref
  1518. * according to "increment" arg.
  1519. *
  1520. * Return: 0 if successful, else errno
  1521. */
  1522. static int binder_update_ref_for_handle(struct binder_proc *proc,
  1523. uint32_t desc, bool increment, bool strong,
  1524. struct binder_ref_data *rdata)
  1525. {
  1526. int ret = 0;
  1527. struct binder_ref *ref;
  1528. bool delete_ref = false;
  1529. binder_proc_lock(proc);
  1530. ref = binder_get_ref_olocked(proc, desc, strong);
  1531. if (!ref) {
  1532. ret = -EINVAL;
  1533. goto err_no_ref;
  1534. }
  1535. if (increment)
  1536. ret = binder_inc_ref_olocked(ref, strong, NULL);
  1537. else
  1538. delete_ref = binder_dec_ref_olocked(ref, strong);
  1539. if (rdata)
  1540. *rdata = ref->data;
  1541. binder_proc_unlock(proc);
  1542. if (delete_ref)
  1543. binder_free_ref(ref);
  1544. return ret;
  1545. err_no_ref:
  1546. binder_proc_unlock(proc);
  1547. return ret;
  1548. }
  1549. /**
  1550. * binder_dec_ref_for_handle() - dec the ref for given handle
  1551. * @proc: proc containing the ref
  1552. * @desc: the handle associated with the ref
  1553. * @strong: true=strong reference, false=weak reference
  1554. * @rdata: the id/refcount data for the ref
  1555. *
  1556. * Just calls binder_update_ref_for_handle() to decrement the ref.
  1557. *
  1558. * Return: 0 if successful, else errno
  1559. */
  1560. static int binder_dec_ref_for_handle(struct binder_proc *proc,
  1561. uint32_t desc, bool strong, struct binder_ref_data *rdata)
  1562. {
  1563. return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
  1564. }
  1565. /**
  1566. * binder_inc_ref_for_node() - increment the ref for given proc/node
  1567. * @proc: proc containing the ref
  1568. * @node: target node
  1569. * @strong: true=strong reference, false=weak reference
  1570. * @target_list: worklist to use if node is incremented
  1571. * @rdata: the id/refcount data for the ref
  1572. *
  1573. * Given a proc and node, increment the ref. Create the ref if it
  1574. * doesn't already exist
  1575. *
  1576. * Return: 0 if successful, else errno
  1577. */
  1578. static int binder_inc_ref_for_node(struct binder_proc *proc,
  1579. struct binder_node *node,
  1580. bool strong,
  1581. struct list_head *target_list,
  1582. struct binder_ref_data *rdata)
  1583. {
  1584. struct binder_ref *ref;
  1585. struct binder_ref *new_ref = NULL;
  1586. int ret = 0;
  1587. binder_proc_lock(proc);
  1588. ref = binder_get_ref_for_node_olocked(proc, node, NULL);
  1589. if (!ref) {
  1590. binder_proc_unlock(proc);
  1591. new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
  1592. if (!new_ref)
  1593. return -ENOMEM;
  1594. binder_proc_lock(proc);
  1595. ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
  1596. }
  1597. ret = binder_inc_ref_olocked(ref, strong, target_list);
  1598. *rdata = ref->data;
  1599. binder_proc_unlock(proc);
  1600. if (new_ref && ref != new_ref)
  1601. /*
  1602. * Another thread created the ref first so
  1603. * free the one we allocated
  1604. */
  1605. kfree(new_ref);
  1606. return ret;
  1607. }
  1608. static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
  1609. struct binder_transaction *t)
  1610. {
  1611. BUG_ON(!target_thread);
  1612. assert_spin_locked(&target_thread->proc->inner_lock);
  1613. BUG_ON(target_thread->transaction_stack != t);
  1614. BUG_ON(target_thread->transaction_stack->from != target_thread);
  1615. target_thread->transaction_stack =
  1616. target_thread->transaction_stack->from_parent;
  1617. t->from = NULL;
  1618. }
  1619. /**
  1620. * binder_thread_dec_tmpref() - decrement thread->tmp_ref
  1621. * @thread: thread to decrement
  1622. *
  1623. * A thread needs to be kept alive while being used to create or
  1624. * handle a transaction. binder_get_txn_from() is used to safely
  1625. * extract t->from from a binder_transaction and keep the thread
  1626. * indicated by t->from from being freed. When done with that
  1627. * binder_thread, this function is called to decrement the
  1628. * tmp_ref and free if appropriate (thread has been released
  1629. * and no transaction being processed by the driver)
  1630. */
  1631. static void binder_thread_dec_tmpref(struct binder_thread *thread)
  1632. {
  1633. /*
  1634. * atomic is used to protect the counter value while
  1635. * it cannot reach zero or thread->is_dead is false
  1636. */
  1637. binder_inner_proc_lock(thread->proc);
  1638. atomic_dec(&thread->tmp_ref);
  1639. if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
  1640. binder_inner_proc_unlock(thread->proc);
  1641. binder_free_thread(thread);
  1642. return;
  1643. }
  1644. binder_inner_proc_unlock(thread->proc);
  1645. }
  1646. /**
  1647. * binder_proc_dec_tmpref() - decrement proc->tmp_ref
  1648. * @proc: proc to decrement
  1649. *
  1650. * A binder_proc needs to be kept alive while being used to create or
  1651. * handle a transaction. proc->tmp_ref is incremented when
  1652. * creating a new transaction or the binder_proc is currently in-use
  1653. * by threads that are being released. When done with the binder_proc,
  1654. * this function is called to decrement the counter and free the
  1655. * proc if appropriate (proc has been released, all threads have
  1656. * been released and not currenly in-use to process a transaction).
  1657. */
  1658. static void binder_proc_dec_tmpref(struct binder_proc *proc)
  1659. {
  1660. binder_inner_proc_lock(proc);
  1661. proc->tmp_ref--;
  1662. if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
  1663. !proc->tmp_ref) {
  1664. binder_inner_proc_unlock(proc);
  1665. binder_free_proc(proc);
  1666. return;
  1667. }
  1668. binder_inner_proc_unlock(proc);
  1669. }
  1670. /**
  1671. * binder_get_txn_from() - safely extract the "from" thread in transaction
  1672. * @t: binder transaction for t->from
  1673. *
  1674. * Atomically return the "from" thread and increment the tmp_ref
  1675. * count for the thread to ensure it stays alive until
  1676. * binder_thread_dec_tmpref() is called.
  1677. *
  1678. * Return: the value of t->from
  1679. */
  1680. static struct binder_thread *binder_get_txn_from(
  1681. struct binder_transaction *t)
  1682. {
  1683. struct binder_thread *from;
  1684. spin_lock(&t->lock);
  1685. from = t->from;
  1686. if (from)
  1687. atomic_inc(&from->tmp_ref);
  1688. spin_unlock(&t->lock);
  1689. return from;
  1690. }
  1691. /**
  1692. * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
  1693. * @t: binder transaction for t->from
  1694. *
  1695. * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
  1696. * to guarantee that the thread cannot be released while operating on it.
  1697. * The caller must call binder_inner_proc_unlock() to release the inner lock
  1698. * as well as call binder_dec_thread_txn() to release the reference.
  1699. *
  1700. * Return: the value of t->from
  1701. */
  1702. static struct binder_thread *binder_get_txn_from_and_acq_inner(
  1703. struct binder_transaction *t)
  1704. {
  1705. struct binder_thread *from;
  1706. from = binder_get_txn_from(t);
  1707. if (!from)
  1708. return NULL;
  1709. binder_inner_proc_lock(from->proc);
  1710. if (t->from) {
  1711. BUG_ON(from != t->from);
  1712. return from;
  1713. }
  1714. binder_inner_proc_unlock(from->proc);
  1715. binder_thread_dec_tmpref(from);
  1716. return NULL;
  1717. }
  1718. static void binder_free_transaction(struct binder_transaction *t)
  1719. {
  1720. if (t->buffer)
  1721. t->buffer->transaction = NULL;
  1722. kfree(t);
  1723. binder_stats_deleted(BINDER_STAT_TRANSACTION);
  1724. }
  1725. static void binder_send_failed_reply(struct binder_transaction *t,
  1726. uint32_t error_code)
  1727. {
  1728. struct binder_thread *target_thread;
  1729. struct binder_transaction *next;
  1730. BUG_ON(t->flags & TF_ONE_WAY);
  1731. while (1) {
  1732. target_thread = binder_get_txn_from_and_acq_inner(t);
  1733. if (target_thread) {
  1734. binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
  1735. "send failed reply for transaction %d to %d:%d\n",
  1736. t->debug_id,
  1737. target_thread->proc->pid,
  1738. target_thread->pid);
  1739. binder_pop_transaction_ilocked(target_thread, t);
  1740. if (target_thread->reply_error.cmd == BR_OK) {
  1741. target_thread->reply_error.cmd = error_code;
  1742. binder_enqueue_work_ilocked(
  1743. &target_thread->reply_error.work,
  1744. &target_thread->todo);
  1745. wake_up_interruptible(&target_thread->wait);
  1746. } else {
  1747. WARN(1, "Unexpected reply error: %u\n",
  1748. target_thread->reply_error.cmd);
  1749. }
  1750. binder_inner_proc_unlock(target_thread->proc);
  1751. binder_thread_dec_tmpref(target_thread);
  1752. binder_free_transaction(t);
  1753. return;
  1754. }
  1755. next = t->from_parent;
  1756. binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
  1757. "send failed reply for transaction %d, target dead\n",
  1758. t->debug_id);
  1759. binder_free_transaction(t);
  1760. if (next == NULL) {
  1761. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  1762. "reply failed, no target thread at root\n");
  1763. return;
  1764. }
  1765. t = next;
  1766. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  1767. "reply failed, no target thread -- retry %d\n",
  1768. t->debug_id);
  1769. }
  1770. }
  1771. /**
  1772. * binder_validate_object() - checks for a valid metadata object in a buffer.
  1773. * @buffer: binder_buffer that we're parsing.
  1774. * @offset: offset in the buffer at which to validate an object.
  1775. *
  1776. * Return: If there's a valid metadata object at @offset in @buffer, the
  1777. * size of that object. Otherwise, it returns zero.
  1778. */
  1779. static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
  1780. {
  1781. /* Check if we can read a header first */
  1782. struct binder_object_header *hdr;
  1783. size_t object_size = 0;
  1784. if (offset > buffer->data_size - sizeof(*hdr) ||
  1785. buffer->data_size < sizeof(*hdr) ||
  1786. !IS_ALIGNED(offset, sizeof(u32)))
  1787. return 0;
  1788. /* Ok, now see if we can read a complete object. */
  1789. hdr = (struct binder_object_header *)(buffer->data + offset);
  1790. switch (hdr->type) {
  1791. case BINDER_TYPE_BINDER:
  1792. case BINDER_TYPE_WEAK_BINDER:
  1793. case BINDER_TYPE_HANDLE:
  1794. case BINDER_TYPE_WEAK_HANDLE:
  1795. object_size = sizeof(struct flat_binder_object);
  1796. break;
  1797. case BINDER_TYPE_FD:
  1798. object_size = sizeof(struct binder_fd_object);
  1799. break;
  1800. case BINDER_TYPE_PTR:
  1801. object_size = sizeof(struct binder_buffer_object);
  1802. break;
  1803. case BINDER_TYPE_FDA:
  1804. object_size = sizeof(struct binder_fd_array_object);
  1805. break;
  1806. default:
  1807. return 0;
  1808. }
  1809. if (offset <= buffer->data_size - object_size &&
  1810. buffer->data_size >= object_size)
  1811. return object_size;
  1812. else
  1813. return 0;
  1814. }
  1815. /**
  1816. * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
  1817. * @b: binder_buffer containing the object
  1818. * @index: index in offset array at which the binder_buffer_object is
  1819. * located
  1820. * @start: points to the start of the offset array
  1821. * @num_valid: the number of valid offsets in the offset array
  1822. *
  1823. * Return: If @index is within the valid range of the offset array
  1824. * described by @start and @num_valid, and if there's a valid
  1825. * binder_buffer_object at the offset found in index @index
  1826. * of the offset array, that object is returned. Otherwise,
  1827. * %NULL is returned.
  1828. * Note that the offset found in index @index itself is not
  1829. * verified; this function assumes that @num_valid elements
  1830. * from @start were previously verified to have valid offsets.
  1831. */
  1832. static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
  1833. binder_size_t index,
  1834. binder_size_t *start,
  1835. binder_size_t num_valid)
  1836. {
  1837. struct binder_buffer_object *buffer_obj;
  1838. binder_size_t *offp;
  1839. if (index >= num_valid)
  1840. return NULL;
  1841. offp = start + index;
  1842. buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
  1843. if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
  1844. return NULL;
  1845. return buffer_obj;
  1846. }
  1847. /**
  1848. * binder_validate_fixup() - validates pointer/fd fixups happen in order.
  1849. * @b: transaction buffer
  1850. * @objects_start start of objects buffer
  1851. * @buffer: binder_buffer_object in which to fix up
  1852. * @offset: start offset in @buffer to fix up
  1853. * @last_obj: last binder_buffer_object that we fixed up in
  1854. * @last_min_offset: minimum fixup offset in @last_obj
  1855. *
  1856. * Return: %true if a fixup in buffer @buffer at offset @offset is
  1857. * allowed.
  1858. *
  1859. * For safety reasons, we only allow fixups inside a buffer to happen
  1860. * at increasing offsets; additionally, we only allow fixup on the last
  1861. * buffer object that was verified, or one of its parents.
  1862. *
  1863. * Example of what is allowed:
  1864. *
  1865. * A
  1866. * B (parent = A, offset = 0)
  1867. * C (parent = A, offset = 16)
  1868. * D (parent = C, offset = 0)
  1869. * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
  1870. *
  1871. * Examples of what is not allowed:
  1872. *
  1873. * Decreasing offsets within the same parent:
  1874. * A
  1875. * C (parent = A, offset = 16)
  1876. * B (parent = A, offset = 0) // decreasing offset within A
  1877. *
  1878. * Referring to a parent that wasn't the last object or any of its parents:
  1879. * A
  1880. * B (parent = A, offset = 0)
  1881. * C (parent = A, offset = 0)
  1882. * C (parent = A, offset = 16)
  1883. * D (parent = B, offset = 0) // B is not A or any of A's parents
  1884. */
  1885. static bool binder_validate_fixup(struct binder_buffer *b,
  1886. binder_size_t *objects_start,
  1887. struct binder_buffer_object *buffer,
  1888. binder_size_t fixup_offset,
  1889. struct binder_buffer_object *last_obj,
  1890. binder_size_t last_min_offset)
  1891. {
  1892. if (!last_obj) {
  1893. /* Nothing to fix up in */
  1894. return false;
  1895. }
  1896. while (last_obj != buffer) {
  1897. /*
  1898. * Safe to retrieve the parent of last_obj, since it
  1899. * was already previously verified by the driver.
  1900. */
  1901. if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
  1902. return false;
  1903. last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
  1904. last_obj = (struct binder_buffer_object *)
  1905. (b->data + *(objects_start + last_obj->parent));
  1906. }
  1907. return (fixup_offset >= last_min_offset);
  1908. }
  1909. static void binder_transaction_buffer_release(struct binder_proc *proc,
  1910. struct binder_buffer *buffer,
  1911. binder_size_t *failed_at)
  1912. {
  1913. binder_size_t *offp, *off_start, *off_end;
  1914. int debug_id = buffer->debug_id;
  1915. binder_debug(BINDER_DEBUG_TRANSACTION,
  1916. "%d buffer release %d, size %zd-%zd, failed at %p\n",
  1917. proc->pid, buffer->debug_id,
  1918. buffer->data_size, buffer->offsets_size, failed_at);
  1919. if (buffer->target_node)
  1920. binder_dec_node(buffer->target_node, 1, 0);
  1921. off_start = (binder_size_t *)(buffer->data +
  1922. ALIGN(buffer->data_size, sizeof(void *)));
  1923. if (failed_at)
  1924. off_end = failed_at;
  1925. else
  1926. off_end = (void *)off_start + buffer->offsets_size;
  1927. for (offp = off_start; offp < off_end; offp++) {
  1928. struct binder_object_header *hdr;
  1929. size_t object_size = binder_validate_object(buffer, *offp);
  1930. if (object_size == 0) {
  1931. pr_err("transaction release %d bad object at offset %lld, size %zd\n",
  1932. debug_id, (u64)*offp, buffer->data_size);
  1933. continue;
  1934. }
  1935. hdr = (struct binder_object_header *)(buffer->data + *offp);
  1936. switch (hdr->type) {
  1937. case BINDER_TYPE_BINDER:
  1938. case BINDER_TYPE_WEAK_BINDER: {
  1939. struct flat_binder_object *fp;
  1940. struct binder_node *node;
  1941. fp = to_flat_binder_object(hdr);
  1942. node = binder_get_node(proc, fp->binder);
  1943. if (node == NULL) {
  1944. pr_err("transaction release %d bad node %016llx\n",
  1945. debug_id, (u64)fp->binder);
  1946. break;
  1947. }
  1948. binder_debug(BINDER_DEBUG_TRANSACTION,
  1949. " node %d u%016llx\n",
  1950. node->debug_id, (u64)node->ptr);
  1951. binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
  1952. 0);
  1953. binder_put_node(node);
  1954. } break;
  1955. case BINDER_TYPE_HANDLE:
  1956. case BINDER_TYPE_WEAK_HANDLE: {
  1957. struct flat_binder_object *fp;
  1958. struct binder_ref_data rdata;
  1959. int ret;
  1960. fp = to_flat_binder_object(hdr);
  1961. ret = binder_dec_ref_for_handle(proc, fp->handle,
  1962. hdr->type == BINDER_TYPE_HANDLE, &rdata);
  1963. if (ret) {
  1964. pr_err("transaction release %d bad handle %d, ret = %d\n",
  1965. debug_id, fp->handle, ret);
  1966. break;
  1967. }
  1968. binder_debug(BINDER_DEBUG_TRANSACTION,
  1969. " ref %d desc %d\n",
  1970. rdata.debug_id, rdata.desc);
  1971. } break;
  1972. case BINDER_TYPE_FD: {
  1973. struct binder_fd_object *fp = to_binder_fd_object(hdr);
  1974. binder_debug(BINDER_DEBUG_TRANSACTION,
  1975. " fd %d\n", fp->fd);
  1976. if (failed_at)
  1977. task_close_fd(proc, fp->fd);
  1978. } break;
  1979. case BINDER_TYPE_PTR:
  1980. /*
  1981. * Nothing to do here, this will get cleaned up when the
  1982. * transaction buffer gets freed
  1983. */
  1984. break;
  1985. case BINDER_TYPE_FDA: {
  1986. struct binder_fd_array_object *fda;
  1987. struct binder_buffer_object *parent;
  1988. uintptr_t parent_buffer;
  1989. u32 *fd_array;
  1990. size_t fd_index;
  1991. binder_size_t fd_buf_size;
  1992. fda = to_binder_fd_array_object(hdr);
  1993. parent = binder_validate_ptr(buffer, fda->parent,
  1994. off_start,
  1995. offp - off_start);
  1996. if (!parent) {
  1997. pr_err("transaction release %d bad parent offset",
  1998. debug_id);
  1999. continue;
  2000. }
  2001. /*
  2002. * Since the parent was already fixed up, convert it
  2003. * back to kernel address space to access it
  2004. */
  2005. parent_buffer = parent->buffer -
  2006. binder_alloc_get_user_buffer_offset(
  2007. &proc->alloc);
  2008. fd_buf_size = sizeof(u32) * fda->num_fds;
  2009. if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
  2010. pr_err("transaction release %d invalid number of fds (%lld)\n",
  2011. debug_id, (u64)fda->num_fds);
  2012. continue;
  2013. }
  2014. if (fd_buf_size > parent->length ||
  2015. fda->parent_offset > parent->length - fd_buf_size) {
  2016. /* No space for all file descriptors here. */
  2017. pr_err("transaction release %d not enough space for %lld fds in buffer\n",
  2018. debug_id, (u64)fda->num_fds);
  2019. continue;
  2020. }
  2021. fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
  2022. for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
  2023. task_close_fd(proc, fd_array[fd_index]);
  2024. } break;
  2025. default:
  2026. pr_err("transaction release %d bad object type %x\n",
  2027. debug_id, hdr->type);
  2028. break;
  2029. }
  2030. }
  2031. }
  2032. static int binder_translate_binder(struct flat_binder_object *fp,
  2033. struct binder_transaction *t,
  2034. struct binder_thread *thread)
  2035. {
  2036. struct binder_node *node;
  2037. struct binder_proc *proc = thread->proc;
  2038. struct binder_proc *target_proc = t->to_proc;
  2039. struct binder_ref_data rdata;
  2040. int ret = 0;
  2041. node = binder_get_node(proc, fp->binder);
  2042. if (!node) {
  2043. node = binder_new_node(proc, fp);
  2044. if (!node)
  2045. return -ENOMEM;
  2046. }
  2047. if (fp->cookie != node->cookie) {
  2048. binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
  2049. proc->pid, thread->pid, (u64)fp->binder,
  2050. node->debug_id, (u64)fp->cookie,
  2051. (u64)node->cookie);
  2052. ret = -EINVAL;
  2053. goto done;
  2054. }
  2055. if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
  2056. ret = -EPERM;
  2057. goto done;
  2058. }
  2059. ret = binder_inc_ref_for_node(target_proc, node,
  2060. fp->hdr.type == BINDER_TYPE_BINDER,
  2061. &thread->todo, &rdata);
  2062. if (ret)
  2063. goto done;
  2064. if (fp->hdr.type == BINDER_TYPE_BINDER)
  2065. fp->hdr.type = BINDER_TYPE_HANDLE;
  2066. else
  2067. fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
  2068. fp->binder = 0;
  2069. fp->handle = rdata.desc;
  2070. fp->cookie = 0;
  2071. trace_binder_transaction_node_to_ref(t, node, &rdata);
  2072. binder_debug(BINDER_DEBUG_TRANSACTION,
  2073. " node %d u%016llx -> ref %d desc %d\n",
  2074. node->debug_id, (u64)node->ptr,
  2075. rdata.debug_id, rdata.desc);
  2076. done:
  2077. binder_put_node(node);
  2078. return ret;
  2079. }
  2080. static int binder_translate_handle(struct flat_binder_object *fp,
  2081. struct binder_transaction *t,
  2082. struct binder_thread *thread)
  2083. {
  2084. struct binder_proc *proc = thread->proc;
  2085. struct binder_proc *target_proc = t->to_proc;
  2086. struct binder_node *node;
  2087. struct binder_ref_data src_rdata;
  2088. int ret = 0;
  2089. node = binder_get_node_from_ref(proc, fp->handle,
  2090. fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
  2091. if (!node) {
  2092. binder_user_error("%d:%d got transaction with invalid handle, %d\n",
  2093. proc->pid, thread->pid, fp->handle);
  2094. return -EINVAL;
  2095. }
  2096. if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
  2097. ret = -EPERM;
  2098. goto done;
  2099. }
  2100. binder_node_lock(node);
  2101. if (node->proc == target_proc) {
  2102. if (fp->hdr.type == BINDER_TYPE_HANDLE)
  2103. fp->hdr.type = BINDER_TYPE_BINDER;
  2104. else
  2105. fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
  2106. fp->binder = node->ptr;
  2107. fp->cookie = node->cookie;
  2108. if (node->proc)
  2109. binder_inner_proc_lock(node->proc);
  2110. binder_inc_node_nilocked(node,
  2111. fp->hdr.type == BINDER_TYPE_BINDER,
  2112. 0, NULL);
  2113. if (node->proc)
  2114. binder_inner_proc_unlock(node->proc);
  2115. trace_binder_transaction_ref_to_node(t, node, &src_rdata);
  2116. binder_debug(BINDER_DEBUG_TRANSACTION,
  2117. " ref %d desc %d -> node %d u%016llx\n",
  2118. src_rdata.debug_id, src_rdata.desc, node->debug_id,
  2119. (u64)node->ptr);
  2120. binder_node_unlock(node);
  2121. } else {
  2122. struct binder_ref_data dest_rdata;
  2123. binder_node_unlock(node);
  2124. ret = binder_inc_ref_for_node(target_proc, node,
  2125. fp->hdr.type == BINDER_TYPE_HANDLE,
  2126. NULL, &dest_rdata);
  2127. if (ret)
  2128. goto done;
  2129. fp->binder = 0;
  2130. fp->handle = dest_rdata.desc;
  2131. fp->cookie = 0;
  2132. trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
  2133. &dest_rdata);
  2134. binder_debug(BINDER_DEBUG_TRANSACTION,
  2135. " ref %d desc %d -> ref %d desc %d (node %d)\n",
  2136. src_rdata.debug_id, src_rdata.desc,
  2137. dest_rdata.debug_id, dest_rdata.desc,
  2138. node->debug_id);
  2139. }
  2140. done:
  2141. binder_put_node(node);
  2142. return ret;
  2143. }
  2144. static int binder_translate_fd(int fd,
  2145. struct binder_transaction *t,
  2146. struct binder_thread *thread,
  2147. struct binder_transaction *in_reply_to)
  2148. {
  2149. struct binder_proc *proc = thread->proc;
  2150. struct binder_proc *target_proc = t->to_proc;
  2151. int target_fd;
  2152. struct file *file;
  2153. int ret;
  2154. bool target_allows_fd;
  2155. if (in_reply_to)
  2156. target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
  2157. else
  2158. target_allows_fd = t->buffer->target_node->accept_fds;
  2159. if (!target_allows_fd) {
  2160. binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
  2161. proc->pid, thread->pid,
  2162. in_reply_to ? "reply" : "transaction",
  2163. fd);
  2164. ret = -EPERM;
  2165. goto err_fd_not_accepted;
  2166. }
  2167. file = fget(fd);
  2168. if (!file) {
  2169. binder_user_error("%d:%d got transaction with invalid fd, %d\n",
  2170. proc->pid, thread->pid, fd);
  2171. ret = -EBADF;
  2172. goto err_fget;
  2173. }
  2174. ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
  2175. if (ret < 0) {
  2176. ret = -EPERM;
  2177. goto err_security;
  2178. }
  2179. target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
  2180. if (target_fd < 0) {
  2181. ret = -ENOMEM;
  2182. goto err_get_unused_fd;
  2183. }
  2184. task_fd_install(target_proc, target_fd, file);
  2185. trace_binder_transaction_fd(t, fd, target_fd);
  2186. binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
  2187. fd, target_fd);
  2188. return target_fd;
  2189. err_get_unused_fd:
  2190. err_security:
  2191. fput(file);
  2192. err_fget:
  2193. err_fd_not_accepted:
  2194. return ret;
  2195. }
  2196. static int binder_translate_fd_array(struct binder_fd_array_object *fda,
  2197. struct binder_buffer_object *parent,
  2198. struct binder_transaction *t,
  2199. struct binder_thread *thread,
  2200. struct binder_transaction *in_reply_to)
  2201. {
  2202. binder_size_t fdi, fd_buf_size, num_installed_fds;
  2203. int target_fd;
  2204. uintptr_t parent_buffer;
  2205. u32 *fd_array;
  2206. struct binder_proc *proc = thread->proc;
  2207. struct binder_proc *target_proc = t->to_proc;
  2208. fd_buf_size = sizeof(u32) * fda->num_fds;
  2209. if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
  2210. binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
  2211. proc->pid, thread->pid, (u64)fda->num_fds);
  2212. return -EINVAL;
  2213. }
  2214. if (fd_buf_size > parent->length ||
  2215. fda->parent_offset > parent->length - fd_buf_size) {
  2216. /* No space for all file descriptors here. */
  2217. binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
  2218. proc->pid, thread->pid, (u64)fda->num_fds);
  2219. return -EINVAL;
  2220. }
  2221. /*
  2222. * Since the parent was already fixed up, convert it
  2223. * back to the kernel address space to access it
  2224. */
  2225. parent_buffer = parent->buffer -
  2226. binder_alloc_get_user_buffer_offset(&target_proc->alloc);
  2227. fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
  2228. if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
  2229. binder_user_error("%d:%d parent offset not aligned correctly.\n",
  2230. proc->pid, thread->pid);
  2231. return -EINVAL;
  2232. }
  2233. for (fdi = 0; fdi < fda->num_fds; fdi++) {
  2234. target_fd = binder_translate_fd(fd_array[fdi], t, thread,
  2235. in_reply_to);
  2236. if (target_fd < 0)
  2237. goto err_translate_fd_failed;
  2238. fd_array[fdi] = target_fd;
  2239. }
  2240. return 0;
  2241. err_translate_fd_failed:
  2242. /*
  2243. * Failed to allocate fd or security error, free fds
  2244. * installed so far.
  2245. */
  2246. num_installed_fds = fdi;
  2247. for (fdi = 0; fdi < num_installed_fds; fdi++)
  2248. task_close_fd(target_proc, fd_array[fdi]);
  2249. return target_fd;
  2250. }
  2251. static int binder_fixup_parent(struct binder_transaction *t,
  2252. struct binder_thread *thread,
  2253. struct binder_buffer_object *bp,
  2254. binder_size_t *off_start,
  2255. binder_size_t num_valid,
  2256. struct binder_buffer_object *last_fixup_obj,
  2257. binder_size_t last_fixup_min_off)
  2258. {
  2259. struct binder_buffer_object *parent;
  2260. u8 *parent_buffer;
  2261. struct binder_buffer *b = t->buffer;
  2262. struct binder_proc *proc = thread->proc;
  2263. struct binder_proc *target_proc = t->to_proc;
  2264. if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
  2265. return 0;
  2266. parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
  2267. if (!parent) {
  2268. binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
  2269. proc->pid, thread->pid);
  2270. return -EINVAL;
  2271. }
  2272. if (!binder_validate_fixup(b, off_start,
  2273. parent, bp->parent_offset,
  2274. last_fixup_obj,
  2275. last_fixup_min_off)) {
  2276. binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
  2277. proc->pid, thread->pid);
  2278. return -EINVAL;
  2279. }
  2280. if (parent->length < sizeof(binder_uintptr_t) ||
  2281. bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
  2282. /* No space for a pointer here! */
  2283. binder_user_error("%d:%d got transaction with invalid parent offset\n",
  2284. proc->pid, thread->pid);
  2285. return -EINVAL;
  2286. }
  2287. parent_buffer = (u8 *)((uintptr_t)parent->buffer -
  2288. binder_alloc_get_user_buffer_offset(
  2289. &target_proc->alloc));
  2290. *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
  2291. return 0;
  2292. }
  2293. /**
  2294. * binder_proc_transaction() - sends a transaction to a process and wakes it up
  2295. * @t: transaction to send
  2296. * @proc: process to send the transaction to
  2297. * @thread: thread in @proc to send the transaction to (may be NULL)
  2298. *
  2299. * This function queues a transaction to the specified process. It will try
  2300. * to find a thread in the target process to handle the transaction and
  2301. * wake it up. If no thread is found, the work is queued to the proc
  2302. * waitqueue.
  2303. *
  2304. * If the @thread parameter is not NULL, the transaction is always queued
  2305. * to the waitlist of that specific thread.
  2306. *
  2307. * Return: true if the transactions was successfully queued
  2308. * false if the target process or thread is dead
  2309. */
  2310. static bool binder_proc_transaction(struct binder_transaction *t,
  2311. struct binder_proc *proc,
  2312. struct binder_thread *thread)
  2313. {
  2314. struct list_head *target_list = NULL;
  2315. struct binder_node *node = t->buffer->target_node;
  2316. bool oneway = !!(t->flags & TF_ONE_WAY);
  2317. bool wakeup = true;
  2318. BUG_ON(!node);
  2319. binder_node_lock(node);
  2320. if (oneway) {
  2321. BUG_ON(thread);
  2322. if (node->has_async_transaction) {
  2323. target_list = &node->async_todo;
  2324. wakeup = false;
  2325. } else {
  2326. node->has_async_transaction = 1;
  2327. }
  2328. }
  2329. binder_inner_proc_lock(proc);
  2330. if (proc->is_dead || (thread && thread->is_dead)) {
  2331. binder_inner_proc_unlock(proc);
  2332. binder_node_unlock(node);
  2333. return false;
  2334. }
  2335. if (!thread && !target_list)
  2336. thread = binder_select_thread_ilocked(proc);
  2337. if (thread)
  2338. target_list = &thread->todo;
  2339. else if (!target_list)
  2340. target_list = &proc->todo;
  2341. else
  2342. BUG_ON(target_list != &node->async_todo);
  2343. binder_enqueue_work_ilocked(&t->work, target_list);
  2344. if (wakeup)
  2345. binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
  2346. binder_inner_proc_unlock(proc);
  2347. binder_node_unlock(node);
  2348. return true;
  2349. }
  2350. /**
  2351. * binder_get_node_refs_for_txn() - Get required refs on node for txn
  2352. * @node: struct binder_node for which to get refs
  2353. * @proc: returns @node->proc if valid
  2354. * @error: if no @proc then returns BR_DEAD_REPLY
  2355. *
  2356. * User-space normally keeps the node alive when creating a transaction
  2357. * since it has a reference to the target. The local strong ref keeps it
  2358. * alive if the sending process dies before the target process processes
  2359. * the transaction. If the source process is malicious or has a reference
  2360. * counting bug, relying on the local strong ref can fail.
  2361. *
  2362. * Since user-space can cause the local strong ref to go away, we also take
  2363. * a tmpref on the node to ensure it survives while we are constructing
  2364. * the transaction. We also need a tmpref on the proc while we are
  2365. * constructing the transaction, so we take that here as well.
  2366. *
  2367. * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
  2368. * Also sets @proc if valid. If the @node->proc is NULL indicating that the
  2369. * target proc has died, @error is set to BR_DEAD_REPLY
  2370. */
  2371. static struct binder_node *binder_get_node_refs_for_txn(
  2372. struct binder_node *node,
  2373. struct binder_proc **procp,
  2374. uint32_t *error)
  2375. {
  2376. struct binder_node *target_node = NULL;
  2377. binder_node_inner_lock(node);
  2378. if (node->proc) {
  2379. target_node = node;
  2380. binder_inc_node_nilocked(node, 1, 0, NULL);
  2381. binder_inc_node_tmpref_ilocked(node);
  2382. node->proc->tmp_ref++;
  2383. *procp = node->proc;
  2384. } else
  2385. *error = BR_DEAD_REPLY;
  2386. binder_node_inner_unlock(node);
  2387. return target_node;
  2388. }
  2389. static void binder_transaction(struct binder_proc *proc,
  2390. struct binder_thread *thread,
  2391. struct binder_transaction_data *tr, int reply,
  2392. binder_size_t extra_buffers_size)
  2393. {
  2394. int ret;
  2395. struct binder_transaction *t;
  2396. struct binder_work *tcomplete;
  2397. binder_size_t *offp, *off_end, *off_start;
  2398. binder_size_t off_min;
  2399. u8 *sg_bufp, *sg_buf_end;
  2400. struct binder_proc *target_proc = NULL;
  2401. struct binder_thread *target_thread = NULL;
  2402. struct binder_node *target_node = NULL;
  2403. struct binder_transaction *in_reply_to = NULL;
  2404. struct binder_transaction_log_entry *e;
  2405. uint32_t return_error = 0;
  2406. uint32_t return_error_param = 0;
  2407. uint32_t return_error_line = 0;
  2408. struct binder_buffer_object *last_fixup_obj = NULL;
  2409. binder_size_t last_fixup_min_off = 0;
  2410. struct binder_context *context = proc->context;
  2411. int t_debug_id = atomic_inc_return(&binder_last_id);
  2412. e = binder_transaction_log_add(&binder_transaction_log);
  2413. e->debug_id = t_debug_id;
  2414. e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
  2415. e->from_proc = proc->pid;
  2416. e->from_thread = thread->pid;
  2417. e->target_handle = tr->target.handle;
  2418. e->data_size = tr->data_size;
  2419. e->offsets_size = tr->offsets_size;
  2420. e->context_name = proc->context->name;
  2421. if (reply) {
  2422. binder_inner_proc_lock(proc);
  2423. in_reply_to = thread->transaction_stack;
  2424. if (in_reply_to == NULL) {
  2425. binder_inner_proc_unlock(proc);
  2426. binder_user_error("%d:%d got reply transaction with no transaction stack\n",
  2427. proc->pid, thread->pid);
  2428. return_error = BR_FAILED_REPLY;
  2429. return_error_param = -EPROTO;
  2430. return_error_line = __LINE__;
  2431. goto err_empty_call_stack;
  2432. }
  2433. if (in_reply_to->to_thread != thread) {
  2434. spin_lock(&in_reply_to->lock);
  2435. binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
  2436. proc->pid, thread->pid, in_reply_to->debug_id,
  2437. in_reply_to->to_proc ?
  2438. in_reply_to->to_proc->pid : 0,
  2439. in_reply_to->to_thread ?
  2440. in_reply_to->to_thread->pid : 0);
  2441. spin_unlock(&in_reply_to->lock);
  2442. binder_inner_proc_unlock(proc);
  2443. return_error = BR_FAILED_REPLY;
  2444. return_error_param = -EPROTO;
  2445. return_error_line = __LINE__;
  2446. in_reply_to = NULL;
  2447. goto err_bad_call_stack;
  2448. }
  2449. thread->transaction_stack = in_reply_to->to_parent;
  2450. binder_inner_proc_unlock(proc);
  2451. binder_set_nice(in_reply_to->saved_priority);
  2452. target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
  2453. if (target_thread == NULL) {
  2454. return_error = BR_DEAD_REPLY;
  2455. return_error_line = __LINE__;
  2456. goto err_dead_binder;
  2457. }
  2458. if (target_thread->transaction_stack != in_reply_to) {
  2459. binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
  2460. proc->pid, thread->pid,
  2461. target_thread->transaction_stack ?
  2462. target_thread->transaction_stack->debug_id : 0,
  2463. in_reply_to->debug_id);
  2464. binder_inner_proc_unlock(target_thread->proc);
  2465. return_error = BR_FAILED_REPLY;
  2466. return_error_param = -EPROTO;
  2467. return_error_line = __LINE__;
  2468. in_reply_to = NULL;
  2469. target_thread = NULL;
  2470. goto err_dead_binder;
  2471. }
  2472. target_proc = target_thread->proc;
  2473. target_proc->tmp_ref++;
  2474. binder_inner_proc_unlock(target_thread->proc);
  2475. } else {
  2476. if (tr->target.handle) {
  2477. struct binder_ref *ref;
  2478. /*
  2479. * There must already be a strong ref
  2480. * on this node. If so, do a strong
  2481. * increment on the node to ensure it
  2482. * stays alive until the transaction is
  2483. * done.
  2484. */
  2485. binder_proc_lock(proc);
  2486. ref = binder_get_ref_olocked(proc, tr->target.handle,
  2487. true);
  2488. if (ref) {
  2489. target_node = binder_get_node_refs_for_txn(
  2490. ref->node, &target_proc,
  2491. &return_error);
  2492. } else {
  2493. binder_user_error("%d:%d got transaction to invalid handle\n",
  2494. proc->pid, thread->pid);
  2495. return_error = BR_FAILED_REPLY;
  2496. }
  2497. binder_proc_unlock(proc);
  2498. } else {
  2499. mutex_lock(&context->context_mgr_node_lock);
  2500. target_node = context->binder_context_mgr_node;
  2501. if (target_node)
  2502. target_node = binder_get_node_refs_for_txn(
  2503. target_node, &target_proc,
  2504. &return_error);
  2505. else
  2506. return_error = BR_DEAD_REPLY;
  2507. mutex_unlock(&context->context_mgr_node_lock);
  2508. }
  2509. if (!target_node) {
  2510. /*
  2511. * return_error is set above
  2512. */
  2513. return_error_param = -EINVAL;
  2514. return_error_line = __LINE__;
  2515. goto err_dead_binder;
  2516. }
  2517. e->to_node = target_node->debug_id;
  2518. if (security_binder_transaction(proc->tsk,
  2519. target_proc->tsk) < 0) {
  2520. return_error = BR_FAILED_REPLY;
  2521. return_error_param = -EPERM;
  2522. return_error_line = __LINE__;
  2523. goto err_invalid_target_handle;
  2524. }
  2525. binder_inner_proc_lock(proc);
  2526. if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
  2527. struct binder_transaction *tmp;
  2528. tmp = thread->transaction_stack;
  2529. if (tmp->to_thread != thread) {
  2530. spin_lock(&tmp->lock);
  2531. binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
  2532. proc->pid, thread->pid, tmp->debug_id,
  2533. tmp->to_proc ? tmp->to_proc->pid : 0,
  2534. tmp->to_thread ?
  2535. tmp->to_thread->pid : 0);
  2536. spin_unlock(&tmp->lock);
  2537. binder_inner_proc_unlock(proc);
  2538. return_error = BR_FAILED_REPLY;
  2539. return_error_param = -EPROTO;
  2540. return_error_line = __LINE__;
  2541. goto err_bad_call_stack;
  2542. }
  2543. while (tmp) {
  2544. struct binder_thread *from;
  2545. spin_lock(&tmp->lock);
  2546. from = tmp->from;
  2547. if (from && from->proc == target_proc) {
  2548. atomic_inc(&from->tmp_ref);
  2549. target_thread = from;
  2550. spin_unlock(&tmp->lock);
  2551. break;
  2552. }
  2553. spin_unlock(&tmp->lock);
  2554. tmp = tmp->from_parent;
  2555. }
  2556. }
  2557. binder_inner_proc_unlock(proc);
  2558. }
  2559. if (target_thread)
  2560. e->to_thread = target_thread->pid;
  2561. e->to_proc = target_proc->pid;
  2562. /* TODO: reuse incoming transaction for reply */
  2563. t = kzalloc(sizeof(*t), GFP_KERNEL);
  2564. if (t == NULL) {
  2565. return_error = BR_FAILED_REPLY;
  2566. return_error_param = -ENOMEM;
  2567. return_error_line = __LINE__;
  2568. goto err_alloc_t_failed;
  2569. }
  2570. binder_stats_created(BINDER_STAT_TRANSACTION);
  2571. spin_lock_init(&t->lock);
  2572. tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
  2573. if (tcomplete == NULL) {
  2574. return_error = BR_FAILED_REPLY;
  2575. return_error_param = -ENOMEM;
  2576. return_error_line = __LINE__;
  2577. goto err_alloc_tcomplete_failed;
  2578. }
  2579. binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
  2580. t->debug_id = t_debug_id;
  2581. if (reply)
  2582. binder_debug(BINDER_DEBUG_TRANSACTION,
  2583. "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
  2584. proc->pid, thread->pid, t->debug_id,
  2585. target_proc->pid, target_thread->pid,
  2586. (u64)tr->data.ptr.buffer,
  2587. (u64)tr->data.ptr.offsets,
  2588. (u64)tr->data_size, (u64)tr->offsets_size,
  2589. (u64)extra_buffers_size);
  2590. else
  2591. binder_debug(BINDER_DEBUG_TRANSACTION,
  2592. "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
  2593. proc->pid, thread->pid, t->debug_id,
  2594. target_proc->pid, target_node->debug_id,
  2595. (u64)tr->data.ptr.buffer,
  2596. (u64)tr->data.ptr.offsets,
  2597. (u64)tr->data_size, (u64)tr->offsets_size,
  2598. (u64)extra_buffers_size);
  2599. if (!reply && !(tr->flags & TF_ONE_WAY))
  2600. t->from = thread;
  2601. else
  2602. t->from = NULL;
  2603. t->sender_euid = task_euid(proc->tsk);
  2604. t->to_proc = target_proc;
  2605. t->to_thread = target_thread;
  2606. t->code = tr->code;
  2607. t->flags = tr->flags;
  2608. t->priority = task_nice(current);
  2609. trace_binder_transaction(reply, t, target_node);
  2610. t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
  2611. tr->offsets_size, extra_buffers_size,
  2612. !reply && (t->flags & TF_ONE_WAY));
  2613. if (IS_ERR(t->buffer)) {
  2614. /*
  2615. * -ESRCH indicates VMA cleared. The target is dying.
  2616. */
  2617. return_error_param = PTR_ERR(t->buffer);
  2618. return_error = return_error_param == -ESRCH ?
  2619. BR_DEAD_REPLY : BR_FAILED_REPLY;
  2620. return_error_line = __LINE__;
  2621. t->buffer = NULL;
  2622. goto err_binder_alloc_buf_failed;
  2623. }
  2624. t->buffer->allow_user_free = 0;
  2625. t->buffer->debug_id = t->debug_id;
  2626. t->buffer->transaction = t;
  2627. t->buffer->target_node = target_node;
  2628. trace_binder_transaction_alloc_buf(t->buffer);
  2629. off_start = (binder_size_t *)(t->buffer->data +
  2630. ALIGN(tr->data_size, sizeof(void *)));
  2631. offp = off_start;
  2632. if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
  2633. tr->data.ptr.buffer, tr->data_size)) {
  2634. binder_user_error("%d:%d got transaction with invalid data ptr\n",
  2635. proc->pid, thread->pid);
  2636. return_error = BR_FAILED_REPLY;
  2637. return_error_param = -EFAULT;
  2638. return_error_line = __LINE__;
  2639. goto err_copy_data_failed;
  2640. }
  2641. if (copy_from_user(offp, (const void __user *)(uintptr_t)
  2642. tr->data.ptr.offsets, tr->offsets_size)) {
  2643. binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
  2644. proc->pid, thread->pid);
  2645. return_error = BR_FAILED_REPLY;
  2646. return_error_param = -EFAULT;
  2647. return_error_line = __LINE__;
  2648. goto err_copy_data_failed;
  2649. }
  2650. if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
  2651. binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
  2652. proc->pid, thread->pid, (u64)tr->offsets_size);
  2653. return_error = BR_FAILED_REPLY;
  2654. return_error_param = -EINVAL;
  2655. return_error_line = __LINE__;
  2656. goto err_bad_offset;
  2657. }
  2658. if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
  2659. binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
  2660. proc->pid, thread->pid,
  2661. (u64)extra_buffers_size);
  2662. return_error = BR_FAILED_REPLY;
  2663. return_error_param = -EINVAL;
  2664. return_error_line = __LINE__;
  2665. goto err_bad_offset;
  2666. }
  2667. off_end = (void *)off_start + tr->offsets_size;
  2668. sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
  2669. sg_buf_end = sg_bufp + extra_buffers_size;
  2670. off_min = 0;
  2671. for (; offp < off_end; offp++) {
  2672. struct binder_object_header *hdr;
  2673. size_t object_size = binder_validate_object(t->buffer, *offp);
  2674. if (object_size == 0 || *offp < off_min) {
  2675. binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
  2676. proc->pid, thread->pid, (u64)*offp,
  2677. (u64)off_min,
  2678. (u64)t->buffer->data_size);
  2679. return_error = BR_FAILED_REPLY;
  2680. return_error_param = -EINVAL;
  2681. return_error_line = __LINE__;
  2682. goto err_bad_offset;
  2683. }
  2684. hdr = (struct binder_object_header *)(t->buffer->data + *offp);
  2685. off_min = *offp + object_size;
  2686. switch (hdr->type) {
  2687. case BINDER_TYPE_BINDER:
  2688. case BINDER_TYPE_WEAK_BINDER: {
  2689. struct flat_binder_object *fp;
  2690. fp = to_flat_binder_object(hdr);
  2691. ret = binder_translate_binder(fp, t, thread);
  2692. if (ret < 0) {
  2693. return_error = BR_FAILED_REPLY;
  2694. return_error_param = ret;
  2695. return_error_line = __LINE__;
  2696. goto err_translate_failed;
  2697. }
  2698. } break;
  2699. case BINDER_TYPE_HANDLE:
  2700. case BINDER_TYPE_WEAK_HANDLE: {
  2701. struct flat_binder_object *fp;
  2702. fp = to_flat_binder_object(hdr);
  2703. ret = binder_translate_handle(fp, t, thread);
  2704. if (ret < 0) {
  2705. return_error = BR_FAILED_REPLY;
  2706. return_error_param = ret;
  2707. return_error_line = __LINE__;
  2708. goto err_translate_failed;
  2709. }
  2710. } break;
  2711. case BINDER_TYPE_FD: {
  2712. struct binder_fd_object *fp = to_binder_fd_object(hdr);
  2713. int target_fd = binder_translate_fd(fp->fd, t, thread,
  2714. in_reply_to);
  2715. if (target_fd < 0) {
  2716. return_error = BR_FAILED_REPLY;
  2717. return_error_param = target_fd;
  2718. return_error_line = __LINE__;
  2719. goto err_translate_failed;
  2720. }
  2721. fp->pad_binder = 0;
  2722. fp->fd = target_fd;
  2723. } break;
  2724. case BINDER_TYPE_FDA: {
  2725. struct binder_fd_array_object *fda =
  2726. to_binder_fd_array_object(hdr);
  2727. struct binder_buffer_object *parent =
  2728. binder_validate_ptr(t->buffer, fda->parent,
  2729. off_start,
  2730. offp - off_start);
  2731. if (!parent) {
  2732. binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
  2733. proc->pid, thread->pid);
  2734. return_error = BR_FAILED_REPLY;
  2735. return_error_param = -EINVAL;
  2736. return_error_line = __LINE__;
  2737. goto err_bad_parent;
  2738. }
  2739. if (!binder_validate_fixup(t->buffer, off_start,
  2740. parent, fda->parent_offset,
  2741. last_fixup_obj,
  2742. last_fixup_min_off)) {
  2743. binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
  2744. proc->pid, thread->pid);
  2745. return_error = BR_FAILED_REPLY;
  2746. return_error_param = -EINVAL;
  2747. return_error_line = __LINE__;
  2748. goto err_bad_parent;
  2749. }
  2750. ret = binder_translate_fd_array(fda, parent, t, thread,
  2751. in_reply_to);
  2752. if (ret < 0) {
  2753. return_error = BR_FAILED_REPLY;
  2754. return_error_param = ret;
  2755. return_error_line = __LINE__;
  2756. goto err_translate_failed;
  2757. }
  2758. last_fixup_obj = parent;
  2759. last_fixup_min_off =
  2760. fda->parent_offset + sizeof(u32) * fda->num_fds;
  2761. } break;
  2762. case BINDER_TYPE_PTR: {
  2763. struct binder_buffer_object *bp =
  2764. to_binder_buffer_object(hdr);
  2765. size_t buf_left = sg_buf_end - sg_bufp;
  2766. if (bp->length > buf_left) {
  2767. binder_user_error("%d:%d got transaction with too large buffer\n",
  2768. proc->pid, thread->pid);
  2769. return_error = BR_FAILED_REPLY;
  2770. return_error_param = -EINVAL;
  2771. return_error_line = __LINE__;
  2772. goto err_bad_offset;
  2773. }
  2774. if (copy_from_user(sg_bufp,
  2775. (const void __user *)(uintptr_t)
  2776. bp->buffer, bp->length)) {
  2777. binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
  2778. proc->pid, thread->pid);
  2779. return_error_param = -EFAULT;
  2780. return_error = BR_FAILED_REPLY;
  2781. return_error_line = __LINE__;
  2782. goto err_copy_data_failed;
  2783. }
  2784. /* Fixup buffer pointer to target proc address space */
  2785. bp->buffer = (uintptr_t)sg_bufp +
  2786. binder_alloc_get_user_buffer_offset(
  2787. &target_proc->alloc);
  2788. sg_bufp += ALIGN(bp->length, sizeof(u64));
  2789. ret = binder_fixup_parent(t, thread, bp, off_start,
  2790. offp - off_start,
  2791. last_fixup_obj,
  2792. last_fixup_min_off);
  2793. if (ret < 0) {
  2794. return_error = BR_FAILED_REPLY;
  2795. return_error_param = ret;
  2796. return_error_line = __LINE__;
  2797. goto err_translate_failed;
  2798. }
  2799. last_fixup_obj = bp;
  2800. last_fixup_min_off = 0;
  2801. } break;
  2802. default:
  2803. binder_user_error("%d:%d got transaction with invalid object type, %x\n",
  2804. proc->pid, thread->pid, hdr->type);
  2805. return_error = BR_FAILED_REPLY;
  2806. return_error_param = -EINVAL;
  2807. return_error_line = __LINE__;
  2808. goto err_bad_object_type;
  2809. }
  2810. }
  2811. tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
  2812. binder_enqueue_work(proc, tcomplete, &thread->todo);
  2813. t->work.type = BINDER_WORK_TRANSACTION;
  2814. if (reply) {
  2815. binder_inner_proc_lock(target_proc);
  2816. if (target_thread->is_dead) {
  2817. binder_inner_proc_unlock(target_proc);
  2818. goto err_dead_proc_or_thread;
  2819. }
  2820. BUG_ON(t->buffer->async_transaction != 0);
  2821. binder_pop_transaction_ilocked(target_thread, in_reply_to);
  2822. binder_enqueue_work_ilocked(&t->work, &target_thread->todo);
  2823. binder_inner_proc_unlock(target_proc);
  2824. wake_up_interruptible_sync(&target_thread->wait);
  2825. binder_free_transaction(in_reply_to);
  2826. } else if (!(t->flags & TF_ONE_WAY)) {
  2827. BUG_ON(t->buffer->async_transaction != 0);
  2828. binder_inner_proc_lock(proc);
  2829. t->need_reply = 1;
  2830. t->from_parent = thread->transaction_stack;
  2831. thread->transaction_stack = t;
  2832. binder_inner_proc_unlock(proc);
  2833. if (!binder_proc_transaction(t, target_proc, target_thread)) {
  2834. binder_inner_proc_lock(proc);
  2835. binder_pop_transaction_ilocked(thread, t);
  2836. binder_inner_proc_unlock(proc);
  2837. goto err_dead_proc_or_thread;
  2838. }
  2839. } else {
  2840. BUG_ON(target_node == NULL);
  2841. BUG_ON(t->buffer->async_transaction != 1);
  2842. if (!binder_proc_transaction(t, target_proc, NULL))
  2843. goto err_dead_proc_or_thread;
  2844. }
  2845. if (target_thread)
  2846. binder_thread_dec_tmpref(target_thread);
  2847. binder_proc_dec_tmpref(target_proc);
  2848. if (target_node)
  2849. binder_dec_node_tmpref(target_node);
  2850. /*
  2851. * write barrier to synchronize with initialization
  2852. * of log entry
  2853. */
  2854. smp_wmb();
  2855. WRITE_ONCE(e->debug_id_done, t_debug_id);
  2856. return;
  2857. err_dead_proc_or_thread:
  2858. return_error = BR_DEAD_REPLY;
  2859. return_error_line = __LINE__;
  2860. binder_dequeue_work(proc, tcomplete);
  2861. err_translate_failed:
  2862. err_bad_object_type:
  2863. err_bad_offset:
  2864. err_bad_parent:
  2865. err_copy_data_failed:
  2866. trace_binder_transaction_failed_buffer_release(t->buffer);
  2867. binder_transaction_buffer_release(target_proc, t->buffer, offp);
  2868. if (target_node)
  2869. binder_dec_node_tmpref(target_node);
  2870. target_node = NULL;
  2871. t->buffer->transaction = NULL;
  2872. binder_alloc_free_buf(&target_proc->alloc, t->buffer);
  2873. err_binder_alloc_buf_failed:
  2874. kfree(tcomplete);
  2875. binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
  2876. err_alloc_tcomplete_failed:
  2877. kfree(t);
  2878. binder_stats_deleted(BINDER_STAT_TRANSACTION);
  2879. err_alloc_t_failed:
  2880. err_bad_call_stack:
  2881. err_empty_call_stack:
  2882. err_dead_binder:
  2883. err_invalid_target_handle:
  2884. if (target_thread)
  2885. binder_thread_dec_tmpref(target_thread);
  2886. if (target_proc)
  2887. binder_proc_dec_tmpref(target_proc);
  2888. if (target_node) {
  2889. binder_dec_node(target_node, 1, 0);
  2890. binder_dec_node_tmpref(target_node);
  2891. }
  2892. binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
  2893. "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
  2894. proc->pid, thread->pid, return_error, return_error_param,
  2895. (u64)tr->data_size, (u64)tr->offsets_size,
  2896. return_error_line);
  2897. {
  2898. struct binder_transaction_log_entry *fe;
  2899. e->return_error = return_error;
  2900. e->return_error_param = return_error_param;
  2901. e->return_error_line = return_error_line;
  2902. fe = binder_transaction_log_add(&binder_transaction_log_failed);
  2903. *fe = *e;
  2904. /*
  2905. * write barrier to synchronize with initialization
  2906. * of log entry
  2907. */
  2908. smp_wmb();
  2909. WRITE_ONCE(e->debug_id_done, t_debug_id);
  2910. WRITE_ONCE(fe->debug_id_done, t_debug_id);
  2911. }
  2912. BUG_ON(thread->return_error.cmd != BR_OK);
  2913. if (in_reply_to) {
  2914. thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
  2915. binder_enqueue_work(thread->proc,
  2916. &thread->return_error.work,
  2917. &thread->todo);
  2918. binder_send_failed_reply(in_reply_to, return_error);
  2919. } else {
  2920. thread->return_error.cmd = return_error;
  2921. binder_enqueue_work(thread->proc,
  2922. &thread->return_error.work,
  2923. &thread->todo);
  2924. }
  2925. }
  2926. static int binder_thread_write(struct binder_proc *proc,
  2927. struct binder_thread *thread,
  2928. binder_uintptr_t binder_buffer, size_t size,
  2929. binder_size_t *consumed)
  2930. {
  2931. uint32_t cmd;
  2932. struct binder_context *context = proc->context;
  2933. void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
  2934. void __user *ptr = buffer + *consumed;
  2935. void __user *end = buffer + size;
  2936. while (ptr < end && thread->return_error.cmd == BR_OK) {
  2937. int ret;
  2938. if (get_user(cmd, (uint32_t __user *)ptr))
  2939. return -EFAULT;
  2940. ptr += sizeof(uint32_t);
  2941. trace_binder_command(cmd);
  2942. if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
  2943. atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
  2944. atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
  2945. atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
  2946. }
  2947. switch (cmd) {
  2948. case BC_INCREFS:
  2949. case BC_ACQUIRE:
  2950. case BC_RELEASE:
  2951. case BC_DECREFS: {
  2952. uint32_t target;
  2953. const char *debug_string;
  2954. bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
  2955. bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
  2956. struct binder_ref_data rdata;
  2957. if (get_user(target, (uint32_t __user *)ptr))
  2958. return -EFAULT;
  2959. ptr += sizeof(uint32_t);
  2960. ret = -1;
  2961. if (increment && !target) {
  2962. struct binder_node *ctx_mgr_node;
  2963. mutex_lock(&context->context_mgr_node_lock);
  2964. ctx_mgr_node = context->binder_context_mgr_node;
  2965. if (ctx_mgr_node)
  2966. ret = binder_inc_ref_for_node(
  2967. proc, ctx_mgr_node,
  2968. strong, NULL, &rdata);
  2969. mutex_unlock(&context->context_mgr_node_lock);
  2970. }
  2971. if (ret)
  2972. ret = binder_update_ref_for_handle(
  2973. proc, target, increment, strong,
  2974. &rdata);
  2975. if (!ret && rdata.desc != target) {
  2976. binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
  2977. proc->pid, thread->pid,
  2978. target, rdata.desc);
  2979. }
  2980. switch (cmd) {
  2981. case BC_INCREFS:
  2982. debug_string = "IncRefs";
  2983. break;
  2984. case BC_ACQUIRE:
  2985. debug_string = "Acquire";
  2986. break;
  2987. case BC_RELEASE:
  2988. debug_string = "Release";
  2989. break;
  2990. case BC_DECREFS:
  2991. default:
  2992. debug_string = "DecRefs";
  2993. break;
  2994. }
  2995. if (ret) {
  2996. binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
  2997. proc->pid, thread->pid, debug_string,
  2998. strong, target, ret);
  2999. break;
  3000. }
  3001. binder_debug(BINDER_DEBUG_USER_REFS,
  3002. "%d:%d %s ref %d desc %d s %d w %d\n",
  3003. proc->pid, thread->pid, debug_string,
  3004. rdata.debug_id, rdata.desc, rdata.strong,
  3005. rdata.weak);
  3006. break;
  3007. }
  3008. case BC_INCREFS_DONE:
  3009. case BC_ACQUIRE_DONE: {
  3010. binder_uintptr_t node_ptr;
  3011. binder_uintptr_t cookie;
  3012. struct binder_node *node;
  3013. bool free_node;
  3014. if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
  3015. return -EFAULT;
  3016. ptr += sizeof(binder_uintptr_t);
  3017. if (get_user(cookie, (binder_uintptr_t __user *)ptr))
  3018. return -EFAULT;
  3019. ptr += sizeof(binder_uintptr_t);
  3020. node = binder_get_node(proc, node_ptr);
  3021. if (node == NULL) {
  3022. binder_user_error("%d:%d %s u%016llx no match\n",
  3023. proc->pid, thread->pid,
  3024. cmd == BC_INCREFS_DONE ?
  3025. "BC_INCREFS_DONE" :
  3026. "BC_ACQUIRE_DONE",
  3027. (u64)node_ptr);
  3028. break;
  3029. }
  3030. if (cookie != node->cookie) {
  3031. binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
  3032. proc->pid, thread->pid,
  3033. cmd == BC_INCREFS_DONE ?
  3034. "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
  3035. (u64)node_ptr, node->debug_id,
  3036. (u64)cookie, (u64)node->cookie);
  3037. binder_put_node(node);
  3038. break;
  3039. }
  3040. binder_node_inner_lock(node);
  3041. if (cmd == BC_ACQUIRE_DONE) {
  3042. if (node->pending_strong_ref == 0) {
  3043. binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
  3044. proc->pid, thread->pid,
  3045. node->debug_id);
  3046. binder_node_inner_unlock(node);
  3047. binder_put_node(node);
  3048. break;
  3049. }
  3050. node->pending_strong_ref = 0;
  3051. } else {
  3052. if (node->pending_weak_ref == 0) {
  3053. binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
  3054. proc->pid, thread->pid,
  3055. node->debug_id);
  3056. binder_node_inner_unlock(node);
  3057. binder_put_node(node);
  3058. break;
  3059. }
  3060. node->pending_weak_ref = 0;
  3061. }
  3062. free_node = binder_dec_node_nilocked(node,
  3063. cmd == BC_ACQUIRE_DONE, 0);
  3064. WARN_ON(free_node);
  3065. binder_debug(BINDER_DEBUG_USER_REFS,
  3066. "%d:%d %s node %d ls %d lw %d tr %d\n",
  3067. proc->pid, thread->pid,
  3068. cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
  3069. node->debug_id, node->local_strong_refs,
  3070. node->local_weak_refs, node->tmp_refs);
  3071. binder_node_inner_unlock(node);
  3072. binder_put_node(node);
  3073. break;
  3074. }
  3075. case BC_ATTEMPT_ACQUIRE:
  3076. pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
  3077. return -EINVAL;
  3078. case BC_ACQUIRE_RESULT:
  3079. pr_err("BC_ACQUIRE_RESULT not supported\n");
  3080. return -EINVAL;
  3081. case BC_FREE_BUFFER: {
  3082. binder_uintptr_t data_ptr;
  3083. struct binder_buffer *buffer;
  3084. if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
  3085. return -EFAULT;
  3086. ptr += sizeof(binder_uintptr_t);
  3087. buffer = binder_alloc_prepare_to_free(&proc->alloc,
  3088. data_ptr);
  3089. if (buffer == NULL) {
  3090. binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
  3091. proc->pid, thread->pid, (u64)data_ptr);
  3092. break;
  3093. }
  3094. if (!buffer->allow_user_free) {
  3095. binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
  3096. proc->pid, thread->pid, (u64)data_ptr);
  3097. break;
  3098. }
  3099. binder_debug(BINDER_DEBUG_FREE_BUFFER,
  3100. "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
  3101. proc->pid, thread->pid, (u64)data_ptr,
  3102. buffer->debug_id,
  3103. buffer->transaction ? "active" : "finished");
  3104. if (buffer->transaction) {
  3105. buffer->transaction->buffer = NULL;
  3106. buffer->transaction = NULL;
  3107. }
  3108. if (buffer->async_transaction && buffer->target_node) {
  3109. struct binder_node *buf_node;
  3110. struct binder_work *w;
  3111. buf_node = buffer->target_node;
  3112. binder_node_inner_lock(buf_node);
  3113. BUG_ON(!buf_node->has_async_transaction);
  3114. BUG_ON(buf_node->proc != proc);
  3115. w = binder_dequeue_work_head_ilocked(
  3116. &buf_node->async_todo);
  3117. if (!w) {
  3118. buf_node->has_async_transaction = 0;
  3119. } else {
  3120. binder_enqueue_work_ilocked(
  3121. w, &proc->todo);
  3122. binder_wakeup_proc_ilocked(proc);
  3123. }
  3124. binder_node_inner_unlock(buf_node);
  3125. }
  3126. trace_binder_transaction_buffer_release(buffer);
  3127. binder_transaction_buffer_release(proc, buffer, NULL);
  3128. binder_alloc_free_buf(&proc->alloc, buffer);
  3129. break;
  3130. }
  3131. case BC_TRANSACTION_SG:
  3132. case BC_REPLY_SG: {
  3133. struct binder_transaction_data_sg tr;
  3134. if (copy_from_user(&tr, ptr, sizeof(tr)))
  3135. return -EFAULT;
  3136. ptr += sizeof(tr);
  3137. binder_transaction(proc, thread, &tr.transaction_data,
  3138. cmd == BC_REPLY_SG, tr.buffers_size);
  3139. break;
  3140. }
  3141. case BC_TRANSACTION:
  3142. case BC_REPLY: {
  3143. struct binder_transaction_data tr;
  3144. if (copy_from_user(&tr, ptr, sizeof(tr)))
  3145. return -EFAULT;
  3146. ptr += sizeof(tr);
  3147. binder_transaction(proc, thread, &tr,
  3148. cmd == BC_REPLY, 0);
  3149. break;
  3150. }
  3151. case BC_REGISTER_LOOPER:
  3152. binder_debug(BINDER_DEBUG_THREADS,
  3153. "%d:%d BC_REGISTER_LOOPER\n",
  3154. proc->pid, thread->pid);
  3155. binder_inner_proc_lock(proc);
  3156. if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
  3157. thread->looper |= BINDER_LOOPER_STATE_INVALID;
  3158. binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
  3159. proc->pid, thread->pid);
  3160. } else if (proc->requested_threads == 0) {
  3161. thread->looper |= BINDER_LOOPER_STATE_INVALID;
  3162. binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
  3163. proc->pid, thread->pid);
  3164. } else {
  3165. proc->requested_threads--;
  3166. proc->requested_threads_started++;
  3167. }
  3168. thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
  3169. binder_inner_proc_unlock(proc);
  3170. break;
  3171. case BC_ENTER_LOOPER:
  3172. binder_debug(BINDER_DEBUG_THREADS,
  3173. "%d:%d BC_ENTER_LOOPER\n",
  3174. proc->pid, thread->pid);
  3175. if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
  3176. thread->looper |= BINDER_LOOPER_STATE_INVALID;
  3177. binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
  3178. proc->pid, thread->pid);
  3179. }
  3180. thread->looper |= BINDER_LOOPER_STATE_ENTERED;
  3181. break;
  3182. case BC_EXIT_LOOPER:
  3183. binder_debug(BINDER_DEBUG_THREADS,
  3184. "%d:%d BC_EXIT_LOOPER\n",
  3185. proc->pid, thread->pid);
  3186. thread->looper |= BINDER_LOOPER_STATE_EXITED;
  3187. break;
  3188. case BC_REQUEST_DEATH_NOTIFICATION:
  3189. case BC_CLEAR_DEATH_NOTIFICATION: {
  3190. uint32_t target;
  3191. binder_uintptr_t cookie;
  3192. struct binder_ref *ref;
  3193. struct binder_ref_death *death = NULL;
  3194. if (get_user(target, (uint32_t __user *)ptr))
  3195. return -EFAULT;
  3196. ptr += sizeof(uint32_t);
  3197. if (get_user(cookie, (binder_uintptr_t __user *)ptr))
  3198. return -EFAULT;
  3199. ptr += sizeof(binder_uintptr_t);
  3200. if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
  3201. /*
  3202. * Allocate memory for death notification
  3203. * before taking lock
  3204. */
  3205. death = kzalloc(sizeof(*death), GFP_KERNEL);
  3206. if (death == NULL) {
  3207. WARN_ON(thread->return_error.cmd !=
  3208. BR_OK);
  3209. thread->return_error.cmd = BR_ERROR;
  3210. binder_enqueue_work(
  3211. thread->proc,
  3212. &thread->return_error.work,
  3213. &thread->todo);
  3214. binder_debug(
  3215. BINDER_DEBUG_FAILED_TRANSACTION,
  3216. "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
  3217. proc->pid, thread->pid);
  3218. break;
  3219. }
  3220. }
  3221. binder_proc_lock(proc);
  3222. ref = binder_get_ref_olocked(proc, target, false);
  3223. if (ref == NULL) {
  3224. binder_user_error("%d:%d %s invalid ref %d\n",
  3225. proc->pid, thread->pid,
  3226. cmd == BC_REQUEST_DEATH_NOTIFICATION ?
  3227. "BC_REQUEST_DEATH_NOTIFICATION" :
  3228. "BC_CLEAR_DEATH_NOTIFICATION",
  3229. target);
  3230. binder_proc_unlock(proc);
  3231. kfree(death);
  3232. break;
  3233. }
  3234. binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
  3235. "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
  3236. proc->pid, thread->pid,
  3237. cmd == BC_REQUEST_DEATH_NOTIFICATION ?
  3238. "BC_REQUEST_DEATH_NOTIFICATION" :
  3239. "BC_CLEAR_DEATH_NOTIFICATION",
  3240. (u64)cookie, ref->data.debug_id,
  3241. ref->data.desc, ref->data.strong,
  3242. ref->data.weak, ref->node->debug_id);
  3243. binder_node_lock(ref->node);
  3244. if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
  3245. if (ref->death) {
  3246. binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
  3247. proc->pid, thread->pid);
  3248. binder_node_unlock(ref->node);
  3249. binder_proc_unlock(proc);
  3250. kfree(death);
  3251. break;
  3252. }
  3253. binder_stats_created(BINDER_STAT_DEATH);
  3254. INIT_LIST_HEAD(&death->work.entry);
  3255. death->cookie = cookie;
  3256. ref->death = death;
  3257. if (ref->node->proc == NULL) {
  3258. ref->death->work.type = BINDER_WORK_DEAD_BINDER;
  3259. binder_inner_proc_lock(proc);
  3260. binder_enqueue_work_ilocked(
  3261. &ref->death->work, &proc->todo);
  3262. binder_wakeup_proc_ilocked(proc);
  3263. binder_inner_proc_unlock(proc);
  3264. }
  3265. } else {
  3266. if (ref->death == NULL) {
  3267. binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
  3268. proc->pid, thread->pid);
  3269. binder_node_unlock(ref->node);
  3270. binder_proc_unlock(proc);
  3271. break;
  3272. }
  3273. death = ref->death;
  3274. if (death->cookie != cookie) {
  3275. binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
  3276. proc->pid, thread->pid,
  3277. (u64)death->cookie,
  3278. (u64)cookie);
  3279. binder_node_unlock(ref->node);
  3280. binder_proc_unlock(proc);
  3281. break;
  3282. }
  3283. ref->death = NULL;
  3284. binder_inner_proc_lock(proc);
  3285. if (list_empty(&death->work.entry)) {
  3286. death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
  3287. if (thread->looper &
  3288. (BINDER_LOOPER_STATE_REGISTERED |
  3289. BINDER_LOOPER_STATE_ENTERED))
  3290. binder_enqueue_work_ilocked(
  3291. &death->work,
  3292. &thread->todo);
  3293. else {
  3294. binder_enqueue_work_ilocked(
  3295. &death->work,
  3296. &proc->todo);
  3297. binder_wakeup_proc_ilocked(
  3298. proc);
  3299. }
  3300. } else {
  3301. BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
  3302. death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
  3303. }
  3304. binder_inner_proc_unlock(proc);
  3305. }
  3306. binder_node_unlock(ref->node);
  3307. binder_proc_unlock(proc);
  3308. } break;
  3309. case BC_DEAD_BINDER_DONE: {
  3310. struct binder_work *w;
  3311. binder_uintptr_t cookie;
  3312. struct binder_ref_death *death = NULL;
  3313. if (get_user(cookie, (binder_uintptr_t __user *)ptr))
  3314. return -EFAULT;
  3315. ptr += sizeof(cookie);
  3316. binder_inner_proc_lock(proc);
  3317. list_for_each_entry(w, &proc->delivered_death,
  3318. entry) {
  3319. struct binder_ref_death *tmp_death =
  3320. container_of(w,
  3321. struct binder_ref_death,
  3322. work);
  3323. if (tmp_death->cookie == cookie) {
  3324. death = tmp_death;
  3325. break;
  3326. }
  3327. }
  3328. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  3329. "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
  3330. proc->pid, thread->pid, (u64)cookie,
  3331. death);
  3332. if (death == NULL) {
  3333. binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
  3334. proc->pid, thread->pid, (u64)cookie);
  3335. binder_inner_proc_unlock(proc);
  3336. break;
  3337. }
  3338. binder_dequeue_work_ilocked(&death->work);
  3339. if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
  3340. death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
  3341. if (thread->looper &
  3342. (BINDER_LOOPER_STATE_REGISTERED |
  3343. BINDER_LOOPER_STATE_ENTERED))
  3344. binder_enqueue_work_ilocked(
  3345. &death->work, &thread->todo);
  3346. else {
  3347. binder_enqueue_work_ilocked(
  3348. &death->work,
  3349. &proc->todo);
  3350. binder_wakeup_proc_ilocked(proc);
  3351. }
  3352. }
  3353. binder_inner_proc_unlock(proc);
  3354. } break;
  3355. default:
  3356. pr_err("%d:%d unknown command %d\n",
  3357. proc->pid, thread->pid, cmd);
  3358. return -EINVAL;
  3359. }
  3360. *consumed = ptr - buffer;
  3361. }
  3362. return 0;
  3363. }
  3364. static void binder_stat_br(struct binder_proc *proc,
  3365. struct binder_thread *thread, uint32_t cmd)
  3366. {
  3367. trace_binder_return(cmd);
  3368. if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
  3369. atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
  3370. atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
  3371. atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
  3372. }
  3373. }
  3374. static int binder_put_node_cmd(struct binder_proc *proc,
  3375. struct binder_thread *thread,
  3376. void __user **ptrp,
  3377. binder_uintptr_t node_ptr,
  3378. binder_uintptr_t node_cookie,
  3379. int node_debug_id,
  3380. uint32_t cmd, const char *cmd_name)
  3381. {
  3382. void __user *ptr = *ptrp;
  3383. if (put_user(cmd, (uint32_t __user *)ptr))
  3384. return -EFAULT;
  3385. ptr += sizeof(uint32_t);
  3386. if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
  3387. return -EFAULT;
  3388. ptr += sizeof(binder_uintptr_t);
  3389. if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
  3390. return -EFAULT;
  3391. ptr += sizeof(binder_uintptr_t);
  3392. binder_stat_br(proc, thread, cmd);
  3393. binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
  3394. proc->pid, thread->pid, cmd_name, node_debug_id,
  3395. (u64)node_ptr, (u64)node_cookie);
  3396. *ptrp = ptr;
  3397. return 0;
  3398. }
  3399. static int binder_wait_for_work(struct binder_thread *thread,
  3400. bool do_proc_work)
  3401. {
  3402. DEFINE_WAIT(wait);
  3403. struct binder_proc *proc = thread->proc;
  3404. int ret = 0;
  3405. freezer_do_not_count();
  3406. binder_inner_proc_lock(proc);
  3407. for (;;) {
  3408. prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
  3409. if (binder_has_work_ilocked(thread, do_proc_work))
  3410. break;
  3411. if (do_proc_work)
  3412. list_add(&thread->waiting_thread_node,
  3413. &proc->waiting_threads);
  3414. binder_inner_proc_unlock(proc);
  3415. schedule();
  3416. binder_inner_proc_lock(proc);
  3417. list_del_init(&thread->waiting_thread_node);
  3418. if (signal_pending(current)) {
  3419. ret = -ERESTARTSYS;
  3420. break;
  3421. }
  3422. }
  3423. finish_wait(&thread->wait, &wait);
  3424. binder_inner_proc_unlock(proc);
  3425. freezer_count();
  3426. return ret;
  3427. }
  3428. static int binder_thread_read(struct binder_proc *proc,
  3429. struct binder_thread *thread,
  3430. binder_uintptr_t binder_buffer, size_t size,
  3431. binder_size_t *consumed, int non_block)
  3432. {
  3433. void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
  3434. void __user *ptr = buffer + *consumed;
  3435. void __user *end = buffer + size;
  3436. int ret = 0;
  3437. int wait_for_proc_work;
  3438. if (*consumed == 0) {
  3439. if (put_user(BR_NOOP, (uint32_t __user *)ptr))
  3440. return -EFAULT;
  3441. ptr += sizeof(uint32_t);
  3442. }
  3443. retry:
  3444. binder_inner_proc_lock(proc);
  3445. wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
  3446. binder_inner_proc_unlock(proc);
  3447. thread->looper |= BINDER_LOOPER_STATE_WAITING;
  3448. trace_binder_wait_for_work(wait_for_proc_work,
  3449. !!thread->transaction_stack,
  3450. !binder_worklist_empty(proc, &thread->todo));
  3451. if (wait_for_proc_work) {
  3452. if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
  3453. BINDER_LOOPER_STATE_ENTERED))) {
  3454. binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
  3455. proc->pid, thread->pid, thread->looper);
  3456. wait_event_interruptible(binder_user_error_wait,
  3457. binder_stop_on_user_error < 2);
  3458. }
  3459. binder_set_nice(proc->default_priority);
  3460. }
  3461. if (non_block) {
  3462. if (!binder_has_work(thread, wait_for_proc_work))
  3463. ret = -EAGAIN;
  3464. } else {
  3465. ret = binder_wait_for_work(thread, wait_for_proc_work);
  3466. }
  3467. thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
  3468. if (ret)
  3469. return ret;
  3470. while (1) {
  3471. uint32_t cmd;
  3472. struct binder_transaction_data tr;
  3473. struct binder_work *w = NULL;
  3474. struct list_head *list = NULL;
  3475. struct binder_transaction *t = NULL;
  3476. struct binder_thread *t_from;
  3477. binder_inner_proc_lock(proc);
  3478. if (!binder_worklist_empty_ilocked(&thread->todo))
  3479. list = &thread->todo;
  3480. else if (!binder_worklist_empty_ilocked(&proc->todo) &&
  3481. wait_for_proc_work)
  3482. list = &proc->todo;
  3483. else {
  3484. binder_inner_proc_unlock(proc);
  3485. /* no data added */
  3486. if (ptr - buffer == 4 && !thread->looper_need_return)
  3487. goto retry;
  3488. break;
  3489. }
  3490. if (end - ptr < sizeof(tr) + 4) {
  3491. binder_inner_proc_unlock(proc);
  3492. break;
  3493. }
  3494. w = binder_dequeue_work_head_ilocked(list);
  3495. switch (w->type) {
  3496. case BINDER_WORK_TRANSACTION: {
  3497. binder_inner_proc_unlock(proc);
  3498. t = container_of(w, struct binder_transaction, work);
  3499. } break;
  3500. case BINDER_WORK_RETURN_ERROR: {
  3501. struct binder_error *e = container_of(
  3502. w, struct binder_error, work);
  3503. WARN_ON(e->cmd == BR_OK);
  3504. binder_inner_proc_unlock(proc);
  3505. if (put_user(e->cmd, (uint32_t __user *)ptr))
  3506. return -EFAULT;
  3507. e->cmd = BR_OK;
  3508. ptr += sizeof(uint32_t);
  3509. binder_stat_br(proc, thread, e->cmd);
  3510. } break;
  3511. case BINDER_WORK_TRANSACTION_COMPLETE: {
  3512. binder_inner_proc_unlock(proc);
  3513. cmd = BR_TRANSACTION_COMPLETE;
  3514. if (put_user(cmd, (uint32_t __user *)ptr))
  3515. return -EFAULT;
  3516. ptr += sizeof(uint32_t);
  3517. binder_stat_br(proc, thread, cmd);
  3518. binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
  3519. "%d:%d BR_TRANSACTION_COMPLETE\n",
  3520. proc->pid, thread->pid);
  3521. kfree(w);
  3522. binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
  3523. } break;
  3524. case BINDER_WORK_NODE: {
  3525. struct binder_node *node = container_of(w, struct binder_node, work);
  3526. int strong, weak;
  3527. binder_uintptr_t node_ptr = node->ptr;
  3528. binder_uintptr_t node_cookie = node->cookie;
  3529. int node_debug_id = node->debug_id;
  3530. int has_weak_ref;
  3531. int has_strong_ref;
  3532. void __user *orig_ptr = ptr;
  3533. BUG_ON(proc != node->proc);
  3534. strong = node->internal_strong_refs ||
  3535. node->local_strong_refs;
  3536. weak = !hlist_empty(&node->refs) ||
  3537. node->local_weak_refs ||
  3538. node->tmp_refs || strong;
  3539. has_strong_ref = node->has_strong_ref;
  3540. has_weak_ref = node->has_weak_ref;
  3541. if (weak && !has_weak_ref) {
  3542. node->has_weak_ref = 1;
  3543. node->pending_weak_ref = 1;
  3544. node->local_weak_refs++;
  3545. }
  3546. if (strong && !has_strong_ref) {
  3547. node->has_strong_ref = 1;
  3548. node->pending_strong_ref = 1;
  3549. node->local_strong_refs++;
  3550. }
  3551. if (!strong && has_strong_ref)
  3552. node->has_strong_ref = 0;
  3553. if (!weak && has_weak_ref)
  3554. node->has_weak_ref = 0;
  3555. if (!weak && !strong) {
  3556. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  3557. "%d:%d node %d u%016llx c%016llx deleted\n",
  3558. proc->pid, thread->pid,
  3559. node_debug_id,
  3560. (u64)node_ptr,
  3561. (u64)node_cookie);
  3562. rb_erase(&node->rb_node, &proc->nodes);
  3563. binder_inner_proc_unlock(proc);
  3564. binder_node_lock(node);
  3565. /*
  3566. * Acquire the node lock before freeing the
  3567. * node to serialize with other threads that
  3568. * may have been holding the node lock while
  3569. * decrementing this node (avoids race where
  3570. * this thread frees while the other thread
  3571. * is unlocking the node after the final
  3572. * decrement)
  3573. */
  3574. binder_node_unlock(node);
  3575. binder_free_node(node);
  3576. } else
  3577. binder_inner_proc_unlock(proc);
  3578. if (weak && !has_weak_ref)
  3579. ret = binder_put_node_cmd(
  3580. proc, thread, &ptr, node_ptr,
  3581. node_cookie, node_debug_id,
  3582. BR_INCREFS, "BR_INCREFS");
  3583. if (!ret && strong && !has_strong_ref)
  3584. ret = binder_put_node_cmd(
  3585. proc, thread, &ptr, node_ptr,
  3586. node_cookie, node_debug_id,
  3587. BR_ACQUIRE, "BR_ACQUIRE");
  3588. if (!ret && !strong && has_strong_ref)
  3589. ret = binder_put_node_cmd(
  3590. proc, thread, &ptr, node_ptr,
  3591. node_cookie, node_debug_id,
  3592. BR_RELEASE, "BR_RELEASE");
  3593. if (!ret && !weak && has_weak_ref)
  3594. ret = binder_put_node_cmd(
  3595. proc, thread, &ptr, node_ptr,
  3596. node_cookie, node_debug_id,
  3597. BR_DECREFS, "BR_DECREFS");
  3598. if (orig_ptr == ptr)
  3599. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  3600. "%d:%d node %d u%016llx c%016llx state unchanged\n",
  3601. proc->pid, thread->pid,
  3602. node_debug_id,
  3603. (u64)node_ptr,
  3604. (u64)node_cookie);
  3605. if (ret)
  3606. return ret;
  3607. } break;
  3608. case BINDER_WORK_DEAD_BINDER:
  3609. case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
  3610. case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
  3611. struct binder_ref_death *death;
  3612. uint32_t cmd;
  3613. binder_uintptr_t cookie;
  3614. death = container_of(w, struct binder_ref_death, work);
  3615. if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
  3616. cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
  3617. else
  3618. cmd = BR_DEAD_BINDER;
  3619. cookie = death->cookie;
  3620. binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
  3621. "%d:%d %s %016llx\n",
  3622. proc->pid, thread->pid,
  3623. cmd == BR_DEAD_BINDER ?
  3624. "BR_DEAD_BINDER" :
  3625. "BR_CLEAR_DEATH_NOTIFICATION_DONE",
  3626. (u64)cookie);
  3627. if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
  3628. binder_inner_proc_unlock(proc);
  3629. kfree(death);
  3630. binder_stats_deleted(BINDER_STAT_DEATH);
  3631. } else {
  3632. binder_enqueue_work_ilocked(
  3633. w, &proc->delivered_death);
  3634. binder_inner_proc_unlock(proc);
  3635. }
  3636. if (put_user(cmd, (uint32_t __user *)ptr))
  3637. return -EFAULT;
  3638. ptr += sizeof(uint32_t);
  3639. if (put_user(cookie,
  3640. (binder_uintptr_t __user *)ptr))
  3641. return -EFAULT;
  3642. ptr += sizeof(binder_uintptr_t);
  3643. binder_stat_br(proc, thread, cmd);
  3644. if (cmd == BR_DEAD_BINDER)
  3645. goto done; /* DEAD_BINDER notifications can cause transactions */
  3646. } break;
  3647. }
  3648. if (!t)
  3649. continue;
  3650. BUG_ON(t->buffer == NULL);
  3651. if (t->buffer->target_node) {
  3652. struct binder_node *target_node = t->buffer->target_node;
  3653. tr.target.ptr = target_node->ptr;
  3654. tr.cookie = target_node->cookie;
  3655. t->saved_priority = task_nice(current);
  3656. if (t->priority < target_node->min_priority &&
  3657. !(t->flags & TF_ONE_WAY))
  3658. binder_set_nice(t->priority);
  3659. else if (!(t->flags & TF_ONE_WAY) ||
  3660. t->saved_priority > target_node->min_priority)
  3661. binder_set_nice(target_node->min_priority);
  3662. cmd = BR_TRANSACTION;
  3663. } else {
  3664. tr.target.ptr = 0;
  3665. tr.cookie = 0;
  3666. cmd = BR_REPLY;
  3667. }
  3668. tr.code = t->code;
  3669. tr.flags = t->flags;
  3670. tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
  3671. t_from = binder_get_txn_from(t);
  3672. if (t_from) {
  3673. struct task_struct *sender = t_from->proc->tsk;
  3674. tr.sender_pid = task_tgid_nr_ns(sender,
  3675. task_active_pid_ns(current));
  3676. } else {
  3677. tr.sender_pid = 0;
  3678. }
  3679. tr.data_size = t->buffer->data_size;
  3680. tr.offsets_size = t->buffer->offsets_size;
  3681. tr.data.ptr.buffer = (binder_uintptr_t)
  3682. ((uintptr_t)t->buffer->data +
  3683. binder_alloc_get_user_buffer_offset(&proc->alloc));
  3684. tr.data.ptr.offsets = tr.data.ptr.buffer +
  3685. ALIGN(t->buffer->data_size,
  3686. sizeof(void *));
  3687. if (put_user(cmd, (uint32_t __user *)ptr)) {
  3688. if (t_from)
  3689. binder_thread_dec_tmpref(t_from);
  3690. return -EFAULT;
  3691. }
  3692. ptr += sizeof(uint32_t);
  3693. if (copy_to_user(ptr, &tr, sizeof(tr))) {
  3694. if (t_from)
  3695. binder_thread_dec_tmpref(t_from);
  3696. return -EFAULT;
  3697. }
  3698. ptr += sizeof(tr);
  3699. trace_binder_transaction_received(t);
  3700. binder_stat_br(proc, thread, cmd);
  3701. binder_debug(BINDER_DEBUG_TRANSACTION,
  3702. "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
  3703. proc->pid, thread->pid,
  3704. (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
  3705. "BR_REPLY",
  3706. t->debug_id, t_from ? t_from->proc->pid : 0,
  3707. t_from ? t_from->pid : 0, cmd,
  3708. t->buffer->data_size, t->buffer->offsets_size,
  3709. (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
  3710. if (t_from)
  3711. binder_thread_dec_tmpref(t_from);
  3712. t->buffer->allow_user_free = 1;
  3713. if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
  3714. binder_inner_proc_lock(thread->proc);
  3715. t->to_parent = thread->transaction_stack;
  3716. t->to_thread = thread;
  3717. thread->transaction_stack = t;
  3718. binder_inner_proc_unlock(thread->proc);
  3719. } else {
  3720. binder_free_transaction(t);
  3721. }
  3722. break;
  3723. }
  3724. done:
  3725. *consumed = ptr - buffer;
  3726. binder_inner_proc_lock(proc);
  3727. if (proc->requested_threads == 0 &&
  3728. list_empty(&thread->proc->waiting_threads) &&
  3729. proc->requested_threads_started < proc->max_threads &&
  3730. (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
  3731. BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
  3732. /*spawn a new thread if we leave this out */) {
  3733. proc->requested_threads++;
  3734. binder_inner_proc_unlock(proc);
  3735. binder_debug(BINDER_DEBUG_THREADS,
  3736. "%d:%d BR_SPAWN_LOOPER\n",
  3737. proc->pid, thread->pid);
  3738. if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
  3739. return -EFAULT;
  3740. binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
  3741. } else
  3742. binder_inner_proc_unlock(proc);
  3743. return 0;
  3744. }
  3745. static void binder_release_work(struct binder_proc *proc,
  3746. struct list_head *list)
  3747. {
  3748. struct binder_work *w;
  3749. while (1) {
  3750. w = binder_dequeue_work_head(proc, list);
  3751. if (!w)
  3752. return;
  3753. switch (w->type) {
  3754. case BINDER_WORK_TRANSACTION: {
  3755. struct binder_transaction *t;
  3756. t = container_of(w, struct binder_transaction, work);
  3757. if (t->buffer->target_node &&
  3758. !(t->flags & TF_ONE_WAY)) {
  3759. binder_send_failed_reply(t, BR_DEAD_REPLY);
  3760. } else {
  3761. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  3762. "undelivered transaction %d\n",
  3763. t->debug_id);
  3764. binder_free_transaction(t);
  3765. }
  3766. } break;
  3767. case BINDER_WORK_RETURN_ERROR: {
  3768. struct binder_error *e = container_of(
  3769. w, struct binder_error, work);
  3770. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  3771. "undelivered TRANSACTION_ERROR: %u\n",
  3772. e->cmd);
  3773. } break;
  3774. case BINDER_WORK_TRANSACTION_COMPLETE: {
  3775. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  3776. "undelivered TRANSACTION_COMPLETE\n");
  3777. kfree(w);
  3778. binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
  3779. } break;
  3780. case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
  3781. case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
  3782. struct binder_ref_death *death;
  3783. death = container_of(w, struct binder_ref_death, work);
  3784. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  3785. "undelivered death notification, %016llx\n",
  3786. (u64)death->cookie);
  3787. kfree(death);
  3788. binder_stats_deleted(BINDER_STAT_DEATH);
  3789. } break;
  3790. default:
  3791. pr_err("unexpected work type, %d, not freed\n",
  3792. w->type);
  3793. break;
  3794. }
  3795. }
  3796. }
  3797. static struct binder_thread *binder_get_thread_ilocked(
  3798. struct binder_proc *proc, struct binder_thread *new_thread)
  3799. {
  3800. struct binder_thread *thread = NULL;
  3801. struct rb_node *parent = NULL;
  3802. struct rb_node **p = &proc->threads.rb_node;
  3803. while (*p) {
  3804. parent = *p;
  3805. thread = rb_entry(parent, struct binder_thread, rb_node);
  3806. if (current->pid < thread->pid)
  3807. p = &(*p)->rb_left;
  3808. else if (current->pid > thread->pid)
  3809. p = &(*p)->rb_right;
  3810. else
  3811. return thread;
  3812. }
  3813. if (!new_thread)
  3814. return NULL;
  3815. thread = new_thread;
  3816. binder_stats_created(BINDER_STAT_THREAD);
  3817. thread->proc = proc;
  3818. thread->pid = current->pid;
  3819. atomic_set(&thread->tmp_ref, 0);
  3820. init_waitqueue_head(&thread->wait);
  3821. INIT_LIST_HEAD(&thread->todo);
  3822. rb_link_node(&thread->rb_node, parent, p);
  3823. rb_insert_color(&thread->rb_node, &proc->threads);
  3824. thread->looper_need_return = true;
  3825. thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
  3826. thread->return_error.cmd = BR_OK;
  3827. thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
  3828. thread->reply_error.cmd = BR_OK;
  3829. INIT_LIST_HEAD(&new_thread->waiting_thread_node);
  3830. return thread;
  3831. }
  3832. static struct binder_thread *binder_get_thread(struct binder_proc *proc)
  3833. {
  3834. struct binder_thread *thread;
  3835. struct binder_thread *new_thread;
  3836. binder_inner_proc_lock(proc);
  3837. thread = binder_get_thread_ilocked(proc, NULL);
  3838. binder_inner_proc_unlock(proc);
  3839. if (!thread) {
  3840. new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
  3841. if (new_thread == NULL)
  3842. return NULL;
  3843. binder_inner_proc_lock(proc);
  3844. thread = binder_get_thread_ilocked(proc, new_thread);
  3845. binder_inner_proc_unlock(proc);
  3846. if (thread != new_thread)
  3847. kfree(new_thread);
  3848. }
  3849. return thread;
  3850. }
  3851. static void binder_free_proc(struct binder_proc *proc)
  3852. {
  3853. BUG_ON(!list_empty(&proc->todo));
  3854. BUG_ON(!list_empty(&proc->delivered_death));
  3855. binder_alloc_deferred_release(&proc->alloc);
  3856. put_task_struct(proc->tsk);
  3857. binder_stats_deleted(BINDER_STAT_PROC);
  3858. kfree(proc);
  3859. }
  3860. static void binder_free_thread(struct binder_thread *thread)
  3861. {
  3862. BUG_ON(!list_empty(&thread->todo));
  3863. binder_stats_deleted(BINDER_STAT_THREAD);
  3864. binder_proc_dec_tmpref(thread->proc);
  3865. kfree(thread);
  3866. }
  3867. static int binder_thread_release(struct binder_proc *proc,
  3868. struct binder_thread *thread)
  3869. {
  3870. struct binder_transaction *t;
  3871. struct binder_transaction *send_reply = NULL;
  3872. int active_transactions = 0;
  3873. struct binder_transaction *last_t = NULL;
  3874. binder_inner_proc_lock(thread->proc);
  3875. /*
  3876. * take a ref on the proc so it survives
  3877. * after we remove this thread from proc->threads.
  3878. * The corresponding dec is when we actually
  3879. * free the thread in binder_free_thread()
  3880. */
  3881. proc->tmp_ref++;
  3882. /*
  3883. * take a ref on this thread to ensure it
  3884. * survives while we are releasing it
  3885. */
  3886. atomic_inc(&thread->tmp_ref);
  3887. rb_erase(&thread->rb_node, &proc->threads);
  3888. t = thread->transaction_stack;
  3889. if (t) {
  3890. spin_lock(&t->lock);
  3891. if (t->to_thread == thread)
  3892. send_reply = t;
  3893. }
  3894. thread->is_dead = true;
  3895. while (t) {
  3896. last_t = t;
  3897. active_transactions++;
  3898. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  3899. "release %d:%d transaction %d %s, still active\n",
  3900. proc->pid, thread->pid,
  3901. t->debug_id,
  3902. (t->to_thread == thread) ? "in" : "out");
  3903. if (t->to_thread == thread) {
  3904. t->to_proc = NULL;
  3905. t->to_thread = NULL;
  3906. if (t->buffer) {
  3907. t->buffer->transaction = NULL;
  3908. t->buffer = NULL;
  3909. }
  3910. t = t->to_parent;
  3911. } else if (t->from == thread) {
  3912. t->from = NULL;
  3913. t = t->from_parent;
  3914. } else
  3915. BUG();
  3916. spin_unlock(&last_t->lock);
  3917. if (t)
  3918. spin_lock(&t->lock);
  3919. }
  3920. binder_inner_proc_unlock(thread->proc);
  3921. if (send_reply)
  3922. binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
  3923. binder_release_work(proc, &thread->todo);
  3924. binder_thread_dec_tmpref(thread);
  3925. return active_transactions;
  3926. }
  3927. static unsigned int binder_poll(struct file *filp,
  3928. struct poll_table_struct *wait)
  3929. {
  3930. struct binder_proc *proc = filp->private_data;
  3931. struct binder_thread *thread = NULL;
  3932. bool wait_for_proc_work;
  3933. thread = binder_get_thread(proc);
  3934. binder_inner_proc_lock(thread->proc);
  3935. thread->looper |= BINDER_LOOPER_STATE_POLL;
  3936. wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
  3937. binder_inner_proc_unlock(thread->proc);
  3938. poll_wait(filp, &thread->wait, wait);
  3939. if (binder_has_work(thread, wait_for_proc_work))
  3940. return POLLIN;
  3941. return 0;
  3942. }
  3943. static int binder_ioctl_write_read(struct file *filp,
  3944. unsigned int cmd, unsigned long arg,
  3945. struct binder_thread *thread)
  3946. {
  3947. int ret = 0;
  3948. struct binder_proc *proc = filp->private_data;
  3949. unsigned int size = _IOC_SIZE(cmd);
  3950. void __user *ubuf = (void __user *)arg;
  3951. struct binder_write_read bwr;
  3952. if (size != sizeof(struct binder_write_read)) {
  3953. ret = -EINVAL;
  3954. goto out;
  3955. }
  3956. if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
  3957. ret = -EFAULT;
  3958. goto out;
  3959. }
  3960. binder_debug(BINDER_DEBUG_READ_WRITE,
  3961. "%d:%d write %lld at %016llx, read %lld at %016llx\n",
  3962. proc->pid, thread->pid,
  3963. (u64)bwr.write_size, (u64)bwr.write_buffer,
  3964. (u64)bwr.read_size, (u64)bwr.read_buffer);
  3965. if (bwr.write_size > 0) {
  3966. ret = binder_thread_write(proc, thread,
  3967. bwr.write_buffer,
  3968. bwr.write_size,
  3969. &bwr.write_consumed);
  3970. trace_binder_write_done(ret);
  3971. if (ret < 0) {
  3972. bwr.read_consumed = 0;
  3973. if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
  3974. ret = -EFAULT;
  3975. goto out;
  3976. }
  3977. }
  3978. if (bwr.read_size > 0) {
  3979. ret = binder_thread_read(proc, thread, bwr.read_buffer,
  3980. bwr.read_size,
  3981. &bwr.read_consumed,
  3982. filp->f_flags & O_NONBLOCK);
  3983. trace_binder_read_done(ret);
  3984. binder_inner_proc_lock(proc);
  3985. if (!binder_worklist_empty_ilocked(&proc->todo))
  3986. binder_wakeup_proc_ilocked(proc);
  3987. binder_inner_proc_unlock(proc);
  3988. if (ret < 0) {
  3989. if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
  3990. ret = -EFAULT;
  3991. goto out;
  3992. }
  3993. }
  3994. binder_debug(BINDER_DEBUG_READ_WRITE,
  3995. "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
  3996. proc->pid, thread->pid,
  3997. (u64)bwr.write_consumed, (u64)bwr.write_size,
  3998. (u64)bwr.read_consumed, (u64)bwr.read_size);
  3999. if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
  4000. ret = -EFAULT;
  4001. goto out;
  4002. }
  4003. out:
  4004. return ret;
  4005. }
  4006. static int binder_ioctl_set_ctx_mgr(struct file *filp)
  4007. {
  4008. int ret = 0;
  4009. struct binder_proc *proc = filp->private_data;
  4010. struct binder_context *context = proc->context;
  4011. struct binder_node *new_node;
  4012. kuid_t curr_euid = current_euid();
  4013. mutex_lock(&context->context_mgr_node_lock);
  4014. if (context->binder_context_mgr_node) {
  4015. pr_err("BINDER_SET_CONTEXT_MGR already set\n");
  4016. ret = -EBUSY;
  4017. goto out;
  4018. }
  4019. ret = security_binder_set_context_mgr(proc->tsk);
  4020. if (ret < 0)
  4021. goto out;
  4022. if (uid_valid(context->binder_context_mgr_uid)) {
  4023. if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
  4024. pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
  4025. from_kuid(&init_user_ns, curr_euid),
  4026. from_kuid(&init_user_ns,
  4027. context->binder_context_mgr_uid));
  4028. ret = -EPERM;
  4029. goto out;
  4030. }
  4031. } else {
  4032. context->binder_context_mgr_uid = curr_euid;
  4033. }
  4034. new_node = binder_new_node(proc, NULL);
  4035. if (!new_node) {
  4036. ret = -ENOMEM;
  4037. goto out;
  4038. }
  4039. binder_node_lock(new_node);
  4040. new_node->local_weak_refs++;
  4041. new_node->local_strong_refs++;
  4042. new_node->has_strong_ref = 1;
  4043. new_node->has_weak_ref = 1;
  4044. context->binder_context_mgr_node = new_node;
  4045. binder_node_unlock(new_node);
  4046. binder_put_node(new_node);
  4047. out:
  4048. mutex_unlock(&context->context_mgr_node_lock);
  4049. return ret;
  4050. }
  4051. static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
  4052. struct binder_node_debug_info *info)
  4053. {
  4054. struct rb_node *n;
  4055. binder_uintptr_t ptr = info->ptr;
  4056. memset(info, 0, sizeof(*info));
  4057. binder_inner_proc_lock(proc);
  4058. for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
  4059. struct binder_node *node = rb_entry(n, struct binder_node,
  4060. rb_node);
  4061. if (node->ptr > ptr) {
  4062. info->ptr = node->ptr;
  4063. info->cookie = node->cookie;
  4064. info->has_strong_ref = node->has_strong_ref;
  4065. info->has_weak_ref = node->has_weak_ref;
  4066. break;
  4067. }
  4068. }
  4069. binder_inner_proc_unlock(proc);
  4070. return 0;
  4071. }
  4072. static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  4073. {
  4074. int ret;
  4075. struct binder_proc *proc = filp->private_data;
  4076. struct binder_thread *thread;
  4077. unsigned int size = _IOC_SIZE(cmd);
  4078. void __user *ubuf = (void __user *)arg;
  4079. /*pr_info("binder_ioctl: %d:%d %x %lx\n",
  4080. proc->pid, current->pid, cmd, arg);*/
  4081. binder_selftest_alloc(&proc->alloc);
  4082. trace_binder_ioctl(cmd, arg);
  4083. ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
  4084. if (ret)
  4085. goto err_unlocked;
  4086. thread = binder_get_thread(proc);
  4087. if (thread == NULL) {
  4088. ret = -ENOMEM;
  4089. goto err;
  4090. }
  4091. switch (cmd) {
  4092. case BINDER_WRITE_READ:
  4093. ret = binder_ioctl_write_read(filp, cmd, arg, thread);
  4094. if (ret)
  4095. goto err;
  4096. break;
  4097. case BINDER_SET_MAX_THREADS: {
  4098. int max_threads;
  4099. if (copy_from_user(&max_threads, ubuf,
  4100. sizeof(max_threads))) {
  4101. ret = -EINVAL;
  4102. goto err;
  4103. }
  4104. binder_inner_proc_lock(proc);
  4105. proc->max_threads = max_threads;
  4106. binder_inner_proc_unlock(proc);
  4107. break;
  4108. }
  4109. case BINDER_SET_CONTEXT_MGR:
  4110. ret = binder_ioctl_set_ctx_mgr(filp);
  4111. if (ret)
  4112. goto err;
  4113. break;
  4114. case BINDER_THREAD_EXIT:
  4115. binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
  4116. proc->pid, thread->pid);
  4117. binder_thread_release(proc, thread);
  4118. thread = NULL;
  4119. break;
  4120. case BINDER_VERSION: {
  4121. struct binder_version __user *ver = ubuf;
  4122. if (size != sizeof(struct binder_version)) {
  4123. ret = -EINVAL;
  4124. goto err;
  4125. }
  4126. if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
  4127. &ver->protocol_version)) {
  4128. ret = -EINVAL;
  4129. goto err;
  4130. }
  4131. break;
  4132. }
  4133. case BINDER_GET_NODE_DEBUG_INFO: {
  4134. struct binder_node_debug_info info;
  4135. if (copy_from_user(&info, ubuf, sizeof(info))) {
  4136. ret = -EFAULT;
  4137. goto err;
  4138. }
  4139. ret = binder_ioctl_get_node_debug_info(proc, &info);
  4140. if (ret < 0)
  4141. goto err;
  4142. if (copy_to_user(ubuf, &info, sizeof(info))) {
  4143. ret = -EFAULT;
  4144. goto err;
  4145. }
  4146. break;
  4147. }
  4148. default:
  4149. ret = -EINVAL;
  4150. goto err;
  4151. }
  4152. ret = 0;
  4153. err:
  4154. if (thread)
  4155. thread->looper_need_return = false;
  4156. wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
  4157. if (ret && ret != -ERESTARTSYS)
  4158. pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
  4159. err_unlocked:
  4160. trace_binder_ioctl_done(ret);
  4161. return ret;
  4162. }
  4163. static void binder_vma_open(struct vm_area_struct *vma)
  4164. {
  4165. struct binder_proc *proc = vma->vm_private_data;
  4166. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  4167. "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
  4168. proc->pid, vma->vm_start, vma->vm_end,
  4169. (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
  4170. (unsigned long)pgprot_val(vma->vm_page_prot));
  4171. }
  4172. static void binder_vma_close(struct vm_area_struct *vma)
  4173. {
  4174. struct binder_proc *proc = vma->vm_private_data;
  4175. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  4176. "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
  4177. proc->pid, vma->vm_start, vma->vm_end,
  4178. (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
  4179. (unsigned long)pgprot_val(vma->vm_page_prot));
  4180. binder_alloc_vma_close(&proc->alloc);
  4181. binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
  4182. }
  4183. static int binder_vm_fault(struct vm_fault *vmf)
  4184. {
  4185. return VM_FAULT_SIGBUS;
  4186. }
  4187. static const struct vm_operations_struct binder_vm_ops = {
  4188. .open = binder_vma_open,
  4189. .close = binder_vma_close,
  4190. .fault = binder_vm_fault,
  4191. };
  4192. static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
  4193. {
  4194. int ret;
  4195. struct binder_proc *proc = filp->private_data;
  4196. const char *failure_string;
  4197. if (proc->tsk != current->group_leader)
  4198. return -EINVAL;
  4199. if ((vma->vm_end - vma->vm_start) > SZ_4M)
  4200. vma->vm_end = vma->vm_start + SZ_4M;
  4201. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  4202. "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
  4203. __func__, proc->pid, vma->vm_start, vma->vm_end,
  4204. (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
  4205. (unsigned long)pgprot_val(vma->vm_page_prot));
  4206. if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
  4207. ret = -EPERM;
  4208. failure_string = "bad vm_flags";
  4209. goto err_bad_arg;
  4210. }
  4211. vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
  4212. vma->vm_ops = &binder_vm_ops;
  4213. vma->vm_private_data = proc;
  4214. ret = binder_alloc_mmap_handler(&proc->alloc, vma);
  4215. if (ret)
  4216. return ret;
  4217. proc->files = get_files_struct(current);
  4218. return 0;
  4219. err_bad_arg:
  4220. pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
  4221. proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
  4222. return ret;
  4223. }
  4224. static int binder_open(struct inode *nodp, struct file *filp)
  4225. {
  4226. struct binder_proc *proc;
  4227. struct binder_device *binder_dev;
  4228. binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
  4229. current->group_leader->pid, current->pid);
  4230. proc = kzalloc(sizeof(*proc), GFP_KERNEL);
  4231. if (proc == NULL)
  4232. return -ENOMEM;
  4233. spin_lock_init(&proc->inner_lock);
  4234. spin_lock_init(&proc->outer_lock);
  4235. get_task_struct(current->group_leader);
  4236. proc->tsk = current->group_leader;
  4237. INIT_LIST_HEAD(&proc->todo);
  4238. proc->default_priority = task_nice(current);
  4239. binder_dev = container_of(filp->private_data, struct binder_device,
  4240. miscdev);
  4241. proc->context = &binder_dev->context;
  4242. binder_alloc_init(&proc->alloc);
  4243. binder_stats_created(BINDER_STAT_PROC);
  4244. proc->pid = current->group_leader->pid;
  4245. INIT_LIST_HEAD(&proc->delivered_death);
  4246. INIT_LIST_HEAD(&proc->waiting_threads);
  4247. filp->private_data = proc;
  4248. mutex_lock(&binder_procs_lock);
  4249. hlist_add_head(&proc->proc_node, &binder_procs);
  4250. mutex_unlock(&binder_procs_lock);
  4251. if (binder_debugfs_dir_entry_proc) {
  4252. char strbuf[11];
  4253. snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
  4254. /*
  4255. * proc debug entries are shared between contexts, so
  4256. * this will fail if the process tries to open the driver
  4257. * again with a different context. The priting code will
  4258. * anyway print all contexts that a given PID has, so this
  4259. * is not a problem.
  4260. */
  4261. proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
  4262. binder_debugfs_dir_entry_proc,
  4263. (void *)(unsigned long)proc->pid,
  4264. &binder_proc_fops);
  4265. }
  4266. return 0;
  4267. }
  4268. static int binder_flush(struct file *filp, fl_owner_t id)
  4269. {
  4270. struct binder_proc *proc = filp->private_data;
  4271. binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
  4272. return 0;
  4273. }
  4274. static void binder_deferred_flush(struct binder_proc *proc)
  4275. {
  4276. struct rb_node *n;
  4277. int wake_count = 0;
  4278. binder_inner_proc_lock(proc);
  4279. for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
  4280. struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
  4281. thread->looper_need_return = true;
  4282. if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
  4283. wake_up_interruptible(&thread->wait);
  4284. wake_count++;
  4285. }
  4286. }
  4287. binder_inner_proc_unlock(proc);
  4288. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  4289. "binder_flush: %d woke %d threads\n", proc->pid,
  4290. wake_count);
  4291. }
  4292. static int binder_release(struct inode *nodp, struct file *filp)
  4293. {
  4294. struct binder_proc *proc = filp->private_data;
  4295. debugfs_remove(proc->debugfs_entry);
  4296. binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
  4297. return 0;
  4298. }
  4299. static int binder_node_release(struct binder_node *node, int refs)
  4300. {
  4301. struct binder_ref *ref;
  4302. int death = 0;
  4303. struct binder_proc *proc = node->proc;
  4304. binder_release_work(proc, &node->async_todo);
  4305. binder_node_lock(node);
  4306. binder_inner_proc_lock(proc);
  4307. binder_dequeue_work_ilocked(&node->work);
  4308. /*
  4309. * The caller must have taken a temporary ref on the node,
  4310. */
  4311. BUG_ON(!node->tmp_refs);
  4312. if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
  4313. binder_inner_proc_unlock(proc);
  4314. binder_node_unlock(node);
  4315. binder_free_node(node);
  4316. return refs;
  4317. }
  4318. node->proc = NULL;
  4319. node->local_strong_refs = 0;
  4320. node->local_weak_refs = 0;
  4321. binder_inner_proc_unlock(proc);
  4322. spin_lock(&binder_dead_nodes_lock);
  4323. hlist_add_head(&node->dead_node, &binder_dead_nodes);
  4324. spin_unlock(&binder_dead_nodes_lock);
  4325. hlist_for_each_entry(ref, &node->refs, node_entry) {
  4326. refs++;
  4327. /*
  4328. * Need the node lock to synchronize
  4329. * with new notification requests and the
  4330. * inner lock to synchronize with queued
  4331. * death notifications.
  4332. */
  4333. binder_inner_proc_lock(ref->proc);
  4334. if (!ref->death) {
  4335. binder_inner_proc_unlock(ref->proc);
  4336. continue;
  4337. }
  4338. death++;
  4339. BUG_ON(!list_empty(&ref->death->work.entry));
  4340. ref->death->work.type = BINDER_WORK_DEAD_BINDER;
  4341. binder_enqueue_work_ilocked(&ref->death->work,
  4342. &ref->proc->todo);
  4343. binder_wakeup_proc_ilocked(ref->proc);
  4344. binder_inner_proc_unlock(ref->proc);
  4345. }
  4346. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  4347. "node %d now dead, refs %d, death %d\n",
  4348. node->debug_id, refs, death);
  4349. binder_node_unlock(node);
  4350. binder_put_node(node);
  4351. return refs;
  4352. }
  4353. static void binder_deferred_release(struct binder_proc *proc)
  4354. {
  4355. struct binder_context *context = proc->context;
  4356. struct rb_node *n;
  4357. int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
  4358. BUG_ON(proc->files);
  4359. mutex_lock(&binder_procs_lock);
  4360. hlist_del(&proc->proc_node);
  4361. mutex_unlock(&binder_procs_lock);
  4362. mutex_lock(&context->context_mgr_node_lock);
  4363. if (context->binder_context_mgr_node &&
  4364. context->binder_context_mgr_node->proc == proc) {
  4365. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  4366. "%s: %d context_mgr_node gone\n",
  4367. __func__, proc->pid);
  4368. context->binder_context_mgr_node = NULL;
  4369. }
  4370. mutex_unlock(&context->context_mgr_node_lock);
  4371. binder_inner_proc_lock(proc);
  4372. /*
  4373. * Make sure proc stays alive after we
  4374. * remove all the threads
  4375. */
  4376. proc->tmp_ref++;
  4377. proc->is_dead = true;
  4378. threads = 0;
  4379. active_transactions = 0;
  4380. while ((n = rb_first(&proc->threads))) {
  4381. struct binder_thread *thread;
  4382. thread = rb_entry(n, struct binder_thread, rb_node);
  4383. binder_inner_proc_unlock(proc);
  4384. threads++;
  4385. active_transactions += binder_thread_release(proc, thread);
  4386. binder_inner_proc_lock(proc);
  4387. }
  4388. nodes = 0;
  4389. incoming_refs = 0;
  4390. while ((n = rb_first(&proc->nodes))) {
  4391. struct binder_node *node;
  4392. node = rb_entry(n, struct binder_node, rb_node);
  4393. nodes++;
  4394. /*
  4395. * take a temporary ref on the node before
  4396. * calling binder_node_release() which will either
  4397. * kfree() the node or call binder_put_node()
  4398. */
  4399. binder_inc_node_tmpref_ilocked(node);
  4400. rb_erase(&node->rb_node, &proc->nodes);
  4401. binder_inner_proc_unlock(proc);
  4402. incoming_refs = binder_node_release(node, incoming_refs);
  4403. binder_inner_proc_lock(proc);
  4404. }
  4405. binder_inner_proc_unlock(proc);
  4406. outgoing_refs = 0;
  4407. binder_proc_lock(proc);
  4408. while ((n = rb_first(&proc->refs_by_desc))) {
  4409. struct binder_ref *ref;
  4410. ref = rb_entry(n, struct binder_ref, rb_node_desc);
  4411. outgoing_refs++;
  4412. binder_cleanup_ref_olocked(ref);
  4413. binder_proc_unlock(proc);
  4414. binder_free_ref(ref);
  4415. binder_proc_lock(proc);
  4416. }
  4417. binder_proc_unlock(proc);
  4418. binder_release_work(proc, &proc->todo);
  4419. binder_release_work(proc, &proc->delivered_death);
  4420. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  4421. "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
  4422. __func__, proc->pid, threads, nodes, incoming_refs,
  4423. outgoing_refs, active_transactions);
  4424. binder_proc_dec_tmpref(proc);
  4425. }
  4426. static void binder_deferred_func(struct work_struct *work)
  4427. {
  4428. struct binder_proc *proc;
  4429. struct files_struct *files;
  4430. int defer;
  4431. do {
  4432. mutex_lock(&binder_deferred_lock);
  4433. if (!hlist_empty(&binder_deferred_list)) {
  4434. proc = hlist_entry(binder_deferred_list.first,
  4435. struct binder_proc, deferred_work_node);
  4436. hlist_del_init(&proc->deferred_work_node);
  4437. defer = proc->deferred_work;
  4438. proc->deferred_work = 0;
  4439. } else {
  4440. proc = NULL;
  4441. defer = 0;
  4442. }
  4443. mutex_unlock(&binder_deferred_lock);
  4444. files = NULL;
  4445. if (defer & BINDER_DEFERRED_PUT_FILES) {
  4446. files = proc->files;
  4447. if (files)
  4448. proc->files = NULL;
  4449. }
  4450. if (defer & BINDER_DEFERRED_FLUSH)
  4451. binder_deferred_flush(proc);
  4452. if (defer & BINDER_DEFERRED_RELEASE)
  4453. binder_deferred_release(proc); /* frees proc */
  4454. if (files)
  4455. put_files_struct(files);
  4456. } while (proc);
  4457. }
  4458. static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
  4459. static void
  4460. binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
  4461. {
  4462. mutex_lock(&binder_deferred_lock);
  4463. proc->deferred_work |= defer;
  4464. if (hlist_unhashed(&proc->deferred_work_node)) {
  4465. hlist_add_head(&proc->deferred_work_node,
  4466. &binder_deferred_list);
  4467. schedule_work(&binder_deferred_work);
  4468. }
  4469. mutex_unlock(&binder_deferred_lock);
  4470. }
  4471. static void print_binder_transaction_ilocked(struct seq_file *m,
  4472. struct binder_proc *proc,
  4473. const char *prefix,
  4474. struct binder_transaction *t)
  4475. {
  4476. struct binder_proc *to_proc;
  4477. struct binder_buffer *buffer = t->buffer;
  4478. spin_lock(&t->lock);
  4479. to_proc = t->to_proc;
  4480. seq_printf(m,
  4481. "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
  4482. prefix, t->debug_id, t,
  4483. t->from ? t->from->proc->pid : 0,
  4484. t->from ? t->from->pid : 0,
  4485. to_proc ? to_proc->pid : 0,
  4486. t->to_thread ? t->to_thread->pid : 0,
  4487. t->code, t->flags, t->priority, t->need_reply);
  4488. spin_unlock(&t->lock);
  4489. if (proc != to_proc) {
  4490. /*
  4491. * Can only safely deref buffer if we are holding the
  4492. * correct proc inner lock for this node
  4493. */
  4494. seq_puts(m, "\n");
  4495. return;
  4496. }
  4497. if (buffer == NULL) {
  4498. seq_puts(m, " buffer free\n");
  4499. return;
  4500. }
  4501. if (buffer->target_node)
  4502. seq_printf(m, " node %d", buffer->target_node->debug_id);
  4503. seq_printf(m, " size %zd:%zd data %p\n",
  4504. buffer->data_size, buffer->offsets_size,
  4505. buffer->data);
  4506. }
  4507. static void print_binder_work_ilocked(struct seq_file *m,
  4508. struct binder_proc *proc,
  4509. const char *prefix,
  4510. const char *transaction_prefix,
  4511. struct binder_work *w)
  4512. {
  4513. struct binder_node *node;
  4514. struct binder_transaction *t;
  4515. switch (w->type) {
  4516. case BINDER_WORK_TRANSACTION:
  4517. t = container_of(w, struct binder_transaction, work);
  4518. print_binder_transaction_ilocked(
  4519. m, proc, transaction_prefix, t);
  4520. break;
  4521. case BINDER_WORK_RETURN_ERROR: {
  4522. struct binder_error *e = container_of(
  4523. w, struct binder_error, work);
  4524. seq_printf(m, "%stransaction error: %u\n",
  4525. prefix, e->cmd);
  4526. } break;
  4527. case BINDER_WORK_TRANSACTION_COMPLETE:
  4528. seq_printf(m, "%stransaction complete\n", prefix);
  4529. break;
  4530. case BINDER_WORK_NODE:
  4531. node = container_of(w, struct binder_node, work);
  4532. seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
  4533. prefix, node->debug_id,
  4534. (u64)node->ptr, (u64)node->cookie);
  4535. break;
  4536. case BINDER_WORK_DEAD_BINDER:
  4537. seq_printf(m, "%shas dead binder\n", prefix);
  4538. break;
  4539. case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
  4540. seq_printf(m, "%shas cleared dead binder\n", prefix);
  4541. break;
  4542. case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
  4543. seq_printf(m, "%shas cleared death notification\n", prefix);
  4544. break;
  4545. default:
  4546. seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
  4547. break;
  4548. }
  4549. }
  4550. static void print_binder_thread_ilocked(struct seq_file *m,
  4551. struct binder_thread *thread,
  4552. int print_always)
  4553. {
  4554. struct binder_transaction *t;
  4555. struct binder_work *w;
  4556. size_t start_pos = m->count;
  4557. size_t header_pos;
  4558. seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
  4559. thread->pid, thread->looper,
  4560. thread->looper_need_return,
  4561. atomic_read(&thread->tmp_ref));
  4562. header_pos = m->count;
  4563. t = thread->transaction_stack;
  4564. while (t) {
  4565. if (t->from == thread) {
  4566. print_binder_transaction_ilocked(m, thread->proc,
  4567. " outgoing transaction", t);
  4568. t = t->from_parent;
  4569. } else if (t->to_thread == thread) {
  4570. print_binder_transaction_ilocked(m, thread->proc,
  4571. " incoming transaction", t);
  4572. t = t->to_parent;
  4573. } else {
  4574. print_binder_transaction_ilocked(m, thread->proc,
  4575. " bad transaction", t);
  4576. t = NULL;
  4577. }
  4578. }
  4579. list_for_each_entry(w, &thread->todo, entry) {
  4580. print_binder_work_ilocked(m, thread->proc, " ",
  4581. " pending transaction", w);
  4582. }
  4583. if (!print_always && m->count == header_pos)
  4584. m->count = start_pos;
  4585. }
  4586. static void print_binder_node_nilocked(struct seq_file *m,
  4587. struct binder_node *node)
  4588. {
  4589. struct binder_ref *ref;
  4590. struct binder_work *w;
  4591. int count;
  4592. count = 0;
  4593. hlist_for_each_entry(ref, &node->refs, node_entry)
  4594. count++;
  4595. seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d tr %d",
  4596. node->debug_id, (u64)node->ptr, (u64)node->cookie,
  4597. node->has_strong_ref, node->has_weak_ref,
  4598. node->local_strong_refs, node->local_weak_refs,
  4599. node->internal_strong_refs, count, node->tmp_refs);
  4600. if (count) {
  4601. seq_puts(m, " proc");
  4602. hlist_for_each_entry(ref, &node->refs, node_entry)
  4603. seq_printf(m, " %d", ref->proc->pid);
  4604. }
  4605. seq_puts(m, "\n");
  4606. if (node->proc) {
  4607. list_for_each_entry(w, &node->async_todo, entry)
  4608. print_binder_work_ilocked(m, node->proc, " ",
  4609. " pending async transaction", w);
  4610. }
  4611. }
  4612. static void print_binder_ref_olocked(struct seq_file *m,
  4613. struct binder_ref *ref)
  4614. {
  4615. binder_node_lock(ref->node);
  4616. seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
  4617. ref->data.debug_id, ref->data.desc,
  4618. ref->node->proc ? "" : "dead ",
  4619. ref->node->debug_id, ref->data.strong,
  4620. ref->data.weak, ref->death);
  4621. binder_node_unlock(ref->node);
  4622. }
  4623. static void print_binder_proc(struct seq_file *m,
  4624. struct binder_proc *proc, int print_all)
  4625. {
  4626. struct binder_work *w;
  4627. struct rb_node *n;
  4628. size_t start_pos = m->count;
  4629. size_t header_pos;
  4630. struct binder_node *last_node = NULL;
  4631. seq_printf(m, "proc %d\n", proc->pid);
  4632. seq_printf(m, "context %s\n", proc->context->name);
  4633. header_pos = m->count;
  4634. binder_inner_proc_lock(proc);
  4635. for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
  4636. print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
  4637. rb_node), print_all);
  4638. for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
  4639. struct binder_node *node = rb_entry(n, struct binder_node,
  4640. rb_node);
  4641. /*
  4642. * take a temporary reference on the node so it
  4643. * survives and isn't removed from the tree
  4644. * while we print it.
  4645. */
  4646. binder_inc_node_tmpref_ilocked(node);
  4647. /* Need to drop inner lock to take node lock */
  4648. binder_inner_proc_unlock(proc);
  4649. if (last_node)
  4650. binder_put_node(last_node);
  4651. binder_node_inner_lock(node);
  4652. print_binder_node_nilocked(m, node);
  4653. binder_node_inner_unlock(node);
  4654. last_node = node;
  4655. binder_inner_proc_lock(proc);
  4656. }
  4657. binder_inner_proc_unlock(proc);
  4658. if (last_node)
  4659. binder_put_node(last_node);
  4660. if (print_all) {
  4661. binder_proc_lock(proc);
  4662. for (n = rb_first(&proc->refs_by_desc);
  4663. n != NULL;
  4664. n = rb_next(n))
  4665. print_binder_ref_olocked(m, rb_entry(n,
  4666. struct binder_ref,
  4667. rb_node_desc));
  4668. binder_proc_unlock(proc);
  4669. }
  4670. binder_alloc_print_allocated(m, &proc->alloc);
  4671. binder_inner_proc_lock(proc);
  4672. list_for_each_entry(w, &proc->todo, entry)
  4673. print_binder_work_ilocked(m, proc, " ",
  4674. " pending transaction", w);
  4675. list_for_each_entry(w, &proc->delivered_death, entry) {
  4676. seq_puts(m, " has delivered dead binder\n");
  4677. break;
  4678. }
  4679. binder_inner_proc_unlock(proc);
  4680. if (!print_all && m->count == header_pos)
  4681. m->count = start_pos;
  4682. }
  4683. static const char * const binder_return_strings[] = {
  4684. "BR_ERROR",
  4685. "BR_OK",
  4686. "BR_TRANSACTION",
  4687. "BR_REPLY",
  4688. "BR_ACQUIRE_RESULT",
  4689. "BR_DEAD_REPLY",
  4690. "BR_TRANSACTION_COMPLETE",
  4691. "BR_INCREFS",
  4692. "BR_ACQUIRE",
  4693. "BR_RELEASE",
  4694. "BR_DECREFS",
  4695. "BR_ATTEMPT_ACQUIRE",
  4696. "BR_NOOP",
  4697. "BR_SPAWN_LOOPER",
  4698. "BR_FINISHED",
  4699. "BR_DEAD_BINDER",
  4700. "BR_CLEAR_DEATH_NOTIFICATION_DONE",
  4701. "BR_FAILED_REPLY"
  4702. };
  4703. static const char * const binder_command_strings[] = {
  4704. "BC_TRANSACTION",
  4705. "BC_REPLY",
  4706. "BC_ACQUIRE_RESULT",
  4707. "BC_FREE_BUFFER",
  4708. "BC_INCREFS",
  4709. "BC_ACQUIRE",
  4710. "BC_RELEASE",
  4711. "BC_DECREFS",
  4712. "BC_INCREFS_DONE",
  4713. "BC_ACQUIRE_DONE",
  4714. "BC_ATTEMPT_ACQUIRE",
  4715. "BC_REGISTER_LOOPER",
  4716. "BC_ENTER_LOOPER",
  4717. "BC_EXIT_LOOPER",
  4718. "BC_REQUEST_DEATH_NOTIFICATION",
  4719. "BC_CLEAR_DEATH_NOTIFICATION",
  4720. "BC_DEAD_BINDER_DONE",
  4721. "BC_TRANSACTION_SG",
  4722. "BC_REPLY_SG",
  4723. };
  4724. static const char * const binder_objstat_strings[] = {
  4725. "proc",
  4726. "thread",
  4727. "node",
  4728. "ref",
  4729. "death",
  4730. "transaction",
  4731. "transaction_complete"
  4732. };
  4733. static void print_binder_stats(struct seq_file *m, const char *prefix,
  4734. struct binder_stats *stats)
  4735. {
  4736. int i;
  4737. BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
  4738. ARRAY_SIZE(binder_command_strings));
  4739. for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
  4740. int temp = atomic_read(&stats->bc[i]);
  4741. if (temp)
  4742. seq_printf(m, "%s%s: %d\n", prefix,
  4743. binder_command_strings[i], temp);
  4744. }
  4745. BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
  4746. ARRAY_SIZE(binder_return_strings));
  4747. for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
  4748. int temp = atomic_read(&stats->br[i]);
  4749. if (temp)
  4750. seq_printf(m, "%s%s: %d\n", prefix,
  4751. binder_return_strings[i], temp);
  4752. }
  4753. BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
  4754. ARRAY_SIZE(binder_objstat_strings));
  4755. BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
  4756. ARRAY_SIZE(stats->obj_deleted));
  4757. for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
  4758. int created = atomic_read(&stats->obj_created[i]);
  4759. int deleted = atomic_read(&stats->obj_deleted[i]);
  4760. if (created || deleted)
  4761. seq_printf(m, "%s%s: active %d total %d\n",
  4762. prefix,
  4763. binder_objstat_strings[i],
  4764. created - deleted,
  4765. created);
  4766. }
  4767. }
  4768. static void print_binder_proc_stats(struct seq_file *m,
  4769. struct binder_proc *proc)
  4770. {
  4771. struct binder_work *w;
  4772. struct binder_thread *thread;
  4773. struct rb_node *n;
  4774. int count, strong, weak, ready_threads;
  4775. size_t free_async_space =
  4776. binder_alloc_get_free_async_space(&proc->alloc);
  4777. seq_printf(m, "proc %d\n", proc->pid);
  4778. seq_printf(m, "context %s\n", proc->context->name);
  4779. count = 0;
  4780. ready_threads = 0;
  4781. binder_inner_proc_lock(proc);
  4782. for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
  4783. count++;
  4784. list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
  4785. ready_threads++;
  4786. seq_printf(m, " threads: %d\n", count);
  4787. seq_printf(m, " requested threads: %d+%d/%d\n"
  4788. " ready threads %d\n"
  4789. " free async space %zd\n", proc->requested_threads,
  4790. proc->requested_threads_started, proc->max_threads,
  4791. ready_threads,
  4792. free_async_space);
  4793. count = 0;
  4794. for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
  4795. count++;
  4796. binder_inner_proc_unlock(proc);
  4797. seq_printf(m, " nodes: %d\n", count);
  4798. count = 0;
  4799. strong = 0;
  4800. weak = 0;
  4801. binder_proc_lock(proc);
  4802. for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
  4803. struct binder_ref *ref = rb_entry(n, struct binder_ref,
  4804. rb_node_desc);
  4805. count++;
  4806. strong += ref->data.strong;
  4807. weak += ref->data.weak;
  4808. }
  4809. binder_proc_unlock(proc);
  4810. seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
  4811. count = binder_alloc_get_allocated_count(&proc->alloc);
  4812. seq_printf(m, " buffers: %d\n", count);
  4813. binder_alloc_print_pages(m, &proc->alloc);
  4814. count = 0;
  4815. binder_inner_proc_lock(proc);
  4816. list_for_each_entry(w, &proc->todo, entry) {
  4817. if (w->type == BINDER_WORK_TRANSACTION)
  4818. count++;
  4819. }
  4820. binder_inner_proc_unlock(proc);
  4821. seq_printf(m, " pending transactions: %d\n", count);
  4822. print_binder_stats(m, " ", &proc->stats);
  4823. }
  4824. static int binder_state_show(struct seq_file *m, void *unused)
  4825. {
  4826. struct binder_proc *proc;
  4827. struct binder_node *node;
  4828. struct binder_node *last_node = NULL;
  4829. seq_puts(m, "binder state:\n");
  4830. spin_lock(&binder_dead_nodes_lock);
  4831. if (!hlist_empty(&binder_dead_nodes))
  4832. seq_puts(m, "dead nodes:\n");
  4833. hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
  4834. /*
  4835. * take a temporary reference on the node so it
  4836. * survives and isn't removed from the list
  4837. * while we print it.
  4838. */
  4839. node->tmp_refs++;
  4840. spin_unlock(&binder_dead_nodes_lock);
  4841. if (last_node)
  4842. binder_put_node(last_node);
  4843. binder_node_lock(node);
  4844. print_binder_node_nilocked(m, node);
  4845. binder_node_unlock(node);
  4846. last_node = node;
  4847. spin_lock(&binder_dead_nodes_lock);
  4848. }
  4849. spin_unlock(&binder_dead_nodes_lock);
  4850. if (last_node)
  4851. binder_put_node(last_node);
  4852. mutex_lock(&binder_procs_lock);
  4853. hlist_for_each_entry(proc, &binder_procs, proc_node)
  4854. print_binder_proc(m, proc, 1);
  4855. mutex_unlock(&binder_procs_lock);
  4856. return 0;
  4857. }
  4858. static int binder_stats_show(struct seq_file *m, void *unused)
  4859. {
  4860. struct binder_proc *proc;
  4861. seq_puts(m, "binder stats:\n");
  4862. print_binder_stats(m, "", &binder_stats);
  4863. mutex_lock(&binder_procs_lock);
  4864. hlist_for_each_entry(proc, &binder_procs, proc_node)
  4865. print_binder_proc_stats(m, proc);
  4866. mutex_unlock(&binder_procs_lock);
  4867. return 0;
  4868. }
  4869. static int binder_transactions_show(struct seq_file *m, void *unused)
  4870. {
  4871. struct binder_proc *proc;
  4872. seq_puts(m, "binder transactions:\n");
  4873. mutex_lock(&binder_procs_lock);
  4874. hlist_for_each_entry(proc, &binder_procs, proc_node)
  4875. print_binder_proc(m, proc, 0);
  4876. mutex_unlock(&binder_procs_lock);
  4877. return 0;
  4878. }
  4879. static int binder_proc_show(struct seq_file *m, void *unused)
  4880. {
  4881. struct binder_proc *itr;
  4882. int pid = (unsigned long)m->private;
  4883. mutex_lock(&binder_procs_lock);
  4884. hlist_for_each_entry(itr, &binder_procs, proc_node) {
  4885. if (itr->pid == pid) {
  4886. seq_puts(m, "binder proc state:\n");
  4887. print_binder_proc(m, itr, 1);
  4888. }
  4889. }
  4890. mutex_unlock(&binder_procs_lock);
  4891. return 0;
  4892. }
  4893. static void print_binder_transaction_log_entry(struct seq_file *m,
  4894. struct binder_transaction_log_entry *e)
  4895. {
  4896. int debug_id = READ_ONCE(e->debug_id_done);
  4897. /*
  4898. * read barrier to guarantee debug_id_done read before
  4899. * we print the log values
  4900. */
  4901. smp_rmb();
  4902. seq_printf(m,
  4903. "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
  4904. e->debug_id, (e->call_type == 2) ? "reply" :
  4905. ((e->call_type == 1) ? "async" : "call "), e->from_proc,
  4906. e->from_thread, e->to_proc, e->to_thread, e->context_name,
  4907. e->to_node, e->target_handle, e->data_size, e->offsets_size,
  4908. e->return_error, e->return_error_param,
  4909. e->return_error_line);
  4910. /*
  4911. * read-barrier to guarantee read of debug_id_done after
  4912. * done printing the fields of the entry
  4913. */
  4914. smp_rmb();
  4915. seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
  4916. "\n" : " (incomplete)\n");
  4917. }
  4918. static int binder_transaction_log_show(struct seq_file *m, void *unused)
  4919. {
  4920. struct binder_transaction_log *log = m->private;
  4921. unsigned int log_cur = atomic_read(&log->cur);
  4922. unsigned int count;
  4923. unsigned int cur;
  4924. int i;
  4925. count = log_cur + 1;
  4926. cur = count < ARRAY_SIZE(log->entry) && !log->full ?
  4927. 0 : count % ARRAY_SIZE(log->entry);
  4928. if (count > ARRAY_SIZE(log->entry) || log->full)
  4929. count = ARRAY_SIZE(log->entry);
  4930. for (i = 0; i < count; i++) {
  4931. unsigned int index = cur++ % ARRAY_SIZE(log->entry);
  4932. print_binder_transaction_log_entry(m, &log->entry[index]);
  4933. }
  4934. return 0;
  4935. }
  4936. static const struct file_operations binder_fops = {
  4937. .owner = THIS_MODULE,
  4938. .poll = binder_poll,
  4939. .unlocked_ioctl = binder_ioctl,
  4940. .compat_ioctl = binder_ioctl,
  4941. .mmap = binder_mmap,
  4942. .open = binder_open,
  4943. .flush = binder_flush,
  4944. .release = binder_release,
  4945. };
  4946. BINDER_DEBUG_ENTRY(state);
  4947. BINDER_DEBUG_ENTRY(stats);
  4948. BINDER_DEBUG_ENTRY(transactions);
  4949. BINDER_DEBUG_ENTRY(transaction_log);
  4950. static int __init init_binder_device(const char *name)
  4951. {
  4952. int ret;
  4953. struct binder_device *binder_device;
  4954. binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
  4955. if (!binder_device)
  4956. return -ENOMEM;
  4957. binder_device->miscdev.fops = &binder_fops;
  4958. binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
  4959. binder_device->miscdev.name = name;
  4960. binder_device->context.binder_context_mgr_uid = INVALID_UID;
  4961. binder_device->context.name = name;
  4962. mutex_init(&binder_device->context.context_mgr_node_lock);
  4963. ret = misc_register(&binder_device->miscdev);
  4964. if (ret < 0) {
  4965. kfree(binder_device);
  4966. return ret;
  4967. }
  4968. hlist_add_head(&binder_device->hlist, &binder_devices);
  4969. return ret;
  4970. }
  4971. static int __init binder_init(void)
  4972. {
  4973. int ret;
  4974. char *device_name, *device_names, *device_tmp;
  4975. struct binder_device *device;
  4976. struct hlist_node *tmp;
  4977. binder_alloc_shrinker_init();
  4978. atomic_set(&binder_transaction_log.cur, ~0U);
  4979. atomic_set(&binder_transaction_log_failed.cur, ~0U);
  4980. binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
  4981. if (binder_debugfs_dir_entry_root)
  4982. binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
  4983. binder_debugfs_dir_entry_root);
  4984. if (binder_debugfs_dir_entry_root) {
  4985. debugfs_create_file("state",
  4986. S_IRUGO,
  4987. binder_debugfs_dir_entry_root,
  4988. NULL,
  4989. &binder_state_fops);
  4990. debugfs_create_file("stats",
  4991. S_IRUGO,
  4992. binder_debugfs_dir_entry_root,
  4993. NULL,
  4994. &binder_stats_fops);
  4995. debugfs_create_file("transactions",
  4996. S_IRUGO,
  4997. binder_debugfs_dir_entry_root,
  4998. NULL,
  4999. &binder_transactions_fops);
  5000. debugfs_create_file("transaction_log",
  5001. S_IRUGO,
  5002. binder_debugfs_dir_entry_root,
  5003. &binder_transaction_log,
  5004. &binder_transaction_log_fops);
  5005. debugfs_create_file("failed_transaction_log",
  5006. S_IRUGO,
  5007. binder_debugfs_dir_entry_root,
  5008. &binder_transaction_log_failed,
  5009. &binder_transaction_log_fops);
  5010. }
  5011. /*
  5012. * Copy the module_parameter string, because we don't want to
  5013. * tokenize it in-place.
  5014. */
  5015. device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
  5016. if (!device_names) {
  5017. ret = -ENOMEM;
  5018. goto err_alloc_device_names_failed;
  5019. }
  5020. strcpy(device_names, binder_devices_param);
  5021. device_tmp = device_names;
  5022. while ((device_name = strsep(&device_tmp, ","))) {
  5023. ret = init_binder_device(device_name);
  5024. if (ret)
  5025. goto err_init_binder_device_failed;
  5026. }
  5027. return ret;
  5028. err_init_binder_device_failed:
  5029. hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
  5030. misc_deregister(&device->miscdev);
  5031. hlist_del(&device->hlist);
  5032. kfree(device);
  5033. }
  5034. kfree(device_names);
  5035. err_alloc_device_names_failed:
  5036. debugfs_remove_recursive(binder_debugfs_dir_entry_root);
  5037. return ret;
  5038. }
  5039. device_initcall(binder_init);
  5040. #define CREATE_TRACE_POINTS
  5041. #include "binder_trace.h"
  5042. MODULE_LICENSE("GPL v2");