binder.c 163 KB

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