ftrace.c 71 KB

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