binder.c 177 KB

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