ftrace.c 35 KB

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