ftrace.c 85 KB

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