ftrace.c 74 KB

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