ftrace.c 97 KB

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