ftrace.c 69 KB

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