ftrace.c 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636
  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 void print_ip_ins(const char *fmt, unsigned char *p)
  1265. {
  1266. int i;
  1267. printk(KERN_CONT "%s", fmt);
  1268. for (i = 0; i < MCOUNT_INSN_SIZE; i++)
  1269. printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
  1270. }
  1271. /**
  1272. * ftrace_bug - report and shutdown function tracer
  1273. * @failed: The failed type (EFAULT, EINVAL, EPERM)
  1274. * @ip: The address that failed
  1275. *
  1276. * The arch code that enables or disables the function tracing
  1277. * can call ftrace_bug() when it has detected a problem in
  1278. * modifying the code. @failed should be one of either:
  1279. * EFAULT - if the problem happens on reading the @ip address
  1280. * EINVAL - if what is read at @ip is not what was expected
  1281. * EPERM - if the problem happens on writting to the @ip address
  1282. */
  1283. void ftrace_bug(int failed, unsigned long ip)
  1284. {
  1285. switch (failed) {
  1286. case -EFAULT:
  1287. FTRACE_WARN_ON_ONCE(1);
  1288. pr_info("ftrace faulted on modifying ");
  1289. print_ip_sym(ip);
  1290. break;
  1291. case -EINVAL:
  1292. FTRACE_WARN_ON_ONCE(1);
  1293. pr_info("ftrace failed to modify ");
  1294. print_ip_sym(ip);
  1295. print_ip_ins(" actual: ", (unsigned char *)ip);
  1296. printk(KERN_CONT "\n");
  1297. break;
  1298. case -EPERM:
  1299. FTRACE_WARN_ON_ONCE(1);
  1300. pr_info("ftrace faulted on writing ");
  1301. print_ip_sym(ip);
  1302. break;
  1303. default:
  1304. FTRACE_WARN_ON_ONCE(1);
  1305. pr_info("ftrace faulted on unknown error ");
  1306. print_ip_sym(ip);
  1307. }
  1308. }
  1309. /* Return 1 if the address range is reserved for ftrace */
  1310. int ftrace_text_reserved(void *start, void *end)
  1311. {
  1312. struct dyn_ftrace *rec;
  1313. struct ftrace_page *pg;
  1314. do_for_each_ftrace_rec(pg, rec) {
  1315. if (rec->ip <= (unsigned long)end &&
  1316. rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
  1317. return 1;
  1318. } while_for_each_ftrace_rec();
  1319. return 0;
  1320. }
  1321. static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
  1322. {
  1323. unsigned long flag = 0UL;
  1324. /*
  1325. * If we are updating calls:
  1326. *
  1327. * If the record has a ref count, then we need to enable it
  1328. * because someone is using it.
  1329. *
  1330. * Otherwise we make sure its disabled.
  1331. *
  1332. * If we are disabling calls, then disable all records that
  1333. * are enabled.
  1334. */
  1335. if (enable && (rec->flags & ~FTRACE_FL_MASK))
  1336. flag = FTRACE_FL_ENABLED;
  1337. /* If the state of this record hasn't changed, then do nothing */
  1338. if ((rec->flags & FTRACE_FL_ENABLED) == flag)
  1339. return FTRACE_UPDATE_IGNORE;
  1340. if (flag) {
  1341. if (update)
  1342. rec->flags |= FTRACE_FL_ENABLED;
  1343. return FTRACE_UPDATE_MAKE_CALL;
  1344. }
  1345. if (update)
  1346. rec->flags &= ~FTRACE_FL_ENABLED;
  1347. return FTRACE_UPDATE_MAKE_NOP;
  1348. }
  1349. /**
  1350. * ftrace_update_record, set a record that now is tracing or not
  1351. * @rec: the record to update
  1352. * @enable: set to 1 if the record is tracing, zero to force disable
  1353. *
  1354. * The records that represent all functions that can be traced need
  1355. * to be updated when tracing has been enabled.
  1356. */
  1357. int ftrace_update_record(struct dyn_ftrace *rec, int enable)
  1358. {
  1359. return ftrace_check_record(rec, enable, 1);
  1360. }
  1361. /**
  1362. * ftrace_test_record, check if the record has been enabled or not
  1363. * @rec: the record to test
  1364. * @enable: set to 1 to check if enabled, 0 if it is disabled
  1365. *
  1366. * The arch code may need to test if a record is already set to
  1367. * tracing to determine how to modify the function code that it
  1368. * represents.
  1369. */
  1370. int ftrace_test_record(struct dyn_ftrace *rec, int enable)
  1371. {
  1372. return ftrace_check_record(rec, enable, 0);
  1373. }
  1374. static int
  1375. __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
  1376. {
  1377. unsigned long ftrace_addr;
  1378. int ret;
  1379. ftrace_addr = (unsigned long)FTRACE_ADDR;
  1380. ret = ftrace_update_record(rec, enable);
  1381. switch (ret) {
  1382. case FTRACE_UPDATE_IGNORE:
  1383. return 0;
  1384. case FTRACE_UPDATE_MAKE_CALL:
  1385. return ftrace_make_call(rec, ftrace_addr);
  1386. case FTRACE_UPDATE_MAKE_NOP:
  1387. return ftrace_make_nop(NULL, rec, ftrace_addr);
  1388. }
  1389. return -1; /* unknow ftrace bug */
  1390. }
  1391. static void ftrace_replace_code(int update)
  1392. {
  1393. struct dyn_ftrace *rec;
  1394. struct ftrace_page *pg;
  1395. int failed;
  1396. if (unlikely(ftrace_disabled))
  1397. return;
  1398. do_for_each_ftrace_rec(pg, rec) {
  1399. failed = __ftrace_replace_code(rec, update);
  1400. if (failed) {
  1401. ftrace_bug(failed, rec->ip);
  1402. /* Stop processing */
  1403. return;
  1404. }
  1405. } while_for_each_ftrace_rec();
  1406. }
  1407. struct ftrace_rec_iter {
  1408. struct ftrace_page *pg;
  1409. int index;
  1410. };
  1411. /**
  1412. * ftrace_rec_iter_start, start up iterating over traced functions
  1413. *
  1414. * Returns an iterator handle that is used to iterate over all
  1415. * the records that represent address locations where functions
  1416. * are traced.
  1417. *
  1418. * May return NULL if no records are available.
  1419. */
  1420. struct ftrace_rec_iter *ftrace_rec_iter_start(void)
  1421. {
  1422. /*
  1423. * We only use a single iterator.
  1424. * Protected by the ftrace_lock mutex.
  1425. */
  1426. static struct ftrace_rec_iter ftrace_rec_iter;
  1427. struct ftrace_rec_iter *iter = &ftrace_rec_iter;
  1428. iter->pg = ftrace_pages_start;
  1429. iter->index = 0;
  1430. /* Could have empty pages */
  1431. while (iter->pg && !iter->pg->index)
  1432. iter->pg = iter->pg->next;
  1433. if (!iter->pg)
  1434. return NULL;
  1435. return iter;
  1436. }
  1437. /**
  1438. * ftrace_rec_iter_next, get the next record to process.
  1439. * @iter: The handle to the iterator.
  1440. *
  1441. * Returns the next iterator after the given iterator @iter.
  1442. */
  1443. struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
  1444. {
  1445. iter->index++;
  1446. if (iter->index >= iter->pg->index) {
  1447. iter->pg = iter->pg->next;
  1448. iter->index = 0;
  1449. /* Could have empty pages */
  1450. while (iter->pg && !iter->pg->index)
  1451. iter->pg = iter->pg->next;
  1452. }
  1453. if (!iter->pg)
  1454. return NULL;
  1455. return iter;
  1456. }
  1457. /**
  1458. * ftrace_rec_iter_record, get the record at the iterator location
  1459. * @iter: The current iterator location
  1460. *
  1461. * Returns the record that the current @iter is at.
  1462. */
  1463. struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
  1464. {
  1465. return &iter->pg->records[iter->index];
  1466. }
  1467. static int
  1468. ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
  1469. {
  1470. unsigned long ip;
  1471. int ret;
  1472. ip = rec->ip;
  1473. if (unlikely(ftrace_disabled))
  1474. return 0;
  1475. ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
  1476. if (ret) {
  1477. ftrace_bug(ret, ip);
  1478. return 0;
  1479. }
  1480. return 1;
  1481. }
  1482. /*
  1483. * archs can override this function if they must do something
  1484. * before the modifying code is performed.
  1485. */
  1486. int __weak ftrace_arch_code_modify_prepare(void)
  1487. {
  1488. return 0;
  1489. }
  1490. /*
  1491. * archs can override this function if they must do something
  1492. * after the modifying code is performed.
  1493. */
  1494. int __weak ftrace_arch_code_modify_post_process(void)
  1495. {
  1496. return 0;
  1497. }
  1498. static int __ftrace_modify_code(void *data)
  1499. {
  1500. int *command = data;
  1501. if (*command & FTRACE_UPDATE_CALLS)
  1502. ftrace_replace_code(1);
  1503. else if (*command & FTRACE_DISABLE_CALLS)
  1504. ftrace_replace_code(0);
  1505. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  1506. ftrace_update_ftrace_func(ftrace_trace_function);
  1507. if (*command & FTRACE_START_FUNC_RET)
  1508. ftrace_enable_ftrace_graph_caller();
  1509. else if (*command & FTRACE_STOP_FUNC_RET)
  1510. ftrace_disable_ftrace_graph_caller();
  1511. return 0;
  1512. }
  1513. /**
  1514. * ftrace_run_stop_machine, go back to the stop machine method
  1515. * @command: The command to tell ftrace what to do
  1516. *
  1517. * If an arch needs to fall back to the stop machine method, the
  1518. * it can call this function.
  1519. */
  1520. void ftrace_run_stop_machine(int command)
  1521. {
  1522. stop_machine(__ftrace_modify_code, &command, NULL);
  1523. }
  1524. /**
  1525. * arch_ftrace_update_code, modify the code to trace or not trace
  1526. * @command: The command that needs to be done
  1527. *
  1528. * Archs can override this function if it does not need to
  1529. * run stop_machine() to modify code.
  1530. */
  1531. void __weak arch_ftrace_update_code(int command)
  1532. {
  1533. ftrace_run_stop_machine(command);
  1534. }
  1535. static void ftrace_run_update_code(int command)
  1536. {
  1537. int ret;
  1538. ret = ftrace_arch_code_modify_prepare();
  1539. FTRACE_WARN_ON(ret);
  1540. if (ret)
  1541. return;
  1542. /*
  1543. * Do not call function tracer while we update the code.
  1544. * We are in stop machine.
  1545. */
  1546. function_trace_stop++;
  1547. /*
  1548. * By default we use stop_machine() to modify the code.
  1549. * But archs can do what ever they want as long as it
  1550. * is safe. The stop_machine() is the safest, but also
  1551. * produces the most overhead.
  1552. */
  1553. arch_ftrace_update_code(command);
  1554. #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  1555. /*
  1556. * For archs that call ftrace_test_stop_func(), we must
  1557. * wait till after we update all the function callers
  1558. * before we update the callback. This keeps different
  1559. * ops that record different functions from corrupting
  1560. * each other.
  1561. */
  1562. __ftrace_trace_function = __ftrace_trace_function_delay;
  1563. #endif
  1564. function_trace_stop--;
  1565. ret = ftrace_arch_code_modify_post_process();
  1566. FTRACE_WARN_ON(ret);
  1567. }
  1568. static ftrace_func_t saved_ftrace_func;
  1569. static int ftrace_start_up;
  1570. static int global_start_up;
  1571. static void ftrace_startup_enable(int command)
  1572. {
  1573. if (saved_ftrace_func != ftrace_trace_function) {
  1574. saved_ftrace_func = ftrace_trace_function;
  1575. command |= FTRACE_UPDATE_TRACE_FUNC;
  1576. }
  1577. if (!command || !ftrace_enabled)
  1578. return;
  1579. ftrace_run_update_code(command);
  1580. }
  1581. static int ftrace_startup(struct ftrace_ops *ops, int command)
  1582. {
  1583. bool hash_enable = true;
  1584. if (unlikely(ftrace_disabled))
  1585. return -ENODEV;
  1586. ftrace_start_up++;
  1587. command |= FTRACE_UPDATE_CALLS;
  1588. /* ops marked global share the filter hashes */
  1589. if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
  1590. ops = &global_ops;
  1591. /* Don't update hash if global is already set */
  1592. if (global_start_up)
  1593. hash_enable = false;
  1594. global_start_up++;
  1595. }
  1596. ops->flags |= FTRACE_OPS_FL_ENABLED;
  1597. if (hash_enable)
  1598. ftrace_hash_rec_enable(ops, 1);
  1599. ftrace_startup_enable(command);
  1600. return 0;
  1601. }
  1602. static void ftrace_shutdown(struct ftrace_ops *ops, int command)
  1603. {
  1604. bool hash_disable = true;
  1605. if (unlikely(ftrace_disabled))
  1606. return;
  1607. ftrace_start_up--;
  1608. /*
  1609. * Just warn in case of unbalance, no need to kill ftrace, it's not
  1610. * critical but the ftrace_call callers may be never nopped again after
  1611. * further ftrace uses.
  1612. */
  1613. WARN_ON_ONCE(ftrace_start_up < 0);
  1614. if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
  1615. ops = &global_ops;
  1616. global_start_up--;
  1617. WARN_ON_ONCE(global_start_up < 0);
  1618. /* Don't update hash if global still has users */
  1619. if (global_start_up) {
  1620. WARN_ON_ONCE(!ftrace_start_up);
  1621. hash_disable = false;
  1622. }
  1623. }
  1624. if (hash_disable)
  1625. ftrace_hash_rec_disable(ops, 1);
  1626. if (ops != &global_ops || !global_start_up)
  1627. ops->flags &= ~FTRACE_OPS_FL_ENABLED;
  1628. command |= FTRACE_UPDATE_CALLS;
  1629. if (saved_ftrace_func != ftrace_trace_function) {
  1630. saved_ftrace_func = ftrace_trace_function;
  1631. command |= FTRACE_UPDATE_TRACE_FUNC;
  1632. }
  1633. if (!command || !ftrace_enabled)
  1634. return;
  1635. ftrace_run_update_code(command);
  1636. }
  1637. static void ftrace_startup_sysctl(void)
  1638. {
  1639. if (unlikely(ftrace_disabled))
  1640. return;
  1641. /* Force update next time */
  1642. saved_ftrace_func = NULL;
  1643. /* ftrace_start_up is true if we want ftrace running */
  1644. if (ftrace_start_up)
  1645. ftrace_run_update_code(FTRACE_UPDATE_CALLS);
  1646. }
  1647. static void ftrace_shutdown_sysctl(void)
  1648. {
  1649. if (unlikely(ftrace_disabled))
  1650. return;
  1651. /* ftrace_start_up is true if ftrace is running */
  1652. if (ftrace_start_up)
  1653. ftrace_run_update_code(FTRACE_DISABLE_CALLS);
  1654. }
  1655. static cycle_t ftrace_update_time;
  1656. static unsigned long ftrace_update_cnt;
  1657. unsigned long ftrace_update_tot_cnt;
  1658. static int ops_traces_mod(struct ftrace_ops *ops)
  1659. {
  1660. struct ftrace_hash *hash;
  1661. hash = ops->filter_hash;
  1662. return ftrace_hash_empty(hash);
  1663. }
  1664. static int ftrace_update_code(struct module *mod)
  1665. {
  1666. struct ftrace_page *pg;
  1667. struct dyn_ftrace *p;
  1668. cycle_t start, stop;
  1669. unsigned long ref = 0;
  1670. int i;
  1671. /*
  1672. * When adding a module, we need to check if tracers are
  1673. * currently enabled and if they are set to trace all functions.
  1674. * If they are, we need to enable the module functions as well
  1675. * as update the reference counts for those function records.
  1676. */
  1677. if (mod) {
  1678. struct ftrace_ops *ops;
  1679. for (ops = ftrace_ops_list;
  1680. ops != &ftrace_list_end; ops = ops->next) {
  1681. if (ops->flags & FTRACE_OPS_FL_ENABLED &&
  1682. ops_traces_mod(ops))
  1683. ref++;
  1684. }
  1685. }
  1686. start = ftrace_now(raw_smp_processor_id());
  1687. ftrace_update_cnt = 0;
  1688. for (pg = ftrace_new_pgs; pg; pg = pg->next) {
  1689. for (i = 0; i < pg->index; i++) {
  1690. /* If something went wrong, bail without enabling anything */
  1691. if (unlikely(ftrace_disabled))
  1692. return -1;
  1693. p = &pg->records[i];
  1694. p->flags = ref;
  1695. /*
  1696. * Do the initial record conversion from mcount jump
  1697. * to the NOP instructions.
  1698. */
  1699. if (!ftrace_code_disable(mod, p))
  1700. break;
  1701. ftrace_update_cnt++;
  1702. /*
  1703. * If the tracing is enabled, go ahead and enable the record.
  1704. *
  1705. * The reason not to enable the record immediatelly is the
  1706. * inherent check of ftrace_make_nop/ftrace_make_call for
  1707. * correct previous instructions. Making first the NOP
  1708. * conversion puts the module to the correct state, thus
  1709. * passing the ftrace_make_call check.
  1710. */
  1711. if (ftrace_start_up && ref) {
  1712. int failed = __ftrace_replace_code(p, 1);
  1713. if (failed)
  1714. ftrace_bug(failed, p->ip);
  1715. }
  1716. }
  1717. }
  1718. ftrace_new_pgs = NULL;
  1719. stop = ftrace_now(raw_smp_processor_id());
  1720. ftrace_update_time = stop - start;
  1721. ftrace_update_tot_cnt += ftrace_update_cnt;
  1722. return 0;
  1723. }
  1724. static int ftrace_allocate_records(struct ftrace_page *pg, int count)
  1725. {
  1726. int order;
  1727. int cnt;
  1728. if (WARN_ON(!count))
  1729. return -EINVAL;
  1730. order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
  1731. /*
  1732. * We want to fill as much as possible. No more than a page
  1733. * may be empty.
  1734. */
  1735. while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
  1736. order--;
  1737. again:
  1738. pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
  1739. if (!pg->records) {
  1740. /* if we can't allocate this size, try something smaller */
  1741. if (!order)
  1742. return -ENOMEM;
  1743. order >>= 1;
  1744. goto again;
  1745. }
  1746. cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
  1747. pg->size = cnt;
  1748. if (cnt > count)
  1749. cnt = count;
  1750. return cnt;
  1751. }
  1752. static struct ftrace_page *
  1753. ftrace_allocate_pages(unsigned long num_to_init)
  1754. {
  1755. struct ftrace_page *start_pg;
  1756. struct ftrace_page *pg;
  1757. int order;
  1758. int cnt;
  1759. if (!num_to_init)
  1760. return 0;
  1761. start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
  1762. if (!pg)
  1763. return NULL;
  1764. /*
  1765. * Try to allocate as much as possible in one continues
  1766. * location that fills in all of the space. We want to
  1767. * waste as little space as possible.
  1768. */
  1769. for (;;) {
  1770. cnt = ftrace_allocate_records(pg, num_to_init);
  1771. if (cnt < 0)
  1772. goto free_pages;
  1773. num_to_init -= cnt;
  1774. if (!num_to_init)
  1775. break;
  1776. pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
  1777. if (!pg->next)
  1778. goto free_pages;
  1779. pg = pg->next;
  1780. }
  1781. return start_pg;
  1782. free_pages:
  1783. while (start_pg) {
  1784. order = get_count_order(pg->size / ENTRIES_PER_PAGE);
  1785. free_pages((unsigned long)pg->records, order);
  1786. start_pg = pg->next;
  1787. kfree(pg);
  1788. pg = start_pg;
  1789. }
  1790. pr_info("ftrace: FAILED to allocate memory for functions\n");
  1791. return NULL;
  1792. }
  1793. static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
  1794. {
  1795. int cnt;
  1796. if (!num_to_init) {
  1797. pr_info("ftrace: No functions to be traced?\n");
  1798. return -1;
  1799. }
  1800. cnt = num_to_init / ENTRIES_PER_PAGE;
  1801. pr_info("ftrace: allocating %ld entries in %d pages\n",
  1802. num_to_init, cnt + 1);
  1803. return 0;
  1804. }
  1805. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  1806. struct ftrace_iterator {
  1807. loff_t pos;
  1808. loff_t func_pos;
  1809. struct ftrace_page *pg;
  1810. struct dyn_ftrace *func;
  1811. struct ftrace_func_probe *probe;
  1812. struct trace_parser parser;
  1813. struct ftrace_hash *hash;
  1814. struct ftrace_ops *ops;
  1815. int hidx;
  1816. int idx;
  1817. unsigned flags;
  1818. };
  1819. static void *
  1820. t_hash_next(struct seq_file *m, loff_t *pos)
  1821. {
  1822. struct ftrace_iterator *iter = m->private;
  1823. struct hlist_node *hnd = NULL;
  1824. struct hlist_head *hhd;
  1825. (*pos)++;
  1826. iter->pos = *pos;
  1827. if (iter->probe)
  1828. hnd = &iter->probe->node;
  1829. retry:
  1830. if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
  1831. return NULL;
  1832. hhd = &ftrace_func_hash[iter->hidx];
  1833. if (hlist_empty(hhd)) {
  1834. iter->hidx++;
  1835. hnd = NULL;
  1836. goto retry;
  1837. }
  1838. if (!hnd)
  1839. hnd = hhd->first;
  1840. else {
  1841. hnd = hnd->next;
  1842. if (!hnd) {
  1843. iter->hidx++;
  1844. goto retry;
  1845. }
  1846. }
  1847. if (WARN_ON_ONCE(!hnd))
  1848. return NULL;
  1849. iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
  1850. return iter;
  1851. }
  1852. static void *t_hash_start(struct seq_file *m, loff_t *pos)
  1853. {
  1854. struct ftrace_iterator *iter = m->private;
  1855. void *p = NULL;
  1856. loff_t l;
  1857. if (!(iter->flags & FTRACE_ITER_DO_HASH))
  1858. return NULL;
  1859. if (iter->func_pos > *pos)
  1860. return NULL;
  1861. iter->hidx = 0;
  1862. for (l = 0; l <= (*pos - iter->func_pos); ) {
  1863. p = t_hash_next(m, &l);
  1864. if (!p)
  1865. break;
  1866. }
  1867. if (!p)
  1868. return NULL;
  1869. /* Only set this if we have an item */
  1870. iter->flags |= FTRACE_ITER_HASH;
  1871. return iter;
  1872. }
  1873. static int
  1874. t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
  1875. {
  1876. struct ftrace_func_probe *rec;
  1877. rec = iter->probe;
  1878. if (WARN_ON_ONCE(!rec))
  1879. return -EIO;
  1880. if (rec->ops->print)
  1881. return rec->ops->print(m, rec->ip, rec->ops, rec->data);
  1882. seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
  1883. if (rec->data)
  1884. seq_printf(m, ":%p", rec->data);
  1885. seq_putc(m, '\n');
  1886. return 0;
  1887. }
  1888. static void *
  1889. t_next(struct seq_file *m, void *v, loff_t *pos)
  1890. {
  1891. struct ftrace_iterator *iter = m->private;
  1892. struct ftrace_ops *ops = iter->ops;
  1893. struct dyn_ftrace *rec = NULL;
  1894. if (unlikely(ftrace_disabled))
  1895. return NULL;
  1896. if (iter->flags & FTRACE_ITER_HASH)
  1897. return t_hash_next(m, pos);
  1898. (*pos)++;
  1899. iter->pos = iter->func_pos = *pos;
  1900. if (iter->flags & FTRACE_ITER_PRINTALL)
  1901. return t_hash_start(m, pos);
  1902. retry:
  1903. if (iter->idx >= iter->pg->index) {
  1904. if (iter->pg->next) {
  1905. iter->pg = iter->pg->next;
  1906. iter->idx = 0;
  1907. goto retry;
  1908. }
  1909. } else {
  1910. rec = &iter->pg->records[iter->idx++];
  1911. if (((iter->flags & FTRACE_ITER_FILTER) &&
  1912. !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
  1913. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  1914. !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
  1915. ((iter->flags & FTRACE_ITER_ENABLED) &&
  1916. !(rec->flags & ~FTRACE_FL_MASK))) {
  1917. rec = NULL;
  1918. goto retry;
  1919. }
  1920. }
  1921. if (!rec)
  1922. return t_hash_start(m, pos);
  1923. iter->func = rec;
  1924. return iter;
  1925. }
  1926. static void reset_iter_read(struct ftrace_iterator *iter)
  1927. {
  1928. iter->pos = 0;
  1929. iter->func_pos = 0;
  1930. iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
  1931. }
  1932. static void *t_start(struct seq_file *m, loff_t *pos)
  1933. {
  1934. struct ftrace_iterator *iter = m->private;
  1935. struct ftrace_ops *ops = iter->ops;
  1936. void *p = NULL;
  1937. loff_t l;
  1938. mutex_lock(&ftrace_lock);
  1939. if (unlikely(ftrace_disabled))
  1940. return NULL;
  1941. /*
  1942. * If an lseek was done, then reset and start from beginning.
  1943. */
  1944. if (*pos < iter->pos)
  1945. reset_iter_read(iter);
  1946. /*
  1947. * For set_ftrace_filter reading, if we have the filter
  1948. * off, we can short cut and just print out that all
  1949. * functions are enabled.
  1950. */
  1951. if (iter->flags & FTRACE_ITER_FILTER &&
  1952. ftrace_hash_empty(ops->filter_hash)) {
  1953. if (*pos > 0)
  1954. return t_hash_start(m, pos);
  1955. iter->flags |= FTRACE_ITER_PRINTALL;
  1956. /* reset in case of seek/pread */
  1957. iter->flags &= ~FTRACE_ITER_HASH;
  1958. return iter;
  1959. }
  1960. if (iter->flags & FTRACE_ITER_HASH)
  1961. return t_hash_start(m, pos);
  1962. /*
  1963. * Unfortunately, we need to restart at ftrace_pages_start
  1964. * every time we let go of the ftrace_mutex. This is because
  1965. * those pointers can change without the lock.
  1966. */
  1967. iter->pg = ftrace_pages_start;
  1968. iter->idx = 0;
  1969. for (l = 0; l <= *pos; ) {
  1970. p = t_next(m, p, &l);
  1971. if (!p)
  1972. break;
  1973. }
  1974. if (!p)
  1975. return t_hash_start(m, pos);
  1976. return iter;
  1977. }
  1978. static void t_stop(struct seq_file *m, void *p)
  1979. {
  1980. mutex_unlock(&ftrace_lock);
  1981. }
  1982. static int t_show(struct seq_file *m, void *v)
  1983. {
  1984. struct ftrace_iterator *iter = m->private;
  1985. struct dyn_ftrace *rec;
  1986. if (iter->flags & FTRACE_ITER_HASH)
  1987. return t_hash_show(m, iter);
  1988. if (iter->flags & FTRACE_ITER_PRINTALL) {
  1989. seq_printf(m, "#### all functions enabled ####\n");
  1990. return 0;
  1991. }
  1992. rec = iter->func;
  1993. if (!rec)
  1994. return 0;
  1995. seq_printf(m, "%ps", (void *)rec->ip);
  1996. if (iter->flags & FTRACE_ITER_ENABLED)
  1997. seq_printf(m, " (%ld)",
  1998. rec->flags & ~FTRACE_FL_MASK);
  1999. seq_printf(m, "\n");
  2000. return 0;
  2001. }
  2002. static const struct seq_operations show_ftrace_seq_ops = {
  2003. .start = t_start,
  2004. .next = t_next,
  2005. .stop = t_stop,
  2006. .show = t_show,
  2007. };
  2008. static int
  2009. ftrace_avail_open(struct inode *inode, struct file *file)
  2010. {
  2011. struct ftrace_iterator *iter;
  2012. if (unlikely(ftrace_disabled))
  2013. return -ENODEV;
  2014. iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
  2015. if (iter) {
  2016. iter->pg = ftrace_pages_start;
  2017. iter->ops = &global_ops;
  2018. }
  2019. return iter ? 0 : -ENOMEM;
  2020. }
  2021. static int
  2022. ftrace_enabled_open(struct inode *inode, struct file *file)
  2023. {
  2024. struct ftrace_iterator *iter;
  2025. if (unlikely(ftrace_disabled))
  2026. return -ENODEV;
  2027. iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
  2028. if (iter) {
  2029. iter->pg = ftrace_pages_start;
  2030. iter->flags = FTRACE_ITER_ENABLED;
  2031. iter->ops = &global_ops;
  2032. }
  2033. return iter ? 0 : -ENOMEM;
  2034. }
  2035. static void ftrace_filter_reset(struct ftrace_hash *hash)
  2036. {
  2037. mutex_lock(&ftrace_lock);
  2038. ftrace_hash_clear(hash);
  2039. mutex_unlock(&ftrace_lock);
  2040. }
  2041. /**
  2042. * ftrace_regex_open - initialize function tracer filter files
  2043. * @ops: The ftrace_ops that hold the hash filters
  2044. * @flag: The type of filter to process
  2045. * @inode: The inode, usually passed in to your open routine
  2046. * @file: The file, usually passed in to your open routine
  2047. *
  2048. * ftrace_regex_open() initializes the filter files for the
  2049. * @ops. Depending on @flag it may process the filter hash or
  2050. * the notrace hash of @ops. With this called from the open
  2051. * routine, you can use ftrace_filter_write() for the write
  2052. * routine if @flag has FTRACE_ITER_FILTER set, or
  2053. * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
  2054. * ftrace_regex_lseek() should be used as the lseek routine, and
  2055. * release must call ftrace_regex_release().
  2056. */
  2057. int
  2058. ftrace_regex_open(struct ftrace_ops *ops, int flag,
  2059. struct inode *inode, struct file *file)
  2060. {
  2061. struct ftrace_iterator *iter;
  2062. struct ftrace_hash *hash;
  2063. int ret = 0;
  2064. if (unlikely(ftrace_disabled))
  2065. return -ENODEV;
  2066. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  2067. if (!iter)
  2068. return -ENOMEM;
  2069. if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
  2070. kfree(iter);
  2071. return -ENOMEM;
  2072. }
  2073. if (flag & FTRACE_ITER_NOTRACE)
  2074. hash = ops->notrace_hash;
  2075. else
  2076. hash = ops->filter_hash;
  2077. iter->ops = ops;
  2078. iter->flags = flag;
  2079. if (file->f_mode & FMODE_WRITE) {
  2080. mutex_lock(&ftrace_lock);
  2081. iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
  2082. mutex_unlock(&ftrace_lock);
  2083. if (!iter->hash) {
  2084. trace_parser_put(&iter->parser);
  2085. kfree(iter);
  2086. return -ENOMEM;
  2087. }
  2088. }
  2089. mutex_lock(&ftrace_regex_lock);
  2090. if ((file->f_mode & FMODE_WRITE) &&
  2091. (file->f_flags & O_TRUNC))
  2092. ftrace_filter_reset(iter->hash);
  2093. if (file->f_mode & FMODE_READ) {
  2094. iter->pg = ftrace_pages_start;
  2095. ret = seq_open(file, &show_ftrace_seq_ops);
  2096. if (!ret) {
  2097. struct seq_file *m = file->private_data;
  2098. m->private = iter;
  2099. } else {
  2100. /* Failed */
  2101. free_ftrace_hash(iter->hash);
  2102. trace_parser_put(&iter->parser);
  2103. kfree(iter);
  2104. }
  2105. } else
  2106. file->private_data = iter;
  2107. mutex_unlock(&ftrace_regex_lock);
  2108. return ret;
  2109. }
  2110. static int
  2111. ftrace_filter_open(struct inode *inode, struct file *file)
  2112. {
  2113. return ftrace_regex_open(&global_ops,
  2114. FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
  2115. inode, file);
  2116. }
  2117. static int
  2118. ftrace_notrace_open(struct inode *inode, struct file *file)
  2119. {
  2120. return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
  2121. inode, file);
  2122. }
  2123. loff_t
  2124. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  2125. {
  2126. loff_t ret;
  2127. if (file->f_mode & FMODE_READ)
  2128. ret = seq_lseek(file, offset, origin);
  2129. else
  2130. file->f_pos = ret = 1;
  2131. return ret;
  2132. }
  2133. static int ftrace_match(char *str, char *regex, int len, int type)
  2134. {
  2135. int matched = 0;
  2136. int slen;
  2137. switch (type) {
  2138. case MATCH_FULL:
  2139. if (strcmp(str, regex) == 0)
  2140. matched = 1;
  2141. break;
  2142. case MATCH_FRONT_ONLY:
  2143. if (strncmp(str, regex, len) == 0)
  2144. matched = 1;
  2145. break;
  2146. case MATCH_MIDDLE_ONLY:
  2147. if (strstr(str, regex))
  2148. matched = 1;
  2149. break;
  2150. case MATCH_END_ONLY:
  2151. slen = strlen(str);
  2152. if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
  2153. matched = 1;
  2154. break;
  2155. }
  2156. return matched;
  2157. }
  2158. static int
  2159. enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
  2160. {
  2161. struct ftrace_func_entry *entry;
  2162. int ret = 0;
  2163. entry = ftrace_lookup_ip(hash, rec->ip);
  2164. if (not) {
  2165. /* Do nothing if it doesn't exist */
  2166. if (!entry)
  2167. return 0;
  2168. free_hash_entry(hash, entry);
  2169. } else {
  2170. /* Do nothing if it exists */
  2171. if (entry)
  2172. return 0;
  2173. ret = add_hash_entry(hash, rec->ip);
  2174. }
  2175. return ret;
  2176. }
  2177. static int
  2178. ftrace_match_record(struct dyn_ftrace *rec, char *mod,
  2179. char *regex, int len, int type)
  2180. {
  2181. char str[KSYM_SYMBOL_LEN];
  2182. char *modname;
  2183. kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
  2184. if (mod) {
  2185. /* module lookup requires matching the module */
  2186. if (!modname || strcmp(modname, mod))
  2187. return 0;
  2188. /* blank search means to match all funcs in the mod */
  2189. if (!len)
  2190. return 1;
  2191. }
  2192. return ftrace_match(str, regex, len, type);
  2193. }
  2194. static int
  2195. match_records(struct ftrace_hash *hash, char *buff,
  2196. int len, char *mod, int not)
  2197. {
  2198. unsigned search_len = 0;
  2199. struct ftrace_page *pg;
  2200. struct dyn_ftrace *rec;
  2201. int type = MATCH_FULL;
  2202. char *search = buff;
  2203. int found = 0;
  2204. int ret;
  2205. if (len) {
  2206. type = filter_parse_regex(buff, len, &search, &not);
  2207. search_len = strlen(search);
  2208. }
  2209. mutex_lock(&ftrace_lock);
  2210. if (unlikely(ftrace_disabled))
  2211. goto out_unlock;
  2212. do_for_each_ftrace_rec(pg, rec) {
  2213. if (ftrace_match_record(rec, mod, search, search_len, type)) {
  2214. ret = enter_record(hash, rec, not);
  2215. if (ret < 0) {
  2216. found = ret;
  2217. goto out_unlock;
  2218. }
  2219. found = 1;
  2220. }
  2221. } while_for_each_ftrace_rec();
  2222. out_unlock:
  2223. mutex_unlock(&ftrace_lock);
  2224. return found;
  2225. }
  2226. static int
  2227. ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
  2228. {
  2229. return match_records(hash, buff, len, NULL, 0);
  2230. }
  2231. static int
  2232. ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
  2233. {
  2234. int not = 0;
  2235. /* blank or '*' mean the same */
  2236. if (strcmp(buff, "*") == 0)
  2237. buff[0] = 0;
  2238. /* handle the case of 'dont filter this module' */
  2239. if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
  2240. buff[0] = 0;
  2241. not = 1;
  2242. }
  2243. return match_records(hash, buff, strlen(buff), mod, not);
  2244. }
  2245. /*
  2246. * We register the module command as a template to show others how
  2247. * to register the a command as well.
  2248. */
  2249. static int
  2250. ftrace_mod_callback(struct ftrace_hash *hash,
  2251. char *func, char *cmd, char *param, int enable)
  2252. {
  2253. char *mod;
  2254. int ret = -EINVAL;
  2255. /*
  2256. * cmd == 'mod' because we only registered this func
  2257. * for the 'mod' ftrace_func_command.
  2258. * But if you register one func with multiple commands,
  2259. * you can tell which command was used by the cmd
  2260. * parameter.
  2261. */
  2262. /* we must have a module name */
  2263. if (!param)
  2264. return ret;
  2265. mod = strsep(&param, ":");
  2266. if (!strlen(mod))
  2267. return ret;
  2268. ret = ftrace_match_module_records(hash, func, mod);
  2269. if (!ret)
  2270. ret = -EINVAL;
  2271. if (ret < 0)
  2272. return ret;
  2273. return 0;
  2274. }
  2275. static struct ftrace_func_command ftrace_mod_cmd = {
  2276. .name = "mod",
  2277. .func = ftrace_mod_callback,
  2278. };
  2279. static int __init ftrace_mod_cmd_init(void)
  2280. {
  2281. return register_ftrace_command(&ftrace_mod_cmd);
  2282. }
  2283. device_initcall(ftrace_mod_cmd_init);
  2284. static void
  2285. function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
  2286. {
  2287. struct ftrace_func_probe *entry;
  2288. struct hlist_head *hhd;
  2289. struct hlist_node *n;
  2290. unsigned long key;
  2291. key = hash_long(ip, FTRACE_HASH_BITS);
  2292. hhd = &ftrace_func_hash[key];
  2293. if (hlist_empty(hhd))
  2294. return;
  2295. /*
  2296. * Disable preemption for these calls to prevent a RCU grace
  2297. * period. This syncs the hash iteration and freeing of items
  2298. * on the hash. rcu_read_lock is too dangerous here.
  2299. */
  2300. preempt_disable_notrace();
  2301. hlist_for_each_entry_rcu(entry, n, hhd, node) {
  2302. if (entry->ip == ip)
  2303. entry->ops->func(ip, parent_ip, &entry->data);
  2304. }
  2305. preempt_enable_notrace();
  2306. }
  2307. static struct ftrace_ops trace_probe_ops __read_mostly =
  2308. {
  2309. .func = function_trace_probe_call,
  2310. };
  2311. static int ftrace_probe_registered;
  2312. static void __enable_ftrace_function_probe(void)
  2313. {
  2314. int ret;
  2315. int i;
  2316. if (ftrace_probe_registered)
  2317. return;
  2318. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  2319. struct hlist_head *hhd = &ftrace_func_hash[i];
  2320. if (hhd->first)
  2321. break;
  2322. }
  2323. /* Nothing registered? */
  2324. if (i == FTRACE_FUNC_HASHSIZE)
  2325. return;
  2326. ret = __register_ftrace_function(&trace_probe_ops);
  2327. if (!ret)
  2328. ret = ftrace_startup(&trace_probe_ops, 0);
  2329. ftrace_probe_registered = 1;
  2330. }
  2331. static void __disable_ftrace_function_probe(void)
  2332. {
  2333. int ret;
  2334. int i;
  2335. if (!ftrace_probe_registered)
  2336. return;
  2337. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  2338. struct hlist_head *hhd = &ftrace_func_hash[i];
  2339. if (hhd->first)
  2340. return;
  2341. }
  2342. /* no more funcs left */
  2343. ret = __unregister_ftrace_function(&trace_probe_ops);
  2344. if (!ret)
  2345. ftrace_shutdown(&trace_probe_ops, 0);
  2346. ftrace_probe_registered = 0;
  2347. }
  2348. static void ftrace_free_entry_rcu(struct rcu_head *rhp)
  2349. {
  2350. struct ftrace_func_probe *entry =
  2351. container_of(rhp, struct ftrace_func_probe, rcu);
  2352. if (entry->ops->free)
  2353. entry->ops->free(&entry->data);
  2354. kfree(entry);
  2355. }
  2356. int
  2357. register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  2358. void *data)
  2359. {
  2360. struct ftrace_func_probe *entry;
  2361. struct ftrace_page *pg;
  2362. struct dyn_ftrace *rec;
  2363. int type, len, not;
  2364. unsigned long key;
  2365. int count = 0;
  2366. char *search;
  2367. type = filter_parse_regex(glob, strlen(glob), &search, &not);
  2368. len = strlen(search);
  2369. /* we do not support '!' for function probes */
  2370. if (WARN_ON(not))
  2371. return -EINVAL;
  2372. mutex_lock(&ftrace_lock);
  2373. if (unlikely(ftrace_disabled))
  2374. goto out_unlock;
  2375. do_for_each_ftrace_rec(pg, rec) {
  2376. if (!ftrace_match_record(rec, NULL, search, len, type))
  2377. continue;
  2378. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  2379. if (!entry) {
  2380. /* If we did not process any, then return error */
  2381. if (!count)
  2382. count = -ENOMEM;
  2383. goto out_unlock;
  2384. }
  2385. count++;
  2386. entry->data = data;
  2387. /*
  2388. * The caller might want to do something special
  2389. * for each function we find. We call the callback
  2390. * to give the caller an opportunity to do so.
  2391. */
  2392. if (ops->callback) {
  2393. if (ops->callback(rec->ip, &entry->data) < 0) {
  2394. /* caller does not like this func */
  2395. kfree(entry);
  2396. continue;
  2397. }
  2398. }
  2399. entry->ops = ops;
  2400. entry->ip = rec->ip;
  2401. key = hash_long(entry->ip, FTRACE_HASH_BITS);
  2402. hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
  2403. } while_for_each_ftrace_rec();
  2404. __enable_ftrace_function_probe();
  2405. out_unlock:
  2406. mutex_unlock(&ftrace_lock);
  2407. return count;
  2408. }
  2409. enum {
  2410. PROBE_TEST_FUNC = 1,
  2411. PROBE_TEST_DATA = 2
  2412. };
  2413. static void
  2414. __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  2415. void *data, int flags)
  2416. {
  2417. struct ftrace_func_probe *entry;
  2418. struct hlist_node *n, *tmp;
  2419. char str[KSYM_SYMBOL_LEN];
  2420. int type = MATCH_FULL;
  2421. int i, len = 0;
  2422. char *search;
  2423. if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
  2424. glob = NULL;
  2425. else if (glob) {
  2426. int not;
  2427. type = filter_parse_regex(glob, strlen(glob), &search, &not);
  2428. len = strlen(search);
  2429. /* we do not support '!' for function probes */
  2430. if (WARN_ON(not))
  2431. return;
  2432. }
  2433. mutex_lock(&ftrace_lock);
  2434. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  2435. struct hlist_head *hhd = &ftrace_func_hash[i];
  2436. hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
  2437. /* break up if statements for readability */
  2438. if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
  2439. continue;
  2440. if ((flags & PROBE_TEST_DATA) && entry->data != data)
  2441. continue;
  2442. /* do this last, since it is the most expensive */
  2443. if (glob) {
  2444. kallsyms_lookup(entry->ip, NULL, NULL,
  2445. NULL, str);
  2446. if (!ftrace_match(str, glob, len, type))
  2447. continue;
  2448. }
  2449. hlist_del(&entry->node);
  2450. call_rcu(&entry->rcu, ftrace_free_entry_rcu);
  2451. }
  2452. }
  2453. __disable_ftrace_function_probe();
  2454. mutex_unlock(&ftrace_lock);
  2455. }
  2456. void
  2457. unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  2458. void *data)
  2459. {
  2460. __unregister_ftrace_function_probe(glob, ops, data,
  2461. PROBE_TEST_FUNC | PROBE_TEST_DATA);
  2462. }
  2463. void
  2464. unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
  2465. {
  2466. __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
  2467. }
  2468. void unregister_ftrace_function_probe_all(char *glob)
  2469. {
  2470. __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
  2471. }
  2472. static LIST_HEAD(ftrace_commands);
  2473. static DEFINE_MUTEX(ftrace_cmd_mutex);
  2474. int register_ftrace_command(struct ftrace_func_command *cmd)
  2475. {
  2476. struct ftrace_func_command *p;
  2477. int ret = 0;
  2478. mutex_lock(&ftrace_cmd_mutex);
  2479. list_for_each_entry(p, &ftrace_commands, list) {
  2480. if (strcmp(cmd->name, p->name) == 0) {
  2481. ret = -EBUSY;
  2482. goto out_unlock;
  2483. }
  2484. }
  2485. list_add(&cmd->list, &ftrace_commands);
  2486. out_unlock:
  2487. mutex_unlock(&ftrace_cmd_mutex);
  2488. return ret;
  2489. }
  2490. int unregister_ftrace_command(struct ftrace_func_command *cmd)
  2491. {
  2492. struct ftrace_func_command *p, *n;
  2493. int ret = -ENODEV;
  2494. mutex_lock(&ftrace_cmd_mutex);
  2495. list_for_each_entry_safe(p, n, &ftrace_commands, list) {
  2496. if (strcmp(cmd->name, p->name) == 0) {
  2497. ret = 0;
  2498. list_del_init(&p->list);
  2499. goto out_unlock;
  2500. }
  2501. }
  2502. out_unlock:
  2503. mutex_unlock(&ftrace_cmd_mutex);
  2504. return ret;
  2505. }
  2506. static int ftrace_process_regex(struct ftrace_hash *hash,
  2507. char *buff, int len, int enable)
  2508. {
  2509. char *func, *command, *next = buff;
  2510. struct ftrace_func_command *p;
  2511. int ret = -EINVAL;
  2512. func = strsep(&next, ":");
  2513. if (!next) {
  2514. ret = ftrace_match_records(hash, func, len);
  2515. if (!ret)
  2516. ret = -EINVAL;
  2517. if (ret < 0)
  2518. return ret;
  2519. return 0;
  2520. }
  2521. /* command found */
  2522. command = strsep(&next, ":");
  2523. mutex_lock(&ftrace_cmd_mutex);
  2524. list_for_each_entry(p, &ftrace_commands, list) {
  2525. if (strcmp(p->name, command) == 0) {
  2526. ret = p->func(hash, func, command, next, enable);
  2527. goto out_unlock;
  2528. }
  2529. }
  2530. out_unlock:
  2531. mutex_unlock(&ftrace_cmd_mutex);
  2532. return ret;
  2533. }
  2534. static ssize_t
  2535. ftrace_regex_write(struct file *file, const char __user *ubuf,
  2536. size_t cnt, loff_t *ppos, int enable)
  2537. {
  2538. struct ftrace_iterator *iter;
  2539. struct trace_parser *parser;
  2540. ssize_t ret, read;
  2541. if (!cnt)
  2542. return 0;
  2543. mutex_lock(&ftrace_regex_lock);
  2544. ret = -ENODEV;
  2545. if (unlikely(ftrace_disabled))
  2546. goto out_unlock;
  2547. if (file->f_mode & FMODE_READ) {
  2548. struct seq_file *m = file->private_data;
  2549. iter = m->private;
  2550. } else
  2551. iter = file->private_data;
  2552. parser = &iter->parser;
  2553. read = trace_get_user(parser, ubuf, cnt, ppos);
  2554. if (read >= 0 && trace_parser_loaded(parser) &&
  2555. !trace_parser_cont(parser)) {
  2556. ret = ftrace_process_regex(iter->hash, parser->buffer,
  2557. parser->idx, enable);
  2558. trace_parser_clear(parser);
  2559. if (ret)
  2560. goto out_unlock;
  2561. }
  2562. ret = read;
  2563. out_unlock:
  2564. mutex_unlock(&ftrace_regex_lock);
  2565. return ret;
  2566. }
  2567. ssize_t
  2568. ftrace_filter_write(struct file *file, const char __user *ubuf,
  2569. size_t cnt, loff_t *ppos)
  2570. {
  2571. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  2572. }
  2573. ssize_t
  2574. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  2575. size_t cnt, loff_t *ppos)
  2576. {
  2577. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  2578. }
  2579. static int
  2580. ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
  2581. int reset, int enable)
  2582. {
  2583. struct ftrace_hash **orig_hash;
  2584. struct ftrace_hash *hash;
  2585. int ret;
  2586. /* All global ops uses the global ops filters */
  2587. if (ops->flags & FTRACE_OPS_FL_GLOBAL)
  2588. ops = &global_ops;
  2589. if (unlikely(ftrace_disabled))
  2590. return -ENODEV;
  2591. if (enable)
  2592. orig_hash = &ops->filter_hash;
  2593. else
  2594. orig_hash = &ops->notrace_hash;
  2595. hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
  2596. if (!hash)
  2597. return -ENOMEM;
  2598. mutex_lock(&ftrace_regex_lock);
  2599. if (reset)
  2600. ftrace_filter_reset(hash);
  2601. if (buf && !ftrace_match_records(hash, buf, len)) {
  2602. ret = -EINVAL;
  2603. goto out_regex_unlock;
  2604. }
  2605. mutex_lock(&ftrace_lock);
  2606. ret = ftrace_hash_move(ops, enable, orig_hash, hash);
  2607. if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
  2608. && ftrace_enabled)
  2609. ftrace_run_update_code(FTRACE_UPDATE_CALLS);
  2610. mutex_unlock(&ftrace_lock);
  2611. out_regex_unlock:
  2612. mutex_unlock(&ftrace_regex_lock);
  2613. free_ftrace_hash(hash);
  2614. return ret;
  2615. }
  2616. /**
  2617. * ftrace_set_filter - set a function to filter on in ftrace
  2618. * @ops - the ops to set the filter with
  2619. * @buf - the string that holds the function filter text.
  2620. * @len - the length of the string.
  2621. * @reset - non zero to reset all filters before applying this filter.
  2622. *
  2623. * Filters denote which functions should be enabled when tracing is enabled.
  2624. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  2625. */
  2626. int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
  2627. int len, int reset)
  2628. {
  2629. return ftrace_set_regex(ops, buf, len, reset, 1);
  2630. }
  2631. EXPORT_SYMBOL_GPL(ftrace_set_filter);
  2632. /**
  2633. * ftrace_set_notrace - set a function to not trace in ftrace
  2634. * @ops - the ops to set the notrace filter with
  2635. * @buf - the string that holds the function notrace text.
  2636. * @len - the length of the string.
  2637. * @reset - non zero to reset all filters before applying this filter.
  2638. *
  2639. * Notrace Filters denote which functions should not be enabled when tracing
  2640. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  2641. * for tracing.
  2642. */
  2643. int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
  2644. int len, int reset)
  2645. {
  2646. return ftrace_set_regex(ops, buf, len, reset, 0);
  2647. }
  2648. EXPORT_SYMBOL_GPL(ftrace_set_notrace);
  2649. /**
  2650. * ftrace_set_filter - set a function to filter on in ftrace
  2651. * @ops - the ops to set the filter with
  2652. * @buf - the string that holds the function filter text.
  2653. * @len - the length of the string.
  2654. * @reset - non zero to reset all filters before applying this filter.
  2655. *
  2656. * Filters denote which functions should be enabled when tracing is enabled.
  2657. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  2658. */
  2659. void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
  2660. {
  2661. ftrace_set_regex(&global_ops, buf, len, reset, 1);
  2662. }
  2663. EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
  2664. /**
  2665. * ftrace_set_notrace - set a function to not trace in ftrace
  2666. * @ops - the ops to set the notrace filter with
  2667. * @buf - the string that holds the function notrace text.
  2668. * @len - the length of the string.
  2669. * @reset - non zero to reset all filters before applying this filter.
  2670. *
  2671. * Notrace Filters denote which functions should not be enabled when tracing
  2672. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  2673. * for tracing.
  2674. */
  2675. void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
  2676. {
  2677. ftrace_set_regex(&global_ops, buf, len, reset, 0);
  2678. }
  2679. EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
  2680. /*
  2681. * command line interface to allow users to set filters on boot up.
  2682. */
  2683. #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
  2684. static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
  2685. static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
  2686. static int __init set_ftrace_notrace(char *str)
  2687. {
  2688. strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
  2689. return 1;
  2690. }
  2691. __setup("ftrace_notrace=", set_ftrace_notrace);
  2692. static int __init set_ftrace_filter(char *str)
  2693. {
  2694. strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
  2695. return 1;
  2696. }
  2697. __setup("ftrace_filter=", set_ftrace_filter);
  2698. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2699. static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
  2700. static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
  2701. static int __init set_graph_function(char *str)
  2702. {
  2703. strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
  2704. return 1;
  2705. }
  2706. __setup("ftrace_graph_filter=", set_graph_function);
  2707. static void __init set_ftrace_early_graph(char *buf)
  2708. {
  2709. int ret;
  2710. char *func;
  2711. while (buf) {
  2712. func = strsep(&buf, ",");
  2713. /* we allow only one expression at a time */
  2714. ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
  2715. func);
  2716. if (ret)
  2717. printk(KERN_DEBUG "ftrace: function %s not "
  2718. "traceable\n", func);
  2719. }
  2720. }
  2721. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2722. void __init
  2723. ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
  2724. {
  2725. char *func;
  2726. while (buf) {
  2727. func = strsep(&buf, ",");
  2728. ftrace_set_regex(ops, func, strlen(func), 0, enable);
  2729. }
  2730. }
  2731. static void __init set_ftrace_early_filters(void)
  2732. {
  2733. if (ftrace_filter_buf[0])
  2734. ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
  2735. if (ftrace_notrace_buf[0])
  2736. ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
  2737. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2738. if (ftrace_graph_buf[0])
  2739. set_ftrace_early_graph(ftrace_graph_buf);
  2740. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2741. }
  2742. int ftrace_regex_release(struct inode *inode, struct file *file)
  2743. {
  2744. struct seq_file *m = (struct seq_file *)file->private_data;
  2745. struct ftrace_iterator *iter;
  2746. struct ftrace_hash **orig_hash;
  2747. struct trace_parser *parser;
  2748. int filter_hash;
  2749. int ret;
  2750. mutex_lock(&ftrace_regex_lock);
  2751. if (file->f_mode & FMODE_READ) {
  2752. iter = m->private;
  2753. seq_release(inode, file);
  2754. } else
  2755. iter = file->private_data;
  2756. parser = &iter->parser;
  2757. if (trace_parser_loaded(parser)) {
  2758. parser->buffer[parser->idx] = 0;
  2759. ftrace_match_records(iter->hash, parser->buffer, parser->idx);
  2760. }
  2761. trace_parser_put(parser);
  2762. if (file->f_mode & FMODE_WRITE) {
  2763. filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
  2764. if (filter_hash)
  2765. orig_hash = &iter->ops->filter_hash;
  2766. else
  2767. orig_hash = &iter->ops->notrace_hash;
  2768. mutex_lock(&ftrace_lock);
  2769. ret = ftrace_hash_move(iter->ops, filter_hash,
  2770. orig_hash, iter->hash);
  2771. if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
  2772. && ftrace_enabled)
  2773. ftrace_run_update_code(FTRACE_UPDATE_CALLS);
  2774. mutex_unlock(&ftrace_lock);
  2775. }
  2776. free_ftrace_hash(iter->hash);
  2777. kfree(iter);
  2778. mutex_unlock(&ftrace_regex_lock);
  2779. return 0;
  2780. }
  2781. static const struct file_operations ftrace_avail_fops = {
  2782. .open = ftrace_avail_open,
  2783. .read = seq_read,
  2784. .llseek = seq_lseek,
  2785. .release = seq_release_private,
  2786. };
  2787. static const struct file_operations ftrace_enabled_fops = {
  2788. .open = ftrace_enabled_open,
  2789. .read = seq_read,
  2790. .llseek = seq_lseek,
  2791. .release = seq_release_private,
  2792. };
  2793. static const struct file_operations ftrace_filter_fops = {
  2794. .open = ftrace_filter_open,
  2795. .read = seq_read,
  2796. .write = ftrace_filter_write,
  2797. .llseek = ftrace_regex_lseek,
  2798. .release = ftrace_regex_release,
  2799. };
  2800. static const struct file_operations ftrace_notrace_fops = {
  2801. .open = ftrace_notrace_open,
  2802. .read = seq_read,
  2803. .write = ftrace_notrace_write,
  2804. .llseek = ftrace_regex_lseek,
  2805. .release = ftrace_regex_release,
  2806. };
  2807. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2808. static DEFINE_MUTEX(graph_lock);
  2809. int ftrace_graph_count;
  2810. int ftrace_graph_filter_enabled;
  2811. unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
  2812. static void *
  2813. __g_next(struct seq_file *m, loff_t *pos)
  2814. {
  2815. if (*pos >= ftrace_graph_count)
  2816. return NULL;
  2817. return &ftrace_graph_funcs[*pos];
  2818. }
  2819. static void *
  2820. g_next(struct seq_file *m, void *v, loff_t *pos)
  2821. {
  2822. (*pos)++;
  2823. return __g_next(m, pos);
  2824. }
  2825. static void *g_start(struct seq_file *m, loff_t *pos)
  2826. {
  2827. mutex_lock(&graph_lock);
  2828. /* Nothing, tell g_show to print all functions are enabled */
  2829. if (!ftrace_graph_filter_enabled && !*pos)
  2830. return (void *)1;
  2831. return __g_next(m, pos);
  2832. }
  2833. static void g_stop(struct seq_file *m, void *p)
  2834. {
  2835. mutex_unlock(&graph_lock);
  2836. }
  2837. static int g_show(struct seq_file *m, void *v)
  2838. {
  2839. unsigned long *ptr = v;
  2840. if (!ptr)
  2841. return 0;
  2842. if (ptr == (unsigned long *)1) {
  2843. seq_printf(m, "#### all functions enabled ####\n");
  2844. return 0;
  2845. }
  2846. seq_printf(m, "%ps\n", (void *)*ptr);
  2847. return 0;
  2848. }
  2849. static const struct seq_operations ftrace_graph_seq_ops = {
  2850. .start = g_start,
  2851. .next = g_next,
  2852. .stop = g_stop,
  2853. .show = g_show,
  2854. };
  2855. static int
  2856. ftrace_graph_open(struct inode *inode, struct file *file)
  2857. {
  2858. int ret = 0;
  2859. if (unlikely(ftrace_disabled))
  2860. return -ENODEV;
  2861. mutex_lock(&graph_lock);
  2862. if ((file->f_mode & FMODE_WRITE) &&
  2863. (file->f_flags & O_TRUNC)) {
  2864. ftrace_graph_filter_enabled = 0;
  2865. ftrace_graph_count = 0;
  2866. memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
  2867. }
  2868. mutex_unlock(&graph_lock);
  2869. if (file->f_mode & FMODE_READ)
  2870. ret = seq_open(file, &ftrace_graph_seq_ops);
  2871. return ret;
  2872. }
  2873. static int
  2874. ftrace_graph_release(struct inode *inode, struct file *file)
  2875. {
  2876. if (file->f_mode & FMODE_READ)
  2877. seq_release(inode, file);
  2878. return 0;
  2879. }
  2880. static int
  2881. ftrace_set_func(unsigned long *array, int *idx, char *buffer)
  2882. {
  2883. struct dyn_ftrace *rec;
  2884. struct ftrace_page *pg;
  2885. int search_len;
  2886. int fail = 1;
  2887. int type, not;
  2888. char *search;
  2889. bool exists;
  2890. int i;
  2891. /* decode regex */
  2892. type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
  2893. if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
  2894. return -EBUSY;
  2895. search_len = strlen(search);
  2896. mutex_lock(&ftrace_lock);
  2897. if (unlikely(ftrace_disabled)) {
  2898. mutex_unlock(&ftrace_lock);
  2899. return -ENODEV;
  2900. }
  2901. do_for_each_ftrace_rec(pg, rec) {
  2902. if (ftrace_match_record(rec, NULL, search, search_len, type)) {
  2903. /* if it is in the array */
  2904. exists = false;
  2905. for (i = 0; i < *idx; i++) {
  2906. if (array[i] == rec->ip) {
  2907. exists = true;
  2908. break;
  2909. }
  2910. }
  2911. if (!not) {
  2912. fail = 0;
  2913. if (!exists) {
  2914. array[(*idx)++] = rec->ip;
  2915. if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
  2916. goto out;
  2917. }
  2918. } else {
  2919. if (exists) {
  2920. array[i] = array[--(*idx)];
  2921. array[*idx] = 0;
  2922. fail = 0;
  2923. }
  2924. }
  2925. }
  2926. } while_for_each_ftrace_rec();
  2927. out:
  2928. mutex_unlock(&ftrace_lock);
  2929. if (fail)
  2930. return -EINVAL;
  2931. ftrace_graph_filter_enabled = 1;
  2932. return 0;
  2933. }
  2934. static ssize_t
  2935. ftrace_graph_write(struct file *file, const char __user *ubuf,
  2936. size_t cnt, loff_t *ppos)
  2937. {
  2938. struct trace_parser parser;
  2939. ssize_t read, ret;
  2940. if (!cnt)
  2941. return 0;
  2942. mutex_lock(&graph_lock);
  2943. if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
  2944. ret = -ENOMEM;
  2945. goto out_unlock;
  2946. }
  2947. read = trace_get_user(&parser, ubuf, cnt, ppos);
  2948. if (read >= 0 && trace_parser_loaded((&parser))) {
  2949. parser.buffer[parser.idx] = 0;
  2950. /* we allow only one expression at a time */
  2951. ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
  2952. parser.buffer);
  2953. if (ret)
  2954. goto out_free;
  2955. }
  2956. ret = read;
  2957. out_free:
  2958. trace_parser_put(&parser);
  2959. out_unlock:
  2960. mutex_unlock(&graph_lock);
  2961. return ret;
  2962. }
  2963. static const struct file_operations ftrace_graph_fops = {
  2964. .open = ftrace_graph_open,
  2965. .read = seq_read,
  2966. .write = ftrace_graph_write,
  2967. .release = ftrace_graph_release,
  2968. .llseek = seq_lseek,
  2969. };
  2970. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2971. static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
  2972. {
  2973. trace_create_file("available_filter_functions", 0444,
  2974. d_tracer, NULL, &ftrace_avail_fops);
  2975. trace_create_file("enabled_functions", 0444,
  2976. d_tracer, NULL, &ftrace_enabled_fops);
  2977. trace_create_file("set_ftrace_filter", 0644, d_tracer,
  2978. NULL, &ftrace_filter_fops);
  2979. trace_create_file("set_ftrace_notrace", 0644, d_tracer,
  2980. NULL, &ftrace_notrace_fops);
  2981. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2982. trace_create_file("set_graph_function", 0444, d_tracer,
  2983. NULL,
  2984. &ftrace_graph_fops);
  2985. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2986. return 0;
  2987. }
  2988. static int ftrace_cmp_ips(const void *a, const void *b)
  2989. {
  2990. const unsigned long *ipa = a;
  2991. const unsigned long *ipb = b;
  2992. if (*ipa > *ipb)
  2993. return 1;
  2994. if (*ipa < *ipb)
  2995. return -1;
  2996. return 0;
  2997. }
  2998. static void ftrace_swap_ips(void *a, void *b, int size)
  2999. {
  3000. unsigned long *ipa = a;
  3001. unsigned long *ipb = b;
  3002. unsigned long t;
  3003. t = *ipa;
  3004. *ipa = *ipb;
  3005. *ipb = t;
  3006. }
  3007. static int ftrace_process_locs(struct module *mod,
  3008. unsigned long *start,
  3009. unsigned long *end)
  3010. {
  3011. struct ftrace_page *start_pg;
  3012. struct ftrace_page *pg;
  3013. struct dyn_ftrace *rec;
  3014. unsigned long count;
  3015. unsigned long *p;
  3016. unsigned long addr;
  3017. unsigned long flags = 0; /* Shut up gcc */
  3018. int ret = -ENOMEM;
  3019. count = end - start;
  3020. if (!count)
  3021. return 0;
  3022. sort(start, count, sizeof(*start),
  3023. ftrace_cmp_ips, ftrace_swap_ips);
  3024. start_pg = ftrace_allocate_pages(count);
  3025. if (!start_pg)
  3026. return -ENOMEM;
  3027. mutex_lock(&ftrace_lock);
  3028. /*
  3029. * Core and each module needs their own pages, as
  3030. * modules will free them when they are removed.
  3031. * Force a new page to be allocated for modules.
  3032. */
  3033. if (!mod) {
  3034. WARN_ON(ftrace_pages || ftrace_pages_start);
  3035. /* First initialization */
  3036. ftrace_pages = ftrace_pages_start = start_pg;
  3037. } else {
  3038. if (!ftrace_pages)
  3039. goto out;
  3040. if (WARN_ON(ftrace_pages->next)) {
  3041. /* Hmm, we have free pages? */
  3042. while (ftrace_pages->next)
  3043. ftrace_pages = ftrace_pages->next;
  3044. }
  3045. ftrace_pages->next = start_pg;
  3046. }
  3047. p = start;
  3048. pg = start_pg;
  3049. while (p < end) {
  3050. addr = ftrace_call_adjust(*p++);
  3051. /*
  3052. * Some architecture linkers will pad between
  3053. * the different mcount_loc sections of different
  3054. * object files to satisfy alignments.
  3055. * Skip any NULL pointers.
  3056. */
  3057. if (!addr)
  3058. continue;
  3059. if (pg->index == pg->size) {
  3060. /* We should have allocated enough */
  3061. if (WARN_ON(!pg->next))
  3062. break;
  3063. pg = pg->next;
  3064. }
  3065. rec = &pg->records[pg->index++];
  3066. rec->ip = addr;
  3067. }
  3068. /* We should have used all pages */
  3069. WARN_ON(pg->next);
  3070. /* Assign the last page to ftrace_pages */
  3071. ftrace_pages = pg;
  3072. /* These new locations need to be initialized */
  3073. ftrace_new_pgs = start_pg;
  3074. /*
  3075. * We only need to disable interrupts on start up
  3076. * because we are modifying code that an interrupt
  3077. * may execute, and the modification is not atomic.
  3078. * But for modules, nothing runs the code we modify
  3079. * until we are finished with it, and there's no
  3080. * reason to cause large interrupt latencies while we do it.
  3081. */
  3082. if (!mod)
  3083. local_irq_save(flags);
  3084. ftrace_update_code(mod);
  3085. if (!mod)
  3086. local_irq_restore(flags);
  3087. ret = 0;
  3088. out:
  3089. mutex_unlock(&ftrace_lock);
  3090. return ret;
  3091. }
  3092. #ifdef CONFIG_MODULES
  3093. #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
  3094. void ftrace_release_mod(struct module *mod)
  3095. {
  3096. struct dyn_ftrace *rec;
  3097. struct ftrace_page **last_pg;
  3098. struct ftrace_page *pg;
  3099. int order;
  3100. mutex_lock(&ftrace_lock);
  3101. if (ftrace_disabled)
  3102. goto out_unlock;
  3103. /*
  3104. * Each module has its own ftrace_pages, remove
  3105. * them from the list.
  3106. */
  3107. last_pg = &ftrace_pages_start;
  3108. for (pg = ftrace_pages_start; pg; pg = *last_pg) {
  3109. rec = &pg->records[0];
  3110. if (within_module_core(rec->ip, mod)) {
  3111. /*
  3112. * As core pages are first, the first
  3113. * page should never be a module page.
  3114. */
  3115. if (WARN_ON(pg == ftrace_pages_start))
  3116. goto out_unlock;
  3117. /* Check if we are deleting the last page */
  3118. if (pg == ftrace_pages)
  3119. ftrace_pages = next_to_ftrace_page(last_pg);
  3120. *last_pg = pg->next;
  3121. order = get_count_order(pg->size / ENTRIES_PER_PAGE);
  3122. free_pages((unsigned long)pg->records, order);
  3123. kfree(pg);
  3124. } else
  3125. last_pg = &pg->next;
  3126. }
  3127. out_unlock:
  3128. mutex_unlock(&ftrace_lock);
  3129. }
  3130. static void ftrace_init_module(struct module *mod,
  3131. unsigned long *start, unsigned long *end)
  3132. {
  3133. if (ftrace_disabled || start == end)
  3134. return;
  3135. ftrace_process_locs(mod, start, end);
  3136. }
  3137. static int ftrace_module_notify(struct notifier_block *self,
  3138. unsigned long val, void *data)
  3139. {
  3140. struct module *mod = data;
  3141. switch (val) {
  3142. case MODULE_STATE_COMING:
  3143. ftrace_init_module(mod, mod->ftrace_callsites,
  3144. mod->ftrace_callsites +
  3145. mod->num_ftrace_callsites);
  3146. break;
  3147. case MODULE_STATE_GOING:
  3148. ftrace_release_mod(mod);
  3149. break;
  3150. }
  3151. return 0;
  3152. }
  3153. #else
  3154. static int ftrace_module_notify(struct notifier_block *self,
  3155. unsigned long val, void *data)
  3156. {
  3157. return 0;
  3158. }
  3159. #endif /* CONFIG_MODULES */
  3160. struct notifier_block ftrace_module_nb = {
  3161. .notifier_call = ftrace_module_notify,
  3162. .priority = 0,
  3163. };
  3164. extern unsigned long __start_mcount_loc[];
  3165. extern unsigned long __stop_mcount_loc[];
  3166. void __init ftrace_init(void)
  3167. {
  3168. unsigned long count, addr, flags;
  3169. int ret;
  3170. /* Keep the ftrace pointer to the stub */
  3171. addr = (unsigned long)ftrace_stub;
  3172. local_irq_save(flags);
  3173. ftrace_dyn_arch_init(&addr);
  3174. local_irq_restore(flags);
  3175. /* ftrace_dyn_arch_init places the return code in addr */
  3176. if (addr)
  3177. goto failed;
  3178. count = __stop_mcount_loc - __start_mcount_loc;
  3179. ret = ftrace_dyn_table_alloc(count);
  3180. if (ret)
  3181. goto failed;
  3182. last_ftrace_enabled = ftrace_enabled = 1;
  3183. ret = ftrace_process_locs(NULL,
  3184. __start_mcount_loc,
  3185. __stop_mcount_loc);
  3186. ret = register_module_notifier(&ftrace_module_nb);
  3187. if (ret)
  3188. pr_warning("Failed to register trace ftrace module notifier\n");
  3189. set_ftrace_early_filters();
  3190. return;
  3191. failed:
  3192. ftrace_disabled = 1;
  3193. }
  3194. #else
  3195. static struct ftrace_ops global_ops = {
  3196. .func = ftrace_stub,
  3197. };
  3198. static int __init ftrace_nodyn_init(void)
  3199. {
  3200. ftrace_enabled = 1;
  3201. return 0;
  3202. }
  3203. device_initcall(ftrace_nodyn_init);
  3204. static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
  3205. static inline void ftrace_startup_enable(int command) { }
  3206. /* Keep as macros so we do not need to define the commands */
  3207. # define ftrace_startup(ops, command) \
  3208. ({ \
  3209. (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
  3210. 0; \
  3211. })
  3212. # define ftrace_shutdown(ops, command) do { } while (0)
  3213. # define ftrace_startup_sysctl() do { } while (0)
  3214. # define ftrace_shutdown_sysctl() do { } while (0)
  3215. static inline int
  3216. ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
  3217. {
  3218. return 1;
  3219. }
  3220. #endif /* CONFIG_DYNAMIC_FTRACE */
  3221. static void
  3222. ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip)
  3223. {
  3224. struct ftrace_ops *op;
  3225. if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
  3226. return;
  3227. /*
  3228. * Some of the ops may be dynamically allocated,
  3229. * they must be freed after a synchronize_sched().
  3230. */
  3231. preempt_disable_notrace();
  3232. trace_recursion_set(TRACE_CONTROL_BIT);
  3233. op = rcu_dereference_raw(ftrace_control_list);
  3234. while (op != &ftrace_list_end) {
  3235. if (!ftrace_function_local_disabled(op) &&
  3236. ftrace_ops_test(op, ip))
  3237. op->func(ip, parent_ip);
  3238. op = rcu_dereference_raw(op->next);
  3239. };
  3240. trace_recursion_clear(TRACE_CONTROL_BIT);
  3241. preempt_enable_notrace();
  3242. }
  3243. static struct ftrace_ops control_ops = {
  3244. .func = ftrace_ops_control_func,
  3245. };
  3246. static void
  3247. ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
  3248. {
  3249. struct ftrace_ops *op;
  3250. if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
  3251. return;
  3252. trace_recursion_set(TRACE_INTERNAL_BIT);
  3253. /*
  3254. * Some of the ops may be dynamically allocated,
  3255. * they must be freed after a synchronize_sched().
  3256. */
  3257. preempt_disable_notrace();
  3258. op = rcu_dereference_raw(ftrace_ops_list);
  3259. while (op != &ftrace_list_end) {
  3260. if (ftrace_ops_test(op, ip))
  3261. op->func(ip, parent_ip);
  3262. op = rcu_dereference_raw(op->next);
  3263. };
  3264. preempt_enable_notrace();
  3265. trace_recursion_clear(TRACE_INTERNAL_BIT);
  3266. }
  3267. static void clear_ftrace_swapper(void)
  3268. {
  3269. struct task_struct *p;
  3270. int cpu;
  3271. get_online_cpus();
  3272. for_each_online_cpu(cpu) {
  3273. p = idle_task(cpu);
  3274. clear_tsk_trace_trace(p);
  3275. }
  3276. put_online_cpus();
  3277. }
  3278. static void set_ftrace_swapper(void)
  3279. {
  3280. struct task_struct *p;
  3281. int cpu;
  3282. get_online_cpus();
  3283. for_each_online_cpu(cpu) {
  3284. p = idle_task(cpu);
  3285. set_tsk_trace_trace(p);
  3286. }
  3287. put_online_cpus();
  3288. }
  3289. static void clear_ftrace_pid(struct pid *pid)
  3290. {
  3291. struct task_struct *p;
  3292. rcu_read_lock();
  3293. do_each_pid_task(pid, PIDTYPE_PID, p) {
  3294. clear_tsk_trace_trace(p);
  3295. } while_each_pid_task(pid, PIDTYPE_PID, p);
  3296. rcu_read_unlock();
  3297. put_pid(pid);
  3298. }
  3299. static void set_ftrace_pid(struct pid *pid)
  3300. {
  3301. struct task_struct *p;
  3302. rcu_read_lock();
  3303. do_each_pid_task(pid, PIDTYPE_PID, p) {
  3304. set_tsk_trace_trace(p);
  3305. } while_each_pid_task(pid, PIDTYPE_PID, p);
  3306. rcu_read_unlock();
  3307. }
  3308. static void clear_ftrace_pid_task(struct pid *pid)
  3309. {
  3310. if (pid == ftrace_swapper_pid)
  3311. clear_ftrace_swapper();
  3312. else
  3313. clear_ftrace_pid(pid);
  3314. }
  3315. static void set_ftrace_pid_task(struct pid *pid)
  3316. {
  3317. if (pid == ftrace_swapper_pid)
  3318. set_ftrace_swapper();
  3319. else
  3320. set_ftrace_pid(pid);
  3321. }
  3322. static int ftrace_pid_add(int p)
  3323. {
  3324. struct pid *pid;
  3325. struct ftrace_pid *fpid;
  3326. int ret = -EINVAL;
  3327. mutex_lock(&ftrace_lock);
  3328. if (!p)
  3329. pid = ftrace_swapper_pid;
  3330. else
  3331. pid = find_get_pid(p);
  3332. if (!pid)
  3333. goto out;
  3334. ret = 0;
  3335. list_for_each_entry(fpid, &ftrace_pids, list)
  3336. if (fpid->pid == pid)
  3337. goto out_put;
  3338. ret = -ENOMEM;
  3339. fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
  3340. if (!fpid)
  3341. goto out_put;
  3342. list_add(&fpid->list, &ftrace_pids);
  3343. fpid->pid = pid;
  3344. set_ftrace_pid_task(pid);
  3345. ftrace_update_pid_func();
  3346. ftrace_startup_enable(0);
  3347. mutex_unlock(&ftrace_lock);
  3348. return 0;
  3349. out_put:
  3350. if (pid != ftrace_swapper_pid)
  3351. put_pid(pid);
  3352. out:
  3353. mutex_unlock(&ftrace_lock);
  3354. return ret;
  3355. }
  3356. static void ftrace_pid_reset(void)
  3357. {
  3358. struct ftrace_pid *fpid, *safe;
  3359. mutex_lock(&ftrace_lock);
  3360. list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
  3361. struct pid *pid = fpid->pid;
  3362. clear_ftrace_pid_task(pid);
  3363. list_del(&fpid->list);
  3364. kfree(fpid);
  3365. }
  3366. ftrace_update_pid_func();
  3367. ftrace_startup_enable(0);
  3368. mutex_unlock(&ftrace_lock);
  3369. }
  3370. static void *fpid_start(struct seq_file *m, loff_t *pos)
  3371. {
  3372. mutex_lock(&ftrace_lock);
  3373. if (list_empty(&ftrace_pids) && (!*pos))
  3374. return (void *) 1;
  3375. return seq_list_start(&ftrace_pids, *pos);
  3376. }
  3377. static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
  3378. {
  3379. if (v == (void *)1)
  3380. return NULL;
  3381. return seq_list_next(v, &ftrace_pids, pos);
  3382. }
  3383. static void fpid_stop(struct seq_file *m, void *p)
  3384. {
  3385. mutex_unlock(&ftrace_lock);
  3386. }
  3387. static int fpid_show(struct seq_file *m, void *v)
  3388. {
  3389. const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
  3390. if (v == (void *)1) {
  3391. seq_printf(m, "no pid\n");
  3392. return 0;
  3393. }
  3394. if (fpid->pid == ftrace_swapper_pid)
  3395. seq_printf(m, "swapper tasks\n");
  3396. else
  3397. seq_printf(m, "%u\n", pid_vnr(fpid->pid));
  3398. return 0;
  3399. }
  3400. static const struct seq_operations ftrace_pid_sops = {
  3401. .start = fpid_start,
  3402. .next = fpid_next,
  3403. .stop = fpid_stop,
  3404. .show = fpid_show,
  3405. };
  3406. static int
  3407. ftrace_pid_open(struct inode *inode, struct file *file)
  3408. {
  3409. int ret = 0;
  3410. if ((file->f_mode & FMODE_WRITE) &&
  3411. (file->f_flags & O_TRUNC))
  3412. ftrace_pid_reset();
  3413. if (file->f_mode & FMODE_READ)
  3414. ret = seq_open(file, &ftrace_pid_sops);
  3415. return ret;
  3416. }
  3417. static ssize_t
  3418. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  3419. size_t cnt, loff_t *ppos)
  3420. {
  3421. char buf[64], *tmp;
  3422. long val;
  3423. int ret;
  3424. if (cnt >= sizeof(buf))
  3425. return -EINVAL;
  3426. if (copy_from_user(&buf, ubuf, cnt))
  3427. return -EFAULT;
  3428. buf[cnt] = 0;
  3429. /*
  3430. * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
  3431. * to clean the filter quietly.
  3432. */
  3433. tmp = strstrip(buf);
  3434. if (strlen(tmp) == 0)
  3435. return 1;
  3436. ret = strict_strtol(tmp, 10, &val);
  3437. if (ret < 0)
  3438. return ret;
  3439. ret = ftrace_pid_add(val);
  3440. return ret ? ret : cnt;
  3441. }
  3442. static int
  3443. ftrace_pid_release(struct inode *inode, struct file *file)
  3444. {
  3445. if (file->f_mode & FMODE_READ)
  3446. seq_release(inode, file);
  3447. return 0;
  3448. }
  3449. static const struct file_operations ftrace_pid_fops = {
  3450. .open = ftrace_pid_open,
  3451. .write = ftrace_pid_write,
  3452. .read = seq_read,
  3453. .llseek = seq_lseek,
  3454. .release = ftrace_pid_release,
  3455. };
  3456. static __init int ftrace_init_debugfs(void)
  3457. {
  3458. struct dentry *d_tracer;
  3459. d_tracer = tracing_init_dentry();
  3460. if (!d_tracer)
  3461. return 0;
  3462. ftrace_init_dyn_debugfs(d_tracer);
  3463. trace_create_file("set_ftrace_pid", 0644, d_tracer,
  3464. NULL, &ftrace_pid_fops);
  3465. ftrace_profile_debugfs(d_tracer);
  3466. return 0;
  3467. }
  3468. fs_initcall(ftrace_init_debugfs);
  3469. /**
  3470. * ftrace_kill - kill ftrace
  3471. *
  3472. * This function should be used by panic code. It stops ftrace
  3473. * but in a not so nice way. If you need to simply kill ftrace
  3474. * from a non-atomic section, use ftrace_kill.
  3475. */
  3476. void ftrace_kill(void)
  3477. {
  3478. ftrace_disabled = 1;
  3479. ftrace_enabled = 0;
  3480. clear_ftrace_function();
  3481. }
  3482. /**
  3483. * Test if ftrace is dead or not.
  3484. */
  3485. int ftrace_is_dead(void)
  3486. {
  3487. return ftrace_disabled;
  3488. }
  3489. /**
  3490. * register_ftrace_function - register a function for profiling
  3491. * @ops - ops structure that holds the function for profiling.
  3492. *
  3493. * Register a function to be called by all functions in the
  3494. * kernel.
  3495. *
  3496. * Note: @ops->func and all the functions it calls must be labeled
  3497. * with "notrace", otherwise it will go into a
  3498. * recursive loop.
  3499. */
  3500. int register_ftrace_function(struct ftrace_ops *ops)
  3501. {
  3502. int ret = -1;
  3503. mutex_lock(&ftrace_lock);
  3504. if (unlikely(ftrace_disabled))
  3505. goto out_unlock;
  3506. ret = __register_ftrace_function(ops);
  3507. if (!ret)
  3508. ret = ftrace_startup(ops, 0);
  3509. out_unlock:
  3510. mutex_unlock(&ftrace_lock);
  3511. return ret;
  3512. }
  3513. EXPORT_SYMBOL_GPL(register_ftrace_function);
  3514. /**
  3515. * unregister_ftrace_function - unregister a function for profiling.
  3516. * @ops - ops structure that holds the function to unregister
  3517. *
  3518. * Unregister a function that was added to be called by ftrace profiling.
  3519. */
  3520. int unregister_ftrace_function(struct ftrace_ops *ops)
  3521. {
  3522. int ret;
  3523. mutex_lock(&ftrace_lock);
  3524. ret = __unregister_ftrace_function(ops);
  3525. if (!ret)
  3526. ftrace_shutdown(ops, 0);
  3527. mutex_unlock(&ftrace_lock);
  3528. return ret;
  3529. }
  3530. EXPORT_SYMBOL_GPL(unregister_ftrace_function);
  3531. int
  3532. ftrace_enable_sysctl(struct ctl_table *table, int write,
  3533. void __user *buffer, size_t *lenp,
  3534. loff_t *ppos)
  3535. {
  3536. int ret = -ENODEV;
  3537. mutex_lock(&ftrace_lock);
  3538. if (unlikely(ftrace_disabled))
  3539. goto out;
  3540. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  3541. if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
  3542. goto out;
  3543. last_ftrace_enabled = !!ftrace_enabled;
  3544. if (ftrace_enabled) {
  3545. ftrace_startup_sysctl();
  3546. /* we are starting ftrace again */
  3547. if (ftrace_ops_list != &ftrace_list_end) {
  3548. if (ftrace_ops_list->next == &ftrace_list_end)
  3549. ftrace_trace_function = ftrace_ops_list->func;
  3550. else
  3551. ftrace_trace_function = ftrace_ops_list_func;
  3552. }
  3553. } else {
  3554. /* stopping ftrace calls (just send to ftrace_stub) */
  3555. ftrace_trace_function = ftrace_stub;
  3556. ftrace_shutdown_sysctl();
  3557. }
  3558. out:
  3559. mutex_unlock(&ftrace_lock);
  3560. return ret;
  3561. }
  3562. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  3563. static int ftrace_graph_active;
  3564. static struct notifier_block ftrace_suspend_notifier;
  3565. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
  3566. {
  3567. return 0;
  3568. }
  3569. /* The callbacks that hook a function */
  3570. trace_func_graph_ret_t ftrace_graph_return =
  3571. (trace_func_graph_ret_t)ftrace_stub;
  3572. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  3573. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  3574. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  3575. {
  3576. int i;
  3577. int ret = 0;
  3578. unsigned long flags;
  3579. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  3580. struct task_struct *g, *t;
  3581. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  3582. ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
  3583. * sizeof(struct ftrace_ret_stack),
  3584. GFP_KERNEL);
  3585. if (!ret_stack_list[i]) {
  3586. start = 0;
  3587. end = i;
  3588. ret = -ENOMEM;
  3589. goto free;
  3590. }
  3591. }
  3592. read_lock_irqsave(&tasklist_lock, flags);
  3593. do_each_thread(g, t) {
  3594. if (start == end) {
  3595. ret = -EAGAIN;
  3596. goto unlock;
  3597. }
  3598. if (t->ret_stack == NULL) {
  3599. atomic_set(&t->tracing_graph_pause, 0);
  3600. atomic_set(&t->trace_overrun, 0);
  3601. t->curr_ret_stack = -1;
  3602. /* Make sure the tasks see the -1 first: */
  3603. smp_wmb();
  3604. t->ret_stack = ret_stack_list[start++];
  3605. }
  3606. } while_each_thread(g, t);
  3607. unlock:
  3608. read_unlock_irqrestore(&tasklist_lock, flags);
  3609. free:
  3610. for (i = start; i < end; i++)
  3611. kfree(ret_stack_list[i]);
  3612. return ret;
  3613. }
  3614. static void
  3615. ftrace_graph_probe_sched_switch(void *ignore,
  3616. struct task_struct *prev, struct task_struct *next)
  3617. {
  3618. unsigned long long timestamp;
  3619. int index;
  3620. /*
  3621. * Does the user want to count the time a function was asleep.
  3622. * If so, do not update the time stamps.
  3623. */
  3624. if (trace_flags & TRACE_ITER_SLEEP_TIME)
  3625. return;
  3626. timestamp = trace_clock_local();
  3627. prev->ftrace_timestamp = timestamp;
  3628. /* only process tasks that we timestamped */
  3629. if (!next->ftrace_timestamp)
  3630. return;
  3631. /*
  3632. * Update all the counters in next to make up for the
  3633. * time next was sleeping.
  3634. */
  3635. timestamp -= next->ftrace_timestamp;
  3636. for (index = next->curr_ret_stack; index >= 0; index--)
  3637. next->ret_stack[index].calltime += timestamp;
  3638. }
  3639. /* Allocate a return stack for each task */
  3640. static int start_graph_tracing(void)
  3641. {
  3642. struct ftrace_ret_stack **ret_stack_list;
  3643. int ret, cpu;
  3644. ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
  3645. sizeof(struct ftrace_ret_stack *),
  3646. GFP_KERNEL);
  3647. if (!ret_stack_list)
  3648. return -ENOMEM;
  3649. /* The cpu_boot init_task->ret_stack will never be freed */
  3650. for_each_online_cpu(cpu) {
  3651. if (!idle_task(cpu)->ret_stack)
  3652. ftrace_graph_init_idle_task(idle_task(cpu), cpu);
  3653. }
  3654. do {
  3655. ret = alloc_retstack_tasklist(ret_stack_list);
  3656. } while (ret == -EAGAIN);
  3657. if (!ret) {
  3658. ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  3659. if (ret)
  3660. pr_info("ftrace_graph: Couldn't activate tracepoint"
  3661. " probe to kernel_sched_switch\n");
  3662. }
  3663. kfree(ret_stack_list);
  3664. return ret;
  3665. }
  3666. /*
  3667. * Hibernation protection.
  3668. * The state of the current task is too much unstable during
  3669. * suspend/restore to disk. We want to protect against that.
  3670. */
  3671. static int
  3672. ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
  3673. void *unused)
  3674. {
  3675. switch (state) {
  3676. case PM_HIBERNATION_PREPARE:
  3677. pause_graph_tracing();
  3678. break;
  3679. case PM_POST_HIBERNATION:
  3680. unpause_graph_tracing();
  3681. break;
  3682. }
  3683. return NOTIFY_DONE;
  3684. }
  3685. int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  3686. trace_func_graph_ent_t entryfunc)
  3687. {
  3688. int ret = 0;
  3689. mutex_lock(&ftrace_lock);
  3690. /* we currently allow only one tracer registered at a time */
  3691. if (ftrace_graph_active) {
  3692. ret = -EBUSY;
  3693. goto out;
  3694. }
  3695. ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
  3696. register_pm_notifier(&ftrace_suspend_notifier);
  3697. ftrace_graph_active++;
  3698. ret = start_graph_tracing();
  3699. if (ret) {
  3700. ftrace_graph_active--;
  3701. goto out;
  3702. }
  3703. ftrace_graph_return = retfunc;
  3704. ftrace_graph_entry = entryfunc;
  3705. ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
  3706. out:
  3707. mutex_unlock(&ftrace_lock);
  3708. return ret;
  3709. }
  3710. void unregister_ftrace_graph(void)
  3711. {
  3712. mutex_lock(&ftrace_lock);
  3713. if (unlikely(!ftrace_graph_active))
  3714. goto out;
  3715. ftrace_graph_active--;
  3716. ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
  3717. ftrace_graph_entry = ftrace_graph_entry_stub;
  3718. ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
  3719. unregister_pm_notifier(&ftrace_suspend_notifier);
  3720. unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  3721. out:
  3722. mutex_unlock(&ftrace_lock);
  3723. }
  3724. static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
  3725. static void
  3726. graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
  3727. {
  3728. atomic_set(&t->tracing_graph_pause, 0);
  3729. atomic_set(&t->trace_overrun, 0);
  3730. t->ftrace_timestamp = 0;
  3731. /* make curr_ret_stack visible before we add the ret_stack */
  3732. smp_wmb();
  3733. t->ret_stack = ret_stack;
  3734. }
  3735. /*
  3736. * Allocate a return stack for the idle task. May be the first
  3737. * time through, or it may be done by CPU hotplug online.
  3738. */
  3739. void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
  3740. {
  3741. t->curr_ret_stack = -1;
  3742. /*
  3743. * The idle task has no parent, it either has its own
  3744. * stack or no stack at all.
  3745. */
  3746. if (t->ret_stack)
  3747. WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
  3748. if (ftrace_graph_active) {
  3749. struct ftrace_ret_stack *ret_stack;
  3750. ret_stack = per_cpu(idle_ret_stack, cpu);
  3751. if (!ret_stack) {
  3752. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  3753. * sizeof(struct ftrace_ret_stack),
  3754. GFP_KERNEL);
  3755. if (!ret_stack)
  3756. return;
  3757. per_cpu(idle_ret_stack, cpu) = ret_stack;
  3758. }
  3759. graph_init_task(t, ret_stack);
  3760. }
  3761. }
  3762. /* Allocate a return stack for newly created task */
  3763. void ftrace_graph_init_task(struct task_struct *t)
  3764. {
  3765. /* Make sure we do not use the parent ret_stack */
  3766. t->ret_stack = NULL;
  3767. t->curr_ret_stack = -1;
  3768. if (ftrace_graph_active) {
  3769. struct ftrace_ret_stack *ret_stack;
  3770. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  3771. * sizeof(struct ftrace_ret_stack),
  3772. GFP_KERNEL);
  3773. if (!ret_stack)
  3774. return;
  3775. graph_init_task(t, ret_stack);
  3776. }
  3777. }
  3778. void ftrace_graph_exit_task(struct task_struct *t)
  3779. {
  3780. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  3781. t->ret_stack = NULL;
  3782. /* NULL must become visible to IRQs before we free it: */
  3783. barrier();
  3784. kfree(ret_stack);
  3785. }
  3786. void ftrace_graph_stop(void)
  3787. {
  3788. ftrace_stop();
  3789. }
  3790. #endif