ftrace.c 71 KB

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