binder.c 163 KB

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