ftrace.c 72 KB

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