ftrace.c 212 KB

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