ftrace.c 70 KB

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