ftrace.c 71 KB

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