ftrace.c 68 KB

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