ftrace.c 139 KB

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