ftrace.c 88 KB

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