hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/arch/riscv/kernel/ftrace.c
....@@ -1,4 +1,4 @@
1
-/* SPDX-License-Identifier: GPL-2.0 */
1
+// SPDX-License-Identifier: GPL-2.0
22 /*
33 * Copyright (C) 2013 Linaro Limited
44 * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
....@@ -7,9 +7,23 @@
77
88 #include <linux/ftrace.h>
99 #include <linux/uaccess.h>
10
+#include <linux/memory.h>
1011 #include <asm/cacheflush.h>
12
+#include <asm/patch.h>
1113
1214 #ifdef CONFIG_DYNAMIC_FTRACE
15
+int ftrace_arch_code_modify_prepare(void) __acquires(&text_mutex)
16
+{
17
+ mutex_lock(&text_mutex);
18
+ return 0;
19
+}
20
+
21
+int ftrace_arch_code_modify_post_process(void) __releases(&text_mutex)
22
+{
23
+ mutex_unlock(&text_mutex);
24
+ return 0;
25
+}
26
+
1327 static int ftrace_check_current_call(unsigned long hook_pos,
1428 unsigned int *expected)
1529 {
....@@ -24,7 +38,8 @@
2438 * Read the text we want to modify;
2539 * return must be -EFAULT on read error
2640 */
27
- if (probe_kernel_read(replaced, (void *)hook_pos, MCOUNT_INSN_SIZE))
41
+ if (copy_from_kernel_nofault(replaced, (void *)hook_pos,
42
+ MCOUNT_INSN_SIZE))
2843 return -EFAULT;
2944
3045 /*
....@@ -32,7 +47,7 @@
3247 * return must be -EINVAL on failed comparison
3348 */
3449 if (memcmp(expected, replaced, sizeof(replaced))) {
35
- pr_err("%p: expected (%08x %08x) but get (%08x %08x)",
50
+ pr_err("%p: expected (%08x %08x) but got (%08x %08x)\n",
3651 (void *)hook_pos, expected[0], expected[1], replaced[0],
3752 replaced[1]);
3853 return -EINVAL;
....@@ -46,19 +61,13 @@
4661 {
4762 unsigned int call[2];
4863 unsigned int nops[2] = {NOP4, NOP4};
49
- int ret = 0;
5064
5165 make_call(hook_pos, target, call);
5266
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)
67
+ /* Replace the auipc-jalr pair at once. Return -EPERM on write error. */
68
+ if (patch_text_nosync
69
+ ((void *)hook_pos, enable ? call : nops, MCOUNT_INSN_SIZE))
5870 return -EPERM;
59
-
60
- smp_mb();
61
- flush_icache_range((void *)hook_pos, (void *)hook_pos + MCOUNT_INSN_SIZE);
6271
6372 return 0;
6473 }