ftrace.c 43 KB

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