binder.c 189 KB

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