ftrace.c 31 KB

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