binder.c 200 KB

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