binder.c 179 KB

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