ftrace.c 102 KB

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