ftrace.c 117 KB

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