ftrace.c 90 KB

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