ftrace.c 70 KB

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