ftrace.c 152 KB

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