binder.c 163 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706
  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. if (t->buffer)
  1792. t->buffer->transaction = NULL;
  1793. kfree(t);
  1794. binder_stats_deleted(BINDER_STAT_TRANSACTION);
  1795. }
  1796. static void binder_send_failed_reply(struct binder_transaction *t,
  1797. uint32_t error_code)
  1798. {
  1799. struct binder_thread *target_thread;
  1800. struct binder_transaction *next;
  1801. BUG_ON(t->flags & TF_ONE_WAY);
  1802. while (1) {
  1803. target_thread = binder_get_txn_from_and_acq_inner(t);
  1804. if (target_thread) {
  1805. binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
  1806. "send failed reply for transaction %d to %d:%d\n",
  1807. t->debug_id,
  1808. target_thread->proc->pid,
  1809. target_thread->pid);
  1810. binder_pop_transaction_ilocked(target_thread, t);
  1811. if (target_thread->reply_error.cmd == BR_OK) {
  1812. target_thread->reply_error.cmd = error_code;
  1813. binder_enqueue_thread_work_ilocked(
  1814. target_thread,
  1815. &target_thread->reply_error.work);
  1816. wake_up_interruptible(&target_thread->wait);
  1817. } else {
  1818. /*
  1819. * Cannot get here for normal operation, but
  1820. * we can if multiple synchronous transactions
  1821. * are sent without blocking for responses.
  1822. * Just ignore the 2nd error in this case.
  1823. */
  1824. pr_warn("Unexpected reply error: %u\n",
  1825. target_thread->reply_error.cmd);
  1826. }
  1827. binder_inner_proc_unlock(target_thread->proc);
  1828. binder_thread_dec_tmpref(target_thread);
  1829. binder_free_transaction(t);
  1830. return;
  1831. }
  1832. next = t->from_parent;
  1833. binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
  1834. "send failed reply for transaction %d, target dead\n",
  1835. t->debug_id);
  1836. binder_free_transaction(t);
  1837. if (next == NULL) {
  1838. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  1839. "reply failed, no target thread at root\n");
  1840. return;
  1841. }
  1842. t = next;
  1843. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  1844. "reply failed, no target thread -- retry %d\n",
  1845. t->debug_id);
  1846. }
  1847. }
  1848. /**
  1849. * binder_cleanup_transaction() - cleans up undelivered transaction
  1850. * @t: transaction that needs to be cleaned up
  1851. * @reason: reason the transaction wasn't delivered
  1852. * @error_code: error to return to caller (if synchronous call)
  1853. */
  1854. static void binder_cleanup_transaction(struct binder_transaction *t,
  1855. const char *reason,
  1856. uint32_t error_code)
  1857. {
  1858. if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) {
  1859. binder_send_failed_reply(t, error_code);
  1860. } else {
  1861. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  1862. "undelivered transaction %d, %s\n",
  1863. t->debug_id, reason);
  1864. binder_free_transaction(t);
  1865. }
  1866. }
  1867. /**
  1868. * binder_validate_object() - checks for a valid metadata object in a buffer.
  1869. * @buffer: binder_buffer that we're parsing.
  1870. * @offset: offset in the buffer at which to validate an object.
  1871. *
  1872. * Return: If there's a valid metadata object at @offset in @buffer, the
  1873. * size of that object. Otherwise, it returns zero.
  1874. */
  1875. static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
  1876. {
  1877. /* Check if we can read a header first */
  1878. struct binder_object_header *hdr;
  1879. size_t object_size = 0;
  1880. if (buffer->data_size < sizeof(*hdr) ||
  1881. offset > buffer->data_size - sizeof(*hdr) ||
  1882. !IS_ALIGNED(offset, sizeof(u32)))
  1883. return 0;
  1884. /* Ok, now see if we can read a complete object. */
  1885. hdr = (struct binder_object_header *)(buffer->data + offset);
  1886. switch (hdr->type) {
  1887. case BINDER_TYPE_BINDER:
  1888. case BINDER_TYPE_WEAK_BINDER:
  1889. case BINDER_TYPE_HANDLE:
  1890. case BINDER_TYPE_WEAK_HANDLE:
  1891. object_size = sizeof(struct flat_binder_object);
  1892. break;
  1893. case BINDER_TYPE_FD:
  1894. object_size = sizeof(struct binder_fd_object);
  1895. break;
  1896. case BINDER_TYPE_PTR:
  1897. object_size = sizeof(struct binder_buffer_object);
  1898. break;
  1899. case BINDER_TYPE_FDA:
  1900. object_size = sizeof(struct binder_fd_array_object);
  1901. break;
  1902. default:
  1903. return 0;
  1904. }
  1905. if (offset <= buffer->data_size - object_size &&
  1906. buffer->data_size >= object_size)
  1907. return object_size;
  1908. else
  1909. return 0;
  1910. }
  1911. /**
  1912. * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
  1913. * @b: binder_buffer containing the object
  1914. * @index: index in offset array at which the binder_buffer_object is
  1915. * located
  1916. * @start: points to the start of the offset array
  1917. * @num_valid: the number of valid offsets in the offset array
  1918. *
  1919. * Return: If @index is within the valid range of the offset array
  1920. * described by @start and @num_valid, and if there's a valid
  1921. * binder_buffer_object at the offset found in index @index
  1922. * of the offset array, that object is returned. Otherwise,
  1923. * %NULL is returned.
  1924. * Note that the offset found in index @index itself is not
  1925. * verified; this function assumes that @num_valid elements
  1926. * from @start were previously verified to have valid offsets.
  1927. */
  1928. static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
  1929. binder_size_t index,
  1930. binder_size_t *start,
  1931. binder_size_t num_valid)
  1932. {
  1933. struct binder_buffer_object *buffer_obj;
  1934. binder_size_t *offp;
  1935. if (index >= num_valid)
  1936. return NULL;
  1937. offp = start + index;
  1938. buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
  1939. if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
  1940. return NULL;
  1941. return buffer_obj;
  1942. }
  1943. /**
  1944. * binder_validate_fixup() - validates pointer/fd fixups happen in order.
  1945. * @b: transaction buffer
  1946. * @objects_start start of objects buffer
  1947. * @buffer: binder_buffer_object in which to fix up
  1948. * @offset: start offset in @buffer to fix up
  1949. * @last_obj: last binder_buffer_object that we fixed up in
  1950. * @last_min_offset: minimum fixup offset in @last_obj
  1951. *
  1952. * Return: %true if a fixup in buffer @buffer at offset @offset is
  1953. * allowed.
  1954. *
  1955. * For safety reasons, we only allow fixups inside a buffer to happen
  1956. * at increasing offsets; additionally, we only allow fixup on the last
  1957. * buffer object that was verified, or one of its parents.
  1958. *
  1959. * Example of what is allowed:
  1960. *
  1961. * A
  1962. * B (parent = A, offset = 0)
  1963. * C (parent = A, offset = 16)
  1964. * D (parent = C, offset = 0)
  1965. * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
  1966. *
  1967. * Examples of what is not allowed:
  1968. *
  1969. * Decreasing offsets within the same parent:
  1970. * A
  1971. * C (parent = A, offset = 16)
  1972. * B (parent = A, offset = 0) // decreasing offset within A
  1973. *
  1974. * Referring to a parent that wasn't the last object or any of its parents:
  1975. * A
  1976. * B (parent = A, offset = 0)
  1977. * C (parent = A, offset = 0)
  1978. * C (parent = A, offset = 16)
  1979. * D (parent = B, offset = 0) // B is not A or any of A's parents
  1980. */
  1981. static bool binder_validate_fixup(struct binder_buffer *b,
  1982. binder_size_t *objects_start,
  1983. struct binder_buffer_object *buffer,
  1984. binder_size_t fixup_offset,
  1985. struct binder_buffer_object *last_obj,
  1986. binder_size_t last_min_offset)
  1987. {
  1988. if (!last_obj) {
  1989. /* Nothing to fix up in */
  1990. return false;
  1991. }
  1992. while (last_obj != buffer) {
  1993. /*
  1994. * Safe to retrieve the parent of last_obj, since it
  1995. * was already previously verified by the driver.
  1996. */
  1997. if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
  1998. return false;
  1999. last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
  2000. last_obj = (struct binder_buffer_object *)
  2001. (b->data + *(objects_start + last_obj->parent));
  2002. }
  2003. return (fixup_offset >= last_min_offset);
  2004. }
  2005. static void binder_transaction_buffer_release(struct binder_proc *proc,
  2006. struct binder_buffer *buffer,
  2007. binder_size_t *failed_at)
  2008. {
  2009. binder_size_t *offp, *off_start, *off_end;
  2010. int debug_id = buffer->debug_id;
  2011. binder_debug(BINDER_DEBUG_TRANSACTION,
  2012. "%d buffer release %d, size %zd-%zd, failed at %pK\n",
  2013. proc->pid, buffer->debug_id,
  2014. buffer->data_size, buffer->offsets_size, failed_at);
  2015. if (buffer->target_node)
  2016. binder_dec_node(buffer->target_node, 1, 0);
  2017. off_start = (binder_size_t *)(buffer->data +
  2018. ALIGN(buffer->data_size, sizeof(void *)));
  2019. if (failed_at)
  2020. off_end = failed_at;
  2021. else
  2022. off_end = (void *)off_start + buffer->offsets_size;
  2023. for (offp = off_start; offp < off_end; offp++) {
  2024. struct binder_object_header *hdr;
  2025. size_t object_size = binder_validate_object(buffer, *offp);
  2026. if (object_size == 0) {
  2027. pr_err("transaction release %d bad object at offset %lld, size %zd\n",
  2028. debug_id, (u64)*offp, buffer->data_size);
  2029. continue;
  2030. }
  2031. hdr = (struct binder_object_header *)(buffer->data + *offp);
  2032. switch (hdr->type) {
  2033. case BINDER_TYPE_BINDER:
  2034. case BINDER_TYPE_WEAK_BINDER: {
  2035. struct flat_binder_object *fp;
  2036. struct binder_node *node;
  2037. fp = to_flat_binder_object(hdr);
  2038. node = binder_get_node(proc, fp->binder);
  2039. if (node == NULL) {
  2040. pr_err("transaction release %d bad node %016llx\n",
  2041. debug_id, (u64)fp->binder);
  2042. break;
  2043. }
  2044. binder_debug(BINDER_DEBUG_TRANSACTION,
  2045. " node %d u%016llx\n",
  2046. node->debug_id, (u64)node->ptr);
  2047. binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
  2048. 0);
  2049. binder_put_node(node);
  2050. } break;
  2051. case BINDER_TYPE_HANDLE:
  2052. case BINDER_TYPE_WEAK_HANDLE: {
  2053. struct flat_binder_object *fp;
  2054. struct binder_ref_data rdata;
  2055. int ret;
  2056. fp = to_flat_binder_object(hdr);
  2057. ret = binder_dec_ref_for_handle(proc, fp->handle,
  2058. hdr->type == BINDER_TYPE_HANDLE, &rdata);
  2059. if (ret) {
  2060. pr_err("transaction release %d bad handle %d, ret = %d\n",
  2061. debug_id, fp->handle, ret);
  2062. break;
  2063. }
  2064. binder_debug(BINDER_DEBUG_TRANSACTION,
  2065. " ref %d desc %d\n",
  2066. rdata.debug_id, rdata.desc);
  2067. } break;
  2068. case BINDER_TYPE_FD: {
  2069. struct binder_fd_object *fp = to_binder_fd_object(hdr);
  2070. binder_debug(BINDER_DEBUG_TRANSACTION,
  2071. " fd %d\n", fp->fd);
  2072. if (failed_at)
  2073. task_close_fd(proc, fp->fd);
  2074. } break;
  2075. case BINDER_TYPE_PTR:
  2076. /*
  2077. * Nothing to do here, this will get cleaned up when the
  2078. * transaction buffer gets freed
  2079. */
  2080. break;
  2081. case BINDER_TYPE_FDA: {
  2082. struct binder_fd_array_object *fda;
  2083. struct binder_buffer_object *parent;
  2084. uintptr_t parent_buffer;
  2085. u32 *fd_array;
  2086. size_t fd_index;
  2087. binder_size_t fd_buf_size;
  2088. fda = to_binder_fd_array_object(hdr);
  2089. parent = binder_validate_ptr(buffer, fda->parent,
  2090. off_start,
  2091. offp - off_start);
  2092. if (!parent) {
  2093. pr_err("transaction release %d bad parent offset\n",
  2094. debug_id);
  2095. continue;
  2096. }
  2097. /*
  2098. * Since the parent was already fixed up, convert it
  2099. * back to kernel address space to access it
  2100. */
  2101. parent_buffer = parent->buffer -
  2102. binder_alloc_get_user_buffer_offset(
  2103. &proc->alloc);
  2104. fd_buf_size = sizeof(u32) * fda->num_fds;
  2105. if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
  2106. pr_err("transaction release %d invalid number of fds (%lld)\n",
  2107. debug_id, (u64)fda->num_fds);
  2108. continue;
  2109. }
  2110. if (fd_buf_size > parent->length ||
  2111. fda->parent_offset > parent->length - fd_buf_size) {
  2112. /* No space for all file descriptors here. */
  2113. pr_err("transaction release %d not enough space for %lld fds in buffer\n",
  2114. debug_id, (u64)fda->num_fds);
  2115. continue;
  2116. }
  2117. fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
  2118. for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
  2119. task_close_fd(proc, fd_array[fd_index]);
  2120. } break;
  2121. default:
  2122. pr_err("transaction release %d bad object type %x\n",
  2123. debug_id, hdr->type);
  2124. break;
  2125. }
  2126. }
  2127. }
  2128. static int binder_translate_binder(struct flat_binder_object *fp,
  2129. struct binder_transaction *t,
  2130. struct binder_thread *thread)
  2131. {
  2132. struct binder_node *node;
  2133. struct binder_proc *proc = thread->proc;
  2134. struct binder_proc *target_proc = t->to_proc;
  2135. struct binder_ref_data rdata;
  2136. int ret = 0;
  2137. node = binder_get_node(proc, fp->binder);
  2138. if (!node) {
  2139. node = binder_new_node(proc, fp);
  2140. if (!node)
  2141. return -ENOMEM;
  2142. }
  2143. if (fp->cookie != node->cookie) {
  2144. binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
  2145. proc->pid, thread->pid, (u64)fp->binder,
  2146. node->debug_id, (u64)fp->cookie,
  2147. (u64)node->cookie);
  2148. ret = -EINVAL;
  2149. goto done;
  2150. }
  2151. if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
  2152. ret = -EPERM;
  2153. goto done;
  2154. }
  2155. ret = binder_inc_ref_for_node(target_proc, node,
  2156. fp->hdr.type == BINDER_TYPE_BINDER,
  2157. &thread->todo, &rdata);
  2158. if (ret)
  2159. goto done;
  2160. if (fp->hdr.type == BINDER_TYPE_BINDER)
  2161. fp->hdr.type = BINDER_TYPE_HANDLE;
  2162. else
  2163. fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
  2164. fp->binder = 0;
  2165. fp->handle = rdata.desc;
  2166. fp->cookie = 0;
  2167. trace_binder_transaction_node_to_ref(t, node, &rdata);
  2168. binder_debug(BINDER_DEBUG_TRANSACTION,
  2169. " node %d u%016llx -> ref %d desc %d\n",
  2170. node->debug_id, (u64)node->ptr,
  2171. rdata.debug_id, rdata.desc);
  2172. done:
  2173. binder_put_node(node);
  2174. return ret;
  2175. }
  2176. static int binder_translate_handle(struct flat_binder_object *fp,
  2177. struct binder_transaction *t,
  2178. struct binder_thread *thread)
  2179. {
  2180. struct binder_proc *proc = thread->proc;
  2181. struct binder_proc *target_proc = t->to_proc;
  2182. struct binder_node *node;
  2183. struct binder_ref_data src_rdata;
  2184. int ret = 0;
  2185. node = binder_get_node_from_ref(proc, fp->handle,
  2186. fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
  2187. if (!node) {
  2188. binder_user_error("%d:%d got transaction with invalid handle, %d\n",
  2189. proc->pid, thread->pid, fp->handle);
  2190. return -EINVAL;
  2191. }
  2192. if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
  2193. ret = -EPERM;
  2194. goto done;
  2195. }
  2196. binder_node_lock(node);
  2197. if (node->proc == target_proc) {
  2198. if (fp->hdr.type == BINDER_TYPE_HANDLE)
  2199. fp->hdr.type = BINDER_TYPE_BINDER;
  2200. else
  2201. fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
  2202. fp->binder = node->ptr;
  2203. fp->cookie = node->cookie;
  2204. if (node->proc)
  2205. binder_inner_proc_lock(node->proc);
  2206. binder_inc_node_nilocked(node,
  2207. fp->hdr.type == BINDER_TYPE_BINDER,
  2208. 0, NULL);
  2209. if (node->proc)
  2210. binder_inner_proc_unlock(node->proc);
  2211. trace_binder_transaction_ref_to_node(t, node, &src_rdata);
  2212. binder_debug(BINDER_DEBUG_TRANSACTION,
  2213. " ref %d desc %d -> node %d u%016llx\n",
  2214. src_rdata.debug_id, src_rdata.desc, node->debug_id,
  2215. (u64)node->ptr);
  2216. binder_node_unlock(node);
  2217. } else {
  2218. struct binder_ref_data dest_rdata;
  2219. binder_node_unlock(node);
  2220. ret = binder_inc_ref_for_node(target_proc, node,
  2221. fp->hdr.type == BINDER_TYPE_HANDLE,
  2222. NULL, &dest_rdata);
  2223. if (ret)
  2224. goto done;
  2225. fp->binder = 0;
  2226. fp->handle = dest_rdata.desc;
  2227. fp->cookie = 0;
  2228. trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
  2229. &dest_rdata);
  2230. binder_debug(BINDER_DEBUG_TRANSACTION,
  2231. " ref %d desc %d -> ref %d desc %d (node %d)\n",
  2232. src_rdata.debug_id, src_rdata.desc,
  2233. dest_rdata.debug_id, dest_rdata.desc,
  2234. node->debug_id);
  2235. }
  2236. done:
  2237. binder_put_node(node);
  2238. return ret;
  2239. }
  2240. static int binder_translate_fd(int fd,
  2241. struct binder_transaction *t,
  2242. struct binder_thread *thread,
  2243. struct binder_transaction *in_reply_to)
  2244. {
  2245. struct binder_proc *proc = thread->proc;
  2246. struct binder_proc *target_proc = t->to_proc;
  2247. int target_fd;
  2248. struct file *file;
  2249. int ret;
  2250. bool target_allows_fd;
  2251. if (in_reply_to)
  2252. target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
  2253. else
  2254. target_allows_fd = t->buffer->target_node->accept_fds;
  2255. if (!target_allows_fd) {
  2256. binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
  2257. proc->pid, thread->pid,
  2258. in_reply_to ? "reply" : "transaction",
  2259. fd);
  2260. ret = -EPERM;
  2261. goto err_fd_not_accepted;
  2262. }
  2263. file = fget(fd);
  2264. if (!file) {
  2265. binder_user_error("%d:%d got transaction with invalid fd, %d\n",
  2266. proc->pid, thread->pid, fd);
  2267. ret = -EBADF;
  2268. goto err_fget;
  2269. }
  2270. ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
  2271. if (ret < 0) {
  2272. ret = -EPERM;
  2273. goto err_security;
  2274. }
  2275. target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
  2276. if (target_fd < 0) {
  2277. ret = -ENOMEM;
  2278. goto err_get_unused_fd;
  2279. }
  2280. task_fd_install(target_proc, target_fd, file);
  2281. trace_binder_transaction_fd(t, fd, target_fd);
  2282. binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
  2283. fd, target_fd);
  2284. return target_fd;
  2285. err_get_unused_fd:
  2286. err_security:
  2287. fput(file);
  2288. err_fget:
  2289. err_fd_not_accepted:
  2290. return ret;
  2291. }
  2292. static int binder_translate_fd_array(struct binder_fd_array_object *fda,
  2293. struct binder_buffer_object *parent,
  2294. struct binder_transaction *t,
  2295. struct binder_thread *thread,
  2296. struct binder_transaction *in_reply_to)
  2297. {
  2298. binder_size_t fdi, fd_buf_size, num_installed_fds;
  2299. int target_fd;
  2300. uintptr_t parent_buffer;
  2301. u32 *fd_array;
  2302. struct binder_proc *proc = thread->proc;
  2303. struct binder_proc *target_proc = t->to_proc;
  2304. fd_buf_size = sizeof(u32) * fda->num_fds;
  2305. if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
  2306. binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
  2307. proc->pid, thread->pid, (u64)fda->num_fds);
  2308. return -EINVAL;
  2309. }
  2310. if (fd_buf_size > parent->length ||
  2311. fda->parent_offset > parent->length - fd_buf_size) {
  2312. /* No space for all file descriptors here. */
  2313. binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
  2314. proc->pid, thread->pid, (u64)fda->num_fds);
  2315. return -EINVAL;
  2316. }
  2317. /*
  2318. * Since the parent was already fixed up, convert it
  2319. * back to the kernel address space to access it
  2320. */
  2321. parent_buffer = parent->buffer -
  2322. binder_alloc_get_user_buffer_offset(&target_proc->alloc);
  2323. fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
  2324. if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
  2325. binder_user_error("%d:%d parent offset not aligned correctly.\n",
  2326. proc->pid, thread->pid);
  2327. return -EINVAL;
  2328. }
  2329. for (fdi = 0; fdi < fda->num_fds; fdi++) {
  2330. target_fd = binder_translate_fd(fd_array[fdi], t, thread,
  2331. in_reply_to);
  2332. if (target_fd < 0)
  2333. goto err_translate_fd_failed;
  2334. fd_array[fdi] = target_fd;
  2335. }
  2336. return 0;
  2337. err_translate_fd_failed:
  2338. /*
  2339. * Failed to allocate fd or security error, free fds
  2340. * installed so far.
  2341. */
  2342. num_installed_fds = fdi;
  2343. for (fdi = 0; fdi < num_installed_fds; fdi++)
  2344. task_close_fd(target_proc, fd_array[fdi]);
  2345. return target_fd;
  2346. }
  2347. static int binder_fixup_parent(struct binder_transaction *t,
  2348. struct binder_thread *thread,
  2349. struct binder_buffer_object *bp,
  2350. binder_size_t *off_start,
  2351. binder_size_t num_valid,
  2352. struct binder_buffer_object *last_fixup_obj,
  2353. binder_size_t last_fixup_min_off)
  2354. {
  2355. struct binder_buffer_object *parent;
  2356. u8 *parent_buffer;
  2357. struct binder_buffer *b = t->buffer;
  2358. struct binder_proc *proc = thread->proc;
  2359. struct binder_proc *target_proc = t->to_proc;
  2360. if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
  2361. return 0;
  2362. parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
  2363. if (!parent) {
  2364. binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
  2365. proc->pid, thread->pid);
  2366. return -EINVAL;
  2367. }
  2368. if (!binder_validate_fixup(b, off_start,
  2369. parent, bp->parent_offset,
  2370. last_fixup_obj,
  2371. last_fixup_min_off)) {
  2372. binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
  2373. proc->pid, thread->pid);
  2374. return -EINVAL;
  2375. }
  2376. if (parent->length < sizeof(binder_uintptr_t) ||
  2377. bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
  2378. /* No space for a pointer here! */
  2379. binder_user_error("%d:%d got transaction with invalid parent offset\n",
  2380. proc->pid, thread->pid);
  2381. return -EINVAL;
  2382. }
  2383. parent_buffer = (u8 *)((uintptr_t)parent->buffer -
  2384. binder_alloc_get_user_buffer_offset(
  2385. &target_proc->alloc));
  2386. *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
  2387. return 0;
  2388. }
  2389. /**
  2390. * binder_proc_transaction() - sends a transaction to a process and wakes it up
  2391. * @t: transaction to send
  2392. * @proc: process to send the transaction to
  2393. * @thread: thread in @proc to send the transaction to (may be NULL)
  2394. *
  2395. * This function queues a transaction to the specified process. It will try
  2396. * to find a thread in the target process to handle the transaction and
  2397. * wake it up. If no thread is found, the work is queued to the proc
  2398. * waitqueue.
  2399. *
  2400. * If the @thread parameter is not NULL, the transaction is always queued
  2401. * to the waitlist of that specific thread.
  2402. *
  2403. * Return: true if the transactions was successfully queued
  2404. * false if the target process or thread is dead
  2405. */
  2406. static bool binder_proc_transaction(struct binder_transaction *t,
  2407. struct binder_proc *proc,
  2408. struct binder_thread *thread)
  2409. {
  2410. struct binder_node *node = t->buffer->target_node;
  2411. bool oneway = !!(t->flags & TF_ONE_WAY);
  2412. bool pending_async = false;
  2413. BUG_ON(!node);
  2414. binder_node_lock(node);
  2415. if (oneway) {
  2416. BUG_ON(thread);
  2417. if (node->has_async_transaction) {
  2418. pending_async = true;
  2419. } else {
  2420. node->has_async_transaction = true;
  2421. }
  2422. }
  2423. binder_inner_proc_lock(proc);
  2424. if (proc->is_dead || (thread && thread->is_dead)) {
  2425. binder_inner_proc_unlock(proc);
  2426. binder_node_unlock(node);
  2427. return false;
  2428. }
  2429. if (!thread && !pending_async)
  2430. thread = binder_select_thread_ilocked(proc);
  2431. if (thread)
  2432. binder_enqueue_thread_work_ilocked(thread, &t->work);
  2433. else if (!pending_async)
  2434. binder_enqueue_work_ilocked(&t->work, &proc->todo);
  2435. else
  2436. binder_enqueue_work_ilocked(&t->work, &node->async_todo);
  2437. if (!pending_async)
  2438. binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
  2439. binder_inner_proc_unlock(proc);
  2440. binder_node_unlock(node);
  2441. return true;
  2442. }
  2443. /**
  2444. * binder_get_node_refs_for_txn() - Get required refs on node for txn
  2445. * @node: struct binder_node for which to get refs
  2446. * @proc: returns @node->proc if valid
  2447. * @error: if no @proc then returns BR_DEAD_REPLY
  2448. *
  2449. * User-space normally keeps the node alive when creating a transaction
  2450. * since it has a reference to the target. The local strong ref keeps it
  2451. * alive if the sending process dies before the target process processes
  2452. * the transaction. If the source process is malicious or has a reference
  2453. * counting bug, relying on the local strong ref can fail.
  2454. *
  2455. * Since user-space can cause the local strong ref to go away, we also take
  2456. * a tmpref on the node to ensure it survives while we are constructing
  2457. * the transaction. We also need a tmpref on the proc while we are
  2458. * constructing the transaction, so we take that here as well.
  2459. *
  2460. * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
  2461. * Also sets @proc if valid. If the @node->proc is NULL indicating that the
  2462. * target proc has died, @error is set to BR_DEAD_REPLY
  2463. */
  2464. static struct binder_node *binder_get_node_refs_for_txn(
  2465. struct binder_node *node,
  2466. struct binder_proc **procp,
  2467. uint32_t *error)
  2468. {
  2469. struct binder_node *target_node = NULL;
  2470. binder_node_inner_lock(node);
  2471. if (node->proc) {
  2472. target_node = node;
  2473. binder_inc_node_nilocked(node, 1, 0, NULL);
  2474. binder_inc_node_tmpref_ilocked(node);
  2475. node->proc->tmp_ref++;
  2476. *procp = node->proc;
  2477. } else
  2478. *error = BR_DEAD_REPLY;
  2479. binder_node_inner_unlock(node);
  2480. return target_node;
  2481. }
  2482. static void binder_transaction(struct binder_proc *proc,
  2483. struct binder_thread *thread,
  2484. struct binder_transaction_data *tr, int reply,
  2485. binder_size_t extra_buffers_size)
  2486. {
  2487. int ret;
  2488. struct binder_transaction *t;
  2489. struct binder_work *tcomplete;
  2490. binder_size_t *offp, *off_end, *off_start;
  2491. binder_size_t off_min;
  2492. u8 *sg_bufp, *sg_buf_end;
  2493. struct binder_proc *target_proc = NULL;
  2494. struct binder_thread *target_thread = NULL;
  2495. struct binder_node *target_node = NULL;
  2496. struct binder_transaction *in_reply_to = NULL;
  2497. struct binder_transaction_log_entry *e;
  2498. uint32_t return_error = 0;
  2499. uint32_t return_error_param = 0;
  2500. uint32_t return_error_line = 0;
  2501. struct binder_buffer_object *last_fixup_obj = NULL;
  2502. binder_size_t last_fixup_min_off = 0;
  2503. struct binder_context *context = proc->context;
  2504. int t_debug_id = atomic_inc_return(&binder_last_id);
  2505. e = binder_transaction_log_add(&binder_transaction_log);
  2506. e->debug_id = t_debug_id;
  2507. e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
  2508. e->from_proc = proc->pid;
  2509. e->from_thread = thread->pid;
  2510. e->target_handle = tr->target.handle;
  2511. e->data_size = tr->data_size;
  2512. e->offsets_size = tr->offsets_size;
  2513. e->context_name = proc->context->name;
  2514. if (reply) {
  2515. binder_inner_proc_lock(proc);
  2516. in_reply_to = thread->transaction_stack;
  2517. if (in_reply_to == NULL) {
  2518. binder_inner_proc_unlock(proc);
  2519. binder_user_error("%d:%d got reply transaction with no transaction stack\n",
  2520. proc->pid, thread->pid);
  2521. return_error = BR_FAILED_REPLY;
  2522. return_error_param = -EPROTO;
  2523. return_error_line = __LINE__;
  2524. goto err_empty_call_stack;
  2525. }
  2526. if (in_reply_to->to_thread != thread) {
  2527. spin_lock(&in_reply_to->lock);
  2528. binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
  2529. proc->pid, thread->pid, in_reply_to->debug_id,
  2530. in_reply_to->to_proc ?
  2531. in_reply_to->to_proc->pid : 0,
  2532. in_reply_to->to_thread ?
  2533. in_reply_to->to_thread->pid : 0);
  2534. spin_unlock(&in_reply_to->lock);
  2535. binder_inner_proc_unlock(proc);
  2536. return_error = BR_FAILED_REPLY;
  2537. return_error_param = -EPROTO;
  2538. return_error_line = __LINE__;
  2539. in_reply_to = NULL;
  2540. goto err_bad_call_stack;
  2541. }
  2542. thread->transaction_stack = in_reply_to->to_parent;
  2543. binder_inner_proc_unlock(proc);
  2544. binder_set_nice(in_reply_to->saved_priority);
  2545. target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
  2546. if (target_thread == NULL) {
  2547. return_error = BR_DEAD_REPLY;
  2548. return_error_line = __LINE__;
  2549. goto err_dead_binder;
  2550. }
  2551. if (target_thread->transaction_stack != in_reply_to) {
  2552. binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
  2553. proc->pid, thread->pid,
  2554. target_thread->transaction_stack ?
  2555. target_thread->transaction_stack->debug_id : 0,
  2556. in_reply_to->debug_id);
  2557. binder_inner_proc_unlock(target_thread->proc);
  2558. return_error = BR_FAILED_REPLY;
  2559. return_error_param = -EPROTO;
  2560. return_error_line = __LINE__;
  2561. in_reply_to = NULL;
  2562. target_thread = NULL;
  2563. goto err_dead_binder;
  2564. }
  2565. target_proc = target_thread->proc;
  2566. target_proc->tmp_ref++;
  2567. binder_inner_proc_unlock(target_thread->proc);
  2568. } else {
  2569. if (tr->target.handle) {
  2570. struct binder_ref *ref;
  2571. /*
  2572. * There must already be a strong ref
  2573. * on this node. If so, do a strong
  2574. * increment on the node to ensure it
  2575. * stays alive until the transaction is
  2576. * done.
  2577. */
  2578. binder_proc_lock(proc);
  2579. ref = binder_get_ref_olocked(proc, tr->target.handle,
  2580. true);
  2581. if (ref) {
  2582. target_node = binder_get_node_refs_for_txn(
  2583. ref->node, &target_proc,
  2584. &return_error);
  2585. } else {
  2586. binder_user_error("%d:%d got transaction to invalid handle\n",
  2587. proc->pid, thread->pid);
  2588. return_error = BR_FAILED_REPLY;
  2589. }
  2590. binder_proc_unlock(proc);
  2591. } else {
  2592. mutex_lock(&context->context_mgr_node_lock);
  2593. target_node = context->binder_context_mgr_node;
  2594. if (target_node)
  2595. target_node = binder_get_node_refs_for_txn(
  2596. target_node, &target_proc,
  2597. &return_error);
  2598. else
  2599. return_error = BR_DEAD_REPLY;
  2600. mutex_unlock(&context->context_mgr_node_lock);
  2601. if (target_node && target_proc == proc) {
  2602. binder_user_error("%d:%d got transaction to context manager from process owning it\n",
  2603. proc->pid, thread->pid);
  2604. return_error = BR_FAILED_REPLY;
  2605. return_error_param = -EINVAL;
  2606. return_error_line = __LINE__;
  2607. goto err_invalid_target_handle;
  2608. }
  2609. }
  2610. if (!target_node) {
  2611. /*
  2612. * return_error is set above
  2613. */
  2614. return_error_param = -EINVAL;
  2615. return_error_line = __LINE__;
  2616. goto err_dead_binder;
  2617. }
  2618. e->to_node = target_node->debug_id;
  2619. if (security_binder_transaction(proc->tsk,
  2620. target_proc->tsk) < 0) {
  2621. return_error = BR_FAILED_REPLY;
  2622. return_error_param = -EPERM;
  2623. return_error_line = __LINE__;
  2624. goto err_invalid_target_handle;
  2625. }
  2626. binder_inner_proc_lock(proc);
  2627. if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
  2628. struct binder_transaction *tmp;
  2629. tmp = thread->transaction_stack;
  2630. if (tmp->to_thread != thread) {
  2631. spin_lock(&tmp->lock);
  2632. binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
  2633. proc->pid, thread->pid, tmp->debug_id,
  2634. tmp->to_proc ? tmp->to_proc->pid : 0,
  2635. tmp->to_thread ?
  2636. tmp->to_thread->pid : 0);
  2637. spin_unlock(&tmp->lock);
  2638. binder_inner_proc_unlock(proc);
  2639. return_error = BR_FAILED_REPLY;
  2640. return_error_param = -EPROTO;
  2641. return_error_line = __LINE__;
  2642. goto err_bad_call_stack;
  2643. }
  2644. while (tmp) {
  2645. struct binder_thread *from;
  2646. spin_lock(&tmp->lock);
  2647. from = tmp->from;
  2648. if (from && from->proc == target_proc) {
  2649. atomic_inc(&from->tmp_ref);
  2650. target_thread = from;
  2651. spin_unlock(&tmp->lock);
  2652. break;
  2653. }
  2654. spin_unlock(&tmp->lock);
  2655. tmp = tmp->from_parent;
  2656. }
  2657. }
  2658. binder_inner_proc_unlock(proc);
  2659. }
  2660. if (target_thread)
  2661. e->to_thread = target_thread->pid;
  2662. e->to_proc = target_proc->pid;
  2663. /* TODO: reuse incoming transaction for reply */
  2664. t = kzalloc(sizeof(*t), GFP_KERNEL);
  2665. if (t == NULL) {
  2666. return_error = BR_FAILED_REPLY;
  2667. return_error_param = -ENOMEM;
  2668. return_error_line = __LINE__;
  2669. goto err_alloc_t_failed;
  2670. }
  2671. binder_stats_created(BINDER_STAT_TRANSACTION);
  2672. spin_lock_init(&t->lock);
  2673. tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
  2674. if (tcomplete == NULL) {
  2675. return_error = BR_FAILED_REPLY;
  2676. return_error_param = -ENOMEM;
  2677. return_error_line = __LINE__;
  2678. goto err_alloc_tcomplete_failed;
  2679. }
  2680. binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
  2681. t->debug_id = t_debug_id;
  2682. if (reply)
  2683. binder_debug(BINDER_DEBUG_TRANSACTION,
  2684. "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
  2685. proc->pid, thread->pid, t->debug_id,
  2686. target_proc->pid, target_thread->pid,
  2687. (u64)tr->data.ptr.buffer,
  2688. (u64)tr->data.ptr.offsets,
  2689. (u64)tr->data_size, (u64)tr->offsets_size,
  2690. (u64)extra_buffers_size);
  2691. else
  2692. binder_debug(BINDER_DEBUG_TRANSACTION,
  2693. "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
  2694. proc->pid, thread->pid, t->debug_id,
  2695. target_proc->pid, target_node->debug_id,
  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. if (!reply && !(tr->flags & TF_ONE_WAY))
  2701. t->from = thread;
  2702. else
  2703. t->from = NULL;
  2704. t->sender_euid = task_euid(proc->tsk);
  2705. t->to_proc = target_proc;
  2706. t->to_thread = target_thread;
  2707. t->code = tr->code;
  2708. t->flags = tr->flags;
  2709. t->priority = task_nice(current);
  2710. trace_binder_transaction(reply, t, target_node);
  2711. t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
  2712. tr->offsets_size, extra_buffers_size,
  2713. !reply && (t->flags & TF_ONE_WAY));
  2714. if (IS_ERR(t->buffer)) {
  2715. /*
  2716. * -ESRCH indicates VMA cleared. The target is dying.
  2717. */
  2718. return_error_param = PTR_ERR(t->buffer);
  2719. return_error = return_error_param == -ESRCH ?
  2720. BR_DEAD_REPLY : BR_FAILED_REPLY;
  2721. return_error_line = __LINE__;
  2722. t->buffer = NULL;
  2723. goto err_binder_alloc_buf_failed;
  2724. }
  2725. t->buffer->allow_user_free = 0;
  2726. t->buffer->debug_id = t->debug_id;
  2727. t->buffer->transaction = t;
  2728. t->buffer->target_node = target_node;
  2729. trace_binder_transaction_alloc_buf(t->buffer);
  2730. off_start = (binder_size_t *)(t->buffer->data +
  2731. ALIGN(tr->data_size, sizeof(void *)));
  2732. offp = off_start;
  2733. if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
  2734. tr->data.ptr.buffer, tr->data_size)) {
  2735. binder_user_error("%d:%d got transaction with invalid data ptr\n",
  2736. proc->pid, thread->pid);
  2737. return_error = BR_FAILED_REPLY;
  2738. return_error_param = -EFAULT;
  2739. return_error_line = __LINE__;
  2740. goto err_copy_data_failed;
  2741. }
  2742. if (copy_from_user(offp, (const void __user *)(uintptr_t)
  2743. tr->data.ptr.offsets, tr->offsets_size)) {
  2744. binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
  2745. proc->pid, thread->pid);
  2746. return_error = BR_FAILED_REPLY;
  2747. return_error_param = -EFAULT;
  2748. return_error_line = __LINE__;
  2749. goto err_copy_data_failed;
  2750. }
  2751. if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
  2752. binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
  2753. proc->pid, thread->pid, (u64)tr->offsets_size);
  2754. return_error = BR_FAILED_REPLY;
  2755. return_error_param = -EINVAL;
  2756. return_error_line = __LINE__;
  2757. goto err_bad_offset;
  2758. }
  2759. if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
  2760. binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
  2761. proc->pid, thread->pid,
  2762. (u64)extra_buffers_size);
  2763. return_error = BR_FAILED_REPLY;
  2764. return_error_param = -EINVAL;
  2765. return_error_line = __LINE__;
  2766. goto err_bad_offset;
  2767. }
  2768. off_end = (void *)off_start + tr->offsets_size;
  2769. sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
  2770. sg_buf_end = sg_bufp + extra_buffers_size;
  2771. off_min = 0;
  2772. for (; offp < off_end; offp++) {
  2773. struct binder_object_header *hdr;
  2774. size_t object_size = binder_validate_object(t->buffer, *offp);
  2775. if (object_size == 0 || *offp < off_min) {
  2776. binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
  2777. proc->pid, thread->pid, (u64)*offp,
  2778. (u64)off_min,
  2779. (u64)t->buffer->data_size);
  2780. return_error = BR_FAILED_REPLY;
  2781. return_error_param = -EINVAL;
  2782. return_error_line = __LINE__;
  2783. goto err_bad_offset;
  2784. }
  2785. hdr = (struct binder_object_header *)(t->buffer->data + *offp);
  2786. off_min = *offp + object_size;
  2787. switch (hdr->type) {
  2788. case BINDER_TYPE_BINDER:
  2789. case BINDER_TYPE_WEAK_BINDER: {
  2790. struct flat_binder_object *fp;
  2791. fp = to_flat_binder_object(hdr);
  2792. ret = binder_translate_binder(fp, t, thread);
  2793. if (ret < 0) {
  2794. return_error = BR_FAILED_REPLY;
  2795. return_error_param = ret;
  2796. return_error_line = __LINE__;
  2797. goto err_translate_failed;
  2798. }
  2799. } break;
  2800. case BINDER_TYPE_HANDLE:
  2801. case BINDER_TYPE_WEAK_HANDLE: {
  2802. struct flat_binder_object *fp;
  2803. fp = to_flat_binder_object(hdr);
  2804. ret = binder_translate_handle(fp, t, thread);
  2805. if (ret < 0) {
  2806. return_error = BR_FAILED_REPLY;
  2807. return_error_param = ret;
  2808. return_error_line = __LINE__;
  2809. goto err_translate_failed;
  2810. }
  2811. } break;
  2812. case BINDER_TYPE_FD: {
  2813. struct binder_fd_object *fp = to_binder_fd_object(hdr);
  2814. int target_fd = binder_translate_fd(fp->fd, t, thread,
  2815. in_reply_to);
  2816. if (target_fd < 0) {
  2817. return_error = BR_FAILED_REPLY;
  2818. return_error_param = target_fd;
  2819. return_error_line = __LINE__;
  2820. goto err_translate_failed;
  2821. }
  2822. fp->pad_binder = 0;
  2823. fp->fd = target_fd;
  2824. } break;
  2825. case BINDER_TYPE_FDA: {
  2826. struct binder_fd_array_object *fda =
  2827. to_binder_fd_array_object(hdr);
  2828. struct binder_buffer_object *parent =
  2829. binder_validate_ptr(t->buffer, fda->parent,
  2830. off_start,
  2831. offp - off_start);
  2832. if (!parent) {
  2833. binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
  2834. proc->pid, thread->pid);
  2835. return_error = BR_FAILED_REPLY;
  2836. return_error_param = -EINVAL;
  2837. return_error_line = __LINE__;
  2838. goto err_bad_parent;
  2839. }
  2840. if (!binder_validate_fixup(t->buffer, off_start,
  2841. parent, fda->parent_offset,
  2842. last_fixup_obj,
  2843. last_fixup_min_off)) {
  2844. binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
  2845. proc->pid, thread->pid);
  2846. return_error = BR_FAILED_REPLY;
  2847. return_error_param = -EINVAL;
  2848. return_error_line = __LINE__;
  2849. goto err_bad_parent;
  2850. }
  2851. ret = binder_translate_fd_array(fda, parent, t, thread,
  2852. in_reply_to);
  2853. if (ret < 0) {
  2854. return_error = BR_FAILED_REPLY;
  2855. return_error_param = ret;
  2856. return_error_line = __LINE__;
  2857. goto err_translate_failed;
  2858. }
  2859. last_fixup_obj = parent;
  2860. last_fixup_min_off =
  2861. fda->parent_offset + sizeof(u32) * fda->num_fds;
  2862. } break;
  2863. case BINDER_TYPE_PTR: {
  2864. struct binder_buffer_object *bp =
  2865. to_binder_buffer_object(hdr);
  2866. size_t buf_left = sg_buf_end - sg_bufp;
  2867. if (bp->length > buf_left) {
  2868. binder_user_error("%d:%d got transaction with too large buffer\n",
  2869. proc->pid, thread->pid);
  2870. return_error = BR_FAILED_REPLY;
  2871. return_error_param = -EINVAL;
  2872. return_error_line = __LINE__;
  2873. goto err_bad_offset;
  2874. }
  2875. if (copy_from_user(sg_bufp,
  2876. (const void __user *)(uintptr_t)
  2877. bp->buffer, bp->length)) {
  2878. binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
  2879. proc->pid, thread->pid);
  2880. return_error_param = -EFAULT;
  2881. return_error = BR_FAILED_REPLY;
  2882. return_error_line = __LINE__;
  2883. goto err_copy_data_failed;
  2884. }
  2885. /* Fixup buffer pointer to target proc address space */
  2886. bp->buffer = (uintptr_t)sg_bufp +
  2887. binder_alloc_get_user_buffer_offset(
  2888. &target_proc->alloc);
  2889. sg_bufp += ALIGN(bp->length, sizeof(u64));
  2890. ret = binder_fixup_parent(t, thread, bp, off_start,
  2891. offp - off_start,
  2892. last_fixup_obj,
  2893. last_fixup_min_off);
  2894. if (ret < 0) {
  2895. return_error = BR_FAILED_REPLY;
  2896. return_error_param = ret;
  2897. return_error_line = __LINE__;
  2898. goto err_translate_failed;
  2899. }
  2900. last_fixup_obj = bp;
  2901. last_fixup_min_off = 0;
  2902. } break;
  2903. default:
  2904. binder_user_error("%d:%d got transaction with invalid object type, %x\n",
  2905. proc->pid, thread->pid, hdr->type);
  2906. return_error = BR_FAILED_REPLY;
  2907. return_error_param = -EINVAL;
  2908. return_error_line = __LINE__;
  2909. goto err_bad_object_type;
  2910. }
  2911. }
  2912. tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
  2913. t->work.type = BINDER_WORK_TRANSACTION;
  2914. if (reply) {
  2915. binder_enqueue_thread_work(thread, tcomplete);
  2916. binder_inner_proc_lock(target_proc);
  2917. if (target_thread->is_dead) {
  2918. binder_inner_proc_unlock(target_proc);
  2919. goto err_dead_proc_or_thread;
  2920. }
  2921. BUG_ON(t->buffer->async_transaction != 0);
  2922. binder_pop_transaction_ilocked(target_thread, in_reply_to);
  2923. binder_enqueue_thread_work_ilocked(target_thread, &t->work);
  2924. binder_inner_proc_unlock(target_proc);
  2925. wake_up_interruptible_sync(&target_thread->wait);
  2926. binder_free_transaction(in_reply_to);
  2927. } else if (!(t->flags & TF_ONE_WAY)) {
  2928. BUG_ON(t->buffer->async_transaction != 0);
  2929. binder_inner_proc_lock(proc);
  2930. /*
  2931. * Defer the TRANSACTION_COMPLETE, so we don't return to
  2932. * userspace immediately; this allows the target process to
  2933. * immediately start processing this transaction, reducing
  2934. * latency. We will then return the TRANSACTION_COMPLETE when
  2935. * the target replies (or there is an error).
  2936. */
  2937. binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
  2938. t->need_reply = 1;
  2939. t->from_parent = thread->transaction_stack;
  2940. thread->transaction_stack = t;
  2941. binder_inner_proc_unlock(proc);
  2942. if (!binder_proc_transaction(t, target_proc, target_thread)) {
  2943. binder_inner_proc_lock(proc);
  2944. binder_pop_transaction_ilocked(thread, t);
  2945. binder_inner_proc_unlock(proc);
  2946. goto err_dead_proc_or_thread;
  2947. }
  2948. } else {
  2949. BUG_ON(target_node == NULL);
  2950. BUG_ON(t->buffer->async_transaction != 1);
  2951. binder_enqueue_thread_work(thread, tcomplete);
  2952. if (!binder_proc_transaction(t, target_proc, NULL))
  2953. goto err_dead_proc_or_thread;
  2954. }
  2955. if (target_thread)
  2956. binder_thread_dec_tmpref(target_thread);
  2957. binder_proc_dec_tmpref(target_proc);
  2958. if (target_node)
  2959. binder_dec_node_tmpref(target_node);
  2960. /*
  2961. * write barrier to synchronize with initialization
  2962. * of log entry
  2963. */
  2964. smp_wmb();
  2965. WRITE_ONCE(e->debug_id_done, t_debug_id);
  2966. return;
  2967. err_dead_proc_or_thread:
  2968. return_error = BR_DEAD_REPLY;
  2969. return_error_line = __LINE__;
  2970. binder_dequeue_work(proc, tcomplete);
  2971. err_translate_failed:
  2972. err_bad_object_type:
  2973. err_bad_offset:
  2974. err_bad_parent:
  2975. err_copy_data_failed:
  2976. trace_binder_transaction_failed_buffer_release(t->buffer);
  2977. binder_transaction_buffer_release(target_proc, t->buffer, offp);
  2978. if (target_node)
  2979. binder_dec_node_tmpref(target_node);
  2980. target_node = NULL;
  2981. t->buffer->transaction = NULL;
  2982. binder_alloc_free_buf(&target_proc->alloc, t->buffer);
  2983. err_binder_alloc_buf_failed:
  2984. kfree(tcomplete);
  2985. binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
  2986. err_alloc_tcomplete_failed:
  2987. kfree(t);
  2988. binder_stats_deleted(BINDER_STAT_TRANSACTION);
  2989. err_alloc_t_failed:
  2990. err_bad_call_stack:
  2991. err_empty_call_stack:
  2992. err_dead_binder:
  2993. err_invalid_target_handle:
  2994. if (target_thread)
  2995. binder_thread_dec_tmpref(target_thread);
  2996. if (target_proc)
  2997. binder_proc_dec_tmpref(target_proc);
  2998. if (target_node) {
  2999. binder_dec_node(target_node, 1, 0);
  3000. binder_dec_node_tmpref(target_node);
  3001. }
  3002. binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
  3003. "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
  3004. proc->pid, thread->pid, return_error, return_error_param,
  3005. (u64)tr->data_size, (u64)tr->offsets_size,
  3006. return_error_line);
  3007. {
  3008. struct binder_transaction_log_entry *fe;
  3009. e->return_error = return_error;
  3010. e->return_error_param = return_error_param;
  3011. e->return_error_line = return_error_line;
  3012. fe = binder_transaction_log_add(&binder_transaction_log_failed);
  3013. *fe = *e;
  3014. /*
  3015. * write barrier to synchronize with initialization
  3016. * of log entry
  3017. */
  3018. smp_wmb();
  3019. WRITE_ONCE(e->debug_id_done, t_debug_id);
  3020. WRITE_ONCE(fe->debug_id_done, t_debug_id);
  3021. }
  3022. BUG_ON(thread->return_error.cmd != BR_OK);
  3023. if (in_reply_to) {
  3024. thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
  3025. binder_enqueue_thread_work(thread, &thread->return_error.work);
  3026. binder_send_failed_reply(in_reply_to, return_error);
  3027. } else {
  3028. thread->return_error.cmd = return_error;
  3029. binder_enqueue_thread_work(thread, &thread->return_error.work);
  3030. }
  3031. }
  3032. static int binder_thread_write(struct binder_proc *proc,
  3033. struct binder_thread *thread,
  3034. binder_uintptr_t binder_buffer, size_t size,
  3035. binder_size_t *consumed)
  3036. {
  3037. uint32_t cmd;
  3038. struct binder_context *context = proc->context;
  3039. void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
  3040. void __user *ptr = buffer + *consumed;
  3041. void __user *end = buffer + size;
  3042. while (ptr < end && thread->return_error.cmd == BR_OK) {
  3043. int ret;
  3044. if (get_user(cmd, (uint32_t __user *)ptr))
  3045. return -EFAULT;
  3046. ptr += sizeof(uint32_t);
  3047. trace_binder_command(cmd);
  3048. if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
  3049. atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
  3050. atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
  3051. atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
  3052. }
  3053. switch (cmd) {
  3054. case BC_INCREFS:
  3055. case BC_ACQUIRE:
  3056. case BC_RELEASE:
  3057. case BC_DECREFS: {
  3058. uint32_t target;
  3059. const char *debug_string;
  3060. bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
  3061. bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
  3062. struct binder_ref_data rdata;
  3063. if (get_user(target, (uint32_t __user *)ptr))
  3064. return -EFAULT;
  3065. ptr += sizeof(uint32_t);
  3066. ret = -1;
  3067. if (increment && !target) {
  3068. struct binder_node *ctx_mgr_node;
  3069. mutex_lock(&context->context_mgr_node_lock);
  3070. ctx_mgr_node = context->binder_context_mgr_node;
  3071. if (ctx_mgr_node)
  3072. ret = binder_inc_ref_for_node(
  3073. proc, ctx_mgr_node,
  3074. strong, NULL, &rdata);
  3075. mutex_unlock(&context->context_mgr_node_lock);
  3076. }
  3077. if (ret)
  3078. ret = binder_update_ref_for_handle(
  3079. proc, target, increment, strong,
  3080. &rdata);
  3081. if (!ret && rdata.desc != target) {
  3082. binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
  3083. proc->pid, thread->pid,
  3084. target, rdata.desc);
  3085. }
  3086. switch (cmd) {
  3087. case BC_INCREFS:
  3088. debug_string = "IncRefs";
  3089. break;
  3090. case BC_ACQUIRE:
  3091. debug_string = "Acquire";
  3092. break;
  3093. case BC_RELEASE:
  3094. debug_string = "Release";
  3095. break;
  3096. case BC_DECREFS:
  3097. default:
  3098. debug_string = "DecRefs";
  3099. break;
  3100. }
  3101. if (ret) {
  3102. binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
  3103. proc->pid, thread->pid, debug_string,
  3104. strong, target, ret);
  3105. break;
  3106. }
  3107. binder_debug(BINDER_DEBUG_USER_REFS,
  3108. "%d:%d %s ref %d desc %d s %d w %d\n",
  3109. proc->pid, thread->pid, debug_string,
  3110. rdata.debug_id, rdata.desc, rdata.strong,
  3111. rdata.weak);
  3112. break;
  3113. }
  3114. case BC_INCREFS_DONE:
  3115. case BC_ACQUIRE_DONE: {
  3116. binder_uintptr_t node_ptr;
  3117. binder_uintptr_t cookie;
  3118. struct binder_node *node;
  3119. bool free_node;
  3120. if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
  3121. return -EFAULT;
  3122. ptr += sizeof(binder_uintptr_t);
  3123. if (get_user(cookie, (binder_uintptr_t __user *)ptr))
  3124. return -EFAULT;
  3125. ptr += sizeof(binder_uintptr_t);
  3126. node = binder_get_node(proc, node_ptr);
  3127. if (node == NULL) {
  3128. binder_user_error("%d:%d %s u%016llx no match\n",
  3129. proc->pid, thread->pid,
  3130. cmd == BC_INCREFS_DONE ?
  3131. "BC_INCREFS_DONE" :
  3132. "BC_ACQUIRE_DONE",
  3133. (u64)node_ptr);
  3134. break;
  3135. }
  3136. if (cookie != node->cookie) {
  3137. binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
  3138. proc->pid, thread->pid,
  3139. cmd == BC_INCREFS_DONE ?
  3140. "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
  3141. (u64)node_ptr, node->debug_id,
  3142. (u64)cookie, (u64)node->cookie);
  3143. binder_put_node(node);
  3144. break;
  3145. }
  3146. binder_node_inner_lock(node);
  3147. if (cmd == BC_ACQUIRE_DONE) {
  3148. if (node->pending_strong_ref == 0) {
  3149. binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
  3150. proc->pid, thread->pid,
  3151. node->debug_id);
  3152. binder_node_inner_unlock(node);
  3153. binder_put_node(node);
  3154. break;
  3155. }
  3156. node->pending_strong_ref = 0;
  3157. } else {
  3158. if (node->pending_weak_ref == 0) {
  3159. binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
  3160. proc->pid, thread->pid,
  3161. node->debug_id);
  3162. binder_node_inner_unlock(node);
  3163. binder_put_node(node);
  3164. break;
  3165. }
  3166. node->pending_weak_ref = 0;
  3167. }
  3168. free_node = binder_dec_node_nilocked(node,
  3169. cmd == BC_ACQUIRE_DONE, 0);
  3170. WARN_ON(free_node);
  3171. binder_debug(BINDER_DEBUG_USER_REFS,
  3172. "%d:%d %s node %d ls %d lw %d tr %d\n",
  3173. proc->pid, thread->pid,
  3174. cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
  3175. node->debug_id, node->local_strong_refs,
  3176. node->local_weak_refs, node->tmp_refs);
  3177. binder_node_inner_unlock(node);
  3178. binder_put_node(node);
  3179. break;
  3180. }
  3181. case BC_ATTEMPT_ACQUIRE:
  3182. pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
  3183. return -EINVAL;
  3184. case BC_ACQUIRE_RESULT:
  3185. pr_err("BC_ACQUIRE_RESULT not supported\n");
  3186. return -EINVAL;
  3187. case BC_FREE_BUFFER: {
  3188. binder_uintptr_t data_ptr;
  3189. struct binder_buffer *buffer;
  3190. if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
  3191. return -EFAULT;
  3192. ptr += sizeof(binder_uintptr_t);
  3193. buffer = binder_alloc_prepare_to_free(&proc->alloc,
  3194. data_ptr);
  3195. if (buffer == NULL) {
  3196. binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
  3197. proc->pid, thread->pid, (u64)data_ptr);
  3198. break;
  3199. }
  3200. if (!buffer->allow_user_free) {
  3201. binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
  3202. proc->pid, thread->pid, (u64)data_ptr);
  3203. break;
  3204. }
  3205. binder_debug(BINDER_DEBUG_FREE_BUFFER,
  3206. "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
  3207. proc->pid, thread->pid, (u64)data_ptr,
  3208. buffer->debug_id,
  3209. buffer->transaction ? "active" : "finished");
  3210. if (buffer->transaction) {
  3211. buffer->transaction->buffer = NULL;
  3212. buffer->transaction = NULL;
  3213. }
  3214. if (buffer->async_transaction && buffer->target_node) {
  3215. struct binder_node *buf_node;
  3216. struct binder_work *w;
  3217. buf_node = buffer->target_node;
  3218. binder_node_inner_lock(buf_node);
  3219. BUG_ON(!buf_node->has_async_transaction);
  3220. BUG_ON(buf_node->proc != proc);
  3221. w = binder_dequeue_work_head_ilocked(
  3222. &buf_node->async_todo);
  3223. if (!w) {
  3224. buf_node->has_async_transaction = false;
  3225. } else {
  3226. binder_enqueue_work_ilocked(
  3227. w, &proc->todo);
  3228. binder_wakeup_proc_ilocked(proc);
  3229. }
  3230. binder_node_inner_unlock(buf_node);
  3231. }
  3232. trace_binder_transaction_buffer_release(buffer);
  3233. binder_transaction_buffer_release(proc, buffer, NULL);
  3234. binder_alloc_free_buf(&proc->alloc, buffer);
  3235. break;
  3236. }
  3237. case BC_TRANSACTION_SG:
  3238. case BC_REPLY_SG: {
  3239. struct binder_transaction_data_sg tr;
  3240. if (copy_from_user(&tr, ptr, sizeof(tr)))
  3241. return -EFAULT;
  3242. ptr += sizeof(tr);
  3243. binder_transaction(proc, thread, &tr.transaction_data,
  3244. cmd == BC_REPLY_SG, tr.buffers_size);
  3245. break;
  3246. }
  3247. case BC_TRANSACTION:
  3248. case BC_REPLY: {
  3249. struct binder_transaction_data tr;
  3250. if (copy_from_user(&tr, ptr, sizeof(tr)))
  3251. return -EFAULT;
  3252. ptr += sizeof(tr);
  3253. binder_transaction(proc, thread, &tr,
  3254. cmd == BC_REPLY, 0);
  3255. break;
  3256. }
  3257. case BC_REGISTER_LOOPER:
  3258. binder_debug(BINDER_DEBUG_THREADS,
  3259. "%d:%d BC_REGISTER_LOOPER\n",
  3260. proc->pid, thread->pid);
  3261. binder_inner_proc_lock(proc);
  3262. if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
  3263. thread->looper |= BINDER_LOOPER_STATE_INVALID;
  3264. binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
  3265. proc->pid, thread->pid);
  3266. } else if (proc->requested_threads == 0) {
  3267. thread->looper |= BINDER_LOOPER_STATE_INVALID;
  3268. binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
  3269. proc->pid, thread->pid);
  3270. } else {
  3271. proc->requested_threads--;
  3272. proc->requested_threads_started++;
  3273. }
  3274. thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
  3275. binder_inner_proc_unlock(proc);
  3276. break;
  3277. case BC_ENTER_LOOPER:
  3278. binder_debug(BINDER_DEBUG_THREADS,
  3279. "%d:%d BC_ENTER_LOOPER\n",
  3280. proc->pid, thread->pid);
  3281. if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
  3282. thread->looper |= BINDER_LOOPER_STATE_INVALID;
  3283. binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
  3284. proc->pid, thread->pid);
  3285. }
  3286. thread->looper |= BINDER_LOOPER_STATE_ENTERED;
  3287. break;
  3288. case BC_EXIT_LOOPER:
  3289. binder_debug(BINDER_DEBUG_THREADS,
  3290. "%d:%d BC_EXIT_LOOPER\n",
  3291. proc->pid, thread->pid);
  3292. thread->looper |= BINDER_LOOPER_STATE_EXITED;
  3293. break;
  3294. case BC_REQUEST_DEATH_NOTIFICATION:
  3295. case BC_CLEAR_DEATH_NOTIFICATION: {
  3296. uint32_t target;
  3297. binder_uintptr_t cookie;
  3298. struct binder_ref *ref;
  3299. struct binder_ref_death *death = NULL;
  3300. if (get_user(target, (uint32_t __user *)ptr))
  3301. return -EFAULT;
  3302. ptr += sizeof(uint32_t);
  3303. if (get_user(cookie, (binder_uintptr_t __user *)ptr))
  3304. return -EFAULT;
  3305. ptr += sizeof(binder_uintptr_t);
  3306. if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
  3307. /*
  3308. * Allocate memory for death notification
  3309. * before taking lock
  3310. */
  3311. death = kzalloc(sizeof(*death), GFP_KERNEL);
  3312. if (death == NULL) {
  3313. WARN_ON(thread->return_error.cmd !=
  3314. BR_OK);
  3315. thread->return_error.cmd = BR_ERROR;
  3316. binder_enqueue_thread_work(
  3317. thread,
  3318. &thread->return_error.work);
  3319. binder_debug(
  3320. BINDER_DEBUG_FAILED_TRANSACTION,
  3321. "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
  3322. proc->pid, thread->pid);
  3323. break;
  3324. }
  3325. }
  3326. binder_proc_lock(proc);
  3327. ref = binder_get_ref_olocked(proc, target, false);
  3328. if (ref == NULL) {
  3329. binder_user_error("%d:%d %s invalid ref %d\n",
  3330. proc->pid, thread->pid,
  3331. cmd == BC_REQUEST_DEATH_NOTIFICATION ?
  3332. "BC_REQUEST_DEATH_NOTIFICATION" :
  3333. "BC_CLEAR_DEATH_NOTIFICATION",
  3334. target);
  3335. binder_proc_unlock(proc);
  3336. kfree(death);
  3337. break;
  3338. }
  3339. binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
  3340. "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
  3341. proc->pid, thread->pid,
  3342. cmd == BC_REQUEST_DEATH_NOTIFICATION ?
  3343. "BC_REQUEST_DEATH_NOTIFICATION" :
  3344. "BC_CLEAR_DEATH_NOTIFICATION",
  3345. (u64)cookie, ref->data.debug_id,
  3346. ref->data.desc, ref->data.strong,
  3347. ref->data.weak, ref->node->debug_id);
  3348. binder_node_lock(ref->node);
  3349. if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
  3350. if (ref->death) {
  3351. binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
  3352. proc->pid, thread->pid);
  3353. binder_node_unlock(ref->node);
  3354. binder_proc_unlock(proc);
  3355. kfree(death);
  3356. break;
  3357. }
  3358. binder_stats_created(BINDER_STAT_DEATH);
  3359. INIT_LIST_HEAD(&death->work.entry);
  3360. death->cookie = cookie;
  3361. ref->death = death;
  3362. if (ref->node->proc == NULL) {
  3363. ref->death->work.type = BINDER_WORK_DEAD_BINDER;
  3364. binder_inner_proc_lock(proc);
  3365. binder_enqueue_work_ilocked(
  3366. &ref->death->work, &proc->todo);
  3367. binder_wakeup_proc_ilocked(proc);
  3368. binder_inner_proc_unlock(proc);
  3369. }
  3370. } else {
  3371. if (ref->death == NULL) {
  3372. binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
  3373. proc->pid, thread->pid);
  3374. binder_node_unlock(ref->node);
  3375. binder_proc_unlock(proc);
  3376. break;
  3377. }
  3378. death = ref->death;
  3379. if (death->cookie != cookie) {
  3380. binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
  3381. proc->pid, thread->pid,
  3382. (u64)death->cookie,
  3383. (u64)cookie);
  3384. binder_node_unlock(ref->node);
  3385. binder_proc_unlock(proc);
  3386. break;
  3387. }
  3388. ref->death = NULL;
  3389. binder_inner_proc_lock(proc);
  3390. if (list_empty(&death->work.entry)) {
  3391. death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
  3392. if (thread->looper &
  3393. (BINDER_LOOPER_STATE_REGISTERED |
  3394. BINDER_LOOPER_STATE_ENTERED))
  3395. binder_enqueue_thread_work_ilocked(
  3396. thread,
  3397. &death->work);
  3398. else {
  3399. binder_enqueue_work_ilocked(
  3400. &death->work,
  3401. &proc->todo);
  3402. binder_wakeup_proc_ilocked(
  3403. proc);
  3404. }
  3405. } else {
  3406. BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
  3407. death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
  3408. }
  3409. binder_inner_proc_unlock(proc);
  3410. }
  3411. binder_node_unlock(ref->node);
  3412. binder_proc_unlock(proc);
  3413. } break;
  3414. case BC_DEAD_BINDER_DONE: {
  3415. struct binder_work *w;
  3416. binder_uintptr_t cookie;
  3417. struct binder_ref_death *death = NULL;
  3418. if (get_user(cookie, (binder_uintptr_t __user *)ptr))
  3419. return -EFAULT;
  3420. ptr += sizeof(cookie);
  3421. binder_inner_proc_lock(proc);
  3422. list_for_each_entry(w, &proc->delivered_death,
  3423. entry) {
  3424. struct binder_ref_death *tmp_death =
  3425. container_of(w,
  3426. struct binder_ref_death,
  3427. work);
  3428. if (tmp_death->cookie == cookie) {
  3429. death = tmp_death;
  3430. break;
  3431. }
  3432. }
  3433. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  3434. "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
  3435. proc->pid, thread->pid, (u64)cookie,
  3436. death);
  3437. if (death == NULL) {
  3438. binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
  3439. proc->pid, thread->pid, (u64)cookie);
  3440. binder_inner_proc_unlock(proc);
  3441. break;
  3442. }
  3443. binder_dequeue_work_ilocked(&death->work);
  3444. if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
  3445. death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
  3446. if (thread->looper &
  3447. (BINDER_LOOPER_STATE_REGISTERED |
  3448. BINDER_LOOPER_STATE_ENTERED))
  3449. binder_enqueue_thread_work_ilocked(
  3450. thread, &death->work);
  3451. else {
  3452. binder_enqueue_work_ilocked(
  3453. &death->work,
  3454. &proc->todo);
  3455. binder_wakeup_proc_ilocked(proc);
  3456. }
  3457. }
  3458. binder_inner_proc_unlock(proc);
  3459. } break;
  3460. default:
  3461. pr_err("%d:%d unknown command %d\n",
  3462. proc->pid, thread->pid, cmd);
  3463. return -EINVAL;
  3464. }
  3465. *consumed = ptr - buffer;
  3466. }
  3467. return 0;
  3468. }
  3469. static void binder_stat_br(struct binder_proc *proc,
  3470. struct binder_thread *thread, uint32_t cmd)
  3471. {
  3472. trace_binder_return(cmd);
  3473. if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
  3474. atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
  3475. atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
  3476. atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
  3477. }
  3478. }
  3479. static int binder_put_node_cmd(struct binder_proc *proc,
  3480. struct binder_thread *thread,
  3481. void __user **ptrp,
  3482. binder_uintptr_t node_ptr,
  3483. binder_uintptr_t node_cookie,
  3484. int node_debug_id,
  3485. uint32_t cmd, const char *cmd_name)
  3486. {
  3487. void __user *ptr = *ptrp;
  3488. if (put_user(cmd, (uint32_t __user *)ptr))
  3489. return -EFAULT;
  3490. ptr += sizeof(uint32_t);
  3491. if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
  3492. return -EFAULT;
  3493. ptr += sizeof(binder_uintptr_t);
  3494. if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
  3495. return -EFAULT;
  3496. ptr += sizeof(binder_uintptr_t);
  3497. binder_stat_br(proc, thread, cmd);
  3498. binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
  3499. proc->pid, thread->pid, cmd_name, node_debug_id,
  3500. (u64)node_ptr, (u64)node_cookie);
  3501. *ptrp = ptr;
  3502. return 0;
  3503. }
  3504. static int binder_wait_for_work(struct binder_thread *thread,
  3505. bool do_proc_work)
  3506. {
  3507. DEFINE_WAIT(wait);
  3508. struct binder_proc *proc = thread->proc;
  3509. int ret = 0;
  3510. freezer_do_not_count();
  3511. binder_inner_proc_lock(proc);
  3512. for (;;) {
  3513. prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
  3514. if (binder_has_work_ilocked(thread, do_proc_work))
  3515. break;
  3516. if (do_proc_work)
  3517. list_add(&thread->waiting_thread_node,
  3518. &proc->waiting_threads);
  3519. binder_inner_proc_unlock(proc);
  3520. schedule();
  3521. binder_inner_proc_lock(proc);
  3522. list_del_init(&thread->waiting_thread_node);
  3523. if (signal_pending(current)) {
  3524. ret = -ERESTARTSYS;
  3525. break;
  3526. }
  3527. }
  3528. finish_wait(&thread->wait, &wait);
  3529. binder_inner_proc_unlock(proc);
  3530. freezer_count();
  3531. return ret;
  3532. }
  3533. static int binder_thread_read(struct binder_proc *proc,
  3534. struct binder_thread *thread,
  3535. binder_uintptr_t binder_buffer, size_t size,
  3536. binder_size_t *consumed, int non_block)
  3537. {
  3538. void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
  3539. void __user *ptr = buffer + *consumed;
  3540. void __user *end = buffer + size;
  3541. int ret = 0;
  3542. int wait_for_proc_work;
  3543. if (*consumed == 0) {
  3544. if (put_user(BR_NOOP, (uint32_t __user *)ptr))
  3545. return -EFAULT;
  3546. ptr += sizeof(uint32_t);
  3547. }
  3548. retry:
  3549. binder_inner_proc_lock(proc);
  3550. wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
  3551. binder_inner_proc_unlock(proc);
  3552. thread->looper |= BINDER_LOOPER_STATE_WAITING;
  3553. trace_binder_wait_for_work(wait_for_proc_work,
  3554. !!thread->transaction_stack,
  3555. !binder_worklist_empty(proc, &thread->todo));
  3556. if (wait_for_proc_work) {
  3557. if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
  3558. BINDER_LOOPER_STATE_ENTERED))) {
  3559. binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
  3560. proc->pid, thread->pid, thread->looper);
  3561. wait_event_interruptible(binder_user_error_wait,
  3562. binder_stop_on_user_error < 2);
  3563. }
  3564. binder_set_nice(proc->default_priority);
  3565. }
  3566. if (non_block) {
  3567. if (!binder_has_work(thread, wait_for_proc_work))
  3568. ret = -EAGAIN;
  3569. } else {
  3570. ret = binder_wait_for_work(thread, wait_for_proc_work);
  3571. }
  3572. thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
  3573. if (ret)
  3574. return ret;
  3575. while (1) {
  3576. uint32_t cmd;
  3577. struct binder_transaction_data tr;
  3578. struct binder_work *w = NULL;
  3579. struct list_head *list = NULL;
  3580. struct binder_transaction *t = NULL;
  3581. struct binder_thread *t_from;
  3582. binder_inner_proc_lock(proc);
  3583. if (!binder_worklist_empty_ilocked(&thread->todo))
  3584. list = &thread->todo;
  3585. else if (!binder_worklist_empty_ilocked(&proc->todo) &&
  3586. wait_for_proc_work)
  3587. list = &proc->todo;
  3588. else {
  3589. binder_inner_proc_unlock(proc);
  3590. /* no data added */
  3591. if (ptr - buffer == 4 && !thread->looper_need_return)
  3592. goto retry;
  3593. break;
  3594. }
  3595. if (end - ptr < sizeof(tr) + 4) {
  3596. binder_inner_proc_unlock(proc);
  3597. break;
  3598. }
  3599. w = binder_dequeue_work_head_ilocked(list);
  3600. if (binder_worklist_empty_ilocked(&thread->todo))
  3601. thread->process_todo = false;
  3602. switch (w->type) {
  3603. case BINDER_WORK_TRANSACTION: {
  3604. binder_inner_proc_unlock(proc);
  3605. t = container_of(w, struct binder_transaction, work);
  3606. } break;
  3607. case BINDER_WORK_RETURN_ERROR: {
  3608. struct binder_error *e = container_of(
  3609. w, struct binder_error, work);
  3610. WARN_ON(e->cmd == BR_OK);
  3611. binder_inner_proc_unlock(proc);
  3612. if (put_user(e->cmd, (uint32_t __user *)ptr))
  3613. return -EFAULT;
  3614. cmd = e->cmd;
  3615. e->cmd = BR_OK;
  3616. ptr += sizeof(uint32_t);
  3617. binder_stat_br(proc, thread, cmd);
  3618. } break;
  3619. case BINDER_WORK_TRANSACTION_COMPLETE: {
  3620. binder_inner_proc_unlock(proc);
  3621. cmd = BR_TRANSACTION_COMPLETE;
  3622. if (put_user(cmd, (uint32_t __user *)ptr))
  3623. return -EFAULT;
  3624. ptr += sizeof(uint32_t);
  3625. binder_stat_br(proc, thread, cmd);
  3626. binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
  3627. "%d:%d BR_TRANSACTION_COMPLETE\n",
  3628. proc->pid, thread->pid);
  3629. kfree(w);
  3630. binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
  3631. } break;
  3632. case BINDER_WORK_NODE: {
  3633. struct binder_node *node = container_of(w, struct binder_node, work);
  3634. int strong, weak;
  3635. binder_uintptr_t node_ptr = node->ptr;
  3636. binder_uintptr_t node_cookie = node->cookie;
  3637. int node_debug_id = node->debug_id;
  3638. int has_weak_ref;
  3639. int has_strong_ref;
  3640. void __user *orig_ptr = ptr;
  3641. BUG_ON(proc != node->proc);
  3642. strong = node->internal_strong_refs ||
  3643. node->local_strong_refs;
  3644. weak = !hlist_empty(&node->refs) ||
  3645. node->local_weak_refs ||
  3646. node->tmp_refs || strong;
  3647. has_strong_ref = node->has_strong_ref;
  3648. has_weak_ref = node->has_weak_ref;
  3649. if (weak && !has_weak_ref) {
  3650. node->has_weak_ref = 1;
  3651. node->pending_weak_ref = 1;
  3652. node->local_weak_refs++;
  3653. }
  3654. if (strong && !has_strong_ref) {
  3655. node->has_strong_ref = 1;
  3656. node->pending_strong_ref = 1;
  3657. node->local_strong_refs++;
  3658. }
  3659. if (!strong && has_strong_ref)
  3660. node->has_strong_ref = 0;
  3661. if (!weak && has_weak_ref)
  3662. node->has_weak_ref = 0;
  3663. if (!weak && !strong) {
  3664. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  3665. "%d:%d node %d u%016llx c%016llx deleted\n",
  3666. proc->pid, thread->pid,
  3667. node_debug_id,
  3668. (u64)node_ptr,
  3669. (u64)node_cookie);
  3670. rb_erase(&node->rb_node, &proc->nodes);
  3671. binder_inner_proc_unlock(proc);
  3672. binder_node_lock(node);
  3673. /*
  3674. * Acquire the node lock before freeing the
  3675. * node to serialize with other threads that
  3676. * may have been holding the node lock while
  3677. * decrementing this node (avoids race where
  3678. * this thread frees while the other thread
  3679. * is unlocking the node after the final
  3680. * decrement)
  3681. */
  3682. binder_node_unlock(node);
  3683. binder_free_node(node);
  3684. } else
  3685. binder_inner_proc_unlock(proc);
  3686. if (weak && !has_weak_ref)
  3687. ret = binder_put_node_cmd(
  3688. proc, thread, &ptr, node_ptr,
  3689. node_cookie, node_debug_id,
  3690. BR_INCREFS, "BR_INCREFS");
  3691. if (!ret && strong && !has_strong_ref)
  3692. ret = binder_put_node_cmd(
  3693. proc, thread, &ptr, node_ptr,
  3694. node_cookie, node_debug_id,
  3695. BR_ACQUIRE, "BR_ACQUIRE");
  3696. if (!ret && !strong && has_strong_ref)
  3697. ret = binder_put_node_cmd(
  3698. proc, thread, &ptr, node_ptr,
  3699. node_cookie, node_debug_id,
  3700. BR_RELEASE, "BR_RELEASE");
  3701. if (!ret && !weak && has_weak_ref)
  3702. ret = binder_put_node_cmd(
  3703. proc, thread, &ptr, node_ptr,
  3704. node_cookie, node_debug_id,
  3705. BR_DECREFS, "BR_DECREFS");
  3706. if (orig_ptr == ptr)
  3707. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  3708. "%d:%d node %d u%016llx c%016llx state unchanged\n",
  3709. proc->pid, thread->pid,
  3710. node_debug_id,
  3711. (u64)node_ptr,
  3712. (u64)node_cookie);
  3713. if (ret)
  3714. return ret;
  3715. } break;
  3716. case BINDER_WORK_DEAD_BINDER:
  3717. case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
  3718. case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
  3719. struct binder_ref_death *death;
  3720. uint32_t cmd;
  3721. binder_uintptr_t cookie;
  3722. death = container_of(w, struct binder_ref_death, work);
  3723. if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
  3724. cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
  3725. else
  3726. cmd = BR_DEAD_BINDER;
  3727. cookie = death->cookie;
  3728. binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
  3729. "%d:%d %s %016llx\n",
  3730. proc->pid, thread->pid,
  3731. cmd == BR_DEAD_BINDER ?
  3732. "BR_DEAD_BINDER" :
  3733. "BR_CLEAR_DEATH_NOTIFICATION_DONE",
  3734. (u64)cookie);
  3735. if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
  3736. binder_inner_proc_unlock(proc);
  3737. kfree(death);
  3738. binder_stats_deleted(BINDER_STAT_DEATH);
  3739. } else {
  3740. binder_enqueue_work_ilocked(
  3741. w, &proc->delivered_death);
  3742. binder_inner_proc_unlock(proc);
  3743. }
  3744. if (put_user(cmd, (uint32_t __user *)ptr))
  3745. return -EFAULT;
  3746. ptr += sizeof(uint32_t);
  3747. if (put_user(cookie,
  3748. (binder_uintptr_t __user *)ptr))
  3749. return -EFAULT;
  3750. ptr += sizeof(binder_uintptr_t);
  3751. binder_stat_br(proc, thread, cmd);
  3752. if (cmd == BR_DEAD_BINDER)
  3753. goto done; /* DEAD_BINDER notifications can cause transactions */
  3754. } break;
  3755. }
  3756. if (!t)
  3757. continue;
  3758. BUG_ON(t->buffer == NULL);
  3759. if (t->buffer->target_node) {
  3760. struct binder_node *target_node = t->buffer->target_node;
  3761. tr.target.ptr = target_node->ptr;
  3762. tr.cookie = target_node->cookie;
  3763. t->saved_priority = task_nice(current);
  3764. if (t->priority < target_node->min_priority &&
  3765. !(t->flags & TF_ONE_WAY))
  3766. binder_set_nice(t->priority);
  3767. else if (!(t->flags & TF_ONE_WAY) ||
  3768. t->saved_priority > target_node->min_priority)
  3769. binder_set_nice(target_node->min_priority);
  3770. cmd = BR_TRANSACTION;
  3771. } else {
  3772. tr.target.ptr = 0;
  3773. tr.cookie = 0;
  3774. cmd = BR_REPLY;
  3775. }
  3776. tr.code = t->code;
  3777. tr.flags = t->flags;
  3778. tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
  3779. t_from = binder_get_txn_from(t);
  3780. if (t_from) {
  3781. struct task_struct *sender = t_from->proc->tsk;
  3782. tr.sender_pid = task_tgid_nr_ns(sender,
  3783. task_active_pid_ns(current));
  3784. } else {
  3785. tr.sender_pid = 0;
  3786. }
  3787. tr.data_size = t->buffer->data_size;
  3788. tr.offsets_size = t->buffer->offsets_size;
  3789. tr.data.ptr.buffer = (binder_uintptr_t)
  3790. ((uintptr_t)t->buffer->data +
  3791. binder_alloc_get_user_buffer_offset(&proc->alloc));
  3792. tr.data.ptr.offsets = tr.data.ptr.buffer +
  3793. ALIGN(t->buffer->data_size,
  3794. sizeof(void *));
  3795. if (put_user(cmd, (uint32_t __user *)ptr)) {
  3796. if (t_from)
  3797. binder_thread_dec_tmpref(t_from);
  3798. binder_cleanup_transaction(t, "put_user failed",
  3799. BR_FAILED_REPLY);
  3800. return -EFAULT;
  3801. }
  3802. ptr += sizeof(uint32_t);
  3803. if (copy_to_user(ptr, &tr, sizeof(tr))) {
  3804. if (t_from)
  3805. binder_thread_dec_tmpref(t_from);
  3806. binder_cleanup_transaction(t, "copy_to_user failed",
  3807. BR_FAILED_REPLY);
  3808. return -EFAULT;
  3809. }
  3810. ptr += sizeof(tr);
  3811. trace_binder_transaction_received(t);
  3812. binder_stat_br(proc, thread, cmd);
  3813. binder_debug(BINDER_DEBUG_TRANSACTION,
  3814. "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
  3815. proc->pid, thread->pid,
  3816. (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
  3817. "BR_REPLY",
  3818. t->debug_id, t_from ? t_from->proc->pid : 0,
  3819. t_from ? t_from->pid : 0, cmd,
  3820. t->buffer->data_size, t->buffer->offsets_size,
  3821. (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
  3822. if (t_from)
  3823. binder_thread_dec_tmpref(t_from);
  3824. t->buffer->allow_user_free = 1;
  3825. if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
  3826. binder_inner_proc_lock(thread->proc);
  3827. t->to_parent = thread->transaction_stack;
  3828. t->to_thread = thread;
  3829. thread->transaction_stack = t;
  3830. binder_inner_proc_unlock(thread->proc);
  3831. } else {
  3832. binder_free_transaction(t);
  3833. }
  3834. break;
  3835. }
  3836. done:
  3837. *consumed = ptr - buffer;
  3838. binder_inner_proc_lock(proc);
  3839. if (proc->requested_threads == 0 &&
  3840. list_empty(&thread->proc->waiting_threads) &&
  3841. proc->requested_threads_started < proc->max_threads &&
  3842. (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
  3843. BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
  3844. /*spawn a new thread if we leave this out */) {
  3845. proc->requested_threads++;
  3846. binder_inner_proc_unlock(proc);
  3847. binder_debug(BINDER_DEBUG_THREADS,
  3848. "%d:%d BR_SPAWN_LOOPER\n",
  3849. proc->pid, thread->pid);
  3850. if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
  3851. return -EFAULT;
  3852. binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
  3853. } else
  3854. binder_inner_proc_unlock(proc);
  3855. return 0;
  3856. }
  3857. static void binder_release_work(struct binder_proc *proc,
  3858. struct list_head *list)
  3859. {
  3860. struct binder_work *w;
  3861. while (1) {
  3862. w = binder_dequeue_work_head(proc, list);
  3863. if (!w)
  3864. return;
  3865. switch (w->type) {
  3866. case BINDER_WORK_TRANSACTION: {
  3867. struct binder_transaction *t;
  3868. t = container_of(w, struct binder_transaction, work);
  3869. binder_cleanup_transaction(t, "process died.",
  3870. BR_DEAD_REPLY);
  3871. } break;
  3872. case BINDER_WORK_RETURN_ERROR: {
  3873. struct binder_error *e = container_of(
  3874. w, struct binder_error, work);
  3875. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  3876. "undelivered TRANSACTION_ERROR: %u\n",
  3877. e->cmd);
  3878. } break;
  3879. case BINDER_WORK_TRANSACTION_COMPLETE: {
  3880. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  3881. "undelivered TRANSACTION_COMPLETE\n");
  3882. kfree(w);
  3883. binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
  3884. } break;
  3885. case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
  3886. case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
  3887. struct binder_ref_death *death;
  3888. death = container_of(w, struct binder_ref_death, work);
  3889. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  3890. "undelivered death notification, %016llx\n",
  3891. (u64)death->cookie);
  3892. kfree(death);
  3893. binder_stats_deleted(BINDER_STAT_DEATH);
  3894. } break;
  3895. default:
  3896. pr_err("unexpected work type, %d, not freed\n",
  3897. w->type);
  3898. break;
  3899. }
  3900. }
  3901. }
  3902. static struct binder_thread *binder_get_thread_ilocked(
  3903. struct binder_proc *proc, struct binder_thread *new_thread)
  3904. {
  3905. struct binder_thread *thread = NULL;
  3906. struct rb_node *parent = NULL;
  3907. struct rb_node **p = &proc->threads.rb_node;
  3908. while (*p) {
  3909. parent = *p;
  3910. thread = rb_entry(parent, struct binder_thread, rb_node);
  3911. if (current->pid < thread->pid)
  3912. p = &(*p)->rb_left;
  3913. else if (current->pid > thread->pid)
  3914. p = &(*p)->rb_right;
  3915. else
  3916. return thread;
  3917. }
  3918. if (!new_thread)
  3919. return NULL;
  3920. thread = new_thread;
  3921. binder_stats_created(BINDER_STAT_THREAD);
  3922. thread->proc = proc;
  3923. thread->pid = current->pid;
  3924. atomic_set(&thread->tmp_ref, 0);
  3925. init_waitqueue_head(&thread->wait);
  3926. INIT_LIST_HEAD(&thread->todo);
  3927. rb_link_node(&thread->rb_node, parent, p);
  3928. rb_insert_color(&thread->rb_node, &proc->threads);
  3929. thread->looper_need_return = true;
  3930. thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
  3931. thread->return_error.cmd = BR_OK;
  3932. thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
  3933. thread->reply_error.cmd = BR_OK;
  3934. INIT_LIST_HEAD(&new_thread->waiting_thread_node);
  3935. return thread;
  3936. }
  3937. static struct binder_thread *binder_get_thread(struct binder_proc *proc)
  3938. {
  3939. struct binder_thread *thread;
  3940. struct binder_thread *new_thread;
  3941. binder_inner_proc_lock(proc);
  3942. thread = binder_get_thread_ilocked(proc, NULL);
  3943. binder_inner_proc_unlock(proc);
  3944. if (!thread) {
  3945. new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
  3946. if (new_thread == NULL)
  3947. return NULL;
  3948. binder_inner_proc_lock(proc);
  3949. thread = binder_get_thread_ilocked(proc, new_thread);
  3950. binder_inner_proc_unlock(proc);
  3951. if (thread != new_thread)
  3952. kfree(new_thread);
  3953. }
  3954. return thread;
  3955. }
  3956. static void binder_free_proc(struct binder_proc *proc)
  3957. {
  3958. BUG_ON(!list_empty(&proc->todo));
  3959. BUG_ON(!list_empty(&proc->delivered_death));
  3960. binder_alloc_deferred_release(&proc->alloc);
  3961. put_task_struct(proc->tsk);
  3962. binder_stats_deleted(BINDER_STAT_PROC);
  3963. kfree(proc);
  3964. }
  3965. static void binder_free_thread(struct binder_thread *thread)
  3966. {
  3967. BUG_ON(!list_empty(&thread->todo));
  3968. binder_stats_deleted(BINDER_STAT_THREAD);
  3969. binder_proc_dec_tmpref(thread->proc);
  3970. kfree(thread);
  3971. }
  3972. static int binder_thread_release(struct binder_proc *proc,
  3973. struct binder_thread *thread)
  3974. {
  3975. struct binder_transaction *t;
  3976. struct binder_transaction *send_reply = NULL;
  3977. int active_transactions = 0;
  3978. struct binder_transaction *last_t = NULL;
  3979. binder_inner_proc_lock(thread->proc);
  3980. /*
  3981. * take a ref on the proc so it survives
  3982. * after we remove this thread from proc->threads.
  3983. * The corresponding dec is when we actually
  3984. * free the thread in binder_free_thread()
  3985. */
  3986. proc->tmp_ref++;
  3987. /*
  3988. * take a ref on this thread to ensure it
  3989. * survives while we are releasing it
  3990. */
  3991. atomic_inc(&thread->tmp_ref);
  3992. rb_erase(&thread->rb_node, &proc->threads);
  3993. t = thread->transaction_stack;
  3994. if (t) {
  3995. spin_lock(&t->lock);
  3996. if (t->to_thread == thread)
  3997. send_reply = t;
  3998. }
  3999. thread->is_dead = true;
  4000. while (t) {
  4001. last_t = t;
  4002. active_transactions++;
  4003. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  4004. "release %d:%d transaction %d %s, still active\n",
  4005. proc->pid, thread->pid,
  4006. t->debug_id,
  4007. (t->to_thread == thread) ? "in" : "out");
  4008. if (t->to_thread == thread) {
  4009. t->to_proc = NULL;
  4010. t->to_thread = NULL;
  4011. if (t->buffer) {
  4012. t->buffer->transaction = NULL;
  4013. t->buffer = NULL;
  4014. }
  4015. t = t->to_parent;
  4016. } else if (t->from == thread) {
  4017. t->from = NULL;
  4018. t = t->from_parent;
  4019. } else
  4020. BUG();
  4021. spin_unlock(&last_t->lock);
  4022. if (t)
  4023. spin_lock(&t->lock);
  4024. }
  4025. /*
  4026. * If this thread used poll, make sure we remove the waitqueue
  4027. * from any epoll data structures holding it with POLLFREE.
  4028. * waitqueue_active() is safe to use here because we're holding
  4029. * the inner lock.
  4030. */
  4031. if ((thread->looper & BINDER_LOOPER_STATE_POLL) &&
  4032. waitqueue_active(&thread->wait)) {
  4033. wake_up_poll(&thread->wait, EPOLLHUP | POLLFREE);
  4034. }
  4035. binder_inner_proc_unlock(thread->proc);
  4036. /*
  4037. * This is needed to avoid races between wake_up_poll() above and
  4038. * and ep_remove_waitqueue() called for other reasons (eg the epoll file
  4039. * descriptor being closed); ep_remove_waitqueue() holds an RCU read
  4040. * lock, so we can be sure it's done after calling synchronize_rcu().
  4041. */
  4042. if (thread->looper & BINDER_LOOPER_STATE_POLL)
  4043. synchronize_rcu();
  4044. if (send_reply)
  4045. binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
  4046. binder_release_work(proc, &thread->todo);
  4047. binder_thread_dec_tmpref(thread);
  4048. return active_transactions;
  4049. }
  4050. static __poll_t binder_poll(struct file *filp,
  4051. struct poll_table_struct *wait)
  4052. {
  4053. struct binder_proc *proc = filp->private_data;
  4054. struct binder_thread *thread = NULL;
  4055. bool wait_for_proc_work;
  4056. thread = binder_get_thread(proc);
  4057. if (!thread)
  4058. return POLLERR;
  4059. binder_inner_proc_lock(thread->proc);
  4060. thread->looper |= BINDER_LOOPER_STATE_POLL;
  4061. wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
  4062. binder_inner_proc_unlock(thread->proc);
  4063. poll_wait(filp, &thread->wait, wait);
  4064. if (binder_has_work(thread, wait_for_proc_work))
  4065. return EPOLLIN;
  4066. return 0;
  4067. }
  4068. static int binder_ioctl_write_read(struct file *filp,
  4069. unsigned int cmd, unsigned long arg,
  4070. struct binder_thread *thread)
  4071. {
  4072. int ret = 0;
  4073. struct binder_proc *proc = filp->private_data;
  4074. unsigned int size = _IOC_SIZE(cmd);
  4075. void __user *ubuf = (void __user *)arg;
  4076. struct binder_write_read bwr;
  4077. if (size != sizeof(struct binder_write_read)) {
  4078. ret = -EINVAL;
  4079. goto out;
  4080. }
  4081. if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
  4082. ret = -EFAULT;
  4083. goto out;
  4084. }
  4085. binder_debug(BINDER_DEBUG_READ_WRITE,
  4086. "%d:%d write %lld at %016llx, read %lld at %016llx\n",
  4087. proc->pid, thread->pid,
  4088. (u64)bwr.write_size, (u64)bwr.write_buffer,
  4089. (u64)bwr.read_size, (u64)bwr.read_buffer);
  4090. if (bwr.write_size > 0) {
  4091. ret = binder_thread_write(proc, thread,
  4092. bwr.write_buffer,
  4093. bwr.write_size,
  4094. &bwr.write_consumed);
  4095. trace_binder_write_done(ret);
  4096. if (ret < 0) {
  4097. bwr.read_consumed = 0;
  4098. if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
  4099. ret = -EFAULT;
  4100. goto out;
  4101. }
  4102. }
  4103. if (bwr.read_size > 0) {
  4104. ret = binder_thread_read(proc, thread, bwr.read_buffer,
  4105. bwr.read_size,
  4106. &bwr.read_consumed,
  4107. filp->f_flags & O_NONBLOCK);
  4108. trace_binder_read_done(ret);
  4109. binder_inner_proc_lock(proc);
  4110. if (!binder_worklist_empty_ilocked(&proc->todo))
  4111. binder_wakeup_proc_ilocked(proc);
  4112. binder_inner_proc_unlock(proc);
  4113. if (ret < 0) {
  4114. if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
  4115. ret = -EFAULT;
  4116. goto out;
  4117. }
  4118. }
  4119. binder_debug(BINDER_DEBUG_READ_WRITE,
  4120. "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
  4121. proc->pid, thread->pid,
  4122. (u64)bwr.write_consumed, (u64)bwr.write_size,
  4123. (u64)bwr.read_consumed, (u64)bwr.read_size);
  4124. if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
  4125. ret = -EFAULT;
  4126. goto out;
  4127. }
  4128. out:
  4129. return ret;
  4130. }
  4131. static int binder_ioctl_set_ctx_mgr(struct file *filp)
  4132. {
  4133. int ret = 0;
  4134. struct binder_proc *proc = filp->private_data;
  4135. struct binder_context *context = proc->context;
  4136. struct binder_node *new_node;
  4137. kuid_t curr_euid = current_euid();
  4138. mutex_lock(&context->context_mgr_node_lock);
  4139. if (context->binder_context_mgr_node) {
  4140. pr_err("BINDER_SET_CONTEXT_MGR already set\n");
  4141. ret = -EBUSY;
  4142. goto out;
  4143. }
  4144. ret = security_binder_set_context_mgr(proc->tsk);
  4145. if (ret < 0)
  4146. goto out;
  4147. if (uid_valid(context->binder_context_mgr_uid)) {
  4148. if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
  4149. pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
  4150. from_kuid(&init_user_ns, curr_euid),
  4151. from_kuid(&init_user_ns,
  4152. context->binder_context_mgr_uid));
  4153. ret = -EPERM;
  4154. goto out;
  4155. }
  4156. } else {
  4157. context->binder_context_mgr_uid = curr_euid;
  4158. }
  4159. new_node = binder_new_node(proc, NULL);
  4160. if (!new_node) {
  4161. ret = -ENOMEM;
  4162. goto out;
  4163. }
  4164. binder_node_lock(new_node);
  4165. new_node->local_weak_refs++;
  4166. new_node->local_strong_refs++;
  4167. new_node->has_strong_ref = 1;
  4168. new_node->has_weak_ref = 1;
  4169. context->binder_context_mgr_node = new_node;
  4170. binder_node_unlock(new_node);
  4171. binder_put_node(new_node);
  4172. out:
  4173. mutex_unlock(&context->context_mgr_node_lock);
  4174. return ret;
  4175. }
  4176. static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
  4177. struct binder_node_debug_info *info)
  4178. {
  4179. struct rb_node *n;
  4180. binder_uintptr_t ptr = info->ptr;
  4181. memset(info, 0, sizeof(*info));
  4182. binder_inner_proc_lock(proc);
  4183. for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
  4184. struct binder_node *node = rb_entry(n, struct binder_node,
  4185. rb_node);
  4186. if (node->ptr > ptr) {
  4187. info->ptr = node->ptr;
  4188. info->cookie = node->cookie;
  4189. info->has_strong_ref = node->has_strong_ref;
  4190. info->has_weak_ref = node->has_weak_ref;
  4191. break;
  4192. }
  4193. }
  4194. binder_inner_proc_unlock(proc);
  4195. return 0;
  4196. }
  4197. static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  4198. {
  4199. int ret;
  4200. struct binder_proc *proc = filp->private_data;
  4201. struct binder_thread *thread;
  4202. unsigned int size = _IOC_SIZE(cmd);
  4203. void __user *ubuf = (void __user *)arg;
  4204. /*pr_info("binder_ioctl: %d:%d %x %lx\n",
  4205. proc->pid, current->pid, cmd, arg);*/
  4206. binder_selftest_alloc(&proc->alloc);
  4207. trace_binder_ioctl(cmd, arg);
  4208. ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
  4209. if (ret)
  4210. goto err_unlocked;
  4211. thread = binder_get_thread(proc);
  4212. if (thread == NULL) {
  4213. ret = -ENOMEM;
  4214. goto err;
  4215. }
  4216. switch (cmd) {
  4217. case BINDER_WRITE_READ:
  4218. ret = binder_ioctl_write_read(filp, cmd, arg, thread);
  4219. if (ret)
  4220. goto err;
  4221. break;
  4222. case BINDER_SET_MAX_THREADS: {
  4223. int max_threads;
  4224. if (copy_from_user(&max_threads, ubuf,
  4225. sizeof(max_threads))) {
  4226. ret = -EINVAL;
  4227. goto err;
  4228. }
  4229. binder_inner_proc_lock(proc);
  4230. proc->max_threads = max_threads;
  4231. binder_inner_proc_unlock(proc);
  4232. break;
  4233. }
  4234. case BINDER_SET_CONTEXT_MGR:
  4235. ret = binder_ioctl_set_ctx_mgr(filp);
  4236. if (ret)
  4237. goto err;
  4238. break;
  4239. case BINDER_THREAD_EXIT:
  4240. binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
  4241. proc->pid, thread->pid);
  4242. binder_thread_release(proc, thread);
  4243. thread = NULL;
  4244. break;
  4245. case BINDER_VERSION: {
  4246. struct binder_version __user *ver = ubuf;
  4247. if (size != sizeof(struct binder_version)) {
  4248. ret = -EINVAL;
  4249. goto err;
  4250. }
  4251. if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
  4252. &ver->protocol_version)) {
  4253. ret = -EINVAL;
  4254. goto err;
  4255. }
  4256. break;
  4257. }
  4258. case BINDER_GET_NODE_DEBUG_INFO: {
  4259. struct binder_node_debug_info info;
  4260. if (copy_from_user(&info, ubuf, sizeof(info))) {
  4261. ret = -EFAULT;
  4262. goto err;
  4263. }
  4264. ret = binder_ioctl_get_node_debug_info(proc, &info);
  4265. if (ret < 0)
  4266. goto err;
  4267. if (copy_to_user(ubuf, &info, sizeof(info))) {
  4268. ret = -EFAULT;
  4269. goto err;
  4270. }
  4271. break;
  4272. }
  4273. default:
  4274. ret = -EINVAL;
  4275. goto err;
  4276. }
  4277. ret = 0;
  4278. err:
  4279. if (thread)
  4280. thread->looper_need_return = false;
  4281. wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
  4282. if (ret && ret != -ERESTARTSYS)
  4283. pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
  4284. err_unlocked:
  4285. trace_binder_ioctl_done(ret);
  4286. return ret;
  4287. }
  4288. static void binder_vma_open(struct vm_area_struct *vma)
  4289. {
  4290. struct binder_proc *proc = vma->vm_private_data;
  4291. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  4292. "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
  4293. proc->pid, vma->vm_start, vma->vm_end,
  4294. (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
  4295. (unsigned long)pgprot_val(vma->vm_page_prot));
  4296. }
  4297. static void binder_vma_close(struct vm_area_struct *vma)
  4298. {
  4299. struct binder_proc *proc = vma->vm_private_data;
  4300. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  4301. "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
  4302. proc->pid, vma->vm_start, vma->vm_end,
  4303. (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
  4304. (unsigned long)pgprot_val(vma->vm_page_prot));
  4305. binder_alloc_vma_close(&proc->alloc);
  4306. binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
  4307. }
  4308. static vm_fault_t binder_vm_fault(struct vm_fault *vmf)
  4309. {
  4310. return VM_FAULT_SIGBUS;
  4311. }
  4312. static const struct vm_operations_struct binder_vm_ops = {
  4313. .open = binder_vma_open,
  4314. .close = binder_vma_close,
  4315. .fault = binder_vm_fault,
  4316. };
  4317. static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
  4318. {
  4319. int ret;
  4320. struct binder_proc *proc = filp->private_data;
  4321. const char *failure_string;
  4322. if (proc->tsk != current->group_leader)
  4323. return -EINVAL;
  4324. if ((vma->vm_end - vma->vm_start) > SZ_4M)
  4325. vma->vm_end = vma->vm_start + SZ_4M;
  4326. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  4327. "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
  4328. __func__, proc->pid, vma->vm_start, vma->vm_end,
  4329. (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
  4330. (unsigned long)pgprot_val(vma->vm_page_prot));
  4331. if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
  4332. ret = -EPERM;
  4333. failure_string = "bad vm_flags";
  4334. goto err_bad_arg;
  4335. }
  4336. vma->vm_flags |= VM_DONTCOPY | VM_MIXEDMAP;
  4337. vma->vm_flags &= ~VM_MAYWRITE;
  4338. vma->vm_ops = &binder_vm_ops;
  4339. vma->vm_private_data = proc;
  4340. ret = binder_alloc_mmap_handler(&proc->alloc, vma);
  4341. if (ret)
  4342. return ret;
  4343. mutex_lock(&proc->files_lock);
  4344. proc->files = get_files_struct(current);
  4345. mutex_unlock(&proc->files_lock);
  4346. return 0;
  4347. err_bad_arg:
  4348. pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
  4349. proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
  4350. return ret;
  4351. }
  4352. static int binder_open(struct inode *nodp, struct file *filp)
  4353. {
  4354. struct binder_proc *proc;
  4355. struct binder_device *binder_dev;
  4356. binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__,
  4357. current->group_leader->pid, current->pid);
  4358. proc = kzalloc(sizeof(*proc), GFP_KERNEL);
  4359. if (proc == NULL)
  4360. return -ENOMEM;
  4361. spin_lock_init(&proc->inner_lock);
  4362. spin_lock_init(&proc->outer_lock);
  4363. get_task_struct(current->group_leader);
  4364. proc->tsk = current->group_leader;
  4365. mutex_init(&proc->files_lock);
  4366. INIT_LIST_HEAD(&proc->todo);
  4367. proc->default_priority = task_nice(current);
  4368. binder_dev = container_of(filp->private_data, struct binder_device,
  4369. miscdev);
  4370. proc->context = &binder_dev->context;
  4371. binder_alloc_init(&proc->alloc);
  4372. binder_stats_created(BINDER_STAT_PROC);
  4373. proc->pid = current->group_leader->pid;
  4374. INIT_LIST_HEAD(&proc->delivered_death);
  4375. INIT_LIST_HEAD(&proc->waiting_threads);
  4376. filp->private_data = proc;
  4377. mutex_lock(&binder_procs_lock);
  4378. hlist_add_head(&proc->proc_node, &binder_procs);
  4379. mutex_unlock(&binder_procs_lock);
  4380. if (binder_debugfs_dir_entry_proc) {
  4381. char strbuf[11];
  4382. snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
  4383. /*
  4384. * proc debug entries are shared between contexts, so
  4385. * this will fail if the process tries to open the driver
  4386. * again with a different context. The priting code will
  4387. * anyway print all contexts that a given PID has, so this
  4388. * is not a problem.
  4389. */
  4390. proc->debugfs_entry = debugfs_create_file(strbuf, 0444,
  4391. binder_debugfs_dir_entry_proc,
  4392. (void *)(unsigned long)proc->pid,
  4393. &binder_proc_fops);
  4394. }
  4395. return 0;
  4396. }
  4397. static int binder_flush(struct file *filp, fl_owner_t id)
  4398. {
  4399. struct binder_proc *proc = filp->private_data;
  4400. binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
  4401. return 0;
  4402. }
  4403. static void binder_deferred_flush(struct binder_proc *proc)
  4404. {
  4405. struct rb_node *n;
  4406. int wake_count = 0;
  4407. binder_inner_proc_lock(proc);
  4408. for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
  4409. struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
  4410. thread->looper_need_return = true;
  4411. if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
  4412. wake_up_interruptible(&thread->wait);
  4413. wake_count++;
  4414. }
  4415. }
  4416. binder_inner_proc_unlock(proc);
  4417. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  4418. "binder_flush: %d woke %d threads\n", proc->pid,
  4419. wake_count);
  4420. }
  4421. static int binder_release(struct inode *nodp, struct file *filp)
  4422. {
  4423. struct binder_proc *proc = filp->private_data;
  4424. debugfs_remove(proc->debugfs_entry);
  4425. binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
  4426. return 0;
  4427. }
  4428. static int binder_node_release(struct binder_node *node, int refs)
  4429. {
  4430. struct binder_ref *ref;
  4431. int death = 0;
  4432. struct binder_proc *proc = node->proc;
  4433. binder_release_work(proc, &node->async_todo);
  4434. binder_node_lock(node);
  4435. binder_inner_proc_lock(proc);
  4436. binder_dequeue_work_ilocked(&node->work);
  4437. /*
  4438. * The caller must have taken a temporary ref on the node,
  4439. */
  4440. BUG_ON(!node->tmp_refs);
  4441. if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
  4442. binder_inner_proc_unlock(proc);
  4443. binder_node_unlock(node);
  4444. binder_free_node(node);
  4445. return refs;
  4446. }
  4447. node->proc = NULL;
  4448. node->local_strong_refs = 0;
  4449. node->local_weak_refs = 0;
  4450. binder_inner_proc_unlock(proc);
  4451. spin_lock(&binder_dead_nodes_lock);
  4452. hlist_add_head(&node->dead_node, &binder_dead_nodes);
  4453. spin_unlock(&binder_dead_nodes_lock);
  4454. hlist_for_each_entry(ref, &node->refs, node_entry) {
  4455. refs++;
  4456. /*
  4457. * Need the node lock to synchronize
  4458. * with new notification requests and the
  4459. * inner lock to synchronize with queued
  4460. * death notifications.
  4461. */
  4462. binder_inner_proc_lock(ref->proc);
  4463. if (!ref->death) {
  4464. binder_inner_proc_unlock(ref->proc);
  4465. continue;
  4466. }
  4467. death++;
  4468. BUG_ON(!list_empty(&ref->death->work.entry));
  4469. ref->death->work.type = BINDER_WORK_DEAD_BINDER;
  4470. binder_enqueue_work_ilocked(&ref->death->work,
  4471. &ref->proc->todo);
  4472. binder_wakeup_proc_ilocked(ref->proc);
  4473. binder_inner_proc_unlock(ref->proc);
  4474. }
  4475. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  4476. "node %d now dead, refs %d, death %d\n",
  4477. node->debug_id, refs, death);
  4478. binder_node_unlock(node);
  4479. binder_put_node(node);
  4480. return refs;
  4481. }
  4482. static void binder_deferred_release(struct binder_proc *proc)
  4483. {
  4484. struct binder_context *context = proc->context;
  4485. struct rb_node *n;
  4486. int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
  4487. BUG_ON(proc->files);
  4488. mutex_lock(&binder_procs_lock);
  4489. hlist_del(&proc->proc_node);
  4490. mutex_unlock(&binder_procs_lock);
  4491. mutex_lock(&context->context_mgr_node_lock);
  4492. if (context->binder_context_mgr_node &&
  4493. context->binder_context_mgr_node->proc == proc) {
  4494. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  4495. "%s: %d context_mgr_node gone\n",
  4496. __func__, proc->pid);
  4497. context->binder_context_mgr_node = NULL;
  4498. }
  4499. mutex_unlock(&context->context_mgr_node_lock);
  4500. binder_inner_proc_lock(proc);
  4501. /*
  4502. * Make sure proc stays alive after we
  4503. * remove all the threads
  4504. */
  4505. proc->tmp_ref++;
  4506. proc->is_dead = true;
  4507. threads = 0;
  4508. active_transactions = 0;
  4509. while ((n = rb_first(&proc->threads))) {
  4510. struct binder_thread *thread;
  4511. thread = rb_entry(n, struct binder_thread, rb_node);
  4512. binder_inner_proc_unlock(proc);
  4513. threads++;
  4514. active_transactions += binder_thread_release(proc, thread);
  4515. binder_inner_proc_lock(proc);
  4516. }
  4517. nodes = 0;
  4518. incoming_refs = 0;
  4519. while ((n = rb_first(&proc->nodes))) {
  4520. struct binder_node *node;
  4521. node = rb_entry(n, struct binder_node, rb_node);
  4522. nodes++;
  4523. /*
  4524. * take a temporary ref on the node before
  4525. * calling binder_node_release() which will either
  4526. * kfree() the node or call binder_put_node()
  4527. */
  4528. binder_inc_node_tmpref_ilocked(node);
  4529. rb_erase(&node->rb_node, &proc->nodes);
  4530. binder_inner_proc_unlock(proc);
  4531. incoming_refs = binder_node_release(node, incoming_refs);
  4532. binder_inner_proc_lock(proc);
  4533. }
  4534. binder_inner_proc_unlock(proc);
  4535. outgoing_refs = 0;
  4536. binder_proc_lock(proc);
  4537. while ((n = rb_first(&proc->refs_by_desc))) {
  4538. struct binder_ref *ref;
  4539. ref = rb_entry(n, struct binder_ref, rb_node_desc);
  4540. outgoing_refs++;
  4541. binder_cleanup_ref_olocked(ref);
  4542. binder_proc_unlock(proc);
  4543. binder_free_ref(ref);
  4544. binder_proc_lock(proc);
  4545. }
  4546. binder_proc_unlock(proc);
  4547. binder_release_work(proc, &proc->todo);
  4548. binder_release_work(proc, &proc->delivered_death);
  4549. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  4550. "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
  4551. __func__, proc->pid, threads, nodes, incoming_refs,
  4552. outgoing_refs, active_transactions);
  4553. binder_proc_dec_tmpref(proc);
  4554. }
  4555. static void binder_deferred_func(struct work_struct *work)
  4556. {
  4557. struct binder_proc *proc;
  4558. struct files_struct *files;
  4559. int defer;
  4560. do {
  4561. mutex_lock(&binder_deferred_lock);
  4562. if (!hlist_empty(&binder_deferred_list)) {
  4563. proc = hlist_entry(binder_deferred_list.first,
  4564. struct binder_proc, deferred_work_node);
  4565. hlist_del_init(&proc->deferred_work_node);
  4566. defer = proc->deferred_work;
  4567. proc->deferred_work = 0;
  4568. } else {
  4569. proc = NULL;
  4570. defer = 0;
  4571. }
  4572. mutex_unlock(&binder_deferred_lock);
  4573. files = NULL;
  4574. if (defer & BINDER_DEFERRED_PUT_FILES) {
  4575. mutex_lock(&proc->files_lock);
  4576. files = proc->files;
  4577. if (files)
  4578. proc->files = NULL;
  4579. mutex_unlock(&proc->files_lock);
  4580. }
  4581. if (defer & BINDER_DEFERRED_FLUSH)
  4582. binder_deferred_flush(proc);
  4583. if (defer & BINDER_DEFERRED_RELEASE)
  4584. binder_deferred_release(proc); /* frees proc */
  4585. if (files)
  4586. put_files_struct(files);
  4587. } while (proc);
  4588. }
  4589. static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
  4590. static void
  4591. binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
  4592. {
  4593. mutex_lock(&binder_deferred_lock);
  4594. proc->deferred_work |= defer;
  4595. if (hlist_unhashed(&proc->deferred_work_node)) {
  4596. hlist_add_head(&proc->deferred_work_node,
  4597. &binder_deferred_list);
  4598. schedule_work(&binder_deferred_work);
  4599. }
  4600. mutex_unlock(&binder_deferred_lock);
  4601. }
  4602. static void print_binder_transaction_ilocked(struct seq_file *m,
  4603. struct binder_proc *proc,
  4604. const char *prefix,
  4605. struct binder_transaction *t)
  4606. {
  4607. struct binder_proc *to_proc;
  4608. struct binder_buffer *buffer = t->buffer;
  4609. spin_lock(&t->lock);
  4610. to_proc = t->to_proc;
  4611. seq_printf(m,
  4612. "%s %d: %pK from %d:%d to %d:%d code %x flags %x pri %ld r%d",
  4613. prefix, t->debug_id, t,
  4614. t->from ? t->from->proc->pid : 0,
  4615. t->from ? t->from->pid : 0,
  4616. to_proc ? to_proc->pid : 0,
  4617. t->to_thread ? t->to_thread->pid : 0,
  4618. t->code, t->flags, t->priority, t->need_reply);
  4619. spin_unlock(&t->lock);
  4620. if (proc != to_proc) {
  4621. /*
  4622. * Can only safely deref buffer if we are holding the
  4623. * correct proc inner lock for this node
  4624. */
  4625. seq_puts(m, "\n");
  4626. return;
  4627. }
  4628. if (buffer == NULL) {
  4629. seq_puts(m, " buffer free\n");
  4630. return;
  4631. }
  4632. if (buffer->target_node)
  4633. seq_printf(m, " node %d", buffer->target_node->debug_id);
  4634. seq_printf(m, " size %zd:%zd data %pK\n",
  4635. buffer->data_size, buffer->offsets_size,
  4636. buffer->data);
  4637. }
  4638. static void print_binder_work_ilocked(struct seq_file *m,
  4639. struct binder_proc *proc,
  4640. const char *prefix,
  4641. const char *transaction_prefix,
  4642. struct binder_work *w)
  4643. {
  4644. struct binder_node *node;
  4645. struct binder_transaction *t;
  4646. switch (w->type) {
  4647. case BINDER_WORK_TRANSACTION:
  4648. t = container_of(w, struct binder_transaction, work);
  4649. print_binder_transaction_ilocked(
  4650. m, proc, transaction_prefix, t);
  4651. break;
  4652. case BINDER_WORK_RETURN_ERROR: {
  4653. struct binder_error *e = container_of(
  4654. w, struct binder_error, work);
  4655. seq_printf(m, "%stransaction error: %u\n",
  4656. prefix, e->cmd);
  4657. } break;
  4658. case BINDER_WORK_TRANSACTION_COMPLETE:
  4659. seq_printf(m, "%stransaction complete\n", prefix);
  4660. break;
  4661. case BINDER_WORK_NODE:
  4662. node = container_of(w, struct binder_node, work);
  4663. seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
  4664. prefix, node->debug_id,
  4665. (u64)node->ptr, (u64)node->cookie);
  4666. break;
  4667. case BINDER_WORK_DEAD_BINDER:
  4668. seq_printf(m, "%shas dead binder\n", prefix);
  4669. break;
  4670. case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
  4671. seq_printf(m, "%shas cleared dead binder\n", prefix);
  4672. break;
  4673. case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
  4674. seq_printf(m, "%shas cleared death notification\n", prefix);
  4675. break;
  4676. default:
  4677. seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
  4678. break;
  4679. }
  4680. }
  4681. static void print_binder_thread_ilocked(struct seq_file *m,
  4682. struct binder_thread *thread,
  4683. int print_always)
  4684. {
  4685. struct binder_transaction *t;
  4686. struct binder_work *w;
  4687. size_t start_pos = m->count;
  4688. size_t header_pos;
  4689. seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
  4690. thread->pid, thread->looper,
  4691. thread->looper_need_return,
  4692. atomic_read(&thread->tmp_ref));
  4693. header_pos = m->count;
  4694. t = thread->transaction_stack;
  4695. while (t) {
  4696. if (t->from == thread) {
  4697. print_binder_transaction_ilocked(m, thread->proc,
  4698. " outgoing transaction", t);
  4699. t = t->from_parent;
  4700. } else if (t->to_thread == thread) {
  4701. print_binder_transaction_ilocked(m, thread->proc,
  4702. " incoming transaction", t);
  4703. t = t->to_parent;
  4704. } else {
  4705. print_binder_transaction_ilocked(m, thread->proc,
  4706. " bad transaction", t);
  4707. t = NULL;
  4708. }
  4709. }
  4710. list_for_each_entry(w, &thread->todo, entry) {
  4711. print_binder_work_ilocked(m, thread->proc, " ",
  4712. " pending transaction", w);
  4713. }
  4714. if (!print_always && m->count == header_pos)
  4715. m->count = start_pos;
  4716. }
  4717. static void print_binder_node_nilocked(struct seq_file *m,
  4718. struct binder_node *node)
  4719. {
  4720. struct binder_ref *ref;
  4721. struct binder_work *w;
  4722. int count;
  4723. count = 0;
  4724. hlist_for_each_entry(ref, &node->refs, node_entry)
  4725. count++;
  4726. seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d tr %d",
  4727. node->debug_id, (u64)node->ptr, (u64)node->cookie,
  4728. node->has_strong_ref, node->has_weak_ref,
  4729. node->local_strong_refs, node->local_weak_refs,
  4730. node->internal_strong_refs, count, node->tmp_refs);
  4731. if (count) {
  4732. seq_puts(m, " proc");
  4733. hlist_for_each_entry(ref, &node->refs, node_entry)
  4734. seq_printf(m, " %d", ref->proc->pid);
  4735. }
  4736. seq_puts(m, "\n");
  4737. if (node->proc) {
  4738. list_for_each_entry(w, &node->async_todo, entry)
  4739. print_binder_work_ilocked(m, node->proc, " ",
  4740. " pending async transaction", w);
  4741. }
  4742. }
  4743. static void print_binder_ref_olocked(struct seq_file *m,
  4744. struct binder_ref *ref)
  4745. {
  4746. binder_node_lock(ref->node);
  4747. seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
  4748. ref->data.debug_id, ref->data.desc,
  4749. ref->node->proc ? "" : "dead ",
  4750. ref->node->debug_id, ref->data.strong,
  4751. ref->data.weak, ref->death);
  4752. binder_node_unlock(ref->node);
  4753. }
  4754. static void print_binder_proc(struct seq_file *m,
  4755. struct binder_proc *proc, int print_all)
  4756. {
  4757. struct binder_work *w;
  4758. struct rb_node *n;
  4759. size_t start_pos = m->count;
  4760. size_t header_pos;
  4761. struct binder_node *last_node = NULL;
  4762. seq_printf(m, "proc %d\n", proc->pid);
  4763. seq_printf(m, "context %s\n", proc->context->name);
  4764. header_pos = m->count;
  4765. binder_inner_proc_lock(proc);
  4766. for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
  4767. print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
  4768. rb_node), print_all);
  4769. for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
  4770. struct binder_node *node = rb_entry(n, struct binder_node,
  4771. rb_node);
  4772. /*
  4773. * take a temporary reference on the node so it
  4774. * survives and isn't removed from the tree
  4775. * while we print it.
  4776. */
  4777. binder_inc_node_tmpref_ilocked(node);
  4778. /* Need to drop inner lock to take node lock */
  4779. binder_inner_proc_unlock(proc);
  4780. if (last_node)
  4781. binder_put_node(last_node);
  4782. binder_node_inner_lock(node);
  4783. print_binder_node_nilocked(m, node);
  4784. binder_node_inner_unlock(node);
  4785. last_node = node;
  4786. binder_inner_proc_lock(proc);
  4787. }
  4788. binder_inner_proc_unlock(proc);
  4789. if (last_node)
  4790. binder_put_node(last_node);
  4791. if (print_all) {
  4792. binder_proc_lock(proc);
  4793. for (n = rb_first(&proc->refs_by_desc);
  4794. n != NULL;
  4795. n = rb_next(n))
  4796. print_binder_ref_olocked(m, rb_entry(n,
  4797. struct binder_ref,
  4798. rb_node_desc));
  4799. binder_proc_unlock(proc);
  4800. }
  4801. binder_alloc_print_allocated(m, &proc->alloc);
  4802. binder_inner_proc_lock(proc);
  4803. list_for_each_entry(w, &proc->todo, entry)
  4804. print_binder_work_ilocked(m, proc, " ",
  4805. " pending transaction", w);
  4806. list_for_each_entry(w, &proc->delivered_death, entry) {
  4807. seq_puts(m, " has delivered dead binder\n");
  4808. break;
  4809. }
  4810. binder_inner_proc_unlock(proc);
  4811. if (!print_all && m->count == header_pos)
  4812. m->count = start_pos;
  4813. }
  4814. static const char * const binder_return_strings[] = {
  4815. "BR_ERROR",
  4816. "BR_OK",
  4817. "BR_TRANSACTION",
  4818. "BR_REPLY",
  4819. "BR_ACQUIRE_RESULT",
  4820. "BR_DEAD_REPLY",
  4821. "BR_TRANSACTION_COMPLETE",
  4822. "BR_INCREFS",
  4823. "BR_ACQUIRE",
  4824. "BR_RELEASE",
  4825. "BR_DECREFS",
  4826. "BR_ATTEMPT_ACQUIRE",
  4827. "BR_NOOP",
  4828. "BR_SPAWN_LOOPER",
  4829. "BR_FINISHED",
  4830. "BR_DEAD_BINDER",
  4831. "BR_CLEAR_DEATH_NOTIFICATION_DONE",
  4832. "BR_FAILED_REPLY"
  4833. };
  4834. static const char * const binder_command_strings[] = {
  4835. "BC_TRANSACTION",
  4836. "BC_REPLY",
  4837. "BC_ACQUIRE_RESULT",
  4838. "BC_FREE_BUFFER",
  4839. "BC_INCREFS",
  4840. "BC_ACQUIRE",
  4841. "BC_RELEASE",
  4842. "BC_DECREFS",
  4843. "BC_INCREFS_DONE",
  4844. "BC_ACQUIRE_DONE",
  4845. "BC_ATTEMPT_ACQUIRE",
  4846. "BC_REGISTER_LOOPER",
  4847. "BC_ENTER_LOOPER",
  4848. "BC_EXIT_LOOPER",
  4849. "BC_REQUEST_DEATH_NOTIFICATION",
  4850. "BC_CLEAR_DEATH_NOTIFICATION",
  4851. "BC_DEAD_BINDER_DONE",
  4852. "BC_TRANSACTION_SG",
  4853. "BC_REPLY_SG",
  4854. };
  4855. static const char * const binder_objstat_strings[] = {
  4856. "proc",
  4857. "thread",
  4858. "node",
  4859. "ref",
  4860. "death",
  4861. "transaction",
  4862. "transaction_complete"
  4863. };
  4864. static void print_binder_stats(struct seq_file *m, const char *prefix,
  4865. struct binder_stats *stats)
  4866. {
  4867. int i;
  4868. BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
  4869. ARRAY_SIZE(binder_command_strings));
  4870. for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
  4871. int temp = atomic_read(&stats->bc[i]);
  4872. if (temp)
  4873. seq_printf(m, "%s%s: %d\n", prefix,
  4874. binder_command_strings[i], temp);
  4875. }
  4876. BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
  4877. ARRAY_SIZE(binder_return_strings));
  4878. for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
  4879. int temp = atomic_read(&stats->br[i]);
  4880. if (temp)
  4881. seq_printf(m, "%s%s: %d\n", prefix,
  4882. binder_return_strings[i], temp);
  4883. }
  4884. BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
  4885. ARRAY_SIZE(binder_objstat_strings));
  4886. BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
  4887. ARRAY_SIZE(stats->obj_deleted));
  4888. for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
  4889. int created = atomic_read(&stats->obj_created[i]);
  4890. int deleted = atomic_read(&stats->obj_deleted[i]);
  4891. if (created || deleted)
  4892. seq_printf(m, "%s%s: active %d total %d\n",
  4893. prefix,
  4894. binder_objstat_strings[i],
  4895. created - deleted,
  4896. created);
  4897. }
  4898. }
  4899. static void print_binder_proc_stats(struct seq_file *m,
  4900. struct binder_proc *proc)
  4901. {
  4902. struct binder_work *w;
  4903. struct binder_thread *thread;
  4904. struct rb_node *n;
  4905. int count, strong, weak, ready_threads;
  4906. size_t free_async_space =
  4907. binder_alloc_get_free_async_space(&proc->alloc);
  4908. seq_printf(m, "proc %d\n", proc->pid);
  4909. seq_printf(m, "context %s\n", proc->context->name);
  4910. count = 0;
  4911. ready_threads = 0;
  4912. binder_inner_proc_lock(proc);
  4913. for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
  4914. count++;
  4915. list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
  4916. ready_threads++;
  4917. seq_printf(m, " threads: %d\n", count);
  4918. seq_printf(m, " requested threads: %d+%d/%d\n"
  4919. " ready threads %d\n"
  4920. " free async space %zd\n", proc->requested_threads,
  4921. proc->requested_threads_started, proc->max_threads,
  4922. ready_threads,
  4923. free_async_space);
  4924. count = 0;
  4925. for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
  4926. count++;
  4927. binder_inner_proc_unlock(proc);
  4928. seq_printf(m, " nodes: %d\n", count);
  4929. count = 0;
  4930. strong = 0;
  4931. weak = 0;
  4932. binder_proc_lock(proc);
  4933. for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
  4934. struct binder_ref *ref = rb_entry(n, struct binder_ref,
  4935. rb_node_desc);
  4936. count++;
  4937. strong += ref->data.strong;
  4938. weak += ref->data.weak;
  4939. }
  4940. binder_proc_unlock(proc);
  4941. seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
  4942. count = binder_alloc_get_allocated_count(&proc->alloc);
  4943. seq_printf(m, " buffers: %d\n", count);
  4944. binder_alloc_print_pages(m, &proc->alloc);
  4945. count = 0;
  4946. binder_inner_proc_lock(proc);
  4947. list_for_each_entry(w, &proc->todo, entry) {
  4948. if (w->type == BINDER_WORK_TRANSACTION)
  4949. count++;
  4950. }
  4951. binder_inner_proc_unlock(proc);
  4952. seq_printf(m, " pending transactions: %d\n", count);
  4953. print_binder_stats(m, " ", &proc->stats);
  4954. }
  4955. static int binder_state_show(struct seq_file *m, void *unused)
  4956. {
  4957. struct binder_proc *proc;
  4958. struct binder_node *node;
  4959. struct binder_node *last_node = NULL;
  4960. seq_puts(m, "binder state:\n");
  4961. spin_lock(&binder_dead_nodes_lock);
  4962. if (!hlist_empty(&binder_dead_nodes))
  4963. seq_puts(m, "dead nodes:\n");
  4964. hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
  4965. /*
  4966. * take a temporary reference on the node so it
  4967. * survives and isn't removed from the list
  4968. * while we print it.
  4969. */
  4970. node->tmp_refs++;
  4971. spin_unlock(&binder_dead_nodes_lock);
  4972. if (last_node)
  4973. binder_put_node(last_node);
  4974. binder_node_lock(node);
  4975. print_binder_node_nilocked(m, node);
  4976. binder_node_unlock(node);
  4977. last_node = node;
  4978. spin_lock(&binder_dead_nodes_lock);
  4979. }
  4980. spin_unlock(&binder_dead_nodes_lock);
  4981. if (last_node)
  4982. binder_put_node(last_node);
  4983. mutex_lock(&binder_procs_lock);
  4984. hlist_for_each_entry(proc, &binder_procs, proc_node)
  4985. print_binder_proc(m, proc, 1);
  4986. mutex_unlock(&binder_procs_lock);
  4987. return 0;
  4988. }
  4989. static int binder_stats_show(struct seq_file *m, void *unused)
  4990. {
  4991. struct binder_proc *proc;
  4992. seq_puts(m, "binder stats:\n");
  4993. print_binder_stats(m, "", &binder_stats);
  4994. mutex_lock(&binder_procs_lock);
  4995. hlist_for_each_entry(proc, &binder_procs, proc_node)
  4996. print_binder_proc_stats(m, proc);
  4997. mutex_unlock(&binder_procs_lock);
  4998. return 0;
  4999. }
  5000. static int binder_transactions_show(struct seq_file *m, void *unused)
  5001. {
  5002. struct binder_proc *proc;
  5003. seq_puts(m, "binder transactions:\n");
  5004. mutex_lock(&binder_procs_lock);
  5005. hlist_for_each_entry(proc, &binder_procs, proc_node)
  5006. print_binder_proc(m, proc, 0);
  5007. mutex_unlock(&binder_procs_lock);
  5008. return 0;
  5009. }
  5010. static int binder_proc_show(struct seq_file *m, void *unused)
  5011. {
  5012. struct binder_proc *itr;
  5013. int pid = (unsigned long)m->private;
  5014. mutex_lock(&binder_procs_lock);
  5015. hlist_for_each_entry(itr, &binder_procs, proc_node) {
  5016. if (itr->pid == pid) {
  5017. seq_puts(m, "binder proc state:\n");
  5018. print_binder_proc(m, itr, 1);
  5019. }
  5020. }
  5021. mutex_unlock(&binder_procs_lock);
  5022. return 0;
  5023. }
  5024. static void print_binder_transaction_log_entry(struct seq_file *m,
  5025. struct binder_transaction_log_entry *e)
  5026. {
  5027. int debug_id = READ_ONCE(e->debug_id_done);
  5028. /*
  5029. * read barrier to guarantee debug_id_done read before
  5030. * we print the log values
  5031. */
  5032. smp_rmb();
  5033. seq_printf(m,
  5034. "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
  5035. e->debug_id, (e->call_type == 2) ? "reply" :
  5036. ((e->call_type == 1) ? "async" : "call "), e->from_proc,
  5037. e->from_thread, e->to_proc, e->to_thread, e->context_name,
  5038. e->to_node, e->target_handle, e->data_size, e->offsets_size,
  5039. e->return_error, e->return_error_param,
  5040. e->return_error_line);
  5041. /*
  5042. * read-barrier to guarantee read of debug_id_done after
  5043. * done printing the fields of the entry
  5044. */
  5045. smp_rmb();
  5046. seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
  5047. "\n" : " (incomplete)\n");
  5048. }
  5049. static int binder_transaction_log_show(struct seq_file *m, void *unused)
  5050. {
  5051. struct binder_transaction_log *log = m->private;
  5052. unsigned int log_cur = atomic_read(&log->cur);
  5053. unsigned int count;
  5054. unsigned int cur;
  5055. int i;
  5056. count = log_cur + 1;
  5057. cur = count < ARRAY_SIZE(log->entry) && !log->full ?
  5058. 0 : count % ARRAY_SIZE(log->entry);
  5059. if (count > ARRAY_SIZE(log->entry) || log->full)
  5060. count = ARRAY_SIZE(log->entry);
  5061. for (i = 0; i < count; i++) {
  5062. unsigned int index = cur++ % ARRAY_SIZE(log->entry);
  5063. print_binder_transaction_log_entry(m, &log->entry[index]);
  5064. }
  5065. return 0;
  5066. }
  5067. static const struct file_operations binder_fops = {
  5068. .owner = THIS_MODULE,
  5069. .poll = binder_poll,
  5070. .unlocked_ioctl = binder_ioctl,
  5071. .compat_ioctl = binder_ioctl,
  5072. .mmap = binder_mmap,
  5073. .open = binder_open,
  5074. .flush = binder_flush,
  5075. .release = binder_release,
  5076. };
  5077. BINDER_DEBUG_ENTRY(state);
  5078. BINDER_DEBUG_ENTRY(stats);
  5079. BINDER_DEBUG_ENTRY(transactions);
  5080. BINDER_DEBUG_ENTRY(transaction_log);
  5081. static int __init init_binder_device(const char *name)
  5082. {
  5083. int ret;
  5084. struct binder_device *binder_device;
  5085. binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
  5086. if (!binder_device)
  5087. return -ENOMEM;
  5088. binder_device->miscdev.fops = &binder_fops;
  5089. binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
  5090. binder_device->miscdev.name = name;
  5091. binder_device->context.binder_context_mgr_uid = INVALID_UID;
  5092. binder_device->context.name = name;
  5093. mutex_init(&binder_device->context.context_mgr_node_lock);
  5094. ret = misc_register(&binder_device->miscdev);
  5095. if (ret < 0) {
  5096. kfree(binder_device);
  5097. return ret;
  5098. }
  5099. hlist_add_head(&binder_device->hlist, &binder_devices);
  5100. return ret;
  5101. }
  5102. static int __init binder_init(void)
  5103. {
  5104. int ret;
  5105. char *device_name, *device_names, *device_tmp;
  5106. struct binder_device *device;
  5107. struct hlist_node *tmp;
  5108. ret = binder_alloc_shrinker_init();
  5109. if (ret)
  5110. return ret;
  5111. atomic_set(&binder_transaction_log.cur, ~0U);
  5112. atomic_set(&binder_transaction_log_failed.cur, ~0U);
  5113. binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
  5114. if (binder_debugfs_dir_entry_root)
  5115. binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
  5116. binder_debugfs_dir_entry_root);
  5117. if (binder_debugfs_dir_entry_root) {
  5118. debugfs_create_file("state",
  5119. 0444,
  5120. binder_debugfs_dir_entry_root,
  5121. NULL,
  5122. &binder_state_fops);
  5123. debugfs_create_file("stats",
  5124. 0444,
  5125. binder_debugfs_dir_entry_root,
  5126. NULL,
  5127. &binder_stats_fops);
  5128. debugfs_create_file("transactions",
  5129. 0444,
  5130. binder_debugfs_dir_entry_root,
  5131. NULL,
  5132. &binder_transactions_fops);
  5133. debugfs_create_file("transaction_log",
  5134. 0444,
  5135. binder_debugfs_dir_entry_root,
  5136. &binder_transaction_log,
  5137. &binder_transaction_log_fops);
  5138. debugfs_create_file("failed_transaction_log",
  5139. 0444,
  5140. binder_debugfs_dir_entry_root,
  5141. &binder_transaction_log_failed,
  5142. &binder_transaction_log_fops);
  5143. }
  5144. /*
  5145. * Copy the module_parameter string, because we don't want to
  5146. * tokenize it in-place.
  5147. */
  5148. device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
  5149. if (!device_names) {
  5150. ret = -ENOMEM;
  5151. goto err_alloc_device_names_failed;
  5152. }
  5153. strcpy(device_names, binder_devices_param);
  5154. device_tmp = device_names;
  5155. while ((device_name = strsep(&device_tmp, ","))) {
  5156. ret = init_binder_device(device_name);
  5157. if (ret)
  5158. goto err_init_binder_device_failed;
  5159. }
  5160. return ret;
  5161. err_init_binder_device_failed:
  5162. hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
  5163. misc_deregister(&device->miscdev);
  5164. hlist_del(&device->hlist);
  5165. kfree(device);
  5166. }
  5167. kfree(device_names);
  5168. err_alloc_device_names_failed:
  5169. debugfs_remove_recursive(binder_debugfs_dir_entry_root);
  5170. return ret;
  5171. }
  5172. device_initcall(binder_init);
  5173. #define CREATE_TRACE_POINTS
  5174. #include "binder_trace.h"
  5175. MODULE_LICENSE("GPL v2");