ftrace.c 71 KB

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