ftrace.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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/kthread.h>
  21. #include <linux/hardirq.h>
  22. #include <linux/ftrace.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/sysctl.h>
  25. #include <linux/hash.h>
  26. #include <linux/ctype.h>
  27. #include <linux/list.h>
  28. #include "trace.h"
  29. int ftrace_enabled;
  30. static int last_ftrace_enabled;
  31. static DEFINE_SPINLOCK(ftrace_lock);
  32. static DEFINE_MUTEX(ftrace_sysctl_lock);
  33. static struct ftrace_ops ftrace_list_end __read_mostly =
  34. {
  35. .func = ftrace_stub,
  36. };
  37. static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
  38. ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
  39. /* mcount is defined per arch in assembly */
  40. EXPORT_SYMBOL(mcount);
  41. notrace void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
  42. {
  43. struct ftrace_ops *op = ftrace_list;
  44. /* in case someone actually ports this to alpha! */
  45. read_barrier_depends();
  46. while (op != &ftrace_list_end) {
  47. /* silly alpha */
  48. read_barrier_depends();
  49. op->func(ip, parent_ip);
  50. op = op->next;
  51. };
  52. }
  53. /**
  54. * clear_ftrace_function - reset the ftrace function
  55. *
  56. * This NULLs the ftrace function and in essence stops
  57. * tracing. There may be lag
  58. */
  59. void clear_ftrace_function(void)
  60. {
  61. ftrace_trace_function = ftrace_stub;
  62. }
  63. static int notrace __register_ftrace_function(struct ftrace_ops *ops)
  64. {
  65. /* Should never be called by interrupts */
  66. spin_lock(&ftrace_lock);
  67. ops->next = ftrace_list;
  68. /*
  69. * We are entering ops into the ftrace_list but another
  70. * CPU might be walking that list. We need to make sure
  71. * the ops->next pointer is valid before another CPU sees
  72. * the ops pointer included into the ftrace_list.
  73. */
  74. smp_wmb();
  75. ftrace_list = ops;
  76. if (ftrace_enabled) {
  77. /*
  78. * For one func, simply call it directly.
  79. * For more than one func, call the chain.
  80. */
  81. if (ops->next == &ftrace_list_end)
  82. ftrace_trace_function = ops->func;
  83. else
  84. ftrace_trace_function = ftrace_list_func;
  85. }
  86. spin_unlock(&ftrace_lock);
  87. return 0;
  88. }
  89. static int notrace __unregister_ftrace_function(struct ftrace_ops *ops)
  90. {
  91. struct ftrace_ops **p;
  92. int ret = 0;
  93. spin_lock(&ftrace_lock);
  94. /*
  95. * If we are removing the last function, then simply point
  96. * to the ftrace_stub.
  97. */
  98. if (ftrace_list == ops && ops->next == &ftrace_list_end) {
  99. ftrace_trace_function = ftrace_stub;
  100. ftrace_list = &ftrace_list_end;
  101. goto out;
  102. }
  103. for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
  104. if (*p == ops)
  105. break;
  106. if (*p != ops) {
  107. ret = -1;
  108. goto out;
  109. }
  110. *p = (*p)->next;
  111. if (ftrace_enabled) {
  112. /* If we only have one func left, then call that directly */
  113. if (ftrace_list == &ftrace_list_end ||
  114. ftrace_list->next == &ftrace_list_end)
  115. ftrace_trace_function = ftrace_list->func;
  116. }
  117. out:
  118. spin_unlock(&ftrace_lock);
  119. return ret;
  120. }
  121. #ifdef CONFIG_DYNAMIC_FTRACE
  122. static struct task_struct *ftraced_task;
  123. static DECLARE_WAIT_QUEUE_HEAD(ftraced_waiters);
  124. static unsigned long ftraced_iteration_counter;
  125. enum {
  126. FTRACE_ENABLE_CALLS = (1 << 0),
  127. FTRACE_DISABLE_CALLS = (1 << 1),
  128. FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
  129. FTRACE_ENABLE_MCOUNT = (1 << 3),
  130. FTRACE_DISABLE_MCOUNT = (1 << 4),
  131. };
  132. static int ftrace_filtered;
  133. static struct hlist_head ftrace_hash[FTRACE_HASHSIZE];
  134. static DEFINE_PER_CPU(int, ftrace_shutdown_disable_cpu);
  135. static DEFINE_SPINLOCK(ftrace_shutdown_lock);
  136. static DEFINE_MUTEX(ftraced_lock);
  137. static DEFINE_MUTEX(ftrace_filter_lock);
  138. struct ftrace_page {
  139. struct ftrace_page *next;
  140. int index;
  141. struct dyn_ftrace records[];
  142. } __attribute__((packed));
  143. #define ENTRIES_PER_PAGE \
  144. ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
  145. /* estimate from running different kernels */
  146. #define NR_TO_INIT 10000
  147. static struct ftrace_page *ftrace_pages_start;
  148. static struct ftrace_page *ftrace_pages;
  149. static int ftraced_trigger;
  150. static int ftraced_suspend;
  151. static int ftrace_record_suspend;
  152. static inline int
  153. notrace ftrace_ip_in_hash(unsigned long ip, unsigned long key)
  154. {
  155. struct dyn_ftrace *p;
  156. struct hlist_node *t;
  157. int found = 0;
  158. hlist_for_each_entry(p, t, &ftrace_hash[key], node) {
  159. if (p->ip == ip) {
  160. found = 1;
  161. break;
  162. }
  163. }
  164. return found;
  165. }
  166. static inline void notrace
  167. ftrace_add_hash(struct dyn_ftrace *node, unsigned long key)
  168. {
  169. hlist_add_head(&node->node, &ftrace_hash[key]);
  170. }
  171. static notrace struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
  172. {
  173. if (ftrace_pages->index == ENTRIES_PER_PAGE) {
  174. if (!ftrace_pages->next)
  175. return NULL;
  176. ftrace_pages = ftrace_pages->next;
  177. }
  178. return &ftrace_pages->records[ftrace_pages->index++];
  179. }
  180. static void notrace
  181. ftrace_record_ip(unsigned long ip)
  182. {
  183. struct dyn_ftrace *node;
  184. unsigned long flags;
  185. unsigned long key;
  186. int resched;
  187. int atomic;
  188. if (!ftrace_enabled)
  189. return;
  190. resched = need_resched();
  191. preempt_disable_notrace();
  192. /* We simply need to protect against recursion */
  193. __get_cpu_var(ftrace_shutdown_disable_cpu)++;
  194. if (__get_cpu_var(ftrace_shutdown_disable_cpu) != 1)
  195. goto out;
  196. if (unlikely(ftrace_record_suspend))
  197. goto out;
  198. key = hash_long(ip, FTRACE_HASHBITS);
  199. WARN_ON_ONCE(key >= FTRACE_HASHSIZE);
  200. if (ftrace_ip_in_hash(ip, key))
  201. goto out;
  202. atomic = irqs_disabled();
  203. spin_lock_irqsave(&ftrace_shutdown_lock, flags);
  204. /* This ip may have hit the hash before the lock */
  205. if (ftrace_ip_in_hash(ip, key))
  206. goto out_unlock;
  207. /*
  208. * There's a slight race that the ftraced will update the
  209. * hash and reset here. If it is already converted, skip it.
  210. */
  211. if (ftrace_ip_converted(ip))
  212. goto out_unlock;
  213. node = ftrace_alloc_dyn_node(ip);
  214. if (!node)
  215. goto out_unlock;
  216. node->ip = ip;
  217. ftrace_add_hash(node, key);
  218. ftraced_trigger = 1;
  219. out_unlock:
  220. spin_unlock_irqrestore(&ftrace_shutdown_lock, flags);
  221. out:
  222. __get_cpu_var(ftrace_shutdown_disable_cpu)--;
  223. /* prevent recursion with scheduler */
  224. if (resched)
  225. preempt_enable_no_resched_notrace();
  226. else
  227. preempt_enable_notrace();
  228. }
  229. #define FTRACE_ADDR ((long)(&ftrace_caller))
  230. #define MCOUNT_ADDR ((long)(&mcount))
  231. static void notrace
  232. __ftrace_replace_code(struct dyn_ftrace *rec,
  233. unsigned char *old, unsigned char *new, int enable)
  234. {
  235. unsigned long ip;
  236. int failed;
  237. ip = rec->ip;
  238. if (ftrace_filtered && enable) {
  239. unsigned long fl;
  240. /*
  241. * If filtering is on:
  242. *
  243. * If this record is set to be filtered and
  244. * is enabled then do nothing.
  245. *
  246. * If this record is set to be filtered and
  247. * it is not enabled, enable it.
  248. *
  249. * If this record is not set to be filtered
  250. * and it is not enabled do nothing.
  251. *
  252. * If this record is not set to be filtered and
  253. * it is enabled, disable it.
  254. */
  255. fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
  256. if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
  257. (fl == 0))
  258. return;
  259. /*
  260. * If it is enabled disable it,
  261. * otherwise enable it!
  262. */
  263. if (fl == FTRACE_FL_ENABLED) {
  264. /* swap new and old */
  265. new = old;
  266. old = ftrace_call_replace(ip, FTRACE_ADDR);
  267. rec->flags &= ~FTRACE_FL_ENABLED;
  268. } else {
  269. new = ftrace_call_replace(ip, FTRACE_ADDR);
  270. rec->flags |= FTRACE_FL_ENABLED;
  271. }
  272. } else {
  273. if (enable)
  274. new = ftrace_call_replace(ip, FTRACE_ADDR);
  275. else
  276. old = ftrace_call_replace(ip, FTRACE_ADDR);
  277. if (enable) {
  278. if (rec->flags & FTRACE_FL_ENABLED)
  279. return;
  280. rec->flags |= FTRACE_FL_ENABLED;
  281. } else {
  282. if (!(rec->flags & FTRACE_FL_ENABLED))
  283. return;
  284. rec->flags &= ~FTRACE_FL_ENABLED;
  285. }
  286. }
  287. failed = ftrace_modify_code(ip, old, new);
  288. if (failed)
  289. rec->flags |= FTRACE_FL_FAILED;
  290. }
  291. static void notrace ftrace_replace_code(int enable)
  292. {
  293. unsigned char *new = NULL, *old = NULL;
  294. struct dyn_ftrace *rec;
  295. struct ftrace_page *pg;
  296. int i;
  297. if (enable)
  298. old = ftrace_nop_replace();
  299. else
  300. new = ftrace_nop_replace();
  301. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  302. for (i = 0; i < pg->index; i++) {
  303. rec = &pg->records[i];
  304. /* don't modify code that has already faulted */
  305. if (rec->flags & FTRACE_FL_FAILED)
  306. continue;
  307. __ftrace_replace_code(rec, old, new, enable);
  308. }
  309. }
  310. }
  311. static notrace void ftrace_shutdown_replenish(void)
  312. {
  313. if (ftrace_pages->next)
  314. return;
  315. /* allocate another page */
  316. ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL);
  317. }
  318. static notrace void
  319. ftrace_code_disable(struct dyn_ftrace *rec)
  320. {
  321. unsigned long ip;
  322. unsigned char *nop, *call;
  323. int failed;
  324. ip = rec->ip;
  325. nop = ftrace_nop_replace();
  326. call = ftrace_call_replace(ip, MCOUNT_ADDR);
  327. failed = ftrace_modify_code(ip, call, nop);
  328. if (failed)
  329. rec->flags |= FTRACE_FL_FAILED;
  330. }
  331. static int notrace __ftrace_modify_code(void *data)
  332. {
  333. unsigned long addr;
  334. int *command = data;
  335. if (*command & FTRACE_ENABLE_CALLS)
  336. ftrace_replace_code(1);
  337. else if (*command & FTRACE_DISABLE_CALLS)
  338. ftrace_replace_code(0);
  339. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  340. ftrace_update_ftrace_func(ftrace_trace_function);
  341. if (*command & FTRACE_ENABLE_MCOUNT) {
  342. addr = (unsigned long)ftrace_record_ip;
  343. ftrace_mcount_set(&addr);
  344. } else if (*command & FTRACE_DISABLE_MCOUNT) {
  345. addr = (unsigned long)ftrace_stub;
  346. ftrace_mcount_set(&addr);
  347. }
  348. return 0;
  349. }
  350. static void notrace ftrace_run_update_code(int command)
  351. {
  352. stop_machine_run(__ftrace_modify_code, &command, NR_CPUS);
  353. }
  354. static ftrace_func_t saved_ftrace_func;
  355. static void notrace ftrace_startup(void)
  356. {
  357. int command = 0;
  358. mutex_lock(&ftraced_lock);
  359. ftraced_suspend++;
  360. if (ftraced_suspend == 1)
  361. command |= FTRACE_ENABLE_CALLS;
  362. if (saved_ftrace_func != ftrace_trace_function) {
  363. saved_ftrace_func = ftrace_trace_function;
  364. command |= FTRACE_UPDATE_TRACE_FUNC;
  365. }
  366. if (!command || !ftrace_enabled)
  367. goto out;
  368. ftrace_run_update_code(command);
  369. out:
  370. mutex_unlock(&ftraced_lock);
  371. }
  372. static void notrace ftrace_shutdown(void)
  373. {
  374. int command = 0;
  375. mutex_lock(&ftraced_lock);
  376. ftraced_suspend--;
  377. if (!ftraced_suspend)
  378. command |= FTRACE_DISABLE_CALLS;
  379. if (saved_ftrace_func != ftrace_trace_function) {
  380. saved_ftrace_func = ftrace_trace_function;
  381. command |= FTRACE_UPDATE_TRACE_FUNC;
  382. }
  383. if (!command || !ftrace_enabled)
  384. goto out;
  385. ftrace_run_update_code(command);
  386. out:
  387. mutex_unlock(&ftraced_lock);
  388. }
  389. static void notrace ftrace_startup_sysctl(void)
  390. {
  391. int command = FTRACE_ENABLE_MCOUNT;
  392. mutex_lock(&ftraced_lock);
  393. /* Force update next time */
  394. saved_ftrace_func = NULL;
  395. /* ftraced_suspend is true if we want ftrace running */
  396. if (ftraced_suspend)
  397. command |= FTRACE_ENABLE_CALLS;
  398. ftrace_run_update_code(command);
  399. mutex_unlock(&ftraced_lock);
  400. }
  401. static void notrace ftrace_shutdown_sysctl(void)
  402. {
  403. int command = FTRACE_DISABLE_MCOUNT;
  404. mutex_lock(&ftraced_lock);
  405. /* ftraced_suspend is true if ftrace is running */
  406. if (ftraced_suspend)
  407. command |= FTRACE_DISABLE_CALLS;
  408. ftrace_run_update_code(command);
  409. mutex_unlock(&ftraced_lock);
  410. }
  411. static cycle_t ftrace_update_time;
  412. static unsigned long ftrace_update_cnt;
  413. unsigned long ftrace_update_tot_cnt;
  414. static int notrace __ftrace_update_code(void *ignore)
  415. {
  416. struct dyn_ftrace *p;
  417. struct hlist_head head;
  418. struct hlist_node *t;
  419. int save_ftrace_enabled;
  420. cycle_t start, stop;
  421. int i;
  422. /* Don't be recording funcs now */
  423. save_ftrace_enabled = ftrace_enabled;
  424. ftrace_enabled = 0;
  425. start = now(raw_smp_processor_id());
  426. ftrace_update_cnt = 0;
  427. /* No locks needed, the machine is stopped! */
  428. for (i = 0; i < FTRACE_HASHSIZE; i++) {
  429. if (hlist_empty(&ftrace_hash[i]))
  430. continue;
  431. head = ftrace_hash[i];
  432. INIT_HLIST_HEAD(&ftrace_hash[i]);
  433. /* all CPUS are stopped, we are safe to modify code */
  434. hlist_for_each_entry(p, t, &head, node) {
  435. ftrace_code_disable(p);
  436. ftrace_update_cnt++;
  437. }
  438. }
  439. stop = now(raw_smp_processor_id());
  440. ftrace_update_time = stop - start;
  441. ftrace_update_tot_cnt += ftrace_update_cnt;
  442. ftrace_enabled = save_ftrace_enabled;
  443. return 0;
  444. }
  445. static void notrace ftrace_update_code(void)
  446. {
  447. stop_machine_run(__ftrace_update_code, NULL, NR_CPUS);
  448. }
  449. static int notrace ftraced(void *ignore)
  450. {
  451. unsigned long usecs;
  452. set_current_state(TASK_INTERRUPTIBLE);
  453. while (!kthread_should_stop()) {
  454. /* check once a second */
  455. schedule_timeout(HZ);
  456. mutex_lock(&ftrace_sysctl_lock);
  457. mutex_lock(&ftraced_lock);
  458. if (ftrace_enabled && ftraced_trigger && !ftraced_suspend) {
  459. ftrace_record_suspend++;
  460. ftrace_update_code();
  461. usecs = nsecs_to_usecs(ftrace_update_time);
  462. if (ftrace_update_tot_cnt > 100000) {
  463. ftrace_update_tot_cnt = 0;
  464. pr_info("hm, dftrace overflow: %lu change%s"
  465. " (%lu total) in %lu usec%s\n",
  466. ftrace_update_cnt,
  467. ftrace_update_cnt != 1 ? "s" : "",
  468. ftrace_update_tot_cnt,
  469. usecs, usecs != 1 ? "s" : "");
  470. WARN_ON_ONCE(1);
  471. }
  472. ftraced_trigger = 0;
  473. ftrace_record_suspend--;
  474. }
  475. ftraced_iteration_counter++;
  476. mutex_unlock(&ftraced_lock);
  477. mutex_unlock(&ftrace_sysctl_lock);
  478. wake_up_interruptible(&ftraced_waiters);
  479. ftrace_shutdown_replenish();
  480. set_current_state(TASK_INTERRUPTIBLE);
  481. }
  482. __set_current_state(TASK_RUNNING);
  483. return 0;
  484. }
  485. static int __init ftrace_dyn_table_alloc(void)
  486. {
  487. struct ftrace_page *pg;
  488. int cnt;
  489. int i;
  490. /* allocate a few pages */
  491. ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
  492. if (!ftrace_pages_start)
  493. return -1;
  494. /*
  495. * Allocate a few more pages.
  496. *
  497. * TODO: have some parser search vmlinux before
  498. * final linking to find all calls to ftrace.
  499. * Then we can:
  500. * a) know how many pages to allocate.
  501. * and/or
  502. * b) set up the table then.
  503. *
  504. * The dynamic code is still necessary for
  505. * modules.
  506. */
  507. pg = ftrace_pages = ftrace_pages_start;
  508. cnt = NR_TO_INIT / ENTRIES_PER_PAGE;
  509. for (i = 0; i < cnt; i++) {
  510. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  511. /* If we fail, we'll try later anyway */
  512. if (!pg->next)
  513. break;
  514. pg = pg->next;
  515. }
  516. return 0;
  517. }
  518. enum {
  519. FTRACE_ITER_FILTER = (1 << 0),
  520. FTRACE_ITER_CONT = (1 << 1),
  521. };
  522. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  523. struct ftrace_iterator {
  524. loff_t pos;
  525. struct ftrace_page *pg;
  526. unsigned idx;
  527. unsigned flags;
  528. unsigned char buffer[FTRACE_BUFF_MAX+1];
  529. unsigned buffer_idx;
  530. unsigned filtered;
  531. };
  532. static void notrace *
  533. t_next(struct seq_file *m, void *v, loff_t *pos)
  534. {
  535. struct ftrace_iterator *iter = m->private;
  536. struct dyn_ftrace *rec = NULL;
  537. (*pos)++;
  538. retry:
  539. if (iter->idx >= iter->pg->index) {
  540. if (iter->pg->next) {
  541. iter->pg = iter->pg->next;
  542. iter->idx = 0;
  543. goto retry;
  544. }
  545. } else {
  546. rec = &iter->pg->records[iter->idx++];
  547. if ((rec->flags & FTRACE_FL_FAILED) ||
  548. ((iter->flags & FTRACE_ITER_FILTER) &&
  549. !(rec->flags & FTRACE_FL_FILTER))) {
  550. rec = NULL;
  551. goto retry;
  552. }
  553. }
  554. iter->pos = *pos;
  555. return rec;
  556. }
  557. static void *t_start(struct seq_file *m, loff_t *pos)
  558. {
  559. struct ftrace_iterator *iter = m->private;
  560. void *p = NULL;
  561. loff_t l = -1;
  562. if (*pos != iter->pos) {
  563. for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
  564. ;
  565. } else {
  566. l = *pos;
  567. p = t_next(m, p, &l);
  568. }
  569. return p;
  570. }
  571. static void t_stop(struct seq_file *m, void *p)
  572. {
  573. }
  574. static int t_show(struct seq_file *m, void *v)
  575. {
  576. struct dyn_ftrace *rec = v;
  577. char str[KSYM_SYMBOL_LEN];
  578. if (!rec)
  579. return 0;
  580. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  581. seq_printf(m, "%s\n", str);
  582. return 0;
  583. }
  584. static struct seq_operations show_ftrace_seq_ops = {
  585. .start = t_start,
  586. .next = t_next,
  587. .stop = t_stop,
  588. .show = t_show,
  589. };
  590. static int notrace
  591. ftrace_avail_open(struct inode *inode, struct file *file)
  592. {
  593. struct ftrace_iterator *iter;
  594. int ret;
  595. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  596. if (!iter)
  597. return -ENOMEM;
  598. iter->pg = ftrace_pages_start;
  599. iter->pos = -1;
  600. ret = seq_open(file, &show_ftrace_seq_ops);
  601. if (!ret) {
  602. struct seq_file *m = file->private_data;
  603. m->private = iter;
  604. } else
  605. kfree(iter);
  606. return ret;
  607. }
  608. int ftrace_avail_release(struct inode *inode, struct file *file)
  609. {
  610. struct seq_file *m = (struct seq_file *)file->private_data;
  611. struct ftrace_iterator *iter = m->private;
  612. seq_release(inode, file);
  613. kfree(iter);
  614. return 0;
  615. }
  616. static void notrace ftrace_filter_reset(void)
  617. {
  618. struct ftrace_page *pg;
  619. struct dyn_ftrace *rec;
  620. unsigned i;
  621. /* keep kstop machine from running */
  622. preempt_disable();
  623. ftrace_filtered = 0;
  624. pg = ftrace_pages_start;
  625. while (pg) {
  626. for (i = 0; i < pg->index; i++) {
  627. rec = &pg->records[i];
  628. if (rec->flags & FTRACE_FL_FAILED)
  629. continue;
  630. rec->flags &= ~FTRACE_FL_FILTER;
  631. }
  632. pg = pg->next;
  633. }
  634. preempt_enable();
  635. }
  636. static int notrace
  637. ftrace_filter_open(struct inode *inode, struct file *file)
  638. {
  639. struct ftrace_iterator *iter;
  640. int ret = 0;
  641. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  642. if (!iter)
  643. return -ENOMEM;
  644. mutex_lock(&ftrace_filter_lock);
  645. if ((file->f_mode & FMODE_WRITE) &&
  646. !(file->f_flags & O_APPEND))
  647. ftrace_filter_reset();
  648. if (file->f_mode & FMODE_READ) {
  649. iter->pg = ftrace_pages_start;
  650. iter->pos = -1;
  651. iter->flags = FTRACE_ITER_FILTER;
  652. ret = seq_open(file, &show_ftrace_seq_ops);
  653. if (!ret) {
  654. struct seq_file *m = file->private_data;
  655. m->private = iter;
  656. } else
  657. kfree(iter);
  658. } else
  659. file->private_data = iter;
  660. mutex_unlock(&ftrace_filter_lock);
  661. return ret;
  662. }
  663. static ssize_t notrace
  664. ftrace_filter_read(struct file *file, char __user *ubuf,
  665. size_t cnt, loff_t *ppos)
  666. {
  667. if (file->f_mode & FMODE_READ)
  668. return seq_read(file, ubuf, cnt, ppos);
  669. else
  670. return -EPERM;
  671. }
  672. static loff_t notrace
  673. ftrace_filter_lseek(struct file *file, loff_t offset, int origin)
  674. {
  675. loff_t ret;
  676. if (file->f_mode & FMODE_READ)
  677. ret = seq_lseek(file, offset, origin);
  678. else
  679. file->f_pos = ret = 1;
  680. return ret;
  681. }
  682. enum {
  683. MATCH_FULL,
  684. MATCH_FRONT_ONLY,
  685. MATCH_MIDDLE_ONLY,
  686. MATCH_END_ONLY,
  687. };
  688. static void notrace
  689. ftrace_match(unsigned char *buff, int len)
  690. {
  691. char str[KSYM_SYMBOL_LEN];
  692. char *search = NULL;
  693. struct ftrace_page *pg;
  694. struct dyn_ftrace *rec;
  695. int type = MATCH_FULL;
  696. unsigned i, match = 0, search_len = 0;
  697. for (i = 0; i < len; i++) {
  698. if (buff[i] == '*') {
  699. if (!i) {
  700. search = buff + i + 1;
  701. type = MATCH_END_ONLY;
  702. search_len = len - (i + 1);
  703. } else {
  704. if (type == MATCH_END_ONLY) {
  705. type = MATCH_MIDDLE_ONLY;
  706. } else {
  707. match = i;
  708. type = MATCH_FRONT_ONLY;
  709. }
  710. buff[i] = 0;
  711. break;
  712. }
  713. }
  714. }
  715. /* keep kstop machine from running */
  716. preempt_disable();
  717. ftrace_filtered = 1;
  718. pg = ftrace_pages_start;
  719. while (pg) {
  720. for (i = 0; i < pg->index; i++) {
  721. int matched = 0;
  722. char *ptr;
  723. rec = &pg->records[i];
  724. if (rec->flags & FTRACE_FL_FAILED)
  725. continue;
  726. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  727. switch (type) {
  728. case MATCH_FULL:
  729. if (strcmp(str, buff) == 0)
  730. matched = 1;
  731. break;
  732. case MATCH_FRONT_ONLY:
  733. if (memcmp(str, buff, match) == 0)
  734. matched = 1;
  735. break;
  736. case MATCH_MIDDLE_ONLY:
  737. if (strstr(str, search))
  738. matched = 1;
  739. break;
  740. case MATCH_END_ONLY:
  741. ptr = strstr(str, search);
  742. if (ptr && (ptr[search_len] == 0))
  743. matched = 1;
  744. break;
  745. }
  746. if (matched)
  747. rec->flags |= FTRACE_FL_FILTER;
  748. }
  749. pg = pg->next;
  750. }
  751. preempt_enable();
  752. }
  753. static ssize_t notrace
  754. ftrace_filter_write(struct file *file, const char __user *ubuf,
  755. size_t cnt, loff_t *ppos)
  756. {
  757. struct ftrace_iterator *iter;
  758. char ch;
  759. size_t read = 0;
  760. ssize_t ret;
  761. if (!cnt || cnt < 0)
  762. return 0;
  763. mutex_lock(&ftrace_filter_lock);
  764. if (file->f_mode & FMODE_READ) {
  765. struct seq_file *m = file->private_data;
  766. iter = m->private;
  767. } else
  768. iter = file->private_data;
  769. if (!*ppos) {
  770. iter->flags &= ~FTRACE_ITER_CONT;
  771. iter->buffer_idx = 0;
  772. }
  773. ret = get_user(ch, ubuf++);
  774. if (ret)
  775. goto out;
  776. read++;
  777. cnt--;
  778. if (!(iter->flags & ~FTRACE_ITER_CONT)) {
  779. /* skip white space */
  780. while (cnt && isspace(ch)) {
  781. ret = get_user(ch, ubuf++);
  782. if (ret)
  783. goto out;
  784. read++;
  785. cnt--;
  786. }
  787. if (isspace(ch)) {
  788. file->f_pos += read;
  789. ret = read;
  790. goto out;
  791. }
  792. iter->buffer_idx = 0;
  793. }
  794. while (cnt && !isspace(ch)) {
  795. if (iter->buffer_idx < FTRACE_BUFF_MAX)
  796. iter->buffer[iter->buffer_idx++] = ch;
  797. else {
  798. ret = -EINVAL;
  799. goto out;
  800. }
  801. ret = get_user(ch, ubuf++);
  802. if (ret)
  803. goto out;
  804. read++;
  805. cnt--;
  806. }
  807. if (isspace(ch)) {
  808. iter->filtered++;
  809. iter->buffer[iter->buffer_idx] = 0;
  810. ftrace_match(iter->buffer, iter->buffer_idx);
  811. iter->buffer_idx = 0;
  812. } else
  813. iter->flags |= FTRACE_ITER_CONT;
  814. file->f_pos += read;
  815. ret = read;
  816. out:
  817. mutex_unlock(&ftrace_filter_lock);
  818. return ret;
  819. }
  820. static int notrace
  821. ftrace_filter_release(struct inode *inode, struct file *file)
  822. {
  823. struct seq_file *m = (struct seq_file *)file->private_data;
  824. struct ftrace_iterator *iter;
  825. mutex_lock(&ftrace_filter_lock);
  826. if (file->f_mode & FMODE_READ) {
  827. iter = m->private;
  828. seq_release(inode, file);
  829. } else
  830. iter = file->private_data;
  831. if (iter->buffer_idx) {
  832. iter->filtered++;
  833. iter->buffer[iter->buffer_idx] = 0;
  834. ftrace_match(iter->buffer, iter->buffer_idx);
  835. }
  836. mutex_lock(&ftrace_sysctl_lock);
  837. mutex_lock(&ftraced_lock);
  838. if (iter->filtered && ftraced_suspend && ftrace_enabled)
  839. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  840. mutex_unlock(&ftraced_lock);
  841. mutex_unlock(&ftrace_sysctl_lock);
  842. kfree(iter);
  843. mutex_unlock(&ftrace_filter_lock);
  844. return 0;
  845. }
  846. static struct file_operations ftrace_avail_fops = {
  847. .open = ftrace_avail_open,
  848. .read = seq_read,
  849. .llseek = seq_lseek,
  850. .release = ftrace_avail_release,
  851. };
  852. static struct file_operations ftrace_filter_fops = {
  853. .open = ftrace_filter_open,
  854. .read = ftrace_filter_read,
  855. .write = ftrace_filter_write,
  856. .llseek = ftrace_filter_lseek,
  857. .release = ftrace_filter_release,
  858. };
  859. /**
  860. * ftrace_force_update - force an update to all recording ftrace functions
  861. *
  862. * The ftrace dynamic update daemon only wakes up once a second.
  863. * There may be cases where an update needs to be done immediately
  864. * for tests or internal kernel tracing to begin. This function
  865. * wakes the daemon to do an update and will not return until the
  866. * update is complete.
  867. */
  868. int ftrace_force_update(void)
  869. {
  870. unsigned long last_counter;
  871. DECLARE_WAITQUEUE(wait, current);
  872. int ret = 0;
  873. if (!ftraced_task)
  874. return -ENODEV;
  875. mutex_lock(&ftraced_lock);
  876. last_counter = ftraced_iteration_counter;
  877. set_current_state(TASK_INTERRUPTIBLE);
  878. add_wait_queue(&ftraced_waiters, &wait);
  879. do {
  880. mutex_unlock(&ftraced_lock);
  881. wake_up_process(ftraced_task);
  882. schedule();
  883. mutex_lock(&ftraced_lock);
  884. if (signal_pending(current)) {
  885. ret = -EINTR;
  886. break;
  887. }
  888. set_current_state(TASK_INTERRUPTIBLE);
  889. } while (last_counter == ftraced_iteration_counter);
  890. mutex_unlock(&ftraced_lock);
  891. remove_wait_queue(&ftraced_waiters, &wait);
  892. set_current_state(TASK_RUNNING);
  893. return ret;
  894. }
  895. static __init int ftrace_init_debugfs(void)
  896. {
  897. struct dentry *d_tracer;
  898. struct dentry *entry;
  899. d_tracer = tracing_init_dentry();
  900. entry = debugfs_create_file("available_filter_functions", 0444,
  901. d_tracer, NULL, &ftrace_avail_fops);
  902. if (!entry)
  903. pr_warning("Could not create debugfs "
  904. "'available_filter_functions' entry\n");
  905. entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
  906. NULL, &ftrace_filter_fops);
  907. if (!entry)
  908. pr_warning("Could not create debugfs "
  909. "'set_ftrace_filter' entry\n");
  910. return 0;
  911. }
  912. fs_initcall(ftrace_init_debugfs);
  913. static int __init notrace ftrace_dynamic_init(void)
  914. {
  915. struct task_struct *p;
  916. unsigned long addr;
  917. int ret;
  918. addr = (unsigned long)ftrace_record_ip;
  919. stop_machine_run(ftrace_dyn_arch_init, &addr, NR_CPUS);
  920. /* ftrace_dyn_arch_init places the return code in addr */
  921. if (addr)
  922. return addr;
  923. ret = ftrace_dyn_table_alloc();
  924. if (ret)
  925. return ret;
  926. p = kthread_run(ftraced, NULL, "ftraced");
  927. if (IS_ERR(p))
  928. return -1;
  929. last_ftrace_enabled = ftrace_enabled = 1;
  930. ftraced_task = p;
  931. return 0;
  932. }
  933. core_initcall(ftrace_dynamic_init);
  934. #else
  935. # define ftrace_startup() do { } while (0)
  936. # define ftrace_shutdown() do { } while (0)
  937. # define ftrace_startup_sysctl() do { } while (0)
  938. # define ftrace_shutdown_sysctl() do { } while (0)
  939. #endif /* CONFIG_DYNAMIC_FTRACE */
  940. /**
  941. * register_ftrace_function - register a function for profiling
  942. * @ops - ops structure that holds the function for profiling.
  943. *
  944. * Register a function to be called by all functions in the
  945. * kernel.
  946. *
  947. * Note: @ops->func and all the functions it calls must be labeled
  948. * with "notrace", otherwise it will go into a
  949. * recursive loop.
  950. */
  951. int register_ftrace_function(struct ftrace_ops *ops)
  952. {
  953. int ret;
  954. mutex_lock(&ftrace_sysctl_lock);
  955. ret = __register_ftrace_function(ops);
  956. ftrace_startup();
  957. mutex_unlock(&ftrace_sysctl_lock);
  958. return ret;
  959. }
  960. /**
  961. * unregister_ftrace_function - unresgister a function for profiling.
  962. * @ops - ops structure that holds the function to unregister
  963. *
  964. * Unregister a function that was added to be called by ftrace profiling.
  965. */
  966. int unregister_ftrace_function(struct ftrace_ops *ops)
  967. {
  968. int ret;
  969. mutex_lock(&ftrace_sysctl_lock);
  970. ret = __unregister_ftrace_function(ops);
  971. ftrace_shutdown();
  972. mutex_unlock(&ftrace_sysctl_lock);
  973. return ret;
  974. }
  975. notrace int
  976. ftrace_enable_sysctl(struct ctl_table *table, int write,
  977. struct file *file, void __user *buffer, size_t *lenp,
  978. loff_t *ppos)
  979. {
  980. int ret;
  981. mutex_lock(&ftrace_sysctl_lock);
  982. ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
  983. if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
  984. goto out;
  985. last_ftrace_enabled = ftrace_enabled;
  986. if (ftrace_enabled) {
  987. ftrace_startup_sysctl();
  988. /* we are starting ftrace again */
  989. if (ftrace_list != &ftrace_list_end) {
  990. if (ftrace_list->next == &ftrace_list_end)
  991. ftrace_trace_function = ftrace_list->func;
  992. else
  993. ftrace_trace_function = ftrace_list_func;
  994. }
  995. } else {
  996. /* stopping ftrace calls (just send to ftrace_stub) */
  997. ftrace_trace_function = ftrace_stub;
  998. ftrace_shutdown_sysctl();
  999. }
  1000. out:
  1001. mutex_unlock(&ftrace_sysctl_lock);
  1002. return ret;
  1003. }