ftrace.c 138 KB

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