ftrace.c 106 KB

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