ftrace.c 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539
  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 struct ftrace_func_entry *
  823. ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
  824. {
  825. unsigned long key;
  826. struct ftrace_func_entry *entry;
  827. struct hlist_head *hhd;
  828. struct hlist_node *n;
  829. if (!hash->count)
  830. return NULL;
  831. if (hash->size_bits > 0)
  832. key = hash_long(ip, hash->size_bits);
  833. else
  834. key = 0;
  835. hhd = &hash->buckets[key];
  836. hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
  837. if (entry->ip == ip)
  838. return entry;
  839. }
  840. return NULL;
  841. }
  842. static void __add_hash_entry(struct ftrace_hash *hash,
  843. struct ftrace_func_entry *entry)
  844. {
  845. struct hlist_head *hhd;
  846. unsigned long key;
  847. if (hash->size_bits)
  848. key = hash_long(entry->ip, hash->size_bits);
  849. else
  850. key = 0;
  851. hhd = &hash->buckets[key];
  852. hlist_add_head(&entry->hlist, hhd);
  853. hash->count++;
  854. }
  855. static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
  856. {
  857. struct ftrace_func_entry *entry;
  858. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  859. if (!entry)
  860. return -ENOMEM;
  861. entry->ip = ip;
  862. __add_hash_entry(hash, entry);
  863. return 0;
  864. }
  865. static void
  866. free_hash_entry(struct ftrace_hash *hash,
  867. struct ftrace_func_entry *entry)
  868. {
  869. hlist_del(&entry->hlist);
  870. kfree(entry);
  871. hash->count--;
  872. }
  873. static void
  874. remove_hash_entry(struct ftrace_hash *hash,
  875. struct ftrace_func_entry *entry)
  876. {
  877. hlist_del(&entry->hlist);
  878. hash->count--;
  879. }
  880. static void ftrace_hash_clear(struct ftrace_hash *hash)
  881. {
  882. struct hlist_head *hhd;
  883. struct hlist_node *tp, *tn;
  884. struct ftrace_func_entry *entry;
  885. int size = 1 << hash->size_bits;
  886. int i;
  887. if (!hash->count)
  888. return;
  889. for (i = 0; i < size; i++) {
  890. hhd = &hash->buckets[i];
  891. hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
  892. free_hash_entry(hash, entry);
  893. }
  894. FTRACE_WARN_ON(hash->count);
  895. }
  896. static void free_ftrace_hash(struct ftrace_hash *hash)
  897. {
  898. if (!hash || hash == EMPTY_HASH)
  899. return;
  900. ftrace_hash_clear(hash);
  901. kfree(hash->buckets);
  902. kfree(hash);
  903. }
  904. static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
  905. {
  906. struct ftrace_hash *hash;
  907. hash = container_of(rcu, struct ftrace_hash, rcu);
  908. free_ftrace_hash(hash);
  909. }
  910. static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
  911. {
  912. if (!hash || hash == EMPTY_HASH)
  913. return;
  914. call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
  915. }
  916. static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
  917. {
  918. struct ftrace_hash *hash;
  919. int size;
  920. hash = kzalloc(sizeof(*hash), GFP_KERNEL);
  921. if (!hash)
  922. return NULL;
  923. size = 1 << size_bits;
  924. hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);
  925. if (!hash->buckets) {
  926. kfree(hash);
  927. return NULL;
  928. }
  929. hash->size_bits = size_bits;
  930. return hash;
  931. }
  932. static struct ftrace_hash *
  933. alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
  934. {
  935. struct ftrace_func_entry *entry;
  936. struct ftrace_hash *new_hash;
  937. struct hlist_node *tp;
  938. int size;
  939. int ret;
  940. int i;
  941. new_hash = alloc_ftrace_hash(size_bits);
  942. if (!new_hash)
  943. return NULL;
  944. /* Empty hash? */
  945. if (!hash || !hash->count)
  946. return new_hash;
  947. size = 1 << hash->size_bits;
  948. for (i = 0; i < size; i++) {
  949. hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
  950. ret = add_hash_entry(new_hash, entry->ip);
  951. if (ret < 0)
  952. goto free_hash;
  953. }
  954. }
  955. FTRACE_WARN_ON(new_hash->count != hash->count);
  956. return new_hash;
  957. free_hash:
  958. free_ftrace_hash(new_hash);
  959. return NULL;
  960. }
  961. static void
  962. ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
  963. static void
  964. ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
  965. static int
  966. ftrace_hash_move(struct ftrace_ops *ops, int enable,
  967. struct ftrace_hash **dst, struct ftrace_hash *src)
  968. {
  969. struct ftrace_func_entry *entry;
  970. struct hlist_node *tp, *tn;
  971. struct hlist_head *hhd;
  972. struct ftrace_hash *old_hash;
  973. struct ftrace_hash *new_hash;
  974. unsigned long key;
  975. int size = src->count;
  976. int bits = 0;
  977. int ret;
  978. int i;
  979. /*
  980. * Remove the current set, update the hash and add
  981. * them back.
  982. */
  983. ftrace_hash_rec_disable(ops, enable);
  984. /*
  985. * If the new source is empty, just free dst and assign it
  986. * the empty_hash.
  987. */
  988. if (!src->count) {
  989. free_ftrace_hash_rcu(*dst);
  990. rcu_assign_pointer(*dst, EMPTY_HASH);
  991. /* still need to update the function records */
  992. ret = 0;
  993. goto out;
  994. }
  995. /*
  996. * Make the hash size about 1/2 the # found
  997. */
  998. for (size /= 2; size; size >>= 1)
  999. bits++;
  1000. /* Don't allocate too much */
  1001. if (bits > FTRACE_HASH_MAX_BITS)
  1002. bits = FTRACE_HASH_MAX_BITS;
  1003. ret = -ENOMEM;
  1004. new_hash = alloc_ftrace_hash(bits);
  1005. if (!new_hash)
  1006. goto out;
  1007. size = 1 << src->size_bits;
  1008. for (i = 0; i < size; i++) {
  1009. hhd = &src->buckets[i];
  1010. hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
  1011. if (bits > 0)
  1012. key = hash_long(entry->ip, bits);
  1013. else
  1014. key = 0;
  1015. remove_hash_entry(src, entry);
  1016. __add_hash_entry(new_hash, entry);
  1017. }
  1018. }
  1019. old_hash = *dst;
  1020. rcu_assign_pointer(*dst, new_hash);
  1021. free_ftrace_hash_rcu(old_hash);
  1022. ret = 0;
  1023. out:
  1024. /*
  1025. * Enable regardless of ret:
  1026. * On success, we enable the new hash.
  1027. * On failure, we re-enable the original hash.
  1028. */
  1029. ftrace_hash_rec_enable(ops, enable);
  1030. return ret;
  1031. }
  1032. /*
  1033. * Test the hashes for this ops to see if we want to call
  1034. * the ops->func or not.
  1035. *
  1036. * It's a match if the ip is in the ops->filter_hash or
  1037. * the filter_hash does not exist or is empty,
  1038. * AND
  1039. * the ip is not in the ops->notrace_hash.
  1040. *
  1041. * This needs to be called with preemption disabled as
  1042. * the hashes are freed with call_rcu_sched().
  1043. */
  1044. static int
  1045. ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
  1046. {
  1047. struct ftrace_hash *filter_hash;
  1048. struct ftrace_hash *notrace_hash;
  1049. int ret;
  1050. filter_hash = rcu_dereference_raw(ops->filter_hash);
  1051. notrace_hash = rcu_dereference_raw(ops->notrace_hash);
  1052. if ((!filter_hash || !filter_hash->count ||
  1053. ftrace_lookup_ip(filter_hash, ip)) &&
  1054. (!notrace_hash || !notrace_hash->count ||
  1055. !ftrace_lookup_ip(notrace_hash, ip)))
  1056. ret = 1;
  1057. else
  1058. ret = 0;
  1059. return ret;
  1060. }
  1061. /*
  1062. * This is a double for. Do not use 'break' to break out of the loop,
  1063. * you must use a goto.
  1064. */
  1065. #define do_for_each_ftrace_rec(pg, rec) \
  1066. for (pg = ftrace_pages_start; pg; pg = pg->next) { \
  1067. int _____i; \
  1068. for (_____i = 0; _____i < pg->index; _____i++) { \
  1069. rec = &pg->records[_____i];
  1070. #define while_for_each_ftrace_rec() \
  1071. } \
  1072. }
  1073. static int ftrace_cmp_recs(const void *a, const void *b)
  1074. {
  1075. const struct dyn_ftrace *reca = a;
  1076. const struct dyn_ftrace *recb = b;
  1077. if (reca->ip > recb->ip)
  1078. return 1;
  1079. if (reca->ip < recb->ip)
  1080. return -1;
  1081. return 0;
  1082. }
  1083. /**
  1084. * ftrace_location - return true if the ip giving is a traced location
  1085. * @ip: the instruction pointer to check
  1086. *
  1087. * Returns 1 if @ip given is a pointer to a ftrace location.
  1088. * That is, the instruction that is either a NOP or call to
  1089. * the function tracer. It checks the ftrace internal tables to
  1090. * determine if the address belongs or not.
  1091. */
  1092. int ftrace_location(unsigned long ip)
  1093. {
  1094. struct ftrace_page *pg;
  1095. struct dyn_ftrace *rec;
  1096. struct dyn_ftrace key;
  1097. key.ip = ip;
  1098. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  1099. rec = bsearch(&key, pg->records, pg->index,
  1100. sizeof(struct dyn_ftrace),
  1101. ftrace_cmp_recs);
  1102. if (rec)
  1103. return 1;
  1104. }
  1105. return 0;
  1106. }
  1107. static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
  1108. int filter_hash,
  1109. bool inc)
  1110. {
  1111. struct ftrace_hash *hash;
  1112. struct ftrace_hash *other_hash;
  1113. struct ftrace_page *pg;
  1114. struct dyn_ftrace *rec;
  1115. int count = 0;
  1116. int all = 0;
  1117. /* Only update if the ops has been registered */
  1118. if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
  1119. return;
  1120. /*
  1121. * In the filter_hash case:
  1122. * If the count is zero, we update all records.
  1123. * Otherwise we just update the items in the hash.
  1124. *
  1125. * In the notrace_hash case:
  1126. * We enable the update in the hash.
  1127. * As disabling notrace means enabling the tracing,
  1128. * and enabling notrace means disabling, the inc variable
  1129. * gets inversed.
  1130. */
  1131. if (filter_hash) {
  1132. hash = ops->filter_hash;
  1133. other_hash = ops->notrace_hash;
  1134. if (!hash || !hash->count)
  1135. all = 1;
  1136. } else {
  1137. inc = !inc;
  1138. hash = ops->notrace_hash;
  1139. other_hash = ops->filter_hash;
  1140. /*
  1141. * If the notrace hash has no items,
  1142. * then there's nothing to do.
  1143. */
  1144. if (!hash || !hash->count)
  1145. return;
  1146. }
  1147. do_for_each_ftrace_rec(pg, rec) {
  1148. int in_other_hash = 0;
  1149. int in_hash = 0;
  1150. int match = 0;
  1151. if (all) {
  1152. /*
  1153. * Only the filter_hash affects all records.
  1154. * Update if the record is not in the notrace hash.
  1155. */
  1156. if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
  1157. match = 1;
  1158. } else {
  1159. in_hash = hash && !!ftrace_lookup_ip(hash, rec->ip);
  1160. in_other_hash = other_hash && !!ftrace_lookup_ip(other_hash, rec->ip);
  1161. /*
  1162. *
  1163. */
  1164. if (filter_hash && in_hash && !in_other_hash)
  1165. match = 1;
  1166. else if (!filter_hash && in_hash &&
  1167. (in_other_hash || !other_hash->count))
  1168. match = 1;
  1169. }
  1170. if (!match)
  1171. continue;
  1172. if (inc) {
  1173. rec->flags++;
  1174. if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
  1175. return;
  1176. } else {
  1177. if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
  1178. return;
  1179. rec->flags--;
  1180. }
  1181. count++;
  1182. /* Shortcut, if we handled all records, we are done. */
  1183. if (!all && count == hash->count)
  1184. return;
  1185. } while_for_each_ftrace_rec();
  1186. }
  1187. static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
  1188. int filter_hash)
  1189. {
  1190. __ftrace_hash_rec_update(ops, filter_hash, 0);
  1191. }
  1192. static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
  1193. int filter_hash)
  1194. {
  1195. __ftrace_hash_rec_update(ops, filter_hash, 1);
  1196. }
  1197. static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
  1198. {
  1199. if (ftrace_pages->index == ftrace_pages->size) {
  1200. /* We should have allocated enough */
  1201. if (WARN_ON(!ftrace_pages->next))
  1202. return NULL;
  1203. ftrace_pages = ftrace_pages->next;
  1204. }
  1205. return &ftrace_pages->records[ftrace_pages->index++];
  1206. }
  1207. static struct dyn_ftrace *
  1208. ftrace_record_ip(unsigned long ip)
  1209. {
  1210. struct dyn_ftrace *rec;
  1211. if (ftrace_disabled)
  1212. return NULL;
  1213. rec = ftrace_alloc_dyn_node(ip);
  1214. if (!rec)
  1215. return NULL;
  1216. rec->ip = ip;
  1217. return rec;
  1218. }
  1219. static void print_ip_ins(const char *fmt, unsigned char *p)
  1220. {
  1221. int i;
  1222. printk(KERN_CONT "%s", fmt);
  1223. for (i = 0; i < MCOUNT_INSN_SIZE; i++)
  1224. printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
  1225. }
  1226. /**
  1227. * ftrace_bug - report and shutdown function tracer
  1228. * @failed: The failed type (EFAULT, EINVAL, EPERM)
  1229. * @ip: The address that failed
  1230. *
  1231. * The arch code that enables or disables the function tracing
  1232. * can call ftrace_bug() when it has detected a problem in
  1233. * modifying the code. @failed should be one of either:
  1234. * EFAULT - if the problem happens on reading the @ip address
  1235. * EINVAL - if what is read at @ip is not what was expected
  1236. * EPERM - if the problem happens on writting to the @ip address
  1237. */
  1238. void ftrace_bug(int failed, unsigned long ip)
  1239. {
  1240. switch (failed) {
  1241. case -EFAULT:
  1242. FTRACE_WARN_ON_ONCE(1);
  1243. pr_info("ftrace faulted on modifying ");
  1244. print_ip_sym(ip);
  1245. break;
  1246. case -EINVAL:
  1247. FTRACE_WARN_ON_ONCE(1);
  1248. pr_info("ftrace failed to modify ");
  1249. print_ip_sym(ip);
  1250. print_ip_ins(" actual: ", (unsigned char *)ip);
  1251. printk(KERN_CONT "\n");
  1252. break;
  1253. case -EPERM:
  1254. FTRACE_WARN_ON_ONCE(1);
  1255. pr_info("ftrace faulted on writing ");
  1256. print_ip_sym(ip);
  1257. break;
  1258. default:
  1259. FTRACE_WARN_ON_ONCE(1);
  1260. pr_info("ftrace faulted on unknown error ");
  1261. print_ip_sym(ip);
  1262. }
  1263. }
  1264. /* Return 1 if the address range is reserved for ftrace */
  1265. int ftrace_text_reserved(void *start, void *end)
  1266. {
  1267. struct dyn_ftrace *rec;
  1268. struct ftrace_page *pg;
  1269. do_for_each_ftrace_rec(pg, rec) {
  1270. if (rec->ip <= (unsigned long)end &&
  1271. rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
  1272. return 1;
  1273. } while_for_each_ftrace_rec();
  1274. return 0;
  1275. }
  1276. static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
  1277. {
  1278. unsigned long flag = 0UL;
  1279. /*
  1280. * If we are updating calls:
  1281. *
  1282. * If the record has a ref count, then we need to enable it
  1283. * because someone is using it.
  1284. *
  1285. * Otherwise we make sure its disabled.
  1286. *
  1287. * If we are disabling calls, then disable all records that
  1288. * are enabled.
  1289. */
  1290. if (enable && (rec->flags & ~FTRACE_FL_MASK))
  1291. flag = FTRACE_FL_ENABLED;
  1292. /* If the state of this record hasn't changed, then do nothing */
  1293. if ((rec->flags & FTRACE_FL_ENABLED) == flag)
  1294. return FTRACE_UPDATE_IGNORE;
  1295. if (flag) {
  1296. if (update)
  1297. rec->flags |= FTRACE_FL_ENABLED;
  1298. return FTRACE_UPDATE_MAKE_CALL;
  1299. }
  1300. if (update)
  1301. rec->flags &= ~FTRACE_FL_ENABLED;
  1302. return FTRACE_UPDATE_MAKE_NOP;
  1303. }
  1304. /**
  1305. * ftrace_update_record, set a record that now is tracing or not
  1306. * @rec: the record to update
  1307. * @enable: set to 1 if the record is tracing, zero to force disable
  1308. *
  1309. * The records that represent all functions that can be traced need
  1310. * to be updated when tracing has been enabled.
  1311. */
  1312. int ftrace_update_record(struct dyn_ftrace *rec, int enable)
  1313. {
  1314. return ftrace_check_record(rec, enable, 1);
  1315. }
  1316. /**
  1317. * ftrace_test_record, check if the record has been enabled or not
  1318. * @rec: the record to test
  1319. * @enable: set to 1 to check if enabled, 0 if it is disabled
  1320. *
  1321. * The arch code may need to test if a record is already set to
  1322. * tracing to determine how to modify the function code that it
  1323. * represents.
  1324. */
  1325. int ftrace_test_record(struct dyn_ftrace *rec, int enable)
  1326. {
  1327. return ftrace_check_record(rec, enable, 0);
  1328. }
  1329. static int
  1330. __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
  1331. {
  1332. unsigned long ftrace_addr;
  1333. int ret;
  1334. ftrace_addr = (unsigned long)FTRACE_ADDR;
  1335. ret = ftrace_update_record(rec, enable);
  1336. switch (ret) {
  1337. case FTRACE_UPDATE_IGNORE:
  1338. return 0;
  1339. case FTRACE_UPDATE_MAKE_CALL:
  1340. return ftrace_make_call(rec, ftrace_addr);
  1341. case FTRACE_UPDATE_MAKE_NOP:
  1342. return ftrace_make_nop(NULL, rec, ftrace_addr);
  1343. }
  1344. return -1; /* unknow ftrace bug */
  1345. }
  1346. static void ftrace_replace_code(int update)
  1347. {
  1348. struct dyn_ftrace *rec;
  1349. struct ftrace_page *pg;
  1350. int failed;
  1351. if (unlikely(ftrace_disabled))
  1352. return;
  1353. do_for_each_ftrace_rec(pg, rec) {
  1354. failed = __ftrace_replace_code(rec, update);
  1355. if (failed) {
  1356. ftrace_bug(failed, rec->ip);
  1357. /* Stop processing */
  1358. return;
  1359. }
  1360. } while_for_each_ftrace_rec();
  1361. }
  1362. struct ftrace_rec_iter {
  1363. struct ftrace_page *pg;
  1364. int index;
  1365. };
  1366. /**
  1367. * ftrace_rec_iter_start, start up iterating over traced functions
  1368. *
  1369. * Returns an iterator handle that is used to iterate over all
  1370. * the records that represent address locations where functions
  1371. * are traced.
  1372. *
  1373. * May return NULL if no records are available.
  1374. */
  1375. struct ftrace_rec_iter *ftrace_rec_iter_start(void)
  1376. {
  1377. /*
  1378. * We only use a single iterator.
  1379. * Protected by the ftrace_lock mutex.
  1380. */
  1381. static struct ftrace_rec_iter ftrace_rec_iter;
  1382. struct ftrace_rec_iter *iter = &ftrace_rec_iter;
  1383. iter->pg = ftrace_pages_start;
  1384. iter->index = 0;
  1385. /* Could have empty pages */
  1386. while (iter->pg && !iter->pg->index)
  1387. iter->pg = iter->pg->next;
  1388. if (!iter->pg)
  1389. return NULL;
  1390. return iter;
  1391. }
  1392. /**
  1393. * ftrace_rec_iter_next, get the next record to process.
  1394. * @iter: The handle to the iterator.
  1395. *
  1396. * Returns the next iterator after the given iterator @iter.
  1397. */
  1398. struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
  1399. {
  1400. iter->index++;
  1401. if (iter->index >= iter->pg->index) {
  1402. iter->pg = iter->pg->next;
  1403. iter->index = 0;
  1404. /* Could have empty pages */
  1405. while (iter->pg && !iter->pg->index)
  1406. iter->pg = iter->pg->next;
  1407. }
  1408. if (!iter->pg)
  1409. return NULL;
  1410. return iter;
  1411. }
  1412. /**
  1413. * ftrace_rec_iter_record, get the record at the iterator location
  1414. * @iter: The current iterator location
  1415. *
  1416. * Returns the record that the current @iter is at.
  1417. */
  1418. struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
  1419. {
  1420. return &iter->pg->records[iter->index];
  1421. }
  1422. static int
  1423. ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
  1424. {
  1425. unsigned long ip;
  1426. int ret;
  1427. ip = rec->ip;
  1428. if (unlikely(ftrace_disabled))
  1429. return 0;
  1430. ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
  1431. if (ret) {
  1432. ftrace_bug(ret, ip);
  1433. return 0;
  1434. }
  1435. return 1;
  1436. }
  1437. /*
  1438. * archs can override this function if they must do something
  1439. * before the modifying code is performed.
  1440. */
  1441. int __weak ftrace_arch_code_modify_prepare(void)
  1442. {
  1443. return 0;
  1444. }
  1445. /*
  1446. * archs can override this function if they must do something
  1447. * after the modifying code is performed.
  1448. */
  1449. int __weak ftrace_arch_code_modify_post_process(void)
  1450. {
  1451. return 0;
  1452. }
  1453. static int __ftrace_modify_code(void *data)
  1454. {
  1455. int *command = data;
  1456. if (*command & FTRACE_UPDATE_CALLS)
  1457. ftrace_replace_code(1);
  1458. else if (*command & FTRACE_DISABLE_CALLS)
  1459. ftrace_replace_code(0);
  1460. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  1461. ftrace_update_ftrace_func(ftrace_trace_function);
  1462. if (*command & FTRACE_START_FUNC_RET)
  1463. ftrace_enable_ftrace_graph_caller();
  1464. else if (*command & FTRACE_STOP_FUNC_RET)
  1465. ftrace_disable_ftrace_graph_caller();
  1466. return 0;
  1467. }
  1468. /**
  1469. * ftrace_run_stop_machine, go back to the stop machine method
  1470. * @command: The command to tell ftrace what to do
  1471. *
  1472. * If an arch needs to fall back to the stop machine method, the
  1473. * it can call this function.
  1474. */
  1475. void ftrace_run_stop_machine(int command)
  1476. {
  1477. stop_machine(__ftrace_modify_code, &command, NULL);
  1478. }
  1479. /**
  1480. * arch_ftrace_update_code, modify the code to trace or not trace
  1481. * @command: The command that needs to be done
  1482. *
  1483. * Archs can override this function if it does not need to
  1484. * run stop_machine() to modify code.
  1485. */
  1486. void __weak arch_ftrace_update_code(int command)
  1487. {
  1488. ftrace_run_stop_machine(command);
  1489. }
  1490. static void ftrace_run_update_code(int command)
  1491. {
  1492. int ret;
  1493. ret = ftrace_arch_code_modify_prepare();
  1494. FTRACE_WARN_ON(ret);
  1495. if (ret)
  1496. return;
  1497. /*
  1498. * Do not call function tracer while we update the code.
  1499. * We are in stop machine.
  1500. */
  1501. function_trace_stop++;
  1502. /*
  1503. * By default we use stop_machine() to modify the code.
  1504. * But archs can do what ever they want as long as it
  1505. * is safe. The stop_machine() is the safest, but also
  1506. * produces the most overhead.
  1507. */
  1508. arch_ftrace_update_code(command);
  1509. #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  1510. /*
  1511. * For archs that call ftrace_test_stop_func(), we must
  1512. * wait till after we update all the function callers
  1513. * before we update the callback. This keeps different
  1514. * ops that record different functions from corrupting
  1515. * each other.
  1516. */
  1517. __ftrace_trace_function = __ftrace_trace_function_delay;
  1518. #endif
  1519. function_trace_stop--;
  1520. ret = ftrace_arch_code_modify_post_process();
  1521. FTRACE_WARN_ON(ret);
  1522. }
  1523. static ftrace_func_t saved_ftrace_func;
  1524. static int ftrace_start_up;
  1525. static int global_start_up;
  1526. static void ftrace_startup_enable(int command)
  1527. {
  1528. if (saved_ftrace_func != ftrace_trace_function) {
  1529. saved_ftrace_func = ftrace_trace_function;
  1530. command |= FTRACE_UPDATE_TRACE_FUNC;
  1531. }
  1532. if (!command || !ftrace_enabled)
  1533. return;
  1534. ftrace_run_update_code(command);
  1535. }
  1536. static int ftrace_startup(struct ftrace_ops *ops, int command)
  1537. {
  1538. bool hash_enable = true;
  1539. if (unlikely(ftrace_disabled))
  1540. return -ENODEV;
  1541. ftrace_start_up++;
  1542. command |= FTRACE_UPDATE_CALLS;
  1543. /* ops marked global share the filter hashes */
  1544. if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
  1545. ops = &global_ops;
  1546. /* Don't update hash if global is already set */
  1547. if (global_start_up)
  1548. hash_enable = false;
  1549. global_start_up++;
  1550. }
  1551. ops->flags |= FTRACE_OPS_FL_ENABLED;
  1552. if (hash_enable)
  1553. ftrace_hash_rec_enable(ops, 1);
  1554. ftrace_startup_enable(command);
  1555. return 0;
  1556. }
  1557. static void ftrace_shutdown(struct ftrace_ops *ops, int command)
  1558. {
  1559. bool hash_disable = true;
  1560. if (unlikely(ftrace_disabled))
  1561. return;
  1562. ftrace_start_up--;
  1563. /*
  1564. * Just warn in case of unbalance, no need to kill ftrace, it's not
  1565. * critical but the ftrace_call callers may be never nopped again after
  1566. * further ftrace uses.
  1567. */
  1568. WARN_ON_ONCE(ftrace_start_up < 0);
  1569. if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
  1570. ops = &global_ops;
  1571. global_start_up--;
  1572. WARN_ON_ONCE(global_start_up < 0);
  1573. /* Don't update hash if global still has users */
  1574. if (global_start_up) {
  1575. WARN_ON_ONCE(!ftrace_start_up);
  1576. hash_disable = false;
  1577. }
  1578. }
  1579. if (hash_disable)
  1580. ftrace_hash_rec_disable(ops, 1);
  1581. if (ops != &global_ops || !global_start_up)
  1582. ops->flags &= ~FTRACE_OPS_FL_ENABLED;
  1583. command |= FTRACE_UPDATE_CALLS;
  1584. if (saved_ftrace_func != ftrace_trace_function) {
  1585. saved_ftrace_func = ftrace_trace_function;
  1586. command |= FTRACE_UPDATE_TRACE_FUNC;
  1587. }
  1588. if (!command || !ftrace_enabled)
  1589. return;
  1590. ftrace_run_update_code(command);
  1591. }
  1592. static void ftrace_startup_sysctl(void)
  1593. {
  1594. if (unlikely(ftrace_disabled))
  1595. return;
  1596. /* Force update next time */
  1597. saved_ftrace_func = NULL;
  1598. /* ftrace_start_up is true if we want ftrace running */
  1599. if (ftrace_start_up)
  1600. ftrace_run_update_code(FTRACE_UPDATE_CALLS);
  1601. }
  1602. static void ftrace_shutdown_sysctl(void)
  1603. {
  1604. if (unlikely(ftrace_disabled))
  1605. return;
  1606. /* ftrace_start_up is true if ftrace is running */
  1607. if (ftrace_start_up)
  1608. ftrace_run_update_code(FTRACE_DISABLE_CALLS);
  1609. }
  1610. static cycle_t ftrace_update_time;
  1611. static unsigned long ftrace_update_cnt;
  1612. unsigned long ftrace_update_tot_cnt;
  1613. static int ops_traces_mod(struct ftrace_ops *ops)
  1614. {
  1615. struct ftrace_hash *hash;
  1616. hash = ops->filter_hash;
  1617. return !!(!hash || !hash->count);
  1618. }
  1619. static int ftrace_update_code(struct module *mod)
  1620. {
  1621. struct ftrace_page *pg;
  1622. struct dyn_ftrace *p;
  1623. cycle_t start, stop;
  1624. unsigned long ref = 0;
  1625. int i;
  1626. /*
  1627. * When adding a module, we need to check if tracers are
  1628. * currently enabled and if they are set to trace all functions.
  1629. * If they are, we need to enable the module functions as well
  1630. * as update the reference counts for those function records.
  1631. */
  1632. if (mod) {
  1633. struct ftrace_ops *ops;
  1634. for (ops = ftrace_ops_list;
  1635. ops != &ftrace_list_end; ops = ops->next) {
  1636. if (ops->flags & FTRACE_OPS_FL_ENABLED &&
  1637. ops_traces_mod(ops))
  1638. ref++;
  1639. }
  1640. }
  1641. start = ftrace_now(raw_smp_processor_id());
  1642. ftrace_update_cnt = 0;
  1643. for (pg = ftrace_new_pgs; pg; pg = pg->next) {
  1644. for (i = 0; i < pg->index; i++) {
  1645. /* If something went wrong, bail without enabling anything */
  1646. if (unlikely(ftrace_disabled))
  1647. return -1;
  1648. p = &pg->records[i];
  1649. p->flags = ref;
  1650. /*
  1651. * Do the initial record conversion from mcount jump
  1652. * to the NOP instructions.
  1653. */
  1654. if (!ftrace_code_disable(mod, p))
  1655. break;
  1656. ftrace_update_cnt++;
  1657. /*
  1658. * If the tracing is enabled, go ahead and enable the record.
  1659. *
  1660. * The reason not to enable the record immediatelly is the
  1661. * inherent check of ftrace_make_nop/ftrace_make_call for
  1662. * correct previous instructions. Making first the NOP
  1663. * conversion puts the module to the correct state, thus
  1664. * passing the ftrace_make_call check.
  1665. */
  1666. if (ftrace_start_up && ref) {
  1667. int failed = __ftrace_replace_code(p, 1);
  1668. if (failed)
  1669. ftrace_bug(failed, p->ip);
  1670. }
  1671. }
  1672. }
  1673. ftrace_new_pgs = NULL;
  1674. stop = ftrace_now(raw_smp_processor_id());
  1675. ftrace_update_time = stop - start;
  1676. ftrace_update_tot_cnt += ftrace_update_cnt;
  1677. return 0;
  1678. }
  1679. static int ftrace_allocate_records(struct ftrace_page *pg, int count)
  1680. {
  1681. int order;
  1682. int cnt;
  1683. if (WARN_ON(!count))
  1684. return -EINVAL;
  1685. order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
  1686. /*
  1687. * We want to fill as much as possible. No more than a page
  1688. * may be empty.
  1689. */
  1690. while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
  1691. order--;
  1692. again:
  1693. pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
  1694. if (!pg->records) {
  1695. /* if we can't allocate this size, try something smaller */
  1696. if (!order)
  1697. return -ENOMEM;
  1698. order >>= 1;
  1699. goto again;
  1700. }
  1701. cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
  1702. pg->size = cnt;
  1703. if (cnt > count)
  1704. cnt = count;
  1705. return cnt;
  1706. }
  1707. static struct ftrace_page *
  1708. ftrace_allocate_pages(unsigned long num_to_init)
  1709. {
  1710. struct ftrace_page *start_pg;
  1711. struct ftrace_page *pg;
  1712. int order;
  1713. int cnt;
  1714. if (!num_to_init)
  1715. return 0;
  1716. start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
  1717. if (!pg)
  1718. return NULL;
  1719. /*
  1720. * Try to allocate as much as possible in one continues
  1721. * location that fills in all of the space. We want to
  1722. * waste as little space as possible.
  1723. */
  1724. for (;;) {
  1725. cnt = ftrace_allocate_records(pg, num_to_init);
  1726. if (cnt < 0)
  1727. goto free_pages;
  1728. num_to_init -= cnt;
  1729. if (!num_to_init)
  1730. break;
  1731. pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
  1732. if (!pg->next)
  1733. goto free_pages;
  1734. pg = pg->next;
  1735. }
  1736. return start_pg;
  1737. free_pages:
  1738. while (start_pg) {
  1739. order = get_count_order(pg->size / ENTRIES_PER_PAGE);
  1740. free_pages((unsigned long)pg->records, order);
  1741. start_pg = pg->next;
  1742. kfree(pg);
  1743. pg = start_pg;
  1744. }
  1745. pr_info("ftrace: FAILED to allocate memory for functions\n");
  1746. return NULL;
  1747. }
  1748. static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
  1749. {
  1750. int cnt;
  1751. if (!num_to_init) {
  1752. pr_info("ftrace: No functions to be traced?\n");
  1753. return -1;
  1754. }
  1755. cnt = num_to_init / ENTRIES_PER_PAGE;
  1756. pr_info("ftrace: allocating %ld entries in %d pages\n",
  1757. num_to_init, cnt + 1);
  1758. return 0;
  1759. }
  1760. enum {
  1761. FTRACE_ITER_FILTER = (1 << 0),
  1762. FTRACE_ITER_NOTRACE = (1 << 1),
  1763. FTRACE_ITER_PRINTALL = (1 << 2),
  1764. FTRACE_ITER_HASH = (1 << 3),
  1765. FTRACE_ITER_ENABLED = (1 << 4),
  1766. };
  1767. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  1768. struct ftrace_iterator {
  1769. loff_t pos;
  1770. loff_t func_pos;
  1771. struct ftrace_page *pg;
  1772. struct dyn_ftrace *func;
  1773. struct ftrace_func_probe *probe;
  1774. struct trace_parser parser;
  1775. struct ftrace_hash *hash;
  1776. struct ftrace_ops *ops;
  1777. int hidx;
  1778. int idx;
  1779. unsigned flags;
  1780. };
  1781. static void *
  1782. t_hash_next(struct seq_file *m, loff_t *pos)
  1783. {
  1784. struct ftrace_iterator *iter = m->private;
  1785. struct hlist_node *hnd = NULL;
  1786. struct hlist_head *hhd;
  1787. (*pos)++;
  1788. iter->pos = *pos;
  1789. if (iter->probe)
  1790. hnd = &iter->probe->node;
  1791. retry:
  1792. if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
  1793. return NULL;
  1794. hhd = &ftrace_func_hash[iter->hidx];
  1795. if (hlist_empty(hhd)) {
  1796. iter->hidx++;
  1797. hnd = NULL;
  1798. goto retry;
  1799. }
  1800. if (!hnd)
  1801. hnd = hhd->first;
  1802. else {
  1803. hnd = hnd->next;
  1804. if (!hnd) {
  1805. iter->hidx++;
  1806. goto retry;
  1807. }
  1808. }
  1809. if (WARN_ON_ONCE(!hnd))
  1810. return NULL;
  1811. iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
  1812. return iter;
  1813. }
  1814. static void *t_hash_start(struct seq_file *m, loff_t *pos)
  1815. {
  1816. struct ftrace_iterator *iter = m->private;
  1817. void *p = NULL;
  1818. loff_t l;
  1819. if (iter->func_pos > *pos)
  1820. return NULL;
  1821. iter->hidx = 0;
  1822. for (l = 0; l <= (*pos - iter->func_pos); ) {
  1823. p = t_hash_next(m, &l);
  1824. if (!p)
  1825. break;
  1826. }
  1827. if (!p)
  1828. return NULL;
  1829. /* Only set this if we have an item */
  1830. iter->flags |= FTRACE_ITER_HASH;
  1831. return iter;
  1832. }
  1833. static int
  1834. t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
  1835. {
  1836. struct ftrace_func_probe *rec;
  1837. rec = iter->probe;
  1838. if (WARN_ON_ONCE(!rec))
  1839. return -EIO;
  1840. if (rec->ops->print)
  1841. return rec->ops->print(m, rec->ip, rec->ops, rec->data);
  1842. seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
  1843. if (rec->data)
  1844. seq_printf(m, ":%p", rec->data);
  1845. seq_putc(m, '\n');
  1846. return 0;
  1847. }
  1848. static void *
  1849. t_next(struct seq_file *m, void *v, loff_t *pos)
  1850. {
  1851. struct ftrace_iterator *iter = m->private;
  1852. struct ftrace_ops *ops = &global_ops;
  1853. struct dyn_ftrace *rec = NULL;
  1854. if (unlikely(ftrace_disabled))
  1855. return NULL;
  1856. if (iter->flags & FTRACE_ITER_HASH)
  1857. return t_hash_next(m, pos);
  1858. (*pos)++;
  1859. iter->pos = iter->func_pos = *pos;
  1860. if (iter->flags & FTRACE_ITER_PRINTALL)
  1861. return t_hash_start(m, pos);
  1862. retry:
  1863. if (iter->idx >= iter->pg->index) {
  1864. if (iter->pg->next) {
  1865. iter->pg = iter->pg->next;
  1866. iter->idx = 0;
  1867. goto retry;
  1868. }
  1869. } else {
  1870. rec = &iter->pg->records[iter->idx++];
  1871. if (((iter->flags & FTRACE_ITER_FILTER) &&
  1872. !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
  1873. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  1874. !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
  1875. ((iter->flags & FTRACE_ITER_ENABLED) &&
  1876. !(rec->flags & ~FTRACE_FL_MASK))) {
  1877. rec = NULL;
  1878. goto retry;
  1879. }
  1880. }
  1881. if (!rec)
  1882. return t_hash_start(m, pos);
  1883. iter->func = rec;
  1884. return iter;
  1885. }
  1886. static void reset_iter_read(struct ftrace_iterator *iter)
  1887. {
  1888. iter->pos = 0;
  1889. iter->func_pos = 0;
  1890. iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
  1891. }
  1892. static void *t_start(struct seq_file *m, loff_t *pos)
  1893. {
  1894. struct ftrace_iterator *iter = m->private;
  1895. struct ftrace_ops *ops = &global_ops;
  1896. void *p = NULL;
  1897. loff_t l;
  1898. mutex_lock(&ftrace_lock);
  1899. if (unlikely(ftrace_disabled))
  1900. return NULL;
  1901. /*
  1902. * If an lseek was done, then reset and start from beginning.
  1903. */
  1904. if (*pos < iter->pos)
  1905. reset_iter_read(iter);
  1906. /*
  1907. * For set_ftrace_filter reading, if we have the filter
  1908. * off, we can short cut and just print out that all
  1909. * functions are enabled.
  1910. */
  1911. if (iter->flags & FTRACE_ITER_FILTER && !ops->filter_hash->count) {
  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. if (iter->flags & FTRACE_ITER_FILTER)
  1935. return t_hash_start(m, pos);
  1936. return NULL;
  1937. }
  1938. return iter;
  1939. }
  1940. static void t_stop(struct seq_file *m, void *p)
  1941. {
  1942. mutex_unlock(&ftrace_lock);
  1943. }
  1944. static int t_show(struct seq_file *m, void *v)
  1945. {
  1946. struct ftrace_iterator *iter = m->private;
  1947. struct dyn_ftrace *rec;
  1948. if (iter->flags & FTRACE_ITER_HASH)
  1949. return t_hash_show(m, iter);
  1950. if (iter->flags & FTRACE_ITER_PRINTALL) {
  1951. seq_printf(m, "#### all functions enabled ####\n");
  1952. return 0;
  1953. }
  1954. rec = iter->func;
  1955. if (!rec)
  1956. return 0;
  1957. seq_printf(m, "%ps", (void *)rec->ip);
  1958. if (iter->flags & FTRACE_ITER_ENABLED)
  1959. seq_printf(m, " (%ld)",
  1960. rec->flags & ~FTRACE_FL_MASK);
  1961. seq_printf(m, "\n");
  1962. return 0;
  1963. }
  1964. static const struct seq_operations show_ftrace_seq_ops = {
  1965. .start = t_start,
  1966. .next = t_next,
  1967. .stop = t_stop,
  1968. .show = t_show,
  1969. };
  1970. static int
  1971. ftrace_avail_open(struct inode *inode, struct file *file)
  1972. {
  1973. struct ftrace_iterator *iter;
  1974. int ret;
  1975. if (unlikely(ftrace_disabled))
  1976. return -ENODEV;
  1977. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1978. if (!iter)
  1979. return -ENOMEM;
  1980. iter->pg = ftrace_pages_start;
  1981. ret = seq_open(file, &show_ftrace_seq_ops);
  1982. if (!ret) {
  1983. struct seq_file *m = file->private_data;
  1984. m->private = iter;
  1985. } else {
  1986. kfree(iter);
  1987. }
  1988. return ret;
  1989. }
  1990. static int
  1991. ftrace_enabled_open(struct inode *inode, struct file *file)
  1992. {
  1993. struct ftrace_iterator *iter;
  1994. int ret;
  1995. if (unlikely(ftrace_disabled))
  1996. return -ENODEV;
  1997. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1998. if (!iter)
  1999. return -ENOMEM;
  2000. iter->pg = ftrace_pages_start;
  2001. iter->flags = FTRACE_ITER_ENABLED;
  2002. ret = seq_open(file, &show_ftrace_seq_ops);
  2003. if (!ret) {
  2004. struct seq_file *m = file->private_data;
  2005. m->private = iter;
  2006. } else {
  2007. kfree(iter);
  2008. }
  2009. return ret;
  2010. }
  2011. static void ftrace_filter_reset(struct ftrace_hash *hash)
  2012. {
  2013. mutex_lock(&ftrace_lock);
  2014. ftrace_hash_clear(hash);
  2015. mutex_unlock(&ftrace_lock);
  2016. }
  2017. static int
  2018. ftrace_regex_open(struct ftrace_ops *ops, int flag,
  2019. struct inode *inode, struct file *file)
  2020. {
  2021. struct ftrace_iterator *iter;
  2022. struct ftrace_hash *hash;
  2023. int ret = 0;
  2024. if (unlikely(ftrace_disabled))
  2025. return -ENODEV;
  2026. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  2027. if (!iter)
  2028. return -ENOMEM;
  2029. if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
  2030. kfree(iter);
  2031. return -ENOMEM;
  2032. }
  2033. if (flag & FTRACE_ITER_NOTRACE)
  2034. hash = ops->notrace_hash;
  2035. else
  2036. hash = ops->filter_hash;
  2037. iter->ops = ops;
  2038. iter->flags = flag;
  2039. if (file->f_mode & FMODE_WRITE) {
  2040. mutex_lock(&ftrace_lock);
  2041. iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
  2042. mutex_unlock(&ftrace_lock);
  2043. if (!iter->hash) {
  2044. trace_parser_put(&iter->parser);
  2045. kfree(iter);
  2046. return -ENOMEM;
  2047. }
  2048. }
  2049. mutex_lock(&ftrace_regex_lock);
  2050. if ((file->f_mode & FMODE_WRITE) &&
  2051. (file->f_flags & O_TRUNC))
  2052. ftrace_filter_reset(iter->hash);
  2053. if (file->f_mode & FMODE_READ) {
  2054. iter->pg = ftrace_pages_start;
  2055. ret = seq_open(file, &show_ftrace_seq_ops);
  2056. if (!ret) {
  2057. struct seq_file *m = file->private_data;
  2058. m->private = iter;
  2059. } else {
  2060. /* Failed */
  2061. free_ftrace_hash(iter->hash);
  2062. trace_parser_put(&iter->parser);
  2063. kfree(iter);
  2064. }
  2065. } else
  2066. file->private_data = iter;
  2067. mutex_unlock(&ftrace_regex_lock);
  2068. return ret;
  2069. }
  2070. static int
  2071. ftrace_filter_open(struct inode *inode, struct file *file)
  2072. {
  2073. return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER,
  2074. inode, file);
  2075. }
  2076. static int
  2077. ftrace_notrace_open(struct inode *inode, struct file *file)
  2078. {
  2079. return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
  2080. inode, file);
  2081. }
  2082. static loff_t
  2083. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  2084. {
  2085. loff_t ret;
  2086. if (file->f_mode & FMODE_READ)
  2087. ret = seq_lseek(file, offset, origin);
  2088. else
  2089. file->f_pos = ret = 1;
  2090. return ret;
  2091. }
  2092. static int ftrace_match(char *str, char *regex, int len, int type)
  2093. {
  2094. int matched = 0;
  2095. int slen;
  2096. switch (type) {
  2097. case MATCH_FULL:
  2098. if (strcmp(str, regex) == 0)
  2099. matched = 1;
  2100. break;
  2101. case MATCH_FRONT_ONLY:
  2102. if (strncmp(str, regex, len) == 0)
  2103. matched = 1;
  2104. break;
  2105. case MATCH_MIDDLE_ONLY:
  2106. if (strstr(str, regex))
  2107. matched = 1;
  2108. break;
  2109. case MATCH_END_ONLY:
  2110. slen = strlen(str);
  2111. if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
  2112. matched = 1;
  2113. break;
  2114. }
  2115. return matched;
  2116. }
  2117. static int
  2118. enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
  2119. {
  2120. struct ftrace_func_entry *entry;
  2121. int ret = 0;
  2122. entry = ftrace_lookup_ip(hash, rec->ip);
  2123. if (not) {
  2124. /* Do nothing if it doesn't exist */
  2125. if (!entry)
  2126. return 0;
  2127. free_hash_entry(hash, entry);
  2128. } else {
  2129. /* Do nothing if it exists */
  2130. if (entry)
  2131. return 0;
  2132. ret = add_hash_entry(hash, rec->ip);
  2133. }
  2134. return ret;
  2135. }
  2136. static int
  2137. ftrace_match_record(struct dyn_ftrace *rec, char *mod,
  2138. char *regex, int len, int type)
  2139. {
  2140. char str[KSYM_SYMBOL_LEN];
  2141. char *modname;
  2142. kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
  2143. if (mod) {
  2144. /* module lookup requires matching the module */
  2145. if (!modname || strcmp(modname, mod))
  2146. return 0;
  2147. /* blank search means to match all funcs in the mod */
  2148. if (!len)
  2149. return 1;
  2150. }
  2151. return ftrace_match(str, regex, len, type);
  2152. }
  2153. static int
  2154. match_records(struct ftrace_hash *hash, char *buff,
  2155. int len, char *mod, int not)
  2156. {
  2157. unsigned search_len = 0;
  2158. struct ftrace_page *pg;
  2159. struct dyn_ftrace *rec;
  2160. int type = MATCH_FULL;
  2161. char *search = buff;
  2162. int found = 0;
  2163. int ret;
  2164. if (len) {
  2165. type = filter_parse_regex(buff, len, &search, &not);
  2166. search_len = strlen(search);
  2167. }
  2168. mutex_lock(&ftrace_lock);
  2169. if (unlikely(ftrace_disabled))
  2170. goto out_unlock;
  2171. do_for_each_ftrace_rec(pg, rec) {
  2172. if (ftrace_match_record(rec, mod, search, search_len, type)) {
  2173. ret = enter_record(hash, rec, not);
  2174. if (ret < 0) {
  2175. found = ret;
  2176. goto out_unlock;
  2177. }
  2178. found = 1;
  2179. }
  2180. } while_for_each_ftrace_rec();
  2181. out_unlock:
  2182. mutex_unlock(&ftrace_lock);
  2183. return found;
  2184. }
  2185. static int
  2186. ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
  2187. {
  2188. return match_records(hash, buff, len, NULL, 0);
  2189. }
  2190. static int
  2191. ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
  2192. {
  2193. int not = 0;
  2194. /* blank or '*' mean the same */
  2195. if (strcmp(buff, "*") == 0)
  2196. buff[0] = 0;
  2197. /* handle the case of 'dont filter this module' */
  2198. if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
  2199. buff[0] = 0;
  2200. not = 1;
  2201. }
  2202. return match_records(hash, buff, strlen(buff), mod, not);
  2203. }
  2204. /*
  2205. * We register the module command as a template to show others how
  2206. * to register the a command as well.
  2207. */
  2208. static int
  2209. ftrace_mod_callback(struct ftrace_hash *hash,
  2210. char *func, char *cmd, char *param, int enable)
  2211. {
  2212. char *mod;
  2213. int ret = -EINVAL;
  2214. /*
  2215. * cmd == 'mod' because we only registered this func
  2216. * for the 'mod' ftrace_func_command.
  2217. * But if you register one func with multiple commands,
  2218. * you can tell which command was used by the cmd
  2219. * parameter.
  2220. */
  2221. /* we must have a module name */
  2222. if (!param)
  2223. return ret;
  2224. mod = strsep(&param, ":");
  2225. if (!strlen(mod))
  2226. return ret;
  2227. ret = ftrace_match_module_records(hash, func, mod);
  2228. if (!ret)
  2229. ret = -EINVAL;
  2230. if (ret < 0)
  2231. return ret;
  2232. return 0;
  2233. }
  2234. static struct ftrace_func_command ftrace_mod_cmd = {
  2235. .name = "mod",
  2236. .func = ftrace_mod_callback,
  2237. };
  2238. static int __init ftrace_mod_cmd_init(void)
  2239. {
  2240. return register_ftrace_command(&ftrace_mod_cmd);
  2241. }
  2242. device_initcall(ftrace_mod_cmd_init);
  2243. static void
  2244. function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
  2245. {
  2246. struct ftrace_func_probe *entry;
  2247. struct hlist_head *hhd;
  2248. struct hlist_node *n;
  2249. unsigned long key;
  2250. key = hash_long(ip, FTRACE_HASH_BITS);
  2251. hhd = &ftrace_func_hash[key];
  2252. if (hlist_empty(hhd))
  2253. return;
  2254. /*
  2255. * Disable preemption for these calls to prevent a RCU grace
  2256. * period. This syncs the hash iteration and freeing of items
  2257. * on the hash. rcu_read_lock is too dangerous here.
  2258. */
  2259. preempt_disable_notrace();
  2260. hlist_for_each_entry_rcu(entry, n, hhd, node) {
  2261. if (entry->ip == ip)
  2262. entry->ops->func(ip, parent_ip, &entry->data);
  2263. }
  2264. preempt_enable_notrace();
  2265. }
  2266. static struct ftrace_ops trace_probe_ops __read_mostly =
  2267. {
  2268. .func = function_trace_probe_call,
  2269. };
  2270. static int ftrace_probe_registered;
  2271. static void __enable_ftrace_function_probe(void)
  2272. {
  2273. int ret;
  2274. int i;
  2275. if (ftrace_probe_registered)
  2276. return;
  2277. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  2278. struct hlist_head *hhd = &ftrace_func_hash[i];
  2279. if (hhd->first)
  2280. break;
  2281. }
  2282. /* Nothing registered? */
  2283. if (i == FTRACE_FUNC_HASHSIZE)
  2284. return;
  2285. ret = __register_ftrace_function(&trace_probe_ops);
  2286. if (!ret)
  2287. ret = ftrace_startup(&trace_probe_ops, 0);
  2288. ftrace_probe_registered = 1;
  2289. }
  2290. static void __disable_ftrace_function_probe(void)
  2291. {
  2292. int ret;
  2293. int i;
  2294. if (!ftrace_probe_registered)
  2295. return;
  2296. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  2297. struct hlist_head *hhd = &ftrace_func_hash[i];
  2298. if (hhd->first)
  2299. return;
  2300. }
  2301. /* no more funcs left */
  2302. ret = __unregister_ftrace_function(&trace_probe_ops);
  2303. if (!ret)
  2304. ftrace_shutdown(&trace_probe_ops, 0);
  2305. ftrace_probe_registered = 0;
  2306. }
  2307. static void ftrace_free_entry_rcu(struct rcu_head *rhp)
  2308. {
  2309. struct ftrace_func_probe *entry =
  2310. container_of(rhp, struct ftrace_func_probe, rcu);
  2311. if (entry->ops->free)
  2312. entry->ops->free(&entry->data);
  2313. kfree(entry);
  2314. }
  2315. int
  2316. register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  2317. void *data)
  2318. {
  2319. struct ftrace_func_probe *entry;
  2320. struct ftrace_page *pg;
  2321. struct dyn_ftrace *rec;
  2322. int type, len, not;
  2323. unsigned long key;
  2324. int count = 0;
  2325. char *search;
  2326. type = filter_parse_regex(glob, strlen(glob), &search, &not);
  2327. len = strlen(search);
  2328. /* we do not support '!' for function probes */
  2329. if (WARN_ON(not))
  2330. return -EINVAL;
  2331. mutex_lock(&ftrace_lock);
  2332. if (unlikely(ftrace_disabled))
  2333. goto out_unlock;
  2334. do_for_each_ftrace_rec(pg, rec) {
  2335. if (!ftrace_match_record(rec, NULL, search, len, type))
  2336. continue;
  2337. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  2338. if (!entry) {
  2339. /* If we did not process any, then return error */
  2340. if (!count)
  2341. count = -ENOMEM;
  2342. goto out_unlock;
  2343. }
  2344. count++;
  2345. entry->data = data;
  2346. /*
  2347. * The caller might want to do something special
  2348. * for each function we find. We call the callback
  2349. * to give the caller an opportunity to do so.
  2350. */
  2351. if (ops->callback) {
  2352. if (ops->callback(rec->ip, &entry->data) < 0) {
  2353. /* caller does not like this func */
  2354. kfree(entry);
  2355. continue;
  2356. }
  2357. }
  2358. entry->ops = ops;
  2359. entry->ip = rec->ip;
  2360. key = hash_long(entry->ip, FTRACE_HASH_BITS);
  2361. hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
  2362. } while_for_each_ftrace_rec();
  2363. __enable_ftrace_function_probe();
  2364. out_unlock:
  2365. mutex_unlock(&ftrace_lock);
  2366. return count;
  2367. }
  2368. enum {
  2369. PROBE_TEST_FUNC = 1,
  2370. PROBE_TEST_DATA = 2
  2371. };
  2372. static void
  2373. __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  2374. void *data, int flags)
  2375. {
  2376. struct ftrace_func_probe *entry;
  2377. struct hlist_node *n, *tmp;
  2378. char str[KSYM_SYMBOL_LEN];
  2379. int type = MATCH_FULL;
  2380. int i, len = 0;
  2381. char *search;
  2382. if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
  2383. glob = NULL;
  2384. else if (glob) {
  2385. int not;
  2386. type = filter_parse_regex(glob, strlen(glob), &search, &not);
  2387. len = strlen(search);
  2388. /* we do not support '!' for function probes */
  2389. if (WARN_ON(not))
  2390. return;
  2391. }
  2392. mutex_lock(&ftrace_lock);
  2393. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  2394. struct hlist_head *hhd = &ftrace_func_hash[i];
  2395. hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
  2396. /* break up if statements for readability */
  2397. if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
  2398. continue;
  2399. if ((flags & PROBE_TEST_DATA) && entry->data != data)
  2400. continue;
  2401. /* do this last, since it is the most expensive */
  2402. if (glob) {
  2403. kallsyms_lookup(entry->ip, NULL, NULL,
  2404. NULL, str);
  2405. if (!ftrace_match(str, glob, len, type))
  2406. continue;
  2407. }
  2408. hlist_del(&entry->node);
  2409. call_rcu(&entry->rcu, ftrace_free_entry_rcu);
  2410. }
  2411. }
  2412. __disable_ftrace_function_probe();
  2413. mutex_unlock(&ftrace_lock);
  2414. }
  2415. void
  2416. unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  2417. void *data)
  2418. {
  2419. __unregister_ftrace_function_probe(glob, ops, data,
  2420. PROBE_TEST_FUNC | PROBE_TEST_DATA);
  2421. }
  2422. void
  2423. unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
  2424. {
  2425. __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
  2426. }
  2427. void unregister_ftrace_function_probe_all(char *glob)
  2428. {
  2429. __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
  2430. }
  2431. static LIST_HEAD(ftrace_commands);
  2432. static DEFINE_MUTEX(ftrace_cmd_mutex);
  2433. int register_ftrace_command(struct ftrace_func_command *cmd)
  2434. {
  2435. struct ftrace_func_command *p;
  2436. int ret = 0;
  2437. mutex_lock(&ftrace_cmd_mutex);
  2438. list_for_each_entry(p, &ftrace_commands, list) {
  2439. if (strcmp(cmd->name, p->name) == 0) {
  2440. ret = -EBUSY;
  2441. goto out_unlock;
  2442. }
  2443. }
  2444. list_add(&cmd->list, &ftrace_commands);
  2445. out_unlock:
  2446. mutex_unlock(&ftrace_cmd_mutex);
  2447. return ret;
  2448. }
  2449. int unregister_ftrace_command(struct ftrace_func_command *cmd)
  2450. {
  2451. struct ftrace_func_command *p, *n;
  2452. int ret = -ENODEV;
  2453. mutex_lock(&ftrace_cmd_mutex);
  2454. list_for_each_entry_safe(p, n, &ftrace_commands, list) {
  2455. if (strcmp(cmd->name, p->name) == 0) {
  2456. ret = 0;
  2457. list_del_init(&p->list);
  2458. goto out_unlock;
  2459. }
  2460. }
  2461. out_unlock:
  2462. mutex_unlock(&ftrace_cmd_mutex);
  2463. return ret;
  2464. }
  2465. static int ftrace_process_regex(struct ftrace_hash *hash,
  2466. char *buff, int len, int enable)
  2467. {
  2468. char *func, *command, *next = buff;
  2469. struct ftrace_func_command *p;
  2470. int ret = -EINVAL;
  2471. func = strsep(&next, ":");
  2472. if (!next) {
  2473. ret = ftrace_match_records(hash, func, len);
  2474. if (!ret)
  2475. ret = -EINVAL;
  2476. if (ret < 0)
  2477. return ret;
  2478. return 0;
  2479. }
  2480. /* command found */
  2481. command = strsep(&next, ":");
  2482. mutex_lock(&ftrace_cmd_mutex);
  2483. list_for_each_entry(p, &ftrace_commands, list) {
  2484. if (strcmp(p->name, command) == 0) {
  2485. ret = p->func(hash, func, command, next, enable);
  2486. goto out_unlock;
  2487. }
  2488. }
  2489. out_unlock:
  2490. mutex_unlock(&ftrace_cmd_mutex);
  2491. return ret;
  2492. }
  2493. static ssize_t
  2494. ftrace_regex_write(struct file *file, const char __user *ubuf,
  2495. size_t cnt, loff_t *ppos, int enable)
  2496. {
  2497. struct ftrace_iterator *iter;
  2498. struct trace_parser *parser;
  2499. ssize_t ret, read;
  2500. if (!cnt)
  2501. return 0;
  2502. mutex_lock(&ftrace_regex_lock);
  2503. ret = -ENODEV;
  2504. if (unlikely(ftrace_disabled))
  2505. goto out_unlock;
  2506. if (file->f_mode & FMODE_READ) {
  2507. struct seq_file *m = file->private_data;
  2508. iter = m->private;
  2509. } else
  2510. iter = file->private_data;
  2511. parser = &iter->parser;
  2512. read = trace_get_user(parser, ubuf, cnt, ppos);
  2513. if (read >= 0 && trace_parser_loaded(parser) &&
  2514. !trace_parser_cont(parser)) {
  2515. ret = ftrace_process_regex(iter->hash, parser->buffer,
  2516. parser->idx, enable);
  2517. trace_parser_clear(parser);
  2518. if (ret)
  2519. goto out_unlock;
  2520. }
  2521. ret = read;
  2522. out_unlock:
  2523. mutex_unlock(&ftrace_regex_lock);
  2524. return ret;
  2525. }
  2526. static ssize_t
  2527. ftrace_filter_write(struct file *file, const char __user *ubuf,
  2528. size_t cnt, loff_t *ppos)
  2529. {
  2530. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  2531. }
  2532. static ssize_t
  2533. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  2534. size_t cnt, loff_t *ppos)
  2535. {
  2536. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  2537. }
  2538. static int
  2539. ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
  2540. int reset, int enable)
  2541. {
  2542. struct ftrace_hash **orig_hash;
  2543. struct ftrace_hash *hash;
  2544. int ret;
  2545. /* All global ops uses the global ops filters */
  2546. if (ops->flags & FTRACE_OPS_FL_GLOBAL)
  2547. ops = &global_ops;
  2548. if (unlikely(ftrace_disabled))
  2549. return -ENODEV;
  2550. if (enable)
  2551. orig_hash = &ops->filter_hash;
  2552. else
  2553. orig_hash = &ops->notrace_hash;
  2554. hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
  2555. if (!hash)
  2556. return -ENOMEM;
  2557. mutex_lock(&ftrace_regex_lock);
  2558. if (reset)
  2559. ftrace_filter_reset(hash);
  2560. if (buf)
  2561. ftrace_match_records(hash, buf, len);
  2562. mutex_lock(&ftrace_lock);
  2563. ret = ftrace_hash_move(ops, enable, orig_hash, hash);
  2564. if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
  2565. && ftrace_enabled)
  2566. ftrace_run_update_code(FTRACE_UPDATE_CALLS);
  2567. mutex_unlock(&ftrace_lock);
  2568. mutex_unlock(&ftrace_regex_lock);
  2569. free_ftrace_hash(hash);
  2570. return ret;
  2571. }
  2572. /**
  2573. * ftrace_set_filter - set a function to filter on in ftrace
  2574. * @ops - the ops to set the filter with
  2575. * @buf - the string that holds the function filter text.
  2576. * @len - the length of the string.
  2577. * @reset - non zero to reset all filters before applying this filter.
  2578. *
  2579. * Filters denote which functions should be enabled when tracing is enabled.
  2580. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  2581. */
  2582. void ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
  2583. int len, int reset)
  2584. {
  2585. ftrace_set_regex(ops, buf, len, reset, 1);
  2586. }
  2587. EXPORT_SYMBOL_GPL(ftrace_set_filter);
  2588. /**
  2589. * ftrace_set_notrace - set a function to not trace in ftrace
  2590. * @ops - the ops to set the notrace filter with
  2591. * @buf - the string that holds the function notrace text.
  2592. * @len - the length of the string.
  2593. * @reset - non zero to reset all filters before applying this filter.
  2594. *
  2595. * Notrace Filters denote which functions should not be enabled when tracing
  2596. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  2597. * for tracing.
  2598. */
  2599. void ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
  2600. int len, int reset)
  2601. {
  2602. ftrace_set_regex(ops, buf, len, reset, 0);
  2603. }
  2604. EXPORT_SYMBOL_GPL(ftrace_set_notrace);
  2605. /**
  2606. * ftrace_set_filter - set a function to filter on in ftrace
  2607. * @ops - the ops to set the filter with
  2608. * @buf - the string that holds the function filter text.
  2609. * @len - the length of the string.
  2610. * @reset - non zero to reset all filters before applying this filter.
  2611. *
  2612. * Filters denote which functions should be enabled when tracing is enabled.
  2613. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  2614. */
  2615. void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
  2616. {
  2617. ftrace_set_regex(&global_ops, buf, len, reset, 1);
  2618. }
  2619. EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
  2620. /**
  2621. * ftrace_set_notrace - set a function to not trace in ftrace
  2622. * @ops - the ops to set the notrace filter with
  2623. * @buf - the string that holds the function notrace text.
  2624. * @len - the length of the string.
  2625. * @reset - non zero to reset all filters before applying this filter.
  2626. *
  2627. * Notrace Filters denote which functions should not be enabled when tracing
  2628. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  2629. * for tracing.
  2630. */
  2631. void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
  2632. {
  2633. ftrace_set_regex(&global_ops, buf, len, reset, 0);
  2634. }
  2635. EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
  2636. /*
  2637. * command line interface to allow users to set filters on boot up.
  2638. */
  2639. #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
  2640. static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
  2641. static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
  2642. static int __init set_ftrace_notrace(char *str)
  2643. {
  2644. strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
  2645. return 1;
  2646. }
  2647. __setup("ftrace_notrace=", set_ftrace_notrace);
  2648. static int __init set_ftrace_filter(char *str)
  2649. {
  2650. strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
  2651. return 1;
  2652. }
  2653. __setup("ftrace_filter=", set_ftrace_filter);
  2654. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2655. static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
  2656. static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
  2657. static int __init set_graph_function(char *str)
  2658. {
  2659. strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
  2660. return 1;
  2661. }
  2662. __setup("ftrace_graph_filter=", set_graph_function);
  2663. static void __init set_ftrace_early_graph(char *buf)
  2664. {
  2665. int ret;
  2666. char *func;
  2667. while (buf) {
  2668. func = strsep(&buf, ",");
  2669. /* we allow only one expression at a time */
  2670. ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
  2671. func);
  2672. if (ret)
  2673. printk(KERN_DEBUG "ftrace: function %s not "
  2674. "traceable\n", func);
  2675. }
  2676. }
  2677. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2678. static void __init
  2679. set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable)
  2680. {
  2681. char *func;
  2682. while (buf) {
  2683. func = strsep(&buf, ",");
  2684. ftrace_set_regex(ops, func, strlen(func), 0, enable);
  2685. }
  2686. }
  2687. static void __init set_ftrace_early_filters(void)
  2688. {
  2689. if (ftrace_filter_buf[0])
  2690. set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1);
  2691. if (ftrace_notrace_buf[0])
  2692. set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0);
  2693. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2694. if (ftrace_graph_buf[0])
  2695. set_ftrace_early_graph(ftrace_graph_buf);
  2696. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2697. }
  2698. static int
  2699. ftrace_regex_release(struct inode *inode, struct file *file)
  2700. {
  2701. struct seq_file *m = (struct seq_file *)file->private_data;
  2702. struct ftrace_iterator *iter;
  2703. struct ftrace_hash **orig_hash;
  2704. struct trace_parser *parser;
  2705. int filter_hash;
  2706. int ret;
  2707. mutex_lock(&ftrace_regex_lock);
  2708. if (file->f_mode & FMODE_READ) {
  2709. iter = m->private;
  2710. seq_release(inode, file);
  2711. } else
  2712. iter = file->private_data;
  2713. parser = &iter->parser;
  2714. if (trace_parser_loaded(parser)) {
  2715. parser->buffer[parser->idx] = 0;
  2716. ftrace_match_records(iter->hash, parser->buffer, parser->idx);
  2717. }
  2718. trace_parser_put(parser);
  2719. if (file->f_mode & FMODE_WRITE) {
  2720. filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
  2721. if (filter_hash)
  2722. orig_hash = &iter->ops->filter_hash;
  2723. else
  2724. orig_hash = &iter->ops->notrace_hash;
  2725. mutex_lock(&ftrace_lock);
  2726. ret = ftrace_hash_move(iter->ops, filter_hash,
  2727. orig_hash, iter->hash);
  2728. if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
  2729. && ftrace_enabled)
  2730. ftrace_run_update_code(FTRACE_UPDATE_CALLS);
  2731. mutex_unlock(&ftrace_lock);
  2732. }
  2733. free_ftrace_hash(iter->hash);
  2734. kfree(iter);
  2735. mutex_unlock(&ftrace_regex_lock);
  2736. return 0;
  2737. }
  2738. static const struct file_operations ftrace_avail_fops = {
  2739. .open = ftrace_avail_open,
  2740. .read = seq_read,
  2741. .llseek = seq_lseek,
  2742. .release = seq_release_private,
  2743. };
  2744. static const struct file_operations ftrace_enabled_fops = {
  2745. .open = ftrace_enabled_open,
  2746. .read = seq_read,
  2747. .llseek = seq_lseek,
  2748. .release = seq_release_private,
  2749. };
  2750. static const struct file_operations ftrace_filter_fops = {
  2751. .open = ftrace_filter_open,
  2752. .read = seq_read,
  2753. .write = ftrace_filter_write,
  2754. .llseek = ftrace_regex_lseek,
  2755. .release = ftrace_regex_release,
  2756. };
  2757. static const struct file_operations ftrace_notrace_fops = {
  2758. .open = ftrace_notrace_open,
  2759. .read = seq_read,
  2760. .write = ftrace_notrace_write,
  2761. .llseek = ftrace_regex_lseek,
  2762. .release = ftrace_regex_release,
  2763. };
  2764. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2765. static DEFINE_MUTEX(graph_lock);
  2766. int ftrace_graph_count;
  2767. int ftrace_graph_filter_enabled;
  2768. unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
  2769. static void *
  2770. __g_next(struct seq_file *m, loff_t *pos)
  2771. {
  2772. if (*pos >= ftrace_graph_count)
  2773. return NULL;
  2774. return &ftrace_graph_funcs[*pos];
  2775. }
  2776. static void *
  2777. g_next(struct seq_file *m, void *v, loff_t *pos)
  2778. {
  2779. (*pos)++;
  2780. return __g_next(m, pos);
  2781. }
  2782. static void *g_start(struct seq_file *m, loff_t *pos)
  2783. {
  2784. mutex_lock(&graph_lock);
  2785. /* Nothing, tell g_show to print all functions are enabled */
  2786. if (!ftrace_graph_filter_enabled && !*pos)
  2787. return (void *)1;
  2788. return __g_next(m, pos);
  2789. }
  2790. static void g_stop(struct seq_file *m, void *p)
  2791. {
  2792. mutex_unlock(&graph_lock);
  2793. }
  2794. static int g_show(struct seq_file *m, void *v)
  2795. {
  2796. unsigned long *ptr = v;
  2797. if (!ptr)
  2798. return 0;
  2799. if (ptr == (unsigned long *)1) {
  2800. seq_printf(m, "#### all functions enabled ####\n");
  2801. return 0;
  2802. }
  2803. seq_printf(m, "%ps\n", (void *)*ptr);
  2804. return 0;
  2805. }
  2806. static const struct seq_operations ftrace_graph_seq_ops = {
  2807. .start = g_start,
  2808. .next = g_next,
  2809. .stop = g_stop,
  2810. .show = g_show,
  2811. };
  2812. static int
  2813. ftrace_graph_open(struct inode *inode, struct file *file)
  2814. {
  2815. int ret = 0;
  2816. if (unlikely(ftrace_disabled))
  2817. return -ENODEV;
  2818. mutex_lock(&graph_lock);
  2819. if ((file->f_mode & FMODE_WRITE) &&
  2820. (file->f_flags & O_TRUNC)) {
  2821. ftrace_graph_filter_enabled = 0;
  2822. ftrace_graph_count = 0;
  2823. memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
  2824. }
  2825. mutex_unlock(&graph_lock);
  2826. if (file->f_mode & FMODE_READ)
  2827. ret = seq_open(file, &ftrace_graph_seq_ops);
  2828. return ret;
  2829. }
  2830. static int
  2831. ftrace_graph_release(struct inode *inode, struct file *file)
  2832. {
  2833. if (file->f_mode & FMODE_READ)
  2834. seq_release(inode, file);
  2835. return 0;
  2836. }
  2837. static int
  2838. ftrace_set_func(unsigned long *array, int *idx, char *buffer)
  2839. {
  2840. struct dyn_ftrace *rec;
  2841. struct ftrace_page *pg;
  2842. int search_len;
  2843. int fail = 1;
  2844. int type, not;
  2845. char *search;
  2846. bool exists;
  2847. int i;
  2848. /* decode regex */
  2849. type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
  2850. if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
  2851. return -EBUSY;
  2852. search_len = strlen(search);
  2853. mutex_lock(&ftrace_lock);
  2854. if (unlikely(ftrace_disabled)) {
  2855. mutex_unlock(&ftrace_lock);
  2856. return -ENODEV;
  2857. }
  2858. do_for_each_ftrace_rec(pg, rec) {
  2859. if (ftrace_match_record(rec, NULL, search, search_len, type)) {
  2860. /* if it is in the array */
  2861. exists = false;
  2862. for (i = 0; i < *idx; i++) {
  2863. if (array[i] == rec->ip) {
  2864. exists = true;
  2865. break;
  2866. }
  2867. }
  2868. if (!not) {
  2869. fail = 0;
  2870. if (!exists) {
  2871. array[(*idx)++] = rec->ip;
  2872. if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
  2873. goto out;
  2874. }
  2875. } else {
  2876. if (exists) {
  2877. array[i] = array[--(*idx)];
  2878. array[*idx] = 0;
  2879. fail = 0;
  2880. }
  2881. }
  2882. }
  2883. } while_for_each_ftrace_rec();
  2884. out:
  2885. mutex_unlock(&ftrace_lock);
  2886. if (fail)
  2887. return -EINVAL;
  2888. ftrace_graph_filter_enabled = 1;
  2889. return 0;
  2890. }
  2891. static ssize_t
  2892. ftrace_graph_write(struct file *file, const char __user *ubuf,
  2893. size_t cnt, loff_t *ppos)
  2894. {
  2895. struct trace_parser parser;
  2896. ssize_t read, ret;
  2897. if (!cnt)
  2898. return 0;
  2899. mutex_lock(&graph_lock);
  2900. if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
  2901. ret = -ENOMEM;
  2902. goto out_unlock;
  2903. }
  2904. read = trace_get_user(&parser, ubuf, cnt, ppos);
  2905. if (read >= 0 && trace_parser_loaded((&parser))) {
  2906. parser.buffer[parser.idx] = 0;
  2907. /* we allow only one expression at a time */
  2908. ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
  2909. parser.buffer);
  2910. if (ret)
  2911. goto out_free;
  2912. }
  2913. ret = read;
  2914. out_free:
  2915. trace_parser_put(&parser);
  2916. out_unlock:
  2917. mutex_unlock(&graph_lock);
  2918. return ret;
  2919. }
  2920. static const struct file_operations ftrace_graph_fops = {
  2921. .open = ftrace_graph_open,
  2922. .read = seq_read,
  2923. .write = ftrace_graph_write,
  2924. .release = ftrace_graph_release,
  2925. .llseek = seq_lseek,
  2926. };
  2927. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2928. static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
  2929. {
  2930. trace_create_file("available_filter_functions", 0444,
  2931. d_tracer, NULL, &ftrace_avail_fops);
  2932. trace_create_file("enabled_functions", 0444,
  2933. d_tracer, NULL, &ftrace_enabled_fops);
  2934. trace_create_file("set_ftrace_filter", 0644, d_tracer,
  2935. NULL, &ftrace_filter_fops);
  2936. trace_create_file("set_ftrace_notrace", 0644, d_tracer,
  2937. NULL, &ftrace_notrace_fops);
  2938. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2939. trace_create_file("set_graph_function", 0444, d_tracer,
  2940. NULL,
  2941. &ftrace_graph_fops);
  2942. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2943. return 0;
  2944. }
  2945. static void ftrace_swap_recs(void *a, void *b, int size)
  2946. {
  2947. struct dyn_ftrace *reca = a;
  2948. struct dyn_ftrace *recb = b;
  2949. struct dyn_ftrace t;
  2950. t = *reca;
  2951. *reca = *recb;
  2952. *recb = t;
  2953. }
  2954. static int ftrace_process_locs(struct module *mod,
  2955. unsigned long *start,
  2956. unsigned long *end)
  2957. {
  2958. struct ftrace_page *pg;
  2959. unsigned long count;
  2960. unsigned long *p;
  2961. unsigned long addr;
  2962. unsigned long flags = 0; /* Shut up gcc */
  2963. int ret = -ENOMEM;
  2964. count = end - start;
  2965. if (!count)
  2966. return 0;
  2967. pg = ftrace_allocate_pages(count);
  2968. if (!pg)
  2969. return -ENOMEM;
  2970. mutex_lock(&ftrace_lock);
  2971. /*
  2972. * Core and each module needs their own pages, as
  2973. * modules will free them when they are removed.
  2974. * Force a new page to be allocated for modules.
  2975. */
  2976. if (!mod) {
  2977. WARN_ON(ftrace_pages || ftrace_pages_start);
  2978. /* First initialization */
  2979. ftrace_pages = ftrace_pages_start = pg;
  2980. } else {
  2981. if (!ftrace_pages)
  2982. goto out;
  2983. if (WARN_ON(ftrace_pages->next)) {
  2984. /* Hmm, we have free pages? */
  2985. while (ftrace_pages->next)
  2986. ftrace_pages = ftrace_pages->next;
  2987. }
  2988. ftrace_pages->next = pg;
  2989. ftrace_pages = pg;
  2990. }
  2991. p = start;
  2992. while (p < end) {
  2993. addr = ftrace_call_adjust(*p++);
  2994. /*
  2995. * Some architecture linkers will pad between
  2996. * the different mcount_loc sections of different
  2997. * object files to satisfy alignments.
  2998. * Skip any NULL pointers.
  2999. */
  3000. if (!addr)
  3001. continue;
  3002. if (!ftrace_record_ip(addr))
  3003. break;
  3004. }
  3005. /* These new locations need to be initialized */
  3006. ftrace_new_pgs = pg;
  3007. /* Make each individual set of pages sorted by ips */
  3008. for (; pg; pg = pg->next)
  3009. sort(pg->records, pg->index, sizeof(struct dyn_ftrace),
  3010. ftrace_cmp_recs, ftrace_swap_recs);
  3011. /*
  3012. * We only need to disable interrupts on start up
  3013. * because we are modifying code that an interrupt
  3014. * may execute, and the modification is not atomic.
  3015. * But for modules, nothing runs the code we modify
  3016. * until we are finished with it, and there's no
  3017. * reason to cause large interrupt latencies while we do it.
  3018. */
  3019. if (!mod)
  3020. local_irq_save(flags);
  3021. ftrace_update_code(mod);
  3022. if (!mod)
  3023. local_irq_restore(flags);
  3024. ret = 0;
  3025. out:
  3026. mutex_unlock(&ftrace_lock);
  3027. return ret;
  3028. }
  3029. #ifdef CONFIG_MODULES
  3030. #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
  3031. void ftrace_release_mod(struct module *mod)
  3032. {
  3033. struct dyn_ftrace *rec;
  3034. struct ftrace_page **last_pg;
  3035. struct ftrace_page *pg;
  3036. int order;
  3037. mutex_lock(&ftrace_lock);
  3038. if (ftrace_disabled)
  3039. goto out_unlock;
  3040. /*
  3041. * Each module has its own ftrace_pages, remove
  3042. * them from the list.
  3043. */
  3044. last_pg = &ftrace_pages_start;
  3045. for (pg = ftrace_pages_start; pg; pg = *last_pg) {
  3046. rec = &pg->records[0];
  3047. if (within_module_core(rec->ip, mod)) {
  3048. /*
  3049. * As core pages are first, the first
  3050. * page should never be a module page.
  3051. */
  3052. if (WARN_ON(pg == ftrace_pages_start))
  3053. goto out_unlock;
  3054. /* Check if we are deleting the last page */
  3055. if (pg == ftrace_pages)
  3056. ftrace_pages = next_to_ftrace_page(last_pg);
  3057. *last_pg = pg->next;
  3058. order = get_count_order(pg->size / ENTRIES_PER_PAGE);
  3059. free_pages((unsigned long)pg->records, order);
  3060. kfree(pg);
  3061. } else
  3062. last_pg = &pg->next;
  3063. }
  3064. out_unlock:
  3065. mutex_unlock(&ftrace_lock);
  3066. }
  3067. static void ftrace_init_module(struct module *mod,
  3068. unsigned long *start, unsigned long *end)
  3069. {
  3070. if (ftrace_disabled || start == end)
  3071. return;
  3072. ftrace_process_locs(mod, start, end);
  3073. }
  3074. static int ftrace_module_notify(struct notifier_block *self,
  3075. unsigned long val, void *data)
  3076. {
  3077. struct module *mod = data;
  3078. switch (val) {
  3079. case MODULE_STATE_COMING:
  3080. ftrace_init_module(mod, mod->ftrace_callsites,
  3081. mod->ftrace_callsites +
  3082. mod->num_ftrace_callsites);
  3083. break;
  3084. case MODULE_STATE_GOING:
  3085. ftrace_release_mod(mod);
  3086. break;
  3087. }
  3088. return 0;
  3089. }
  3090. #else
  3091. static int ftrace_module_notify(struct notifier_block *self,
  3092. unsigned long val, void *data)
  3093. {
  3094. return 0;
  3095. }
  3096. #endif /* CONFIG_MODULES */
  3097. struct notifier_block ftrace_module_nb = {
  3098. .notifier_call = ftrace_module_notify,
  3099. .priority = 0,
  3100. };
  3101. extern unsigned long __start_mcount_loc[];
  3102. extern unsigned long __stop_mcount_loc[];
  3103. void __init ftrace_init(void)
  3104. {
  3105. unsigned long count, addr, flags;
  3106. int ret;
  3107. /* Keep the ftrace pointer to the stub */
  3108. addr = (unsigned long)ftrace_stub;
  3109. local_irq_save(flags);
  3110. ftrace_dyn_arch_init(&addr);
  3111. local_irq_restore(flags);
  3112. /* ftrace_dyn_arch_init places the return code in addr */
  3113. if (addr)
  3114. goto failed;
  3115. count = __stop_mcount_loc - __start_mcount_loc;
  3116. ret = ftrace_dyn_table_alloc(count);
  3117. if (ret)
  3118. goto failed;
  3119. last_ftrace_enabled = ftrace_enabled = 1;
  3120. ret = ftrace_process_locs(NULL,
  3121. __start_mcount_loc,
  3122. __stop_mcount_loc);
  3123. ret = register_module_notifier(&ftrace_module_nb);
  3124. if (ret)
  3125. pr_warning("Failed to register trace ftrace module notifier\n");
  3126. set_ftrace_early_filters();
  3127. return;
  3128. failed:
  3129. ftrace_disabled = 1;
  3130. }
  3131. #else
  3132. static struct ftrace_ops global_ops = {
  3133. .func = ftrace_stub,
  3134. };
  3135. static int __init ftrace_nodyn_init(void)
  3136. {
  3137. ftrace_enabled = 1;
  3138. return 0;
  3139. }
  3140. device_initcall(ftrace_nodyn_init);
  3141. static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
  3142. static inline void ftrace_startup_enable(int command) { }
  3143. /* Keep as macros so we do not need to define the commands */
  3144. # define ftrace_startup(ops, command) \
  3145. ({ \
  3146. (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
  3147. 0; \
  3148. })
  3149. # define ftrace_shutdown(ops, command) do { } while (0)
  3150. # define ftrace_startup_sysctl() do { } while (0)
  3151. # define ftrace_shutdown_sysctl() do { } while (0)
  3152. static inline int
  3153. ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
  3154. {
  3155. return 1;
  3156. }
  3157. #endif /* CONFIG_DYNAMIC_FTRACE */
  3158. static void
  3159. ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
  3160. {
  3161. struct ftrace_ops *op;
  3162. if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
  3163. return;
  3164. trace_recursion_set(TRACE_INTERNAL_BIT);
  3165. /*
  3166. * Some of the ops may be dynamically allocated,
  3167. * they must be freed after a synchronize_sched().
  3168. */
  3169. preempt_disable_notrace();
  3170. op = rcu_dereference_raw(ftrace_ops_list);
  3171. while (op != &ftrace_list_end) {
  3172. if (ftrace_ops_test(op, ip))
  3173. op->func(ip, parent_ip);
  3174. op = rcu_dereference_raw(op->next);
  3175. };
  3176. preempt_enable_notrace();
  3177. trace_recursion_clear(TRACE_INTERNAL_BIT);
  3178. }
  3179. static void clear_ftrace_swapper(void)
  3180. {
  3181. struct task_struct *p;
  3182. int cpu;
  3183. get_online_cpus();
  3184. for_each_online_cpu(cpu) {
  3185. p = idle_task(cpu);
  3186. clear_tsk_trace_trace(p);
  3187. }
  3188. put_online_cpus();
  3189. }
  3190. static void set_ftrace_swapper(void)
  3191. {
  3192. struct task_struct *p;
  3193. int cpu;
  3194. get_online_cpus();
  3195. for_each_online_cpu(cpu) {
  3196. p = idle_task(cpu);
  3197. set_tsk_trace_trace(p);
  3198. }
  3199. put_online_cpus();
  3200. }
  3201. static void clear_ftrace_pid(struct pid *pid)
  3202. {
  3203. struct task_struct *p;
  3204. rcu_read_lock();
  3205. do_each_pid_task(pid, PIDTYPE_PID, p) {
  3206. clear_tsk_trace_trace(p);
  3207. } while_each_pid_task(pid, PIDTYPE_PID, p);
  3208. rcu_read_unlock();
  3209. put_pid(pid);
  3210. }
  3211. static void set_ftrace_pid(struct pid *pid)
  3212. {
  3213. struct task_struct *p;
  3214. rcu_read_lock();
  3215. do_each_pid_task(pid, PIDTYPE_PID, p) {
  3216. set_tsk_trace_trace(p);
  3217. } while_each_pid_task(pid, PIDTYPE_PID, p);
  3218. rcu_read_unlock();
  3219. }
  3220. static void clear_ftrace_pid_task(struct pid *pid)
  3221. {
  3222. if (pid == ftrace_swapper_pid)
  3223. clear_ftrace_swapper();
  3224. else
  3225. clear_ftrace_pid(pid);
  3226. }
  3227. static void set_ftrace_pid_task(struct pid *pid)
  3228. {
  3229. if (pid == ftrace_swapper_pid)
  3230. set_ftrace_swapper();
  3231. else
  3232. set_ftrace_pid(pid);
  3233. }
  3234. static int ftrace_pid_add(int p)
  3235. {
  3236. struct pid *pid;
  3237. struct ftrace_pid *fpid;
  3238. int ret = -EINVAL;
  3239. mutex_lock(&ftrace_lock);
  3240. if (!p)
  3241. pid = ftrace_swapper_pid;
  3242. else
  3243. pid = find_get_pid(p);
  3244. if (!pid)
  3245. goto out;
  3246. ret = 0;
  3247. list_for_each_entry(fpid, &ftrace_pids, list)
  3248. if (fpid->pid == pid)
  3249. goto out_put;
  3250. ret = -ENOMEM;
  3251. fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
  3252. if (!fpid)
  3253. goto out_put;
  3254. list_add(&fpid->list, &ftrace_pids);
  3255. fpid->pid = pid;
  3256. set_ftrace_pid_task(pid);
  3257. ftrace_update_pid_func();
  3258. ftrace_startup_enable(0);
  3259. mutex_unlock(&ftrace_lock);
  3260. return 0;
  3261. out_put:
  3262. if (pid != ftrace_swapper_pid)
  3263. put_pid(pid);
  3264. out:
  3265. mutex_unlock(&ftrace_lock);
  3266. return ret;
  3267. }
  3268. static void ftrace_pid_reset(void)
  3269. {
  3270. struct ftrace_pid *fpid, *safe;
  3271. mutex_lock(&ftrace_lock);
  3272. list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
  3273. struct pid *pid = fpid->pid;
  3274. clear_ftrace_pid_task(pid);
  3275. list_del(&fpid->list);
  3276. kfree(fpid);
  3277. }
  3278. ftrace_update_pid_func();
  3279. ftrace_startup_enable(0);
  3280. mutex_unlock(&ftrace_lock);
  3281. }
  3282. static void *fpid_start(struct seq_file *m, loff_t *pos)
  3283. {
  3284. mutex_lock(&ftrace_lock);
  3285. if (list_empty(&ftrace_pids) && (!*pos))
  3286. return (void *) 1;
  3287. return seq_list_start(&ftrace_pids, *pos);
  3288. }
  3289. static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
  3290. {
  3291. if (v == (void *)1)
  3292. return NULL;
  3293. return seq_list_next(v, &ftrace_pids, pos);
  3294. }
  3295. static void fpid_stop(struct seq_file *m, void *p)
  3296. {
  3297. mutex_unlock(&ftrace_lock);
  3298. }
  3299. static int fpid_show(struct seq_file *m, void *v)
  3300. {
  3301. const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
  3302. if (v == (void *)1) {
  3303. seq_printf(m, "no pid\n");
  3304. return 0;
  3305. }
  3306. if (fpid->pid == ftrace_swapper_pid)
  3307. seq_printf(m, "swapper tasks\n");
  3308. else
  3309. seq_printf(m, "%u\n", pid_vnr(fpid->pid));
  3310. return 0;
  3311. }
  3312. static const struct seq_operations ftrace_pid_sops = {
  3313. .start = fpid_start,
  3314. .next = fpid_next,
  3315. .stop = fpid_stop,
  3316. .show = fpid_show,
  3317. };
  3318. static int
  3319. ftrace_pid_open(struct inode *inode, struct file *file)
  3320. {
  3321. int ret = 0;
  3322. if ((file->f_mode & FMODE_WRITE) &&
  3323. (file->f_flags & O_TRUNC))
  3324. ftrace_pid_reset();
  3325. if (file->f_mode & FMODE_READ)
  3326. ret = seq_open(file, &ftrace_pid_sops);
  3327. return ret;
  3328. }
  3329. static ssize_t
  3330. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  3331. size_t cnt, loff_t *ppos)
  3332. {
  3333. char buf[64], *tmp;
  3334. long val;
  3335. int ret;
  3336. if (cnt >= sizeof(buf))
  3337. return -EINVAL;
  3338. if (copy_from_user(&buf, ubuf, cnt))
  3339. return -EFAULT;
  3340. buf[cnt] = 0;
  3341. /*
  3342. * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
  3343. * to clean the filter quietly.
  3344. */
  3345. tmp = strstrip(buf);
  3346. if (strlen(tmp) == 0)
  3347. return 1;
  3348. ret = strict_strtol(tmp, 10, &val);
  3349. if (ret < 0)
  3350. return ret;
  3351. ret = ftrace_pid_add(val);
  3352. return ret ? ret : cnt;
  3353. }
  3354. static int
  3355. ftrace_pid_release(struct inode *inode, struct file *file)
  3356. {
  3357. if (file->f_mode & FMODE_READ)
  3358. seq_release(inode, file);
  3359. return 0;
  3360. }
  3361. static const struct file_operations ftrace_pid_fops = {
  3362. .open = ftrace_pid_open,
  3363. .write = ftrace_pid_write,
  3364. .read = seq_read,
  3365. .llseek = seq_lseek,
  3366. .release = ftrace_pid_release,
  3367. };
  3368. static __init int ftrace_init_debugfs(void)
  3369. {
  3370. struct dentry *d_tracer;
  3371. d_tracer = tracing_init_dentry();
  3372. if (!d_tracer)
  3373. return 0;
  3374. ftrace_init_dyn_debugfs(d_tracer);
  3375. trace_create_file("set_ftrace_pid", 0644, d_tracer,
  3376. NULL, &ftrace_pid_fops);
  3377. ftrace_profile_debugfs(d_tracer);
  3378. return 0;
  3379. }
  3380. fs_initcall(ftrace_init_debugfs);
  3381. /**
  3382. * ftrace_kill - kill ftrace
  3383. *
  3384. * This function should be used by panic code. It stops ftrace
  3385. * but in a not so nice way. If you need to simply kill ftrace
  3386. * from a non-atomic section, use ftrace_kill.
  3387. */
  3388. void ftrace_kill(void)
  3389. {
  3390. ftrace_disabled = 1;
  3391. ftrace_enabled = 0;
  3392. clear_ftrace_function();
  3393. }
  3394. /**
  3395. * Test if ftrace is dead or not.
  3396. */
  3397. int ftrace_is_dead(void)
  3398. {
  3399. return ftrace_disabled;
  3400. }
  3401. /**
  3402. * register_ftrace_function - register a function for profiling
  3403. * @ops - ops structure that holds the function for profiling.
  3404. *
  3405. * Register a function to be called by all functions in the
  3406. * kernel.
  3407. *
  3408. * Note: @ops->func and all the functions it calls must be labeled
  3409. * with "notrace", otherwise it will go into a
  3410. * recursive loop.
  3411. */
  3412. int register_ftrace_function(struct ftrace_ops *ops)
  3413. {
  3414. int ret = -1;
  3415. mutex_lock(&ftrace_lock);
  3416. if (unlikely(ftrace_disabled))
  3417. goto out_unlock;
  3418. ret = __register_ftrace_function(ops);
  3419. if (!ret)
  3420. ret = ftrace_startup(ops, 0);
  3421. out_unlock:
  3422. mutex_unlock(&ftrace_lock);
  3423. return ret;
  3424. }
  3425. EXPORT_SYMBOL_GPL(register_ftrace_function);
  3426. /**
  3427. * unregister_ftrace_function - unregister a function for profiling.
  3428. * @ops - ops structure that holds the function to unregister
  3429. *
  3430. * Unregister a function that was added to be called by ftrace profiling.
  3431. */
  3432. int unregister_ftrace_function(struct ftrace_ops *ops)
  3433. {
  3434. int ret;
  3435. mutex_lock(&ftrace_lock);
  3436. ret = __unregister_ftrace_function(ops);
  3437. if (!ret)
  3438. ftrace_shutdown(ops, 0);
  3439. mutex_unlock(&ftrace_lock);
  3440. return ret;
  3441. }
  3442. EXPORT_SYMBOL_GPL(unregister_ftrace_function);
  3443. int
  3444. ftrace_enable_sysctl(struct ctl_table *table, int write,
  3445. void __user *buffer, size_t *lenp,
  3446. loff_t *ppos)
  3447. {
  3448. int ret = -ENODEV;
  3449. mutex_lock(&ftrace_lock);
  3450. if (unlikely(ftrace_disabled))
  3451. goto out;
  3452. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  3453. if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
  3454. goto out;
  3455. last_ftrace_enabled = !!ftrace_enabled;
  3456. if (ftrace_enabled) {
  3457. ftrace_startup_sysctl();
  3458. /* we are starting ftrace again */
  3459. if (ftrace_ops_list != &ftrace_list_end) {
  3460. if (ftrace_ops_list->next == &ftrace_list_end)
  3461. ftrace_trace_function = ftrace_ops_list->func;
  3462. else
  3463. ftrace_trace_function = ftrace_ops_list_func;
  3464. }
  3465. } else {
  3466. /* stopping ftrace calls (just send to ftrace_stub) */
  3467. ftrace_trace_function = ftrace_stub;
  3468. ftrace_shutdown_sysctl();
  3469. }
  3470. out:
  3471. mutex_unlock(&ftrace_lock);
  3472. return ret;
  3473. }
  3474. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  3475. static int ftrace_graph_active;
  3476. static struct notifier_block ftrace_suspend_notifier;
  3477. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
  3478. {
  3479. return 0;
  3480. }
  3481. /* The callbacks that hook a function */
  3482. trace_func_graph_ret_t ftrace_graph_return =
  3483. (trace_func_graph_ret_t)ftrace_stub;
  3484. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  3485. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  3486. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  3487. {
  3488. int i;
  3489. int ret = 0;
  3490. unsigned long flags;
  3491. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  3492. struct task_struct *g, *t;
  3493. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  3494. ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
  3495. * sizeof(struct ftrace_ret_stack),
  3496. GFP_KERNEL);
  3497. if (!ret_stack_list[i]) {
  3498. start = 0;
  3499. end = i;
  3500. ret = -ENOMEM;
  3501. goto free;
  3502. }
  3503. }
  3504. read_lock_irqsave(&tasklist_lock, flags);
  3505. do_each_thread(g, t) {
  3506. if (start == end) {
  3507. ret = -EAGAIN;
  3508. goto unlock;
  3509. }
  3510. if (t->ret_stack == NULL) {
  3511. atomic_set(&t->tracing_graph_pause, 0);
  3512. atomic_set(&t->trace_overrun, 0);
  3513. t->curr_ret_stack = -1;
  3514. /* Make sure the tasks see the -1 first: */
  3515. smp_wmb();
  3516. t->ret_stack = ret_stack_list[start++];
  3517. }
  3518. } while_each_thread(g, t);
  3519. unlock:
  3520. read_unlock_irqrestore(&tasklist_lock, flags);
  3521. free:
  3522. for (i = start; i < end; i++)
  3523. kfree(ret_stack_list[i]);
  3524. return ret;
  3525. }
  3526. static void
  3527. ftrace_graph_probe_sched_switch(void *ignore,
  3528. struct task_struct *prev, struct task_struct *next)
  3529. {
  3530. unsigned long long timestamp;
  3531. int index;
  3532. /*
  3533. * Does the user want to count the time a function was asleep.
  3534. * If so, do not update the time stamps.
  3535. */
  3536. if (trace_flags & TRACE_ITER_SLEEP_TIME)
  3537. return;
  3538. timestamp = trace_clock_local();
  3539. prev->ftrace_timestamp = timestamp;
  3540. /* only process tasks that we timestamped */
  3541. if (!next->ftrace_timestamp)
  3542. return;
  3543. /*
  3544. * Update all the counters in next to make up for the
  3545. * time next was sleeping.
  3546. */
  3547. timestamp -= next->ftrace_timestamp;
  3548. for (index = next->curr_ret_stack; index >= 0; index--)
  3549. next->ret_stack[index].calltime += timestamp;
  3550. }
  3551. /* Allocate a return stack for each task */
  3552. static int start_graph_tracing(void)
  3553. {
  3554. struct ftrace_ret_stack **ret_stack_list;
  3555. int ret, cpu;
  3556. ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
  3557. sizeof(struct ftrace_ret_stack *),
  3558. GFP_KERNEL);
  3559. if (!ret_stack_list)
  3560. return -ENOMEM;
  3561. /* The cpu_boot init_task->ret_stack will never be freed */
  3562. for_each_online_cpu(cpu) {
  3563. if (!idle_task(cpu)->ret_stack)
  3564. ftrace_graph_init_idle_task(idle_task(cpu), cpu);
  3565. }
  3566. do {
  3567. ret = alloc_retstack_tasklist(ret_stack_list);
  3568. } while (ret == -EAGAIN);
  3569. if (!ret) {
  3570. ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  3571. if (ret)
  3572. pr_info("ftrace_graph: Couldn't activate tracepoint"
  3573. " probe to kernel_sched_switch\n");
  3574. }
  3575. kfree(ret_stack_list);
  3576. return ret;
  3577. }
  3578. /*
  3579. * Hibernation protection.
  3580. * The state of the current task is too much unstable during
  3581. * suspend/restore to disk. We want to protect against that.
  3582. */
  3583. static int
  3584. ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
  3585. void *unused)
  3586. {
  3587. switch (state) {
  3588. case PM_HIBERNATION_PREPARE:
  3589. pause_graph_tracing();
  3590. break;
  3591. case PM_POST_HIBERNATION:
  3592. unpause_graph_tracing();
  3593. break;
  3594. }
  3595. return NOTIFY_DONE;
  3596. }
  3597. int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  3598. trace_func_graph_ent_t entryfunc)
  3599. {
  3600. int ret = 0;
  3601. mutex_lock(&ftrace_lock);
  3602. /* we currently allow only one tracer registered at a time */
  3603. if (ftrace_graph_active) {
  3604. ret = -EBUSY;
  3605. goto out;
  3606. }
  3607. ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
  3608. register_pm_notifier(&ftrace_suspend_notifier);
  3609. ftrace_graph_active++;
  3610. ret = start_graph_tracing();
  3611. if (ret) {
  3612. ftrace_graph_active--;
  3613. goto out;
  3614. }
  3615. ftrace_graph_return = retfunc;
  3616. ftrace_graph_entry = entryfunc;
  3617. ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
  3618. out:
  3619. mutex_unlock(&ftrace_lock);
  3620. return ret;
  3621. }
  3622. void unregister_ftrace_graph(void)
  3623. {
  3624. mutex_lock(&ftrace_lock);
  3625. if (unlikely(!ftrace_graph_active))
  3626. goto out;
  3627. ftrace_graph_active--;
  3628. ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
  3629. ftrace_graph_entry = ftrace_graph_entry_stub;
  3630. ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
  3631. unregister_pm_notifier(&ftrace_suspend_notifier);
  3632. unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  3633. out:
  3634. mutex_unlock(&ftrace_lock);
  3635. }
  3636. static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
  3637. static void
  3638. graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
  3639. {
  3640. atomic_set(&t->tracing_graph_pause, 0);
  3641. atomic_set(&t->trace_overrun, 0);
  3642. t->ftrace_timestamp = 0;
  3643. /* make curr_ret_stack visible before we add the ret_stack */
  3644. smp_wmb();
  3645. t->ret_stack = ret_stack;
  3646. }
  3647. /*
  3648. * Allocate a return stack for the idle task. May be the first
  3649. * time through, or it may be done by CPU hotplug online.
  3650. */
  3651. void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
  3652. {
  3653. t->curr_ret_stack = -1;
  3654. /*
  3655. * The idle task has no parent, it either has its own
  3656. * stack or no stack at all.
  3657. */
  3658. if (t->ret_stack)
  3659. WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
  3660. if (ftrace_graph_active) {
  3661. struct ftrace_ret_stack *ret_stack;
  3662. ret_stack = per_cpu(idle_ret_stack, cpu);
  3663. if (!ret_stack) {
  3664. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  3665. * sizeof(struct ftrace_ret_stack),
  3666. GFP_KERNEL);
  3667. if (!ret_stack)
  3668. return;
  3669. per_cpu(idle_ret_stack, cpu) = ret_stack;
  3670. }
  3671. graph_init_task(t, ret_stack);
  3672. }
  3673. }
  3674. /* Allocate a return stack for newly created task */
  3675. void ftrace_graph_init_task(struct task_struct *t)
  3676. {
  3677. /* Make sure we do not use the parent ret_stack */
  3678. t->ret_stack = NULL;
  3679. t->curr_ret_stack = -1;
  3680. if (ftrace_graph_active) {
  3681. struct ftrace_ret_stack *ret_stack;
  3682. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  3683. * sizeof(struct ftrace_ret_stack),
  3684. GFP_KERNEL);
  3685. if (!ret_stack)
  3686. return;
  3687. graph_init_task(t, ret_stack);
  3688. }
  3689. }
  3690. void ftrace_graph_exit_task(struct task_struct *t)
  3691. {
  3692. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  3693. t->ret_stack = NULL;
  3694. /* NULL must become visible to IRQs before we free it: */
  3695. barrier();
  3696. kfree(ret_stack);
  3697. }
  3698. void ftrace_graph_stop(void)
  3699. {
  3700. ftrace_stop();
  3701. }
  3702. #endif