ftrace.c 31 KB

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