ftrace.c 88 KB

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