ftrace.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  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) \
  138. spin_unlock_irqrestore(&ftrace_hash_lock, flags)
  139. #else
  140. /* This is protected via the ftrace_lock with MCOUNT_RECORD. */
  141. #define ftrace_hash_lock(flags) do { (void)(flags); } while (0)
  142. #define ftrace_hash_unlock(flags) do { } while(0)
  143. #endif
  144. /*
  145. * Since MCOUNT_ADDR may point to mcount itself, we do not want
  146. * to get it confused by reading a reference in the code as we
  147. * are parsing on objcopy output of text. Use a variable for
  148. * it instead.
  149. */
  150. static unsigned long mcount_addr = MCOUNT_ADDR;
  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 void print_ip_ins(const char *fmt, unsigned char *p)
  473. {
  474. int i;
  475. printk(KERN_CONT "%s", fmt);
  476. for (i = 0; i < MCOUNT_INSN_SIZE; i++)
  477. printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
  478. }
  479. static int
  480. ftrace_code_disable(struct dyn_ftrace *rec)
  481. {
  482. unsigned long ip;
  483. unsigned char *nop, *call;
  484. int failed;
  485. ip = rec->ip;
  486. nop = ftrace_nop_replace();
  487. call = ftrace_call_replace(ip, mcount_addr);
  488. failed = ftrace_modify_code(ip, call, nop);
  489. if (failed) {
  490. switch (failed) {
  491. case 1:
  492. WARN_ON_ONCE(1);
  493. pr_info("ftrace faulted on modifying ");
  494. print_ip_sym(ip);
  495. break;
  496. case 2:
  497. WARN_ON_ONCE(1);
  498. pr_info("ftrace failed to modify ");
  499. print_ip_sym(ip);
  500. print_ip_ins(" expected: ", call);
  501. print_ip_ins(" actual: ", (unsigned char *)ip);
  502. print_ip_ins(" replace: ", nop);
  503. printk(KERN_CONT "\n");
  504. break;
  505. }
  506. rec->flags |= FTRACE_FL_FAILED;
  507. return 0;
  508. }
  509. return 1;
  510. }
  511. static int __ftrace_update_code(void *ignore);
  512. static int __ftrace_modify_code(void *data)
  513. {
  514. unsigned long addr;
  515. int *command = data;
  516. if (*command & FTRACE_ENABLE_CALLS) {
  517. /*
  518. * Update any recorded ips now that we have the
  519. * machine stopped
  520. */
  521. __ftrace_update_code(NULL);
  522. ftrace_replace_code(1);
  523. tracing_on = 1;
  524. } else if (*command & FTRACE_DISABLE_CALLS) {
  525. ftrace_replace_code(0);
  526. tracing_on = 0;
  527. }
  528. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  529. ftrace_update_ftrace_func(ftrace_trace_function);
  530. if (*command & FTRACE_ENABLE_MCOUNT) {
  531. addr = (unsigned long)ftrace_record_ip;
  532. ftrace_mcount_set(&addr);
  533. } else if (*command & FTRACE_DISABLE_MCOUNT) {
  534. addr = (unsigned long)ftrace_stub;
  535. ftrace_mcount_set(&addr);
  536. }
  537. return 0;
  538. }
  539. static void ftrace_run_update_code(int command)
  540. {
  541. stop_machine(__ftrace_modify_code, &command, NULL);
  542. }
  543. void ftrace_disable_daemon(void)
  544. {
  545. /* Stop the daemon from calling kstop_machine */
  546. mutex_lock(&ftraced_lock);
  547. ftraced_stop = 1;
  548. mutex_unlock(&ftraced_lock);
  549. ftrace_force_update();
  550. }
  551. void ftrace_enable_daemon(void)
  552. {
  553. mutex_lock(&ftraced_lock);
  554. ftraced_stop = 0;
  555. mutex_unlock(&ftraced_lock);
  556. ftrace_force_update();
  557. }
  558. static ftrace_func_t saved_ftrace_func;
  559. static void ftrace_startup(void)
  560. {
  561. int command = 0;
  562. if (unlikely(ftrace_disabled))
  563. return;
  564. mutex_lock(&ftraced_lock);
  565. ftraced_suspend++;
  566. if (ftraced_suspend == 1)
  567. command |= FTRACE_ENABLE_CALLS;
  568. if (saved_ftrace_func != ftrace_trace_function) {
  569. saved_ftrace_func = ftrace_trace_function;
  570. command |= FTRACE_UPDATE_TRACE_FUNC;
  571. }
  572. if (!command || !ftrace_enabled)
  573. goto out;
  574. ftrace_run_update_code(command);
  575. out:
  576. mutex_unlock(&ftraced_lock);
  577. }
  578. static void ftrace_shutdown(void)
  579. {
  580. int command = 0;
  581. if (unlikely(ftrace_disabled))
  582. return;
  583. mutex_lock(&ftraced_lock);
  584. ftraced_suspend--;
  585. if (!ftraced_suspend)
  586. command |= FTRACE_DISABLE_CALLS;
  587. if (saved_ftrace_func != ftrace_trace_function) {
  588. saved_ftrace_func = ftrace_trace_function;
  589. command |= FTRACE_UPDATE_TRACE_FUNC;
  590. }
  591. if (!command || !ftrace_enabled)
  592. goto out;
  593. ftrace_run_update_code(command);
  594. out:
  595. mutex_unlock(&ftraced_lock);
  596. }
  597. static void ftrace_startup_sysctl(void)
  598. {
  599. int command = FTRACE_ENABLE_MCOUNT;
  600. if (unlikely(ftrace_disabled))
  601. return;
  602. mutex_lock(&ftraced_lock);
  603. /* Force update next time */
  604. saved_ftrace_func = NULL;
  605. /* ftraced_suspend is true if we want ftrace running */
  606. if (ftraced_suspend)
  607. command |= FTRACE_ENABLE_CALLS;
  608. ftrace_run_update_code(command);
  609. mutex_unlock(&ftraced_lock);
  610. }
  611. static void ftrace_shutdown_sysctl(void)
  612. {
  613. int command = FTRACE_DISABLE_MCOUNT;
  614. if (unlikely(ftrace_disabled))
  615. return;
  616. mutex_lock(&ftraced_lock);
  617. /* ftraced_suspend is true if ftrace is running */
  618. if (ftraced_suspend)
  619. command |= FTRACE_DISABLE_CALLS;
  620. ftrace_run_update_code(command);
  621. mutex_unlock(&ftraced_lock);
  622. }
  623. static cycle_t ftrace_update_time;
  624. static unsigned long ftrace_update_cnt;
  625. unsigned long ftrace_update_tot_cnt;
  626. static int __ftrace_update_code(void *ignore)
  627. {
  628. int i, save_ftrace_enabled;
  629. cycle_t start, stop;
  630. struct dyn_ftrace *p;
  631. struct hlist_node *t, *n;
  632. struct hlist_head *head, temp_list;
  633. /* Don't be recording funcs now */
  634. ftrace_record_suspend++;
  635. save_ftrace_enabled = ftrace_enabled;
  636. ftrace_enabled = 0;
  637. start = ftrace_now(raw_smp_processor_id());
  638. ftrace_update_cnt = 0;
  639. /* No locks needed, the machine is stopped! */
  640. for (i = 0; i < FTRACE_HASHSIZE; i++) {
  641. INIT_HLIST_HEAD(&temp_list);
  642. head = &ftrace_hash[i];
  643. /* all CPUS are stopped, we are safe to modify code */
  644. hlist_for_each_entry_safe(p, t, n, head, node) {
  645. /* Skip over failed records which have not been
  646. * freed. */
  647. if (p->flags & FTRACE_FL_FAILED)
  648. continue;
  649. /* Unconverted records are always at the head of the
  650. * hash bucket. Once we encounter a converted record,
  651. * simply skip over to the next bucket. Saves ftraced
  652. * some processor cycles (ftrace does its bid for
  653. * global warming :-p ). */
  654. if (p->flags & (FTRACE_FL_CONVERTED))
  655. break;
  656. /* Ignore updates to this record's mcount site.
  657. * Reintroduce this record at the head of this
  658. * bucket to attempt to "convert" it again if
  659. * the kprobe on it is unregistered before the
  660. * next run. */
  661. if (get_kprobe((void *)p->ip)) {
  662. ftrace_del_hash(p);
  663. INIT_HLIST_NODE(&p->node);
  664. hlist_add_head(&p->node, &temp_list);
  665. freeze_record(p);
  666. continue;
  667. } else {
  668. unfreeze_record(p);
  669. }
  670. /* convert record (i.e, patch mcount-call with NOP) */
  671. if (ftrace_code_disable(p)) {
  672. p->flags |= FTRACE_FL_CONVERTED;
  673. ftrace_update_cnt++;
  674. } else {
  675. if ((system_state == SYSTEM_BOOTING) ||
  676. !core_kernel_text(p->ip)) {
  677. ftrace_del_hash(p);
  678. ftrace_free_rec(p);
  679. }
  680. }
  681. }
  682. hlist_for_each_entry_safe(p, t, n, &temp_list, node) {
  683. hlist_del(&p->node);
  684. INIT_HLIST_NODE(&p->node);
  685. hlist_add_head(&p->node, head);
  686. }
  687. }
  688. stop = ftrace_now(raw_smp_processor_id());
  689. ftrace_update_time = stop - start;
  690. ftrace_update_tot_cnt += ftrace_update_cnt;
  691. ftraced_trigger = 0;
  692. ftrace_enabled = save_ftrace_enabled;
  693. ftrace_record_suspend--;
  694. return 0;
  695. }
  696. static int ftrace_update_code(void)
  697. {
  698. if (unlikely(ftrace_disabled) ||
  699. !ftrace_enabled || !ftraced_trigger)
  700. return 0;
  701. stop_machine(__ftrace_update_code, NULL, NULL);
  702. return 1;
  703. }
  704. static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
  705. {
  706. struct ftrace_page *pg;
  707. int cnt;
  708. int i;
  709. /* allocate a few pages */
  710. ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
  711. if (!ftrace_pages_start)
  712. return -1;
  713. /*
  714. * Allocate a few more pages.
  715. *
  716. * TODO: have some parser search vmlinux before
  717. * final linking to find all calls to ftrace.
  718. * Then we can:
  719. * a) know how many pages to allocate.
  720. * and/or
  721. * b) set up the table then.
  722. *
  723. * The dynamic code is still necessary for
  724. * modules.
  725. */
  726. pg = ftrace_pages = ftrace_pages_start;
  727. cnt = num_to_init / ENTRIES_PER_PAGE;
  728. pr_info("ftrace: allocating %ld hash entries in %d pages\n",
  729. num_to_init, cnt);
  730. for (i = 0; i < cnt; i++) {
  731. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  732. /* If we fail, we'll try later anyway */
  733. if (!pg->next)
  734. break;
  735. pg = pg->next;
  736. }
  737. return 0;
  738. }
  739. enum {
  740. FTRACE_ITER_FILTER = (1 << 0),
  741. FTRACE_ITER_CONT = (1 << 1),
  742. FTRACE_ITER_NOTRACE = (1 << 2),
  743. FTRACE_ITER_FAILURES = (1 << 3),
  744. };
  745. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  746. struct ftrace_iterator {
  747. loff_t pos;
  748. struct ftrace_page *pg;
  749. unsigned idx;
  750. unsigned flags;
  751. unsigned char buffer[FTRACE_BUFF_MAX+1];
  752. unsigned buffer_idx;
  753. unsigned filtered;
  754. };
  755. static void *
  756. t_next(struct seq_file *m, void *v, loff_t *pos)
  757. {
  758. struct ftrace_iterator *iter = m->private;
  759. struct dyn_ftrace *rec = NULL;
  760. (*pos)++;
  761. /* should not be called from interrupt context */
  762. spin_lock(&ftrace_lock);
  763. retry:
  764. if (iter->idx >= iter->pg->index) {
  765. if (iter->pg->next) {
  766. iter->pg = iter->pg->next;
  767. iter->idx = 0;
  768. goto retry;
  769. }
  770. } else {
  771. rec = &iter->pg->records[iter->idx++];
  772. if ((rec->flags & FTRACE_FL_FREE) ||
  773. (!(iter->flags & FTRACE_ITER_FAILURES) &&
  774. (rec->flags & FTRACE_FL_FAILED)) ||
  775. ((iter->flags & FTRACE_ITER_FAILURES) &&
  776. !(rec->flags & FTRACE_FL_FAILED)) ||
  777. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  778. !(rec->flags & FTRACE_FL_NOTRACE))) {
  779. rec = NULL;
  780. goto retry;
  781. }
  782. }
  783. spin_unlock(&ftrace_lock);
  784. iter->pos = *pos;
  785. return rec;
  786. }
  787. static void *t_start(struct seq_file *m, loff_t *pos)
  788. {
  789. struct ftrace_iterator *iter = m->private;
  790. void *p = NULL;
  791. loff_t l = -1;
  792. if (*pos != iter->pos) {
  793. for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
  794. ;
  795. } else {
  796. l = *pos;
  797. p = t_next(m, p, &l);
  798. }
  799. return p;
  800. }
  801. static void t_stop(struct seq_file *m, void *p)
  802. {
  803. }
  804. static int t_show(struct seq_file *m, void *v)
  805. {
  806. struct dyn_ftrace *rec = v;
  807. char str[KSYM_SYMBOL_LEN];
  808. if (!rec)
  809. return 0;
  810. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  811. seq_printf(m, "%s\n", str);
  812. return 0;
  813. }
  814. static struct seq_operations show_ftrace_seq_ops = {
  815. .start = t_start,
  816. .next = t_next,
  817. .stop = t_stop,
  818. .show = t_show,
  819. };
  820. static int
  821. ftrace_avail_open(struct inode *inode, struct file *file)
  822. {
  823. struct ftrace_iterator *iter;
  824. int ret;
  825. if (unlikely(ftrace_disabled))
  826. return -ENODEV;
  827. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  828. if (!iter)
  829. return -ENOMEM;
  830. iter->pg = ftrace_pages_start;
  831. iter->pos = -1;
  832. ret = seq_open(file, &show_ftrace_seq_ops);
  833. if (!ret) {
  834. struct seq_file *m = file->private_data;
  835. m->private = iter;
  836. } else {
  837. kfree(iter);
  838. }
  839. return ret;
  840. }
  841. int ftrace_avail_release(struct inode *inode, struct file *file)
  842. {
  843. struct seq_file *m = (struct seq_file *)file->private_data;
  844. struct ftrace_iterator *iter = m->private;
  845. seq_release(inode, file);
  846. kfree(iter);
  847. return 0;
  848. }
  849. static int
  850. ftrace_failures_open(struct inode *inode, struct file *file)
  851. {
  852. int ret;
  853. struct seq_file *m;
  854. struct ftrace_iterator *iter;
  855. ret = ftrace_avail_open(inode, file);
  856. if (!ret) {
  857. m = (struct seq_file *)file->private_data;
  858. iter = (struct ftrace_iterator *)m->private;
  859. iter->flags = FTRACE_ITER_FAILURES;
  860. }
  861. return ret;
  862. }
  863. static void ftrace_filter_reset(int enable)
  864. {
  865. struct ftrace_page *pg;
  866. struct dyn_ftrace *rec;
  867. unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  868. unsigned i;
  869. /* should not be called from interrupt context */
  870. spin_lock(&ftrace_lock);
  871. if (enable)
  872. ftrace_filtered = 0;
  873. pg = ftrace_pages_start;
  874. while (pg) {
  875. for (i = 0; i < pg->index; i++) {
  876. rec = &pg->records[i];
  877. if (rec->flags & FTRACE_FL_FAILED)
  878. continue;
  879. rec->flags &= ~type;
  880. }
  881. pg = pg->next;
  882. }
  883. spin_unlock(&ftrace_lock);
  884. }
  885. static int
  886. ftrace_regex_open(struct inode *inode, struct file *file, int enable)
  887. {
  888. struct ftrace_iterator *iter;
  889. int ret = 0;
  890. if (unlikely(ftrace_disabled))
  891. return -ENODEV;
  892. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  893. if (!iter)
  894. return -ENOMEM;
  895. mutex_lock(&ftrace_regex_lock);
  896. if ((file->f_mode & FMODE_WRITE) &&
  897. !(file->f_flags & O_APPEND))
  898. ftrace_filter_reset(enable);
  899. if (file->f_mode & FMODE_READ) {
  900. iter->pg = ftrace_pages_start;
  901. iter->pos = -1;
  902. iter->flags = enable ? FTRACE_ITER_FILTER :
  903. FTRACE_ITER_NOTRACE;
  904. ret = seq_open(file, &show_ftrace_seq_ops);
  905. if (!ret) {
  906. struct seq_file *m = file->private_data;
  907. m->private = iter;
  908. } else
  909. kfree(iter);
  910. } else
  911. file->private_data = iter;
  912. mutex_unlock(&ftrace_regex_lock);
  913. return ret;
  914. }
  915. static int
  916. ftrace_filter_open(struct inode *inode, struct file *file)
  917. {
  918. return ftrace_regex_open(inode, file, 1);
  919. }
  920. static int
  921. ftrace_notrace_open(struct inode *inode, struct file *file)
  922. {
  923. return ftrace_regex_open(inode, file, 0);
  924. }
  925. static ssize_t
  926. ftrace_regex_read(struct file *file, char __user *ubuf,
  927. size_t cnt, loff_t *ppos)
  928. {
  929. if (file->f_mode & FMODE_READ)
  930. return seq_read(file, ubuf, cnt, ppos);
  931. else
  932. return -EPERM;
  933. }
  934. static loff_t
  935. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  936. {
  937. loff_t ret;
  938. if (file->f_mode & FMODE_READ)
  939. ret = seq_lseek(file, offset, origin);
  940. else
  941. file->f_pos = ret = 1;
  942. return ret;
  943. }
  944. enum {
  945. MATCH_FULL,
  946. MATCH_FRONT_ONLY,
  947. MATCH_MIDDLE_ONLY,
  948. MATCH_END_ONLY,
  949. };
  950. static void
  951. ftrace_match(unsigned char *buff, int len, int enable)
  952. {
  953. char str[KSYM_SYMBOL_LEN];
  954. char *search = NULL;
  955. struct ftrace_page *pg;
  956. struct dyn_ftrace *rec;
  957. int type = MATCH_FULL;
  958. unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  959. unsigned i, match = 0, search_len = 0;
  960. for (i = 0; i < len; i++) {
  961. if (buff[i] == '*') {
  962. if (!i) {
  963. search = buff + i + 1;
  964. type = MATCH_END_ONLY;
  965. search_len = len - (i + 1);
  966. } else {
  967. if (type == MATCH_END_ONLY) {
  968. type = MATCH_MIDDLE_ONLY;
  969. } else {
  970. match = i;
  971. type = MATCH_FRONT_ONLY;
  972. }
  973. buff[i] = 0;
  974. break;
  975. }
  976. }
  977. }
  978. /* should not be called from interrupt context */
  979. spin_lock(&ftrace_lock);
  980. if (enable)
  981. ftrace_filtered = 1;
  982. pg = ftrace_pages_start;
  983. while (pg) {
  984. for (i = 0; i < pg->index; i++) {
  985. int matched = 0;
  986. char *ptr;
  987. rec = &pg->records[i];
  988. if (rec->flags & FTRACE_FL_FAILED)
  989. continue;
  990. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  991. switch (type) {
  992. case MATCH_FULL:
  993. if (strcmp(str, buff) == 0)
  994. matched = 1;
  995. break;
  996. case MATCH_FRONT_ONLY:
  997. if (memcmp(str, buff, match) == 0)
  998. matched = 1;
  999. break;
  1000. case MATCH_MIDDLE_ONLY:
  1001. if (strstr(str, search))
  1002. matched = 1;
  1003. break;
  1004. case MATCH_END_ONLY:
  1005. ptr = strstr(str, search);
  1006. if (ptr && (ptr[search_len] == 0))
  1007. matched = 1;
  1008. break;
  1009. }
  1010. if (matched)
  1011. rec->flags |= flag;
  1012. }
  1013. pg = pg->next;
  1014. }
  1015. spin_unlock(&ftrace_lock);
  1016. }
  1017. static ssize_t
  1018. ftrace_regex_write(struct file *file, const char __user *ubuf,
  1019. size_t cnt, loff_t *ppos, int enable)
  1020. {
  1021. struct ftrace_iterator *iter;
  1022. char ch;
  1023. size_t read = 0;
  1024. ssize_t ret;
  1025. if (!cnt || cnt < 0)
  1026. return 0;
  1027. mutex_lock(&ftrace_regex_lock);
  1028. if (file->f_mode & FMODE_READ) {
  1029. struct seq_file *m = file->private_data;
  1030. iter = m->private;
  1031. } else
  1032. iter = file->private_data;
  1033. if (!*ppos) {
  1034. iter->flags &= ~FTRACE_ITER_CONT;
  1035. iter->buffer_idx = 0;
  1036. }
  1037. ret = get_user(ch, ubuf++);
  1038. if (ret)
  1039. goto out;
  1040. read++;
  1041. cnt--;
  1042. if (!(iter->flags & ~FTRACE_ITER_CONT)) {
  1043. /* skip white space */
  1044. while (cnt && isspace(ch)) {
  1045. ret = get_user(ch, ubuf++);
  1046. if (ret)
  1047. goto out;
  1048. read++;
  1049. cnt--;
  1050. }
  1051. if (isspace(ch)) {
  1052. file->f_pos += read;
  1053. ret = read;
  1054. goto out;
  1055. }
  1056. iter->buffer_idx = 0;
  1057. }
  1058. while (cnt && !isspace(ch)) {
  1059. if (iter->buffer_idx < FTRACE_BUFF_MAX)
  1060. iter->buffer[iter->buffer_idx++] = ch;
  1061. else {
  1062. ret = -EINVAL;
  1063. goto out;
  1064. }
  1065. ret = get_user(ch, ubuf++);
  1066. if (ret)
  1067. goto out;
  1068. read++;
  1069. cnt--;
  1070. }
  1071. if (isspace(ch)) {
  1072. iter->filtered++;
  1073. iter->buffer[iter->buffer_idx] = 0;
  1074. ftrace_match(iter->buffer, iter->buffer_idx, enable);
  1075. iter->buffer_idx = 0;
  1076. } else
  1077. iter->flags |= FTRACE_ITER_CONT;
  1078. file->f_pos += read;
  1079. ret = read;
  1080. out:
  1081. mutex_unlock(&ftrace_regex_lock);
  1082. return ret;
  1083. }
  1084. static ssize_t
  1085. ftrace_filter_write(struct file *file, const char __user *ubuf,
  1086. size_t cnt, loff_t *ppos)
  1087. {
  1088. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  1089. }
  1090. static ssize_t
  1091. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  1092. size_t cnt, loff_t *ppos)
  1093. {
  1094. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  1095. }
  1096. static void
  1097. ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
  1098. {
  1099. if (unlikely(ftrace_disabled))
  1100. return;
  1101. mutex_lock(&ftrace_regex_lock);
  1102. if (reset)
  1103. ftrace_filter_reset(enable);
  1104. if (buf)
  1105. ftrace_match(buf, len, enable);
  1106. mutex_unlock(&ftrace_regex_lock);
  1107. }
  1108. /**
  1109. * ftrace_set_filter - set a function to filter on in ftrace
  1110. * @buf - the string that holds the function filter text.
  1111. * @len - the length of the string.
  1112. * @reset - non zero to reset all filters before applying this filter.
  1113. *
  1114. * Filters denote which functions should be enabled when tracing is enabled.
  1115. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  1116. */
  1117. void ftrace_set_filter(unsigned char *buf, int len, int reset)
  1118. {
  1119. ftrace_set_regex(buf, len, reset, 1);
  1120. }
  1121. /**
  1122. * ftrace_set_notrace - set a function to not trace in ftrace
  1123. * @buf - the string that holds the function notrace text.
  1124. * @len - the length of the string.
  1125. * @reset - non zero to reset all filters before applying this filter.
  1126. *
  1127. * Notrace Filters denote which functions should not be enabled when tracing
  1128. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  1129. * for tracing.
  1130. */
  1131. void ftrace_set_notrace(unsigned char *buf, int len, int reset)
  1132. {
  1133. ftrace_set_regex(buf, len, reset, 0);
  1134. }
  1135. static int
  1136. ftrace_regex_release(struct inode *inode, struct file *file, int enable)
  1137. {
  1138. struct seq_file *m = (struct seq_file *)file->private_data;
  1139. struct ftrace_iterator *iter;
  1140. mutex_lock(&ftrace_regex_lock);
  1141. if (file->f_mode & FMODE_READ) {
  1142. iter = m->private;
  1143. seq_release(inode, file);
  1144. } else
  1145. iter = file->private_data;
  1146. if (iter->buffer_idx) {
  1147. iter->filtered++;
  1148. iter->buffer[iter->buffer_idx] = 0;
  1149. ftrace_match(iter->buffer, iter->buffer_idx, enable);
  1150. }
  1151. mutex_lock(&ftrace_sysctl_lock);
  1152. mutex_lock(&ftraced_lock);
  1153. if (iter->filtered && ftraced_suspend && ftrace_enabled)
  1154. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  1155. mutex_unlock(&ftraced_lock);
  1156. mutex_unlock(&ftrace_sysctl_lock);
  1157. kfree(iter);
  1158. mutex_unlock(&ftrace_regex_lock);
  1159. return 0;
  1160. }
  1161. static int
  1162. ftrace_filter_release(struct inode *inode, struct file *file)
  1163. {
  1164. return ftrace_regex_release(inode, file, 1);
  1165. }
  1166. static int
  1167. ftrace_notrace_release(struct inode *inode, struct file *file)
  1168. {
  1169. return ftrace_regex_release(inode, file, 0);
  1170. }
  1171. static ssize_t
  1172. ftraced_read(struct file *filp, char __user *ubuf,
  1173. size_t cnt, loff_t *ppos)
  1174. {
  1175. /* don't worry about races */
  1176. char *buf = ftraced_stop ? "disabled\n" : "enabled\n";
  1177. int r = strlen(buf);
  1178. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1179. }
  1180. static ssize_t
  1181. ftraced_write(struct file *filp, const char __user *ubuf,
  1182. size_t cnt, loff_t *ppos)
  1183. {
  1184. char buf[64];
  1185. long val;
  1186. int ret;
  1187. if (cnt >= sizeof(buf))
  1188. return -EINVAL;
  1189. if (copy_from_user(&buf, ubuf, cnt))
  1190. return -EFAULT;
  1191. if (strncmp(buf, "enable", 6) == 0)
  1192. val = 1;
  1193. else if (strncmp(buf, "disable", 7) == 0)
  1194. val = 0;
  1195. else {
  1196. buf[cnt] = 0;
  1197. ret = strict_strtoul(buf, 10, &val);
  1198. if (ret < 0)
  1199. return ret;
  1200. val = !!val;
  1201. }
  1202. if (val)
  1203. ftrace_enable_daemon();
  1204. else
  1205. ftrace_disable_daemon();
  1206. filp->f_pos += cnt;
  1207. return cnt;
  1208. }
  1209. static struct file_operations ftrace_avail_fops = {
  1210. .open = ftrace_avail_open,
  1211. .read = seq_read,
  1212. .llseek = seq_lseek,
  1213. .release = ftrace_avail_release,
  1214. };
  1215. static struct file_operations ftrace_failures_fops = {
  1216. .open = ftrace_failures_open,
  1217. .read = seq_read,
  1218. .llseek = seq_lseek,
  1219. .release = ftrace_avail_release,
  1220. };
  1221. static struct file_operations ftrace_filter_fops = {
  1222. .open = ftrace_filter_open,
  1223. .read = ftrace_regex_read,
  1224. .write = ftrace_filter_write,
  1225. .llseek = ftrace_regex_lseek,
  1226. .release = ftrace_filter_release,
  1227. };
  1228. static struct file_operations ftrace_notrace_fops = {
  1229. .open = ftrace_notrace_open,
  1230. .read = ftrace_regex_read,
  1231. .write = ftrace_notrace_write,
  1232. .llseek = ftrace_regex_lseek,
  1233. .release = ftrace_notrace_release,
  1234. };
  1235. static struct file_operations ftraced_fops = {
  1236. .open = tracing_open_generic,
  1237. .read = ftraced_read,
  1238. .write = ftraced_write,
  1239. };
  1240. /**
  1241. * ftrace_force_update - force an update to all recording ftrace functions
  1242. */
  1243. int ftrace_force_update(void)
  1244. {
  1245. int ret = 0;
  1246. if (unlikely(ftrace_disabled))
  1247. return -ENODEV;
  1248. mutex_lock(&ftrace_sysctl_lock);
  1249. mutex_lock(&ftraced_lock);
  1250. /*
  1251. * If ftraced_trigger is not set, then there is nothing
  1252. * to update.
  1253. */
  1254. if (ftraced_trigger && !ftrace_update_code())
  1255. ret = -EBUSY;
  1256. mutex_unlock(&ftraced_lock);
  1257. mutex_unlock(&ftrace_sysctl_lock);
  1258. return ret;
  1259. }
  1260. static void ftrace_force_shutdown(void)
  1261. {
  1262. struct task_struct *task;
  1263. int command = FTRACE_DISABLE_CALLS | FTRACE_UPDATE_TRACE_FUNC;
  1264. mutex_lock(&ftraced_lock);
  1265. task = ftraced_task;
  1266. ftraced_task = NULL;
  1267. ftraced_suspend = -1;
  1268. ftrace_run_update_code(command);
  1269. mutex_unlock(&ftraced_lock);
  1270. if (task)
  1271. kthread_stop(task);
  1272. }
  1273. static __init int ftrace_init_debugfs(void)
  1274. {
  1275. struct dentry *d_tracer;
  1276. struct dentry *entry;
  1277. d_tracer = tracing_init_dentry();
  1278. entry = debugfs_create_file("available_filter_functions", 0444,
  1279. d_tracer, NULL, &ftrace_avail_fops);
  1280. if (!entry)
  1281. pr_warning("Could not create debugfs "
  1282. "'available_filter_functions' entry\n");
  1283. entry = debugfs_create_file("failures", 0444,
  1284. d_tracer, NULL, &ftrace_failures_fops);
  1285. if (!entry)
  1286. pr_warning("Could not create debugfs 'failures' entry\n");
  1287. entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
  1288. NULL, &ftrace_filter_fops);
  1289. if (!entry)
  1290. pr_warning("Could not create debugfs "
  1291. "'set_ftrace_filter' entry\n");
  1292. entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
  1293. NULL, &ftrace_notrace_fops);
  1294. if (!entry)
  1295. pr_warning("Could not create debugfs "
  1296. "'set_ftrace_notrace' entry\n");
  1297. entry = debugfs_create_file("ftraced_enabled", 0644, d_tracer,
  1298. NULL, &ftraced_fops);
  1299. if (!entry)
  1300. pr_warning("Could not create debugfs "
  1301. "'ftraced_enabled' entry\n");
  1302. return 0;
  1303. }
  1304. fs_initcall(ftrace_init_debugfs);
  1305. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  1306. static int ftrace_convert_nops(unsigned long *start,
  1307. unsigned long *end)
  1308. {
  1309. unsigned long *p;
  1310. unsigned long addr;
  1311. unsigned long flags;
  1312. p = start;
  1313. while (p < end) {
  1314. addr = ftrace_call_adjust(*p++);
  1315. /* should not be called from interrupt context */
  1316. spin_lock(&ftrace_lock);
  1317. ftrace_record_ip(addr);
  1318. spin_unlock(&ftrace_lock);
  1319. ftrace_shutdown_replenish();
  1320. }
  1321. /* p is ignored */
  1322. local_irq_save(flags);
  1323. __ftrace_update_code(p);
  1324. local_irq_restore(flags);
  1325. return 0;
  1326. }
  1327. void ftrace_init_module(unsigned long *start, unsigned long *end)
  1328. {
  1329. if (ftrace_disabled || start == end)
  1330. return;
  1331. ftrace_convert_nops(start, end);
  1332. }
  1333. extern unsigned long __start_mcount_loc[];
  1334. extern unsigned long __stop_mcount_loc[];
  1335. void __init ftrace_init(void)
  1336. {
  1337. unsigned long count, addr, flags;
  1338. int ret;
  1339. /* Keep the ftrace pointer to the stub */
  1340. addr = (unsigned long)ftrace_stub;
  1341. local_irq_save(flags);
  1342. ftrace_dyn_arch_init(&addr);
  1343. local_irq_restore(flags);
  1344. /* ftrace_dyn_arch_init places the return code in addr */
  1345. if (addr)
  1346. goto failed;
  1347. count = __stop_mcount_loc - __start_mcount_loc;
  1348. ret = ftrace_dyn_table_alloc(count);
  1349. if (ret)
  1350. goto failed;
  1351. last_ftrace_enabled = ftrace_enabled = 1;
  1352. ret = ftrace_convert_nops(__start_mcount_loc,
  1353. __stop_mcount_loc);
  1354. return;
  1355. failed:
  1356. ftrace_disabled = 1;
  1357. }
  1358. #else /* CONFIG_FTRACE_MCOUNT_RECORD */
  1359. static int ftraced(void *ignore)
  1360. {
  1361. unsigned long usecs;
  1362. while (!kthread_should_stop()) {
  1363. set_current_state(TASK_INTERRUPTIBLE);
  1364. /* check once a second */
  1365. schedule_timeout(HZ);
  1366. if (unlikely(ftrace_disabled))
  1367. continue;
  1368. mutex_lock(&ftrace_sysctl_lock);
  1369. mutex_lock(&ftraced_lock);
  1370. if (!ftraced_suspend && !ftraced_stop &&
  1371. ftrace_update_code()) {
  1372. usecs = nsecs_to_usecs(ftrace_update_time);
  1373. if (ftrace_update_tot_cnt > 100000) {
  1374. ftrace_update_tot_cnt = 0;
  1375. pr_info("hm, dftrace overflow: %lu change%s"
  1376. " (%lu total) in %lu usec%s\n",
  1377. ftrace_update_cnt,
  1378. ftrace_update_cnt != 1 ? "s" : "",
  1379. ftrace_update_tot_cnt,
  1380. usecs, usecs != 1 ? "s" : "");
  1381. ftrace_disabled = 1;
  1382. WARN_ON_ONCE(1);
  1383. }
  1384. }
  1385. mutex_unlock(&ftraced_lock);
  1386. mutex_unlock(&ftrace_sysctl_lock);
  1387. ftrace_shutdown_replenish();
  1388. }
  1389. __set_current_state(TASK_RUNNING);
  1390. return 0;
  1391. }
  1392. static int __init ftrace_dynamic_init(void)
  1393. {
  1394. struct task_struct *p;
  1395. unsigned long addr;
  1396. int ret;
  1397. addr = (unsigned long)ftrace_record_ip;
  1398. stop_machine(ftrace_dyn_arch_init, &addr, NULL);
  1399. /* ftrace_dyn_arch_init places the return code in addr */
  1400. if (addr) {
  1401. ret = (int)addr;
  1402. goto failed;
  1403. }
  1404. ret = ftrace_dyn_table_alloc(NR_TO_INIT);
  1405. if (ret)
  1406. goto failed;
  1407. p = kthread_run(ftraced, NULL, "ftraced");
  1408. if (IS_ERR(p)) {
  1409. ret = -1;
  1410. goto failed;
  1411. }
  1412. last_ftrace_enabled = ftrace_enabled = 1;
  1413. ftraced_task = p;
  1414. return 0;
  1415. failed:
  1416. ftrace_disabled = 1;
  1417. return ret;
  1418. }
  1419. core_initcall(ftrace_dynamic_init);
  1420. #endif /* CONFIG_FTRACE_MCOUNT_RECORD */
  1421. #else
  1422. # define ftrace_startup() do { } while (0)
  1423. # define ftrace_shutdown() do { } while (0)
  1424. # define ftrace_startup_sysctl() do { } while (0)
  1425. # define ftrace_shutdown_sysctl() do { } while (0)
  1426. # define ftrace_force_shutdown() do { } while (0)
  1427. #endif /* CONFIG_DYNAMIC_FTRACE */
  1428. /**
  1429. * ftrace_kill_atomic - kill ftrace from critical sections
  1430. *
  1431. * This function should be used by panic code. It stops ftrace
  1432. * but in a not so nice way. If you need to simply kill ftrace
  1433. * from a non-atomic section, use ftrace_kill.
  1434. */
  1435. void ftrace_kill_atomic(void)
  1436. {
  1437. ftrace_disabled = 1;
  1438. ftrace_enabled = 0;
  1439. #ifdef CONFIG_DYNAMIC_FTRACE
  1440. ftraced_suspend = -1;
  1441. #endif
  1442. clear_ftrace_function();
  1443. }
  1444. /**
  1445. * ftrace_kill - totally shutdown ftrace
  1446. *
  1447. * This is a safety measure. If something was detected that seems
  1448. * wrong, calling this function will keep ftrace from doing
  1449. * any more modifications, and updates.
  1450. * used when something went wrong.
  1451. */
  1452. void ftrace_kill(void)
  1453. {
  1454. mutex_lock(&ftrace_sysctl_lock);
  1455. ftrace_disabled = 1;
  1456. ftrace_enabled = 0;
  1457. clear_ftrace_function();
  1458. mutex_unlock(&ftrace_sysctl_lock);
  1459. /* Try to totally disable ftrace */
  1460. ftrace_force_shutdown();
  1461. }
  1462. /**
  1463. * register_ftrace_function - register a function for profiling
  1464. * @ops - ops structure that holds the function for profiling.
  1465. *
  1466. * Register a function to be called by all functions in the
  1467. * kernel.
  1468. *
  1469. * Note: @ops->func and all the functions it calls must be labeled
  1470. * with "notrace", otherwise it will go into a
  1471. * recursive loop.
  1472. */
  1473. int register_ftrace_function(struct ftrace_ops *ops)
  1474. {
  1475. int ret;
  1476. if (unlikely(ftrace_disabled))
  1477. return -1;
  1478. mutex_lock(&ftrace_sysctl_lock);
  1479. ret = __register_ftrace_function(ops);
  1480. ftrace_startup();
  1481. mutex_unlock(&ftrace_sysctl_lock);
  1482. return ret;
  1483. }
  1484. /**
  1485. * unregister_ftrace_function - unresgister a function for profiling.
  1486. * @ops - ops structure that holds the function to unregister
  1487. *
  1488. * Unregister a function that was added to be called by ftrace profiling.
  1489. */
  1490. int unregister_ftrace_function(struct ftrace_ops *ops)
  1491. {
  1492. int ret;
  1493. mutex_lock(&ftrace_sysctl_lock);
  1494. ret = __unregister_ftrace_function(ops);
  1495. ftrace_shutdown();
  1496. mutex_unlock(&ftrace_sysctl_lock);
  1497. return ret;
  1498. }
  1499. int
  1500. ftrace_enable_sysctl(struct ctl_table *table, int write,
  1501. struct file *file, void __user *buffer, size_t *lenp,
  1502. loff_t *ppos)
  1503. {
  1504. int ret;
  1505. if (unlikely(ftrace_disabled))
  1506. return -ENODEV;
  1507. mutex_lock(&ftrace_sysctl_lock);
  1508. ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
  1509. if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
  1510. goto out;
  1511. last_ftrace_enabled = ftrace_enabled;
  1512. if (ftrace_enabled) {
  1513. ftrace_startup_sysctl();
  1514. /* we are starting ftrace again */
  1515. if (ftrace_list != &ftrace_list_end) {
  1516. if (ftrace_list->next == &ftrace_list_end)
  1517. ftrace_trace_function = ftrace_list->func;
  1518. else
  1519. ftrace_trace_function = ftrace_list_func;
  1520. }
  1521. } else {
  1522. /* stopping ftrace calls (just send to ftrace_stub) */
  1523. ftrace_trace_function = ftrace_stub;
  1524. ftrace_shutdown_sysctl();
  1525. }
  1526. out:
  1527. mutex_unlock(&ftrace_sysctl_lock);
  1528. return ret;
  1529. }