ftrace.c 55 KB

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