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