Skip to content

Commit 657480d

Browse files
svens-s390Vasily Gorbik
authored andcommitted
s390: support KPROBES_ON_FTRACE
Instead of using our own kprobes-on-ftrace handling convert the code to support KPROBES_ON_FTRACE. Signed-off-by: Sven Schnelle <svens@linux.ibm.com> Signed-off-by: Vasily Gorbik <gor@linux.ibm.com>
1 parent 5f490a5 commit 657480d

6 files changed

Lines changed: 59 additions & 92 deletions

File tree

‎Documentation/features/debug/kprobes-on-ftrace/arch-support.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
| parisc: | ok |
2525
| powerpc: | ok |
2626
| riscv: | TODO |
27-
| s390: | TODO |
27+
| s390: | ok |
2828
| sh: | TODO |
2929
| sparc: | TODO |
3030
| um: | TODO |

‎arch/s390/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ config S390
156156
select HAVE_KERNEL_UNCOMPRESSED
157157
select HAVE_KERNEL_XZ
158158
select HAVE_KPROBES
159+
select HAVE_KPROBES_ON_FTRACE
159160
select HAVE_KRETPROBES
160161
select HAVE_KVM
161162
select HAVE_LIVEPATCH

‎arch/s390/include/asm/kprobes.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ typedef u16 kprobe_opcode_t;
5454
struct arch_specific_insn {
5555
/* copy of original instruction */
5656
kprobe_opcode_t *insn;
57-
unsigned int is_ftrace_insn : 1;
5857
};
5958

6059
struct prev_kprobe {

‎arch/s390/kernel/ftrace.c‎

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,6 @@ static inline void ftrace_generate_orig_insn(struct ftrace_insn *insn)
7272
#endif
7373
}
7474

75-
static inline int is_kprobe_on_ftrace(struct ftrace_insn *insn)
76-
{
77-
#ifdef CONFIG_KPROBES
78-
if (insn->opc == BREAKPOINT_INSTRUCTION)
79-
return 1;
80-
#endif
81-
return 0;
82-
}
83-
8475
static inline void ftrace_generate_kprobe_nop_insn(struct ftrace_insn *insn)
8576
{
8677
#ifdef CONFIG_KPROBES
@@ -114,16 +105,6 @@ int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
114105
/* Initial code replacement */
115106
ftrace_generate_orig_insn(&orig);
116107
ftrace_generate_nop_insn(&new);
117-
} else if (is_kprobe_on_ftrace(&old)) {
118-
/*
119-
* If we find a breakpoint instruction, a kprobe has been
120-
* placed at the beginning of the function. We write the
121-
* constant KPROBE_ON_FTRACE_NOP into the remaining four
122-
* bytes of the original instruction so that the kprobes
123-
* handler can execute a nop, if it reaches this breakpoint.
124-
*/
125-
ftrace_generate_kprobe_call_insn(&orig);
126-
ftrace_generate_kprobe_nop_insn(&new);
127108
} else {
128109
/* Replace ftrace call with a nop. */
129110
ftrace_generate_call_insn(&orig, rec->ip);
@@ -142,21 +123,10 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
142123

143124
if (probe_kernel_read(&old, (void *) rec->ip, sizeof(old)))
144125
return -EFAULT;
145-
if (is_kprobe_on_ftrace(&old)) {
146-
/*
147-
* If we find a breakpoint instruction, a kprobe has been
148-
* placed at the beginning of the function. We write the
149-
* constant KPROBE_ON_FTRACE_CALL into the remaining four
150-
* bytes of the original instruction so that the kprobes
151-
* handler can execute a brasl if it reaches this breakpoint.
152-
*/
153-
ftrace_generate_kprobe_nop_insn(&orig);
154-
ftrace_generate_kprobe_call_insn(&new);
155-
} else {
156-
/* Replace nop with an ftrace call. */
157-
ftrace_generate_nop_insn(&orig);
158-
ftrace_generate_call_insn(&new, rec->ip);
159-
}
126+
/* Replace nop with an ftrace call. */
127+
ftrace_generate_nop_insn(&orig);
128+
ftrace_generate_call_insn(&new, rec->ip);
129+
160130
/* Verify that the to be replaced code matches what we expect. */
161131
if (memcmp(&orig, &old, sizeof(old)))
162132
return -EINVAL;
@@ -241,3 +211,45 @@ int ftrace_disable_ftrace_graph_caller(void)
241211
}
242212

243213
#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
214+
215+
#ifdef CONFIG_KPROBES_ON_FTRACE
216+
void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
217+
struct ftrace_ops *ops, struct pt_regs *regs)
218+
{
219+
struct kprobe_ctlblk *kcb;
220+
struct kprobe *p = get_kprobe((kprobe_opcode_t *)ip);
221+
222+
if (unlikely(!p) || kprobe_disabled(p))
223+
return;
224+
225+
if (kprobe_running()) {
226+
kprobes_inc_nmissed_count(p);
227+
return;
228+
}
229+
230+
__this_cpu_write(current_kprobe, p);
231+
232+
kcb = get_kprobe_ctlblk();
233+
kcb->kprobe_status = KPROBE_HIT_ACTIVE;
234+
235+
instruction_pointer_set(regs, ip);
236+
237+
if (!p->pre_handler || !p->pre_handler(p, regs)) {
238+
239+
instruction_pointer_set(regs, ip + MCOUNT_INSN_SIZE);
240+
241+
if (unlikely(p->post_handler)) {
242+
kcb->kprobe_status = KPROBE_HIT_SSDONE;
243+
p->post_handler(p, regs, 0);
244+
}
245+
}
246+
__this_cpu_write(current_kprobe, NULL);
247+
}
248+
NOKPROBE_SYMBOL(kprobe_ftrace_handler);
249+
250+
int arch_prepare_kprobe_ftrace(struct kprobe *p)
251+
{
252+
p->ainsn.insn = NULL;
253+
return 0;
254+
}
255+
#endif

‎arch/s390/kernel/kprobes.c‎

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,10 @@ struct kprobe_insn_cache kprobe_s390_insn_slots = {
5656

5757
static void copy_instruction(struct kprobe *p)
5858
{
59-
unsigned long ip = (unsigned long) p->addr;
6059
s64 disp, new_disp;
6160
u64 addr, new_addr;
6261

63-
if (ftrace_location(ip) == ip) {
64-
/*
65-
* If kprobes patches the instruction that is morphed by
66-
* ftrace make sure that kprobes always sees the branch
67-
* "jg .+24" that skips the mcount block or the "brcl 0,0"
68-
* in case of hotpatch.
69-
*/
70-
ftrace_generate_nop_insn((struct ftrace_insn *)p->ainsn.insn);
71-
p->ainsn.is_ftrace_insn = 1;
72-
} else
73-
memcpy(p->ainsn.insn, p->addr, insn_length(*p->addr >> 8));
62+
memcpy(p->ainsn.insn, p->addr, insn_length(*p->addr >> 8));
7463
p->opcode = p->ainsn.insn[0];
7564
if (!probe_is_insn_relative_long(p->ainsn.insn))
7665
return;
@@ -136,11 +125,6 @@ int arch_prepare_kprobe(struct kprobe *p)
136125
}
137126
NOKPROBE_SYMBOL(arch_prepare_kprobe);
138127

139-
int arch_check_ftrace_location(struct kprobe *p)
140-
{
141-
return 0;
142-
}
143-
144128
struct swap_insn_args {
145129
struct kprobe *p;
146130
unsigned int arm_kprobe : 1;
@@ -149,28 +133,11 @@ struct swap_insn_args {
149133
static int swap_instruction(void *data)
150134
{
151135
struct swap_insn_args *args = data;
152-
struct ftrace_insn new_insn, *insn;
153136
struct kprobe *p = args->p;
154-
size_t len;
155-
156-
new_insn.opc = args->arm_kprobe ? BREAKPOINT_INSTRUCTION : p->opcode;
157-
len = sizeof(new_insn.opc);
158-
if (!p->ainsn.is_ftrace_insn)
159-
goto skip_ftrace;
160-
len = sizeof(new_insn);
161-
insn = (struct ftrace_insn *) p->addr;
162-
if (args->arm_kprobe) {
163-
if (is_ftrace_nop(insn))
164-
new_insn.disp = KPROBE_ON_FTRACE_NOP;
165-
else
166-
new_insn.disp = KPROBE_ON_FTRACE_CALL;
167-
} else {
168-
ftrace_generate_call_insn(&new_insn, (unsigned long)p->addr);
169-
if (insn->disp == KPROBE_ON_FTRACE_NOP)
170-
ftrace_generate_nop_insn(&new_insn);
171-
}
172-
skip_ftrace:
173-
s390_kernel_write(p->addr, &new_insn, len);
137+
u16 opc;
138+
139+
opc = args->arm_kprobe ? BREAKPOINT_INSTRUCTION : p->opcode;
140+
s390_kernel_write(p->addr, &opc, sizeof(opc));
174141
return 0;
175142
}
176143
NOKPROBE_SYMBOL(swap_instruction);
@@ -464,24 +431,6 @@ static void resume_execution(struct kprobe *p, struct pt_regs *regs)
464431
unsigned long ip = regs->psw.addr;
465432
int fixup = probe_get_fixup_type(p->ainsn.insn);
466433

467-
/* Check if the kprobes location is an enabled ftrace caller */
468-
if (p->ainsn.is_ftrace_insn) {
469-
struct ftrace_insn *insn = (struct ftrace_insn *) p->addr;
470-
struct ftrace_insn call_insn;
471-
472-
ftrace_generate_call_insn(&call_insn, (unsigned long) p->addr);
473-
/*
474-
* A kprobe on an enabled ftrace call site actually single
475-
* stepped an unconditional branch (ftrace nop equivalent).
476-
* Now we need to fixup things and pretend that a brasl r0,...
477-
* was executed instead.
478-
*/
479-
if (insn->disp == KPROBE_ON_FTRACE_CALL) {
480-
ip += call_insn.disp * 2 - MCOUNT_INSN_SIZE;
481-
regs->gprs[0] = (unsigned long)p->addr + sizeof(*insn);
482-
}
483-
}
484-
485434
if (fixup & FIXUP_PSW_NORMAL)
486435
ip += (unsigned long) p->addr - (unsigned long) p->ainsn.insn;
487436

‎arch/s390/kernel/mcount.S‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ ENTRY(ftrace_caller)
4242
.globl ftrace_regs_caller
4343
.set ftrace_regs_caller,ftrace_caller
4444
stg %r14,(__SF_GPRS+8*8)(%r15) # save traced function caller
45+
lghi %r14,0 # save condition code
46+
ipm %r14 # don't put any instructions
47+
sllg %r14,%r14,16 # clobbering CC before this point
4548
lgr %r1,%r15
4649
#if !(defined(CC_USING_HOTPATCH) || defined(CC_USING_NOP_MCOUNT))
4750
aghi %r0,MCOUNT_RETURN_FIXUP
@@ -54,6 +57,9 @@ ENTRY(ftrace_caller)
5457
# allocate pt_regs and stack frame for ftrace_trace_function
5558
aghi %r15,-STACK_FRAME_SIZE
5659
stg %r1,(STACK_PTREGS_GPRS+15*8)(%r15)
60+
stg %r14,(STACK_PTREGS_PSW)(%r15)
61+
lg %r14,(__SF_GPRS+8*8)(%r1) # restore original return address
62+
stosm (STACK_PTREGS_PSW)(%r15),0
5763
aghi %r1,-TRACED_FUNC_FRAME_SIZE
5864
stg %r1,__SF_BACKCHAIN(%r15)
5965
stg %r0,(STACK_PTREGS_PSW+8)(%r15)

0 commit comments

Comments
 (0)