ftrace.c 151 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489
  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/sections.h>
  37. #include <asm/setup.h>
  38. #include "trace_output.h"
  39. #include "trace_stat.h"
  40. #define FTRACE_WARN_ON(cond) \
  41. ({ \
  42. int ___r = cond; \
  43. if (WARN_ON(___r)) \
  44. ftrace_kill(); \
  45. ___r; \
  46. })
  47. #define FTRACE_WARN_ON_ONCE(cond) \
  48. ({ \
  49. int ___r = cond; \
  50. if (WARN_ON_ONCE(___r)) \
  51. ftrace_kill(); \
  52. ___r; \
  53. })
  54. /* hash bits for specific function selection */
  55. #define FTRACE_HASH_BITS 7
  56. #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
  57. #define FTRACE_HASH_DEFAULT_BITS 10
  58. #define FTRACE_HASH_MAX_BITS 12
  59. #ifdef CONFIG_DYNAMIC_FTRACE
  60. #define INIT_OPS_HASH(opsname) \
  61. .func_hash = &opsname.local_hash, \
  62. .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
  63. #define ASSIGN_OPS_HASH(opsname, val) \
  64. .func_hash = val, \
  65. .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
  66. #else
  67. #define INIT_OPS_HASH(opsname)
  68. #define ASSIGN_OPS_HASH(opsname, val)
  69. #endif
  70. static struct ftrace_ops ftrace_list_end __read_mostly = {
  71. .func = ftrace_stub,
  72. .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
  73. INIT_OPS_HASH(ftrace_list_end)
  74. };
  75. /* ftrace_enabled is a method to turn ftrace on or off */
  76. int ftrace_enabled __read_mostly;
  77. static int last_ftrace_enabled;
  78. /* Current function tracing op */
  79. struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
  80. /* What to set function_trace_op to */
  81. static struct ftrace_ops *set_function_trace_op;
  82. static bool ftrace_pids_enabled(struct ftrace_ops *ops)
  83. {
  84. struct trace_array *tr;
  85. if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
  86. return false;
  87. tr = ops->private;
  88. return tr->function_pids != NULL;
  89. }
  90. static void ftrace_update_trampoline(struct ftrace_ops *ops);
  91. /*
  92. * ftrace_disabled is set when an anomaly is discovered.
  93. * ftrace_disabled is much stronger than ftrace_enabled.
  94. */
  95. static int ftrace_disabled __read_mostly;
  96. static DEFINE_MUTEX(ftrace_lock);
  97. static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
  98. ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
  99. static struct ftrace_ops global_ops;
  100. #if ARCH_SUPPORTS_FTRACE_OPS
  101. static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
  102. struct ftrace_ops *op, struct pt_regs *regs);
  103. #else
  104. /* See comment below, where ftrace_ops_list_func is defined */
  105. static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
  106. #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
  107. #endif
  108. /*
  109. * Traverse the ftrace_global_list, invoking all entries. The reason that we
  110. * can use rcu_dereference_raw_notrace() is that elements removed from this list
  111. * are simply leaked, so there is no need to interact with a grace-period
  112. * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
  113. * concurrent insertions into the ftrace_global_list.
  114. *
  115. * Silly Alpha and silly pointer-speculation compiler optimizations!
  116. */
  117. #define do_for_each_ftrace_op(op, list) \
  118. op = rcu_dereference_raw_notrace(list); \
  119. do
  120. /*
  121. * Optimized for just a single item in the list (as that is the normal case).
  122. */
  123. #define while_for_each_ftrace_op(op) \
  124. while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
  125. unlikely((op) != &ftrace_list_end))
  126. static inline void ftrace_ops_init(struct ftrace_ops *ops)
  127. {
  128. #ifdef CONFIG_DYNAMIC_FTRACE
  129. if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
  130. mutex_init(&ops->local_hash.regex_lock);
  131. ops->func_hash = &ops->local_hash;
  132. ops->flags |= FTRACE_OPS_FL_INITIALIZED;
  133. }
  134. #endif
  135. }
  136. /**
  137. * ftrace_nr_registered_ops - return number of ops registered
  138. *
  139. * Returns the number of ftrace_ops registered and tracing functions
  140. */
  141. int ftrace_nr_registered_ops(void)
  142. {
  143. struct ftrace_ops *ops;
  144. int cnt = 0;
  145. mutex_lock(&ftrace_lock);
  146. for (ops = ftrace_ops_list;
  147. ops != &ftrace_list_end; ops = ops->next)
  148. cnt++;
  149. mutex_unlock(&ftrace_lock);
  150. return cnt;
  151. }
  152. static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
  153. struct ftrace_ops *op, struct pt_regs *regs)
  154. {
  155. struct trace_array *tr = op->private;
  156. if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid))
  157. return;
  158. op->saved_func(ip, parent_ip, op, regs);
  159. }
  160. /**
  161. * clear_ftrace_function - reset the ftrace function
  162. *
  163. * This NULLs the ftrace function and in essence stops
  164. * tracing. There may be lag
  165. */
  166. void clear_ftrace_function(void)
  167. {
  168. ftrace_trace_function = ftrace_stub;
  169. }
  170. static void per_cpu_ops_disable_all(struct ftrace_ops *ops)
  171. {
  172. int cpu;
  173. for_each_possible_cpu(cpu)
  174. *per_cpu_ptr(ops->disabled, cpu) = 1;
  175. }
  176. static int per_cpu_ops_alloc(struct ftrace_ops *ops)
  177. {
  178. int __percpu *disabled;
  179. if (WARN_ON_ONCE(!(ops->flags & FTRACE_OPS_FL_PER_CPU)))
  180. return -EINVAL;
  181. disabled = alloc_percpu(int);
  182. if (!disabled)
  183. return -ENOMEM;
  184. ops->disabled = disabled;
  185. per_cpu_ops_disable_all(ops);
  186. return 0;
  187. }
  188. static void ftrace_sync(struct work_struct *work)
  189. {
  190. /*
  191. * This function is just a stub to implement a hard force
  192. * of synchronize_sched(). This requires synchronizing
  193. * tasks even in userspace and idle.
  194. *
  195. * Yes, function tracing is rude.
  196. */
  197. }
  198. static void ftrace_sync_ipi(void *data)
  199. {
  200. /* Probably not needed, but do it anyway */
  201. smp_rmb();
  202. }
  203. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  204. static void update_function_graph_func(void);
  205. /* Both enabled by default (can be cleared by function_graph tracer flags */
  206. static bool fgraph_sleep_time = true;
  207. static bool fgraph_graph_time = true;
  208. #else
  209. static inline void update_function_graph_func(void) { }
  210. #endif
  211. static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
  212. {
  213. /*
  214. * If this is a dynamic, RCU, or per CPU ops, or we force list func,
  215. * then it needs to call the list anyway.
  216. */
  217. if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU |
  218. FTRACE_OPS_FL_RCU) || FTRACE_FORCE_LIST_FUNC)
  219. return ftrace_ops_list_func;
  220. return ftrace_ops_get_func(ops);
  221. }
  222. static void update_ftrace_function(void)
  223. {
  224. ftrace_func_t func;
  225. /*
  226. * Prepare the ftrace_ops that the arch callback will use.
  227. * If there's only one ftrace_ops registered, the ftrace_ops_list
  228. * will point to the ops we want.
  229. */
  230. set_function_trace_op = ftrace_ops_list;
  231. /* If there's no ftrace_ops registered, just call the stub function */
  232. if (ftrace_ops_list == &ftrace_list_end) {
  233. func = ftrace_stub;
  234. /*
  235. * If we are at the end of the list and this ops is
  236. * recursion safe and not dynamic and the arch supports passing ops,
  237. * then have the mcount trampoline call the function directly.
  238. */
  239. } else if (ftrace_ops_list->next == &ftrace_list_end) {
  240. func = ftrace_ops_get_list_func(ftrace_ops_list);
  241. } else {
  242. /* Just use the default ftrace_ops */
  243. set_function_trace_op = &ftrace_list_end;
  244. func = ftrace_ops_list_func;
  245. }
  246. update_function_graph_func();
  247. /* If there's no change, then do nothing more here */
  248. if (ftrace_trace_function == func)
  249. return;
  250. /*
  251. * If we are using the list function, it doesn't care
  252. * about the function_trace_ops.
  253. */
  254. if (func == ftrace_ops_list_func) {
  255. ftrace_trace_function = func;
  256. /*
  257. * Don't even bother setting function_trace_ops,
  258. * it would be racy to do so anyway.
  259. */
  260. return;
  261. }
  262. #ifndef CONFIG_DYNAMIC_FTRACE
  263. /*
  264. * For static tracing, we need to be a bit more careful.
  265. * The function change takes affect immediately. Thus,
  266. * we need to coorditate the setting of the function_trace_ops
  267. * with the setting of the ftrace_trace_function.
  268. *
  269. * Set the function to the list ops, which will call the
  270. * function we want, albeit indirectly, but it handles the
  271. * ftrace_ops and doesn't depend on function_trace_op.
  272. */
  273. ftrace_trace_function = ftrace_ops_list_func;
  274. /*
  275. * Make sure all CPUs see this. Yes this is slow, but static
  276. * tracing is slow and nasty to have enabled.
  277. */
  278. schedule_on_each_cpu(ftrace_sync);
  279. /* Now all cpus are using the list ops. */
  280. function_trace_op = set_function_trace_op;
  281. /* Make sure the function_trace_op is visible on all CPUs */
  282. smp_wmb();
  283. /* Nasty way to force a rmb on all cpus */
  284. smp_call_function(ftrace_sync_ipi, NULL, 1);
  285. /* OK, we are all set to update the ftrace_trace_function now! */
  286. #endif /* !CONFIG_DYNAMIC_FTRACE */
  287. ftrace_trace_function = func;
  288. }
  289. int using_ftrace_ops_list_func(void)
  290. {
  291. return ftrace_trace_function == ftrace_ops_list_func;
  292. }
  293. static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
  294. {
  295. ops->next = *list;
  296. /*
  297. * We are entering ops into the list but another
  298. * CPU might be walking that list. We need to make sure
  299. * the ops->next pointer is valid before another CPU sees
  300. * the ops pointer included into the list.
  301. */
  302. rcu_assign_pointer(*list, ops);
  303. }
  304. static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
  305. {
  306. struct ftrace_ops **p;
  307. /*
  308. * If we are removing the last function, then simply point
  309. * to the ftrace_stub.
  310. */
  311. if (*list == ops && ops->next == &ftrace_list_end) {
  312. *list = &ftrace_list_end;
  313. return 0;
  314. }
  315. for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
  316. if (*p == ops)
  317. break;
  318. if (*p != ops)
  319. return -1;
  320. *p = (*p)->next;
  321. return 0;
  322. }
  323. static void ftrace_update_trampoline(struct ftrace_ops *ops);
  324. static int __register_ftrace_function(struct ftrace_ops *ops)
  325. {
  326. if (ops->flags & FTRACE_OPS_FL_DELETED)
  327. return -EINVAL;
  328. if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
  329. return -EBUSY;
  330. #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  331. /*
  332. * If the ftrace_ops specifies SAVE_REGS, then it only can be used
  333. * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
  334. * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
  335. */
  336. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
  337. !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
  338. return -EINVAL;
  339. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
  340. ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
  341. #endif
  342. if (!core_kernel_data((unsigned long)ops))
  343. ops->flags |= FTRACE_OPS_FL_DYNAMIC;
  344. if (ops->flags & FTRACE_OPS_FL_PER_CPU) {
  345. if (per_cpu_ops_alloc(ops))
  346. return -ENOMEM;
  347. }
  348. add_ftrace_ops(&ftrace_ops_list, ops);
  349. /* Always save the function, and reset at unregistering */
  350. ops->saved_func = ops->func;
  351. if (ftrace_pids_enabled(ops))
  352. ops->func = ftrace_pid_func;
  353. ftrace_update_trampoline(ops);
  354. if (ftrace_enabled)
  355. update_ftrace_function();
  356. return 0;
  357. }
  358. static int __unregister_ftrace_function(struct ftrace_ops *ops)
  359. {
  360. int ret;
  361. if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
  362. return -EBUSY;
  363. ret = remove_ftrace_ops(&ftrace_ops_list, ops);
  364. if (ret < 0)
  365. return ret;
  366. if (ftrace_enabled)
  367. update_ftrace_function();
  368. ops->func = ops->saved_func;
  369. return 0;
  370. }
  371. static void ftrace_update_pid_func(void)
  372. {
  373. struct ftrace_ops *op;
  374. /* Only do something if we are tracing something */
  375. if (ftrace_trace_function == ftrace_stub)
  376. return;
  377. do_for_each_ftrace_op(op, ftrace_ops_list) {
  378. if (op->flags & FTRACE_OPS_FL_PID) {
  379. op->func = ftrace_pids_enabled(op) ?
  380. ftrace_pid_func : op->saved_func;
  381. ftrace_update_trampoline(op);
  382. }
  383. } while_for_each_ftrace_op(op);
  384. update_ftrace_function();
  385. }
  386. #ifdef CONFIG_FUNCTION_PROFILER
  387. struct ftrace_profile {
  388. struct hlist_node node;
  389. unsigned long ip;
  390. unsigned long counter;
  391. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  392. unsigned long long time;
  393. unsigned long long time_squared;
  394. #endif
  395. };
  396. struct ftrace_profile_page {
  397. struct ftrace_profile_page *next;
  398. unsigned long index;
  399. struct ftrace_profile records[];
  400. };
  401. struct ftrace_profile_stat {
  402. atomic_t disabled;
  403. struct hlist_head *hash;
  404. struct ftrace_profile_page *pages;
  405. struct ftrace_profile_page *start;
  406. struct tracer_stat stat;
  407. };
  408. #define PROFILE_RECORDS_SIZE \
  409. (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
  410. #define PROFILES_PER_PAGE \
  411. (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
  412. static int ftrace_profile_enabled __read_mostly;
  413. /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
  414. static DEFINE_MUTEX(ftrace_profile_lock);
  415. static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
  416. #define FTRACE_PROFILE_HASH_BITS 10
  417. #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
  418. static void *
  419. function_stat_next(void *v, int idx)
  420. {
  421. struct ftrace_profile *rec = v;
  422. struct ftrace_profile_page *pg;
  423. pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
  424. again:
  425. if (idx != 0)
  426. rec++;
  427. if ((void *)rec >= (void *)&pg->records[pg->index]) {
  428. pg = pg->next;
  429. if (!pg)
  430. return NULL;
  431. rec = &pg->records[0];
  432. if (!rec->counter)
  433. goto again;
  434. }
  435. return rec;
  436. }
  437. static void *function_stat_start(struct tracer_stat *trace)
  438. {
  439. struct ftrace_profile_stat *stat =
  440. container_of(trace, struct ftrace_profile_stat, stat);
  441. if (!stat || !stat->start)
  442. return NULL;
  443. return function_stat_next(&stat->start->records[0], 0);
  444. }
  445. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  446. /* function graph compares on total time */
  447. static int function_stat_cmp(void *p1, void *p2)
  448. {
  449. struct ftrace_profile *a = p1;
  450. struct ftrace_profile *b = p2;
  451. if (a->time < b->time)
  452. return -1;
  453. if (a->time > b->time)
  454. return 1;
  455. else
  456. return 0;
  457. }
  458. #else
  459. /* not function graph compares against hits */
  460. static int function_stat_cmp(void *p1, void *p2)
  461. {
  462. struct ftrace_profile *a = p1;
  463. struct ftrace_profile *b = p2;
  464. if (a->counter < b->counter)
  465. return -1;
  466. if (a->counter > b->counter)
  467. return 1;
  468. else
  469. return 0;
  470. }
  471. #endif
  472. static int function_stat_headers(struct seq_file *m)
  473. {
  474. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  475. seq_puts(m, " Function "
  476. "Hit Time Avg s^2\n"
  477. " -------- "
  478. "--- ---- --- ---\n");
  479. #else
  480. seq_puts(m, " Function Hit\n"
  481. " -------- ---\n");
  482. #endif
  483. return 0;
  484. }
  485. static int function_stat_show(struct seq_file *m, void *v)
  486. {
  487. struct ftrace_profile *rec = v;
  488. char str[KSYM_SYMBOL_LEN];
  489. int ret = 0;
  490. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  491. static struct trace_seq s;
  492. unsigned long long avg;
  493. unsigned long long stddev;
  494. #endif
  495. mutex_lock(&ftrace_profile_lock);
  496. /* we raced with function_profile_reset() */
  497. if (unlikely(rec->counter == 0)) {
  498. ret = -EBUSY;
  499. goto out;
  500. }
  501. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  502. avg = rec->time;
  503. do_div(avg, rec->counter);
  504. if (tracing_thresh && (avg < tracing_thresh))
  505. goto out;
  506. #endif
  507. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  508. seq_printf(m, " %-30.30s %10lu", str, rec->counter);
  509. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  510. seq_puts(m, " ");
  511. /* Sample standard deviation (s^2) */
  512. if (rec->counter <= 1)
  513. stddev = 0;
  514. else {
  515. /*
  516. * Apply Welford's method:
  517. * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
  518. */
  519. stddev = rec->counter * rec->time_squared -
  520. rec->time * rec->time;
  521. /*
  522. * Divide only 1000 for ns^2 -> us^2 conversion.
  523. * trace_print_graph_duration will divide 1000 again.
  524. */
  525. do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
  526. }
  527. trace_seq_init(&s);
  528. trace_print_graph_duration(rec->time, &s);
  529. trace_seq_puts(&s, " ");
  530. trace_print_graph_duration(avg, &s);
  531. trace_seq_puts(&s, " ");
  532. trace_print_graph_duration(stddev, &s);
  533. trace_print_seq(m, &s);
  534. #endif
  535. seq_putc(m, '\n');
  536. out:
  537. mutex_unlock(&ftrace_profile_lock);
  538. return ret;
  539. }
  540. static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
  541. {
  542. struct ftrace_profile_page *pg;
  543. pg = stat->pages = stat->start;
  544. while (pg) {
  545. memset(pg->records, 0, PROFILE_RECORDS_SIZE);
  546. pg->index = 0;
  547. pg = pg->next;
  548. }
  549. memset(stat->hash, 0,
  550. FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
  551. }
  552. int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
  553. {
  554. struct ftrace_profile_page *pg;
  555. int functions;
  556. int pages;
  557. int i;
  558. /* If we already allocated, do nothing */
  559. if (stat->pages)
  560. return 0;
  561. stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
  562. if (!stat->pages)
  563. return -ENOMEM;
  564. #ifdef CONFIG_DYNAMIC_FTRACE
  565. functions = ftrace_update_tot_cnt;
  566. #else
  567. /*
  568. * We do not know the number of functions that exist because
  569. * dynamic tracing is what counts them. With past experience
  570. * we have around 20K functions. That should be more than enough.
  571. * It is highly unlikely we will execute every function in
  572. * the kernel.
  573. */
  574. functions = 20000;
  575. #endif
  576. pg = stat->start = stat->pages;
  577. pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
  578. for (i = 1; i < pages; i++) {
  579. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  580. if (!pg->next)
  581. goto out_free;
  582. pg = pg->next;
  583. }
  584. return 0;
  585. out_free:
  586. pg = stat->start;
  587. while (pg) {
  588. unsigned long tmp = (unsigned long)pg;
  589. pg = pg->next;
  590. free_page(tmp);
  591. }
  592. stat->pages = NULL;
  593. stat->start = NULL;
  594. return -ENOMEM;
  595. }
  596. static int ftrace_profile_init_cpu(int cpu)
  597. {
  598. struct ftrace_profile_stat *stat;
  599. int size;
  600. stat = &per_cpu(ftrace_profile_stats, cpu);
  601. if (stat->hash) {
  602. /* If the profile is already created, simply reset it */
  603. ftrace_profile_reset(stat);
  604. return 0;
  605. }
  606. /*
  607. * We are profiling all functions, but usually only a few thousand
  608. * functions are hit. We'll make a hash of 1024 items.
  609. */
  610. size = FTRACE_PROFILE_HASH_SIZE;
  611. stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
  612. if (!stat->hash)
  613. return -ENOMEM;
  614. /* Preallocate the function profiling pages */
  615. if (ftrace_profile_pages_init(stat) < 0) {
  616. kfree(stat->hash);
  617. stat->hash = NULL;
  618. return -ENOMEM;
  619. }
  620. return 0;
  621. }
  622. static int ftrace_profile_init(void)
  623. {
  624. int cpu;
  625. int ret = 0;
  626. for_each_possible_cpu(cpu) {
  627. ret = ftrace_profile_init_cpu(cpu);
  628. if (ret)
  629. break;
  630. }
  631. return ret;
  632. }
  633. /* interrupts must be disabled */
  634. static struct ftrace_profile *
  635. ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
  636. {
  637. struct ftrace_profile *rec;
  638. struct hlist_head *hhd;
  639. unsigned long key;
  640. key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
  641. hhd = &stat->hash[key];
  642. if (hlist_empty(hhd))
  643. return NULL;
  644. hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
  645. if (rec->ip == ip)
  646. return rec;
  647. }
  648. return NULL;
  649. }
  650. static void ftrace_add_profile(struct ftrace_profile_stat *stat,
  651. struct ftrace_profile *rec)
  652. {
  653. unsigned long key;
  654. key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
  655. hlist_add_head_rcu(&rec->node, &stat->hash[key]);
  656. }
  657. /*
  658. * The memory is already allocated, this simply finds a new record to use.
  659. */
  660. static struct ftrace_profile *
  661. ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
  662. {
  663. struct ftrace_profile *rec = NULL;
  664. /* prevent recursion (from NMIs) */
  665. if (atomic_inc_return(&stat->disabled) != 1)
  666. goto out;
  667. /*
  668. * Try to find the function again since an NMI
  669. * could have added it
  670. */
  671. rec = ftrace_find_profiled_func(stat, ip);
  672. if (rec)
  673. goto out;
  674. if (stat->pages->index == PROFILES_PER_PAGE) {
  675. if (!stat->pages->next)
  676. goto out;
  677. stat->pages = stat->pages->next;
  678. }
  679. rec = &stat->pages->records[stat->pages->index++];
  680. rec->ip = ip;
  681. ftrace_add_profile(stat, rec);
  682. out:
  683. atomic_dec(&stat->disabled);
  684. return rec;
  685. }
  686. static void
  687. function_profile_call(unsigned long ip, unsigned long parent_ip,
  688. struct ftrace_ops *ops, struct pt_regs *regs)
  689. {
  690. struct ftrace_profile_stat *stat;
  691. struct ftrace_profile *rec;
  692. unsigned long flags;
  693. if (!ftrace_profile_enabled)
  694. return;
  695. local_irq_save(flags);
  696. stat = this_cpu_ptr(&ftrace_profile_stats);
  697. if (!stat->hash || !ftrace_profile_enabled)
  698. goto out;
  699. rec = ftrace_find_profiled_func(stat, ip);
  700. if (!rec) {
  701. rec = ftrace_profile_alloc(stat, ip);
  702. if (!rec)
  703. goto out;
  704. }
  705. rec->counter++;
  706. out:
  707. local_irq_restore(flags);
  708. }
  709. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  710. static int profile_graph_entry(struct ftrace_graph_ent *trace)
  711. {
  712. int index = trace->depth;
  713. function_profile_call(trace->func, 0, NULL, NULL);
  714. if (index >= 0 && index < FTRACE_RETFUNC_DEPTH)
  715. current->ret_stack[index].subtime = 0;
  716. return 1;
  717. }
  718. static void profile_graph_return(struct ftrace_graph_ret *trace)
  719. {
  720. struct ftrace_profile_stat *stat;
  721. unsigned long long calltime;
  722. struct ftrace_profile *rec;
  723. unsigned long flags;
  724. local_irq_save(flags);
  725. stat = this_cpu_ptr(&ftrace_profile_stats);
  726. if (!stat->hash || !ftrace_profile_enabled)
  727. goto out;
  728. /* If the calltime was zero'd ignore it */
  729. if (!trace->calltime)
  730. goto out;
  731. calltime = trace->rettime - trace->calltime;
  732. if (!fgraph_graph_time) {
  733. int index;
  734. index = trace->depth;
  735. /* Append this call time to the parent time to subtract */
  736. if (index)
  737. current->ret_stack[index - 1].subtime += calltime;
  738. if (current->ret_stack[index].subtime < calltime)
  739. calltime -= current->ret_stack[index].subtime;
  740. else
  741. calltime = 0;
  742. }
  743. rec = ftrace_find_profiled_func(stat, trace->func);
  744. if (rec) {
  745. rec->time += calltime;
  746. rec->time_squared += calltime * calltime;
  747. }
  748. out:
  749. local_irq_restore(flags);
  750. }
  751. static int register_ftrace_profiler(void)
  752. {
  753. return register_ftrace_graph(&profile_graph_return,
  754. &profile_graph_entry);
  755. }
  756. static void unregister_ftrace_profiler(void)
  757. {
  758. unregister_ftrace_graph();
  759. }
  760. #else
  761. static struct ftrace_ops ftrace_profile_ops __read_mostly = {
  762. .func = function_profile_call,
  763. .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
  764. INIT_OPS_HASH(ftrace_profile_ops)
  765. };
  766. static int register_ftrace_profiler(void)
  767. {
  768. return register_ftrace_function(&ftrace_profile_ops);
  769. }
  770. static void unregister_ftrace_profiler(void)
  771. {
  772. unregister_ftrace_function(&ftrace_profile_ops);
  773. }
  774. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  775. static ssize_t
  776. ftrace_profile_write(struct file *filp, const char __user *ubuf,
  777. size_t cnt, loff_t *ppos)
  778. {
  779. unsigned long val;
  780. int ret;
  781. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  782. if (ret)
  783. return ret;
  784. val = !!val;
  785. mutex_lock(&ftrace_profile_lock);
  786. if (ftrace_profile_enabled ^ val) {
  787. if (val) {
  788. ret = ftrace_profile_init();
  789. if (ret < 0) {
  790. cnt = ret;
  791. goto out;
  792. }
  793. ret = register_ftrace_profiler();
  794. if (ret < 0) {
  795. cnt = ret;
  796. goto out;
  797. }
  798. ftrace_profile_enabled = 1;
  799. } else {
  800. ftrace_profile_enabled = 0;
  801. /*
  802. * unregister_ftrace_profiler calls stop_machine
  803. * so this acts like an synchronize_sched.
  804. */
  805. unregister_ftrace_profiler();
  806. }
  807. }
  808. out:
  809. mutex_unlock(&ftrace_profile_lock);
  810. *ppos += cnt;
  811. return cnt;
  812. }
  813. static ssize_t
  814. ftrace_profile_read(struct file *filp, char __user *ubuf,
  815. size_t cnt, loff_t *ppos)
  816. {
  817. char buf[64]; /* big enough to hold a number */
  818. int r;
  819. r = sprintf(buf, "%u\n", ftrace_profile_enabled);
  820. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  821. }
  822. static const struct file_operations ftrace_profile_fops = {
  823. .open = tracing_open_generic,
  824. .read = ftrace_profile_read,
  825. .write = ftrace_profile_write,
  826. .llseek = default_llseek,
  827. };
  828. /* used to initialize the real stat files */
  829. static struct tracer_stat function_stats __initdata = {
  830. .name = "functions",
  831. .stat_start = function_stat_start,
  832. .stat_next = function_stat_next,
  833. .stat_cmp = function_stat_cmp,
  834. .stat_headers = function_stat_headers,
  835. .stat_show = function_stat_show
  836. };
  837. static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
  838. {
  839. struct ftrace_profile_stat *stat;
  840. struct dentry *entry;
  841. char *name;
  842. int ret;
  843. int cpu;
  844. for_each_possible_cpu(cpu) {
  845. stat = &per_cpu(ftrace_profile_stats, cpu);
  846. name = kasprintf(GFP_KERNEL, "function%d", cpu);
  847. if (!name) {
  848. /*
  849. * The files created are permanent, if something happens
  850. * we still do not free memory.
  851. */
  852. WARN(1,
  853. "Could not allocate stat file for cpu %d\n",
  854. cpu);
  855. return;
  856. }
  857. stat->stat = function_stats;
  858. stat->stat.name = name;
  859. ret = register_stat_tracer(&stat->stat);
  860. if (ret) {
  861. WARN(1,
  862. "Could not register function stat for cpu %d\n",
  863. cpu);
  864. kfree(name);
  865. return;
  866. }
  867. }
  868. entry = tracefs_create_file("function_profile_enabled", 0644,
  869. d_tracer, NULL, &ftrace_profile_fops);
  870. if (!entry)
  871. pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
  872. }
  873. #else /* CONFIG_FUNCTION_PROFILER */
  874. static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
  875. {
  876. }
  877. #endif /* CONFIG_FUNCTION_PROFILER */
  878. static struct pid * const ftrace_swapper_pid = &init_struct_pid;
  879. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  880. static int ftrace_graph_active;
  881. #else
  882. # define ftrace_graph_active 0
  883. #endif
  884. #ifdef CONFIG_DYNAMIC_FTRACE
  885. static struct ftrace_ops *removed_ops;
  886. /*
  887. * Set when doing a global update, like enabling all recs or disabling them.
  888. * It is not set when just updating a single ftrace_ops.
  889. */
  890. static bool update_all_ops;
  891. #ifndef CONFIG_FTRACE_MCOUNT_RECORD
  892. # error Dynamic ftrace depends on MCOUNT_RECORD
  893. #endif
  894. struct ftrace_func_entry {
  895. struct hlist_node hlist;
  896. unsigned long ip;
  897. };
  898. struct ftrace_func_probe {
  899. struct ftrace_probe_ops *probe_ops;
  900. struct ftrace_ops ops;
  901. struct trace_array *tr;
  902. struct list_head list;
  903. void *data;
  904. int ref;
  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_rcu(&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. if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU)) {
  2335. /*
  2336. * We need to do a hard force of sched synchronization.
  2337. * This is because we use preempt_disable() to do RCU, but
  2338. * the function tracers can be called where RCU is not watching
  2339. * (like before user_exit()). We can not rely on the RCU
  2340. * infrastructure to do the synchronization, thus we must do it
  2341. * ourselves.
  2342. */
  2343. schedule_on_each_cpu(ftrace_sync);
  2344. /*
  2345. * When the kernel is preeptive, tasks can be preempted
  2346. * while on a ftrace trampoline. Just scheduling a task on
  2347. * a CPU is not good enough to flush them. Calling
  2348. * synchornize_rcu_tasks() will wait for those tasks to
  2349. * execute and either schedule voluntarily or enter user space.
  2350. */
  2351. if (IS_ENABLED(CONFIG_PREEMPT))
  2352. synchronize_rcu_tasks();
  2353. arch_ftrace_trampoline_free(ops);
  2354. if (ops->flags & FTRACE_OPS_FL_PER_CPU)
  2355. per_cpu_ops_free(ops);
  2356. }
  2357. return 0;
  2358. }
  2359. static void ftrace_startup_sysctl(void)
  2360. {
  2361. int command;
  2362. if (unlikely(ftrace_disabled))
  2363. return;
  2364. /* Force update next time */
  2365. saved_ftrace_func = NULL;
  2366. /* ftrace_start_up is true if we want ftrace running */
  2367. if (ftrace_start_up) {
  2368. command = FTRACE_UPDATE_CALLS;
  2369. if (ftrace_graph_active)
  2370. command |= FTRACE_START_FUNC_RET;
  2371. ftrace_startup_enable(command);
  2372. }
  2373. }
  2374. static void ftrace_shutdown_sysctl(void)
  2375. {
  2376. int command;
  2377. if (unlikely(ftrace_disabled))
  2378. return;
  2379. /* ftrace_start_up is true if ftrace is running */
  2380. if (ftrace_start_up) {
  2381. command = FTRACE_DISABLE_CALLS;
  2382. if (ftrace_graph_active)
  2383. command |= FTRACE_STOP_FUNC_RET;
  2384. ftrace_run_update_code(command);
  2385. }
  2386. }
  2387. static u64 ftrace_update_time;
  2388. unsigned long ftrace_update_tot_cnt;
  2389. static inline int ops_traces_mod(struct ftrace_ops *ops)
  2390. {
  2391. /*
  2392. * Filter_hash being empty will default to trace module.
  2393. * But notrace hash requires a test of individual module functions.
  2394. */
  2395. return ftrace_hash_empty(ops->func_hash->filter_hash) &&
  2396. ftrace_hash_empty(ops->func_hash->notrace_hash);
  2397. }
  2398. /*
  2399. * Check if the current ops references the record.
  2400. *
  2401. * If the ops traces all functions, then it was already accounted for.
  2402. * If the ops does not trace the current record function, skip it.
  2403. * If the ops ignores the function via notrace filter, skip it.
  2404. */
  2405. static inline bool
  2406. ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
  2407. {
  2408. /* If ops isn't enabled, ignore it */
  2409. if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
  2410. return 0;
  2411. /* If ops traces all then it includes this function */
  2412. if (ops_traces_mod(ops))
  2413. return 1;
  2414. /* The function must be in the filter */
  2415. if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
  2416. !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
  2417. return 0;
  2418. /* If in notrace hash, we ignore it too */
  2419. if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
  2420. return 0;
  2421. return 1;
  2422. }
  2423. static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
  2424. {
  2425. struct ftrace_page *pg;
  2426. struct dyn_ftrace *p;
  2427. u64 start, stop;
  2428. unsigned long update_cnt = 0;
  2429. unsigned long rec_flags = 0;
  2430. int i;
  2431. start = ftrace_now(raw_smp_processor_id());
  2432. /*
  2433. * When a module is loaded, this function is called to convert
  2434. * the calls to mcount in its text to nops, and also to create
  2435. * an entry in the ftrace data. Now, if ftrace is activated
  2436. * after this call, but before the module sets its text to
  2437. * read-only, the modification of enabling ftrace can fail if
  2438. * the read-only is done while ftrace is converting the calls.
  2439. * To prevent this, the module's records are set as disabled
  2440. * and will be enabled after the call to set the module's text
  2441. * to read-only.
  2442. */
  2443. if (mod)
  2444. rec_flags |= FTRACE_FL_DISABLED;
  2445. for (pg = new_pgs; pg; pg = pg->next) {
  2446. for (i = 0; i < pg->index; i++) {
  2447. /* If something went wrong, bail without enabling anything */
  2448. if (unlikely(ftrace_disabled))
  2449. return -1;
  2450. p = &pg->records[i];
  2451. p->flags = rec_flags;
  2452. /*
  2453. * Do the initial record conversion from mcount jump
  2454. * to the NOP instructions.
  2455. */
  2456. if (!ftrace_code_disable(mod, p))
  2457. break;
  2458. update_cnt++;
  2459. }
  2460. }
  2461. stop = ftrace_now(raw_smp_processor_id());
  2462. ftrace_update_time = stop - start;
  2463. ftrace_update_tot_cnt += update_cnt;
  2464. return 0;
  2465. }
  2466. static int ftrace_allocate_records(struct ftrace_page *pg, int count)
  2467. {
  2468. int order;
  2469. int cnt;
  2470. if (WARN_ON(!count))
  2471. return -EINVAL;
  2472. order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
  2473. /*
  2474. * We want to fill as much as possible. No more than a page
  2475. * may be empty.
  2476. */
  2477. while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
  2478. order--;
  2479. again:
  2480. pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
  2481. if (!pg->records) {
  2482. /* if we can't allocate this size, try something smaller */
  2483. if (!order)
  2484. return -ENOMEM;
  2485. order >>= 1;
  2486. goto again;
  2487. }
  2488. cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
  2489. pg->size = cnt;
  2490. if (cnt > count)
  2491. cnt = count;
  2492. return cnt;
  2493. }
  2494. static struct ftrace_page *
  2495. ftrace_allocate_pages(unsigned long num_to_init)
  2496. {
  2497. struct ftrace_page *start_pg;
  2498. struct ftrace_page *pg;
  2499. int order;
  2500. int cnt;
  2501. if (!num_to_init)
  2502. return 0;
  2503. start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
  2504. if (!pg)
  2505. return NULL;
  2506. /*
  2507. * Try to allocate as much as possible in one continues
  2508. * location that fills in all of the space. We want to
  2509. * waste as little space as possible.
  2510. */
  2511. for (;;) {
  2512. cnt = ftrace_allocate_records(pg, num_to_init);
  2513. if (cnt < 0)
  2514. goto free_pages;
  2515. num_to_init -= cnt;
  2516. if (!num_to_init)
  2517. break;
  2518. pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
  2519. if (!pg->next)
  2520. goto free_pages;
  2521. pg = pg->next;
  2522. }
  2523. return start_pg;
  2524. free_pages:
  2525. pg = start_pg;
  2526. while (pg) {
  2527. order = get_count_order(pg->size / ENTRIES_PER_PAGE);
  2528. free_pages((unsigned long)pg->records, order);
  2529. start_pg = pg->next;
  2530. kfree(pg);
  2531. pg = start_pg;
  2532. }
  2533. pr_info("ftrace: FAILED to allocate memory for functions\n");
  2534. return NULL;
  2535. }
  2536. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  2537. struct ftrace_iterator {
  2538. loff_t pos;
  2539. loff_t func_pos;
  2540. struct ftrace_page *pg;
  2541. struct dyn_ftrace *func;
  2542. struct ftrace_func_probe *probe;
  2543. struct ftrace_func_entry *probe_entry;
  2544. struct trace_parser parser;
  2545. struct ftrace_hash *hash;
  2546. struct ftrace_ops *ops;
  2547. int pidx;
  2548. int idx;
  2549. unsigned flags;
  2550. };
  2551. static void *
  2552. t_probe_next(struct seq_file *m, loff_t *pos)
  2553. {
  2554. struct ftrace_iterator *iter = m->private;
  2555. struct trace_array *tr = iter->ops->private;
  2556. struct list_head *func_probes;
  2557. struct ftrace_hash *hash;
  2558. struct list_head *next;
  2559. struct hlist_node *hnd = NULL;
  2560. struct hlist_head *hhd;
  2561. int size;
  2562. (*pos)++;
  2563. iter->pos = *pos;
  2564. if (!tr)
  2565. return NULL;
  2566. func_probes = &tr->func_probes;
  2567. if (list_empty(func_probes))
  2568. return NULL;
  2569. if (!iter->probe) {
  2570. next = func_probes->next;
  2571. iter->probe = list_entry(next, struct ftrace_func_probe, list);
  2572. }
  2573. if (iter->probe_entry)
  2574. hnd = &iter->probe_entry->hlist;
  2575. hash = iter->probe->ops.func_hash->filter_hash;
  2576. size = 1 << hash->size_bits;
  2577. retry:
  2578. if (iter->pidx >= size) {
  2579. if (iter->probe->list.next == func_probes)
  2580. return NULL;
  2581. next = iter->probe->list.next;
  2582. iter->probe = list_entry(next, struct ftrace_func_probe, list);
  2583. hash = iter->probe->ops.func_hash->filter_hash;
  2584. size = 1 << hash->size_bits;
  2585. iter->pidx = 0;
  2586. }
  2587. hhd = &hash->buckets[iter->pidx];
  2588. if (hlist_empty(hhd)) {
  2589. iter->pidx++;
  2590. hnd = NULL;
  2591. goto retry;
  2592. }
  2593. if (!hnd)
  2594. hnd = hhd->first;
  2595. else {
  2596. hnd = hnd->next;
  2597. if (!hnd) {
  2598. iter->pidx++;
  2599. goto retry;
  2600. }
  2601. }
  2602. if (WARN_ON_ONCE(!hnd))
  2603. return NULL;
  2604. iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
  2605. return iter;
  2606. }
  2607. static void *t_probe_start(struct seq_file *m, loff_t *pos)
  2608. {
  2609. struct ftrace_iterator *iter = m->private;
  2610. void *p = NULL;
  2611. loff_t l;
  2612. if (!(iter->flags & FTRACE_ITER_DO_PROBES))
  2613. return NULL;
  2614. if (iter->func_pos > *pos)
  2615. return NULL;
  2616. iter->probe = NULL;
  2617. iter->probe_entry = NULL;
  2618. iter->pidx = 0;
  2619. for (l = 0; l <= (*pos - iter->func_pos); ) {
  2620. p = t_probe_next(m, &l);
  2621. if (!p)
  2622. break;
  2623. }
  2624. if (!p)
  2625. return NULL;
  2626. /* Only set this if we have an item */
  2627. iter->flags |= FTRACE_ITER_PROBE;
  2628. return iter;
  2629. }
  2630. static int
  2631. t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
  2632. {
  2633. struct ftrace_func_entry *probe_entry;
  2634. struct ftrace_probe_ops *probe_ops;
  2635. struct ftrace_func_probe *probe;
  2636. probe = iter->probe;
  2637. probe_entry = iter->probe_entry;
  2638. if (WARN_ON_ONCE(!probe || !probe_entry))
  2639. return -EIO;
  2640. probe_ops = probe->probe_ops;
  2641. if (probe_ops->print)
  2642. return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
  2643. seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
  2644. (void *)probe_ops->func);
  2645. return 0;
  2646. }
  2647. static void *
  2648. t_func_next(struct seq_file *m, loff_t *pos)
  2649. {
  2650. struct ftrace_iterator *iter = m->private;
  2651. struct dyn_ftrace *rec = NULL;
  2652. (*pos)++;
  2653. retry:
  2654. if (iter->idx >= iter->pg->index) {
  2655. if (iter->pg->next) {
  2656. iter->pg = iter->pg->next;
  2657. iter->idx = 0;
  2658. goto retry;
  2659. }
  2660. } else {
  2661. rec = &iter->pg->records[iter->idx++];
  2662. if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
  2663. !ftrace_lookup_ip(iter->hash, rec->ip)) ||
  2664. ((iter->flags & FTRACE_ITER_ENABLED) &&
  2665. !(rec->flags & FTRACE_FL_ENABLED))) {
  2666. rec = NULL;
  2667. goto retry;
  2668. }
  2669. }
  2670. if (!rec)
  2671. return NULL;
  2672. iter->pos = iter->func_pos = *pos;
  2673. iter->func = rec;
  2674. return iter;
  2675. }
  2676. static void *
  2677. t_next(struct seq_file *m, void *v, loff_t *pos)
  2678. {
  2679. struct ftrace_iterator *iter = m->private;
  2680. loff_t l = *pos; /* t_hash_start() must use original pos */
  2681. void *ret;
  2682. if (unlikely(ftrace_disabled))
  2683. return NULL;
  2684. if (iter->flags & FTRACE_ITER_PROBE)
  2685. return t_probe_next(m, pos);
  2686. if (iter->flags & FTRACE_ITER_PRINTALL) {
  2687. /* next must increment pos, and t_probe_start does not */
  2688. (*pos)++;
  2689. return t_probe_start(m, &l);
  2690. }
  2691. ret = t_func_next(m, pos);
  2692. if (!ret)
  2693. return t_probe_start(m, &l);
  2694. return ret;
  2695. }
  2696. static void reset_iter_read(struct ftrace_iterator *iter)
  2697. {
  2698. iter->pos = 0;
  2699. iter->func_pos = 0;
  2700. iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE);
  2701. }
  2702. static void *t_start(struct seq_file *m, loff_t *pos)
  2703. {
  2704. struct ftrace_iterator *iter = m->private;
  2705. void *p = NULL;
  2706. loff_t l;
  2707. mutex_lock(&ftrace_lock);
  2708. if (unlikely(ftrace_disabled))
  2709. return NULL;
  2710. /*
  2711. * If an lseek was done, then reset and start from beginning.
  2712. */
  2713. if (*pos < iter->pos)
  2714. reset_iter_read(iter);
  2715. /*
  2716. * For set_ftrace_filter reading, if we have the filter
  2717. * off, we can short cut and just print out that all
  2718. * functions are enabled.
  2719. */
  2720. if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
  2721. ftrace_hash_empty(iter->hash)) {
  2722. iter->func_pos = 1; /* Account for the message */
  2723. if (*pos > 0)
  2724. return t_probe_start(m, pos);
  2725. iter->flags |= FTRACE_ITER_PRINTALL;
  2726. /* reset in case of seek/pread */
  2727. iter->flags &= ~FTRACE_ITER_PROBE;
  2728. return iter;
  2729. }
  2730. if (iter->flags & FTRACE_ITER_PROBE)
  2731. return t_probe_start(m, pos);
  2732. /*
  2733. * Unfortunately, we need to restart at ftrace_pages_start
  2734. * every time we let go of the ftrace_mutex. This is because
  2735. * those pointers can change without the lock.
  2736. */
  2737. iter->pg = ftrace_pages_start;
  2738. iter->idx = 0;
  2739. for (l = 0; l <= *pos; ) {
  2740. p = t_func_next(m, &l);
  2741. if (!p)
  2742. break;
  2743. }
  2744. if (!p)
  2745. return t_probe_start(m, pos);
  2746. return iter;
  2747. }
  2748. static void t_stop(struct seq_file *m, void *p)
  2749. {
  2750. mutex_unlock(&ftrace_lock);
  2751. }
  2752. void * __weak
  2753. arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
  2754. {
  2755. return NULL;
  2756. }
  2757. static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
  2758. struct dyn_ftrace *rec)
  2759. {
  2760. void *ptr;
  2761. ptr = arch_ftrace_trampoline_func(ops, rec);
  2762. if (ptr)
  2763. seq_printf(m, " ->%pS", ptr);
  2764. }
  2765. static int t_show(struct seq_file *m, void *v)
  2766. {
  2767. struct ftrace_iterator *iter = m->private;
  2768. struct dyn_ftrace *rec;
  2769. if (iter->flags & FTRACE_ITER_PROBE)
  2770. return t_probe_show(m, iter);
  2771. if (iter->flags & FTRACE_ITER_PRINTALL) {
  2772. if (iter->flags & FTRACE_ITER_NOTRACE)
  2773. seq_puts(m, "#### no functions disabled ####\n");
  2774. else
  2775. seq_puts(m, "#### all functions enabled ####\n");
  2776. return 0;
  2777. }
  2778. rec = iter->func;
  2779. if (!rec)
  2780. return 0;
  2781. seq_printf(m, "%ps", (void *)rec->ip);
  2782. if (iter->flags & FTRACE_ITER_ENABLED) {
  2783. struct ftrace_ops *ops;
  2784. seq_printf(m, " (%ld)%s%s",
  2785. ftrace_rec_count(rec),
  2786. rec->flags & FTRACE_FL_REGS ? " R" : " ",
  2787. rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
  2788. if (rec->flags & FTRACE_FL_TRAMP_EN) {
  2789. ops = ftrace_find_tramp_ops_any(rec);
  2790. if (ops) {
  2791. do {
  2792. seq_printf(m, "\ttramp: %pS (%pS)",
  2793. (void *)ops->trampoline,
  2794. (void *)ops->func);
  2795. add_trampoline_func(m, ops, rec);
  2796. ops = ftrace_find_tramp_ops_next(rec, ops);
  2797. } while (ops);
  2798. } else
  2799. seq_puts(m, "\ttramp: ERROR!");
  2800. } else {
  2801. add_trampoline_func(m, NULL, rec);
  2802. }
  2803. }
  2804. seq_putc(m, '\n');
  2805. return 0;
  2806. }
  2807. static const struct seq_operations show_ftrace_seq_ops = {
  2808. .start = t_start,
  2809. .next = t_next,
  2810. .stop = t_stop,
  2811. .show = t_show,
  2812. };
  2813. static int
  2814. ftrace_avail_open(struct inode *inode, struct file *file)
  2815. {
  2816. struct ftrace_iterator *iter;
  2817. if (unlikely(ftrace_disabled))
  2818. return -ENODEV;
  2819. iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
  2820. if (!iter)
  2821. return -ENOMEM;
  2822. iter->pg = ftrace_pages_start;
  2823. iter->ops = &global_ops;
  2824. return 0;
  2825. }
  2826. static int
  2827. ftrace_enabled_open(struct inode *inode, struct file *file)
  2828. {
  2829. struct ftrace_iterator *iter;
  2830. iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
  2831. if (!iter)
  2832. return -ENOMEM;
  2833. iter->pg = ftrace_pages_start;
  2834. iter->flags = FTRACE_ITER_ENABLED;
  2835. iter->ops = &global_ops;
  2836. return 0;
  2837. }
  2838. /**
  2839. * ftrace_regex_open - initialize function tracer filter files
  2840. * @ops: The ftrace_ops that hold the hash filters
  2841. * @flag: The type of filter to process
  2842. * @inode: The inode, usually passed in to your open routine
  2843. * @file: The file, usually passed in to your open routine
  2844. *
  2845. * ftrace_regex_open() initializes the filter files for the
  2846. * @ops. Depending on @flag it may process the filter hash or
  2847. * the notrace hash of @ops. With this called from the open
  2848. * routine, you can use ftrace_filter_write() for the write
  2849. * routine if @flag has FTRACE_ITER_FILTER set, or
  2850. * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
  2851. * tracing_lseek() should be used as the lseek routine, and
  2852. * release must call ftrace_regex_release().
  2853. */
  2854. int
  2855. ftrace_regex_open(struct ftrace_ops *ops, int flag,
  2856. struct inode *inode, struct file *file)
  2857. {
  2858. struct ftrace_iterator *iter;
  2859. struct ftrace_hash *hash;
  2860. int ret = 0;
  2861. ftrace_ops_init(ops);
  2862. if (unlikely(ftrace_disabled))
  2863. return -ENODEV;
  2864. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  2865. if (!iter)
  2866. return -ENOMEM;
  2867. if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
  2868. kfree(iter);
  2869. return -ENOMEM;
  2870. }
  2871. iter->ops = ops;
  2872. iter->flags = flag;
  2873. mutex_lock(&ops->func_hash->regex_lock);
  2874. if (flag & FTRACE_ITER_NOTRACE)
  2875. hash = ops->func_hash->notrace_hash;
  2876. else
  2877. hash = ops->func_hash->filter_hash;
  2878. if (file->f_mode & FMODE_WRITE) {
  2879. const int size_bits = FTRACE_HASH_DEFAULT_BITS;
  2880. if (file->f_flags & O_TRUNC)
  2881. iter->hash = alloc_ftrace_hash(size_bits);
  2882. else
  2883. iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
  2884. if (!iter->hash) {
  2885. trace_parser_put(&iter->parser);
  2886. kfree(iter);
  2887. ret = -ENOMEM;
  2888. goto out_unlock;
  2889. }
  2890. } else
  2891. iter->hash = hash;
  2892. if (file->f_mode & FMODE_READ) {
  2893. iter->pg = ftrace_pages_start;
  2894. ret = seq_open(file, &show_ftrace_seq_ops);
  2895. if (!ret) {
  2896. struct seq_file *m = file->private_data;
  2897. m->private = iter;
  2898. } else {
  2899. /* Failed */
  2900. free_ftrace_hash(iter->hash);
  2901. trace_parser_put(&iter->parser);
  2902. kfree(iter);
  2903. }
  2904. } else
  2905. file->private_data = iter;
  2906. out_unlock:
  2907. mutex_unlock(&ops->func_hash->regex_lock);
  2908. return ret;
  2909. }
  2910. static int
  2911. ftrace_filter_open(struct inode *inode, struct file *file)
  2912. {
  2913. struct ftrace_ops *ops = inode->i_private;
  2914. return ftrace_regex_open(ops,
  2915. FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
  2916. inode, file);
  2917. }
  2918. static int
  2919. ftrace_notrace_open(struct inode *inode, struct file *file)
  2920. {
  2921. struct ftrace_ops *ops = inode->i_private;
  2922. return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
  2923. inode, file);
  2924. }
  2925. /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
  2926. struct ftrace_glob {
  2927. char *search;
  2928. unsigned len;
  2929. int type;
  2930. };
  2931. /*
  2932. * If symbols in an architecture don't correspond exactly to the user-visible
  2933. * name of what they represent, it is possible to define this function to
  2934. * perform the necessary adjustments.
  2935. */
  2936. char * __weak arch_ftrace_match_adjust(char *str, const char *search)
  2937. {
  2938. return str;
  2939. }
  2940. static int ftrace_match(char *str, struct ftrace_glob *g)
  2941. {
  2942. int matched = 0;
  2943. int slen;
  2944. str = arch_ftrace_match_adjust(str, g->search);
  2945. switch (g->type) {
  2946. case MATCH_FULL:
  2947. if (strcmp(str, g->search) == 0)
  2948. matched = 1;
  2949. break;
  2950. case MATCH_FRONT_ONLY:
  2951. if (strncmp(str, g->search, g->len) == 0)
  2952. matched = 1;
  2953. break;
  2954. case MATCH_MIDDLE_ONLY:
  2955. if (strstr(str, g->search))
  2956. matched = 1;
  2957. break;
  2958. case MATCH_END_ONLY:
  2959. slen = strlen(str);
  2960. if (slen >= g->len &&
  2961. memcmp(str + slen - g->len, g->search, g->len) == 0)
  2962. matched = 1;
  2963. break;
  2964. case MATCH_GLOB:
  2965. if (glob_match(g->search, str))
  2966. matched = 1;
  2967. break;
  2968. }
  2969. return matched;
  2970. }
  2971. static int
  2972. enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
  2973. {
  2974. struct ftrace_func_entry *entry;
  2975. int ret = 0;
  2976. entry = ftrace_lookup_ip(hash, rec->ip);
  2977. if (clear_filter) {
  2978. /* Do nothing if it doesn't exist */
  2979. if (!entry)
  2980. return 0;
  2981. free_hash_entry(hash, entry);
  2982. } else {
  2983. /* Do nothing if it exists */
  2984. if (entry)
  2985. return 0;
  2986. ret = add_hash_entry(hash, rec->ip);
  2987. }
  2988. return ret;
  2989. }
  2990. static int
  2991. ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
  2992. struct ftrace_glob *mod_g, int exclude_mod)
  2993. {
  2994. char str[KSYM_SYMBOL_LEN];
  2995. char *modname;
  2996. kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
  2997. if (mod_g) {
  2998. int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
  2999. /* blank module name to match all modules */
  3000. if (!mod_g->len) {
  3001. /* blank module globbing: modname xor exclude_mod */
  3002. if ((!exclude_mod) != (!modname))
  3003. goto func_match;
  3004. return 0;
  3005. }
  3006. /* not matching the module */
  3007. if (!modname || !mod_matches) {
  3008. if (exclude_mod)
  3009. goto func_match;
  3010. else
  3011. return 0;
  3012. }
  3013. if (mod_matches && exclude_mod)
  3014. return 0;
  3015. func_match:
  3016. /* blank search means to match all funcs in the mod */
  3017. if (!func_g->len)
  3018. return 1;
  3019. }
  3020. return ftrace_match(str, func_g);
  3021. }
  3022. static int
  3023. match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
  3024. {
  3025. struct ftrace_page *pg;
  3026. struct dyn_ftrace *rec;
  3027. struct ftrace_glob func_g = { .type = MATCH_FULL };
  3028. struct ftrace_glob mod_g = { .type = MATCH_FULL };
  3029. struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
  3030. int exclude_mod = 0;
  3031. int found = 0;
  3032. int ret;
  3033. int clear_filter;
  3034. if (func) {
  3035. func_g.type = filter_parse_regex(func, len, &func_g.search,
  3036. &clear_filter);
  3037. func_g.len = strlen(func_g.search);
  3038. }
  3039. if (mod) {
  3040. mod_g.type = filter_parse_regex(mod, strlen(mod),
  3041. &mod_g.search, &exclude_mod);
  3042. mod_g.len = strlen(mod_g.search);
  3043. }
  3044. mutex_lock(&ftrace_lock);
  3045. if (unlikely(ftrace_disabled))
  3046. goto out_unlock;
  3047. do_for_each_ftrace_rec(pg, rec) {
  3048. if (rec->flags & FTRACE_FL_DISABLED)
  3049. continue;
  3050. if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
  3051. ret = enter_record(hash, rec, clear_filter);
  3052. if (ret < 0) {
  3053. found = ret;
  3054. goto out_unlock;
  3055. }
  3056. found = 1;
  3057. }
  3058. } while_for_each_ftrace_rec();
  3059. out_unlock:
  3060. mutex_unlock(&ftrace_lock);
  3061. return found;
  3062. }
  3063. static int
  3064. ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
  3065. {
  3066. return match_records(hash, buff, len, NULL);
  3067. }
  3068. static void ftrace_ops_update_code(struct ftrace_ops *ops,
  3069. struct ftrace_ops_hash *old_hash)
  3070. {
  3071. struct ftrace_ops *op;
  3072. if (!ftrace_enabled)
  3073. return;
  3074. if (ops->flags & FTRACE_OPS_FL_ENABLED) {
  3075. ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
  3076. return;
  3077. }
  3078. /*
  3079. * If this is the shared global_ops filter, then we need to
  3080. * check if there is another ops that shares it, is enabled.
  3081. * If so, we still need to run the modify code.
  3082. */
  3083. if (ops->func_hash != &global_ops.local_hash)
  3084. return;
  3085. do_for_each_ftrace_op(op, ftrace_ops_list) {
  3086. if (op->func_hash == &global_ops.local_hash &&
  3087. op->flags & FTRACE_OPS_FL_ENABLED) {
  3088. ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
  3089. /* Only need to do this once */
  3090. return;
  3091. }
  3092. } while_for_each_ftrace_op(op);
  3093. }
  3094. static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
  3095. struct ftrace_hash **orig_hash,
  3096. struct ftrace_hash *hash,
  3097. int enable)
  3098. {
  3099. struct ftrace_ops_hash old_hash_ops;
  3100. struct ftrace_hash *old_hash;
  3101. int ret;
  3102. old_hash = *orig_hash;
  3103. old_hash_ops.filter_hash = ops->func_hash->filter_hash;
  3104. old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
  3105. ret = ftrace_hash_move(ops, enable, orig_hash, hash);
  3106. if (!ret) {
  3107. ftrace_ops_update_code(ops, &old_hash_ops);
  3108. free_ftrace_hash_rcu(old_hash);
  3109. }
  3110. return ret;
  3111. }
  3112. /*
  3113. * We register the module command as a template to show others how
  3114. * to register the a command as well.
  3115. */
  3116. static int
  3117. ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
  3118. char *func, char *cmd, char *module, int enable)
  3119. {
  3120. int ret;
  3121. /*
  3122. * cmd == 'mod' because we only registered this func
  3123. * for the 'mod' ftrace_func_command.
  3124. * But if you register one func with multiple commands,
  3125. * you can tell which command was used by the cmd
  3126. * parameter.
  3127. */
  3128. ret = match_records(hash, func, strlen(func), module);
  3129. if (!ret)
  3130. return -EINVAL;
  3131. if (ret < 0)
  3132. return ret;
  3133. return 0;
  3134. }
  3135. static struct ftrace_func_command ftrace_mod_cmd = {
  3136. .name = "mod",
  3137. .func = ftrace_mod_callback,
  3138. };
  3139. static int __init ftrace_mod_cmd_init(void)
  3140. {
  3141. return register_ftrace_command(&ftrace_mod_cmd);
  3142. }
  3143. core_initcall(ftrace_mod_cmd_init);
  3144. static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
  3145. struct ftrace_ops *op, struct pt_regs *pt_regs)
  3146. {
  3147. struct ftrace_probe_ops *probe_ops;
  3148. struct ftrace_func_probe *probe;
  3149. probe = container_of(op, struct ftrace_func_probe, ops);
  3150. probe_ops = probe->probe_ops;
  3151. /*
  3152. * Disable preemption for these calls to prevent a RCU grace
  3153. * period. This syncs the hash iteration and freeing of items
  3154. * on the hash. rcu_read_lock is too dangerous here.
  3155. */
  3156. preempt_disable_notrace();
  3157. probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
  3158. preempt_enable_notrace();
  3159. }
  3160. struct ftrace_func_map {
  3161. struct ftrace_func_entry entry;
  3162. void *data;
  3163. };
  3164. struct ftrace_func_mapper {
  3165. struct ftrace_hash hash;
  3166. };
  3167. /**
  3168. * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
  3169. *
  3170. * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
  3171. */
  3172. struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
  3173. {
  3174. struct ftrace_hash *hash;
  3175. /*
  3176. * The mapper is simply a ftrace_hash, but since the entries
  3177. * in the hash are not ftrace_func_entry type, we define it
  3178. * as a separate structure.
  3179. */
  3180. hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
  3181. return (struct ftrace_func_mapper *)hash;
  3182. }
  3183. /**
  3184. * ftrace_func_mapper_find_ip - Find some data mapped to an ip
  3185. * @mapper: The mapper that has the ip maps
  3186. * @ip: the instruction pointer to find the data for
  3187. *
  3188. * Returns the data mapped to @ip if found otherwise NULL. The return
  3189. * is actually the address of the mapper data pointer. The address is
  3190. * returned for use cases where the data is no bigger than a long, and
  3191. * the user can use the data pointer as its data instead of having to
  3192. * allocate more memory for the reference.
  3193. */
  3194. void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
  3195. unsigned long ip)
  3196. {
  3197. struct ftrace_func_entry *entry;
  3198. struct ftrace_func_map *map;
  3199. entry = ftrace_lookup_ip(&mapper->hash, ip);
  3200. if (!entry)
  3201. return NULL;
  3202. map = (struct ftrace_func_map *)entry;
  3203. return &map->data;
  3204. }
  3205. /**
  3206. * ftrace_func_mapper_add_ip - Map some data to an ip
  3207. * @mapper: The mapper that has the ip maps
  3208. * @ip: The instruction pointer address to map @data to
  3209. * @data: The data to map to @ip
  3210. *
  3211. * Returns 0 on succes otherwise an error.
  3212. */
  3213. int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
  3214. unsigned long ip, void *data)
  3215. {
  3216. struct ftrace_func_entry *entry;
  3217. struct ftrace_func_map *map;
  3218. entry = ftrace_lookup_ip(&mapper->hash, ip);
  3219. if (entry)
  3220. return -EBUSY;
  3221. map = kmalloc(sizeof(*map), GFP_KERNEL);
  3222. if (!map)
  3223. return -ENOMEM;
  3224. map->entry.ip = ip;
  3225. map->data = data;
  3226. __add_hash_entry(&mapper->hash, &map->entry);
  3227. return 0;
  3228. }
  3229. /**
  3230. * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
  3231. * @mapper: The mapper that has the ip maps
  3232. * @ip: The instruction pointer address to remove the data from
  3233. *
  3234. * Returns the data if it is found, otherwise NULL.
  3235. * Note, if the data pointer is used as the data itself, (see
  3236. * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
  3237. * if the data pointer was set to zero.
  3238. */
  3239. void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
  3240. unsigned long ip)
  3241. {
  3242. struct ftrace_func_entry *entry;
  3243. struct ftrace_func_map *map;
  3244. void *data;
  3245. entry = ftrace_lookup_ip(&mapper->hash, ip);
  3246. if (!entry)
  3247. return NULL;
  3248. map = (struct ftrace_func_map *)entry;
  3249. data = map->data;
  3250. remove_hash_entry(&mapper->hash, entry);
  3251. kfree(entry);
  3252. return data;
  3253. }
  3254. /**
  3255. * free_ftrace_func_mapper - free a mapping of ips and data
  3256. * @mapper: The mapper that has the ip maps
  3257. * @free_func: A function to be called on each data item.
  3258. *
  3259. * This is used to free the function mapper. The @free_func is optional
  3260. * and can be used if the data needs to be freed as well.
  3261. */
  3262. void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
  3263. ftrace_mapper_func free_func)
  3264. {
  3265. struct ftrace_func_entry *entry;
  3266. struct ftrace_func_map *map;
  3267. struct hlist_head *hhd;
  3268. int size = 1 << mapper->hash.size_bits;
  3269. int i;
  3270. if (free_func && mapper->hash.count) {
  3271. for (i = 0; i < size; i++) {
  3272. hhd = &mapper->hash.buckets[i];
  3273. hlist_for_each_entry(entry, hhd, hlist) {
  3274. map = (struct ftrace_func_map *)entry;
  3275. free_func(map);
  3276. }
  3277. }
  3278. }
  3279. free_ftrace_hash(&mapper->hash);
  3280. }
  3281. static void release_probe(struct ftrace_func_probe *probe)
  3282. {
  3283. struct ftrace_probe_ops *probe_ops;
  3284. mutex_lock(&ftrace_lock);
  3285. WARN_ON(probe->ref <= 0);
  3286. /* Subtract the ref that was used to protect this instance */
  3287. probe->ref--;
  3288. if (!probe->ref) {
  3289. probe_ops = probe->probe_ops;
  3290. /*
  3291. * Sending zero as ip tells probe_ops to free
  3292. * the probe->data itself
  3293. */
  3294. if (probe_ops->free)
  3295. probe_ops->free(probe_ops, probe->tr, 0, probe->data);
  3296. list_del(&probe->list);
  3297. kfree(probe);
  3298. }
  3299. mutex_unlock(&ftrace_lock);
  3300. }
  3301. static void acquire_probe_locked(struct ftrace_func_probe *probe)
  3302. {
  3303. /*
  3304. * Add one ref to keep it from being freed when releasing the
  3305. * ftrace_lock mutex.
  3306. */
  3307. probe->ref++;
  3308. }
  3309. int
  3310. register_ftrace_function_probe(char *glob, struct trace_array *tr,
  3311. struct ftrace_probe_ops *probe_ops,
  3312. void *data)
  3313. {
  3314. struct ftrace_func_entry *entry;
  3315. struct ftrace_func_probe *probe;
  3316. struct ftrace_hash **orig_hash;
  3317. struct ftrace_hash *old_hash;
  3318. struct ftrace_hash *hash;
  3319. int count = 0;
  3320. int size;
  3321. int ret;
  3322. int i;
  3323. if (WARN_ON(!tr))
  3324. return -EINVAL;
  3325. /* We do not support '!' for function probes */
  3326. if (WARN_ON(glob[0] == '!'))
  3327. return -EINVAL;
  3328. mutex_lock(&ftrace_lock);
  3329. /* Check if the probe_ops is already registered */
  3330. list_for_each_entry(probe, &tr->func_probes, list) {
  3331. if (probe->probe_ops == probe_ops)
  3332. break;
  3333. }
  3334. if (&probe->list == &tr->func_probes) {
  3335. probe = kzalloc(sizeof(*probe), GFP_KERNEL);
  3336. if (!probe) {
  3337. mutex_unlock(&ftrace_lock);
  3338. return -ENOMEM;
  3339. }
  3340. probe->probe_ops = probe_ops;
  3341. probe->ops.func = function_trace_probe_call;
  3342. probe->tr = tr;
  3343. ftrace_ops_init(&probe->ops);
  3344. list_add(&probe->list, &tr->func_probes);
  3345. }
  3346. acquire_probe_locked(probe);
  3347. mutex_unlock(&ftrace_lock);
  3348. mutex_lock(&probe->ops.func_hash->regex_lock);
  3349. orig_hash = &probe->ops.func_hash->filter_hash;
  3350. old_hash = *orig_hash;
  3351. hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
  3352. ret = ftrace_match_records(hash, glob, strlen(glob));
  3353. /* Nothing found? */
  3354. if (!ret)
  3355. ret = -EINVAL;
  3356. if (ret < 0)
  3357. goto out;
  3358. size = 1 << hash->size_bits;
  3359. for (i = 0; i < size; i++) {
  3360. hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
  3361. if (ftrace_lookup_ip(old_hash, entry->ip))
  3362. continue;
  3363. /*
  3364. * The caller might want to do something special
  3365. * for each function we find. We call the callback
  3366. * to give the caller an opportunity to do so.
  3367. */
  3368. if (probe_ops->init) {
  3369. ret = probe_ops->init(probe_ops, tr,
  3370. entry->ip, data,
  3371. &probe->data);
  3372. if (ret < 0) {
  3373. if (probe_ops->free && count)
  3374. probe_ops->free(probe_ops, tr,
  3375. 0, probe->data);
  3376. probe->data = NULL;
  3377. goto out;
  3378. }
  3379. }
  3380. count++;
  3381. }
  3382. }
  3383. mutex_lock(&ftrace_lock);
  3384. if (!count) {
  3385. /* Nothing was added? */
  3386. ret = -EINVAL;
  3387. goto out_unlock;
  3388. }
  3389. ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
  3390. hash, 1);
  3391. if (ret < 0)
  3392. goto err_unlock;
  3393. /* One ref for each new function traced */
  3394. probe->ref += count;
  3395. if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
  3396. ret = ftrace_startup(&probe->ops, 0);
  3397. out_unlock:
  3398. mutex_unlock(&ftrace_lock);
  3399. if (!ret)
  3400. ret = count;
  3401. out:
  3402. mutex_unlock(&probe->ops.func_hash->regex_lock);
  3403. free_ftrace_hash(hash);
  3404. release_probe(probe);
  3405. return ret;
  3406. err_unlock:
  3407. if (!probe_ops->free || !count)
  3408. goto out_unlock;
  3409. /* Failed to do the move, need to call the free functions */
  3410. for (i = 0; i < size; i++) {
  3411. hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
  3412. if (ftrace_lookup_ip(old_hash, entry->ip))
  3413. continue;
  3414. probe_ops->free(probe_ops, tr, entry->ip, probe->data);
  3415. }
  3416. }
  3417. goto out_unlock;
  3418. }
  3419. int
  3420. unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
  3421. struct ftrace_probe_ops *probe_ops)
  3422. {
  3423. struct ftrace_ops_hash old_hash_ops;
  3424. struct ftrace_func_entry *entry;
  3425. struct ftrace_func_probe *probe;
  3426. struct ftrace_glob func_g;
  3427. struct ftrace_hash **orig_hash;
  3428. struct ftrace_hash *old_hash;
  3429. struct ftrace_hash *hash = NULL;
  3430. struct hlist_node *tmp;
  3431. struct hlist_head hhd;
  3432. char str[KSYM_SYMBOL_LEN];
  3433. int count = 0;
  3434. int i, ret = -ENODEV;
  3435. int size;
  3436. if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
  3437. func_g.search = NULL;
  3438. else if (glob) {
  3439. int not;
  3440. func_g.type = filter_parse_regex(glob, strlen(glob),
  3441. &func_g.search, &not);
  3442. func_g.len = strlen(func_g.search);
  3443. func_g.search = glob;
  3444. /* we do not support '!' for function probes */
  3445. if (WARN_ON(not))
  3446. return -EINVAL;
  3447. }
  3448. mutex_lock(&ftrace_lock);
  3449. /* Check if the probe_ops is already registered */
  3450. list_for_each_entry(probe, &tr->func_probes, list) {
  3451. if (probe->probe_ops == probe_ops)
  3452. break;
  3453. }
  3454. if (&probe->list == &tr->func_probes)
  3455. goto err_unlock_ftrace;
  3456. ret = -EINVAL;
  3457. if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
  3458. goto err_unlock_ftrace;
  3459. acquire_probe_locked(probe);
  3460. mutex_unlock(&ftrace_lock);
  3461. mutex_lock(&probe->ops.func_hash->regex_lock);
  3462. orig_hash = &probe->ops.func_hash->filter_hash;
  3463. old_hash = *orig_hash;
  3464. if (ftrace_hash_empty(old_hash))
  3465. goto out_unlock;
  3466. old_hash_ops.filter_hash = old_hash;
  3467. /* Probes only have filters */
  3468. old_hash_ops.notrace_hash = NULL;
  3469. ret = -ENOMEM;
  3470. hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
  3471. if (!hash)
  3472. goto out_unlock;
  3473. INIT_HLIST_HEAD(&hhd);
  3474. size = 1 << hash->size_bits;
  3475. for (i = 0; i < size; i++) {
  3476. hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
  3477. if (func_g.search) {
  3478. kallsyms_lookup(entry->ip, NULL, NULL,
  3479. NULL, str);
  3480. if (!ftrace_match(str, &func_g))
  3481. continue;
  3482. }
  3483. count++;
  3484. remove_hash_entry(hash, entry);
  3485. hlist_add_head(&entry->hlist, &hhd);
  3486. }
  3487. }
  3488. /* Nothing found? */
  3489. if (!count) {
  3490. ret = -EINVAL;
  3491. goto out_unlock;
  3492. }
  3493. mutex_lock(&ftrace_lock);
  3494. WARN_ON(probe->ref < count);
  3495. probe->ref -= count;
  3496. if (ftrace_hash_empty(hash))
  3497. ftrace_shutdown(&probe->ops, 0);
  3498. ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
  3499. hash, 1);
  3500. /* still need to update the function call sites */
  3501. if (ftrace_enabled && !ftrace_hash_empty(hash))
  3502. ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
  3503. &old_hash_ops);
  3504. synchronize_sched();
  3505. hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
  3506. hlist_del(&entry->hlist);
  3507. if (probe_ops->free)
  3508. probe_ops->free(probe_ops, tr, entry->ip, probe->data);
  3509. kfree(entry);
  3510. }
  3511. mutex_unlock(&ftrace_lock);
  3512. out_unlock:
  3513. mutex_unlock(&probe->ops.func_hash->regex_lock);
  3514. free_ftrace_hash(hash);
  3515. release_probe(probe);
  3516. return ret;
  3517. err_unlock_ftrace:
  3518. mutex_unlock(&ftrace_lock);
  3519. return ret;
  3520. }
  3521. static LIST_HEAD(ftrace_commands);
  3522. static DEFINE_MUTEX(ftrace_cmd_mutex);
  3523. /*
  3524. * Currently we only register ftrace commands from __init, so mark this
  3525. * __init too.
  3526. */
  3527. __init int register_ftrace_command(struct ftrace_func_command *cmd)
  3528. {
  3529. struct ftrace_func_command *p;
  3530. int ret = 0;
  3531. mutex_lock(&ftrace_cmd_mutex);
  3532. list_for_each_entry(p, &ftrace_commands, list) {
  3533. if (strcmp(cmd->name, p->name) == 0) {
  3534. ret = -EBUSY;
  3535. goto out_unlock;
  3536. }
  3537. }
  3538. list_add(&cmd->list, &ftrace_commands);
  3539. out_unlock:
  3540. mutex_unlock(&ftrace_cmd_mutex);
  3541. return ret;
  3542. }
  3543. /*
  3544. * Currently we only unregister ftrace commands from __init, so mark
  3545. * this __init too.
  3546. */
  3547. __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
  3548. {
  3549. struct ftrace_func_command *p, *n;
  3550. int ret = -ENODEV;
  3551. mutex_lock(&ftrace_cmd_mutex);
  3552. list_for_each_entry_safe(p, n, &ftrace_commands, list) {
  3553. if (strcmp(cmd->name, p->name) == 0) {
  3554. ret = 0;
  3555. list_del_init(&p->list);
  3556. goto out_unlock;
  3557. }
  3558. }
  3559. out_unlock:
  3560. mutex_unlock(&ftrace_cmd_mutex);
  3561. return ret;
  3562. }
  3563. static int ftrace_process_regex(struct ftrace_iterator *iter,
  3564. char *buff, int len, int enable)
  3565. {
  3566. struct ftrace_hash *hash = iter->hash;
  3567. struct trace_array *tr = iter->ops->private;
  3568. char *func, *command, *next = buff;
  3569. struct ftrace_func_command *p;
  3570. int ret = -EINVAL;
  3571. func = strsep(&next, ":");
  3572. if (!next) {
  3573. ret = ftrace_match_records(hash, func, len);
  3574. if (!ret)
  3575. ret = -EINVAL;
  3576. if (ret < 0)
  3577. return ret;
  3578. return 0;
  3579. }
  3580. /* command found */
  3581. command = strsep(&next, ":");
  3582. if (WARN_ON_ONCE(!tr))
  3583. return -EINVAL;
  3584. mutex_lock(&ftrace_cmd_mutex);
  3585. list_for_each_entry(p, &ftrace_commands, list) {
  3586. if (strcmp(p->name, command) == 0) {
  3587. ret = p->func(tr, hash, func, command, next, enable);
  3588. goto out_unlock;
  3589. }
  3590. }
  3591. out_unlock:
  3592. mutex_unlock(&ftrace_cmd_mutex);
  3593. return ret;
  3594. }
  3595. static ssize_t
  3596. ftrace_regex_write(struct file *file, const char __user *ubuf,
  3597. size_t cnt, loff_t *ppos, int enable)
  3598. {
  3599. struct ftrace_iterator *iter;
  3600. struct trace_parser *parser;
  3601. ssize_t ret, read;
  3602. if (!cnt)
  3603. return 0;
  3604. if (file->f_mode & FMODE_READ) {
  3605. struct seq_file *m = file->private_data;
  3606. iter = m->private;
  3607. } else
  3608. iter = file->private_data;
  3609. if (unlikely(ftrace_disabled))
  3610. return -ENODEV;
  3611. /* iter->hash is a local copy, so we don't need regex_lock */
  3612. parser = &iter->parser;
  3613. read = trace_get_user(parser, ubuf, cnt, ppos);
  3614. if (read >= 0 && trace_parser_loaded(parser) &&
  3615. !trace_parser_cont(parser)) {
  3616. ret = ftrace_process_regex(iter, parser->buffer,
  3617. parser->idx, enable);
  3618. trace_parser_clear(parser);
  3619. if (ret < 0)
  3620. goto out;
  3621. }
  3622. ret = read;
  3623. out:
  3624. return ret;
  3625. }
  3626. ssize_t
  3627. ftrace_filter_write(struct file *file, const char __user *ubuf,
  3628. size_t cnt, loff_t *ppos)
  3629. {
  3630. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  3631. }
  3632. ssize_t
  3633. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  3634. size_t cnt, loff_t *ppos)
  3635. {
  3636. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  3637. }
  3638. static int
  3639. ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
  3640. {
  3641. struct ftrace_func_entry *entry;
  3642. if (!ftrace_location(ip))
  3643. return -EINVAL;
  3644. if (remove) {
  3645. entry = ftrace_lookup_ip(hash, ip);
  3646. if (!entry)
  3647. return -ENOENT;
  3648. free_hash_entry(hash, entry);
  3649. return 0;
  3650. }
  3651. return add_hash_entry(hash, ip);
  3652. }
  3653. static int
  3654. ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
  3655. unsigned long ip, int remove, int reset, int enable)
  3656. {
  3657. struct ftrace_hash **orig_hash;
  3658. struct ftrace_hash *hash;
  3659. int ret;
  3660. if (unlikely(ftrace_disabled))
  3661. return -ENODEV;
  3662. mutex_lock(&ops->func_hash->regex_lock);
  3663. if (enable)
  3664. orig_hash = &ops->func_hash->filter_hash;
  3665. else
  3666. orig_hash = &ops->func_hash->notrace_hash;
  3667. if (reset)
  3668. hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
  3669. else
  3670. hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
  3671. if (!hash) {
  3672. ret = -ENOMEM;
  3673. goto out_regex_unlock;
  3674. }
  3675. if (buf && !ftrace_match_records(hash, buf, len)) {
  3676. ret = -EINVAL;
  3677. goto out_regex_unlock;
  3678. }
  3679. if (ip) {
  3680. ret = ftrace_match_addr(hash, ip, remove);
  3681. if (ret < 0)
  3682. goto out_regex_unlock;
  3683. }
  3684. mutex_lock(&ftrace_lock);
  3685. ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
  3686. mutex_unlock(&ftrace_lock);
  3687. out_regex_unlock:
  3688. mutex_unlock(&ops->func_hash->regex_lock);
  3689. free_ftrace_hash(hash);
  3690. return ret;
  3691. }
  3692. static int
  3693. ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
  3694. int reset, int enable)
  3695. {
  3696. return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
  3697. }
  3698. /**
  3699. * ftrace_set_filter_ip - set a function to filter on in ftrace by address
  3700. * @ops - the ops to set the filter with
  3701. * @ip - the address to add to or remove from the filter.
  3702. * @remove - non zero to remove the ip from the filter
  3703. * @reset - non zero to reset all filters before applying this filter.
  3704. *
  3705. * Filters denote which functions should be enabled when tracing is enabled
  3706. * If @ip is NULL, it failes to update filter.
  3707. */
  3708. int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
  3709. int remove, int reset)
  3710. {
  3711. ftrace_ops_init(ops);
  3712. return ftrace_set_addr(ops, ip, remove, reset, 1);
  3713. }
  3714. EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
  3715. /**
  3716. * ftrace_ops_set_global_filter - setup ops to use global filters
  3717. * @ops - the ops which will use the global filters
  3718. *
  3719. * ftrace users who need global function trace filtering should call this.
  3720. * It can set the global filter only if ops were not initialized before.
  3721. */
  3722. void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
  3723. {
  3724. if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
  3725. return;
  3726. ftrace_ops_init(ops);
  3727. ops->func_hash = &global_ops.local_hash;
  3728. }
  3729. EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
  3730. static int
  3731. ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
  3732. int reset, int enable)
  3733. {
  3734. return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
  3735. }
  3736. /**
  3737. * ftrace_set_filter - set a function to filter on in ftrace
  3738. * @ops - the ops to set the filter with
  3739. * @buf - the string that holds the function filter text.
  3740. * @len - the length of the string.
  3741. * @reset - non zero to reset all filters before applying this filter.
  3742. *
  3743. * Filters denote which functions should be enabled when tracing is enabled.
  3744. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  3745. */
  3746. int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
  3747. int len, int reset)
  3748. {
  3749. ftrace_ops_init(ops);
  3750. return ftrace_set_regex(ops, buf, len, reset, 1);
  3751. }
  3752. EXPORT_SYMBOL_GPL(ftrace_set_filter);
  3753. /**
  3754. * ftrace_set_notrace - set a function to not trace in ftrace
  3755. * @ops - the ops to set the notrace filter with
  3756. * @buf - the string that holds the function notrace text.
  3757. * @len - the length of the string.
  3758. * @reset - non zero to reset all filters before applying this filter.
  3759. *
  3760. * Notrace Filters denote which functions should not be enabled when tracing
  3761. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  3762. * for tracing.
  3763. */
  3764. int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
  3765. int len, int reset)
  3766. {
  3767. ftrace_ops_init(ops);
  3768. return ftrace_set_regex(ops, buf, len, reset, 0);
  3769. }
  3770. EXPORT_SYMBOL_GPL(ftrace_set_notrace);
  3771. /**
  3772. * ftrace_set_global_filter - set a function to filter on with global tracers
  3773. * @buf - the string that holds the function filter text.
  3774. * @len - the length of the string.
  3775. * @reset - non zero to reset all filters before applying this filter.
  3776. *
  3777. * Filters denote which functions should be enabled when tracing is enabled.
  3778. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  3779. */
  3780. void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
  3781. {
  3782. ftrace_set_regex(&global_ops, buf, len, reset, 1);
  3783. }
  3784. EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
  3785. /**
  3786. * ftrace_set_global_notrace - set a function to not trace with global tracers
  3787. * @buf - the string that holds the function notrace text.
  3788. * @len - the length of the string.
  3789. * @reset - non zero to reset all filters before applying this filter.
  3790. *
  3791. * Notrace Filters denote which functions should not be enabled when tracing
  3792. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  3793. * for tracing.
  3794. */
  3795. void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
  3796. {
  3797. ftrace_set_regex(&global_ops, buf, len, reset, 0);
  3798. }
  3799. EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
  3800. /*
  3801. * command line interface to allow users to set filters on boot up.
  3802. */
  3803. #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
  3804. static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
  3805. static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
  3806. /* Used by function selftest to not test if filter is set */
  3807. bool ftrace_filter_param __initdata;
  3808. static int __init set_ftrace_notrace(char *str)
  3809. {
  3810. ftrace_filter_param = true;
  3811. strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
  3812. return 1;
  3813. }
  3814. __setup("ftrace_notrace=", set_ftrace_notrace);
  3815. static int __init set_ftrace_filter(char *str)
  3816. {
  3817. ftrace_filter_param = true;
  3818. strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
  3819. return 1;
  3820. }
  3821. __setup("ftrace_filter=", set_ftrace_filter);
  3822. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  3823. static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
  3824. static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
  3825. static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
  3826. static unsigned long save_global_trampoline;
  3827. static unsigned long save_global_flags;
  3828. static int __init set_graph_function(char *str)
  3829. {
  3830. strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
  3831. return 1;
  3832. }
  3833. __setup("ftrace_graph_filter=", set_graph_function);
  3834. static int __init set_graph_notrace_function(char *str)
  3835. {
  3836. strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
  3837. return 1;
  3838. }
  3839. __setup("ftrace_graph_notrace=", set_graph_notrace_function);
  3840. static int __init set_graph_max_depth_function(char *str)
  3841. {
  3842. if (!str)
  3843. return 0;
  3844. fgraph_max_depth = simple_strtoul(str, NULL, 0);
  3845. return 1;
  3846. }
  3847. __setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
  3848. static void __init set_ftrace_early_graph(char *buf, int enable)
  3849. {
  3850. int ret;
  3851. char *func;
  3852. struct ftrace_hash *hash;
  3853. hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
  3854. if (WARN_ON(!hash))
  3855. return;
  3856. while (buf) {
  3857. func = strsep(&buf, ",");
  3858. /* we allow only one expression at a time */
  3859. ret = ftrace_graph_set_hash(hash, func);
  3860. if (ret)
  3861. printk(KERN_DEBUG "ftrace: function %s not "
  3862. "traceable\n", func);
  3863. }
  3864. if (enable)
  3865. ftrace_graph_hash = hash;
  3866. else
  3867. ftrace_graph_notrace_hash = hash;
  3868. }
  3869. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  3870. void __init
  3871. ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
  3872. {
  3873. char *func;
  3874. ftrace_ops_init(ops);
  3875. while (buf) {
  3876. func = strsep(&buf, ",");
  3877. ftrace_set_regex(ops, func, strlen(func), 0, enable);
  3878. }
  3879. }
  3880. static void __init set_ftrace_early_filters(void)
  3881. {
  3882. if (ftrace_filter_buf[0])
  3883. ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
  3884. if (ftrace_notrace_buf[0])
  3885. ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
  3886. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  3887. if (ftrace_graph_buf[0])
  3888. set_ftrace_early_graph(ftrace_graph_buf, 1);
  3889. if (ftrace_graph_notrace_buf[0])
  3890. set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
  3891. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  3892. }
  3893. int ftrace_regex_release(struct inode *inode, struct file *file)
  3894. {
  3895. struct seq_file *m = (struct seq_file *)file->private_data;
  3896. struct ftrace_iterator *iter;
  3897. struct ftrace_hash **orig_hash;
  3898. struct trace_parser *parser;
  3899. int filter_hash;
  3900. int ret;
  3901. if (file->f_mode & FMODE_READ) {
  3902. iter = m->private;
  3903. seq_release(inode, file);
  3904. } else
  3905. iter = file->private_data;
  3906. parser = &iter->parser;
  3907. if (trace_parser_loaded(parser)) {
  3908. parser->buffer[parser->idx] = 0;
  3909. ftrace_match_records(iter->hash, parser->buffer, parser->idx);
  3910. }
  3911. trace_parser_put(parser);
  3912. mutex_lock(&iter->ops->func_hash->regex_lock);
  3913. if (file->f_mode & FMODE_WRITE) {
  3914. filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
  3915. if (filter_hash)
  3916. orig_hash = &iter->ops->func_hash->filter_hash;
  3917. else
  3918. orig_hash = &iter->ops->func_hash->notrace_hash;
  3919. mutex_lock(&ftrace_lock);
  3920. ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
  3921. iter->hash, filter_hash);
  3922. mutex_unlock(&ftrace_lock);
  3923. } else {
  3924. /* For read only, the hash is the ops hash */
  3925. iter->hash = NULL;
  3926. }
  3927. mutex_unlock(&iter->ops->func_hash->regex_lock);
  3928. free_ftrace_hash(iter->hash);
  3929. kfree(iter);
  3930. return 0;
  3931. }
  3932. static const struct file_operations ftrace_avail_fops = {
  3933. .open = ftrace_avail_open,
  3934. .read = seq_read,
  3935. .llseek = seq_lseek,
  3936. .release = seq_release_private,
  3937. };
  3938. static const struct file_operations ftrace_enabled_fops = {
  3939. .open = ftrace_enabled_open,
  3940. .read = seq_read,
  3941. .llseek = seq_lseek,
  3942. .release = seq_release_private,
  3943. };
  3944. static const struct file_operations ftrace_filter_fops = {
  3945. .open = ftrace_filter_open,
  3946. .read = seq_read,
  3947. .write = ftrace_filter_write,
  3948. .llseek = tracing_lseek,
  3949. .release = ftrace_regex_release,
  3950. };
  3951. static const struct file_operations ftrace_notrace_fops = {
  3952. .open = ftrace_notrace_open,
  3953. .read = seq_read,
  3954. .write = ftrace_notrace_write,
  3955. .llseek = tracing_lseek,
  3956. .release = ftrace_regex_release,
  3957. };
  3958. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  3959. static DEFINE_MUTEX(graph_lock);
  3960. struct ftrace_hash *ftrace_graph_hash = EMPTY_HASH;
  3961. struct ftrace_hash *ftrace_graph_notrace_hash = EMPTY_HASH;
  3962. enum graph_filter_type {
  3963. GRAPH_FILTER_NOTRACE = 0,
  3964. GRAPH_FILTER_FUNCTION,
  3965. };
  3966. #define FTRACE_GRAPH_EMPTY ((void *)1)
  3967. struct ftrace_graph_data {
  3968. struct ftrace_hash *hash;
  3969. struct ftrace_func_entry *entry;
  3970. int idx; /* for hash table iteration */
  3971. enum graph_filter_type type;
  3972. struct ftrace_hash *new_hash;
  3973. const struct seq_operations *seq_ops;
  3974. struct trace_parser parser;
  3975. };
  3976. static void *
  3977. __g_next(struct seq_file *m, loff_t *pos)
  3978. {
  3979. struct ftrace_graph_data *fgd = m->private;
  3980. struct ftrace_func_entry *entry = fgd->entry;
  3981. struct hlist_head *head;
  3982. int i, idx = fgd->idx;
  3983. if (*pos >= fgd->hash->count)
  3984. return NULL;
  3985. if (entry) {
  3986. hlist_for_each_entry_continue(entry, hlist) {
  3987. fgd->entry = entry;
  3988. return entry;
  3989. }
  3990. idx++;
  3991. }
  3992. for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
  3993. head = &fgd->hash->buckets[i];
  3994. hlist_for_each_entry(entry, head, hlist) {
  3995. fgd->entry = entry;
  3996. fgd->idx = i;
  3997. return entry;
  3998. }
  3999. }
  4000. return NULL;
  4001. }
  4002. static void *
  4003. g_next(struct seq_file *m, void *v, loff_t *pos)
  4004. {
  4005. (*pos)++;
  4006. return __g_next(m, pos);
  4007. }
  4008. static void *g_start(struct seq_file *m, loff_t *pos)
  4009. {
  4010. struct ftrace_graph_data *fgd = m->private;
  4011. mutex_lock(&graph_lock);
  4012. if (fgd->type == GRAPH_FILTER_FUNCTION)
  4013. fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
  4014. lockdep_is_held(&graph_lock));
  4015. else
  4016. fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
  4017. lockdep_is_held(&graph_lock));
  4018. /* Nothing, tell g_show to print all functions are enabled */
  4019. if (ftrace_hash_empty(fgd->hash) && !*pos)
  4020. return FTRACE_GRAPH_EMPTY;
  4021. fgd->idx = 0;
  4022. fgd->entry = NULL;
  4023. return __g_next(m, pos);
  4024. }
  4025. static void g_stop(struct seq_file *m, void *p)
  4026. {
  4027. mutex_unlock(&graph_lock);
  4028. }
  4029. static int g_show(struct seq_file *m, void *v)
  4030. {
  4031. struct ftrace_func_entry *entry = v;
  4032. if (!entry)
  4033. return 0;
  4034. if (entry == FTRACE_GRAPH_EMPTY) {
  4035. struct ftrace_graph_data *fgd = m->private;
  4036. if (fgd->type == GRAPH_FILTER_FUNCTION)
  4037. seq_puts(m, "#### all functions enabled ####\n");
  4038. else
  4039. seq_puts(m, "#### no functions disabled ####\n");
  4040. return 0;
  4041. }
  4042. seq_printf(m, "%ps\n", (void *)entry->ip);
  4043. return 0;
  4044. }
  4045. static const struct seq_operations ftrace_graph_seq_ops = {
  4046. .start = g_start,
  4047. .next = g_next,
  4048. .stop = g_stop,
  4049. .show = g_show,
  4050. };
  4051. static int
  4052. __ftrace_graph_open(struct inode *inode, struct file *file,
  4053. struct ftrace_graph_data *fgd)
  4054. {
  4055. int ret = 0;
  4056. struct ftrace_hash *new_hash = NULL;
  4057. if (file->f_mode & FMODE_WRITE) {
  4058. const int size_bits = FTRACE_HASH_DEFAULT_BITS;
  4059. if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
  4060. return -ENOMEM;
  4061. if (file->f_flags & O_TRUNC)
  4062. new_hash = alloc_ftrace_hash(size_bits);
  4063. else
  4064. new_hash = alloc_and_copy_ftrace_hash(size_bits,
  4065. fgd->hash);
  4066. if (!new_hash) {
  4067. ret = -ENOMEM;
  4068. goto out;
  4069. }
  4070. }
  4071. if (file->f_mode & FMODE_READ) {
  4072. ret = seq_open(file, &ftrace_graph_seq_ops);
  4073. if (!ret) {
  4074. struct seq_file *m = file->private_data;
  4075. m->private = fgd;
  4076. } else {
  4077. /* Failed */
  4078. free_ftrace_hash(new_hash);
  4079. new_hash = NULL;
  4080. }
  4081. } else
  4082. file->private_data = fgd;
  4083. out:
  4084. if (ret < 0 && file->f_mode & FMODE_WRITE)
  4085. trace_parser_put(&fgd->parser);
  4086. fgd->new_hash = new_hash;
  4087. /*
  4088. * All uses of fgd->hash must be taken with the graph_lock
  4089. * held. The graph_lock is going to be released, so force
  4090. * fgd->hash to be reinitialized when it is taken again.
  4091. */
  4092. fgd->hash = NULL;
  4093. return ret;
  4094. }
  4095. static int
  4096. ftrace_graph_open(struct inode *inode, struct file *file)
  4097. {
  4098. struct ftrace_graph_data *fgd;
  4099. int ret;
  4100. if (unlikely(ftrace_disabled))
  4101. return -ENODEV;
  4102. fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
  4103. if (fgd == NULL)
  4104. return -ENOMEM;
  4105. mutex_lock(&graph_lock);
  4106. fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
  4107. lockdep_is_held(&graph_lock));
  4108. fgd->type = GRAPH_FILTER_FUNCTION;
  4109. fgd->seq_ops = &ftrace_graph_seq_ops;
  4110. ret = __ftrace_graph_open(inode, file, fgd);
  4111. if (ret < 0)
  4112. kfree(fgd);
  4113. mutex_unlock(&graph_lock);
  4114. return ret;
  4115. }
  4116. static int
  4117. ftrace_graph_notrace_open(struct inode *inode, struct file *file)
  4118. {
  4119. struct ftrace_graph_data *fgd;
  4120. int ret;
  4121. if (unlikely(ftrace_disabled))
  4122. return -ENODEV;
  4123. fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
  4124. if (fgd == NULL)
  4125. return -ENOMEM;
  4126. mutex_lock(&graph_lock);
  4127. fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
  4128. lockdep_is_held(&graph_lock));
  4129. fgd->type = GRAPH_FILTER_NOTRACE;
  4130. fgd->seq_ops = &ftrace_graph_seq_ops;
  4131. ret = __ftrace_graph_open(inode, file, fgd);
  4132. if (ret < 0)
  4133. kfree(fgd);
  4134. mutex_unlock(&graph_lock);
  4135. return ret;
  4136. }
  4137. static int
  4138. ftrace_graph_release(struct inode *inode, struct file *file)
  4139. {
  4140. struct ftrace_graph_data *fgd;
  4141. struct ftrace_hash *old_hash, *new_hash;
  4142. struct trace_parser *parser;
  4143. int ret = 0;
  4144. if (file->f_mode & FMODE_READ) {
  4145. struct seq_file *m = file->private_data;
  4146. fgd = m->private;
  4147. seq_release(inode, file);
  4148. } else {
  4149. fgd = file->private_data;
  4150. }
  4151. if (file->f_mode & FMODE_WRITE) {
  4152. parser = &fgd->parser;
  4153. if (trace_parser_loaded((parser))) {
  4154. parser->buffer[parser->idx] = 0;
  4155. ret = ftrace_graph_set_hash(fgd->new_hash,
  4156. parser->buffer);
  4157. }
  4158. trace_parser_put(parser);
  4159. new_hash = __ftrace_hash_move(fgd->new_hash);
  4160. if (!new_hash) {
  4161. ret = -ENOMEM;
  4162. goto out;
  4163. }
  4164. mutex_lock(&graph_lock);
  4165. if (fgd->type == GRAPH_FILTER_FUNCTION) {
  4166. old_hash = rcu_dereference_protected(ftrace_graph_hash,
  4167. lockdep_is_held(&graph_lock));
  4168. rcu_assign_pointer(ftrace_graph_hash, new_hash);
  4169. } else {
  4170. old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
  4171. lockdep_is_held(&graph_lock));
  4172. rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
  4173. }
  4174. mutex_unlock(&graph_lock);
  4175. /* Wait till all users are no longer using the old hash */
  4176. synchronize_sched();
  4177. free_ftrace_hash(old_hash);
  4178. }
  4179. out:
  4180. kfree(fgd->new_hash);
  4181. kfree(fgd);
  4182. return ret;
  4183. }
  4184. static int
  4185. ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
  4186. {
  4187. struct ftrace_glob func_g;
  4188. struct dyn_ftrace *rec;
  4189. struct ftrace_page *pg;
  4190. struct ftrace_func_entry *entry;
  4191. int fail = 1;
  4192. int not;
  4193. /* decode regex */
  4194. func_g.type = filter_parse_regex(buffer, strlen(buffer),
  4195. &func_g.search, &not);
  4196. func_g.len = strlen(func_g.search);
  4197. mutex_lock(&ftrace_lock);
  4198. if (unlikely(ftrace_disabled)) {
  4199. mutex_unlock(&ftrace_lock);
  4200. return -ENODEV;
  4201. }
  4202. do_for_each_ftrace_rec(pg, rec) {
  4203. if (rec->flags & FTRACE_FL_DISABLED)
  4204. continue;
  4205. if (ftrace_match_record(rec, &func_g, NULL, 0)) {
  4206. entry = ftrace_lookup_ip(hash, rec->ip);
  4207. if (!not) {
  4208. fail = 0;
  4209. if (entry)
  4210. continue;
  4211. if (add_hash_entry(hash, rec->ip) < 0)
  4212. goto out;
  4213. } else {
  4214. if (entry) {
  4215. free_hash_entry(hash, entry);
  4216. fail = 0;
  4217. }
  4218. }
  4219. }
  4220. } while_for_each_ftrace_rec();
  4221. out:
  4222. mutex_unlock(&ftrace_lock);
  4223. if (fail)
  4224. return -EINVAL;
  4225. return 0;
  4226. }
  4227. static ssize_t
  4228. ftrace_graph_write(struct file *file, const char __user *ubuf,
  4229. size_t cnt, loff_t *ppos)
  4230. {
  4231. ssize_t read, ret = 0;
  4232. struct ftrace_graph_data *fgd = file->private_data;
  4233. struct trace_parser *parser;
  4234. if (!cnt)
  4235. return 0;
  4236. /* Read mode uses seq functions */
  4237. if (file->f_mode & FMODE_READ) {
  4238. struct seq_file *m = file->private_data;
  4239. fgd = m->private;
  4240. }
  4241. parser = &fgd->parser;
  4242. read = trace_get_user(parser, ubuf, cnt, ppos);
  4243. if (read >= 0 && trace_parser_loaded(parser) &&
  4244. !trace_parser_cont(parser)) {
  4245. ret = ftrace_graph_set_hash(fgd->new_hash,
  4246. parser->buffer);
  4247. trace_parser_clear(parser);
  4248. }
  4249. if (!ret)
  4250. ret = read;
  4251. return ret;
  4252. }
  4253. static const struct file_operations ftrace_graph_fops = {
  4254. .open = ftrace_graph_open,
  4255. .read = seq_read,
  4256. .write = ftrace_graph_write,
  4257. .llseek = tracing_lseek,
  4258. .release = ftrace_graph_release,
  4259. };
  4260. static const struct file_operations ftrace_graph_notrace_fops = {
  4261. .open = ftrace_graph_notrace_open,
  4262. .read = seq_read,
  4263. .write = ftrace_graph_write,
  4264. .llseek = tracing_lseek,
  4265. .release = ftrace_graph_release,
  4266. };
  4267. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  4268. void ftrace_create_filter_files(struct ftrace_ops *ops,
  4269. struct dentry *parent)
  4270. {
  4271. trace_create_file("set_ftrace_filter", 0644, parent,
  4272. ops, &ftrace_filter_fops);
  4273. trace_create_file("set_ftrace_notrace", 0644, parent,
  4274. ops, &ftrace_notrace_fops);
  4275. }
  4276. /*
  4277. * The name "destroy_filter_files" is really a misnomer. Although
  4278. * in the future, it may actualy delete the files, but this is
  4279. * really intended to make sure the ops passed in are disabled
  4280. * and that when this function returns, the caller is free to
  4281. * free the ops.
  4282. *
  4283. * The "destroy" name is only to match the "create" name that this
  4284. * should be paired with.
  4285. */
  4286. void ftrace_destroy_filter_files(struct ftrace_ops *ops)
  4287. {
  4288. mutex_lock(&ftrace_lock);
  4289. if (ops->flags & FTRACE_OPS_FL_ENABLED)
  4290. ftrace_shutdown(ops, 0);
  4291. ops->flags |= FTRACE_OPS_FL_DELETED;
  4292. mutex_unlock(&ftrace_lock);
  4293. }
  4294. static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
  4295. {
  4296. trace_create_file("available_filter_functions", 0444,
  4297. d_tracer, NULL, &ftrace_avail_fops);
  4298. trace_create_file("enabled_functions", 0444,
  4299. d_tracer, NULL, &ftrace_enabled_fops);
  4300. ftrace_create_filter_files(&global_ops, d_tracer);
  4301. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  4302. trace_create_file("set_graph_function", 0444, d_tracer,
  4303. NULL,
  4304. &ftrace_graph_fops);
  4305. trace_create_file("set_graph_notrace", 0444, d_tracer,
  4306. NULL,
  4307. &ftrace_graph_notrace_fops);
  4308. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  4309. return 0;
  4310. }
  4311. static int ftrace_cmp_ips(const void *a, const void *b)
  4312. {
  4313. const unsigned long *ipa = a;
  4314. const unsigned long *ipb = b;
  4315. if (*ipa > *ipb)
  4316. return 1;
  4317. if (*ipa < *ipb)
  4318. return -1;
  4319. return 0;
  4320. }
  4321. static int ftrace_process_locs(struct module *mod,
  4322. unsigned long *start,
  4323. unsigned long *end)
  4324. {
  4325. struct ftrace_page *start_pg;
  4326. struct ftrace_page *pg;
  4327. struct dyn_ftrace *rec;
  4328. unsigned long count;
  4329. unsigned long *p;
  4330. unsigned long addr;
  4331. unsigned long flags = 0; /* Shut up gcc */
  4332. int ret = -ENOMEM;
  4333. count = end - start;
  4334. if (!count)
  4335. return 0;
  4336. sort(start, count, sizeof(*start),
  4337. ftrace_cmp_ips, NULL);
  4338. start_pg = ftrace_allocate_pages(count);
  4339. if (!start_pg)
  4340. return -ENOMEM;
  4341. mutex_lock(&ftrace_lock);
  4342. /*
  4343. * Core and each module needs their own pages, as
  4344. * modules will free them when they are removed.
  4345. * Force a new page to be allocated for modules.
  4346. */
  4347. if (!mod) {
  4348. WARN_ON(ftrace_pages || ftrace_pages_start);
  4349. /* First initialization */
  4350. ftrace_pages = ftrace_pages_start = start_pg;
  4351. } else {
  4352. if (!ftrace_pages)
  4353. goto out;
  4354. if (WARN_ON(ftrace_pages->next)) {
  4355. /* Hmm, we have free pages? */
  4356. while (ftrace_pages->next)
  4357. ftrace_pages = ftrace_pages->next;
  4358. }
  4359. ftrace_pages->next = start_pg;
  4360. }
  4361. p = start;
  4362. pg = start_pg;
  4363. while (p < end) {
  4364. addr = ftrace_call_adjust(*p++);
  4365. /*
  4366. * Some architecture linkers will pad between
  4367. * the different mcount_loc sections of different
  4368. * object files to satisfy alignments.
  4369. * Skip any NULL pointers.
  4370. */
  4371. if (!addr)
  4372. continue;
  4373. if (pg->index == pg->size) {
  4374. /* We should have allocated enough */
  4375. if (WARN_ON(!pg->next))
  4376. break;
  4377. pg = pg->next;
  4378. }
  4379. rec = &pg->records[pg->index++];
  4380. rec->ip = addr;
  4381. }
  4382. /* We should have used all pages */
  4383. WARN_ON(pg->next);
  4384. /* Assign the last page to ftrace_pages */
  4385. ftrace_pages = pg;
  4386. /*
  4387. * We only need to disable interrupts on start up
  4388. * because we are modifying code that an interrupt
  4389. * may execute, and the modification is not atomic.
  4390. * But for modules, nothing runs the code we modify
  4391. * until we are finished with it, and there's no
  4392. * reason to cause large interrupt latencies while we do it.
  4393. */
  4394. if (!mod)
  4395. local_irq_save(flags);
  4396. ftrace_update_code(mod, start_pg);
  4397. if (!mod)
  4398. local_irq_restore(flags);
  4399. ret = 0;
  4400. out:
  4401. mutex_unlock(&ftrace_lock);
  4402. return ret;
  4403. }
  4404. #ifdef CONFIG_MODULES
  4405. #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
  4406. static int referenced_filters(struct dyn_ftrace *rec)
  4407. {
  4408. struct ftrace_ops *ops;
  4409. int cnt = 0;
  4410. for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
  4411. if (ops_references_rec(ops, rec))
  4412. cnt++;
  4413. }
  4414. return cnt;
  4415. }
  4416. void ftrace_release_mod(struct module *mod)
  4417. {
  4418. struct dyn_ftrace *rec;
  4419. struct ftrace_page **last_pg;
  4420. struct ftrace_page *pg;
  4421. int order;
  4422. mutex_lock(&ftrace_lock);
  4423. if (ftrace_disabled)
  4424. goto out_unlock;
  4425. /*
  4426. * Each module has its own ftrace_pages, remove
  4427. * them from the list.
  4428. */
  4429. last_pg = &ftrace_pages_start;
  4430. for (pg = ftrace_pages_start; pg; pg = *last_pg) {
  4431. rec = &pg->records[0];
  4432. if (within_module_core(rec->ip, mod)) {
  4433. /*
  4434. * As core pages are first, the first
  4435. * page should never be a module page.
  4436. */
  4437. if (WARN_ON(pg == ftrace_pages_start))
  4438. goto out_unlock;
  4439. /* Check if we are deleting the last page */
  4440. if (pg == ftrace_pages)
  4441. ftrace_pages = next_to_ftrace_page(last_pg);
  4442. *last_pg = pg->next;
  4443. order = get_count_order(pg->size / ENTRIES_PER_PAGE);
  4444. free_pages((unsigned long)pg->records, order);
  4445. kfree(pg);
  4446. } else
  4447. last_pg = &pg->next;
  4448. }
  4449. out_unlock:
  4450. mutex_unlock(&ftrace_lock);
  4451. }
  4452. void ftrace_module_enable(struct module *mod)
  4453. {
  4454. struct dyn_ftrace *rec;
  4455. struct ftrace_page *pg;
  4456. mutex_lock(&ftrace_lock);
  4457. if (ftrace_disabled)
  4458. goto out_unlock;
  4459. /*
  4460. * If the tracing is enabled, go ahead and enable the record.
  4461. *
  4462. * The reason not to enable the record immediatelly is the
  4463. * inherent check of ftrace_make_nop/ftrace_make_call for
  4464. * correct previous instructions. Making first the NOP
  4465. * conversion puts the module to the correct state, thus
  4466. * passing the ftrace_make_call check.
  4467. *
  4468. * We also delay this to after the module code already set the
  4469. * text to read-only, as we now need to set it back to read-write
  4470. * so that we can modify the text.
  4471. */
  4472. if (ftrace_start_up)
  4473. ftrace_arch_code_modify_prepare();
  4474. do_for_each_ftrace_rec(pg, rec) {
  4475. int cnt;
  4476. /*
  4477. * do_for_each_ftrace_rec() is a double loop.
  4478. * module text shares the pg. If a record is
  4479. * not part of this module, then skip this pg,
  4480. * which the "break" will do.
  4481. */
  4482. if (!within_module_core(rec->ip, mod))
  4483. break;
  4484. cnt = 0;
  4485. /*
  4486. * When adding a module, we need to check if tracers are
  4487. * currently enabled and if they are, and can trace this record,
  4488. * we need to enable the module functions as well as update the
  4489. * reference counts for those function records.
  4490. */
  4491. if (ftrace_start_up)
  4492. cnt += referenced_filters(rec);
  4493. /* This clears FTRACE_FL_DISABLED */
  4494. rec->flags = cnt;
  4495. if (ftrace_start_up && cnt) {
  4496. int failed = __ftrace_replace_code(rec, 1);
  4497. if (failed) {
  4498. ftrace_bug(failed, rec);
  4499. goto out_loop;
  4500. }
  4501. }
  4502. } while_for_each_ftrace_rec();
  4503. out_loop:
  4504. if (ftrace_start_up)
  4505. ftrace_arch_code_modify_post_process();
  4506. out_unlock:
  4507. mutex_unlock(&ftrace_lock);
  4508. }
  4509. void ftrace_module_init(struct module *mod)
  4510. {
  4511. if (ftrace_disabled || !mod->num_ftrace_callsites)
  4512. return;
  4513. ftrace_process_locs(mod, mod->ftrace_callsites,
  4514. mod->ftrace_callsites + mod->num_ftrace_callsites);
  4515. }
  4516. #endif /* CONFIG_MODULES */
  4517. void __init ftrace_free_init_mem(void)
  4518. {
  4519. unsigned long start = (unsigned long)(&__init_begin);
  4520. unsigned long end = (unsigned long)(&__init_end);
  4521. struct ftrace_page **last_pg = &ftrace_pages_start;
  4522. struct ftrace_page *pg;
  4523. struct dyn_ftrace *rec;
  4524. struct dyn_ftrace key;
  4525. int order;
  4526. key.ip = start;
  4527. key.flags = end; /* overload flags, as it is unsigned long */
  4528. mutex_lock(&ftrace_lock);
  4529. for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
  4530. if (end < pg->records[0].ip ||
  4531. start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
  4532. continue;
  4533. again:
  4534. rec = bsearch(&key, pg->records, pg->index,
  4535. sizeof(struct dyn_ftrace),
  4536. ftrace_cmp_recs);
  4537. if (!rec)
  4538. continue;
  4539. pg->index--;
  4540. if (!pg->index) {
  4541. *last_pg = pg->next;
  4542. order = get_count_order(pg->size / ENTRIES_PER_PAGE);
  4543. free_pages((unsigned long)pg->records, order);
  4544. kfree(pg);
  4545. pg = container_of(last_pg, struct ftrace_page, next);
  4546. if (!(*last_pg))
  4547. ftrace_pages = pg;
  4548. continue;
  4549. }
  4550. memmove(rec, rec + 1,
  4551. (pg->index - (rec - pg->records)) * sizeof(*rec));
  4552. /* More than one function may be in this block */
  4553. goto again;
  4554. }
  4555. mutex_unlock(&ftrace_lock);
  4556. }
  4557. void __init ftrace_init(void)
  4558. {
  4559. extern unsigned long __start_mcount_loc[];
  4560. extern unsigned long __stop_mcount_loc[];
  4561. unsigned long count, flags;
  4562. int ret;
  4563. local_irq_save(flags);
  4564. ret = ftrace_dyn_arch_init();
  4565. local_irq_restore(flags);
  4566. if (ret)
  4567. goto failed;
  4568. count = __stop_mcount_loc - __start_mcount_loc;
  4569. if (!count) {
  4570. pr_info("ftrace: No functions to be traced?\n");
  4571. goto failed;
  4572. }
  4573. pr_info("ftrace: allocating %ld entries in %ld pages\n",
  4574. count, count / ENTRIES_PER_PAGE + 1);
  4575. last_ftrace_enabled = ftrace_enabled = 1;
  4576. ret = ftrace_process_locs(NULL,
  4577. __start_mcount_loc,
  4578. __stop_mcount_loc);
  4579. set_ftrace_early_filters();
  4580. return;
  4581. failed:
  4582. ftrace_disabled = 1;
  4583. }
  4584. /* Do nothing if arch does not support this */
  4585. void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
  4586. {
  4587. }
  4588. static void ftrace_update_trampoline(struct ftrace_ops *ops)
  4589. {
  4590. arch_ftrace_update_trampoline(ops);
  4591. }
  4592. void ftrace_init_trace_array(struct trace_array *tr)
  4593. {
  4594. INIT_LIST_HEAD(&tr->func_probes);
  4595. }
  4596. #else
  4597. static struct ftrace_ops global_ops = {
  4598. .func = ftrace_stub,
  4599. .flags = FTRACE_OPS_FL_RECURSION_SAFE |
  4600. FTRACE_OPS_FL_INITIALIZED |
  4601. FTRACE_OPS_FL_PID,
  4602. };
  4603. static int __init ftrace_nodyn_init(void)
  4604. {
  4605. ftrace_enabled = 1;
  4606. return 0;
  4607. }
  4608. core_initcall(ftrace_nodyn_init);
  4609. static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
  4610. static inline void ftrace_startup_enable(int command) { }
  4611. static inline void ftrace_startup_all(int command) { }
  4612. /* Keep as macros so we do not need to define the commands */
  4613. # define ftrace_startup(ops, command) \
  4614. ({ \
  4615. int ___ret = __register_ftrace_function(ops); \
  4616. if (!___ret) \
  4617. (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
  4618. ___ret; \
  4619. })
  4620. # define ftrace_shutdown(ops, command) \
  4621. ({ \
  4622. int ___ret = __unregister_ftrace_function(ops); \
  4623. if (!___ret) \
  4624. (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
  4625. ___ret; \
  4626. })
  4627. # define ftrace_startup_sysctl() do { } while (0)
  4628. # define ftrace_shutdown_sysctl() do { } while (0)
  4629. static inline int
  4630. ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
  4631. {
  4632. return 1;
  4633. }
  4634. static void ftrace_update_trampoline(struct ftrace_ops *ops)
  4635. {
  4636. }
  4637. #endif /* CONFIG_DYNAMIC_FTRACE */
  4638. __init void ftrace_init_global_array_ops(struct trace_array *tr)
  4639. {
  4640. tr->ops = &global_ops;
  4641. tr->ops->private = tr;
  4642. ftrace_init_trace_array(tr);
  4643. }
  4644. void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
  4645. {
  4646. /* If we filter on pids, update to use the pid function */
  4647. if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
  4648. if (WARN_ON(tr->ops->func != ftrace_stub))
  4649. printk("ftrace ops had %pS for function\n",
  4650. tr->ops->func);
  4651. }
  4652. tr->ops->func = func;
  4653. tr->ops->private = tr;
  4654. }
  4655. void ftrace_reset_array_ops(struct trace_array *tr)
  4656. {
  4657. tr->ops->func = ftrace_stub;
  4658. }
  4659. static inline void
  4660. __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
  4661. struct ftrace_ops *ignored, struct pt_regs *regs)
  4662. {
  4663. struct ftrace_ops *op;
  4664. int bit;
  4665. bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
  4666. if (bit < 0)
  4667. return;
  4668. /*
  4669. * Some of the ops may be dynamically allocated,
  4670. * they must be freed after a synchronize_sched().
  4671. */
  4672. preempt_disable_notrace();
  4673. do_for_each_ftrace_op(op, ftrace_ops_list) {
  4674. /*
  4675. * Check the following for each ops before calling their func:
  4676. * if RCU flag is set, then rcu_is_watching() must be true
  4677. * if PER_CPU is set, then ftrace_function_local_disable()
  4678. * must be false
  4679. * Otherwise test if the ip matches the ops filter
  4680. *
  4681. * If any of the above fails then the op->func() is not executed.
  4682. */
  4683. if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
  4684. (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
  4685. !ftrace_function_local_disabled(op)) &&
  4686. ftrace_ops_test(op, ip, regs)) {
  4687. if (FTRACE_WARN_ON(!op->func)) {
  4688. pr_warn("op=%p %pS\n", op, op);
  4689. goto out;
  4690. }
  4691. op->func(ip, parent_ip, op, regs);
  4692. }
  4693. } while_for_each_ftrace_op(op);
  4694. out:
  4695. preempt_enable_notrace();
  4696. trace_clear_recursion(bit);
  4697. }
  4698. /*
  4699. * Some archs only support passing ip and parent_ip. Even though
  4700. * the list function ignores the op parameter, we do not want any
  4701. * C side effects, where a function is called without the caller
  4702. * sending a third parameter.
  4703. * Archs are to support both the regs and ftrace_ops at the same time.
  4704. * If they support ftrace_ops, it is assumed they support regs.
  4705. * If call backs want to use regs, they must either check for regs
  4706. * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
  4707. * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
  4708. * An architecture can pass partial regs with ftrace_ops and still
  4709. * set the ARCH_SUPPORTS_FTRACE_OPS.
  4710. */
  4711. #if ARCH_SUPPORTS_FTRACE_OPS
  4712. static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
  4713. struct ftrace_ops *op, struct pt_regs *regs)
  4714. {
  4715. __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
  4716. }
  4717. #else
  4718. static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
  4719. {
  4720. __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
  4721. }
  4722. #endif
  4723. /*
  4724. * If there's only one function registered but it does not support
  4725. * recursion, needs RCU protection and/or requires per cpu handling, then
  4726. * this function will be called by the mcount trampoline.
  4727. */
  4728. static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
  4729. struct ftrace_ops *op, struct pt_regs *regs)
  4730. {
  4731. int bit;
  4732. if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
  4733. return;
  4734. bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
  4735. if (bit < 0)
  4736. return;
  4737. preempt_disable_notrace();
  4738. if (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
  4739. !ftrace_function_local_disabled(op)) {
  4740. op->func(ip, parent_ip, op, regs);
  4741. }
  4742. preempt_enable_notrace();
  4743. trace_clear_recursion(bit);
  4744. }
  4745. /**
  4746. * ftrace_ops_get_func - get the function a trampoline should call
  4747. * @ops: the ops to get the function for
  4748. *
  4749. * Normally the mcount trampoline will call the ops->func, but there
  4750. * are times that it should not. For example, if the ops does not
  4751. * have its own recursion protection, then it should call the
  4752. * ftrace_ops_assist_func() instead.
  4753. *
  4754. * Returns the function that the trampoline should call for @ops.
  4755. */
  4756. ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
  4757. {
  4758. /*
  4759. * If the function does not handle recursion, needs to be RCU safe,
  4760. * or does per cpu logic, then we need to call the assist handler.
  4761. */
  4762. if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
  4763. ops->flags & (FTRACE_OPS_FL_RCU | FTRACE_OPS_FL_PER_CPU))
  4764. return ftrace_ops_assist_func;
  4765. return ops->func;
  4766. }
  4767. static void
  4768. ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
  4769. struct task_struct *prev, struct task_struct *next)
  4770. {
  4771. struct trace_array *tr = data;
  4772. struct trace_pid_list *pid_list;
  4773. pid_list = rcu_dereference_sched(tr->function_pids);
  4774. this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
  4775. trace_ignore_this_task(pid_list, next));
  4776. }
  4777. static void
  4778. ftrace_pid_follow_sched_process_fork(void *data,
  4779. struct task_struct *self,
  4780. struct task_struct *task)
  4781. {
  4782. struct trace_pid_list *pid_list;
  4783. struct trace_array *tr = data;
  4784. pid_list = rcu_dereference_sched(tr->function_pids);
  4785. trace_filter_add_remove_task(pid_list, self, task);
  4786. }
  4787. static void
  4788. ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
  4789. {
  4790. struct trace_pid_list *pid_list;
  4791. struct trace_array *tr = data;
  4792. pid_list = rcu_dereference_sched(tr->function_pids);
  4793. trace_filter_add_remove_task(pid_list, NULL, task);
  4794. }
  4795. void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
  4796. {
  4797. if (enable) {
  4798. register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
  4799. tr);
  4800. register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
  4801. tr);
  4802. } else {
  4803. unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
  4804. tr);
  4805. unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
  4806. tr);
  4807. }
  4808. }
  4809. static void clear_ftrace_pids(struct trace_array *tr)
  4810. {
  4811. struct trace_pid_list *pid_list;
  4812. int cpu;
  4813. pid_list = rcu_dereference_protected(tr->function_pids,
  4814. lockdep_is_held(&ftrace_lock));
  4815. if (!pid_list)
  4816. return;
  4817. unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
  4818. for_each_possible_cpu(cpu)
  4819. per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
  4820. rcu_assign_pointer(tr->function_pids, NULL);
  4821. /* Wait till all users are no longer using pid filtering */
  4822. synchronize_sched();
  4823. trace_free_pid_list(pid_list);
  4824. }
  4825. static void ftrace_pid_reset(struct trace_array *tr)
  4826. {
  4827. mutex_lock(&ftrace_lock);
  4828. clear_ftrace_pids(tr);
  4829. ftrace_update_pid_func();
  4830. ftrace_startup_all(0);
  4831. mutex_unlock(&ftrace_lock);
  4832. }
  4833. /* Greater than any max PID */
  4834. #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
  4835. static void *fpid_start(struct seq_file *m, loff_t *pos)
  4836. __acquires(RCU)
  4837. {
  4838. struct trace_pid_list *pid_list;
  4839. struct trace_array *tr = m->private;
  4840. mutex_lock(&ftrace_lock);
  4841. rcu_read_lock_sched();
  4842. pid_list = rcu_dereference_sched(tr->function_pids);
  4843. if (!pid_list)
  4844. return !(*pos) ? FTRACE_NO_PIDS : NULL;
  4845. return trace_pid_start(pid_list, pos);
  4846. }
  4847. static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
  4848. {
  4849. struct trace_array *tr = m->private;
  4850. struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
  4851. if (v == FTRACE_NO_PIDS)
  4852. return NULL;
  4853. return trace_pid_next(pid_list, v, pos);
  4854. }
  4855. static void fpid_stop(struct seq_file *m, void *p)
  4856. __releases(RCU)
  4857. {
  4858. rcu_read_unlock_sched();
  4859. mutex_unlock(&ftrace_lock);
  4860. }
  4861. static int fpid_show(struct seq_file *m, void *v)
  4862. {
  4863. if (v == FTRACE_NO_PIDS) {
  4864. seq_puts(m, "no pid\n");
  4865. return 0;
  4866. }
  4867. return trace_pid_show(m, v);
  4868. }
  4869. static const struct seq_operations ftrace_pid_sops = {
  4870. .start = fpid_start,
  4871. .next = fpid_next,
  4872. .stop = fpid_stop,
  4873. .show = fpid_show,
  4874. };
  4875. static int
  4876. ftrace_pid_open(struct inode *inode, struct file *file)
  4877. {
  4878. struct trace_array *tr = inode->i_private;
  4879. struct seq_file *m;
  4880. int ret = 0;
  4881. if (trace_array_get(tr) < 0)
  4882. return -ENODEV;
  4883. if ((file->f_mode & FMODE_WRITE) &&
  4884. (file->f_flags & O_TRUNC))
  4885. ftrace_pid_reset(tr);
  4886. ret = seq_open(file, &ftrace_pid_sops);
  4887. if (ret < 0) {
  4888. trace_array_put(tr);
  4889. } else {
  4890. m = file->private_data;
  4891. /* copy tr over to seq ops */
  4892. m->private = tr;
  4893. }
  4894. return ret;
  4895. }
  4896. static void ignore_task_cpu(void *data)
  4897. {
  4898. struct trace_array *tr = data;
  4899. struct trace_pid_list *pid_list;
  4900. /*
  4901. * This function is called by on_each_cpu() while the
  4902. * event_mutex is held.
  4903. */
  4904. pid_list = rcu_dereference_protected(tr->function_pids,
  4905. mutex_is_locked(&ftrace_lock));
  4906. this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
  4907. trace_ignore_this_task(pid_list, current));
  4908. }
  4909. static ssize_t
  4910. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  4911. size_t cnt, loff_t *ppos)
  4912. {
  4913. struct seq_file *m = filp->private_data;
  4914. struct trace_array *tr = m->private;
  4915. struct trace_pid_list *filtered_pids = NULL;
  4916. struct trace_pid_list *pid_list;
  4917. ssize_t ret;
  4918. if (!cnt)
  4919. return 0;
  4920. mutex_lock(&ftrace_lock);
  4921. filtered_pids = rcu_dereference_protected(tr->function_pids,
  4922. lockdep_is_held(&ftrace_lock));
  4923. ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
  4924. if (ret < 0)
  4925. goto out;
  4926. rcu_assign_pointer(tr->function_pids, pid_list);
  4927. if (filtered_pids) {
  4928. synchronize_sched();
  4929. trace_free_pid_list(filtered_pids);
  4930. } else if (pid_list) {
  4931. /* Register a probe to set whether to ignore the tracing of a task */
  4932. register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
  4933. }
  4934. /*
  4935. * Ignoring of pids is done at task switch. But we have to
  4936. * check for those tasks that are currently running.
  4937. * Always do this in case a pid was appended or removed.
  4938. */
  4939. on_each_cpu(ignore_task_cpu, tr, 1);
  4940. ftrace_update_pid_func();
  4941. ftrace_startup_all(0);
  4942. out:
  4943. mutex_unlock(&ftrace_lock);
  4944. if (ret > 0)
  4945. *ppos += ret;
  4946. return ret;
  4947. }
  4948. static int
  4949. ftrace_pid_release(struct inode *inode, struct file *file)
  4950. {
  4951. struct trace_array *tr = inode->i_private;
  4952. trace_array_put(tr);
  4953. return seq_release(inode, file);
  4954. }
  4955. static const struct file_operations ftrace_pid_fops = {
  4956. .open = ftrace_pid_open,
  4957. .write = ftrace_pid_write,
  4958. .read = seq_read,
  4959. .llseek = tracing_lseek,
  4960. .release = ftrace_pid_release,
  4961. };
  4962. void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
  4963. {
  4964. trace_create_file("set_ftrace_pid", 0644, d_tracer,
  4965. tr, &ftrace_pid_fops);
  4966. }
  4967. void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
  4968. struct dentry *d_tracer)
  4969. {
  4970. /* Only the top level directory has the dyn_tracefs and profile */
  4971. WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
  4972. ftrace_init_dyn_tracefs(d_tracer);
  4973. ftrace_profile_tracefs(d_tracer);
  4974. }
  4975. /**
  4976. * ftrace_kill - kill ftrace
  4977. *
  4978. * This function should be used by panic code. It stops ftrace
  4979. * but in a not so nice way. If you need to simply kill ftrace
  4980. * from a non-atomic section, use ftrace_kill.
  4981. */
  4982. void ftrace_kill(void)
  4983. {
  4984. ftrace_disabled = 1;
  4985. ftrace_enabled = 0;
  4986. clear_ftrace_function();
  4987. }
  4988. /**
  4989. * Test if ftrace is dead or not.
  4990. */
  4991. int ftrace_is_dead(void)
  4992. {
  4993. return ftrace_disabled;
  4994. }
  4995. /**
  4996. * register_ftrace_function - register a function for profiling
  4997. * @ops - ops structure that holds the function for profiling.
  4998. *
  4999. * Register a function to be called by all functions in the
  5000. * kernel.
  5001. *
  5002. * Note: @ops->func and all the functions it calls must be labeled
  5003. * with "notrace", otherwise it will go into a
  5004. * recursive loop.
  5005. */
  5006. int register_ftrace_function(struct ftrace_ops *ops)
  5007. {
  5008. int ret = -1;
  5009. ftrace_ops_init(ops);
  5010. mutex_lock(&ftrace_lock);
  5011. ret = ftrace_startup(ops, 0);
  5012. mutex_unlock(&ftrace_lock);
  5013. return ret;
  5014. }
  5015. EXPORT_SYMBOL_GPL(register_ftrace_function);
  5016. /**
  5017. * unregister_ftrace_function - unregister a function for profiling.
  5018. * @ops - ops structure that holds the function to unregister
  5019. *
  5020. * Unregister a function that was added to be called by ftrace profiling.
  5021. */
  5022. int unregister_ftrace_function(struct ftrace_ops *ops)
  5023. {
  5024. int ret;
  5025. mutex_lock(&ftrace_lock);
  5026. ret = ftrace_shutdown(ops, 0);
  5027. mutex_unlock(&ftrace_lock);
  5028. return ret;
  5029. }
  5030. EXPORT_SYMBOL_GPL(unregister_ftrace_function);
  5031. int
  5032. ftrace_enable_sysctl(struct ctl_table *table, int write,
  5033. void __user *buffer, size_t *lenp,
  5034. loff_t *ppos)
  5035. {
  5036. int ret = -ENODEV;
  5037. mutex_lock(&ftrace_lock);
  5038. if (unlikely(ftrace_disabled))
  5039. goto out;
  5040. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  5041. if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
  5042. goto out;
  5043. last_ftrace_enabled = !!ftrace_enabled;
  5044. if (ftrace_enabled) {
  5045. /* we are starting ftrace again */
  5046. if (ftrace_ops_list != &ftrace_list_end)
  5047. update_ftrace_function();
  5048. ftrace_startup_sysctl();
  5049. } else {
  5050. /* stopping ftrace calls (just send to ftrace_stub) */
  5051. ftrace_trace_function = ftrace_stub;
  5052. ftrace_shutdown_sysctl();
  5053. }
  5054. out:
  5055. mutex_unlock(&ftrace_lock);
  5056. return ret;
  5057. }
  5058. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  5059. static struct ftrace_ops graph_ops = {
  5060. .func = ftrace_stub,
  5061. .flags = FTRACE_OPS_FL_RECURSION_SAFE |
  5062. FTRACE_OPS_FL_INITIALIZED |
  5063. FTRACE_OPS_FL_PID |
  5064. FTRACE_OPS_FL_STUB,
  5065. #ifdef FTRACE_GRAPH_TRAMP_ADDR
  5066. .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
  5067. /* trampoline_size is only needed for dynamically allocated tramps */
  5068. #endif
  5069. ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
  5070. };
  5071. void ftrace_graph_sleep_time_control(bool enable)
  5072. {
  5073. fgraph_sleep_time = enable;
  5074. }
  5075. void ftrace_graph_graph_time_control(bool enable)
  5076. {
  5077. fgraph_graph_time = enable;
  5078. }
  5079. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
  5080. {
  5081. return 0;
  5082. }
  5083. /* The callbacks that hook a function */
  5084. trace_func_graph_ret_t ftrace_graph_return =
  5085. (trace_func_graph_ret_t)ftrace_stub;
  5086. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  5087. static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
  5088. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  5089. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  5090. {
  5091. int i;
  5092. int ret = 0;
  5093. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  5094. struct task_struct *g, *t;
  5095. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  5096. ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
  5097. * sizeof(struct ftrace_ret_stack),
  5098. GFP_KERNEL);
  5099. if (!ret_stack_list[i]) {
  5100. start = 0;
  5101. end = i;
  5102. ret = -ENOMEM;
  5103. goto free;
  5104. }
  5105. }
  5106. read_lock(&tasklist_lock);
  5107. do_each_thread(g, t) {
  5108. if (start == end) {
  5109. ret = -EAGAIN;
  5110. goto unlock;
  5111. }
  5112. if (t->ret_stack == NULL) {
  5113. atomic_set(&t->tracing_graph_pause, 0);
  5114. atomic_set(&t->trace_overrun, 0);
  5115. t->curr_ret_stack = -1;
  5116. /* Make sure the tasks see the -1 first: */
  5117. smp_wmb();
  5118. t->ret_stack = ret_stack_list[start++];
  5119. }
  5120. } while_each_thread(g, t);
  5121. unlock:
  5122. read_unlock(&tasklist_lock);
  5123. free:
  5124. for (i = start; i < end; i++)
  5125. kfree(ret_stack_list[i]);
  5126. return ret;
  5127. }
  5128. static void
  5129. ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
  5130. struct task_struct *prev, struct task_struct *next)
  5131. {
  5132. unsigned long long timestamp;
  5133. int index;
  5134. /*
  5135. * Does the user want to count the time a function was asleep.
  5136. * If so, do not update the time stamps.
  5137. */
  5138. if (fgraph_sleep_time)
  5139. return;
  5140. timestamp = trace_clock_local();
  5141. prev->ftrace_timestamp = timestamp;
  5142. /* only process tasks that we timestamped */
  5143. if (!next->ftrace_timestamp)
  5144. return;
  5145. /*
  5146. * Update all the counters in next to make up for the
  5147. * time next was sleeping.
  5148. */
  5149. timestamp -= next->ftrace_timestamp;
  5150. for (index = next->curr_ret_stack; index >= 0; index--)
  5151. next->ret_stack[index].calltime += timestamp;
  5152. }
  5153. /* Allocate a return stack for each task */
  5154. static int start_graph_tracing(void)
  5155. {
  5156. struct ftrace_ret_stack **ret_stack_list;
  5157. int ret, cpu;
  5158. ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
  5159. sizeof(struct ftrace_ret_stack *),
  5160. GFP_KERNEL);
  5161. if (!ret_stack_list)
  5162. return -ENOMEM;
  5163. /* The cpu_boot init_task->ret_stack will never be freed */
  5164. for_each_online_cpu(cpu) {
  5165. if (!idle_task(cpu)->ret_stack)
  5166. ftrace_graph_init_idle_task(idle_task(cpu), cpu);
  5167. }
  5168. do {
  5169. ret = alloc_retstack_tasklist(ret_stack_list);
  5170. } while (ret == -EAGAIN);
  5171. if (!ret) {
  5172. ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  5173. if (ret)
  5174. pr_info("ftrace_graph: Couldn't activate tracepoint"
  5175. " probe to kernel_sched_switch\n");
  5176. }
  5177. kfree(ret_stack_list);
  5178. return ret;
  5179. }
  5180. /*
  5181. * Hibernation protection.
  5182. * The state of the current task is too much unstable during
  5183. * suspend/restore to disk. We want to protect against that.
  5184. */
  5185. static int
  5186. ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
  5187. void *unused)
  5188. {
  5189. switch (state) {
  5190. case PM_HIBERNATION_PREPARE:
  5191. pause_graph_tracing();
  5192. break;
  5193. case PM_POST_HIBERNATION:
  5194. unpause_graph_tracing();
  5195. break;
  5196. }
  5197. return NOTIFY_DONE;
  5198. }
  5199. static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
  5200. {
  5201. if (!ftrace_ops_test(&global_ops, trace->func, NULL))
  5202. return 0;
  5203. return __ftrace_graph_entry(trace);
  5204. }
  5205. /*
  5206. * The function graph tracer should only trace the functions defined
  5207. * by set_ftrace_filter and set_ftrace_notrace. If another function
  5208. * tracer ops is registered, the graph tracer requires testing the
  5209. * function against the global ops, and not just trace any function
  5210. * that any ftrace_ops registered.
  5211. */
  5212. static void update_function_graph_func(void)
  5213. {
  5214. struct ftrace_ops *op;
  5215. bool do_test = false;
  5216. /*
  5217. * The graph and global ops share the same set of functions
  5218. * to test. If any other ops is on the list, then
  5219. * the graph tracing needs to test if its the function
  5220. * it should call.
  5221. */
  5222. do_for_each_ftrace_op(op, ftrace_ops_list) {
  5223. if (op != &global_ops && op != &graph_ops &&
  5224. op != &ftrace_list_end) {
  5225. do_test = true;
  5226. /* in double loop, break out with goto */
  5227. goto out;
  5228. }
  5229. } while_for_each_ftrace_op(op);
  5230. out:
  5231. if (do_test)
  5232. ftrace_graph_entry = ftrace_graph_entry_test;
  5233. else
  5234. ftrace_graph_entry = __ftrace_graph_entry;
  5235. }
  5236. static struct notifier_block ftrace_suspend_notifier = {
  5237. .notifier_call = ftrace_suspend_notifier_call,
  5238. };
  5239. int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  5240. trace_func_graph_ent_t entryfunc)
  5241. {
  5242. int ret = 0;
  5243. mutex_lock(&ftrace_lock);
  5244. /* we currently allow only one tracer registered at a time */
  5245. if (ftrace_graph_active) {
  5246. ret = -EBUSY;
  5247. goto out;
  5248. }
  5249. register_pm_notifier(&ftrace_suspend_notifier);
  5250. ftrace_graph_active++;
  5251. ret = start_graph_tracing();
  5252. if (ret) {
  5253. ftrace_graph_active--;
  5254. goto out;
  5255. }
  5256. ftrace_graph_return = retfunc;
  5257. /*
  5258. * Update the indirect function to the entryfunc, and the
  5259. * function that gets called to the entry_test first. Then
  5260. * call the update fgraph entry function to determine if
  5261. * the entryfunc should be called directly or not.
  5262. */
  5263. __ftrace_graph_entry = entryfunc;
  5264. ftrace_graph_entry = ftrace_graph_entry_test;
  5265. update_function_graph_func();
  5266. ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
  5267. out:
  5268. mutex_unlock(&ftrace_lock);
  5269. return ret;
  5270. }
  5271. void unregister_ftrace_graph(void)
  5272. {
  5273. mutex_lock(&ftrace_lock);
  5274. if (unlikely(!ftrace_graph_active))
  5275. goto out;
  5276. ftrace_graph_active--;
  5277. ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
  5278. ftrace_graph_entry = ftrace_graph_entry_stub;
  5279. __ftrace_graph_entry = ftrace_graph_entry_stub;
  5280. ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
  5281. unregister_pm_notifier(&ftrace_suspend_notifier);
  5282. unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  5283. #ifdef CONFIG_DYNAMIC_FTRACE
  5284. /*
  5285. * Function graph does not allocate the trampoline, but
  5286. * other global_ops do. We need to reset the ALLOC_TRAMP flag
  5287. * if one was used.
  5288. */
  5289. global_ops.trampoline = save_global_trampoline;
  5290. if (save_global_flags & FTRACE_OPS_FL_ALLOC_TRAMP)
  5291. global_ops.flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
  5292. #endif
  5293. out:
  5294. mutex_unlock(&ftrace_lock);
  5295. }
  5296. static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
  5297. static void
  5298. graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
  5299. {
  5300. atomic_set(&t->tracing_graph_pause, 0);
  5301. atomic_set(&t->trace_overrun, 0);
  5302. t->ftrace_timestamp = 0;
  5303. /* make curr_ret_stack visible before we add the ret_stack */
  5304. smp_wmb();
  5305. t->ret_stack = ret_stack;
  5306. }
  5307. /*
  5308. * Allocate a return stack for the idle task. May be the first
  5309. * time through, or it may be done by CPU hotplug online.
  5310. */
  5311. void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
  5312. {
  5313. t->curr_ret_stack = -1;
  5314. /*
  5315. * The idle task has no parent, it either has its own
  5316. * stack or no stack at all.
  5317. */
  5318. if (t->ret_stack)
  5319. WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
  5320. if (ftrace_graph_active) {
  5321. struct ftrace_ret_stack *ret_stack;
  5322. ret_stack = per_cpu(idle_ret_stack, cpu);
  5323. if (!ret_stack) {
  5324. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  5325. * sizeof(struct ftrace_ret_stack),
  5326. GFP_KERNEL);
  5327. if (!ret_stack)
  5328. return;
  5329. per_cpu(idle_ret_stack, cpu) = ret_stack;
  5330. }
  5331. graph_init_task(t, ret_stack);
  5332. }
  5333. }
  5334. /* Allocate a return stack for newly created task */
  5335. void ftrace_graph_init_task(struct task_struct *t)
  5336. {
  5337. /* Make sure we do not use the parent ret_stack */
  5338. t->ret_stack = NULL;
  5339. t->curr_ret_stack = -1;
  5340. if (ftrace_graph_active) {
  5341. struct ftrace_ret_stack *ret_stack;
  5342. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  5343. * sizeof(struct ftrace_ret_stack),
  5344. GFP_KERNEL);
  5345. if (!ret_stack)
  5346. return;
  5347. graph_init_task(t, ret_stack);
  5348. }
  5349. }
  5350. void ftrace_graph_exit_task(struct task_struct *t)
  5351. {
  5352. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  5353. t->ret_stack = NULL;
  5354. /* NULL must become visible to IRQs before we free it: */
  5355. barrier();
  5356. kfree(ret_stack);
  5357. }
  5358. #endif