ftrace.c 63 KB

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