ftrace.c 159 KB

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