ftrace.c 88 KB

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