Skip to content

Commit 8c9f494

Browse files
xhackerustcpalmer-dabbelt
authored andcommitted
riscv: kprobes: Remove redundant kprobe_step_ctx
Inspired by commit ba090f9 ("arm64: kprobes: Remove redundant kprobe_step_ctx"), the ss_pending and match_addr of kprobe_step_ctx are redundant because those can be replaced by KPROBE_HIT_SS and &cur_kprobe->ainsn.api.insn[0] + GET_INSN_LENGTH(cur->opcode) respectively. Remove the kprobe_step_ctx to simplify the code. Signed-off-by: Jisheng Zhang <[email protected]> Reviewed-by: Masami Hiramatsu <[email protected]> Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 37a7a2a commit 8c9f494

File tree

2 files changed

+9
-38
lines changed

2 files changed

+9
-38
lines changed

arch/riscv/include/asm/kprobes.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,11 @@ struct prev_kprobe {
2929
unsigned int status;
3030
};
3131

32-
/* Single step context for kprobe */
33-
struct kprobe_step_ctx {
34-
unsigned long ss_pending;
35-
unsigned long match_addr;
36-
};
37-
3832
/* per-cpu kprobe control block */
3933
struct kprobe_ctlblk {
4034
unsigned int kprobe_status;
4135
unsigned long saved_status;
4236
struct prev_kprobe prev_kprobe;
43-
struct kprobe_step_ctx ss_ctx;
4437
};
4538

4639
void arch_remove_kprobe(struct kprobe *p);

arch/riscv/kernel/probes/kprobes.c

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
1717
DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
1818

1919
static void __kprobes
20-
post_kprobe_handler(struct kprobe_ctlblk *, struct pt_regs *);
20+
post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
2121

2222
static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
2323
{
@@ -43,7 +43,7 @@ static void __kprobes arch_simulate_insn(struct kprobe *p, struct pt_regs *regs)
4343
p->ainsn.api.handler((u32)p->opcode,
4444
(unsigned long)p->addr, regs);
4545

46-
post_kprobe_handler(kcb, regs);
46+
post_kprobe_handler(p, kcb, regs);
4747
}
4848

4949
int __kprobes arch_prepare_kprobe(struct kprobe *p)
@@ -149,21 +149,6 @@ static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
149149
regs->status = kcb->saved_status;
150150
}
151151

152-
static void __kprobes
153-
set_ss_context(struct kprobe_ctlblk *kcb, unsigned long addr, struct kprobe *p)
154-
{
155-
unsigned long offset = GET_INSN_LENGTH(p->opcode);
156-
157-
kcb->ss_ctx.ss_pending = true;
158-
kcb->ss_ctx.match_addr = addr + offset;
159-
}
160-
161-
static void __kprobes clear_ss_context(struct kprobe_ctlblk *kcb)
162-
{
163-
kcb->ss_ctx.ss_pending = false;
164-
kcb->ss_ctx.match_addr = 0;
165-
}
166-
167152
static void __kprobes setup_singlestep(struct kprobe *p,
168153
struct pt_regs *regs,
169154
struct kprobe_ctlblk *kcb, int reenter)
@@ -182,8 +167,6 @@ static void __kprobes setup_singlestep(struct kprobe *p,
182167
/* prepare for single stepping */
183168
slot = (unsigned long)p->ainsn.api.insn;
184169

185-
set_ss_context(kcb, slot, p); /* mark pending ss */
186-
187170
/* IRQs and single stepping do not mix well. */
188171
kprobes_save_local_irqflag(kcb, regs);
189172

@@ -219,13 +202,8 @@ static int __kprobes reenter_kprobe(struct kprobe *p,
219202
}
220203

221204
static void __kprobes
222-
post_kprobe_handler(struct kprobe_ctlblk *kcb, struct pt_regs *regs)
205+
post_kprobe_handler(struct kprobe *cur, struct kprobe_ctlblk *kcb, struct pt_regs *regs)
223206
{
224-
struct kprobe *cur = kprobe_running();
225-
226-
if (!cur)
227-
return;
228-
229207
/* return addr restore if non-branching insn */
230208
if (cur->ainsn.api.restore != 0)
231209
regs->epc = cur->ainsn.api.restore;
@@ -357,16 +335,16 @@ bool __kprobes
357335
kprobe_single_step_handler(struct pt_regs *regs)
358336
{
359337
struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
338+
unsigned long addr = instruction_pointer(regs);
339+
struct kprobe *cur = kprobe_running();
360340

361-
if ((kcb->ss_ctx.ss_pending)
362-
&& (kcb->ss_ctx.match_addr == instruction_pointer(regs))) {
363-
clear_ss_context(kcb); /* clear pending ss */
364-
341+
if (cur && (kcb->kprobe_status & (KPROBE_HIT_SS | KPROBE_REENTER)) &&
342+
((unsigned long)&cur->ainsn.api.insn[0] + GET_INSN_LENGTH(cur->opcode) == addr)) {
365343
kprobes_restore_local_irqflag(kcb, regs);
366-
367-
post_kprobe_handler(kcb, regs);
344+
post_kprobe_handler(cur, kcb, regs);
368345
return true;
369346
}
347+
/* not ours, kprobes should ignore it */
370348
return false;
371349
}
372350

0 commit comments

Comments
 (0)