ftrace.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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/kthread.h>
  19. #include <linux/hardirq.h>
  20. #include <linux/ftrace.h>
  21. #include <linux/module.h>
  22. #include <linux/sysctl.h>
  23. #include <linux/hash.h>
  24. #include <linux/list.h>
  25. #include "trace.h"
  26. int ftrace_enabled;
  27. static int last_ftrace_enabled;
  28. static DEFINE_SPINLOCK(ftrace_lock);
  29. static DEFINE_MUTEX(ftrace_sysctl_lock);
  30. static struct ftrace_ops ftrace_list_end __read_mostly =
  31. {
  32. .func = ftrace_stub,
  33. };
  34. static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
  35. ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
  36. /* mcount is defined per arch in assembly */
  37. EXPORT_SYMBOL(mcount);
  38. notrace void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
  39. {
  40. struct ftrace_ops *op = ftrace_list;
  41. /* in case someone actually ports this to alpha! */
  42. read_barrier_depends();
  43. while (op != &ftrace_list_end) {
  44. /* silly alpha */
  45. read_barrier_depends();
  46. op->func(ip, parent_ip);
  47. op = op->next;
  48. };
  49. }
  50. /**
  51. * clear_ftrace_function - reset the ftrace function
  52. *
  53. * This NULLs the ftrace function and in essence stops
  54. * tracing. There may be lag
  55. */
  56. void clear_ftrace_function(void)
  57. {
  58. ftrace_trace_function = ftrace_stub;
  59. }
  60. static int notrace __register_ftrace_function(struct ftrace_ops *ops)
  61. {
  62. /* Should never be called by interrupts */
  63. spin_lock(&ftrace_lock);
  64. ops->next = ftrace_list;
  65. /*
  66. * We are entering ops into the ftrace_list but another
  67. * CPU might be walking that list. We need to make sure
  68. * the ops->next pointer is valid before another CPU sees
  69. * the ops pointer included into the ftrace_list.
  70. */
  71. smp_wmb();
  72. ftrace_list = ops;
  73. if (ftrace_enabled) {
  74. /*
  75. * For one func, simply call it directly.
  76. * For more than one func, call the chain.
  77. */
  78. if (ops->next == &ftrace_list_end)
  79. ftrace_trace_function = ops->func;
  80. else
  81. ftrace_trace_function = ftrace_list_func;
  82. }
  83. spin_unlock(&ftrace_lock);
  84. return 0;
  85. }
  86. static int notrace __unregister_ftrace_function(struct ftrace_ops *ops)
  87. {
  88. struct ftrace_ops **p;
  89. int ret = 0;
  90. spin_lock(&ftrace_lock);
  91. /*
  92. * If we are removing the last function, then simply point
  93. * to the ftrace_stub.
  94. */
  95. if (ftrace_list == ops && ops->next == &ftrace_list_end) {
  96. ftrace_trace_function = ftrace_stub;
  97. ftrace_list = &ftrace_list_end;
  98. goto out;
  99. }
  100. for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
  101. if (*p == ops)
  102. break;
  103. if (*p != ops) {
  104. ret = -1;
  105. goto out;
  106. }
  107. *p = (*p)->next;
  108. if (ftrace_enabled) {
  109. /* If we only have one func left, then call that directly */
  110. if (ftrace_list == &ftrace_list_end ||
  111. ftrace_list->next == &ftrace_list_end)
  112. ftrace_trace_function = ftrace_list->func;
  113. }
  114. out:
  115. spin_unlock(&ftrace_lock);
  116. return ret;
  117. }
  118. #ifdef CONFIG_DYNAMIC_FTRACE
  119. enum {
  120. FTRACE_ENABLE_CALLS = (1 << 0),
  121. FTRACE_DISABLE_CALLS = (1 << 1),
  122. FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
  123. FTRACE_ENABLE_MCOUNT = (1 << 3),
  124. FTRACE_DISABLE_MCOUNT = (1 << 4),
  125. };
  126. static struct hlist_head ftrace_hash[FTRACE_HASHSIZE];
  127. static DEFINE_PER_CPU(int, ftrace_shutdown_disable_cpu);
  128. static DEFINE_SPINLOCK(ftrace_shutdown_lock);
  129. static DEFINE_MUTEX(ftraced_lock);
  130. struct ftrace_page {
  131. struct ftrace_page *next;
  132. int index;
  133. struct dyn_ftrace records[];
  134. } __attribute__((packed));
  135. #define ENTRIES_PER_PAGE \
  136. ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
  137. /* estimate from running different kernels */
  138. #define NR_TO_INIT 10000
  139. static struct ftrace_page *ftrace_pages_start;
  140. static struct ftrace_page *ftrace_pages;
  141. static int ftraced_trigger;
  142. static int ftraced_suspend;
  143. static int ftrace_record_suspend;
  144. static inline int
  145. notrace ftrace_ip_in_hash(unsigned long ip, unsigned long key)
  146. {
  147. struct dyn_ftrace *p;
  148. struct hlist_node *t;
  149. int found = 0;
  150. hlist_for_each_entry(p, t, &ftrace_hash[key], node) {
  151. if (p->ip == ip) {
  152. found = 1;
  153. break;
  154. }
  155. }
  156. return found;
  157. }
  158. static inline void notrace
  159. ftrace_add_hash(struct dyn_ftrace *node, unsigned long key)
  160. {
  161. hlist_add_head(&node->node, &ftrace_hash[key]);
  162. }
  163. static notrace struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
  164. {
  165. if (ftrace_pages->index == ENTRIES_PER_PAGE) {
  166. if (!ftrace_pages->next)
  167. return NULL;
  168. ftrace_pages = ftrace_pages->next;
  169. }
  170. return &ftrace_pages->records[ftrace_pages->index++];
  171. }
  172. static void notrace
  173. ftrace_record_ip(unsigned long ip)
  174. {
  175. struct dyn_ftrace *node;
  176. unsigned long flags;
  177. unsigned long key;
  178. int resched;
  179. int atomic;
  180. if (!ftrace_enabled)
  181. return;
  182. resched = need_resched();
  183. preempt_disable_notrace();
  184. /* We simply need to protect against recursion */
  185. __get_cpu_var(ftrace_shutdown_disable_cpu)++;
  186. if (__get_cpu_var(ftrace_shutdown_disable_cpu) != 1)
  187. goto out;
  188. if (unlikely(ftrace_record_suspend))
  189. goto out;
  190. key = hash_long(ip, FTRACE_HASHBITS);
  191. WARN_ON_ONCE(key >= FTRACE_HASHSIZE);
  192. if (ftrace_ip_in_hash(ip, key))
  193. goto out;
  194. atomic = irqs_disabled();
  195. spin_lock_irqsave(&ftrace_shutdown_lock, flags);
  196. /* This ip may have hit the hash before the lock */
  197. if (ftrace_ip_in_hash(ip, key))
  198. goto out_unlock;
  199. /*
  200. * There's a slight race that the ftraced will update the
  201. * hash and reset here. If it is already converted, skip it.
  202. */
  203. if (ftrace_ip_converted(ip))
  204. goto out_unlock;
  205. node = ftrace_alloc_dyn_node(ip);
  206. if (!node)
  207. goto out_unlock;
  208. node->ip = ip;
  209. ftrace_add_hash(node, key);
  210. ftraced_trigger = 1;
  211. out_unlock:
  212. spin_unlock_irqrestore(&ftrace_shutdown_lock, flags);
  213. out:
  214. __get_cpu_var(ftrace_shutdown_disable_cpu)--;
  215. /* prevent recursion with scheduler */
  216. if (resched)
  217. preempt_enable_no_resched_notrace();
  218. else
  219. preempt_enable_notrace();
  220. }
  221. #define FTRACE_ADDR ((long)(&ftrace_caller))
  222. #define MCOUNT_ADDR ((long)(&mcount))
  223. static void notrace ftrace_replace_code(int saved)
  224. {
  225. unsigned char *new = NULL, *old = NULL;
  226. struct dyn_ftrace *rec;
  227. struct ftrace_page *pg;
  228. unsigned long ip;
  229. int failed;
  230. int i;
  231. if (saved)
  232. old = ftrace_nop_replace();
  233. else
  234. new = ftrace_nop_replace();
  235. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  236. for (i = 0; i < pg->index; i++) {
  237. rec = &pg->records[i];
  238. /* don't modify code that has already faulted */
  239. if (rec->flags & FTRACE_FL_FAILED)
  240. continue;
  241. ip = rec->ip;
  242. if (saved)
  243. new = ftrace_call_replace(ip, FTRACE_ADDR);
  244. else
  245. old = ftrace_call_replace(ip, FTRACE_ADDR);
  246. failed = ftrace_modify_code(ip, old, new);
  247. if (failed)
  248. rec->flags |= FTRACE_FL_FAILED;
  249. }
  250. }
  251. }
  252. static notrace void ftrace_shutdown_replenish(void)
  253. {
  254. if (ftrace_pages->next)
  255. return;
  256. /* allocate another page */
  257. ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL);
  258. }
  259. static notrace void
  260. ftrace_code_disable(struct dyn_ftrace *rec)
  261. {
  262. unsigned long ip;
  263. unsigned char *nop, *call;
  264. int failed;
  265. ip = rec->ip;
  266. nop = ftrace_nop_replace();
  267. call = ftrace_call_replace(ip, MCOUNT_ADDR);
  268. failed = ftrace_modify_code(ip, call, nop);
  269. if (failed)
  270. rec->flags |= FTRACE_FL_FAILED;
  271. }
  272. static int notrace __ftrace_modify_code(void *data)
  273. {
  274. unsigned long addr;
  275. int *command = data;
  276. if (*command & FTRACE_ENABLE_CALLS)
  277. ftrace_replace_code(1);
  278. else if (*command & FTRACE_DISABLE_CALLS)
  279. ftrace_replace_code(0);
  280. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  281. ftrace_update_ftrace_func(ftrace_trace_function);
  282. if (*command & FTRACE_ENABLE_MCOUNT) {
  283. addr = (unsigned long)ftrace_record_ip;
  284. ftrace_mcount_set(&addr);
  285. } else if (*command & FTRACE_DISABLE_MCOUNT) {
  286. addr = (unsigned long)ftrace_stub;
  287. ftrace_mcount_set(&addr);
  288. }
  289. return 0;
  290. }
  291. static void notrace ftrace_run_update_code(int command)
  292. {
  293. stop_machine_run(__ftrace_modify_code, &command, NR_CPUS);
  294. }
  295. static ftrace_func_t saved_ftrace_func;
  296. static void notrace ftrace_startup(void)
  297. {
  298. int command = 0;
  299. mutex_lock(&ftraced_lock);
  300. ftraced_suspend++;
  301. if (ftraced_suspend == 1)
  302. command |= FTRACE_ENABLE_CALLS;
  303. if (saved_ftrace_func != ftrace_trace_function) {
  304. saved_ftrace_func = ftrace_trace_function;
  305. command |= FTRACE_UPDATE_TRACE_FUNC;
  306. }
  307. if (!command || !ftrace_enabled)
  308. goto out;
  309. ftrace_run_update_code(command);
  310. out:
  311. mutex_unlock(&ftraced_lock);
  312. }
  313. static void notrace ftrace_shutdown(void)
  314. {
  315. int command = 0;
  316. mutex_lock(&ftraced_lock);
  317. ftraced_suspend--;
  318. if (!ftraced_suspend)
  319. command |= FTRACE_DISABLE_CALLS;
  320. if (saved_ftrace_func != ftrace_trace_function) {
  321. saved_ftrace_func = ftrace_trace_function;
  322. command |= FTRACE_UPDATE_TRACE_FUNC;
  323. }
  324. if (!command || !ftrace_enabled)
  325. goto out;
  326. ftrace_run_update_code(command);
  327. out:
  328. mutex_unlock(&ftraced_lock);
  329. }
  330. static void notrace ftrace_startup_sysctl(void)
  331. {
  332. int command = FTRACE_ENABLE_MCOUNT;
  333. mutex_lock(&ftraced_lock);
  334. /* Force update next time */
  335. saved_ftrace_func = NULL;
  336. /* ftraced_suspend is true if we want ftrace running */
  337. if (ftraced_suspend)
  338. command |= FTRACE_ENABLE_CALLS;
  339. ftrace_run_update_code(command);
  340. mutex_unlock(&ftraced_lock);
  341. }
  342. static void notrace ftrace_shutdown_sysctl(void)
  343. {
  344. int command = FTRACE_DISABLE_MCOUNT;
  345. mutex_lock(&ftraced_lock);
  346. /* ftraced_suspend is true if ftrace is running */
  347. if (ftraced_suspend)
  348. command |= FTRACE_DISABLE_CALLS;
  349. ftrace_run_update_code(command);
  350. mutex_unlock(&ftraced_lock);
  351. }
  352. static cycle_t ftrace_update_time;
  353. static unsigned long ftrace_update_cnt;
  354. unsigned long ftrace_update_tot_cnt;
  355. static int notrace __ftrace_update_code(void *ignore)
  356. {
  357. struct dyn_ftrace *p;
  358. struct hlist_head head;
  359. struct hlist_node *t;
  360. int save_ftrace_enabled;
  361. cycle_t start, stop;
  362. int i;
  363. /* Don't be recording funcs now */
  364. save_ftrace_enabled = ftrace_enabled;
  365. ftrace_enabled = 0;
  366. start = now(raw_smp_processor_id());
  367. ftrace_update_cnt = 0;
  368. /* No locks needed, the machine is stopped! */
  369. for (i = 0; i < FTRACE_HASHSIZE; i++) {
  370. if (hlist_empty(&ftrace_hash[i]))
  371. continue;
  372. head = ftrace_hash[i];
  373. INIT_HLIST_HEAD(&ftrace_hash[i]);
  374. /* all CPUS are stopped, we are safe to modify code */
  375. hlist_for_each_entry(p, t, &head, node) {
  376. ftrace_code_disable(p);
  377. ftrace_update_cnt++;
  378. }
  379. }
  380. stop = now(raw_smp_processor_id());
  381. ftrace_update_time = stop - start;
  382. ftrace_update_tot_cnt += ftrace_update_cnt;
  383. ftrace_enabled = save_ftrace_enabled;
  384. return 0;
  385. }
  386. static void notrace ftrace_update_code(void)
  387. {
  388. stop_machine_run(__ftrace_update_code, NULL, NR_CPUS);
  389. }
  390. static int notrace ftraced(void *ignore)
  391. {
  392. unsigned long usecs;
  393. set_current_state(TASK_INTERRUPTIBLE);
  394. while (!kthread_should_stop()) {
  395. /* check once a second */
  396. schedule_timeout(HZ);
  397. mutex_lock(&ftrace_sysctl_lock);
  398. mutex_lock(&ftraced_lock);
  399. if (ftrace_enabled && ftraced_trigger && !ftraced_suspend) {
  400. ftrace_record_suspend++;
  401. ftrace_update_code();
  402. usecs = nsecs_to_usecs(ftrace_update_time);
  403. if (ftrace_update_tot_cnt > 100000) {
  404. ftrace_update_tot_cnt = 0;
  405. pr_info("hm, dftrace overflow: %lu change%s"
  406. " (%lu total) in %lu usec%s\n",
  407. ftrace_update_cnt,
  408. ftrace_update_cnt != 1 ? "s" : "",
  409. ftrace_update_tot_cnt,
  410. usecs, usecs != 1 ? "s" : "");
  411. WARN_ON_ONCE(1);
  412. }
  413. ftraced_trigger = 0;
  414. ftrace_record_suspend--;
  415. }
  416. mutex_unlock(&ftraced_lock);
  417. mutex_unlock(&ftrace_sysctl_lock);
  418. ftrace_shutdown_replenish();
  419. set_current_state(TASK_INTERRUPTIBLE);
  420. }
  421. __set_current_state(TASK_RUNNING);
  422. return 0;
  423. }
  424. static int __init ftrace_dyn_table_alloc(void)
  425. {
  426. struct ftrace_page *pg;
  427. int cnt;
  428. int i;
  429. /* allocate a few pages */
  430. ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
  431. if (!ftrace_pages_start)
  432. return -1;
  433. /*
  434. * Allocate a few more pages.
  435. *
  436. * TODO: have some parser search vmlinux before
  437. * final linking to find all calls to ftrace.
  438. * Then we can:
  439. * a) know how many pages to allocate.
  440. * and/or
  441. * b) set up the table then.
  442. *
  443. * The dynamic code is still necessary for
  444. * modules.
  445. */
  446. pg = ftrace_pages = ftrace_pages_start;
  447. cnt = NR_TO_INIT / ENTRIES_PER_PAGE;
  448. for (i = 0; i < cnt; i++) {
  449. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  450. /* If we fail, we'll try later anyway */
  451. if (!pg->next)
  452. break;
  453. pg = pg->next;
  454. }
  455. return 0;
  456. }
  457. static int __init notrace ftrace_dynamic_init(void)
  458. {
  459. struct task_struct *p;
  460. unsigned long addr;
  461. int ret;
  462. addr = (unsigned long)ftrace_record_ip;
  463. stop_machine_run(ftrace_dyn_arch_init, &addr, NR_CPUS);
  464. /* ftrace_dyn_arch_init places the return code in addr */
  465. if (addr)
  466. return addr;
  467. ret = ftrace_dyn_table_alloc();
  468. if (ret)
  469. return ret;
  470. p = kthread_run(ftraced, NULL, "ftraced");
  471. if (IS_ERR(p))
  472. return -1;
  473. last_ftrace_enabled = ftrace_enabled = 1;
  474. return 0;
  475. }
  476. core_initcall(ftrace_dynamic_init);
  477. #else
  478. # define ftrace_startup() do { } while (0)
  479. # define ftrace_shutdown() do { } while (0)
  480. # define ftrace_startup_sysctl() do { } while (0)
  481. # define ftrace_shutdown_sysctl() do { } while (0)
  482. #endif /* CONFIG_DYNAMIC_FTRACE */
  483. /**
  484. * register_ftrace_function - register a function for profiling
  485. * @ops - ops structure that holds the function for profiling.
  486. *
  487. * Register a function to be called by all functions in the
  488. * kernel.
  489. *
  490. * Note: @ops->func and all the functions it calls must be labeled
  491. * with "notrace", otherwise it will go into a
  492. * recursive loop.
  493. */
  494. int register_ftrace_function(struct ftrace_ops *ops)
  495. {
  496. int ret;
  497. mutex_lock(&ftrace_sysctl_lock);
  498. ret = __register_ftrace_function(ops);
  499. ftrace_startup();
  500. mutex_unlock(&ftrace_sysctl_lock);
  501. return ret;
  502. }
  503. /**
  504. * unregister_ftrace_function - unresgister a function for profiling.
  505. * @ops - ops structure that holds the function to unregister
  506. *
  507. * Unregister a function that was added to be called by ftrace profiling.
  508. */
  509. int unregister_ftrace_function(struct ftrace_ops *ops)
  510. {
  511. int ret;
  512. mutex_lock(&ftrace_sysctl_lock);
  513. ret = __unregister_ftrace_function(ops);
  514. ftrace_shutdown();
  515. mutex_unlock(&ftrace_sysctl_lock);
  516. return ret;
  517. }
  518. notrace int
  519. ftrace_enable_sysctl(struct ctl_table *table, int write,
  520. struct file *filp, void __user *buffer, size_t *lenp,
  521. loff_t *ppos)
  522. {
  523. int ret;
  524. mutex_lock(&ftrace_sysctl_lock);
  525. ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
  526. if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
  527. goto out;
  528. last_ftrace_enabled = ftrace_enabled;
  529. if (ftrace_enabled) {
  530. ftrace_startup_sysctl();
  531. /* we are starting ftrace again */
  532. if (ftrace_list != &ftrace_list_end) {
  533. if (ftrace_list->next == &ftrace_list_end)
  534. ftrace_trace_function = ftrace_list->func;
  535. else
  536. ftrace_trace_function = ftrace_list_func;
  537. }
  538. } else {
  539. /* stopping ftrace calls (just send to ftrace_stub) */
  540. ftrace_trace_function = ftrace_stub;
  541. ftrace_shutdown_sysctl();
  542. }
  543. out:
  544. mutex_unlock(&ftrace_sysctl_lock);
  545. return ret;
  546. }