binder.c 163 KB

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