ftrace.c 105 KB

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