ftrace.c 32 KB

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