ftrace.c 48 KB

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