ftrace.c 117 KB

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