ftrace.c 89 KB

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