ftrace.c 71 KB

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