ftrace.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297
  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 __init 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. WARN(1,
  643. "Could not allocate stat file for cpu %d\n",
  644. cpu);
  645. return;
  646. }
  647. stat->stat = function_stats;
  648. snprintf(name, 32, "function%d", cpu);
  649. stat->stat.name = name;
  650. ret = register_stat_tracer(&stat->stat);
  651. if (ret) {
  652. WARN(1,
  653. "Could not register function stat for cpu %d\n",
  654. cpu);
  655. kfree(name);
  656. return;
  657. }
  658. }
  659. entry = debugfs_create_file("function_profile_enabled", 0644,
  660. d_tracer, NULL, &ftrace_profile_fops);
  661. if (!entry)
  662. pr_warning("Could not create debugfs "
  663. "'function_profile_enabled' entry\n");
  664. }
  665. #else /* CONFIG_FUNCTION_PROFILER */
  666. static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
  667. {
  668. }
  669. #endif /* CONFIG_FUNCTION_PROFILER */
  670. /* set when tracing only a pid */
  671. struct pid *ftrace_pid_trace;
  672. static struct pid * const ftrace_swapper_pid = &init_struct_pid;
  673. #ifdef CONFIG_DYNAMIC_FTRACE
  674. #ifndef CONFIG_FTRACE_MCOUNT_RECORD
  675. # error Dynamic ftrace depends on MCOUNT_RECORD
  676. #endif
  677. static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
  678. struct ftrace_func_probe {
  679. struct hlist_node node;
  680. struct ftrace_probe_ops *ops;
  681. unsigned long flags;
  682. unsigned long ip;
  683. void *data;
  684. struct rcu_head rcu;
  685. };
  686. enum {
  687. FTRACE_ENABLE_CALLS = (1 << 0),
  688. FTRACE_DISABLE_CALLS = (1 << 1),
  689. FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
  690. FTRACE_ENABLE_MCOUNT = (1 << 3),
  691. FTRACE_DISABLE_MCOUNT = (1 << 4),
  692. FTRACE_START_FUNC_RET = (1 << 5),
  693. FTRACE_STOP_FUNC_RET = (1 << 6),
  694. };
  695. static int ftrace_filtered;
  696. static struct dyn_ftrace *ftrace_new_addrs;
  697. static DEFINE_MUTEX(ftrace_regex_lock);
  698. struct ftrace_page {
  699. struct ftrace_page *next;
  700. int index;
  701. struct dyn_ftrace records[];
  702. };
  703. #define ENTRIES_PER_PAGE \
  704. ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
  705. /* estimate from running different kernels */
  706. #define NR_TO_INIT 10000
  707. static struct ftrace_page *ftrace_pages_start;
  708. static struct ftrace_page *ftrace_pages;
  709. static struct dyn_ftrace *ftrace_free_records;
  710. /*
  711. * This is a double for. Do not use 'break' to break out of the loop,
  712. * you must use a goto.
  713. */
  714. #define do_for_each_ftrace_rec(pg, rec) \
  715. for (pg = ftrace_pages_start; pg; pg = pg->next) { \
  716. int _____i; \
  717. for (_____i = 0; _____i < pg->index; _____i++) { \
  718. rec = &pg->records[_____i];
  719. #define while_for_each_ftrace_rec() \
  720. } \
  721. }
  722. #ifdef CONFIG_KPROBES
  723. static int frozen_record_count;
  724. static inline void freeze_record(struct dyn_ftrace *rec)
  725. {
  726. if (!(rec->flags & FTRACE_FL_FROZEN)) {
  727. rec->flags |= FTRACE_FL_FROZEN;
  728. frozen_record_count++;
  729. }
  730. }
  731. static inline void unfreeze_record(struct dyn_ftrace *rec)
  732. {
  733. if (rec->flags & FTRACE_FL_FROZEN) {
  734. rec->flags &= ~FTRACE_FL_FROZEN;
  735. frozen_record_count--;
  736. }
  737. }
  738. static inline int record_frozen(struct dyn_ftrace *rec)
  739. {
  740. return rec->flags & FTRACE_FL_FROZEN;
  741. }
  742. #else
  743. # define freeze_record(rec) ({ 0; })
  744. # define unfreeze_record(rec) ({ 0; })
  745. # define record_frozen(rec) ({ 0; })
  746. #endif /* CONFIG_KPROBES */
  747. static void ftrace_free_rec(struct dyn_ftrace *rec)
  748. {
  749. rec->freelist = ftrace_free_records;
  750. ftrace_free_records = rec;
  751. rec->flags |= FTRACE_FL_FREE;
  752. }
  753. static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
  754. {
  755. struct dyn_ftrace *rec;
  756. /* First check for freed records */
  757. if (ftrace_free_records) {
  758. rec = ftrace_free_records;
  759. if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
  760. FTRACE_WARN_ON_ONCE(1);
  761. ftrace_free_records = NULL;
  762. return NULL;
  763. }
  764. ftrace_free_records = rec->freelist;
  765. memset(rec, 0, sizeof(*rec));
  766. return rec;
  767. }
  768. if (ftrace_pages->index == ENTRIES_PER_PAGE) {
  769. if (!ftrace_pages->next) {
  770. /* allocate another page */
  771. ftrace_pages->next =
  772. (void *)get_zeroed_page(GFP_KERNEL);
  773. if (!ftrace_pages->next)
  774. return NULL;
  775. }
  776. ftrace_pages = ftrace_pages->next;
  777. }
  778. return &ftrace_pages->records[ftrace_pages->index++];
  779. }
  780. static struct dyn_ftrace *
  781. ftrace_record_ip(unsigned long ip)
  782. {
  783. struct dyn_ftrace *rec;
  784. if (ftrace_disabled)
  785. return NULL;
  786. rec = ftrace_alloc_dyn_node(ip);
  787. if (!rec)
  788. return NULL;
  789. rec->ip = ip;
  790. rec->newlist = ftrace_new_addrs;
  791. ftrace_new_addrs = rec;
  792. return rec;
  793. }
  794. static void print_ip_ins(const char *fmt, unsigned char *p)
  795. {
  796. int i;
  797. printk(KERN_CONT "%s", fmt);
  798. for (i = 0; i < MCOUNT_INSN_SIZE; i++)
  799. printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
  800. }
  801. static void ftrace_bug(int failed, unsigned long ip)
  802. {
  803. switch (failed) {
  804. case -EFAULT:
  805. FTRACE_WARN_ON_ONCE(1);
  806. pr_info("ftrace faulted on modifying ");
  807. print_ip_sym(ip);
  808. break;
  809. case -EINVAL:
  810. FTRACE_WARN_ON_ONCE(1);
  811. pr_info("ftrace failed to modify ");
  812. print_ip_sym(ip);
  813. print_ip_ins(" actual: ", (unsigned char *)ip);
  814. printk(KERN_CONT "\n");
  815. break;
  816. case -EPERM:
  817. FTRACE_WARN_ON_ONCE(1);
  818. pr_info("ftrace faulted on writing ");
  819. print_ip_sym(ip);
  820. break;
  821. default:
  822. FTRACE_WARN_ON_ONCE(1);
  823. pr_info("ftrace faulted on unknown error ");
  824. print_ip_sym(ip);
  825. }
  826. }
  827. static int
  828. __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
  829. {
  830. unsigned long ftrace_addr;
  831. unsigned long flag = 0UL;
  832. ftrace_addr = (unsigned long)FTRACE_ADDR;
  833. /*
  834. * If this record is not to be traced or we want to disable it,
  835. * then disable it.
  836. *
  837. * If we want to enable it and filtering is off, then enable it.
  838. *
  839. * If we want to enable it and filtering is on, enable it only if
  840. * it's filtered
  841. */
  842. if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
  843. if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
  844. flag = FTRACE_FL_ENABLED;
  845. }
  846. /* If the state of this record hasn't changed, then do nothing */
  847. if ((rec->flags & FTRACE_FL_ENABLED) == flag)
  848. return 0;
  849. if (flag) {
  850. rec->flags |= FTRACE_FL_ENABLED;
  851. return ftrace_make_call(rec, ftrace_addr);
  852. }
  853. rec->flags &= ~FTRACE_FL_ENABLED;
  854. return ftrace_make_nop(NULL, rec, ftrace_addr);
  855. }
  856. static void ftrace_replace_code(int enable)
  857. {
  858. struct dyn_ftrace *rec;
  859. struct ftrace_page *pg;
  860. int failed;
  861. do_for_each_ftrace_rec(pg, rec) {
  862. /*
  863. * Skip over free records, records that have
  864. * failed and not converted.
  865. */
  866. if (rec->flags & FTRACE_FL_FREE ||
  867. rec->flags & FTRACE_FL_FAILED ||
  868. !(rec->flags & FTRACE_FL_CONVERTED))
  869. continue;
  870. /* ignore updates to this record's mcount site */
  871. if (get_kprobe((void *)rec->ip)) {
  872. freeze_record(rec);
  873. continue;
  874. } else {
  875. unfreeze_record(rec);
  876. }
  877. failed = __ftrace_replace_code(rec, enable);
  878. if (failed) {
  879. rec->flags |= FTRACE_FL_FAILED;
  880. if ((system_state == SYSTEM_BOOTING) ||
  881. !core_kernel_text(rec->ip)) {
  882. ftrace_free_rec(rec);
  883. } else {
  884. ftrace_bug(failed, rec->ip);
  885. /* Stop processing */
  886. return;
  887. }
  888. }
  889. } while_for_each_ftrace_rec();
  890. }
  891. static int
  892. ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
  893. {
  894. unsigned long ip;
  895. int ret;
  896. ip = rec->ip;
  897. ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
  898. if (ret) {
  899. ftrace_bug(ret, ip);
  900. rec->flags |= FTRACE_FL_FAILED;
  901. return 0;
  902. }
  903. return 1;
  904. }
  905. /*
  906. * archs can override this function if they must do something
  907. * before the modifying code is performed.
  908. */
  909. int __weak ftrace_arch_code_modify_prepare(void)
  910. {
  911. return 0;
  912. }
  913. /*
  914. * archs can override this function if they must do something
  915. * after the modifying code is performed.
  916. */
  917. int __weak ftrace_arch_code_modify_post_process(void)
  918. {
  919. return 0;
  920. }
  921. static int __ftrace_modify_code(void *data)
  922. {
  923. int *command = data;
  924. if (*command & FTRACE_ENABLE_CALLS)
  925. ftrace_replace_code(1);
  926. else if (*command & FTRACE_DISABLE_CALLS)
  927. ftrace_replace_code(0);
  928. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  929. ftrace_update_ftrace_func(ftrace_trace_function);
  930. if (*command & FTRACE_START_FUNC_RET)
  931. ftrace_enable_ftrace_graph_caller();
  932. else if (*command & FTRACE_STOP_FUNC_RET)
  933. ftrace_disable_ftrace_graph_caller();
  934. return 0;
  935. }
  936. static void ftrace_run_update_code(int command)
  937. {
  938. int ret;
  939. ret = ftrace_arch_code_modify_prepare();
  940. FTRACE_WARN_ON(ret);
  941. if (ret)
  942. return;
  943. stop_machine(__ftrace_modify_code, &command, NULL);
  944. ret = ftrace_arch_code_modify_post_process();
  945. FTRACE_WARN_ON(ret);
  946. }
  947. static ftrace_func_t saved_ftrace_func;
  948. static int ftrace_start_up;
  949. static void ftrace_startup_enable(int command)
  950. {
  951. if (saved_ftrace_func != ftrace_trace_function) {
  952. saved_ftrace_func = ftrace_trace_function;
  953. command |= FTRACE_UPDATE_TRACE_FUNC;
  954. }
  955. if (!command || !ftrace_enabled)
  956. return;
  957. ftrace_run_update_code(command);
  958. }
  959. static void ftrace_startup(int command)
  960. {
  961. if (unlikely(ftrace_disabled))
  962. return;
  963. ftrace_start_up++;
  964. command |= FTRACE_ENABLE_CALLS;
  965. ftrace_startup_enable(command);
  966. }
  967. static void ftrace_shutdown(int command)
  968. {
  969. if (unlikely(ftrace_disabled))
  970. return;
  971. ftrace_start_up--;
  972. /*
  973. * Just warn in case of unbalance, no need to kill ftrace, it's not
  974. * critical but the ftrace_call callers may be never nopped again after
  975. * further ftrace uses.
  976. */
  977. WARN_ON_ONCE(ftrace_start_up < 0);
  978. if (!ftrace_start_up)
  979. command |= FTRACE_DISABLE_CALLS;
  980. if (saved_ftrace_func != ftrace_trace_function) {
  981. saved_ftrace_func = ftrace_trace_function;
  982. command |= FTRACE_UPDATE_TRACE_FUNC;
  983. }
  984. if (!command || !ftrace_enabled)
  985. return;
  986. ftrace_run_update_code(command);
  987. }
  988. static void ftrace_startup_sysctl(void)
  989. {
  990. int command = FTRACE_ENABLE_MCOUNT;
  991. if (unlikely(ftrace_disabled))
  992. return;
  993. /* Force update next time */
  994. saved_ftrace_func = NULL;
  995. /* ftrace_start_up is true if we want ftrace running */
  996. if (ftrace_start_up)
  997. command |= FTRACE_ENABLE_CALLS;
  998. ftrace_run_update_code(command);
  999. }
  1000. static void ftrace_shutdown_sysctl(void)
  1001. {
  1002. int command = FTRACE_DISABLE_MCOUNT;
  1003. if (unlikely(ftrace_disabled))
  1004. return;
  1005. /* ftrace_start_up is true if ftrace is running */
  1006. if (ftrace_start_up)
  1007. command |= FTRACE_DISABLE_CALLS;
  1008. ftrace_run_update_code(command);
  1009. }
  1010. static cycle_t ftrace_update_time;
  1011. static unsigned long ftrace_update_cnt;
  1012. unsigned long ftrace_update_tot_cnt;
  1013. static int ftrace_update_code(struct module *mod)
  1014. {
  1015. struct dyn_ftrace *p;
  1016. cycle_t start, stop;
  1017. start = ftrace_now(raw_smp_processor_id());
  1018. ftrace_update_cnt = 0;
  1019. while (ftrace_new_addrs) {
  1020. /* If something went wrong, bail without enabling anything */
  1021. if (unlikely(ftrace_disabled))
  1022. return -1;
  1023. p = ftrace_new_addrs;
  1024. ftrace_new_addrs = p->newlist;
  1025. p->flags = 0L;
  1026. /* convert record (i.e, patch mcount-call with NOP) */
  1027. if (ftrace_code_disable(mod, p)) {
  1028. p->flags |= FTRACE_FL_CONVERTED;
  1029. ftrace_update_cnt++;
  1030. } else
  1031. ftrace_free_rec(p);
  1032. }
  1033. stop = ftrace_now(raw_smp_processor_id());
  1034. ftrace_update_time = stop - start;
  1035. ftrace_update_tot_cnt += ftrace_update_cnt;
  1036. return 0;
  1037. }
  1038. static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
  1039. {
  1040. struct ftrace_page *pg;
  1041. int cnt;
  1042. int i;
  1043. /* allocate a few pages */
  1044. ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
  1045. if (!ftrace_pages_start)
  1046. return -1;
  1047. /*
  1048. * Allocate a few more pages.
  1049. *
  1050. * TODO: have some parser search vmlinux before
  1051. * final linking to find all calls to ftrace.
  1052. * Then we can:
  1053. * a) know how many pages to allocate.
  1054. * and/or
  1055. * b) set up the table then.
  1056. *
  1057. * The dynamic code is still necessary for
  1058. * modules.
  1059. */
  1060. pg = ftrace_pages = ftrace_pages_start;
  1061. cnt = num_to_init / ENTRIES_PER_PAGE;
  1062. pr_info("ftrace: allocating %ld entries in %d pages\n",
  1063. num_to_init, cnt + 1);
  1064. for (i = 0; i < cnt; i++) {
  1065. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  1066. /* If we fail, we'll try later anyway */
  1067. if (!pg->next)
  1068. break;
  1069. pg = pg->next;
  1070. }
  1071. return 0;
  1072. }
  1073. enum {
  1074. FTRACE_ITER_FILTER = (1 << 0),
  1075. FTRACE_ITER_NOTRACE = (1 << 1),
  1076. FTRACE_ITER_FAILURES = (1 << 2),
  1077. FTRACE_ITER_PRINTALL = (1 << 3),
  1078. FTRACE_ITER_HASH = (1 << 4),
  1079. };
  1080. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  1081. struct ftrace_iterator {
  1082. struct ftrace_page *pg;
  1083. int hidx;
  1084. int idx;
  1085. unsigned flags;
  1086. struct trace_parser parser;
  1087. };
  1088. static void *
  1089. t_hash_next(struct seq_file *m, void *v, loff_t *pos)
  1090. {
  1091. struct ftrace_iterator *iter = m->private;
  1092. struct hlist_node *hnd = v;
  1093. struct hlist_head *hhd;
  1094. WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
  1095. (*pos)++;
  1096. retry:
  1097. if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
  1098. return NULL;
  1099. hhd = &ftrace_func_hash[iter->hidx];
  1100. if (hlist_empty(hhd)) {
  1101. iter->hidx++;
  1102. hnd = NULL;
  1103. goto retry;
  1104. }
  1105. if (!hnd)
  1106. hnd = hhd->first;
  1107. else {
  1108. hnd = hnd->next;
  1109. if (!hnd) {
  1110. iter->hidx++;
  1111. goto retry;
  1112. }
  1113. }
  1114. return hnd;
  1115. }
  1116. static void *t_hash_start(struct seq_file *m, loff_t *pos)
  1117. {
  1118. struct ftrace_iterator *iter = m->private;
  1119. void *p = NULL;
  1120. loff_t l;
  1121. if (!(iter->flags & FTRACE_ITER_HASH))
  1122. *pos = 0;
  1123. iter->flags |= FTRACE_ITER_HASH;
  1124. iter->hidx = 0;
  1125. for (l = 0; l <= *pos; ) {
  1126. p = t_hash_next(m, p, &l);
  1127. if (!p)
  1128. break;
  1129. }
  1130. return p;
  1131. }
  1132. static int t_hash_show(struct seq_file *m, void *v)
  1133. {
  1134. struct ftrace_func_probe *rec;
  1135. struct hlist_node *hnd = v;
  1136. rec = hlist_entry(hnd, struct ftrace_func_probe, node);
  1137. if (rec->ops->print)
  1138. return rec->ops->print(m, rec->ip, rec->ops, rec->data);
  1139. seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
  1140. if (rec->data)
  1141. seq_printf(m, ":%p", rec->data);
  1142. seq_putc(m, '\n');
  1143. return 0;
  1144. }
  1145. static void *
  1146. t_next(struct seq_file *m, void *v, loff_t *pos)
  1147. {
  1148. struct ftrace_iterator *iter = m->private;
  1149. struct dyn_ftrace *rec = NULL;
  1150. if (iter->flags & FTRACE_ITER_HASH)
  1151. return t_hash_next(m, v, pos);
  1152. (*pos)++;
  1153. if (iter->flags & FTRACE_ITER_PRINTALL)
  1154. return NULL;
  1155. retry:
  1156. if (iter->idx >= iter->pg->index) {
  1157. if (iter->pg->next) {
  1158. iter->pg = iter->pg->next;
  1159. iter->idx = 0;
  1160. goto retry;
  1161. }
  1162. } else {
  1163. rec = &iter->pg->records[iter->idx++];
  1164. if ((rec->flags & FTRACE_FL_FREE) ||
  1165. (!(iter->flags & FTRACE_ITER_FAILURES) &&
  1166. (rec->flags & FTRACE_FL_FAILED)) ||
  1167. ((iter->flags & FTRACE_ITER_FAILURES) &&
  1168. !(rec->flags & FTRACE_FL_FAILED)) ||
  1169. ((iter->flags & FTRACE_ITER_FILTER) &&
  1170. !(rec->flags & FTRACE_FL_FILTER)) ||
  1171. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  1172. !(rec->flags & FTRACE_FL_NOTRACE))) {
  1173. rec = NULL;
  1174. goto retry;
  1175. }
  1176. }
  1177. return rec;
  1178. }
  1179. static void *t_start(struct seq_file *m, loff_t *pos)
  1180. {
  1181. struct ftrace_iterator *iter = m->private;
  1182. void *p = NULL;
  1183. loff_t l;
  1184. mutex_lock(&ftrace_lock);
  1185. /*
  1186. * For set_ftrace_filter reading, if we have the filter
  1187. * off, we can short cut and just print out that all
  1188. * functions are enabled.
  1189. */
  1190. if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
  1191. if (*pos > 0)
  1192. return t_hash_start(m, pos);
  1193. iter->flags |= FTRACE_ITER_PRINTALL;
  1194. return iter;
  1195. }
  1196. if (iter->flags & FTRACE_ITER_HASH)
  1197. return t_hash_start(m, pos);
  1198. iter->pg = ftrace_pages_start;
  1199. iter->idx = 0;
  1200. for (l = 0; l <= *pos; ) {
  1201. p = t_next(m, p, &l);
  1202. if (!p)
  1203. break;
  1204. }
  1205. if (!p && iter->flags & FTRACE_ITER_FILTER)
  1206. return t_hash_start(m, pos);
  1207. return p;
  1208. }
  1209. static void t_stop(struct seq_file *m, void *p)
  1210. {
  1211. mutex_unlock(&ftrace_lock);
  1212. }
  1213. static int t_show(struct seq_file *m, void *v)
  1214. {
  1215. struct ftrace_iterator *iter = m->private;
  1216. struct dyn_ftrace *rec = v;
  1217. if (iter->flags & FTRACE_ITER_HASH)
  1218. return t_hash_show(m, v);
  1219. if (iter->flags & FTRACE_ITER_PRINTALL) {
  1220. seq_printf(m, "#### all functions enabled ####\n");
  1221. return 0;
  1222. }
  1223. if (!rec)
  1224. return 0;
  1225. seq_printf(m, "%ps\n", (void *)rec->ip);
  1226. return 0;
  1227. }
  1228. static const struct seq_operations show_ftrace_seq_ops = {
  1229. .start = t_start,
  1230. .next = t_next,
  1231. .stop = t_stop,
  1232. .show = t_show,
  1233. };
  1234. static int
  1235. ftrace_avail_open(struct inode *inode, struct file *file)
  1236. {
  1237. struct ftrace_iterator *iter;
  1238. int ret;
  1239. if (unlikely(ftrace_disabled))
  1240. return -ENODEV;
  1241. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1242. if (!iter)
  1243. return -ENOMEM;
  1244. iter->pg = ftrace_pages_start;
  1245. ret = seq_open(file, &show_ftrace_seq_ops);
  1246. if (!ret) {
  1247. struct seq_file *m = file->private_data;
  1248. m->private = iter;
  1249. } else {
  1250. kfree(iter);
  1251. }
  1252. return ret;
  1253. }
  1254. static int
  1255. ftrace_failures_open(struct inode *inode, struct file *file)
  1256. {
  1257. int ret;
  1258. struct seq_file *m;
  1259. struct ftrace_iterator *iter;
  1260. ret = ftrace_avail_open(inode, file);
  1261. if (!ret) {
  1262. m = (struct seq_file *)file->private_data;
  1263. iter = (struct ftrace_iterator *)m->private;
  1264. iter->flags = FTRACE_ITER_FAILURES;
  1265. }
  1266. return ret;
  1267. }
  1268. static void ftrace_filter_reset(int enable)
  1269. {
  1270. struct ftrace_page *pg;
  1271. struct dyn_ftrace *rec;
  1272. unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1273. mutex_lock(&ftrace_lock);
  1274. if (enable)
  1275. ftrace_filtered = 0;
  1276. do_for_each_ftrace_rec(pg, rec) {
  1277. if (rec->flags & FTRACE_FL_FAILED)
  1278. continue;
  1279. rec->flags &= ~type;
  1280. } while_for_each_ftrace_rec();
  1281. mutex_unlock(&ftrace_lock);
  1282. }
  1283. static int
  1284. ftrace_regex_open(struct inode *inode, struct file *file, int enable)
  1285. {
  1286. struct ftrace_iterator *iter;
  1287. int ret = 0;
  1288. if (unlikely(ftrace_disabled))
  1289. return -ENODEV;
  1290. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1291. if (!iter)
  1292. return -ENOMEM;
  1293. if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
  1294. kfree(iter);
  1295. return -ENOMEM;
  1296. }
  1297. mutex_lock(&ftrace_regex_lock);
  1298. if ((file->f_mode & FMODE_WRITE) &&
  1299. (file->f_flags & O_TRUNC))
  1300. ftrace_filter_reset(enable);
  1301. if (file->f_mode & FMODE_READ) {
  1302. iter->pg = ftrace_pages_start;
  1303. iter->flags = enable ? FTRACE_ITER_FILTER :
  1304. FTRACE_ITER_NOTRACE;
  1305. ret = seq_open(file, &show_ftrace_seq_ops);
  1306. if (!ret) {
  1307. struct seq_file *m = file->private_data;
  1308. m->private = iter;
  1309. } else {
  1310. trace_parser_put(&iter->parser);
  1311. kfree(iter);
  1312. }
  1313. } else
  1314. file->private_data = iter;
  1315. mutex_unlock(&ftrace_regex_lock);
  1316. return ret;
  1317. }
  1318. static int
  1319. ftrace_filter_open(struct inode *inode, struct file *file)
  1320. {
  1321. return ftrace_regex_open(inode, file, 1);
  1322. }
  1323. static int
  1324. ftrace_notrace_open(struct inode *inode, struct file *file)
  1325. {
  1326. return ftrace_regex_open(inode, file, 0);
  1327. }
  1328. static loff_t
  1329. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  1330. {
  1331. loff_t ret;
  1332. if (file->f_mode & FMODE_READ)
  1333. ret = seq_lseek(file, offset, origin);
  1334. else
  1335. file->f_pos = ret = 1;
  1336. return ret;
  1337. }
  1338. enum {
  1339. MATCH_FULL,
  1340. MATCH_FRONT_ONLY,
  1341. MATCH_MIDDLE_ONLY,
  1342. MATCH_END_ONLY,
  1343. };
  1344. /*
  1345. * (static function - no need for kernel doc)
  1346. *
  1347. * Pass in a buffer containing a glob and this function will
  1348. * set search to point to the search part of the buffer and
  1349. * return the type of search it is (see enum above).
  1350. * This does modify buff.
  1351. *
  1352. * Returns enum type.
  1353. * search returns the pointer to use for comparison.
  1354. * not returns 1 if buff started with a '!'
  1355. * 0 otherwise.
  1356. */
  1357. static int
  1358. ftrace_setup_glob(char *buff, int len, char **search, int *not)
  1359. {
  1360. int type = MATCH_FULL;
  1361. int i;
  1362. if (buff[0] == '!') {
  1363. *not = 1;
  1364. buff++;
  1365. len--;
  1366. } else
  1367. *not = 0;
  1368. *search = buff;
  1369. for (i = 0; i < len; i++) {
  1370. if (buff[i] == '*') {
  1371. if (!i) {
  1372. *search = buff + 1;
  1373. type = MATCH_END_ONLY;
  1374. } else {
  1375. if (type == MATCH_END_ONLY)
  1376. type = MATCH_MIDDLE_ONLY;
  1377. else
  1378. type = MATCH_FRONT_ONLY;
  1379. buff[i] = 0;
  1380. break;
  1381. }
  1382. }
  1383. }
  1384. return type;
  1385. }
  1386. static int ftrace_match(char *str, char *regex, int len, int type)
  1387. {
  1388. int matched = 0;
  1389. char *ptr;
  1390. switch (type) {
  1391. case MATCH_FULL:
  1392. if (strcmp(str, regex) == 0)
  1393. matched = 1;
  1394. break;
  1395. case MATCH_FRONT_ONLY:
  1396. if (strncmp(str, regex, len) == 0)
  1397. matched = 1;
  1398. break;
  1399. case MATCH_MIDDLE_ONLY:
  1400. if (strstr(str, regex))
  1401. matched = 1;
  1402. break;
  1403. case MATCH_END_ONLY:
  1404. ptr = strstr(str, regex);
  1405. if (ptr && (ptr[len] == 0))
  1406. matched = 1;
  1407. break;
  1408. }
  1409. return matched;
  1410. }
  1411. static int
  1412. ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
  1413. {
  1414. char str[KSYM_SYMBOL_LEN];
  1415. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  1416. return ftrace_match(str, regex, len, type);
  1417. }
  1418. static void ftrace_match_records(char *buff, int len, int enable)
  1419. {
  1420. unsigned int search_len;
  1421. struct ftrace_page *pg;
  1422. struct dyn_ftrace *rec;
  1423. unsigned long flag;
  1424. char *search;
  1425. int type;
  1426. int not;
  1427. flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1428. type = ftrace_setup_glob(buff, len, &search, &not);
  1429. search_len = strlen(search);
  1430. mutex_lock(&ftrace_lock);
  1431. do_for_each_ftrace_rec(pg, rec) {
  1432. if (rec->flags & FTRACE_FL_FAILED)
  1433. continue;
  1434. if (ftrace_match_record(rec, search, search_len, type)) {
  1435. if (not)
  1436. rec->flags &= ~flag;
  1437. else
  1438. rec->flags |= flag;
  1439. }
  1440. /*
  1441. * Only enable filtering if we have a function that
  1442. * is filtered on.
  1443. */
  1444. if (enable && (rec->flags & FTRACE_FL_FILTER))
  1445. ftrace_filtered = 1;
  1446. } while_for_each_ftrace_rec();
  1447. mutex_unlock(&ftrace_lock);
  1448. }
  1449. static int
  1450. ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
  1451. char *regex, int len, int type)
  1452. {
  1453. char str[KSYM_SYMBOL_LEN];
  1454. char *modname;
  1455. kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
  1456. if (!modname || strcmp(modname, mod))
  1457. return 0;
  1458. /* blank search means to match all funcs in the mod */
  1459. if (len)
  1460. return ftrace_match(str, regex, len, type);
  1461. else
  1462. return 1;
  1463. }
  1464. static void ftrace_match_module_records(char *buff, char *mod, int enable)
  1465. {
  1466. unsigned search_len = 0;
  1467. struct ftrace_page *pg;
  1468. struct dyn_ftrace *rec;
  1469. int type = MATCH_FULL;
  1470. char *search = buff;
  1471. unsigned long flag;
  1472. int not = 0;
  1473. flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1474. /* blank or '*' mean the same */
  1475. if (strcmp(buff, "*") == 0)
  1476. buff[0] = 0;
  1477. /* handle the case of 'dont filter this module' */
  1478. if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
  1479. buff[0] = 0;
  1480. not = 1;
  1481. }
  1482. if (strlen(buff)) {
  1483. type = ftrace_setup_glob(buff, strlen(buff), &search, &not);
  1484. search_len = strlen(search);
  1485. }
  1486. mutex_lock(&ftrace_lock);
  1487. do_for_each_ftrace_rec(pg, rec) {
  1488. if (rec->flags & FTRACE_FL_FAILED)
  1489. continue;
  1490. if (ftrace_match_module_record(rec, mod,
  1491. search, search_len, type)) {
  1492. if (not)
  1493. rec->flags &= ~flag;
  1494. else
  1495. rec->flags |= flag;
  1496. }
  1497. if (enable && (rec->flags & FTRACE_FL_FILTER))
  1498. ftrace_filtered = 1;
  1499. } while_for_each_ftrace_rec();
  1500. mutex_unlock(&ftrace_lock);
  1501. }
  1502. /*
  1503. * We register the module command as a template to show others how
  1504. * to register the a command as well.
  1505. */
  1506. static int
  1507. ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
  1508. {
  1509. char *mod;
  1510. /*
  1511. * cmd == 'mod' because we only registered this func
  1512. * for the 'mod' ftrace_func_command.
  1513. * But if you register one func with multiple commands,
  1514. * you can tell which command was used by the cmd
  1515. * parameter.
  1516. */
  1517. /* we must have a module name */
  1518. if (!param)
  1519. return -EINVAL;
  1520. mod = strsep(&param, ":");
  1521. if (!strlen(mod))
  1522. return -EINVAL;
  1523. ftrace_match_module_records(func, mod, enable);
  1524. return 0;
  1525. }
  1526. static struct ftrace_func_command ftrace_mod_cmd = {
  1527. .name = "mod",
  1528. .func = ftrace_mod_callback,
  1529. };
  1530. static int __init ftrace_mod_cmd_init(void)
  1531. {
  1532. return register_ftrace_command(&ftrace_mod_cmd);
  1533. }
  1534. device_initcall(ftrace_mod_cmd_init);
  1535. static void
  1536. function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
  1537. {
  1538. struct ftrace_func_probe *entry;
  1539. struct hlist_head *hhd;
  1540. struct hlist_node *n;
  1541. unsigned long key;
  1542. int resched;
  1543. key = hash_long(ip, FTRACE_HASH_BITS);
  1544. hhd = &ftrace_func_hash[key];
  1545. if (hlist_empty(hhd))
  1546. return;
  1547. /*
  1548. * Disable preemption for these calls to prevent a RCU grace
  1549. * period. This syncs the hash iteration and freeing of items
  1550. * on the hash. rcu_read_lock is too dangerous here.
  1551. */
  1552. resched = ftrace_preempt_disable();
  1553. hlist_for_each_entry_rcu(entry, n, hhd, node) {
  1554. if (entry->ip == ip)
  1555. entry->ops->func(ip, parent_ip, &entry->data);
  1556. }
  1557. ftrace_preempt_enable(resched);
  1558. }
  1559. static struct ftrace_ops trace_probe_ops __read_mostly =
  1560. {
  1561. .func = function_trace_probe_call,
  1562. };
  1563. static int ftrace_probe_registered;
  1564. static void __enable_ftrace_function_probe(void)
  1565. {
  1566. int i;
  1567. if (ftrace_probe_registered)
  1568. return;
  1569. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1570. struct hlist_head *hhd = &ftrace_func_hash[i];
  1571. if (hhd->first)
  1572. break;
  1573. }
  1574. /* Nothing registered? */
  1575. if (i == FTRACE_FUNC_HASHSIZE)
  1576. return;
  1577. __register_ftrace_function(&trace_probe_ops);
  1578. ftrace_startup(0);
  1579. ftrace_probe_registered = 1;
  1580. }
  1581. static void __disable_ftrace_function_probe(void)
  1582. {
  1583. int i;
  1584. if (!ftrace_probe_registered)
  1585. return;
  1586. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1587. struct hlist_head *hhd = &ftrace_func_hash[i];
  1588. if (hhd->first)
  1589. return;
  1590. }
  1591. /* no more funcs left */
  1592. __unregister_ftrace_function(&trace_probe_ops);
  1593. ftrace_shutdown(0);
  1594. ftrace_probe_registered = 0;
  1595. }
  1596. static void ftrace_free_entry_rcu(struct rcu_head *rhp)
  1597. {
  1598. struct ftrace_func_probe *entry =
  1599. container_of(rhp, struct ftrace_func_probe, rcu);
  1600. if (entry->ops->free)
  1601. entry->ops->free(&entry->data);
  1602. kfree(entry);
  1603. }
  1604. int
  1605. register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1606. void *data)
  1607. {
  1608. struct ftrace_func_probe *entry;
  1609. struct ftrace_page *pg;
  1610. struct dyn_ftrace *rec;
  1611. int type, len, not;
  1612. unsigned long key;
  1613. int count = 0;
  1614. char *search;
  1615. type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
  1616. len = strlen(search);
  1617. /* we do not support '!' for function probes */
  1618. if (WARN_ON(not))
  1619. return -EINVAL;
  1620. mutex_lock(&ftrace_lock);
  1621. do_for_each_ftrace_rec(pg, rec) {
  1622. if (rec->flags & FTRACE_FL_FAILED)
  1623. continue;
  1624. if (!ftrace_match_record(rec, search, len, type))
  1625. continue;
  1626. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  1627. if (!entry) {
  1628. /* If we did not process any, then return error */
  1629. if (!count)
  1630. count = -ENOMEM;
  1631. goto out_unlock;
  1632. }
  1633. count++;
  1634. entry->data = data;
  1635. /*
  1636. * The caller might want to do something special
  1637. * for each function we find. We call the callback
  1638. * to give the caller an opportunity to do so.
  1639. */
  1640. if (ops->callback) {
  1641. if (ops->callback(rec->ip, &entry->data) < 0) {
  1642. /* caller does not like this func */
  1643. kfree(entry);
  1644. continue;
  1645. }
  1646. }
  1647. entry->ops = ops;
  1648. entry->ip = rec->ip;
  1649. key = hash_long(entry->ip, FTRACE_HASH_BITS);
  1650. hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
  1651. } while_for_each_ftrace_rec();
  1652. __enable_ftrace_function_probe();
  1653. out_unlock:
  1654. mutex_unlock(&ftrace_lock);
  1655. return count;
  1656. }
  1657. enum {
  1658. PROBE_TEST_FUNC = 1,
  1659. PROBE_TEST_DATA = 2
  1660. };
  1661. static void
  1662. __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1663. void *data, int flags)
  1664. {
  1665. struct ftrace_func_probe *entry;
  1666. struct hlist_node *n, *tmp;
  1667. char str[KSYM_SYMBOL_LEN];
  1668. int type = MATCH_FULL;
  1669. int i, len = 0;
  1670. char *search;
  1671. if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
  1672. glob = NULL;
  1673. else if (glob) {
  1674. int not;
  1675. type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
  1676. len = strlen(search);
  1677. /* we do not support '!' for function probes */
  1678. if (WARN_ON(not))
  1679. return;
  1680. }
  1681. mutex_lock(&ftrace_lock);
  1682. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1683. struct hlist_head *hhd = &ftrace_func_hash[i];
  1684. hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
  1685. /* break up if statements for readability */
  1686. if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
  1687. continue;
  1688. if ((flags & PROBE_TEST_DATA) && entry->data != data)
  1689. continue;
  1690. /* do this last, since it is the most expensive */
  1691. if (glob) {
  1692. kallsyms_lookup(entry->ip, NULL, NULL,
  1693. NULL, str);
  1694. if (!ftrace_match(str, glob, len, type))
  1695. continue;
  1696. }
  1697. hlist_del(&entry->node);
  1698. call_rcu(&entry->rcu, ftrace_free_entry_rcu);
  1699. }
  1700. }
  1701. __disable_ftrace_function_probe();
  1702. mutex_unlock(&ftrace_lock);
  1703. }
  1704. void
  1705. unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1706. void *data)
  1707. {
  1708. __unregister_ftrace_function_probe(glob, ops, data,
  1709. PROBE_TEST_FUNC | PROBE_TEST_DATA);
  1710. }
  1711. void
  1712. unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
  1713. {
  1714. __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
  1715. }
  1716. void unregister_ftrace_function_probe_all(char *glob)
  1717. {
  1718. __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
  1719. }
  1720. static LIST_HEAD(ftrace_commands);
  1721. static DEFINE_MUTEX(ftrace_cmd_mutex);
  1722. int register_ftrace_command(struct ftrace_func_command *cmd)
  1723. {
  1724. struct ftrace_func_command *p;
  1725. int ret = 0;
  1726. mutex_lock(&ftrace_cmd_mutex);
  1727. list_for_each_entry(p, &ftrace_commands, list) {
  1728. if (strcmp(cmd->name, p->name) == 0) {
  1729. ret = -EBUSY;
  1730. goto out_unlock;
  1731. }
  1732. }
  1733. list_add(&cmd->list, &ftrace_commands);
  1734. out_unlock:
  1735. mutex_unlock(&ftrace_cmd_mutex);
  1736. return ret;
  1737. }
  1738. int unregister_ftrace_command(struct ftrace_func_command *cmd)
  1739. {
  1740. struct ftrace_func_command *p, *n;
  1741. int ret = -ENODEV;
  1742. mutex_lock(&ftrace_cmd_mutex);
  1743. list_for_each_entry_safe(p, n, &ftrace_commands, list) {
  1744. if (strcmp(cmd->name, p->name) == 0) {
  1745. ret = 0;
  1746. list_del_init(&p->list);
  1747. goto out_unlock;
  1748. }
  1749. }
  1750. out_unlock:
  1751. mutex_unlock(&ftrace_cmd_mutex);
  1752. return ret;
  1753. }
  1754. static int ftrace_process_regex(char *buff, int len, int enable)
  1755. {
  1756. char *func, *command, *next = buff;
  1757. struct ftrace_func_command *p;
  1758. int ret = -EINVAL;
  1759. func = strsep(&next, ":");
  1760. if (!next) {
  1761. ftrace_match_records(func, len, enable);
  1762. return 0;
  1763. }
  1764. /* command found */
  1765. command = strsep(&next, ":");
  1766. mutex_lock(&ftrace_cmd_mutex);
  1767. list_for_each_entry(p, &ftrace_commands, list) {
  1768. if (strcmp(p->name, command) == 0) {
  1769. ret = p->func(func, command, next, enable);
  1770. goto out_unlock;
  1771. }
  1772. }
  1773. out_unlock:
  1774. mutex_unlock(&ftrace_cmd_mutex);
  1775. return ret;
  1776. }
  1777. static ssize_t
  1778. ftrace_regex_write(struct file *file, const char __user *ubuf,
  1779. size_t cnt, loff_t *ppos, int enable)
  1780. {
  1781. struct ftrace_iterator *iter;
  1782. struct trace_parser *parser;
  1783. ssize_t ret, read;
  1784. if (!cnt)
  1785. return 0;
  1786. mutex_lock(&ftrace_regex_lock);
  1787. if (file->f_mode & FMODE_READ) {
  1788. struct seq_file *m = file->private_data;
  1789. iter = m->private;
  1790. } else
  1791. iter = file->private_data;
  1792. parser = &iter->parser;
  1793. read = trace_get_user(parser, ubuf, cnt, ppos);
  1794. if (read >= 0 && trace_parser_loaded(parser) &&
  1795. !trace_parser_cont(parser)) {
  1796. ret = ftrace_process_regex(parser->buffer,
  1797. parser->idx, enable);
  1798. if (ret)
  1799. goto out;
  1800. trace_parser_clear(parser);
  1801. }
  1802. ret = read;
  1803. mutex_unlock(&ftrace_regex_lock);
  1804. out:
  1805. return ret;
  1806. }
  1807. static ssize_t
  1808. ftrace_filter_write(struct file *file, const char __user *ubuf,
  1809. size_t cnt, loff_t *ppos)
  1810. {
  1811. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  1812. }
  1813. static ssize_t
  1814. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  1815. size_t cnt, loff_t *ppos)
  1816. {
  1817. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  1818. }
  1819. static void
  1820. ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
  1821. {
  1822. if (unlikely(ftrace_disabled))
  1823. return;
  1824. mutex_lock(&ftrace_regex_lock);
  1825. if (reset)
  1826. ftrace_filter_reset(enable);
  1827. if (buf)
  1828. ftrace_match_records(buf, len, enable);
  1829. mutex_unlock(&ftrace_regex_lock);
  1830. }
  1831. /**
  1832. * ftrace_set_filter - set a function to filter on in ftrace
  1833. * @buf - the string that holds the function filter text.
  1834. * @len - the length of the string.
  1835. * @reset - non zero to reset all filters before applying this filter.
  1836. *
  1837. * Filters denote which functions should be enabled when tracing is enabled.
  1838. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  1839. */
  1840. void ftrace_set_filter(unsigned char *buf, int len, int reset)
  1841. {
  1842. ftrace_set_regex(buf, len, reset, 1);
  1843. }
  1844. /**
  1845. * ftrace_set_notrace - set a function to not trace in ftrace
  1846. * @buf - the string that holds the function notrace text.
  1847. * @len - the length of the string.
  1848. * @reset - non zero to reset all filters before applying this filter.
  1849. *
  1850. * Notrace Filters denote which functions should not be enabled when tracing
  1851. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  1852. * for tracing.
  1853. */
  1854. void ftrace_set_notrace(unsigned char *buf, int len, int reset)
  1855. {
  1856. ftrace_set_regex(buf, len, reset, 0);
  1857. }
  1858. /*
  1859. * command line interface to allow users to set filters on boot up.
  1860. */
  1861. #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
  1862. static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
  1863. static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
  1864. static int __init set_ftrace_notrace(char *str)
  1865. {
  1866. strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
  1867. return 1;
  1868. }
  1869. __setup("ftrace_notrace=", set_ftrace_notrace);
  1870. static int __init set_ftrace_filter(char *str)
  1871. {
  1872. strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
  1873. return 1;
  1874. }
  1875. __setup("ftrace_filter=", set_ftrace_filter);
  1876. static void __init set_ftrace_early_filter(char *buf, int enable)
  1877. {
  1878. char *func;
  1879. while (buf) {
  1880. func = strsep(&buf, ",");
  1881. ftrace_set_regex(func, strlen(func), 0, enable);
  1882. }
  1883. }
  1884. static void __init set_ftrace_early_filters(void)
  1885. {
  1886. if (ftrace_filter_buf[0])
  1887. set_ftrace_early_filter(ftrace_filter_buf, 1);
  1888. if (ftrace_notrace_buf[0])
  1889. set_ftrace_early_filter(ftrace_notrace_buf, 0);
  1890. }
  1891. static int
  1892. ftrace_regex_release(struct inode *inode, struct file *file, int enable)
  1893. {
  1894. struct seq_file *m = (struct seq_file *)file->private_data;
  1895. struct ftrace_iterator *iter;
  1896. struct trace_parser *parser;
  1897. mutex_lock(&ftrace_regex_lock);
  1898. if (file->f_mode & FMODE_READ) {
  1899. iter = m->private;
  1900. seq_release(inode, file);
  1901. } else
  1902. iter = file->private_data;
  1903. parser = &iter->parser;
  1904. if (trace_parser_loaded(parser)) {
  1905. parser->buffer[parser->idx] = 0;
  1906. ftrace_match_records(parser->buffer, parser->idx, enable);
  1907. }
  1908. mutex_lock(&ftrace_lock);
  1909. if (ftrace_start_up && ftrace_enabled)
  1910. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  1911. mutex_unlock(&ftrace_lock);
  1912. trace_parser_put(parser);
  1913. kfree(iter);
  1914. mutex_unlock(&ftrace_regex_lock);
  1915. return 0;
  1916. }
  1917. static int
  1918. ftrace_filter_release(struct inode *inode, struct file *file)
  1919. {
  1920. return ftrace_regex_release(inode, file, 1);
  1921. }
  1922. static int
  1923. ftrace_notrace_release(struct inode *inode, struct file *file)
  1924. {
  1925. return ftrace_regex_release(inode, file, 0);
  1926. }
  1927. static const struct file_operations ftrace_avail_fops = {
  1928. .open = ftrace_avail_open,
  1929. .read = seq_read,
  1930. .llseek = seq_lseek,
  1931. .release = seq_release_private,
  1932. };
  1933. static const struct file_operations ftrace_failures_fops = {
  1934. .open = ftrace_failures_open,
  1935. .read = seq_read,
  1936. .llseek = seq_lseek,
  1937. .release = seq_release_private,
  1938. };
  1939. static const struct file_operations ftrace_filter_fops = {
  1940. .open = ftrace_filter_open,
  1941. .read = seq_read,
  1942. .write = ftrace_filter_write,
  1943. .llseek = ftrace_regex_lseek,
  1944. .release = ftrace_filter_release,
  1945. };
  1946. static const struct file_operations ftrace_notrace_fops = {
  1947. .open = ftrace_notrace_open,
  1948. .read = seq_read,
  1949. .write = ftrace_notrace_write,
  1950. .llseek = ftrace_regex_lseek,
  1951. .release = ftrace_notrace_release,
  1952. };
  1953. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1954. static DEFINE_MUTEX(graph_lock);
  1955. int ftrace_graph_count;
  1956. unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
  1957. static void *
  1958. __g_next(struct seq_file *m, loff_t *pos)
  1959. {
  1960. if (*pos >= ftrace_graph_count)
  1961. return NULL;
  1962. return &ftrace_graph_funcs[*pos];
  1963. }
  1964. static void *
  1965. g_next(struct seq_file *m, void *v, loff_t *pos)
  1966. {
  1967. (*pos)++;
  1968. return __g_next(m, pos);
  1969. }
  1970. static void *g_start(struct seq_file *m, loff_t *pos)
  1971. {
  1972. mutex_lock(&graph_lock);
  1973. /* Nothing, tell g_show to print all functions are enabled */
  1974. if (!ftrace_graph_count && !*pos)
  1975. return (void *)1;
  1976. return __g_next(m, pos);
  1977. }
  1978. static void g_stop(struct seq_file *m, void *p)
  1979. {
  1980. mutex_unlock(&graph_lock);
  1981. }
  1982. static int g_show(struct seq_file *m, void *v)
  1983. {
  1984. unsigned long *ptr = v;
  1985. if (!ptr)
  1986. return 0;
  1987. if (ptr == (unsigned long *)1) {
  1988. seq_printf(m, "#### all functions enabled ####\n");
  1989. return 0;
  1990. }
  1991. seq_printf(m, "%ps\n", (void *)*ptr);
  1992. return 0;
  1993. }
  1994. static const struct seq_operations ftrace_graph_seq_ops = {
  1995. .start = g_start,
  1996. .next = g_next,
  1997. .stop = g_stop,
  1998. .show = g_show,
  1999. };
  2000. static int
  2001. ftrace_graph_open(struct inode *inode, struct file *file)
  2002. {
  2003. int ret = 0;
  2004. if (unlikely(ftrace_disabled))
  2005. return -ENODEV;
  2006. mutex_lock(&graph_lock);
  2007. if ((file->f_mode & FMODE_WRITE) &&
  2008. (file->f_flags & O_TRUNC)) {
  2009. ftrace_graph_count = 0;
  2010. memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
  2011. }
  2012. mutex_unlock(&graph_lock);
  2013. if (file->f_mode & FMODE_READ)
  2014. ret = seq_open(file, &ftrace_graph_seq_ops);
  2015. return ret;
  2016. }
  2017. static int
  2018. ftrace_graph_release(struct inode *inode, struct file *file)
  2019. {
  2020. if (file->f_mode & FMODE_READ)
  2021. seq_release(inode, file);
  2022. return 0;
  2023. }
  2024. static int
  2025. ftrace_set_func(unsigned long *array, int *idx, char *buffer)
  2026. {
  2027. struct dyn_ftrace *rec;
  2028. struct ftrace_page *pg;
  2029. int search_len;
  2030. int found = 0;
  2031. int type, not;
  2032. char *search;
  2033. bool exists;
  2034. int i;
  2035. if (ftrace_disabled)
  2036. return -ENODEV;
  2037. /* decode regex */
  2038. type = ftrace_setup_glob(buffer, strlen(buffer), &search, &not);
  2039. if (not)
  2040. return -EINVAL;
  2041. search_len = strlen(search);
  2042. mutex_lock(&ftrace_lock);
  2043. do_for_each_ftrace_rec(pg, rec) {
  2044. if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
  2045. break;
  2046. if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
  2047. continue;
  2048. if (ftrace_match_record(rec, search, search_len, type)) {
  2049. /* ensure it is not already in the array */
  2050. exists = false;
  2051. for (i = 0; i < *idx; i++)
  2052. if (array[i] == rec->ip) {
  2053. exists = true;
  2054. break;
  2055. }
  2056. if (!exists) {
  2057. array[(*idx)++] = rec->ip;
  2058. found = 1;
  2059. }
  2060. }
  2061. } while_for_each_ftrace_rec();
  2062. mutex_unlock(&ftrace_lock);
  2063. return found ? 0 : -EINVAL;
  2064. }
  2065. static ssize_t
  2066. ftrace_graph_write(struct file *file, const char __user *ubuf,
  2067. size_t cnt, loff_t *ppos)
  2068. {
  2069. struct trace_parser parser;
  2070. ssize_t read, ret;
  2071. if (!cnt || cnt < 0)
  2072. return 0;
  2073. mutex_lock(&graph_lock);
  2074. if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
  2075. ret = -EBUSY;
  2076. goto out_unlock;
  2077. }
  2078. if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
  2079. ret = -ENOMEM;
  2080. goto out_unlock;
  2081. }
  2082. read = trace_get_user(&parser, ubuf, cnt, ppos);
  2083. if (read >= 0 && trace_parser_loaded((&parser))) {
  2084. parser.buffer[parser.idx] = 0;
  2085. /* we allow only one expression at a time */
  2086. ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
  2087. parser.buffer);
  2088. if (ret)
  2089. goto out_free;
  2090. }
  2091. ret = read;
  2092. out_free:
  2093. trace_parser_put(&parser);
  2094. out_unlock:
  2095. mutex_unlock(&graph_lock);
  2096. return ret;
  2097. }
  2098. static const struct file_operations ftrace_graph_fops = {
  2099. .open = ftrace_graph_open,
  2100. .read = seq_read,
  2101. .write = ftrace_graph_write,
  2102. .release = ftrace_graph_release,
  2103. };
  2104. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2105. static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
  2106. {
  2107. trace_create_file("available_filter_functions", 0444,
  2108. d_tracer, NULL, &ftrace_avail_fops);
  2109. trace_create_file("failures", 0444,
  2110. d_tracer, NULL, &ftrace_failures_fops);
  2111. trace_create_file("set_ftrace_filter", 0644, d_tracer,
  2112. NULL, &ftrace_filter_fops);
  2113. trace_create_file("set_ftrace_notrace", 0644, d_tracer,
  2114. NULL, &ftrace_notrace_fops);
  2115. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2116. trace_create_file("set_graph_function", 0444, d_tracer,
  2117. NULL,
  2118. &ftrace_graph_fops);
  2119. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2120. return 0;
  2121. }
  2122. static int ftrace_convert_nops(struct module *mod,
  2123. unsigned long *start,
  2124. unsigned long *end)
  2125. {
  2126. unsigned long *p;
  2127. unsigned long addr;
  2128. unsigned long flags;
  2129. mutex_lock(&ftrace_lock);
  2130. p = start;
  2131. while (p < end) {
  2132. addr = ftrace_call_adjust(*p++);
  2133. /*
  2134. * Some architecture linkers will pad between
  2135. * the different mcount_loc sections of different
  2136. * object files to satisfy alignments.
  2137. * Skip any NULL pointers.
  2138. */
  2139. if (!addr)
  2140. continue;
  2141. ftrace_record_ip(addr);
  2142. }
  2143. /* disable interrupts to prevent kstop machine */
  2144. local_irq_save(flags);
  2145. ftrace_update_code(mod);
  2146. local_irq_restore(flags);
  2147. mutex_unlock(&ftrace_lock);
  2148. return 0;
  2149. }
  2150. #ifdef CONFIG_MODULES
  2151. void ftrace_release_mod(struct module *mod)
  2152. {
  2153. struct dyn_ftrace *rec;
  2154. struct ftrace_page *pg;
  2155. if (ftrace_disabled)
  2156. return;
  2157. mutex_lock(&ftrace_lock);
  2158. do_for_each_ftrace_rec(pg, rec) {
  2159. if (within_module_core(rec->ip, mod)) {
  2160. /*
  2161. * rec->ip is changed in ftrace_free_rec()
  2162. * It should not between s and e if record was freed.
  2163. */
  2164. FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
  2165. ftrace_free_rec(rec);
  2166. }
  2167. } while_for_each_ftrace_rec();
  2168. mutex_unlock(&ftrace_lock);
  2169. }
  2170. static void ftrace_init_module(struct module *mod,
  2171. unsigned long *start, unsigned long *end)
  2172. {
  2173. if (ftrace_disabled || start == end)
  2174. return;
  2175. ftrace_convert_nops(mod, start, end);
  2176. }
  2177. static int ftrace_module_notify(struct notifier_block *self,
  2178. unsigned long val, void *data)
  2179. {
  2180. struct module *mod = data;
  2181. switch (val) {
  2182. case MODULE_STATE_COMING:
  2183. ftrace_init_module(mod, mod->ftrace_callsites,
  2184. mod->ftrace_callsites +
  2185. mod->num_ftrace_callsites);
  2186. break;
  2187. case MODULE_STATE_GOING:
  2188. ftrace_release_mod(mod);
  2189. break;
  2190. }
  2191. return 0;
  2192. }
  2193. #else
  2194. static int ftrace_module_notify(struct notifier_block *self,
  2195. unsigned long val, void *data)
  2196. {
  2197. return 0;
  2198. }
  2199. #endif /* CONFIG_MODULES */
  2200. struct notifier_block ftrace_module_nb = {
  2201. .notifier_call = ftrace_module_notify,
  2202. .priority = 0,
  2203. };
  2204. extern unsigned long __start_mcount_loc[];
  2205. extern unsigned long __stop_mcount_loc[];
  2206. void __init ftrace_init(void)
  2207. {
  2208. unsigned long count, addr, flags;
  2209. int ret;
  2210. /* Keep the ftrace pointer to the stub */
  2211. addr = (unsigned long)ftrace_stub;
  2212. local_irq_save(flags);
  2213. ftrace_dyn_arch_init(&addr);
  2214. local_irq_restore(flags);
  2215. /* ftrace_dyn_arch_init places the return code in addr */
  2216. if (addr)
  2217. goto failed;
  2218. count = __stop_mcount_loc - __start_mcount_loc;
  2219. ret = ftrace_dyn_table_alloc(count);
  2220. if (ret)
  2221. goto failed;
  2222. last_ftrace_enabled = ftrace_enabled = 1;
  2223. ret = ftrace_convert_nops(NULL,
  2224. __start_mcount_loc,
  2225. __stop_mcount_loc);
  2226. ret = register_module_notifier(&ftrace_module_nb);
  2227. if (ret)
  2228. pr_warning("Failed to register trace ftrace module notifier\n");
  2229. set_ftrace_early_filters();
  2230. return;
  2231. failed:
  2232. ftrace_disabled = 1;
  2233. }
  2234. #else
  2235. static int __init ftrace_nodyn_init(void)
  2236. {
  2237. ftrace_enabled = 1;
  2238. return 0;
  2239. }
  2240. device_initcall(ftrace_nodyn_init);
  2241. static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
  2242. static inline void ftrace_startup_enable(int command) { }
  2243. /* Keep as macros so we do not need to define the commands */
  2244. # define ftrace_startup(command) do { } while (0)
  2245. # define ftrace_shutdown(command) do { } while (0)
  2246. # define ftrace_startup_sysctl() do { } while (0)
  2247. # define ftrace_shutdown_sysctl() do { } while (0)
  2248. #endif /* CONFIG_DYNAMIC_FTRACE */
  2249. static ssize_t
  2250. ftrace_pid_read(struct file *file, char __user *ubuf,
  2251. size_t cnt, loff_t *ppos)
  2252. {
  2253. char buf[64];
  2254. int r;
  2255. if (ftrace_pid_trace == ftrace_swapper_pid)
  2256. r = sprintf(buf, "swapper tasks\n");
  2257. else if (ftrace_pid_trace)
  2258. r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
  2259. else
  2260. r = sprintf(buf, "no pid\n");
  2261. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2262. }
  2263. static void clear_ftrace_swapper(void)
  2264. {
  2265. struct task_struct *p;
  2266. int cpu;
  2267. get_online_cpus();
  2268. for_each_online_cpu(cpu) {
  2269. p = idle_task(cpu);
  2270. clear_tsk_trace_trace(p);
  2271. }
  2272. put_online_cpus();
  2273. }
  2274. static void set_ftrace_swapper(void)
  2275. {
  2276. struct task_struct *p;
  2277. int cpu;
  2278. get_online_cpus();
  2279. for_each_online_cpu(cpu) {
  2280. p = idle_task(cpu);
  2281. set_tsk_trace_trace(p);
  2282. }
  2283. put_online_cpus();
  2284. }
  2285. static void clear_ftrace_pid(struct pid *pid)
  2286. {
  2287. struct task_struct *p;
  2288. rcu_read_lock();
  2289. do_each_pid_task(pid, PIDTYPE_PID, p) {
  2290. clear_tsk_trace_trace(p);
  2291. } while_each_pid_task(pid, PIDTYPE_PID, p);
  2292. rcu_read_unlock();
  2293. put_pid(pid);
  2294. }
  2295. static void set_ftrace_pid(struct pid *pid)
  2296. {
  2297. struct task_struct *p;
  2298. rcu_read_lock();
  2299. do_each_pid_task(pid, PIDTYPE_PID, p) {
  2300. set_tsk_trace_trace(p);
  2301. } while_each_pid_task(pid, PIDTYPE_PID, p);
  2302. rcu_read_unlock();
  2303. }
  2304. static void clear_ftrace_pid_task(struct pid **pid)
  2305. {
  2306. if (*pid == ftrace_swapper_pid)
  2307. clear_ftrace_swapper();
  2308. else
  2309. clear_ftrace_pid(*pid);
  2310. *pid = NULL;
  2311. }
  2312. static void set_ftrace_pid_task(struct pid *pid)
  2313. {
  2314. if (pid == ftrace_swapper_pid)
  2315. set_ftrace_swapper();
  2316. else
  2317. set_ftrace_pid(pid);
  2318. }
  2319. static ssize_t
  2320. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  2321. size_t cnt, loff_t *ppos)
  2322. {
  2323. struct pid *pid;
  2324. char buf[64];
  2325. long val;
  2326. int ret;
  2327. if (cnt >= sizeof(buf))
  2328. return -EINVAL;
  2329. if (copy_from_user(&buf, ubuf, cnt))
  2330. return -EFAULT;
  2331. buf[cnt] = 0;
  2332. ret = strict_strtol(buf, 10, &val);
  2333. if (ret < 0)
  2334. return ret;
  2335. mutex_lock(&ftrace_lock);
  2336. if (val < 0) {
  2337. /* disable pid tracing */
  2338. if (!ftrace_pid_trace)
  2339. goto out;
  2340. clear_ftrace_pid_task(&ftrace_pid_trace);
  2341. } else {
  2342. /* swapper task is special */
  2343. if (!val) {
  2344. pid = ftrace_swapper_pid;
  2345. if (pid == ftrace_pid_trace)
  2346. goto out;
  2347. } else {
  2348. pid = find_get_pid(val);
  2349. if (pid == ftrace_pid_trace) {
  2350. put_pid(pid);
  2351. goto out;
  2352. }
  2353. }
  2354. if (ftrace_pid_trace)
  2355. clear_ftrace_pid_task(&ftrace_pid_trace);
  2356. if (!pid)
  2357. goto out;
  2358. ftrace_pid_trace = pid;
  2359. set_ftrace_pid_task(ftrace_pid_trace);
  2360. }
  2361. /* update the function call */
  2362. ftrace_update_pid_func();
  2363. ftrace_startup_enable(0);
  2364. out:
  2365. mutex_unlock(&ftrace_lock);
  2366. return cnt;
  2367. }
  2368. static const struct file_operations ftrace_pid_fops = {
  2369. .read = ftrace_pid_read,
  2370. .write = ftrace_pid_write,
  2371. };
  2372. static __init int ftrace_init_debugfs(void)
  2373. {
  2374. struct dentry *d_tracer;
  2375. d_tracer = tracing_init_dentry();
  2376. if (!d_tracer)
  2377. return 0;
  2378. ftrace_init_dyn_debugfs(d_tracer);
  2379. trace_create_file("set_ftrace_pid", 0644, d_tracer,
  2380. NULL, &ftrace_pid_fops);
  2381. ftrace_profile_debugfs(d_tracer);
  2382. return 0;
  2383. }
  2384. fs_initcall(ftrace_init_debugfs);
  2385. /**
  2386. * ftrace_kill - kill ftrace
  2387. *
  2388. * This function should be used by panic code. It stops ftrace
  2389. * but in a not so nice way. If you need to simply kill ftrace
  2390. * from a non-atomic section, use ftrace_kill.
  2391. */
  2392. void ftrace_kill(void)
  2393. {
  2394. ftrace_disabled = 1;
  2395. ftrace_enabled = 0;
  2396. clear_ftrace_function();
  2397. }
  2398. /**
  2399. * register_ftrace_function - register a function for profiling
  2400. * @ops - ops structure that holds the function for profiling.
  2401. *
  2402. * Register a function to be called by all functions in the
  2403. * kernel.
  2404. *
  2405. * Note: @ops->func and all the functions it calls must be labeled
  2406. * with "notrace", otherwise it will go into a
  2407. * recursive loop.
  2408. */
  2409. int register_ftrace_function(struct ftrace_ops *ops)
  2410. {
  2411. int ret;
  2412. if (unlikely(ftrace_disabled))
  2413. return -1;
  2414. mutex_lock(&ftrace_lock);
  2415. ret = __register_ftrace_function(ops);
  2416. ftrace_startup(0);
  2417. mutex_unlock(&ftrace_lock);
  2418. return ret;
  2419. }
  2420. /**
  2421. * unregister_ftrace_function - unregister a function for profiling.
  2422. * @ops - ops structure that holds the function to unregister
  2423. *
  2424. * Unregister a function that was added to be called by ftrace profiling.
  2425. */
  2426. int unregister_ftrace_function(struct ftrace_ops *ops)
  2427. {
  2428. int ret;
  2429. mutex_lock(&ftrace_lock);
  2430. ret = __unregister_ftrace_function(ops);
  2431. ftrace_shutdown(0);
  2432. mutex_unlock(&ftrace_lock);
  2433. return ret;
  2434. }
  2435. int
  2436. ftrace_enable_sysctl(struct ctl_table *table, int write,
  2437. void __user *buffer, size_t *lenp,
  2438. loff_t *ppos)
  2439. {
  2440. int ret;
  2441. if (unlikely(ftrace_disabled))
  2442. return -ENODEV;
  2443. mutex_lock(&ftrace_lock);
  2444. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  2445. if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
  2446. goto out;
  2447. last_ftrace_enabled = !!ftrace_enabled;
  2448. if (ftrace_enabled) {
  2449. ftrace_startup_sysctl();
  2450. /* we are starting ftrace again */
  2451. if (ftrace_list != &ftrace_list_end) {
  2452. if (ftrace_list->next == &ftrace_list_end)
  2453. ftrace_trace_function = ftrace_list->func;
  2454. else
  2455. ftrace_trace_function = ftrace_list_func;
  2456. }
  2457. } else {
  2458. /* stopping ftrace calls (just send to ftrace_stub) */
  2459. ftrace_trace_function = ftrace_stub;
  2460. ftrace_shutdown_sysctl();
  2461. }
  2462. out:
  2463. mutex_unlock(&ftrace_lock);
  2464. return ret;
  2465. }
  2466. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2467. static int ftrace_graph_active;
  2468. static struct notifier_block ftrace_suspend_notifier;
  2469. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
  2470. {
  2471. return 0;
  2472. }
  2473. /* The callbacks that hook a function */
  2474. trace_func_graph_ret_t ftrace_graph_return =
  2475. (trace_func_graph_ret_t)ftrace_stub;
  2476. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  2477. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  2478. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  2479. {
  2480. int i;
  2481. int ret = 0;
  2482. unsigned long flags;
  2483. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  2484. struct task_struct *g, *t;
  2485. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  2486. ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
  2487. * sizeof(struct ftrace_ret_stack),
  2488. GFP_KERNEL);
  2489. if (!ret_stack_list[i]) {
  2490. start = 0;
  2491. end = i;
  2492. ret = -ENOMEM;
  2493. goto free;
  2494. }
  2495. }
  2496. read_lock_irqsave(&tasklist_lock, flags);
  2497. do_each_thread(g, t) {
  2498. if (start == end) {
  2499. ret = -EAGAIN;
  2500. goto unlock;
  2501. }
  2502. if (t->ret_stack == NULL) {
  2503. atomic_set(&t->tracing_graph_pause, 0);
  2504. atomic_set(&t->trace_overrun, 0);
  2505. t->curr_ret_stack = -1;
  2506. /* Make sure the tasks see the -1 first: */
  2507. smp_wmb();
  2508. t->ret_stack = ret_stack_list[start++];
  2509. }
  2510. } while_each_thread(g, t);
  2511. unlock:
  2512. read_unlock_irqrestore(&tasklist_lock, flags);
  2513. free:
  2514. for (i = start; i < end; i++)
  2515. kfree(ret_stack_list[i]);
  2516. return ret;
  2517. }
  2518. static void
  2519. ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
  2520. struct task_struct *next)
  2521. {
  2522. unsigned long long timestamp;
  2523. int index;
  2524. /*
  2525. * Does the user want to count the time a function was asleep.
  2526. * If so, do not update the time stamps.
  2527. */
  2528. if (trace_flags & TRACE_ITER_SLEEP_TIME)
  2529. return;
  2530. timestamp = trace_clock_local();
  2531. prev->ftrace_timestamp = timestamp;
  2532. /* only process tasks that we timestamped */
  2533. if (!next->ftrace_timestamp)
  2534. return;
  2535. /*
  2536. * Update all the counters in next to make up for the
  2537. * time next was sleeping.
  2538. */
  2539. timestamp -= next->ftrace_timestamp;
  2540. for (index = next->curr_ret_stack; index >= 0; index--)
  2541. next->ret_stack[index].calltime += timestamp;
  2542. }
  2543. /* Allocate a return stack for each task */
  2544. static int start_graph_tracing(void)
  2545. {
  2546. struct ftrace_ret_stack **ret_stack_list;
  2547. int ret, cpu;
  2548. ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
  2549. sizeof(struct ftrace_ret_stack *),
  2550. GFP_KERNEL);
  2551. if (!ret_stack_list)
  2552. return -ENOMEM;
  2553. /* The cpu_boot init_task->ret_stack will never be freed */
  2554. for_each_online_cpu(cpu) {
  2555. if (!idle_task(cpu)->ret_stack)
  2556. ftrace_graph_init_task(idle_task(cpu));
  2557. }
  2558. do {
  2559. ret = alloc_retstack_tasklist(ret_stack_list);
  2560. } while (ret == -EAGAIN);
  2561. if (!ret) {
  2562. ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
  2563. if (ret)
  2564. pr_info("ftrace_graph: Couldn't activate tracepoint"
  2565. " probe to kernel_sched_switch\n");
  2566. }
  2567. kfree(ret_stack_list);
  2568. return ret;
  2569. }
  2570. /*
  2571. * Hibernation protection.
  2572. * The state of the current task is too much unstable during
  2573. * suspend/restore to disk. We want to protect against that.
  2574. */
  2575. static int
  2576. ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
  2577. void *unused)
  2578. {
  2579. switch (state) {
  2580. case PM_HIBERNATION_PREPARE:
  2581. pause_graph_tracing();
  2582. break;
  2583. case PM_POST_HIBERNATION:
  2584. unpause_graph_tracing();
  2585. break;
  2586. }
  2587. return NOTIFY_DONE;
  2588. }
  2589. int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  2590. trace_func_graph_ent_t entryfunc)
  2591. {
  2592. int ret = 0;
  2593. mutex_lock(&ftrace_lock);
  2594. /* we currently allow only one tracer registered at a time */
  2595. if (ftrace_graph_active) {
  2596. ret = -EBUSY;
  2597. goto out;
  2598. }
  2599. ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
  2600. register_pm_notifier(&ftrace_suspend_notifier);
  2601. ftrace_graph_active++;
  2602. ret = start_graph_tracing();
  2603. if (ret) {
  2604. ftrace_graph_active--;
  2605. goto out;
  2606. }
  2607. ftrace_graph_return = retfunc;
  2608. ftrace_graph_entry = entryfunc;
  2609. ftrace_startup(FTRACE_START_FUNC_RET);
  2610. out:
  2611. mutex_unlock(&ftrace_lock);
  2612. return ret;
  2613. }
  2614. void unregister_ftrace_graph(void)
  2615. {
  2616. mutex_lock(&ftrace_lock);
  2617. if (unlikely(!ftrace_graph_active))
  2618. goto out;
  2619. ftrace_graph_active--;
  2620. unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
  2621. ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
  2622. ftrace_graph_entry = ftrace_graph_entry_stub;
  2623. ftrace_shutdown(FTRACE_STOP_FUNC_RET);
  2624. unregister_pm_notifier(&ftrace_suspend_notifier);
  2625. out:
  2626. mutex_unlock(&ftrace_lock);
  2627. }
  2628. /* Allocate a return stack for newly created task */
  2629. void ftrace_graph_init_task(struct task_struct *t)
  2630. {
  2631. /* Make sure we do not use the parent ret_stack */
  2632. t->ret_stack = NULL;
  2633. if (ftrace_graph_active) {
  2634. struct ftrace_ret_stack *ret_stack;
  2635. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  2636. * sizeof(struct ftrace_ret_stack),
  2637. GFP_KERNEL);
  2638. if (!ret_stack)
  2639. return;
  2640. t->curr_ret_stack = -1;
  2641. atomic_set(&t->tracing_graph_pause, 0);
  2642. atomic_set(&t->trace_overrun, 0);
  2643. t->ftrace_timestamp = 0;
  2644. /* make curr_ret_stack visable before we add the ret_stack */
  2645. smp_wmb();
  2646. t->ret_stack = ret_stack;
  2647. }
  2648. }
  2649. void ftrace_graph_exit_task(struct task_struct *t)
  2650. {
  2651. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  2652. t->ret_stack = NULL;
  2653. /* NULL must become visible to IRQs before we free it: */
  2654. barrier();
  2655. kfree(ret_stack);
  2656. }
  2657. void ftrace_graph_stop(void)
  2658. {
  2659. ftrace_stop();
  2660. }
  2661. #endif