ftrace.c 32 KB

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