binder.c 176 KB

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