ftrace.c 29 KB

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