ftrace.c 100 KB

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