binder.c 164 KB

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