binder.c 159 KB

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