ftrace.c 87 KB

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