ftrace.c 32 KB

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