ftrace.c 127 KB

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