ftrace.c 57 KB

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