ftrace.c 36 KB

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