ftrace.c 106 KB

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