ftrace.c 108 KB

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