ftrace.c 69 KB

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