ftrace.c 71 KB

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