ftrace.c 69 KB

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