ftrace.c 73 KB

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