ftrace.c 73 KB

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