ftrace.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886
  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/debugfs.h>
  20. #include <linux/hardirq.h>
  21. #include <linux/kthread.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/kprobes.h>
  24. #include <linux/ftrace.h>
  25. #include <linux/sysctl.h>
  26. #include <linux/ctype.h>
  27. #include <linux/hash.h>
  28. #include <linux/list.h>
  29. #include <asm/ftrace.h>
  30. #include "trace.h"
  31. /* ftrace_enabled is a method to turn ftrace on or off */
  32. int ftrace_enabled __read_mostly;
  33. static int last_ftrace_enabled;
  34. /*
  35. * ftrace_disabled is set when an anomaly is discovered.
  36. * ftrace_disabled is much stronger than ftrace_enabled.
  37. */
  38. static int ftrace_disabled __read_mostly;
  39. static DEFINE_SPINLOCK(ftrace_lock);
  40. static DEFINE_MUTEX(ftrace_sysctl_lock);
  41. static struct ftrace_ops ftrace_list_end __read_mostly =
  42. {
  43. .func = ftrace_stub,
  44. };
  45. static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
  46. ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
  47. static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
  48. {
  49. struct ftrace_ops *op = ftrace_list;
  50. /* in case someone actually ports this to alpha! */
  51. read_barrier_depends();
  52. while (op != &ftrace_list_end) {
  53. /* silly alpha */
  54. read_barrier_depends();
  55. op->func(ip, parent_ip);
  56. op = op->next;
  57. };
  58. }
  59. /**
  60. * clear_ftrace_function - reset the ftrace function
  61. *
  62. * This NULLs the ftrace function and in essence stops
  63. * tracing. There may be lag
  64. */
  65. void clear_ftrace_function(void)
  66. {
  67. ftrace_trace_function = ftrace_stub;
  68. }
  69. static int __register_ftrace_function(struct ftrace_ops *ops)
  70. {
  71. /* should not be called from interrupt context */
  72. spin_lock(&ftrace_lock);
  73. ops->next = ftrace_list;
  74. /*
  75. * We are entering ops into the ftrace_list but another
  76. * CPU might be walking that list. We need to make sure
  77. * the ops->next pointer is valid before another CPU sees
  78. * the ops pointer included into the ftrace_list.
  79. */
  80. smp_wmb();
  81. ftrace_list = ops;
  82. if (ftrace_enabled) {
  83. /*
  84. * For one func, simply call it directly.
  85. * For more than one func, call the chain.
  86. */
  87. if (ops->next == &ftrace_list_end)
  88. ftrace_trace_function = ops->func;
  89. else
  90. ftrace_trace_function = ftrace_list_func;
  91. }
  92. spin_unlock(&ftrace_lock);
  93. return 0;
  94. }
  95. static int __unregister_ftrace_function(struct ftrace_ops *ops)
  96. {
  97. struct ftrace_ops **p;
  98. int ret = 0;
  99. /* should not be called from interrupt context */
  100. spin_lock(&ftrace_lock);
  101. /*
  102. * If we are removing the last function, then simply point
  103. * to the ftrace_stub.
  104. */
  105. if (ftrace_list == ops && ops->next == &ftrace_list_end) {
  106. ftrace_trace_function = ftrace_stub;
  107. ftrace_list = &ftrace_list_end;
  108. goto out;
  109. }
  110. for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
  111. if (*p == ops)
  112. break;
  113. if (*p != ops) {
  114. ret = -1;
  115. goto out;
  116. }
  117. *p = (*p)->next;
  118. if (ftrace_enabled) {
  119. /* If we only have one func left, then call that directly */
  120. if (ftrace_list == &ftrace_list_end ||
  121. ftrace_list->next == &ftrace_list_end)
  122. ftrace_trace_function = ftrace_list->func;
  123. }
  124. out:
  125. spin_unlock(&ftrace_lock);
  126. return ret;
  127. }
  128. #ifdef CONFIG_DYNAMIC_FTRACE
  129. #ifndef CONFIG_FTRACE_MCOUNT_RECORD
  130. /*
  131. * The hash lock is only needed when the recording of the mcount
  132. * callers are dynamic. That is, by the caller themselves and
  133. * not recorded via the compilation.
  134. */
  135. static DEFINE_SPINLOCK(ftrace_hash_lock);
  136. #define ftrace_hash_lock(flags) spin_lock_irqsave(&ftrace_hash_lock, flags)
  137. #define ftrace_hash_unlock(flags) \
  138. spin_unlock_irqrestore(&ftrace_hash_lock, flags)
  139. static void ftrace_release_hash(unsigned long start, unsigned long end);
  140. #else
  141. /* This is protected via the ftrace_lock with MCOUNT_RECORD. */
  142. #define ftrace_hash_lock(flags) do { (void)(flags); } while (0)
  143. #define ftrace_hash_unlock(flags) do { } while(0)
  144. static inline void ftrace_release_hash(unsigned long start, unsigned long end)
  145. {
  146. }
  147. #endif
  148. /*
  149. * Since MCOUNT_ADDR may point to mcount itself, we do not want
  150. * to get it confused by reading a reference in the code as we
  151. * are parsing on objcopy output of text. Use a variable for
  152. * it instead.
  153. */
  154. static unsigned long mcount_addr = MCOUNT_ADDR;
  155. static struct task_struct *ftraced_task;
  156. enum {
  157. FTRACE_ENABLE_CALLS = (1 << 0),
  158. FTRACE_DISABLE_CALLS = (1 << 1),
  159. FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
  160. FTRACE_ENABLE_MCOUNT = (1 << 3),
  161. FTRACE_DISABLE_MCOUNT = (1 << 4),
  162. };
  163. static int ftrace_filtered;
  164. static int tracing_on;
  165. static int frozen_record_count;
  166. static struct hlist_head ftrace_hash[FTRACE_HASHSIZE];
  167. static DEFINE_PER_CPU(int, ftrace_shutdown_disable_cpu);
  168. static DEFINE_MUTEX(ftraced_lock);
  169. static DEFINE_MUTEX(ftrace_regex_lock);
  170. struct ftrace_page {
  171. struct ftrace_page *next;
  172. unsigned long index;
  173. struct dyn_ftrace records[];
  174. };
  175. #define ENTRIES_PER_PAGE \
  176. ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
  177. /* estimate from running different kernels */
  178. #define NR_TO_INIT 10000
  179. static struct ftrace_page *ftrace_pages_start;
  180. static struct ftrace_page *ftrace_pages;
  181. static int ftraced_trigger;
  182. static int ftraced_suspend;
  183. static int ftraced_stop;
  184. static int ftrace_record_suspend;
  185. static struct dyn_ftrace *ftrace_free_records;
  186. #ifdef CONFIG_KPROBES
  187. static inline void freeze_record(struct dyn_ftrace *rec)
  188. {
  189. if (!(rec->flags & FTRACE_FL_FROZEN)) {
  190. rec->flags |= FTRACE_FL_FROZEN;
  191. frozen_record_count++;
  192. }
  193. }
  194. static inline void unfreeze_record(struct dyn_ftrace *rec)
  195. {
  196. if (rec->flags & FTRACE_FL_FROZEN) {
  197. rec->flags &= ~FTRACE_FL_FROZEN;
  198. frozen_record_count--;
  199. }
  200. }
  201. static inline int record_frozen(struct dyn_ftrace *rec)
  202. {
  203. return rec->flags & FTRACE_FL_FROZEN;
  204. }
  205. #else
  206. # define freeze_record(rec) ({ 0; })
  207. # define unfreeze_record(rec) ({ 0; })
  208. # define record_frozen(rec) ({ 0; })
  209. #endif /* CONFIG_KPROBES */
  210. int skip_trace(unsigned long ip)
  211. {
  212. unsigned long fl;
  213. struct dyn_ftrace *rec;
  214. struct hlist_node *t;
  215. struct hlist_head *head;
  216. if (frozen_record_count == 0)
  217. return 0;
  218. head = &ftrace_hash[hash_long(ip, FTRACE_HASHBITS)];
  219. hlist_for_each_entry_rcu(rec, t, head, node) {
  220. if (rec->ip == ip) {
  221. if (record_frozen(rec)) {
  222. if (rec->flags & FTRACE_FL_FAILED)
  223. return 1;
  224. if (!(rec->flags & FTRACE_FL_CONVERTED))
  225. return 1;
  226. if (!tracing_on || !ftrace_enabled)
  227. return 1;
  228. if (ftrace_filtered) {
  229. fl = rec->flags & (FTRACE_FL_FILTER |
  230. FTRACE_FL_NOTRACE);
  231. if (!fl || (fl & FTRACE_FL_NOTRACE))
  232. return 1;
  233. }
  234. }
  235. break;
  236. }
  237. }
  238. return 0;
  239. }
  240. static inline int
  241. ftrace_ip_in_hash(unsigned long ip, unsigned long key)
  242. {
  243. struct dyn_ftrace *p;
  244. struct hlist_node *t;
  245. int found = 0;
  246. hlist_for_each_entry_rcu(p, t, &ftrace_hash[key], node) {
  247. if (p->ip == ip) {
  248. found = 1;
  249. break;
  250. }
  251. }
  252. return found;
  253. }
  254. static inline void
  255. ftrace_add_hash(struct dyn_ftrace *node, unsigned long key)
  256. {
  257. hlist_add_head_rcu(&node->node, &ftrace_hash[key]);
  258. }
  259. /* called from kstop_machine */
  260. static inline void ftrace_del_hash(struct dyn_ftrace *node)
  261. {
  262. hlist_del(&node->node);
  263. }
  264. static void ftrace_free_rec(struct dyn_ftrace *rec)
  265. {
  266. rec->ip = (unsigned long)ftrace_free_records;
  267. ftrace_free_records = rec;
  268. rec->flags |= FTRACE_FL_FREE;
  269. }
  270. void ftrace_release(void *start, unsigned long size)
  271. {
  272. struct dyn_ftrace *rec;
  273. struct ftrace_page *pg;
  274. unsigned long s = (unsigned long)start;
  275. unsigned long e = s + size;
  276. int i;
  277. if (ftrace_disabled || !start)
  278. return;
  279. /* should not be called from interrupt context */
  280. spin_lock(&ftrace_lock);
  281. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  282. for (i = 0; i < pg->index; i++) {
  283. rec = &pg->records[i];
  284. if ((rec->ip >= s) && (rec->ip < e))
  285. ftrace_free_rec(rec);
  286. }
  287. }
  288. spin_unlock(&ftrace_lock);
  289. ftrace_release_hash(s, e);
  290. }
  291. static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
  292. {
  293. struct dyn_ftrace *rec;
  294. /* First check for freed records */
  295. if (ftrace_free_records) {
  296. rec = ftrace_free_records;
  297. if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
  298. WARN_ON_ONCE(1);
  299. ftrace_free_records = NULL;
  300. ftrace_disabled = 1;
  301. ftrace_enabled = 0;
  302. return NULL;
  303. }
  304. ftrace_free_records = (void *)rec->ip;
  305. memset(rec, 0, sizeof(*rec));
  306. return rec;
  307. }
  308. if (ftrace_pages->index == ENTRIES_PER_PAGE) {
  309. if (!ftrace_pages->next)
  310. return NULL;
  311. ftrace_pages = ftrace_pages->next;
  312. }
  313. return &ftrace_pages->records[ftrace_pages->index++];
  314. }
  315. static void
  316. ftrace_record_ip(unsigned long ip)
  317. {
  318. struct dyn_ftrace *node;
  319. unsigned long flags;
  320. unsigned long key;
  321. int resched;
  322. int cpu;
  323. if (!ftrace_enabled || ftrace_disabled)
  324. return;
  325. resched = need_resched();
  326. preempt_disable_notrace();
  327. /*
  328. * We simply need to protect against recursion.
  329. * Use the the raw version of smp_processor_id and not
  330. * __get_cpu_var which can call debug hooks that can
  331. * cause a recursive crash here.
  332. */
  333. cpu = raw_smp_processor_id();
  334. per_cpu(ftrace_shutdown_disable_cpu, cpu)++;
  335. if (per_cpu(ftrace_shutdown_disable_cpu, cpu) != 1)
  336. goto out;
  337. if (unlikely(ftrace_record_suspend))
  338. goto out;
  339. key = hash_long(ip, FTRACE_HASHBITS);
  340. WARN_ON_ONCE(key >= FTRACE_HASHSIZE);
  341. if (ftrace_ip_in_hash(ip, key))
  342. goto out;
  343. ftrace_hash_lock(flags);
  344. /* This ip may have hit the hash before the lock */
  345. if (ftrace_ip_in_hash(ip, key))
  346. goto out_unlock;
  347. node = ftrace_alloc_dyn_node(ip);
  348. if (!node)
  349. goto out_unlock;
  350. node->ip = ip;
  351. ftrace_add_hash(node, key);
  352. ftraced_trigger = 1;
  353. out_unlock:
  354. ftrace_hash_unlock(flags);
  355. out:
  356. per_cpu(ftrace_shutdown_disable_cpu, cpu)--;
  357. /* prevent recursion with scheduler */
  358. if (resched)
  359. preempt_enable_no_resched_notrace();
  360. else
  361. preempt_enable_notrace();
  362. }
  363. #define FTRACE_ADDR ((long)(ftrace_caller))
  364. static int
  365. __ftrace_replace_code(struct dyn_ftrace *rec,
  366. unsigned char *old, unsigned char *new, int enable)
  367. {
  368. unsigned long ip, fl;
  369. ip = rec->ip;
  370. if (ftrace_filtered && enable) {
  371. /*
  372. * If filtering is on:
  373. *
  374. * If this record is set to be filtered and
  375. * is enabled then do nothing.
  376. *
  377. * If this record is set to be filtered and
  378. * it is not enabled, enable it.
  379. *
  380. * If this record is not set to be filtered
  381. * and it is not enabled do nothing.
  382. *
  383. * If this record is set not to trace then
  384. * do nothing.
  385. *
  386. * If this record is set not to trace and
  387. * it is enabled then disable it.
  388. *
  389. * If this record is not set to be filtered and
  390. * it is enabled, disable it.
  391. */
  392. fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE |
  393. FTRACE_FL_ENABLED);
  394. if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
  395. (fl == (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE)) ||
  396. !fl || (fl == FTRACE_FL_NOTRACE))
  397. return 0;
  398. /*
  399. * If it is enabled disable it,
  400. * otherwise enable it!
  401. */
  402. if (fl & FTRACE_FL_ENABLED) {
  403. /* swap new and old */
  404. new = old;
  405. old = ftrace_call_replace(ip, FTRACE_ADDR);
  406. rec->flags &= ~FTRACE_FL_ENABLED;
  407. } else {
  408. new = ftrace_call_replace(ip, FTRACE_ADDR);
  409. rec->flags |= FTRACE_FL_ENABLED;
  410. }
  411. } else {
  412. if (enable) {
  413. /*
  414. * If this record is set not to trace and is
  415. * not enabled, do nothing.
  416. */
  417. fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
  418. if (fl == FTRACE_FL_NOTRACE)
  419. return 0;
  420. new = ftrace_call_replace(ip, FTRACE_ADDR);
  421. } else
  422. old = ftrace_call_replace(ip, FTRACE_ADDR);
  423. if (enable) {
  424. if (rec->flags & FTRACE_FL_ENABLED)
  425. return 0;
  426. rec->flags |= FTRACE_FL_ENABLED;
  427. } else {
  428. if (!(rec->flags & FTRACE_FL_ENABLED))
  429. return 0;
  430. rec->flags &= ~FTRACE_FL_ENABLED;
  431. }
  432. }
  433. return ftrace_modify_code(ip, old, new);
  434. }
  435. static void ftrace_replace_code(int enable)
  436. {
  437. int i, failed;
  438. unsigned char *new = NULL, *old = NULL;
  439. struct dyn_ftrace *rec;
  440. struct ftrace_page *pg;
  441. if (enable)
  442. old = ftrace_nop_replace();
  443. else
  444. new = ftrace_nop_replace();
  445. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  446. for (i = 0; i < pg->index; i++) {
  447. rec = &pg->records[i];
  448. /* don't modify code that has already faulted */
  449. if (rec->flags & FTRACE_FL_FAILED)
  450. continue;
  451. /* ignore updates to this record's mcount site */
  452. if (get_kprobe((void *)rec->ip)) {
  453. freeze_record(rec);
  454. continue;
  455. } else {
  456. unfreeze_record(rec);
  457. }
  458. failed = __ftrace_replace_code(rec, old, new, enable);
  459. if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
  460. rec->flags |= FTRACE_FL_FAILED;
  461. if ((system_state == SYSTEM_BOOTING) ||
  462. !core_kernel_text(rec->ip)) {
  463. ftrace_del_hash(rec);
  464. ftrace_free_rec(rec);
  465. }
  466. }
  467. }
  468. }
  469. }
  470. static void ftrace_shutdown_replenish(void)
  471. {
  472. if (ftrace_pages->next)
  473. return;
  474. /* allocate another page */
  475. ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL);
  476. }
  477. static void print_ip_ins(const char *fmt, unsigned char *p)
  478. {
  479. int i;
  480. printk(KERN_CONT "%s", fmt);
  481. for (i = 0; i < MCOUNT_INSN_SIZE; i++)
  482. printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
  483. }
  484. static int
  485. ftrace_code_disable(struct dyn_ftrace *rec)
  486. {
  487. unsigned long ip;
  488. unsigned char *nop, *call;
  489. int ret;
  490. ip = rec->ip;
  491. nop = ftrace_nop_replace();
  492. call = ftrace_call_replace(ip, mcount_addr);
  493. ret = ftrace_modify_code(ip, call, nop);
  494. if (ret) {
  495. switch (ret) {
  496. case -EFAULT:
  497. WARN_ON_ONCE(1);
  498. pr_info("ftrace faulted on modifying ");
  499. print_ip_sym(ip);
  500. break;
  501. case -EINVAL:
  502. WARN_ON_ONCE(1);
  503. pr_info("ftrace failed to modify ");
  504. print_ip_sym(ip);
  505. print_ip_ins(" expected: ", call);
  506. print_ip_ins(" actual: ", (unsigned char *)ip);
  507. print_ip_ins(" replace: ", nop);
  508. printk(KERN_CONT "\n");
  509. break;
  510. case -EPERM:
  511. WARN_ON_ONCE(1);
  512. pr_info("ftrace faulted on writing ");
  513. print_ip_sym(ip);
  514. break;
  515. default:
  516. WARN_ON_ONCE(1);
  517. pr_info("ftrace faulted on unknown error ");
  518. print_ip_sym(ip);
  519. }
  520. rec->flags |= FTRACE_FL_FAILED;
  521. return 0;
  522. }
  523. return 1;
  524. }
  525. static int __ftrace_update_code(void *ignore);
  526. static int __ftrace_modify_code(void *data)
  527. {
  528. unsigned long addr;
  529. int *command = data;
  530. if (*command & FTRACE_ENABLE_CALLS) {
  531. /*
  532. * Update any recorded ips now that we have the
  533. * machine stopped
  534. */
  535. __ftrace_update_code(NULL);
  536. ftrace_replace_code(1);
  537. tracing_on = 1;
  538. } else if (*command & FTRACE_DISABLE_CALLS) {
  539. ftrace_replace_code(0);
  540. tracing_on = 0;
  541. }
  542. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  543. ftrace_update_ftrace_func(ftrace_trace_function);
  544. if (*command & FTRACE_ENABLE_MCOUNT) {
  545. addr = (unsigned long)ftrace_record_ip;
  546. ftrace_mcount_set(&addr);
  547. } else if (*command & FTRACE_DISABLE_MCOUNT) {
  548. addr = (unsigned long)ftrace_stub;
  549. ftrace_mcount_set(&addr);
  550. }
  551. return 0;
  552. }
  553. static void ftrace_run_update_code(int command)
  554. {
  555. stop_machine(__ftrace_modify_code, &command, NULL);
  556. }
  557. void ftrace_disable_daemon(void)
  558. {
  559. /* Stop the daemon from calling kstop_machine */
  560. mutex_lock(&ftraced_lock);
  561. ftraced_stop = 1;
  562. mutex_unlock(&ftraced_lock);
  563. ftrace_force_update();
  564. }
  565. void ftrace_enable_daemon(void)
  566. {
  567. mutex_lock(&ftraced_lock);
  568. ftraced_stop = 0;
  569. mutex_unlock(&ftraced_lock);
  570. ftrace_force_update();
  571. }
  572. static ftrace_func_t saved_ftrace_func;
  573. static void ftrace_startup(void)
  574. {
  575. int command = 0;
  576. if (unlikely(ftrace_disabled))
  577. return;
  578. mutex_lock(&ftraced_lock);
  579. ftraced_suspend++;
  580. if (ftraced_suspend == 1)
  581. command |= FTRACE_ENABLE_CALLS;
  582. if (saved_ftrace_func != ftrace_trace_function) {
  583. saved_ftrace_func = ftrace_trace_function;
  584. command |= FTRACE_UPDATE_TRACE_FUNC;
  585. }
  586. if (!command || !ftrace_enabled)
  587. goto out;
  588. ftrace_run_update_code(command);
  589. out:
  590. mutex_unlock(&ftraced_lock);
  591. }
  592. static void ftrace_shutdown(void)
  593. {
  594. int command = 0;
  595. if (unlikely(ftrace_disabled))
  596. return;
  597. mutex_lock(&ftraced_lock);
  598. ftraced_suspend--;
  599. if (!ftraced_suspend)
  600. command |= FTRACE_DISABLE_CALLS;
  601. if (saved_ftrace_func != ftrace_trace_function) {
  602. saved_ftrace_func = ftrace_trace_function;
  603. command |= FTRACE_UPDATE_TRACE_FUNC;
  604. }
  605. if (!command || !ftrace_enabled)
  606. goto out;
  607. ftrace_run_update_code(command);
  608. out:
  609. mutex_unlock(&ftraced_lock);
  610. }
  611. static void ftrace_startup_sysctl(void)
  612. {
  613. int command = FTRACE_ENABLE_MCOUNT;
  614. if (unlikely(ftrace_disabled))
  615. return;
  616. mutex_lock(&ftraced_lock);
  617. /* Force update next time */
  618. saved_ftrace_func = NULL;
  619. /* ftraced_suspend is true if we want ftrace running */
  620. if (ftraced_suspend)
  621. command |= FTRACE_ENABLE_CALLS;
  622. ftrace_run_update_code(command);
  623. mutex_unlock(&ftraced_lock);
  624. }
  625. static void ftrace_shutdown_sysctl(void)
  626. {
  627. int command = FTRACE_DISABLE_MCOUNT;
  628. if (unlikely(ftrace_disabled))
  629. return;
  630. mutex_lock(&ftraced_lock);
  631. /* ftraced_suspend is true if ftrace is running */
  632. if (ftraced_suspend)
  633. command |= FTRACE_DISABLE_CALLS;
  634. ftrace_run_update_code(command);
  635. mutex_unlock(&ftraced_lock);
  636. }
  637. static cycle_t ftrace_update_time;
  638. static unsigned long ftrace_update_cnt;
  639. unsigned long ftrace_update_tot_cnt;
  640. static int __ftrace_update_code(void *ignore)
  641. {
  642. int i, save_ftrace_enabled;
  643. cycle_t start, stop;
  644. struct dyn_ftrace *p;
  645. struct hlist_node *t, *n;
  646. struct hlist_head *head, temp_list;
  647. /* Don't be recording funcs now */
  648. ftrace_record_suspend++;
  649. save_ftrace_enabled = ftrace_enabled;
  650. ftrace_enabled = 0;
  651. start = ftrace_now(raw_smp_processor_id());
  652. ftrace_update_cnt = 0;
  653. /* No locks needed, the machine is stopped! */
  654. for (i = 0; i < FTRACE_HASHSIZE; i++) {
  655. INIT_HLIST_HEAD(&temp_list);
  656. head = &ftrace_hash[i];
  657. /* all CPUS are stopped, we are safe to modify code */
  658. hlist_for_each_entry_safe(p, t, n, head, node) {
  659. /* Skip over failed records which have not been
  660. * freed. */
  661. if (p->flags & FTRACE_FL_FAILED)
  662. continue;
  663. /* Unconverted records are always at the head of the
  664. * hash bucket. Once we encounter a converted record,
  665. * simply skip over to the next bucket. Saves ftraced
  666. * some processor cycles (ftrace does its bid for
  667. * global warming :-p ). */
  668. if (p->flags & (FTRACE_FL_CONVERTED))
  669. break;
  670. /* Ignore updates to this record's mcount site.
  671. * Reintroduce this record at the head of this
  672. * bucket to attempt to "convert" it again if
  673. * the kprobe on it is unregistered before the
  674. * next run. */
  675. if (get_kprobe((void *)p->ip)) {
  676. ftrace_del_hash(p);
  677. INIT_HLIST_NODE(&p->node);
  678. hlist_add_head(&p->node, &temp_list);
  679. freeze_record(p);
  680. continue;
  681. } else {
  682. unfreeze_record(p);
  683. }
  684. /* convert record (i.e, patch mcount-call with NOP) */
  685. if (ftrace_code_disable(p)) {
  686. p->flags |= FTRACE_FL_CONVERTED;
  687. ftrace_update_cnt++;
  688. } else {
  689. if ((system_state == SYSTEM_BOOTING) ||
  690. !core_kernel_text(p->ip)) {
  691. ftrace_del_hash(p);
  692. ftrace_free_rec(p);
  693. }
  694. }
  695. }
  696. hlist_for_each_entry_safe(p, t, n, &temp_list, node) {
  697. hlist_del(&p->node);
  698. INIT_HLIST_NODE(&p->node);
  699. hlist_add_head(&p->node, head);
  700. }
  701. }
  702. stop = ftrace_now(raw_smp_processor_id());
  703. ftrace_update_time = stop - start;
  704. ftrace_update_tot_cnt += ftrace_update_cnt;
  705. ftraced_trigger = 0;
  706. ftrace_enabled = save_ftrace_enabled;
  707. ftrace_record_suspend--;
  708. return 0;
  709. }
  710. static int ftrace_update_code(void)
  711. {
  712. if (unlikely(ftrace_disabled) ||
  713. !ftrace_enabled || !ftraced_trigger)
  714. return 0;
  715. stop_machine(__ftrace_update_code, NULL, NULL);
  716. return 1;
  717. }
  718. static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
  719. {
  720. struct ftrace_page *pg;
  721. int cnt;
  722. int i;
  723. /* allocate a few pages */
  724. ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
  725. if (!ftrace_pages_start)
  726. return -1;
  727. /*
  728. * Allocate a few more pages.
  729. *
  730. * TODO: have some parser search vmlinux before
  731. * final linking to find all calls to ftrace.
  732. * Then we can:
  733. * a) know how many pages to allocate.
  734. * and/or
  735. * b) set up the table then.
  736. *
  737. * The dynamic code is still necessary for
  738. * modules.
  739. */
  740. pg = ftrace_pages = ftrace_pages_start;
  741. cnt = num_to_init / ENTRIES_PER_PAGE;
  742. pr_info("ftrace: allocating %ld hash entries in %d pages\n",
  743. num_to_init, cnt);
  744. for (i = 0; i < cnt; i++) {
  745. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  746. /* If we fail, we'll try later anyway */
  747. if (!pg->next)
  748. break;
  749. pg = pg->next;
  750. }
  751. return 0;
  752. }
  753. enum {
  754. FTRACE_ITER_FILTER = (1 << 0),
  755. FTRACE_ITER_CONT = (1 << 1),
  756. FTRACE_ITER_NOTRACE = (1 << 2),
  757. FTRACE_ITER_FAILURES = (1 << 3),
  758. };
  759. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  760. struct ftrace_iterator {
  761. loff_t pos;
  762. struct ftrace_page *pg;
  763. unsigned idx;
  764. unsigned flags;
  765. unsigned char buffer[FTRACE_BUFF_MAX+1];
  766. unsigned buffer_idx;
  767. unsigned filtered;
  768. };
  769. static void *
  770. t_next(struct seq_file *m, void *v, loff_t *pos)
  771. {
  772. struct ftrace_iterator *iter = m->private;
  773. struct dyn_ftrace *rec = NULL;
  774. (*pos)++;
  775. /* should not be called from interrupt context */
  776. spin_lock(&ftrace_lock);
  777. retry:
  778. if (iter->idx >= iter->pg->index) {
  779. if (iter->pg->next) {
  780. iter->pg = iter->pg->next;
  781. iter->idx = 0;
  782. goto retry;
  783. }
  784. } else {
  785. rec = &iter->pg->records[iter->idx++];
  786. if ((rec->flags & FTRACE_FL_FREE) ||
  787. (!(iter->flags & FTRACE_ITER_FAILURES) &&
  788. (rec->flags & FTRACE_FL_FAILED)) ||
  789. ((iter->flags & FTRACE_ITER_FAILURES) &&
  790. !(rec->flags & FTRACE_FL_FAILED)) ||
  791. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  792. !(rec->flags & FTRACE_FL_NOTRACE))) {
  793. rec = NULL;
  794. goto retry;
  795. }
  796. }
  797. spin_unlock(&ftrace_lock);
  798. iter->pos = *pos;
  799. return rec;
  800. }
  801. static void *t_start(struct seq_file *m, loff_t *pos)
  802. {
  803. struct ftrace_iterator *iter = m->private;
  804. void *p = NULL;
  805. loff_t l = -1;
  806. if (*pos != iter->pos) {
  807. for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
  808. ;
  809. } else {
  810. l = *pos;
  811. p = t_next(m, p, &l);
  812. }
  813. return p;
  814. }
  815. static void t_stop(struct seq_file *m, void *p)
  816. {
  817. }
  818. static int t_show(struct seq_file *m, void *v)
  819. {
  820. struct dyn_ftrace *rec = v;
  821. char str[KSYM_SYMBOL_LEN];
  822. if (!rec)
  823. return 0;
  824. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  825. seq_printf(m, "%s\n", str);
  826. return 0;
  827. }
  828. static struct seq_operations show_ftrace_seq_ops = {
  829. .start = t_start,
  830. .next = t_next,
  831. .stop = t_stop,
  832. .show = t_show,
  833. };
  834. static int
  835. ftrace_avail_open(struct inode *inode, struct file *file)
  836. {
  837. struct ftrace_iterator *iter;
  838. int ret;
  839. if (unlikely(ftrace_disabled))
  840. return -ENODEV;
  841. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  842. if (!iter)
  843. return -ENOMEM;
  844. iter->pg = ftrace_pages_start;
  845. iter->pos = -1;
  846. ret = seq_open(file, &show_ftrace_seq_ops);
  847. if (!ret) {
  848. struct seq_file *m = file->private_data;
  849. m->private = iter;
  850. } else {
  851. kfree(iter);
  852. }
  853. return ret;
  854. }
  855. int ftrace_avail_release(struct inode *inode, struct file *file)
  856. {
  857. struct seq_file *m = (struct seq_file *)file->private_data;
  858. struct ftrace_iterator *iter = m->private;
  859. seq_release(inode, file);
  860. kfree(iter);
  861. return 0;
  862. }
  863. static int
  864. ftrace_failures_open(struct inode *inode, struct file *file)
  865. {
  866. int ret;
  867. struct seq_file *m;
  868. struct ftrace_iterator *iter;
  869. ret = ftrace_avail_open(inode, file);
  870. if (!ret) {
  871. m = (struct seq_file *)file->private_data;
  872. iter = (struct ftrace_iterator *)m->private;
  873. iter->flags = FTRACE_ITER_FAILURES;
  874. }
  875. return ret;
  876. }
  877. static void ftrace_filter_reset(int enable)
  878. {
  879. struct ftrace_page *pg;
  880. struct dyn_ftrace *rec;
  881. unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  882. unsigned i;
  883. /* should not be called from interrupt context */
  884. spin_lock(&ftrace_lock);
  885. if (enable)
  886. ftrace_filtered = 0;
  887. pg = ftrace_pages_start;
  888. while (pg) {
  889. for (i = 0; i < pg->index; i++) {
  890. rec = &pg->records[i];
  891. if (rec->flags & FTRACE_FL_FAILED)
  892. continue;
  893. rec->flags &= ~type;
  894. }
  895. pg = pg->next;
  896. }
  897. spin_unlock(&ftrace_lock);
  898. }
  899. static int
  900. ftrace_regex_open(struct inode *inode, struct file *file, int enable)
  901. {
  902. struct ftrace_iterator *iter;
  903. int ret = 0;
  904. if (unlikely(ftrace_disabled))
  905. return -ENODEV;
  906. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  907. if (!iter)
  908. return -ENOMEM;
  909. mutex_lock(&ftrace_regex_lock);
  910. if ((file->f_mode & FMODE_WRITE) &&
  911. !(file->f_flags & O_APPEND))
  912. ftrace_filter_reset(enable);
  913. if (file->f_mode & FMODE_READ) {
  914. iter->pg = ftrace_pages_start;
  915. iter->pos = -1;
  916. iter->flags = enable ? FTRACE_ITER_FILTER :
  917. FTRACE_ITER_NOTRACE;
  918. ret = seq_open(file, &show_ftrace_seq_ops);
  919. if (!ret) {
  920. struct seq_file *m = file->private_data;
  921. m->private = iter;
  922. } else
  923. kfree(iter);
  924. } else
  925. file->private_data = iter;
  926. mutex_unlock(&ftrace_regex_lock);
  927. return ret;
  928. }
  929. static int
  930. ftrace_filter_open(struct inode *inode, struct file *file)
  931. {
  932. return ftrace_regex_open(inode, file, 1);
  933. }
  934. static int
  935. ftrace_notrace_open(struct inode *inode, struct file *file)
  936. {
  937. return ftrace_regex_open(inode, file, 0);
  938. }
  939. static ssize_t
  940. ftrace_regex_read(struct file *file, char __user *ubuf,
  941. size_t cnt, loff_t *ppos)
  942. {
  943. if (file->f_mode & FMODE_READ)
  944. return seq_read(file, ubuf, cnt, ppos);
  945. else
  946. return -EPERM;
  947. }
  948. static loff_t
  949. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  950. {
  951. loff_t ret;
  952. if (file->f_mode & FMODE_READ)
  953. ret = seq_lseek(file, offset, origin);
  954. else
  955. file->f_pos = ret = 1;
  956. return ret;
  957. }
  958. enum {
  959. MATCH_FULL,
  960. MATCH_FRONT_ONLY,
  961. MATCH_MIDDLE_ONLY,
  962. MATCH_END_ONLY,
  963. };
  964. static void
  965. ftrace_match(unsigned char *buff, int len, int enable)
  966. {
  967. char str[KSYM_SYMBOL_LEN];
  968. char *search = NULL;
  969. struct ftrace_page *pg;
  970. struct dyn_ftrace *rec;
  971. int type = MATCH_FULL;
  972. unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  973. unsigned i, match = 0, search_len = 0;
  974. for (i = 0; i < len; i++) {
  975. if (buff[i] == '*') {
  976. if (!i) {
  977. search = buff + i + 1;
  978. type = MATCH_END_ONLY;
  979. search_len = len - (i + 1);
  980. } else {
  981. if (type == MATCH_END_ONLY) {
  982. type = MATCH_MIDDLE_ONLY;
  983. } else {
  984. match = i;
  985. type = MATCH_FRONT_ONLY;
  986. }
  987. buff[i] = 0;
  988. break;
  989. }
  990. }
  991. }
  992. /* should not be called from interrupt context */
  993. spin_lock(&ftrace_lock);
  994. if (enable)
  995. ftrace_filtered = 1;
  996. pg = ftrace_pages_start;
  997. while (pg) {
  998. for (i = 0; i < pg->index; i++) {
  999. int matched = 0;
  1000. char *ptr;
  1001. rec = &pg->records[i];
  1002. if (rec->flags & FTRACE_FL_FAILED)
  1003. continue;
  1004. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  1005. switch (type) {
  1006. case MATCH_FULL:
  1007. if (strcmp(str, buff) == 0)
  1008. matched = 1;
  1009. break;
  1010. case MATCH_FRONT_ONLY:
  1011. if (memcmp(str, buff, match) == 0)
  1012. matched = 1;
  1013. break;
  1014. case MATCH_MIDDLE_ONLY:
  1015. if (strstr(str, search))
  1016. matched = 1;
  1017. break;
  1018. case MATCH_END_ONLY:
  1019. ptr = strstr(str, search);
  1020. if (ptr && (ptr[search_len] == 0))
  1021. matched = 1;
  1022. break;
  1023. }
  1024. if (matched)
  1025. rec->flags |= flag;
  1026. }
  1027. pg = pg->next;
  1028. }
  1029. spin_unlock(&ftrace_lock);
  1030. }
  1031. static ssize_t
  1032. ftrace_regex_write(struct file *file, const char __user *ubuf,
  1033. size_t cnt, loff_t *ppos, int enable)
  1034. {
  1035. struct ftrace_iterator *iter;
  1036. char ch;
  1037. size_t read = 0;
  1038. ssize_t ret;
  1039. if (!cnt || cnt < 0)
  1040. return 0;
  1041. mutex_lock(&ftrace_regex_lock);
  1042. if (file->f_mode & FMODE_READ) {
  1043. struct seq_file *m = file->private_data;
  1044. iter = m->private;
  1045. } else
  1046. iter = file->private_data;
  1047. if (!*ppos) {
  1048. iter->flags &= ~FTRACE_ITER_CONT;
  1049. iter->buffer_idx = 0;
  1050. }
  1051. ret = get_user(ch, ubuf++);
  1052. if (ret)
  1053. goto out;
  1054. read++;
  1055. cnt--;
  1056. if (!(iter->flags & ~FTRACE_ITER_CONT)) {
  1057. /* skip white space */
  1058. while (cnt && isspace(ch)) {
  1059. ret = get_user(ch, ubuf++);
  1060. if (ret)
  1061. goto out;
  1062. read++;
  1063. cnt--;
  1064. }
  1065. if (isspace(ch)) {
  1066. file->f_pos += read;
  1067. ret = read;
  1068. goto out;
  1069. }
  1070. iter->buffer_idx = 0;
  1071. }
  1072. while (cnt && !isspace(ch)) {
  1073. if (iter->buffer_idx < FTRACE_BUFF_MAX)
  1074. iter->buffer[iter->buffer_idx++] = ch;
  1075. else {
  1076. ret = -EINVAL;
  1077. goto out;
  1078. }
  1079. ret = get_user(ch, ubuf++);
  1080. if (ret)
  1081. goto out;
  1082. read++;
  1083. cnt--;
  1084. }
  1085. if (isspace(ch)) {
  1086. iter->filtered++;
  1087. iter->buffer[iter->buffer_idx] = 0;
  1088. ftrace_match(iter->buffer, iter->buffer_idx, enable);
  1089. iter->buffer_idx = 0;
  1090. } else
  1091. iter->flags |= FTRACE_ITER_CONT;
  1092. file->f_pos += read;
  1093. ret = read;
  1094. out:
  1095. mutex_unlock(&ftrace_regex_lock);
  1096. return ret;
  1097. }
  1098. static ssize_t
  1099. ftrace_filter_write(struct file *file, const char __user *ubuf,
  1100. size_t cnt, loff_t *ppos)
  1101. {
  1102. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  1103. }
  1104. static ssize_t
  1105. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  1106. size_t cnt, loff_t *ppos)
  1107. {
  1108. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  1109. }
  1110. static void
  1111. ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
  1112. {
  1113. if (unlikely(ftrace_disabled))
  1114. return;
  1115. mutex_lock(&ftrace_regex_lock);
  1116. if (reset)
  1117. ftrace_filter_reset(enable);
  1118. if (buf)
  1119. ftrace_match(buf, len, enable);
  1120. mutex_unlock(&ftrace_regex_lock);
  1121. }
  1122. /**
  1123. * ftrace_set_filter - set a function to filter on in ftrace
  1124. * @buf - the string that holds the function filter text.
  1125. * @len - the length of the string.
  1126. * @reset - non zero to reset all filters before applying this filter.
  1127. *
  1128. * Filters denote which functions should be enabled when tracing is enabled.
  1129. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  1130. */
  1131. void ftrace_set_filter(unsigned char *buf, int len, int reset)
  1132. {
  1133. ftrace_set_regex(buf, len, reset, 1);
  1134. }
  1135. /**
  1136. * ftrace_set_notrace - set a function to not trace in ftrace
  1137. * @buf - the string that holds the function notrace text.
  1138. * @len - the length of the string.
  1139. * @reset - non zero to reset all filters before applying this filter.
  1140. *
  1141. * Notrace Filters denote which functions should not be enabled when tracing
  1142. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  1143. * for tracing.
  1144. */
  1145. void ftrace_set_notrace(unsigned char *buf, int len, int reset)
  1146. {
  1147. ftrace_set_regex(buf, len, reset, 0);
  1148. }
  1149. static int
  1150. ftrace_regex_release(struct inode *inode, struct file *file, int enable)
  1151. {
  1152. struct seq_file *m = (struct seq_file *)file->private_data;
  1153. struct ftrace_iterator *iter;
  1154. mutex_lock(&ftrace_regex_lock);
  1155. if (file->f_mode & FMODE_READ) {
  1156. iter = m->private;
  1157. seq_release(inode, file);
  1158. } else
  1159. iter = file->private_data;
  1160. if (iter->buffer_idx) {
  1161. iter->filtered++;
  1162. iter->buffer[iter->buffer_idx] = 0;
  1163. ftrace_match(iter->buffer, iter->buffer_idx, enable);
  1164. }
  1165. mutex_lock(&ftrace_sysctl_lock);
  1166. mutex_lock(&ftraced_lock);
  1167. if (iter->filtered && ftraced_suspend && ftrace_enabled)
  1168. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  1169. mutex_unlock(&ftraced_lock);
  1170. mutex_unlock(&ftrace_sysctl_lock);
  1171. kfree(iter);
  1172. mutex_unlock(&ftrace_regex_lock);
  1173. return 0;
  1174. }
  1175. static int
  1176. ftrace_filter_release(struct inode *inode, struct file *file)
  1177. {
  1178. return ftrace_regex_release(inode, file, 1);
  1179. }
  1180. static int
  1181. ftrace_notrace_release(struct inode *inode, struct file *file)
  1182. {
  1183. return ftrace_regex_release(inode, file, 0);
  1184. }
  1185. static ssize_t
  1186. ftraced_read(struct file *filp, char __user *ubuf,
  1187. size_t cnt, loff_t *ppos)
  1188. {
  1189. /* don't worry about races */
  1190. char *buf = ftraced_stop ? "disabled\n" : "enabled\n";
  1191. int r = strlen(buf);
  1192. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1193. }
  1194. static ssize_t
  1195. ftraced_write(struct file *filp, const char __user *ubuf,
  1196. size_t cnt, loff_t *ppos)
  1197. {
  1198. char buf[64];
  1199. long val;
  1200. int ret;
  1201. if (cnt >= sizeof(buf))
  1202. return -EINVAL;
  1203. if (copy_from_user(&buf, ubuf, cnt))
  1204. return -EFAULT;
  1205. if (strncmp(buf, "enable", 6) == 0)
  1206. val = 1;
  1207. else if (strncmp(buf, "disable", 7) == 0)
  1208. val = 0;
  1209. else {
  1210. buf[cnt] = 0;
  1211. ret = strict_strtoul(buf, 10, &val);
  1212. if (ret < 0)
  1213. return ret;
  1214. val = !!val;
  1215. }
  1216. if (val)
  1217. ftrace_enable_daemon();
  1218. else
  1219. ftrace_disable_daemon();
  1220. filp->f_pos += cnt;
  1221. return cnt;
  1222. }
  1223. static struct file_operations ftrace_avail_fops = {
  1224. .open = ftrace_avail_open,
  1225. .read = seq_read,
  1226. .llseek = seq_lseek,
  1227. .release = ftrace_avail_release,
  1228. };
  1229. static struct file_operations ftrace_failures_fops = {
  1230. .open = ftrace_failures_open,
  1231. .read = seq_read,
  1232. .llseek = seq_lseek,
  1233. .release = ftrace_avail_release,
  1234. };
  1235. static struct file_operations ftrace_filter_fops = {
  1236. .open = ftrace_filter_open,
  1237. .read = ftrace_regex_read,
  1238. .write = ftrace_filter_write,
  1239. .llseek = ftrace_regex_lseek,
  1240. .release = ftrace_filter_release,
  1241. };
  1242. static struct file_operations ftrace_notrace_fops = {
  1243. .open = ftrace_notrace_open,
  1244. .read = ftrace_regex_read,
  1245. .write = ftrace_notrace_write,
  1246. .llseek = ftrace_regex_lseek,
  1247. .release = ftrace_notrace_release,
  1248. };
  1249. static struct file_operations ftraced_fops = {
  1250. .open = tracing_open_generic,
  1251. .read = ftraced_read,
  1252. .write = ftraced_write,
  1253. };
  1254. /**
  1255. * ftrace_force_update - force an update to all recording ftrace functions
  1256. */
  1257. int ftrace_force_update(void)
  1258. {
  1259. int ret = 0;
  1260. if (unlikely(ftrace_disabled))
  1261. return -ENODEV;
  1262. mutex_lock(&ftrace_sysctl_lock);
  1263. mutex_lock(&ftraced_lock);
  1264. /*
  1265. * If ftraced_trigger is not set, then there is nothing
  1266. * to update.
  1267. */
  1268. if (ftraced_trigger && !ftrace_update_code())
  1269. ret = -EBUSY;
  1270. mutex_unlock(&ftraced_lock);
  1271. mutex_unlock(&ftrace_sysctl_lock);
  1272. return ret;
  1273. }
  1274. static __init int ftrace_init_debugfs(void)
  1275. {
  1276. struct dentry *d_tracer;
  1277. struct dentry *entry;
  1278. d_tracer = tracing_init_dentry();
  1279. entry = debugfs_create_file("available_filter_functions", 0444,
  1280. d_tracer, NULL, &ftrace_avail_fops);
  1281. if (!entry)
  1282. pr_warning("Could not create debugfs "
  1283. "'available_filter_functions' entry\n");
  1284. entry = debugfs_create_file("failures", 0444,
  1285. d_tracer, NULL, &ftrace_failures_fops);
  1286. if (!entry)
  1287. pr_warning("Could not create debugfs 'failures' entry\n");
  1288. entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
  1289. NULL, &ftrace_filter_fops);
  1290. if (!entry)
  1291. pr_warning("Could not create debugfs "
  1292. "'set_ftrace_filter' entry\n");
  1293. entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
  1294. NULL, &ftrace_notrace_fops);
  1295. if (!entry)
  1296. pr_warning("Could not create debugfs "
  1297. "'set_ftrace_notrace' entry\n");
  1298. entry = debugfs_create_file("ftraced_enabled", 0644, d_tracer,
  1299. NULL, &ftraced_fops);
  1300. if (!entry)
  1301. pr_warning("Could not create debugfs "
  1302. "'ftraced_enabled' entry\n");
  1303. return 0;
  1304. }
  1305. fs_initcall(ftrace_init_debugfs);
  1306. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  1307. static int ftrace_convert_nops(unsigned long *start,
  1308. unsigned long *end)
  1309. {
  1310. unsigned long *p;
  1311. unsigned long addr;
  1312. unsigned long flags;
  1313. p = start;
  1314. while (p < end) {
  1315. addr = ftrace_call_adjust(*p++);
  1316. /* should not be called from interrupt context */
  1317. spin_lock(&ftrace_lock);
  1318. ftrace_record_ip(addr);
  1319. spin_unlock(&ftrace_lock);
  1320. ftrace_shutdown_replenish();
  1321. }
  1322. /* p is ignored */
  1323. local_irq_save(flags);
  1324. __ftrace_update_code(p);
  1325. local_irq_restore(flags);
  1326. return 0;
  1327. }
  1328. void ftrace_init_module(unsigned long *start, unsigned long *end)
  1329. {
  1330. if (ftrace_disabled || start == end)
  1331. return;
  1332. ftrace_convert_nops(start, end);
  1333. }
  1334. extern unsigned long __start_mcount_loc[];
  1335. extern unsigned long __stop_mcount_loc[];
  1336. void __init ftrace_init(void)
  1337. {
  1338. unsigned long count, addr, flags;
  1339. int ret;
  1340. /* Keep the ftrace pointer to the stub */
  1341. addr = (unsigned long)ftrace_stub;
  1342. local_irq_save(flags);
  1343. ftrace_dyn_arch_init(&addr);
  1344. local_irq_restore(flags);
  1345. /* ftrace_dyn_arch_init places the return code in addr */
  1346. if (addr)
  1347. goto failed;
  1348. count = __stop_mcount_loc - __start_mcount_loc;
  1349. ret = ftrace_dyn_table_alloc(count);
  1350. if (ret)
  1351. goto failed;
  1352. last_ftrace_enabled = ftrace_enabled = 1;
  1353. ret = ftrace_convert_nops(__start_mcount_loc,
  1354. __stop_mcount_loc);
  1355. return;
  1356. failed:
  1357. ftrace_disabled = 1;
  1358. }
  1359. #else /* CONFIG_FTRACE_MCOUNT_RECORD */
  1360. static void ftrace_release_hash(unsigned long start, unsigned long end)
  1361. {
  1362. struct dyn_ftrace *rec;
  1363. struct hlist_node *t, *n;
  1364. struct hlist_head *head, temp_list;
  1365. unsigned long flags;
  1366. int i, cpu;
  1367. preempt_disable_notrace();
  1368. /* disable incase we call something that calls mcount */
  1369. cpu = raw_smp_processor_id();
  1370. per_cpu(ftrace_shutdown_disable_cpu, cpu)++;
  1371. ftrace_hash_lock(flags);
  1372. for (i = 0; i < FTRACE_HASHSIZE; i++) {
  1373. INIT_HLIST_HEAD(&temp_list);
  1374. head = &ftrace_hash[i];
  1375. /* all CPUS are stopped, we are safe to modify code */
  1376. hlist_for_each_entry_safe(rec, t, n, head, node) {
  1377. if (rec->flags & FTRACE_FL_FREE)
  1378. continue;
  1379. if ((rec->ip >= start) && (rec->ip < end))
  1380. ftrace_free_rec(rec);
  1381. }
  1382. }
  1383. ftrace_hash_unlock(flags);
  1384. per_cpu(ftrace_shutdown_disable_cpu, cpu)--;
  1385. preempt_enable_notrace();
  1386. }
  1387. static int ftraced(void *ignore)
  1388. {
  1389. unsigned long usecs;
  1390. while (!kthread_should_stop()) {
  1391. set_current_state(TASK_INTERRUPTIBLE);
  1392. /* check once a second */
  1393. schedule_timeout(HZ);
  1394. if (unlikely(ftrace_disabled))
  1395. continue;
  1396. mutex_lock(&ftrace_sysctl_lock);
  1397. mutex_lock(&ftraced_lock);
  1398. if (!ftraced_suspend && !ftraced_stop &&
  1399. ftrace_update_code()) {
  1400. usecs = nsecs_to_usecs(ftrace_update_time);
  1401. if (ftrace_update_tot_cnt > 100000) {
  1402. ftrace_update_tot_cnt = 0;
  1403. pr_info("hm, dftrace overflow: %lu change%s"
  1404. " (%lu total) in %lu usec%s\n",
  1405. ftrace_update_cnt,
  1406. ftrace_update_cnt != 1 ? "s" : "",
  1407. ftrace_update_tot_cnt,
  1408. usecs, usecs != 1 ? "s" : "");
  1409. ftrace_disabled = 1;
  1410. WARN_ON_ONCE(1);
  1411. }
  1412. }
  1413. mutex_unlock(&ftraced_lock);
  1414. mutex_unlock(&ftrace_sysctl_lock);
  1415. ftrace_shutdown_replenish();
  1416. }
  1417. __set_current_state(TASK_RUNNING);
  1418. return 0;
  1419. }
  1420. static int __init ftrace_dynamic_init(void)
  1421. {
  1422. struct task_struct *p;
  1423. unsigned long addr;
  1424. int ret;
  1425. addr = (unsigned long)ftrace_record_ip;
  1426. stop_machine(ftrace_dyn_arch_init, &addr, NULL);
  1427. /* ftrace_dyn_arch_init places the return code in addr */
  1428. if (addr) {
  1429. ret = (int)addr;
  1430. goto failed;
  1431. }
  1432. ret = ftrace_dyn_table_alloc(NR_TO_INIT);
  1433. if (ret)
  1434. goto failed;
  1435. p = kthread_run(ftraced, NULL, "ftraced");
  1436. if (IS_ERR(p)) {
  1437. ret = -1;
  1438. goto failed;
  1439. }
  1440. last_ftrace_enabled = ftrace_enabled = 1;
  1441. ftraced_task = p;
  1442. return 0;
  1443. failed:
  1444. ftrace_disabled = 1;
  1445. return ret;
  1446. }
  1447. core_initcall(ftrace_dynamic_init);
  1448. #endif /* CONFIG_FTRACE_MCOUNT_RECORD */
  1449. #else
  1450. # define ftrace_startup() do { } while (0)
  1451. # define ftrace_shutdown() do { } while (0)
  1452. # define ftrace_startup_sysctl() do { } while (0)
  1453. # define ftrace_shutdown_sysctl() do { } while (0)
  1454. #endif /* CONFIG_DYNAMIC_FTRACE */
  1455. /**
  1456. * ftrace_kill - kill ftrace
  1457. *
  1458. * This function should be used by panic code. It stops ftrace
  1459. * but in a not so nice way. If you need to simply kill ftrace
  1460. * from a non-atomic section, use ftrace_kill.
  1461. */
  1462. void ftrace_kill(void)
  1463. {
  1464. ftrace_disabled = 1;
  1465. ftrace_enabled = 0;
  1466. #ifdef CONFIG_DYNAMIC_FTRACE
  1467. ftraced_suspend = -1;
  1468. #endif
  1469. clear_ftrace_function();
  1470. }
  1471. /**
  1472. * register_ftrace_function - register a function for profiling
  1473. * @ops - ops structure that holds the function for profiling.
  1474. *
  1475. * Register a function to be called by all functions in the
  1476. * kernel.
  1477. *
  1478. * Note: @ops->func and all the functions it calls must be labeled
  1479. * with "notrace", otherwise it will go into a
  1480. * recursive loop.
  1481. */
  1482. int register_ftrace_function(struct ftrace_ops *ops)
  1483. {
  1484. int ret;
  1485. if (unlikely(ftrace_disabled))
  1486. return -1;
  1487. mutex_lock(&ftrace_sysctl_lock);
  1488. ret = __register_ftrace_function(ops);
  1489. ftrace_startup();
  1490. mutex_unlock(&ftrace_sysctl_lock);
  1491. return ret;
  1492. }
  1493. /**
  1494. * unregister_ftrace_function - unresgister a function for profiling.
  1495. * @ops - ops structure that holds the function to unregister
  1496. *
  1497. * Unregister a function that was added to be called by ftrace profiling.
  1498. */
  1499. int unregister_ftrace_function(struct ftrace_ops *ops)
  1500. {
  1501. int ret;
  1502. mutex_lock(&ftrace_sysctl_lock);
  1503. ret = __unregister_ftrace_function(ops);
  1504. ftrace_shutdown();
  1505. mutex_unlock(&ftrace_sysctl_lock);
  1506. return ret;
  1507. }
  1508. int
  1509. ftrace_enable_sysctl(struct ctl_table *table, int write,
  1510. struct file *file, void __user *buffer, size_t *lenp,
  1511. loff_t *ppos)
  1512. {
  1513. int ret;
  1514. if (unlikely(ftrace_disabled))
  1515. return -ENODEV;
  1516. mutex_lock(&ftrace_sysctl_lock);
  1517. ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
  1518. if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
  1519. goto out;
  1520. last_ftrace_enabled = ftrace_enabled;
  1521. if (ftrace_enabled) {
  1522. ftrace_startup_sysctl();
  1523. /* we are starting ftrace again */
  1524. if (ftrace_list != &ftrace_list_end) {
  1525. if (ftrace_list->next == &ftrace_list_end)
  1526. ftrace_trace_function = ftrace_list->func;
  1527. else
  1528. ftrace_trace_function = ftrace_list_func;
  1529. }
  1530. } else {
  1531. /* stopping ftrace calls (just send to ftrace_stub) */
  1532. ftrace_trace_function = ftrace_stub;
  1533. ftrace_shutdown_sysctl();
  1534. }
  1535. out:
  1536. mutex_unlock(&ftrace_sysctl_lock);
  1537. return ret;
  1538. }