ftrace.c 106 KB

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