Skip to content

Commit 5e57fb7

Browse files
Pu Lehuiborkmann
authored andcommitted
riscv: Extend patch_text for multiple instructions
Extend patch_text for multiple instructions. This is the preparaiton for multiple instructions text patching in riscv BPF trampoline, and may be useful for other scenario. Signed-off-by: Pu Lehui <pulehui@huawei.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Tested-by: Björn Töpel <bjorn@rivosinc.com> Reviewed-by: Conor Dooley <conor.dooley@microchip.com> Acked-by: Björn Töpel <bjorn@rivosinc.com> Link: https://lore.kernel.org/bpf/20230215135205.1411105-2-pulehui@huaweicloud.com
1 parent 181127f commit 5e57fb7

3 files changed

Lines changed: 21 additions & 15 deletions

File tree

arch/riscv/include/asm/patch.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
#define _ASM_RISCV_PATCH_H
88

99
int patch_text_nosync(void *addr, const void *insns, size_t len);
10-
int patch_text(void *addr, u32 insn);
10+
int patch_text(void *addr, u32 *insns, int ninsns);
1111

1212
#endif /* _ASM_RISCV_PATCH_H */

arch/riscv/kernel/patch.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
struct patch_insn {
1717
void *addr;
18-
u32 insn;
18+
u32 *insns;
19+
int ninsns;
1920
atomic_t cpu_count;
2021
};
2122

@@ -102,12 +103,15 @@ NOKPROBE_SYMBOL(patch_text_nosync);
102103
static int patch_text_cb(void *data)
103104
{
104105
struct patch_insn *patch = data;
105-
int ret = 0;
106+
unsigned long len;
107+
int i, ret = 0;
106108

107109
if (atomic_inc_return(&patch->cpu_count) == num_online_cpus()) {
108-
ret =
109-
patch_text_nosync(patch->addr, &patch->insn,
110-
GET_INSN_LENGTH(patch->insn));
110+
for (i = 0; ret == 0 && i < patch->ninsns; i++) {
111+
len = GET_INSN_LENGTH(patch->insns[i]);
112+
ret = patch_text_nosync(patch->addr + i * len,
113+
&patch->insns[i], len);
114+
}
111115
atomic_inc(&patch->cpu_count);
112116
} else {
113117
while (atomic_read(&patch->cpu_count) <= num_online_cpus())
@@ -119,11 +123,12 @@ static int patch_text_cb(void *data)
119123
}
120124
NOKPROBE_SYMBOL(patch_text_cb);
121125

122-
int patch_text(void *addr, u32 insn)
126+
int patch_text(void *addr, u32 *insns, int ninsns)
123127
{
124128
struct patch_insn patch = {
125129
.addr = addr,
126-
.insn = insn,
130+
.insns = insns,
131+
.ninsns = ninsns,
127132
.cpu_count = ATOMIC_INIT(0),
128133
};
129134

arch/riscv/kernel/probes/kprobes.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
2323

2424
static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
2525
{
26+
u32 insn = __BUG_INSN_32;
2627
unsigned long offset = GET_INSN_LENGTH(p->opcode);
2728

2829
p->ainsn.api.restore = (unsigned long)p->addr + offset;
2930

30-
patch_text(p->ainsn.api.insn, p->opcode);
31+
patch_text(p->ainsn.api.insn, &p->opcode, 1);
3132
patch_text((void *)((unsigned long)(p->ainsn.api.insn) + offset),
32-
__BUG_INSN_32);
33+
&insn, 1);
3334
}
3435

3536
static void __kprobes arch_prepare_simulate(struct kprobe *p)
@@ -114,16 +115,16 @@ void *alloc_insn_page(void)
114115
/* install breakpoint in text */
115116
void __kprobes arch_arm_kprobe(struct kprobe *p)
116117
{
117-
if ((p->opcode & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
118-
patch_text(p->addr, __BUG_INSN_32);
119-
else
120-
patch_text(p->addr, __BUG_INSN_16);
118+
u32 insn = (p->opcode & __INSN_LENGTH_MASK) == __INSN_LENGTH_32 ?
119+
__BUG_INSN_32 : __BUG_INSN_16;
120+
121+
patch_text(p->addr, &insn, 1);
121122
}
122123

123124
/* remove breakpoint from text */
124125
void __kprobes arch_disarm_kprobe(struct kprobe *p)
125126
{
126-
patch_text(p->addr, p->opcode);
127+
patch_text(p->addr, &p->opcode, 1);
127128
}
128129

129130
void __kprobes arch_remove_kprobe(struct kprobe *p)

0 commit comments

Comments
 (0)