ftrace.c 166 KB

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