ftrace.c 212 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Infrastructure for profiling code inserted by 'gcc -pg'.
  4. *
  5. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  6. * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
  7. *
  8. * Originally ported from the -rt patch by:
  9. * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
  10. *
  11. * Based on code in the latency_tracer, that is:
  12. *
  13. * Copyright (C) 2004-2006 Ingo Molnar
  14. * Copyright (C) 2004 Nadia Yvette Chambers
  15. */
  16. #include <linux/stop_machine.h>
  17. #include <linux/clocksource.h>
  18. #include <linux/sched/task.h>
  19. #include <linux/kallsyms.h>
  20. #include <linux/security.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/tracefs.h>
  23. #include <linux/hardirq.h>
  24. #include <linux/kthread.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/bsearch.h>
  27. #include <linux/module.h>
  28. #include <linux/ftrace.h>
  29. #include <linux/sysctl.h>
  30. #include <linux/slab.h>
  31. #include <linux/ctype.h>
  32. #include <linux/sort.h>
  33. #include <linux/list.h>
  34. #include <linux/hash.h>
  35. #include <linux/rcupdate.h>
  36. #include <linux/kprobes.h>
  37. #include <trace/events/sched.h>
  38. #include <asm/sections.h>
  39. #include <asm/setup.h>
  40. #include "ftrace_internal.h"
  41. #include "trace_output.h"
  42. #include "trace_stat.h"
  43. /* Flags that do not get reset */
  44. #define FTRACE_NOCLEAR_FLAGS (FTRACE_FL_DISABLED | FTRACE_FL_TOUCHED | \
  45. FTRACE_FL_MODIFIED)
  46. #define FTRACE_INVALID_FUNCTION "__ftrace_invalid_address__"
  47. #define FTRACE_WARN_ON(cond) \
  48. ({ \
  49. int ___r = cond; \
  50. if (WARN_ON(___r)) \
  51. ftrace_kill(); \
  52. ___r; \
  53. })
  54. #define FTRACE_WARN_ON_ONCE(cond) \
  55. ({ \
  56. int ___r = cond; \
  57. if (WARN_ON_ONCE(___r)) \
  58. ftrace_kill(); \
  59. ___r; \
  60. })
  61. /* hash bits for specific function selection */
  62. #define FTRACE_HASH_DEFAULT_BITS 10
  63. #define FTRACE_HASH_MAX_BITS 12
  64. #ifdef CONFIG_DYNAMIC_FTRACE
  65. #define INIT_OPS_HASH(opsname) \
  66. .func_hash = &opsname.local_hash, \
  67. .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock), \
  68. .subop_list = LIST_HEAD_INIT(opsname.subop_list),
  69. #else
  70. #define INIT_OPS_HASH(opsname)
  71. #endif
  72. enum {
  73. FTRACE_MODIFY_ENABLE_FL = (1 << 0),
  74. FTRACE_MODIFY_MAY_SLEEP_FL = (1 << 1),
  75. };
  76. struct ftrace_ops ftrace_list_end __read_mostly = {
  77. .func = ftrace_stub,
  78. .flags = FTRACE_OPS_FL_STUB,
  79. INIT_OPS_HASH(ftrace_list_end)
  80. };
  81. /* ftrace_enabled is a method to turn ftrace on or off */
  82. int ftrace_enabled __read_mostly;
  83. static int __maybe_unused last_ftrace_enabled;
  84. /* Current function tracing op */
  85. struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
  86. /* What to set function_trace_op to */
  87. static struct ftrace_ops *set_function_trace_op;
  88. bool ftrace_pids_enabled(struct ftrace_ops *ops)
  89. {
  90. struct trace_array *tr;
  91. if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
  92. return false;
  93. tr = ops->private;
  94. return tr->function_pids != NULL || tr->function_no_pids != NULL;
  95. }
  96. static void ftrace_update_trampoline(struct ftrace_ops *ops);
  97. /*
  98. * ftrace_disabled is set when an anomaly is discovered.
  99. * ftrace_disabled is much stronger than ftrace_enabled.
  100. */
  101. static int ftrace_disabled __read_mostly;
  102. DEFINE_MUTEX(ftrace_lock);
  103. struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = (struct ftrace_ops __rcu *)&ftrace_list_end;
  104. ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
  105. struct ftrace_ops global_ops;
  106. /* Defined by vmlinux.lds.h see the comment above arch_ftrace_ops_list_func for details */
  107. void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
  108. struct ftrace_ops *op, struct ftrace_regs *fregs);
  109. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS
  110. /*
  111. * Stub used to invoke the list ops without requiring a separate trampoline.
  112. */
  113. const struct ftrace_ops ftrace_list_ops = {
  114. .func = ftrace_ops_list_func,
  115. .flags = FTRACE_OPS_FL_STUB,
  116. };
  117. static void ftrace_ops_nop_func(unsigned long ip, unsigned long parent_ip,
  118. struct ftrace_ops *op,
  119. struct ftrace_regs *fregs)
  120. {
  121. /* do nothing */
  122. }
  123. /*
  124. * Stub used when a call site is disabled. May be called transiently by threads
  125. * which have made it into ftrace_caller but haven't yet recovered the ops at
  126. * the point the call site is disabled.
  127. */
  128. const struct ftrace_ops ftrace_nop_ops = {
  129. .func = ftrace_ops_nop_func,
  130. .flags = FTRACE_OPS_FL_STUB,
  131. };
  132. #endif
  133. static inline void ftrace_ops_init(struct ftrace_ops *ops)
  134. {
  135. #ifdef CONFIG_DYNAMIC_FTRACE
  136. if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
  137. mutex_init(&ops->local_hash.regex_lock);
  138. INIT_LIST_HEAD(&ops->subop_list);
  139. ops->func_hash = &ops->local_hash;
  140. ops->flags |= FTRACE_OPS_FL_INITIALIZED;
  141. }
  142. #endif
  143. }
  144. /* Call this function for when a callback filters on set_ftrace_pid */
  145. static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
  146. struct ftrace_ops *op, struct ftrace_regs *fregs)
  147. {
  148. struct trace_array *tr = op->private;
  149. int pid;
  150. if (tr) {
  151. pid = this_cpu_read(tr->array_buffer.data->ftrace_ignore_pid);
  152. if (pid == FTRACE_PID_IGNORE)
  153. return;
  154. if (pid != FTRACE_PID_TRACE &&
  155. pid != current->pid)
  156. return;
  157. }
  158. op->saved_func(ip, parent_ip, op, fregs);
  159. }
  160. static void ftrace_sync_ipi(void *data)
  161. {
  162. /* Probably not needed, but do it anyway */
  163. smp_rmb();
  164. }
  165. static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
  166. {
  167. /*
  168. * If this is a dynamic or RCU ops, or we force list func,
  169. * then it needs to call the list anyway.
  170. */
  171. if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) ||
  172. FTRACE_FORCE_LIST_FUNC)
  173. return ftrace_ops_list_func;
  174. return ftrace_ops_get_func(ops);
  175. }
  176. static void update_ftrace_function(void)
  177. {
  178. ftrace_func_t func;
  179. /*
  180. * Prepare the ftrace_ops that the arch callback will use.
  181. * If there's only one ftrace_ops registered, the ftrace_ops_list
  182. * will point to the ops we want.
  183. */
  184. set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
  185. lockdep_is_held(&ftrace_lock));
  186. /* If there's no ftrace_ops registered, just call the stub function */
  187. if (set_function_trace_op == &ftrace_list_end) {
  188. func = ftrace_stub;
  189. /*
  190. * If we are at the end of the list and this ops is
  191. * recursion safe and not dynamic and the arch supports passing ops,
  192. * then have the mcount trampoline call the function directly.
  193. */
  194. } else if (rcu_dereference_protected(ftrace_ops_list->next,
  195. lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
  196. func = ftrace_ops_get_list_func(ftrace_ops_list);
  197. } else {
  198. /* Just use the default ftrace_ops */
  199. set_function_trace_op = &ftrace_list_end;
  200. func = ftrace_ops_list_func;
  201. }
  202. /* If there's no change, then do nothing more here */
  203. if (ftrace_trace_function == func)
  204. return;
  205. /*
  206. * If we are using the list function, it doesn't care
  207. * about the function_trace_ops.
  208. */
  209. if (func == ftrace_ops_list_func) {
  210. ftrace_trace_function = func;
  211. /*
  212. * Don't even bother setting function_trace_ops,
  213. * it would be racy to do so anyway.
  214. */
  215. return;
  216. }
  217. #ifndef CONFIG_DYNAMIC_FTRACE
  218. /*
  219. * For static tracing, we need to be a bit more careful.
  220. * The function change takes affect immediately. Thus,
  221. * we need to coordinate the setting of the function_trace_ops
  222. * with the setting of the ftrace_trace_function.
  223. *
  224. * Set the function to the list ops, which will call the
  225. * function we want, albeit indirectly, but it handles the
  226. * ftrace_ops and doesn't depend on function_trace_op.
  227. */
  228. ftrace_trace_function = ftrace_ops_list_func;
  229. /*
  230. * Make sure all CPUs see this. Yes this is slow, but static
  231. * tracing is slow and nasty to have enabled.
  232. */
  233. synchronize_rcu_tasks_rude();
  234. /* Now all cpus are using the list ops. */
  235. function_trace_op = set_function_trace_op;
  236. /* Make sure the function_trace_op is visible on all CPUs */
  237. smp_wmb();
  238. /* Nasty way to force a rmb on all cpus */
  239. smp_call_function(ftrace_sync_ipi, NULL, 1);
  240. /* OK, we are all set to update the ftrace_trace_function now! */
  241. #endif /* !CONFIG_DYNAMIC_FTRACE */
  242. ftrace_trace_function = func;
  243. }
  244. static void add_ftrace_ops(struct ftrace_ops __rcu **list,
  245. struct ftrace_ops *ops)
  246. {
  247. rcu_assign_pointer(ops->next, *list);
  248. /*
  249. * We are entering ops into the list but another
  250. * CPU might be walking that list. We need to make sure
  251. * the ops->next pointer is valid before another CPU sees
  252. * the ops pointer included into the list.
  253. */
  254. rcu_assign_pointer(*list, ops);
  255. }
  256. static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
  257. struct ftrace_ops *ops)
  258. {
  259. struct ftrace_ops **p;
  260. /*
  261. * If we are removing the last function, then simply point
  262. * to the ftrace_stub.
  263. */
  264. if (rcu_dereference_protected(*list,
  265. lockdep_is_held(&ftrace_lock)) == ops &&
  266. rcu_dereference_protected(ops->next,
  267. lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
  268. rcu_assign_pointer(*list, &ftrace_list_end);
  269. return 0;
  270. }
  271. for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
  272. if (*p == ops)
  273. break;
  274. if (*p != ops)
  275. return -1;
  276. *p = (*p)->next;
  277. return 0;
  278. }
  279. static void ftrace_update_trampoline(struct ftrace_ops *ops);
  280. int __register_ftrace_function(struct ftrace_ops *ops)
  281. {
  282. if (ops->flags & FTRACE_OPS_FL_DELETED)
  283. return -EINVAL;
  284. if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
  285. return -EBUSY;
  286. #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  287. /*
  288. * If the ftrace_ops specifies SAVE_REGS, then it only can be used
  289. * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
  290. * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
  291. */
  292. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
  293. !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
  294. return -EINVAL;
  295. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
  296. ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
  297. #endif
  298. if (!ftrace_enabled && (ops->flags & FTRACE_OPS_FL_PERMANENT))
  299. return -EBUSY;
  300. if (!is_kernel_core_data((unsigned long)ops))
  301. ops->flags |= FTRACE_OPS_FL_DYNAMIC;
  302. add_ftrace_ops(&ftrace_ops_list, ops);
  303. /* Always save the function, and reset at unregistering */
  304. ops->saved_func = ops->func;
  305. if (ftrace_pids_enabled(ops))
  306. ops->func = ftrace_pid_func;
  307. ftrace_update_trampoline(ops);
  308. if (ftrace_enabled)
  309. update_ftrace_function();
  310. return 0;
  311. }
  312. int __unregister_ftrace_function(struct ftrace_ops *ops)
  313. {
  314. int ret;
  315. if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
  316. return -EBUSY;
  317. ret = remove_ftrace_ops(&ftrace_ops_list, ops);
  318. if (ret < 0)
  319. return ret;
  320. if (ftrace_enabled)
  321. update_ftrace_function();
  322. ops->func = ops->saved_func;
  323. return 0;
  324. }
  325. static void ftrace_update_pid_func(void)
  326. {
  327. struct ftrace_ops *op;
  328. /* Only do something if we are tracing something */
  329. if (ftrace_trace_function == ftrace_stub)
  330. return;
  331. do_for_each_ftrace_op(op, ftrace_ops_list) {
  332. if (op->flags & FTRACE_OPS_FL_PID) {
  333. op->func = ftrace_pids_enabled(op) ?
  334. ftrace_pid_func : op->saved_func;
  335. ftrace_update_trampoline(op);
  336. }
  337. } while_for_each_ftrace_op(op);
  338. fgraph_update_pid_func();
  339. update_ftrace_function();
  340. }
  341. #ifdef CONFIG_FUNCTION_PROFILER
  342. struct ftrace_profile {
  343. struct hlist_node node;
  344. unsigned long ip;
  345. unsigned long counter;
  346. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  347. unsigned long long time;
  348. unsigned long long time_squared;
  349. #endif
  350. };
  351. struct ftrace_profile_page {
  352. struct ftrace_profile_page *next;
  353. unsigned long index;
  354. struct ftrace_profile records[];
  355. };
  356. struct ftrace_profile_stat {
  357. atomic_t disabled;
  358. struct hlist_head *hash;
  359. struct ftrace_profile_page *pages;
  360. struct ftrace_profile_page *start;
  361. struct tracer_stat stat;
  362. };
  363. #define PROFILE_RECORDS_SIZE \
  364. (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
  365. #define PROFILES_PER_PAGE \
  366. (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
  367. static int ftrace_profile_enabled __read_mostly;
  368. /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
  369. static DEFINE_MUTEX(ftrace_profile_lock);
  370. static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
  371. #define FTRACE_PROFILE_HASH_BITS 10
  372. #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
  373. static void *
  374. function_stat_next(void *v, int idx)
  375. {
  376. struct ftrace_profile *rec = v;
  377. struct ftrace_profile_page *pg;
  378. pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
  379. again:
  380. if (idx != 0)
  381. rec++;
  382. if ((void *)rec >= (void *)&pg->records[pg->index]) {
  383. pg = pg->next;
  384. if (!pg)
  385. return NULL;
  386. rec = &pg->records[0];
  387. if (!rec->counter)
  388. goto again;
  389. }
  390. return rec;
  391. }
  392. static void *function_stat_start(struct tracer_stat *trace)
  393. {
  394. struct ftrace_profile_stat *stat =
  395. container_of(trace, struct ftrace_profile_stat, stat);
  396. if (!stat || !stat->start)
  397. return NULL;
  398. return function_stat_next(&stat->start->records[0], 0);
  399. }
  400. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  401. /* function graph compares on total time */
  402. static int function_stat_cmp(const void *p1, const void *p2)
  403. {
  404. const struct ftrace_profile *a = p1;
  405. const struct ftrace_profile *b = p2;
  406. if (a->time < b->time)
  407. return -1;
  408. if (a->time > b->time)
  409. return 1;
  410. else
  411. return 0;
  412. }
  413. #else
  414. /* not function graph compares against hits */
  415. static int function_stat_cmp(const void *p1, const void *p2)
  416. {
  417. const struct ftrace_profile *a = p1;
  418. const struct ftrace_profile *b = p2;
  419. if (a->counter < b->counter)
  420. return -1;
  421. if (a->counter > b->counter)
  422. return 1;
  423. else
  424. return 0;
  425. }
  426. #endif
  427. static int function_stat_headers(struct seq_file *m)
  428. {
  429. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  430. seq_puts(m, " Function "
  431. "Hit Time Avg s^2\n"
  432. " -------- "
  433. "--- ---- --- ---\n");
  434. #else
  435. seq_puts(m, " Function Hit\n"
  436. " -------- ---\n");
  437. #endif
  438. return 0;
  439. }
  440. static int function_stat_show(struct seq_file *m, void *v)
  441. {
  442. struct ftrace_profile *rec = v;
  443. char str[KSYM_SYMBOL_LEN];
  444. int ret = 0;
  445. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  446. static struct trace_seq s;
  447. unsigned long long avg;
  448. unsigned long long stddev;
  449. unsigned long long stddev_denom;
  450. #endif
  451. mutex_lock(&ftrace_profile_lock);
  452. /* we raced with function_profile_reset() */
  453. if (unlikely(rec->counter == 0)) {
  454. ret = -EBUSY;
  455. goto out;
  456. }
  457. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  458. avg = div64_ul(rec->time, rec->counter);
  459. if (tracing_thresh && (avg < tracing_thresh))
  460. goto out;
  461. #endif
  462. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  463. seq_printf(m, " %-30.30s %10lu", str, rec->counter);
  464. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  465. seq_puts(m, " ");
  466. /*
  467. * Variance formula:
  468. * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
  469. * Maybe Welford's method is better here?
  470. * Divide only by 1000 for ns^2 -> us^2 conversion.
  471. * trace_print_graph_duration will divide by 1000 again.
  472. */
  473. stddev = 0;
  474. stddev_denom = rec->counter * (rec->counter - 1) * 1000;
  475. if (stddev_denom) {
  476. stddev = rec->counter * rec->time_squared -
  477. rec->time * rec->time;
  478. stddev = div64_ul(stddev, stddev_denom);
  479. }
  480. trace_seq_init(&s);
  481. trace_print_graph_duration(rec->time, &s);
  482. trace_seq_puts(&s, " ");
  483. trace_print_graph_duration(avg, &s);
  484. trace_seq_puts(&s, " ");
  485. trace_print_graph_duration(stddev, &s);
  486. trace_print_seq(m, &s);
  487. #endif
  488. seq_putc(m, '\n');
  489. out:
  490. mutex_unlock(&ftrace_profile_lock);
  491. return ret;
  492. }
  493. static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
  494. {
  495. struct ftrace_profile_page *pg;
  496. pg = stat->pages = stat->start;
  497. while (pg) {
  498. memset(pg->records, 0, PROFILE_RECORDS_SIZE);
  499. pg->index = 0;
  500. pg = pg->next;
  501. }
  502. memset(stat->hash, 0,
  503. FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
  504. }
  505. static int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
  506. {
  507. struct ftrace_profile_page *pg;
  508. int functions;
  509. int pages;
  510. int i;
  511. /* If we already allocated, do nothing */
  512. if (stat->pages)
  513. return 0;
  514. stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
  515. if (!stat->pages)
  516. return -ENOMEM;
  517. #ifdef CONFIG_DYNAMIC_FTRACE
  518. functions = ftrace_update_tot_cnt;
  519. #else
  520. /*
  521. * We do not know the number of functions that exist because
  522. * dynamic tracing is what counts them. With past experience
  523. * we have around 20K functions. That should be more than enough.
  524. * It is highly unlikely we will execute every function in
  525. * the kernel.
  526. */
  527. functions = 20000;
  528. #endif
  529. pg = stat->start = stat->pages;
  530. pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
  531. for (i = 1; i < pages; i++) {
  532. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  533. if (!pg->next)
  534. goto out_free;
  535. pg = pg->next;
  536. }
  537. return 0;
  538. out_free:
  539. pg = stat->start;
  540. while (pg) {
  541. unsigned long tmp = (unsigned long)pg;
  542. pg = pg->next;
  543. free_page(tmp);
  544. }
  545. stat->pages = NULL;
  546. stat->start = NULL;
  547. return -ENOMEM;
  548. }
  549. static int ftrace_profile_init_cpu(int cpu)
  550. {
  551. struct ftrace_profile_stat *stat;
  552. int size;
  553. stat = &per_cpu(ftrace_profile_stats, cpu);
  554. if (stat->hash) {
  555. /* If the profile is already created, simply reset it */
  556. ftrace_profile_reset(stat);
  557. return 0;
  558. }
  559. /*
  560. * We are profiling all functions, but usually only a few thousand
  561. * functions are hit. We'll make a hash of 1024 items.
  562. */
  563. size = FTRACE_PROFILE_HASH_SIZE;
  564. stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL);
  565. if (!stat->hash)
  566. return -ENOMEM;
  567. /* Preallocate the function profiling pages */
  568. if (ftrace_profile_pages_init(stat) < 0) {
  569. kfree(stat->hash);
  570. stat->hash = NULL;
  571. return -ENOMEM;
  572. }
  573. return 0;
  574. }
  575. static int ftrace_profile_init(void)
  576. {
  577. int cpu;
  578. int ret = 0;
  579. for_each_possible_cpu(cpu) {
  580. ret = ftrace_profile_init_cpu(cpu);
  581. if (ret)
  582. break;
  583. }
  584. return ret;
  585. }
  586. /* interrupts must be disabled */
  587. static struct ftrace_profile *
  588. ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
  589. {
  590. struct ftrace_profile *rec;
  591. struct hlist_head *hhd;
  592. unsigned long key;
  593. key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
  594. hhd = &stat->hash[key];
  595. if (hlist_empty(hhd))
  596. return NULL;
  597. hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
  598. if (rec->ip == ip)
  599. return rec;
  600. }
  601. return NULL;
  602. }
  603. static void ftrace_add_profile(struct ftrace_profile_stat *stat,
  604. struct ftrace_profile *rec)
  605. {
  606. unsigned long key;
  607. key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
  608. hlist_add_head_rcu(&rec->node, &stat->hash[key]);
  609. }
  610. /*
  611. * The memory is already allocated, this simply finds a new record to use.
  612. */
  613. static struct ftrace_profile *
  614. ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
  615. {
  616. struct ftrace_profile *rec = NULL;
  617. /* prevent recursion (from NMIs) */
  618. if (atomic_inc_return(&stat->disabled) != 1)
  619. goto out;
  620. /*
  621. * Try to find the function again since an NMI
  622. * could have added it
  623. */
  624. rec = ftrace_find_profiled_func(stat, ip);
  625. if (rec)
  626. goto out;
  627. if (stat->pages->index == PROFILES_PER_PAGE) {
  628. if (!stat->pages->next)
  629. goto out;
  630. stat->pages = stat->pages->next;
  631. }
  632. rec = &stat->pages->records[stat->pages->index++];
  633. rec->ip = ip;
  634. ftrace_add_profile(stat, rec);
  635. out:
  636. atomic_dec(&stat->disabled);
  637. return rec;
  638. }
  639. static void
  640. function_profile_call(unsigned long ip, unsigned long parent_ip,
  641. struct ftrace_ops *ops, struct ftrace_regs *fregs)
  642. {
  643. struct ftrace_profile_stat *stat;
  644. struct ftrace_profile *rec;
  645. unsigned long flags;
  646. if (!ftrace_profile_enabled)
  647. return;
  648. local_irq_save(flags);
  649. stat = this_cpu_ptr(&ftrace_profile_stats);
  650. if (!stat->hash || !ftrace_profile_enabled)
  651. goto out;
  652. rec = ftrace_find_profiled_func(stat, ip);
  653. if (!rec) {
  654. rec = ftrace_profile_alloc(stat, ip);
  655. if (!rec)
  656. goto out;
  657. }
  658. rec->counter++;
  659. out:
  660. local_irq_restore(flags);
  661. }
  662. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  663. static bool fgraph_graph_time = true;
  664. void ftrace_graph_graph_time_control(bool enable)
  665. {
  666. fgraph_graph_time = enable;
  667. }
  668. static int profile_graph_entry(struct ftrace_graph_ent *trace,
  669. struct fgraph_ops *gops)
  670. {
  671. struct ftrace_ret_stack *ret_stack;
  672. function_profile_call(trace->func, 0, NULL, NULL);
  673. /* If function graph is shutting down, ret_stack can be NULL */
  674. if (!current->ret_stack)
  675. return 0;
  676. ret_stack = ftrace_graph_get_ret_stack(current, 0);
  677. if (ret_stack)
  678. ret_stack->subtime = 0;
  679. return 1;
  680. }
  681. static void profile_graph_return(struct ftrace_graph_ret *trace,
  682. struct fgraph_ops *gops)
  683. {
  684. struct ftrace_ret_stack *ret_stack;
  685. struct ftrace_profile_stat *stat;
  686. unsigned long long calltime;
  687. struct ftrace_profile *rec;
  688. unsigned long flags;
  689. local_irq_save(flags);
  690. stat = this_cpu_ptr(&ftrace_profile_stats);
  691. if (!stat->hash || !ftrace_profile_enabled)
  692. goto out;
  693. /* If the calltime was zero'd ignore it */
  694. if (!trace->calltime)
  695. goto out;
  696. calltime = trace->rettime - trace->calltime;
  697. if (!fgraph_graph_time) {
  698. /* Append this call time to the parent time to subtract */
  699. ret_stack = ftrace_graph_get_ret_stack(current, 1);
  700. if (ret_stack)
  701. ret_stack->subtime += calltime;
  702. ret_stack = ftrace_graph_get_ret_stack(current, 0);
  703. if (ret_stack && ret_stack->subtime < calltime)
  704. calltime -= ret_stack->subtime;
  705. else
  706. calltime = 0;
  707. }
  708. rec = ftrace_find_profiled_func(stat, trace->func);
  709. if (rec) {
  710. rec->time += calltime;
  711. rec->time_squared += calltime * calltime;
  712. }
  713. out:
  714. local_irq_restore(flags);
  715. }
  716. static struct fgraph_ops fprofiler_ops = {
  717. .entryfunc = &profile_graph_entry,
  718. .retfunc = &profile_graph_return,
  719. };
  720. static int register_ftrace_profiler(void)
  721. {
  722. ftrace_ops_set_global_filter(&fprofiler_ops.ops);
  723. return register_ftrace_graph(&fprofiler_ops);
  724. }
  725. static void unregister_ftrace_profiler(void)
  726. {
  727. unregister_ftrace_graph(&fprofiler_ops);
  728. }
  729. #else
  730. static struct ftrace_ops ftrace_profile_ops __read_mostly = {
  731. .func = function_profile_call,
  732. };
  733. static int register_ftrace_profiler(void)
  734. {
  735. ftrace_ops_set_global_filter(&ftrace_profile_ops);
  736. return register_ftrace_function(&ftrace_profile_ops);
  737. }
  738. static void unregister_ftrace_profiler(void)
  739. {
  740. unregister_ftrace_function(&ftrace_profile_ops);
  741. }
  742. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  743. static ssize_t
  744. ftrace_profile_write(struct file *filp, const char __user *ubuf,
  745. size_t cnt, loff_t *ppos)
  746. {
  747. unsigned long val;
  748. int ret;
  749. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  750. if (ret)
  751. return ret;
  752. val = !!val;
  753. mutex_lock(&ftrace_profile_lock);
  754. if (ftrace_profile_enabled ^ val) {
  755. if (val) {
  756. ret = ftrace_profile_init();
  757. if (ret < 0) {
  758. cnt = ret;
  759. goto out;
  760. }
  761. ret = register_ftrace_profiler();
  762. if (ret < 0) {
  763. cnt = ret;
  764. goto out;
  765. }
  766. ftrace_profile_enabled = 1;
  767. } else {
  768. ftrace_profile_enabled = 0;
  769. /*
  770. * unregister_ftrace_profiler calls stop_machine
  771. * so this acts like an synchronize_rcu.
  772. */
  773. unregister_ftrace_profiler();
  774. }
  775. }
  776. out:
  777. mutex_unlock(&ftrace_profile_lock);
  778. *ppos += cnt;
  779. return cnt;
  780. }
  781. static ssize_t
  782. ftrace_profile_read(struct file *filp, char __user *ubuf,
  783. size_t cnt, loff_t *ppos)
  784. {
  785. char buf[64]; /* big enough to hold a number */
  786. int r;
  787. r = sprintf(buf, "%u\n", ftrace_profile_enabled);
  788. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  789. }
  790. static const struct file_operations ftrace_profile_fops = {
  791. .open = tracing_open_generic,
  792. .read = ftrace_profile_read,
  793. .write = ftrace_profile_write,
  794. .llseek = default_llseek,
  795. };
  796. /* used to initialize the real stat files */
  797. static struct tracer_stat function_stats __initdata = {
  798. .name = "functions",
  799. .stat_start = function_stat_start,
  800. .stat_next = function_stat_next,
  801. .stat_cmp = function_stat_cmp,
  802. .stat_headers = function_stat_headers,
  803. .stat_show = function_stat_show
  804. };
  805. static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
  806. {
  807. struct ftrace_profile_stat *stat;
  808. char *name;
  809. int ret;
  810. int cpu;
  811. for_each_possible_cpu(cpu) {
  812. stat = &per_cpu(ftrace_profile_stats, cpu);
  813. name = kasprintf(GFP_KERNEL, "function%d", cpu);
  814. if (!name) {
  815. /*
  816. * The files created are permanent, if something happens
  817. * we still do not free memory.
  818. */
  819. WARN(1,
  820. "Could not allocate stat file for cpu %d\n",
  821. cpu);
  822. return;
  823. }
  824. stat->stat = function_stats;
  825. stat->stat.name = name;
  826. ret = register_stat_tracer(&stat->stat);
  827. if (ret) {
  828. WARN(1,
  829. "Could not register function stat for cpu %d\n",
  830. cpu);
  831. kfree(name);
  832. return;
  833. }
  834. }
  835. trace_create_file("function_profile_enabled",
  836. TRACE_MODE_WRITE, d_tracer, NULL,
  837. &ftrace_profile_fops);
  838. }
  839. #else /* CONFIG_FUNCTION_PROFILER */
  840. static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
  841. {
  842. }
  843. #endif /* CONFIG_FUNCTION_PROFILER */
  844. #ifdef CONFIG_DYNAMIC_FTRACE
  845. static struct ftrace_ops *removed_ops;
  846. /*
  847. * Set when doing a global update, like enabling all recs or disabling them.
  848. * It is not set when just updating a single ftrace_ops.
  849. */
  850. static bool update_all_ops;
  851. #ifndef CONFIG_FTRACE_MCOUNT_RECORD
  852. # error Dynamic ftrace depends on MCOUNT_RECORD
  853. #endif
  854. struct ftrace_func_probe {
  855. struct ftrace_probe_ops *probe_ops;
  856. struct ftrace_ops ops;
  857. struct trace_array *tr;
  858. struct list_head list;
  859. void *data;
  860. int ref;
  861. };
  862. /*
  863. * We make these constant because no one should touch them,
  864. * but they are used as the default "empty hash", to avoid allocating
  865. * it all the time. These are in a read only section such that if
  866. * anyone does try to modify it, it will cause an exception.
  867. */
  868. static const struct hlist_head empty_buckets[1];
  869. static const struct ftrace_hash empty_hash = {
  870. .buckets = (struct hlist_head *)empty_buckets,
  871. };
  872. #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
  873. struct ftrace_ops global_ops = {
  874. .func = ftrace_stub,
  875. .local_hash.notrace_hash = EMPTY_HASH,
  876. .local_hash.filter_hash = EMPTY_HASH,
  877. INIT_OPS_HASH(global_ops)
  878. .flags = FTRACE_OPS_FL_INITIALIZED |
  879. FTRACE_OPS_FL_PID,
  880. };
  881. /*
  882. * Used by the stack unwinder to know about dynamic ftrace trampolines.
  883. */
  884. struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr)
  885. {
  886. struct ftrace_ops *op = NULL;
  887. /*
  888. * Some of the ops may be dynamically allocated,
  889. * they are freed after a synchronize_rcu().
  890. */
  891. preempt_disable_notrace();
  892. do_for_each_ftrace_op(op, ftrace_ops_list) {
  893. /*
  894. * This is to check for dynamically allocated trampolines.
  895. * Trampolines that are in kernel text will have
  896. * core_kernel_text() return true.
  897. */
  898. if (op->trampoline && op->trampoline_size)
  899. if (addr >= op->trampoline &&
  900. addr < op->trampoline + op->trampoline_size) {
  901. preempt_enable_notrace();
  902. return op;
  903. }
  904. } while_for_each_ftrace_op(op);
  905. preempt_enable_notrace();
  906. return NULL;
  907. }
  908. /*
  909. * This is used by __kernel_text_address() to return true if the
  910. * address is on a dynamically allocated trampoline that would
  911. * not return true for either core_kernel_text() or
  912. * is_module_text_address().
  913. */
  914. bool is_ftrace_trampoline(unsigned long addr)
  915. {
  916. return ftrace_ops_trampoline(addr) != NULL;
  917. }
  918. struct ftrace_page {
  919. struct ftrace_page *next;
  920. struct dyn_ftrace *records;
  921. int index;
  922. int order;
  923. };
  924. #define ENTRY_SIZE sizeof(struct dyn_ftrace)
  925. #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
  926. static struct ftrace_page *ftrace_pages_start;
  927. static struct ftrace_page *ftrace_pages;
  928. static __always_inline unsigned long
  929. ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
  930. {
  931. if (hash->size_bits > 0)
  932. return hash_long(ip, hash->size_bits);
  933. return 0;
  934. }
  935. /* Only use this function if ftrace_hash_empty() has already been tested */
  936. static __always_inline struct ftrace_func_entry *
  937. __ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
  938. {
  939. unsigned long key;
  940. struct ftrace_func_entry *entry;
  941. struct hlist_head *hhd;
  942. key = ftrace_hash_key(hash, ip);
  943. hhd = &hash->buckets[key];
  944. hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
  945. if (entry->ip == ip)
  946. return entry;
  947. }
  948. return NULL;
  949. }
  950. /**
  951. * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
  952. * @hash: The hash to look at
  953. * @ip: The instruction pointer to test
  954. *
  955. * Search a given @hash to see if a given instruction pointer (@ip)
  956. * exists in it.
  957. *
  958. * Returns: the entry that holds the @ip if found. NULL otherwise.
  959. */
  960. struct ftrace_func_entry *
  961. ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
  962. {
  963. if (ftrace_hash_empty(hash))
  964. return NULL;
  965. return __ftrace_lookup_ip(hash, ip);
  966. }
  967. static void __add_hash_entry(struct ftrace_hash *hash,
  968. struct ftrace_func_entry *entry)
  969. {
  970. struct hlist_head *hhd;
  971. unsigned long key;
  972. key = ftrace_hash_key(hash, entry->ip);
  973. hhd = &hash->buckets[key];
  974. hlist_add_head(&entry->hlist, hhd);
  975. hash->count++;
  976. }
  977. static struct ftrace_func_entry *
  978. add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
  979. {
  980. struct ftrace_func_entry *entry;
  981. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  982. if (!entry)
  983. return NULL;
  984. entry->ip = ip;
  985. __add_hash_entry(hash, entry);
  986. return entry;
  987. }
  988. static void
  989. free_hash_entry(struct ftrace_hash *hash,
  990. struct ftrace_func_entry *entry)
  991. {
  992. hlist_del(&entry->hlist);
  993. kfree(entry);
  994. hash->count--;
  995. }
  996. static void
  997. remove_hash_entry(struct ftrace_hash *hash,
  998. struct ftrace_func_entry *entry)
  999. {
  1000. hlist_del_rcu(&entry->hlist);
  1001. hash->count--;
  1002. }
  1003. static void ftrace_hash_clear(struct ftrace_hash *hash)
  1004. {
  1005. struct hlist_head *hhd;
  1006. struct hlist_node *tn;
  1007. struct ftrace_func_entry *entry;
  1008. int size = 1 << hash->size_bits;
  1009. int i;
  1010. if (!hash->count)
  1011. return;
  1012. for (i = 0; i < size; i++) {
  1013. hhd = &hash->buckets[i];
  1014. hlist_for_each_entry_safe(entry, tn, hhd, hlist)
  1015. free_hash_entry(hash, entry);
  1016. }
  1017. FTRACE_WARN_ON(hash->count);
  1018. }
  1019. static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
  1020. {
  1021. list_del(&ftrace_mod->list);
  1022. kfree(ftrace_mod->module);
  1023. kfree(ftrace_mod->func);
  1024. kfree(ftrace_mod);
  1025. }
  1026. static void clear_ftrace_mod_list(struct list_head *head)
  1027. {
  1028. struct ftrace_mod_load *p, *n;
  1029. /* stack tracer isn't supported yet */
  1030. if (!head)
  1031. return;
  1032. mutex_lock(&ftrace_lock);
  1033. list_for_each_entry_safe(p, n, head, list)
  1034. free_ftrace_mod(p);
  1035. mutex_unlock(&ftrace_lock);
  1036. }
  1037. static void free_ftrace_hash(struct ftrace_hash *hash)
  1038. {
  1039. if (!hash || hash == EMPTY_HASH)
  1040. return;
  1041. ftrace_hash_clear(hash);
  1042. kfree(hash->buckets);
  1043. kfree(hash);
  1044. }
  1045. static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
  1046. {
  1047. struct ftrace_hash *hash;
  1048. hash = container_of(rcu, struct ftrace_hash, rcu);
  1049. free_ftrace_hash(hash);
  1050. }
  1051. static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
  1052. {
  1053. if (!hash || hash == EMPTY_HASH)
  1054. return;
  1055. call_rcu(&hash->rcu, __free_ftrace_hash_rcu);
  1056. }
  1057. /**
  1058. * ftrace_free_filter - remove all filters for an ftrace_ops
  1059. * @ops: the ops to remove the filters from
  1060. */
  1061. void ftrace_free_filter(struct ftrace_ops *ops)
  1062. {
  1063. ftrace_ops_init(ops);
  1064. free_ftrace_hash(ops->func_hash->filter_hash);
  1065. free_ftrace_hash(ops->func_hash->notrace_hash);
  1066. }
  1067. EXPORT_SYMBOL_GPL(ftrace_free_filter);
  1068. static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
  1069. {
  1070. struct ftrace_hash *hash;
  1071. int size;
  1072. hash = kzalloc(sizeof(*hash), GFP_KERNEL);
  1073. if (!hash)
  1074. return NULL;
  1075. size = 1 << size_bits;
  1076. hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
  1077. if (!hash->buckets) {
  1078. kfree(hash);
  1079. return NULL;
  1080. }
  1081. hash->size_bits = size_bits;
  1082. return hash;
  1083. }
  1084. /* Used to save filters on functions for modules not loaded yet */
  1085. static int ftrace_add_mod(struct trace_array *tr,
  1086. const char *func, const char *module,
  1087. int enable)
  1088. {
  1089. struct ftrace_mod_load *ftrace_mod;
  1090. struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
  1091. ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
  1092. if (!ftrace_mod)
  1093. return -ENOMEM;
  1094. INIT_LIST_HEAD(&ftrace_mod->list);
  1095. ftrace_mod->func = kstrdup(func, GFP_KERNEL);
  1096. ftrace_mod->module = kstrdup(module, GFP_KERNEL);
  1097. ftrace_mod->enable = enable;
  1098. if (!ftrace_mod->func || !ftrace_mod->module)
  1099. goto out_free;
  1100. list_add(&ftrace_mod->list, mod_head);
  1101. return 0;
  1102. out_free:
  1103. free_ftrace_mod(ftrace_mod);
  1104. return -ENOMEM;
  1105. }
  1106. static struct ftrace_hash *
  1107. alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
  1108. {
  1109. struct ftrace_func_entry *entry;
  1110. struct ftrace_hash *new_hash;
  1111. int size;
  1112. int i;
  1113. new_hash = alloc_ftrace_hash(size_bits);
  1114. if (!new_hash)
  1115. return NULL;
  1116. if (hash)
  1117. new_hash->flags = hash->flags;
  1118. /* Empty hash? */
  1119. if (ftrace_hash_empty(hash))
  1120. return new_hash;
  1121. size = 1 << hash->size_bits;
  1122. for (i = 0; i < size; i++) {
  1123. hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
  1124. if (add_hash_entry(new_hash, entry->ip) == NULL)
  1125. goto free_hash;
  1126. }
  1127. }
  1128. FTRACE_WARN_ON(new_hash->count != hash->count);
  1129. return new_hash;
  1130. free_hash:
  1131. free_ftrace_hash(new_hash);
  1132. return NULL;
  1133. }
  1134. static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops);
  1135. static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops);
  1136. static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
  1137. struct ftrace_hash *new_hash);
  1138. /*
  1139. * Allocate a new hash and remove entries from @src and move them to the new hash.
  1140. * On success, the @src hash will be empty and should be freed.
  1141. */
  1142. static struct ftrace_hash *__move_hash(struct ftrace_hash *src, int size)
  1143. {
  1144. struct ftrace_func_entry *entry;
  1145. struct ftrace_hash *new_hash;
  1146. struct hlist_head *hhd;
  1147. struct hlist_node *tn;
  1148. int bits = 0;
  1149. int i;
  1150. /*
  1151. * Use around half the size (max bit of it), but
  1152. * a minimum of 2 is fine (as size of 0 or 1 both give 1 for bits).
  1153. */
  1154. bits = fls(size / 2);
  1155. /* Don't allocate too much */
  1156. if (bits > FTRACE_HASH_MAX_BITS)
  1157. bits = FTRACE_HASH_MAX_BITS;
  1158. new_hash = alloc_ftrace_hash(bits);
  1159. if (!new_hash)
  1160. return NULL;
  1161. new_hash->flags = src->flags;
  1162. size = 1 << src->size_bits;
  1163. for (i = 0; i < size; i++) {
  1164. hhd = &src->buckets[i];
  1165. hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
  1166. remove_hash_entry(src, entry);
  1167. __add_hash_entry(new_hash, entry);
  1168. }
  1169. }
  1170. return new_hash;
  1171. }
  1172. /* Move the @src entries to a newly allocated hash */
  1173. static struct ftrace_hash *
  1174. __ftrace_hash_move(struct ftrace_hash *src)
  1175. {
  1176. int size = src->count;
  1177. /*
  1178. * If the new source is empty, just return the empty_hash.
  1179. */
  1180. if (ftrace_hash_empty(src))
  1181. return EMPTY_HASH;
  1182. return __move_hash(src, size);
  1183. }
  1184. /**
  1185. * ftrace_hash_move - move a new hash to a filter and do updates
  1186. * @ops: The ops with the hash that @dst points to
  1187. * @enable: True if for the filter hash, false for the notrace hash
  1188. * @dst: Points to the @ops hash that should be updated
  1189. * @src: The hash to update @dst with
  1190. *
  1191. * This is called when an ftrace_ops hash is being updated and the
  1192. * the kernel needs to reflect this. Note, this only updates the kernel
  1193. * function callbacks if the @ops is enabled (not to be confused with
  1194. * @enable above). If the @ops is enabled, its hash determines what
  1195. * callbacks get called. This function gets called when the @ops hash
  1196. * is updated and it requires new callbacks.
  1197. *
  1198. * On success the elements of @src is moved to @dst, and @dst is updated
  1199. * properly, as well as the functions determined by the @ops hashes
  1200. * are now calling the @ops callback function.
  1201. *
  1202. * Regardless of return type, @src should be freed with free_ftrace_hash().
  1203. */
  1204. static int
  1205. ftrace_hash_move(struct ftrace_ops *ops, int enable,
  1206. struct ftrace_hash **dst, struct ftrace_hash *src)
  1207. {
  1208. struct ftrace_hash *new_hash;
  1209. int ret;
  1210. /* Reject setting notrace hash on IPMODIFY ftrace_ops */
  1211. if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
  1212. return -EINVAL;
  1213. new_hash = __ftrace_hash_move(src);
  1214. if (!new_hash)
  1215. return -ENOMEM;
  1216. /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
  1217. if (enable) {
  1218. /* IPMODIFY should be updated only when filter_hash updating */
  1219. ret = ftrace_hash_ipmodify_update(ops, new_hash);
  1220. if (ret < 0) {
  1221. free_ftrace_hash(new_hash);
  1222. return ret;
  1223. }
  1224. }
  1225. /*
  1226. * Remove the current set, update the hash and add
  1227. * them back.
  1228. */
  1229. ftrace_hash_rec_disable_modify(ops);
  1230. rcu_assign_pointer(*dst, new_hash);
  1231. ftrace_hash_rec_enable_modify(ops);
  1232. return 0;
  1233. }
  1234. static bool hash_contains_ip(unsigned long ip,
  1235. struct ftrace_ops_hash *hash)
  1236. {
  1237. /*
  1238. * The function record is a match if it exists in the filter
  1239. * hash and not in the notrace hash. Note, an empty hash is
  1240. * considered a match for the filter hash, but an empty
  1241. * notrace hash is considered not in the notrace hash.
  1242. */
  1243. return (ftrace_hash_empty(hash->filter_hash) ||
  1244. __ftrace_lookup_ip(hash->filter_hash, ip)) &&
  1245. (ftrace_hash_empty(hash->notrace_hash) ||
  1246. !__ftrace_lookup_ip(hash->notrace_hash, ip));
  1247. }
  1248. /*
  1249. * Test the hashes for this ops to see if we want to call
  1250. * the ops->func or not.
  1251. *
  1252. * It's a match if the ip is in the ops->filter_hash or
  1253. * the filter_hash does not exist or is empty,
  1254. * AND
  1255. * the ip is not in the ops->notrace_hash.
  1256. *
  1257. * This needs to be called with preemption disabled as
  1258. * the hashes are freed with call_rcu().
  1259. */
  1260. int
  1261. ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
  1262. {
  1263. struct ftrace_ops_hash hash;
  1264. int ret;
  1265. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  1266. /*
  1267. * There's a small race when adding ops that the ftrace handler
  1268. * that wants regs, may be called without them. We can not
  1269. * allow that handler to be called if regs is NULL.
  1270. */
  1271. if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
  1272. return 0;
  1273. #endif
  1274. rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
  1275. rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
  1276. if (hash_contains_ip(ip, &hash))
  1277. ret = 1;
  1278. else
  1279. ret = 0;
  1280. return ret;
  1281. }
  1282. /*
  1283. * This is a double for. Do not use 'break' to break out of the loop,
  1284. * you must use a goto.
  1285. */
  1286. #define do_for_each_ftrace_rec(pg, rec) \
  1287. for (pg = ftrace_pages_start; pg; pg = pg->next) { \
  1288. int _____i; \
  1289. for (_____i = 0; _____i < pg->index; _____i++) { \
  1290. rec = &pg->records[_____i];
  1291. #define while_for_each_ftrace_rec() \
  1292. } \
  1293. }
  1294. static int ftrace_cmp_recs(const void *a, const void *b)
  1295. {
  1296. const struct dyn_ftrace *key = a;
  1297. const struct dyn_ftrace *rec = b;
  1298. if (key->flags < rec->ip)
  1299. return -1;
  1300. if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
  1301. return 1;
  1302. return 0;
  1303. }
  1304. static struct dyn_ftrace *lookup_rec(unsigned long start, unsigned long end)
  1305. {
  1306. struct ftrace_page *pg;
  1307. struct dyn_ftrace *rec = NULL;
  1308. struct dyn_ftrace key;
  1309. key.ip = start;
  1310. key.flags = end; /* overload flags, as it is unsigned long */
  1311. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  1312. if (pg->index == 0 ||
  1313. end < pg->records[0].ip ||
  1314. start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
  1315. continue;
  1316. rec = bsearch(&key, pg->records, pg->index,
  1317. sizeof(struct dyn_ftrace),
  1318. ftrace_cmp_recs);
  1319. if (rec)
  1320. break;
  1321. }
  1322. return rec;
  1323. }
  1324. /**
  1325. * ftrace_location_range - return the first address of a traced location
  1326. * if it touches the given ip range
  1327. * @start: start of range to search.
  1328. * @end: end of range to search (inclusive). @end points to the last byte
  1329. * to check.
  1330. *
  1331. * Returns: rec->ip if the related ftrace location is a least partly within
  1332. * the given address range. That is, the first address of the instruction
  1333. * that is either a NOP or call to the function tracer. It checks the ftrace
  1334. * internal tables to determine if the address belongs or not.
  1335. */
  1336. unsigned long ftrace_location_range(unsigned long start, unsigned long end)
  1337. {
  1338. struct dyn_ftrace *rec;
  1339. unsigned long ip = 0;
  1340. rcu_read_lock();
  1341. rec = lookup_rec(start, end);
  1342. if (rec)
  1343. ip = rec->ip;
  1344. rcu_read_unlock();
  1345. return ip;
  1346. }
  1347. /**
  1348. * ftrace_location - return the ftrace location
  1349. * @ip: the instruction pointer to check
  1350. *
  1351. * Returns:
  1352. * * If @ip matches the ftrace location, return @ip.
  1353. * * If @ip matches sym+0, return sym's ftrace location.
  1354. * * Otherwise, return 0.
  1355. */
  1356. unsigned long ftrace_location(unsigned long ip)
  1357. {
  1358. unsigned long loc;
  1359. unsigned long offset;
  1360. unsigned long size;
  1361. loc = ftrace_location_range(ip, ip);
  1362. if (!loc) {
  1363. if (!kallsyms_lookup_size_offset(ip, &size, &offset))
  1364. goto out;
  1365. /* map sym+0 to __fentry__ */
  1366. if (!offset)
  1367. loc = ftrace_location_range(ip, ip + size - 1);
  1368. }
  1369. out:
  1370. return loc;
  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 skip_record(struct dyn_ftrace *rec)
  1413. {
  1414. /*
  1415. * At boot up, weak functions are set to disable. Function tracing
  1416. * can be enabled before they are, and they still need to be disabled now.
  1417. * If the record is disabled, still continue if it is marked as already
  1418. * enabled (this is needed to keep the accounting working).
  1419. */
  1420. return rec->flags & FTRACE_FL_DISABLED &&
  1421. !(rec->flags & FTRACE_FL_ENABLED);
  1422. }
  1423. /*
  1424. * This is the main engine to the ftrace updates to the dyn_ftrace records.
  1425. *
  1426. * It will iterate through all the available ftrace functions
  1427. * (the ones that ftrace can have callbacks to) and set the flags
  1428. * in the associated dyn_ftrace records.
  1429. *
  1430. * @inc: If true, the functions associated to @ops are added to
  1431. * the dyn_ftrace records, otherwise they are removed.
  1432. */
  1433. static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
  1434. bool inc)
  1435. {
  1436. struct ftrace_hash *hash;
  1437. struct ftrace_hash *notrace_hash;
  1438. struct ftrace_page *pg;
  1439. struct dyn_ftrace *rec;
  1440. bool update = false;
  1441. int count = 0;
  1442. int all = false;
  1443. /* Only update if the ops has been registered */
  1444. if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
  1445. return false;
  1446. /*
  1447. * If the count is zero, we update all records.
  1448. * Otherwise we just update the items in the hash.
  1449. */
  1450. hash = ops->func_hash->filter_hash;
  1451. notrace_hash = ops->func_hash->notrace_hash;
  1452. if (ftrace_hash_empty(hash))
  1453. all = true;
  1454. do_for_each_ftrace_rec(pg, rec) {
  1455. int in_notrace_hash = 0;
  1456. int in_hash = 0;
  1457. int match = 0;
  1458. if (skip_record(rec))
  1459. continue;
  1460. if (all) {
  1461. /*
  1462. * Only the filter_hash affects all records.
  1463. * Update if the record is not in the notrace hash.
  1464. */
  1465. if (!notrace_hash || !ftrace_lookup_ip(notrace_hash, rec->ip))
  1466. match = 1;
  1467. } else {
  1468. in_hash = !!ftrace_lookup_ip(hash, rec->ip);
  1469. in_notrace_hash = !!ftrace_lookup_ip(notrace_hash, rec->ip);
  1470. /*
  1471. * We want to match all functions that are in the hash but
  1472. * not in the other hash.
  1473. */
  1474. if (in_hash && !in_notrace_hash)
  1475. match = 1;
  1476. }
  1477. if (!match)
  1478. continue;
  1479. if (inc) {
  1480. rec->flags++;
  1481. if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
  1482. return false;
  1483. if (ops->flags & FTRACE_OPS_FL_DIRECT)
  1484. rec->flags |= FTRACE_FL_DIRECT;
  1485. /*
  1486. * If there's only a single callback registered to a
  1487. * function, and the ops has a trampoline registered
  1488. * for it, then we can call it directly.
  1489. */
  1490. if (ftrace_rec_count(rec) == 1 && ops->trampoline)
  1491. rec->flags |= FTRACE_FL_TRAMP;
  1492. else
  1493. /*
  1494. * If we are adding another function callback
  1495. * to this function, and the previous had a
  1496. * custom trampoline in use, then we need to go
  1497. * back to the default trampoline.
  1498. */
  1499. rec->flags &= ~FTRACE_FL_TRAMP;
  1500. /*
  1501. * If any ops wants regs saved for this function
  1502. * then all ops will get saved regs.
  1503. */
  1504. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
  1505. rec->flags |= FTRACE_FL_REGS;
  1506. } else {
  1507. if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
  1508. return false;
  1509. rec->flags--;
  1510. /*
  1511. * Only the internal direct_ops should have the
  1512. * DIRECT flag set. Thus, if it is removing a
  1513. * function, then that function should no longer
  1514. * be direct.
  1515. */
  1516. if (ops->flags & FTRACE_OPS_FL_DIRECT)
  1517. rec->flags &= ~FTRACE_FL_DIRECT;
  1518. /*
  1519. * If the rec had REGS enabled and the ops that is
  1520. * being removed had REGS set, then see if there is
  1521. * still any ops for this record that wants regs.
  1522. * If not, we can stop recording them.
  1523. */
  1524. if (ftrace_rec_count(rec) > 0 &&
  1525. rec->flags & FTRACE_FL_REGS &&
  1526. ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
  1527. if (!test_rec_ops_needs_regs(rec))
  1528. rec->flags &= ~FTRACE_FL_REGS;
  1529. }
  1530. /*
  1531. * The TRAMP needs to be set only if rec count
  1532. * is decremented to one, and the ops that is
  1533. * left has a trampoline. As TRAMP can only be
  1534. * enabled if there is only a single ops attached
  1535. * to it.
  1536. */
  1537. if (ftrace_rec_count(rec) == 1 &&
  1538. ftrace_find_tramp_ops_any_other(rec, ops))
  1539. rec->flags |= FTRACE_FL_TRAMP;
  1540. else
  1541. rec->flags &= ~FTRACE_FL_TRAMP;
  1542. /*
  1543. * flags will be cleared in ftrace_check_record()
  1544. * if rec count is zero.
  1545. */
  1546. }
  1547. /*
  1548. * If the rec has a single associated ops, and ops->func can be
  1549. * called directly, allow the call site to call via the ops.
  1550. */
  1551. if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS) &&
  1552. ftrace_rec_count(rec) == 1 &&
  1553. ftrace_ops_get_func(ops) == ops->func)
  1554. rec->flags |= FTRACE_FL_CALL_OPS;
  1555. else
  1556. rec->flags &= ~FTRACE_FL_CALL_OPS;
  1557. count++;
  1558. /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
  1559. update |= ftrace_test_record(rec, true) != FTRACE_UPDATE_IGNORE;
  1560. /* Shortcut, if we handled all records, we are done. */
  1561. if (!all && count == hash->count)
  1562. return update;
  1563. } while_for_each_ftrace_rec();
  1564. return update;
  1565. }
  1566. /*
  1567. * This is called when an ops is removed from tracing. It will decrement
  1568. * the counters of the dyn_ftrace records for all the functions that
  1569. * the @ops attached to.
  1570. */
  1571. static bool ftrace_hash_rec_disable(struct ftrace_ops *ops)
  1572. {
  1573. return __ftrace_hash_rec_update(ops, false);
  1574. }
  1575. /*
  1576. * This is called when an ops is added to tracing. It will increment
  1577. * the counters of the dyn_ftrace records for all the functions that
  1578. * the @ops attached to.
  1579. */
  1580. static bool ftrace_hash_rec_enable(struct ftrace_ops *ops)
  1581. {
  1582. return __ftrace_hash_rec_update(ops, true);
  1583. }
  1584. /*
  1585. * This function will update what functions @ops traces when its filter
  1586. * changes.
  1587. *
  1588. * The @inc states if the @ops callbacks are going to be added or removed.
  1589. * When one of the @ops hashes are updated to a "new_hash" the dyn_ftrace
  1590. * records are update via:
  1591. *
  1592. * ftrace_hash_rec_disable_modify(ops);
  1593. * ops->hash = new_hash
  1594. * ftrace_hash_rec_enable_modify(ops);
  1595. *
  1596. * Where the @ops is removed from all the records it is tracing using
  1597. * its old hash. The @ops hash is updated to the new hash, and then
  1598. * the @ops is added back to the records so that it is tracing all
  1599. * the new functions.
  1600. */
  1601. static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops, bool inc)
  1602. {
  1603. struct ftrace_ops *op;
  1604. __ftrace_hash_rec_update(ops, inc);
  1605. if (ops->func_hash != &global_ops.local_hash)
  1606. return;
  1607. /*
  1608. * If the ops shares the global_ops hash, then we need to update
  1609. * all ops that are enabled and use this hash.
  1610. */
  1611. do_for_each_ftrace_op(op, ftrace_ops_list) {
  1612. /* Already done */
  1613. if (op == ops)
  1614. continue;
  1615. if (op->func_hash == &global_ops.local_hash)
  1616. __ftrace_hash_rec_update(op, inc);
  1617. } while_for_each_ftrace_op(op);
  1618. }
  1619. static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops)
  1620. {
  1621. ftrace_hash_rec_update_modify(ops, false);
  1622. }
  1623. static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops)
  1624. {
  1625. ftrace_hash_rec_update_modify(ops, true);
  1626. }
  1627. /*
  1628. * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
  1629. * or no-needed to update, -EBUSY if it detects a conflict of the flag
  1630. * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
  1631. * Note that old_hash and new_hash has below meanings
  1632. * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
  1633. * - If the hash is EMPTY_HASH, it hits nothing
  1634. * - Anything else hits the recs which match the hash entries.
  1635. *
  1636. * DIRECT ops does not have IPMODIFY flag, but we still need to check it
  1637. * against functions with FTRACE_FL_IPMODIFY. If there is any overlap, call
  1638. * ops_func(SHARE_IPMODIFY_SELF) to make sure current ops can share with
  1639. * IPMODIFY. If ops_func(SHARE_IPMODIFY_SELF) returns non-zero, propagate
  1640. * the return value to the caller and eventually to the owner of the DIRECT
  1641. * ops.
  1642. */
  1643. static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
  1644. struct ftrace_hash *old_hash,
  1645. struct ftrace_hash *new_hash)
  1646. {
  1647. struct ftrace_page *pg;
  1648. struct dyn_ftrace *rec, *end = NULL;
  1649. int in_old, in_new;
  1650. bool is_ipmodify, is_direct;
  1651. /* Only update if the ops has been registered */
  1652. if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
  1653. return 0;
  1654. is_ipmodify = ops->flags & FTRACE_OPS_FL_IPMODIFY;
  1655. is_direct = ops->flags & FTRACE_OPS_FL_DIRECT;
  1656. /* neither IPMODIFY nor DIRECT, skip */
  1657. if (!is_ipmodify && !is_direct)
  1658. return 0;
  1659. if (WARN_ON_ONCE(is_ipmodify && is_direct))
  1660. return 0;
  1661. /*
  1662. * Since the IPMODIFY and DIRECT are very address sensitive
  1663. * actions, we do not allow ftrace_ops to set all functions to new
  1664. * hash.
  1665. */
  1666. if (!new_hash || !old_hash)
  1667. return -EINVAL;
  1668. /* Update rec->flags */
  1669. do_for_each_ftrace_rec(pg, rec) {
  1670. if (rec->flags & FTRACE_FL_DISABLED)
  1671. continue;
  1672. /* We need to update only differences of filter_hash */
  1673. in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
  1674. in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
  1675. if (in_old == in_new)
  1676. continue;
  1677. if (in_new) {
  1678. if (rec->flags & FTRACE_FL_IPMODIFY) {
  1679. int ret;
  1680. /* Cannot have two ipmodify on same rec */
  1681. if (is_ipmodify)
  1682. goto rollback;
  1683. FTRACE_WARN_ON(rec->flags & FTRACE_FL_DIRECT);
  1684. /*
  1685. * Another ops with IPMODIFY is already
  1686. * attached. We are now attaching a direct
  1687. * ops. Run SHARE_IPMODIFY_SELF, to check
  1688. * whether sharing is supported.
  1689. */
  1690. if (!ops->ops_func)
  1691. return -EBUSY;
  1692. ret = ops->ops_func(ops, FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_SELF);
  1693. if (ret)
  1694. return ret;
  1695. } else if (is_ipmodify) {
  1696. rec->flags |= FTRACE_FL_IPMODIFY;
  1697. }
  1698. } else if (is_ipmodify) {
  1699. rec->flags &= ~FTRACE_FL_IPMODIFY;
  1700. }
  1701. } while_for_each_ftrace_rec();
  1702. return 0;
  1703. rollback:
  1704. end = rec;
  1705. /* Roll back what we did above */
  1706. do_for_each_ftrace_rec(pg, rec) {
  1707. if (rec->flags & FTRACE_FL_DISABLED)
  1708. continue;
  1709. if (rec == end)
  1710. goto err_out;
  1711. in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
  1712. in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
  1713. if (in_old == in_new)
  1714. continue;
  1715. if (in_new)
  1716. rec->flags &= ~FTRACE_FL_IPMODIFY;
  1717. else
  1718. rec->flags |= FTRACE_FL_IPMODIFY;
  1719. } while_for_each_ftrace_rec();
  1720. err_out:
  1721. return -EBUSY;
  1722. }
  1723. static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
  1724. {
  1725. struct ftrace_hash *hash = ops->func_hash->filter_hash;
  1726. if (ftrace_hash_empty(hash))
  1727. hash = NULL;
  1728. return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
  1729. }
  1730. /* Disabling always succeeds */
  1731. static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
  1732. {
  1733. struct ftrace_hash *hash = ops->func_hash->filter_hash;
  1734. if (ftrace_hash_empty(hash))
  1735. hash = NULL;
  1736. __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
  1737. }
  1738. static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
  1739. struct ftrace_hash *new_hash)
  1740. {
  1741. struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
  1742. if (ftrace_hash_empty(old_hash))
  1743. old_hash = NULL;
  1744. if (ftrace_hash_empty(new_hash))
  1745. new_hash = NULL;
  1746. return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
  1747. }
  1748. static void print_ip_ins(const char *fmt, const unsigned char *p)
  1749. {
  1750. char ins[MCOUNT_INSN_SIZE];
  1751. if (copy_from_kernel_nofault(ins, p, MCOUNT_INSN_SIZE)) {
  1752. printk(KERN_CONT "%s[FAULT] %px\n", fmt, p);
  1753. return;
  1754. }
  1755. printk(KERN_CONT "%s", fmt);
  1756. pr_cont("%*phC", MCOUNT_INSN_SIZE, ins);
  1757. }
  1758. enum ftrace_bug_type ftrace_bug_type;
  1759. const void *ftrace_expected;
  1760. static void print_bug_type(void)
  1761. {
  1762. switch (ftrace_bug_type) {
  1763. case FTRACE_BUG_UNKNOWN:
  1764. break;
  1765. case FTRACE_BUG_INIT:
  1766. pr_info("Initializing ftrace call sites\n");
  1767. break;
  1768. case FTRACE_BUG_NOP:
  1769. pr_info("Setting ftrace call site to NOP\n");
  1770. break;
  1771. case FTRACE_BUG_CALL:
  1772. pr_info("Setting ftrace call site to call ftrace function\n");
  1773. break;
  1774. case FTRACE_BUG_UPDATE:
  1775. pr_info("Updating ftrace call site to call a different ftrace function\n");
  1776. break;
  1777. }
  1778. }
  1779. /**
  1780. * ftrace_bug - report and shutdown function tracer
  1781. * @failed: The failed type (EFAULT, EINVAL, EPERM)
  1782. * @rec: The record that failed
  1783. *
  1784. * The arch code that enables or disables the function tracing
  1785. * can call ftrace_bug() when it has detected a problem in
  1786. * modifying the code. @failed should be one of either:
  1787. * EFAULT - if the problem happens on reading the @ip address
  1788. * EINVAL - if what is read at @ip is not what was expected
  1789. * EPERM - if the problem happens on writing to the @ip address
  1790. */
  1791. void ftrace_bug(int failed, struct dyn_ftrace *rec)
  1792. {
  1793. unsigned long ip = rec ? rec->ip : 0;
  1794. pr_info("------------[ ftrace bug ]------------\n");
  1795. switch (failed) {
  1796. case -EFAULT:
  1797. pr_info("ftrace faulted on modifying ");
  1798. print_ip_sym(KERN_INFO, ip);
  1799. break;
  1800. case -EINVAL:
  1801. pr_info("ftrace failed to modify ");
  1802. print_ip_sym(KERN_INFO, ip);
  1803. print_ip_ins(" actual: ", (unsigned char *)ip);
  1804. pr_cont("\n");
  1805. if (ftrace_expected) {
  1806. print_ip_ins(" expected: ", ftrace_expected);
  1807. pr_cont("\n");
  1808. }
  1809. break;
  1810. case -EPERM:
  1811. pr_info("ftrace faulted on writing ");
  1812. print_ip_sym(KERN_INFO, ip);
  1813. break;
  1814. default:
  1815. pr_info("ftrace faulted on unknown error ");
  1816. print_ip_sym(KERN_INFO, ip);
  1817. }
  1818. print_bug_type();
  1819. if (rec) {
  1820. struct ftrace_ops *ops = NULL;
  1821. pr_info("ftrace record flags: %lx\n", rec->flags);
  1822. pr_cont(" (%ld)%s%s", ftrace_rec_count(rec),
  1823. rec->flags & FTRACE_FL_REGS ? " R" : " ",
  1824. rec->flags & FTRACE_FL_CALL_OPS ? " O" : " ");
  1825. if (rec->flags & FTRACE_FL_TRAMP_EN) {
  1826. ops = ftrace_find_tramp_ops_any(rec);
  1827. if (ops) {
  1828. do {
  1829. pr_cont("\ttramp: %pS (%pS)",
  1830. (void *)ops->trampoline,
  1831. (void *)ops->func);
  1832. ops = ftrace_find_tramp_ops_next(rec, ops);
  1833. } while (ops);
  1834. } else
  1835. pr_cont("\ttramp: ERROR!");
  1836. }
  1837. ip = ftrace_get_addr_curr(rec);
  1838. pr_cont("\n expected tramp: %lx\n", ip);
  1839. }
  1840. FTRACE_WARN_ON_ONCE(1);
  1841. }
  1842. static int ftrace_check_record(struct dyn_ftrace *rec, bool enable, bool update)
  1843. {
  1844. unsigned long flag = 0UL;
  1845. ftrace_bug_type = FTRACE_BUG_UNKNOWN;
  1846. if (skip_record(rec))
  1847. return FTRACE_UPDATE_IGNORE;
  1848. /*
  1849. * If we are updating calls:
  1850. *
  1851. * If the record has a ref count, then we need to enable it
  1852. * because someone is using it.
  1853. *
  1854. * Otherwise we make sure its disabled.
  1855. *
  1856. * If we are disabling calls, then disable all records that
  1857. * are enabled.
  1858. */
  1859. if (enable && ftrace_rec_count(rec))
  1860. flag = FTRACE_FL_ENABLED;
  1861. /*
  1862. * If enabling and the REGS flag does not match the REGS_EN, or
  1863. * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
  1864. * this record. Set flags to fail the compare against ENABLED.
  1865. * Same for direct calls.
  1866. */
  1867. if (flag) {
  1868. if (!(rec->flags & FTRACE_FL_REGS) !=
  1869. !(rec->flags & FTRACE_FL_REGS_EN))
  1870. flag |= FTRACE_FL_REGS;
  1871. if (!(rec->flags & FTRACE_FL_TRAMP) !=
  1872. !(rec->flags & FTRACE_FL_TRAMP_EN))
  1873. flag |= FTRACE_FL_TRAMP;
  1874. /*
  1875. * Direct calls are special, as count matters.
  1876. * We must test the record for direct, if the
  1877. * DIRECT and DIRECT_EN do not match, but only
  1878. * if the count is 1. That's because, if the
  1879. * count is something other than one, we do not
  1880. * want the direct enabled (it will be done via the
  1881. * direct helper). But if DIRECT_EN is set, and
  1882. * the count is not one, we need to clear it.
  1883. *
  1884. */
  1885. if (ftrace_rec_count(rec) == 1) {
  1886. if (!(rec->flags & FTRACE_FL_DIRECT) !=
  1887. !(rec->flags & FTRACE_FL_DIRECT_EN))
  1888. flag |= FTRACE_FL_DIRECT;
  1889. } else if (rec->flags & FTRACE_FL_DIRECT_EN) {
  1890. flag |= FTRACE_FL_DIRECT;
  1891. }
  1892. /*
  1893. * Ops calls are special, as count matters.
  1894. * As with direct calls, they must only be enabled when count
  1895. * is one, otherwise they'll be handled via the list ops.
  1896. */
  1897. if (ftrace_rec_count(rec) == 1) {
  1898. if (!(rec->flags & FTRACE_FL_CALL_OPS) !=
  1899. !(rec->flags & FTRACE_FL_CALL_OPS_EN))
  1900. flag |= FTRACE_FL_CALL_OPS;
  1901. } else if (rec->flags & FTRACE_FL_CALL_OPS_EN) {
  1902. flag |= FTRACE_FL_CALL_OPS;
  1903. }
  1904. }
  1905. /* If the state of this record hasn't changed, then do nothing */
  1906. if ((rec->flags & FTRACE_FL_ENABLED) == flag)
  1907. return FTRACE_UPDATE_IGNORE;
  1908. if (flag) {
  1909. /* Save off if rec is being enabled (for return value) */
  1910. flag ^= rec->flags & FTRACE_FL_ENABLED;
  1911. if (update) {
  1912. rec->flags |= FTRACE_FL_ENABLED | FTRACE_FL_TOUCHED;
  1913. if (flag & FTRACE_FL_REGS) {
  1914. if (rec->flags & FTRACE_FL_REGS)
  1915. rec->flags |= FTRACE_FL_REGS_EN;
  1916. else
  1917. rec->flags &= ~FTRACE_FL_REGS_EN;
  1918. }
  1919. if (flag & FTRACE_FL_TRAMP) {
  1920. if (rec->flags & FTRACE_FL_TRAMP)
  1921. rec->flags |= FTRACE_FL_TRAMP_EN;
  1922. else
  1923. rec->flags &= ~FTRACE_FL_TRAMP_EN;
  1924. }
  1925. /* Keep track of anything that modifies the function */
  1926. if (rec->flags & (FTRACE_FL_DIRECT | FTRACE_FL_IPMODIFY))
  1927. rec->flags |= FTRACE_FL_MODIFIED;
  1928. if (flag & FTRACE_FL_DIRECT) {
  1929. /*
  1930. * If there's only one user (direct_ops helper)
  1931. * then we can call the direct function
  1932. * directly (no ftrace trampoline).
  1933. */
  1934. if (ftrace_rec_count(rec) == 1) {
  1935. if (rec->flags & FTRACE_FL_DIRECT)
  1936. rec->flags |= FTRACE_FL_DIRECT_EN;
  1937. else
  1938. rec->flags &= ~FTRACE_FL_DIRECT_EN;
  1939. } else {
  1940. /*
  1941. * Can only call directly if there's
  1942. * only one callback to the function.
  1943. */
  1944. rec->flags &= ~FTRACE_FL_DIRECT_EN;
  1945. }
  1946. }
  1947. if (flag & FTRACE_FL_CALL_OPS) {
  1948. if (ftrace_rec_count(rec) == 1) {
  1949. if (rec->flags & FTRACE_FL_CALL_OPS)
  1950. rec->flags |= FTRACE_FL_CALL_OPS_EN;
  1951. else
  1952. rec->flags &= ~FTRACE_FL_CALL_OPS_EN;
  1953. } else {
  1954. /*
  1955. * Can only call directly if there's
  1956. * only one set of associated ops.
  1957. */
  1958. rec->flags &= ~FTRACE_FL_CALL_OPS_EN;
  1959. }
  1960. }
  1961. }
  1962. /*
  1963. * If this record is being updated from a nop, then
  1964. * return UPDATE_MAKE_CALL.
  1965. * Otherwise,
  1966. * return UPDATE_MODIFY_CALL to tell the caller to convert
  1967. * from the save regs, to a non-save regs function or
  1968. * vice versa, or from a trampoline call.
  1969. */
  1970. if (flag & FTRACE_FL_ENABLED) {
  1971. ftrace_bug_type = FTRACE_BUG_CALL;
  1972. return FTRACE_UPDATE_MAKE_CALL;
  1973. }
  1974. ftrace_bug_type = FTRACE_BUG_UPDATE;
  1975. return FTRACE_UPDATE_MODIFY_CALL;
  1976. }
  1977. if (update) {
  1978. /* If there's no more users, clear all flags */
  1979. if (!ftrace_rec_count(rec))
  1980. rec->flags &= FTRACE_NOCLEAR_FLAGS;
  1981. else
  1982. /*
  1983. * Just disable the record, but keep the ops TRAMP
  1984. * and REGS states. The _EN flags must be disabled though.
  1985. */
  1986. rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
  1987. FTRACE_FL_REGS_EN | FTRACE_FL_DIRECT_EN |
  1988. FTRACE_FL_CALL_OPS_EN);
  1989. }
  1990. ftrace_bug_type = FTRACE_BUG_NOP;
  1991. return FTRACE_UPDATE_MAKE_NOP;
  1992. }
  1993. /**
  1994. * ftrace_update_record - set a record that now is tracing or not
  1995. * @rec: the record to update
  1996. * @enable: set to true if the record is tracing, false to force disable
  1997. *
  1998. * The records that represent all functions that can be traced need
  1999. * to be updated when tracing has been enabled.
  2000. */
  2001. int ftrace_update_record(struct dyn_ftrace *rec, bool enable)
  2002. {
  2003. return ftrace_check_record(rec, enable, true);
  2004. }
  2005. /**
  2006. * ftrace_test_record - check if the record has been enabled or not
  2007. * @rec: the record to test
  2008. * @enable: set to true to check if enabled, false if it is disabled
  2009. *
  2010. * The arch code may need to test if a record is already set to
  2011. * tracing to determine how to modify the function code that it
  2012. * represents.
  2013. */
  2014. int ftrace_test_record(struct dyn_ftrace *rec, bool enable)
  2015. {
  2016. return ftrace_check_record(rec, enable, false);
  2017. }
  2018. static struct ftrace_ops *
  2019. ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
  2020. {
  2021. struct ftrace_ops *op;
  2022. unsigned long ip = rec->ip;
  2023. do_for_each_ftrace_op(op, ftrace_ops_list) {
  2024. if (!op->trampoline)
  2025. continue;
  2026. if (hash_contains_ip(ip, op->func_hash))
  2027. return op;
  2028. } while_for_each_ftrace_op(op);
  2029. return NULL;
  2030. }
  2031. static struct ftrace_ops *
  2032. ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude)
  2033. {
  2034. struct ftrace_ops *op;
  2035. unsigned long ip = rec->ip;
  2036. do_for_each_ftrace_op(op, ftrace_ops_list) {
  2037. if (op == op_exclude || !op->trampoline)
  2038. continue;
  2039. if (hash_contains_ip(ip, op->func_hash))
  2040. return op;
  2041. } while_for_each_ftrace_op(op);
  2042. return NULL;
  2043. }
  2044. static struct ftrace_ops *
  2045. ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
  2046. struct ftrace_ops *op)
  2047. {
  2048. unsigned long ip = rec->ip;
  2049. while_for_each_ftrace_op(op) {
  2050. if (!op->trampoline)
  2051. continue;
  2052. if (hash_contains_ip(ip, op->func_hash))
  2053. return op;
  2054. }
  2055. return NULL;
  2056. }
  2057. static struct ftrace_ops *
  2058. ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
  2059. {
  2060. struct ftrace_ops *op;
  2061. unsigned long ip = rec->ip;
  2062. /*
  2063. * Need to check removed ops first.
  2064. * If they are being removed, and this rec has a tramp,
  2065. * and this rec is in the ops list, then it would be the
  2066. * one with the tramp.
  2067. */
  2068. if (removed_ops) {
  2069. if (hash_contains_ip(ip, &removed_ops->old_hash))
  2070. return removed_ops;
  2071. }
  2072. /*
  2073. * Need to find the current trampoline for a rec.
  2074. * Now, a trampoline is only attached to a rec if there
  2075. * was a single 'ops' attached to it. But this can be called
  2076. * when we are adding another op to the rec or removing the
  2077. * current one. Thus, if the op is being added, we can
  2078. * ignore it because it hasn't attached itself to the rec
  2079. * yet.
  2080. *
  2081. * If an ops is being modified (hooking to different functions)
  2082. * then we don't care about the new functions that are being
  2083. * added, just the old ones (that are probably being removed).
  2084. *
  2085. * If we are adding an ops to a function that already is using
  2086. * a trampoline, it needs to be removed (trampolines are only
  2087. * for single ops connected), then an ops that is not being
  2088. * modified also needs to be checked.
  2089. */
  2090. do_for_each_ftrace_op(op, ftrace_ops_list) {
  2091. if (!op->trampoline)
  2092. continue;
  2093. /*
  2094. * If the ops is being added, it hasn't gotten to
  2095. * the point to be removed from this tree yet.
  2096. */
  2097. if (op->flags & FTRACE_OPS_FL_ADDING)
  2098. continue;
  2099. /*
  2100. * If the ops is being modified and is in the old
  2101. * hash, then it is probably being removed from this
  2102. * function.
  2103. */
  2104. if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
  2105. hash_contains_ip(ip, &op->old_hash))
  2106. return op;
  2107. /*
  2108. * If the ops is not being added or modified, and it's
  2109. * in its normal filter hash, then this must be the one
  2110. * we want!
  2111. */
  2112. if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
  2113. hash_contains_ip(ip, op->func_hash))
  2114. return op;
  2115. } while_for_each_ftrace_op(op);
  2116. return NULL;
  2117. }
  2118. static struct ftrace_ops *
  2119. ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
  2120. {
  2121. struct ftrace_ops *op;
  2122. unsigned long ip = rec->ip;
  2123. do_for_each_ftrace_op(op, ftrace_ops_list) {
  2124. /* pass rec in as regs to have non-NULL val */
  2125. if (hash_contains_ip(ip, op->func_hash))
  2126. return op;
  2127. } while_for_each_ftrace_op(op);
  2128. return NULL;
  2129. }
  2130. struct ftrace_ops *
  2131. ftrace_find_unique_ops(struct dyn_ftrace *rec)
  2132. {
  2133. struct ftrace_ops *op, *found = NULL;
  2134. unsigned long ip = rec->ip;
  2135. do_for_each_ftrace_op(op, ftrace_ops_list) {
  2136. if (hash_contains_ip(ip, op->func_hash)) {
  2137. if (found)
  2138. return NULL;
  2139. found = op;
  2140. }
  2141. } while_for_each_ftrace_op(op);
  2142. return found;
  2143. }
  2144. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
  2145. /* Protected by rcu_tasks for reading, and direct_mutex for writing */
  2146. static struct ftrace_hash __rcu *direct_functions = EMPTY_HASH;
  2147. static DEFINE_MUTEX(direct_mutex);
  2148. /*
  2149. * Search the direct_functions hash to see if the given instruction pointer
  2150. * has a direct caller attached to it.
  2151. */
  2152. unsigned long ftrace_find_rec_direct(unsigned long ip)
  2153. {
  2154. struct ftrace_func_entry *entry;
  2155. entry = __ftrace_lookup_ip(direct_functions, ip);
  2156. if (!entry)
  2157. return 0;
  2158. return entry->direct;
  2159. }
  2160. static void call_direct_funcs(unsigned long ip, unsigned long pip,
  2161. struct ftrace_ops *ops, struct ftrace_regs *fregs)
  2162. {
  2163. unsigned long addr = READ_ONCE(ops->direct_call);
  2164. if (!addr)
  2165. return;
  2166. arch_ftrace_set_direct_caller(fregs, addr);
  2167. }
  2168. #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
  2169. /**
  2170. * ftrace_get_addr_new - Get the call address to set to
  2171. * @rec: The ftrace record descriptor
  2172. *
  2173. * If the record has the FTRACE_FL_REGS set, that means that it
  2174. * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
  2175. * is not set, then it wants to convert to the normal callback.
  2176. *
  2177. * Returns: the address of the trampoline to set to
  2178. */
  2179. unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
  2180. {
  2181. struct ftrace_ops *ops;
  2182. unsigned long addr;
  2183. if ((rec->flags & FTRACE_FL_DIRECT) &&
  2184. (ftrace_rec_count(rec) == 1)) {
  2185. addr = ftrace_find_rec_direct(rec->ip);
  2186. if (addr)
  2187. return addr;
  2188. WARN_ON_ONCE(1);
  2189. }
  2190. /* Trampolines take precedence over regs */
  2191. if (rec->flags & FTRACE_FL_TRAMP) {
  2192. ops = ftrace_find_tramp_ops_new(rec);
  2193. if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
  2194. pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
  2195. (void *)rec->ip, (void *)rec->ip, rec->flags);
  2196. /* Ftrace is shutting down, return anything */
  2197. return (unsigned long)FTRACE_ADDR;
  2198. }
  2199. return ops->trampoline;
  2200. }
  2201. if (rec->flags & FTRACE_FL_REGS)
  2202. return (unsigned long)FTRACE_REGS_ADDR;
  2203. else
  2204. return (unsigned long)FTRACE_ADDR;
  2205. }
  2206. /**
  2207. * ftrace_get_addr_curr - Get the call address that is already there
  2208. * @rec: The ftrace record descriptor
  2209. *
  2210. * The FTRACE_FL_REGS_EN is set when the record already points to
  2211. * a function that saves all the regs. Basically the '_EN' version
  2212. * represents the current state of the function.
  2213. *
  2214. * Returns: the address of the trampoline that is currently being called
  2215. */
  2216. unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
  2217. {
  2218. struct ftrace_ops *ops;
  2219. unsigned long addr;
  2220. /* Direct calls take precedence over trampolines */
  2221. if (rec->flags & FTRACE_FL_DIRECT_EN) {
  2222. addr = ftrace_find_rec_direct(rec->ip);
  2223. if (addr)
  2224. return addr;
  2225. WARN_ON_ONCE(1);
  2226. }
  2227. /* Trampolines take precedence over regs */
  2228. if (rec->flags & FTRACE_FL_TRAMP_EN) {
  2229. ops = ftrace_find_tramp_ops_curr(rec);
  2230. if (FTRACE_WARN_ON(!ops)) {
  2231. pr_warn("Bad trampoline accounting at: %p (%pS)\n",
  2232. (void *)rec->ip, (void *)rec->ip);
  2233. /* Ftrace is shutting down, return anything */
  2234. return (unsigned long)FTRACE_ADDR;
  2235. }
  2236. return ops->trampoline;
  2237. }
  2238. if (rec->flags & FTRACE_FL_REGS_EN)
  2239. return (unsigned long)FTRACE_REGS_ADDR;
  2240. else
  2241. return (unsigned long)FTRACE_ADDR;
  2242. }
  2243. static int
  2244. __ftrace_replace_code(struct dyn_ftrace *rec, bool enable)
  2245. {
  2246. unsigned long ftrace_old_addr;
  2247. unsigned long ftrace_addr;
  2248. int ret;
  2249. ftrace_addr = ftrace_get_addr_new(rec);
  2250. /* This needs to be done before we call ftrace_update_record */
  2251. ftrace_old_addr = ftrace_get_addr_curr(rec);
  2252. ret = ftrace_update_record(rec, enable);
  2253. ftrace_bug_type = FTRACE_BUG_UNKNOWN;
  2254. switch (ret) {
  2255. case FTRACE_UPDATE_IGNORE:
  2256. return 0;
  2257. case FTRACE_UPDATE_MAKE_CALL:
  2258. ftrace_bug_type = FTRACE_BUG_CALL;
  2259. return ftrace_make_call(rec, ftrace_addr);
  2260. case FTRACE_UPDATE_MAKE_NOP:
  2261. ftrace_bug_type = FTRACE_BUG_NOP;
  2262. return ftrace_make_nop(NULL, rec, ftrace_old_addr);
  2263. case FTRACE_UPDATE_MODIFY_CALL:
  2264. ftrace_bug_type = FTRACE_BUG_UPDATE;
  2265. return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
  2266. }
  2267. return -1; /* unknown ftrace bug */
  2268. }
  2269. void __weak ftrace_replace_code(int mod_flags)
  2270. {
  2271. struct dyn_ftrace *rec;
  2272. struct ftrace_page *pg;
  2273. bool enable = mod_flags & FTRACE_MODIFY_ENABLE_FL;
  2274. int schedulable = mod_flags & FTRACE_MODIFY_MAY_SLEEP_FL;
  2275. int failed;
  2276. if (unlikely(ftrace_disabled))
  2277. return;
  2278. do_for_each_ftrace_rec(pg, rec) {
  2279. if (skip_record(rec))
  2280. continue;
  2281. failed = __ftrace_replace_code(rec, enable);
  2282. if (failed) {
  2283. ftrace_bug(failed, rec);
  2284. /* Stop processing */
  2285. return;
  2286. }
  2287. if (schedulable)
  2288. cond_resched();
  2289. } while_for_each_ftrace_rec();
  2290. }
  2291. struct ftrace_rec_iter {
  2292. struct ftrace_page *pg;
  2293. int index;
  2294. };
  2295. /**
  2296. * ftrace_rec_iter_start - start up iterating over traced functions
  2297. *
  2298. * Returns: an iterator handle that is used to iterate over all
  2299. * the records that represent address locations where functions
  2300. * are traced.
  2301. *
  2302. * May return NULL if no records are available.
  2303. */
  2304. struct ftrace_rec_iter *ftrace_rec_iter_start(void)
  2305. {
  2306. /*
  2307. * We only use a single iterator.
  2308. * Protected by the ftrace_lock mutex.
  2309. */
  2310. static struct ftrace_rec_iter ftrace_rec_iter;
  2311. struct ftrace_rec_iter *iter = &ftrace_rec_iter;
  2312. iter->pg = ftrace_pages_start;
  2313. iter->index = 0;
  2314. /* Could have empty pages */
  2315. while (iter->pg && !iter->pg->index)
  2316. iter->pg = iter->pg->next;
  2317. if (!iter->pg)
  2318. return NULL;
  2319. return iter;
  2320. }
  2321. /**
  2322. * ftrace_rec_iter_next - get the next record to process.
  2323. * @iter: The handle to the iterator.
  2324. *
  2325. * Returns: the next iterator after the given iterator @iter.
  2326. */
  2327. struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
  2328. {
  2329. iter->index++;
  2330. if (iter->index >= iter->pg->index) {
  2331. iter->pg = iter->pg->next;
  2332. iter->index = 0;
  2333. /* Could have empty pages */
  2334. while (iter->pg && !iter->pg->index)
  2335. iter->pg = iter->pg->next;
  2336. }
  2337. if (!iter->pg)
  2338. return NULL;
  2339. return iter;
  2340. }
  2341. /**
  2342. * ftrace_rec_iter_record - get the record at the iterator location
  2343. * @iter: The current iterator location
  2344. *
  2345. * Returns: the record that the current @iter is at.
  2346. */
  2347. struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
  2348. {
  2349. return &iter->pg->records[iter->index];
  2350. }
  2351. static int
  2352. ftrace_nop_initialize(struct module *mod, struct dyn_ftrace *rec)
  2353. {
  2354. int ret;
  2355. if (unlikely(ftrace_disabled))
  2356. return 0;
  2357. ret = ftrace_init_nop(mod, rec);
  2358. if (ret) {
  2359. ftrace_bug_type = FTRACE_BUG_INIT;
  2360. ftrace_bug(ret, rec);
  2361. return 0;
  2362. }
  2363. return 1;
  2364. }
  2365. /*
  2366. * archs can override this function if they must do something
  2367. * before the modifying code is performed.
  2368. */
  2369. void __weak ftrace_arch_code_modify_prepare(void)
  2370. {
  2371. }
  2372. /*
  2373. * archs can override this function if they must do something
  2374. * after the modifying code is performed.
  2375. */
  2376. void __weak ftrace_arch_code_modify_post_process(void)
  2377. {
  2378. }
  2379. static int update_ftrace_func(ftrace_func_t func)
  2380. {
  2381. static ftrace_func_t save_func;
  2382. /* Avoid updating if it hasn't changed */
  2383. if (func == save_func)
  2384. return 0;
  2385. save_func = func;
  2386. return ftrace_update_ftrace_func(func);
  2387. }
  2388. void ftrace_modify_all_code(int command)
  2389. {
  2390. int update = command & FTRACE_UPDATE_TRACE_FUNC;
  2391. int mod_flags = 0;
  2392. int err = 0;
  2393. if (command & FTRACE_MAY_SLEEP)
  2394. mod_flags = FTRACE_MODIFY_MAY_SLEEP_FL;
  2395. /*
  2396. * If the ftrace_caller calls a ftrace_ops func directly,
  2397. * we need to make sure that it only traces functions it
  2398. * expects to trace. When doing the switch of functions,
  2399. * we need to update to the ftrace_ops_list_func first
  2400. * before the transition between old and new calls are set,
  2401. * as the ftrace_ops_list_func will check the ops hashes
  2402. * to make sure the ops are having the right functions
  2403. * traced.
  2404. */
  2405. if (update) {
  2406. err = update_ftrace_func(ftrace_ops_list_func);
  2407. if (FTRACE_WARN_ON(err))
  2408. return;
  2409. }
  2410. if (command & FTRACE_UPDATE_CALLS)
  2411. ftrace_replace_code(mod_flags | FTRACE_MODIFY_ENABLE_FL);
  2412. else if (command & FTRACE_DISABLE_CALLS)
  2413. ftrace_replace_code(mod_flags);
  2414. if (update && ftrace_trace_function != ftrace_ops_list_func) {
  2415. function_trace_op = set_function_trace_op;
  2416. smp_wmb();
  2417. /* If irqs are disabled, we are in stop machine */
  2418. if (!irqs_disabled())
  2419. smp_call_function(ftrace_sync_ipi, NULL, 1);
  2420. err = update_ftrace_func(ftrace_trace_function);
  2421. if (FTRACE_WARN_ON(err))
  2422. return;
  2423. }
  2424. if (command & FTRACE_START_FUNC_RET)
  2425. err = ftrace_enable_ftrace_graph_caller();
  2426. else if (command & FTRACE_STOP_FUNC_RET)
  2427. err = ftrace_disable_ftrace_graph_caller();
  2428. FTRACE_WARN_ON(err);
  2429. }
  2430. static int __ftrace_modify_code(void *data)
  2431. {
  2432. int *command = data;
  2433. ftrace_modify_all_code(*command);
  2434. return 0;
  2435. }
  2436. /**
  2437. * ftrace_run_stop_machine - go back to the stop machine method
  2438. * @command: The command to tell ftrace what to do
  2439. *
  2440. * If an arch needs to fall back to the stop machine method, the
  2441. * it can call this function.
  2442. */
  2443. void ftrace_run_stop_machine(int command)
  2444. {
  2445. stop_machine(__ftrace_modify_code, &command, NULL);
  2446. }
  2447. /**
  2448. * arch_ftrace_update_code - modify the code to trace or not trace
  2449. * @command: The command that needs to be done
  2450. *
  2451. * Archs can override this function if it does not need to
  2452. * run stop_machine() to modify code.
  2453. */
  2454. void __weak arch_ftrace_update_code(int command)
  2455. {
  2456. ftrace_run_stop_machine(command);
  2457. }
  2458. static void ftrace_run_update_code(int command)
  2459. {
  2460. ftrace_arch_code_modify_prepare();
  2461. /*
  2462. * By default we use stop_machine() to modify the code.
  2463. * But archs can do what ever they want as long as it
  2464. * is safe. The stop_machine() is the safest, but also
  2465. * produces the most overhead.
  2466. */
  2467. arch_ftrace_update_code(command);
  2468. ftrace_arch_code_modify_post_process();
  2469. }
  2470. static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
  2471. struct ftrace_ops_hash *old_hash)
  2472. {
  2473. ops->flags |= FTRACE_OPS_FL_MODIFYING;
  2474. ops->old_hash.filter_hash = old_hash->filter_hash;
  2475. ops->old_hash.notrace_hash = old_hash->notrace_hash;
  2476. ftrace_run_update_code(command);
  2477. ops->old_hash.filter_hash = NULL;
  2478. ops->old_hash.notrace_hash = NULL;
  2479. ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
  2480. }
  2481. static ftrace_func_t saved_ftrace_func;
  2482. static int ftrace_start_up;
  2483. void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
  2484. {
  2485. }
  2486. /* List of trace_ops that have allocated trampolines */
  2487. static LIST_HEAD(ftrace_ops_trampoline_list);
  2488. static void ftrace_add_trampoline_to_kallsyms(struct ftrace_ops *ops)
  2489. {
  2490. lockdep_assert_held(&ftrace_lock);
  2491. list_add_rcu(&ops->list, &ftrace_ops_trampoline_list);
  2492. }
  2493. static void ftrace_remove_trampoline_from_kallsyms(struct ftrace_ops *ops)
  2494. {
  2495. lockdep_assert_held(&ftrace_lock);
  2496. list_del_rcu(&ops->list);
  2497. synchronize_rcu();
  2498. }
  2499. /*
  2500. * "__builtin__ftrace" is used as a module name in /proc/kallsyms for symbols
  2501. * for pages allocated for ftrace purposes, even though "__builtin__ftrace" is
  2502. * not a module.
  2503. */
  2504. #define FTRACE_TRAMPOLINE_MOD "__builtin__ftrace"
  2505. #define FTRACE_TRAMPOLINE_SYM "ftrace_trampoline"
  2506. static void ftrace_trampoline_free(struct ftrace_ops *ops)
  2507. {
  2508. if (ops && (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP) &&
  2509. ops->trampoline) {
  2510. /*
  2511. * Record the text poke event before the ksymbol unregister
  2512. * event.
  2513. */
  2514. perf_event_text_poke((void *)ops->trampoline,
  2515. (void *)ops->trampoline,
  2516. ops->trampoline_size, NULL, 0);
  2517. perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
  2518. ops->trampoline, ops->trampoline_size,
  2519. true, FTRACE_TRAMPOLINE_SYM);
  2520. /* Remove from kallsyms after the perf events */
  2521. ftrace_remove_trampoline_from_kallsyms(ops);
  2522. }
  2523. arch_ftrace_trampoline_free(ops);
  2524. }
  2525. static void ftrace_startup_enable(int command)
  2526. {
  2527. if (saved_ftrace_func != ftrace_trace_function) {
  2528. saved_ftrace_func = ftrace_trace_function;
  2529. command |= FTRACE_UPDATE_TRACE_FUNC;
  2530. }
  2531. if (!command || !ftrace_enabled)
  2532. return;
  2533. ftrace_run_update_code(command);
  2534. }
  2535. static void ftrace_startup_all(int command)
  2536. {
  2537. update_all_ops = true;
  2538. ftrace_startup_enable(command);
  2539. update_all_ops = false;
  2540. }
  2541. int ftrace_startup(struct ftrace_ops *ops, int command)
  2542. {
  2543. int ret;
  2544. if (unlikely(ftrace_disabled))
  2545. return -ENODEV;
  2546. ret = __register_ftrace_function(ops);
  2547. if (ret)
  2548. return ret;
  2549. ftrace_start_up++;
  2550. /*
  2551. * Note that ftrace probes uses this to start up
  2552. * and modify functions it will probe. But we still
  2553. * set the ADDING flag for modification, as probes
  2554. * do not have trampolines. If they add them in the
  2555. * future, then the probes will need to distinguish
  2556. * between adding and updating probes.
  2557. */
  2558. ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
  2559. ret = ftrace_hash_ipmodify_enable(ops);
  2560. if (ret < 0) {
  2561. /* Rollback registration process */
  2562. __unregister_ftrace_function(ops);
  2563. ftrace_start_up--;
  2564. ops->flags &= ~FTRACE_OPS_FL_ENABLED;
  2565. if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
  2566. ftrace_trampoline_free(ops);
  2567. return ret;
  2568. }
  2569. if (ftrace_hash_rec_enable(ops))
  2570. command |= FTRACE_UPDATE_CALLS;
  2571. ftrace_startup_enable(command);
  2572. /*
  2573. * If ftrace is in an undefined state, we just remove ops from list
  2574. * to prevent the NULL pointer, instead of totally rolling it back and
  2575. * free trampoline, because those actions could cause further damage.
  2576. */
  2577. if (unlikely(ftrace_disabled)) {
  2578. __unregister_ftrace_function(ops);
  2579. return -ENODEV;
  2580. }
  2581. ops->flags &= ~FTRACE_OPS_FL_ADDING;
  2582. return 0;
  2583. }
  2584. int ftrace_shutdown(struct ftrace_ops *ops, int command)
  2585. {
  2586. int ret;
  2587. if (unlikely(ftrace_disabled))
  2588. return -ENODEV;
  2589. ret = __unregister_ftrace_function(ops);
  2590. if (ret)
  2591. return ret;
  2592. ftrace_start_up--;
  2593. /*
  2594. * Just warn in case of unbalance, no need to kill ftrace, it's not
  2595. * critical but the ftrace_call callers may be never nopped again after
  2596. * further ftrace uses.
  2597. */
  2598. WARN_ON_ONCE(ftrace_start_up < 0);
  2599. /* Disabling ipmodify never fails */
  2600. ftrace_hash_ipmodify_disable(ops);
  2601. if (ftrace_hash_rec_disable(ops))
  2602. command |= FTRACE_UPDATE_CALLS;
  2603. ops->flags &= ~FTRACE_OPS_FL_ENABLED;
  2604. if (saved_ftrace_func != ftrace_trace_function) {
  2605. saved_ftrace_func = ftrace_trace_function;
  2606. command |= FTRACE_UPDATE_TRACE_FUNC;
  2607. }
  2608. if (!command || !ftrace_enabled)
  2609. goto out;
  2610. /*
  2611. * If the ops uses a trampoline, then it needs to be
  2612. * tested first on update.
  2613. */
  2614. ops->flags |= FTRACE_OPS_FL_REMOVING;
  2615. removed_ops = ops;
  2616. /* The trampoline logic checks the old hashes */
  2617. ops->old_hash.filter_hash = ops->func_hash->filter_hash;
  2618. ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
  2619. ftrace_run_update_code(command);
  2620. /*
  2621. * If there's no more ops registered with ftrace, run a
  2622. * sanity check to make sure all rec flags are cleared.
  2623. */
  2624. if (rcu_dereference_protected(ftrace_ops_list,
  2625. lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
  2626. struct ftrace_page *pg;
  2627. struct dyn_ftrace *rec;
  2628. do_for_each_ftrace_rec(pg, rec) {
  2629. if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_NOCLEAR_FLAGS))
  2630. pr_warn(" %pS flags:%lx\n",
  2631. (void *)rec->ip, rec->flags);
  2632. } while_for_each_ftrace_rec();
  2633. }
  2634. ops->old_hash.filter_hash = NULL;
  2635. ops->old_hash.notrace_hash = NULL;
  2636. removed_ops = NULL;
  2637. ops->flags &= ~FTRACE_OPS_FL_REMOVING;
  2638. out:
  2639. /*
  2640. * Dynamic ops may be freed, we must make sure that all
  2641. * callers are done before leaving this function.
  2642. */
  2643. if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
  2644. /*
  2645. * We need to do a hard force of sched synchronization.
  2646. * This is because we use preempt_disable() to do RCU, but
  2647. * the function tracers can be called where RCU is not watching
  2648. * (like before user_exit()). We can not rely on the RCU
  2649. * infrastructure to do the synchronization, thus we must do it
  2650. * ourselves.
  2651. */
  2652. synchronize_rcu_tasks_rude();
  2653. /*
  2654. * When the kernel is preemptive, tasks can be preempted
  2655. * while on a ftrace trampoline. Just scheduling a task on
  2656. * a CPU is not good enough to flush them. Calling
  2657. * synchronize_rcu_tasks() will wait for those tasks to
  2658. * execute and either schedule voluntarily or enter user space.
  2659. */
  2660. synchronize_rcu_tasks();
  2661. ftrace_trampoline_free(ops);
  2662. }
  2663. return 0;
  2664. }
  2665. /* Simply make a copy of @src and return it */
  2666. static struct ftrace_hash *copy_hash(struct ftrace_hash *src)
  2667. {
  2668. if (ftrace_hash_empty(src))
  2669. return EMPTY_HASH;
  2670. return alloc_and_copy_ftrace_hash(src->size_bits, src);
  2671. }
  2672. /*
  2673. * Append @new_hash entries to @hash:
  2674. *
  2675. * If @hash is the EMPTY_HASH then it traces all functions and nothing
  2676. * needs to be done.
  2677. *
  2678. * If @new_hash is the EMPTY_HASH, then make *hash the EMPTY_HASH so
  2679. * that it traces everything.
  2680. *
  2681. * Otherwise, go through all of @new_hash and add anything that @hash
  2682. * doesn't already have, to @hash.
  2683. *
  2684. * The filter_hash updates uses just the append_hash() function
  2685. * and the notrace_hash does not.
  2686. */
  2687. static int append_hash(struct ftrace_hash **hash, struct ftrace_hash *new_hash,
  2688. int size_bits)
  2689. {
  2690. struct ftrace_func_entry *entry;
  2691. int size;
  2692. int i;
  2693. if (*hash) {
  2694. /* An empty hash does everything */
  2695. if (ftrace_hash_empty(*hash))
  2696. return 0;
  2697. } else {
  2698. *hash = alloc_ftrace_hash(size_bits);
  2699. if (!*hash)
  2700. return -ENOMEM;
  2701. }
  2702. /* If new_hash has everything make hash have everything */
  2703. if (ftrace_hash_empty(new_hash)) {
  2704. free_ftrace_hash(*hash);
  2705. *hash = EMPTY_HASH;
  2706. return 0;
  2707. }
  2708. size = 1 << new_hash->size_bits;
  2709. for (i = 0; i < size; i++) {
  2710. hlist_for_each_entry(entry, &new_hash->buckets[i], hlist) {
  2711. /* Only add if not already in hash */
  2712. if (!__ftrace_lookup_ip(*hash, entry->ip) &&
  2713. add_hash_entry(*hash, entry->ip) == NULL)
  2714. return -ENOMEM;
  2715. }
  2716. }
  2717. return 0;
  2718. }
  2719. /*
  2720. * Add to @hash only those that are in both @new_hash1 and @new_hash2
  2721. *
  2722. * The notrace_hash updates uses just the intersect_hash() function
  2723. * and the filter_hash does not.
  2724. */
  2725. static int intersect_hash(struct ftrace_hash **hash, struct ftrace_hash *new_hash1,
  2726. struct ftrace_hash *new_hash2)
  2727. {
  2728. struct ftrace_func_entry *entry;
  2729. int size;
  2730. int i;
  2731. /*
  2732. * If new_hash1 or new_hash2 is the EMPTY_HASH then make the hash
  2733. * empty as well as empty for notrace means none are notraced.
  2734. */
  2735. if (ftrace_hash_empty(new_hash1) || ftrace_hash_empty(new_hash2)) {
  2736. free_ftrace_hash(*hash);
  2737. *hash = EMPTY_HASH;
  2738. return 0;
  2739. }
  2740. size = 1 << new_hash1->size_bits;
  2741. for (i = 0; i < size; i++) {
  2742. hlist_for_each_entry(entry, &new_hash1->buckets[i], hlist) {
  2743. /* Only add if in both @new_hash1 and @new_hash2 */
  2744. if (__ftrace_lookup_ip(new_hash2, entry->ip) &&
  2745. add_hash_entry(*hash, entry->ip) == NULL)
  2746. return -ENOMEM;
  2747. }
  2748. }
  2749. /* If nothing intersects, make it the empty set */
  2750. if (ftrace_hash_empty(*hash)) {
  2751. free_ftrace_hash(*hash);
  2752. *hash = EMPTY_HASH;
  2753. }
  2754. return 0;
  2755. }
  2756. /* Return a new hash that has a union of all @ops->filter_hash entries */
  2757. static struct ftrace_hash *append_hashes(struct ftrace_ops *ops)
  2758. {
  2759. struct ftrace_hash *new_hash = NULL;
  2760. struct ftrace_ops *subops;
  2761. int size_bits;
  2762. int ret;
  2763. if (ops->func_hash->filter_hash)
  2764. size_bits = ops->func_hash->filter_hash->size_bits;
  2765. else
  2766. size_bits = FTRACE_HASH_DEFAULT_BITS;
  2767. list_for_each_entry(subops, &ops->subop_list, list) {
  2768. ret = append_hash(&new_hash, subops->func_hash->filter_hash, size_bits);
  2769. if (ret < 0) {
  2770. free_ftrace_hash(new_hash);
  2771. return NULL;
  2772. }
  2773. /* Nothing more to do if new_hash is empty */
  2774. if (ftrace_hash_empty(new_hash))
  2775. break;
  2776. }
  2777. /* Can't return NULL as that means this failed */
  2778. return new_hash ? : EMPTY_HASH;
  2779. }
  2780. /* Make @ops trace evenything except what all its subops do not trace */
  2781. static struct ftrace_hash *intersect_hashes(struct ftrace_ops *ops)
  2782. {
  2783. struct ftrace_hash *new_hash = NULL;
  2784. struct ftrace_ops *subops;
  2785. int size_bits;
  2786. int ret;
  2787. list_for_each_entry(subops, &ops->subop_list, list) {
  2788. struct ftrace_hash *next_hash;
  2789. if (!new_hash) {
  2790. size_bits = subops->func_hash->notrace_hash->size_bits;
  2791. new_hash = alloc_and_copy_ftrace_hash(size_bits, ops->func_hash->notrace_hash);
  2792. if (!new_hash)
  2793. return NULL;
  2794. continue;
  2795. }
  2796. size_bits = new_hash->size_bits;
  2797. next_hash = new_hash;
  2798. new_hash = alloc_ftrace_hash(size_bits);
  2799. ret = intersect_hash(&new_hash, next_hash, subops->func_hash->notrace_hash);
  2800. free_ftrace_hash(next_hash);
  2801. if (ret < 0) {
  2802. free_ftrace_hash(new_hash);
  2803. return NULL;
  2804. }
  2805. /* Nothing more to do if new_hash is empty */
  2806. if (ftrace_hash_empty(new_hash))
  2807. break;
  2808. }
  2809. return new_hash;
  2810. }
  2811. static bool ops_equal(struct ftrace_hash *A, struct ftrace_hash *B)
  2812. {
  2813. struct ftrace_func_entry *entry;
  2814. int size;
  2815. int i;
  2816. if (ftrace_hash_empty(A))
  2817. return ftrace_hash_empty(B);
  2818. if (ftrace_hash_empty(B))
  2819. return ftrace_hash_empty(A);
  2820. if (A->count != B->count)
  2821. return false;
  2822. size = 1 << A->size_bits;
  2823. for (i = 0; i < size; i++) {
  2824. hlist_for_each_entry(entry, &A->buckets[i], hlist) {
  2825. if (!__ftrace_lookup_ip(B, entry->ip))
  2826. return false;
  2827. }
  2828. }
  2829. return true;
  2830. }
  2831. static void ftrace_ops_update_code(struct ftrace_ops *ops,
  2832. struct ftrace_ops_hash *old_hash);
  2833. static int __ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
  2834. struct ftrace_hash **orig_hash,
  2835. struct ftrace_hash *hash,
  2836. int enable)
  2837. {
  2838. struct ftrace_ops_hash old_hash_ops;
  2839. struct ftrace_hash *old_hash;
  2840. int ret;
  2841. old_hash = *orig_hash;
  2842. old_hash_ops.filter_hash = ops->func_hash->filter_hash;
  2843. old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
  2844. ret = ftrace_hash_move(ops, enable, orig_hash, hash);
  2845. if (!ret) {
  2846. ftrace_ops_update_code(ops, &old_hash_ops);
  2847. free_ftrace_hash_rcu(old_hash);
  2848. }
  2849. return ret;
  2850. }
  2851. static int ftrace_update_ops(struct ftrace_ops *ops, struct ftrace_hash *filter_hash,
  2852. struct ftrace_hash *notrace_hash)
  2853. {
  2854. int ret;
  2855. if (!ops_equal(filter_hash, ops->func_hash->filter_hash)) {
  2856. ret = __ftrace_hash_move_and_update_ops(ops, &ops->func_hash->filter_hash,
  2857. filter_hash, 1);
  2858. if (ret < 0)
  2859. return ret;
  2860. }
  2861. if (!ops_equal(notrace_hash, ops->func_hash->notrace_hash)) {
  2862. ret = __ftrace_hash_move_and_update_ops(ops, &ops->func_hash->notrace_hash,
  2863. notrace_hash, 0);
  2864. if (ret < 0)
  2865. return ret;
  2866. }
  2867. return 0;
  2868. }
  2869. /**
  2870. * ftrace_startup_subops - enable tracing for subops of an ops
  2871. * @ops: Manager ops (used to pick all the functions of its subops)
  2872. * @subops: A new ops to add to @ops
  2873. * @command: Extra commands to use to enable tracing
  2874. *
  2875. * The @ops is a manager @ops that has the filter that includes all the functions
  2876. * that its list of subops are tracing. Adding a new @subops will add the
  2877. * functions of @subops to @ops.
  2878. */
  2879. int ftrace_startup_subops(struct ftrace_ops *ops, struct ftrace_ops *subops, int command)
  2880. {
  2881. struct ftrace_hash *filter_hash;
  2882. struct ftrace_hash *notrace_hash;
  2883. struct ftrace_hash *save_filter_hash;
  2884. struct ftrace_hash *save_notrace_hash;
  2885. int size_bits;
  2886. int ret;
  2887. if (unlikely(ftrace_disabled))
  2888. return -ENODEV;
  2889. ftrace_ops_init(ops);
  2890. ftrace_ops_init(subops);
  2891. if (WARN_ON_ONCE(subops->flags & FTRACE_OPS_FL_ENABLED))
  2892. return -EBUSY;
  2893. /* Make everything canonical (Just in case!) */
  2894. if (!ops->func_hash->filter_hash)
  2895. ops->func_hash->filter_hash = EMPTY_HASH;
  2896. if (!ops->func_hash->notrace_hash)
  2897. ops->func_hash->notrace_hash = EMPTY_HASH;
  2898. if (!subops->func_hash->filter_hash)
  2899. subops->func_hash->filter_hash = EMPTY_HASH;
  2900. if (!subops->func_hash->notrace_hash)
  2901. subops->func_hash->notrace_hash = EMPTY_HASH;
  2902. /* For the first subops to ops just enable it normally */
  2903. if (list_empty(&ops->subop_list)) {
  2904. /* Just use the subops hashes */
  2905. filter_hash = copy_hash(subops->func_hash->filter_hash);
  2906. notrace_hash = copy_hash(subops->func_hash->notrace_hash);
  2907. if (!filter_hash || !notrace_hash) {
  2908. free_ftrace_hash(filter_hash);
  2909. free_ftrace_hash(notrace_hash);
  2910. return -ENOMEM;
  2911. }
  2912. save_filter_hash = ops->func_hash->filter_hash;
  2913. save_notrace_hash = ops->func_hash->notrace_hash;
  2914. ops->func_hash->filter_hash = filter_hash;
  2915. ops->func_hash->notrace_hash = notrace_hash;
  2916. list_add(&subops->list, &ops->subop_list);
  2917. ret = ftrace_startup(ops, command);
  2918. if (ret < 0) {
  2919. list_del(&subops->list);
  2920. ops->func_hash->filter_hash = save_filter_hash;
  2921. ops->func_hash->notrace_hash = save_notrace_hash;
  2922. free_ftrace_hash(filter_hash);
  2923. free_ftrace_hash(notrace_hash);
  2924. } else {
  2925. free_ftrace_hash(save_filter_hash);
  2926. free_ftrace_hash(save_notrace_hash);
  2927. subops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_SUBOP;
  2928. subops->managed = ops;
  2929. }
  2930. return ret;
  2931. }
  2932. /*
  2933. * Here there's already something attached. Here are the rules:
  2934. * o If either filter_hash is empty then the final stays empty
  2935. * o Otherwise, the final is a superset of both hashes
  2936. * o If either notrace_hash is empty then the final stays empty
  2937. * o Otherwise, the final is an intersection between the hashes
  2938. */
  2939. if (ftrace_hash_empty(ops->func_hash->filter_hash) ||
  2940. ftrace_hash_empty(subops->func_hash->filter_hash)) {
  2941. filter_hash = EMPTY_HASH;
  2942. } else {
  2943. size_bits = max(ops->func_hash->filter_hash->size_bits,
  2944. subops->func_hash->filter_hash->size_bits);
  2945. filter_hash = alloc_and_copy_ftrace_hash(size_bits, ops->func_hash->filter_hash);
  2946. if (!filter_hash)
  2947. return -ENOMEM;
  2948. ret = append_hash(&filter_hash, subops->func_hash->filter_hash,
  2949. size_bits);
  2950. if (ret < 0) {
  2951. free_ftrace_hash(filter_hash);
  2952. return ret;
  2953. }
  2954. }
  2955. if (ftrace_hash_empty(ops->func_hash->notrace_hash) ||
  2956. ftrace_hash_empty(subops->func_hash->notrace_hash)) {
  2957. notrace_hash = EMPTY_HASH;
  2958. } else {
  2959. size_bits = max(ops->func_hash->notrace_hash->size_bits,
  2960. subops->func_hash->notrace_hash->size_bits);
  2961. notrace_hash = alloc_ftrace_hash(size_bits);
  2962. if (!notrace_hash) {
  2963. free_ftrace_hash(filter_hash);
  2964. return -ENOMEM;
  2965. }
  2966. ret = intersect_hash(&notrace_hash, ops->func_hash->notrace_hash,
  2967. subops->func_hash->notrace_hash);
  2968. if (ret < 0) {
  2969. free_ftrace_hash(filter_hash);
  2970. free_ftrace_hash(notrace_hash);
  2971. return ret;
  2972. }
  2973. }
  2974. list_add(&subops->list, &ops->subop_list);
  2975. ret = ftrace_update_ops(ops, filter_hash, notrace_hash);
  2976. free_ftrace_hash(filter_hash);
  2977. free_ftrace_hash(notrace_hash);
  2978. if (ret < 0) {
  2979. list_del(&subops->list);
  2980. } else {
  2981. subops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_SUBOP;
  2982. subops->managed = ops;
  2983. }
  2984. return ret;
  2985. }
  2986. /**
  2987. * ftrace_shutdown_subops - Remove a subops from a manager ops
  2988. * @ops: A manager ops to remove @subops from
  2989. * @subops: The subops to remove from @ops
  2990. * @command: Any extra command flags to add to modifying the text
  2991. *
  2992. * Removes the functions being traced by the @subops from @ops. Note, it
  2993. * will not affect functions that are being traced by other subops that
  2994. * still exist in @ops.
  2995. *
  2996. * If the last subops is removed from @ops, then @ops is shutdown normally.
  2997. */
  2998. int ftrace_shutdown_subops(struct ftrace_ops *ops, struct ftrace_ops *subops, int command)
  2999. {
  3000. struct ftrace_hash *filter_hash;
  3001. struct ftrace_hash *notrace_hash;
  3002. int ret;
  3003. if (unlikely(ftrace_disabled))
  3004. return -ENODEV;
  3005. if (WARN_ON_ONCE(!(subops->flags & FTRACE_OPS_FL_ENABLED)))
  3006. return -EINVAL;
  3007. list_del(&subops->list);
  3008. if (list_empty(&ops->subop_list)) {
  3009. /* Last one, just disable the current ops */
  3010. ret = ftrace_shutdown(ops, command);
  3011. if (ret < 0) {
  3012. list_add(&subops->list, &ops->subop_list);
  3013. return ret;
  3014. }
  3015. subops->flags &= ~FTRACE_OPS_FL_ENABLED;
  3016. free_ftrace_hash(ops->func_hash->filter_hash);
  3017. free_ftrace_hash(ops->func_hash->notrace_hash);
  3018. ops->func_hash->filter_hash = EMPTY_HASH;
  3019. ops->func_hash->notrace_hash = EMPTY_HASH;
  3020. subops->flags &= ~(FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_SUBOP);
  3021. subops->managed = NULL;
  3022. return 0;
  3023. }
  3024. /* Rebuild the hashes without subops */
  3025. filter_hash = append_hashes(ops);
  3026. notrace_hash = intersect_hashes(ops);
  3027. if (!filter_hash || !notrace_hash) {
  3028. free_ftrace_hash(filter_hash);
  3029. free_ftrace_hash(notrace_hash);
  3030. list_add(&subops->list, &ops->subop_list);
  3031. return -ENOMEM;
  3032. }
  3033. ret = ftrace_update_ops(ops, filter_hash, notrace_hash);
  3034. if (ret < 0) {
  3035. list_add(&subops->list, &ops->subop_list);
  3036. } else {
  3037. subops->flags &= ~(FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_SUBOP);
  3038. subops->managed = NULL;
  3039. }
  3040. free_ftrace_hash(filter_hash);
  3041. free_ftrace_hash(notrace_hash);
  3042. return ret;
  3043. }
  3044. static int ftrace_hash_move_and_update_subops(struct ftrace_ops *subops,
  3045. struct ftrace_hash **orig_subhash,
  3046. struct ftrace_hash *hash,
  3047. int enable)
  3048. {
  3049. struct ftrace_ops *ops = subops->managed;
  3050. struct ftrace_hash **orig_hash;
  3051. struct ftrace_hash *save_hash;
  3052. struct ftrace_hash *new_hash;
  3053. int ret;
  3054. /* Manager ops can not be subops (yet) */
  3055. if (WARN_ON_ONCE(!ops || ops->flags & FTRACE_OPS_FL_SUBOP))
  3056. return -EINVAL;
  3057. /* Move the new hash over to the subops hash */
  3058. save_hash = *orig_subhash;
  3059. *orig_subhash = __ftrace_hash_move(hash);
  3060. if (!*orig_subhash) {
  3061. *orig_subhash = save_hash;
  3062. return -ENOMEM;
  3063. }
  3064. /* Create a new_hash to hold the ops new functions */
  3065. if (enable) {
  3066. orig_hash = &ops->func_hash->filter_hash;
  3067. new_hash = append_hashes(ops);
  3068. } else {
  3069. orig_hash = &ops->func_hash->notrace_hash;
  3070. new_hash = intersect_hashes(ops);
  3071. }
  3072. /* Move the hash over to the new hash */
  3073. ret = __ftrace_hash_move_and_update_ops(ops, orig_hash, new_hash, enable);
  3074. free_ftrace_hash(new_hash);
  3075. if (ret) {
  3076. /* Put back the original hash */
  3077. free_ftrace_hash_rcu(*orig_subhash);
  3078. *orig_subhash = save_hash;
  3079. } else {
  3080. free_ftrace_hash_rcu(save_hash);
  3081. }
  3082. return ret;
  3083. }
  3084. static u64 ftrace_update_time;
  3085. unsigned long ftrace_update_tot_cnt;
  3086. unsigned long ftrace_number_of_pages;
  3087. unsigned long ftrace_number_of_groups;
  3088. static inline int ops_traces_mod(struct ftrace_ops *ops)
  3089. {
  3090. /*
  3091. * Filter_hash being empty will default to trace module.
  3092. * But notrace hash requires a test of individual module functions.
  3093. */
  3094. return ftrace_hash_empty(ops->func_hash->filter_hash) &&
  3095. ftrace_hash_empty(ops->func_hash->notrace_hash);
  3096. }
  3097. static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
  3098. {
  3099. bool init_nop = ftrace_need_init_nop();
  3100. struct ftrace_page *pg;
  3101. struct dyn_ftrace *p;
  3102. u64 start, stop;
  3103. unsigned long update_cnt = 0;
  3104. unsigned long rec_flags = 0;
  3105. int i;
  3106. start = ftrace_now(raw_smp_processor_id());
  3107. /*
  3108. * When a module is loaded, this function is called to convert
  3109. * the calls to mcount in its text to nops, and also to create
  3110. * an entry in the ftrace data. Now, if ftrace is activated
  3111. * after this call, but before the module sets its text to
  3112. * read-only, the modification of enabling ftrace can fail if
  3113. * the read-only is done while ftrace is converting the calls.
  3114. * To prevent this, the module's records are set as disabled
  3115. * and will be enabled after the call to set the module's text
  3116. * to read-only.
  3117. */
  3118. if (mod)
  3119. rec_flags |= FTRACE_FL_DISABLED;
  3120. for (pg = new_pgs; pg; pg = pg->next) {
  3121. for (i = 0; i < pg->index; i++) {
  3122. /* If something went wrong, bail without enabling anything */
  3123. if (unlikely(ftrace_disabled))
  3124. return -1;
  3125. p = &pg->records[i];
  3126. p->flags = rec_flags;
  3127. /*
  3128. * Do the initial record conversion from mcount jump
  3129. * to the NOP instructions.
  3130. */
  3131. if (init_nop && !ftrace_nop_initialize(mod, p))
  3132. break;
  3133. update_cnt++;
  3134. }
  3135. }
  3136. stop = ftrace_now(raw_smp_processor_id());
  3137. ftrace_update_time = stop - start;
  3138. ftrace_update_tot_cnt += update_cnt;
  3139. return 0;
  3140. }
  3141. static int ftrace_allocate_records(struct ftrace_page *pg, int count)
  3142. {
  3143. int order;
  3144. int pages;
  3145. int cnt;
  3146. if (WARN_ON(!count))
  3147. return -EINVAL;
  3148. /* We want to fill as much as possible, with no empty pages */
  3149. pages = DIV_ROUND_UP(count, ENTRIES_PER_PAGE);
  3150. order = fls(pages) - 1;
  3151. again:
  3152. pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
  3153. if (!pg->records) {
  3154. /* if we can't allocate this size, try something smaller */
  3155. if (!order)
  3156. return -ENOMEM;
  3157. order--;
  3158. goto again;
  3159. }
  3160. ftrace_number_of_pages += 1 << order;
  3161. ftrace_number_of_groups++;
  3162. cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
  3163. pg->order = order;
  3164. if (cnt > count)
  3165. cnt = count;
  3166. return cnt;
  3167. }
  3168. static void ftrace_free_pages(struct ftrace_page *pages)
  3169. {
  3170. struct ftrace_page *pg = pages;
  3171. while (pg) {
  3172. if (pg->records) {
  3173. free_pages((unsigned long)pg->records, pg->order);
  3174. ftrace_number_of_pages -= 1 << pg->order;
  3175. }
  3176. pages = pg->next;
  3177. kfree(pg);
  3178. pg = pages;
  3179. ftrace_number_of_groups--;
  3180. }
  3181. }
  3182. static struct ftrace_page *
  3183. ftrace_allocate_pages(unsigned long num_to_init)
  3184. {
  3185. struct ftrace_page *start_pg;
  3186. struct ftrace_page *pg;
  3187. int cnt;
  3188. if (!num_to_init)
  3189. return NULL;
  3190. start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
  3191. if (!pg)
  3192. return NULL;
  3193. /*
  3194. * Try to allocate as much as possible in one continues
  3195. * location that fills in all of the space. We want to
  3196. * waste as little space as possible.
  3197. */
  3198. for (;;) {
  3199. cnt = ftrace_allocate_records(pg, num_to_init);
  3200. if (cnt < 0)
  3201. goto free_pages;
  3202. num_to_init -= cnt;
  3203. if (!num_to_init)
  3204. break;
  3205. pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
  3206. if (!pg->next)
  3207. goto free_pages;
  3208. pg = pg->next;
  3209. }
  3210. return start_pg;
  3211. free_pages:
  3212. ftrace_free_pages(start_pg);
  3213. pr_info("ftrace: FAILED to allocate memory for functions\n");
  3214. return NULL;
  3215. }
  3216. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  3217. struct ftrace_iterator {
  3218. loff_t pos;
  3219. loff_t func_pos;
  3220. loff_t mod_pos;
  3221. struct ftrace_page *pg;
  3222. struct dyn_ftrace *func;
  3223. struct ftrace_func_probe *probe;
  3224. struct ftrace_func_entry *probe_entry;
  3225. struct trace_parser parser;
  3226. struct ftrace_hash *hash;
  3227. struct ftrace_ops *ops;
  3228. struct trace_array *tr;
  3229. struct list_head *mod_list;
  3230. int pidx;
  3231. int idx;
  3232. unsigned flags;
  3233. };
  3234. static void *
  3235. t_probe_next(struct seq_file *m, loff_t *pos)
  3236. {
  3237. struct ftrace_iterator *iter = m->private;
  3238. struct trace_array *tr = iter->ops->private;
  3239. struct list_head *func_probes;
  3240. struct ftrace_hash *hash;
  3241. struct list_head *next;
  3242. struct hlist_node *hnd = NULL;
  3243. struct hlist_head *hhd;
  3244. int size;
  3245. (*pos)++;
  3246. iter->pos = *pos;
  3247. if (!tr)
  3248. return NULL;
  3249. func_probes = &tr->func_probes;
  3250. if (list_empty(func_probes))
  3251. return NULL;
  3252. if (!iter->probe) {
  3253. next = func_probes->next;
  3254. iter->probe = list_entry(next, struct ftrace_func_probe, list);
  3255. }
  3256. if (iter->probe_entry)
  3257. hnd = &iter->probe_entry->hlist;
  3258. hash = iter->probe->ops.func_hash->filter_hash;
  3259. /*
  3260. * A probe being registered may temporarily have an empty hash
  3261. * and it's at the end of the func_probes list.
  3262. */
  3263. if (!hash || hash == EMPTY_HASH)
  3264. return NULL;
  3265. size = 1 << hash->size_bits;
  3266. retry:
  3267. if (iter->pidx >= size) {
  3268. if (iter->probe->list.next == func_probes)
  3269. return NULL;
  3270. next = iter->probe->list.next;
  3271. iter->probe = list_entry(next, struct ftrace_func_probe, list);
  3272. hash = iter->probe->ops.func_hash->filter_hash;
  3273. size = 1 << hash->size_bits;
  3274. iter->pidx = 0;
  3275. }
  3276. hhd = &hash->buckets[iter->pidx];
  3277. if (hlist_empty(hhd)) {
  3278. iter->pidx++;
  3279. hnd = NULL;
  3280. goto retry;
  3281. }
  3282. if (!hnd)
  3283. hnd = hhd->first;
  3284. else {
  3285. hnd = hnd->next;
  3286. if (!hnd) {
  3287. iter->pidx++;
  3288. goto retry;
  3289. }
  3290. }
  3291. if (WARN_ON_ONCE(!hnd))
  3292. return NULL;
  3293. iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
  3294. return iter;
  3295. }
  3296. static void *t_probe_start(struct seq_file *m, loff_t *pos)
  3297. {
  3298. struct ftrace_iterator *iter = m->private;
  3299. void *p = NULL;
  3300. loff_t l;
  3301. if (!(iter->flags & FTRACE_ITER_DO_PROBES))
  3302. return NULL;
  3303. if (iter->mod_pos > *pos)
  3304. return NULL;
  3305. iter->probe = NULL;
  3306. iter->probe_entry = NULL;
  3307. iter->pidx = 0;
  3308. for (l = 0; l <= (*pos - iter->mod_pos); ) {
  3309. p = t_probe_next(m, &l);
  3310. if (!p)
  3311. break;
  3312. }
  3313. if (!p)
  3314. return NULL;
  3315. /* Only set this if we have an item */
  3316. iter->flags |= FTRACE_ITER_PROBE;
  3317. return iter;
  3318. }
  3319. static int
  3320. t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
  3321. {
  3322. struct ftrace_func_entry *probe_entry;
  3323. struct ftrace_probe_ops *probe_ops;
  3324. struct ftrace_func_probe *probe;
  3325. probe = iter->probe;
  3326. probe_entry = iter->probe_entry;
  3327. if (WARN_ON_ONCE(!probe || !probe_entry))
  3328. return -EIO;
  3329. probe_ops = probe->probe_ops;
  3330. if (probe_ops->print)
  3331. return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
  3332. seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
  3333. (void *)probe_ops->func);
  3334. return 0;
  3335. }
  3336. static void *
  3337. t_mod_next(struct seq_file *m, loff_t *pos)
  3338. {
  3339. struct ftrace_iterator *iter = m->private;
  3340. struct trace_array *tr = iter->tr;
  3341. (*pos)++;
  3342. iter->pos = *pos;
  3343. iter->mod_list = iter->mod_list->next;
  3344. if (iter->mod_list == &tr->mod_trace ||
  3345. iter->mod_list == &tr->mod_notrace) {
  3346. iter->flags &= ~FTRACE_ITER_MOD;
  3347. return NULL;
  3348. }
  3349. iter->mod_pos = *pos;
  3350. return iter;
  3351. }
  3352. static void *t_mod_start(struct seq_file *m, loff_t *pos)
  3353. {
  3354. struct ftrace_iterator *iter = m->private;
  3355. void *p = NULL;
  3356. loff_t l;
  3357. if (iter->func_pos > *pos)
  3358. return NULL;
  3359. iter->mod_pos = iter->func_pos;
  3360. /* probes are only available if tr is set */
  3361. if (!iter->tr)
  3362. return NULL;
  3363. for (l = 0; l <= (*pos - iter->func_pos); ) {
  3364. p = t_mod_next(m, &l);
  3365. if (!p)
  3366. break;
  3367. }
  3368. if (!p) {
  3369. iter->flags &= ~FTRACE_ITER_MOD;
  3370. return t_probe_start(m, pos);
  3371. }
  3372. /* Only set this if we have an item */
  3373. iter->flags |= FTRACE_ITER_MOD;
  3374. return iter;
  3375. }
  3376. static int
  3377. t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
  3378. {
  3379. struct ftrace_mod_load *ftrace_mod;
  3380. struct trace_array *tr = iter->tr;
  3381. if (WARN_ON_ONCE(!iter->mod_list) ||
  3382. iter->mod_list == &tr->mod_trace ||
  3383. iter->mod_list == &tr->mod_notrace)
  3384. return -EIO;
  3385. ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
  3386. if (ftrace_mod->func)
  3387. seq_printf(m, "%s", ftrace_mod->func);
  3388. else
  3389. seq_putc(m, '*');
  3390. seq_printf(m, ":mod:%s\n", ftrace_mod->module);
  3391. return 0;
  3392. }
  3393. static void *
  3394. t_func_next(struct seq_file *m, loff_t *pos)
  3395. {
  3396. struct ftrace_iterator *iter = m->private;
  3397. struct dyn_ftrace *rec = NULL;
  3398. (*pos)++;
  3399. retry:
  3400. if (iter->idx >= iter->pg->index) {
  3401. if (iter->pg->next) {
  3402. iter->pg = iter->pg->next;
  3403. iter->idx = 0;
  3404. goto retry;
  3405. }
  3406. } else {
  3407. rec = &iter->pg->records[iter->idx++];
  3408. if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
  3409. !ftrace_lookup_ip(iter->hash, rec->ip)) ||
  3410. ((iter->flags & FTRACE_ITER_ENABLED) &&
  3411. !(rec->flags & FTRACE_FL_ENABLED)) ||
  3412. ((iter->flags & FTRACE_ITER_TOUCHED) &&
  3413. !(rec->flags & FTRACE_FL_TOUCHED))) {
  3414. rec = NULL;
  3415. goto retry;
  3416. }
  3417. }
  3418. if (!rec)
  3419. return NULL;
  3420. iter->pos = iter->func_pos = *pos;
  3421. iter->func = rec;
  3422. return iter;
  3423. }
  3424. static void *
  3425. t_next(struct seq_file *m, void *v, loff_t *pos)
  3426. {
  3427. struct ftrace_iterator *iter = m->private;
  3428. loff_t l = *pos; /* t_probe_start() must use original pos */
  3429. void *ret;
  3430. if (unlikely(ftrace_disabled))
  3431. return NULL;
  3432. if (iter->flags & FTRACE_ITER_PROBE)
  3433. return t_probe_next(m, pos);
  3434. if (iter->flags & FTRACE_ITER_MOD)
  3435. return t_mod_next(m, pos);
  3436. if (iter->flags & FTRACE_ITER_PRINTALL) {
  3437. /* next must increment pos, and t_probe_start does not */
  3438. (*pos)++;
  3439. return t_mod_start(m, &l);
  3440. }
  3441. ret = t_func_next(m, pos);
  3442. if (!ret)
  3443. return t_mod_start(m, &l);
  3444. return ret;
  3445. }
  3446. static void reset_iter_read(struct ftrace_iterator *iter)
  3447. {
  3448. iter->pos = 0;
  3449. iter->func_pos = 0;
  3450. iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
  3451. }
  3452. static void *t_start(struct seq_file *m, loff_t *pos)
  3453. {
  3454. struct ftrace_iterator *iter = m->private;
  3455. void *p = NULL;
  3456. loff_t l;
  3457. mutex_lock(&ftrace_lock);
  3458. if (unlikely(ftrace_disabled))
  3459. return NULL;
  3460. /*
  3461. * If an lseek was done, then reset and start from beginning.
  3462. */
  3463. if (*pos < iter->pos)
  3464. reset_iter_read(iter);
  3465. /*
  3466. * For set_ftrace_filter reading, if we have the filter
  3467. * off, we can short cut and just print out that all
  3468. * functions are enabled.
  3469. */
  3470. if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
  3471. ftrace_hash_empty(iter->hash)) {
  3472. iter->func_pos = 1; /* Account for the message */
  3473. if (*pos > 0)
  3474. return t_mod_start(m, pos);
  3475. iter->flags |= FTRACE_ITER_PRINTALL;
  3476. /* reset in case of seek/pread */
  3477. iter->flags &= ~FTRACE_ITER_PROBE;
  3478. return iter;
  3479. }
  3480. if (iter->flags & FTRACE_ITER_MOD)
  3481. return t_mod_start(m, pos);
  3482. /*
  3483. * Unfortunately, we need to restart at ftrace_pages_start
  3484. * every time we let go of the ftrace_mutex. This is because
  3485. * those pointers can change without the lock.
  3486. */
  3487. iter->pg = ftrace_pages_start;
  3488. iter->idx = 0;
  3489. for (l = 0; l <= *pos; ) {
  3490. p = t_func_next(m, &l);
  3491. if (!p)
  3492. break;
  3493. }
  3494. if (!p)
  3495. return t_mod_start(m, pos);
  3496. return iter;
  3497. }
  3498. static void t_stop(struct seq_file *m, void *p)
  3499. {
  3500. mutex_unlock(&ftrace_lock);
  3501. }
  3502. void * __weak
  3503. arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
  3504. {
  3505. return NULL;
  3506. }
  3507. static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
  3508. struct dyn_ftrace *rec)
  3509. {
  3510. void *ptr;
  3511. ptr = arch_ftrace_trampoline_func(ops, rec);
  3512. if (ptr)
  3513. seq_printf(m, " ->%pS", ptr);
  3514. }
  3515. #ifdef FTRACE_MCOUNT_MAX_OFFSET
  3516. /*
  3517. * Weak functions can still have an mcount/fentry that is saved in
  3518. * the __mcount_loc section. These can be detected by having a
  3519. * symbol offset of greater than FTRACE_MCOUNT_MAX_OFFSET, as the
  3520. * symbol found by kallsyms is not the function that the mcount/fentry
  3521. * is part of. The offset is much greater in these cases.
  3522. *
  3523. * Test the record to make sure that the ip points to a valid kallsyms
  3524. * and if not, mark it disabled.
  3525. */
  3526. static int test_for_valid_rec(struct dyn_ftrace *rec)
  3527. {
  3528. char str[KSYM_SYMBOL_LEN];
  3529. unsigned long offset;
  3530. const char *ret;
  3531. ret = kallsyms_lookup(rec->ip, NULL, &offset, NULL, str);
  3532. /* Weak functions can cause invalid addresses */
  3533. if (!ret || offset > FTRACE_MCOUNT_MAX_OFFSET) {
  3534. rec->flags |= FTRACE_FL_DISABLED;
  3535. return 0;
  3536. }
  3537. return 1;
  3538. }
  3539. static struct workqueue_struct *ftrace_check_wq __initdata;
  3540. static struct work_struct ftrace_check_work __initdata;
  3541. /*
  3542. * Scan all the mcount/fentry entries to make sure they are valid.
  3543. */
  3544. static __init void ftrace_check_work_func(struct work_struct *work)
  3545. {
  3546. struct ftrace_page *pg;
  3547. struct dyn_ftrace *rec;
  3548. mutex_lock(&ftrace_lock);
  3549. do_for_each_ftrace_rec(pg, rec) {
  3550. test_for_valid_rec(rec);
  3551. } while_for_each_ftrace_rec();
  3552. mutex_unlock(&ftrace_lock);
  3553. }
  3554. static int __init ftrace_check_for_weak_functions(void)
  3555. {
  3556. INIT_WORK(&ftrace_check_work, ftrace_check_work_func);
  3557. ftrace_check_wq = alloc_workqueue("ftrace_check_wq", WQ_UNBOUND, 0);
  3558. queue_work(ftrace_check_wq, &ftrace_check_work);
  3559. return 0;
  3560. }
  3561. static int __init ftrace_check_sync(void)
  3562. {
  3563. /* Make sure the ftrace_check updates are finished */
  3564. if (ftrace_check_wq)
  3565. destroy_workqueue(ftrace_check_wq);
  3566. return 0;
  3567. }
  3568. late_initcall_sync(ftrace_check_sync);
  3569. subsys_initcall(ftrace_check_for_weak_functions);
  3570. static int print_rec(struct seq_file *m, unsigned long ip)
  3571. {
  3572. unsigned long offset;
  3573. char str[KSYM_SYMBOL_LEN];
  3574. char *modname;
  3575. const char *ret;
  3576. ret = kallsyms_lookup(ip, NULL, &offset, &modname, str);
  3577. /* Weak functions can cause invalid addresses */
  3578. if (!ret || offset > FTRACE_MCOUNT_MAX_OFFSET) {
  3579. snprintf(str, KSYM_SYMBOL_LEN, "%s_%ld",
  3580. FTRACE_INVALID_FUNCTION, offset);
  3581. ret = NULL;
  3582. }
  3583. seq_puts(m, str);
  3584. if (modname)
  3585. seq_printf(m, " [%s]", modname);
  3586. return ret == NULL ? -1 : 0;
  3587. }
  3588. #else
  3589. static inline int test_for_valid_rec(struct dyn_ftrace *rec)
  3590. {
  3591. return 1;
  3592. }
  3593. static inline int print_rec(struct seq_file *m, unsigned long ip)
  3594. {
  3595. seq_printf(m, "%ps", (void *)ip);
  3596. return 0;
  3597. }
  3598. #endif
  3599. static int t_show(struct seq_file *m, void *v)
  3600. {
  3601. struct ftrace_iterator *iter = m->private;
  3602. struct dyn_ftrace *rec;
  3603. if (iter->flags & FTRACE_ITER_PROBE)
  3604. return t_probe_show(m, iter);
  3605. if (iter->flags & FTRACE_ITER_MOD)
  3606. return t_mod_show(m, iter);
  3607. if (iter->flags & FTRACE_ITER_PRINTALL) {
  3608. if (iter->flags & FTRACE_ITER_NOTRACE)
  3609. seq_puts(m, "#### no functions disabled ####\n");
  3610. else
  3611. seq_puts(m, "#### all functions enabled ####\n");
  3612. return 0;
  3613. }
  3614. rec = iter->func;
  3615. if (!rec)
  3616. return 0;
  3617. if (iter->flags & FTRACE_ITER_ADDRS)
  3618. seq_printf(m, "%lx ", rec->ip);
  3619. if (print_rec(m, rec->ip)) {
  3620. /* This should only happen when a rec is disabled */
  3621. WARN_ON_ONCE(!(rec->flags & FTRACE_FL_DISABLED));
  3622. seq_putc(m, '\n');
  3623. return 0;
  3624. }
  3625. if (iter->flags & (FTRACE_ITER_ENABLED | FTRACE_ITER_TOUCHED)) {
  3626. struct ftrace_ops *ops;
  3627. seq_printf(m, " (%ld)%s%s%s%s%s",
  3628. ftrace_rec_count(rec),
  3629. rec->flags & FTRACE_FL_REGS ? " R" : " ",
  3630. rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ",
  3631. rec->flags & FTRACE_FL_DIRECT ? " D" : " ",
  3632. rec->flags & FTRACE_FL_CALL_OPS ? " O" : " ",
  3633. rec->flags & FTRACE_FL_MODIFIED ? " M " : " ");
  3634. if (rec->flags & FTRACE_FL_TRAMP_EN) {
  3635. ops = ftrace_find_tramp_ops_any(rec);
  3636. if (ops) {
  3637. do {
  3638. seq_printf(m, "\ttramp: %pS (%pS)",
  3639. (void *)ops->trampoline,
  3640. (void *)ops->func);
  3641. add_trampoline_func(m, ops, rec);
  3642. ops = ftrace_find_tramp_ops_next(rec, ops);
  3643. } while (ops);
  3644. } else
  3645. seq_puts(m, "\ttramp: ERROR!");
  3646. } else {
  3647. add_trampoline_func(m, NULL, rec);
  3648. }
  3649. if (rec->flags & FTRACE_FL_CALL_OPS_EN) {
  3650. ops = ftrace_find_unique_ops(rec);
  3651. if (ops) {
  3652. seq_printf(m, "\tops: %pS (%pS)",
  3653. ops, ops->func);
  3654. } else {
  3655. seq_puts(m, "\tops: ERROR!");
  3656. }
  3657. }
  3658. if (rec->flags & FTRACE_FL_DIRECT) {
  3659. unsigned long direct;
  3660. direct = ftrace_find_rec_direct(rec->ip);
  3661. if (direct)
  3662. seq_printf(m, "\n\tdirect-->%pS", (void *)direct);
  3663. }
  3664. }
  3665. seq_putc(m, '\n');
  3666. return 0;
  3667. }
  3668. static const struct seq_operations show_ftrace_seq_ops = {
  3669. .start = t_start,
  3670. .next = t_next,
  3671. .stop = t_stop,
  3672. .show = t_show,
  3673. };
  3674. static int
  3675. ftrace_avail_open(struct inode *inode, struct file *file)
  3676. {
  3677. struct ftrace_iterator *iter;
  3678. int ret;
  3679. ret = security_locked_down(LOCKDOWN_TRACEFS);
  3680. if (ret)
  3681. return ret;
  3682. if (unlikely(ftrace_disabled))
  3683. return -ENODEV;
  3684. iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
  3685. if (!iter)
  3686. return -ENOMEM;
  3687. iter->pg = ftrace_pages_start;
  3688. iter->ops = &global_ops;
  3689. return 0;
  3690. }
  3691. static int
  3692. ftrace_enabled_open(struct inode *inode, struct file *file)
  3693. {
  3694. struct ftrace_iterator *iter;
  3695. /*
  3696. * This shows us what functions are currently being
  3697. * traced and by what. Not sure if we want lockdown
  3698. * to hide such critical information for an admin.
  3699. * Although, perhaps it can show information we don't
  3700. * want people to see, but if something is tracing
  3701. * something, we probably want to know about it.
  3702. */
  3703. iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
  3704. if (!iter)
  3705. return -ENOMEM;
  3706. iter->pg = ftrace_pages_start;
  3707. iter->flags = FTRACE_ITER_ENABLED;
  3708. iter->ops = &global_ops;
  3709. return 0;
  3710. }
  3711. static int
  3712. ftrace_touched_open(struct inode *inode, struct file *file)
  3713. {
  3714. struct ftrace_iterator *iter;
  3715. /*
  3716. * This shows us what functions have ever been enabled
  3717. * (traced, direct, patched, etc). Not sure if we want lockdown
  3718. * to hide such critical information for an admin.
  3719. * Although, perhaps it can show information we don't
  3720. * want people to see, but if something had traced
  3721. * something, we probably want to know about it.
  3722. */
  3723. iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
  3724. if (!iter)
  3725. return -ENOMEM;
  3726. iter->pg = ftrace_pages_start;
  3727. iter->flags = FTRACE_ITER_TOUCHED;
  3728. iter->ops = &global_ops;
  3729. return 0;
  3730. }
  3731. static int
  3732. ftrace_avail_addrs_open(struct inode *inode, struct file *file)
  3733. {
  3734. struct ftrace_iterator *iter;
  3735. int ret;
  3736. ret = security_locked_down(LOCKDOWN_TRACEFS);
  3737. if (ret)
  3738. return ret;
  3739. if (unlikely(ftrace_disabled))
  3740. return -ENODEV;
  3741. iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
  3742. if (!iter)
  3743. return -ENOMEM;
  3744. iter->pg = ftrace_pages_start;
  3745. iter->flags = FTRACE_ITER_ADDRS;
  3746. iter->ops = &global_ops;
  3747. return 0;
  3748. }
  3749. /**
  3750. * ftrace_regex_open - initialize function tracer filter files
  3751. * @ops: The ftrace_ops that hold the hash filters
  3752. * @flag: The type of filter to process
  3753. * @inode: The inode, usually passed in to your open routine
  3754. * @file: The file, usually passed in to your open routine
  3755. *
  3756. * ftrace_regex_open() initializes the filter files for the
  3757. * @ops. Depending on @flag it may process the filter hash or
  3758. * the notrace hash of @ops. With this called from the open
  3759. * routine, you can use ftrace_filter_write() for the write
  3760. * routine if @flag has FTRACE_ITER_FILTER set, or
  3761. * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
  3762. * tracing_lseek() should be used as the lseek routine, and
  3763. * release must call ftrace_regex_release().
  3764. *
  3765. * Returns: 0 on success or a negative errno value on failure
  3766. */
  3767. int
  3768. ftrace_regex_open(struct ftrace_ops *ops, int flag,
  3769. struct inode *inode, struct file *file)
  3770. {
  3771. struct ftrace_iterator *iter;
  3772. struct ftrace_hash *hash;
  3773. struct list_head *mod_head;
  3774. struct trace_array *tr = ops->private;
  3775. int ret = -ENOMEM;
  3776. ftrace_ops_init(ops);
  3777. if (unlikely(ftrace_disabled))
  3778. return -ENODEV;
  3779. if (tracing_check_open_get_tr(tr))
  3780. return -ENODEV;
  3781. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  3782. if (!iter)
  3783. goto out;
  3784. if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX))
  3785. goto out;
  3786. iter->ops = ops;
  3787. iter->flags = flag;
  3788. iter->tr = tr;
  3789. mutex_lock(&ops->func_hash->regex_lock);
  3790. if (flag & FTRACE_ITER_NOTRACE) {
  3791. hash = ops->func_hash->notrace_hash;
  3792. mod_head = tr ? &tr->mod_notrace : NULL;
  3793. } else {
  3794. hash = ops->func_hash->filter_hash;
  3795. mod_head = tr ? &tr->mod_trace : NULL;
  3796. }
  3797. iter->mod_list = mod_head;
  3798. if (file->f_mode & FMODE_WRITE) {
  3799. const int size_bits = FTRACE_HASH_DEFAULT_BITS;
  3800. if (file->f_flags & O_TRUNC) {
  3801. iter->hash = alloc_ftrace_hash(size_bits);
  3802. clear_ftrace_mod_list(mod_head);
  3803. } else {
  3804. iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
  3805. }
  3806. } else {
  3807. if (hash)
  3808. iter->hash = alloc_and_copy_ftrace_hash(hash->size_bits, hash);
  3809. else
  3810. iter->hash = EMPTY_HASH;
  3811. }
  3812. if (!iter->hash) {
  3813. trace_parser_put(&iter->parser);
  3814. goto out_unlock;
  3815. }
  3816. ret = 0;
  3817. if (file->f_mode & FMODE_READ) {
  3818. iter->pg = ftrace_pages_start;
  3819. ret = seq_open(file, &show_ftrace_seq_ops);
  3820. if (!ret) {
  3821. struct seq_file *m = file->private_data;
  3822. m->private = iter;
  3823. } else {
  3824. /* Failed */
  3825. free_ftrace_hash(iter->hash);
  3826. trace_parser_put(&iter->parser);
  3827. }
  3828. } else
  3829. file->private_data = iter;
  3830. out_unlock:
  3831. mutex_unlock(&ops->func_hash->regex_lock);
  3832. out:
  3833. if (ret) {
  3834. kfree(iter);
  3835. if (tr)
  3836. trace_array_put(tr);
  3837. }
  3838. return ret;
  3839. }
  3840. static int
  3841. ftrace_filter_open(struct inode *inode, struct file *file)
  3842. {
  3843. struct ftrace_ops *ops = inode->i_private;
  3844. /* Checks for tracefs lockdown */
  3845. return ftrace_regex_open(ops,
  3846. FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
  3847. inode, file);
  3848. }
  3849. static int
  3850. ftrace_notrace_open(struct inode *inode, struct file *file)
  3851. {
  3852. struct ftrace_ops *ops = inode->i_private;
  3853. /* Checks for tracefs lockdown */
  3854. return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
  3855. inode, file);
  3856. }
  3857. /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
  3858. struct ftrace_glob {
  3859. char *search;
  3860. unsigned len;
  3861. int type;
  3862. };
  3863. /*
  3864. * If symbols in an architecture don't correspond exactly to the user-visible
  3865. * name of what they represent, it is possible to define this function to
  3866. * perform the necessary adjustments.
  3867. */
  3868. char * __weak arch_ftrace_match_adjust(char *str, const char *search)
  3869. {
  3870. return str;
  3871. }
  3872. static int ftrace_match(char *str, struct ftrace_glob *g)
  3873. {
  3874. int matched = 0;
  3875. int slen;
  3876. str = arch_ftrace_match_adjust(str, g->search);
  3877. switch (g->type) {
  3878. case MATCH_FULL:
  3879. if (strcmp(str, g->search) == 0)
  3880. matched = 1;
  3881. break;
  3882. case MATCH_FRONT_ONLY:
  3883. if (strncmp(str, g->search, g->len) == 0)
  3884. matched = 1;
  3885. break;
  3886. case MATCH_MIDDLE_ONLY:
  3887. if (strstr(str, g->search))
  3888. matched = 1;
  3889. break;
  3890. case MATCH_END_ONLY:
  3891. slen = strlen(str);
  3892. if (slen >= g->len &&
  3893. memcmp(str + slen - g->len, g->search, g->len) == 0)
  3894. matched = 1;
  3895. break;
  3896. case MATCH_GLOB:
  3897. if (glob_match(g->search, str))
  3898. matched = 1;
  3899. break;
  3900. }
  3901. return matched;
  3902. }
  3903. static int
  3904. enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
  3905. {
  3906. struct ftrace_func_entry *entry;
  3907. int ret = 0;
  3908. entry = ftrace_lookup_ip(hash, rec->ip);
  3909. if (clear_filter) {
  3910. /* Do nothing if it doesn't exist */
  3911. if (!entry)
  3912. return 0;
  3913. free_hash_entry(hash, entry);
  3914. } else {
  3915. /* Do nothing if it exists */
  3916. if (entry)
  3917. return 0;
  3918. if (add_hash_entry(hash, rec->ip) == NULL)
  3919. ret = -ENOMEM;
  3920. }
  3921. return ret;
  3922. }
  3923. static int
  3924. add_rec_by_index(struct ftrace_hash *hash, struct ftrace_glob *func_g,
  3925. int clear_filter)
  3926. {
  3927. long index;
  3928. struct ftrace_page *pg;
  3929. struct dyn_ftrace *rec;
  3930. /* The index starts at 1 */
  3931. if (kstrtoul(func_g->search, 0, &index) || --index < 0)
  3932. return 0;
  3933. do_for_each_ftrace_rec(pg, rec) {
  3934. if (pg->index <= index) {
  3935. index -= pg->index;
  3936. /* this is a double loop, break goes to the next page */
  3937. break;
  3938. }
  3939. rec = &pg->records[index];
  3940. enter_record(hash, rec, clear_filter);
  3941. return 1;
  3942. } while_for_each_ftrace_rec();
  3943. return 0;
  3944. }
  3945. #ifdef FTRACE_MCOUNT_MAX_OFFSET
  3946. static int lookup_ip(unsigned long ip, char **modname, char *str)
  3947. {
  3948. unsigned long offset;
  3949. kallsyms_lookup(ip, NULL, &offset, modname, str);
  3950. if (offset > FTRACE_MCOUNT_MAX_OFFSET)
  3951. return -1;
  3952. return 0;
  3953. }
  3954. #else
  3955. static int lookup_ip(unsigned long ip, char **modname, char *str)
  3956. {
  3957. kallsyms_lookup(ip, NULL, NULL, modname, str);
  3958. return 0;
  3959. }
  3960. #endif
  3961. static int
  3962. ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
  3963. struct ftrace_glob *mod_g, int exclude_mod)
  3964. {
  3965. char str[KSYM_SYMBOL_LEN];
  3966. char *modname;
  3967. if (lookup_ip(rec->ip, &modname, str)) {
  3968. /* This should only happen when a rec is disabled */
  3969. WARN_ON_ONCE(system_state == SYSTEM_RUNNING &&
  3970. !(rec->flags & FTRACE_FL_DISABLED));
  3971. return 0;
  3972. }
  3973. if (mod_g) {
  3974. int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
  3975. /* blank module name to match all modules */
  3976. if (!mod_g->len) {
  3977. /* blank module globbing: modname xor exclude_mod */
  3978. if (!exclude_mod != !modname)
  3979. goto func_match;
  3980. return 0;
  3981. }
  3982. /*
  3983. * exclude_mod is set to trace everything but the given
  3984. * module. If it is set and the module matches, then
  3985. * return 0. If it is not set, and the module doesn't match
  3986. * also return 0. Otherwise, check the function to see if
  3987. * that matches.
  3988. */
  3989. if (!mod_matches == !exclude_mod)
  3990. return 0;
  3991. func_match:
  3992. /* blank search means to match all funcs in the mod */
  3993. if (!func_g->len)
  3994. return 1;
  3995. }
  3996. return ftrace_match(str, func_g);
  3997. }
  3998. static int
  3999. match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
  4000. {
  4001. struct ftrace_page *pg;
  4002. struct dyn_ftrace *rec;
  4003. struct ftrace_glob func_g = { .type = MATCH_FULL };
  4004. struct ftrace_glob mod_g = { .type = MATCH_FULL };
  4005. struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
  4006. int exclude_mod = 0;
  4007. int found = 0;
  4008. int ret;
  4009. int clear_filter = 0;
  4010. if (func) {
  4011. func_g.type = filter_parse_regex(func, len, &func_g.search,
  4012. &clear_filter);
  4013. func_g.len = strlen(func_g.search);
  4014. }
  4015. if (mod) {
  4016. mod_g.type = filter_parse_regex(mod, strlen(mod),
  4017. &mod_g.search, &exclude_mod);
  4018. mod_g.len = strlen(mod_g.search);
  4019. }
  4020. mutex_lock(&ftrace_lock);
  4021. if (unlikely(ftrace_disabled))
  4022. goto out_unlock;
  4023. if (func_g.type == MATCH_INDEX) {
  4024. found = add_rec_by_index(hash, &func_g, clear_filter);
  4025. goto out_unlock;
  4026. }
  4027. do_for_each_ftrace_rec(pg, rec) {
  4028. if (rec->flags & FTRACE_FL_DISABLED)
  4029. continue;
  4030. if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
  4031. ret = enter_record(hash, rec, clear_filter);
  4032. if (ret < 0) {
  4033. found = ret;
  4034. goto out_unlock;
  4035. }
  4036. found = 1;
  4037. }
  4038. cond_resched();
  4039. } while_for_each_ftrace_rec();
  4040. out_unlock:
  4041. mutex_unlock(&ftrace_lock);
  4042. return found;
  4043. }
  4044. static int
  4045. ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
  4046. {
  4047. return match_records(hash, buff, len, NULL);
  4048. }
  4049. static void ftrace_ops_update_code(struct ftrace_ops *ops,
  4050. struct ftrace_ops_hash *old_hash)
  4051. {
  4052. struct ftrace_ops *op;
  4053. if (!ftrace_enabled)
  4054. return;
  4055. if (ops->flags & FTRACE_OPS_FL_ENABLED) {
  4056. ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
  4057. return;
  4058. }
  4059. /*
  4060. * If this is the shared global_ops filter, then we need to
  4061. * check if there is another ops that shares it, is enabled.
  4062. * If so, we still need to run the modify code.
  4063. */
  4064. if (ops->func_hash != &global_ops.local_hash)
  4065. return;
  4066. do_for_each_ftrace_op(op, ftrace_ops_list) {
  4067. if (op->func_hash == &global_ops.local_hash &&
  4068. op->flags & FTRACE_OPS_FL_ENABLED) {
  4069. ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
  4070. /* Only need to do this once */
  4071. return;
  4072. }
  4073. } while_for_each_ftrace_op(op);
  4074. }
  4075. static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
  4076. struct ftrace_hash **orig_hash,
  4077. struct ftrace_hash *hash,
  4078. int enable)
  4079. {
  4080. if (ops->flags & FTRACE_OPS_FL_SUBOP)
  4081. return ftrace_hash_move_and_update_subops(ops, orig_hash, hash, enable);
  4082. /*
  4083. * If this ops is not enabled, it could be sharing its filters
  4084. * with a subop. If that's the case, update the subop instead of
  4085. * this ops. Shared filters are only allowed to have one ops set
  4086. * at a time, and if we update the ops that is not enabled,
  4087. * it will not affect subops that share it.
  4088. */
  4089. if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) {
  4090. struct ftrace_ops *op;
  4091. /* Check if any other manager subops maps to this hash */
  4092. do_for_each_ftrace_op(op, ftrace_ops_list) {
  4093. struct ftrace_ops *subops;
  4094. list_for_each_entry(subops, &op->subop_list, list) {
  4095. if ((subops->flags & FTRACE_OPS_FL_ENABLED) &&
  4096. subops->func_hash == ops->func_hash) {
  4097. return ftrace_hash_move_and_update_subops(subops, orig_hash, hash, enable);
  4098. }
  4099. }
  4100. } while_for_each_ftrace_op(op);
  4101. }
  4102. return __ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
  4103. }
  4104. static bool module_exists(const char *module)
  4105. {
  4106. /* All modules have the symbol __this_module */
  4107. static const char this_mod[] = "__this_module";
  4108. char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
  4109. unsigned long val;
  4110. int n;
  4111. n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
  4112. if (n > sizeof(modname) - 1)
  4113. return false;
  4114. val = module_kallsyms_lookup_name(modname);
  4115. return val != 0;
  4116. }
  4117. static int cache_mod(struct trace_array *tr,
  4118. const char *func, char *module, int enable)
  4119. {
  4120. struct ftrace_mod_load *ftrace_mod, *n;
  4121. struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
  4122. int ret;
  4123. mutex_lock(&ftrace_lock);
  4124. /* We do not cache inverse filters */
  4125. if (func[0] == '!') {
  4126. func++;
  4127. ret = -EINVAL;
  4128. /* Look to remove this hash */
  4129. list_for_each_entry_safe(ftrace_mod, n, head, list) {
  4130. if (strcmp(ftrace_mod->module, module) != 0)
  4131. continue;
  4132. /* no func matches all */
  4133. if (strcmp(func, "*") == 0 ||
  4134. (ftrace_mod->func &&
  4135. strcmp(ftrace_mod->func, func) == 0)) {
  4136. ret = 0;
  4137. free_ftrace_mod(ftrace_mod);
  4138. continue;
  4139. }
  4140. }
  4141. goto out;
  4142. }
  4143. ret = -EINVAL;
  4144. /* We only care about modules that have not been loaded yet */
  4145. if (module_exists(module))
  4146. goto out;
  4147. /* Save this string off, and execute it when the module is loaded */
  4148. ret = ftrace_add_mod(tr, func, module, enable);
  4149. out:
  4150. mutex_unlock(&ftrace_lock);
  4151. return ret;
  4152. }
  4153. static int
  4154. ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
  4155. int reset, int enable);
  4156. #ifdef CONFIG_MODULES
  4157. static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
  4158. char *mod, bool enable)
  4159. {
  4160. struct ftrace_mod_load *ftrace_mod, *n;
  4161. struct ftrace_hash **orig_hash, *new_hash;
  4162. LIST_HEAD(process_mods);
  4163. char *func;
  4164. mutex_lock(&ops->func_hash->regex_lock);
  4165. if (enable)
  4166. orig_hash = &ops->func_hash->filter_hash;
  4167. else
  4168. orig_hash = &ops->func_hash->notrace_hash;
  4169. new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
  4170. *orig_hash);
  4171. if (!new_hash)
  4172. goto out; /* warn? */
  4173. mutex_lock(&ftrace_lock);
  4174. list_for_each_entry_safe(ftrace_mod, n, head, list) {
  4175. if (strcmp(ftrace_mod->module, mod) != 0)
  4176. continue;
  4177. if (ftrace_mod->func)
  4178. func = kstrdup(ftrace_mod->func, GFP_KERNEL);
  4179. else
  4180. func = kstrdup("*", GFP_KERNEL);
  4181. if (!func) /* warn? */
  4182. continue;
  4183. list_move(&ftrace_mod->list, &process_mods);
  4184. /* Use the newly allocated func, as it may be "*" */
  4185. kfree(ftrace_mod->func);
  4186. ftrace_mod->func = func;
  4187. }
  4188. mutex_unlock(&ftrace_lock);
  4189. list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
  4190. func = ftrace_mod->func;
  4191. /* Grabs ftrace_lock, which is why we have this extra step */
  4192. match_records(new_hash, func, strlen(func), mod);
  4193. free_ftrace_mod(ftrace_mod);
  4194. }
  4195. if (enable && list_empty(head))
  4196. new_hash->flags &= ~FTRACE_HASH_FL_MOD;
  4197. mutex_lock(&ftrace_lock);
  4198. ftrace_hash_move_and_update_ops(ops, orig_hash,
  4199. new_hash, enable);
  4200. mutex_unlock(&ftrace_lock);
  4201. out:
  4202. mutex_unlock(&ops->func_hash->regex_lock);
  4203. free_ftrace_hash(new_hash);
  4204. }
  4205. static void process_cached_mods(const char *mod_name)
  4206. {
  4207. struct trace_array *tr;
  4208. char *mod;
  4209. mod = kstrdup(mod_name, GFP_KERNEL);
  4210. if (!mod)
  4211. return;
  4212. mutex_lock(&trace_types_lock);
  4213. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  4214. if (!list_empty(&tr->mod_trace))
  4215. process_mod_list(&tr->mod_trace, tr->ops, mod, true);
  4216. if (!list_empty(&tr->mod_notrace))
  4217. process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
  4218. }
  4219. mutex_unlock(&trace_types_lock);
  4220. kfree(mod);
  4221. }
  4222. #endif
  4223. /*
  4224. * We register the module command as a template to show others how
  4225. * to register the a command as well.
  4226. */
  4227. static int
  4228. ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
  4229. char *func_orig, char *cmd, char *module, int enable)
  4230. {
  4231. char *func;
  4232. int ret;
  4233. if (!tr)
  4234. return -ENODEV;
  4235. /* match_records() modifies func, and we need the original */
  4236. func = kstrdup(func_orig, GFP_KERNEL);
  4237. if (!func)
  4238. return -ENOMEM;
  4239. /*
  4240. * cmd == 'mod' because we only registered this func
  4241. * for the 'mod' ftrace_func_command.
  4242. * But if you register one func with multiple commands,
  4243. * you can tell which command was used by the cmd
  4244. * parameter.
  4245. */
  4246. ret = match_records(hash, func, strlen(func), module);
  4247. kfree(func);
  4248. if (!ret)
  4249. return cache_mod(tr, func_orig, module, enable);
  4250. if (ret < 0)
  4251. return ret;
  4252. return 0;
  4253. }
  4254. static struct ftrace_func_command ftrace_mod_cmd = {
  4255. .name = "mod",
  4256. .func = ftrace_mod_callback,
  4257. };
  4258. static int __init ftrace_mod_cmd_init(void)
  4259. {
  4260. return register_ftrace_command(&ftrace_mod_cmd);
  4261. }
  4262. core_initcall(ftrace_mod_cmd_init);
  4263. static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
  4264. struct ftrace_ops *op, struct ftrace_regs *fregs)
  4265. {
  4266. struct ftrace_probe_ops *probe_ops;
  4267. struct ftrace_func_probe *probe;
  4268. probe = container_of(op, struct ftrace_func_probe, ops);
  4269. probe_ops = probe->probe_ops;
  4270. /*
  4271. * Disable preemption for these calls to prevent a RCU grace
  4272. * period. This syncs the hash iteration and freeing of items
  4273. * on the hash. rcu_read_lock is too dangerous here.
  4274. */
  4275. preempt_disable_notrace();
  4276. probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
  4277. preempt_enable_notrace();
  4278. }
  4279. struct ftrace_func_map {
  4280. struct ftrace_func_entry entry;
  4281. void *data;
  4282. };
  4283. struct ftrace_func_mapper {
  4284. struct ftrace_hash hash;
  4285. };
  4286. /**
  4287. * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
  4288. *
  4289. * Returns: a ftrace_func_mapper descriptor that can be used to map ips to data.
  4290. */
  4291. struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
  4292. {
  4293. struct ftrace_hash *hash;
  4294. /*
  4295. * The mapper is simply a ftrace_hash, but since the entries
  4296. * in the hash are not ftrace_func_entry type, we define it
  4297. * as a separate structure.
  4298. */
  4299. hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
  4300. return (struct ftrace_func_mapper *)hash;
  4301. }
  4302. /**
  4303. * ftrace_func_mapper_find_ip - Find some data mapped to an ip
  4304. * @mapper: The mapper that has the ip maps
  4305. * @ip: the instruction pointer to find the data for
  4306. *
  4307. * Returns: the data mapped to @ip if found otherwise NULL. The return
  4308. * is actually the address of the mapper data pointer. The address is
  4309. * returned for use cases where the data is no bigger than a long, and
  4310. * the user can use the data pointer as its data instead of having to
  4311. * allocate more memory for the reference.
  4312. */
  4313. void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
  4314. unsigned long ip)
  4315. {
  4316. struct ftrace_func_entry *entry;
  4317. struct ftrace_func_map *map;
  4318. entry = ftrace_lookup_ip(&mapper->hash, ip);
  4319. if (!entry)
  4320. return NULL;
  4321. map = (struct ftrace_func_map *)entry;
  4322. return &map->data;
  4323. }
  4324. /**
  4325. * ftrace_func_mapper_add_ip - Map some data to an ip
  4326. * @mapper: The mapper that has the ip maps
  4327. * @ip: The instruction pointer address to map @data to
  4328. * @data: The data to map to @ip
  4329. *
  4330. * Returns: 0 on success otherwise an error.
  4331. */
  4332. int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
  4333. unsigned long ip, void *data)
  4334. {
  4335. struct ftrace_func_entry *entry;
  4336. struct ftrace_func_map *map;
  4337. entry = ftrace_lookup_ip(&mapper->hash, ip);
  4338. if (entry)
  4339. return -EBUSY;
  4340. map = kmalloc(sizeof(*map), GFP_KERNEL);
  4341. if (!map)
  4342. return -ENOMEM;
  4343. map->entry.ip = ip;
  4344. map->data = data;
  4345. __add_hash_entry(&mapper->hash, &map->entry);
  4346. return 0;
  4347. }
  4348. /**
  4349. * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
  4350. * @mapper: The mapper that has the ip maps
  4351. * @ip: The instruction pointer address to remove the data from
  4352. *
  4353. * Returns: the data if it is found, otherwise NULL.
  4354. * Note, if the data pointer is used as the data itself, (see
  4355. * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
  4356. * if the data pointer was set to zero.
  4357. */
  4358. void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
  4359. unsigned long ip)
  4360. {
  4361. struct ftrace_func_entry *entry;
  4362. struct ftrace_func_map *map;
  4363. void *data;
  4364. entry = ftrace_lookup_ip(&mapper->hash, ip);
  4365. if (!entry)
  4366. return NULL;
  4367. map = (struct ftrace_func_map *)entry;
  4368. data = map->data;
  4369. remove_hash_entry(&mapper->hash, entry);
  4370. kfree(entry);
  4371. return data;
  4372. }
  4373. /**
  4374. * free_ftrace_func_mapper - free a mapping of ips and data
  4375. * @mapper: The mapper that has the ip maps
  4376. * @free_func: A function to be called on each data item.
  4377. *
  4378. * This is used to free the function mapper. The @free_func is optional
  4379. * and can be used if the data needs to be freed as well.
  4380. */
  4381. void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
  4382. ftrace_mapper_func free_func)
  4383. {
  4384. struct ftrace_func_entry *entry;
  4385. struct ftrace_func_map *map;
  4386. struct hlist_head *hhd;
  4387. int size, i;
  4388. if (!mapper)
  4389. return;
  4390. if (free_func && mapper->hash.count) {
  4391. size = 1 << mapper->hash.size_bits;
  4392. for (i = 0; i < size; i++) {
  4393. hhd = &mapper->hash.buckets[i];
  4394. hlist_for_each_entry(entry, hhd, hlist) {
  4395. map = (struct ftrace_func_map *)entry;
  4396. free_func(map);
  4397. }
  4398. }
  4399. }
  4400. free_ftrace_hash(&mapper->hash);
  4401. }
  4402. static void release_probe(struct ftrace_func_probe *probe)
  4403. {
  4404. struct ftrace_probe_ops *probe_ops;
  4405. mutex_lock(&ftrace_lock);
  4406. WARN_ON(probe->ref <= 0);
  4407. /* Subtract the ref that was used to protect this instance */
  4408. probe->ref--;
  4409. if (!probe->ref) {
  4410. probe_ops = probe->probe_ops;
  4411. /*
  4412. * Sending zero as ip tells probe_ops to free
  4413. * the probe->data itself
  4414. */
  4415. if (probe_ops->free)
  4416. probe_ops->free(probe_ops, probe->tr, 0, probe->data);
  4417. list_del(&probe->list);
  4418. kfree(probe);
  4419. }
  4420. mutex_unlock(&ftrace_lock);
  4421. }
  4422. static void acquire_probe_locked(struct ftrace_func_probe *probe)
  4423. {
  4424. /*
  4425. * Add one ref to keep it from being freed when releasing the
  4426. * ftrace_lock mutex.
  4427. */
  4428. probe->ref++;
  4429. }
  4430. int
  4431. register_ftrace_function_probe(char *glob, struct trace_array *tr,
  4432. struct ftrace_probe_ops *probe_ops,
  4433. void *data)
  4434. {
  4435. struct ftrace_func_probe *probe = NULL, *iter;
  4436. struct ftrace_func_entry *entry;
  4437. struct ftrace_hash **orig_hash;
  4438. struct ftrace_hash *old_hash;
  4439. struct ftrace_hash *hash;
  4440. int count = 0;
  4441. int size;
  4442. int ret;
  4443. int i;
  4444. if (WARN_ON(!tr))
  4445. return -EINVAL;
  4446. /* We do not support '!' for function probes */
  4447. if (WARN_ON(glob[0] == '!'))
  4448. return -EINVAL;
  4449. mutex_lock(&ftrace_lock);
  4450. /* Check if the probe_ops is already registered */
  4451. list_for_each_entry(iter, &tr->func_probes, list) {
  4452. if (iter->probe_ops == probe_ops) {
  4453. probe = iter;
  4454. break;
  4455. }
  4456. }
  4457. if (!probe) {
  4458. probe = kzalloc(sizeof(*probe), GFP_KERNEL);
  4459. if (!probe) {
  4460. mutex_unlock(&ftrace_lock);
  4461. return -ENOMEM;
  4462. }
  4463. probe->probe_ops = probe_ops;
  4464. probe->ops.func = function_trace_probe_call;
  4465. probe->tr = tr;
  4466. ftrace_ops_init(&probe->ops);
  4467. list_add(&probe->list, &tr->func_probes);
  4468. }
  4469. acquire_probe_locked(probe);
  4470. mutex_unlock(&ftrace_lock);
  4471. /*
  4472. * Note, there's a small window here that the func_hash->filter_hash
  4473. * may be NULL or empty. Need to be careful when reading the loop.
  4474. */
  4475. mutex_lock(&probe->ops.func_hash->regex_lock);
  4476. orig_hash = &probe->ops.func_hash->filter_hash;
  4477. old_hash = *orig_hash;
  4478. hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
  4479. if (!hash) {
  4480. ret = -ENOMEM;
  4481. goto out;
  4482. }
  4483. ret = ftrace_match_records(hash, glob, strlen(glob));
  4484. /* Nothing found? */
  4485. if (!ret)
  4486. ret = -EINVAL;
  4487. if (ret < 0)
  4488. goto out;
  4489. size = 1 << hash->size_bits;
  4490. for (i = 0; i < size; i++) {
  4491. hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
  4492. if (ftrace_lookup_ip(old_hash, entry->ip))
  4493. continue;
  4494. /*
  4495. * The caller might want to do something special
  4496. * for each function we find. We call the callback
  4497. * to give the caller an opportunity to do so.
  4498. */
  4499. if (probe_ops->init) {
  4500. ret = probe_ops->init(probe_ops, tr,
  4501. entry->ip, data,
  4502. &probe->data);
  4503. if (ret < 0) {
  4504. if (probe_ops->free && count)
  4505. probe_ops->free(probe_ops, tr,
  4506. 0, probe->data);
  4507. probe->data = NULL;
  4508. goto out;
  4509. }
  4510. }
  4511. count++;
  4512. }
  4513. }
  4514. mutex_lock(&ftrace_lock);
  4515. if (!count) {
  4516. /* Nothing was added? */
  4517. ret = -EINVAL;
  4518. goto out_unlock;
  4519. }
  4520. ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
  4521. hash, 1);
  4522. if (ret < 0)
  4523. goto err_unlock;
  4524. /* One ref for each new function traced */
  4525. probe->ref += count;
  4526. if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
  4527. ret = ftrace_startup(&probe->ops, 0);
  4528. out_unlock:
  4529. mutex_unlock(&ftrace_lock);
  4530. if (!ret)
  4531. ret = count;
  4532. out:
  4533. mutex_unlock(&probe->ops.func_hash->regex_lock);
  4534. free_ftrace_hash(hash);
  4535. release_probe(probe);
  4536. return ret;
  4537. err_unlock:
  4538. if (!probe_ops->free || !count)
  4539. goto out_unlock;
  4540. /* Failed to do the move, need to call the free functions */
  4541. for (i = 0; i < size; i++) {
  4542. hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
  4543. if (ftrace_lookup_ip(old_hash, entry->ip))
  4544. continue;
  4545. probe_ops->free(probe_ops, tr, entry->ip, probe->data);
  4546. }
  4547. }
  4548. goto out_unlock;
  4549. }
  4550. int
  4551. unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
  4552. struct ftrace_probe_ops *probe_ops)
  4553. {
  4554. struct ftrace_func_probe *probe = NULL, *iter;
  4555. struct ftrace_ops_hash old_hash_ops;
  4556. struct ftrace_func_entry *entry;
  4557. struct ftrace_glob func_g;
  4558. struct ftrace_hash **orig_hash;
  4559. struct ftrace_hash *old_hash;
  4560. struct ftrace_hash *hash = NULL;
  4561. struct hlist_node *tmp;
  4562. struct hlist_head hhd;
  4563. char str[KSYM_SYMBOL_LEN];
  4564. int count = 0;
  4565. int i, ret = -ENODEV;
  4566. int size;
  4567. if (!glob || !strlen(glob) || !strcmp(glob, "*"))
  4568. func_g.search = NULL;
  4569. else {
  4570. int not;
  4571. func_g.type = filter_parse_regex(glob, strlen(glob),
  4572. &func_g.search, &not);
  4573. func_g.len = strlen(func_g.search);
  4574. /* we do not support '!' for function probes */
  4575. if (WARN_ON(not))
  4576. return -EINVAL;
  4577. }
  4578. mutex_lock(&ftrace_lock);
  4579. /* Check if the probe_ops is already registered */
  4580. list_for_each_entry(iter, &tr->func_probes, list) {
  4581. if (iter->probe_ops == probe_ops) {
  4582. probe = iter;
  4583. break;
  4584. }
  4585. }
  4586. if (!probe)
  4587. goto err_unlock_ftrace;
  4588. ret = -EINVAL;
  4589. if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
  4590. goto err_unlock_ftrace;
  4591. acquire_probe_locked(probe);
  4592. mutex_unlock(&ftrace_lock);
  4593. mutex_lock(&probe->ops.func_hash->regex_lock);
  4594. orig_hash = &probe->ops.func_hash->filter_hash;
  4595. old_hash = *orig_hash;
  4596. if (ftrace_hash_empty(old_hash))
  4597. goto out_unlock;
  4598. old_hash_ops.filter_hash = old_hash;
  4599. /* Probes only have filters */
  4600. old_hash_ops.notrace_hash = NULL;
  4601. ret = -ENOMEM;
  4602. hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
  4603. if (!hash)
  4604. goto out_unlock;
  4605. INIT_HLIST_HEAD(&hhd);
  4606. size = 1 << hash->size_bits;
  4607. for (i = 0; i < size; i++) {
  4608. hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
  4609. if (func_g.search) {
  4610. kallsyms_lookup(entry->ip, NULL, NULL,
  4611. NULL, str);
  4612. if (!ftrace_match(str, &func_g))
  4613. continue;
  4614. }
  4615. count++;
  4616. remove_hash_entry(hash, entry);
  4617. hlist_add_head(&entry->hlist, &hhd);
  4618. }
  4619. }
  4620. /* Nothing found? */
  4621. if (!count) {
  4622. ret = -EINVAL;
  4623. goto out_unlock;
  4624. }
  4625. mutex_lock(&ftrace_lock);
  4626. WARN_ON(probe->ref < count);
  4627. probe->ref -= count;
  4628. if (ftrace_hash_empty(hash))
  4629. ftrace_shutdown(&probe->ops, 0);
  4630. ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
  4631. hash, 1);
  4632. /* still need to update the function call sites */
  4633. if (ftrace_enabled && !ftrace_hash_empty(hash))
  4634. ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
  4635. &old_hash_ops);
  4636. synchronize_rcu();
  4637. hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
  4638. hlist_del(&entry->hlist);
  4639. if (probe_ops->free)
  4640. probe_ops->free(probe_ops, tr, entry->ip, probe->data);
  4641. kfree(entry);
  4642. }
  4643. mutex_unlock(&ftrace_lock);
  4644. out_unlock:
  4645. mutex_unlock(&probe->ops.func_hash->regex_lock);
  4646. free_ftrace_hash(hash);
  4647. release_probe(probe);
  4648. return ret;
  4649. err_unlock_ftrace:
  4650. mutex_unlock(&ftrace_lock);
  4651. return ret;
  4652. }
  4653. void clear_ftrace_function_probes(struct trace_array *tr)
  4654. {
  4655. struct ftrace_func_probe *probe, *n;
  4656. list_for_each_entry_safe(probe, n, &tr->func_probes, list)
  4657. unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
  4658. }
  4659. static LIST_HEAD(ftrace_commands);
  4660. static DEFINE_MUTEX(ftrace_cmd_mutex);
  4661. /*
  4662. * Currently we only register ftrace commands from __init, so mark this
  4663. * __init too.
  4664. */
  4665. __init int register_ftrace_command(struct ftrace_func_command *cmd)
  4666. {
  4667. struct ftrace_func_command *p;
  4668. int ret = 0;
  4669. mutex_lock(&ftrace_cmd_mutex);
  4670. list_for_each_entry(p, &ftrace_commands, list) {
  4671. if (strcmp(cmd->name, p->name) == 0) {
  4672. ret = -EBUSY;
  4673. goto out_unlock;
  4674. }
  4675. }
  4676. list_add(&cmd->list, &ftrace_commands);
  4677. out_unlock:
  4678. mutex_unlock(&ftrace_cmd_mutex);
  4679. return ret;
  4680. }
  4681. /*
  4682. * Currently we only unregister ftrace commands from __init, so mark
  4683. * this __init too.
  4684. */
  4685. __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
  4686. {
  4687. struct ftrace_func_command *p, *n;
  4688. int ret = -ENODEV;
  4689. mutex_lock(&ftrace_cmd_mutex);
  4690. list_for_each_entry_safe(p, n, &ftrace_commands, list) {
  4691. if (strcmp(cmd->name, p->name) == 0) {
  4692. ret = 0;
  4693. list_del_init(&p->list);
  4694. goto out_unlock;
  4695. }
  4696. }
  4697. out_unlock:
  4698. mutex_unlock(&ftrace_cmd_mutex);
  4699. return ret;
  4700. }
  4701. static int ftrace_process_regex(struct ftrace_iterator *iter,
  4702. char *buff, int len, int enable)
  4703. {
  4704. struct ftrace_hash *hash = iter->hash;
  4705. struct trace_array *tr = iter->ops->private;
  4706. char *func, *command, *next = buff;
  4707. struct ftrace_func_command *p;
  4708. int ret = -EINVAL;
  4709. func = strsep(&next, ":");
  4710. if (!next) {
  4711. ret = ftrace_match_records(hash, func, len);
  4712. if (!ret)
  4713. ret = -EINVAL;
  4714. if (ret < 0)
  4715. return ret;
  4716. return 0;
  4717. }
  4718. /* command found */
  4719. command = strsep(&next, ":");
  4720. mutex_lock(&ftrace_cmd_mutex);
  4721. list_for_each_entry(p, &ftrace_commands, list) {
  4722. if (strcmp(p->name, command) == 0) {
  4723. ret = p->func(tr, hash, func, command, next, enable);
  4724. goto out_unlock;
  4725. }
  4726. }
  4727. out_unlock:
  4728. mutex_unlock(&ftrace_cmd_mutex);
  4729. return ret;
  4730. }
  4731. static ssize_t
  4732. ftrace_regex_write(struct file *file, const char __user *ubuf,
  4733. size_t cnt, loff_t *ppos, int enable)
  4734. {
  4735. struct ftrace_iterator *iter;
  4736. struct trace_parser *parser;
  4737. ssize_t ret, read;
  4738. if (!cnt)
  4739. return 0;
  4740. if (file->f_mode & FMODE_READ) {
  4741. struct seq_file *m = file->private_data;
  4742. iter = m->private;
  4743. } else
  4744. iter = file->private_data;
  4745. if (unlikely(ftrace_disabled))
  4746. return -ENODEV;
  4747. /* iter->hash is a local copy, so we don't need regex_lock */
  4748. parser = &iter->parser;
  4749. read = trace_get_user(parser, ubuf, cnt, ppos);
  4750. if (read >= 0 && trace_parser_loaded(parser) &&
  4751. !trace_parser_cont(parser)) {
  4752. ret = ftrace_process_regex(iter, parser->buffer,
  4753. parser->idx, enable);
  4754. trace_parser_clear(parser);
  4755. if (ret < 0)
  4756. goto out;
  4757. }
  4758. ret = read;
  4759. out:
  4760. return ret;
  4761. }
  4762. ssize_t
  4763. ftrace_filter_write(struct file *file, const char __user *ubuf,
  4764. size_t cnt, loff_t *ppos)
  4765. {
  4766. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  4767. }
  4768. ssize_t
  4769. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  4770. size_t cnt, loff_t *ppos)
  4771. {
  4772. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  4773. }
  4774. static int
  4775. __ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
  4776. {
  4777. struct ftrace_func_entry *entry;
  4778. ip = ftrace_location(ip);
  4779. if (!ip)
  4780. return -EINVAL;
  4781. if (remove) {
  4782. entry = ftrace_lookup_ip(hash, ip);
  4783. if (!entry)
  4784. return -ENOENT;
  4785. free_hash_entry(hash, entry);
  4786. return 0;
  4787. } else if (__ftrace_lookup_ip(hash, ip) != NULL) {
  4788. /* Already exists */
  4789. return 0;
  4790. }
  4791. entry = add_hash_entry(hash, ip);
  4792. return entry ? 0 : -ENOMEM;
  4793. }
  4794. static int
  4795. ftrace_match_addr(struct ftrace_hash *hash, unsigned long *ips,
  4796. unsigned int cnt, int remove)
  4797. {
  4798. unsigned int i;
  4799. int err;
  4800. for (i = 0; i < cnt; i++) {
  4801. err = __ftrace_match_addr(hash, ips[i], remove);
  4802. if (err) {
  4803. /*
  4804. * This expects the @hash is a temporary hash and if this
  4805. * fails the caller must free the @hash.
  4806. */
  4807. return err;
  4808. }
  4809. }
  4810. return 0;
  4811. }
  4812. static int
  4813. ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
  4814. unsigned long *ips, unsigned int cnt,
  4815. int remove, int reset, int enable)
  4816. {
  4817. struct ftrace_hash **orig_hash;
  4818. struct ftrace_hash *hash;
  4819. int ret;
  4820. if (unlikely(ftrace_disabled))
  4821. return -ENODEV;
  4822. mutex_lock(&ops->func_hash->regex_lock);
  4823. if (enable)
  4824. orig_hash = &ops->func_hash->filter_hash;
  4825. else
  4826. orig_hash = &ops->func_hash->notrace_hash;
  4827. if (reset)
  4828. hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
  4829. else
  4830. hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
  4831. if (!hash) {
  4832. ret = -ENOMEM;
  4833. goto out_regex_unlock;
  4834. }
  4835. if (buf && !ftrace_match_records(hash, buf, len)) {
  4836. ret = -EINVAL;
  4837. goto out_regex_unlock;
  4838. }
  4839. if (ips) {
  4840. ret = ftrace_match_addr(hash, ips, cnt, remove);
  4841. if (ret < 0)
  4842. goto out_regex_unlock;
  4843. }
  4844. mutex_lock(&ftrace_lock);
  4845. ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
  4846. mutex_unlock(&ftrace_lock);
  4847. out_regex_unlock:
  4848. mutex_unlock(&ops->func_hash->regex_lock);
  4849. free_ftrace_hash(hash);
  4850. return ret;
  4851. }
  4852. static int
  4853. ftrace_set_addr(struct ftrace_ops *ops, unsigned long *ips, unsigned int cnt,
  4854. int remove, int reset, int enable)
  4855. {
  4856. return ftrace_set_hash(ops, NULL, 0, ips, cnt, remove, reset, enable);
  4857. }
  4858. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
  4859. static int register_ftrace_function_nolock(struct ftrace_ops *ops);
  4860. /*
  4861. * If there are multiple ftrace_ops, use SAVE_REGS by default, so that direct
  4862. * call will be jumped from ftrace_regs_caller. Only if the architecture does
  4863. * not support ftrace_regs_caller but direct_call, use SAVE_ARGS so that it
  4864. * jumps from ftrace_caller for multiple ftrace_ops.
  4865. */
  4866. #ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_REGS
  4867. #define MULTI_FLAGS (FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_ARGS)
  4868. #else
  4869. #define MULTI_FLAGS (FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_REGS)
  4870. #endif
  4871. static int check_direct_multi(struct ftrace_ops *ops)
  4872. {
  4873. if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED))
  4874. return -EINVAL;
  4875. if ((ops->flags & MULTI_FLAGS) != MULTI_FLAGS)
  4876. return -EINVAL;
  4877. return 0;
  4878. }
  4879. static void remove_direct_functions_hash(struct ftrace_hash *hash, unsigned long addr)
  4880. {
  4881. struct ftrace_func_entry *entry, *del;
  4882. int size, i;
  4883. size = 1 << hash->size_bits;
  4884. for (i = 0; i < size; i++) {
  4885. hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
  4886. del = __ftrace_lookup_ip(direct_functions, entry->ip);
  4887. if (del && del->direct == addr) {
  4888. remove_hash_entry(direct_functions, del);
  4889. kfree(del);
  4890. }
  4891. }
  4892. }
  4893. }
  4894. static void register_ftrace_direct_cb(struct rcu_head *rhp)
  4895. {
  4896. struct ftrace_hash *fhp = container_of(rhp, struct ftrace_hash, rcu);
  4897. free_ftrace_hash(fhp);
  4898. }
  4899. /**
  4900. * register_ftrace_direct - Call a custom trampoline directly
  4901. * for multiple functions registered in @ops
  4902. * @ops: The address of the struct ftrace_ops object
  4903. * @addr: The address of the trampoline to call at @ops functions
  4904. *
  4905. * This is used to connect a direct calls to @addr from the nop locations
  4906. * of the functions registered in @ops (with by ftrace_set_filter_ip
  4907. * function).
  4908. *
  4909. * The location that it calls (@addr) must be able to handle a direct call,
  4910. * and save the parameters of the function being traced, and restore them
  4911. * (or inject new ones if needed), before returning.
  4912. *
  4913. * Returns:
  4914. * 0 on success
  4915. * -EINVAL - The @ops object was already registered with this call or
  4916. * when there are no functions in @ops object.
  4917. * -EBUSY - Another direct function is already attached (there can be only one)
  4918. * -ENODEV - @ip does not point to a ftrace nop location (or not supported)
  4919. * -ENOMEM - There was an allocation failure.
  4920. */
  4921. int register_ftrace_direct(struct ftrace_ops *ops, unsigned long addr)
  4922. {
  4923. struct ftrace_hash *hash, *new_hash = NULL, *free_hash = NULL;
  4924. struct ftrace_func_entry *entry, *new;
  4925. int err = -EBUSY, size, i;
  4926. if (ops->func || ops->trampoline)
  4927. return -EINVAL;
  4928. if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED))
  4929. return -EINVAL;
  4930. if (ops->flags & FTRACE_OPS_FL_ENABLED)
  4931. return -EINVAL;
  4932. hash = ops->func_hash->filter_hash;
  4933. if (ftrace_hash_empty(hash))
  4934. return -EINVAL;
  4935. mutex_lock(&direct_mutex);
  4936. /* Make sure requested entries are not already registered.. */
  4937. size = 1 << hash->size_bits;
  4938. for (i = 0; i < size; i++) {
  4939. hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
  4940. if (ftrace_find_rec_direct(entry->ip))
  4941. goto out_unlock;
  4942. }
  4943. }
  4944. err = -ENOMEM;
  4945. /* Make a copy hash to place the new and the old entries in */
  4946. size = hash->count + direct_functions->count;
  4947. size = fls(size);
  4948. if (size > FTRACE_HASH_MAX_BITS)
  4949. size = FTRACE_HASH_MAX_BITS;
  4950. new_hash = alloc_ftrace_hash(size);
  4951. if (!new_hash)
  4952. goto out_unlock;
  4953. /* Now copy over the existing direct entries */
  4954. size = 1 << direct_functions->size_bits;
  4955. for (i = 0; i < size; i++) {
  4956. hlist_for_each_entry(entry, &direct_functions->buckets[i], hlist) {
  4957. new = add_hash_entry(new_hash, entry->ip);
  4958. if (!new)
  4959. goto out_unlock;
  4960. new->direct = entry->direct;
  4961. }
  4962. }
  4963. /* ... and add the new entries */
  4964. size = 1 << hash->size_bits;
  4965. for (i = 0; i < size; i++) {
  4966. hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
  4967. new = add_hash_entry(new_hash, entry->ip);
  4968. if (!new)
  4969. goto out_unlock;
  4970. /* Update both the copy and the hash entry */
  4971. new->direct = addr;
  4972. entry->direct = addr;
  4973. }
  4974. }
  4975. free_hash = direct_functions;
  4976. rcu_assign_pointer(direct_functions, new_hash);
  4977. new_hash = NULL;
  4978. ops->func = call_direct_funcs;
  4979. ops->flags = MULTI_FLAGS;
  4980. ops->trampoline = FTRACE_REGS_ADDR;
  4981. ops->direct_call = addr;
  4982. err = register_ftrace_function_nolock(ops);
  4983. out_unlock:
  4984. mutex_unlock(&direct_mutex);
  4985. if (free_hash && free_hash != EMPTY_HASH)
  4986. call_rcu_tasks(&free_hash->rcu, register_ftrace_direct_cb);
  4987. if (new_hash)
  4988. free_ftrace_hash(new_hash);
  4989. return err;
  4990. }
  4991. EXPORT_SYMBOL_GPL(register_ftrace_direct);
  4992. /**
  4993. * unregister_ftrace_direct - Remove calls to custom trampoline
  4994. * previously registered by register_ftrace_direct for @ops object.
  4995. * @ops: The address of the struct ftrace_ops object
  4996. * @addr: The address of the direct function that is called by the @ops functions
  4997. * @free_filters: Set to true to remove all filters for the ftrace_ops, false otherwise
  4998. *
  4999. * This is used to remove a direct calls to @addr from the nop locations
  5000. * of the functions registered in @ops (with by ftrace_set_filter_ip
  5001. * function).
  5002. *
  5003. * Returns:
  5004. * 0 on success
  5005. * -EINVAL - The @ops object was not properly registered.
  5006. */
  5007. int unregister_ftrace_direct(struct ftrace_ops *ops, unsigned long addr,
  5008. bool free_filters)
  5009. {
  5010. struct ftrace_hash *hash = ops->func_hash->filter_hash;
  5011. int err;
  5012. if (check_direct_multi(ops))
  5013. return -EINVAL;
  5014. if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
  5015. return -EINVAL;
  5016. mutex_lock(&direct_mutex);
  5017. err = unregister_ftrace_function(ops);
  5018. remove_direct_functions_hash(hash, addr);
  5019. mutex_unlock(&direct_mutex);
  5020. /* cleanup for possible another register call */
  5021. ops->func = NULL;
  5022. ops->trampoline = 0;
  5023. if (free_filters)
  5024. ftrace_free_filter(ops);
  5025. return err;
  5026. }
  5027. EXPORT_SYMBOL_GPL(unregister_ftrace_direct);
  5028. static int
  5029. __modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr)
  5030. {
  5031. struct ftrace_hash *hash;
  5032. struct ftrace_func_entry *entry, *iter;
  5033. static struct ftrace_ops tmp_ops = {
  5034. .func = ftrace_stub,
  5035. .flags = FTRACE_OPS_FL_STUB,
  5036. };
  5037. int i, size;
  5038. int err;
  5039. lockdep_assert_held_once(&direct_mutex);
  5040. /* Enable the tmp_ops to have the same functions as the direct ops */
  5041. ftrace_ops_init(&tmp_ops);
  5042. tmp_ops.func_hash = ops->func_hash;
  5043. tmp_ops.direct_call = addr;
  5044. err = register_ftrace_function_nolock(&tmp_ops);
  5045. if (err)
  5046. return err;
  5047. /*
  5048. * Now the ftrace_ops_list_func() is called to do the direct callers.
  5049. * We can safely change the direct functions attached to each entry.
  5050. */
  5051. mutex_lock(&ftrace_lock);
  5052. hash = ops->func_hash->filter_hash;
  5053. size = 1 << hash->size_bits;
  5054. for (i = 0; i < size; i++) {
  5055. hlist_for_each_entry(iter, &hash->buckets[i], hlist) {
  5056. entry = __ftrace_lookup_ip(direct_functions, iter->ip);
  5057. if (!entry)
  5058. continue;
  5059. entry->direct = addr;
  5060. }
  5061. }
  5062. /* Prevent store tearing if a trampoline concurrently accesses the value */
  5063. WRITE_ONCE(ops->direct_call, addr);
  5064. mutex_unlock(&ftrace_lock);
  5065. /* Removing the tmp_ops will add the updated direct callers to the functions */
  5066. unregister_ftrace_function(&tmp_ops);
  5067. return err;
  5068. }
  5069. /**
  5070. * modify_ftrace_direct_nolock - Modify an existing direct 'multi' call
  5071. * to call something else
  5072. * @ops: The address of the struct ftrace_ops object
  5073. * @addr: The address of the new trampoline to call at @ops functions
  5074. *
  5075. * This is used to unregister currently registered direct caller and
  5076. * register new one @addr on functions registered in @ops object.
  5077. *
  5078. * Note there's window between ftrace_shutdown and ftrace_startup calls
  5079. * where there will be no callbacks called.
  5080. *
  5081. * Caller should already have direct_mutex locked, so we don't lock
  5082. * direct_mutex here.
  5083. *
  5084. * Returns: zero on success. Non zero on error, which includes:
  5085. * -EINVAL - The @ops object was not properly registered.
  5086. */
  5087. int modify_ftrace_direct_nolock(struct ftrace_ops *ops, unsigned long addr)
  5088. {
  5089. if (check_direct_multi(ops))
  5090. return -EINVAL;
  5091. if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
  5092. return -EINVAL;
  5093. return __modify_ftrace_direct(ops, addr);
  5094. }
  5095. EXPORT_SYMBOL_GPL(modify_ftrace_direct_nolock);
  5096. /**
  5097. * modify_ftrace_direct - Modify an existing direct 'multi' call
  5098. * to call something else
  5099. * @ops: The address of the struct ftrace_ops object
  5100. * @addr: The address of the new trampoline to call at @ops functions
  5101. *
  5102. * This is used to unregister currently registered direct caller and
  5103. * register new one @addr on functions registered in @ops object.
  5104. *
  5105. * Note there's window between ftrace_shutdown and ftrace_startup calls
  5106. * where there will be no callbacks called.
  5107. *
  5108. * Returns: zero on success. Non zero on error, which includes:
  5109. * -EINVAL - The @ops object was not properly registered.
  5110. */
  5111. int modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr)
  5112. {
  5113. int err;
  5114. if (check_direct_multi(ops))
  5115. return -EINVAL;
  5116. if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
  5117. return -EINVAL;
  5118. mutex_lock(&direct_mutex);
  5119. err = __modify_ftrace_direct(ops, addr);
  5120. mutex_unlock(&direct_mutex);
  5121. return err;
  5122. }
  5123. EXPORT_SYMBOL_GPL(modify_ftrace_direct);
  5124. #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
  5125. /**
  5126. * ftrace_set_filter_ip - set a function to filter on in ftrace by address
  5127. * @ops: the ops to set the filter with
  5128. * @ip: the address to add to or remove from the filter.
  5129. * @remove: non zero to remove the ip from the filter
  5130. * @reset: non zero to reset all filters before applying this filter.
  5131. *
  5132. * Filters denote which functions should be enabled when tracing is enabled
  5133. * If @ip is NULL, it fails to update filter.
  5134. *
  5135. * This can allocate memory which must be freed before @ops can be freed,
  5136. * either by removing each filtered addr or by using
  5137. * ftrace_free_filter(@ops).
  5138. */
  5139. int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
  5140. int remove, int reset)
  5141. {
  5142. ftrace_ops_init(ops);
  5143. return ftrace_set_addr(ops, &ip, 1, remove, reset, 1);
  5144. }
  5145. EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
  5146. /**
  5147. * ftrace_set_filter_ips - set functions to filter on in ftrace by addresses
  5148. * @ops: the ops to set the filter with
  5149. * @ips: the array of addresses to add to or remove from the filter.
  5150. * @cnt: the number of addresses in @ips
  5151. * @remove: non zero to remove ips from the filter
  5152. * @reset: non zero to reset all filters before applying this filter.
  5153. *
  5154. * Filters denote which functions should be enabled when tracing is enabled
  5155. * If @ips array or any ip specified within is NULL , it fails to update filter.
  5156. *
  5157. * This can allocate memory which must be freed before @ops can be freed,
  5158. * either by removing each filtered addr or by using
  5159. * ftrace_free_filter(@ops).
  5160. */
  5161. int ftrace_set_filter_ips(struct ftrace_ops *ops, unsigned long *ips,
  5162. unsigned int cnt, int remove, int reset)
  5163. {
  5164. ftrace_ops_init(ops);
  5165. return ftrace_set_addr(ops, ips, cnt, remove, reset, 1);
  5166. }
  5167. EXPORT_SYMBOL_GPL(ftrace_set_filter_ips);
  5168. /**
  5169. * ftrace_ops_set_global_filter - setup ops to use global filters
  5170. * @ops: the ops which will use the global filters
  5171. *
  5172. * ftrace users who need global function trace filtering should call this.
  5173. * It can set the global filter only if ops were not initialized before.
  5174. */
  5175. void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
  5176. {
  5177. if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
  5178. return;
  5179. ftrace_ops_init(ops);
  5180. ops->func_hash = &global_ops.local_hash;
  5181. }
  5182. EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
  5183. static int
  5184. ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
  5185. int reset, int enable)
  5186. {
  5187. return ftrace_set_hash(ops, buf, len, NULL, 0, 0, reset, enable);
  5188. }
  5189. /**
  5190. * ftrace_set_filter - set a function to filter on in ftrace
  5191. * @ops: the ops to set the filter with
  5192. * @buf: the string that holds the function filter text.
  5193. * @len: the length of the string.
  5194. * @reset: non-zero to reset all filters before applying this filter.
  5195. *
  5196. * Filters denote which functions should be enabled when tracing is enabled.
  5197. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  5198. *
  5199. * This can allocate memory which must be freed before @ops can be freed,
  5200. * either by removing each filtered addr or by using
  5201. * ftrace_free_filter(@ops).
  5202. */
  5203. int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
  5204. int len, int reset)
  5205. {
  5206. ftrace_ops_init(ops);
  5207. return ftrace_set_regex(ops, buf, len, reset, 1);
  5208. }
  5209. EXPORT_SYMBOL_GPL(ftrace_set_filter);
  5210. /**
  5211. * ftrace_set_notrace - set a function to not trace in ftrace
  5212. * @ops: the ops to set the notrace filter with
  5213. * @buf: the string that holds the function notrace text.
  5214. * @len: the length of the string.
  5215. * @reset: non-zero to reset all filters before applying this filter.
  5216. *
  5217. * Notrace Filters denote which functions should not be enabled when tracing
  5218. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  5219. * for tracing.
  5220. *
  5221. * This can allocate memory which must be freed before @ops can be freed,
  5222. * either by removing each filtered addr or by using
  5223. * ftrace_free_filter(@ops).
  5224. */
  5225. int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
  5226. int len, int reset)
  5227. {
  5228. ftrace_ops_init(ops);
  5229. return ftrace_set_regex(ops, buf, len, reset, 0);
  5230. }
  5231. EXPORT_SYMBOL_GPL(ftrace_set_notrace);
  5232. /**
  5233. * ftrace_set_global_filter - set a function to filter on with global tracers
  5234. * @buf: the string that holds the function filter text.
  5235. * @len: the length of the string.
  5236. * @reset: non-zero to reset all filters before applying this filter.
  5237. *
  5238. * Filters denote which functions should be enabled when tracing is enabled.
  5239. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  5240. */
  5241. void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
  5242. {
  5243. ftrace_set_regex(&global_ops, buf, len, reset, 1);
  5244. }
  5245. EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
  5246. /**
  5247. * ftrace_set_global_notrace - set a function to not trace with global tracers
  5248. * @buf: the string that holds the function notrace text.
  5249. * @len: the length of the string.
  5250. * @reset: non-zero to reset all filters before applying this filter.
  5251. *
  5252. * Notrace Filters denote which functions should not be enabled when tracing
  5253. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  5254. * for tracing.
  5255. */
  5256. void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
  5257. {
  5258. ftrace_set_regex(&global_ops, buf, len, reset, 0);
  5259. }
  5260. EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
  5261. /*
  5262. * command line interface to allow users to set filters on boot up.
  5263. */
  5264. #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
  5265. static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
  5266. static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
  5267. /* Used by function selftest to not test if filter is set */
  5268. bool ftrace_filter_param __initdata;
  5269. static int __init set_ftrace_notrace(char *str)
  5270. {
  5271. ftrace_filter_param = true;
  5272. strscpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
  5273. return 1;
  5274. }
  5275. __setup("ftrace_notrace=", set_ftrace_notrace);
  5276. static int __init set_ftrace_filter(char *str)
  5277. {
  5278. ftrace_filter_param = true;
  5279. strscpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
  5280. return 1;
  5281. }
  5282. __setup("ftrace_filter=", set_ftrace_filter);
  5283. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  5284. static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
  5285. static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
  5286. static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
  5287. static int __init set_graph_function(char *str)
  5288. {
  5289. strscpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
  5290. return 1;
  5291. }
  5292. __setup("ftrace_graph_filter=", set_graph_function);
  5293. static int __init set_graph_notrace_function(char *str)
  5294. {
  5295. strscpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
  5296. return 1;
  5297. }
  5298. __setup("ftrace_graph_notrace=", set_graph_notrace_function);
  5299. static int __init set_graph_max_depth_function(char *str)
  5300. {
  5301. if (!str || kstrtouint(str, 0, &fgraph_max_depth))
  5302. return 0;
  5303. return 1;
  5304. }
  5305. __setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
  5306. static void __init set_ftrace_early_graph(char *buf, int enable)
  5307. {
  5308. int ret;
  5309. char *func;
  5310. struct ftrace_hash *hash;
  5311. hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
  5312. if (MEM_FAIL(!hash, "Failed to allocate hash\n"))
  5313. return;
  5314. while (buf) {
  5315. func = strsep(&buf, ",");
  5316. /* we allow only one expression at a time */
  5317. ret = ftrace_graph_set_hash(hash, func);
  5318. if (ret)
  5319. printk(KERN_DEBUG "ftrace: function %s not "
  5320. "traceable\n", func);
  5321. }
  5322. if (enable)
  5323. ftrace_graph_hash = hash;
  5324. else
  5325. ftrace_graph_notrace_hash = hash;
  5326. }
  5327. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  5328. void __init
  5329. ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
  5330. {
  5331. char *func;
  5332. ftrace_ops_init(ops);
  5333. while (buf) {
  5334. func = strsep(&buf, ",");
  5335. ftrace_set_regex(ops, func, strlen(func), 0, enable);
  5336. }
  5337. }
  5338. static void __init set_ftrace_early_filters(void)
  5339. {
  5340. if (ftrace_filter_buf[0])
  5341. ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
  5342. if (ftrace_notrace_buf[0])
  5343. ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
  5344. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  5345. if (ftrace_graph_buf[0])
  5346. set_ftrace_early_graph(ftrace_graph_buf, 1);
  5347. if (ftrace_graph_notrace_buf[0])
  5348. set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
  5349. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  5350. }
  5351. int ftrace_regex_release(struct inode *inode, struct file *file)
  5352. {
  5353. struct seq_file *m = (struct seq_file *)file->private_data;
  5354. struct ftrace_iterator *iter;
  5355. struct ftrace_hash **orig_hash;
  5356. struct trace_parser *parser;
  5357. int filter_hash;
  5358. if (file->f_mode & FMODE_READ) {
  5359. iter = m->private;
  5360. seq_release(inode, file);
  5361. } else
  5362. iter = file->private_data;
  5363. parser = &iter->parser;
  5364. if (trace_parser_loaded(parser)) {
  5365. int enable = !(iter->flags & FTRACE_ITER_NOTRACE);
  5366. ftrace_process_regex(iter, parser->buffer,
  5367. parser->idx, enable);
  5368. }
  5369. trace_parser_put(parser);
  5370. mutex_lock(&iter->ops->func_hash->regex_lock);
  5371. if (file->f_mode & FMODE_WRITE) {
  5372. filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
  5373. if (filter_hash) {
  5374. orig_hash = &iter->ops->func_hash->filter_hash;
  5375. if (iter->tr) {
  5376. if (list_empty(&iter->tr->mod_trace))
  5377. iter->hash->flags &= ~FTRACE_HASH_FL_MOD;
  5378. else
  5379. iter->hash->flags |= FTRACE_HASH_FL_MOD;
  5380. }
  5381. } else
  5382. orig_hash = &iter->ops->func_hash->notrace_hash;
  5383. mutex_lock(&ftrace_lock);
  5384. ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
  5385. iter->hash, filter_hash);
  5386. mutex_unlock(&ftrace_lock);
  5387. }
  5388. mutex_unlock(&iter->ops->func_hash->regex_lock);
  5389. free_ftrace_hash(iter->hash);
  5390. if (iter->tr)
  5391. trace_array_put(iter->tr);
  5392. kfree(iter);
  5393. return 0;
  5394. }
  5395. static const struct file_operations ftrace_avail_fops = {
  5396. .open = ftrace_avail_open,
  5397. .read = seq_read,
  5398. .llseek = seq_lseek,
  5399. .release = seq_release_private,
  5400. };
  5401. static const struct file_operations ftrace_enabled_fops = {
  5402. .open = ftrace_enabled_open,
  5403. .read = seq_read,
  5404. .llseek = seq_lseek,
  5405. .release = seq_release_private,
  5406. };
  5407. static const struct file_operations ftrace_touched_fops = {
  5408. .open = ftrace_touched_open,
  5409. .read = seq_read,
  5410. .llseek = seq_lseek,
  5411. .release = seq_release_private,
  5412. };
  5413. static const struct file_operations ftrace_avail_addrs_fops = {
  5414. .open = ftrace_avail_addrs_open,
  5415. .read = seq_read,
  5416. .llseek = seq_lseek,
  5417. .release = seq_release_private,
  5418. };
  5419. static const struct file_operations ftrace_filter_fops = {
  5420. .open = ftrace_filter_open,
  5421. .read = seq_read,
  5422. .write = ftrace_filter_write,
  5423. .llseek = tracing_lseek,
  5424. .release = ftrace_regex_release,
  5425. };
  5426. static const struct file_operations ftrace_notrace_fops = {
  5427. .open = ftrace_notrace_open,
  5428. .read = seq_read,
  5429. .write = ftrace_notrace_write,
  5430. .llseek = tracing_lseek,
  5431. .release = ftrace_regex_release,
  5432. };
  5433. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  5434. static DEFINE_MUTEX(graph_lock);
  5435. struct ftrace_hash __rcu *ftrace_graph_hash = EMPTY_HASH;
  5436. struct ftrace_hash __rcu *ftrace_graph_notrace_hash = EMPTY_HASH;
  5437. enum graph_filter_type {
  5438. GRAPH_FILTER_NOTRACE = 0,
  5439. GRAPH_FILTER_FUNCTION,
  5440. };
  5441. #define FTRACE_GRAPH_EMPTY ((void *)1)
  5442. struct ftrace_graph_data {
  5443. struct ftrace_hash *hash;
  5444. struct ftrace_func_entry *entry;
  5445. int idx; /* for hash table iteration */
  5446. enum graph_filter_type type;
  5447. struct ftrace_hash *new_hash;
  5448. const struct seq_operations *seq_ops;
  5449. struct trace_parser parser;
  5450. };
  5451. static void *
  5452. __g_next(struct seq_file *m, loff_t *pos)
  5453. {
  5454. struct ftrace_graph_data *fgd = m->private;
  5455. struct ftrace_func_entry *entry = fgd->entry;
  5456. struct hlist_head *head;
  5457. int i, idx = fgd->idx;
  5458. if (*pos >= fgd->hash->count)
  5459. return NULL;
  5460. if (entry) {
  5461. hlist_for_each_entry_continue(entry, hlist) {
  5462. fgd->entry = entry;
  5463. return entry;
  5464. }
  5465. idx++;
  5466. }
  5467. for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
  5468. head = &fgd->hash->buckets[i];
  5469. hlist_for_each_entry(entry, head, hlist) {
  5470. fgd->entry = entry;
  5471. fgd->idx = i;
  5472. return entry;
  5473. }
  5474. }
  5475. return NULL;
  5476. }
  5477. static void *
  5478. g_next(struct seq_file *m, void *v, loff_t *pos)
  5479. {
  5480. (*pos)++;
  5481. return __g_next(m, pos);
  5482. }
  5483. static void *g_start(struct seq_file *m, loff_t *pos)
  5484. {
  5485. struct ftrace_graph_data *fgd = m->private;
  5486. mutex_lock(&graph_lock);
  5487. if (fgd->type == GRAPH_FILTER_FUNCTION)
  5488. fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
  5489. lockdep_is_held(&graph_lock));
  5490. else
  5491. fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
  5492. lockdep_is_held(&graph_lock));
  5493. /* Nothing, tell g_show to print all functions are enabled */
  5494. if (ftrace_hash_empty(fgd->hash) && !*pos)
  5495. return FTRACE_GRAPH_EMPTY;
  5496. fgd->idx = 0;
  5497. fgd->entry = NULL;
  5498. return __g_next(m, pos);
  5499. }
  5500. static void g_stop(struct seq_file *m, void *p)
  5501. {
  5502. mutex_unlock(&graph_lock);
  5503. }
  5504. static int g_show(struct seq_file *m, void *v)
  5505. {
  5506. struct ftrace_func_entry *entry = v;
  5507. if (!entry)
  5508. return 0;
  5509. if (entry == FTRACE_GRAPH_EMPTY) {
  5510. struct ftrace_graph_data *fgd = m->private;
  5511. if (fgd->type == GRAPH_FILTER_FUNCTION)
  5512. seq_puts(m, "#### all functions enabled ####\n");
  5513. else
  5514. seq_puts(m, "#### no functions disabled ####\n");
  5515. return 0;
  5516. }
  5517. seq_printf(m, "%ps\n", (void *)entry->ip);
  5518. return 0;
  5519. }
  5520. static const struct seq_operations ftrace_graph_seq_ops = {
  5521. .start = g_start,
  5522. .next = g_next,
  5523. .stop = g_stop,
  5524. .show = g_show,
  5525. };
  5526. static int
  5527. __ftrace_graph_open(struct inode *inode, struct file *file,
  5528. struct ftrace_graph_data *fgd)
  5529. {
  5530. int ret;
  5531. struct ftrace_hash *new_hash = NULL;
  5532. ret = security_locked_down(LOCKDOWN_TRACEFS);
  5533. if (ret)
  5534. return ret;
  5535. if (file->f_mode & FMODE_WRITE) {
  5536. const int size_bits = FTRACE_HASH_DEFAULT_BITS;
  5537. if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
  5538. return -ENOMEM;
  5539. if (file->f_flags & O_TRUNC)
  5540. new_hash = alloc_ftrace_hash(size_bits);
  5541. else
  5542. new_hash = alloc_and_copy_ftrace_hash(size_bits,
  5543. fgd->hash);
  5544. if (!new_hash) {
  5545. ret = -ENOMEM;
  5546. goto out;
  5547. }
  5548. }
  5549. if (file->f_mode & FMODE_READ) {
  5550. ret = seq_open(file, &ftrace_graph_seq_ops);
  5551. if (!ret) {
  5552. struct seq_file *m = file->private_data;
  5553. m->private = fgd;
  5554. } else {
  5555. /* Failed */
  5556. free_ftrace_hash(new_hash);
  5557. new_hash = NULL;
  5558. }
  5559. } else
  5560. file->private_data = fgd;
  5561. out:
  5562. if (ret < 0 && file->f_mode & FMODE_WRITE)
  5563. trace_parser_put(&fgd->parser);
  5564. fgd->new_hash = new_hash;
  5565. /*
  5566. * All uses of fgd->hash must be taken with the graph_lock
  5567. * held. The graph_lock is going to be released, so force
  5568. * fgd->hash to be reinitialized when it is taken again.
  5569. */
  5570. fgd->hash = NULL;
  5571. return ret;
  5572. }
  5573. static int
  5574. ftrace_graph_open(struct inode *inode, struct file *file)
  5575. {
  5576. struct ftrace_graph_data *fgd;
  5577. int ret;
  5578. if (unlikely(ftrace_disabled))
  5579. return -ENODEV;
  5580. fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
  5581. if (fgd == NULL)
  5582. return -ENOMEM;
  5583. mutex_lock(&graph_lock);
  5584. fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
  5585. lockdep_is_held(&graph_lock));
  5586. fgd->type = GRAPH_FILTER_FUNCTION;
  5587. fgd->seq_ops = &ftrace_graph_seq_ops;
  5588. ret = __ftrace_graph_open(inode, file, fgd);
  5589. if (ret < 0)
  5590. kfree(fgd);
  5591. mutex_unlock(&graph_lock);
  5592. return ret;
  5593. }
  5594. static int
  5595. ftrace_graph_notrace_open(struct inode *inode, struct file *file)
  5596. {
  5597. struct ftrace_graph_data *fgd;
  5598. int ret;
  5599. if (unlikely(ftrace_disabled))
  5600. return -ENODEV;
  5601. fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
  5602. if (fgd == NULL)
  5603. return -ENOMEM;
  5604. mutex_lock(&graph_lock);
  5605. fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
  5606. lockdep_is_held(&graph_lock));
  5607. fgd->type = GRAPH_FILTER_NOTRACE;
  5608. fgd->seq_ops = &ftrace_graph_seq_ops;
  5609. ret = __ftrace_graph_open(inode, file, fgd);
  5610. if (ret < 0)
  5611. kfree(fgd);
  5612. mutex_unlock(&graph_lock);
  5613. return ret;
  5614. }
  5615. static int
  5616. ftrace_graph_release(struct inode *inode, struct file *file)
  5617. {
  5618. struct ftrace_graph_data *fgd;
  5619. struct ftrace_hash *old_hash, *new_hash;
  5620. struct trace_parser *parser;
  5621. int ret = 0;
  5622. if (file->f_mode & FMODE_READ) {
  5623. struct seq_file *m = file->private_data;
  5624. fgd = m->private;
  5625. seq_release(inode, file);
  5626. } else {
  5627. fgd = file->private_data;
  5628. }
  5629. if (file->f_mode & FMODE_WRITE) {
  5630. parser = &fgd->parser;
  5631. if (trace_parser_loaded((parser))) {
  5632. ret = ftrace_graph_set_hash(fgd->new_hash,
  5633. parser->buffer);
  5634. }
  5635. trace_parser_put(parser);
  5636. new_hash = __ftrace_hash_move(fgd->new_hash);
  5637. if (!new_hash) {
  5638. ret = -ENOMEM;
  5639. goto out;
  5640. }
  5641. mutex_lock(&graph_lock);
  5642. if (fgd->type == GRAPH_FILTER_FUNCTION) {
  5643. old_hash = rcu_dereference_protected(ftrace_graph_hash,
  5644. lockdep_is_held(&graph_lock));
  5645. rcu_assign_pointer(ftrace_graph_hash, new_hash);
  5646. } else {
  5647. old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
  5648. lockdep_is_held(&graph_lock));
  5649. rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
  5650. }
  5651. mutex_unlock(&graph_lock);
  5652. /*
  5653. * We need to do a hard force of sched synchronization.
  5654. * This is because we use preempt_disable() to do RCU, but
  5655. * the function tracers can be called where RCU is not watching
  5656. * (like before user_exit()). We can not rely on the RCU
  5657. * infrastructure to do the synchronization, thus we must do it
  5658. * ourselves.
  5659. */
  5660. if (old_hash != EMPTY_HASH)
  5661. synchronize_rcu_tasks_rude();
  5662. free_ftrace_hash(old_hash);
  5663. }
  5664. out:
  5665. free_ftrace_hash(fgd->new_hash);
  5666. kfree(fgd);
  5667. return ret;
  5668. }
  5669. static int
  5670. ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
  5671. {
  5672. struct ftrace_glob func_g;
  5673. struct dyn_ftrace *rec;
  5674. struct ftrace_page *pg;
  5675. struct ftrace_func_entry *entry;
  5676. int fail = 1;
  5677. int not;
  5678. /* decode regex */
  5679. func_g.type = filter_parse_regex(buffer, strlen(buffer),
  5680. &func_g.search, &not);
  5681. func_g.len = strlen(func_g.search);
  5682. mutex_lock(&ftrace_lock);
  5683. if (unlikely(ftrace_disabled)) {
  5684. mutex_unlock(&ftrace_lock);
  5685. return -ENODEV;
  5686. }
  5687. do_for_each_ftrace_rec(pg, rec) {
  5688. if (rec->flags & FTRACE_FL_DISABLED)
  5689. continue;
  5690. if (ftrace_match_record(rec, &func_g, NULL, 0)) {
  5691. entry = ftrace_lookup_ip(hash, rec->ip);
  5692. if (!not) {
  5693. fail = 0;
  5694. if (entry)
  5695. continue;
  5696. if (add_hash_entry(hash, rec->ip) == NULL)
  5697. goto out;
  5698. } else {
  5699. if (entry) {
  5700. free_hash_entry(hash, entry);
  5701. fail = 0;
  5702. }
  5703. }
  5704. }
  5705. cond_resched();
  5706. } while_for_each_ftrace_rec();
  5707. out:
  5708. mutex_unlock(&ftrace_lock);
  5709. if (fail)
  5710. return -EINVAL;
  5711. return 0;
  5712. }
  5713. static ssize_t
  5714. ftrace_graph_write(struct file *file, const char __user *ubuf,
  5715. size_t cnt, loff_t *ppos)
  5716. {
  5717. ssize_t read, ret = 0;
  5718. struct ftrace_graph_data *fgd = file->private_data;
  5719. struct trace_parser *parser;
  5720. if (!cnt)
  5721. return 0;
  5722. /* Read mode uses seq functions */
  5723. if (file->f_mode & FMODE_READ) {
  5724. struct seq_file *m = file->private_data;
  5725. fgd = m->private;
  5726. }
  5727. parser = &fgd->parser;
  5728. read = trace_get_user(parser, ubuf, cnt, ppos);
  5729. if (read >= 0 && trace_parser_loaded(parser) &&
  5730. !trace_parser_cont(parser)) {
  5731. ret = ftrace_graph_set_hash(fgd->new_hash,
  5732. parser->buffer);
  5733. trace_parser_clear(parser);
  5734. }
  5735. if (!ret)
  5736. ret = read;
  5737. return ret;
  5738. }
  5739. static const struct file_operations ftrace_graph_fops = {
  5740. .open = ftrace_graph_open,
  5741. .read = seq_read,
  5742. .write = ftrace_graph_write,
  5743. .llseek = tracing_lseek,
  5744. .release = ftrace_graph_release,
  5745. };
  5746. static const struct file_operations ftrace_graph_notrace_fops = {
  5747. .open = ftrace_graph_notrace_open,
  5748. .read = seq_read,
  5749. .write = ftrace_graph_write,
  5750. .llseek = tracing_lseek,
  5751. .release = ftrace_graph_release,
  5752. };
  5753. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  5754. void ftrace_create_filter_files(struct ftrace_ops *ops,
  5755. struct dentry *parent)
  5756. {
  5757. trace_create_file("set_ftrace_filter", TRACE_MODE_WRITE, parent,
  5758. ops, &ftrace_filter_fops);
  5759. trace_create_file("set_ftrace_notrace", TRACE_MODE_WRITE, parent,
  5760. ops, &ftrace_notrace_fops);
  5761. }
  5762. /*
  5763. * The name "destroy_filter_files" is really a misnomer. Although
  5764. * in the future, it may actually delete the files, but this is
  5765. * really intended to make sure the ops passed in are disabled
  5766. * and that when this function returns, the caller is free to
  5767. * free the ops.
  5768. *
  5769. * The "destroy" name is only to match the "create" name that this
  5770. * should be paired with.
  5771. */
  5772. void ftrace_destroy_filter_files(struct ftrace_ops *ops)
  5773. {
  5774. mutex_lock(&ftrace_lock);
  5775. if (ops->flags & FTRACE_OPS_FL_ENABLED)
  5776. ftrace_shutdown(ops, 0);
  5777. ops->flags |= FTRACE_OPS_FL_DELETED;
  5778. ftrace_free_filter(ops);
  5779. mutex_unlock(&ftrace_lock);
  5780. }
  5781. static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
  5782. {
  5783. trace_create_file("available_filter_functions", TRACE_MODE_READ,
  5784. d_tracer, NULL, &ftrace_avail_fops);
  5785. trace_create_file("available_filter_functions_addrs", TRACE_MODE_READ,
  5786. d_tracer, NULL, &ftrace_avail_addrs_fops);
  5787. trace_create_file("enabled_functions", TRACE_MODE_READ,
  5788. d_tracer, NULL, &ftrace_enabled_fops);
  5789. trace_create_file("touched_functions", TRACE_MODE_READ,
  5790. d_tracer, NULL, &ftrace_touched_fops);
  5791. ftrace_create_filter_files(&global_ops, d_tracer);
  5792. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  5793. trace_create_file("set_graph_function", TRACE_MODE_WRITE, d_tracer,
  5794. NULL,
  5795. &ftrace_graph_fops);
  5796. trace_create_file("set_graph_notrace", TRACE_MODE_WRITE, d_tracer,
  5797. NULL,
  5798. &ftrace_graph_notrace_fops);
  5799. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  5800. return 0;
  5801. }
  5802. static int ftrace_cmp_ips(const void *a, const void *b)
  5803. {
  5804. const unsigned long *ipa = a;
  5805. const unsigned long *ipb = b;
  5806. if (*ipa > *ipb)
  5807. return 1;
  5808. if (*ipa < *ipb)
  5809. return -1;
  5810. return 0;
  5811. }
  5812. #ifdef CONFIG_FTRACE_SORT_STARTUP_TEST
  5813. static void test_is_sorted(unsigned long *start, unsigned long count)
  5814. {
  5815. int i;
  5816. for (i = 1; i < count; i++) {
  5817. if (WARN(start[i - 1] > start[i],
  5818. "[%d] %pS at %lx is not sorted with %pS at %lx\n", i,
  5819. (void *)start[i - 1], start[i - 1],
  5820. (void *)start[i], start[i]))
  5821. break;
  5822. }
  5823. if (i == count)
  5824. pr_info("ftrace section at %px sorted properly\n", start);
  5825. }
  5826. #else
  5827. static void test_is_sorted(unsigned long *start, unsigned long count)
  5828. {
  5829. }
  5830. #endif
  5831. static int ftrace_process_locs(struct module *mod,
  5832. unsigned long *start,
  5833. unsigned long *end)
  5834. {
  5835. struct ftrace_page *pg_unuse = NULL;
  5836. struct ftrace_page *start_pg;
  5837. struct ftrace_page *pg;
  5838. struct dyn_ftrace *rec;
  5839. unsigned long skipped = 0;
  5840. unsigned long count;
  5841. unsigned long *p;
  5842. unsigned long addr;
  5843. unsigned long flags = 0; /* Shut up gcc */
  5844. int ret = -ENOMEM;
  5845. count = end - start;
  5846. if (!count)
  5847. return 0;
  5848. /*
  5849. * Sorting mcount in vmlinux at build time depend on
  5850. * CONFIG_BUILDTIME_MCOUNT_SORT, while mcount loc in
  5851. * modules can not be sorted at build time.
  5852. */
  5853. if (!IS_ENABLED(CONFIG_BUILDTIME_MCOUNT_SORT) || mod) {
  5854. sort(start, count, sizeof(*start),
  5855. ftrace_cmp_ips, NULL);
  5856. } else {
  5857. test_is_sorted(start, count);
  5858. }
  5859. start_pg = ftrace_allocate_pages(count);
  5860. if (!start_pg)
  5861. return -ENOMEM;
  5862. mutex_lock(&ftrace_lock);
  5863. /*
  5864. * Core and each module needs their own pages, as
  5865. * modules will free them when they are removed.
  5866. * Force a new page to be allocated for modules.
  5867. */
  5868. if (!mod) {
  5869. WARN_ON(ftrace_pages || ftrace_pages_start);
  5870. /* First initialization */
  5871. ftrace_pages = ftrace_pages_start = start_pg;
  5872. } else {
  5873. if (!ftrace_pages)
  5874. goto out;
  5875. if (WARN_ON(ftrace_pages->next)) {
  5876. /* Hmm, we have free pages? */
  5877. while (ftrace_pages->next)
  5878. ftrace_pages = ftrace_pages->next;
  5879. }
  5880. ftrace_pages->next = start_pg;
  5881. }
  5882. p = start;
  5883. pg = start_pg;
  5884. while (p < end) {
  5885. unsigned long end_offset;
  5886. addr = ftrace_call_adjust(*p++);
  5887. /*
  5888. * Some architecture linkers will pad between
  5889. * the different mcount_loc sections of different
  5890. * object files to satisfy alignments.
  5891. * Skip any NULL pointers.
  5892. */
  5893. if (!addr) {
  5894. skipped++;
  5895. continue;
  5896. }
  5897. end_offset = (pg->index+1) * sizeof(pg->records[0]);
  5898. if (end_offset > PAGE_SIZE << pg->order) {
  5899. /* We should have allocated enough */
  5900. if (WARN_ON(!pg->next))
  5901. break;
  5902. pg = pg->next;
  5903. }
  5904. rec = &pg->records[pg->index++];
  5905. rec->ip = addr;
  5906. }
  5907. if (pg->next) {
  5908. pg_unuse = pg->next;
  5909. pg->next = NULL;
  5910. }
  5911. /* Assign the last page to ftrace_pages */
  5912. ftrace_pages = pg;
  5913. /*
  5914. * We only need to disable interrupts on start up
  5915. * because we are modifying code that an interrupt
  5916. * may execute, and the modification is not atomic.
  5917. * But for modules, nothing runs the code we modify
  5918. * until we are finished with it, and there's no
  5919. * reason to cause large interrupt latencies while we do it.
  5920. */
  5921. if (!mod)
  5922. local_irq_save(flags);
  5923. ftrace_update_code(mod, start_pg);
  5924. if (!mod)
  5925. local_irq_restore(flags);
  5926. ret = 0;
  5927. out:
  5928. mutex_unlock(&ftrace_lock);
  5929. /* We should have used all pages unless we skipped some */
  5930. if (pg_unuse) {
  5931. WARN_ON(!skipped);
  5932. /* Need to synchronize with ftrace_location_range() */
  5933. synchronize_rcu();
  5934. ftrace_free_pages(pg_unuse);
  5935. }
  5936. return ret;
  5937. }
  5938. struct ftrace_mod_func {
  5939. struct list_head list;
  5940. char *name;
  5941. unsigned long ip;
  5942. unsigned int size;
  5943. };
  5944. struct ftrace_mod_map {
  5945. struct rcu_head rcu;
  5946. struct list_head list;
  5947. struct module *mod;
  5948. unsigned long start_addr;
  5949. unsigned long end_addr;
  5950. struct list_head funcs;
  5951. unsigned int num_funcs;
  5952. };
  5953. static int ftrace_get_trampoline_kallsym(unsigned int symnum,
  5954. unsigned long *value, char *type,
  5955. char *name, char *module_name,
  5956. int *exported)
  5957. {
  5958. struct ftrace_ops *op;
  5959. list_for_each_entry_rcu(op, &ftrace_ops_trampoline_list, list) {
  5960. if (!op->trampoline || symnum--)
  5961. continue;
  5962. *value = op->trampoline;
  5963. *type = 't';
  5964. strscpy(name, FTRACE_TRAMPOLINE_SYM, KSYM_NAME_LEN);
  5965. strscpy(module_name, FTRACE_TRAMPOLINE_MOD, MODULE_NAME_LEN);
  5966. *exported = 0;
  5967. return 0;
  5968. }
  5969. return -ERANGE;
  5970. }
  5971. #if defined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS) || defined(CONFIG_MODULES)
  5972. /*
  5973. * Check if the current ops references the given ip.
  5974. *
  5975. * If the ops traces all functions, then it was already accounted for.
  5976. * If the ops does not trace the current record function, skip it.
  5977. * If the ops ignores the function via notrace filter, skip it.
  5978. */
  5979. static bool
  5980. ops_references_ip(struct ftrace_ops *ops, unsigned long ip)
  5981. {
  5982. /* If ops isn't enabled, ignore it */
  5983. if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
  5984. return false;
  5985. /* If ops traces all then it includes this function */
  5986. if (ops_traces_mod(ops))
  5987. return true;
  5988. /* The function must be in the filter */
  5989. if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
  5990. !__ftrace_lookup_ip(ops->func_hash->filter_hash, ip))
  5991. return false;
  5992. /* If in notrace hash, we ignore it too */
  5993. if (ftrace_lookup_ip(ops->func_hash->notrace_hash, ip))
  5994. return false;
  5995. return true;
  5996. }
  5997. #endif
  5998. #ifdef CONFIG_MODULES
  5999. #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
  6000. static LIST_HEAD(ftrace_mod_maps);
  6001. static int referenced_filters(struct dyn_ftrace *rec)
  6002. {
  6003. struct ftrace_ops *ops;
  6004. int cnt = 0;
  6005. for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
  6006. if (ops_references_ip(ops, rec->ip)) {
  6007. if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_DIRECT))
  6008. continue;
  6009. if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_IPMODIFY))
  6010. continue;
  6011. cnt++;
  6012. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
  6013. rec->flags |= FTRACE_FL_REGS;
  6014. if (cnt == 1 && ops->trampoline)
  6015. rec->flags |= FTRACE_FL_TRAMP;
  6016. else
  6017. rec->flags &= ~FTRACE_FL_TRAMP;
  6018. }
  6019. }
  6020. return cnt;
  6021. }
  6022. static void
  6023. clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
  6024. {
  6025. struct ftrace_func_entry *entry;
  6026. struct dyn_ftrace *rec;
  6027. int i;
  6028. if (ftrace_hash_empty(hash))
  6029. return;
  6030. for (i = 0; i < pg->index; i++) {
  6031. rec = &pg->records[i];
  6032. entry = __ftrace_lookup_ip(hash, rec->ip);
  6033. /*
  6034. * Do not allow this rec to match again.
  6035. * Yeah, it may waste some memory, but will be removed
  6036. * if/when the hash is modified again.
  6037. */
  6038. if (entry)
  6039. entry->ip = 0;
  6040. }
  6041. }
  6042. /* Clear any records from hashes */
  6043. static void clear_mod_from_hashes(struct ftrace_page *pg)
  6044. {
  6045. struct trace_array *tr;
  6046. mutex_lock(&trace_types_lock);
  6047. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  6048. if (!tr->ops || !tr->ops->func_hash)
  6049. continue;
  6050. mutex_lock(&tr->ops->func_hash->regex_lock);
  6051. clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
  6052. clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
  6053. mutex_unlock(&tr->ops->func_hash->regex_lock);
  6054. }
  6055. mutex_unlock(&trace_types_lock);
  6056. }
  6057. static void ftrace_free_mod_map(struct rcu_head *rcu)
  6058. {
  6059. struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
  6060. struct ftrace_mod_func *mod_func;
  6061. struct ftrace_mod_func *n;
  6062. /* All the contents of mod_map are now not visible to readers */
  6063. list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
  6064. kfree(mod_func->name);
  6065. list_del(&mod_func->list);
  6066. kfree(mod_func);
  6067. }
  6068. kfree(mod_map);
  6069. }
  6070. void ftrace_release_mod(struct module *mod)
  6071. {
  6072. struct ftrace_mod_map *mod_map;
  6073. struct ftrace_mod_map *n;
  6074. struct dyn_ftrace *rec;
  6075. struct ftrace_page **last_pg;
  6076. struct ftrace_page *tmp_page = NULL;
  6077. struct ftrace_page *pg;
  6078. mutex_lock(&ftrace_lock);
  6079. /*
  6080. * To avoid the UAF problem after the module is unloaded, the
  6081. * 'mod_map' resource needs to be released unconditionally.
  6082. */
  6083. list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
  6084. if (mod_map->mod == mod) {
  6085. list_del_rcu(&mod_map->list);
  6086. call_rcu(&mod_map->rcu, ftrace_free_mod_map);
  6087. break;
  6088. }
  6089. }
  6090. if (ftrace_disabled)
  6091. goto out_unlock;
  6092. /*
  6093. * Each module has its own ftrace_pages, remove
  6094. * them from the list.
  6095. */
  6096. last_pg = &ftrace_pages_start;
  6097. for (pg = ftrace_pages_start; pg; pg = *last_pg) {
  6098. rec = &pg->records[0];
  6099. if (within_module(rec->ip, mod)) {
  6100. /*
  6101. * As core pages are first, the first
  6102. * page should never be a module page.
  6103. */
  6104. if (WARN_ON(pg == ftrace_pages_start))
  6105. goto out_unlock;
  6106. /* Check if we are deleting the last page */
  6107. if (pg == ftrace_pages)
  6108. ftrace_pages = next_to_ftrace_page(last_pg);
  6109. ftrace_update_tot_cnt -= pg->index;
  6110. *last_pg = pg->next;
  6111. pg->next = tmp_page;
  6112. tmp_page = pg;
  6113. } else
  6114. last_pg = &pg->next;
  6115. }
  6116. out_unlock:
  6117. mutex_unlock(&ftrace_lock);
  6118. /* Need to synchronize with ftrace_location_range() */
  6119. if (tmp_page)
  6120. synchronize_rcu();
  6121. for (pg = tmp_page; pg; pg = tmp_page) {
  6122. /* Needs to be called outside of ftrace_lock */
  6123. clear_mod_from_hashes(pg);
  6124. if (pg->records) {
  6125. free_pages((unsigned long)pg->records, pg->order);
  6126. ftrace_number_of_pages -= 1 << pg->order;
  6127. }
  6128. tmp_page = pg->next;
  6129. kfree(pg);
  6130. ftrace_number_of_groups--;
  6131. }
  6132. }
  6133. void ftrace_module_enable(struct module *mod)
  6134. {
  6135. struct dyn_ftrace *rec;
  6136. struct ftrace_page *pg;
  6137. mutex_lock(&ftrace_lock);
  6138. if (ftrace_disabled)
  6139. goto out_unlock;
  6140. /*
  6141. * If the tracing is enabled, go ahead and enable the record.
  6142. *
  6143. * The reason not to enable the record immediately is the
  6144. * inherent check of ftrace_make_nop/ftrace_make_call for
  6145. * correct previous instructions. Making first the NOP
  6146. * conversion puts the module to the correct state, thus
  6147. * passing the ftrace_make_call check.
  6148. *
  6149. * We also delay this to after the module code already set the
  6150. * text to read-only, as we now need to set it back to read-write
  6151. * so that we can modify the text.
  6152. */
  6153. if (ftrace_start_up)
  6154. ftrace_arch_code_modify_prepare();
  6155. do_for_each_ftrace_rec(pg, rec) {
  6156. int cnt;
  6157. /*
  6158. * do_for_each_ftrace_rec() is a double loop.
  6159. * module text shares the pg. If a record is
  6160. * not part of this module, then skip this pg,
  6161. * which the "break" will do.
  6162. */
  6163. if (!within_module(rec->ip, mod))
  6164. break;
  6165. /* Weak functions should still be ignored */
  6166. if (!test_for_valid_rec(rec)) {
  6167. /* Clear all other flags. Should not be enabled anyway */
  6168. rec->flags = FTRACE_FL_DISABLED;
  6169. continue;
  6170. }
  6171. cnt = 0;
  6172. /*
  6173. * When adding a module, we need to check if tracers are
  6174. * currently enabled and if they are, and can trace this record,
  6175. * we need to enable the module functions as well as update the
  6176. * reference counts for those function records.
  6177. */
  6178. if (ftrace_start_up)
  6179. cnt += referenced_filters(rec);
  6180. rec->flags &= ~FTRACE_FL_DISABLED;
  6181. rec->flags += cnt;
  6182. if (ftrace_start_up && cnt) {
  6183. int failed = __ftrace_replace_code(rec, 1);
  6184. if (failed) {
  6185. ftrace_bug(failed, rec);
  6186. goto out_loop;
  6187. }
  6188. }
  6189. } while_for_each_ftrace_rec();
  6190. out_loop:
  6191. if (ftrace_start_up)
  6192. ftrace_arch_code_modify_post_process();
  6193. out_unlock:
  6194. mutex_unlock(&ftrace_lock);
  6195. process_cached_mods(mod->name);
  6196. }
  6197. void ftrace_module_init(struct module *mod)
  6198. {
  6199. int ret;
  6200. if (ftrace_disabled || !mod->num_ftrace_callsites)
  6201. return;
  6202. ret = ftrace_process_locs(mod, mod->ftrace_callsites,
  6203. mod->ftrace_callsites + mod->num_ftrace_callsites);
  6204. if (ret)
  6205. pr_warn("ftrace: failed to allocate entries for module '%s' functions\n",
  6206. mod->name);
  6207. }
  6208. static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
  6209. struct dyn_ftrace *rec)
  6210. {
  6211. struct ftrace_mod_func *mod_func;
  6212. unsigned long symsize;
  6213. unsigned long offset;
  6214. char str[KSYM_SYMBOL_LEN];
  6215. char *modname;
  6216. const char *ret;
  6217. ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
  6218. if (!ret)
  6219. return;
  6220. mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
  6221. if (!mod_func)
  6222. return;
  6223. mod_func->name = kstrdup(str, GFP_KERNEL);
  6224. if (!mod_func->name) {
  6225. kfree(mod_func);
  6226. return;
  6227. }
  6228. mod_func->ip = rec->ip - offset;
  6229. mod_func->size = symsize;
  6230. mod_map->num_funcs++;
  6231. list_add_rcu(&mod_func->list, &mod_map->funcs);
  6232. }
  6233. static struct ftrace_mod_map *
  6234. allocate_ftrace_mod_map(struct module *mod,
  6235. unsigned long start, unsigned long end)
  6236. {
  6237. struct ftrace_mod_map *mod_map;
  6238. mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
  6239. if (!mod_map)
  6240. return NULL;
  6241. mod_map->mod = mod;
  6242. mod_map->start_addr = start;
  6243. mod_map->end_addr = end;
  6244. mod_map->num_funcs = 0;
  6245. INIT_LIST_HEAD_RCU(&mod_map->funcs);
  6246. list_add_rcu(&mod_map->list, &ftrace_mod_maps);
  6247. return mod_map;
  6248. }
  6249. static int
  6250. ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
  6251. unsigned long addr, unsigned long *size,
  6252. unsigned long *off, char *sym)
  6253. {
  6254. struct ftrace_mod_func *found_func = NULL;
  6255. struct ftrace_mod_func *mod_func;
  6256. list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
  6257. if (addr >= mod_func->ip &&
  6258. addr < mod_func->ip + mod_func->size) {
  6259. found_func = mod_func;
  6260. break;
  6261. }
  6262. }
  6263. if (found_func) {
  6264. if (size)
  6265. *size = found_func->size;
  6266. if (off)
  6267. *off = addr - found_func->ip;
  6268. return strscpy(sym, found_func->name, KSYM_NAME_LEN);
  6269. }
  6270. return 0;
  6271. }
  6272. int
  6273. ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
  6274. unsigned long *off, char **modname, char *sym)
  6275. {
  6276. struct ftrace_mod_map *mod_map;
  6277. int ret = 0;
  6278. /* mod_map is freed via call_rcu() */
  6279. preempt_disable();
  6280. list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
  6281. ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
  6282. if (ret) {
  6283. if (modname)
  6284. *modname = mod_map->mod->name;
  6285. break;
  6286. }
  6287. }
  6288. preempt_enable();
  6289. return ret;
  6290. }
  6291. int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
  6292. char *type, char *name,
  6293. char *module_name, int *exported)
  6294. {
  6295. struct ftrace_mod_map *mod_map;
  6296. struct ftrace_mod_func *mod_func;
  6297. int ret;
  6298. preempt_disable();
  6299. list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
  6300. if (symnum >= mod_map->num_funcs) {
  6301. symnum -= mod_map->num_funcs;
  6302. continue;
  6303. }
  6304. list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
  6305. if (symnum > 1) {
  6306. symnum--;
  6307. continue;
  6308. }
  6309. *value = mod_func->ip;
  6310. *type = 'T';
  6311. strscpy(name, mod_func->name, KSYM_NAME_LEN);
  6312. strscpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
  6313. *exported = 1;
  6314. preempt_enable();
  6315. return 0;
  6316. }
  6317. WARN_ON(1);
  6318. break;
  6319. }
  6320. ret = ftrace_get_trampoline_kallsym(symnum, value, type, name,
  6321. module_name, exported);
  6322. preempt_enable();
  6323. return ret;
  6324. }
  6325. #else
  6326. static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
  6327. struct dyn_ftrace *rec) { }
  6328. static inline struct ftrace_mod_map *
  6329. allocate_ftrace_mod_map(struct module *mod,
  6330. unsigned long start, unsigned long end)
  6331. {
  6332. return NULL;
  6333. }
  6334. int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
  6335. char *type, char *name, char *module_name,
  6336. int *exported)
  6337. {
  6338. int ret;
  6339. preempt_disable();
  6340. ret = ftrace_get_trampoline_kallsym(symnum, value, type, name,
  6341. module_name, exported);
  6342. preempt_enable();
  6343. return ret;
  6344. }
  6345. #endif /* CONFIG_MODULES */
  6346. struct ftrace_init_func {
  6347. struct list_head list;
  6348. unsigned long ip;
  6349. };
  6350. /* Clear any init ips from hashes */
  6351. static void
  6352. clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
  6353. {
  6354. struct ftrace_func_entry *entry;
  6355. entry = ftrace_lookup_ip(hash, func->ip);
  6356. /*
  6357. * Do not allow this rec to match again.
  6358. * Yeah, it may waste some memory, but will be removed
  6359. * if/when the hash is modified again.
  6360. */
  6361. if (entry)
  6362. entry->ip = 0;
  6363. }
  6364. static void
  6365. clear_func_from_hashes(struct ftrace_init_func *func)
  6366. {
  6367. struct trace_array *tr;
  6368. mutex_lock(&trace_types_lock);
  6369. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  6370. if (!tr->ops || !tr->ops->func_hash)
  6371. continue;
  6372. mutex_lock(&tr->ops->func_hash->regex_lock);
  6373. clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
  6374. clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
  6375. mutex_unlock(&tr->ops->func_hash->regex_lock);
  6376. }
  6377. mutex_unlock(&trace_types_lock);
  6378. }
  6379. static void add_to_clear_hash_list(struct list_head *clear_list,
  6380. struct dyn_ftrace *rec)
  6381. {
  6382. struct ftrace_init_func *func;
  6383. func = kmalloc(sizeof(*func), GFP_KERNEL);
  6384. if (!func) {
  6385. MEM_FAIL(1, "alloc failure, ftrace filter could be stale\n");
  6386. return;
  6387. }
  6388. func->ip = rec->ip;
  6389. list_add(&func->list, clear_list);
  6390. }
  6391. void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
  6392. {
  6393. unsigned long start = (unsigned long)(start_ptr);
  6394. unsigned long end = (unsigned long)(end_ptr);
  6395. struct ftrace_page **last_pg = &ftrace_pages_start;
  6396. struct ftrace_page *tmp_page = NULL;
  6397. struct ftrace_page *pg;
  6398. struct dyn_ftrace *rec;
  6399. struct dyn_ftrace key;
  6400. struct ftrace_mod_map *mod_map = NULL;
  6401. struct ftrace_init_func *func, *func_next;
  6402. LIST_HEAD(clear_hash);
  6403. key.ip = start;
  6404. key.flags = end; /* overload flags, as it is unsigned long */
  6405. mutex_lock(&ftrace_lock);
  6406. /*
  6407. * If we are freeing module init memory, then check if
  6408. * any tracer is active. If so, we need to save a mapping of
  6409. * the module functions being freed with the address.
  6410. */
  6411. if (mod && ftrace_ops_list != &ftrace_list_end)
  6412. mod_map = allocate_ftrace_mod_map(mod, start, end);
  6413. for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
  6414. if (end < pg->records[0].ip ||
  6415. start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
  6416. continue;
  6417. again:
  6418. rec = bsearch(&key, pg->records, pg->index,
  6419. sizeof(struct dyn_ftrace),
  6420. ftrace_cmp_recs);
  6421. if (!rec)
  6422. continue;
  6423. /* rec will be cleared from hashes after ftrace_lock unlock */
  6424. add_to_clear_hash_list(&clear_hash, rec);
  6425. if (mod_map)
  6426. save_ftrace_mod_rec(mod_map, rec);
  6427. pg->index--;
  6428. ftrace_update_tot_cnt--;
  6429. if (!pg->index) {
  6430. *last_pg = pg->next;
  6431. pg->next = tmp_page;
  6432. tmp_page = pg;
  6433. pg = container_of(last_pg, struct ftrace_page, next);
  6434. if (!(*last_pg))
  6435. ftrace_pages = pg;
  6436. continue;
  6437. }
  6438. memmove(rec, rec + 1,
  6439. (pg->index - (rec - pg->records)) * sizeof(*rec));
  6440. /* More than one function may be in this block */
  6441. goto again;
  6442. }
  6443. mutex_unlock(&ftrace_lock);
  6444. list_for_each_entry_safe(func, func_next, &clear_hash, list) {
  6445. clear_func_from_hashes(func);
  6446. kfree(func);
  6447. }
  6448. /* Need to synchronize with ftrace_location_range() */
  6449. if (tmp_page) {
  6450. synchronize_rcu();
  6451. ftrace_free_pages(tmp_page);
  6452. }
  6453. }
  6454. void __init ftrace_free_init_mem(void)
  6455. {
  6456. void *start = (void *)(&__init_begin);
  6457. void *end = (void *)(&__init_end);
  6458. ftrace_boot_snapshot();
  6459. ftrace_free_mem(NULL, start, end);
  6460. }
  6461. int __init __weak ftrace_dyn_arch_init(void)
  6462. {
  6463. return 0;
  6464. }
  6465. void __init ftrace_init(void)
  6466. {
  6467. extern unsigned long __start_mcount_loc[];
  6468. extern unsigned long __stop_mcount_loc[];
  6469. unsigned long count, flags;
  6470. int ret;
  6471. local_irq_save(flags);
  6472. ret = ftrace_dyn_arch_init();
  6473. local_irq_restore(flags);
  6474. if (ret)
  6475. goto failed;
  6476. count = __stop_mcount_loc - __start_mcount_loc;
  6477. if (!count) {
  6478. pr_info("ftrace: No functions to be traced?\n");
  6479. goto failed;
  6480. }
  6481. pr_info("ftrace: allocating %ld entries in %ld pages\n",
  6482. count, DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
  6483. ret = ftrace_process_locs(NULL,
  6484. __start_mcount_loc,
  6485. __stop_mcount_loc);
  6486. if (ret) {
  6487. pr_warn("ftrace: failed to allocate entries for functions\n");
  6488. goto failed;
  6489. }
  6490. pr_info("ftrace: allocated %ld pages with %ld groups\n",
  6491. ftrace_number_of_pages, ftrace_number_of_groups);
  6492. last_ftrace_enabled = ftrace_enabled = 1;
  6493. set_ftrace_early_filters();
  6494. return;
  6495. failed:
  6496. ftrace_disabled = 1;
  6497. }
  6498. /* Do nothing if arch does not support this */
  6499. void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
  6500. {
  6501. }
  6502. static void ftrace_update_trampoline(struct ftrace_ops *ops)
  6503. {
  6504. unsigned long trampoline = ops->trampoline;
  6505. arch_ftrace_update_trampoline(ops);
  6506. if (ops->trampoline && ops->trampoline != trampoline &&
  6507. (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP)) {
  6508. /* Add to kallsyms before the perf events */
  6509. ftrace_add_trampoline_to_kallsyms(ops);
  6510. perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
  6511. ops->trampoline, ops->trampoline_size, false,
  6512. FTRACE_TRAMPOLINE_SYM);
  6513. /*
  6514. * Record the perf text poke event after the ksymbol register
  6515. * event.
  6516. */
  6517. perf_event_text_poke((void *)ops->trampoline, NULL, 0,
  6518. (void *)ops->trampoline,
  6519. ops->trampoline_size);
  6520. }
  6521. }
  6522. void ftrace_init_trace_array(struct trace_array *tr)
  6523. {
  6524. INIT_LIST_HEAD(&tr->func_probes);
  6525. INIT_LIST_HEAD(&tr->mod_trace);
  6526. INIT_LIST_HEAD(&tr->mod_notrace);
  6527. }
  6528. #else
  6529. struct ftrace_ops global_ops = {
  6530. .func = ftrace_stub,
  6531. .flags = FTRACE_OPS_FL_INITIALIZED |
  6532. FTRACE_OPS_FL_PID,
  6533. };
  6534. static int __init ftrace_nodyn_init(void)
  6535. {
  6536. ftrace_enabled = 1;
  6537. return 0;
  6538. }
  6539. core_initcall(ftrace_nodyn_init);
  6540. static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
  6541. static inline void ftrace_startup_all(int command) { }
  6542. static void ftrace_update_trampoline(struct ftrace_ops *ops)
  6543. {
  6544. }
  6545. #endif /* CONFIG_DYNAMIC_FTRACE */
  6546. __init void ftrace_init_global_array_ops(struct trace_array *tr)
  6547. {
  6548. tr->ops = &global_ops;
  6549. tr->ops->private = tr;
  6550. ftrace_init_trace_array(tr);
  6551. init_array_fgraph_ops(tr, tr->ops);
  6552. }
  6553. void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
  6554. {
  6555. /* If we filter on pids, update to use the pid function */
  6556. if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
  6557. if (WARN_ON(tr->ops->func != ftrace_stub))
  6558. printk("ftrace ops had %pS for function\n",
  6559. tr->ops->func);
  6560. }
  6561. tr->ops->func = func;
  6562. tr->ops->private = tr;
  6563. }
  6564. void ftrace_reset_array_ops(struct trace_array *tr)
  6565. {
  6566. tr->ops->func = ftrace_stub;
  6567. }
  6568. static nokprobe_inline void
  6569. __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
  6570. struct ftrace_ops *ignored, struct ftrace_regs *fregs)
  6571. {
  6572. struct pt_regs *regs = ftrace_get_regs(fregs);
  6573. struct ftrace_ops *op;
  6574. int bit;
  6575. /*
  6576. * The ftrace_test_and_set_recursion() will disable preemption,
  6577. * which is required since some of the ops may be dynamically
  6578. * allocated, they must be freed after a synchronize_rcu().
  6579. */
  6580. bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START);
  6581. if (bit < 0)
  6582. return;
  6583. do_for_each_ftrace_op(op, ftrace_ops_list) {
  6584. /* Stub functions don't need to be called nor tested */
  6585. if (op->flags & FTRACE_OPS_FL_STUB)
  6586. continue;
  6587. /*
  6588. * Check the following for each ops before calling their func:
  6589. * if RCU flag is set, then rcu_is_watching() must be true
  6590. * Otherwise test if the ip matches the ops filter
  6591. *
  6592. * If any of the above fails then the op->func() is not executed.
  6593. */
  6594. if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
  6595. ftrace_ops_test(op, ip, regs)) {
  6596. if (FTRACE_WARN_ON(!op->func)) {
  6597. pr_warn("op=%p %pS\n", op, op);
  6598. goto out;
  6599. }
  6600. op->func(ip, parent_ip, op, fregs);
  6601. }
  6602. } while_for_each_ftrace_op(op);
  6603. out:
  6604. trace_clear_recursion(bit);
  6605. }
  6606. /*
  6607. * Some archs only support passing ip and parent_ip. Even though
  6608. * the list function ignores the op parameter, we do not want any
  6609. * C side effects, where a function is called without the caller
  6610. * sending a third parameter.
  6611. * Archs are to support both the regs and ftrace_ops at the same time.
  6612. * If they support ftrace_ops, it is assumed they support regs.
  6613. * If call backs want to use regs, they must either check for regs
  6614. * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
  6615. * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
  6616. * An architecture can pass partial regs with ftrace_ops and still
  6617. * set the ARCH_SUPPORTS_FTRACE_OPS.
  6618. *
  6619. * In vmlinux.lds.h, ftrace_ops_list_func() is defined to be
  6620. * arch_ftrace_ops_list_func.
  6621. */
  6622. #if ARCH_SUPPORTS_FTRACE_OPS
  6623. void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
  6624. struct ftrace_ops *op, struct ftrace_regs *fregs)
  6625. {
  6626. kmsan_unpoison_memory(fregs, sizeof(*fregs));
  6627. __ftrace_ops_list_func(ip, parent_ip, NULL, fregs);
  6628. }
  6629. #else
  6630. void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
  6631. {
  6632. __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
  6633. }
  6634. #endif
  6635. NOKPROBE_SYMBOL(arch_ftrace_ops_list_func);
  6636. /*
  6637. * If there's only one function registered but it does not support
  6638. * recursion, needs RCU protection, then this function will be called
  6639. * by the mcount trampoline.
  6640. */
  6641. static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
  6642. struct ftrace_ops *op, struct ftrace_regs *fregs)
  6643. {
  6644. int bit;
  6645. bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START);
  6646. if (bit < 0)
  6647. return;
  6648. if (!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching())
  6649. op->func(ip, parent_ip, op, fregs);
  6650. trace_clear_recursion(bit);
  6651. }
  6652. NOKPROBE_SYMBOL(ftrace_ops_assist_func);
  6653. /**
  6654. * ftrace_ops_get_func - get the function a trampoline should call
  6655. * @ops: the ops to get the function for
  6656. *
  6657. * Normally the mcount trampoline will call the ops->func, but there
  6658. * are times that it should not. For example, if the ops does not
  6659. * have its own recursion protection, then it should call the
  6660. * ftrace_ops_assist_func() instead.
  6661. *
  6662. * Returns: the function that the trampoline should call for @ops.
  6663. */
  6664. ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
  6665. {
  6666. /*
  6667. * If the function does not handle recursion or needs to be RCU safe,
  6668. * then we need to call the assist handler.
  6669. */
  6670. if (ops->flags & (FTRACE_OPS_FL_RECURSION |
  6671. FTRACE_OPS_FL_RCU))
  6672. return ftrace_ops_assist_func;
  6673. return ops->func;
  6674. }
  6675. static void
  6676. ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
  6677. struct task_struct *prev,
  6678. struct task_struct *next,
  6679. unsigned int prev_state)
  6680. {
  6681. struct trace_array *tr = data;
  6682. struct trace_pid_list *pid_list;
  6683. struct trace_pid_list *no_pid_list;
  6684. pid_list = rcu_dereference_sched(tr->function_pids);
  6685. no_pid_list = rcu_dereference_sched(tr->function_no_pids);
  6686. if (trace_ignore_this_task(pid_list, no_pid_list, next))
  6687. this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
  6688. FTRACE_PID_IGNORE);
  6689. else
  6690. this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
  6691. next->pid);
  6692. }
  6693. static void
  6694. ftrace_pid_follow_sched_process_fork(void *data,
  6695. struct task_struct *self,
  6696. struct task_struct *task)
  6697. {
  6698. struct trace_pid_list *pid_list;
  6699. struct trace_array *tr = data;
  6700. pid_list = rcu_dereference_sched(tr->function_pids);
  6701. trace_filter_add_remove_task(pid_list, self, task);
  6702. pid_list = rcu_dereference_sched(tr->function_no_pids);
  6703. trace_filter_add_remove_task(pid_list, self, task);
  6704. }
  6705. static void
  6706. ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
  6707. {
  6708. struct trace_pid_list *pid_list;
  6709. struct trace_array *tr = data;
  6710. pid_list = rcu_dereference_sched(tr->function_pids);
  6711. trace_filter_add_remove_task(pid_list, NULL, task);
  6712. pid_list = rcu_dereference_sched(tr->function_no_pids);
  6713. trace_filter_add_remove_task(pid_list, NULL, task);
  6714. }
  6715. void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
  6716. {
  6717. if (enable) {
  6718. register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
  6719. tr);
  6720. register_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
  6721. tr);
  6722. } else {
  6723. unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
  6724. tr);
  6725. unregister_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
  6726. tr);
  6727. }
  6728. }
  6729. static void clear_ftrace_pids(struct trace_array *tr, int type)
  6730. {
  6731. struct trace_pid_list *pid_list;
  6732. struct trace_pid_list *no_pid_list;
  6733. int cpu;
  6734. pid_list = rcu_dereference_protected(tr->function_pids,
  6735. lockdep_is_held(&ftrace_lock));
  6736. no_pid_list = rcu_dereference_protected(tr->function_no_pids,
  6737. lockdep_is_held(&ftrace_lock));
  6738. /* Make sure there's something to do */
  6739. if (!pid_type_enabled(type, pid_list, no_pid_list))
  6740. return;
  6741. /* See if the pids still need to be checked after this */
  6742. if (!still_need_pid_events(type, pid_list, no_pid_list)) {
  6743. unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
  6744. for_each_possible_cpu(cpu)
  6745. per_cpu_ptr(tr->array_buffer.data, cpu)->ftrace_ignore_pid = FTRACE_PID_TRACE;
  6746. }
  6747. if (type & TRACE_PIDS)
  6748. rcu_assign_pointer(tr->function_pids, NULL);
  6749. if (type & TRACE_NO_PIDS)
  6750. rcu_assign_pointer(tr->function_no_pids, NULL);
  6751. /* Wait till all users are no longer using pid filtering */
  6752. synchronize_rcu();
  6753. if ((type & TRACE_PIDS) && pid_list)
  6754. trace_pid_list_free(pid_list);
  6755. if ((type & TRACE_NO_PIDS) && no_pid_list)
  6756. trace_pid_list_free(no_pid_list);
  6757. }
  6758. void ftrace_clear_pids(struct trace_array *tr)
  6759. {
  6760. mutex_lock(&ftrace_lock);
  6761. clear_ftrace_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
  6762. mutex_unlock(&ftrace_lock);
  6763. }
  6764. static void ftrace_pid_reset(struct trace_array *tr, int type)
  6765. {
  6766. mutex_lock(&ftrace_lock);
  6767. clear_ftrace_pids(tr, type);
  6768. ftrace_update_pid_func();
  6769. ftrace_startup_all(0);
  6770. mutex_unlock(&ftrace_lock);
  6771. }
  6772. /* Greater than any max PID */
  6773. #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
  6774. static void *fpid_start(struct seq_file *m, loff_t *pos)
  6775. __acquires(RCU)
  6776. {
  6777. struct trace_pid_list *pid_list;
  6778. struct trace_array *tr = m->private;
  6779. mutex_lock(&ftrace_lock);
  6780. rcu_read_lock_sched();
  6781. pid_list = rcu_dereference_sched(tr->function_pids);
  6782. if (!pid_list)
  6783. return !(*pos) ? FTRACE_NO_PIDS : NULL;
  6784. return trace_pid_start(pid_list, pos);
  6785. }
  6786. static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
  6787. {
  6788. struct trace_array *tr = m->private;
  6789. struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
  6790. if (v == FTRACE_NO_PIDS) {
  6791. (*pos)++;
  6792. return NULL;
  6793. }
  6794. return trace_pid_next(pid_list, v, pos);
  6795. }
  6796. static void fpid_stop(struct seq_file *m, void *p)
  6797. __releases(RCU)
  6798. {
  6799. rcu_read_unlock_sched();
  6800. mutex_unlock(&ftrace_lock);
  6801. }
  6802. static int fpid_show(struct seq_file *m, void *v)
  6803. {
  6804. if (v == FTRACE_NO_PIDS) {
  6805. seq_puts(m, "no pid\n");
  6806. return 0;
  6807. }
  6808. return trace_pid_show(m, v);
  6809. }
  6810. static const struct seq_operations ftrace_pid_sops = {
  6811. .start = fpid_start,
  6812. .next = fpid_next,
  6813. .stop = fpid_stop,
  6814. .show = fpid_show,
  6815. };
  6816. static void *fnpid_start(struct seq_file *m, loff_t *pos)
  6817. __acquires(RCU)
  6818. {
  6819. struct trace_pid_list *pid_list;
  6820. struct trace_array *tr = m->private;
  6821. mutex_lock(&ftrace_lock);
  6822. rcu_read_lock_sched();
  6823. pid_list = rcu_dereference_sched(tr->function_no_pids);
  6824. if (!pid_list)
  6825. return !(*pos) ? FTRACE_NO_PIDS : NULL;
  6826. return trace_pid_start(pid_list, pos);
  6827. }
  6828. static void *fnpid_next(struct seq_file *m, void *v, loff_t *pos)
  6829. {
  6830. struct trace_array *tr = m->private;
  6831. struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_no_pids);
  6832. if (v == FTRACE_NO_PIDS) {
  6833. (*pos)++;
  6834. return NULL;
  6835. }
  6836. return trace_pid_next(pid_list, v, pos);
  6837. }
  6838. static const struct seq_operations ftrace_no_pid_sops = {
  6839. .start = fnpid_start,
  6840. .next = fnpid_next,
  6841. .stop = fpid_stop,
  6842. .show = fpid_show,
  6843. };
  6844. static int pid_open(struct inode *inode, struct file *file, int type)
  6845. {
  6846. const struct seq_operations *seq_ops;
  6847. struct trace_array *tr = inode->i_private;
  6848. struct seq_file *m;
  6849. int ret = 0;
  6850. ret = tracing_check_open_get_tr(tr);
  6851. if (ret)
  6852. return ret;
  6853. if ((file->f_mode & FMODE_WRITE) &&
  6854. (file->f_flags & O_TRUNC))
  6855. ftrace_pid_reset(tr, type);
  6856. switch (type) {
  6857. case TRACE_PIDS:
  6858. seq_ops = &ftrace_pid_sops;
  6859. break;
  6860. case TRACE_NO_PIDS:
  6861. seq_ops = &ftrace_no_pid_sops;
  6862. break;
  6863. default:
  6864. trace_array_put(tr);
  6865. WARN_ON_ONCE(1);
  6866. return -EINVAL;
  6867. }
  6868. ret = seq_open(file, seq_ops);
  6869. if (ret < 0) {
  6870. trace_array_put(tr);
  6871. } else {
  6872. m = file->private_data;
  6873. /* copy tr over to seq ops */
  6874. m->private = tr;
  6875. }
  6876. return ret;
  6877. }
  6878. static int
  6879. ftrace_pid_open(struct inode *inode, struct file *file)
  6880. {
  6881. return pid_open(inode, file, TRACE_PIDS);
  6882. }
  6883. static int
  6884. ftrace_no_pid_open(struct inode *inode, struct file *file)
  6885. {
  6886. return pid_open(inode, file, TRACE_NO_PIDS);
  6887. }
  6888. static void ignore_task_cpu(void *data)
  6889. {
  6890. struct trace_array *tr = data;
  6891. struct trace_pid_list *pid_list;
  6892. struct trace_pid_list *no_pid_list;
  6893. /*
  6894. * This function is called by on_each_cpu() while the
  6895. * event_mutex is held.
  6896. */
  6897. pid_list = rcu_dereference_protected(tr->function_pids,
  6898. mutex_is_locked(&ftrace_lock));
  6899. no_pid_list = rcu_dereference_protected(tr->function_no_pids,
  6900. mutex_is_locked(&ftrace_lock));
  6901. if (trace_ignore_this_task(pid_list, no_pid_list, current))
  6902. this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
  6903. FTRACE_PID_IGNORE);
  6904. else
  6905. this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
  6906. current->pid);
  6907. }
  6908. static ssize_t
  6909. pid_write(struct file *filp, const char __user *ubuf,
  6910. size_t cnt, loff_t *ppos, int type)
  6911. {
  6912. struct seq_file *m = filp->private_data;
  6913. struct trace_array *tr = m->private;
  6914. struct trace_pid_list *filtered_pids;
  6915. struct trace_pid_list *other_pids;
  6916. struct trace_pid_list *pid_list;
  6917. ssize_t ret;
  6918. if (!cnt)
  6919. return 0;
  6920. mutex_lock(&ftrace_lock);
  6921. switch (type) {
  6922. case TRACE_PIDS:
  6923. filtered_pids = rcu_dereference_protected(tr->function_pids,
  6924. lockdep_is_held(&ftrace_lock));
  6925. other_pids = rcu_dereference_protected(tr->function_no_pids,
  6926. lockdep_is_held(&ftrace_lock));
  6927. break;
  6928. case TRACE_NO_PIDS:
  6929. filtered_pids = rcu_dereference_protected(tr->function_no_pids,
  6930. lockdep_is_held(&ftrace_lock));
  6931. other_pids = rcu_dereference_protected(tr->function_pids,
  6932. lockdep_is_held(&ftrace_lock));
  6933. break;
  6934. default:
  6935. ret = -EINVAL;
  6936. WARN_ON_ONCE(1);
  6937. goto out;
  6938. }
  6939. ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
  6940. if (ret < 0)
  6941. goto out;
  6942. switch (type) {
  6943. case TRACE_PIDS:
  6944. rcu_assign_pointer(tr->function_pids, pid_list);
  6945. break;
  6946. case TRACE_NO_PIDS:
  6947. rcu_assign_pointer(tr->function_no_pids, pid_list);
  6948. break;
  6949. }
  6950. if (filtered_pids) {
  6951. synchronize_rcu();
  6952. trace_pid_list_free(filtered_pids);
  6953. } else if (pid_list && !other_pids) {
  6954. /* Register a probe to set whether to ignore the tracing of a task */
  6955. register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
  6956. }
  6957. /*
  6958. * Ignoring of pids is done at task switch. But we have to
  6959. * check for those tasks that are currently running.
  6960. * Always do this in case a pid was appended or removed.
  6961. */
  6962. on_each_cpu(ignore_task_cpu, tr, 1);
  6963. ftrace_update_pid_func();
  6964. ftrace_startup_all(0);
  6965. out:
  6966. mutex_unlock(&ftrace_lock);
  6967. if (ret > 0)
  6968. *ppos += ret;
  6969. return ret;
  6970. }
  6971. static ssize_t
  6972. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  6973. size_t cnt, loff_t *ppos)
  6974. {
  6975. return pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
  6976. }
  6977. static ssize_t
  6978. ftrace_no_pid_write(struct file *filp, const char __user *ubuf,
  6979. size_t cnt, loff_t *ppos)
  6980. {
  6981. return pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
  6982. }
  6983. static int
  6984. ftrace_pid_release(struct inode *inode, struct file *file)
  6985. {
  6986. struct trace_array *tr = inode->i_private;
  6987. trace_array_put(tr);
  6988. return seq_release(inode, file);
  6989. }
  6990. static const struct file_operations ftrace_pid_fops = {
  6991. .open = ftrace_pid_open,
  6992. .write = ftrace_pid_write,
  6993. .read = seq_read,
  6994. .llseek = tracing_lseek,
  6995. .release = ftrace_pid_release,
  6996. };
  6997. static const struct file_operations ftrace_no_pid_fops = {
  6998. .open = ftrace_no_pid_open,
  6999. .write = ftrace_no_pid_write,
  7000. .read = seq_read,
  7001. .llseek = tracing_lseek,
  7002. .release = ftrace_pid_release,
  7003. };
  7004. void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
  7005. {
  7006. trace_create_file("set_ftrace_pid", TRACE_MODE_WRITE, d_tracer,
  7007. tr, &ftrace_pid_fops);
  7008. trace_create_file("set_ftrace_notrace_pid", TRACE_MODE_WRITE,
  7009. d_tracer, tr, &ftrace_no_pid_fops);
  7010. }
  7011. void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
  7012. struct dentry *d_tracer)
  7013. {
  7014. /* Only the top level directory has the dyn_tracefs and profile */
  7015. WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
  7016. ftrace_init_dyn_tracefs(d_tracer);
  7017. ftrace_profile_tracefs(d_tracer);
  7018. }
  7019. /**
  7020. * ftrace_kill - kill ftrace
  7021. *
  7022. * This function should be used by panic code. It stops ftrace
  7023. * but in a not so nice way. If you need to simply kill ftrace
  7024. * from a non-atomic section, use ftrace_kill.
  7025. */
  7026. void ftrace_kill(void)
  7027. {
  7028. ftrace_disabled = 1;
  7029. ftrace_enabled = 0;
  7030. ftrace_trace_function = ftrace_stub;
  7031. kprobe_ftrace_kill();
  7032. }
  7033. /**
  7034. * ftrace_is_dead - Test if ftrace is dead or not.
  7035. *
  7036. * Returns: 1 if ftrace is "dead", zero otherwise.
  7037. */
  7038. int ftrace_is_dead(void)
  7039. {
  7040. return ftrace_disabled;
  7041. }
  7042. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
  7043. /*
  7044. * When registering ftrace_ops with IPMODIFY, it is necessary to make sure
  7045. * it doesn't conflict with any direct ftrace_ops. If there is existing
  7046. * direct ftrace_ops on a kernel function being patched, call
  7047. * FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER on it to enable sharing.
  7048. *
  7049. * @ops: ftrace_ops being registered.
  7050. *
  7051. * Returns:
  7052. * 0 on success;
  7053. * Negative on failure.
  7054. */
  7055. static int prepare_direct_functions_for_ipmodify(struct ftrace_ops *ops)
  7056. {
  7057. struct ftrace_func_entry *entry;
  7058. struct ftrace_hash *hash;
  7059. struct ftrace_ops *op;
  7060. int size, i, ret;
  7061. lockdep_assert_held_once(&direct_mutex);
  7062. if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
  7063. return 0;
  7064. hash = ops->func_hash->filter_hash;
  7065. size = 1 << hash->size_bits;
  7066. for (i = 0; i < size; i++) {
  7067. hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
  7068. unsigned long ip = entry->ip;
  7069. bool found_op = false;
  7070. mutex_lock(&ftrace_lock);
  7071. do_for_each_ftrace_op(op, ftrace_ops_list) {
  7072. if (!(op->flags & FTRACE_OPS_FL_DIRECT))
  7073. continue;
  7074. if (ops_references_ip(op, ip)) {
  7075. found_op = true;
  7076. break;
  7077. }
  7078. } while_for_each_ftrace_op(op);
  7079. mutex_unlock(&ftrace_lock);
  7080. if (found_op) {
  7081. if (!op->ops_func)
  7082. return -EBUSY;
  7083. ret = op->ops_func(op, FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER);
  7084. if (ret)
  7085. return ret;
  7086. }
  7087. }
  7088. }
  7089. return 0;
  7090. }
  7091. /*
  7092. * Similar to prepare_direct_functions_for_ipmodify, clean up after ops
  7093. * with IPMODIFY is unregistered. The cleanup is optional for most DIRECT
  7094. * ops.
  7095. */
  7096. static void cleanup_direct_functions_after_ipmodify(struct ftrace_ops *ops)
  7097. {
  7098. struct ftrace_func_entry *entry;
  7099. struct ftrace_hash *hash;
  7100. struct ftrace_ops *op;
  7101. int size, i;
  7102. if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
  7103. return;
  7104. mutex_lock(&direct_mutex);
  7105. hash = ops->func_hash->filter_hash;
  7106. size = 1 << hash->size_bits;
  7107. for (i = 0; i < size; i++) {
  7108. hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
  7109. unsigned long ip = entry->ip;
  7110. bool found_op = false;
  7111. mutex_lock(&ftrace_lock);
  7112. do_for_each_ftrace_op(op, ftrace_ops_list) {
  7113. if (!(op->flags & FTRACE_OPS_FL_DIRECT))
  7114. continue;
  7115. if (ops_references_ip(op, ip)) {
  7116. found_op = true;
  7117. break;
  7118. }
  7119. } while_for_each_ftrace_op(op);
  7120. mutex_unlock(&ftrace_lock);
  7121. /* The cleanup is optional, ignore any errors */
  7122. if (found_op && op->ops_func)
  7123. op->ops_func(op, FTRACE_OPS_CMD_DISABLE_SHARE_IPMODIFY_PEER);
  7124. }
  7125. }
  7126. mutex_unlock(&direct_mutex);
  7127. }
  7128. #define lock_direct_mutex() mutex_lock(&direct_mutex)
  7129. #define unlock_direct_mutex() mutex_unlock(&direct_mutex)
  7130. #else /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
  7131. static int prepare_direct_functions_for_ipmodify(struct ftrace_ops *ops)
  7132. {
  7133. return 0;
  7134. }
  7135. static void cleanup_direct_functions_after_ipmodify(struct ftrace_ops *ops)
  7136. {
  7137. }
  7138. #define lock_direct_mutex() do { } while (0)
  7139. #define unlock_direct_mutex() do { } while (0)
  7140. #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
  7141. /*
  7142. * Similar to register_ftrace_function, except we don't lock direct_mutex.
  7143. */
  7144. static int register_ftrace_function_nolock(struct ftrace_ops *ops)
  7145. {
  7146. int ret;
  7147. ftrace_ops_init(ops);
  7148. mutex_lock(&ftrace_lock);
  7149. ret = ftrace_startup(ops, 0);
  7150. mutex_unlock(&ftrace_lock);
  7151. return ret;
  7152. }
  7153. /**
  7154. * register_ftrace_function - register a function for profiling
  7155. * @ops: ops structure that holds the function for profiling.
  7156. *
  7157. * Register a function to be called by all functions in the
  7158. * kernel.
  7159. *
  7160. * Note: @ops->func and all the functions it calls must be labeled
  7161. * with "notrace", otherwise it will go into a
  7162. * recursive loop.
  7163. */
  7164. int register_ftrace_function(struct ftrace_ops *ops)
  7165. {
  7166. int ret;
  7167. lock_direct_mutex();
  7168. ret = prepare_direct_functions_for_ipmodify(ops);
  7169. if (ret < 0)
  7170. goto out_unlock;
  7171. ret = register_ftrace_function_nolock(ops);
  7172. out_unlock:
  7173. unlock_direct_mutex();
  7174. return ret;
  7175. }
  7176. EXPORT_SYMBOL_GPL(register_ftrace_function);
  7177. /**
  7178. * unregister_ftrace_function - unregister a function for profiling.
  7179. * @ops: ops structure that holds the function to unregister
  7180. *
  7181. * Unregister a function that was added to be called by ftrace profiling.
  7182. */
  7183. int unregister_ftrace_function(struct ftrace_ops *ops)
  7184. {
  7185. int ret;
  7186. mutex_lock(&ftrace_lock);
  7187. ret = ftrace_shutdown(ops, 0);
  7188. mutex_unlock(&ftrace_lock);
  7189. cleanup_direct_functions_after_ipmodify(ops);
  7190. return ret;
  7191. }
  7192. EXPORT_SYMBOL_GPL(unregister_ftrace_function);
  7193. static int symbols_cmp(const void *a, const void *b)
  7194. {
  7195. const char **str_a = (const char **) a;
  7196. const char **str_b = (const char **) b;
  7197. return strcmp(*str_a, *str_b);
  7198. }
  7199. struct kallsyms_data {
  7200. unsigned long *addrs;
  7201. const char **syms;
  7202. size_t cnt;
  7203. size_t found;
  7204. };
  7205. /* This function gets called for all kernel and module symbols
  7206. * and returns 1 in case we resolved all the requested symbols,
  7207. * 0 otherwise.
  7208. */
  7209. static int kallsyms_callback(void *data, const char *name, unsigned long addr)
  7210. {
  7211. struct kallsyms_data *args = data;
  7212. const char **sym;
  7213. int idx;
  7214. sym = bsearch(&name, args->syms, args->cnt, sizeof(*args->syms), symbols_cmp);
  7215. if (!sym)
  7216. return 0;
  7217. idx = sym - args->syms;
  7218. if (args->addrs[idx])
  7219. return 0;
  7220. if (!ftrace_location(addr))
  7221. return 0;
  7222. args->addrs[idx] = addr;
  7223. args->found++;
  7224. return args->found == args->cnt ? 1 : 0;
  7225. }
  7226. /**
  7227. * ftrace_lookup_symbols - Lookup addresses for array of symbols
  7228. *
  7229. * @sorted_syms: array of symbols pointers symbols to resolve,
  7230. * must be alphabetically sorted
  7231. * @cnt: number of symbols/addresses in @syms/@addrs arrays
  7232. * @addrs: array for storing resulting addresses
  7233. *
  7234. * This function looks up addresses for array of symbols provided in
  7235. * @syms array (must be alphabetically sorted) and stores them in
  7236. * @addrs array, which needs to be big enough to store at least @cnt
  7237. * addresses.
  7238. *
  7239. * Returns: 0 if all provided symbols are found, -ESRCH otherwise.
  7240. */
  7241. int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *addrs)
  7242. {
  7243. struct kallsyms_data args;
  7244. int found_all;
  7245. memset(addrs, 0, sizeof(*addrs) * cnt);
  7246. args.addrs = addrs;
  7247. args.syms = sorted_syms;
  7248. args.cnt = cnt;
  7249. args.found = 0;
  7250. found_all = kallsyms_on_each_symbol(kallsyms_callback, &args);
  7251. if (found_all)
  7252. return 0;
  7253. found_all = module_kallsyms_on_each_symbol(NULL, kallsyms_callback, &args);
  7254. return found_all ? 0 : -ESRCH;
  7255. }
  7256. #ifdef CONFIG_SYSCTL
  7257. #ifdef CONFIG_DYNAMIC_FTRACE
  7258. static void ftrace_startup_sysctl(void)
  7259. {
  7260. int command;
  7261. if (unlikely(ftrace_disabled))
  7262. return;
  7263. /* Force update next time */
  7264. saved_ftrace_func = NULL;
  7265. /* ftrace_start_up is true if we want ftrace running */
  7266. if (ftrace_start_up) {
  7267. command = FTRACE_UPDATE_CALLS;
  7268. if (ftrace_graph_active)
  7269. command |= FTRACE_START_FUNC_RET;
  7270. ftrace_startup_enable(command);
  7271. }
  7272. }
  7273. static void ftrace_shutdown_sysctl(void)
  7274. {
  7275. int command;
  7276. if (unlikely(ftrace_disabled))
  7277. return;
  7278. /* ftrace_start_up is true if ftrace is running */
  7279. if (ftrace_start_up) {
  7280. command = FTRACE_DISABLE_CALLS;
  7281. if (ftrace_graph_active)
  7282. command |= FTRACE_STOP_FUNC_RET;
  7283. ftrace_run_update_code(command);
  7284. }
  7285. }
  7286. #else
  7287. # define ftrace_startup_sysctl() do { } while (0)
  7288. # define ftrace_shutdown_sysctl() do { } while (0)
  7289. #endif /* CONFIG_DYNAMIC_FTRACE */
  7290. static bool is_permanent_ops_registered(void)
  7291. {
  7292. struct ftrace_ops *op;
  7293. do_for_each_ftrace_op(op, ftrace_ops_list) {
  7294. if (op->flags & FTRACE_OPS_FL_PERMANENT)
  7295. return true;
  7296. } while_for_each_ftrace_op(op);
  7297. return false;
  7298. }
  7299. static int
  7300. ftrace_enable_sysctl(const struct ctl_table *table, int write,
  7301. void *buffer, size_t *lenp, loff_t *ppos)
  7302. {
  7303. int ret = -ENODEV;
  7304. mutex_lock(&ftrace_lock);
  7305. if (unlikely(ftrace_disabled))
  7306. goto out;
  7307. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  7308. if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
  7309. goto out;
  7310. if (ftrace_enabled) {
  7311. /* we are starting ftrace again */
  7312. if (rcu_dereference_protected(ftrace_ops_list,
  7313. lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
  7314. update_ftrace_function();
  7315. ftrace_startup_sysctl();
  7316. } else {
  7317. if (is_permanent_ops_registered()) {
  7318. ftrace_enabled = true;
  7319. ret = -EBUSY;
  7320. goto out;
  7321. }
  7322. /* stopping ftrace calls (just send to ftrace_stub) */
  7323. ftrace_trace_function = ftrace_stub;
  7324. ftrace_shutdown_sysctl();
  7325. }
  7326. last_ftrace_enabled = !!ftrace_enabled;
  7327. out:
  7328. mutex_unlock(&ftrace_lock);
  7329. return ret;
  7330. }
  7331. static struct ctl_table ftrace_sysctls[] = {
  7332. {
  7333. .procname = "ftrace_enabled",
  7334. .data = &ftrace_enabled,
  7335. .maxlen = sizeof(int),
  7336. .mode = 0644,
  7337. .proc_handler = ftrace_enable_sysctl,
  7338. },
  7339. };
  7340. static int __init ftrace_sysctl_init(void)
  7341. {
  7342. register_sysctl_init("kernel", ftrace_sysctls);
  7343. return 0;
  7344. }
  7345. late_initcall(ftrace_sysctl_init);
  7346. #endif