ftrace.c 68 KB

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