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. ({ \
  38. int ___r = cond; \
  39. if (WARN_ON(___r)) \
  40. ftrace_kill(); \
  41. ___r; \
  42. })
  43. #define FTRACE_WARN_ON_ONCE(cond) \
  44. ({ \
  45. int ___r = cond; \
  46. if (WARN_ON_ONCE(___r)) \
  47. ftrace_kill(); \
  48. ___r; \
  49. })
  50. /* hash bits for specific function selection */
  51. #define FTRACE_HASH_BITS 7
  52. #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
  53. /* ftrace_enabled is a method to turn ftrace on or off */
  54. int ftrace_enabled __read_mostly;
  55. static int last_ftrace_enabled;
  56. /* Quick disabling of function tracer. */
  57. int function_trace_stop;
  58. /* List for set_ftrace_pid's pids. */
  59. LIST_HEAD(ftrace_pids);
  60. struct ftrace_pid {
  61. struct list_head list;
  62. struct pid *pid;
  63. };
  64. /*
  65. * ftrace_disabled is set when an anomaly is discovered.
  66. * ftrace_disabled is much stronger than ftrace_enabled.
  67. */
  68. static int ftrace_disabled __read_mostly;
  69. static DEFINE_MUTEX(ftrace_lock);
  70. static struct ftrace_ops ftrace_list_end __read_mostly =
  71. {
  72. .func = ftrace_stub,
  73. };
  74. static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
  75. ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
  76. ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
  77. ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
  78. /*
  79. * Traverse the ftrace_list, invoking all entries. The reason that we
  80. * can use rcu_dereference_raw() is that elements removed from this list
  81. * are simply leaked, so there is no need to interact with a grace-period
  82. * mechanism. The rcu_dereference_raw() calls are needed to handle
  83. * concurrent insertions into the ftrace_list.
  84. *
  85. * Silly Alpha and silly pointer-speculation compiler optimizations!
  86. */
  87. static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
  88. {
  89. struct ftrace_ops *op = rcu_dereference_raw(ftrace_list); /*see above*/
  90. while (op != &ftrace_list_end) {
  91. op->func(ip, parent_ip);
  92. op = rcu_dereference_raw(op->next); /*see above*/
  93. };
  94. }
  95. static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
  96. {
  97. if (!test_tsk_trace_trace(current))
  98. return;
  99. ftrace_pid_function(ip, parent_ip);
  100. }
  101. static void set_ftrace_pid_function(ftrace_func_t func)
  102. {
  103. /* do not set ftrace_pid_function to itself! */
  104. if (func != ftrace_pid_func)
  105. ftrace_pid_function = func;
  106. }
  107. /**
  108. * clear_ftrace_function - reset the ftrace function
  109. *
  110. * This NULLs the ftrace function and in essence stops
  111. * tracing. There may be lag
  112. */
  113. void clear_ftrace_function(void)
  114. {
  115. ftrace_trace_function = ftrace_stub;
  116. __ftrace_trace_function = ftrace_stub;
  117. ftrace_pid_function = ftrace_stub;
  118. }
  119. #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  120. /*
  121. * For those archs that do not test ftrace_trace_stop in their
  122. * mcount call site, we need to do it from C.
  123. */
  124. static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
  125. {
  126. if (function_trace_stop)
  127. return;
  128. __ftrace_trace_function(ip, parent_ip);
  129. }
  130. #endif
  131. static int __register_ftrace_function(struct ftrace_ops *ops)
  132. {
  133. ops->next = ftrace_list;
  134. /*
  135. * We are entering ops into the ftrace_list but another
  136. * CPU might be walking that list. We need to make sure
  137. * the ops->next pointer is valid before another CPU sees
  138. * the ops pointer included into the ftrace_list.
  139. */
  140. rcu_assign_pointer(ftrace_list, ops);
  141. if (ftrace_enabled) {
  142. ftrace_func_t func;
  143. if (ops->next == &ftrace_list_end)
  144. func = ops->func;
  145. else
  146. func = ftrace_list_func;
  147. if (!list_empty(&ftrace_pids)) {
  148. set_ftrace_pid_function(func);
  149. func = ftrace_pid_func;
  150. }
  151. /*
  152. * For one func, simply call it directly.
  153. * For more than one func, call the chain.
  154. */
  155. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  156. ftrace_trace_function = func;
  157. #else
  158. __ftrace_trace_function = func;
  159. ftrace_trace_function = ftrace_test_stop_func;
  160. #endif
  161. }
  162. return 0;
  163. }
  164. static int __unregister_ftrace_function(struct ftrace_ops *ops)
  165. {
  166. struct ftrace_ops **p;
  167. /*
  168. * If we are removing the last function, then simply point
  169. * to the ftrace_stub.
  170. */
  171. if (ftrace_list == ops && ops->next == &ftrace_list_end) {
  172. ftrace_trace_function = ftrace_stub;
  173. ftrace_list = &ftrace_list_end;
  174. return 0;
  175. }
  176. for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
  177. if (*p == ops)
  178. break;
  179. if (*p != ops)
  180. return -1;
  181. *p = (*p)->next;
  182. if (ftrace_enabled) {
  183. /* If we only have one func left, then call that directly */
  184. if (ftrace_list->next == &ftrace_list_end) {
  185. ftrace_func_t func = ftrace_list->func;
  186. if (!list_empty(&ftrace_pids)) {
  187. set_ftrace_pid_function(func);
  188. func = ftrace_pid_func;
  189. }
  190. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  191. ftrace_trace_function = func;
  192. #else
  193. __ftrace_trace_function = func;
  194. #endif
  195. }
  196. }
  197. return 0;
  198. }
  199. static void ftrace_update_pid_func(void)
  200. {
  201. ftrace_func_t func;
  202. if (ftrace_trace_function == ftrace_stub)
  203. return;
  204. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  205. func = ftrace_trace_function;
  206. #else
  207. func = __ftrace_trace_function;
  208. #endif
  209. if (!list_empty(&ftrace_pids)) {
  210. set_ftrace_pid_function(func);
  211. func = ftrace_pid_func;
  212. } else {
  213. if (func == ftrace_pid_func)
  214. func = ftrace_pid_function;
  215. }
  216. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  217. ftrace_trace_function = func;
  218. #else
  219. __ftrace_trace_function = func;
  220. #endif
  221. }
  222. #ifdef CONFIG_FUNCTION_PROFILER
  223. struct ftrace_profile {
  224. struct hlist_node node;
  225. unsigned long ip;
  226. unsigned long counter;
  227. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  228. unsigned long long time;
  229. unsigned long long time_squared;
  230. #endif
  231. };
  232. struct ftrace_profile_page {
  233. struct ftrace_profile_page *next;
  234. unsigned long index;
  235. struct ftrace_profile records[];
  236. };
  237. struct ftrace_profile_stat {
  238. atomic_t disabled;
  239. struct hlist_head *hash;
  240. struct ftrace_profile_page *pages;
  241. struct ftrace_profile_page *start;
  242. struct tracer_stat stat;
  243. };
  244. #define PROFILE_RECORDS_SIZE \
  245. (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
  246. #define PROFILES_PER_PAGE \
  247. (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
  248. static int ftrace_profile_bits __read_mostly;
  249. static int ftrace_profile_enabled __read_mostly;
  250. /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
  251. static DEFINE_MUTEX(ftrace_profile_lock);
  252. static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
  253. #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
  254. static void *
  255. function_stat_next(void *v, int idx)
  256. {
  257. struct ftrace_profile *rec = v;
  258. struct ftrace_profile_page *pg;
  259. pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
  260. again:
  261. if (idx != 0)
  262. rec++;
  263. if ((void *)rec >= (void *)&pg->records[pg->index]) {
  264. pg = pg->next;
  265. if (!pg)
  266. return NULL;
  267. rec = &pg->records[0];
  268. if (!rec->counter)
  269. goto again;
  270. }
  271. return rec;
  272. }
  273. static void *function_stat_start(struct tracer_stat *trace)
  274. {
  275. struct ftrace_profile_stat *stat =
  276. container_of(trace, struct ftrace_profile_stat, stat);
  277. if (!stat || !stat->start)
  278. return NULL;
  279. return function_stat_next(&stat->start->records[0], 0);
  280. }
  281. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  282. /* function graph compares on total time */
  283. static int function_stat_cmp(void *p1, void *p2)
  284. {
  285. struct ftrace_profile *a = p1;
  286. struct ftrace_profile *b = p2;
  287. if (a->time < b->time)
  288. return -1;
  289. if (a->time > b->time)
  290. return 1;
  291. else
  292. return 0;
  293. }
  294. #else
  295. /* not function graph compares against hits */
  296. static int function_stat_cmp(void *p1, void *p2)
  297. {
  298. struct ftrace_profile *a = p1;
  299. struct ftrace_profile *b = p2;
  300. if (a->counter < b->counter)
  301. return -1;
  302. if (a->counter > b->counter)
  303. return 1;
  304. else
  305. return 0;
  306. }
  307. #endif
  308. static int function_stat_headers(struct seq_file *m)
  309. {
  310. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  311. seq_printf(m, " Function "
  312. "Hit Time Avg s^2\n"
  313. " -------- "
  314. "--- ---- --- ---\n");
  315. #else
  316. seq_printf(m, " Function Hit\n"
  317. " -------- ---\n");
  318. #endif
  319. return 0;
  320. }
  321. static int function_stat_show(struct seq_file *m, void *v)
  322. {
  323. struct ftrace_profile *rec = v;
  324. char str[KSYM_SYMBOL_LEN];
  325. int ret = 0;
  326. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  327. static struct trace_seq s;
  328. unsigned long long avg;
  329. unsigned long long stddev;
  330. #endif
  331. mutex_lock(&ftrace_profile_lock);
  332. /* we raced with function_profile_reset() */
  333. if (unlikely(rec->counter == 0)) {
  334. ret = -EBUSY;
  335. goto out;
  336. }
  337. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  338. seq_printf(m, " %-30.30s %10lu", str, rec->counter);
  339. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  340. seq_printf(m, " ");
  341. avg = rec->time;
  342. do_div(avg, rec->counter);
  343. /* Sample standard deviation (s^2) */
  344. if (rec->counter <= 1)
  345. stddev = 0;
  346. else {
  347. stddev = rec->time_squared - rec->counter * avg * avg;
  348. /*
  349. * Divide only 1000 for ns^2 -> us^2 conversion.
  350. * trace_print_graph_duration will divide 1000 again.
  351. */
  352. do_div(stddev, (rec->counter - 1) * 1000);
  353. }
  354. trace_seq_init(&s);
  355. trace_print_graph_duration(rec->time, &s);
  356. trace_seq_puts(&s, " ");
  357. trace_print_graph_duration(avg, &s);
  358. trace_seq_puts(&s, " ");
  359. trace_print_graph_duration(stddev, &s);
  360. trace_print_seq(m, &s);
  361. #endif
  362. seq_putc(m, '\n');
  363. out:
  364. mutex_unlock(&ftrace_profile_lock);
  365. return ret;
  366. }
  367. static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
  368. {
  369. struct ftrace_profile_page *pg;
  370. pg = stat->pages = stat->start;
  371. while (pg) {
  372. memset(pg->records, 0, PROFILE_RECORDS_SIZE);
  373. pg->index = 0;
  374. pg = pg->next;
  375. }
  376. memset(stat->hash, 0,
  377. FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
  378. }
  379. int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
  380. {
  381. struct ftrace_profile_page *pg;
  382. int functions;
  383. int pages;
  384. int i;
  385. /* If we already allocated, do nothing */
  386. if (stat->pages)
  387. return 0;
  388. stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
  389. if (!stat->pages)
  390. return -ENOMEM;
  391. #ifdef CONFIG_DYNAMIC_FTRACE
  392. functions = ftrace_update_tot_cnt;
  393. #else
  394. /*
  395. * We do not know the number of functions that exist because
  396. * dynamic tracing is what counts them. With past experience
  397. * we have around 20K functions. That should be more than enough.
  398. * It is highly unlikely we will execute every function in
  399. * the kernel.
  400. */
  401. functions = 20000;
  402. #endif
  403. pg = stat->start = stat->pages;
  404. pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
  405. for (i = 0; i < pages; i++) {
  406. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  407. if (!pg->next)
  408. goto out_free;
  409. pg = pg->next;
  410. }
  411. return 0;
  412. out_free:
  413. pg = stat->start;
  414. while (pg) {
  415. unsigned long tmp = (unsigned long)pg;
  416. pg = pg->next;
  417. free_page(tmp);
  418. }
  419. free_page((unsigned long)stat->pages);
  420. stat->pages = NULL;
  421. stat->start = NULL;
  422. return -ENOMEM;
  423. }
  424. static int ftrace_profile_init_cpu(int cpu)
  425. {
  426. struct ftrace_profile_stat *stat;
  427. int size;
  428. stat = &per_cpu(ftrace_profile_stats, cpu);
  429. if (stat->hash) {
  430. /* If the profile is already created, simply reset it */
  431. ftrace_profile_reset(stat);
  432. return 0;
  433. }
  434. /*
  435. * We are profiling all functions, but usually only a few thousand
  436. * functions are hit. We'll make a hash of 1024 items.
  437. */
  438. size = FTRACE_PROFILE_HASH_SIZE;
  439. stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
  440. if (!stat->hash)
  441. return -ENOMEM;
  442. if (!ftrace_profile_bits) {
  443. size--;
  444. for (; size; size >>= 1)
  445. ftrace_profile_bits++;
  446. }
  447. /* Preallocate the function profiling pages */
  448. if (ftrace_profile_pages_init(stat) < 0) {
  449. kfree(stat->hash);
  450. stat->hash = NULL;
  451. return -ENOMEM;
  452. }
  453. return 0;
  454. }
  455. static int ftrace_profile_init(void)
  456. {
  457. int cpu;
  458. int ret = 0;
  459. for_each_online_cpu(cpu) {
  460. ret = ftrace_profile_init_cpu(cpu);
  461. if (ret)
  462. break;
  463. }
  464. return ret;
  465. }
  466. /* interrupts must be disabled */
  467. static struct ftrace_profile *
  468. ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
  469. {
  470. struct ftrace_profile *rec;
  471. struct hlist_head *hhd;
  472. struct hlist_node *n;
  473. unsigned long key;
  474. key = hash_long(ip, ftrace_profile_bits);
  475. hhd = &stat->hash[key];
  476. if (hlist_empty(hhd))
  477. return NULL;
  478. hlist_for_each_entry_rcu(rec, n, hhd, node) {
  479. if (rec->ip == ip)
  480. return rec;
  481. }
  482. return NULL;
  483. }
  484. static void ftrace_add_profile(struct ftrace_profile_stat *stat,
  485. struct ftrace_profile *rec)
  486. {
  487. unsigned long key;
  488. key = hash_long(rec->ip, ftrace_profile_bits);
  489. hlist_add_head_rcu(&rec->node, &stat->hash[key]);
  490. }
  491. /*
  492. * The memory is already allocated, this simply finds a new record to use.
  493. */
  494. static struct ftrace_profile *
  495. ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
  496. {
  497. struct ftrace_profile *rec = NULL;
  498. /* prevent recursion (from NMIs) */
  499. if (atomic_inc_return(&stat->disabled) != 1)
  500. goto out;
  501. /*
  502. * Try to find the function again since an NMI
  503. * could have added it
  504. */
  505. rec = ftrace_find_profiled_func(stat, ip);
  506. if (rec)
  507. goto out;
  508. if (stat->pages->index == PROFILES_PER_PAGE) {
  509. if (!stat->pages->next)
  510. goto out;
  511. stat->pages = stat->pages->next;
  512. }
  513. rec = &stat->pages->records[stat->pages->index++];
  514. rec->ip = ip;
  515. ftrace_add_profile(stat, rec);
  516. out:
  517. atomic_dec(&stat->disabled);
  518. return rec;
  519. }
  520. static void
  521. function_profile_call(unsigned long ip, unsigned long parent_ip)
  522. {
  523. struct ftrace_profile_stat *stat;
  524. struct ftrace_profile *rec;
  525. unsigned long flags;
  526. if (!ftrace_profile_enabled)
  527. return;
  528. local_irq_save(flags);
  529. stat = &__get_cpu_var(ftrace_profile_stats);
  530. if (!stat->hash || !ftrace_profile_enabled)
  531. goto out;
  532. rec = ftrace_find_profiled_func(stat, ip);
  533. if (!rec) {
  534. rec = ftrace_profile_alloc(stat, ip);
  535. if (!rec)
  536. goto out;
  537. }
  538. rec->counter++;
  539. out:
  540. local_irq_restore(flags);
  541. }
  542. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  543. static int profile_graph_entry(struct ftrace_graph_ent *trace)
  544. {
  545. function_profile_call(trace->func, 0);
  546. return 1;
  547. }
  548. static void profile_graph_return(struct ftrace_graph_ret *trace)
  549. {
  550. struct ftrace_profile_stat *stat;
  551. unsigned long long calltime;
  552. struct ftrace_profile *rec;
  553. unsigned long flags;
  554. local_irq_save(flags);
  555. stat = &__get_cpu_var(ftrace_profile_stats);
  556. if (!stat->hash || !ftrace_profile_enabled)
  557. goto out;
  558. /* If the calltime was zero'd ignore it */
  559. if (!trace->calltime)
  560. goto out;
  561. calltime = trace->rettime - trace->calltime;
  562. if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
  563. int index;
  564. index = trace->depth;
  565. /* Append this call time to the parent time to subtract */
  566. if (index)
  567. current->ret_stack[index - 1].subtime += calltime;
  568. if (current->ret_stack[index].subtime < calltime)
  569. calltime -= current->ret_stack[index].subtime;
  570. else
  571. calltime = 0;
  572. }
  573. rec = ftrace_find_profiled_func(stat, trace->func);
  574. if (rec) {
  575. rec->time += calltime;
  576. rec->time_squared += calltime * calltime;
  577. }
  578. out:
  579. local_irq_restore(flags);
  580. }
  581. static int register_ftrace_profiler(void)
  582. {
  583. return register_ftrace_graph(&profile_graph_return,
  584. &profile_graph_entry);
  585. }
  586. static void unregister_ftrace_profiler(void)
  587. {
  588. unregister_ftrace_graph();
  589. }
  590. #else
  591. static struct ftrace_ops ftrace_profile_ops __read_mostly =
  592. {
  593. .func = function_profile_call,
  594. };
  595. static int register_ftrace_profiler(void)
  596. {
  597. return register_ftrace_function(&ftrace_profile_ops);
  598. }
  599. static void unregister_ftrace_profiler(void)
  600. {
  601. unregister_ftrace_function(&ftrace_profile_ops);
  602. }
  603. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  604. static ssize_t
  605. ftrace_profile_write(struct file *filp, const char __user *ubuf,
  606. size_t cnt, loff_t *ppos)
  607. {
  608. unsigned long val;
  609. char buf[64]; /* big enough to hold a number */
  610. int ret;
  611. if (cnt >= sizeof(buf))
  612. return -EINVAL;
  613. if (copy_from_user(&buf, ubuf, cnt))
  614. return -EFAULT;
  615. buf[cnt] = 0;
  616. ret = strict_strtoul(buf, 10, &val);
  617. if (ret < 0)
  618. return ret;
  619. val = !!val;
  620. mutex_lock(&ftrace_profile_lock);
  621. if (ftrace_profile_enabled ^ val) {
  622. if (val) {
  623. ret = ftrace_profile_init();
  624. if (ret < 0) {
  625. cnt = ret;
  626. goto out;
  627. }
  628. ret = register_ftrace_profiler();
  629. if (ret < 0) {
  630. cnt = ret;
  631. goto out;
  632. }
  633. ftrace_profile_enabled = 1;
  634. } else {
  635. ftrace_profile_enabled = 0;
  636. /*
  637. * unregister_ftrace_profiler calls stop_machine
  638. * so this acts like an synchronize_sched.
  639. */
  640. unregister_ftrace_profiler();
  641. }
  642. }
  643. out:
  644. mutex_unlock(&ftrace_profile_lock);
  645. *ppos += cnt;
  646. return cnt;
  647. }
  648. static ssize_t
  649. ftrace_profile_read(struct file *filp, char __user *ubuf,
  650. size_t cnt, loff_t *ppos)
  651. {
  652. char buf[64]; /* big enough to hold a number */
  653. int r;
  654. r = sprintf(buf, "%u\n", ftrace_profile_enabled);
  655. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  656. }
  657. static const struct file_operations ftrace_profile_fops = {
  658. .open = tracing_open_generic,
  659. .read = ftrace_profile_read,
  660. .write = ftrace_profile_write,
  661. .llseek = default_llseek,
  662. };
  663. /* used to initialize the real stat files */
  664. static struct tracer_stat function_stats __initdata = {
  665. .name = "functions",
  666. .stat_start = function_stat_start,
  667. .stat_next = function_stat_next,
  668. .stat_cmp = function_stat_cmp,
  669. .stat_headers = function_stat_headers,
  670. .stat_show = function_stat_show
  671. };
  672. static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
  673. {
  674. struct ftrace_profile_stat *stat;
  675. struct dentry *entry;
  676. char *name;
  677. int ret;
  678. int cpu;
  679. for_each_possible_cpu(cpu) {
  680. stat = &per_cpu(ftrace_profile_stats, cpu);
  681. /* allocate enough for function name + cpu number */
  682. name = kmalloc(32, GFP_KERNEL);
  683. if (!name) {
  684. /*
  685. * The files created are permanent, if something happens
  686. * we still do not free memory.
  687. */
  688. WARN(1,
  689. "Could not allocate stat file for cpu %d\n",
  690. cpu);
  691. return;
  692. }
  693. stat->stat = function_stats;
  694. snprintf(name, 32, "function%d", cpu);
  695. stat->stat.name = name;
  696. ret = register_stat_tracer(&stat->stat);
  697. if (ret) {
  698. WARN(1,
  699. "Could not register function stat for cpu %d\n",
  700. cpu);
  701. kfree(name);
  702. return;
  703. }
  704. }
  705. entry = debugfs_create_file("function_profile_enabled", 0644,
  706. d_tracer, NULL, &ftrace_profile_fops);
  707. if (!entry)
  708. pr_warning("Could not create debugfs "
  709. "'function_profile_enabled' entry\n");
  710. }
  711. #else /* CONFIG_FUNCTION_PROFILER */
  712. static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
  713. {
  714. }
  715. #endif /* CONFIG_FUNCTION_PROFILER */
  716. static struct pid * const ftrace_swapper_pid = &init_struct_pid;
  717. #ifdef CONFIG_DYNAMIC_FTRACE
  718. #ifndef CONFIG_FTRACE_MCOUNT_RECORD
  719. # error Dynamic ftrace depends on MCOUNT_RECORD
  720. #endif
  721. static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
  722. struct ftrace_func_probe {
  723. struct hlist_node node;
  724. struct ftrace_probe_ops *ops;
  725. unsigned long flags;
  726. unsigned long ip;
  727. void *data;
  728. struct rcu_head rcu;
  729. };
  730. enum {
  731. FTRACE_ENABLE_CALLS = (1 << 0),
  732. FTRACE_DISABLE_CALLS = (1 << 1),
  733. FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
  734. FTRACE_START_FUNC_RET = (1 << 3),
  735. FTRACE_STOP_FUNC_RET = (1 << 4),
  736. };
  737. static int ftrace_filtered;
  738. static struct dyn_ftrace *ftrace_new_addrs;
  739. static DEFINE_MUTEX(ftrace_regex_lock);
  740. struct ftrace_page {
  741. struct ftrace_page *next;
  742. int index;
  743. struct dyn_ftrace records[];
  744. };
  745. #define ENTRIES_PER_PAGE \
  746. ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
  747. /* estimate from running different kernels */
  748. #define NR_TO_INIT 10000
  749. static struct ftrace_page *ftrace_pages_start;
  750. static struct ftrace_page *ftrace_pages;
  751. static struct dyn_ftrace *ftrace_free_records;
  752. /*
  753. * This is a double for. Do not use 'break' to break out of the loop,
  754. * you must use a goto.
  755. */
  756. #define do_for_each_ftrace_rec(pg, rec) \
  757. for (pg = ftrace_pages_start; pg; pg = pg->next) { \
  758. int _____i; \
  759. for (_____i = 0; _____i < pg->index; _____i++) { \
  760. rec = &pg->records[_____i];
  761. #define while_for_each_ftrace_rec() \
  762. } \
  763. }
  764. static void ftrace_free_rec(struct dyn_ftrace *rec)
  765. {
  766. rec->freelist = ftrace_free_records;
  767. ftrace_free_records = rec;
  768. rec->flags |= FTRACE_FL_FREE;
  769. }
  770. static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
  771. {
  772. struct dyn_ftrace *rec;
  773. /* First check for freed records */
  774. if (ftrace_free_records) {
  775. rec = ftrace_free_records;
  776. if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
  777. FTRACE_WARN_ON_ONCE(1);
  778. ftrace_free_records = NULL;
  779. return NULL;
  780. }
  781. ftrace_free_records = rec->freelist;
  782. memset(rec, 0, sizeof(*rec));
  783. return rec;
  784. }
  785. if (ftrace_pages->index == ENTRIES_PER_PAGE) {
  786. if (!ftrace_pages->next) {
  787. /* allocate another page */
  788. ftrace_pages->next =
  789. (void *)get_zeroed_page(GFP_KERNEL);
  790. if (!ftrace_pages->next)
  791. return NULL;
  792. }
  793. ftrace_pages = ftrace_pages->next;
  794. }
  795. return &ftrace_pages->records[ftrace_pages->index++];
  796. }
  797. static struct dyn_ftrace *
  798. ftrace_record_ip(unsigned long ip)
  799. {
  800. struct dyn_ftrace *rec;
  801. if (ftrace_disabled)
  802. return NULL;
  803. rec = ftrace_alloc_dyn_node(ip);
  804. if (!rec)
  805. return NULL;
  806. rec->ip = ip;
  807. rec->newlist = ftrace_new_addrs;
  808. ftrace_new_addrs = rec;
  809. return rec;
  810. }
  811. static void print_ip_ins(const char *fmt, unsigned char *p)
  812. {
  813. int i;
  814. printk(KERN_CONT "%s", fmt);
  815. for (i = 0; i < MCOUNT_INSN_SIZE; i++)
  816. printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
  817. }
  818. static void ftrace_bug(int failed, unsigned long ip)
  819. {
  820. switch (failed) {
  821. case -EFAULT:
  822. FTRACE_WARN_ON_ONCE(1);
  823. pr_info("ftrace faulted on modifying ");
  824. print_ip_sym(ip);
  825. break;
  826. case -EINVAL:
  827. FTRACE_WARN_ON_ONCE(1);
  828. pr_info("ftrace failed to modify ");
  829. print_ip_sym(ip);
  830. print_ip_ins(" actual: ", (unsigned char *)ip);
  831. printk(KERN_CONT "\n");
  832. break;
  833. case -EPERM:
  834. FTRACE_WARN_ON_ONCE(1);
  835. pr_info("ftrace faulted on writing ");
  836. print_ip_sym(ip);
  837. break;
  838. default:
  839. FTRACE_WARN_ON_ONCE(1);
  840. pr_info("ftrace faulted on unknown error ");
  841. print_ip_sym(ip);
  842. }
  843. }
  844. /* Return 1 if the address range is reserved for ftrace */
  845. int ftrace_text_reserved(void *start, void *end)
  846. {
  847. struct dyn_ftrace *rec;
  848. struct ftrace_page *pg;
  849. do_for_each_ftrace_rec(pg, rec) {
  850. if (rec->ip <= (unsigned long)end &&
  851. rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
  852. return 1;
  853. } while_for_each_ftrace_rec();
  854. return 0;
  855. }
  856. static int
  857. __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
  858. {
  859. unsigned long ftrace_addr;
  860. unsigned long flag = 0UL;
  861. ftrace_addr = (unsigned long)FTRACE_ADDR;
  862. /*
  863. * If this record is not to be traced or we want to disable it,
  864. * then disable it.
  865. *
  866. * If we want to enable it and filtering is off, then enable it.
  867. *
  868. * If we want to enable it and filtering is on, enable it only if
  869. * it's filtered
  870. */
  871. if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
  872. if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
  873. flag = FTRACE_FL_ENABLED;
  874. }
  875. /* If the state of this record hasn't changed, then do nothing */
  876. if ((rec->flags & FTRACE_FL_ENABLED) == flag)
  877. return 0;
  878. if (flag) {
  879. rec->flags |= FTRACE_FL_ENABLED;
  880. return ftrace_make_call(rec, ftrace_addr);
  881. }
  882. rec->flags &= ~FTRACE_FL_ENABLED;
  883. return ftrace_make_nop(NULL, rec, ftrace_addr);
  884. }
  885. static void ftrace_replace_code(int enable)
  886. {
  887. struct dyn_ftrace *rec;
  888. struct ftrace_page *pg;
  889. int failed;
  890. do_for_each_ftrace_rec(pg, rec) {
  891. /*
  892. * Skip over free records, records that have
  893. * failed and not converted.
  894. */
  895. if (rec->flags & FTRACE_FL_FREE ||
  896. rec->flags & FTRACE_FL_FAILED ||
  897. !(rec->flags & FTRACE_FL_CONVERTED))
  898. continue;
  899. failed = __ftrace_replace_code(rec, enable);
  900. if (failed) {
  901. rec->flags |= FTRACE_FL_FAILED;
  902. ftrace_bug(failed, rec->ip);
  903. /* Stop processing */
  904. return;
  905. }
  906. } while_for_each_ftrace_rec();
  907. }
  908. static int
  909. ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
  910. {
  911. unsigned long ip;
  912. int ret;
  913. ip = rec->ip;
  914. ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
  915. if (ret) {
  916. ftrace_bug(ret, ip);
  917. rec->flags |= FTRACE_FL_FAILED;
  918. return 0;
  919. }
  920. return 1;
  921. }
  922. /*
  923. * archs can override this function if they must do something
  924. * before the modifying code is performed.
  925. */
  926. int __weak ftrace_arch_code_modify_prepare(void)
  927. {
  928. return 0;
  929. }
  930. /*
  931. * archs can override this function if they must do something
  932. * after the modifying code is performed.
  933. */
  934. int __weak ftrace_arch_code_modify_post_process(void)
  935. {
  936. return 0;
  937. }
  938. static int __ftrace_modify_code(void *data)
  939. {
  940. int *command = data;
  941. if (*command & FTRACE_ENABLE_CALLS)
  942. ftrace_replace_code(1);
  943. else if (*command & FTRACE_DISABLE_CALLS)
  944. ftrace_replace_code(0);
  945. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  946. ftrace_update_ftrace_func(ftrace_trace_function);
  947. if (*command & FTRACE_START_FUNC_RET)
  948. ftrace_enable_ftrace_graph_caller();
  949. else if (*command & FTRACE_STOP_FUNC_RET)
  950. ftrace_disable_ftrace_graph_caller();
  951. return 0;
  952. }
  953. static void ftrace_run_update_code(int command)
  954. {
  955. int ret;
  956. ret = ftrace_arch_code_modify_prepare();
  957. FTRACE_WARN_ON(ret);
  958. if (ret)
  959. return;
  960. stop_machine(__ftrace_modify_code, &command, NULL);
  961. ret = ftrace_arch_code_modify_post_process();
  962. FTRACE_WARN_ON(ret);
  963. }
  964. static ftrace_func_t saved_ftrace_func;
  965. static int ftrace_start_up;
  966. static void ftrace_startup_enable(int command)
  967. {
  968. if (saved_ftrace_func != ftrace_trace_function) {
  969. saved_ftrace_func = ftrace_trace_function;
  970. command |= FTRACE_UPDATE_TRACE_FUNC;
  971. }
  972. if (!command || !ftrace_enabled)
  973. return;
  974. ftrace_run_update_code(command);
  975. }
  976. static void ftrace_startup(int command)
  977. {
  978. if (unlikely(ftrace_disabled))
  979. return;
  980. ftrace_start_up++;
  981. command |= FTRACE_ENABLE_CALLS;
  982. ftrace_startup_enable(command);
  983. }
  984. static void ftrace_shutdown(int command)
  985. {
  986. if (unlikely(ftrace_disabled))
  987. return;
  988. ftrace_start_up--;
  989. /*
  990. * Just warn in case of unbalance, no need to kill ftrace, it's not
  991. * critical but the ftrace_call callers may be never nopped again after
  992. * further ftrace uses.
  993. */
  994. WARN_ON_ONCE(ftrace_start_up < 0);
  995. if (!ftrace_start_up)
  996. command |= FTRACE_DISABLE_CALLS;
  997. if (saved_ftrace_func != ftrace_trace_function) {
  998. saved_ftrace_func = ftrace_trace_function;
  999. command |= FTRACE_UPDATE_TRACE_FUNC;
  1000. }
  1001. if (!command || !ftrace_enabled)
  1002. return;
  1003. ftrace_run_update_code(command);
  1004. }
  1005. static void ftrace_startup_sysctl(void)
  1006. {
  1007. if (unlikely(ftrace_disabled))
  1008. return;
  1009. /* Force update next time */
  1010. saved_ftrace_func = NULL;
  1011. /* ftrace_start_up is true if we want ftrace running */
  1012. if (ftrace_start_up)
  1013. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  1014. }
  1015. static void ftrace_shutdown_sysctl(void)
  1016. {
  1017. if (unlikely(ftrace_disabled))
  1018. return;
  1019. /* ftrace_start_up is true if ftrace is running */
  1020. if (ftrace_start_up)
  1021. ftrace_run_update_code(FTRACE_DISABLE_CALLS);
  1022. }
  1023. static cycle_t ftrace_update_time;
  1024. static unsigned long ftrace_update_cnt;
  1025. unsigned long ftrace_update_tot_cnt;
  1026. static int ftrace_update_code(struct module *mod)
  1027. {
  1028. struct dyn_ftrace *p;
  1029. cycle_t start, stop;
  1030. start = ftrace_now(raw_smp_processor_id());
  1031. ftrace_update_cnt = 0;
  1032. while (ftrace_new_addrs) {
  1033. /* If something went wrong, bail without enabling anything */
  1034. if (unlikely(ftrace_disabled))
  1035. return -1;
  1036. p = ftrace_new_addrs;
  1037. ftrace_new_addrs = p->newlist;
  1038. p->flags = 0L;
  1039. /*
  1040. * Do the initial record conversion from mcount jump
  1041. * to the NOP instructions.
  1042. */
  1043. if (!ftrace_code_disable(mod, p)) {
  1044. ftrace_free_rec(p);
  1045. continue;
  1046. }
  1047. p->flags |= FTRACE_FL_CONVERTED;
  1048. ftrace_update_cnt++;
  1049. /*
  1050. * If the tracing is enabled, go ahead and enable the record.
  1051. *
  1052. * The reason not to enable the record immediatelly is the
  1053. * inherent check of ftrace_make_nop/ftrace_make_call for
  1054. * correct previous instructions. Making first the NOP
  1055. * conversion puts the module to the correct state, thus
  1056. * passing the ftrace_make_call check.
  1057. */
  1058. if (ftrace_start_up) {
  1059. int failed = __ftrace_replace_code(p, 1);
  1060. if (failed) {
  1061. ftrace_bug(failed, p->ip);
  1062. ftrace_free_rec(p);
  1063. }
  1064. }
  1065. }
  1066. stop = ftrace_now(raw_smp_processor_id());
  1067. ftrace_update_time = stop - start;
  1068. ftrace_update_tot_cnt += ftrace_update_cnt;
  1069. return 0;
  1070. }
  1071. static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
  1072. {
  1073. struct ftrace_page *pg;
  1074. int cnt;
  1075. int i;
  1076. /* allocate a few pages */
  1077. ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
  1078. if (!ftrace_pages_start)
  1079. return -1;
  1080. /*
  1081. * Allocate a few more pages.
  1082. *
  1083. * TODO: have some parser search vmlinux before
  1084. * final linking to find all calls to ftrace.
  1085. * Then we can:
  1086. * a) know how many pages to allocate.
  1087. * and/or
  1088. * b) set up the table then.
  1089. *
  1090. * The dynamic code is still necessary for
  1091. * modules.
  1092. */
  1093. pg = ftrace_pages = ftrace_pages_start;
  1094. cnt = num_to_init / ENTRIES_PER_PAGE;
  1095. pr_info("ftrace: allocating %ld entries in %d pages\n",
  1096. num_to_init, cnt + 1);
  1097. for (i = 0; i < cnt; i++) {
  1098. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  1099. /* If we fail, we'll try later anyway */
  1100. if (!pg->next)
  1101. break;
  1102. pg = pg->next;
  1103. }
  1104. return 0;
  1105. }
  1106. enum {
  1107. FTRACE_ITER_FILTER = (1 << 0),
  1108. FTRACE_ITER_NOTRACE = (1 << 1),
  1109. FTRACE_ITER_FAILURES = (1 << 2),
  1110. FTRACE_ITER_PRINTALL = (1 << 3),
  1111. FTRACE_ITER_HASH = (1 << 4),
  1112. };
  1113. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  1114. struct ftrace_iterator {
  1115. loff_t pos;
  1116. loff_t func_pos;
  1117. struct ftrace_page *pg;
  1118. struct dyn_ftrace *func;
  1119. struct ftrace_func_probe *probe;
  1120. struct trace_parser parser;
  1121. int hidx;
  1122. int idx;
  1123. unsigned flags;
  1124. };
  1125. static void *
  1126. t_hash_next(struct seq_file *m, loff_t *pos)
  1127. {
  1128. struct ftrace_iterator *iter = m->private;
  1129. struct hlist_node *hnd = NULL;
  1130. struct hlist_head *hhd;
  1131. (*pos)++;
  1132. iter->pos = *pos;
  1133. if (iter->probe)
  1134. hnd = &iter->probe->node;
  1135. retry:
  1136. if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
  1137. return NULL;
  1138. hhd = &ftrace_func_hash[iter->hidx];
  1139. if (hlist_empty(hhd)) {
  1140. iter->hidx++;
  1141. hnd = NULL;
  1142. goto retry;
  1143. }
  1144. if (!hnd)
  1145. hnd = hhd->first;
  1146. else {
  1147. hnd = hnd->next;
  1148. if (!hnd) {
  1149. iter->hidx++;
  1150. goto retry;
  1151. }
  1152. }
  1153. if (WARN_ON_ONCE(!hnd))
  1154. return NULL;
  1155. iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
  1156. return iter;
  1157. }
  1158. static void *t_hash_start(struct seq_file *m, loff_t *pos)
  1159. {
  1160. struct ftrace_iterator *iter = m->private;
  1161. void *p = NULL;
  1162. loff_t l;
  1163. if (iter->func_pos > *pos)
  1164. return NULL;
  1165. iter->hidx = 0;
  1166. for (l = 0; l <= (*pos - iter->func_pos); ) {
  1167. p = t_hash_next(m, &l);
  1168. if (!p)
  1169. break;
  1170. }
  1171. if (!p)
  1172. return NULL;
  1173. /* Only set this if we have an item */
  1174. iter->flags |= FTRACE_ITER_HASH;
  1175. return iter;
  1176. }
  1177. static int
  1178. t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
  1179. {
  1180. struct ftrace_func_probe *rec;
  1181. rec = iter->probe;
  1182. if (WARN_ON_ONCE(!rec))
  1183. return -EIO;
  1184. if (rec->ops->print)
  1185. return rec->ops->print(m, rec->ip, rec->ops, rec->data);
  1186. seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
  1187. if (rec->data)
  1188. seq_printf(m, ":%p", rec->data);
  1189. seq_putc(m, '\n');
  1190. return 0;
  1191. }
  1192. static void *
  1193. t_next(struct seq_file *m, void *v, loff_t *pos)
  1194. {
  1195. struct ftrace_iterator *iter = m->private;
  1196. struct dyn_ftrace *rec = NULL;
  1197. if (iter->flags & FTRACE_ITER_HASH)
  1198. return t_hash_next(m, pos);
  1199. (*pos)++;
  1200. iter->pos = iter->func_pos = *pos;
  1201. if (iter->flags & FTRACE_ITER_PRINTALL)
  1202. return t_hash_start(m, pos);
  1203. retry:
  1204. if (iter->idx >= iter->pg->index) {
  1205. if (iter->pg->next) {
  1206. iter->pg = iter->pg->next;
  1207. iter->idx = 0;
  1208. goto retry;
  1209. }
  1210. } else {
  1211. rec = &iter->pg->records[iter->idx++];
  1212. if ((rec->flags & FTRACE_FL_FREE) ||
  1213. (!(iter->flags & FTRACE_ITER_FAILURES) &&
  1214. (rec->flags & FTRACE_FL_FAILED)) ||
  1215. ((iter->flags & FTRACE_ITER_FAILURES) &&
  1216. !(rec->flags & FTRACE_FL_FAILED)) ||
  1217. ((iter->flags & FTRACE_ITER_FILTER) &&
  1218. !(rec->flags & FTRACE_FL_FILTER)) ||
  1219. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  1220. !(rec->flags & FTRACE_FL_NOTRACE))) {
  1221. rec = NULL;
  1222. goto retry;
  1223. }
  1224. }
  1225. if (!rec)
  1226. return t_hash_start(m, pos);
  1227. iter->func = rec;
  1228. return iter;
  1229. }
  1230. static void reset_iter_read(struct ftrace_iterator *iter)
  1231. {
  1232. iter->pos = 0;
  1233. iter->func_pos = 0;
  1234. iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
  1235. }
  1236. static void *t_start(struct seq_file *m, loff_t *pos)
  1237. {
  1238. struct ftrace_iterator *iter = m->private;
  1239. void *p = NULL;
  1240. loff_t l;
  1241. mutex_lock(&ftrace_lock);
  1242. /*
  1243. * If an lseek was done, then reset and start from beginning.
  1244. */
  1245. if (*pos < iter->pos)
  1246. reset_iter_read(iter);
  1247. /*
  1248. * For set_ftrace_filter reading, if we have the filter
  1249. * off, we can short cut and just print out that all
  1250. * functions are enabled.
  1251. */
  1252. if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
  1253. if (*pos > 0)
  1254. return t_hash_start(m, pos);
  1255. iter->flags |= FTRACE_ITER_PRINTALL;
  1256. /* reset in case of seek/pread */
  1257. iter->flags &= ~FTRACE_ITER_HASH;
  1258. return iter;
  1259. }
  1260. if (iter->flags & FTRACE_ITER_HASH)
  1261. return t_hash_start(m, pos);
  1262. /*
  1263. * Unfortunately, we need to restart at ftrace_pages_start
  1264. * every time we let go of the ftrace_mutex. This is because
  1265. * those pointers can change without the lock.
  1266. */
  1267. iter->pg = ftrace_pages_start;
  1268. iter->idx = 0;
  1269. for (l = 0; l <= *pos; ) {
  1270. p = t_next(m, p, &l);
  1271. if (!p)
  1272. break;
  1273. }
  1274. if (!p) {
  1275. if (iter->flags & FTRACE_ITER_FILTER)
  1276. return t_hash_start(m, pos);
  1277. return NULL;
  1278. }
  1279. return iter;
  1280. }
  1281. static void t_stop(struct seq_file *m, void *p)
  1282. {
  1283. mutex_unlock(&ftrace_lock);
  1284. }
  1285. static int t_show(struct seq_file *m, void *v)
  1286. {
  1287. struct ftrace_iterator *iter = m->private;
  1288. struct dyn_ftrace *rec;
  1289. if (iter->flags & FTRACE_ITER_HASH)
  1290. return t_hash_show(m, iter);
  1291. if (iter->flags & FTRACE_ITER_PRINTALL) {
  1292. seq_printf(m, "#### all functions enabled ####\n");
  1293. return 0;
  1294. }
  1295. rec = iter->func;
  1296. if (!rec)
  1297. return 0;
  1298. seq_printf(m, "%ps\n", (void *)rec->ip);
  1299. return 0;
  1300. }
  1301. static const struct seq_operations show_ftrace_seq_ops = {
  1302. .start = t_start,
  1303. .next = t_next,
  1304. .stop = t_stop,
  1305. .show = t_show,
  1306. };
  1307. static int
  1308. ftrace_avail_open(struct inode *inode, struct file *file)
  1309. {
  1310. struct ftrace_iterator *iter;
  1311. int ret;
  1312. if (unlikely(ftrace_disabled))
  1313. return -ENODEV;
  1314. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1315. if (!iter)
  1316. return -ENOMEM;
  1317. iter->pg = ftrace_pages_start;
  1318. ret = seq_open(file, &show_ftrace_seq_ops);
  1319. if (!ret) {
  1320. struct seq_file *m = file->private_data;
  1321. m->private = iter;
  1322. } else {
  1323. kfree(iter);
  1324. }
  1325. return ret;
  1326. }
  1327. static int
  1328. ftrace_failures_open(struct inode *inode, struct file *file)
  1329. {
  1330. int ret;
  1331. struct seq_file *m;
  1332. struct ftrace_iterator *iter;
  1333. ret = ftrace_avail_open(inode, file);
  1334. if (!ret) {
  1335. m = file->private_data;
  1336. iter = m->private;
  1337. iter->flags = FTRACE_ITER_FAILURES;
  1338. }
  1339. return ret;
  1340. }
  1341. static void ftrace_filter_reset(int enable)
  1342. {
  1343. struct ftrace_page *pg;
  1344. struct dyn_ftrace *rec;
  1345. unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1346. mutex_lock(&ftrace_lock);
  1347. if (enable)
  1348. ftrace_filtered = 0;
  1349. do_for_each_ftrace_rec(pg, rec) {
  1350. if (rec->flags & FTRACE_FL_FAILED)
  1351. continue;
  1352. rec->flags &= ~type;
  1353. } while_for_each_ftrace_rec();
  1354. mutex_unlock(&ftrace_lock);
  1355. }
  1356. static int
  1357. ftrace_regex_open(struct inode *inode, struct file *file, int enable)
  1358. {
  1359. struct ftrace_iterator *iter;
  1360. int ret = 0;
  1361. if (unlikely(ftrace_disabled))
  1362. return -ENODEV;
  1363. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1364. if (!iter)
  1365. return -ENOMEM;
  1366. if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
  1367. kfree(iter);
  1368. return -ENOMEM;
  1369. }
  1370. mutex_lock(&ftrace_regex_lock);
  1371. if ((file->f_mode & FMODE_WRITE) &&
  1372. (file->f_flags & O_TRUNC))
  1373. ftrace_filter_reset(enable);
  1374. if (file->f_mode & FMODE_READ) {
  1375. iter->pg = ftrace_pages_start;
  1376. iter->flags = enable ? FTRACE_ITER_FILTER :
  1377. FTRACE_ITER_NOTRACE;
  1378. ret = seq_open(file, &show_ftrace_seq_ops);
  1379. if (!ret) {
  1380. struct seq_file *m = file->private_data;
  1381. m->private = iter;
  1382. } else {
  1383. trace_parser_put(&iter->parser);
  1384. kfree(iter);
  1385. }
  1386. } else
  1387. file->private_data = iter;
  1388. mutex_unlock(&ftrace_regex_lock);
  1389. return ret;
  1390. }
  1391. static int
  1392. ftrace_filter_open(struct inode *inode, struct file *file)
  1393. {
  1394. return ftrace_regex_open(inode, file, 1);
  1395. }
  1396. static int
  1397. ftrace_notrace_open(struct inode *inode, struct file *file)
  1398. {
  1399. return ftrace_regex_open(inode, file, 0);
  1400. }
  1401. static loff_t
  1402. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  1403. {
  1404. loff_t ret;
  1405. if (file->f_mode & FMODE_READ)
  1406. ret = seq_lseek(file, offset, origin);
  1407. else
  1408. file->f_pos = ret = 1;
  1409. return ret;
  1410. }
  1411. static int ftrace_match(char *str, char *regex, int len, int type)
  1412. {
  1413. int matched = 0;
  1414. int slen;
  1415. switch (type) {
  1416. case MATCH_FULL:
  1417. if (strcmp(str, regex) == 0)
  1418. matched = 1;
  1419. break;
  1420. case MATCH_FRONT_ONLY:
  1421. if (strncmp(str, regex, len) == 0)
  1422. matched = 1;
  1423. break;
  1424. case MATCH_MIDDLE_ONLY:
  1425. if (strstr(str, regex))
  1426. matched = 1;
  1427. break;
  1428. case MATCH_END_ONLY:
  1429. slen = strlen(str);
  1430. if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
  1431. matched = 1;
  1432. break;
  1433. }
  1434. return matched;
  1435. }
  1436. static int
  1437. ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
  1438. {
  1439. char str[KSYM_SYMBOL_LEN];
  1440. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  1441. return ftrace_match(str, regex, len, type);
  1442. }
  1443. static int ftrace_match_records(char *buff, int len, int enable)
  1444. {
  1445. unsigned int search_len;
  1446. struct ftrace_page *pg;
  1447. struct dyn_ftrace *rec;
  1448. unsigned long flag;
  1449. char *search;
  1450. int type;
  1451. int not;
  1452. int found = 0;
  1453. flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1454. type = filter_parse_regex(buff, len, &search, &not);
  1455. search_len = strlen(search);
  1456. mutex_lock(&ftrace_lock);
  1457. do_for_each_ftrace_rec(pg, rec) {
  1458. if (rec->flags & FTRACE_FL_FAILED)
  1459. continue;
  1460. if (ftrace_match_record(rec, search, search_len, type)) {
  1461. if (not)
  1462. rec->flags &= ~flag;
  1463. else
  1464. rec->flags |= flag;
  1465. found = 1;
  1466. }
  1467. /*
  1468. * Only enable filtering if we have a function that
  1469. * is filtered on.
  1470. */
  1471. if (enable && (rec->flags & FTRACE_FL_FILTER))
  1472. ftrace_filtered = 1;
  1473. } while_for_each_ftrace_rec();
  1474. mutex_unlock(&ftrace_lock);
  1475. return found;
  1476. }
  1477. static int
  1478. ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
  1479. char *regex, int len, int type)
  1480. {
  1481. char str[KSYM_SYMBOL_LEN];
  1482. char *modname;
  1483. kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
  1484. if (!modname || strcmp(modname, mod))
  1485. return 0;
  1486. /* blank search means to match all funcs in the mod */
  1487. if (len)
  1488. return ftrace_match(str, regex, len, type);
  1489. else
  1490. return 1;
  1491. }
  1492. static int ftrace_match_module_records(char *buff, char *mod, int enable)
  1493. {
  1494. unsigned search_len = 0;
  1495. struct ftrace_page *pg;
  1496. struct dyn_ftrace *rec;
  1497. int type = MATCH_FULL;
  1498. char *search = buff;
  1499. unsigned long flag;
  1500. int not = 0;
  1501. int found = 0;
  1502. flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1503. /* blank or '*' mean the same */
  1504. if (strcmp(buff, "*") == 0)
  1505. buff[0] = 0;
  1506. /* handle the case of 'dont filter this module' */
  1507. if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
  1508. buff[0] = 0;
  1509. not = 1;
  1510. }
  1511. if (strlen(buff)) {
  1512. type = filter_parse_regex(buff, strlen(buff), &search, &not);
  1513. search_len = strlen(search);
  1514. }
  1515. mutex_lock(&ftrace_lock);
  1516. do_for_each_ftrace_rec(pg, rec) {
  1517. if (rec->flags & FTRACE_FL_FAILED)
  1518. continue;
  1519. if (ftrace_match_module_record(rec, mod,
  1520. search, search_len, type)) {
  1521. if (not)
  1522. rec->flags &= ~flag;
  1523. else
  1524. rec->flags |= flag;
  1525. found = 1;
  1526. }
  1527. if (enable && (rec->flags & FTRACE_FL_FILTER))
  1528. ftrace_filtered = 1;
  1529. } while_for_each_ftrace_rec();
  1530. mutex_unlock(&ftrace_lock);
  1531. return found;
  1532. }
  1533. /*
  1534. * We register the module command as a template to show others how
  1535. * to register the a command as well.
  1536. */
  1537. static int
  1538. ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
  1539. {
  1540. char *mod;
  1541. /*
  1542. * cmd == 'mod' because we only registered this func
  1543. * for the 'mod' ftrace_func_command.
  1544. * But if you register one func with multiple commands,
  1545. * you can tell which command was used by the cmd
  1546. * parameter.
  1547. */
  1548. /* we must have a module name */
  1549. if (!param)
  1550. return -EINVAL;
  1551. mod = strsep(&param, ":");
  1552. if (!strlen(mod))
  1553. return -EINVAL;
  1554. if (ftrace_match_module_records(func, mod, enable))
  1555. return 0;
  1556. return -EINVAL;
  1557. }
  1558. static struct ftrace_func_command ftrace_mod_cmd = {
  1559. .name = "mod",
  1560. .func = ftrace_mod_callback,
  1561. };
  1562. static int __init ftrace_mod_cmd_init(void)
  1563. {
  1564. return register_ftrace_command(&ftrace_mod_cmd);
  1565. }
  1566. device_initcall(ftrace_mod_cmd_init);
  1567. static void
  1568. function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
  1569. {
  1570. struct ftrace_func_probe *entry;
  1571. struct hlist_head *hhd;
  1572. struct hlist_node *n;
  1573. unsigned long key;
  1574. key = hash_long(ip, FTRACE_HASH_BITS);
  1575. hhd = &ftrace_func_hash[key];
  1576. if (hlist_empty(hhd))
  1577. return;
  1578. /*
  1579. * Disable preemption for these calls to prevent a RCU grace
  1580. * period. This syncs the hash iteration and freeing of items
  1581. * on the hash. rcu_read_lock is too dangerous here.
  1582. */
  1583. preempt_disable_notrace();
  1584. hlist_for_each_entry_rcu(entry, n, hhd, node) {
  1585. if (entry->ip == ip)
  1586. entry->ops->func(ip, parent_ip, &entry->data);
  1587. }
  1588. preempt_enable_notrace();
  1589. }
  1590. static struct ftrace_ops trace_probe_ops __read_mostly =
  1591. {
  1592. .func = function_trace_probe_call,
  1593. };
  1594. static int ftrace_probe_registered;
  1595. static void __enable_ftrace_function_probe(void)
  1596. {
  1597. int i;
  1598. if (ftrace_probe_registered)
  1599. return;
  1600. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1601. struct hlist_head *hhd = &ftrace_func_hash[i];
  1602. if (hhd->first)
  1603. break;
  1604. }
  1605. /* Nothing registered? */
  1606. if (i == FTRACE_FUNC_HASHSIZE)
  1607. return;
  1608. __register_ftrace_function(&trace_probe_ops);
  1609. ftrace_startup(0);
  1610. ftrace_probe_registered = 1;
  1611. }
  1612. static void __disable_ftrace_function_probe(void)
  1613. {
  1614. int i;
  1615. if (!ftrace_probe_registered)
  1616. return;
  1617. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1618. struct hlist_head *hhd = &ftrace_func_hash[i];
  1619. if (hhd->first)
  1620. return;
  1621. }
  1622. /* no more funcs left */
  1623. __unregister_ftrace_function(&trace_probe_ops);
  1624. ftrace_shutdown(0);
  1625. ftrace_probe_registered = 0;
  1626. }
  1627. static void ftrace_free_entry_rcu(struct rcu_head *rhp)
  1628. {
  1629. struct ftrace_func_probe *entry =
  1630. container_of(rhp, struct ftrace_func_probe, rcu);
  1631. if (entry->ops->free)
  1632. entry->ops->free(&entry->data);
  1633. kfree(entry);
  1634. }
  1635. int
  1636. register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1637. void *data)
  1638. {
  1639. struct ftrace_func_probe *entry;
  1640. struct ftrace_page *pg;
  1641. struct dyn_ftrace *rec;
  1642. int type, len, not;
  1643. unsigned long key;
  1644. int count = 0;
  1645. char *search;
  1646. type = filter_parse_regex(glob, strlen(glob), &search, &not);
  1647. len = strlen(search);
  1648. /* we do not support '!' for function probes */
  1649. if (WARN_ON(not))
  1650. return -EINVAL;
  1651. mutex_lock(&ftrace_lock);
  1652. do_for_each_ftrace_rec(pg, rec) {
  1653. if (rec->flags & FTRACE_FL_FAILED)
  1654. continue;
  1655. if (!ftrace_match_record(rec, search, len, type))
  1656. continue;
  1657. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  1658. if (!entry) {
  1659. /* If we did not process any, then return error */
  1660. if (!count)
  1661. count = -ENOMEM;
  1662. goto out_unlock;
  1663. }
  1664. count++;
  1665. entry->data = data;
  1666. /*
  1667. * The caller might want to do something special
  1668. * for each function we find. We call the callback
  1669. * to give the caller an opportunity to do so.
  1670. */
  1671. if (ops->callback) {
  1672. if (ops->callback(rec->ip, &entry->data) < 0) {
  1673. /* caller does not like this func */
  1674. kfree(entry);
  1675. continue;
  1676. }
  1677. }
  1678. entry->ops = ops;
  1679. entry->ip = rec->ip;
  1680. key = hash_long(entry->ip, FTRACE_HASH_BITS);
  1681. hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
  1682. } while_for_each_ftrace_rec();
  1683. __enable_ftrace_function_probe();
  1684. out_unlock:
  1685. mutex_unlock(&ftrace_lock);
  1686. return count;
  1687. }
  1688. enum {
  1689. PROBE_TEST_FUNC = 1,
  1690. PROBE_TEST_DATA = 2
  1691. };
  1692. static void
  1693. __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1694. void *data, int flags)
  1695. {
  1696. struct ftrace_func_probe *entry;
  1697. struct hlist_node *n, *tmp;
  1698. char str[KSYM_SYMBOL_LEN];
  1699. int type = MATCH_FULL;
  1700. int i, len = 0;
  1701. char *search;
  1702. if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
  1703. glob = NULL;
  1704. else if (glob) {
  1705. int not;
  1706. type = filter_parse_regex(glob, strlen(glob), &search, &not);
  1707. len = strlen(search);
  1708. /* we do not support '!' for function probes */
  1709. if (WARN_ON(not))
  1710. return;
  1711. }
  1712. mutex_lock(&ftrace_lock);
  1713. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1714. struct hlist_head *hhd = &ftrace_func_hash[i];
  1715. hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
  1716. /* break up if statements for readability */
  1717. if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
  1718. continue;
  1719. if ((flags & PROBE_TEST_DATA) && entry->data != data)
  1720. continue;
  1721. /* do this last, since it is the most expensive */
  1722. if (glob) {
  1723. kallsyms_lookup(entry->ip, NULL, NULL,
  1724. NULL, str);
  1725. if (!ftrace_match(str, glob, len, type))
  1726. continue;
  1727. }
  1728. hlist_del(&entry->node);
  1729. call_rcu(&entry->rcu, ftrace_free_entry_rcu);
  1730. }
  1731. }
  1732. __disable_ftrace_function_probe();
  1733. mutex_unlock(&ftrace_lock);
  1734. }
  1735. void
  1736. unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1737. void *data)
  1738. {
  1739. __unregister_ftrace_function_probe(glob, ops, data,
  1740. PROBE_TEST_FUNC | PROBE_TEST_DATA);
  1741. }
  1742. void
  1743. unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
  1744. {
  1745. __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
  1746. }
  1747. void unregister_ftrace_function_probe_all(char *glob)
  1748. {
  1749. __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
  1750. }
  1751. static LIST_HEAD(ftrace_commands);
  1752. static DEFINE_MUTEX(ftrace_cmd_mutex);
  1753. int register_ftrace_command(struct ftrace_func_command *cmd)
  1754. {
  1755. struct ftrace_func_command *p;
  1756. int ret = 0;
  1757. mutex_lock(&ftrace_cmd_mutex);
  1758. list_for_each_entry(p, &ftrace_commands, list) {
  1759. if (strcmp(cmd->name, p->name) == 0) {
  1760. ret = -EBUSY;
  1761. goto out_unlock;
  1762. }
  1763. }
  1764. list_add(&cmd->list, &ftrace_commands);
  1765. out_unlock:
  1766. mutex_unlock(&ftrace_cmd_mutex);
  1767. return ret;
  1768. }
  1769. int unregister_ftrace_command(struct ftrace_func_command *cmd)
  1770. {
  1771. struct ftrace_func_command *p, *n;
  1772. int ret = -ENODEV;
  1773. mutex_lock(&ftrace_cmd_mutex);
  1774. list_for_each_entry_safe(p, n, &ftrace_commands, list) {
  1775. if (strcmp(cmd->name, p->name) == 0) {
  1776. ret = 0;
  1777. list_del_init(&p->list);
  1778. goto out_unlock;
  1779. }
  1780. }
  1781. out_unlock:
  1782. mutex_unlock(&ftrace_cmd_mutex);
  1783. return ret;
  1784. }
  1785. static int ftrace_process_regex(char *buff, int len, int enable)
  1786. {
  1787. char *func, *command, *next = buff;
  1788. struct ftrace_func_command *p;
  1789. int ret = -EINVAL;
  1790. func = strsep(&next, ":");
  1791. if (!next) {
  1792. if (ftrace_match_records(func, len, enable))
  1793. return 0;
  1794. return ret;
  1795. }
  1796. /* command found */
  1797. command = strsep(&next, ":");
  1798. mutex_lock(&ftrace_cmd_mutex);
  1799. list_for_each_entry(p, &ftrace_commands, list) {
  1800. if (strcmp(p->name, command) == 0) {
  1801. ret = p->func(func, command, next, enable);
  1802. goto out_unlock;
  1803. }
  1804. }
  1805. out_unlock:
  1806. mutex_unlock(&ftrace_cmd_mutex);
  1807. return ret;
  1808. }
  1809. static ssize_t
  1810. ftrace_regex_write(struct file *file, const char __user *ubuf,
  1811. size_t cnt, loff_t *ppos, int enable)
  1812. {
  1813. struct ftrace_iterator *iter;
  1814. struct trace_parser *parser;
  1815. ssize_t ret, read;
  1816. if (!cnt)
  1817. return 0;
  1818. mutex_lock(&ftrace_regex_lock);
  1819. if (file->f_mode & FMODE_READ) {
  1820. struct seq_file *m = file->private_data;
  1821. iter = m->private;
  1822. } else
  1823. iter = file->private_data;
  1824. parser = &iter->parser;
  1825. read = trace_get_user(parser, ubuf, cnt, ppos);
  1826. if (read >= 0 && trace_parser_loaded(parser) &&
  1827. !trace_parser_cont(parser)) {
  1828. ret = ftrace_process_regex(parser->buffer,
  1829. parser->idx, enable);
  1830. trace_parser_clear(parser);
  1831. if (ret)
  1832. goto out_unlock;
  1833. }
  1834. ret = read;
  1835. out_unlock:
  1836. mutex_unlock(&ftrace_regex_lock);
  1837. return ret;
  1838. }
  1839. static ssize_t
  1840. ftrace_filter_write(struct file *file, const char __user *ubuf,
  1841. size_t cnt, loff_t *ppos)
  1842. {
  1843. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  1844. }
  1845. static ssize_t
  1846. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  1847. size_t cnt, loff_t *ppos)
  1848. {
  1849. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  1850. }
  1851. static void
  1852. ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
  1853. {
  1854. if (unlikely(ftrace_disabled))
  1855. return;
  1856. mutex_lock(&ftrace_regex_lock);
  1857. if (reset)
  1858. ftrace_filter_reset(enable);
  1859. if (buf)
  1860. ftrace_match_records(buf, len, enable);
  1861. mutex_unlock(&ftrace_regex_lock);
  1862. }
  1863. /**
  1864. * ftrace_set_filter - set a function to filter on in ftrace
  1865. * @buf - the string that holds the function filter text.
  1866. * @len - the length of the string.
  1867. * @reset - non zero to reset all filters before applying this filter.
  1868. *
  1869. * Filters denote which functions should be enabled when tracing is enabled.
  1870. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  1871. */
  1872. void ftrace_set_filter(unsigned char *buf, int len, int reset)
  1873. {
  1874. ftrace_set_regex(buf, len, reset, 1);
  1875. }
  1876. /**
  1877. * ftrace_set_notrace - set a function to not trace in ftrace
  1878. * @buf - the string that holds the function notrace text.
  1879. * @len - the length of the string.
  1880. * @reset - non zero to reset all filters before applying this filter.
  1881. *
  1882. * Notrace Filters denote which functions should not be enabled when tracing
  1883. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  1884. * for tracing.
  1885. */
  1886. void ftrace_set_notrace(unsigned char *buf, int len, int reset)
  1887. {
  1888. ftrace_set_regex(buf, len, reset, 0);
  1889. }
  1890. /*
  1891. * command line interface to allow users to set filters on boot up.
  1892. */
  1893. #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
  1894. static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
  1895. static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
  1896. static int __init set_ftrace_notrace(char *str)
  1897. {
  1898. strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
  1899. return 1;
  1900. }
  1901. __setup("ftrace_notrace=", set_ftrace_notrace);
  1902. static int __init set_ftrace_filter(char *str)
  1903. {
  1904. strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
  1905. return 1;
  1906. }
  1907. __setup("ftrace_filter=", set_ftrace_filter);
  1908. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1909. static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
  1910. static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
  1911. static int __init set_graph_function(char *str)
  1912. {
  1913. strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
  1914. return 1;
  1915. }
  1916. __setup("ftrace_graph_filter=", set_graph_function);
  1917. static void __init set_ftrace_early_graph(char *buf)
  1918. {
  1919. int ret;
  1920. char *func;
  1921. while (buf) {
  1922. func = strsep(&buf, ",");
  1923. /* we allow only one expression at a time */
  1924. ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
  1925. func);
  1926. if (ret)
  1927. printk(KERN_DEBUG "ftrace: function %s not "
  1928. "traceable\n", func);
  1929. }
  1930. }
  1931. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1932. static void __init set_ftrace_early_filter(char *buf, int enable)
  1933. {
  1934. char *func;
  1935. while (buf) {
  1936. func = strsep(&buf, ",");
  1937. ftrace_set_regex(func, strlen(func), 0, enable);
  1938. }
  1939. }
  1940. static void __init set_ftrace_early_filters(void)
  1941. {
  1942. if (ftrace_filter_buf[0])
  1943. set_ftrace_early_filter(ftrace_filter_buf, 1);
  1944. if (ftrace_notrace_buf[0])
  1945. set_ftrace_early_filter(ftrace_notrace_buf, 0);
  1946. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1947. if (ftrace_graph_buf[0])
  1948. set_ftrace_early_graph(ftrace_graph_buf);
  1949. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1950. }
  1951. static int
  1952. ftrace_regex_release(struct inode *inode, struct file *file, int enable)
  1953. {
  1954. struct seq_file *m = (struct seq_file *)file->private_data;
  1955. struct ftrace_iterator *iter;
  1956. struct trace_parser *parser;
  1957. mutex_lock(&ftrace_regex_lock);
  1958. if (file->f_mode & FMODE_READ) {
  1959. iter = m->private;
  1960. seq_release(inode, file);
  1961. } else
  1962. iter = file->private_data;
  1963. parser = &iter->parser;
  1964. if (trace_parser_loaded(parser)) {
  1965. parser->buffer[parser->idx] = 0;
  1966. ftrace_match_records(parser->buffer, parser->idx, enable);
  1967. }
  1968. mutex_lock(&ftrace_lock);
  1969. if (ftrace_start_up && ftrace_enabled)
  1970. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  1971. mutex_unlock(&ftrace_lock);
  1972. trace_parser_put(parser);
  1973. kfree(iter);
  1974. mutex_unlock(&ftrace_regex_lock);
  1975. return 0;
  1976. }
  1977. static int
  1978. ftrace_filter_release(struct inode *inode, struct file *file)
  1979. {
  1980. return ftrace_regex_release(inode, file, 1);
  1981. }
  1982. static int
  1983. ftrace_notrace_release(struct inode *inode, struct file *file)
  1984. {
  1985. return ftrace_regex_release(inode, file, 0);
  1986. }
  1987. static const struct file_operations ftrace_avail_fops = {
  1988. .open = ftrace_avail_open,
  1989. .read = seq_read,
  1990. .llseek = seq_lseek,
  1991. .release = seq_release_private,
  1992. };
  1993. static const struct file_operations ftrace_failures_fops = {
  1994. .open = ftrace_failures_open,
  1995. .read = seq_read,
  1996. .llseek = seq_lseek,
  1997. .release = seq_release_private,
  1998. };
  1999. static const struct file_operations ftrace_filter_fops = {
  2000. .open = ftrace_filter_open,
  2001. .read = seq_read,
  2002. .write = ftrace_filter_write,
  2003. .llseek = ftrace_regex_lseek,
  2004. .release = ftrace_filter_release,
  2005. };
  2006. static const struct file_operations ftrace_notrace_fops = {
  2007. .open = ftrace_notrace_open,
  2008. .read = seq_read,
  2009. .write = ftrace_notrace_write,
  2010. .llseek = ftrace_regex_lseek,
  2011. .release = ftrace_notrace_release,
  2012. };
  2013. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2014. static DEFINE_MUTEX(graph_lock);
  2015. int ftrace_graph_count;
  2016. int ftrace_graph_filter_enabled;
  2017. unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
  2018. static void *
  2019. __g_next(struct seq_file *m, loff_t *pos)
  2020. {
  2021. if (*pos >= ftrace_graph_count)
  2022. return NULL;
  2023. return &ftrace_graph_funcs[*pos];
  2024. }
  2025. static void *
  2026. g_next(struct seq_file *m, void *v, loff_t *pos)
  2027. {
  2028. (*pos)++;
  2029. return __g_next(m, pos);
  2030. }
  2031. static void *g_start(struct seq_file *m, loff_t *pos)
  2032. {
  2033. mutex_lock(&graph_lock);
  2034. /* Nothing, tell g_show to print all functions are enabled */
  2035. if (!ftrace_graph_filter_enabled && !*pos)
  2036. return (void *)1;
  2037. return __g_next(m, pos);
  2038. }
  2039. static void g_stop(struct seq_file *m, void *p)
  2040. {
  2041. mutex_unlock(&graph_lock);
  2042. }
  2043. static int g_show(struct seq_file *m, void *v)
  2044. {
  2045. unsigned long *ptr = v;
  2046. if (!ptr)
  2047. return 0;
  2048. if (ptr == (unsigned long *)1) {
  2049. seq_printf(m, "#### all functions enabled ####\n");
  2050. return 0;
  2051. }
  2052. seq_printf(m, "%ps\n", (void *)*ptr);
  2053. return 0;
  2054. }
  2055. static const struct seq_operations ftrace_graph_seq_ops = {
  2056. .start = g_start,
  2057. .next = g_next,
  2058. .stop = g_stop,
  2059. .show = g_show,
  2060. };
  2061. static int
  2062. ftrace_graph_open(struct inode *inode, struct file *file)
  2063. {
  2064. int ret = 0;
  2065. if (unlikely(ftrace_disabled))
  2066. return -ENODEV;
  2067. mutex_lock(&graph_lock);
  2068. if ((file->f_mode & FMODE_WRITE) &&
  2069. (file->f_flags & O_TRUNC)) {
  2070. ftrace_graph_filter_enabled = 0;
  2071. ftrace_graph_count = 0;
  2072. memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
  2073. }
  2074. mutex_unlock(&graph_lock);
  2075. if (file->f_mode & FMODE_READ)
  2076. ret = seq_open(file, &ftrace_graph_seq_ops);
  2077. return ret;
  2078. }
  2079. static int
  2080. ftrace_graph_release(struct inode *inode, struct file *file)
  2081. {
  2082. if (file->f_mode & FMODE_READ)
  2083. seq_release(inode, file);
  2084. return 0;
  2085. }
  2086. static int
  2087. ftrace_set_func(unsigned long *array, int *idx, char *buffer)
  2088. {
  2089. struct dyn_ftrace *rec;
  2090. struct ftrace_page *pg;
  2091. int search_len;
  2092. int fail = 1;
  2093. int type, not;
  2094. char *search;
  2095. bool exists;
  2096. int i;
  2097. if (ftrace_disabled)
  2098. return -ENODEV;
  2099. /* decode regex */
  2100. type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
  2101. if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
  2102. return -EBUSY;
  2103. search_len = strlen(search);
  2104. mutex_lock(&ftrace_lock);
  2105. do_for_each_ftrace_rec(pg, rec) {
  2106. if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
  2107. continue;
  2108. if (ftrace_match_record(rec, search, search_len, type)) {
  2109. /* if it is in the array */
  2110. exists = false;
  2111. for (i = 0; i < *idx; i++) {
  2112. if (array[i] == rec->ip) {
  2113. exists = true;
  2114. break;
  2115. }
  2116. }
  2117. if (!not) {
  2118. fail = 0;
  2119. if (!exists) {
  2120. array[(*idx)++] = rec->ip;
  2121. if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
  2122. goto out;
  2123. }
  2124. } else {
  2125. if (exists) {
  2126. array[i] = array[--(*idx)];
  2127. array[*idx] = 0;
  2128. fail = 0;
  2129. }
  2130. }
  2131. }
  2132. } while_for_each_ftrace_rec();
  2133. out:
  2134. mutex_unlock(&ftrace_lock);
  2135. if (fail)
  2136. return -EINVAL;
  2137. ftrace_graph_filter_enabled = 1;
  2138. return 0;
  2139. }
  2140. static ssize_t
  2141. ftrace_graph_write(struct file *file, const char __user *ubuf,
  2142. size_t cnt, loff_t *ppos)
  2143. {
  2144. struct trace_parser parser;
  2145. ssize_t read, ret;
  2146. if (!cnt)
  2147. return 0;
  2148. mutex_lock(&graph_lock);
  2149. if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
  2150. ret = -ENOMEM;
  2151. goto out_unlock;
  2152. }
  2153. read = trace_get_user(&parser, ubuf, cnt, ppos);
  2154. if (read >= 0 && trace_parser_loaded((&parser))) {
  2155. parser.buffer[parser.idx] = 0;
  2156. /* we allow only one expression at a time */
  2157. ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
  2158. parser.buffer);
  2159. if (ret)
  2160. goto out_free;
  2161. }
  2162. ret = read;
  2163. out_free:
  2164. trace_parser_put(&parser);
  2165. out_unlock:
  2166. mutex_unlock(&graph_lock);
  2167. return ret;
  2168. }
  2169. static const struct file_operations ftrace_graph_fops = {
  2170. .open = ftrace_graph_open,
  2171. .read = seq_read,
  2172. .write = ftrace_graph_write,
  2173. .release = ftrace_graph_release,
  2174. .llseek = seq_lseek,
  2175. };
  2176. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2177. static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
  2178. {
  2179. trace_create_file("available_filter_functions", 0444,
  2180. d_tracer, NULL, &ftrace_avail_fops);
  2181. trace_create_file("failures", 0444,
  2182. d_tracer, NULL, &ftrace_failures_fops);
  2183. trace_create_file("set_ftrace_filter", 0644, d_tracer,
  2184. NULL, &ftrace_filter_fops);
  2185. trace_create_file("set_ftrace_notrace", 0644, d_tracer,
  2186. NULL, &ftrace_notrace_fops);
  2187. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2188. trace_create_file("set_graph_function", 0444, d_tracer,
  2189. NULL,
  2190. &ftrace_graph_fops);
  2191. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2192. return 0;
  2193. }
  2194. static int ftrace_process_locs(struct module *mod,
  2195. unsigned long *start,
  2196. unsigned long *end)
  2197. {
  2198. unsigned long *p;
  2199. unsigned long addr;
  2200. mutex_lock(&ftrace_lock);
  2201. p = start;
  2202. while (p < end) {
  2203. addr = ftrace_call_adjust(*p++);
  2204. /*
  2205. * Some architecture linkers will pad between
  2206. * the different mcount_loc sections of different
  2207. * object files to satisfy alignments.
  2208. * Skip any NULL pointers.
  2209. */
  2210. if (!addr)
  2211. continue;
  2212. ftrace_record_ip(addr);
  2213. }
  2214. ftrace_update_code(mod);
  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 visible 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