ftrace.c 97 KB

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