ftrace.c 128 KB

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