ftrace.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301
  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 <linux/hash.h>
  30. #include <trace/events/sched.h>
  31. #include <asm/ftrace.h>
  32. #include <asm/setup.h>
  33. #include "trace_output.h"
  34. #include "trace_stat.h"
  35. #define FTRACE_WARN_ON(cond) \
  36. do { \
  37. if (WARN_ON(cond)) \
  38. ftrace_kill(); \
  39. } while (0)
  40. #define FTRACE_WARN_ON_ONCE(cond) \
  41. do { \
  42. if (WARN_ON_ONCE(cond)) \
  43. ftrace_kill(); \
  44. } while (0)
  45. /* hash bits for specific function selection */
  46. #define FTRACE_HASH_BITS 7
  47. #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
  48. /* ftrace_enabled is a method to turn ftrace on or off */
  49. int ftrace_enabled __read_mostly;
  50. static int last_ftrace_enabled;
  51. /* Quick disabling of function tracer. */
  52. int function_trace_stop;
  53. /*
  54. * ftrace_disabled is set when an anomaly is discovered.
  55. * ftrace_disabled is much stronger than ftrace_enabled.
  56. */
  57. static int ftrace_disabled __read_mostly;
  58. static DEFINE_MUTEX(ftrace_lock);
  59. static struct ftrace_ops ftrace_list_end __read_mostly =
  60. {
  61. .func = ftrace_stub,
  62. };
  63. static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
  64. ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
  65. ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
  66. ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
  67. static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
  68. {
  69. struct ftrace_ops *op = ftrace_list;
  70. /* in case someone actually ports this to alpha! */
  71. read_barrier_depends();
  72. while (op != &ftrace_list_end) {
  73. /* silly alpha */
  74. read_barrier_depends();
  75. op->func(ip, parent_ip);
  76. op = op->next;
  77. };
  78. }
  79. static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
  80. {
  81. if (!test_tsk_trace_trace(current))
  82. return;
  83. ftrace_pid_function(ip, parent_ip);
  84. }
  85. static void set_ftrace_pid_function(ftrace_func_t func)
  86. {
  87. /* do not set ftrace_pid_function to itself! */
  88. if (func != ftrace_pid_func)
  89. ftrace_pid_function = func;
  90. }
  91. /**
  92. * clear_ftrace_function - reset the ftrace function
  93. *
  94. * This NULLs the ftrace function and in essence stops
  95. * tracing. There may be lag
  96. */
  97. void clear_ftrace_function(void)
  98. {
  99. ftrace_trace_function = ftrace_stub;
  100. __ftrace_trace_function = ftrace_stub;
  101. ftrace_pid_function = ftrace_stub;
  102. }
  103. #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  104. /*
  105. * For those archs that do not test ftrace_trace_stop in their
  106. * mcount call site, we need to do it from C.
  107. */
  108. static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
  109. {
  110. if (function_trace_stop)
  111. return;
  112. __ftrace_trace_function(ip, parent_ip);
  113. }
  114. #endif
  115. static int __register_ftrace_function(struct ftrace_ops *ops)
  116. {
  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. return 0;
  148. }
  149. static int __unregister_ftrace_function(struct ftrace_ops *ops)
  150. {
  151. struct ftrace_ops **p;
  152. /*
  153. * If we are removing the last function, then simply point
  154. * to the ftrace_stub.
  155. */
  156. if (ftrace_list == ops && ops->next == &ftrace_list_end) {
  157. ftrace_trace_function = ftrace_stub;
  158. ftrace_list = &ftrace_list_end;
  159. return 0;
  160. }
  161. for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
  162. if (*p == ops)
  163. break;
  164. if (*p != ops)
  165. return -1;
  166. *p = (*p)->next;
  167. if (ftrace_enabled) {
  168. /* If we only have one func left, then call that directly */
  169. if (ftrace_list->next == &ftrace_list_end) {
  170. ftrace_func_t func = ftrace_list->func;
  171. if (ftrace_pid_trace) {
  172. set_ftrace_pid_function(func);
  173. func = ftrace_pid_func;
  174. }
  175. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  176. ftrace_trace_function = func;
  177. #else
  178. __ftrace_trace_function = func;
  179. #endif
  180. }
  181. }
  182. return 0;
  183. }
  184. static void ftrace_update_pid_func(void)
  185. {
  186. ftrace_func_t func;
  187. if (ftrace_trace_function == ftrace_stub)
  188. return;
  189. func = ftrace_trace_function;
  190. if (ftrace_pid_trace) {
  191. set_ftrace_pid_function(func);
  192. func = ftrace_pid_func;
  193. } else {
  194. if (func == ftrace_pid_func)
  195. func = ftrace_pid_function;
  196. }
  197. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  198. ftrace_trace_function = func;
  199. #else
  200. __ftrace_trace_function = func;
  201. #endif
  202. }
  203. #ifdef CONFIG_FUNCTION_PROFILER
  204. struct ftrace_profile {
  205. struct hlist_node node;
  206. unsigned long ip;
  207. unsigned long counter;
  208. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  209. unsigned long long time;
  210. #endif
  211. };
  212. struct ftrace_profile_page {
  213. struct ftrace_profile_page *next;
  214. unsigned long index;
  215. struct ftrace_profile records[];
  216. };
  217. struct ftrace_profile_stat {
  218. atomic_t disabled;
  219. struct hlist_head *hash;
  220. struct ftrace_profile_page *pages;
  221. struct ftrace_profile_page *start;
  222. struct tracer_stat stat;
  223. };
  224. #define PROFILE_RECORDS_SIZE \
  225. (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
  226. #define PROFILES_PER_PAGE \
  227. (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
  228. static int ftrace_profile_bits __read_mostly;
  229. static int ftrace_profile_enabled __read_mostly;
  230. /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
  231. static DEFINE_MUTEX(ftrace_profile_lock);
  232. static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
  233. #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
  234. static void *
  235. function_stat_next(void *v, int idx)
  236. {
  237. struct ftrace_profile *rec = v;
  238. struct ftrace_profile_page *pg;
  239. pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
  240. again:
  241. if (idx != 0)
  242. rec++;
  243. if ((void *)rec >= (void *)&pg->records[pg->index]) {
  244. pg = pg->next;
  245. if (!pg)
  246. return NULL;
  247. rec = &pg->records[0];
  248. if (!rec->counter)
  249. goto again;
  250. }
  251. return rec;
  252. }
  253. static void *function_stat_start(struct tracer_stat *trace)
  254. {
  255. struct ftrace_profile_stat *stat =
  256. container_of(trace, struct ftrace_profile_stat, stat);
  257. if (!stat || !stat->start)
  258. return NULL;
  259. return function_stat_next(&stat->start->records[0], 0);
  260. }
  261. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  262. /* function graph compares on total time */
  263. static int function_stat_cmp(void *p1, void *p2)
  264. {
  265. struct ftrace_profile *a = p1;
  266. struct ftrace_profile *b = p2;
  267. if (a->time < b->time)
  268. return -1;
  269. if (a->time > b->time)
  270. return 1;
  271. else
  272. return 0;
  273. }
  274. #else
  275. /* not function graph compares against hits */
  276. static int function_stat_cmp(void *p1, void *p2)
  277. {
  278. struct ftrace_profile *a = p1;
  279. struct ftrace_profile *b = p2;
  280. if (a->counter < b->counter)
  281. return -1;
  282. if (a->counter > b->counter)
  283. return 1;
  284. else
  285. return 0;
  286. }
  287. #endif
  288. static int function_stat_headers(struct seq_file *m)
  289. {
  290. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  291. seq_printf(m, " Function "
  292. "Hit Time Avg\n"
  293. " -------- "
  294. "--- ---- ---\n");
  295. #else
  296. seq_printf(m, " Function Hit\n"
  297. " -------- ---\n");
  298. #endif
  299. return 0;
  300. }
  301. static int function_stat_show(struct seq_file *m, void *v)
  302. {
  303. struct ftrace_profile *rec = v;
  304. char str[KSYM_SYMBOL_LEN];
  305. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  306. static DEFINE_MUTEX(mutex);
  307. static struct trace_seq s;
  308. unsigned long long avg;
  309. #endif
  310. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  311. seq_printf(m, " %-30.30s %10lu", str, rec->counter);
  312. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  313. seq_printf(m, " ");
  314. avg = rec->time;
  315. do_div(avg, rec->counter);
  316. mutex_lock(&mutex);
  317. trace_seq_init(&s);
  318. trace_print_graph_duration(rec->time, &s);
  319. trace_seq_puts(&s, " ");
  320. trace_print_graph_duration(avg, &s);
  321. trace_print_seq(m, &s);
  322. mutex_unlock(&mutex);
  323. #endif
  324. seq_putc(m, '\n');
  325. return 0;
  326. }
  327. static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
  328. {
  329. struct ftrace_profile_page *pg;
  330. pg = stat->pages = stat->start;
  331. while (pg) {
  332. memset(pg->records, 0, PROFILE_RECORDS_SIZE);
  333. pg->index = 0;
  334. pg = pg->next;
  335. }
  336. memset(stat->hash, 0,
  337. FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
  338. }
  339. int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
  340. {
  341. struct ftrace_profile_page *pg;
  342. int functions;
  343. int pages;
  344. int i;
  345. /* If we already allocated, do nothing */
  346. if (stat->pages)
  347. return 0;
  348. stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
  349. if (!stat->pages)
  350. return -ENOMEM;
  351. #ifdef CONFIG_DYNAMIC_FTRACE
  352. functions = ftrace_update_tot_cnt;
  353. #else
  354. /*
  355. * We do not know the number of functions that exist because
  356. * dynamic tracing is what counts them. With past experience
  357. * we have around 20K functions. That should be more than enough.
  358. * It is highly unlikely we will execute every function in
  359. * the kernel.
  360. */
  361. functions = 20000;
  362. #endif
  363. pg = stat->start = stat->pages;
  364. pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
  365. for (i = 0; i < pages; i++) {
  366. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  367. if (!pg->next)
  368. goto out_free;
  369. pg = pg->next;
  370. }
  371. return 0;
  372. out_free:
  373. pg = stat->start;
  374. while (pg) {
  375. unsigned long tmp = (unsigned long)pg;
  376. pg = pg->next;
  377. free_page(tmp);
  378. }
  379. free_page((unsigned long)stat->pages);
  380. stat->pages = NULL;
  381. stat->start = NULL;
  382. return -ENOMEM;
  383. }
  384. static int ftrace_profile_init_cpu(int cpu)
  385. {
  386. struct ftrace_profile_stat *stat;
  387. int size;
  388. stat = &per_cpu(ftrace_profile_stats, cpu);
  389. if (stat->hash) {
  390. /* If the profile is already created, simply reset it */
  391. ftrace_profile_reset(stat);
  392. return 0;
  393. }
  394. /*
  395. * We are profiling all functions, but usually only a few thousand
  396. * functions are hit. We'll make a hash of 1024 items.
  397. */
  398. size = FTRACE_PROFILE_HASH_SIZE;
  399. stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
  400. if (!stat->hash)
  401. return -ENOMEM;
  402. if (!ftrace_profile_bits) {
  403. size--;
  404. for (; size; size >>= 1)
  405. ftrace_profile_bits++;
  406. }
  407. /* Preallocate the function profiling pages */
  408. if (ftrace_profile_pages_init(stat) < 0) {
  409. kfree(stat->hash);
  410. stat->hash = NULL;
  411. return -ENOMEM;
  412. }
  413. return 0;
  414. }
  415. static int ftrace_profile_init(void)
  416. {
  417. int cpu;
  418. int ret = 0;
  419. for_each_online_cpu(cpu) {
  420. ret = ftrace_profile_init_cpu(cpu);
  421. if (ret)
  422. break;
  423. }
  424. return ret;
  425. }
  426. /* interrupts must be disabled */
  427. static struct ftrace_profile *
  428. ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
  429. {
  430. struct ftrace_profile *rec;
  431. struct hlist_head *hhd;
  432. struct hlist_node *n;
  433. unsigned long key;
  434. key = hash_long(ip, ftrace_profile_bits);
  435. hhd = &stat->hash[key];
  436. if (hlist_empty(hhd))
  437. return NULL;
  438. hlist_for_each_entry_rcu(rec, n, hhd, node) {
  439. if (rec->ip == ip)
  440. return rec;
  441. }
  442. return NULL;
  443. }
  444. static void ftrace_add_profile(struct ftrace_profile_stat *stat,
  445. struct ftrace_profile *rec)
  446. {
  447. unsigned long key;
  448. key = hash_long(rec->ip, ftrace_profile_bits);
  449. hlist_add_head_rcu(&rec->node, &stat->hash[key]);
  450. }
  451. /*
  452. * The memory is already allocated, this simply finds a new record to use.
  453. */
  454. static struct ftrace_profile *
  455. ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
  456. {
  457. struct ftrace_profile *rec = NULL;
  458. /* prevent recursion (from NMIs) */
  459. if (atomic_inc_return(&stat->disabled) != 1)
  460. goto out;
  461. /*
  462. * Try to find the function again since an NMI
  463. * could have added it
  464. */
  465. rec = ftrace_find_profiled_func(stat, ip);
  466. if (rec)
  467. goto out;
  468. if (stat->pages->index == PROFILES_PER_PAGE) {
  469. if (!stat->pages->next)
  470. goto out;
  471. stat->pages = stat->pages->next;
  472. }
  473. rec = &stat->pages->records[stat->pages->index++];
  474. rec->ip = ip;
  475. ftrace_add_profile(stat, rec);
  476. out:
  477. atomic_dec(&stat->disabled);
  478. return rec;
  479. }
  480. static void
  481. function_profile_call(unsigned long ip, unsigned long parent_ip)
  482. {
  483. struct ftrace_profile_stat *stat;
  484. struct ftrace_profile *rec;
  485. unsigned long flags;
  486. if (!ftrace_profile_enabled)
  487. return;
  488. local_irq_save(flags);
  489. stat = &__get_cpu_var(ftrace_profile_stats);
  490. if (!stat->hash || !ftrace_profile_enabled)
  491. goto out;
  492. rec = ftrace_find_profiled_func(stat, ip);
  493. if (!rec) {
  494. rec = ftrace_profile_alloc(stat, ip);
  495. if (!rec)
  496. goto out;
  497. }
  498. rec->counter++;
  499. out:
  500. local_irq_restore(flags);
  501. }
  502. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  503. static int profile_graph_entry(struct ftrace_graph_ent *trace)
  504. {
  505. function_profile_call(trace->func, 0);
  506. return 1;
  507. }
  508. static void profile_graph_return(struct ftrace_graph_ret *trace)
  509. {
  510. struct ftrace_profile_stat *stat;
  511. unsigned long long calltime;
  512. struct ftrace_profile *rec;
  513. unsigned long flags;
  514. local_irq_save(flags);
  515. stat = &__get_cpu_var(ftrace_profile_stats);
  516. if (!stat->hash || !ftrace_profile_enabled)
  517. goto out;
  518. calltime = trace->rettime - trace->calltime;
  519. if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
  520. int index;
  521. index = trace->depth;
  522. /* Append this call time to the parent time to subtract */
  523. if (index)
  524. current->ret_stack[index - 1].subtime += calltime;
  525. if (current->ret_stack[index].subtime < calltime)
  526. calltime -= current->ret_stack[index].subtime;
  527. else
  528. calltime = 0;
  529. }
  530. rec = ftrace_find_profiled_func(stat, trace->func);
  531. if (rec)
  532. rec->time += calltime;
  533. out:
  534. local_irq_restore(flags);
  535. }
  536. static int register_ftrace_profiler(void)
  537. {
  538. return register_ftrace_graph(&profile_graph_return,
  539. &profile_graph_entry);
  540. }
  541. static void unregister_ftrace_profiler(void)
  542. {
  543. unregister_ftrace_graph();
  544. }
  545. #else
  546. static struct ftrace_ops ftrace_profile_ops __read_mostly =
  547. {
  548. .func = function_profile_call,
  549. };
  550. static int register_ftrace_profiler(void)
  551. {
  552. return register_ftrace_function(&ftrace_profile_ops);
  553. }
  554. static void unregister_ftrace_profiler(void)
  555. {
  556. unregister_ftrace_function(&ftrace_profile_ops);
  557. }
  558. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  559. static ssize_t
  560. ftrace_profile_write(struct file *filp, const char __user *ubuf,
  561. size_t cnt, loff_t *ppos)
  562. {
  563. unsigned long val;
  564. char buf[64]; /* big enough to hold a number */
  565. int ret;
  566. if (cnt >= sizeof(buf))
  567. return -EINVAL;
  568. if (copy_from_user(&buf, ubuf, cnt))
  569. return -EFAULT;
  570. buf[cnt] = 0;
  571. ret = strict_strtoul(buf, 10, &val);
  572. if (ret < 0)
  573. return ret;
  574. val = !!val;
  575. mutex_lock(&ftrace_profile_lock);
  576. if (ftrace_profile_enabled ^ val) {
  577. if (val) {
  578. ret = ftrace_profile_init();
  579. if (ret < 0) {
  580. cnt = ret;
  581. goto out;
  582. }
  583. ret = register_ftrace_profiler();
  584. if (ret < 0) {
  585. cnt = ret;
  586. goto out;
  587. }
  588. ftrace_profile_enabled = 1;
  589. } else {
  590. ftrace_profile_enabled = 0;
  591. /*
  592. * unregister_ftrace_profiler calls stop_machine
  593. * so this acts like an synchronize_sched.
  594. */
  595. unregister_ftrace_profiler();
  596. }
  597. }
  598. out:
  599. mutex_unlock(&ftrace_profile_lock);
  600. filp->f_pos += cnt;
  601. return cnt;
  602. }
  603. static ssize_t
  604. ftrace_profile_read(struct file *filp, char __user *ubuf,
  605. size_t cnt, loff_t *ppos)
  606. {
  607. char buf[64]; /* big enough to hold a number */
  608. int r;
  609. r = sprintf(buf, "%u\n", ftrace_profile_enabled);
  610. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  611. }
  612. static const struct file_operations ftrace_profile_fops = {
  613. .open = tracing_open_generic,
  614. .read = ftrace_profile_read,
  615. .write = ftrace_profile_write,
  616. };
  617. /* used to initialize the real stat files */
  618. static struct tracer_stat function_stats __initdata = {
  619. .name = "functions",
  620. .stat_start = function_stat_start,
  621. .stat_next = function_stat_next,
  622. .stat_cmp = function_stat_cmp,
  623. .stat_headers = function_stat_headers,
  624. .stat_show = function_stat_show
  625. };
  626. static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
  627. {
  628. struct ftrace_profile_stat *stat;
  629. struct dentry *entry;
  630. char *name;
  631. int ret;
  632. int cpu;
  633. for_each_possible_cpu(cpu) {
  634. stat = &per_cpu(ftrace_profile_stats, cpu);
  635. /* allocate enough for function name + cpu number */
  636. name = kmalloc(32, GFP_KERNEL);
  637. if (!name) {
  638. /*
  639. * The files created are permanent, if something happens
  640. * we still do not free memory.
  641. */
  642. WARN(1,
  643. "Could not allocate stat file for cpu %d\n",
  644. cpu);
  645. return;
  646. }
  647. stat->stat = function_stats;
  648. snprintf(name, 32, "function%d", cpu);
  649. stat->stat.name = name;
  650. ret = register_stat_tracer(&stat->stat);
  651. if (ret) {
  652. WARN(1,
  653. "Could not register function stat for cpu %d\n",
  654. cpu);
  655. kfree(name);
  656. return;
  657. }
  658. }
  659. entry = debugfs_create_file("function_profile_enabled", 0644,
  660. d_tracer, NULL, &ftrace_profile_fops);
  661. if (!entry)
  662. pr_warning("Could not create debugfs "
  663. "'function_profile_enabled' entry\n");
  664. }
  665. #else /* CONFIG_FUNCTION_PROFILER */
  666. static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
  667. {
  668. }
  669. #endif /* CONFIG_FUNCTION_PROFILER */
  670. /* set when tracing only a pid */
  671. struct pid *ftrace_pid_trace;
  672. static struct pid * const ftrace_swapper_pid = &init_struct_pid;
  673. #ifdef CONFIG_DYNAMIC_FTRACE
  674. #ifndef CONFIG_FTRACE_MCOUNT_RECORD
  675. # error Dynamic ftrace depends on MCOUNT_RECORD
  676. #endif
  677. static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
  678. struct ftrace_func_probe {
  679. struct hlist_node node;
  680. struct ftrace_probe_ops *ops;
  681. unsigned long flags;
  682. unsigned long ip;
  683. void *data;
  684. struct rcu_head rcu;
  685. };
  686. enum {
  687. FTRACE_ENABLE_CALLS = (1 << 0),
  688. FTRACE_DISABLE_CALLS = (1 << 1),
  689. FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
  690. FTRACE_ENABLE_MCOUNT = (1 << 3),
  691. FTRACE_DISABLE_MCOUNT = (1 << 4),
  692. FTRACE_START_FUNC_RET = (1 << 5),
  693. FTRACE_STOP_FUNC_RET = (1 << 6),
  694. };
  695. static int ftrace_filtered;
  696. static struct dyn_ftrace *ftrace_new_addrs;
  697. static DEFINE_MUTEX(ftrace_regex_lock);
  698. struct ftrace_page {
  699. struct ftrace_page *next;
  700. int index;
  701. struct dyn_ftrace records[];
  702. };
  703. #define ENTRIES_PER_PAGE \
  704. ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
  705. /* estimate from running different kernels */
  706. #define NR_TO_INIT 10000
  707. static struct ftrace_page *ftrace_pages_start;
  708. static struct ftrace_page *ftrace_pages;
  709. static struct dyn_ftrace *ftrace_free_records;
  710. /*
  711. * This is a double for. Do not use 'break' to break out of the loop,
  712. * you must use a goto.
  713. */
  714. #define do_for_each_ftrace_rec(pg, rec) \
  715. for (pg = ftrace_pages_start; pg; pg = pg->next) { \
  716. int _____i; \
  717. for (_____i = 0; _____i < pg->index; _____i++) { \
  718. rec = &pg->records[_____i];
  719. #define while_for_each_ftrace_rec() \
  720. } \
  721. }
  722. #ifdef CONFIG_KPROBES
  723. static int frozen_record_count;
  724. static inline void freeze_record(struct dyn_ftrace *rec)
  725. {
  726. if (!(rec->flags & FTRACE_FL_FROZEN)) {
  727. rec->flags |= FTRACE_FL_FROZEN;
  728. frozen_record_count++;
  729. }
  730. }
  731. static inline void unfreeze_record(struct dyn_ftrace *rec)
  732. {
  733. if (rec->flags & FTRACE_FL_FROZEN) {
  734. rec->flags &= ~FTRACE_FL_FROZEN;
  735. frozen_record_count--;
  736. }
  737. }
  738. static inline int record_frozen(struct dyn_ftrace *rec)
  739. {
  740. return rec->flags & FTRACE_FL_FROZEN;
  741. }
  742. #else
  743. # define freeze_record(rec) ({ 0; })
  744. # define unfreeze_record(rec) ({ 0; })
  745. # define record_frozen(rec) ({ 0; })
  746. #endif /* CONFIG_KPROBES */
  747. static void ftrace_free_rec(struct dyn_ftrace *rec)
  748. {
  749. rec->freelist = ftrace_free_records;
  750. ftrace_free_records = rec;
  751. rec->flags |= FTRACE_FL_FREE;
  752. }
  753. static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
  754. {
  755. struct dyn_ftrace *rec;
  756. /* First check for freed records */
  757. if (ftrace_free_records) {
  758. rec = ftrace_free_records;
  759. if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
  760. FTRACE_WARN_ON_ONCE(1);
  761. ftrace_free_records = NULL;
  762. return NULL;
  763. }
  764. ftrace_free_records = rec->freelist;
  765. memset(rec, 0, sizeof(*rec));
  766. return rec;
  767. }
  768. if (ftrace_pages->index == ENTRIES_PER_PAGE) {
  769. if (!ftrace_pages->next) {
  770. /* allocate another page */
  771. ftrace_pages->next =
  772. (void *)get_zeroed_page(GFP_KERNEL);
  773. if (!ftrace_pages->next)
  774. return NULL;
  775. }
  776. ftrace_pages = ftrace_pages->next;
  777. }
  778. return &ftrace_pages->records[ftrace_pages->index++];
  779. }
  780. static struct dyn_ftrace *
  781. ftrace_record_ip(unsigned long ip)
  782. {
  783. struct dyn_ftrace *rec;
  784. if (ftrace_disabled)
  785. return NULL;
  786. rec = ftrace_alloc_dyn_node(ip);
  787. if (!rec)
  788. return NULL;
  789. rec->ip = ip;
  790. rec->newlist = ftrace_new_addrs;
  791. ftrace_new_addrs = rec;
  792. return rec;
  793. }
  794. static void print_ip_ins(const char *fmt, unsigned char *p)
  795. {
  796. int i;
  797. printk(KERN_CONT "%s", fmt);
  798. for (i = 0; i < MCOUNT_INSN_SIZE; i++)
  799. printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
  800. }
  801. static void ftrace_bug(int failed, unsigned long ip)
  802. {
  803. switch (failed) {
  804. case -EFAULT:
  805. FTRACE_WARN_ON_ONCE(1);
  806. pr_info("ftrace faulted on modifying ");
  807. print_ip_sym(ip);
  808. break;
  809. case -EINVAL:
  810. FTRACE_WARN_ON_ONCE(1);
  811. pr_info("ftrace failed to modify ");
  812. print_ip_sym(ip);
  813. print_ip_ins(" actual: ", (unsigned char *)ip);
  814. printk(KERN_CONT "\n");
  815. break;
  816. case -EPERM:
  817. FTRACE_WARN_ON_ONCE(1);
  818. pr_info("ftrace faulted on writing ");
  819. print_ip_sym(ip);
  820. break;
  821. default:
  822. FTRACE_WARN_ON_ONCE(1);
  823. pr_info("ftrace faulted on unknown error ");
  824. print_ip_sym(ip);
  825. }
  826. }
  827. static int
  828. __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
  829. {
  830. unsigned long ftrace_addr;
  831. unsigned long flag = 0UL;
  832. ftrace_addr = (unsigned long)FTRACE_ADDR;
  833. /*
  834. * If this record is not to be traced or we want to disable it,
  835. * then disable it.
  836. *
  837. * If we want to enable it and filtering is off, then enable it.
  838. *
  839. * If we want to enable it and filtering is on, enable it only if
  840. * it's filtered
  841. */
  842. if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
  843. if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
  844. flag = FTRACE_FL_ENABLED;
  845. }
  846. /* If the state of this record hasn't changed, then do nothing */
  847. if ((rec->flags & FTRACE_FL_ENABLED) == flag)
  848. return 0;
  849. if (flag) {
  850. rec->flags |= FTRACE_FL_ENABLED;
  851. return ftrace_make_call(rec, ftrace_addr);
  852. }
  853. rec->flags &= ~FTRACE_FL_ENABLED;
  854. return ftrace_make_nop(NULL, rec, ftrace_addr);
  855. }
  856. static void ftrace_replace_code(int enable)
  857. {
  858. struct dyn_ftrace *rec;
  859. struct ftrace_page *pg;
  860. int failed;
  861. do_for_each_ftrace_rec(pg, rec) {
  862. /*
  863. * Skip over free records, records that have
  864. * failed and not converted.
  865. */
  866. if (rec->flags & FTRACE_FL_FREE ||
  867. rec->flags & FTRACE_FL_FAILED ||
  868. !(rec->flags & FTRACE_FL_CONVERTED))
  869. continue;
  870. /* ignore updates to this record's mcount site */
  871. if (get_kprobe((void *)rec->ip)) {
  872. freeze_record(rec);
  873. continue;
  874. } else {
  875. unfreeze_record(rec);
  876. }
  877. failed = __ftrace_replace_code(rec, enable);
  878. if (failed) {
  879. rec->flags |= FTRACE_FL_FAILED;
  880. if ((system_state == SYSTEM_BOOTING) ||
  881. !core_kernel_text(rec->ip)) {
  882. ftrace_free_rec(rec);
  883. } else {
  884. ftrace_bug(failed, rec->ip);
  885. /* Stop processing */
  886. return;
  887. }
  888. }
  889. } while_for_each_ftrace_rec();
  890. }
  891. static int
  892. ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
  893. {
  894. unsigned long ip;
  895. int ret;
  896. ip = rec->ip;
  897. ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
  898. if (ret) {
  899. ftrace_bug(ret, ip);
  900. rec->flags |= FTRACE_FL_FAILED;
  901. return 0;
  902. }
  903. return 1;
  904. }
  905. /*
  906. * archs can override this function if they must do something
  907. * before the modifying code is performed.
  908. */
  909. int __weak ftrace_arch_code_modify_prepare(void)
  910. {
  911. return 0;
  912. }
  913. /*
  914. * archs can override this function if they must do something
  915. * after the modifying code is performed.
  916. */
  917. int __weak ftrace_arch_code_modify_post_process(void)
  918. {
  919. return 0;
  920. }
  921. static int __ftrace_modify_code(void *data)
  922. {
  923. int *command = data;
  924. if (*command & FTRACE_ENABLE_CALLS)
  925. ftrace_replace_code(1);
  926. else if (*command & FTRACE_DISABLE_CALLS)
  927. ftrace_replace_code(0);
  928. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  929. ftrace_update_ftrace_func(ftrace_trace_function);
  930. if (*command & FTRACE_START_FUNC_RET)
  931. ftrace_enable_ftrace_graph_caller();
  932. else if (*command & FTRACE_STOP_FUNC_RET)
  933. ftrace_disable_ftrace_graph_caller();
  934. return 0;
  935. }
  936. static void ftrace_run_update_code(int command)
  937. {
  938. int ret;
  939. ret = ftrace_arch_code_modify_prepare();
  940. FTRACE_WARN_ON(ret);
  941. if (ret)
  942. return;
  943. stop_machine(__ftrace_modify_code, &command, NULL);
  944. ret = ftrace_arch_code_modify_post_process();
  945. FTRACE_WARN_ON(ret);
  946. }
  947. static ftrace_func_t saved_ftrace_func;
  948. static int ftrace_start_up;
  949. static void ftrace_startup_enable(int command)
  950. {
  951. if (saved_ftrace_func != ftrace_trace_function) {
  952. saved_ftrace_func = ftrace_trace_function;
  953. command |= FTRACE_UPDATE_TRACE_FUNC;
  954. }
  955. if (!command || !ftrace_enabled)
  956. return;
  957. ftrace_run_update_code(command);
  958. }
  959. static void ftrace_startup(int command)
  960. {
  961. if (unlikely(ftrace_disabled))
  962. return;
  963. ftrace_start_up++;
  964. command |= FTRACE_ENABLE_CALLS;
  965. ftrace_startup_enable(command);
  966. }
  967. static void ftrace_shutdown(int command)
  968. {
  969. if (unlikely(ftrace_disabled))
  970. return;
  971. ftrace_start_up--;
  972. /*
  973. * Just warn in case of unbalance, no need to kill ftrace, it's not
  974. * critical but the ftrace_call callers may be never nopped again after
  975. * further ftrace uses.
  976. */
  977. WARN_ON_ONCE(ftrace_start_up < 0);
  978. if (!ftrace_start_up)
  979. command |= FTRACE_DISABLE_CALLS;
  980. if (saved_ftrace_func != ftrace_trace_function) {
  981. saved_ftrace_func = ftrace_trace_function;
  982. command |= FTRACE_UPDATE_TRACE_FUNC;
  983. }
  984. if (!command || !ftrace_enabled)
  985. return;
  986. ftrace_run_update_code(command);
  987. }
  988. static void ftrace_startup_sysctl(void)
  989. {
  990. int command = FTRACE_ENABLE_MCOUNT;
  991. if (unlikely(ftrace_disabled))
  992. return;
  993. /* Force update next time */
  994. saved_ftrace_func = NULL;
  995. /* ftrace_start_up is true if we want ftrace running */
  996. if (ftrace_start_up)
  997. command |= FTRACE_ENABLE_CALLS;
  998. ftrace_run_update_code(command);
  999. }
  1000. static void ftrace_shutdown_sysctl(void)
  1001. {
  1002. int command = FTRACE_DISABLE_MCOUNT;
  1003. if (unlikely(ftrace_disabled))
  1004. return;
  1005. /* ftrace_start_up is true if ftrace is running */
  1006. if (ftrace_start_up)
  1007. command |= FTRACE_DISABLE_CALLS;
  1008. ftrace_run_update_code(command);
  1009. }
  1010. static cycle_t ftrace_update_time;
  1011. static unsigned long ftrace_update_cnt;
  1012. unsigned long ftrace_update_tot_cnt;
  1013. static int ftrace_update_code(struct module *mod)
  1014. {
  1015. struct dyn_ftrace *p;
  1016. cycle_t start, stop;
  1017. start = ftrace_now(raw_smp_processor_id());
  1018. ftrace_update_cnt = 0;
  1019. while (ftrace_new_addrs) {
  1020. /* If something went wrong, bail without enabling anything */
  1021. if (unlikely(ftrace_disabled))
  1022. return -1;
  1023. p = ftrace_new_addrs;
  1024. ftrace_new_addrs = p->newlist;
  1025. p->flags = 0L;
  1026. /* convert record (i.e, patch mcount-call with NOP) */
  1027. if (ftrace_code_disable(mod, p)) {
  1028. p->flags |= FTRACE_FL_CONVERTED;
  1029. ftrace_update_cnt++;
  1030. } else
  1031. ftrace_free_rec(p);
  1032. }
  1033. stop = ftrace_now(raw_smp_processor_id());
  1034. ftrace_update_time = stop - start;
  1035. ftrace_update_tot_cnt += ftrace_update_cnt;
  1036. return 0;
  1037. }
  1038. static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
  1039. {
  1040. struct ftrace_page *pg;
  1041. int cnt;
  1042. int i;
  1043. /* allocate a few pages */
  1044. ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
  1045. if (!ftrace_pages_start)
  1046. return -1;
  1047. /*
  1048. * Allocate a few more pages.
  1049. *
  1050. * TODO: have some parser search vmlinux before
  1051. * final linking to find all calls to ftrace.
  1052. * Then we can:
  1053. * a) know how many pages to allocate.
  1054. * and/or
  1055. * b) set up the table then.
  1056. *
  1057. * The dynamic code is still necessary for
  1058. * modules.
  1059. */
  1060. pg = ftrace_pages = ftrace_pages_start;
  1061. cnt = num_to_init / ENTRIES_PER_PAGE;
  1062. pr_info("ftrace: allocating %ld entries in %d pages\n",
  1063. num_to_init, cnt + 1);
  1064. for (i = 0; i < cnt; i++) {
  1065. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  1066. /* If we fail, we'll try later anyway */
  1067. if (!pg->next)
  1068. break;
  1069. pg = pg->next;
  1070. }
  1071. return 0;
  1072. }
  1073. enum {
  1074. FTRACE_ITER_FILTER = (1 << 0),
  1075. FTRACE_ITER_NOTRACE = (1 << 1),
  1076. FTRACE_ITER_FAILURES = (1 << 2),
  1077. FTRACE_ITER_PRINTALL = (1 << 3),
  1078. FTRACE_ITER_HASH = (1 << 4),
  1079. };
  1080. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  1081. struct ftrace_iterator {
  1082. struct ftrace_page *pg;
  1083. int hidx;
  1084. int idx;
  1085. unsigned flags;
  1086. struct trace_parser parser;
  1087. };
  1088. static void *
  1089. t_hash_next(struct seq_file *m, void *v, loff_t *pos)
  1090. {
  1091. struct ftrace_iterator *iter = m->private;
  1092. struct hlist_node *hnd = v;
  1093. struct hlist_head *hhd;
  1094. WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
  1095. (*pos)++;
  1096. retry:
  1097. if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
  1098. return NULL;
  1099. hhd = &ftrace_func_hash[iter->hidx];
  1100. if (hlist_empty(hhd)) {
  1101. iter->hidx++;
  1102. hnd = NULL;
  1103. goto retry;
  1104. }
  1105. if (!hnd)
  1106. hnd = hhd->first;
  1107. else {
  1108. hnd = hnd->next;
  1109. if (!hnd) {
  1110. iter->hidx++;
  1111. goto retry;
  1112. }
  1113. }
  1114. return hnd;
  1115. }
  1116. static void *t_hash_start(struct seq_file *m, loff_t *pos)
  1117. {
  1118. struct ftrace_iterator *iter = m->private;
  1119. void *p = NULL;
  1120. loff_t l;
  1121. if (!(iter->flags & FTRACE_ITER_HASH))
  1122. *pos = 0;
  1123. iter->flags |= FTRACE_ITER_HASH;
  1124. iter->hidx = 0;
  1125. for (l = 0; l <= *pos; ) {
  1126. p = t_hash_next(m, p, &l);
  1127. if (!p)
  1128. break;
  1129. }
  1130. return p;
  1131. }
  1132. static int t_hash_show(struct seq_file *m, void *v)
  1133. {
  1134. struct ftrace_func_probe *rec;
  1135. struct hlist_node *hnd = v;
  1136. rec = hlist_entry(hnd, struct ftrace_func_probe, node);
  1137. if (rec->ops->print)
  1138. return rec->ops->print(m, rec->ip, rec->ops, rec->data);
  1139. seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
  1140. if (rec->data)
  1141. seq_printf(m, ":%p", rec->data);
  1142. seq_putc(m, '\n');
  1143. return 0;
  1144. }
  1145. static void *
  1146. t_next(struct seq_file *m, void *v, loff_t *pos)
  1147. {
  1148. struct ftrace_iterator *iter = m->private;
  1149. struct dyn_ftrace *rec = NULL;
  1150. if (iter->flags & FTRACE_ITER_HASH)
  1151. return t_hash_next(m, v, pos);
  1152. (*pos)++;
  1153. if (iter->flags & FTRACE_ITER_PRINTALL)
  1154. return NULL;
  1155. retry:
  1156. if (iter->idx >= iter->pg->index) {
  1157. if (iter->pg->next) {
  1158. iter->pg = iter->pg->next;
  1159. iter->idx = 0;
  1160. goto retry;
  1161. }
  1162. } else {
  1163. rec = &iter->pg->records[iter->idx++];
  1164. if ((rec->flags & FTRACE_FL_FREE) ||
  1165. (!(iter->flags & FTRACE_ITER_FAILURES) &&
  1166. (rec->flags & FTRACE_FL_FAILED)) ||
  1167. ((iter->flags & FTRACE_ITER_FAILURES) &&
  1168. !(rec->flags & FTRACE_FL_FAILED)) ||
  1169. ((iter->flags & FTRACE_ITER_FILTER) &&
  1170. !(rec->flags & FTRACE_FL_FILTER)) ||
  1171. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  1172. !(rec->flags & FTRACE_FL_NOTRACE))) {
  1173. rec = NULL;
  1174. goto retry;
  1175. }
  1176. }
  1177. return rec;
  1178. }
  1179. static void *t_start(struct seq_file *m, loff_t *pos)
  1180. {
  1181. struct ftrace_iterator *iter = m->private;
  1182. void *p = NULL;
  1183. loff_t l;
  1184. mutex_lock(&ftrace_lock);
  1185. /*
  1186. * For set_ftrace_filter reading, if we have the filter
  1187. * off, we can short cut and just print out that all
  1188. * functions are enabled.
  1189. */
  1190. if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
  1191. if (*pos > 0)
  1192. return t_hash_start(m, pos);
  1193. iter->flags |= FTRACE_ITER_PRINTALL;
  1194. return iter;
  1195. }
  1196. if (iter->flags & FTRACE_ITER_HASH)
  1197. return t_hash_start(m, pos);
  1198. iter->pg = ftrace_pages_start;
  1199. iter->idx = 0;
  1200. for (l = 0; l <= *pos; ) {
  1201. p = t_next(m, p, &l);
  1202. if (!p)
  1203. break;
  1204. }
  1205. if (!p && iter->flags & FTRACE_ITER_FILTER)
  1206. return t_hash_start(m, pos);
  1207. return p;
  1208. }
  1209. static void t_stop(struct seq_file *m, void *p)
  1210. {
  1211. mutex_unlock(&ftrace_lock);
  1212. }
  1213. static int t_show(struct seq_file *m, void *v)
  1214. {
  1215. struct ftrace_iterator *iter = m->private;
  1216. struct dyn_ftrace *rec = v;
  1217. if (iter->flags & FTRACE_ITER_HASH)
  1218. return t_hash_show(m, v);
  1219. if (iter->flags & FTRACE_ITER_PRINTALL) {
  1220. seq_printf(m, "#### all functions enabled ####\n");
  1221. return 0;
  1222. }
  1223. if (!rec)
  1224. return 0;
  1225. seq_printf(m, "%ps\n", (void *)rec->ip);
  1226. return 0;
  1227. }
  1228. static const struct seq_operations show_ftrace_seq_ops = {
  1229. .start = t_start,
  1230. .next = t_next,
  1231. .stop = t_stop,
  1232. .show = t_show,
  1233. };
  1234. static int
  1235. ftrace_avail_open(struct inode *inode, struct file *file)
  1236. {
  1237. struct ftrace_iterator *iter;
  1238. int ret;
  1239. if (unlikely(ftrace_disabled))
  1240. return -ENODEV;
  1241. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1242. if (!iter)
  1243. return -ENOMEM;
  1244. iter->pg = ftrace_pages_start;
  1245. ret = seq_open(file, &show_ftrace_seq_ops);
  1246. if (!ret) {
  1247. struct seq_file *m = file->private_data;
  1248. m->private = iter;
  1249. } else {
  1250. kfree(iter);
  1251. }
  1252. return ret;
  1253. }
  1254. static int
  1255. ftrace_failures_open(struct inode *inode, struct file *file)
  1256. {
  1257. int ret;
  1258. struct seq_file *m;
  1259. struct ftrace_iterator *iter;
  1260. ret = ftrace_avail_open(inode, file);
  1261. if (!ret) {
  1262. m = (struct seq_file *)file->private_data;
  1263. iter = (struct ftrace_iterator *)m->private;
  1264. iter->flags = FTRACE_ITER_FAILURES;
  1265. }
  1266. return ret;
  1267. }
  1268. static void ftrace_filter_reset(int enable)
  1269. {
  1270. struct ftrace_page *pg;
  1271. struct dyn_ftrace *rec;
  1272. unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1273. mutex_lock(&ftrace_lock);
  1274. if (enable)
  1275. ftrace_filtered = 0;
  1276. do_for_each_ftrace_rec(pg, rec) {
  1277. if (rec->flags & FTRACE_FL_FAILED)
  1278. continue;
  1279. rec->flags &= ~type;
  1280. } while_for_each_ftrace_rec();
  1281. mutex_unlock(&ftrace_lock);
  1282. }
  1283. static int
  1284. ftrace_regex_open(struct inode *inode, struct file *file, int enable)
  1285. {
  1286. struct ftrace_iterator *iter;
  1287. int ret = 0;
  1288. if (unlikely(ftrace_disabled))
  1289. return -ENODEV;
  1290. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1291. if (!iter)
  1292. return -ENOMEM;
  1293. if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
  1294. kfree(iter);
  1295. return -ENOMEM;
  1296. }
  1297. mutex_lock(&ftrace_regex_lock);
  1298. if ((file->f_mode & FMODE_WRITE) &&
  1299. (file->f_flags & O_TRUNC))
  1300. ftrace_filter_reset(enable);
  1301. if (file->f_mode & FMODE_READ) {
  1302. iter->pg = ftrace_pages_start;
  1303. iter->flags = enable ? FTRACE_ITER_FILTER :
  1304. FTRACE_ITER_NOTRACE;
  1305. ret = seq_open(file, &show_ftrace_seq_ops);
  1306. if (!ret) {
  1307. struct seq_file *m = file->private_data;
  1308. m->private = iter;
  1309. } else {
  1310. trace_parser_put(&iter->parser);
  1311. kfree(iter);
  1312. }
  1313. } else
  1314. file->private_data = iter;
  1315. mutex_unlock(&ftrace_regex_lock);
  1316. return ret;
  1317. }
  1318. static int
  1319. ftrace_filter_open(struct inode *inode, struct file *file)
  1320. {
  1321. return ftrace_regex_open(inode, file, 1);
  1322. }
  1323. static int
  1324. ftrace_notrace_open(struct inode *inode, struct file *file)
  1325. {
  1326. return ftrace_regex_open(inode, file, 0);
  1327. }
  1328. static loff_t
  1329. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  1330. {
  1331. loff_t ret;
  1332. if (file->f_mode & FMODE_READ)
  1333. ret = seq_lseek(file, offset, origin);
  1334. else
  1335. file->f_pos = ret = 1;
  1336. return ret;
  1337. }
  1338. enum {
  1339. MATCH_FULL,
  1340. MATCH_FRONT_ONLY,
  1341. MATCH_MIDDLE_ONLY,
  1342. MATCH_END_ONLY,
  1343. };
  1344. /*
  1345. * (static function - no need for kernel doc)
  1346. *
  1347. * Pass in a buffer containing a glob and this function will
  1348. * set search to point to the search part of the buffer and
  1349. * return the type of search it is (see enum above).
  1350. * This does modify buff.
  1351. *
  1352. * Returns enum type.
  1353. * search returns the pointer to use for comparison.
  1354. * not returns 1 if buff started with a '!'
  1355. * 0 otherwise.
  1356. */
  1357. static int
  1358. ftrace_setup_glob(char *buff, int len, char **search, int *not)
  1359. {
  1360. int type = MATCH_FULL;
  1361. int i;
  1362. if (buff[0] == '!') {
  1363. *not = 1;
  1364. buff++;
  1365. len--;
  1366. } else
  1367. *not = 0;
  1368. *search = buff;
  1369. for (i = 0; i < len; i++) {
  1370. if (buff[i] == '*') {
  1371. if (!i) {
  1372. *search = buff + 1;
  1373. type = MATCH_END_ONLY;
  1374. } else {
  1375. if (type == MATCH_END_ONLY)
  1376. type = MATCH_MIDDLE_ONLY;
  1377. else
  1378. type = MATCH_FRONT_ONLY;
  1379. buff[i] = 0;
  1380. break;
  1381. }
  1382. }
  1383. }
  1384. return type;
  1385. }
  1386. static int ftrace_match(char *str, char *regex, int len, int type)
  1387. {
  1388. int matched = 0;
  1389. char *ptr;
  1390. switch (type) {
  1391. case MATCH_FULL:
  1392. if (strcmp(str, regex) == 0)
  1393. matched = 1;
  1394. break;
  1395. case MATCH_FRONT_ONLY:
  1396. if (strncmp(str, regex, len) == 0)
  1397. matched = 1;
  1398. break;
  1399. case MATCH_MIDDLE_ONLY:
  1400. if (strstr(str, regex))
  1401. matched = 1;
  1402. break;
  1403. case MATCH_END_ONLY:
  1404. ptr = strstr(str, regex);
  1405. if (ptr && (ptr[len] == 0))
  1406. matched = 1;
  1407. break;
  1408. }
  1409. return matched;
  1410. }
  1411. static int
  1412. ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
  1413. {
  1414. char str[KSYM_SYMBOL_LEN];
  1415. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  1416. return ftrace_match(str, regex, len, type);
  1417. }
  1418. static void ftrace_match_records(char *buff, int len, int enable)
  1419. {
  1420. unsigned int search_len;
  1421. struct ftrace_page *pg;
  1422. struct dyn_ftrace *rec;
  1423. unsigned long flag;
  1424. char *search;
  1425. int type;
  1426. int not;
  1427. flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1428. type = ftrace_setup_glob(buff, len, &search, &not);
  1429. search_len = strlen(search);
  1430. mutex_lock(&ftrace_lock);
  1431. do_for_each_ftrace_rec(pg, rec) {
  1432. if (rec->flags & FTRACE_FL_FAILED)
  1433. continue;
  1434. if (ftrace_match_record(rec, search, search_len, type)) {
  1435. if (not)
  1436. rec->flags &= ~flag;
  1437. else
  1438. rec->flags |= flag;
  1439. }
  1440. /*
  1441. * Only enable filtering if we have a function that
  1442. * is filtered on.
  1443. */
  1444. if (enable && (rec->flags & FTRACE_FL_FILTER))
  1445. ftrace_filtered = 1;
  1446. } while_for_each_ftrace_rec();
  1447. mutex_unlock(&ftrace_lock);
  1448. }
  1449. static int
  1450. ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
  1451. char *regex, int len, int type)
  1452. {
  1453. char str[KSYM_SYMBOL_LEN];
  1454. char *modname;
  1455. kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
  1456. if (!modname || strcmp(modname, mod))
  1457. return 0;
  1458. /* blank search means to match all funcs in the mod */
  1459. if (len)
  1460. return ftrace_match(str, regex, len, type);
  1461. else
  1462. return 1;
  1463. }
  1464. static void ftrace_match_module_records(char *buff, char *mod, int enable)
  1465. {
  1466. unsigned search_len = 0;
  1467. struct ftrace_page *pg;
  1468. struct dyn_ftrace *rec;
  1469. int type = MATCH_FULL;
  1470. char *search = buff;
  1471. unsigned long flag;
  1472. int not = 0;
  1473. flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1474. /* blank or '*' mean the same */
  1475. if (strcmp(buff, "*") == 0)
  1476. buff[0] = 0;
  1477. /* handle the case of 'dont filter this module' */
  1478. if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
  1479. buff[0] = 0;
  1480. not = 1;
  1481. }
  1482. if (strlen(buff)) {
  1483. type = ftrace_setup_glob(buff, strlen(buff), &search, &not);
  1484. search_len = strlen(search);
  1485. }
  1486. mutex_lock(&ftrace_lock);
  1487. do_for_each_ftrace_rec(pg, rec) {
  1488. if (rec->flags & FTRACE_FL_FAILED)
  1489. continue;
  1490. if (ftrace_match_module_record(rec, mod,
  1491. search, search_len, type)) {
  1492. if (not)
  1493. rec->flags &= ~flag;
  1494. else
  1495. rec->flags |= flag;
  1496. }
  1497. if (enable && (rec->flags & FTRACE_FL_FILTER))
  1498. ftrace_filtered = 1;
  1499. } while_for_each_ftrace_rec();
  1500. mutex_unlock(&ftrace_lock);
  1501. }
  1502. /*
  1503. * We register the module command as a template to show others how
  1504. * to register the a command as well.
  1505. */
  1506. static int
  1507. ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
  1508. {
  1509. char *mod;
  1510. /*
  1511. * cmd == 'mod' because we only registered this func
  1512. * for the 'mod' ftrace_func_command.
  1513. * But if you register one func with multiple commands,
  1514. * you can tell which command was used by the cmd
  1515. * parameter.
  1516. */
  1517. /* we must have a module name */
  1518. if (!param)
  1519. return -EINVAL;
  1520. mod = strsep(&param, ":");
  1521. if (!strlen(mod))
  1522. return -EINVAL;
  1523. ftrace_match_module_records(func, mod, enable);
  1524. return 0;
  1525. }
  1526. static struct ftrace_func_command ftrace_mod_cmd = {
  1527. .name = "mod",
  1528. .func = ftrace_mod_callback,
  1529. };
  1530. static int __init ftrace_mod_cmd_init(void)
  1531. {
  1532. return register_ftrace_command(&ftrace_mod_cmd);
  1533. }
  1534. device_initcall(ftrace_mod_cmd_init);
  1535. static void
  1536. function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
  1537. {
  1538. struct ftrace_func_probe *entry;
  1539. struct hlist_head *hhd;
  1540. struct hlist_node *n;
  1541. unsigned long key;
  1542. int resched;
  1543. key = hash_long(ip, FTRACE_HASH_BITS);
  1544. hhd = &ftrace_func_hash[key];
  1545. if (hlist_empty(hhd))
  1546. return;
  1547. /*
  1548. * Disable preemption for these calls to prevent a RCU grace
  1549. * period. This syncs the hash iteration and freeing of items
  1550. * on the hash. rcu_read_lock is too dangerous here.
  1551. */
  1552. resched = ftrace_preempt_disable();
  1553. hlist_for_each_entry_rcu(entry, n, hhd, node) {
  1554. if (entry->ip == ip)
  1555. entry->ops->func(ip, parent_ip, &entry->data);
  1556. }
  1557. ftrace_preempt_enable(resched);
  1558. }
  1559. static struct ftrace_ops trace_probe_ops __read_mostly =
  1560. {
  1561. .func = function_trace_probe_call,
  1562. };
  1563. static int ftrace_probe_registered;
  1564. static void __enable_ftrace_function_probe(void)
  1565. {
  1566. int i;
  1567. if (ftrace_probe_registered)
  1568. return;
  1569. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1570. struct hlist_head *hhd = &ftrace_func_hash[i];
  1571. if (hhd->first)
  1572. break;
  1573. }
  1574. /* Nothing registered? */
  1575. if (i == FTRACE_FUNC_HASHSIZE)
  1576. return;
  1577. __register_ftrace_function(&trace_probe_ops);
  1578. ftrace_startup(0);
  1579. ftrace_probe_registered = 1;
  1580. }
  1581. static void __disable_ftrace_function_probe(void)
  1582. {
  1583. int i;
  1584. if (!ftrace_probe_registered)
  1585. return;
  1586. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1587. struct hlist_head *hhd = &ftrace_func_hash[i];
  1588. if (hhd->first)
  1589. return;
  1590. }
  1591. /* no more funcs left */
  1592. __unregister_ftrace_function(&trace_probe_ops);
  1593. ftrace_shutdown(0);
  1594. ftrace_probe_registered = 0;
  1595. }
  1596. static void ftrace_free_entry_rcu(struct rcu_head *rhp)
  1597. {
  1598. struct ftrace_func_probe *entry =
  1599. container_of(rhp, struct ftrace_func_probe, rcu);
  1600. if (entry->ops->free)
  1601. entry->ops->free(&entry->data);
  1602. kfree(entry);
  1603. }
  1604. int
  1605. register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1606. void *data)
  1607. {
  1608. struct ftrace_func_probe *entry;
  1609. struct ftrace_page *pg;
  1610. struct dyn_ftrace *rec;
  1611. int type, len, not;
  1612. unsigned long key;
  1613. int count = 0;
  1614. char *search;
  1615. type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
  1616. len = strlen(search);
  1617. /* we do not support '!' for function probes */
  1618. if (WARN_ON(not))
  1619. return -EINVAL;
  1620. mutex_lock(&ftrace_lock);
  1621. do_for_each_ftrace_rec(pg, rec) {
  1622. if (rec->flags & FTRACE_FL_FAILED)
  1623. continue;
  1624. if (!ftrace_match_record(rec, search, len, type))
  1625. continue;
  1626. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  1627. if (!entry) {
  1628. /* If we did not process any, then return error */
  1629. if (!count)
  1630. count = -ENOMEM;
  1631. goto out_unlock;
  1632. }
  1633. count++;
  1634. entry->data = data;
  1635. /*
  1636. * The caller might want to do something special
  1637. * for each function we find. We call the callback
  1638. * to give the caller an opportunity to do so.
  1639. */
  1640. if (ops->callback) {
  1641. if (ops->callback(rec->ip, &entry->data) < 0) {
  1642. /* caller does not like this func */
  1643. kfree(entry);
  1644. continue;
  1645. }
  1646. }
  1647. entry->ops = ops;
  1648. entry->ip = rec->ip;
  1649. key = hash_long(entry->ip, FTRACE_HASH_BITS);
  1650. hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
  1651. } while_for_each_ftrace_rec();
  1652. __enable_ftrace_function_probe();
  1653. out_unlock:
  1654. mutex_unlock(&ftrace_lock);
  1655. return count;
  1656. }
  1657. enum {
  1658. PROBE_TEST_FUNC = 1,
  1659. PROBE_TEST_DATA = 2
  1660. };
  1661. static void
  1662. __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1663. void *data, int flags)
  1664. {
  1665. struct ftrace_func_probe *entry;
  1666. struct hlist_node *n, *tmp;
  1667. char str[KSYM_SYMBOL_LEN];
  1668. int type = MATCH_FULL;
  1669. int i, len = 0;
  1670. char *search;
  1671. if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
  1672. glob = NULL;
  1673. else if (glob) {
  1674. int not;
  1675. type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
  1676. len = strlen(search);
  1677. /* we do not support '!' for function probes */
  1678. if (WARN_ON(not))
  1679. return;
  1680. }
  1681. mutex_lock(&ftrace_lock);
  1682. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1683. struct hlist_head *hhd = &ftrace_func_hash[i];
  1684. hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
  1685. /* break up if statements for readability */
  1686. if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
  1687. continue;
  1688. if ((flags & PROBE_TEST_DATA) && entry->data != data)
  1689. continue;
  1690. /* do this last, since it is the most expensive */
  1691. if (glob) {
  1692. kallsyms_lookup(entry->ip, NULL, NULL,
  1693. NULL, str);
  1694. if (!ftrace_match(str, glob, len, type))
  1695. continue;
  1696. }
  1697. hlist_del(&entry->node);
  1698. call_rcu(&entry->rcu, ftrace_free_entry_rcu);
  1699. }
  1700. }
  1701. __disable_ftrace_function_probe();
  1702. mutex_unlock(&ftrace_lock);
  1703. }
  1704. void
  1705. unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1706. void *data)
  1707. {
  1708. __unregister_ftrace_function_probe(glob, ops, data,
  1709. PROBE_TEST_FUNC | PROBE_TEST_DATA);
  1710. }
  1711. void
  1712. unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
  1713. {
  1714. __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
  1715. }
  1716. void unregister_ftrace_function_probe_all(char *glob)
  1717. {
  1718. __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
  1719. }
  1720. static LIST_HEAD(ftrace_commands);
  1721. static DEFINE_MUTEX(ftrace_cmd_mutex);
  1722. int register_ftrace_command(struct ftrace_func_command *cmd)
  1723. {
  1724. struct ftrace_func_command *p;
  1725. int ret = 0;
  1726. mutex_lock(&ftrace_cmd_mutex);
  1727. list_for_each_entry(p, &ftrace_commands, list) {
  1728. if (strcmp(cmd->name, p->name) == 0) {
  1729. ret = -EBUSY;
  1730. goto out_unlock;
  1731. }
  1732. }
  1733. list_add(&cmd->list, &ftrace_commands);
  1734. out_unlock:
  1735. mutex_unlock(&ftrace_cmd_mutex);
  1736. return ret;
  1737. }
  1738. int unregister_ftrace_command(struct ftrace_func_command *cmd)
  1739. {
  1740. struct ftrace_func_command *p, *n;
  1741. int ret = -ENODEV;
  1742. mutex_lock(&ftrace_cmd_mutex);
  1743. list_for_each_entry_safe(p, n, &ftrace_commands, list) {
  1744. if (strcmp(cmd->name, p->name) == 0) {
  1745. ret = 0;
  1746. list_del_init(&p->list);
  1747. goto out_unlock;
  1748. }
  1749. }
  1750. out_unlock:
  1751. mutex_unlock(&ftrace_cmd_mutex);
  1752. return ret;
  1753. }
  1754. static int ftrace_process_regex(char *buff, int len, int enable)
  1755. {
  1756. char *func, *command, *next = buff;
  1757. struct ftrace_func_command *p;
  1758. int ret = -EINVAL;
  1759. func = strsep(&next, ":");
  1760. if (!next) {
  1761. ftrace_match_records(func, len, enable);
  1762. return 0;
  1763. }
  1764. /* command found */
  1765. command = strsep(&next, ":");
  1766. mutex_lock(&ftrace_cmd_mutex);
  1767. list_for_each_entry(p, &ftrace_commands, list) {
  1768. if (strcmp(p->name, command) == 0) {
  1769. ret = p->func(func, command, next, enable);
  1770. goto out_unlock;
  1771. }
  1772. }
  1773. out_unlock:
  1774. mutex_unlock(&ftrace_cmd_mutex);
  1775. return ret;
  1776. }
  1777. static ssize_t
  1778. ftrace_regex_write(struct file *file, const char __user *ubuf,
  1779. size_t cnt, loff_t *ppos, int enable)
  1780. {
  1781. struct ftrace_iterator *iter;
  1782. struct trace_parser *parser;
  1783. ssize_t ret, read;
  1784. if (!cnt)
  1785. return 0;
  1786. mutex_lock(&ftrace_regex_lock);
  1787. if (file->f_mode & FMODE_READ) {
  1788. struct seq_file *m = file->private_data;
  1789. iter = m->private;
  1790. } else
  1791. iter = file->private_data;
  1792. parser = &iter->parser;
  1793. read = trace_get_user(parser, ubuf, cnt, ppos);
  1794. if (read >= 0 && trace_parser_loaded(parser) &&
  1795. !trace_parser_cont(parser)) {
  1796. ret = ftrace_process_regex(parser->buffer,
  1797. parser->idx, enable);
  1798. if (ret)
  1799. goto out;
  1800. trace_parser_clear(parser);
  1801. }
  1802. ret = read;
  1803. mutex_unlock(&ftrace_regex_lock);
  1804. out:
  1805. return ret;
  1806. }
  1807. static ssize_t
  1808. ftrace_filter_write(struct file *file, const char __user *ubuf,
  1809. size_t cnt, loff_t *ppos)
  1810. {
  1811. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  1812. }
  1813. static ssize_t
  1814. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  1815. size_t cnt, loff_t *ppos)
  1816. {
  1817. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  1818. }
  1819. static void
  1820. ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
  1821. {
  1822. if (unlikely(ftrace_disabled))
  1823. return;
  1824. mutex_lock(&ftrace_regex_lock);
  1825. if (reset)
  1826. ftrace_filter_reset(enable);
  1827. if (buf)
  1828. ftrace_match_records(buf, len, enable);
  1829. mutex_unlock(&ftrace_regex_lock);
  1830. }
  1831. /**
  1832. * ftrace_set_filter - set a function to filter on in ftrace
  1833. * @buf - the string that holds the function filter text.
  1834. * @len - the length of the string.
  1835. * @reset - non zero to reset all filters before applying this filter.
  1836. *
  1837. * Filters denote which functions should be enabled when tracing is enabled.
  1838. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  1839. */
  1840. void ftrace_set_filter(unsigned char *buf, int len, int reset)
  1841. {
  1842. ftrace_set_regex(buf, len, reset, 1);
  1843. }
  1844. /**
  1845. * ftrace_set_notrace - set a function to not trace in ftrace
  1846. * @buf - the string that holds the function notrace text.
  1847. * @len - the length of the string.
  1848. * @reset - non zero to reset all filters before applying this filter.
  1849. *
  1850. * Notrace Filters denote which functions should not be enabled when tracing
  1851. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  1852. * for tracing.
  1853. */
  1854. void ftrace_set_notrace(unsigned char *buf, int len, int reset)
  1855. {
  1856. ftrace_set_regex(buf, len, reset, 0);
  1857. }
  1858. /*
  1859. * command line interface to allow users to set filters on boot up.
  1860. */
  1861. #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
  1862. static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
  1863. static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
  1864. static int __init set_ftrace_notrace(char *str)
  1865. {
  1866. strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
  1867. return 1;
  1868. }
  1869. __setup("ftrace_notrace=", set_ftrace_notrace);
  1870. static int __init set_ftrace_filter(char *str)
  1871. {
  1872. strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
  1873. return 1;
  1874. }
  1875. __setup("ftrace_filter=", set_ftrace_filter);
  1876. static void __init set_ftrace_early_filter(char *buf, int enable)
  1877. {
  1878. char *func;
  1879. while (buf) {
  1880. func = strsep(&buf, ",");
  1881. ftrace_set_regex(func, strlen(func), 0, enable);
  1882. }
  1883. }
  1884. static void __init set_ftrace_early_filters(void)
  1885. {
  1886. if (ftrace_filter_buf[0])
  1887. set_ftrace_early_filter(ftrace_filter_buf, 1);
  1888. if (ftrace_notrace_buf[0])
  1889. set_ftrace_early_filter(ftrace_notrace_buf, 0);
  1890. }
  1891. static int
  1892. ftrace_regex_release(struct inode *inode, struct file *file, int enable)
  1893. {
  1894. struct seq_file *m = (struct seq_file *)file->private_data;
  1895. struct ftrace_iterator *iter;
  1896. struct trace_parser *parser;
  1897. mutex_lock(&ftrace_regex_lock);
  1898. if (file->f_mode & FMODE_READ) {
  1899. iter = m->private;
  1900. seq_release(inode, file);
  1901. } else
  1902. iter = file->private_data;
  1903. parser = &iter->parser;
  1904. if (trace_parser_loaded(parser)) {
  1905. parser->buffer[parser->idx] = 0;
  1906. ftrace_match_records(parser->buffer, parser->idx, enable);
  1907. }
  1908. mutex_lock(&ftrace_lock);
  1909. if (ftrace_start_up && ftrace_enabled)
  1910. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  1911. mutex_unlock(&ftrace_lock);
  1912. trace_parser_put(parser);
  1913. kfree(iter);
  1914. mutex_unlock(&ftrace_regex_lock);
  1915. return 0;
  1916. }
  1917. static int
  1918. ftrace_filter_release(struct inode *inode, struct file *file)
  1919. {
  1920. return ftrace_regex_release(inode, file, 1);
  1921. }
  1922. static int
  1923. ftrace_notrace_release(struct inode *inode, struct file *file)
  1924. {
  1925. return ftrace_regex_release(inode, file, 0);
  1926. }
  1927. static const struct file_operations ftrace_avail_fops = {
  1928. .open = ftrace_avail_open,
  1929. .read = seq_read,
  1930. .llseek = seq_lseek,
  1931. .release = seq_release_private,
  1932. };
  1933. static const struct file_operations ftrace_failures_fops = {
  1934. .open = ftrace_failures_open,
  1935. .read = seq_read,
  1936. .llseek = seq_lseek,
  1937. .release = seq_release_private,
  1938. };
  1939. static const struct file_operations ftrace_filter_fops = {
  1940. .open = ftrace_filter_open,
  1941. .read = seq_read,
  1942. .write = ftrace_filter_write,
  1943. .llseek = ftrace_regex_lseek,
  1944. .release = ftrace_filter_release,
  1945. };
  1946. static const struct file_operations ftrace_notrace_fops = {
  1947. .open = ftrace_notrace_open,
  1948. .read = seq_read,
  1949. .write = ftrace_notrace_write,
  1950. .llseek = ftrace_regex_lseek,
  1951. .release = ftrace_notrace_release,
  1952. };
  1953. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1954. static DEFINE_MUTEX(graph_lock);
  1955. int ftrace_graph_count;
  1956. unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
  1957. static void *
  1958. __g_next(struct seq_file *m, loff_t *pos)
  1959. {
  1960. if (*pos >= ftrace_graph_count)
  1961. return NULL;
  1962. return &ftrace_graph_funcs[*pos];
  1963. }
  1964. static void *
  1965. g_next(struct seq_file *m, void *v, loff_t *pos)
  1966. {
  1967. (*pos)++;
  1968. return __g_next(m, pos);
  1969. }
  1970. static void *g_start(struct seq_file *m, loff_t *pos)
  1971. {
  1972. mutex_lock(&graph_lock);
  1973. /* Nothing, tell g_show to print all functions are enabled */
  1974. if (!ftrace_graph_count && !*pos)
  1975. return (void *)1;
  1976. return __g_next(m, pos);
  1977. }
  1978. static void g_stop(struct seq_file *m, void *p)
  1979. {
  1980. mutex_unlock(&graph_lock);
  1981. }
  1982. static int g_show(struct seq_file *m, void *v)
  1983. {
  1984. unsigned long *ptr = v;
  1985. if (!ptr)
  1986. return 0;
  1987. if (ptr == (unsigned long *)1) {
  1988. seq_printf(m, "#### all functions enabled ####\n");
  1989. return 0;
  1990. }
  1991. seq_printf(m, "%ps\n", (void *)*ptr);
  1992. return 0;
  1993. }
  1994. static const struct seq_operations ftrace_graph_seq_ops = {
  1995. .start = g_start,
  1996. .next = g_next,
  1997. .stop = g_stop,
  1998. .show = g_show,
  1999. };
  2000. static int
  2001. ftrace_graph_open(struct inode *inode, struct file *file)
  2002. {
  2003. int ret = 0;
  2004. if (unlikely(ftrace_disabled))
  2005. return -ENODEV;
  2006. mutex_lock(&graph_lock);
  2007. if ((file->f_mode & FMODE_WRITE) &&
  2008. (file->f_flags & O_TRUNC)) {
  2009. ftrace_graph_count = 0;
  2010. memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
  2011. }
  2012. mutex_unlock(&graph_lock);
  2013. if (file->f_mode & FMODE_READ)
  2014. ret = seq_open(file, &ftrace_graph_seq_ops);
  2015. return ret;
  2016. }
  2017. static int
  2018. ftrace_graph_release(struct inode *inode, struct file *file)
  2019. {
  2020. if (file->f_mode & FMODE_READ)
  2021. seq_release(inode, file);
  2022. return 0;
  2023. }
  2024. static int
  2025. ftrace_set_func(unsigned long *array, int *idx, char *buffer)
  2026. {
  2027. struct dyn_ftrace *rec;
  2028. struct ftrace_page *pg;
  2029. int search_len;
  2030. int found = 0;
  2031. int type, not;
  2032. char *search;
  2033. bool exists;
  2034. int i;
  2035. if (ftrace_disabled)
  2036. return -ENODEV;
  2037. /* decode regex */
  2038. type = ftrace_setup_glob(buffer, strlen(buffer), &search, &not);
  2039. if (not)
  2040. return -EINVAL;
  2041. search_len = strlen(search);
  2042. mutex_lock(&ftrace_lock);
  2043. do_for_each_ftrace_rec(pg, rec) {
  2044. if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
  2045. break;
  2046. if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
  2047. continue;
  2048. if (ftrace_match_record(rec, search, search_len, type)) {
  2049. /* ensure it is not already in the array */
  2050. exists = false;
  2051. for (i = 0; i < *idx; i++)
  2052. if (array[i] == rec->ip) {
  2053. exists = true;
  2054. break;
  2055. }
  2056. if (!exists) {
  2057. array[(*idx)++] = rec->ip;
  2058. found = 1;
  2059. }
  2060. }
  2061. } while_for_each_ftrace_rec();
  2062. mutex_unlock(&ftrace_lock);
  2063. return found ? 0 : -EINVAL;
  2064. }
  2065. static ssize_t
  2066. ftrace_graph_write(struct file *file, const char __user *ubuf,
  2067. size_t cnt, loff_t *ppos)
  2068. {
  2069. struct trace_parser parser;
  2070. ssize_t read, ret;
  2071. if (!cnt || cnt < 0)
  2072. return 0;
  2073. mutex_lock(&graph_lock);
  2074. if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
  2075. ret = -EBUSY;
  2076. goto out_unlock;
  2077. }
  2078. if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
  2079. ret = -ENOMEM;
  2080. goto out_unlock;
  2081. }
  2082. read = trace_get_user(&parser, ubuf, cnt, ppos);
  2083. if (read >= 0 && trace_parser_loaded((&parser))) {
  2084. parser.buffer[parser.idx] = 0;
  2085. /* we allow only one expression at a time */
  2086. ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
  2087. parser.buffer);
  2088. if (ret)
  2089. goto out_free;
  2090. }
  2091. ret = read;
  2092. out_free:
  2093. trace_parser_put(&parser);
  2094. out_unlock:
  2095. mutex_unlock(&graph_lock);
  2096. return ret;
  2097. }
  2098. static const struct file_operations ftrace_graph_fops = {
  2099. .open = ftrace_graph_open,
  2100. .read = seq_read,
  2101. .write = ftrace_graph_write,
  2102. .release = ftrace_graph_release,
  2103. };
  2104. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2105. static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
  2106. {
  2107. trace_create_file("available_filter_functions", 0444,
  2108. d_tracer, NULL, &ftrace_avail_fops);
  2109. trace_create_file("failures", 0444,
  2110. d_tracer, NULL, &ftrace_failures_fops);
  2111. trace_create_file("set_ftrace_filter", 0644, d_tracer,
  2112. NULL, &ftrace_filter_fops);
  2113. trace_create_file("set_ftrace_notrace", 0644, d_tracer,
  2114. NULL, &ftrace_notrace_fops);
  2115. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2116. trace_create_file("set_graph_function", 0444, d_tracer,
  2117. NULL,
  2118. &ftrace_graph_fops);
  2119. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2120. return 0;
  2121. }
  2122. static int ftrace_convert_nops(struct module *mod,
  2123. unsigned long *start,
  2124. unsigned long *end)
  2125. {
  2126. unsigned long *p;
  2127. unsigned long addr;
  2128. unsigned long flags;
  2129. mutex_lock(&ftrace_lock);
  2130. p = start;
  2131. while (p < end) {
  2132. addr = ftrace_call_adjust(*p++);
  2133. /*
  2134. * Some architecture linkers will pad between
  2135. * the different mcount_loc sections of different
  2136. * object files to satisfy alignments.
  2137. * Skip any NULL pointers.
  2138. */
  2139. if (!addr)
  2140. continue;
  2141. ftrace_record_ip(addr);
  2142. }
  2143. /* disable interrupts to prevent kstop machine */
  2144. local_irq_save(flags);
  2145. ftrace_update_code(mod);
  2146. local_irq_restore(flags);
  2147. mutex_unlock(&ftrace_lock);
  2148. return 0;
  2149. }
  2150. #ifdef CONFIG_MODULES
  2151. void ftrace_release(void *start, void *end)
  2152. {
  2153. struct dyn_ftrace *rec;
  2154. struct ftrace_page *pg;
  2155. unsigned long s = (unsigned long)start;
  2156. unsigned long e = (unsigned long)end;
  2157. if (ftrace_disabled || !start || start == end)
  2158. return;
  2159. mutex_lock(&ftrace_lock);
  2160. do_for_each_ftrace_rec(pg, rec) {
  2161. if ((rec->ip >= s) && (rec->ip < e)) {
  2162. /*
  2163. * rec->ip is changed in ftrace_free_rec()
  2164. * It should not between s and e if record was freed.
  2165. */
  2166. FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
  2167. ftrace_free_rec(rec);
  2168. }
  2169. } while_for_each_ftrace_rec();
  2170. mutex_unlock(&ftrace_lock);
  2171. }
  2172. static void ftrace_init_module(struct module *mod,
  2173. unsigned long *start, unsigned long *end)
  2174. {
  2175. if (ftrace_disabled || start == end)
  2176. return;
  2177. ftrace_convert_nops(mod, start, end);
  2178. }
  2179. static int ftrace_module_notify(struct notifier_block *self,
  2180. unsigned long val, void *data)
  2181. {
  2182. struct module *mod = data;
  2183. switch (val) {
  2184. case MODULE_STATE_COMING:
  2185. ftrace_init_module(mod, mod->ftrace_callsites,
  2186. mod->ftrace_callsites +
  2187. mod->num_ftrace_callsites);
  2188. break;
  2189. case MODULE_STATE_GOING:
  2190. ftrace_release(mod->ftrace_callsites,
  2191. mod->ftrace_callsites +
  2192. mod->num_ftrace_callsites);
  2193. break;
  2194. }
  2195. return 0;
  2196. }
  2197. #else
  2198. static int ftrace_module_notify(struct notifier_block *self,
  2199. unsigned long val, void *data)
  2200. {
  2201. return 0;
  2202. }
  2203. #endif /* CONFIG_MODULES */
  2204. struct notifier_block ftrace_module_nb = {
  2205. .notifier_call = ftrace_module_notify,
  2206. .priority = 0,
  2207. };
  2208. extern unsigned long __start_mcount_loc[];
  2209. extern unsigned long __stop_mcount_loc[];
  2210. void __init ftrace_init(void)
  2211. {
  2212. unsigned long count, addr, flags;
  2213. int ret;
  2214. /* Keep the ftrace pointer to the stub */
  2215. addr = (unsigned long)ftrace_stub;
  2216. local_irq_save(flags);
  2217. ftrace_dyn_arch_init(&addr);
  2218. local_irq_restore(flags);
  2219. /* ftrace_dyn_arch_init places the return code in addr */
  2220. if (addr)
  2221. goto failed;
  2222. count = __stop_mcount_loc - __start_mcount_loc;
  2223. ret = ftrace_dyn_table_alloc(count);
  2224. if (ret)
  2225. goto failed;
  2226. last_ftrace_enabled = ftrace_enabled = 1;
  2227. ret = ftrace_convert_nops(NULL,
  2228. __start_mcount_loc,
  2229. __stop_mcount_loc);
  2230. ret = register_module_notifier(&ftrace_module_nb);
  2231. if (ret)
  2232. pr_warning("Failed to register trace ftrace module notifier\n");
  2233. set_ftrace_early_filters();
  2234. return;
  2235. failed:
  2236. ftrace_disabled = 1;
  2237. }
  2238. #else
  2239. static int __init ftrace_nodyn_init(void)
  2240. {
  2241. ftrace_enabled = 1;
  2242. return 0;
  2243. }
  2244. device_initcall(ftrace_nodyn_init);
  2245. static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
  2246. static inline void ftrace_startup_enable(int command) { }
  2247. /* Keep as macros so we do not need to define the commands */
  2248. # define ftrace_startup(command) do { } while (0)
  2249. # define ftrace_shutdown(command) do { } while (0)
  2250. # define ftrace_startup_sysctl() do { } while (0)
  2251. # define ftrace_shutdown_sysctl() do { } while (0)
  2252. #endif /* CONFIG_DYNAMIC_FTRACE */
  2253. static ssize_t
  2254. ftrace_pid_read(struct file *file, char __user *ubuf,
  2255. size_t cnt, loff_t *ppos)
  2256. {
  2257. char buf[64];
  2258. int r;
  2259. if (ftrace_pid_trace == ftrace_swapper_pid)
  2260. r = sprintf(buf, "swapper tasks\n");
  2261. else if (ftrace_pid_trace)
  2262. r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
  2263. else
  2264. r = sprintf(buf, "no pid\n");
  2265. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2266. }
  2267. static void clear_ftrace_swapper(void)
  2268. {
  2269. struct task_struct *p;
  2270. int cpu;
  2271. get_online_cpus();
  2272. for_each_online_cpu(cpu) {
  2273. p = idle_task(cpu);
  2274. clear_tsk_trace_trace(p);
  2275. }
  2276. put_online_cpus();
  2277. }
  2278. static void set_ftrace_swapper(void)
  2279. {
  2280. struct task_struct *p;
  2281. int cpu;
  2282. get_online_cpus();
  2283. for_each_online_cpu(cpu) {
  2284. p = idle_task(cpu);
  2285. set_tsk_trace_trace(p);
  2286. }
  2287. put_online_cpus();
  2288. }
  2289. static void clear_ftrace_pid(struct pid *pid)
  2290. {
  2291. struct task_struct *p;
  2292. rcu_read_lock();
  2293. do_each_pid_task(pid, PIDTYPE_PID, p) {
  2294. clear_tsk_trace_trace(p);
  2295. } while_each_pid_task(pid, PIDTYPE_PID, p);
  2296. rcu_read_unlock();
  2297. put_pid(pid);
  2298. }
  2299. static void set_ftrace_pid(struct pid *pid)
  2300. {
  2301. struct task_struct *p;
  2302. rcu_read_lock();
  2303. do_each_pid_task(pid, PIDTYPE_PID, p) {
  2304. set_tsk_trace_trace(p);
  2305. } while_each_pid_task(pid, PIDTYPE_PID, p);
  2306. rcu_read_unlock();
  2307. }
  2308. static void clear_ftrace_pid_task(struct pid **pid)
  2309. {
  2310. if (*pid == ftrace_swapper_pid)
  2311. clear_ftrace_swapper();
  2312. else
  2313. clear_ftrace_pid(*pid);
  2314. *pid = NULL;
  2315. }
  2316. static void set_ftrace_pid_task(struct pid *pid)
  2317. {
  2318. if (pid == ftrace_swapper_pid)
  2319. set_ftrace_swapper();
  2320. else
  2321. set_ftrace_pid(pid);
  2322. }
  2323. static ssize_t
  2324. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  2325. size_t cnt, loff_t *ppos)
  2326. {
  2327. struct pid *pid;
  2328. char buf[64];
  2329. long val;
  2330. int ret;
  2331. if (cnt >= sizeof(buf))
  2332. return -EINVAL;
  2333. if (copy_from_user(&buf, ubuf, cnt))
  2334. return -EFAULT;
  2335. buf[cnt] = 0;
  2336. ret = strict_strtol(buf, 10, &val);
  2337. if (ret < 0)
  2338. return ret;
  2339. mutex_lock(&ftrace_lock);
  2340. if (val < 0) {
  2341. /* disable pid tracing */
  2342. if (!ftrace_pid_trace)
  2343. goto out;
  2344. clear_ftrace_pid_task(&ftrace_pid_trace);
  2345. } else {
  2346. /* swapper task is special */
  2347. if (!val) {
  2348. pid = ftrace_swapper_pid;
  2349. if (pid == ftrace_pid_trace)
  2350. goto out;
  2351. } else {
  2352. pid = find_get_pid(val);
  2353. if (pid == ftrace_pid_trace) {
  2354. put_pid(pid);
  2355. goto out;
  2356. }
  2357. }
  2358. if (ftrace_pid_trace)
  2359. clear_ftrace_pid_task(&ftrace_pid_trace);
  2360. if (!pid)
  2361. goto out;
  2362. ftrace_pid_trace = pid;
  2363. set_ftrace_pid_task(ftrace_pid_trace);
  2364. }
  2365. /* update the function call */
  2366. ftrace_update_pid_func();
  2367. ftrace_startup_enable(0);
  2368. out:
  2369. mutex_unlock(&ftrace_lock);
  2370. return cnt;
  2371. }
  2372. static const struct file_operations ftrace_pid_fops = {
  2373. .read = ftrace_pid_read,
  2374. .write = ftrace_pid_write,
  2375. };
  2376. static __init int ftrace_init_debugfs(void)
  2377. {
  2378. struct dentry *d_tracer;
  2379. d_tracer = tracing_init_dentry();
  2380. if (!d_tracer)
  2381. return 0;
  2382. ftrace_init_dyn_debugfs(d_tracer);
  2383. trace_create_file("set_ftrace_pid", 0644, d_tracer,
  2384. NULL, &ftrace_pid_fops);
  2385. ftrace_profile_debugfs(d_tracer);
  2386. return 0;
  2387. }
  2388. fs_initcall(ftrace_init_debugfs);
  2389. /**
  2390. * ftrace_kill - kill ftrace
  2391. *
  2392. * This function should be used by panic code. It stops ftrace
  2393. * but in a not so nice way. If you need to simply kill ftrace
  2394. * from a non-atomic section, use ftrace_kill.
  2395. */
  2396. void ftrace_kill(void)
  2397. {
  2398. ftrace_disabled = 1;
  2399. ftrace_enabled = 0;
  2400. clear_ftrace_function();
  2401. }
  2402. /**
  2403. * register_ftrace_function - register a function for profiling
  2404. * @ops - ops structure that holds the function for profiling.
  2405. *
  2406. * Register a function to be called by all functions in the
  2407. * kernel.
  2408. *
  2409. * Note: @ops->func and all the functions it calls must be labeled
  2410. * with "notrace", otherwise it will go into a
  2411. * recursive loop.
  2412. */
  2413. int register_ftrace_function(struct ftrace_ops *ops)
  2414. {
  2415. int ret;
  2416. if (unlikely(ftrace_disabled))
  2417. return -1;
  2418. mutex_lock(&ftrace_lock);
  2419. ret = __register_ftrace_function(ops);
  2420. ftrace_startup(0);
  2421. mutex_unlock(&ftrace_lock);
  2422. return ret;
  2423. }
  2424. /**
  2425. * unregister_ftrace_function - unregister a function for profiling.
  2426. * @ops - ops structure that holds the function to unregister
  2427. *
  2428. * Unregister a function that was added to be called by ftrace profiling.
  2429. */
  2430. int unregister_ftrace_function(struct ftrace_ops *ops)
  2431. {
  2432. int ret;
  2433. mutex_lock(&ftrace_lock);
  2434. ret = __unregister_ftrace_function(ops);
  2435. ftrace_shutdown(0);
  2436. mutex_unlock(&ftrace_lock);
  2437. return ret;
  2438. }
  2439. int
  2440. ftrace_enable_sysctl(struct ctl_table *table, int write,
  2441. void __user *buffer, size_t *lenp,
  2442. loff_t *ppos)
  2443. {
  2444. int ret;
  2445. if (unlikely(ftrace_disabled))
  2446. return -ENODEV;
  2447. mutex_lock(&ftrace_lock);
  2448. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  2449. if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
  2450. goto out;
  2451. last_ftrace_enabled = !!ftrace_enabled;
  2452. if (ftrace_enabled) {
  2453. ftrace_startup_sysctl();
  2454. /* we are starting ftrace again */
  2455. if (ftrace_list != &ftrace_list_end) {
  2456. if (ftrace_list->next == &ftrace_list_end)
  2457. ftrace_trace_function = ftrace_list->func;
  2458. else
  2459. ftrace_trace_function = ftrace_list_func;
  2460. }
  2461. } else {
  2462. /* stopping ftrace calls (just send to ftrace_stub) */
  2463. ftrace_trace_function = ftrace_stub;
  2464. ftrace_shutdown_sysctl();
  2465. }
  2466. out:
  2467. mutex_unlock(&ftrace_lock);
  2468. return ret;
  2469. }
  2470. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2471. static int ftrace_graph_active;
  2472. static struct notifier_block ftrace_suspend_notifier;
  2473. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
  2474. {
  2475. return 0;
  2476. }
  2477. /* The callbacks that hook a function */
  2478. trace_func_graph_ret_t ftrace_graph_return =
  2479. (trace_func_graph_ret_t)ftrace_stub;
  2480. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  2481. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  2482. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  2483. {
  2484. int i;
  2485. int ret = 0;
  2486. unsigned long flags;
  2487. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  2488. struct task_struct *g, *t;
  2489. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  2490. ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
  2491. * sizeof(struct ftrace_ret_stack),
  2492. GFP_KERNEL);
  2493. if (!ret_stack_list[i]) {
  2494. start = 0;
  2495. end = i;
  2496. ret = -ENOMEM;
  2497. goto free;
  2498. }
  2499. }
  2500. read_lock_irqsave(&tasklist_lock, flags);
  2501. do_each_thread(g, t) {
  2502. if (start == end) {
  2503. ret = -EAGAIN;
  2504. goto unlock;
  2505. }
  2506. if (t->ret_stack == NULL) {
  2507. atomic_set(&t->tracing_graph_pause, 0);
  2508. atomic_set(&t->trace_overrun, 0);
  2509. t->curr_ret_stack = -1;
  2510. /* Make sure the tasks see the -1 first: */
  2511. smp_wmb();
  2512. t->ret_stack = ret_stack_list[start++];
  2513. }
  2514. } while_each_thread(g, t);
  2515. unlock:
  2516. read_unlock_irqrestore(&tasklist_lock, flags);
  2517. free:
  2518. for (i = start; i < end; i++)
  2519. kfree(ret_stack_list[i]);
  2520. return ret;
  2521. }
  2522. static void
  2523. ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
  2524. struct task_struct *next)
  2525. {
  2526. unsigned long long timestamp;
  2527. int index;
  2528. /*
  2529. * Does the user want to count the time a function was asleep.
  2530. * If so, do not update the time stamps.
  2531. */
  2532. if (trace_flags & TRACE_ITER_SLEEP_TIME)
  2533. return;
  2534. timestamp = trace_clock_local();
  2535. prev->ftrace_timestamp = timestamp;
  2536. /* only process tasks that we timestamped */
  2537. if (!next->ftrace_timestamp)
  2538. return;
  2539. /*
  2540. * Update all the counters in next to make up for the
  2541. * time next was sleeping.
  2542. */
  2543. timestamp -= next->ftrace_timestamp;
  2544. for (index = next->curr_ret_stack; index >= 0; index--)
  2545. next->ret_stack[index].calltime += timestamp;
  2546. }
  2547. /* Allocate a return stack for each task */
  2548. static int start_graph_tracing(void)
  2549. {
  2550. struct ftrace_ret_stack **ret_stack_list;
  2551. int ret, cpu;
  2552. ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
  2553. sizeof(struct ftrace_ret_stack *),
  2554. GFP_KERNEL);
  2555. if (!ret_stack_list)
  2556. return -ENOMEM;
  2557. /* The cpu_boot init_task->ret_stack will never be freed */
  2558. for_each_online_cpu(cpu) {
  2559. if (!idle_task(cpu)->ret_stack)
  2560. ftrace_graph_init_task(idle_task(cpu));
  2561. }
  2562. do {
  2563. ret = alloc_retstack_tasklist(ret_stack_list);
  2564. } while (ret == -EAGAIN);
  2565. if (!ret) {
  2566. ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
  2567. if (ret)
  2568. pr_info("ftrace_graph: Couldn't activate tracepoint"
  2569. " probe to kernel_sched_switch\n");
  2570. }
  2571. kfree(ret_stack_list);
  2572. return ret;
  2573. }
  2574. /*
  2575. * Hibernation protection.
  2576. * The state of the current task is too much unstable during
  2577. * suspend/restore to disk. We want to protect against that.
  2578. */
  2579. static int
  2580. ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
  2581. void *unused)
  2582. {
  2583. switch (state) {
  2584. case PM_HIBERNATION_PREPARE:
  2585. pause_graph_tracing();
  2586. break;
  2587. case PM_POST_HIBERNATION:
  2588. unpause_graph_tracing();
  2589. break;
  2590. }
  2591. return NOTIFY_DONE;
  2592. }
  2593. int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  2594. trace_func_graph_ent_t entryfunc)
  2595. {
  2596. int ret = 0;
  2597. mutex_lock(&ftrace_lock);
  2598. /* we currently allow only one tracer registered at a time */
  2599. if (ftrace_graph_active) {
  2600. ret = -EBUSY;
  2601. goto out;
  2602. }
  2603. ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
  2604. register_pm_notifier(&ftrace_suspend_notifier);
  2605. ftrace_graph_active++;
  2606. ret = start_graph_tracing();
  2607. if (ret) {
  2608. ftrace_graph_active--;
  2609. goto out;
  2610. }
  2611. ftrace_graph_return = retfunc;
  2612. ftrace_graph_entry = entryfunc;
  2613. ftrace_startup(FTRACE_START_FUNC_RET);
  2614. out:
  2615. mutex_unlock(&ftrace_lock);
  2616. return ret;
  2617. }
  2618. void unregister_ftrace_graph(void)
  2619. {
  2620. mutex_lock(&ftrace_lock);
  2621. if (unlikely(!ftrace_graph_active))
  2622. goto out;
  2623. ftrace_graph_active--;
  2624. unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
  2625. ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
  2626. ftrace_graph_entry = ftrace_graph_entry_stub;
  2627. ftrace_shutdown(FTRACE_STOP_FUNC_RET);
  2628. unregister_pm_notifier(&ftrace_suspend_notifier);
  2629. out:
  2630. mutex_unlock(&ftrace_lock);
  2631. }
  2632. /* Allocate a return stack for newly created task */
  2633. void ftrace_graph_init_task(struct task_struct *t)
  2634. {
  2635. /* Make sure we do not use the parent ret_stack */
  2636. t->ret_stack = NULL;
  2637. if (ftrace_graph_active) {
  2638. struct ftrace_ret_stack *ret_stack;
  2639. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  2640. * sizeof(struct ftrace_ret_stack),
  2641. GFP_KERNEL);
  2642. if (!ret_stack)
  2643. return;
  2644. t->curr_ret_stack = -1;
  2645. atomic_set(&t->tracing_graph_pause, 0);
  2646. atomic_set(&t->trace_overrun, 0);
  2647. t->ftrace_timestamp = 0;
  2648. /* make curr_ret_stack visable before we add the ret_stack */
  2649. smp_wmb();
  2650. t->ret_stack = ret_stack;
  2651. }
  2652. }
  2653. void ftrace_graph_exit_task(struct task_struct *t)
  2654. {
  2655. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  2656. t->ret_stack = NULL;
  2657. /* NULL must become visible to IRQs before we free it: */
  2658. barrier();
  2659. kfree(ret_stack);
  2660. }
  2661. void ftrace_graph_stop(void)
  2662. {
  2663. ftrace_stop();
  2664. }
  2665. #endif