ftrace.c 125 KB

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