ftrace.c 70 KB

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