ftrace.c 73 KB

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