ftrace.c 111 KB

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