ftrace.c 57 KB

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