ftrace.c 94 KB

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