ftrace.c 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235
  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/ftrace.h>
  25. #include <linux/sysctl.h>
  26. #include <linux/slab.h>
  27. #include <linux/ctype.h>
  28. #include <linux/list.h>
  29. #include <linux/hash.h>
  30. #include <linux/rcupdate.h>
  31. #include <trace/events/sched.h>
  32. #include <asm/setup.h>
  33. #include "trace_output.h"
  34. #include "trace_stat.h"
  35. #define FTRACE_WARN_ON(cond) \
  36. ({ \
  37. int ___r = cond; \
  38. if (WARN_ON(___r)) \
  39. ftrace_kill(); \
  40. ___r; \
  41. })
  42. #define FTRACE_WARN_ON_ONCE(cond) \
  43. ({ \
  44. int ___r = cond; \
  45. if (WARN_ON_ONCE(___r)) \
  46. ftrace_kill(); \
  47. ___r; \
  48. })
  49. /* hash bits for specific function selection */
  50. #define FTRACE_HASH_BITS 7
  51. #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
  52. #define FTRACE_HASH_DEFAULT_BITS 10
  53. #define FTRACE_HASH_MAX_BITS 12
  54. /* ftrace_enabled is a method to turn ftrace on or off */
  55. int ftrace_enabled __read_mostly;
  56. static int last_ftrace_enabled;
  57. /* Quick disabling of function tracer. */
  58. int function_trace_stop;
  59. /* List for set_ftrace_pid's pids. */
  60. LIST_HEAD(ftrace_pids);
  61. struct ftrace_pid {
  62. struct list_head list;
  63. struct pid *pid;
  64. };
  65. /*
  66. * ftrace_disabled is set when an anomaly is discovered.
  67. * ftrace_disabled is much stronger than ftrace_enabled.
  68. */
  69. static int ftrace_disabled __read_mostly;
  70. static DEFINE_MUTEX(ftrace_lock);
  71. static struct ftrace_ops ftrace_list_end __read_mostly = {
  72. .func = ftrace_stub,
  73. };
  74. static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
  75. static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
  76. ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
  77. static ftrace_func_t __ftrace_trace_function_delay __read_mostly = ftrace_stub;
  78. ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
  79. ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
  80. static struct ftrace_ops global_ops;
  81. static void
  82. ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
  83. /*
  84. * Traverse the ftrace_global_list, invoking all entries. The reason that we
  85. * can use rcu_dereference_raw() is that elements removed from this list
  86. * are simply leaked, so there is no need to interact with a grace-period
  87. * mechanism. The rcu_dereference_raw() calls are needed to handle
  88. * concurrent insertions into the ftrace_global_list.
  89. *
  90. * Silly Alpha and silly pointer-speculation compiler optimizations!
  91. */
  92. static void ftrace_global_list_func(unsigned long ip,
  93. unsigned long parent_ip)
  94. {
  95. struct ftrace_ops *op;
  96. if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
  97. return;
  98. trace_recursion_set(TRACE_GLOBAL_BIT);
  99. op = rcu_dereference_raw(ftrace_global_list); /*see above*/
  100. while (op != &ftrace_list_end) {
  101. op->func(ip, parent_ip);
  102. op = rcu_dereference_raw(op->next); /*see above*/
  103. };
  104. trace_recursion_clear(TRACE_GLOBAL_BIT);
  105. }
  106. static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
  107. {
  108. if (!test_tsk_trace_trace(current))
  109. return;
  110. ftrace_pid_function(ip, parent_ip);
  111. }
  112. static void set_ftrace_pid_function(ftrace_func_t func)
  113. {
  114. /* do not set ftrace_pid_function to itself! */
  115. if (func != ftrace_pid_func)
  116. ftrace_pid_function = func;
  117. }
  118. /**
  119. * clear_ftrace_function - reset the ftrace function
  120. *
  121. * This NULLs the ftrace function and in essence stops
  122. * tracing. There may be lag
  123. */
  124. void clear_ftrace_function(void)
  125. {
  126. ftrace_trace_function = ftrace_stub;
  127. __ftrace_trace_function = ftrace_stub;
  128. __ftrace_trace_function_delay = ftrace_stub;
  129. ftrace_pid_function = ftrace_stub;
  130. }
  131. #undef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  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. enum {
  781. FTRACE_ENABLE_CALLS = (1 << 0),
  782. FTRACE_DISABLE_CALLS = (1 << 1),
  783. FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
  784. FTRACE_START_FUNC_RET = (1 << 3),
  785. FTRACE_STOP_FUNC_RET = (1 << 4),
  786. };
  787. struct ftrace_func_entry {
  788. struct hlist_node hlist;
  789. unsigned long ip;
  790. };
  791. struct ftrace_hash {
  792. unsigned long size_bits;
  793. struct hlist_head *buckets;
  794. unsigned long count;
  795. struct rcu_head rcu;
  796. };
  797. /*
  798. * We make these constant because no one should touch them,
  799. * but they are used as the default "empty hash", to avoid allocating
  800. * it all the time. These are in a read only section such that if
  801. * anyone does try to modify it, it will cause an exception.
  802. */
  803. static const struct hlist_head empty_buckets[1];
  804. static const struct ftrace_hash empty_hash = {
  805. .buckets = (struct hlist_head *)empty_buckets,
  806. };
  807. #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
  808. static struct ftrace_ops global_ops = {
  809. .func = ftrace_stub,
  810. .notrace_hash = EMPTY_HASH,
  811. .filter_hash = EMPTY_HASH,
  812. };
  813. static struct dyn_ftrace *ftrace_new_addrs;
  814. static DEFINE_MUTEX(ftrace_regex_lock);
  815. struct ftrace_page {
  816. struct ftrace_page *next;
  817. int index;
  818. struct dyn_ftrace records[];
  819. };
  820. #define ENTRIES_PER_PAGE \
  821. ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
  822. /* estimate from running different kernels */
  823. #define NR_TO_INIT 10000
  824. static struct ftrace_page *ftrace_pages_start;
  825. static struct ftrace_page *ftrace_pages;
  826. static struct dyn_ftrace *ftrace_free_records;
  827. static struct ftrace_func_entry *
  828. ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
  829. {
  830. unsigned long key;
  831. struct ftrace_func_entry *entry;
  832. struct hlist_head *hhd;
  833. struct hlist_node *n;
  834. if (!hash->count)
  835. return NULL;
  836. if (hash->size_bits > 0)
  837. key = hash_long(ip, hash->size_bits);
  838. else
  839. key = 0;
  840. hhd = &hash->buckets[key];
  841. hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
  842. if (entry->ip == ip)
  843. return entry;
  844. }
  845. return NULL;
  846. }
  847. static void __add_hash_entry(struct ftrace_hash *hash,
  848. struct ftrace_func_entry *entry)
  849. {
  850. struct hlist_head *hhd;
  851. unsigned long key;
  852. if (hash->size_bits)
  853. key = hash_long(entry->ip, hash->size_bits);
  854. else
  855. key = 0;
  856. hhd = &hash->buckets[key];
  857. hlist_add_head(&entry->hlist, hhd);
  858. hash->count++;
  859. }
  860. static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
  861. {
  862. struct ftrace_func_entry *entry;
  863. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  864. if (!entry)
  865. return -ENOMEM;
  866. entry->ip = ip;
  867. __add_hash_entry(hash, entry);
  868. return 0;
  869. }
  870. static void
  871. free_hash_entry(struct ftrace_hash *hash,
  872. struct ftrace_func_entry *entry)
  873. {
  874. hlist_del(&entry->hlist);
  875. kfree(entry);
  876. hash->count--;
  877. }
  878. static void
  879. remove_hash_entry(struct ftrace_hash *hash,
  880. struct ftrace_func_entry *entry)
  881. {
  882. hlist_del(&entry->hlist);
  883. hash->count--;
  884. }
  885. static void ftrace_hash_clear(struct ftrace_hash *hash)
  886. {
  887. struct hlist_head *hhd;
  888. struct hlist_node *tp, *tn;
  889. struct ftrace_func_entry *entry;
  890. int size = 1 << hash->size_bits;
  891. int i;
  892. if (!hash->count)
  893. return;
  894. for (i = 0; i < size; i++) {
  895. hhd = &hash->buckets[i];
  896. hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
  897. free_hash_entry(hash, entry);
  898. }
  899. FTRACE_WARN_ON(hash->count);
  900. }
  901. static void free_ftrace_hash(struct ftrace_hash *hash)
  902. {
  903. if (!hash || hash == EMPTY_HASH)
  904. return;
  905. ftrace_hash_clear(hash);
  906. kfree(hash->buckets);
  907. kfree(hash);
  908. }
  909. static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
  910. {
  911. struct ftrace_hash *hash;
  912. hash = container_of(rcu, struct ftrace_hash, rcu);
  913. free_ftrace_hash(hash);
  914. }
  915. static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
  916. {
  917. if (!hash || hash == EMPTY_HASH)
  918. return;
  919. call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
  920. }
  921. static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
  922. {
  923. struct ftrace_hash *hash;
  924. int size;
  925. hash = kzalloc(sizeof(*hash), GFP_KERNEL);
  926. if (!hash)
  927. return NULL;
  928. size = 1 << size_bits;
  929. hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);
  930. if (!hash->buckets) {
  931. kfree(hash);
  932. return NULL;
  933. }
  934. hash->size_bits = size_bits;
  935. return hash;
  936. }
  937. static struct ftrace_hash *
  938. alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
  939. {
  940. struct ftrace_func_entry *entry;
  941. struct ftrace_hash *new_hash;
  942. struct hlist_node *tp;
  943. int size;
  944. int ret;
  945. int i;
  946. new_hash = alloc_ftrace_hash(size_bits);
  947. if (!new_hash)
  948. return NULL;
  949. /* Empty hash? */
  950. if (!hash || !hash->count)
  951. return new_hash;
  952. size = 1 << hash->size_bits;
  953. for (i = 0; i < size; i++) {
  954. hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
  955. ret = add_hash_entry(new_hash, entry->ip);
  956. if (ret < 0)
  957. goto free_hash;
  958. }
  959. }
  960. FTRACE_WARN_ON(new_hash->count != hash->count);
  961. return new_hash;
  962. free_hash:
  963. free_ftrace_hash(new_hash);
  964. return NULL;
  965. }
  966. static void
  967. ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
  968. static void
  969. ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
  970. static int
  971. ftrace_hash_move(struct ftrace_ops *ops, int enable,
  972. struct ftrace_hash **dst, struct ftrace_hash *src)
  973. {
  974. struct ftrace_func_entry *entry;
  975. struct hlist_node *tp, *tn;
  976. struct hlist_head *hhd;
  977. struct ftrace_hash *old_hash;
  978. struct ftrace_hash *new_hash;
  979. unsigned long key;
  980. int size = src->count;
  981. int bits = 0;
  982. int ret;
  983. int i;
  984. /*
  985. * Remove the current set, update the hash and add
  986. * them back.
  987. */
  988. ftrace_hash_rec_disable(ops, enable);
  989. /*
  990. * If the new source is empty, just free dst and assign it
  991. * the empty_hash.
  992. */
  993. if (!src->count) {
  994. free_ftrace_hash_rcu(*dst);
  995. rcu_assign_pointer(*dst, EMPTY_HASH);
  996. return 0;
  997. }
  998. /*
  999. * Make the hash size about 1/2 the # found
  1000. */
  1001. for (size /= 2; size; size >>= 1)
  1002. bits++;
  1003. /* Don't allocate too much */
  1004. if (bits > FTRACE_HASH_MAX_BITS)
  1005. bits = FTRACE_HASH_MAX_BITS;
  1006. ret = -ENOMEM;
  1007. new_hash = alloc_ftrace_hash(bits);
  1008. if (!new_hash)
  1009. goto out;
  1010. size = 1 << src->size_bits;
  1011. for (i = 0; i < size; i++) {
  1012. hhd = &src->buckets[i];
  1013. hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
  1014. if (bits > 0)
  1015. key = hash_long(entry->ip, bits);
  1016. else
  1017. key = 0;
  1018. remove_hash_entry(src, entry);
  1019. __add_hash_entry(new_hash, entry);
  1020. }
  1021. }
  1022. old_hash = *dst;
  1023. rcu_assign_pointer(*dst, new_hash);
  1024. free_ftrace_hash_rcu(old_hash);
  1025. ret = 0;
  1026. out:
  1027. /*
  1028. * Enable regardless of ret:
  1029. * On success, we enable the new hash.
  1030. * On failure, we re-enable the original hash.
  1031. */
  1032. ftrace_hash_rec_enable(ops, enable);
  1033. return ret;
  1034. }
  1035. /*
  1036. * Test the hashes for this ops to see if we want to call
  1037. * the ops->func or not.
  1038. *
  1039. * It's a match if the ip is in the ops->filter_hash or
  1040. * the filter_hash does not exist or is empty,
  1041. * AND
  1042. * the ip is not in the ops->notrace_hash.
  1043. *
  1044. * This needs to be called with preemption disabled as
  1045. * the hashes are freed with call_rcu_sched().
  1046. */
  1047. static int
  1048. ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
  1049. {
  1050. struct ftrace_hash *filter_hash;
  1051. struct ftrace_hash *notrace_hash;
  1052. int ret;
  1053. filter_hash = rcu_dereference_raw(ops->filter_hash);
  1054. notrace_hash = rcu_dereference_raw(ops->notrace_hash);
  1055. if ((!filter_hash || !filter_hash->count ||
  1056. ftrace_lookup_ip(filter_hash, ip)) &&
  1057. (!notrace_hash || !notrace_hash->count ||
  1058. !ftrace_lookup_ip(notrace_hash, ip)))
  1059. ret = 1;
  1060. else
  1061. ret = 0;
  1062. return ret;
  1063. }
  1064. /*
  1065. * This is a double for. Do not use 'break' to break out of the loop,
  1066. * you must use a goto.
  1067. */
  1068. #define do_for_each_ftrace_rec(pg, rec) \
  1069. for (pg = ftrace_pages_start; pg; pg = pg->next) { \
  1070. int _____i; \
  1071. for (_____i = 0; _____i < pg->index; _____i++) { \
  1072. rec = &pg->records[_____i];
  1073. #define while_for_each_ftrace_rec() \
  1074. } \
  1075. }
  1076. static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
  1077. int filter_hash,
  1078. bool inc)
  1079. {
  1080. struct ftrace_hash *hash;
  1081. struct ftrace_hash *other_hash;
  1082. struct ftrace_page *pg;
  1083. struct dyn_ftrace *rec;
  1084. int count = 0;
  1085. int all = 0;
  1086. /* Only update if the ops has been registered */
  1087. if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
  1088. return;
  1089. /*
  1090. * In the filter_hash case:
  1091. * If the count is zero, we update all records.
  1092. * Otherwise we just update the items in the hash.
  1093. *
  1094. * In the notrace_hash case:
  1095. * We enable the update in the hash.
  1096. * As disabling notrace means enabling the tracing,
  1097. * and enabling notrace means disabling, the inc variable
  1098. * gets inversed.
  1099. */
  1100. if (filter_hash) {
  1101. hash = ops->filter_hash;
  1102. other_hash = ops->notrace_hash;
  1103. if (!hash || !hash->count)
  1104. all = 1;
  1105. } else {
  1106. inc = !inc;
  1107. hash = ops->notrace_hash;
  1108. other_hash = ops->filter_hash;
  1109. /*
  1110. * If the notrace hash has no items,
  1111. * then there's nothing to do.
  1112. */
  1113. if (hash && !hash->count)
  1114. return;
  1115. }
  1116. do_for_each_ftrace_rec(pg, rec) {
  1117. int in_other_hash = 0;
  1118. int in_hash = 0;
  1119. int match = 0;
  1120. if (all) {
  1121. /*
  1122. * Only the filter_hash affects all records.
  1123. * Update if the record is not in the notrace hash.
  1124. */
  1125. if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
  1126. match = 1;
  1127. } else {
  1128. in_hash = hash && !!ftrace_lookup_ip(hash, rec->ip);
  1129. in_other_hash = other_hash && !!ftrace_lookup_ip(other_hash, rec->ip);
  1130. /*
  1131. *
  1132. */
  1133. if (filter_hash && in_hash && !in_other_hash)
  1134. match = 1;
  1135. else if (!filter_hash && in_hash &&
  1136. (in_other_hash || !other_hash->count))
  1137. match = 1;
  1138. }
  1139. if (!match)
  1140. continue;
  1141. if (inc) {
  1142. rec->flags++;
  1143. if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
  1144. return;
  1145. } else {
  1146. if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
  1147. return;
  1148. rec->flags--;
  1149. }
  1150. count++;
  1151. /* Shortcut, if we handled all records, we are done. */
  1152. if (!all && count == hash->count)
  1153. return;
  1154. } while_for_each_ftrace_rec();
  1155. }
  1156. static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
  1157. int filter_hash)
  1158. {
  1159. __ftrace_hash_rec_update(ops, filter_hash, 0);
  1160. }
  1161. static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
  1162. int filter_hash)
  1163. {
  1164. __ftrace_hash_rec_update(ops, filter_hash, 1);
  1165. }
  1166. static void ftrace_free_rec(struct dyn_ftrace *rec)
  1167. {
  1168. rec->freelist = ftrace_free_records;
  1169. ftrace_free_records = rec;
  1170. rec->flags |= FTRACE_FL_FREE;
  1171. }
  1172. static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
  1173. {
  1174. struct dyn_ftrace *rec;
  1175. /* First check for freed records */
  1176. if (ftrace_free_records) {
  1177. rec = ftrace_free_records;
  1178. if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
  1179. FTRACE_WARN_ON_ONCE(1);
  1180. ftrace_free_records = NULL;
  1181. return NULL;
  1182. }
  1183. ftrace_free_records = rec->freelist;
  1184. memset(rec, 0, sizeof(*rec));
  1185. return rec;
  1186. }
  1187. if (ftrace_pages->index == ENTRIES_PER_PAGE) {
  1188. if (!ftrace_pages->next) {
  1189. /* allocate another page */
  1190. ftrace_pages->next =
  1191. (void *)get_zeroed_page(GFP_KERNEL);
  1192. if (!ftrace_pages->next)
  1193. return NULL;
  1194. }
  1195. ftrace_pages = ftrace_pages->next;
  1196. }
  1197. return &ftrace_pages->records[ftrace_pages->index++];
  1198. }
  1199. static struct dyn_ftrace *
  1200. ftrace_record_ip(unsigned long ip)
  1201. {
  1202. struct dyn_ftrace *rec;
  1203. if (ftrace_disabled)
  1204. return NULL;
  1205. rec = ftrace_alloc_dyn_node(ip);
  1206. if (!rec)
  1207. return NULL;
  1208. rec->ip = ip;
  1209. rec->newlist = ftrace_new_addrs;
  1210. ftrace_new_addrs = rec;
  1211. return rec;
  1212. }
  1213. static void print_ip_ins(const char *fmt, unsigned char *p)
  1214. {
  1215. int i;
  1216. printk(KERN_CONT "%s", fmt);
  1217. for (i = 0; i < MCOUNT_INSN_SIZE; i++)
  1218. printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
  1219. }
  1220. static void ftrace_bug(int failed, unsigned long ip)
  1221. {
  1222. switch (failed) {
  1223. case -EFAULT:
  1224. FTRACE_WARN_ON_ONCE(1);
  1225. pr_info("ftrace faulted on modifying ");
  1226. print_ip_sym(ip);
  1227. break;
  1228. case -EINVAL:
  1229. FTRACE_WARN_ON_ONCE(1);
  1230. pr_info("ftrace failed to modify ");
  1231. print_ip_sym(ip);
  1232. print_ip_ins(" actual: ", (unsigned char *)ip);
  1233. printk(KERN_CONT "\n");
  1234. break;
  1235. case -EPERM:
  1236. FTRACE_WARN_ON_ONCE(1);
  1237. pr_info("ftrace faulted on writing ");
  1238. print_ip_sym(ip);
  1239. break;
  1240. default:
  1241. FTRACE_WARN_ON_ONCE(1);
  1242. pr_info("ftrace faulted on unknown error ");
  1243. print_ip_sym(ip);
  1244. }
  1245. }
  1246. /* Return 1 if the address range is reserved for ftrace */
  1247. int ftrace_text_reserved(void *start, void *end)
  1248. {
  1249. struct dyn_ftrace *rec;
  1250. struct ftrace_page *pg;
  1251. do_for_each_ftrace_rec(pg, rec) {
  1252. if (rec->ip <= (unsigned long)end &&
  1253. rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
  1254. return 1;
  1255. } while_for_each_ftrace_rec();
  1256. return 0;
  1257. }
  1258. static int
  1259. __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
  1260. {
  1261. unsigned long ftrace_addr;
  1262. unsigned long flag = 0UL;
  1263. ftrace_addr = (unsigned long)FTRACE_ADDR;
  1264. /*
  1265. * If we are enabling tracing:
  1266. *
  1267. * If the record has a ref count, then we need to enable it
  1268. * because someone is using it.
  1269. *
  1270. * Otherwise we make sure its disabled.
  1271. *
  1272. * If we are disabling tracing, then disable all records that
  1273. * are enabled.
  1274. */
  1275. if (enable && (rec->flags & ~FTRACE_FL_MASK))
  1276. flag = FTRACE_FL_ENABLED;
  1277. /* If the state of this record hasn't changed, then do nothing */
  1278. if ((rec->flags & FTRACE_FL_ENABLED) == flag)
  1279. return 0;
  1280. if (flag) {
  1281. rec->flags |= FTRACE_FL_ENABLED;
  1282. return ftrace_make_call(rec, ftrace_addr);
  1283. }
  1284. rec->flags &= ~FTRACE_FL_ENABLED;
  1285. return ftrace_make_nop(NULL, rec, ftrace_addr);
  1286. }
  1287. static void ftrace_replace_code(int enable)
  1288. {
  1289. struct dyn_ftrace *rec;
  1290. struct ftrace_page *pg;
  1291. int failed;
  1292. if (unlikely(ftrace_disabled))
  1293. return;
  1294. do_for_each_ftrace_rec(pg, rec) {
  1295. /* Skip over free records */
  1296. if (rec->flags & FTRACE_FL_FREE)
  1297. continue;
  1298. failed = __ftrace_replace_code(rec, enable);
  1299. if (failed) {
  1300. ftrace_bug(failed, rec->ip);
  1301. /* Stop processing */
  1302. return;
  1303. }
  1304. } while_for_each_ftrace_rec();
  1305. }
  1306. static int
  1307. ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
  1308. {
  1309. unsigned long ip;
  1310. int ret;
  1311. ip = rec->ip;
  1312. if (unlikely(ftrace_disabled))
  1313. return 0;
  1314. ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
  1315. if (ret) {
  1316. ftrace_bug(ret, ip);
  1317. return 0;
  1318. }
  1319. return 1;
  1320. }
  1321. /*
  1322. * archs can override this function if they must do something
  1323. * before the modifying code is performed.
  1324. */
  1325. int __weak ftrace_arch_code_modify_prepare(void)
  1326. {
  1327. return 0;
  1328. }
  1329. /*
  1330. * archs can override this function if they must do something
  1331. * after the modifying code is performed.
  1332. */
  1333. int __weak ftrace_arch_code_modify_post_process(void)
  1334. {
  1335. return 0;
  1336. }
  1337. static int __ftrace_modify_code(void *data)
  1338. {
  1339. int *command = data;
  1340. /*
  1341. * Do not call function tracer while we update the code.
  1342. * We are in stop machine, no worrying about races.
  1343. */
  1344. function_trace_stop++;
  1345. if (*command & FTRACE_ENABLE_CALLS)
  1346. ftrace_replace_code(1);
  1347. else if (*command & FTRACE_DISABLE_CALLS)
  1348. ftrace_replace_code(0);
  1349. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  1350. ftrace_update_ftrace_func(ftrace_trace_function);
  1351. if (*command & FTRACE_START_FUNC_RET)
  1352. ftrace_enable_ftrace_graph_caller();
  1353. else if (*command & FTRACE_STOP_FUNC_RET)
  1354. ftrace_disable_ftrace_graph_caller();
  1355. #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  1356. /*
  1357. * For archs that call ftrace_test_stop_func(), we must
  1358. * wait till after we update all the function callers
  1359. * before we update the callback. This keeps different
  1360. * ops that record different functions from corrupting
  1361. * each other.
  1362. */
  1363. __ftrace_trace_function = __ftrace_trace_function_delay;
  1364. #endif
  1365. function_trace_stop--;
  1366. return 0;
  1367. }
  1368. static void ftrace_run_update_code(int command)
  1369. {
  1370. int ret;
  1371. ret = ftrace_arch_code_modify_prepare();
  1372. FTRACE_WARN_ON(ret);
  1373. if (ret)
  1374. return;
  1375. stop_machine(__ftrace_modify_code, &command, NULL);
  1376. ret = ftrace_arch_code_modify_post_process();
  1377. FTRACE_WARN_ON(ret);
  1378. }
  1379. static ftrace_func_t saved_ftrace_func;
  1380. static int ftrace_start_up;
  1381. static int global_start_up;
  1382. static void ftrace_startup_enable(int command)
  1383. {
  1384. if (saved_ftrace_func != ftrace_trace_function) {
  1385. saved_ftrace_func = ftrace_trace_function;
  1386. command |= FTRACE_UPDATE_TRACE_FUNC;
  1387. }
  1388. if (!command || !ftrace_enabled)
  1389. return;
  1390. ftrace_run_update_code(command);
  1391. }
  1392. static int ftrace_startup(struct ftrace_ops *ops, int command)
  1393. {
  1394. bool hash_enable = true;
  1395. if (unlikely(ftrace_disabled))
  1396. return -ENODEV;
  1397. ftrace_start_up++;
  1398. command |= FTRACE_ENABLE_CALLS;
  1399. /* ops marked global share the filter hashes */
  1400. if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
  1401. ops = &global_ops;
  1402. /* Don't update hash if global is already set */
  1403. if (global_start_up)
  1404. hash_enable = false;
  1405. global_start_up++;
  1406. }
  1407. ops->flags |= FTRACE_OPS_FL_ENABLED;
  1408. if (hash_enable)
  1409. ftrace_hash_rec_enable(ops, 1);
  1410. ftrace_startup_enable(command);
  1411. return 0;
  1412. }
  1413. static void ftrace_shutdown(struct ftrace_ops *ops, int command)
  1414. {
  1415. bool hash_disable = true;
  1416. if (unlikely(ftrace_disabled))
  1417. return;
  1418. ftrace_start_up--;
  1419. /*
  1420. * Just warn in case of unbalance, no need to kill ftrace, it's not
  1421. * critical but the ftrace_call callers may be never nopped again after
  1422. * further ftrace uses.
  1423. */
  1424. WARN_ON_ONCE(ftrace_start_up < 0);
  1425. if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
  1426. ops = &global_ops;
  1427. global_start_up--;
  1428. WARN_ON_ONCE(global_start_up < 0);
  1429. /* Don't update hash if global still has users */
  1430. if (global_start_up) {
  1431. WARN_ON_ONCE(!ftrace_start_up);
  1432. hash_disable = false;
  1433. }
  1434. }
  1435. if (hash_disable)
  1436. ftrace_hash_rec_disable(ops, 1);
  1437. if (ops != &global_ops || !global_start_up)
  1438. ops->flags &= ~FTRACE_OPS_FL_ENABLED;
  1439. if (!ftrace_start_up)
  1440. command |= FTRACE_DISABLE_CALLS;
  1441. if (saved_ftrace_func != ftrace_trace_function) {
  1442. saved_ftrace_func = ftrace_trace_function;
  1443. command |= FTRACE_UPDATE_TRACE_FUNC;
  1444. }
  1445. if (!command || !ftrace_enabled)
  1446. return;
  1447. ftrace_run_update_code(command);
  1448. }
  1449. static void ftrace_startup_sysctl(void)
  1450. {
  1451. if (unlikely(ftrace_disabled))
  1452. return;
  1453. /* Force update next time */
  1454. saved_ftrace_func = NULL;
  1455. /* ftrace_start_up is true if we want ftrace running */
  1456. if (ftrace_start_up)
  1457. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  1458. }
  1459. static void ftrace_shutdown_sysctl(void)
  1460. {
  1461. if (unlikely(ftrace_disabled))
  1462. return;
  1463. /* ftrace_start_up is true if ftrace is running */
  1464. if (ftrace_start_up)
  1465. ftrace_run_update_code(FTRACE_DISABLE_CALLS);
  1466. }
  1467. static cycle_t ftrace_update_time;
  1468. static unsigned long ftrace_update_cnt;
  1469. unsigned long ftrace_update_tot_cnt;
  1470. static int ops_traces_mod(struct ftrace_ops *ops)
  1471. {
  1472. struct ftrace_hash *hash;
  1473. hash = ops->filter_hash;
  1474. return !!(!hash || !hash->count);
  1475. }
  1476. static int ftrace_update_code(struct module *mod)
  1477. {
  1478. struct dyn_ftrace *p;
  1479. cycle_t start, stop;
  1480. unsigned long ref = 0;
  1481. /*
  1482. * When adding a module, we need to check if tracers are
  1483. * currently enabled and if they are set to trace all functions.
  1484. * If they are, we need to enable the module functions as well
  1485. * as update the reference counts for those function records.
  1486. */
  1487. if (mod) {
  1488. struct ftrace_ops *ops;
  1489. for (ops = ftrace_ops_list;
  1490. ops != &ftrace_list_end; ops = ops->next) {
  1491. if (ops->flags & FTRACE_OPS_FL_ENABLED &&
  1492. ops_traces_mod(ops))
  1493. ref++;
  1494. }
  1495. }
  1496. start = ftrace_now(raw_smp_processor_id());
  1497. ftrace_update_cnt = 0;
  1498. while (ftrace_new_addrs) {
  1499. /* If something went wrong, bail without enabling anything */
  1500. if (unlikely(ftrace_disabled))
  1501. return -1;
  1502. p = ftrace_new_addrs;
  1503. ftrace_new_addrs = p->newlist;
  1504. p->flags = ref;
  1505. /*
  1506. * Do the initial record conversion from mcount jump
  1507. * to the NOP instructions.
  1508. */
  1509. if (!ftrace_code_disable(mod, p)) {
  1510. ftrace_free_rec(p);
  1511. /* Game over */
  1512. break;
  1513. }
  1514. ftrace_update_cnt++;
  1515. /*
  1516. * If the tracing is enabled, go ahead and enable the record.
  1517. *
  1518. * The reason not to enable the record immediatelly is the
  1519. * inherent check of ftrace_make_nop/ftrace_make_call for
  1520. * correct previous instructions. Making first the NOP
  1521. * conversion puts the module to the correct state, thus
  1522. * passing the ftrace_make_call check.
  1523. */
  1524. if (ftrace_start_up && ref) {
  1525. int failed = __ftrace_replace_code(p, 1);
  1526. if (failed) {
  1527. ftrace_bug(failed, p->ip);
  1528. ftrace_free_rec(p);
  1529. }
  1530. }
  1531. }
  1532. stop = ftrace_now(raw_smp_processor_id());
  1533. ftrace_update_time = stop - start;
  1534. ftrace_update_tot_cnt += ftrace_update_cnt;
  1535. return 0;
  1536. }
  1537. static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
  1538. {
  1539. struct ftrace_page *pg;
  1540. int cnt;
  1541. int i;
  1542. /* allocate a few pages */
  1543. ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
  1544. if (!ftrace_pages_start)
  1545. return -1;
  1546. /*
  1547. * Allocate a few more pages.
  1548. *
  1549. * TODO: have some parser search vmlinux before
  1550. * final linking to find all calls to ftrace.
  1551. * Then we can:
  1552. * a) know how many pages to allocate.
  1553. * and/or
  1554. * b) set up the table then.
  1555. *
  1556. * The dynamic code is still necessary for
  1557. * modules.
  1558. */
  1559. pg = ftrace_pages = ftrace_pages_start;
  1560. cnt = num_to_init / ENTRIES_PER_PAGE;
  1561. pr_info("ftrace: allocating %ld entries in %d pages\n",
  1562. num_to_init, cnt + 1);
  1563. for (i = 0; i < cnt; i++) {
  1564. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  1565. /* If we fail, we'll try later anyway */
  1566. if (!pg->next)
  1567. break;
  1568. pg = pg->next;
  1569. }
  1570. return 0;
  1571. }
  1572. enum {
  1573. FTRACE_ITER_FILTER = (1 << 0),
  1574. FTRACE_ITER_NOTRACE = (1 << 1),
  1575. FTRACE_ITER_PRINTALL = (1 << 2),
  1576. FTRACE_ITER_HASH = (1 << 3),
  1577. FTRACE_ITER_ENABLED = (1 << 4),
  1578. };
  1579. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  1580. struct ftrace_iterator {
  1581. loff_t pos;
  1582. loff_t func_pos;
  1583. struct ftrace_page *pg;
  1584. struct dyn_ftrace *func;
  1585. struct ftrace_func_probe *probe;
  1586. struct trace_parser parser;
  1587. struct ftrace_hash *hash;
  1588. struct ftrace_ops *ops;
  1589. int hidx;
  1590. int idx;
  1591. unsigned flags;
  1592. };
  1593. static void *
  1594. t_hash_next(struct seq_file *m, loff_t *pos)
  1595. {
  1596. struct ftrace_iterator *iter = m->private;
  1597. struct hlist_node *hnd = NULL;
  1598. struct hlist_head *hhd;
  1599. (*pos)++;
  1600. iter->pos = *pos;
  1601. if (iter->probe)
  1602. hnd = &iter->probe->node;
  1603. retry:
  1604. if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
  1605. return NULL;
  1606. hhd = &ftrace_func_hash[iter->hidx];
  1607. if (hlist_empty(hhd)) {
  1608. iter->hidx++;
  1609. hnd = NULL;
  1610. goto retry;
  1611. }
  1612. if (!hnd)
  1613. hnd = hhd->first;
  1614. else {
  1615. hnd = hnd->next;
  1616. if (!hnd) {
  1617. iter->hidx++;
  1618. goto retry;
  1619. }
  1620. }
  1621. if (WARN_ON_ONCE(!hnd))
  1622. return NULL;
  1623. iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
  1624. return iter;
  1625. }
  1626. static void *t_hash_start(struct seq_file *m, loff_t *pos)
  1627. {
  1628. struct ftrace_iterator *iter = m->private;
  1629. void *p = NULL;
  1630. loff_t l;
  1631. if (iter->func_pos > *pos)
  1632. return NULL;
  1633. iter->hidx = 0;
  1634. for (l = 0; l <= (*pos - iter->func_pos); ) {
  1635. p = t_hash_next(m, &l);
  1636. if (!p)
  1637. break;
  1638. }
  1639. if (!p)
  1640. return NULL;
  1641. /* Only set this if we have an item */
  1642. iter->flags |= FTRACE_ITER_HASH;
  1643. return iter;
  1644. }
  1645. static int
  1646. t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
  1647. {
  1648. struct ftrace_func_probe *rec;
  1649. rec = iter->probe;
  1650. if (WARN_ON_ONCE(!rec))
  1651. return -EIO;
  1652. if (rec->ops->print)
  1653. return rec->ops->print(m, rec->ip, rec->ops, rec->data);
  1654. seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
  1655. if (rec->data)
  1656. seq_printf(m, ":%p", rec->data);
  1657. seq_putc(m, '\n');
  1658. return 0;
  1659. }
  1660. static void *
  1661. t_next(struct seq_file *m, void *v, loff_t *pos)
  1662. {
  1663. struct ftrace_iterator *iter = m->private;
  1664. struct ftrace_ops *ops = &global_ops;
  1665. struct dyn_ftrace *rec = NULL;
  1666. if (unlikely(ftrace_disabled))
  1667. return NULL;
  1668. if (iter->flags & FTRACE_ITER_HASH)
  1669. return t_hash_next(m, pos);
  1670. (*pos)++;
  1671. iter->pos = iter->func_pos = *pos;
  1672. if (iter->flags & FTRACE_ITER_PRINTALL)
  1673. return t_hash_start(m, pos);
  1674. retry:
  1675. if (iter->idx >= iter->pg->index) {
  1676. if (iter->pg->next) {
  1677. iter->pg = iter->pg->next;
  1678. iter->idx = 0;
  1679. goto retry;
  1680. }
  1681. } else {
  1682. rec = &iter->pg->records[iter->idx++];
  1683. if ((rec->flags & FTRACE_FL_FREE) ||
  1684. ((iter->flags & FTRACE_ITER_FILTER) &&
  1685. !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
  1686. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  1687. !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
  1688. ((iter->flags & FTRACE_ITER_ENABLED) &&
  1689. !(rec->flags & ~FTRACE_FL_MASK))) {
  1690. rec = NULL;
  1691. goto retry;
  1692. }
  1693. }
  1694. if (!rec)
  1695. return t_hash_start(m, pos);
  1696. iter->func = rec;
  1697. return iter;
  1698. }
  1699. static void reset_iter_read(struct ftrace_iterator *iter)
  1700. {
  1701. iter->pos = 0;
  1702. iter->func_pos = 0;
  1703. iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
  1704. }
  1705. static void *t_start(struct seq_file *m, loff_t *pos)
  1706. {
  1707. struct ftrace_iterator *iter = m->private;
  1708. struct ftrace_ops *ops = &global_ops;
  1709. void *p = NULL;
  1710. loff_t l;
  1711. mutex_lock(&ftrace_lock);
  1712. if (unlikely(ftrace_disabled))
  1713. return NULL;
  1714. /*
  1715. * If an lseek was done, then reset and start from beginning.
  1716. */
  1717. if (*pos < iter->pos)
  1718. reset_iter_read(iter);
  1719. /*
  1720. * For set_ftrace_filter reading, if we have the filter
  1721. * off, we can short cut and just print out that all
  1722. * functions are enabled.
  1723. */
  1724. if (iter->flags & FTRACE_ITER_FILTER && !ops->filter_hash->count) {
  1725. if (*pos > 0)
  1726. return t_hash_start(m, pos);
  1727. iter->flags |= FTRACE_ITER_PRINTALL;
  1728. /* reset in case of seek/pread */
  1729. iter->flags &= ~FTRACE_ITER_HASH;
  1730. return iter;
  1731. }
  1732. if (iter->flags & FTRACE_ITER_HASH)
  1733. return t_hash_start(m, pos);
  1734. /*
  1735. * Unfortunately, we need to restart at ftrace_pages_start
  1736. * every time we let go of the ftrace_mutex. This is because
  1737. * those pointers can change without the lock.
  1738. */
  1739. iter->pg = ftrace_pages_start;
  1740. iter->idx = 0;
  1741. for (l = 0; l <= *pos; ) {
  1742. p = t_next(m, p, &l);
  1743. if (!p)
  1744. break;
  1745. }
  1746. if (!p) {
  1747. if (iter->flags & FTRACE_ITER_FILTER)
  1748. return t_hash_start(m, pos);
  1749. return NULL;
  1750. }
  1751. return iter;
  1752. }
  1753. static void t_stop(struct seq_file *m, void *p)
  1754. {
  1755. mutex_unlock(&ftrace_lock);
  1756. }
  1757. static int t_show(struct seq_file *m, void *v)
  1758. {
  1759. struct ftrace_iterator *iter = m->private;
  1760. struct dyn_ftrace *rec;
  1761. if (iter->flags & FTRACE_ITER_HASH)
  1762. return t_hash_show(m, iter);
  1763. if (iter->flags & FTRACE_ITER_PRINTALL) {
  1764. seq_printf(m, "#### all functions enabled ####\n");
  1765. return 0;
  1766. }
  1767. rec = iter->func;
  1768. if (!rec)
  1769. return 0;
  1770. seq_printf(m, "%ps", (void *)rec->ip);
  1771. if (iter->flags & FTRACE_ITER_ENABLED)
  1772. seq_printf(m, " (%ld)",
  1773. rec->flags & ~FTRACE_FL_MASK);
  1774. seq_printf(m, "\n");
  1775. return 0;
  1776. }
  1777. static const struct seq_operations show_ftrace_seq_ops = {
  1778. .start = t_start,
  1779. .next = t_next,
  1780. .stop = t_stop,
  1781. .show = t_show,
  1782. };
  1783. static int
  1784. ftrace_avail_open(struct inode *inode, struct file *file)
  1785. {
  1786. struct ftrace_iterator *iter;
  1787. int ret;
  1788. if (unlikely(ftrace_disabled))
  1789. return -ENODEV;
  1790. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1791. if (!iter)
  1792. return -ENOMEM;
  1793. iter->pg = ftrace_pages_start;
  1794. ret = seq_open(file, &show_ftrace_seq_ops);
  1795. if (!ret) {
  1796. struct seq_file *m = file->private_data;
  1797. m->private = iter;
  1798. } else {
  1799. kfree(iter);
  1800. }
  1801. return ret;
  1802. }
  1803. static int
  1804. ftrace_enabled_open(struct inode *inode, struct file *file)
  1805. {
  1806. struct ftrace_iterator *iter;
  1807. int ret;
  1808. if (unlikely(ftrace_disabled))
  1809. return -ENODEV;
  1810. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1811. if (!iter)
  1812. return -ENOMEM;
  1813. iter->pg = ftrace_pages_start;
  1814. iter->flags = FTRACE_ITER_ENABLED;
  1815. ret = seq_open(file, &show_ftrace_seq_ops);
  1816. if (!ret) {
  1817. struct seq_file *m = file->private_data;
  1818. m->private = iter;
  1819. } else {
  1820. kfree(iter);
  1821. }
  1822. return ret;
  1823. }
  1824. static void ftrace_filter_reset(struct ftrace_hash *hash)
  1825. {
  1826. mutex_lock(&ftrace_lock);
  1827. ftrace_hash_clear(hash);
  1828. mutex_unlock(&ftrace_lock);
  1829. }
  1830. static int
  1831. ftrace_regex_open(struct ftrace_ops *ops, int flag,
  1832. struct inode *inode, struct file *file)
  1833. {
  1834. struct ftrace_iterator *iter;
  1835. struct ftrace_hash *hash;
  1836. int ret = 0;
  1837. if (unlikely(ftrace_disabled))
  1838. return -ENODEV;
  1839. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1840. if (!iter)
  1841. return -ENOMEM;
  1842. if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
  1843. kfree(iter);
  1844. return -ENOMEM;
  1845. }
  1846. if (flag & FTRACE_ITER_NOTRACE)
  1847. hash = ops->notrace_hash;
  1848. else
  1849. hash = ops->filter_hash;
  1850. iter->ops = ops;
  1851. iter->flags = flag;
  1852. if (file->f_mode & FMODE_WRITE) {
  1853. mutex_lock(&ftrace_lock);
  1854. iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
  1855. mutex_unlock(&ftrace_lock);
  1856. if (!iter->hash) {
  1857. trace_parser_put(&iter->parser);
  1858. kfree(iter);
  1859. return -ENOMEM;
  1860. }
  1861. }
  1862. mutex_lock(&ftrace_regex_lock);
  1863. if ((file->f_mode & FMODE_WRITE) &&
  1864. (file->f_flags & O_TRUNC))
  1865. ftrace_filter_reset(iter->hash);
  1866. if (file->f_mode & FMODE_READ) {
  1867. iter->pg = ftrace_pages_start;
  1868. ret = seq_open(file, &show_ftrace_seq_ops);
  1869. if (!ret) {
  1870. struct seq_file *m = file->private_data;
  1871. m->private = iter;
  1872. } else {
  1873. /* Failed */
  1874. free_ftrace_hash(iter->hash);
  1875. trace_parser_put(&iter->parser);
  1876. kfree(iter);
  1877. }
  1878. } else
  1879. file->private_data = iter;
  1880. mutex_unlock(&ftrace_regex_lock);
  1881. return ret;
  1882. }
  1883. static int
  1884. ftrace_filter_open(struct inode *inode, struct file *file)
  1885. {
  1886. return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER,
  1887. inode, file);
  1888. }
  1889. static int
  1890. ftrace_notrace_open(struct inode *inode, struct file *file)
  1891. {
  1892. return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
  1893. inode, file);
  1894. }
  1895. static loff_t
  1896. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  1897. {
  1898. loff_t ret;
  1899. if (file->f_mode & FMODE_READ)
  1900. ret = seq_lseek(file, offset, origin);
  1901. else
  1902. file->f_pos = ret = 1;
  1903. return ret;
  1904. }
  1905. static int ftrace_match(char *str, char *regex, int len, int type)
  1906. {
  1907. int matched = 0;
  1908. int slen;
  1909. switch (type) {
  1910. case MATCH_FULL:
  1911. if (strcmp(str, regex) == 0)
  1912. matched = 1;
  1913. break;
  1914. case MATCH_FRONT_ONLY:
  1915. if (strncmp(str, regex, len) == 0)
  1916. matched = 1;
  1917. break;
  1918. case MATCH_MIDDLE_ONLY:
  1919. if (strstr(str, regex))
  1920. matched = 1;
  1921. break;
  1922. case MATCH_END_ONLY:
  1923. slen = strlen(str);
  1924. if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
  1925. matched = 1;
  1926. break;
  1927. }
  1928. return matched;
  1929. }
  1930. static int
  1931. enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
  1932. {
  1933. struct ftrace_func_entry *entry;
  1934. int ret = 0;
  1935. entry = ftrace_lookup_ip(hash, rec->ip);
  1936. if (not) {
  1937. /* Do nothing if it doesn't exist */
  1938. if (!entry)
  1939. return 0;
  1940. free_hash_entry(hash, entry);
  1941. } else {
  1942. /* Do nothing if it exists */
  1943. if (entry)
  1944. return 0;
  1945. ret = add_hash_entry(hash, rec->ip);
  1946. }
  1947. return ret;
  1948. }
  1949. static int
  1950. ftrace_match_record(struct dyn_ftrace *rec, char *mod,
  1951. char *regex, int len, int type)
  1952. {
  1953. char str[KSYM_SYMBOL_LEN];
  1954. char *modname;
  1955. kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
  1956. if (mod) {
  1957. /* module lookup requires matching the module */
  1958. if (!modname || strcmp(modname, mod))
  1959. return 0;
  1960. /* blank search means to match all funcs in the mod */
  1961. if (!len)
  1962. return 1;
  1963. }
  1964. return ftrace_match(str, regex, len, type);
  1965. }
  1966. static int
  1967. match_records(struct ftrace_hash *hash, char *buff,
  1968. int len, char *mod, int not)
  1969. {
  1970. unsigned search_len = 0;
  1971. struct ftrace_page *pg;
  1972. struct dyn_ftrace *rec;
  1973. int type = MATCH_FULL;
  1974. char *search = buff;
  1975. int found = 0;
  1976. int ret;
  1977. if (len) {
  1978. type = filter_parse_regex(buff, len, &search, &not);
  1979. search_len = strlen(search);
  1980. }
  1981. mutex_lock(&ftrace_lock);
  1982. if (unlikely(ftrace_disabled))
  1983. goto out_unlock;
  1984. do_for_each_ftrace_rec(pg, rec) {
  1985. if (ftrace_match_record(rec, mod, search, search_len, type)) {
  1986. ret = enter_record(hash, rec, not);
  1987. if (ret < 0) {
  1988. found = ret;
  1989. goto out_unlock;
  1990. }
  1991. found = 1;
  1992. }
  1993. } while_for_each_ftrace_rec();
  1994. out_unlock:
  1995. mutex_unlock(&ftrace_lock);
  1996. return found;
  1997. }
  1998. static int
  1999. ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
  2000. {
  2001. return match_records(hash, buff, len, NULL, 0);
  2002. }
  2003. static int
  2004. ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
  2005. {
  2006. int not = 0;
  2007. /* blank or '*' mean the same */
  2008. if (strcmp(buff, "*") == 0)
  2009. buff[0] = 0;
  2010. /* handle the case of 'dont filter this module' */
  2011. if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
  2012. buff[0] = 0;
  2013. not = 1;
  2014. }
  2015. return match_records(hash, buff, strlen(buff), mod, not);
  2016. }
  2017. /*
  2018. * We register the module command as a template to show others how
  2019. * to register the a command as well.
  2020. */
  2021. static int
  2022. ftrace_mod_callback(struct ftrace_hash *hash,
  2023. char *func, char *cmd, char *param, int enable)
  2024. {
  2025. char *mod;
  2026. int ret = -EINVAL;
  2027. /*
  2028. * cmd == 'mod' because we only registered this func
  2029. * for the 'mod' ftrace_func_command.
  2030. * But if you register one func with multiple commands,
  2031. * you can tell which command was used by the cmd
  2032. * parameter.
  2033. */
  2034. /* we must have a module name */
  2035. if (!param)
  2036. return ret;
  2037. mod = strsep(&param, ":");
  2038. if (!strlen(mod))
  2039. return ret;
  2040. ret = ftrace_match_module_records(hash, func, mod);
  2041. if (!ret)
  2042. ret = -EINVAL;
  2043. if (ret < 0)
  2044. return ret;
  2045. return 0;
  2046. }
  2047. static struct ftrace_func_command ftrace_mod_cmd = {
  2048. .name = "mod",
  2049. .func = ftrace_mod_callback,
  2050. };
  2051. static int __init ftrace_mod_cmd_init(void)
  2052. {
  2053. return register_ftrace_command(&ftrace_mod_cmd);
  2054. }
  2055. device_initcall(ftrace_mod_cmd_init);
  2056. static void
  2057. function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
  2058. {
  2059. struct ftrace_func_probe *entry;
  2060. struct hlist_head *hhd;
  2061. struct hlist_node *n;
  2062. unsigned long key;
  2063. key = hash_long(ip, FTRACE_HASH_BITS);
  2064. hhd = &ftrace_func_hash[key];
  2065. if (hlist_empty(hhd))
  2066. return;
  2067. /*
  2068. * Disable preemption for these calls to prevent a RCU grace
  2069. * period. This syncs the hash iteration and freeing of items
  2070. * on the hash. rcu_read_lock is too dangerous here.
  2071. */
  2072. preempt_disable_notrace();
  2073. hlist_for_each_entry_rcu(entry, n, hhd, node) {
  2074. if (entry->ip == ip)
  2075. entry->ops->func(ip, parent_ip, &entry->data);
  2076. }
  2077. preempt_enable_notrace();
  2078. }
  2079. static struct ftrace_ops trace_probe_ops __read_mostly =
  2080. {
  2081. .func = function_trace_probe_call,
  2082. };
  2083. static int ftrace_probe_registered;
  2084. static void __enable_ftrace_function_probe(void)
  2085. {
  2086. int ret;
  2087. int i;
  2088. if (ftrace_probe_registered)
  2089. return;
  2090. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  2091. struct hlist_head *hhd = &ftrace_func_hash[i];
  2092. if (hhd->first)
  2093. break;
  2094. }
  2095. /* Nothing registered? */
  2096. if (i == FTRACE_FUNC_HASHSIZE)
  2097. return;
  2098. ret = __register_ftrace_function(&trace_probe_ops);
  2099. if (!ret)
  2100. ret = ftrace_startup(&trace_probe_ops, 0);
  2101. ftrace_probe_registered = 1;
  2102. }
  2103. static void __disable_ftrace_function_probe(void)
  2104. {
  2105. int ret;
  2106. int i;
  2107. if (!ftrace_probe_registered)
  2108. return;
  2109. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  2110. struct hlist_head *hhd = &ftrace_func_hash[i];
  2111. if (hhd->first)
  2112. return;
  2113. }
  2114. /* no more funcs left */
  2115. ret = __unregister_ftrace_function(&trace_probe_ops);
  2116. if (!ret)
  2117. ftrace_shutdown(&trace_probe_ops, 0);
  2118. ftrace_probe_registered = 0;
  2119. }
  2120. static void ftrace_free_entry_rcu(struct rcu_head *rhp)
  2121. {
  2122. struct ftrace_func_probe *entry =
  2123. container_of(rhp, struct ftrace_func_probe, rcu);
  2124. if (entry->ops->free)
  2125. entry->ops->free(&entry->data);
  2126. kfree(entry);
  2127. }
  2128. int
  2129. register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  2130. void *data)
  2131. {
  2132. struct ftrace_func_probe *entry;
  2133. struct ftrace_page *pg;
  2134. struct dyn_ftrace *rec;
  2135. int type, len, not;
  2136. unsigned long key;
  2137. int count = 0;
  2138. char *search;
  2139. type = filter_parse_regex(glob, strlen(glob), &search, &not);
  2140. len = strlen(search);
  2141. /* we do not support '!' for function probes */
  2142. if (WARN_ON(not))
  2143. return -EINVAL;
  2144. mutex_lock(&ftrace_lock);
  2145. if (unlikely(ftrace_disabled))
  2146. goto out_unlock;
  2147. do_for_each_ftrace_rec(pg, rec) {
  2148. if (!ftrace_match_record(rec, NULL, search, len, type))
  2149. continue;
  2150. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  2151. if (!entry) {
  2152. /* If we did not process any, then return error */
  2153. if (!count)
  2154. count = -ENOMEM;
  2155. goto out_unlock;
  2156. }
  2157. count++;
  2158. entry->data = data;
  2159. /*
  2160. * The caller might want to do something special
  2161. * for each function we find. We call the callback
  2162. * to give the caller an opportunity to do so.
  2163. */
  2164. if (ops->callback) {
  2165. if (ops->callback(rec->ip, &entry->data) < 0) {
  2166. /* caller does not like this func */
  2167. kfree(entry);
  2168. continue;
  2169. }
  2170. }
  2171. entry->ops = ops;
  2172. entry->ip = rec->ip;
  2173. key = hash_long(entry->ip, FTRACE_HASH_BITS);
  2174. hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
  2175. } while_for_each_ftrace_rec();
  2176. __enable_ftrace_function_probe();
  2177. out_unlock:
  2178. mutex_unlock(&ftrace_lock);
  2179. return count;
  2180. }
  2181. enum {
  2182. PROBE_TEST_FUNC = 1,
  2183. PROBE_TEST_DATA = 2
  2184. };
  2185. static void
  2186. __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  2187. void *data, int flags)
  2188. {
  2189. struct ftrace_func_probe *entry;
  2190. struct hlist_node *n, *tmp;
  2191. char str[KSYM_SYMBOL_LEN];
  2192. int type = MATCH_FULL;
  2193. int i, len = 0;
  2194. char *search;
  2195. if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
  2196. glob = NULL;
  2197. else if (glob) {
  2198. int not;
  2199. type = filter_parse_regex(glob, strlen(glob), &search, &not);
  2200. len = strlen(search);
  2201. /* we do not support '!' for function probes */
  2202. if (WARN_ON(not))
  2203. return;
  2204. }
  2205. mutex_lock(&ftrace_lock);
  2206. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  2207. struct hlist_head *hhd = &ftrace_func_hash[i];
  2208. hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
  2209. /* break up if statements for readability */
  2210. if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
  2211. continue;
  2212. if ((flags & PROBE_TEST_DATA) && entry->data != data)
  2213. continue;
  2214. /* do this last, since it is the most expensive */
  2215. if (glob) {
  2216. kallsyms_lookup(entry->ip, NULL, NULL,
  2217. NULL, str);
  2218. if (!ftrace_match(str, glob, len, type))
  2219. continue;
  2220. }
  2221. hlist_del(&entry->node);
  2222. call_rcu(&entry->rcu, ftrace_free_entry_rcu);
  2223. }
  2224. }
  2225. __disable_ftrace_function_probe();
  2226. mutex_unlock(&ftrace_lock);
  2227. }
  2228. void
  2229. unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  2230. void *data)
  2231. {
  2232. __unregister_ftrace_function_probe(glob, ops, data,
  2233. PROBE_TEST_FUNC | PROBE_TEST_DATA);
  2234. }
  2235. void
  2236. unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
  2237. {
  2238. __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
  2239. }
  2240. void unregister_ftrace_function_probe_all(char *glob)
  2241. {
  2242. __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
  2243. }
  2244. static LIST_HEAD(ftrace_commands);
  2245. static DEFINE_MUTEX(ftrace_cmd_mutex);
  2246. int register_ftrace_command(struct ftrace_func_command *cmd)
  2247. {
  2248. struct ftrace_func_command *p;
  2249. int ret = 0;
  2250. mutex_lock(&ftrace_cmd_mutex);
  2251. list_for_each_entry(p, &ftrace_commands, list) {
  2252. if (strcmp(cmd->name, p->name) == 0) {
  2253. ret = -EBUSY;
  2254. goto out_unlock;
  2255. }
  2256. }
  2257. list_add(&cmd->list, &ftrace_commands);
  2258. out_unlock:
  2259. mutex_unlock(&ftrace_cmd_mutex);
  2260. return ret;
  2261. }
  2262. int unregister_ftrace_command(struct ftrace_func_command *cmd)
  2263. {
  2264. struct ftrace_func_command *p, *n;
  2265. int ret = -ENODEV;
  2266. mutex_lock(&ftrace_cmd_mutex);
  2267. list_for_each_entry_safe(p, n, &ftrace_commands, list) {
  2268. if (strcmp(cmd->name, p->name) == 0) {
  2269. ret = 0;
  2270. list_del_init(&p->list);
  2271. goto out_unlock;
  2272. }
  2273. }
  2274. out_unlock:
  2275. mutex_unlock(&ftrace_cmd_mutex);
  2276. return ret;
  2277. }
  2278. static int ftrace_process_regex(struct ftrace_hash *hash,
  2279. char *buff, int len, int enable)
  2280. {
  2281. char *func, *command, *next = buff;
  2282. struct ftrace_func_command *p;
  2283. int ret = -EINVAL;
  2284. func = strsep(&next, ":");
  2285. if (!next) {
  2286. ret = ftrace_match_records(hash, func, len);
  2287. if (!ret)
  2288. ret = -EINVAL;
  2289. if (ret < 0)
  2290. return ret;
  2291. return 0;
  2292. }
  2293. /* command found */
  2294. command = strsep(&next, ":");
  2295. mutex_lock(&ftrace_cmd_mutex);
  2296. list_for_each_entry(p, &ftrace_commands, list) {
  2297. if (strcmp(p->name, command) == 0) {
  2298. ret = p->func(hash, func, command, next, enable);
  2299. goto out_unlock;
  2300. }
  2301. }
  2302. out_unlock:
  2303. mutex_unlock(&ftrace_cmd_mutex);
  2304. return ret;
  2305. }
  2306. static ssize_t
  2307. ftrace_regex_write(struct file *file, const char __user *ubuf,
  2308. size_t cnt, loff_t *ppos, int enable)
  2309. {
  2310. struct ftrace_iterator *iter;
  2311. struct trace_parser *parser;
  2312. ssize_t ret, read;
  2313. if (!cnt)
  2314. return 0;
  2315. mutex_lock(&ftrace_regex_lock);
  2316. ret = -ENODEV;
  2317. if (unlikely(ftrace_disabled))
  2318. goto out_unlock;
  2319. if (file->f_mode & FMODE_READ) {
  2320. struct seq_file *m = file->private_data;
  2321. iter = m->private;
  2322. } else
  2323. iter = file->private_data;
  2324. parser = &iter->parser;
  2325. read = trace_get_user(parser, ubuf, cnt, ppos);
  2326. if (read >= 0 && trace_parser_loaded(parser) &&
  2327. !trace_parser_cont(parser)) {
  2328. ret = ftrace_process_regex(iter->hash, parser->buffer,
  2329. parser->idx, enable);
  2330. trace_parser_clear(parser);
  2331. if (ret)
  2332. goto out_unlock;
  2333. }
  2334. ret = read;
  2335. out_unlock:
  2336. mutex_unlock(&ftrace_regex_lock);
  2337. return ret;
  2338. }
  2339. static ssize_t
  2340. ftrace_filter_write(struct file *file, const char __user *ubuf,
  2341. size_t cnt, loff_t *ppos)
  2342. {
  2343. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  2344. }
  2345. static ssize_t
  2346. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  2347. size_t cnt, loff_t *ppos)
  2348. {
  2349. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  2350. }
  2351. static int
  2352. ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
  2353. int reset, int enable)
  2354. {
  2355. struct ftrace_hash **orig_hash;
  2356. struct ftrace_hash *hash;
  2357. int ret;
  2358. /* All global ops uses the global ops filters */
  2359. if (ops->flags & FTRACE_OPS_FL_GLOBAL)
  2360. ops = &global_ops;
  2361. if (unlikely(ftrace_disabled))
  2362. return -ENODEV;
  2363. if (enable)
  2364. orig_hash = &ops->filter_hash;
  2365. else
  2366. orig_hash = &ops->notrace_hash;
  2367. hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
  2368. if (!hash)
  2369. return -ENOMEM;
  2370. mutex_lock(&ftrace_regex_lock);
  2371. if (reset)
  2372. ftrace_filter_reset(hash);
  2373. if (buf)
  2374. ftrace_match_records(hash, buf, len);
  2375. mutex_lock(&ftrace_lock);
  2376. ret = ftrace_hash_move(ops, enable, orig_hash, hash);
  2377. if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
  2378. && ftrace_enabled)
  2379. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  2380. mutex_unlock(&ftrace_lock);
  2381. mutex_unlock(&ftrace_regex_lock);
  2382. free_ftrace_hash(hash);
  2383. return ret;
  2384. }
  2385. /**
  2386. * ftrace_set_filter - set a function to filter on in ftrace
  2387. * @ops - the ops to set the filter with
  2388. * @buf - the string that holds the function filter text.
  2389. * @len - the length of the string.
  2390. * @reset - non zero to reset all filters before applying this filter.
  2391. *
  2392. * Filters denote which functions should be enabled when tracing is enabled.
  2393. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  2394. */
  2395. void ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
  2396. int len, int reset)
  2397. {
  2398. ftrace_set_regex(ops, buf, len, reset, 1);
  2399. }
  2400. EXPORT_SYMBOL_GPL(ftrace_set_filter);
  2401. /**
  2402. * ftrace_set_notrace - set a function to not trace in ftrace
  2403. * @ops - the ops to set the notrace filter with
  2404. * @buf - the string that holds the function notrace text.
  2405. * @len - the length of the string.
  2406. * @reset - non zero to reset all filters before applying this filter.
  2407. *
  2408. * Notrace Filters denote which functions should not be enabled when tracing
  2409. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  2410. * for tracing.
  2411. */
  2412. void ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
  2413. int len, int reset)
  2414. {
  2415. ftrace_set_regex(ops, buf, len, reset, 0);
  2416. }
  2417. EXPORT_SYMBOL_GPL(ftrace_set_notrace);
  2418. /**
  2419. * ftrace_set_filter - set a function to filter on in ftrace
  2420. * @ops - the ops to set the filter with
  2421. * @buf - the string that holds the function filter text.
  2422. * @len - the length of the string.
  2423. * @reset - non zero to reset all filters before applying this filter.
  2424. *
  2425. * Filters denote which functions should be enabled when tracing is enabled.
  2426. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  2427. */
  2428. void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
  2429. {
  2430. ftrace_set_regex(&global_ops, buf, len, reset, 1);
  2431. }
  2432. EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
  2433. /**
  2434. * ftrace_set_notrace - set a function to not trace in ftrace
  2435. * @ops - the ops to set the notrace filter with
  2436. * @buf - the string that holds the function notrace text.
  2437. * @len - the length of the string.
  2438. * @reset - non zero to reset all filters before applying this filter.
  2439. *
  2440. * Notrace Filters denote which functions should not be enabled when tracing
  2441. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  2442. * for tracing.
  2443. */
  2444. void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
  2445. {
  2446. ftrace_set_regex(&global_ops, buf, len, reset, 0);
  2447. }
  2448. EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
  2449. /*
  2450. * command line interface to allow users to set filters on boot up.
  2451. */
  2452. #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
  2453. static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
  2454. static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
  2455. static int __init set_ftrace_notrace(char *str)
  2456. {
  2457. strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
  2458. return 1;
  2459. }
  2460. __setup("ftrace_notrace=", set_ftrace_notrace);
  2461. static int __init set_ftrace_filter(char *str)
  2462. {
  2463. strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
  2464. return 1;
  2465. }
  2466. __setup("ftrace_filter=", set_ftrace_filter);
  2467. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2468. static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
  2469. static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
  2470. static int __init set_graph_function(char *str)
  2471. {
  2472. strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
  2473. return 1;
  2474. }
  2475. __setup("ftrace_graph_filter=", set_graph_function);
  2476. static void __init set_ftrace_early_graph(char *buf)
  2477. {
  2478. int ret;
  2479. char *func;
  2480. while (buf) {
  2481. func = strsep(&buf, ",");
  2482. /* we allow only one expression at a time */
  2483. ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
  2484. func);
  2485. if (ret)
  2486. printk(KERN_DEBUG "ftrace: function %s not "
  2487. "traceable\n", func);
  2488. }
  2489. }
  2490. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2491. static void __init
  2492. set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable)
  2493. {
  2494. char *func;
  2495. while (buf) {
  2496. func = strsep(&buf, ",");
  2497. ftrace_set_regex(ops, func, strlen(func), 0, enable);
  2498. }
  2499. }
  2500. static void __init set_ftrace_early_filters(void)
  2501. {
  2502. if (ftrace_filter_buf[0])
  2503. set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1);
  2504. if (ftrace_notrace_buf[0])
  2505. set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0);
  2506. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2507. if (ftrace_graph_buf[0])
  2508. set_ftrace_early_graph(ftrace_graph_buf);
  2509. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2510. }
  2511. static int
  2512. ftrace_regex_release(struct inode *inode, struct file *file)
  2513. {
  2514. struct seq_file *m = (struct seq_file *)file->private_data;
  2515. struct ftrace_iterator *iter;
  2516. struct ftrace_hash **orig_hash;
  2517. struct trace_parser *parser;
  2518. int filter_hash;
  2519. int ret;
  2520. mutex_lock(&ftrace_regex_lock);
  2521. if (file->f_mode & FMODE_READ) {
  2522. iter = m->private;
  2523. seq_release(inode, file);
  2524. } else
  2525. iter = file->private_data;
  2526. parser = &iter->parser;
  2527. if (trace_parser_loaded(parser)) {
  2528. parser->buffer[parser->idx] = 0;
  2529. ftrace_match_records(iter->hash, parser->buffer, parser->idx);
  2530. }
  2531. trace_parser_put(parser);
  2532. if (file->f_mode & FMODE_WRITE) {
  2533. filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
  2534. if (filter_hash)
  2535. orig_hash = &iter->ops->filter_hash;
  2536. else
  2537. orig_hash = &iter->ops->notrace_hash;
  2538. mutex_lock(&ftrace_lock);
  2539. ret = ftrace_hash_move(iter->ops, filter_hash,
  2540. orig_hash, iter->hash);
  2541. if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
  2542. && ftrace_enabled)
  2543. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  2544. mutex_unlock(&ftrace_lock);
  2545. }
  2546. free_ftrace_hash(iter->hash);
  2547. kfree(iter);
  2548. mutex_unlock(&ftrace_regex_lock);
  2549. return 0;
  2550. }
  2551. static const struct file_operations ftrace_avail_fops = {
  2552. .open = ftrace_avail_open,
  2553. .read = seq_read,
  2554. .llseek = seq_lseek,
  2555. .release = seq_release_private,
  2556. };
  2557. static const struct file_operations ftrace_enabled_fops = {
  2558. .open = ftrace_enabled_open,
  2559. .read = seq_read,
  2560. .llseek = seq_lseek,
  2561. .release = seq_release_private,
  2562. };
  2563. static const struct file_operations ftrace_filter_fops = {
  2564. .open = ftrace_filter_open,
  2565. .read = seq_read,
  2566. .write = ftrace_filter_write,
  2567. .llseek = ftrace_regex_lseek,
  2568. .release = ftrace_regex_release,
  2569. };
  2570. static const struct file_operations ftrace_notrace_fops = {
  2571. .open = ftrace_notrace_open,
  2572. .read = seq_read,
  2573. .write = ftrace_notrace_write,
  2574. .llseek = ftrace_regex_lseek,
  2575. .release = ftrace_regex_release,
  2576. };
  2577. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2578. static DEFINE_MUTEX(graph_lock);
  2579. int ftrace_graph_count;
  2580. int ftrace_graph_filter_enabled;
  2581. unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
  2582. static void *
  2583. __g_next(struct seq_file *m, loff_t *pos)
  2584. {
  2585. if (*pos >= ftrace_graph_count)
  2586. return NULL;
  2587. return &ftrace_graph_funcs[*pos];
  2588. }
  2589. static void *
  2590. g_next(struct seq_file *m, void *v, loff_t *pos)
  2591. {
  2592. (*pos)++;
  2593. return __g_next(m, pos);
  2594. }
  2595. static void *g_start(struct seq_file *m, loff_t *pos)
  2596. {
  2597. mutex_lock(&graph_lock);
  2598. /* Nothing, tell g_show to print all functions are enabled */
  2599. if (!ftrace_graph_filter_enabled && !*pos)
  2600. return (void *)1;
  2601. return __g_next(m, pos);
  2602. }
  2603. static void g_stop(struct seq_file *m, void *p)
  2604. {
  2605. mutex_unlock(&graph_lock);
  2606. }
  2607. static int g_show(struct seq_file *m, void *v)
  2608. {
  2609. unsigned long *ptr = v;
  2610. if (!ptr)
  2611. return 0;
  2612. if (ptr == (unsigned long *)1) {
  2613. seq_printf(m, "#### all functions enabled ####\n");
  2614. return 0;
  2615. }
  2616. seq_printf(m, "%ps\n", (void *)*ptr);
  2617. return 0;
  2618. }
  2619. static const struct seq_operations ftrace_graph_seq_ops = {
  2620. .start = g_start,
  2621. .next = g_next,
  2622. .stop = g_stop,
  2623. .show = g_show,
  2624. };
  2625. static int
  2626. ftrace_graph_open(struct inode *inode, struct file *file)
  2627. {
  2628. int ret = 0;
  2629. if (unlikely(ftrace_disabled))
  2630. return -ENODEV;
  2631. mutex_lock(&graph_lock);
  2632. if ((file->f_mode & FMODE_WRITE) &&
  2633. (file->f_flags & O_TRUNC)) {
  2634. ftrace_graph_filter_enabled = 0;
  2635. ftrace_graph_count = 0;
  2636. memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
  2637. }
  2638. mutex_unlock(&graph_lock);
  2639. if (file->f_mode & FMODE_READ)
  2640. ret = seq_open(file, &ftrace_graph_seq_ops);
  2641. return ret;
  2642. }
  2643. static int
  2644. ftrace_graph_release(struct inode *inode, struct file *file)
  2645. {
  2646. if (file->f_mode & FMODE_READ)
  2647. seq_release(inode, file);
  2648. return 0;
  2649. }
  2650. static int
  2651. ftrace_set_func(unsigned long *array, int *idx, char *buffer)
  2652. {
  2653. struct dyn_ftrace *rec;
  2654. struct ftrace_page *pg;
  2655. int search_len;
  2656. int fail = 1;
  2657. int type, not;
  2658. char *search;
  2659. bool exists;
  2660. int i;
  2661. /* decode regex */
  2662. type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
  2663. if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
  2664. return -EBUSY;
  2665. search_len = strlen(search);
  2666. mutex_lock(&ftrace_lock);
  2667. if (unlikely(ftrace_disabled)) {
  2668. mutex_unlock(&ftrace_lock);
  2669. return -ENODEV;
  2670. }
  2671. do_for_each_ftrace_rec(pg, rec) {
  2672. if (rec->flags & FTRACE_FL_FREE)
  2673. continue;
  2674. if (ftrace_match_record(rec, NULL, search, search_len, type)) {
  2675. /* if it is in the array */
  2676. exists = false;
  2677. for (i = 0; i < *idx; i++) {
  2678. if (array[i] == rec->ip) {
  2679. exists = true;
  2680. break;
  2681. }
  2682. }
  2683. if (!not) {
  2684. fail = 0;
  2685. if (!exists) {
  2686. array[(*idx)++] = rec->ip;
  2687. if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
  2688. goto out;
  2689. }
  2690. } else {
  2691. if (exists) {
  2692. array[i] = array[--(*idx)];
  2693. array[*idx] = 0;
  2694. fail = 0;
  2695. }
  2696. }
  2697. }
  2698. } while_for_each_ftrace_rec();
  2699. out:
  2700. mutex_unlock(&ftrace_lock);
  2701. if (fail)
  2702. return -EINVAL;
  2703. ftrace_graph_filter_enabled = 1;
  2704. return 0;
  2705. }
  2706. static ssize_t
  2707. ftrace_graph_write(struct file *file, const char __user *ubuf,
  2708. size_t cnt, loff_t *ppos)
  2709. {
  2710. struct trace_parser parser;
  2711. ssize_t read, ret;
  2712. if (!cnt)
  2713. return 0;
  2714. mutex_lock(&graph_lock);
  2715. if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
  2716. ret = -ENOMEM;
  2717. goto out_unlock;
  2718. }
  2719. read = trace_get_user(&parser, ubuf, cnt, ppos);
  2720. if (read >= 0 && trace_parser_loaded((&parser))) {
  2721. parser.buffer[parser.idx] = 0;
  2722. /* we allow only one expression at a time */
  2723. ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
  2724. parser.buffer);
  2725. if (ret)
  2726. goto out_free;
  2727. }
  2728. ret = read;
  2729. out_free:
  2730. trace_parser_put(&parser);
  2731. out_unlock:
  2732. mutex_unlock(&graph_lock);
  2733. return ret;
  2734. }
  2735. static const struct file_operations ftrace_graph_fops = {
  2736. .open = ftrace_graph_open,
  2737. .read = seq_read,
  2738. .write = ftrace_graph_write,
  2739. .release = ftrace_graph_release,
  2740. .llseek = seq_lseek,
  2741. };
  2742. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2743. static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
  2744. {
  2745. trace_create_file("available_filter_functions", 0444,
  2746. d_tracer, NULL, &ftrace_avail_fops);
  2747. trace_create_file("enabled_functions", 0444,
  2748. d_tracer, NULL, &ftrace_enabled_fops);
  2749. trace_create_file("set_ftrace_filter", 0644, d_tracer,
  2750. NULL, &ftrace_filter_fops);
  2751. trace_create_file("set_ftrace_notrace", 0644, d_tracer,
  2752. NULL, &ftrace_notrace_fops);
  2753. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2754. trace_create_file("set_graph_function", 0444, d_tracer,
  2755. NULL,
  2756. &ftrace_graph_fops);
  2757. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2758. return 0;
  2759. }
  2760. static int ftrace_process_locs(struct module *mod,
  2761. unsigned long *start,
  2762. unsigned long *end)
  2763. {
  2764. unsigned long *p;
  2765. unsigned long addr;
  2766. unsigned long flags = 0; /* Shut up gcc */
  2767. mutex_lock(&ftrace_lock);
  2768. p = start;
  2769. while (p < end) {
  2770. addr = ftrace_call_adjust(*p++);
  2771. /*
  2772. * Some architecture linkers will pad between
  2773. * the different mcount_loc sections of different
  2774. * object files to satisfy alignments.
  2775. * Skip any NULL pointers.
  2776. */
  2777. if (!addr)
  2778. continue;
  2779. ftrace_record_ip(addr);
  2780. }
  2781. /*
  2782. * We only need to disable interrupts on start up
  2783. * because we are modifying code that an interrupt
  2784. * may execute, and the modification is not atomic.
  2785. * But for modules, nothing runs the code we modify
  2786. * until we are finished with it, and there's no
  2787. * reason to cause large interrupt latencies while we do it.
  2788. */
  2789. if (!mod)
  2790. local_irq_save(flags);
  2791. ftrace_update_code(mod);
  2792. if (!mod)
  2793. local_irq_restore(flags);
  2794. mutex_unlock(&ftrace_lock);
  2795. return 0;
  2796. }
  2797. #ifdef CONFIG_MODULES
  2798. void ftrace_release_mod(struct module *mod)
  2799. {
  2800. struct dyn_ftrace *rec;
  2801. struct ftrace_page *pg;
  2802. mutex_lock(&ftrace_lock);
  2803. if (ftrace_disabled)
  2804. goto out_unlock;
  2805. do_for_each_ftrace_rec(pg, rec) {
  2806. if (within_module_core(rec->ip, mod)) {
  2807. /*
  2808. * rec->ip is changed in ftrace_free_rec()
  2809. * It should not between s and e if record was freed.
  2810. */
  2811. FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
  2812. ftrace_free_rec(rec);
  2813. }
  2814. } while_for_each_ftrace_rec();
  2815. out_unlock:
  2816. mutex_unlock(&ftrace_lock);
  2817. }
  2818. static void ftrace_init_module(struct module *mod,
  2819. unsigned long *start, unsigned long *end)
  2820. {
  2821. if (ftrace_disabled || start == end)
  2822. return;
  2823. ftrace_process_locs(mod, start, end);
  2824. }
  2825. static int ftrace_module_notify(struct notifier_block *self,
  2826. unsigned long val, void *data)
  2827. {
  2828. struct module *mod = data;
  2829. switch (val) {
  2830. case MODULE_STATE_COMING:
  2831. ftrace_init_module(mod, mod->ftrace_callsites,
  2832. mod->ftrace_callsites +
  2833. mod->num_ftrace_callsites);
  2834. break;
  2835. case MODULE_STATE_GOING:
  2836. ftrace_release_mod(mod);
  2837. break;
  2838. }
  2839. return 0;
  2840. }
  2841. #else
  2842. static int ftrace_module_notify(struct notifier_block *self,
  2843. unsigned long val, void *data)
  2844. {
  2845. return 0;
  2846. }
  2847. #endif /* CONFIG_MODULES */
  2848. struct notifier_block ftrace_module_nb = {
  2849. .notifier_call = ftrace_module_notify,
  2850. .priority = 0,
  2851. };
  2852. extern unsigned long __start_mcount_loc[];
  2853. extern unsigned long __stop_mcount_loc[];
  2854. void __init ftrace_init(void)
  2855. {
  2856. unsigned long count, addr, flags;
  2857. int ret;
  2858. /* Keep the ftrace pointer to the stub */
  2859. addr = (unsigned long)ftrace_stub;
  2860. local_irq_save(flags);
  2861. ftrace_dyn_arch_init(&addr);
  2862. local_irq_restore(flags);
  2863. /* ftrace_dyn_arch_init places the return code in addr */
  2864. if (addr)
  2865. goto failed;
  2866. count = __stop_mcount_loc - __start_mcount_loc;
  2867. ret = ftrace_dyn_table_alloc(count);
  2868. if (ret)
  2869. goto failed;
  2870. last_ftrace_enabled = ftrace_enabled = 1;
  2871. ret = ftrace_process_locs(NULL,
  2872. __start_mcount_loc,
  2873. __stop_mcount_loc);
  2874. ret = register_module_notifier(&ftrace_module_nb);
  2875. if (ret)
  2876. pr_warning("Failed to register trace ftrace module notifier\n");
  2877. set_ftrace_early_filters();
  2878. return;
  2879. failed:
  2880. ftrace_disabled = 1;
  2881. }
  2882. #else
  2883. static struct ftrace_ops global_ops = {
  2884. .func = ftrace_stub,
  2885. };
  2886. static int __init ftrace_nodyn_init(void)
  2887. {
  2888. ftrace_enabled = 1;
  2889. return 0;
  2890. }
  2891. device_initcall(ftrace_nodyn_init);
  2892. static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
  2893. static inline void ftrace_startup_enable(int command) { }
  2894. /* Keep as macros so we do not need to define the commands */
  2895. # define ftrace_startup(ops, command) \
  2896. ({ \
  2897. (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
  2898. 0; \
  2899. })
  2900. # define ftrace_shutdown(ops, command) do { } while (0)
  2901. # define ftrace_startup_sysctl() do { } while (0)
  2902. # define ftrace_shutdown_sysctl() do { } while (0)
  2903. static inline int
  2904. ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
  2905. {
  2906. return 1;
  2907. }
  2908. #endif /* CONFIG_DYNAMIC_FTRACE */
  2909. static void
  2910. ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
  2911. {
  2912. struct ftrace_ops *op;
  2913. if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
  2914. return;
  2915. trace_recursion_set(TRACE_INTERNAL_BIT);
  2916. /*
  2917. * Some of the ops may be dynamically allocated,
  2918. * they must be freed after a synchronize_sched().
  2919. */
  2920. preempt_disable_notrace();
  2921. op = rcu_dereference_raw(ftrace_ops_list);
  2922. while (op != &ftrace_list_end) {
  2923. if (ftrace_ops_test(op, ip))
  2924. op->func(ip, parent_ip);
  2925. op = rcu_dereference_raw(op->next);
  2926. };
  2927. preempt_enable_notrace();
  2928. trace_recursion_clear(TRACE_INTERNAL_BIT);
  2929. }
  2930. static void clear_ftrace_swapper(void)
  2931. {
  2932. struct task_struct *p;
  2933. int cpu;
  2934. get_online_cpus();
  2935. for_each_online_cpu(cpu) {
  2936. p = idle_task(cpu);
  2937. clear_tsk_trace_trace(p);
  2938. }
  2939. put_online_cpus();
  2940. }
  2941. static void set_ftrace_swapper(void)
  2942. {
  2943. struct task_struct *p;
  2944. int cpu;
  2945. get_online_cpus();
  2946. for_each_online_cpu(cpu) {
  2947. p = idle_task(cpu);
  2948. set_tsk_trace_trace(p);
  2949. }
  2950. put_online_cpus();
  2951. }
  2952. static void clear_ftrace_pid(struct pid *pid)
  2953. {
  2954. struct task_struct *p;
  2955. rcu_read_lock();
  2956. do_each_pid_task(pid, PIDTYPE_PID, p) {
  2957. clear_tsk_trace_trace(p);
  2958. } while_each_pid_task(pid, PIDTYPE_PID, p);
  2959. rcu_read_unlock();
  2960. put_pid(pid);
  2961. }
  2962. static void set_ftrace_pid(struct pid *pid)
  2963. {
  2964. struct task_struct *p;
  2965. rcu_read_lock();
  2966. do_each_pid_task(pid, PIDTYPE_PID, p) {
  2967. set_tsk_trace_trace(p);
  2968. } while_each_pid_task(pid, PIDTYPE_PID, p);
  2969. rcu_read_unlock();
  2970. }
  2971. static void clear_ftrace_pid_task(struct pid *pid)
  2972. {
  2973. if (pid == ftrace_swapper_pid)
  2974. clear_ftrace_swapper();
  2975. else
  2976. clear_ftrace_pid(pid);
  2977. }
  2978. static void set_ftrace_pid_task(struct pid *pid)
  2979. {
  2980. if (pid == ftrace_swapper_pid)
  2981. set_ftrace_swapper();
  2982. else
  2983. set_ftrace_pid(pid);
  2984. }
  2985. static int ftrace_pid_add(int p)
  2986. {
  2987. struct pid *pid;
  2988. struct ftrace_pid *fpid;
  2989. int ret = -EINVAL;
  2990. mutex_lock(&ftrace_lock);
  2991. if (!p)
  2992. pid = ftrace_swapper_pid;
  2993. else
  2994. pid = find_get_pid(p);
  2995. if (!pid)
  2996. goto out;
  2997. ret = 0;
  2998. list_for_each_entry(fpid, &ftrace_pids, list)
  2999. if (fpid->pid == pid)
  3000. goto out_put;
  3001. ret = -ENOMEM;
  3002. fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
  3003. if (!fpid)
  3004. goto out_put;
  3005. list_add(&fpid->list, &ftrace_pids);
  3006. fpid->pid = pid;
  3007. set_ftrace_pid_task(pid);
  3008. ftrace_update_pid_func();
  3009. ftrace_startup_enable(0);
  3010. mutex_unlock(&ftrace_lock);
  3011. return 0;
  3012. out_put:
  3013. if (pid != ftrace_swapper_pid)
  3014. put_pid(pid);
  3015. out:
  3016. mutex_unlock(&ftrace_lock);
  3017. return ret;
  3018. }
  3019. static void ftrace_pid_reset(void)
  3020. {
  3021. struct ftrace_pid *fpid, *safe;
  3022. mutex_lock(&ftrace_lock);
  3023. list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
  3024. struct pid *pid = fpid->pid;
  3025. clear_ftrace_pid_task(pid);
  3026. list_del(&fpid->list);
  3027. kfree(fpid);
  3028. }
  3029. ftrace_update_pid_func();
  3030. ftrace_startup_enable(0);
  3031. mutex_unlock(&ftrace_lock);
  3032. }
  3033. static void *fpid_start(struct seq_file *m, loff_t *pos)
  3034. {
  3035. mutex_lock(&ftrace_lock);
  3036. if (list_empty(&ftrace_pids) && (!*pos))
  3037. return (void *) 1;
  3038. return seq_list_start(&ftrace_pids, *pos);
  3039. }
  3040. static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
  3041. {
  3042. if (v == (void *)1)
  3043. return NULL;
  3044. return seq_list_next(v, &ftrace_pids, pos);
  3045. }
  3046. static void fpid_stop(struct seq_file *m, void *p)
  3047. {
  3048. mutex_unlock(&ftrace_lock);
  3049. }
  3050. static int fpid_show(struct seq_file *m, void *v)
  3051. {
  3052. const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
  3053. if (v == (void *)1) {
  3054. seq_printf(m, "no pid\n");
  3055. return 0;
  3056. }
  3057. if (fpid->pid == ftrace_swapper_pid)
  3058. seq_printf(m, "swapper tasks\n");
  3059. else
  3060. seq_printf(m, "%u\n", pid_vnr(fpid->pid));
  3061. return 0;
  3062. }
  3063. static const struct seq_operations ftrace_pid_sops = {
  3064. .start = fpid_start,
  3065. .next = fpid_next,
  3066. .stop = fpid_stop,
  3067. .show = fpid_show,
  3068. };
  3069. static int
  3070. ftrace_pid_open(struct inode *inode, struct file *file)
  3071. {
  3072. int ret = 0;
  3073. if ((file->f_mode & FMODE_WRITE) &&
  3074. (file->f_flags & O_TRUNC))
  3075. ftrace_pid_reset();
  3076. if (file->f_mode & FMODE_READ)
  3077. ret = seq_open(file, &ftrace_pid_sops);
  3078. return ret;
  3079. }
  3080. static ssize_t
  3081. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  3082. size_t cnt, loff_t *ppos)
  3083. {
  3084. char buf[64], *tmp;
  3085. long val;
  3086. int ret;
  3087. if (cnt >= sizeof(buf))
  3088. return -EINVAL;
  3089. if (copy_from_user(&buf, ubuf, cnt))
  3090. return -EFAULT;
  3091. buf[cnt] = 0;
  3092. /*
  3093. * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
  3094. * to clean the filter quietly.
  3095. */
  3096. tmp = strstrip(buf);
  3097. if (strlen(tmp) == 0)
  3098. return 1;
  3099. ret = strict_strtol(tmp, 10, &val);
  3100. if (ret < 0)
  3101. return ret;
  3102. ret = ftrace_pid_add(val);
  3103. return ret ? ret : cnt;
  3104. }
  3105. static int
  3106. ftrace_pid_release(struct inode *inode, struct file *file)
  3107. {
  3108. if (file->f_mode & FMODE_READ)
  3109. seq_release(inode, file);
  3110. return 0;
  3111. }
  3112. static const struct file_operations ftrace_pid_fops = {
  3113. .open = ftrace_pid_open,
  3114. .write = ftrace_pid_write,
  3115. .read = seq_read,
  3116. .llseek = seq_lseek,
  3117. .release = ftrace_pid_release,
  3118. };
  3119. static __init int ftrace_init_debugfs(void)
  3120. {
  3121. struct dentry *d_tracer;
  3122. d_tracer = tracing_init_dentry();
  3123. if (!d_tracer)
  3124. return 0;
  3125. ftrace_init_dyn_debugfs(d_tracer);
  3126. trace_create_file("set_ftrace_pid", 0644, d_tracer,
  3127. NULL, &ftrace_pid_fops);
  3128. ftrace_profile_debugfs(d_tracer);
  3129. return 0;
  3130. }
  3131. fs_initcall(ftrace_init_debugfs);
  3132. /**
  3133. * ftrace_kill - kill ftrace
  3134. *
  3135. * This function should be used by panic code. It stops ftrace
  3136. * but in a not so nice way. If you need to simply kill ftrace
  3137. * from a non-atomic section, use ftrace_kill.
  3138. */
  3139. void ftrace_kill(void)
  3140. {
  3141. ftrace_disabled = 1;
  3142. ftrace_enabled = 0;
  3143. clear_ftrace_function();
  3144. }
  3145. /**
  3146. * register_ftrace_function - register a function for profiling
  3147. * @ops - ops structure that holds the function for profiling.
  3148. *
  3149. * Register a function to be called by all functions in the
  3150. * kernel.
  3151. *
  3152. * Note: @ops->func and all the functions it calls must be labeled
  3153. * with "notrace", otherwise it will go into a
  3154. * recursive loop.
  3155. */
  3156. int register_ftrace_function(struct ftrace_ops *ops)
  3157. {
  3158. int ret = -1;
  3159. mutex_lock(&ftrace_lock);
  3160. if (unlikely(ftrace_disabled))
  3161. goto out_unlock;
  3162. ret = __register_ftrace_function(ops);
  3163. if (!ret)
  3164. ret = ftrace_startup(ops, 0);
  3165. out_unlock:
  3166. mutex_unlock(&ftrace_lock);
  3167. return ret;
  3168. }
  3169. EXPORT_SYMBOL_GPL(register_ftrace_function);
  3170. /**
  3171. * unregister_ftrace_function - unregister a function for profiling.
  3172. * @ops - ops structure that holds the function to unregister
  3173. *
  3174. * Unregister a function that was added to be called by ftrace profiling.
  3175. */
  3176. int unregister_ftrace_function(struct ftrace_ops *ops)
  3177. {
  3178. int ret;
  3179. mutex_lock(&ftrace_lock);
  3180. ret = __unregister_ftrace_function(ops);
  3181. if (!ret)
  3182. ftrace_shutdown(ops, 0);
  3183. mutex_unlock(&ftrace_lock);
  3184. return ret;
  3185. }
  3186. EXPORT_SYMBOL_GPL(unregister_ftrace_function);
  3187. int
  3188. ftrace_enable_sysctl(struct ctl_table *table, int write,
  3189. void __user *buffer, size_t *lenp,
  3190. loff_t *ppos)
  3191. {
  3192. int ret = -ENODEV;
  3193. mutex_lock(&ftrace_lock);
  3194. if (unlikely(ftrace_disabled))
  3195. goto out;
  3196. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  3197. if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
  3198. goto out;
  3199. last_ftrace_enabled = !!ftrace_enabled;
  3200. if (ftrace_enabled) {
  3201. ftrace_startup_sysctl();
  3202. /* we are starting ftrace again */
  3203. if (ftrace_ops_list != &ftrace_list_end) {
  3204. if (ftrace_ops_list->next == &ftrace_list_end)
  3205. ftrace_trace_function = ftrace_ops_list->func;
  3206. else
  3207. ftrace_trace_function = ftrace_ops_list_func;
  3208. }
  3209. } else {
  3210. /* stopping ftrace calls (just send to ftrace_stub) */
  3211. ftrace_trace_function = ftrace_stub;
  3212. ftrace_shutdown_sysctl();
  3213. }
  3214. out:
  3215. mutex_unlock(&ftrace_lock);
  3216. return ret;
  3217. }
  3218. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  3219. static int ftrace_graph_active;
  3220. static struct notifier_block ftrace_suspend_notifier;
  3221. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
  3222. {
  3223. return 0;
  3224. }
  3225. /* The callbacks that hook a function */
  3226. trace_func_graph_ret_t ftrace_graph_return =
  3227. (trace_func_graph_ret_t)ftrace_stub;
  3228. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  3229. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  3230. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  3231. {
  3232. int i;
  3233. int ret = 0;
  3234. unsigned long flags;
  3235. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  3236. struct task_struct *g, *t;
  3237. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  3238. ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
  3239. * sizeof(struct ftrace_ret_stack),
  3240. GFP_KERNEL);
  3241. if (!ret_stack_list[i]) {
  3242. start = 0;
  3243. end = i;
  3244. ret = -ENOMEM;
  3245. goto free;
  3246. }
  3247. }
  3248. read_lock_irqsave(&tasklist_lock, flags);
  3249. do_each_thread(g, t) {
  3250. if (start == end) {
  3251. ret = -EAGAIN;
  3252. goto unlock;
  3253. }
  3254. if (t->ret_stack == NULL) {
  3255. atomic_set(&t->tracing_graph_pause, 0);
  3256. atomic_set(&t->trace_overrun, 0);
  3257. t->curr_ret_stack = -1;
  3258. /* Make sure the tasks see the -1 first: */
  3259. smp_wmb();
  3260. t->ret_stack = ret_stack_list[start++];
  3261. }
  3262. } while_each_thread(g, t);
  3263. unlock:
  3264. read_unlock_irqrestore(&tasklist_lock, flags);
  3265. free:
  3266. for (i = start; i < end; i++)
  3267. kfree(ret_stack_list[i]);
  3268. return ret;
  3269. }
  3270. static void
  3271. ftrace_graph_probe_sched_switch(void *ignore,
  3272. struct task_struct *prev, struct task_struct *next)
  3273. {
  3274. unsigned long long timestamp;
  3275. int index;
  3276. /*
  3277. * Does the user want to count the time a function was asleep.
  3278. * If so, do not update the time stamps.
  3279. */
  3280. if (trace_flags & TRACE_ITER_SLEEP_TIME)
  3281. return;
  3282. timestamp = trace_clock_local();
  3283. prev->ftrace_timestamp = timestamp;
  3284. /* only process tasks that we timestamped */
  3285. if (!next->ftrace_timestamp)
  3286. return;
  3287. /*
  3288. * Update all the counters in next to make up for the
  3289. * time next was sleeping.
  3290. */
  3291. timestamp -= next->ftrace_timestamp;
  3292. for (index = next->curr_ret_stack; index >= 0; index--)
  3293. next->ret_stack[index].calltime += timestamp;
  3294. }
  3295. /* Allocate a return stack for each task */
  3296. static int start_graph_tracing(void)
  3297. {
  3298. struct ftrace_ret_stack **ret_stack_list;
  3299. int ret, cpu;
  3300. ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
  3301. sizeof(struct ftrace_ret_stack *),
  3302. GFP_KERNEL);
  3303. if (!ret_stack_list)
  3304. return -ENOMEM;
  3305. /* The cpu_boot init_task->ret_stack will never be freed */
  3306. for_each_online_cpu(cpu) {
  3307. if (!idle_task(cpu)->ret_stack)
  3308. ftrace_graph_init_idle_task(idle_task(cpu), cpu);
  3309. }
  3310. do {
  3311. ret = alloc_retstack_tasklist(ret_stack_list);
  3312. } while (ret == -EAGAIN);
  3313. if (!ret) {
  3314. ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  3315. if (ret)
  3316. pr_info("ftrace_graph: Couldn't activate tracepoint"
  3317. " probe to kernel_sched_switch\n");
  3318. }
  3319. kfree(ret_stack_list);
  3320. return ret;
  3321. }
  3322. /*
  3323. * Hibernation protection.
  3324. * The state of the current task is too much unstable during
  3325. * suspend/restore to disk. We want to protect against that.
  3326. */
  3327. static int
  3328. ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
  3329. void *unused)
  3330. {
  3331. switch (state) {
  3332. case PM_HIBERNATION_PREPARE:
  3333. pause_graph_tracing();
  3334. break;
  3335. case PM_POST_HIBERNATION:
  3336. unpause_graph_tracing();
  3337. break;
  3338. }
  3339. return NOTIFY_DONE;
  3340. }
  3341. int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  3342. trace_func_graph_ent_t entryfunc)
  3343. {
  3344. int ret = 0;
  3345. mutex_lock(&ftrace_lock);
  3346. /* we currently allow only one tracer registered at a time */
  3347. if (ftrace_graph_active) {
  3348. ret = -EBUSY;
  3349. goto out;
  3350. }
  3351. ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
  3352. register_pm_notifier(&ftrace_suspend_notifier);
  3353. ftrace_graph_active++;
  3354. ret = start_graph_tracing();
  3355. if (ret) {
  3356. ftrace_graph_active--;
  3357. goto out;
  3358. }
  3359. ftrace_graph_return = retfunc;
  3360. ftrace_graph_entry = entryfunc;
  3361. ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
  3362. out:
  3363. mutex_unlock(&ftrace_lock);
  3364. return ret;
  3365. }
  3366. void unregister_ftrace_graph(void)
  3367. {
  3368. mutex_lock(&ftrace_lock);
  3369. if (unlikely(!ftrace_graph_active))
  3370. goto out;
  3371. ftrace_graph_active--;
  3372. ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
  3373. ftrace_graph_entry = ftrace_graph_entry_stub;
  3374. ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
  3375. unregister_pm_notifier(&ftrace_suspend_notifier);
  3376. unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  3377. out:
  3378. mutex_unlock(&ftrace_lock);
  3379. }
  3380. static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
  3381. static void
  3382. graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
  3383. {
  3384. atomic_set(&t->tracing_graph_pause, 0);
  3385. atomic_set(&t->trace_overrun, 0);
  3386. t->ftrace_timestamp = 0;
  3387. /* make curr_ret_stack visible before we add the ret_stack */
  3388. smp_wmb();
  3389. t->ret_stack = ret_stack;
  3390. }
  3391. /*
  3392. * Allocate a return stack for the idle task. May be the first
  3393. * time through, or it may be done by CPU hotplug online.
  3394. */
  3395. void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
  3396. {
  3397. t->curr_ret_stack = -1;
  3398. /*
  3399. * The idle task has no parent, it either has its own
  3400. * stack or no stack at all.
  3401. */
  3402. if (t->ret_stack)
  3403. WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
  3404. if (ftrace_graph_active) {
  3405. struct ftrace_ret_stack *ret_stack;
  3406. ret_stack = per_cpu(idle_ret_stack, cpu);
  3407. if (!ret_stack) {
  3408. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  3409. * sizeof(struct ftrace_ret_stack),
  3410. GFP_KERNEL);
  3411. if (!ret_stack)
  3412. return;
  3413. per_cpu(idle_ret_stack, cpu) = ret_stack;
  3414. }
  3415. graph_init_task(t, ret_stack);
  3416. }
  3417. }
  3418. /* Allocate a return stack for newly created task */
  3419. void ftrace_graph_init_task(struct task_struct *t)
  3420. {
  3421. /* Make sure we do not use the parent ret_stack */
  3422. t->ret_stack = NULL;
  3423. t->curr_ret_stack = -1;
  3424. if (ftrace_graph_active) {
  3425. struct ftrace_ret_stack *ret_stack;
  3426. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  3427. * sizeof(struct ftrace_ret_stack),
  3428. GFP_KERNEL);
  3429. if (!ret_stack)
  3430. return;
  3431. graph_init_task(t, ret_stack);
  3432. }
  3433. }
  3434. void ftrace_graph_exit_task(struct task_struct *t)
  3435. {
  3436. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  3437. t->ret_stack = NULL;
  3438. /* NULL must become visible to IRQs before we free it: */
  3439. barrier();
  3440. kfree(ret_stack);
  3441. }
  3442. void ftrace_graph_stop(void)
  3443. {
  3444. ftrace_stop();
  3445. }
  3446. #endif