ftrace.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  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/kprobes.h>
  24. #include <linux/ftrace.h>
  25. #include <linux/sysctl.h>
  26. #include <linux/ctype.h>
  27. #include <linux/list.h>
  28. #include <asm/ftrace.h>
  29. #include "trace.h"
  30. #define FTRACE_WARN_ON(cond) \
  31. do { \
  32. if (WARN_ON(cond)) \
  33. ftrace_kill(); \
  34. } while (0)
  35. #define FTRACE_WARN_ON_ONCE(cond) \
  36. do { \
  37. if (WARN_ON_ONCE(cond)) \
  38. ftrace_kill(); \
  39. } while (0)
  40. /* ftrace_enabled is a method to turn ftrace on or off */
  41. int ftrace_enabled __read_mostly;
  42. static int last_ftrace_enabled;
  43. /* Quick disabling of function tracer. */
  44. int function_trace_stop;
  45. /*
  46. * ftrace_disabled is set when an anomaly is discovered.
  47. * ftrace_disabled is much stronger than ftrace_enabled.
  48. */
  49. static int ftrace_disabled __read_mostly;
  50. static DEFINE_SPINLOCK(ftrace_lock);
  51. static DEFINE_MUTEX(ftrace_sysctl_lock);
  52. static struct ftrace_ops ftrace_list_end __read_mostly =
  53. {
  54. .func = ftrace_stub,
  55. };
  56. static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
  57. ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
  58. ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
  59. static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
  60. {
  61. struct ftrace_ops *op = ftrace_list;
  62. /* in case someone actually ports this to alpha! */
  63. read_barrier_depends();
  64. while (op != &ftrace_list_end) {
  65. /* silly alpha */
  66. read_barrier_depends();
  67. op->func(ip, parent_ip);
  68. op = op->next;
  69. };
  70. }
  71. /**
  72. * clear_ftrace_function - reset the ftrace function
  73. *
  74. * This NULLs the ftrace function and in essence stops
  75. * tracing. There may be lag
  76. */
  77. void clear_ftrace_function(void)
  78. {
  79. ftrace_trace_function = ftrace_stub;
  80. __ftrace_trace_function = ftrace_stub;
  81. }
  82. #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  83. /*
  84. * For those archs that do not test ftrace_trace_stop in their
  85. * mcount call site, we need to do it from C.
  86. */
  87. static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
  88. {
  89. if (function_trace_stop)
  90. return;
  91. __ftrace_trace_function(ip, parent_ip);
  92. }
  93. #endif
  94. static int __register_ftrace_function(struct ftrace_ops *ops)
  95. {
  96. /* should not be called from interrupt context */
  97. spin_lock(&ftrace_lock);
  98. ops->next = ftrace_list;
  99. /*
  100. * We are entering ops into the ftrace_list but another
  101. * CPU might be walking that list. We need to make sure
  102. * the ops->next pointer is valid before another CPU sees
  103. * the ops pointer included into the ftrace_list.
  104. */
  105. smp_wmb();
  106. ftrace_list = ops;
  107. if (ftrace_enabled) {
  108. /*
  109. * For one func, simply call it directly.
  110. * For more than one func, call the chain.
  111. */
  112. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  113. if (ops->next == &ftrace_list_end)
  114. ftrace_trace_function = ops->func;
  115. else
  116. ftrace_trace_function = ftrace_list_func;
  117. #else
  118. if (ops->next == &ftrace_list_end)
  119. __ftrace_trace_function = ops->func;
  120. else
  121. __ftrace_trace_function = ftrace_list_func;
  122. ftrace_trace_function = ftrace_test_stop_func;
  123. #endif
  124. }
  125. spin_unlock(&ftrace_lock);
  126. return 0;
  127. }
  128. static int __unregister_ftrace_function(struct ftrace_ops *ops)
  129. {
  130. struct ftrace_ops **p;
  131. int ret = 0;
  132. /* should not be called from interrupt context */
  133. spin_lock(&ftrace_lock);
  134. /*
  135. * If we are removing the last function, then simply point
  136. * to the ftrace_stub.
  137. */
  138. if (ftrace_list == ops && ops->next == &ftrace_list_end) {
  139. ftrace_trace_function = ftrace_stub;
  140. ftrace_list = &ftrace_list_end;
  141. goto out;
  142. }
  143. for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
  144. if (*p == ops)
  145. break;
  146. if (*p != ops) {
  147. ret = -1;
  148. goto out;
  149. }
  150. *p = (*p)->next;
  151. if (ftrace_enabled) {
  152. /* If we only have one func left, then call that directly */
  153. if (ftrace_list->next == &ftrace_list_end)
  154. ftrace_trace_function = ftrace_list->func;
  155. }
  156. out:
  157. spin_unlock(&ftrace_lock);
  158. return ret;
  159. }
  160. #ifdef CONFIG_DYNAMIC_FTRACE
  161. #ifndef CONFIG_FTRACE_MCOUNT_RECORD
  162. # error Dynamic ftrace depends on MCOUNT_RECORD
  163. #endif
  164. /*
  165. * Since MCOUNT_ADDR may point to mcount itself, we do not want
  166. * to get it confused by reading a reference in the code as we
  167. * are parsing on objcopy output of text. Use a variable for
  168. * it instead.
  169. */
  170. static unsigned long mcount_addr = MCOUNT_ADDR;
  171. enum {
  172. FTRACE_ENABLE_CALLS = (1 << 0),
  173. FTRACE_DISABLE_CALLS = (1 << 1),
  174. FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
  175. FTRACE_ENABLE_MCOUNT = (1 << 3),
  176. FTRACE_DISABLE_MCOUNT = (1 << 4),
  177. };
  178. static int ftrace_filtered;
  179. static LIST_HEAD(ftrace_new_addrs);
  180. static DEFINE_MUTEX(ftrace_regex_lock);
  181. struct ftrace_page {
  182. struct ftrace_page *next;
  183. unsigned long index;
  184. struct dyn_ftrace records[];
  185. };
  186. #define ENTRIES_PER_PAGE \
  187. ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
  188. /* estimate from running different kernels */
  189. #define NR_TO_INIT 10000
  190. static struct ftrace_page *ftrace_pages_start;
  191. static struct ftrace_page *ftrace_pages;
  192. static struct dyn_ftrace *ftrace_free_records;
  193. #ifdef CONFIG_KPROBES
  194. static int frozen_record_count;
  195. static inline void freeze_record(struct dyn_ftrace *rec)
  196. {
  197. if (!(rec->flags & FTRACE_FL_FROZEN)) {
  198. rec->flags |= FTRACE_FL_FROZEN;
  199. frozen_record_count++;
  200. }
  201. }
  202. static inline void unfreeze_record(struct dyn_ftrace *rec)
  203. {
  204. if (rec->flags & FTRACE_FL_FROZEN) {
  205. rec->flags &= ~FTRACE_FL_FROZEN;
  206. frozen_record_count--;
  207. }
  208. }
  209. static inline int record_frozen(struct dyn_ftrace *rec)
  210. {
  211. return rec->flags & FTRACE_FL_FROZEN;
  212. }
  213. #else
  214. # define freeze_record(rec) ({ 0; })
  215. # define unfreeze_record(rec) ({ 0; })
  216. # define record_frozen(rec) ({ 0; })
  217. #endif /* CONFIG_KPROBES */
  218. static void ftrace_free_rec(struct dyn_ftrace *rec)
  219. {
  220. rec->ip = (unsigned long)ftrace_free_records;
  221. ftrace_free_records = rec;
  222. rec->flags |= FTRACE_FL_FREE;
  223. }
  224. void ftrace_release(void *start, unsigned long size)
  225. {
  226. struct dyn_ftrace *rec;
  227. struct ftrace_page *pg;
  228. unsigned long s = (unsigned long)start;
  229. unsigned long e = s + size;
  230. int i;
  231. if (ftrace_disabled || !start)
  232. return;
  233. /* should not be called from interrupt context */
  234. spin_lock(&ftrace_lock);
  235. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  236. for (i = 0; i < pg->index; i++) {
  237. rec = &pg->records[i];
  238. if ((rec->ip >= s) && (rec->ip < e))
  239. ftrace_free_rec(rec);
  240. }
  241. }
  242. spin_unlock(&ftrace_lock);
  243. }
  244. static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
  245. {
  246. struct dyn_ftrace *rec;
  247. /* First check for freed records */
  248. if (ftrace_free_records) {
  249. rec = ftrace_free_records;
  250. if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
  251. FTRACE_WARN_ON_ONCE(1);
  252. ftrace_free_records = NULL;
  253. return NULL;
  254. }
  255. ftrace_free_records = (void *)rec->ip;
  256. memset(rec, 0, sizeof(*rec));
  257. return rec;
  258. }
  259. if (ftrace_pages->index == ENTRIES_PER_PAGE) {
  260. if (!ftrace_pages->next) {
  261. /* allocate another page */
  262. ftrace_pages->next =
  263. (void *)get_zeroed_page(GFP_KERNEL);
  264. if (!ftrace_pages->next)
  265. return NULL;
  266. }
  267. ftrace_pages = ftrace_pages->next;
  268. }
  269. return &ftrace_pages->records[ftrace_pages->index++];
  270. }
  271. static struct dyn_ftrace *
  272. ftrace_record_ip(unsigned long ip)
  273. {
  274. struct dyn_ftrace *rec;
  275. if (!ftrace_enabled || ftrace_disabled)
  276. return NULL;
  277. rec = ftrace_alloc_dyn_node(ip);
  278. if (!rec)
  279. return NULL;
  280. rec->ip = ip;
  281. list_add(&rec->list, &ftrace_new_addrs);
  282. return rec;
  283. }
  284. #define FTRACE_ADDR ((long)(ftrace_caller))
  285. static int
  286. __ftrace_replace_code(struct dyn_ftrace *rec,
  287. unsigned char *old, unsigned char *new, int enable)
  288. {
  289. unsigned long ip, fl;
  290. ip = rec->ip;
  291. if (ftrace_filtered && enable) {
  292. /*
  293. * If filtering is on:
  294. *
  295. * If this record is set to be filtered and
  296. * is enabled then do nothing.
  297. *
  298. * If this record is set to be filtered and
  299. * it is not enabled, enable it.
  300. *
  301. * If this record is not set to be filtered
  302. * and it is not enabled do nothing.
  303. *
  304. * If this record is set not to trace then
  305. * do nothing.
  306. *
  307. * If this record is set not to trace and
  308. * it is enabled then disable it.
  309. *
  310. * If this record is not set to be filtered and
  311. * it is enabled, disable it.
  312. */
  313. fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE |
  314. FTRACE_FL_ENABLED);
  315. if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
  316. (fl == (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE)) ||
  317. !fl || (fl == FTRACE_FL_NOTRACE))
  318. return 0;
  319. /*
  320. * If it is enabled disable it,
  321. * otherwise enable it!
  322. */
  323. if (fl & FTRACE_FL_ENABLED) {
  324. /* swap new and old */
  325. new = old;
  326. old = ftrace_call_replace(ip, FTRACE_ADDR);
  327. rec->flags &= ~FTRACE_FL_ENABLED;
  328. } else {
  329. new = ftrace_call_replace(ip, FTRACE_ADDR);
  330. rec->flags |= FTRACE_FL_ENABLED;
  331. }
  332. } else {
  333. if (enable) {
  334. /*
  335. * If this record is set not to trace and is
  336. * not enabled, do nothing.
  337. */
  338. fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
  339. if (fl == FTRACE_FL_NOTRACE)
  340. return 0;
  341. new = ftrace_call_replace(ip, FTRACE_ADDR);
  342. } else
  343. old = ftrace_call_replace(ip, FTRACE_ADDR);
  344. if (enable) {
  345. if (rec->flags & FTRACE_FL_ENABLED)
  346. return 0;
  347. rec->flags |= FTRACE_FL_ENABLED;
  348. } else {
  349. if (!(rec->flags & FTRACE_FL_ENABLED))
  350. return 0;
  351. rec->flags &= ~FTRACE_FL_ENABLED;
  352. }
  353. }
  354. return ftrace_modify_code(ip, old, new);
  355. }
  356. static void ftrace_replace_code(int enable)
  357. {
  358. int i, failed;
  359. unsigned char *new = NULL, *old = NULL;
  360. struct dyn_ftrace *rec;
  361. struct ftrace_page *pg;
  362. if (enable)
  363. old = ftrace_nop_replace();
  364. else
  365. new = ftrace_nop_replace();
  366. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  367. for (i = 0; i < pg->index; i++) {
  368. rec = &pg->records[i];
  369. /* don't modify code that has already faulted */
  370. if (rec->flags & FTRACE_FL_FAILED)
  371. continue;
  372. /* ignore updates to this record's mcount site */
  373. if (get_kprobe((void *)rec->ip)) {
  374. freeze_record(rec);
  375. continue;
  376. } else {
  377. unfreeze_record(rec);
  378. }
  379. failed = __ftrace_replace_code(rec, old, new, enable);
  380. if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
  381. rec->flags |= FTRACE_FL_FAILED;
  382. if ((system_state == SYSTEM_BOOTING) ||
  383. !core_kernel_text(rec->ip)) {
  384. ftrace_free_rec(rec);
  385. }
  386. }
  387. }
  388. }
  389. }
  390. static void print_ip_ins(const char *fmt, unsigned char *p)
  391. {
  392. int i;
  393. printk(KERN_CONT "%s", fmt);
  394. for (i = 0; i < MCOUNT_INSN_SIZE; i++)
  395. printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
  396. }
  397. static int
  398. ftrace_code_disable(struct dyn_ftrace *rec)
  399. {
  400. unsigned long ip;
  401. unsigned char *nop, *call;
  402. int ret;
  403. ip = rec->ip;
  404. nop = ftrace_nop_replace();
  405. call = ftrace_call_replace(ip, mcount_addr);
  406. ret = ftrace_modify_code(ip, call, nop);
  407. if (ret) {
  408. switch (ret) {
  409. case -EFAULT:
  410. FTRACE_WARN_ON_ONCE(1);
  411. pr_info("ftrace faulted on modifying ");
  412. print_ip_sym(ip);
  413. break;
  414. case -EINVAL:
  415. FTRACE_WARN_ON_ONCE(1);
  416. pr_info("ftrace failed to modify ");
  417. print_ip_sym(ip);
  418. print_ip_ins(" expected: ", call);
  419. print_ip_ins(" actual: ", (unsigned char *)ip);
  420. print_ip_ins(" replace: ", nop);
  421. printk(KERN_CONT "\n");
  422. break;
  423. case -EPERM:
  424. FTRACE_WARN_ON_ONCE(1);
  425. pr_info("ftrace faulted on writing ");
  426. print_ip_sym(ip);
  427. break;
  428. default:
  429. FTRACE_WARN_ON_ONCE(1);
  430. pr_info("ftrace faulted on unknown error ");
  431. print_ip_sym(ip);
  432. }
  433. rec->flags |= FTRACE_FL_FAILED;
  434. return 0;
  435. }
  436. return 1;
  437. }
  438. static int __ftrace_modify_code(void *data)
  439. {
  440. int *command = data;
  441. if (*command & FTRACE_ENABLE_CALLS)
  442. ftrace_replace_code(1);
  443. else if (*command & FTRACE_DISABLE_CALLS)
  444. ftrace_replace_code(0);
  445. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  446. ftrace_update_ftrace_func(ftrace_trace_function);
  447. return 0;
  448. }
  449. static void ftrace_run_update_code(int command)
  450. {
  451. stop_machine(__ftrace_modify_code, &command, NULL);
  452. }
  453. static ftrace_func_t saved_ftrace_func;
  454. static int ftrace_start_up;
  455. static DEFINE_MUTEX(ftrace_start_lock);
  456. static void ftrace_startup(void)
  457. {
  458. int command = 0;
  459. if (unlikely(ftrace_disabled))
  460. return;
  461. mutex_lock(&ftrace_start_lock);
  462. ftrace_start_up++;
  463. if (ftrace_start_up == 1)
  464. command |= FTRACE_ENABLE_CALLS;
  465. if (saved_ftrace_func != ftrace_trace_function) {
  466. saved_ftrace_func = ftrace_trace_function;
  467. command |= FTRACE_UPDATE_TRACE_FUNC;
  468. }
  469. if (!command || !ftrace_enabled)
  470. goto out;
  471. ftrace_run_update_code(command);
  472. out:
  473. mutex_unlock(&ftrace_start_lock);
  474. }
  475. static void ftrace_shutdown(void)
  476. {
  477. int command = 0;
  478. if (unlikely(ftrace_disabled))
  479. return;
  480. mutex_lock(&ftrace_start_lock);
  481. ftrace_start_up--;
  482. if (!ftrace_start_up)
  483. command |= FTRACE_DISABLE_CALLS;
  484. if (saved_ftrace_func != ftrace_trace_function) {
  485. saved_ftrace_func = ftrace_trace_function;
  486. command |= FTRACE_UPDATE_TRACE_FUNC;
  487. }
  488. if (!command || !ftrace_enabled)
  489. goto out;
  490. ftrace_run_update_code(command);
  491. out:
  492. mutex_unlock(&ftrace_start_lock);
  493. }
  494. static void ftrace_startup_sysctl(void)
  495. {
  496. int command = FTRACE_ENABLE_MCOUNT;
  497. if (unlikely(ftrace_disabled))
  498. return;
  499. mutex_lock(&ftrace_start_lock);
  500. /* Force update next time */
  501. saved_ftrace_func = NULL;
  502. /* ftrace_start_up is true if we want ftrace running */
  503. if (ftrace_start_up)
  504. command |= FTRACE_ENABLE_CALLS;
  505. ftrace_run_update_code(command);
  506. mutex_unlock(&ftrace_start_lock);
  507. }
  508. static void ftrace_shutdown_sysctl(void)
  509. {
  510. int command = FTRACE_DISABLE_MCOUNT;
  511. if (unlikely(ftrace_disabled))
  512. return;
  513. mutex_lock(&ftrace_start_lock);
  514. /* ftrace_start_up is true if ftrace is running */
  515. if (ftrace_start_up)
  516. command |= FTRACE_DISABLE_CALLS;
  517. ftrace_run_update_code(command);
  518. mutex_unlock(&ftrace_start_lock);
  519. }
  520. static cycle_t ftrace_update_time;
  521. static unsigned long ftrace_update_cnt;
  522. unsigned long ftrace_update_tot_cnt;
  523. static int ftrace_update_code(void)
  524. {
  525. struct dyn_ftrace *p, *t;
  526. cycle_t start, stop;
  527. start = ftrace_now(raw_smp_processor_id());
  528. ftrace_update_cnt = 0;
  529. list_for_each_entry_safe(p, t, &ftrace_new_addrs, list) {
  530. /* If something went wrong, bail without enabling anything */
  531. if (unlikely(ftrace_disabled))
  532. return -1;
  533. list_del_init(&p->list);
  534. /* convert record (i.e, patch mcount-call with NOP) */
  535. if (ftrace_code_disable(p)) {
  536. p->flags |= FTRACE_FL_CONVERTED;
  537. ftrace_update_cnt++;
  538. } else
  539. ftrace_free_rec(p);
  540. }
  541. stop = ftrace_now(raw_smp_processor_id());
  542. ftrace_update_time = stop - start;
  543. ftrace_update_tot_cnt += ftrace_update_cnt;
  544. return 0;
  545. }
  546. static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
  547. {
  548. struct ftrace_page *pg;
  549. int cnt;
  550. int i;
  551. /* allocate a few pages */
  552. ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
  553. if (!ftrace_pages_start)
  554. return -1;
  555. /*
  556. * Allocate a few more pages.
  557. *
  558. * TODO: have some parser search vmlinux before
  559. * final linking to find all calls to ftrace.
  560. * Then we can:
  561. * a) know how many pages to allocate.
  562. * and/or
  563. * b) set up the table then.
  564. *
  565. * The dynamic code is still necessary for
  566. * modules.
  567. */
  568. pg = ftrace_pages = ftrace_pages_start;
  569. cnt = num_to_init / ENTRIES_PER_PAGE;
  570. pr_info("ftrace: allocating %ld entries in %d pages\n",
  571. num_to_init, cnt);
  572. for (i = 0; i < cnt; i++) {
  573. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  574. /* If we fail, we'll try later anyway */
  575. if (!pg->next)
  576. break;
  577. pg = pg->next;
  578. }
  579. return 0;
  580. }
  581. enum {
  582. FTRACE_ITER_FILTER = (1 << 0),
  583. FTRACE_ITER_CONT = (1 << 1),
  584. FTRACE_ITER_NOTRACE = (1 << 2),
  585. FTRACE_ITER_FAILURES = (1 << 3),
  586. };
  587. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  588. struct ftrace_iterator {
  589. loff_t pos;
  590. struct ftrace_page *pg;
  591. unsigned idx;
  592. unsigned flags;
  593. unsigned char buffer[FTRACE_BUFF_MAX+1];
  594. unsigned buffer_idx;
  595. unsigned filtered;
  596. };
  597. static void *
  598. t_next(struct seq_file *m, void *v, loff_t *pos)
  599. {
  600. struct ftrace_iterator *iter = m->private;
  601. struct dyn_ftrace *rec = NULL;
  602. (*pos)++;
  603. /* should not be called from interrupt context */
  604. spin_lock(&ftrace_lock);
  605. retry:
  606. if (iter->idx >= iter->pg->index) {
  607. if (iter->pg->next) {
  608. iter->pg = iter->pg->next;
  609. iter->idx = 0;
  610. goto retry;
  611. }
  612. } else {
  613. rec = &iter->pg->records[iter->idx++];
  614. if ((rec->flags & FTRACE_FL_FREE) ||
  615. (!(iter->flags & FTRACE_ITER_FAILURES) &&
  616. (rec->flags & FTRACE_FL_FAILED)) ||
  617. ((iter->flags & FTRACE_ITER_FAILURES) &&
  618. !(rec->flags & FTRACE_FL_FAILED)) ||
  619. ((iter->flags & FTRACE_ITER_FILTER) &&
  620. !(rec->flags & FTRACE_FL_FILTER)) ||
  621. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  622. !(rec->flags & FTRACE_FL_NOTRACE))) {
  623. rec = NULL;
  624. goto retry;
  625. }
  626. }
  627. spin_unlock(&ftrace_lock);
  628. iter->pos = *pos;
  629. return rec;
  630. }
  631. static void *t_start(struct seq_file *m, loff_t *pos)
  632. {
  633. struct ftrace_iterator *iter = m->private;
  634. void *p = NULL;
  635. loff_t l = -1;
  636. if (*pos != iter->pos) {
  637. for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
  638. ;
  639. } else {
  640. l = *pos;
  641. p = t_next(m, p, &l);
  642. }
  643. return p;
  644. }
  645. static void t_stop(struct seq_file *m, void *p)
  646. {
  647. }
  648. static int t_show(struct seq_file *m, void *v)
  649. {
  650. struct dyn_ftrace *rec = v;
  651. char str[KSYM_SYMBOL_LEN];
  652. if (!rec)
  653. return 0;
  654. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  655. seq_printf(m, "%s\n", str);
  656. return 0;
  657. }
  658. static struct seq_operations show_ftrace_seq_ops = {
  659. .start = t_start,
  660. .next = t_next,
  661. .stop = t_stop,
  662. .show = t_show,
  663. };
  664. static int
  665. ftrace_avail_open(struct inode *inode, struct file *file)
  666. {
  667. struct ftrace_iterator *iter;
  668. int ret;
  669. if (unlikely(ftrace_disabled))
  670. return -ENODEV;
  671. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  672. if (!iter)
  673. return -ENOMEM;
  674. iter->pg = ftrace_pages_start;
  675. iter->pos = -1;
  676. ret = seq_open(file, &show_ftrace_seq_ops);
  677. if (!ret) {
  678. struct seq_file *m = file->private_data;
  679. m->private = iter;
  680. } else {
  681. kfree(iter);
  682. }
  683. return ret;
  684. }
  685. int ftrace_avail_release(struct inode *inode, struct file *file)
  686. {
  687. struct seq_file *m = (struct seq_file *)file->private_data;
  688. struct ftrace_iterator *iter = m->private;
  689. seq_release(inode, file);
  690. kfree(iter);
  691. return 0;
  692. }
  693. static int
  694. ftrace_failures_open(struct inode *inode, struct file *file)
  695. {
  696. int ret;
  697. struct seq_file *m;
  698. struct ftrace_iterator *iter;
  699. ret = ftrace_avail_open(inode, file);
  700. if (!ret) {
  701. m = (struct seq_file *)file->private_data;
  702. iter = (struct ftrace_iterator *)m->private;
  703. iter->flags = FTRACE_ITER_FAILURES;
  704. }
  705. return ret;
  706. }
  707. static void ftrace_filter_reset(int enable)
  708. {
  709. struct ftrace_page *pg;
  710. struct dyn_ftrace *rec;
  711. unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  712. unsigned i;
  713. /* should not be called from interrupt context */
  714. spin_lock(&ftrace_lock);
  715. if (enable)
  716. ftrace_filtered = 0;
  717. pg = ftrace_pages_start;
  718. while (pg) {
  719. for (i = 0; i < pg->index; i++) {
  720. rec = &pg->records[i];
  721. if (rec->flags & FTRACE_FL_FAILED)
  722. continue;
  723. rec->flags &= ~type;
  724. }
  725. pg = pg->next;
  726. }
  727. spin_unlock(&ftrace_lock);
  728. }
  729. static int
  730. ftrace_regex_open(struct inode *inode, struct file *file, int enable)
  731. {
  732. struct ftrace_iterator *iter;
  733. int ret = 0;
  734. if (unlikely(ftrace_disabled))
  735. return -ENODEV;
  736. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  737. if (!iter)
  738. return -ENOMEM;
  739. mutex_lock(&ftrace_regex_lock);
  740. if ((file->f_mode & FMODE_WRITE) &&
  741. !(file->f_flags & O_APPEND))
  742. ftrace_filter_reset(enable);
  743. if (file->f_mode & FMODE_READ) {
  744. iter->pg = ftrace_pages_start;
  745. iter->pos = -1;
  746. iter->flags = enable ? FTRACE_ITER_FILTER :
  747. FTRACE_ITER_NOTRACE;
  748. ret = seq_open(file, &show_ftrace_seq_ops);
  749. if (!ret) {
  750. struct seq_file *m = file->private_data;
  751. m->private = iter;
  752. } else
  753. kfree(iter);
  754. } else
  755. file->private_data = iter;
  756. mutex_unlock(&ftrace_regex_lock);
  757. return ret;
  758. }
  759. static int
  760. ftrace_filter_open(struct inode *inode, struct file *file)
  761. {
  762. return ftrace_regex_open(inode, file, 1);
  763. }
  764. static int
  765. ftrace_notrace_open(struct inode *inode, struct file *file)
  766. {
  767. return ftrace_regex_open(inode, file, 0);
  768. }
  769. static ssize_t
  770. ftrace_regex_read(struct file *file, char __user *ubuf,
  771. size_t cnt, loff_t *ppos)
  772. {
  773. if (file->f_mode & FMODE_READ)
  774. return seq_read(file, ubuf, cnt, ppos);
  775. else
  776. return -EPERM;
  777. }
  778. static loff_t
  779. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  780. {
  781. loff_t ret;
  782. if (file->f_mode & FMODE_READ)
  783. ret = seq_lseek(file, offset, origin);
  784. else
  785. file->f_pos = ret = 1;
  786. return ret;
  787. }
  788. enum {
  789. MATCH_FULL,
  790. MATCH_FRONT_ONLY,
  791. MATCH_MIDDLE_ONLY,
  792. MATCH_END_ONLY,
  793. };
  794. static void
  795. ftrace_match(unsigned char *buff, int len, int enable)
  796. {
  797. char str[KSYM_SYMBOL_LEN];
  798. char *search = NULL;
  799. struct ftrace_page *pg;
  800. struct dyn_ftrace *rec;
  801. int type = MATCH_FULL;
  802. unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  803. unsigned i, match = 0, search_len = 0;
  804. for (i = 0; i < len; i++) {
  805. if (buff[i] == '*') {
  806. if (!i) {
  807. search = buff + i + 1;
  808. type = MATCH_END_ONLY;
  809. search_len = len - (i + 1);
  810. } else {
  811. if (type == MATCH_END_ONLY) {
  812. type = MATCH_MIDDLE_ONLY;
  813. } else {
  814. match = i;
  815. type = MATCH_FRONT_ONLY;
  816. }
  817. buff[i] = 0;
  818. break;
  819. }
  820. }
  821. }
  822. /* should not be called from interrupt context */
  823. spin_lock(&ftrace_lock);
  824. if (enable)
  825. ftrace_filtered = 1;
  826. pg = ftrace_pages_start;
  827. while (pg) {
  828. for (i = 0; i < pg->index; i++) {
  829. int matched = 0;
  830. char *ptr;
  831. rec = &pg->records[i];
  832. if (rec->flags & FTRACE_FL_FAILED)
  833. continue;
  834. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  835. switch (type) {
  836. case MATCH_FULL:
  837. if (strcmp(str, buff) == 0)
  838. matched = 1;
  839. break;
  840. case MATCH_FRONT_ONLY:
  841. if (memcmp(str, buff, match) == 0)
  842. matched = 1;
  843. break;
  844. case MATCH_MIDDLE_ONLY:
  845. if (strstr(str, search))
  846. matched = 1;
  847. break;
  848. case MATCH_END_ONLY:
  849. ptr = strstr(str, search);
  850. if (ptr && (ptr[search_len] == 0))
  851. matched = 1;
  852. break;
  853. }
  854. if (matched)
  855. rec->flags |= flag;
  856. }
  857. pg = pg->next;
  858. }
  859. spin_unlock(&ftrace_lock);
  860. }
  861. static ssize_t
  862. ftrace_regex_write(struct file *file, const char __user *ubuf,
  863. size_t cnt, loff_t *ppos, int enable)
  864. {
  865. struct ftrace_iterator *iter;
  866. char ch;
  867. size_t read = 0;
  868. ssize_t ret;
  869. if (!cnt || cnt < 0)
  870. return 0;
  871. mutex_lock(&ftrace_regex_lock);
  872. if (file->f_mode & FMODE_READ) {
  873. struct seq_file *m = file->private_data;
  874. iter = m->private;
  875. } else
  876. iter = file->private_data;
  877. if (!*ppos) {
  878. iter->flags &= ~FTRACE_ITER_CONT;
  879. iter->buffer_idx = 0;
  880. }
  881. ret = get_user(ch, ubuf++);
  882. if (ret)
  883. goto out;
  884. read++;
  885. cnt--;
  886. if (!(iter->flags & ~FTRACE_ITER_CONT)) {
  887. /* skip white space */
  888. while (cnt && isspace(ch)) {
  889. ret = get_user(ch, ubuf++);
  890. if (ret)
  891. goto out;
  892. read++;
  893. cnt--;
  894. }
  895. if (isspace(ch)) {
  896. file->f_pos += read;
  897. ret = read;
  898. goto out;
  899. }
  900. iter->buffer_idx = 0;
  901. }
  902. while (cnt && !isspace(ch)) {
  903. if (iter->buffer_idx < FTRACE_BUFF_MAX)
  904. iter->buffer[iter->buffer_idx++] = ch;
  905. else {
  906. ret = -EINVAL;
  907. goto out;
  908. }
  909. ret = get_user(ch, ubuf++);
  910. if (ret)
  911. goto out;
  912. read++;
  913. cnt--;
  914. }
  915. if (isspace(ch)) {
  916. iter->filtered++;
  917. iter->buffer[iter->buffer_idx] = 0;
  918. ftrace_match(iter->buffer, iter->buffer_idx, enable);
  919. iter->buffer_idx = 0;
  920. } else
  921. iter->flags |= FTRACE_ITER_CONT;
  922. file->f_pos += read;
  923. ret = read;
  924. out:
  925. mutex_unlock(&ftrace_regex_lock);
  926. return ret;
  927. }
  928. static ssize_t
  929. ftrace_filter_write(struct file *file, const char __user *ubuf,
  930. size_t cnt, loff_t *ppos)
  931. {
  932. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  933. }
  934. static ssize_t
  935. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  936. size_t cnt, loff_t *ppos)
  937. {
  938. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  939. }
  940. static void
  941. ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
  942. {
  943. if (unlikely(ftrace_disabled))
  944. return;
  945. mutex_lock(&ftrace_regex_lock);
  946. if (reset)
  947. ftrace_filter_reset(enable);
  948. if (buf)
  949. ftrace_match(buf, len, enable);
  950. mutex_unlock(&ftrace_regex_lock);
  951. }
  952. /**
  953. * ftrace_set_filter - set a function to filter on in ftrace
  954. * @buf - the string that holds the function filter text.
  955. * @len - the length of the string.
  956. * @reset - non zero to reset all filters before applying this filter.
  957. *
  958. * Filters denote which functions should be enabled when tracing is enabled.
  959. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  960. */
  961. void ftrace_set_filter(unsigned char *buf, int len, int reset)
  962. {
  963. ftrace_set_regex(buf, len, reset, 1);
  964. }
  965. /**
  966. * ftrace_set_notrace - set a function to not trace in ftrace
  967. * @buf - the string that holds the function notrace text.
  968. * @len - the length of the string.
  969. * @reset - non zero to reset all filters before applying this filter.
  970. *
  971. * Notrace Filters denote which functions should not be enabled when tracing
  972. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  973. * for tracing.
  974. */
  975. void ftrace_set_notrace(unsigned char *buf, int len, int reset)
  976. {
  977. ftrace_set_regex(buf, len, reset, 0);
  978. }
  979. static int
  980. ftrace_regex_release(struct inode *inode, struct file *file, int enable)
  981. {
  982. struct seq_file *m = (struct seq_file *)file->private_data;
  983. struct ftrace_iterator *iter;
  984. mutex_lock(&ftrace_regex_lock);
  985. if (file->f_mode & FMODE_READ) {
  986. iter = m->private;
  987. seq_release(inode, file);
  988. } else
  989. iter = file->private_data;
  990. if (iter->buffer_idx) {
  991. iter->filtered++;
  992. iter->buffer[iter->buffer_idx] = 0;
  993. ftrace_match(iter->buffer, iter->buffer_idx, enable);
  994. }
  995. mutex_lock(&ftrace_sysctl_lock);
  996. mutex_lock(&ftrace_start_lock);
  997. if (iter->filtered && ftrace_start_up && ftrace_enabled)
  998. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  999. mutex_unlock(&ftrace_start_lock);
  1000. mutex_unlock(&ftrace_sysctl_lock);
  1001. kfree(iter);
  1002. mutex_unlock(&ftrace_regex_lock);
  1003. return 0;
  1004. }
  1005. static int
  1006. ftrace_filter_release(struct inode *inode, struct file *file)
  1007. {
  1008. return ftrace_regex_release(inode, file, 1);
  1009. }
  1010. static int
  1011. ftrace_notrace_release(struct inode *inode, struct file *file)
  1012. {
  1013. return ftrace_regex_release(inode, file, 0);
  1014. }
  1015. static struct file_operations ftrace_avail_fops = {
  1016. .open = ftrace_avail_open,
  1017. .read = seq_read,
  1018. .llseek = seq_lseek,
  1019. .release = ftrace_avail_release,
  1020. };
  1021. static struct file_operations ftrace_failures_fops = {
  1022. .open = ftrace_failures_open,
  1023. .read = seq_read,
  1024. .llseek = seq_lseek,
  1025. .release = ftrace_avail_release,
  1026. };
  1027. static struct file_operations ftrace_filter_fops = {
  1028. .open = ftrace_filter_open,
  1029. .read = ftrace_regex_read,
  1030. .write = ftrace_filter_write,
  1031. .llseek = ftrace_regex_lseek,
  1032. .release = ftrace_filter_release,
  1033. };
  1034. static struct file_operations ftrace_notrace_fops = {
  1035. .open = ftrace_notrace_open,
  1036. .read = ftrace_regex_read,
  1037. .write = ftrace_notrace_write,
  1038. .llseek = ftrace_regex_lseek,
  1039. .release = ftrace_notrace_release,
  1040. };
  1041. static __init int ftrace_init_debugfs(void)
  1042. {
  1043. struct dentry *d_tracer;
  1044. struct dentry *entry;
  1045. d_tracer = tracing_init_dentry();
  1046. entry = debugfs_create_file("available_filter_functions", 0444,
  1047. d_tracer, NULL, &ftrace_avail_fops);
  1048. if (!entry)
  1049. pr_warning("Could not create debugfs "
  1050. "'available_filter_functions' entry\n");
  1051. entry = debugfs_create_file("failures", 0444,
  1052. d_tracer, NULL, &ftrace_failures_fops);
  1053. if (!entry)
  1054. pr_warning("Could not create debugfs 'failures' entry\n");
  1055. entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
  1056. NULL, &ftrace_filter_fops);
  1057. if (!entry)
  1058. pr_warning("Could not create debugfs "
  1059. "'set_ftrace_filter' entry\n");
  1060. entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
  1061. NULL, &ftrace_notrace_fops);
  1062. if (!entry)
  1063. pr_warning("Could not create debugfs "
  1064. "'set_ftrace_notrace' entry\n");
  1065. return 0;
  1066. }
  1067. fs_initcall(ftrace_init_debugfs);
  1068. static int ftrace_convert_nops(unsigned long *start,
  1069. unsigned long *end)
  1070. {
  1071. unsigned long *p;
  1072. unsigned long addr;
  1073. unsigned long flags;
  1074. mutex_lock(&ftrace_start_lock);
  1075. p = start;
  1076. while (p < end) {
  1077. addr = ftrace_call_adjust(*p++);
  1078. ftrace_record_ip(addr);
  1079. }
  1080. /* disable interrupts to prevent kstop machine */
  1081. local_irq_save(flags);
  1082. ftrace_update_code();
  1083. local_irq_restore(flags);
  1084. mutex_unlock(&ftrace_start_lock);
  1085. return 0;
  1086. }
  1087. void ftrace_init_module(unsigned long *start, unsigned long *end)
  1088. {
  1089. if (ftrace_disabled || start == end)
  1090. return;
  1091. ftrace_convert_nops(start, end);
  1092. }
  1093. extern unsigned long __start_mcount_loc[];
  1094. extern unsigned long __stop_mcount_loc[];
  1095. void __init ftrace_init(void)
  1096. {
  1097. unsigned long count, addr, flags;
  1098. int ret;
  1099. /* Keep the ftrace pointer to the stub */
  1100. addr = (unsigned long)ftrace_stub;
  1101. local_irq_save(flags);
  1102. ftrace_dyn_arch_init(&addr);
  1103. local_irq_restore(flags);
  1104. /* ftrace_dyn_arch_init places the return code in addr */
  1105. if (addr)
  1106. goto failed;
  1107. count = __stop_mcount_loc - __start_mcount_loc;
  1108. ret = ftrace_dyn_table_alloc(count);
  1109. if (ret)
  1110. goto failed;
  1111. last_ftrace_enabled = ftrace_enabled = 1;
  1112. ret = ftrace_convert_nops(__start_mcount_loc,
  1113. __stop_mcount_loc);
  1114. return;
  1115. failed:
  1116. ftrace_disabled = 1;
  1117. }
  1118. #else
  1119. static int __init ftrace_nodyn_init(void)
  1120. {
  1121. ftrace_enabled = 1;
  1122. return 0;
  1123. }
  1124. device_initcall(ftrace_nodyn_init);
  1125. # define ftrace_startup() do { } while (0)
  1126. # define ftrace_shutdown() do { } while (0)
  1127. # define ftrace_startup_sysctl() do { } while (0)
  1128. # define ftrace_shutdown_sysctl() do { } while (0)
  1129. #endif /* CONFIG_DYNAMIC_FTRACE */
  1130. /**
  1131. * ftrace_kill - kill ftrace
  1132. *
  1133. * This function should be used by panic code. It stops ftrace
  1134. * but in a not so nice way. If you need to simply kill ftrace
  1135. * from a non-atomic section, use ftrace_kill.
  1136. */
  1137. void ftrace_kill(void)
  1138. {
  1139. ftrace_disabled = 1;
  1140. ftrace_enabled = 0;
  1141. clear_ftrace_function();
  1142. }
  1143. /**
  1144. * register_ftrace_function - register a function for profiling
  1145. * @ops - ops structure that holds the function for profiling.
  1146. *
  1147. * Register a function to be called by all functions in the
  1148. * kernel.
  1149. *
  1150. * Note: @ops->func and all the functions it calls must be labeled
  1151. * with "notrace", otherwise it will go into a
  1152. * recursive loop.
  1153. */
  1154. int register_ftrace_function(struct ftrace_ops *ops)
  1155. {
  1156. int ret;
  1157. if (unlikely(ftrace_disabled))
  1158. return -1;
  1159. mutex_lock(&ftrace_sysctl_lock);
  1160. ret = __register_ftrace_function(ops);
  1161. ftrace_startup();
  1162. mutex_unlock(&ftrace_sysctl_lock);
  1163. return ret;
  1164. }
  1165. /**
  1166. * unregister_ftrace_function - unresgister a function for profiling.
  1167. * @ops - ops structure that holds the function to unregister
  1168. *
  1169. * Unregister a function that was added to be called by ftrace profiling.
  1170. */
  1171. int unregister_ftrace_function(struct ftrace_ops *ops)
  1172. {
  1173. int ret;
  1174. mutex_lock(&ftrace_sysctl_lock);
  1175. ret = __unregister_ftrace_function(ops);
  1176. ftrace_shutdown();
  1177. mutex_unlock(&ftrace_sysctl_lock);
  1178. return ret;
  1179. }
  1180. int
  1181. ftrace_enable_sysctl(struct ctl_table *table, int write,
  1182. struct file *file, void __user *buffer, size_t *lenp,
  1183. loff_t *ppos)
  1184. {
  1185. int ret;
  1186. if (unlikely(ftrace_disabled))
  1187. return -ENODEV;
  1188. mutex_lock(&ftrace_sysctl_lock);
  1189. ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
  1190. if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
  1191. goto out;
  1192. last_ftrace_enabled = ftrace_enabled;
  1193. if (ftrace_enabled) {
  1194. ftrace_startup_sysctl();
  1195. /* we are starting ftrace again */
  1196. if (ftrace_list != &ftrace_list_end) {
  1197. if (ftrace_list->next == &ftrace_list_end)
  1198. ftrace_trace_function = ftrace_list->func;
  1199. else
  1200. ftrace_trace_function = ftrace_list_func;
  1201. }
  1202. } else {
  1203. /* stopping ftrace calls (just send to ftrace_stub) */
  1204. ftrace_trace_function = ftrace_stub;
  1205. ftrace_shutdown_sysctl();
  1206. }
  1207. out:
  1208. mutex_unlock(&ftrace_sysctl_lock);
  1209. return ret;
  1210. }
  1211. #ifdef CONFIG_FUNCTION_RET_TRACER
  1212. trace_function_return_t ftrace_function_return =
  1213. (trace_function_return_t)ftrace_stub;
  1214. void register_ftrace_return(trace_function_return_t func)
  1215. {
  1216. ftrace_function_return = func;
  1217. }
  1218. void unregister_ftrace_return(void)
  1219. {
  1220. ftrace_function_return = (trace_function_return_t)ftrace_stub;
  1221. }
  1222. #endif