ftrace.c 90 KB

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