ftrace.c 114 KB

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