binder.c 202 KB

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