ftrace.c 71 KB

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