Skip to content

Commit b2137c3

Browse files
AndybnACTpalmer-dabbelt
authored andcommitted
riscv: ftrace: prepare ftrace for atomic code patching
We use an AUIPC+JALR pair to jump into a ftrace trampoline. Since instruction fetch can break down to 4 byte at a time, it is impossible to update two instructions without a race. In order to mitigate it, we initialize the patchable entry to AUIPC + NOP4. Then, the run-time code patching can change NOP4 to JALR to eable/disable ftrcae from a function. This limits the reach of each ftrace entry to +-2KB displacing from ftrace_caller. Starting from the trampoline, we add a level of indirection for it to reach ftrace caller target. Now, it loads the target address from a memory location, then perform the jump. This enable the kernel to update the target atomically. The new don't-stop-the-world text patching on change only one RISC-V instruction: | -8: &ftrace_ops of the associated tracer function. | <ftrace enable>: | 0: auipc t0, hi(ftrace_caller) | 4: jalr t0, lo(ftrace_caller) | | -8: &ftrace_nop_ops | <ftrace disable>: | 0: auipc t0, hi(ftrace_caller) | 4: nop This means that f+0x0 is fixed, and should not be claimed by ftrace, e.g. kprobe should be able to put a probe in f+0x0. Thus, we adjust the offset and MCOUNT_INSN_SIZE accordingly. [ alex: Fix build errors with !CONFIG_DYNAMIC_FTRACE ] Co-developed-by: Björn Töpel <[email protected]> Signed-off-by: Björn Töpel <[email protected]> Signed-off-by: Andy Chiu <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexandre Ghiti <[email protected]> Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 500e626 commit b2137c3

File tree

3 files changed

+98
-97
lines changed

3 files changed

+98
-97
lines changed

arch/riscv/include/asm/ftrace.h

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ extern void *return_address(unsigned int level);
2020
#define ftrace_return_address(n) return_address(n)
2121

2222
void _mcount(void);
23-
static inline unsigned long ftrace_call_adjust(unsigned long addr)
24-
{
25-
return addr;
26-
}
23+
unsigned long ftrace_call_adjust(unsigned long addr);
24+
unsigned long arch_ftrace_get_symaddr(unsigned long fentry_ip);
25+
#define ftrace_get_symaddr(fentry_ip) arch_ftrace_get_symaddr(fentry_ip)
2726

2827
/*
2928
* Let's do like x86/arm64 and ignore the compat syscalls.
@@ -57,12 +56,21 @@ struct dyn_arch_ftrace {
5756
* 2) jalr: setting low-12 offset to ra, jump to ra, and set ra to
5857
* return address (original pc + 4)
5958
*
59+
* The first 2 instructions for each tracable function is compiled to 2 nop
60+
* instructions. Then, the kernel initializes the first instruction to auipc at
61+
* boot time (<ftrace disable>). The second instruction is patched to jalr to
62+
* start the trace.
63+
*
64+
*<Image>:
65+
* 0: nop
66+
* 4: nop
67+
*
6068
*<ftrace enable>:
61-
* 0: auipc t0/ra, 0x?
62-
* 4: jalr t0/ra, ?(t0/ra)
69+
* 0: auipc t0, 0x?
70+
* 4: jalr t0, ?(t0)
6371
*
6472
*<ftrace disable>:
65-
* 0: nop
73+
* 0: auipc t0, 0x?
6674
* 4: nop
6775
*
6876
* Dynamic ftrace generates probes to call sites, so we must deal with
@@ -75,10 +83,9 @@ struct dyn_arch_ftrace {
7583
#define AUIPC_OFFSET_MASK (0xfffff000)
7684
#define AUIPC_PAD (0x00001000)
7785
#define JALR_SHIFT 20
78-
#define JALR_RA (0x000080e7)
79-
#define AUIPC_RA (0x00000097)
8086
#define JALR_T0 (0x000282e7)
8187
#define AUIPC_T0 (0x00000297)
88+
#define JALR_RANGE (JALR_SIGN_MASK - 1)
8289

8390
#define to_jalr_t0(offset) \
8491
(((offset & JALR_OFFSET_MASK) << JALR_SHIFT) | JALR_T0)
@@ -96,26 +103,14 @@ do { \
96103
call[1] = to_jalr_t0(offset); \
97104
} while (0)
98105

99-
#define to_jalr_ra(offset) \
100-
(((offset & JALR_OFFSET_MASK) << JALR_SHIFT) | JALR_RA)
101-
102-
#define to_auipc_ra(offset) \
103-
((offset & JALR_SIGN_MASK) ? \
104-
(((offset & AUIPC_OFFSET_MASK) + AUIPC_PAD) | AUIPC_RA) : \
105-
((offset & AUIPC_OFFSET_MASK) | AUIPC_RA))
106-
107-
#define make_call_ra(caller, callee, call) \
108-
do { \
109-
unsigned int offset = \
110-
(unsigned long) (callee) - (unsigned long) (caller); \
111-
call[0] = to_auipc_ra(offset); \
112-
call[1] = to_jalr_ra(offset); \
113-
} while (0)
114-
115106
/*
116-
* Let auipc+jalr be the basic *mcount unit*, so we make it 8 bytes here.
107+
* Only the jalr insn in the auipc+jalr is patched, so we make it 4
108+
* bytes here.
117109
*/
118-
#define MCOUNT_INSN_SIZE 8
110+
#define MCOUNT_INSN_SIZE 4
111+
#define MCOUNT_AUIPC_SIZE 4
112+
#define MCOUNT_JALR_SIZE 4
113+
#define MCOUNT_NOP4_SIZE 4
119114

120115
#ifndef __ASSEMBLY__
121116
struct dyn_ftrace;

arch/riscv/kernel/ftrace.c

Lines changed: 72 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,22 @@
88
#include <linux/ftrace.h>
99
#include <linux/uaccess.h>
1010
#include <linux/memory.h>
11+
#include <linux/irqflags.h>
1112
#include <linux/stop_machine.h>
1213
#include <asm/cacheflush.h>
1314
#include <asm/text-patching.h>
1415

1516
#ifdef CONFIG_DYNAMIC_FTRACE
17+
unsigned long ftrace_call_adjust(unsigned long addr)
18+
{
19+
return addr + MCOUNT_AUIPC_SIZE;
20+
}
21+
22+
unsigned long arch_ftrace_get_symaddr(unsigned long fentry_ip)
23+
{
24+
return fentry_ip - MCOUNT_AUIPC_SIZE;
25+
}
26+
1627
void ftrace_arch_code_modify_prepare(void) __acquires(&text_mutex)
1728
{
1829
mutex_lock(&text_mutex);
@@ -32,74 +43,54 @@ void ftrace_arch_code_modify_post_process(void) __releases(&text_mutex)
3243
mutex_unlock(&text_mutex);
3344
}
3445

35-
static int ftrace_check_current_call(unsigned long hook_pos,
36-
unsigned int *expected)
46+
static int __ftrace_modify_call(unsigned long source, unsigned long target, bool validate)
3747
{
48+
unsigned int call[2], offset;
3849
unsigned int replaced[2];
39-
unsigned int nops[2] = {RISCV_INSN_NOP4, RISCV_INSN_NOP4};
4050

41-
/* we expect nops at the hook position */
42-
if (!expected)
43-
expected = nops;
51+
offset = target - source;
52+
call[1] = to_jalr_t0(offset);
4453

45-
/*
46-
* Read the text we want to modify;
47-
* return must be -EFAULT on read error
48-
*/
49-
if (copy_from_kernel_nofault(replaced, (void *)hook_pos,
50-
MCOUNT_INSN_SIZE))
51-
return -EFAULT;
52-
53-
/*
54-
* Make sure it is what we expect it to be;
55-
* return must be -EINVAL on failed comparison
56-
*/
57-
if (memcmp(expected, replaced, sizeof(replaced))) {
58-
pr_err("%p: expected (%08x %08x) but got (%08x %08x)\n",
59-
(void *)hook_pos, expected[0], expected[1], replaced[0],
60-
replaced[1]);
61-
return -EINVAL;
54+
if (validate) {
55+
call[0] = to_auipc_t0(offset);
56+
/*
57+
* Read the text we want to modify;
58+
* return must be -EFAULT on read error
59+
*/
60+
if (copy_from_kernel_nofault(replaced, (void *)source, 2 * MCOUNT_INSN_SIZE))
61+
return -EFAULT;
62+
63+
if (replaced[0] != call[0]) {
64+
pr_err("%p: expected (%08x) but got (%08x)\n",
65+
(void *)source, call[0], replaced[0]);
66+
return -EINVAL;
67+
}
6268
}
6369

64-
return 0;
65-
}
66-
67-
static int __ftrace_modify_call(unsigned long hook_pos, unsigned long target,
68-
bool enable, bool ra)
69-
{
70-
unsigned int call[2];
71-
unsigned int nops[2] = {RISCV_INSN_NOP4, RISCV_INSN_NOP4};
72-
73-
if (ra)
74-
make_call_ra(hook_pos, target, call);
75-
else
76-
make_call_t0(hook_pos, target, call);
77-
78-
/* Replace the auipc-jalr pair at once. Return -EPERM on write error. */
79-
if (patch_insn_write((void *)hook_pos, enable ? call : nops, MCOUNT_INSN_SIZE))
70+
/* Replace the jalr at once. Return -EPERM on write error. */
71+
if (patch_insn_write((void *)(source + MCOUNT_AUIPC_SIZE), call + 1, MCOUNT_JALR_SIZE))
8072
return -EPERM;
8173

8274
return 0;
8375
}
8476

8577
int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
8678
{
87-
unsigned int call[2];
88-
89-
make_call_t0(rec->ip, addr, call);
79+
unsigned long distance, orig_addr, pc = rec->ip - MCOUNT_AUIPC_SIZE;
9080

91-
if (patch_insn_write((void *)rec->ip, call, MCOUNT_INSN_SIZE))
92-
return -EPERM;
81+
orig_addr = (unsigned long)&ftrace_caller;
82+
distance = addr > orig_addr ? addr - orig_addr : orig_addr - addr;
83+
if (distance > JALR_RANGE)
84+
return -EINVAL;
9385

94-
return 0;
86+
return __ftrace_modify_call(pc, addr, false);
9587
}
9688

97-
int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
98-
unsigned long addr)
89+
int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsigned long addr)
9990
{
100-
unsigned int nops[2] = {RISCV_INSN_NOP4, RISCV_INSN_NOP4};
91+
u32 nop4 = RISCV_INSN_NOP4;
10192

102-
if (patch_insn_write((void *)rec->ip, nops, MCOUNT_INSN_SIZE))
93+
if (patch_insn_write((void *)rec->ip, &nop4, MCOUNT_NOP4_SIZE))
10394
return -EPERM;
10495

10596
return 0;
@@ -114,21 +105,38 @@ int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
114105
*/
115106
int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
116107
{
117-
int out;
108+
unsigned long pc = rec->ip - MCOUNT_AUIPC_SIZE;
109+
unsigned int nops[2], offset;
110+
int ret;
111+
112+
offset = (unsigned long) &ftrace_caller - pc;
113+
nops[0] = to_auipc_t0(offset);
114+
nops[1] = RISCV_INSN_NOP4;
118115

119116
mutex_lock(&text_mutex);
120-
out = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
117+
ret = patch_insn_write((void *)pc, nops, 2 * MCOUNT_INSN_SIZE);
121118
mutex_unlock(&text_mutex);
122119

123-
return out;
120+
return ret;
124121
}
125122

123+
ftrace_func_t ftrace_call_dest = ftrace_stub;
126124
int ftrace_update_ftrace_func(ftrace_func_t func)
127125
{
128-
int ret = __ftrace_modify_call((unsigned long)&ftrace_call,
129-
(unsigned long)func, true, true);
130-
131-
return ret;
126+
WRITE_ONCE(ftrace_call_dest, func);
127+
/*
128+
* The data fence ensure that the update to ftrace_call_dest happens
129+
* before the write to function_trace_op later in the generic ftrace.
130+
* If the sequence is not enforced, then an old ftrace_call_dest may
131+
* race loading a new function_trace_op set in ftrace_modify_all_code
132+
*
133+
* If we are in stop_machine, then we don't need to call remote fence
134+
* as there is no concurrent read-side of ftrace_call_dest.
135+
*/
136+
smp_wmb();
137+
if (!irqs_disabled())
138+
smp_call_function(ftrace_sync_ipi, NULL, 1);
139+
return 0;
132140
}
133141

134142
struct ftrace_modify_param {
@@ -166,23 +174,22 @@ void arch_ftrace_update_code(int command)
166174

167175
stop_machine(__ftrace_modify_code, &param, cpu_online_mask);
168176
}
169-
#endif
177+
#else /* CONFIG_DYNAMIC_FTRACE */
178+
unsigned long ftrace_call_adjust(unsigned long addr)
179+
{
180+
return addr;
181+
}
182+
#endif /* CONFIG_DYNAMIC_FTRACE */
170183

171184
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
172185
int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
173186
unsigned long addr)
174187
{
188+
unsigned long caller = rec->ip - MCOUNT_AUIPC_SIZE;
175189
unsigned int call[2];
176-
unsigned long caller = rec->ip;
177-
int ret;
178190

179191
make_call_t0(caller, old_addr, call);
180-
ret = ftrace_check_current_call(caller, call);
181-
182-
if (ret)
183-
return ret;
184-
185-
return __ftrace_modify_call(caller, addr, true, false);
192+
return __ftrace_modify_call(caller, addr, true);
186193
}
187194
#endif
188195

arch/riscv/kernel/mcount-dyn.S

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
.text
1515

16-
#define FENTRY_RA_OFFSET 8
1716
#define ABI_SIZE_ON_STACK 80
1817
#define ABI_A0 0
1918
#define ABI_A1 8
@@ -62,8 +61,7 @@
6261
* After the stack is established,
6362
*
6463
* 0(sp) stores the PC of the traced function which can be accessed
65-
* by &(fregs)->epc in tracing function. Note that the real
66-
* function entry address should be computed with -FENTRY_RA_OFFSET.
64+
* by &(fregs)->epc in tracing function.
6765
*
6866
* 8(sp) stores the function return address (i.e. parent IP) that
6967
* can be accessed by &(fregs)->ra in tracing function.
@@ -140,7 +138,7 @@
140138
.endm
141139

142140
.macro PREPARE_ARGS
143-
addi a0, t0, -FENTRY_RA_OFFSET
141+
addi a0, t0, -MCOUNT_JALR_SIZE // ip (callsite's jalr insn)
144142
la a1, function_trace_op
145143
REG_L a2, 0(a1)
146144
mv a1, ra
@@ -153,7 +151,8 @@ SYM_FUNC_START(ftrace_caller)
153151
PREPARE_ARGS
154152

155153
SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL)
156-
call ftrace_stub
154+
REG_L ra, ftrace_call_dest
155+
jalr ra, 0(ra)
157156

158157
RESTORE_ABI_REGS
159158
bnez t1, .Ldirect

0 commit comments

Comments
 (0)