ftrace.c 70 KB

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