ftrace.c 166 KB

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