ftrace.c 139 KB

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