ftrace.c 88 KB

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