ftrace.c 106 KB

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