ftrace.c 108 KB

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