ftrace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * Code for replacing ftrace calls with jumps.
  3. *
  4. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  5. *
  6. * Thanks goes to Ingo Molnar, for suggesting the idea.
  7. * Mathieu Desnoyers, for suggesting postponing the modifications.
  8. * Arjan van de Ven, for keeping me straight, and explaining to me
  9. * the dangers of modifying code on the run.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/spinlock.h>
  13. #include <linux/hardirq.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/ftrace.h>
  16. #include <linux/percpu.h>
  17. #include <linux/sched.h>
  18. #include <linux/init.h>
  19. #include <linux/list.h>
  20. #include <trace/syscall.h>
  21. #include <asm/cacheflush.h>
  22. #include <asm/ftrace.h>
  23. #include <asm/nops.h>
  24. #include <asm/nmi.h>
  25. #ifdef CONFIG_DYNAMIC_FTRACE
  26. /*
  27. * modifying_code is set to notify NMIs that they need to use
  28. * memory barriers when entering or exiting. But we don't want
  29. * to burden NMIs with unnecessary memory barriers when code
  30. * modification is not being done (which is most of the time).
  31. *
  32. * A mutex is already held when ftrace_arch_code_modify_prepare
  33. * and post_process are called. No locks need to be taken here.
  34. *
  35. * Stop machine will make sure currently running NMIs are done
  36. * and new NMIs will see the updated variable before we need
  37. * to worry about NMIs doing memory barriers.
  38. */
  39. static int modifying_code __read_mostly;
  40. static DEFINE_PER_CPU(int, save_modifying_code);
  41. int ftrace_arch_code_modify_prepare(void)
  42. {
  43. set_kernel_text_rw();
  44. modifying_code = 1;
  45. return 0;
  46. }
  47. int ftrace_arch_code_modify_post_process(void)
  48. {
  49. modifying_code = 0;
  50. set_kernel_text_ro();
  51. return 0;
  52. }
  53. union ftrace_code_union {
  54. char code[MCOUNT_INSN_SIZE];
  55. struct {
  56. char e8;
  57. int offset;
  58. } __attribute__((packed));
  59. };
  60. static int ftrace_calc_offset(long ip, long addr)
  61. {
  62. return (int)(addr - ip);
  63. }
  64. static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
  65. {
  66. static union ftrace_code_union calc;
  67. calc.e8 = 0xe8;
  68. calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
  69. /*
  70. * No locking needed, this must be called via kstop_machine
  71. * which in essence is like running on a uniprocessor machine.
  72. */
  73. return calc.code;
  74. }
  75. /*
  76. * Modifying code must take extra care. On an SMP machine, if
  77. * the code being modified is also being executed on another CPU
  78. * that CPU will have undefined results and possibly take a GPF.
  79. * We use kstop_machine to stop other CPUS from exectuing code.
  80. * But this does not stop NMIs from happening. We still need
  81. * to protect against that. We separate out the modification of
  82. * the code to take care of this.
  83. *
  84. * Two buffers are added: An IP buffer and a "code" buffer.
  85. *
  86. * 1) Put the instruction pointer into the IP buffer
  87. * and the new code into the "code" buffer.
  88. * 2) Wait for any running NMIs to finish and set a flag that says
  89. * we are modifying code, it is done in an atomic operation.
  90. * 3) Write the code
  91. * 4) clear the flag.
  92. * 5) Wait for any running NMIs to finish.
  93. *
  94. * If an NMI is executed, the first thing it does is to call
  95. * "ftrace_nmi_enter". This will check if the flag is set to write
  96. * and if it is, it will write what is in the IP and "code" buffers.
  97. *
  98. * The trick is, it does not matter if everyone is writing the same
  99. * content to the code location. Also, if a CPU is executing code
  100. * it is OK to write to that code location if the contents being written
  101. * are the same as what exists.
  102. */
  103. #define MOD_CODE_WRITE_FLAG (1 << 31) /* set when NMI should do the write */
  104. static atomic_t nmi_running = ATOMIC_INIT(0);
  105. static int mod_code_status; /* holds return value of text write */
  106. static void *mod_code_ip; /* holds the IP to write to */
  107. static void *mod_code_newcode; /* holds the text to write to the IP */
  108. static unsigned nmi_wait_count;
  109. static atomic_t nmi_update_count = ATOMIC_INIT(0);
  110. int ftrace_arch_read_dyn_info(char *buf, int size)
  111. {
  112. int r;
  113. r = snprintf(buf, size, "%u %u",
  114. nmi_wait_count,
  115. atomic_read(&nmi_update_count));
  116. return r;
  117. }
  118. static void clear_mod_flag(void)
  119. {
  120. int old = atomic_read(&nmi_running);
  121. for (;;) {
  122. int new = old & ~MOD_CODE_WRITE_FLAG;
  123. if (old == new)
  124. break;
  125. old = atomic_cmpxchg(&nmi_running, old, new);
  126. }
  127. }
  128. static void ftrace_mod_code(void)
  129. {
  130. /*
  131. * Yes, more than one CPU process can be writing to mod_code_status.
  132. * (and the code itself)
  133. * But if one were to fail, then they all should, and if one were
  134. * to succeed, then they all should.
  135. */
  136. mod_code_status = probe_kernel_write(mod_code_ip, mod_code_newcode,
  137. MCOUNT_INSN_SIZE);
  138. /* if we fail, then kill any new writers */
  139. if (mod_code_status)
  140. clear_mod_flag();
  141. }
  142. void ftrace_nmi_enter(void)
  143. {
  144. __get_cpu_var(save_modifying_code) = modifying_code;
  145. if (!__get_cpu_var(save_modifying_code))
  146. return;
  147. if (atomic_inc_return(&nmi_running) & MOD_CODE_WRITE_FLAG) {
  148. smp_rmb();
  149. ftrace_mod_code();
  150. atomic_inc(&nmi_update_count);
  151. }
  152. /* Must have previous changes seen before executions */
  153. smp_mb();
  154. }
  155. void ftrace_nmi_exit(void)
  156. {
  157. if (!__get_cpu_var(save_modifying_code))
  158. return;
  159. /* Finish all executions before clearing nmi_running */
  160. smp_mb();
  161. atomic_dec(&nmi_running);
  162. }
  163. static void wait_for_nmi_and_set_mod_flag(void)
  164. {
  165. if (!atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG))
  166. return;
  167. do {
  168. cpu_relax();
  169. } while (atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG));
  170. nmi_wait_count++;
  171. }
  172. static void wait_for_nmi(void)
  173. {
  174. if (!atomic_read(&nmi_running))
  175. return;
  176. do {
  177. cpu_relax();
  178. } while (atomic_read(&nmi_running));
  179. nmi_wait_count++;
  180. }
  181. static inline int
  182. within(unsigned long addr, unsigned long start, unsigned long end)
  183. {
  184. return addr >= start && addr < end;
  185. }
  186. static int
  187. do_ftrace_mod_code(unsigned long ip, void *new_code)
  188. {
  189. /*
  190. * On x86_64, kernel text mappings are mapped read-only with
  191. * CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead
  192. * of the kernel text mapping to modify the kernel text.
  193. *
  194. * For 32bit kernels, these mappings are same and we can use
  195. * kernel identity mapping to modify code.
  196. */
  197. if (within(ip, (unsigned long)_text, (unsigned long)_etext))
  198. ip = (unsigned long)__va(__pa(ip));
  199. mod_code_ip = (void *)ip;
  200. mod_code_newcode = new_code;
  201. /* The buffers need to be visible before we let NMIs write them */
  202. smp_mb();
  203. wait_for_nmi_and_set_mod_flag();
  204. /* Make sure all running NMIs have finished before we write the code */
  205. smp_mb();
  206. ftrace_mod_code();
  207. /* Make sure the write happens before clearing the bit */
  208. smp_mb();
  209. clear_mod_flag();
  210. wait_for_nmi();
  211. return mod_code_status;
  212. }
  213. static unsigned char *ftrace_nop_replace(void)
  214. {
  215. return ideal_nop5;
  216. }
  217. static int
  218. ftrace_modify_code(unsigned long ip, unsigned char *old_code,
  219. unsigned char *new_code)
  220. {
  221. unsigned char replaced[MCOUNT_INSN_SIZE];
  222. /*
  223. * Note: Due to modules and __init, code can
  224. * disappear and change, we need to protect against faulting
  225. * as well as code changing. We do this by using the
  226. * probe_kernel_* functions.
  227. *
  228. * No real locking needed, this code is run through
  229. * kstop_machine, or before SMP starts.
  230. */
  231. /* read the text we want to modify */
  232. if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
  233. return -EFAULT;
  234. /* Make sure it is what we expect it to be */
  235. if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
  236. return -EINVAL;
  237. /* replace the text with the new text */
  238. if (do_ftrace_mod_code(ip, new_code))
  239. return -EPERM;
  240. sync_core();
  241. return 0;
  242. }
  243. int ftrace_make_nop(struct module *mod,
  244. struct dyn_ftrace *rec, unsigned long addr)
  245. {
  246. unsigned char *new, *old;
  247. unsigned long ip = rec->ip;
  248. old = ftrace_call_replace(ip, addr);
  249. new = ftrace_nop_replace();
  250. return ftrace_modify_code(rec->ip, old, new);
  251. }
  252. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  253. {
  254. unsigned char *new, *old;
  255. unsigned long ip = rec->ip;
  256. old = ftrace_nop_replace();
  257. new = ftrace_call_replace(ip, addr);
  258. return ftrace_modify_code(rec->ip, old, new);
  259. }
  260. int ftrace_update_ftrace_func(ftrace_func_t func)
  261. {
  262. unsigned long ip = (unsigned long)(&ftrace_call);
  263. unsigned char old[MCOUNT_INSN_SIZE], *new;
  264. int ret;
  265. memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
  266. new = ftrace_call_replace(ip, (unsigned long)func);
  267. ret = ftrace_modify_code(ip, old, new);
  268. return ret;
  269. }
  270. int __init ftrace_dyn_arch_init(void *data)
  271. {
  272. /* The return code is retured via data */
  273. *(unsigned long *)data = 0;
  274. return 0;
  275. }
  276. #endif
  277. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  278. #ifdef CONFIG_DYNAMIC_FTRACE
  279. extern void ftrace_graph_call(void);
  280. static int ftrace_mod_jmp(unsigned long ip,
  281. int old_offset, int new_offset)
  282. {
  283. unsigned char code[MCOUNT_INSN_SIZE];
  284. if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE))
  285. return -EFAULT;
  286. if (code[0] != 0xe9 || old_offset != *(int *)(&code[1]))
  287. return -EINVAL;
  288. *(int *)(&code[1]) = new_offset;
  289. if (do_ftrace_mod_code(ip, &code))
  290. return -EPERM;
  291. return 0;
  292. }
  293. int ftrace_enable_ftrace_graph_caller(void)
  294. {
  295. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  296. int old_offset, new_offset;
  297. old_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
  298. new_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
  299. return ftrace_mod_jmp(ip, old_offset, new_offset);
  300. }
  301. int ftrace_disable_ftrace_graph_caller(void)
  302. {
  303. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  304. int old_offset, new_offset;
  305. old_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
  306. new_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
  307. return ftrace_mod_jmp(ip, old_offset, new_offset);
  308. }
  309. #endif /* !CONFIG_DYNAMIC_FTRACE */
  310. /*
  311. * Hook the return address and push it in the stack of return addrs
  312. * in current thread info.
  313. */
  314. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
  315. unsigned long frame_pointer)
  316. {
  317. unsigned long old;
  318. int faulted;
  319. struct ftrace_graph_ent trace;
  320. unsigned long return_hooker = (unsigned long)
  321. &return_to_handler;
  322. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  323. return;
  324. /*
  325. * Protect against fault, even if it shouldn't
  326. * happen. This tool is too much intrusive to
  327. * ignore such a protection.
  328. */
  329. asm volatile(
  330. "1: " _ASM_MOV " (%[parent]), %[old]\n"
  331. "2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
  332. " movl $0, %[faulted]\n"
  333. "3:\n"
  334. ".section .fixup, \"ax\"\n"
  335. "4: movl $1, %[faulted]\n"
  336. " jmp 3b\n"
  337. ".previous\n"
  338. _ASM_EXTABLE(1b, 4b)
  339. _ASM_EXTABLE(2b, 4b)
  340. : [old] "=&r" (old), [faulted] "=r" (faulted)
  341. : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
  342. : "memory"
  343. );
  344. if (unlikely(faulted)) {
  345. ftrace_graph_stop();
  346. WARN_ON(1);
  347. return;
  348. }
  349. if (ftrace_push_return_trace(old, self_addr, &trace.depth,
  350. frame_pointer) == -EBUSY) {
  351. *parent = old;
  352. return;
  353. }
  354. trace.func = self_addr;
  355. /* Only trace if the calling function expects to */
  356. if (!ftrace_graph_entry(&trace)) {
  357. current->curr_ret_stack--;
  358. *parent = old;
  359. }
  360. }
  361. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */