ftrace.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423
  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. /*
  1000. * Just warn in case of unbalance, no need to kill ftrace, it's not
  1001. * critical but the ftrace_call callers may be never nopped again after
  1002. * further ftrace uses.
  1003. */
  1004. WARN_ON_ONCE(ftrace_start_up < 0);
  1005. if (!ftrace_start_up)
  1006. command |= FTRACE_DISABLE_CALLS;
  1007. if (saved_ftrace_func != ftrace_trace_function) {
  1008. saved_ftrace_func = ftrace_trace_function;
  1009. command |= FTRACE_UPDATE_TRACE_FUNC;
  1010. }
  1011. if (!command || !ftrace_enabled)
  1012. return;
  1013. ftrace_run_update_code(command);
  1014. }
  1015. static void ftrace_startup_sysctl(void)
  1016. {
  1017. int command = FTRACE_ENABLE_MCOUNT;
  1018. if (unlikely(ftrace_disabled))
  1019. return;
  1020. /* Force update next time */
  1021. saved_ftrace_func = NULL;
  1022. /* ftrace_start_up is true if we want ftrace running */
  1023. if (ftrace_start_up)
  1024. command |= FTRACE_ENABLE_CALLS;
  1025. ftrace_run_update_code(command);
  1026. }
  1027. static void ftrace_shutdown_sysctl(void)
  1028. {
  1029. int command = FTRACE_DISABLE_MCOUNT;
  1030. if (unlikely(ftrace_disabled))
  1031. return;
  1032. /* ftrace_start_up is true if ftrace is running */
  1033. if (ftrace_start_up)
  1034. command |= FTRACE_DISABLE_CALLS;
  1035. ftrace_run_update_code(command);
  1036. }
  1037. static cycle_t ftrace_update_time;
  1038. static unsigned long ftrace_update_cnt;
  1039. unsigned long ftrace_update_tot_cnt;
  1040. static int ftrace_update_code(struct module *mod)
  1041. {
  1042. struct dyn_ftrace *p;
  1043. cycle_t start, stop;
  1044. start = ftrace_now(raw_smp_processor_id());
  1045. ftrace_update_cnt = 0;
  1046. while (ftrace_new_addrs) {
  1047. /* If something went wrong, bail without enabling anything */
  1048. if (unlikely(ftrace_disabled))
  1049. return -1;
  1050. p = ftrace_new_addrs;
  1051. ftrace_new_addrs = p->newlist;
  1052. p->flags = 0L;
  1053. /* convert record (i.e, patch mcount-call with NOP) */
  1054. if (ftrace_code_disable(mod, p)) {
  1055. p->flags |= FTRACE_FL_CONVERTED;
  1056. ftrace_update_cnt++;
  1057. } else
  1058. ftrace_free_rec(p);
  1059. }
  1060. stop = ftrace_now(raw_smp_processor_id());
  1061. ftrace_update_time = stop - start;
  1062. ftrace_update_tot_cnt += ftrace_update_cnt;
  1063. return 0;
  1064. }
  1065. static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
  1066. {
  1067. struct ftrace_page *pg;
  1068. int cnt;
  1069. int i;
  1070. /* allocate a few pages */
  1071. ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
  1072. if (!ftrace_pages_start)
  1073. return -1;
  1074. /*
  1075. * Allocate a few more pages.
  1076. *
  1077. * TODO: have some parser search vmlinux before
  1078. * final linking to find all calls to ftrace.
  1079. * Then we can:
  1080. * a) know how many pages to allocate.
  1081. * and/or
  1082. * b) set up the table then.
  1083. *
  1084. * The dynamic code is still necessary for
  1085. * modules.
  1086. */
  1087. pg = ftrace_pages = ftrace_pages_start;
  1088. cnt = num_to_init / ENTRIES_PER_PAGE;
  1089. pr_info("ftrace: allocating %ld entries in %d pages\n",
  1090. num_to_init, cnt + 1);
  1091. for (i = 0; i < cnt; i++) {
  1092. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  1093. /* If we fail, we'll try later anyway */
  1094. if (!pg->next)
  1095. break;
  1096. pg = pg->next;
  1097. }
  1098. return 0;
  1099. }
  1100. enum {
  1101. FTRACE_ITER_FILTER = (1 << 0),
  1102. FTRACE_ITER_CONT = (1 << 1),
  1103. FTRACE_ITER_NOTRACE = (1 << 2),
  1104. FTRACE_ITER_FAILURES = (1 << 3),
  1105. FTRACE_ITER_PRINTALL = (1 << 4),
  1106. FTRACE_ITER_HASH = (1 << 5),
  1107. };
  1108. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  1109. struct ftrace_iterator {
  1110. struct ftrace_page *pg;
  1111. int hidx;
  1112. int idx;
  1113. unsigned flags;
  1114. unsigned char buffer[FTRACE_BUFF_MAX+1];
  1115. unsigned buffer_idx;
  1116. unsigned filtered;
  1117. };
  1118. static void *
  1119. t_hash_next(struct seq_file *m, void *v, loff_t *pos)
  1120. {
  1121. struct ftrace_iterator *iter = m->private;
  1122. struct hlist_node *hnd = v;
  1123. struct hlist_head *hhd;
  1124. WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
  1125. (*pos)++;
  1126. retry:
  1127. if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
  1128. return NULL;
  1129. hhd = &ftrace_func_hash[iter->hidx];
  1130. if (hlist_empty(hhd)) {
  1131. iter->hidx++;
  1132. hnd = NULL;
  1133. goto retry;
  1134. }
  1135. if (!hnd)
  1136. hnd = hhd->first;
  1137. else {
  1138. hnd = hnd->next;
  1139. if (!hnd) {
  1140. iter->hidx++;
  1141. goto retry;
  1142. }
  1143. }
  1144. return hnd;
  1145. }
  1146. static void *t_hash_start(struct seq_file *m, loff_t *pos)
  1147. {
  1148. struct ftrace_iterator *iter = m->private;
  1149. void *p = NULL;
  1150. iter->flags |= FTRACE_ITER_HASH;
  1151. return t_hash_next(m, p, pos);
  1152. }
  1153. static int t_hash_show(struct seq_file *m, void *v)
  1154. {
  1155. struct ftrace_func_probe *rec;
  1156. struct hlist_node *hnd = v;
  1157. char str[KSYM_SYMBOL_LEN];
  1158. rec = hlist_entry(hnd, struct ftrace_func_probe, node);
  1159. if (rec->ops->print)
  1160. return rec->ops->print(m, rec->ip, rec->ops, rec->data);
  1161. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  1162. seq_printf(m, "%s:", str);
  1163. kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
  1164. seq_printf(m, "%s", str);
  1165. if (rec->data)
  1166. seq_printf(m, ":%p", rec->data);
  1167. seq_putc(m, '\n');
  1168. return 0;
  1169. }
  1170. static void *
  1171. t_next(struct seq_file *m, void *v, loff_t *pos)
  1172. {
  1173. struct ftrace_iterator *iter = m->private;
  1174. struct dyn_ftrace *rec = NULL;
  1175. if (iter->flags & FTRACE_ITER_HASH)
  1176. return t_hash_next(m, v, pos);
  1177. (*pos)++;
  1178. if (iter->flags & FTRACE_ITER_PRINTALL)
  1179. return NULL;
  1180. retry:
  1181. if (iter->idx >= iter->pg->index) {
  1182. if (iter->pg->next) {
  1183. iter->pg = iter->pg->next;
  1184. iter->idx = 0;
  1185. goto retry;
  1186. } else {
  1187. iter->idx = -1;
  1188. }
  1189. } else {
  1190. rec = &iter->pg->records[iter->idx++];
  1191. if ((rec->flags & FTRACE_FL_FREE) ||
  1192. (!(iter->flags & FTRACE_ITER_FAILURES) &&
  1193. (rec->flags & FTRACE_FL_FAILED)) ||
  1194. ((iter->flags & FTRACE_ITER_FAILURES) &&
  1195. !(rec->flags & FTRACE_FL_FAILED)) ||
  1196. ((iter->flags & FTRACE_ITER_FILTER) &&
  1197. !(rec->flags & FTRACE_FL_FILTER)) ||
  1198. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  1199. !(rec->flags & FTRACE_FL_NOTRACE))) {
  1200. rec = NULL;
  1201. goto retry;
  1202. }
  1203. }
  1204. return rec;
  1205. }
  1206. static void *t_start(struct seq_file *m, loff_t *pos)
  1207. {
  1208. struct ftrace_iterator *iter = m->private;
  1209. void *p = NULL;
  1210. mutex_lock(&ftrace_lock);
  1211. /*
  1212. * For set_ftrace_filter reading, if we have the filter
  1213. * off, we can short cut and just print out that all
  1214. * functions are enabled.
  1215. */
  1216. if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
  1217. if (*pos > 0)
  1218. return t_hash_start(m, pos);
  1219. iter->flags |= FTRACE_ITER_PRINTALL;
  1220. (*pos)++;
  1221. return iter;
  1222. }
  1223. if (iter->flags & FTRACE_ITER_HASH)
  1224. return t_hash_start(m, pos);
  1225. if (*pos > 0) {
  1226. if (iter->idx < 0)
  1227. return p;
  1228. (*pos)--;
  1229. iter->idx--;
  1230. }
  1231. p = t_next(m, p, pos);
  1232. if (!p)
  1233. return t_hash_start(m, pos);
  1234. return p;
  1235. }
  1236. static void t_stop(struct seq_file *m, void *p)
  1237. {
  1238. mutex_unlock(&ftrace_lock);
  1239. }
  1240. static int t_show(struct seq_file *m, void *v)
  1241. {
  1242. struct ftrace_iterator *iter = m->private;
  1243. struct dyn_ftrace *rec = v;
  1244. char str[KSYM_SYMBOL_LEN];
  1245. if (iter->flags & FTRACE_ITER_HASH)
  1246. return t_hash_show(m, v);
  1247. if (iter->flags & FTRACE_ITER_PRINTALL) {
  1248. seq_printf(m, "#### all functions enabled ####\n");
  1249. return 0;
  1250. }
  1251. if (!rec)
  1252. return 0;
  1253. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  1254. seq_printf(m, "%s\n", str);
  1255. return 0;
  1256. }
  1257. static struct seq_operations show_ftrace_seq_ops = {
  1258. .start = t_start,
  1259. .next = t_next,
  1260. .stop = t_stop,
  1261. .show = t_show,
  1262. };
  1263. static int
  1264. ftrace_avail_open(struct inode *inode, struct file *file)
  1265. {
  1266. struct ftrace_iterator *iter;
  1267. int ret;
  1268. if (unlikely(ftrace_disabled))
  1269. return -ENODEV;
  1270. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1271. if (!iter)
  1272. return -ENOMEM;
  1273. iter->pg = ftrace_pages_start;
  1274. ret = seq_open(file, &show_ftrace_seq_ops);
  1275. if (!ret) {
  1276. struct seq_file *m = file->private_data;
  1277. m->private = iter;
  1278. } else {
  1279. kfree(iter);
  1280. }
  1281. return ret;
  1282. }
  1283. int ftrace_avail_release(struct inode *inode, struct file *file)
  1284. {
  1285. struct seq_file *m = (struct seq_file *)file->private_data;
  1286. struct ftrace_iterator *iter = m->private;
  1287. seq_release(inode, file);
  1288. kfree(iter);
  1289. return 0;
  1290. }
  1291. static int
  1292. ftrace_failures_open(struct inode *inode, struct file *file)
  1293. {
  1294. int ret;
  1295. struct seq_file *m;
  1296. struct ftrace_iterator *iter;
  1297. ret = ftrace_avail_open(inode, file);
  1298. if (!ret) {
  1299. m = (struct seq_file *)file->private_data;
  1300. iter = (struct ftrace_iterator *)m->private;
  1301. iter->flags = FTRACE_ITER_FAILURES;
  1302. }
  1303. return ret;
  1304. }
  1305. static void ftrace_filter_reset(int enable)
  1306. {
  1307. struct ftrace_page *pg;
  1308. struct dyn_ftrace *rec;
  1309. unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1310. mutex_lock(&ftrace_lock);
  1311. if (enable)
  1312. ftrace_filtered = 0;
  1313. do_for_each_ftrace_rec(pg, rec) {
  1314. if (rec->flags & FTRACE_FL_FAILED)
  1315. continue;
  1316. rec->flags &= ~type;
  1317. } while_for_each_ftrace_rec();
  1318. mutex_unlock(&ftrace_lock);
  1319. }
  1320. static int
  1321. ftrace_regex_open(struct inode *inode, struct file *file, int enable)
  1322. {
  1323. struct ftrace_iterator *iter;
  1324. int ret = 0;
  1325. if (unlikely(ftrace_disabled))
  1326. return -ENODEV;
  1327. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1328. if (!iter)
  1329. return -ENOMEM;
  1330. mutex_lock(&ftrace_regex_lock);
  1331. if ((file->f_mode & FMODE_WRITE) &&
  1332. !(file->f_flags & O_APPEND))
  1333. ftrace_filter_reset(enable);
  1334. if (file->f_mode & FMODE_READ) {
  1335. iter->pg = ftrace_pages_start;
  1336. iter->flags = enable ? FTRACE_ITER_FILTER :
  1337. FTRACE_ITER_NOTRACE;
  1338. ret = seq_open(file, &show_ftrace_seq_ops);
  1339. if (!ret) {
  1340. struct seq_file *m = file->private_data;
  1341. m->private = iter;
  1342. } else
  1343. kfree(iter);
  1344. } else
  1345. file->private_data = iter;
  1346. mutex_unlock(&ftrace_regex_lock);
  1347. return ret;
  1348. }
  1349. static int
  1350. ftrace_filter_open(struct inode *inode, struct file *file)
  1351. {
  1352. return ftrace_regex_open(inode, file, 1);
  1353. }
  1354. static int
  1355. ftrace_notrace_open(struct inode *inode, struct file *file)
  1356. {
  1357. return ftrace_regex_open(inode, file, 0);
  1358. }
  1359. static loff_t
  1360. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  1361. {
  1362. loff_t ret;
  1363. if (file->f_mode & FMODE_READ)
  1364. ret = seq_lseek(file, offset, origin);
  1365. else
  1366. file->f_pos = ret = 1;
  1367. return ret;
  1368. }
  1369. enum {
  1370. MATCH_FULL,
  1371. MATCH_FRONT_ONLY,
  1372. MATCH_MIDDLE_ONLY,
  1373. MATCH_END_ONLY,
  1374. };
  1375. /*
  1376. * (static function - no need for kernel doc)
  1377. *
  1378. * Pass in a buffer containing a glob and this function will
  1379. * set search to point to the search part of the buffer and
  1380. * return the type of search it is (see enum above).
  1381. * This does modify buff.
  1382. *
  1383. * Returns enum type.
  1384. * search returns the pointer to use for comparison.
  1385. * not returns 1 if buff started with a '!'
  1386. * 0 otherwise.
  1387. */
  1388. static int
  1389. ftrace_setup_glob(char *buff, int len, char **search, int *not)
  1390. {
  1391. int type = MATCH_FULL;
  1392. int i;
  1393. if (buff[0] == '!') {
  1394. *not = 1;
  1395. buff++;
  1396. len--;
  1397. } else
  1398. *not = 0;
  1399. *search = buff;
  1400. for (i = 0; i < len; i++) {
  1401. if (buff[i] == '*') {
  1402. if (!i) {
  1403. *search = buff + 1;
  1404. type = MATCH_END_ONLY;
  1405. } else {
  1406. if (type == MATCH_END_ONLY)
  1407. type = MATCH_MIDDLE_ONLY;
  1408. else
  1409. type = MATCH_FRONT_ONLY;
  1410. buff[i] = 0;
  1411. break;
  1412. }
  1413. }
  1414. }
  1415. return type;
  1416. }
  1417. static int ftrace_match(char *str, char *regex, int len, int type)
  1418. {
  1419. int matched = 0;
  1420. char *ptr;
  1421. switch (type) {
  1422. case MATCH_FULL:
  1423. if (strcmp(str, regex) == 0)
  1424. matched = 1;
  1425. break;
  1426. case MATCH_FRONT_ONLY:
  1427. if (strncmp(str, regex, len) == 0)
  1428. matched = 1;
  1429. break;
  1430. case MATCH_MIDDLE_ONLY:
  1431. if (strstr(str, regex))
  1432. matched = 1;
  1433. break;
  1434. case MATCH_END_ONLY:
  1435. ptr = strstr(str, regex);
  1436. if (ptr && (ptr[len] == 0))
  1437. matched = 1;
  1438. break;
  1439. }
  1440. return matched;
  1441. }
  1442. static int
  1443. ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
  1444. {
  1445. char str[KSYM_SYMBOL_LEN];
  1446. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  1447. return ftrace_match(str, regex, len, type);
  1448. }
  1449. static void ftrace_match_records(char *buff, int len, int enable)
  1450. {
  1451. unsigned int search_len;
  1452. struct ftrace_page *pg;
  1453. struct dyn_ftrace *rec;
  1454. unsigned long flag;
  1455. char *search;
  1456. int type;
  1457. int not;
  1458. flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1459. type = ftrace_setup_glob(buff, len, &search, &not);
  1460. search_len = strlen(search);
  1461. mutex_lock(&ftrace_lock);
  1462. do_for_each_ftrace_rec(pg, rec) {
  1463. if (rec->flags & FTRACE_FL_FAILED)
  1464. continue;
  1465. if (ftrace_match_record(rec, search, search_len, type)) {
  1466. if (not)
  1467. rec->flags &= ~flag;
  1468. else
  1469. rec->flags |= flag;
  1470. }
  1471. /*
  1472. * Only enable filtering if we have a function that
  1473. * is filtered on.
  1474. */
  1475. if (enable && (rec->flags & FTRACE_FL_FILTER))
  1476. ftrace_filtered = 1;
  1477. } while_for_each_ftrace_rec();
  1478. mutex_unlock(&ftrace_lock);
  1479. }
  1480. static int
  1481. ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
  1482. char *regex, int len, int type)
  1483. {
  1484. char str[KSYM_SYMBOL_LEN];
  1485. char *modname;
  1486. kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
  1487. if (!modname || strcmp(modname, mod))
  1488. return 0;
  1489. /* blank search means to match all funcs in the mod */
  1490. if (len)
  1491. return ftrace_match(str, regex, len, type);
  1492. else
  1493. return 1;
  1494. }
  1495. static void ftrace_match_module_records(char *buff, char *mod, int enable)
  1496. {
  1497. unsigned search_len = 0;
  1498. struct ftrace_page *pg;
  1499. struct dyn_ftrace *rec;
  1500. int type = MATCH_FULL;
  1501. char *search = buff;
  1502. unsigned long flag;
  1503. int not = 0;
  1504. flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1505. /* blank or '*' mean the same */
  1506. if (strcmp(buff, "*") == 0)
  1507. buff[0] = 0;
  1508. /* handle the case of 'dont filter this module' */
  1509. if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
  1510. buff[0] = 0;
  1511. not = 1;
  1512. }
  1513. if (strlen(buff)) {
  1514. type = ftrace_setup_glob(buff, strlen(buff), &search, &not);
  1515. search_len = strlen(search);
  1516. }
  1517. mutex_lock(&ftrace_lock);
  1518. do_for_each_ftrace_rec(pg, rec) {
  1519. if (rec->flags & FTRACE_FL_FAILED)
  1520. continue;
  1521. if (ftrace_match_module_record(rec, mod,
  1522. search, search_len, type)) {
  1523. if (not)
  1524. rec->flags &= ~flag;
  1525. else
  1526. rec->flags |= flag;
  1527. }
  1528. if (enable && (rec->flags & FTRACE_FL_FILTER))
  1529. ftrace_filtered = 1;
  1530. } while_for_each_ftrace_rec();
  1531. mutex_unlock(&ftrace_lock);
  1532. }
  1533. /*
  1534. * We register the module command as a template to show others how
  1535. * to register the a command as well.
  1536. */
  1537. static int
  1538. ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
  1539. {
  1540. char *mod;
  1541. /*
  1542. * cmd == 'mod' because we only registered this func
  1543. * for the 'mod' ftrace_func_command.
  1544. * But if you register one func with multiple commands,
  1545. * you can tell which command was used by the cmd
  1546. * parameter.
  1547. */
  1548. /* we must have a module name */
  1549. if (!param)
  1550. return -EINVAL;
  1551. mod = strsep(&param, ":");
  1552. if (!strlen(mod))
  1553. return -EINVAL;
  1554. ftrace_match_module_records(func, mod, enable);
  1555. return 0;
  1556. }
  1557. static struct ftrace_func_command ftrace_mod_cmd = {
  1558. .name = "mod",
  1559. .func = ftrace_mod_callback,
  1560. };
  1561. static int __init ftrace_mod_cmd_init(void)
  1562. {
  1563. return register_ftrace_command(&ftrace_mod_cmd);
  1564. }
  1565. device_initcall(ftrace_mod_cmd_init);
  1566. static void
  1567. function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
  1568. {
  1569. struct ftrace_func_probe *entry;
  1570. struct hlist_head *hhd;
  1571. struct hlist_node *n;
  1572. unsigned long key;
  1573. int resched;
  1574. key = hash_long(ip, FTRACE_HASH_BITS);
  1575. hhd = &ftrace_func_hash[key];
  1576. if (hlist_empty(hhd))
  1577. return;
  1578. /*
  1579. * Disable preemption for these calls to prevent a RCU grace
  1580. * period. This syncs the hash iteration and freeing of items
  1581. * on the hash. rcu_read_lock is too dangerous here.
  1582. */
  1583. resched = ftrace_preempt_disable();
  1584. hlist_for_each_entry_rcu(entry, n, hhd, node) {
  1585. if (entry->ip == ip)
  1586. entry->ops->func(ip, parent_ip, &entry->data);
  1587. }
  1588. ftrace_preempt_enable(resched);
  1589. }
  1590. static struct ftrace_ops trace_probe_ops __read_mostly =
  1591. {
  1592. .func = function_trace_probe_call,
  1593. };
  1594. static int ftrace_probe_registered;
  1595. static void __enable_ftrace_function_probe(void)
  1596. {
  1597. int i;
  1598. if (ftrace_probe_registered)
  1599. return;
  1600. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1601. struct hlist_head *hhd = &ftrace_func_hash[i];
  1602. if (hhd->first)
  1603. break;
  1604. }
  1605. /* Nothing registered? */
  1606. if (i == FTRACE_FUNC_HASHSIZE)
  1607. return;
  1608. __register_ftrace_function(&trace_probe_ops);
  1609. ftrace_startup(0);
  1610. ftrace_probe_registered = 1;
  1611. }
  1612. static void __disable_ftrace_function_probe(void)
  1613. {
  1614. int i;
  1615. if (!ftrace_probe_registered)
  1616. return;
  1617. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1618. struct hlist_head *hhd = &ftrace_func_hash[i];
  1619. if (hhd->first)
  1620. return;
  1621. }
  1622. /* no more funcs left */
  1623. __unregister_ftrace_function(&trace_probe_ops);
  1624. ftrace_shutdown(0);
  1625. ftrace_probe_registered = 0;
  1626. }
  1627. static void ftrace_free_entry_rcu(struct rcu_head *rhp)
  1628. {
  1629. struct ftrace_func_probe *entry =
  1630. container_of(rhp, struct ftrace_func_probe, rcu);
  1631. if (entry->ops->free)
  1632. entry->ops->free(&entry->data);
  1633. kfree(entry);
  1634. }
  1635. int
  1636. register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1637. void *data)
  1638. {
  1639. struct ftrace_func_probe *entry;
  1640. struct ftrace_page *pg;
  1641. struct dyn_ftrace *rec;
  1642. int type, len, not;
  1643. unsigned long key;
  1644. int count = 0;
  1645. char *search;
  1646. type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
  1647. len = strlen(search);
  1648. /* we do not support '!' for function probes */
  1649. if (WARN_ON(not))
  1650. return -EINVAL;
  1651. mutex_lock(&ftrace_lock);
  1652. do_for_each_ftrace_rec(pg, rec) {
  1653. if (rec->flags & FTRACE_FL_FAILED)
  1654. continue;
  1655. if (!ftrace_match_record(rec, search, len, type))
  1656. continue;
  1657. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  1658. if (!entry) {
  1659. /* If we did not process any, then return error */
  1660. if (!count)
  1661. count = -ENOMEM;
  1662. goto out_unlock;
  1663. }
  1664. count++;
  1665. entry->data = data;
  1666. /*
  1667. * The caller might want to do something special
  1668. * for each function we find. We call the callback
  1669. * to give the caller an opportunity to do so.
  1670. */
  1671. if (ops->callback) {
  1672. if (ops->callback(rec->ip, &entry->data) < 0) {
  1673. /* caller does not like this func */
  1674. kfree(entry);
  1675. continue;
  1676. }
  1677. }
  1678. entry->ops = ops;
  1679. entry->ip = rec->ip;
  1680. key = hash_long(entry->ip, FTRACE_HASH_BITS);
  1681. hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
  1682. } while_for_each_ftrace_rec();
  1683. __enable_ftrace_function_probe();
  1684. out_unlock:
  1685. mutex_unlock(&ftrace_lock);
  1686. return count;
  1687. }
  1688. enum {
  1689. PROBE_TEST_FUNC = 1,
  1690. PROBE_TEST_DATA = 2
  1691. };
  1692. static void
  1693. __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1694. void *data, int flags)
  1695. {
  1696. struct ftrace_func_probe *entry;
  1697. struct hlist_node *n, *tmp;
  1698. char str[KSYM_SYMBOL_LEN];
  1699. int type = MATCH_FULL;
  1700. int i, len = 0;
  1701. char *search;
  1702. if (glob && (strcmp(glob, "*") || !strlen(glob)))
  1703. glob = NULL;
  1704. else {
  1705. int not;
  1706. type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
  1707. len = strlen(search);
  1708. /* we do not support '!' for function probes */
  1709. if (WARN_ON(not))
  1710. return;
  1711. }
  1712. mutex_lock(&ftrace_lock);
  1713. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1714. struct hlist_head *hhd = &ftrace_func_hash[i];
  1715. hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
  1716. /* break up if statements for readability */
  1717. if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
  1718. continue;
  1719. if ((flags & PROBE_TEST_DATA) && entry->data != data)
  1720. continue;
  1721. /* do this last, since it is the most expensive */
  1722. if (glob) {
  1723. kallsyms_lookup(entry->ip, NULL, NULL,
  1724. NULL, str);
  1725. if (!ftrace_match(str, glob, len, type))
  1726. continue;
  1727. }
  1728. hlist_del(&entry->node);
  1729. call_rcu(&entry->rcu, ftrace_free_entry_rcu);
  1730. }
  1731. }
  1732. __disable_ftrace_function_probe();
  1733. mutex_unlock(&ftrace_lock);
  1734. }
  1735. void
  1736. unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1737. void *data)
  1738. {
  1739. __unregister_ftrace_function_probe(glob, ops, data,
  1740. PROBE_TEST_FUNC | PROBE_TEST_DATA);
  1741. }
  1742. void
  1743. unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
  1744. {
  1745. __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
  1746. }
  1747. void unregister_ftrace_function_probe_all(char *glob)
  1748. {
  1749. __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
  1750. }
  1751. static LIST_HEAD(ftrace_commands);
  1752. static DEFINE_MUTEX(ftrace_cmd_mutex);
  1753. int register_ftrace_command(struct ftrace_func_command *cmd)
  1754. {
  1755. struct ftrace_func_command *p;
  1756. int ret = 0;
  1757. mutex_lock(&ftrace_cmd_mutex);
  1758. list_for_each_entry(p, &ftrace_commands, list) {
  1759. if (strcmp(cmd->name, p->name) == 0) {
  1760. ret = -EBUSY;
  1761. goto out_unlock;
  1762. }
  1763. }
  1764. list_add(&cmd->list, &ftrace_commands);
  1765. out_unlock:
  1766. mutex_unlock(&ftrace_cmd_mutex);
  1767. return ret;
  1768. }
  1769. int unregister_ftrace_command(struct ftrace_func_command *cmd)
  1770. {
  1771. struct ftrace_func_command *p, *n;
  1772. int ret = -ENODEV;
  1773. mutex_lock(&ftrace_cmd_mutex);
  1774. list_for_each_entry_safe(p, n, &ftrace_commands, list) {
  1775. if (strcmp(cmd->name, p->name) == 0) {
  1776. ret = 0;
  1777. list_del_init(&p->list);
  1778. goto out_unlock;
  1779. }
  1780. }
  1781. out_unlock:
  1782. mutex_unlock(&ftrace_cmd_mutex);
  1783. return ret;
  1784. }
  1785. static int ftrace_process_regex(char *buff, int len, int enable)
  1786. {
  1787. char *func, *command, *next = buff;
  1788. struct ftrace_func_command *p;
  1789. int ret = -EINVAL;
  1790. func = strsep(&next, ":");
  1791. if (!next) {
  1792. ftrace_match_records(func, len, enable);
  1793. return 0;
  1794. }
  1795. /* command found */
  1796. command = strsep(&next, ":");
  1797. mutex_lock(&ftrace_cmd_mutex);
  1798. list_for_each_entry(p, &ftrace_commands, list) {
  1799. if (strcmp(p->name, command) == 0) {
  1800. ret = p->func(func, command, next, enable);
  1801. goto out_unlock;
  1802. }
  1803. }
  1804. out_unlock:
  1805. mutex_unlock(&ftrace_cmd_mutex);
  1806. return ret;
  1807. }
  1808. static ssize_t
  1809. ftrace_regex_write(struct file *file, const char __user *ubuf,
  1810. size_t cnt, loff_t *ppos, int enable)
  1811. {
  1812. struct ftrace_iterator *iter;
  1813. char ch;
  1814. size_t read = 0;
  1815. ssize_t ret;
  1816. if (!cnt || cnt < 0)
  1817. return 0;
  1818. mutex_lock(&ftrace_regex_lock);
  1819. if (file->f_mode & FMODE_READ) {
  1820. struct seq_file *m = file->private_data;
  1821. iter = m->private;
  1822. } else
  1823. iter = file->private_data;
  1824. if (!*ppos) {
  1825. iter->flags &= ~FTRACE_ITER_CONT;
  1826. iter->buffer_idx = 0;
  1827. }
  1828. ret = get_user(ch, ubuf++);
  1829. if (ret)
  1830. goto out;
  1831. read++;
  1832. cnt--;
  1833. if (!(iter->flags & ~FTRACE_ITER_CONT)) {
  1834. /* skip white space */
  1835. while (cnt && isspace(ch)) {
  1836. ret = get_user(ch, ubuf++);
  1837. if (ret)
  1838. goto out;
  1839. read++;
  1840. cnt--;
  1841. }
  1842. if (isspace(ch)) {
  1843. file->f_pos += read;
  1844. ret = read;
  1845. goto out;
  1846. }
  1847. iter->buffer_idx = 0;
  1848. }
  1849. while (cnt && !isspace(ch)) {
  1850. if (iter->buffer_idx < FTRACE_BUFF_MAX)
  1851. iter->buffer[iter->buffer_idx++] = ch;
  1852. else {
  1853. ret = -EINVAL;
  1854. goto out;
  1855. }
  1856. ret = get_user(ch, ubuf++);
  1857. if (ret)
  1858. goto out;
  1859. read++;
  1860. cnt--;
  1861. }
  1862. if (isspace(ch)) {
  1863. iter->filtered++;
  1864. iter->buffer[iter->buffer_idx] = 0;
  1865. ret = ftrace_process_regex(iter->buffer,
  1866. iter->buffer_idx, enable);
  1867. if (ret)
  1868. goto out;
  1869. iter->buffer_idx = 0;
  1870. } else
  1871. iter->flags |= FTRACE_ITER_CONT;
  1872. file->f_pos += read;
  1873. ret = read;
  1874. out:
  1875. mutex_unlock(&ftrace_regex_lock);
  1876. return ret;
  1877. }
  1878. static ssize_t
  1879. ftrace_filter_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, 1);
  1883. }
  1884. static ssize_t
  1885. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  1886. size_t cnt, loff_t *ppos)
  1887. {
  1888. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  1889. }
  1890. static void
  1891. ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
  1892. {
  1893. if (unlikely(ftrace_disabled))
  1894. return;
  1895. mutex_lock(&ftrace_regex_lock);
  1896. if (reset)
  1897. ftrace_filter_reset(enable);
  1898. if (buf)
  1899. ftrace_match_records(buf, len, enable);
  1900. mutex_unlock(&ftrace_regex_lock);
  1901. }
  1902. /**
  1903. * ftrace_set_filter - set a function to filter on in ftrace
  1904. * @buf - the string that holds the function filter text.
  1905. * @len - the length of the string.
  1906. * @reset - non zero to reset all filters before applying this filter.
  1907. *
  1908. * Filters denote which functions should be enabled when tracing is enabled.
  1909. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  1910. */
  1911. void ftrace_set_filter(unsigned char *buf, int len, int reset)
  1912. {
  1913. ftrace_set_regex(buf, len, reset, 1);
  1914. }
  1915. /**
  1916. * ftrace_set_notrace - set a function to not trace in ftrace
  1917. * @buf - the string that holds the function notrace text.
  1918. * @len - the length of the string.
  1919. * @reset - non zero to reset all filters before applying this filter.
  1920. *
  1921. * Notrace Filters denote which functions should not be enabled when tracing
  1922. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  1923. * for tracing.
  1924. */
  1925. void ftrace_set_notrace(unsigned char *buf, int len, int reset)
  1926. {
  1927. ftrace_set_regex(buf, len, reset, 0);
  1928. }
  1929. /*
  1930. * command line interface to allow users to set filters on boot up.
  1931. */
  1932. #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
  1933. static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
  1934. static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
  1935. static int __init set_ftrace_notrace(char *str)
  1936. {
  1937. strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
  1938. return 1;
  1939. }
  1940. __setup("ftrace_notrace=", set_ftrace_notrace);
  1941. static int __init set_ftrace_filter(char *str)
  1942. {
  1943. strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
  1944. return 1;
  1945. }
  1946. __setup("ftrace_filter=", set_ftrace_filter);
  1947. static void __init set_ftrace_early_filter(char *buf, int enable)
  1948. {
  1949. char *func;
  1950. while (buf) {
  1951. func = strsep(&buf, ",");
  1952. ftrace_set_regex(func, strlen(func), 0, enable);
  1953. }
  1954. }
  1955. static void __init set_ftrace_early_filters(void)
  1956. {
  1957. if (ftrace_filter_buf[0])
  1958. set_ftrace_early_filter(ftrace_filter_buf, 1);
  1959. if (ftrace_notrace_buf[0])
  1960. set_ftrace_early_filter(ftrace_notrace_buf, 0);
  1961. }
  1962. static int
  1963. ftrace_regex_release(struct inode *inode, struct file *file, int enable)
  1964. {
  1965. struct seq_file *m = (struct seq_file *)file->private_data;
  1966. struct ftrace_iterator *iter;
  1967. mutex_lock(&ftrace_regex_lock);
  1968. if (file->f_mode & FMODE_READ) {
  1969. iter = m->private;
  1970. seq_release(inode, file);
  1971. } else
  1972. iter = file->private_data;
  1973. if (iter->buffer_idx) {
  1974. iter->filtered++;
  1975. iter->buffer[iter->buffer_idx] = 0;
  1976. ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
  1977. }
  1978. mutex_lock(&ftrace_lock);
  1979. if (ftrace_start_up && ftrace_enabled)
  1980. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  1981. mutex_unlock(&ftrace_lock);
  1982. kfree(iter);
  1983. mutex_unlock(&ftrace_regex_lock);
  1984. return 0;
  1985. }
  1986. static int
  1987. ftrace_filter_release(struct inode *inode, struct file *file)
  1988. {
  1989. return ftrace_regex_release(inode, file, 1);
  1990. }
  1991. static int
  1992. ftrace_notrace_release(struct inode *inode, struct file *file)
  1993. {
  1994. return ftrace_regex_release(inode, file, 0);
  1995. }
  1996. static const struct file_operations ftrace_avail_fops = {
  1997. .open = ftrace_avail_open,
  1998. .read = seq_read,
  1999. .llseek = seq_lseek,
  2000. .release = ftrace_avail_release,
  2001. };
  2002. static const struct file_operations ftrace_failures_fops = {
  2003. .open = ftrace_failures_open,
  2004. .read = seq_read,
  2005. .llseek = seq_lseek,
  2006. .release = ftrace_avail_release,
  2007. };
  2008. static const struct file_operations ftrace_filter_fops = {
  2009. .open = ftrace_filter_open,
  2010. .read = seq_read,
  2011. .write = ftrace_filter_write,
  2012. .llseek = ftrace_regex_lseek,
  2013. .release = ftrace_filter_release,
  2014. };
  2015. static const struct file_operations ftrace_notrace_fops = {
  2016. .open = ftrace_notrace_open,
  2017. .read = seq_read,
  2018. .write = ftrace_notrace_write,
  2019. .llseek = ftrace_regex_lseek,
  2020. .release = ftrace_notrace_release,
  2021. };
  2022. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2023. static DEFINE_MUTEX(graph_lock);
  2024. int ftrace_graph_count;
  2025. unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
  2026. static void *
  2027. g_next(struct seq_file *m, void *v, loff_t *pos)
  2028. {
  2029. unsigned long *array = m->private;
  2030. int index = *pos;
  2031. (*pos)++;
  2032. if (index >= ftrace_graph_count)
  2033. return NULL;
  2034. return &array[index];
  2035. }
  2036. static void *g_start(struct seq_file *m, loff_t *pos)
  2037. {
  2038. void *p = NULL;
  2039. mutex_lock(&graph_lock);
  2040. /* Nothing, tell g_show to print all functions are enabled */
  2041. if (!ftrace_graph_count && !*pos)
  2042. return (void *)1;
  2043. p = g_next(m, p, pos);
  2044. return p;
  2045. }
  2046. static void g_stop(struct seq_file *m, void *p)
  2047. {
  2048. mutex_unlock(&graph_lock);
  2049. }
  2050. static int g_show(struct seq_file *m, void *v)
  2051. {
  2052. unsigned long *ptr = v;
  2053. char str[KSYM_SYMBOL_LEN];
  2054. if (!ptr)
  2055. return 0;
  2056. if (ptr == (unsigned long *)1) {
  2057. seq_printf(m, "#### all functions enabled ####\n");
  2058. return 0;
  2059. }
  2060. kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
  2061. seq_printf(m, "%s\n", str);
  2062. return 0;
  2063. }
  2064. static struct seq_operations ftrace_graph_seq_ops = {
  2065. .start = g_start,
  2066. .next = g_next,
  2067. .stop = g_stop,
  2068. .show = g_show,
  2069. };
  2070. static int
  2071. ftrace_graph_open(struct inode *inode, struct file *file)
  2072. {
  2073. int ret = 0;
  2074. if (unlikely(ftrace_disabled))
  2075. return -ENODEV;
  2076. mutex_lock(&graph_lock);
  2077. if ((file->f_mode & FMODE_WRITE) &&
  2078. !(file->f_flags & O_APPEND)) {
  2079. ftrace_graph_count = 0;
  2080. memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
  2081. }
  2082. if (file->f_mode & FMODE_READ) {
  2083. ret = seq_open(file, &ftrace_graph_seq_ops);
  2084. if (!ret) {
  2085. struct seq_file *m = file->private_data;
  2086. m->private = ftrace_graph_funcs;
  2087. }
  2088. } else
  2089. file->private_data = ftrace_graph_funcs;
  2090. mutex_unlock(&graph_lock);
  2091. return ret;
  2092. }
  2093. static int
  2094. ftrace_set_func(unsigned long *array, int *idx, char *buffer)
  2095. {
  2096. struct dyn_ftrace *rec;
  2097. struct ftrace_page *pg;
  2098. int search_len;
  2099. int found = 0;
  2100. int type, not;
  2101. char *search;
  2102. bool exists;
  2103. int i;
  2104. if (ftrace_disabled)
  2105. return -ENODEV;
  2106. /* decode regex */
  2107. type = ftrace_setup_glob(buffer, strlen(buffer), &search, &not);
  2108. if (not)
  2109. return -EINVAL;
  2110. search_len = strlen(search);
  2111. mutex_lock(&ftrace_lock);
  2112. do_for_each_ftrace_rec(pg, rec) {
  2113. if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
  2114. break;
  2115. if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
  2116. continue;
  2117. if (ftrace_match_record(rec, search, search_len, type)) {
  2118. /* ensure it is not already in the array */
  2119. exists = false;
  2120. for (i = 0; i < *idx; i++)
  2121. if (array[i] == rec->ip) {
  2122. exists = true;
  2123. break;
  2124. }
  2125. if (!exists) {
  2126. array[(*idx)++] = rec->ip;
  2127. found = 1;
  2128. }
  2129. }
  2130. } while_for_each_ftrace_rec();
  2131. mutex_unlock(&ftrace_lock);
  2132. return found ? 0 : -EINVAL;
  2133. }
  2134. static ssize_t
  2135. ftrace_graph_write(struct file *file, const char __user *ubuf,
  2136. size_t cnt, loff_t *ppos)
  2137. {
  2138. unsigned char buffer[FTRACE_BUFF_MAX+1];
  2139. unsigned long *array;
  2140. size_t read = 0;
  2141. ssize_t ret;
  2142. int index = 0;
  2143. char ch;
  2144. if (!cnt || cnt < 0)
  2145. return 0;
  2146. mutex_lock(&graph_lock);
  2147. if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
  2148. ret = -EBUSY;
  2149. goto out;
  2150. }
  2151. if (file->f_mode & FMODE_READ) {
  2152. struct seq_file *m = file->private_data;
  2153. array = m->private;
  2154. } else
  2155. array = file->private_data;
  2156. ret = get_user(ch, ubuf++);
  2157. if (ret)
  2158. goto out;
  2159. read++;
  2160. cnt--;
  2161. /* skip white space */
  2162. while (cnt && isspace(ch)) {
  2163. ret = get_user(ch, ubuf++);
  2164. if (ret)
  2165. goto out;
  2166. read++;
  2167. cnt--;
  2168. }
  2169. if (isspace(ch)) {
  2170. *ppos += read;
  2171. ret = read;
  2172. goto out;
  2173. }
  2174. while (cnt && !isspace(ch)) {
  2175. if (index < FTRACE_BUFF_MAX)
  2176. buffer[index++] = ch;
  2177. else {
  2178. ret = -EINVAL;
  2179. goto out;
  2180. }
  2181. ret = get_user(ch, ubuf++);
  2182. if (ret)
  2183. goto out;
  2184. read++;
  2185. cnt--;
  2186. }
  2187. buffer[index] = 0;
  2188. /* we allow only one expression at a time */
  2189. ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
  2190. if (ret)
  2191. goto out;
  2192. file->f_pos += read;
  2193. ret = read;
  2194. out:
  2195. mutex_unlock(&graph_lock);
  2196. return ret;
  2197. }
  2198. static const struct file_operations ftrace_graph_fops = {
  2199. .open = ftrace_graph_open,
  2200. .read = seq_read,
  2201. .write = ftrace_graph_write,
  2202. };
  2203. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2204. static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
  2205. {
  2206. trace_create_file("available_filter_functions", 0444,
  2207. d_tracer, NULL, &ftrace_avail_fops);
  2208. trace_create_file("failures", 0444,
  2209. d_tracer, NULL, &ftrace_failures_fops);
  2210. trace_create_file("set_ftrace_filter", 0644, d_tracer,
  2211. NULL, &ftrace_filter_fops);
  2212. trace_create_file("set_ftrace_notrace", 0644, d_tracer,
  2213. NULL, &ftrace_notrace_fops);
  2214. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2215. trace_create_file("set_graph_function", 0444, d_tracer,
  2216. NULL,
  2217. &ftrace_graph_fops);
  2218. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2219. return 0;
  2220. }
  2221. static int ftrace_convert_nops(struct module *mod,
  2222. unsigned long *start,
  2223. unsigned long *end)
  2224. {
  2225. unsigned long *p;
  2226. unsigned long addr;
  2227. unsigned long flags;
  2228. mutex_lock(&ftrace_lock);
  2229. p = start;
  2230. while (p < end) {
  2231. addr = ftrace_call_adjust(*p++);
  2232. /*
  2233. * Some architecture linkers will pad between
  2234. * the different mcount_loc sections of different
  2235. * object files to satisfy alignments.
  2236. * Skip any NULL pointers.
  2237. */
  2238. if (!addr)
  2239. continue;
  2240. ftrace_record_ip(addr);
  2241. }
  2242. /* disable interrupts to prevent kstop machine */
  2243. local_irq_save(flags);
  2244. ftrace_update_code(mod);
  2245. local_irq_restore(flags);
  2246. mutex_unlock(&ftrace_lock);
  2247. return 0;
  2248. }
  2249. #ifdef CONFIG_MODULES
  2250. void ftrace_release(void *start, void *end)
  2251. {
  2252. struct dyn_ftrace *rec;
  2253. struct ftrace_page *pg;
  2254. unsigned long s = (unsigned long)start;
  2255. unsigned long e = (unsigned long)end;
  2256. if (ftrace_disabled || !start || start == end)
  2257. return;
  2258. mutex_lock(&ftrace_lock);
  2259. do_for_each_ftrace_rec(pg, rec) {
  2260. if ((rec->ip >= s) && (rec->ip < e)) {
  2261. /*
  2262. * rec->ip is changed in ftrace_free_rec()
  2263. * It should not between s and e if record was freed.
  2264. */
  2265. FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
  2266. ftrace_free_rec(rec);
  2267. }
  2268. } while_for_each_ftrace_rec();
  2269. mutex_unlock(&ftrace_lock);
  2270. }
  2271. static void ftrace_init_module(struct module *mod,
  2272. unsigned long *start, unsigned long *end)
  2273. {
  2274. if (ftrace_disabled || start == end)
  2275. return;
  2276. ftrace_convert_nops(mod, start, end);
  2277. }
  2278. static int ftrace_module_notify(struct notifier_block *self,
  2279. unsigned long val, void *data)
  2280. {
  2281. struct module *mod = data;
  2282. switch (val) {
  2283. case MODULE_STATE_COMING:
  2284. ftrace_init_module(mod, mod->ftrace_callsites,
  2285. mod->ftrace_callsites +
  2286. mod->num_ftrace_callsites);
  2287. break;
  2288. case MODULE_STATE_GOING:
  2289. ftrace_release(mod->ftrace_callsites,
  2290. mod->ftrace_callsites +
  2291. mod->num_ftrace_callsites);
  2292. break;
  2293. }
  2294. return 0;
  2295. }
  2296. #else
  2297. static int ftrace_module_notify(struct notifier_block *self,
  2298. unsigned long val, void *data)
  2299. {
  2300. return 0;
  2301. }
  2302. #endif /* CONFIG_MODULES */
  2303. struct notifier_block ftrace_module_nb = {
  2304. .notifier_call = ftrace_module_notify,
  2305. .priority = 0,
  2306. };
  2307. extern unsigned long __start_mcount_loc[];
  2308. extern unsigned long __stop_mcount_loc[];
  2309. void __init ftrace_init(void)
  2310. {
  2311. unsigned long count, addr, flags;
  2312. int ret;
  2313. /* Keep the ftrace pointer to the stub */
  2314. addr = (unsigned long)ftrace_stub;
  2315. local_irq_save(flags);
  2316. ftrace_dyn_arch_init(&addr);
  2317. local_irq_restore(flags);
  2318. /* ftrace_dyn_arch_init places the return code in addr */
  2319. if (addr)
  2320. goto failed;
  2321. count = __stop_mcount_loc - __start_mcount_loc;
  2322. ret = ftrace_dyn_table_alloc(count);
  2323. if (ret)
  2324. goto failed;
  2325. last_ftrace_enabled = ftrace_enabled = 1;
  2326. ret = ftrace_convert_nops(NULL,
  2327. __start_mcount_loc,
  2328. __stop_mcount_loc);
  2329. ret = register_module_notifier(&ftrace_module_nb);
  2330. if (ret)
  2331. pr_warning("Failed to register trace ftrace module notifier\n");
  2332. set_ftrace_early_filters();
  2333. return;
  2334. failed:
  2335. ftrace_disabled = 1;
  2336. }
  2337. #else
  2338. static int __init ftrace_nodyn_init(void)
  2339. {
  2340. ftrace_enabled = 1;
  2341. return 0;
  2342. }
  2343. device_initcall(ftrace_nodyn_init);
  2344. static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
  2345. static inline void ftrace_startup_enable(int command) { }
  2346. /* Keep as macros so we do not need to define the commands */
  2347. # define ftrace_startup(command) do { } while (0)
  2348. # define ftrace_shutdown(command) do { } while (0)
  2349. # define ftrace_startup_sysctl() do { } while (0)
  2350. # define ftrace_shutdown_sysctl() do { } while (0)
  2351. #endif /* CONFIG_DYNAMIC_FTRACE */
  2352. static ssize_t
  2353. ftrace_pid_read(struct file *file, char __user *ubuf,
  2354. size_t cnt, loff_t *ppos)
  2355. {
  2356. char buf[64];
  2357. int r;
  2358. if (ftrace_pid_trace == ftrace_swapper_pid)
  2359. r = sprintf(buf, "swapper tasks\n");
  2360. else if (ftrace_pid_trace)
  2361. r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
  2362. else
  2363. r = sprintf(buf, "no pid\n");
  2364. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2365. }
  2366. static void clear_ftrace_swapper(void)
  2367. {
  2368. struct task_struct *p;
  2369. int cpu;
  2370. get_online_cpus();
  2371. for_each_online_cpu(cpu) {
  2372. p = idle_task(cpu);
  2373. clear_tsk_trace_trace(p);
  2374. }
  2375. put_online_cpus();
  2376. }
  2377. static void set_ftrace_swapper(void)
  2378. {
  2379. struct task_struct *p;
  2380. int cpu;
  2381. get_online_cpus();
  2382. for_each_online_cpu(cpu) {
  2383. p = idle_task(cpu);
  2384. set_tsk_trace_trace(p);
  2385. }
  2386. put_online_cpus();
  2387. }
  2388. static void clear_ftrace_pid(struct pid *pid)
  2389. {
  2390. struct task_struct *p;
  2391. rcu_read_lock();
  2392. do_each_pid_task(pid, PIDTYPE_PID, p) {
  2393. clear_tsk_trace_trace(p);
  2394. } while_each_pid_task(pid, PIDTYPE_PID, p);
  2395. rcu_read_unlock();
  2396. put_pid(pid);
  2397. }
  2398. static void set_ftrace_pid(struct pid *pid)
  2399. {
  2400. struct task_struct *p;
  2401. rcu_read_lock();
  2402. do_each_pid_task(pid, PIDTYPE_PID, p) {
  2403. set_tsk_trace_trace(p);
  2404. } while_each_pid_task(pid, PIDTYPE_PID, p);
  2405. rcu_read_unlock();
  2406. }
  2407. static void clear_ftrace_pid_task(struct pid **pid)
  2408. {
  2409. if (*pid == ftrace_swapper_pid)
  2410. clear_ftrace_swapper();
  2411. else
  2412. clear_ftrace_pid(*pid);
  2413. *pid = NULL;
  2414. }
  2415. static void set_ftrace_pid_task(struct pid *pid)
  2416. {
  2417. if (pid == ftrace_swapper_pid)
  2418. set_ftrace_swapper();
  2419. else
  2420. set_ftrace_pid(pid);
  2421. }
  2422. static ssize_t
  2423. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  2424. size_t cnt, loff_t *ppos)
  2425. {
  2426. struct pid *pid;
  2427. char buf[64];
  2428. long val;
  2429. int ret;
  2430. if (cnt >= sizeof(buf))
  2431. return -EINVAL;
  2432. if (copy_from_user(&buf, ubuf, cnt))
  2433. return -EFAULT;
  2434. buf[cnt] = 0;
  2435. ret = strict_strtol(buf, 10, &val);
  2436. if (ret < 0)
  2437. return ret;
  2438. mutex_lock(&ftrace_lock);
  2439. if (val < 0) {
  2440. /* disable pid tracing */
  2441. if (!ftrace_pid_trace)
  2442. goto out;
  2443. clear_ftrace_pid_task(&ftrace_pid_trace);
  2444. } else {
  2445. /* swapper task is special */
  2446. if (!val) {
  2447. pid = ftrace_swapper_pid;
  2448. if (pid == ftrace_pid_trace)
  2449. goto out;
  2450. } else {
  2451. pid = find_get_pid(val);
  2452. if (pid == ftrace_pid_trace) {
  2453. put_pid(pid);
  2454. goto out;
  2455. }
  2456. }
  2457. if (ftrace_pid_trace)
  2458. clear_ftrace_pid_task(&ftrace_pid_trace);
  2459. if (!pid)
  2460. goto out;
  2461. ftrace_pid_trace = pid;
  2462. set_ftrace_pid_task(ftrace_pid_trace);
  2463. }
  2464. /* update the function call */
  2465. ftrace_update_pid_func();
  2466. ftrace_startup_enable(0);
  2467. out:
  2468. mutex_unlock(&ftrace_lock);
  2469. return cnt;
  2470. }
  2471. static const struct file_operations ftrace_pid_fops = {
  2472. .read = ftrace_pid_read,
  2473. .write = ftrace_pid_write,
  2474. };
  2475. static __init int ftrace_init_debugfs(void)
  2476. {
  2477. struct dentry *d_tracer;
  2478. d_tracer = tracing_init_dentry();
  2479. if (!d_tracer)
  2480. return 0;
  2481. ftrace_init_dyn_debugfs(d_tracer);
  2482. trace_create_file("set_ftrace_pid", 0644, d_tracer,
  2483. NULL, &ftrace_pid_fops);
  2484. ftrace_profile_debugfs(d_tracer);
  2485. return 0;
  2486. }
  2487. fs_initcall(ftrace_init_debugfs);
  2488. /**
  2489. * ftrace_kill - kill ftrace
  2490. *
  2491. * This function should be used by panic code. It stops ftrace
  2492. * but in a not so nice way. If you need to simply kill ftrace
  2493. * from a non-atomic section, use ftrace_kill.
  2494. */
  2495. void ftrace_kill(void)
  2496. {
  2497. ftrace_disabled = 1;
  2498. ftrace_enabled = 0;
  2499. clear_ftrace_function();
  2500. }
  2501. /**
  2502. * register_ftrace_function - register a function for profiling
  2503. * @ops - ops structure that holds the function for profiling.
  2504. *
  2505. * Register a function to be called by all functions in the
  2506. * kernel.
  2507. *
  2508. * Note: @ops->func and all the functions it calls must be labeled
  2509. * with "notrace", otherwise it will go into a
  2510. * recursive loop.
  2511. */
  2512. int register_ftrace_function(struct ftrace_ops *ops)
  2513. {
  2514. int ret;
  2515. if (unlikely(ftrace_disabled))
  2516. return -1;
  2517. mutex_lock(&ftrace_lock);
  2518. ret = __register_ftrace_function(ops);
  2519. ftrace_startup(0);
  2520. mutex_unlock(&ftrace_lock);
  2521. return ret;
  2522. }
  2523. /**
  2524. * unregister_ftrace_function - unregister a function for profiling.
  2525. * @ops - ops structure that holds the function to unregister
  2526. *
  2527. * Unregister a function that was added to be called by ftrace profiling.
  2528. */
  2529. int unregister_ftrace_function(struct ftrace_ops *ops)
  2530. {
  2531. int ret;
  2532. mutex_lock(&ftrace_lock);
  2533. ret = __unregister_ftrace_function(ops);
  2534. ftrace_shutdown(0);
  2535. mutex_unlock(&ftrace_lock);
  2536. return ret;
  2537. }
  2538. int
  2539. ftrace_enable_sysctl(struct ctl_table *table, int write,
  2540. struct file *file, void __user *buffer, size_t *lenp,
  2541. loff_t *ppos)
  2542. {
  2543. int ret;
  2544. if (unlikely(ftrace_disabled))
  2545. return -ENODEV;
  2546. mutex_lock(&ftrace_lock);
  2547. ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
  2548. if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
  2549. goto out;
  2550. last_ftrace_enabled = ftrace_enabled;
  2551. if (ftrace_enabled) {
  2552. ftrace_startup_sysctl();
  2553. /* we are starting ftrace again */
  2554. if (ftrace_list != &ftrace_list_end) {
  2555. if (ftrace_list->next == &ftrace_list_end)
  2556. ftrace_trace_function = ftrace_list->func;
  2557. else
  2558. ftrace_trace_function = ftrace_list_func;
  2559. }
  2560. } else {
  2561. /* stopping ftrace calls (just send to ftrace_stub) */
  2562. ftrace_trace_function = ftrace_stub;
  2563. ftrace_shutdown_sysctl();
  2564. }
  2565. out:
  2566. mutex_unlock(&ftrace_lock);
  2567. return ret;
  2568. }
  2569. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2570. static int ftrace_graph_active;
  2571. static struct notifier_block ftrace_suspend_notifier;
  2572. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
  2573. {
  2574. return 0;
  2575. }
  2576. /* The callbacks that hook a function */
  2577. trace_func_graph_ret_t ftrace_graph_return =
  2578. (trace_func_graph_ret_t)ftrace_stub;
  2579. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  2580. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  2581. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  2582. {
  2583. int i;
  2584. int ret = 0;
  2585. unsigned long flags;
  2586. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  2587. struct task_struct *g, *t;
  2588. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  2589. ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
  2590. * sizeof(struct ftrace_ret_stack),
  2591. GFP_KERNEL);
  2592. if (!ret_stack_list[i]) {
  2593. start = 0;
  2594. end = i;
  2595. ret = -ENOMEM;
  2596. goto free;
  2597. }
  2598. }
  2599. read_lock_irqsave(&tasklist_lock, flags);
  2600. do_each_thread(g, t) {
  2601. if (start == end) {
  2602. ret = -EAGAIN;
  2603. goto unlock;
  2604. }
  2605. if (t->ret_stack == NULL) {
  2606. atomic_set(&t->tracing_graph_pause, 0);
  2607. atomic_set(&t->trace_overrun, 0);
  2608. t->curr_ret_stack = -1;
  2609. /* Make sure the tasks see the -1 first: */
  2610. smp_wmb();
  2611. t->ret_stack = ret_stack_list[start++];
  2612. }
  2613. } while_each_thread(g, t);
  2614. unlock:
  2615. read_unlock_irqrestore(&tasklist_lock, flags);
  2616. free:
  2617. for (i = start; i < end; i++)
  2618. kfree(ret_stack_list[i]);
  2619. return ret;
  2620. }
  2621. static void
  2622. ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
  2623. struct task_struct *next)
  2624. {
  2625. unsigned long long timestamp;
  2626. int index;
  2627. /*
  2628. * Does the user want to count the time a function was asleep.
  2629. * If so, do not update the time stamps.
  2630. */
  2631. if (trace_flags & TRACE_ITER_SLEEP_TIME)
  2632. return;
  2633. timestamp = trace_clock_local();
  2634. prev->ftrace_timestamp = timestamp;
  2635. /* only process tasks that we timestamped */
  2636. if (!next->ftrace_timestamp)
  2637. return;
  2638. /*
  2639. * Update all the counters in next to make up for the
  2640. * time next was sleeping.
  2641. */
  2642. timestamp -= next->ftrace_timestamp;
  2643. for (index = next->curr_ret_stack; index >= 0; index--)
  2644. next->ret_stack[index].calltime += timestamp;
  2645. }
  2646. /* Allocate a return stack for each task */
  2647. static int start_graph_tracing(void)
  2648. {
  2649. struct ftrace_ret_stack **ret_stack_list;
  2650. int ret, cpu;
  2651. ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
  2652. sizeof(struct ftrace_ret_stack *),
  2653. GFP_KERNEL);
  2654. if (!ret_stack_list)
  2655. return -ENOMEM;
  2656. /* The cpu_boot init_task->ret_stack will never be freed */
  2657. for_each_online_cpu(cpu) {
  2658. if (!idle_task(cpu)->ret_stack)
  2659. ftrace_graph_init_task(idle_task(cpu));
  2660. }
  2661. do {
  2662. ret = alloc_retstack_tasklist(ret_stack_list);
  2663. } while (ret == -EAGAIN);
  2664. if (!ret) {
  2665. ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
  2666. if (ret)
  2667. pr_info("ftrace_graph: Couldn't activate tracepoint"
  2668. " probe to kernel_sched_switch\n");
  2669. }
  2670. kfree(ret_stack_list);
  2671. return ret;
  2672. }
  2673. /*
  2674. * Hibernation protection.
  2675. * The state of the current task is too much unstable during
  2676. * suspend/restore to disk. We want to protect against that.
  2677. */
  2678. static int
  2679. ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
  2680. void *unused)
  2681. {
  2682. switch (state) {
  2683. case PM_HIBERNATION_PREPARE:
  2684. pause_graph_tracing();
  2685. break;
  2686. case PM_POST_HIBERNATION:
  2687. unpause_graph_tracing();
  2688. break;
  2689. }
  2690. return NOTIFY_DONE;
  2691. }
  2692. int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  2693. trace_func_graph_ent_t entryfunc)
  2694. {
  2695. int ret = 0;
  2696. mutex_lock(&ftrace_lock);
  2697. /* we currently allow only one tracer registered at a time */
  2698. if (ftrace_graph_active) {
  2699. ret = -EBUSY;
  2700. goto out;
  2701. }
  2702. ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
  2703. register_pm_notifier(&ftrace_suspend_notifier);
  2704. ftrace_graph_active++;
  2705. ret = start_graph_tracing();
  2706. if (ret) {
  2707. ftrace_graph_active--;
  2708. goto out;
  2709. }
  2710. ftrace_graph_return = retfunc;
  2711. ftrace_graph_entry = entryfunc;
  2712. ftrace_startup(FTRACE_START_FUNC_RET);
  2713. out:
  2714. mutex_unlock(&ftrace_lock);
  2715. return ret;
  2716. }
  2717. void unregister_ftrace_graph(void)
  2718. {
  2719. mutex_lock(&ftrace_lock);
  2720. if (unlikely(!ftrace_graph_active))
  2721. goto out;
  2722. ftrace_graph_active--;
  2723. unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
  2724. ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
  2725. ftrace_graph_entry = ftrace_graph_entry_stub;
  2726. ftrace_shutdown(FTRACE_STOP_FUNC_RET);
  2727. unregister_pm_notifier(&ftrace_suspend_notifier);
  2728. out:
  2729. mutex_unlock(&ftrace_lock);
  2730. }
  2731. /* Allocate a return stack for newly created task */
  2732. void ftrace_graph_init_task(struct task_struct *t)
  2733. {
  2734. /* Make sure we do not use the parent ret_stack */
  2735. t->ret_stack = NULL;
  2736. if (ftrace_graph_active) {
  2737. struct ftrace_ret_stack *ret_stack;
  2738. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  2739. * sizeof(struct ftrace_ret_stack),
  2740. GFP_KERNEL);
  2741. if (!ret_stack)
  2742. return;
  2743. t->curr_ret_stack = -1;
  2744. atomic_set(&t->tracing_graph_pause, 0);
  2745. atomic_set(&t->trace_overrun, 0);
  2746. t->ftrace_timestamp = 0;
  2747. /* make curr_ret_stack visable before we add the ret_stack */
  2748. smp_wmb();
  2749. t->ret_stack = ret_stack;
  2750. }
  2751. }
  2752. void ftrace_graph_exit_task(struct task_struct *t)
  2753. {
  2754. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  2755. t->ret_stack = NULL;
  2756. /* NULL must become visible to IRQs before we free it: */
  2757. barrier();
  2758. kfree(ret_stack);
  2759. }
  2760. void ftrace_graph_stop(void)
  2761. {
  2762. ftrace_stop();
  2763. }
  2764. #endif