ftrace.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298
  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. kfree(iter);
  1311. } else
  1312. file->private_data = iter;
  1313. mutex_unlock(&ftrace_regex_lock);
  1314. return ret;
  1315. }
  1316. static int
  1317. ftrace_filter_open(struct inode *inode, struct file *file)
  1318. {
  1319. return ftrace_regex_open(inode, file, 1);
  1320. }
  1321. static int
  1322. ftrace_notrace_open(struct inode *inode, struct file *file)
  1323. {
  1324. return ftrace_regex_open(inode, file, 0);
  1325. }
  1326. static loff_t
  1327. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  1328. {
  1329. loff_t ret;
  1330. if (file->f_mode & FMODE_READ)
  1331. ret = seq_lseek(file, offset, origin);
  1332. else
  1333. file->f_pos = ret = 1;
  1334. return ret;
  1335. }
  1336. enum {
  1337. MATCH_FULL,
  1338. MATCH_FRONT_ONLY,
  1339. MATCH_MIDDLE_ONLY,
  1340. MATCH_END_ONLY,
  1341. };
  1342. /*
  1343. * (static function - no need for kernel doc)
  1344. *
  1345. * Pass in a buffer containing a glob and this function will
  1346. * set search to point to the search part of the buffer and
  1347. * return the type of search it is (see enum above).
  1348. * This does modify buff.
  1349. *
  1350. * Returns enum type.
  1351. * search returns the pointer to use for comparison.
  1352. * not returns 1 if buff started with a '!'
  1353. * 0 otherwise.
  1354. */
  1355. static int
  1356. ftrace_setup_glob(char *buff, int len, char **search, int *not)
  1357. {
  1358. int type = MATCH_FULL;
  1359. int i;
  1360. if (buff[0] == '!') {
  1361. *not = 1;
  1362. buff++;
  1363. len--;
  1364. } else
  1365. *not = 0;
  1366. *search = buff;
  1367. for (i = 0; i < len; i++) {
  1368. if (buff[i] == '*') {
  1369. if (!i) {
  1370. *search = buff + 1;
  1371. type = MATCH_END_ONLY;
  1372. } else {
  1373. if (type == MATCH_END_ONLY)
  1374. type = MATCH_MIDDLE_ONLY;
  1375. else
  1376. type = MATCH_FRONT_ONLY;
  1377. buff[i] = 0;
  1378. break;
  1379. }
  1380. }
  1381. }
  1382. return type;
  1383. }
  1384. static int ftrace_match(char *str, char *regex, int len, int type)
  1385. {
  1386. int matched = 0;
  1387. char *ptr;
  1388. switch (type) {
  1389. case MATCH_FULL:
  1390. if (strcmp(str, regex) == 0)
  1391. matched = 1;
  1392. break;
  1393. case MATCH_FRONT_ONLY:
  1394. if (strncmp(str, regex, len) == 0)
  1395. matched = 1;
  1396. break;
  1397. case MATCH_MIDDLE_ONLY:
  1398. if (strstr(str, regex))
  1399. matched = 1;
  1400. break;
  1401. case MATCH_END_ONLY:
  1402. ptr = strstr(str, regex);
  1403. if (ptr && (ptr[len] == 0))
  1404. matched = 1;
  1405. break;
  1406. }
  1407. return matched;
  1408. }
  1409. static int
  1410. ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
  1411. {
  1412. char str[KSYM_SYMBOL_LEN];
  1413. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  1414. return ftrace_match(str, regex, len, type);
  1415. }
  1416. static void ftrace_match_records(char *buff, int len, int enable)
  1417. {
  1418. unsigned int search_len;
  1419. struct ftrace_page *pg;
  1420. struct dyn_ftrace *rec;
  1421. unsigned long flag;
  1422. char *search;
  1423. int type;
  1424. int not;
  1425. flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1426. type = ftrace_setup_glob(buff, len, &search, &not);
  1427. search_len = strlen(search);
  1428. mutex_lock(&ftrace_lock);
  1429. do_for_each_ftrace_rec(pg, rec) {
  1430. if (rec->flags & FTRACE_FL_FAILED)
  1431. continue;
  1432. if (ftrace_match_record(rec, search, search_len, type)) {
  1433. if (not)
  1434. rec->flags &= ~flag;
  1435. else
  1436. rec->flags |= flag;
  1437. }
  1438. /*
  1439. * Only enable filtering if we have a function that
  1440. * is filtered on.
  1441. */
  1442. if (enable && (rec->flags & FTRACE_FL_FILTER))
  1443. ftrace_filtered = 1;
  1444. } while_for_each_ftrace_rec();
  1445. mutex_unlock(&ftrace_lock);
  1446. }
  1447. static int
  1448. ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
  1449. char *regex, int len, int type)
  1450. {
  1451. char str[KSYM_SYMBOL_LEN];
  1452. char *modname;
  1453. kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
  1454. if (!modname || strcmp(modname, mod))
  1455. return 0;
  1456. /* blank search means to match all funcs in the mod */
  1457. if (len)
  1458. return ftrace_match(str, regex, len, type);
  1459. else
  1460. return 1;
  1461. }
  1462. static void ftrace_match_module_records(char *buff, char *mod, int enable)
  1463. {
  1464. unsigned search_len = 0;
  1465. struct ftrace_page *pg;
  1466. struct dyn_ftrace *rec;
  1467. int type = MATCH_FULL;
  1468. char *search = buff;
  1469. unsigned long flag;
  1470. int not = 0;
  1471. flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1472. /* blank or '*' mean the same */
  1473. if (strcmp(buff, "*") == 0)
  1474. buff[0] = 0;
  1475. /* handle the case of 'dont filter this module' */
  1476. if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
  1477. buff[0] = 0;
  1478. not = 1;
  1479. }
  1480. if (strlen(buff)) {
  1481. type = ftrace_setup_glob(buff, strlen(buff), &search, &not);
  1482. search_len = strlen(search);
  1483. }
  1484. mutex_lock(&ftrace_lock);
  1485. do_for_each_ftrace_rec(pg, rec) {
  1486. if (rec->flags & FTRACE_FL_FAILED)
  1487. continue;
  1488. if (ftrace_match_module_record(rec, mod,
  1489. search, search_len, type)) {
  1490. if (not)
  1491. rec->flags &= ~flag;
  1492. else
  1493. rec->flags |= flag;
  1494. }
  1495. if (enable && (rec->flags & FTRACE_FL_FILTER))
  1496. ftrace_filtered = 1;
  1497. } while_for_each_ftrace_rec();
  1498. mutex_unlock(&ftrace_lock);
  1499. }
  1500. /*
  1501. * We register the module command as a template to show others how
  1502. * to register the a command as well.
  1503. */
  1504. static int
  1505. ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
  1506. {
  1507. char *mod;
  1508. /*
  1509. * cmd == 'mod' because we only registered this func
  1510. * for the 'mod' ftrace_func_command.
  1511. * But if you register one func with multiple commands,
  1512. * you can tell which command was used by the cmd
  1513. * parameter.
  1514. */
  1515. /* we must have a module name */
  1516. if (!param)
  1517. return -EINVAL;
  1518. mod = strsep(&param, ":");
  1519. if (!strlen(mod))
  1520. return -EINVAL;
  1521. ftrace_match_module_records(func, mod, enable);
  1522. return 0;
  1523. }
  1524. static struct ftrace_func_command ftrace_mod_cmd = {
  1525. .name = "mod",
  1526. .func = ftrace_mod_callback,
  1527. };
  1528. static int __init ftrace_mod_cmd_init(void)
  1529. {
  1530. return register_ftrace_command(&ftrace_mod_cmd);
  1531. }
  1532. device_initcall(ftrace_mod_cmd_init);
  1533. static void
  1534. function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
  1535. {
  1536. struct ftrace_func_probe *entry;
  1537. struct hlist_head *hhd;
  1538. struct hlist_node *n;
  1539. unsigned long key;
  1540. int resched;
  1541. key = hash_long(ip, FTRACE_HASH_BITS);
  1542. hhd = &ftrace_func_hash[key];
  1543. if (hlist_empty(hhd))
  1544. return;
  1545. /*
  1546. * Disable preemption for these calls to prevent a RCU grace
  1547. * period. This syncs the hash iteration and freeing of items
  1548. * on the hash. rcu_read_lock is too dangerous here.
  1549. */
  1550. resched = ftrace_preempt_disable();
  1551. hlist_for_each_entry_rcu(entry, n, hhd, node) {
  1552. if (entry->ip == ip)
  1553. entry->ops->func(ip, parent_ip, &entry->data);
  1554. }
  1555. ftrace_preempt_enable(resched);
  1556. }
  1557. static struct ftrace_ops trace_probe_ops __read_mostly =
  1558. {
  1559. .func = function_trace_probe_call,
  1560. };
  1561. static int ftrace_probe_registered;
  1562. static void __enable_ftrace_function_probe(void)
  1563. {
  1564. int i;
  1565. if (ftrace_probe_registered)
  1566. return;
  1567. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1568. struct hlist_head *hhd = &ftrace_func_hash[i];
  1569. if (hhd->first)
  1570. break;
  1571. }
  1572. /* Nothing registered? */
  1573. if (i == FTRACE_FUNC_HASHSIZE)
  1574. return;
  1575. __register_ftrace_function(&trace_probe_ops);
  1576. ftrace_startup(0);
  1577. ftrace_probe_registered = 1;
  1578. }
  1579. static void __disable_ftrace_function_probe(void)
  1580. {
  1581. int i;
  1582. if (!ftrace_probe_registered)
  1583. return;
  1584. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1585. struct hlist_head *hhd = &ftrace_func_hash[i];
  1586. if (hhd->first)
  1587. return;
  1588. }
  1589. /* no more funcs left */
  1590. __unregister_ftrace_function(&trace_probe_ops);
  1591. ftrace_shutdown(0);
  1592. ftrace_probe_registered = 0;
  1593. }
  1594. static void ftrace_free_entry_rcu(struct rcu_head *rhp)
  1595. {
  1596. struct ftrace_func_probe *entry =
  1597. container_of(rhp, struct ftrace_func_probe, rcu);
  1598. if (entry->ops->free)
  1599. entry->ops->free(&entry->data);
  1600. kfree(entry);
  1601. }
  1602. int
  1603. register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1604. void *data)
  1605. {
  1606. struct ftrace_func_probe *entry;
  1607. struct ftrace_page *pg;
  1608. struct dyn_ftrace *rec;
  1609. int type, len, not;
  1610. unsigned long key;
  1611. int count = 0;
  1612. char *search;
  1613. type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
  1614. len = strlen(search);
  1615. /* we do not support '!' for function probes */
  1616. if (WARN_ON(not))
  1617. return -EINVAL;
  1618. mutex_lock(&ftrace_lock);
  1619. do_for_each_ftrace_rec(pg, rec) {
  1620. if (rec->flags & FTRACE_FL_FAILED)
  1621. continue;
  1622. if (!ftrace_match_record(rec, search, len, type))
  1623. continue;
  1624. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  1625. if (!entry) {
  1626. /* If we did not process any, then return error */
  1627. if (!count)
  1628. count = -ENOMEM;
  1629. goto out_unlock;
  1630. }
  1631. count++;
  1632. entry->data = data;
  1633. /*
  1634. * The caller might want to do something special
  1635. * for each function we find. We call the callback
  1636. * to give the caller an opportunity to do so.
  1637. */
  1638. if (ops->callback) {
  1639. if (ops->callback(rec->ip, &entry->data) < 0) {
  1640. /* caller does not like this func */
  1641. kfree(entry);
  1642. continue;
  1643. }
  1644. }
  1645. entry->ops = ops;
  1646. entry->ip = rec->ip;
  1647. key = hash_long(entry->ip, FTRACE_HASH_BITS);
  1648. hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
  1649. } while_for_each_ftrace_rec();
  1650. __enable_ftrace_function_probe();
  1651. out_unlock:
  1652. mutex_unlock(&ftrace_lock);
  1653. return count;
  1654. }
  1655. enum {
  1656. PROBE_TEST_FUNC = 1,
  1657. PROBE_TEST_DATA = 2
  1658. };
  1659. static void
  1660. __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1661. void *data, int flags)
  1662. {
  1663. struct ftrace_func_probe *entry;
  1664. struct hlist_node *n, *tmp;
  1665. char str[KSYM_SYMBOL_LEN];
  1666. int type = MATCH_FULL;
  1667. int i, len = 0;
  1668. char *search;
  1669. if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
  1670. glob = NULL;
  1671. else if (glob) {
  1672. int not;
  1673. type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
  1674. len = strlen(search);
  1675. /* we do not support '!' for function probes */
  1676. if (WARN_ON(not))
  1677. return;
  1678. }
  1679. mutex_lock(&ftrace_lock);
  1680. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1681. struct hlist_head *hhd = &ftrace_func_hash[i];
  1682. hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
  1683. /* break up if statements for readability */
  1684. if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
  1685. continue;
  1686. if ((flags & PROBE_TEST_DATA) && entry->data != data)
  1687. continue;
  1688. /* do this last, since it is the most expensive */
  1689. if (glob) {
  1690. kallsyms_lookup(entry->ip, NULL, NULL,
  1691. NULL, str);
  1692. if (!ftrace_match(str, glob, len, type))
  1693. continue;
  1694. }
  1695. hlist_del(&entry->node);
  1696. call_rcu(&entry->rcu, ftrace_free_entry_rcu);
  1697. }
  1698. }
  1699. __disable_ftrace_function_probe();
  1700. mutex_unlock(&ftrace_lock);
  1701. }
  1702. void
  1703. unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1704. void *data)
  1705. {
  1706. __unregister_ftrace_function_probe(glob, ops, data,
  1707. PROBE_TEST_FUNC | PROBE_TEST_DATA);
  1708. }
  1709. void
  1710. unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
  1711. {
  1712. __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
  1713. }
  1714. void unregister_ftrace_function_probe_all(char *glob)
  1715. {
  1716. __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
  1717. }
  1718. static LIST_HEAD(ftrace_commands);
  1719. static DEFINE_MUTEX(ftrace_cmd_mutex);
  1720. int register_ftrace_command(struct ftrace_func_command *cmd)
  1721. {
  1722. struct ftrace_func_command *p;
  1723. int ret = 0;
  1724. mutex_lock(&ftrace_cmd_mutex);
  1725. list_for_each_entry(p, &ftrace_commands, list) {
  1726. if (strcmp(cmd->name, p->name) == 0) {
  1727. ret = -EBUSY;
  1728. goto out_unlock;
  1729. }
  1730. }
  1731. list_add(&cmd->list, &ftrace_commands);
  1732. out_unlock:
  1733. mutex_unlock(&ftrace_cmd_mutex);
  1734. return ret;
  1735. }
  1736. int unregister_ftrace_command(struct ftrace_func_command *cmd)
  1737. {
  1738. struct ftrace_func_command *p, *n;
  1739. int ret = -ENODEV;
  1740. mutex_lock(&ftrace_cmd_mutex);
  1741. list_for_each_entry_safe(p, n, &ftrace_commands, list) {
  1742. if (strcmp(cmd->name, p->name) == 0) {
  1743. ret = 0;
  1744. list_del_init(&p->list);
  1745. goto out_unlock;
  1746. }
  1747. }
  1748. out_unlock:
  1749. mutex_unlock(&ftrace_cmd_mutex);
  1750. return ret;
  1751. }
  1752. static int ftrace_process_regex(char *buff, int len, int enable)
  1753. {
  1754. char *func, *command, *next = buff;
  1755. struct ftrace_func_command *p;
  1756. int ret = -EINVAL;
  1757. func = strsep(&next, ":");
  1758. if (!next) {
  1759. ftrace_match_records(func, len, enable);
  1760. return 0;
  1761. }
  1762. /* command found */
  1763. command = strsep(&next, ":");
  1764. mutex_lock(&ftrace_cmd_mutex);
  1765. list_for_each_entry(p, &ftrace_commands, list) {
  1766. if (strcmp(p->name, command) == 0) {
  1767. ret = p->func(func, command, next, enable);
  1768. goto out_unlock;
  1769. }
  1770. }
  1771. out_unlock:
  1772. mutex_unlock(&ftrace_cmd_mutex);
  1773. return ret;
  1774. }
  1775. static ssize_t
  1776. ftrace_regex_write(struct file *file, const char __user *ubuf,
  1777. size_t cnt, loff_t *ppos, int enable)
  1778. {
  1779. struct ftrace_iterator *iter;
  1780. struct trace_parser *parser;
  1781. ssize_t ret, read;
  1782. if (!cnt || cnt < 0)
  1783. return 0;
  1784. mutex_lock(&ftrace_regex_lock);
  1785. if (file->f_mode & FMODE_READ) {
  1786. struct seq_file *m = file->private_data;
  1787. iter = m->private;
  1788. } else
  1789. iter = file->private_data;
  1790. parser = &iter->parser;
  1791. read = trace_get_user(parser, ubuf, cnt, ppos);
  1792. if (trace_parser_loaded(parser) &&
  1793. !trace_parser_cont(parser)) {
  1794. ret = ftrace_process_regex(parser->buffer,
  1795. parser->idx, enable);
  1796. if (ret)
  1797. goto out;
  1798. trace_parser_clear(parser);
  1799. }
  1800. ret = read;
  1801. mutex_unlock(&ftrace_regex_lock);
  1802. out:
  1803. return ret;
  1804. }
  1805. static ssize_t
  1806. ftrace_filter_write(struct file *file, const char __user *ubuf,
  1807. size_t cnt, loff_t *ppos)
  1808. {
  1809. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  1810. }
  1811. static ssize_t
  1812. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  1813. size_t cnt, loff_t *ppos)
  1814. {
  1815. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  1816. }
  1817. static void
  1818. ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
  1819. {
  1820. if (unlikely(ftrace_disabled))
  1821. return;
  1822. mutex_lock(&ftrace_regex_lock);
  1823. if (reset)
  1824. ftrace_filter_reset(enable);
  1825. if (buf)
  1826. ftrace_match_records(buf, len, enable);
  1827. mutex_unlock(&ftrace_regex_lock);
  1828. }
  1829. /**
  1830. * ftrace_set_filter - set a function to filter on in ftrace
  1831. * @buf - the string that holds the function filter text.
  1832. * @len - the length of the string.
  1833. * @reset - non zero to reset all filters before applying this filter.
  1834. *
  1835. * Filters denote which functions should be enabled when tracing is enabled.
  1836. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  1837. */
  1838. void ftrace_set_filter(unsigned char *buf, int len, int reset)
  1839. {
  1840. ftrace_set_regex(buf, len, reset, 1);
  1841. }
  1842. /**
  1843. * ftrace_set_notrace - set a function to not trace in ftrace
  1844. * @buf - the string that holds the function notrace text.
  1845. * @len - the length of the string.
  1846. * @reset - non zero to reset all filters before applying this filter.
  1847. *
  1848. * Notrace Filters denote which functions should not be enabled when tracing
  1849. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  1850. * for tracing.
  1851. */
  1852. void ftrace_set_notrace(unsigned char *buf, int len, int reset)
  1853. {
  1854. ftrace_set_regex(buf, len, reset, 0);
  1855. }
  1856. /*
  1857. * command line interface to allow users to set filters on boot up.
  1858. */
  1859. #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
  1860. static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
  1861. static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
  1862. static int __init set_ftrace_notrace(char *str)
  1863. {
  1864. strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
  1865. return 1;
  1866. }
  1867. __setup("ftrace_notrace=", set_ftrace_notrace);
  1868. static int __init set_ftrace_filter(char *str)
  1869. {
  1870. strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
  1871. return 1;
  1872. }
  1873. __setup("ftrace_filter=", set_ftrace_filter);
  1874. static void __init set_ftrace_early_filter(char *buf, int enable)
  1875. {
  1876. char *func;
  1877. while (buf) {
  1878. func = strsep(&buf, ",");
  1879. ftrace_set_regex(func, strlen(func), 0, enable);
  1880. }
  1881. }
  1882. static void __init set_ftrace_early_filters(void)
  1883. {
  1884. if (ftrace_filter_buf[0])
  1885. set_ftrace_early_filter(ftrace_filter_buf, 1);
  1886. if (ftrace_notrace_buf[0])
  1887. set_ftrace_early_filter(ftrace_notrace_buf, 0);
  1888. }
  1889. static int
  1890. ftrace_regex_release(struct inode *inode, struct file *file, int enable)
  1891. {
  1892. struct seq_file *m = (struct seq_file *)file->private_data;
  1893. struct ftrace_iterator *iter;
  1894. struct trace_parser *parser;
  1895. mutex_lock(&ftrace_regex_lock);
  1896. if (file->f_mode & FMODE_READ) {
  1897. iter = m->private;
  1898. seq_release(inode, file);
  1899. } else
  1900. iter = file->private_data;
  1901. parser = &iter->parser;
  1902. if (trace_parser_loaded(parser)) {
  1903. parser->buffer[parser->idx] = 0;
  1904. ftrace_match_records(parser->buffer, parser->idx, enable);
  1905. }
  1906. mutex_lock(&ftrace_lock);
  1907. if (ftrace_start_up && ftrace_enabled)
  1908. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  1909. mutex_unlock(&ftrace_lock);
  1910. trace_parser_put(parser);
  1911. kfree(iter);
  1912. mutex_unlock(&ftrace_regex_lock);
  1913. return 0;
  1914. }
  1915. static int
  1916. ftrace_filter_release(struct inode *inode, struct file *file)
  1917. {
  1918. return ftrace_regex_release(inode, file, 1);
  1919. }
  1920. static int
  1921. ftrace_notrace_release(struct inode *inode, struct file *file)
  1922. {
  1923. return ftrace_regex_release(inode, file, 0);
  1924. }
  1925. static const struct file_operations ftrace_avail_fops = {
  1926. .open = ftrace_avail_open,
  1927. .read = seq_read,
  1928. .llseek = seq_lseek,
  1929. .release = seq_release_private,
  1930. };
  1931. static const struct file_operations ftrace_failures_fops = {
  1932. .open = ftrace_failures_open,
  1933. .read = seq_read,
  1934. .llseek = seq_lseek,
  1935. .release = seq_release_private,
  1936. };
  1937. static const struct file_operations ftrace_filter_fops = {
  1938. .open = ftrace_filter_open,
  1939. .read = seq_read,
  1940. .write = ftrace_filter_write,
  1941. .llseek = ftrace_regex_lseek,
  1942. .release = ftrace_filter_release,
  1943. };
  1944. static const struct file_operations ftrace_notrace_fops = {
  1945. .open = ftrace_notrace_open,
  1946. .read = seq_read,
  1947. .write = ftrace_notrace_write,
  1948. .llseek = ftrace_regex_lseek,
  1949. .release = ftrace_notrace_release,
  1950. };
  1951. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1952. static DEFINE_MUTEX(graph_lock);
  1953. int ftrace_graph_count;
  1954. unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
  1955. static void *
  1956. __g_next(struct seq_file *m, loff_t *pos)
  1957. {
  1958. if (*pos >= ftrace_graph_count)
  1959. return NULL;
  1960. return &ftrace_graph_funcs[*pos];
  1961. }
  1962. static void *
  1963. g_next(struct seq_file *m, void *v, loff_t *pos)
  1964. {
  1965. (*pos)++;
  1966. return __g_next(m, pos);
  1967. }
  1968. static void *g_start(struct seq_file *m, loff_t *pos)
  1969. {
  1970. mutex_lock(&graph_lock);
  1971. /* Nothing, tell g_show to print all functions are enabled */
  1972. if (!ftrace_graph_count && !*pos)
  1973. return (void *)1;
  1974. return __g_next(m, pos);
  1975. }
  1976. static void g_stop(struct seq_file *m, void *p)
  1977. {
  1978. mutex_unlock(&graph_lock);
  1979. }
  1980. static int g_show(struct seq_file *m, void *v)
  1981. {
  1982. unsigned long *ptr = v;
  1983. if (!ptr)
  1984. return 0;
  1985. if (ptr == (unsigned long *)1) {
  1986. seq_printf(m, "#### all functions enabled ####\n");
  1987. return 0;
  1988. }
  1989. seq_printf(m, "%ps\n", (void *)*ptr);
  1990. return 0;
  1991. }
  1992. static const struct seq_operations ftrace_graph_seq_ops = {
  1993. .start = g_start,
  1994. .next = g_next,
  1995. .stop = g_stop,
  1996. .show = g_show,
  1997. };
  1998. static int
  1999. ftrace_graph_open(struct inode *inode, struct file *file)
  2000. {
  2001. int ret = 0;
  2002. if (unlikely(ftrace_disabled))
  2003. return -ENODEV;
  2004. mutex_lock(&graph_lock);
  2005. if ((file->f_mode & FMODE_WRITE) &&
  2006. (file->f_flags & O_TRUNC)) {
  2007. ftrace_graph_count = 0;
  2008. memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
  2009. }
  2010. mutex_unlock(&graph_lock);
  2011. if (file->f_mode & FMODE_READ)
  2012. ret = seq_open(file, &ftrace_graph_seq_ops);
  2013. return ret;
  2014. }
  2015. static int
  2016. ftrace_graph_release(struct inode *inode, struct file *file)
  2017. {
  2018. if (file->f_mode & FMODE_READ)
  2019. seq_release(inode, file);
  2020. return 0;
  2021. }
  2022. static int
  2023. ftrace_set_func(unsigned long *array, int *idx, char *buffer)
  2024. {
  2025. struct dyn_ftrace *rec;
  2026. struct ftrace_page *pg;
  2027. int search_len;
  2028. int found = 0;
  2029. int type, not;
  2030. char *search;
  2031. bool exists;
  2032. int i;
  2033. if (ftrace_disabled)
  2034. return -ENODEV;
  2035. /* decode regex */
  2036. type = ftrace_setup_glob(buffer, strlen(buffer), &search, &not);
  2037. if (not)
  2038. return -EINVAL;
  2039. search_len = strlen(search);
  2040. mutex_lock(&ftrace_lock);
  2041. do_for_each_ftrace_rec(pg, rec) {
  2042. if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
  2043. break;
  2044. if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
  2045. continue;
  2046. if (ftrace_match_record(rec, search, search_len, type)) {
  2047. /* ensure it is not already in the array */
  2048. exists = false;
  2049. for (i = 0; i < *idx; i++)
  2050. if (array[i] == rec->ip) {
  2051. exists = true;
  2052. break;
  2053. }
  2054. if (!exists) {
  2055. array[(*idx)++] = rec->ip;
  2056. found = 1;
  2057. }
  2058. }
  2059. } while_for_each_ftrace_rec();
  2060. mutex_unlock(&ftrace_lock);
  2061. return found ? 0 : -EINVAL;
  2062. }
  2063. static ssize_t
  2064. ftrace_graph_write(struct file *file, const char __user *ubuf,
  2065. size_t cnt, loff_t *ppos)
  2066. {
  2067. struct trace_parser parser;
  2068. size_t read = 0;
  2069. ssize_t ret;
  2070. if (!cnt || cnt < 0)
  2071. return 0;
  2072. mutex_lock(&graph_lock);
  2073. if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
  2074. ret = -EBUSY;
  2075. goto out;
  2076. }
  2077. if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
  2078. ret = -ENOMEM;
  2079. goto out;
  2080. }
  2081. read = trace_get_user(&parser, ubuf, cnt, ppos);
  2082. if (trace_parser_loaded((&parser))) {
  2083. parser.buffer[parser.idx] = 0;
  2084. /* we allow only one expression at a time */
  2085. ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
  2086. parser.buffer);
  2087. if (ret)
  2088. goto out;
  2089. }
  2090. ret = read;
  2091. out:
  2092. trace_parser_put(&parser);
  2093. mutex_unlock(&graph_lock);
  2094. return ret;
  2095. }
  2096. static const struct file_operations ftrace_graph_fops = {
  2097. .open = ftrace_graph_open,
  2098. .read = seq_read,
  2099. .write = ftrace_graph_write,
  2100. .release = ftrace_graph_release,
  2101. };
  2102. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2103. static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
  2104. {
  2105. trace_create_file("available_filter_functions", 0444,
  2106. d_tracer, NULL, &ftrace_avail_fops);
  2107. trace_create_file("failures", 0444,
  2108. d_tracer, NULL, &ftrace_failures_fops);
  2109. trace_create_file("set_ftrace_filter", 0644, d_tracer,
  2110. NULL, &ftrace_filter_fops);
  2111. trace_create_file("set_ftrace_notrace", 0644, d_tracer,
  2112. NULL, &ftrace_notrace_fops);
  2113. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2114. trace_create_file("set_graph_function", 0444, d_tracer,
  2115. NULL,
  2116. &ftrace_graph_fops);
  2117. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2118. return 0;
  2119. }
  2120. static int ftrace_convert_nops(struct module *mod,
  2121. unsigned long *start,
  2122. unsigned long *end)
  2123. {
  2124. unsigned long *p;
  2125. unsigned long addr;
  2126. unsigned long flags;
  2127. mutex_lock(&ftrace_lock);
  2128. p = start;
  2129. while (p < end) {
  2130. addr = ftrace_call_adjust(*p++);
  2131. /*
  2132. * Some architecture linkers will pad between
  2133. * the different mcount_loc sections of different
  2134. * object files to satisfy alignments.
  2135. * Skip any NULL pointers.
  2136. */
  2137. if (!addr)
  2138. continue;
  2139. ftrace_record_ip(addr);
  2140. }
  2141. /* disable interrupts to prevent kstop machine */
  2142. local_irq_save(flags);
  2143. ftrace_update_code(mod);
  2144. local_irq_restore(flags);
  2145. mutex_unlock(&ftrace_lock);
  2146. return 0;
  2147. }
  2148. #ifdef CONFIG_MODULES
  2149. void ftrace_release(void *start, void *end)
  2150. {
  2151. struct dyn_ftrace *rec;
  2152. struct ftrace_page *pg;
  2153. unsigned long s = (unsigned long)start;
  2154. unsigned long e = (unsigned long)end;
  2155. if (ftrace_disabled || !start || start == end)
  2156. return;
  2157. mutex_lock(&ftrace_lock);
  2158. do_for_each_ftrace_rec(pg, rec) {
  2159. if ((rec->ip >= s) && (rec->ip < e)) {
  2160. /*
  2161. * rec->ip is changed in ftrace_free_rec()
  2162. * It should not between s and e if record was freed.
  2163. */
  2164. FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
  2165. ftrace_free_rec(rec);
  2166. }
  2167. } while_for_each_ftrace_rec();
  2168. mutex_unlock(&ftrace_lock);
  2169. }
  2170. static void ftrace_init_module(struct module *mod,
  2171. unsigned long *start, unsigned long *end)
  2172. {
  2173. if (ftrace_disabled || start == end)
  2174. return;
  2175. ftrace_convert_nops(mod, start, end);
  2176. }
  2177. static int ftrace_module_notify(struct notifier_block *self,
  2178. unsigned long val, void *data)
  2179. {
  2180. struct module *mod = data;
  2181. switch (val) {
  2182. case MODULE_STATE_COMING:
  2183. ftrace_init_module(mod, mod->ftrace_callsites,
  2184. mod->ftrace_callsites +
  2185. mod->num_ftrace_callsites);
  2186. break;
  2187. case MODULE_STATE_GOING:
  2188. ftrace_release(mod->ftrace_callsites,
  2189. mod->ftrace_callsites +
  2190. mod->num_ftrace_callsites);
  2191. break;
  2192. }
  2193. return 0;
  2194. }
  2195. #else
  2196. static int ftrace_module_notify(struct notifier_block *self,
  2197. unsigned long val, void *data)
  2198. {
  2199. return 0;
  2200. }
  2201. #endif /* CONFIG_MODULES */
  2202. struct notifier_block ftrace_module_nb = {
  2203. .notifier_call = ftrace_module_notify,
  2204. .priority = 0,
  2205. };
  2206. extern unsigned long __start_mcount_loc[];
  2207. extern unsigned long __stop_mcount_loc[];
  2208. void __init ftrace_init(void)
  2209. {
  2210. unsigned long count, addr, flags;
  2211. int ret;
  2212. /* Keep the ftrace pointer to the stub */
  2213. addr = (unsigned long)ftrace_stub;
  2214. local_irq_save(flags);
  2215. ftrace_dyn_arch_init(&addr);
  2216. local_irq_restore(flags);
  2217. /* ftrace_dyn_arch_init places the return code in addr */
  2218. if (addr)
  2219. goto failed;
  2220. count = __stop_mcount_loc - __start_mcount_loc;
  2221. ret = ftrace_dyn_table_alloc(count);
  2222. if (ret)
  2223. goto failed;
  2224. last_ftrace_enabled = ftrace_enabled = 1;
  2225. ret = ftrace_convert_nops(NULL,
  2226. __start_mcount_loc,
  2227. __stop_mcount_loc);
  2228. ret = register_module_notifier(&ftrace_module_nb);
  2229. if (ret)
  2230. pr_warning("Failed to register trace ftrace module notifier\n");
  2231. set_ftrace_early_filters();
  2232. return;
  2233. failed:
  2234. ftrace_disabled = 1;
  2235. }
  2236. #else
  2237. static int __init ftrace_nodyn_init(void)
  2238. {
  2239. ftrace_enabled = 1;
  2240. return 0;
  2241. }
  2242. device_initcall(ftrace_nodyn_init);
  2243. static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
  2244. static inline void ftrace_startup_enable(int command) { }
  2245. /* Keep as macros so we do not need to define the commands */
  2246. # define ftrace_startup(command) do { } while (0)
  2247. # define ftrace_shutdown(command) do { } while (0)
  2248. # define ftrace_startup_sysctl() do { } while (0)
  2249. # define ftrace_shutdown_sysctl() do { } while (0)
  2250. #endif /* CONFIG_DYNAMIC_FTRACE */
  2251. static ssize_t
  2252. ftrace_pid_read(struct file *file, char __user *ubuf,
  2253. size_t cnt, loff_t *ppos)
  2254. {
  2255. char buf[64];
  2256. int r;
  2257. if (ftrace_pid_trace == ftrace_swapper_pid)
  2258. r = sprintf(buf, "swapper tasks\n");
  2259. else if (ftrace_pid_trace)
  2260. r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
  2261. else
  2262. r = sprintf(buf, "no pid\n");
  2263. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2264. }
  2265. static void clear_ftrace_swapper(void)
  2266. {
  2267. struct task_struct *p;
  2268. int cpu;
  2269. get_online_cpus();
  2270. for_each_online_cpu(cpu) {
  2271. p = idle_task(cpu);
  2272. clear_tsk_trace_trace(p);
  2273. }
  2274. put_online_cpus();
  2275. }
  2276. static void set_ftrace_swapper(void)
  2277. {
  2278. struct task_struct *p;
  2279. int cpu;
  2280. get_online_cpus();
  2281. for_each_online_cpu(cpu) {
  2282. p = idle_task(cpu);
  2283. set_tsk_trace_trace(p);
  2284. }
  2285. put_online_cpus();
  2286. }
  2287. static void clear_ftrace_pid(struct pid *pid)
  2288. {
  2289. struct task_struct *p;
  2290. rcu_read_lock();
  2291. do_each_pid_task(pid, PIDTYPE_PID, p) {
  2292. clear_tsk_trace_trace(p);
  2293. } while_each_pid_task(pid, PIDTYPE_PID, p);
  2294. rcu_read_unlock();
  2295. put_pid(pid);
  2296. }
  2297. static void set_ftrace_pid(struct pid *pid)
  2298. {
  2299. struct task_struct *p;
  2300. rcu_read_lock();
  2301. do_each_pid_task(pid, PIDTYPE_PID, p) {
  2302. set_tsk_trace_trace(p);
  2303. } while_each_pid_task(pid, PIDTYPE_PID, p);
  2304. rcu_read_unlock();
  2305. }
  2306. static void clear_ftrace_pid_task(struct pid **pid)
  2307. {
  2308. if (*pid == ftrace_swapper_pid)
  2309. clear_ftrace_swapper();
  2310. else
  2311. clear_ftrace_pid(*pid);
  2312. *pid = NULL;
  2313. }
  2314. static void set_ftrace_pid_task(struct pid *pid)
  2315. {
  2316. if (pid == ftrace_swapper_pid)
  2317. set_ftrace_swapper();
  2318. else
  2319. set_ftrace_pid(pid);
  2320. }
  2321. static ssize_t
  2322. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  2323. size_t cnt, loff_t *ppos)
  2324. {
  2325. struct pid *pid;
  2326. char buf[64];
  2327. long val;
  2328. int ret;
  2329. if (cnt >= sizeof(buf))
  2330. return -EINVAL;
  2331. if (copy_from_user(&buf, ubuf, cnt))
  2332. return -EFAULT;
  2333. buf[cnt] = 0;
  2334. ret = strict_strtol(buf, 10, &val);
  2335. if (ret < 0)
  2336. return ret;
  2337. mutex_lock(&ftrace_lock);
  2338. if (val < 0) {
  2339. /* disable pid tracing */
  2340. if (!ftrace_pid_trace)
  2341. goto out;
  2342. clear_ftrace_pid_task(&ftrace_pid_trace);
  2343. } else {
  2344. /* swapper task is special */
  2345. if (!val) {
  2346. pid = ftrace_swapper_pid;
  2347. if (pid == ftrace_pid_trace)
  2348. goto out;
  2349. } else {
  2350. pid = find_get_pid(val);
  2351. if (pid == ftrace_pid_trace) {
  2352. put_pid(pid);
  2353. goto out;
  2354. }
  2355. }
  2356. if (ftrace_pid_trace)
  2357. clear_ftrace_pid_task(&ftrace_pid_trace);
  2358. if (!pid)
  2359. goto out;
  2360. ftrace_pid_trace = pid;
  2361. set_ftrace_pid_task(ftrace_pid_trace);
  2362. }
  2363. /* update the function call */
  2364. ftrace_update_pid_func();
  2365. ftrace_startup_enable(0);
  2366. out:
  2367. mutex_unlock(&ftrace_lock);
  2368. return cnt;
  2369. }
  2370. static const struct file_operations ftrace_pid_fops = {
  2371. .read = ftrace_pid_read,
  2372. .write = ftrace_pid_write,
  2373. };
  2374. static __init int ftrace_init_debugfs(void)
  2375. {
  2376. struct dentry *d_tracer;
  2377. d_tracer = tracing_init_dentry();
  2378. if (!d_tracer)
  2379. return 0;
  2380. ftrace_init_dyn_debugfs(d_tracer);
  2381. trace_create_file("set_ftrace_pid", 0644, d_tracer,
  2382. NULL, &ftrace_pid_fops);
  2383. ftrace_profile_debugfs(d_tracer);
  2384. return 0;
  2385. }
  2386. fs_initcall(ftrace_init_debugfs);
  2387. /**
  2388. * ftrace_kill - kill ftrace
  2389. *
  2390. * This function should be used by panic code. It stops ftrace
  2391. * but in a not so nice way. If you need to simply kill ftrace
  2392. * from a non-atomic section, use ftrace_kill.
  2393. */
  2394. void ftrace_kill(void)
  2395. {
  2396. ftrace_disabled = 1;
  2397. ftrace_enabled = 0;
  2398. clear_ftrace_function();
  2399. }
  2400. /**
  2401. * register_ftrace_function - register a function for profiling
  2402. * @ops - ops structure that holds the function for profiling.
  2403. *
  2404. * Register a function to be called by all functions in the
  2405. * kernel.
  2406. *
  2407. * Note: @ops->func and all the functions it calls must be labeled
  2408. * with "notrace", otherwise it will go into a
  2409. * recursive loop.
  2410. */
  2411. int register_ftrace_function(struct ftrace_ops *ops)
  2412. {
  2413. int ret;
  2414. if (unlikely(ftrace_disabled))
  2415. return -1;
  2416. mutex_lock(&ftrace_lock);
  2417. ret = __register_ftrace_function(ops);
  2418. ftrace_startup(0);
  2419. mutex_unlock(&ftrace_lock);
  2420. return ret;
  2421. }
  2422. /**
  2423. * unregister_ftrace_function - unregister a function for profiling.
  2424. * @ops - ops structure that holds the function to unregister
  2425. *
  2426. * Unregister a function that was added to be called by ftrace profiling.
  2427. */
  2428. int unregister_ftrace_function(struct ftrace_ops *ops)
  2429. {
  2430. int ret;
  2431. mutex_lock(&ftrace_lock);
  2432. ret = __unregister_ftrace_function(ops);
  2433. ftrace_shutdown(0);
  2434. mutex_unlock(&ftrace_lock);
  2435. return ret;
  2436. }
  2437. int
  2438. ftrace_enable_sysctl(struct ctl_table *table, int write,
  2439. struct file *file, void __user *buffer, size_t *lenp,
  2440. loff_t *ppos)
  2441. {
  2442. int ret;
  2443. if (unlikely(ftrace_disabled))
  2444. return -ENODEV;
  2445. mutex_lock(&ftrace_lock);
  2446. ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
  2447. if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
  2448. goto out;
  2449. last_ftrace_enabled = !!ftrace_enabled;
  2450. if (ftrace_enabled) {
  2451. ftrace_startup_sysctl();
  2452. /* we are starting ftrace again */
  2453. if (ftrace_list != &ftrace_list_end) {
  2454. if (ftrace_list->next == &ftrace_list_end)
  2455. ftrace_trace_function = ftrace_list->func;
  2456. else
  2457. ftrace_trace_function = ftrace_list_func;
  2458. }
  2459. } else {
  2460. /* stopping ftrace calls (just send to ftrace_stub) */
  2461. ftrace_trace_function = ftrace_stub;
  2462. ftrace_shutdown_sysctl();
  2463. }
  2464. out:
  2465. mutex_unlock(&ftrace_lock);
  2466. return ret;
  2467. }
  2468. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2469. static int ftrace_graph_active;
  2470. static struct notifier_block ftrace_suspend_notifier;
  2471. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
  2472. {
  2473. return 0;
  2474. }
  2475. /* The callbacks that hook a function */
  2476. trace_func_graph_ret_t ftrace_graph_return =
  2477. (trace_func_graph_ret_t)ftrace_stub;
  2478. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  2479. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  2480. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  2481. {
  2482. int i;
  2483. int ret = 0;
  2484. unsigned long flags;
  2485. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  2486. struct task_struct *g, *t;
  2487. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  2488. ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
  2489. * sizeof(struct ftrace_ret_stack),
  2490. GFP_KERNEL);
  2491. if (!ret_stack_list[i]) {
  2492. start = 0;
  2493. end = i;
  2494. ret = -ENOMEM;
  2495. goto free;
  2496. }
  2497. }
  2498. read_lock_irqsave(&tasklist_lock, flags);
  2499. do_each_thread(g, t) {
  2500. if (start == end) {
  2501. ret = -EAGAIN;
  2502. goto unlock;
  2503. }
  2504. if (t->ret_stack == NULL) {
  2505. atomic_set(&t->tracing_graph_pause, 0);
  2506. atomic_set(&t->trace_overrun, 0);
  2507. t->curr_ret_stack = -1;
  2508. /* Make sure the tasks see the -1 first: */
  2509. smp_wmb();
  2510. t->ret_stack = ret_stack_list[start++];
  2511. }
  2512. } while_each_thread(g, t);
  2513. unlock:
  2514. read_unlock_irqrestore(&tasklist_lock, flags);
  2515. free:
  2516. for (i = start; i < end; i++)
  2517. kfree(ret_stack_list[i]);
  2518. return ret;
  2519. }
  2520. static void
  2521. ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
  2522. struct task_struct *next)
  2523. {
  2524. unsigned long long timestamp;
  2525. int index;
  2526. /*
  2527. * Does the user want to count the time a function was asleep.
  2528. * If so, do not update the time stamps.
  2529. */
  2530. if (trace_flags & TRACE_ITER_SLEEP_TIME)
  2531. return;
  2532. timestamp = trace_clock_local();
  2533. prev->ftrace_timestamp = timestamp;
  2534. /* only process tasks that we timestamped */
  2535. if (!next->ftrace_timestamp)
  2536. return;
  2537. /*
  2538. * Update all the counters in next to make up for the
  2539. * time next was sleeping.
  2540. */
  2541. timestamp -= next->ftrace_timestamp;
  2542. for (index = next->curr_ret_stack; index >= 0; index--)
  2543. next->ret_stack[index].calltime += timestamp;
  2544. }
  2545. /* Allocate a return stack for each task */
  2546. static int start_graph_tracing(void)
  2547. {
  2548. struct ftrace_ret_stack **ret_stack_list;
  2549. int ret, cpu;
  2550. ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
  2551. sizeof(struct ftrace_ret_stack *),
  2552. GFP_KERNEL);
  2553. if (!ret_stack_list)
  2554. return -ENOMEM;
  2555. /* The cpu_boot init_task->ret_stack will never be freed */
  2556. for_each_online_cpu(cpu) {
  2557. if (!idle_task(cpu)->ret_stack)
  2558. ftrace_graph_init_task(idle_task(cpu));
  2559. }
  2560. do {
  2561. ret = alloc_retstack_tasklist(ret_stack_list);
  2562. } while (ret == -EAGAIN);
  2563. if (!ret) {
  2564. ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
  2565. if (ret)
  2566. pr_info("ftrace_graph: Couldn't activate tracepoint"
  2567. " probe to kernel_sched_switch\n");
  2568. }
  2569. kfree(ret_stack_list);
  2570. return ret;
  2571. }
  2572. /*
  2573. * Hibernation protection.
  2574. * The state of the current task is too much unstable during
  2575. * suspend/restore to disk. We want to protect against that.
  2576. */
  2577. static int
  2578. ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
  2579. void *unused)
  2580. {
  2581. switch (state) {
  2582. case PM_HIBERNATION_PREPARE:
  2583. pause_graph_tracing();
  2584. break;
  2585. case PM_POST_HIBERNATION:
  2586. unpause_graph_tracing();
  2587. break;
  2588. }
  2589. return NOTIFY_DONE;
  2590. }
  2591. int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  2592. trace_func_graph_ent_t entryfunc)
  2593. {
  2594. int ret = 0;
  2595. mutex_lock(&ftrace_lock);
  2596. /* we currently allow only one tracer registered at a time */
  2597. if (ftrace_graph_active) {
  2598. ret = -EBUSY;
  2599. goto out;
  2600. }
  2601. ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
  2602. register_pm_notifier(&ftrace_suspend_notifier);
  2603. ftrace_graph_active++;
  2604. ret = start_graph_tracing();
  2605. if (ret) {
  2606. ftrace_graph_active--;
  2607. goto out;
  2608. }
  2609. ftrace_graph_return = retfunc;
  2610. ftrace_graph_entry = entryfunc;
  2611. ftrace_startup(FTRACE_START_FUNC_RET);
  2612. out:
  2613. mutex_unlock(&ftrace_lock);
  2614. return ret;
  2615. }
  2616. void unregister_ftrace_graph(void)
  2617. {
  2618. mutex_lock(&ftrace_lock);
  2619. if (unlikely(!ftrace_graph_active))
  2620. goto out;
  2621. ftrace_graph_active--;
  2622. unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
  2623. ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
  2624. ftrace_graph_entry = ftrace_graph_entry_stub;
  2625. ftrace_shutdown(FTRACE_STOP_FUNC_RET);
  2626. unregister_pm_notifier(&ftrace_suspend_notifier);
  2627. out:
  2628. mutex_unlock(&ftrace_lock);
  2629. }
  2630. /* Allocate a return stack for newly created task */
  2631. void ftrace_graph_init_task(struct task_struct *t)
  2632. {
  2633. /* Make sure we do not use the parent ret_stack */
  2634. t->ret_stack = NULL;
  2635. if (ftrace_graph_active) {
  2636. struct ftrace_ret_stack *ret_stack;
  2637. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  2638. * sizeof(struct ftrace_ret_stack),
  2639. GFP_KERNEL);
  2640. if (!ret_stack)
  2641. return;
  2642. t->curr_ret_stack = -1;
  2643. atomic_set(&t->tracing_graph_pause, 0);
  2644. atomic_set(&t->trace_overrun, 0);
  2645. t->ftrace_timestamp = 0;
  2646. /* make curr_ret_stack visable before we add the ret_stack */
  2647. smp_wmb();
  2648. t->ret_stack = ret_stack;
  2649. }
  2650. }
  2651. void ftrace_graph_exit_task(struct task_struct *t)
  2652. {
  2653. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  2654. t->ret_stack = NULL;
  2655. /* NULL must become visible to IRQs before we free it: */
  2656. barrier();
  2657. kfree(ret_stack);
  2658. }
  2659. void ftrace_graph_stop(void)
  2660. {
  2661. ftrace_stop();
  2662. }
  2663. #endif