ftrace.c 71 KB

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