ftrace.c 109 KB

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