ftrace.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765
  1. /*
  2. * Infrastructure for profiling code inserted by 'gcc -pg'.
  3. *
  4. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  5. * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
  6. *
  7. * Originally ported from the -rt patch by:
  8. * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
  9. *
  10. * Based on code in the latency_tracer, that is:
  11. *
  12. * Copyright (C) 2004-2006 Ingo Molnar
  13. * Copyright (C) 2004 William Lee Irwin III
  14. */
  15. #include <linux/stop_machine.h>
  16. #include <linux/clocksource.h>
  17. #include <linux/kallsyms.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/suspend.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/hardirq.h>
  22. #include <linux/kthread.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/kprobes.h>
  25. #include <linux/ftrace.h>
  26. #include <linux/sysctl.h>
  27. #include <linux/ctype.h>
  28. #include <linux/list.h>
  29. #include <linux/hash.h>
  30. #include <trace/sched.h>
  31. #include <asm/ftrace.h>
  32. #include "trace.h"
  33. #define FTRACE_WARN_ON(cond) \
  34. do { \
  35. if (WARN_ON(cond)) \
  36. ftrace_kill(); \
  37. } while (0)
  38. #define FTRACE_WARN_ON_ONCE(cond) \
  39. do { \
  40. if (WARN_ON_ONCE(cond)) \
  41. ftrace_kill(); \
  42. } while (0)
  43. /* hash bits for specific function selection */
  44. #define FTRACE_HASH_BITS 7
  45. #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
  46. /* ftrace_enabled is a method to turn ftrace on or off */
  47. int ftrace_enabled __read_mostly;
  48. static int last_ftrace_enabled;
  49. /* Quick disabling of function tracer. */
  50. int function_trace_stop;
  51. /*
  52. * ftrace_disabled is set when an anomaly is discovered.
  53. * ftrace_disabled is much stronger than ftrace_enabled.
  54. */
  55. static int ftrace_disabled __read_mostly;
  56. static DEFINE_MUTEX(ftrace_lock);
  57. static struct ftrace_ops ftrace_list_end __read_mostly =
  58. {
  59. .func = ftrace_stub,
  60. };
  61. static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
  62. ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
  63. ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
  64. ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
  65. static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
  66. {
  67. struct ftrace_ops *op = ftrace_list;
  68. /* in case someone actually ports this to alpha! */
  69. read_barrier_depends();
  70. while (op != &ftrace_list_end) {
  71. /* silly alpha */
  72. read_barrier_depends();
  73. op->func(ip, parent_ip);
  74. op = op->next;
  75. };
  76. }
  77. static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
  78. {
  79. if (!test_tsk_trace_trace(current))
  80. return;
  81. ftrace_pid_function(ip, parent_ip);
  82. }
  83. static void set_ftrace_pid_function(ftrace_func_t func)
  84. {
  85. /* do not set ftrace_pid_function to itself! */
  86. if (func != ftrace_pid_func)
  87. ftrace_pid_function = func;
  88. }
  89. /**
  90. * clear_ftrace_function - reset the ftrace function
  91. *
  92. * This NULLs the ftrace function and in essence stops
  93. * tracing. There may be lag
  94. */
  95. void clear_ftrace_function(void)
  96. {
  97. ftrace_trace_function = ftrace_stub;
  98. __ftrace_trace_function = ftrace_stub;
  99. ftrace_pid_function = ftrace_stub;
  100. }
  101. #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  102. /*
  103. * For those archs that do not test ftrace_trace_stop in their
  104. * mcount call site, we need to do it from C.
  105. */
  106. static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
  107. {
  108. if (function_trace_stop)
  109. return;
  110. __ftrace_trace_function(ip, parent_ip);
  111. }
  112. #endif
  113. static int __register_ftrace_function(struct ftrace_ops *ops)
  114. {
  115. ops->next = ftrace_list;
  116. /*
  117. * We are entering ops into the ftrace_list but another
  118. * CPU might be walking that list. We need to make sure
  119. * the ops->next pointer is valid before another CPU sees
  120. * the ops pointer included into the ftrace_list.
  121. */
  122. smp_wmb();
  123. ftrace_list = ops;
  124. if (ftrace_enabled) {
  125. ftrace_func_t func;
  126. if (ops->next == &ftrace_list_end)
  127. func = ops->func;
  128. else
  129. func = ftrace_list_func;
  130. if (ftrace_pid_trace) {
  131. set_ftrace_pid_function(func);
  132. func = ftrace_pid_func;
  133. }
  134. /*
  135. * For one func, simply call it directly.
  136. * For more than one func, call the chain.
  137. */
  138. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  139. ftrace_trace_function = func;
  140. #else
  141. __ftrace_trace_function = func;
  142. ftrace_trace_function = ftrace_test_stop_func;
  143. #endif
  144. }
  145. return 0;
  146. }
  147. static int __unregister_ftrace_function(struct ftrace_ops *ops)
  148. {
  149. struct ftrace_ops **p;
  150. /*
  151. * If we are removing the last function, then simply point
  152. * to the ftrace_stub.
  153. */
  154. if (ftrace_list == ops && ops->next == &ftrace_list_end) {
  155. ftrace_trace_function = ftrace_stub;
  156. ftrace_list = &ftrace_list_end;
  157. return 0;
  158. }
  159. for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
  160. if (*p == ops)
  161. break;
  162. if (*p != ops)
  163. return -1;
  164. *p = (*p)->next;
  165. if (ftrace_enabled) {
  166. /* If we only have one func left, then call that directly */
  167. if (ftrace_list->next == &ftrace_list_end) {
  168. ftrace_func_t func = ftrace_list->func;
  169. if (ftrace_pid_trace) {
  170. set_ftrace_pid_function(func);
  171. func = ftrace_pid_func;
  172. }
  173. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  174. ftrace_trace_function = func;
  175. #else
  176. __ftrace_trace_function = func;
  177. #endif
  178. }
  179. }
  180. return 0;
  181. }
  182. static void ftrace_update_pid_func(void)
  183. {
  184. ftrace_func_t func;
  185. if (ftrace_trace_function == ftrace_stub)
  186. return;
  187. func = ftrace_trace_function;
  188. if (ftrace_pid_trace) {
  189. set_ftrace_pid_function(func);
  190. func = ftrace_pid_func;
  191. } else {
  192. if (func == ftrace_pid_func)
  193. func = ftrace_pid_function;
  194. }
  195. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  196. ftrace_trace_function = func;
  197. #else
  198. __ftrace_trace_function = func;
  199. #endif
  200. }
  201. /* set when tracing only a pid */
  202. struct pid *ftrace_pid_trace;
  203. static struct pid * const ftrace_swapper_pid = &init_struct_pid;
  204. #ifdef CONFIG_DYNAMIC_FTRACE
  205. #ifndef CONFIG_FTRACE_MCOUNT_RECORD
  206. # error Dynamic ftrace depends on MCOUNT_RECORD
  207. #endif
  208. static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
  209. struct ftrace_func_probe {
  210. struct hlist_node node;
  211. struct ftrace_probe_ops *ops;
  212. unsigned long flags;
  213. unsigned long ip;
  214. void *data;
  215. struct rcu_head rcu;
  216. };
  217. enum {
  218. FTRACE_ENABLE_CALLS = (1 << 0),
  219. FTRACE_DISABLE_CALLS = (1 << 1),
  220. FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
  221. FTRACE_ENABLE_MCOUNT = (1 << 3),
  222. FTRACE_DISABLE_MCOUNT = (1 << 4),
  223. FTRACE_START_FUNC_RET = (1 << 5),
  224. FTRACE_STOP_FUNC_RET = (1 << 6),
  225. };
  226. static int ftrace_filtered;
  227. static struct dyn_ftrace *ftrace_new_addrs;
  228. static DEFINE_MUTEX(ftrace_regex_lock);
  229. struct ftrace_page {
  230. struct ftrace_page *next;
  231. int index;
  232. struct dyn_ftrace records[];
  233. };
  234. #define ENTRIES_PER_PAGE \
  235. ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
  236. /* estimate from running different kernels */
  237. #define NR_TO_INIT 10000
  238. static struct ftrace_page *ftrace_pages_start;
  239. static struct ftrace_page *ftrace_pages;
  240. static struct dyn_ftrace *ftrace_free_records;
  241. /*
  242. * This is a double for. Do not use 'break' to break out of the loop,
  243. * you must use a goto.
  244. */
  245. #define do_for_each_ftrace_rec(pg, rec) \
  246. for (pg = ftrace_pages_start; pg; pg = pg->next) { \
  247. int _____i; \
  248. for (_____i = 0; _____i < pg->index; _____i++) { \
  249. rec = &pg->records[_____i];
  250. #define while_for_each_ftrace_rec() \
  251. } \
  252. }
  253. #ifdef CONFIG_KPROBES
  254. static int frozen_record_count;
  255. static inline void freeze_record(struct dyn_ftrace *rec)
  256. {
  257. if (!(rec->flags & FTRACE_FL_FROZEN)) {
  258. rec->flags |= FTRACE_FL_FROZEN;
  259. frozen_record_count++;
  260. }
  261. }
  262. static inline void unfreeze_record(struct dyn_ftrace *rec)
  263. {
  264. if (rec->flags & FTRACE_FL_FROZEN) {
  265. rec->flags &= ~FTRACE_FL_FROZEN;
  266. frozen_record_count--;
  267. }
  268. }
  269. static inline int record_frozen(struct dyn_ftrace *rec)
  270. {
  271. return rec->flags & FTRACE_FL_FROZEN;
  272. }
  273. #else
  274. # define freeze_record(rec) ({ 0; })
  275. # define unfreeze_record(rec) ({ 0; })
  276. # define record_frozen(rec) ({ 0; })
  277. #endif /* CONFIG_KPROBES */
  278. static void ftrace_free_rec(struct dyn_ftrace *rec)
  279. {
  280. rec->freelist = ftrace_free_records;
  281. ftrace_free_records = rec;
  282. rec->flags |= FTRACE_FL_FREE;
  283. }
  284. void ftrace_release(void *start, unsigned long size)
  285. {
  286. struct dyn_ftrace *rec;
  287. struct ftrace_page *pg;
  288. unsigned long s = (unsigned long)start;
  289. unsigned long e = s + size;
  290. if (ftrace_disabled || !start)
  291. return;
  292. mutex_lock(&ftrace_lock);
  293. do_for_each_ftrace_rec(pg, rec) {
  294. if ((rec->ip >= s) && (rec->ip < e)) {
  295. /*
  296. * rec->ip is changed in ftrace_free_rec()
  297. * It should not between s and e if record was freed.
  298. */
  299. FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
  300. ftrace_free_rec(rec);
  301. }
  302. } while_for_each_ftrace_rec();
  303. mutex_unlock(&ftrace_lock);
  304. }
  305. static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
  306. {
  307. struct dyn_ftrace *rec;
  308. /* First check for freed records */
  309. if (ftrace_free_records) {
  310. rec = ftrace_free_records;
  311. if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
  312. FTRACE_WARN_ON_ONCE(1);
  313. ftrace_free_records = NULL;
  314. return NULL;
  315. }
  316. ftrace_free_records = rec->freelist;
  317. memset(rec, 0, sizeof(*rec));
  318. return rec;
  319. }
  320. if (ftrace_pages->index == ENTRIES_PER_PAGE) {
  321. if (!ftrace_pages->next) {
  322. /* allocate another page */
  323. ftrace_pages->next =
  324. (void *)get_zeroed_page(GFP_KERNEL);
  325. if (!ftrace_pages->next)
  326. return NULL;
  327. }
  328. ftrace_pages = ftrace_pages->next;
  329. }
  330. return &ftrace_pages->records[ftrace_pages->index++];
  331. }
  332. static struct dyn_ftrace *
  333. ftrace_record_ip(unsigned long ip)
  334. {
  335. struct dyn_ftrace *rec;
  336. if (ftrace_disabled)
  337. return NULL;
  338. rec = ftrace_alloc_dyn_node(ip);
  339. if (!rec)
  340. return NULL;
  341. rec->ip = ip;
  342. rec->newlist = ftrace_new_addrs;
  343. ftrace_new_addrs = rec;
  344. return rec;
  345. }
  346. static void print_ip_ins(const char *fmt, unsigned char *p)
  347. {
  348. int i;
  349. printk(KERN_CONT "%s", fmt);
  350. for (i = 0; i < MCOUNT_INSN_SIZE; i++)
  351. printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
  352. }
  353. static void ftrace_bug(int failed, unsigned long ip)
  354. {
  355. switch (failed) {
  356. case -EFAULT:
  357. FTRACE_WARN_ON_ONCE(1);
  358. pr_info("ftrace faulted on modifying ");
  359. print_ip_sym(ip);
  360. break;
  361. case -EINVAL:
  362. FTRACE_WARN_ON_ONCE(1);
  363. pr_info("ftrace failed to modify ");
  364. print_ip_sym(ip);
  365. print_ip_ins(" actual: ", (unsigned char *)ip);
  366. printk(KERN_CONT "\n");
  367. break;
  368. case -EPERM:
  369. FTRACE_WARN_ON_ONCE(1);
  370. pr_info("ftrace faulted on writing ");
  371. print_ip_sym(ip);
  372. break;
  373. default:
  374. FTRACE_WARN_ON_ONCE(1);
  375. pr_info("ftrace faulted on unknown error ");
  376. print_ip_sym(ip);
  377. }
  378. }
  379. static int
  380. __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
  381. {
  382. unsigned long ftrace_addr;
  383. unsigned long ip, fl;
  384. ftrace_addr = (unsigned long)FTRACE_ADDR;
  385. ip = rec->ip;
  386. /*
  387. * If this record is not to be traced and
  388. * it is not enabled then do nothing.
  389. *
  390. * If this record is not to be traced and
  391. * it is enabled then disable it.
  392. *
  393. */
  394. if (rec->flags & FTRACE_FL_NOTRACE) {
  395. if (rec->flags & FTRACE_FL_ENABLED)
  396. rec->flags &= ~FTRACE_FL_ENABLED;
  397. else
  398. return 0;
  399. } else if (ftrace_filtered && enable) {
  400. /*
  401. * Filtering is on:
  402. */
  403. fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
  404. /* Record is filtered and enabled, do nothing */
  405. if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
  406. return 0;
  407. /* Record is not filtered or enabled, do nothing */
  408. if (!fl)
  409. return 0;
  410. /* Record is not filtered but enabled, disable it */
  411. if (fl == FTRACE_FL_ENABLED)
  412. rec->flags &= ~FTRACE_FL_ENABLED;
  413. else
  414. /* Otherwise record is filtered but not enabled, enable it */
  415. rec->flags |= FTRACE_FL_ENABLED;
  416. } else {
  417. /* Disable or not filtered */
  418. if (enable) {
  419. /* if record is enabled, do nothing */
  420. if (rec->flags & FTRACE_FL_ENABLED)
  421. return 0;
  422. rec->flags |= FTRACE_FL_ENABLED;
  423. } else {
  424. /* if record is not enabled, do nothing */
  425. if (!(rec->flags & FTRACE_FL_ENABLED))
  426. return 0;
  427. rec->flags &= ~FTRACE_FL_ENABLED;
  428. }
  429. }
  430. if (rec->flags & FTRACE_FL_ENABLED)
  431. return ftrace_make_call(rec, ftrace_addr);
  432. else
  433. return ftrace_make_nop(NULL, rec, ftrace_addr);
  434. }
  435. static void ftrace_replace_code(int enable)
  436. {
  437. struct dyn_ftrace *rec;
  438. struct ftrace_page *pg;
  439. int failed;
  440. do_for_each_ftrace_rec(pg, rec) {
  441. /*
  442. * Skip over free records, records that have
  443. * failed and not converted.
  444. */
  445. if (rec->flags & FTRACE_FL_FREE ||
  446. rec->flags & FTRACE_FL_FAILED ||
  447. !(rec->flags & FTRACE_FL_CONVERTED))
  448. continue;
  449. /* ignore updates to this record's mcount site */
  450. if (get_kprobe((void *)rec->ip)) {
  451. freeze_record(rec);
  452. continue;
  453. } else {
  454. unfreeze_record(rec);
  455. }
  456. failed = __ftrace_replace_code(rec, enable);
  457. if (failed) {
  458. rec->flags |= FTRACE_FL_FAILED;
  459. if ((system_state == SYSTEM_BOOTING) ||
  460. !core_kernel_text(rec->ip)) {
  461. ftrace_free_rec(rec);
  462. } else {
  463. ftrace_bug(failed, rec->ip);
  464. /* Stop processing */
  465. return;
  466. }
  467. }
  468. } while_for_each_ftrace_rec();
  469. }
  470. static int
  471. ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
  472. {
  473. unsigned long ip;
  474. int ret;
  475. ip = rec->ip;
  476. ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
  477. if (ret) {
  478. ftrace_bug(ret, ip);
  479. rec->flags |= FTRACE_FL_FAILED;
  480. return 0;
  481. }
  482. return 1;
  483. }
  484. /*
  485. * archs can override this function if they must do something
  486. * before the modifying code is performed.
  487. */
  488. int __weak ftrace_arch_code_modify_prepare(void)
  489. {
  490. return 0;
  491. }
  492. /*
  493. * archs can override this function if they must do something
  494. * after the modifying code is performed.
  495. */
  496. int __weak ftrace_arch_code_modify_post_process(void)
  497. {
  498. return 0;
  499. }
  500. static int __ftrace_modify_code(void *data)
  501. {
  502. int *command = data;
  503. if (*command & FTRACE_ENABLE_CALLS)
  504. ftrace_replace_code(1);
  505. else if (*command & FTRACE_DISABLE_CALLS)
  506. ftrace_replace_code(0);
  507. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  508. ftrace_update_ftrace_func(ftrace_trace_function);
  509. if (*command & FTRACE_START_FUNC_RET)
  510. ftrace_enable_ftrace_graph_caller();
  511. else if (*command & FTRACE_STOP_FUNC_RET)
  512. ftrace_disable_ftrace_graph_caller();
  513. return 0;
  514. }
  515. static void ftrace_run_update_code(int command)
  516. {
  517. int ret;
  518. ret = ftrace_arch_code_modify_prepare();
  519. FTRACE_WARN_ON(ret);
  520. if (ret)
  521. return;
  522. stop_machine(__ftrace_modify_code, &command, NULL);
  523. ret = ftrace_arch_code_modify_post_process();
  524. FTRACE_WARN_ON(ret);
  525. }
  526. static ftrace_func_t saved_ftrace_func;
  527. static int ftrace_start_up;
  528. static void ftrace_startup_enable(int command)
  529. {
  530. if (saved_ftrace_func != ftrace_trace_function) {
  531. saved_ftrace_func = ftrace_trace_function;
  532. command |= FTRACE_UPDATE_TRACE_FUNC;
  533. }
  534. if (!command || !ftrace_enabled)
  535. return;
  536. ftrace_run_update_code(command);
  537. }
  538. static void ftrace_startup(int command)
  539. {
  540. if (unlikely(ftrace_disabled))
  541. return;
  542. ftrace_start_up++;
  543. command |= FTRACE_ENABLE_CALLS;
  544. ftrace_startup_enable(command);
  545. }
  546. static void ftrace_shutdown(int command)
  547. {
  548. if (unlikely(ftrace_disabled))
  549. return;
  550. ftrace_start_up--;
  551. if (!ftrace_start_up)
  552. command |= FTRACE_DISABLE_CALLS;
  553. if (saved_ftrace_func != ftrace_trace_function) {
  554. saved_ftrace_func = ftrace_trace_function;
  555. command |= FTRACE_UPDATE_TRACE_FUNC;
  556. }
  557. if (!command || !ftrace_enabled)
  558. return;
  559. ftrace_run_update_code(command);
  560. }
  561. static void ftrace_startup_sysctl(void)
  562. {
  563. int command = FTRACE_ENABLE_MCOUNT;
  564. if (unlikely(ftrace_disabled))
  565. return;
  566. /* Force update next time */
  567. saved_ftrace_func = NULL;
  568. /* ftrace_start_up is true if we want ftrace running */
  569. if (ftrace_start_up)
  570. command |= FTRACE_ENABLE_CALLS;
  571. ftrace_run_update_code(command);
  572. }
  573. static void ftrace_shutdown_sysctl(void)
  574. {
  575. int command = FTRACE_DISABLE_MCOUNT;
  576. if (unlikely(ftrace_disabled))
  577. return;
  578. /* ftrace_start_up is true if ftrace is running */
  579. if (ftrace_start_up)
  580. command |= FTRACE_DISABLE_CALLS;
  581. ftrace_run_update_code(command);
  582. }
  583. static cycle_t ftrace_update_time;
  584. static unsigned long ftrace_update_cnt;
  585. unsigned long ftrace_update_tot_cnt;
  586. static int ftrace_update_code(struct module *mod)
  587. {
  588. struct dyn_ftrace *p;
  589. cycle_t start, stop;
  590. start = ftrace_now(raw_smp_processor_id());
  591. ftrace_update_cnt = 0;
  592. while (ftrace_new_addrs) {
  593. /* If something went wrong, bail without enabling anything */
  594. if (unlikely(ftrace_disabled))
  595. return -1;
  596. p = ftrace_new_addrs;
  597. ftrace_new_addrs = p->newlist;
  598. p->flags = 0L;
  599. /* convert record (i.e, patch mcount-call with NOP) */
  600. if (ftrace_code_disable(mod, p)) {
  601. p->flags |= FTRACE_FL_CONVERTED;
  602. ftrace_update_cnt++;
  603. } else
  604. ftrace_free_rec(p);
  605. }
  606. stop = ftrace_now(raw_smp_processor_id());
  607. ftrace_update_time = stop - start;
  608. ftrace_update_tot_cnt += ftrace_update_cnt;
  609. return 0;
  610. }
  611. static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
  612. {
  613. struct ftrace_page *pg;
  614. int cnt;
  615. int i;
  616. /* allocate a few pages */
  617. ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
  618. if (!ftrace_pages_start)
  619. return -1;
  620. /*
  621. * Allocate a few more pages.
  622. *
  623. * TODO: have some parser search vmlinux before
  624. * final linking to find all calls to ftrace.
  625. * Then we can:
  626. * a) know how many pages to allocate.
  627. * and/or
  628. * b) set up the table then.
  629. *
  630. * The dynamic code is still necessary for
  631. * modules.
  632. */
  633. pg = ftrace_pages = ftrace_pages_start;
  634. cnt = num_to_init / ENTRIES_PER_PAGE;
  635. pr_info("ftrace: allocating %ld entries in %d pages\n",
  636. num_to_init, cnt + 1);
  637. for (i = 0; i < cnt; i++) {
  638. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  639. /* If we fail, we'll try later anyway */
  640. if (!pg->next)
  641. break;
  642. pg = pg->next;
  643. }
  644. return 0;
  645. }
  646. enum {
  647. FTRACE_ITER_FILTER = (1 << 0),
  648. FTRACE_ITER_CONT = (1 << 1),
  649. FTRACE_ITER_NOTRACE = (1 << 2),
  650. FTRACE_ITER_FAILURES = (1 << 3),
  651. FTRACE_ITER_PRINTALL = (1 << 4),
  652. FTRACE_ITER_HASH = (1 << 5),
  653. };
  654. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  655. struct ftrace_iterator {
  656. struct ftrace_page *pg;
  657. int hidx;
  658. int idx;
  659. unsigned flags;
  660. unsigned char buffer[FTRACE_BUFF_MAX+1];
  661. unsigned buffer_idx;
  662. unsigned filtered;
  663. };
  664. static void *
  665. t_hash_next(struct seq_file *m, void *v, loff_t *pos)
  666. {
  667. struct ftrace_iterator *iter = m->private;
  668. struct hlist_node *hnd = v;
  669. struct hlist_head *hhd;
  670. WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
  671. (*pos)++;
  672. retry:
  673. if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
  674. return NULL;
  675. hhd = &ftrace_func_hash[iter->hidx];
  676. if (hlist_empty(hhd)) {
  677. iter->hidx++;
  678. hnd = NULL;
  679. goto retry;
  680. }
  681. if (!hnd)
  682. hnd = hhd->first;
  683. else {
  684. hnd = hnd->next;
  685. if (!hnd) {
  686. iter->hidx++;
  687. goto retry;
  688. }
  689. }
  690. return hnd;
  691. }
  692. static void *t_hash_start(struct seq_file *m, loff_t *pos)
  693. {
  694. struct ftrace_iterator *iter = m->private;
  695. void *p = NULL;
  696. iter->flags |= FTRACE_ITER_HASH;
  697. return t_hash_next(m, p, pos);
  698. }
  699. static int t_hash_show(struct seq_file *m, void *v)
  700. {
  701. struct ftrace_func_probe *rec;
  702. struct hlist_node *hnd = v;
  703. char str[KSYM_SYMBOL_LEN];
  704. rec = hlist_entry(hnd, struct ftrace_func_probe, node);
  705. if (rec->ops->print)
  706. return rec->ops->print(m, rec->ip, rec->ops, rec->data);
  707. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  708. seq_printf(m, "%s:", str);
  709. kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
  710. seq_printf(m, "%s", str);
  711. if (rec->data)
  712. seq_printf(m, ":%p", rec->data);
  713. seq_putc(m, '\n');
  714. return 0;
  715. }
  716. static void *
  717. t_next(struct seq_file *m, void *v, loff_t *pos)
  718. {
  719. struct ftrace_iterator *iter = m->private;
  720. struct dyn_ftrace *rec = NULL;
  721. if (iter->flags & FTRACE_ITER_HASH)
  722. return t_hash_next(m, v, pos);
  723. (*pos)++;
  724. if (iter->flags & FTRACE_ITER_PRINTALL)
  725. return NULL;
  726. retry:
  727. if (iter->idx >= iter->pg->index) {
  728. if (iter->pg->next) {
  729. iter->pg = iter->pg->next;
  730. iter->idx = 0;
  731. goto retry;
  732. } else {
  733. iter->idx = -1;
  734. }
  735. } else {
  736. rec = &iter->pg->records[iter->idx++];
  737. if ((rec->flags & FTRACE_FL_FREE) ||
  738. (!(iter->flags & FTRACE_ITER_FAILURES) &&
  739. (rec->flags & FTRACE_FL_FAILED)) ||
  740. ((iter->flags & FTRACE_ITER_FAILURES) &&
  741. !(rec->flags & FTRACE_FL_FAILED)) ||
  742. ((iter->flags & FTRACE_ITER_FILTER) &&
  743. !(rec->flags & FTRACE_FL_FILTER)) ||
  744. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  745. !(rec->flags & FTRACE_FL_NOTRACE))) {
  746. rec = NULL;
  747. goto retry;
  748. }
  749. }
  750. return rec;
  751. }
  752. static void *t_start(struct seq_file *m, loff_t *pos)
  753. {
  754. struct ftrace_iterator *iter = m->private;
  755. void *p = NULL;
  756. mutex_lock(&ftrace_lock);
  757. /*
  758. * For set_ftrace_filter reading, if we have the filter
  759. * off, we can short cut and just print out that all
  760. * functions are enabled.
  761. */
  762. if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
  763. if (*pos > 0)
  764. return t_hash_start(m, pos);
  765. iter->flags |= FTRACE_ITER_PRINTALL;
  766. (*pos)++;
  767. return iter;
  768. }
  769. if (iter->flags & FTRACE_ITER_HASH)
  770. return t_hash_start(m, pos);
  771. if (*pos > 0) {
  772. if (iter->idx < 0)
  773. return p;
  774. (*pos)--;
  775. iter->idx--;
  776. }
  777. p = t_next(m, p, pos);
  778. if (!p)
  779. return t_hash_start(m, pos);
  780. return p;
  781. }
  782. static void t_stop(struct seq_file *m, void *p)
  783. {
  784. mutex_unlock(&ftrace_lock);
  785. }
  786. static int t_show(struct seq_file *m, void *v)
  787. {
  788. struct ftrace_iterator *iter = m->private;
  789. struct dyn_ftrace *rec = v;
  790. char str[KSYM_SYMBOL_LEN];
  791. if (iter->flags & FTRACE_ITER_HASH)
  792. return t_hash_show(m, v);
  793. if (iter->flags & FTRACE_ITER_PRINTALL) {
  794. seq_printf(m, "#### all functions enabled ####\n");
  795. return 0;
  796. }
  797. if (!rec)
  798. return 0;
  799. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  800. seq_printf(m, "%s\n", str);
  801. return 0;
  802. }
  803. static struct seq_operations show_ftrace_seq_ops = {
  804. .start = t_start,
  805. .next = t_next,
  806. .stop = t_stop,
  807. .show = t_show,
  808. };
  809. static int
  810. ftrace_avail_open(struct inode *inode, struct file *file)
  811. {
  812. struct ftrace_iterator *iter;
  813. int ret;
  814. if (unlikely(ftrace_disabled))
  815. return -ENODEV;
  816. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  817. if (!iter)
  818. return -ENOMEM;
  819. iter->pg = ftrace_pages_start;
  820. ret = seq_open(file, &show_ftrace_seq_ops);
  821. if (!ret) {
  822. struct seq_file *m = file->private_data;
  823. m->private = iter;
  824. } else {
  825. kfree(iter);
  826. }
  827. return ret;
  828. }
  829. int ftrace_avail_release(struct inode *inode, struct file *file)
  830. {
  831. struct seq_file *m = (struct seq_file *)file->private_data;
  832. struct ftrace_iterator *iter = m->private;
  833. seq_release(inode, file);
  834. kfree(iter);
  835. return 0;
  836. }
  837. static int
  838. ftrace_failures_open(struct inode *inode, struct file *file)
  839. {
  840. int ret;
  841. struct seq_file *m;
  842. struct ftrace_iterator *iter;
  843. ret = ftrace_avail_open(inode, file);
  844. if (!ret) {
  845. m = (struct seq_file *)file->private_data;
  846. iter = (struct ftrace_iterator *)m->private;
  847. iter->flags = FTRACE_ITER_FAILURES;
  848. }
  849. return ret;
  850. }
  851. static void ftrace_filter_reset(int enable)
  852. {
  853. struct ftrace_page *pg;
  854. struct dyn_ftrace *rec;
  855. unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  856. mutex_lock(&ftrace_lock);
  857. if (enable)
  858. ftrace_filtered = 0;
  859. do_for_each_ftrace_rec(pg, rec) {
  860. if (rec->flags & FTRACE_FL_FAILED)
  861. continue;
  862. rec->flags &= ~type;
  863. } while_for_each_ftrace_rec();
  864. mutex_unlock(&ftrace_lock);
  865. }
  866. static int
  867. ftrace_regex_open(struct inode *inode, struct file *file, int enable)
  868. {
  869. struct ftrace_iterator *iter;
  870. int ret = 0;
  871. if (unlikely(ftrace_disabled))
  872. return -ENODEV;
  873. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  874. if (!iter)
  875. return -ENOMEM;
  876. mutex_lock(&ftrace_regex_lock);
  877. if ((file->f_mode & FMODE_WRITE) &&
  878. !(file->f_flags & O_APPEND))
  879. ftrace_filter_reset(enable);
  880. if (file->f_mode & FMODE_READ) {
  881. iter->pg = ftrace_pages_start;
  882. iter->flags = enable ? FTRACE_ITER_FILTER :
  883. FTRACE_ITER_NOTRACE;
  884. ret = seq_open(file, &show_ftrace_seq_ops);
  885. if (!ret) {
  886. struct seq_file *m = file->private_data;
  887. m->private = iter;
  888. } else
  889. kfree(iter);
  890. } else
  891. file->private_data = iter;
  892. mutex_unlock(&ftrace_regex_lock);
  893. return ret;
  894. }
  895. static int
  896. ftrace_filter_open(struct inode *inode, struct file *file)
  897. {
  898. return ftrace_regex_open(inode, file, 1);
  899. }
  900. static int
  901. ftrace_notrace_open(struct inode *inode, struct file *file)
  902. {
  903. return ftrace_regex_open(inode, file, 0);
  904. }
  905. static loff_t
  906. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  907. {
  908. loff_t ret;
  909. if (file->f_mode & FMODE_READ)
  910. ret = seq_lseek(file, offset, origin);
  911. else
  912. file->f_pos = ret = 1;
  913. return ret;
  914. }
  915. enum {
  916. MATCH_FULL,
  917. MATCH_FRONT_ONLY,
  918. MATCH_MIDDLE_ONLY,
  919. MATCH_END_ONLY,
  920. };
  921. /*
  922. * (static function - no need for kernel doc)
  923. *
  924. * Pass in a buffer containing a glob and this function will
  925. * set search to point to the search part of the buffer and
  926. * return the type of search it is (see enum above).
  927. * This does modify buff.
  928. *
  929. * Returns enum type.
  930. * search returns the pointer to use for comparison.
  931. * not returns 1 if buff started with a '!'
  932. * 0 otherwise.
  933. */
  934. static int
  935. ftrace_setup_glob(char *buff, int len, char **search, int *not)
  936. {
  937. int type = MATCH_FULL;
  938. int i;
  939. if (buff[0] == '!') {
  940. *not = 1;
  941. buff++;
  942. len--;
  943. } else
  944. *not = 0;
  945. *search = buff;
  946. for (i = 0; i < len; i++) {
  947. if (buff[i] == '*') {
  948. if (!i) {
  949. *search = buff + 1;
  950. type = MATCH_END_ONLY;
  951. } else {
  952. if (type == MATCH_END_ONLY)
  953. type = MATCH_MIDDLE_ONLY;
  954. else
  955. type = MATCH_FRONT_ONLY;
  956. buff[i] = 0;
  957. break;
  958. }
  959. }
  960. }
  961. return type;
  962. }
  963. static int ftrace_match(char *str, char *regex, int len, int type)
  964. {
  965. int matched = 0;
  966. char *ptr;
  967. switch (type) {
  968. case MATCH_FULL:
  969. if (strcmp(str, regex) == 0)
  970. matched = 1;
  971. break;
  972. case MATCH_FRONT_ONLY:
  973. if (strncmp(str, regex, len) == 0)
  974. matched = 1;
  975. break;
  976. case MATCH_MIDDLE_ONLY:
  977. if (strstr(str, regex))
  978. matched = 1;
  979. break;
  980. case MATCH_END_ONLY:
  981. ptr = strstr(str, regex);
  982. if (ptr && (ptr[len] == 0))
  983. matched = 1;
  984. break;
  985. }
  986. return matched;
  987. }
  988. static int
  989. ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
  990. {
  991. char str[KSYM_SYMBOL_LEN];
  992. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  993. return ftrace_match(str, regex, len, type);
  994. }
  995. static void ftrace_match_records(char *buff, int len, int enable)
  996. {
  997. unsigned int search_len;
  998. struct ftrace_page *pg;
  999. struct dyn_ftrace *rec;
  1000. unsigned long flag;
  1001. char *search;
  1002. int type;
  1003. int not;
  1004. flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1005. type = ftrace_setup_glob(buff, len, &search, &not);
  1006. search_len = strlen(search);
  1007. mutex_lock(&ftrace_lock);
  1008. do_for_each_ftrace_rec(pg, rec) {
  1009. if (rec->flags & FTRACE_FL_FAILED)
  1010. continue;
  1011. if (ftrace_match_record(rec, search, search_len, type)) {
  1012. if (not)
  1013. rec->flags &= ~flag;
  1014. else
  1015. rec->flags |= flag;
  1016. }
  1017. /*
  1018. * Only enable filtering if we have a function that
  1019. * is filtered on.
  1020. */
  1021. if (enable && (rec->flags & FTRACE_FL_FILTER))
  1022. ftrace_filtered = 1;
  1023. } while_for_each_ftrace_rec();
  1024. mutex_unlock(&ftrace_lock);
  1025. }
  1026. static int
  1027. ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
  1028. char *regex, int len, int type)
  1029. {
  1030. char str[KSYM_SYMBOL_LEN];
  1031. char *modname;
  1032. kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
  1033. if (!modname || strcmp(modname, mod))
  1034. return 0;
  1035. /* blank search means to match all funcs in the mod */
  1036. if (len)
  1037. return ftrace_match(str, regex, len, type);
  1038. else
  1039. return 1;
  1040. }
  1041. static void ftrace_match_module_records(char *buff, char *mod, int enable)
  1042. {
  1043. unsigned search_len = 0;
  1044. struct ftrace_page *pg;
  1045. struct dyn_ftrace *rec;
  1046. int type = MATCH_FULL;
  1047. char *search = buff;
  1048. unsigned long flag;
  1049. int not = 0;
  1050. flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1051. /* blank or '*' mean the same */
  1052. if (strcmp(buff, "*") == 0)
  1053. buff[0] = 0;
  1054. /* handle the case of 'dont filter this module' */
  1055. if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
  1056. buff[0] = 0;
  1057. not = 1;
  1058. }
  1059. if (strlen(buff)) {
  1060. type = ftrace_setup_glob(buff, strlen(buff), &search, &not);
  1061. search_len = strlen(search);
  1062. }
  1063. mutex_lock(&ftrace_lock);
  1064. do_for_each_ftrace_rec(pg, rec) {
  1065. if (rec->flags & FTRACE_FL_FAILED)
  1066. continue;
  1067. if (ftrace_match_module_record(rec, mod,
  1068. search, search_len, type)) {
  1069. if (not)
  1070. rec->flags &= ~flag;
  1071. else
  1072. rec->flags |= flag;
  1073. }
  1074. if (enable && (rec->flags & FTRACE_FL_FILTER))
  1075. ftrace_filtered = 1;
  1076. } while_for_each_ftrace_rec();
  1077. mutex_unlock(&ftrace_lock);
  1078. }
  1079. /*
  1080. * We register the module command as a template to show others how
  1081. * to register the a command as well.
  1082. */
  1083. static int
  1084. ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
  1085. {
  1086. char *mod;
  1087. /*
  1088. * cmd == 'mod' because we only registered this func
  1089. * for the 'mod' ftrace_func_command.
  1090. * But if you register one func with multiple commands,
  1091. * you can tell which command was used by the cmd
  1092. * parameter.
  1093. */
  1094. /* we must have a module name */
  1095. if (!param)
  1096. return -EINVAL;
  1097. mod = strsep(&param, ":");
  1098. if (!strlen(mod))
  1099. return -EINVAL;
  1100. ftrace_match_module_records(func, mod, enable);
  1101. return 0;
  1102. }
  1103. static struct ftrace_func_command ftrace_mod_cmd = {
  1104. .name = "mod",
  1105. .func = ftrace_mod_callback,
  1106. };
  1107. static int __init ftrace_mod_cmd_init(void)
  1108. {
  1109. return register_ftrace_command(&ftrace_mod_cmd);
  1110. }
  1111. device_initcall(ftrace_mod_cmd_init);
  1112. static void
  1113. function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
  1114. {
  1115. struct ftrace_func_probe *entry;
  1116. struct hlist_head *hhd;
  1117. struct hlist_node *n;
  1118. unsigned long key;
  1119. int resched;
  1120. key = hash_long(ip, FTRACE_HASH_BITS);
  1121. hhd = &ftrace_func_hash[key];
  1122. if (hlist_empty(hhd))
  1123. return;
  1124. /*
  1125. * Disable preemption for these calls to prevent a RCU grace
  1126. * period. This syncs the hash iteration and freeing of items
  1127. * on the hash. rcu_read_lock is too dangerous here.
  1128. */
  1129. resched = ftrace_preempt_disable();
  1130. hlist_for_each_entry_rcu(entry, n, hhd, node) {
  1131. if (entry->ip == ip)
  1132. entry->ops->func(ip, parent_ip, &entry->data);
  1133. }
  1134. ftrace_preempt_enable(resched);
  1135. }
  1136. static struct ftrace_ops trace_probe_ops __read_mostly =
  1137. {
  1138. .func = function_trace_probe_call,
  1139. };
  1140. static int ftrace_probe_registered;
  1141. static void __enable_ftrace_function_probe(void)
  1142. {
  1143. int i;
  1144. if (ftrace_probe_registered)
  1145. return;
  1146. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1147. struct hlist_head *hhd = &ftrace_func_hash[i];
  1148. if (hhd->first)
  1149. break;
  1150. }
  1151. /* Nothing registered? */
  1152. if (i == FTRACE_FUNC_HASHSIZE)
  1153. return;
  1154. __register_ftrace_function(&trace_probe_ops);
  1155. ftrace_startup(0);
  1156. ftrace_probe_registered = 1;
  1157. }
  1158. static void __disable_ftrace_function_probe(void)
  1159. {
  1160. int i;
  1161. if (!ftrace_probe_registered)
  1162. return;
  1163. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1164. struct hlist_head *hhd = &ftrace_func_hash[i];
  1165. if (hhd->first)
  1166. return;
  1167. }
  1168. /* no more funcs left */
  1169. __unregister_ftrace_function(&trace_probe_ops);
  1170. ftrace_shutdown(0);
  1171. ftrace_probe_registered = 0;
  1172. }
  1173. static void ftrace_free_entry_rcu(struct rcu_head *rhp)
  1174. {
  1175. struct ftrace_func_probe *entry =
  1176. container_of(rhp, struct ftrace_func_probe, rcu);
  1177. if (entry->ops->free)
  1178. entry->ops->free(&entry->data);
  1179. kfree(entry);
  1180. }
  1181. int
  1182. register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1183. void *data)
  1184. {
  1185. struct ftrace_func_probe *entry;
  1186. struct ftrace_page *pg;
  1187. struct dyn_ftrace *rec;
  1188. int type, len, not;
  1189. unsigned long key;
  1190. int count = 0;
  1191. char *search;
  1192. type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
  1193. len = strlen(search);
  1194. /* we do not support '!' for function probes */
  1195. if (WARN_ON(not))
  1196. return -EINVAL;
  1197. mutex_lock(&ftrace_lock);
  1198. do_for_each_ftrace_rec(pg, rec) {
  1199. if (rec->flags & FTRACE_FL_FAILED)
  1200. continue;
  1201. if (!ftrace_match_record(rec, search, len, type))
  1202. continue;
  1203. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  1204. if (!entry) {
  1205. /* If we did not process any, then return error */
  1206. if (!count)
  1207. count = -ENOMEM;
  1208. goto out_unlock;
  1209. }
  1210. count++;
  1211. entry->data = data;
  1212. /*
  1213. * The caller might want to do something special
  1214. * for each function we find. We call the callback
  1215. * to give the caller an opportunity to do so.
  1216. */
  1217. if (ops->callback) {
  1218. if (ops->callback(rec->ip, &entry->data) < 0) {
  1219. /* caller does not like this func */
  1220. kfree(entry);
  1221. continue;
  1222. }
  1223. }
  1224. entry->ops = ops;
  1225. entry->ip = rec->ip;
  1226. key = hash_long(entry->ip, FTRACE_HASH_BITS);
  1227. hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
  1228. } while_for_each_ftrace_rec();
  1229. __enable_ftrace_function_probe();
  1230. out_unlock:
  1231. mutex_unlock(&ftrace_lock);
  1232. return count;
  1233. }
  1234. enum {
  1235. PROBE_TEST_FUNC = 1,
  1236. PROBE_TEST_DATA = 2
  1237. };
  1238. static void
  1239. __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1240. void *data, int flags)
  1241. {
  1242. struct ftrace_func_probe *entry;
  1243. struct hlist_node *n, *tmp;
  1244. char str[KSYM_SYMBOL_LEN];
  1245. int type = MATCH_FULL;
  1246. int i, len = 0;
  1247. char *search;
  1248. if (glob && (strcmp(glob, "*") || !strlen(glob)))
  1249. glob = NULL;
  1250. else {
  1251. int not;
  1252. type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
  1253. len = strlen(search);
  1254. /* we do not support '!' for function probes */
  1255. if (WARN_ON(not))
  1256. return;
  1257. }
  1258. mutex_lock(&ftrace_lock);
  1259. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1260. struct hlist_head *hhd = &ftrace_func_hash[i];
  1261. hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
  1262. /* break up if statements for readability */
  1263. if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
  1264. continue;
  1265. if ((flags & PROBE_TEST_DATA) && entry->data != data)
  1266. continue;
  1267. /* do this last, since it is the most expensive */
  1268. if (glob) {
  1269. kallsyms_lookup(entry->ip, NULL, NULL,
  1270. NULL, str);
  1271. if (!ftrace_match(str, glob, len, type))
  1272. continue;
  1273. }
  1274. hlist_del(&entry->node);
  1275. call_rcu(&entry->rcu, ftrace_free_entry_rcu);
  1276. }
  1277. }
  1278. __disable_ftrace_function_probe();
  1279. mutex_unlock(&ftrace_lock);
  1280. }
  1281. void
  1282. unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1283. void *data)
  1284. {
  1285. __unregister_ftrace_function_probe(glob, ops, data,
  1286. PROBE_TEST_FUNC | PROBE_TEST_DATA);
  1287. }
  1288. void
  1289. unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
  1290. {
  1291. __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
  1292. }
  1293. void unregister_ftrace_function_probe_all(char *glob)
  1294. {
  1295. __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
  1296. }
  1297. static LIST_HEAD(ftrace_commands);
  1298. static DEFINE_MUTEX(ftrace_cmd_mutex);
  1299. int register_ftrace_command(struct ftrace_func_command *cmd)
  1300. {
  1301. struct ftrace_func_command *p;
  1302. int ret = 0;
  1303. mutex_lock(&ftrace_cmd_mutex);
  1304. list_for_each_entry(p, &ftrace_commands, list) {
  1305. if (strcmp(cmd->name, p->name) == 0) {
  1306. ret = -EBUSY;
  1307. goto out_unlock;
  1308. }
  1309. }
  1310. list_add(&cmd->list, &ftrace_commands);
  1311. out_unlock:
  1312. mutex_unlock(&ftrace_cmd_mutex);
  1313. return ret;
  1314. }
  1315. int unregister_ftrace_command(struct ftrace_func_command *cmd)
  1316. {
  1317. struct ftrace_func_command *p, *n;
  1318. int ret = -ENODEV;
  1319. mutex_lock(&ftrace_cmd_mutex);
  1320. list_for_each_entry_safe(p, n, &ftrace_commands, list) {
  1321. if (strcmp(cmd->name, p->name) == 0) {
  1322. ret = 0;
  1323. list_del_init(&p->list);
  1324. goto out_unlock;
  1325. }
  1326. }
  1327. out_unlock:
  1328. mutex_unlock(&ftrace_cmd_mutex);
  1329. return ret;
  1330. }
  1331. static int ftrace_process_regex(char *buff, int len, int enable)
  1332. {
  1333. char *func, *command, *next = buff;
  1334. struct ftrace_func_command *p;
  1335. int ret = -EINVAL;
  1336. func = strsep(&next, ":");
  1337. if (!next) {
  1338. ftrace_match_records(func, len, enable);
  1339. return 0;
  1340. }
  1341. /* command found */
  1342. command = strsep(&next, ":");
  1343. mutex_lock(&ftrace_cmd_mutex);
  1344. list_for_each_entry(p, &ftrace_commands, list) {
  1345. if (strcmp(p->name, command) == 0) {
  1346. ret = p->func(func, command, next, enable);
  1347. goto out_unlock;
  1348. }
  1349. }
  1350. out_unlock:
  1351. mutex_unlock(&ftrace_cmd_mutex);
  1352. return ret;
  1353. }
  1354. static ssize_t
  1355. ftrace_regex_write(struct file *file, const char __user *ubuf,
  1356. size_t cnt, loff_t *ppos, int enable)
  1357. {
  1358. struct ftrace_iterator *iter;
  1359. char ch;
  1360. size_t read = 0;
  1361. ssize_t ret;
  1362. if (!cnt || cnt < 0)
  1363. return 0;
  1364. mutex_lock(&ftrace_regex_lock);
  1365. if (file->f_mode & FMODE_READ) {
  1366. struct seq_file *m = file->private_data;
  1367. iter = m->private;
  1368. } else
  1369. iter = file->private_data;
  1370. if (!*ppos) {
  1371. iter->flags &= ~FTRACE_ITER_CONT;
  1372. iter->buffer_idx = 0;
  1373. }
  1374. ret = get_user(ch, ubuf++);
  1375. if (ret)
  1376. goto out;
  1377. read++;
  1378. cnt--;
  1379. if (!(iter->flags & ~FTRACE_ITER_CONT)) {
  1380. /* skip white space */
  1381. while (cnt && isspace(ch)) {
  1382. ret = get_user(ch, ubuf++);
  1383. if (ret)
  1384. goto out;
  1385. read++;
  1386. cnt--;
  1387. }
  1388. if (isspace(ch)) {
  1389. file->f_pos += read;
  1390. ret = read;
  1391. goto out;
  1392. }
  1393. iter->buffer_idx = 0;
  1394. }
  1395. while (cnt && !isspace(ch)) {
  1396. if (iter->buffer_idx < FTRACE_BUFF_MAX)
  1397. iter->buffer[iter->buffer_idx++] = ch;
  1398. else {
  1399. ret = -EINVAL;
  1400. goto out;
  1401. }
  1402. ret = get_user(ch, ubuf++);
  1403. if (ret)
  1404. goto out;
  1405. read++;
  1406. cnt--;
  1407. }
  1408. if (isspace(ch)) {
  1409. iter->filtered++;
  1410. iter->buffer[iter->buffer_idx] = 0;
  1411. ret = ftrace_process_regex(iter->buffer,
  1412. iter->buffer_idx, enable);
  1413. if (ret)
  1414. goto out;
  1415. iter->buffer_idx = 0;
  1416. } else
  1417. iter->flags |= FTRACE_ITER_CONT;
  1418. file->f_pos += read;
  1419. ret = read;
  1420. out:
  1421. mutex_unlock(&ftrace_regex_lock);
  1422. return ret;
  1423. }
  1424. static ssize_t
  1425. ftrace_filter_write(struct file *file, const char __user *ubuf,
  1426. size_t cnt, loff_t *ppos)
  1427. {
  1428. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  1429. }
  1430. static ssize_t
  1431. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  1432. size_t cnt, loff_t *ppos)
  1433. {
  1434. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  1435. }
  1436. static void
  1437. ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
  1438. {
  1439. if (unlikely(ftrace_disabled))
  1440. return;
  1441. mutex_lock(&ftrace_regex_lock);
  1442. if (reset)
  1443. ftrace_filter_reset(enable);
  1444. if (buf)
  1445. ftrace_match_records(buf, len, enable);
  1446. mutex_unlock(&ftrace_regex_lock);
  1447. }
  1448. /**
  1449. * ftrace_set_filter - set a function to filter on in ftrace
  1450. * @buf - the string that holds the function filter text.
  1451. * @len - the length of the string.
  1452. * @reset - non zero to reset all filters before applying this filter.
  1453. *
  1454. * Filters denote which functions should be enabled when tracing is enabled.
  1455. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  1456. */
  1457. void ftrace_set_filter(unsigned char *buf, int len, int reset)
  1458. {
  1459. ftrace_set_regex(buf, len, reset, 1);
  1460. }
  1461. /**
  1462. * ftrace_set_notrace - set a function to not trace in ftrace
  1463. * @buf - the string that holds the function notrace text.
  1464. * @len - the length of the string.
  1465. * @reset - non zero to reset all filters before applying this filter.
  1466. *
  1467. * Notrace Filters denote which functions should not be enabled when tracing
  1468. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  1469. * for tracing.
  1470. */
  1471. void ftrace_set_notrace(unsigned char *buf, int len, int reset)
  1472. {
  1473. ftrace_set_regex(buf, len, reset, 0);
  1474. }
  1475. static int
  1476. ftrace_regex_release(struct inode *inode, struct file *file, int enable)
  1477. {
  1478. struct seq_file *m = (struct seq_file *)file->private_data;
  1479. struct ftrace_iterator *iter;
  1480. mutex_lock(&ftrace_regex_lock);
  1481. if (file->f_mode & FMODE_READ) {
  1482. iter = m->private;
  1483. seq_release(inode, file);
  1484. } else
  1485. iter = file->private_data;
  1486. if (iter->buffer_idx) {
  1487. iter->filtered++;
  1488. iter->buffer[iter->buffer_idx] = 0;
  1489. ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
  1490. }
  1491. mutex_lock(&ftrace_lock);
  1492. if (ftrace_start_up && ftrace_enabled)
  1493. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  1494. mutex_unlock(&ftrace_lock);
  1495. kfree(iter);
  1496. mutex_unlock(&ftrace_regex_lock);
  1497. return 0;
  1498. }
  1499. static int
  1500. ftrace_filter_release(struct inode *inode, struct file *file)
  1501. {
  1502. return ftrace_regex_release(inode, file, 1);
  1503. }
  1504. static int
  1505. ftrace_notrace_release(struct inode *inode, struct file *file)
  1506. {
  1507. return ftrace_regex_release(inode, file, 0);
  1508. }
  1509. static const struct file_operations ftrace_avail_fops = {
  1510. .open = ftrace_avail_open,
  1511. .read = seq_read,
  1512. .llseek = seq_lseek,
  1513. .release = ftrace_avail_release,
  1514. };
  1515. static const struct file_operations ftrace_failures_fops = {
  1516. .open = ftrace_failures_open,
  1517. .read = seq_read,
  1518. .llseek = seq_lseek,
  1519. .release = ftrace_avail_release,
  1520. };
  1521. static const struct file_operations ftrace_filter_fops = {
  1522. .open = ftrace_filter_open,
  1523. .read = seq_read,
  1524. .write = ftrace_filter_write,
  1525. .llseek = ftrace_regex_lseek,
  1526. .release = ftrace_filter_release,
  1527. };
  1528. static const struct file_operations ftrace_notrace_fops = {
  1529. .open = ftrace_notrace_open,
  1530. .read = seq_read,
  1531. .write = ftrace_notrace_write,
  1532. .llseek = ftrace_regex_lseek,
  1533. .release = ftrace_notrace_release,
  1534. };
  1535. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1536. static DEFINE_MUTEX(graph_lock);
  1537. int ftrace_graph_count;
  1538. unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
  1539. static void *
  1540. g_next(struct seq_file *m, void *v, loff_t *pos)
  1541. {
  1542. unsigned long *array = m->private;
  1543. int index = *pos;
  1544. (*pos)++;
  1545. if (index >= ftrace_graph_count)
  1546. return NULL;
  1547. return &array[index];
  1548. }
  1549. static void *g_start(struct seq_file *m, loff_t *pos)
  1550. {
  1551. void *p = NULL;
  1552. mutex_lock(&graph_lock);
  1553. /* Nothing, tell g_show to print all functions are enabled */
  1554. if (!ftrace_graph_count && !*pos)
  1555. return (void *)1;
  1556. p = g_next(m, p, pos);
  1557. return p;
  1558. }
  1559. static void g_stop(struct seq_file *m, void *p)
  1560. {
  1561. mutex_unlock(&graph_lock);
  1562. }
  1563. static int g_show(struct seq_file *m, void *v)
  1564. {
  1565. unsigned long *ptr = v;
  1566. char str[KSYM_SYMBOL_LEN];
  1567. if (!ptr)
  1568. return 0;
  1569. if (ptr == (unsigned long *)1) {
  1570. seq_printf(m, "#### all functions enabled ####\n");
  1571. return 0;
  1572. }
  1573. kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
  1574. seq_printf(m, "%s\n", str);
  1575. return 0;
  1576. }
  1577. static struct seq_operations ftrace_graph_seq_ops = {
  1578. .start = g_start,
  1579. .next = g_next,
  1580. .stop = g_stop,
  1581. .show = g_show,
  1582. };
  1583. static int
  1584. ftrace_graph_open(struct inode *inode, struct file *file)
  1585. {
  1586. int ret = 0;
  1587. if (unlikely(ftrace_disabled))
  1588. return -ENODEV;
  1589. mutex_lock(&graph_lock);
  1590. if ((file->f_mode & FMODE_WRITE) &&
  1591. !(file->f_flags & O_APPEND)) {
  1592. ftrace_graph_count = 0;
  1593. memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
  1594. }
  1595. if (file->f_mode & FMODE_READ) {
  1596. ret = seq_open(file, &ftrace_graph_seq_ops);
  1597. if (!ret) {
  1598. struct seq_file *m = file->private_data;
  1599. m->private = ftrace_graph_funcs;
  1600. }
  1601. } else
  1602. file->private_data = ftrace_graph_funcs;
  1603. mutex_unlock(&graph_lock);
  1604. return ret;
  1605. }
  1606. static int
  1607. ftrace_set_func(unsigned long *array, int *idx, char *buffer)
  1608. {
  1609. struct dyn_ftrace *rec;
  1610. struct ftrace_page *pg;
  1611. int search_len;
  1612. int found = 0;
  1613. int type, not;
  1614. char *search;
  1615. bool exists;
  1616. int i;
  1617. if (ftrace_disabled)
  1618. return -ENODEV;
  1619. /* decode regex */
  1620. type = ftrace_setup_glob(buffer, strlen(buffer), &search, &not);
  1621. if (not)
  1622. return -EINVAL;
  1623. search_len = strlen(search);
  1624. mutex_lock(&ftrace_lock);
  1625. do_for_each_ftrace_rec(pg, rec) {
  1626. if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
  1627. break;
  1628. if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
  1629. continue;
  1630. if (ftrace_match_record(rec, search, search_len, type)) {
  1631. /* ensure it is not already in the array */
  1632. exists = false;
  1633. for (i = 0; i < *idx; i++)
  1634. if (array[i] == rec->ip) {
  1635. exists = true;
  1636. break;
  1637. }
  1638. if (!exists) {
  1639. array[(*idx)++] = rec->ip;
  1640. found = 1;
  1641. }
  1642. }
  1643. } while_for_each_ftrace_rec();
  1644. mutex_unlock(&ftrace_lock);
  1645. return found ? 0 : -EINVAL;
  1646. }
  1647. static ssize_t
  1648. ftrace_graph_write(struct file *file, const char __user *ubuf,
  1649. size_t cnt, loff_t *ppos)
  1650. {
  1651. unsigned char buffer[FTRACE_BUFF_MAX+1];
  1652. unsigned long *array;
  1653. size_t read = 0;
  1654. ssize_t ret;
  1655. int index = 0;
  1656. char ch;
  1657. if (!cnt || cnt < 0)
  1658. return 0;
  1659. mutex_lock(&graph_lock);
  1660. if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
  1661. ret = -EBUSY;
  1662. goto out;
  1663. }
  1664. if (file->f_mode & FMODE_READ) {
  1665. struct seq_file *m = file->private_data;
  1666. array = m->private;
  1667. } else
  1668. array = file->private_data;
  1669. ret = get_user(ch, ubuf++);
  1670. if (ret)
  1671. goto out;
  1672. read++;
  1673. cnt--;
  1674. /* skip white space */
  1675. while (cnt && isspace(ch)) {
  1676. ret = get_user(ch, ubuf++);
  1677. if (ret)
  1678. goto out;
  1679. read++;
  1680. cnt--;
  1681. }
  1682. if (isspace(ch)) {
  1683. *ppos += read;
  1684. ret = read;
  1685. goto out;
  1686. }
  1687. while (cnt && !isspace(ch)) {
  1688. if (index < FTRACE_BUFF_MAX)
  1689. buffer[index++] = ch;
  1690. else {
  1691. ret = -EINVAL;
  1692. goto out;
  1693. }
  1694. ret = get_user(ch, ubuf++);
  1695. if (ret)
  1696. goto out;
  1697. read++;
  1698. cnt--;
  1699. }
  1700. buffer[index] = 0;
  1701. /* we allow only one expression at a time */
  1702. ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
  1703. if (ret)
  1704. goto out;
  1705. file->f_pos += read;
  1706. ret = read;
  1707. out:
  1708. mutex_unlock(&graph_lock);
  1709. return ret;
  1710. }
  1711. static const struct file_operations ftrace_graph_fops = {
  1712. .open = ftrace_graph_open,
  1713. .read = seq_read,
  1714. .write = ftrace_graph_write,
  1715. };
  1716. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1717. static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
  1718. {
  1719. struct dentry *entry;
  1720. entry = debugfs_create_file("available_filter_functions", 0444,
  1721. d_tracer, NULL, &ftrace_avail_fops);
  1722. if (!entry)
  1723. pr_warning("Could not create debugfs "
  1724. "'available_filter_functions' entry\n");
  1725. entry = debugfs_create_file("failures", 0444,
  1726. d_tracer, NULL, &ftrace_failures_fops);
  1727. if (!entry)
  1728. pr_warning("Could not create debugfs 'failures' entry\n");
  1729. entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
  1730. NULL, &ftrace_filter_fops);
  1731. if (!entry)
  1732. pr_warning("Could not create debugfs "
  1733. "'set_ftrace_filter' entry\n");
  1734. entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
  1735. NULL, &ftrace_notrace_fops);
  1736. if (!entry)
  1737. pr_warning("Could not create debugfs "
  1738. "'set_ftrace_notrace' entry\n");
  1739. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1740. entry = debugfs_create_file("set_graph_function", 0444, d_tracer,
  1741. NULL,
  1742. &ftrace_graph_fops);
  1743. if (!entry)
  1744. pr_warning("Could not create debugfs "
  1745. "'set_graph_function' entry\n");
  1746. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1747. return 0;
  1748. }
  1749. static int ftrace_convert_nops(struct module *mod,
  1750. unsigned long *start,
  1751. unsigned long *end)
  1752. {
  1753. unsigned long *p;
  1754. unsigned long addr;
  1755. unsigned long flags;
  1756. mutex_lock(&ftrace_lock);
  1757. p = start;
  1758. while (p < end) {
  1759. addr = ftrace_call_adjust(*p++);
  1760. /*
  1761. * Some architecture linkers will pad between
  1762. * the different mcount_loc sections of different
  1763. * object files to satisfy alignments.
  1764. * Skip any NULL pointers.
  1765. */
  1766. if (!addr)
  1767. continue;
  1768. ftrace_record_ip(addr);
  1769. }
  1770. /* disable interrupts to prevent kstop machine */
  1771. local_irq_save(flags);
  1772. ftrace_update_code(mod);
  1773. local_irq_restore(flags);
  1774. mutex_unlock(&ftrace_lock);
  1775. return 0;
  1776. }
  1777. void ftrace_init_module(struct module *mod,
  1778. unsigned long *start, unsigned long *end)
  1779. {
  1780. if (ftrace_disabled || start == end)
  1781. return;
  1782. ftrace_convert_nops(mod, start, end);
  1783. }
  1784. extern unsigned long __start_mcount_loc[];
  1785. extern unsigned long __stop_mcount_loc[];
  1786. void __init ftrace_init(void)
  1787. {
  1788. unsigned long count, addr, flags;
  1789. int ret;
  1790. /* Keep the ftrace pointer to the stub */
  1791. addr = (unsigned long)ftrace_stub;
  1792. local_irq_save(flags);
  1793. ftrace_dyn_arch_init(&addr);
  1794. local_irq_restore(flags);
  1795. /* ftrace_dyn_arch_init places the return code in addr */
  1796. if (addr)
  1797. goto failed;
  1798. count = __stop_mcount_loc - __start_mcount_loc;
  1799. ret = ftrace_dyn_table_alloc(count);
  1800. if (ret)
  1801. goto failed;
  1802. last_ftrace_enabled = ftrace_enabled = 1;
  1803. ret = ftrace_convert_nops(NULL,
  1804. __start_mcount_loc,
  1805. __stop_mcount_loc);
  1806. return;
  1807. failed:
  1808. ftrace_disabled = 1;
  1809. }
  1810. #else
  1811. static int __init ftrace_nodyn_init(void)
  1812. {
  1813. ftrace_enabled = 1;
  1814. return 0;
  1815. }
  1816. device_initcall(ftrace_nodyn_init);
  1817. static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
  1818. static inline void ftrace_startup_enable(int command) { }
  1819. /* Keep as macros so we do not need to define the commands */
  1820. # define ftrace_startup(command) do { } while (0)
  1821. # define ftrace_shutdown(command) do { } while (0)
  1822. # define ftrace_startup_sysctl() do { } while (0)
  1823. # define ftrace_shutdown_sysctl() do { } while (0)
  1824. #endif /* CONFIG_DYNAMIC_FTRACE */
  1825. static ssize_t
  1826. ftrace_pid_read(struct file *file, char __user *ubuf,
  1827. size_t cnt, loff_t *ppos)
  1828. {
  1829. char buf[64];
  1830. int r;
  1831. if (ftrace_pid_trace == ftrace_swapper_pid)
  1832. r = sprintf(buf, "swapper tasks\n");
  1833. else if (ftrace_pid_trace)
  1834. r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
  1835. else
  1836. r = sprintf(buf, "no pid\n");
  1837. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1838. }
  1839. static void clear_ftrace_swapper(void)
  1840. {
  1841. struct task_struct *p;
  1842. int cpu;
  1843. get_online_cpus();
  1844. for_each_online_cpu(cpu) {
  1845. p = idle_task(cpu);
  1846. clear_tsk_trace_trace(p);
  1847. }
  1848. put_online_cpus();
  1849. }
  1850. static void set_ftrace_swapper(void)
  1851. {
  1852. struct task_struct *p;
  1853. int cpu;
  1854. get_online_cpus();
  1855. for_each_online_cpu(cpu) {
  1856. p = idle_task(cpu);
  1857. set_tsk_trace_trace(p);
  1858. }
  1859. put_online_cpus();
  1860. }
  1861. static void clear_ftrace_pid(struct pid *pid)
  1862. {
  1863. struct task_struct *p;
  1864. rcu_read_lock();
  1865. do_each_pid_task(pid, PIDTYPE_PID, p) {
  1866. clear_tsk_trace_trace(p);
  1867. } while_each_pid_task(pid, PIDTYPE_PID, p);
  1868. rcu_read_unlock();
  1869. put_pid(pid);
  1870. }
  1871. static void set_ftrace_pid(struct pid *pid)
  1872. {
  1873. struct task_struct *p;
  1874. rcu_read_lock();
  1875. do_each_pid_task(pid, PIDTYPE_PID, p) {
  1876. set_tsk_trace_trace(p);
  1877. } while_each_pid_task(pid, PIDTYPE_PID, p);
  1878. rcu_read_unlock();
  1879. }
  1880. static void clear_ftrace_pid_task(struct pid **pid)
  1881. {
  1882. if (*pid == ftrace_swapper_pid)
  1883. clear_ftrace_swapper();
  1884. else
  1885. clear_ftrace_pid(*pid);
  1886. *pid = NULL;
  1887. }
  1888. static void set_ftrace_pid_task(struct pid *pid)
  1889. {
  1890. if (pid == ftrace_swapper_pid)
  1891. set_ftrace_swapper();
  1892. else
  1893. set_ftrace_pid(pid);
  1894. }
  1895. static ssize_t
  1896. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  1897. size_t cnt, loff_t *ppos)
  1898. {
  1899. struct pid *pid;
  1900. char buf[64];
  1901. long val;
  1902. int ret;
  1903. if (cnt >= sizeof(buf))
  1904. return -EINVAL;
  1905. if (copy_from_user(&buf, ubuf, cnt))
  1906. return -EFAULT;
  1907. buf[cnt] = 0;
  1908. ret = strict_strtol(buf, 10, &val);
  1909. if (ret < 0)
  1910. return ret;
  1911. mutex_lock(&ftrace_lock);
  1912. if (val < 0) {
  1913. /* disable pid tracing */
  1914. if (!ftrace_pid_trace)
  1915. goto out;
  1916. clear_ftrace_pid_task(&ftrace_pid_trace);
  1917. } else {
  1918. /* swapper task is special */
  1919. if (!val) {
  1920. pid = ftrace_swapper_pid;
  1921. if (pid == ftrace_pid_trace)
  1922. goto out;
  1923. } else {
  1924. pid = find_get_pid(val);
  1925. if (pid == ftrace_pid_trace) {
  1926. put_pid(pid);
  1927. goto out;
  1928. }
  1929. }
  1930. if (ftrace_pid_trace)
  1931. clear_ftrace_pid_task(&ftrace_pid_trace);
  1932. if (!pid)
  1933. goto out;
  1934. ftrace_pid_trace = pid;
  1935. set_ftrace_pid_task(ftrace_pid_trace);
  1936. }
  1937. /* update the function call */
  1938. ftrace_update_pid_func();
  1939. ftrace_startup_enable(0);
  1940. out:
  1941. mutex_unlock(&ftrace_lock);
  1942. return cnt;
  1943. }
  1944. static const struct file_operations ftrace_pid_fops = {
  1945. .read = ftrace_pid_read,
  1946. .write = ftrace_pid_write,
  1947. };
  1948. static __init int ftrace_init_debugfs(void)
  1949. {
  1950. struct dentry *d_tracer;
  1951. struct dentry *entry;
  1952. d_tracer = tracing_init_dentry();
  1953. if (!d_tracer)
  1954. return 0;
  1955. ftrace_init_dyn_debugfs(d_tracer);
  1956. entry = debugfs_create_file("set_ftrace_pid", 0644, d_tracer,
  1957. NULL, &ftrace_pid_fops);
  1958. if (!entry)
  1959. pr_warning("Could not create debugfs "
  1960. "'set_ftrace_pid' entry\n");
  1961. return 0;
  1962. }
  1963. fs_initcall(ftrace_init_debugfs);
  1964. /**
  1965. * ftrace_kill - kill ftrace
  1966. *
  1967. * This function should be used by panic code. It stops ftrace
  1968. * but in a not so nice way. If you need to simply kill ftrace
  1969. * from a non-atomic section, use ftrace_kill.
  1970. */
  1971. void ftrace_kill(void)
  1972. {
  1973. ftrace_disabled = 1;
  1974. ftrace_enabled = 0;
  1975. clear_ftrace_function();
  1976. }
  1977. /**
  1978. * register_ftrace_function - register a function for profiling
  1979. * @ops - ops structure that holds the function for profiling.
  1980. *
  1981. * Register a function to be called by all functions in the
  1982. * kernel.
  1983. *
  1984. * Note: @ops->func and all the functions it calls must be labeled
  1985. * with "notrace", otherwise it will go into a
  1986. * recursive loop.
  1987. */
  1988. int register_ftrace_function(struct ftrace_ops *ops)
  1989. {
  1990. int ret;
  1991. if (unlikely(ftrace_disabled))
  1992. return -1;
  1993. mutex_lock(&ftrace_lock);
  1994. ret = __register_ftrace_function(ops);
  1995. ftrace_startup(0);
  1996. mutex_unlock(&ftrace_lock);
  1997. return ret;
  1998. }
  1999. /**
  2000. * unregister_ftrace_function - unregister a function for profiling.
  2001. * @ops - ops structure that holds the function to unregister
  2002. *
  2003. * Unregister a function that was added to be called by ftrace profiling.
  2004. */
  2005. int unregister_ftrace_function(struct ftrace_ops *ops)
  2006. {
  2007. int ret;
  2008. mutex_lock(&ftrace_lock);
  2009. ret = __unregister_ftrace_function(ops);
  2010. ftrace_shutdown(0);
  2011. mutex_unlock(&ftrace_lock);
  2012. return ret;
  2013. }
  2014. int
  2015. ftrace_enable_sysctl(struct ctl_table *table, int write,
  2016. struct file *file, void __user *buffer, size_t *lenp,
  2017. loff_t *ppos)
  2018. {
  2019. int ret;
  2020. if (unlikely(ftrace_disabled))
  2021. return -ENODEV;
  2022. mutex_lock(&ftrace_lock);
  2023. ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
  2024. if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
  2025. goto out;
  2026. last_ftrace_enabled = ftrace_enabled;
  2027. if (ftrace_enabled) {
  2028. ftrace_startup_sysctl();
  2029. /* we are starting ftrace again */
  2030. if (ftrace_list != &ftrace_list_end) {
  2031. if (ftrace_list->next == &ftrace_list_end)
  2032. ftrace_trace_function = ftrace_list->func;
  2033. else
  2034. ftrace_trace_function = ftrace_list_func;
  2035. }
  2036. } else {
  2037. /* stopping ftrace calls (just send to ftrace_stub) */
  2038. ftrace_trace_function = ftrace_stub;
  2039. ftrace_shutdown_sysctl();
  2040. }
  2041. out:
  2042. mutex_unlock(&ftrace_lock);
  2043. return ret;
  2044. }
  2045. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2046. static atomic_t ftrace_graph_active;
  2047. static struct notifier_block ftrace_suspend_notifier;
  2048. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
  2049. {
  2050. return 0;
  2051. }
  2052. /* The callbacks that hook a function */
  2053. trace_func_graph_ret_t ftrace_graph_return =
  2054. (trace_func_graph_ret_t)ftrace_stub;
  2055. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  2056. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  2057. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  2058. {
  2059. int i;
  2060. int ret = 0;
  2061. unsigned long flags;
  2062. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  2063. struct task_struct *g, *t;
  2064. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  2065. ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
  2066. * sizeof(struct ftrace_ret_stack),
  2067. GFP_KERNEL);
  2068. if (!ret_stack_list[i]) {
  2069. start = 0;
  2070. end = i;
  2071. ret = -ENOMEM;
  2072. goto free;
  2073. }
  2074. }
  2075. read_lock_irqsave(&tasklist_lock, flags);
  2076. do_each_thread(g, t) {
  2077. if (start == end) {
  2078. ret = -EAGAIN;
  2079. goto unlock;
  2080. }
  2081. if (t->ret_stack == NULL) {
  2082. t->curr_ret_stack = -1;
  2083. /* Make sure IRQs see the -1 first: */
  2084. barrier();
  2085. t->ret_stack = ret_stack_list[start++];
  2086. atomic_set(&t->tracing_graph_pause, 0);
  2087. atomic_set(&t->trace_overrun, 0);
  2088. }
  2089. } while_each_thread(g, t);
  2090. unlock:
  2091. read_unlock_irqrestore(&tasklist_lock, flags);
  2092. free:
  2093. for (i = start; i < end; i++)
  2094. kfree(ret_stack_list[i]);
  2095. return ret;
  2096. }
  2097. static void
  2098. ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
  2099. struct task_struct *next)
  2100. {
  2101. unsigned long long timestamp;
  2102. int index;
  2103. /*
  2104. * Does the user want to count the time a function was asleep.
  2105. * If so, do not update the time stamps.
  2106. */
  2107. if (trace_flags & TRACE_ITER_SLEEP_TIME)
  2108. return;
  2109. timestamp = trace_clock_local();
  2110. prev->ftrace_timestamp = timestamp;
  2111. /* only process tasks that we timestamped */
  2112. if (!next->ftrace_timestamp)
  2113. return;
  2114. /*
  2115. * Update all the counters in next to make up for the
  2116. * time next was sleeping.
  2117. */
  2118. timestamp -= next->ftrace_timestamp;
  2119. for (index = next->curr_ret_stack; index >= 0; index--)
  2120. next->ret_stack[index].calltime += timestamp;
  2121. }
  2122. /* Allocate a return stack for each task */
  2123. static int start_graph_tracing(void)
  2124. {
  2125. struct ftrace_ret_stack **ret_stack_list;
  2126. int ret, cpu;
  2127. ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
  2128. sizeof(struct ftrace_ret_stack *),
  2129. GFP_KERNEL);
  2130. if (!ret_stack_list)
  2131. return -ENOMEM;
  2132. /* The cpu_boot init_task->ret_stack will never be freed */
  2133. for_each_online_cpu(cpu)
  2134. ftrace_graph_init_task(idle_task(cpu));
  2135. do {
  2136. ret = alloc_retstack_tasklist(ret_stack_list);
  2137. } while (ret == -EAGAIN);
  2138. if (!ret) {
  2139. ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
  2140. if (ret)
  2141. pr_info("ftrace_graph: Couldn't activate tracepoint"
  2142. " probe to kernel_sched_switch\n");
  2143. }
  2144. kfree(ret_stack_list);
  2145. return ret;
  2146. }
  2147. /*
  2148. * Hibernation protection.
  2149. * The state of the current task is too much unstable during
  2150. * suspend/restore to disk. We want to protect against that.
  2151. */
  2152. static int
  2153. ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
  2154. void *unused)
  2155. {
  2156. switch (state) {
  2157. case PM_HIBERNATION_PREPARE:
  2158. pause_graph_tracing();
  2159. break;
  2160. case PM_POST_HIBERNATION:
  2161. unpause_graph_tracing();
  2162. break;
  2163. }
  2164. return NOTIFY_DONE;
  2165. }
  2166. int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  2167. trace_func_graph_ent_t entryfunc)
  2168. {
  2169. int ret = 0;
  2170. mutex_lock(&ftrace_lock);
  2171. /* we currently allow only one tracer registered at a time */
  2172. if (atomic_read(&ftrace_graph_active)) {
  2173. ret = -EBUSY;
  2174. goto out;
  2175. }
  2176. ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
  2177. register_pm_notifier(&ftrace_suspend_notifier);
  2178. atomic_inc(&ftrace_graph_active);
  2179. ret = start_graph_tracing();
  2180. if (ret) {
  2181. atomic_dec(&ftrace_graph_active);
  2182. goto out;
  2183. }
  2184. ftrace_graph_return = retfunc;
  2185. ftrace_graph_entry = entryfunc;
  2186. ftrace_startup(FTRACE_START_FUNC_RET);
  2187. out:
  2188. mutex_unlock(&ftrace_lock);
  2189. return ret;
  2190. }
  2191. void unregister_ftrace_graph(void)
  2192. {
  2193. mutex_lock(&ftrace_lock);
  2194. atomic_dec(&ftrace_graph_active);
  2195. unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
  2196. ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
  2197. ftrace_graph_entry = ftrace_graph_entry_stub;
  2198. ftrace_shutdown(FTRACE_STOP_FUNC_RET);
  2199. unregister_pm_notifier(&ftrace_suspend_notifier);
  2200. mutex_unlock(&ftrace_lock);
  2201. }
  2202. /* Allocate a return stack for newly created task */
  2203. void ftrace_graph_init_task(struct task_struct *t)
  2204. {
  2205. if (atomic_read(&ftrace_graph_active)) {
  2206. t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  2207. * sizeof(struct ftrace_ret_stack),
  2208. GFP_KERNEL);
  2209. if (!t->ret_stack)
  2210. return;
  2211. t->curr_ret_stack = -1;
  2212. atomic_set(&t->tracing_graph_pause, 0);
  2213. atomic_set(&t->trace_overrun, 0);
  2214. t->ftrace_timestamp = 0;
  2215. } else
  2216. t->ret_stack = NULL;
  2217. }
  2218. void ftrace_graph_exit_task(struct task_struct *t)
  2219. {
  2220. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  2221. t->ret_stack = NULL;
  2222. /* NULL must become visible to IRQs before we free it: */
  2223. barrier();
  2224. kfree(ret_stack);
  2225. }
  2226. void ftrace_graph_stop(void)
  2227. {
  2228. ftrace_stop();
  2229. }
  2230. #endif