.. | .. |
---|
1 | | -/* SPDX-License-Identifier: GPL-2.0 */ |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
---|
2 | 2 | /* |
---|
3 | 3 | * Copyright (C) 2013 Linaro Limited |
---|
4 | 4 | * Author: AKASHI Takahiro <takahiro.akashi@linaro.org> |
---|
.. | .. |
---|
7 | 7 | |
---|
8 | 8 | #include <linux/ftrace.h> |
---|
9 | 9 | #include <linux/uaccess.h> |
---|
| 10 | +#include <linux/memory.h> |
---|
10 | 11 | #include <asm/cacheflush.h> |
---|
| 12 | +#include <asm/patch.h> |
---|
11 | 13 | |
---|
12 | 14 | #ifdef CONFIG_DYNAMIC_FTRACE |
---|
| 15 | +int ftrace_arch_code_modify_prepare(void) __acquires(&text_mutex) |
---|
| 16 | +{ |
---|
| 17 | + mutex_lock(&text_mutex); |
---|
| 18 | + |
---|
| 19 | + /* |
---|
| 20 | + * The code sequences we use for ftrace can't be patched while the |
---|
| 21 | + * kernel is running, so we need to use stop_machine() to modify them |
---|
| 22 | + * for now. This doesn't play nice with text_mutex, we use this flag |
---|
| 23 | + * to elide the check. |
---|
| 24 | + */ |
---|
| 25 | + riscv_patch_in_stop_machine = true; |
---|
| 26 | + |
---|
| 27 | + return 0; |
---|
| 28 | +} |
---|
| 29 | + |
---|
| 30 | +int ftrace_arch_code_modify_post_process(void) __releases(&text_mutex) |
---|
| 31 | +{ |
---|
| 32 | + riscv_patch_in_stop_machine = false; |
---|
| 33 | + mutex_unlock(&text_mutex); |
---|
| 34 | + return 0; |
---|
| 35 | +} |
---|
| 36 | + |
---|
13 | 37 | static int ftrace_check_current_call(unsigned long hook_pos, |
---|
14 | 38 | unsigned int *expected) |
---|
15 | 39 | { |
---|
.. | .. |
---|
24 | 48 | * Read the text we want to modify; |
---|
25 | 49 | * return must be -EFAULT on read error |
---|
26 | 50 | */ |
---|
27 | | - if (probe_kernel_read(replaced, (void *)hook_pos, MCOUNT_INSN_SIZE)) |
---|
| 51 | + if (copy_from_kernel_nofault(replaced, (void *)hook_pos, |
---|
| 52 | + MCOUNT_INSN_SIZE)) |
---|
28 | 53 | return -EFAULT; |
---|
29 | 54 | |
---|
30 | 55 | /* |
---|
.. | .. |
---|
32 | 57 | * return must be -EINVAL on failed comparison |
---|
33 | 58 | */ |
---|
34 | 59 | if (memcmp(expected, replaced, sizeof(replaced))) { |
---|
35 | | - pr_err("%p: expected (%08x %08x) but get (%08x %08x)", |
---|
| 60 | + pr_err("%p: expected (%08x %08x) but got (%08x %08x)\n", |
---|
36 | 61 | (void *)hook_pos, expected[0], expected[1], replaced[0], |
---|
37 | 62 | replaced[1]); |
---|
38 | 63 | return -EINVAL; |
---|
.. | .. |
---|
46 | 71 | { |
---|
47 | 72 | unsigned int call[2]; |
---|
48 | 73 | unsigned int nops[2] = {NOP4, NOP4}; |
---|
49 | | - int ret = 0; |
---|
50 | 74 | |
---|
51 | 75 | make_call(hook_pos, target, call); |
---|
52 | 76 | |
---|
53 | | - /* replace the auipc-jalr pair at once */ |
---|
54 | | - ret = probe_kernel_write((void *)hook_pos, enable ? call : nops, |
---|
55 | | - MCOUNT_INSN_SIZE); |
---|
56 | | - /* return must be -EPERM on write error */ |
---|
57 | | - if (ret) |
---|
| 77 | + /* Replace the auipc-jalr pair at once. Return -EPERM on write error. */ |
---|
| 78 | + if (patch_text_nosync |
---|
| 79 | + ((void *)hook_pos, enable ? call : nops, MCOUNT_INSN_SIZE)) |
---|
58 | 80 | return -EPERM; |
---|
59 | | - |
---|
60 | | - smp_mb(); |
---|
61 | | - flush_icache_range((void *)hook_pos, (void *)hook_pos + MCOUNT_INSN_SIZE); |
---|
62 | 81 | |
---|
63 | 82 | return 0; |
---|
64 | 83 | } |
---|
.. | .. |
---|
100 | 119 | { |
---|
101 | 120 | int out; |
---|
102 | 121 | |
---|
103 | | - ftrace_arch_code_modify_prepare(); |
---|
| 122 | + mutex_lock(&text_mutex); |
---|
104 | 123 | out = ftrace_make_nop(mod, rec, MCOUNT_ADDR); |
---|
105 | | - ftrace_arch_code_modify_post_process(); |
---|
| 124 | + mutex_unlock(&text_mutex); |
---|
106 | 125 | |
---|
107 | 126 | return out; |
---|
108 | 127 | } |
---|