ftrace.c 74 KB

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