ftrace.c 71 KB

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