binder.c 167 KB

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