ftrace.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  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/debugfs.h>
  20. #include <linux/hardirq.h>
  21. #include <linux/kthread.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/kprobes.h>
  24. #include <linux/ftrace.h>
  25. #include <linux/sysctl.h>
  26. #include <linux/ctype.h>
  27. #include <linux/list.h>
  28. #include <asm/ftrace.h>
  29. #include "trace.h"
  30. #define FTRACE_WARN_ON(cond) \
  31. do { \
  32. if (WARN_ON(cond)) \
  33. ftrace_kill(); \
  34. } while (0)
  35. #define FTRACE_WARN_ON_ONCE(cond) \
  36. do { \
  37. if (WARN_ON_ONCE(cond)) \
  38. ftrace_kill(); \
  39. } while (0)
  40. /* ftrace_enabled is a method to turn ftrace on or off */
  41. int ftrace_enabled __read_mostly;
  42. static int last_ftrace_enabled;
  43. /* ftrace_pid_trace >= 0 will only trace threads with this pid */
  44. static int ftrace_pid_trace = -1;
  45. /* Quick disabling of function tracer. */
  46. int function_trace_stop;
  47. /*
  48. * ftrace_disabled is set when an anomaly is discovered.
  49. * ftrace_disabled is much stronger than ftrace_enabled.
  50. */
  51. static int ftrace_disabled __read_mostly;
  52. static DEFINE_SPINLOCK(ftrace_lock);
  53. static DEFINE_MUTEX(ftrace_sysctl_lock);
  54. static DEFINE_MUTEX(ftrace_start_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 (current->pid != ftrace_pid_trace)
  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. /* should not be called from interrupt context */
  114. spin_lock(&ftrace_lock);
  115. ops->next = ftrace_list;
  116. /*
  117. * We are entering ops into the ftrace_list but another
  118. * CPU might be walking that list. We need to make sure
  119. * the ops->next pointer is valid before another CPU sees
  120. * the ops pointer included into the ftrace_list.
  121. */
  122. smp_wmb();
  123. ftrace_list = ops;
  124. if (ftrace_enabled) {
  125. ftrace_func_t func;
  126. if (ops->next == &ftrace_list_end)
  127. func = ops->func;
  128. else
  129. func = ftrace_list_func;
  130. if (ftrace_pid_trace >= 0) {
  131. set_ftrace_pid_function(func);
  132. func = ftrace_pid_func;
  133. }
  134. /*
  135. * For one func, simply call it directly.
  136. * For more than one func, call the chain.
  137. */
  138. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  139. ftrace_trace_function = func;
  140. #else
  141. __ftrace_trace_function = func;
  142. ftrace_trace_function = ftrace_test_stop_func;
  143. #endif
  144. }
  145. spin_unlock(&ftrace_lock);
  146. return 0;
  147. }
  148. static int __unregister_ftrace_function(struct ftrace_ops *ops)
  149. {
  150. struct ftrace_ops **p;
  151. int ret = 0;
  152. /* should not be called from interrupt context */
  153. spin_lock(&ftrace_lock);
  154. /*
  155. * If we are removing the last function, then simply point
  156. * to the ftrace_stub.
  157. */
  158. if (ftrace_list == ops && ops->next == &ftrace_list_end) {
  159. ftrace_trace_function = ftrace_stub;
  160. ftrace_list = &ftrace_list_end;
  161. goto out;
  162. }
  163. for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
  164. if (*p == ops)
  165. break;
  166. if (*p != ops) {
  167. ret = -1;
  168. goto out;
  169. }
  170. *p = (*p)->next;
  171. if (ftrace_enabled) {
  172. /* If we only have one func left, then call that directly */
  173. if (ftrace_list->next == &ftrace_list_end) {
  174. ftrace_func_t func = ftrace_list->func;
  175. if (ftrace_pid_trace >= 0) {
  176. set_ftrace_pid_function(func);
  177. func = ftrace_pid_func;
  178. }
  179. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  180. ftrace_trace_function = func;
  181. #else
  182. __ftrace_trace_function = func;
  183. #endif
  184. }
  185. }
  186. out:
  187. spin_unlock(&ftrace_lock);
  188. return ret;
  189. }
  190. static void ftrace_update_pid_func(void)
  191. {
  192. ftrace_func_t func;
  193. /* should not be called from interrupt context */
  194. spin_lock(&ftrace_lock);
  195. if (ftrace_trace_function == ftrace_stub)
  196. goto out;
  197. func = ftrace_trace_function;
  198. if (ftrace_pid_trace >= 0) {
  199. set_ftrace_pid_function(func);
  200. func = ftrace_pid_func;
  201. } else {
  202. if (func != ftrace_pid_func)
  203. goto out;
  204. set_ftrace_pid_function(func);
  205. }
  206. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  207. ftrace_trace_function = func;
  208. #else
  209. __ftrace_trace_function = func;
  210. #endif
  211. out:
  212. spin_unlock(&ftrace_lock);
  213. }
  214. #ifdef CONFIG_DYNAMIC_FTRACE
  215. #ifndef CONFIG_FTRACE_MCOUNT_RECORD
  216. # error Dynamic ftrace depends on MCOUNT_RECORD
  217. #endif
  218. /*
  219. * Since MCOUNT_ADDR may point to mcount itself, we do not want
  220. * to get it confused by reading a reference in the code as we
  221. * are parsing on objcopy output of text. Use a variable for
  222. * it instead.
  223. */
  224. static unsigned long mcount_addr = MCOUNT_ADDR;
  225. enum {
  226. FTRACE_ENABLE_CALLS = (1 << 0),
  227. FTRACE_DISABLE_CALLS = (1 << 1),
  228. FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
  229. FTRACE_ENABLE_MCOUNT = (1 << 3),
  230. FTRACE_DISABLE_MCOUNT = (1 << 4),
  231. FTRACE_START_FUNC_RET = (1 << 5),
  232. FTRACE_STOP_FUNC_RET = (1 << 6),
  233. };
  234. static int ftrace_filtered;
  235. static LIST_HEAD(ftrace_new_addrs);
  236. static DEFINE_MUTEX(ftrace_regex_lock);
  237. struct ftrace_page {
  238. struct ftrace_page *next;
  239. unsigned long index;
  240. struct dyn_ftrace records[];
  241. };
  242. #define ENTRIES_PER_PAGE \
  243. ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
  244. /* estimate from running different kernels */
  245. #define NR_TO_INIT 10000
  246. static struct ftrace_page *ftrace_pages_start;
  247. static struct ftrace_page *ftrace_pages;
  248. static struct dyn_ftrace *ftrace_free_records;
  249. #ifdef CONFIG_KPROBES
  250. static int frozen_record_count;
  251. static inline void freeze_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 void unfreeze_record(struct dyn_ftrace *rec)
  259. {
  260. if (rec->flags & FTRACE_FL_FROZEN) {
  261. rec->flags &= ~FTRACE_FL_FROZEN;
  262. frozen_record_count--;
  263. }
  264. }
  265. static inline int record_frozen(struct dyn_ftrace *rec)
  266. {
  267. return rec->flags & FTRACE_FL_FROZEN;
  268. }
  269. #else
  270. # define freeze_record(rec) ({ 0; })
  271. # define unfreeze_record(rec) ({ 0; })
  272. # define record_frozen(rec) ({ 0; })
  273. #endif /* CONFIG_KPROBES */
  274. static void ftrace_free_rec(struct dyn_ftrace *rec)
  275. {
  276. rec->ip = (unsigned long)ftrace_free_records;
  277. ftrace_free_records = rec;
  278. rec->flags |= FTRACE_FL_FREE;
  279. }
  280. void ftrace_release(void *start, unsigned long size)
  281. {
  282. struct dyn_ftrace *rec;
  283. struct ftrace_page *pg;
  284. unsigned long s = (unsigned long)start;
  285. unsigned long e = s + size;
  286. int i;
  287. if (ftrace_disabled || !start)
  288. return;
  289. /* should not be called from interrupt context */
  290. spin_lock(&ftrace_lock);
  291. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  292. for (i = 0; i < pg->index; i++) {
  293. rec = &pg->records[i];
  294. if ((rec->ip >= s) && (rec->ip < e))
  295. ftrace_free_rec(rec);
  296. }
  297. }
  298. spin_unlock(&ftrace_lock);
  299. }
  300. static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
  301. {
  302. struct dyn_ftrace *rec;
  303. /* First check for freed records */
  304. if (ftrace_free_records) {
  305. rec = ftrace_free_records;
  306. if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
  307. FTRACE_WARN_ON_ONCE(1);
  308. ftrace_free_records = NULL;
  309. return NULL;
  310. }
  311. ftrace_free_records = (void *)rec->ip;
  312. memset(rec, 0, sizeof(*rec));
  313. return rec;
  314. }
  315. if (ftrace_pages->index == ENTRIES_PER_PAGE) {
  316. if (!ftrace_pages->next) {
  317. /* allocate another page */
  318. ftrace_pages->next =
  319. (void *)get_zeroed_page(GFP_KERNEL);
  320. if (!ftrace_pages->next)
  321. return NULL;
  322. }
  323. ftrace_pages = ftrace_pages->next;
  324. }
  325. return &ftrace_pages->records[ftrace_pages->index++];
  326. }
  327. static struct dyn_ftrace *
  328. ftrace_record_ip(unsigned long ip)
  329. {
  330. struct dyn_ftrace *rec;
  331. if (ftrace_disabled)
  332. return NULL;
  333. rec = ftrace_alloc_dyn_node(ip);
  334. if (!rec)
  335. return NULL;
  336. rec->ip = ip;
  337. list_add(&rec->list, &ftrace_new_addrs);
  338. return rec;
  339. }
  340. static void print_ip_ins(const char *fmt, unsigned char *p)
  341. {
  342. int i;
  343. printk(KERN_CONT "%s", fmt);
  344. for (i = 0; i < MCOUNT_INSN_SIZE; i++)
  345. printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
  346. }
  347. static void ftrace_bug(int failed, unsigned long ip)
  348. {
  349. switch (failed) {
  350. case -EFAULT:
  351. FTRACE_WARN_ON_ONCE(1);
  352. pr_info("ftrace faulted on modifying ");
  353. print_ip_sym(ip);
  354. break;
  355. case -EINVAL:
  356. FTRACE_WARN_ON_ONCE(1);
  357. pr_info("ftrace failed to modify ");
  358. print_ip_sym(ip);
  359. print_ip_ins(" actual: ", (unsigned char *)ip);
  360. printk(KERN_CONT "\n");
  361. break;
  362. case -EPERM:
  363. FTRACE_WARN_ON_ONCE(1);
  364. pr_info("ftrace faulted on writing ");
  365. print_ip_sym(ip);
  366. break;
  367. default:
  368. FTRACE_WARN_ON_ONCE(1);
  369. pr_info("ftrace faulted on unknown error ");
  370. print_ip_sym(ip);
  371. }
  372. }
  373. static int
  374. __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
  375. {
  376. unsigned long ip, fl;
  377. unsigned long ftrace_addr;
  378. ftrace_addr = (unsigned long)ftrace_caller;
  379. ip = rec->ip;
  380. /*
  381. * If this record is not to be traced and
  382. * it is not enabled then do nothing.
  383. *
  384. * If this record is not to be traced and
  385. * it is enabled then disabled it.
  386. *
  387. */
  388. if (rec->flags & FTRACE_FL_NOTRACE) {
  389. if (rec->flags & FTRACE_FL_ENABLED)
  390. rec->flags &= ~FTRACE_FL_ENABLED;
  391. else
  392. return 0;
  393. } else if (ftrace_filtered && enable) {
  394. /*
  395. * Filtering is on:
  396. */
  397. fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
  398. /* Record is filtered and enabled, do nothing */
  399. if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
  400. return 0;
  401. /* Record is not filtered and is not enabled do nothing */
  402. if (!fl)
  403. return 0;
  404. /* Record is not filtered but enabled, disable it */
  405. if (fl == FTRACE_FL_ENABLED)
  406. rec->flags &= ~FTRACE_FL_ENABLED;
  407. else
  408. /* Otherwise record is filtered but not enabled, enable it */
  409. rec->flags |= FTRACE_FL_ENABLED;
  410. } else {
  411. /* Disable or not filtered */
  412. if (enable) {
  413. /* if record is enabled, do nothing */
  414. if (rec->flags & FTRACE_FL_ENABLED)
  415. return 0;
  416. rec->flags |= FTRACE_FL_ENABLED;
  417. } else {
  418. /* if record is not enabled do nothing */
  419. if (!(rec->flags & FTRACE_FL_ENABLED))
  420. return 0;
  421. rec->flags &= ~FTRACE_FL_ENABLED;
  422. }
  423. }
  424. if (rec->flags & FTRACE_FL_ENABLED)
  425. return ftrace_make_call(rec, ftrace_addr);
  426. else
  427. return ftrace_make_nop(NULL, rec, ftrace_addr);
  428. }
  429. static void ftrace_replace_code(int enable)
  430. {
  431. int i, failed;
  432. struct dyn_ftrace *rec;
  433. struct ftrace_page *pg;
  434. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  435. for (i = 0; i < pg->index; i++) {
  436. rec = &pg->records[i];
  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. }
  461. }
  462. }
  463. static int
  464. ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
  465. {
  466. unsigned long ip;
  467. int ret;
  468. ip = rec->ip;
  469. ret = ftrace_make_nop(mod, rec, mcount_addr);
  470. if (ret) {
  471. ftrace_bug(ret, ip);
  472. rec->flags |= FTRACE_FL_FAILED;
  473. return 0;
  474. }
  475. return 1;
  476. }
  477. static int __ftrace_modify_code(void *data)
  478. {
  479. int *command = data;
  480. if (*command & FTRACE_ENABLE_CALLS)
  481. ftrace_replace_code(1);
  482. else if (*command & FTRACE_DISABLE_CALLS)
  483. ftrace_replace_code(0);
  484. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  485. ftrace_update_ftrace_func(ftrace_trace_function);
  486. if (*command & FTRACE_START_FUNC_RET)
  487. ftrace_enable_ftrace_graph_caller();
  488. else if (*command & FTRACE_STOP_FUNC_RET)
  489. ftrace_disable_ftrace_graph_caller();
  490. return 0;
  491. }
  492. static void ftrace_run_update_code(int command)
  493. {
  494. stop_machine(__ftrace_modify_code, &command, NULL);
  495. }
  496. static ftrace_func_t saved_ftrace_func;
  497. static int ftrace_start_up;
  498. static void ftrace_startup_enable(int command)
  499. {
  500. if (saved_ftrace_func != ftrace_trace_function) {
  501. saved_ftrace_func = ftrace_trace_function;
  502. command |= FTRACE_UPDATE_TRACE_FUNC;
  503. }
  504. if (!command || !ftrace_enabled)
  505. return;
  506. ftrace_run_update_code(command);
  507. }
  508. static void ftrace_startup(int command)
  509. {
  510. if (unlikely(ftrace_disabled))
  511. return;
  512. mutex_lock(&ftrace_start_lock);
  513. ftrace_start_up++;
  514. command |= FTRACE_ENABLE_CALLS;
  515. ftrace_startup_enable(command);
  516. mutex_unlock(&ftrace_start_lock);
  517. }
  518. static void ftrace_shutdown(int command)
  519. {
  520. if (unlikely(ftrace_disabled))
  521. return;
  522. mutex_lock(&ftrace_start_lock);
  523. ftrace_start_up--;
  524. if (!ftrace_start_up)
  525. command |= FTRACE_DISABLE_CALLS;
  526. if (saved_ftrace_func != ftrace_trace_function) {
  527. saved_ftrace_func = ftrace_trace_function;
  528. command |= FTRACE_UPDATE_TRACE_FUNC;
  529. }
  530. if (!command || !ftrace_enabled)
  531. goto out;
  532. ftrace_run_update_code(command);
  533. out:
  534. mutex_unlock(&ftrace_start_lock);
  535. }
  536. static void ftrace_startup_sysctl(void)
  537. {
  538. int command = FTRACE_ENABLE_MCOUNT;
  539. if (unlikely(ftrace_disabled))
  540. return;
  541. mutex_lock(&ftrace_start_lock);
  542. /* Force update next time */
  543. saved_ftrace_func = NULL;
  544. /* ftrace_start_up is true if we want ftrace running */
  545. if (ftrace_start_up)
  546. command |= FTRACE_ENABLE_CALLS;
  547. ftrace_run_update_code(command);
  548. mutex_unlock(&ftrace_start_lock);
  549. }
  550. static void ftrace_shutdown_sysctl(void)
  551. {
  552. int command = FTRACE_DISABLE_MCOUNT;
  553. if (unlikely(ftrace_disabled))
  554. return;
  555. mutex_lock(&ftrace_start_lock);
  556. /* ftrace_start_up is true if ftrace is running */
  557. if (ftrace_start_up)
  558. command |= FTRACE_DISABLE_CALLS;
  559. ftrace_run_update_code(command);
  560. mutex_unlock(&ftrace_start_lock);
  561. }
  562. static cycle_t ftrace_update_time;
  563. static unsigned long ftrace_update_cnt;
  564. unsigned long ftrace_update_tot_cnt;
  565. static int ftrace_update_code(struct module *mod)
  566. {
  567. struct dyn_ftrace *p, *t;
  568. cycle_t start, stop;
  569. start = ftrace_now(raw_smp_processor_id());
  570. ftrace_update_cnt = 0;
  571. list_for_each_entry_safe(p, t, &ftrace_new_addrs, list) {
  572. /* If something went wrong, bail without enabling anything */
  573. if (unlikely(ftrace_disabled))
  574. return -1;
  575. list_del_init(&p->list);
  576. /* convert record (i.e, patch mcount-call with NOP) */
  577. if (ftrace_code_disable(mod, p)) {
  578. p->flags |= FTRACE_FL_CONVERTED;
  579. ftrace_update_cnt++;
  580. } else
  581. ftrace_free_rec(p);
  582. }
  583. stop = ftrace_now(raw_smp_processor_id());
  584. ftrace_update_time = stop - start;
  585. ftrace_update_tot_cnt += ftrace_update_cnt;
  586. return 0;
  587. }
  588. static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
  589. {
  590. struct ftrace_page *pg;
  591. int cnt;
  592. int i;
  593. /* allocate a few pages */
  594. ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
  595. if (!ftrace_pages_start)
  596. return -1;
  597. /*
  598. * Allocate a few more pages.
  599. *
  600. * TODO: have some parser search vmlinux before
  601. * final linking to find all calls to ftrace.
  602. * Then we can:
  603. * a) know how many pages to allocate.
  604. * and/or
  605. * b) set up the table then.
  606. *
  607. * The dynamic code is still necessary for
  608. * modules.
  609. */
  610. pg = ftrace_pages = ftrace_pages_start;
  611. cnt = num_to_init / ENTRIES_PER_PAGE;
  612. pr_info("ftrace: allocating %ld entries in %d pages\n",
  613. num_to_init, cnt + 1);
  614. for (i = 0; i < cnt; i++) {
  615. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  616. /* If we fail, we'll try later anyway */
  617. if (!pg->next)
  618. break;
  619. pg = pg->next;
  620. }
  621. return 0;
  622. }
  623. enum {
  624. FTRACE_ITER_FILTER = (1 << 0),
  625. FTRACE_ITER_CONT = (1 << 1),
  626. FTRACE_ITER_NOTRACE = (1 << 2),
  627. FTRACE_ITER_FAILURES = (1 << 3),
  628. };
  629. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  630. struct ftrace_iterator {
  631. struct ftrace_page *pg;
  632. unsigned 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. /* should not be called from interrupt context */
  645. spin_lock(&ftrace_lock);
  646. retry:
  647. if (iter->idx >= iter->pg->index) {
  648. if (iter->pg->next) {
  649. iter->pg = iter->pg->next;
  650. iter->idx = 0;
  651. goto retry;
  652. } else {
  653. iter->idx = -1;
  654. }
  655. } else {
  656. rec = &iter->pg->records[iter->idx++];
  657. if ((rec->flags & FTRACE_FL_FREE) ||
  658. (!(iter->flags & FTRACE_ITER_FAILURES) &&
  659. (rec->flags & FTRACE_FL_FAILED)) ||
  660. ((iter->flags & FTRACE_ITER_FAILURES) &&
  661. !(rec->flags & FTRACE_FL_FAILED)) ||
  662. ((iter->flags & FTRACE_ITER_FILTER) &&
  663. !(rec->flags & FTRACE_FL_FILTER)) ||
  664. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  665. !(rec->flags & FTRACE_FL_NOTRACE))) {
  666. rec = NULL;
  667. goto retry;
  668. }
  669. }
  670. spin_unlock(&ftrace_lock);
  671. return rec;
  672. }
  673. static void *t_start(struct seq_file *m, loff_t *pos)
  674. {
  675. struct ftrace_iterator *iter = m->private;
  676. void *p = NULL;
  677. if (*pos > 0) {
  678. if (iter->idx < 0)
  679. return p;
  680. (*pos)--;
  681. iter->idx--;
  682. }
  683. p = t_next(m, p, pos);
  684. return p;
  685. }
  686. static void t_stop(struct seq_file *m, void *p)
  687. {
  688. }
  689. static int t_show(struct seq_file *m, void *v)
  690. {
  691. struct dyn_ftrace *rec = v;
  692. char str[KSYM_SYMBOL_LEN];
  693. if (!rec)
  694. return 0;
  695. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  696. seq_printf(m, "%s\n", str);
  697. return 0;
  698. }
  699. static struct seq_operations show_ftrace_seq_ops = {
  700. .start = t_start,
  701. .next = t_next,
  702. .stop = t_stop,
  703. .show = t_show,
  704. };
  705. static int
  706. ftrace_avail_open(struct inode *inode, struct file *file)
  707. {
  708. struct ftrace_iterator *iter;
  709. int ret;
  710. if (unlikely(ftrace_disabled))
  711. return -ENODEV;
  712. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  713. if (!iter)
  714. return -ENOMEM;
  715. iter->pg = ftrace_pages_start;
  716. ret = seq_open(file, &show_ftrace_seq_ops);
  717. if (!ret) {
  718. struct seq_file *m = file->private_data;
  719. m->private = iter;
  720. } else {
  721. kfree(iter);
  722. }
  723. return ret;
  724. }
  725. int ftrace_avail_release(struct inode *inode, struct file *file)
  726. {
  727. struct seq_file *m = (struct seq_file *)file->private_data;
  728. struct ftrace_iterator *iter = m->private;
  729. seq_release(inode, file);
  730. kfree(iter);
  731. return 0;
  732. }
  733. static int
  734. ftrace_failures_open(struct inode *inode, struct file *file)
  735. {
  736. int ret;
  737. struct seq_file *m;
  738. struct ftrace_iterator *iter;
  739. ret = ftrace_avail_open(inode, file);
  740. if (!ret) {
  741. m = (struct seq_file *)file->private_data;
  742. iter = (struct ftrace_iterator *)m->private;
  743. iter->flags = FTRACE_ITER_FAILURES;
  744. }
  745. return ret;
  746. }
  747. static void ftrace_filter_reset(int enable)
  748. {
  749. struct ftrace_page *pg;
  750. struct dyn_ftrace *rec;
  751. unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  752. unsigned i;
  753. /* should not be called from interrupt context */
  754. spin_lock(&ftrace_lock);
  755. if (enable)
  756. ftrace_filtered = 0;
  757. pg = ftrace_pages_start;
  758. while (pg) {
  759. for (i = 0; i < pg->index; i++) {
  760. rec = &pg->records[i];
  761. if (rec->flags & FTRACE_FL_FAILED)
  762. continue;
  763. rec->flags &= ~type;
  764. }
  765. pg = pg->next;
  766. }
  767. spin_unlock(&ftrace_lock);
  768. }
  769. static int
  770. ftrace_regex_open(struct inode *inode, struct file *file, int enable)
  771. {
  772. struct ftrace_iterator *iter;
  773. int ret = 0;
  774. if (unlikely(ftrace_disabled))
  775. return -ENODEV;
  776. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  777. if (!iter)
  778. return -ENOMEM;
  779. mutex_lock(&ftrace_regex_lock);
  780. if ((file->f_mode & FMODE_WRITE) &&
  781. !(file->f_flags & O_APPEND))
  782. ftrace_filter_reset(enable);
  783. if (file->f_mode & FMODE_READ) {
  784. iter->pg = ftrace_pages_start;
  785. iter->flags = enable ? FTRACE_ITER_FILTER :
  786. FTRACE_ITER_NOTRACE;
  787. ret = seq_open(file, &show_ftrace_seq_ops);
  788. if (!ret) {
  789. struct seq_file *m = file->private_data;
  790. m->private = iter;
  791. } else
  792. kfree(iter);
  793. } else
  794. file->private_data = iter;
  795. mutex_unlock(&ftrace_regex_lock);
  796. return ret;
  797. }
  798. static int
  799. ftrace_filter_open(struct inode *inode, struct file *file)
  800. {
  801. return ftrace_regex_open(inode, file, 1);
  802. }
  803. static int
  804. ftrace_notrace_open(struct inode *inode, struct file *file)
  805. {
  806. return ftrace_regex_open(inode, file, 0);
  807. }
  808. static ssize_t
  809. ftrace_regex_read(struct file *file, char __user *ubuf,
  810. size_t cnt, loff_t *ppos)
  811. {
  812. if (file->f_mode & FMODE_READ)
  813. return seq_read(file, ubuf, cnt, ppos);
  814. else
  815. return -EPERM;
  816. }
  817. static loff_t
  818. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  819. {
  820. loff_t ret;
  821. if (file->f_mode & FMODE_READ)
  822. ret = seq_lseek(file, offset, origin);
  823. else
  824. file->f_pos = ret = 1;
  825. return ret;
  826. }
  827. enum {
  828. MATCH_FULL,
  829. MATCH_FRONT_ONLY,
  830. MATCH_MIDDLE_ONLY,
  831. MATCH_END_ONLY,
  832. };
  833. static void
  834. ftrace_match(unsigned char *buff, int len, int enable)
  835. {
  836. char str[KSYM_SYMBOL_LEN];
  837. char *search = NULL;
  838. struct ftrace_page *pg;
  839. struct dyn_ftrace *rec;
  840. int type = MATCH_FULL;
  841. unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  842. unsigned i, match = 0, search_len = 0;
  843. for (i = 0; i < len; i++) {
  844. if (buff[i] == '*') {
  845. if (!i) {
  846. search = buff + i + 1;
  847. type = MATCH_END_ONLY;
  848. search_len = len - (i + 1);
  849. } else {
  850. if (type == MATCH_END_ONLY) {
  851. type = MATCH_MIDDLE_ONLY;
  852. } else {
  853. match = i;
  854. type = MATCH_FRONT_ONLY;
  855. }
  856. buff[i] = 0;
  857. break;
  858. }
  859. }
  860. }
  861. /* should not be called from interrupt context */
  862. spin_lock(&ftrace_lock);
  863. if (enable)
  864. ftrace_filtered = 1;
  865. pg = ftrace_pages_start;
  866. while (pg) {
  867. for (i = 0; i < pg->index; i++) {
  868. int matched = 0;
  869. char *ptr;
  870. rec = &pg->records[i];
  871. if (rec->flags & FTRACE_FL_FAILED)
  872. continue;
  873. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  874. switch (type) {
  875. case MATCH_FULL:
  876. if (strcmp(str, buff) == 0)
  877. matched = 1;
  878. break;
  879. case MATCH_FRONT_ONLY:
  880. if (memcmp(str, buff, match) == 0)
  881. matched = 1;
  882. break;
  883. case MATCH_MIDDLE_ONLY:
  884. if (strstr(str, search))
  885. matched = 1;
  886. break;
  887. case MATCH_END_ONLY:
  888. ptr = strstr(str, search);
  889. if (ptr && (ptr[search_len] == 0))
  890. matched = 1;
  891. break;
  892. }
  893. if (matched)
  894. rec->flags |= flag;
  895. }
  896. pg = pg->next;
  897. }
  898. spin_unlock(&ftrace_lock);
  899. }
  900. static ssize_t
  901. ftrace_regex_write(struct file *file, const char __user *ubuf,
  902. size_t cnt, loff_t *ppos, int enable)
  903. {
  904. struct ftrace_iterator *iter;
  905. char ch;
  906. size_t read = 0;
  907. ssize_t ret;
  908. if (!cnt || cnt < 0)
  909. return 0;
  910. mutex_lock(&ftrace_regex_lock);
  911. if (file->f_mode & FMODE_READ) {
  912. struct seq_file *m = file->private_data;
  913. iter = m->private;
  914. } else
  915. iter = file->private_data;
  916. if (!*ppos) {
  917. iter->flags &= ~FTRACE_ITER_CONT;
  918. iter->buffer_idx = 0;
  919. }
  920. ret = get_user(ch, ubuf++);
  921. if (ret)
  922. goto out;
  923. read++;
  924. cnt--;
  925. if (!(iter->flags & ~FTRACE_ITER_CONT)) {
  926. /* skip white space */
  927. while (cnt && isspace(ch)) {
  928. ret = get_user(ch, ubuf++);
  929. if (ret)
  930. goto out;
  931. read++;
  932. cnt--;
  933. }
  934. if (isspace(ch)) {
  935. file->f_pos += read;
  936. ret = read;
  937. goto out;
  938. }
  939. iter->buffer_idx = 0;
  940. }
  941. while (cnt && !isspace(ch)) {
  942. if (iter->buffer_idx < FTRACE_BUFF_MAX)
  943. iter->buffer[iter->buffer_idx++] = ch;
  944. else {
  945. ret = -EINVAL;
  946. goto out;
  947. }
  948. ret = get_user(ch, ubuf++);
  949. if (ret)
  950. goto out;
  951. read++;
  952. cnt--;
  953. }
  954. if (isspace(ch)) {
  955. iter->filtered++;
  956. iter->buffer[iter->buffer_idx] = 0;
  957. ftrace_match(iter->buffer, iter->buffer_idx, enable);
  958. iter->buffer_idx = 0;
  959. } else
  960. iter->flags |= FTRACE_ITER_CONT;
  961. file->f_pos += read;
  962. ret = read;
  963. out:
  964. mutex_unlock(&ftrace_regex_lock);
  965. return ret;
  966. }
  967. static ssize_t
  968. ftrace_filter_write(struct file *file, const char __user *ubuf,
  969. size_t cnt, loff_t *ppos)
  970. {
  971. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  972. }
  973. static ssize_t
  974. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  975. size_t cnt, loff_t *ppos)
  976. {
  977. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  978. }
  979. static void
  980. ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
  981. {
  982. if (unlikely(ftrace_disabled))
  983. return;
  984. mutex_lock(&ftrace_regex_lock);
  985. if (reset)
  986. ftrace_filter_reset(enable);
  987. if (buf)
  988. ftrace_match(buf, len, enable);
  989. mutex_unlock(&ftrace_regex_lock);
  990. }
  991. /**
  992. * ftrace_set_filter - set a function to filter on in ftrace
  993. * @buf - the string that holds the function filter text.
  994. * @len - the length of the string.
  995. * @reset - non zero to reset all filters before applying this filter.
  996. *
  997. * Filters denote which functions should be enabled when tracing is enabled.
  998. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  999. */
  1000. void ftrace_set_filter(unsigned char *buf, int len, int reset)
  1001. {
  1002. ftrace_set_regex(buf, len, reset, 1);
  1003. }
  1004. /**
  1005. * ftrace_set_notrace - set a function to not trace in ftrace
  1006. * @buf - the string that holds the function notrace text.
  1007. * @len - the length of the string.
  1008. * @reset - non zero to reset all filters before applying this filter.
  1009. *
  1010. * Notrace Filters denote which functions should not be enabled when tracing
  1011. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  1012. * for tracing.
  1013. */
  1014. void ftrace_set_notrace(unsigned char *buf, int len, int reset)
  1015. {
  1016. ftrace_set_regex(buf, len, reset, 0);
  1017. }
  1018. static int
  1019. ftrace_regex_release(struct inode *inode, struct file *file, int enable)
  1020. {
  1021. struct seq_file *m = (struct seq_file *)file->private_data;
  1022. struct ftrace_iterator *iter;
  1023. mutex_lock(&ftrace_regex_lock);
  1024. if (file->f_mode & FMODE_READ) {
  1025. iter = m->private;
  1026. seq_release(inode, file);
  1027. } else
  1028. iter = file->private_data;
  1029. if (iter->buffer_idx) {
  1030. iter->filtered++;
  1031. iter->buffer[iter->buffer_idx] = 0;
  1032. ftrace_match(iter->buffer, iter->buffer_idx, enable);
  1033. }
  1034. mutex_lock(&ftrace_sysctl_lock);
  1035. mutex_lock(&ftrace_start_lock);
  1036. if (ftrace_start_up && ftrace_enabled)
  1037. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  1038. mutex_unlock(&ftrace_start_lock);
  1039. mutex_unlock(&ftrace_sysctl_lock);
  1040. kfree(iter);
  1041. mutex_unlock(&ftrace_regex_lock);
  1042. return 0;
  1043. }
  1044. static int
  1045. ftrace_filter_release(struct inode *inode, struct file *file)
  1046. {
  1047. return ftrace_regex_release(inode, file, 1);
  1048. }
  1049. static int
  1050. ftrace_notrace_release(struct inode *inode, struct file *file)
  1051. {
  1052. return ftrace_regex_release(inode, file, 0);
  1053. }
  1054. static struct file_operations ftrace_avail_fops = {
  1055. .open = ftrace_avail_open,
  1056. .read = seq_read,
  1057. .llseek = seq_lseek,
  1058. .release = ftrace_avail_release,
  1059. };
  1060. static struct file_operations ftrace_failures_fops = {
  1061. .open = ftrace_failures_open,
  1062. .read = seq_read,
  1063. .llseek = seq_lseek,
  1064. .release = ftrace_avail_release,
  1065. };
  1066. static struct file_operations ftrace_filter_fops = {
  1067. .open = ftrace_filter_open,
  1068. .read = ftrace_regex_read,
  1069. .write = ftrace_filter_write,
  1070. .llseek = ftrace_regex_lseek,
  1071. .release = ftrace_filter_release,
  1072. };
  1073. static struct file_operations ftrace_notrace_fops = {
  1074. .open = ftrace_notrace_open,
  1075. .read = ftrace_regex_read,
  1076. .write = ftrace_notrace_write,
  1077. .llseek = ftrace_regex_lseek,
  1078. .release = ftrace_notrace_release,
  1079. };
  1080. static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
  1081. {
  1082. struct dentry *entry;
  1083. entry = debugfs_create_file("available_filter_functions", 0444,
  1084. d_tracer, NULL, &ftrace_avail_fops);
  1085. if (!entry)
  1086. pr_warning("Could not create debugfs "
  1087. "'available_filter_functions' entry\n");
  1088. entry = debugfs_create_file("failures", 0444,
  1089. d_tracer, NULL, &ftrace_failures_fops);
  1090. if (!entry)
  1091. pr_warning("Could not create debugfs 'failures' entry\n");
  1092. entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
  1093. NULL, &ftrace_filter_fops);
  1094. if (!entry)
  1095. pr_warning("Could not create debugfs "
  1096. "'set_ftrace_filter' entry\n");
  1097. entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
  1098. NULL, &ftrace_notrace_fops);
  1099. if (!entry)
  1100. pr_warning("Could not create debugfs "
  1101. "'set_ftrace_notrace' entry\n");
  1102. return 0;
  1103. }
  1104. static int ftrace_convert_nops(struct module *mod,
  1105. unsigned long *start,
  1106. unsigned long *end)
  1107. {
  1108. unsigned long *p;
  1109. unsigned long addr;
  1110. unsigned long flags;
  1111. mutex_lock(&ftrace_start_lock);
  1112. p = start;
  1113. while (p < end) {
  1114. addr = ftrace_call_adjust(*p++);
  1115. /*
  1116. * Some architecture linkers will pad between
  1117. * the different mcount_loc sections of different
  1118. * object files to satisfy alignments.
  1119. * Skip any NULL pointers.
  1120. */
  1121. if (!addr)
  1122. continue;
  1123. ftrace_record_ip(addr);
  1124. }
  1125. /* disable interrupts to prevent kstop machine */
  1126. local_irq_save(flags);
  1127. ftrace_update_code(mod);
  1128. local_irq_restore(flags);
  1129. mutex_unlock(&ftrace_start_lock);
  1130. return 0;
  1131. }
  1132. void ftrace_init_module(struct module *mod,
  1133. unsigned long *start, unsigned long *end)
  1134. {
  1135. if (ftrace_disabled || start == end)
  1136. return;
  1137. ftrace_convert_nops(mod, start, end);
  1138. }
  1139. extern unsigned long __start_mcount_loc[];
  1140. extern unsigned long __stop_mcount_loc[];
  1141. void __init ftrace_init(void)
  1142. {
  1143. unsigned long count, addr, flags;
  1144. int ret;
  1145. /* Keep the ftrace pointer to the stub */
  1146. addr = (unsigned long)ftrace_stub;
  1147. local_irq_save(flags);
  1148. ftrace_dyn_arch_init(&addr);
  1149. local_irq_restore(flags);
  1150. /* ftrace_dyn_arch_init places the return code in addr */
  1151. if (addr)
  1152. goto failed;
  1153. count = __stop_mcount_loc - __start_mcount_loc;
  1154. ret = ftrace_dyn_table_alloc(count);
  1155. if (ret)
  1156. goto failed;
  1157. last_ftrace_enabled = ftrace_enabled = 1;
  1158. ret = ftrace_convert_nops(NULL,
  1159. __start_mcount_loc,
  1160. __stop_mcount_loc);
  1161. return;
  1162. failed:
  1163. ftrace_disabled = 1;
  1164. }
  1165. #else
  1166. static int __init ftrace_nodyn_init(void)
  1167. {
  1168. ftrace_enabled = 1;
  1169. return 0;
  1170. }
  1171. device_initcall(ftrace_nodyn_init);
  1172. static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
  1173. static inline void ftrace_startup_enable(int command) { }
  1174. /* Keep as macros so we do not need to define the commands */
  1175. # define ftrace_startup(command) do { } while (0)
  1176. # define ftrace_shutdown(command) do { } while (0)
  1177. # define ftrace_startup_sysctl() do { } while (0)
  1178. # define ftrace_shutdown_sysctl() do { } while (0)
  1179. #endif /* CONFIG_DYNAMIC_FTRACE */
  1180. static ssize_t
  1181. ftrace_pid_read(struct file *file, char __user *ubuf,
  1182. size_t cnt, loff_t *ppos)
  1183. {
  1184. char buf[64];
  1185. int r;
  1186. if (ftrace_pid_trace >= 0)
  1187. r = sprintf(buf, "%u\n", ftrace_pid_trace);
  1188. else
  1189. r = sprintf(buf, "no pid\n");
  1190. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1191. }
  1192. static ssize_t
  1193. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  1194. size_t cnt, loff_t *ppos)
  1195. {
  1196. char buf[64];
  1197. long val;
  1198. int ret;
  1199. if (cnt >= sizeof(buf))
  1200. return -EINVAL;
  1201. if (copy_from_user(&buf, ubuf, cnt))
  1202. return -EFAULT;
  1203. buf[cnt] = 0;
  1204. ret = strict_strtol(buf, 10, &val);
  1205. if (ret < 0)
  1206. return ret;
  1207. mutex_lock(&ftrace_start_lock);
  1208. if (ret < 0) {
  1209. /* disable pid tracing */
  1210. if (ftrace_pid_trace < 0)
  1211. goto out;
  1212. ftrace_pid_trace = -1;
  1213. } else {
  1214. if (ftrace_pid_trace == val)
  1215. goto out;
  1216. ftrace_pid_trace = val;
  1217. }
  1218. /* update the function call */
  1219. ftrace_update_pid_func();
  1220. ftrace_startup_enable(0);
  1221. out:
  1222. mutex_unlock(&ftrace_start_lock);
  1223. return cnt;
  1224. }
  1225. static struct file_operations ftrace_pid_fops = {
  1226. .read = ftrace_pid_read,
  1227. .write = ftrace_pid_write,
  1228. };
  1229. static __init int ftrace_init_debugfs(void)
  1230. {
  1231. struct dentry *d_tracer;
  1232. struct dentry *entry;
  1233. d_tracer = tracing_init_dentry();
  1234. if (!d_tracer)
  1235. return 0;
  1236. ftrace_init_dyn_debugfs(d_tracer);
  1237. entry = debugfs_create_file("set_ftrace_pid", 0644, d_tracer,
  1238. NULL, &ftrace_pid_fops);
  1239. if (!entry)
  1240. pr_warning("Could not create debugfs "
  1241. "'set_ftrace_pid' entry\n");
  1242. return 0;
  1243. }
  1244. fs_initcall(ftrace_init_debugfs);
  1245. /**
  1246. * ftrace_kill - kill ftrace
  1247. *
  1248. * This function should be used by panic code. It stops ftrace
  1249. * but in a not so nice way. If you need to simply kill ftrace
  1250. * from a non-atomic section, use ftrace_kill.
  1251. */
  1252. void ftrace_kill(void)
  1253. {
  1254. ftrace_disabled = 1;
  1255. ftrace_enabled = 0;
  1256. clear_ftrace_function();
  1257. }
  1258. /**
  1259. * register_ftrace_function - register a function for profiling
  1260. * @ops - ops structure that holds the function for profiling.
  1261. *
  1262. * Register a function to be called by all functions in the
  1263. * kernel.
  1264. *
  1265. * Note: @ops->func and all the functions it calls must be labeled
  1266. * with "notrace", otherwise it will go into a
  1267. * recursive loop.
  1268. */
  1269. int register_ftrace_function(struct ftrace_ops *ops)
  1270. {
  1271. int ret;
  1272. if (unlikely(ftrace_disabled))
  1273. return -1;
  1274. mutex_lock(&ftrace_sysctl_lock);
  1275. ret = __register_ftrace_function(ops);
  1276. ftrace_startup(0);
  1277. mutex_unlock(&ftrace_sysctl_lock);
  1278. return ret;
  1279. }
  1280. /**
  1281. * unregister_ftrace_function - unresgister a function for profiling.
  1282. * @ops - ops structure that holds the function to unregister
  1283. *
  1284. * Unregister a function that was added to be called by ftrace profiling.
  1285. */
  1286. int unregister_ftrace_function(struct ftrace_ops *ops)
  1287. {
  1288. int ret;
  1289. mutex_lock(&ftrace_sysctl_lock);
  1290. ret = __unregister_ftrace_function(ops);
  1291. ftrace_shutdown(0);
  1292. mutex_unlock(&ftrace_sysctl_lock);
  1293. return ret;
  1294. }
  1295. int
  1296. ftrace_enable_sysctl(struct ctl_table *table, int write,
  1297. struct file *file, void __user *buffer, size_t *lenp,
  1298. loff_t *ppos)
  1299. {
  1300. int ret;
  1301. if (unlikely(ftrace_disabled))
  1302. return -ENODEV;
  1303. mutex_lock(&ftrace_sysctl_lock);
  1304. ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
  1305. if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
  1306. goto out;
  1307. last_ftrace_enabled = ftrace_enabled;
  1308. if (ftrace_enabled) {
  1309. ftrace_startup_sysctl();
  1310. /* we are starting ftrace again */
  1311. if (ftrace_list != &ftrace_list_end) {
  1312. if (ftrace_list->next == &ftrace_list_end)
  1313. ftrace_trace_function = ftrace_list->func;
  1314. else
  1315. ftrace_trace_function = ftrace_list_func;
  1316. }
  1317. } else {
  1318. /* stopping ftrace calls (just send to ftrace_stub) */
  1319. ftrace_trace_function = ftrace_stub;
  1320. ftrace_shutdown_sysctl();
  1321. }
  1322. out:
  1323. mutex_unlock(&ftrace_sysctl_lock);
  1324. return ret;
  1325. }
  1326. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1327. static atomic_t ftrace_graph_active;
  1328. /* The callbacks that hook a function */
  1329. trace_func_graph_ret_t ftrace_graph_return =
  1330. (trace_func_graph_ret_t)ftrace_stub;
  1331. trace_func_graph_ent_t ftrace_graph_entry =
  1332. (trace_func_graph_ent_t)ftrace_stub;
  1333. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  1334. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  1335. {
  1336. int i;
  1337. int ret = 0;
  1338. unsigned long flags;
  1339. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  1340. struct task_struct *g, *t;
  1341. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  1342. ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
  1343. * sizeof(struct ftrace_ret_stack),
  1344. GFP_KERNEL);
  1345. if (!ret_stack_list[i]) {
  1346. start = 0;
  1347. end = i;
  1348. ret = -ENOMEM;
  1349. goto free;
  1350. }
  1351. }
  1352. read_lock_irqsave(&tasklist_lock, flags);
  1353. do_each_thread(g, t) {
  1354. if (start == end) {
  1355. ret = -EAGAIN;
  1356. goto unlock;
  1357. }
  1358. if (t->ret_stack == NULL) {
  1359. t->ret_stack = ret_stack_list[start++];
  1360. t->curr_ret_stack = -1;
  1361. atomic_set(&t->trace_overrun, 0);
  1362. }
  1363. } while_each_thread(g, t);
  1364. unlock:
  1365. read_unlock_irqrestore(&tasklist_lock, flags);
  1366. free:
  1367. for (i = start; i < end; i++)
  1368. kfree(ret_stack_list[i]);
  1369. return ret;
  1370. }
  1371. /* Allocate a return stack for each task */
  1372. static int start_graph_tracing(void)
  1373. {
  1374. struct ftrace_ret_stack **ret_stack_list;
  1375. int ret;
  1376. ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
  1377. sizeof(struct ftrace_ret_stack *),
  1378. GFP_KERNEL);
  1379. if (!ret_stack_list)
  1380. return -ENOMEM;
  1381. do {
  1382. ret = alloc_retstack_tasklist(ret_stack_list);
  1383. } while (ret == -EAGAIN);
  1384. kfree(ret_stack_list);
  1385. return ret;
  1386. }
  1387. int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  1388. trace_func_graph_ent_t entryfunc)
  1389. {
  1390. int ret = 0;
  1391. mutex_lock(&ftrace_sysctl_lock);
  1392. atomic_inc(&ftrace_graph_active);
  1393. ret = start_graph_tracing();
  1394. if (ret) {
  1395. atomic_dec(&ftrace_graph_active);
  1396. goto out;
  1397. }
  1398. ftrace_graph_return = retfunc;
  1399. ftrace_graph_entry = entryfunc;
  1400. ftrace_startup(FTRACE_START_FUNC_RET);
  1401. out:
  1402. mutex_unlock(&ftrace_sysctl_lock);
  1403. return ret;
  1404. }
  1405. void unregister_ftrace_graph(void)
  1406. {
  1407. mutex_lock(&ftrace_sysctl_lock);
  1408. atomic_dec(&ftrace_graph_active);
  1409. ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
  1410. ftrace_graph_entry = (trace_func_graph_ent_t)ftrace_stub;
  1411. ftrace_shutdown(FTRACE_STOP_FUNC_RET);
  1412. mutex_unlock(&ftrace_sysctl_lock);
  1413. }
  1414. /* Allocate a return stack for newly created task */
  1415. void ftrace_graph_init_task(struct task_struct *t)
  1416. {
  1417. if (atomic_read(&ftrace_graph_active)) {
  1418. t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  1419. * sizeof(struct ftrace_ret_stack),
  1420. GFP_KERNEL);
  1421. if (!t->ret_stack)
  1422. return;
  1423. t->curr_ret_stack = -1;
  1424. atomic_set(&t->trace_overrun, 0);
  1425. } else
  1426. t->ret_stack = NULL;
  1427. }
  1428. void ftrace_graph_exit_task(struct task_struct *t)
  1429. {
  1430. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  1431. t->ret_stack = NULL;
  1432. /* NULL must become visible to IRQs before we free it: */
  1433. barrier();
  1434. kfree(ret_stack);
  1435. }
  1436. #endif