]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - arch/x86/kernel/ftrace.c
Merge branch 'tip/tracing/ftrace' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-artful-kernel.git] / arch / x86 / kernel / ftrace.c
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
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
21 #include <asm/ftrace.h>
22 #include <linux/ftrace.h>
23 #include <asm/nops.h>
24 #include <asm/nmi.h>
25
26
27 #ifdef CONFIG_DYNAMIC_FTRACE
28
29 union ftrace_code_union {
30 char code[MCOUNT_INSN_SIZE];
31 struct {
32 char e8;
33 int offset;
34 } __attribute__((packed));
35 };
36
37 static int ftrace_calc_offset(long ip, long addr)
38 {
39 return (int)(addr - ip);
40 }
41
42 static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
43 {
44 static union ftrace_code_union calc;
45
46 calc.e8 = 0xe8;
47 calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
48
49 /*
50 * No locking needed, this must be called via kstop_machine
51 * which in essence is like running on a uniprocessor machine.
52 */
53 return calc.code;
54 }
55
56 /*
57 * Modifying code must take extra care. On an SMP machine, if
58 * the code being modified is also being executed on another CPU
59 * that CPU will have undefined results and possibly take a GPF.
60 * We use kstop_machine to stop other CPUS from exectuing code.
61 * But this does not stop NMIs from happening. We still need
62 * to protect against that. We separate out the modification of
63 * the code to take care of this.
64 *
65 * Two buffers are added: An IP buffer and a "code" buffer.
66 *
67 * 1) Put the instruction pointer into the IP buffer
68 * and the new code into the "code" buffer.
69 * 2) Set a flag that says we are modifying code
70 * 3) Wait for any running NMIs to finish.
71 * 4) Write the code
72 * 5) clear the flag.
73 * 6) Wait for any running NMIs to finish.
74 *
75 * If an NMI is executed, the first thing it does is to call
76 * "ftrace_nmi_enter". This will check if the flag is set to write
77 * and if it is, it will write what is in the IP and "code" buffers.
78 *
79 * The trick is, it does not matter if everyone is writing the same
80 * content to the code location. Also, if a CPU is executing code
81 * it is OK to write to that code location if the contents being written
82 * are the same as what exists.
83 */
84
85 static atomic_t nmi_running = ATOMIC_INIT(0);
86 static int mod_code_status; /* holds return value of text write */
87 static int mod_code_write; /* set when NMI should do the write */
88 static void *mod_code_ip; /* holds the IP to write to */
89 static void *mod_code_newcode; /* holds the text to write to the IP */
90
91 static unsigned nmi_wait_count;
92 static atomic_t nmi_update_count = ATOMIC_INIT(0);
93
94 int ftrace_arch_read_dyn_info(char *buf, int size)
95 {
96 int r;
97
98 r = snprintf(buf, size, "%u %u",
99 nmi_wait_count,
100 atomic_read(&nmi_update_count));
101 return r;
102 }
103
104 static void ftrace_mod_code(void)
105 {
106 /*
107 * Yes, more than one CPU process can be writing to mod_code_status.
108 * (and the code itself)
109 * But if one were to fail, then they all should, and if one were
110 * to succeed, then they all should.
111 */
112 mod_code_status = probe_kernel_write(mod_code_ip, mod_code_newcode,
113 MCOUNT_INSN_SIZE);
114 }
115
116 void ftrace_nmi_enter(void)
117 {
118 atomic_inc(&nmi_running);
119 /* Must have nmi_running seen before reading write flag */
120 smp_mb();
121 if (mod_code_write) {
122 ftrace_mod_code();
123 atomic_inc(&nmi_update_count);
124 }
125 }
126
127 void ftrace_nmi_exit(void)
128 {
129 /* Finish all executions before clearing nmi_running */
130 smp_wmb();
131 atomic_dec(&nmi_running);
132 }
133
134 static void wait_for_nmi(void)
135 {
136 if (!atomic_read(&nmi_running))
137 return;
138
139 do {
140 cpu_relax();
141 } while (atomic_read(&nmi_running));
142
143 nmi_wait_count++;
144 }
145
146 static int
147 do_ftrace_mod_code(unsigned long ip, void *new_code)
148 {
149 mod_code_ip = (void *)ip;
150 mod_code_newcode = new_code;
151
152 /* The buffers need to be visible before we let NMIs write them */
153 smp_wmb();
154
155 mod_code_write = 1;
156
157 /* Make sure write bit is visible before we wait on NMIs */
158 smp_mb();
159
160 wait_for_nmi();
161
162 /* Make sure all running NMIs have finished before we write the code */
163 smp_mb();
164
165 ftrace_mod_code();
166
167 /* Make sure the write happens before clearing the bit */
168 smp_wmb();
169
170 mod_code_write = 0;
171
172 /* make sure NMIs see the cleared bit */
173 smp_mb();
174
175 wait_for_nmi();
176
177 return mod_code_status;
178 }
179
180
181
182
183 static unsigned char ftrace_nop[MCOUNT_INSN_SIZE];
184
185 static unsigned char *ftrace_nop_replace(void)
186 {
187 return ftrace_nop;
188 }
189
190 static int
191 ftrace_modify_code(unsigned long ip, unsigned char *old_code,
192 unsigned char *new_code)
193 {
194 unsigned char replaced[MCOUNT_INSN_SIZE];
195
196 /*
197 * Note: Due to modules and __init, code can
198 * disappear and change, we need to protect against faulting
199 * as well as code changing. We do this by using the
200 * probe_kernel_* functions.
201 *
202 * No real locking needed, this code is run through
203 * kstop_machine, or before SMP starts.
204 */
205
206 /* read the text we want to modify */
207 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
208 return -EFAULT;
209
210 /* Make sure it is what we expect it to be */
211 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
212 return -EINVAL;
213
214 /* replace the text with the new text */
215 if (do_ftrace_mod_code(ip, new_code))
216 return -EPERM;
217
218 sync_core();
219
220 return 0;
221 }
222
223 int ftrace_make_nop(struct module *mod,
224 struct dyn_ftrace *rec, unsigned long addr)
225 {
226 unsigned char *new, *old;
227 unsigned long ip = rec->ip;
228
229 old = ftrace_call_replace(ip, addr);
230 new = ftrace_nop_replace();
231
232 return ftrace_modify_code(rec->ip, old, new);
233 }
234
235 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
236 {
237 unsigned char *new, *old;
238 unsigned long ip = rec->ip;
239
240 old = ftrace_nop_replace();
241 new = ftrace_call_replace(ip, addr);
242
243 return ftrace_modify_code(rec->ip, old, new);
244 }
245
246 int ftrace_update_ftrace_func(ftrace_func_t func)
247 {
248 unsigned long ip = (unsigned long)(&ftrace_call);
249 unsigned char old[MCOUNT_INSN_SIZE], *new;
250 int ret;
251
252 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
253 new = ftrace_call_replace(ip, (unsigned long)func);
254 ret = ftrace_modify_code(ip, old, new);
255
256 return ret;
257 }
258
259 int __init ftrace_dyn_arch_init(void *data)
260 {
261 extern const unsigned char ftrace_test_p6nop[];
262 extern const unsigned char ftrace_test_nop5[];
263 extern const unsigned char ftrace_test_jmp[];
264 int faulted = 0;
265
266 /*
267 * There is no good nop for all x86 archs.
268 * We will default to using the P6_NOP5, but first we
269 * will test to make sure that the nop will actually
270 * work on this CPU. If it faults, we will then
271 * go to a lesser efficient 5 byte nop. If that fails
272 * we then just use a jmp as our nop. This isn't the most
273 * efficient nop, but we can not use a multi part nop
274 * since we would then risk being preempted in the middle
275 * of that nop, and if we enabled tracing then, it might
276 * cause a system crash.
277 *
278 * TODO: check the cpuid to determine the best nop.
279 */
280 asm volatile (
281 "ftrace_test_jmp:"
282 "jmp ftrace_test_p6nop\n"
283 "nop\n"
284 "nop\n"
285 "nop\n" /* 2 byte jmp + 3 bytes */
286 "ftrace_test_p6nop:"
287 P6_NOP5
288 "jmp 1f\n"
289 "ftrace_test_nop5:"
290 ".byte 0x66,0x66,0x66,0x66,0x90\n"
291 "1:"
292 ".section .fixup, \"ax\"\n"
293 "2: movl $1, %0\n"
294 " jmp ftrace_test_nop5\n"
295 "3: movl $2, %0\n"
296 " jmp 1b\n"
297 ".previous\n"
298 _ASM_EXTABLE(ftrace_test_p6nop, 2b)
299 _ASM_EXTABLE(ftrace_test_nop5, 3b)
300 : "=r"(faulted) : "0" (faulted));
301
302 switch (faulted) {
303 case 0:
304 pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
305 memcpy(ftrace_nop, ftrace_test_p6nop, MCOUNT_INSN_SIZE);
306 break;
307 case 1:
308 pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
309 memcpy(ftrace_nop, ftrace_test_nop5, MCOUNT_INSN_SIZE);
310 break;
311 case 2:
312 pr_info("ftrace: converting mcount calls to jmp . + 5\n");
313 memcpy(ftrace_nop, ftrace_test_jmp, MCOUNT_INSN_SIZE);
314 break;
315 }
316
317 /* The return code is retured via data */
318 *(unsigned long *)data = 0;
319
320 return 0;
321 }
322 #endif
323
324 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
325
326 #ifdef CONFIG_DYNAMIC_FTRACE
327 extern void ftrace_graph_call(void);
328
329 static int ftrace_mod_jmp(unsigned long ip,
330 int old_offset, int new_offset)
331 {
332 unsigned char code[MCOUNT_INSN_SIZE];
333
334 if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE))
335 return -EFAULT;
336
337 if (code[0] != 0xe9 || old_offset != *(int *)(&code[1]))
338 return -EINVAL;
339
340 *(int *)(&code[1]) = new_offset;
341
342 if (do_ftrace_mod_code(ip, &code))
343 return -EPERM;
344
345 return 0;
346 }
347
348 int ftrace_enable_ftrace_graph_caller(void)
349 {
350 unsigned long ip = (unsigned long)(&ftrace_graph_call);
351 int old_offset, new_offset;
352
353 old_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
354 new_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
355
356 return ftrace_mod_jmp(ip, old_offset, new_offset);
357 }
358
359 int ftrace_disable_ftrace_graph_caller(void)
360 {
361 unsigned long ip = (unsigned long)(&ftrace_graph_call);
362 int old_offset, new_offset;
363
364 old_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
365 new_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
366
367 return ftrace_mod_jmp(ip, old_offset, new_offset);
368 }
369
370 #endif /* !CONFIG_DYNAMIC_FTRACE */
371
372 /* Add a function return address to the trace stack on thread info.*/
373 static int push_return_trace(unsigned long ret, unsigned long long time,
374 unsigned long func, int *depth)
375 {
376 int index;
377
378 if (!current->ret_stack)
379 return -EBUSY;
380
381 /* The return trace stack is full */
382 if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
383 atomic_inc(&current->trace_overrun);
384 return -EBUSY;
385 }
386
387 index = ++current->curr_ret_stack;
388 barrier();
389 current->ret_stack[index].ret = ret;
390 current->ret_stack[index].func = func;
391 current->ret_stack[index].calltime = time;
392 *depth = index;
393
394 return 0;
395 }
396
397 /* Retrieve a function return address to the trace stack on thread info.*/
398 static void pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret)
399 {
400 int index;
401
402 index = current->curr_ret_stack;
403
404 if (unlikely(index < 0)) {
405 ftrace_graph_stop();
406 WARN_ON(1);
407 /* Might as well panic, otherwise we have no where to go */
408 *ret = (unsigned long)panic;
409 return;
410 }
411
412 *ret = current->ret_stack[index].ret;
413 trace->func = current->ret_stack[index].func;
414 trace->calltime = current->ret_stack[index].calltime;
415 trace->overrun = atomic_read(&current->trace_overrun);
416 trace->depth = index;
417 barrier();
418 current->curr_ret_stack--;
419
420 }
421
422 /*
423 * Send the trace to the ring-buffer.
424 * @return the original return address.
425 */
426 unsigned long ftrace_return_to_handler(void)
427 {
428 struct ftrace_graph_ret trace;
429 unsigned long ret;
430
431 pop_return_trace(&trace, &ret);
432 trace.rettime = cpu_clock(raw_smp_processor_id());
433 ftrace_graph_return(&trace);
434
435 if (unlikely(!ret)) {
436 ftrace_graph_stop();
437 WARN_ON(1);
438 /* Might as well panic. What else to do? */
439 ret = (unsigned long)panic;
440 }
441
442 return ret;
443 }
444
445 /*
446 * Hook the return address and push it in the stack of return addrs
447 * in current thread info.
448 */
449 void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
450 {
451 unsigned long old;
452 unsigned long long calltime;
453 int faulted;
454 struct ftrace_graph_ent trace;
455 unsigned long return_hooker = (unsigned long)
456 &return_to_handler;
457
458 /* Nmi's are currently unsupported */
459 if (unlikely(in_nmi()))
460 return;
461
462 if (unlikely(atomic_read(&current->tracing_graph_pause)))
463 return;
464
465 /*
466 * Protect against fault, even if it shouldn't
467 * happen. This tool is too much intrusive to
468 * ignore such a protection.
469 */
470 asm volatile(
471 "1: " _ASM_MOV " (%[parent]), %[old]\n"
472 "2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
473 " movl $0, %[faulted]\n"
474 "3:\n"
475
476 ".section .fixup, \"ax\"\n"
477 "4: movl $1, %[faulted]\n"
478 " jmp 3b\n"
479 ".previous\n"
480
481 _ASM_EXTABLE(1b, 4b)
482 _ASM_EXTABLE(2b, 4b)
483
484 : [old] "=r" (old), [faulted] "=r" (faulted)
485 : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
486 : "memory"
487 );
488
489 if (unlikely(faulted)) {
490 ftrace_graph_stop();
491 WARN_ON(1);
492 return;
493 }
494
495 calltime = cpu_clock(raw_smp_processor_id());
496
497 if (push_return_trace(old, calltime,
498 self_addr, &trace.depth) == -EBUSY) {
499 *parent = old;
500 return;
501 }
502
503 trace.func = self_addr;
504
505 /* Only trace if the calling function expects to */
506 if (!ftrace_graph_entry(&trace)) {
507 current->curr_ret_stack--;
508 *parent = old;
509 }
510 }
511 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */