ftrace.c 47 KB

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