ftrace.c 201 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Infrastructure for profiling code inserted by 'gcc -pg'.
  4. *
  5. * Copyright (C) 2007-2008 Steven Rostedt <[email protected]>
  6. * Copyright (C) 2004-2008 Ingo Molnar <[email protected]>
  7. *
  8. * Originally ported from the -rt patch by:
  9. * Copyright (C) 2007 Arnaldo Carvalho de Melo <[email protected]>
  10. *
  11. * Based on code in the latency_tracer, that is:
  12. *
  13. * Copyright (C) 2004-2006 Ingo Molnar
  14. * Copyright (C) 2004 Nadia Yvette Chambers
  15. */
  16. #include <linux/stop_machine.h>
  17. #include <linux/clocksource.h>
  18. #include <linux/sched/task.h>
  19. #include <linux/kallsyms.h>
  20. #include <linux/security.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/tracefs.h>
  23. #include <linux/hardirq.h>
  24. #include <linux/kthread.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/bsearch.h>
  27. #include <linux/module.h>
  28. #include <linux/ftrace.h>
  29. #include <linux/sysctl.h>
  30. #include <linux/slab.h>
  31. #include <linux/ctype.h>
  32. #include <linux/sort.h>
  33. #include <linux/list.h>
  34. #include <linux/hash.h>
  35. #include <linux/rcupdate.h>
  36. #include <linux/kprobes.h>
  37. #include <trace/events/sched.h>
  38. #include <asm/sections.h>
  39. #include <asm/setup.h>
  40. #include "ftrace_internal.h"
  41. #include "trace_output.h"
  42. #include "trace_stat.h"
  43. #define FTRACE_INVALID_FUNCTION "__ftrace_invalid_address__"
  44. #define FTRACE_WARN_ON(cond) \
  45. ({ \
  46. int ___r = cond; \
  47. if (WARN_ON(___r)) \
  48. ftrace_kill(); \
  49. ___r; \
  50. })
  51. #define FTRACE_WARN_ON_ONCE(cond) \
  52. ({ \
  53. int ___r = cond; \
  54. if (WARN_ON_ONCE(___r)) \
  55. ftrace_kill(); \
  56. ___r; \
  57. })
  58. /* hash bits for specific function selection */
  59. #define FTRACE_HASH_DEFAULT_BITS 10
  60. #define FTRACE_HASH_MAX_BITS 12
  61. #ifdef CONFIG_DYNAMIC_FTRACE
  62. #define INIT_OPS_HASH(opsname) \
  63. .func_hash = &opsname.local_hash, \
  64. .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
  65. #else
  66. #define INIT_OPS_HASH(opsname)
  67. #endif
  68. enum {
  69. FTRACE_MODIFY_ENABLE_FL = (1 << 0),
  70. FTRACE_MODIFY_MAY_SLEEP_FL = (1 << 1),
  71. };
  72. struct ftrace_ops ftrace_list_end __read_mostly = {
  73. .func = ftrace_stub,
  74. .flags = FTRACE_OPS_FL_STUB,
  75. INIT_OPS_HASH(ftrace_list_end)
  76. };
  77. /* ftrace_enabled is a method to turn ftrace on or off */
  78. int ftrace_enabled __read_mostly;
  79. static int __maybe_unused last_ftrace_enabled;
  80. /* Current function tracing op */
  81. struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
  82. /* What to set function_trace_op to */
  83. static struct ftrace_ops *set_function_trace_op;
  84. static bool ftrace_pids_enabled(struct ftrace_ops *ops)
  85. {
  86. struct trace_array *tr;
  87. if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
  88. return false;
  89. tr = ops->private;
  90. return tr->function_pids != NULL || tr->function_no_pids != NULL;
  91. }
  92. static void ftrace_update_trampoline(struct ftrace_ops *ops);
  93. /*
  94. * ftrace_disabled is set when an anomaly is discovered.
  95. * ftrace_disabled is much stronger than ftrace_enabled.
  96. */
  97. static int ftrace_disabled __read_mostly;
  98. DEFINE_MUTEX(ftrace_lock);
  99. struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end;
  100. ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
  101. struct ftrace_ops global_ops;
  102. /* Defined by vmlinux.lds.h see the comment above arch_ftrace_ops_list_func for details */
  103. void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
  104. struct ftrace_ops *op, struct ftrace_regs *fregs);
  105. static inline void ftrace_ops_init(struct ftrace_ops *ops)
  106. {
  107. #ifdef CONFIG_DYNAMIC_FTRACE
  108. if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
  109. mutex_init(&ops->local_hash.regex_lock);
  110. ops->func_hash = &ops->local_hash;
  111. ops->flags |= FTRACE_OPS_FL_INITIALIZED;
  112. }
  113. #endif
  114. }
  115. static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
  116. struct ftrace_ops *op, struct ftrace_regs *fregs)
  117. {
  118. struct trace_array *tr = op->private;
  119. int pid;
  120. if (tr) {
  121. pid = this_cpu_read(tr->array_buffer.data->ftrace_ignore_pid);
  122. if (pid == FTRACE_PID_IGNORE)
  123. return;
  124. if (pid != FTRACE_PID_TRACE &&
  125. pid != current->pid)
  126. return;
  127. }
  128. op->saved_func(ip, parent_ip, op, fregs);
  129. }
  130. static void ftrace_sync_ipi(void *data)
  131. {
  132. /* Probably not needed, but do it anyway */
  133. smp_rmb();
  134. }
  135. static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
  136. {
  137. /*
  138. * If this is a dynamic, RCU, or per CPU ops, or we force list func,
  139. * then it needs to call the list anyway.
  140. */
  141. if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) ||
  142. FTRACE_FORCE_LIST_FUNC)
  143. return ftrace_ops_list_func;
  144. return ftrace_ops_get_func(ops);
  145. }
  146. static void update_ftrace_function(void)
  147. {
  148. ftrace_func_t func;
  149. /*
  150. * Prepare the ftrace_ops that the arch callback will use.
  151. * If there's only one ftrace_ops registered, the ftrace_ops_list
  152. * will point to the ops we want.
  153. */
  154. set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
  155. lockdep_is_held(&ftrace_lock));
  156. /* If there's no ftrace_ops registered, just call the stub function */
  157. if (set_function_trace_op == &ftrace_list_end) {
  158. func = ftrace_stub;
  159. /*
  160. * If we are at the end of the list and this ops is
  161. * recursion safe and not dynamic and the arch supports passing ops,
  162. * then have the mcount trampoline call the function directly.
  163. */
  164. } else if (rcu_dereference_protected(ftrace_ops_list->next,
  165. lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
  166. func = ftrace_ops_get_list_func(ftrace_ops_list);
  167. } else {
  168. /* Just use the default ftrace_ops */
  169. set_function_trace_op = &ftrace_list_end;
  170. func = ftrace_ops_list_func;
  171. }
  172. update_function_graph_func();
  173. /* If there's no change, then do nothing more here */
  174. if (ftrace_trace_function == func)
  175. return;
  176. /*
  177. * If we are using the list function, it doesn't care
  178. * about the function_trace_ops.
  179. */
  180. if (func == ftrace_ops_list_func) {
  181. ftrace_trace_function = func;
  182. /*
  183. * Don't even bother setting function_trace_ops,
  184. * it would be racy to do so anyway.
  185. */
  186. return;
  187. }
  188. #ifndef CONFIG_DYNAMIC_FTRACE
  189. /*
  190. * For static tracing, we need to be a bit more careful.
  191. * The function change takes affect immediately. Thus,
  192. * we need to coordinate the setting of the function_trace_ops
  193. * with the setting of the ftrace_trace_function.
  194. *
  195. * Set the function to the list ops, which will call the
  196. * function we want, albeit indirectly, but it handles the
  197. * ftrace_ops and doesn't depend on function_trace_op.
  198. */
  199. ftrace_trace_function = ftrace_ops_list_func;
  200. /*
  201. * Make sure all CPUs see this. Yes this is slow, but static
  202. * tracing is slow and nasty to have enabled.
  203. */
  204. synchronize_rcu_tasks_rude();
  205. /* Now all cpus are using the list ops. */
  206. function_trace_op = set_function_trace_op;
  207. /* Make sure the function_trace_op is visible on all CPUs */
  208. smp_wmb();
  209. /* Nasty way to force a rmb on all cpus */
  210. smp_call_function(ftrace_sync_ipi, NULL, 1);
  211. /* OK, we are all set to update the ftrace_trace_function now! */
  212. #endif /* !CONFIG_DYNAMIC_FTRACE */
  213. ftrace_trace_function = func;
  214. }
  215. static void add_ftrace_ops(struct ftrace_ops __rcu **list,
  216. struct ftrace_ops *ops)
  217. {
  218. rcu_assign_pointer(ops->next, *list);
  219. /*
  220. * We are entering ops into the list but another
  221. * CPU might be walking that list. We need to make sure
  222. * the ops->next pointer is valid before another CPU sees
  223. * the ops pointer included into the list.
  224. */
  225. rcu_assign_pointer(*list, ops);
  226. }
  227. static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
  228. struct ftrace_ops *ops)
  229. {
  230. struct ftrace_ops **p;
  231. /*
  232. * If we are removing the last function, then simply point
  233. * to the ftrace_stub.
  234. */
  235. if (rcu_dereference_protected(*list,
  236. lockdep_is_held(&ftrace_lock)) == ops &&
  237. rcu_dereference_protected(ops->next,
  238. lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
  239. *list = &ftrace_list_end;
  240. return 0;
  241. }
  242. for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
  243. if (*p == ops)
  244. break;
  245. if (*p != ops)
  246. return -1;
  247. *p = (*p)->next;
  248. return 0;
  249. }
  250. static void ftrace_update_trampoline(struct ftrace_ops *ops);
  251. int __register_ftrace_function(struct ftrace_ops *ops)
  252. {
  253. if (ops->flags & FTRACE_OPS_FL_DELETED)
  254. return -EINVAL;
  255. if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
  256. return -EBUSY;
  257. #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  258. /*
  259. * If the ftrace_ops specifies SAVE_REGS, then it only can be used
  260. * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
  261. * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
  262. */
  263. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
  264. !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
  265. return -EINVAL;
  266. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
  267. ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
  268. #endif
  269. if (!ftrace_enabled && (ops->flags & FTRACE_OPS_FL_PERMANENT))
  270. return -EBUSY;
  271. if (!is_kernel_core_data((unsigned long)ops))
  272. ops->flags |= FTRACE_OPS_FL_DYNAMIC;
  273. add_ftrace_ops(&ftrace_ops_list, ops);
  274. /* Always save the function, and reset at unregistering */
  275. ops->saved_func = ops->func;
  276. if (ftrace_pids_enabled(ops))
  277. ops->func = ftrace_pid_func;
  278. ftrace_update_trampoline(ops);
  279. if (ftrace_enabled)
  280. update_ftrace_function();
  281. return 0;
  282. }
  283. int __unregister_ftrace_function(struct ftrace_ops *ops)
  284. {
  285. int ret;
  286. if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
  287. return -EBUSY;
  288. ret = remove_ftrace_ops(&ftrace_ops_list, ops);
  289. if (ret < 0)
  290. return ret;
  291. if (ftrace_enabled)
  292. update_ftrace_function();
  293. ops->func = ops->saved_func;
  294. return 0;
  295. }
  296. static void ftrace_update_pid_func(void)
  297. {
  298. struct ftrace_ops *op;
  299. /* Only do something if we are tracing something */
  300. if (ftrace_trace_function == ftrace_stub)
  301. return;
  302. do_for_each_ftrace_op(op, ftrace_ops_list) {
  303. if (op->flags & FTRACE_OPS_FL_PID) {
  304. op->func = ftrace_pids_enabled(op) ?
  305. ftrace_pid_func : op->saved_func;
  306. ftrace_update_trampoline(op);
  307. }
  308. } while_for_each_ftrace_op(op);
  309. update_ftrace_function();
  310. }
  311. #ifdef CONFIG_FUNCTION_PROFILER
  312. struct ftrace_profile {
  313. struct hlist_node node;
  314. unsigned long ip;
  315. unsigned long counter;
  316. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  317. unsigned long long time;
  318. unsigned long long time_squared;
  319. #endif
  320. };
  321. struct ftrace_profile_page {
  322. struct ftrace_profile_page *next;
  323. unsigned long index;
  324. struct ftrace_profile records[];
  325. };
  326. struct ftrace_profile_stat {
  327. atomic_t disabled;
  328. struct hlist_head *hash;
  329. struct ftrace_profile_page *pages;
  330. struct ftrace_profile_page *start;
  331. struct tracer_stat stat;
  332. };
  333. #define PROFILE_RECORDS_SIZE \
  334. (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
  335. #define PROFILES_PER_PAGE \
  336. (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
  337. static int ftrace_profile_enabled __read_mostly;
  338. /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
  339. static DEFINE_MUTEX(ftrace_profile_lock);
  340. static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
  341. #define FTRACE_PROFILE_HASH_BITS 10
  342. #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
  343. static void *
  344. function_stat_next(void *v, int idx)
  345. {
  346. struct ftrace_profile *rec = v;
  347. struct ftrace_profile_page *pg;
  348. pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
  349. again:
  350. if (idx != 0)
  351. rec++;
  352. if ((void *)rec >= (void *)&pg->records[pg->index]) {
  353. pg = pg->next;
  354. if (!pg)
  355. return NULL;
  356. rec = &pg->records[0];
  357. if (!rec->counter)
  358. goto again;
  359. }
  360. return rec;
  361. }
  362. static void *function_stat_start(struct tracer_stat *trace)
  363. {
  364. struct ftrace_profile_stat *stat =
  365. container_of(trace, struct ftrace_profile_stat, stat);
  366. if (!stat || !stat->start)
  367. return NULL;
  368. return function_stat_next(&stat->start->records[0], 0);
  369. }
  370. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  371. /* function graph compares on total time */
  372. static int function_stat_cmp(const void *p1, const void *p2)
  373. {
  374. const struct ftrace_profile *a = p1;
  375. const struct ftrace_profile *b = p2;
  376. if (a->time < b->time)
  377. return -1;
  378. if (a->time > b->time)
  379. return 1;
  380. else
  381. return 0;
  382. }
  383. #else
  384. /* not function graph compares against hits */
  385. static int function_stat_cmp(const void *p1, const void *p2)
  386. {
  387. const struct ftrace_profile *a = p1;
  388. const struct ftrace_profile *b = p2;
  389. if (a->counter < b->counter)
  390. return -1;
  391. if (a->counter > b->counter)
  392. return 1;
  393. else
  394. return 0;
  395. }
  396. #endif
  397. static int function_stat_headers(struct seq_file *m)
  398. {
  399. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  400. seq_puts(m, " Function "
  401. "Hit Time Avg s^2\n"
  402. " -------- "
  403. "--- ---- --- ---\n");
  404. #else
  405. seq_puts(m, " Function Hit\n"
  406. " -------- ---\n");
  407. #endif
  408. return 0;
  409. }
  410. static int function_stat_show(struct seq_file *m, void *v)
  411. {
  412. struct ftrace_profile *rec = v;
  413. char str[KSYM_SYMBOL_LEN];
  414. int ret = 0;
  415. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  416. static struct trace_seq s;
  417. unsigned long long avg;
  418. unsigned long long stddev;
  419. #endif
  420. mutex_lock(&ftrace_profile_lock);
  421. /* we raced with function_profile_reset() */
  422. if (unlikely(rec->counter == 0)) {
  423. ret = -EBUSY;
  424. goto out;
  425. }
  426. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  427. avg = div64_ul(rec->time, rec->counter);
  428. if (tracing_thresh && (avg < tracing_thresh))
  429. goto out;
  430. #endif
  431. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  432. seq_printf(m, " %-30.30s %10lu", str, rec->counter);
  433. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  434. seq_puts(m, " ");
  435. /* Sample standard deviation (s^2) */
  436. if (rec->counter <= 1)
  437. stddev = 0;
  438. else {
  439. /*
  440. * Apply Welford's method:
  441. * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
  442. */
  443. stddev = rec->counter * rec->time_squared -
  444. rec->time * rec->time;
  445. /*
  446. * Divide only 1000 for ns^2 -> us^2 conversion.
  447. * trace_print_graph_duration will divide 1000 again.
  448. */
  449. stddev = div64_ul(stddev,
  450. rec->counter * (rec->counter - 1) * 1000);
  451. }
  452. trace_seq_init(&s);
  453. trace_print_graph_duration(rec->time, &s);
  454. trace_seq_puts(&s, " ");
  455. trace_print_graph_duration(avg, &s);
  456. trace_seq_puts(&s, " ");
  457. trace_print_graph_duration(stddev, &s);
  458. trace_print_seq(m, &s);
  459. #endif
  460. seq_putc(m, '\n');
  461. out:
  462. mutex_unlock(&ftrace_profile_lock);
  463. return ret;
  464. }
  465. static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
  466. {
  467. struct ftrace_profile_page *pg;
  468. pg = stat->pages = stat->start;
  469. while (pg) {
  470. memset(pg->records, 0, PROFILE_RECORDS_SIZE);
  471. pg->index = 0;
  472. pg = pg->next;
  473. }
  474. memset(stat->hash, 0,
  475. FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
  476. }
  477. static int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
  478. {
  479. struct ftrace_profile_page *pg;
  480. int functions;
  481. int pages;
  482. int i;
  483. /* If we already allocated, do nothing */
  484. if (stat->pages)
  485. return 0;
  486. stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
  487. if (!stat->pages)
  488. return -ENOMEM;
  489. #ifdef CONFIG_DYNAMIC_FTRACE
  490. functions = ftrace_update_tot_cnt;
  491. #else
  492. /*
  493. * We do not know the number of functions that exist because
  494. * dynamic tracing is what counts them. With past experience
  495. * we have around 20K functions. That should be more than enough.
  496. * It is highly unlikely we will execute every function in
  497. * the kernel.
  498. */
  499. functions = 20000;
  500. #endif
  501. pg = stat->start = stat->pages;
  502. pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
  503. for (i = 1; i < pages; i++) {
  504. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  505. if (!pg->next)
  506. goto out_free;
  507. pg = pg->next;
  508. }
  509. return 0;
  510. out_free:
  511. pg = stat->start;
  512. while (pg) {
  513. unsigned long tmp = (unsigned long)pg;
  514. pg = pg->next;
  515. free_page(tmp);
  516. }
  517. stat->pages = NULL;
  518. stat->start = NULL;
  519. return -ENOMEM;
  520. }
  521. static int ftrace_profile_init_cpu(int cpu)
  522. {
  523. struct ftrace_profile_stat *stat;
  524. int size;
  525. stat = &per_cpu(ftrace_profile_stats, cpu);
  526. if (stat->hash) {
  527. /* If the profile is already created, simply reset it */
  528. ftrace_profile_reset(stat);
  529. return 0;
  530. }
  531. /*
  532. * We are profiling all functions, but usually only a few thousand
  533. * functions are hit. We'll make a hash of 1024 items.
  534. */
  535. size = FTRACE_PROFILE_HASH_SIZE;
  536. stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL);
  537. if (!stat->hash)
  538. return -ENOMEM;
  539. /* Preallocate the function profiling pages */
  540. if (ftrace_profile_pages_init(stat) < 0) {
  541. kfree(stat->hash);
  542. stat->hash = NULL;
  543. return -ENOMEM;
  544. }
  545. return 0;
  546. }
  547. static int ftrace_profile_init(void)
  548. {
  549. int cpu;
  550. int ret = 0;
  551. for_each_possible_cpu(cpu) {
  552. ret = ftrace_profile_init_cpu(cpu);
  553. if (ret)
  554. break;
  555. }
  556. return ret;
  557. }
  558. /* interrupts must be disabled */
  559. static struct ftrace_profile *
  560. ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
  561. {
  562. struct ftrace_profile *rec;
  563. struct hlist_head *hhd;
  564. unsigned long key;
  565. key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
  566. hhd = &stat->hash[key];
  567. if (hlist_empty(hhd))
  568. return NULL;
  569. hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
  570. if (rec->ip == ip)
  571. return rec;
  572. }
  573. return NULL;
  574. }
  575. static void ftrace_add_profile(struct ftrace_profile_stat *stat,
  576. struct ftrace_profile *rec)
  577. {
  578. unsigned long key;
  579. key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
  580. hlist_add_head_rcu(&rec->node, &stat->hash[key]);
  581. }
  582. /*
  583. * The memory is already allocated, this simply finds a new record to use.
  584. */
  585. static struct ftrace_profile *
  586. ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
  587. {
  588. struct ftrace_profile *rec = NULL;
  589. /* prevent recursion (from NMIs) */
  590. if (atomic_inc_return(&stat->disabled) != 1)
  591. goto out;
  592. /*
  593. * Try to find the function again since an NMI
  594. * could have added it
  595. */
  596. rec = ftrace_find_profiled_func(stat, ip);
  597. if (rec)
  598. goto out;
  599. if (stat->pages->index == PROFILES_PER_PAGE) {
  600. if (!stat->pages->next)
  601. goto out;
  602. stat->pages = stat->pages->next;
  603. }
  604. rec = &stat->pages->records[stat->pages->index++];
  605. rec->ip = ip;
  606. ftrace_add_profile(stat, rec);
  607. out:
  608. atomic_dec(&stat->disabled);
  609. return rec;
  610. }
  611. static void
  612. function_profile_call(unsigned long ip, unsigned long parent_ip,
  613. struct ftrace_ops *ops, struct ftrace_regs *fregs)
  614. {
  615. struct ftrace_profile_stat *stat;
  616. struct ftrace_profile *rec;
  617. unsigned long flags;
  618. if (!ftrace_profile_enabled)
  619. return;
  620. local_irq_save(flags);
  621. stat = this_cpu_ptr(&ftrace_profile_stats);
  622. if (!stat->hash || !ftrace_profile_enabled)
  623. goto out;
  624. rec = ftrace_find_profiled_func(stat, ip);
  625. if (!rec) {
  626. rec = ftrace_profile_alloc(stat, ip);
  627. if (!rec)
  628. goto out;
  629. }
  630. rec->counter++;
  631. out:
  632. local_irq_restore(flags);
  633. }
  634. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  635. static bool fgraph_graph_time = true;
  636. void ftrace_graph_graph_time_control(bool enable)
  637. {
  638. fgraph_graph_time = enable;
  639. }
  640. static int profile_graph_entry(struct ftrace_graph_ent *trace)
  641. {
  642. struct ftrace_ret_stack *ret_stack;
  643. function_profile_call(trace->func, 0, NULL, NULL);
  644. /* If function graph is shutting down, ret_stack can be NULL */
  645. if (!current->ret_stack)
  646. return 0;
  647. ret_stack = ftrace_graph_get_ret_stack(current, 0);
  648. if (ret_stack)
  649. ret_stack->subtime = 0;
  650. return 1;
  651. }
  652. static void profile_graph_return(struct ftrace_graph_ret *trace)
  653. {
  654. struct ftrace_ret_stack *ret_stack;
  655. struct ftrace_profile_stat *stat;
  656. unsigned long long calltime;
  657. struct ftrace_profile *rec;
  658. unsigned long flags;
  659. local_irq_save(flags);
  660. stat = this_cpu_ptr(&ftrace_profile_stats);
  661. if (!stat->hash || !ftrace_profile_enabled)
  662. goto out;
  663. /* If the calltime was zero'd ignore it */
  664. if (!trace->calltime)
  665. goto out;
  666. calltime = trace->rettime - trace->calltime;
  667. if (!fgraph_graph_time) {
  668. /* Append this call time to the parent time to subtract */
  669. ret_stack = ftrace_graph_get_ret_stack(current, 1);
  670. if (ret_stack)
  671. ret_stack->subtime += calltime;
  672. ret_stack = ftrace_graph_get_ret_stack(current, 0);
  673. if (ret_stack && ret_stack->subtime < calltime)
  674. calltime -= ret_stack->subtime;
  675. else
  676. calltime = 0;
  677. }
  678. rec = ftrace_find_profiled_func(stat, trace->func);
  679. if (rec) {
  680. rec->time += calltime;
  681. rec->time_squared += calltime * calltime;
  682. }
  683. out:
  684. local_irq_restore(flags);
  685. }
  686. static struct fgraph_ops fprofiler_ops = {
  687. .entryfunc = &profile_graph_entry,
  688. .retfunc = &profile_graph_return,
  689. };
  690. static int register_ftrace_profiler(void)
  691. {
  692. return register_ftrace_graph(&fprofiler_ops);
  693. }
  694. static void unregister_ftrace_profiler(void)
  695. {
  696. unregister_ftrace_graph(&fprofiler_ops);
  697. }
  698. #else
  699. static struct ftrace_ops ftrace_profile_ops __read_mostly = {
  700. .func = function_profile_call,
  701. .flags = FTRACE_OPS_FL_INITIALIZED,
  702. INIT_OPS_HASH(ftrace_profile_ops)
  703. };
  704. static int register_ftrace_profiler(void)
  705. {
  706. return register_ftrace_function(&ftrace_profile_ops);
  707. }
  708. static void unregister_ftrace_profiler(void)
  709. {
  710. unregister_ftrace_function(&ftrace_profile_ops);
  711. }
  712. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  713. static ssize_t
  714. ftrace_profile_write(struct file *filp, const char __user *ubuf,
  715. size_t cnt, loff_t *ppos)
  716. {
  717. unsigned long val;
  718. int ret;
  719. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  720. if (ret)
  721. return ret;
  722. val = !!val;
  723. mutex_lock(&ftrace_profile_lock);
  724. if (ftrace_profile_enabled ^ val) {
  725. if (val) {
  726. ret = ftrace_profile_init();
  727. if (ret < 0) {
  728. cnt = ret;
  729. goto out;
  730. }
  731. ret = register_ftrace_profiler();
  732. if (ret < 0) {
  733. cnt = ret;
  734. goto out;
  735. }
  736. ftrace_profile_enabled = 1;
  737. } else {
  738. ftrace_profile_enabled = 0;
  739. /*
  740. * unregister_ftrace_profiler calls stop_machine
  741. * so this acts like an synchronize_rcu.
  742. */
  743. unregister_ftrace_profiler();
  744. }
  745. }
  746. out:
  747. mutex_unlock(&ftrace_profile_lock);
  748. *ppos += cnt;
  749. return cnt;
  750. }
  751. static ssize_t
  752. ftrace_profile_read(struct file *filp, char __user *ubuf,
  753. size_t cnt, loff_t *ppos)
  754. {
  755. char buf[64]; /* big enough to hold a number */
  756. int r;
  757. r = sprintf(buf, "%u\n", ftrace_profile_enabled);
  758. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  759. }
  760. static const struct file_operations ftrace_profile_fops = {
  761. .open = tracing_open_generic,
  762. .read = ftrace_profile_read,
  763. .write = ftrace_profile_write,
  764. .llseek = default_llseek,
  765. };
  766. /* used to initialize the real stat files */
  767. static struct tracer_stat function_stats __initdata = {
  768. .name = "functions",
  769. .stat_start = function_stat_start,
  770. .stat_next = function_stat_next,
  771. .stat_cmp = function_stat_cmp,
  772. .stat_headers = function_stat_headers,
  773. .stat_show = function_stat_show
  774. };
  775. static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
  776. {
  777. struct ftrace_profile_stat *stat;
  778. char *name;
  779. int ret;
  780. int cpu;
  781. for_each_possible_cpu(cpu) {
  782. stat = &per_cpu(ftrace_profile_stats, cpu);
  783. name = kasprintf(GFP_KERNEL, "function%d", cpu);
  784. if (!name) {
  785. /*
  786. * The files created are permanent, if something happens
  787. * we still do not free memory.
  788. */
  789. WARN(1,
  790. "Could not allocate stat file for cpu %d\n",
  791. cpu);
  792. return;
  793. }
  794. stat->stat = function_stats;
  795. stat->stat.name = name;
  796. ret = register_stat_tracer(&stat->stat);
  797. if (ret) {
  798. WARN(1,
  799. "Could not register function stat for cpu %d\n",
  800. cpu);
  801. kfree(name);
  802. return;
  803. }
  804. }
  805. trace_create_file("function_profile_enabled",
  806. TRACE_MODE_WRITE, d_tracer, NULL,
  807. &ftrace_profile_fops);
  808. }
  809. #else /* CONFIG_FUNCTION_PROFILER */
  810. static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
  811. {
  812. }
  813. #endif /* CONFIG_FUNCTION_PROFILER */
  814. #ifdef CONFIG_DYNAMIC_FTRACE
  815. static struct ftrace_ops *removed_ops;
  816. /*
  817. * Set when doing a global update, like enabling all recs or disabling them.
  818. * It is not set when just updating a single ftrace_ops.
  819. */
  820. static bool update_all_ops;
  821. #ifndef CONFIG_FTRACE_MCOUNT_RECORD
  822. # error Dynamic ftrace depends on MCOUNT_RECORD
  823. #endif
  824. struct ftrace_func_probe {
  825. struct ftrace_probe_ops *probe_ops;
  826. struct ftrace_ops ops;
  827. struct trace_array *tr;
  828. struct list_head list;
  829. void *data;
  830. int ref;
  831. };
  832. /*
  833. * We make these constant because no one should touch them,
  834. * but they are used as the default "empty hash", to avoid allocating
  835. * it all the time. These are in a read only section such that if
  836. * anyone does try to modify it, it will cause an exception.
  837. */
  838. static const struct hlist_head empty_buckets[1];
  839. static const struct ftrace_hash empty_hash = {
  840. .buckets = (struct hlist_head *)empty_buckets,
  841. };
  842. #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
  843. struct ftrace_ops global_ops = {
  844. .func = ftrace_stub,
  845. .local_hash.notrace_hash = EMPTY_HASH,
  846. .local_hash.filter_hash = EMPTY_HASH,
  847. INIT_OPS_HASH(global_ops)
  848. .flags = FTRACE_OPS_FL_INITIALIZED |
  849. FTRACE_OPS_FL_PID,
  850. };
  851. /*
  852. * Used by the stack unwinder to know about dynamic ftrace trampolines.
  853. */
  854. struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr)
  855. {
  856. struct ftrace_ops *op = NULL;
  857. /*
  858. * Some of the ops may be dynamically allocated,
  859. * they are freed after a synchronize_rcu().
  860. */
  861. preempt_disable_notrace();
  862. do_for_each_ftrace_op(op, ftrace_ops_list) {
  863. /*
  864. * This is to check for dynamically allocated trampolines.
  865. * Trampolines that are in kernel text will have
  866. * core_kernel_text() return true.
  867. */
  868. if (op->trampoline && op->trampoline_size)
  869. if (addr >= op->trampoline &&
  870. addr < op->trampoline + op->trampoline_size) {
  871. preempt_enable_notrace();
  872. return op;
  873. }
  874. } while_for_each_ftrace_op(op);
  875. preempt_enable_notrace();
  876. return NULL;
  877. }
  878. /*
  879. * This is used by __kernel_text_address() to return true if the
  880. * address is on a dynamically allocated trampoline that would
  881. * not return true for either core_kernel_text() or
  882. * is_module_text_address().
  883. */
  884. bool is_ftrace_trampoline(unsigned long addr)
  885. {
  886. return ftrace_ops_trampoline(addr) != NULL;
  887. }
  888. struct ftrace_page {
  889. struct ftrace_page *next;
  890. struct dyn_ftrace *records;
  891. int index;
  892. int order;
  893. };
  894. #define ENTRY_SIZE sizeof(struct dyn_ftrace)
  895. #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
  896. static struct ftrace_page *ftrace_pages_start;
  897. static struct ftrace_page *ftrace_pages;
  898. static __always_inline unsigned long
  899. ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
  900. {
  901. if (hash->size_bits > 0)
  902. return hash_long(ip, hash->size_bits);
  903. return 0;
  904. }
  905. /* Only use this function if ftrace_hash_empty() has already been tested */
  906. static __always_inline struct ftrace_func_entry *
  907. __ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
  908. {
  909. unsigned long key;
  910. struct ftrace_func_entry *entry;
  911. struct hlist_head *hhd;
  912. key = ftrace_hash_key(hash, ip);
  913. hhd = &hash->buckets[key];
  914. hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
  915. if (entry->ip == ip)
  916. return entry;
  917. }
  918. return NULL;
  919. }
  920. /**
  921. * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
  922. * @hash: The hash to look at
  923. * @ip: The instruction pointer to test
  924. *
  925. * Search a given @hash to see if a given instruction pointer (@ip)
  926. * exists in it.
  927. *
  928. * Returns the entry that holds the @ip if found. NULL otherwise.
  929. */
  930. struct ftrace_func_entry *
  931. ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
  932. {
  933. if (ftrace_hash_empty(hash))
  934. return NULL;
  935. return __ftrace_lookup_ip(hash, ip);
  936. }
  937. static void __add_hash_entry(struct ftrace_hash *hash,
  938. struct ftrace_func_entry *entry)
  939. {
  940. struct hlist_head *hhd;
  941. unsigned long key;
  942. key = ftrace_hash_key(hash, entry->ip);
  943. hhd = &hash->buckets[key];
  944. hlist_add_head(&entry->hlist, hhd);
  945. hash->count++;
  946. }
  947. static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
  948. {
  949. struct ftrace_func_entry *entry;
  950. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  951. if (!entry)
  952. return -ENOMEM;
  953. entry->ip = ip;
  954. __add_hash_entry(hash, entry);
  955. return 0;
  956. }
  957. static void
  958. free_hash_entry(struct ftrace_hash *hash,
  959. struct ftrace_func_entry *entry)
  960. {
  961. hlist_del(&entry->hlist);
  962. kfree(entry);
  963. hash->count--;
  964. }
  965. static void
  966. remove_hash_entry(struct ftrace_hash *hash,
  967. struct ftrace_func_entry *entry)
  968. {
  969. hlist_del_rcu(&entry->hlist);
  970. hash->count--;
  971. }
  972. static void ftrace_hash_clear(struct ftrace_hash *hash)
  973. {
  974. struct hlist_head *hhd;
  975. struct hlist_node *tn;
  976. struct ftrace_func_entry *entry;
  977. int size = 1 << hash->size_bits;
  978. int i;
  979. if (!hash->count)
  980. return;
  981. for (i = 0; i < size; i++) {
  982. hhd = &hash->buckets[i];
  983. hlist_for_each_entry_safe(entry, tn, hhd, hlist)
  984. free_hash_entry(hash, entry);
  985. }
  986. FTRACE_WARN_ON(hash->count);
  987. }
  988. static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
  989. {
  990. list_del(&ftrace_mod->list);
  991. kfree(ftrace_mod->module);
  992. kfree(ftrace_mod->func);
  993. kfree(ftrace_mod);
  994. }
  995. static void clear_ftrace_mod_list(struct list_head *head)
  996. {
  997. struct ftrace_mod_load *p, *n;
  998. /* stack tracer isn't supported yet */
  999. if (!head)
  1000. return;
  1001. mutex_lock(&ftrace_lock);
  1002. list_for_each_entry_safe(p, n, head, list)
  1003. free_ftrace_mod(p);
  1004. mutex_unlock(&ftrace_lock);
  1005. }
  1006. static void free_ftrace_hash(struct ftrace_hash *hash)
  1007. {
  1008. if (!hash || hash == EMPTY_HASH)
  1009. return;
  1010. ftrace_hash_clear(hash);
  1011. kfree(hash->buckets);
  1012. kfree(hash);
  1013. }
  1014. static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
  1015. {
  1016. struct ftrace_hash *hash;
  1017. hash = container_of(rcu, struct ftrace_hash, rcu);
  1018. free_ftrace_hash(hash);
  1019. }
  1020. static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
  1021. {
  1022. if (!hash || hash == EMPTY_HASH)
  1023. return;
  1024. call_rcu(&hash->rcu, __free_ftrace_hash_rcu);
  1025. }
  1026. /**
  1027. * ftrace_free_filter - remove all filters for an ftrace_ops
  1028. * @ops - the ops to remove the filters from
  1029. */
  1030. void ftrace_free_filter(struct ftrace_ops *ops)
  1031. {
  1032. ftrace_ops_init(ops);
  1033. free_ftrace_hash(ops->func_hash->filter_hash);
  1034. free_ftrace_hash(ops->func_hash->notrace_hash);
  1035. }
  1036. EXPORT_SYMBOL_GPL(ftrace_free_filter);
  1037. static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
  1038. {
  1039. struct ftrace_hash *hash;
  1040. int size;
  1041. hash = kzalloc(sizeof(*hash), GFP_KERNEL);
  1042. if (!hash)
  1043. return NULL;
  1044. size = 1 << size_bits;
  1045. hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
  1046. if (!hash->buckets) {
  1047. kfree(hash);
  1048. return NULL;
  1049. }
  1050. hash->size_bits = size_bits;
  1051. return hash;
  1052. }
  1053. static int ftrace_add_mod(struct trace_array *tr,
  1054. const char *func, const char *module,
  1055. int enable)
  1056. {
  1057. struct ftrace_mod_load *ftrace_mod;
  1058. struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
  1059. ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
  1060. if (!ftrace_mod)
  1061. return -ENOMEM;
  1062. INIT_LIST_HEAD(&ftrace_mod->list);
  1063. ftrace_mod->func = kstrdup(func, GFP_KERNEL);
  1064. ftrace_mod->module = kstrdup(module, GFP_KERNEL);
  1065. ftrace_mod->enable = enable;
  1066. if (!ftrace_mod->func || !ftrace_mod->module)
  1067. goto out_free;
  1068. list_add(&ftrace_mod->list, mod_head);
  1069. return 0;
  1070. out_free:
  1071. free_ftrace_mod(ftrace_mod);
  1072. return -ENOMEM;
  1073. }
  1074. static struct ftrace_hash *
  1075. alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
  1076. {
  1077. struct ftrace_func_entry *entry;
  1078. struct ftrace_hash *new_hash;
  1079. int size;
  1080. int ret;
  1081. int i;
  1082. new_hash = alloc_ftrace_hash(size_bits);
  1083. if (!new_hash)
  1084. return NULL;
  1085. if (hash)
  1086. new_hash->flags = hash->flags;
  1087. /* Empty hash? */
  1088. if (ftrace_hash_empty(hash))
  1089. return new_hash;
  1090. size = 1 << hash->size_bits;
  1091. for (i = 0; i < size; i++) {
  1092. hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
  1093. ret = add_hash_entry(new_hash, entry->ip);
  1094. if (ret < 0)
  1095. goto free_hash;
  1096. }
  1097. }
  1098. FTRACE_WARN_ON(new_hash->count != hash->count);
  1099. return new_hash;
  1100. free_hash:
  1101. free_ftrace_hash(new_hash);
  1102. return NULL;
  1103. }
  1104. static void
  1105. ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
  1106. static void
  1107. ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
  1108. static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
  1109. struct ftrace_hash *new_hash);
  1110. static struct ftrace_hash *dup_hash(struct ftrace_hash *src, int size)
  1111. {
  1112. struct ftrace_func_entry *entry;
  1113. struct ftrace_hash *new_hash;
  1114. struct hlist_head *hhd;
  1115. struct hlist_node *tn;
  1116. int bits = 0;
  1117. int i;
  1118. /*
  1119. * Use around half the size (max bit of it), but
  1120. * a minimum of 2 is fine (as size of 0 or 1 both give 1 for bits).
  1121. */
  1122. bits = fls(size / 2);
  1123. /* Don't allocate too much */
  1124. if (bits > FTRACE_HASH_MAX_BITS)
  1125. bits = FTRACE_HASH_MAX_BITS;
  1126. new_hash = alloc_ftrace_hash(bits);
  1127. if (!new_hash)
  1128. return NULL;
  1129. new_hash->flags = src->flags;
  1130. size = 1 << src->size_bits;
  1131. for (i = 0; i < size; i++) {
  1132. hhd = &src->buckets[i];
  1133. hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
  1134. remove_hash_entry(src, entry);
  1135. __add_hash_entry(new_hash, entry);
  1136. }
  1137. }
  1138. return new_hash;
  1139. }
  1140. static struct ftrace_hash *
  1141. __ftrace_hash_move(struct ftrace_hash *src)
  1142. {
  1143. int size = src->count;
  1144. /*
  1145. * If the new source is empty, just return the empty_hash.
  1146. */
  1147. if (ftrace_hash_empty(src))
  1148. return EMPTY_HASH;
  1149. return dup_hash(src, size);
  1150. }
  1151. static int
  1152. ftrace_hash_move(struct ftrace_ops *ops, int enable,
  1153. struct ftrace_hash **dst, struct ftrace_hash *src)
  1154. {
  1155. struct ftrace_hash *new_hash;
  1156. int ret;
  1157. /* Reject setting notrace hash on IPMODIFY ftrace_ops */
  1158. if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
  1159. return -EINVAL;
  1160. new_hash = __ftrace_hash_move(src);
  1161. if (!new_hash)
  1162. return -ENOMEM;
  1163. /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
  1164. if (enable) {
  1165. /* IPMODIFY should be updated only when filter_hash updating */
  1166. ret = ftrace_hash_ipmodify_update(ops, new_hash);
  1167. if (ret < 0) {
  1168. free_ftrace_hash(new_hash);
  1169. return ret;
  1170. }
  1171. }
  1172. /*
  1173. * Remove the current set, update the hash and add
  1174. * them back.
  1175. */
  1176. ftrace_hash_rec_disable_modify(ops, enable);
  1177. rcu_assign_pointer(*dst, new_hash);
  1178. ftrace_hash_rec_enable_modify(ops, enable);
  1179. return 0;
  1180. }
  1181. static bool hash_contains_ip(unsigned long ip,
  1182. struct ftrace_ops_hash *hash)
  1183. {
  1184. /*
  1185. * The function record is a match if it exists in the filter
  1186. * hash and not in the notrace hash. Note, an empty hash is
  1187. * considered a match for the filter hash, but an empty
  1188. * notrace hash is considered not in the notrace hash.
  1189. */
  1190. return (ftrace_hash_empty(hash->filter_hash) ||
  1191. __ftrace_lookup_ip(hash->filter_hash, ip)) &&
  1192. (ftrace_hash_empty(hash->notrace_hash) ||
  1193. !__ftrace_lookup_ip(hash->notrace_hash, ip));
  1194. }
  1195. /*
  1196. * Test the hashes for this ops to see if we want to call
  1197. * the ops->func or not.
  1198. *
  1199. * It's a match if the ip is in the ops->filter_hash or
  1200. * the filter_hash does not exist or is empty,
  1201. * AND
  1202. * the ip is not in the ops->notrace_hash.
  1203. *
  1204. * This needs to be called with preemption disabled as
  1205. * the hashes are freed with call_rcu().
  1206. */
  1207. int
  1208. ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
  1209. {
  1210. struct ftrace_ops_hash hash;
  1211. int ret;
  1212. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  1213. /*
  1214. * There's a small race when adding ops that the ftrace handler
  1215. * that wants regs, may be called without them. We can not
  1216. * allow that handler to be called if regs is NULL.
  1217. */
  1218. if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
  1219. return 0;
  1220. #endif
  1221. rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
  1222. rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
  1223. if (hash_contains_ip(ip, &hash))
  1224. ret = 1;
  1225. else
  1226. ret = 0;
  1227. return ret;
  1228. }
  1229. /*
  1230. * This is a double for. Do not use 'break' to break out of the loop,
  1231. * you must use a goto.
  1232. */
  1233. #define do_for_each_ftrace_rec(pg, rec) \
  1234. for (pg = ftrace_pages_start; pg; pg = pg->next) { \
  1235. int _____i; \
  1236. for (_____i = 0; _____i < pg->index; _____i++) { \
  1237. rec = &pg->records[_____i];
  1238. #define while_for_each_ftrace_rec() \
  1239. } \
  1240. }
  1241. static int ftrace_cmp_recs(const void *a, const void *b)
  1242. {
  1243. const struct dyn_ftrace *key = a;
  1244. const struct dyn_ftrace *rec = b;
  1245. if (key->flags < rec->ip)
  1246. return -1;
  1247. if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
  1248. return 1;
  1249. return 0;
  1250. }
  1251. static struct dyn_ftrace *lookup_rec(unsigned long start, unsigned long end)
  1252. {
  1253. struct ftrace_page *pg;
  1254. struct dyn_ftrace *rec = NULL;
  1255. struct dyn_ftrace key;
  1256. key.ip = start;
  1257. key.flags = end; /* overload flags, as it is unsigned long */
  1258. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  1259. if (pg->index == 0 ||
  1260. end < pg->records[0].ip ||
  1261. start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
  1262. continue;
  1263. rec = bsearch(&key, pg->records, pg->index,
  1264. sizeof(struct dyn_ftrace),
  1265. ftrace_cmp_recs);
  1266. if (rec)
  1267. break;
  1268. }
  1269. return rec;
  1270. }
  1271. /**
  1272. * ftrace_location_range - return the first address of a traced location
  1273. * if it touches the given ip range
  1274. * @start: start of range to search.
  1275. * @end: end of range to search (inclusive). @end points to the last byte
  1276. * to check.
  1277. *
  1278. * Returns rec->ip if the related ftrace location is a least partly within
  1279. * the given address range. That is, the first address of the instruction
  1280. * that is either a NOP or call to the function tracer. It checks the ftrace
  1281. * internal tables to determine if the address belongs or not.
  1282. */
  1283. unsigned long ftrace_location_range(unsigned long start, unsigned long end)
  1284. {
  1285. struct dyn_ftrace *rec;
  1286. rec = lookup_rec(start, end);
  1287. if (rec)
  1288. return rec->ip;
  1289. return 0;
  1290. }
  1291. /**
  1292. * ftrace_location - return the ftrace location
  1293. * @ip: the instruction pointer to check
  1294. *
  1295. * If @ip matches the ftrace location, return @ip.
  1296. * If @ip matches sym+0, return sym's ftrace location.
  1297. * Otherwise, return 0.
  1298. */
  1299. unsigned long ftrace_location(unsigned long ip)
  1300. {
  1301. struct dyn_ftrace *rec;
  1302. unsigned long offset;
  1303. unsigned long size;
  1304. rec = lookup_rec(ip, ip);
  1305. if (!rec) {
  1306. if (!kallsyms_lookup_size_offset(ip, &size, &offset))
  1307. goto out;
  1308. /* map sym+0 to __fentry__ */
  1309. if (!offset)
  1310. rec = lookup_rec(ip, ip + size - 1);
  1311. }
  1312. if (rec)
  1313. return rec->ip;
  1314. out:
  1315. return 0;
  1316. }
  1317. /**
  1318. * ftrace_text_reserved - return true if range contains an ftrace location
  1319. * @start: start of range to search
  1320. * @end: end of range to search (inclusive). @end points to the last byte to check.
  1321. *
  1322. * Returns 1 if @start and @end contains a ftrace location.
  1323. * That is, the instruction that is either a NOP or call to
  1324. * the function tracer. It checks the ftrace internal tables to
  1325. * determine if the address belongs or not.
  1326. */
  1327. int ftrace_text_reserved(const void *start, const void *end)
  1328. {
  1329. unsigned long ret;
  1330. ret = ftrace_location_range((unsigned long)start,
  1331. (unsigned long)end);
  1332. return (int)!!ret;
  1333. }
  1334. /* Test if ops registered to this rec needs regs */
  1335. static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
  1336. {
  1337. struct ftrace_ops *ops;
  1338. bool keep_regs = false;
  1339. for (ops = ftrace_ops_list;
  1340. ops != &ftrace_list_end; ops = ops->next) {
  1341. /* pass rec in as regs to have non-NULL val */
  1342. if (ftrace_ops_test(ops, rec->ip, rec)) {
  1343. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
  1344. keep_regs = true;
  1345. break;
  1346. }
  1347. }
  1348. }
  1349. return keep_regs;
  1350. }
  1351. static struct ftrace_ops *
  1352. ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
  1353. static struct ftrace_ops *
  1354. ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude);
  1355. static struct ftrace_ops *
  1356. ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
  1357. static bool skip_record(struct dyn_ftrace *rec)
  1358. {
  1359. /*
  1360. * At boot up, weak functions are set to disable. Function tracing
  1361. * can be enabled before they are, and they still need to be disabled now.
  1362. * If the record is disabled, still continue if it is marked as already
  1363. * enabled (this is needed to keep the accounting working).
  1364. */
  1365. return rec->flags & FTRACE_FL_DISABLED &&
  1366. !(rec->flags & FTRACE_FL_ENABLED);
  1367. }
  1368. static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
  1369. int filter_hash,
  1370. bool inc)
  1371. {
  1372. struct ftrace_hash *hash;
  1373. struct ftrace_hash *other_hash;
  1374. struct ftrace_page *pg;
  1375. struct dyn_ftrace *rec;
  1376. bool update = false;
  1377. int count = 0;
  1378. int all = false;
  1379. /* Only update if the ops has been registered */
  1380. if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
  1381. return false;
  1382. /*
  1383. * In the filter_hash case:
  1384. * If the count is zero, we update all records.
  1385. * Otherwise we just update the items in the hash.
  1386. *
  1387. * In the notrace_hash case:
  1388. * We enable the update in the hash.
  1389. * As disabling notrace means enabling the tracing,
  1390. * and enabling notrace means disabling, the inc variable
  1391. * gets inversed.
  1392. */
  1393. if (filter_hash) {
  1394. hash = ops->func_hash->filter_hash;
  1395. other_hash = ops->func_hash->notrace_hash;
  1396. if (ftrace_hash_empty(hash))
  1397. all = true;
  1398. } else {
  1399. inc = !inc;
  1400. hash = ops->func_hash->notrace_hash;
  1401. other_hash = ops->func_hash->filter_hash;
  1402. /*
  1403. * If the notrace hash has no items,
  1404. * then there's nothing to do.
  1405. */
  1406. if (ftrace_hash_empty(hash))
  1407. return false;
  1408. }
  1409. do_for_each_ftrace_rec(pg, rec) {
  1410. int in_other_hash = 0;
  1411. int in_hash = 0;
  1412. int match = 0;
  1413. if (skip_record(rec))
  1414. continue;
  1415. if (all) {
  1416. /*
  1417. * Only the filter_hash affects all records.
  1418. * Update if the record is not in the notrace hash.
  1419. */
  1420. if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
  1421. match = 1;
  1422. } else {
  1423. in_hash = !!ftrace_lookup_ip(hash, rec->ip);
  1424. in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
  1425. /*
  1426. * If filter_hash is set, we want to match all functions
  1427. * that are in the hash but not in the other hash.
  1428. *
  1429. * If filter_hash is not set, then we are decrementing.
  1430. * That means we match anything that is in the hash
  1431. * and also in the other_hash. That is, we need to turn
  1432. * off functions in the other hash because they are disabled
  1433. * by this hash.
  1434. */
  1435. if (filter_hash && in_hash && !in_other_hash)
  1436. match = 1;
  1437. else if (!filter_hash && in_hash &&
  1438. (in_other_hash || ftrace_hash_empty(other_hash)))
  1439. match = 1;
  1440. }
  1441. if (!match)
  1442. continue;
  1443. if (inc) {
  1444. rec->flags++;
  1445. if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
  1446. return false;
  1447. if (ops->flags & FTRACE_OPS_FL_DIRECT)
  1448. rec->flags |= FTRACE_FL_DIRECT;
  1449. /*
  1450. * If there's only a single callback registered to a
  1451. * function, and the ops has a trampoline registered
  1452. * for it, then we can call it directly.
  1453. */
  1454. if (ftrace_rec_count(rec) == 1 && ops->trampoline)
  1455. rec->flags |= FTRACE_FL_TRAMP;
  1456. else
  1457. /*
  1458. * If we are adding another function callback
  1459. * to this function, and the previous had a
  1460. * custom trampoline in use, then we need to go
  1461. * back to the default trampoline.
  1462. */
  1463. rec->flags &= ~FTRACE_FL_TRAMP;
  1464. /*
  1465. * If any ops wants regs saved for this function
  1466. * then all ops will get saved regs.
  1467. */
  1468. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
  1469. rec->flags |= FTRACE_FL_REGS;
  1470. } else {
  1471. if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
  1472. return false;
  1473. rec->flags--;
  1474. /*
  1475. * Only the internal direct_ops should have the
  1476. * DIRECT flag set. Thus, if it is removing a
  1477. * function, then that function should no longer
  1478. * be direct.
  1479. */
  1480. if (ops->flags & FTRACE_OPS_FL_DIRECT)
  1481. rec->flags &= ~FTRACE_FL_DIRECT;
  1482. /*
  1483. * If the rec had REGS enabled and the ops that is
  1484. * being removed had REGS set, then see if there is
  1485. * still any ops for this record that wants regs.
  1486. * If not, we can stop recording them.
  1487. */
  1488. if (ftrace_rec_count(rec) > 0 &&
  1489. rec->flags & FTRACE_FL_REGS &&
  1490. ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
  1491. if (!test_rec_ops_needs_regs(rec))
  1492. rec->flags &= ~FTRACE_FL_REGS;
  1493. }
  1494. /*
  1495. * The TRAMP needs to be set only if rec count
  1496. * is decremented to one, and the ops that is
  1497. * left has a trampoline. As TRAMP can only be
  1498. * enabled if there is only a single ops attached
  1499. * to it.
  1500. */
  1501. if (ftrace_rec_count(rec) == 1 &&
  1502. ftrace_find_tramp_ops_any_other(rec, ops))
  1503. rec->flags |= FTRACE_FL_TRAMP;
  1504. else
  1505. rec->flags &= ~FTRACE_FL_TRAMP;
  1506. /*
  1507. * flags will be cleared in ftrace_check_record()
  1508. * if rec count is zero.
  1509. */
  1510. }
  1511. count++;
  1512. /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
  1513. update |= ftrace_test_record(rec, true) != FTRACE_UPDATE_IGNORE;
  1514. /* Shortcut, if we handled all records, we are done. */
  1515. if (!all && count == hash->count)
  1516. return update;
  1517. } while_for_each_ftrace_rec();
  1518. return update;
  1519. }
  1520. static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
  1521. int filter_hash)
  1522. {
  1523. return __ftrace_hash_rec_update(ops, filter_hash, 0);
  1524. }
  1525. static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
  1526. int filter_hash)
  1527. {
  1528. return __ftrace_hash_rec_update(ops, filter_hash, 1);
  1529. }
  1530. static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
  1531. int filter_hash, int inc)
  1532. {
  1533. struct ftrace_ops *op;
  1534. __ftrace_hash_rec_update(ops, filter_hash, inc);
  1535. if (ops->func_hash != &global_ops.local_hash)
  1536. return;
  1537. /*
  1538. * If the ops shares the global_ops hash, then we need to update
  1539. * all ops that are enabled and use this hash.
  1540. */
  1541. do_for_each_ftrace_op(op, ftrace_ops_list) {
  1542. /* Already done */
  1543. if (op == ops)
  1544. continue;
  1545. if (op->func_hash == &global_ops.local_hash)
  1546. __ftrace_hash_rec_update(op, filter_hash, inc);
  1547. } while_for_each_ftrace_op(op);
  1548. }
  1549. static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
  1550. int filter_hash)
  1551. {
  1552. ftrace_hash_rec_update_modify(ops, filter_hash, 0);
  1553. }
  1554. static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
  1555. int filter_hash)
  1556. {
  1557. ftrace_hash_rec_update_modify(ops, filter_hash, 1);
  1558. }
  1559. /*
  1560. * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
  1561. * or no-needed to update, -EBUSY if it detects a conflict of the flag
  1562. * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
  1563. * Note that old_hash and new_hash has below meanings
  1564. * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
  1565. * - If the hash is EMPTY_HASH, it hits nothing
  1566. * - Anything else hits the recs which match the hash entries.
  1567. *
  1568. * DIRECT ops does not have IPMODIFY flag, but we still need to check it
  1569. * against functions with FTRACE_FL_IPMODIFY. If there is any overlap, call
  1570. * ops_func(SHARE_IPMODIFY_SELF) to make sure current ops can share with
  1571. * IPMODIFY. If ops_func(SHARE_IPMODIFY_SELF) returns non-zero, propagate
  1572. * the return value to the caller and eventually to the owner of the DIRECT
  1573. * ops.
  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. bool is_ipmodify, is_direct;
  1583. /* Only update if the ops has been registered */
  1584. if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
  1585. return 0;
  1586. is_ipmodify = ops->flags & FTRACE_OPS_FL_IPMODIFY;
  1587. is_direct = ops->flags & FTRACE_OPS_FL_DIRECT;
  1588. /* neither IPMODIFY nor DIRECT, skip */
  1589. if (!is_ipmodify && !is_direct)
  1590. return 0;
  1591. if (WARN_ON_ONCE(is_ipmodify && is_direct))
  1592. return 0;
  1593. /*
  1594. * Since the IPMODIFY and DIRECT are very address sensitive
  1595. * actions, we do not allow ftrace_ops to set all functions to new
  1596. * hash.
  1597. */
  1598. if (!new_hash || !old_hash)
  1599. return -EINVAL;
  1600. /* Update rec->flags */
  1601. do_for_each_ftrace_rec(pg, rec) {
  1602. if (rec->flags & FTRACE_FL_DISABLED)
  1603. continue;
  1604. /* We need to update only differences of filter_hash */
  1605. in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
  1606. in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
  1607. if (in_old == in_new)
  1608. continue;
  1609. if (in_new) {
  1610. if (rec->flags & FTRACE_FL_IPMODIFY) {
  1611. int ret;
  1612. /* Cannot have two ipmodify on same rec */
  1613. if (is_ipmodify)
  1614. goto rollback;
  1615. FTRACE_WARN_ON(rec->flags & FTRACE_FL_DIRECT);
  1616. /*
  1617. * Another ops with IPMODIFY is already
  1618. * attached. We are now attaching a direct
  1619. * ops. Run SHARE_IPMODIFY_SELF, to check
  1620. * whether sharing is supported.
  1621. */
  1622. if (!ops->ops_func)
  1623. return -EBUSY;
  1624. ret = ops->ops_func(ops, FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_SELF);
  1625. if (ret)
  1626. return ret;
  1627. } else if (is_ipmodify) {
  1628. rec->flags |= FTRACE_FL_IPMODIFY;
  1629. }
  1630. } else if (is_ipmodify) {
  1631. rec->flags &= ~FTRACE_FL_IPMODIFY;
  1632. }
  1633. } while_for_each_ftrace_rec();
  1634. return 0;
  1635. rollback:
  1636. end = rec;
  1637. /* Roll back what we did above */
  1638. do_for_each_ftrace_rec(pg, rec) {
  1639. if (rec->flags & FTRACE_FL_DISABLED)
  1640. continue;
  1641. if (rec == end)
  1642. goto err_out;
  1643. in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
  1644. in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
  1645. if (in_old == in_new)
  1646. continue;
  1647. if (in_new)
  1648. rec->flags &= ~FTRACE_FL_IPMODIFY;
  1649. else
  1650. rec->flags |= FTRACE_FL_IPMODIFY;
  1651. } while_for_each_ftrace_rec();
  1652. err_out:
  1653. return -EBUSY;
  1654. }
  1655. static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
  1656. {
  1657. struct ftrace_hash *hash = ops->func_hash->filter_hash;
  1658. if (ftrace_hash_empty(hash))
  1659. hash = NULL;
  1660. return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
  1661. }
  1662. /* Disabling always succeeds */
  1663. static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
  1664. {
  1665. struct ftrace_hash *hash = ops->func_hash->filter_hash;
  1666. if (ftrace_hash_empty(hash))
  1667. hash = NULL;
  1668. __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
  1669. }
  1670. static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
  1671. struct ftrace_hash *new_hash)
  1672. {
  1673. struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
  1674. if (ftrace_hash_empty(old_hash))
  1675. old_hash = NULL;
  1676. if (ftrace_hash_empty(new_hash))
  1677. new_hash = NULL;
  1678. return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
  1679. }
  1680. static void print_ip_ins(const char *fmt, const unsigned char *p)
  1681. {
  1682. char ins[MCOUNT_INSN_SIZE];
  1683. if (copy_from_kernel_nofault(ins, p, MCOUNT_INSN_SIZE)) {
  1684. printk(KERN_CONT "%s[FAULT] %px\n", fmt, p);
  1685. return;
  1686. }
  1687. printk(KERN_CONT "%s", fmt);
  1688. pr_cont("%*phC", MCOUNT_INSN_SIZE, ins);
  1689. }
  1690. enum ftrace_bug_type ftrace_bug_type;
  1691. const void *ftrace_expected;
  1692. static void print_bug_type(void)
  1693. {
  1694. switch (ftrace_bug_type) {
  1695. case FTRACE_BUG_UNKNOWN:
  1696. break;
  1697. case FTRACE_BUG_INIT:
  1698. pr_info("Initializing ftrace call sites\n");
  1699. break;
  1700. case FTRACE_BUG_NOP:
  1701. pr_info("Setting ftrace call site to NOP\n");
  1702. break;
  1703. case FTRACE_BUG_CALL:
  1704. pr_info("Setting ftrace call site to call ftrace function\n");
  1705. break;
  1706. case FTRACE_BUG_UPDATE:
  1707. pr_info("Updating ftrace call site to call a different ftrace function\n");
  1708. break;
  1709. }
  1710. }
  1711. /**
  1712. * ftrace_bug - report and shutdown function tracer
  1713. * @failed: The failed type (EFAULT, EINVAL, EPERM)
  1714. * @rec: The record that failed
  1715. *
  1716. * The arch code that enables or disables the function tracing
  1717. * can call ftrace_bug() when it has detected a problem in
  1718. * modifying the code. @failed should be one of either:
  1719. * EFAULT - if the problem happens on reading the @ip address
  1720. * EINVAL - if what is read at @ip is not what was expected
  1721. * EPERM - if the problem happens on writing to the @ip address
  1722. */
  1723. void ftrace_bug(int failed, struct dyn_ftrace *rec)
  1724. {
  1725. unsigned long ip = rec ? rec->ip : 0;
  1726. pr_info("------------[ ftrace bug ]------------\n");
  1727. switch (failed) {
  1728. case -EFAULT:
  1729. pr_info("ftrace faulted on modifying ");
  1730. print_ip_sym(KERN_INFO, ip);
  1731. break;
  1732. case -EINVAL:
  1733. pr_info("ftrace failed to modify ");
  1734. print_ip_sym(KERN_INFO, ip);
  1735. print_ip_ins(" actual: ", (unsigned char *)ip);
  1736. pr_cont("\n");
  1737. if (ftrace_expected) {
  1738. print_ip_ins(" expected: ", ftrace_expected);
  1739. pr_cont("\n");
  1740. }
  1741. break;
  1742. case -EPERM:
  1743. pr_info("ftrace faulted on writing ");
  1744. print_ip_sym(KERN_INFO, ip);
  1745. break;
  1746. default:
  1747. pr_info("ftrace faulted on unknown error ");
  1748. print_ip_sym(KERN_INFO, ip);
  1749. }
  1750. print_bug_type();
  1751. if (rec) {
  1752. struct ftrace_ops *ops = NULL;
  1753. pr_info("ftrace record flags: %lx\n", rec->flags);
  1754. pr_cont(" (%ld)%s", ftrace_rec_count(rec),
  1755. rec->flags & FTRACE_FL_REGS ? " R" : " ");
  1756. if (rec->flags & FTRACE_FL_TRAMP_EN) {
  1757. ops = ftrace_find_tramp_ops_any(rec);
  1758. if (ops) {
  1759. do {
  1760. pr_cont("\ttramp: %pS (%pS)",
  1761. (void *)ops->trampoline,
  1762. (void *)ops->func);
  1763. ops = ftrace_find_tramp_ops_next(rec, ops);
  1764. } while (ops);
  1765. } else
  1766. pr_cont("\ttramp: ERROR!");
  1767. }
  1768. ip = ftrace_get_addr_curr(rec);
  1769. pr_cont("\n expected tramp: %lx\n", ip);
  1770. }
  1771. FTRACE_WARN_ON_ONCE(1);
  1772. }
  1773. static int ftrace_check_record(struct dyn_ftrace *rec, bool enable, bool update)
  1774. {
  1775. unsigned long flag = 0UL;
  1776. ftrace_bug_type = FTRACE_BUG_UNKNOWN;
  1777. if (skip_record(rec))
  1778. return FTRACE_UPDATE_IGNORE;
  1779. /*
  1780. * If we are updating calls:
  1781. *
  1782. * If the record has a ref count, then we need to enable it
  1783. * because someone is using it.
  1784. *
  1785. * Otherwise we make sure its disabled.
  1786. *
  1787. * If we are disabling calls, then disable all records that
  1788. * are enabled.
  1789. */
  1790. if (enable && ftrace_rec_count(rec))
  1791. flag = FTRACE_FL_ENABLED;
  1792. /*
  1793. * If enabling and the REGS flag does not match the REGS_EN, or
  1794. * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
  1795. * this record. Set flags to fail the compare against ENABLED.
  1796. * Same for direct calls.
  1797. */
  1798. if (flag) {
  1799. if (!(rec->flags & FTRACE_FL_REGS) !=
  1800. !(rec->flags & FTRACE_FL_REGS_EN))
  1801. flag |= FTRACE_FL_REGS;
  1802. if (!(rec->flags & FTRACE_FL_TRAMP) !=
  1803. !(rec->flags & FTRACE_FL_TRAMP_EN))
  1804. flag |= FTRACE_FL_TRAMP;
  1805. /*
  1806. * Direct calls are special, as count matters.
  1807. * We must test the record for direct, if the
  1808. * DIRECT and DIRECT_EN do not match, but only
  1809. * if the count is 1. That's because, if the
  1810. * count is something other than one, we do not
  1811. * want the direct enabled (it will be done via the
  1812. * direct helper). But if DIRECT_EN is set, and
  1813. * the count is not one, we need to clear it.
  1814. */
  1815. if (ftrace_rec_count(rec) == 1) {
  1816. if (!(rec->flags & FTRACE_FL_DIRECT) !=
  1817. !(rec->flags & FTRACE_FL_DIRECT_EN))
  1818. flag |= FTRACE_FL_DIRECT;
  1819. } else if (rec->flags & FTRACE_FL_DIRECT_EN) {
  1820. flag |= FTRACE_FL_DIRECT;
  1821. }
  1822. }
  1823. /* If the state of this record hasn't changed, then do nothing */
  1824. if ((rec->flags & FTRACE_FL_ENABLED) == flag)
  1825. return FTRACE_UPDATE_IGNORE;
  1826. if (flag) {
  1827. /* Save off if rec is being enabled (for return value) */
  1828. flag ^= rec->flags & FTRACE_FL_ENABLED;
  1829. if (update) {
  1830. rec->flags |= FTRACE_FL_ENABLED;
  1831. if (flag & FTRACE_FL_REGS) {
  1832. if (rec->flags & FTRACE_FL_REGS)
  1833. rec->flags |= FTRACE_FL_REGS_EN;
  1834. else
  1835. rec->flags &= ~FTRACE_FL_REGS_EN;
  1836. }
  1837. if (flag & FTRACE_FL_TRAMP) {
  1838. if (rec->flags & FTRACE_FL_TRAMP)
  1839. rec->flags |= FTRACE_FL_TRAMP_EN;
  1840. else
  1841. rec->flags &= ~FTRACE_FL_TRAMP_EN;
  1842. }
  1843. if (flag & FTRACE_FL_DIRECT) {
  1844. /*
  1845. * If there's only one user (direct_ops helper)
  1846. * then we can call the direct function
  1847. * directly (no ftrace trampoline).
  1848. */
  1849. if (ftrace_rec_count(rec) == 1) {
  1850. if (rec->flags & FTRACE_FL_DIRECT)
  1851. rec->flags |= FTRACE_FL_DIRECT_EN;
  1852. else
  1853. rec->flags &= ~FTRACE_FL_DIRECT_EN;
  1854. } else {
  1855. /*
  1856. * Can only call directly if there's
  1857. * only one callback to the function.
  1858. */
  1859. rec->flags &= ~FTRACE_FL_DIRECT_EN;
  1860. }
  1861. }
  1862. }
  1863. /*
  1864. * If this record is being updated from a nop, then
  1865. * return UPDATE_MAKE_CALL.
  1866. * Otherwise,
  1867. * return UPDATE_MODIFY_CALL to tell the caller to convert
  1868. * from the save regs, to a non-save regs function or
  1869. * vice versa, or from a trampoline call.
  1870. */
  1871. if (flag & FTRACE_FL_ENABLED) {
  1872. ftrace_bug_type = FTRACE_BUG_CALL;
  1873. return FTRACE_UPDATE_MAKE_CALL;
  1874. }
  1875. ftrace_bug_type = FTRACE_BUG_UPDATE;
  1876. return FTRACE_UPDATE_MODIFY_CALL;
  1877. }
  1878. if (update) {
  1879. /* If there's no more users, clear all flags */
  1880. if (!ftrace_rec_count(rec))
  1881. rec->flags &= FTRACE_FL_DISABLED;
  1882. else
  1883. /*
  1884. * Just disable the record, but keep the ops TRAMP
  1885. * and REGS states. The _EN flags must be disabled though.
  1886. */
  1887. rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
  1888. FTRACE_FL_REGS_EN | FTRACE_FL_DIRECT_EN);
  1889. }
  1890. ftrace_bug_type = FTRACE_BUG_NOP;
  1891. return FTRACE_UPDATE_MAKE_NOP;
  1892. }
  1893. /**
  1894. * ftrace_update_record - set a record that now is tracing or not
  1895. * @rec: the record to update
  1896. * @enable: set to true if the record is tracing, false to force disable
  1897. *
  1898. * The records that represent all functions that can be traced need
  1899. * to be updated when tracing has been enabled.
  1900. */
  1901. int ftrace_update_record(struct dyn_ftrace *rec, bool enable)
  1902. {
  1903. return ftrace_check_record(rec, enable, true);
  1904. }
  1905. /**
  1906. * ftrace_test_record - check if the record has been enabled or not
  1907. * @rec: the record to test
  1908. * @enable: set to true to check if enabled, false if it is disabled
  1909. *
  1910. * The arch code may need to test if a record is already set to
  1911. * tracing to determine how to modify the function code that it
  1912. * represents.
  1913. */
  1914. int ftrace_test_record(struct dyn_ftrace *rec, bool enable)
  1915. {
  1916. return ftrace_check_record(rec, enable, false);
  1917. }
  1918. static struct ftrace_ops *
  1919. ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
  1920. {
  1921. struct ftrace_ops *op;
  1922. unsigned long ip = rec->ip;
  1923. do_for_each_ftrace_op(op, ftrace_ops_list) {
  1924. if (!op->trampoline)
  1925. continue;
  1926. if (hash_contains_ip(ip, op->func_hash))
  1927. return op;
  1928. } while_for_each_ftrace_op(op);
  1929. return NULL;
  1930. }
  1931. static struct ftrace_ops *
  1932. ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude)
  1933. {
  1934. struct ftrace_ops *op;
  1935. unsigned long ip = rec->ip;
  1936. do_for_each_ftrace_op(op, ftrace_ops_list) {
  1937. if (op == op_exclude || !op->trampoline)
  1938. continue;
  1939. if (hash_contains_ip(ip, op->func_hash))
  1940. return op;
  1941. } while_for_each_ftrace_op(op);
  1942. return NULL;
  1943. }
  1944. static struct ftrace_ops *
  1945. ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
  1946. struct ftrace_ops *op)
  1947. {
  1948. unsigned long ip = rec->ip;
  1949. while_for_each_ftrace_op(op) {
  1950. if (!op->trampoline)
  1951. continue;
  1952. if (hash_contains_ip(ip, op->func_hash))
  1953. return op;
  1954. }
  1955. return NULL;
  1956. }
  1957. static struct ftrace_ops *
  1958. ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
  1959. {
  1960. struct ftrace_ops *op;
  1961. unsigned long ip = rec->ip;
  1962. /*
  1963. * Need to check removed ops first.
  1964. * If they are being removed, and this rec has a tramp,
  1965. * and this rec is in the ops list, then it would be the
  1966. * one with the tramp.
  1967. */
  1968. if (removed_ops) {
  1969. if (hash_contains_ip(ip, &removed_ops->old_hash))
  1970. return removed_ops;
  1971. }
  1972. /*
  1973. * Need to find the current trampoline for a rec.
  1974. * Now, a trampoline is only attached to a rec if there
  1975. * was a single 'ops' attached to it. But this can be called
  1976. * when we are adding another op to the rec or removing the
  1977. * current one. Thus, if the op is being added, we can
  1978. * ignore it because it hasn't attached itself to the rec
  1979. * yet.
  1980. *
  1981. * If an ops is being modified (hooking to different functions)
  1982. * then we don't care about the new functions that are being
  1983. * added, just the old ones (that are probably being removed).
  1984. *
  1985. * If we are adding an ops to a function that already is using
  1986. * a trampoline, it needs to be removed (trampolines are only
  1987. * for single ops connected), then an ops that is not being
  1988. * modified also needs to be checked.
  1989. */
  1990. do_for_each_ftrace_op(op, ftrace_ops_list) {
  1991. if (!op->trampoline)
  1992. continue;
  1993. /*
  1994. * If the ops is being added, it hasn't gotten to
  1995. * the point to be removed from this tree yet.
  1996. */
  1997. if (op->flags & FTRACE_OPS_FL_ADDING)
  1998. continue;
  1999. /*
  2000. * If the ops is being modified and is in the old
  2001. * hash, then it is probably being removed from this
  2002. * function.
  2003. */
  2004. if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
  2005. hash_contains_ip(ip, &op->old_hash))
  2006. return op;
  2007. /*
  2008. * If the ops is not being added or modified, and it's
  2009. * in its normal filter hash, then this must be the one
  2010. * we want!
  2011. */
  2012. if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
  2013. hash_contains_ip(ip, op->func_hash))
  2014. return op;
  2015. } while_for_each_ftrace_op(op);
  2016. return NULL;
  2017. }
  2018. static struct ftrace_ops *
  2019. ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
  2020. {
  2021. struct ftrace_ops *op;
  2022. unsigned long ip = rec->ip;
  2023. do_for_each_ftrace_op(op, ftrace_ops_list) {
  2024. /* pass rec in as regs to have non-NULL val */
  2025. if (hash_contains_ip(ip, op->func_hash))
  2026. return op;
  2027. } while_for_each_ftrace_op(op);
  2028. return NULL;
  2029. }
  2030. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
  2031. /* Protected by rcu_tasks for reading, and direct_mutex for writing */
  2032. static struct ftrace_hash *direct_functions = EMPTY_HASH;
  2033. static DEFINE_MUTEX(direct_mutex);
  2034. int ftrace_direct_func_count;
  2035. /*
  2036. * Search the direct_functions hash to see if the given instruction pointer
  2037. * has a direct caller attached to it.
  2038. */
  2039. unsigned long ftrace_find_rec_direct(unsigned long ip)
  2040. {
  2041. struct ftrace_func_entry *entry;
  2042. entry = __ftrace_lookup_ip(direct_functions, ip);
  2043. if (!entry)
  2044. return 0;
  2045. return entry->direct;
  2046. }
  2047. static struct ftrace_func_entry*
  2048. ftrace_add_rec_direct(unsigned long ip, unsigned long addr,
  2049. struct ftrace_hash **free_hash)
  2050. {
  2051. struct ftrace_func_entry *entry;
  2052. if (ftrace_hash_empty(direct_functions) ||
  2053. direct_functions->count > 2 * (1 << direct_functions->size_bits)) {
  2054. struct ftrace_hash *new_hash;
  2055. int size = ftrace_hash_empty(direct_functions) ? 0 :
  2056. direct_functions->count + 1;
  2057. if (size < 32)
  2058. size = 32;
  2059. new_hash = dup_hash(direct_functions, size);
  2060. if (!new_hash)
  2061. return NULL;
  2062. *free_hash = direct_functions;
  2063. direct_functions = new_hash;
  2064. }
  2065. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  2066. if (!entry)
  2067. return NULL;
  2068. entry->ip = ip;
  2069. entry->direct = addr;
  2070. __add_hash_entry(direct_functions, entry);
  2071. return entry;
  2072. }
  2073. static void call_direct_funcs(unsigned long ip, unsigned long pip,
  2074. struct ftrace_ops *ops, struct ftrace_regs *fregs)
  2075. {
  2076. struct pt_regs *regs = ftrace_get_regs(fregs);
  2077. unsigned long addr;
  2078. addr = ftrace_find_rec_direct(ip);
  2079. if (!addr)
  2080. return;
  2081. arch_ftrace_set_direct_caller(regs, addr);
  2082. }
  2083. struct ftrace_ops direct_ops = {
  2084. .func = call_direct_funcs,
  2085. .flags = FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_REGS
  2086. | FTRACE_OPS_FL_PERMANENT,
  2087. /*
  2088. * By declaring the main trampoline as this trampoline
  2089. * it will never have one allocated for it. Allocated
  2090. * trampolines should not call direct functions.
  2091. * The direct_ops should only be called by the builtin
  2092. * ftrace_regs_caller trampoline.
  2093. */
  2094. .trampoline = FTRACE_REGS_ADDR,
  2095. };
  2096. #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
  2097. /**
  2098. * ftrace_get_addr_new - Get the call address to set to
  2099. * @rec: The ftrace record descriptor
  2100. *
  2101. * If the record has the FTRACE_FL_REGS set, that means that it
  2102. * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
  2103. * is not set, then it wants to convert to the normal callback.
  2104. *
  2105. * Returns the address of the trampoline to set to
  2106. */
  2107. unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
  2108. {
  2109. struct ftrace_ops *ops;
  2110. unsigned long addr;
  2111. if ((rec->flags & FTRACE_FL_DIRECT) &&
  2112. (ftrace_rec_count(rec) == 1)) {
  2113. addr = ftrace_find_rec_direct(rec->ip);
  2114. if (addr)
  2115. return addr;
  2116. WARN_ON_ONCE(1);
  2117. }
  2118. /* Trampolines take precedence over regs */
  2119. if (rec->flags & FTRACE_FL_TRAMP) {
  2120. ops = ftrace_find_tramp_ops_new(rec);
  2121. if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
  2122. pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
  2123. (void *)rec->ip, (void *)rec->ip, rec->flags);
  2124. /* Ftrace is shutting down, return anything */
  2125. return (unsigned long)FTRACE_ADDR;
  2126. }
  2127. return ops->trampoline;
  2128. }
  2129. if (rec->flags & FTRACE_FL_REGS)
  2130. return (unsigned long)FTRACE_REGS_ADDR;
  2131. else
  2132. return (unsigned long)FTRACE_ADDR;
  2133. }
  2134. /**
  2135. * ftrace_get_addr_curr - Get the call address that is already there
  2136. * @rec: The ftrace record descriptor
  2137. *
  2138. * The FTRACE_FL_REGS_EN is set when the record already points to
  2139. * a function that saves all the regs. Basically the '_EN' version
  2140. * represents the current state of the function.
  2141. *
  2142. * Returns the address of the trampoline that is currently being called
  2143. */
  2144. unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
  2145. {
  2146. struct ftrace_ops *ops;
  2147. unsigned long addr;
  2148. /* Direct calls take precedence over trampolines */
  2149. if (rec->flags & FTRACE_FL_DIRECT_EN) {
  2150. addr = ftrace_find_rec_direct(rec->ip);
  2151. if (addr)
  2152. return addr;
  2153. WARN_ON_ONCE(1);
  2154. }
  2155. /* Trampolines take precedence over regs */
  2156. if (rec->flags & FTRACE_FL_TRAMP_EN) {
  2157. ops = ftrace_find_tramp_ops_curr(rec);
  2158. if (FTRACE_WARN_ON(!ops)) {
  2159. pr_warn("Bad trampoline accounting at: %p (%pS)\n",
  2160. (void *)rec->ip, (void *)rec->ip);
  2161. /* Ftrace is shutting down, return anything */
  2162. return (unsigned long)FTRACE_ADDR;
  2163. }
  2164. return ops->trampoline;
  2165. }
  2166. if (rec->flags & FTRACE_FL_REGS_EN)
  2167. return (unsigned long)FTRACE_REGS_ADDR;
  2168. else
  2169. return (unsigned long)FTRACE_ADDR;
  2170. }
  2171. static int
  2172. __ftrace_replace_code(struct dyn_ftrace *rec, bool enable)
  2173. {
  2174. unsigned long ftrace_old_addr;
  2175. unsigned long ftrace_addr;
  2176. int ret;
  2177. ftrace_addr = ftrace_get_addr_new(rec);
  2178. /* This needs to be done before we call ftrace_update_record */
  2179. ftrace_old_addr = ftrace_get_addr_curr(rec);
  2180. ret = ftrace_update_record(rec, enable);
  2181. ftrace_bug_type = FTRACE_BUG_UNKNOWN;
  2182. switch (ret) {
  2183. case FTRACE_UPDATE_IGNORE:
  2184. return 0;
  2185. case FTRACE_UPDATE_MAKE_CALL:
  2186. ftrace_bug_type = FTRACE_BUG_CALL;
  2187. return ftrace_make_call(rec, ftrace_addr);
  2188. case FTRACE_UPDATE_MAKE_NOP:
  2189. ftrace_bug_type = FTRACE_BUG_NOP;
  2190. return ftrace_make_nop(NULL, rec, ftrace_old_addr);
  2191. case FTRACE_UPDATE_MODIFY_CALL:
  2192. ftrace_bug_type = FTRACE_BUG_UPDATE;
  2193. return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
  2194. }
  2195. return -1; /* unknown ftrace bug */
  2196. }
  2197. void __weak ftrace_replace_code(int mod_flags)
  2198. {
  2199. struct dyn_ftrace *rec;
  2200. struct ftrace_page *pg;
  2201. bool enable = mod_flags & FTRACE_MODIFY_ENABLE_FL;
  2202. int schedulable = mod_flags & FTRACE_MODIFY_MAY_SLEEP_FL;
  2203. int failed;
  2204. if (unlikely(ftrace_disabled))
  2205. return;
  2206. do_for_each_ftrace_rec(pg, rec) {
  2207. if (skip_record(rec))
  2208. continue;
  2209. failed = __ftrace_replace_code(rec, enable);
  2210. if (failed) {
  2211. ftrace_bug(failed, rec);
  2212. /* Stop processing */
  2213. return;
  2214. }
  2215. if (schedulable)
  2216. cond_resched();
  2217. } while_for_each_ftrace_rec();
  2218. }
  2219. struct ftrace_rec_iter {
  2220. struct ftrace_page *pg;
  2221. int index;
  2222. };
  2223. /**
  2224. * ftrace_rec_iter_start - start up iterating over traced functions
  2225. *
  2226. * Returns an iterator handle that is used to iterate over all
  2227. * the records that represent address locations where functions
  2228. * are traced.
  2229. *
  2230. * May return NULL if no records are available.
  2231. */
  2232. struct ftrace_rec_iter *ftrace_rec_iter_start(void)
  2233. {
  2234. /*
  2235. * We only use a single iterator.
  2236. * Protected by the ftrace_lock mutex.
  2237. */
  2238. static struct ftrace_rec_iter ftrace_rec_iter;
  2239. struct ftrace_rec_iter *iter = &ftrace_rec_iter;
  2240. iter->pg = ftrace_pages_start;
  2241. iter->index = 0;
  2242. /* Could have empty pages */
  2243. while (iter->pg && !iter->pg->index)
  2244. iter->pg = iter->pg->next;
  2245. if (!iter->pg)
  2246. return NULL;
  2247. return iter;
  2248. }
  2249. /**
  2250. * ftrace_rec_iter_next - get the next record to process.
  2251. * @iter: The handle to the iterator.
  2252. *
  2253. * Returns the next iterator after the given iterator @iter.
  2254. */
  2255. struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
  2256. {
  2257. iter->index++;
  2258. if (iter->index >= iter->pg->index) {
  2259. iter->pg = iter->pg->next;
  2260. iter->index = 0;
  2261. /* Could have empty pages */
  2262. while (iter->pg && !iter->pg->index)
  2263. iter->pg = iter->pg->next;
  2264. }
  2265. if (!iter->pg)
  2266. return NULL;
  2267. return iter;
  2268. }
  2269. /**
  2270. * ftrace_rec_iter_record - get the record at the iterator location
  2271. * @iter: The current iterator location
  2272. *
  2273. * Returns the record that the current @iter is at.
  2274. */
  2275. struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
  2276. {
  2277. return &iter->pg->records[iter->index];
  2278. }
  2279. static int
  2280. ftrace_nop_initialize(struct module *mod, struct dyn_ftrace *rec)
  2281. {
  2282. int ret;
  2283. if (unlikely(ftrace_disabled))
  2284. return 0;
  2285. ret = ftrace_init_nop(mod, rec);
  2286. if (ret) {
  2287. ftrace_bug_type = FTRACE_BUG_INIT;
  2288. ftrace_bug(ret, rec);
  2289. return 0;
  2290. }
  2291. return 1;
  2292. }
  2293. /*
  2294. * archs can override this function if they must do something
  2295. * before the modifying code is performed.
  2296. */
  2297. void __weak ftrace_arch_code_modify_prepare(void)
  2298. {
  2299. }
  2300. /*
  2301. * archs can override this function if they must do something
  2302. * after the modifying code is performed.
  2303. */
  2304. void __weak ftrace_arch_code_modify_post_process(void)
  2305. {
  2306. }
  2307. void ftrace_modify_all_code(int command)
  2308. {
  2309. int update = command & FTRACE_UPDATE_TRACE_FUNC;
  2310. int mod_flags = 0;
  2311. int err = 0;
  2312. if (command & FTRACE_MAY_SLEEP)
  2313. mod_flags = FTRACE_MODIFY_MAY_SLEEP_FL;
  2314. /*
  2315. * If the ftrace_caller calls a ftrace_ops func directly,
  2316. * we need to make sure that it only traces functions it
  2317. * expects to trace. When doing the switch of functions,
  2318. * we need to update to the ftrace_ops_list_func first
  2319. * before the transition between old and new calls are set,
  2320. * as the ftrace_ops_list_func will check the ops hashes
  2321. * to make sure the ops are having the right functions
  2322. * traced.
  2323. */
  2324. if (update) {
  2325. err = ftrace_update_ftrace_func(ftrace_ops_list_func);
  2326. if (FTRACE_WARN_ON(err))
  2327. return;
  2328. }
  2329. if (command & FTRACE_UPDATE_CALLS)
  2330. ftrace_replace_code(mod_flags | FTRACE_MODIFY_ENABLE_FL);
  2331. else if (command & FTRACE_DISABLE_CALLS)
  2332. ftrace_replace_code(mod_flags);
  2333. if (update && ftrace_trace_function != ftrace_ops_list_func) {
  2334. function_trace_op = set_function_trace_op;
  2335. smp_wmb();
  2336. /* If irqs are disabled, we are in stop machine */
  2337. if (!irqs_disabled())
  2338. smp_call_function(ftrace_sync_ipi, NULL, 1);
  2339. err = ftrace_update_ftrace_func(ftrace_trace_function);
  2340. if (FTRACE_WARN_ON(err))
  2341. return;
  2342. }
  2343. if (command & FTRACE_START_FUNC_RET)
  2344. err = ftrace_enable_ftrace_graph_caller();
  2345. else if (command & FTRACE_STOP_FUNC_RET)
  2346. err = ftrace_disable_ftrace_graph_caller();
  2347. FTRACE_WARN_ON(err);
  2348. }
  2349. static int __ftrace_modify_code(void *data)
  2350. {
  2351. int *command = data;
  2352. ftrace_modify_all_code(*command);
  2353. return 0;
  2354. }
  2355. /**
  2356. * ftrace_run_stop_machine - go back to the stop machine method
  2357. * @command: The command to tell ftrace what to do
  2358. *
  2359. * If an arch needs to fall back to the stop machine method, the
  2360. * it can call this function.
  2361. */
  2362. void ftrace_run_stop_machine(int command)
  2363. {
  2364. stop_machine(__ftrace_modify_code, &command, NULL);
  2365. }
  2366. /**
  2367. * arch_ftrace_update_code - modify the code to trace or not trace
  2368. * @command: The command that needs to be done
  2369. *
  2370. * Archs can override this function if it does not need to
  2371. * run stop_machine() to modify code.
  2372. */
  2373. void __weak arch_ftrace_update_code(int command)
  2374. {
  2375. ftrace_run_stop_machine(command);
  2376. }
  2377. static void ftrace_run_update_code(int command)
  2378. {
  2379. ftrace_arch_code_modify_prepare();
  2380. /*
  2381. * By default we use stop_machine() to modify the code.
  2382. * But archs can do what ever they want as long as it
  2383. * is safe. The stop_machine() is the safest, but also
  2384. * produces the most overhead.
  2385. */
  2386. arch_ftrace_update_code(command);
  2387. ftrace_arch_code_modify_post_process();
  2388. }
  2389. static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
  2390. struct ftrace_ops_hash *old_hash)
  2391. {
  2392. ops->flags |= FTRACE_OPS_FL_MODIFYING;
  2393. ops->old_hash.filter_hash = old_hash->filter_hash;
  2394. ops->old_hash.notrace_hash = old_hash->notrace_hash;
  2395. ftrace_run_update_code(command);
  2396. ops->old_hash.filter_hash = NULL;
  2397. ops->old_hash.notrace_hash = NULL;
  2398. ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
  2399. }
  2400. static ftrace_func_t saved_ftrace_func;
  2401. static int ftrace_start_up;
  2402. void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
  2403. {
  2404. }
  2405. /* List of trace_ops that have allocated trampolines */
  2406. static LIST_HEAD(ftrace_ops_trampoline_list);
  2407. static void ftrace_add_trampoline_to_kallsyms(struct ftrace_ops *ops)
  2408. {
  2409. lockdep_assert_held(&ftrace_lock);
  2410. list_add_rcu(&ops->list, &ftrace_ops_trampoline_list);
  2411. }
  2412. static void ftrace_remove_trampoline_from_kallsyms(struct ftrace_ops *ops)
  2413. {
  2414. lockdep_assert_held(&ftrace_lock);
  2415. list_del_rcu(&ops->list);
  2416. synchronize_rcu();
  2417. }
  2418. /*
  2419. * "__builtin__ftrace" is used as a module name in /proc/kallsyms for symbols
  2420. * for pages allocated for ftrace purposes, even though "__builtin__ftrace" is
  2421. * not a module.
  2422. */
  2423. #define FTRACE_TRAMPOLINE_MOD "__builtin__ftrace"
  2424. #define FTRACE_TRAMPOLINE_SYM "ftrace_trampoline"
  2425. static void ftrace_trampoline_free(struct ftrace_ops *ops)
  2426. {
  2427. if (ops && (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP) &&
  2428. ops->trampoline) {
  2429. /*
  2430. * Record the text poke event before the ksymbol unregister
  2431. * event.
  2432. */
  2433. perf_event_text_poke((void *)ops->trampoline,
  2434. (void *)ops->trampoline,
  2435. ops->trampoline_size, NULL, 0);
  2436. perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
  2437. ops->trampoline, ops->trampoline_size,
  2438. true, FTRACE_TRAMPOLINE_SYM);
  2439. /* Remove from kallsyms after the perf events */
  2440. ftrace_remove_trampoline_from_kallsyms(ops);
  2441. }
  2442. arch_ftrace_trampoline_free(ops);
  2443. }
  2444. static void ftrace_startup_enable(int command)
  2445. {
  2446. if (saved_ftrace_func != ftrace_trace_function) {
  2447. saved_ftrace_func = ftrace_trace_function;
  2448. command |= FTRACE_UPDATE_TRACE_FUNC;
  2449. }
  2450. if (!command || !ftrace_enabled)
  2451. return;
  2452. ftrace_run_update_code(command);
  2453. }
  2454. static void ftrace_startup_all(int command)
  2455. {
  2456. update_all_ops = true;
  2457. ftrace_startup_enable(command);
  2458. update_all_ops = false;
  2459. }
  2460. int ftrace_startup(struct ftrace_ops *ops, int command)
  2461. {
  2462. int ret;
  2463. if (unlikely(ftrace_disabled))
  2464. return -ENODEV;
  2465. ret = __register_ftrace_function(ops);
  2466. if (ret)
  2467. return ret;
  2468. ftrace_start_up++;
  2469. /*
  2470. * Note that ftrace probes uses this to start up
  2471. * and modify functions it will probe. But we still
  2472. * set the ADDING flag for modification, as probes
  2473. * do not have trampolines. If they add them in the
  2474. * future, then the probes will need to distinguish
  2475. * between adding and updating probes.
  2476. */
  2477. ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
  2478. ret = ftrace_hash_ipmodify_enable(ops);
  2479. if (ret < 0) {
  2480. /* Rollback registration process */
  2481. __unregister_ftrace_function(ops);
  2482. ftrace_start_up--;
  2483. ops->flags &= ~FTRACE_OPS_FL_ENABLED;
  2484. if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
  2485. ftrace_trampoline_free(ops);
  2486. return ret;
  2487. }
  2488. if (ftrace_hash_rec_enable(ops, 1))
  2489. command |= FTRACE_UPDATE_CALLS;
  2490. ftrace_startup_enable(command);
  2491. /*
  2492. * If ftrace is in an undefined state, we just remove ops from list
  2493. * to prevent the NULL pointer, instead of totally rolling it back and
  2494. * free trampoline, because those actions could cause further damage.
  2495. */
  2496. if (unlikely(ftrace_disabled)) {
  2497. __unregister_ftrace_function(ops);
  2498. return -ENODEV;
  2499. }
  2500. ops->flags &= ~FTRACE_OPS_FL_ADDING;
  2501. return 0;
  2502. }
  2503. int ftrace_shutdown(struct ftrace_ops *ops, int command)
  2504. {
  2505. int ret;
  2506. if (unlikely(ftrace_disabled))
  2507. return -ENODEV;
  2508. ret = __unregister_ftrace_function(ops);
  2509. if (ret)
  2510. return ret;
  2511. ftrace_start_up--;
  2512. /*
  2513. * Just warn in case of unbalance, no need to kill ftrace, it's not
  2514. * critical but the ftrace_call callers may be never nopped again after
  2515. * further ftrace uses.
  2516. */
  2517. WARN_ON_ONCE(ftrace_start_up < 0);
  2518. /* Disabling ipmodify never fails */
  2519. ftrace_hash_ipmodify_disable(ops);
  2520. if (ftrace_hash_rec_disable(ops, 1))
  2521. command |= FTRACE_UPDATE_CALLS;
  2522. ops->flags &= ~FTRACE_OPS_FL_ENABLED;
  2523. if (saved_ftrace_func != ftrace_trace_function) {
  2524. saved_ftrace_func = ftrace_trace_function;
  2525. command |= FTRACE_UPDATE_TRACE_FUNC;
  2526. }
  2527. if (!command || !ftrace_enabled)
  2528. goto out;
  2529. /*
  2530. * If the ops uses a trampoline, then it needs to be
  2531. * tested first on update.
  2532. */
  2533. ops->flags |= FTRACE_OPS_FL_REMOVING;
  2534. removed_ops = ops;
  2535. /* The trampoline logic checks the old hashes */
  2536. ops->old_hash.filter_hash = ops->func_hash->filter_hash;
  2537. ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
  2538. ftrace_run_update_code(command);
  2539. /*
  2540. * If there's no more ops registered with ftrace, run a
  2541. * sanity check to make sure all rec flags are cleared.
  2542. */
  2543. if (rcu_dereference_protected(ftrace_ops_list,
  2544. lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
  2545. struct ftrace_page *pg;
  2546. struct dyn_ftrace *rec;
  2547. do_for_each_ftrace_rec(pg, rec) {
  2548. if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
  2549. pr_warn(" %pS flags:%lx\n",
  2550. (void *)rec->ip, rec->flags);
  2551. } while_for_each_ftrace_rec();
  2552. }
  2553. ops->old_hash.filter_hash = NULL;
  2554. ops->old_hash.notrace_hash = NULL;
  2555. removed_ops = NULL;
  2556. ops->flags &= ~FTRACE_OPS_FL_REMOVING;
  2557. out:
  2558. /*
  2559. * Dynamic ops may be freed, we must make sure that all
  2560. * callers are done before leaving this function.
  2561. * The same goes for freeing the per_cpu data of the per_cpu
  2562. * ops.
  2563. */
  2564. if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
  2565. /*
  2566. * We need to do a hard force of sched synchronization.
  2567. * This is because we use preempt_disable() to do RCU, but
  2568. * the function tracers can be called where RCU is not watching
  2569. * (like before user_exit()). We can not rely on the RCU
  2570. * infrastructure to do the synchronization, thus we must do it
  2571. * ourselves.
  2572. */
  2573. synchronize_rcu_tasks_rude();
  2574. /*
  2575. * When the kernel is preemptive, tasks can be preempted
  2576. * while on a ftrace trampoline. Just scheduling a task on
  2577. * a CPU is not good enough to flush them. Calling
  2578. * synchronize_rcu_tasks() will wait for those tasks to
  2579. * execute and either schedule voluntarily or enter user space.
  2580. */
  2581. if (IS_ENABLED(CONFIG_PREEMPTION))
  2582. synchronize_rcu_tasks();
  2583. ftrace_trampoline_free(ops);
  2584. }
  2585. return 0;
  2586. }
  2587. static u64 ftrace_update_time;
  2588. unsigned long ftrace_update_tot_cnt;
  2589. unsigned long ftrace_number_of_pages;
  2590. unsigned long ftrace_number_of_groups;
  2591. static inline int ops_traces_mod(struct ftrace_ops *ops)
  2592. {
  2593. /*
  2594. * Filter_hash being empty will default to trace module.
  2595. * But notrace hash requires a test of individual module functions.
  2596. */
  2597. return ftrace_hash_empty(ops->func_hash->filter_hash) &&
  2598. ftrace_hash_empty(ops->func_hash->notrace_hash);
  2599. }
  2600. static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
  2601. {
  2602. bool init_nop = ftrace_need_init_nop();
  2603. struct ftrace_page *pg;
  2604. struct dyn_ftrace *p;
  2605. u64 start, stop;
  2606. unsigned long update_cnt = 0;
  2607. unsigned long rec_flags = 0;
  2608. int i;
  2609. start = ftrace_now(raw_smp_processor_id());
  2610. /*
  2611. * When a module is loaded, this function is called to convert
  2612. * the calls to mcount in its text to nops, and also to create
  2613. * an entry in the ftrace data. Now, if ftrace is activated
  2614. * after this call, but before the module sets its text to
  2615. * read-only, the modification of enabling ftrace can fail if
  2616. * the read-only is done while ftrace is converting the calls.
  2617. * To prevent this, the module's records are set as disabled
  2618. * and will be enabled after the call to set the module's text
  2619. * to read-only.
  2620. */
  2621. if (mod)
  2622. rec_flags |= FTRACE_FL_DISABLED;
  2623. for (pg = new_pgs; pg; pg = pg->next) {
  2624. for (i = 0; i < pg->index; i++) {
  2625. /* If something went wrong, bail without enabling anything */
  2626. if (unlikely(ftrace_disabled))
  2627. return -1;
  2628. p = &pg->records[i];
  2629. p->flags = rec_flags;
  2630. /*
  2631. * Do the initial record conversion from mcount jump
  2632. * to the NOP instructions.
  2633. */
  2634. if (init_nop && !ftrace_nop_initialize(mod, p))
  2635. break;
  2636. update_cnt++;
  2637. }
  2638. }
  2639. stop = ftrace_now(raw_smp_processor_id());
  2640. ftrace_update_time = stop - start;
  2641. ftrace_update_tot_cnt += update_cnt;
  2642. return 0;
  2643. }
  2644. static int ftrace_allocate_records(struct ftrace_page *pg, int count)
  2645. {
  2646. int order;
  2647. int pages;
  2648. int cnt;
  2649. if (WARN_ON(!count))
  2650. return -EINVAL;
  2651. /* We want to fill as much as possible, with no empty pages */
  2652. pages = DIV_ROUND_UP(count, ENTRIES_PER_PAGE);
  2653. order = fls(pages) - 1;
  2654. again:
  2655. pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
  2656. if (!pg->records) {
  2657. /* if we can't allocate this size, try something smaller */
  2658. if (!order)
  2659. return -ENOMEM;
  2660. order--;
  2661. goto again;
  2662. }
  2663. ftrace_number_of_pages += 1 << order;
  2664. ftrace_number_of_groups++;
  2665. cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
  2666. pg->order = order;
  2667. if (cnt > count)
  2668. cnt = count;
  2669. return cnt;
  2670. }
  2671. static void ftrace_free_pages(struct ftrace_page *pages)
  2672. {
  2673. struct ftrace_page *pg = pages;
  2674. while (pg) {
  2675. if (pg->records) {
  2676. free_pages((unsigned long)pg->records, pg->order);
  2677. ftrace_number_of_pages -= 1 << pg->order;
  2678. }
  2679. pages = pg->next;
  2680. kfree(pg);
  2681. pg = pages;
  2682. ftrace_number_of_groups--;
  2683. }
  2684. }
  2685. static struct ftrace_page *
  2686. ftrace_allocate_pages(unsigned long num_to_init)
  2687. {
  2688. struct ftrace_page *start_pg;
  2689. struct ftrace_page *pg;
  2690. int cnt;
  2691. if (!num_to_init)
  2692. return NULL;
  2693. start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
  2694. if (!pg)
  2695. return NULL;
  2696. /*
  2697. * Try to allocate as much as possible in one continues
  2698. * location that fills in all of the space. We want to
  2699. * waste as little space as possible.
  2700. */
  2701. for (;;) {
  2702. cnt = ftrace_allocate_records(pg, num_to_init);
  2703. if (cnt < 0)
  2704. goto free_pages;
  2705. num_to_init -= cnt;
  2706. if (!num_to_init)
  2707. break;
  2708. pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
  2709. if (!pg->next)
  2710. goto free_pages;
  2711. pg = pg->next;
  2712. }
  2713. return start_pg;
  2714. free_pages:
  2715. ftrace_free_pages(start_pg);
  2716. pr_info("ftrace: FAILED to allocate memory for functions\n");
  2717. return NULL;
  2718. }
  2719. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  2720. struct ftrace_iterator {
  2721. loff_t pos;
  2722. loff_t func_pos;
  2723. loff_t mod_pos;
  2724. struct ftrace_page *pg;
  2725. struct dyn_ftrace *func;
  2726. struct ftrace_func_probe *probe;
  2727. struct ftrace_func_entry *probe_entry;
  2728. struct trace_parser parser;
  2729. struct ftrace_hash *hash;
  2730. struct ftrace_ops *ops;
  2731. struct trace_array *tr;
  2732. struct list_head *mod_list;
  2733. int pidx;
  2734. int idx;
  2735. unsigned flags;
  2736. };
  2737. static void *
  2738. t_probe_next(struct seq_file *m, loff_t *pos)
  2739. {
  2740. struct ftrace_iterator *iter = m->private;
  2741. struct trace_array *tr = iter->ops->private;
  2742. struct list_head *func_probes;
  2743. struct ftrace_hash *hash;
  2744. struct list_head *next;
  2745. struct hlist_node *hnd = NULL;
  2746. struct hlist_head *hhd;
  2747. int size;
  2748. (*pos)++;
  2749. iter->pos = *pos;
  2750. if (!tr)
  2751. return NULL;
  2752. func_probes = &tr->func_probes;
  2753. if (list_empty(func_probes))
  2754. return NULL;
  2755. if (!iter->probe) {
  2756. next = func_probes->next;
  2757. iter->probe = list_entry(next, struct ftrace_func_probe, list);
  2758. }
  2759. if (iter->probe_entry)
  2760. hnd = &iter->probe_entry->hlist;
  2761. hash = iter->probe->ops.func_hash->filter_hash;
  2762. /*
  2763. * A probe being registered may temporarily have an empty hash
  2764. * and it's at the end of the func_probes list.
  2765. */
  2766. if (!hash || hash == EMPTY_HASH)
  2767. return NULL;
  2768. size = 1 << hash->size_bits;
  2769. retry:
  2770. if (iter->pidx >= size) {
  2771. if (iter->probe->list.next == func_probes)
  2772. return NULL;
  2773. next = iter->probe->list.next;
  2774. iter->probe = list_entry(next, struct ftrace_func_probe, list);
  2775. hash = iter->probe->ops.func_hash->filter_hash;
  2776. size = 1 << hash->size_bits;
  2777. iter->pidx = 0;
  2778. }
  2779. hhd = &hash->buckets[iter->pidx];
  2780. if (hlist_empty(hhd)) {
  2781. iter->pidx++;
  2782. hnd = NULL;
  2783. goto retry;
  2784. }
  2785. if (!hnd)
  2786. hnd = hhd->first;
  2787. else {
  2788. hnd = hnd->next;
  2789. if (!hnd) {
  2790. iter->pidx++;
  2791. goto retry;
  2792. }
  2793. }
  2794. if (WARN_ON_ONCE(!hnd))
  2795. return NULL;
  2796. iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
  2797. return iter;
  2798. }
  2799. static void *t_probe_start(struct seq_file *m, loff_t *pos)
  2800. {
  2801. struct ftrace_iterator *iter = m->private;
  2802. void *p = NULL;
  2803. loff_t l;
  2804. if (!(iter->flags & FTRACE_ITER_DO_PROBES))
  2805. return NULL;
  2806. if (iter->mod_pos > *pos)
  2807. return NULL;
  2808. iter->probe = NULL;
  2809. iter->probe_entry = NULL;
  2810. iter->pidx = 0;
  2811. for (l = 0; l <= (*pos - iter->mod_pos); ) {
  2812. p = t_probe_next(m, &l);
  2813. if (!p)
  2814. break;
  2815. }
  2816. if (!p)
  2817. return NULL;
  2818. /* Only set this if we have an item */
  2819. iter->flags |= FTRACE_ITER_PROBE;
  2820. return iter;
  2821. }
  2822. static int
  2823. t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
  2824. {
  2825. struct ftrace_func_entry *probe_entry;
  2826. struct ftrace_probe_ops *probe_ops;
  2827. struct ftrace_func_probe *probe;
  2828. probe = iter->probe;
  2829. probe_entry = iter->probe_entry;
  2830. if (WARN_ON_ONCE(!probe || !probe_entry))
  2831. return -EIO;
  2832. probe_ops = probe->probe_ops;
  2833. if (probe_ops->print)
  2834. return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
  2835. seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
  2836. (void *)probe_ops->func);
  2837. return 0;
  2838. }
  2839. static void *
  2840. t_mod_next(struct seq_file *m, loff_t *pos)
  2841. {
  2842. struct ftrace_iterator *iter = m->private;
  2843. struct trace_array *tr = iter->tr;
  2844. (*pos)++;
  2845. iter->pos = *pos;
  2846. iter->mod_list = iter->mod_list->next;
  2847. if (iter->mod_list == &tr->mod_trace ||
  2848. iter->mod_list == &tr->mod_notrace) {
  2849. iter->flags &= ~FTRACE_ITER_MOD;
  2850. return NULL;
  2851. }
  2852. iter->mod_pos = *pos;
  2853. return iter;
  2854. }
  2855. static void *t_mod_start(struct seq_file *m, loff_t *pos)
  2856. {
  2857. struct ftrace_iterator *iter = m->private;
  2858. void *p = NULL;
  2859. loff_t l;
  2860. if (iter->func_pos > *pos)
  2861. return NULL;
  2862. iter->mod_pos = iter->func_pos;
  2863. /* probes are only available if tr is set */
  2864. if (!iter->tr)
  2865. return NULL;
  2866. for (l = 0; l <= (*pos - iter->func_pos); ) {
  2867. p = t_mod_next(m, &l);
  2868. if (!p)
  2869. break;
  2870. }
  2871. if (!p) {
  2872. iter->flags &= ~FTRACE_ITER_MOD;
  2873. return t_probe_start(m, pos);
  2874. }
  2875. /* Only set this if we have an item */
  2876. iter->flags |= FTRACE_ITER_MOD;
  2877. return iter;
  2878. }
  2879. static int
  2880. t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
  2881. {
  2882. struct ftrace_mod_load *ftrace_mod;
  2883. struct trace_array *tr = iter->tr;
  2884. if (WARN_ON_ONCE(!iter->mod_list) ||
  2885. iter->mod_list == &tr->mod_trace ||
  2886. iter->mod_list == &tr->mod_notrace)
  2887. return -EIO;
  2888. ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
  2889. if (ftrace_mod->func)
  2890. seq_printf(m, "%s", ftrace_mod->func);
  2891. else
  2892. seq_putc(m, '*');
  2893. seq_printf(m, ":mod:%s\n", ftrace_mod->module);
  2894. return 0;
  2895. }
  2896. static void *
  2897. t_func_next(struct seq_file *m, loff_t *pos)
  2898. {
  2899. struct ftrace_iterator *iter = m->private;
  2900. struct dyn_ftrace *rec = NULL;
  2901. (*pos)++;
  2902. retry:
  2903. if (iter->idx >= iter->pg->index) {
  2904. if (iter->pg->next) {
  2905. iter->pg = iter->pg->next;
  2906. iter->idx = 0;
  2907. goto retry;
  2908. }
  2909. } else {
  2910. rec = &iter->pg->records[iter->idx++];
  2911. if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
  2912. !ftrace_lookup_ip(iter->hash, rec->ip)) ||
  2913. ((iter->flags & FTRACE_ITER_ENABLED) &&
  2914. !(rec->flags & FTRACE_FL_ENABLED))) {
  2915. rec = NULL;
  2916. goto retry;
  2917. }
  2918. }
  2919. if (!rec)
  2920. return NULL;
  2921. iter->pos = iter->func_pos = *pos;
  2922. iter->func = rec;
  2923. return iter;
  2924. }
  2925. static void *
  2926. t_next(struct seq_file *m, void *v, loff_t *pos)
  2927. {
  2928. struct ftrace_iterator *iter = m->private;
  2929. loff_t l = *pos; /* t_probe_start() must use original pos */
  2930. void *ret;
  2931. if (unlikely(ftrace_disabled))
  2932. return NULL;
  2933. if (iter->flags & FTRACE_ITER_PROBE)
  2934. return t_probe_next(m, pos);
  2935. if (iter->flags & FTRACE_ITER_MOD)
  2936. return t_mod_next(m, pos);
  2937. if (iter->flags & FTRACE_ITER_PRINTALL) {
  2938. /* next must increment pos, and t_probe_start does not */
  2939. (*pos)++;
  2940. return t_mod_start(m, &l);
  2941. }
  2942. ret = t_func_next(m, pos);
  2943. if (!ret)
  2944. return t_mod_start(m, &l);
  2945. return ret;
  2946. }
  2947. static void reset_iter_read(struct ftrace_iterator *iter)
  2948. {
  2949. iter->pos = 0;
  2950. iter->func_pos = 0;
  2951. iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
  2952. }
  2953. static void *t_start(struct seq_file *m, loff_t *pos)
  2954. {
  2955. struct ftrace_iterator *iter = m->private;
  2956. void *p = NULL;
  2957. loff_t l;
  2958. mutex_lock(&ftrace_lock);
  2959. if (unlikely(ftrace_disabled))
  2960. return NULL;
  2961. /*
  2962. * If an lseek was done, then reset and start from beginning.
  2963. */
  2964. if (*pos < iter->pos)
  2965. reset_iter_read(iter);
  2966. /*
  2967. * For set_ftrace_filter reading, if we have the filter
  2968. * off, we can short cut and just print out that all
  2969. * functions are enabled.
  2970. */
  2971. if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
  2972. ftrace_hash_empty(iter->hash)) {
  2973. iter->func_pos = 1; /* Account for the message */
  2974. if (*pos > 0)
  2975. return t_mod_start(m, pos);
  2976. iter->flags |= FTRACE_ITER_PRINTALL;
  2977. /* reset in case of seek/pread */
  2978. iter->flags &= ~FTRACE_ITER_PROBE;
  2979. return iter;
  2980. }
  2981. if (iter->flags & FTRACE_ITER_MOD)
  2982. return t_mod_start(m, pos);
  2983. /*
  2984. * Unfortunately, we need to restart at ftrace_pages_start
  2985. * every time we let go of the ftrace_mutex. This is because
  2986. * those pointers can change without the lock.
  2987. */
  2988. iter->pg = ftrace_pages_start;
  2989. iter->idx = 0;
  2990. for (l = 0; l <= *pos; ) {
  2991. p = t_func_next(m, &l);
  2992. if (!p)
  2993. break;
  2994. }
  2995. if (!p)
  2996. return t_mod_start(m, pos);
  2997. return iter;
  2998. }
  2999. static void t_stop(struct seq_file *m, void *p)
  3000. {
  3001. mutex_unlock(&ftrace_lock);
  3002. }
  3003. void * __weak
  3004. arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
  3005. {
  3006. return NULL;
  3007. }
  3008. static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
  3009. struct dyn_ftrace *rec)
  3010. {
  3011. void *ptr;
  3012. ptr = arch_ftrace_trampoline_func(ops, rec);
  3013. if (ptr)
  3014. seq_printf(m, " ->%pS", ptr);
  3015. }
  3016. #ifdef FTRACE_MCOUNT_MAX_OFFSET
  3017. /*
  3018. * Weak functions can still have an mcount/fentry that is saved in
  3019. * the __mcount_loc section. These can be detected by having a
  3020. * symbol offset of greater than FTRACE_MCOUNT_MAX_OFFSET, as the
  3021. * symbol found by kallsyms is not the function that the mcount/fentry
  3022. * is part of. The offset is much greater in these cases.
  3023. *
  3024. * Test the record to make sure that the ip points to a valid kallsyms
  3025. * and if not, mark it disabled.
  3026. */
  3027. static int test_for_valid_rec(struct dyn_ftrace *rec)
  3028. {
  3029. char str[KSYM_SYMBOL_LEN];
  3030. unsigned long offset;
  3031. const char *ret;
  3032. ret = kallsyms_lookup(rec->ip, NULL, &offset, NULL, str);
  3033. /* Weak functions can cause invalid addresses */
  3034. if (!ret || offset > FTRACE_MCOUNT_MAX_OFFSET) {
  3035. rec->flags |= FTRACE_FL_DISABLED;
  3036. return 0;
  3037. }
  3038. return 1;
  3039. }
  3040. static struct workqueue_struct *ftrace_check_wq __initdata;
  3041. static struct work_struct ftrace_check_work __initdata;
  3042. /*
  3043. * Scan all the mcount/fentry entries to make sure they are valid.
  3044. */
  3045. static __init void ftrace_check_work_func(struct work_struct *work)
  3046. {
  3047. struct ftrace_page *pg;
  3048. struct dyn_ftrace *rec;
  3049. mutex_lock(&ftrace_lock);
  3050. do_for_each_ftrace_rec(pg, rec) {
  3051. test_for_valid_rec(rec);
  3052. } while_for_each_ftrace_rec();
  3053. mutex_unlock(&ftrace_lock);
  3054. }
  3055. static int __init ftrace_check_for_weak_functions(void)
  3056. {
  3057. INIT_WORK(&ftrace_check_work, ftrace_check_work_func);
  3058. ftrace_check_wq = alloc_workqueue("ftrace_check_wq", WQ_UNBOUND, 0);
  3059. queue_work(ftrace_check_wq, &ftrace_check_work);
  3060. return 0;
  3061. }
  3062. static int __init ftrace_check_sync(void)
  3063. {
  3064. /* Make sure the ftrace_check updates are finished */
  3065. if (ftrace_check_wq)
  3066. destroy_workqueue(ftrace_check_wq);
  3067. return 0;
  3068. }
  3069. late_initcall_sync(ftrace_check_sync);
  3070. subsys_initcall(ftrace_check_for_weak_functions);
  3071. static int print_rec(struct seq_file *m, unsigned long ip)
  3072. {
  3073. unsigned long offset;
  3074. char str[KSYM_SYMBOL_LEN];
  3075. char *modname;
  3076. const char *ret;
  3077. ret = kallsyms_lookup(ip, NULL, &offset, &modname, str);
  3078. /* Weak functions can cause invalid addresses */
  3079. if (!ret || offset > FTRACE_MCOUNT_MAX_OFFSET) {
  3080. snprintf(str, KSYM_SYMBOL_LEN, "%s_%ld",
  3081. FTRACE_INVALID_FUNCTION, offset);
  3082. ret = NULL;
  3083. }
  3084. seq_puts(m, str);
  3085. if (modname)
  3086. seq_printf(m, " [%s]", modname);
  3087. return ret == NULL ? -1 : 0;
  3088. }
  3089. #else
  3090. static inline int test_for_valid_rec(struct dyn_ftrace *rec)
  3091. {
  3092. return 1;
  3093. }
  3094. static inline int print_rec(struct seq_file *m, unsigned long ip)
  3095. {
  3096. seq_printf(m, "%ps", (void *)ip);
  3097. return 0;
  3098. }
  3099. #endif
  3100. static int t_show(struct seq_file *m, void *v)
  3101. {
  3102. struct ftrace_iterator *iter = m->private;
  3103. struct dyn_ftrace *rec;
  3104. if (iter->flags & FTRACE_ITER_PROBE)
  3105. return t_probe_show(m, iter);
  3106. if (iter->flags & FTRACE_ITER_MOD)
  3107. return t_mod_show(m, iter);
  3108. if (iter->flags & FTRACE_ITER_PRINTALL) {
  3109. if (iter->flags & FTRACE_ITER_NOTRACE)
  3110. seq_puts(m, "#### no functions disabled ####\n");
  3111. else
  3112. seq_puts(m, "#### all functions enabled ####\n");
  3113. return 0;
  3114. }
  3115. rec = iter->func;
  3116. if (!rec)
  3117. return 0;
  3118. if (print_rec(m, rec->ip)) {
  3119. /* This should only happen when a rec is disabled */
  3120. WARN_ON_ONCE(!(rec->flags & FTRACE_FL_DISABLED));
  3121. seq_putc(m, '\n');
  3122. return 0;
  3123. }
  3124. if (iter->flags & FTRACE_ITER_ENABLED) {
  3125. struct ftrace_ops *ops;
  3126. seq_printf(m, " (%ld)%s%s%s",
  3127. ftrace_rec_count(rec),
  3128. rec->flags & FTRACE_FL_REGS ? " R" : " ",
  3129. rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ",
  3130. rec->flags & FTRACE_FL_DIRECT ? " D" : " ");
  3131. if (rec->flags & FTRACE_FL_TRAMP_EN) {
  3132. ops = ftrace_find_tramp_ops_any(rec);
  3133. if (ops) {
  3134. do {
  3135. seq_printf(m, "\ttramp: %pS (%pS)",
  3136. (void *)ops->trampoline,
  3137. (void *)ops->func);
  3138. add_trampoline_func(m, ops, rec);
  3139. ops = ftrace_find_tramp_ops_next(rec, ops);
  3140. } while (ops);
  3141. } else
  3142. seq_puts(m, "\ttramp: ERROR!");
  3143. } else {
  3144. add_trampoline_func(m, NULL, rec);
  3145. }
  3146. if (rec->flags & FTRACE_FL_DIRECT) {
  3147. unsigned long direct;
  3148. direct = ftrace_find_rec_direct(rec->ip);
  3149. if (direct)
  3150. seq_printf(m, "\n\tdirect-->%pS", (void *)direct);
  3151. }
  3152. }
  3153. seq_putc(m, '\n');
  3154. return 0;
  3155. }
  3156. static const struct seq_operations show_ftrace_seq_ops = {
  3157. .start = t_start,
  3158. .next = t_next,
  3159. .stop = t_stop,
  3160. .show = t_show,
  3161. };
  3162. static int
  3163. ftrace_avail_open(struct inode *inode, struct file *file)
  3164. {
  3165. struct ftrace_iterator *iter;
  3166. int ret;
  3167. ret = security_locked_down(LOCKDOWN_TRACEFS);
  3168. if (ret)
  3169. return ret;
  3170. if (unlikely(ftrace_disabled))
  3171. return -ENODEV;
  3172. iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
  3173. if (!iter)
  3174. return -ENOMEM;
  3175. iter->pg = ftrace_pages_start;
  3176. iter->ops = &global_ops;
  3177. return 0;
  3178. }
  3179. static int
  3180. ftrace_enabled_open(struct inode *inode, struct file *file)
  3181. {
  3182. struct ftrace_iterator *iter;
  3183. /*
  3184. * This shows us what functions are currently being
  3185. * traced and by what. Not sure if we want lockdown
  3186. * to hide such critical information for an admin.
  3187. * Although, perhaps it can show information we don't
  3188. * want people to see, but if something is tracing
  3189. * something, we probably want to know about it.
  3190. */
  3191. iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
  3192. if (!iter)
  3193. return -ENOMEM;
  3194. iter->pg = ftrace_pages_start;
  3195. iter->flags = FTRACE_ITER_ENABLED;
  3196. iter->ops = &global_ops;
  3197. return 0;
  3198. }
  3199. /**
  3200. * ftrace_regex_open - initialize function tracer filter files
  3201. * @ops: The ftrace_ops that hold the hash filters
  3202. * @flag: The type of filter to process
  3203. * @inode: The inode, usually passed in to your open routine
  3204. * @file: The file, usually passed in to your open routine
  3205. *
  3206. * ftrace_regex_open() initializes the filter files for the
  3207. * @ops. Depending on @flag it may process the filter hash or
  3208. * the notrace hash of @ops. With this called from the open
  3209. * routine, you can use ftrace_filter_write() for the write
  3210. * routine if @flag has FTRACE_ITER_FILTER set, or
  3211. * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
  3212. * tracing_lseek() should be used as the lseek routine, and
  3213. * release must call ftrace_regex_release().
  3214. */
  3215. int
  3216. ftrace_regex_open(struct ftrace_ops *ops, int flag,
  3217. struct inode *inode, struct file *file)
  3218. {
  3219. struct ftrace_iterator *iter;
  3220. struct ftrace_hash *hash;
  3221. struct list_head *mod_head;
  3222. struct trace_array *tr = ops->private;
  3223. int ret = -ENOMEM;
  3224. ftrace_ops_init(ops);
  3225. if (unlikely(ftrace_disabled))
  3226. return -ENODEV;
  3227. if (tracing_check_open_get_tr(tr))
  3228. return -ENODEV;
  3229. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  3230. if (!iter)
  3231. goto out;
  3232. if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX))
  3233. goto out;
  3234. iter->ops = ops;
  3235. iter->flags = flag;
  3236. iter->tr = tr;
  3237. mutex_lock(&ops->func_hash->regex_lock);
  3238. if (flag & FTRACE_ITER_NOTRACE) {
  3239. hash = ops->func_hash->notrace_hash;
  3240. mod_head = tr ? &tr->mod_notrace : NULL;
  3241. } else {
  3242. hash = ops->func_hash->filter_hash;
  3243. mod_head = tr ? &tr->mod_trace : NULL;
  3244. }
  3245. iter->mod_list = mod_head;
  3246. if (file->f_mode & FMODE_WRITE) {
  3247. const int size_bits = FTRACE_HASH_DEFAULT_BITS;
  3248. if (file->f_flags & O_TRUNC) {
  3249. iter->hash = alloc_ftrace_hash(size_bits);
  3250. clear_ftrace_mod_list(mod_head);
  3251. } else {
  3252. iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
  3253. }
  3254. if (!iter->hash) {
  3255. trace_parser_put(&iter->parser);
  3256. goto out_unlock;
  3257. }
  3258. } else
  3259. iter->hash = hash;
  3260. ret = 0;
  3261. if (file->f_mode & FMODE_READ) {
  3262. iter->pg = ftrace_pages_start;
  3263. ret = seq_open(file, &show_ftrace_seq_ops);
  3264. if (!ret) {
  3265. struct seq_file *m = file->private_data;
  3266. m->private = iter;
  3267. } else {
  3268. /* Failed */
  3269. free_ftrace_hash(iter->hash);
  3270. trace_parser_put(&iter->parser);
  3271. }
  3272. } else
  3273. file->private_data = iter;
  3274. out_unlock:
  3275. mutex_unlock(&ops->func_hash->regex_lock);
  3276. out:
  3277. if (ret) {
  3278. kfree(iter);
  3279. if (tr)
  3280. trace_array_put(tr);
  3281. }
  3282. return ret;
  3283. }
  3284. static int
  3285. ftrace_filter_open(struct inode *inode, struct file *file)
  3286. {
  3287. struct ftrace_ops *ops = inode->i_private;
  3288. /* Checks for tracefs lockdown */
  3289. return ftrace_regex_open(ops,
  3290. FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
  3291. inode, file);
  3292. }
  3293. static int
  3294. ftrace_notrace_open(struct inode *inode, struct file *file)
  3295. {
  3296. struct ftrace_ops *ops = inode->i_private;
  3297. /* Checks for tracefs lockdown */
  3298. return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
  3299. inode, file);
  3300. }
  3301. /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
  3302. struct ftrace_glob {
  3303. char *search;
  3304. unsigned len;
  3305. int type;
  3306. };
  3307. /*
  3308. * If symbols in an architecture don't correspond exactly to the user-visible
  3309. * name of what they represent, it is possible to define this function to
  3310. * perform the necessary adjustments.
  3311. */
  3312. char * __weak arch_ftrace_match_adjust(char *str, const char *search)
  3313. {
  3314. return str;
  3315. }
  3316. static int ftrace_match(char *str, struct ftrace_glob *g)
  3317. {
  3318. int matched = 0;
  3319. int slen;
  3320. str = arch_ftrace_match_adjust(str, g->search);
  3321. switch (g->type) {
  3322. case MATCH_FULL:
  3323. if (strcmp(str, g->search) == 0)
  3324. matched = 1;
  3325. break;
  3326. case MATCH_FRONT_ONLY:
  3327. if (strncmp(str, g->search, g->len) == 0)
  3328. matched = 1;
  3329. break;
  3330. case MATCH_MIDDLE_ONLY:
  3331. if (strstr(str, g->search))
  3332. matched = 1;
  3333. break;
  3334. case MATCH_END_ONLY:
  3335. slen = strlen(str);
  3336. if (slen >= g->len &&
  3337. memcmp(str + slen - g->len, g->search, g->len) == 0)
  3338. matched = 1;
  3339. break;
  3340. case MATCH_GLOB:
  3341. if (glob_match(g->search, str))
  3342. matched = 1;
  3343. break;
  3344. }
  3345. return matched;
  3346. }
  3347. static int
  3348. enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
  3349. {
  3350. struct ftrace_func_entry *entry;
  3351. int ret = 0;
  3352. entry = ftrace_lookup_ip(hash, rec->ip);
  3353. if (clear_filter) {
  3354. /* Do nothing if it doesn't exist */
  3355. if (!entry)
  3356. return 0;
  3357. free_hash_entry(hash, entry);
  3358. } else {
  3359. /* Do nothing if it exists */
  3360. if (entry)
  3361. return 0;
  3362. ret = add_hash_entry(hash, rec->ip);
  3363. }
  3364. return ret;
  3365. }
  3366. static int
  3367. add_rec_by_index(struct ftrace_hash *hash, struct ftrace_glob *func_g,
  3368. int clear_filter)
  3369. {
  3370. long index = simple_strtoul(func_g->search, NULL, 0);
  3371. struct ftrace_page *pg;
  3372. struct dyn_ftrace *rec;
  3373. /* The index starts at 1 */
  3374. if (--index < 0)
  3375. return 0;
  3376. do_for_each_ftrace_rec(pg, rec) {
  3377. if (pg->index <= index) {
  3378. index -= pg->index;
  3379. /* this is a double loop, break goes to the next page */
  3380. break;
  3381. }
  3382. rec = &pg->records[index];
  3383. enter_record(hash, rec, clear_filter);
  3384. return 1;
  3385. } while_for_each_ftrace_rec();
  3386. return 0;
  3387. }
  3388. #ifdef FTRACE_MCOUNT_MAX_OFFSET
  3389. static int lookup_ip(unsigned long ip, char **modname, char *str)
  3390. {
  3391. unsigned long offset;
  3392. kallsyms_lookup(ip, NULL, &offset, modname, str);
  3393. if (offset > FTRACE_MCOUNT_MAX_OFFSET)
  3394. return -1;
  3395. return 0;
  3396. }
  3397. #else
  3398. static int lookup_ip(unsigned long ip, char **modname, char *str)
  3399. {
  3400. kallsyms_lookup(ip, NULL, NULL, modname, str);
  3401. return 0;
  3402. }
  3403. #endif
  3404. static int
  3405. ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
  3406. struct ftrace_glob *mod_g, int exclude_mod)
  3407. {
  3408. char str[KSYM_SYMBOL_LEN];
  3409. char *modname;
  3410. if (lookup_ip(rec->ip, &modname, str)) {
  3411. /* This should only happen when a rec is disabled */
  3412. WARN_ON_ONCE(system_state == SYSTEM_RUNNING &&
  3413. !(rec->flags & FTRACE_FL_DISABLED));
  3414. return 0;
  3415. }
  3416. if (mod_g) {
  3417. int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
  3418. /* blank module name to match all modules */
  3419. if (!mod_g->len) {
  3420. /* blank module globbing: modname xor exclude_mod */
  3421. if (!exclude_mod != !modname)
  3422. goto func_match;
  3423. return 0;
  3424. }
  3425. /*
  3426. * exclude_mod is set to trace everything but the given
  3427. * module. If it is set and the module matches, then
  3428. * return 0. If it is not set, and the module doesn't match
  3429. * also return 0. Otherwise, check the function to see if
  3430. * that matches.
  3431. */
  3432. if (!mod_matches == !exclude_mod)
  3433. return 0;
  3434. func_match:
  3435. /* blank search means to match all funcs in the mod */
  3436. if (!func_g->len)
  3437. return 1;
  3438. }
  3439. return ftrace_match(str, func_g);
  3440. }
  3441. static int
  3442. match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
  3443. {
  3444. struct ftrace_page *pg;
  3445. struct dyn_ftrace *rec;
  3446. struct ftrace_glob func_g = { .type = MATCH_FULL };
  3447. struct ftrace_glob mod_g = { .type = MATCH_FULL };
  3448. struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
  3449. int exclude_mod = 0;
  3450. int found = 0;
  3451. int ret;
  3452. int clear_filter = 0;
  3453. if (func) {
  3454. func_g.type = filter_parse_regex(func, len, &func_g.search,
  3455. &clear_filter);
  3456. func_g.len = strlen(func_g.search);
  3457. }
  3458. if (mod) {
  3459. mod_g.type = filter_parse_regex(mod, strlen(mod),
  3460. &mod_g.search, &exclude_mod);
  3461. mod_g.len = strlen(mod_g.search);
  3462. }
  3463. mutex_lock(&ftrace_lock);
  3464. if (unlikely(ftrace_disabled))
  3465. goto out_unlock;
  3466. if (func_g.type == MATCH_INDEX) {
  3467. found = add_rec_by_index(hash, &func_g, clear_filter);
  3468. goto out_unlock;
  3469. }
  3470. do_for_each_ftrace_rec(pg, rec) {
  3471. if (rec->flags & FTRACE_FL_DISABLED)
  3472. continue;
  3473. if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
  3474. ret = enter_record(hash, rec, clear_filter);
  3475. if (ret < 0) {
  3476. found = ret;
  3477. goto out_unlock;
  3478. }
  3479. found = 1;
  3480. }
  3481. } while_for_each_ftrace_rec();
  3482. out_unlock:
  3483. mutex_unlock(&ftrace_lock);
  3484. return found;
  3485. }
  3486. static int
  3487. ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
  3488. {
  3489. return match_records(hash, buff, len, NULL);
  3490. }
  3491. static void ftrace_ops_update_code(struct ftrace_ops *ops,
  3492. struct ftrace_ops_hash *old_hash)
  3493. {
  3494. struct ftrace_ops *op;
  3495. if (!ftrace_enabled)
  3496. return;
  3497. if (ops->flags & FTRACE_OPS_FL_ENABLED) {
  3498. ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
  3499. return;
  3500. }
  3501. /*
  3502. * If this is the shared global_ops filter, then we need to
  3503. * check if there is another ops that shares it, is enabled.
  3504. * If so, we still need to run the modify code.
  3505. */
  3506. if (ops->func_hash != &global_ops.local_hash)
  3507. return;
  3508. do_for_each_ftrace_op(op, ftrace_ops_list) {
  3509. if (op->func_hash == &global_ops.local_hash &&
  3510. op->flags & FTRACE_OPS_FL_ENABLED) {
  3511. ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
  3512. /* Only need to do this once */
  3513. return;
  3514. }
  3515. } while_for_each_ftrace_op(op);
  3516. }
  3517. static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
  3518. struct ftrace_hash **orig_hash,
  3519. struct ftrace_hash *hash,
  3520. int enable)
  3521. {
  3522. struct ftrace_ops_hash old_hash_ops;
  3523. struct ftrace_hash *old_hash;
  3524. int ret;
  3525. old_hash = *orig_hash;
  3526. old_hash_ops.filter_hash = ops->func_hash->filter_hash;
  3527. old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
  3528. ret = ftrace_hash_move(ops, enable, orig_hash, hash);
  3529. if (!ret) {
  3530. ftrace_ops_update_code(ops, &old_hash_ops);
  3531. free_ftrace_hash_rcu(old_hash);
  3532. }
  3533. return ret;
  3534. }
  3535. static bool module_exists(const char *module)
  3536. {
  3537. /* All modules have the symbol __this_module */
  3538. static const char this_mod[] = "__this_module";
  3539. char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
  3540. unsigned long val;
  3541. int n;
  3542. n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
  3543. if (n > sizeof(modname) - 1)
  3544. return false;
  3545. val = module_kallsyms_lookup_name(modname);
  3546. return val != 0;
  3547. }
  3548. static int cache_mod(struct trace_array *tr,
  3549. const char *func, char *module, int enable)
  3550. {
  3551. struct ftrace_mod_load *ftrace_mod, *n;
  3552. struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
  3553. int ret;
  3554. mutex_lock(&ftrace_lock);
  3555. /* We do not cache inverse filters */
  3556. if (func[0] == '!') {
  3557. func++;
  3558. ret = -EINVAL;
  3559. /* Look to remove this hash */
  3560. list_for_each_entry_safe(ftrace_mod, n, head, list) {
  3561. if (strcmp(ftrace_mod->module, module) != 0)
  3562. continue;
  3563. /* no func matches all */
  3564. if (strcmp(func, "*") == 0 ||
  3565. (ftrace_mod->func &&
  3566. strcmp(ftrace_mod->func, func) == 0)) {
  3567. ret = 0;
  3568. free_ftrace_mod(ftrace_mod);
  3569. continue;
  3570. }
  3571. }
  3572. goto out;
  3573. }
  3574. ret = -EINVAL;
  3575. /* We only care about modules that have not been loaded yet */
  3576. if (module_exists(module))
  3577. goto out;
  3578. /* Save this string off, and execute it when the module is loaded */
  3579. ret = ftrace_add_mod(tr, func, module, enable);
  3580. out:
  3581. mutex_unlock(&ftrace_lock);
  3582. return ret;
  3583. }
  3584. static int
  3585. ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
  3586. int reset, int enable);
  3587. #ifdef CONFIG_MODULES
  3588. static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
  3589. char *mod, bool enable)
  3590. {
  3591. struct ftrace_mod_load *ftrace_mod, *n;
  3592. struct ftrace_hash **orig_hash, *new_hash;
  3593. LIST_HEAD(process_mods);
  3594. char *func;
  3595. mutex_lock(&ops->func_hash->regex_lock);
  3596. if (enable)
  3597. orig_hash = &ops->func_hash->filter_hash;
  3598. else
  3599. orig_hash = &ops->func_hash->notrace_hash;
  3600. new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
  3601. *orig_hash);
  3602. if (!new_hash)
  3603. goto out; /* warn? */
  3604. mutex_lock(&ftrace_lock);
  3605. list_for_each_entry_safe(ftrace_mod, n, head, list) {
  3606. if (strcmp(ftrace_mod->module, mod) != 0)
  3607. continue;
  3608. if (ftrace_mod->func)
  3609. func = kstrdup(ftrace_mod->func, GFP_KERNEL);
  3610. else
  3611. func = kstrdup("*", GFP_KERNEL);
  3612. if (!func) /* warn? */
  3613. continue;
  3614. list_move(&ftrace_mod->list, &process_mods);
  3615. /* Use the newly allocated func, as it may be "*" */
  3616. kfree(ftrace_mod->func);
  3617. ftrace_mod->func = func;
  3618. }
  3619. mutex_unlock(&ftrace_lock);
  3620. list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
  3621. func = ftrace_mod->func;
  3622. /* Grabs ftrace_lock, which is why we have this extra step */
  3623. match_records(new_hash, func, strlen(func), mod);
  3624. free_ftrace_mod(ftrace_mod);
  3625. }
  3626. if (enable && list_empty(head))
  3627. new_hash->flags &= ~FTRACE_HASH_FL_MOD;
  3628. mutex_lock(&ftrace_lock);
  3629. ftrace_hash_move_and_update_ops(ops, orig_hash,
  3630. new_hash, enable);
  3631. mutex_unlock(&ftrace_lock);
  3632. out:
  3633. mutex_unlock(&ops->func_hash->regex_lock);
  3634. free_ftrace_hash(new_hash);
  3635. }
  3636. static void process_cached_mods(const char *mod_name)
  3637. {
  3638. struct trace_array *tr;
  3639. char *mod;
  3640. mod = kstrdup(mod_name, GFP_KERNEL);
  3641. if (!mod)
  3642. return;
  3643. mutex_lock(&trace_types_lock);
  3644. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  3645. if (!list_empty(&tr->mod_trace))
  3646. process_mod_list(&tr->mod_trace, tr->ops, mod, true);
  3647. if (!list_empty(&tr->mod_notrace))
  3648. process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
  3649. }
  3650. mutex_unlock(&trace_types_lock);
  3651. kfree(mod);
  3652. }
  3653. #endif
  3654. /*
  3655. * We register the module command as a template to show others how
  3656. * to register the a command as well.
  3657. */
  3658. static int
  3659. ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
  3660. char *func_orig, char *cmd, char *module, int enable)
  3661. {
  3662. char *func;
  3663. int ret;
  3664. /* match_records() modifies func, and we need the original */
  3665. func = kstrdup(func_orig, GFP_KERNEL);
  3666. if (!func)
  3667. return -ENOMEM;
  3668. /*
  3669. * cmd == 'mod' because we only registered this func
  3670. * for the 'mod' ftrace_func_command.
  3671. * But if you register one func with multiple commands,
  3672. * you can tell which command was used by the cmd
  3673. * parameter.
  3674. */
  3675. ret = match_records(hash, func, strlen(func), module);
  3676. kfree(func);
  3677. if (!ret)
  3678. return cache_mod(tr, func_orig, module, enable);
  3679. if (ret < 0)
  3680. return ret;
  3681. return 0;
  3682. }
  3683. static struct ftrace_func_command ftrace_mod_cmd = {
  3684. .name = "mod",
  3685. .func = ftrace_mod_callback,
  3686. };
  3687. static int __init ftrace_mod_cmd_init(void)
  3688. {
  3689. return register_ftrace_command(&ftrace_mod_cmd);
  3690. }
  3691. core_initcall(ftrace_mod_cmd_init);
  3692. static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
  3693. struct ftrace_ops *op, struct ftrace_regs *fregs)
  3694. {
  3695. struct ftrace_probe_ops *probe_ops;
  3696. struct ftrace_func_probe *probe;
  3697. probe = container_of(op, struct ftrace_func_probe, ops);
  3698. probe_ops = probe->probe_ops;
  3699. /*
  3700. * Disable preemption for these calls to prevent a RCU grace
  3701. * period. This syncs the hash iteration and freeing of items
  3702. * on the hash. rcu_read_lock is too dangerous here.
  3703. */
  3704. preempt_disable_notrace();
  3705. probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
  3706. preempt_enable_notrace();
  3707. }
  3708. struct ftrace_func_map {
  3709. struct ftrace_func_entry entry;
  3710. void *data;
  3711. };
  3712. struct ftrace_func_mapper {
  3713. struct ftrace_hash hash;
  3714. };
  3715. /**
  3716. * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
  3717. *
  3718. * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
  3719. */
  3720. struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
  3721. {
  3722. struct ftrace_hash *hash;
  3723. /*
  3724. * The mapper is simply a ftrace_hash, but since the entries
  3725. * in the hash are not ftrace_func_entry type, we define it
  3726. * as a separate structure.
  3727. */
  3728. hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
  3729. return (struct ftrace_func_mapper *)hash;
  3730. }
  3731. /**
  3732. * ftrace_func_mapper_find_ip - Find some data mapped to an ip
  3733. * @mapper: The mapper that has the ip maps
  3734. * @ip: the instruction pointer to find the data for
  3735. *
  3736. * Returns the data mapped to @ip if found otherwise NULL. The return
  3737. * is actually the address of the mapper data pointer. The address is
  3738. * returned for use cases where the data is no bigger than a long, and
  3739. * the user can use the data pointer as its data instead of having to
  3740. * allocate more memory for the reference.
  3741. */
  3742. void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
  3743. unsigned long ip)
  3744. {
  3745. struct ftrace_func_entry *entry;
  3746. struct ftrace_func_map *map;
  3747. entry = ftrace_lookup_ip(&mapper->hash, ip);
  3748. if (!entry)
  3749. return NULL;
  3750. map = (struct ftrace_func_map *)entry;
  3751. return &map->data;
  3752. }
  3753. /**
  3754. * ftrace_func_mapper_add_ip - Map some data to an ip
  3755. * @mapper: The mapper that has the ip maps
  3756. * @ip: The instruction pointer address to map @data to
  3757. * @data: The data to map to @ip
  3758. *
  3759. * Returns 0 on success otherwise an error.
  3760. */
  3761. int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
  3762. unsigned long ip, void *data)
  3763. {
  3764. struct ftrace_func_entry *entry;
  3765. struct ftrace_func_map *map;
  3766. entry = ftrace_lookup_ip(&mapper->hash, ip);
  3767. if (entry)
  3768. return -EBUSY;
  3769. map = kmalloc(sizeof(*map), GFP_KERNEL);
  3770. if (!map)
  3771. return -ENOMEM;
  3772. map->entry.ip = ip;
  3773. map->data = data;
  3774. __add_hash_entry(&mapper->hash, &map->entry);
  3775. return 0;
  3776. }
  3777. /**
  3778. * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
  3779. * @mapper: The mapper that has the ip maps
  3780. * @ip: The instruction pointer address to remove the data from
  3781. *
  3782. * Returns the data if it is found, otherwise NULL.
  3783. * Note, if the data pointer is used as the data itself, (see
  3784. * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
  3785. * if the data pointer was set to zero.
  3786. */
  3787. void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
  3788. unsigned long ip)
  3789. {
  3790. struct ftrace_func_entry *entry;
  3791. struct ftrace_func_map *map;
  3792. void *data;
  3793. entry = ftrace_lookup_ip(&mapper->hash, ip);
  3794. if (!entry)
  3795. return NULL;
  3796. map = (struct ftrace_func_map *)entry;
  3797. data = map->data;
  3798. remove_hash_entry(&mapper->hash, entry);
  3799. kfree(entry);
  3800. return data;
  3801. }
  3802. /**
  3803. * free_ftrace_func_mapper - free a mapping of ips and data
  3804. * @mapper: The mapper that has the ip maps
  3805. * @free_func: A function to be called on each data item.
  3806. *
  3807. * This is used to free the function mapper. The @free_func is optional
  3808. * and can be used if the data needs to be freed as well.
  3809. */
  3810. void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
  3811. ftrace_mapper_func free_func)
  3812. {
  3813. struct ftrace_func_entry *entry;
  3814. struct ftrace_func_map *map;
  3815. struct hlist_head *hhd;
  3816. int size, i;
  3817. if (!mapper)
  3818. return;
  3819. if (free_func && mapper->hash.count) {
  3820. size = 1 << mapper->hash.size_bits;
  3821. for (i = 0; i < size; i++) {
  3822. hhd = &mapper->hash.buckets[i];
  3823. hlist_for_each_entry(entry, hhd, hlist) {
  3824. map = (struct ftrace_func_map *)entry;
  3825. free_func(map);
  3826. }
  3827. }
  3828. }
  3829. free_ftrace_hash(&mapper->hash);
  3830. }
  3831. static void release_probe(struct ftrace_func_probe *probe)
  3832. {
  3833. struct ftrace_probe_ops *probe_ops;
  3834. mutex_lock(&ftrace_lock);
  3835. WARN_ON(probe->ref <= 0);
  3836. /* Subtract the ref that was used to protect this instance */
  3837. probe->ref--;
  3838. if (!probe->ref) {
  3839. probe_ops = probe->probe_ops;
  3840. /*
  3841. * Sending zero as ip tells probe_ops to free
  3842. * the probe->data itself
  3843. */
  3844. if (probe_ops->free)
  3845. probe_ops->free(probe_ops, probe->tr, 0, probe->data);
  3846. list_del(&probe->list);
  3847. kfree(probe);
  3848. }
  3849. mutex_unlock(&ftrace_lock);
  3850. }
  3851. static void acquire_probe_locked(struct ftrace_func_probe *probe)
  3852. {
  3853. /*
  3854. * Add one ref to keep it from being freed when releasing the
  3855. * ftrace_lock mutex.
  3856. */
  3857. probe->ref++;
  3858. }
  3859. int
  3860. register_ftrace_function_probe(char *glob, struct trace_array *tr,
  3861. struct ftrace_probe_ops *probe_ops,
  3862. void *data)
  3863. {
  3864. struct ftrace_func_probe *probe = NULL, *iter;
  3865. struct ftrace_func_entry *entry;
  3866. struct ftrace_hash **orig_hash;
  3867. struct ftrace_hash *old_hash;
  3868. struct ftrace_hash *hash;
  3869. int count = 0;
  3870. int size;
  3871. int ret;
  3872. int i;
  3873. if (WARN_ON(!tr))
  3874. return -EINVAL;
  3875. /* We do not support '!' for function probes */
  3876. if (WARN_ON(glob[0] == '!'))
  3877. return -EINVAL;
  3878. mutex_lock(&ftrace_lock);
  3879. /* Check if the probe_ops is already registered */
  3880. list_for_each_entry(iter, &tr->func_probes, list) {
  3881. if (iter->probe_ops == probe_ops) {
  3882. probe = iter;
  3883. break;
  3884. }
  3885. }
  3886. if (!probe) {
  3887. probe = kzalloc(sizeof(*probe), GFP_KERNEL);
  3888. if (!probe) {
  3889. mutex_unlock(&ftrace_lock);
  3890. return -ENOMEM;
  3891. }
  3892. probe->probe_ops = probe_ops;
  3893. probe->ops.func = function_trace_probe_call;
  3894. probe->tr = tr;
  3895. ftrace_ops_init(&probe->ops);
  3896. list_add(&probe->list, &tr->func_probes);
  3897. }
  3898. acquire_probe_locked(probe);
  3899. mutex_unlock(&ftrace_lock);
  3900. /*
  3901. * Note, there's a small window here that the func_hash->filter_hash
  3902. * may be NULL or empty. Need to be careful when reading the loop.
  3903. */
  3904. mutex_lock(&probe->ops.func_hash->regex_lock);
  3905. orig_hash = &probe->ops.func_hash->filter_hash;
  3906. old_hash = *orig_hash;
  3907. hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
  3908. if (!hash) {
  3909. ret = -ENOMEM;
  3910. goto out;
  3911. }
  3912. ret = ftrace_match_records(hash, glob, strlen(glob));
  3913. /* Nothing found? */
  3914. if (!ret)
  3915. ret = -EINVAL;
  3916. if (ret < 0)
  3917. goto out;
  3918. size = 1 << hash->size_bits;
  3919. for (i = 0; i < size; i++) {
  3920. hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
  3921. if (ftrace_lookup_ip(old_hash, entry->ip))
  3922. continue;
  3923. /*
  3924. * The caller might want to do something special
  3925. * for each function we find. We call the callback
  3926. * to give the caller an opportunity to do so.
  3927. */
  3928. if (probe_ops->init) {
  3929. ret = probe_ops->init(probe_ops, tr,
  3930. entry->ip, data,
  3931. &probe->data);
  3932. if (ret < 0) {
  3933. if (probe_ops->free && count)
  3934. probe_ops->free(probe_ops, tr,
  3935. 0, probe->data);
  3936. probe->data = NULL;
  3937. goto out;
  3938. }
  3939. }
  3940. count++;
  3941. }
  3942. }
  3943. mutex_lock(&ftrace_lock);
  3944. if (!count) {
  3945. /* Nothing was added? */
  3946. ret = -EINVAL;
  3947. goto out_unlock;
  3948. }
  3949. ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
  3950. hash, 1);
  3951. if (ret < 0)
  3952. goto err_unlock;
  3953. /* One ref for each new function traced */
  3954. probe->ref += count;
  3955. if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
  3956. ret = ftrace_startup(&probe->ops, 0);
  3957. out_unlock:
  3958. mutex_unlock(&ftrace_lock);
  3959. if (!ret)
  3960. ret = count;
  3961. out:
  3962. mutex_unlock(&probe->ops.func_hash->regex_lock);
  3963. free_ftrace_hash(hash);
  3964. release_probe(probe);
  3965. return ret;
  3966. err_unlock:
  3967. if (!probe_ops->free || !count)
  3968. goto out_unlock;
  3969. /* Failed to do the move, need to call the free functions */
  3970. for (i = 0; i < size; i++) {
  3971. hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
  3972. if (ftrace_lookup_ip(old_hash, entry->ip))
  3973. continue;
  3974. probe_ops->free(probe_ops, tr, entry->ip, probe->data);
  3975. }
  3976. }
  3977. goto out_unlock;
  3978. }
  3979. int
  3980. unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
  3981. struct ftrace_probe_ops *probe_ops)
  3982. {
  3983. struct ftrace_func_probe *probe = NULL, *iter;
  3984. struct ftrace_ops_hash old_hash_ops;
  3985. struct ftrace_func_entry *entry;
  3986. struct ftrace_glob func_g;
  3987. struct ftrace_hash **orig_hash;
  3988. struct ftrace_hash *old_hash;
  3989. struct ftrace_hash *hash = NULL;
  3990. struct hlist_node *tmp;
  3991. struct hlist_head hhd;
  3992. char str[KSYM_SYMBOL_LEN];
  3993. int count = 0;
  3994. int i, ret = -ENODEV;
  3995. int size;
  3996. if (!glob || !strlen(glob) || !strcmp(glob, "*"))
  3997. func_g.search = NULL;
  3998. else {
  3999. int not;
  4000. func_g.type = filter_parse_regex(glob, strlen(glob),
  4001. &func_g.search, &not);
  4002. func_g.len = strlen(func_g.search);
  4003. /* we do not support '!' for function probes */
  4004. if (WARN_ON(not))
  4005. return -EINVAL;
  4006. }
  4007. mutex_lock(&ftrace_lock);
  4008. /* Check if the probe_ops is already registered */
  4009. list_for_each_entry(iter, &tr->func_probes, list) {
  4010. if (iter->probe_ops == probe_ops) {
  4011. probe = iter;
  4012. break;
  4013. }
  4014. }
  4015. if (!probe)
  4016. goto err_unlock_ftrace;
  4017. ret = -EINVAL;
  4018. if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
  4019. goto err_unlock_ftrace;
  4020. acquire_probe_locked(probe);
  4021. mutex_unlock(&ftrace_lock);
  4022. mutex_lock(&probe->ops.func_hash->regex_lock);
  4023. orig_hash = &probe->ops.func_hash->filter_hash;
  4024. old_hash = *orig_hash;
  4025. if (ftrace_hash_empty(old_hash))
  4026. goto out_unlock;
  4027. old_hash_ops.filter_hash = old_hash;
  4028. /* Probes only have filters */
  4029. old_hash_ops.notrace_hash = NULL;
  4030. ret = -ENOMEM;
  4031. hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
  4032. if (!hash)
  4033. goto out_unlock;
  4034. INIT_HLIST_HEAD(&hhd);
  4035. size = 1 << hash->size_bits;
  4036. for (i = 0; i < size; i++) {
  4037. hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
  4038. if (func_g.search) {
  4039. kallsyms_lookup(entry->ip, NULL, NULL,
  4040. NULL, str);
  4041. if (!ftrace_match(str, &func_g))
  4042. continue;
  4043. }
  4044. count++;
  4045. remove_hash_entry(hash, entry);
  4046. hlist_add_head(&entry->hlist, &hhd);
  4047. }
  4048. }
  4049. /* Nothing found? */
  4050. if (!count) {
  4051. ret = -EINVAL;
  4052. goto out_unlock;
  4053. }
  4054. mutex_lock(&ftrace_lock);
  4055. WARN_ON(probe->ref < count);
  4056. probe->ref -= count;
  4057. if (ftrace_hash_empty(hash))
  4058. ftrace_shutdown(&probe->ops, 0);
  4059. ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
  4060. hash, 1);
  4061. /* still need to update the function call sites */
  4062. if (ftrace_enabled && !ftrace_hash_empty(hash))
  4063. ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
  4064. &old_hash_ops);
  4065. synchronize_rcu();
  4066. hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
  4067. hlist_del(&entry->hlist);
  4068. if (probe_ops->free)
  4069. probe_ops->free(probe_ops, tr, entry->ip, probe->data);
  4070. kfree(entry);
  4071. }
  4072. mutex_unlock(&ftrace_lock);
  4073. out_unlock:
  4074. mutex_unlock(&probe->ops.func_hash->regex_lock);
  4075. free_ftrace_hash(hash);
  4076. release_probe(probe);
  4077. return ret;
  4078. err_unlock_ftrace:
  4079. mutex_unlock(&ftrace_lock);
  4080. return ret;
  4081. }
  4082. void clear_ftrace_function_probes(struct trace_array *tr)
  4083. {
  4084. struct ftrace_func_probe *probe, *n;
  4085. list_for_each_entry_safe(probe, n, &tr->func_probes, list)
  4086. unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
  4087. }
  4088. static LIST_HEAD(ftrace_commands);
  4089. static DEFINE_MUTEX(ftrace_cmd_mutex);
  4090. /*
  4091. * Currently we only register ftrace commands from __init, so mark this
  4092. * __init too.
  4093. */
  4094. __init int register_ftrace_command(struct ftrace_func_command *cmd)
  4095. {
  4096. struct ftrace_func_command *p;
  4097. int ret = 0;
  4098. mutex_lock(&ftrace_cmd_mutex);
  4099. list_for_each_entry(p, &ftrace_commands, list) {
  4100. if (strcmp(cmd->name, p->name) == 0) {
  4101. ret = -EBUSY;
  4102. goto out_unlock;
  4103. }
  4104. }
  4105. list_add(&cmd->list, &ftrace_commands);
  4106. out_unlock:
  4107. mutex_unlock(&ftrace_cmd_mutex);
  4108. return ret;
  4109. }
  4110. /*
  4111. * Currently we only unregister ftrace commands from __init, so mark
  4112. * this __init too.
  4113. */
  4114. __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
  4115. {
  4116. struct ftrace_func_command *p, *n;
  4117. int ret = -ENODEV;
  4118. mutex_lock(&ftrace_cmd_mutex);
  4119. list_for_each_entry_safe(p, n, &ftrace_commands, list) {
  4120. if (strcmp(cmd->name, p->name) == 0) {
  4121. ret = 0;
  4122. list_del_init(&p->list);
  4123. goto out_unlock;
  4124. }
  4125. }
  4126. out_unlock:
  4127. mutex_unlock(&ftrace_cmd_mutex);
  4128. return ret;
  4129. }
  4130. static int ftrace_process_regex(struct ftrace_iterator *iter,
  4131. char *buff, int len, int enable)
  4132. {
  4133. struct ftrace_hash *hash = iter->hash;
  4134. struct trace_array *tr = iter->ops->private;
  4135. char *func, *command, *next = buff;
  4136. struct ftrace_func_command *p;
  4137. int ret = -EINVAL;
  4138. func = strsep(&next, ":");
  4139. if (!next) {
  4140. ret = ftrace_match_records(hash, func, len);
  4141. if (!ret)
  4142. ret = -EINVAL;
  4143. if (ret < 0)
  4144. return ret;
  4145. return 0;
  4146. }
  4147. /* command found */
  4148. command = strsep(&next, ":");
  4149. mutex_lock(&ftrace_cmd_mutex);
  4150. list_for_each_entry(p, &ftrace_commands, list) {
  4151. if (strcmp(p->name, command) == 0) {
  4152. ret = p->func(tr, hash, func, command, next, enable);
  4153. goto out_unlock;
  4154. }
  4155. }
  4156. out_unlock:
  4157. mutex_unlock(&ftrace_cmd_mutex);
  4158. return ret;
  4159. }
  4160. static ssize_t
  4161. ftrace_regex_write(struct file *file, const char __user *ubuf,
  4162. size_t cnt, loff_t *ppos, int enable)
  4163. {
  4164. struct ftrace_iterator *iter;
  4165. struct trace_parser *parser;
  4166. ssize_t ret, read;
  4167. if (!cnt)
  4168. return 0;
  4169. if (file->f_mode & FMODE_READ) {
  4170. struct seq_file *m = file->private_data;
  4171. iter = m->private;
  4172. } else
  4173. iter = file->private_data;
  4174. if (unlikely(ftrace_disabled))
  4175. return -ENODEV;
  4176. /* iter->hash is a local copy, so we don't need regex_lock */
  4177. parser = &iter->parser;
  4178. read = trace_get_user(parser, ubuf, cnt, ppos);
  4179. if (read >= 0 && trace_parser_loaded(parser) &&
  4180. !trace_parser_cont(parser)) {
  4181. ret = ftrace_process_regex(iter, parser->buffer,
  4182. parser->idx, enable);
  4183. trace_parser_clear(parser);
  4184. if (ret < 0)
  4185. goto out;
  4186. }
  4187. ret = read;
  4188. out:
  4189. return ret;
  4190. }
  4191. ssize_t
  4192. ftrace_filter_write(struct file *file, const char __user *ubuf,
  4193. size_t cnt, loff_t *ppos)
  4194. {
  4195. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  4196. }
  4197. ssize_t
  4198. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  4199. size_t cnt, loff_t *ppos)
  4200. {
  4201. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  4202. }
  4203. static int
  4204. __ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
  4205. {
  4206. struct ftrace_func_entry *entry;
  4207. ip = ftrace_location(ip);
  4208. if (!ip)
  4209. return -EINVAL;
  4210. if (remove) {
  4211. entry = ftrace_lookup_ip(hash, ip);
  4212. if (!entry)
  4213. return -ENOENT;
  4214. free_hash_entry(hash, entry);
  4215. return 0;
  4216. }
  4217. return add_hash_entry(hash, ip);
  4218. }
  4219. static int
  4220. ftrace_match_addr(struct ftrace_hash *hash, unsigned long *ips,
  4221. unsigned int cnt, int remove)
  4222. {
  4223. unsigned int i;
  4224. int err;
  4225. for (i = 0; i < cnt; i++) {
  4226. err = __ftrace_match_addr(hash, ips[i], remove);
  4227. if (err) {
  4228. /*
  4229. * This expects the @hash is a temporary hash and if this
  4230. * fails the caller must free the @hash.
  4231. */
  4232. return err;
  4233. }
  4234. }
  4235. return 0;
  4236. }
  4237. static int
  4238. ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
  4239. unsigned long *ips, unsigned int cnt,
  4240. int remove, int reset, int enable)
  4241. {
  4242. struct ftrace_hash **orig_hash;
  4243. struct ftrace_hash *hash;
  4244. int ret;
  4245. if (unlikely(ftrace_disabled))
  4246. return -ENODEV;
  4247. mutex_lock(&ops->func_hash->regex_lock);
  4248. if (enable)
  4249. orig_hash = &ops->func_hash->filter_hash;
  4250. else
  4251. orig_hash = &ops->func_hash->notrace_hash;
  4252. if (reset)
  4253. hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
  4254. else
  4255. hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
  4256. if (!hash) {
  4257. ret = -ENOMEM;
  4258. goto out_regex_unlock;
  4259. }
  4260. if (buf && !ftrace_match_records(hash, buf, len)) {
  4261. ret = -EINVAL;
  4262. goto out_regex_unlock;
  4263. }
  4264. if (ips) {
  4265. ret = ftrace_match_addr(hash, ips, cnt, remove);
  4266. if (ret < 0)
  4267. goto out_regex_unlock;
  4268. }
  4269. mutex_lock(&ftrace_lock);
  4270. ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
  4271. mutex_unlock(&ftrace_lock);
  4272. out_regex_unlock:
  4273. mutex_unlock(&ops->func_hash->regex_lock);
  4274. free_ftrace_hash(hash);
  4275. return ret;
  4276. }
  4277. static int
  4278. ftrace_set_addr(struct ftrace_ops *ops, unsigned long *ips, unsigned int cnt,
  4279. int remove, int reset, int enable)
  4280. {
  4281. return ftrace_set_hash(ops, NULL, 0, ips, cnt, remove, reset, enable);
  4282. }
  4283. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
  4284. struct ftrace_direct_func {
  4285. struct list_head next;
  4286. unsigned long addr;
  4287. int count;
  4288. };
  4289. static LIST_HEAD(ftrace_direct_funcs);
  4290. /**
  4291. * ftrace_find_direct_func - test an address if it is a registered direct caller
  4292. * @addr: The address of a registered direct caller
  4293. *
  4294. * This searches to see if a ftrace direct caller has been registered
  4295. * at a specific address, and if so, it returns a descriptor for it.
  4296. *
  4297. * This can be used by architecture code to see if an address is
  4298. * a direct caller (trampoline) attached to a fentry/mcount location.
  4299. * This is useful for the function_graph tracer, as it may need to
  4300. * do adjustments if it traced a location that also has a direct
  4301. * trampoline attached to it.
  4302. */
  4303. struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr)
  4304. {
  4305. struct ftrace_direct_func *entry;
  4306. bool found = false;
  4307. /* May be called by fgraph trampoline (protected by rcu tasks) */
  4308. list_for_each_entry_rcu(entry, &ftrace_direct_funcs, next) {
  4309. if (entry->addr == addr) {
  4310. found = true;
  4311. break;
  4312. }
  4313. }
  4314. if (found)
  4315. return entry;
  4316. return NULL;
  4317. }
  4318. static struct ftrace_direct_func *ftrace_alloc_direct_func(unsigned long addr)
  4319. {
  4320. struct ftrace_direct_func *direct;
  4321. direct = kmalloc(sizeof(*direct), GFP_KERNEL);
  4322. if (!direct)
  4323. return NULL;
  4324. direct->addr = addr;
  4325. direct->count = 0;
  4326. list_add_rcu(&direct->next, &ftrace_direct_funcs);
  4327. ftrace_direct_func_count++;
  4328. return direct;
  4329. }
  4330. static int register_ftrace_function_nolock(struct ftrace_ops *ops);
  4331. /**
  4332. * register_ftrace_direct - Call a custom trampoline directly
  4333. * @ip: The address of the nop at the beginning of a function
  4334. * @addr: The address of the trampoline to call at @ip
  4335. *
  4336. * This is used to connect a direct call from the nop location (@ip)
  4337. * at the start of ftrace traced functions. The location that it calls
  4338. * (@addr) must be able to handle a direct call, and save the parameters
  4339. * of the function being traced, and restore them (or inject new ones
  4340. * if needed), before returning.
  4341. *
  4342. * Returns:
  4343. * 0 on success
  4344. * -EBUSY - Another direct function is already attached (there can be only one)
  4345. * -ENODEV - @ip does not point to a ftrace nop location (or not supported)
  4346. * -ENOMEM - There was an allocation failure.
  4347. */
  4348. int register_ftrace_direct(unsigned long ip, unsigned long addr)
  4349. {
  4350. struct ftrace_direct_func *direct;
  4351. struct ftrace_func_entry *entry;
  4352. struct ftrace_hash *free_hash = NULL;
  4353. struct dyn_ftrace *rec;
  4354. int ret = -ENODEV;
  4355. mutex_lock(&direct_mutex);
  4356. ip = ftrace_location(ip);
  4357. if (!ip)
  4358. goto out_unlock;
  4359. /* See if there's a direct function at @ip already */
  4360. ret = -EBUSY;
  4361. if (ftrace_find_rec_direct(ip))
  4362. goto out_unlock;
  4363. ret = -ENODEV;
  4364. rec = lookup_rec(ip, ip);
  4365. if (!rec)
  4366. goto out_unlock;
  4367. /*
  4368. * Check if the rec says it has a direct call but we didn't
  4369. * find one earlier?
  4370. */
  4371. if (WARN_ON(rec->flags & FTRACE_FL_DIRECT))
  4372. goto out_unlock;
  4373. /* Make sure the ip points to the exact record */
  4374. if (ip != rec->ip) {
  4375. ip = rec->ip;
  4376. /* Need to check this ip for a direct. */
  4377. if (ftrace_find_rec_direct(ip))
  4378. goto out_unlock;
  4379. }
  4380. ret = -ENOMEM;
  4381. direct = ftrace_find_direct_func(addr);
  4382. if (!direct) {
  4383. direct = ftrace_alloc_direct_func(addr);
  4384. if (!direct)
  4385. goto out_unlock;
  4386. }
  4387. entry = ftrace_add_rec_direct(ip, addr, &free_hash);
  4388. if (!entry)
  4389. goto out_unlock;
  4390. ret = ftrace_set_filter_ip(&direct_ops, ip, 0, 0);
  4391. if (!ret && !(direct_ops.flags & FTRACE_OPS_FL_ENABLED)) {
  4392. ret = register_ftrace_function_nolock(&direct_ops);
  4393. if (ret)
  4394. ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
  4395. }
  4396. if (ret) {
  4397. remove_hash_entry(direct_functions, entry);
  4398. kfree(entry);
  4399. if (!direct->count) {
  4400. list_del_rcu(&direct->next);
  4401. synchronize_rcu_tasks();
  4402. kfree(direct);
  4403. if (free_hash)
  4404. free_ftrace_hash(free_hash);
  4405. free_hash = NULL;
  4406. ftrace_direct_func_count--;
  4407. }
  4408. } else {
  4409. direct->count++;
  4410. }
  4411. out_unlock:
  4412. mutex_unlock(&direct_mutex);
  4413. if (free_hash) {
  4414. synchronize_rcu_tasks();
  4415. free_ftrace_hash(free_hash);
  4416. }
  4417. return ret;
  4418. }
  4419. EXPORT_SYMBOL_GPL(register_ftrace_direct);
  4420. static struct ftrace_func_entry *find_direct_entry(unsigned long *ip,
  4421. struct dyn_ftrace **recp)
  4422. {
  4423. struct ftrace_func_entry *entry;
  4424. struct dyn_ftrace *rec;
  4425. rec = lookup_rec(*ip, *ip);
  4426. if (!rec)
  4427. return NULL;
  4428. entry = __ftrace_lookup_ip(direct_functions, rec->ip);
  4429. if (!entry) {
  4430. WARN_ON(rec->flags & FTRACE_FL_DIRECT);
  4431. return NULL;
  4432. }
  4433. WARN_ON(!(rec->flags & FTRACE_FL_DIRECT));
  4434. /* Passed in ip just needs to be on the call site */
  4435. *ip = rec->ip;
  4436. if (recp)
  4437. *recp = rec;
  4438. return entry;
  4439. }
  4440. int unregister_ftrace_direct(unsigned long ip, unsigned long addr)
  4441. {
  4442. struct ftrace_direct_func *direct;
  4443. struct ftrace_func_entry *entry;
  4444. struct ftrace_hash *hash;
  4445. int ret = -ENODEV;
  4446. mutex_lock(&direct_mutex);
  4447. ip = ftrace_location(ip);
  4448. if (!ip)
  4449. goto out_unlock;
  4450. entry = find_direct_entry(&ip, NULL);
  4451. if (!entry)
  4452. goto out_unlock;
  4453. hash = direct_ops.func_hash->filter_hash;
  4454. if (hash->count == 1)
  4455. unregister_ftrace_function(&direct_ops);
  4456. ret = ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
  4457. WARN_ON(ret);
  4458. remove_hash_entry(direct_functions, entry);
  4459. direct = ftrace_find_direct_func(addr);
  4460. if (!WARN_ON(!direct)) {
  4461. /* This is the good path (see the ! before WARN) */
  4462. direct->count--;
  4463. WARN_ON(direct->count < 0);
  4464. if (!direct->count) {
  4465. list_del_rcu(&direct->next);
  4466. synchronize_rcu_tasks();
  4467. kfree(direct);
  4468. kfree(entry);
  4469. ftrace_direct_func_count--;
  4470. }
  4471. }
  4472. out_unlock:
  4473. mutex_unlock(&direct_mutex);
  4474. return ret;
  4475. }
  4476. EXPORT_SYMBOL_GPL(unregister_ftrace_direct);
  4477. static struct ftrace_ops stub_ops = {
  4478. .func = ftrace_stub,
  4479. };
  4480. /**
  4481. * ftrace_modify_direct_caller - modify ftrace nop directly
  4482. * @entry: The ftrace hash entry of the direct helper for @rec
  4483. * @rec: The record representing the function site to patch
  4484. * @old_addr: The location that the site at @rec->ip currently calls
  4485. * @new_addr: The location that the site at @rec->ip should call
  4486. *
  4487. * An architecture may overwrite this function to optimize the
  4488. * changing of the direct callback on an ftrace nop location.
  4489. * This is called with the ftrace_lock mutex held, and no other
  4490. * ftrace callbacks are on the associated record (@rec). Thus,
  4491. * it is safe to modify the ftrace record, where it should be
  4492. * currently calling @old_addr directly, to call @new_addr.
  4493. *
  4494. * This is called with direct_mutex locked.
  4495. *
  4496. * Safety checks should be made to make sure that the code at
  4497. * @rec->ip is currently calling @old_addr. And this must
  4498. * also update entry->direct to @new_addr.
  4499. */
  4500. int __weak ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
  4501. struct dyn_ftrace *rec,
  4502. unsigned long old_addr,
  4503. unsigned long new_addr)
  4504. {
  4505. unsigned long ip = rec->ip;
  4506. int ret;
  4507. lockdep_assert_held(&direct_mutex);
  4508. /*
  4509. * The ftrace_lock was used to determine if the record
  4510. * had more than one registered user to it. If it did,
  4511. * we needed to prevent that from changing to do the quick
  4512. * switch. But if it did not (only a direct caller was attached)
  4513. * then this function is called. But this function can deal
  4514. * with attached callers to the rec that we care about, and
  4515. * since this function uses standard ftrace calls that take
  4516. * the ftrace_lock mutex, we need to release it.
  4517. */
  4518. mutex_unlock(&ftrace_lock);
  4519. /*
  4520. * By setting a stub function at the same address, we force
  4521. * the code to call the iterator and the direct_ops helper.
  4522. * This means that @ip does not call the direct call, and
  4523. * we can simply modify it.
  4524. */
  4525. ret = ftrace_set_filter_ip(&stub_ops, ip, 0, 0);
  4526. if (ret)
  4527. goto out_lock;
  4528. ret = register_ftrace_function_nolock(&stub_ops);
  4529. if (ret) {
  4530. ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
  4531. goto out_lock;
  4532. }
  4533. entry->direct = new_addr;
  4534. /*
  4535. * By removing the stub, we put back the direct call, calling
  4536. * the @new_addr.
  4537. */
  4538. unregister_ftrace_function(&stub_ops);
  4539. ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
  4540. out_lock:
  4541. mutex_lock(&ftrace_lock);
  4542. return ret;
  4543. }
  4544. /**
  4545. * modify_ftrace_direct - Modify an existing direct call to call something else
  4546. * @ip: The instruction pointer to modify
  4547. * @old_addr: The address that the current @ip calls directly
  4548. * @new_addr: The address that the @ip should call
  4549. *
  4550. * This modifies a ftrace direct caller at an instruction pointer without
  4551. * having to disable it first. The direct call will switch over to the
  4552. * @new_addr without missing anything.
  4553. *
  4554. * Returns: zero on success. Non zero on error, which includes:
  4555. * -ENODEV : the @ip given has no direct caller attached
  4556. * -EINVAL : the @old_addr does not match the current direct caller
  4557. */
  4558. int modify_ftrace_direct(unsigned long ip,
  4559. unsigned long old_addr, unsigned long new_addr)
  4560. {
  4561. struct ftrace_direct_func *direct, *new_direct = NULL;
  4562. struct ftrace_func_entry *entry;
  4563. struct dyn_ftrace *rec;
  4564. int ret = -ENODEV;
  4565. mutex_lock(&direct_mutex);
  4566. mutex_lock(&ftrace_lock);
  4567. ip = ftrace_location(ip);
  4568. if (!ip)
  4569. goto out_unlock;
  4570. entry = find_direct_entry(&ip, &rec);
  4571. if (!entry)
  4572. goto out_unlock;
  4573. ret = -EINVAL;
  4574. if (entry->direct != old_addr)
  4575. goto out_unlock;
  4576. direct = ftrace_find_direct_func(old_addr);
  4577. if (WARN_ON(!direct))
  4578. goto out_unlock;
  4579. if (direct->count > 1) {
  4580. ret = -ENOMEM;
  4581. new_direct = ftrace_alloc_direct_func(new_addr);
  4582. if (!new_direct)
  4583. goto out_unlock;
  4584. direct->count--;
  4585. new_direct->count++;
  4586. } else {
  4587. direct->addr = new_addr;
  4588. }
  4589. /*
  4590. * If there's no other ftrace callback on the rec->ip location,
  4591. * then it can be changed directly by the architecture.
  4592. * If there is another caller, then we just need to change the
  4593. * direct caller helper to point to @new_addr.
  4594. */
  4595. if (ftrace_rec_count(rec) == 1) {
  4596. ret = ftrace_modify_direct_caller(entry, rec, old_addr, new_addr);
  4597. } else {
  4598. entry->direct = new_addr;
  4599. ret = 0;
  4600. }
  4601. if (ret) {
  4602. direct->addr = old_addr;
  4603. if (unlikely(new_direct)) {
  4604. direct->count++;
  4605. list_del_rcu(&new_direct->next);
  4606. synchronize_rcu_tasks();
  4607. kfree(new_direct);
  4608. ftrace_direct_func_count--;
  4609. }
  4610. }
  4611. out_unlock:
  4612. mutex_unlock(&ftrace_lock);
  4613. mutex_unlock(&direct_mutex);
  4614. return ret;
  4615. }
  4616. EXPORT_SYMBOL_GPL(modify_ftrace_direct);
  4617. #define MULTI_FLAGS (FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_REGS)
  4618. static int check_direct_multi(struct ftrace_ops *ops)
  4619. {
  4620. if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED))
  4621. return -EINVAL;
  4622. if ((ops->flags & MULTI_FLAGS) != MULTI_FLAGS)
  4623. return -EINVAL;
  4624. return 0;
  4625. }
  4626. static void remove_direct_functions_hash(struct ftrace_hash *hash, unsigned long addr)
  4627. {
  4628. struct ftrace_func_entry *entry, *del;
  4629. int size, i;
  4630. size = 1 << hash->size_bits;
  4631. for (i = 0; i < size; i++) {
  4632. hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
  4633. del = __ftrace_lookup_ip(direct_functions, entry->ip);
  4634. if (del && del->direct == addr) {
  4635. remove_hash_entry(direct_functions, del);
  4636. kfree(del);
  4637. }
  4638. }
  4639. }
  4640. }
  4641. /**
  4642. * register_ftrace_direct_multi - Call a custom trampoline directly
  4643. * for multiple functions registered in @ops
  4644. * @ops: The address of the struct ftrace_ops object
  4645. * @addr: The address of the trampoline to call at @ops functions
  4646. *
  4647. * This is used to connect a direct calls to @addr from the nop locations
  4648. * of the functions registered in @ops (with by ftrace_set_filter_ip
  4649. * function).
  4650. *
  4651. * The location that it calls (@addr) must be able to handle a direct call,
  4652. * and save the parameters of the function being traced, and restore them
  4653. * (or inject new ones if needed), before returning.
  4654. *
  4655. * Returns:
  4656. * 0 on success
  4657. * -EINVAL - The @ops object was already registered with this call or
  4658. * when there are no functions in @ops object.
  4659. * -EBUSY - Another direct function is already attached (there can be only one)
  4660. * -ENODEV - @ip does not point to a ftrace nop location (or not supported)
  4661. * -ENOMEM - There was an allocation failure.
  4662. */
  4663. int register_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
  4664. {
  4665. struct ftrace_hash *hash, *free_hash = NULL;
  4666. struct ftrace_func_entry *entry, *new;
  4667. int err = -EBUSY, size, i;
  4668. if (ops->func || ops->trampoline)
  4669. return -EINVAL;
  4670. if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED))
  4671. return -EINVAL;
  4672. if (ops->flags & FTRACE_OPS_FL_ENABLED)
  4673. return -EINVAL;
  4674. hash = ops->func_hash->filter_hash;
  4675. if (ftrace_hash_empty(hash))
  4676. return -EINVAL;
  4677. mutex_lock(&direct_mutex);
  4678. /* Make sure requested entries are not already registered.. */
  4679. size = 1 << hash->size_bits;
  4680. for (i = 0; i < size; i++) {
  4681. hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
  4682. if (ftrace_find_rec_direct(entry->ip))
  4683. goto out_unlock;
  4684. }
  4685. }
  4686. /* ... and insert them to direct_functions hash. */
  4687. err = -ENOMEM;
  4688. for (i = 0; i < size; i++) {
  4689. hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
  4690. new = ftrace_add_rec_direct(entry->ip, addr, &free_hash);
  4691. if (!new)
  4692. goto out_remove;
  4693. entry->direct = addr;
  4694. }
  4695. }
  4696. ops->func = call_direct_funcs;
  4697. ops->flags = MULTI_FLAGS;
  4698. ops->trampoline = FTRACE_REGS_ADDR;
  4699. err = register_ftrace_function_nolock(ops);
  4700. out_remove:
  4701. if (err)
  4702. remove_direct_functions_hash(hash, addr);
  4703. out_unlock:
  4704. mutex_unlock(&direct_mutex);
  4705. if (free_hash) {
  4706. synchronize_rcu_tasks();
  4707. free_ftrace_hash(free_hash);
  4708. }
  4709. return err;
  4710. }
  4711. EXPORT_SYMBOL_GPL(register_ftrace_direct_multi);
  4712. /**
  4713. * unregister_ftrace_direct_multi - Remove calls to custom trampoline
  4714. * previously registered by register_ftrace_direct_multi for @ops object.
  4715. * @ops: The address of the struct ftrace_ops object
  4716. *
  4717. * This is used to remove a direct calls to @addr from the nop locations
  4718. * of the functions registered in @ops (with by ftrace_set_filter_ip
  4719. * function).
  4720. *
  4721. * Returns:
  4722. * 0 on success
  4723. * -EINVAL - The @ops object was not properly registered.
  4724. */
  4725. int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
  4726. {
  4727. struct ftrace_hash *hash = ops->func_hash->filter_hash;
  4728. int err;
  4729. if (check_direct_multi(ops))
  4730. return -EINVAL;
  4731. if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
  4732. return -EINVAL;
  4733. mutex_lock(&direct_mutex);
  4734. err = unregister_ftrace_function(ops);
  4735. remove_direct_functions_hash(hash, addr);
  4736. mutex_unlock(&direct_mutex);
  4737. /* cleanup for possible another register call */
  4738. ops->func = NULL;
  4739. ops->trampoline = 0;
  4740. return err;
  4741. }
  4742. EXPORT_SYMBOL_GPL(unregister_ftrace_direct_multi);
  4743. static int
  4744. __modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
  4745. {
  4746. struct ftrace_hash *hash;
  4747. struct ftrace_func_entry *entry, *iter;
  4748. static struct ftrace_ops tmp_ops = {
  4749. .func = ftrace_stub,
  4750. .flags = FTRACE_OPS_FL_STUB,
  4751. };
  4752. int i, size;
  4753. int err;
  4754. lockdep_assert_held_once(&direct_mutex);
  4755. /* Enable the tmp_ops to have the same functions as the direct ops */
  4756. ftrace_ops_init(&tmp_ops);
  4757. tmp_ops.func_hash = ops->func_hash;
  4758. err = register_ftrace_function_nolock(&tmp_ops);
  4759. if (err)
  4760. return err;
  4761. /*
  4762. * Now the ftrace_ops_list_func() is called to do the direct callers.
  4763. * We can safely change the direct functions attached to each entry.
  4764. */
  4765. mutex_lock(&ftrace_lock);
  4766. hash = ops->func_hash->filter_hash;
  4767. size = 1 << hash->size_bits;
  4768. for (i = 0; i < size; i++) {
  4769. hlist_for_each_entry(iter, &hash->buckets[i], hlist) {
  4770. entry = __ftrace_lookup_ip(direct_functions, iter->ip);
  4771. if (!entry)
  4772. continue;
  4773. entry->direct = addr;
  4774. }
  4775. }
  4776. mutex_unlock(&ftrace_lock);
  4777. /* Removing the tmp_ops will add the updated direct callers to the functions */
  4778. unregister_ftrace_function(&tmp_ops);
  4779. return err;
  4780. }
  4781. /**
  4782. * modify_ftrace_direct_multi_nolock - Modify an existing direct 'multi' call
  4783. * to call something else
  4784. * @ops: The address of the struct ftrace_ops object
  4785. * @addr: The address of the new trampoline to call at @ops functions
  4786. *
  4787. * This is used to unregister currently registered direct caller and
  4788. * register new one @addr on functions registered in @ops object.
  4789. *
  4790. * Note there's window between ftrace_shutdown and ftrace_startup calls
  4791. * where there will be no callbacks called.
  4792. *
  4793. * Caller should already have direct_mutex locked, so we don't lock
  4794. * direct_mutex here.
  4795. *
  4796. * Returns: zero on success. Non zero on error, which includes:
  4797. * -EINVAL - The @ops object was not properly registered.
  4798. */
  4799. int modify_ftrace_direct_multi_nolock(struct ftrace_ops *ops, unsigned long addr)
  4800. {
  4801. if (check_direct_multi(ops))
  4802. return -EINVAL;
  4803. if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
  4804. return -EINVAL;
  4805. return __modify_ftrace_direct_multi(ops, addr);
  4806. }
  4807. EXPORT_SYMBOL_GPL(modify_ftrace_direct_multi_nolock);
  4808. /**
  4809. * modify_ftrace_direct_multi - Modify an existing direct 'multi' call
  4810. * to call something else
  4811. * @ops: The address of the struct ftrace_ops object
  4812. * @addr: The address of the new trampoline to call at @ops functions
  4813. *
  4814. * This is used to unregister currently registered direct caller and
  4815. * register new one @addr on functions registered in @ops object.
  4816. *
  4817. * Note there's window between ftrace_shutdown and ftrace_startup calls
  4818. * where there will be no callbacks called.
  4819. *
  4820. * Returns: zero on success. Non zero on error, which includes:
  4821. * -EINVAL - The @ops object was not properly registered.
  4822. */
  4823. int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
  4824. {
  4825. int err;
  4826. if (check_direct_multi(ops))
  4827. return -EINVAL;
  4828. if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
  4829. return -EINVAL;
  4830. mutex_lock(&direct_mutex);
  4831. err = __modify_ftrace_direct_multi(ops, addr);
  4832. mutex_unlock(&direct_mutex);
  4833. return err;
  4834. }
  4835. EXPORT_SYMBOL_GPL(modify_ftrace_direct_multi);
  4836. #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
  4837. /**
  4838. * ftrace_set_filter_ip - set a function to filter on in ftrace by address
  4839. * @ops - the ops to set the filter with
  4840. * @ip - the address to add to or remove from the filter.
  4841. * @remove - non zero to remove the ip from the filter
  4842. * @reset - non zero to reset all filters before applying this filter.
  4843. *
  4844. * Filters denote which functions should be enabled when tracing is enabled
  4845. * If @ip is NULL, it fails to update filter.
  4846. *
  4847. * This can allocate memory which must be freed before @ops can be freed,
  4848. * either by removing each filtered addr or by using
  4849. * ftrace_free_filter(@ops).
  4850. */
  4851. int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
  4852. int remove, int reset)
  4853. {
  4854. ftrace_ops_init(ops);
  4855. return ftrace_set_addr(ops, &ip, 1, remove, reset, 1);
  4856. }
  4857. EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
  4858. /**
  4859. * ftrace_set_filter_ips - set functions to filter on in ftrace by addresses
  4860. * @ops - the ops to set the filter with
  4861. * @ips - the array of addresses to add to or remove from the filter.
  4862. * @cnt - the number of addresses in @ips
  4863. * @remove - non zero to remove ips from the filter
  4864. * @reset - non zero to reset all filters before applying this filter.
  4865. *
  4866. * Filters denote which functions should be enabled when tracing is enabled
  4867. * If @ips array or any ip specified within is NULL , it fails to update filter.
  4868. *
  4869. * This can allocate memory which must be freed before @ops can be freed,
  4870. * either by removing each filtered addr or by using
  4871. * ftrace_free_filter(@ops).
  4872. */
  4873. int ftrace_set_filter_ips(struct ftrace_ops *ops, unsigned long *ips,
  4874. unsigned int cnt, int remove, int reset)
  4875. {
  4876. ftrace_ops_init(ops);
  4877. return ftrace_set_addr(ops, ips, cnt, remove, reset, 1);
  4878. }
  4879. EXPORT_SYMBOL_GPL(ftrace_set_filter_ips);
  4880. /**
  4881. * ftrace_ops_set_global_filter - setup ops to use global filters
  4882. * @ops - the ops which will use the global filters
  4883. *
  4884. * ftrace users who need global function trace filtering should call this.
  4885. * It can set the global filter only if ops were not initialized before.
  4886. */
  4887. void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
  4888. {
  4889. if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
  4890. return;
  4891. ftrace_ops_init(ops);
  4892. ops->func_hash = &global_ops.local_hash;
  4893. }
  4894. EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
  4895. static int
  4896. ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
  4897. int reset, int enable)
  4898. {
  4899. return ftrace_set_hash(ops, buf, len, NULL, 0, 0, reset, enable);
  4900. }
  4901. /**
  4902. * ftrace_set_filter - set a function to filter on in ftrace
  4903. * @ops - the ops to set the filter with
  4904. * @buf - the string that holds the function filter text.
  4905. * @len - the length of the string.
  4906. * @reset - non zero to reset all filters before applying this filter.
  4907. *
  4908. * Filters denote which functions should be enabled when tracing is enabled.
  4909. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  4910. *
  4911. * This can allocate memory which must be freed before @ops can be freed,
  4912. * either by removing each filtered addr or by using
  4913. * ftrace_free_filter(@ops).
  4914. */
  4915. int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
  4916. int len, int reset)
  4917. {
  4918. ftrace_ops_init(ops);
  4919. return ftrace_set_regex(ops, buf, len, reset, 1);
  4920. }
  4921. EXPORT_SYMBOL_GPL(ftrace_set_filter);
  4922. /**
  4923. * ftrace_set_notrace - set a function to not trace in ftrace
  4924. * @ops - the ops to set the notrace filter with
  4925. * @buf - the string that holds the function notrace text.
  4926. * @len - the length of the string.
  4927. * @reset - non zero to reset all filters before applying this filter.
  4928. *
  4929. * Notrace Filters denote which functions should not be enabled when tracing
  4930. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  4931. * for tracing.
  4932. *
  4933. * This can allocate memory which must be freed before @ops can be freed,
  4934. * either by removing each filtered addr or by using
  4935. * ftrace_free_filter(@ops).
  4936. */
  4937. int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
  4938. int len, int reset)
  4939. {
  4940. ftrace_ops_init(ops);
  4941. return ftrace_set_regex(ops, buf, len, reset, 0);
  4942. }
  4943. EXPORT_SYMBOL_GPL(ftrace_set_notrace);
  4944. /**
  4945. * ftrace_set_global_filter - set a function to filter on with global tracers
  4946. * @buf - the string that holds the function filter text.
  4947. * @len - the length of the string.
  4948. * @reset - non zero to reset all filters before applying this filter.
  4949. *
  4950. * Filters denote which functions should be enabled when tracing is enabled.
  4951. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  4952. */
  4953. void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
  4954. {
  4955. ftrace_set_regex(&global_ops, buf, len, reset, 1);
  4956. }
  4957. EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
  4958. /**
  4959. * ftrace_set_global_notrace - set a function to not trace with global tracers
  4960. * @buf - the string that holds the function notrace text.
  4961. * @len - the length of the string.
  4962. * @reset - non zero to reset all filters before applying this filter.
  4963. *
  4964. * Notrace Filters denote which functions should not be enabled when tracing
  4965. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  4966. * for tracing.
  4967. */
  4968. void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
  4969. {
  4970. ftrace_set_regex(&global_ops, buf, len, reset, 0);
  4971. }
  4972. EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
  4973. /*
  4974. * command line interface to allow users to set filters on boot up.
  4975. */
  4976. #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
  4977. static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
  4978. static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
  4979. /* Used by function selftest to not test if filter is set */
  4980. bool ftrace_filter_param __initdata;
  4981. static int __init set_ftrace_notrace(char *str)
  4982. {
  4983. ftrace_filter_param = true;
  4984. strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
  4985. return 1;
  4986. }
  4987. __setup("ftrace_notrace=", set_ftrace_notrace);
  4988. static int __init set_ftrace_filter(char *str)
  4989. {
  4990. ftrace_filter_param = true;
  4991. strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
  4992. return 1;
  4993. }
  4994. __setup("ftrace_filter=", set_ftrace_filter);
  4995. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  4996. static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
  4997. static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
  4998. static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
  4999. static int __init set_graph_function(char *str)
  5000. {
  5001. strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
  5002. return 1;
  5003. }
  5004. __setup("ftrace_graph_filter=", set_graph_function);
  5005. static int __init set_graph_notrace_function(char *str)
  5006. {
  5007. strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
  5008. return 1;
  5009. }
  5010. __setup("ftrace_graph_notrace=", set_graph_notrace_function);
  5011. static int __init set_graph_max_depth_function(char *str)
  5012. {
  5013. if (!str)
  5014. return 0;
  5015. fgraph_max_depth = simple_strtoul(str, NULL, 0);
  5016. return 1;
  5017. }
  5018. __setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
  5019. static void __init set_ftrace_early_graph(char *buf, int enable)
  5020. {
  5021. int ret;
  5022. char *func;
  5023. struct ftrace_hash *hash;
  5024. hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
  5025. if (MEM_FAIL(!hash, "Failed to allocate hash\n"))
  5026. return;
  5027. while (buf) {
  5028. func = strsep(&buf, ",");
  5029. /* we allow only one expression at a time */
  5030. ret = ftrace_graph_set_hash(hash, func);
  5031. if (ret)
  5032. printk(KERN_DEBUG "ftrace: function %s not "
  5033. "traceable\n", func);
  5034. }
  5035. if (enable)
  5036. ftrace_graph_hash = hash;
  5037. else
  5038. ftrace_graph_notrace_hash = hash;
  5039. }
  5040. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  5041. void __init
  5042. ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
  5043. {
  5044. char *func;
  5045. ftrace_ops_init(ops);
  5046. while (buf) {
  5047. func = strsep(&buf, ",");
  5048. ftrace_set_regex(ops, func, strlen(func), 0, enable);
  5049. }
  5050. }
  5051. static void __init set_ftrace_early_filters(void)
  5052. {
  5053. if (ftrace_filter_buf[0])
  5054. ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
  5055. if (ftrace_notrace_buf[0])
  5056. ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
  5057. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  5058. if (ftrace_graph_buf[0])
  5059. set_ftrace_early_graph(ftrace_graph_buf, 1);
  5060. if (ftrace_graph_notrace_buf[0])
  5061. set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
  5062. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  5063. }
  5064. int ftrace_regex_release(struct inode *inode, struct file *file)
  5065. {
  5066. struct seq_file *m = (struct seq_file *)file->private_data;
  5067. struct ftrace_iterator *iter;
  5068. struct ftrace_hash **orig_hash;
  5069. struct trace_parser *parser;
  5070. int filter_hash;
  5071. if (file->f_mode & FMODE_READ) {
  5072. iter = m->private;
  5073. seq_release(inode, file);
  5074. } else
  5075. iter = file->private_data;
  5076. parser = &iter->parser;
  5077. if (trace_parser_loaded(parser)) {
  5078. int enable = !(iter->flags & FTRACE_ITER_NOTRACE);
  5079. ftrace_process_regex(iter, parser->buffer,
  5080. parser->idx, enable);
  5081. }
  5082. trace_parser_put(parser);
  5083. mutex_lock(&iter->ops->func_hash->regex_lock);
  5084. if (file->f_mode & FMODE_WRITE) {
  5085. filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
  5086. if (filter_hash) {
  5087. orig_hash = &iter->ops->func_hash->filter_hash;
  5088. if (iter->tr) {
  5089. if (list_empty(&iter->tr->mod_trace))
  5090. iter->hash->flags &= ~FTRACE_HASH_FL_MOD;
  5091. else
  5092. iter->hash->flags |= FTRACE_HASH_FL_MOD;
  5093. }
  5094. } else
  5095. orig_hash = &iter->ops->func_hash->notrace_hash;
  5096. mutex_lock(&ftrace_lock);
  5097. ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
  5098. iter->hash, filter_hash);
  5099. mutex_unlock(&ftrace_lock);
  5100. } else {
  5101. /* For read only, the hash is the ops hash */
  5102. iter->hash = NULL;
  5103. }
  5104. mutex_unlock(&iter->ops->func_hash->regex_lock);
  5105. free_ftrace_hash(iter->hash);
  5106. if (iter->tr)
  5107. trace_array_put(iter->tr);
  5108. kfree(iter);
  5109. return 0;
  5110. }
  5111. static const struct file_operations ftrace_avail_fops = {
  5112. .open = ftrace_avail_open,
  5113. .read = seq_read,
  5114. .llseek = seq_lseek,
  5115. .release = seq_release_private,
  5116. };
  5117. static const struct file_operations ftrace_enabled_fops = {
  5118. .open = ftrace_enabled_open,
  5119. .read = seq_read,
  5120. .llseek = seq_lseek,
  5121. .release = seq_release_private,
  5122. };
  5123. static const struct file_operations ftrace_filter_fops = {
  5124. .open = ftrace_filter_open,
  5125. .read = seq_read,
  5126. .write = ftrace_filter_write,
  5127. .llseek = tracing_lseek,
  5128. .release = ftrace_regex_release,
  5129. };
  5130. static const struct file_operations ftrace_notrace_fops = {
  5131. .open = ftrace_notrace_open,
  5132. .read = seq_read,
  5133. .write = ftrace_notrace_write,
  5134. .llseek = tracing_lseek,
  5135. .release = ftrace_regex_release,
  5136. };
  5137. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  5138. static DEFINE_MUTEX(graph_lock);
  5139. struct ftrace_hash __rcu *ftrace_graph_hash = EMPTY_HASH;
  5140. struct ftrace_hash __rcu *ftrace_graph_notrace_hash = EMPTY_HASH;
  5141. enum graph_filter_type {
  5142. GRAPH_FILTER_NOTRACE = 0,
  5143. GRAPH_FILTER_FUNCTION,
  5144. };
  5145. #define FTRACE_GRAPH_EMPTY ((void *)1)
  5146. struct ftrace_graph_data {
  5147. struct ftrace_hash *hash;
  5148. struct ftrace_func_entry *entry;
  5149. int idx; /* for hash table iteration */
  5150. enum graph_filter_type type;
  5151. struct ftrace_hash *new_hash;
  5152. const struct seq_operations *seq_ops;
  5153. struct trace_parser parser;
  5154. };
  5155. static void *
  5156. __g_next(struct seq_file *m, loff_t *pos)
  5157. {
  5158. struct ftrace_graph_data *fgd = m->private;
  5159. struct ftrace_func_entry *entry = fgd->entry;
  5160. struct hlist_head *head;
  5161. int i, idx = fgd->idx;
  5162. if (*pos >= fgd->hash->count)
  5163. return NULL;
  5164. if (entry) {
  5165. hlist_for_each_entry_continue(entry, hlist) {
  5166. fgd->entry = entry;
  5167. return entry;
  5168. }
  5169. idx++;
  5170. }
  5171. for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
  5172. head = &fgd->hash->buckets[i];
  5173. hlist_for_each_entry(entry, head, hlist) {
  5174. fgd->entry = entry;
  5175. fgd->idx = i;
  5176. return entry;
  5177. }
  5178. }
  5179. return NULL;
  5180. }
  5181. static void *
  5182. g_next(struct seq_file *m, void *v, loff_t *pos)
  5183. {
  5184. (*pos)++;
  5185. return __g_next(m, pos);
  5186. }
  5187. static void *g_start(struct seq_file *m, loff_t *pos)
  5188. {
  5189. struct ftrace_graph_data *fgd = m->private;
  5190. mutex_lock(&graph_lock);
  5191. if (fgd->type == GRAPH_FILTER_FUNCTION)
  5192. fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
  5193. lockdep_is_held(&graph_lock));
  5194. else
  5195. fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
  5196. lockdep_is_held(&graph_lock));
  5197. /* Nothing, tell g_show to print all functions are enabled */
  5198. if (ftrace_hash_empty(fgd->hash) && !*pos)
  5199. return FTRACE_GRAPH_EMPTY;
  5200. fgd->idx = 0;
  5201. fgd->entry = NULL;
  5202. return __g_next(m, pos);
  5203. }
  5204. static void g_stop(struct seq_file *m, void *p)
  5205. {
  5206. mutex_unlock(&graph_lock);
  5207. }
  5208. static int g_show(struct seq_file *m, void *v)
  5209. {
  5210. struct ftrace_func_entry *entry = v;
  5211. if (!entry)
  5212. return 0;
  5213. if (entry == FTRACE_GRAPH_EMPTY) {
  5214. struct ftrace_graph_data *fgd = m->private;
  5215. if (fgd->type == GRAPH_FILTER_FUNCTION)
  5216. seq_puts(m, "#### all functions enabled ####\n");
  5217. else
  5218. seq_puts(m, "#### no functions disabled ####\n");
  5219. return 0;
  5220. }
  5221. seq_printf(m, "%ps\n", (void *)entry->ip);
  5222. return 0;
  5223. }
  5224. static const struct seq_operations ftrace_graph_seq_ops = {
  5225. .start = g_start,
  5226. .next = g_next,
  5227. .stop = g_stop,
  5228. .show = g_show,
  5229. };
  5230. static int
  5231. __ftrace_graph_open(struct inode *inode, struct file *file,
  5232. struct ftrace_graph_data *fgd)
  5233. {
  5234. int ret;
  5235. struct ftrace_hash *new_hash = NULL;
  5236. ret = security_locked_down(LOCKDOWN_TRACEFS);
  5237. if (ret)
  5238. return ret;
  5239. if (file->f_mode & FMODE_WRITE) {
  5240. const int size_bits = FTRACE_HASH_DEFAULT_BITS;
  5241. if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
  5242. return -ENOMEM;
  5243. if (file->f_flags & O_TRUNC)
  5244. new_hash = alloc_ftrace_hash(size_bits);
  5245. else
  5246. new_hash = alloc_and_copy_ftrace_hash(size_bits,
  5247. fgd->hash);
  5248. if (!new_hash) {
  5249. ret = -ENOMEM;
  5250. goto out;
  5251. }
  5252. }
  5253. if (file->f_mode & FMODE_READ) {
  5254. ret = seq_open(file, &ftrace_graph_seq_ops);
  5255. if (!ret) {
  5256. struct seq_file *m = file->private_data;
  5257. m->private = fgd;
  5258. } else {
  5259. /* Failed */
  5260. free_ftrace_hash(new_hash);
  5261. new_hash = NULL;
  5262. }
  5263. } else
  5264. file->private_data = fgd;
  5265. out:
  5266. if (ret < 0 && file->f_mode & FMODE_WRITE)
  5267. trace_parser_put(&fgd->parser);
  5268. fgd->new_hash = new_hash;
  5269. /*
  5270. * All uses of fgd->hash must be taken with the graph_lock
  5271. * held. The graph_lock is going to be released, so force
  5272. * fgd->hash to be reinitialized when it is taken again.
  5273. */
  5274. fgd->hash = NULL;
  5275. return ret;
  5276. }
  5277. static int
  5278. ftrace_graph_open(struct inode *inode, struct file *file)
  5279. {
  5280. struct ftrace_graph_data *fgd;
  5281. int ret;
  5282. if (unlikely(ftrace_disabled))
  5283. return -ENODEV;
  5284. fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
  5285. if (fgd == NULL)
  5286. return -ENOMEM;
  5287. mutex_lock(&graph_lock);
  5288. fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
  5289. lockdep_is_held(&graph_lock));
  5290. fgd->type = GRAPH_FILTER_FUNCTION;
  5291. fgd->seq_ops = &ftrace_graph_seq_ops;
  5292. ret = __ftrace_graph_open(inode, file, fgd);
  5293. if (ret < 0)
  5294. kfree(fgd);
  5295. mutex_unlock(&graph_lock);
  5296. return ret;
  5297. }
  5298. static int
  5299. ftrace_graph_notrace_open(struct inode *inode, struct file *file)
  5300. {
  5301. struct ftrace_graph_data *fgd;
  5302. int ret;
  5303. if (unlikely(ftrace_disabled))
  5304. return -ENODEV;
  5305. fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
  5306. if (fgd == NULL)
  5307. return -ENOMEM;
  5308. mutex_lock(&graph_lock);
  5309. fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
  5310. lockdep_is_held(&graph_lock));
  5311. fgd->type = GRAPH_FILTER_NOTRACE;
  5312. fgd->seq_ops = &ftrace_graph_seq_ops;
  5313. ret = __ftrace_graph_open(inode, file, fgd);
  5314. if (ret < 0)
  5315. kfree(fgd);
  5316. mutex_unlock(&graph_lock);
  5317. return ret;
  5318. }
  5319. static int
  5320. ftrace_graph_release(struct inode *inode, struct file *file)
  5321. {
  5322. struct ftrace_graph_data *fgd;
  5323. struct ftrace_hash *old_hash, *new_hash;
  5324. struct trace_parser *parser;
  5325. int ret = 0;
  5326. if (file->f_mode & FMODE_READ) {
  5327. struct seq_file *m = file->private_data;
  5328. fgd = m->private;
  5329. seq_release(inode, file);
  5330. } else {
  5331. fgd = file->private_data;
  5332. }
  5333. if (file->f_mode & FMODE_WRITE) {
  5334. parser = &fgd->parser;
  5335. if (trace_parser_loaded((parser))) {
  5336. ret = ftrace_graph_set_hash(fgd->new_hash,
  5337. parser->buffer);
  5338. }
  5339. trace_parser_put(parser);
  5340. new_hash = __ftrace_hash_move(fgd->new_hash);
  5341. if (!new_hash) {
  5342. ret = -ENOMEM;
  5343. goto out;
  5344. }
  5345. mutex_lock(&graph_lock);
  5346. if (fgd->type == GRAPH_FILTER_FUNCTION) {
  5347. old_hash = rcu_dereference_protected(ftrace_graph_hash,
  5348. lockdep_is_held(&graph_lock));
  5349. rcu_assign_pointer(ftrace_graph_hash, new_hash);
  5350. } else {
  5351. old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
  5352. lockdep_is_held(&graph_lock));
  5353. rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
  5354. }
  5355. mutex_unlock(&graph_lock);
  5356. /*
  5357. * We need to do a hard force of sched synchronization.
  5358. * This is because we use preempt_disable() to do RCU, but
  5359. * the function tracers can be called where RCU is not watching
  5360. * (like before user_exit()). We can not rely on the RCU
  5361. * infrastructure to do the synchronization, thus we must do it
  5362. * ourselves.
  5363. */
  5364. if (old_hash != EMPTY_HASH)
  5365. synchronize_rcu_tasks_rude();
  5366. free_ftrace_hash(old_hash);
  5367. }
  5368. out:
  5369. free_ftrace_hash(fgd->new_hash);
  5370. kfree(fgd);
  5371. return ret;
  5372. }
  5373. static int
  5374. ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
  5375. {
  5376. struct ftrace_glob func_g;
  5377. struct dyn_ftrace *rec;
  5378. struct ftrace_page *pg;
  5379. struct ftrace_func_entry *entry;
  5380. int fail = 1;
  5381. int not;
  5382. /* decode regex */
  5383. func_g.type = filter_parse_regex(buffer, strlen(buffer),
  5384. &func_g.search, &not);
  5385. func_g.len = strlen(func_g.search);
  5386. mutex_lock(&ftrace_lock);
  5387. if (unlikely(ftrace_disabled)) {
  5388. mutex_unlock(&ftrace_lock);
  5389. return -ENODEV;
  5390. }
  5391. do_for_each_ftrace_rec(pg, rec) {
  5392. if (rec->flags & FTRACE_FL_DISABLED)
  5393. continue;
  5394. if (ftrace_match_record(rec, &func_g, NULL, 0)) {
  5395. entry = ftrace_lookup_ip(hash, rec->ip);
  5396. if (!not) {
  5397. fail = 0;
  5398. if (entry)
  5399. continue;
  5400. if (add_hash_entry(hash, rec->ip) < 0)
  5401. goto out;
  5402. } else {
  5403. if (entry) {
  5404. free_hash_entry(hash, entry);
  5405. fail = 0;
  5406. }
  5407. }
  5408. }
  5409. } while_for_each_ftrace_rec();
  5410. out:
  5411. mutex_unlock(&ftrace_lock);
  5412. if (fail)
  5413. return -EINVAL;
  5414. return 0;
  5415. }
  5416. static ssize_t
  5417. ftrace_graph_write(struct file *file, const char __user *ubuf,
  5418. size_t cnt, loff_t *ppos)
  5419. {
  5420. ssize_t read, ret = 0;
  5421. struct ftrace_graph_data *fgd = file->private_data;
  5422. struct trace_parser *parser;
  5423. if (!cnt)
  5424. return 0;
  5425. /* Read mode uses seq functions */
  5426. if (file->f_mode & FMODE_READ) {
  5427. struct seq_file *m = file->private_data;
  5428. fgd = m->private;
  5429. }
  5430. parser = &fgd->parser;
  5431. read = trace_get_user(parser, ubuf, cnt, ppos);
  5432. if (read >= 0 && trace_parser_loaded(parser) &&
  5433. !trace_parser_cont(parser)) {
  5434. ret = ftrace_graph_set_hash(fgd->new_hash,
  5435. parser->buffer);
  5436. trace_parser_clear(parser);
  5437. }
  5438. if (!ret)
  5439. ret = read;
  5440. return ret;
  5441. }
  5442. static const struct file_operations ftrace_graph_fops = {
  5443. .open = ftrace_graph_open,
  5444. .read = seq_read,
  5445. .write = ftrace_graph_write,
  5446. .llseek = tracing_lseek,
  5447. .release = ftrace_graph_release,
  5448. };
  5449. static const struct file_operations ftrace_graph_notrace_fops = {
  5450. .open = ftrace_graph_notrace_open,
  5451. .read = seq_read,
  5452. .write = ftrace_graph_write,
  5453. .llseek = tracing_lseek,
  5454. .release = ftrace_graph_release,
  5455. };
  5456. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  5457. void ftrace_create_filter_files(struct ftrace_ops *ops,
  5458. struct dentry *parent)
  5459. {
  5460. trace_create_file("set_ftrace_filter", TRACE_MODE_WRITE, parent,
  5461. ops, &ftrace_filter_fops);
  5462. trace_create_file("set_ftrace_notrace", TRACE_MODE_WRITE, parent,
  5463. ops, &ftrace_notrace_fops);
  5464. }
  5465. /*
  5466. * The name "destroy_filter_files" is really a misnomer. Although
  5467. * in the future, it may actually delete the files, but this is
  5468. * really intended to make sure the ops passed in are disabled
  5469. * and that when this function returns, the caller is free to
  5470. * free the ops.
  5471. *
  5472. * The "destroy" name is only to match the "create" name that this
  5473. * should be paired with.
  5474. */
  5475. void ftrace_destroy_filter_files(struct ftrace_ops *ops)
  5476. {
  5477. mutex_lock(&ftrace_lock);
  5478. if (ops->flags & FTRACE_OPS_FL_ENABLED)
  5479. ftrace_shutdown(ops, 0);
  5480. ops->flags |= FTRACE_OPS_FL_DELETED;
  5481. ftrace_free_filter(ops);
  5482. mutex_unlock(&ftrace_lock);
  5483. }
  5484. static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
  5485. {
  5486. trace_create_file("available_filter_functions", TRACE_MODE_READ,
  5487. d_tracer, NULL, &ftrace_avail_fops);
  5488. trace_create_file("enabled_functions", TRACE_MODE_READ,
  5489. d_tracer, NULL, &ftrace_enabled_fops);
  5490. ftrace_create_filter_files(&global_ops, d_tracer);
  5491. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  5492. trace_create_file("set_graph_function", TRACE_MODE_WRITE, d_tracer,
  5493. NULL,
  5494. &ftrace_graph_fops);
  5495. trace_create_file("set_graph_notrace", TRACE_MODE_WRITE, d_tracer,
  5496. NULL,
  5497. &ftrace_graph_notrace_fops);
  5498. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  5499. return 0;
  5500. }
  5501. static int ftrace_cmp_ips(const void *a, const void *b)
  5502. {
  5503. const unsigned long *ipa = a;
  5504. const unsigned long *ipb = b;
  5505. if (*ipa > *ipb)
  5506. return 1;
  5507. if (*ipa < *ipb)
  5508. return -1;
  5509. return 0;
  5510. }
  5511. #ifdef CONFIG_FTRACE_SORT_STARTUP_TEST
  5512. static void test_is_sorted(unsigned long *start, unsigned long count)
  5513. {
  5514. int i;
  5515. for (i = 1; i < count; i++) {
  5516. if (WARN(start[i - 1] > start[i],
  5517. "[%d] %pS at %lx is not sorted with %pS at %lx\n", i,
  5518. (void *)start[i - 1], start[i - 1],
  5519. (void *)start[i], start[i]))
  5520. break;
  5521. }
  5522. if (i == count)
  5523. pr_info("ftrace section at %px sorted properly\n", start);
  5524. }
  5525. #else
  5526. static void test_is_sorted(unsigned long *start, unsigned long count)
  5527. {
  5528. }
  5529. #endif
  5530. static int ftrace_process_locs(struct module *mod,
  5531. unsigned long *start,
  5532. unsigned long *end)
  5533. {
  5534. struct ftrace_page *pg_unuse = NULL;
  5535. struct ftrace_page *start_pg;
  5536. struct ftrace_page *pg;
  5537. struct dyn_ftrace *rec;
  5538. unsigned long skipped = 0;
  5539. unsigned long count;
  5540. unsigned long *p;
  5541. unsigned long addr;
  5542. unsigned long flags = 0; /* Shut up gcc */
  5543. int ret = -ENOMEM;
  5544. count = end - start;
  5545. if (!count)
  5546. return 0;
  5547. /*
  5548. * Sorting mcount in vmlinux at build time depend on
  5549. * CONFIG_BUILDTIME_MCOUNT_SORT, while mcount loc in
  5550. * modules can not be sorted at build time.
  5551. */
  5552. if (!IS_ENABLED(CONFIG_BUILDTIME_MCOUNT_SORT) || mod) {
  5553. sort(start, count, sizeof(*start),
  5554. ftrace_cmp_ips, NULL);
  5555. } else {
  5556. test_is_sorted(start, count);
  5557. }
  5558. start_pg = ftrace_allocate_pages(count);
  5559. if (!start_pg)
  5560. return -ENOMEM;
  5561. mutex_lock(&ftrace_lock);
  5562. /*
  5563. * Core and each module needs their own pages, as
  5564. * modules will free them when they are removed.
  5565. * Force a new page to be allocated for modules.
  5566. */
  5567. if (!mod) {
  5568. WARN_ON(ftrace_pages || ftrace_pages_start);
  5569. /* First initialization */
  5570. ftrace_pages = ftrace_pages_start = start_pg;
  5571. } else {
  5572. if (!ftrace_pages)
  5573. goto out;
  5574. if (WARN_ON(ftrace_pages->next)) {
  5575. /* Hmm, we have free pages? */
  5576. while (ftrace_pages->next)
  5577. ftrace_pages = ftrace_pages->next;
  5578. }
  5579. ftrace_pages->next = start_pg;
  5580. }
  5581. p = start;
  5582. pg = start_pg;
  5583. while (p < end) {
  5584. unsigned long end_offset;
  5585. addr = ftrace_call_adjust(*p++);
  5586. /*
  5587. * Some architecture linkers will pad between
  5588. * the different mcount_loc sections of different
  5589. * object files to satisfy alignments.
  5590. * Skip any NULL pointers.
  5591. */
  5592. if (!addr) {
  5593. skipped++;
  5594. continue;
  5595. }
  5596. end_offset = (pg->index+1) * sizeof(pg->records[0]);
  5597. if (end_offset > PAGE_SIZE << pg->order) {
  5598. /* We should have allocated enough */
  5599. if (WARN_ON(!pg->next))
  5600. break;
  5601. pg = pg->next;
  5602. }
  5603. rec = &pg->records[pg->index++];
  5604. rec->ip = addr;
  5605. }
  5606. if (pg->next) {
  5607. pg_unuse = pg->next;
  5608. pg->next = NULL;
  5609. }
  5610. /* Assign the last page to ftrace_pages */
  5611. ftrace_pages = pg;
  5612. /*
  5613. * We only need to disable interrupts on start up
  5614. * because we are modifying code that an interrupt
  5615. * may execute, and the modification is not atomic.
  5616. * But for modules, nothing runs the code we modify
  5617. * until we are finished with it, and there's no
  5618. * reason to cause large interrupt latencies while we do it.
  5619. */
  5620. if (!mod)
  5621. local_irq_save(flags);
  5622. ftrace_update_code(mod, start_pg);
  5623. if (!mod)
  5624. local_irq_restore(flags);
  5625. ret = 0;
  5626. out:
  5627. mutex_unlock(&ftrace_lock);
  5628. /* We should have used all pages unless we skipped some */
  5629. if (pg_unuse) {
  5630. WARN_ON(!skipped);
  5631. ftrace_free_pages(pg_unuse);
  5632. }
  5633. return ret;
  5634. }
  5635. struct ftrace_mod_func {
  5636. struct list_head list;
  5637. char *name;
  5638. unsigned long ip;
  5639. unsigned int size;
  5640. };
  5641. struct ftrace_mod_map {
  5642. struct rcu_head rcu;
  5643. struct list_head list;
  5644. struct module *mod;
  5645. unsigned long start_addr;
  5646. unsigned long end_addr;
  5647. struct list_head funcs;
  5648. unsigned int num_funcs;
  5649. };
  5650. static int ftrace_get_trampoline_kallsym(unsigned int symnum,
  5651. unsigned long *value, char *type,
  5652. char *name, char *module_name,
  5653. int *exported)
  5654. {
  5655. struct ftrace_ops *op;
  5656. list_for_each_entry_rcu(op, &ftrace_ops_trampoline_list, list) {
  5657. if (!op->trampoline || symnum--)
  5658. continue;
  5659. *value = op->trampoline;
  5660. *type = 't';
  5661. strlcpy(name, FTRACE_TRAMPOLINE_SYM, KSYM_NAME_LEN);
  5662. strlcpy(module_name, FTRACE_TRAMPOLINE_MOD, MODULE_NAME_LEN);
  5663. *exported = 0;
  5664. return 0;
  5665. }
  5666. return -ERANGE;
  5667. }
  5668. #if defined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS) || defined(CONFIG_MODULES)
  5669. /*
  5670. * Check if the current ops references the given ip.
  5671. *
  5672. * If the ops traces all functions, then it was already accounted for.
  5673. * If the ops does not trace the current record function, skip it.
  5674. * If the ops ignores the function via notrace filter, skip it.
  5675. */
  5676. static bool
  5677. ops_references_ip(struct ftrace_ops *ops, unsigned long ip)
  5678. {
  5679. /* If ops isn't enabled, ignore it */
  5680. if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
  5681. return false;
  5682. /* If ops traces all then it includes this function */
  5683. if (ops_traces_mod(ops))
  5684. return true;
  5685. /* The function must be in the filter */
  5686. if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
  5687. !__ftrace_lookup_ip(ops->func_hash->filter_hash, ip))
  5688. return false;
  5689. /* If in notrace hash, we ignore it too */
  5690. if (ftrace_lookup_ip(ops->func_hash->notrace_hash, ip))
  5691. return false;
  5692. return true;
  5693. }
  5694. #endif
  5695. #ifdef CONFIG_MODULES
  5696. #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
  5697. static LIST_HEAD(ftrace_mod_maps);
  5698. static int referenced_filters(struct dyn_ftrace *rec)
  5699. {
  5700. struct ftrace_ops *ops;
  5701. int cnt = 0;
  5702. for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
  5703. if (ops_references_ip(ops, rec->ip)) {
  5704. if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_DIRECT))
  5705. continue;
  5706. if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_IPMODIFY))
  5707. continue;
  5708. cnt++;
  5709. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
  5710. rec->flags |= FTRACE_FL_REGS;
  5711. if (cnt == 1 && ops->trampoline)
  5712. rec->flags |= FTRACE_FL_TRAMP;
  5713. else
  5714. rec->flags &= ~FTRACE_FL_TRAMP;
  5715. }
  5716. }
  5717. return cnt;
  5718. }
  5719. static void
  5720. clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
  5721. {
  5722. struct ftrace_func_entry *entry;
  5723. struct dyn_ftrace *rec;
  5724. int i;
  5725. if (ftrace_hash_empty(hash))
  5726. return;
  5727. for (i = 0; i < pg->index; i++) {
  5728. rec = &pg->records[i];
  5729. entry = __ftrace_lookup_ip(hash, rec->ip);
  5730. /*
  5731. * Do not allow this rec to match again.
  5732. * Yeah, it may waste some memory, but will be removed
  5733. * if/when the hash is modified again.
  5734. */
  5735. if (entry)
  5736. entry->ip = 0;
  5737. }
  5738. }
  5739. /* Clear any records from hashes */
  5740. static void clear_mod_from_hashes(struct ftrace_page *pg)
  5741. {
  5742. struct trace_array *tr;
  5743. mutex_lock(&trace_types_lock);
  5744. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  5745. if (!tr->ops || !tr->ops->func_hash)
  5746. continue;
  5747. mutex_lock(&tr->ops->func_hash->regex_lock);
  5748. clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
  5749. clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
  5750. mutex_unlock(&tr->ops->func_hash->regex_lock);
  5751. }
  5752. mutex_unlock(&trace_types_lock);
  5753. }
  5754. static void ftrace_free_mod_map(struct rcu_head *rcu)
  5755. {
  5756. struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
  5757. struct ftrace_mod_func *mod_func;
  5758. struct ftrace_mod_func *n;
  5759. /* All the contents of mod_map are now not visible to readers */
  5760. list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
  5761. kfree(mod_func->name);
  5762. list_del(&mod_func->list);
  5763. kfree(mod_func);
  5764. }
  5765. kfree(mod_map);
  5766. }
  5767. void ftrace_release_mod(struct module *mod)
  5768. {
  5769. struct ftrace_mod_map *mod_map;
  5770. struct ftrace_mod_map *n;
  5771. struct dyn_ftrace *rec;
  5772. struct ftrace_page **last_pg;
  5773. struct ftrace_page *tmp_page = NULL;
  5774. struct ftrace_page *pg;
  5775. mutex_lock(&ftrace_lock);
  5776. if (ftrace_disabled)
  5777. goto out_unlock;
  5778. list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
  5779. if (mod_map->mod == mod) {
  5780. list_del_rcu(&mod_map->list);
  5781. call_rcu(&mod_map->rcu, ftrace_free_mod_map);
  5782. break;
  5783. }
  5784. }
  5785. /*
  5786. * Each module has its own ftrace_pages, remove
  5787. * them from the list.
  5788. */
  5789. last_pg = &ftrace_pages_start;
  5790. for (pg = ftrace_pages_start; pg; pg = *last_pg) {
  5791. rec = &pg->records[0];
  5792. if (within_module_core(rec->ip, mod) ||
  5793. within_module_init(rec->ip, mod)) {
  5794. /*
  5795. * As core pages are first, the first
  5796. * page should never be a module page.
  5797. */
  5798. if (WARN_ON(pg == ftrace_pages_start))
  5799. goto out_unlock;
  5800. /* Check if we are deleting the last page */
  5801. if (pg == ftrace_pages)
  5802. ftrace_pages = next_to_ftrace_page(last_pg);
  5803. ftrace_update_tot_cnt -= pg->index;
  5804. *last_pg = pg->next;
  5805. pg->next = tmp_page;
  5806. tmp_page = pg;
  5807. } else
  5808. last_pg = &pg->next;
  5809. }
  5810. out_unlock:
  5811. mutex_unlock(&ftrace_lock);
  5812. for (pg = tmp_page; pg; pg = tmp_page) {
  5813. /* Needs to be called outside of ftrace_lock */
  5814. clear_mod_from_hashes(pg);
  5815. if (pg->records) {
  5816. free_pages((unsigned long)pg->records, pg->order);
  5817. ftrace_number_of_pages -= 1 << pg->order;
  5818. }
  5819. tmp_page = pg->next;
  5820. kfree(pg);
  5821. ftrace_number_of_groups--;
  5822. }
  5823. }
  5824. void ftrace_module_enable(struct module *mod)
  5825. {
  5826. struct dyn_ftrace *rec;
  5827. struct ftrace_page *pg;
  5828. mutex_lock(&ftrace_lock);
  5829. if (ftrace_disabled)
  5830. goto out_unlock;
  5831. /*
  5832. * If the tracing is enabled, go ahead and enable the record.
  5833. *
  5834. * The reason not to enable the record immediately is the
  5835. * inherent check of ftrace_make_nop/ftrace_make_call for
  5836. * correct previous instructions. Making first the NOP
  5837. * conversion puts the module to the correct state, thus
  5838. * passing the ftrace_make_call check.
  5839. *
  5840. * We also delay this to after the module code already set the
  5841. * text to read-only, as we now need to set it back to read-write
  5842. * so that we can modify the text.
  5843. */
  5844. if (ftrace_start_up)
  5845. ftrace_arch_code_modify_prepare();
  5846. do_for_each_ftrace_rec(pg, rec) {
  5847. int cnt;
  5848. /*
  5849. * do_for_each_ftrace_rec() is a double loop.
  5850. * module text shares the pg. If a record is
  5851. * not part of this module, then skip this pg,
  5852. * which the "break" will do.
  5853. */
  5854. if (!within_module_core(rec->ip, mod) &&
  5855. !within_module_init(rec->ip, mod))
  5856. break;
  5857. /* Weak functions should still be ignored */
  5858. if (!test_for_valid_rec(rec)) {
  5859. /* Clear all other flags. Should not be enabled anyway */
  5860. rec->flags = FTRACE_FL_DISABLED;
  5861. continue;
  5862. }
  5863. cnt = 0;
  5864. /*
  5865. * When adding a module, we need to check if tracers are
  5866. * currently enabled and if they are, and can trace this record,
  5867. * we need to enable the module functions as well as update the
  5868. * reference counts for those function records.
  5869. */
  5870. if (ftrace_start_up)
  5871. cnt += referenced_filters(rec);
  5872. rec->flags &= ~FTRACE_FL_DISABLED;
  5873. rec->flags += cnt;
  5874. if (ftrace_start_up && cnt) {
  5875. int failed = __ftrace_replace_code(rec, 1);
  5876. if (failed) {
  5877. ftrace_bug(failed, rec);
  5878. goto out_loop;
  5879. }
  5880. }
  5881. } while_for_each_ftrace_rec();
  5882. out_loop:
  5883. if (ftrace_start_up)
  5884. ftrace_arch_code_modify_post_process();
  5885. out_unlock:
  5886. mutex_unlock(&ftrace_lock);
  5887. process_cached_mods(mod->name);
  5888. }
  5889. void ftrace_module_init(struct module *mod)
  5890. {
  5891. int ret;
  5892. if (ftrace_disabled || !mod->num_ftrace_callsites)
  5893. return;
  5894. ret = ftrace_process_locs(mod, mod->ftrace_callsites,
  5895. mod->ftrace_callsites + mod->num_ftrace_callsites);
  5896. if (ret)
  5897. pr_warn("ftrace: failed to allocate entries for module '%s' functions\n",
  5898. mod->name);
  5899. }
  5900. static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
  5901. struct dyn_ftrace *rec)
  5902. {
  5903. struct ftrace_mod_func *mod_func;
  5904. unsigned long symsize;
  5905. unsigned long offset;
  5906. char str[KSYM_SYMBOL_LEN];
  5907. char *modname;
  5908. const char *ret;
  5909. ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
  5910. if (!ret)
  5911. return;
  5912. mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
  5913. if (!mod_func)
  5914. return;
  5915. mod_func->name = kstrdup(str, GFP_KERNEL);
  5916. if (!mod_func->name) {
  5917. kfree(mod_func);
  5918. return;
  5919. }
  5920. mod_func->ip = rec->ip - offset;
  5921. mod_func->size = symsize;
  5922. mod_map->num_funcs++;
  5923. list_add_rcu(&mod_func->list, &mod_map->funcs);
  5924. }
  5925. static struct ftrace_mod_map *
  5926. allocate_ftrace_mod_map(struct module *mod,
  5927. unsigned long start, unsigned long end)
  5928. {
  5929. struct ftrace_mod_map *mod_map;
  5930. mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
  5931. if (!mod_map)
  5932. return NULL;
  5933. mod_map->mod = mod;
  5934. mod_map->start_addr = start;
  5935. mod_map->end_addr = end;
  5936. mod_map->num_funcs = 0;
  5937. INIT_LIST_HEAD_RCU(&mod_map->funcs);
  5938. list_add_rcu(&mod_map->list, &ftrace_mod_maps);
  5939. return mod_map;
  5940. }
  5941. static const char *
  5942. ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
  5943. unsigned long addr, unsigned long *size,
  5944. unsigned long *off, char *sym)
  5945. {
  5946. struct ftrace_mod_func *found_func = NULL;
  5947. struct ftrace_mod_func *mod_func;
  5948. list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
  5949. if (addr >= mod_func->ip &&
  5950. addr < mod_func->ip + mod_func->size) {
  5951. found_func = mod_func;
  5952. break;
  5953. }
  5954. }
  5955. if (found_func) {
  5956. if (size)
  5957. *size = found_func->size;
  5958. if (off)
  5959. *off = addr - found_func->ip;
  5960. if (sym)
  5961. strlcpy(sym, found_func->name, KSYM_NAME_LEN);
  5962. return found_func->name;
  5963. }
  5964. return NULL;
  5965. }
  5966. const char *
  5967. ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
  5968. unsigned long *off, char **modname, char *sym)
  5969. {
  5970. struct ftrace_mod_map *mod_map;
  5971. const char *ret = NULL;
  5972. /* mod_map is freed via call_rcu() */
  5973. preempt_disable();
  5974. list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
  5975. ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
  5976. if (ret) {
  5977. if (modname)
  5978. *modname = mod_map->mod->name;
  5979. break;
  5980. }
  5981. }
  5982. preempt_enable();
  5983. return ret;
  5984. }
  5985. int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
  5986. char *type, char *name,
  5987. char *module_name, int *exported)
  5988. {
  5989. struct ftrace_mod_map *mod_map;
  5990. struct ftrace_mod_func *mod_func;
  5991. int ret;
  5992. preempt_disable();
  5993. list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
  5994. if (symnum >= mod_map->num_funcs) {
  5995. symnum -= mod_map->num_funcs;
  5996. continue;
  5997. }
  5998. list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
  5999. if (symnum > 1) {
  6000. symnum--;
  6001. continue;
  6002. }
  6003. *value = mod_func->ip;
  6004. *type = 'T';
  6005. strlcpy(name, mod_func->name, KSYM_NAME_LEN);
  6006. strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
  6007. *exported = 1;
  6008. preempt_enable();
  6009. return 0;
  6010. }
  6011. WARN_ON(1);
  6012. break;
  6013. }
  6014. ret = ftrace_get_trampoline_kallsym(symnum, value, type, name,
  6015. module_name, exported);
  6016. preempt_enable();
  6017. return ret;
  6018. }
  6019. #else
  6020. static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
  6021. struct dyn_ftrace *rec) { }
  6022. static inline struct ftrace_mod_map *
  6023. allocate_ftrace_mod_map(struct module *mod,
  6024. unsigned long start, unsigned long end)
  6025. {
  6026. return NULL;
  6027. }
  6028. int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
  6029. char *type, char *name, char *module_name,
  6030. int *exported)
  6031. {
  6032. int ret;
  6033. preempt_disable();
  6034. ret = ftrace_get_trampoline_kallsym(symnum, value, type, name,
  6035. module_name, exported);
  6036. preempt_enable();
  6037. return ret;
  6038. }
  6039. #endif /* CONFIG_MODULES */
  6040. struct ftrace_init_func {
  6041. struct list_head list;
  6042. unsigned long ip;
  6043. };
  6044. /* Clear any init ips from hashes */
  6045. static void
  6046. clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
  6047. {
  6048. struct ftrace_func_entry *entry;
  6049. entry = ftrace_lookup_ip(hash, func->ip);
  6050. /*
  6051. * Do not allow this rec to match again.
  6052. * Yeah, it may waste some memory, but will be removed
  6053. * if/when the hash is modified again.
  6054. */
  6055. if (entry)
  6056. entry->ip = 0;
  6057. }
  6058. static void
  6059. clear_func_from_hashes(struct ftrace_init_func *func)
  6060. {
  6061. struct trace_array *tr;
  6062. mutex_lock(&trace_types_lock);
  6063. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  6064. if (!tr->ops || !tr->ops->func_hash)
  6065. continue;
  6066. mutex_lock(&tr->ops->func_hash->regex_lock);
  6067. clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
  6068. clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
  6069. mutex_unlock(&tr->ops->func_hash->regex_lock);
  6070. }
  6071. mutex_unlock(&trace_types_lock);
  6072. }
  6073. static void add_to_clear_hash_list(struct list_head *clear_list,
  6074. struct dyn_ftrace *rec)
  6075. {
  6076. struct ftrace_init_func *func;
  6077. func = kmalloc(sizeof(*func), GFP_KERNEL);
  6078. if (!func) {
  6079. MEM_FAIL(1, "alloc failure, ftrace filter could be stale\n");
  6080. return;
  6081. }
  6082. func->ip = rec->ip;
  6083. list_add(&func->list, clear_list);
  6084. }
  6085. void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
  6086. {
  6087. unsigned long start = (unsigned long)(start_ptr);
  6088. unsigned long end = (unsigned long)(end_ptr);
  6089. struct ftrace_page **last_pg = &ftrace_pages_start;
  6090. struct ftrace_page *pg;
  6091. struct dyn_ftrace *rec;
  6092. struct dyn_ftrace key;
  6093. struct ftrace_mod_map *mod_map = NULL;
  6094. struct ftrace_init_func *func, *func_next;
  6095. struct list_head clear_hash;
  6096. INIT_LIST_HEAD(&clear_hash);
  6097. key.ip = start;
  6098. key.flags = end; /* overload flags, as it is unsigned long */
  6099. mutex_lock(&ftrace_lock);
  6100. /*
  6101. * If we are freeing module init memory, then check if
  6102. * any tracer is active. If so, we need to save a mapping of
  6103. * the module functions being freed with the address.
  6104. */
  6105. if (mod && ftrace_ops_list != &ftrace_list_end)
  6106. mod_map = allocate_ftrace_mod_map(mod, start, end);
  6107. for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
  6108. if (end < pg->records[0].ip ||
  6109. start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
  6110. continue;
  6111. again:
  6112. rec = bsearch(&key, pg->records, pg->index,
  6113. sizeof(struct dyn_ftrace),
  6114. ftrace_cmp_recs);
  6115. if (!rec)
  6116. continue;
  6117. /* rec will be cleared from hashes after ftrace_lock unlock */
  6118. add_to_clear_hash_list(&clear_hash, rec);
  6119. if (mod_map)
  6120. save_ftrace_mod_rec(mod_map, rec);
  6121. pg->index--;
  6122. ftrace_update_tot_cnt--;
  6123. if (!pg->index) {
  6124. *last_pg = pg->next;
  6125. if (pg->records) {
  6126. free_pages((unsigned long)pg->records, pg->order);
  6127. ftrace_number_of_pages -= 1 << pg->order;
  6128. }
  6129. ftrace_number_of_groups--;
  6130. kfree(pg);
  6131. pg = container_of(last_pg, struct ftrace_page, next);
  6132. if (!(*last_pg))
  6133. ftrace_pages = pg;
  6134. continue;
  6135. }
  6136. memmove(rec, rec + 1,
  6137. (pg->index - (rec - pg->records)) * sizeof(*rec));
  6138. /* More than one function may be in this block */
  6139. goto again;
  6140. }
  6141. mutex_unlock(&ftrace_lock);
  6142. list_for_each_entry_safe(func, func_next, &clear_hash, list) {
  6143. clear_func_from_hashes(func);
  6144. kfree(func);
  6145. }
  6146. }
  6147. void __init ftrace_free_init_mem(void)
  6148. {
  6149. void *start = (void *)(&__init_begin);
  6150. void *end = (void *)(&__init_end);
  6151. ftrace_boot_snapshot();
  6152. ftrace_free_mem(NULL, start, end);
  6153. }
  6154. int __init __weak ftrace_dyn_arch_init(void)
  6155. {
  6156. return 0;
  6157. }
  6158. void __init ftrace_init(void)
  6159. {
  6160. extern unsigned long __start_mcount_loc[];
  6161. extern unsigned long __stop_mcount_loc[];
  6162. unsigned long count, flags;
  6163. int ret;
  6164. local_irq_save(flags);
  6165. ret = ftrace_dyn_arch_init();
  6166. local_irq_restore(flags);
  6167. if (ret)
  6168. goto failed;
  6169. count = __stop_mcount_loc - __start_mcount_loc;
  6170. if (!count) {
  6171. pr_info("ftrace: No functions to be traced?\n");
  6172. goto failed;
  6173. }
  6174. pr_info("ftrace: allocating %ld entries in %ld pages\n",
  6175. count, DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
  6176. ret = ftrace_process_locs(NULL,
  6177. __start_mcount_loc,
  6178. __stop_mcount_loc);
  6179. if (ret) {
  6180. pr_warn("ftrace: failed to allocate entries for functions\n");
  6181. goto failed;
  6182. }
  6183. pr_info("ftrace: allocated %ld pages with %ld groups\n",
  6184. ftrace_number_of_pages, ftrace_number_of_groups);
  6185. last_ftrace_enabled = ftrace_enabled = 1;
  6186. set_ftrace_early_filters();
  6187. return;
  6188. failed:
  6189. ftrace_disabled = 1;
  6190. }
  6191. /* Do nothing if arch does not support this */
  6192. void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
  6193. {
  6194. }
  6195. static void ftrace_update_trampoline(struct ftrace_ops *ops)
  6196. {
  6197. unsigned long trampoline = ops->trampoline;
  6198. arch_ftrace_update_trampoline(ops);
  6199. if (ops->trampoline && ops->trampoline != trampoline &&
  6200. (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP)) {
  6201. /* Add to kallsyms before the perf events */
  6202. ftrace_add_trampoline_to_kallsyms(ops);
  6203. perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
  6204. ops->trampoline, ops->trampoline_size, false,
  6205. FTRACE_TRAMPOLINE_SYM);
  6206. /*
  6207. * Record the perf text poke event after the ksymbol register
  6208. * event.
  6209. */
  6210. perf_event_text_poke((void *)ops->trampoline, NULL, 0,
  6211. (void *)ops->trampoline,
  6212. ops->trampoline_size);
  6213. }
  6214. }
  6215. void ftrace_init_trace_array(struct trace_array *tr)
  6216. {
  6217. INIT_LIST_HEAD(&tr->func_probes);
  6218. INIT_LIST_HEAD(&tr->mod_trace);
  6219. INIT_LIST_HEAD(&tr->mod_notrace);
  6220. }
  6221. #else
  6222. struct ftrace_ops global_ops = {
  6223. .func = ftrace_stub,
  6224. .flags = FTRACE_OPS_FL_INITIALIZED |
  6225. FTRACE_OPS_FL_PID,
  6226. };
  6227. static int __init ftrace_nodyn_init(void)
  6228. {
  6229. ftrace_enabled = 1;
  6230. return 0;
  6231. }
  6232. core_initcall(ftrace_nodyn_init);
  6233. static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
  6234. static inline void ftrace_startup_all(int command) { }
  6235. static void ftrace_update_trampoline(struct ftrace_ops *ops)
  6236. {
  6237. }
  6238. #endif /* CONFIG_DYNAMIC_FTRACE */
  6239. __init void ftrace_init_global_array_ops(struct trace_array *tr)
  6240. {
  6241. tr->ops = &global_ops;
  6242. tr->ops->private = tr;
  6243. ftrace_init_trace_array(tr);
  6244. }
  6245. void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
  6246. {
  6247. /* If we filter on pids, update to use the pid function */
  6248. if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
  6249. if (WARN_ON(tr->ops->func != ftrace_stub))
  6250. printk("ftrace ops had %pS for function\n",
  6251. tr->ops->func);
  6252. }
  6253. tr->ops->func = func;
  6254. tr->ops->private = tr;
  6255. }
  6256. void ftrace_reset_array_ops(struct trace_array *tr)
  6257. {
  6258. tr->ops->func = ftrace_stub;
  6259. }
  6260. static nokprobe_inline void
  6261. __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
  6262. struct ftrace_ops *ignored, struct ftrace_regs *fregs)
  6263. {
  6264. struct pt_regs *regs = ftrace_get_regs(fregs);
  6265. struct ftrace_ops *op;
  6266. int bit;
  6267. /*
  6268. * The ftrace_test_and_set_recursion() will disable preemption,
  6269. * which is required since some of the ops may be dynamically
  6270. * allocated, they must be freed after a synchronize_rcu().
  6271. */
  6272. bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START);
  6273. if (bit < 0)
  6274. return;
  6275. do_for_each_ftrace_op(op, ftrace_ops_list) {
  6276. /* Stub functions don't need to be called nor tested */
  6277. if (op->flags & FTRACE_OPS_FL_STUB)
  6278. continue;
  6279. /*
  6280. * Check the following for each ops before calling their func:
  6281. * if RCU flag is set, then rcu_is_watching() must be true
  6282. * if PER_CPU is set, then ftrace_function_local_disable()
  6283. * must be false
  6284. * Otherwise test if the ip matches the ops filter
  6285. *
  6286. * If any of the above fails then the op->func() is not executed.
  6287. */
  6288. if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
  6289. ftrace_ops_test(op, ip, regs)) {
  6290. if (FTRACE_WARN_ON(!op->func)) {
  6291. pr_warn("op=%p %pS\n", op, op);
  6292. goto out;
  6293. }
  6294. op->func(ip, parent_ip, op, fregs);
  6295. }
  6296. } while_for_each_ftrace_op(op);
  6297. out:
  6298. trace_clear_recursion(bit);
  6299. }
  6300. /*
  6301. * Some archs only support passing ip and parent_ip. Even though
  6302. * the list function ignores the op parameter, we do not want any
  6303. * C side effects, where a function is called without the caller
  6304. * sending a third parameter.
  6305. * Archs are to support both the regs and ftrace_ops at the same time.
  6306. * If they support ftrace_ops, it is assumed they support regs.
  6307. * If call backs want to use regs, they must either check for regs
  6308. * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
  6309. * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
  6310. * An architecture can pass partial regs with ftrace_ops and still
  6311. * set the ARCH_SUPPORTS_FTRACE_OPS.
  6312. *
  6313. * In vmlinux.lds.h, ftrace_ops_list_func() is defined to be
  6314. * arch_ftrace_ops_list_func.
  6315. */
  6316. #if ARCH_SUPPORTS_FTRACE_OPS
  6317. void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
  6318. struct ftrace_ops *op, struct ftrace_regs *fregs)
  6319. {
  6320. __ftrace_ops_list_func(ip, parent_ip, NULL, fregs);
  6321. }
  6322. #else
  6323. void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
  6324. {
  6325. __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
  6326. }
  6327. #endif
  6328. NOKPROBE_SYMBOL(arch_ftrace_ops_list_func);
  6329. /*
  6330. * If there's only one function registered but it does not support
  6331. * recursion, needs RCU protection and/or requires per cpu handling, then
  6332. * this function will be called by the mcount trampoline.
  6333. */
  6334. static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
  6335. struct ftrace_ops *op, struct ftrace_regs *fregs)
  6336. {
  6337. int bit;
  6338. bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START);
  6339. if (bit < 0)
  6340. return;
  6341. if (!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching())
  6342. op->func(ip, parent_ip, op, fregs);
  6343. trace_clear_recursion(bit);
  6344. }
  6345. NOKPROBE_SYMBOL(ftrace_ops_assist_func);
  6346. /**
  6347. * ftrace_ops_get_func - get the function a trampoline should call
  6348. * @ops: the ops to get the function for
  6349. *
  6350. * Normally the mcount trampoline will call the ops->func, but there
  6351. * are times that it should not. For example, if the ops does not
  6352. * have its own recursion protection, then it should call the
  6353. * ftrace_ops_assist_func() instead.
  6354. *
  6355. * Returns the function that the trampoline should call for @ops.
  6356. */
  6357. ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
  6358. {
  6359. /*
  6360. * If the function does not handle recursion or needs to be RCU safe,
  6361. * then we need to call the assist handler.
  6362. */
  6363. if (ops->flags & (FTRACE_OPS_FL_RECURSION |
  6364. FTRACE_OPS_FL_RCU))
  6365. return ftrace_ops_assist_func;
  6366. return ops->func;
  6367. }
  6368. static void
  6369. ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
  6370. struct task_struct *prev,
  6371. struct task_struct *next,
  6372. unsigned int prev_state)
  6373. {
  6374. struct trace_array *tr = data;
  6375. struct trace_pid_list *pid_list;
  6376. struct trace_pid_list *no_pid_list;
  6377. pid_list = rcu_dereference_sched(tr->function_pids);
  6378. no_pid_list = rcu_dereference_sched(tr->function_no_pids);
  6379. if (trace_ignore_this_task(pid_list, no_pid_list, next))
  6380. this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
  6381. FTRACE_PID_IGNORE);
  6382. else
  6383. this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
  6384. next->pid);
  6385. }
  6386. static void
  6387. ftrace_pid_follow_sched_process_fork(void *data,
  6388. struct task_struct *self,
  6389. struct task_struct *task)
  6390. {
  6391. struct trace_pid_list *pid_list;
  6392. struct trace_array *tr = data;
  6393. pid_list = rcu_dereference_sched(tr->function_pids);
  6394. trace_filter_add_remove_task(pid_list, self, task);
  6395. pid_list = rcu_dereference_sched(tr->function_no_pids);
  6396. trace_filter_add_remove_task(pid_list, self, task);
  6397. }
  6398. static void
  6399. ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
  6400. {
  6401. struct trace_pid_list *pid_list;
  6402. struct trace_array *tr = data;
  6403. pid_list = rcu_dereference_sched(tr->function_pids);
  6404. trace_filter_add_remove_task(pid_list, NULL, task);
  6405. pid_list = rcu_dereference_sched(tr->function_no_pids);
  6406. trace_filter_add_remove_task(pid_list, NULL, task);
  6407. }
  6408. void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
  6409. {
  6410. if (enable) {
  6411. register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
  6412. tr);
  6413. register_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
  6414. tr);
  6415. } else {
  6416. unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
  6417. tr);
  6418. unregister_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
  6419. tr);
  6420. }
  6421. }
  6422. static void clear_ftrace_pids(struct trace_array *tr, int type)
  6423. {
  6424. struct trace_pid_list *pid_list;
  6425. struct trace_pid_list *no_pid_list;
  6426. int cpu;
  6427. pid_list = rcu_dereference_protected(tr->function_pids,
  6428. lockdep_is_held(&ftrace_lock));
  6429. no_pid_list = rcu_dereference_protected(tr->function_no_pids,
  6430. lockdep_is_held(&ftrace_lock));
  6431. /* Make sure there's something to do */
  6432. if (!pid_type_enabled(type, pid_list, no_pid_list))
  6433. return;
  6434. /* See if the pids still need to be checked after this */
  6435. if (!still_need_pid_events(type, pid_list, no_pid_list)) {
  6436. unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
  6437. for_each_possible_cpu(cpu)
  6438. per_cpu_ptr(tr->array_buffer.data, cpu)->ftrace_ignore_pid = FTRACE_PID_TRACE;
  6439. }
  6440. if (type & TRACE_PIDS)
  6441. rcu_assign_pointer(tr->function_pids, NULL);
  6442. if (type & TRACE_NO_PIDS)
  6443. rcu_assign_pointer(tr->function_no_pids, NULL);
  6444. /* Wait till all users are no longer using pid filtering */
  6445. synchronize_rcu();
  6446. if ((type & TRACE_PIDS) && pid_list)
  6447. trace_pid_list_free(pid_list);
  6448. if ((type & TRACE_NO_PIDS) && no_pid_list)
  6449. trace_pid_list_free(no_pid_list);
  6450. }
  6451. void ftrace_clear_pids(struct trace_array *tr)
  6452. {
  6453. mutex_lock(&ftrace_lock);
  6454. clear_ftrace_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
  6455. mutex_unlock(&ftrace_lock);
  6456. }
  6457. static void ftrace_pid_reset(struct trace_array *tr, int type)
  6458. {
  6459. mutex_lock(&ftrace_lock);
  6460. clear_ftrace_pids(tr, type);
  6461. ftrace_update_pid_func();
  6462. ftrace_startup_all(0);
  6463. mutex_unlock(&ftrace_lock);
  6464. }
  6465. /* Greater than any max PID */
  6466. #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
  6467. static void *fpid_start(struct seq_file *m, loff_t *pos)
  6468. __acquires(RCU)
  6469. {
  6470. struct trace_pid_list *pid_list;
  6471. struct trace_array *tr = m->private;
  6472. mutex_lock(&ftrace_lock);
  6473. rcu_read_lock_sched();
  6474. pid_list = rcu_dereference_sched(tr->function_pids);
  6475. if (!pid_list)
  6476. return !(*pos) ? FTRACE_NO_PIDS : NULL;
  6477. return trace_pid_start(pid_list, pos);
  6478. }
  6479. static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
  6480. {
  6481. struct trace_array *tr = m->private;
  6482. struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
  6483. if (v == FTRACE_NO_PIDS) {
  6484. (*pos)++;
  6485. return NULL;
  6486. }
  6487. return trace_pid_next(pid_list, v, pos);
  6488. }
  6489. static void fpid_stop(struct seq_file *m, void *p)
  6490. __releases(RCU)
  6491. {
  6492. rcu_read_unlock_sched();
  6493. mutex_unlock(&ftrace_lock);
  6494. }
  6495. static int fpid_show(struct seq_file *m, void *v)
  6496. {
  6497. if (v == FTRACE_NO_PIDS) {
  6498. seq_puts(m, "no pid\n");
  6499. return 0;
  6500. }
  6501. return trace_pid_show(m, v);
  6502. }
  6503. static const struct seq_operations ftrace_pid_sops = {
  6504. .start = fpid_start,
  6505. .next = fpid_next,
  6506. .stop = fpid_stop,
  6507. .show = fpid_show,
  6508. };
  6509. static void *fnpid_start(struct seq_file *m, loff_t *pos)
  6510. __acquires(RCU)
  6511. {
  6512. struct trace_pid_list *pid_list;
  6513. struct trace_array *tr = m->private;
  6514. mutex_lock(&ftrace_lock);
  6515. rcu_read_lock_sched();
  6516. pid_list = rcu_dereference_sched(tr->function_no_pids);
  6517. if (!pid_list)
  6518. return !(*pos) ? FTRACE_NO_PIDS : NULL;
  6519. return trace_pid_start(pid_list, pos);
  6520. }
  6521. static void *fnpid_next(struct seq_file *m, void *v, loff_t *pos)
  6522. {
  6523. struct trace_array *tr = m->private;
  6524. struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_no_pids);
  6525. if (v == FTRACE_NO_PIDS) {
  6526. (*pos)++;
  6527. return NULL;
  6528. }
  6529. return trace_pid_next(pid_list, v, pos);
  6530. }
  6531. static const struct seq_operations ftrace_no_pid_sops = {
  6532. .start = fnpid_start,
  6533. .next = fnpid_next,
  6534. .stop = fpid_stop,
  6535. .show = fpid_show,
  6536. };
  6537. static int pid_open(struct inode *inode, struct file *file, int type)
  6538. {
  6539. const struct seq_operations *seq_ops;
  6540. struct trace_array *tr = inode->i_private;
  6541. struct seq_file *m;
  6542. int ret = 0;
  6543. ret = tracing_check_open_get_tr(tr);
  6544. if (ret)
  6545. return ret;
  6546. if ((file->f_mode & FMODE_WRITE) &&
  6547. (file->f_flags & O_TRUNC))
  6548. ftrace_pid_reset(tr, type);
  6549. switch (type) {
  6550. case TRACE_PIDS:
  6551. seq_ops = &ftrace_pid_sops;
  6552. break;
  6553. case TRACE_NO_PIDS:
  6554. seq_ops = &ftrace_no_pid_sops;
  6555. break;
  6556. default:
  6557. trace_array_put(tr);
  6558. WARN_ON_ONCE(1);
  6559. return -EINVAL;
  6560. }
  6561. ret = seq_open(file, seq_ops);
  6562. if (ret < 0) {
  6563. trace_array_put(tr);
  6564. } else {
  6565. m = file->private_data;
  6566. /* copy tr over to seq ops */
  6567. m->private = tr;
  6568. }
  6569. return ret;
  6570. }
  6571. static int
  6572. ftrace_pid_open(struct inode *inode, struct file *file)
  6573. {
  6574. return pid_open(inode, file, TRACE_PIDS);
  6575. }
  6576. static int
  6577. ftrace_no_pid_open(struct inode *inode, struct file *file)
  6578. {
  6579. return pid_open(inode, file, TRACE_NO_PIDS);
  6580. }
  6581. static void ignore_task_cpu(void *data)
  6582. {
  6583. struct trace_array *tr = data;
  6584. struct trace_pid_list *pid_list;
  6585. struct trace_pid_list *no_pid_list;
  6586. /*
  6587. * This function is called by on_each_cpu() while the
  6588. * event_mutex is held.
  6589. */
  6590. pid_list = rcu_dereference_protected(tr->function_pids,
  6591. mutex_is_locked(&ftrace_lock));
  6592. no_pid_list = rcu_dereference_protected(tr->function_no_pids,
  6593. mutex_is_locked(&ftrace_lock));
  6594. if (trace_ignore_this_task(pid_list, no_pid_list, current))
  6595. this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
  6596. FTRACE_PID_IGNORE);
  6597. else
  6598. this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
  6599. current->pid);
  6600. }
  6601. static ssize_t
  6602. pid_write(struct file *filp, const char __user *ubuf,
  6603. size_t cnt, loff_t *ppos, int type)
  6604. {
  6605. struct seq_file *m = filp->private_data;
  6606. struct trace_array *tr = m->private;
  6607. struct trace_pid_list *filtered_pids;
  6608. struct trace_pid_list *other_pids;
  6609. struct trace_pid_list *pid_list;
  6610. ssize_t ret;
  6611. if (!cnt)
  6612. return 0;
  6613. mutex_lock(&ftrace_lock);
  6614. switch (type) {
  6615. case TRACE_PIDS:
  6616. filtered_pids = rcu_dereference_protected(tr->function_pids,
  6617. lockdep_is_held(&ftrace_lock));
  6618. other_pids = rcu_dereference_protected(tr->function_no_pids,
  6619. lockdep_is_held(&ftrace_lock));
  6620. break;
  6621. case TRACE_NO_PIDS:
  6622. filtered_pids = rcu_dereference_protected(tr->function_no_pids,
  6623. lockdep_is_held(&ftrace_lock));
  6624. other_pids = rcu_dereference_protected(tr->function_pids,
  6625. lockdep_is_held(&ftrace_lock));
  6626. break;
  6627. default:
  6628. ret = -EINVAL;
  6629. WARN_ON_ONCE(1);
  6630. goto out;
  6631. }
  6632. ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
  6633. if (ret < 0)
  6634. goto out;
  6635. switch (type) {
  6636. case TRACE_PIDS:
  6637. rcu_assign_pointer(tr->function_pids, pid_list);
  6638. break;
  6639. case TRACE_NO_PIDS:
  6640. rcu_assign_pointer(tr->function_no_pids, pid_list);
  6641. break;
  6642. }
  6643. if (filtered_pids) {
  6644. synchronize_rcu();
  6645. trace_pid_list_free(filtered_pids);
  6646. } else if (pid_list && !other_pids) {
  6647. /* Register a probe to set whether to ignore the tracing of a task */
  6648. register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
  6649. }
  6650. /*
  6651. * Ignoring of pids is done at task switch. But we have to
  6652. * check for those tasks that are currently running.
  6653. * Always do this in case a pid was appended or removed.
  6654. */
  6655. on_each_cpu(ignore_task_cpu, tr, 1);
  6656. ftrace_update_pid_func();
  6657. ftrace_startup_all(0);
  6658. out:
  6659. mutex_unlock(&ftrace_lock);
  6660. if (ret > 0)
  6661. *ppos += ret;
  6662. return ret;
  6663. }
  6664. static ssize_t
  6665. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  6666. size_t cnt, loff_t *ppos)
  6667. {
  6668. return pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
  6669. }
  6670. static ssize_t
  6671. ftrace_no_pid_write(struct file *filp, const char __user *ubuf,
  6672. size_t cnt, loff_t *ppos)
  6673. {
  6674. return pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
  6675. }
  6676. static int
  6677. ftrace_pid_release(struct inode *inode, struct file *file)
  6678. {
  6679. struct trace_array *tr = inode->i_private;
  6680. trace_array_put(tr);
  6681. return seq_release(inode, file);
  6682. }
  6683. static const struct file_operations ftrace_pid_fops = {
  6684. .open = ftrace_pid_open,
  6685. .write = ftrace_pid_write,
  6686. .read = seq_read,
  6687. .llseek = tracing_lseek,
  6688. .release = ftrace_pid_release,
  6689. };
  6690. static const struct file_operations ftrace_no_pid_fops = {
  6691. .open = ftrace_no_pid_open,
  6692. .write = ftrace_no_pid_write,
  6693. .read = seq_read,
  6694. .llseek = tracing_lseek,
  6695. .release = ftrace_pid_release,
  6696. };
  6697. void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
  6698. {
  6699. trace_create_file("set_ftrace_pid", TRACE_MODE_WRITE, d_tracer,
  6700. tr, &ftrace_pid_fops);
  6701. trace_create_file("set_ftrace_notrace_pid", TRACE_MODE_WRITE,
  6702. d_tracer, tr, &ftrace_no_pid_fops);
  6703. }
  6704. void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
  6705. struct dentry *d_tracer)
  6706. {
  6707. /* Only the top level directory has the dyn_tracefs and profile */
  6708. WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
  6709. ftrace_init_dyn_tracefs(d_tracer);
  6710. ftrace_profile_tracefs(d_tracer);
  6711. }
  6712. /**
  6713. * ftrace_kill - kill ftrace
  6714. *
  6715. * This function should be used by panic code. It stops ftrace
  6716. * but in a not so nice way. If you need to simply kill ftrace
  6717. * from a non-atomic section, use ftrace_kill.
  6718. */
  6719. void ftrace_kill(void)
  6720. {
  6721. ftrace_disabled = 1;
  6722. ftrace_enabled = 0;
  6723. ftrace_trace_function = ftrace_stub;
  6724. }
  6725. /**
  6726. * ftrace_is_dead - Test if ftrace is dead or not.
  6727. *
  6728. * Returns 1 if ftrace is "dead", zero otherwise.
  6729. */
  6730. int ftrace_is_dead(void)
  6731. {
  6732. return ftrace_disabled;
  6733. }
  6734. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
  6735. /*
  6736. * When registering ftrace_ops with IPMODIFY, it is necessary to make sure
  6737. * it doesn't conflict with any direct ftrace_ops. If there is existing
  6738. * direct ftrace_ops on a kernel function being patched, call
  6739. * FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER on it to enable sharing.
  6740. *
  6741. * @ops: ftrace_ops being registered.
  6742. *
  6743. * Returns:
  6744. * 0 on success;
  6745. * Negative on failure.
  6746. */
  6747. static int prepare_direct_functions_for_ipmodify(struct ftrace_ops *ops)
  6748. {
  6749. struct ftrace_func_entry *entry;
  6750. struct ftrace_hash *hash;
  6751. struct ftrace_ops *op;
  6752. int size, i, ret;
  6753. lockdep_assert_held_once(&direct_mutex);
  6754. if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
  6755. return 0;
  6756. hash = ops->func_hash->filter_hash;
  6757. size = 1 << hash->size_bits;
  6758. for (i = 0; i < size; i++) {
  6759. hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
  6760. unsigned long ip = entry->ip;
  6761. bool found_op = false;
  6762. mutex_lock(&ftrace_lock);
  6763. do_for_each_ftrace_op(op, ftrace_ops_list) {
  6764. if (!(op->flags & FTRACE_OPS_FL_DIRECT))
  6765. continue;
  6766. if (ops_references_ip(op, ip)) {
  6767. found_op = true;
  6768. break;
  6769. }
  6770. } while_for_each_ftrace_op(op);
  6771. mutex_unlock(&ftrace_lock);
  6772. if (found_op) {
  6773. if (!op->ops_func)
  6774. return -EBUSY;
  6775. ret = op->ops_func(op, FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER);
  6776. if (ret)
  6777. return ret;
  6778. }
  6779. }
  6780. }
  6781. return 0;
  6782. }
  6783. /*
  6784. * Similar to prepare_direct_functions_for_ipmodify, clean up after ops
  6785. * with IPMODIFY is unregistered. The cleanup is optional for most DIRECT
  6786. * ops.
  6787. */
  6788. static void cleanup_direct_functions_after_ipmodify(struct ftrace_ops *ops)
  6789. {
  6790. struct ftrace_func_entry *entry;
  6791. struct ftrace_hash *hash;
  6792. struct ftrace_ops *op;
  6793. int size, i;
  6794. if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
  6795. return;
  6796. mutex_lock(&direct_mutex);
  6797. hash = ops->func_hash->filter_hash;
  6798. size = 1 << hash->size_bits;
  6799. for (i = 0; i < size; i++) {
  6800. hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
  6801. unsigned long ip = entry->ip;
  6802. bool found_op = false;
  6803. mutex_lock(&ftrace_lock);
  6804. do_for_each_ftrace_op(op, ftrace_ops_list) {
  6805. if (!(op->flags & FTRACE_OPS_FL_DIRECT))
  6806. continue;
  6807. if (ops_references_ip(op, ip)) {
  6808. found_op = true;
  6809. break;
  6810. }
  6811. } while_for_each_ftrace_op(op);
  6812. mutex_unlock(&ftrace_lock);
  6813. /* The cleanup is optional, ignore any errors */
  6814. if (found_op && op->ops_func)
  6815. op->ops_func(op, FTRACE_OPS_CMD_DISABLE_SHARE_IPMODIFY_PEER);
  6816. }
  6817. }
  6818. mutex_unlock(&direct_mutex);
  6819. }
  6820. #define lock_direct_mutex() mutex_lock(&direct_mutex)
  6821. #define unlock_direct_mutex() mutex_unlock(&direct_mutex)
  6822. #else /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
  6823. static int prepare_direct_functions_for_ipmodify(struct ftrace_ops *ops)
  6824. {
  6825. return 0;
  6826. }
  6827. static void cleanup_direct_functions_after_ipmodify(struct ftrace_ops *ops)
  6828. {
  6829. }
  6830. #define lock_direct_mutex() do { } while (0)
  6831. #define unlock_direct_mutex() do { } while (0)
  6832. #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
  6833. /*
  6834. * Similar to register_ftrace_function, except we don't lock direct_mutex.
  6835. */
  6836. static int register_ftrace_function_nolock(struct ftrace_ops *ops)
  6837. {
  6838. int ret;
  6839. ftrace_ops_init(ops);
  6840. mutex_lock(&ftrace_lock);
  6841. ret = ftrace_startup(ops, 0);
  6842. mutex_unlock(&ftrace_lock);
  6843. return ret;
  6844. }
  6845. /**
  6846. * register_ftrace_function - register a function for profiling
  6847. * @ops: ops structure that holds the function for profiling.
  6848. *
  6849. * Register a function to be called by all functions in the
  6850. * kernel.
  6851. *
  6852. * Note: @ops->func and all the functions it calls must be labeled
  6853. * with "notrace", otherwise it will go into a
  6854. * recursive loop.
  6855. */
  6856. int register_ftrace_function(struct ftrace_ops *ops)
  6857. {
  6858. int ret;
  6859. lock_direct_mutex();
  6860. ret = prepare_direct_functions_for_ipmodify(ops);
  6861. if (ret < 0)
  6862. goto out_unlock;
  6863. ret = register_ftrace_function_nolock(ops);
  6864. out_unlock:
  6865. unlock_direct_mutex();
  6866. return ret;
  6867. }
  6868. EXPORT_SYMBOL_GPL(register_ftrace_function);
  6869. /**
  6870. * unregister_ftrace_function - unregister a function for profiling.
  6871. * @ops: ops structure that holds the function to unregister
  6872. *
  6873. * Unregister a function that was added to be called by ftrace profiling.
  6874. */
  6875. int unregister_ftrace_function(struct ftrace_ops *ops)
  6876. {
  6877. int ret;
  6878. mutex_lock(&ftrace_lock);
  6879. ret = ftrace_shutdown(ops, 0);
  6880. mutex_unlock(&ftrace_lock);
  6881. cleanup_direct_functions_after_ipmodify(ops);
  6882. return ret;
  6883. }
  6884. EXPORT_SYMBOL_GPL(unregister_ftrace_function);
  6885. static int symbols_cmp(const void *a, const void *b)
  6886. {
  6887. const char **str_a = (const char **) a;
  6888. const char **str_b = (const char **) b;
  6889. return strcmp(*str_a, *str_b);
  6890. }
  6891. struct kallsyms_data {
  6892. unsigned long *addrs;
  6893. const char **syms;
  6894. size_t cnt;
  6895. size_t found;
  6896. };
  6897. static int kallsyms_callback(void *data, const char *name,
  6898. struct module *mod, unsigned long addr)
  6899. {
  6900. struct kallsyms_data *args = data;
  6901. const char **sym;
  6902. int idx;
  6903. sym = bsearch(&name, args->syms, args->cnt, sizeof(*args->syms), symbols_cmp);
  6904. if (!sym)
  6905. return 0;
  6906. idx = sym - args->syms;
  6907. if (args->addrs[idx])
  6908. return 0;
  6909. if (!ftrace_location(addr))
  6910. return 0;
  6911. args->addrs[idx] = addr;
  6912. args->found++;
  6913. return args->found == args->cnt ? 1 : 0;
  6914. }
  6915. /**
  6916. * ftrace_lookup_symbols - Lookup addresses for array of symbols
  6917. *
  6918. * @sorted_syms: array of symbols pointers symbols to resolve,
  6919. * must be alphabetically sorted
  6920. * @cnt: number of symbols/addresses in @syms/@addrs arrays
  6921. * @addrs: array for storing resulting addresses
  6922. *
  6923. * This function looks up addresses for array of symbols provided in
  6924. * @syms array (must be alphabetically sorted) and stores them in
  6925. * @addrs array, which needs to be big enough to store at least @cnt
  6926. * addresses.
  6927. *
  6928. * This function returns 0 if all provided symbols are found,
  6929. * -ESRCH otherwise.
  6930. */
  6931. int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *addrs)
  6932. {
  6933. struct kallsyms_data args;
  6934. int err;
  6935. memset(addrs, 0, sizeof(*addrs) * cnt);
  6936. args.addrs = addrs;
  6937. args.syms = sorted_syms;
  6938. args.cnt = cnt;
  6939. args.found = 0;
  6940. err = kallsyms_on_each_symbol(kallsyms_callback, &args);
  6941. if (err < 0)
  6942. return err;
  6943. return args.found == args.cnt ? 0 : -ESRCH;
  6944. }
  6945. #ifdef CONFIG_SYSCTL
  6946. #ifdef CONFIG_DYNAMIC_FTRACE
  6947. static void ftrace_startup_sysctl(void)
  6948. {
  6949. int command;
  6950. if (unlikely(ftrace_disabled))
  6951. return;
  6952. /* Force update next time */
  6953. saved_ftrace_func = NULL;
  6954. /* ftrace_start_up is true if we want ftrace running */
  6955. if (ftrace_start_up) {
  6956. command = FTRACE_UPDATE_CALLS;
  6957. if (ftrace_graph_active)
  6958. command |= FTRACE_START_FUNC_RET;
  6959. ftrace_startup_enable(command);
  6960. }
  6961. }
  6962. static void ftrace_shutdown_sysctl(void)
  6963. {
  6964. int command;
  6965. if (unlikely(ftrace_disabled))
  6966. return;
  6967. /* ftrace_start_up is true if ftrace is running */
  6968. if (ftrace_start_up) {
  6969. command = FTRACE_DISABLE_CALLS;
  6970. if (ftrace_graph_active)
  6971. command |= FTRACE_STOP_FUNC_RET;
  6972. ftrace_run_update_code(command);
  6973. }
  6974. }
  6975. #else
  6976. # define ftrace_startup_sysctl() do { } while (0)
  6977. # define ftrace_shutdown_sysctl() do { } while (0)
  6978. #endif /* CONFIG_DYNAMIC_FTRACE */
  6979. static bool is_permanent_ops_registered(void)
  6980. {
  6981. struct ftrace_ops *op;
  6982. do_for_each_ftrace_op(op, ftrace_ops_list) {
  6983. if (op->flags & FTRACE_OPS_FL_PERMANENT)
  6984. return true;
  6985. } while_for_each_ftrace_op(op);
  6986. return false;
  6987. }
  6988. static int
  6989. ftrace_enable_sysctl(struct ctl_table *table, int write,
  6990. void *buffer, size_t *lenp, loff_t *ppos)
  6991. {
  6992. int ret = -ENODEV;
  6993. mutex_lock(&ftrace_lock);
  6994. if (unlikely(ftrace_disabled))
  6995. goto out;
  6996. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  6997. if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
  6998. goto out;
  6999. if (ftrace_enabled) {
  7000. /* we are starting ftrace again */
  7001. if (rcu_dereference_protected(ftrace_ops_list,
  7002. lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
  7003. update_ftrace_function();
  7004. ftrace_startup_sysctl();
  7005. } else {
  7006. if (is_permanent_ops_registered()) {
  7007. ftrace_enabled = true;
  7008. ret = -EBUSY;
  7009. goto out;
  7010. }
  7011. /* stopping ftrace calls (just send to ftrace_stub) */
  7012. ftrace_trace_function = ftrace_stub;
  7013. ftrace_shutdown_sysctl();
  7014. }
  7015. last_ftrace_enabled = !!ftrace_enabled;
  7016. out:
  7017. mutex_unlock(&ftrace_lock);
  7018. return ret;
  7019. }
  7020. static struct ctl_table ftrace_sysctls[] = {
  7021. {
  7022. .procname = "ftrace_enabled",
  7023. .data = &ftrace_enabled,
  7024. .maxlen = sizeof(int),
  7025. .mode = 0644,
  7026. .proc_handler = ftrace_enable_sysctl,
  7027. },
  7028. {}
  7029. };
  7030. static int __init ftrace_sysctl_init(void)
  7031. {
  7032. register_sysctl_init("kernel", ftrace_sysctls);
  7033. return 0;
  7034. }
  7035. late_initcall(ftrace_sysctl_init);
  7036. #endif