ftrace.c 70 KB

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