binder.c 195 KB

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