Skip to content

Commit ce194e9

Browse files
committed
powerpc/ftrace: Add support for DYNAMIC_FTRACE_WITH_DIRECT_CALLS
JIRA: https://issues.redhat.com/browse/RHEL-24555 commit a52f604 Author: Naveen N Rao <naveen@kernel.org> Date: Wed Oct 30 12:38:48 2024 +0530 powerpc/ftrace: Add support for DYNAMIC_FTRACE_WITH_DIRECT_CALLS Add support for DYNAMIC_FTRACE_WITH_DIRECT_CALLS similar to the arm64 implementation. ftrace direct calls allow custom trampolines to be called into directly from function ftrace call sites, bypassing the ftrace trampoline completely. This functionality is currently utilized by BPF trampolines to hook into kernel function entries. Since we have limited relative branch range, we support ftrace direct calls through support for DYNAMIC_FTRACE_WITH_CALL_OPS. In this approach, ftrace trampoline is not entirely bypassed. Rather, it is re-purposed into a stub that reads direct_call field from the associated ftrace_ops structure and branches into that, if it is not NULL. For this, it is sufficient if we can ensure that the ftrace trampoline is reachable from all traceable functions. When multiple ftrace_ops are associated with a call site, we utilize a call back to set pt_regs->orig_gpr3 that can then be tested on the return path from the ftrace trampoline to branch into the direct caller. Signed-off-by: Naveen N Rao <naveen@kernel.org> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au> Link: https://patch.msgid.link/20241030070850.1361304-16-hbathini@linux.ibm.com Signed-off-by: Viktor Malik <vmalik@redhat.com>
1 parent 23ab18d commit ce194e9

File tree

5 files changed

+116
-29
lines changed

5 files changed

+116
-29
lines changed

arch/powerpc/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ config PPC
237237
select HAVE_DYNAMIC_FTRACE
238238
select HAVE_DYNAMIC_FTRACE_WITH_ARGS if ARCH_USING_PATCHABLE_FUNCTION_ENTRY || MPROFILE_KERNEL || PPC32
239239
select HAVE_DYNAMIC_FTRACE_WITH_CALL_OPS if PPC_FTRACE_OUT_OF_LINE || (PPC32 && ARCH_USING_PATCHABLE_FUNCTION_ENTRY)
240+
select HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS if HAVE_DYNAMIC_FTRACE_WITH_CALL_OPS
240241
select HAVE_DYNAMIC_FTRACE_WITH_REGS if ARCH_USING_PATCHABLE_FUNCTION_ENTRY || MPROFILE_KERNEL || PPC32
241242
select HAVE_EBPF_JIT
242243
select HAVE_EFFICIENT_UNALIGNED_ACCESS

arch/powerpc/include/asm/ftrace.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,22 @@ extern unsigned int ftrace_ool_stub_text_end_count, ftrace_ool_stub_text_count,
148148
#endif
149149
void ftrace_free_init_tramp(void);
150150
unsigned long ftrace_call_adjust(unsigned long addr);
151+
152+
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
153+
/*
154+
* When an ftrace registered caller is tracing a function that is also set by a
155+
* register_ftrace_direct() call, it needs to be differentiated in the
156+
* ftrace_caller trampoline so that the direct call can be invoked after the
157+
* other ftrace ops. To do this, place the direct caller in the orig_gpr3 field
158+
* of pt_regs. This tells ftrace_caller that there's a direct caller.
159+
*/
160+
static inline void arch_ftrace_set_direct_caller(struct ftrace_regs *fregs, unsigned long addr)
161+
{
162+
struct pt_regs *regs = &fregs->regs;
163+
164+
regs->orig_gpr3 = addr;
165+
}
166+
#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
151167
#else
152168
static inline void ftrace_free_init_tramp(void) { }
153169
static inline unsigned long ftrace_call_adjust(unsigned long addr) { return addr; }

arch/powerpc/kernel/asm-offsets.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,9 @@ int main(void)
683683

684684
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS
685685
OFFSET(FTRACE_OPS_FUNC, ftrace_ops, func);
686+
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
687+
OFFSET(FTRACE_OPS_DIRECT_CALL, ftrace_ops, direct_call);
688+
#endif
686689
#endif
687690

688691
return 0;

arch/powerpc/kernel/trace/ftrace.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,17 @@ static int ftrace_get_call_inst(struct dyn_ftrace *rec, unsigned long addr, ppc_
150150
else
151151
ip = rec->ip;
152152

153+
if (!is_offset_in_branch_range(addr - ip) && addr != FTRACE_ADDR &&
154+
addr != FTRACE_REGS_ADDR) {
155+
/* This can only happen with ftrace direct */
156+
if (!IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS)) {
157+
pr_err("0x%lx (0x%lx): Unexpected target address 0x%lx\n",
158+
ip, rec->ip, addr);
159+
return -EINVAL;
160+
}
161+
addr = FTRACE_ADDR;
162+
}
163+
153164
if (is_offset_in_branch_range(addr - ip))
154165
/* Within range */
155166
stub = addr;

arch/powerpc/kernel/trace/ftrace_entry.S

Lines changed: 85 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,38 @@
3333
* and then arrange for the ftrace function to be called.
3434
*/
3535
.macro ftrace_regs_entry allregs
36-
/* Save the original return address in A's stack frame */
37-
PPC_STL r0, LRSAVE(r1)
3836
/* Create a minimal stack frame for representing B */
3937
PPC_STLU r1, -STACK_FRAME_MIN_SIZE(r1)
4038

4139
/* Create our stack frame + pt_regs */
4240
PPC_STLU r1,-SWITCH_FRAME_SIZE(r1)
4341

42+
.if \allregs == 1
43+
SAVE_GPRS(11, 12, r1)
44+
.endif
45+
46+
/* Get the _mcount() call site out of LR */
47+
mflr r11
48+
49+
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
50+
/* Load the ftrace_op */
51+
PPC_LL r12, -(MCOUNT_INSN_SIZE*2 + SZL)(r11)
52+
53+
/* Load direct_call from the ftrace_op */
54+
PPC_LL r12, FTRACE_OPS_DIRECT_CALL(r12)
55+
PPC_LCMPI r12, 0
56+
.if \allregs == 1
57+
bne .Lftrace_direct_call_regs
58+
.else
59+
bne .Lftrace_direct_call
60+
.endif
61+
#endif
62+
63+
/* Save the previous LR in pt_regs->link */
64+
PPC_STL r0, _LINK(r1)
65+
/* Also save it in A's stack frame */
66+
PPC_STL r0, SWITCH_FRAME_SIZE+STACK_FRAME_MIN_SIZE+LRSAVE(r1)
67+
4468
/* Save all gprs to pt_regs */
4569
SAVE_GPR(0, r1)
4670
SAVE_GPRS(3, 10, r1)
@@ -54,7 +78,7 @@
5478

5579
.if \allregs == 1
5680
SAVE_GPR(2, r1)
57-
SAVE_GPRS(11, 31, r1)
81+
SAVE_GPRS(13, 31, r1)
5882
.else
5983
#if defined(CONFIG_LIVEPATCH_64) || defined(CONFIG_PPC_FTRACE_OUT_OF_LINE)
6084
SAVE_GPR(14, r1)
@@ -67,29 +91,24 @@
6791

6892
.if \allregs == 1
6993
/* Load special regs for save below */
94+
mfcr r7
7095
mfmsr r8
7196
mfctr r9
7297
mfxer r10
73-
mfcr r11
7498
.else
7599
/* Clear MSR to flag as ftrace_caller versus frace_regs_caller */
76100
li r8, 0
77101
.endif
78102

79-
/* Get the _mcount() call site out of LR */
80-
mflr r7
81-
/* Save the read LR in pt_regs->link */
82-
PPC_STL r0, _LINK(r1)
83-
84103
#ifdef CONFIG_PPC64
85104
/* Save callee's TOC in the ABI compliant location */
86105
std r2, STK_GOT(r1)
87106
LOAD_PACA_TOC() /* get kernel TOC in r2 */
88107
#endif
89108

90109
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS
91-
/* r7 points to the instruction following the call to ftrace */
92-
PPC_LL r5, -(MCOUNT_INSN_SIZE*2 + SZL)(r7)
110+
/* r11 points to the instruction following the call to ftrace */
111+
PPC_LL r5, -(MCOUNT_INSN_SIZE*2 + SZL)(r11)
93112
PPC_LL r12, FTRACE_OPS_FUNC(r5)
94113
mtctr r12
95114
#else /* !CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS */
@@ -105,45 +124,51 @@
105124
/* Save special regs */
106125
PPC_STL r8, _MSR(r1)
107126
.if \allregs == 1
127+
PPC_STL r7, _CCR(r1)
108128
PPC_STL r9, _CTR(r1)
109129
PPC_STL r10, _XER(r1)
110-
PPC_STL r11, _CCR(r1)
111130
.endif
112131

132+
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
133+
/* Clear orig_gpr3 to later detect ftrace_direct call */
134+
li r7, 0
135+
PPC_STL r7, ORIG_GPR3(r1)
136+
#endif
137+
113138
#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
114139
/* Save our real return address in nvr for return */
115140
.if \allregs == 0
116141
SAVE_GPR(15, r1)
117142
.endif
118-
mr r15, r7
143+
mr r15, r11
119144
/*
120-
* We want the ftrace location in the function, but our lr (in r7)
145+
* We want the ftrace location in the function, but our lr (in r11)
121146
* points at the 'mtlr r0' instruction in the out of line stub. To
122147
* recover the ftrace location, we read the branch instruction in the
123148
* stub, and adjust our lr by the branch offset.
124149
*
125150
* See ftrace_init_ool_stub() for the profile sequence.
126151
*/
127-
lwz r8, MCOUNT_INSN_SIZE(r7)
152+
lwz r8, MCOUNT_INSN_SIZE(r11)
128153
slwi r8, r8, 6
129154
srawi r8, r8, 6
130-
add r3, r7, r8
155+
add r3, r11, r8
131156
/*
132157
* Override our nip to point past the branch in the original function.
133158
* This allows reliable stack trace and the ftrace stack tracer to work as-is.
134159
*/
135-
addi r7, r3, MCOUNT_INSN_SIZE
160+
addi r11, r3, MCOUNT_INSN_SIZE
136161
#else
137162
/* Calculate ip from nip-4 into r3 for call below */
138-
subi r3, r7, MCOUNT_INSN_SIZE
163+
subi r3, r11, MCOUNT_INSN_SIZE
139164
#endif
140165

141166
/* Save NIP as pt_regs->nip */
142-
PPC_STL r7, _NIP(r1)
167+
PPC_STL r11, _NIP(r1)
143168
/* Also save it in B's stackframe header for proper unwind */
144-
PPC_STL r7, LRSAVE+SWITCH_FRAME_SIZE(r1)
169+
PPC_STL r11, LRSAVE+SWITCH_FRAME_SIZE(r1)
145170
#if defined(CONFIG_LIVEPATCH_64) || defined(CONFIG_PPC_FTRACE_OUT_OF_LINE)
146-
mr r14, r7 /* remember old NIP */
171+
mr r14, r11 /* remember old NIP */
147172
#endif
148173

149174
/* Put the original return address in r4 as parent_ip */
@@ -154,14 +179,32 @@
154179
.endm
155180

156181
.macro ftrace_regs_exit allregs
182+
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
183+
/* Check orig_gpr3 to detect ftrace_direct call */
184+
PPC_LL r3, ORIG_GPR3(r1)
185+
PPC_LCMPI cr1, r3, 0
186+
mtctr r3
187+
#endif
188+
189+
/* Restore possibly modified LR */
190+
PPC_LL r0, _LINK(r1)
191+
157192
#ifndef CONFIG_PPC_FTRACE_OUT_OF_LINE
158193
/* Load ctr with the possibly modified NIP */
159194
PPC_LL r3, _NIP(r1)
160-
mtctr r3
161-
162195
#ifdef CONFIG_LIVEPATCH_64
163196
cmpd r14, r3 /* has NIP been altered? */
164197
#endif
198+
199+
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
200+
beq cr1,2f
201+
mtlr r3
202+
b 3f
203+
#endif
204+
2: mtctr r3
205+
mtlr r0
206+
3:
207+
165208
#else /* !CONFIG_PPC_FTRACE_OUT_OF_LINE */
166209
/* Load LR with the possibly modified NIP */
167210
PPC_LL r3, _NIP(r1)
@@ -185,12 +228,6 @@
185228
#endif
186229
.endif
187230

188-
/* Restore possibly modified LR */
189-
PPC_LL r0, _LINK(r1)
190-
#ifndef CONFIG_PPC_FTRACE_OUT_OF_LINE
191-
mtlr r0
192-
#endif
193-
194231
#ifdef CONFIG_PPC64
195232
/* Restore callee's TOC */
196233
ld r2, STK_GOT(r1)
@@ -203,8 +240,12 @@
203240
/* Based on the cmpd above, if the NIP was altered handle livepatch */
204241
bne- livepatch_handler
205242
#endif
243+
206244
/* jump after _mcount site */
207245
#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
246+
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
247+
bnectr cr1
248+
#endif
208249
/*
209250
* Return with blr to keep the link stack balanced. The function profiling sequence
210251
* uses 'mtlr r0' to restore LR.
@@ -260,6 +301,21 @@ ftrace_no_trace:
260301
#endif
261302
#endif
262303

304+
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
305+
.Lftrace_direct_call_regs:
306+
mtctr r12
307+
REST_GPRS(11, 12, r1)
308+
addi r1, r1, SWITCH_FRAME_SIZE+STACK_FRAME_MIN_SIZE
309+
bctr
310+
.Lftrace_direct_call:
311+
mtctr r12
312+
addi r1, r1, SWITCH_FRAME_SIZE+STACK_FRAME_MIN_SIZE
313+
bctr
314+
SYM_FUNC_START(ftrace_stub_direct_tramp)
315+
blr
316+
SYM_FUNC_END(ftrace_stub_direct_tramp)
317+
#endif
318+
263319
#ifdef CONFIG_LIVEPATCH_64
264320
/*
265321
* This function runs in the mcount context, between two functions. As

0 commit comments

Comments
 (0)