ftrace.c 39 KB

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