ftrace.c 70 KB

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