ftrace.c 162 KB

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