ftrace.c 146 KB

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