ftrace.c 71 KB

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