ftrace.c 108 KB

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