binder.c 189 KB

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