ftrace.c 130 KB

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