ftrace.c 109 KB

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