ftrace.c 29 KB

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