ftrace.c 104 KB

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