ftrace.c 117 KB

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