ftrace.c 106 KB

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