riscv: Extend patch_text for multiple instructions
authorPu Lehui <pulehui@huawei.com>
Wed, 15 Feb 2023 13:52:02 +0000 (21:52 +0800)
committerDaniel Borkmann <daniel@iogearbox.net>
Fri, 17 Feb 2023 20:45:30 +0000 (21:45 +0100)
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
arch/riscv/include/asm/patch.h
arch/riscv/kernel/patch.c
arch/riscv/kernel/probes/kprobes.c

index 9a7d7346001ee249678a1a5c28f1de09a758d2ab..f433121774c0190628b0a2f071059c92d782deb7 100644 (file)
@@ -7,6 +7,6 @@
 #define _ASM_RISCV_PATCH_H
 
 int patch_text_nosync(void *addr, const void *insns, size_t len);
-int patch_text(void *addr, u32 insn);
+int patch_text(void *addr, u32 *insns, int ninsns);
 
 #endif /* _ASM_RISCV_PATCH_H */
index 765004b605132abebbc3fbe9a5819d2ce14e81d4..8086d1a281cd32c52fb126beb5a514732618a6e2 100644 (file)
@@ -15,7 +15,8 @@
 
 struct patch_insn {
        void *addr;
-       u32 insn;
+       u32 *insns;
+       int ninsns;
        atomic_t cpu_count;
 };
 
@@ -102,12 +103,15 @@ NOKPROBE_SYMBOL(patch_text_nosync);
 static int patch_text_cb(void *data)
 {
        struct patch_insn *patch = data;
-       int ret = 0;
+       unsigned long len;
+       int i, ret = 0;
 
        if (atomic_inc_return(&patch->cpu_count) == num_online_cpus()) {
-               ret =
-                   patch_text_nosync(patch->addr, &patch->insn,
-                                           GET_INSN_LENGTH(patch->insn));
+               for (i = 0; ret == 0 && i < patch->ninsns; i++) {
+                       len = GET_INSN_LENGTH(patch->insns[i]);
+                       ret = patch_text_nosync(patch->addr + i * len,
+                                               &patch->insns[i], len);
+               }
                atomic_inc(&patch->cpu_count);
        } else {
                while (atomic_read(&patch->cpu_count) <= num_online_cpus())
@@ -119,11 +123,12 @@ static int patch_text_cb(void *data)
 }
 NOKPROBE_SYMBOL(patch_text_cb);
 
-int patch_text(void *addr, u32 insn)
+int patch_text(void *addr, u32 *insns, int ninsns)
 {
        struct patch_insn patch = {
                .addr = addr,
-               .insn = insn,
+               .insns = insns,
+               .ninsns = ninsns,
                .cpu_count = ATOMIC_INIT(0),
        };
 
index 41c7481afde368ce2bdc75475aa52345b79ad980..ef6d6e702485f80c24ccbf7e1b75e63dd9ef4e0f 100644 (file)
@@ -23,13 +23,14 @@ post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
 
 static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
 {
+       u32 insn = __BUG_INSN_32;
        unsigned long offset = GET_INSN_LENGTH(p->opcode);
 
        p->ainsn.api.restore = (unsigned long)p->addr + offset;
 
-       patch_text(p->ainsn.api.insn, p->opcode);
+       patch_text(p->ainsn.api.insn, &p->opcode, 1);
        patch_text((void *)((unsigned long)(p->ainsn.api.insn) + offset),
-                  __BUG_INSN_32);
+                  &insn, 1);
 }
 
 static void __kprobes arch_prepare_simulate(struct kprobe *p)
@@ -114,16 +115,16 @@ void *alloc_insn_page(void)
 /* install breakpoint in text */
 void __kprobes arch_arm_kprobe(struct kprobe *p)
 {
-       if ((p->opcode & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
-               patch_text(p->addr, __BUG_INSN_32);
-       else
-               patch_text(p->addr, __BUG_INSN_16);
+       u32 insn = (p->opcode & __INSN_LENGTH_MASK) == __INSN_LENGTH_32 ?
+                  __BUG_INSN_32 : __BUG_INSN_16;
+
+       patch_text(p->addr, &insn, 1);
 }
 
 /* remove breakpoint from text */
 void __kprobes arch_disarm_kprobe(struct kprobe *p)
 {
-       patch_text(p->addr, p->opcode);
+       patch_text(p->addr, &p->opcode, 1);
 }
 
 void __kprobes arch_remove_kprobe(struct kprobe *p)