ftrace.c 137 KB

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