ftrace.c 36 KB

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