ftrace.c 56 KB

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