ftrace.c 39 KB

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