ftrace.c 70 KB

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