ftrace.c 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655
  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 *key = a;
  1143. const struct dyn_ftrace *rec = b;
  1144. if (key->flags < rec->ip)
  1145. return -1;
  1146. if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
  1147. return 1;
  1148. return 0;
  1149. }
  1150. static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
  1151. {
  1152. struct ftrace_page *pg;
  1153. struct dyn_ftrace *rec;
  1154. struct dyn_ftrace key;
  1155. key.ip = start;
  1156. key.flags = end; /* overload flags, as it is unsigned long */
  1157. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  1158. if (end < pg->records[0].ip ||
  1159. start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
  1160. continue;
  1161. rec = bsearch(&key, pg->records, pg->index,
  1162. sizeof(struct dyn_ftrace),
  1163. ftrace_cmp_recs);
  1164. if (rec)
  1165. return rec->ip;
  1166. }
  1167. return 0;
  1168. }
  1169. /**
  1170. * ftrace_location - return true if the ip giving is a traced location
  1171. * @ip: the instruction pointer to check
  1172. *
  1173. * Returns rec->ip if @ip given is a pointer to a ftrace location.
  1174. * That is, the instruction that is either a NOP or call to
  1175. * the function tracer. It checks the ftrace internal tables to
  1176. * determine if the address belongs or not.
  1177. */
  1178. unsigned long ftrace_location(unsigned long ip)
  1179. {
  1180. return ftrace_location_range(ip, ip);
  1181. }
  1182. /**
  1183. * ftrace_text_reserved - return true if range contains an ftrace location
  1184. * @start: start of range to search
  1185. * @end: end of range to search (inclusive). @end points to the last byte to check.
  1186. *
  1187. * Returns 1 if @start and @end contains a ftrace location.
  1188. * That is, the instruction that is either a NOP or call to
  1189. * the function tracer. It checks the ftrace internal tables to
  1190. * determine if the address belongs or not.
  1191. */
  1192. int ftrace_text_reserved(void *start, void *end)
  1193. {
  1194. unsigned long ret;
  1195. ret = ftrace_location_range((unsigned long)start,
  1196. (unsigned long)end);
  1197. return (int)!!ret;
  1198. }
  1199. static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
  1200. int filter_hash,
  1201. bool inc)
  1202. {
  1203. struct ftrace_hash *hash;
  1204. struct ftrace_hash *other_hash;
  1205. struct ftrace_page *pg;
  1206. struct dyn_ftrace *rec;
  1207. int count = 0;
  1208. int all = 0;
  1209. /* Only update if the ops has been registered */
  1210. if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
  1211. return;
  1212. /*
  1213. * In the filter_hash case:
  1214. * If the count is zero, we update all records.
  1215. * Otherwise we just update the items in the hash.
  1216. *
  1217. * In the notrace_hash case:
  1218. * We enable the update in the hash.
  1219. * As disabling notrace means enabling the tracing,
  1220. * and enabling notrace means disabling, the inc variable
  1221. * gets inversed.
  1222. */
  1223. if (filter_hash) {
  1224. hash = ops->filter_hash;
  1225. other_hash = ops->notrace_hash;
  1226. if (ftrace_hash_empty(hash))
  1227. all = 1;
  1228. } else {
  1229. inc = !inc;
  1230. hash = ops->notrace_hash;
  1231. other_hash = ops->filter_hash;
  1232. /*
  1233. * If the notrace hash has no items,
  1234. * then there's nothing to do.
  1235. */
  1236. if (ftrace_hash_empty(hash))
  1237. return;
  1238. }
  1239. do_for_each_ftrace_rec(pg, rec) {
  1240. int in_other_hash = 0;
  1241. int in_hash = 0;
  1242. int match = 0;
  1243. if (all) {
  1244. /*
  1245. * Only the filter_hash affects all records.
  1246. * Update if the record is not in the notrace hash.
  1247. */
  1248. if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
  1249. match = 1;
  1250. } else {
  1251. in_hash = !!ftrace_lookup_ip(hash, rec->ip);
  1252. in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
  1253. /*
  1254. *
  1255. */
  1256. if (filter_hash && in_hash && !in_other_hash)
  1257. match = 1;
  1258. else if (!filter_hash && in_hash &&
  1259. (in_other_hash || ftrace_hash_empty(other_hash)))
  1260. match = 1;
  1261. }
  1262. if (!match)
  1263. continue;
  1264. if (inc) {
  1265. rec->flags++;
  1266. if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
  1267. return;
  1268. } else {
  1269. if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
  1270. return;
  1271. rec->flags--;
  1272. }
  1273. count++;
  1274. /* Shortcut, if we handled all records, we are done. */
  1275. if (!all && count == hash->count)
  1276. return;
  1277. } while_for_each_ftrace_rec();
  1278. }
  1279. static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
  1280. int filter_hash)
  1281. {
  1282. __ftrace_hash_rec_update(ops, filter_hash, 0);
  1283. }
  1284. static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
  1285. int filter_hash)
  1286. {
  1287. __ftrace_hash_rec_update(ops, filter_hash, 1);
  1288. }
  1289. static void print_ip_ins(const char *fmt, unsigned char *p)
  1290. {
  1291. int i;
  1292. printk(KERN_CONT "%s", fmt);
  1293. for (i = 0; i < MCOUNT_INSN_SIZE; i++)
  1294. printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
  1295. }
  1296. /**
  1297. * ftrace_bug - report and shutdown function tracer
  1298. * @failed: The failed type (EFAULT, EINVAL, EPERM)
  1299. * @ip: The address that failed
  1300. *
  1301. * The arch code that enables or disables the function tracing
  1302. * can call ftrace_bug() when it has detected a problem in
  1303. * modifying the code. @failed should be one of either:
  1304. * EFAULT - if the problem happens on reading the @ip address
  1305. * EINVAL - if what is read at @ip is not what was expected
  1306. * EPERM - if the problem happens on writting to the @ip address
  1307. */
  1308. void ftrace_bug(int failed, unsigned long ip)
  1309. {
  1310. switch (failed) {
  1311. case -EFAULT:
  1312. FTRACE_WARN_ON_ONCE(1);
  1313. pr_info("ftrace faulted on modifying ");
  1314. print_ip_sym(ip);
  1315. break;
  1316. case -EINVAL:
  1317. FTRACE_WARN_ON_ONCE(1);
  1318. pr_info("ftrace failed to modify ");
  1319. print_ip_sym(ip);
  1320. print_ip_ins(" actual: ", (unsigned char *)ip);
  1321. printk(KERN_CONT "\n");
  1322. break;
  1323. case -EPERM:
  1324. FTRACE_WARN_ON_ONCE(1);
  1325. pr_info("ftrace faulted on writing ");
  1326. print_ip_sym(ip);
  1327. break;
  1328. default:
  1329. FTRACE_WARN_ON_ONCE(1);
  1330. pr_info("ftrace faulted on unknown error ");
  1331. print_ip_sym(ip);
  1332. }
  1333. }
  1334. static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
  1335. {
  1336. unsigned long flag = 0UL;
  1337. /*
  1338. * If we are updating calls:
  1339. *
  1340. * If the record has a ref count, then we need to enable it
  1341. * because someone is using it.
  1342. *
  1343. * Otherwise we make sure its disabled.
  1344. *
  1345. * If we are disabling calls, then disable all records that
  1346. * are enabled.
  1347. */
  1348. if (enable && (rec->flags & ~FTRACE_FL_MASK))
  1349. flag = FTRACE_FL_ENABLED;
  1350. /* If the state of this record hasn't changed, then do nothing */
  1351. if ((rec->flags & FTRACE_FL_ENABLED) == flag)
  1352. return FTRACE_UPDATE_IGNORE;
  1353. if (flag) {
  1354. if (update)
  1355. rec->flags |= FTRACE_FL_ENABLED;
  1356. return FTRACE_UPDATE_MAKE_CALL;
  1357. }
  1358. if (update)
  1359. rec->flags &= ~FTRACE_FL_ENABLED;
  1360. return FTRACE_UPDATE_MAKE_NOP;
  1361. }
  1362. /**
  1363. * ftrace_update_record, set a record that now is tracing or not
  1364. * @rec: the record to update
  1365. * @enable: set to 1 if the record is tracing, zero to force disable
  1366. *
  1367. * The records that represent all functions that can be traced need
  1368. * to be updated when tracing has been enabled.
  1369. */
  1370. int ftrace_update_record(struct dyn_ftrace *rec, int enable)
  1371. {
  1372. return ftrace_check_record(rec, enable, 1);
  1373. }
  1374. /**
  1375. * ftrace_test_record, check if the record has been enabled or not
  1376. * @rec: the record to test
  1377. * @enable: set to 1 to check if enabled, 0 if it is disabled
  1378. *
  1379. * The arch code may need to test if a record is already set to
  1380. * tracing to determine how to modify the function code that it
  1381. * represents.
  1382. */
  1383. int ftrace_test_record(struct dyn_ftrace *rec, int enable)
  1384. {
  1385. return ftrace_check_record(rec, enable, 0);
  1386. }
  1387. static int
  1388. __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
  1389. {
  1390. unsigned long ftrace_addr;
  1391. int ret;
  1392. ftrace_addr = (unsigned long)FTRACE_ADDR;
  1393. ret = ftrace_update_record(rec, enable);
  1394. switch (ret) {
  1395. case FTRACE_UPDATE_IGNORE:
  1396. return 0;
  1397. case FTRACE_UPDATE_MAKE_CALL:
  1398. return ftrace_make_call(rec, ftrace_addr);
  1399. case FTRACE_UPDATE_MAKE_NOP:
  1400. return ftrace_make_nop(NULL, rec, ftrace_addr);
  1401. }
  1402. return -1; /* unknow ftrace bug */
  1403. }
  1404. void __weak ftrace_replace_code(int enable)
  1405. {
  1406. struct dyn_ftrace *rec;
  1407. struct ftrace_page *pg;
  1408. int failed;
  1409. if (unlikely(ftrace_disabled))
  1410. return;
  1411. do_for_each_ftrace_rec(pg, rec) {
  1412. failed = __ftrace_replace_code(rec, enable);
  1413. if (failed) {
  1414. ftrace_bug(failed, rec->ip);
  1415. /* Stop processing */
  1416. return;
  1417. }
  1418. } while_for_each_ftrace_rec();
  1419. }
  1420. struct ftrace_rec_iter {
  1421. struct ftrace_page *pg;
  1422. int index;
  1423. };
  1424. /**
  1425. * ftrace_rec_iter_start, start up iterating over traced functions
  1426. *
  1427. * Returns an iterator handle that is used to iterate over all
  1428. * the records that represent address locations where functions
  1429. * are traced.
  1430. *
  1431. * May return NULL if no records are available.
  1432. */
  1433. struct ftrace_rec_iter *ftrace_rec_iter_start(void)
  1434. {
  1435. /*
  1436. * We only use a single iterator.
  1437. * Protected by the ftrace_lock mutex.
  1438. */
  1439. static struct ftrace_rec_iter ftrace_rec_iter;
  1440. struct ftrace_rec_iter *iter = &ftrace_rec_iter;
  1441. iter->pg = ftrace_pages_start;
  1442. iter->index = 0;
  1443. /* Could have empty pages */
  1444. while (iter->pg && !iter->pg->index)
  1445. iter->pg = iter->pg->next;
  1446. if (!iter->pg)
  1447. return NULL;
  1448. return iter;
  1449. }
  1450. /**
  1451. * ftrace_rec_iter_next, get the next record to process.
  1452. * @iter: The handle to the iterator.
  1453. *
  1454. * Returns the next iterator after the given iterator @iter.
  1455. */
  1456. struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
  1457. {
  1458. iter->index++;
  1459. if (iter->index >= iter->pg->index) {
  1460. iter->pg = iter->pg->next;
  1461. iter->index = 0;
  1462. /* Could have empty pages */
  1463. while (iter->pg && !iter->pg->index)
  1464. iter->pg = iter->pg->next;
  1465. }
  1466. if (!iter->pg)
  1467. return NULL;
  1468. return iter;
  1469. }
  1470. /**
  1471. * ftrace_rec_iter_record, get the record at the iterator location
  1472. * @iter: The current iterator location
  1473. *
  1474. * Returns the record that the current @iter is at.
  1475. */
  1476. struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
  1477. {
  1478. return &iter->pg->records[iter->index];
  1479. }
  1480. static int
  1481. ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
  1482. {
  1483. unsigned long ip;
  1484. int ret;
  1485. ip = rec->ip;
  1486. if (unlikely(ftrace_disabled))
  1487. return 0;
  1488. ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
  1489. if (ret) {
  1490. ftrace_bug(ret, ip);
  1491. return 0;
  1492. }
  1493. return 1;
  1494. }
  1495. /*
  1496. * archs can override this function if they must do something
  1497. * before the modifying code is performed.
  1498. */
  1499. int __weak ftrace_arch_code_modify_prepare(void)
  1500. {
  1501. return 0;
  1502. }
  1503. /*
  1504. * archs can override this function if they must do something
  1505. * after the modifying code is performed.
  1506. */
  1507. int __weak ftrace_arch_code_modify_post_process(void)
  1508. {
  1509. return 0;
  1510. }
  1511. void ftrace_modify_all_code(int command)
  1512. {
  1513. if (command & FTRACE_UPDATE_CALLS)
  1514. ftrace_replace_code(1);
  1515. else if (command & FTRACE_DISABLE_CALLS)
  1516. ftrace_replace_code(0);
  1517. if (command & FTRACE_UPDATE_TRACE_FUNC)
  1518. ftrace_update_ftrace_func(ftrace_trace_function);
  1519. if (command & FTRACE_START_FUNC_RET)
  1520. ftrace_enable_ftrace_graph_caller();
  1521. else if (command & FTRACE_STOP_FUNC_RET)
  1522. ftrace_disable_ftrace_graph_caller();
  1523. }
  1524. static int __ftrace_modify_code(void *data)
  1525. {
  1526. int *command = data;
  1527. ftrace_modify_all_code(*command);
  1528. return 0;
  1529. }
  1530. /**
  1531. * ftrace_run_stop_machine, go back to the stop machine method
  1532. * @command: The command to tell ftrace what to do
  1533. *
  1534. * If an arch needs to fall back to the stop machine method, the
  1535. * it can call this function.
  1536. */
  1537. void ftrace_run_stop_machine(int command)
  1538. {
  1539. stop_machine(__ftrace_modify_code, &command, NULL);
  1540. }
  1541. /**
  1542. * arch_ftrace_update_code, modify the code to trace or not trace
  1543. * @command: The command that needs to be done
  1544. *
  1545. * Archs can override this function if it does not need to
  1546. * run stop_machine() to modify code.
  1547. */
  1548. void __weak arch_ftrace_update_code(int command)
  1549. {
  1550. ftrace_run_stop_machine(command);
  1551. }
  1552. static void ftrace_run_update_code(int command)
  1553. {
  1554. int ret;
  1555. ret = ftrace_arch_code_modify_prepare();
  1556. FTRACE_WARN_ON(ret);
  1557. if (ret)
  1558. return;
  1559. /*
  1560. * Do not call function tracer while we update the code.
  1561. * We are in stop machine.
  1562. */
  1563. function_trace_stop++;
  1564. /*
  1565. * By default we use stop_machine() to modify the code.
  1566. * But archs can do what ever they want as long as it
  1567. * is safe. The stop_machine() is the safest, but also
  1568. * produces the most overhead.
  1569. */
  1570. arch_ftrace_update_code(command);
  1571. #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  1572. /*
  1573. * For archs that call ftrace_test_stop_func(), we must
  1574. * wait till after we update all the function callers
  1575. * before we update the callback. This keeps different
  1576. * ops that record different functions from corrupting
  1577. * each other.
  1578. */
  1579. __ftrace_trace_function = __ftrace_trace_function_delay;
  1580. #endif
  1581. function_trace_stop--;
  1582. ret = ftrace_arch_code_modify_post_process();
  1583. FTRACE_WARN_ON(ret);
  1584. }
  1585. static ftrace_func_t saved_ftrace_func;
  1586. static int ftrace_start_up;
  1587. static int global_start_up;
  1588. static void ftrace_startup_enable(int command)
  1589. {
  1590. if (saved_ftrace_func != ftrace_trace_function) {
  1591. saved_ftrace_func = ftrace_trace_function;
  1592. command |= FTRACE_UPDATE_TRACE_FUNC;
  1593. }
  1594. if (!command || !ftrace_enabled)
  1595. return;
  1596. ftrace_run_update_code(command);
  1597. }
  1598. static int ftrace_startup(struct ftrace_ops *ops, int command)
  1599. {
  1600. bool hash_enable = true;
  1601. if (unlikely(ftrace_disabled))
  1602. return -ENODEV;
  1603. ftrace_start_up++;
  1604. command |= FTRACE_UPDATE_CALLS;
  1605. /* ops marked global share the filter hashes */
  1606. if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
  1607. ops = &global_ops;
  1608. /* Don't update hash if global is already set */
  1609. if (global_start_up)
  1610. hash_enable = false;
  1611. global_start_up++;
  1612. }
  1613. ops->flags |= FTRACE_OPS_FL_ENABLED;
  1614. if (hash_enable)
  1615. ftrace_hash_rec_enable(ops, 1);
  1616. ftrace_startup_enable(command);
  1617. return 0;
  1618. }
  1619. static void ftrace_shutdown(struct ftrace_ops *ops, int command)
  1620. {
  1621. bool hash_disable = true;
  1622. if (unlikely(ftrace_disabled))
  1623. return;
  1624. ftrace_start_up--;
  1625. /*
  1626. * Just warn in case of unbalance, no need to kill ftrace, it's not
  1627. * critical but the ftrace_call callers may be never nopped again after
  1628. * further ftrace uses.
  1629. */
  1630. WARN_ON_ONCE(ftrace_start_up < 0);
  1631. if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
  1632. ops = &global_ops;
  1633. global_start_up--;
  1634. WARN_ON_ONCE(global_start_up < 0);
  1635. /* Don't update hash if global still has users */
  1636. if (global_start_up) {
  1637. WARN_ON_ONCE(!ftrace_start_up);
  1638. hash_disable = false;
  1639. }
  1640. }
  1641. if (hash_disable)
  1642. ftrace_hash_rec_disable(ops, 1);
  1643. if (ops != &global_ops || !global_start_up)
  1644. ops->flags &= ~FTRACE_OPS_FL_ENABLED;
  1645. command |= FTRACE_UPDATE_CALLS;
  1646. if (saved_ftrace_func != ftrace_trace_function) {
  1647. saved_ftrace_func = ftrace_trace_function;
  1648. command |= FTRACE_UPDATE_TRACE_FUNC;
  1649. }
  1650. if (!command || !ftrace_enabled)
  1651. return;
  1652. ftrace_run_update_code(command);
  1653. }
  1654. static void ftrace_startup_sysctl(void)
  1655. {
  1656. if (unlikely(ftrace_disabled))
  1657. return;
  1658. /* Force update next time */
  1659. saved_ftrace_func = NULL;
  1660. /* ftrace_start_up is true if we want ftrace running */
  1661. if (ftrace_start_up)
  1662. ftrace_run_update_code(FTRACE_UPDATE_CALLS);
  1663. }
  1664. static void ftrace_shutdown_sysctl(void)
  1665. {
  1666. if (unlikely(ftrace_disabled))
  1667. return;
  1668. /* ftrace_start_up is true if ftrace is running */
  1669. if (ftrace_start_up)
  1670. ftrace_run_update_code(FTRACE_DISABLE_CALLS);
  1671. }
  1672. static cycle_t ftrace_update_time;
  1673. static unsigned long ftrace_update_cnt;
  1674. unsigned long ftrace_update_tot_cnt;
  1675. static int ops_traces_mod(struct ftrace_ops *ops)
  1676. {
  1677. struct ftrace_hash *hash;
  1678. hash = ops->filter_hash;
  1679. return ftrace_hash_empty(hash);
  1680. }
  1681. static int ftrace_update_code(struct module *mod)
  1682. {
  1683. struct ftrace_page *pg;
  1684. struct dyn_ftrace *p;
  1685. cycle_t start, stop;
  1686. unsigned long ref = 0;
  1687. int i;
  1688. /*
  1689. * When adding a module, we need to check if tracers are
  1690. * currently enabled and if they are set to trace all functions.
  1691. * If they are, we need to enable the module functions as well
  1692. * as update the reference counts for those function records.
  1693. */
  1694. if (mod) {
  1695. struct ftrace_ops *ops;
  1696. for (ops = ftrace_ops_list;
  1697. ops != &ftrace_list_end; ops = ops->next) {
  1698. if (ops->flags & FTRACE_OPS_FL_ENABLED &&
  1699. ops_traces_mod(ops))
  1700. ref++;
  1701. }
  1702. }
  1703. start = ftrace_now(raw_smp_processor_id());
  1704. ftrace_update_cnt = 0;
  1705. for (pg = ftrace_new_pgs; pg; pg = pg->next) {
  1706. for (i = 0; i < pg->index; i++) {
  1707. /* If something went wrong, bail without enabling anything */
  1708. if (unlikely(ftrace_disabled))
  1709. return -1;
  1710. p = &pg->records[i];
  1711. p->flags = ref;
  1712. /*
  1713. * Do the initial record conversion from mcount jump
  1714. * to the NOP instructions.
  1715. */
  1716. if (!ftrace_code_disable(mod, p))
  1717. break;
  1718. ftrace_update_cnt++;
  1719. /*
  1720. * If the tracing is enabled, go ahead and enable the record.
  1721. *
  1722. * The reason not to enable the record immediatelly is the
  1723. * inherent check of ftrace_make_nop/ftrace_make_call for
  1724. * correct previous instructions. Making first the NOP
  1725. * conversion puts the module to the correct state, thus
  1726. * passing the ftrace_make_call check.
  1727. */
  1728. if (ftrace_start_up && ref) {
  1729. int failed = __ftrace_replace_code(p, 1);
  1730. if (failed)
  1731. ftrace_bug(failed, p->ip);
  1732. }
  1733. }
  1734. }
  1735. ftrace_new_pgs = NULL;
  1736. stop = ftrace_now(raw_smp_processor_id());
  1737. ftrace_update_time = stop - start;
  1738. ftrace_update_tot_cnt += ftrace_update_cnt;
  1739. return 0;
  1740. }
  1741. static int ftrace_allocate_records(struct ftrace_page *pg, int count)
  1742. {
  1743. int order;
  1744. int cnt;
  1745. if (WARN_ON(!count))
  1746. return -EINVAL;
  1747. order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
  1748. /*
  1749. * We want to fill as much as possible. No more than a page
  1750. * may be empty.
  1751. */
  1752. while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
  1753. order--;
  1754. again:
  1755. pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
  1756. if (!pg->records) {
  1757. /* if we can't allocate this size, try something smaller */
  1758. if (!order)
  1759. return -ENOMEM;
  1760. order >>= 1;
  1761. goto again;
  1762. }
  1763. cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
  1764. pg->size = cnt;
  1765. if (cnt > count)
  1766. cnt = count;
  1767. return cnt;
  1768. }
  1769. static struct ftrace_page *
  1770. ftrace_allocate_pages(unsigned long num_to_init)
  1771. {
  1772. struct ftrace_page *start_pg;
  1773. struct ftrace_page *pg;
  1774. int order;
  1775. int cnt;
  1776. if (!num_to_init)
  1777. return 0;
  1778. start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
  1779. if (!pg)
  1780. return NULL;
  1781. /*
  1782. * Try to allocate as much as possible in one continues
  1783. * location that fills in all of the space. We want to
  1784. * waste as little space as possible.
  1785. */
  1786. for (;;) {
  1787. cnt = ftrace_allocate_records(pg, num_to_init);
  1788. if (cnt < 0)
  1789. goto free_pages;
  1790. num_to_init -= cnt;
  1791. if (!num_to_init)
  1792. break;
  1793. pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
  1794. if (!pg->next)
  1795. goto free_pages;
  1796. pg = pg->next;
  1797. }
  1798. return start_pg;
  1799. free_pages:
  1800. while (start_pg) {
  1801. order = get_count_order(pg->size / ENTRIES_PER_PAGE);
  1802. free_pages((unsigned long)pg->records, order);
  1803. start_pg = pg->next;
  1804. kfree(pg);
  1805. pg = start_pg;
  1806. }
  1807. pr_info("ftrace: FAILED to allocate memory for functions\n");
  1808. return NULL;
  1809. }
  1810. static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
  1811. {
  1812. int cnt;
  1813. if (!num_to_init) {
  1814. pr_info("ftrace: No functions to be traced?\n");
  1815. return -1;
  1816. }
  1817. cnt = num_to_init / ENTRIES_PER_PAGE;
  1818. pr_info("ftrace: allocating %ld entries in %d pages\n",
  1819. num_to_init, cnt + 1);
  1820. return 0;
  1821. }
  1822. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  1823. struct ftrace_iterator {
  1824. loff_t pos;
  1825. loff_t func_pos;
  1826. struct ftrace_page *pg;
  1827. struct dyn_ftrace *func;
  1828. struct ftrace_func_probe *probe;
  1829. struct trace_parser parser;
  1830. struct ftrace_hash *hash;
  1831. struct ftrace_ops *ops;
  1832. int hidx;
  1833. int idx;
  1834. unsigned flags;
  1835. };
  1836. static void *
  1837. t_hash_next(struct seq_file *m, loff_t *pos)
  1838. {
  1839. struct ftrace_iterator *iter = m->private;
  1840. struct hlist_node *hnd = NULL;
  1841. struct hlist_head *hhd;
  1842. (*pos)++;
  1843. iter->pos = *pos;
  1844. if (iter->probe)
  1845. hnd = &iter->probe->node;
  1846. retry:
  1847. if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
  1848. return NULL;
  1849. hhd = &ftrace_func_hash[iter->hidx];
  1850. if (hlist_empty(hhd)) {
  1851. iter->hidx++;
  1852. hnd = NULL;
  1853. goto retry;
  1854. }
  1855. if (!hnd)
  1856. hnd = hhd->first;
  1857. else {
  1858. hnd = hnd->next;
  1859. if (!hnd) {
  1860. iter->hidx++;
  1861. goto retry;
  1862. }
  1863. }
  1864. if (WARN_ON_ONCE(!hnd))
  1865. return NULL;
  1866. iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
  1867. return iter;
  1868. }
  1869. static void *t_hash_start(struct seq_file *m, loff_t *pos)
  1870. {
  1871. struct ftrace_iterator *iter = m->private;
  1872. void *p = NULL;
  1873. loff_t l;
  1874. if (!(iter->flags & FTRACE_ITER_DO_HASH))
  1875. return NULL;
  1876. if (iter->func_pos > *pos)
  1877. return NULL;
  1878. iter->hidx = 0;
  1879. for (l = 0; l <= (*pos - iter->func_pos); ) {
  1880. p = t_hash_next(m, &l);
  1881. if (!p)
  1882. break;
  1883. }
  1884. if (!p)
  1885. return NULL;
  1886. /* Only set this if we have an item */
  1887. iter->flags |= FTRACE_ITER_HASH;
  1888. return iter;
  1889. }
  1890. static int
  1891. t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
  1892. {
  1893. struct ftrace_func_probe *rec;
  1894. rec = iter->probe;
  1895. if (WARN_ON_ONCE(!rec))
  1896. return -EIO;
  1897. if (rec->ops->print)
  1898. return rec->ops->print(m, rec->ip, rec->ops, rec->data);
  1899. seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
  1900. if (rec->data)
  1901. seq_printf(m, ":%p", rec->data);
  1902. seq_putc(m, '\n');
  1903. return 0;
  1904. }
  1905. static void *
  1906. t_next(struct seq_file *m, void *v, loff_t *pos)
  1907. {
  1908. struct ftrace_iterator *iter = m->private;
  1909. struct ftrace_ops *ops = iter->ops;
  1910. struct dyn_ftrace *rec = NULL;
  1911. if (unlikely(ftrace_disabled))
  1912. return NULL;
  1913. if (iter->flags & FTRACE_ITER_HASH)
  1914. return t_hash_next(m, pos);
  1915. (*pos)++;
  1916. iter->pos = iter->func_pos = *pos;
  1917. if (iter->flags & FTRACE_ITER_PRINTALL)
  1918. return t_hash_start(m, pos);
  1919. retry:
  1920. if (iter->idx >= iter->pg->index) {
  1921. if (iter->pg->next) {
  1922. iter->pg = iter->pg->next;
  1923. iter->idx = 0;
  1924. goto retry;
  1925. }
  1926. } else {
  1927. rec = &iter->pg->records[iter->idx++];
  1928. if (((iter->flags & FTRACE_ITER_FILTER) &&
  1929. !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
  1930. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  1931. !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
  1932. ((iter->flags & FTRACE_ITER_ENABLED) &&
  1933. !(rec->flags & ~FTRACE_FL_MASK))) {
  1934. rec = NULL;
  1935. goto retry;
  1936. }
  1937. }
  1938. if (!rec)
  1939. return t_hash_start(m, pos);
  1940. iter->func = rec;
  1941. return iter;
  1942. }
  1943. static void reset_iter_read(struct ftrace_iterator *iter)
  1944. {
  1945. iter->pos = 0;
  1946. iter->func_pos = 0;
  1947. iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
  1948. }
  1949. static void *t_start(struct seq_file *m, loff_t *pos)
  1950. {
  1951. struct ftrace_iterator *iter = m->private;
  1952. struct ftrace_ops *ops = iter->ops;
  1953. void *p = NULL;
  1954. loff_t l;
  1955. mutex_lock(&ftrace_lock);
  1956. if (unlikely(ftrace_disabled))
  1957. return NULL;
  1958. /*
  1959. * If an lseek was done, then reset and start from beginning.
  1960. */
  1961. if (*pos < iter->pos)
  1962. reset_iter_read(iter);
  1963. /*
  1964. * For set_ftrace_filter reading, if we have the filter
  1965. * off, we can short cut and just print out that all
  1966. * functions are enabled.
  1967. */
  1968. if (iter->flags & FTRACE_ITER_FILTER &&
  1969. ftrace_hash_empty(ops->filter_hash)) {
  1970. if (*pos > 0)
  1971. return t_hash_start(m, pos);
  1972. iter->flags |= FTRACE_ITER_PRINTALL;
  1973. /* reset in case of seek/pread */
  1974. iter->flags &= ~FTRACE_ITER_HASH;
  1975. return iter;
  1976. }
  1977. if (iter->flags & FTRACE_ITER_HASH)
  1978. return t_hash_start(m, pos);
  1979. /*
  1980. * Unfortunately, we need to restart at ftrace_pages_start
  1981. * every time we let go of the ftrace_mutex. This is because
  1982. * those pointers can change without the lock.
  1983. */
  1984. iter->pg = ftrace_pages_start;
  1985. iter->idx = 0;
  1986. for (l = 0; l <= *pos; ) {
  1987. p = t_next(m, p, &l);
  1988. if (!p)
  1989. break;
  1990. }
  1991. if (!p)
  1992. return t_hash_start(m, pos);
  1993. return iter;
  1994. }
  1995. static void t_stop(struct seq_file *m, void *p)
  1996. {
  1997. mutex_unlock(&ftrace_lock);
  1998. }
  1999. static int t_show(struct seq_file *m, void *v)
  2000. {
  2001. struct ftrace_iterator *iter = m->private;
  2002. struct dyn_ftrace *rec;
  2003. if (iter->flags & FTRACE_ITER_HASH)
  2004. return t_hash_show(m, iter);
  2005. if (iter->flags & FTRACE_ITER_PRINTALL) {
  2006. seq_printf(m, "#### all functions enabled ####\n");
  2007. return 0;
  2008. }
  2009. rec = iter->func;
  2010. if (!rec)
  2011. return 0;
  2012. seq_printf(m, "%ps", (void *)rec->ip);
  2013. if (iter->flags & FTRACE_ITER_ENABLED)
  2014. seq_printf(m, " (%ld)",
  2015. rec->flags & ~FTRACE_FL_MASK);
  2016. seq_printf(m, "\n");
  2017. return 0;
  2018. }
  2019. static const struct seq_operations show_ftrace_seq_ops = {
  2020. .start = t_start,
  2021. .next = t_next,
  2022. .stop = t_stop,
  2023. .show = t_show,
  2024. };
  2025. static int
  2026. ftrace_avail_open(struct inode *inode, struct file *file)
  2027. {
  2028. struct ftrace_iterator *iter;
  2029. if (unlikely(ftrace_disabled))
  2030. return -ENODEV;
  2031. iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
  2032. if (iter) {
  2033. iter->pg = ftrace_pages_start;
  2034. iter->ops = &global_ops;
  2035. }
  2036. return iter ? 0 : -ENOMEM;
  2037. }
  2038. static int
  2039. ftrace_enabled_open(struct inode *inode, struct file *file)
  2040. {
  2041. struct ftrace_iterator *iter;
  2042. if (unlikely(ftrace_disabled))
  2043. return -ENODEV;
  2044. iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
  2045. if (iter) {
  2046. iter->pg = ftrace_pages_start;
  2047. iter->flags = FTRACE_ITER_ENABLED;
  2048. iter->ops = &global_ops;
  2049. }
  2050. return iter ? 0 : -ENOMEM;
  2051. }
  2052. static void ftrace_filter_reset(struct ftrace_hash *hash)
  2053. {
  2054. mutex_lock(&ftrace_lock);
  2055. ftrace_hash_clear(hash);
  2056. mutex_unlock(&ftrace_lock);
  2057. }
  2058. /**
  2059. * ftrace_regex_open - initialize function tracer filter files
  2060. * @ops: The ftrace_ops that hold the hash filters
  2061. * @flag: The type of filter to process
  2062. * @inode: The inode, usually passed in to your open routine
  2063. * @file: The file, usually passed in to your open routine
  2064. *
  2065. * ftrace_regex_open() initializes the filter files for the
  2066. * @ops. Depending on @flag it may process the filter hash or
  2067. * the notrace hash of @ops. With this called from the open
  2068. * routine, you can use ftrace_filter_write() for the write
  2069. * routine if @flag has FTRACE_ITER_FILTER set, or
  2070. * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
  2071. * ftrace_regex_lseek() should be used as the lseek routine, and
  2072. * release must call ftrace_regex_release().
  2073. */
  2074. int
  2075. ftrace_regex_open(struct ftrace_ops *ops, int flag,
  2076. struct inode *inode, struct file *file)
  2077. {
  2078. struct ftrace_iterator *iter;
  2079. struct ftrace_hash *hash;
  2080. int ret = 0;
  2081. if (unlikely(ftrace_disabled))
  2082. return -ENODEV;
  2083. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  2084. if (!iter)
  2085. return -ENOMEM;
  2086. if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
  2087. kfree(iter);
  2088. return -ENOMEM;
  2089. }
  2090. if (flag & FTRACE_ITER_NOTRACE)
  2091. hash = ops->notrace_hash;
  2092. else
  2093. hash = ops->filter_hash;
  2094. iter->ops = ops;
  2095. iter->flags = flag;
  2096. if (file->f_mode & FMODE_WRITE) {
  2097. mutex_lock(&ftrace_lock);
  2098. iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
  2099. mutex_unlock(&ftrace_lock);
  2100. if (!iter->hash) {
  2101. trace_parser_put(&iter->parser);
  2102. kfree(iter);
  2103. return -ENOMEM;
  2104. }
  2105. }
  2106. mutex_lock(&ftrace_regex_lock);
  2107. if ((file->f_mode & FMODE_WRITE) &&
  2108. (file->f_flags & O_TRUNC))
  2109. ftrace_filter_reset(iter->hash);
  2110. if (file->f_mode & FMODE_READ) {
  2111. iter->pg = ftrace_pages_start;
  2112. ret = seq_open(file, &show_ftrace_seq_ops);
  2113. if (!ret) {
  2114. struct seq_file *m = file->private_data;
  2115. m->private = iter;
  2116. } else {
  2117. /* Failed */
  2118. free_ftrace_hash(iter->hash);
  2119. trace_parser_put(&iter->parser);
  2120. kfree(iter);
  2121. }
  2122. } else
  2123. file->private_data = iter;
  2124. mutex_unlock(&ftrace_regex_lock);
  2125. return ret;
  2126. }
  2127. static int
  2128. ftrace_filter_open(struct inode *inode, struct file *file)
  2129. {
  2130. return ftrace_regex_open(&global_ops,
  2131. FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
  2132. inode, file);
  2133. }
  2134. static int
  2135. ftrace_notrace_open(struct inode *inode, struct file *file)
  2136. {
  2137. return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
  2138. inode, file);
  2139. }
  2140. loff_t
  2141. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  2142. {
  2143. loff_t ret;
  2144. if (file->f_mode & FMODE_READ)
  2145. ret = seq_lseek(file, offset, origin);
  2146. else
  2147. file->f_pos = ret = 1;
  2148. return ret;
  2149. }
  2150. static int ftrace_match(char *str, char *regex, int len, int type)
  2151. {
  2152. int matched = 0;
  2153. int slen;
  2154. switch (type) {
  2155. case MATCH_FULL:
  2156. if (strcmp(str, regex) == 0)
  2157. matched = 1;
  2158. break;
  2159. case MATCH_FRONT_ONLY:
  2160. if (strncmp(str, regex, len) == 0)
  2161. matched = 1;
  2162. break;
  2163. case MATCH_MIDDLE_ONLY:
  2164. if (strstr(str, regex))
  2165. matched = 1;
  2166. break;
  2167. case MATCH_END_ONLY:
  2168. slen = strlen(str);
  2169. if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
  2170. matched = 1;
  2171. break;
  2172. }
  2173. return matched;
  2174. }
  2175. static int
  2176. enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
  2177. {
  2178. struct ftrace_func_entry *entry;
  2179. int ret = 0;
  2180. entry = ftrace_lookup_ip(hash, rec->ip);
  2181. if (not) {
  2182. /* Do nothing if it doesn't exist */
  2183. if (!entry)
  2184. return 0;
  2185. free_hash_entry(hash, entry);
  2186. } else {
  2187. /* Do nothing if it exists */
  2188. if (entry)
  2189. return 0;
  2190. ret = add_hash_entry(hash, rec->ip);
  2191. }
  2192. return ret;
  2193. }
  2194. static int
  2195. ftrace_match_record(struct dyn_ftrace *rec, char *mod,
  2196. char *regex, int len, int type)
  2197. {
  2198. char str[KSYM_SYMBOL_LEN];
  2199. char *modname;
  2200. kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
  2201. if (mod) {
  2202. /* module lookup requires matching the module */
  2203. if (!modname || strcmp(modname, mod))
  2204. return 0;
  2205. /* blank search means to match all funcs in the mod */
  2206. if (!len)
  2207. return 1;
  2208. }
  2209. return ftrace_match(str, regex, len, type);
  2210. }
  2211. static int
  2212. match_records(struct ftrace_hash *hash, char *buff,
  2213. int len, char *mod, int not)
  2214. {
  2215. unsigned search_len = 0;
  2216. struct ftrace_page *pg;
  2217. struct dyn_ftrace *rec;
  2218. int type = MATCH_FULL;
  2219. char *search = buff;
  2220. int found = 0;
  2221. int ret;
  2222. if (len) {
  2223. type = filter_parse_regex(buff, len, &search, &not);
  2224. search_len = strlen(search);
  2225. }
  2226. mutex_lock(&ftrace_lock);
  2227. if (unlikely(ftrace_disabled))
  2228. goto out_unlock;
  2229. do_for_each_ftrace_rec(pg, rec) {
  2230. if (ftrace_match_record(rec, mod, search, search_len, type)) {
  2231. ret = enter_record(hash, rec, not);
  2232. if (ret < 0) {
  2233. found = ret;
  2234. goto out_unlock;
  2235. }
  2236. found = 1;
  2237. }
  2238. } while_for_each_ftrace_rec();
  2239. out_unlock:
  2240. mutex_unlock(&ftrace_lock);
  2241. return found;
  2242. }
  2243. static int
  2244. ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
  2245. {
  2246. return match_records(hash, buff, len, NULL, 0);
  2247. }
  2248. static int
  2249. ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
  2250. {
  2251. int not = 0;
  2252. /* blank or '*' mean the same */
  2253. if (strcmp(buff, "*") == 0)
  2254. buff[0] = 0;
  2255. /* handle the case of 'dont filter this module' */
  2256. if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
  2257. buff[0] = 0;
  2258. not = 1;
  2259. }
  2260. return match_records(hash, buff, strlen(buff), mod, not);
  2261. }
  2262. /*
  2263. * We register the module command as a template to show others how
  2264. * to register the a command as well.
  2265. */
  2266. static int
  2267. ftrace_mod_callback(struct ftrace_hash *hash,
  2268. char *func, char *cmd, char *param, int enable)
  2269. {
  2270. char *mod;
  2271. int ret = -EINVAL;
  2272. /*
  2273. * cmd == 'mod' because we only registered this func
  2274. * for the 'mod' ftrace_func_command.
  2275. * But if you register one func with multiple commands,
  2276. * you can tell which command was used by the cmd
  2277. * parameter.
  2278. */
  2279. /* we must have a module name */
  2280. if (!param)
  2281. return ret;
  2282. mod = strsep(&param, ":");
  2283. if (!strlen(mod))
  2284. return ret;
  2285. ret = ftrace_match_module_records(hash, func, mod);
  2286. if (!ret)
  2287. ret = -EINVAL;
  2288. if (ret < 0)
  2289. return ret;
  2290. return 0;
  2291. }
  2292. static struct ftrace_func_command ftrace_mod_cmd = {
  2293. .name = "mod",
  2294. .func = ftrace_mod_callback,
  2295. };
  2296. static int __init ftrace_mod_cmd_init(void)
  2297. {
  2298. return register_ftrace_command(&ftrace_mod_cmd);
  2299. }
  2300. device_initcall(ftrace_mod_cmd_init);
  2301. static void
  2302. function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
  2303. {
  2304. struct ftrace_func_probe *entry;
  2305. struct hlist_head *hhd;
  2306. struct hlist_node *n;
  2307. unsigned long key;
  2308. key = hash_long(ip, FTRACE_HASH_BITS);
  2309. hhd = &ftrace_func_hash[key];
  2310. if (hlist_empty(hhd))
  2311. return;
  2312. /*
  2313. * Disable preemption for these calls to prevent a RCU grace
  2314. * period. This syncs the hash iteration and freeing of items
  2315. * on the hash. rcu_read_lock is too dangerous here.
  2316. */
  2317. preempt_disable_notrace();
  2318. hlist_for_each_entry_rcu(entry, n, hhd, node) {
  2319. if (entry->ip == ip)
  2320. entry->ops->func(ip, parent_ip, &entry->data);
  2321. }
  2322. preempt_enable_notrace();
  2323. }
  2324. static struct ftrace_ops trace_probe_ops __read_mostly =
  2325. {
  2326. .func = function_trace_probe_call,
  2327. };
  2328. static int ftrace_probe_registered;
  2329. static void __enable_ftrace_function_probe(void)
  2330. {
  2331. int ret;
  2332. int i;
  2333. if (ftrace_probe_registered)
  2334. return;
  2335. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  2336. struct hlist_head *hhd = &ftrace_func_hash[i];
  2337. if (hhd->first)
  2338. break;
  2339. }
  2340. /* Nothing registered? */
  2341. if (i == FTRACE_FUNC_HASHSIZE)
  2342. return;
  2343. ret = __register_ftrace_function(&trace_probe_ops);
  2344. if (!ret)
  2345. ret = ftrace_startup(&trace_probe_ops, 0);
  2346. ftrace_probe_registered = 1;
  2347. }
  2348. static void __disable_ftrace_function_probe(void)
  2349. {
  2350. int ret;
  2351. int i;
  2352. if (!ftrace_probe_registered)
  2353. return;
  2354. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  2355. struct hlist_head *hhd = &ftrace_func_hash[i];
  2356. if (hhd->first)
  2357. return;
  2358. }
  2359. /* no more funcs left */
  2360. ret = __unregister_ftrace_function(&trace_probe_ops);
  2361. if (!ret)
  2362. ftrace_shutdown(&trace_probe_ops, 0);
  2363. ftrace_probe_registered = 0;
  2364. }
  2365. static void ftrace_free_entry_rcu(struct rcu_head *rhp)
  2366. {
  2367. struct ftrace_func_probe *entry =
  2368. container_of(rhp, struct ftrace_func_probe, rcu);
  2369. if (entry->ops->free)
  2370. entry->ops->free(&entry->data);
  2371. kfree(entry);
  2372. }
  2373. int
  2374. register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  2375. void *data)
  2376. {
  2377. struct ftrace_func_probe *entry;
  2378. struct ftrace_page *pg;
  2379. struct dyn_ftrace *rec;
  2380. int type, len, not;
  2381. unsigned long key;
  2382. int count = 0;
  2383. char *search;
  2384. type = filter_parse_regex(glob, strlen(glob), &search, &not);
  2385. len = strlen(search);
  2386. /* we do not support '!' for function probes */
  2387. if (WARN_ON(not))
  2388. return -EINVAL;
  2389. mutex_lock(&ftrace_lock);
  2390. if (unlikely(ftrace_disabled))
  2391. goto out_unlock;
  2392. do_for_each_ftrace_rec(pg, rec) {
  2393. if (!ftrace_match_record(rec, NULL, search, len, type))
  2394. continue;
  2395. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  2396. if (!entry) {
  2397. /* If we did not process any, then return error */
  2398. if (!count)
  2399. count = -ENOMEM;
  2400. goto out_unlock;
  2401. }
  2402. count++;
  2403. entry->data = data;
  2404. /*
  2405. * The caller might want to do something special
  2406. * for each function we find. We call the callback
  2407. * to give the caller an opportunity to do so.
  2408. */
  2409. if (ops->callback) {
  2410. if (ops->callback(rec->ip, &entry->data) < 0) {
  2411. /* caller does not like this func */
  2412. kfree(entry);
  2413. continue;
  2414. }
  2415. }
  2416. entry->ops = ops;
  2417. entry->ip = rec->ip;
  2418. key = hash_long(entry->ip, FTRACE_HASH_BITS);
  2419. hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
  2420. } while_for_each_ftrace_rec();
  2421. __enable_ftrace_function_probe();
  2422. out_unlock:
  2423. mutex_unlock(&ftrace_lock);
  2424. return count;
  2425. }
  2426. enum {
  2427. PROBE_TEST_FUNC = 1,
  2428. PROBE_TEST_DATA = 2
  2429. };
  2430. static void
  2431. __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  2432. void *data, int flags)
  2433. {
  2434. struct ftrace_func_probe *entry;
  2435. struct hlist_node *n, *tmp;
  2436. char str[KSYM_SYMBOL_LEN];
  2437. int type = MATCH_FULL;
  2438. int i, len = 0;
  2439. char *search;
  2440. if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
  2441. glob = NULL;
  2442. else if (glob) {
  2443. int not;
  2444. type = filter_parse_regex(glob, strlen(glob), &search, &not);
  2445. len = strlen(search);
  2446. /* we do not support '!' for function probes */
  2447. if (WARN_ON(not))
  2448. return;
  2449. }
  2450. mutex_lock(&ftrace_lock);
  2451. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  2452. struct hlist_head *hhd = &ftrace_func_hash[i];
  2453. hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
  2454. /* break up if statements for readability */
  2455. if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
  2456. continue;
  2457. if ((flags & PROBE_TEST_DATA) && entry->data != data)
  2458. continue;
  2459. /* do this last, since it is the most expensive */
  2460. if (glob) {
  2461. kallsyms_lookup(entry->ip, NULL, NULL,
  2462. NULL, str);
  2463. if (!ftrace_match(str, glob, len, type))
  2464. continue;
  2465. }
  2466. hlist_del(&entry->node);
  2467. call_rcu(&entry->rcu, ftrace_free_entry_rcu);
  2468. }
  2469. }
  2470. __disable_ftrace_function_probe();
  2471. mutex_unlock(&ftrace_lock);
  2472. }
  2473. void
  2474. unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  2475. void *data)
  2476. {
  2477. __unregister_ftrace_function_probe(glob, ops, data,
  2478. PROBE_TEST_FUNC | PROBE_TEST_DATA);
  2479. }
  2480. void
  2481. unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
  2482. {
  2483. __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
  2484. }
  2485. void unregister_ftrace_function_probe_all(char *glob)
  2486. {
  2487. __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
  2488. }
  2489. static LIST_HEAD(ftrace_commands);
  2490. static DEFINE_MUTEX(ftrace_cmd_mutex);
  2491. int register_ftrace_command(struct ftrace_func_command *cmd)
  2492. {
  2493. struct ftrace_func_command *p;
  2494. int ret = 0;
  2495. mutex_lock(&ftrace_cmd_mutex);
  2496. list_for_each_entry(p, &ftrace_commands, list) {
  2497. if (strcmp(cmd->name, p->name) == 0) {
  2498. ret = -EBUSY;
  2499. goto out_unlock;
  2500. }
  2501. }
  2502. list_add(&cmd->list, &ftrace_commands);
  2503. out_unlock:
  2504. mutex_unlock(&ftrace_cmd_mutex);
  2505. return ret;
  2506. }
  2507. int unregister_ftrace_command(struct ftrace_func_command *cmd)
  2508. {
  2509. struct ftrace_func_command *p, *n;
  2510. int ret = -ENODEV;
  2511. mutex_lock(&ftrace_cmd_mutex);
  2512. list_for_each_entry_safe(p, n, &ftrace_commands, list) {
  2513. if (strcmp(cmd->name, p->name) == 0) {
  2514. ret = 0;
  2515. list_del_init(&p->list);
  2516. goto out_unlock;
  2517. }
  2518. }
  2519. out_unlock:
  2520. mutex_unlock(&ftrace_cmd_mutex);
  2521. return ret;
  2522. }
  2523. static int ftrace_process_regex(struct ftrace_hash *hash,
  2524. char *buff, int len, int enable)
  2525. {
  2526. char *func, *command, *next = buff;
  2527. struct ftrace_func_command *p;
  2528. int ret = -EINVAL;
  2529. func = strsep(&next, ":");
  2530. if (!next) {
  2531. ret = ftrace_match_records(hash, func, len);
  2532. if (!ret)
  2533. ret = -EINVAL;
  2534. if (ret < 0)
  2535. return ret;
  2536. return 0;
  2537. }
  2538. /* command found */
  2539. command = strsep(&next, ":");
  2540. mutex_lock(&ftrace_cmd_mutex);
  2541. list_for_each_entry(p, &ftrace_commands, list) {
  2542. if (strcmp(p->name, command) == 0) {
  2543. ret = p->func(hash, func, command, next, enable);
  2544. goto out_unlock;
  2545. }
  2546. }
  2547. out_unlock:
  2548. mutex_unlock(&ftrace_cmd_mutex);
  2549. return ret;
  2550. }
  2551. static ssize_t
  2552. ftrace_regex_write(struct file *file, const char __user *ubuf,
  2553. size_t cnt, loff_t *ppos, int enable)
  2554. {
  2555. struct ftrace_iterator *iter;
  2556. struct trace_parser *parser;
  2557. ssize_t ret, read;
  2558. if (!cnt)
  2559. return 0;
  2560. mutex_lock(&ftrace_regex_lock);
  2561. ret = -ENODEV;
  2562. if (unlikely(ftrace_disabled))
  2563. goto out_unlock;
  2564. if (file->f_mode & FMODE_READ) {
  2565. struct seq_file *m = file->private_data;
  2566. iter = m->private;
  2567. } else
  2568. iter = file->private_data;
  2569. parser = &iter->parser;
  2570. read = trace_get_user(parser, ubuf, cnt, ppos);
  2571. if (read >= 0 && trace_parser_loaded(parser) &&
  2572. !trace_parser_cont(parser)) {
  2573. ret = ftrace_process_regex(iter->hash, parser->buffer,
  2574. parser->idx, enable);
  2575. trace_parser_clear(parser);
  2576. if (ret)
  2577. goto out_unlock;
  2578. }
  2579. ret = read;
  2580. out_unlock:
  2581. mutex_unlock(&ftrace_regex_lock);
  2582. return ret;
  2583. }
  2584. ssize_t
  2585. ftrace_filter_write(struct file *file, const char __user *ubuf,
  2586. size_t cnt, loff_t *ppos)
  2587. {
  2588. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  2589. }
  2590. ssize_t
  2591. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  2592. size_t cnt, loff_t *ppos)
  2593. {
  2594. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  2595. }
  2596. static int
  2597. ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
  2598. int reset, int enable)
  2599. {
  2600. struct ftrace_hash **orig_hash;
  2601. struct ftrace_hash *hash;
  2602. int ret;
  2603. /* All global ops uses the global ops filters */
  2604. if (ops->flags & FTRACE_OPS_FL_GLOBAL)
  2605. ops = &global_ops;
  2606. if (unlikely(ftrace_disabled))
  2607. return -ENODEV;
  2608. if (enable)
  2609. orig_hash = &ops->filter_hash;
  2610. else
  2611. orig_hash = &ops->notrace_hash;
  2612. hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
  2613. if (!hash)
  2614. return -ENOMEM;
  2615. mutex_lock(&ftrace_regex_lock);
  2616. if (reset)
  2617. ftrace_filter_reset(hash);
  2618. if (buf && !ftrace_match_records(hash, buf, len)) {
  2619. ret = -EINVAL;
  2620. goto out_regex_unlock;
  2621. }
  2622. mutex_lock(&ftrace_lock);
  2623. ret = ftrace_hash_move(ops, enable, orig_hash, hash);
  2624. if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
  2625. && ftrace_enabled)
  2626. ftrace_run_update_code(FTRACE_UPDATE_CALLS);
  2627. mutex_unlock(&ftrace_lock);
  2628. out_regex_unlock:
  2629. mutex_unlock(&ftrace_regex_lock);
  2630. free_ftrace_hash(hash);
  2631. return ret;
  2632. }
  2633. /**
  2634. * ftrace_set_filter - set a function to filter on in ftrace
  2635. * @ops - the ops to set the filter with
  2636. * @buf - the string that holds the function filter text.
  2637. * @len - the length of the string.
  2638. * @reset - non zero to reset all filters before applying this filter.
  2639. *
  2640. * Filters denote which functions should be enabled when tracing is enabled.
  2641. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  2642. */
  2643. int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
  2644. int len, int reset)
  2645. {
  2646. return ftrace_set_regex(ops, buf, len, reset, 1);
  2647. }
  2648. EXPORT_SYMBOL_GPL(ftrace_set_filter);
  2649. /**
  2650. * ftrace_set_notrace - set a function to not trace in ftrace
  2651. * @ops - the ops to set the notrace filter with
  2652. * @buf - the string that holds the function notrace text.
  2653. * @len - the length of the string.
  2654. * @reset - non zero to reset all filters before applying this filter.
  2655. *
  2656. * Notrace Filters denote which functions should not be enabled when tracing
  2657. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  2658. * for tracing.
  2659. */
  2660. int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
  2661. int len, int reset)
  2662. {
  2663. return ftrace_set_regex(ops, buf, len, reset, 0);
  2664. }
  2665. EXPORT_SYMBOL_GPL(ftrace_set_notrace);
  2666. /**
  2667. * ftrace_set_filter - set a function to filter on in ftrace
  2668. * @ops - the ops to set the filter with
  2669. * @buf - the string that holds the function filter text.
  2670. * @len - the length of the string.
  2671. * @reset - non zero to reset all filters before applying this filter.
  2672. *
  2673. * Filters denote which functions should be enabled when tracing is enabled.
  2674. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  2675. */
  2676. void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
  2677. {
  2678. ftrace_set_regex(&global_ops, buf, len, reset, 1);
  2679. }
  2680. EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
  2681. /**
  2682. * ftrace_set_notrace - set a function to not trace in ftrace
  2683. * @ops - the ops to set the notrace filter with
  2684. * @buf - the string that holds the function notrace text.
  2685. * @len - the length of the string.
  2686. * @reset - non zero to reset all filters before applying this filter.
  2687. *
  2688. * Notrace Filters denote which functions should not be enabled when tracing
  2689. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  2690. * for tracing.
  2691. */
  2692. void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
  2693. {
  2694. ftrace_set_regex(&global_ops, buf, len, reset, 0);
  2695. }
  2696. EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
  2697. /*
  2698. * command line interface to allow users to set filters on boot up.
  2699. */
  2700. #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
  2701. static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
  2702. static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
  2703. static int __init set_ftrace_notrace(char *str)
  2704. {
  2705. strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
  2706. return 1;
  2707. }
  2708. __setup("ftrace_notrace=", set_ftrace_notrace);
  2709. static int __init set_ftrace_filter(char *str)
  2710. {
  2711. strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
  2712. return 1;
  2713. }
  2714. __setup("ftrace_filter=", set_ftrace_filter);
  2715. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2716. static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
  2717. static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
  2718. static int __init set_graph_function(char *str)
  2719. {
  2720. strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
  2721. return 1;
  2722. }
  2723. __setup("ftrace_graph_filter=", set_graph_function);
  2724. static void __init set_ftrace_early_graph(char *buf)
  2725. {
  2726. int ret;
  2727. char *func;
  2728. while (buf) {
  2729. func = strsep(&buf, ",");
  2730. /* we allow only one expression at a time */
  2731. ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
  2732. func);
  2733. if (ret)
  2734. printk(KERN_DEBUG "ftrace: function %s not "
  2735. "traceable\n", func);
  2736. }
  2737. }
  2738. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2739. void __init
  2740. ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
  2741. {
  2742. char *func;
  2743. while (buf) {
  2744. func = strsep(&buf, ",");
  2745. ftrace_set_regex(ops, func, strlen(func), 0, enable);
  2746. }
  2747. }
  2748. static void __init set_ftrace_early_filters(void)
  2749. {
  2750. if (ftrace_filter_buf[0])
  2751. ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
  2752. if (ftrace_notrace_buf[0])
  2753. ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
  2754. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2755. if (ftrace_graph_buf[0])
  2756. set_ftrace_early_graph(ftrace_graph_buf);
  2757. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2758. }
  2759. int ftrace_regex_release(struct inode *inode, struct file *file)
  2760. {
  2761. struct seq_file *m = (struct seq_file *)file->private_data;
  2762. struct ftrace_iterator *iter;
  2763. struct ftrace_hash **orig_hash;
  2764. struct trace_parser *parser;
  2765. int filter_hash;
  2766. int ret;
  2767. mutex_lock(&ftrace_regex_lock);
  2768. if (file->f_mode & FMODE_READ) {
  2769. iter = m->private;
  2770. seq_release(inode, file);
  2771. } else
  2772. iter = file->private_data;
  2773. parser = &iter->parser;
  2774. if (trace_parser_loaded(parser)) {
  2775. parser->buffer[parser->idx] = 0;
  2776. ftrace_match_records(iter->hash, parser->buffer, parser->idx);
  2777. }
  2778. trace_parser_put(parser);
  2779. if (file->f_mode & FMODE_WRITE) {
  2780. filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
  2781. if (filter_hash)
  2782. orig_hash = &iter->ops->filter_hash;
  2783. else
  2784. orig_hash = &iter->ops->notrace_hash;
  2785. mutex_lock(&ftrace_lock);
  2786. ret = ftrace_hash_move(iter->ops, filter_hash,
  2787. orig_hash, iter->hash);
  2788. if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
  2789. && ftrace_enabled)
  2790. ftrace_run_update_code(FTRACE_UPDATE_CALLS);
  2791. mutex_unlock(&ftrace_lock);
  2792. }
  2793. free_ftrace_hash(iter->hash);
  2794. kfree(iter);
  2795. mutex_unlock(&ftrace_regex_lock);
  2796. return 0;
  2797. }
  2798. static const struct file_operations ftrace_avail_fops = {
  2799. .open = ftrace_avail_open,
  2800. .read = seq_read,
  2801. .llseek = seq_lseek,
  2802. .release = seq_release_private,
  2803. };
  2804. static const struct file_operations ftrace_enabled_fops = {
  2805. .open = ftrace_enabled_open,
  2806. .read = seq_read,
  2807. .llseek = seq_lseek,
  2808. .release = seq_release_private,
  2809. };
  2810. static const struct file_operations ftrace_filter_fops = {
  2811. .open = ftrace_filter_open,
  2812. .read = seq_read,
  2813. .write = ftrace_filter_write,
  2814. .llseek = ftrace_regex_lseek,
  2815. .release = ftrace_regex_release,
  2816. };
  2817. static const struct file_operations ftrace_notrace_fops = {
  2818. .open = ftrace_notrace_open,
  2819. .read = seq_read,
  2820. .write = ftrace_notrace_write,
  2821. .llseek = ftrace_regex_lseek,
  2822. .release = ftrace_regex_release,
  2823. };
  2824. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2825. static DEFINE_MUTEX(graph_lock);
  2826. int ftrace_graph_count;
  2827. int ftrace_graph_filter_enabled;
  2828. unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
  2829. static void *
  2830. __g_next(struct seq_file *m, loff_t *pos)
  2831. {
  2832. if (*pos >= ftrace_graph_count)
  2833. return NULL;
  2834. return &ftrace_graph_funcs[*pos];
  2835. }
  2836. static void *
  2837. g_next(struct seq_file *m, void *v, loff_t *pos)
  2838. {
  2839. (*pos)++;
  2840. return __g_next(m, pos);
  2841. }
  2842. static void *g_start(struct seq_file *m, loff_t *pos)
  2843. {
  2844. mutex_lock(&graph_lock);
  2845. /* Nothing, tell g_show to print all functions are enabled */
  2846. if (!ftrace_graph_filter_enabled && !*pos)
  2847. return (void *)1;
  2848. return __g_next(m, pos);
  2849. }
  2850. static void g_stop(struct seq_file *m, void *p)
  2851. {
  2852. mutex_unlock(&graph_lock);
  2853. }
  2854. static int g_show(struct seq_file *m, void *v)
  2855. {
  2856. unsigned long *ptr = v;
  2857. if (!ptr)
  2858. return 0;
  2859. if (ptr == (unsigned long *)1) {
  2860. seq_printf(m, "#### all functions enabled ####\n");
  2861. return 0;
  2862. }
  2863. seq_printf(m, "%ps\n", (void *)*ptr);
  2864. return 0;
  2865. }
  2866. static const struct seq_operations ftrace_graph_seq_ops = {
  2867. .start = g_start,
  2868. .next = g_next,
  2869. .stop = g_stop,
  2870. .show = g_show,
  2871. };
  2872. static int
  2873. ftrace_graph_open(struct inode *inode, struct file *file)
  2874. {
  2875. int ret = 0;
  2876. if (unlikely(ftrace_disabled))
  2877. return -ENODEV;
  2878. mutex_lock(&graph_lock);
  2879. if ((file->f_mode & FMODE_WRITE) &&
  2880. (file->f_flags & O_TRUNC)) {
  2881. ftrace_graph_filter_enabled = 0;
  2882. ftrace_graph_count = 0;
  2883. memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
  2884. }
  2885. mutex_unlock(&graph_lock);
  2886. if (file->f_mode & FMODE_READ)
  2887. ret = seq_open(file, &ftrace_graph_seq_ops);
  2888. return ret;
  2889. }
  2890. static int
  2891. ftrace_graph_release(struct inode *inode, struct file *file)
  2892. {
  2893. if (file->f_mode & FMODE_READ)
  2894. seq_release(inode, file);
  2895. return 0;
  2896. }
  2897. static int
  2898. ftrace_set_func(unsigned long *array, int *idx, char *buffer)
  2899. {
  2900. struct dyn_ftrace *rec;
  2901. struct ftrace_page *pg;
  2902. int search_len;
  2903. int fail = 1;
  2904. int type, not;
  2905. char *search;
  2906. bool exists;
  2907. int i;
  2908. /* decode regex */
  2909. type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
  2910. if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
  2911. return -EBUSY;
  2912. search_len = strlen(search);
  2913. mutex_lock(&ftrace_lock);
  2914. if (unlikely(ftrace_disabled)) {
  2915. mutex_unlock(&ftrace_lock);
  2916. return -ENODEV;
  2917. }
  2918. do_for_each_ftrace_rec(pg, rec) {
  2919. if (ftrace_match_record(rec, NULL, search, search_len, type)) {
  2920. /* if it is in the array */
  2921. exists = false;
  2922. for (i = 0; i < *idx; i++) {
  2923. if (array[i] == rec->ip) {
  2924. exists = true;
  2925. break;
  2926. }
  2927. }
  2928. if (!not) {
  2929. fail = 0;
  2930. if (!exists) {
  2931. array[(*idx)++] = rec->ip;
  2932. if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
  2933. goto out;
  2934. }
  2935. } else {
  2936. if (exists) {
  2937. array[i] = array[--(*idx)];
  2938. array[*idx] = 0;
  2939. fail = 0;
  2940. }
  2941. }
  2942. }
  2943. } while_for_each_ftrace_rec();
  2944. out:
  2945. mutex_unlock(&ftrace_lock);
  2946. if (fail)
  2947. return -EINVAL;
  2948. ftrace_graph_filter_enabled = 1;
  2949. return 0;
  2950. }
  2951. static ssize_t
  2952. ftrace_graph_write(struct file *file, const char __user *ubuf,
  2953. size_t cnt, loff_t *ppos)
  2954. {
  2955. struct trace_parser parser;
  2956. ssize_t read, ret;
  2957. if (!cnt)
  2958. return 0;
  2959. mutex_lock(&graph_lock);
  2960. if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
  2961. ret = -ENOMEM;
  2962. goto out_unlock;
  2963. }
  2964. read = trace_get_user(&parser, ubuf, cnt, ppos);
  2965. if (read >= 0 && trace_parser_loaded((&parser))) {
  2966. parser.buffer[parser.idx] = 0;
  2967. /* we allow only one expression at a time */
  2968. ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
  2969. parser.buffer);
  2970. if (ret)
  2971. goto out_free;
  2972. }
  2973. ret = read;
  2974. out_free:
  2975. trace_parser_put(&parser);
  2976. out_unlock:
  2977. mutex_unlock(&graph_lock);
  2978. return ret;
  2979. }
  2980. static const struct file_operations ftrace_graph_fops = {
  2981. .open = ftrace_graph_open,
  2982. .read = seq_read,
  2983. .write = ftrace_graph_write,
  2984. .release = ftrace_graph_release,
  2985. .llseek = seq_lseek,
  2986. };
  2987. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2988. static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
  2989. {
  2990. trace_create_file("available_filter_functions", 0444,
  2991. d_tracer, NULL, &ftrace_avail_fops);
  2992. trace_create_file("enabled_functions", 0444,
  2993. d_tracer, NULL, &ftrace_enabled_fops);
  2994. trace_create_file("set_ftrace_filter", 0644, d_tracer,
  2995. NULL, &ftrace_filter_fops);
  2996. trace_create_file("set_ftrace_notrace", 0644, d_tracer,
  2997. NULL, &ftrace_notrace_fops);
  2998. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2999. trace_create_file("set_graph_function", 0444, d_tracer,
  3000. NULL,
  3001. &ftrace_graph_fops);
  3002. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  3003. return 0;
  3004. }
  3005. static int ftrace_cmp_ips(const void *a, const void *b)
  3006. {
  3007. const unsigned long *ipa = a;
  3008. const unsigned long *ipb = b;
  3009. if (*ipa > *ipb)
  3010. return 1;
  3011. if (*ipa < *ipb)
  3012. return -1;
  3013. return 0;
  3014. }
  3015. static void ftrace_swap_ips(void *a, void *b, int size)
  3016. {
  3017. unsigned long *ipa = a;
  3018. unsigned long *ipb = b;
  3019. unsigned long t;
  3020. t = *ipa;
  3021. *ipa = *ipb;
  3022. *ipb = t;
  3023. }
  3024. static int ftrace_process_locs(struct module *mod,
  3025. unsigned long *start,
  3026. unsigned long *end)
  3027. {
  3028. struct ftrace_page *start_pg;
  3029. struct ftrace_page *pg;
  3030. struct dyn_ftrace *rec;
  3031. unsigned long count;
  3032. unsigned long *p;
  3033. unsigned long addr;
  3034. unsigned long flags = 0; /* Shut up gcc */
  3035. int ret = -ENOMEM;
  3036. count = end - start;
  3037. if (!count)
  3038. return 0;
  3039. sort(start, count, sizeof(*start),
  3040. ftrace_cmp_ips, ftrace_swap_ips);
  3041. start_pg = ftrace_allocate_pages(count);
  3042. if (!start_pg)
  3043. return -ENOMEM;
  3044. mutex_lock(&ftrace_lock);
  3045. /*
  3046. * Core and each module needs their own pages, as
  3047. * modules will free them when they are removed.
  3048. * Force a new page to be allocated for modules.
  3049. */
  3050. if (!mod) {
  3051. WARN_ON(ftrace_pages || ftrace_pages_start);
  3052. /* First initialization */
  3053. ftrace_pages = ftrace_pages_start = start_pg;
  3054. } else {
  3055. if (!ftrace_pages)
  3056. goto out;
  3057. if (WARN_ON(ftrace_pages->next)) {
  3058. /* Hmm, we have free pages? */
  3059. while (ftrace_pages->next)
  3060. ftrace_pages = ftrace_pages->next;
  3061. }
  3062. ftrace_pages->next = start_pg;
  3063. }
  3064. p = start;
  3065. pg = start_pg;
  3066. while (p < end) {
  3067. addr = ftrace_call_adjust(*p++);
  3068. /*
  3069. * Some architecture linkers will pad between
  3070. * the different mcount_loc sections of different
  3071. * object files to satisfy alignments.
  3072. * Skip any NULL pointers.
  3073. */
  3074. if (!addr)
  3075. continue;
  3076. if (pg->index == pg->size) {
  3077. /* We should have allocated enough */
  3078. if (WARN_ON(!pg->next))
  3079. break;
  3080. pg = pg->next;
  3081. }
  3082. rec = &pg->records[pg->index++];
  3083. rec->ip = addr;
  3084. }
  3085. /* We should have used all pages */
  3086. WARN_ON(pg->next);
  3087. /* Assign the last page to ftrace_pages */
  3088. ftrace_pages = pg;
  3089. /* These new locations need to be initialized */
  3090. ftrace_new_pgs = start_pg;
  3091. /*
  3092. * We only need to disable interrupts on start up
  3093. * because we are modifying code that an interrupt
  3094. * may execute, and the modification is not atomic.
  3095. * But for modules, nothing runs the code we modify
  3096. * until we are finished with it, and there's no
  3097. * reason to cause large interrupt latencies while we do it.
  3098. */
  3099. if (!mod)
  3100. local_irq_save(flags);
  3101. ftrace_update_code(mod);
  3102. if (!mod)
  3103. local_irq_restore(flags);
  3104. ret = 0;
  3105. out:
  3106. mutex_unlock(&ftrace_lock);
  3107. return ret;
  3108. }
  3109. #ifdef CONFIG_MODULES
  3110. #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
  3111. void ftrace_release_mod(struct module *mod)
  3112. {
  3113. struct dyn_ftrace *rec;
  3114. struct ftrace_page **last_pg;
  3115. struct ftrace_page *pg;
  3116. int order;
  3117. mutex_lock(&ftrace_lock);
  3118. if (ftrace_disabled)
  3119. goto out_unlock;
  3120. /*
  3121. * Each module has its own ftrace_pages, remove
  3122. * them from the list.
  3123. */
  3124. last_pg = &ftrace_pages_start;
  3125. for (pg = ftrace_pages_start; pg; pg = *last_pg) {
  3126. rec = &pg->records[0];
  3127. if (within_module_core(rec->ip, mod)) {
  3128. /*
  3129. * As core pages are first, the first
  3130. * page should never be a module page.
  3131. */
  3132. if (WARN_ON(pg == ftrace_pages_start))
  3133. goto out_unlock;
  3134. /* Check if we are deleting the last page */
  3135. if (pg == ftrace_pages)
  3136. ftrace_pages = next_to_ftrace_page(last_pg);
  3137. *last_pg = pg->next;
  3138. order = get_count_order(pg->size / ENTRIES_PER_PAGE);
  3139. free_pages((unsigned long)pg->records, order);
  3140. kfree(pg);
  3141. } else
  3142. last_pg = &pg->next;
  3143. }
  3144. out_unlock:
  3145. mutex_unlock(&ftrace_lock);
  3146. }
  3147. static void ftrace_init_module(struct module *mod,
  3148. unsigned long *start, unsigned long *end)
  3149. {
  3150. if (ftrace_disabled || start == end)
  3151. return;
  3152. ftrace_process_locs(mod, start, end);
  3153. }
  3154. static int ftrace_module_notify(struct notifier_block *self,
  3155. unsigned long val, void *data)
  3156. {
  3157. struct module *mod = data;
  3158. switch (val) {
  3159. case MODULE_STATE_COMING:
  3160. ftrace_init_module(mod, mod->ftrace_callsites,
  3161. mod->ftrace_callsites +
  3162. mod->num_ftrace_callsites);
  3163. break;
  3164. case MODULE_STATE_GOING:
  3165. ftrace_release_mod(mod);
  3166. break;
  3167. }
  3168. return 0;
  3169. }
  3170. #else
  3171. static int ftrace_module_notify(struct notifier_block *self,
  3172. unsigned long val, void *data)
  3173. {
  3174. return 0;
  3175. }
  3176. #endif /* CONFIG_MODULES */
  3177. struct notifier_block ftrace_module_nb = {
  3178. .notifier_call = ftrace_module_notify,
  3179. .priority = 0,
  3180. };
  3181. extern unsigned long __start_mcount_loc[];
  3182. extern unsigned long __stop_mcount_loc[];
  3183. void __init ftrace_init(void)
  3184. {
  3185. unsigned long count, addr, flags;
  3186. int ret;
  3187. /* Keep the ftrace pointer to the stub */
  3188. addr = (unsigned long)ftrace_stub;
  3189. local_irq_save(flags);
  3190. ftrace_dyn_arch_init(&addr);
  3191. local_irq_restore(flags);
  3192. /* ftrace_dyn_arch_init places the return code in addr */
  3193. if (addr)
  3194. goto failed;
  3195. count = __stop_mcount_loc - __start_mcount_loc;
  3196. ret = ftrace_dyn_table_alloc(count);
  3197. if (ret)
  3198. goto failed;
  3199. last_ftrace_enabled = ftrace_enabled = 1;
  3200. ret = ftrace_process_locs(NULL,
  3201. __start_mcount_loc,
  3202. __stop_mcount_loc);
  3203. ret = register_module_notifier(&ftrace_module_nb);
  3204. if (ret)
  3205. pr_warning("Failed to register trace ftrace module notifier\n");
  3206. set_ftrace_early_filters();
  3207. return;
  3208. failed:
  3209. ftrace_disabled = 1;
  3210. }
  3211. #else
  3212. static struct ftrace_ops global_ops = {
  3213. .func = ftrace_stub,
  3214. };
  3215. static int __init ftrace_nodyn_init(void)
  3216. {
  3217. ftrace_enabled = 1;
  3218. return 0;
  3219. }
  3220. device_initcall(ftrace_nodyn_init);
  3221. static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
  3222. static inline void ftrace_startup_enable(int command) { }
  3223. /* Keep as macros so we do not need to define the commands */
  3224. # define ftrace_startup(ops, command) \
  3225. ({ \
  3226. (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
  3227. 0; \
  3228. })
  3229. # define ftrace_shutdown(ops, command) do { } while (0)
  3230. # define ftrace_startup_sysctl() do { } while (0)
  3231. # define ftrace_shutdown_sysctl() do { } while (0)
  3232. static inline int
  3233. ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
  3234. {
  3235. return 1;
  3236. }
  3237. #endif /* CONFIG_DYNAMIC_FTRACE */
  3238. static void
  3239. ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip)
  3240. {
  3241. struct ftrace_ops *op;
  3242. if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
  3243. return;
  3244. /*
  3245. * Some of the ops may be dynamically allocated,
  3246. * they must be freed after a synchronize_sched().
  3247. */
  3248. preempt_disable_notrace();
  3249. trace_recursion_set(TRACE_CONTROL_BIT);
  3250. op = rcu_dereference_raw(ftrace_control_list);
  3251. while (op != &ftrace_list_end) {
  3252. if (!ftrace_function_local_disabled(op) &&
  3253. ftrace_ops_test(op, ip))
  3254. op->func(ip, parent_ip);
  3255. op = rcu_dereference_raw(op->next);
  3256. };
  3257. trace_recursion_clear(TRACE_CONTROL_BIT);
  3258. preempt_enable_notrace();
  3259. }
  3260. static struct ftrace_ops control_ops = {
  3261. .func = ftrace_ops_control_func,
  3262. };
  3263. static void
  3264. ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
  3265. {
  3266. struct ftrace_ops *op;
  3267. if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
  3268. return;
  3269. trace_recursion_set(TRACE_INTERNAL_BIT);
  3270. /*
  3271. * Some of the ops may be dynamically allocated,
  3272. * they must be freed after a synchronize_sched().
  3273. */
  3274. preempt_disable_notrace();
  3275. op = rcu_dereference_raw(ftrace_ops_list);
  3276. while (op != &ftrace_list_end) {
  3277. if (ftrace_ops_test(op, ip))
  3278. op->func(ip, parent_ip);
  3279. op = rcu_dereference_raw(op->next);
  3280. };
  3281. preempt_enable_notrace();
  3282. trace_recursion_clear(TRACE_INTERNAL_BIT);
  3283. }
  3284. static void clear_ftrace_swapper(void)
  3285. {
  3286. struct task_struct *p;
  3287. int cpu;
  3288. get_online_cpus();
  3289. for_each_online_cpu(cpu) {
  3290. p = idle_task(cpu);
  3291. clear_tsk_trace_trace(p);
  3292. }
  3293. put_online_cpus();
  3294. }
  3295. static void set_ftrace_swapper(void)
  3296. {
  3297. struct task_struct *p;
  3298. int cpu;
  3299. get_online_cpus();
  3300. for_each_online_cpu(cpu) {
  3301. p = idle_task(cpu);
  3302. set_tsk_trace_trace(p);
  3303. }
  3304. put_online_cpus();
  3305. }
  3306. static void clear_ftrace_pid(struct pid *pid)
  3307. {
  3308. struct task_struct *p;
  3309. rcu_read_lock();
  3310. do_each_pid_task(pid, PIDTYPE_PID, p) {
  3311. clear_tsk_trace_trace(p);
  3312. } while_each_pid_task(pid, PIDTYPE_PID, p);
  3313. rcu_read_unlock();
  3314. put_pid(pid);
  3315. }
  3316. static void set_ftrace_pid(struct pid *pid)
  3317. {
  3318. struct task_struct *p;
  3319. rcu_read_lock();
  3320. do_each_pid_task(pid, PIDTYPE_PID, p) {
  3321. set_tsk_trace_trace(p);
  3322. } while_each_pid_task(pid, PIDTYPE_PID, p);
  3323. rcu_read_unlock();
  3324. }
  3325. static void clear_ftrace_pid_task(struct pid *pid)
  3326. {
  3327. if (pid == ftrace_swapper_pid)
  3328. clear_ftrace_swapper();
  3329. else
  3330. clear_ftrace_pid(pid);
  3331. }
  3332. static void set_ftrace_pid_task(struct pid *pid)
  3333. {
  3334. if (pid == ftrace_swapper_pid)
  3335. set_ftrace_swapper();
  3336. else
  3337. set_ftrace_pid(pid);
  3338. }
  3339. static int ftrace_pid_add(int p)
  3340. {
  3341. struct pid *pid;
  3342. struct ftrace_pid *fpid;
  3343. int ret = -EINVAL;
  3344. mutex_lock(&ftrace_lock);
  3345. if (!p)
  3346. pid = ftrace_swapper_pid;
  3347. else
  3348. pid = find_get_pid(p);
  3349. if (!pid)
  3350. goto out;
  3351. ret = 0;
  3352. list_for_each_entry(fpid, &ftrace_pids, list)
  3353. if (fpid->pid == pid)
  3354. goto out_put;
  3355. ret = -ENOMEM;
  3356. fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
  3357. if (!fpid)
  3358. goto out_put;
  3359. list_add(&fpid->list, &ftrace_pids);
  3360. fpid->pid = pid;
  3361. set_ftrace_pid_task(pid);
  3362. ftrace_update_pid_func();
  3363. ftrace_startup_enable(0);
  3364. mutex_unlock(&ftrace_lock);
  3365. return 0;
  3366. out_put:
  3367. if (pid != ftrace_swapper_pid)
  3368. put_pid(pid);
  3369. out:
  3370. mutex_unlock(&ftrace_lock);
  3371. return ret;
  3372. }
  3373. static void ftrace_pid_reset(void)
  3374. {
  3375. struct ftrace_pid *fpid, *safe;
  3376. mutex_lock(&ftrace_lock);
  3377. list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
  3378. struct pid *pid = fpid->pid;
  3379. clear_ftrace_pid_task(pid);
  3380. list_del(&fpid->list);
  3381. kfree(fpid);
  3382. }
  3383. ftrace_update_pid_func();
  3384. ftrace_startup_enable(0);
  3385. mutex_unlock(&ftrace_lock);
  3386. }
  3387. static void *fpid_start(struct seq_file *m, loff_t *pos)
  3388. {
  3389. mutex_lock(&ftrace_lock);
  3390. if (list_empty(&ftrace_pids) && (!*pos))
  3391. return (void *) 1;
  3392. return seq_list_start(&ftrace_pids, *pos);
  3393. }
  3394. static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
  3395. {
  3396. if (v == (void *)1)
  3397. return NULL;
  3398. return seq_list_next(v, &ftrace_pids, pos);
  3399. }
  3400. static void fpid_stop(struct seq_file *m, void *p)
  3401. {
  3402. mutex_unlock(&ftrace_lock);
  3403. }
  3404. static int fpid_show(struct seq_file *m, void *v)
  3405. {
  3406. const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
  3407. if (v == (void *)1) {
  3408. seq_printf(m, "no pid\n");
  3409. return 0;
  3410. }
  3411. if (fpid->pid == ftrace_swapper_pid)
  3412. seq_printf(m, "swapper tasks\n");
  3413. else
  3414. seq_printf(m, "%u\n", pid_vnr(fpid->pid));
  3415. return 0;
  3416. }
  3417. static const struct seq_operations ftrace_pid_sops = {
  3418. .start = fpid_start,
  3419. .next = fpid_next,
  3420. .stop = fpid_stop,
  3421. .show = fpid_show,
  3422. };
  3423. static int
  3424. ftrace_pid_open(struct inode *inode, struct file *file)
  3425. {
  3426. int ret = 0;
  3427. if ((file->f_mode & FMODE_WRITE) &&
  3428. (file->f_flags & O_TRUNC))
  3429. ftrace_pid_reset();
  3430. if (file->f_mode & FMODE_READ)
  3431. ret = seq_open(file, &ftrace_pid_sops);
  3432. return ret;
  3433. }
  3434. static ssize_t
  3435. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  3436. size_t cnt, loff_t *ppos)
  3437. {
  3438. char buf[64], *tmp;
  3439. long val;
  3440. int ret;
  3441. if (cnt >= sizeof(buf))
  3442. return -EINVAL;
  3443. if (copy_from_user(&buf, ubuf, cnt))
  3444. return -EFAULT;
  3445. buf[cnt] = 0;
  3446. /*
  3447. * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
  3448. * to clean the filter quietly.
  3449. */
  3450. tmp = strstrip(buf);
  3451. if (strlen(tmp) == 0)
  3452. return 1;
  3453. ret = strict_strtol(tmp, 10, &val);
  3454. if (ret < 0)
  3455. return ret;
  3456. ret = ftrace_pid_add(val);
  3457. return ret ? ret : cnt;
  3458. }
  3459. static int
  3460. ftrace_pid_release(struct inode *inode, struct file *file)
  3461. {
  3462. if (file->f_mode & FMODE_READ)
  3463. seq_release(inode, file);
  3464. return 0;
  3465. }
  3466. static const struct file_operations ftrace_pid_fops = {
  3467. .open = ftrace_pid_open,
  3468. .write = ftrace_pid_write,
  3469. .read = seq_read,
  3470. .llseek = seq_lseek,
  3471. .release = ftrace_pid_release,
  3472. };
  3473. static __init int ftrace_init_debugfs(void)
  3474. {
  3475. struct dentry *d_tracer;
  3476. d_tracer = tracing_init_dentry();
  3477. if (!d_tracer)
  3478. return 0;
  3479. ftrace_init_dyn_debugfs(d_tracer);
  3480. trace_create_file("set_ftrace_pid", 0644, d_tracer,
  3481. NULL, &ftrace_pid_fops);
  3482. ftrace_profile_debugfs(d_tracer);
  3483. return 0;
  3484. }
  3485. fs_initcall(ftrace_init_debugfs);
  3486. /**
  3487. * ftrace_kill - kill ftrace
  3488. *
  3489. * This function should be used by panic code. It stops ftrace
  3490. * but in a not so nice way. If you need to simply kill ftrace
  3491. * from a non-atomic section, use ftrace_kill.
  3492. */
  3493. void ftrace_kill(void)
  3494. {
  3495. ftrace_disabled = 1;
  3496. ftrace_enabled = 0;
  3497. clear_ftrace_function();
  3498. }
  3499. /**
  3500. * Test if ftrace is dead or not.
  3501. */
  3502. int ftrace_is_dead(void)
  3503. {
  3504. return ftrace_disabled;
  3505. }
  3506. /**
  3507. * register_ftrace_function - register a function for profiling
  3508. * @ops - ops structure that holds the function for profiling.
  3509. *
  3510. * Register a function to be called by all functions in the
  3511. * kernel.
  3512. *
  3513. * Note: @ops->func and all the functions it calls must be labeled
  3514. * with "notrace", otherwise it will go into a
  3515. * recursive loop.
  3516. */
  3517. int register_ftrace_function(struct ftrace_ops *ops)
  3518. {
  3519. int ret = -1;
  3520. mutex_lock(&ftrace_lock);
  3521. if (unlikely(ftrace_disabled))
  3522. goto out_unlock;
  3523. ret = __register_ftrace_function(ops);
  3524. if (!ret)
  3525. ret = ftrace_startup(ops, 0);
  3526. out_unlock:
  3527. mutex_unlock(&ftrace_lock);
  3528. return ret;
  3529. }
  3530. EXPORT_SYMBOL_GPL(register_ftrace_function);
  3531. /**
  3532. * unregister_ftrace_function - unregister a function for profiling.
  3533. * @ops - ops structure that holds the function to unregister
  3534. *
  3535. * Unregister a function that was added to be called by ftrace profiling.
  3536. */
  3537. int unregister_ftrace_function(struct ftrace_ops *ops)
  3538. {
  3539. int ret;
  3540. mutex_lock(&ftrace_lock);
  3541. ret = __unregister_ftrace_function(ops);
  3542. if (!ret)
  3543. ftrace_shutdown(ops, 0);
  3544. mutex_unlock(&ftrace_lock);
  3545. return ret;
  3546. }
  3547. EXPORT_SYMBOL_GPL(unregister_ftrace_function);
  3548. int
  3549. ftrace_enable_sysctl(struct ctl_table *table, int write,
  3550. void __user *buffer, size_t *lenp,
  3551. loff_t *ppos)
  3552. {
  3553. int ret = -ENODEV;
  3554. mutex_lock(&ftrace_lock);
  3555. if (unlikely(ftrace_disabled))
  3556. goto out;
  3557. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  3558. if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
  3559. goto out;
  3560. last_ftrace_enabled = !!ftrace_enabled;
  3561. if (ftrace_enabled) {
  3562. ftrace_startup_sysctl();
  3563. /* we are starting ftrace again */
  3564. if (ftrace_ops_list != &ftrace_list_end) {
  3565. if (ftrace_ops_list->next == &ftrace_list_end)
  3566. ftrace_trace_function = ftrace_ops_list->func;
  3567. else
  3568. ftrace_trace_function = ftrace_ops_list_func;
  3569. }
  3570. } else {
  3571. /* stopping ftrace calls (just send to ftrace_stub) */
  3572. ftrace_trace_function = ftrace_stub;
  3573. ftrace_shutdown_sysctl();
  3574. }
  3575. out:
  3576. mutex_unlock(&ftrace_lock);
  3577. return ret;
  3578. }
  3579. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  3580. static int ftrace_graph_active;
  3581. static struct notifier_block ftrace_suspend_notifier;
  3582. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
  3583. {
  3584. return 0;
  3585. }
  3586. /* The callbacks that hook a function */
  3587. trace_func_graph_ret_t ftrace_graph_return =
  3588. (trace_func_graph_ret_t)ftrace_stub;
  3589. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  3590. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  3591. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  3592. {
  3593. int i;
  3594. int ret = 0;
  3595. unsigned long flags;
  3596. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  3597. struct task_struct *g, *t;
  3598. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  3599. ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
  3600. * sizeof(struct ftrace_ret_stack),
  3601. GFP_KERNEL);
  3602. if (!ret_stack_list[i]) {
  3603. start = 0;
  3604. end = i;
  3605. ret = -ENOMEM;
  3606. goto free;
  3607. }
  3608. }
  3609. read_lock_irqsave(&tasklist_lock, flags);
  3610. do_each_thread(g, t) {
  3611. if (start == end) {
  3612. ret = -EAGAIN;
  3613. goto unlock;
  3614. }
  3615. if (t->ret_stack == NULL) {
  3616. atomic_set(&t->tracing_graph_pause, 0);
  3617. atomic_set(&t->trace_overrun, 0);
  3618. t->curr_ret_stack = -1;
  3619. /* Make sure the tasks see the -1 first: */
  3620. smp_wmb();
  3621. t->ret_stack = ret_stack_list[start++];
  3622. }
  3623. } while_each_thread(g, t);
  3624. unlock:
  3625. read_unlock_irqrestore(&tasklist_lock, flags);
  3626. free:
  3627. for (i = start; i < end; i++)
  3628. kfree(ret_stack_list[i]);
  3629. return ret;
  3630. }
  3631. static void
  3632. ftrace_graph_probe_sched_switch(void *ignore,
  3633. struct task_struct *prev, struct task_struct *next)
  3634. {
  3635. unsigned long long timestamp;
  3636. int index;
  3637. /*
  3638. * Does the user want to count the time a function was asleep.
  3639. * If so, do not update the time stamps.
  3640. */
  3641. if (trace_flags & TRACE_ITER_SLEEP_TIME)
  3642. return;
  3643. timestamp = trace_clock_local();
  3644. prev->ftrace_timestamp = timestamp;
  3645. /* only process tasks that we timestamped */
  3646. if (!next->ftrace_timestamp)
  3647. return;
  3648. /*
  3649. * Update all the counters in next to make up for the
  3650. * time next was sleeping.
  3651. */
  3652. timestamp -= next->ftrace_timestamp;
  3653. for (index = next->curr_ret_stack; index >= 0; index--)
  3654. next->ret_stack[index].calltime += timestamp;
  3655. }
  3656. /* Allocate a return stack for each task */
  3657. static int start_graph_tracing(void)
  3658. {
  3659. struct ftrace_ret_stack **ret_stack_list;
  3660. int ret, cpu;
  3661. ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
  3662. sizeof(struct ftrace_ret_stack *),
  3663. GFP_KERNEL);
  3664. if (!ret_stack_list)
  3665. return -ENOMEM;
  3666. /* The cpu_boot init_task->ret_stack will never be freed */
  3667. for_each_online_cpu(cpu) {
  3668. if (!idle_task(cpu)->ret_stack)
  3669. ftrace_graph_init_idle_task(idle_task(cpu), cpu);
  3670. }
  3671. do {
  3672. ret = alloc_retstack_tasklist(ret_stack_list);
  3673. } while (ret == -EAGAIN);
  3674. if (!ret) {
  3675. ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  3676. if (ret)
  3677. pr_info("ftrace_graph: Couldn't activate tracepoint"
  3678. " probe to kernel_sched_switch\n");
  3679. }
  3680. kfree(ret_stack_list);
  3681. return ret;
  3682. }
  3683. /*
  3684. * Hibernation protection.
  3685. * The state of the current task is too much unstable during
  3686. * suspend/restore to disk. We want to protect against that.
  3687. */
  3688. static int
  3689. ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
  3690. void *unused)
  3691. {
  3692. switch (state) {
  3693. case PM_HIBERNATION_PREPARE:
  3694. pause_graph_tracing();
  3695. break;
  3696. case PM_POST_HIBERNATION:
  3697. unpause_graph_tracing();
  3698. break;
  3699. }
  3700. return NOTIFY_DONE;
  3701. }
  3702. int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  3703. trace_func_graph_ent_t entryfunc)
  3704. {
  3705. int ret = 0;
  3706. mutex_lock(&ftrace_lock);
  3707. /* we currently allow only one tracer registered at a time */
  3708. if (ftrace_graph_active) {
  3709. ret = -EBUSY;
  3710. goto out;
  3711. }
  3712. ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
  3713. register_pm_notifier(&ftrace_suspend_notifier);
  3714. ftrace_graph_active++;
  3715. ret = start_graph_tracing();
  3716. if (ret) {
  3717. ftrace_graph_active--;
  3718. goto out;
  3719. }
  3720. ftrace_graph_return = retfunc;
  3721. ftrace_graph_entry = entryfunc;
  3722. ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
  3723. out:
  3724. mutex_unlock(&ftrace_lock);
  3725. return ret;
  3726. }
  3727. void unregister_ftrace_graph(void)
  3728. {
  3729. mutex_lock(&ftrace_lock);
  3730. if (unlikely(!ftrace_graph_active))
  3731. goto out;
  3732. ftrace_graph_active--;
  3733. ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
  3734. ftrace_graph_entry = ftrace_graph_entry_stub;
  3735. ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
  3736. unregister_pm_notifier(&ftrace_suspend_notifier);
  3737. unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  3738. out:
  3739. mutex_unlock(&ftrace_lock);
  3740. }
  3741. static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
  3742. static void
  3743. graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
  3744. {
  3745. atomic_set(&t->tracing_graph_pause, 0);
  3746. atomic_set(&t->trace_overrun, 0);
  3747. t->ftrace_timestamp = 0;
  3748. /* make curr_ret_stack visible before we add the ret_stack */
  3749. smp_wmb();
  3750. t->ret_stack = ret_stack;
  3751. }
  3752. /*
  3753. * Allocate a return stack for the idle task. May be the first
  3754. * time through, or it may be done by CPU hotplug online.
  3755. */
  3756. void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
  3757. {
  3758. t->curr_ret_stack = -1;
  3759. /*
  3760. * The idle task has no parent, it either has its own
  3761. * stack or no stack at all.
  3762. */
  3763. if (t->ret_stack)
  3764. WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
  3765. if (ftrace_graph_active) {
  3766. struct ftrace_ret_stack *ret_stack;
  3767. ret_stack = per_cpu(idle_ret_stack, cpu);
  3768. if (!ret_stack) {
  3769. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  3770. * sizeof(struct ftrace_ret_stack),
  3771. GFP_KERNEL);
  3772. if (!ret_stack)
  3773. return;
  3774. per_cpu(idle_ret_stack, cpu) = ret_stack;
  3775. }
  3776. graph_init_task(t, ret_stack);
  3777. }
  3778. }
  3779. /* Allocate a return stack for newly created task */
  3780. void ftrace_graph_init_task(struct task_struct *t)
  3781. {
  3782. /* Make sure we do not use the parent ret_stack */
  3783. t->ret_stack = NULL;
  3784. t->curr_ret_stack = -1;
  3785. if (ftrace_graph_active) {
  3786. struct ftrace_ret_stack *ret_stack;
  3787. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  3788. * sizeof(struct ftrace_ret_stack),
  3789. GFP_KERNEL);
  3790. if (!ret_stack)
  3791. return;
  3792. graph_init_task(t, ret_stack);
  3793. }
  3794. }
  3795. void ftrace_graph_exit_task(struct task_struct *t)
  3796. {
  3797. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  3798. t->ret_stack = NULL;
  3799. /* NULL must become visible to IRQs before we free it: */
  3800. barrier();
  3801. kfree(ret_stack);
  3802. }
  3803. void ftrace_graph_stop(void)
  3804. {
  3805. ftrace_stop();
  3806. }
  3807. #endif