ftrace.c 39 KB

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