ftrace.c 80 KB

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