ftrace.c 108 KB

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