ftrace.c 68 KB

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