ftrace.c 38 KB

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