ftrace.c 71 KB

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