ftrace.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  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/ftrace.h>
  24. #include <linux/sysctl.h>
  25. #include <linux/ctype.h>
  26. #include <linux/hash.h>
  27. #include <linux/list.h>
  28. #include "trace.h"
  29. /* ftrace_enabled is a method to turn ftrace on or off */
  30. int ftrace_enabled __read_mostly;
  31. static int last_ftrace_enabled;
  32. /*
  33. * ftrace_disabled is set when an anomaly is discovered.
  34. * ftrace_disabled is much stronger than ftrace_enabled.
  35. */
  36. static int ftrace_disabled __read_mostly;
  37. static DEFINE_SPINLOCK(ftrace_lock);
  38. static DEFINE_MUTEX(ftrace_sysctl_lock);
  39. static struct ftrace_ops ftrace_list_end __read_mostly =
  40. {
  41. .func = ftrace_stub,
  42. };
  43. static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
  44. ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
  45. void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
  46. {
  47. struct ftrace_ops *op = ftrace_list;
  48. /* in case someone actually ports this to alpha! */
  49. read_barrier_depends();
  50. while (op != &ftrace_list_end) {
  51. /* silly alpha */
  52. read_barrier_depends();
  53. op->func(ip, parent_ip);
  54. op = op->next;
  55. };
  56. }
  57. /**
  58. * clear_ftrace_function - reset the ftrace function
  59. *
  60. * This NULLs the ftrace function and in essence stops
  61. * tracing. There may be lag
  62. */
  63. void clear_ftrace_function(void)
  64. {
  65. ftrace_trace_function = ftrace_stub;
  66. }
  67. static int __register_ftrace_function(struct ftrace_ops *ops)
  68. {
  69. /* Should never be called by interrupts */
  70. spin_lock(&ftrace_lock);
  71. ops->next = ftrace_list;
  72. /*
  73. * We are entering ops into the ftrace_list but another
  74. * CPU might be walking that list. We need to make sure
  75. * the ops->next pointer is valid before another CPU sees
  76. * the ops pointer included into the ftrace_list.
  77. */
  78. smp_wmb();
  79. ftrace_list = ops;
  80. if (ftrace_enabled) {
  81. /*
  82. * For one func, simply call it directly.
  83. * For more than one func, call the chain.
  84. */
  85. if (ops->next == &ftrace_list_end)
  86. ftrace_trace_function = ops->func;
  87. else
  88. ftrace_trace_function = ftrace_list_func;
  89. }
  90. spin_unlock(&ftrace_lock);
  91. return 0;
  92. }
  93. static int __unregister_ftrace_function(struct ftrace_ops *ops)
  94. {
  95. struct ftrace_ops **p;
  96. int ret = 0;
  97. spin_lock(&ftrace_lock);
  98. /*
  99. * If we are removing the last function, then simply point
  100. * to the ftrace_stub.
  101. */
  102. if (ftrace_list == ops && ops->next == &ftrace_list_end) {
  103. ftrace_trace_function = ftrace_stub;
  104. ftrace_list = &ftrace_list_end;
  105. goto out;
  106. }
  107. for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
  108. if (*p == ops)
  109. break;
  110. if (*p != ops) {
  111. ret = -1;
  112. goto out;
  113. }
  114. *p = (*p)->next;
  115. if (ftrace_enabled) {
  116. /* If we only have one func left, then call that directly */
  117. if (ftrace_list == &ftrace_list_end ||
  118. ftrace_list->next == &ftrace_list_end)
  119. ftrace_trace_function = ftrace_list->func;
  120. }
  121. out:
  122. spin_unlock(&ftrace_lock);
  123. return ret;
  124. }
  125. #ifdef CONFIG_DYNAMIC_FTRACE
  126. static struct task_struct *ftraced_task;
  127. static DECLARE_WAIT_QUEUE_HEAD(ftraced_waiters);
  128. static unsigned long ftraced_iteration_counter;
  129. enum {
  130. FTRACE_ENABLE_CALLS = (1 << 0),
  131. FTRACE_DISABLE_CALLS = (1 << 1),
  132. FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
  133. FTRACE_ENABLE_MCOUNT = (1 << 3),
  134. FTRACE_DISABLE_MCOUNT = (1 << 4),
  135. };
  136. static int ftrace_filtered;
  137. static struct hlist_head ftrace_hash[FTRACE_HASHSIZE];
  138. static DEFINE_PER_CPU(int, ftrace_shutdown_disable_cpu);
  139. static DEFINE_SPINLOCK(ftrace_shutdown_lock);
  140. static DEFINE_MUTEX(ftraced_lock);
  141. static DEFINE_MUTEX(ftrace_regex_lock);
  142. struct ftrace_page {
  143. struct ftrace_page *next;
  144. unsigned long index;
  145. struct dyn_ftrace records[];
  146. };
  147. #define ENTRIES_PER_PAGE \
  148. ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
  149. /* estimate from running different kernels */
  150. #define NR_TO_INIT 10000
  151. static struct ftrace_page *ftrace_pages_start;
  152. static struct ftrace_page *ftrace_pages;
  153. static int ftraced_trigger;
  154. static int ftraced_suspend;
  155. static int ftrace_record_suspend;
  156. static struct dyn_ftrace *ftrace_free_records;
  157. static inline int
  158. ftrace_ip_in_hash(unsigned long ip, unsigned long key)
  159. {
  160. struct dyn_ftrace *p;
  161. struct hlist_node *t;
  162. int found = 0;
  163. hlist_for_each_entry_rcu(p, t, &ftrace_hash[key], node) {
  164. if (p->ip == ip) {
  165. found = 1;
  166. break;
  167. }
  168. }
  169. return found;
  170. }
  171. static inline void
  172. ftrace_add_hash(struct dyn_ftrace *node, unsigned long key)
  173. {
  174. hlist_add_head_rcu(&node->node, &ftrace_hash[key]);
  175. }
  176. static void ftrace_free_rec(struct dyn_ftrace *rec)
  177. {
  178. /* no locking, only called from kstop_machine */
  179. rec->ip = (unsigned long)ftrace_free_records;
  180. ftrace_free_records = rec;
  181. rec->flags |= FTRACE_FL_FREE;
  182. }
  183. static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
  184. {
  185. struct dyn_ftrace *rec;
  186. /* First check for freed records */
  187. if (ftrace_free_records) {
  188. rec = ftrace_free_records;
  189. if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
  190. WARN_ON_ONCE(1);
  191. ftrace_free_records = NULL;
  192. ftrace_disabled = 1;
  193. ftrace_enabled = 0;
  194. return NULL;
  195. }
  196. ftrace_free_records = (void *)rec->ip;
  197. memset(rec, 0, sizeof(*rec));
  198. return rec;
  199. }
  200. if (ftrace_pages->index == ENTRIES_PER_PAGE) {
  201. if (!ftrace_pages->next)
  202. return NULL;
  203. ftrace_pages = ftrace_pages->next;
  204. }
  205. return &ftrace_pages->records[ftrace_pages->index++];
  206. }
  207. static void
  208. ftrace_record_ip(unsigned long ip)
  209. {
  210. struct dyn_ftrace *node;
  211. unsigned long flags;
  212. unsigned long key;
  213. int resched;
  214. int atomic;
  215. int cpu;
  216. if (!ftrace_enabled || ftrace_disabled)
  217. return;
  218. resched = need_resched();
  219. preempt_disable_notrace();
  220. /*
  221. * We simply need to protect against recursion.
  222. * Use the the raw version of smp_processor_id and not
  223. * __get_cpu_var which can call debug hooks that can
  224. * cause a recursive crash here.
  225. */
  226. cpu = raw_smp_processor_id();
  227. per_cpu(ftrace_shutdown_disable_cpu, cpu)++;
  228. if (per_cpu(ftrace_shutdown_disable_cpu, cpu) != 1)
  229. goto out;
  230. if (unlikely(ftrace_record_suspend))
  231. goto out;
  232. key = hash_long(ip, FTRACE_HASHBITS);
  233. WARN_ON_ONCE(key >= FTRACE_HASHSIZE);
  234. if (ftrace_ip_in_hash(ip, key))
  235. goto out;
  236. atomic = irqs_disabled();
  237. spin_lock_irqsave(&ftrace_shutdown_lock, flags);
  238. /* This ip may have hit the hash before the lock */
  239. if (ftrace_ip_in_hash(ip, key))
  240. goto out_unlock;
  241. /*
  242. * There's a slight race that the ftraced will update the
  243. * hash and reset here. If it is already converted, skip it.
  244. */
  245. if (ftrace_ip_converted(ip))
  246. goto out_unlock;
  247. node = ftrace_alloc_dyn_node(ip);
  248. if (!node)
  249. goto out_unlock;
  250. node->ip = ip;
  251. ftrace_add_hash(node, key);
  252. ftraced_trigger = 1;
  253. out_unlock:
  254. spin_unlock_irqrestore(&ftrace_shutdown_lock, flags);
  255. out:
  256. per_cpu(ftrace_shutdown_disable_cpu, cpu)--;
  257. /* prevent recursion with scheduler */
  258. if (resched)
  259. preempt_enable_no_resched_notrace();
  260. else
  261. preempt_enable_notrace();
  262. }
  263. #define FTRACE_ADDR ((long)(ftrace_caller))
  264. #define MCOUNT_ADDR ((long)(mcount))
  265. static void
  266. __ftrace_replace_code(struct dyn_ftrace *rec,
  267. unsigned char *old, unsigned char *new, int enable)
  268. {
  269. unsigned long ip, fl;
  270. int failed;
  271. ip = rec->ip;
  272. if (ftrace_filtered && enable) {
  273. /*
  274. * If filtering is on:
  275. *
  276. * If this record is set to be filtered and
  277. * is enabled then do nothing.
  278. *
  279. * If this record is set to be filtered and
  280. * it is not enabled, enable it.
  281. *
  282. * If this record is not set to be filtered
  283. * and it is not enabled do nothing.
  284. *
  285. * If this record is set not to trace then
  286. * do nothing.
  287. *
  288. * If this record is not set to be filtered and
  289. * it is enabled, disable it.
  290. */
  291. fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
  292. if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
  293. (fl == 0) || (rec->flags & FTRACE_FL_NOTRACE))
  294. return;
  295. /*
  296. * If it is enabled disable it,
  297. * otherwise enable it!
  298. */
  299. if (fl == FTRACE_FL_ENABLED) {
  300. /* swap new and old */
  301. new = old;
  302. old = ftrace_call_replace(ip, FTRACE_ADDR);
  303. rec->flags &= ~FTRACE_FL_ENABLED;
  304. } else {
  305. new = ftrace_call_replace(ip, FTRACE_ADDR);
  306. rec->flags |= FTRACE_FL_ENABLED;
  307. }
  308. } else {
  309. if (enable) {
  310. /*
  311. * If this record is set not to trace and is
  312. * not enabled, do nothing.
  313. */
  314. fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
  315. if (fl == FTRACE_FL_NOTRACE)
  316. return;
  317. new = ftrace_call_replace(ip, FTRACE_ADDR);
  318. } else
  319. old = ftrace_call_replace(ip, FTRACE_ADDR);
  320. if (enable) {
  321. if (rec->flags & FTRACE_FL_ENABLED)
  322. return;
  323. rec->flags |= FTRACE_FL_ENABLED;
  324. } else {
  325. if (!(rec->flags & FTRACE_FL_ENABLED))
  326. return;
  327. rec->flags &= ~FTRACE_FL_ENABLED;
  328. }
  329. }
  330. failed = ftrace_modify_code(ip, old, new);
  331. if (failed) {
  332. unsigned long key;
  333. /* It is possible that the function hasn't been converted yet */
  334. key = hash_long(ip, FTRACE_HASHBITS);
  335. if (!ftrace_ip_in_hash(ip, key)) {
  336. rec->flags |= FTRACE_FL_FAILED;
  337. ftrace_free_rec(rec);
  338. }
  339. }
  340. }
  341. static void ftrace_replace_code(int enable)
  342. {
  343. unsigned char *new = NULL, *old = NULL;
  344. struct dyn_ftrace *rec;
  345. struct ftrace_page *pg;
  346. int i;
  347. if (enable)
  348. old = ftrace_nop_replace();
  349. else
  350. new = ftrace_nop_replace();
  351. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  352. for (i = 0; i < pg->index; i++) {
  353. rec = &pg->records[i];
  354. /* don't modify code that has already faulted */
  355. if (rec->flags & FTRACE_FL_FAILED)
  356. continue;
  357. __ftrace_replace_code(rec, old, new, enable);
  358. }
  359. }
  360. }
  361. static void ftrace_shutdown_replenish(void)
  362. {
  363. if (ftrace_pages->next)
  364. return;
  365. /* allocate another page */
  366. ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL);
  367. }
  368. static void
  369. ftrace_code_disable(struct dyn_ftrace *rec)
  370. {
  371. unsigned long ip;
  372. unsigned char *nop, *call;
  373. int failed;
  374. ip = rec->ip;
  375. nop = ftrace_nop_replace();
  376. call = ftrace_call_replace(ip, MCOUNT_ADDR);
  377. failed = ftrace_modify_code(ip, call, nop);
  378. if (failed) {
  379. rec->flags |= FTRACE_FL_FAILED;
  380. ftrace_free_rec(rec);
  381. }
  382. }
  383. static int __ftrace_modify_code(void *data)
  384. {
  385. unsigned long addr;
  386. int *command = data;
  387. if (*command & FTRACE_ENABLE_CALLS)
  388. ftrace_replace_code(1);
  389. else if (*command & FTRACE_DISABLE_CALLS)
  390. ftrace_replace_code(0);
  391. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  392. ftrace_update_ftrace_func(ftrace_trace_function);
  393. if (*command & FTRACE_ENABLE_MCOUNT) {
  394. addr = (unsigned long)ftrace_record_ip;
  395. ftrace_mcount_set(&addr);
  396. } else if (*command & FTRACE_DISABLE_MCOUNT) {
  397. addr = (unsigned long)ftrace_stub;
  398. ftrace_mcount_set(&addr);
  399. }
  400. return 0;
  401. }
  402. static void ftrace_run_update_code(int command)
  403. {
  404. stop_machine_run(__ftrace_modify_code, &command, NR_CPUS);
  405. }
  406. static ftrace_func_t saved_ftrace_func;
  407. static void ftrace_startup(void)
  408. {
  409. int command = 0;
  410. if (unlikely(ftrace_disabled))
  411. return;
  412. mutex_lock(&ftraced_lock);
  413. ftraced_suspend++;
  414. if (ftraced_suspend == 1)
  415. command |= FTRACE_ENABLE_CALLS;
  416. if (saved_ftrace_func != ftrace_trace_function) {
  417. saved_ftrace_func = ftrace_trace_function;
  418. command |= FTRACE_UPDATE_TRACE_FUNC;
  419. }
  420. if (!command || !ftrace_enabled)
  421. goto out;
  422. ftrace_run_update_code(command);
  423. out:
  424. mutex_unlock(&ftraced_lock);
  425. }
  426. static void ftrace_shutdown(void)
  427. {
  428. int command = 0;
  429. if (unlikely(ftrace_disabled))
  430. return;
  431. mutex_lock(&ftraced_lock);
  432. ftraced_suspend--;
  433. if (!ftraced_suspend)
  434. command |= FTRACE_DISABLE_CALLS;
  435. if (saved_ftrace_func != ftrace_trace_function) {
  436. saved_ftrace_func = ftrace_trace_function;
  437. command |= FTRACE_UPDATE_TRACE_FUNC;
  438. }
  439. if (!command || !ftrace_enabled)
  440. goto out;
  441. ftrace_run_update_code(command);
  442. out:
  443. mutex_unlock(&ftraced_lock);
  444. }
  445. static void ftrace_startup_sysctl(void)
  446. {
  447. int command = FTRACE_ENABLE_MCOUNT;
  448. if (unlikely(ftrace_disabled))
  449. return;
  450. mutex_lock(&ftraced_lock);
  451. /* Force update next time */
  452. saved_ftrace_func = NULL;
  453. /* ftraced_suspend is true if we want ftrace running */
  454. if (ftraced_suspend)
  455. command |= FTRACE_ENABLE_CALLS;
  456. ftrace_run_update_code(command);
  457. mutex_unlock(&ftraced_lock);
  458. }
  459. static void ftrace_shutdown_sysctl(void)
  460. {
  461. int command = FTRACE_DISABLE_MCOUNT;
  462. if (unlikely(ftrace_disabled))
  463. return;
  464. mutex_lock(&ftraced_lock);
  465. /* ftraced_suspend is true if ftrace is running */
  466. if (ftraced_suspend)
  467. command |= FTRACE_DISABLE_CALLS;
  468. ftrace_run_update_code(command);
  469. mutex_unlock(&ftraced_lock);
  470. }
  471. static cycle_t ftrace_update_time;
  472. static unsigned long ftrace_update_cnt;
  473. unsigned long ftrace_update_tot_cnt;
  474. static int __ftrace_update_code(void *ignore)
  475. {
  476. struct dyn_ftrace *p;
  477. struct hlist_head head;
  478. struct hlist_node *t;
  479. int save_ftrace_enabled;
  480. cycle_t start, stop;
  481. int i;
  482. /* Don't be recording funcs now */
  483. save_ftrace_enabled = ftrace_enabled;
  484. ftrace_enabled = 0;
  485. start = ftrace_now(raw_smp_processor_id());
  486. ftrace_update_cnt = 0;
  487. /* No locks needed, the machine is stopped! */
  488. for (i = 0; i < FTRACE_HASHSIZE; i++) {
  489. if (hlist_empty(&ftrace_hash[i]))
  490. continue;
  491. head = ftrace_hash[i];
  492. INIT_HLIST_HEAD(&ftrace_hash[i]);
  493. /* all CPUS are stopped, we are safe to modify code */
  494. hlist_for_each_entry(p, t, &head, node) {
  495. ftrace_code_disable(p);
  496. ftrace_update_cnt++;
  497. }
  498. }
  499. stop = ftrace_now(raw_smp_processor_id());
  500. ftrace_update_time = stop - start;
  501. ftrace_update_tot_cnt += ftrace_update_cnt;
  502. ftrace_enabled = save_ftrace_enabled;
  503. return 0;
  504. }
  505. static void ftrace_update_code(void)
  506. {
  507. if (unlikely(ftrace_disabled))
  508. return;
  509. stop_machine_run(__ftrace_update_code, NULL, NR_CPUS);
  510. }
  511. static int ftraced(void *ignore)
  512. {
  513. unsigned long usecs;
  514. while (!kthread_should_stop()) {
  515. set_current_state(TASK_INTERRUPTIBLE);
  516. /* check once a second */
  517. schedule_timeout(HZ);
  518. if (unlikely(ftrace_disabled))
  519. continue;
  520. mutex_lock(&ftrace_sysctl_lock);
  521. mutex_lock(&ftraced_lock);
  522. if (ftrace_enabled && ftraced_trigger && !ftraced_suspend) {
  523. ftrace_record_suspend++;
  524. ftrace_update_code();
  525. usecs = nsecs_to_usecs(ftrace_update_time);
  526. if (ftrace_update_tot_cnt > 100000) {
  527. ftrace_update_tot_cnt = 0;
  528. pr_info("hm, dftrace overflow: %lu change%s"
  529. " (%lu total) in %lu usec%s\n",
  530. ftrace_update_cnt,
  531. ftrace_update_cnt != 1 ? "s" : "",
  532. ftrace_update_tot_cnt,
  533. usecs, usecs != 1 ? "s" : "");
  534. ftrace_disabled = 1;
  535. WARN_ON_ONCE(1);
  536. }
  537. ftraced_trigger = 0;
  538. ftrace_record_suspend--;
  539. }
  540. ftraced_iteration_counter++;
  541. mutex_unlock(&ftraced_lock);
  542. mutex_unlock(&ftrace_sysctl_lock);
  543. wake_up_interruptible(&ftraced_waiters);
  544. ftrace_shutdown_replenish();
  545. }
  546. __set_current_state(TASK_RUNNING);
  547. return 0;
  548. }
  549. static int __init ftrace_dyn_table_alloc(void)
  550. {
  551. struct ftrace_page *pg;
  552. int cnt;
  553. int i;
  554. /* allocate a few pages */
  555. ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
  556. if (!ftrace_pages_start)
  557. return -1;
  558. /*
  559. * Allocate a few more pages.
  560. *
  561. * TODO: have some parser search vmlinux before
  562. * final linking to find all calls to ftrace.
  563. * Then we can:
  564. * a) know how many pages to allocate.
  565. * and/or
  566. * b) set up the table then.
  567. *
  568. * The dynamic code is still necessary for
  569. * modules.
  570. */
  571. pg = ftrace_pages = ftrace_pages_start;
  572. cnt = NR_TO_INIT / ENTRIES_PER_PAGE;
  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. };
  587. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  588. struct ftrace_iterator {
  589. loff_t pos;
  590. struct ftrace_page *pg;
  591. unsigned idx;
  592. unsigned flags;
  593. unsigned char buffer[FTRACE_BUFF_MAX+1];
  594. unsigned buffer_idx;
  595. unsigned filtered;
  596. };
  597. static void *
  598. t_next(struct seq_file *m, void *v, loff_t *pos)
  599. {
  600. struct ftrace_iterator *iter = m->private;
  601. struct dyn_ftrace *rec = NULL;
  602. (*pos)++;
  603. retry:
  604. if (iter->idx >= iter->pg->index) {
  605. if (iter->pg->next) {
  606. iter->pg = iter->pg->next;
  607. iter->idx = 0;
  608. goto retry;
  609. }
  610. } else {
  611. rec = &iter->pg->records[iter->idx++];
  612. if ((rec->flags & FTRACE_FL_FAILED) ||
  613. ((iter->flags & FTRACE_ITER_FILTER) &&
  614. !(rec->flags & FTRACE_FL_FILTER)) ||
  615. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  616. !(rec->flags & FTRACE_FL_NOTRACE))) {
  617. rec = NULL;
  618. goto retry;
  619. }
  620. }
  621. iter->pos = *pos;
  622. return rec;
  623. }
  624. static void *t_start(struct seq_file *m, loff_t *pos)
  625. {
  626. struct ftrace_iterator *iter = m->private;
  627. void *p = NULL;
  628. loff_t l = -1;
  629. if (*pos != iter->pos) {
  630. for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
  631. ;
  632. } else {
  633. l = *pos;
  634. p = t_next(m, p, &l);
  635. }
  636. return p;
  637. }
  638. static void t_stop(struct seq_file *m, void *p)
  639. {
  640. }
  641. static int t_show(struct seq_file *m, void *v)
  642. {
  643. struct dyn_ftrace *rec = v;
  644. char str[KSYM_SYMBOL_LEN];
  645. if (!rec)
  646. return 0;
  647. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  648. seq_printf(m, "%s\n", str);
  649. return 0;
  650. }
  651. static struct seq_operations show_ftrace_seq_ops = {
  652. .start = t_start,
  653. .next = t_next,
  654. .stop = t_stop,
  655. .show = t_show,
  656. };
  657. static int
  658. ftrace_avail_open(struct inode *inode, struct file *file)
  659. {
  660. struct ftrace_iterator *iter;
  661. int ret;
  662. if (unlikely(ftrace_disabled))
  663. return -ENODEV;
  664. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  665. if (!iter)
  666. return -ENOMEM;
  667. iter->pg = ftrace_pages_start;
  668. iter->pos = -1;
  669. ret = seq_open(file, &show_ftrace_seq_ops);
  670. if (!ret) {
  671. struct seq_file *m = file->private_data;
  672. m->private = iter;
  673. } else {
  674. kfree(iter);
  675. }
  676. return ret;
  677. }
  678. int ftrace_avail_release(struct inode *inode, struct file *file)
  679. {
  680. struct seq_file *m = (struct seq_file *)file->private_data;
  681. struct ftrace_iterator *iter = m->private;
  682. seq_release(inode, file);
  683. kfree(iter);
  684. return 0;
  685. }
  686. static void ftrace_filter_reset(int enable)
  687. {
  688. struct ftrace_page *pg;
  689. struct dyn_ftrace *rec;
  690. unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  691. unsigned i;
  692. /* keep kstop machine from running */
  693. preempt_disable();
  694. if (enable)
  695. ftrace_filtered = 0;
  696. pg = ftrace_pages_start;
  697. while (pg) {
  698. for (i = 0; i < pg->index; i++) {
  699. rec = &pg->records[i];
  700. if (rec->flags & FTRACE_FL_FAILED)
  701. continue;
  702. rec->flags &= ~type;
  703. }
  704. pg = pg->next;
  705. }
  706. preempt_enable();
  707. }
  708. static int
  709. ftrace_regex_open(struct inode *inode, struct file *file, int enable)
  710. {
  711. struct ftrace_iterator *iter;
  712. int ret = 0;
  713. if (unlikely(ftrace_disabled))
  714. return -ENODEV;
  715. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  716. if (!iter)
  717. return -ENOMEM;
  718. mutex_lock(&ftrace_regex_lock);
  719. if ((file->f_mode & FMODE_WRITE) &&
  720. !(file->f_flags & O_APPEND))
  721. ftrace_filter_reset(enable);
  722. if (file->f_mode & FMODE_READ) {
  723. iter->pg = ftrace_pages_start;
  724. iter->pos = -1;
  725. iter->flags = enable ? FTRACE_ITER_FILTER :
  726. FTRACE_ITER_NOTRACE;
  727. ret = seq_open(file, &show_ftrace_seq_ops);
  728. if (!ret) {
  729. struct seq_file *m = file->private_data;
  730. m->private = iter;
  731. } else
  732. kfree(iter);
  733. } else
  734. file->private_data = iter;
  735. mutex_unlock(&ftrace_regex_lock);
  736. return ret;
  737. }
  738. static int
  739. ftrace_filter_open(struct inode *inode, struct file *file)
  740. {
  741. return ftrace_regex_open(inode, file, 1);
  742. }
  743. static int
  744. ftrace_notrace_open(struct inode *inode, struct file *file)
  745. {
  746. return ftrace_regex_open(inode, file, 0);
  747. }
  748. static ssize_t
  749. ftrace_regex_read(struct file *file, char __user *ubuf,
  750. size_t cnt, loff_t *ppos)
  751. {
  752. if (file->f_mode & FMODE_READ)
  753. return seq_read(file, ubuf, cnt, ppos);
  754. else
  755. return -EPERM;
  756. }
  757. static loff_t
  758. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  759. {
  760. loff_t ret;
  761. if (file->f_mode & FMODE_READ)
  762. ret = seq_lseek(file, offset, origin);
  763. else
  764. file->f_pos = ret = 1;
  765. return ret;
  766. }
  767. enum {
  768. MATCH_FULL,
  769. MATCH_FRONT_ONLY,
  770. MATCH_MIDDLE_ONLY,
  771. MATCH_END_ONLY,
  772. };
  773. static void
  774. ftrace_match(unsigned char *buff, int len, int enable)
  775. {
  776. char str[KSYM_SYMBOL_LEN];
  777. char *search = NULL;
  778. struct ftrace_page *pg;
  779. struct dyn_ftrace *rec;
  780. int type = MATCH_FULL;
  781. unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  782. unsigned i, match = 0, search_len = 0;
  783. for (i = 0; i < len; i++) {
  784. if (buff[i] == '*') {
  785. if (!i) {
  786. search = buff + i + 1;
  787. type = MATCH_END_ONLY;
  788. search_len = len - (i + 1);
  789. } else {
  790. if (type == MATCH_END_ONLY) {
  791. type = MATCH_MIDDLE_ONLY;
  792. } else {
  793. match = i;
  794. type = MATCH_FRONT_ONLY;
  795. }
  796. buff[i] = 0;
  797. break;
  798. }
  799. }
  800. }
  801. /* keep kstop machine from running */
  802. preempt_disable();
  803. if (enable)
  804. ftrace_filtered = 1;
  805. pg = ftrace_pages_start;
  806. while (pg) {
  807. for (i = 0; i < pg->index; i++) {
  808. int matched = 0;
  809. char *ptr;
  810. rec = &pg->records[i];
  811. if (rec->flags & FTRACE_FL_FAILED)
  812. continue;
  813. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  814. switch (type) {
  815. case MATCH_FULL:
  816. if (strcmp(str, buff) == 0)
  817. matched = 1;
  818. break;
  819. case MATCH_FRONT_ONLY:
  820. if (memcmp(str, buff, match) == 0)
  821. matched = 1;
  822. break;
  823. case MATCH_MIDDLE_ONLY:
  824. if (strstr(str, search))
  825. matched = 1;
  826. break;
  827. case MATCH_END_ONLY:
  828. ptr = strstr(str, search);
  829. if (ptr && (ptr[search_len] == 0))
  830. matched = 1;
  831. break;
  832. }
  833. if (matched)
  834. rec->flags |= flag;
  835. }
  836. pg = pg->next;
  837. }
  838. preempt_enable();
  839. }
  840. static ssize_t
  841. ftrace_regex_write(struct file *file, const char __user *ubuf,
  842. size_t cnt, loff_t *ppos, int enable)
  843. {
  844. struct ftrace_iterator *iter;
  845. char ch;
  846. size_t read = 0;
  847. ssize_t ret;
  848. if (!cnt || cnt < 0)
  849. return 0;
  850. mutex_lock(&ftrace_regex_lock);
  851. if (file->f_mode & FMODE_READ) {
  852. struct seq_file *m = file->private_data;
  853. iter = m->private;
  854. } else
  855. iter = file->private_data;
  856. if (!*ppos) {
  857. iter->flags &= ~FTRACE_ITER_CONT;
  858. iter->buffer_idx = 0;
  859. }
  860. ret = get_user(ch, ubuf++);
  861. if (ret)
  862. goto out;
  863. read++;
  864. cnt--;
  865. if (!(iter->flags & ~FTRACE_ITER_CONT)) {
  866. /* skip white space */
  867. while (cnt && isspace(ch)) {
  868. ret = get_user(ch, ubuf++);
  869. if (ret)
  870. goto out;
  871. read++;
  872. cnt--;
  873. }
  874. if (isspace(ch)) {
  875. file->f_pos += read;
  876. ret = read;
  877. goto out;
  878. }
  879. iter->buffer_idx = 0;
  880. }
  881. while (cnt && !isspace(ch)) {
  882. if (iter->buffer_idx < FTRACE_BUFF_MAX)
  883. iter->buffer[iter->buffer_idx++] = ch;
  884. else {
  885. ret = -EINVAL;
  886. goto out;
  887. }
  888. ret = get_user(ch, ubuf++);
  889. if (ret)
  890. goto out;
  891. read++;
  892. cnt--;
  893. }
  894. if (isspace(ch)) {
  895. iter->filtered++;
  896. iter->buffer[iter->buffer_idx] = 0;
  897. ftrace_match(iter->buffer, iter->buffer_idx, enable);
  898. iter->buffer_idx = 0;
  899. } else
  900. iter->flags |= FTRACE_ITER_CONT;
  901. file->f_pos += read;
  902. ret = read;
  903. out:
  904. mutex_unlock(&ftrace_regex_lock);
  905. return ret;
  906. }
  907. static ssize_t
  908. ftrace_filter_write(struct file *file, const char __user *ubuf,
  909. size_t cnt, loff_t *ppos)
  910. {
  911. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  912. }
  913. static ssize_t
  914. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  915. size_t cnt, loff_t *ppos)
  916. {
  917. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  918. }
  919. static void
  920. ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
  921. {
  922. if (unlikely(ftrace_disabled))
  923. return;
  924. mutex_lock(&ftrace_regex_lock);
  925. if (reset)
  926. ftrace_filter_reset(enable);
  927. if (buf)
  928. ftrace_match(buf, len, enable);
  929. mutex_unlock(&ftrace_regex_lock);
  930. }
  931. /**
  932. * ftrace_set_filter - set a function to filter on in ftrace
  933. * @buf - the string that holds the function filter text.
  934. * @len - the length of the string.
  935. * @reset - non zero to reset all filters before applying this filter.
  936. *
  937. * Filters denote which functions should be enabled when tracing is enabled.
  938. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  939. */
  940. void ftrace_set_filter(unsigned char *buf, int len, int reset)
  941. {
  942. ftrace_set_regex(buf, len, reset, 1);
  943. }
  944. /**
  945. * ftrace_set_notrace - set a function to not trace in ftrace
  946. * @buf - the string that holds the function notrace text.
  947. * @len - the length of the string.
  948. * @reset - non zero to reset all filters before applying this filter.
  949. *
  950. * Notrace Filters denote which functions should not be enabled when tracing
  951. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  952. * for tracing.
  953. */
  954. void ftrace_set_notrace(unsigned char *buf, int len, int reset)
  955. {
  956. ftrace_set_regex(buf, len, reset, 0);
  957. }
  958. static int
  959. ftrace_regex_release(struct inode *inode, struct file *file, int enable)
  960. {
  961. struct seq_file *m = (struct seq_file *)file->private_data;
  962. struct ftrace_iterator *iter;
  963. mutex_lock(&ftrace_regex_lock);
  964. if (file->f_mode & FMODE_READ) {
  965. iter = m->private;
  966. seq_release(inode, file);
  967. } else
  968. iter = file->private_data;
  969. if (iter->buffer_idx) {
  970. iter->filtered++;
  971. iter->buffer[iter->buffer_idx] = 0;
  972. ftrace_match(iter->buffer, iter->buffer_idx, enable);
  973. }
  974. mutex_lock(&ftrace_sysctl_lock);
  975. mutex_lock(&ftraced_lock);
  976. if (iter->filtered && ftraced_suspend && ftrace_enabled)
  977. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  978. mutex_unlock(&ftraced_lock);
  979. mutex_unlock(&ftrace_sysctl_lock);
  980. kfree(iter);
  981. mutex_unlock(&ftrace_regex_lock);
  982. return 0;
  983. }
  984. static int
  985. ftrace_filter_release(struct inode *inode, struct file *file)
  986. {
  987. return ftrace_regex_release(inode, file, 1);
  988. }
  989. static int
  990. ftrace_notrace_release(struct inode *inode, struct file *file)
  991. {
  992. return ftrace_regex_release(inode, file, 0);
  993. }
  994. static struct file_operations ftrace_avail_fops = {
  995. .open = ftrace_avail_open,
  996. .read = seq_read,
  997. .llseek = seq_lseek,
  998. .release = ftrace_avail_release,
  999. };
  1000. static struct file_operations ftrace_filter_fops = {
  1001. .open = ftrace_filter_open,
  1002. .read = ftrace_regex_read,
  1003. .write = ftrace_filter_write,
  1004. .llseek = ftrace_regex_lseek,
  1005. .release = ftrace_filter_release,
  1006. };
  1007. static struct file_operations ftrace_notrace_fops = {
  1008. .open = ftrace_notrace_open,
  1009. .read = ftrace_regex_read,
  1010. .write = ftrace_notrace_write,
  1011. .llseek = ftrace_regex_lseek,
  1012. .release = ftrace_notrace_release,
  1013. };
  1014. /**
  1015. * ftrace_force_update - force an update to all recording ftrace functions
  1016. *
  1017. * The ftrace dynamic update daemon only wakes up once a second.
  1018. * There may be cases where an update needs to be done immediately
  1019. * for tests or internal kernel tracing to begin. This function
  1020. * wakes the daemon to do an update and will not return until the
  1021. * update is complete.
  1022. */
  1023. int ftrace_force_update(void)
  1024. {
  1025. unsigned long last_counter;
  1026. DECLARE_WAITQUEUE(wait, current);
  1027. int ret = 0;
  1028. if (unlikely(ftrace_disabled))
  1029. return -ENODEV;
  1030. mutex_lock(&ftraced_lock);
  1031. last_counter = ftraced_iteration_counter;
  1032. set_current_state(TASK_INTERRUPTIBLE);
  1033. add_wait_queue(&ftraced_waiters, &wait);
  1034. if (unlikely(!ftraced_task)) {
  1035. ret = -ENODEV;
  1036. goto out;
  1037. }
  1038. do {
  1039. mutex_unlock(&ftraced_lock);
  1040. wake_up_process(ftraced_task);
  1041. schedule();
  1042. mutex_lock(&ftraced_lock);
  1043. if (signal_pending(current)) {
  1044. ret = -EINTR;
  1045. break;
  1046. }
  1047. set_current_state(TASK_INTERRUPTIBLE);
  1048. } while (last_counter == ftraced_iteration_counter);
  1049. out:
  1050. mutex_unlock(&ftraced_lock);
  1051. remove_wait_queue(&ftraced_waiters, &wait);
  1052. set_current_state(TASK_RUNNING);
  1053. return ret;
  1054. }
  1055. static void ftrace_force_shutdown(void)
  1056. {
  1057. struct task_struct *task;
  1058. int command = FTRACE_DISABLE_CALLS | FTRACE_UPDATE_TRACE_FUNC;
  1059. mutex_lock(&ftraced_lock);
  1060. task = ftraced_task;
  1061. ftraced_task = NULL;
  1062. ftraced_suspend = -1;
  1063. ftrace_run_update_code(command);
  1064. mutex_unlock(&ftraced_lock);
  1065. if (task)
  1066. kthread_stop(task);
  1067. }
  1068. static __init int ftrace_init_debugfs(void)
  1069. {
  1070. struct dentry *d_tracer;
  1071. struct dentry *entry;
  1072. d_tracer = tracing_init_dentry();
  1073. entry = debugfs_create_file("available_filter_functions", 0444,
  1074. d_tracer, NULL, &ftrace_avail_fops);
  1075. if (!entry)
  1076. pr_warning("Could not create debugfs "
  1077. "'available_filter_functions' entry\n");
  1078. entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
  1079. NULL, &ftrace_filter_fops);
  1080. if (!entry)
  1081. pr_warning("Could not create debugfs "
  1082. "'set_ftrace_filter' entry\n");
  1083. entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
  1084. NULL, &ftrace_notrace_fops);
  1085. if (!entry)
  1086. pr_warning("Could not create debugfs "
  1087. "'set_ftrace_notrace' entry\n");
  1088. return 0;
  1089. }
  1090. fs_initcall(ftrace_init_debugfs);
  1091. static int __init ftrace_dynamic_init(void)
  1092. {
  1093. struct task_struct *p;
  1094. unsigned long addr;
  1095. int ret;
  1096. addr = (unsigned long)ftrace_record_ip;
  1097. stop_machine_run(ftrace_dyn_arch_init, &addr, NR_CPUS);
  1098. /* ftrace_dyn_arch_init places the return code in addr */
  1099. if (addr) {
  1100. ret = (int)addr;
  1101. goto failed;
  1102. }
  1103. ret = ftrace_dyn_table_alloc();
  1104. if (ret)
  1105. goto failed;
  1106. p = kthread_run(ftraced, NULL, "ftraced");
  1107. if (IS_ERR(p)) {
  1108. ret = -1;
  1109. goto failed;
  1110. }
  1111. last_ftrace_enabled = ftrace_enabled = 1;
  1112. ftraced_task = p;
  1113. return 0;
  1114. failed:
  1115. ftrace_disabled = 1;
  1116. return ret;
  1117. }
  1118. core_initcall(ftrace_dynamic_init);
  1119. #else
  1120. # define ftrace_startup() do { } while (0)
  1121. # define ftrace_shutdown() do { } while (0)
  1122. # define ftrace_startup_sysctl() do { } while (0)
  1123. # define ftrace_shutdown_sysctl() do { } while (0)
  1124. # define ftrace_force_shutdown() do { } while (0)
  1125. #endif /* CONFIG_DYNAMIC_FTRACE */
  1126. /**
  1127. * ftrace_kill - totally shutdown ftrace
  1128. *
  1129. * This is a safety measure. If something was detected that seems
  1130. * wrong, calling this function will keep ftrace from doing
  1131. * any more modifications, and updates.
  1132. * used when something went wrong.
  1133. */
  1134. void ftrace_kill(void)
  1135. {
  1136. mutex_lock(&ftrace_sysctl_lock);
  1137. ftrace_disabled = 1;
  1138. ftrace_enabled = 0;
  1139. clear_ftrace_function();
  1140. mutex_unlock(&ftrace_sysctl_lock);
  1141. /* Try to totally disable ftrace */
  1142. ftrace_force_shutdown();
  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. }