ftrace.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450
  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. };
  658. /* used to initialize the real stat files */
  659. static struct tracer_stat function_stats __initdata = {
  660. .name = "functions",
  661. .stat_start = function_stat_start,
  662. .stat_next = function_stat_next,
  663. .stat_cmp = function_stat_cmp,
  664. .stat_headers = function_stat_headers,
  665. .stat_show = function_stat_show
  666. };
  667. static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
  668. {
  669. struct ftrace_profile_stat *stat;
  670. struct dentry *entry;
  671. char *name;
  672. int ret;
  673. int cpu;
  674. for_each_possible_cpu(cpu) {
  675. stat = &per_cpu(ftrace_profile_stats, cpu);
  676. /* allocate enough for function name + cpu number */
  677. name = kmalloc(32, GFP_KERNEL);
  678. if (!name) {
  679. /*
  680. * The files created are permanent, if something happens
  681. * we still do not free memory.
  682. */
  683. WARN(1,
  684. "Could not allocate stat file for cpu %d\n",
  685. cpu);
  686. return;
  687. }
  688. stat->stat = function_stats;
  689. snprintf(name, 32, "function%d", cpu);
  690. stat->stat.name = name;
  691. ret = register_stat_tracer(&stat->stat);
  692. if (ret) {
  693. WARN(1,
  694. "Could not register function stat for cpu %d\n",
  695. cpu);
  696. kfree(name);
  697. return;
  698. }
  699. }
  700. entry = debugfs_create_file("function_profile_enabled", 0644,
  701. d_tracer, NULL, &ftrace_profile_fops);
  702. if (!entry)
  703. pr_warning("Could not create debugfs "
  704. "'function_profile_enabled' entry\n");
  705. }
  706. #else /* CONFIG_FUNCTION_PROFILER */
  707. static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
  708. {
  709. }
  710. #endif /* CONFIG_FUNCTION_PROFILER */
  711. static struct pid * const ftrace_swapper_pid = &init_struct_pid;
  712. #ifdef CONFIG_DYNAMIC_FTRACE
  713. #ifndef CONFIG_FTRACE_MCOUNT_RECORD
  714. # error Dynamic ftrace depends on MCOUNT_RECORD
  715. #endif
  716. static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
  717. struct ftrace_func_probe {
  718. struct hlist_node node;
  719. struct ftrace_probe_ops *ops;
  720. unsigned long flags;
  721. unsigned long ip;
  722. void *data;
  723. struct rcu_head rcu;
  724. };
  725. enum {
  726. FTRACE_ENABLE_CALLS = (1 << 0),
  727. FTRACE_DISABLE_CALLS = (1 << 1),
  728. FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
  729. FTRACE_ENABLE_MCOUNT = (1 << 3),
  730. FTRACE_DISABLE_MCOUNT = (1 << 4),
  731. FTRACE_START_FUNC_RET = (1 << 5),
  732. FTRACE_STOP_FUNC_RET = (1 << 6),
  733. };
  734. static int ftrace_filtered;
  735. static struct dyn_ftrace *ftrace_new_addrs;
  736. static DEFINE_MUTEX(ftrace_regex_lock);
  737. struct ftrace_page {
  738. struct ftrace_page *next;
  739. int index;
  740. struct dyn_ftrace records[];
  741. };
  742. #define ENTRIES_PER_PAGE \
  743. ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
  744. /* estimate from running different kernels */
  745. #define NR_TO_INIT 10000
  746. static struct ftrace_page *ftrace_pages_start;
  747. static struct ftrace_page *ftrace_pages;
  748. static struct dyn_ftrace *ftrace_free_records;
  749. /*
  750. * This is a double for. Do not use 'break' to break out of the loop,
  751. * you must use a goto.
  752. */
  753. #define do_for_each_ftrace_rec(pg, rec) \
  754. for (pg = ftrace_pages_start; pg; pg = pg->next) { \
  755. int _____i; \
  756. for (_____i = 0; _____i < pg->index; _____i++) { \
  757. rec = &pg->records[_____i];
  758. #define while_for_each_ftrace_rec() \
  759. } \
  760. }
  761. static void ftrace_free_rec(struct dyn_ftrace *rec)
  762. {
  763. rec->freelist = ftrace_free_records;
  764. ftrace_free_records = rec;
  765. rec->flags |= FTRACE_FL_FREE;
  766. }
  767. static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
  768. {
  769. struct dyn_ftrace *rec;
  770. /* First check for freed records */
  771. if (ftrace_free_records) {
  772. rec = ftrace_free_records;
  773. if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
  774. FTRACE_WARN_ON_ONCE(1);
  775. ftrace_free_records = NULL;
  776. return NULL;
  777. }
  778. ftrace_free_records = rec->freelist;
  779. memset(rec, 0, sizeof(*rec));
  780. return rec;
  781. }
  782. if (ftrace_pages->index == ENTRIES_PER_PAGE) {
  783. if (!ftrace_pages->next) {
  784. /* allocate another page */
  785. ftrace_pages->next =
  786. (void *)get_zeroed_page(GFP_KERNEL);
  787. if (!ftrace_pages->next)
  788. return NULL;
  789. }
  790. ftrace_pages = ftrace_pages->next;
  791. }
  792. return &ftrace_pages->records[ftrace_pages->index++];
  793. }
  794. static struct dyn_ftrace *
  795. ftrace_record_ip(unsigned long ip)
  796. {
  797. struct dyn_ftrace *rec;
  798. if (ftrace_disabled)
  799. return NULL;
  800. rec = ftrace_alloc_dyn_node(ip);
  801. if (!rec)
  802. return NULL;
  803. rec->ip = ip;
  804. rec->newlist = ftrace_new_addrs;
  805. ftrace_new_addrs = rec;
  806. return rec;
  807. }
  808. static void print_ip_ins(const char *fmt, unsigned char *p)
  809. {
  810. int i;
  811. printk(KERN_CONT "%s", fmt);
  812. for (i = 0; i < MCOUNT_INSN_SIZE; i++)
  813. printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
  814. }
  815. static void ftrace_bug(int failed, unsigned long ip)
  816. {
  817. switch (failed) {
  818. case -EFAULT:
  819. FTRACE_WARN_ON_ONCE(1);
  820. pr_info("ftrace faulted on modifying ");
  821. print_ip_sym(ip);
  822. break;
  823. case -EINVAL:
  824. FTRACE_WARN_ON_ONCE(1);
  825. pr_info("ftrace failed to modify ");
  826. print_ip_sym(ip);
  827. print_ip_ins(" actual: ", (unsigned char *)ip);
  828. printk(KERN_CONT "\n");
  829. break;
  830. case -EPERM:
  831. FTRACE_WARN_ON_ONCE(1);
  832. pr_info("ftrace faulted on writing ");
  833. print_ip_sym(ip);
  834. break;
  835. default:
  836. FTRACE_WARN_ON_ONCE(1);
  837. pr_info("ftrace faulted on unknown error ");
  838. print_ip_sym(ip);
  839. }
  840. }
  841. /* Return 1 if the address range is reserved for ftrace */
  842. int ftrace_text_reserved(void *start, void *end)
  843. {
  844. struct dyn_ftrace *rec;
  845. struct ftrace_page *pg;
  846. do_for_each_ftrace_rec(pg, rec) {
  847. if (rec->ip <= (unsigned long)end &&
  848. rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
  849. return 1;
  850. } while_for_each_ftrace_rec();
  851. return 0;
  852. }
  853. static int
  854. __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
  855. {
  856. unsigned long ftrace_addr;
  857. unsigned long flag = 0UL;
  858. ftrace_addr = (unsigned long)FTRACE_ADDR;
  859. /*
  860. * If this record is not to be traced or we want to disable it,
  861. * then disable it.
  862. *
  863. * If we want to enable it and filtering is off, then enable it.
  864. *
  865. * If we want to enable it and filtering is on, enable it only if
  866. * it's filtered
  867. */
  868. if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
  869. if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
  870. flag = FTRACE_FL_ENABLED;
  871. }
  872. /* If the state of this record hasn't changed, then do nothing */
  873. if ((rec->flags & FTRACE_FL_ENABLED) == flag)
  874. return 0;
  875. if (flag) {
  876. rec->flags |= FTRACE_FL_ENABLED;
  877. return ftrace_make_call(rec, ftrace_addr);
  878. }
  879. rec->flags &= ~FTRACE_FL_ENABLED;
  880. return ftrace_make_nop(NULL, rec, ftrace_addr);
  881. }
  882. static void ftrace_replace_code(int enable)
  883. {
  884. struct dyn_ftrace *rec;
  885. struct ftrace_page *pg;
  886. int failed;
  887. do_for_each_ftrace_rec(pg, rec) {
  888. /*
  889. * Skip over free records, records that have
  890. * failed and not converted.
  891. */
  892. if (rec->flags & FTRACE_FL_FREE ||
  893. rec->flags & FTRACE_FL_FAILED ||
  894. !(rec->flags & FTRACE_FL_CONVERTED))
  895. continue;
  896. failed = __ftrace_replace_code(rec, enable);
  897. if (failed) {
  898. rec->flags |= FTRACE_FL_FAILED;
  899. ftrace_bug(failed, rec->ip);
  900. /* Stop processing */
  901. return;
  902. }
  903. } while_for_each_ftrace_rec();
  904. }
  905. static int
  906. ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
  907. {
  908. unsigned long ip;
  909. int ret;
  910. ip = rec->ip;
  911. ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
  912. if (ret) {
  913. ftrace_bug(ret, ip);
  914. rec->flags |= FTRACE_FL_FAILED;
  915. return 0;
  916. }
  917. return 1;
  918. }
  919. /*
  920. * archs can override this function if they must do something
  921. * before the modifying code is performed.
  922. */
  923. int __weak ftrace_arch_code_modify_prepare(void)
  924. {
  925. return 0;
  926. }
  927. /*
  928. * archs can override this function if they must do something
  929. * after the modifying code is performed.
  930. */
  931. int __weak ftrace_arch_code_modify_post_process(void)
  932. {
  933. return 0;
  934. }
  935. static int __ftrace_modify_code(void *data)
  936. {
  937. int *command = data;
  938. if (*command & FTRACE_ENABLE_CALLS)
  939. ftrace_replace_code(1);
  940. else if (*command & FTRACE_DISABLE_CALLS)
  941. ftrace_replace_code(0);
  942. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  943. ftrace_update_ftrace_func(ftrace_trace_function);
  944. if (*command & FTRACE_START_FUNC_RET)
  945. ftrace_enable_ftrace_graph_caller();
  946. else if (*command & FTRACE_STOP_FUNC_RET)
  947. ftrace_disable_ftrace_graph_caller();
  948. return 0;
  949. }
  950. static void ftrace_run_update_code(int command)
  951. {
  952. int ret;
  953. ret = ftrace_arch_code_modify_prepare();
  954. FTRACE_WARN_ON(ret);
  955. if (ret)
  956. return;
  957. stop_machine(__ftrace_modify_code, &command, NULL);
  958. ret = ftrace_arch_code_modify_post_process();
  959. FTRACE_WARN_ON(ret);
  960. }
  961. static ftrace_func_t saved_ftrace_func;
  962. static int ftrace_start_up;
  963. static void ftrace_startup_enable(int command)
  964. {
  965. if (saved_ftrace_func != ftrace_trace_function) {
  966. saved_ftrace_func = ftrace_trace_function;
  967. command |= FTRACE_UPDATE_TRACE_FUNC;
  968. }
  969. if (!command || !ftrace_enabled)
  970. return;
  971. ftrace_run_update_code(command);
  972. }
  973. static void ftrace_startup(int command)
  974. {
  975. if (unlikely(ftrace_disabled))
  976. return;
  977. ftrace_start_up++;
  978. command |= FTRACE_ENABLE_CALLS;
  979. ftrace_startup_enable(command);
  980. }
  981. static void ftrace_shutdown(int command)
  982. {
  983. if (unlikely(ftrace_disabled))
  984. return;
  985. ftrace_start_up--;
  986. /*
  987. * Just warn in case of unbalance, no need to kill ftrace, it's not
  988. * critical but the ftrace_call callers may be never nopped again after
  989. * further ftrace uses.
  990. */
  991. WARN_ON_ONCE(ftrace_start_up < 0);
  992. if (!ftrace_start_up)
  993. command |= FTRACE_DISABLE_CALLS;
  994. if (saved_ftrace_func != ftrace_trace_function) {
  995. saved_ftrace_func = ftrace_trace_function;
  996. command |= FTRACE_UPDATE_TRACE_FUNC;
  997. }
  998. if (!command || !ftrace_enabled)
  999. return;
  1000. ftrace_run_update_code(command);
  1001. }
  1002. static void ftrace_startup_sysctl(void)
  1003. {
  1004. int command = FTRACE_ENABLE_MCOUNT;
  1005. if (unlikely(ftrace_disabled))
  1006. return;
  1007. /* Force update next time */
  1008. saved_ftrace_func = NULL;
  1009. /* ftrace_start_up is true if we want ftrace running */
  1010. if (ftrace_start_up)
  1011. command |= FTRACE_ENABLE_CALLS;
  1012. ftrace_run_update_code(command);
  1013. }
  1014. static void ftrace_shutdown_sysctl(void)
  1015. {
  1016. int command = FTRACE_DISABLE_MCOUNT;
  1017. if (unlikely(ftrace_disabled))
  1018. return;
  1019. /* ftrace_start_up is true if ftrace is running */
  1020. if (ftrace_start_up)
  1021. command |= FTRACE_DISABLE_CALLS;
  1022. ftrace_run_update_code(command);
  1023. }
  1024. static cycle_t ftrace_update_time;
  1025. static unsigned long ftrace_update_cnt;
  1026. unsigned long ftrace_update_tot_cnt;
  1027. static int ftrace_update_code(struct module *mod)
  1028. {
  1029. struct dyn_ftrace *p;
  1030. cycle_t start, stop;
  1031. start = ftrace_now(raw_smp_processor_id());
  1032. ftrace_update_cnt = 0;
  1033. while (ftrace_new_addrs) {
  1034. /* If something went wrong, bail without enabling anything */
  1035. if (unlikely(ftrace_disabled))
  1036. return -1;
  1037. p = ftrace_new_addrs;
  1038. ftrace_new_addrs = p->newlist;
  1039. p->flags = 0L;
  1040. /*
  1041. * Do the initial record convertion from mcount jump
  1042. * to the NOP instructions.
  1043. */
  1044. if (!ftrace_code_disable(mod, p)) {
  1045. ftrace_free_rec(p);
  1046. continue;
  1047. }
  1048. p->flags |= FTRACE_FL_CONVERTED;
  1049. ftrace_update_cnt++;
  1050. /*
  1051. * If the tracing is enabled, go ahead and enable the record.
  1052. *
  1053. * The reason not to enable the record immediatelly is the
  1054. * inherent check of ftrace_make_nop/ftrace_make_call for
  1055. * correct previous instructions. Making first the NOP
  1056. * conversion puts the module to the correct state, thus
  1057. * passing the ftrace_make_call check.
  1058. */
  1059. if (ftrace_start_up) {
  1060. int failed = __ftrace_replace_code(p, 1);
  1061. if (failed) {
  1062. ftrace_bug(failed, p->ip);
  1063. ftrace_free_rec(p);
  1064. }
  1065. }
  1066. }
  1067. stop = ftrace_now(raw_smp_processor_id());
  1068. ftrace_update_time = stop - start;
  1069. ftrace_update_tot_cnt += ftrace_update_cnt;
  1070. return 0;
  1071. }
  1072. static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
  1073. {
  1074. struct ftrace_page *pg;
  1075. int cnt;
  1076. int i;
  1077. /* allocate a few pages */
  1078. ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
  1079. if (!ftrace_pages_start)
  1080. return -1;
  1081. /*
  1082. * Allocate a few more pages.
  1083. *
  1084. * TODO: have some parser search vmlinux before
  1085. * final linking to find all calls to ftrace.
  1086. * Then we can:
  1087. * a) know how many pages to allocate.
  1088. * and/or
  1089. * b) set up the table then.
  1090. *
  1091. * The dynamic code is still necessary for
  1092. * modules.
  1093. */
  1094. pg = ftrace_pages = ftrace_pages_start;
  1095. cnt = num_to_init / ENTRIES_PER_PAGE;
  1096. pr_info("ftrace: allocating %ld entries in %d pages\n",
  1097. num_to_init, cnt + 1);
  1098. for (i = 0; i < cnt; i++) {
  1099. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  1100. /* If we fail, we'll try later anyway */
  1101. if (!pg->next)
  1102. break;
  1103. pg = pg->next;
  1104. }
  1105. return 0;
  1106. }
  1107. enum {
  1108. FTRACE_ITER_FILTER = (1 << 0),
  1109. FTRACE_ITER_NOTRACE = (1 << 1),
  1110. FTRACE_ITER_FAILURES = (1 << 2),
  1111. FTRACE_ITER_PRINTALL = (1 << 3),
  1112. FTRACE_ITER_HASH = (1 << 4),
  1113. };
  1114. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  1115. struct ftrace_iterator {
  1116. loff_t func_pos;
  1117. struct ftrace_page *pg;
  1118. struct dyn_ftrace *func;
  1119. struct ftrace_func_probe *probe;
  1120. struct trace_parser parser;
  1121. int hidx;
  1122. int idx;
  1123. unsigned flags;
  1124. };
  1125. static void *
  1126. t_hash_next(struct seq_file *m, loff_t *pos)
  1127. {
  1128. struct ftrace_iterator *iter = m->private;
  1129. struct hlist_node *hnd = NULL;
  1130. struct hlist_head *hhd;
  1131. WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
  1132. (*pos)++;
  1133. if (iter->probe)
  1134. hnd = &iter->probe->node;
  1135. retry:
  1136. if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
  1137. return NULL;
  1138. hhd = &ftrace_func_hash[iter->hidx];
  1139. if (hlist_empty(hhd)) {
  1140. iter->hidx++;
  1141. hnd = NULL;
  1142. goto retry;
  1143. }
  1144. if (!hnd)
  1145. hnd = hhd->first;
  1146. else {
  1147. hnd = hnd->next;
  1148. if (!hnd) {
  1149. iter->hidx++;
  1150. goto retry;
  1151. }
  1152. }
  1153. if (WARN_ON_ONCE(!hnd))
  1154. return NULL;
  1155. iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
  1156. return iter;
  1157. }
  1158. static void *t_hash_start(struct seq_file *m, loff_t *pos)
  1159. {
  1160. struct ftrace_iterator *iter = m->private;
  1161. void *p = NULL;
  1162. loff_t l;
  1163. if (!(iter->flags & FTRACE_ITER_HASH))
  1164. iter->func_pos = *pos;
  1165. if (iter->func_pos > *pos)
  1166. return NULL;
  1167. iter->flags |= FTRACE_ITER_HASH;
  1168. iter->hidx = 0;
  1169. for (l = 0; l <= (*pos - iter->func_pos); ) {
  1170. p = t_hash_next(m, &l);
  1171. if (!p)
  1172. break;
  1173. }
  1174. if (!p)
  1175. return NULL;
  1176. return iter;
  1177. }
  1178. static int
  1179. t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
  1180. {
  1181. struct ftrace_func_probe *rec;
  1182. rec = iter->probe;
  1183. if (WARN_ON_ONCE(!rec))
  1184. return -EIO;
  1185. if (rec->ops->print)
  1186. return rec->ops->print(m, rec->ip, rec->ops, rec->data);
  1187. seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
  1188. if (rec->data)
  1189. seq_printf(m, ":%p", rec->data);
  1190. seq_putc(m, '\n');
  1191. return 0;
  1192. }
  1193. static void *
  1194. t_next(struct seq_file *m, void *v, loff_t *pos)
  1195. {
  1196. struct ftrace_iterator *iter = m->private;
  1197. struct dyn_ftrace *rec = NULL;
  1198. if (iter->flags & FTRACE_ITER_HASH)
  1199. return t_hash_next(m, pos);
  1200. (*pos)++;
  1201. if (iter->flags & FTRACE_ITER_PRINTALL)
  1202. return NULL;
  1203. retry:
  1204. if (iter->idx >= iter->pg->index) {
  1205. if (iter->pg->next) {
  1206. iter->pg = iter->pg->next;
  1207. iter->idx = 0;
  1208. goto retry;
  1209. }
  1210. } else {
  1211. rec = &iter->pg->records[iter->idx++];
  1212. if ((rec->flags & FTRACE_FL_FREE) ||
  1213. (!(iter->flags & FTRACE_ITER_FAILURES) &&
  1214. (rec->flags & FTRACE_FL_FAILED)) ||
  1215. ((iter->flags & FTRACE_ITER_FAILURES) &&
  1216. !(rec->flags & FTRACE_FL_FAILED)) ||
  1217. ((iter->flags & FTRACE_ITER_FILTER) &&
  1218. !(rec->flags & FTRACE_FL_FILTER)) ||
  1219. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  1220. !(rec->flags & FTRACE_FL_NOTRACE))) {
  1221. rec = NULL;
  1222. goto retry;
  1223. }
  1224. }
  1225. if (!rec)
  1226. return NULL;
  1227. iter->func = rec;
  1228. return iter;
  1229. }
  1230. static void *t_start(struct seq_file *m, loff_t *pos)
  1231. {
  1232. struct ftrace_iterator *iter = m->private;
  1233. void *p = NULL;
  1234. loff_t l;
  1235. mutex_lock(&ftrace_lock);
  1236. /*
  1237. * For set_ftrace_filter reading, if we have the filter
  1238. * off, we can short cut and just print out that all
  1239. * functions are enabled.
  1240. */
  1241. if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
  1242. if (*pos > 0)
  1243. return t_hash_start(m, pos);
  1244. iter->flags |= FTRACE_ITER_PRINTALL;
  1245. /* reset in case of seek/pread */
  1246. iter->flags &= ~FTRACE_ITER_HASH;
  1247. return iter;
  1248. }
  1249. if (iter->flags & FTRACE_ITER_HASH)
  1250. return t_hash_start(m, pos);
  1251. iter->pg = ftrace_pages_start;
  1252. iter->idx = 0;
  1253. for (l = 0; l <= *pos; ) {
  1254. p = t_next(m, p, &l);
  1255. if (!p)
  1256. break;
  1257. }
  1258. if (!p) {
  1259. if (iter->flags & FTRACE_ITER_FILTER)
  1260. return t_hash_start(m, pos);
  1261. return NULL;
  1262. }
  1263. return iter;
  1264. }
  1265. static void t_stop(struct seq_file *m, void *p)
  1266. {
  1267. mutex_unlock(&ftrace_lock);
  1268. }
  1269. static int t_show(struct seq_file *m, void *v)
  1270. {
  1271. struct ftrace_iterator *iter = m->private;
  1272. struct dyn_ftrace *rec;
  1273. if (iter->flags & FTRACE_ITER_HASH)
  1274. return t_hash_show(m, iter);
  1275. if (iter->flags & FTRACE_ITER_PRINTALL) {
  1276. seq_printf(m, "#### all functions enabled ####\n");
  1277. return 0;
  1278. }
  1279. rec = iter->func;
  1280. if (!rec)
  1281. return 0;
  1282. seq_printf(m, "%ps\n", (void *)rec->ip);
  1283. return 0;
  1284. }
  1285. static const struct seq_operations show_ftrace_seq_ops = {
  1286. .start = t_start,
  1287. .next = t_next,
  1288. .stop = t_stop,
  1289. .show = t_show,
  1290. };
  1291. static int
  1292. ftrace_avail_open(struct inode *inode, struct file *file)
  1293. {
  1294. struct ftrace_iterator *iter;
  1295. int ret;
  1296. if (unlikely(ftrace_disabled))
  1297. return -ENODEV;
  1298. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1299. if (!iter)
  1300. return -ENOMEM;
  1301. iter->pg = ftrace_pages_start;
  1302. ret = seq_open(file, &show_ftrace_seq_ops);
  1303. if (!ret) {
  1304. struct seq_file *m = file->private_data;
  1305. m->private = iter;
  1306. } else {
  1307. kfree(iter);
  1308. }
  1309. return ret;
  1310. }
  1311. static int
  1312. ftrace_failures_open(struct inode *inode, struct file *file)
  1313. {
  1314. int ret;
  1315. struct seq_file *m;
  1316. struct ftrace_iterator *iter;
  1317. ret = ftrace_avail_open(inode, file);
  1318. if (!ret) {
  1319. m = (struct seq_file *)file->private_data;
  1320. iter = (struct ftrace_iterator *)m->private;
  1321. iter->flags = FTRACE_ITER_FAILURES;
  1322. }
  1323. return ret;
  1324. }
  1325. static void ftrace_filter_reset(int enable)
  1326. {
  1327. struct ftrace_page *pg;
  1328. struct dyn_ftrace *rec;
  1329. unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1330. mutex_lock(&ftrace_lock);
  1331. if (enable)
  1332. ftrace_filtered = 0;
  1333. do_for_each_ftrace_rec(pg, rec) {
  1334. if (rec->flags & FTRACE_FL_FAILED)
  1335. continue;
  1336. rec->flags &= ~type;
  1337. } while_for_each_ftrace_rec();
  1338. mutex_unlock(&ftrace_lock);
  1339. }
  1340. static int
  1341. ftrace_regex_open(struct inode *inode, struct file *file, int enable)
  1342. {
  1343. struct ftrace_iterator *iter;
  1344. int ret = 0;
  1345. if (unlikely(ftrace_disabled))
  1346. return -ENODEV;
  1347. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1348. if (!iter)
  1349. return -ENOMEM;
  1350. if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
  1351. kfree(iter);
  1352. return -ENOMEM;
  1353. }
  1354. mutex_lock(&ftrace_regex_lock);
  1355. if ((file->f_mode & FMODE_WRITE) &&
  1356. (file->f_flags & O_TRUNC))
  1357. ftrace_filter_reset(enable);
  1358. if (file->f_mode & FMODE_READ) {
  1359. iter->pg = ftrace_pages_start;
  1360. iter->flags = enable ? FTRACE_ITER_FILTER :
  1361. FTRACE_ITER_NOTRACE;
  1362. ret = seq_open(file, &show_ftrace_seq_ops);
  1363. if (!ret) {
  1364. struct seq_file *m = file->private_data;
  1365. m->private = iter;
  1366. } else {
  1367. trace_parser_put(&iter->parser);
  1368. kfree(iter);
  1369. }
  1370. } else
  1371. file->private_data = iter;
  1372. mutex_unlock(&ftrace_regex_lock);
  1373. return ret;
  1374. }
  1375. static int
  1376. ftrace_filter_open(struct inode *inode, struct file *file)
  1377. {
  1378. return ftrace_regex_open(inode, file, 1);
  1379. }
  1380. static int
  1381. ftrace_notrace_open(struct inode *inode, struct file *file)
  1382. {
  1383. return ftrace_regex_open(inode, file, 0);
  1384. }
  1385. static loff_t
  1386. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  1387. {
  1388. loff_t ret;
  1389. if (file->f_mode & FMODE_READ)
  1390. ret = seq_lseek(file, offset, origin);
  1391. else
  1392. file->f_pos = ret = 1;
  1393. return ret;
  1394. }
  1395. static int ftrace_match(char *str, char *regex, int len, int type)
  1396. {
  1397. int matched = 0;
  1398. int slen;
  1399. switch (type) {
  1400. case MATCH_FULL:
  1401. if (strcmp(str, regex) == 0)
  1402. matched = 1;
  1403. break;
  1404. case MATCH_FRONT_ONLY:
  1405. if (strncmp(str, regex, len) == 0)
  1406. matched = 1;
  1407. break;
  1408. case MATCH_MIDDLE_ONLY:
  1409. if (strstr(str, regex))
  1410. matched = 1;
  1411. break;
  1412. case MATCH_END_ONLY:
  1413. slen = strlen(str);
  1414. if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
  1415. matched = 1;
  1416. break;
  1417. }
  1418. return matched;
  1419. }
  1420. static int
  1421. ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
  1422. {
  1423. char str[KSYM_SYMBOL_LEN];
  1424. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  1425. return ftrace_match(str, regex, len, type);
  1426. }
  1427. static int ftrace_match_records(char *buff, int len, int enable)
  1428. {
  1429. unsigned int search_len;
  1430. struct ftrace_page *pg;
  1431. struct dyn_ftrace *rec;
  1432. unsigned long flag;
  1433. char *search;
  1434. int type;
  1435. int not;
  1436. int found = 0;
  1437. flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1438. type = filter_parse_regex(buff, len, &search, &not);
  1439. search_len = strlen(search);
  1440. mutex_lock(&ftrace_lock);
  1441. do_for_each_ftrace_rec(pg, rec) {
  1442. if (rec->flags & FTRACE_FL_FAILED)
  1443. continue;
  1444. if (ftrace_match_record(rec, search, search_len, type)) {
  1445. if (not)
  1446. rec->flags &= ~flag;
  1447. else
  1448. rec->flags |= flag;
  1449. found = 1;
  1450. }
  1451. /*
  1452. * Only enable filtering if we have a function that
  1453. * is filtered on.
  1454. */
  1455. if (enable && (rec->flags & FTRACE_FL_FILTER))
  1456. ftrace_filtered = 1;
  1457. } while_for_each_ftrace_rec();
  1458. mutex_unlock(&ftrace_lock);
  1459. return found;
  1460. }
  1461. static int
  1462. ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
  1463. char *regex, int len, int type)
  1464. {
  1465. char str[KSYM_SYMBOL_LEN];
  1466. char *modname;
  1467. kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
  1468. if (!modname || strcmp(modname, mod))
  1469. return 0;
  1470. /* blank search means to match all funcs in the mod */
  1471. if (len)
  1472. return ftrace_match(str, regex, len, type);
  1473. else
  1474. return 1;
  1475. }
  1476. static int ftrace_match_module_records(char *buff, char *mod, int enable)
  1477. {
  1478. unsigned search_len = 0;
  1479. struct ftrace_page *pg;
  1480. struct dyn_ftrace *rec;
  1481. int type = MATCH_FULL;
  1482. char *search = buff;
  1483. unsigned long flag;
  1484. int not = 0;
  1485. int found = 0;
  1486. flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1487. /* blank or '*' mean the same */
  1488. if (strcmp(buff, "*") == 0)
  1489. buff[0] = 0;
  1490. /* handle the case of 'dont filter this module' */
  1491. if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
  1492. buff[0] = 0;
  1493. not = 1;
  1494. }
  1495. if (strlen(buff)) {
  1496. type = filter_parse_regex(buff, strlen(buff), &search, &not);
  1497. search_len = strlen(search);
  1498. }
  1499. mutex_lock(&ftrace_lock);
  1500. do_for_each_ftrace_rec(pg, rec) {
  1501. if (rec->flags & FTRACE_FL_FAILED)
  1502. continue;
  1503. if (ftrace_match_module_record(rec, mod,
  1504. search, search_len, type)) {
  1505. if (not)
  1506. rec->flags &= ~flag;
  1507. else
  1508. rec->flags |= flag;
  1509. found = 1;
  1510. }
  1511. if (enable && (rec->flags & FTRACE_FL_FILTER))
  1512. ftrace_filtered = 1;
  1513. } while_for_each_ftrace_rec();
  1514. mutex_unlock(&ftrace_lock);
  1515. return found;
  1516. }
  1517. /*
  1518. * We register the module command as a template to show others how
  1519. * to register the a command as well.
  1520. */
  1521. static int
  1522. ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
  1523. {
  1524. char *mod;
  1525. /*
  1526. * cmd == 'mod' because we only registered this func
  1527. * for the 'mod' ftrace_func_command.
  1528. * But if you register one func with multiple commands,
  1529. * you can tell which command was used by the cmd
  1530. * parameter.
  1531. */
  1532. /* we must have a module name */
  1533. if (!param)
  1534. return -EINVAL;
  1535. mod = strsep(&param, ":");
  1536. if (!strlen(mod))
  1537. return -EINVAL;
  1538. if (ftrace_match_module_records(func, mod, enable))
  1539. return 0;
  1540. return -EINVAL;
  1541. }
  1542. static struct ftrace_func_command ftrace_mod_cmd = {
  1543. .name = "mod",
  1544. .func = ftrace_mod_callback,
  1545. };
  1546. static int __init ftrace_mod_cmd_init(void)
  1547. {
  1548. return register_ftrace_command(&ftrace_mod_cmd);
  1549. }
  1550. device_initcall(ftrace_mod_cmd_init);
  1551. static void
  1552. function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
  1553. {
  1554. struct ftrace_func_probe *entry;
  1555. struct hlist_head *hhd;
  1556. struct hlist_node *n;
  1557. unsigned long key;
  1558. key = hash_long(ip, FTRACE_HASH_BITS);
  1559. hhd = &ftrace_func_hash[key];
  1560. if (hlist_empty(hhd))
  1561. return;
  1562. /*
  1563. * Disable preemption for these calls to prevent a RCU grace
  1564. * period. This syncs the hash iteration and freeing of items
  1565. * on the hash. rcu_read_lock is too dangerous here.
  1566. */
  1567. preempt_disable_notrace();
  1568. hlist_for_each_entry_rcu(entry, n, hhd, node) {
  1569. if (entry->ip == ip)
  1570. entry->ops->func(ip, parent_ip, &entry->data);
  1571. }
  1572. preempt_enable_notrace();
  1573. }
  1574. static struct ftrace_ops trace_probe_ops __read_mostly =
  1575. {
  1576. .func = function_trace_probe_call,
  1577. };
  1578. static int ftrace_probe_registered;
  1579. static void __enable_ftrace_function_probe(void)
  1580. {
  1581. int i;
  1582. if (ftrace_probe_registered)
  1583. return;
  1584. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1585. struct hlist_head *hhd = &ftrace_func_hash[i];
  1586. if (hhd->first)
  1587. break;
  1588. }
  1589. /* Nothing registered? */
  1590. if (i == FTRACE_FUNC_HASHSIZE)
  1591. return;
  1592. __register_ftrace_function(&trace_probe_ops);
  1593. ftrace_startup(0);
  1594. ftrace_probe_registered = 1;
  1595. }
  1596. static void __disable_ftrace_function_probe(void)
  1597. {
  1598. int i;
  1599. if (!ftrace_probe_registered)
  1600. return;
  1601. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1602. struct hlist_head *hhd = &ftrace_func_hash[i];
  1603. if (hhd->first)
  1604. return;
  1605. }
  1606. /* no more funcs left */
  1607. __unregister_ftrace_function(&trace_probe_ops);
  1608. ftrace_shutdown(0);
  1609. ftrace_probe_registered = 0;
  1610. }
  1611. static void ftrace_free_entry_rcu(struct rcu_head *rhp)
  1612. {
  1613. struct ftrace_func_probe *entry =
  1614. container_of(rhp, struct ftrace_func_probe, rcu);
  1615. if (entry->ops->free)
  1616. entry->ops->free(&entry->data);
  1617. kfree(entry);
  1618. }
  1619. int
  1620. register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1621. void *data)
  1622. {
  1623. struct ftrace_func_probe *entry;
  1624. struct ftrace_page *pg;
  1625. struct dyn_ftrace *rec;
  1626. int type, len, not;
  1627. unsigned long key;
  1628. int count = 0;
  1629. char *search;
  1630. type = filter_parse_regex(glob, strlen(glob), &search, &not);
  1631. len = strlen(search);
  1632. /* we do not support '!' for function probes */
  1633. if (WARN_ON(not))
  1634. return -EINVAL;
  1635. mutex_lock(&ftrace_lock);
  1636. do_for_each_ftrace_rec(pg, rec) {
  1637. if (rec->flags & FTRACE_FL_FAILED)
  1638. continue;
  1639. if (!ftrace_match_record(rec, search, len, type))
  1640. continue;
  1641. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  1642. if (!entry) {
  1643. /* If we did not process any, then return error */
  1644. if (!count)
  1645. count = -ENOMEM;
  1646. goto out_unlock;
  1647. }
  1648. count++;
  1649. entry->data = data;
  1650. /*
  1651. * The caller might want to do something special
  1652. * for each function we find. We call the callback
  1653. * to give the caller an opportunity to do so.
  1654. */
  1655. if (ops->callback) {
  1656. if (ops->callback(rec->ip, &entry->data) < 0) {
  1657. /* caller does not like this func */
  1658. kfree(entry);
  1659. continue;
  1660. }
  1661. }
  1662. entry->ops = ops;
  1663. entry->ip = rec->ip;
  1664. key = hash_long(entry->ip, FTRACE_HASH_BITS);
  1665. hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
  1666. } while_for_each_ftrace_rec();
  1667. __enable_ftrace_function_probe();
  1668. out_unlock:
  1669. mutex_unlock(&ftrace_lock);
  1670. return count;
  1671. }
  1672. enum {
  1673. PROBE_TEST_FUNC = 1,
  1674. PROBE_TEST_DATA = 2
  1675. };
  1676. static void
  1677. __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1678. void *data, int flags)
  1679. {
  1680. struct ftrace_func_probe *entry;
  1681. struct hlist_node *n, *tmp;
  1682. char str[KSYM_SYMBOL_LEN];
  1683. int type = MATCH_FULL;
  1684. int i, len = 0;
  1685. char *search;
  1686. if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
  1687. glob = NULL;
  1688. else if (glob) {
  1689. int not;
  1690. type = filter_parse_regex(glob, strlen(glob), &search, &not);
  1691. len = strlen(search);
  1692. /* we do not support '!' for function probes */
  1693. if (WARN_ON(not))
  1694. return;
  1695. }
  1696. mutex_lock(&ftrace_lock);
  1697. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1698. struct hlist_head *hhd = &ftrace_func_hash[i];
  1699. hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
  1700. /* break up if statements for readability */
  1701. if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
  1702. continue;
  1703. if ((flags & PROBE_TEST_DATA) && entry->data != data)
  1704. continue;
  1705. /* do this last, since it is the most expensive */
  1706. if (glob) {
  1707. kallsyms_lookup(entry->ip, NULL, NULL,
  1708. NULL, str);
  1709. if (!ftrace_match(str, glob, len, type))
  1710. continue;
  1711. }
  1712. hlist_del(&entry->node);
  1713. call_rcu(&entry->rcu, ftrace_free_entry_rcu);
  1714. }
  1715. }
  1716. __disable_ftrace_function_probe();
  1717. mutex_unlock(&ftrace_lock);
  1718. }
  1719. void
  1720. unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1721. void *data)
  1722. {
  1723. __unregister_ftrace_function_probe(glob, ops, data,
  1724. PROBE_TEST_FUNC | PROBE_TEST_DATA);
  1725. }
  1726. void
  1727. unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
  1728. {
  1729. __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
  1730. }
  1731. void unregister_ftrace_function_probe_all(char *glob)
  1732. {
  1733. __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
  1734. }
  1735. static LIST_HEAD(ftrace_commands);
  1736. static DEFINE_MUTEX(ftrace_cmd_mutex);
  1737. int register_ftrace_command(struct ftrace_func_command *cmd)
  1738. {
  1739. struct ftrace_func_command *p;
  1740. int ret = 0;
  1741. mutex_lock(&ftrace_cmd_mutex);
  1742. list_for_each_entry(p, &ftrace_commands, list) {
  1743. if (strcmp(cmd->name, p->name) == 0) {
  1744. ret = -EBUSY;
  1745. goto out_unlock;
  1746. }
  1747. }
  1748. list_add(&cmd->list, &ftrace_commands);
  1749. out_unlock:
  1750. mutex_unlock(&ftrace_cmd_mutex);
  1751. return ret;
  1752. }
  1753. int unregister_ftrace_command(struct ftrace_func_command *cmd)
  1754. {
  1755. struct ftrace_func_command *p, *n;
  1756. int ret = -ENODEV;
  1757. mutex_lock(&ftrace_cmd_mutex);
  1758. list_for_each_entry_safe(p, n, &ftrace_commands, list) {
  1759. if (strcmp(cmd->name, p->name) == 0) {
  1760. ret = 0;
  1761. list_del_init(&p->list);
  1762. goto out_unlock;
  1763. }
  1764. }
  1765. out_unlock:
  1766. mutex_unlock(&ftrace_cmd_mutex);
  1767. return ret;
  1768. }
  1769. static int ftrace_process_regex(char *buff, int len, int enable)
  1770. {
  1771. char *func, *command, *next = buff;
  1772. struct ftrace_func_command *p;
  1773. int ret = -EINVAL;
  1774. func = strsep(&next, ":");
  1775. if (!next) {
  1776. if (ftrace_match_records(func, len, enable))
  1777. return 0;
  1778. return ret;
  1779. }
  1780. /* command found */
  1781. command = strsep(&next, ":");
  1782. mutex_lock(&ftrace_cmd_mutex);
  1783. list_for_each_entry(p, &ftrace_commands, list) {
  1784. if (strcmp(p->name, command) == 0) {
  1785. ret = p->func(func, command, next, enable);
  1786. goto out_unlock;
  1787. }
  1788. }
  1789. out_unlock:
  1790. mutex_unlock(&ftrace_cmd_mutex);
  1791. return ret;
  1792. }
  1793. static ssize_t
  1794. ftrace_regex_write(struct file *file, const char __user *ubuf,
  1795. size_t cnt, loff_t *ppos, int enable)
  1796. {
  1797. struct ftrace_iterator *iter;
  1798. struct trace_parser *parser;
  1799. ssize_t ret, read;
  1800. if (!cnt)
  1801. return 0;
  1802. mutex_lock(&ftrace_regex_lock);
  1803. if (file->f_mode & FMODE_READ) {
  1804. struct seq_file *m = file->private_data;
  1805. iter = m->private;
  1806. } else
  1807. iter = file->private_data;
  1808. parser = &iter->parser;
  1809. read = trace_get_user(parser, ubuf, cnt, ppos);
  1810. if (read >= 0 && trace_parser_loaded(parser) &&
  1811. !trace_parser_cont(parser)) {
  1812. ret = ftrace_process_regex(parser->buffer,
  1813. parser->idx, enable);
  1814. trace_parser_clear(parser);
  1815. if (ret)
  1816. goto out_unlock;
  1817. }
  1818. ret = read;
  1819. out_unlock:
  1820. mutex_unlock(&ftrace_regex_lock);
  1821. return ret;
  1822. }
  1823. static ssize_t
  1824. ftrace_filter_write(struct file *file, const char __user *ubuf,
  1825. size_t cnt, loff_t *ppos)
  1826. {
  1827. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  1828. }
  1829. static ssize_t
  1830. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  1831. size_t cnt, loff_t *ppos)
  1832. {
  1833. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  1834. }
  1835. static void
  1836. ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
  1837. {
  1838. if (unlikely(ftrace_disabled))
  1839. return;
  1840. mutex_lock(&ftrace_regex_lock);
  1841. if (reset)
  1842. ftrace_filter_reset(enable);
  1843. if (buf)
  1844. ftrace_match_records(buf, len, enable);
  1845. mutex_unlock(&ftrace_regex_lock);
  1846. }
  1847. /**
  1848. * ftrace_set_filter - set a function to filter on in ftrace
  1849. * @buf - the string that holds the function filter text.
  1850. * @len - the length of the string.
  1851. * @reset - non zero to reset all filters before applying this filter.
  1852. *
  1853. * Filters denote which functions should be enabled when tracing is enabled.
  1854. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  1855. */
  1856. void ftrace_set_filter(unsigned char *buf, int len, int reset)
  1857. {
  1858. ftrace_set_regex(buf, len, reset, 1);
  1859. }
  1860. /**
  1861. * ftrace_set_notrace - set a function to not trace in ftrace
  1862. * @buf - the string that holds the function notrace text.
  1863. * @len - the length of the string.
  1864. * @reset - non zero to reset all filters before applying this filter.
  1865. *
  1866. * Notrace Filters denote which functions should not be enabled when tracing
  1867. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  1868. * for tracing.
  1869. */
  1870. void ftrace_set_notrace(unsigned char *buf, int len, int reset)
  1871. {
  1872. ftrace_set_regex(buf, len, reset, 0);
  1873. }
  1874. /*
  1875. * command line interface to allow users to set filters on boot up.
  1876. */
  1877. #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
  1878. static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
  1879. static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
  1880. static int __init set_ftrace_notrace(char *str)
  1881. {
  1882. strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
  1883. return 1;
  1884. }
  1885. __setup("ftrace_notrace=", set_ftrace_notrace);
  1886. static int __init set_ftrace_filter(char *str)
  1887. {
  1888. strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
  1889. return 1;
  1890. }
  1891. __setup("ftrace_filter=", set_ftrace_filter);
  1892. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1893. static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
  1894. static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
  1895. static int __init set_graph_function(char *str)
  1896. {
  1897. strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
  1898. return 1;
  1899. }
  1900. __setup("ftrace_graph_filter=", set_graph_function);
  1901. static void __init set_ftrace_early_graph(char *buf)
  1902. {
  1903. int ret;
  1904. char *func;
  1905. while (buf) {
  1906. func = strsep(&buf, ",");
  1907. /* we allow only one expression at a time */
  1908. ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
  1909. func);
  1910. if (ret)
  1911. printk(KERN_DEBUG "ftrace: function %s not "
  1912. "traceable\n", func);
  1913. }
  1914. }
  1915. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1916. static void __init set_ftrace_early_filter(char *buf, int enable)
  1917. {
  1918. char *func;
  1919. while (buf) {
  1920. func = strsep(&buf, ",");
  1921. ftrace_set_regex(func, strlen(func), 0, enable);
  1922. }
  1923. }
  1924. static void __init set_ftrace_early_filters(void)
  1925. {
  1926. if (ftrace_filter_buf[0])
  1927. set_ftrace_early_filter(ftrace_filter_buf, 1);
  1928. if (ftrace_notrace_buf[0])
  1929. set_ftrace_early_filter(ftrace_notrace_buf, 0);
  1930. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1931. if (ftrace_graph_buf[0])
  1932. set_ftrace_early_graph(ftrace_graph_buf);
  1933. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1934. }
  1935. static int
  1936. ftrace_regex_release(struct inode *inode, struct file *file, int enable)
  1937. {
  1938. struct seq_file *m = (struct seq_file *)file->private_data;
  1939. struct ftrace_iterator *iter;
  1940. struct trace_parser *parser;
  1941. mutex_lock(&ftrace_regex_lock);
  1942. if (file->f_mode & FMODE_READ) {
  1943. iter = m->private;
  1944. seq_release(inode, file);
  1945. } else
  1946. iter = file->private_data;
  1947. parser = &iter->parser;
  1948. if (trace_parser_loaded(parser)) {
  1949. parser->buffer[parser->idx] = 0;
  1950. ftrace_match_records(parser->buffer, parser->idx, enable);
  1951. }
  1952. mutex_lock(&ftrace_lock);
  1953. if (ftrace_start_up && ftrace_enabled)
  1954. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  1955. mutex_unlock(&ftrace_lock);
  1956. trace_parser_put(parser);
  1957. kfree(iter);
  1958. mutex_unlock(&ftrace_regex_lock);
  1959. return 0;
  1960. }
  1961. static int
  1962. ftrace_filter_release(struct inode *inode, struct file *file)
  1963. {
  1964. return ftrace_regex_release(inode, file, 1);
  1965. }
  1966. static int
  1967. ftrace_notrace_release(struct inode *inode, struct file *file)
  1968. {
  1969. return ftrace_regex_release(inode, file, 0);
  1970. }
  1971. static const struct file_operations ftrace_avail_fops = {
  1972. .open = ftrace_avail_open,
  1973. .read = seq_read,
  1974. .llseek = seq_lseek,
  1975. .release = seq_release_private,
  1976. };
  1977. static const struct file_operations ftrace_failures_fops = {
  1978. .open = ftrace_failures_open,
  1979. .read = seq_read,
  1980. .llseek = seq_lseek,
  1981. .release = seq_release_private,
  1982. };
  1983. static const struct file_operations ftrace_filter_fops = {
  1984. .open = ftrace_filter_open,
  1985. .read = seq_read,
  1986. .write = ftrace_filter_write,
  1987. .llseek = no_llseek,
  1988. .release = ftrace_filter_release,
  1989. };
  1990. static const struct file_operations ftrace_notrace_fops = {
  1991. .open = ftrace_notrace_open,
  1992. .read = seq_read,
  1993. .write = ftrace_notrace_write,
  1994. .llseek = ftrace_regex_lseek,
  1995. .release = ftrace_notrace_release,
  1996. };
  1997. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1998. static DEFINE_MUTEX(graph_lock);
  1999. int ftrace_graph_count;
  2000. int ftrace_graph_filter_enabled;
  2001. unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
  2002. static void *
  2003. __g_next(struct seq_file *m, loff_t *pos)
  2004. {
  2005. if (*pos >= ftrace_graph_count)
  2006. return NULL;
  2007. return &ftrace_graph_funcs[*pos];
  2008. }
  2009. static void *
  2010. g_next(struct seq_file *m, void *v, loff_t *pos)
  2011. {
  2012. (*pos)++;
  2013. return __g_next(m, pos);
  2014. }
  2015. static void *g_start(struct seq_file *m, loff_t *pos)
  2016. {
  2017. mutex_lock(&graph_lock);
  2018. /* Nothing, tell g_show to print all functions are enabled */
  2019. if (!ftrace_graph_filter_enabled && !*pos)
  2020. return (void *)1;
  2021. return __g_next(m, pos);
  2022. }
  2023. static void g_stop(struct seq_file *m, void *p)
  2024. {
  2025. mutex_unlock(&graph_lock);
  2026. }
  2027. static int g_show(struct seq_file *m, void *v)
  2028. {
  2029. unsigned long *ptr = v;
  2030. if (!ptr)
  2031. return 0;
  2032. if (ptr == (unsigned long *)1) {
  2033. seq_printf(m, "#### all functions enabled ####\n");
  2034. return 0;
  2035. }
  2036. seq_printf(m, "%ps\n", (void *)*ptr);
  2037. return 0;
  2038. }
  2039. static const struct seq_operations ftrace_graph_seq_ops = {
  2040. .start = g_start,
  2041. .next = g_next,
  2042. .stop = g_stop,
  2043. .show = g_show,
  2044. };
  2045. static int
  2046. ftrace_graph_open(struct inode *inode, struct file *file)
  2047. {
  2048. int ret = 0;
  2049. if (unlikely(ftrace_disabled))
  2050. return -ENODEV;
  2051. mutex_lock(&graph_lock);
  2052. if ((file->f_mode & FMODE_WRITE) &&
  2053. (file->f_flags & O_TRUNC)) {
  2054. ftrace_graph_filter_enabled = 0;
  2055. ftrace_graph_count = 0;
  2056. memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
  2057. }
  2058. mutex_unlock(&graph_lock);
  2059. if (file->f_mode & FMODE_READ)
  2060. ret = seq_open(file, &ftrace_graph_seq_ops);
  2061. return ret;
  2062. }
  2063. static int
  2064. ftrace_graph_release(struct inode *inode, struct file *file)
  2065. {
  2066. if (file->f_mode & FMODE_READ)
  2067. seq_release(inode, file);
  2068. return 0;
  2069. }
  2070. static int
  2071. ftrace_set_func(unsigned long *array, int *idx, char *buffer)
  2072. {
  2073. struct dyn_ftrace *rec;
  2074. struct ftrace_page *pg;
  2075. int search_len;
  2076. int fail = 1;
  2077. int type, not;
  2078. char *search;
  2079. bool exists;
  2080. int i;
  2081. if (ftrace_disabled)
  2082. return -ENODEV;
  2083. /* decode regex */
  2084. type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
  2085. if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
  2086. return -EBUSY;
  2087. search_len = strlen(search);
  2088. mutex_lock(&ftrace_lock);
  2089. do_for_each_ftrace_rec(pg, rec) {
  2090. if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
  2091. continue;
  2092. if (ftrace_match_record(rec, search, search_len, type)) {
  2093. /* if it is in the array */
  2094. exists = false;
  2095. for (i = 0; i < *idx; i++) {
  2096. if (array[i] == rec->ip) {
  2097. exists = true;
  2098. break;
  2099. }
  2100. }
  2101. if (!not) {
  2102. fail = 0;
  2103. if (!exists) {
  2104. array[(*idx)++] = rec->ip;
  2105. if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
  2106. goto out;
  2107. }
  2108. } else {
  2109. if (exists) {
  2110. array[i] = array[--(*idx)];
  2111. array[*idx] = 0;
  2112. fail = 0;
  2113. }
  2114. }
  2115. }
  2116. } while_for_each_ftrace_rec();
  2117. out:
  2118. mutex_unlock(&ftrace_lock);
  2119. if (fail)
  2120. return -EINVAL;
  2121. ftrace_graph_filter_enabled = 1;
  2122. return 0;
  2123. }
  2124. static ssize_t
  2125. ftrace_graph_write(struct file *file, const char __user *ubuf,
  2126. size_t cnt, loff_t *ppos)
  2127. {
  2128. struct trace_parser parser;
  2129. ssize_t read, ret;
  2130. if (!cnt)
  2131. return 0;
  2132. mutex_lock(&graph_lock);
  2133. if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
  2134. ret = -ENOMEM;
  2135. goto out_unlock;
  2136. }
  2137. read = trace_get_user(&parser, ubuf, cnt, ppos);
  2138. if (read >= 0 && trace_parser_loaded((&parser))) {
  2139. parser.buffer[parser.idx] = 0;
  2140. /* we allow only one expression at a time */
  2141. ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
  2142. parser.buffer);
  2143. if (ret)
  2144. goto out_free;
  2145. }
  2146. ret = read;
  2147. out_free:
  2148. trace_parser_put(&parser);
  2149. out_unlock:
  2150. mutex_unlock(&graph_lock);
  2151. return ret;
  2152. }
  2153. static const struct file_operations ftrace_graph_fops = {
  2154. .open = ftrace_graph_open,
  2155. .read = seq_read,
  2156. .write = ftrace_graph_write,
  2157. .release = ftrace_graph_release,
  2158. };
  2159. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2160. static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
  2161. {
  2162. trace_create_file("available_filter_functions", 0444,
  2163. d_tracer, NULL, &ftrace_avail_fops);
  2164. trace_create_file("failures", 0444,
  2165. d_tracer, NULL, &ftrace_failures_fops);
  2166. trace_create_file("set_ftrace_filter", 0644, d_tracer,
  2167. NULL, &ftrace_filter_fops);
  2168. trace_create_file("set_ftrace_notrace", 0644, d_tracer,
  2169. NULL, &ftrace_notrace_fops);
  2170. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2171. trace_create_file("set_graph_function", 0444, d_tracer,
  2172. NULL,
  2173. &ftrace_graph_fops);
  2174. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2175. return 0;
  2176. }
  2177. static int ftrace_process_locs(struct module *mod,
  2178. unsigned long *start,
  2179. unsigned long *end)
  2180. {
  2181. unsigned long *p;
  2182. unsigned long addr;
  2183. unsigned long flags;
  2184. mutex_lock(&ftrace_lock);
  2185. p = start;
  2186. while (p < end) {
  2187. addr = ftrace_call_adjust(*p++);
  2188. /*
  2189. * Some architecture linkers will pad between
  2190. * the different mcount_loc sections of different
  2191. * object files to satisfy alignments.
  2192. * Skip any NULL pointers.
  2193. */
  2194. if (!addr)
  2195. continue;
  2196. ftrace_record_ip(addr);
  2197. }
  2198. /* disable interrupts to prevent kstop machine */
  2199. local_irq_save(flags);
  2200. ftrace_update_code(mod);
  2201. local_irq_restore(flags);
  2202. mutex_unlock(&ftrace_lock);
  2203. return 0;
  2204. }
  2205. #ifdef CONFIG_MODULES
  2206. void ftrace_release_mod(struct module *mod)
  2207. {
  2208. struct dyn_ftrace *rec;
  2209. struct ftrace_page *pg;
  2210. if (ftrace_disabled)
  2211. return;
  2212. mutex_lock(&ftrace_lock);
  2213. do_for_each_ftrace_rec(pg, rec) {
  2214. if (within_module_core(rec->ip, mod)) {
  2215. /*
  2216. * rec->ip is changed in ftrace_free_rec()
  2217. * It should not between s and e if record was freed.
  2218. */
  2219. FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
  2220. ftrace_free_rec(rec);
  2221. }
  2222. } while_for_each_ftrace_rec();
  2223. mutex_unlock(&ftrace_lock);
  2224. }
  2225. static void ftrace_init_module(struct module *mod,
  2226. unsigned long *start, unsigned long *end)
  2227. {
  2228. if (ftrace_disabled || start == end)
  2229. return;
  2230. ftrace_process_locs(mod, start, end);
  2231. }
  2232. static int ftrace_module_notify(struct notifier_block *self,
  2233. unsigned long val, void *data)
  2234. {
  2235. struct module *mod = data;
  2236. switch (val) {
  2237. case MODULE_STATE_COMING:
  2238. ftrace_init_module(mod, mod->ftrace_callsites,
  2239. mod->ftrace_callsites +
  2240. mod->num_ftrace_callsites);
  2241. break;
  2242. case MODULE_STATE_GOING:
  2243. ftrace_release_mod(mod);
  2244. break;
  2245. }
  2246. return 0;
  2247. }
  2248. #else
  2249. static int ftrace_module_notify(struct notifier_block *self,
  2250. unsigned long val, void *data)
  2251. {
  2252. return 0;
  2253. }
  2254. #endif /* CONFIG_MODULES */
  2255. struct notifier_block ftrace_module_nb = {
  2256. .notifier_call = ftrace_module_notify,
  2257. .priority = 0,
  2258. };
  2259. extern unsigned long __start_mcount_loc[];
  2260. extern unsigned long __stop_mcount_loc[];
  2261. void __init ftrace_init(void)
  2262. {
  2263. unsigned long count, addr, flags;
  2264. int ret;
  2265. /* Keep the ftrace pointer to the stub */
  2266. addr = (unsigned long)ftrace_stub;
  2267. local_irq_save(flags);
  2268. ftrace_dyn_arch_init(&addr);
  2269. local_irq_restore(flags);
  2270. /* ftrace_dyn_arch_init places the return code in addr */
  2271. if (addr)
  2272. goto failed;
  2273. count = __stop_mcount_loc - __start_mcount_loc;
  2274. ret = ftrace_dyn_table_alloc(count);
  2275. if (ret)
  2276. goto failed;
  2277. last_ftrace_enabled = ftrace_enabled = 1;
  2278. ret = ftrace_process_locs(NULL,
  2279. __start_mcount_loc,
  2280. __stop_mcount_loc);
  2281. ret = register_module_notifier(&ftrace_module_nb);
  2282. if (ret)
  2283. pr_warning("Failed to register trace ftrace module notifier\n");
  2284. set_ftrace_early_filters();
  2285. return;
  2286. failed:
  2287. ftrace_disabled = 1;
  2288. }
  2289. #else
  2290. static int __init ftrace_nodyn_init(void)
  2291. {
  2292. ftrace_enabled = 1;
  2293. return 0;
  2294. }
  2295. device_initcall(ftrace_nodyn_init);
  2296. static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
  2297. static inline void ftrace_startup_enable(int command) { }
  2298. /* Keep as macros so we do not need to define the commands */
  2299. # define ftrace_startup(command) do { } while (0)
  2300. # define ftrace_shutdown(command) do { } while (0)
  2301. # define ftrace_startup_sysctl() do { } while (0)
  2302. # define ftrace_shutdown_sysctl() do { } while (0)
  2303. #endif /* CONFIG_DYNAMIC_FTRACE */
  2304. static void clear_ftrace_swapper(void)
  2305. {
  2306. struct task_struct *p;
  2307. int cpu;
  2308. get_online_cpus();
  2309. for_each_online_cpu(cpu) {
  2310. p = idle_task(cpu);
  2311. clear_tsk_trace_trace(p);
  2312. }
  2313. put_online_cpus();
  2314. }
  2315. static void set_ftrace_swapper(void)
  2316. {
  2317. struct task_struct *p;
  2318. int cpu;
  2319. get_online_cpus();
  2320. for_each_online_cpu(cpu) {
  2321. p = idle_task(cpu);
  2322. set_tsk_trace_trace(p);
  2323. }
  2324. put_online_cpus();
  2325. }
  2326. static void clear_ftrace_pid(struct pid *pid)
  2327. {
  2328. struct task_struct *p;
  2329. rcu_read_lock();
  2330. do_each_pid_task(pid, PIDTYPE_PID, p) {
  2331. clear_tsk_trace_trace(p);
  2332. } while_each_pid_task(pid, PIDTYPE_PID, p);
  2333. rcu_read_unlock();
  2334. put_pid(pid);
  2335. }
  2336. static void set_ftrace_pid(struct pid *pid)
  2337. {
  2338. struct task_struct *p;
  2339. rcu_read_lock();
  2340. do_each_pid_task(pid, PIDTYPE_PID, p) {
  2341. set_tsk_trace_trace(p);
  2342. } while_each_pid_task(pid, PIDTYPE_PID, p);
  2343. rcu_read_unlock();
  2344. }
  2345. static void clear_ftrace_pid_task(struct pid *pid)
  2346. {
  2347. if (pid == ftrace_swapper_pid)
  2348. clear_ftrace_swapper();
  2349. else
  2350. clear_ftrace_pid(pid);
  2351. }
  2352. static void set_ftrace_pid_task(struct pid *pid)
  2353. {
  2354. if (pid == ftrace_swapper_pid)
  2355. set_ftrace_swapper();
  2356. else
  2357. set_ftrace_pid(pid);
  2358. }
  2359. static int ftrace_pid_add(int p)
  2360. {
  2361. struct pid *pid;
  2362. struct ftrace_pid *fpid;
  2363. int ret = -EINVAL;
  2364. mutex_lock(&ftrace_lock);
  2365. if (!p)
  2366. pid = ftrace_swapper_pid;
  2367. else
  2368. pid = find_get_pid(p);
  2369. if (!pid)
  2370. goto out;
  2371. ret = 0;
  2372. list_for_each_entry(fpid, &ftrace_pids, list)
  2373. if (fpid->pid == pid)
  2374. goto out_put;
  2375. ret = -ENOMEM;
  2376. fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
  2377. if (!fpid)
  2378. goto out_put;
  2379. list_add(&fpid->list, &ftrace_pids);
  2380. fpid->pid = pid;
  2381. set_ftrace_pid_task(pid);
  2382. ftrace_update_pid_func();
  2383. ftrace_startup_enable(0);
  2384. mutex_unlock(&ftrace_lock);
  2385. return 0;
  2386. out_put:
  2387. if (pid != ftrace_swapper_pid)
  2388. put_pid(pid);
  2389. out:
  2390. mutex_unlock(&ftrace_lock);
  2391. return ret;
  2392. }
  2393. static void ftrace_pid_reset(void)
  2394. {
  2395. struct ftrace_pid *fpid, *safe;
  2396. mutex_lock(&ftrace_lock);
  2397. list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
  2398. struct pid *pid = fpid->pid;
  2399. clear_ftrace_pid_task(pid);
  2400. list_del(&fpid->list);
  2401. kfree(fpid);
  2402. }
  2403. ftrace_update_pid_func();
  2404. ftrace_startup_enable(0);
  2405. mutex_unlock(&ftrace_lock);
  2406. }
  2407. static void *fpid_start(struct seq_file *m, loff_t *pos)
  2408. {
  2409. mutex_lock(&ftrace_lock);
  2410. if (list_empty(&ftrace_pids) && (!*pos))
  2411. return (void *) 1;
  2412. return seq_list_start(&ftrace_pids, *pos);
  2413. }
  2414. static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
  2415. {
  2416. if (v == (void *)1)
  2417. return NULL;
  2418. return seq_list_next(v, &ftrace_pids, pos);
  2419. }
  2420. static void fpid_stop(struct seq_file *m, void *p)
  2421. {
  2422. mutex_unlock(&ftrace_lock);
  2423. }
  2424. static int fpid_show(struct seq_file *m, void *v)
  2425. {
  2426. const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
  2427. if (v == (void *)1) {
  2428. seq_printf(m, "no pid\n");
  2429. return 0;
  2430. }
  2431. if (fpid->pid == ftrace_swapper_pid)
  2432. seq_printf(m, "swapper tasks\n");
  2433. else
  2434. seq_printf(m, "%u\n", pid_vnr(fpid->pid));
  2435. return 0;
  2436. }
  2437. static const struct seq_operations ftrace_pid_sops = {
  2438. .start = fpid_start,
  2439. .next = fpid_next,
  2440. .stop = fpid_stop,
  2441. .show = fpid_show,
  2442. };
  2443. static int
  2444. ftrace_pid_open(struct inode *inode, struct file *file)
  2445. {
  2446. int ret = 0;
  2447. if ((file->f_mode & FMODE_WRITE) &&
  2448. (file->f_flags & O_TRUNC))
  2449. ftrace_pid_reset();
  2450. if (file->f_mode & FMODE_READ)
  2451. ret = seq_open(file, &ftrace_pid_sops);
  2452. return ret;
  2453. }
  2454. static ssize_t
  2455. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  2456. size_t cnt, loff_t *ppos)
  2457. {
  2458. char buf[64], *tmp;
  2459. long val;
  2460. int ret;
  2461. if (cnt >= sizeof(buf))
  2462. return -EINVAL;
  2463. if (copy_from_user(&buf, ubuf, cnt))
  2464. return -EFAULT;
  2465. buf[cnt] = 0;
  2466. /*
  2467. * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
  2468. * to clean the filter quietly.
  2469. */
  2470. tmp = strstrip(buf);
  2471. if (strlen(tmp) == 0)
  2472. return 1;
  2473. ret = strict_strtol(tmp, 10, &val);
  2474. if (ret < 0)
  2475. return ret;
  2476. ret = ftrace_pid_add(val);
  2477. return ret ? ret : cnt;
  2478. }
  2479. static int
  2480. ftrace_pid_release(struct inode *inode, struct file *file)
  2481. {
  2482. if (file->f_mode & FMODE_READ)
  2483. seq_release(inode, file);
  2484. return 0;
  2485. }
  2486. static const struct file_operations ftrace_pid_fops = {
  2487. .open = ftrace_pid_open,
  2488. .write = ftrace_pid_write,
  2489. .read = seq_read,
  2490. .llseek = seq_lseek,
  2491. .release = ftrace_pid_release,
  2492. };
  2493. static __init int ftrace_init_debugfs(void)
  2494. {
  2495. struct dentry *d_tracer;
  2496. d_tracer = tracing_init_dentry();
  2497. if (!d_tracer)
  2498. return 0;
  2499. ftrace_init_dyn_debugfs(d_tracer);
  2500. trace_create_file("set_ftrace_pid", 0644, d_tracer,
  2501. NULL, &ftrace_pid_fops);
  2502. ftrace_profile_debugfs(d_tracer);
  2503. return 0;
  2504. }
  2505. fs_initcall(ftrace_init_debugfs);
  2506. /**
  2507. * ftrace_kill - kill ftrace
  2508. *
  2509. * This function should be used by panic code. It stops ftrace
  2510. * but in a not so nice way. If you need to simply kill ftrace
  2511. * from a non-atomic section, use ftrace_kill.
  2512. */
  2513. void ftrace_kill(void)
  2514. {
  2515. ftrace_disabled = 1;
  2516. ftrace_enabled = 0;
  2517. clear_ftrace_function();
  2518. }
  2519. /**
  2520. * register_ftrace_function - register a function for profiling
  2521. * @ops - ops structure that holds the function for profiling.
  2522. *
  2523. * Register a function to be called by all functions in the
  2524. * kernel.
  2525. *
  2526. * Note: @ops->func and all the functions it calls must be labeled
  2527. * with "notrace", otherwise it will go into a
  2528. * recursive loop.
  2529. */
  2530. int register_ftrace_function(struct ftrace_ops *ops)
  2531. {
  2532. int ret;
  2533. if (unlikely(ftrace_disabled))
  2534. return -1;
  2535. mutex_lock(&ftrace_lock);
  2536. ret = __register_ftrace_function(ops);
  2537. ftrace_startup(0);
  2538. mutex_unlock(&ftrace_lock);
  2539. return ret;
  2540. }
  2541. /**
  2542. * unregister_ftrace_function - unregister a function for profiling.
  2543. * @ops - ops structure that holds the function to unregister
  2544. *
  2545. * Unregister a function that was added to be called by ftrace profiling.
  2546. */
  2547. int unregister_ftrace_function(struct ftrace_ops *ops)
  2548. {
  2549. int ret;
  2550. mutex_lock(&ftrace_lock);
  2551. ret = __unregister_ftrace_function(ops);
  2552. ftrace_shutdown(0);
  2553. mutex_unlock(&ftrace_lock);
  2554. return ret;
  2555. }
  2556. int
  2557. ftrace_enable_sysctl(struct ctl_table *table, int write,
  2558. void __user *buffer, size_t *lenp,
  2559. loff_t *ppos)
  2560. {
  2561. int ret;
  2562. if (unlikely(ftrace_disabled))
  2563. return -ENODEV;
  2564. mutex_lock(&ftrace_lock);
  2565. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  2566. if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
  2567. goto out;
  2568. last_ftrace_enabled = !!ftrace_enabled;
  2569. if (ftrace_enabled) {
  2570. ftrace_startup_sysctl();
  2571. /* we are starting ftrace again */
  2572. if (ftrace_list != &ftrace_list_end) {
  2573. if (ftrace_list->next == &ftrace_list_end)
  2574. ftrace_trace_function = ftrace_list->func;
  2575. else
  2576. ftrace_trace_function = ftrace_list_func;
  2577. }
  2578. } else {
  2579. /* stopping ftrace calls (just send to ftrace_stub) */
  2580. ftrace_trace_function = ftrace_stub;
  2581. ftrace_shutdown_sysctl();
  2582. }
  2583. out:
  2584. mutex_unlock(&ftrace_lock);
  2585. return ret;
  2586. }
  2587. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2588. static int ftrace_graph_active;
  2589. static struct notifier_block ftrace_suspend_notifier;
  2590. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
  2591. {
  2592. return 0;
  2593. }
  2594. /* The callbacks that hook a function */
  2595. trace_func_graph_ret_t ftrace_graph_return =
  2596. (trace_func_graph_ret_t)ftrace_stub;
  2597. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  2598. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  2599. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  2600. {
  2601. int i;
  2602. int ret = 0;
  2603. unsigned long flags;
  2604. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  2605. struct task_struct *g, *t;
  2606. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  2607. ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
  2608. * sizeof(struct ftrace_ret_stack),
  2609. GFP_KERNEL);
  2610. if (!ret_stack_list[i]) {
  2611. start = 0;
  2612. end = i;
  2613. ret = -ENOMEM;
  2614. goto free;
  2615. }
  2616. }
  2617. read_lock_irqsave(&tasklist_lock, flags);
  2618. do_each_thread(g, t) {
  2619. if (start == end) {
  2620. ret = -EAGAIN;
  2621. goto unlock;
  2622. }
  2623. if (t->ret_stack == NULL) {
  2624. atomic_set(&t->tracing_graph_pause, 0);
  2625. atomic_set(&t->trace_overrun, 0);
  2626. t->curr_ret_stack = -1;
  2627. /* Make sure the tasks see the -1 first: */
  2628. smp_wmb();
  2629. t->ret_stack = ret_stack_list[start++];
  2630. }
  2631. } while_each_thread(g, t);
  2632. unlock:
  2633. read_unlock_irqrestore(&tasklist_lock, flags);
  2634. free:
  2635. for (i = start; i < end; i++)
  2636. kfree(ret_stack_list[i]);
  2637. return ret;
  2638. }
  2639. static void
  2640. ftrace_graph_probe_sched_switch(void *ignore,
  2641. struct task_struct *prev, struct task_struct *next)
  2642. {
  2643. unsigned long long timestamp;
  2644. int index;
  2645. /*
  2646. * Does the user want to count the time a function was asleep.
  2647. * If so, do not update the time stamps.
  2648. */
  2649. if (trace_flags & TRACE_ITER_SLEEP_TIME)
  2650. return;
  2651. timestamp = trace_clock_local();
  2652. prev->ftrace_timestamp = timestamp;
  2653. /* only process tasks that we timestamped */
  2654. if (!next->ftrace_timestamp)
  2655. return;
  2656. /*
  2657. * Update all the counters in next to make up for the
  2658. * time next was sleeping.
  2659. */
  2660. timestamp -= next->ftrace_timestamp;
  2661. for (index = next->curr_ret_stack; index >= 0; index--)
  2662. next->ret_stack[index].calltime += timestamp;
  2663. }
  2664. /* Allocate a return stack for each task */
  2665. static int start_graph_tracing(void)
  2666. {
  2667. struct ftrace_ret_stack **ret_stack_list;
  2668. int ret, cpu;
  2669. ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
  2670. sizeof(struct ftrace_ret_stack *),
  2671. GFP_KERNEL);
  2672. if (!ret_stack_list)
  2673. return -ENOMEM;
  2674. /* The cpu_boot init_task->ret_stack will never be freed */
  2675. for_each_online_cpu(cpu) {
  2676. if (!idle_task(cpu)->ret_stack)
  2677. ftrace_graph_init_task(idle_task(cpu));
  2678. }
  2679. do {
  2680. ret = alloc_retstack_tasklist(ret_stack_list);
  2681. } while (ret == -EAGAIN);
  2682. if (!ret) {
  2683. ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  2684. if (ret)
  2685. pr_info("ftrace_graph: Couldn't activate tracepoint"
  2686. " probe to kernel_sched_switch\n");
  2687. }
  2688. kfree(ret_stack_list);
  2689. return ret;
  2690. }
  2691. /*
  2692. * Hibernation protection.
  2693. * The state of the current task is too much unstable during
  2694. * suspend/restore to disk. We want to protect against that.
  2695. */
  2696. static int
  2697. ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
  2698. void *unused)
  2699. {
  2700. switch (state) {
  2701. case PM_HIBERNATION_PREPARE:
  2702. pause_graph_tracing();
  2703. break;
  2704. case PM_POST_HIBERNATION:
  2705. unpause_graph_tracing();
  2706. break;
  2707. }
  2708. return NOTIFY_DONE;
  2709. }
  2710. int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  2711. trace_func_graph_ent_t entryfunc)
  2712. {
  2713. int ret = 0;
  2714. mutex_lock(&ftrace_lock);
  2715. /* we currently allow only one tracer registered at a time */
  2716. if (ftrace_graph_active) {
  2717. ret = -EBUSY;
  2718. goto out;
  2719. }
  2720. ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
  2721. register_pm_notifier(&ftrace_suspend_notifier);
  2722. ftrace_graph_active++;
  2723. ret = start_graph_tracing();
  2724. if (ret) {
  2725. ftrace_graph_active--;
  2726. goto out;
  2727. }
  2728. ftrace_graph_return = retfunc;
  2729. ftrace_graph_entry = entryfunc;
  2730. ftrace_startup(FTRACE_START_FUNC_RET);
  2731. out:
  2732. mutex_unlock(&ftrace_lock);
  2733. return ret;
  2734. }
  2735. void unregister_ftrace_graph(void)
  2736. {
  2737. mutex_lock(&ftrace_lock);
  2738. if (unlikely(!ftrace_graph_active))
  2739. goto out;
  2740. ftrace_graph_active--;
  2741. ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
  2742. ftrace_graph_entry = ftrace_graph_entry_stub;
  2743. ftrace_shutdown(FTRACE_STOP_FUNC_RET);
  2744. unregister_pm_notifier(&ftrace_suspend_notifier);
  2745. unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  2746. out:
  2747. mutex_unlock(&ftrace_lock);
  2748. }
  2749. /* Allocate a return stack for newly created task */
  2750. void ftrace_graph_init_task(struct task_struct *t)
  2751. {
  2752. /* Make sure we do not use the parent ret_stack */
  2753. t->ret_stack = NULL;
  2754. t->curr_ret_stack = -1;
  2755. if (ftrace_graph_active) {
  2756. struct ftrace_ret_stack *ret_stack;
  2757. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  2758. * sizeof(struct ftrace_ret_stack),
  2759. GFP_KERNEL);
  2760. if (!ret_stack)
  2761. return;
  2762. atomic_set(&t->tracing_graph_pause, 0);
  2763. atomic_set(&t->trace_overrun, 0);
  2764. t->ftrace_timestamp = 0;
  2765. /* make curr_ret_stack visable before we add the ret_stack */
  2766. smp_wmb();
  2767. t->ret_stack = ret_stack;
  2768. }
  2769. }
  2770. void ftrace_graph_exit_task(struct task_struct *t)
  2771. {
  2772. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  2773. t->ret_stack = NULL;
  2774. /* NULL must become visible to IRQs before we free it: */
  2775. barrier();
  2776. kfree(ret_stack);
  2777. }
  2778. void ftrace_graph_stop(void)
  2779. {
  2780. ftrace_stop();
  2781. }
  2782. #endif