ftrace.c 47 KB

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