ftrace.c 165 KB

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