ftrace.c 40 KB

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