binder.c 161 KB

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