ftrace.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419
  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_ENABLE_MCOUNT = (1 << 3),
  730. FTRACE_DISABLE_MCOUNT = (1 << 4),
  731. FTRACE_START_FUNC_RET = (1 << 5),
  732. FTRACE_STOP_FUNC_RET = (1 << 6),
  733. };
  734. static int ftrace_filtered;
  735. static struct dyn_ftrace *ftrace_new_addrs;
  736. static DEFINE_MUTEX(ftrace_regex_lock);
  737. struct ftrace_page {
  738. struct ftrace_page *next;
  739. int index;
  740. struct dyn_ftrace records[];
  741. };
  742. #define ENTRIES_PER_PAGE \
  743. ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
  744. /* estimate from running different kernels */
  745. #define NR_TO_INIT 10000
  746. static struct ftrace_page *ftrace_pages_start;
  747. static struct ftrace_page *ftrace_pages;
  748. static struct dyn_ftrace *ftrace_free_records;
  749. /*
  750. * This is a double for. Do not use 'break' to break out of the loop,
  751. * you must use a goto.
  752. */
  753. #define do_for_each_ftrace_rec(pg, rec) \
  754. for (pg = ftrace_pages_start; pg; pg = pg->next) { \
  755. int _____i; \
  756. for (_____i = 0; _____i < pg->index; _____i++) { \
  757. rec = &pg->records[_____i];
  758. #define while_for_each_ftrace_rec() \
  759. } \
  760. }
  761. static void ftrace_free_rec(struct dyn_ftrace *rec)
  762. {
  763. rec->freelist = ftrace_free_records;
  764. ftrace_free_records = rec;
  765. rec->flags |= FTRACE_FL_FREE;
  766. }
  767. static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
  768. {
  769. struct dyn_ftrace *rec;
  770. /* First check for freed records */
  771. if (ftrace_free_records) {
  772. rec = ftrace_free_records;
  773. if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
  774. FTRACE_WARN_ON_ONCE(1);
  775. ftrace_free_records = NULL;
  776. return NULL;
  777. }
  778. ftrace_free_records = rec->freelist;
  779. memset(rec, 0, sizeof(*rec));
  780. return rec;
  781. }
  782. if (ftrace_pages->index == ENTRIES_PER_PAGE) {
  783. if (!ftrace_pages->next) {
  784. /* allocate another page */
  785. ftrace_pages->next =
  786. (void *)get_zeroed_page(GFP_KERNEL);
  787. if (!ftrace_pages->next)
  788. return NULL;
  789. }
  790. ftrace_pages = ftrace_pages->next;
  791. }
  792. return &ftrace_pages->records[ftrace_pages->index++];
  793. }
  794. static struct dyn_ftrace *
  795. ftrace_record_ip(unsigned long ip)
  796. {
  797. struct dyn_ftrace *rec;
  798. if (ftrace_disabled)
  799. return NULL;
  800. rec = ftrace_alloc_dyn_node(ip);
  801. if (!rec)
  802. return NULL;
  803. rec->ip = ip;
  804. rec->newlist = ftrace_new_addrs;
  805. ftrace_new_addrs = rec;
  806. return rec;
  807. }
  808. static void print_ip_ins(const char *fmt, unsigned char *p)
  809. {
  810. int i;
  811. printk(KERN_CONT "%s", fmt);
  812. for (i = 0; i < MCOUNT_INSN_SIZE; i++)
  813. printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
  814. }
  815. static void ftrace_bug(int failed, unsigned long ip)
  816. {
  817. switch (failed) {
  818. case -EFAULT:
  819. FTRACE_WARN_ON_ONCE(1);
  820. pr_info("ftrace faulted on modifying ");
  821. print_ip_sym(ip);
  822. break;
  823. case -EINVAL:
  824. FTRACE_WARN_ON_ONCE(1);
  825. pr_info("ftrace failed to modify ");
  826. print_ip_sym(ip);
  827. print_ip_ins(" actual: ", (unsigned char *)ip);
  828. printk(KERN_CONT "\n");
  829. break;
  830. case -EPERM:
  831. FTRACE_WARN_ON_ONCE(1);
  832. pr_info("ftrace faulted on writing ");
  833. print_ip_sym(ip);
  834. break;
  835. default:
  836. FTRACE_WARN_ON_ONCE(1);
  837. pr_info("ftrace faulted on unknown error ");
  838. print_ip_sym(ip);
  839. }
  840. }
  841. /* Return 1 if the address range is reserved for ftrace */
  842. int ftrace_text_reserved(void *start, void *end)
  843. {
  844. struct dyn_ftrace *rec;
  845. struct ftrace_page *pg;
  846. do_for_each_ftrace_rec(pg, rec) {
  847. if (rec->ip <= (unsigned long)end &&
  848. rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
  849. return 1;
  850. } while_for_each_ftrace_rec();
  851. return 0;
  852. }
  853. static int
  854. __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
  855. {
  856. unsigned long ftrace_addr;
  857. unsigned long flag = 0UL;
  858. ftrace_addr = (unsigned long)FTRACE_ADDR;
  859. /*
  860. * If this record is not to be traced or we want to disable it,
  861. * then disable it.
  862. *
  863. * If we want to enable it and filtering is off, then enable it.
  864. *
  865. * If we want to enable it and filtering is on, enable it only if
  866. * it's filtered
  867. */
  868. if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
  869. if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
  870. flag = FTRACE_FL_ENABLED;
  871. }
  872. /* If the state of this record hasn't changed, then do nothing */
  873. if ((rec->flags & FTRACE_FL_ENABLED) == flag)
  874. return 0;
  875. if (flag) {
  876. rec->flags |= FTRACE_FL_ENABLED;
  877. return ftrace_make_call(rec, ftrace_addr);
  878. }
  879. rec->flags &= ~FTRACE_FL_ENABLED;
  880. return ftrace_make_nop(NULL, rec, ftrace_addr);
  881. }
  882. static void ftrace_replace_code(int enable)
  883. {
  884. struct dyn_ftrace *rec;
  885. struct ftrace_page *pg;
  886. int failed;
  887. do_for_each_ftrace_rec(pg, rec) {
  888. /*
  889. * Skip over free records, records that have
  890. * failed and not converted.
  891. */
  892. if (rec->flags & FTRACE_FL_FREE ||
  893. rec->flags & FTRACE_FL_FAILED ||
  894. !(rec->flags & FTRACE_FL_CONVERTED))
  895. continue;
  896. failed = __ftrace_replace_code(rec, enable);
  897. if (failed) {
  898. rec->flags |= FTRACE_FL_FAILED;
  899. ftrace_bug(failed, rec->ip);
  900. /* Stop processing */
  901. return;
  902. }
  903. } while_for_each_ftrace_rec();
  904. }
  905. static int
  906. ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
  907. {
  908. unsigned long ip;
  909. int ret;
  910. ip = rec->ip;
  911. ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
  912. if (ret) {
  913. ftrace_bug(ret, ip);
  914. rec->flags |= FTRACE_FL_FAILED;
  915. return 0;
  916. }
  917. return 1;
  918. }
  919. /*
  920. * archs can override this function if they must do something
  921. * before the modifying code is performed.
  922. */
  923. int __weak ftrace_arch_code_modify_prepare(void)
  924. {
  925. return 0;
  926. }
  927. /*
  928. * archs can override this function if they must do something
  929. * after the modifying code is performed.
  930. */
  931. int __weak ftrace_arch_code_modify_post_process(void)
  932. {
  933. return 0;
  934. }
  935. static int __ftrace_modify_code(void *data)
  936. {
  937. int *command = data;
  938. if (*command & FTRACE_ENABLE_CALLS)
  939. ftrace_replace_code(1);
  940. else if (*command & FTRACE_DISABLE_CALLS)
  941. ftrace_replace_code(0);
  942. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  943. ftrace_update_ftrace_func(ftrace_trace_function);
  944. if (*command & FTRACE_START_FUNC_RET)
  945. ftrace_enable_ftrace_graph_caller();
  946. else if (*command & FTRACE_STOP_FUNC_RET)
  947. ftrace_disable_ftrace_graph_caller();
  948. return 0;
  949. }
  950. static void ftrace_run_update_code(int command)
  951. {
  952. int ret;
  953. ret = ftrace_arch_code_modify_prepare();
  954. FTRACE_WARN_ON(ret);
  955. if (ret)
  956. return;
  957. stop_machine(__ftrace_modify_code, &command, NULL);
  958. ret = ftrace_arch_code_modify_post_process();
  959. FTRACE_WARN_ON(ret);
  960. }
  961. static ftrace_func_t saved_ftrace_func;
  962. static int ftrace_start_up;
  963. static void ftrace_startup_enable(int command)
  964. {
  965. if (saved_ftrace_func != ftrace_trace_function) {
  966. saved_ftrace_func = ftrace_trace_function;
  967. command |= FTRACE_UPDATE_TRACE_FUNC;
  968. }
  969. if (!command || !ftrace_enabled)
  970. return;
  971. ftrace_run_update_code(command);
  972. }
  973. static void ftrace_startup(int command)
  974. {
  975. if (unlikely(ftrace_disabled))
  976. return;
  977. ftrace_start_up++;
  978. command |= FTRACE_ENABLE_CALLS;
  979. ftrace_startup_enable(command);
  980. }
  981. static void ftrace_shutdown(int command)
  982. {
  983. if (unlikely(ftrace_disabled))
  984. return;
  985. ftrace_start_up--;
  986. /*
  987. * Just warn in case of unbalance, no need to kill ftrace, it's not
  988. * critical but the ftrace_call callers may be never nopped again after
  989. * further ftrace uses.
  990. */
  991. WARN_ON_ONCE(ftrace_start_up < 0);
  992. if (!ftrace_start_up)
  993. command |= FTRACE_DISABLE_CALLS;
  994. if (saved_ftrace_func != ftrace_trace_function) {
  995. saved_ftrace_func = ftrace_trace_function;
  996. command |= FTRACE_UPDATE_TRACE_FUNC;
  997. }
  998. if (!command || !ftrace_enabled)
  999. return;
  1000. ftrace_run_update_code(command);
  1001. }
  1002. static void ftrace_startup_sysctl(void)
  1003. {
  1004. int command = FTRACE_ENABLE_MCOUNT;
  1005. if (unlikely(ftrace_disabled))
  1006. return;
  1007. /* Force update next time */
  1008. saved_ftrace_func = NULL;
  1009. /* ftrace_start_up is true if we want ftrace running */
  1010. if (ftrace_start_up)
  1011. command |= FTRACE_ENABLE_CALLS;
  1012. ftrace_run_update_code(command);
  1013. }
  1014. static void ftrace_shutdown_sysctl(void)
  1015. {
  1016. int command = FTRACE_DISABLE_MCOUNT;
  1017. if (unlikely(ftrace_disabled))
  1018. return;
  1019. /* ftrace_start_up is true if ftrace is running */
  1020. if (ftrace_start_up)
  1021. command |= FTRACE_DISABLE_CALLS;
  1022. ftrace_run_update_code(command);
  1023. }
  1024. static cycle_t ftrace_update_time;
  1025. static unsigned long ftrace_update_cnt;
  1026. unsigned long ftrace_update_tot_cnt;
  1027. static int ftrace_update_code(struct module *mod)
  1028. {
  1029. struct dyn_ftrace *p;
  1030. cycle_t start, stop;
  1031. start = ftrace_now(raw_smp_processor_id());
  1032. ftrace_update_cnt = 0;
  1033. while (ftrace_new_addrs) {
  1034. /* If something went wrong, bail without enabling anything */
  1035. if (unlikely(ftrace_disabled))
  1036. return -1;
  1037. p = ftrace_new_addrs;
  1038. ftrace_new_addrs = p->newlist;
  1039. p->flags = 0L;
  1040. /*
  1041. * Do the initial record convertion from mcount jump
  1042. * to the NOP instructions.
  1043. */
  1044. if (!ftrace_code_disable(mod, p)) {
  1045. ftrace_free_rec(p);
  1046. continue;
  1047. }
  1048. p->flags |= FTRACE_FL_CONVERTED;
  1049. ftrace_update_cnt++;
  1050. /*
  1051. * If the tracing is enabled, go ahead and enable the record.
  1052. *
  1053. * The reason not to enable the record immediatelly is the
  1054. * inherent check of ftrace_make_nop/ftrace_make_call for
  1055. * correct previous instructions. Making first the NOP
  1056. * conversion puts the module to the correct state, thus
  1057. * passing the ftrace_make_call check.
  1058. */
  1059. if (ftrace_start_up) {
  1060. int failed = __ftrace_replace_code(p, 1);
  1061. if (failed) {
  1062. ftrace_bug(failed, p->ip);
  1063. ftrace_free_rec(p);
  1064. }
  1065. }
  1066. }
  1067. stop = ftrace_now(raw_smp_processor_id());
  1068. ftrace_update_time = stop - start;
  1069. ftrace_update_tot_cnt += ftrace_update_cnt;
  1070. return 0;
  1071. }
  1072. static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
  1073. {
  1074. struct ftrace_page *pg;
  1075. int cnt;
  1076. int i;
  1077. /* allocate a few pages */
  1078. ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
  1079. if (!ftrace_pages_start)
  1080. return -1;
  1081. /*
  1082. * Allocate a few more pages.
  1083. *
  1084. * TODO: have some parser search vmlinux before
  1085. * final linking to find all calls to ftrace.
  1086. * Then we can:
  1087. * a) know how many pages to allocate.
  1088. * and/or
  1089. * b) set up the table then.
  1090. *
  1091. * The dynamic code is still necessary for
  1092. * modules.
  1093. */
  1094. pg = ftrace_pages = ftrace_pages_start;
  1095. cnt = num_to_init / ENTRIES_PER_PAGE;
  1096. pr_info("ftrace: allocating %ld entries in %d pages\n",
  1097. num_to_init, cnt + 1);
  1098. for (i = 0; i < cnt; i++) {
  1099. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  1100. /* If we fail, we'll try later anyway */
  1101. if (!pg->next)
  1102. break;
  1103. pg = pg->next;
  1104. }
  1105. return 0;
  1106. }
  1107. enum {
  1108. FTRACE_ITER_FILTER = (1 << 0),
  1109. FTRACE_ITER_NOTRACE = (1 << 1),
  1110. FTRACE_ITER_FAILURES = (1 << 2),
  1111. FTRACE_ITER_PRINTALL = (1 << 3),
  1112. FTRACE_ITER_HASH = (1 << 4),
  1113. };
  1114. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  1115. struct ftrace_iterator {
  1116. struct ftrace_page *pg;
  1117. int hidx;
  1118. int idx;
  1119. unsigned flags;
  1120. struct trace_parser parser;
  1121. };
  1122. static void *
  1123. t_hash_next(struct seq_file *m, void *v, loff_t *pos)
  1124. {
  1125. struct ftrace_iterator *iter = m->private;
  1126. struct hlist_node *hnd = v;
  1127. struct hlist_head *hhd;
  1128. WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
  1129. (*pos)++;
  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. return hnd;
  1149. }
  1150. static void *t_hash_start(struct seq_file *m, loff_t *pos)
  1151. {
  1152. struct ftrace_iterator *iter = m->private;
  1153. void *p = NULL;
  1154. loff_t l;
  1155. if (!(iter->flags & FTRACE_ITER_HASH))
  1156. *pos = 0;
  1157. iter->flags |= FTRACE_ITER_HASH;
  1158. iter->hidx = 0;
  1159. for (l = 0; l <= *pos; ) {
  1160. p = t_hash_next(m, p, &l);
  1161. if (!p)
  1162. break;
  1163. }
  1164. return p;
  1165. }
  1166. static int t_hash_show(struct seq_file *m, void *v)
  1167. {
  1168. struct ftrace_func_probe *rec;
  1169. struct hlist_node *hnd = v;
  1170. rec = hlist_entry(hnd, struct ftrace_func_probe, node);
  1171. if (rec->ops->print)
  1172. return rec->ops->print(m, rec->ip, rec->ops, rec->data);
  1173. seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
  1174. if (rec->data)
  1175. seq_printf(m, ":%p", rec->data);
  1176. seq_putc(m, '\n');
  1177. return 0;
  1178. }
  1179. static void *
  1180. t_next(struct seq_file *m, void *v, loff_t *pos)
  1181. {
  1182. struct ftrace_iterator *iter = m->private;
  1183. struct dyn_ftrace *rec = NULL;
  1184. if (iter->flags & FTRACE_ITER_HASH)
  1185. return t_hash_next(m, v, pos);
  1186. (*pos)++;
  1187. if (iter->flags & FTRACE_ITER_PRINTALL)
  1188. return NULL;
  1189. retry:
  1190. if (iter->idx >= iter->pg->index) {
  1191. if (iter->pg->next) {
  1192. iter->pg = iter->pg->next;
  1193. iter->idx = 0;
  1194. goto retry;
  1195. }
  1196. } else {
  1197. rec = &iter->pg->records[iter->idx++];
  1198. if ((rec->flags & FTRACE_FL_FREE) ||
  1199. (!(iter->flags & FTRACE_ITER_FAILURES) &&
  1200. (rec->flags & FTRACE_FL_FAILED)) ||
  1201. ((iter->flags & FTRACE_ITER_FAILURES) &&
  1202. !(rec->flags & FTRACE_FL_FAILED)) ||
  1203. ((iter->flags & FTRACE_ITER_FILTER) &&
  1204. !(rec->flags & FTRACE_FL_FILTER)) ||
  1205. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  1206. !(rec->flags & FTRACE_FL_NOTRACE))) {
  1207. rec = NULL;
  1208. goto retry;
  1209. }
  1210. }
  1211. return rec;
  1212. }
  1213. static void *t_start(struct seq_file *m, loff_t *pos)
  1214. {
  1215. struct ftrace_iterator *iter = m->private;
  1216. void *p = NULL;
  1217. loff_t l;
  1218. mutex_lock(&ftrace_lock);
  1219. /*
  1220. * For set_ftrace_filter reading, if we have the filter
  1221. * off, we can short cut and just print out that all
  1222. * functions are enabled.
  1223. */
  1224. if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
  1225. if (*pos > 0)
  1226. return t_hash_start(m, pos);
  1227. iter->flags |= FTRACE_ITER_PRINTALL;
  1228. return iter;
  1229. }
  1230. if (iter->flags & FTRACE_ITER_HASH)
  1231. return t_hash_start(m, pos);
  1232. iter->pg = ftrace_pages_start;
  1233. iter->idx = 0;
  1234. for (l = 0; l <= *pos; ) {
  1235. p = t_next(m, p, &l);
  1236. if (!p)
  1237. break;
  1238. }
  1239. if (!p && iter->flags & FTRACE_ITER_FILTER)
  1240. return t_hash_start(m, pos);
  1241. return p;
  1242. }
  1243. static void t_stop(struct seq_file *m, void *p)
  1244. {
  1245. mutex_unlock(&ftrace_lock);
  1246. }
  1247. static int t_show(struct seq_file *m, void *v)
  1248. {
  1249. struct ftrace_iterator *iter = m->private;
  1250. struct dyn_ftrace *rec = v;
  1251. if (iter->flags & FTRACE_ITER_HASH)
  1252. return t_hash_show(m, v);
  1253. if (iter->flags & FTRACE_ITER_PRINTALL) {
  1254. seq_printf(m, "#### all functions enabled ####\n");
  1255. return 0;
  1256. }
  1257. if (!rec)
  1258. return 0;
  1259. seq_printf(m, "%ps\n", (void *)rec->ip);
  1260. return 0;
  1261. }
  1262. static const struct seq_operations show_ftrace_seq_ops = {
  1263. .start = t_start,
  1264. .next = t_next,
  1265. .stop = t_stop,
  1266. .show = t_show,
  1267. };
  1268. static int
  1269. ftrace_avail_open(struct inode *inode, struct file *file)
  1270. {
  1271. struct ftrace_iterator *iter;
  1272. int ret;
  1273. if (unlikely(ftrace_disabled))
  1274. return -ENODEV;
  1275. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1276. if (!iter)
  1277. return -ENOMEM;
  1278. iter->pg = ftrace_pages_start;
  1279. ret = seq_open(file, &show_ftrace_seq_ops);
  1280. if (!ret) {
  1281. struct seq_file *m = file->private_data;
  1282. m->private = iter;
  1283. } else {
  1284. kfree(iter);
  1285. }
  1286. return ret;
  1287. }
  1288. static int
  1289. ftrace_failures_open(struct inode *inode, struct file *file)
  1290. {
  1291. int ret;
  1292. struct seq_file *m;
  1293. struct ftrace_iterator *iter;
  1294. ret = ftrace_avail_open(inode, file);
  1295. if (!ret) {
  1296. m = (struct seq_file *)file->private_data;
  1297. iter = (struct ftrace_iterator *)m->private;
  1298. iter->flags = FTRACE_ITER_FAILURES;
  1299. }
  1300. return ret;
  1301. }
  1302. static void ftrace_filter_reset(int enable)
  1303. {
  1304. struct ftrace_page *pg;
  1305. struct dyn_ftrace *rec;
  1306. unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1307. mutex_lock(&ftrace_lock);
  1308. if (enable)
  1309. ftrace_filtered = 0;
  1310. do_for_each_ftrace_rec(pg, rec) {
  1311. if (rec->flags & FTRACE_FL_FAILED)
  1312. continue;
  1313. rec->flags &= ~type;
  1314. } while_for_each_ftrace_rec();
  1315. mutex_unlock(&ftrace_lock);
  1316. }
  1317. static int
  1318. ftrace_regex_open(struct inode *inode, struct file *file, int enable)
  1319. {
  1320. struct ftrace_iterator *iter;
  1321. int ret = 0;
  1322. if (unlikely(ftrace_disabled))
  1323. return -ENODEV;
  1324. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1325. if (!iter)
  1326. return -ENOMEM;
  1327. if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
  1328. kfree(iter);
  1329. return -ENOMEM;
  1330. }
  1331. mutex_lock(&ftrace_regex_lock);
  1332. if ((file->f_mode & FMODE_WRITE) &&
  1333. (file->f_flags & O_TRUNC))
  1334. ftrace_filter_reset(enable);
  1335. if (file->f_mode & FMODE_READ) {
  1336. iter->pg = ftrace_pages_start;
  1337. iter->flags = enable ? FTRACE_ITER_FILTER :
  1338. FTRACE_ITER_NOTRACE;
  1339. ret = seq_open(file, &show_ftrace_seq_ops);
  1340. if (!ret) {
  1341. struct seq_file *m = file->private_data;
  1342. m->private = iter;
  1343. } else {
  1344. trace_parser_put(&iter->parser);
  1345. kfree(iter);
  1346. }
  1347. } else
  1348. file->private_data = iter;
  1349. mutex_unlock(&ftrace_regex_lock);
  1350. return ret;
  1351. }
  1352. static int
  1353. ftrace_filter_open(struct inode *inode, struct file *file)
  1354. {
  1355. return ftrace_regex_open(inode, file, 1);
  1356. }
  1357. static int
  1358. ftrace_notrace_open(struct inode *inode, struct file *file)
  1359. {
  1360. return ftrace_regex_open(inode, file, 0);
  1361. }
  1362. static loff_t
  1363. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  1364. {
  1365. loff_t ret;
  1366. if (file->f_mode & FMODE_READ)
  1367. ret = seq_lseek(file, offset, origin);
  1368. else
  1369. file->f_pos = ret = 1;
  1370. return ret;
  1371. }
  1372. static int ftrace_match(char *str, char *regex, int len, int type)
  1373. {
  1374. int matched = 0;
  1375. int slen;
  1376. switch (type) {
  1377. case MATCH_FULL:
  1378. if (strcmp(str, regex) == 0)
  1379. matched = 1;
  1380. break;
  1381. case MATCH_FRONT_ONLY:
  1382. if (strncmp(str, regex, len) == 0)
  1383. matched = 1;
  1384. break;
  1385. case MATCH_MIDDLE_ONLY:
  1386. if (strstr(str, regex))
  1387. matched = 1;
  1388. break;
  1389. case MATCH_END_ONLY:
  1390. slen = strlen(str);
  1391. if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
  1392. matched = 1;
  1393. break;
  1394. }
  1395. return matched;
  1396. }
  1397. static int
  1398. ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
  1399. {
  1400. char str[KSYM_SYMBOL_LEN];
  1401. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  1402. return ftrace_match(str, regex, len, type);
  1403. }
  1404. static int ftrace_match_records(char *buff, int len, int enable)
  1405. {
  1406. unsigned int search_len;
  1407. struct ftrace_page *pg;
  1408. struct dyn_ftrace *rec;
  1409. unsigned long flag;
  1410. char *search;
  1411. int type;
  1412. int not;
  1413. int found = 0;
  1414. flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1415. type = filter_parse_regex(buff, len, &search, &not);
  1416. search_len = strlen(search);
  1417. mutex_lock(&ftrace_lock);
  1418. do_for_each_ftrace_rec(pg, rec) {
  1419. if (rec->flags & FTRACE_FL_FAILED)
  1420. continue;
  1421. if (ftrace_match_record(rec, search, search_len, type)) {
  1422. if (not)
  1423. rec->flags &= ~flag;
  1424. else
  1425. rec->flags |= flag;
  1426. found = 1;
  1427. }
  1428. /*
  1429. * Only enable filtering if we have a function that
  1430. * is filtered on.
  1431. */
  1432. if (enable && (rec->flags & FTRACE_FL_FILTER))
  1433. ftrace_filtered = 1;
  1434. } while_for_each_ftrace_rec();
  1435. mutex_unlock(&ftrace_lock);
  1436. return found;
  1437. }
  1438. static int
  1439. ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
  1440. char *regex, int len, int type)
  1441. {
  1442. char str[KSYM_SYMBOL_LEN];
  1443. char *modname;
  1444. kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
  1445. if (!modname || strcmp(modname, mod))
  1446. return 0;
  1447. /* blank search means to match all funcs in the mod */
  1448. if (len)
  1449. return ftrace_match(str, regex, len, type);
  1450. else
  1451. return 1;
  1452. }
  1453. static int ftrace_match_module_records(char *buff, char *mod, int enable)
  1454. {
  1455. unsigned search_len = 0;
  1456. struct ftrace_page *pg;
  1457. struct dyn_ftrace *rec;
  1458. int type = MATCH_FULL;
  1459. char *search = buff;
  1460. unsigned long flag;
  1461. int not = 0;
  1462. int found = 0;
  1463. flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1464. /* blank or '*' mean the same */
  1465. if (strcmp(buff, "*") == 0)
  1466. buff[0] = 0;
  1467. /* handle the case of 'dont filter this module' */
  1468. if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
  1469. buff[0] = 0;
  1470. not = 1;
  1471. }
  1472. if (strlen(buff)) {
  1473. type = filter_parse_regex(buff, strlen(buff), &search, &not);
  1474. search_len = strlen(search);
  1475. }
  1476. mutex_lock(&ftrace_lock);
  1477. do_for_each_ftrace_rec(pg, rec) {
  1478. if (rec->flags & FTRACE_FL_FAILED)
  1479. continue;
  1480. if (ftrace_match_module_record(rec, mod,
  1481. search, search_len, type)) {
  1482. if (not)
  1483. rec->flags &= ~flag;
  1484. else
  1485. rec->flags |= flag;
  1486. found = 1;
  1487. }
  1488. if (enable && (rec->flags & FTRACE_FL_FILTER))
  1489. ftrace_filtered = 1;
  1490. } while_for_each_ftrace_rec();
  1491. mutex_unlock(&ftrace_lock);
  1492. return found;
  1493. }
  1494. /*
  1495. * We register the module command as a template to show others how
  1496. * to register the a command as well.
  1497. */
  1498. static int
  1499. ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
  1500. {
  1501. char *mod;
  1502. /*
  1503. * cmd == 'mod' because we only registered this func
  1504. * for the 'mod' ftrace_func_command.
  1505. * But if you register one func with multiple commands,
  1506. * you can tell which command was used by the cmd
  1507. * parameter.
  1508. */
  1509. /* we must have a module name */
  1510. if (!param)
  1511. return -EINVAL;
  1512. mod = strsep(&param, ":");
  1513. if (!strlen(mod))
  1514. return -EINVAL;
  1515. if (ftrace_match_module_records(func, mod, enable))
  1516. return 0;
  1517. return -EINVAL;
  1518. }
  1519. static struct ftrace_func_command ftrace_mod_cmd = {
  1520. .name = "mod",
  1521. .func = ftrace_mod_callback,
  1522. };
  1523. static int __init ftrace_mod_cmd_init(void)
  1524. {
  1525. return register_ftrace_command(&ftrace_mod_cmd);
  1526. }
  1527. device_initcall(ftrace_mod_cmd_init);
  1528. static void
  1529. function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
  1530. {
  1531. struct ftrace_func_probe *entry;
  1532. struct hlist_head *hhd;
  1533. struct hlist_node *n;
  1534. unsigned long key;
  1535. key = hash_long(ip, FTRACE_HASH_BITS);
  1536. hhd = &ftrace_func_hash[key];
  1537. if (hlist_empty(hhd))
  1538. return;
  1539. /*
  1540. * Disable preemption for these calls to prevent a RCU grace
  1541. * period. This syncs the hash iteration and freeing of items
  1542. * on the hash. rcu_read_lock is too dangerous here.
  1543. */
  1544. preempt_disable_notrace();
  1545. hlist_for_each_entry_rcu(entry, n, hhd, node) {
  1546. if (entry->ip == ip)
  1547. entry->ops->func(ip, parent_ip, &entry->data);
  1548. }
  1549. preempt_enable_notrace();
  1550. }
  1551. static struct ftrace_ops trace_probe_ops __read_mostly =
  1552. {
  1553. .func = function_trace_probe_call,
  1554. };
  1555. static int ftrace_probe_registered;
  1556. static void __enable_ftrace_function_probe(void)
  1557. {
  1558. int i;
  1559. if (ftrace_probe_registered)
  1560. return;
  1561. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1562. struct hlist_head *hhd = &ftrace_func_hash[i];
  1563. if (hhd->first)
  1564. break;
  1565. }
  1566. /* Nothing registered? */
  1567. if (i == FTRACE_FUNC_HASHSIZE)
  1568. return;
  1569. __register_ftrace_function(&trace_probe_ops);
  1570. ftrace_startup(0);
  1571. ftrace_probe_registered = 1;
  1572. }
  1573. static void __disable_ftrace_function_probe(void)
  1574. {
  1575. int i;
  1576. if (!ftrace_probe_registered)
  1577. return;
  1578. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1579. struct hlist_head *hhd = &ftrace_func_hash[i];
  1580. if (hhd->first)
  1581. return;
  1582. }
  1583. /* no more funcs left */
  1584. __unregister_ftrace_function(&trace_probe_ops);
  1585. ftrace_shutdown(0);
  1586. ftrace_probe_registered = 0;
  1587. }
  1588. static void ftrace_free_entry_rcu(struct rcu_head *rhp)
  1589. {
  1590. struct ftrace_func_probe *entry =
  1591. container_of(rhp, struct ftrace_func_probe, rcu);
  1592. if (entry->ops->free)
  1593. entry->ops->free(&entry->data);
  1594. kfree(entry);
  1595. }
  1596. int
  1597. register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1598. void *data)
  1599. {
  1600. struct ftrace_func_probe *entry;
  1601. struct ftrace_page *pg;
  1602. struct dyn_ftrace *rec;
  1603. int type, len, not;
  1604. unsigned long key;
  1605. int count = 0;
  1606. char *search;
  1607. type = filter_parse_regex(glob, strlen(glob), &search, &not);
  1608. len = strlen(search);
  1609. /* we do not support '!' for function probes */
  1610. if (WARN_ON(not))
  1611. return -EINVAL;
  1612. mutex_lock(&ftrace_lock);
  1613. do_for_each_ftrace_rec(pg, rec) {
  1614. if (rec->flags & FTRACE_FL_FAILED)
  1615. continue;
  1616. if (!ftrace_match_record(rec, search, len, type))
  1617. continue;
  1618. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  1619. if (!entry) {
  1620. /* If we did not process any, then return error */
  1621. if (!count)
  1622. count = -ENOMEM;
  1623. goto out_unlock;
  1624. }
  1625. count++;
  1626. entry->data = data;
  1627. /*
  1628. * The caller might want to do something special
  1629. * for each function we find. We call the callback
  1630. * to give the caller an opportunity to do so.
  1631. */
  1632. if (ops->callback) {
  1633. if (ops->callback(rec->ip, &entry->data) < 0) {
  1634. /* caller does not like this func */
  1635. kfree(entry);
  1636. continue;
  1637. }
  1638. }
  1639. entry->ops = ops;
  1640. entry->ip = rec->ip;
  1641. key = hash_long(entry->ip, FTRACE_HASH_BITS);
  1642. hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
  1643. } while_for_each_ftrace_rec();
  1644. __enable_ftrace_function_probe();
  1645. out_unlock:
  1646. mutex_unlock(&ftrace_lock);
  1647. return count;
  1648. }
  1649. enum {
  1650. PROBE_TEST_FUNC = 1,
  1651. PROBE_TEST_DATA = 2
  1652. };
  1653. static void
  1654. __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1655. void *data, int flags)
  1656. {
  1657. struct ftrace_func_probe *entry;
  1658. struct hlist_node *n, *tmp;
  1659. char str[KSYM_SYMBOL_LEN];
  1660. int type = MATCH_FULL;
  1661. int i, len = 0;
  1662. char *search;
  1663. if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
  1664. glob = NULL;
  1665. else if (glob) {
  1666. int not;
  1667. type = filter_parse_regex(glob, strlen(glob), &search, &not);
  1668. len = strlen(search);
  1669. /* we do not support '!' for function probes */
  1670. if (WARN_ON(not))
  1671. return;
  1672. }
  1673. mutex_lock(&ftrace_lock);
  1674. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1675. struct hlist_head *hhd = &ftrace_func_hash[i];
  1676. hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
  1677. /* break up if statements for readability */
  1678. if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
  1679. continue;
  1680. if ((flags & PROBE_TEST_DATA) && entry->data != data)
  1681. continue;
  1682. /* do this last, since it is the most expensive */
  1683. if (glob) {
  1684. kallsyms_lookup(entry->ip, NULL, NULL,
  1685. NULL, str);
  1686. if (!ftrace_match(str, glob, len, type))
  1687. continue;
  1688. }
  1689. hlist_del(&entry->node);
  1690. call_rcu(&entry->rcu, ftrace_free_entry_rcu);
  1691. }
  1692. }
  1693. __disable_ftrace_function_probe();
  1694. mutex_unlock(&ftrace_lock);
  1695. }
  1696. void
  1697. unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1698. void *data)
  1699. {
  1700. __unregister_ftrace_function_probe(glob, ops, data,
  1701. PROBE_TEST_FUNC | PROBE_TEST_DATA);
  1702. }
  1703. void
  1704. unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
  1705. {
  1706. __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
  1707. }
  1708. void unregister_ftrace_function_probe_all(char *glob)
  1709. {
  1710. __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
  1711. }
  1712. static LIST_HEAD(ftrace_commands);
  1713. static DEFINE_MUTEX(ftrace_cmd_mutex);
  1714. int register_ftrace_command(struct ftrace_func_command *cmd)
  1715. {
  1716. struct ftrace_func_command *p;
  1717. int ret = 0;
  1718. mutex_lock(&ftrace_cmd_mutex);
  1719. list_for_each_entry(p, &ftrace_commands, list) {
  1720. if (strcmp(cmd->name, p->name) == 0) {
  1721. ret = -EBUSY;
  1722. goto out_unlock;
  1723. }
  1724. }
  1725. list_add(&cmd->list, &ftrace_commands);
  1726. out_unlock:
  1727. mutex_unlock(&ftrace_cmd_mutex);
  1728. return ret;
  1729. }
  1730. int unregister_ftrace_command(struct ftrace_func_command *cmd)
  1731. {
  1732. struct ftrace_func_command *p, *n;
  1733. int ret = -ENODEV;
  1734. mutex_lock(&ftrace_cmd_mutex);
  1735. list_for_each_entry_safe(p, n, &ftrace_commands, list) {
  1736. if (strcmp(cmd->name, p->name) == 0) {
  1737. ret = 0;
  1738. list_del_init(&p->list);
  1739. goto out_unlock;
  1740. }
  1741. }
  1742. out_unlock:
  1743. mutex_unlock(&ftrace_cmd_mutex);
  1744. return ret;
  1745. }
  1746. static int ftrace_process_regex(char *buff, int len, int enable)
  1747. {
  1748. char *func, *command, *next = buff;
  1749. struct ftrace_func_command *p;
  1750. int ret = -EINVAL;
  1751. func = strsep(&next, ":");
  1752. if (!next) {
  1753. if (ftrace_match_records(func, len, enable))
  1754. return 0;
  1755. return ret;
  1756. }
  1757. /* command found */
  1758. command = strsep(&next, ":");
  1759. mutex_lock(&ftrace_cmd_mutex);
  1760. list_for_each_entry(p, &ftrace_commands, list) {
  1761. if (strcmp(p->name, command) == 0) {
  1762. ret = p->func(func, command, next, enable);
  1763. goto out_unlock;
  1764. }
  1765. }
  1766. out_unlock:
  1767. mutex_unlock(&ftrace_cmd_mutex);
  1768. return ret;
  1769. }
  1770. static ssize_t
  1771. ftrace_regex_write(struct file *file, const char __user *ubuf,
  1772. size_t cnt, loff_t *ppos, int enable)
  1773. {
  1774. struct ftrace_iterator *iter;
  1775. struct trace_parser *parser;
  1776. ssize_t ret, read;
  1777. if (!cnt)
  1778. return 0;
  1779. mutex_lock(&ftrace_regex_lock);
  1780. if (file->f_mode & FMODE_READ) {
  1781. struct seq_file *m = file->private_data;
  1782. iter = m->private;
  1783. } else
  1784. iter = file->private_data;
  1785. parser = &iter->parser;
  1786. read = trace_get_user(parser, ubuf, cnt, ppos);
  1787. if (read >= 0 && trace_parser_loaded(parser) &&
  1788. !trace_parser_cont(parser)) {
  1789. ret = ftrace_process_regex(parser->buffer,
  1790. parser->idx, enable);
  1791. trace_parser_clear(parser);
  1792. if (ret)
  1793. goto out_unlock;
  1794. }
  1795. ret = read;
  1796. out_unlock:
  1797. mutex_unlock(&ftrace_regex_lock);
  1798. return ret;
  1799. }
  1800. static ssize_t
  1801. ftrace_filter_write(struct file *file, const char __user *ubuf,
  1802. size_t cnt, loff_t *ppos)
  1803. {
  1804. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  1805. }
  1806. static ssize_t
  1807. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  1808. size_t cnt, loff_t *ppos)
  1809. {
  1810. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  1811. }
  1812. static void
  1813. ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
  1814. {
  1815. if (unlikely(ftrace_disabled))
  1816. return;
  1817. mutex_lock(&ftrace_regex_lock);
  1818. if (reset)
  1819. ftrace_filter_reset(enable);
  1820. if (buf)
  1821. ftrace_match_records(buf, len, enable);
  1822. mutex_unlock(&ftrace_regex_lock);
  1823. }
  1824. /**
  1825. * ftrace_set_filter - set a function to filter on in ftrace
  1826. * @buf - the string that holds the function filter text.
  1827. * @len - the length of the string.
  1828. * @reset - non zero to reset all filters before applying this filter.
  1829. *
  1830. * Filters denote which functions should be enabled when tracing is enabled.
  1831. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  1832. */
  1833. void ftrace_set_filter(unsigned char *buf, int len, int reset)
  1834. {
  1835. ftrace_set_regex(buf, len, reset, 1);
  1836. }
  1837. /**
  1838. * ftrace_set_notrace - set a function to not trace in ftrace
  1839. * @buf - the string that holds the function notrace text.
  1840. * @len - the length of the string.
  1841. * @reset - non zero to reset all filters before applying this filter.
  1842. *
  1843. * Notrace Filters denote which functions should not be enabled when tracing
  1844. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  1845. * for tracing.
  1846. */
  1847. void ftrace_set_notrace(unsigned char *buf, int len, int reset)
  1848. {
  1849. ftrace_set_regex(buf, len, reset, 0);
  1850. }
  1851. /*
  1852. * command line interface to allow users to set filters on boot up.
  1853. */
  1854. #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
  1855. static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
  1856. static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
  1857. static int __init set_ftrace_notrace(char *str)
  1858. {
  1859. strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
  1860. return 1;
  1861. }
  1862. __setup("ftrace_notrace=", set_ftrace_notrace);
  1863. static int __init set_ftrace_filter(char *str)
  1864. {
  1865. strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
  1866. return 1;
  1867. }
  1868. __setup("ftrace_filter=", set_ftrace_filter);
  1869. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1870. static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
  1871. static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
  1872. static int __init set_graph_function(char *str)
  1873. {
  1874. strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
  1875. return 1;
  1876. }
  1877. __setup("ftrace_graph_filter=", set_graph_function);
  1878. static void __init set_ftrace_early_graph(char *buf)
  1879. {
  1880. int ret;
  1881. char *func;
  1882. while (buf) {
  1883. func = strsep(&buf, ",");
  1884. /* we allow only one expression at a time */
  1885. ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
  1886. func);
  1887. if (ret)
  1888. printk(KERN_DEBUG "ftrace: function %s not "
  1889. "traceable\n", func);
  1890. }
  1891. }
  1892. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1893. static void __init set_ftrace_early_filter(char *buf, int enable)
  1894. {
  1895. char *func;
  1896. while (buf) {
  1897. func = strsep(&buf, ",");
  1898. ftrace_set_regex(func, strlen(func), 0, enable);
  1899. }
  1900. }
  1901. static void __init set_ftrace_early_filters(void)
  1902. {
  1903. if (ftrace_filter_buf[0])
  1904. set_ftrace_early_filter(ftrace_filter_buf, 1);
  1905. if (ftrace_notrace_buf[0])
  1906. set_ftrace_early_filter(ftrace_notrace_buf, 0);
  1907. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1908. if (ftrace_graph_buf[0])
  1909. set_ftrace_early_graph(ftrace_graph_buf);
  1910. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1911. }
  1912. static int
  1913. ftrace_regex_release(struct inode *inode, struct file *file, int enable)
  1914. {
  1915. struct seq_file *m = (struct seq_file *)file->private_data;
  1916. struct ftrace_iterator *iter;
  1917. struct trace_parser *parser;
  1918. mutex_lock(&ftrace_regex_lock);
  1919. if (file->f_mode & FMODE_READ) {
  1920. iter = m->private;
  1921. seq_release(inode, file);
  1922. } else
  1923. iter = file->private_data;
  1924. parser = &iter->parser;
  1925. if (trace_parser_loaded(parser)) {
  1926. parser->buffer[parser->idx] = 0;
  1927. ftrace_match_records(parser->buffer, parser->idx, enable);
  1928. }
  1929. mutex_lock(&ftrace_lock);
  1930. if (ftrace_start_up && ftrace_enabled)
  1931. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  1932. mutex_unlock(&ftrace_lock);
  1933. trace_parser_put(parser);
  1934. kfree(iter);
  1935. mutex_unlock(&ftrace_regex_lock);
  1936. return 0;
  1937. }
  1938. static int
  1939. ftrace_filter_release(struct inode *inode, struct file *file)
  1940. {
  1941. return ftrace_regex_release(inode, file, 1);
  1942. }
  1943. static int
  1944. ftrace_notrace_release(struct inode *inode, struct file *file)
  1945. {
  1946. return ftrace_regex_release(inode, file, 0);
  1947. }
  1948. static const struct file_operations ftrace_avail_fops = {
  1949. .open = ftrace_avail_open,
  1950. .read = seq_read,
  1951. .llseek = seq_lseek,
  1952. .release = seq_release_private,
  1953. };
  1954. static const struct file_operations ftrace_failures_fops = {
  1955. .open = ftrace_failures_open,
  1956. .read = seq_read,
  1957. .llseek = seq_lseek,
  1958. .release = seq_release_private,
  1959. };
  1960. static const struct file_operations ftrace_filter_fops = {
  1961. .open = ftrace_filter_open,
  1962. .read = seq_read,
  1963. .write = ftrace_filter_write,
  1964. .llseek = ftrace_regex_lseek,
  1965. .release = ftrace_filter_release,
  1966. };
  1967. static const struct file_operations ftrace_notrace_fops = {
  1968. .open = ftrace_notrace_open,
  1969. .read = seq_read,
  1970. .write = ftrace_notrace_write,
  1971. .llseek = ftrace_regex_lseek,
  1972. .release = ftrace_notrace_release,
  1973. };
  1974. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1975. static DEFINE_MUTEX(graph_lock);
  1976. int ftrace_graph_count;
  1977. int ftrace_graph_filter_enabled;
  1978. unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
  1979. static void *
  1980. __g_next(struct seq_file *m, loff_t *pos)
  1981. {
  1982. if (*pos >= ftrace_graph_count)
  1983. return NULL;
  1984. return &ftrace_graph_funcs[*pos];
  1985. }
  1986. static void *
  1987. g_next(struct seq_file *m, void *v, loff_t *pos)
  1988. {
  1989. (*pos)++;
  1990. return __g_next(m, pos);
  1991. }
  1992. static void *g_start(struct seq_file *m, loff_t *pos)
  1993. {
  1994. mutex_lock(&graph_lock);
  1995. /* Nothing, tell g_show to print all functions are enabled */
  1996. if (!ftrace_graph_filter_enabled && !*pos)
  1997. return (void *)1;
  1998. return __g_next(m, pos);
  1999. }
  2000. static void g_stop(struct seq_file *m, void *p)
  2001. {
  2002. mutex_unlock(&graph_lock);
  2003. }
  2004. static int g_show(struct seq_file *m, void *v)
  2005. {
  2006. unsigned long *ptr = v;
  2007. if (!ptr)
  2008. return 0;
  2009. if (ptr == (unsigned long *)1) {
  2010. seq_printf(m, "#### all functions enabled ####\n");
  2011. return 0;
  2012. }
  2013. seq_printf(m, "%ps\n", (void *)*ptr);
  2014. return 0;
  2015. }
  2016. static const struct seq_operations ftrace_graph_seq_ops = {
  2017. .start = g_start,
  2018. .next = g_next,
  2019. .stop = g_stop,
  2020. .show = g_show,
  2021. };
  2022. static int
  2023. ftrace_graph_open(struct inode *inode, struct file *file)
  2024. {
  2025. int ret = 0;
  2026. if (unlikely(ftrace_disabled))
  2027. return -ENODEV;
  2028. mutex_lock(&graph_lock);
  2029. if ((file->f_mode & FMODE_WRITE) &&
  2030. (file->f_flags & O_TRUNC)) {
  2031. ftrace_graph_filter_enabled = 0;
  2032. ftrace_graph_count = 0;
  2033. memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
  2034. }
  2035. mutex_unlock(&graph_lock);
  2036. if (file->f_mode & FMODE_READ)
  2037. ret = seq_open(file, &ftrace_graph_seq_ops);
  2038. return ret;
  2039. }
  2040. static int
  2041. ftrace_graph_release(struct inode *inode, struct file *file)
  2042. {
  2043. if (file->f_mode & FMODE_READ)
  2044. seq_release(inode, file);
  2045. return 0;
  2046. }
  2047. static int
  2048. ftrace_set_func(unsigned long *array, int *idx, char *buffer)
  2049. {
  2050. struct dyn_ftrace *rec;
  2051. struct ftrace_page *pg;
  2052. int search_len;
  2053. int fail = 1;
  2054. int type, not;
  2055. char *search;
  2056. bool exists;
  2057. int i;
  2058. if (ftrace_disabled)
  2059. return -ENODEV;
  2060. /* decode regex */
  2061. type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
  2062. if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
  2063. return -EBUSY;
  2064. search_len = strlen(search);
  2065. mutex_lock(&ftrace_lock);
  2066. do_for_each_ftrace_rec(pg, rec) {
  2067. if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
  2068. continue;
  2069. if (ftrace_match_record(rec, search, search_len, type)) {
  2070. /* if it is in the array */
  2071. exists = false;
  2072. for (i = 0; i < *idx; i++) {
  2073. if (array[i] == rec->ip) {
  2074. exists = true;
  2075. break;
  2076. }
  2077. }
  2078. if (!not) {
  2079. fail = 0;
  2080. if (!exists) {
  2081. array[(*idx)++] = rec->ip;
  2082. if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
  2083. goto out;
  2084. }
  2085. } else {
  2086. if (exists) {
  2087. array[i] = array[--(*idx)];
  2088. array[*idx] = 0;
  2089. fail = 0;
  2090. }
  2091. }
  2092. }
  2093. } while_for_each_ftrace_rec();
  2094. out:
  2095. mutex_unlock(&ftrace_lock);
  2096. if (fail)
  2097. return -EINVAL;
  2098. ftrace_graph_filter_enabled = 1;
  2099. return 0;
  2100. }
  2101. static ssize_t
  2102. ftrace_graph_write(struct file *file, const char __user *ubuf,
  2103. size_t cnt, loff_t *ppos)
  2104. {
  2105. struct trace_parser parser;
  2106. ssize_t read, ret;
  2107. if (!cnt)
  2108. return 0;
  2109. mutex_lock(&graph_lock);
  2110. if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
  2111. ret = -ENOMEM;
  2112. goto out_unlock;
  2113. }
  2114. read = trace_get_user(&parser, ubuf, cnt, ppos);
  2115. if (read >= 0 && trace_parser_loaded((&parser))) {
  2116. parser.buffer[parser.idx] = 0;
  2117. /* we allow only one expression at a time */
  2118. ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
  2119. parser.buffer);
  2120. if (ret)
  2121. goto out_free;
  2122. }
  2123. ret = read;
  2124. out_free:
  2125. trace_parser_put(&parser);
  2126. out_unlock:
  2127. mutex_unlock(&graph_lock);
  2128. return ret;
  2129. }
  2130. static const struct file_operations ftrace_graph_fops = {
  2131. .open = ftrace_graph_open,
  2132. .read = seq_read,
  2133. .write = ftrace_graph_write,
  2134. .release = ftrace_graph_release,
  2135. };
  2136. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2137. static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
  2138. {
  2139. trace_create_file("available_filter_functions", 0444,
  2140. d_tracer, NULL, &ftrace_avail_fops);
  2141. trace_create_file("failures", 0444,
  2142. d_tracer, NULL, &ftrace_failures_fops);
  2143. trace_create_file("set_ftrace_filter", 0644, d_tracer,
  2144. NULL, &ftrace_filter_fops);
  2145. trace_create_file("set_ftrace_notrace", 0644, d_tracer,
  2146. NULL, &ftrace_notrace_fops);
  2147. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2148. trace_create_file("set_graph_function", 0444, d_tracer,
  2149. NULL,
  2150. &ftrace_graph_fops);
  2151. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2152. return 0;
  2153. }
  2154. static int ftrace_process_locs(struct module *mod,
  2155. unsigned long *start,
  2156. unsigned long *end)
  2157. {
  2158. unsigned long *p;
  2159. unsigned long addr;
  2160. unsigned long flags;
  2161. mutex_lock(&ftrace_lock);
  2162. p = start;
  2163. while (p < end) {
  2164. addr = ftrace_call_adjust(*p++);
  2165. /*
  2166. * Some architecture linkers will pad between
  2167. * the different mcount_loc sections of different
  2168. * object files to satisfy alignments.
  2169. * Skip any NULL pointers.
  2170. */
  2171. if (!addr)
  2172. continue;
  2173. ftrace_record_ip(addr);
  2174. }
  2175. /* disable interrupts to prevent kstop machine */
  2176. local_irq_save(flags);
  2177. ftrace_update_code(mod);
  2178. local_irq_restore(flags);
  2179. mutex_unlock(&ftrace_lock);
  2180. return 0;
  2181. }
  2182. #ifdef CONFIG_MODULES
  2183. void ftrace_release_mod(struct module *mod)
  2184. {
  2185. struct dyn_ftrace *rec;
  2186. struct ftrace_page *pg;
  2187. if (ftrace_disabled)
  2188. return;
  2189. mutex_lock(&ftrace_lock);
  2190. do_for_each_ftrace_rec(pg, rec) {
  2191. if (within_module_core(rec->ip, mod)) {
  2192. /*
  2193. * rec->ip is changed in ftrace_free_rec()
  2194. * It should not between s and e if record was freed.
  2195. */
  2196. FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
  2197. ftrace_free_rec(rec);
  2198. }
  2199. } while_for_each_ftrace_rec();
  2200. mutex_unlock(&ftrace_lock);
  2201. }
  2202. static void ftrace_init_module(struct module *mod,
  2203. unsigned long *start, unsigned long *end)
  2204. {
  2205. if (ftrace_disabled || start == end)
  2206. return;
  2207. ftrace_process_locs(mod, start, end);
  2208. }
  2209. static int ftrace_module_notify(struct notifier_block *self,
  2210. unsigned long val, void *data)
  2211. {
  2212. struct module *mod = data;
  2213. switch (val) {
  2214. case MODULE_STATE_COMING:
  2215. ftrace_init_module(mod, mod->ftrace_callsites,
  2216. mod->ftrace_callsites +
  2217. mod->num_ftrace_callsites);
  2218. break;
  2219. case MODULE_STATE_GOING:
  2220. ftrace_release_mod(mod);
  2221. break;
  2222. }
  2223. return 0;
  2224. }
  2225. #else
  2226. static int ftrace_module_notify(struct notifier_block *self,
  2227. unsigned long val, void *data)
  2228. {
  2229. return 0;
  2230. }
  2231. #endif /* CONFIG_MODULES */
  2232. struct notifier_block ftrace_module_nb = {
  2233. .notifier_call = ftrace_module_notify,
  2234. .priority = 0,
  2235. };
  2236. extern unsigned long __start_mcount_loc[];
  2237. extern unsigned long __stop_mcount_loc[];
  2238. void __init ftrace_init(void)
  2239. {
  2240. unsigned long count, addr, flags;
  2241. int ret;
  2242. /* Keep the ftrace pointer to the stub */
  2243. addr = (unsigned long)ftrace_stub;
  2244. local_irq_save(flags);
  2245. ftrace_dyn_arch_init(&addr);
  2246. local_irq_restore(flags);
  2247. /* ftrace_dyn_arch_init places the return code in addr */
  2248. if (addr)
  2249. goto failed;
  2250. count = __stop_mcount_loc - __start_mcount_loc;
  2251. ret = ftrace_dyn_table_alloc(count);
  2252. if (ret)
  2253. goto failed;
  2254. last_ftrace_enabled = ftrace_enabled = 1;
  2255. ret = ftrace_process_locs(NULL,
  2256. __start_mcount_loc,
  2257. __stop_mcount_loc);
  2258. ret = register_module_notifier(&ftrace_module_nb);
  2259. if (ret)
  2260. pr_warning("Failed to register trace ftrace module notifier\n");
  2261. set_ftrace_early_filters();
  2262. return;
  2263. failed:
  2264. ftrace_disabled = 1;
  2265. }
  2266. #else
  2267. static int __init ftrace_nodyn_init(void)
  2268. {
  2269. ftrace_enabled = 1;
  2270. return 0;
  2271. }
  2272. device_initcall(ftrace_nodyn_init);
  2273. static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
  2274. static inline void ftrace_startup_enable(int command) { }
  2275. /* Keep as macros so we do not need to define the commands */
  2276. # define ftrace_startup(command) do { } while (0)
  2277. # define ftrace_shutdown(command) do { } while (0)
  2278. # define ftrace_startup_sysctl() do { } while (0)
  2279. # define ftrace_shutdown_sysctl() do { } while (0)
  2280. #endif /* CONFIG_DYNAMIC_FTRACE */
  2281. static void clear_ftrace_swapper(void)
  2282. {
  2283. struct task_struct *p;
  2284. int cpu;
  2285. get_online_cpus();
  2286. for_each_online_cpu(cpu) {
  2287. p = idle_task(cpu);
  2288. clear_tsk_trace_trace(p);
  2289. }
  2290. put_online_cpus();
  2291. }
  2292. static void set_ftrace_swapper(void)
  2293. {
  2294. struct task_struct *p;
  2295. int cpu;
  2296. get_online_cpus();
  2297. for_each_online_cpu(cpu) {
  2298. p = idle_task(cpu);
  2299. set_tsk_trace_trace(p);
  2300. }
  2301. put_online_cpus();
  2302. }
  2303. static void clear_ftrace_pid(struct pid *pid)
  2304. {
  2305. struct task_struct *p;
  2306. rcu_read_lock();
  2307. do_each_pid_task(pid, PIDTYPE_PID, p) {
  2308. clear_tsk_trace_trace(p);
  2309. } while_each_pid_task(pid, PIDTYPE_PID, p);
  2310. rcu_read_unlock();
  2311. put_pid(pid);
  2312. }
  2313. static void set_ftrace_pid(struct pid *pid)
  2314. {
  2315. struct task_struct *p;
  2316. rcu_read_lock();
  2317. do_each_pid_task(pid, PIDTYPE_PID, p) {
  2318. set_tsk_trace_trace(p);
  2319. } while_each_pid_task(pid, PIDTYPE_PID, p);
  2320. rcu_read_unlock();
  2321. }
  2322. static void clear_ftrace_pid_task(struct pid *pid)
  2323. {
  2324. if (pid == ftrace_swapper_pid)
  2325. clear_ftrace_swapper();
  2326. else
  2327. clear_ftrace_pid(pid);
  2328. }
  2329. static void set_ftrace_pid_task(struct pid *pid)
  2330. {
  2331. if (pid == ftrace_swapper_pid)
  2332. set_ftrace_swapper();
  2333. else
  2334. set_ftrace_pid(pid);
  2335. }
  2336. static int ftrace_pid_add(int p)
  2337. {
  2338. struct pid *pid;
  2339. struct ftrace_pid *fpid;
  2340. int ret = -EINVAL;
  2341. mutex_lock(&ftrace_lock);
  2342. if (!p)
  2343. pid = ftrace_swapper_pid;
  2344. else
  2345. pid = find_get_pid(p);
  2346. if (!pid)
  2347. goto out;
  2348. ret = 0;
  2349. list_for_each_entry(fpid, &ftrace_pids, list)
  2350. if (fpid->pid == pid)
  2351. goto out_put;
  2352. ret = -ENOMEM;
  2353. fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
  2354. if (!fpid)
  2355. goto out_put;
  2356. list_add(&fpid->list, &ftrace_pids);
  2357. fpid->pid = pid;
  2358. set_ftrace_pid_task(pid);
  2359. ftrace_update_pid_func();
  2360. ftrace_startup_enable(0);
  2361. mutex_unlock(&ftrace_lock);
  2362. return 0;
  2363. out_put:
  2364. if (pid != ftrace_swapper_pid)
  2365. put_pid(pid);
  2366. out:
  2367. mutex_unlock(&ftrace_lock);
  2368. return ret;
  2369. }
  2370. static void ftrace_pid_reset(void)
  2371. {
  2372. struct ftrace_pid *fpid, *safe;
  2373. mutex_lock(&ftrace_lock);
  2374. list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
  2375. struct pid *pid = fpid->pid;
  2376. clear_ftrace_pid_task(pid);
  2377. list_del(&fpid->list);
  2378. kfree(fpid);
  2379. }
  2380. ftrace_update_pid_func();
  2381. ftrace_startup_enable(0);
  2382. mutex_unlock(&ftrace_lock);
  2383. }
  2384. static void *fpid_start(struct seq_file *m, loff_t *pos)
  2385. {
  2386. mutex_lock(&ftrace_lock);
  2387. if (list_empty(&ftrace_pids) && (!*pos))
  2388. return (void *) 1;
  2389. return seq_list_start(&ftrace_pids, *pos);
  2390. }
  2391. static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
  2392. {
  2393. if (v == (void *)1)
  2394. return NULL;
  2395. return seq_list_next(v, &ftrace_pids, pos);
  2396. }
  2397. static void fpid_stop(struct seq_file *m, void *p)
  2398. {
  2399. mutex_unlock(&ftrace_lock);
  2400. }
  2401. static int fpid_show(struct seq_file *m, void *v)
  2402. {
  2403. const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
  2404. if (v == (void *)1) {
  2405. seq_printf(m, "no pid\n");
  2406. return 0;
  2407. }
  2408. if (fpid->pid == ftrace_swapper_pid)
  2409. seq_printf(m, "swapper tasks\n");
  2410. else
  2411. seq_printf(m, "%u\n", pid_vnr(fpid->pid));
  2412. return 0;
  2413. }
  2414. static const struct seq_operations ftrace_pid_sops = {
  2415. .start = fpid_start,
  2416. .next = fpid_next,
  2417. .stop = fpid_stop,
  2418. .show = fpid_show,
  2419. };
  2420. static int
  2421. ftrace_pid_open(struct inode *inode, struct file *file)
  2422. {
  2423. int ret = 0;
  2424. if ((file->f_mode & FMODE_WRITE) &&
  2425. (file->f_flags & O_TRUNC))
  2426. ftrace_pid_reset();
  2427. if (file->f_mode & FMODE_READ)
  2428. ret = seq_open(file, &ftrace_pid_sops);
  2429. return ret;
  2430. }
  2431. static ssize_t
  2432. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  2433. size_t cnt, loff_t *ppos)
  2434. {
  2435. char buf[64], *tmp;
  2436. long val;
  2437. int ret;
  2438. if (cnt >= sizeof(buf))
  2439. return -EINVAL;
  2440. if (copy_from_user(&buf, ubuf, cnt))
  2441. return -EFAULT;
  2442. buf[cnt] = 0;
  2443. /*
  2444. * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
  2445. * to clean the filter quietly.
  2446. */
  2447. tmp = strstrip(buf);
  2448. if (strlen(tmp) == 0)
  2449. return 1;
  2450. ret = strict_strtol(tmp, 10, &val);
  2451. if (ret < 0)
  2452. return ret;
  2453. ret = ftrace_pid_add(val);
  2454. return ret ? ret : cnt;
  2455. }
  2456. static int
  2457. ftrace_pid_release(struct inode *inode, struct file *file)
  2458. {
  2459. if (file->f_mode & FMODE_READ)
  2460. seq_release(inode, file);
  2461. return 0;
  2462. }
  2463. static const struct file_operations ftrace_pid_fops = {
  2464. .open = ftrace_pid_open,
  2465. .write = ftrace_pid_write,
  2466. .read = seq_read,
  2467. .llseek = seq_lseek,
  2468. .release = ftrace_pid_release,
  2469. };
  2470. static __init int ftrace_init_debugfs(void)
  2471. {
  2472. struct dentry *d_tracer;
  2473. d_tracer = tracing_init_dentry();
  2474. if (!d_tracer)
  2475. return 0;
  2476. ftrace_init_dyn_debugfs(d_tracer);
  2477. trace_create_file("set_ftrace_pid", 0644, d_tracer,
  2478. NULL, &ftrace_pid_fops);
  2479. ftrace_profile_debugfs(d_tracer);
  2480. return 0;
  2481. }
  2482. fs_initcall(ftrace_init_debugfs);
  2483. /**
  2484. * ftrace_kill - kill ftrace
  2485. *
  2486. * This function should be used by panic code. It stops ftrace
  2487. * but in a not so nice way. If you need to simply kill ftrace
  2488. * from a non-atomic section, use ftrace_kill.
  2489. */
  2490. void ftrace_kill(void)
  2491. {
  2492. ftrace_disabled = 1;
  2493. ftrace_enabled = 0;
  2494. clear_ftrace_function();
  2495. }
  2496. /**
  2497. * register_ftrace_function - register a function for profiling
  2498. * @ops - ops structure that holds the function for profiling.
  2499. *
  2500. * Register a function to be called by all functions in the
  2501. * kernel.
  2502. *
  2503. * Note: @ops->func and all the functions it calls must be labeled
  2504. * with "notrace", otherwise it will go into a
  2505. * recursive loop.
  2506. */
  2507. int register_ftrace_function(struct ftrace_ops *ops)
  2508. {
  2509. int ret;
  2510. if (unlikely(ftrace_disabled))
  2511. return -1;
  2512. mutex_lock(&ftrace_lock);
  2513. ret = __register_ftrace_function(ops);
  2514. ftrace_startup(0);
  2515. mutex_unlock(&ftrace_lock);
  2516. return ret;
  2517. }
  2518. /**
  2519. * unregister_ftrace_function - unregister a function for profiling.
  2520. * @ops - ops structure that holds the function to unregister
  2521. *
  2522. * Unregister a function that was added to be called by ftrace profiling.
  2523. */
  2524. int unregister_ftrace_function(struct ftrace_ops *ops)
  2525. {
  2526. int ret;
  2527. mutex_lock(&ftrace_lock);
  2528. ret = __unregister_ftrace_function(ops);
  2529. ftrace_shutdown(0);
  2530. mutex_unlock(&ftrace_lock);
  2531. return ret;
  2532. }
  2533. int
  2534. ftrace_enable_sysctl(struct ctl_table *table, int write,
  2535. void __user *buffer, size_t *lenp,
  2536. loff_t *ppos)
  2537. {
  2538. int ret;
  2539. if (unlikely(ftrace_disabled))
  2540. return -ENODEV;
  2541. mutex_lock(&ftrace_lock);
  2542. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  2543. if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
  2544. goto out;
  2545. last_ftrace_enabled = !!ftrace_enabled;
  2546. if (ftrace_enabled) {
  2547. ftrace_startup_sysctl();
  2548. /* we are starting ftrace again */
  2549. if (ftrace_list != &ftrace_list_end) {
  2550. if (ftrace_list->next == &ftrace_list_end)
  2551. ftrace_trace_function = ftrace_list->func;
  2552. else
  2553. ftrace_trace_function = ftrace_list_func;
  2554. }
  2555. } else {
  2556. /* stopping ftrace calls (just send to ftrace_stub) */
  2557. ftrace_trace_function = ftrace_stub;
  2558. ftrace_shutdown_sysctl();
  2559. }
  2560. out:
  2561. mutex_unlock(&ftrace_lock);
  2562. return ret;
  2563. }
  2564. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2565. static int ftrace_graph_active;
  2566. static struct notifier_block ftrace_suspend_notifier;
  2567. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
  2568. {
  2569. return 0;
  2570. }
  2571. /* The callbacks that hook a function */
  2572. trace_func_graph_ret_t ftrace_graph_return =
  2573. (trace_func_graph_ret_t)ftrace_stub;
  2574. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  2575. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  2576. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  2577. {
  2578. int i;
  2579. int ret = 0;
  2580. unsigned long flags;
  2581. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  2582. struct task_struct *g, *t;
  2583. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  2584. ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
  2585. * sizeof(struct ftrace_ret_stack),
  2586. GFP_KERNEL);
  2587. if (!ret_stack_list[i]) {
  2588. start = 0;
  2589. end = i;
  2590. ret = -ENOMEM;
  2591. goto free;
  2592. }
  2593. }
  2594. read_lock_irqsave(&tasklist_lock, flags);
  2595. do_each_thread(g, t) {
  2596. if (start == end) {
  2597. ret = -EAGAIN;
  2598. goto unlock;
  2599. }
  2600. if (t->ret_stack == NULL) {
  2601. atomic_set(&t->tracing_graph_pause, 0);
  2602. atomic_set(&t->trace_overrun, 0);
  2603. t->curr_ret_stack = -1;
  2604. /* Make sure the tasks see the -1 first: */
  2605. smp_wmb();
  2606. t->ret_stack = ret_stack_list[start++];
  2607. }
  2608. } while_each_thread(g, t);
  2609. unlock:
  2610. read_unlock_irqrestore(&tasklist_lock, flags);
  2611. free:
  2612. for (i = start; i < end; i++)
  2613. kfree(ret_stack_list[i]);
  2614. return ret;
  2615. }
  2616. static void
  2617. ftrace_graph_probe_sched_switch(void *ignore,
  2618. struct task_struct *prev, struct task_struct *next)
  2619. {
  2620. unsigned long long timestamp;
  2621. int index;
  2622. /*
  2623. * Does the user want to count the time a function was asleep.
  2624. * If so, do not update the time stamps.
  2625. */
  2626. if (trace_flags & TRACE_ITER_SLEEP_TIME)
  2627. return;
  2628. timestamp = trace_clock_local();
  2629. prev->ftrace_timestamp = timestamp;
  2630. /* only process tasks that we timestamped */
  2631. if (!next->ftrace_timestamp)
  2632. return;
  2633. /*
  2634. * Update all the counters in next to make up for the
  2635. * time next was sleeping.
  2636. */
  2637. timestamp -= next->ftrace_timestamp;
  2638. for (index = next->curr_ret_stack; index >= 0; index--)
  2639. next->ret_stack[index].calltime += timestamp;
  2640. }
  2641. /* Allocate a return stack for each task */
  2642. static int start_graph_tracing(void)
  2643. {
  2644. struct ftrace_ret_stack **ret_stack_list;
  2645. int ret, cpu;
  2646. ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
  2647. sizeof(struct ftrace_ret_stack *),
  2648. GFP_KERNEL);
  2649. if (!ret_stack_list)
  2650. return -ENOMEM;
  2651. /* The cpu_boot init_task->ret_stack will never be freed */
  2652. for_each_online_cpu(cpu) {
  2653. if (!idle_task(cpu)->ret_stack)
  2654. ftrace_graph_init_task(idle_task(cpu));
  2655. }
  2656. do {
  2657. ret = alloc_retstack_tasklist(ret_stack_list);
  2658. } while (ret == -EAGAIN);
  2659. if (!ret) {
  2660. ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  2661. if (ret)
  2662. pr_info("ftrace_graph: Couldn't activate tracepoint"
  2663. " probe to kernel_sched_switch\n");
  2664. }
  2665. kfree(ret_stack_list);
  2666. return ret;
  2667. }
  2668. /*
  2669. * Hibernation protection.
  2670. * The state of the current task is too much unstable during
  2671. * suspend/restore to disk. We want to protect against that.
  2672. */
  2673. static int
  2674. ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
  2675. void *unused)
  2676. {
  2677. switch (state) {
  2678. case PM_HIBERNATION_PREPARE:
  2679. pause_graph_tracing();
  2680. break;
  2681. case PM_POST_HIBERNATION:
  2682. unpause_graph_tracing();
  2683. break;
  2684. }
  2685. return NOTIFY_DONE;
  2686. }
  2687. int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  2688. trace_func_graph_ent_t entryfunc)
  2689. {
  2690. int ret = 0;
  2691. mutex_lock(&ftrace_lock);
  2692. /* we currently allow only one tracer registered at a time */
  2693. if (ftrace_graph_active) {
  2694. ret = -EBUSY;
  2695. goto out;
  2696. }
  2697. ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
  2698. register_pm_notifier(&ftrace_suspend_notifier);
  2699. ftrace_graph_active++;
  2700. ret = start_graph_tracing();
  2701. if (ret) {
  2702. ftrace_graph_active--;
  2703. goto out;
  2704. }
  2705. ftrace_graph_return = retfunc;
  2706. ftrace_graph_entry = entryfunc;
  2707. ftrace_startup(FTRACE_START_FUNC_RET);
  2708. out:
  2709. mutex_unlock(&ftrace_lock);
  2710. return ret;
  2711. }
  2712. void unregister_ftrace_graph(void)
  2713. {
  2714. mutex_lock(&ftrace_lock);
  2715. if (unlikely(!ftrace_graph_active))
  2716. goto out;
  2717. ftrace_graph_active--;
  2718. ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
  2719. ftrace_graph_entry = ftrace_graph_entry_stub;
  2720. ftrace_shutdown(FTRACE_STOP_FUNC_RET);
  2721. unregister_pm_notifier(&ftrace_suspend_notifier);
  2722. unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  2723. out:
  2724. mutex_unlock(&ftrace_lock);
  2725. }
  2726. /* Allocate a return stack for newly created task */
  2727. void ftrace_graph_init_task(struct task_struct *t)
  2728. {
  2729. /* Make sure we do not use the parent ret_stack */
  2730. t->ret_stack = NULL;
  2731. t->curr_ret_stack = -1;
  2732. if (ftrace_graph_active) {
  2733. struct ftrace_ret_stack *ret_stack;
  2734. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  2735. * sizeof(struct ftrace_ret_stack),
  2736. GFP_KERNEL);
  2737. if (!ret_stack)
  2738. return;
  2739. atomic_set(&t->tracing_graph_pause, 0);
  2740. atomic_set(&t->trace_overrun, 0);
  2741. t->ftrace_timestamp = 0;
  2742. /* make curr_ret_stack visable before we add the ret_stack */
  2743. smp_wmb();
  2744. t->ret_stack = ret_stack;
  2745. }
  2746. }
  2747. void ftrace_graph_exit_task(struct task_struct *t)
  2748. {
  2749. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  2750. t->ret_stack = NULL;
  2751. /* NULL must become visible to IRQs before we free it: */
  2752. barrier();
  2753. kfree(ret_stack);
  2754. }
  2755. void ftrace_graph_stop(void)
  2756. {
  2757. ftrace_stop();
  2758. }
  2759. #endif