ftrace.c 159 KB

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