binder.c 161 KB

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