ftrace.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  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. enum {
  128. FTRACE_ENABLE_CALLS = (1 << 0),
  129. FTRACE_DISABLE_CALLS = (1 << 1),
  130. FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
  131. FTRACE_ENABLE_MCOUNT = (1 << 3),
  132. FTRACE_DISABLE_MCOUNT = (1 << 4),
  133. };
  134. static int ftrace_filtered;
  135. static struct hlist_head ftrace_hash[FTRACE_HASHSIZE];
  136. static DEFINE_PER_CPU(int, ftrace_shutdown_disable_cpu);
  137. static DEFINE_SPINLOCK(ftrace_shutdown_lock);
  138. static DEFINE_MUTEX(ftraced_lock);
  139. static DEFINE_MUTEX(ftrace_regex_lock);
  140. struct ftrace_page {
  141. struct ftrace_page *next;
  142. unsigned long index;
  143. struct dyn_ftrace records[];
  144. };
  145. #define ENTRIES_PER_PAGE \
  146. ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
  147. /* estimate from running different kernels */
  148. #define NR_TO_INIT 10000
  149. static struct ftrace_page *ftrace_pages_start;
  150. static struct ftrace_page *ftrace_pages;
  151. static int ftraced_trigger;
  152. static int ftraced_suspend;
  153. static int ftraced_stop;
  154. static int ftrace_record_suspend;
  155. static struct dyn_ftrace *ftrace_free_records;
  156. static inline int
  157. ftrace_ip_in_hash(unsigned long ip, unsigned long key)
  158. {
  159. struct dyn_ftrace *p;
  160. struct hlist_node *t;
  161. int found = 0;
  162. hlist_for_each_entry_rcu(p, t, &ftrace_hash[key], node) {
  163. if (p->ip == ip) {
  164. found = 1;
  165. break;
  166. }
  167. }
  168. return found;
  169. }
  170. static inline void
  171. ftrace_add_hash(struct dyn_ftrace *node, unsigned long key)
  172. {
  173. hlist_add_head_rcu(&node->node, &ftrace_hash[key]);
  174. }
  175. /* called from kstop_machine */
  176. static inline void ftrace_del_hash(struct dyn_ftrace *node)
  177. {
  178. hlist_del(&node->node);
  179. }
  180. static void ftrace_free_rec(struct dyn_ftrace *rec)
  181. {
  182. /* no locking, only called from kstop_machine */
  183. rec->ip = (unsigned long)ftrace_free_records;
  184. ftrace_free_records = rec;
  185. rec->flags |= FTRACE_FL_FREE;
  186. }
  187. static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
  188. {
  189. struct dyn_ftrace *rec;
  190. /* First check for freed records */
  191. if (ftrace_free_records) {
  192. rec = ftrace_free_records;
  193. if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
  194. WARN_ON_ONCE(1);
  195. ftrace_free_records = NULL;
  196. ftrace_disabled = 1;
  197. ftrace_enabled = 0;
  198. return NULL;
  199. }
  200. ftrace_free_records = (void *)rec->ip;
  201. memset(rec, 0, sizeof(*rec));
  202. return rec;
  203. }
  204. if (ftrace_pages->index == ENTRIES_PER_PAGE) {
  205. if (!ftrace_pages->next)
  206. return NULL;
  207. ftrace_pages = ftrace_pages->next;
  208. }
  209. return &ftrace_pages->records[ftrace_pages->index++];
  210. }
  211. static void
  212. ftrace_record_ip(unsigned long ip)
  213. {
  214. struct dyn_ftrace *node;
  215. unsigned long flags;
  216. unsigned long key;
  217. int resched;
  218. int atomic;
  219. int cpu;
  220. if (!ftrace_enabled || ftrace_disabled)
  221. return;
  222. resched = need_resched();
  223. preempt_disable_notrace();
  224. /*
  225. * We simply need to protect against recursion.
  226. * Use the the raw version of smp_processor_id and not
  227. * __get_cpu_var which can call debug hooks that can
  228. * cause a recursive crash here.
  229. */
  230. cpu = raw_smp_processor_id();
  231. per_cpu(ftrace_shutdown_disable_cpu, cpu)++;
  232. if (per_cpu(ftrace_shutdown_disable_cpu, cpu) != 1)
  233. goto out;
  234. if (unlikely(ftrace_record_suspend))
  235. goto out;
  236. key = hash_long(ip, FTRACE_HASHBITS);
  237. WARN_ON_ONCE(key >= FTRACE_HASHSIZE);
  238. if (ftrace_ip_in_hash(ip, key))
  239. goto out;
  240. atomic = irqs_disabled();
  241. spin_lock_irqsave(&ftrace_shutdown_lock, flags);
  242. /* This ip may have hit the hash before the lock */
  243. if (ftrace_ip_in_hash(ip, key))
  244. goto out_unlock;
  245. node = ftrace_alloc_dyn_node(ip);
  246. if (!node)
  247. goto out_unlock;
  248. node->ip = ip;
  249. ftrace_add_hash(node, key);
  250. ftraced_trigger = 1;
  251. out_unlock:
  252. spin_unlock_irqrestore(&ftrace_shutdown_lock, flags);
  253. out:
  254. per_cpu(ftrace_shutdown_disable_cpu, cpu)--;
  255. /* prevent recursion with scheduler */
  256. if (resched)
  257. preempt_enable_no_resched_notrace();
  258. else
  259. preempt_enable_notrace();
  260. }
  261. #define FTRACE_ADDR ((long)(ftrace_caller))
  262. #define MCOUNT_ADDR ((long)(mcount))
  263. static int
  264. __ftrace_replace_code(struct dyn_ftrace *rec,
  265. unsigned char *old, unsigned char *new, int enable)
  266. {
  267. unsigned long ip, fl;
  268. ip = rec->ip;
  269. if (ftrace_filtered && enable) {
  270. /*
  271. * If filtering is on:
  272. *
  273. * If this record is set to be filtered and
  274. * is enabled then do nothing.
  275. *
  276. * If this record is set to be filtered and
  277. * it is not enabled, enable it.
  278. *
  279. * If this record is not set to be filtered
  280. * and it is not enabled do nothing.
  281. *
  282. * If this record is set not to trace then
  283. * do nothing.
  284. *
  285. * If this record is not set to be filtered and
  286. * it is enabled, disable it.
  287. */
  288. fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
  289. if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
  290. (fl == 0) || (rec->flags & FTRACE_FL_NOTRACE))
  291. return 0;
  292. /*
  293. * If it is enabled disable it,
  294. * otherwise enable it!
  295. */
  296. if (fl == FTRACE_FL_ENABLED) {
  297. /* swap new and old */
  298. new = old;
  299. old = ftrace_call_replace(ip, FTRACE_ADDR);
  300. rec->flags &= ~FTRACE_FL_ENABLED;
  301. } else {
  302. new = ftrace_call_replace(ip, FTRACE_ADDR);
  303. rec->flags |= FTRACE_FL_ENABLED;
  304. }
  305. } else {
  306. if (enable) {
  307. /*
  308. * If this record is set not to trace and is
  309. * not enabled, do nothing.
  310. */
  311. fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
  312. if (fl == FTRACE_FL_NOTRACE)
  313. return 0;
  314. new = ftrace_call_replace(ip, FTRACE_ADDR);
  315. } else
  316. old = ftrace_call_replace(ip, FTRACE_ADDR);
  317. if (enable) {
  318. if (rec->flags & FTRACE_FL_ENABLED)
  319. return 0;
  320. rec->flags |= FTRACE_FL_ENABLED;
  321. } else {
  322. if (!(rec->flags & FTRACE_FL_ENABLED))
  323. return 0;
  324. rec->flags &= ~FTRACE_FL_ENABLED;
  325. }
  326. }
  327. return ftrace_modify_code(ip, old, new);
  328. }
  329. static void ftrace_replace_code(int enable)
  330. {
  331. int i, failed;
  332. unsigned char *new = NULL, *old = NULL;
  333. struct dyn_ftrace *rec;
  334. struct ftrace_page *pg;
  335. if (enable)
  336. old = ftrace_nop_replace();
  337. else
  338. new = ftrace_nop_replace();
  339. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  340. for (i = 0; i < pg->index; i++) {
  341. rec = &pg->records[i];
  342. /* don't modify code that has already faulted */
  343. if (rec->flags & FTRACE_FL_FAILED)
  344. continue;
  345. failed = __ftrace_replace_code(rec, old, new, enable);
  346. if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
  347. rec->flags |= FTRACE_FL_FAILED;
  348. if ((system_state == SYSTEM_BOOTING) ||
  349. !core_kernel_text(rec->ip)) {
  350. ftrace_del_hash(rec);
  351. ftrace_free_rec(rec);
  352. }
  353. }
  354. }
  355. }
  356. }
  357. static void ftrace_shutdown_replenish(void)
  358. {
  359. if (ftrace_pages->next)
  360. return;
  361. /* allocate another page */
  362. ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL);
  363. }
  364. static int
  365. ftrace_code_disable(struct dyn_ftrace *rec)
  366. {
  367. unsigned long ip;
  368. unsigned char *nop, *call;
  369. int failed;
  370. ip = rec->ip;
  371. nop = ftrace_nop_replace();
  372. call = ftrace_call_replace(ip, MCOUNT_ADDR);
  373. failed = ftrace_modify_code(ip, call, nop);
  374. if (failed) {
  375. rec->flags |= FTRACE_FL_FAILED;
  376. return 0;
  377. }
  378. return 1;
  379. }
  380. static int __ftrace_update_code(void *ignore);
  381. static int __ftrace_modify_code(void *data)
  382. {
  383. unsigned long addr;
  384. int *command = data;
  385. if (*command & FTRACE_ENABLE_CALLS) {
  386. /*
  387. * Update any recorded ips now that we have the
  388. * machine stopped
  389. */
  390. __ftrace_update_code(NULL);
  391. ftrace_replace_code(1);
  392. } else if (*command & FTRACE_DISABLE_CALLS)
  393. ftrace_replace_code(0);
  394. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  395. ftrace_update_ftrace_func(ftrace_trace_function);
  396. if (*command & FTRACE_ENABLE_MCOUNT) {
  397. addr = (unsigned long)ftrace_record_ip;
  398. ftrace_mcount_set(&addr);
  399. } else if (*command & FTRACE_DISABLE_MCOUNT) {
  400. addr = (unsigned long)ftrace_stub;
  401. ftrace_mcount_set(&addr);
  402. }
  403. return 0;
  404. }
  405. static void ftrace_run_update_code(int command)
  406. {
  407. stop_machine_run(__ftrace_modify_code, &command, NR_CPUS);
  408. }
  409. void ftrace_disable_daemon(void)
  410. {
  411. /* Stop the daemon from calling kstop_machine */
  412. mutex_lock(&ftraced_lock);
  413. ftraced_stop = 1;
  414. mutex_unlock(&ftraced_lock);
  415. ftrace_force_update();
  416. }
  417. void ftrace_enable_daemon(void)
  418. {
  419. mutex_lock(&ftraced_lock);
  420. ftraced_stop = 0;
  421. mutex_unlock(&ftraced_lock);
  422. ftrace_force_update();
  423. }
  424. static ftrace_func_t saved_ftrace_func;
  425. static void ftrace_startup(void)
  426. {
  427. int command = 0;
  428. if (unlikely(ftrace_disabled))
  429. return;
  430. mutex_lock(&ftraced_lock);
  431. ftraced_suspend++;
  432. if (ftraced_suspend == 1)
  433. command |= FTRACE_ENABLE_CALLS;
  434. if (saved_ftrace_func != ftrace_trace_function) {
  435. saved_ftrace_func = ftrace_trace_function;
  436. command |= FTRACE_UPDATE_TRACE_FUNC;
  437. }
  438. if (!command || !ftrace_enabled)
  439. goto out;
  440. ftrace_run_update_code(command);
  441. out:
  442. mutex_unlock(&ftraced_lock);
  443. }
  444. static void ftrace_shutdown(void)
  445. {
  446. int command = 0;
  447. if (unlikely(ftrace_disabled))
  448. return;
  449. mutex_lock(&ftraced_lock);
  450. ftraced_suspend--;
  451. if (!ftraced_suspend)
  452. command |= FTRACE_DISABLE_CALLS;
  453. if (saved_ftrace_func != ftrace_trace_function) {
  454. saved_ftrace_func = ftrace_trace_function;
  455. command |= FTRACE_UPDATE_TRACE_FUNC;
  456. }
  457. if (!command || !ftrace_enabled)
  458. goto out;
  459. ftrace_run_update_code(command);
  460. out:
  461. mutex_unlock(&ftraced_lock);
  462. }
  463. static void ftrace_startup_sysctl(void)
  464. {
  465. int command = FTRACE_ENABLE_MCOUNT;
  466. if (unlikely(ftrace_disabled))
  467. return;
  468. mutex_lock(&ftraced_lock);
  469. /* Force update next time */
  470. saved_ftrace_func = NULL;
  471. /* ftraced_suspend is true if we want ftrace running */
  472. if (ftraced_suspend)
  473. command |= FTRACE_ENABLE_CALLS;
  474. ftrace_run_update_code(command);
  475. mutex_unlock(&ftraced_lock);
  476. }
  477. static void ftrace_shutdown_sysctl(void)
  478. {
  479. int command = FTRACE_DISABLE_MCOUNT;
  480. if (unlikely(ftrace_disabled))
  481. return;
  482. mutex_lock(&ftraced_lock);
  483. /* ftraced_suspend is true if ftrace is running */
  484. if (ftraced_suspend)
  485. command |= FTRACE_DISABLE_CALLS;
  486. ftrace_run_update_code(command);
  487. mutex_unlock(&ftraced_lock);
  488. }
  489. static cycle_t ftrace_update_time;
  490. static unsigned long ftrace_update_cnt;
  491. unsigned long ftrace_update_tot_cnt;
  492. static int __ftrace_update_code(void *ignore)
  493. {
  494. struct dyn_ftrace *p;
  495. struct hlist_node *t, *n;
  496. int save_ftrace_enabled;
  497. cycle_t start, stop;
  498. int i;
  499. /* Don't be recording funcs now */
  500. ftrace_record_suspend++;
  501. save_ftrace_enabled = ftrace_enabled;
  502. ftrace_enabled = 0;
  503. start = ftrace_now(raw_smp_processor_id());
  504. ftrace_update_cnt = 0;
  505. /* No locks needed, the machine is stopped! */
  506. for (i = 0; i < FTRACE_HASHSIZE; i++) {
  507. /* all CPUS are stopped, we are safe to modify code */
  508. hlist_for_each_entry_safe(p, t, n, &ftrace_hash[i], node) {
  509. /* Skip over failed records which have not been
  510. * freed. */
  511. if (p->flags & FTRACE_FL_FAILED)
  512. continue;
  513. /* Unconverted records are always at the head of the
  514. * hash bucket. Once we encounter a converted record,
  515. * simply skip over to the next bucket. Saves ftraced
  516. * some processor cycles (ftrace does its bid for
  517. * global warming :-p ). */
  518. if (p->flags & (FTRACE_FL_CONVERTED))
  519. break;
  520. if (ftrace_code_disable(p)) {
  521. p->flags |= FTRACE_FL_CONVERTED;
  522. ftrace_update_cnt++;
  523. } else {
  524. if ((system_state == SYSTEM_BOOTING) ||
  525. !core_kernel_text(p->ip)) {
  526. ftrace_del_hash(p);
  527. ftrace_free_rec(p);
  528. }
  529. }
  530. }
  531. }
  532. stop = ftrace_now(raw_smp_processor_id());
  533. ftrace_update_time = stop - start;
  534. ftrace_update_tot_cnt += ftrace_update_cnt;
  535. ftraced_trigger = 0;
  536. ftrace_enabled = save_ftrace_enabled;
  537. ftrace_record_suspend--;
  538. return 0;
  539. }
  540. static int ftrace_update_code(void)
  541. {
  542. if (unlikely(ftrace_disabled) ||
  543. !ftrace_enabled || !ftraced_trigger)
  544. return 0;
  545. stop_machine_run(__ftrace_update_code, NULL, NR_CPUS);
  546. return 1;
  547. }
  548. static int ftraced(void *ignore)
  549. {
  550. unsigned long usecs;
  551. while (!kthread_should_stop()) {
  552. set_current_state(TASK_INTERRUPTIBLE);
  553. /* check once a second */
  554. schedule_timeout(HZ);
  555. if (unlikely(ftrace_disabled))
  556. continue;
  557. mutex_lock(&ftrace_sysctl_lock);
  558. mutex_lock(&ftraced_lock);
  559. if (!ftraced_suspend && !ftraced_stop &&
  560. ftrace_update_code()) {
  561. usecs = nsecs_to_usecs(ftrace_update_time);
  562. if (ftrace_update_tot_cnt > 100000) {
  563. ftrace_update_tot_cnt = 0;
  564. pr_info("hm, dftrace overflow: %lu change%s"
  565. " (%lu total) in %lu usec%s\n",
  566. ftrace_update_cnt,
  567. ftrace_update_cnt != 1 ? "s" : "",
  568. ftrace_update_tot_cnt,
  569. usecs, usecs != 1 ? "s" : "");
  570. ftrace_disabled = 1;
  571. WARN_ON_ONCE(1);
  572. }
  573. }
  574. mutex_unlock(&ftraced_lock);
  575. mutex_unlock(&ftrace_sysctl_lock);
  576. ftrace_shutdown_replenish();
  577. }
  578. __set_current_state(TASK_RUNNING);
  579. return 0;
  580. }
  581. static int __init ftrace_dyn_table_alloc(void)
  582. {
  583. struct ftrace_page *pg;
  584. int cnt;
  585. int i;
  586. /* allocate a few pages */
  587. ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
  588. if (!ftrace_pages_start)
  589. return -1;
  590. /*
  591. * Allocate a few more pages.
  592. *
  593. * TODO: have some parser search vmlinux before
  594. * final linking to find all calls to ftrace.
  595. * Then we can:
  596. * a) know how many pages to allocate.
  597. * and/or
  598. * b) set up the table then.
  599. *
  600. * The dynamic code is still necessary for
  601. * modules.
  602. */
  603. pg = ftrace_pages = ftrace_pages_start;
  604. cnt = NR_TO_INIT / ENTRIES_PER_PAGE;
  605. for (i = 0; i < cnt; i++) {
  606. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  607. /* If we fail, we'll try later anyway */
  608. if (!pg->next)
  609. break;
  610. pg = pg->next;
  611. }
  612. return 0;
  613. }
  614. enum {
  615. FTRACE_ITER_FILTER = (1 << 0),
  616. FTRACE_ITER_CONT = (1 << 1),
  617. FTRACE_ITER_NOTRACE = (1 << 2),
  618. FTRACE_ITER_FAILURES = (1 << 3),
  619. };
  620. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  621. struct ftrace_iterator {
  622. loff_t pos;
  623. struct ftrace_page *pg;
  624. unsigned idx;
  625. unsigned flags;
  626. unsigned char buffer[FTRACE_BUFF_MAX+1];
  627. unsigned buffer_idx;
  628. unsigned filtered;
  629. };
  630. static void *
  631. t_next(struct seq_file *m, void *v, loff_t *pos)
  632. {
  633. struct ftrace_iterator *iter = m->private;
  634. struct dyn_ftrace *rec = NULL;
  635. (*pos)++;
  636. retry:
  637. if (iter->idx >= iter->pg->index) {
  638. if (iter->pg->next) {
  639. iter->pg = iter->pg->next;
  640. iter->idx = 0;
  641. goto retry;
  642. }
  643. } else {
  644. rec = &iter->pg->records[iter->idx++];
  645. if ((!(iter->flags & FTRACE_ITER_FAILURES) &&
  646. (rec->flags & FTRACE_FL_FAILED)) ||
  647. ((iter->flags & FTRACE_ITER_FAILURES) &&
  648. (!(rec->flags & FTRACE_FL_FAILED) ||
  649. (rec->flags & FTRACE_FL_FREE))) ||
  650. ((iter->flags & FTRACE_ITER_FILTER) &&
  651. !(rec->flags & FTRACE_FL_FILTER)) ||
  652. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  653. !(rec->flags & FTRACE_FL_NOTRACE))) {
  654. rec = NULL;
  655. goto retry;
  656. }
  657. }
  658. iter->pos = *pos;
  659. return rec;
  660. }
  661. static void *t_start(struct seq_file *m, loff_t *pos)
  662. {
  663. struct ftrace_iterator *iter = m->private;
  664. void *p = NULL;
  665. loff_t l = -1;
  666. if (*pos != iter->pos) {
  667. for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
  668. ;
  669. } else {
  670. l = *pos;
  671. p = t_next(m, p, &l);
  672. }
  673. return p;
  674. }
  675. static void t_stop(struct seq_file *m, void *p)
  676. {
  677. }
  678. static int t_show(struct seq_file *m, void *v)
  679. {
  680. struct dyn_ftrace *rec = v;
  681. char str[KSYM_SYMBOL_LEN];
  682. if (!rec)
  683. return 0;
  684. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  685. seq_printf(m, "%s\n", str);
  686. return 0;
  687. }
  688. static struct seq_operations show_ftrace_seq_ops = {
  689. .start = t_start,
  690. .next = t_next,
  691. .stop = t_stop,
  692. .show = t_show,
  693. };
  694. static int
  695. ftrace_avail_open(struct inode *inode, struct file *file)
  696. {
  697. struct ftrace_iterator *iter;
  698. int ret;
  699. if (unlikely(ftrace_disabled))
  700. return -ENODEV;
  701. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  702. if (!iter)
  703. return -ENOMEM;
  704. iter->pg = ftrace_pages_start;
  705. iter->pos = -1;
  706. ret = seq_open(file, &show_ftrace_seq_ops);
  707. if (!ret) {
  708. struct seq_file *m = file->private_data;
  709. m->private = iter;
  710. } else {
  711. kfree(iter);
  712. }
  713. return ret;
  714. }
  715. int ftrace_avail_release(struct inode *inode, struct file *file)
  716. {
  717. struct seq_file *m = (struct seq_file *)file->private_data;
  718. struct ftrace_iterator *iter = m->private;
  719. seq_release(inode, file);
  720. kfree(iter);
  721. return 0;
  722. }
  723. static int
  724. ftrace_failures_open(struct inode *inode, struct file *file)
  725. {
  726. int ret;
  727. struct seq_file *m;
  728. struct ftrace_iterator *iter;
  729. ret = ftrace_avail_open(inode, file);
  730. if (!ret) {
  731. m = (struct seq_file *)file->private_data;
  732. iter = (struct ftrace_iterator *)m->private;
  733. iter->flags = FTRACE_ITER_FAILURES;
  734. }
  735. return ret;
  736. }
  737. static void ftrace_filter_reset(int enable)
  738. {
  739. struct ftrace_page *pg;
  740. struct dyn_ftrace *rec;
  741. unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  742. unsigned i;
  743. /* keep kstop machine from running */
  744. preempt_disable();
  745. if (enable)
  746. ftrace_filtered = 0;
  747. pg = ftrace_pages_start;
  748. while (pg) {
  749. for (i = 0; i < pg->index; i++) {
  750. rec = &pg->records[i];
  751. if (rec->flags & FTRACE_FL_FAILED)
  752. continue;
  753. rec->flags &= ~type;
  754. }
  755. pg = pg->next;
  756. }
  757. preempt_enable();
  758. }
  759. static int
  760. ftrace_regex_open(struct inode *inode, struct file *file, int enable)
  761. {
  762. struct ftrace_iterator *iter;
  763. int ret = 0;
  764. if (unlikely(ftrace_disabled))
  765. return -ENODEV;
  766. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  767. if (!iter)
  768. return -ENOMEM;
  769. mutex_lock(&ftrace_regex_lock);
  770. if ((file->f_mode & FMODE_WRITE) &&
  771. !(file->f_flags & O_APPEND))
  772. ftrace_filter_reset(enable);
  773. if (file->f_mode & FMODE_READ) {
  774. iter->pg = ftrace_pages_start;
  775. iter->pos = -1;
  776. iter->flags = enable ? FTRACE_ITER_FILTER :
  777. FTRACE_ITER_NOTRACE;
  778. ret = seq_open(file, &show_ftrace_seq_ops);
  779. if (!ret) {
  780. struct seq_file *m = file->private_data;
  781. m->private = iter;
  782. } else
  783. kfree(iter);
  784. } else
  785. file->private_data = iter;
  786. mutex_unlock(&ftrace_regex_lock);
  787. return ret;
  788. }
  789. static int
  790. ftrace_filter_open(struct inode *inode, struct file *file)
  791. {
  792. return ftrace_regex_open(inode, file, 1);
  793. }
  794. static int
  795. ftrace_notrace_open(struct inode *inode, struct file *file)
  796. {
  797. return ftrace_regex_open(inode, file, 0);
  798. }
  799. static ssize_t
  800. ftrace_regex_read(struct file *file, char __user *ubuf,
  801. size_t cnt, loff_t *ppos)
  802. {
  803. if (file->f_mode & FMODE_READ)
  804. return seq_read(file, ubuf, cnt, ppos);
  805. else
  806. return -EPERM;
  807. }
  808. static loff_t
  809. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  810. {
  811. loff_t ret;
  812. if (file->f_mode & FMODE_READ)
  813. ret = seq_lseek(file, offset, origin);
  814. else
  815. file->f_pos = ret = 1;
  816. return ret;
  817. }
  818. enum {
  819. MATCH_FULL,
  820. MATCH_FRONT_ONLY,
  821. MATCH_MIDDLE_ONLY,
  822. MATCH_END_ONLY,
  823. };
  824. static void
  825. ftrace_match(unsigned char *buff, int len, int enable)
  826. {
  827. char str[KSYM_SYMBOL_LEN];
  828. char *search = NULL;
  829. struct ftrace_page *pg;
  830. struct dyn_ftrace *rec;
  831. int type = MATCH_FULL;
  832. unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  833. unsigned i, match = 0, search_len = 0;
  834. for (i = 0; i < len; i++) {
  835. if (buff[i] == '*') {
  836. if (!i) {
  837. search = buff + i + 1;
  838. type = MATCH_END_ONLY;
  839. search_len = len - (i + 1);
  840. } else {
  841. if (type == MATCH_END_ONLY) {
  842. type = MATCH_MIDDLE_ONLY;
  843. } else {
  844. match = i;
  845. type = MATCH_FRONT_ONLY;
  846. }
  847. buff[i] = 0;
  848. break;
  849. }
  850. }
  851. }
  852. /* keep kstop machine from running */
  853. preempt_disable();
  854. if (enable)
  855. ftrace_filtered = 1;
  856. pg = ftrace_pages_start;
  857. while (pg) {
  858. for (i = 0; i < pg->index; i++) {
  859. int matched = 0;
  860. char *ptr;
  861. rec = &pg->records[i];
  862. if (rec->flags & FTRACE_FL_FAILED)
  863. continue;
  864. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  865. switch (type) {
  866. case MATCH_FULL:
  867. if (strcmp(str, buff) == 0)
  868. matched = 1;
  869. break;
  870. case MATCH_FRONT_ONLY:
  871. if (memcmp(str, buff, match) == 0)
  872. matched = 1;
  873. break;
  874. case MATCH_MIDDLE_ONLY:
  875. if (strstr(str, search))
  876. matched = 1;
  877. break;
  878. case MATCH_END_ONLY:
  879. ptr = strstr(str, search);
  880. if (ptr && (ptr[search_len] == 0))
  881. matched = 1;
  882. break;
  883. }
  884. if (matched)
  885. rec->flags |= flag;
  886. }
  887. pg = pg->next;
  888. }
  889. preempt_enable();
  890. }
  891. static ssize_t
  892. ftrace_regex_write(struct file *file, const char __user *ubuf,
  893. size_t cnt, loff_t *ppos, int enable)
  894. {
  895. struct ftrace_iterator *iter;
  896. char ch;
  897. size_t read = 0;
  898. ssize_t ret;
  899. if (!cnt || cnt < 0)
  900. return 0;
  901. mutex_lock(&ftrace_regex_lock);
  902. if (file->f_mode & FMODE_READ) {
  903. struct seq_file *m = file->private_data;
  904. iter = m->private;
  905. } else
  906. iter = file->private_data;
  907. if (!*ppos) {
  908. iter->flags &= ~FTRACE_ITER_CONT;
  909. iter->buffer_idx = 0;
  910. }
  911. ret = get_user(ch, ubuf++);
  912. if (ret)
  913. goto out;
  914. read++;
  915. cnt--;
  916. if (!(iter->flags & ~FTRACE_ITER_CONT)) {
  917. /* skip white space */
  918. while (cnt && isspace(ch)) {
  919. ret = get_user(ch, ubuf++);
  920. if (ret)
  921. goto out;
  922. read++;
  923. cnt--;
  924. }
  925. if (isspace(ch)) {
  926. file->f_pos += read;
  927. ret = read;
  928. goto out;
  929. }
  930. iter->buffer_idx = 0;
  931. }
  932. while (cnt && !isspace(ch)) {
  933. if (iter->buffer_idx < FTRACE_BUFF_MAX)
  934. iter->buffer[iter->buffer_idx++] = ch;
  935. else {
  936. ret = -EINVAL;
  937. goto out;
  938. }
  939. ret = get_user(ch, ubuf++);
  940. if (ret)
  941. goto out;
  942. read++;
  943. cnt--;
  944. }
  945. if (isspace(ch)) {
  946. iter->filtered++;
  947. iter->buffer[iter->buffer_idx] = 0;
  948. ftrace_match(iter->buffer, iter->buffer_idx, enable);
  949. iter->buffer_idx = 0;
  950. } else
  951. iter->flags |= FTRACE_ITER_CONT;
  952. file->f_pos += read;
  953. ret = read;
  954. out:
  955. mutex_unlock(&ftrace_regex_lock);
  956. return ret;
  957. }
  958. static ssize_t
  959. ftrace_filter_write(struct file *file, const char __user *ubuf,
  960. size_t cnt, loff_t *ppos)
  961. {
  962. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  963. }
  964. static ssize_t
  965. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  966. size_t cnt, loff_t *ppos)
  967. {
  968. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  969. }
  970. static void
  971. ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
  972. {
  973. if (unlikely(ftrace_disabled))
  974. return;
  975. mutex_lock(&ftrace_regex_lock);
  976. if (reset)
  977. ftrace_filter_reset(enable);
  978. if (buf)
  979. ftrace_match(buf, len, enable);
  980. mutex_unlock(&ftrace_regex_lock);
  981. }
  982. /**
  983. * ftrace_set_filter - set a function to filter on in ftrace
  984. * @buf - the string that holds the function filter text.
  985. * @len - the length of the string.
  986. * @reset - non zero to reset all filters before applying this filter.
  987. *
  988. * Filters denote which functions should be enabled when tracing is enabled.
  989. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  990. */
  991. void ftrace_set_filter(unsigned char *buf, int len, int reset)
  992. {
  993. ftrace_set_regex(buf, len, reset, 1);
  994. }
  995. /**
  996. * ftrace_set_notrace - set a function to not trace in ftrace
  997. * @buf - the string that holds the function notrace text.
  998. * @len - the length of the string.
  999. * @reset - non zero to reset all filters before applying this filter.
  1000. *
  1001. * Notrace Filters denote which functions should not be enabled when tracing
  1002. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  1003. * for tracing.
  1004. */
  1005. void ftrace_set_notrace(unsigned char *buf, int len, int reset)
  1006. {
  1007. ftrace_set_regex(buf, len, reset, 0);
  1008. }
  1009. static int
  1010. ftrace_regex_release(struct inode *inode, struct file *file, int enable)
  1011. {
  1012. struct seq_file *m = (struct seq_file *)file->private_data;
  1013. struct ftrace_iterator *iter;
  1014. mutex_lock(&ftrace_regex_lock);
  1015. if (file->f_mode & FMODE_READ) {
  1016. iter = m->private;
  1017. seq_release(inode, file);
  1018. } else
  1019. iter = file->private_data;
  1020. if (iter->buffer_idx) {
  1021. iter->filtered++;
  1022. iter->buffer[iter->buffer_idx] = 0;
  1023. ftrace_match(iter->buffer, iter->buffer_idx, enable);
  1024. }
  1025. mutex_lock(&ftrace_sysctl_lock);
  1026. mutex_lock(&ftraced_lock);
  1027. if (iter->filtered && ftraced_suspend && ftrace_enabled)
  1028. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  1029. mutex_unlock(&ftraced_lock);
  1030. mutex_unlock(&ftrace_sysctl_lock);
  1031. kfree(iter);
  1032. mutex_unlock(&ftrace_regex_lock);
  1033. return 0;
  1034. }
  1035. static int
  1036. ftrace_filter_release(struct inode *inode, struct file *file)
  1037. {
  1038. return ftrace_regex_release(inode, file, 1);
  1039. }
  1040. static int
  1041. ftrace_notrace_release(struct inode *inode, struct file *file)
  1042. {
  1043. return ftrace_regex_release(inode, file, 0);
  1044. }
  1045. static ssize_t
  1046. ftraced_read(struct file *filp, char __user *ubuf,
  1047. size_t cnt, loff_t *ppos)
  1048. {
  1049. /* don't worry about races */
  1050. char *buf = ftraced_stop ? "disabled\n" : "enabled\n";
  1051. int r = strlen(buf);
  1052. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1053. }
  1054. static ssize_t
  1055. ftraced_write(struct file *filp, const char __user *ubuf,
  1056. size_t cnt, loff_t *ppos)
  1057. {
  1058. char buf[64];
  1059. long val;
  1060. int ret;
  1061. if (cnt >= sizeof(buf))
  1062. return -EINVAL;
  1063. if (copy_from_user(&buf, ubuf, cnt))
  1064. return -EFAULT;
  1065. if (strncmp(buf, "enable", 6) == 0)
  1066. val = 1;
  1067. else if (strncmp(buf, "disable", 7) == 0)
  1068. val = 0;
  1069. else {
  1070. buf[cnt] = 0;
  1071. ret = strict_strtoul(buf, 10, &val);
  1072. if (ret < 0)
  1073. return ret;
  1074. val = !!val;
  1075. }
  1076. if (val)
  1077. ftrace_enable_daemon();
  1078. else
  1079. ftrace_disable_daemon();
  1080. filp->f_pos += cnt;
  1081. return cnt;
  1082. }
  1083. static struct file_operations ftrace_avail_fops = {
  1084. .open = ftrace_avail_open,
  1085. .read = seq_read,
  1086. .llseek = seq_lseek,
  1087. .release = ftrace_avail_release,
  1088. };
  1089. static struct file_operations ftrace_failures_fops = {
  1090. .open = ftrace_failures_open,
  1091. .read = seq_read,
  1092. .llseek = seq_lseek,
  1093. .release = ftrace_avail_release,
  1094. };
  1095. static struct file_operations ftrace_filter_fops = {
  1096. .open = ftrace_filter_open,
  1097. .read = ftrace_regex_read,
  1098. .write = ftrace_filter_write,
  1099. .llseek = ftrace_regex_lseek,
  1100. .release = ftrace_filter_release,
  1101. };
  1102. static struct file_operations ftrace_notrace_fops = {
  1103. .open = ftrace_notrace_open,
  1104. .read = ftrace_regex_read,
  1105. .write = ftrace_notrace_write,
  1106. .llseek = ftrace_regex_lseek,
  1107. .release = ftrace_notrace_release,
  1108. };
  1109. static struct file_operations ftraced_fops = {
  1110. .open = tracing_open_generic,
  1111. .read = ftraced_read,
  1112. .write = ftraced_write,
  1113. };
  1114. /**
  1115. * ftrace_force_update - force an update to all recording ftrace functions
  1116. */
  1117. int ftrace_force_update(void)
  1118. {
  1119. int ret = 0;
  1120. if (unlikely(ftrace_disabled))
  1121. return -ENODEV;
  1122. mutex_lock(&ftrace_sysctl_lock);
  1123. mutex_lock(&ftraced_lock);
  1124. /*
  1125. * If ftraced_trigger is not set, then there is nothing
  1126. * to update.
  1127. */
  1128. if (ftraced_trigger && !ftrace_update_code())
  1129. ret = -EBUSY;
  1130. mutex_unlock(&ftraced_lock);
  1131. mutex_unlock(&ftrace_sysctl_lock);
  1132. return ret;
  1133. }
  1134. static void ftrace_force_shutdown(void)
  1135. {
  1136. struct task_struct *task;
  1137. int command = FTRACE_DISABLE_CALLS | FTRACE_UPDATE_TRACE_FUNC;
  1138. mutex_lock(&ftraced_lock);
  1139. task = ftraced_task;
  1140. ftraced_task = NULL;
  1141. ftraced_suspend = -1;
  1142. ftrace_run_update_code(command);
  1143. mutex_unlock(&ftraced_lock);
  1144. if (task)
  1145. kthread_stop(task);
  1146. }
  1147. static __init int ftrace_init_debugfs(void)
  1148. {
  1149. struct dentry *d_tracer;
  1150. struct dentry *entry;
  1151. d_tracer = tracing_init_dentry();
  1152. entry = debugfs_create_file("available_filter_functions", 0444,
  1153. d_tracer, NULL, &ftrace_avail_fops);
  1154. if (!entry)
  1155. pr_warning("Could not create debugfs "
  1156. "'available_filter_functions' entry\n");
  1157. entry = debugfs_create_file("failures", 0444,
  1158. d_tracer, NULL, &ftrace_failures_fops);
  1159. if (!entry)
  1160. pr_warning("Could not create debugfs 'failures' entry\n");
  1161. entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
  1162. NULL, &ftrace_filter_fops);
  1163. if (!entry)
  1164. pr_warning("Could not create debugfs "
  1165. "'set_ftrace_filter' entry\n");
  1166. entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
  1167. NULL, &ftrace_notrace_fops);
  1168. if (!entry)
  1169. pr_warning("Could not create debugfs "
  1170. "'set_ftrace_notrace' entry\n");
  1171. entry = debugfs_create_file("ftraced_enabled", 0644, d_tracer,
  1172. NULL, &ftraced_fops);
  1173. if (!entry)
  1174. pr_warning("Could not create debugfs "
  1175. "'ftraced_enabled' entry\n");
  1176. return 0;
  1177. }
  1178. fs_initcall(ftrace_init_debugfs);
  1179. static int __init ftrace_dynamic_init(void)
  1180. {
  1181. struct task_struct *p;
  1182. unsigned long addr;
  1183. int ret;
  1184. addr = (unsigned long)ftrace_record_ip;
  1185. stop_machine_run(ftrace_dyn_arch_init, &addr, NR_CPUS);
  1186. /* ftrace_dyn_arch_init places the return code in addr */
  1187. if (addr) {
  1188. ret = (int)addr;
  1189. goto failed;
  1190. }
  1191. ret = ftrace_dyn_table_alloc();
  1192. if (ret)
  1193. goto failed;
  1194. p = kthread_run(ftraced, NULL, "ftraced");
  1195. if (IS_ERR(p)) {
  1196. ret = -1;
  1197. goto failed;
  1198. }
  1199. last_ftrace_enabled = ftrace_enabled = 1;
  1200. ftraced_task = p;
  1201. return 0;
  1202. failed:
  1203. ftrace_disabled = 1;
  1204. return ret;
  1205. }
  1206. core_initcall(ftrace_dynamic_init);
  1207. #else
  1208. # define ftrace_startup() do { } while (0)
  1209. # define ftrace_shutdown() do { } while (0)
  1210. # define ftrace_startup_sysctl() do { } while (0)
  1211. # define ftrace_shutdown_sysctl() do { } while (0)
  1212. # define ftrace_force_shutdown() do { } while (0)
  1213. #endif /* CONFIG_DYNAMIC_FTRACE */
  1214. /**
  1215. * ftrace_kill - totally shutdown ftrace
  1216. *
  1217. * This is a safety measure. If something was detected that seems
  1218. * wrong, calling this function will keep ftrace from doing
  1219. * any more modifications, and updates.
  1220. * used when something went wrong.
  1221. */
  1222. void ftrace_kill(void)
  1223. {
  1224. mutex_lock(&ftrace_sysctl_lock);
  1225. ftrace_disabled = 1;
  1226. ftrace_enabled = 0;
  1227. clear_ftrace_function();
  1228. mutex_unlock(&ftrace_sysctl_lock);
  1229. /* Try to totally disable ftrace */
  1230. ftrace_force_shutdown();
  1231. }
  1232. /**
  1233. * register_ftrace_function - register a function for profiling
  1234. * @ops - ops structure that holds the function for profiling.
  1235. *
  1236. * Register a function to be called by all functions in the
  1237. * kernel.
  1238. *
  1239. * Note: @ops->func and all the functions it calls must be labeled
  1240. * with "notrace", otherwise it will go into a
  1241. * recursive loop.
  1242. */
  1243. int register_ftrace_function(struct ftrace_ops *ops)
  1244. {
  1245. int ret;
  1246. if (unlikely(ftrace_disabled))
  1247. return -1;
  1248. mutex_lock(&ftrace_sysctl_lock);
  1249. ret = __register_ftrace_function(ops);
  1250. ftrace_startup();
  1251. mutex_unlock(&ftrace_sysctl_lock);
  1252. return ret;
  1253. }
  1254. /**
  1255. * unregister_ftrace_function - unresgister a function for profiling.
  1256. * @ops - ops structure that holds the function to unregister
  1257. *
  1258. * Unregister a function that was added to be called by ftrace profiling.
  1259. */
  1260. int unregister_ftrace_function(struct ftrace_ops *ops)
  1261. {
  1262. int ret;
  1263. mutex_lock(&ftrace_sysctl_lock);
  1264. ret = __unregister_ftrace_function(ops);
  1265. ftrace_shutdown();
  1266. mutex_unlock(&ftrace_sysctl_lock);
  1267. return ret;
  1268. }
  1269. int
  1270. ftrace_enable_sysctl(struct ctl_table *table, int write,
  1271. struct file *file, void __user *buffer, size_t *lenp,
  1272. loff_t *ppos)
  1273. {
  1274. int ret;
  1275. if (unlikely(ftrace_disabled))
  1276. return -ENODEV;
  1277. mutex_lock(&ftrace_sysctl_lock);
  1278. ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
  1279. if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
  1280. goto out;
  1281. last_ftrace_enabled = ftrace_enabled;
  1282. if (ftrace_enabled) {
  1283. ftrace_startup_sysctl();
  1284. /* we are starting ftrace again */
  1285. if (ftrace_list != &ftrace_list_end) {
  1286. if (ftrace_list->next == &ftrace_list_end)
  1287. ftrace_trace_function = ftrace_list->func;
  1288. else
  1289. ftrace_trace_function = ftrace_list_func;
  1290. }
  1291. } else {
  1292. /* stopping ftrace calls (just send to ftrace_stub) */
  1293. ftrace_trace_function = ftrace_stub;
  1294. ftrace_shutdown_sysctl();
  1295. }
  1296. out:
  1297. mutex_unlock(&ftrace_sysctl_lock);
  1298. return ret;
  1299. }