ftrace.c 32 KB

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