ftrace.c 71 KB

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