ftrace.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138
  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/suspend.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/hardirq.h>
  22. #include <linux/kthread.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/kprobes.h>
  25. #include <linux/ftrace.h>
  26. #include <linux/sysctl.h>
  27. #include <linux/ctype.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. /* set when tracing only a pid */
  45. struct pid *ftrace_pid_trace;
  46. static struct pid * const ftrace_swapper_pid = &init_struct_pid;
  47. /* Quick disabling of function tracer. */
  48. int function_trace_stop;
  49. /*
  50. * ftrace_disabled is set when an anomaly is discovered.
  51. * ftrace_disabled is much stronger than ftrace_enabled.
  52. */
  53. static int ftrace_disabled __read_mostly;
  54. static DEFINE_SPINLOCK(ftrace_lock);
  55. static DEFINE_MUTEX(ftrace_sysctl_lock);
  56. static DEFINE_MUTEX(ftrace_start_lock);
  57. static struct ftrace_ops ftrace_list_end __read_mostly =
  58. {
  59. .func = ftrace_stub,
  60. };
  61. static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
  62. ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
  63. ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
  64. ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
  65. static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
  66. {
  67. struct ftrace_ops *op = ftrace_list;
  68. /* in case someone actually ports this to alpha! */
  69. read_barrier_depends();
  70. while (op != &ftrace_list_end) {
  71. /* silly alpha */
  72. read_barrier_depends();
  73. op->func(ip, parent_ip);
  74. op = op->next;
  75. };
  76. }
  77. static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
  78. {
  79. if (!test_tsk_trace_trace(current))
  80. return;
  81. ftrace_pid_function(ip, parent_ip);
  82. }
  83. static void set_ftrace_pid_function(ftrace_func_t func)
  84. {
  85. /* do not set ftrace_pid_function to itself! */
  86. if (func != ftrace_pid_func)
  87. ftrace_pid_function = func;
  88. }
  89. /**
  90. * clear_ftrace_function - reset the ftrace function
  91. *
  92. * This NULLs the ftrace function and in essence stops
  93. * tracing. There may be lag
  94. */
  95. void clear_ftrace_function(void)
  96. {
  97. ftrace_trace_function = ftrace_stub;
  98. __ftrace_trace_function = ftrace_stub;
  99. ftrace_pid_function = ftrace_stub;
  100. }
  101. #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  102. /*
  103. * For those archs that do not test ftrace_trace_stop in their
  104. * mcount call site, we need to do it from C.
  105. */
  106. static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
  107. {
  108. if (function_trace_stop)
  109. return;
  110. __ftrace_trace_function(ip, parent_ip);
  111. }
  112. #endif
  113. static int __register_ftrace_function(struct ftrace_ops *ops)
  114. {
  115. /* should not be called from interrupt context */
  116. spin_lock(&ftrace_lock);
  117. ops->next = ftrace_list;
  118. /*
  119. * We are entering ops into the ftrace_list but another
  120. * CPU might be walking that list. We need to make sure
  121. * the ops->next pointer is valid before another CPU sees
  122. * the ops pointer included into the ftrace_list.
  123. */
  124. smp_wmb();
  125. ftrace_list = ops;
  126. if (ftrace_enabled) {
  127. ftrace_func_t func;
  128. if (ops->next == &ftrace_list_end)
  129. func = ops->func;
  130. else
  131. func = ftrace_list_func;
  132. if (ftrace_pid_trace) {
  133. set_ftrace_pid_function(func);
  134. func = ftrace_pid_func;
  135. }
  136. /*
  137. * For one func, simply call it directly.
  138. * For more than one func, call the chain.
  139. */
  140. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  141. ftrace_trace_function = func;
  142. #else
  143. __ftrace_trace_function = func;
  144. ftrace_trace_function = ftrace_test_stop_func;
  145. #endif
  146. }
  147. spin_unlock(&ftrace_lock);
  148. return 0;
  149. }
  150. static int __unregister_ftrace_function(struct ftrace_ops *ops)
  151. {
  152. struct ftrace_ops **p;
  153. int ret = 0;
  154. /* should not be called from interrupt context */
  155. spin_lock(&ftrace_lock);
  156. /*
  157. * If we are removing the last function, then simply point
  158. * to the ftrace_stub.
  159. */
  160. if (ftrace_list == ops && ops->next == &ftrace_list_end) {
  161. ftrace_trace_function = ftrace_stub;
  162. ftrace_list = &ftrace_list_end;
  163. goto out;
  164. }
  165. for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
  166. if (*p == ops)
  167. break;
  168. if (*p != ops) {
  169. ret = -1;
  170. goto out;
  171. }
  172. *p = (*p)->next;
  173. if (ftrace_enabled) {
  174. /* If we only have one func left, then call that directly */
  175. if (ftrace_list->next == &ftrace_list_end) {
  176. ftrace_func_t func = ftrace_list->func;
  177. if (ftrace_pid_trace) {
  178. set_ftrace_pid_function(func);
  179. func = ftrace_pid_func;
  180. }
  181. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  182. ftrace_trace_function = func;
  183. #else
  184. __ftrace_trace_function = func;
  185. #endif
  186. }
  187. }
  188. out:
  189. spin_unlock(&ftrace_lock);
  190. return ret;
  191. }
  192. static void ftrace_update_pid_func(void)
  193. {
  194. ftrace_func_t func;
  195. /* should not be called from interrupt context */
  196. spin_lock(&ftrace_lock);
  197. if (ftrace_trace_function == ftrace_stub)
  198. goto out;
  199. func = ftrace_trace_function;
  200. if (ftrace_pid_trace) {
  201. set_ftrace_pid_function(func);
  202. func = ftrace_pid_func;
  203. } else {
  204. if (func == ftrace_pid_func)
  205. func = ftrace_pid_function;
  206. }
  207. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  208. ftrace_trace_function = func;
  209. #else
  210. __ftrace_trace_function = func;
  211. #endif
  212. out:
  213. spin_unlock(&ftrace_lock);
  214. }
  215. #ifdef CONFIG_DYNAMIC_FTRACE
  216. #ifndef CONFIG_FTRACE_MCOUNT_RECORD
  217. # error Dynamic ftrace depends on MCOUNT_RECORD
  218. #endif
  219. enum {
  220. FTRACE_ENABLE_CALLS = (1 << 0),
  221. FTRACE_DISABLE_CALLS = (1 << 1),
  222. FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
  223. FTRACE_ENABLE_MCOUNT = (1 << 3),
  224. FTRACE_DISABLE_MCOUNT = (1 << 4),
  225. FTRACE_START_FUNC_RET = (1 << 5),
  226. FTRACE_STOP_FUNC_RET = (1 << 6),
  227. };
  228. static int ftrace_filtered;
  229. static LIST_HEAD(ftrace_new_addrs);
  230. static DEFINE_MUTEX(ftrace_regex_lock);
  231. struct ftrace_page {
  232. struct ftrace_page *next;
  233. int index;
  234. struct dyn_ftrace records[];
  235. };
  236. #define ENTRIES_PER_PAGE \
  237. ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
  238. /* estimate from running different kernels */
  239. #define NR_TO_INIT 10000
  240. static struct ftrace_page *ftrace_pages_start;
  241. static struct ftrace_page *ftrace_pages;
  242. static struct dyn_ftrace *ftrace_free_records;
  243. #ifdef CONFIG_KPROBES
  244. static int frozen_record_count;
  245. static inline void freeze_record(struct dyn_ftrace *rec)
  246. {
  247. if (!(rec->flags & FTRACE_FL_FROZEN)) {
  248. rec->flags |= FTRACE_FL_FROZEN;
  249. frozen_record_count++;
  250. }
  251. }
  252. static inline void unfreeze_record(struct dyn_ftrace *rec)
  253. {
  254. if (rec->flags & FTRACE_FL_FROZEN) {
  255. rec->flags &= ~FTRACE_FL_FROZEN;
  256. frozen_record_count--;
  257. }
  258. }
  259. static inline int record_frozen(struct dyn_ftrace *rec)
  260. {
  261. return rec->flags & FTRACE_FL_FROZEN;
  262. }
  263. #else
  264. # define freeze_record(rec) ({ 0; })
  265. # define unfreeze_record(rec) ({ 0; })
  266. # define record_frozen(rec) ({ 0; })
  267. #endif /* CONFIG_KPROBES */
  268. static void ftrace_free_rec(struct dyn_ftrace *rec)
  269. {
  270. rec->ip = (unsigned long)ftrace_free_records;
  271. ftrace_free_records = rec;
  272. rec->flags |= FTRACE_FL_FREE;
  273. }
  274. void ftrace_release(void *start, unsigned long size)
  275. {
  276. struct dyn_ftrace *rec;
  277. struct ftrace_page *pg;
  278. unsigned long s = (unsigned long)start;
  279. unsigned long e = s + size;
  280. int i;
  281. if (ftrace_disabled || !start)
  282. return;
  283. /* should not be called from interrupt context */
  284. spin_lock(&ftrace_lock);
  285. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  286. for (i = 0; i < pg->index; i++) {
  287. rec = &pg->records[i];
  288. if ((rec->ip >= s) && (rec->ip < e))
  289. ftrace_free_rec(rec);
  290. }
  291. }
  292. spin_unlock(&ftrace_lock);
  293. }
  294. static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
  295. {
  296. struct dyn_ftrace *rec;
  297. /* First check for freed records */
  298. if (ftrace_free_records) {
  299. rec = ftrace_free_records;
  300. if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
  301. FTRACE_WARN_ON_ONCE(1);
  302. ftrace_free_records = NULL;
  303. return NULL;
  304. }
  305. ftrace_free_records = (void *)rec->ip;
  306. memset(rec, 0, sizeof(*rec));
  307. return rec;
  308. }
  309. if (ftrace_pages->index == ENTRIES_PER_PAGE) {
  310. if (!ftrace_pages->next) {
  311. /* allocate another page */
  312. ftrace_pages->next =
  313. (void *)get_zeroed_page(GFP_KERNEL);
  314. if (!ftrace_pages->next)
  315. return NULL;
  316. }
  317. ftrace_pages = ftrace_pages->next;
  318. }
  319. return &ftrace_pages->records[ftrace_pages->index++];
  320. }
  321. static struct dyn_ftrace *
  322. ftrace_record_ip(unsigned long ip)
  323. {
  324. struct dyn_ftrace *rec;
  325. if (ftrace_disabled)
  326. return NULL;
  327. rec = ftrace_alloc_dyn_node(ip);
  328. if (!rec)
  329. return NULL;
  330. rec->ip = ip;
  331. list_add(&rec->list, &ftrace_new_addrs);
  332. return rec;
  333. }
  334. static void print_ip_ins(const char *fmt, unsigned char *p)
  335. {
  336. int i;
  337. printk(KERN_CONT "%s", fmt);
  338. for (i = 0; i < MCOUNT_INSN_SIZE; i++)
  339. printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
  340. }
  341. static void ftrace_bug(int failed, unsigned long ip)
  342. {
  343. switch (failed) {
  344. case -EFAULT:
  345. FTRACE_WARN_ON_ONCE(1);
  346. pr_info("ftrace faulted on modifying ");
  347. print_ip_sym(ip);
  348. break;
  349. case -EINVAL:
  350. FTRACE_WARN_ON_ONCE(1);
  351. pr_info("ftrace failed to modify ");
  352. print_ip_sym(ip);
  353. print_ip_ins(" actual: ", (unsigned char *)ip);
  354. printk(KERN_CONT "\n");
  355. break;
  356. case -EPERM:
  357. FTRACE_WARN_ON_ONCE(1);
  358. pr_info("ftrace faulted on writing ");
  359. print_ip_sym(ip);
  360. break;
  361. default:
  362. FTRACE_WARN_ON_ONCE(1);
  363. pr_info("ftrace faulted on unknown error ");
  364. print_ip_sym(ip);
  365. }
  366. }
  367. static int
  368. __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
  369. {
  370. unsigned long ip, fl;
  371. unsigned long ftrace_addr;
  372. ftrace_addr = (unsigned long)FTRACE_ADDR;
  373. ip = rec->ip;
  374. /*
  375. * If this record is not to be traced and
  376. * it is not enabled then do nothing.
  377. *
  378. * If this record is not to be traced and
  379. * it is enabled then disable it.
  380. *
  381. */
  382. if (rec->flags & FTRACE_FL_NOTRACE) {
  383. if (rec->flags & FTRACE_FL_ENABLED)
  384. rec->flags &= ~FTRACE_FL_ENABLED;
  385. else
  386. return 0;
  387. } else if (ftrace_filtered && enable) {
  388. /*
  389. * Filtering is on:
  390. */
  391. fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
  392. /* Record is filtered and enabled, do nothing */
  393. if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
  394. return 0;
  395. /* Record is not filtered or enabled, do nothing */
  396. if (!fl)
  397. return 0;
  398. /* Record is not filtered but enabled, disable it */
  399. if (fl == FTRACE_FL_ENABLED)
  400. rec->flags &= ~FTRACE_FL_ENABLED;
  401. else
  402. /* Otherwise record is filtered but not enabled, enable it */
  403. rec->flags |= FTRACE_FL_ENABLED;
  404. } else {
  405. /* Disable or not filtered */
  406. if (enable) {
  407. /* if record is enabled, do nothing */
  408. if (rec->flags & FTRACE_FL_ENABLED)
  409. return 0;
  410. rec->flags |= FTRACE_FL_ENABLED;
  411. } else {
  412. /* if record is not enabled, do nothing */
  413. if (!(rec->flags & FTRACE_FL_ENABLED))
  414. return 0;
  415. rec->flags &= ~FTRACE_FL_ENABLED;
  416. }
  417. }
  418. if (rec->flags & FTRACE_FL_ENABLED)
  419. return ftrace_make_call(rec, ftrace_addr);
  420. else
  421. return ftrace_make_nop(NULL, rec, ftrace_addr);
  422. }
  423. static void ftrace_replace_code(int enable)
  424. {
  425. int i, failed;
  426. struct dyn_ftrace *rec;
  427. struct ftrace_page *pg;
  428. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  429. for (i = 0; i < pg->index; i++) {
  430. rec = &pg->records[i];
  431. /*
  432. * Skip over free records and records that have
  433. * failed.
  434. */
  435. if (rec->flags & FTRACE_FL_FREE ||
  436. rec->flags & FTRACE_FL_FAILED)
  437. continue;
  438. /* ignore updates to this record's mcount site */
  439. if (get_kprobe((void *)rec->ip)) {
  440. freeze_record(rec);
  441. continue;
  442. } else {
  443. unfreeze_record(rec);
  444. }
  445. failed = __ftrace_replace_code(rec, enable);
  446. if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
  447. rec->flags |= FTRACE_FL_FAILED;
  448. if ((system_state == SYSTEM_BOOTING) ||
  449. !core_kernel_text(rec->ip)) {
  450. ftrace_free_rec(rec);
  451. } else
  452. ftrace_bug(failed, rec->ip);
  453. }
  454. }
  455. }
  456. }
  457. static int
  458. ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
  459. {
  460. unsigned long ip;
  461. int ret;
  462. ip = rec->ip;
  463. ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
  464. if (ret) {
  465. ftrace_bug(ret, ip);
  466. rec->flags |= FTRACE_FL_FAILED;
  467. return 0;
  468. }
  469. return 1;
  470. }
  471. static int __ftrace_modify_code(void *data)
  472. {
  473. int *command = data;
  474. if (*command & FTRACE_ENABLE_CALLS)
  475. ftrace_replace_code(1);
  476. else if (*command & FTRACE_DISABLE_CALLS)
  477. ftrace_replace_code(0);
  478. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  479. ftrace_update_ftrace_func(ftrace_trace_function);
  480. if (*command & FTRACE_START_FUNC_RET)
  481. ftrace_enable_ftrace_graph_caller();
  482. else if (*command & FTRACE_STOP_FUNC_RET)
  483. ftrace_disable_ftrace_graph_caller();
  484. return 0;
  485. }
  486. static void ftrace_run_update_code(int command)
  487. {
  488. stop_machine(__ftrace_modify_code, &command, NULL);
  489. }
  490. static ftrace_func_t saved_ftrace_func;
  491. static int ftrace_start_up;
  492. static void ftrace_startup_enable(int command)
  493. {
  494. if (saved_ftrace_func != ftrace_trace_function) {
  495. saved_ftrace_func = ftrace_trace_function;
  496. command |= FTRACE_UPDATE_TRACE_FUNC;
  497. }
  498. if (!command || !ftrace_enabled)
  499. return;
  500. ftrace_run_update_code(command);
  501. }
  502. static void ftrace_startup(int command)
  503. {
  504. if (unlikely(ftrace_disabled))
  505. return;
  506. mutex_lock(&ftrace_start_lock);
  507. ftrace_start_up++;
  508. command |= FTRACE_ENABLE_CALLS;
  509. ftrace_startup_enable(command);
  510. mutex_unlock(&ftrace_start_lock);
  511. }
  512. static void ftrace_shutdown(int command)
  513. {
  514. if (unlikely(ftrace_disabled))
  515. return;
  516. mutex_lock(&ftrace_start_lock);
  517. ftrace_start_up--;
  518. if (!ftrace_start_up)
  519. command |= FTRACE_DISABLE_CALLS;
  520. if (saved_ftrace_func != ftrace_trace_function) {
  521. saved_ftrace_func = ftrace_trace_function;
  522. command |= FTRACE_UPDATE_TRACE_FUNC;
  523. }
  524. if (!command || !ftrace_enabled)
  525. goto out;
  526. ftrace_run_update_code(command);
  527. out:
  528. mutex_unlock(&ftrace_start_lock);
  529. }
  530. static void ftrace_startup_sysctl(void)
  531. {
  532. int command = FTRACE_ENABLE_MCOUNT;
  533. if (unlikely(ftrace_disabled))
  534. return;
  535. mutex_lock(&ftrace_start_lock);
  536. /* Force update next time */
  537. saved_ftrace_func = NULL;
  538. /* ftrace_start_up is true if we want ftrace running */
  539. if (ftrace_start_up)
  540. command |= FTRACE_ENABLE_CALLS;
  541. ftrace_run_update_code(command);
  542. mutex_unlock(&ftrace_start_lock);
  543. }
  544. static void ftrace_shutdown_sysctl(void)
  545. {
  546. int command = FTRACE_DISABLE_MCOUNT;
  547. if (unlikely(ftrace_disabled))
  548. return;
  549. mutex_lock(&ftrace_start_lock);
  550. /* ftrace_start_up is true if ftrace is running */
  551. if (ftrace_start_up)
  552. command |= FTRACE_DISABLE_CALLS;
  553. ftrace_run_update_code(command);
  554. mutex_unlock(&ftrace_start_lock);
  555. }
  556. static cycle_t ftrace_update_time;
  557. static unsigned long ftrace_update_cnt;
  558. unsigned long ftrace_update_tot_cnt;
  559. static int ftrace_update_code(struct module *mod)
  560. {
  561. struct dyn_ftrace *p, *t;
  562. cycle_t start, stop;
  563. start = ftrace_now(raw_smp_processor_id());
  564. ftrace_update_cnt = 0;
  565. list_for_each_entry_safe(p, t, &ftrace_new_addrs, list) {
  566. /* If something went wrong, bail without enabling anything */
  567. if (unlikely(ftrace_disabled))
  568. return -1;
  569. list_del_init(&p->list);
  570. /* convert record (i.e, patch mcount-call with NOP) */
  571. if (ftrace_code_disable(mod, p)) {
  572. p->flags |= FTRACE_FL_CONVERTED;
  573. ftrace_update_cnt++;
  574. } else
  575. ftrace_free_rec(p);
  576. }
  577. stop = ftrace_now(raw_smp_processor_id());
  578. ftrace_update_time = stop - start;
  579. ftrace_update_tot_cnt += ftrace_update_cnt;
  580. return 0;
  581. }
  582. static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
  583. {
  584. struct ftrace_page *pg;
  585. int cnt;
  586. int i;
  587. /* allocate a few pages */
  588. ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
  589. if (!ftrace_pages_start)
  590. return -1;
  591. /*
  592. * Allocate a few more pages.
  593. *
  594. * TODO: have some parser search vmlinux before
  595. * final linking to find all calls to ftrace.
  596. * Then we can:
  597. * a) know how many pages to allocate.
  598. * and/or
  599. * b) set up the table then.
  600. *
  601. * The dynamic code is still necessary for
  602. * modules.
  603. */
  604. pg = ftrace_pages = ftrace_pages_start;
  605. cnt = num_to_init / ENTRIES_PER_PAGE;
  606. pr_info("ftrace: allocating %ld entries in %d pages\n",
  607. num_to_init, cnt + 1);
  608. for (i = 0; i < cnt; i++) {
  609. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  610. /* If we fail, we'll try later anyway */
  611. if (!pg->next)
  612. break;
  613. pg = pg->next;
  614. }
  615. return 0;
  616. }
  617. enum {
  618. FTRACE_ITER_FILTER = (1 << 0),
  619. FTRACE_ITER_CONT = (1 << 1),
  620. FTRACE_ITER_NOTRACE = (1 << 2),
  621. FTRACE_ITER_FAILURES = (1 << 3),
  622. };
  623. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  624. struct ftrace_iterator {
  625. struct ftrace_page *pg;
  626. int idx;
  627. unsigned flags;
  628. unsigned char buffer[FTRACE_BUFF_MAX+1];
  629. unsigned buffer_idx;
  630. unsigned filtered;
  631. };
  632. static void *
  633. t_next(struct seq_file *m, void *v, loff_t *pos)
  634. {
  635. struct ftrace_iterator *iter = m->private;
  636. struct dyn_ftrace *rec = NULL;
  637. (*pos)++;
  638. /* should not be called from interrupt context */
  639. spin_lock(&ftrace_lock);
  640. retry:
  641. if (iter->idx >= iter->pg->index) {
  642. if (iter->pg->next) {
  643. iter->pg = iter->pg->next;
  644. iter->idx = 0;
  645. goto retry;
  646. } else {
  647. iter->idx = -1;
  648. }
  649. } else {
  650. rec = &iter->pg->records[iter->idx++];
  651. if ((rec->flags & FTRACE_FL_FREE) ||
  652. (!(iter->flags & FTRACE_ITER_FAILURES) &&
  653. (rec->flags & FTRACE_FL_FAILED)) ||
  654. ((iter->flags & FTRACE_ITER_FAILURES) &&
  655. !(rec->flags & FTRACE_FL_FAILED)) ||
  656. ((iter->flags & FTRACE_ITER_FILTER) &&
  657. !(rec->flags & FTRACE_FL_FILTER)) ||
  658. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  659. !(rec->flags & FTRACE_FL_NOTRACE))) {
  660. rec = NULL;
  661. goto retry;
  662. }
  663. }
  664. spin_unlock(&ftrace_lock);
  665. return rec;
  666. }
  667. static void *t_start(struct seq_file *m, loff_t *pos)
  668. {
  669. struct ftrace_iterator *iter = m->private;
  670. void *p = NULL;
  671. if (*pos > 0) {
  672. if (iter->idx < 0)
  673. return p;
  674. (*pos)--;
  675. iter->idx--;
  676. }
  677. p = t_next(m, p, pos);
  678. return p;
  679. }
  680. static void t_stop(struct seq_file *m, void *p)
  681. {
  682. }
  683. static int t_show(struct seq_file *m, void *v)
  684. {
  685. struct dyn_ftrace *rec = v;
  686. char str[KSYM_SYMBOL_LEN];
  687. if (!rec)
  688. return 0;
  689. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  690. seq_printf(m, "%s\n", str);
  691. return 0;
  692. }
  693. static struct seq_operations show_ftrace_seq_ops = {
  694. .start = t_start,
  695. .next = t_next,
  696. .stop = t_stop,
  697. .show = t_show,
  698. };
  699. static int
  700. ftrace_avail_open(struct inode *inode, struct file *file)
  701. {
  702. struct ftrace_iterator *iter;
  703. int ret;
  704. if (unlikely(ftrace_disabled))
  705. return -ENODEV;
  706. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  707. if (!iter)
  708. return -ENOMEM;
  709. iter->pg = ftrace_pages_start;
  710. ret = seq_open(file, &show_ftrace_seq_ops);
  711. if (!ret) {
  712. struct seq_file *m = file->private_data;
  713. m->private = iter;
  714. } else {
  715. kfree(iter);
  716. }
  717. return ret;
  718. }
  719. int ftrace_avail_release(struct inode *inode, struct file *file)
  720. {
  721. struct seq_file *m = (struct seq_file *)file->private_data;
  722. struct ftrace_iterator *iter = m->private;
  723. seq_release(inode, file);
  724. kfree(iter);
  725. return 0;
  726. }
  727. static int
  728. ftrace_failures_open(struct inode *inode, struct file *file)
  729. {
  730. int ret;
  731. struct seq_file *m;
  732. struct ftrace_iterator *iter;
  733. ret = ftrace_avail_open(inode, file);
  734. if (!ret) {
  735. m = (struct seq_file *)file->private_data;
  736. iter = (struct ftrace_iterator *)m->private;
  737. iter->flags = FTRACE_ITER_FAILURES;
  738. }
  739. return ret;
  740. }
  741. static void ftrace_filter_reset(int enable)
  742. {
  743. struct ftrace_page *pg;
  744. struct dyn_ftrace *rec;
  745. unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  746. unsigned i;
  747. /* should not be called from interrupt context */
  748. spin_lock(&ftrace_lock);
  749. if (enable)
  750. ftrace_filtered = 0;
  751. pg = ftrace_pages_start;
  752. while (pg) {
  753. for (i = 0; i < pg->index; i++) {
  754. rec = &pg->records[i];
  755. if (rec->flags & FTRACE_FL_FAILED)
  756. continue;
  757. rec->flags &= ~type;
  758. }
  759. pg = pg->next;
  760. }
  761. spin_unlock(&ftrace_lock);
  762. }
  763. static int
  764. ftrace_regex_open(struct inode *inode, struct file *file, int enable)
  765. {
  766. struct ftrace_iterator *iter;
  767. int ret = 0;
  768. if (unlikely(ftrace_disabled))
  769. return -ENODEV;
  770. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  771. if (!iter)
  772. return -ENOMEM;
  773. mutex_lock(&ftrace_regex_lock);
  774. if ((file->f_mode & FMODE_WRITE) &&
  775. !(file->f_flags & O_APPEND))
  776. ftrace_filter_reset(enable);
  777. if (file->f_mode & FMODE_READ) {
  778. iter->pg = ftrace_pages_start;
  779. iter->flags = enable ? FTRACE_ITER_FILTER :
  780. FTRACE_ITER_NOTRACE;
  781. ret = seq_open(file, &show_ftrace_seq_ops);
  782. if (!ret) {
  783. struct seq_file *m = file->private_data;
  784. m->private = iter;
  785. } else
  786. kfree(iter);
  787. } else
  788. file->private_data = iter;
  789. mutex_unlock(&ftrace_regex_lock);
  790. return ret;
  791. }
  792. static int
  793. ftrace_filter_open(struct inode *inode, struct file *file)
  794. {
  795. return ftrace_regex_open(inode, file, 1);
  796. }
  797. static int
  798. ftrace_notrace_open(struct inode *inode, struct file *file)
  799. {
  800. return ftrace_regex_open(inode, file, 0);
  801. }
  802. static ssize_t
  803. ftrace_regex_read(struct file *file, char __user *ubuf,
  804. size_t cnt, loff_t *ppos)
  805. {
  806. if (file->f_mode & FMODE_READ)
  807. return seq_read(file, ubuf, cnt, ppos);
  808. else
  809. return -EPERM;
  810. }
  811. static loff_t
  812. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  813. {
  814. loff_t ret;
  815. if (file->f_mode & FMODE_READ)
  816. ret = seq_lseek(file, offset, origin);
  817. else
  818. file->f_pos = ret = 1;
  819. return ret;
  820. }
  821. enum {
  822. MATCH_FULL,
  823. MATCH_FRONT_ONLY,
  824. MATCH_MIDDLE_ONLY,
  825. MATCH_END_ONLY,
  826. };
  827. static void
  828. ftrace_match(unsigned char *buff, int len, int enable)
  829. {
  830. char str[KSYM_SYMBOL_LEN];
  831. char *search = NULL;
  832. struct ftrace_page *pg;
  833. struct dyn_ftrace *rec;
  834. int type = MATCH_FULL;
  835. unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  836. unsigned i, match = 0, search_len = 0;
  837. int not = 0;
  838. if (buff[0] == '!') {
  839. not = 1;
  840. buff++;
  841. len--;
  842. }
  843. for (i = 0; i < len; i++) {
  844. if (buff[i] == '*') {
  845. if (!i) {
  846. search = buff + i + 1;
  847. type = MATCH_END_ONLY;
  848. search_len = len - (i + 1);
  849. } else {
  850. if (type == MATCH_END_ONLY) {
  851. type = MATCH_MIDDLE_ONLY;
  852. } else {
  853. match = i;
  854. type = MATCH_FRONT_ONLY;
  855. }
  856. buff[i] = 0;
  857. break;
  858. }
  859. }
  860. }
  861. /* should not be called from interrupt context */
  862. spin_lock(&ftrace_lock);
  863. if (enable)
  864. ftrace_filtered = 1;
  865. pg = ftrace_pages_start;
  866. while (pg) {
  867. for (i = 0; i < pg->index; i++) {
  868. int matched = 0;
  869. char *ptr;
  870. rec = &pg->records[i];
  871. if (rec->flags & FTRACE_FL_FAILED)
  872. continue;
  873. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  874. switch (type) {
  875. case MATCH_FULL:
  876. if (strcmp(str, buff) == 0)
  877. matched = 1;
  878. break;
  879. case MATCH_FRONT_ONLY:
  880. if (memcmp(str, buff, match) == 0)
  881. matched = 1;
  882. break;
  883. case MATCH_MIDDLE_ONLY:
  884. if (strstr(str, search))
  885. matched = 1;
  886. break;
  887. case MATCH_END_ONLY:
  888. ptr = strstr(str, search);
  889. if (ptr && (ptr[search_len] == 0))
  890. matched = 1;
  891. break;
  892. }
  893. if (matched) {
  894. if (not)
  895. rec->flags &= ~flag;
  896. else
  897. rec->flags |= flag;
  898. }
  899. }
  900. pg = pg->next;
  901. }
  902. spin_unlock(&ftrace_lock);
  903. }
  904. static ssize_t
  905. ftrace_regex_write(struct file *file, const char __user *ubuf,
  906. size_t cnt, loff_t *ppos, int enable)
  907. {
  908. struct ftrace_iterator *iter;
  909. char ch;
  910. size_t read = 0;
  911. ssize_t ret;
  912. if (!cnt || cnt < 0)
  913. return 0;
  914. mutex_lock(&ftrace_regex_lock);
  915. if (file->f_mode & FMODE_READ) {
  916. struct seq_file *m = file->private_data;
  917. iter = m->private;
  918. } else
  919. iter = file->private_data;
  920. if (!*ppos) {
  921. iter->flags &= ~FTRACE_ITER_CONT;
  922. iter->buffer_idx = 0;
  923. }
  924. ret = get_user(ch, ubuf++);
  925. if (ret)
  926. goto out;
  927. read++;
  928. cnt--;
  929. if (!(iter->flags & ~FTRACE_ITER_CONT)) {
  930. /* skip white space */
  931. while (cnt && isspace(ch)) {
  932. ret = get_user(ch, ubuf++);
  933. if (ret)
  934. goto out;
  935. read++;
  936. cnt--;
  937. }
  938. if (isspace(ch)) {
  939. file->f_pos += read;
  940. ret = read;
  941. goto out;
  942. }
  943. iter->buffer_idx = 0;
  944. }
  945. while (cnt && !isspace(ch)) {
  946. if (iter->buffer_idx < FTRACE_BUFF_MAX)
  947. iter->buffer[iter->buffer_idx++] = ch;
  948. else {
  949. ret = -EINVAL;
  950. goto out;
  951. }
  952. ret = get_user(ch, ubuf++);
  953. if (ret)
  954. goto out;
  955. read++;
  956. cnt--;
  957. }
  958. if (isspace(ch)) {
  959. iter->filtered++;
  960. iter->buffer[iter->buffer_idx] = 0;
  961. ftrace_match(iter->buffer, iter->buffer_idx, enable);
  962. iter->buffer_idx = 0;
  963. } else
  964. iter->flags |= FTRACE_ITER_CONT;
  965. file->f_pos += read;
  966. ret = read;
  967. out:
  968. mutex_unlock(&ftrace_regex_lock);
  969. return ret;
  970. }
  971. static ssize_t
  972. ftrace_filter_write(struct file *file, const char __user *ubuf,
  973. size_t cnt, loff_t *ppos)
  974. {
  975. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  976. }
  977. static ssize_t
  978. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  979. size_t cnt, loff_t *ppos)
  980. {
  981. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  982. }
  983. static void
  984. ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
  985. {
  986. if (unlikely(ftrace_disabled))
  987. return;
  988. mutex_lock(&ftrace_regex_lock);
  989. if (reset)
  990. ftrace_filter_reset(enable);
  991. if (buf)
  992. ftrace_match(buf, len, enable);
  993. mutex_unlock(&ftrace_regex_lock);
  994. }
  995. /**
  996. * ftrace_set_filter - set a function to filter on in ftrace
  997. * @buf - the string that holds the function filter text.
  998. * @len - the length of the string.
  999. * @reset - non zero to reset all filters before applying this filter.
  1000. *
  1001. * Filters denote which functions should be enabled when tracing is enabled.
  1002. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  1003. */
  1004. void ftrace_set_filter(unsigned char *buf, int len, int reset)
  1005. {
  1006. ftrace_set_regex(buf, len, reset, 1);
  1007. }
  1008. /**
  1009. * ftrace_set_notrace - set a function to not trace in ftrace
  1010. * @buf - the string that holds the function notrace text.
  1011. * @len - the length of the string.
  1012. * @reset - non zero to reset all filters before applying this filter.
  1013. *
  1014. * Notrace Filters denote which functions should not be enabled when tracing
  1015. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  1016. * for tracing.
  1017. */
  1018. void ftrace_set_notrace(unsigned char *buf, int len, int reset)
  1019. {
  1020. ftrace_set_regex(buf, len, reset, 0);
  1021. }
  1022. static int
  1023. ftrace_regex_release(struct inode *inode, struct file *file, int enable)
  1024. {
  1025. struct seq_file *m = (struct seq_file *)file->private_data;
  1026. struct ftrace_iterator *iter;
  1027. mutex_lock(&ftrace_regex_lock);
  1028. if (file->f_mode & FMODE_READ) {
  1029. iter = m->private;
  1030. seq_release(inode, file);
  1031. } else
  1032. iter = file->private_data;
  1033. if (iter->buffer_idx) {
  1034. iter->filtered++;
  1035. iter->buffer[iter->buffer_idx] = 0;
  1036. ftrace_match(iter->buffer, iter->buffer_idx, enable);
  1037. }
  1038. mutex_lock(&ftrace_sysctl_lock);
  1039. mutex_lock(&ftrace_start_lock);
  1040. if (ftrace_start_up && ftrace_enabled)
  1041. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  1042. mutex_unlock(&ftrace_start_lock);
  1043. mutex_unlock(&ftrace_sysctl_lock);
  1044. kfree(iter);
  1045. mutex_unlock(&ftrace_regex_lock);
  1046. return 0;
  1047. }
  1048. static int
  1049. ftrace_filter_release(struct inode *inode, struct file *file)
  1050. {
  1051. return ftrace_regex_release(inode, file, 1);
  1052. }
  1053. static int
  1054. ftrace_notrace_release(struct inode *inode, struct file *file)
  1055. {
  1056. return ftrace_regex_release(inode, file, 0);
  1057. }
  1058. static struct file_operations ftrace_avail_fops = {
  1059. .open = ftrace_avail_open,
  1060. .read = seq_read,
  1061. .llseek = seq_lseek,
  1062. .release = ftrace_avail_release,
  1063. };
  1064. static struct file_operations ftrace_failures_fops = {
  1065. .open = ftrace_failures_open,
  1066. .read = seq_read,
  1067. .llseek = seq_lseek,
  1068. .release = ftrace_avail_release,
  1069. };
  1070. static struct file_operations ftrace_filter_fops = {
  1071. .open = ftrace_filter_open,
  1072. .read = ftrace_regex_read,
  1073. .write = ftrace_filter_write,
  1074. .llseek = ftrace_regex_lseek,
  1075. .release = ftrace_filter_release,
  1076. };
  1077. static struct file_operations ftrace_notrace_fops = {
  1078. .open = ftrace_notrace_open,
  1079. .read = ftrace_regex_read,
  1080. .write = ftrace_notrace_write,
  1081. .llseek = ftrace_regex_lseek,
  1082. .release = ftrace_notrace_release,
  1083. };
  1084. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1085. static DEFINE_MUTEX(graph_lock);
  1086. int ftrace_graph_count;
  1087. unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
  1088. static void *
  1089. g_next(struct seq_file *m, void *v, loff_t *pos)
  1090. {
  1091. unsigned long *array = m->private;
  1092. int index = *pos;
  1093. (*pos)++;
  1094. if (index >= ftrace_graph_count)
  1095. return NULL;
  1096. return &array[index];
  1097. }
  1098. static void *g_start(struct seq_file *m, loff_t *pos)
  1099. {
  1100. void *p = NULL;
  1101. mutex_lock(&graph_lock);
  1102. p = g_next(m, p, pos);
  1103. return p;
  1104. }
  1105. static void g_stop(struct seq_file *m, void *p)
  1106. {
  1107. mutex_unlock(&graph_lock);
  1108. }
  1109. static int g_show(struct seq_file *m, void *v)
  1110. {
  1111. unsigned long *ptr = v;
  1112. char str[KSYM_SYMBOL_LEN];
  1113. if (!ptr)
  1114. return 0;
  1115. kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
  1116. seq_printf(m, "%s\n", str);
  1117. return 0;
  1118. }
  1119. static struct seq_operations ftrace_graph_seq_ops = {
  1120. .start = g_start,
  1121. .next = g_next,
  1122. .stop = g_stop,
  1123. .show = g_show,
  1124. };
  1125. static int
  1126. ftrace_graph_open(struct inode *inode, struct file *file)
  1127. {
  1128. int ret = 0;
  1129. if (unlikely(ftrace_disabled))
  1130. return -ENODEV;
  1131. mutex_lock(&graph_lock);
  1132. if ((file->f_mode & FMODE_WRITE) &&
  1133. !(file->f_flags & O_APPEND)) {
  1134. ftrace_graph_count = 0;
  1135. memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
  1136. }
  1137. if (file->f_mode & FMODE_READ) {
  1138. ret = seq_open(file, &ftrace_graph_seq_ops);
  1139. if (!ret) {
  1140. struct seq_file *m = file->private_data;
  1141. m->private = ftrace_graph_funcs;
  1142. }
  1143. } else
  1144. file->private_data = ftrace_graph_funcs;
  1145. mutex_unlock(&graph_lock);
  1146. return ret;
  1147. }
  1148. static ssize_t
  1149. ftrace_graph_read(struct file *file, char __user *ubuf,
  1150. size_t cnt, loff_t *ppos)
  1151. {
  1152. if (file->f_mode & FMODE_READ)
  1153. return seq_read(file, ubuf, cnt, ppos);
  1154. else
  1155. return -EPERM;
  1156. }
  1157. static int
  1158. ftrace_set_func(unsigned long *array, int idx, char *buffer)
  1159. {
  1160. char str[KSYM_SYMBOL_LEN];
  1161. struct dyn_ftrace *rec;
  1162. struct ftrace_page *pg;
  1163. int found = 0;
  1164. int i, j;
  1165. if (ftrace_disabled)
  1166. return -ENODEV;
  1167. /* should not be called from interrupt context */
  1168. spin_lock(&ftrace_lock);
  1169. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  1170. for (i = 0; i < pg->index; i++) {
  1171. rec = &pg->records[i];
  1172. if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
  1173. continue;
  1174. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  1175. if (strcmp(str, buffer) == 0) {
  1176. found = 1;
  1177. for (j = 0; j < idx; j++)
  1178. if (array[j] == rec->ip) {
  1179. found = 0;
  1180. break;
  1181. }
  1182. if (found)
  1183. array[idx] = rec->ip;
  1184. break;
  1185. }
  1186. }
  1187. }
  1188. spin_unlock(&ftrace_lock);
  1189. return found ? 0 : -EINVAL;
  1190. }
  1191. static ssize_t
  1192. ftrace_graph_write(struct file *file, const char __user *ubuf,
  1193. size_t cnt, loff_t *ppos)
  1194. {
  1195. unsigned char buffer[FTRACE_BUFF_MAX+1];
  1196. unsigned long *array;
  1197. size_t read = 0;
  1198. ssize_t ret;
  1199. int index = 0;
  1200. char ch;
  1201. if (!cnt || cnt < 0)
  1202. return 0;
  1203. mutex_lock(&graph_lock);
  1204. if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
  1205. ret = -EBUSY;
  1206. goto out;
  1207. }
  1208. if (file->f_mode & FMODE_READ) {
  1209. struct seq_file *m = file->private_data;
  1210. array = m->private;
  1211. } else
  1212. array = file->private_data;
  1213. ret = get_user(ch, ubuf++);
  1214. if (ret)
  1215. goto out;
  1216. read++;
  1217. cnt--;
  1218. /* skip white space */
  1219. while (cnt && isspace(ch)) {
  1220. ret = get_user(ch, ubuf++);
  1221. if (ret)
  1222. goto out;
  1223. read++;
  1224. cnt--;
  1225. }
  1226. if (isspace(ch)) {
  1227. *ppos += read;
  1228. ret = read;
  1229. goto out;
  1230. }
  1231. while (cnt && !isspace(ch)) {
  1232. if (index < FTRACE_BUFF_MAX)
  1233. buffer[index++] = ch;
  1234. else {
  1235. ret = -EINVAL;
  1236. goto out;
  1237. }
  1238. ret = get_user(ch, ubuf++);
  1239. if (ret)
  1240. goto out;
  1241. read++;
  1242. cnt--;
  1243. }
  1244. buffer[index] = 0;
  1245. /* we allow only one at a time */
  1246. ret = ftrace_set_func(array, ftrace_graph_count, buffer);
  1247. if (ret)
  1248. goto out;
  1249. ftrace_graph_count++;
  1250. file->f_pos += read;
  1251. ret = read;
  1252. out:
  1253. mutex_unlock(&graph_lock);
  1254. return ret;
  1255. }
  1256. static const struct file_operations ftrace_graph_fops = {
  1257. .open = ftrace_graph_open,
  1258. .read = ftrace_graph_read,
  1259. .write = ftrace_graph_write,
  1260. };
  1261. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1262. static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
  1263. {
  1264. struct dentry *entry;
  1265. entry = debugfs_create_file("available_filter_functions", 0444,
  1266. d_tracer, NULL, &ftrace_avail_fops);
  1267. if (!entry)
  1268. pr_warning("Could not create debugfs "
  1269. "'available_filter_functions' entry\n");
  1270. entry = debugfs_create_file("failures", 0444,
  1271. d_tracer, NULL, &ftrace_failures_fops);
  1272. if (!entry)
  1273. pr_warning("Could not create debugfs 'failures' entry\n");
  1274. entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
  1275. NULL, &ftrace_filter_fops);
  1276. if (!entry)
  1277. pr_warning("Could not create debugfs "
  1278. "'set_ftrace_filter' entry\n");
  1279. entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
  1280. NULL, &ftrace_notrace_fops);
  1281. if (!entry)
  1282. pr_warning("Could not create debugfs "
  1283. "'set_ftrace_notrace' entry\n");
  1284. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1285. entry = debugfs_create_file("set_graph_function", 0444, d_tracer,
  1286. NULL,
  1287. &ftrace_graph_fops);
  1288. if (!entry)
  1289. pr_warning("Could not create debugfs "
  1290. "'set_graph_function' entry\n");
  1291. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1292. return 0;
  1293. }
  1294. static int ftrace_convert_nops(struct module *mod,
  1295. unsigned long *start,
  1296. unsigned long *end)
  1297. {
  1298. unsigned long *p;
  1299. unsigned long addr;
  1300. unsigned long flags;
  1301. mutex_lock(&ftrace_start_lock);
  1302. p = start;
  1303. while (p < end) {
  1304. addr = ftrace_call_adjust(*p++);
  1305. /*
  1306. * Some architecture linkers will pad between
  1307. * the different mcount_loc sections of different
  1308. * object files to satisfy alignments.
  1309. * Skip any NULL pointers.
  1310. */
  1311. if (!addr)
  1312. continue;
  1313. ftrace_record_ip(addr);
  1314. }
  1315. /* disable interrupts to prevent kstop machine */
  1316. local_irq_save(flags);
  1317. ftrace_update_code(mod);
  1318. local_irq_restore(flags);
  1319. mutex_unlock(&ftrace_start_lock);
  1320. return 0;
  1321. }
  1322. void ftrace_init_module(struct module *mod,
  1323. unsigned long *start, unsigned long *end)
  1324. {
  1325. if (ftrace_disabled || start == end)
  1326. return;
  1327. ftrace_convert_nops(mod, start, end);
  1328. }
  1329. extern unsigned long __start_mcount_loc[];
  1330. extern unsigned long __stop_mcount_loc[];
  1331. void __init ftrace_init(void)
  1332. {
  1333. unsigned long count, addr, flags;
  1334. int ret;
  1335. /* Keep the ftrace pointer to the stub */
  1336. addr = (unsigned long)ftrace_stub;
  1337. local_irq_save(flags);
  1338. ftrace_dyn_arch_init(&addr);
  1339. local_irq_restore(flags);
  1340. /* ftrace_dyn_arch_init places the return code in addr */
  1341. if (addr)
  1342. goto failed;
  1343. count = __stop_mcount_loc - __start_mcount_loc;
  1344. ret = ftrace_dyn_table_alloc(count);
  1345. if (ret)
  1346. goto failed;
  1347. last_ftrace_enabled = ftrace_enabled = 1;
  1348. ret = ftrace_convert_nops(NULL,
  1349. __start_mcount_loc,
  1350. __stop_mcount_loc);
  1351. return;
  1352. failed:
  1353. ftrace_disabled = 1;
  1354. }
  1355. #else
  1356. static int __init ftrace_nodyn_init(void)
  1357. {
  1358. ftrace_enabled = 1;
  1359. return 0;
  1360. }
  1361. device_initcall(ftrace_nodyn_init);
  1362. static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
  1363. static inline void ftrace_startup_enable(int command) { }
  1364. /* Keep as macros so we do not need to define the commands */
  1365. # define ftrace_startup(command) do { } while (0)
  1366. # define ftrace_shutdown(command) do { } while (0)
  1367. # define ftrace_startup_sysctl() do { } while (0)
  1368. # define ftrace_shutdown_sysctl() do { } while (0)
  1369. #endif /* CONFIG_DYNAMIC_FTRACE */
  1370. static ssize_t
  1371. ftrace_pid_read(struct file *file, char __user *ubuf,
  1372. size_t cnt, loff_t *ppos)
  1373. {
  1374. char buf[64];
  1375. int r;
  1376. if (ftrace_pid_trace == ftrace_swapper_pid)
  1377. r = sprintf(buf, "swapper tasks\n");
  1378. else if (ftrace_pid_trace)
  1379. r = sprintf(buf, "%u\n", pid_nr(ftrace_pid_trace));
  1380. else
  1381. r = sprintf(buf, "no pid\n");
  1382. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1383. }
  1384. static void clear_ftrace_swapper(void)
  1385. {
  1386. struct task_struct *p;
  1387. int cpu;
  1388. get_online_cpus();
  1389. for_each_online_cpu(cpu) {
  1390. p = idle_task(cpu);
  1391. clear_tsk_trace_trace(p);
  1392. }
  1393. put_online_cpus();
  1394. }
  1395. static void set_ftrace_swapper(void)
  1396. {
  1397. struct task_struct *p;
  1398. int cpu;
  1399. get_online_cpus();
  1400. for_each_online_cpu(cpu) {
  1401. p = idle_task(cpu);
  1402. set_tsk_trace_trace(p);
  1403. }
  1404. put_online_cpus();
  1405. }
  1406. static void clear_ftrace_pid(struct pid *pid)
  1407. {
  1408. struct task_struct *p;
  1409. rcu_read_lock();
  1410. do_each_pid_task(pid, PIDTYPE_PID, p) {
  1411. clear_tsk_trace_trace(p);
  1412. } while_each_pid_task(pid, PIDTYPE_PID, p);
  1413. rcu_read_unlock();
  1414. put_pid(pid);
  1415. }
  1416. static void set_ftrace_pid(struct pid *pid)
  1417. {
  1418. struct task_struct *p;
  1419. rcu_read_lock();
  1420. do_each_pid_task(pid, PIDTYPE_PID, p) {
  1421. set_tsk_trace_trace(p);
  1422. } while_each_pid_task(pid, PIDTYPE_PID, p);
  1423. rcu_read_unlock();
  1424. }
  1425. static void clear_ftrace_pid_task(struct pid **pid)
  1426. {
  1427. if (*pid == ftrace_swapper_pid)
  1428. clear_ftrace_swapper();
  1429. else
  1430. clear_ftrace_pid(*pid);
  1431. *pid = NULL;
  1432. }
  1433. static void set_ftrace_pid_task(struct pid *pid)
  1434. {
  1435. if (pid == ftrace_swapper_pid)
  1436. set_ftrace_swapper();
  1437. else
  1438. set_ftrace_pid(pid);
  1439. }
  1440. static ssize_t
  1441. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  1442. size_t cnt, loff_t *ppos)
  1443. {
  1444. struct pid *pid;
  1445. char buf[64];
  1446. long val;
  1447. int ret;
  1448. if (cnt >= sizeof(buf))
  1449. return -EINVAL;
  1450. if (copy_from_user(&buf, ubuf, cnt))
  1451. return -EFAULT;
  1452. buf[cnt] = 0;
  1453. ret = strict_strtol(buf, 10, &val);
  1454. if (ret < 0)
  1455. return ret;
  1456. mutex_lock(&ftrace_start_lock);
  1457. if (val < 0) {
  1458. /* disable pid tracing */
  1459. if (!ftrace_pid_trace)
  1460. goto out;
  1461. clear_ftrace_pid_task(&ftrace_pid_trace);
  1462. } else {
  1463. /* swapper task is special */
  1464. if (!val) {
  1465. pid = ftrace_swapper_pid;
  1466. if (pid == ftrace_pid_trace)
  1467. goto out;
  1468. } else {
  1469. pid = find_get_pid(val);
  1470. if (pid == ftrace_pid_trace) {
  1471. put_pid(pid);
  1472. goto out;
  1473. }
  1474. }
  1475. if (ftrace_pid_trace)
  1476. clear_ftrace_pid_task(&ftrace_pid_trace);
  1477. if (!pid)
  1478. goto out;
  1479. ftrace_pid_trace = pid;
  1480. set_ftrace_pid_task(ftrace_pid_trace);
  1481. }
  1482. /* update the function call */
  1483. ftrace_update_pid_func();
  1484. ftrace_startup_enable(0);
  1485. out:
  1486. mutex_unlock(&ftrace_start_lock);
  1487. return cnt;
  1488. }
  1489. static struct file_operations ftrace_pid_fops = {
  1490. .read = ftrace_pid_read,
  1491. .write = ftrace_pid_write,
  1492. };
  1493. static __init int ftrace_init_debugfs(void)
  1494. {
  1495. struct dentry *d_tracer;
  1496. struct dentry *entry;
  1497. d_tracer = tracing_init_dentry();
  1498. if (!d_tracer)
  1499. return 0;
  1500. ftrace_init_dyn_debugfs(d_tracer);
  1501. entry = debugfs_create_file("set_ftrace_pid", 0644, d_tracer,
  1502. NULL, &ftrace_pid_fops);
  1503. if (!entry)
  1504. pr_warning("Could not create debugfs "
  1505. "'set_ftrace_pid' entry\n");
  1506. return 0;
  1507. }
  1508. fs_initcall(ftrace_init_debugfs);
  1509. /**
  1510. * ftrace_kill - kill ftrace
  1511. *
  1512. * This function should be used by panic code. It stops ftrace
  1513. * but in a not so nice way. If you need to simply kill ftrace
  1514. * from a non-atomic section, use ftrace_kill.
  1515. */
  1516. void ftrace_kill(void)
  1517. {
  1518. ftrace_disabled = 1;
  1519. ftrace_enabled = 0;
  1520. clear_ftrace_function();
  1521. }
  1522. /**
  1523. * register_ftrace_function - register a function for profiling
  1524. * @ops - ops structure that holds the function for profiling.
  1525. *
  1526. * Register a function to be called by all functions in the
  1527. * kernel.
  1528. *
  1529. * Note: @ops->func and all the functions it calls must be labeled
  1530. * with "notrace", otherwise it will go into a
  1531. * recursive loop.
  1532. */
  1533. int register_ftrace_function(struct ftrace_ops *ops)
  1534. {
  1535. int ret;
  1536. if (unlikely(ftrace_disabled))
  1537. return -1;
  1538. mutex_lock(&ftrace_sysctl_lock);
  1539. ret = __register_ftrace_function(ops);
  1540. ftrace_startup(0);
  1541. mutex_unlock(&ftrace_sysctl_lock);
  1542. return ret;
  1543. }
  1544. /**
  1545. * unregister_ftrace_function - unregister a function for profiling.
  1546. * @ops - ops structure that holds the function to unregister
  1547. *
  1548. * Unregister a function that was added to be called by ftrace profiling.
  1549. */
  1550. int unregister_ftrace_function(struct ftrace_ops *ops)
  1551. {
  1552. int ret;
  1553. mutex_lock(&ftrace_sysctl_lock);
  1554. ret = __unregister_ftrace_function(ops);
  1555. ftrace_shutdown(0);
  1556. mutex_unlock(&ftrace_sysctl_lock);
  1557. return ret;
  1558. }
  1559. int
  1560. ftrace_enable_sysctl(struct ctl_table *table, int write,
  1561. struct file *file, void __user *buffer, size_t *lenp,
  1562. loff_t *ppos)
  1563. {
  1564. int ret;
  1565. if (unlikely(ftrace_disabled))
  1566. return -ENODEV;
  1567. mutex_lock(&ftrace_sysctl_lock);
  1568. ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
  1569. if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
  1570. goto out;
  1571. last_ftrace_enabled = ftrace_enabled;
  1572. if (ftrace_enabled) {
  1573. ftrace_startup_sysctl();
  1574. /* we are starting ftrace again */
  1575. if (ftrace_list != &ftrace_list_end) {
  1576. if (ftrace_list->next == &ftrace_list_end)
  1577. ftrace_trace_function = ftrace_list->func;
  1578. else
  1579. ftrace_trace_function = ftrace_list_func;
  1580. }
  1581. } else {
  1582. /* stopping ftrace calls (just send to ftrace_stub) */
  1583. ftrace_trace_function = ftrace_stub;
  1584. ftrace_shutdown_sysctl();
  1585. }
  1586. out:
  1587. mutex_unlock(&ftrace_sysctl_lock);
  1588. return ret;
  1589. }
  1590. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1591. static atomic_t ftrace_graph_active;
  1592. static struct notifier_block ftrace_suspend_notifier;
  1593. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
  1594. {
  1595. return 0;
  1596. }
  1597. /* The callbacks that hook a function */
  1598. trace_func_graph_ret_t ftrace_graph_return =
  1599. (trace_func_graph_ret_t)ftrace_stub;
  1600. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  1601. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  1602. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  1603. {
  1604. int i;
  1605. int ret = 0;
  1606. unsigned long flags;
  1607. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  1608. struct task_struct *g, *t;
  1609. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  1610. ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
  1611. * sizeof(struct ftrace_ret_stack),
  1612. GFP_KERNEL);
  1613. if (!ret_stack_list[i]) {
  1614. start = 0;
  1615. end = i;
  1616. ret = -ENOMEM;
  1617. goto free;
  1618. }
  1619. }
  1620. read_lock_irqsave(&tasklist_lock, flags);
  1621. do_each_thread(g, t) {
  1622. if (start == end) {
  1623. ret = -EAGAIN;
  1624. goto unlock;
  1625. }
  1626. if (t->ret_stack == NULL) {
  1627. t->curr_ret_stack = -1;
  1628. /* Make sure IRQs see the -1 first: */
  1629. barrier();
  1630. t->ret_stack = ret_stack_list[start++];
  1631. atomic_set(&t->tracing_graph_pause, 0);
  1632. atomic_set(&t->trace_overrun, 0);
  1633. }
  1634. } while_each_thread(g, t);
  1635. unlock:
  1636. read_unlock_irqrestore(&tasklist_lock, flags);
  1637. free:
  1638. for (i = start; i < end; i++)
  1639. kfree(ret_stack_list[i]);
  1640. return ret;
  1641. }
  1642. /* Allocate a return stack for each task */
  1643. static int start_graph_tracing(void)
  1644. {
  1645. struct ftrace_ret_stack **ret_stack_list;
  1646. int ret;
  1647. ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
  1648. sizeof(struct ftrace_ret_stack *),
  1649. GFP_KERNEL);
  1650. if (!ret_stack_list)
  1651. return -ENOMEM;
  1652. do {
  1653. ret = alloc_retstack_tasklist(ret_stack_list);
  1654. } while (ret == -EAGAIN);
  1655. kfree(ret_stack_list);
  1656. return ret;
  1657. }
  1658. /*
  1659. * Hibernation protection.
  1660. * The state of the current task is too much unstable during
  1661. * suspend/restore to disk. We want to protect against that.
  1662. */
  1663. static int
  1664. ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
  1665. void *unused)
  1666. {
  1667. switch (state) {
  1668. case PM_HIBERNATION_PREPARE:
  1669. pause_graph_tracing();
  1670. break;
  1671. case PM_POST_HIBERNATION:
  1672. unpause_graph_tracing();
  1673. break;
  1674. }
  1675. return NOTIFY_DONE;
  1676. }
  1677. int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  1678. trace_func_graph_ent_t entryfunc)
  1679. {
  1680. int ret = 0;
  1681. mutex_lock(&ftrace_sysctl_lock);
  1682. ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
  1683. register_pm_notifier(&ftrace_suspend_notifier);
  1684. atomic_inc(&ftrace_graph_active);
  1685. ret = start_graph_tracing();
  1686. if (ret) {
  1687. atomic_dec(&ftrace_graph_active);
  1688. goto out;
  1689. }
  1690. ftrace_graph_return = retfunc;
  1691. ftrace_graph_entry = entryfunc;
  1692. ftrace_startup(FTRACE_START_FUNC_RET);
  1693. out:
  1694. mutex_unlock(&ftrace_sysctl_lock);
  1695. return ret;
  1696. }
  1697. void unregister_ftrace_graph(void)
  1698. {
  1699. mutex_lock(&ftrace_sysctl_lock);
  1700. atomic_dec(&ftrace_graph_active);
  1701. ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
  1702. ftrace_graph_entry = ftrace_graph_entry_stub;
  1703. ftrace_shutdown(FTRACE_STOP_FUNC_RET);
  1704. unregister_pm_notifier(&ftrace_suspend_notifier);
  1705. mutex_unlock(&ftrace_sysctl_lock);
  1706. }
  1707. /* Allocate a return stack for newly created task */
  1708. void ftrace_graph_init_task(struct task_struct *t)
  1709. {
  1710. if (atomic_read(&ftrace_graph_active)) {
  1711. t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  1712. * sizeof(struct ftrace_ret_stack),
  1713. GFP_KERNEL);
  1714. if (!t->ret_stack)
  1715. return;
  1716. t->curr_ret_stack = -1;
  1717. atomic_set(&t->tracing_graph_pause, 0);
  1718. atomic_set(&t->trace_overrun, 0);
  1719. } else
  1720. t->ret_stack = NULL;
  1721. }
  1722. void ftrace_graph_exit_task(struct task_struct *t)
  1723. {
  1724. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  1725. t->ret_stack = NULL;
  1726. /* NULL must become visible to IRQs before we free it: */
  1727. barrier();
  1728. kfree(ret_stack);
  1729. }
  1730. void ftrace_graph_stop(void)
  1731. {
  1732. ftrace_stop();
  1733. }
  1734. #endif