ftrace.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675
  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_hook {
  212. struct hlist_node node;
  213. struct ftrace_hook_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 ip, fl;
  378. unsigned long ftrace_addr;
  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. int failed;
  433. struct dyn_ftrace *rec;
  434. struct ftrace_page *pg;
  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. }
  459. } while_for_each_ftrace_rec();
  460. }
  461. static int
  462. ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
  463. {
  464. unsigned long ip;
  465. int ret;
  466. ip = rec->ip;
  467. ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
  468. if (ret) {
  469. ftrace_bug(ret, ip);
  470. rec->flags |= FTRACE_FL_FAILED;
  471. return 0;
  472. }
  473. return 1;
  474. }
  475. static int __ftrace_modify_code(void *data)
  476. {
  477. int *command = data;
  478. if (*command & FTRACE_ENABLE_CALLS)
  479. ftrace_replace_code(1);
  480. else if (*command & FTRACE_DISABLE_CALLS)
  481. ftrace_replace_code(0);
  482. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  483. ftrace_update_ftrace_func(ftrace_trace_function);
  484. if (*command & FTRACE_START_FUNC_RET)
  485. ftrace_enable_ftrace_graph_caller();
  486. else if (*command & FTRACE_STOP_FUNC_RET)
  487. ftrace_disable_ftrace_graph_caller();
  488. return 0;
  489. }
  490. static void ftrace_run_update_code(int command)
  491. {
  492. stop_machine(__ftrace_modify_code, &command, NULL);
  493. }
  494. static ftrace_func_t saved_ftrace_func;
  495. static int ftrace_start_up;
  496. static void ftrace_startup_enable(int command)
  497. {
  498. if (saved_ftrace_func != ftrace_trace_function) {
  499. saved_ftrace_func = ftrace_trace_function;
  500. command |= FTRACE_UPDATE_TRACE_FUNC;
  501. }
  502. if (!command || !ftrace_enabled)
  503. return;
  504. ftrace_run_update_code(command);
  505. }
  506. static void ftrace_startup(int command)
  507. {
  508. if (unlikely(ftrace_disabled))
  509. return;
  510. ftrace_start_up++;
  511. command |= FTRACE_ENABLE_CALLS;
  512. ftrace_startup_enable(command);
  513. }
  514. static void ftrace_shutdown(int command)
  515. {
  516. if (unlikely(ftrace_disabled))
  517. return;
  518. ftrace_start_up--;
  519. if (!ftrace_start_up)
  520. command |= FTRACE_DISABLE_CALLS;
  521. if (saved_ftrace_func != ftrace_trace_function) {
  522. saved_ftrace_func = ftrace_trace_function;
  523. command |= FTRACE_UPDATE_TRACE_FUNC;
  524. }
  525. if (!command || !ftrace_enabled)
  526. return;
  527. ftrace_run_update_code(command);
  528. }
  529. static void ftrace_startup_sysctl(void)
  530. {
  531. int command = FTRACE_ENABLE_MCOUNT;
  532. if (unlikely(ftrace_disabled))
  533. return;
  534. /* Force update next time */
  535. saved_ftrace_func = NULL;
  536. /* ftrace_start_up is true if we want ftrace running */
  537. if (ftrace_start_up)
  538. command |= FTRACE_ENABLE_CALLS;
  539. ftrace_run_update_code(command);
  540. }
  541. static void ftrace_shutdown_sysctl(void)
  542. {
  543. int command = FTRACE_DISABLE_MCOUNT;
  544. if (unlikely(ftrace_disabled))
  545. return;
  546. /* ftrace_start_up is true if ftrace is running */
  547. if (ftrace_start_up)
  548. command |= FTRACE_DISABLE_CALLS;
  549. ftrace_run_update_code(command);
  550. }
  551. static cycle_t ftrace_update_time;
  552. static unsigned long ftrace_update_cnt;
  553. unsigned long ftrace_update_tot_cnt;
  554. static int ftrace_update_code(struct module *mod)
  555. {
  556. struct dyn_ftrace *p, *t;
  557. cycle_t start, stop;
  558. start = ftrace_now(raw_smp_processor_id());
  559. ftrace_update_cnt = 0;
  560. list_for_each_entry_safe(p, t, &ftrace_new_addrs, list) {
  561. /* If something went wrong, bail without enabling anything */
  562. if (unlikely(ftrace_disabled))
  563. return -1;
  564. list_del_init(&p->list);
  565. /* convert record (i.e, patch mcount-call with NOP) */
  566. if (ftrace_code_disable(mod, p)) {
  567. p->flags |= FTRACE_FL_CONVERTED;
  568. ftrace_update_cnt++;
  569. } else
  570. ftrace_free_rec(p);
  571. }
  572. stop = ftrace_now(raw_smp_processor_id());
  573. ftrace_update_time = stop - start;
  574. ftrace_update_tot_cnt += ftrace_update_cnt;
  575. return 0;
  576. }
  577. static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
  578. {
  579. struct ftrace_page *pg;
  580. int cnt;
  581. int i;
  582. /* allocate a few pages */
  583. ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
  584. if (!ftrace_pages_start)
  585. return -1;
  586. /*
  587. * Allocate a few more pages.
  588. *
  589. * TODO: have some parser search vmlinux before
  590. * final linking to find all calls to ftrace.
  591. * Then we can:
  592. * a) know how many pages to allocate.
  593. * and/or
  594. * b) set up the table then.
  595. *
  596. * The dynamic code is still necessary for
  597. * modules.
  598. */
  599. pg = ftrace_pages = ftrace_pages_start;
  600. cnt = num_to_init / ENTRIES_PER_PAGE;
  601. pr_info("ftrace: allocating %ld entries in %d pages\n",
  602. num_to_init, cnt + 1);
  603. for (i = 0; i < cnt; i++) {
  604. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  605. /* If we fail, we'll try later anyway */
  606. if (!pg->next)
  607. break;
  608. pg = pg->next;
  609. }
  610. return 0;
  611. }
  612. enum {
  613. FTRACE_ITER_FILTER = (1 << 0),
  614. FTRACE_ITER_CONT = (1 << 1),
  615. FTRACE_ITER_NOTRACE = (1 << 2),
  616. FTRACE_ITER_FAILURES = (1 << 3),
  617. FTRACE_ITER_PRINTALL = (1 << 4),
  618. FTRACE_ITER_HASH = (1 << 5),
  619. };
  620. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  621. struct ftrace_iterator {
  622. struct ftrace_page *pg;
  623. int hidx;
  624. int idx;
  625. unsigned flags;
  626. unsigned char buffer[FTRACE_BUFF_MAX+1];
  627. unsigned buffer_idx;
  628. unsigned filtered;
  629. };
  630. static void *
  631. t_hash_next(struct seq_file *m, void *v, loff_t *pos)
  632. {
  633. struct ftrace_iterator *iter = m->private;
  634. struct hlist_node *hnd = v;
  635. struct hlist_head *hhd;
  636. WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
  637. (*pos)++;
  638. retry:
  639. if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
  640. return NULL;
  641. hhd = &ftrace_func_hash[iter->hidx];
  642. if (hlist_empty(hhd)) {
  643. iter->hidx++;
  644. hnd = NULL;
  645. goto retry;
  646. }
  647. if (!hnd)
  648. hnd = hhd->first;
  649. else {
  650. hnd = hnd->next;
  651. if (!hnd) {
  652. iter->hidx++;
  653. goto retry;
  654. }
  655. }
  656. return hnd;
  657. }
  658. static void *t_hash_start(struct seq_file *m, loff_t *pos)
  659. {
  660. struct ftrace_iterator *iter = m->private;
  661. void *p = NULL;
  662. iter->flags |= FTRACE_ITER_HASH;
  663. return t_hash_next(m, p, pos);
  664. }
  665. static int t_hash_show(struct seq_file *m, void *v)
  666. {
  667. struct ftrace_func_hook *rec;
  668. struct hlist_node *hnd = v;
  669. char str[KSYM_SYMBOL_LEN];
  670. rec = hlist_entry(hnd, struct ftrace_func_hook, node);
  671. if (rec->ops->print)
  672. return rec->ops->print(m, rec->ip, rec->ops, rec->data);
  673. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  674. seq_printf(m, "%s:", str);
  675. kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
  676. seq_printf(m, "%s", str);
  677. if (rec->data)
  678. seq_printf(m, ":%p", rec->data);
  679. seq_putc(m, '\n');
  680. return 0;
  681. }
  682. static void *
  683. t_next(struct seq_file *m, void *v, loff_t *pos)
  684. {
  685. struct ftrace_iterator *iter = m->private;
  686. struct dyn_ftrace *rec = NULL;
  687. if (iter->flags & FTRACE_ITER_HASH)
  688. return t_hash_next(m, v, pos);
  689. (*pos)++;
  690. if (iter->flags & FTRACE_ITER_PRINTALL)
  691. return NULL;
  692. retry:
  693. if (iter->idx >= iter->pg->index) {
  694. if (iter->pg->next) {
  695. iter->pg = iter->pg->next;
  696. iter->idx = 0;
  697. goto retry;
  698. } else {
  699. iter->idx = -1;
  700. }
  701. } else {
  702. rec = &iter->pg->records[iter->idx++];
  703. if ((rec->flags & FTRACE_FL_FREE) ||
  704. (!(iter->flags & FTRACE_ITER_FAILURES) &&
  705. (rec->flags & FTRACE_FL_FAILED)) ||
  706. ((iter->flags & FTRACE_ITER_FAILURES) &&
  707. !(rec->flags & FTRACE_FL_FAILED)) ||
  708. ((iter->flags & FTRACE_ITER_FILTER) &&
  709. !(rec->flags & FTRACE_FL_FILTER)) ||
  710. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  711. !(rec->flags & FTRACE_FL_NOTRACE))) {
  712. rec = NULL;
  713. goto retry;
  714. }
  715. }
  716. return rec;
  717. }
  718. static void *t_start(struct seq_file *m, loff_t *pos)
  719. {
  720. struct ftrace_iterator *iter = m->private;
  721. void *p = NULL;
  722. mutex_lock(&ftrace_lock);
  723. /*
  724. * For set_ftrace_filter reading, if we have the filter
  725. * off, we can short cut and just print out that all
  726. * functions are enabled.
  727. */
  728. if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
  729. if (*pos > 0)
  730. return t_hash_start(m, pos);
  731. iter->flags |= FTRACE_ITER_PRINTALL;
  732. (*pos)++;
  733. return iter;
  734. }
  735. if (iter->flags & FTRACE_ITER_HASH)
  736. return t_hash_start(m, pos);
  737. if (*pos > 0) {
  738. if (iter->idx < 0)
  739. return p;
  740. (*pos)--;
  741. iter->idx--;
  742. }
  743. p = t_next(m, p, pos);
  744. if (!p)
  745. return t_hash_start(m, pos);
  746. return p;
  747. }
  748. static void t_stop(struct seq_file *m, void *p)
  749. {
  750. mutex_unlock(&ftrace_lock);
  751. }
  752. static int t_show(struct seq_file *m, void *v)
  753. {
  754. struct ftrace_iterator *iter = m->private;
  755. struct dyn_ftrace *rec = v;
  756. char str[KSYM_SYMBOL_LEN];
  757. if (iter->flags & FTRACE_ITER_HASH)
  758. return t_hash_show(m, v);
  759. if (iter->flags & FTRACE_ITER_PRINTALL) {
  760. seq_printf(m, "#### all functions enabled ####\n");
  761. return 0;
  762. }
  763. if (!rec)
  764. return 0;
  765. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  766. seq_printf(m, "%s\n", str);
  767. return 0;
  768. }
  769. static struct seq_operations show_ftrace_seq_ops = {
  770. .start = t_start,
  771. .next = t_next,
  772. .stop = t_stop,
  773. .show = t_show,
  774. };
  775. static int
  776. ftrace_avail_open(struct inode *inode, struct file *file)
  777. {
  778. struct ftrace_iterator *iter;
  779. int ret;
  780. if (unlikely(ftrace_disabled))
  781. return -ENODEV;
  782. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  783. if (!iter)
  784. return -ENOMEM;
  785. iter->pg = ftrace_pages_start;
  786. ret = seq_open(file, &show_ftrace_seq_ops);
  787. if (!ret) {
  788. struct seq_file *m = file->private_data;
  789. m->private = iter;
  790. } else {
  791. kfree(iter);
  792. }
  793. return ret;
  794. }
  795. int ftrace_avail_release(struct inode *inode, struct file *file)
  796. {
  797. struct seq_file *m = (struct seq_file *)file->private_data;
  798. struct ftrace_iterator *iter = m->private;
  799. seq_release(inode, file);
  800. kfree(iter);
  801. return 0;
  802. }
  803. static int
  804. ftrace_failures_open(struct inode *inode, struct file *file)
  805. {
  806. int ret;
  807. struct seq_file *m;
  808. struct ftrace_iterator *iter;
  809. ret = ftrace_avail_open(inode, file);
  810. if (!ret) {
  811. m = (struct seq_file *)file->private_data;
  812. iter = (struct ftrace_iterator *)m->private;
  813. iter->flags = FTRACE_ITER_FAILURES;
  814. }
  815. return ret;
  816. }
  817. static void ftrace_filter_reset(int enable)
  818. {
  819. struct ftrace_page *pg;
  820. struct dyn_ftrace *rec;
  821. unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  822. mutex_lock(&ftrace_lock);
  823. if (enable)
  824. ftrace_filtered = 0;
  825. do_for_each_ftrace_rec(pg, rec) {
  826. if (rec->flags & FTRACE_FL_FAILED)
  827. continue;
  828. rec->flags &= ~type;
  829. } while_for_each_ftrace_rec();
  830. mutex_unlock(&ftrace_lock);
  831. }
  832. static int
  833. ftrace_regex_open(struct inode *inode, struct file *file, int enable)
  834. {
  835. struct ftrace_iterator *iter;
  836. int ret = 0;
  837. if (unlikely(ftrace_disabled))
  838. return -ENODEV;
  839. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  840. if (!iter)
  841. return -ENOMEM;
  842. mutex_lock(&ftrace_regex_lock);
  843. if ((file->f_mode & FMODE_WRITE) &&
  844. !(file->f_flags & O_APPEND))
  845. ftrace_filter_reset(enable);
  846. if (file->f_mode & FMODE_READ) {
  847. iter->pg = ftrace_pages_start;
  848. iter->flags = enable ? FTRACE_ITER_FILTER :
  849. FTRACE_ITER_NOTRACE;
  850. ret = seq_open(file, &show_ftrace_seq_ops);
  851. if (!ret) {
  852. struct seq_file *m = file->private_data;
  853. m->private = iter;
  854. } else
  855. kfree(iter);
  856. } else
  857. file->private_data = iter;
  858. mutex_unlock(&ftrace_regex_lock);
  859. return ret;
  860. }
  861. static int
  862. ftrace_filter_open(struct inode *inode, struct file *file)
  863. {
  864. return ftrace_regex_open(inode, file, 1);
  865. }
  866. static int
  867. ftrace_notrace_open(struct inode *inode, struct file *file)
  868. {
  869. return ftrace_regex_open(inode, file, 0);
  870. }
  871. static ssize_t
  872. ftrace_regex_read(struct file *file, char __user *ubuf,
  873. size_t cnt, loff_t *ppos)
  874. {
  875. if (file->f_mode & FMODE_READ)
  876. return seq_read(file, ubuf, cnt, ppos);
  877. else
  878. return -EPERM;
  879. }
  880. static loff_t
  881. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  882. {
  883. loff_t ret;
  884. if (file->f_mode & FMODE_READ)
  885. ret = seq_lseek(file, offset, origin);
  886. else
  887. file->f_pos = ret = 1;
  888. return ret;
  889. }
  890. enum {
  891. MATCH_FULL,
  892. MATCH_FRONT_ONLY,
  893. MATCH_MIDDLE_ONLY,
  894. MATCH_END_ONLY,
  895. };
  896. /*
  897. * (static function - no need for kernel doc)
  898. *
  899. * Pass in a buffer containing a glob and this function will
  900. * set search to point to the search part of the buffer and
  901. * return the type of search it is (see enum above).
  902. * This does modify buff.
  903. *
  904. * Returns enum type.
  905. * search returns the pointer to use for comparison.
  906. * not returns 1 if buff started with a '!'
  907. * 0 otherwise.
  908. */
  909. static int
  910. ftrace_setup_glob(char *buff, int len, char **search, int *not)
  911. {
  912. int type = MATCH_FULL;
  913. int i;
  914. if (buff[0] == '!') {
  915. *not = 1;
  916. buff++;
  917. len--;
  918. } else
  919. *not = 0;
  920. *search = buff;
  921. for (i = 0; i < len; i++) {
  922. if (buff[i] == '*') {
  923. if (!i) {
  924. *search = buff + 1;
  925. type = MATCH_END_ONLY;
  926. } else {
  927. if (type == MATCH_END_ONLY)
  928. type = MATCH_MIDDLE_ONLY;
  929. else
  930. type = MATCH_FRONT_ONLY;
  931. buff[i] = 0;
  932. break;
  933. }
  934. }
  935. }
  936. return type;
  937. }
  938. static int ftrace_match(char *str, char *regex, int len, int type)
  939. {
  940. int matched = 0;
  941. char *ptr;
  942. switch (type) {
  943. case MATCH_FULL:
  944. if (strcmp(str, regex) == 0)
  945. matched = 1;
  946. break;
  947. case MATCH_FRONT_ONLY:
  948. if (strncmp(str, regex, len) == 0)
  949. matched = 1;
  950. break;
  951. case MATCH_MIDDLE_ONLY:
  952. if (strstr(str, regex))
  953. matched = 1;
  954. break;
  955. case MATCH_END_ONLY:
  956. ptr = strstr(str, regex);
  957. if (ptr && (ptr[len] == 0))
  958. matched = 1;
  959. break;
  960. }
  961. return matched;
  962. }
  963. static int
  964. ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
  965. {
  966. char str[KSYM_SYMBOL_LEN];
  967. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  968. return ftrace_match(str, regex, len, type);
  969. }
  970. static void ftrace_match_records(char *buff, int len, int enable)
  971. {
  972. char *search;
  973. struct ftrace_page *pg;
  974. struct dyn_ftrace *rec;
  975. int type;
  976. unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  977. unsigned search_len;
  978. int not;
  979. type = ftrace_setup_glob(buff, len, &search, &not);
  980. search_len = strlen(search);
  981. mutex_lock(&ftrace_lock);
  982. do_for_each_ftrace_rec(pg, rec) {
  983. if (rec->flags & FTRACE_FL_FAILED)
  984. continue;
  985. if (ftrace_match_record(rec, search, search_len, type)) {
  986. if (not)
  987. rec->flags &= ~flag;
  988. else
  989. rec->flags |= flag;
  990. }
  991. /*
  992. * Only enable filtering if we have a function that
  993. * is filtered on.
  994. */
  995. if (enable && (rec->flags & FTRACE_FL_FILTER))
  996. ftrace_filtered = 1;
  997. } while_for_each_ftrace_rec();
  998. mutex_unlock(&ftrace_lock);
  999. }
  1000. static int
  1001. ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
  1002. char *regex, int len, int type)
  1003. {
  1004. char str[KSYM_SYMBOL_LEN];
  1005. char *modname;
  1006. kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
  1007. if (!modname || strcmp(modname, mod))
  1008. return 0;
  1009. /* blank search means to match all funcs in the mod */
  1010. if (len)
  1011. return ftrace_match(str, regex, len, type);
  1012. else
  1013. return 1;
  1014. }
  1015. static void ftrace_match_module_records(char *buff, char *mod, int enable)
  1016. {
  1017. char *search = buff;
  1018. struct ftrace_page *pg;
  1019. struct dyn_ftrace *rec;
  1020. int type = MATCH_FULL;
  1021. unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1022. unsigned search_len = 0;
  1023. int not = 0;
  1024. /* blank or '*' mean the same */
  1025. if (strcmp(buff, "*") == 0)
  1026. buff[0] = 0;
  1027. /* handle the case of 'dont filter this module' */
  1028. if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
  1029. buff[0] = 0;
  1030. not = 1;
  1031. }
  1032. if (strlen(buff)) {
  1033. type = ftrace_setup_glob(buff, strlen(buff), &search, &not);
  1034. search_len = strlen(search);
  1035. }
  1036. mutex_lock(&ftrace_lock);
  1037. do_for_each_ftrace_rec(pg, rec) {
  1038. if (rec->flags & FTRACE_FL_FAILED)
  1039. continue;
  1040. if (ftrace_match_module_record(rec, mod,
  1041. search, search_len, type)) {
  1042. if (not)
  1043. rec->flags &= ~flag;
  1044. else
  1045. rec->flags |= flag;
  1046. }
  1047. if (enable && (rec->flags & FTRACE_FL_FILTER))
  1048. ftrace_filtered = 1;
  1049. } while_for_each_ftrace_rec();
  1050. mutex_unlock(&ftrace_lock);
  1051. }
  1052. /*
  1053. * We register the module command as a template to show others how
  1054. * to register the a command as well.
  1055. */
  1056. static int
  1057. ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
  1058. {
  1059. char *mod;
  1060. /*
  1061. * cmd == 'mod' because we only registered this func
  1062. * for the 'mod' ftrace_func_command.
  1063. * But if you register one func with multiple commands,
  1064. * you can tell which command was used by the cmd
  1065. * parameter.
  1066. */
  1067. /* we must have a module name */
  1068. if (!param)
  1069. return -EINVAL;
  1070. mod = strsep(&param, ":");
  1071. if (!strlen(mod))
  1072. return -EINVAL;
  1073. ftrace_match_module_records(func, mod, enable);
  1074. return 0;
  1075. }
  1076. static struct ftrace_func_command ftrace_mod_cmd = {
  1077. .name = "mod",
  1078. .func = ftrace_mod_callback,
  1079. };
  1080. static int __init ftrace_mod_cmd_init(void)
  1081. {
  1082. return register_ftrace_command(&ftrace_mod_cmd);
  1083. }
  1084. device_initcall(ftrace_mod_cmd_init);
  1085. static void
  1086. function_trace_hook_call(unsigned long ip, unsigned long parent_ip)
  1087. {
  1088. struct ftrace_func_hook *entry;
  1089. struct hlist_head *hhd;
  1090. struct hlist_node *n;
  1091. unsigned long key;
  1092. int resched;
  1093. key = hash_long(ip, FTRACE_HASH_BITS);
  1094. hhd = &ftrace_func_hash[key];
  1095. if (hlist_empty(hhd))
  1096. return;
  1097. /*
  1098. * Disable preemption for these calls to prevent a RCU grace
  1099. * period. This syncs the hash iteration and freeing of items
  1100. * on the hash. rcu_read_lock is too dangerous here.
  1101. */
  1102. resched = ftrace_preempt_disable();
  1103. hlist_for_each_entry_rcu(entry, n, hhd, node) {
  1104. if (entry->ip == ip)
  1105. entry->ops->func(ip, parent_ip, &entry->data);
  1106. }
  1107. ftrace_preempt_enable(resched);
  1108. }
  1109. static struct ftrace_ops trace_hook_ops __read_mostly =
  1110. {
  1111. .func = function_trace_hook_call,
  1112. };
  1113. static int ftrace_hook_registered;
  1114. static void __enable_ftrace_function_hook(void)
  1115. {
  1116. int i;
  1117. if (ftrace_hook_registered)
  1118. return;
  1119. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1120. struct hlist_head *hhd = &ftrace_func_hash[i];
  1121. if (hhd->first)
  1122. break;
  1123. }
  1124. /* Nothing registered? */
  1125. if (i == FTRACE_FUNC_HASHSIZE)
  1126. return;
  1127. __register_ftrace_function(&trace_hook_ops);
  1128. ftrace_startup(0);
  1129. ftrace_hook_registered = 1;
  1130. }
  1131. static void __disable_ftrace_function_hook(void)
  1132. {
  1133. int i;
  1134. if (!ftrace_hook_registered)
  1135. return;
  1136. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1137. struct hlist_head *hhd = &ftrace_func_hash[i];
  1138. if (hhd->first)
  1139. return;
  1140. }
  1141. /* no more funcs left */
  1142. __unregister_ftrace_function(&trace_hook_ops);
  1143. ftrace_shutdown(0);
  1144. ftrace_hook_registered = 0;
  1145. }
  1146. static void ftrace_free_entry_rcu(struct rcu_head *rhp)
  1147. {
  1148. struct ftrace_func_hook *entry =
  1149. container_of(rhp, struct ftrace_func_hook, rcu);
  1150. if (entry->ops->free)
  1151. entry->ops->free(&entry->data);
  1152. kfree(entry);
  1153. }
  1154. int
  1155. register_ftrace_function_hook(char *glob, struct ftrace_hook_ops *ops,
  1156. void *data)
  1157. {
  1158. struct ftrace_func_hook *entry;
  1159. struct ftrace_page *pg;
  1160. struct dyn_ftrace *rec;
  1161. unsigned long key;
  1162. int type, len, not;
  1163. int count = 0;
  1164. char *search;
  1165. type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
  1166. len = strlen(search);
  1167. /* we do not support '!' for function hooks */
  1168. if (WARN_ON(not))
  1169. return -EINVAL;
  1170. mutex_lock(&ftrace_lock);
  1171. do_for_each_ftrace_rec(pg, rec) {
  1172. if (rec->flags & FTRACE_FL_FAILED)
  1173. continue;
  1174. if (!ftrace_match_record(rec, search, len, type))
  1175. continue;
  1176. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  1177. if (!entry) {
  1178. /* If we did not hook to any, then return error */
  1179. if (!count)
  1180. count = -ENOMEM;
  1181. goto out_unlock;
  1182. }
  1183. count++;
  1184. entry->data = data;
  1185. /*
  1186. * The caller might want to do something special
  1187. * for each function we find. We call the callback
  1188. * to give the caller an opportunity to do so.
  1189. */
  1190. if (ops->callback) {
  1191. if (ops->callback(rec->ip, &entry->data) < 0) {
  1192. /* caller does not like this func */
  1193. kfree(entry);
  1194. continue;
  1195. }
  1196. }
  1197. entry->ops = ops;
  1198. entry->ip = rec->ip;
  1199. key = hash_long(entry->ip, FTRACE_HASH_BITS);
  1200. hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
  1201. } while_for_each_ftrace_rec();
  1202. __enable_ftrace_function_hook();
  1203. out_unlock:
  1204. mutex_unlock(&ftrace_lock);
  1205. return count;
  1206. }
  1207. enum {
  1208. HOOK_TEST_FUNC = 1,
  1209. HOOK_TEST_DATA = 2
  1210. };
  1211. static void
  1212. __unregister_ftrace_function_hook(char *glob, struct ftrace_hook_ops *ops,
  1213. void *data, int flags)
  1214. {
  1215. struct ftrace_func_hook *entry;
  1216. struct hlist_node *n, *tmp;
  1217. char str[KSYM_SYMBOL_LEN];
  1218. int type = MATCH_FULL;
  1219. int i, len = 0;
  1220. char *search;
  1221. if (glob && (strcmp(glob, "*") || !strlen(glob)))
  1222. glob = NULL;
  1223. else {
  1224. int not;
  1225. type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
  1226. len = strlen(search);
  1227. /* we do not support '!' for function hooks */
  1228. if (WARN_ON(not))
  1229. return;
  1230. }
  1231. mutex_lock(&ftrace_lock);
  1232. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1233. struct hlist_head *hhd = &ftrace_func_hash[i];
  1234. hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
  1235. /* break up if statements for readability */
  1236. if ((flags & HOOK_TEST_FUNC) && entry->ops != ops)
  1237. continue;
  1238. if ((flags & HOOK_TEST_DATA) && entry->data != data)
  1239. continue;
  1240. /* do this last, since it is the most expensive */
  1241. if (glob) {
  1242. kallsyms_lookup(entry->ip, NULL, NULL,
  1243. NULL, str);
  1244. if (!ftrace_match(str, glob, len, type))
  1245. continue;
  1246. }
  1247. hlist_del(&entry->node);
  1248. call_rcu(&entry->rcu, ftrace_free_entry_rcu);
  1249. }
  1250. }
  1251. __disable_ftrace_function_hook();
  1252. mutex_unlock(&ftrace_lock);
  1253. }
  1254. void
  1255. unregister_ftrace_function_hook(char *glob, struct ftrace_hook_ops *ops,
  1256. void *data)
  1257. {
  1258. __unregister_ftrace_function_hook(glob, ops, data,
  1259. HOOK_TEST_FUNC | HOOK_TEST_DATA);
  1260. }
  1261. void
  1262. unregister_ftrace_function_hook_func(char *glob, struct ftrace_hook_ops *ops)
  1263. {
  1264. __unregister_ftrace_function_hook(glob, ops, NULL, HOOK_TEST_FUNC);
  1265. }
  1266. void unregister_ftrace_function_hook_all(char *glob)
  1267. {
  1268. __unregister_ftrace_function_hook(glob, NULL, NULL, 0);
  1269. }
  1270. static LIST_HEAD(ftrace_commands);
  1271. static DEFINE_MUTEX(ftrace_cmd_mutex);
  1272. int register_ftrace_command(struct ftrace_func_command *cmd)
  1273. {
  1274. struct ftrace_func_command *p;
  1275. int ret = 0;
  1276. mutex_lock(&ftrace_cmd_mutex);
  1277. list_for_each_entry(p, &ftrace_commands, list) {
  1278. if (strcmp(cmd->name, p->name) == 0) {
  1279. ret = -EBUSY;
  1280. goto out_unlock;
  1281. }
  1282. }
  1283. list_add(&cmd->list, &ftrace_commands);
  1284. out_unlock:
  1285. mutex_unlock(&ftrace_cmd_mutex);
  1286. return ret;
  1287. }
  1288. int unregister_ftrace_command(struct ftrace_func_command *cmd)
  1289. {
  1290. struct ftrace_func_command *p, *n;
  1291. int ret = -ENODEV;
  1292. mutex_lock(&ftrace_cmd_mutex);
  1293. list_for_each_entry_safe(p, n, &ftrace_commands, list) {
  1294. if (strcmp(cmd->name, p->name) == 0) {
  1295. ret = 0;
  1296. list_del_init(&p->list);
  1297. goto out_unlock;
  1298. }
  1299. }
  1300. out_unlock:
  1301. mutex_unlock(&ftrace_cmd_mutex);
  1302. return ret;
  1303. }
  1304. static int ftrace_process_regex(char *buff, int len, int enable)
  1305. {
  1306. struct ftrace_func_command *p;
  1307. char *func, *command, *next = buff;
  1308. int ret = -EINVAL;
  1309. func = strsep(&next, ":");
  1310. if (!next) {
  1311. ftrace_match_records(func, len, enable);
  1312. return 0;
  1313. }
  1314. /* command found */
  1315. command = strsep(&next, ":");
  1316. mutex_lock(&ftrace_cmd_mutex);
  1317. list_for_each_entry(p, &ftrace_commands, list) {
  1318. if (strcmp(p->name, command) == 0) {
  1319. ret = p->func(func, command, next, enable);
  1320. goto out_unlock;
  1321. }
  1322. }
  1323. out_unlock:
  1324. mutex_unlock(&ftrace_cmd_mutex);
  1325. return ret;
  1326. }
  1327. static ssize_t
  1328. ftrace_regex_write(struct file *file, const char __user *ubuf,
  1329. size_t cnt, loff_t *ppos, int enable)
  1330. {
  1331. struct ftrace_iterator *iter;
  1332. char ch;
  1333. size_t read = 0;
  1334. ssize_t ret;
  1335. if (!cnt || cnt < 0)
  1336. return 0;
  1337. mutex_lock(&ftrace_regex_lock);
  1338. if (file->f_mode & FMODE_READ) {
  1339. struct seq_file *m = file->private_data;
  1340. iter = m->private;
  1341. } else
  1342. iter = file->private_data;
  1343. if (!*ppos) {
  1344. iter->flags &= ~FTRACE_ITER_CONT;
  1345. iter->buffer_idx = 0;
  1346. }
  1347. ret = get_user(ch, ubuf++);
  1348. if (ret)
  1349. goto out;
  1350. read++;
  1351. cnt--;
  1352. if (!(iter->flags & ~FTRACE_ITER_CONT)) {
  1353. /* skip white space */
  1354. while (cnt && isspace(ch)) {
  1355. ret = get_user(ch, ubuf++);
  1356. if (ret)
  1357. goto out;
  1358. read++;
  1359. cnt--;
  1360. }
  1361. if (isspace(ch)) {
  1362. file->f_pos += read;
  1363. ret = read;
  1364. goto out;
  1365. }
  1366. iter->buffer_idx = 0;
  1367. }
  1368. while (cnt && !isspace(ch)) {
  1369. if (iter->buffer_idx < FTRACE_BUFF_MAX)
  1370. iter->buffer[iter->buffer_idx++] = ch;
  1371. else {
  1372. ret = -EINVAL;
  1373. goto out;
  1374. }
  1375. ret = get_user(ch, ubuf++);
  1376. if (ret)
  1377. goto out;
  1378. read++;
  1379. cnt--;
  1380. }
  1381. if (isspace(ch)) {
  1382. iter->filtered++;
  1383. iter->buffer[iter->buffer_idx] = 0;
  1384. ret = ftrace_process_regex(iter->buffer,
  1385. iter->buffer_idx, enable);
  1386. if (ret)
  1387. goto out;
  1388. iter->buffer_idx = 0;
  1389. } else
  1390. iter->flags |= FTRACE_ITER_CONT;
  1391. file->f_pos += read;
  1392. ret = read;
  1393. out:
  1394. mutex_unlock(&ftrace_regex_lock);
  1395. return ret;
  1396. }
  1397. static ssize_t
  1398. ftrace_filter_write(struct file *file, const char __user *ubuf,
  1399. size_t cnt, loff_t *ppos)
  1400. {
  1401. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  1402. }
  1403. static ssize_t
  1404. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  1405. size_t cnt, loff_t *ppos)
  1406. {
  1407. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  1408. }
  1409. static void
  1410. ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
  1411. {
  1412. if (unlikely(ftrace_disabled))
  1413. return;
  1414. mutex_lock(&ftrace_regex_lock);
  1415. if (reset)
  1416. ftrace_filter_reset(enable);
  1417. if (buf)
  1418. ftrace_match_records(buf, len, enable);
  1419. mutex_unlock(&ftrace_regex_lock);
  1420. }
  1421. /**
  1422. * ftrace_set_filter - set a function to filter on in ftrace
  1423. * @buf - the string that holds the function filter text.
  1424. * @len - the length of the string.
  1425. * @reset - non zero to reset all filters before applying this filter.
  1426. *
  1427. * Filters denote which functions should be enabled when tracing is enabled.
  1428. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  1429. */
  1430. void ftrace_set_filter(unsigned char *buf, int len, int reset)
  1431. {
  1432. ftrace_set_regex(buf, len, reset, 1);
  1433. }
  1434. /**
  1435. * ftrace_set_notrace - set a function to not trace in ftrace
  1436. * @buf - the string that holds the function notrace text.
  1437. * @len - the length of the string.
  1438. * @reset - non zero to reset all filters before applying this filter.
  1439. *
  1440. * Notrace Filters denote which functions should not be enabled when tracing
  1441. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  1442. * for tracing.
  1443. */
  1444. void ftrace_set_notrace(unsigned char *buf, int len, int reset)
  1445. {
  1446. ftrace_set_regex(buf, len, reset, 0);
  1447. }
  1448. static int
  1449. ftrace_regex_release(struct inode *inode, struct file *file, int enable)
  1450. {
  1451. struct seq_file *m = (struct seq_file *)file->private_data;
  1452. struct ftrace_iterator *iter;
  1453. mutex_lock(&ftrace_regex_lock);
  1454. if (file->f_mode & FMODE_READ) {
  1455. iter = m->private;
  1456. seq_release(inode, file);
  1457. } else
  1458. iter = file->private_data;
  1459. if (iter->buffer_idx) {
  1460. iter->filtered++;
  1461. iter->buffer[iter->buffer_idx] = 0;
  1462. ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
  1463. }
  1464. mutex_lock(&ftrace_lock);
  1465. if (ftrace_start_up && ftrace_enabled)
  1466. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  1467. mutex_unlock(&ftrace_lock);
  1468. kfree(iter);
  1469. mutex_unlock(&ftrace_regex_lock);
  1470. return 0;
  1471. }
  1472. static int
  1473. ftrace_filter_release(struct inode *inode, struct file *file)
  1474. {
  1475. return ftrace_regex_release(inode, file, 1);
  1476. }
  1477. static int
  1478. ftrace_notrace_release(struct inode *inode, struct file *file)
  1479. {
  1480. return ftrace_regex_release(inode, file, 0);
  1481. }
  1482. static struct file_operations ftrace_avail_fops = {
  1483. .open = ftrace_avail_open,
  1484. .read = seq_read,
  1485. .llseek = seq_lseek,
  1486. .release = ftrace_avail_release,
  1487. };
  1488. static struct file_operations ftrace_failures_fops = {
  1489. .open = ftrace_failures_open,
  1490. .read = seq_read,
  1491. .llseek = seq_lseek,
  1492. .release = ftrace_avail_release,
  1493. };
  1494. static struct file_operations ftrace_filter_fops = {
  1495. .open = ftrace_filter_open,
  1496. .read = ftrace_regex_read,
  1497. .write = ftrace_filter_write,
  1498. .llseek = ftrace_regex_lseek,
  1499. .release = ftrace_filter_release,
  1500. };
  1501. static struct file_operations ftrace_notrace_fops = {
  1502. .open = ftrace_notrace_open,
  1503. .read = ftrace_regex_read,
  1504. .write = ftrace_notrace_write,
  1505. .llseek = ftrace_regex_lseek,
  1506. .release = ftrace_notrace_release,
  1507. };
  1508. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1509. static DEFINE_MUTEX(graph_lock);
  1510. int ftrace_graph_count;
  1511. unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
  1512. static void *
  1513. g_next(struct seq_file *m, void *v, loff_t *pos)
  1514. {
  1515. unsigned long *array = m->private;
  1516. int index = *pos;
  1517. (*pos)++;
  1518. if (index >= ftrace_graph_count)
  1519. return NULL;
  1520. return &array[index];
  1521. }
  1522. static void *g_start(struct seq_file *m, loff_t *pos)
  1523. {
  1524. void *p = NULL;
  1525. mutex_lock(&graph_lock);
  1526. p = g_next(m, p, pos);
  1527. return p;
  1528. }
  1529. static void g_stop(struct seq_file *m, void *p)
  1530. {
  1531. mutex_unlock(&graph_lock);
  1532. }
  1533. static int g_show(struct seq_file *m, void *v)
  1534. {
  1535. unsigned long *ptr = v;
  1536. char str[KSYM_SYMBOL_LEN];
  1537. if (!ptr)
  1538. return 0;
  1539. kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
  1540. seq_printf(m, "%s\n", str);
  1541. return 0;
  1542. }
  1543. static struct seq_operations ftrace_graph_seq_ops = {
  1544. .start = g_start,
  1545. .next = g_next,
  1546. .stop = g_stop,
  1547. .show = g_show,
  1548. };
  1549. static int
  1550. ftrace_graph_open(struct inode *inode, struct file *file)
  1551. {
  1552. int ret = 0;
  1553. if (unlikely(ftrace_disabled))
  1554. return -ENODEV;
  1555. mutex_lock(&graph_lock);
  1556. if ((file->f_mode & FMODE_WRITE) &&
  1557. !(file->f_flags & O_APPEND)) {
  1558. ftrace_graph_count = 0;
  1559. memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
  1560. }
  1561. if (file->f_mode & FMODE_READ) {
  1562. ret = seq_open(file, &ftrace_graph_seq_ops);
  1563. if (!ret) {
  1564. struct seq_file *m = file->private_data;
  1565. m->private = ftrace_graph_funcs;
  1566. }
  1567. } else
  1568. file->private_data = ftrace_graph_funcs;
  1569. mutex_unlock(&graph_lock);
  1570. return ret;
  1571. }
  1572. static ssize_t
  1573. ftrace_graph_read(struct file *file, char __user *ubuf,
  1574. size_t cnt, loff_t *ppos)
  1575. {
  1576. if (file->f_mode & FMODE_READ)
  1577. return seq_read(file, ubuf, cnt, ppos);
  1578. else
  1579. return -EPERM;
  1580. }
  1581. static int
  1582. ftrace_set_func(unsigned long *array, int idx, char *buffer)
  1583. {
  1584. char str[KSYM_SYMBOL_LEN];
  1585. struct dyn_ftrace *rec;
  1586. struct ftrace_page *pg;
  1587. int found = 0;
  1588. int j;
  1589. if (ftrace_disabled)
  1590. return -ENODEV;
  1591. mutex_lock(&ftrace_lock);
  1592. do_for_each_ftrace_rec(pg, rec) {
  1593. if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
  1594. continue;
  1595. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  1596. if (strcmp(str, buffer) == 0) {
  1597. /* Return 1 if we add it to the array */
  1598. found = 1;
  1599. for (j = 0; j < idx; j++)
  1600. if (array[j] == rec->ip) {
  1601. found = 0;
  1602. break;
  1603. }
  1604. if (found)
  1605. array[idx] = rec->ip;
  1606. goto out;
  1607. }
  1608. } while_for_each_ftrace_rec();
  1609. out:
  1610. mutex_unlock(&ftrace_lock);
  1611. return found ? 0 : -EINVAL;
  1612. }
  1613. static ssize_t
  1614. ftrace_graph_write(struct file *file, const char __user *ubuf,
  1615. size_t cnt, loff_t *ppos)
  1616. {
  1617. unsigned char buffer[FTRACE_BUFF_MAX+1];
  1618. unsigned long *array;
  1619. size_t read = 0;
  1620. ssize_t ret;
  1621. int index = 0;
  1622. char ch;
  1623. if (!cnt || cnt < 0)
  1624. return 0;
  1625. mutex_lock(&graph_lock);
  1626. if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
  1627. ret = -EBUSY;
  1628. goto out;
  1629. }
  1630. if (file->f_mode & FMODE_READ) {
  1631. struct seq_file *m = file->private_data;
  1632. array = m->private;
  1633. } else
  1634. array = file->private_data;
  1635. ret = get_user(ch, ubuf++);
  1636. if (ret)
  1637. goto out;
  1638. read++;
  1639. cnt--;
  1640. /* skip white space */
  1641. while (cnt && isspace(ch)) {
  1642. ret = get_user(ch, ubuf++);
  1643. if (ret)
  1644. goto out;
  1645. read++;
  1646. cnt--;
  1647. }
  1648. if (isspace(ch)) {
  1649. *ppos += read;
  1650. ret = read;
  1651. goto out;
  1652. }
  1653. while (cnt && !isspace(ch)) {
  1654. if (index < FTRACE_BUFF_MAX)
  1655. buffer[index++] = ch;
  1656. else {
  1657. ret = -EINVAL;
  1658. goto out;
  1659. }
  1660. ret = get_user(ch, ubuf++);
  1661. if (ret)
  1662. goto out;
  1663. read++;
  1664. cnt--;
  1665. }
  1666. buffer[index] = 0;
  1667. /* we allow only one at a time */
  1668. ret = ftrace_set_func(array, ftrace_graph_count, buffer);
  1669. if (ret)
  1670. goto out;
  1671. ftrace_graph_count++;
  1672. file->f_pos += read;
  1673. ret = read;
  1674. out:
  1675. mutex_unlock(&graph_lock);
  1676. return ret;
  1677. }
  1678. static const struct file_operations ftrace_graph_fops = {
  1679. .open = ftrace_graph_open,
  1680. .read = ftrace_graph_read,
  1681. .write = ftrace_graph_write,
  1682. };
  1683. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1684. static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
  1685. {
  1686. struct dentry *entry;
  1687. entry = debugfs_create_file("available_filter_functions", 0444,
  1688. d_tracer, NULL, &ftrace_avail_fops);
  1689. if (!entry)
  1690. pr_warning("Could not create debugfs "
  1691. "'available_filter_functions' entry\n");
  1692. entry = debugfs_create_file("failures", 0444,
  1693. d_tracer, NULL, &ftrace_failures_fops);
  1694. if (!entry)
  1695. pr_warning("Could not create debugfs 'failures' entry\n");
  1696. entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
  1697. NULL, &ftrace_filter_fops);
  1698. if (!entry)
  1699. pr_warning("Could not create debugfs "
  1700. "'set_ftrace_filter' entry\n");
  1701. entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
  1702. NULL, &ftrace_notrace_fops);
  1703. if (!entry)
  1704. pr_warning("Could not create debugfs "
  1705. "'set_ftrace_notrace' entry\n");
  1706. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1707. entry = debugfs_create_file("set_graph_function", 0444, d_tracer,
  1708. NULL,
  1709. &ftrace_graph_fops);
  1710. if (!entry)
  1711. pr_warning("Could not create debugfs "
  1712. "'set_graph_function' entry\n");
  1713. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1714. return 0;
  1715. }
  1716. static int ftrace_convert_nops(struct module *mod,
  1717. unsigned long *start,
  1718. unsigned long *end)
  1719. {
  1720. unsigned long *p;
  1721. unsigned long addr;
  1722. unsigned long flags;
  1723. mutex_lock(&ftrace_lock);
  1724. p = start;
  1725. while (p < end) {
  1726. addr = ftrace_call_adjust(*p++);
  1727. /*
  1728. * Some architecture linkers will pad between
  1729. * the different mcount_loc sections of different
  1730. * object files to satisfy alignments.
  1731. * Skip any NULL pointers.
  1732. */
  1733. if (!addr)
  1734. continue;
  1735. ftrace_record_ip(addr);
  1736. }
  1737. /* disable interrupts to prevent kstop machine */
  1738. local_irq_save(flags);
  1739. ftrace_update_code(mod);
  1740. local_irq_restore(flags);
  1741. mutex_unlock(&ftrace_lock);
  1742. return 0;
  1743. }
  1744. void ftrace_init_module(struct module *mod,
  1745. unsigned long *start, unsigned long *end)
  1746. {
  1747. if (ftrace_disabled || start == end)
  1748. return;
  1749. ftrace_convert_nops(mod, start, end);
  1750. }
  1751. extern unsigned long __start_mcount_loc[];
  1752. extern unsigned long __stop_mcount_loc[];
  1753. void __init ftrace_init(void)
  1754. {
  1755. unsigned long count, addr, flags;
  1756. int ret;
  1757. /* Keep the ftrace pointer to the stub */
  1758. addr = (unsigned long)ftrace_stub;
  1759. local_irq_save(flags);
  1760. ftrace_dyn_arch_init(&addr);
  1761. local_irq_restore(flags);
  1762. /* ftrace_dyn_arch_init places the return code in addr */
  1763. if (addr)
  1764. goto failed;
  1765. count = __stop_mcount_loc - __start_mcount_loc;
  1766. ret = ftrace_dyn_table_alloc(count);
  1767. if (ret)
  1768. goto failed;
  1769. last_ftrace_enabled = ftrace_enabled = 1;
  1770. ret = ftrace_convert_nops(NULL,
  1771. __start_mcount_loc,
  1772. __stop_mcount_loc);
  1773. return;
  1774. failed:
  1775. ftrace_disabled = 1;
  1776. }
  1777. #else
  1778. static int __init ftrace_nodyn_init(void)
  1779. {
  1780. ftrace_enabled = 1;
  1781. return 0;
  1782. }
  1783. device_initcall(ftrace_nodyn_init);
  1784. static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
  1785. static inline void ftrace_startup_enable(int command) { }
  1786. /* Keep as macros so we do not need to define the commands */
  1787. # define ftrace_startup(command) do { } while (0)
  1788. # define ftrace_shutdown(command) do { } while (0)
  1789. # define ftrace_startup_sysctl() do { } while (0)
  1790. # define ftrace_shutdown_sysctl() do { } while (0)
  1791. #endif /* CONFIG_DYNAMIC_FTRACE */
  1792. static ssize_t
  1793. ftrace_pid_read(struct file *file, char __user *ubuf,
  1794. size_t cnt, loff_t *ppos)
  1795. {
  1796. char buf[64];
  1797. int r;
  1798. if (ftrace_pid_trace == ftrace_swapper_pid)
  1799. r = sprintf(buf, "swapper tasks\n");
  1800. else if (ftrace_pid_trace)
  1801. r = sprintf(buf, "%u\n", pid_nr(ftrace_pid_trace));
  1802. else
  1803. r = sprintf(buf, "no pid\n");
  1804. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1805. }
  1806. static void clear_ftrace_swapper(void)
  1807. {
  1808. struct task_struct *p;
  1809. int cpu;
  1810. get_online_cpus();
  1811. for_each_online_cpu(cpu) {
  1812. p = idle_task(cpu);
  1813. clear_tsk_trace_trace(p);
  1814. }
  1815. put_online_cpus();
  1816. }
  1817. static void set_ftrace_swapper(void)
  1818. {
  1819. struct task_struct *p;
  1820. int cpu;
  1821. get_online_cpus();
  1822. for_each_online_cpu(cpu) {
  1823. p = idle_task(cpu);
  1824. set_tsk_trace_trace(p);
  1825. }
  1826. put_online_cpus();
  1827. }
  1828. static void clear_ftrace_pid(struct pid *pid)
  1829. {
  1830. struct task_struct *p;
  1831. rcu_read_lock();
  1832. do_each_pid_task(pid, PIDTYPE_PID, p) {
  1833. clear_tsk_trace_trace(p);
  1834. } while_each_pid_task(pid, PIDTYPE_PID, p);
  1835. rcu_read_unlock();
  1836. put_pid(pid);
  1837. }
  1838. static void set_ftrace_pid(struct pid *pid)
  1839. {
  1840. struct task_struct *p;
  1841. rcu_read_lock();
  1842. do_each_pid_task(pid, PIDTYPE_PID, p) {
  1843. set_tsk_trace_trace(p);
  1844. } while_each_pid_task(pid, PIDTYPE_PID, p);
  1845. rcu_read_unlock();
  1846. }
  1847. static void clear_ftrace_pid_task(struct pid **pid)
  1848. {
  1849. if (*pid == ftrace_swapper_pid)
  1850. clear_ftrace_swapper();
  1851. else
  1852. clear_ftrace_pid(*pid);
  1853. *pid = NULL;
  1854. }
  1855. static void set_ftrace_pid_task(struct pid *pid)
  1856. {
  1857. if (pid == ftrace_swapper_pid)
  1858. set_ftrace_swapper();
  1859. else
  1860. set_ftrace_pid(pid);
  1861. }
  1862. static ssize_t
  1863. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  1864. size_t cnt, loff_t *ppos)
  1865. {
  1866. struct pid *pid;
  1867. char buf[64];
  1868. long val;
  1869. int ret;
  1870. if (cnt >= sizeof(buf))
  1871. return -EINVAL;
  1872. if (copy_from_user(&buf, ubuf, cnt))
  1873. return -EFAULT;
  1874. buf[cnt] = 0;
  1875. ret = strict_strtol(buf, 10, &val);
  1876. if (ret < 0)
  1877. return ret;
  1878. mutex_lock(&ftrace_lock);
  1879. if (val < 0) {
  1880. /* disable pid tracing */
  1881. if (!ftrace_pid_trace)
  1882. goto out;
  1883. clear_ftrace_pid_task(&ftrace_pid_trace);
  1884. } else {
  1885. /* swapper task is special */
  1886. if (!val) {
  1887. pid = ftrace_swapper_pid;
  1888. if (pid == ftrace_pid_trace)
  1889. goto out;
  1890. } else {
  1891. pid = find_get_pid(val);
  1892. if (pid == ftrace_pid_trace) {
  1893. put_pid(pid);
  1894. goto out;
  1895. }
  1896. }
  1897. if (ftrace_pid_trace)
  1898. clear_ftrace_pid_task(&ftrace_pid_trace);
  1899. if (!pid)
  1900. goto out;
  1901. ftrace_pid_trace = pid;
  1902. set_ftrace_pid_task(ftrace_pid_trace);
  1903. }
  1904. /* update the function call */
  1905. ftrace_update_pid_func();
  1906. ftrace_startup_enable(0);
  1907. out:
  1908. mutex_unlock(&ftrace_lock);
  1909. return cnt;
  1910. }
  1911. static struct file_operations ftrace_pid_fops = {
  1912. .read = ftrace_pid_read,
  1913. .write = ftrace_pid_write,
  1914. };
  1915. static __init int ftrace_init_debugfs(void)
  1916. {
  1917. struct dentry *d_tracer;
  1918. struct dentry *entry;
  1919. d_tracer = tracing_init_dentry();
  1920. if (!d_tracer)
  1921. return 0;
  1922. ftrace_init_dyn_debugfs(d_tracer);
  1923. entry = debugfs_create_file("set_ftrace_pid", 0644, d_tracer,
  1924. NULL, &ftrace_pid_fops);
  1925. if (!entry)
  1926. pr_warning("Could not create debugfs "
  1927. "'set_ftrace_pid' entry\n");
  1928. return 0;
  1929. }
  1930. fs_initcall(ftrace_init_debugfs);
  1931. /**
  1932. * ftrace_kill - kill ftrace
  1933. *
  1934. * This function should be used by panic code. It stops ftrace
  1935. * but in a not so nice way. If you need to simply kill ftrace
  1936. * from a non-atomic section, use ftrace_kill.
  1937. */
  1938. void ftrace_kill(void)
  1939. {
  1940. ftrace_disabled = 1;
  1941. ftrace_enabled = 0;
  1942. clear_ftrace_function();
  1943. }
  1944. /**
  1945. * register_ftrace_function - register a function for profiling
  1946. * @ops - ops structure that holds the function for profiling.
  1947. *
  1948. * Register a function to be called by all functions in the
  1949. * kernel.
  1950. *
  1951. * Note: @ops->func and all the functions it calls must be labeled
  1952. * with "notrace", otherwise it will go into a
  1953. * recursive loop.
  1954. */
  1955. int register_ftrace_function(struct ftrace_ops *ops)
  1956. {
  1957. int ret;
  1958. if (unlikely(ftrace_disabled))
  1959. return -1;
  1960. mutex_lock(&ftrace_lock);
  1961. ret = __register_ftrace_function(ops);
  1962. ftrace_startup(0);
  1963. mutex_unlock(&ftrace_lock);
  1964. return ret;
  1965. }
  1966. /**
  1967. * unregister_ftrace_function - unregister a function for profiling.
  1968. * @ops - ops structure that holds the function to unregister
  1969. *
  1970. * Unregister a function that was added to be called by ftrace profiling.
  1971. */
  1972. int unregister_ftrace_function(struct ftrace_ops *ops)
  1973. {
  1974. int ret;
  1975. mutex_lock(&ftrace_lock);
  1976. ret = __unregister_ftrace_function(ops);
  1977. ftrace_shutdown(0);
  1978. mutex_unlock(&ftrace_lock);
  1979. return ret;
  1980. }
  1981. int
  1982. ftrace_enable_sysctl(struct ctl_table *table, int write,
  1983. struct file *file, void __user *buffer, size_t *lenp,
  1984. loff_t *ppos)
  1985. {
  1986. int ret;
  1987. if (unlikely(ftrace_disabled))
  1988. return -ENODEV;
  1989. mutex_lock(&ftrace_lock);
  1990. ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
  1991. if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
  1992. goto out;
  1993. last_ftrace_enabled = ftrace_enabled;
  1994. if (ftrace_enabled) {
  1995. ftrace_startup_sysctl();
  1996. /* we are starting ftrace again */
  1997. if (ftrace_list != &ftrace_list_end) {
  1998. if (ftrace_list->next == &ftrace_list_end)
  1999. ftrace_trace_function = ftrace_list->func;
  2000. else
  2001. ftrace_trace_function = ftrace_list_func;
  2002. }
  2003. } else {
  2004. /* stopping ftrace calls (just send to ftrace_stub) */
  2005. ftrace_trace_function = ftrace_stub;
  2006. ftrace_shutdown_sysctl();
  2007. }
  2008. out:
  2009. mutex_unlock(&ftrace_lock);
  2010. return ret;
  2011. }
  2012. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2013. static atomic_t ftrace_graph_active;
  2014. static struct notifier_block ftrace_suspend_notifier;
  2015. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
  2016. {
  2017. return 0;
  2018. }
  2019. /* The callbacks that hook a function */
  2020. trace_func_graph_ret_t ftrace_graph_return =
  2021. (trace_func_graph_ret_t)ftrace_stub;
  2022. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  2023. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  2024. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  2025. {
  2026. int i;
  2027. int ret = 0;
  2028. unsigned long flags;
  2029. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  2030. struct task_struct *g, *t;
  2031. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  2032. ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
  2033. * sizeof(struct ftrace_ret_stack),
  2034. GFP_KERNEL);
  2035. if (!ret_stack_list[i]) {
  2036. start = 0;
  2037. end = i;
  2038. ret = -ENOMEM;
  2039. goto free;
  2040. }
  2041. }
  2042. read_lock_irqsave(&tasklist_lock, flags);
  2043. do_each_thread(g, t) {
  2044. if (start == end) {
  2045. ret = -EAGAIN;
  2046. goto unlock;
  2047. }
  2048. if (t->ret_stack == NULL) {
  2049. t->curr_ret_stack = -1;
  2050. /* Make sure IRQs see the -1 first: */
  2051. barrier();
  2052. t->ret_stack = ret_stack_list[start++];
  2053. atomic_set(&t->tracing_graph_pause, 0);
  2054. atomic_set(&t->trace_overrun, 0);
  2055. }
  2056. } while_each_thread(g, t);
  2057. unlock:
  2058. read_unlock_irqrestore(&tasklist_lock, flags);
  2059. free:
  2060. for (i = start; i < end; i++)
  2061. kfree(ret_stack_list[i]);
  2062. return ret;
  2063. }
  2064. /* Allocate a return stack for each task */
  2065. static int start_graph_tracing(void)
  2066. {
  2067. struct ftrace_ret_stack **ret_stack_list;
  2068. int ret;
  2069. ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
  2070. sizeof(struct ftrace_ret_stack *),
  2071. GFP_KERNEL);
  2072. if (!ret_stack_list)
  2073. return -ENOMEM;
  2074. do {
  2075. ret = alloc_retstack_tasklist(ret_stack_list);
  2076. } while (ret == -EAGAIN);
  2077. kfree(ret_stack_list);
  2078. return ret;
  2079. }
  2080. /*
  2081. * Hibernation protection.
  2082. * The state of the current task is too much unstable during
  2083. * suspend/restore to disk. We want to protect against that.
  2084. */
  2085. static int
  2086. ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
  2087. void *unused)
  2088. {
  2089. switch (state) {
  2090. case PM_HIBERNATION_PREPARE:
  2091. pause_graph_tracing();
  2092. break;
  2093. case PM_POST_HIBERNATION:
  2094. unpause_graph_tracing();
  2095. break;
  2096. }
  2097. return NOTIFY_DONE;
  2098. }
  2099. int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  2100. trace_func_graph_ent_t entryfunc)
  2101. {
  2102. int ret = 0;
  2103. mutex_lock(&ftrace_lock);
  2104. ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
  2105. register_pm_notifier(&ftrace_suspend_notifier);
  2106. atomic_inc(&ftrace_graph_active);
  2107. ret = start_graph_tracing();
  2108. if (ret) {
  2109. atomic_dec(&ftrace_graph_active);
  2110. goto out;
  2111. }
  2112. ftrace_graph_return = retfunc;
  2113. ftrace_graph_entry = entryfunc;
  2114. ftrace_startup(FTRACE_START_FUNC_RET);
  2115. out:
  2116. mutex_unlock(&ftrace_lock);
  2117. return ret;
  2118. }
  2119. void unregister_ftrace_graph(void)
  2120. {
  2121. mutex_lock(&ftrace_lock);
  2122. atomic_dec(&ftrace_graph_active);
  2123. ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
  2124. ftrace_graph_entry = ftrace_graph_entry_stub;
  2125. ftrace_shutdown(FTRACE_STOP_FUNC_RET);
  2126. unregister_pm_notifier(&ftrace_suspend_notifier);
  2127. mutex_unlock(&ftrace_lock);
  2128. }
  2129. /* Allocate a return stack for newly created task */
  2130. void ftrace_graph_init_task(struct task_struct *t)
  2131. {
  2132. if (atomic_read(&ftrace_graph_active)) {
  2133. t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  2134. * sizeof(struct ftrace_ret_stack),
  2135. GFP_KERNEL);
  2136. if (!t->ret_stack)
  2137. return;
  2138. t->curr_ret_stack = -1;
  2139. atomic_set(&t->tracing_graph_pause, 0);
  2140. atomic_set(&t->trace_overrun, 0);
  2141. } else
  2142. t->ret_stack = NULL;
  2143. }
  2144. void ftrace_graph_exit_task(struct task_struct *t)
  2145. {
  2146. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  2147. t->ret_stack = NULL;
  2148. /* NULL must become visible to IRQs before we free it: */
  2149. barrier();
  2150. kfree(ret_stack);
  2151. }
  2152. void ftrace_graph_stop(void)
  2153. {
  2154. ftrace_stop();
  2155. }
  2156. #endif