ftrace.c 72 KB

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