ftrace.c 139 KB

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