ftrace.c 100 KB

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