ftrace.c 87 KB

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