ftrace.c 88 KB

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