ftrace.c 104 KB

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