ftrace.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681
  1. /*
  2. * Infrastructure for profiling code inserted by 'gcc -pg'.
  3. *
  4. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  5. * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
  6. *
  7. * Originally ported from the -rt patch by:
  8. * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
  9. *
  10. * Based on code in the latency_tracer, that is:
  11. *
  12. * Copyright (C) 2004-2006 Ingo Molnar
  13. * Copyright (C) 2004 William Lee Irwin III
  14. */
  15. #include <linux/stop_machine.h>
  16. #include <linux/clocksource.h>
  17. #include <linux/kallsyms.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/suspend.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/hardirq.h>
  22. #include <linux/kthread.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/kprobes.h>
  25. #include <linux/ftrace.h>
  26. #include <linux/sysctl.h>
  27. #include <linux/ctype.h>
  28. #include <linux/list.h>
  29. #include <linux/hash.h>
  30. #include <asm/ftrace.h>
  31. #include "trace.h"
  32. #define FTRACE_WARN_ON(cond) \
  33. do { \
  34. if (WARN_ON(cond)) \
  35. ftrace_kill(); \
  36. } while (0)
  37. #define FTRACE_WARN_ON_ONCE(cond) \
  38. do { \
  39. if (WARN_ON_ONCE(cond)) \
  40. ftrace_kill(); \
  41. } while (0)
  42. /* hash bits for specific function selection */
  43. #define FTRACE_HASH_BITS 7
  44. #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
  45. /* ftrace_enabled is a method to turn ftrace on or off */
  46. int ftrace_enabled __read_mostly;
  47. static int last_ftrace_enabled;
  48. /* Quick disabling of function tracer. */
  49. int function_trace_stop;
  50. /*
  51. * ftrace_disabled is set when an anomaly is discovered.
  52. * ftrace_disabled is much stronger than ftrace_enabled.
  53. */
  54. static int ftrace_disabled __read_mostly;
  55. static DEFINE_MUTEX(ftrace_lock);
  56. static struct ftrace_ops ftrace_list_end __read_mostly =
  57. {
  58. .func = ftrace_stub,
  59. };
  60. static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
  61. ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
  62. ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
  63. ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
  64. static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
  65. {
  66. struct ftrace_ops *op = ftrace_list;
  67. /* in case someone actually ports this to alpha! */
  68. read_barrier_depends();
  69. while (op != &ftrace_list_end) {
  70. /* silly alpha */
  71. read_barrier_depends();
  72. op->func(ip, parent_ip);
  73. op = op->next;
  74. };
  75. }
  76. static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
  77. {
  78. if (!test_tsk_trace_trace(current))
  79. return;
  80. ftrace_pid_function(ip, parent_ip);
  81. }
  82. static void set_ftrace_pid_function(ftrace_func_t func)
  83. {
  84. /* do not set ftrace_pid_function to itself! */
  85. if (func != ftrace_pid_func)
  86. ftrace_pid_function = func;
  87. }
  88. /**
  89. * clear_ftrace_function - reset the ftrace function
  90. *
  91. * This NULLs the ftrace function and in essence stops
  92. * tracing. There may be lag
  93. */
  94. void clear_ftrace_function(void)
  95. {
  96. ftrace_trace_function = ftrace_stub;
  97. __ftrace_trace_function = ftrace_stub;
  98. ftrace_pid_function = ftrace_stub;
  99. }
  100. #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  101. /*
  102. * For those archs that do not test ftrace_trace_stop in their
  103. * mcount call site, we need to do it from C.
  104. */
  105. static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
  106. {
  107. if (function_trace_stop)
  108. return;
  109. __ftrace_trace_function(ip, parent_ip);
  110. }
  111. #endif
  112. static int __register_ftrace_function(struct ftrace_ops *ops)
  113. {
  114. ops->next = ftrace_list;
  115. /*
  116. * We are entering ops into the ftrace_list but another
  117. * CPU might be walking that list. We need to make sure
  118. * the ops->next pointer is valid before another CPU sees
  119. * the ops pointer included into the ftrace_list.
  120. */
  121. smp_wmb();
  122. ftrace_list = ops;
  123. if (ftrace_enabled) {
  124. ftrace_func_t func;
  125. if (ops->next == &ftrace_list_end)
  126. func = ops->func;
  127. else
  128. func = ftrace_list_func;
  129. if (ftrace_pid_trace) {
  130. set_ftrace_pid_function(func);
  131. func = ftrace_pid_func;
  132. }
  133. /*
  134. * For one func, simply call it directly.
  135. * For more than one func, call the chain.
  136. */
  137. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  138. ftrace_trace_function = func;
  139. #else
  140. __ftrace_trace_function = func;
  141. ftrace_trace_function = ftrace_test_stop_func;
  142. #endif
  143. }
  144. return 0;
  145. }
  146. static int __unregister_ftrace_function(struct ftrace_ops *ops)
  147. {
  148. struct ftrace_ops **p;
  149. /*
  150. * If we are removing the last function, then simply point
  151. * to the ftrace_stub.
  152. */
  153. if (ftrace_list == ops && ops->next == &ftrace_list_end) {
  154. ftrace_trace_function = ftrace_stub;
  155. ftrace_list = &ftrace_list_end;
  156. return 0;
  157. }
  158. for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
  159. if (*p == ops)
  160. break;
  161. if (*p != ops)
  162. return -1;
  163. *p = (*p)->next;
  164. if (ftrace_enabled) {
  165. /* If we only have one func left, then call that directly */
  166. if (ftrace_list->next == &ftrace_list_end) {
  167. ftrace_func_t func = ftrace_list->func;
  168. if (ftrace_pid_trace) {
  169. set_ftrace_pid_function(func);
  170. func = ftrace_pid_func;
  171. }
  172. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  173. ftrace_trace_function = func;
  174. #else
  175. __ftrace_trace_function = func;
  176. #endif
  177. }
  178. }
  179. return 0;
  180. }
  181. static void ftrace_update_pid_func(void)
  182. {
  183. ftrace_func_t func;
  184. mutex_lock(&ftrace_lock);
  185. if (ftrace_trace_function == ftrace_stub)
  186. goto out;
  187. func = ftrace_trace_function;
  188. if (ftrace_pid_trace) {
  189. set_ftrace_pid_function(func);
  190. func = ftrace_pid_func;
  191. } else {
  192. if (func == ftrace_pid_func)
  193. func = ftrace_pid_function;
  194. }
  195. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  196. ftrace_trace_function = func;
  197. #else
  198. __ftrace_trace_function = func;
  199. #endif
  200. out:
  201. mutex_unlock(&ftrace_lock);
  202. }
  203. /* set when tracing only a pid */
  204. struct pid *ftrace_pid_trace;
  205. static struct pid * const ftrace_swapper_pid = &init_struct_pid;
  206. #ifdef CONFIG_DYNAMIC_FTRACE
  207. #ifndef CONFIG_FTRACE_MCOUNT_RECORD
  208. # error Dynamic ftrace depends on MCOUNT_RECORD
  209. #endif
  210. static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
  211. struct ftrace_func_probe {
  212. struct hlist_node node;
  213. struct ftrace_probe_ops *ops;
  214. unsigned long flags;
  215. unsigned long ip;
  216. void *data;
  217. struct rcu_head rcu;
  218. };
  219. enum {
  220. FTRACE_ENABLE_CALLS = (1 << 0),
  221. FTRACE_DISABLE_CALLS = (1 << 1),
  222. FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
  223. FTRACE_ENABLE_MCOUNT = (1 << 3),
  224. FTRACE_DISABLE_MCOUNT = (1 << 4),
  225. FTRACE_START_FUNC_RET = (1 << 5),
  226. FTRACE_STOP_FUNC_RET = (1 << 6),
  227. };
  228. static int ftrace_filtered;
  229. static LIST_HEAD(ftrace_new_addrs);
  230. static DEFINE_MUTEX(ftrace_regex_lock);
  231. struct ftrace_page {
  232. struct ftrace_page *next;
  233. int index;
  234. struct dyn_ftrace records[];
  235. };
  236. #define ENTRIES_PER_PAGE \
  237. ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
  238. /* estimate from running different kernels */
  239. #define NR_TO_INIT 10000
  240. static struct ftrace_page *ftrace_pages_start;
  241. static struct ftrace_page *ftrace_pages;
  242. static struct dyn_ftrace *ftrace_free_records;
  243. /*
  244. * This is a double for. Do not use 'break' to break out of the loop,
  245. * you must use a goto.
  246. */
  247. #define do_for_each_ftrace_rec(pg, rec) \
  248. for (pg = ftrace_pages_start; pg; pg = pg->next) { \
  249. int _____i; \
  250. for (_____i = 0; _____i < pg->index; _____i++) { \
  251. rec = &pg->records[_____i];
  252. #define while_for_each_ftrace_rec() \
  253. } \
  254. }
  255. #ifdef CONFIG_KPROBES
  256. static int frozen_record_count;
  257. static inline void freeze_record(struct dyn_ftrace *rec)
  258. {
  259. if (!(rec->flags & FTRACE_FL_FROZEN)) {
  260. rec->flags |= FTRACE_FL_FROZEN;
  261. frozen_record_count++;
  262. }
  263. }
  264. static inline void unfreeze_record(struct dyn_ftrace *rec)
  265. {
  266. if (rec->flags & FTRACE_FL_FROZEN) {
  267. rec->flags &= ~FTRACE_FL_FROZEN;
  268. frozen_record_count--;
  269. }
  270. }
  271. static inline int record_frozen(struct dyn_ftrace *rec)
  272. {
  273. return rec->flags & FTRACE_FL_FROZEN;
  274. }
  275. #else
  276. # define freeze_record(rec) ({ 0; })
  277. # define unfreeze_record(rec) ({ 0; })
  278. # define record_frozen(rec) ({ 0; })
  279. #endif /* CONFIG_KPROBES */
  280. static void ftrace_free_rec(struct dyn_ftrace *rec)
  281. {
  282. rec->ip = (unsigned long)ftrace_free_records;
  283. ftrace_free_records = rec;
  284. rec->flags |= FTRACE_FL_FREE;
  285. }
  286. void ftrace_release(void *start, unsigned long size)
  287. {
  288. struct dyn_ftrace *rec;
  289. struct ftrace_page *pg;
  290. unsigned long s = (unsigned long)start;
  291. unsigned long e = s + size;
  292. if (ftrace_disabled || !start)
  293. return;
  294. mutex_lock(&ftrace_lock);
  295. do_for_each_ftrace_rec(pg, rec) {
  296. if ((rec->ip >= s) && (rec->ip < e))
  297. ftrace_free_rec(rec);
  298. } while_for_each_ftrace_rec();
  299. mutex_unlock(&ftrace_lock);
  300. }
  301. static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
  302. {
  303. struct dyn_ftrace *rec;
  304. /* First check for freed records */
  305. if (ftrace_free_records) {
  306. rec = ftrace_free_records;
  307. if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
  308. FTRACE_WARN_ON_ONCE(1);
  309. ftrace_free_records = NULL;
  310. return NULL;
  311. }
  312. ftrace_free_records = (void *)rec->ip;
  313. memset(rec, 0, sizeof(*rec));
  314. return rec;
  315. }
  316. if (ftrace_pages->index == ENTRIES_PER_PAGE) {
  317. if (!ftrace_pages->next) {
  318. /* allocate another page */
  319. ftrace_pages->next =
  320. (void *)get_zeroed_page(GFP_KERNEL);
  321. if (!ftrace_pages->next)
  322. return NULL;
  323. }
  324. ftrace_pages = ftrace_pages->next;
  325. }
  326. return &ftrace_pages->records[ftrace_pages->index++];
  327. }
  328. static struct dyn_ftrace *
  329. ftrace_record_ip(unsigned long ip)
  330. {
  331. struct dyn_ftrace *rec;
  332. if (ftrace_disabled)
  333. return NULL;
  334. rec = ftrace_alloc_dyn_node(ip);
  335. if (!rec)
  336. return NULL;
  337. rec->ip = ip;
  338. list_add(&rec->list, &ftrace_new_addrs);
  339. return rec;
  340. }
  341. static void print_ip_ins(const char *fmt, unsigned char *p)
  342. {
  343. int i;
  344. printk(KERN_CONT "%s", fmt);
  345. for (i = 0; i < MCOUNT_INSN_SIZE; i++)
  346. printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
  347. }
  348. static void ftrace_bug(int failed, unsigned long ip)
  349. {
  350. switch (failed) {
  351. case -EFAULT:
  352. FTRACE_WARN_ON_ONCE(1);
  353. pr_info("ftrace faulted on modifying ");
  354. print_ip_sym(ip);
  355. break;
  356. case -EINVAL:
  357. FTRACE_WARN_ON_ONCE(1);
  358. pr_info("ftrace failed to modify ");
  359. print_ip_sym(ip);
  360. print_ip_ins(" actual: ", (unsigned char *)ip);
  361. printk(KERN_CONT "\n");
  362. break;
  363. case -EPERM:
  364. FTRACE_WARN_ON_ONCE(1);
  365. pr_info("ftrace faulted on writing ");
  366. print_ip_sym(ip);
  367. break;
  368. default:
  369. FTRACE_WARN_ON_ONCE(1);
  370. pr_info("ftrace faulted on unknown error ");
  371. print_ip_sym(ip);
  372. }
  373. }
  374. static int
  375. __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
  376. {
  377. unsigned long ftrace_addr;
  378. unsigned long ip, fl;
  379. ftrace_addr = (unsigned long)FTRACE_ADDR;
  380. ip = rec->ip;
  381. /*
  382. * If this record is not to be traced and
  383. * it is not enabled then do nothing.
  384. *
  385. * If this record is not to be traced and
  386. * it is enabled then disable it.
  387. *
  388. */
  389. if (rec->flags & FTRACE_FL_NOTRACE) {
  390. if (rec->flags & FTRACE_FL_ENABLED)
  391. rec->flags &= ~FTRACE_FL_ENABLED;
  392. else
  393. return 0;
  394. } else if (ftrace_filtered && enable) {
  395. /*
  396. * Filtering is on:
  397. */
  398. fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
  399. /* Record is filtered and enabled, do nothing */
  400. if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
  401. return 0;
  402. /* Record is not filtered or enabled, do nothing */
  403. if (!fl)
  404. return 0;
  405. /* Record is not filtered but enabled, disable it */
  406. if (fl == FTRACE_FL_ENABLED)
  407. rec->flags &= ~FTRACE_FL_ENABLED;
  408. else
  409. /* Otherwise record is filtered but not enabled, enable it */
  410. rec->flags |= FTRACE_FL_ENABLED;
  411. } else {
  412. /* Disable or not filtered */
  413. if (enable) {
  414. /* if record is enabled, do nothing */
  415. if (rec->flags & FTRACE_FL_ENABLED)
  416. return 0;
  417. rec->flags |= FTRACE_FL_ENABLED;
  418. } else {
  419. /* if record is not enabled, do nothing */
  420. if (!(rec->flags & FTRACE_FL_ENABLED))
  421. return 0;
  422. rec->flags &= ~FTRACE_FL_ENABLED;
  423. }
  424. }
  425. if (rec->flags & FTRACE_FL_ENABLED)
  426. return ftrace_make_call(rec, ftrace_addr);
  427. else
  428. return ftrace_make_nop(NULL, rec, ftrace_addr);
  429. }
  430. static void ftrace_replace_code(int enable)
  431. {
  432. struct dyn_ftrace *rec;
  433. struct ftrace_page *pg;
  434. int failed;
  435. do_for_each_ftrace_rec(pg, rec) {
  436. /*
  437. * Skip over free records and records that have
  438. * failed.
  439. */
  440. if (rec->flags & FTRACE_FL_FREE ||
  441. rec->flags & FTRACE_FL_FAILED)
  442. continue;
  443. /* ignore updates to this record's mcount site */
  444. if (get_kprobe((void *)rec->ip)) {
  445. freeze_record(rec);
  446. continue;
  447. } else {
  448. unfreeze_record(rec);
  449. }
  450. failed = __ftrace_replace_code(rec, enable);
  451. if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
  452. rec->flags |= FTRACE_FL_FAILED;
  453. if ((system_state == SYSTEM_BOOTING) ||
  454. !core_kernel_text(rec->ip)) {
  455. ftrace_free_rec(rec);
  456. } else
  457. ftrace_bug(failed, rec->ip);
  458. }
  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_probe *rec;
  668. struct hlist_node *hnd = v;
  669. char str[KSYM_SYMBOL_LEN];
  670. rec = hlist_entry(hnd, struct ftrace_func_probe, 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. unsigned int search_len;
  973. struct ftrace_page *pg;
  974. struct dyn_ftrace *rec;
  975. unsigned long flag;
  976. char *search;
  977. int type;
  978. int not;
  979. flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  980. type = ftrace_setup_glob(buff, len, &search, &not);
  981. search_len = strlen(search);
  982. mutex_lock(&ftrace_lock);
  983. do_for_each_ftrace_rec(pg, rec) {
  984. if (rec->flags & FTRACE_FL_FAILED)
  985. continue;
  986. if (ftrace_match_record(rec, search, search_len, type)) {
  987. if (not)
  988. rec->flags &= ~flag;
  989. else
  990. rec->flags |= flag;
  991. }
  992. /*
  993. * Only enable filtering if we have a function that
  994. * is filtered on.
  995. */
  996. if (enable && (rec->flags & FTRACE_FL_FILTER))
  997. ftrace_filtered = 1;
  998. } while_for_each_ftrace_rec();
  999. mutex_unlock(&ftrace_lock);
  1000. }
  1001. static int
  1002. ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
  1003. char *regex, int len, int type)
  1004. {
  1005. char str[KSYM_SYMBOL_LEN];
  1006. char *modname;
  1007. kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
  1008. if (!modname || strcmp(modname, mod))
  1009. return 0;
  1010. /* blank search means to match all funcs in the mod */
  1011. if (len)
  1012. return ftrace_match(str, regex, len, type);
  1013. else
  1014. return 1;
  1015. }
  1016. static void ftrace_match_module_records(char *buff, char *mod, int enable)
  1017. {
  1018. unsigned search_len = 0;
  1019. struct ftrace_page *pg;
  1020. struct dyn_ftrace *rec;
  1021. int type = MATCH_FULL;
  1022. char *search = buff;
  1023. unsigned long flag;
  1024. int not = 0;
  1025. flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1026. /* blank or '*' mean the same */
  1027. if (strcmp(buff, "*") == 0)
  1028. buff[0] = 0;
  1029. /* handle the case of 'dont filter this module' */
  1030. if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
  1031. buff[0] = 0;
  1032. not = 1;
  1033. }
  1034. if (strlen(buff)) {
  1035. type = ftrace_setup_glob(buff, strlen(buff), &search, &not);
  1036. search_len = strlen(search);
  1037. }
  1038. mutex_lock(&ftrace_lock);
  1039. do_for_each_ftrace_rec(pg, rec) {
  1040. if (rec->flags & FTRACE_FL_FAILED)
  1041. continue;
  1042. if (ftrace_match_module_record(rec, mod,
  1043. search, search_len, type)) {
  1044. if (not)
  1045. rec->flags &= ~flag;
  1046. else
  1047. rec->flags |= flag;
  1048. }
  1049. if (enable && (rec->flags & FTRACE_FL_FILTER))
  1050. ftrace_filtered = 1;
  1051. } while_for_each_ftrace_rec();
  1052. mutex_unlock(&ftrace_lock);
  1053. }
  1054. /*
  1055. * We register the module command as a template to show others how
  1056. * to register the a command as well.
  1057. */
  1058. static int
  1059. ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
  1060. {
  1061. char *mod;
  1062. /*
  1063. * cmd == 'mod' because we only registered this func
  1064. * for the 'mod' ftrace_func_command.
  1065. * But if you register one func with multiple commands,
  1066. * you can tell which command was used by the cmd
  1067. * parameter.
  1068. */
  1069. /* we must have a module name */
  1070. if (!param)
  1071. return -EINVAL;
  1072. mod = strsep(&param, ":");
  1073. if (!strlen(mod))
  1074. return -EINVAL;
  1075. ftrace_match_module_records(func, mod, enable);
  1076. return 0;
  1077. }
  1078. static struct ftrace_func_command ftrace_mod_cmd = {
  1079. .name = "mod",
  1080. .func = ftrace_mod_callback,
  1081. };
  1082. static int __init ftrace_mod_cmd_init(void)
  1083. {
  1084. return register_ftrace_command(&ftrace_mod_cmd);
  1085. }
  1086. device_initcall(ftrace_mod_cmd_init);
  1087. static void
  1088. function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
  1089. {
  1090. struct ftrace_func_probe *entry;
  1091. struct hlist_head *hhd;
  1092. struct hlist_node *n;
  1093. unsigned long key;
  1094. int resched;
  1095. key = hash_long(ip, FTRACE_HASH_BITS);
  1096. hhd = &ftrace_func_hash[key];
  1097. if (hlist_empty(hhd))
  1098. return;
  1099. /*
  1100. * Disable preemption for these calls to prevent a RCU grace
  1101. * period. This syncs the hash iteration and freeing of items
  1102. * on the hash. rcu_read_lock is too dangerous here.
  1103. */
  1104. resched = ftrace_preempt_disable();
  1105. hlist_for_each_entry_rcu(entry, n, hhd, node) {
  1106. if (entry->ip == ip)
  1107. entry->ops->func(ip, parent_ip, &entry->data);
  1108. }
  1109. ftrace_preempt_enable(resched);
  1110. }
  1111. static struct ftrace_ops trace_probe_ops __read_mostly =
  1112. {
  1113. .func = function_trace_probe_call,
  1114. };
  1115. static int ftrace_probe_registered;
  1116. static void __enable_ftrace_function_probe(void)
  1117. {
  1118. int i;
  1119. if (ftrace_probe_registered)
  1120. return;
  1121. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1122. struct hlist_head *hhd = &ftrace_func_hash[i];
  1123. if (hhd->first)
  1124. break;
  1125. }
  1126. /* Nothing registered? */
  1127. if (i == FTRACE_FUNC_HASHSIZE)
  1128. return;
  1129. __register_ftrace_function(&trace_probe_ops);
  1130. ftrace_startup(0);
  1131. ftrace_probe_registered = 1;
  1132. }
  1133. static void __disable_ftrace_function_probe(void)
  1134. {
  1135. int i;
  1136. if (!ftrace_probe_registered)
  1137. return;
  1138. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1139. struct hlist_head *hhd = &ftrace_func_hash[i];
  1140. if (hhd->first)
  1141. return;
  1142. }
  1143. /* no more funcs left */
  1144. __unregister_ftrace_function(&trace_probe_ops);
  1145. ftrace_shutdown(0);
  1146. ftrace_probe_registered = 0;
  1147. }
  1148. static void ftrace_free_entry_rcu(struct rcu_head *rhp)
  1149. {
  1150. struct ftrace_func_probe *entry =
  1151. container_of(rhp, struct ftrace_func_probe, rcu);
  1152. if (entry->ops->free)
  1153. entry->ops->free(&entry->data);
  1154. kfree(entry);
  1155. }
  1156. int
  1157. register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1158. void *data)
  1159. {
  1160. struct ftrace_func_probe *entry;
  1161. struct ftrace_page *pg;
  1162. struct dyn_ftrace *rec;
  1163. int type, len, not;
  1164. unsigned long key;
  1165. int count = 0;
  1166. char *search;
  1167. type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
  1168. len = strlen(search);
  1169. /* we do not support '!' for function probes */
  1170. if (WARN_ON(not))
  1171. return -EINVAL;
  1172. mutex_lock(&ftrace_lock);
  1173. do_for_each_ftrace_rec(pg, rec) {
  1174. if (rec->flags & FTRACE_FL_FAILED)
  1175. continue;
  1176. if (!ftrace_match_record(rec, search, len, type))
  1177. continue;
  1178. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  1179. if (!entry) {
  1180. /* If we did not process any, then return error */
  1181. if (!count)
  1182. count = -ENOMEM;
  1183. goto out_unlock;
  1184. }
  1185. count++;
  1186. entry->data = data;
  1187. /*
  1188. * The caller might want to do something special
  1189. * for each function we find. We call the callback
  1190. * to give the caller an opportunity to do so.
  1191. */
  1192. if (ops->callback) {
  1193. if (ops->callback(rec->ip, &entry->data) < 0) {
  1194. /* caller does not like this func */
  1195. kfree(entry);
  1196. continue;
  1197. }
  1198. }
  1199. entry->ops = ops;
  1200. entry->ip = rec->ip;
  1201. key = hash_long(entry->ip, FTRACE_HASH_BITS);
  1202. hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
  1203. } while_for_each_ftrace_rec();
  1204. __enable_ftrace_function_probe();
  1205. out_unlock:
  1206. mutex_unlock(&ftrace_lock);
  1207. return count;
  1208. }
  1209. enum {
  1210. PROBE_TEST_FUNC = 1,
  1211. PROBE_TEST_DATA = 2
  1212. };
  1213. static void
  1214. __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1215. void *data, int flags)
  1216. {
  1217. struct ftrace_func_probe *entry;
  1218. struct hlist_node *n, *tmp;
  1219. char str[KSYM_SYMBOL_LEN];
  1220. int type = MATCH_FULL;
  1221. int i, len = 0;
  1222. char *search;
  1223. if (glob && (strcmp(glob, "*") || !strlen(glob)))
  1224. glob = NULL;
  1225. else {
  1226. int not;
  1227. type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
  1228. len = strlen(search);
  1229. /* we do not support '!' for function probes */
  1230. if (WARN_ON(not))
  1231. return;
  1232. }
  1233. mutex_lock(&ftrace_lock);
  1234. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1235. struct hlist_head *hhd = &ftrace_func_hash[i];
  1236. hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
  1237. /* break up if statements for readability */
  1238. if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
  1239. continue;
  1240. if ((flags & PROBE_TEST_DATA) && entry->data != data)
  1241. continue;
  1242. /* do this last, since it is the most expensive */
  1243. if (glob) {
  1244. kallsyms_lookup(entry->ip, NULL, NULL,
  1245. NULL, str);
  1246. if (!ftrace_match(str, glob, len, type))
  1247. continue;
  1248. }
  1249. hlist_del(&entry->node);
  1250. call_rcu(&entry->rcu, ftrace_free_entry_rcu);
  1251. }
  1252. }
  1253. __disable_ftrace_function_probe();
  1254. mutex_unlock(&ftrace_lock);
  1255. }
  1256. void
  1257. unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1258. void *data)
  1259. {
  1260. __unregister_ftrace_function_probe(glob, ops, data,
  1261. PROBE_TEST_FUNC | PROBE_TEST_DATA);
  1262. }
  1263. void
  1264. unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
  1265. {
  1266. __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
  1267. }
  1268. void unregister_ftrace_function_probe_all(char *glob)
  1269. {
  1270. __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
  1271. }
  1272. static LIST_HEAD(ftrace_commands);
  1273. static DEFINE_MUTEX(ftrace_cmd_mutex);
  1274. int register_ftrace_command(struct ftrace_func_command *cmd)
  1275. {
  1276. struct ftrace_func_command *p;
  1277. int ret = 0;
  1278. mutex_lock(&ftrace_cmd_mutex);
  1279. list_for_each_entry(p, &ftrace_commands, list) {
  1280. if (strcmp(cmd->name, p->name) == 0) {
  1281. ret = -EBUSY;
  1282. goto out_unlock;
  1283. }
  1284. }
  1285. list_add(&cmd->list, &ftrace_commands);
  1286. out_unlock:
  1287. mutex_unlock(&ftrace_cmd_mutex);
  1288. return ret;
  1289. }
  1290. int unregister_ftrace_command(struct ftrace_func_command *cmd)
  1291. {
  1292. struct ftrace_func_command *p, *n;
  1293. int ret = -ENODEV;
  1294. mutex_lock(&ftrace_cmd_mutex);
  1295. list_for_each_entry_safe(p, n, &ftrace_commands, list) {
  1296. if (strcmp(cmd->name, p->name) == 0) {
  1297. ret = 0;
  1298. list_del_init(&p->list);
  1299. goto out_unlock;
  1300. }
  1301. }
  1302. out_unlock:
  1303. mutex_unlock(&ftrace_cmd_mutex);
  1304. return ret;
  1305. }
  1306. static int ftrace_process_regex(char *buff, int len, int enable)
  1307. {
  1308. char *func, *command, *next = buff;
  1309. struct ftrace_func_command *p;
  1310. int ret = -EINVAL;
  1311. func = strsep(&next, ":");
  1312. if (!next) {
  1313. ftrace_match_records(func, len, enable);
  1314. return 0;
  1315. }
  1316. /* command found */
  1317. command = strsep(&next, ":");
  1318. mutex_lock(&ftrace_cmd_mutex);
  1319. list_for_each_entry(p, &ftrace_commands, list) {
  1320. if (strcmp(p->name, command) == 0) {
  1321. ret = p->func(func, command, next, enable);
  1322. goto out_unlock;
  1323. }
  1324. }
  1325. out_unlock:
  1326. mutex_unlock(&ftrace_cmd_mutex);
  1327. return ret;
  1328. }
  1329. static ssize_t
  1330. ftrace_regex_write(struct file *file, const char __user *ubuf,
  1331. size_t cnt, loff_t *ppos, int enable)
  1332. {
  1333. struct ftrace_iterator *iter;
  1334. char ch;
  1335. size_t read = 0;
  1336. ssize_t ret;
  1337. if (!cnt || cnt < 0)
  1338. return 0;
  1339. mutex_lock(&ftrace_regex_lock);
  1340. if (file->f_mode & FMODE_READ) {
  1341. struct seq_file *m = file->private_data;
  1342. iter = m->private;
  1343. } else
  1344. iter = file->private_data;
  1345. if (!*ppos) {
  1346. iter->flags &= ~FTRACE_ITER_CONT;
  1347. iter->buffer_idx = 0;
  1348. }
  1349. ret = get_user(ch, ubuf++);
  1350. if (ret)
  1351. goto out;
  1352. read++;
  1353. cnt--;
  1354. if (!(iter->flags & ~FTRACE_ITER_CONT)) {
  1355. /* skip white space */
  1356. while (cnt && isspace(ch)) {
  1357. ret = get_user(ch, ubuf++);
  1358. if (ret)
  1359. goto out;
  1360. read++;
  1361. cnt--;
  1362. }
  1363. if (isspace(ch)) {
  1364. file->f_pos += read;
  1365. ret = read;
  1366. goto out;
  1367. }
  1368. iter->buffer_idx = 0;
  1369. }
  1370. while (cnt && !isspace(ch)) {
  1371. if (iter->buffer_idx < FTRACE_BUFF_MAX)
  1372. iter->buffer[iter->buffer_idx++] = ch;
  1373. else {
  1374. ret = -EINVAL;
  1375. goto out;
  1376. }
  1377. ret = get_user(ch, ubuf++);
  1378. if (ret)
  1379. goto out;
  1380. read++;
  1381. cnt--;
  1382. }
  1383. if (isspace(ch)) {
  1384. iter->filtered++;
  1385. iter->buffer[iter->buffer_idx] = 0;
  1386. ret = ftrace_process_regex(iter->buffer,
  1387. iter->buffer_idx, enable);
  1388. if (ret)
  1389. goto out;
  1390. iter->buffer_idx = 0;
  1391. } else
  1392. iter->flags |= FTRACE_ITER_CONT;
  1393. file->f_pos += read;
  1394. ret = read;
  1395. out:
  1396. mutex_unlock(&ftrace_regex_lock);
  1397. return ret;
  1398. }
  1399. static ssize_t
  1400. ftrace_filter_write(struct file *file, const char __user *ubuf,
  1401. size_t cnt, loff_t *ppos)
  1402. {
  1403. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  1404. }
  1405. static ssize_t
  1406. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  1407. size_t cnt, loff_t *ppos)
  1408. {
  1409. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  1410. }
  1411. static void
  1412. ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
  1413. {
  1414. if (unlikely(ftrace_disabled))
  1415. return;
  1416. mutex_lock(&ftrace_regex_lock);
  1417. if (reset)
  1418. ftrace_filter_reset(enable);
  1419. if (buf)
  1420. ftrace_match_records(buf, len, enable);
  1421. mutex_unlock(&ftrace_regex_lock);
  1422. }
  1423. /**
  1424. * ftrace_set_filter - set a function to filter on in ftrace
  1425. * @buf - the string that holds the function filter text.
  1426. * @len - the length of the string.
  1427. * @reset - non zero to reset all filters before applying this filter.
  1428. *
  1429. * Filters denote which functions should be enabled when tracing is enabled.
  1430. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  1431. */
  1432. void ftrace_set_filter(unsigned char *buf, int len, int reset)
  1433. {
  1434. ftrace_set_regex(buf, len, reset, 1);
  1435. }
  1436. /**
  1437. * ftrace_set_notrace - set a function to not trace in ftrace
  1438. * @buf - the string that holds the function notrace text.
  1439. * @len - the length of the string.
  1440. * @reset - non zero to reset all filters before applying this filter.
  1441. *
  1442. * Notrace Filters denote which functions should not be enabled when tracing
  1443. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  1444. * for tracing.
  1445. */
  1446. void ftrace_set_notrace(unsigned char *buf, int len, int reset)
  1447. {
  1448. ftrace_set_regex(buf, len, reset, 0);
  1449. }
  1450. static int
  1451. ftrace_regex_release(struct inode *inode, struct file *file, int enable)
  1452. {
  1453. struct seq_file *m = (struct seq_file *)file->private_data;
  1454. struct ftrace_iterator *iter;
  1455. mutex_lock(&ftrace_regex_lock);
  1456. if (file->f_mode & FMODE_READ) {
  1457. iter = m->private;
  1458. seq_release(inode, file);
  1459. } else
  1460. iter = file->private_data;
  1461. if (iter->buffer_idx) {
  1462. iter->filtered++;
  1463. iter->buffer[iter->buffer_idx] = 0;
  1464. ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
  1465. }
  1466. mutex_lock(&ftrace_lock);
  1467. if (ftrace_start_up && ftrace_enabled)
  1468. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  1469. mutex_unlock(&ftrace_lock);
  1470. kfree(iter);
  1471. mutex_unlock(&ftrace_regex_lock);
  1472. return 0;
  1473. }
  1474. static int
  1475. ftrace_filter_release(struct inode *inode, struct file *file)
  1476. {
  1477. return ftrace_regex_release(inode, file, 1);
  1478. }
  1479. static int
  1480. ftrace_notrace_release(struct inode *inode, struct file *file)
  1481. {
  1482. return ftrace_regex_release(inode, file, 0);
  1483. }
  1484. static struct file_operations ftrace_avail_fops = {
  1485. .open = ftrace_avail_open,
  1486. .read = seq_read,
  1487. .llseek = seq_lseek,
  1488. .release = ftrace_avail_release,
  1489. };
  1490. static struct file_operations ftrace_failures_fops = {
  1491. .open = ftrace_failures_open,
  1492. .read = seq_read,
  1493. .llseek = seq_lseek,
  1494. .release = ftrace_avail_release,
  1495. };
  1496. static struct file_operations ftrace_filter_fops = {
  1497. .open = ftrace_filter_open,
  1498. .read = ftrace_regex_read,
  1499. .write = ftrace_filter_write,
  1500. .llseek = ftrace_regex_lseek,
  1501. .release = ftrace_filter_release,
  1502. };
  1503. static struct file_operations ftrace_notrace_fops = {
  1504. .open = ftrace_notrace_open,
  1505. .read = ftrace_regex_read,
  1506. .write = ftrace_notrace_write,
  1507. .llseek = ftrace_regex_lseek,
  1508. .release = ftrace_notrace_release,
  1509. };
  1510. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1511. static DEFINE_MUTEX(graph_lock);
  1512. int ftrace_graph_count;
  1513. unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
  1514. static void *
  1515. g_next(struct seq_file *m, void *v, loff_t *pos)
  1516. {
  1517. unsigned long *array = m->private;
  1518. int index = *pos;
  1519. (*pos)++;
  1520. if (index >= ftrace_graph_count)
  1521. return NULL;
  1522. return &array[index];
  1523. }
  1524. static void *g_start(struct seq_file *m, loff_t *pos)
  1525. {
  1526. void *p = NULL;
  1527. mutex_lock(&graph_lock);
  1528. p = g_next(m, p, pos);
  1529. return p;
  1530. }
  1531. static void g_stop(struct seq_file *m, void *p)
  1532. {
  1533. mutex_unlock(&graph_lock);
  1534. }
  1535. static int g_show(struct seq_file *m, void *v)
  1536. {
  1537. unsigned long *ptr = v;
  1538. char str[KSYM_SYMBOL_LEN];
  1539. if (!ptr)
  1540. return 0;
  1541. kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
  1542. seq_printf(m, "%s\n", str);
  1543. return 0;
  1544. }
  1545. static struct seq_operations ftrace_graph_seq_ops = {
  1546. .start = g_start,
  1547. .next = g_next,
  1548. .stop = g_stop,
  1549. .show = g_show,
  1550. };
  1551. static int
  1552. ftrace_graph_open(struct inode *inode, struct file *file)
  1553. {
  1554. int ret = 0;
  1555. if (unlikely(ftrace_disabled))
  1556. return -ENODEV;
  1557. mutex_lock(&graph_lock);
  1558. if ((file->f_mode & FMODE_WRITE) &&
  1559. !(file->f_flags & O_APPEND)) {
  1560. ftrace_graph_count = 0;
  1561. memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
  1562. }
  1563. if (file->f_mode & FMODE_READ) {
  1564. ret = seq_open(file, &ftrace_graph_seq_ops);
  1565. if (!ret) {
  1566. struct seq_file *m = file->private_data;
  1567. m->private = ftrace_graph_funcs;
  1568. }
  1569. } else
  1570. file->private_data = ftrace_graph_funcs;
  1571. mutex_unlock(&graph_lock);
  1572. return ret;
  1573. }
  1574. static ssize_t
  1575. ftrace_graph_read(struct file *file, char __user *ubuf,
  1576. size_t cnt, loff_t *ppos)
  1577. {
  1578. if (file->f_mode & FMODE_READ)
  1579. return seq_read(file, ubuf, cnt, ppos);
  1580. else
  1581. return -EPERM;
  1582. }
  1583. static int
  1584. ftrace_set_func(unsigned long *array, int idx, char *buffer)
  1585. {
  1586. char str[KSYM_SYMBOL_LEN];
  1587. struct dyn_ftrace *rec;
  1588. struct ftrace_page *pg;
  1589. int found = 0;
  1590. int j;
  1591. if (ftrace_disabled)
  1592. return -ENODEV;
  1593. mutex_lock(&ftrace_lock);
  1594. do_for_each_ftrace_rec(pg, rec) {
  1595. if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
  1596. continue;
  1597. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  1598. if (strcmp(str, buffer) == 0) {
  1599. /* Return 1 if we add it to the array */
  1600. found = 1;
  1601. for (j = 0; j < idx; j++)
  1602. if (array[j] == rec->ip) {
  1603. found = 0;
  1604. break;
  1605. }
  1606. if (found)
  1607. array[idx] = rec->ip;
  1608. goto out;
  1609. }
  1610. } while_for_each_ftrace_rec();
  1611. out:
  1612. mutex_unlock(&ftrace_lock);
  1613. return found ? 0 : -EINVAL;
  1614. }
  1615. static ssize_t
  1616. ftrace_graph_write(struct file *file, const char __user *ubuf,
  1617. size_t cnt, loff_t *ppos)
  1618. {
  1619. unsigned char buffer[FTRACE_BUFF_MAX+1];
  1620. unsigned long *array;
  1621. size_t read = 0;
  1622. ssize_t ret;
  1623. int index = 0;
  1624. char ch;
  1625. if (!cnt || cnt < 0)
  1626. return 0;
  1627. mutex_lock(&graph_lock);
  1628. if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
  1629. ret = -EBUSY;
  1630. goto out;
  1631. }
  1632. if (file->f_mode & FMODE_READ) {
  1633. struct seq_file *m = file->private_data;
  1634. array = m->private;
  1635. } else
  1636. array = file->private_data;
  1637. ret = get_user(ch, ubuf++);
  1638. if (ret)
  1639. goto out;
  1640. read++;
  1641. cnt--;
  1642. /* skip white space */
  1643. while (cnt && isspace(ch)) {
  1644. ret = get_user(ch, ubuf++);
  1645. if (ret)
  1646. goto out;
  1647. read++;
  1648. cnt--;
  1649. }
  1650. if (isspace(ch)) {
  1651. *ppos += read;
  1652. ret = read;
  1653. goto out;
  1654. }
  1655. while (cnt && !isspace(ch)) {
  1656. if (index < FTRACE_BUFF_MAX)
  1657. buffer[index++] = ch;
  1658. else {
  1659. ret = -EINVAL;
  1660. goto out;
  1661. }
  1662. ret = get_user(ch, ubuf++);
  1663. if (ret)
  1664. goto out;
  1665. read++;
  1666. cnt--;
  1667. }
  1668. buffer[index] = 0;
  1669. /* we allow only one at a time */
  1670. ret = ftrace_set_func(array, ftrace_graph_count, buffer);
  1671. if (ret)
  1672. goto out;
  1673. ftrace_graph_count++;
  1674. file->f_pos += read;
  1675. ret = read;
  1676. out:
  1677. mutex_unlock(&graph_lock);
  1678. return ret;
  1679. }
  1680. static const struct file_operations ftrace_graph_fops = {
  1681. .open = ftrace_graph_open,
  1682. .read = ftrace_graph_read,
  1683. .write = ftrace_graph_write,
  1684. };
  1685. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1686. static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
  1687. {
  1688. struct dentry *entry;
  1689. entry = debugfs_create_file("available_filter_functions", 0444,
  1690. d_tracer, NULL, &ftrace_avail_fops);
  1691. if (!entry)
  1692. pr_warning("Could not create debugfs "
  1693. "'available_filter_functions' entry\n");
  1694. entry = debugfs_create_file("failures", 0444,
  1695. d_tracer, NULL, &ftrace_failures_fops);
  1696. if (!entry)
  1697. pr_warning("Could not create debugfs 'failures' entry\n");
  1698. entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
  1699. NULL, &ftrace_filter_fops);
  1700. if (!entry)
  1701. pr_warning("Could not create debugfs "
  1702. "'set_ftrace_filter' entry\n");
  1703. entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
  1704. NULL, &ftrace_notrace_fops);
  1705. if (!entry)
  1706. pr_warning("Could not create debugfs "
  1707. "'set_ftrace_notrace' entry\n");
  1708. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1709. entry = debugfs_create_file("set_graph_function", 0444, d_tracer,
  1710. NULL,
  1711. &ftrace_graph_fops);
  1712. if (!entry)
  1713. pr_warning("Could not create debugfs "
  1714. "'set_graph_function' entry\n");
  1715. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1716. return 0;
  1717. }
  1718. static int ftrace_convert_nops(struct module *mod,
  1719. unsigned long *start,
  1720. unsigned long *end)
  1721. {
  1722. unsigned long *p;
  1723. unsigned long addr;
  1724. unsigned long flags;
  1725. mutex_lock(&ftrace_lock);
  1726. p = start;
  1727. while (p < end) {
  1728. addr = ftrace_call_adjust(*p++);
  1729. /*
  1730. * Some architecture linkers will pad between
  1731. * the different mcount_loc sections of different
  1732. * object files to satisfy alignments.
  1733. * Skip any NULL pointers.
  1734. */
  1735. if (!addr)
  1736. continue;
  1737. ftrace_record_ip(addr);
  1738. }
  1739. /* disable interrupts to prevent kstop machine */
  1740. local_irq_save(flags);
  1741. ftrace_update_code(mod);
  1742. local_irq_restore(flags);
  1743. mutex_unlock(&ftrace_lock);
  1744. return 0;
  1745. }
  1746. void ftrace_init_module(struct module *mod,
  1747. unsigned long *start, unsigned long *end)
  1748. {
  1749. if (ftrace_disabled || start == end)
  1750. return;
  1751. ftrace_convert_nops(mod, start, end);
  1752. }
  1753. extern unsigned long __start_mcount_loc[];
  1754. extern unsigned long __stop_mcount_loc[];
  1755. void __init ftrace_init(void)
  1756. {
  1757. unsigned long count, addr, flags;
  1758. int ret;
  1759. /* Keep the ftrace pointer to the stub */
  1760. addr = (unsigned long)ftrace_stub;
  1761. local_irq_save(flags);
  1762. ftrace_dyn_arch_init(&addr);
  1763. local_irq_restore(flags);
  1764. /* ftrace_dyn_arch_init places the return code in addr */
  1765. if (addr)
  1766. goto failed;
  1767. count = __stop_mcount_loc - __start_mcount_loc;
  1768. ret = ftrace_dyn_table_alloc(count);
  1769. if (ret)
  1770. goto failed;
  1771. last_ftrace_enabled = ftrace_enabled = 1;
  1772. ret = ftrace_convert_nops(NULL,
  1773. __start_mcount_loc,
  1774. __stop_mcount_loc);
  1775. return;
  1776. failed:
  1777. ftrace_disabled = 1;
  1778. }
  1779. #else
  1780. static int __init ftrace_nodyn_init(void)
  1781. {
  1782. ftrace_enabled = 1;
  1783. return 0;
  1784. }
  1785. device_initcall(ftrace_nodyn_init);
  1786. static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
  1787. static inline void ftrace_startup_enable(int command) { }
  1788. /* Keep as macros so we do not need to define the commands */
  1789. # define ftrace_startup(command) do { } while (0)
  1790. # define ftrace_shutdown(command) do { } while (0)
  1791. # define ftrace_startup_sysctl() do { } while (0)
  1792. # define ftrace_shutdown_sysctl() do { } while (0)
  1793. #endif /* CONFIG_DYNAMIC_FTRACE */
  1794. static ssize_t
  1795. ftrace_pid_read(struct file *file, char __user *ubuf,
  1796. size_t cnt, loff_t *ppos)
  1797. {
  1798. char buf[64];
  1799. int r;
  1800. if (ftrace_pid_trace == ftrace_swapper_pid)
  1801. r = sprintf(buf, "swapper tasks\n");
  1802. else if (ftrace_pid_trace)
  1803. r = sprintf(buf, "%u\n", pid_nr(ftrace_pid_trace));
  1804. else
  1805. r = sprintf(buf, "no pid\n");
  1806. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1807. }
  1808. static void clear_ftrace_swapper(void)
  1809. {
  1810. struct task_struct *p;
  1811. int cpu;
  1812. get_online_cpus();
  1813. for_each_online_cpu(cpu) {
  1814. p = idle_task(cpu);
  1815. clear_tsk_trace_trace(p);
  1816. }
  1817. put_online_cpus();
  1818. }
  1819. static void set_ftrace_swapper(void)
  1820. {
  1821. struct task_struct *p;
  1822. int cpu;
  1823. get_online_cpus();
  1824. for_each_online_cpu(cpu) {
  1825. p = idle_task(cpu);
  1826. set_tsk_trace_trace(p);
  1827. }
  1828. put_online_cpus();
  1829. }
  1830. static void clear_ftrace_pid(struct pid *pid)
  1831. {
  1832. struct task_struct *p;
  1833. rcu_read_lock();
  1834. do_each_pid_task(pid, PIDTYPE_PID, p) {
  1835. clear_tsk_trace_trace(p);
  1836. } while_each_pid_task(pid, PIDTYPE_PID, p);
  1837. rcu_read_unlock();
  1838. put_pid(pid);
  1839. }
  1840. static void set_ftrace_pid(struct pid *pid)
  1841. {
  1842. struct task_struct *p;
  1843. rcu_read_lock();
  1844. do_each_pid_task(pid, PIDTYPE_PID, p) {
  1845. set_tsk_trace_trace(p);
  1846. } while_each_pid_task(pid, PIDTYPE_PID, p);
  1847. rcu_read_unlock();
  1848. }
  1849. static void clear_ftrace_pid_task(struct pid **pid)
  1850. {
  1851. if (*pid == ftrace_swapper_pid)
  1852. clear_ftrace_swapper();
  1853. else
  1854. clear_ftrace_pid(*pid);
  1855. *pid = NULL;
  1856. }
  1857. static void set_ftrace_pid_task(struct pid *pid)
  1858. {
  1859. if (pid == ftrace_swapper_pid)
  1860. set_ftrace_swapper();
  1861. else
  1862. set_ftrace_pid(pid);
  1863. }
  1864. static ssize_t
  1865. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  1866. size_t cnt, loff_t *ppos)
  1867. {
  1868. struct pid *pid;
  1869. char buf[64];
  1870. long val;
  1871. int ret;
  1872. if (cnt >= sizeof(buf))
  1873. return -EINVAL;
  1874. if (copy_from_user(&buf, ubuf, cnt))
  1875. return -EFAULT;
  1876. buf[cnt] = 0;
  1877. ret = strict_strtol(buf, 10, &val);
  1878. if (ret < 0)
  1879. return ret;
  1880. mutex_lock(&ftrace_lock);
  1881. if (val < 0) {
  1882. /* disable pid tracing */
  1883. if (!ftrace_pid_trace)
  1884. goto out;
  1885. clear_ftrace_pid_task(&ftrace_pid_trace);
  1886. } else {
  1887. /* swapper task is special */
  1888. if (!val) {
  1889. pid = ftrace_swapper_pid;
  1890. if (pid == ftrace_pid_trace)
  1891. goto out;
  1892. } else {
  1893. pid = find_get_pid(val);
  1894. if (pid == ftrace_pid_trace) {
  1895. put_pid(pid);
  1896. goto out;
  1897. }
  1898. }
  1899. if (ftrace_pid_trace)
  1900. clear_ftrace_pid_task(&ftrace_pid_trace);
  1901. if (!pid)
  1902. goto out;
  1903. ftrace_pid_trace = pid;
  1904. set_ftrace_pid_task(ftrace_pid_trace);
  1905. }
  1906. /* update the function call */
  1907. ftrace_update_pid_func();
  1908. ftrace_startup_enable(0);
  1909. out:
  1910. mutex_unlock(&ftrace_lock);
  1911. return cnt;
  1912. }
  1913. static struct file_operations ftrace_pid_fops = {
  1914. .read = ftrace_pid_read,
  1915. .write = ftrace_pid_write,
  1916. };
  1917. static __init int ftrace_init_debugfs(void)
  1918. {
  1919. struct dentry *d_tracer;
  1920. struct dentry *entry;
  1921. d_tracer = tracing_init_dentry();
  1922. if (!d_tracer)
  1923. return 0;
  1924. ftrace_init_dyn_debugfs(d_tracer);
  1925. entry = debugfs_create_file("set_ftrace_pid", 0644, d_tracer,
  1926. NULL, &ftrace_pid_fops);
  1927. if (!entry)
  1928. pr_warning("Could not create debugfs "
  1929. "'set_ftrace_pid' entry\n");
  1930. return 0;
  1931. }
  1932. fs_initcall(ftrace_init_debugfs);
  1933. /**
  1934. * ftrace_kill - kill ftrace
  1935. *
  1936. * This function should be used by panic code. It stops ftrace
  1937. * but in a not so nice way. If you need to simply kill ftrace
  1938. * from a non-atomic section, use ftrace_kill.
  1939. */
  1940. void ftrace_kill(void)
  1941. {
  1942. ftrace_disabled = 1;
  1943. ftrace_enabled = 0;
  1944. clear_ftrace_function();
  1945. }
  1946. /**
  1947. * register_ftrace_function - register a function for profiling
  1948. * @ops - ops structure that holds the function for profiling.
  1949. *
  1950. * Register a function to be called by all functions in the
  1951. * kernel.
  1952. *
  1953. * Note: @ops->func and all the functions it calls must be labeled
  1954. * with "notrace", otherwise it will go into a
  1955. * recursive loop.
  1956. */
  1957. int register_ftrace_function(struct ftrace_ops *ops)
  1958. {
  1959. int ret;
  1960. if (unlikely(ftrace_disabled))
  1961. return -1;
  1962. mutex_lock(&ftrace_lock);
  1963. ret = __register_ftrace_function(ops);
  1964. ftrace_startup(0);
  1965. mutex_unlock(&ftrace_lock);
  1966. return ret;
  1967. }
  1968. /**
  1969. * unregister_ftrace_function - unregister a function for profiling.
  1970. * @ops - ops structure that holds the function to unregister
  1971. *
  1972. * Unregister a function that was added to be called by ftrace profiling.
  1973. */
  1974. int unregister_ftrace_function(struct ftrace_ops *ops)
  1975. {
  1976. int ret;
  1977. mutex_lock(&ftrace_lock);
  1978. ret = __unregister_ftrace_function(ops);
  1979. ftrace_shutdown(0);
  1980. mutex_unlock(&ftrace_lock);
  1981. return ret;
  1982. }
  1983. int
  1984. ftrace_enable_sysctl(struct ctl_table *table, int write,
  1985. struct file *file, void __user *buffer, size_t *lenp,
  1986. loff_t *ppos)
  1987. {
  1988. int ret;
  1989. if (unlikely(ftrace_disabled))
  1990. return -ENODEV;
  1991. mutex_lock(&ftrace_lock);
  1992. ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
  1993. if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
  1994. goto out;
  1995. last_ftrace_enabled = ftrace_enabled;
  1996. if (ftrace_enabled) {
  1997. ftrace_startup_sysctl();
  1998. /* we are starting ftrace again */
  1999. if (ftrace_list != &ftrace_list_end) {
  2000. if (ftrace_list->next == &ftrace_list_end)
  2001. ftrace_trace_function = ftrace_list->func;
  2002. else
  2003. ftrace_trace_function = ftrace_list_func;
  2004. }
  2005. } else {
  2006. /* stopping ftrace calls (just send to ftrace_stub) */
  2007. ftrace_trace_function = ftrace_stub;
  2008. ftrace_shutdown_sysctl();
  2009. }
  2010. out:
  2011. mutex_unlock(&ftrace_lock);
  2012. return ret;
  2013. }
  2014. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2015. static atomic_t ftrace_graph_active;
  2016. static struct notifier_block ftrace_suspend_notifier;
  2017. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
  2018. {
  2019. return 0;
  2020. }
  2021. /* The callbacks that hook a function */
  2022. trace_func_graph_ret_t ftrace_graph_return =
  2023. (trace_func_graph_ret_t)ftrace_stub;
  2024. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  2025. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  2026. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  2027. {
  2028. int i;
  2029. int ret = 0;
  2030. unsigned long flags;
  2031. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  2032. struct task_struct *g, *t;
  2033. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  2034. ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
  2035. * sizeof(struct ftrace_ret_stack),
  2036. GFP_KERNEL);
  2037. if (!ret_stack_list[i]) {
  2038. start = 0;
  2039. end = i;
  2040. ret = -ENOMEM;
  2041. goto free;
  2042. }
  2043. }
  2044. read_lock_irqsave(&tasklist_lock, flags);
  2045. do_each_thread(g, t) {
  2046. if (start == end) {
  2047. ret = -EAGAIN;
  2048. goto unlock;
  2049. }
  2050. if (t->ret_stack == NULL) {
  2051. t->curr_ret_stack = -1;
  2052. /* Make sure IRQs see the -1 first: */
  2053. barrier();
  2054. t->ret_stack = ret_stack_list[start++];
  2055. atomic_set(&t->tracing_graph_pause, 0);
  2056. atomic_set(&t->trace_overrun, 0);
  2057. }
  2058. } while_each_thread(g, t);
  2059. unlock:
  2060. read_unlock_irqrestore(&tasklist_lock, flags);
  2061. free:
  2062. for (i = start; i < end; i++)
  2063. kfree(ret_stack_list[i]);
  2064. return ret;
  2065. }
  2066. /* Allocate a return stack for each task */
  2067. static int start_graph_tracing(void)
  2068. {
  2069. struct ftrace_ret_stack **ret_stack_list;
  2070. int ret, cpu;
  2071. ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
  2072. sizeof(struct ftrace_ret_stack *),
  2073. GFP_KERNEL);
  2074. if (!ret_stack_list)
  2075. return -ENOMEM;
  2076. /* The cpu_boot init_task->ret_stack will never be freed */
  2077. for_each_online_cpu(cpu)
  2078. ftrace_graph_init_task(idle_task(cpu));
  2079. do {
  2080. ret = alloc_retstack_tasklist(ret_stack_list);
  2081. } while (ret == -EAGAIN);
  2082. kfree(ret_stack_list);
  2083. return ret;
  2084. }
  2085. /*
  2086. * Hibernation protection.
  2087. * The state of the current task is too much unstable during
  2088. * suspend/restore to disk. We want to protect against that.
  2089. */
  2090. static int
  2091. ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
  2092. void *unused)
  2093. {
  2094. switch (state) {
  2095. case PM_HIBERNATION_PREPARE:
  2096. pause_graph_tracing();
  2097. break;
  2098. case PM_POST_HIBERNATION:
  2099. unpause_graph_tracing();
  2100. break;
  2101. }
  2102. return NOTIFY_DONE;
  2103. }
  2104. int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  2105. trace_func_graph_ent_t entryfunc)
  2106. {
  2107. int ret = 0;
  2108. mutex_lock(&ftrace_lock);
  2109. ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
  2110. register_pm_notifier(&ftrace_suspend_notifier);
  2111. atomic_inc(&ftrace_graph_active);
  2112. ret = start_graph_tracing();
  2113. if (ret) {
  2114. atomic_dec(&ftrace_graph_active);
  2115. goto out;
  2116. }
  2117. ftrace_graph_return = retfunc;
  2118. ftrace_graph_entry = entryfunc;
  2119. ftrace_startup(FTRACE_START_FUNC_RET);
  2120. out:
  2121. mutex_unlock(&ftrace_lock);
  2122. return ret;
  2123. }
  2124. void unregister_ftrace_graph(void)
  2125. {
  2126. mutex_lock(&ftrace_lock);
  2127. atomic_dec(&ftrace_graph_active);
  2128. ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
  2129. ftrace_graph_entry = ftrace_graph_entry_stub;
  2130. ftrace_shutdown(FTRACE_STOP_FUNC_RET);
  2131. unregister_pm_notifier(&ftrace_suspend_notifier);
  2132. mutex_unlock(&ftrace_lock);
  2133. }
  2134. /* Allocate a return stack for newly created task */
  2135. void ftrace_graph_init_task(struct task_struct *t)
  2136. {
  2137. if (atomic_read(&ftrace_graph_active)) {
  2138. t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  2139. * sizeof(struct ftrace_ret_stack),
  2140. GFP_KERNEL);
  2141. if (!t->ret_stack)
  2142. return;
  2143. t->curr_ret_stack = -1;
  2144. atomic_set(&t->tracing_graph_pause, 0);
  2145. atomic_set(&t->trace_overrun, 0);
  2146. } else
  2147. t->ret_stack = NULL;
  2148. }
  2149. void ftrace_graph_exit_task(struct task_struct *t)
  2150. {
  2151. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  2152. t->ret_stack = NULL;
  2153. /* NULL must become visible to IRQs before we free it: */
  2154. barrier();
  2155. kfree(ret_stack);
  2156. }
  2157. void ftrace_graph_stop(void)
  2158. {
  2159. ftrace_stop();
  2160. }
  2161. #endif