ftrace.c 141 KB

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