ftrace.c 122 KB

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