ftrace.c 78 KB

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