ftrace.c 70 KB

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