ftrace.c 136 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925
  1. /*
  2. * Infrastructure for profiling code inserted by 'gcc -pg'.
  3. *
  4. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  5. * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
  6. *
  7. * Originally ported from the -rt patch by:
  8. * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
  9. *
  10. * Based on code in the latency_tracer, that is:
  11. *
  12. * Copyright (C) 2004-2006 Ingo Molnar
  13. * Copyright (C) 2004 Nadia Yvette Chambers
  14. */
  15. #include <linux/stop_machine.h>
  16. #include <linux/clocksource.h>
  17. #include <linux/kallsyms.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/suspend.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/hardirq.h>
  22. #include <linux/kthread.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/bsearch.h>
  25. #include <linux/module.h>
  26. #include <linux/ftrace.h>
  27. #include <linux/sysctl.h>
  28. #include <linux/slab.h>
  29. #include <linux/ctype.h>
  30. #include <linux/sort.h>
  31. #include <linux/list.h>
  32. #include <linux/hash.h>
  33. #include <linux/rcupdate.h>
  34. #include <trace/events/sched.h>
  35. #include <asm/setup.h>
  36. #include "trace_output.h"
  37. #include "trace_stat.h"
  38. #define FTRACE_WARN_ON(cond) \
  39. ({ \
  40. int ___r = cond; \
  41. if (WARN_ON(___r)) \
  42. ftrace_kill(); \
  43. ___r; \
  44. })
  45. #define FTRACE_WARN_ON_ONCE(cond) \
  46. ({ \
  47. int ___r = cond; \
  48. if (WARN_ON_ONCE(___r)) \
  49. ftrace_kill(); \
  50. ___r; \
  51. })
  52. /* hash bits for specific function selection */
  53. #define FTRACE_HASH_BITS 7
  54. #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
  55. #define FTRACE_HASH_DEFAULT_BITS 10
  56. #define FTRACE_HASH_MAX_BITS 12
  57. #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_CONTROL)
  58. #ifdef CONFIG_DYNAMIC_FTRACE
  59. #define INIT_OPS_HASH(opsname) \
  60. .func_hash = &opsname.local_hash, \
  61. .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
  62. #define ASSIGN_OPS_HASH(opsname, val) \
  63. .func_hash = val, \
  64. .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
  65. #else
  66. #define INIT_OPS_HASH(opsname)
  67. #define ASSIGN_OPS_HASH(opsname, val)
  68. #endif
  69. static struct ftrace_ops ftrace_list_end __read_mostly = {
  70. .func = ftrace_stub,
  71. .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
  72. INIT_OPS_HASH(ftrace_list_end)
  73. };
  74. /* ftrace_enabled is a method to turn ftrace on or off */
  75. int ftrace_enabled __read_mostly;
  76. static int last_ftrace_enabled;
  77. /* Current function tracing op */
  78. struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
  79. /* What to set function_trace_op to */
  80. static struct ftrace_ops *set_function_trace_op;
  81. /* List for set_ftrace_pid's pids. */
  82. LIST_HEAD(ftrace_pids);
  83. struct ftrace_pid {
  84. struct list_head list;
  85. struct pid *pid;
  86. };
  87. /*
  88. * ftrace_disabled is set when an anomaly is discovered.
  89. * ftrace_disabled is much stronger than ftrace_enabled.
  90. */
  91. static int ftrace_disabled __read_mostly;
  92. static DEFINE_MUTEX(ftrace_lock);
  93. static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
  94. static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
  95. ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
  96. ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
  97. static struct ftrace_ops global_ops;
  98. static struct ftrace_ops control_ops;
  99. static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
  100. struct ftrace_ops *op, struct pt_regs *regs);
  101. #if ARCH_SUPPORTS_FTRACE_OPS
  102. static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
  103. struct ftrace_ops *op, struct pt_regs *regs);
  104. #else
  105. /* See comment below, where ftrace_ops_list_func is defined */
  106. static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
  107. #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
  108. #endif
  109. /*
  110. * Traverse the ftrace_global_list, invoking all entries. The reason that we
  111. * can use rcu_dereference_raw_notrace() is that elements removed from this list
  112. * are simply leaked, so there is no need to interact with a grace-period
  113. * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
  114. * concurrent insertions into the ftrace_global_list.
  115. *
  116. * Silly Alpha and silly pointer-speculation compiler optimizations!
  117. */
  118. #define do_for_each_ftrace_op(op, list) \
  119. op = rcu_dereference_raw_notrace(list); \
  120. do
  121. /*
  122. * Optimized for just a single item in the list (as that is the normal case).
  123. */
  124. #define while_for_each_ftrace_op(op) \
  125. while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
  126. unlikely((op) != &ftrace_list_end))
  127. static inline void ftrace_ops_init(struct ftrace_ops *ops)
  128. {
  129. #ifdef CONFIG_DYNAMIC_FTRACE
  130. if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
  131. mutex_init(&ops->local_hash.regex_lock);
  132. ops->func_hash = &ops->local_hash;
  133. ops->flags |= FTRACE_OPS_FL_INITIALIZED;
  134. }
  135. #endif
  136. }
  137. /**
  138. * ftrace_nr_registered_ops - return number of ops registered
  139. *
  140. * Returns the number of ftrace_ops registered and tracing functions
  141. */
  142. int ftrace_nr_registered_ops(void)
  143. {
  144. struct ftrace_ops *ops;
  145. int cnt = 0;
  146. mutex_lock(&ftrace_lock);
  147. for (ops = ftrace_ops_list;
  148. ops != &ftrace_list_end; ops = ops->next)
  149. cnt++;
  150. mutex_unlock(&ftrace_lock);
  151. return cnt;
  152. }
  153. static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
  154. struct ftrace_ops *op, struct pt_regs *regs)
  155. {
  156. if (!test_tsk_trace_trace(current))
  157. return;
  158. ftrace_pid_function(ip, parent_ip, op, regs);
  159. }
  160. static void set_ftrace_pid_function(ftrace_func_t func)
  161. {
  162. /* do not set ftrace_pid_function to itself! */
  163. if (func != ftrace_pid_func)
  164. ftrace_pid_function = func;
  165. }
  166. /**
  167. * clear_ftrace_function - reset the ftrace function
  168. *
  169. * This NULLs the ftrace function and in essence stops
  170. * tracing. There may be lag
  171. */
  172. void clear_ftrace_function(void)
  173. {
  174. ftrace_trace_function = ftrace_stub;
  175. ftrace_pid_function = ftrace_stub;
  176. }
  177. static void control_ops_disable_all(struct ftrace_ops *ops)
  178. {
  179. int cpu;
  180. for_each_possible_cpu(cpu)
  181. *per_cpu_ptr(ops->disabled, cpu) = 1;
  182. }
  183. static int control_ops_alloc(struct ftrace_ops *ops)
  184. {
  185. int __percpu *disabled;
  186. disabled = alloc_percpu(int);
  187. if (!disabled)
  188. return -ENOMEM;
  189. ops->disabled = disabled;
  190. control_ops_disable_all(ops);
  191. return 0;
  192. }
  193. static void ftrace_sync(struct work_struct *work)
  194. {
  195. /*
  196. * This function is just a stub to implement a hard force
  197. * of synchronize_sched(). This requires synchronizing
  198. * tasks even in userspace and idle.
  199. *
  200. * Yes, function tracing is rude.
  201. */
  202. }
  203. static void ftrace_sync_ipi(void *data)
  204. {
  205. /* Probably not needed, but do it anyway */
  206. smp_rmb();
  207. }
  208. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  209. static void update_function_graph_func(void);
  210. #else
  211. static inline void update_function_graph_func(void) { }
  212. #endif
  213. static void update_ftrace_function(void)
  214. {
  215. ftrace_func_t func;
  216. /*
  217. * Prepare the ftrace_ops that the arch callback will use.
  218. * If there's only one ftrace_ops registered, the ftrace_ops_list
  219. * will point to the ops we want.
  220. */
  221. set_function_trace_op = ftrace_ops_list;
  222. /* If there's no ftrace_ops registered, just call the stub function */
  223. if (ftrace_ops_list == &ftrace_list_end) {
  224. func = ftrace_stub;
  225. /*
  226. * If we are at the end of the list and this ops is
  227. * recursion safe and not dynamic and the arch supports passing ops,
  228. * then have the mcount trampoline call the function directly.
  229. */
  230. } else if (ftrace_ops_list->next == &ftrace_list_end) {
  231. func = ftrace_ops_get_func(ftrace_ops_list);
  232. } else {
  233. /* Just use the default ftrace_ops */
  234. set_function_trace_op = &ftrace_list_end;
  235. func = ftrace_ops_list_func;
  236. }
  237. update_function_graph_func();
  238. /* If there's no change, then do nothing more here */
  239. if (ftrace_trace_function == func)
  240. return;
  241. /*
  242. * If we are using the list function, it doesn't care
  243. * about the function_trace_ops.
  244. */
  245. if (func == ftrace_ops_list_func) {
  246. ftrace_trace_function = func;
  247. /*
  248. * Don't even bother setting function_trace_ops,
  249. * it would be racy to do so anyway.
  250. */
  251. return;
  252. }
  253. #ifndef CONFIG_DYNAMIC_FTRACE
  254. /*
  255. * For static tracing, we need to be a bit more careful.
  256. * The function change takes affect immediately. Thus,
  257. * we need to coorditate the setting of the function_trace_ops
  258. * with the setting of the ftrace_trace_function.
  259. *
  260. * Set the function to the list ops, which will call the
  261. * function we want, albeit indirectly, but it handles the
  262. * ftrace_ops and doesn't depend on function_trace_op.
  263. */
  264. ftrace_trace_function = ftrace_ops_list_func;
  265. /*
  266. * Make sure all CPUs see this. Yes this is slow, but static
  267. * tracing is slow and nasty to have enabled.
  268. */
  269. schedule_on_each_cpu(ftrace_sync);
  270. /* Now all cpus are using the list ops. */
  271. function_trace_op = set_function_trace_op;
  272. /* Make sure the function_trace_op is visible on all CPUs */
  273. smp_wmb();
  274. /* Nasty way to force a rmb on all cpus */
  275. smp_call_function(ftrace_sync_ipi, NULL, 1);
  276. /* OK, we are all set to update the ftrace_trace_function now! */
  277. #endif /* !CONFIG_DYNAMIC_FTRACE */
  278. ftrace_trace_function = func;
  279. }
  280. int using_ftrace_ops_list_func(void)
  281. {
  282. return ftrace_trace_function == ftrace_ops_list_func;
  283. }
  284. static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
  285. {
  286. ops->next = *list;
  287. /*
  288. * We are entering ops into the list but another
  289. * CPU might be walking that list. We need to make sure
  290. * the ops->next pointer is valid before another CPU sees
  291. * the ops pointer included into the list.
  292. */
  293. rcu_assign_pointer(*list, ops);
  294. }
  295. static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
  296. {
  297. struct ftrace_ops **p;
  298. /*
  299. * If we are removing the last function, then simply point
  300. * to the ftrace_stub.
  301. */
  302. if (*list == ops && ops->next == &ftrace_list_end) {
  303. *list = &ftrace_list_end;
  304. return 0;
  305. }
  306. for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
  307. if (*p == ops)
  308. break;
  309. if (*p != ops)
  310. return -1;
  311. *p = (*p)->next;
  312. return 0;
  313. }
  314. static void add_ftrace_list_ops(struct ftrace_ops **list,
  315. struct ftrace_ops *main_ops,
  316. struct ftrace_ops *ops)
  317. {
  318. int first = *list == &ftrace_list_end;
  319. add_ftrace_ops(list, ops);
  320. if (first)
  321. add_ftrace_ops(&ftrace_ops_list, main_ops);
  322. }
  323. static int remove_ftrace_list_ops(struct ftrace_ops **list,
  324. struct ftrace_ops *main_ops,
  325. struct ftrace_ops *ops)
  326. {
  327. int ret = remove_ftrace_ops(list, ops);
  328. if (!ret && *list == &ftrace_list_end)
  329. ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
  330. return ret;
  331. }
  332. static void ftrace_update_trampoline(struct ftrace_ops *ops);
  333. static int __register_ftrace_function(struct ftrace_ops *ops)
  334. {
  335. if (ops->flags & FTRACE_OPS_FL_DELETED)
  336. return -EINVAL;
  337. if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
  338. return -EBUSY;
  339. #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  340. /*
  341. * If the ftrace_ops specifies SAVE_REGS, then it only can be used
  342. * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
  343. * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
  344. */
  345. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
  346. !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
  347. return -EINVAL;
  348. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
  349. ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
  350. #endif
  351. if (!core_kernel_data((unsigned long)ops))
  352. ops->flags |= FTRACE_OPS_FL_DYNAMIC;
  353. if (ops->flags & FTRACE_OPS_FL_CONTROL) {
  354. if (control_ops_alloc(ops))
  355. return -ENOMEM;
  356. add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
  357. /* The control_ops needs the trampoline update */
  358. ops = &control_ops;
  359. } else
  360. add_ftrace_ops(&ftrace_ops_list, ops);
  361. ftrace_update_trampoline(ops);
  362. if (ftrace_enabled)
  363. update_ftrace_function();
  364. return 0;
  365. }
  366. static int __unregister_ftrace_function(struct ftrace_ops *ops)
  367. {
  368. int ret;
  369. if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
  370. return -EBUSY;
  371. if (ops->flags & FTRACE_OPS_FL_CONTROL) {
  372. ret = remove_ftrace_list_ops(&ftrace_control_list,
  373. &control_ops, ops);
  374. } else
  375. ret = remove_ftrace_ops(&ftrace_ops_list, ops);
  376. if (ret < 0)
  377. return ret;
  378. if (ftrace_enabled)
  379. update_ftrace_function();
  380. return 0;
  381. }
  382. static void ftrace_update_pid_func(void)
  383. {
  384. /* Only do something if we are tracing something */
  385. if (ftrace_trace_function == ftrace_stub)
  386. return;
  387. update_ftrace_function();
  388. }
  389. #ifdef CONFIG_FUNCTION_PROFILER
  390. struct ftrace_profile {
  391. struct hlist_node node;
  392. unsigned long ip;
  393. unsigned long counter;
  394. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  395. unsigned long long time;
  396. unsigned long long time_squared;
  397. #endif
  398. };
  399. struct ftrace_profile_page {
  400. struct ftrace_profile_page *next;
  401. unsigned long index;
  402. struct ftrace_profile records[];
  403. };
  404. struct ftrace_profile_stat {
  405. atomic_t disabled;
  406. struct hlist_head *hash;
  407. struct ftrace_profile_page *pages;
  408. struct ftrace_profile_page *start;
  409. struct tracer_stat stat;
  410. };
  411. #define PROFILE_RECORDS_SIZE \
  412. (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
  413. #define PROFILES_PER_PAGE \
  414. (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
  415. static int ftrace_profile_enabled __read_mostly;
  416. /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
  417. static DEFINE_MUTEX(ftrace_profile_lock);
  418. static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
  419. #define FTRACE_PROFILE_HASH_BITS 10
  420. #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
  421. static void *
  422. function_stat_next(void *v, int idx)
  423. {
  424. struct ftrace_profile *rec = v;
  425. struct ftrace_profile_page *pg;
  426. pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
  427. again:
  428. if (idx != 0)
  429. rec++;
  430. if ((void *)rec >= (void *)&pg->records[pg->index]) {
  431. pg = pg->next;
  432. if (!pg)
  433. return NULL;
  434. rec = &pg->records[0];
  435. if (!rec->counter)
  436. goto again;
  437. }
  438. return rec;
  439. }
  440. static void *function_stat_start(struct tracer_stat *trace)
  441. {
  442. struct ftrace_profile_stat *stat =
  443. container_of(trace, struct ftrace_profile_stat, stat);
  444. if (!stat || !stat->start)
  445. return NULL;
  446. return function_stat_next(&stat->start->records[0], 0);
  447. }
  448. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  449. /* function graph compares on total time */
  450. static int function_stat_cmp(void *p1, void *p2)
  451. {
  452. struct ftrace_profile *a = p1;
  453. struct ftrace_profile *b = p2;
  454. if (a->time < b->time)
  455. return -1;
  456. if (a->time > b->time)
  457. return 1;
  458. else
  459. return 0;
  460. }
  461. #else
  462. /* not function graph compares against hits */
  463. static int function_stat_cmp(void *p1, void *p2)
  464. {
  465. struct ftrace_profile *a = p1;
  466. struct ftrace_profile *b = p2;
  467. if (a->counter < b->counter)
  468. return -1;
  469. if (a->counter > b->counter)
  470. return 1;
  471. else
  472. return 0;
  473. }
  474. #endif
  475. static int function_stat_headers(struct seq_file *m)
  476. {
  477. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  478. seq_puts(m, " Function "
  479. "Hit Time Avg s^2\n"
  480. " -------- "
  481. "--- ---- --- ---\n");
  482. #else
  483. seq_puts(m, " Function Hit\n"
  484. " -------- ---\n");
  485. #endif
  486. return 0;
  487. }
  488. static int function_stat_show(struct seq_file *m, void *v)
  489. {
  490. struct ftrace_profile *rec = v;
  491. char str[KSYM_SYMBOL_LEN];
  492. int ret = 0;
  493. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  494. static struct trace_seq s;
  495. unsigned long long avg;
  496. unsigned long long stddev;
  497. #endif
  498. mutex_lock(&ftrace_profile_lock);
  499. /* we raced with function_profile_reset() */
  500. if (unlikely(rec->counter == 0)) {
  501. ret = -EBUSY;
  502. goto out;
  503. }
  504. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  505. seq_printf(m, " %-30.30s %10lu", str, rec->counter);
  506. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  507. seq_puts(m, " ");
  508. avg = rec->time;
  509. do_div(avg, rec->counter);
  510. /* Sample standard deviation (s^2) */
  511. if (rec->counter <= 1)
  512. stddev = 0;
  513. else {
  514. /*
  515. * Apply Welford's method:
  516. * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
  517. */
  518. stddev = rec->counter * rec->time_squared -
  519. rec->time * rec->time;
  520. /*
  521. * Divide only 1000 for ns^2 -> us^2 conversion.
  522. * trace_print_graph_duration will divide 1000 again.
  523. */
  524. do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
  525. }
  526. trace_seq_init(&s);
  527. trace_print_graph_duration(rec->time, &s);
  528. trace_seq_puts(&s, " ");
  529. trace_print_graph_duration(avg, &s);
  530. trace_seq_puts(&s, " ");
  531. trace_print_graph_duration(stddev, &s);
  532. trace_print_seq(m, &s);
  533. #endif
  534. seq_putc(m, '\n');
  535. out:
  536. mutex_unlock(&ftrace_profile_lock);
  537. return ret;
  538. }
  539. static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
  540. {
  541. struct ftrace_profile_page *pg;
  542. pg = stat->pages = stat->start;
  543. while (pg) {
  544. memset(pg->records, 0, PROFILE_RECORDS_SIZE);
  545. pg->index = 0;
  546. pg = pg->next;
  547. }
  548. memset(stat->hash, 0,
  549. FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
  550. }
  551. int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
  552. {
  553. struct ftrace_profile_page *pg;
  554. int functions;
  555. int pages;
  556. int i;
  557. /* If we already allocated, do nothing */
  558. if (stat->pages)
  559. return 0;
  560. stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
  561. if (!stat->pages)
  562. return -ENOMEM;
  563. #ifdef CONFIG_DYNAMIC_FTRACE
  564. functions = ftrace_update_tot_cnt;
  565. #else
  566. /*
  567. * We do not know the number of functions that exist because
  568. * dynamic tracing is what counts them. With past experience
  569. * we have around 20K functions. That should be more than enough.
  570. * It is highly unlikely we will execute every function in
  571. * the kernel.
  572. */
  573. functions = 20000;
  574. #endif
  575. pg = stat->start = stat->pages;
  576. pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
  577. for (i = 1; i < pages; i++) {
  578. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  579. if (!pg->next)
  580. goto out_free;
  581. pg = pg->next;
  582. }
  583. return 0;
  584. out_free:
  585. pg = stat->start;
  586. while (pg) {
  587. unsigned long tmp = (unsigned long)pg;
  588. pg = pg->next;
  589. free_page(tmp);
  590. }
  591. stat->pages = NULL;
  592. stat->start = NULL;
  593. return -ENOMEM;
  594. }
  595. static int ftrace_profile_init_cpu(int cpu)
  596. {
  597. struct ftrace_profile_stat *stat;
  598. int size;
  599. stat = &per_cpu(ftrace_profile_stats, cpu);
  600. if (stat->hash) {
  601. /* If the profile is already created, simply reset it */
  602. ftrace_profile_reset(stat);
  603. return 0;
  604. }
  605. /*
  606. * We are profiling all functions, but usually only a few thousand
  607. * functions are hit. We'll make a hash of 1024 items.
  608. */
  609. size = FTRACE_PROFILE_HASH_SIZE;
  610. stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
  611. if (!stat->hash)
  612. return -ENOMEM;
  613. /* Preallocate the function profiling pages */
  614. if (ftrace_profile_pages_init(stat) < 0) {
  615. kfree(stat->hash);
  616. stat->hash = NULL;
  617. return -ENOMEM;
  618. }
  619. return 0;
  620. }
  621. static int ftrace_profile_init(void)
  622. {
  623. int cpu;
  624. int ret = 0;
  625. for_each_possible_cpu(cpu) {
  626. ret = ftrace_profile_init_cpu(cpu);
  627. if (ret)
  628. break;
  629. }
  630. return ret;
  631. }
  632. /* interrupts must be disabled */
  633. static struct ftrace_profile *
  634. ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
  635. {
  636. struct ftrace_profile *rec;
  637. struct hlist_head *hhd;
  638. unsigned long key;
  639. key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
  640. hhd = &stat->hash[key];
  641. if (hlist_empty(hhd))
  642. return NULL;
  643. hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
  644. if (rec->ip == ip)
  645. return rec;
  646. }
  647. return NULL;
  648. }
  649. static void ftrace_add_profile(struct ftrace_profile_stat *stat,
  650. struct ftrace_profile *rec)
  651. {
  652. unsigned long key;
  653. key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
  654. hlist_add_head_rcu(&rec->node, &stat->hash[key]);
  655. }
  656. /*
  657. * The memory is already allocated, this simply finds a new record to use.
  658. */
  659. static struct ftrace_profile *
  660. ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
  661. {
  662. struct ftrace_profile *rec = NULL;
  663. /* prevent recursion (from NMIs) */
  664. if (atomic_inc_return(&stat->disabled) != 1)
  665. goto out;
  666. /*
  667. * Try to find the function again since an NMI
  668. * could have added it
  669. */
  670. rec = ftrace_find_profiled_func(stat, ip);
  671. if (rec)
  672. goto out;
  673. if (stat->pages->index == PROFILES_PER_PAGE) {
  674. if (!stat->pages->next)
  675. goto out;
  676. stat->pages = stat->pages->next;
  677. }
  678. rec = &stat->pages->records[stat->pages->index++];
  679. rec->ip = ip;
  680. ftrace_add_profile(stat, rec);
  681. out:
  682. atomic_dec(&stat->disabled);
  683. return rec;
  684. }
  685. static void
  686. function_profile_call(unsigned long ip, unsigned long parent_ip,
  687. struct ftrace_ops *ops, struct pt_regs *regs)
  688. {
  689. struct ftrace_profile_stat *stat;
  690. struct ftrace_profile *rec;
  691. unsigned long flags;
  692. if (!ftrace_profile_enabled)
  693. return;
  694. local_irq_save(flags);
  695. stat = this_cpu_ptr(&ftrace_profile_stats);
  696. if (!stat->hash || !ftrace_profile_enabled)
  697. goto out;
  698. rec = ftrace_find_profiled_func(stat, ip);
  699. if (!rec) {
  700. rec = ftrace_profile_alloc(stat, ip);
  701. if (!rec)
  702. goto out;
  703. }
  704. rec->counter++;
  705. out:
  706. local_irq_restore(flags);
  707. }
  708. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  709. static int profile_graph_entry(struct ftrace_graph_ent *trace)
  710. {
  711. function_profile_call(trace->func, 0, NULL, NULL);
  712. return 1;
  713. }
  714. static void profile_graph_return(struct ftrace_graph_ret *trace)
  715. {
  716. struct ftrace_profile_stat *stat;
  717. unsigned long long calltime;
  718. struct ftrace_profile *rec;
  719. unsigned long flags;
  720. local_irq_save(flags);
  721. stat = this_cpu_ptr(&ftrace_profile_stats);
  722. if (!stat->hash || !ftrace_profile_enabled)
  723. goto out;
  724. /* If the calltime was zero'd ignore it */
  725. if (!trace->calltime)
  726. goto out;
  727. calltime = trace->rettime - trace->calltime;
  728. if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
  729. int index;
  730. index = trace->depth;
  731. /* Append this call time to the parent time to subtract */
  732. if (index)
  733. current->ret_stack[index - 1].subtime += calltime;
  734. if (current->ret_stack[index].subtime < calltime)
  735. calltime -= current->ret_stack[index].subtime;
  736. else
  737. calltime = 0;
  738. }
  739. rec = ftrace_find_profiled_func(stat, trace->func);
  740. if (rec) {
  741. rec->time += calltime;
  742. rec->time_squared += calltime * calltime;
  743. }
  744. out:
  745. local_irq_restore(flags);
  746. }
  747. static int register_ftrace_profiler(void)
  748. {
  749. return register_ftrace_graph(&profile_graph_return,
  750. &profile_graph_entry);
  751. }
  752. static void unregister_ftrace_profiler(void)
  753. {
  754. unregister_ftrace_graph();
  755. }
  756. #else
  757. static struct ftrace_ops ftrace_profile_ops __read_mostly = {
  758. .func = function_profile_call,
  759. .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
  760. INIT_OPS_HASH(ftrace_profile_ops)
  761. };
  762. static int register_ftrace_profiler(void)
  763. {
  764. return register_ftrace_function(&ftrace_profile_ops);
  765. }
  766. static void unregister_ftrace_profiler(void)
  767. {
  768. unregister_ftrace_function(&ftrace_profile_ops);
  769. }
  770. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  771. static ssize_t
  772. ftrace_profile_write(struct file *filp, const char __user *ubuf,
  773. size_t cnt, loff_t *ppos)
  774. {
  775. unsigned long val;
  776. int ret;
  777. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  778. if (ret)
  779. return ret;
  780. val = !!val;
  781. mutex_lock(&ftrace_profile_lock);
  782. if (ftrace_profile_enabled ^ val) {
  783. if (val) {
  784. ret = ftrace_profile_init();
  785. if (ret < 0) {
  786. cnt = ret;
  787. goto out;
  788. }
  789. ret = register_ftrace_profiler();
  790. if (ret < 0) {
  791. cnt = ret;
  792. goto out;
  793. }
  794. ftrace_profile_enabled = 1;
  795. } else {
  796. ftrace_profile_enabled = 0;
  797. /*
  798. * unregister_ftrace_profiler calls stop_machine
  799. * so this acts like an synchronize_sched.
  800. */
  801. unregister_ftrace_profiler();
  802. }
  803. }
  804. out:
  805. mutex_unlock(&ftrace_profile_lock);
  806. *ppos += cnt;
  807. return cnt;
  808. }
  809. static ssize_t
  810. ftrace_profile_read(struct file *filp, char __user *ubuf,
  811. size_t cnt, loff_t *ppos)
  812. {
  813. char buf[64]; /* big enough to hold a number */
  814. int r;
  815. r = sprintf(buf, "%u\n", ftrace_profile_enabled);
  816. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  817. }
  818. static const struct file_operations ftrace_profile_fops = {
  819. .open = tracing_open_generic,
  820. .read = ftrace_profile_read,
  821. .write = ftrace_profile_write,
  822. .llseek = default_llseek,
  823. };
  824. /* used to initialize the real stat files */
  825. static struct tracer_stat function_stats __initdata = {
  826. .name = "functions",
  827. .stat_start = function_stat_start,
  828. .stat_next = function_stat_next,
  829. .stat_cmp = function_stat_cmp,
  830. .stat_headers = function_stat_headers,
  831. .stat_show = function_stat_show
  832. };
  833. static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
  834. {
  835. struct ftrace_profile_stat *stat;
  836. struct dentry *entry;
  837. char *name;
  838. int ret;
  839. int cpu;
  840. for_each_possible_cpu(cpu) {
  841. stat = &per_cpu(ftrace_profile_stats, cpu);
  842. /* allocate enough for function name + cpu number */
  843. name = kmalloc(32, GFP_KERNEL);
  844. if (!name) {
  845. /*
  846. * The files created are permanent, if something happens
  847. * we still do not free memory.
  848. */
  849. WARN(1,
  850. "Could not allocate stat file for cpu %d\n",
  851. cpu);
  852. return;
  853. }
  854. stat->stat = function_stats;
  855. snprintf(name, 32, "function%d", cpu);
  856. stat->stat.name = name;
  857. ret = register_stat_tracer(&stat->stat);
  858. if (ret) {
  859. WARN(1,
  860. "Could not register function stat for cpu %d\n",
  861. cpu);
  862. kfree(name);
  863. return;
  864. }
  865. }
  866. entry = debugfs_create_file("function_profile_enabled", 0644,
  867. d_tracer, NULL, &ftrace_profile_fops);
  868. if (!entry)
  869. pr_warning("Could not create debugfs "
  870. "'function_profile_enabled' entry\n");
  871. }
  872. #else /* CONFIG_FUNCTION_PROFILER */
  873. static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
  874. {
  875. }
  876. #endif /* CONFIG_FUNCTION_PROFILER */
  877. static struct pid * const ftrace_swapper_pid = &init_struct_pid;
  878. #ifdef CONFIG_DYNAMIC_FTRACE
  879. static struct ftrace_ops *removed_ops;
  880. /*
  881. * Set when doing a global update, like enabling all recs or disabling them.
  882. * It is not set when just updating a single ftrace_ops.
  883. */
  884. static bool update_all_ops;
  885. #ifndef CONFIG_FTRACE_MCOUNT_RECORD
  886. # error Dynamic ftrace depends on MCOUNT_RECORD
  887. #endif
  888. static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
  889. struct ftrace_func_probe {
  890. struct hlist_node node;
  891. struct ftrace_probe_ops *ops;
  892. unsigned long flags;
  893. unsigned long ip;
  894. void *data;
  895. struct list_head free_list;
  896. };
  897. struct ftrace_func_entry {
  898. struct hlist_node hlist;
  899. unsigned long ip;
  900. };
  901. struct ftrace_hash {
  902. unsigned long size_bits;
  903. struct hlist_head *buckets;
  904. unsigned long count;
  905. struct rcu_head rcu;
  906. };
  907. /*
  908. * We make these constant because no one should touch them,
  909. * but they are used as the default "empty hash", to avoid allocating
  910. * it all the time. These are in a read only section such that if
  911. * anyone does try to modify it, it will cause an exception.
  912. */
  913. static const struct hlist_head empty_buckets[1];
  914. static const struct ftrace_hash empty_hash = {
  915. .buckets = (struct hlist_head *)empty_buckets,
  916. };
  917. #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
  918. static struct ftrace_ops global_ops = {
  919. .func = ftrace_stub,
  920. .local_hash.notrace_hash = EMPTY_HASH,
  921. .local_hash.filter_hash = EMPTY_HASH,
  922. INIT_OPS_HASH(global_ops)
  923. .flags = FTRACE_OPS_FL_RECURSION_SAFE |
  924. FTRACE_OPS_FL_INITIALIZED,
  925. };
  926. /*
  927. * This is used by __kernel_text_address() to return true if the
  928. * address is on a dynamically allocated trampoline that would
  929. * not return true for either core_kernel_text() or
  930. * is_module_text_address().
  931. */
  932. bool is_ftrace_trampoline(unsigned long addr)
  933. {
  934. struct ftrace_ops *op;
  935. bool ret = false;
  936. /*
  937. * Some of the ops may be dynamically allocated,
  938. * they are freed after a synchronize_sched().
  939. */
  940. preempt_disable_notrace();
  941. do_for_each_ftrace_op(op, ftrace_ops_list) {
  942. /*
  943. * This is to check for dynamically allocated trampolines.
  944. * Trampolines that are in kernel text will have
  945. * core_kernel_text() return true.
  946. */
  947. if (op->trampoline && op->trampoline_size)
  948. if (addr >= op->trampoline &&
  949. addr < op->trampoline + op->trampoline_size) {
  950. ret = true;
  951. goto out;
  952. }
  953. } while_for_each_ftrace_op(op);
  954. out:
  955. preempt_enable_notrace();
  956. return ret;
  957. }
  958. struct ftrace_page {
  959. struct ftrace_page *next;
  960. struct dyn_ftrace *records;
  961. int index;
  962. int size;
  963. };
  964. #define ENTRY_SIZE sizeof(struct dyn_ftrace)
  965. #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
  966. /* estimate from running different kernels */
  967. #define NR_TO_INIT 10000
  968. static struct ftrace_page *ftrace_pages_start;
  969. static struct ftrace_page *ftrace_pages;
  970. static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
  971. {
  972. return !hash || !hash->count;
  973. }
  974. static struct ftrace_func_entry *
  975. ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
  976. {
  977. unsigned long key;
  978. struct ftrace_func_entry *entry;
  979. struct hlist_head *hhd;
  980. if (ftrace_hash_empty(hash))
  981. return NULL;
  982. if (hash->size_bits > 0)
  983. key = hash_long(ip, hash->size_bits);
  984. else
  985. key = 0;
  986. hhd = &hash->buckets[key];
  987. hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
  988. if (entry->ip == ip)
  989. return entry;
  990. }
  991. return NULL;
  992. }
  993. static void __add_hash_entry(struct ftrace_hash *hash,
  994. struct ftrace_func_entry *entry)
  995. {
  996. struct hlist_head *hhd;
  997. unsigned long key;
  998. if (hash->size_bits)
  999. key = hash_long(entry->ip, hash->size_bits);
  1000. else
  1001. key = 0;
  1002. hhd = &hash->buckets[key];
  1003. hlist_add_head(&entry->hlist, hhd);
  1004. hash->count++;
  1005. }
  1006. static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
  1007. {
  1008. struct ftrace_func_entry *entry;
  1009. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  1010. if (!entry)
  1011. return -ENOMEM;
  1012. entry->ip = ip;
  1013. __add_hash_entry(hash, entry);
  1014. return 0;
  1015. }
  1016. static void
  1017. free_hash_entry(struct ftrace_hash *hash,
  1018. struct ftrace_func_entry *entry)
  1019. {
  1020. hlist_del(&entry->hlist);
  1021. kfree(entry);
  1022. hash->count--;
  1023. }
  1024. static void
  1025. remove_hash_entry(struct ftrace_hash *hash,
  1026. struct ftrace_func_entry *entry)
  1027. {
  1028. hlist_del(&entry->hlist);
  1029. hash->count--;
  1030. }
  1031. static void ftrace_hash_clear(struct ftrace_hash *hash)
  1032. {
  1033. struct hlist_head *hhd;
  1034. struct hlist_node *tn;
  1035. struct ftrace_func_entry *entry;
  1036. int size = 1 << hash->size_bits;
  1037. int i;
  1038. if (!hash->count)
  1039. return;
  1040. for (i = 0; i < size; i++) {
  1041. hhd = &hash->buckets[i];
  1042. hlist_for_each_entry_safe(entry, tn, hhd, hlist)
  1043. free_hash_entry(hash, entry);
  1044. }
  1045. FTRACE_WARN_ON(hash->count);
  1046. }
  1047. static void free_ftrace_hash(struct ftrace_hash *hash)
  1048. {
  1049. if (!hash || hash == EMPTY_HASH)
  1050. return;
  1051. ftrace_hash_clear(hash);
  1052. kfree(hash->buckets);
  1053. kfree(hash);
  1054. }
  1055. static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
  1056. {
  1057. struct ftrace_hash *hash;
  1058. hash = container_of(rcu, struct ftrace_hash, rcu);
  1059. free_ftrace_hash(hash);
  1060. }
  1061. static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
  1062. {
  1063. if (!hash || hash == EMPTY_HASH)
  1064. return;
  1065. call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
  1066. }
  1067. void ftrace_free_filter(struct ftrace_ops *ops)
  1068. {
  1069. ftrace_ops_init(ops);
  1070. free_ftrace_hash(ops->func_hash->filter_hash);
  1071. free_ftrace_hash(ops->func_hash->notrace_hash);
  1072. }
  1073. static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
  1074. {
  1075. struct ftrace_hash *hash;
  1076. int size;
  1077. hash = kzalloc(sizeof(*hash), GFP_KERNEL);
  1078. if (!hash)
  1079. return NULL;
  1080. size = 1 << size_bits;
  1081. hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
  1082. if (!hash->buckets) {
  1083. kfree(hash);
  1084. return NULL;
  1085. }
  1086. hash->size_bits = size_bits;
  1087. return hash;
  1088. }
  1089. static struct ftrace_hash *
  1090. alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
  1091. {
  1092. struct ftrace_func_entry *entry;
  1093. struct ftrace_hash *new_hash;
  1094. int size;
  1095. int ret;
  1096. int i;
  1097. new_hash = alloc_ftrace_hash(size_bits);
  1098. if (!new_hash)
  1099. return NULL;
  1100. /* Empty hash? */
  1101. if (ftrace_hash_empty(hash))
  1102. return new_hash;
  1103. size = 1 << hash->size_bits;
  1104. for (i = 0; i < size; i++) {
  1105. hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
  1106. ret = add_hash_entry(new_hash, entry->ip);
  1107. if (ret < 0)
  1108. goto free_hash;
  1109. }
  1110. }
  1111. FTRACE_WARN_ON(new_hash->count != hash->count);
  1112. return new_hash;
  1113. free_hash:
  1114. free_ftrace_hash(new_hash);
  1115. return NULL;
  1116. }
  1117. static void
  1118. ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
  1119. static void
  1120. ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
  1121. static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
  1122. struct ftrace_hash *new_hash);
  1123. static int
  1124. ftrace_hash_move(struct ftrace_ops *ops, int enable,
  1125. struct ftrace_hash **dst, struct ftrace_hash *src)
  1126. {
  1127. struct ftrace_func_entry *entry;
  1128. struct hlist_node *tn;
  1129. struct hlist_head *hhd;
  1130. struct ftrace_hash *new_hash;
  1131. int size = src->count;
  1132. int bits = 0;
  1133. int ret;
  1134. int i;
  1135. /* Reject setting notrace hash on IPMODIFY ftrace_ops */
  1136. if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
  1137. return -EINVAL;
  1138. /*
  1139. * If the new source is empty, just free dst and assign it
  1140. * the empty_hash.
  1141. */
  1142. if (!src->count) {
  1143. new_hash = EMPTY_HASH;
  1144. goto update;
  1145. }
  1146. /*
  1147. * Make the hash size about 1/2 the # found
  1148. */
  1149. for (size /= 2; size; size >>= 1)
  1150. bits++;
  1151. /* Don't allocate too much */
  1152. if (bits > FTRACE_HASH_MAX_BITS)
  1153. bits = FTRACE_HASH_MAX_BITS;
  1154. new_hash = alloc_ftrace_hash(bits);
  1155. if (!new_hash)
  1156. return -ENOMEM;
  1157. size = 1 << src->size_bits;
  1158. for (i = 0; i < size; i++) {
  1159. hhd = &src->buckets[i];
  1160. hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
  1161. remove_hash_entry(src, entry);
  1162. __add_hash_entry(new_hash, entry);
  1163. }
  1164. }
  1165. update:
  1166. /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
  1167. if (enable) {
  1168. /* IPMODIFY should be updated only when filter_hash updating */
  1169. ret = ftrace_hash_ipmodify_update(ops, new_hash);
  1170. if (ret < 0) {
  1171. free_ftrace_hash(new_hash);
  1172. return ret;
  1173. }
  1174. }
  1175. /*
  1176. * Remove the current set, update the hash and add
  1177. * them back.
  1178. */
  1179. ftrace_hash_rec_disable_modify(ops, enable);
  1180. rcu_assign_pointer(*dst, new_hash);
  1181. ftrace_hash_rec_enable_modify(ops, enable);
  1182. return 0;
  1183. }
  1184. static bool hash_contains_ip(unsigned long ip,
  1185. struct ftrace_ops_hash *hash)
  1186. {
  1187. /*
  1188. * The function record is a match if it exists in the filter
  1189. * hash and not in the notrace hash. Note, an emty hash is
  1190. * considered a match for the filter hash, but an empty
  1191. * notrace hash is considered not in the notrace hash.
  1192. */
  1193. return (ftrace_hash_empty(hash->filter_hash) ||
  1194. ftrace_lookup_ip(hash->filter_hash, ip)) &&
  1195. (ftrace_hash_empty(hash->notrace_hash) ||
  1196. !ftrace_lookup_ip(hash->notrace_hash, ip));
  1197. }
  1198. /*
  1199. * Test the hashes for this ops to see if we want to call
  1200. * the ops->func or not.
  1201. *
  1202. * It's a match if the ip is in the ops->filter_hash or
  1203. * the filter_hash does not exist or is empty,
  1204. * AND
  1205. * the ip is not in the ops->notrace_hash.
  1206. *
  1207. * This needs to be called with preemption disabled as
  1208. * the hashes are freed with call_rcu_sched().
  1209. */
  1210. static int
  1211. ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
  1212. {
  1213. struct ftrace_ops_hash hash;
  1214. int ret;
  1215. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  1216. /*
  1217. * There's a small race when adding ops that the ftrace handler
  1218. * that wants regs, may be called without them. We can not
  1219. * allow that handler to be called if regs is NULL.
  1220. */
  1221. if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
  1222. return 0;
  1223. #endif
  1224. hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
  1225. hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
  1226. if (hash_contains_ip(ip, &hash))
  1227. ret = 1;
  1228. else
  1229. ret = 0;
  1230. return ret;
  1231. }
  1232. /*
  1233. * This is a double for. Do not use 'break' to break out of the loop,
  1234. * you must use a goto.
  1235. */
  1236. #define do_for_each_ftrace_rec(pg, rec) \
  1237. for (pg = ftrace_pages_start; pg; pg = pg->next) { \
  1238. int _____i; \
  1239. for (_____i = 0; _____i < pg->index; _____i++) { \
  1240. rec = &pg->records[_____i];
  1241. #define while_for_each_ftrace_rec() \
  1242. } \
  1243. }
  1244. static int ftrace_cmp_recs(const void *a, const void *b)
  1245. {
  1246. const struct dyn_ftrace *key = a;
  1247. const struct dyn_ftrace *rec = b;
  1248. if (key->flags < rec->ip)
  1249. return -1;
  1250. if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
  1251. return 1;
  1252. return 0;
  1253. }
  1254. static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
  1255. {
  1256. struct ftrace_page *pg;
  1257. struct dyn_ftrace *rec;
  1258. struct dyn_ftrace key;
  1259. key.ip = start;
  1260. key.flags = end; /* overload flags, as it is unsigned long */
  1261. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  1262. if (end < pg->records[0].ip ||
  1263. start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
  1264. continue;
  1265. rec = bsearch(&key, pg->records, pg->index,
  1266. sizeof(struct dyn_ftrace),
  1267. ftrace_cmp_recs);
  1268. if (rec)
  1269. return rec->ip;
  1270. }
  1271. return 0;
  1272. }
  1273. /**
  1274. * ftrace_location - return true if the ip giving is a traced location
  1275. * @ip: the instruction pointer to check
  1276. *
  1277. * Returns rec->ip if @ip given is a pointer to a ftrace location.
  1278. * That is, the instruction that is either a NOP or call to
  1279. * the function tracer. It checks the ftrace internal tables to
  1280. * determine if the address belongs or not.
  1281. */
  1282. unsigned long ftrace_location(unsigned long ip)
  1283. {
  1284. return ftrace_location_range(ip, ip);
  1285. }
  1286. /**
  1287. * ftrace_text_reserved - return true if range contains an ftrace location
  1288. * @start: start of range to search
  1289. * @end: end of range to search (inclusive). @end points to the last byte to check.
  1290. *
  1291. * Returns 1 if @start and @end contains a ftrace location.
  1292. * That is, the instruction that is either a NOP or call to
  1293. * the function tracer. It checks the ftrace internal tables to
  1294. * determine if the address belongs or not.
  1295. */
  1296. int ftrace_text_reserved(const void *start, const void *end)
  1297. {
  1298. unsigned long ret;
  1299. ret = ftrace_location_range((unsigned long)start,
  1300. (unsigned long)end);
  1301. return (int)!!ret;
  1302. }
  1303. /* Test if ops registered to this rec needs regs */
  1304. static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
  1305. {
  1306. struct ftrace_ops *ops;
  1307. bool keep_regs = false;
  1308. for (ops = ftrace_ops_list;
  1309. ops != &ftrace_list_end; ops = ops->next) {
  1310. /* pass rec in as regs to have non-NULL val */
  1311. if (ftrace_ops_test(ops, rec->ip, rec)) {
  1312. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
  1313. keep_regs = true;
  1314. break;
  1315. }
  1316. }
  1317. }
  1318. return keep_regs;
  1319. }
  1320. static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
  1321. int filter_hash,
  1322. bool inc)
  1323. {
  1324. struct ftrace_hash *hash;
  1325. struct ftrace_hash *other_hash;
  1326. struct ftrace_page *pg;
  1327. struct dyn_ftrace *rec;
  1328. int count = 0;
  1329. int all = 0;
  1330. /* Only update if the ops has been registered */
  1331. if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
  1332. return;
  1333. /*
  1334. * In the filter_hash case:
  1335. * If the count is zero, we update all records.
  1336. * Otherwise we just update the items in the hash.
  1337. *
  1338. * In the notrace_hash case:
  1339. * We enable the update in the hash.
  1340. * As disabling notrace means enabling the tracing,
  1341. * and enabling notrace means disabling, the inc variable
  1342. * gets inversed.
  1343. */
  1344. if (filter_hash) {
  1345. hash = ops->func_hash->filter_hash;
  1346. other_hash = ops->func_hash->notrace_hash;
  1347. if (ftrace_hash_empty(hash))
  1348. all = 1;
  1349. } else {
  1350. inc = !inc;
  1351. hash = ops->func_hash->notrace_hash;
  1352. other_hash = ops->func_hash->filter_hash;
  1353. /*
  1354. * If the notrace hash has no items,
  1355. * then there's nothing to do.
  1356. */
  1357. if (ftrace_hash_empty(hash))
  1358. return;
  1359. }
  1360. do_for_each_ftrace_rec(pg, rec) {
  1361. int in_other_hash = 0;
  1362. int in_hash = 0;
  1363. int match = 0;
  1364. if (all) {
  1365. /*
  1366. * Only the filter_hash affects all records.
  1367. * Update if the record is not in the notrace hash.
  1368. */
  1369. if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
  1370. match = 1;
  1371. } else {
  1372. in_hash = !!ftrace_lookup_ip(hash, rec->ip);
  1373. in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
  1374. /*
  1375. * If filter_hash is set, we want to match all functions
  1376. * that are in the hash but not in the other hash.
  1377. *
  1378. * If filter_hash is not set, then we are decrementing.
  1379. * That means we match anything that is in the hash
  1380. * and also in the other_hash. That is, we need to turn
  1381. * off functions in the other hash because they are disabled
  1382. * by this hash.
  1383. */
  1384. if (filter_hash && in_hash && !in_other_hash)
  1385. match = 1;
  1386. else if (!filter_hash && in_hash &&
  1387. (in_other_hash || ftrace_hash_empty(other_hash)))
  1388. match = 1;
  1389. }
  1390. if (!match)
  1391. continue;
  1392. if (inc) {
  1393. rec->flags++;
  1394. if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
  1395. return;
  1396. /*
  1397. * If there's only a single callback registered to a
  1398. * function, and the ops has a trampoline registered
  1399. * for it, then we can call it directly.
  1400. */
  1401. if (ftrace_rec_count(rec) == 1 && ops->trampoline)
  1402. rec->flags |= FTRACE_FL_TRAMP;
  1403. else
  1404. /*
  1405. * If we are adding another function callback
  1406. * to this function, and the previous had a
  1407. * custom trampoline in use, then we need to go
  1408. * back to the default trampoline.
  1409. */
  1410. rec->flags &= ~FTRACE_FL_TRAMP;
  1411. /*
  1412. * If any ops wants regs saved for this function
  1413. * then all ops will get saved regs.
  1414. */
  1415. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
  1416. rec->flags |= FTRACE_FL_REGS;
  1417. } else {
  1418. if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
  1419. return;
  1420. rec->flags--;
  1421. /*
  1422. * If the rec had REGS enabled and the ops that is
  1423. * being removed had REGS set, then see if there is
  1424. * still any ops for this record that wants regs.
  1425. * If not, we can stop recording them.
  1426. */
  1427. if (ftrace_rec_count(rec) > 0 &&
  1428. rec->flags & FTRACE_FL_REGS &&
  1429. ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
  1430. if (!test_rec_ops_needs_regs(rec))
  1431. rec->flags &= ~FTRACE_FL_REGS;
  1432. }
  1433. /*
  1434. * If the rec had TRAMP enabled, then it needs to
  1435. * be cleared. As TRAMP can only be enabled iff
  1436. * there is only a single ops attached to it.
  1437. * In otherwords, always disable it on decrementing.
  1438. * In the future, we may set it if rec count is
  1439. * decremented to one, and the ops that is left
  1440. * has a trampoline.
  1441. */
  1442. rec->flags &= ~FTRACE_FL_TRAMP;
  1443. /*
  1444. * flags will be cleared in ftrace_check_record()
  1445. * if rec count is zero.
  1446. */
  1447. }
  1448. count++;
  1449. /* Shortcut, if we handled all records, we are done. */
  1450. if (!all && count == hash->count)
  1451. return;
  1452. } while_for_each_ftrace_rec();
  1453. }
  1454. static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
  1455. int filter_hash)
  1456. {
  1457. __ftrace_hash_rec_update(ops, filter_hash, 0);
  1458. }
  1459. static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
  1460. int filter_hash)
  1461. {
  1462. __ftrace_hash_rec_update(ops, filter_hash, 1);
  1463. }
  1464. static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
  1465. int filter_hash, int inc)
  1466. {
  1467. struct ftrace_ops *op;
  1468. __ftrace_hash_rec_update(ops, filter_hash, inc);
  1469. if (ops->func_hash != &global_ops.local_hash)
  1470. return;
  1471. /*
  1472. * If the ops shares the global_ops hash, then we need to update
  1473. * all ops that are enabled and use this hash.
  1474. */
  1475. do_for_each_ftrace_op(op, ftrace_ops_list) {
  1476. /* Already done */
  1477. if (op == ops)
  1478. continue;
  1479. if (op->func_hash == &global_ops.local_hash)
  1480. __ftrace_hash_rec_update(op, filter_hash, inc);
  1481. } while_for_each_ftrace_op(op);
  1482. }
  1483. static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
  1484. int filter_hash)
  1485. {
  1486. ftrace_hash_rec_update_modify(ops, filter_hash, 0);
  1487. }
  1488. static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
  1489. int filter_hash)
  1490. {
  1491. ftrace_hash_rec_update_modify(ops, filter_hash, 1);
  1492. }
  1493. /*
  1494. * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
  1495. * or no-needed to update, -EBUSY if it detects a conflict of the flag
  1496. * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
  1497. * Note that old_hash and new_hash has below meanings
  1498. * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
  1499. * - If the hash is EMPTY_HASH, it hits nothing
  1500. * - Anything else hits the recs which match the hash entries.
  1501. */
  1502. static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
  1503. struct ftrace_hash *old_hash,
  1504. struct ftrace_hash *new_hash)
  1505. {
  1506. struct ftrace_page *pg;
  1507. struct dyn_ftrace *rec, *end = NULL;
  1508. int in_old, in_new;
  1509. /* Only update if the ops has been registered */
  1510. if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
  1511. return 0;
  1512. if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
  1513. return 0;
  1514. /*
  1515. * Since the IPMODIFY is a very address sensitive action, we do not
  1516. * allow ftrace_ops to set all functions to new hash.
  1517. */
  1518. if (!new_hash || !old_hash)
  1519. return -EINVAL;
  1520. /* Update rec->flags */
  1521. do_for_each_ftrace_rec(pg, rec) {
  1522. /* We need to update only differences of filter_hash */
  1523. in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
  1524. in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
  1525. if (in_old == in_new)
  1526. continue;
  1527. if (in_new) {
  1528. /* New entries must ensure no others are using it */
  1529. if (rec->flags & FTRACE_FL_IPMODIFY)
  1530. goto rollback;
  1531. rec->flags |= FTRACE_FL_IPMODIFY;
  1532. } else /* Removed entry */
  1533. rec->flags &= ~FTRACE_FL_IPMODIFY;
  1534. } while_for_each_ftrace_rec();
  1535. return 0;
  1536. rollback:
  1537. end = rec;
  1538. /* Roll back what we did above */
  1539. do_for_each_ftrace_rec(pg, rec) {
  1540. if (rec == end)
  1541. goto err_out;
  1542. in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
  1543. in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
  1544. if (in_old == in_new)
  1545. continue;
  1546. if (in_new)
  1547. rec->flags &= ~FTRACE_FL_IPMODIFY;
  1548. else
  1549. rec->flags |= FTRACE_FL_IPMODIFY;
  1550. } while_for_each_ftrace_rec();
  1551. err_out:
  1552. return -EBUSY;
  1553. }
  1554. static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
  1555. {
  1556. struct ftrace_hash *hash = ops->func_hash->filter_hash;
  1557. if (ftrace_hash_empty(hash))
  1558. hash = NULL;
  1559. return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
  1560. }
  1561. /* Disabling always succeeds */
  1562. static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
  1563. {
  1564. struct ftrace_hash *hash = ops->func_hash->filter_hash;
  1565. if (ftrace_hash_empty(hash))
  1566. hash = NULL;
  1567. __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
  1568. }
  1569. static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
  1570. struct ftrace_hash *new_hash)
  1571. {
  1572. struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
  1573. if (ftrace_hash_empty(old_hash))
  1574. old_hash = NULL;
  1575. if (ftrace_hash_empty(new_hash))
  1576. new_hash = NULL;
  1577. return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
  1578. }
  1579. static void print_ip_ins(const char *fmt, unsigned char *p)
  1580. {
  1581. int i;
  1582. printk(KERN_CONT "%s", fmt);
  1583. for (i = 0; i < MCOUNT_INSN_SIZE; i++)
  1584. printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
  1585. }
  1586. static struct ftrace_ops *
  1587. ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
  1588. /**
  1589. * ftrace_bug - report and shutdown function tracer
  1590. * @failed: The failed type (EFAULT, EINVAL, EPERM)
  1591. * @rec: The record that failed
  1592. *
  1593. * The arch code that enables or disables the function tracing
  1594. * can call ftrace_bug() when it has detected a problem in
  1595. * modifying the code. @failed should be one of either:
  1596. * EFAULT - if the problem happens on reading the @ip address
  1597. * EINVAL - if what is read at @ip is not what was expected
  1598. * EPERM - if the problem happens on writting to the @ip address
  1599. */
  1600. void ftrace_bug(int failed, struct dyn_ftrace *rec)
  1601. {
  1602. unsigned long ip = rec ? rec->ip : 0;
  1603. switch (failed) {
  1604. case -EFAULT:
  1605. FTRACE_WARN_ON_ONCE(1);
  1606. pr_info("ftrace faulted on modifying ");
  1607. print_ip_sym(ip);
  1608. break;
  1609. case -EINVAL:
  1610. FTRACE_WARN_ON_ONCE(1);
  1611. pr_info("ftrace failed to modify ");
  1612. print_ip_sym(ip);
  1613. print_ip_ins(" actual: ", (unsigned char *)ip);
  1614. pr_cont("\n");
  1615. break;
  1616. case -EPERM:
  1617. FTRACE_WARN_ON_ONCE(1);
  1618. pr_info("ftrace faulted on writing ");
  1619. print_ip_sym(ip);
  1620. break;
  1621. default:
  1622. FTRACE_WARN_ON_ONCE(1);
  1623. pr_info("ftrace faulted on unknown error ");
  1624. print_ip_sym(ip);
  1625. }
  1626. if (rec) {
  1627. struct ftrace_ops *ops = NULL;
  1628. pr_info("ftrace record flags: %lx\n", rec->flags);
  1629. pr_cont(" (%ld)%s", ftrace_rec_count(rec),
  1630. rec->flags & FTRACE_FL_REGS ? " R" : " ");
  1631. if (rec->flags & FTRACE_FL_TRAMP_EN) {
  1632. ops = ftrace_find_tramp_ops_any(rec);
  1633. if (ops)
  1634. pr_cont("\ttramp: %pS",
  1635. (void *)ops->trampoline);
  1636. else
  1637. pr_cont("\ttramp: ERROR!");
  1638. }
  1639. ip = ftrace_get_addr_curr(rec);
  1640. pr_cont(" expected tramp: %lx\n", ip);
  1641. }
  1642. }
  1643. static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
  1644. {
  1645. unsigned long flag = 0UL;
  1646. /*
  1647. * If we are updating calls:
  1648. *
  1649. * If the record has a ref count, then we need to enable it
  1650. * because someone is using it.
  1651. *
  1652. * Otherwise we make sure its disabled.
  1653. *
  1654. * If we are disabling calls, then disable all records that
  1655. * are enabled.
  1656. */
  1657. if (enable && ftrace_rec_count(rec))
  1658. flag = FTRACE_FL_ENABLED;
  1659. /*
  1660. * If enabling and the REGS flag does not match the REGS_EN, or
  1661. * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
  1662. * this record. Set flags to fail the compare against ENABLED.
  1663. */
  1664. if (flag) {
  1665. if (!(rec->flags & FTRACE_FL_REGS) !=
  1666. !(rec->flags & FTRACE_FL_REGS_EN))
  1667. flag |= FTRACE_FL_REGS;
  1668. if (!(rec->flags & FTRACE_FL_TRAMP) !=
  1669. !(rec->flags & FTRACE_FL_TRAMP_EN))
  1670. flag |= FTRACE_FL_TRAMP;
  1671. }
  1672. /* If the state of this record hasn't changed, then do nothing */
  1673. if ((rec->flags & FTRACE_FL_ENABLED) == flag)
  1674. return FTRACE_UPDATE_IGNORE;
  1675. if (flag) {
  1676. /* Save off if rec is being enabled (for return value) */
  1677. flag ^= rec->flags & FTRACE_FL_ENABLED;
  1678. if (update) {
  1679. rec->flags |= FTRACE_FL_ENABLED;
  1680. if (flag & FTRACE_FL_REGS) {
  1681. if (rec->flags & FTRACE_FL_REGS)
  1682. rec->flags |= FTRACE_FL_REGS_EN;
  1683. else
  1684. rec->flags &= ~FTRACE_FL_REGS_EN;
  1685. }
  1686. if (flag & FTRACE_FL_TRAMP) {
  1687. if (rec->flags & FTRACE_FL_TRAMP)
  1688. rec->flags |= FTRACE_FL_TRAMP_EN;
  1689. else
  1690. rec->flags &= ~FTRACE_FL_TRAMP_EN;
  1691. }
  1692. }
  1693. /*
  1694. * If this record is being updated from a nop, then
  1695. * return UPDATE_MAKE_CALL.
  1696. * Otherwise,
  1697. * return UPDATE_MODIFY_CALL to tell the caller to convert
  1698. * from the save regs, to a non-save regs function or
  1699. * vice versa, or from a trampoline call.
  1700. */
  1701. if (flag & FTRACE_FL_ENABLED)
  1702. return FTRACE_UPDATE_MAKE_CALL;
  1703. return FTRACE_UPDATE_MODIFY_CALL;
  1704. }
  1705. if (update) {
  1706. /* If there's no more users, clear all flags */
  1707. if (!ftrace_rec_count(rec))
  1708. rec->flags = 0;
  1709. else
  1710. /* Just disable the record (keep REGS state) */
  1711. rec->flags &= ~FTRACE_FL_ENABLED;
  1712. }
  1713. return FTRACE_UPDATE_MAKE_NOP;
  1714. }
  1715. /**
  1716. * ftrace_update_record, set a record that now is tracing or not
  1717. * @rec: the record to update
  1718. * @enable: set to 1 if the record is tracing, zero to force disable
  1719. *
  1720. * The records that represent all functions that can be traced need
  1721. * to be updated when tracing has been enabled.
  1722. */
  1723. int ftrace_update_record(struct dyn_ftrace *rec, int enable)
  1724. {
  1725. return ftrace_check_record(rec, enable, 1);
  1726. }
  1727. /**
  1728. * ftrace_test_record, check if the record has been enabled or not
  1729. * @rec: the record to test
  1730. * @enable: set to 1 to check if enabled, 0 if it is disabled
  1731. *
  1732. * The arch code may need to test if a record is already set to
  1733. * tracing to determine how to modify the function code that it
  1734. * represents.
  1735. */
  1736. int ftrace_test_record(struct dyn_ftrace *rec, int enable)
  1737. {
  1738. return ftrace_check_record(rec, enable, 0);
  1739. }
  1740. static struct ftrace_ops *
  1741. ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
  1742. {
  1743. struct ftrace_ops *op;
  1744. unsigned long ip = rec->ip;
  1745. do_for_each_ftrace_op(op, ftrace_ops_list) {
  1746. if (!op->trampoline)
  1747. continue;
  1748. if (hash_contains_ip(ip, op->func_hash))
  1749. return op;
  1750. } while_for_each_ftrace_op(op);
  1751. return NULL;
  1752. }
  1753. static struct ftrace_ops *
  1754. ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
  1755. {
  1756. struct ftrace_ops *op;
  1757. unsigned long ip = rec->ip;
  1758. /*
  1759. * Need to check removed ops first.
  1760. * If they are being removed, and this rec has a tramp,
  1761. * and this rec is in the ops list, then it would be the
  1762. * one with the tramp.
  1763. */
  1764. if (removed_ops) {
  1765. if (hash_contains_ip(ip, &removed_ops->old_hash))
  1766. return removed_ops;
  1767. }
  1768. /*
  1769. * Need to find the current trampoline for a rec.
  1770. * Now, a trampoline is only attached to a rec if there
  1771. * was a single 'ops' attached to it. But this can be called
  1772. * when we are adding another op to the rec or removing the
  1773. * current one. Thus, if the op is being added, we can
  1774. * ignore it because it hasn't attached itself to the rec
  1775. * yet.
  1776. *
  1777. * If an ops is being modified (hooking to different functions)
  1778. * then we don't care about the new functions that are being
  1779. * added, just the old ones (that are probably being removed).
  1780. *
  1781. * If we are adding an ops to a function that already is using
  1782. * a trampoline, it needs to be removed (trampolines are only
  1783. * for single ops connected), then an ops that is not being
  1784. * modified also needs to be checked.
  1785. */
  1786. do_for_each_ftrace_op(op, ftrace_ops_list) {
  1787. if (!op->trampoline)
  1788. continue;
  1789. /*
  1790. * If the ops is being added, it hasn't gotten to
  1791. * the point to be removed from this tree yet.
  1792. */
  1793. if (op->flags & FTRACE_OPS_FL_ADDING)
  1794. continue;
  1795. /*
  1796. * If the ops is being modified and is in the old
  1797. * hash, then it is probably being removed from this
  1798. * function.
  1799. */
  1800. if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
  1801. hash_contains_ip(ip, &op->old_hash))
  1802. return op;
  1803. /*
  1804. * If the ops is not being added or modified, and it's
  1805. * in its normal filter hash, then this must be the one
  1806. * we want!
  1807. */
  1808. if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
  1809. hash_contains_ip(ip, op->func_hash))
  1810. return op;
  1811. } while_for_each_ftrace_op(op);
  1812. return NULL;
  1813. }
  1814. static struct ftrace_ops *
  1815. ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
  1816. {
  1817. struct ftrace_ops *op;
  1818. unsigned long ip = rec->ip;
  1819. do_for_each_ftrace_op(op, ftrace_ops_list) {
  1820. /* pass rec in as regs to have non-NULL val */
  1821. if (hash_contains_ip(ip, op->func_hash))
  1822. return op;
  1823. } while_for_each_ftrace_op(op);
  1824. return NULL;
  1825. }
  1826. /**
  1827. * ftrace_get_addr_new - Get the call address to set to
  1828. * @rec: The ftrace record descriptor
  1829. *
  1830. * If the record has the FTRACE_FL_REGS set, that means that it
  1831. * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
  1832. * is not not set, then it wants to convert to the normal callback.
  1833. *
  1834. * Returns the address of the trampoline to set to
  1835. */
  1836. unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
  1837. {
  1838. struct ftrace_ops *ops;
  1839. /* Trampolines take precedence over regs */
  1840. if (rec->flags & FTRACE_FL_TRAMP) {
  1841. ops = ftrace_find_tramp_ops_new(rec);
  1842. if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
  1843. pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
  1844. (void *)rec->ip, (void *)rec->ip, rec->flags);
  1845. /* Ftrace is shutting down, return anything */
  1846. return (unsigned long)FTRACE_ADDR;
  1847. }
  1848. return ops->trampoline;
  1849. }
  1850. if (rec->flags & FTRACE_FL_REGS)
  1851. return (unsigned long)FTRACE_REGS_ADDR;
  1852. else
  1853. return (unsigned long)FTRACE_ADDR;
  1854. }
  1855. /**
  1856. * ftrace_get_addr_curr - Get the call address that is already there
  1857. * @rec: The ftrace record descriptor
  1858. *
  1859. * The FTRACE_FL_REGS_EN is set when the record already points to
  1860. * a function that saves all the regs. Basically the '_EN' version
  1861. * represents the current state of the function.
  1862. *
  1863. * Returns the address of the trampoline that is currently being called
  1864. */
  1865. unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
  1866. {
  1867. struct ftrace_ops *ops;
  1868. /* Trampolines take precedence over regs */
  1869. if (rec->flags & FTRACE_FL_TRAMP_EN) {
  1870. ops = ftrace_find_tramp_ops_curr(rec);
  1871. if (FTRACE_WARN_ON(!ops)) {
  1872. pr_warning("Bad trampoline accounting at: %p (%pS)\n",
  1873. (void *)rec->ip, (void *)rec->ip);
  1874. /* Ftrace is shutting down, return anything */
  1875. return (unsigned long)FTRACE_ADDR;
  1876. }
  1877. return ops->trampoline;
  1878. }
  1879. if (rec->flags & FTRACE_FL_REGS_EN)
  1880. return (unsigned long)FTRACE_REGS_ADDR;
  1881. else
  1882. return (unsigned long)FTRACE_ADDR;
  1883. }
  1884. static int
  1885. __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
  1886. {
  1887. unsigned long ftrace_old_addr;
  1888. unsigned long ftrace_addr;
  1889. int ret;
  1890. ftrace_addr = ftrace_get_addr_new(rec);
  1891. /* This needs to be done before we call ftrace_update_record */
  1892. ftrace_old_addr = ftrace_get_addr_curr(rec);
  1893. ret = ftrace_update_record(rec, enable);
  1894. switch (ret) {
  1895. case FTRACE_UPDATE_IGNORE:
  1896. return 0;
  1897. case FTRACE_UPDATE_MAKE_CALL:
  1898. return ftrace_make_call(rec, ftrace_addr);
  1899. case FTRACE_UPDATE_MAKE_NOP:
  1900. return ftrace_make_nop(NULL, rec, ftrace_old_addr);
  1901. case FTRACE_UPDATE_MODIFY_CALL:
  1902. return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
  1903. }
  1904. return -1; /* unknow ftrace bug */
  1905. }
  1906. void __weak ftrace_replace_code(int enable)
  1907. {
  1908. struct dyn_ftrace *rec;
  1909. struct ftrace_page *pg;
  1910. int failed;
  1911. if (unlikely(ftrace_disabled))
  1912. return;
  1913. do_for_each_ftrace_rec(pg, rec) {
  1914. failed = __ftrace_replace_code(rec, enable);
  1915. if (failed) {
  1916. ftrace_bug(failed, rec);
  1917. /* Stop processing */
  1918. return;
  1919. }
  1920. } while_for_each_ftrace_rec();
  1921. }
  1922. struct ftrace_rec_iter {
  1923. struct ftrace_page *pg;
  1924. int index;
  1925. };
  1926. /**
  1927. * ftrace_rec_iter_start, start up iterating over traced functions
  1928. *
  1929. * Returns an iterator handle that is used to iterate over all
  1930. * the records that represent address locations where functions
  1931. * are traced.
  1932. *
  1933. * May return NULL if no records are available.
  1934. */
  1935. struct ftrace_rec_iter *ftrace_rec_iter_start(void)
  1936. {
  1937. /*
  1938. * We only use a single iterator.
  1939. * Protected by the ftrace_lock mutex.
  1940. */
  1941. static struct ftrace_rec_iter ftrace_rec_iter;
  1942. struct ftrace_rec_iter *iter = &ftrace_rec_iter;
  1943. iter->pg = ftrace_pages_start;
  1944. iter->index = 0;
  1945. /* Could have empty pages */
  1946. while (iter->pg && !iter->pg->index)
  1947. iter->pg = iter->pg->next;
  1948. if (!iter->pg)
  1949. return NULL;
  1950. return iter;
  1951. }
  1952. /**
  1953. * ftrace_rec_iter_next, get the next record to process.
  1954. * @iter: The handle to the iterator.
  1955. *
  1956. * Returns the next iterator after the given iterator @iter.
  1957. */
  1958. struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
  1959. {
  1960. iter->index++;
  1961. if (iter->index >= iter->pg->index) {
  1962. iter->pg = iter->pg->next;
  1963. iter->index = 0;
  1964. /* Could have empty pages */
  1965. while (iter->pg && !iter->pg->index)
  1966. iter->pg = iter->pg->next;
  1967. }
  1968. if (!iter->pg)
  1969. return NULL;
  1970. return iter;
  1971. }
  1972. /**
  1973. * ftrace_rec_iter_record, get the record at the iterator location
  1974. * @iter: The current iterator location
  1975. *
  1976. * Returns the record that the current @iter is at.
  1977. */
  1978. struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
  1979. {
  1980. return &iter->pg->records[iter->index];
  1981. }
  1982. static int
  1983. ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
  1984. {
  1985. int ret;
  1986. if (unlikely(ftrace_disabled))
  1987. return 0;
  1988. ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
  1989. if (ret) {
  1990. ftrace_bug(ret, rec);
  1991. return 0;
  1992. }
  1993. return 1;
  1994. }
  1995. /*
  1996. * archs can override this function if they must do something
  1997. * before the modifying code is performed.
  1998. */
  1999. int __weak ftrace_arch_code_modify_prepare(void)
  2000. {
  2001. return 0;
  2002. }
  2003. /*
  2004. * archs can override this function if they must do something
  2005. * after the modifying code is performed.
  2006. */
  2007. int __weak ftrace_arch_code_modify_post_process(void)
  2008. {
  2009. return 0;
  2010. }
  2011. void ftrace_modify_all_code(int command)
  2012. {
  2013. int update = command & FTRACE_UPDATE_TRACE_FUNC;
  2014. int err = 0;
  2015. /*
  2016. * If the ftrace_caller calls a ftrace_ops func directly,
  2017. * we need to make sure that it only traces functions it
  2018. * expects to trace. When doing the switch of functions,
  2019. * we need to update to the ftrace_ops_list_func first
  2020. * before the transition between old and new calls are set,
  2021. * as the ftrace_ops_list_func will check the ops hashes
  2022. * to make sure the ops are having the right functions
  2023. * traced.
  2024. */
  2025. if (update) {
  2026. err = ftrace_update_ftrace_func(ftrace_ops_list_func);
  2027. if (FTRACE_WARN_ON(err))
  2028. return;
  2029. }
  2030. if (command & FTRACE_UPDATE_CALLS)
  2031. ftrace_replace_code(1);
  2032. else if (command & FTRACE_DISABLE_CALLS)
  2033. ftrace_replace_code(0);
  2034. if (update && ftrace_trace_function != ftrace_ops_list_func) {
  2035. function_trace_op = set_function_trace_op;
  2036. smp_wmb();
  2037. /* If irqs are disabled, we are in stop machine */
  2038. if (!irqs_disabled())
  2039. smp_call_function(ftrace_sync_ipi, NULL, 1);
  2040. err = ftrace_update_ftrace_func(ftrace_trace_function);
  2041. if (FTRACE_WARN_ON(err))
  2042. return;
  2043. }
  2044. if (command & FTRACE_START_FUNC_RET)
  2045. err = ftrace_enable_ftrace_graph_caller();
  2046. else if (command & FTRACE_STOP_FUNC_RET)
  2047. err = ftrace_disable_ftrace_graph_caller();
  2048. FTRACE_WARN_ON(err);
  2049. }
  2050. static int __ftrace_modify_code(void *data)
  2051. {
  2052. int *command = data;
  2053. ftrace_modify_all_code(*command);
  2054. return 0;
  2055. }
  2056. /**
  2057. * ftrace_run_stop_machine, go back to the stop machine method
  2058. * @command: The command to tell ftrace what to do
  2059. *
  2060. * If an arch needs to fall back to the stop machine method, the
  2061. * it can call this function.
  2062. */
  2063. void ftrace_run_stop_machine(int command)
  2064. {
  2065. stop_machine(__ftrace_modify_code, &command, NULL);
  2066. }
  2067. /**
  2068. * arch_ftrace_update_code, modify the code to trace or not trace
  2069. * @command: The command that needs to be done
  2070. *
  2071. * Archs can override this function if it does not need to
  2072. * run stop_machine() to modify code.
  2073. */
  2074. void __weak arch_ftrace_update_code(int command)
  2075. {
  2076. ftrace_run_stop_machine(command);
  2077. }
  2078. static void ftrace_run_update_code(int command)
  2079. {
  2080. int ret;
  2081. ret = ftrace_arch_code_modify_prepare();
  2082. FTRACE_WARN_ON(ret);
  2083. if (ret)
  2084. return;
  2085. /*
  2086. * By default we use stop_machine() to modify the code.
  2087. * But archs can do what ever they want as long as it
  2088. * is safe. The stop_machine() is the safest, but also
  2089. * produces the most overhead.
  2090. */
  2091. arch_ftrace_update_code(command);
  2092. ret = ftrace_arch_code_modify_post_process();
  2093. FTRACE_WARN_ON(ret);
  2094. }
  2095. static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
  2096. struct ftrace_ops_hash *old_hash)
  2097. {
  2098. ops->flags |= FTRACE_OPS_FL_MODIFYING;
  2099. ops->old_hash.filter_hash = old_hash->filter_hash;
  2100. ops->old_hash.notrace_hash = old_hash->notrace_hash;
  2101. ftrace_run_update_code(command);
  2102. ops->old_hash.filter_hash = NULL;
  2103. ops->old_hash.notrace_hash = NULL;
  2104. ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
  2105. }
  2106. static ftrace_func_t saved_ftrace_func;
  2107. static int ftrace_start_up;
  2108. void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
  2109. {
  2110. }
  2111. static void control_ops_free(struct ftrace_ops *ops)
  2112. {
  2113. free_percpu(ops->disabled);
  2114. }
  2115. static void ftrace_startup_enable(int command)
  2116. {
  2117. if (saved_ftrace_func != ftrace_trace_function) {
  2118. saved_ftrace_func = ftrace_trace_function;
  2119. command |= FTRACE_UPDATE_TRACE_FUNC;
  2120. }
  2121. if (!command || !ftrace_enabled)
  2122. return;
  2123. ftrace_run_update_code(command);
  2124. }
  2125. static void ftrace_startup_all(int command)
  2126. {
  2127. update_all_ops = true;
  2128. ftrace_startup_enable(command);
  2129. update_all_ops = false;
  2130. }
  2131. static int ftrace_startup(struct ftrace_ops *ops, int command)
  2132. {
  2133. int ret;
  2134. if (unlikely(ftrace_disabled))
  2135. return -ENODEV;
  2136. ret = __register_ftrace_function(ops);
  2137. if (ret)
  2138. return ret;
  2139. ftrace_start_up++;
  2140. command |= FTRACE_UPDATE_CALLS;
  2141. /*
  2142. * Note that ftrace probes uses this to start up
  2143. * and modify functions it will probe. But we still
  2144. * set the ADDING flag for modification, as probes
  2145. * do not have trampolines. If they add them in the
  2146. * future, then the probes will need to distinguish
  2147. * between adding and updating probes.
  2148. */
  2149. ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
  2150. ret = ftrace_hash_ipmodify_enable(ops);
  2151. if (ret < 0) {
  2152. /* Rollback registration process */
  2153. __unregister_ftrace_function(ops);
  2154. ftrace_start_up--;
  2155. ops->flags &= ~FTRACE_OPS_FL_ENABLED;
  2156. return ret;
  2157. }
  2158. ftrace_hash_rec_enable(ops, 1);
  2159. ftrace_startup_enable(command);
  2160. ops->flags &= ~FTRACE_OPS_FL_ADDING;
  2161. return 0;
  2162. }
  2163. static int ftrace_shutdown(struct ftrace_ops *ops, int command)
  2164. {
  2165. int ret;
  2166. if (unlikely(ftrace_disabled))
  2167. return -ENODEV;
  2168. ret = __unregister_ftrace_function(ops);
  2169. if (ret)
  2170. return ret;
  2171. ftrace_start_up--;
  2172. /*
  2173. * Just warn in case of unbalance, no need to kill ftrace, it's not
  2174. * critical but the ftrace_call callers may be never nopped again after
  2175. * further ftrace uses.
  2176. */
  2177. WARN_ON_ONCE(ftrace_start_up < 0);
  2178. /* Disabling ipmodify never fails */
  2179. ftrace_hash_ipmodify_disable(ops);
  2180. ftrace_hash_rec_disable(ops, 1);
  2181. ops->flags &= ~FTRACE_OPS_FL_ENABLED;
  2182. command |= FTRACE_UPDATE_CALLS;
  2183. if (saved_ftrace_func != ftrace_trace_function) {
  2184. saved_ftrace_func = ftrace_trace_function;
  2185. command |= FTRACE_UPDATE_TRACE_FUNC;
  2186. }
  2187. if (!command || !ftrace_enabled) {
  2188. /*
  2189. * If these are control ops, they still need their
  2190. * per_cpu field freed. Since, function tracing is
  2191. * not currently active, we can just free them
  2192. * without synchronizing all CPUs.
  2193. */
  2194. if (ops->flags & FTRACE_OPS_FL_CONTROL)
  2195. control_ops_free(ops);
  2196. return 0;
  2197. }
  2198. /*
  2199. * If the ops uses a trampoline, then it needs to be
  2200. * tested first on update.
  2201. */
  2202. ops->flags |= FTRACE_OPS_FL_REMOVING;
  2203. removed_ops = ops;
  2204. /* The trampoline logic checks the old hashes */
  2205. ops->old_hash.filter_hash = ops->func_hash->filter_hash;
  2206. ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
  2207. ftrace_run_update_code(command);
  2208. /*
  2209. * If there's no more ops registered with ftrace, run a
  2210. * sanity check to make sure all rec flags are cleared.
  2211. */
  2212. if (ftrace_ops_list == &ftrace_list_end) {
  2213. struct ftrace_page *pg;
  2214. struct dyn_ftrace *rec;
  2215. do_for_each_ftrace_rec(pg, rec) {
  2216. if (FTRACE_WARN_ON_ONCE(rec->flags))
  2217. pr_warn(" %pS flags:%lx\n",
  2218. (void *)rec->ip, rec->flags);
  2219. } while_for_each_ftrace_rec();
  2220. }
  2221. ops->old_hash.filter_hash = NULL;
  2222. ops->old_hash.notrace_hash = NULL;
  2223. removed_ops = NULL;
  2224. ops->flags &= ~FTRACE_OPS_FL_REMOVING;
  2225. /*
  2226. * Dynamic ops may be freed, we must make sure that all
  2227. * callers are done before leaving this function.
  2228. * The same goes for freeing the per_cpu data of the control
  2229. * ops.
  2230. *
  2231. * Again, normal synchronize_sched() is not good enough.
  2232. * We need to do a hard force of sched synchronization.
  2233. * This is because we use preempt_disable() to do RCU, but
  2234. * the function tracers can be called where RCU is not watching
  2235. * (like before user_exit()). We can not rely on the RCU
  2236. * infrastructure to do the synchronization, thus we must do it
  2237. * ourselves.
  2238. */
  2239. if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
  2240. schedule_on_each_cpu(ftrace_sync);
  2241. arch_ftrace_trampoline_free(ops);
  2242. if (ops->flags & FTRACE_OPS_FL_CONTROL)
  2243. control_ops_free(ops);
  2244. }
  2245. return 0;
  2246. }
  2247. static void ftrace_startup_sysctl(void)
  2248. {
  2249. if (unlikely(ftrace_disabled))
  2250. return;
  2251. /* Force update next time */
  2252. saved_ftrace_func = NULL;
  2253. /* ftrace_start_up is true if we want ftrace running */
  2254. if (ftrace_start_up)
  2255. ftrace_run_update_code(FTRACE_UPDATE_CALLS);
  2256. }
  2257. static void ftrace_shutdown_sysctl(void)
  2258. {
  2259. if (unlikely(ftrace_disabled))
  2260. return;
  2261. /* ftrace_start_up is true if ftrace is running */
  2262. if (ftrace_start_up)
  2263. ftrace_run_update_code(FTRACE_DISABLE_CALLS);
  2264. }
  2265. static cycle_t ftrace_update_time;
  2266. unsigned long ftrace_update_tot_cnt;
  2267. static inline int ops_traces_mod(struct ftrace_ops *ops)
  2268. {
  2269. /*
  2270. * Filter_hash being empty will default to trace module.
  2271. * But notrace hash requires a test of individual module functions.
  2272. */
  2273. return ftrace_hash_empty(ops->func_hash->filter_hash) &&
  2274. ftrace_hash_empty(ops->func_hash->notrace_hash);
  2275. }
  2276. /*
  2277. * Check if the current ops references the record.
  2278. *
  2279. * If the ops traces all functions, then it was already accounted for.
  2280. * If the ops does not trace the current record function, skip it.
  2281. * If the ops ignores the function via notrace filter, skip it.
  2282. */
  2283. static inline bool
  2284. ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
  2285. {
  2286. /* If ops isn't enabled, ignore it */
  2287. if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
  2288. return 0;
  2289. /* If ops traces all mods, we already accounted for it */
  2290. if (ops_traces_mod(ops))
  2291. return 0;
  2292. /* The function must be in the filter */
  2293. if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
  2294. !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
  2295. return 0;
  2296. /* If in notrace hash, we ignore it too */
  2297. if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
  2298. return 0;
  2299. return 1;
  2300. }
  2301. static int referenced_filters(struct dyn_ftrace *rec)
  2302. {
  2303. struct ftrace_ops *ops;
  2304. int cnt = 0;
  2305. for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
  2306. if (ops_references_rec(ops, rec))
  2307. cnt++;
  2308. }
  2309. return cnt;
  2310. }
  2311. static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
  2312. {
  2313. struct ftrace_page *pg;
  2314. struct dyn_ftrace *p;
  2315. cycle_t start, stop;
  2316. unsigned long update_cnt = 0;
  2317. unsigned long ref = 0;
  2318. bool test = false;
  2319. int i;
  2320. /*
  2321. * When adding a module, we need to check if tracers are
  2322. * currently enabled and if they are set to trace all functions.
  2323. * If they are, we need to enable the module functions as well
  2324. * as update the reference counts for those function records.
  2325. */
  2326. if (mod) {
  2327. struct ftrace_ops *ops;
  2328. for (ops = ftrace_ops_list;
  2329. ops != &ftrace_list_end; ops = ops->next) {
  2330. if (ops->flags & FTRACE_OPS_FL_ENABLED) {
  2331. if (ops_traces_mod(ops))
  2332. ref++;
  2333. else
  2334. test = true;
  2335. }
  2336. }
  2337. }
  2338. start = ftrace_now(raw_smp_processor_id());
  2339. for (pg = new_pgs; pg; pg = pg->next) {
  2340. for (i = 0; i < pg->index; i++) {
  2341. int cnt = ref;
  2342. /* If something went wrong, bail without enabling anything */
  2343. if (unlikely(ftrace_disabled))
  2344. return -1;
  2345. p = &pg->records[i];
  2346. if (test)
  2347. cnt += referenced_filters(p);
  2348. p->flags = cnt;
  2349. /*
  2350. * Do the initial record conversion from mcount jump
  2351. * to the NOP instructions.
  2352. */
  2353. if (!ftrace_code_disable(mod, p))
  2354. break;
  2355. update_cnt++;
  2356. /*
  2357. * If the tracing is enabled, go ahead and enable the record.
  2358. *
  2359. * The reason not to enable the record immediatelly is the
  2360. * inherent check of ftrace_make_nop/ftrace_make_call for
  2361. * correct previous instructions. Making first the NOP
  2362. * conversion puts the module to the correct state, thus
  2363. * passing the ftrace_make_call check.
  2364. */
  2365. if (ftrace_start_up && cnt) {
  2366. int failed = __ftrace_replace_code(p, 1);
  2367. if (failed)
  2368. ftrace_bug(failed, p);
  2369. }
  2370. }
  2371. }
  2372. stop = ftrace_now(raw_smp_processor_id());
  2373. ftrace_update_time = stop - start;
  2374. ftrace_update_tot_cnt += update_cnt;
  2375. return 0;
  2376. }
  2377. static int ftrace_allocate_records(struct ftrace_page *pg, int count)
  2378. {
  2379. int order;
  2380. int cnt;
  2381. if (WARN_ON(!count))
  2382. return -EINVAL;
  2383. order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
  2384. /*
  2385. * We want to fill as much as possible. No more than a page
  2386. * may be empty.
  2387. */
  2388. while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
  2389. order--;
  2390. again:
  2391. pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
  2392. if (!pg->records) {
  2393. /* if we can't allocate this size, try something smaller */
  2394. if (!order)
  2395. return -ENOMEM;
  2396. order >>= 1;
  2397. goto again;
  2398. }
  2399. cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
  2400. pg->size = cnt;
  2401. if (cnt > count)
  2402. cnt = count;
  2403. return cnt;
  2404. }
  2405. static struct ftrace_page *
  2406. ftrace_allocate_pages(unsigned long num_to_init)
  2407. {
  2408. struct ftrace_page *start_pg;
  2409. struct ftrace_page *pg;
  2410. int order;
  2411. int cnt;
  2412. if (!num_to_init)
  2413. return 0;
  2414. start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
  2415. if (!pg)
  2416. return NULL;
  2417. /*
  2418. * Try to allocate as much as possible in one continues
  2419. * location that fills in all of the space. We want to
  2420. * waste as little space as possible.
  2421. */
  2422. for (;;) {
  2423. cnt = ftrace_allocate_records(pg, num_to_init);
  2424. if (cnt < 0)
  2425. goto free_pages;
  2426. num_to_init -= cnt;
  2427. if (!num_to_init)
  2428. break;
  2429. pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
  2430. if (!pg->next)
  2431. goto free_pages;
  2432. pg = pg->next;
  2433. }
  2434. return start_pg;
  2435. free_pages:
  2436. pg = start_pg;
  2437. while (pg) {
  2438. order = get_count_order(pg->size / ENTRIES_PER_PAGE);
  2439. free_pages((unsigned long)pg->records, order);
  2440. start_pg = pg->next;
  2441. kfree(pg);
  2442. pg = start_pg;
  2443. }
  2444. pr_info("ftrace: FAILED to allocate memory for functions\n");
  2445. return NULL;
  2446. }
  2447. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  2448. struct ftrace_iterator {
  2449. loff_t pos;
  2450. loff_t func_pos;
  2451. struct ftrace_page *pg;
  2452. struct dyn_ftrace *func;
  2453. struct ftrace_func_probe *probe;
  2454. struct trace_parser parser;
  2455. struct ftrace_hash *hash;
  2456. struct ftrace_ops *ops;
  2457. int hidx;
  2458. int idx;
  2459. unsigned flags;
  2460. };
  2461. static void *
  2462. t_hash_next(struct seq_file *m, loff_t *pos)
  2463. {
  2464. struct ftrace_iterator *iter = m->private;
  2465. struct hlist_node *hnd = NULL;
  2466. struct hlist_head *hhd;
  2467. (*pos)++;
  2468. iter->pos = *pos;
  2469. if (iter->probe)
  2470. hnd = &iter->probe->node;
  2471. retry:
  2472. if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
  2473. return NULL;
  2474. hhd = &ftrace_func_hash[iter->hidx];
  2475. if (hlist_empty(hhd)) {
  2476. iter->hidx++;
  2477. hnd = NULL;
  2478. goto retry;
  2479. }
  2480. if (!hnd)
  2481. hnd = hhd->first;
  2482. else {
  2483. hnd = hnd->next;
  2484. if (!hnd) {
  2485. iter->hidx++;
  2486. goto retry;
  2487. }
  2488. }
  2489. if (WARN_ON_ONCE(!hnd))
  2490. return NULL;
  2491. iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
  2492. return iter;
  2493. }
  2494. static void *t_hash_start(struct seq_file *m, loff_t *pos)
  2495. {
  2496. struct ftrace_iterator *iter = m->private;
  2497. void *p = NULL;
  2498. loff_t l;
  2499. if (!(iter->flags & FTRACE_ITER_DO_HASH))
  2500. return NULL;
  2501. if (iter->func_pos > *pos)
  2502. return NULL;
  2503. iter->hidx = 0;
  2504. for (l = 0; l <= (*pos - iter->func_pos); ) {
  2505. p = t_hash_next(m, &l);
  2506. if (!p)
  2507. break;
  2508. }
  2509. if (!p)
  2510. return NULL;
  2511. /* Only set this if we have an item */
  2512. iter->flags |= FTRACE_ITER_HASH;
  2513. return iter;
  2514. }
  2515. static int
  2516. t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
  2517. {
  2518. struct ftrace_func_probe *rec;
  2519. rec = iter->probe;
  2520. if (WARN_ON_ONCE(!rec))
  2521. return -EIO;
  2522. if (rec->ops->print)
  2523. return rec->ops->print(m, rec->ip, rec->ops, rec->data);
  2524. seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
  2525. if (rec->data)
  2526. seq_printf(m, ":%p", rec->data);
  2527. seq_putc(m, '\n');
  2528. return 0;
  2529. }
  2530. static void *
  2531. t_next(struct seq_file *m, void *v, loff_t *pos)
  2532. {
  2533. struct ftrace_iterator *iter = m->private;
  2534. struct ftrace_ops *ops = iter->ops;
  2535. struct dyn_ftrace *rec = NULL;
  2536. if (unlikely(ftrace_disabled))
  2537. return NULL;
  2538. if (iter->flags & FTRACE_ITER_HASH)
  2539. return t_hash_next(m, pos);
  2540. (*pos)++;
  2541. iter->pos = iter->func_pos = *pos;
  2542. if (iter->flags & FTRACE_ITER_PRINTALL)
  2543. return t_hash_start(m, pos);
  2544. retry:
  2545. if (iter->idx >= iter->pg->index) {
  2546. if (iter->pg->next) {
  2547. iter->pg = iter->pg->next;
  2548. iter->idx = 0;
  2549. goto retry;
  2550. }
  2551. } else {
  2552. rec = &iter->pg->records[iter->idx++];
  2553. if (((iter->flags & FTRACE_ITER_FILTER) &&
  2554. !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
  2555. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  2556. !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
  2557. ((iter->flags & FTRACE_ITER_ENABLED) &&
  2558. !(rec->flags & FTRACE_FL_ENABLED))) {
  2559. rec = NULL;
  2560. goto retry;
  2561. }
  2562. }
  2563. if (!rec)
  2564. return t_hash_start(m, pos);
  2565. iter->func = rec;
  2566. return iter;
  2567. }
  2568. static void reset_iter_read(struct ftrace_iterator *iter)
  2569. {
  2570. iter->pos = 0;
  2571. iter->func_pos = 0;
  2572. iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
  2573. }
  2574. static void *t_start(struct seq_file *m, loff_t *pos)
  2575. {
  2576. struct ftrace_iterator *iter = m->private;
  2577. struct ftrace_ops *ops = iter->ops;
  2578. void *p = NULL;
  2579. loff_t l;
  2580. mutex_lock(&ftrace_lock);
  2581. if (unlikely(ftrace_disabled))
  2582. return NULL;
  2583. /*
  2584. * If an lseek was done, then reset and start from beginning.
  2585. */
  2586. if (*pos < iter->pos)
  2587. reset_iter_read(iter);
  2588. /*
  2589. * For set_ftrace_filter reading, if we have the filter
  2590. * off, we can short cut and just print out that all
  2591. * functions are enabled.
  2592. */
  2593. if ((iter->flags & FTRACE_ITER_FILTER &&
  2594. ftrace_hash_empty(ops->func_hash->filter_hash)) ||
  2595. (iter->flags & FTRACE_ITER_NOTRACE &&
  2596. ftrace_hash_empty(ops->func_hash->notrace_hash))) {
  2597. if (*pos > 0)
  2598. return t_hash_start(m, pos);
  2599. iter->flags |= FTRACE_ITER_PRINTALL;
  2600. /* reset in case of seek/pread */
  2601. iter->flags &= ~FTRACE_ITER_HASH;
  2602. return iter;
  2603. }
  2604. if (iter->flags & FTRACE_ITER_HASH)
  2605. return t_hash_start(m, pos);
  2606. /*
  2607. * Unfortunately, we need to restart at ftrace_pages_start
  2608. * every time we let go of the ftrace_mutex. This is because
  2609. * those pointers can change without the lock.
  2610. */
  2611. iter->pg = ftrace_pages_start;
  2612. iter->idx = 0;
  2613. for (l = 0; l <= *pos; ) {
  2614. p = t_next(m, p, &l);
  2615. if (!p)
  2616. break;
  2617. }
  2618. if (!p)
  2619. return t_hash_start(m, pos);
  2620. return iter;
  2621. }
  2622. static void t_stop(struct seq_file *m, void *p)
  2623. {
  2624. mutex_unlock(&ftrace_lock);
  2625. }
  2626. void * __weak
  2627. arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
  2628. {
  2629. return NULL;
  2630. }
  2631. static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
  2632. struct dyn_ftrace *rec)
  2633. {
  2634. void *ptr;
  2635. ptr = arch_ftrace_trampoline_func(ops, rec);
  2636. if (ptr)
  2637. seq_printf(m, " ->%pS", ptr);
  2638. }
  2639. static int t_show(struct seq_file *m, void *v)
  2640. {
  2641. struct ftrace_iterator *iter = m->private;
  2642. struct dyn_ftrace *rec;
  2643. if (iter->flags & FTRACE_ITER_HASH)
  2644. return t_hash_show(m, iter);
  2645. if (iter->flags & FTRACE_ITER_PRINTALL) {
  2646. if (iter->flags & FTRACE_ITER_NOTRACE)
  2647. seq_puts(m, "#### no functions disabled ####\n");
  2648. else
  2649. seq_puts(m, "#### all functions enabled ####\n");
  2650. return 0;
  2651. }
  2652. rec = iter->func;
  2653. if (!rec)
  2654. return 0;
  2655. seq_printf(m, "%ps", (void *)rec->ip);
  2656. if (iter->flags & FTRACE_ITER_ENABLED) {
  2657. struct ftrace_ops *ops = NULL;
  2658. seq_printf(m, " (%ld)%s%s",
  2659. ftrace_rec_count(rec),
  2660. rec->flags & FTRACE_FL_REGS ? " R" : " ",
  2661. rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
  2662. if (rec->flags & FTRACE_FL_TRAMP_EN) {
  2663. ops = ftrace_find_tramp_ops_any(rec);
  2664. if (ops)
  2665. seq_printf(m, "\ttramp: %pS",
  2666. (void *)ops->trampoline);
  2667. else
  2668. seq_puts(m, "\ttramp: ERROR!");
  2669. }
  2670. add_trampoline_func(m, ops, rec);
  2671. }
  2672. seq_putc(m, '\n');
  2673. return 0;
  2674. }
  2675. static const struct seq_operations show_ftrace_seq_ops = {
  2676. .start = t_start,
  2677. .next = t_next,
  2678. .stop = t_stop,
  2679. .show = t_show,
  2680. };
  2681. static int
  2682. ftrace_avail_open(struct inode *inode, struct file *file)
  2683. {
  2684. struct ftrace_iterator *iter;
  2685. if (unlikely(ftrace_disabled))
  2686. return -ENODEV;
  2687. iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
  2688. if (iter) {
  2689. iter->pg = ftrace_pages_start;
  2690. iter->ops = &global_ops;
  2691. }
  2692. return iter ? 0 : -ENOMEM;
  2693. }
  2694. static int
  2695. ftrace_enabled_open(struct inode *inode, struct file *file)
  2696. {
  2697. struct ftrace_iterator *iter;
  2698. iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
  2699. if (iter) {
  2700. iter->pg = ftrace_pages_start;
  2701. iter->flags = FTRACE_ITER_ENABLED;
  2702. iter->ops = &global_ops;
  2703. }
  2704. return iter ? 0 : -ENOMEM;
  2705. }
  2706. /**
  2707. * ftrace_regex_open - initialize function tracer filter files
  2708. * @ops: The ftrace_ops that hold the hash filters
  2709. * @flag: The type of filter to process
  2710. * @inode: The inode, usually passed in to your open routine
  2711. * @file: The file, usually passed in to your open routine
  2712. *
  2713. * ftrace_regex_open() initializes the filter files for the
  2714. * @ops. Depending on @flag it may process the filter hash or
  2715. * the notrace hash of @ops. With this called from the open
  2716. * routine, you can use ftrace_filter_write() for the write
  2717. * routine if @flag has FTRACE_ITER_FILTER set, or
  2718. * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
  2719. * tracing_lseek() should be used as the lseek routine, and
  2720. * release must call ftrace_regex_release().
  2721. */
  2722. int
  2723. ftrace_regex_open(struct ftrace_ops *ops, int flag,
  2724. struct inode *inode, struct file *file)
  2725. {
  2726. struct ftrace_iterator *iter;
  2727. struct ftrace_hash *hash;
  2728. int ret = 0;
  2729. ftrace_ops_init(ops);
  2730. if (unlikely(ftrace_disabled))
  2731. return -ENODEV;
  2732. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  2733. if (!iter)
  2734. return -ENOMEM;
  2735. if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
  2736. kfree(iter);
  2737. return -ENOMEM;
  2738. }
  2739. iter->ops = ops;
  2740. iter->flags = flag;
  2741. mutex_lock(&ops->func_hash->regex_lock);
  2742. if (flag & FTRACE_ITER_NOTRACE)
  2743. hash = ops->func_hash->notrace_hash;
  2744. else
  2745. hash = ops->func_hash->filter_hash;
  2746. if (file->f_mode & FMODE_WRITE) {
  2747. const int size_bits = FTRACE_HASH_DEFAULT_BITS;
  2748. if (file->f_flags & O_TRUNC)
  2749. iter->hash = alloc_ftrace_hash(size_bits);
  2750. else
  2751. iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
  2752. if (!iter->hash) {
  2753. trace_parser_put(&iter->parser);
  2754. kfree(iter);
  2755. ret = -ENOMEM;
  2756. goto out_unlock;
  2757. }
  2758. }
  2759. if (file->f_mode & FMODE_READ) {
  2760. iter->pg = ftrace_pages_start;
  2761. ret = seq_open(file, &show_ftrace_seq_ops);
  2762. if (!ret) {
  2763. struct seq_file *m = file->private_data;
  2764. m->private = iter;
  2765. } else {
  2766. /* Failed */
  2767. free_ftrace_hash(iter->hash);
  2768. trace_parser_put(&iter->parser);
  2769. kfree(iter);
  2770. }
  2771. } else
  2772. file->private_data = iter;
  2773. out_unlock:
  2774. mutex_unlock(&ops->func_hash->regex_lock);
  2775. return ret;
  2776. }
  2777. static int
  2778. ftrace_filter_open(struct inode *inode, struct file *file)
  2779. {
  2780. struct ftrace_ops *ops = inode->i_private;
  2781. return ftrace_regex_open(ops,
  2782. FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
  2783. inode, file);
  2784. }
  2785. static int
  2786. ftrace_notrace_open(struct inode *inode, struct file *file)
  2787. {
  2788. struct ftrace_ops *ops = inode->i_private;
  2789. return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
  2790. inode, file);
  2791. }
  2792. static int ftrace_match(char *str, char *regex, int len, int type)
  2793. {
  2794. int matched = 0;
  2795. int slen;
  2796. switch (type) {
  2797. case MATCH_FULL:
  2798. if (strcmp(str, regex) == 0)
  2799. matched = 1;
  2800. break;
  2801. case MATCH_FRONT_ONLY:
  2802. if (strncmp(str, regex, len) == 0)
  2803. matched = 1;
  2804. break;
  2805. case MATCH_MIDDLE_ONLY:
  2806. if (strstr(str, regex))
  2807. matched = 1;
  2808. break;
  2809. case MATCH_END_ONLY:
  2810. slen = strlen(str);
  2811. if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
  2812. matched = 1;
  2813. break;
  2814. }
  2815. return matched;
  2816. }
  2817. static int
  2818. enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
  2819. {
  2820. struct ftrace_func_entry *entry;
  2821. int ret = 0;
  2822. entry = ftrace_lookup_ip(hash, rec->ip);
  2823. if (not) {
  2824. /* Do nothing if it doesn't exist */
  2825. if (!entry)
  2826. return 0;
  2827. free_hash_entry(hash, entry);
  2828. } else {
  2829. /* Do nothing if it exists */
  2830. if (entry)
  2831. return 0;
  2832. ret = add_hash_entry(hash, rec->ip);
  2833. }
  2834. return ret;
  2835. }
  2836. static int
  2837. ftrace_match_record(struct dyn_ftrace *rec, char *mod,
  2838. char *regex, int len, int type)
  2839. {
  2840. char str[KSYM_SYMBOL_LEN];
  2841. char *modname;
  2842. kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
  2843. if (mod) {
  2844. /* module lookup requires matching the module */
  2845. if (!modname || strcmp(modname, mod))
  2846. return 0;
  2847. /* blank search means to match all funcs in the mod */
  2848. if (!len)
  2849. return 1;
  2850. }
  2851. return ftrace_match(str, regex, len, type);
  2852. }
  2853. static int
  2854. match_records(struct ftrace_hash *hash, char *buff,
  2855. int len, char *mod, int not)
  2856. {
  2857. unsigned search_len = 0;
  2858. struct ftrace_page *pg;
  2859. struct dyn_ftrace *rec;
  2860. int type = MATCH_FULL;
  2861. char *search = buff;
  2862. int found = 0;
  2863. int ret;
  2864. if (len) {
  2865. type = filter_parse_regex(buff, len, &search, &not);
  2866. search_len = strlen(search);
  2867. }
  2868. mutex_lock(&ftrace_lock);
  2869. if (unlikely(ftrace_disabled))
  2870. goto out_unlock;
  2871. do_for_each_ftrace_rec(pg, rec) {
  2872. if (ftrace_match_record(rec, mod, search, search_len, type)) {
  2873. ret = enter_record(hash, rec, not);
  2874. if (ret < 0) {
  2875. found = ret;
  2876. goto out_unlock;
  2877. }
  2878. found = 1;
  2879. }
  2880. } while_for_each_ftrace_rec();
  2881. out_unlock:
  2882. mutex_unlock(&ftrace_lock);
  2883. return found;
  2884. }
  2885. static int
  2886. ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
  2887. {
  2888. return match_records(hash, buff, len, NULL, 0);
  2889. }
  2890. static int
  2891. ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
  2892. {
  2893. int not = 0;
  2894. /* blank or '*' mean the same */
  2895. if (strcmp(buff, "*") == 0)
  2896. buff[0] = 0;
  2897. /* handle the case of 'dont filter this module' */
  2898. if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
  2899. buff[0] = 0;
  2900. not = 1;
  2901. }
  2902. return match_records(hash, buff, strlen(buff), mod, not);
  2903. }
  2904. /*
  2905. * We register the module command as a template to show others how
  2906. * to register the a command as well.
  2907. */
  2908. static int
  2909. ftrace_mod_callback(struct ftrace_hash *hash,
  2910. char *func, char *cmd, char *param, int enable)
  2911. {
  2912. char *mod;
  2913. int ret = -EINVAL;
  2914. /*
  2915. * cmd == 'mod' because we only registered this func
  2916. * for the 'mod' ftrace_func_command.
  2917. * But if you register one func with multiple commands,
  2918. * you can tell which command was used by the cmd
  2919. * parameter.
  2920. */
  2921. /* we must have a module name */
  2922. if (!param)
  2923. return ret;
  2924. mod = strsep(&param, ":");
  2925. if (!strlen(mod))
  2926. return ret;
  2927. ret = ftrace_match_module_records(hash, func, mod);
  2928. if (!ret)
  2929. ret = -EINVAL;
  2930. if (ret < 0)
  2931. return ret;
  2932. return 0;
  2933. }
  2934. static struct ftrace_func_command ftrace_mod_cmd = {
  2935. .name = "mod",
  2936. .func = ftrace_mod_callback,
  2937. };
  2938. static int __init ftrace_mod_cmd_init(void)
  2939. {
  2940. return register_ftrace_command(&ftrace_mod_cmd);
  2941. }
  2942. core_initcall(ftrace_mod_cmd_init);
  2943. static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
  2944. struct ftrace_ops *op, struct pt_regs *pt_regs)
  2945. {
  2946. struct ftrace_func_probe *entry;
  2947. struct hlist_head *hhd;
  2948. unsigned long key;
  2949. key = hash_long(ip, FTRACE_HASH_BITS);
  2950. hhd = &ftrace_func_hash[key];
  2951. if (hlist_empty(hhd))
  2952. return;
  2953. /*
  2954. * Disable preemption for these calls to prevent a RCU grace
  2955. * period. This syncs the hash iteration and freeing of items
  2956. * on the hash. rcu_read_lock is too dangerous here.
  2957. */
  2958. preempt_disable_notrace();
  2959. hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
  2960. if (entry->ip == ip)
  2961. entry->ops->func(ip, parent_ip, &entry->data);
  2962. }
  2963. preempt_enable_notrace();
  2964. }
  2965. static struct ftrace_ops trace_probe_ops __read_mostly =
  2966. {
  2967. .func = function_trace_probe_call,
  2968. .flags = FTRACE_OPS_FL_INITIALIZED,
  2969. INIT_OPS_HASH(trace_probe_ops)
  2970. };
  2971. static int ftrace_probe_registered;
  2972. static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash)
  2973. {
  2974. int ret;
  2975. int i;
  2976. if (ftrace_probe_registered) {
  2977. /* still need to update the function call sites */
  2978. if (ftrace_enabled)
  2979. ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
  2980. old_hash);
  2981. return;
  2982. }
  2983. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  2984. struct hlist_head *hhd = &ftrace_func_hash[i];
  2985. if (hhd->first)
  2986. break;
  2987. }
  2988. /* Nothing registered? */
  2989. if (i == FTRACE_FUNC_HASHSIZE)
  2990. return;
  2991. ret = ftrace_startup(&trace_probe_ops, 0);
  2992. ftrace_probe_registered = 1;
  2993. }
  2994. static void __disable_ftrace_function_probe(void)
  2995. {
  2996. int i;
  2997. if (!ftrace_probe_registered)
  2998. return;
  2999. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  3000. struct hlist_head *hhd = &ftrace_func_hash[i];
  3001. if (hhd->first)
  3002. return;
  3003. }
  3004. /* no more funcs left */
  3005. ftrace_shutdown(&trace_probe_ops, 0);
  3006. ftrace_probe_registered = 0;
  3007. }
  3008. static void ftrace_free_entry(struct ftrace_func_probe *entry)
  3009. {
  3010. if (entry->ops->free)
  3011. entry->ops->free(entry->ops, entry->ip, &entry->data);
  3012. kfree(entry);
  3013. }
  3014. int
  3015. register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  3016. void *data)
  3017. {
  3018. struct ftrace_ops_hash old_hash_ops;
  3019. struct ftrace_func_probe *entry;
  3020. struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
  3021. struct ftrace_hash *old_hash = *orig_hash;
  3022. struct ftrace_hash *hash;
  3023. struct ftrace_page *pg;
  3024. struct dyn_ftrace *rec;
  3025. int type, len, not;
  3026. unsigned long key;
  3027. int count = 0;
  3028. char *search;
  3029. int ret;
  3030. type = filter_parse_regex(glob, strlen(glob), &search, &not);
  3031. len = strlen(search);
  3032. /* we do not support '!' for function probes */
  3033. if (WARN_ON(not))
  3034. return -EINVAL;
  3035. mutex_lock(&trace_probe_ops.func_hash->regex_lock);
  3036. old_hash_ops.filter_hash = old_hash;
  3037. /* Probes only have filters */
  3038. old_hash_ops.notrace_hash = NULL;
  3039. hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
  3040. if (!hash) {
  3041. count = -ENOMEM;
  3042. goto out;
  3043. }
  3044. if (unlikely(ftrace_disabled)) {
  3045. count = -ENODEV;
  3046. goto out;
  3047. }
  3048. mutex_lock(&ftrace_lock);
  3049. do_for_each_ftrace_rec(pg, rec) {
  3050. if (!ftrace_match_record(rec, NULL, search, len, type))
  3051. continue;
  3052. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  3053. if (!entry) {
  3054. /* If we did not process any, then return error */
  3055. if (!count)
  3056. count = -ENOMEM;
  3057. goto out_unlock;
  3058. }
  3059. count++;
  3060. entry->data = data;
  3061. /*
  3062. * The caller might want to do something special
  3063. * for each function we find. We call the callback
  3064. * to give the caller an opportunity to do so.
  3065. */
  3066. if (ops->init) {
  3067. if (ops->init(ops, rec->ip, &entry->data) < 0) {
  3068. /* caller does not like this func */
  3069. kfree(entry);
  3070. continue;
  3071. }
  3072. }
  3073. ret = enter_record(hash, rec, 0);
  3074. if (ret < 0) {
  3075. kfree(entry);
  3076. count = ret;
  3077. goto out_unlock;
  3078. }
  3079. entry->ops = ops;
  3080. entry->ip = rec->ip;
  3081. key = hash_long(entry->ip, FTRACE_HASH_BITS);
  3082. hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
  3083. } while_for_each_ftrace_rec();
  3084. ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
  3085. __enable_ftrace_function_probe(&old_hash_ops);
  3086. if (!ret)
  3087. free_ftrace_hash_rcu(old_hash);
  3088. else
  3089. count = ret;
  3090. out_unlock:
  3091. mutex_unlock(&ftrace_lock);
  3092. out:
  3093. mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
  3094. free_ftrace_hash(hash);
  3095. return count;
  3096. }
  3097. enum {
  3098. PROBE_TEST_FUNC = 1,
  3099. PROBE_TEST_DATA = 2
  3100. };
  3101. static void
  3102. __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  3103. void *data, int flags)
  3104. {
  3105. struct ftrace_func_entry *rec_entry;
  3106. struct ftrace_func_probe *entry;
  3107. struct ftrace_func_probe *p;
  3108. struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
  3109. struct ftrace_hash *old_hash = *orig_hash;
  3110. struct list_head free_list;
  3111. struct ftrace_hash *hash;
  3112. struct hlist_node *tmp;
  3113. char str[KSYM_SYMBOL_LEN];
  3114. int type = MATCH_FULL;
  3115. int i, len = 0;
  3116. char *search;
  3117. int ret;
  3118. if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
  3119. glob = NULL;
  3120. else if (glob) {
  3121. int not;
  3122. type = filter_parse_regex(glob, strlen(glob), &search, &not);
  3123. len = strlen(search);
  3124. /* we do not support '!' for function probes */
  3125. if (WARN_ON(not))
  3126. return;
  3127. }
  3128. mutex_lock(&trace_probe_ops.func_hash->regex_lock);
  3129. hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
  3130. if (!hash)
  3131. /* Hmm, should report this somehow */
  3132. goto out_unlock;
  3133. INIT_LIST_HEAD(&free_list);
  3134. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  3135. struct hlist_head *hhd = &ftrace_func_hash[i];
  3136. hlist_for_each_entry_safe(entry, tmp, hhd, node) {
  3137. /* break up if statements for readability */
  3138. if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
  3139. continue;
  3140. if ((flags & PROBE_TEST_DATA) && entry->data != data)
  3141. continue;
  3142. /* do this last, since it is the most expensive */
  3143. if (glob) {
  3144. kallsyms_lookup(entry->ip, NULL, NULL,
  3145. NULL, str);
  3146. if (!ftrace_match(str, glob, len, type))
  3147. continue;
  3148. }
  3149. rec_entry = ftrace_lookup_ip(hash, entry->ip);
  3150. /* It is possible more than one entry had this ip */
  3151. if (rec_entry)
  3152. free_hash_entry(hash, rec_entry);
  3153. hlist_del_rcu(&entry->node);
  3154. list_add(&entry->free_list, &free_list);
  3155. }
  3156. }
  3157. mutex_lock(&ftrace_lock);
  3158. __disable_ftrace_function_probe();
  3159. /*
  3160. * Remove after the disable is called. Otherwise, if the last
  3161. * probe is removed, a null hash means *all enabled*.
  3162. */
  3163. ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
  3164. synchronize_sched();
  3165. if (!ret)
  3166. free_ftrace_hash_rcu(old_hash);
  3167. list_for_each_entry_safe(entry, p, &free_list, free_list) {
  3168. list_del(&entry->free_list);
  3169. ftrace_free_entry(entry);
  3170. }
  3171. mutex_unlock(&ftrace_lock);
  3172. out_unlock:
  3173. mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
  3174. free_ftrace_hash(hash);
  3175. }
  3176. void
  3177. unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  3178. void *data)
  3179. {
  3180. __unregister_ftrace_function_probe(glob, ops, data,
  3181. PROBE_TEST_FUNC | PROBE_TEST_DATA);
  3182. }
  3183. void
  3184. unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
  3185. {
  3186. __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
  3187. }
  3188. void unregister_ftrace_function_probe_all(char *glob)
  3189. {
  3190. __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
  3191. }
  3192. static LIST_HEAD(ftrace_commands);
  3193. static DEFINE_MUTEX(ftrace_cmd_mutex);
  3194. /*
  3195. * Currently we only register ftrace commands from __init, so mark this
  3196. * __init too.
  3197. */
  3198. __init int register_ftrace_command(struct ftrace_func_command *cmd)
  3199. {
  3200. struct ftrace_func_command *p;
  3201. int ret = 0;
  3202. mutex_lock(&ftrace_cmd_mutex);
  3203. list_for_each_entry(p, &ftrace_commands, list) {
  3204. if (strcmp(cmd->name, p->name) == 0) {
  3205. ret = -EBUSY;
  3206. goto out_unlock;
  3207. }
  3208. }
  3209. list_add(&cmd->list, &ftrace_commands);
  3210. out_unlock:
  3211. mutex_unlock(&ftrace_cmd_mutex);
  3212. return ret;
  3213. }
  3214. /*
  3215. * Currently we only unregister ftrace commands from __init, so mark
  3216. * this __init too.
  3217. */
  3218. __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
  3219. {
  3220. struct ftrace_func_command *p, *n;
  3221. int ret = -ENODEV;
  3222. mutex_lock(&ftrace_cmd_mutex);
  3223. list_for_each_entry_safe(p, n, &ftrace_commands, list) {
  3224. if (strcmp(cmd->name, p->name) == 0) {
  3225. ret = 0;
  3226. list_del_init(&p->list);
  3227. goto out_unlock;
  3228. }
  3229. }
  3230. out_unlock:
  3231. mutex_unlock(&ftrace_cmd_mutex);
  3232. return ret;
  3233. }
  3234. static int ftrace_process_regex(struct ftrace_hash *hash,
  3235. char *buff, int len, int enable)
  3236. {
  3237. char *func, *command, *next = buff;
  3238. struct ftrace_func_command *p;
  3239. int ret = -EINVAL;
  3240. func = strsep(&next, ":");
  3241. if (!next) {
  3242. ret = ftrace_match_records(hash, func, len);
  3243. if (!ret)
  3244. ret = -EINVAL;
  3245. if (ret < 0)
  3246. return ret;
  3247. return 0;
  3248. }
  3249. /* command found */
  3250. command = strsep(&next, ":");
  3251. mutex_lock(&ftrace_cmd_mutex);
  3252. list_for_each_entry(p, &ftrace_commands, list) {
  3253. if (strcmp(p->name, command) == 0) {
  3254. ret = p->func(hash, func, command, next, enable);
  3255. goto out_unlock;
  3256. }
  3257. }
  3258. out_unlock:
  3259. mutex_unlock(&ftrace_cmd_mutex);
  3260. return ret;
  3261. }
  3262. static ssize_t
  3263. ftrace_regex_write(struct file *file, const char __user *ubuf,
  3264. size_t cnt, loff_t *ppos, int enable)
  3265. {
  3266. struct ftrace_iterator *iter;
  3267. struct trace_parser *parser;
  3268. ssize_t ret, read;
  3269. if (!cnt)
  3270. return 0;
  3271. if (file->f_mode & FMODE_READ) {
  3272. struct seq_file *m = file->private_data;
  3273. iter = m->private;
  3274. } else
  3275. iter = file->private_data;
  3276. if (unlikely(ftrace_disabled))
  3277. return -ENODEV;
  3278. /* iter->hash is a local copy, so we don't need regex_lock */
  3279. parser = &iter->parser;
  3280. read = trace_get_user(parser, ubuf, cnt, ppos);
  3281. if (read >= 0 && trace_parser_loaded(parser) &&
  3282. !trace_parser_cont(parser)) {
  3283. ret = ftrace_process_regex(iter->hash, parser->buffer,
  3284. parser->idx, enable);
  3285. trace_parser_clear(parser);
  3286. if (ret < 0)
  3287. goto out;
  3288. }
  3289. ret = read;
  3290. out:
  3291. return ret;
  3292. }
  3293. ssize_t
  3294. ftrace_filter_write(struct file *file, const char __user *ubuf,
  3295. size_t cnt, loff_t *ppos)
  3296. {
  3297. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  3298. }
  3299. ssize_t
  3300. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  3301. size_t cnt, loff_t *ppos)
  3302. {
  3303. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  3304. }
  3305. static int
  3306. ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
  3307. {
  3308. struct ftrace_func_entry *entry;
  3309. if (!ftrace_location(ip))
  3310. return -EINVAL;
  3311. if (remove) {
  3312. entry = ftrace_lookup_ip(hash, ip);
  3313. if (!entry)
  3314. return -ENOENT;
  3315. free_hash_entry(hash, entry);
  3316. return 0;
  3317. }
  3318. return add_hash_entry(hash, ip);
  3319. }
  3320. static void ftrace_ops_update_code(struct ftrace_ops *ops,
  3321. struct ftrace_ops_hash *old_hash)
  3322. {
  3323. struct ftrace_ops *op;
  3324. if (!ftrace_enabled)
  3325. return;
  3326. if (ops->flags & FTRACE_OPS_FL_ENABLED) {
  3327. ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
  3328. return;
  3329. }
  3330. /*
  3331. * If this is the shared global_ops filter, then we need to
  3332. * check if there is another ops that shares it, is enabled.
  3333. * If so, we still need to run the modify code.
  3334. */
  3335. if (ops->func_hash != &global_ops.local_hash)
  3336. return;
  3337. do_for_each_ftrace_op(op, ftrace_ops_list) {
  3338. if (op->func_hash == &global_ops.local_hash &&
  3339. op->flags & FTRACE_OPS_FL_ENABLED) {
  3340. ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
  3341. /* Only need to do this once */
  3342. return;
  3343. }
  3344. } while_for_each_ftrace_op(op);
  3345. }
  3346. static int
  3347. ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
  3348. unsigned long ip, int remove, int reset, int enable)
  3349. {
  3350. struct ftrace_hash **orig_hash;
  3351. struct ftrace_ops_hash old_hash_ops;
  3352. struct ftrace_hash *old_hash;
  3353. struct ftrace_hash *hash;
  3354. int ret;
  3355. if (unlikely(ftrace_disabled))
  3356. return -ENODEV;
  3357. mutex_lock(&ops->func_hash->regex_lock);
  3358. if (enable)
  3359. orig_hash = &ops->func_hash->filter_hash;
  3360. else
  3361. orig_hash = &ops->func_hash->notrace_hash;
  3362. if (reset)
  3363. hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
  3364. else
  3365. hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
  3366. if (!hash) {
  3367. ret = -ENOMEM;
  3368. goto out_regex_unlock;
  3369. }
  3370. if (buf && !ftrace_match_records(hash, buf, len)) {
  3371. ret = -EINVAL;
  3372. goto out_regex_unlock;
  3373. }
  3374. if (ip) {
  3375. ret = ftrace_match_addr(hash, ip, remove);
  3376. if (ret < 0)
  3377. goto out_regex_unlock;
  3378. }
  3379. mutex_lock(&ftrace_lock);
  3380. old_hash = *orig_hash;
  3381. old_hash_ops.filter_hash = ops->func_hash->filter_hash;
  3382. old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
  3383. ret = ftrace_hash_move(ops, enable, orig_hash, hash);
  3384. if (!ret) {
  3385. ftrace_ops_update_code(ops, &old_hash_ops);
  3386. free_ftrace_hash_rcu(old_hash);
  3387. }
  3388. mutex_unlock(&ftrace_lock);
  3389. out_regex_unlock:
  3390. mutex_unlock(&ops->func_hash->regex_lock);
  3391. free_ftrace_hash(hash);
  3392. return ret;
  3393. }
  3394. static int
  3395. ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
  3396. int reset, int enable)
  3397. {
  3398. return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
  3399. }
  3400. /**
  3401. * ftrace_set_filter_ip - set a function to filter on in ftrace by address
  3402. * @ops - the ops to set the filter with
  3403. * @ip - the address to add to or remove from the filter.
  3404. * @remove - non zero to remove the ip from the filter
  3405. * @reset - non zero to reset all filters before applying this filter.
  3406. *
  3407. * Filters denote which functions should be enabled when tracing is enabled
  3408. * If @ip is NULL, it failes to update filter.
  3409. */
  3410. int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
  3411. int remove, int reset)
  3412. {
  3413. ftrace_ops_init(ops);
  3414. return ftrace_set_addr(ops, ip, remove, reset, 1);
  3415. }
  3416. EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
  3417. static int
  3418. ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
  3419. int reset, int enable)
  3420. {
  3421. return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
  3422. }
  3423. /**
  3424. * ftrace_set_filter - set a function to filter on in ftrace
  3425. * @ops - the ops to set the filter with
  3426. * @buf - the string that holds the function filter text.
  3427. * @len - the length of the string.
  3428. * @reset - non zero to reset all filters before applying this filter.
  3429. *
  3430. * Filters denote which functions should be enabled when tracing is enabled.
  3431. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  3432. */
  3433. int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
  3434. int len, int reset)
  3435. {
  3436. ftrace_ops_init(ops);
  3437. return ftrace_set_regex(ops, buf, len, reset, 1);
  3438. }
  3439. EXPORT_SYMBOL_GPL(ftrace_set_filter);
  3440. /**
  3441. * ftrace_set_notrace - set a function to not trace in ftrace
  3442. * @ops - the ops to set the notrace filter with
  3443. * @buf - the string that holds the function notrace text.
  3444. * @len - the length of the string.
  3445. * @reset - non zero to reset all filters before applying this filter.
  3446. *
  3447. * Notrace Filters denote which functions should not be enabled when tracing
  3448. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  3449. * for tracing.
  3450. */
  3451. int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
  3452. int len, int reset)
  3453. {
  3454. ftrace_ops_init(ops);
  3455. return ftrace_set_regex(ops, buf, len, reset, 0);
  3456. }
  3457. EXPORT_SYMBOL_GPL(ftrace_set_notrace);
  3458. /**
  3459. * ftrace_set_global_filter - set a function to filter on with global tracers
  3460. * @buf - the string that holds the function filter text.
  3461. * @len - the length of the string.
  3462. * @reset - non zero to reset all filters before applying this filter.
  3463. *
  3464. * Filters denote which functions should be enabled when tracing is enabled.
  3465. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  3466. */
  3467. void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
  3468. {
  3469. ftrace_set_regex(&global_ops, buf, len, reset, 1);
  3470. }
  3471. EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
  3472. /**
  3473. * ftrace_set_global_notrace - set a function to not trace with global tracers
  3474. * @buf - the string that holds the function notrace text.
  3475. * @len - the length of the string.
  3476. * @reset - non zero to reset all filters before applying this filter.
  3477. *
  3478. * Notrace Filters denote which functions should not be enabled when tracing
  3479. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  3480. * for tracing.
  3481. */
  3482. void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
  3483. {
  3484. ftrace_set_regex(&global_ops, buf, len, reset, 0);
  3485. }
  3486. EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
  3487. /*
  3488. * command line interface to allow users to set filters on boot up.
  3489. */
  3490. #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
  3491. static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
  3492. static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
  3493. /* Used by function selftest to not test if filter is set */
  3494. bool ftrace_filter_param __initdata;
  3495. static int __init set_ftrace_notrace(char *str)
  3496. {
  3497. ftrace_filter_param = true;
  3498. strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
  3499. return 1;
  3500. }
  3501. __setup("ftrace_notrace=", set_ftrace_notrace);
  3502. static int __init set_ftrace_filter(char *str)
  3503. {
  3504. ftrace_filter_param = true;
  3505. strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
  3506. return 1;
  3507. }
  3508. __setup("ftrace_filter=", set_ftrace_filter);
  3509. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  3510. static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
  3511. static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
  3512. static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
  3513. static unsigned long save_global_trampoline;
  3514. static unsigned long save_global_flags;
  3515. static int __init set_graph_function(char *str)
  3516. {
  3517. strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
  3518. return 1;
  3519. }
  3520. __setup("ftrace_graph_filter=", set_graph_function);
  3521. static int __init set_graph_notrace_function(char *str)
  3522. {
  3523. strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
  3524. return 1;
  3525. }
  3526. __setup("ftrace_graph_notrace=", set_graph_notrace_function);
  3527. static void __init set_ftrace_early_graph(char *buf, int enable)
  3528. {
  3529. int ret;
  3530. char *func;
  3531. unsigned long *table = ftrace_graph_funcs;
  3532. int *count = &ftrace_graph_count;
  3533. if (!enable) {
  3534. table = ftrace_graph_notrace_funcs;
  3535. count = &ftrace_graph_notrace_count;
  3536. }
  3537. while (buf) {
  3538. func = strsep(&buf, ",");
  3539. /* we allow only one expression at a time */
  3540. ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
  3541. if (ret)
  3542. printk(KERN_DEBUG "ftrace: function %s not "
  3543. "traceable\n", func);
  3544. }
  3545. }
  3546. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  3547. void __init
  3548. ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
  3549. {
  3550. char *func;
  3551. ftrace_ops_init(ops);
  3552. while (buf) {
  3553. func = strsep(&buf, ",");
  3554. ftrace_set_regex(ops, func, strlen(func), 0, enable);
  3555. }
  3556. }
  3557. static void __init set_ftrace_early_filters(void)
  3558. {
  3559. if (ftrace_filter_buf[0])
  3560. ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
  3561. if (ftrace_notrace_buf[0])
  3562. ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
  3563. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  3564. if (ftrace_graph_buf[0])
  3565. set_ftrace_early_graph(ftrace_graph_buf, 1);
  3566. if (ftrace_graph_notrace_buf[0])
  3567. set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
  3568. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  3569. }
  3570. int ftrace_regex_release(struct inode *inode, struct file *file)
  3571. {
  3572. struct seq_file *m = (struct seq_file *)file->private_data;
  3573. struct ftrace_ops_hash old_hash_ops;
  3574. struct ftrace_iterator *iter;
  3575. struct ftrace_hash **orig_hash;
  3576. struct ftrace_hash *old_hash;
  3577. struct trace_parser *parser;
  3578. int filter_hash;
  3579. int ret;
  3580. if (file->f_mode & FMODE_READ) {
  3581. iter = m->private;
  3582. seq_release(inode, file);
  3583. } else
  3584. iter = file->private_data;
  3585. parser = &iter->parser;
  3586. if (trace_parser_loaded(parser)) {
  3587. parser->buffer[parser->idx] = 0;
  3588. ftrace_match_records(iter->hash, parser->buffer, parser->idx);
  3589. }
  3590. trace_parser_put(parser);
  3591. mutex_lock(&iter->ops->func_hash->regex_lock);
  3592. if (file->f_mode & FMODE_WRITE) {
  3593. filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
  3594. if (filter_hash)
  3595. orig_hash = &iter->ops->func_hash->filter_hash;
  3596. else
  3597. orig_hash = &iter->ops->func_hash->notrace_hash;
  3598. mutex_lock(&ftrace_lock);
  3599. old_hash = *orig_hash;
  3600. old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash;
  3601. old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash;
  3602. ret = ftrace_hash_move(iter->ops, filter_hash,
  3603. orig_hash, iter->hash);
  3604. if (!ret) {
  3605. ftrace_ops_update_code(iter->ops, &old_hash_ops);
  3606. free_ftrace_hash_rcu(old_hash);
  3607. }
  3608. mutex_unlock(&ftrace_lock);
  3609. }
  3610. mutex_unlock(&iter->ops->func_hash->regex_lock);
  3611. free_ftrace_hash(iter->hash);
  3612. kfree(iter);
  3613. return 0;
  3614. }
  3615. static const struct file_operations ftrace_avail_fops = {
  3616. .open = ftrace_avail_open,
  3617. .read = seq_read,
  3618. .llseek = seq_lseek,
  3619. .release = seq_release_private,
  3620. };
  3621. static const struct file_operations ftrace_enabled_fops = {
  3622. .open = ftrace_enabled_open,
  3623. .read = seq_read,
  3624. .llseek = seq_lseek,
  3625. .release = seq_release_private,
  3626. };
  3627. static const struct file_operations ftrace_filter_fops = {
  3628. .open = ftrace_filter_open,
  3629. .read = seq_read,
  3630. .write = ftrace_filter_write,
  3631. .llseek = tracing_lseek,
  3632. .release = ftrace_regex_release,
  3633. };
  3634. static const struct file_operations ftrace_notrace_fops = {
  3635. .open = ftrace_notrace_open,
  3636. .read = seq_read,
  3637. .write = ftrace_notrace_write,
  3638. .llseek = tracing_lseek,
  3639. .release = ftrace_regex_release,
  3640. };
  3641. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  3642. static DEFINE_MUTEX(graph_lock);
  3643. int ftrace_graph_count;
  3644. int ftrace_graph_notrace_count;
  3645. unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
  3646. unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
  3647. struct ftrace_graph_data {
  3648. unsigned long *table;
  3649. size_t size;
  3650. int *count;
  3651. const struct seq_operations *seq_ops;
  3652. };
  3653. static void *
  3654. __g_next(struct seq_file *m, loff_t *pos)
  3655. {
  3656. struct ftrace_graph_data *fgd = m->private;
  3657. if (*pos >= *fgd->count)
  3658. return NULL;
  3659. return &fgd->table[*pos];
  3660. }
  3661. static void *
  3662. g_next(struct seq_file *m, void *v, loff_t *pos)
  3663. {
  3664. (*pos)++;
  3665. return __g_next(m, pos);
  3666. }
  3667. static void *g_start(struct seq_file *m, loff_t *pos)
  3668. {
  3669. struct ftrace_graph_data *fgd = m->private;
  3670. mutex_lock(&graph_lock);
  3671. /* Nothing, tell g_show to print all functions are enabled */
  3672. if (!*fgd->count && !*pos)
  3673. return (void *)1;
  3674. return __g_next(m, pos);
  3675. }
  3676. static void g_stop(struct seq_file *m, void *p)
  3677. {
  3678. mutex_unlock(&graph_lock);
  3679. }
  3680. static int g_show(struct seq_file *m, void *v)
  3681. {
  3682. unsigned long *ptr = v;
  3683. if (!ptr)
  3684. return 0;
  3685. if (ptr == (unsigned long *)1) {
  3686. struct ftrace_graph_data *fgd = m->private;
  3687. if (fgd->table == ftrace_graph_funcs)
  3688. seq_puts(m, "#### all functions enabled ####\n");
  3689. else
  3690. seq_puts(m, "#### no functions disabled ####\n");
  3691. return 0;
  3692. }
  3693. seq_printf(m, "%ps\n", (void *)*ptr);
  3694. return 0;
  3695. }
  3696. static const struct seq_operations ftrace_graph_seq_ops = {
  3697. .start = g_start,
  3698. .next = g_next,
  3699. .stop = g_stop,
  3700. .show = g_show,
  3701. };
  3702. static int
  3703. __ftrace_graph_open(struct inode *inode, struct file *file,
  3704. struct ftrace_graph_data *fgd)
  3705. {
  3706. int ret = 0;
  3707. mutex_lock(&graph_lock);
  3708. if ((file->f_mode & FMODE_WRITE) &&
  3709. (file->f_flags & O_TRUNC)) {
  3710. *fgd->count = 0;
  3711. memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
  3712. }
  3713. mutex_unlock(&graph_lock);
  3714. if (file->f_mode & FMODE_READ) {
  3715. ret = seq_open(file, fgd->seq_ops);
  3716. if (!ret) {
  3717. struct seq_file *m = file->private_data;
  3718. m->private = fgd;
  3719. }
  3720. } else
  3721. file->private_data = fgd;
  3722. return ret;
  3723. }
  3724. static int
  3725. ftrace_graph_open(struct inode *inode, struct file *file)
  3726. {
  3727. struct ftrace_graph_data *fgd;
  3728. if (unlikely(ftrace_disabled))
  3729. return -ENODEV;
  3730. fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
  3731. if (fgd == NULL)
  3732. return -ENOMEM;
  3733. fgd->table = ftrace_graph_funcs;
  3734. fgd->size = FTRACE_GRAPH_MAX_FUNCS;
  3735. fgd->count = &ftrace_graph_count;
  3736. fgd->seq_ops = &ftrace_graph_seq_ops;
  3737. return __ftrace_graph_open(inode, file, fgd);
  3738. }
  3739. static int
  3740. ftrace_graph_notrace_open(struct inode *inode, struct file *file)
  3741. {
  3742. struct ftrace_graph_data *fgd;
  3743. if (unlikely(ftrace_disabled))
  3744. return -ENODEV;
  3745. fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
  3746. if (fgd == NULL)
  3747. return -ENOMEM;
  3748. fgd->table = ftrace_graph_notrace_funcs;
  3749. fgd->size = FTRACE_GRAPH_MAX_FUNCS;
  3750. fgd->count = &ftrace_graph_notrace_count;
  3751. fgd->seq_ops = &ftrace_graph_seq_ops;
  3752. return __ftrace_graph_open(inode, file, fgd);
  3753. }
  3754. static int
  3755. ftrace_graph_release(struct inode *inode, struct file *file)
  3756. {
  3757. if (file->f_mode & FMODE_READ) {
  3758. struct seq_file *m = file->private_data;
  3759. kfree(m->private);
  3760. seq_release(inode, file);
  3761. } else {
  3762. kfree(file->private_data);
  3763. }
  3764. return 0;
  3765. }
  3766. static int
  3767. ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
  3768. {
  3769. struct dyn_ftrace *rec;
  3770. struct ftrace_page *pg;
  3771. int search_len;
  3772. int fail = 1;
  3773. int type, not;
  3774. char *search;
  3775. bool exists;
  3776. int i;
  3777. /* decode regex */
  3778. type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
  3779. if (!not && *idx >= size)
  3780. return -EBUSY;
  3781. search_len = strlen(search);
  3782. mutex_lock(&ftrace_lock);
  3783. if (unlikely(ftrace_disabled)) {
  3784. mutex_unlock(&ftrace_lock);
  3785. return -ENODEV;
  3786. }
  3787. do_for_each_ftrace_rec(pg, rec) {
  3788. if (ftrace_match_record(rec, NULL, search, search_len, type)) {
  3789. /* if it is in the array */
  3790. exists = false;
  3791. for (i = 0; i < *idx; i++) {
  3792. if (array[i] == rec->ip) {
  3793. exists = true;
  3794. break;
  3795. }
  3796. }
  3797. if (!not) {
  3798. fail = 0;
  3799. if (!exists) {
  3800. array[(*idx)++] = rec->ip;
  3801. if (*idx >= size)
  3802. goto out;
  3803. }
  3804. } else {
  3805. if (exists) {
  3806. array[i] = array[--(*idx)];
  3807. array[*idx] = 0;
  3808. fail = 0;
  3809. }
  3810. }
  3811. }
  3812. } while_for_each_ftrace_rec();
  3813. out:
  3814. mutex_unlock(&ftrace_lock);
  3815. if (fail)
  3816. return -EINVAL;
  3817. return 0;
  3818. }
  3819. static ssize_t
  3820. ftrace_graph_write(struct file *file, const char __user *ubuf,
  3821. size_t cnt, loff_t *ppos)
  3822. {
  3823. struct trace_parser parser;
  3824. ssize_t read, ret = 0;
  3825. struct ftrace_graph_data *fgd = file->private_data;
  3826. if (!cnt)
  3827. return 0;
  3828. if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
  3829. return -ENOMEM;
  3830. read = trace_get_user(&parser, ubuf, cnt, ppos);
  3831. if (read >= 0 && trace_parser_loaded((&parser))) {
  3832. parser.buffer[parser.idx] = 0;
  3833. mutex_lock(&graph_lock);
  3834. /* we allow only one expression at a time */
  3835. ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
  3836. parser.buffer);
  3837. mutex_unlock(&graph_lock);
  3838. }
  3839. if (!ret)
  3840. ret = read;
  3841. trace_parser_put(&parser);
  3842. return ret;
  3843. }
  3844. static const struct file_operations ftrace_graph_fops = {
  3845. .open = ftrace_graph_open,
  3846. .read = seq_read,
  3847. .write = ftrace_graph_write,
  3848. .llseek = tracing_lseek,
  3849. .release = ftrace_graph_release,
  3850. };
  3851. static const struct file_operations ftrace_graph_notrace_fops = {
  3852. .open = ftrace_graph_notrace_open,
  3853. .read = seq_read,
  3854. .write = ftrace_graph_write,
  3855. .llseek = tracing_lseek,
  3856. .release = ftrace_graph_release,
  3857. };
  3858. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  3859. void ftrace_create_filter_files(struct ftrace_ops *ops,
  3860. struct dentry *parent)
  3861. {
  3862. trace_create_file("set_ftrace_filter", 0644, parent,
  3863. ops, &ftrace_filter_fops);
  3864. trace_create_file("set_ftrace_notrace", 0644, parent,
  3865. ops, &ftrace_notrace_fops);
  3866. }
  3867. /*
  3868. * The name "destroy_filter_files" is really a misnomer. Although
  3869. * in the future, it may actualy delete the files, but this is
  3870. * really intended to make sure the ops passed in are disabled
  3871. * and that when this function returns, the caller is free to
  3872. * free the ops.
  3873. *
  3874. * The "destroy" name is only to match the "create" name that this
  3875. * should be paired with.
  3876. */
  3877. void ftrace_destroy_filter_files(struct ftrace_ops *ops)
  3878. {
  3879. mutex_lock(&ftrace_lock);
  3880. if (ops->flags & FTRACE_OPS_FL_ENABLED)
  3881. ftrace_shutdown(ops, 0);
  3882. ops->flags |= FTRACE_OPS_FL_DELETED;
  3883. mutex_unlock(&ftrace_lock);
  3884. }
  3885. static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
  3886. {
  3887. trace_create_file("available_filter_functions", 0444,
  3888. d_tracer, NULL, &ftrace_avail_fops);
  3889. trace_create_file("enabled_functions", 0444,
  3890. d_tracer, NULL, &ftrace_enabled_fops);
  3891. ftrace_create_filter_files(&global_ops, d_tracer);
  3892. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  3893. trace_create_file("set_graph_function", 0444, d_tracer,
  3894. NULL,
  3895. &ftrace_graph_fops);
  3896. trace_create_file("set_graph_notrace", 0444, d_tracer,
  3897. NULL,
  3898. &ftrace_graph_notrace_fops);
  3899. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  3900. return 0;
  3901. }
  3902. static int ftrace_cmp_ips(const void *a, const void *b)
  3903. {
  3904. const unsigned long *ipa = a;
  3905. const unsigned long *ipb = b;
  3906. if (*ipa > *ipb)
  3907. return 1;
  3908. if (*ipa < *ipb)
  3909. return -1;
  3910. return 0;
  3911. }
  3912. static void ftrace_swap_ips(void *a, void *b, int size)
  3913. {
  3914. unsigned long *ipa = a;
  3915. unsigned long *ipb = b;
  3916. unsigned long t;
  3917. t = *ipa;
  3918. *ipa = *ipb;
  3919. *ipb = t;
  3920. }
  3921. static int ftrace_process_locs(struct module *mod,
  3922. unsigned long *start,
  3923. unsigned long *end)
  3924. {
  3925. struct ftrace_page *start_pg;
  3926. struct ftrace_page *pg;
  3927. struct dyn_ftrace *rec;
  3928. unsigned long count;
  3929. unsigned long *p;
  3930. unsigned long addr;
  3931. unsigned long flags = 0; /* Shut up gcc */
  3932. int ret = -ENOMEM;
  3933. count = end - start;
  3934. if (!count)
  3935. return 0;
  3936. sort(start, count, sizeof(*start),
  3937. ftrace_cmp_ips, ftrace_swap_ips);
  3938. start_pg = ftrace_allocate_pages(count);
  3939. if (!start_pg)
  3940. return -ENOMEM;
  3941. mutex_lock(&ftrace_lock);
  3942. /*
  3943. * Core and each module needs their own pages, as
  3944. * modules will free them when they are removed.
  3945. * Force a new page to be allocated for modules.
  3946. */
  3947. if (!mod) {
  3948. WARN_ON(ftrace_pages || ftrace_pages_start);
  3949. /* First initialization */
  3950. ftrace_pages = ftrace_pages_start = start_pg;
  3951. } else {
  3952. if (!ftrace_pages)
  3953. goto out;
  3954. if (WARN_ON(ftrace_pages->next)) {
  3955. /* Hmm, we have free pages? */
  3956. while (ftrace_pages->next)
  3957. ftrace_pages = ftrace_pages->next;
  3958. }
  3959. ftrace_pages->next = start_pg;
  3960. }
  3961. p = start;
  3962. pg = start_pg;
  3963. while (p < end) {
  3964. addr = ftrace_call_adjust(*p++);
  3965. /*
  3966. * Some architecture linkers will pad between
  3967. * the different mcount_loc sections of different
  3968. * object files to satisfy alignments.
  3969. * Skip any NULL pointers.
  3970. */
  3971. if (!addr)
  3972. continue;
  3973. if (pg->index == pg->size) {
  3974. /* We should have allocated enough */
  3975. if (WARN_ON(!pg->next))
  3976. break;
  3977. pg = pg->next;
  3978. }
  3979. rec = &pg->records[pg->index++];
  3980. rec->ip = addr;
  3981. }
  3982. /* We should have used all pages */
  3983. WARN_ON(pg->next);
  3984. /* Assign the last page to ftrace_pages */
  3985. ftrace_pages = pg;
  3986. /*
  3987. * We only need to disable interrupts on start up
  3988. * because we are modifying code that an interrupt
  3989. * may execute, and the modification is not atomic.
  3990. * But for modules, nothing runs the code we modify
  3991. * until we are finished with it, and there's no
  3992. * reason to cause large interrupt latencies while we do it.
  3993. */
  3994. if (!mod)
  3995. local_irq_save(flags);
  3996. ftrace_update_code(mod, start_pg);
  3997. if (!mod)
  3998. local_irq_restore(flags);
  3999. ret = 0;
  4000. out:
  4001. mutex_unlock(&ftrace_lock);
  4002. return ret;
  4003. }
  4004. #ifdef CONFIG_MODULES
  4005. #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
  4006. void ftrace_release_mod(struct module *mod)
  4007. {
  4008. struct dyn_ftrace *rec;
  4009. struct ftrace_page **last_pg;
  4010. struct ftrace_page *pg;
  4011. int order;
  4012. mutex_lock(&ftrace_lock);
  4013. if (ftrace_disabled)
  4014. goto out_unlock;
  4015. /*
  4016. * Each module has its own ftrace_pages, remove
  4017. * them from the list.
  4018. */
  4019. last_pg = &ftrace_pages_start;
  4020. for (pg = ftrace_pages_start; pg; pg = *last_pg) {
  4021. rec = &pg->records[0];
  4022. if (within_module_core(rec->ip, mod)) {
  4023. /*
  4024. * As core pages are first, the first
  4025. * page should never be a module page.
  4026. */
  4027. if (WARN_ON(pg == ftrace_pages_start))
  4028. goto out_unlock;
  4029. /* Check if we are deleting the last page */
  4030. if (pg == ftrace_pages)
  4031. ftrace_pages = next_to_ftrace_page(last_pg);
  4032. *last_pg = pg->next;
  4033. order = get_count_order(pg->size / ENTRIES_PER_PAGE);
  4034. free_pages((unsigned long)pg->records, order);
  4035. kfree(pg);
  4036. } else
  4037. last_pg = &pg->next;
  4038. }
  4039. out_unlock:
  4040. mutex_unlock(&ftrace_lock);
  4041. }
  4042. static void ftrace_init_module(struct module *mod,
  4043. unsigned long *start, unsigned long *end)
  4044. {
  4045. if (ftrace_disabled || start == end)
  4046. return;
  4047. ftrace_process_locs(mod, start, end);
  4048. }
  4049. void ftrace_module_init(struct module *mod)
  4050. {
  4051. ftrace_init_module(mod, mod->ftrace_callsites,
  4052. mod->ftrace_callsites +
  4053. mod->num_ftrace_callsites);
  4054. }
  4055. static int ftrace_module_notify_exit(struct notifier_block *self,
  4056. unsigned long val, void *data)
  4057. {
  4058. struct module *mod = data;
  4059. if (val == MODULE_STATE_GOING)
  4060. ftrace_release_mod(mod);
  4061. return 0;
  4062. }
  4063. #else
  4064. static int ftrace_module_notify_exit(struct notifier_block *self,
  4065. unsigned long val, void *data)
  4066. {
  4067. return 0;
  4068. }
  4069. #endif /* CONFIG_MODULES */
  4070. struct notifier_block ftrace_module_exit_nb = {
  4071. .notifier_call = ftrace_module_notify_exit,
  4072. .priority = INT_MIN, /* Run after anything that can remove kprobes */
  4073. };
  4074. void __init ftrace_init(void)
  4075. {
  4076. extern unsigned long __start_mcount_loc[];
  4077. extern unsigned long __stop_mcount_loc[];
  4078. unsigned long count, flags;
  4079. int ret;
  4080. local_irq_save(flags);
  4081. ret = ftrace_dyn_arch_init();
  4082. local_irq_restore(flags);
  4083. if (ret)
  4084. goto failed;
  4085. count = __stop_mcount_loc - __start_mcount_loc;
  4086. if (!count) {
  4087. pr_info("ftrace: No functions to be traced?\n");
  4088. goto failed;
  4089. }
  4090. pr_info("ftrace: allocating %ld entries in %ld pages\n",
  4091. count, count / ENTRIES_PER_PAGE + 1);
  4092. last_ftrace_enabled = ftrace_enabled = 1;
  4093. ret = ftrace_process_locs(NULL,
  4094. __start_mcount_loc,
  4095. __stop_mcount_loc);
  4096. ret = register_module_notifier(&ftrace_module_exit_nb);
  4097. if (ret)
  4098. pr_warning("Failed to register trace ftrace module exit notifier\n");
  4099. set_ftrace_early_filters();
  4100. return;
  4101. failed:
  4102. ftrace_disabled = 1;
  4103. }
  4104. /* Do nothing if arch does not support this */
  4105. void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
  4106. {
  4107. }
  4108. static void ftrace_update_trampoline(struct ftrace_ops *ops)
  4109. {
  4110. /*
  4111. * Currently there's no safe way to free a trampoline when the kernel
  4112. * is configured with PREEMPT. That is because a task could be preempted
  4113. * when it jumped to the trampoline, it may be preempted for a long time
  4114. * depending on the system load, and currently there's no way to know
  4115. * when it will be off the trampoline. If the trampoline is freed
  4116. * too early, when the task runs again, it will be executing on freed
  4117. * memory and crash.
  4118. */
  4119. #ifdef CONFIG_PREEMPT
  4120. /* Currently, only non dynamic ops can have a trampoline */
  4121. if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
  4122. return;
  4123. #endif
  4124. arch_ftrace_update_trampoline(ops);
  4125. }
  4126. #else
  4127. static struct ftrace_ops global_ops = {
  4128. .func = ftrace_stub,
  4129. .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
  4130. };
  4131. static int __init ftrace_nodyn_init(void)
  4132. {
  4133. ftrace_enabled = 1;
  4134. return 0;
  4135. }
  4136. core_initcall(ftrace_nodyn_init);
  4137. static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
  4138. static inline void ftrace_startup_enable(int command) { }
  4139. static inline void ftrace_startup_all(int command) { }
  4140. /* Keep as macros so we do not need to define the commands */
  4141. # define ftrace_startup(ops, command) \
  4142. ({ \
  4143. int ___ret = __register_ftrace_function(ops); \
  4144. if (!___ret) \
  4145. (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
  4146. ___ret; \
  4147. })
  4148. # define ftrace_shutdown(ops, command) \
  4149. ({ \
  4150. int ___ret = __unregister_ftrace_function(ops); \
  4151. if (!___ret) \
  4152. (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
  4153. ___ret; \
  4154. })
  4155. # define ftrace_startup_sysctl() do { } while (0)
  4156. # define ftrace_shutdown_sysctl() do { } while (0)
  4157. static inline int
  4158. ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
  4159. {
  4160. return 1;
  4161. }
  4162. static void ftrace_update_trampoline(struct ftrace_ops *ops)
  4163. {
  4164. }
  4165. #endif /* CONFIG_DYNAMIC_FTRACE */
  4166. __init void ftrace_init_global_array_ops(struct trace_array *tr)
  4167. {
  4168. tr->ops = &global_ops;
  4169. tr->ops->private = tr;
  4170. }
  4171. void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
  4172. {
  4173. /* If we filter on pids, update to use the pid function */
  4174. if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
  4175. if (WARN_ON(tr->ops->func != ftrace_stub))
  4176. printk("ftrace ops had %pS for function\n",
  4177. tr->ops->func);
  4178. /* Only the top level instance does pid tracing */
  4179. if (!list_empty(&ftrace_pids)) {
  4180. set_ftrace_pid_function(func);
  4181. func = ftrace_pid_func;
  4182. }
  4183. }
  4184. tr->ops->func = func;
  4185. tr->ops->private = tr;
  4186. }
  4187. void ftrace_reset_array_ops(struct trace_array *tr)
  4188. {
  4189. tr->ops->func = ftrace_stub;
  4190. }
  4191. static void
  4192. ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
  4193. struct ftrace_ops *op, struct pt_regs *regs)
  4194. {
  4195. if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
  4196. return;
  4197. /*
  4198. * Some of the ops may be dynamically allocated,
  4199. * they must be freed after a synchronize_sched().
  4200. */
  4201. preempt_disable_notrace();
  4202. trace_recursion_set(TRACE_CONTROL_BIT);
  4203. /*
  4204. * Control funcs (perf) uses RCU. Only trace if
  4205. * RCU is currently active.
  4206. */
  4207. if (!rcu_is_watching())
  4208. goto out;
  4209. do_for_each_ftrace_op(op, ftrace_control_list) {
  4210. if (!(op->flags & FTRACE_OPS_FL_STUB) &&
  4211. !ftrace_function_local_disabled(op) &&
  4212. ftrace_ops_test(op, ip, regs))
  4213. op->func(ip, parent_ip, op, regs);
  4214. } while_for_each_ftrace_op(op);
  4215. out:
  4216. trace_recursion_clear(TRACE_CONTROL_BIT);
  4217. preempt_enable_notrace();
  4218. }
  4219. static struct ftrace_ops control_ops = {
  4220. .func = ftrace_ops_control_func,
  4221. .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
  4222. INIT_OPS_HASH(control_ops)
  4223. };
  4224. static inline void
  4225. __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
  4226. struct ftrace_ops *ignored, struct pt_regs *regs)
  4227. {
  4228. struct ftrace_ops *op;
  4229. int bit;
  4230. bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
  4231. if (bit < 0)
  4232. return;
  4233. /*
  4234. * Some of the ops may be dynamically allocated,
  4235. * they must be freed after a synchronize_sched().
  4236. */
  4237. preempt_disable_notrace();
  4238. do_for_each_ftrace_op(op, ftrace_ops_list) {
  4239. if (ftrace_ops_test(op, ip, regs)) {
  4240. if (FTRACE_WARN_ON(!op->func)) {
  4241. pr_warn("op=%p %pS\n", op, op);
  4242. goto out;
  4243. }
  4244. op->func(ip, parent_ip, op, regs);
  4245. }
  4246. } while_for_each_ftrace_op(op);
  4247. out:
  4248. preempt_enable_notrace();
  4249. trace_clear_recursion(bit);
  4250. }
  4251. /*
  4252. * Some archs only support passing ip and parent_ip. Even though
  4253. * the list function ignores the op parameter, we do not want any
  4254. * C side effects, where a function is called without the caller
  4255. * sending a third parameter.
  4256. * Archs are to support both the regs and ftrace_ops at the same time.
  4257. * If they support ftrace_ops, it is assumed they support regs.
  4258. * If call backs want to use regs, they must either check for regs
  4259. * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
  4260. * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
  4261. * An architecture can pass partial regs with ftrace_ops and still
  4262. * set the ARCH_SUPPORT_FTARCE_OPS.
  4263. */
  4264. #if ARCH_SUPPORTS_FTRACE_OPS
  4265. static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
  4266. struct ftrace_ops *op, struct pt_regs *regs)
  4267. {
  4268. __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
  4269. }
  4270. #else
  4271. static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
  4272. {
  4273. __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
  4274. }
  4275. #endif
  4276. /*
  4277. * If there's only one function registered but it does not support
  4278. * recursion, this function will be called by the mcount trampoline.
  4279. * This function will handle recursion protection.
  4280. */
  4281. static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
  4282. struct ftrace_ops *op, struct pt_regs *regs)
  4283. {
  4284. int bit;
  4285. bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
  4286. if (bit < 0)
  4287. return;
  4288. op->func(ip, parent_ip, op, regs);
  4289. trace_clear_recursion(bit);
  4290. }
  4291. /**
  4292. * ftrace_ops_get_func - get the function a trampoline should call
  4293. * @ops: the ops to get the function for
  4294. *
  4295. * Normally the mcount trampoline will call the ops->func, but there
  4296. * are times that it should not. For example, if the ops does not
  4297. * have its own recursion protection, then it should call the
  4298. * ftrace_ops_recurs_func() instead.
  4299. *
  4300. * Returns the function that the trampoline should call for @ops.
  4301. */
  4302. ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
  4303. {
  4304. /*
  4305. * If this is a dynamic ops or we force list func,
  4306. * then it needs to call the list anyway.
  4307. */
  4308. if (ops->flags & FTRACE_OPS_FL_DYNAMIC || FTRACE_FORCE_LIST_FUNC)
  4309. return ftrace_ops_list_func;
  4310. /*
  4311. * If the func handles its own recursion, call it directly.
  4312. * Otherwise call the recursion protected function that
  4313. * will call the ftrace ops function.
  4314. */
  4315. if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE))
  4316. return ftrace_ops_recurs_func;
  4317. return ops->func;
  4318. }
  4319. static void clear_ftrace_swapper(void)
  4320. {
  4321. struct task_struct *p;
  4322. int cpu;
  4323. get_online_cpus();
  4324. for_each_online_cpu(cpu) {
  4325. p = idle_task(cpu);
  4326. clear_tsk_trace_trace(p);
  4327. }
  4328. put_online_cpus();
  4329. }
  4330. static void set_ftrace_swapper(void)
  4331. {
  4332. struct task_struct *p;
  4333. int cpu;
  4334. get_online_cpus();
  4335. for_each_online_cpu(cpu) {
  4336. p = idle_task(cpu);
  4337. set_tsk_trace_trace(p);
  4338. }
  4339. put_online_cpus();
  4340. }
  4341. static void clear_ftrace_pid(struct pid *pid)
  4342. {
  4343. struct task_struct *p;
  4344. rcu_read_lock();
  4345. do_each_pid_task(pid, PIDTYPE_PID, p) {
  4346. clear_tsk_trace_trace(p);
  4347. } while_each_pid_task(pid, PIDTYPE_PID, p);
  4348. rcu_read_unlock();
  4349. put_pid(pid);
  4350. }
  4351. static void set_ftrace_pid(struct pid *pid)
  4352. {
  4353. struct task_struct *p;
  4354. rcu_read_lock();
  4355. do_each_pid_task(pid, PIDTYPE_PID, p) {
  4356. set_tsk_trace_trace(p);
  4357. } while_each_pid_task(pid, PIDTYPE_PID, p);
  4358. rcu_read_unlock();
  4359. }
  4360. static void clear_ftrace_pid_task(struct pid *pid)
  4361. {
  4362. if (pid == ftrace_swapper_pid)
  4363. clear_ftrace_swapper();
  4364. else
  4365. clear_ftrace_pid(pid);
  4366. }
  4367. static void set_ftrace_pid_task(struct pid *pid)
  4368. {
  4369. if (pid == ftrace_swapper_pid)
  4370. set_ftrace_swapper();
  4371. else
  4372. set_ftrace_pid(pid);
  4373. }
  4374. static int ftrace_pid_add(int p)
  4375. {
  4376. struct pid *pid;
  4377. struct ftrace_pid *fpid;
  4378. int ret = -EINVAL;
  4379. mutex_lock(&ftrace_lock);
  4380. if (!p)
  4381. pid = ftrace_swapper_pid;
  4382. else
  4383. pid = find_get_pid(p);
  4384. if (!pid)
  4385. goto out;
  4386. ret = 0;
  4387. list_for_each_entry(fpid, &ftrace_pids, list)
  4388. if (fpid->pid == pid)
  4389. goto out_put;
  4390. ret = -ENOMEM;
  4391. fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
  4392. if (!fpid)
  4393. goto out_put;
  4394. list_add(&fpid->list, &ftrace_pids);
  4395. fpid->pid = pid;
  4396. set_ftrace_pid_task(pid);
  4397. ftrace_update_pid_func();
  4398. ftrace_startup_all(0);
  4399. mutex_unlock(&ftrace_lock);
  4400. return 0;
  4401. out_put:
  4402. if (pid != ftrace_swapper_pid)
  4403. put_pid(pid);
  4404. out:
  4405. mutex_unlock(&ftrace_lock);
  4406. return ret;
  4407. }
  4408. static void ftrace_pid_reset(void)
  4409. {
  4410. struct ftrace_pid *fpid, *safe;
  4411. mutex_lock(&ftrace_lock);
  4412. list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
  4413. struct pid *pid = fpid->pid;
  4414. clear_ftrace_pid_task(pid);
  4415. list_del(&fpid->list);
  4416. kfree(fpid);
  4417. }
  4418. ftrace_update_pid_func();
  4419. ftrace_startup_all(0);
  4420. mutex_unlock(&ftrace_lock);
  4421. }
  4422. static void *fpid_start(struct seq_file *m, loff_t *pos)
  4423. {
  4424. mutex_lock(&ftrace_lock);
  4425. if (list_empty(&ftrace_pids) && (!*pos))
  4426. return (void *) 1;
  4427. return seq_list_start(&ftrace_pids, *pos);
  4428. }
  4429. static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
  4430. {
  4431. if (v == (void *)1)
  4432. return NULL;
  4433. return seq_list_next(v, &ftrace_pids, pos);
  4434. }
  4435. static void fpid_stop(struct seq_file *m, void *p)
  4436. {
  4437. mutex_unlock(&ftrace_lock);
  4438. }
  4439. static int fpid_show(struct seq_file *m, void *v)
  4440. {
  4441. const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
  4442. if (v == (void *)1) {
  4443. seq_puts(m, "no pid\n");
  4444. return 0;
  4445. }
  4446. if (fpid->pid == ftrace_swapper_pid)
  4447. seq_puts(m, "swapper tasks\n");
  4448. else
  4449. seq_printf(m, "%u\n", pid_vnr(fpid->pid));
  4450. return 0;
  4451. }
  4452. static const struct seq_operations ftrace_pid_sops = {
  4453. .start = fpid_start,
  4454. .next = fpid_next,
  4455. .stop = fpid_stop,
  4456. .show = fpid_show,
  4457. };
  4458. static int
  4459. ftrace_pid_open(struct inode *inode, struct file *file)
  4460. {
  4461. int ret = 0;
  4462. if ((file->f_mode & FMODE_WRITE) &&
  4463. (file->f_flags & O_TRUNC))
  4464. ftrace_pid_reset();
  4465. if (file->f_mode & FMODE_READ)
  4466. ret = seq_open(file, &ftrace_pid_sops);
  4467. return ret;
  4468. }
  4469. static ssize_t
  4470. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  4471. size_t cnt, loff_t *ppos)
  4472. {
  4473. char buf[64], *tmp;
  4474. long val;
  4475. int ret;
  4476. if (cnt >= sizeof(buf))
  4477. return -EINVAL;
  4478. if (copy_from_user(&buf, ubuf, cnt))
  4479. return -EFAULT;
  4480. buf[cnt] = 0;
  4481. /*
  4482. * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
  4483. * to clean the filter quietly.
  4484. */
  4485. tmp = strstrip(buf);
  4486. if (strlen(tmp) == 0)
  4487. return 1;
  4488. ret = kstrtol(tmp, 10, &val);
  4489. if (ret < 0)
  4490. return ret;
  4491. ret = ftrace_pid_add(val);
  4492. return ret ? ret : cnt;
  4493. }
  4494. static int
  4495. ftrace_pid_release(struct inode *inode, struct file *file)
  4496. {
  4497. if (file->f_mode & FMODE_READ)
  4498. seq_release(inode, file);
  4499. return 0;
  4500. }
  4501. static const struct file_operations ftrace_pid_fops = {
  4502. .open = ftrace_pid_open,
  4503. .write = ftrace_pid_write,
  4504. .read = seq_read,
  4505. .llseek = tracing_lseek,
  4506. .release = ftrace_pid_release,
  4507. };
  4508. static __init int ftrace_init_debugfs(void)
  4509. {
  4510. struct dentry *d_tracer;
  4511. d_tracer = tracing_init_dentry();
  4512. if (IS_ERR(d_tracer))
  4513. return 0;
  4514. ftrace_init_dyn_debugfs(d_tracer);
  4515. trace_create_file("set_ftrace_pid", 0644, d_tracer,
  4516. NULL, &ftrace_pid_fops);
  4517. ftrace_profile_debugfs(d_tracer);
  4518. return 0;
  4519. }
  4520. fs_initcall(ftrace_init_debugfs);
  4521. /**
  4522. * ftrace_kill - kill ftrace
  4523. *
  4524. * This function should be used by panic code. It stops ftrace
  4525. * but in a not so nice way. If you need to simply kill ftrace
  4526. * from a non-atomic section, use ftrace_kill.
  4527. */
  4528. void ftrace_kill(void)
  4529. {
  4530. ftrace_disabled = 1;
  4531. ftrace_enabled = 0;
  4532. clear_ftrace_function();
  4533. }
  4534. /**
  4535. * Test if ftrace is dead or not.
  4536. */
  4537. int ftrace_is_dead(void)
  4538. {
  4539. return ftrace_disabled;
  4540. }
  4541. /**
  4542. * register_ftrace_function - register a function for profiling
  4543. * @ops - ops structure that holds the function for profiling.
  4544. *
  4545. * Register a function to be called by all functions in the
  4546. * kernel.
  4547. *
  4548. * Note: @ops->func and all the functions it calls must be labeled
  4549. * with "notrace", otherwise it will go into a
  4550. * recursive loop.
  4551. */
  4552. int register_ftrace_function(struct ftrace_ops *ops)
  4553. {
  4554. int ret = -1;
  4555. ftrace_ops_init(ops);
  4556. mutex_lock(&ftrace_lock);
  4557. ret = ftrace_startup(ops, 0);
  4558. mutex_unlock(&ftrace_lock);
  4559. return ret;
  4560. }
  4561. EXPORT_SYMBOL_GPL(register_ftrace_function);
  4562. /**
  4563. * unregister_ftrace_function - unregister a function for profiling.
  4564. * @ops - ops structure that holds the function to unregister
  4565. *
  4566. * Unregister a function that was added to be called by ftrace profiling.
  4567. */
  4568. int unregister_ftrace_function(struct ftrace_ops *ops)
  4569. {
  4570. int ret;
  4571. mutex_lock(&ftrace_lock);
  4572. ret = ftrace_shutdown(ops, 0);
  4573. mutex_unlock(&ftrace_lock);
  4574. return ret;
  4575. }
  4576. EXPORT_SYMBOL_GPL(unregister_ftrace_function);
  4577. int
  4578. ftrace_enable_sysctl(struct ctl_table *table, int write,
  4579. void __user *buffer, size_t *lenp,
  4580. loff_t *ppos)
  4581. {
  4582. int ret = -ENODEV;
  4583. mutex_lock(&ftrace_lock);
  4584. if (unlikely(ftrace_disabled))
  4585. goto out;
  4586. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  4587. if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
  4588. goto out;
  4589. last_ftrace_enabled = !!ftrace_enabled;
  4590. if (ftrace_enabled) {
  4591. ftrace_startup_sysctl();
  4592. /* we are starting ftrace again */
  4593. if (ftrace_ops_list != &ftrace_list_end)
  4594. update_ftrace_function();
  4595. } else {
  4596. /* stopping ftrace calls (just send to ftrace_stub) */
  4597. ftrace_trace_function = ftrace_stub;
  4598. ftrace_shutdown_sysctl();
  4599. }
  4600. out:
  4601. mutex_unlock(&ftrace_lock);
  4602. return ret;
  4603. }
  4604. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  4605. static struct ftrace_ops graph_ops = {
  4606. .func = ftrace_stub,
  4607. .flags = FTRACE_OPS_FL_RECURSION_SAFE |
  4608. FTRACE_OPS_FL_INITIALIZED |
  4609. FTRACE_OPS_FL_STUB,
  4610. #ifdef FTRACE_GRAPH_TRAMP_ADDR
  4611. .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
  4612. /* trampoline_size is only needed for dynamically allocated tramps */
  4613. #endif
  4614. ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
  4615. };
  4616. static int ftrace_graph_active;
  4617. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
  4618. {
  4619. return 0;
  4620. }
  4621. /* The callbacks that hook a function */
  4622. trace_func_graph_ret_t ftrace_graph_return =
  4623. (trace_func_graph_ret_t)ftrace_stub;
  4624. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  4625. static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
  4626. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  4627. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  4628. {
  4629. int i;
  4630. int ret = 0;
  4631. unsigned long flags;
  4632. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  4633. struct task_struct *g, *t;
  4634. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  4635. ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
  4636. * sizeof(struct ftrace_ret_stack),
  4637. GFP_KERNEL);
  4638. if (!ret_stack_list[i]) {
  4639. start = 0;
  4640. end = i;
  4641. ret = -ENOMEM;
  4642. goto free;
  4643. }
  4644. }
  4645. read_lock_irqsave(&tasklist_lock, flags);
  4646. do_each_thread(g, t) {
  4647. if (start == end) {
  4648. ret = -EAGAIN;
  4649. goto unlock;
  4650. }
  4651. if (t->ret_stack == NULL) {
  4652. atomic_set(&t->tracing_graph_pause, 0);
  4653. atomic_set(&t->trace_overrun, 0);
  4654. t->curr_ret_stack = -1;
  4655. /* Make sure the tasks see the -1 first: */
  4656. smp_wmb();
  4657. t->ret_stack = ret_stack_list[start++];
  4658. }
  4659. } while_each_thread(g, t);
  4660. unlock:
  4661. read_unlock_irqrestore(&tasklist_lock, flags);
  4662. free:
  4663. for (i = start; i < end; i++)
  4664. kfree(ret_stack_list[i]);
  4665. return ret;
  4666. }
  4667. static void
  4668. ftrace_graph_probe_sched_switch(void *ignore,
  4669. struct task_struct *prev, struct task_struct *next)
  4670. {
  4671. unsigned long long timestamp;
  4672. int index;
  4673. /*
  4674. * Does the user want to count the time a function was asleep.
  4675. * If so, do not update the time stamps.
  4676. */
  4677. if (trace_flags & TRACE_ITER_SLEEP_TIME)
  4678. return;
  4679. timestamp = trace_clock_local();
  4680. prev->ftrace_timestamp = timestamp;
  4681. /* only process tasks that we timestamped */
  4682. if (!next->ftrace_timestamp)
  4683. return;
  4684. /*
  4685. * Update all the counters in next to make up for the
  4686. * time next was sleeping.
  4687. */
  4688. timestamp -= next->ftrace_timestamp;
  4689. for (index = next->curr_ret_stack; index >= 0; index--)
  4690. next->ret_stack[index].calltime += timestamp;
  4691. }
  4692. /* Allocate a return stack for each task */
  4693. static int start_graph_tracing(void)
  4694. {
  4695. struct ftrace_ret_stack **ret_stack_list;
  4696. int ret, cpu;
  4697. ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
  4698. sizeof(struct ftrace_ret_stack *),
  4699. GFP_KERNEL);
  4700. if (!ret_stack_list)
  4701. return -ENOMEM;
  4702. /* The cpu_boot init_task->ret_stack will never be freed */
  4703. for_each_online_cpu(cpu) {
  4704. if (!idle_task(cpu)->ret_stack)
  4705. ftrace_graph_init_idle_task(idle_task(cpu), cpu);
  4706. }
  4707. do {
  4708. ret = alloc_retstack_tasklist(ret_stack_list);
  4709. } while (ret == -EAGAIN);
  4710. if (!ret) {
  4711. ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  4712. if (ret)
  4713. pr_info("ftrace_graph: Couldn't activate tracepoint"
  4714. " probe to kernel_sched_switch\n");
  4715. }
  4716. kfree(ret_stack_list);
  4717. return ret;
  4718. }
  4719. /*
  4720. * Hibernation protection.
  4721. * The state of the current task is too much unstable during
  4722. * suspend/restore to disk. We want to protect against that.
  4723. */
  4724. static int
  4725. ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
  4726. void *unused)
  4727. {
  4728. switch (state) {
  4729. case PM_HIBERNATION_PREPARE:
  4730. pause_graph_tracing();
  4731. break;
  4732. case PM_POST_HIBERNATION:
  4733. unpause_graph_tracing();
  4734. break;
  4735. }
  4736. return NOTIFY_DONE;
  4737. }
  4738. static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
  4739. {
  4740. if (!ftrace_ops_test(&global_ops, trace->func, NULL))
  4741. return 0;
  4742. return __ftrace_graph_entry(trace);
  4743. }
  4744. /*
  4745. * The function graph tracer should only trace the functions defined
  4746. * by set_ftrace_filter and set_ftrace_notrace. If another function
  4747. * tracer ops is registered, the graph tracer requires testing the
  4748. * function against the global ops, and not just trace any function
  4749. * that any ftrace_ops registered.
  4750. */
  4751. static void update_function_graph_func(void)
  4752. {
  4753. struct ftrace_ops *op;
  4754. bool do_test = false;
  4755. /*
  4756. * The graph and global ops share the same set of functions
  4757. * to test. If any other ops is on the list, then
  4758. * the graph tracing needs to test if its the function
  4759. * it should call.
  4760. */
  4761. do_for_each_ftrace_op(op, ftrace_ops_list) {
  4762. if (op != &global_ops && op != &graph_ops &&
  4763. op != &ftrace_list_end) {
  4764. do_test = true;
  4765. /* in double loop, break out with goto */
  4766. goto out;
  4767. }
  4768. } while_for_each_ftrace_op(op);
  4769. out:
  4770. if (do_test)
  4771. ftrace_graph_entry = ftrace_graph_entry_test;
  4772. else
  4773. ftrace_graph_entry = __ftrace_graph_entry;
  4774. }
  4775. static struct notifier_block ftrace_suspend_notifier = {
  4776. .notifier_call = ftrace_suspend_notifier_call,
  4777. };
  4778. int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  4779. trace_func_graph_ent_t entryfunc)
  4780. {
  4781. int ret = 0;
  4782. mutex_lock(&ftrace_lock);
  4783. /* we currently allow only one tracer registered at a time */
  4784. if (ftrace_graph_active) {
  4785. ret = -EBUSY;
  4786. goto out;
  4787. }
  4788. register_pm_notifier(&ftrace_suspend_notifier);
  4789. ftrace_graph_active++;
  4790. ret = start_graph_tracing();
  4791. if (ret) {
  4792. ftrace_graph_active--;
  4793. goto out;
  4794. }
  4795. ftrace_graph_return = retfunc;
  4796. /*
  4797. * Update the indirect function to the entryfunc, and the
  4798. * function that gets called to the entry_test first. Then
  4799. * call the update fgraph entry function to determine if
  4800. * the entryfunc should be called directly or not.
  4801. */
  4802. __ftrace_graph_entry = entryfunc;
  4803. ftrace_graph_entry = ftrace_graph_entry_test;
  4804. update_function_graph_func();
  4805. ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
  4806. out:
  4807. mutex_unlock(&ftrace_lock);
  4808. return ret;
  4809. }
  4810. void unregister_ftrace_graph(void)
  4811. {
  4812. mutex_lock(&ftrace_lock);
  4813. if (unlikely(!ftrace_graph_active))
  4814. goto out;
  4815. ftrace_graph_active--;
  4816. ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
  4817. ftrace_graph_entry = ftrace_graph_entry_stub;
  4818. __ftrace_graph_entry = ftrace_graph_entry_stub;
  4819. ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
  4820. unregister_pm_notifier(&ftrace_suspend_notifier);
  4821. unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  4822. #ifdef CONFIG_DYNAMIC_FTRACE
  4823. /*
  4824. * Function graph does not allocate the trampoline, but
  4825. * other global_ops do. We need to reset the ALLOC_TRAMP flag
  4826. * if one was used.
  4827. */
  4828. global_ops.trampoline = save_global_trampoline;
  4829. if (save_global_flags & FTRACE_OPS_FL_ALLOC_TRAMP)
  4830. global_ops.flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
  4831. #endif
  4832. out:
  4833. mutex_unlock(&ftrace_lock);
  4834. }
  4835. static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
  4836. static void
  4837. graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
  4838. {
  4839. atomic_set(&t->tracing_graph_pause, 0);
  4840. atomic_set(&t->trace_overrun, 0);
  4841. t->ftrace_timestamp = 0;
  4842. /* make curr_ret_stack visible before we add the ret_stack */
  4843. smp_wmb();
  4844. t->ret_stack = ret_stack;
  4845. }
  4846. /*
  4847. * Allocate a return stack for the idle task. May be the first
  4848. * time through, or it may be done by CPU hotplug online.
  4849. */
  4850. void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
  4851. {
  4852. t->curr_ret_stack = -1;
  4853. /*
  4854. * The idle task has no parent, it either has its own
  4855. * stack or no stack at all.
  4856. */
  4857. if (t->ret_stack)
  4858. WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
  4859. if (ftrace_graph_active) {
  4860. struct ftrace_ret_stack *ret_stack;
  4861. ret_stack = per_cpu(idle_ret_stack, cpu);
  4862. if (!ret_stack) {
  4863. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  4864. * sizeof(struct ftrace_ret_stack),
  4865. GFP_KERNEL);
  4866. if (!ret_stack)
  4867. return;
  4868. per_cpu(idle_ret_stack, cpu) = ret_stack;
  4869. }
  4870. graph_init_task(t, ret_stack);
  4871. }
  4872. }
  4873. /* Allocate a return stack for newly created task */
  4874. void ftrace_graph_init_task(struct task_struct *t)
  4875. {
  4876. /* Make sure we do not use the parent ret_stack */
  4877. t->ret_stack = NULL;
  4878. t->curr_ret_stack = -1;
  4879. if (ftrace_graph_active) {
  4880. struct ftrace_ret_stack *ret_stack;
  4881. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  4882. * sizeof(struct ftrace_ret_stack),
  4883. GFP_KERNEL);
  4884. if (!ret_stack)
  4885. return;
  4886. graph_init_task(t, ret_stack);
  4887. }
  4888. }
  4889. void ftrace_graph_exit_task(struct task_struct *t)
  4890. {
  4891. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  4892. t->ret_stack = NULL;
  4893. /* NULL must become visible to IRQs before we free it: */
  4894. barrier();
  4895. kfree(ret_stack);
  4896. }
  4897. #endif