ftrace.c 137 KB

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