ftrace.c 29 KB

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