ftrace.c 140 KB

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