ftrace.c 56 KB

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