ftrace.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  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/kthread.h>
  21. #include <linux/hardirq.h>
  22. #include <linux/ftrace.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/sysctl.h>
  25. #include <linux/hash.h>
  26. #include <linux/ctype.h>
  27. #include <linux/list.h>
  28. #include "trace.h"
  29. /* ftrace_enabled is a method to turn ftrace on or off */
  30. int ftrace_enabled __read_mostly;
  31. static int last_ftrace_enabled;
  32. /*
  33. * ftrace_disabled is set when an anomaly is discovered.
  34. * ftrace_disabled is much stronger than ftrace_enabled.
  35. */
  36. static int ftrace_disabled __read_mostly;
  37. static DEFINE_SPINLOCK(ftrace_lock);
  38. static DEFINE_MUTEX(ftrace_sysctl_lock);
  39. static struct ftrace_ops ftrace_list_end __read_mostly =
  40. {
  41. .func = ftrace_stub,
  42. };
  43. static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
  44. ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
  45. /* mcount is defined per arch in assembly */
  46. EXPORT_SYMBOL(mcount);
  47. void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
  48. {
  49. struct ftrace_ops *op = ftrace_list;
  50. /* in case someone actually ports this to alpha! */
  51. read_barrier_depends();
  52. while (op != &ftrace_list_end) {
  53. /* silly alpha */
  54. read_barrier_depends();
  55. op->func(ip, parent_ip);
  56. op = op->next;
  57. };
  58. }
  59. /**
  60. * clear_ftrace_function - reset the ftrace function
  61. *
  62. * This NULLs the ftrace function and in essence stops
  63. * tracing. There may be lag
  64. */
  65. void clear_ftrace_function(void)
  66. {
  67. ftrace_trace_function = ftrace_stub;
  68. }
  69. static int __register_ftrace_function(struct ftrace_ops *ops)
  70. {
  71. /* Should never be called by interrupts */
  72. spin_lock(&ftrace_lock);
  73. ops->next = ftrace_list;
  74. /*
  75. * We are entering ops into the ftrace_list but another
  76. * CPU might be walking that list. We need to make sure
  77. * the ops->next pointer is valid before another CPU sees
  78. * the ops pointer included into the ftrace_list.
  79. */
  80. smp_wmb();
  81. ftrace_list = ops;
  82. if (ftrace_enabled) {
  83. /*
  84. * For one func, simply call it directly.
  85. * For more than one func, call the chain.
  86. */
  87. if (ops->next == &ftrace_list_end)
  88. ftrace_trace_function = ops->func;
  89. else
  90. ftrace_trace_function = ftrace_list_func;
  91. }
  92. spin_unlock(&ftrace_lock);
  93. return 0;
  94. }
  95. static int __unregister_ftrace_function(struct ftrace_ops *ops)
  96. {
  97. struct ftrace_ops **p;
  98. int ret = 0;
  99. spin_lock(&ftrace_lock);
  100. /*
  101. * If we are removing the last function, then simply point
  102. * to the ftrace_stub.
  103. */
  104. if (ftrace_list == ops && ops->next == &ftrace_list_end) {
  105. ftrace_trace_function = ftrace_stub;
  106. ftrace_list = &ftrace_list_end;
  107. goto out;
  108. }
  109. for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
  110. if (*p == ops)
  111. break;
  112. if (*p != ops) {
  113. ret = -1;
  114. goto out;
  115. }
  116. *p = (*p)->next;
  117. if (ftrace_enabled) {
  118. /* If we only have one func left, then call that directly */
  119. if (ftrace_list == &ftrace_list_end ||
  120. ftrace_list->next == &ftrace_list_end)
  121. ftrace_trace_function = ftrace_list->func;
  122. }
  123. out:
  124. spin_unlock(&ftrace_lock);
  125. return ret;
  126. }
  127. #ifdef CONFIG_DYNAMIC_FTRACE
  128. static struct task_struct *ftraced_task;
  129. static DECLARE_WAIT_QUEUE_HEAD(ftraced_waiters);
  130. static unsigned long ftraced_iteration_counter;
  131. enum {
  132. FTRACE_ENABLE_CALLS = (1 << 0),
  133. FTRACE_DISABLE_CALLS = (1 << 1),
  134. FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
  135. FTRACE_ENABLE_MCOUNT = (1 << 3),
  136. FTRACE_DISABLE_MCOUNT = (1 << 4),
  137. };
  138. static int ftrace_filtered;
  139. static struct hlist_head ftrace_hash[FTRACE_HASHSIZE];
  140. static DEFINE_PER_CPU(int, ftrace_shutdown_disable_cpu);
  141. static DEFINE_SPINLOCK(ftrace_shutdown_lock);
  142. static DEFINE_MUTEX(ftraced_lock);
  143. static DEFINE_MUTEX(ftrace_filter_lock);
  144. struct ftrace_page {
  145. struct ftrace_page *next;
  146. int index;
  147. struct dyn_ftrace records[];
  148. } __attribute__((packed));
  149. #define ENTRIES_PER_PAGE \
  150. ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
  151. /* estimate from running different kernels */
  152. #define NR_TO_INIT 10000
  153. static struct ftrace_page *ftrace_pages_start;
  154. static struct ftrace_page *ftrace_pages;
  155. static int ftraced_trigger;
  156. static int ftraced_suspend;
  157. static int ftrace_record_suspend;
  158. static struct dyn_ftrace *ftrace_free_records;
  159. static inline int
  160. ftrace_ip_in_hash(unsigned long ip, unsigned long key)
  161. {
  162. struct dyn_ftrace *p;
  163. struct hlist_node *t;
  164. int found = 0;
  165. hlist_for_each_entry(p, t, &ftrace_hash[key], node) {
  166. if (p->ip == ip) {
  167. found = 1;
  168. break;
  169. }
  170. }
  171. return found;
  172. }
  173. static inline void
  174. ftrace_add_hash(struct dyn_ftrace *node, unsigned long key)
  175. {
  176. hlist_add_head(&node->node, &ftrace_hash[key]);
  177. }
  178. static void ftrace_free_rec(struct dyn_ftrace *rec)
  179. {
  180. /* no locking, only called from kstop_machine */
  181. rec->ip = (unsigned long)ftrace_free_records;
  182. ftrace_free_records = rec;
  183. rec->flags |= FTRACE_FL_FREE;
  184. }
  185. static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
  186. {
  187. struct dyn_ftrace *rec;
  188. /* First check for freed records */
  189. if (ftrace_free_records) {
  190. rec = ftrace_free_records;
  191. if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
  192. WARN_ON_ONCE(1);
  193. ftrace_free_records = NULL;
  194. ftrace_disabled = 1;
  195. ftrace_enabled = 0;
  196. return NULL;
  197. }
  198. ftrace_free_records = (void *)rec->ip;
  199. memset(rec, 0, sizeof(*rec));
  200. return rec;
  201. }
  202. if (ftrace_pages->index == ENTRIES_PER_PAGE) {
  203. if (!ftrace_pages->next)
  204. return NULL;
  205. ftrace_pages = ftrace_pages->next;
  206. }
  207. return &ftrace_pages->records[ftrace_pages->index++];
  208. }
  209. static void
  210. ftrace_record_ip(unsigned long ip)
  211. {
  212. struct dyn_ftrace *node;
  213. unsigned long flags;
  214. unsigned long key;
  215. int resched;
  216. int atomic;
  217. if (!ftrace_enabled || ftrace_disabled)
  218. return;
  219. resched = need_resched();
  220. preempt_disable_notrace();
  221. /* We simply need to protect against recursion */
  222. __get_cpu_var(ftrace_shutdown_disable_cpu)++;
  223. if (__get_cpu_var(ftrace_shutdown_disable_cpu) != 1)
  224. goto out;
  225. if (unlikely(ftrace_record_suspend))
  226. goto out;
  227. key = hash_long(ip, FTRACE_HASHBITS);
  228. WARN_ON_ONCE(key >= FTRACE_HASHSIZE);
  229. if (ftrace_ip_in_hash(ip, key))
  230. goto out;
  231. atomic = irqs_disabled();
  232. spin_lock_irqsave(&ftrace_shutdown_lock, flags);
  233. /* This ip may have hit the hash before the lock */
  234. if (ftrace_ip_in_hash(ip, key))
  235. goto out_unlock;
  236. /*
  237. * There's a slight race that the ftraced will update the
  238. * hash and reset here. If it is already converted, skip it.
  239. */
  240. if (ftrace_ip_converted(ip))
  241. goto out_unlock;
  242. node = ftrace_alloc_dyn_node(ip);
  243. if (!node)
  244. goto out_unlock;
  245. node->ip = ip;
  246. ftrace_add_hash(node, key);
  247. ftraced_trigger = 1;
  248. out_unlock:
  249. spin_unlock_irqrestore(&ftrace_shutdown_lock, flags);
  250. out:
  251. __get_cpu_var(ftrace_shutdown_disable_cpu)--;
  252. /* prevent recursion with scheduler */
  253. if (resched)
  254. preempt_enable_no_resched_notrace();
  255. else
  256. preempt_enable_notrace();
  257. }
  258. #define FTRACE_ADDR ((long)(ftrace_caller))
  259. #define MCOUNT_ADDR ((long)(mcount))
  260. static void
  261. __ftrace_replace_code(struct dyn_ftrace *rec,
  262. unsigned char *old, unsigned char *new, int enable)
  263. {
  264. unsigned long ip;
  265. int failed;
  266. ip = rec->ip;
  267. if (ftrace_filtered && enable) {
  268. unsigned long fl;
  269. /*
  270. * If filtering is on:
  271. *
  272. * If this record is set to be filtered and
  273. * is enabled then do nothing.
  274. *
  275. * If this record is set to be filtered and
  276. * it is not enabled, enable it.
  277. *
  278. * If this record is not set to be filtered
  279. * and it is not enabled do nothing.
  280. *
  281. * If this record is not set to be filtered and
  282. * it is enabled, disable it.
  283. */
  284. fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
  285. if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
  286. (fl == 0))
  287. return;
  288. /*
  289. * If it is enabled disable it,
  290. * otherwise enable it!
  291. */
  292. if (fl == FTRACE_FL_ENABLED) {
  293. /* swap new and old */
  294. new = old;
  295. old = ftrace_call_replace(ip, FTRACE_ADDR);
  296. rec->flags &= ~FTRACE_FL_ENABLED;
  297. } else {
  298. new = ftrace_call_replace(ip, FTRACE_ADDR);
  299. rec->flags |= FTRACE_FL_ENABLED;
  300. }
  301. } else {
  302. if (enable)
  303. new = ftrace_call_replace(ip, FTRACE_ADDR);
  304. else
  305. old = ftrace_call_replace(ip, FTRACE_ADDR);
  306. if (enable) {
  307. if (rec->flags & FTRACE_FL_ENABLED)
  308. return;
  309. rec->flags |= FTRACE_FL_ENABLED;
  310. } else {
  311. if (!(rec->flags & FTRACE_FL_ENABLED))
  312. return;
  313. rec->flags &= ~FTRACE_FL_ENABLED;
  314. }
  315. }
  316. failed = ftrace_modify_code(ip, old, new);
  317. if (failed) {
  318. unsigned long key;
  319. /* It is possible that the function hasn't been converted yet */
  320. key = hash_long(ip, FTRACE_HASHBITS);
  321. if (!ftrace_ip_in_hash(ip, key)) {
  322. rec->flags |= FTRACE_FL_FAILED;
  323. ftrace_free_rec(rec);
  324. }
  325. }
  326. }
  327. static void ftrace_replace_code(int enable)
  328. {
  329. unsigned char *new = NULL, *old = NULL;
  330. struct dyn_ftrace *rec;
  331. struct ftrace_page *pg;
  332. int i;
  333. if (enable)
  334. old = ftrace_nop_replace();
  335. else
  336. new = ftrace_nop_replace();
  337. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  338. for (i = 0; i < pg->index; i++) {
  339. rec = &pg->records[i];
  340. /* don't modify code that has already faulted */
  341. if (rec->flags & FTRACE_FL_FAILED)
  342. continue;
  343. __ftrace_replace_code(rec, old, new, enable);
  344. }
  345. }
  346. }
  347. static void ftrace_shutdown_replenish(void)
  348. {
  349. if (ftrace_pages->next)
  350. return;
  351. /* allocate another page */
  352. ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL);
  353. }
  354. static void
  355. ftrace_code_disable(struct dyn_ftrace *rec)
  356. {
  357. unsigned long ip;
  358. unsigned char *nop, *call;
  359. int failed;
  360. ip = rec->ip;
  361. nop = ftrace_nop_replace();
  362. call = ftrace_call_replace(ip, MCOUNT_ADDR);
  363. failed = ftrace_modify_code(ip, call, nop);
  364. if (failed) {
  365. rec->flags |= FTRACE_FL_FAILED;
  366. ftrace_free_rec(rec);
  367. }
  368. }
  369. static int __ftrace_modify_code(void *data)
  370. {
  371. unsigned long addr;
  372. int *command = data;
  373. if (*command & FTRACE_ENABLE_CALLS)
  374. ftrace_replace_code(1);
  375. else if (*command & FTRACE_DISABLE_CALLS)
  376. ftrace_replace_code(0);
  377. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  378. ftrace_update_ftrace_func(ftrace_trace_function);
  379. if (*command & FTRACE_ENABLE_MCOUNT) {
  380. addr = (unsigned long)ftrace_record_ip;
  381. ftrace_mcount_set(&addr);
  382. } else if (*command & FTRACE_DISABLE_MCOUNT) {
  383. addr = (unsigned long)ftrace_stub;
  384. ftrace_mcount_set(&addr);
  385. }
  386. return 0;
  387. }
  388. static void ftrace_run_update_code(int command)
  389. {
  390. stop_machine_run(__ftrace_modify_code, &command, NR_CPUS);
  391. }
  392. static ftrace_func_t saved_ftrace_func;
  393. static void ftrace_startup(void)
  394. {
  395. int command = 0;
  396. if (unlikely(ftrace_disabled))
  397. return;
  398. mutex_lock(&ftraced_lock);
  399. ftraced_suspend++;
  400. if (ftraced_suspend == 1)
  401. command |= FTRACE_ENABLE_CALLS;
  402. if (saved_ftrace_func != ftrace_trace_function) {
  403. saved_ftrace_func = ftrace_trace_function;
  404. command |= FTRACE_UPDATE_TRACE_FUNC;
  405. }
  406. if (!command || !ftrace_enabled)
  407. goto out;
  408. ftrace_run_update_code(command);
  409. out:
  410. mutex_unlock(&ftraced_lock);
  411. }
  412. static void ftrace_shutdown(void)
  413. {
  414. int command = 0;
  415. if (unlikely(ftrace_disabled))
  416. return;
  417. mutex_lock(&ftraced_lock);
  418. ftraced_suspend--;
  419. if (!ftraced_suspend)
  420. command |= FTRACE_DISABLE_CALLS;
  421. if (saved_ftrace_func != ftrace_trace_function) {
  422. saved_ftrace_func = ftrace_trace_function;
  423. command |= FTRACE_UPDATE_TRACE_FUNC;
  424. }
  425. if (!command || !ftrace_enabled)
  426. goto out;
  427. ftrace_run_update_code(command);
  428. out:
  429. mutex_unlock(&ftraced_lock);
  430. }
  431. static void ftrace_startup_sysctl(void)
  432. {
  433. int command = FTRACE_ENABLE_MCOUNT;
  434. if (unlikely(ftrace_disabled))
  435. return;
  436. mutex_lock(&ftraced_lock);
  437. /* Force update next time */
  438. saved_ftrace_func = NULL;
  439. /* ftraced_suspend is true if we want ftrace running */
  440. if (ftraced_suspend)
  441. command |= FTRACE_ENABLE_CALLS;
  442. ftrace_run_update_code(command);
  443. mutex_unlock(&ftraced_lock);
  444. }
  445. static void ftrace_shutdown_sysctl(void)
  446. {
  447. int command = FTRACE_DISABLE_MCOUNT;
  448. if (unlikely(ftrace_disabled))
  449. return;
  450. mutex_lock(&ftraced_lock);
  451. /* ftraced_suspend is true if ftrace is running */
  452. if (ftraced_suspend)
  453. command |= FTRACE_DISABLE_CALLS;
  454. ftrace_run_update_code(command);
  455. mutex_unlock(&ftraced_lock);
  456. }
  457. static cycle_t ftrace_update_time;
  458. static unsigned long ftrace_update_cnt;
  459. unsigned long ftrace_update_tot_cnt;
  460. static int __ftrace_update_code(void *ignore)
  461. {
  462. struct dyn_ftrace *p;
  463. struct hlist_head head;
  464. struct hlist_node *t;
  465. int save_ftrace_enabled;
  466. cycle_t start, stop;
  467. int i;
  468. /* Don't be recording funcs now */
  469. save_ftrace_enabled = ftrace_enabled;
  470. ftrace_enabled = 0;
  471. start = ftrace_now(raw_smp_processor_id());
  472. ftrace_update_cnt = 0;
  473. /* No locks needed, the machine is stopped! */
  474. for (i = 0; i < FTRACE_HASHSIZE; i++) {
  475. if (hlist_empty(&ftrace_hash[i]))
  476. continue;
  477. head = ftrace_hash[i];
  478. INIT_HLIST_HEAD(&ftrace_hash[i]);
  479. /* all CPUS are stopped, we are safe to modify code */
  480. hlist_for_each_entry(p, t, &head, node) {
  481. ftrace_code_disable(p);
  482. ftrace_update_cnt++;
  483. }
  484. }
  485. stop = ftrace_now(raw_smp_processor_id());
  486. ftrace_update_time = stop - start;
  487. ftrace_update_tot_cnt += ftrace_update_cnt;
  488. ftrace_enabled = save_ftrace_enabled;
  489. return 0;
  490. }
  491. static void ftrace_update_code(void)
  492. {
  493. if (unlikely(ftrace_disabled))
  494. return;
  495. stop_machine_run(__ftrace_update_code, NULL, NR_CPUS);
  496. }
  497. static int ftraced(void *ignore)
  498. {
  499. unsigned long usecs;
  500. while (!kthread_should_stop()) {
  501. set_current_state(TASK_INTERRUPTIBLE);
  502. /* check once a second */
  503. schedule_timeout(HZ);
  504. if (unlikely(ftrace_disabled))
  505. continue;
  506. mutex_lock(&ftrace_sysctl_lock);
  507. mutex_lock(&ftraced_lock);
  508. if (ftrace_enabled && ftraced_trigger && !ftraced_suspend) {
  509. ftrace_record_suspend++;
  510. ftrace_update_code();
  511. usecs = nsecs_to_usecs(ftrace_update_time);
  512. if (ftrace_update_tot_cnt > 100000) {
  513. ftrace_update_tot_cnt = 0;
  514. pr_info("hm, dftrace overflow: %lu change%s"
  515. " (%lu total) in %lu usec%s\n",
  516. ftrace_update_cnt,
  517. ftrace_update_cnt != 1 ? "s" : "",
  518. ftrace_update_tot_cnt,
  519. usecs, usecs != 1 ? "s" : "");
  520. ftrace_disabled = 1;
  521. WARN_ON_ONCE(1);
  522. }
  523. ftraced_trigger = 0;
  524. ftrace_record_suspend--;
  525. }
  526. ftraced_iteration_counter++;
  527. mutex_unlock(&ftraced_lock);
  528. mutex_unlock(&ftrace_sysctl_lock);
  529. wake_up_interruptible(&ftraced_waiters);
  530. ftrace_shutdown_replenish();
  531. }
  532. __set_current_state(TASK_RUNNING);
  533. return 0;
  534. }
  535. static int __init ftrace_dyn_table_alloc(void)
  536. {
  537. struct ftrace_page *pg;
  538. int cnt;
  539. int i;
  540. /* allocate a few pages */
  541. ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
  542. if (!ftrace_pages_start)
  543. return -1;
  544. /*
  545. * Allocate a few more pages.
  546. *
  547. * TODO: have some parser search vmlinux before
  548. * final linking to find all calls to ftrace.
  549. * Then we can:
  550. * a) know how many pages to allocate.
  551. * and/or
  552. * b) set up the table then.
  553. *
  554. * The dynamic code is still necessary for
  555. * modules.
  556. */
  557. pg = ftrace_pages = ftrace_pages_start;
  558. cnt = NR_TO_INIT / ENTRIES_PER_PAGE;
  559. for (i = 0; i < cnt; i++) {
  560. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  561. /* If we fail, we'll try later anyway */
  562. if (!pg->next)
  563. break;
  564. pg = pg->next;
  565. }
  566. return 0;
  567. }
  568. enum {
  569. FTRACE_ITER_FILTER = (1 << 0),
  570. FTRACE_ITER_CONT = (1 << 1),
  571. };
  572. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  573. struct ftrace_iterator {
  574. loff_t pos;
  575. struct ftrace_page *pg;
  576. unsigned idx;
  577. unsigned flags;
  578. unsigned char buffer[FTRACE_BUFF_MAX+1];
  579. unsigned buffer_idx;
  580. unsigned filtered;
  581. };
  582. static void *
  583. t_next(struct seq_file *m, void *v, loff_t *pos)
  584. {
  585. struct ftrace_iterator *iter = m->private;
  586. struct dyn_ftrace *rec = NULL;
  587. (*pos)++;
  588. retry:
  589. if (iter->idx >= iter->pg->index) {
  590. if (iter->pg->next) {
  591. iter->pg = iter->pg->next;
  592. iter->idx = 0;
  593. goto retry;
  594. }
  595. } else {
  596. rec = &iter->pg->records[iter->idx++];
  597. if ((rec->flags & FTRACE_FL_FAILED) ||
  598. ((iter->flags & FTRACE_ITER_FILTER) &&
  599. !(rec->flags & FTRACE_FL_FILTER))) {
  600. rec = NULL;
  601. goto retry;
  602. }
  603. }
  604. iter->pos = *pos;
  605. return rec;
  606. }
  607. static void *t_start(struct seq_file *m, loff_t *pos)
  608. {
  609. struct ftrace_iterator *iter = m->private;
  610. void *p = NULL;
  611. loff_t l = -1;
  612. if (*pos != iter->pos) {
  613. for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
  614. ;
  615. } else {
  616. l = *pos;
  617. p = t_next(m, p, &l);
  618. }
  619. return p;
  620. }
  621. static void t_stop(struct seq_file *m, void *p)
  622. {
  623. }
  624. static int t_show(struct seq_file *m, void *v)
  625. {
  626. struct dyn_ftrace *rec = v;
  627. char str[KSYM_SYMBOL_LEN];
  628. if (!rec)
  629. return 0;
  630. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  631. seq_printf(m, "%s\n", str);
  632. return 0;
  633. }
  634. static struct seq_operations show_ftrace_seq_ops = {
  635. .start = t_start,
  636. .next = t_next,
  637. .stop = t_stop,
  638. .show = t_show,
  639. };
  640. static int
  641. ftrace_avail_open(struct inode *inode, struct file *file)
  642. {
  643. struct ftrace_iterator *iter;
  644. int ret;
  645. if (unlikely(ftrace_disabled))
  646. return -ENODEV;
  647. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  648. if (!iter)
  649. return -ENOMEM;
  650. iter->pg = ftrace_pages_start;
  651. iter->pos = -1;
  652. ret = seq_open(file, &show_ftrace_seq_ops);
  653. if (!ret) {
  654. struct seq_file *m = file->private_data;
  655. m->private = iter;
  656. } else {
  657. kfree(iter);
  658. }
  659. return ret;
  660. }
  661. int ftrace_avail_release(struct inode *inode, struct file *file)
  662. {
  663. struct seq_file *m = (struct seq_file *)file->private_data;
  664. struct ftrace_iterator *iter = m->private;
  665. seq_release(inode, file);
  666. kfree(iter);
  667. return 0;
  668. }
  669. static void ftrace_filter_reset(void)
  670. {
  671. struct ftrace_page *pg;
  672. struct dyn_ftrace *rec;
  673. unsigned i;
  674. /* keep kstop machine from running */
  675. preempt_disable();
  676. ftrace_filtered = 0;
  677. pg = ftrace_pages_start;
  678. while (pg) {
  679. for (i = 0; i < pg->index; i++) {
  680. rec = &pg->records[i];
  681. if (rec->flags & FTRACE_FL_FAILED)
  682. continue;
  683. rec->flags &= ~FTRACE_FL_FILTER;
  684. }
  685. pg = pg->next;
  686. }
  687. preempt_enable();
  688. }
  689. static int
  690. ftrace_filter_open(struct inode *inode, struct file *file)
  691. {
  692. struct ftrace_iterator *iter;
  693. int ret = 0;
  694. if (unlikely(ftrace_disabled))
  695. return -ENODEV;
  696. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  697. if (!iter)
  698. return -ENOMEM;
  699. mutex_lock(&ftrace_filter_lock);
  700. if ((file->f_mode & FMODE_WRITE) &&
  701. !(file->f_flags & O_APPEND))
  702. ftrace_filter_reset();
  703. if (file->f_mode & FMODE_READ) {
  704. iter->pg = ftrace_pages_start;
  705. iter->pos = -1;
  706. iter->flags = FTRACE_ITER_FILTER;
  707. ret = seq_open(file, &show_ftrace_seq_ops);
  708. if (!ret) {
  709. struct seq_file *m = file->private_data;
  710. m->private = iter;
  711. } else
  712. kfree(iter);
  713. } else
  714. file->private_data = iter;
  715. mutex_unlock(&ftrace_filter_lock);
  716. return ret;
  717. }
  718. static ssize_t
  719. ftrace_filter_read(struct file *file, char __user *ubuf,
  720. size_t cnt, loff_t *ppos)
  721. {
  722. if (file->f_mode & FMODE_READ)
  723. return seq_read(file, ubuf, cnt, ppos);
  724. else
  725. return -EPERM;
  726. }
  727. static loff_t
  728. ftrace_filter_lseek(struct file *file, loff_t offset, int origin)
  729. {
  730. loff_t ret;
  731. if (file->f_mode & FMODE_READ)
  732. ret = seq_lseek(file, offset, origin);
  733. else
  734. file->f_pos = ret = 1;
  735. return ret;
  736. }
  737. enum {
  738. MATCH_FULL,
  739. MATCH_FRONT_ONLY,
  740. MATCH_MIDDLE_ONLY,
  741. MATCH_END_ONLY,
  742. };
  743. static void
  744. ftrace_match(unsigned char *buff, int len)
  745. {
  746. char str[KSYM_SYMBOL_LEN];
  747. char *search = NULL;
  748. struct ftrace_page *pg;
  749. struct dyn_ftrace *rec;
  750. int type = MATCH_FULL;
  751. unsigned i, match = 0, search_len = 0;
  752. for (i = 0; i < len; i++) {
  753. if (buff[i] == '*') {
  754. if (!i) {
  755. search = buff + i + 1;
  756. type = MATCH_END_ONLY;
  757. search_len = len - (i + 1);
  758. } else {
  759. if (type == MATCH_END_ONLY) {
  760. type = MATCH_MIDDLE_ONLY;
  761. } else {
  762. match = i;
  763. type = MATCH_FRONT_ONLY;
  764. }
  765. buff[i] = 0;
  766. break;
  767. }
  768. }
  769. }
  770. /* keep kstop machine from running */
  771. preempt_disable();
  772. ftrace_filtered = 1;
  773. pg = ftrace_pages_start;
  774. while (pg) {
  775. for (i = 0; i < pg->index; i++) {
  776. int matched = 0;
  777. char *ptr;
  778. rec = &pg->records[i];
  779. if (rec->flags & FTRACE_FL_FAILED)
  780. continue;
  781. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  782. switch (type) {
  783. case MATCH_FULL:
  784. if (strcmp(str, buff) == 0)
  785. matched = 1;
  786. break;
  787. case MATCH_FRONT_ONLY:
  788. if (memcmp(str, buff, match) == 0)
  789. matched = 1;
  790. break;
  791. case MATCH_MIDDLE_ONLY:
  792. if (strstr(str, search))
  793. matched = 1;
  794. break;
  795. case MATCH_END_ONLY:
  796. ptr = strstr(str, search);
  797. if (ptr && (ptr[search_len] == 0))
  798. matched = 1;
  799. break;
  800. }
  801. if (matched)
  802. rec->flags |= FTRACE_FL_FILTER;
  803. }
  804. pg = pg->next;
  805. }
  806. preempt_enable();
  807. }
  808. static ssize_t
  809. ftrace_filter_write(struct file *file, const char __user *ubuf,
  810. size_t cnt, loff_t *ppos)
  811. {
  812. struct ftrace_iterator *iter;
  813. char ch;
  814. size_t read = 0;
  815. ssize_t ret;
  816. if (!cnt || cnt < 0)
  817. return 0;
  818. mutex_lock(&ftrace_filter_lock);
  819. if (file->f_mode & FMODE_READ) {
  820. struct seq_file *m = file->private_data;
  821. iter = m->private;
  822. } else
  823. iter = file->private_data;
  824. if (!*ppos) {
  825. iter->flags &= ~FTRACE_ITER_CONT;
  826. iter->buffer_idx = 0;
  827. }
  828. ret = get_user(ch, ubuf++);
  829. if (ret)
  830. goto out;
  831. read++;
  832. cnt--;
  833. if (!(iter->flags & ~FTRACE_ITER_CONT)) {
  834. /* skip white space */
  835. while (cnt && isspace(ch)) {
  836. ret = get_user(ch, ubuf++);
  837. if (ret)
  838. goto out;
  839. read++;
  840. cnt--;
  841. }
  842. if (isspace(ch)) {
  843. file->f_pos += read;
  844. ret = read;
  845. goto out;
  846. }
  847. iter->buffer_idx = 0;
  848. }
  849. while (cnt && !isspace(ch)) {
  850. if (iter->buffer_idx < FTRACE_BUFF_MAX)
  851. iter->buffer[iter->buffer_idx++] = ch;
  852. else {
  853. ret = -EINVAL;
  854. goto out;
  855. }
  856. ret = get_user(ch, ubuf++);
  857. if (ret)
  858. goto out;
  859. read++;
  860. cnt--;
  861. }
  862. if (isspace(ch)) {
  863. iter->filtered++;
  864. iter->buffer[iter->buffer_idx] = 0;
  865. ftrace_match(iter->buffer, iter->buffer_idx);
  866. iter->buffer_idx = 0;
  867. } else
  868. iter->flags |= FTRACE_ITER_CONT;
  869. file->f_pos += read;
  870. ret = read;
  871. out:
  872. mutex_unlock(&ftrace_filter_lock);
  873. return ret;
  874. }
  875. /**
  876. * ftrace_set_filter - set a function to filter on in ftrace
  877. * @buf - the string that holds the function filter text.
  878. * @len - the length of the string.
  879. * @reset - non zero to reset all filters before applying this filter.
  880. *
  881. * Filters denote which functions should be enabled when tracing is enabled.
  882. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  883. */
  884. void ftrace_set_filter(unsigned char *buf, int len, int reset)
  885. {
  886. if (unlikely(ftrace_disabled))
  887. return;
  888. mutex_lock(&ftrace_filter_lock);
  889. if (reset)
  890. ftrace_filter_reset();
  891. if (buf)
  892. ftrace_match(buf, len);
  893. mutex_unlock(&ftrace_filter_lock);
  894. }
  895. static int
  896. ftrace_filter_release(struct inode *inode, struct file *file)
  897. {
  898. struct seq_file *m = (struct seq_file *)file->private_data;
  899. struct ftrace_iterator *iter;
  900. mutex_lock(&ftrace_filter_lock);
  901. if (file->f_mode & FMODE_READ) {
  902. iter = m->private;
  903. seq_release(inode, file);
  904. } else
  905. iter = file->private_data;
  906. if (iter->buffer_idx) {
  907. iter->filtered++;
  908. iter->buffer[iter->buffer_idx] = 0;
  909. ftrace_match(iter->buffer, iter->buffer_idx);
  910. }
  911. mutex_lock(&ftrace_sysctl_lock);
  912. mutex_lock(&ftraced_lock);
  913. if (iter->filtered && ftraced_suspend && ftrace_enabled)
  914. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  915. mutex_unlock(&ftraced_lock);
  916. mutex_unlock(&ftrace_sysctl_lock);
  917. kfree(iter);
  918. mutex_unlock(&ftrace_filter_lock);
  919. return 0;
  920. }
  921. static struct file_operations ftrace_avail_fops = {
  922. .open = ftrace_avail_open,
  923. .read = seq_read,
  924. .llseek = seq_lseek,
  925. .release = ftrace_avail_release,
  926. };
  927. static struct file_operations ftrace_filter_fops = {
  928. .open = ftrace_filter_open,
  929. .read = ftrace_filter_read,
  930. .write = ftrace_filter_write,
  931. .llseek = ftrace_filter_lseek,
  932. .release = ftrace_filter_release,
  933. };
  934. /**
  935. * ftrace_force_update - force an update to all recording ftrace functions
  936. *
  937. * The ftrace dynamic update daemon only wakes up once a second.
  938. * There may be cases where an update needs to be done immediately
  939. * for tests or internal kernel tracing to begin. This function
  940. * wakes the daemon to do an update and will not return until the
  941. * update is complete.
  942. */
  943. int ftrace_force_update(void)
  944. {
  945. unsigned long last_counter;
  946. DECLARE_WAITQUEUE(wait, current);
  947. int ret = 0;
  948. if (unlikely(ftrace_disabled))
  949. return -ENODEV;
  950. mutex_lock(&ftraced_lock);
  951. last_counter = ftraced_iteration_counter;
  952. set_current_state(TASK_INTERRUPTIBLE);
  953. add_wait_queue(&ftraced_waiters, &wait);
  954. if (unlikely(!ftraced_task)) {
  955. ret = -ENODEV;
  956. goto out;
  957. }
  958. do {
  959. mutex_unlock(&ftraced_lock);
  960. wake_up_process(ftraced_task);
  961. schedule();
  962. mutex_lock(&ftraced_lock);
  963. if (signal_pending(current)) {
  964. ret = -EINTR;
  965. break;
  966. }
  967. set_current_state(TASK_INTERRUPTIBLE);
  968. } while (last_counter == ftraced_iteration_counter);
  969. out:
  970. mutex_unlock(&ftraced_lock);
  971. remove_wait_queue(&ftraced_waiters, &wait);
  972. set_current_state(TASK_RUNNING);
  973. return ret;
  974. }
  975. static void ftrace_force_shutdown(void)
  976. {
  977. struct task_struct *task;
  978. int command = FTRACE_DISABLE_CALLS | FTRACE_UPDATE_TRACE_FUNC;
  979. mutex_lock(&ftraced_lock);
  980. task = ftraced_task;
  981. ftraced_task = NULL;
  982. ftraced_suspend = -1;
  983. ftrace_run_update_code(command);
  984. mutex_unlock(&ftraced_lock);
  985. if (task)
  986. kthread_stop(task);
  987. }
  988. static __init int ftrace_init_debugfs(void)
  989. {
  990. struct dentry *d_tracer;
  991. struct dentry *entry;
  992. d_tracer = tracing_init_dentry();
  993. entry = debugfs_create_file("available_filter_functions", 0444,
  994. d_tracer, NULL, &ftrace_avail_fops);
  995. if (!entry)
  996. pr_warning("Could not create debugfs "
  997. "'available_filter_functions' entry\n");
  998. entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
  999. NULL, &ftrace_filter_fops);
  1000. if (!entry)
  1001. pr_warning("Could not create debugfs "
  1002. "'set_ftrace_filter' entry\n");
  1003. return 0;
  1004. }
  1005. fs_initcall(ftrace_init_debugfs);
  1006. static int __init ftrace_dynamic_init(void)
  1007. {
  1008. struct task_struct *p;
  1009. unsigned long addr;
  1010. int ret;
  1011. addr = (unsigned long)ftrace_record_ip;
  1012. stop_machine_run(ftrace_dyn_arch_init, &addr, NR_CPUS);
  1013. /* ftrace_dyn_arch_init places the return code in addr */
  1014. if (addr) {
  1015. ret = (int)addr;
  1016. goto failed;
  1017. }
  1018. ret = ftrace_dyn_table_alloc();
  1019. if (ret)
  1020. goto failed;
  1021. p = kthread_run(ftraced, NULL, "ftraced");
  1022. if (IS_ERR(p)) {
  1023. ret = -1;
  1024. goto failed;
  1025. }
  1026. last_ftrace_enabled = ftrace_enabled = 1;
  1027. ftraced_task = p;
  1028. return 0;
  1029. failed:
  1030. ftrace_disabled = 1;
  1031. return ret;
  1032. }
  1033. core_initcall(ftrace_dynamic_init);
  1034. #else
  1035. # define ftrace_startup() do { } while (0)
  1036. # define ftrace_shutdown() do { } while (0)
  1037. # define ftrace_startup_sysctl() do { } while (0)
  1038. # define ftrace_shutdown_sysctl() do { } while (0)
  1039. # define ftrace_force_shutdown() do { } while (0)
  1040. #endif /* CONFIG_DYNAMIC_FTRACE */
  1041. /**
  1042. * ftrace_kill - totally shutdown ftrace
  1043. *
  1044. * This is a safety measure. If something was detected that seems
  1045. * wrong, calling this function will keep ftrace from doing
  1046. * any more modifications, and updates.
  1047. * used when something went wrong.
  1048. */
  1049. void ftrace_kill(void)
  1050. {
  1051. mutex_lock(&ftrace_sysctl_lock);
  1052. ftrace_disabled = 1;
  1053. ftrace_enabled = 0;
  1054. clear_ftrace_function();
  1055. mutex_unlock(&ftrace_sysctl_lock);
  1056. /* Try to totally disable ftrace */
  1057. ftrace_force_shutdown();
  1058. }
  1059. /**
  1060. * register_ftrace_function - register a function for profiling
  1061. * @ops - ops structure that holds the function for profiling.
  1062. *
  1063. * Register a function to be called by all functions in the
  1064. * kernel.
  1065. *
  1066. * Note: @ops->func and all the functions it calls must be labeled
  1067. * with "notrace", otherwise it will go into a
  1068. * recursive loop.
  1069. */
  1070. int register_ftrace_function(struct ftrace_ops *ops)
  1071. {
  1072. int ret;
  1073. if (unlikely(ftrace_disabled))
  1074. return -1;
  1075. mutex_lock(&ftrace_sysctl_lock);
  1076. ret = __register_ftrace_function(ops);
  1077. ftrace_startup();
  1078. mutex_unlock(&ftrace_sysctl_lock);
  1079. return ret;
  1080. }
  1081. /**
  1082. * unregister_ftrace_function - unresgister a function for profiling.
  1083. * @ops - ops structure that holds the function to unregister
  1084. *
  1085. * Unregister a function that was added to be called by ftrace profiling.
  1086. */
  1087. int unregister_ftrace_function(struct ftrace_ops *ops)
  1088. {
  1089. int ret;
  1090. mutex_lock(&ftrace_sysctl_lock);
  1091. ret = __unregister_ftrace_function(ops);
  1092. ftrace_shutdown();
  1093. mutex_unlock(&ftrace_sysctl_lock);
  1094. return ret;
  1095. }
  1096. int
  1097. ftrace_enable_sysctl(struct ctl_table *table, int write,
  1098. struct file *file, void __user *buffer, size_t *lenp,
  1099. loff_t *ppos)
  1100. {
  1101. int ret;
  1102. if (unlikely(ftrace_disabled))
  1103. return -ENODEV;
  1104. mutex_lock(&ftrace_sysctl_lock);
  1105. ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
  1106. if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
  1107. goto out;
  1108. last_ftrace_enabled = ftrace_enabled;
  1109. if (ftrace_enabled) {
  1110. ftrace_startup_sysctl();
  1111. /* we are starting ftrace again */
  1112. if (ftrace_list != &ftrace_list_end) {
  1113. if (ftrace_list->next == &ftrace_list_end)
  1114. ftrace_trace_function = ftrace_list->func;
  1115. else
  1116. ftrace_trace_function = ftrace_list_func;
  1117. }
  1118. } else {
  1119. /* stopping ftrace calls (just send to ftrace_stub) */
  1120. ftrace_trace_function = ftrace_stub;
  1121. ftrace_shutdown_sysctl();
  1122. }
  1123. out:
  1124. mutex_unlock(&ftrace_sysctl_lock);
  1125. return ret;
  1126. }