ftrace.c 72 KB

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