ftrace.c 35 KB

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