ftrace.c 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660
  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. /* ftrace_enabled is a method to turn ftrace on or off */
  59. int ftrace_enabled __read_mostly;
  60. static int last_ftrace_enabled;
  61. /* Quick disabling of function tracer. */
  62. int function_trace_stop;
  63. /* List for set_ftrace_pid's pids. */
  64. LIST_HEAD(ftrace_pids);
  65. struct ftrace_pid {
  66. struct list_head list;
  67. struct pid *pid;
  68. };
  69. /*
  70. * ftrace_disabled is set when an anomaly is discovered.
  71. * ftrace_disabled is much stronger than ftrace_enabled.
  72. */
  73. static int ftrace_disabled __read_mostly;
  74. static DEFINE_MUTEX(ftrace_lock);
  75. static struct ftrace_ops ftrace_list_end __read_mostly = {
  76. .func = ftrace_stub,
  77. };
  78. static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
  79. static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
  80. static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
  81. ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
  82. static ftrace_func_t __ftrace_trace_function_delay __read_mostly = ftrace_stub;
  83. ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
  84. ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
  85. static struct ftrace_ops global_ops;
  86. static struct ftrace_ops control_ops;
  87. static void
  88. ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
  89. /*
  90. * Traverse the ftrace_global_list, invoking all entries. The reason that we
  91. * can use rcu_dereference_raw() is that elements removed from this list
  92. * are simply leaked, so there is no need to interact with a grace-period
  93. * mechanism. The rcu_dereference_raw() calls are needed to handle
  94. * concurrent insertions into the ftrace_global_list.
  95. *
  96. * Silly Alpha and silly pointer-speculation compiler optimizations!
  97. */
  98. static void ftrace_global_list_func(unsigned long ip,
  99. unsigned long parent_ip)
  100. {
  101. struct ftrace_ops *op;
  102. if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
  103. return;
  104. trace_recursion_set(TRACE_GLOBAL_BIT);
  105. op = rcu_dereference_raw(ftrace_global_list); /*see above*/
  106. while (op != &ftrace_list_end) {
  107. op->func(ip, parent_ip);
  108. op = rcu_dereference_raw(op->next); /*see above*/
  109. };
  110. trace_recursion_clear(TRACE_GLOBAL_BIT);
  111. }
  112. static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
  113. {
  114. if (!test_tsk_trace_trace(current))
  115. return;
  116. ftrace_pid_function(ip, parent_ip);
  117. }
  118. static void set_ftrace_pid_function(ftrace_func_t func)
  119. {
  120. /* do not set ftrace_pid_function to itself! */
  121. if (func != ftrace_pid_func)
  122. ftrace_pid_function = func;
  123. }
  124. /**
  125. * clear_ftrace_function - reset the ftrace function
  126. *
  127. * This NULLs the ftrace function and in essence stops
  128. * tracing. There may be lag
  129. */
  130. void clear_ftrace_function(void)
  131. {
  132. ftrace_trace_function = ftrace_stub;
  133. __ftrace_trace_function = ftrace_stub;
  134. __ftrace_trace_function_delay = ftrace_stub;
  135. ftrace_pid_function = ftrace_stub;
  136. }
  137. #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  138. /*
  139. * For those archs that do not test ftrace_trace_stop in their
  140. * mcount call site, we need to do it from C.
  141. */
  142. static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
  143. {
  144. if (function_trace_stop)
  145. return;
  146. __ftrace_trace_function(ip, parent_ip);
  147. }
  148. #endif
  149. static void control_ops_disable_all(struct ftrace_ops *ops)
  150. {
  151. int cpu;
  152. for_each_possible_cpu(cpu)
  153. *per_cpu_ptr(ops->disabled, cpu) = 1;
  154. }
  155. static int control_ops_alloc(struct ftrace_ops *ops)
  156. {
  157. int __percpu *disabled;
  158. disabled = alloc_percpu(int);
  159. if (!disabled)
  160. return -ENOMEM;
  161. ops->disabled = disabled;
  162. control_ops_disable_all(ops);
  163. return 0;
  164. }
  165. static void control_ops_free(struct ftrace_ops *ops)
  166. {
  167. free_percpu(ops->disabled);
  168. }
  169. static void update_global_ops(void)
  170. {
  171. ftrace_func_t func;
  172. /*
  173. * If there's only one function registered, then call that
  174. * function directly. Otherwise, we need to iterate over the
  175. * registered callers.
  176. */
  177. if (ftrace_global_list == &ftrace_list_end ||
  178. ftrace_global_list->next == &ftrace_list_end)
  179. func = ftrace_global_list->func;
  180. else
  181. func = ftrace_global_list_func;
  182. /* If we filter on pids, update to use the pid function */
  183. if (!list_empty(&ftrace_pids)) {
  184. set_ftrace_pid_function(func);
  185. func = ftrace_pid_func;
  186. }
  187. global_ops.func = func;
  188. }
  189. static void update_ftrace_function(void)
  190. {
  191. ftrace_func_t func;
  192. update_global_ops();
  193. /*
  194. * If we are at the end of the list and this ops is
  195. * not dynamic, then have the mcount trampoline call
  196. * the function directly
  197. */
  198. if (ftrace_ops_list == &ftrace_list_end ||
  199. (ftrace_ops_list->next == &ftrace_list_end &&
  200. !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC)))
  201. func = ftrace_ops_list->func;
  202. else
  203. func = ftrace_ops_list_func;
  204. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  205. ftrace_trace_function = func;
  206. #else
  207. #ifdef CONFIG_DYNAMIC_FTRACE
  208. /* do not update till all functions have been modified */
  209. __ftrace_trace_function_delay = func;
  210. #else
  211. __ftrace_trace_function = func;
  212. #endif
  213. ftrace_trace_function = ftrace_test_stop_func;
  214. #endif
  215. }
  216. static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
  217. {
  218. ops->next = *list;
  219. /*
  220. * We are entering ops into the list but another
  221. * CPU might be walking that list. We need to make sure
  222. * the ops->next pointer is valid before another CPU sees
  223. * the ops pointer included into the list.
  224. */
  225. rcu_assign_pointer(*list, ops);
  226. }
  227. static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
  228. {
  229. struct ftrace_ops **p;
  230. /*
  231. * If we are removing the last function, then simply point
  232. * to the ftrace_stub.
  233. */
  234. if (*list == ops && ops->next == &ftrace_list_end) {
  235. *list = &ftrace_list_end;
  236. return 0;
  237. }
  238. for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
  239. if (*p == ops)
  240. break;
  241. if (*p != ops)
  242. return -1;
  243. *p = (*p)->next;
  244. return 0;
  245. }
  246. static void add_ftrace_list_ops(struct ftrace_ops **list,
  247. struct ftrace_ops *main_ops,
  248. struct ftrace_ops *ops)
  249. {
  250. int first = *list == &ftrace_list_end;
  251. add_ftrace_ops(list, ops);
  252. if (first)
  253. add_ftrace_ops(&ftrace_ops_list, main_ops);
  254. }
  255. static int remove_ftrace_list_ops(struct ftrace_ops **list,
  256. struct ftrace_ops *main_ops,
  257. struct ftrace_ops *ops)
  258. {
  259. int ret = remove_ftrace_ops(list, ops);
  260. if (!ret && *list == &ftrace_list_end)
  261. ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
  262. return ret;
  263. }
  264. static int __register_ftrace_function(struct ftrace_ops *ops)
  265. {
  266. if (ftrace_disabled)
  267. return -ENODEV;
  268. if (FTRACE_WARN_ON(ops == &global_ops))
  269. return -EINVAL;
  270. if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
  271. return -EBUSY;
  272. /* We don't support both control and global flags set. */
  273. if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
  274. return -EINVAL;
  275. if (!core_kernel_data((unsigned long)ops))
  276. ops->flags |= FTRACE_OPS_FL_DYNAMIC;
  277. if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
  278. add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
  279. ops->flags |= FTRACE_OPS_FL_ENABLED;
  280. } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
  281. if (control_ops_alloc(ops))
  282. return -ENOMEM;
  283. add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
  284. } else
  285. add_ftrace_ops(&ftrace_ops_list, ops);
  286. if (ftrace_enabled)
  287. update_ftrace_function();
  288. return 0;
  289. }
  290. static int __unregister_ftrace_function(struct ftrace_ops *ops)
  291. {
  292. int ret;
  293. if (ftrace_disabled)
  294. return -ENODEV;
  295. if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
  296. return -EBUSY;
  297. if (FTRACE_WARN_ON(ops == &global_ops))
  298. return -EINVAL;
  299. if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
  300. ret = remove_ftrace_list_ops(&ftrace_global_list,
  301. &global_ops, ops);
  302. if (!ret)
  303. ops->flags &= ~FTRACE_OPS_FL_ENABLED;
  304. } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
  305. ret = remove_ftrace_list_ops(&ftrace_control_list,
  306. &control_ops, ops);
  307. if (!ret) {
  308. /*
  309. * The ftrace_ops is now removed from the list,
  310. * so there'll be no new users. We must ensure
  311. * all current users are done before we free
  312. * the control data.
  313. */
  314. synchronize_sched();
  315. control_ops_free(ops);
  316. }
  317. } else
  318. ret = remove_ftrace_ops(&ftrace_ops_list, ops);
  319. if (ret < 0)
  320. return ret;
  321. if (ftrace_enabled)
  322. update_ftrace_function();
  323. /*
  324. * Dynamic ops may be freed, we must make sure that all
  325. * callers are done before leaving this function.
  326. */
  327. if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
  328. synchronize_sched();
  329. return 0;
  330. }
  331. static void ftrace_update_pid_func(void)
  332. {
  333. /* Only do something if we are tracing something */
  334. if (ftrace_trace_function == ftrace_stub)
  335. return;
  336. update_ftrace_function();
  337. }
  338. #ifdef CONFIG_FUNCTION_PROFILER
  339. struct ftrace_profile {
  340. struct hlist_node node;
  341. unsigned long ip;
  342. unsigned long counter;
  343. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  344. unsigned long long time;
  345. unsigned long long time_squared;
  346. #endif
  347. };
  348. struct ftrace_profile_page {
  349. struct ftrace_profile_page *next;
  350. unsigned long index;
  351. struct ftrace_profile records[];
  352. };
  353. struct ftrace_profile_stat {
  354. atomic_t disabled;
  355. struct hlist_head *hash;
  356. struct ftrace_profile_page *pages;
  357. struct ftrace_profile_page *start;
  358. struct tracer_stat stat;
  359. };
  360. #define PROFILE_RECORDS_SIZE \
  361. (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
  362. #define PROFILES_PER_PAGE \
  363. (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
  364. static int ftrace_profile_bits __read_mostly;
  365. static int ftrace_profile_enabled __read_mostly;
  366. /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
  367. static DEFINE_MUTEX(ftrace_profile_lock);
  368. static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
  369. #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
  370. static void *
  371. function_stat_next(void *v, int idx)
  372. {
  373. struct ftrace_profile *rec = v;
  374. struct ftrace_profile_page *pg;
  375. pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
  376. again:
  377. if (idx != 0)
  378. rec++;
  379. if ((void *)rec >= (void *)&pg->records[pg->index]) {
  380. pg = pg->next;
  381. if (!pg)
  382. return NULL;
  383. rec = &pg->records[0];
  384. if (!rec->counter)
  385. goto again;
  386. }
  387. return rec;
  388. }
  389. static void *function_stat_start(struct tracer_stat *trace)
  390. {
  391. struct ftrace_profile_stat *stat =
  392. container_of(trace, struct ftrace_profile_stat, stat);
  393. if (!stat || !stat->start)
  394. return NULL;
  395. return function_stat_next(&stat->start->records[0], 0);
  396. }
  397. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  398. /* function graph compares on total time */
  399. static int function_stat_cmp(void *p1, void *p2)
  400. {
  401. struct ftrace_profile *a = p1;
  402. struct ftrace_profile *b = p2;
  403. if (a->time < b->time)
  404. return -1;
  405. if (a->time > b->time)
  406. return 1;
  407. else
  408. return 0;
  409. }
  410. #else
  411. /* not function graph compares against hits */
  412. static int function_stat_cmp(void *p1, void *p2)
  413. {
  414. struct ftrace_profile *a = p1;
  415. struct ftrace_profile *b = p2;
  416. if (a->counter < b->counter)
  417. return -1;
  418. if (a->counter > b->counter)
  419. return 1;
  420. else
  421. return 0;
  422. }
  423. #endif
  424. static int function_stat_headers(struct seq_file *m)
  425. {
  426. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  427. seq_printf(m, " Function "
  428. "Hit Time Avg s^2\n"
  429. " -------- "
  430. "--- ---- --- ---\n");
  431. #else
  432. seq_printf(m, " Function Hit\n"
  433. " -------- ---\n");
  434. #endif
  435. return 0;
  436. }
  437. static int function_stat_show(struct seq_file *m, void *v)
  438. {
  439. struct ftrace_profile *rec = v;
  440. char str[KSYM_SYMBOL_LEN];
  441. int ret = 0;
  442. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  443. static struct trace_seq s;
  444. unsigned long long avg;
  445. unsigned long long stddev;
  446. #endif
  447. mutex_lock(&ftrace_profile_lock);
  448. /* we raced with function_profile_reset() */
  449. if (unlikely(rec->counter == 0)) {
  450. ret = -EBUSY;
  451. goto out;
  452. }
  453. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  454. seq_printf(m, " %-30.30s %10lu", str, rec->counter);
  455. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  456. seq_printf(m, " ");
  457. avg = rec->time;
  458. do_div(avg, rec->counter);
  459. /* Sample standard deviation (s^2) */
  460. if (rec->counter <= 1)
  461. stddev = 0;
  462. else {
  463. stddev = rec->time_squared - rec->counter * avg * avg;
  464. /*
  465. * Divide only 1000 for ns^2 -> us^2 conversion.
  466. * trace_print_graph_duration will divide 1000 again.
  467. */
  468. do_div(stddev, (rec->counter - 1) * 1000);
  469. }
  470. trace_seq_init(&s);
  471. trace_print_graph_duration(rec->time, &s);
  472. trace_seq_puts(&s, " ");
  473. trace_print_graph_duration(avg, &s);
  474. trace_seq_puts(&s, " ");
  475. trace_print_graph_duration(stddev, &s);
  476. trace_print_seq(m, &s);
  477. #endif
  478. seq_putc(m, '\n');
  479. out:
  480. mutex_unlock(&ftrace_profile_lock);
  481. return ret;
  482. }
  483. static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
  484. {
  485. struct ftrace_profile_page *pg;
  486. pg = stat->pages = stat->start;
  487. while (pg) {
  488. memset(pg->records, 0, PROFILE_RECORDS_SIZE);
  489. pg->index = 0;
  490. pg = pg->next;
  491. }
  492. memset(stat->hash, 0,
  493. FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
  494. }
  495. int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
  496. {
  497. struct ftrace_profile_page *pg;
  498. int functions;
  499. int pages;
  500. int i;
  501. /* If we already allocated, do nothing */
  502. if (stat->pages)
  503. return 0;
  504. stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
  505. if (!stat->pages)
  506. return -ENOMEM;
  507. #ifdef CONFIG_DYNAMIC_FTRACE
  508. functions = ftrace_update_tot_cnt;
  509. #else
  510. /*
  511. * We do not know the number of functions that exist because
  512. * dynamic tracing is what counts them. With past experience
  513. * we have around 20K functions. That should be more than enough.
  514. * It is highly unlikely we will execute every function in
  515. * the kernel.
  516. */
  517. functions = 20000;
  518. #endif
  519. pg = stat->start = stat->pages;
  520. pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
  521. for (i = 0; i < pages; i++) {
  522. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  523. if (!pg->next)
  524. goto out_free;
  525. pg = pg->next;
  526. }
  527. return 0;
  528. out_free:
  529. pg = stat->start;
  530. while (pg) {
  531. unsigned long tmp = (unsigned long)pg;
  532. pg = pg->next;
  533. free_page(tmp);
  534. }
  535. free_page((unsigned long)stat->pages);
  536. stat->pages = NULL;
  537. stat->start = NULL;
  538. return -ENOMEM;
  539. }
  540. static int ftrace_profile_init_cpu(int cpu)
  541. {
  542. struct ftrace_profile_stat *stat;
  543. int size;
  544. stat = &per_cpu(ftrace_profile_stats, cpu);
  545. if (stat->hash) {
  546. /* If the profile is already created, simply reset it */
  547. ftrace_profile_reset(stat);
  548. return 0;
  549. }
  550. /*
  551. * We are profiling all functions, but usually only a few thousand
  552. * functions are hit. We'll make a hash of 1024 items.
  553. */
  554. size = FTRACE_PROFILE_HASH_SIZE;
  555. stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
  556. if (!stat->hash)
  557. return -ENOMEM;
  558. if (!ftrace_profile_bits) {
  559. size--;
  560. for (; size; size >>= 1)
  561. ftrace_profile_bits++;
  562. }
  563. /* Preallocate the function profiling pages */
  564. if (ftrace_profile_pages_init(stat) < 0) {
  565. kfree(stat->hash);
  566. stat->hash = NULL;
  567. return -ENOMEM;
  568. }
  569. return 0;
  570. }
  571. static int ftrace_profile_init(void)
  572. {
  573. int cpu;
  574. int ret = 0;
  575. for_each_online_cpu(cpu) {
  576. ret = ftrace_profile_init_cpu(cpu);
  577. if (ret)
  578. break;
  579. }
  580. return ret;
  581. }
  582. /* interrupts must be disabled */
  583. static struct ftrace_profile *
  584. ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
  585. {
  586. struct ftrace_profile *rec;
  587. struct hlist_head *hhd;
  588. struct hlist_node *n;
  589. unsigned long key;
  590. key = hash_long(ip, ftrace_profile_bits);
  591. hhd = &stat->hash[key];
  592. if (hlist_empty(hhd))
  593. return NULL;
  594. hlist_for_each_entry_rcu(rec, n, hhd, node) {
  595. if (rec->ip == ip)
  596. return rec;
  597. }
  598. return NULL;
  599. }
  600. static void ftrace_add_profile(struct ftrace_profile_stat *stat,
  601. struct ftrace_profile *rec)
  602. {
  603. unsigned long key;
  604. key = hash_long(rec->ip, ftrace_profile_bits);
  605. hlist_add_head_rcu(&rec->node, &stat->hash[key]);
  606. }
  607. /*
  608. * The memory is already allocated, this simply finds a new record to use.
  609. */
  610. static struct ftrace_profile *
  611. ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
  612. {
  613. struct ftrace_profile *rec = NULL;
  614. /* prevent recursion (from NMIs) */
  615. if (atomic_inc_return(&stat->disabled) != 1)
  616. goto out;
  617. /*
  618. * Try to find the function again since an NMI
  619. * could have added it
  620. */
  621. rec = ftrace_find_profiled_func(stat, ip);
  622. if (rec)
  623. goto out;
  624. if (stat->pages->index == PROFILES_PER_PAGE) {
  625. if (!stat->pages->next)
  626. goto out;
  627. stat->pages = stat->pages->next;
  628. }
  629. rec = &stat->pages->records[stat->pages->index++];
  630. rec->ip = ip;
  631. ftrace_add_profile(stat, rec);
  632. out:
  633. atomic_dec(&stat->disabled);
  634. return rec;
  635. }
  636. static void
  637. function_profile_call(unsigned long ip, unsigned long parent_ip)
  638. {
  639. struct ftrace_profile_stat *stat;
  640. struct ftrace_profile *rec;
  641. unsigned long flags;
  642. if (!ftrace_profile_enabled)
  643. return;
  644. local_irq_save(flags);
  645. stat = &__get_cpu_var(ftrace_profile_stats);
  646. if (!stat->hash || !ftrace_profile_enabled)
  647. goto out;
  648. rec = ftrace_find_profiled_func(stat, ip);
  649. if (!rec) {
  650. rec = ftrace_profile_alloc(stat, ip);
  651. if (!rec)
  652. goto out;
  653. }
  654. rec->counter++;
  655. out:
  656. local_irq_restore(flags);
  657. }
  658. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  659. static int profile_graph_entry(struct ftrace_graph_ent *trace)
  660. {
  661. function_profile_call(trace->func, 0);
  662. return 1;
  663. }
  664. static void profile_graph_return(struct ftrace_graph_ret *trace)
  665. {
  666. struct ftrace_profile_stat *stat;
  667. unsigned long long calltime;
  668. struct ftrace_profile *rec;
  669. unsigned long flags;
  670. local_irq_save(flags);
  671. stat = &__get_cpu_var(ftrace_profile_stats);
  672. if (!stat->hash || !ftrace_profile_enabled)
  673. goto out;
  674. /* If the calltime was zero'd ignore it */
  675. if (!trace->calltime)
  676. goto out;
  677. calltime = trace->rettime - trace->calltime;
  678. if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
  679. int index;
  680. index = trace->depth;
  681. /* Append this call time to the parent time to subtract */
  682. if (index)
  683. current->ret_stack[index - 1].subtime += calltime;
  684. if (current->ret_stack[index].subtime < calltime)
  685. calltime -= current->ret_stack[index].subtime;
  686. else
  687. calltime = 0;
  688. }
  689. rec = ftrace_find_profiled_func(stat, trace->func);
  690. if (rec) {
  691. rec->time += calltime;
  692. rec->time_squared += calltime * calltime;
  693. }
  694. out:
  695. local_irq_restore(flags);
  696. }
  697. static int register_ftrace_profiler(void)
  698. {
  699. return register_ftrace_graph(&profile_graph_return,
  700. &profile_graph_entry);
  701. }
  702. static void unregister_ftrace_profiler(void)
  703. {
  704. unregister_ftrace_graph();
  705. }
  706. #else
  707. static struct ftrace_ops ftrace_profile_ops __read_mostly = {
  708. .func = function_profile_call,
  709. };
  710. static int register_ftrace_profiler(void)
  711. {
  712. return register_ftrace_function(&ftrace_profile_ops);
  713. }
  714. static void unregister_ftrace_profiler(void)
  715. {
  716. unregister_ftrace_function(&ftrace_profile_ops);
  717. }
  718. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  719. static ssize_t
  720. ftrace_profile_write(struct file *filp, const char __user *ubuf,
  721. size_t cnt, loff_t *ppos)
  722. {
  723. unsigned long val;
  724. int ret;
  725. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  726. if (ret)
  727. return ret;
  728. val = !!val;
  729. mutex_lock(&ftrace_profile_lock);
  730. if (ftrace_profile_enabled ^ val) {
  731. if (val) {
  732. ret = ftrace_profile_init();
  733. if (ret < 0) {
  734. cnt = ret;
  735. goto out;
  736. }
  737. ret = register_ftrace_profiler();
  738. if (ret < 0) {
  739. cnt = ret;
  740. goto out;
  741. }
  742. ftrace_profile_enabled = 1;
  743. } else {
  744. ftrace_profile_enabled = 0;
  745. /*
  746. * unregister_ftrace_profiler calls stop_machine
  747. * so this acts like an synchronize_sched.
  748. */
  749. unregister_ftrace_profiler();
  750. }
  751. }
  752. out:
  753. mutex_unlock(&ftrace_profile_lock);
  754. *ppos += cnt;
  755. return cnt;
  756. }
  757. static ssize_t
  758. ftrace_profile_read(struct file *filp, char __user *ubuf,
  759. size_t cnt, loff_t *ppos)
  760. {
  761. char buf[64]; /* big enough to hold a number */
  762. int r;
  763. r = sprintf(buf, "%u\n", ftrace_profile_enabled);
  764. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  765. }
  766. static const struct file_operations ftrace_profile_fops = {
  767. .open = tracing_open_generic,
  768. .read = ftrace_profile_read,
  769. .write = ftrace_profile_write,
  770. .llseek = default_llseek,
  771. };
  772. /* used to initialize the real stat files */
  773. static struct tracer_stat function_stats __initdata = {
  774. .name = "functions",
  775. .stat_start = function_stat_start,
  776. .stat_next = function_stat_next,
  777. .stat_cmp = function_stat_cmp,
  778. .stat_headers = function_stat_headers,
  779. .stat_show = function_stat_show
  780. };
  781. static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
  782. {
  783. struct ftrace_profile_stat *stat;
  784. struct dentry *entry;
  785. char *name;
  786. int ret;
  787. int cpu;
  788. for_each_possible_cpu(cpu) {
  789. stat = &per_cpu(ftrace_profile_stats, cpu);
  790. /* allocate enough for function name + cpu number */
  791. name = kmalloc(32, GFP_KERNEL);
  792. if (!name) {
  793. /*
  794. * The files created are permanent, if something happens
  795. * we still do not free memory.
  796. */
  797. WARN(1,
  798. "Could not allocate stat file for cpu %d\n",
  799. cpu);
  800. return;
  801. }
  802. stat->stat = function_stats;
  803. snprintf(name, 32, "function%d", cpu);
  804. stat->stat.name = name;
  805. ret = register_stat_tracer(&stat->stat);
  806. if (ret) {
  807. WARN(1,
  808. "Could not register function stat for cpu %d\n",
  809. cpu);
  810. kfree(name);
  811. return;
  812. }
  813. }
  814. entry = debugfs_create_file("function_profile_enabled", 0644,
  815. d_tracer, NULL, &ftrace_profile_fops);
  816. if (!entry)
  817. pr_warning("Could not create debugfs "
  818. "'function_profile_enabled' entry\n");
  819. }
  820. #else /* CONFIG_FUNCTION_PROFILER */
  821. static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
  822. {
  823. }
  824. #endif /* CONFIG_FUNCTION_PROFILER */
  825. static struct pid * const ftrace_swapper_pid = &init_struct_pid;
  826. #ifdef CONFIG_DYNAMIC_FTRACE
  827. #ifndef CONFIG_FTRACE_MCOUNT_RECORD
  828. # error Dynamic ftrace depends on MCOUNT_RECORD
  829. #endif
  830. static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
  831. struct ftrace_func_probe {
  832. struct hlist_node node;
  833. struct ftrace_probe_ops *ops;
  834. unsigned long flags;
  835. unsigned long ip;
  836. void *data;
  837. struct rcu_head rcu;
  838. };
  839. struct ftrace_func_entry {
  840. struct hlist_node hlist;
  841. unsigned long ip;
  842. };
  843. struct ftrace_hash {
  844. unsigned long size_bits;
  845. struct hlist_head *buckets;
  846. unsigned long count;
  847. struct rcu_head rcu;
  848. };
  849. /*
  850. * We make these constant because no one should touch them,
  851. * but they are used as the default "empty hash", to avoid allocating
  852. * it all the time. These are in a read only section such that if
  853. * anyone does try to modify it, it will cause an exception.
  854. */
  855. static const struct hlist_head empty_buckets[1];
  856. static const struct ftrace_hash empty_hash = {
  857. .buckets = (struct hlist_head *)empty_buckets,
  858. };
  859. #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
  860. static struct ftrace_ops global_ops = {
  861. .func = ftrace_stub,
  862. .notrace_hash = EMPTY_HASH,
  863. .filter_hash = EMPTY_HASH,
  864. };
  865. static DEFINE_MUTEX(ftrace_regex_lock);
  866. struct ftrace_page {
  867. struct ftrace_page *next;
  868. struct dyn_ftrace *records;
  869. int index;
  870. int size;
  871. };
  872. static struct ftrace_page *ftrace_new_pgs;
  873. #define ENTRY_SIZE sizeof(struct dyn_ftrace)
  874. #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
  875. /* estimate from running different kernels */
  876. #define NR_TO_INIT 10000
  877. static struct ftrace_page *ftrace_pages_start;
  878. static struct ftrace_page *ftrace_pages;
  879. static bool ftrace_hash_empty(struct ftrace_hash *hash)
  880. {
  881. return !hash || !hash->count;
  882. }
  883. static struct ftrace_func_entry *
  884. ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
  885. {
  886. unsigned long key;
  887. struct ftrace_func_entry *entry;
  888. struct hlist_head *hhd;
  889. struct hlist_node *n;
  890. if (ftrace_hash_empty(hash))
  891. return NULL;
  892. if (hash->size_bits > 0)
  893. key = hash_long(ip, hash->size_bits);
  894. else
  895. key = 0;
  896. hhd = &hash->buckets[key];
  897. hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
  898. if (entry->ip == ip)
  899. return entry;
  900. }
  901. return NULL;
  902. }
  903. static void __add_hash_entry(struct ftrace_hash *hash,
  904. struct ftrace_func_entry *entry)
  905. {
  906. struct hlist_head *hhd;
  907. unsigned long key;
  908. if (hash->size_bits)
  909. key = hash_long(entry->ip, hash->size_bits);
  910. else
  911. key = 0;
  912. hhd = &hash->buckets[key];
  913. hlist_add_head(&entry->hlist, hhd);
  914. hash->count++;
  915. }
  916. static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
  917. {
  918. struct ftrace_func_entry *entry;
  919. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  920. if (!entry)
  921. return -ENOMEM;
  922. entry->ip = ip;
  923. __add_hash_entry(hash, entry);
  924. return 0;
  925. }
  926. static void
  927. free_hash_entry(struct ftrace_hash *hash,
  928. struct ftrace_func_entry *entry)
  929. {
  930. hlist_del(&entry->hlist);
  931. kfree(entry);
  932. hash->count--;
  933. }
  934. static void
  935. remove_hash_entry(struct ftrace_hash *hash,
  936. struct ftrace_func_entry *entry)
  937. {
  938. hlist_del(&entry->hlist);
  939. hash->count--;
  940. }
  941. static void ftrace_hash_clear(struct ftrace_hash *hash)
  942. {
  943. struct hlist_head *hhd;
  944. struct hlist_node *tp, *tn;
  945. struct ftrace_func_entry *entry;
  946. int size = 1 << hash->size_bits;
  947. int i;
  948. if (!hash->count)
  949. return;
  950. for (i = 0; i < size; i++) {
  951. hhd = &hash->buckets[i];
  952. hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
  953. free_hash_entry(hash, entry);
  954. }
  955. FTRACE_WARN_ON(hash->count);
  956. }
  957. static void free_ftrace_hash(struct ftrace_hash *hash)
  958. {
  959. if (!hash || hash == EMPTY_HASH)
  960. return;
  961. ftrace_hash_clear(hash);
  962. kfree(hash->buckets);
  963. kfree(hash);
  964. }
  965. static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
  966. {
  967. struct ftrace_hash *hash;
  968. hash = container_of(rcu, struct ftrace_hash, rcu);
  969. free_ftrace_hash(hash);
  970. }
  971. static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
  972. {
  973. if (!hash || hash == EMPTY_HASH)
  974. return;
  975. call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
  976. }
  977. void ftrace_free_filter(struct ftrace_ops *ops)
  978. {
  979. free_ftrace_hash(ops->filter_hash);
  980. free_ftrace_hash(ops->notrace_hash);
  981. }
  982. static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
  983. {
  984. struct ftrace_hash *hash;
  985. int size;
  986. hash = kzalloc(sizeof(*hash), GFP_KERNEL);
  987. if (!hash)
  988. return NULL;
  989. size = 1 << size_bits;
  990. hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
  991. if (!hash->buckets) {
  992. kfree(hash);
  993. return NULL;
  994. }
  995. hash->size_bits = size_bits;
  996. return hash;
  997. }
  998. static struct ftrace_hash *
  999. alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
  1000. {
  1001. struct ftrace_func_entry *entry;
  1002. struct ftrace_hash *new_hash;
  1003. struct hlist_node *tp;
  1004. int size;
  1005. int ret;
  1006. int i;
  1007. new_hash = alloc_ftrace_hash(size_bits);
  1008. if (!new_hash)
  1009. return NULL;
  1010. /* Empty hash? */
  1011. if (ftrace_hash_empty(hash))
  1012. return new_hash;
  1013. size = 1 << hash->size_bits;
  1014. for (i = 0; i < size; i++) {
  1015. hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
  1016. ret = add_hash_entry(new_hash, entry->ip);
  1017. if (ret < 0)
  1018. goto free_hash;
  1019. }
  1020. }
  1021. FTRACE_WARN_ON(new_hash->count != hash->count);
  1022. return new_hash;
  1023. free_hash:
  1024. free_ftrace_hash(new_hash);
  1025. return NULL;
  1026. }
  1027. static void
  1028. ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
  1029. static void
  1030. ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
  1031. static int
  1032. ftrace_hash_move(struct ftrace_ops *ops, int enable,
  1033. struct ftrace_hash **dst, struct ftrace_hash *src)
  1034. {
  1035. struct ftrace_func_entry *entry;
  1036. struct hlist_node *tp, *tn;
  1037. struct hlist_head *hhd;
  1038. struct ftrace_hash *old_hash;
  1039. struct ftrace_hash *new_hash;
  1040. unsigned long key;
  1041. int size = src->count;
  1042. int bits = 0;
  1043. int ret;
  1044. int i;
  1045. /*
  1046. * Remove the current set, update the hash and add
  1047. * them back.
  1048. */
  1049. ftrace_hash_rec_disable(ops, enable);
  1050. /*
  1051. * If the new source is empty, just free dst and assign it
  1052. * the empty_hash.
  1053. */
  1054. if (!src->count) {
  1055. free_ftrace_hash_rcu(*dst);
  1056. rcu_assign_pointer(*dst, EMPTY_HASH);
  1057. /* still need to update the function records */
  1058. ret = 0;
  1059. goto out;
  1060. }
  1061. /*
  1062. * Make the hash size about 1/2 the # found
  1063. */
  1064. for (size /= 2; size; size >>= 1)
  1065. bits++;
  1066. /* Don't allocate too much */
  1067. if (bits > FTRACE_HASH_MAX_BITS)
  1068. bits = FTRACE_HASH_MAX_BITS;
  1069. ret = -ENOMEM;
  1070. new_hash = alloc_ftrace_hash(bits);
  1071. if (!new_hash)
  1072. goto out;
  1073. size = 1 << src->size_bits;
  1074. for (i = 0; i < size; i++) {
  1075. hhd = &src->buckets[i];
  1076. hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
  1077. if (bits > 0)
  1078. key = hash_long(entry->ip, bits);
  1079. else
  1080. key = 0;
  1081. remove_hash_entry(src, entry);
  1082. __add_hash_entry(new_hash, entry);
  1083. }
  1084. }
  1085. old_hash = *dst;
  1086. rcu_assign_pointer(*dst, new_hash);
  1087. free_ftrace_hash_rcu(old_hash);
  1088. ret = 0;
  1089. out:
  1090. /*
  1091. * Enable regardless of ret:
  1092. * On success, we enable the new hash.
  1093. * On failure, we re-enable the original hash.
  1094. */
  1095. ftrace_hash_rec_enable(ops, enable);
  1096. return ret;
  1097. }
  1098. /*
  1099. * Test the hashes for this ops to see if we want to call
  1100. * the ops->func or not.
  1101. *
  1102. * It's a match if the ip is in the ops->filter_hash or
  1103. * the filter_hash does not exist or is empty,
  1104. * AND
  1105. * the ip is not in the ops->notrace_hash.
  1106. *
  1107. * This needs to be called with preemption disabled as
  1108. * the hashes are freed with call_rcu_sched().
  1109. */
  1110. static int
  1111. ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
  1112. {
  1113. struct ftrace_hash *filter_hash;
  1114. struct ftrace_hash *notrace_hash;
  1115. int ret;
  1116. filter_hash = rcu_dereference_raw(ops->filter_hash);
  1117. notrace_hash = rcu_dereference_raw(ops->notrace_hash);
  1118. if ((ftrace_hash_empty(filter_hash) ||
  1119. ftrace_lookup_ip(filter_hash, ip)) &&
  1120. (ftrace_hash_empty(notrace_hash) ||
  1121. !ftrace_lookup_ip(notrace_hash, ip)))
  1122. ret = 1;
  1123. else
  1124. ret = 0;
  1125. return ret;
  1126. }
  1127. /*
  1128. * This is a double for. Do not use 'break' to break out of the loop,
  1129. * you must use a goto.
  1130. */
  1131. #define do_for_each_ftrace_rec(pg, rec) \
  1132. for (pg = ftrace_pages_start; pg; pg = pg->next) { \
  1133. int _____i; \
  1134. for (_____i = 0; _____i < pg->index; _____i++) { \
  1135. rec = &pg->records[_____i];
  1136. #define while_for_each_ftrace_rec() \
  1137. } \
  1138. }
  1139. static int ftrace_cmp_recs(const void *a, const void *b)
  1140. {
  1141. const struct dyn_ftrace *reca = a;
  1142. const struct dyn_ftrace *recb = b;
  1143. if (reca->ip > recb->ip)
  1144. return 1;
  1145. if (reca->ip < recb->ip)
  1146. return -1;
  1147. return 0;
  1148. }
  1149. /**
  1150. * ftrace_location - return true if the ip giving is a traced location
  1151. * @ip: the instruction pointer to check
  1152. *
  1153. * Returns 1 if @ip given is a pointer to a ftrace location.
  1154. * That is, the instruction that is either a NOP or call to
  1155. * the function tracer. It checks the ftrace internal tables to
  1156. * determine if the address belongs or not.
  1157. */
  1158. int ftrace_location(unsigned long ip)
  1159. {
  1160. struct ftrace_page *pg;
  1161. struct dyn_ftrace *rec;
  1162. struct dyn_ftrace key;
  1163. key.ip = ip;
  1164. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  1165. rec = bsearch(&key, pg->records, pg->index,
  1166. sizeof(struct dyn_ftrace),
  1167. ftrace_cmp_recs);
  1168. if (rec)
  1169. return 1;
  1170. }
  1171. return 0;
  1172. }
  1173. static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
  1174. int filter_hash,
  1175. bool inc)
  1176. {
  1177. struct ftrace_hash *hash;
  1178. struct ftrace_hash *other_hash;
  1179. struct ftrace_page *pg;
  1180. struct dyn_ftrace *rec;
  1181. int count = 0;
  1182. int all = 0;
  1183. /* Only update if the ops has been registered */
  1184. if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
  1185. return;
  1186. /*
  1187. * In the filter_hash case:
  1188. * If the count is zero, we update all records.
  1189. * Otherwise we just update the items in the hash.
  1190. *
  1191. * In the notrace_hash case:
  1192. * We enable the update in the hash.
  1193. * As disabling notrace means enabling the tracing,
  1194. * and enabling notrace means disabling, the inc variable
  1195. * gets inversed.
  1196. */
  1197. if (filter_hash) {
  1198. hash = ops->filter_hash;
  1199. other_hash = ops->notrace_hash;
  1200. if (ftrace_hash_empty(hash))
  1201. all = 1;
  1202. } else {
  1203. inc = !inc;
  1204. hash = ops->notrace_hash;
  1205. other_hash = ops->filter_hash;
  1206. /*
  1207. * If the notrace hash has no items,
  1208. * then there's nothing to do.
  1209. */
  1210. if (ftrace_hash_empty(hash))
  1211. return;
  1212. }
  1213. do_for_each_ftrace_rec(pg, rec) {
  1214. int in_other_hash = 0;
  1215. int in_hash = 0;
  1216. int match = 0;
  1217. if (all) {
  1218. /*
  1219. * Only the filter_hash affects all records.
  1220. * Update if the record is not in the notrace hash.
  1221. */
  1222. if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
  1223. match = 1;
  1224. } else {
  1225. in_hash = !!ftrace_lookup_ip(hash, rec->ip);
  1226. in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
  1227. /*
  1228. *
  1229. */
  1230. if (filter_hash && in_hash && !in_other_hash)
  1231. match = 1;
  1232. else if (!filter_hash && in_hash &&
  1233. (in_other_hash || ftrace_hash_empty(other_hash)))
  1234. match = 1;
  1235. }
  1236. if (!match)
  1237. continue;
  1238. if (inc) {
  1239. rec->flags++;
  1240. if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
  1241. return;
  1242. } else {
  1243. if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
  1244. return;
  1245. rec->flags--;
  1246. }
  1247. count++;
  1248. /* Shortcut, if we handled all records, we are done. */
  1249. if (!all && count == hash->count)
  1250. return;
  1251. } while_for_each_ftrace_rec();
  1252. }
  1253. static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
  1254. int filter_hash)
  1255. {
  1256. __ftrace_hash_rec_update(ops, filter_hash, 0);
  1257. }
  1258. static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
  1259. int filter_hash)
  1260. {
  1261. __ftrace_hash_rec_update(ops, filter_hash, 1);
  1262. }
  1263. static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
  1264. {
  1265. if (ftrace_pages->index == ftrace_pages->size) {
  1266. /* We should have allocated enough */
  1267. if (WARN_ON(!ftrace_pages->next))
  1268. return NULL;
  1269. ftrace_pages = ftrace_pages->next;
  1270. }
  1271. return &ftrace_pages->records[ftrace_pages->index++];
  1272. }
  1273. static struct dyn_ftrace *
  1274. ftrace_record_ip(unsigned long ip)
  1275. {
  1276. struct dyn_ftrace *rec;
  1277. if (ftrace_disabled)
  1278. return NULL;
  1279. rec = ftrace_alloc_dyn_node(ip);
  1280. if (!rec)
  1281. return NULL;
  1282. rec->ip = ip;
  1283. return rec;
  1284. }
  1285. static void print_ip_ins(const char *fmt, unsigned char *p)
  1286. {
  1287. int i;
  1288. printk(KERN_CONT "%s", fmt);
  1289. for (i = 0; i < MCOUNT_INSN_SIZE; i++)
  1290. printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
  1291. }
  1292. /**
  1293. * ftrace_bug - report and shutdown function tracer
  1294. * @failed: The failed type (EFAULT, EINVAL, EPERM)
  1295. * @ip: The address that failed
  1296. *
  1297. * The arch code that enables or disables the function tracing
  1298. * can call ftrace_bug() when it has detected a problem in
  1299. * modifying the code. @failed should be one of either:
  1300. * EFAULT - if the problem happens on reading the @ip address
  1301. * EINVAL - if what is read at @ip is not what was expected
  1302. * EPERM - if the problem happens on writting to the @ip address
  1303. */
  1304. void ftrace_bug(int failed, unsigned long ip)
  1305. {
  1306. switch (failed) {
  1307. case -EFAULT:
  1308. FTRACE_WARN_ON_ONCE(1);
  1309. pr_info("ftrace faulted on modifying ");
  1310. print_ip_sym(ip);
  1311. break;
  1312. case -EINVAL:
  1313. FTRACE_WARN_ON_ONCE(1);
  1314. pr_info("ftrace failed to modify ");
  1315. print_ip_sym(ip);
  1316. print_ip_ins(" actual: ", (unsigned char *)ip);
  1317. printk(KERN_CONT "\n");
  1318. break;
  1319. case -EPERM:
  1320. FTRACE_WARN_ON_ONCE(1);
  1321. pr_info("ftrace faulted on writing ");
  1322. print_ip_sym(ip);
  1323. break;
  1324. default:
  1325. FTRACE_WARN_ON_ONCE(1);
  1326. pr_info("ftrace faulted on unknown error ");
  1327. print_ip_sym(ip);
  1328. }
  1329. }
  1330. /* Return 1 if the address range is reserved for ftrace */
  1331. int ftrace_text_reserved(void *start, void *end)
  1332. {
  1333. struct dyn_ftrace *rec;
  1334. struct ftrace_page *pg;
  1335. do_for_each_ftrace_rec(pg, rec) {
  1336. if (rec->ip <= (unsigned long)end &&
  1337. rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
  1338. return 1;
  1339. } while_for_each_ftrace_rec();
  1340. return 0;
  1341. }
  1342. static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
  1343. {
  1344. unsigned long flag = 0UL;
  1345. /*
  1346. * If we are updating calls:
  1347. *
  1348. * If the record has a ref count, then we need to enable it
  1349. * because someone is using it.
  1350. *
  1351. * Otherwise we make sure its disabled.
  1352. *
  1353. * If we are disabling calls, then disable all records that
  1354. * are enabled.
  1355. */
  1356. if (enable && (rec->flags & ~FTRACE_FL_MASK))
  1357. flag = FTRACE_FL_ENABLED;
  1358. /* If the state of this record hasn't changed, then do nothing */
  1359. if ((rec->flags & FTRACE_FL_ENABLED) == flag)
  1360. return FTRACE_UPDATE_IGNORE;
  1361. if (flag) {
  1362. if (update)
  1363. rec->flags |= FTRACE_FL_ENABLED;
  1364. return FTRACE_UPDATE_MAKE_CALL;
  1365. }
  1366. if (update)
  1367. rec->flags &= ~FTRACE_FL_ENABLED;
  1368. return FTRACE_UPDATE_MAKE_NOP;
  1369. }
  1370. /**
  1371. * ftrace_update_record, set a record that now is tracing or not
  1372. * @rec: the record to update
  1373. * @enable: set to 1 if the record is tracing, zero to force disable
  1374. *
  1375. * The records that represent all functions that can be traced need
  1376. * to be updated when tracing has been enabled.
  1377. */
  1378. int ftrace_update_record(struct dyn_ftrace *rec, int enable)
  1379. {
  1380. return ftrace_check_record(rec, enable, 1);
  1381. }
  1382. /**
  1383. * ftrace_test_record, check if the record has been enabled or not
  1384. * @rec: the record to test
  1385. * @enable: set to 1 to check if enabled, 0 if it is disabled
  1386. *
  1387. * The arch code may need to test if a record is already set to
  1388. * tracing to determine how to modify the function code that it
  1389. * represents.
  1390. */
  1391. int ftrace_test_record(struct dyn_ftrace *rec, int enable)
  1392. {
  1393. return ftrace_check_record(rec, enable, 0);
  1394. }
  1395. static int
  1396. __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
  1397. {
  1398. unsigned long ftrace_addr;
  1399. int ret;
  1400. ftrace_addr = (unsigned long)FTRACE_ADDR;
  1401. ret = ftrace_update_record(rec, enable);
  1402. switch (ret) {
  1403. case FTRACE_UPDATE_IGNORE:
  1404. return 0;
  1405. case FTRACE_UPDATE_MAKE_CALL:
  1406. return ftrace_make_call(rec, ftrace_addr);
  1407. case FTRACE_UPDATE_MAKE_NOP:
  1408. return ftrace_make_nop(NULL, rec, ftrace_addr);
  1409. }
  1410. return -1; /* unknow ftrace bug */
  1411. }
  1412. static void ftrace_replace_code(int update)
  1413. {
  1414. struct dyn_ftrace *rec;
  1415. struct ftrace_page *pg;
  1416. int failed;
  1417. if (unlikely(ftrace_disabled))
  1418. return;
  1419. do_for_each_ftrace_rec(pg, rec) {
  1420. failed = __ftrace_replace_code(rec, update);
  1421. if (failed) {
  1422. ftrace_bug(failed, rec->ip);
  1423. /* Stop processing */
  1424. return;
  1425. }
  1426. } while_for_each_ftrace_rec();
  1427. }
  1428. struct ftrace_rec_iter {
  1429. struct ftrace_page *pg;
  1430. int index;
  1431. };
  1432. /**
  1433. * ftrace_rec_iter_start, start up iterating over traced functions
  1434. *
  1435. * Returns an iterator handle that is used to iterate over all
  1436. * the records that represent address locations where functions
  1437. * are traced.
  1438. *
  1439. * May return NULL if no records are available.
  1440. */
  1441. struct ftrace_rec_iter *ftrace_rec_iter_start(void)
  1442. {
  1443. /*
  1444. * We only use a single iterator.
  1445. * Protected by the ftrace_lock mutex.
  1446. */
  1447. static struct ftrace_rec_iter ftrace_rec_iter;
  1448. struct ftrace_rec_iter *iter = &ftrace_rec_iter;
  1449. iter->pg = ftrace_pages_start;
  1450. iter->index = 0;
  1451. /* Could have empty pages */
  1452. while (iter->pg && !iter->pg->index)
  1453. iter->pg = iter->pg->next;
  1454. if (!iter->pg)
  1455. return NULL;
  1456. return iter;
  1457. }
  1458. /**
  1459. * ftrace_rec_iter_next, get the next record to process.
  1460. * @iter: The handle to the iterator.
  1461. *
  1462. * Returns the next iterator after the given iterator @iter.
  1463. */
  1464. struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
  1465. {
  1466. iter->index++;
  1467. if (iter->index >= iter->pg->index) {
  1468. iter->pg = iter->pg->next;
  1469. iter->index = 0;
  1470. /* Could have empty pages */
  1471. while (iter->pg && !iter->pg->index)
  1472. iter->pg = iter->pg->next;
  1473. }
  1474. if (!iter->pg)
  1475. return NULL;
  1476. return iter;
  1477. }
  1478. /**
  1479. * ftrace_rec_iter_record, get the record at the iterator location
  1480. * @iter: The current iterator location
  1481. *
  1482. * Returns the record that the current @iter is at.
  1483. */
  1484. struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
  1485. {
  1486. return &iter->pg->records[iter->index];
  1487. }
  1488. static int
  1489. ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
  1490. {
  1491. unsigned long ip;
  1492. int ret;
  1493. ip = rec->ip;
  1494. if (unlikely(ftrace_disabled))
  1495. return 0;
  1496. ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
  1497. if (ret) {
  1498. ftrace_bug(ret, ip);
  1499. return 0;
  1500. }
  1501. return 1;
  1502. }
  1503. /*
  1504. * archs can override this function if they must do something
  1505. * before the modifying code is performed.
  1506. */
  1507. int __weak ftrace_arch_code_modify_prepare(void)
  1508. {
  1509. return 0;
  1510. }
  1511. /*
  1512. * archs can override this function if they must do something
  1513. * after the modifying code is performed.
  1514. */
  1515. int __weak ftrace_arch_code_modify_post_process(void)
  1516. {
  1517. return 0;
  1518. }
  1519. static int __ftrace_modify_code(void *data)
  1520. {
  1521. int *command = data;
  1522. if (*command & FTRACE_UPDATE_CALLS)
  1523. ftrace_replace_code(1);
  1524. else if (*command & FTRACE_DISABLE_CALLS)
  1525. ftrace_replace_code(0);
  1526. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  1527. ftrace_update_ftrace_func(ftrace_trace_function);
  1528. if (*command & FTRACE_START_FUNC_RET)
  1529. ftrace_enable_ftrace_graph_caller();
  1530. else if (*command & FTRACE_STOP_FUNC_RET)
  1531. ftrace_disable_ftrace_graph_caller();
  1532. return 0;
  1533. }
  1534. /**
  1535. * ftrace_run_stop_machine, go back to the stop machine method
  1536. * @command: The command to tell ftrace what to do
  1537. *
  1538. * If an arch needs to fall back to the stop machine method, the
  1539. * it can call this function.
  1540. */
  1541. void ftrace_run_stop_machine(int command)
  1542. {
  1543. stop_machine(__ftrace_modify_code, &command, NULL);
  1544. }
  1545. /**
  1546. * arch_ftrace_update_code, modify the code to trace or not trace
  1547. * @command: The command that needs to be done
  1548. *
  1549. * Archs can override this function if it does not need to
  1550. * run stop_machine() to modify code.
  1551. */
  1552. void __weak arch_ftrace_update_code(int command)
  1553. {
  1554. ftrace_run_stop_machine(command);
  1555. }
  1556. static void ftrace_run_update_code(int command)
  1557. {
  1558. int ret;
  1559. ret = ftrace_arch_code_modify_prepare();
  1560. FTRACE_WARN_ON(ret);
  1561. if (ret)
  1562. return;
  1563. /*
  1564. * Do not call function tracer while we update the code.
  1565. * We are in stop machine.
  1566. */
  1567. function_trace_stop++;
  1568. /*
  1569. * By default we use stop_machine() to modify the code.
  1570. * But archs can do what ever they want as long as it
  1571. * is safe. The stop_machine() is the safest, but also
  1572. * produces the most overhead.
  1573. */
  1574. arch_ftrace_update_code(command);
  1575. #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  1576. /*
  1577. * For archs that call ftrace_test_stop_func(), we must
  1578. * wait till after we update all the function callers
  1579. * before we update the callback. This keeps different
  1580. * ops that record different functions from corrupting
  1581. * each other.
  1582. */
  1583. __ftrace_trace_function = __ftrace_trace_function_delay;
  1584. #endif
  1585. function_trace_stop--;
  1586. ret = ftrace_arch_code_modify_post_process();
  1587. FTRACE_WARN_ON(ret);
  1588. }
  1589. static ftrace_func_t saved_ftrace_func;
  1590. static int ftrace_start_up;
  1591. static int global_start_up;
  1592. static void ftrace_startup_enable(int command)
  1593. {
  1594. if (saved_ftrace_func != ftrace_trace_function) {
  1595. saved_ftrace_func = ftrace_trace_function;
  1596. command |= FTRACE_UPDATE_TRACE_FUNC;
  1597. }
  1598. if (!command || !ftrace_enabled)
  1599. return;
  1600. ftrace_run_update_code(command);
  1601. }
  1602. static int ftrace_startup(struct ftrace_ops *ops, int command)
  1603. {
  1604. bool hash_enable = true;
  1605. if (unlikely(ftrace_disabled))
  1606. return -ENODEV;
  1607. ftrace_start_up++;
  1608. command |= FTRACE_UPDATE_CALLS;
  1609. /* ops marked global share the filter hashes */
  1610. if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
  1611. ops = &global_ops;
  1612. /* Don't update hash if global is already set */
  1613. if (global_start_up)
  1614. hash_enable = false;
  1615. global_start_up++;
  1616. }
  1617. ops->flags |= FTRACE_OPS_FL_ENABLED;
  1618. if (hash_enable)
  1619. ftrace_hash_rec_enable(ops, 1);
  1620. ftrace_startup_enable(command);
  1621. return 0;
  1622. }
  1623. static void ftrace_shutdown(struct ftrace_ops *ops, int command)
  1624. {
  1625. bool hash_disable = true;
  1626. if (unlikely(ftrace_disabled))
  1627. return;
  1628. ftrace_start_up--;
  1629. /*
  1630. * Just warn in case of unbalance, no need to kill ftrace, it's not
  1631. * critical but the ftrace_call callers may be never nopped again after
  1632. * further ftrace uses.
  1633. */
  1634. WARN_ON_ONCE(ftrace_start_up < 0);
  1635. if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
  1636. ops = &global_ops;
  1637. global_start_up--;
  1638. WARN_ON_ONCE(global_start_up < 0);
  1639. /* Don't update hash if global still has users */
  1640. if (global_start_up) {
  1641. WARN_ON_ONCE(!ftrace_start_up);
  1642. hash_disable = false;
  1643. }
  1644. }
  1645. if (hash_disable)
  1646. ftrace_hash_rec_disable(ops, 1);
  1647. if (ops != &global_ops || !global_start_up)
  1648. ops->flags &= ~FTRACE_OPS_FL_ENABLED;
  1649. command |= FTRACE_UPDATE_CALLS;
  1650. if (saved_ftrace_func != ftrace_trace_function) {
  1651. saved_ftrace_func = ftrace_trace_function;
  1652. command |= FTRACE_UPDATE_TRACE_FUNC;
  1653. }
  1654. if (!command || !ftrace_enabled)
  1655. return;
  1656. ftrace_run_update_code(command);
  1657. }
  1658. static void ftrace_startup_sysctl(void)
  1659. {
  1660. if (unlikely(ftrace_disabled))
  1661. return;
  1662. /* Force update next time */
  1663. saved_ftrace_func = NULL;
  1664. /* ftrace_start_up is true if we want ftrace running */
  1665. if (ftrace_start_up)
  1666. ftrace_run_update_code(FTRACE_UPDATE_CALLS);
  1667. }
  1668. static void ftrace_shutdown_sysctl(void)
  1669. {
  1670. if (unlikely(ftrace_disabled))
  1671. return;
  1672. /* ftrace_start_up is true if ftrace is running */
  1673. if (ftrace_start_up)
  1674. ftrace_run_update_code(FTRACE_DISABLE_CALLS);
  1675. }
  1676. static cycle_t ftrace_update_time;
  1677. static unsigned long ftrace_update_cnt;
  1678. unsigned long ftrace_update_tot_cnt;
  1679. static int ops_traces_mod(struct ftrace_ops *ops)
  1680. {
  1681. struct ftrace_hash *hash;
  1682. hash = ops->filter_hash;
  1683. return ftrace_hash_empty(hash);
  1684. }
  1685. static int ftrace_update_code(struct module *mod)
  1686. {
  1687. struct ftrace_page *pg;
  1688. struct dyn_ftrace *p;
  1689. cycle_t start, stop;
  1690. unsigned long ref = 0;
  1691. int i;
  1692. /*
  1693. * When adding a module, we need to check if tracers are
  1694. * currently enabled and if they are set to trace all functions.
  1695. * If they are, we need to enable the module functions as well
  1696. * as update the reference counts for those function records.
  1697. */
  1698. if (mod) {
  1699. struct ftrace_ops *ops;
  1700. for (ops = ftrace_ops_list;
  1701. ops != &ftrace_list_end; ops = ops->next) {
  1702. if (ops->flags & FTRACE_OPS_FL_ENABLED &&
  1703. ops_traces_mod(ops))
  1704. ref++;
  1705. }
  1706. }
  1707. start = ftrace_now(raw_smp_processor_id());
  1708. ftrace_update_cnt = 0;
  1709. for (pg = ftrace_new_pgs; pg; pg = pg->next) {
  1710. for (i = 0; i < pg->index; i++) {
  1711. /* If something went wrong, bail without enabling anything */
  1712. if (unlikely(ftrace_disabled))
  1713. return -1;
  1714. p = &pg->records[i];
  1715. p->flags = ref;
  1716. /*
  1717. * Do the initial record conversion from mcount jump
  1718. * to the NOP instructions.
  1719. */
  1720. if (!ftrace_code_disable(mod, p))
  1721. break;
  1722. ftrace_update_cnt++;
  1723. /*
  1724. * If the tracing is enabled, go ahead and enable the record.
  1725. *
  1726. * The reason not to enable the record immediatelly is the
  1727. * inherent check of ftrace_make_nop/ftrace_make_call for
  1728. * correct previous instructions. Making first the NOP
  1729. * conversion puts the module to the correct state, thus
  1730. * passing the ftrace_make_call check.
  1731. */
  1732. if (ftrace_start_up && ref) {
  1733. int failed = __ftrace_replace_code(p, 1);
  1734. if (failed)
  1735. ftrace_bug(failed, p->ip);
  1736. }
  1737. }
  1738. }
  1739. ftrace_new_pgs = NULL;
  1740. stop = ftrace_now(raw_smp_processor_id());
  1741. ftrace_update_time = stop - start;
  1742. ftrace_update_tot_cnt += ftrace_update_cnt;
  1743. return 0;
  1744. }
  1745. static int ftrace_allocate_records(struct ftrace_page *pg, int count)
  1746. {
  1747. int order;
  1748. int cnt;
  1749. if (WARN_ON(!count))
  1750. return -EINVAL;
  1751. order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
  1752. /*
  1753. * We want to fill as much as possible. No more than a page
  1754. * may be empty.
  1755. */
  1756. while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
  1757. order--;
  1758. again:
  1759. pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
  1760. if (!pg->records) {
  1761. /* if we can't allocate this size, try something smaller */
  1762. if (!order)
  1763. return -ENOMEM;
  1764. order >>= 1;
  1765. goto again;
  1766. }
  1767. cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
  1768. pg->size = cnt;
  1769. if (cnt > count)
  1770. cnt = count;
  1771. return cnt;
  1772. }
  1773. static struct ftrace_page *
  1774. ftrace_allocate_pages(unsigned long num_to_init)
  1775. {
  1776. struct ftrace_page *start_pg;
  1777. struct ftrace_page *pg;
  1778. int order;
  1779. int cnt;
  1780. if (!num_to_init)
  1781. return 0;
  1782. start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
  1783. if (!pg)
  1784. return NULL;
  1785. /*
  1786. * Try to allocate as much as possible in one continues
  1787. * location that fills in all of the space. We want to
  1788. * waste as little space as possible.
  1789. */
  1790. for (;;) {
  1791. cnt = ftrace_allocate_records(pg, num_to_init);
  1792. if (cnt < 0)
  1793. goto free_pages;
  1794. num_to_init -= cnt;
  1795. if (!num_to_init)
  1796. break;
  1797. pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
  1798. if (!pg->next)
  1799. goto free_pages;
  1800. pg = pg->next;
  1801. }
  1802. return start_pg;
  1803. free_pages:
  1804. while (start_pg) {
  1805. order = get_count_order(pg->size / ENTRIES_PER_PAGE);
  1806. free_pages((unsigned long)pg->records, order);
  1807. start_pg = pg->next;
  1808. kfree(pg);
  1809. pg = start_pg;
  1810. }
  1811. pr_info("ftrace: FAILED to allocate memory for functions\n");
  1812. return NULL;
  1813. }
  1814. static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
  1815. {
  1816. int cnt;
  1817. if (!num_to_init) {
  1818. pr_info("ftrace: No functions to be traced?\n");
  1819. return -1;
  1820. }
  1821. cnt = num_to_init / ENTRIES_PER_PAGE;
  1822. pr_info("ftrace: allocating %ld entries in %d pages\n",
  1823. num_to_init, cnt + 1);
  1824. return 0;
  1825. }
  1826. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  1827. struct ftrace_iterator {
  1828. loff_t pos;
  1829. loff_t func_pos;
  1830. struct ftrace_page *pg;
  1831. struct dyn_ftrace *func;
  1832. struct ftrace_func_probe *probe;
  1833. struct trace_parser parser;
  1834. struct ftrace_hash *hash;
  1835. struct ftrace_ops *ops;
  1836. int hidx;
  1837. int idx;
  1838. unsigned flags;
  1839. };
  1840. static void *
  1841. t_hash_next(struct seq_file *m, loff_t *pos)
  1842. {
  1843. struct ftrace_iterator *iter = m->private;
  1844. struct hlist_node *hnd = NULL;
  1845. struct hlist_head *hhd;
  1846. (*pos)++;
  1847. iter->pos = *pos;
  1848. if (iter->probe)
  1849. hnd = &iter->probe->node;
  1850. retry:
  1851. if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
  1852. return NULL;
  1853. hhd = &ftrace_func_hash[iter->hidx];
  1854. if (hlist_empty(hhd)) {
  1855. iter->hidx++;
  1856. hnd = NULL;
  1857. goto retry;
  1858. }
  1859. if (!hnd)
  1860. hnd = hhd->first;
  1861. else {
  1862. hnd = hnd->next;
  1863. if (!hnd) {
  1864. iter->hidx++;
  1865. goto retry;
  1866. }
  1867. }
  1868. if (WARN_ON_ONCE(!hnd))
  1869. return NULL;
  1870. iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
  1871. return iter;
  1872. }
  1873. static void *t_hash_start(struct seq_file *m, loff_t *pos)
  1874. {
  1875. struct ftrace_iterator *iter = m->private;
  1876. void *p = NULL;
  1877. loff_t l;
  1878. if (!(iter->flags & FTRACE_ITER_DO_HASH))
  1879. return NULL;
  1880. if (iter->func_pos > *pos)
  1881. return NULL;
  1882. iter->hidx = 0;
  1883. for (l = 0; l <= (*pos - iter->func_pos); ) {
  1884. p = t_hash_next(m, &l);
  1885. if (!p)
  1886. break;
  1887. }
  1888. if (!p)
  1889. return NULL;
  1890. /* Only set this if we have an item */
  1891. iter->flags |= FTRACE_ITER_HASH;
  1892. return iter;
  1893. }
  1894. static int
  1895. t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
  1896. {
  1897. struct ftrace_func_probe *rec;
  1898. rec = iter->probe;
  1899. if (WARN_ON_ONCE(!rec))
  1900. return -EIO;
  1901. if (rec->ops->print)
  1902. return rec->ops->print(m, rec->ip, rec->ops, rec->data);
  1903. seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
  1904. if (rec->data)
  1905. seq_printf(m, ":%p", rec->data);
  1906. seq_putc(m, '\n');
  1907. return 0;
  1908. }
  1909. static void *
  1910. t_next(struct seq_file *m, void *v, loff_t *pos)
  1911. {
  1912. struct ftrace_iterator *iter = m->private;
  1913. struct ftrace_ops *ops = iter->ops;
  1914. struct dyn_ftrace *rec = NULL;
  1915. if (unlikely(ftrace_disabled))
  1916. return NULL;
  1917. if (iter->flags & FTRACE_ITER_HASH)
  1918. return t_hash_next(m, pos);
  1919. (*pos)++;
  1920. iter->pos = iter->func_pos = *pos;
  1921. if (iter->flags & FTRACE_ITER_PRINTALL)
  1922. return t_hash_start(m, pos);
  1923. retry:
  1924. if (iter->idx >= iter->pg->index) {
  1925. if (iter->pg->next) {
  1926. iter->pg = iter->pg->next;
  1927. iter->idx = 0;
  1928. goto retry;
  1929. }
  1930. } else {
  1931. rec = &iter->pg->records[iter->idx++];
  1932. if (((iter->flags & FTRACE_ITER_FILTER) &&
  1933. !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
  1934. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  1935. !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
  1936. ((iter->flags & FTRACE_ITER_ENABLED) &&
  1937. !(rec->flags & ~FTRACE_FL_MASK))) {
  1938. rec = NULL;
  1939. goto retry;
  1940. }
  1941. }
  1942. if (!rec)
  1943. return t_hash_start(m, pos);
  1944. iter->func = rec;
  1945. return iter;
  1946. }
  1947. static void reset_iter_read(struct ftrace_iterator *iter)
  1948. {
  1949. iter->pos = 0;
  1950. iter->func_pos = 0;
  1951. iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
  1952. }
  1953. static void *t_start(struct seq_file *m, loff_t *pos)
  1954. {
  1955. struct ftrace_iterator *iter = m->private;
  1956. struct ftrace_ops *ops = iter->ops;
  1957. void *p = NULL;
  1958. loff_t l;
  1959. mutex_lock(&ftrace_lock);
  1960. if (unlikely(ftrace_disabled))
  1961. return NULL;
  1962. /*
  1963. * If an lseek was done, then reset and start from beginning.
  1964. */
  1965. if (*pos < iter->pos)
  1966. reset_iter_read(iter);
  1967. /*
  1968. * For set_ftrace_filter reading, if we have the filter
  1969. * off, we can short cut and just print out that all
  1970. * functions are enabled.
  1971. */
  1972. if (iter->flags & FTRACE_ITER_FILTER &&
  1973. ftrace_hash_empty(ops->filter_hash)) {
  1974. if (*pos > 0)
  1975. return t_hash_start(m, pos);
  1976. iter->flags |= FTRACE_ITER_PRINTALL;
  1977. /* reset in case of seek/pread */
  1978. iter->flags &= ~FTRACE_ITER_HASH;
  1979. return iter;
  1980. }
  1981. if (iter->flags & FTRACE_ITER_HASH)
  1982. return t_hash_start(m, pos);
  1983. /*
  1984. * Unfortunately, we need to restart at ftrace_pages_start
  1985. * every time we let go of the ftrace_mutex. This is because
  1986. * those pointers can change without the lock.
  1987. */
  1988. iter->pg = ftrace_pages_start;
  1989. iter->idx = 0;
  1990. for (l = 0; l <= *pos; ) {
  1991. p = t_next(m, p, &l);
  1992. if (!p)
  1993. break;
  1994. }
  1995. if (!p)
  1996. return t_hash_start(m, pos);
  1997. return iter;
  1998. }
  1999. static void t_stop(struct seq_file *m, void *p)
  2000. {
  2001. mutex_unlock(&ftrace_lock);
  2002. }
  2003. static int t_show(struct seq_file *m, void *v)
  2004. {
  2005. struct ftrace_iterator *iter = m->private;
  2006. struct dyn_ftrace *rec;
  2007. if (iter->flags & FTRACE_ITER_HASH)
  2008. return t_hash_show(m, iter);
  2009. if (iter->flags & FTRACE_ITER_PRINTALL) {
  2010. seq_printf(m, "#### all functions enabled ####\n");
  2011. return 0;
  2012. }
  2013. rec = iter->func;
  2014. if (!rec)
  2015. return 0;
  2016. seq_printf(m, "%ps", (void *)rec->ip);
  2017. if (iter->flags & FTRACE_ITER_ENABLED)
  2018. seq_printf(m, " (%ld)",
  2019. rec->flags & ~FTRACE_FL_MASK);
  2020. seq_printf(m, "\n");
  2021. return 0;
  2022. }
  2023. static const struct seq_operations show_ftrace_seq_ops = {
  2024. .start = t_start,
  2025. .next = t_next,
  2026. .stop = t_stop,
  2027. .show = t_show,
  2028. };
  2029. static int
  2030. ftrace_avail_open(struct inode *inode, struct file *file)
  2031. {
  2032. struct ftrace_iterator *iter;
  2033. int ret;
  2034. if (unlikely(ftrace_disabled))
  2035. return -ENODEV;
  2036. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  2037. if (!iter)
  2038. return -ENOMEM;
  2039. iter->pg = ftrace_pages_start;
  2040. iter->ops = &global_ops;
  2041. ret = seq_open(file, &show_ftrace_seq_ops);
  2042. if (!ret) {
  2043. struct seq_file *m = file->private_data;
  2044. m->private = iter;
  2045. } else {
  2046. kfree(iter);
  2047. }
  2048. return ret;
  2049. }
  2050. static int
  2051. ftrace_enabled_open(struct inode *inode, struct file *file)
  2052. {
  2053. struct ftrace_iterator *iter;
  2054. int ret;
  2055. if (unlikely(ftrace_disabled))
  2056. return -ENODEV;
  2057. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  2058. if (!iter)
  2059. return -ENOMEM;
  2060. iter->pg = ftrace_pages_start;
  2061. iter->flags = FTRACE_ITER_ENABLED;
  2062. iter->ops = &global_ops;
  2063. ret = seq_open(file, &show_ftrace_seq_ops);
  2064. if (!ret) {
  2065. struct seq_file *m = file->private_data;
  2066. m->private = iter;
  2067. } else {
  2068. kfree(iter);
  2069. }
  2070. return ret;
  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
  2322. function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
  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 void ftrace_swap_recs(void *a, void *b, int size)
  3026. {
  3027. struct dyn_ftrace *reca = a;
  3028. struct dyn_ftrace *recb = b;
  3029. struct dyn_ftrace t;
  3030. t = *reca;
  3031. *reca = *recb;
  3032. *recb = t;
  3033. }
  3034. static int ftrace_process_locs(struct module *mod,
  3035. unsigned long *start,
  3036. unsigned long *end)
  3037. {
  3038. struct ftrace_page *pg;
  3039. unsigned long count;
  3040. unsigned long *p;
  3041. unsigned long addr;
  3042. unsigned long flags = 0; /* Shut up gcc */
  3043. int ret = -ENOMEM;
  3044. count = end - start;
  3045. if (!count)
  3046. return 0;
  3047. pg = ftrace_allocate_pages(count);
  3048. if (!pg)
  3049. return -ENOMEM;
  3050. mutex_lock(&ftrace_lock);
  3051. /*
  3052. * Core and each module needs their own pages, as
  3053. * modules will free them when they are removed.
  3054. * Force a new page to be allocated for modules.
  3055. */
  3056. if (!mod) {
  3057. WARN_ON(ftrace_pages || ftrace_pages_start);
  3058. /* First initialization */
  3059. ftrace_pages = ftrace_pages_start = pg;
  3060. } else {
  3061. if (!ftrace_pages)
  3062. goto out;
  3063. if (WARN_ON(ftrace_pages->next)) {
  3064. /* Hmm, we have free pages? */
  3065. while (ftrace_pages->next)
  3066. ftrace_pages = ftrace_pages->next;
  3067. }
  3068. ftrace_pages->next = pg;
  3069. ftrace_pages = pg;
  3070. }
  3071. p = start;
  3072. while (p < end) {
  3073. addr = ftrace_call_adjust(*p++);
  3074. /*
  3075. * Some architecture linkers will pad between
  3076. * the different mcount_loc sections of different
  3077. * object files to satisfy alignments.
  3078. * Skip any NULL pointers.
  3079. */
  3080. if (!addr)
  3081. continue;
  3082. if (!ftrace_record_ip(addr))
  3083. break;
  3084. }
  3085. /* These new locations need to be initialized */
  3086. ftrace_new_pgs = pg;
  3087. /* Make each individual set of pages sorted by ips */
  3088. for (; pg; pg = pg->next)
  3089. sort(pg->records, pg->index, sizeof(struct dyn_ftrace),
  3090. ftrace_cmp_recs, ftrace_swap_recs);
  3091. /*
  3092. * We only need to disable interrupts on start up
  3093. * because we are modifying code that an interrupt
  3094. * may execute, and the modification is not atomic.
  3095. * But for modules, nothing runs the code we modify
  3096. * until we are finished with it, and there's no
  3097. * reason to cause large interrupt latencies while we do it.
  3098. */
  3099. if (!mod)
  3100. local_irq_save(flags);
  3101. ftrace_update_code(mod);
  3102. if (!mod)
  3103. local_irq_restore(flags);
  3104. ret = 0;
  3105. out:
  3106. mutex_unlock(&ftrace_lock);
  3107. return ret;
  3108. }
  3109. #ifdef CONFIG_MODULES
  3110. #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
  3111. void ftrace_release_mod(struct module *mod)
  3112. {
  3113. struct dyn_ftrace *rec;
  3114. struct ftrace_page **last_pg;
  3115. struct ftrace_page *pg;
  3116. int order;
  3117. mutex_lock(&ftrace_lock);
  3118. if (ftrace_disabled)
  3119. goto out_unlock;
  3120. /*
  3121. * Each module has its own ftrace_pages, remove
  3122. * them from the list.
  3123. */
  3124. last_pg = &ftrace_pages_start;
  3125. for (pg = ftrace_pages_start; pg; pg = *last_pg) {
  3126. rec = &pg->records[0];
  3127. if (within_module_core(rec->ip, mod)) {
  3128. /*
  3129. * As core pages are first, the first
  3130. * page should never be a module page.
  3131. */
  3132. if (WARN_ON(pg == ftrace_pages_start))
  3133. goto out_unlock;
  3134. /* Check if we are deleting the last page */
  3135. if (pg == ftrace_pages)
  3136. ftrace_pages = next_to_ftrace_page(last_pg);
  3137. *last_pg = pg->next;
  3138. order = get_count_order(pg->size / ENTRIES_PER_PAGE);
  3139. free_pages((unsigned long)pg->records, order);
  3140. kfree(pg);
  3141. } else
  3142. last_pg = &pg->next;
  3143. }
  3144. out_unlock:
  3145. mutex_unlock(&ftrace_lock);
  3146. }
  3147. static void ftrace_init_module(struct module *mod,
  3148. unsigned long *start, unsigned long *end)
  3149. {
  3150. if (ftrace_disabled || start == end)
  3151. return;
  3152. ftrace_process_locs(mod, start, end);
  3153. }
  3154. static int ftrace_module_notify(struct notifier_block *self,
  3155. unsigned long val, void *data)
  3156. {
  3157. struct module *mod = data;
  3158. switch (val) {
  3159. case MODULE_STATE_COMING:
  3160. ftrace_init_module(mod, mod->ftrace_callsites,
  3161. mod->ftrace_callsites +
  3162. mod->num_ftrace_callsites);
  3163. break;
  3164. case MODULE_STATE_GOING:
  3165. ftrace_release_mod(mod);
  3166. break;
  3167. }
  3168. return 0;
  3169. }
  3170. #else
  3171. static int ftrace_module_notify(struct notifier_block *self,
  3172. unsigned long val, void *data)
  3173. {
  3174. return 0;
  3175. }
  3176. #endif /* CONFIG_MODULES */
  3177. struct notifier_block ftrace_module_nb = {
  3178. .notifier_call = ftrace_module_notify,
  3179. .priority = 0,
  3180. };
  3181. extern unsigned long __start_mcount_loc[];
  3182. extern unsigned long __stop_mcount_loc[];
  3183. void __init ftrace_init(void)
  3184. {
  3185. unsigned long count, addr, flags;
  3186. int ret;
  3187. /* Keep the ftrace pointer to the stub */
  3188. addr = (unsigned long)ftrace_stub;
  3189. local_irq_save(flags);
  3190. ftrace_dyn_arch_init(&addr);
  3191. local_irq_restore(flags);
  3192. /* ftrace_dyn_arch_init places the return code in addr */
  3193. if (addr)
  3194. goto failed;
  3195. count = __stop_mcount_loc - __start_mcount_loc;
  3196. ret = ftrace_dyn_table_alloc(count);
  3197. if (ret)
  3198. goto failed;
  3199. last_ftrace_enabled = ftrace_enabled = 1;
  3200. ret = ftrace_process_locs(NULL,
  3201. __start_mcount_loc,
  3202. __stop_mcount_loc);
  3203. ret = register_module_notifier(&ftrace_module_nb);
  3204. if (ret)
  3205. pr_warning("Failed to register trace ftrace module notifier\n");
  3206. set_ftrace_early_filters();
  3207. return;
  3208. failed:
  3209. ftrace_disabled = 1;
  3210. }
  3211. #else
  3212. static struct ftrace_ops global_ops = {
  3213. .func = ftrace_stub,
  3214. };
  3215. static int __init ftrace_nodyn_init(void)
  3216. {
  3217. ftrace_enabled = 1;
  3218. return 0;
  3219. }
  3220. device_initcall(ftrace_nodyn_init);
  3221. static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
  3222. static inline void ftrace_startup_enable(int command) { }
  3223. /* Keep as macros so we do not need to define the commands */
  3224. # define ftrace_startup(ops, command) \
  3225. ({ \
  3226. (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
  3227. 0; \
  3228. })
  3229. # define ftrace_shutdown(ops, command) do { } while (0)
  3230. # define ftrace_startup_sysctl() do { } while (0)
  3231. # define ftrace_shutdown_sysctl() do { } while (0)
  3232. static inline int
  3233. ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
  3234. {
  3235. return 1;
  3236. }
  3237. #endif /* CONFIG_DYNAMIC_FTRACE */
  3238. static void
  3239. ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip)
  3240. {
  3241. struct ftrace_ops *op;
  3242. if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
  3243. return;
  3244. /*
  3245. * Some of the ops may be dynamically allocated,
  3246. * they must be freed after a synchronize_sched().
  3247. */
  3248. preempt_disable_notrace();
  3249. trace_recursion_set(TRACE_CONTROL_BIT);
  3250. op = rcu_dereference_raw(ftrace_control_list);
  3251. while (op != &ftrace_list_end) {
  3252. if (!ftrace_function_local_disabled(op) &&
  3253. ftrace_ops_test(op, ip))
  3254. op->func(ip, parent_ip);
  3255. op = rcu_dereference_raw(op->next);
  3256. };
  3257. trace_recursion_clear(TRACE_CONTROL_BIT);
  3258. preempt_enable_notrace();
  3259. }
  3260. static struct ftrace_ops control_ops = {
  3261. .func = ftrace_ops_control_func,
  3262. };
  3263. static void
  3264. ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
  3265. {
  3266. struct ftrace_ops *op;
  3267. if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
  3268. return;
  3269. trace_recursion_set(TRACE_INTERNAL_BIT);
  3270. /*
  3271. * Some of the ops may be dynamically allocated,
  3272. * they must be freed after a synchronize_sched().
  3273. */
  3274. preempt_disable_notrace();
  3275. op = rcu_dereference_raw(ftrace_ops_list);
  3276. while (op != &ftrace_list_end) {
  3277. if (ftrace_ops_test(op, ip))
  3278. op->func(ip, parent_ip);
  3279. op = rcu_dereference_raw(op->next);
  3280. };
  3281. preempt_enable_notrace();
  3282. trace_recursion_clear(TRACE_INTERNAL_BIT);
  3283. }
  3284. static void clear_ftrace_swapper(void)
  3285. {
  3286. struct task_struct *p;
  3287. int cpu;
  3288. get_online_cpus();
  3289. for_each_online_cpu(cpu) {
  3290. p = idle_task(cpu);
  3291. clear_tsk_trace_trace(p);
  3292. }
  3293. put_online_cpus();
  3294. }
  3295. static void set_ftrace_swapper(void)
  3296. {
  3297. struct task_struct *p;
  3298. int cpu;
  3299. get_online_cpus();
  3300. for_each_online_cpu(cpu) {
  3301. p = idle_task(cpu);
  3302. set_tsk_trace_trace(p);
  3303. }
  3304. put_online_cpus();
  3305. }
  3306. static void clear_ftrace_pid(struct pid *pid)
  3307. {
  3308. struct task_struct *p;
  3309. rcu_read_lock();
  3310. do_each_pid_task(pid, PIDTYPE_PID, p) {
  3311. clear_tsk_trace_trace(p);
  3312. } while_each_pid_task(pid, PIDTYPE_PID, p);
  3313. rcu_read_unlock();
  3314. put_pid(pid);
  3315. }
  3316. static void set_ftrace_pid(struct pid *pid)
  3317. {
  3318. struct task_struct *p;
  3319. rcu_read_lock();
  3320. do_each_pid_task(pid, PIDTYPE_PID, p) {
  3321. set_tsk_trace_trace(p);
  3322. } while_each_pid_task(pid, PIDTYPE_PID, p);
  3323. rcu_read_unlock();
  3324. }
  3325. static void clear_ftrace_pid_task(struct pid *pid)
  3326. {
  3327. if (pid == ftrace_swapper_pid)
  3328. clear_ftrace_swapper();
  3329. else
  3330. clear_ftrace_pid(pid);
  3331. }
  3332. static void set_ftrace_pid_task(struct pid *pid)
  3333. {
  3334. if (pid == ftrace_swapper_pid)
  3335. set_ftrace_swapper();
  3336. else
  3337. set_ftrace_pid(pid);
  3338. }
  3339. static int ftrace_pid_add(int p)
  3340. {
  3341. struct pid *pid;
  3342. struct ftrace_pid *fpid;
  3343. int ret = -EINVAL;
  3344. mutex_lock(&ftrace_lock);
  3345. if (!p)
  3346. pid = ftrace_swapper_pid;
  3347. else
  3348. pid = find_get_pid(p);
  3349. if (!pid)
  3350. goto out;
  3351. ret = 0;
  3352. list_for_each_entry(fpid, &ftrace_pids, list)
  3353. if (fpid->pid == pid)
  3354. goto out_put;
  3355. ret = -ENOMEM;
  3356. fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
  3357. if (!fpid)
  3358. goto out_put;
  3359. list_add(&fpid->list, &ftrace_pids);
  3360. fpid->pid = pid;
  3361. set_ftrace_pid_task(pid);
  3362. ftrace_update_pid_func();
  3363. ftrace_startup_enable(0);
  3364. mutex_unlock(&ftrace_lock);
  3365. return 0;
  3366. out_put:
  3367. if (pid != ftrace_swapper_pid)
  3368. put_pid(pid);
  3369. out:
  3370. mutex_unlock(&ftrace_lock);
  3371. return ret;
  3372. }
  3373. static void ftrace_pid_reset(void)
  3374. {
  3375. struct ftrace_pid *fpid, *safe;
  3376. mutex_lock(&ftrace_lock);
  3377. list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
  3378. struct pid *pid = fpid->pid;
  3379. clear_ftrace_pid_task(pid);
  3380. list_del(&fpid->list);
  3381. kfree(fpid);
  3382. }
  3383. ftrace_update_pid_func();
  3384. ftrace_startup_enable(0);
  3385. mutex_unlock(&ftrace_lock);
  3386. }
  3387. static void *fpid_start(struct seq_file *m, loff_t *pos)
  3388. {
  3389. mutex_lock(&ftrace_lock);
  3390. if (list_empty(&ftrace_pids) && (!*pos))
  3391. return (void *) 1;
  3392. return seq_list_start(&ftrace_pids, *pos);
  3393. }
  3394. static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
  3395. {
  3396. if (v == (void *)1)
  3397. return NULL;
  3398. return seq_list_next(v, &ftrace_pids, pos);
  3399. }
  3400. static void fpid_stop(struct seq_file *m, void *p)
  3401. {
  3402. mutex_unlock(&ftrace_lock);
  3403. }
  3404. static int fpid_show(struct seq_file *m, void *v)
  3405. {
  3406. const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
  3407. if (v == (void *)1) {
  3408. seq_printf(m, "no pid\n");
  3409. return 0;
  3410. }
  3411. if (fpid->pid == ftrace_swapper_pid)
  3412. seq_printf(m, "swapper tasks\n");
  3413. else
  3414. seq_printf(m, "%u\n", pid_vnr(fpid->pid));
  3415. return 0;
  3416. }
  3417. static const struct seq_operations ftrace_pid_sops = {
  3418. .start = fpid_start,
  3419. .next = fpid_next,
  3420. .stop = fpid_stop,
  3421. .show = fpid_show,
  3422. };
  3423. static int
  3424. ftrace_pid_open(struct inode *inode, struct file *file)
  3425. {
  3426. int ret = 0;
  3427. if ((file->f_mode & FMODE_WRITE) &&
  3428. (file->f_flags & O_TRUNC))
  3429. ftrace_pid_reset();
  3430. if (file->f_mode & FMODE_READ)
  3431. ret = seq_open(file, &ftrace_pid_sops);
  3432. return ret;
  3433. }
  3434. static ssize_t
  3435. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  3436. size_t cnt, loff_t *ppos)
  3437. {
  3438. char buf[64], *tmp;
  3439. long val;
  3440. int ret;
  3441. if (cnt >= sizeof(buf))
  3442. return -EINVAL;
  3443. if (copy_from_user(&buf, ubuf, cnt))
  3444. return -EFAULT;
  3445. buf[cnt] = 0;
  3446. /*
  3447. * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
  3448. * to clean the filter quietly.
  3449. */
  3450. tmp = strstrip(buf);
  3451. if (strlen(tmp) == 0)
  3452. return 1;
  3453. ret = strict_strtol(tmp, 10, &val);
  3454. if (ret < 0)
  3455. return ret;
  3456. ret = ftrace_pid_add(val);
  3457. return ret ? ret : cnt;
  3458. }
  3459. static int
  3460. ftrace_pid_release(struct inode *inode, struct file *file)
  3461. {
  3462. if (file->f_mode & FMODE_READ)
  3463. seq_release(inode, file);
  3464. return 0;
  3465. }
  3466. static const struct file_operations ftrace_pid_fops = {
  3467. .open = ftrace_pid_open,
  3468. .write = ftrace_pid_write,
  3469. .read = seq_read,
  3470. .llseek = seq_lseek,
  3471. .release = ftrace_pid_release,
  3472. };
  3473. static __init int ftrace_init_debugfs(void)
  3474. {
  3475. struct dentry *d_tracer;
  3476. d_tracer = tracing_init_dentry();
  3477. if (!d_tracer)
  3478. return 0;
  3479. ftrace_init_dyn_debugfs(d_tracer);
  3480. trace_create_file("set_ftrace_pid", 0644, d_tracer,
  3481. NULL, &ftrace_pid_fops);
  3482. ftrace_profile_debugfs(d_tracer);
  3483. return 0;
  3484. }
  3485. fs_initcall(ftrace_init_debugfs);
  3486. /**
  3487. * ftrace_kill - kill ftrace
  3488. *
  3489. * This function should be used by panic code. It stops ftrace
  3490. * but in a not so nice way. If you need to simply kill ftrace
  3491. * from a non-atomic section, use ftrace_kill.
  3492. */
  3493. void ftrace_kill(void)
  3494. {
  3495. ftrace_disabled = 1;
  3496. ftrace_enabled = 0;
  3497. clear_ftrace_function();
  3498. }
  3499. /**
  3500. * Test if ftrace is dead or not.
  3501. */
  3502. int ftrace_is_dead(void)
  3503. {
  3504. return ftrace_disabled;
  3505. }
  3506. /**
  3507. * register_ftrace_function - register a function for profiling
  3508. * @ops - ops structure that holds the function for profiling.
  3509. *
  3510. * Register a function to be called by all functions in the
  3511. * kernel.
  3512. *
  3513. * Note: @ops->func and all the functions it calls must be labeled
  3514. * with "notrace", otherwise it will go into a
  3515. * recursive loop.
  3516. */
  3517. int register_ftrace_function(struct ftrace_ops *ops)
  3518. {
  3519. int ret = -1;
  3520. mutex_lock(&ftrace_lock);
  3521. if (unlikely(ftrace_disabled))
  3522. goto out_unlock;
  3523. ret = __register_ftrace_function(ops);
  3524. if (!ret)
  3525. ret = ftrace_startup(ops, 0);
  3526. out_unlock:
  3527. mutex_unlock(&ftrace_lock);
  3528. return ret;
  3529. }
  3530. EXPORT_SYMBOL_GPL(register_ftrace_function);
  3531. /**
  3532. * unregister_ftrace_function - unregister a function for profiling.
  3533. * @ops - ops structure that holds the function to unregister
  3534. *
  3535. * Unregister a function that was added to be called by ftrace profiling.
  3536. */
  3537. int unregister_ftrace_function(struct ftrace_ops *ops)
  3538. {
  3539. int ret;
  3540. mutex_lock(&ftrace_lock);
  3541. ret = __unregister_ftrace_function(ops);
  3542. if (!ret)
  3543. ftrace_shutdown(ops, 0);
  3544. mutex_unlock(&ftrace_lock);
  3545. return ret;
  3546. }
  3547. EXPORT_SYMBOL_GPL(unregister_ftrace_function);
  3548. int
  3549. ftrace_enable_sysctl(struct ctl_table *table, int write,
  3550. void __user *buffer, size_t *lenp,
  3551. loff_t *ppos)
  3552. {
  3553. int ret = -ENODEV;
  3554. mutex_lock(&ftrace_lock);
  3555. if (unlikely(ftrace_disabled))
  3556. goto out;
  3557. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  3558. if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
  3559. goto out;
  3560. last_ftrace_enabled = !!ftrace_enabled;
  3561. if (ftrace_enabled) {
  3562. ftrace_startup_sysctl();
  3563. /* we are starting ftrace again */
  3564. if (ftrace_ops_list != &ftrace_list_end) {
  3565. if (ftrace_ops_list->next == &ftrace_list_end)
  3566. ftrace_trace_function = ftrace_ops_list->func;
  3567. else
  3568. ftrace_trace_function = ftrace_ops_list_func;
  3569. }
  3570. } else {
  3571. /* stopping ftrace calls (just send to ftrace_stub) */
  3572. ftrace_trace_function = ftrace_stub;
  3573. ftrace_shutdown_sysctl();
  3574. }
  3575. out:
  3576. mutex_unlock(&ftrace_lock);
  3577. return ret;
  3578. }
  3579. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  3580. static int ftrace_graph_active;
  3581. static struct notifier_block ftrace_suspend_notifier;
  3582. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
  3583. {
  3584. return 0;
  3585. }
  3586. /* The callbacks that hook a function */
  3587. trace_func_graph_ret_t ftrace_graph_return =
  3588. (trace_func_graph_ret_t)ftrace_stub;
  3589. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  3590. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  3591. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  3592. {
  3593. int i;
  3594. int ret = 0;
  3595. unsigned long flags;
  3596. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  3597. struct task_struct *g, *t;
  3598. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  3599. ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
  3600. * sizeof(struct ftrace_ret_stack),
  3601. GFP_KERNEL);
  3602. if (!ret_stack_list[i]) {
  3603. start = 0;
  3604. end = i;
  3605. ret = -ENOMEM;
  3606. goto free;
  3607. }
  3608. }
  3609. read_lock_irqsave(&tasklist_lock, flags);
  3610. do_each_thread(g, t) {
  3611. if (start == end) {
  3612. ret = -EAGAIN;
  3613. goto unlock;
  3614. }
  3615. if (t->ret_stack == NULL) {
  3616. atomic_set(&t->tracing_graph_pause, 0);
  3617. atomic_set(&t->trace_overrun, 0);
  3618. t->curr_ret_stack = -1;
  3619. /* Make sure the tasks see the -1 first: */
  3620. smp_wmb();
  3621. t->ret_stack = ret_stack_list[start++];
  3622. }
  3623. } while_each_thread(g, t);
  3624. unlock:
  3625. read_unlock_irqrestore(&tasklist_lock, flags);
  3626. free:
  3627. for (i = start; i < end; i++)
  3628. kfree(ret_stack_list[i]);
  3629. return ret;
  3630. }
  3631. static void
  3632. ftrace_graph_probe_sched_switch(void *ignore,
  3633. struct task_struct *prev, struct task_struct *next)
  3634. {
  3635. unsigned long long timestamp;
  3636. int index;
  3637. /*
  3638. * Does the user want to count the time a function was asleep.
  3639. * If so, do not update the time stamps.
  3640. */
  3641. if (trace_flags & TRACE_ITER_SLEEP_TIME)
  3642. return;
  3643. timestamp = trace_clock_local();
  3644. prev->ftrace_timestamp = timestamp;
  3645. /* only process tasks that we timestamped */
  3646. if (!next->ftrace_timestamp)
  3647. return;
  3648. /*
  3649. * Update all the counters in next to make up for the
  3650. * time next was sleeping.
  3651. */
  3652. timestamp -= next->ftrace_timestamp;
  3653. for (index = next->curr_ret_stack; index >= 0; index--)
  3654. next->ret_stack[index].calltime += timestamp;
  3655. }
  3656. /* Allocate a return stack for each task */
  3657. static int start_graph_tracing(void)
  3658. {
  3659. struct ftrace_ret_stack **ret_stack_list;
  3660. int ret, cpu;
  3661. ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
  3662. sizeof(struct ftrace_ret_stack *),
  3663. GFP_KERNEL);
  3664. if (!ret_stack_list)
  3665. return -ENOMEM;
  3666. /* The cpu_boot init_task->ret_stack will never be freed */
  3667. for_each_online_cpu(cpu) {
  3668. if (!idle_task(cpu)->ret_stack)
  3669. ftrace_graph_init_idle_task(idle_task(cpu), cpu);
  3670. }
  3671. do {
  3672. ret = alloc_retstack_tasklist(ret_stack_list);
  3673. } while (ret == -EAGAIN);
  3674. if (!ret) {
  3675. ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  3676. if (ret)
  3677. pr_info("ftrace_graph: Couldn't activate tracepoint"
  3678. " probe to kernel_sched_switch\n");
  3679. }
  3680. kfree(ret_stack_list);
  3681. return ret;
  3682. }
  3683. /*
  3684. * Hibernation protection.
  3685. * The state of the current task is too much unstable during
  3686. * suspend/restore to disk. We want to protect against that.
  3687. */
  3688. static int
  3689. ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
  3690. void *unused)
  3691. {
  3692. switch (state) {
  3693. case PM_HIBERNATION_PREPARE:
  3694. pause_graph_tracing();
  3695. break;
  3696. case PM_POST_HIBERNATION:
  3697. unpause_graph_tracing();
  3698. break;
  3699. }
  3700. return NOTIFY_DONE;
  3701. }
  3702. int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  3703. trace_func_graph_ent_t entryfunc)
  3704. {
  3705. int ret = 0;
  3706. mutex_lock(&ftrace_lock);
  3707. /* we currently allow only one tracer registered at a time */
  3708. if (ftrace_graph_active) {
  3709. ret = -EBUSY;
  3710. goto out;
  3711. }
  3712. ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
  3713. register_pm_notifier(&ftrace_suspend_notifier);
  3714. ftrace_graph_active++;
  3715. ret = start_graph_tracing();
  3716. if (ret) {
  3717. ftrace_graph_active--;
  3718. goto out;
  3719. }
  3720. ftrace_graph_return = retfunc;
  3721. ftrace_graph_entry = entryfunc;
  3722. ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
  3723. out:
  3724. mutex_unlock(&ftrace_lock);
  3725. return ret;
  3726. }
  3727. void unregister_ftrace_graph(void)
  3728. {
  3729. mutex_lock(&ftrace_lock);
  3730. if (unlikely(!ftrace_graph_active))
  3731. goto out;
  3732. ftrace_graph_active--;
  3733. ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
  3734. ftrace_graph_entry = ftrace_graph_entry_stub;
  3735. ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
  3736. unregister_pm_notifier(&ftrace_suspend_notifier);
  3737. unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  3738. out:
  3739. mutex_unlock(&ftrace_lock);
  3740. }
  3741. static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
  3742. static void
  3743. graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
  3744. {
  3745. atomic_set(&t->tracing_graph_pause, 0);
  3746. atomic_set(&t->trace_overrun, 0);
  3747. t->ftrace_timestamp = 0;
  3748. /* make curr_ret_stack visible before we add the ret_stack */
  3749. smp_wmb();
  3750. t->ret_stack = ret_stack;
  3751. }
  3752. /*
  3753. * Allocate a return stack for the idle task. May be the first
  3754. * time through, or it may be done by CPU hotplug online.
  3755. */
  3756. void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
  3757. {
  3758. t->curr_ret_stack = -1;
  3759. /*
  3760. * The idle task has no parent, it either has its own
  3761. * stack or no stack at all.
  3762. */
  3763. if (t->ret_stack)
  3764. WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
  3765. if (ftrace_graph_active) {
  3766. struct ftrace_ret_stack *ret_stack;
  3767. ret_stack = per_cpu(idle_ret_stack, cpu);
  3768. if (!ret_stack) {
  3769. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  3770. * sizeof(struct ftrace_ret_stack),
  3771. GFP_KERNEL);
  3772. if (!ret_stack)
  3773. return;
  3774. per_cpu(idle_ret_stack, cpu) = ret_stack;
  3775. }
  3776. graph_init_task(t, ret_stack);
  3777. }
  3778. }
  3779. /* Allocate a return stack for newly created task */
  3780. void ftrace_graph_init_task(struct task_struct *t)
  3781. {
  3782. /* Make sure we do not use the parent ret_stack */
  3783. t->ret_stack = NULL;
  3784. t->curr_ret_stack = -1;
  3785. if (ftrace_graph_active) {
  3786. struct ftrace_ret_stack *ret_stack;
  3787. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  3788. * sizeof(struct ftrace_ret_stack),
  3789. GFP_KERNEL);
  3790. if (!ret_stack)
  3791. return;
  3792. graph_init_task(t, ret_stack);
  3793. }
  3794. }
  3795. void ftrace_graph_exit_task(struct task_struct *t)
  3796. {
  3797. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  3798. t->ret_stack = NULL;
  3799. /* NULL must become visible to IRQs before we free it: */
  3800. barrier();
  3801. kfree(ret_stack);
  3802. }
  3803. void ftrace_graph_stop(void)
  3804. {
  3805. ftrace_stop();
  3806. }
  3807. #endif