ftrace.c 104 KB

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