]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
riscv: Pass patch_text() the length in bytes
authorSamuel Holland <samuel.holland@sifive.com>
Wed, 27 Mar 2024 16:04:44 +0000 (09:04 -0700)
committerPalmer Dabbelt <palmer@rivosinc.com>
Wed, 26 Jun 2024 14:36:31 +0000 (07:36 -0700)
patch_text_nosync() already handles an arbitrary length of code, so this
removes a superfluous loop and reduces the number of icache flushes.

Reviewed-by: Björn Töpel <bjorn@rivosinc.com>
Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Conor Dooley <conor.dooley@microchip.com>
Link: https://lore.kernel.org/r/20240327160520.791322-6-samuel.holland@sifive.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
arch/riscv/include/asm/patch.h
arch/riscv/kernel/patch.c
arch/riscv/kernel/probes/kprobes.c
arch/riscv/net/bpf_jit_comp64.c

index 9f5d6e14c40553ea322958603853e89b114b158a..7228e266b9a1aeebd302cb6b315af13a6ba47f20 100644 (file)
@@ -9,7 +9,7 @@
 int patch_insn_write(void *addr, const void *insn, size_t len);
 int patch_text_nosync(void *addr, const void *insns, size_t len);
 int patch_text_set_nosync(void *addr, u8 c, size_t len);
-int patch_text(void *addr, u32 *insns, int ninsns);
+int patch_text(void *addr, u32 *insns, size_t len);
 
 extern int riscv_patch_in_stop_machine;
 
index cd6159411f519de7d7848c614162b1dedda1ae37..99c5c0385305beb43eb8d33865e543f66cc77922 100644 (file)
@@ -19,7 +19,7 @@
 struct patch_insn {
        void *addr;
        u32 *insns;
-       int ninsns;
+       size_t len;
        atomic_t cpu_count;
 };
 
@@ -239,14 +239,10 @@ NOKPROBE_SYMBOL(patch_text_nosync);
 static int patch_text_cb(void *data)
 {
        struct patch_insn *patch = data;
-       unsigned long len;
-       int i, ret = 0;
+       int ret = 0;
 
        if (atomic_inc_return(&patch->cpu_count) == num_online_cpus()) {
-               for (i = 0; ret == 0 && i < patch->ninsns; i++) {
-                       len = GET_INSN_LENGTH(patch->insns[i]);
-                       ret = patch_insn_write(patch->addr + i * len, &patch->insns[i], len);
-               }
+               ret = patch_insn_write(patch->addr, patch->insns, patch->len);
                /*
                 * Make sure the patching store is effective *before* we
                 * increment the counter which releases all waiting CPUs
@@ -266,13 +262,13 @@ static int patch_text_cb(void *data)
 }
 NOKPROBE_SYMBOL(patch_text_cb);
 
-int patch_text(void *addr, u32 *insns, int ninsns)
+int patch_text(void *addr, u32 *insns, size_t len)
 {
        int ret;
        struct patch_insn patch = {
                .addr = addr,
                .insns = insns,
-               .ninsns = ninsns,
+               .len = len,
                .cpu_count = ATOMIC_INIT(0),
        };
 
index 03cd103b8449483fd9a325b8b627196d7173089c..474a6521365783c76f206c2771df21c63f1edbf7 100644 (file)
@@ -24,13 +24,13 @@ post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
 
 static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
 {
+       size_t len = GET_INSN_LENGTH(p->opcode);
        u32 insn = __BUG_INSN_32;
-       unsigned long offset = GET_INSN_LENGTH(p->opcode);
 
-       p->ainsn.api.restore = (unsigned long)p->addr + offset;
+       p->ainsn.api.restore = (unsigned long)p->addr + len;
 
-       patch_text_nosync(p->ainsn.api.insn, &p->opcode, 1);
-       patch_text_nosync(p->ainsn.api.insn + offset, &insn, 1);
+       patch_text_nosync(p->ainsn.api.insn, &p->opcode, len);
+       patch_text_nosync(p->ainsn.api.insn + len, &insn, GET_INSN_LENGTH(insn));
 }
 
 static void __kprobes arch_prepare_simulate(struct kprobe *p)
@@ -107,16 +107,18 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
 /* install breakpoint in text */
 void __kprobes arch_arm_kprobe(struct kprobe *p)
 {
-       u32 insn = (p->opcode & __INSN_LENGTH_MASK) == __INSN_LENGTH_32 ?
-                  __BUG_INSN_32 : __BUG_INSN_16;
+       size_t len = GET_INSN_LENGTH(p->opcode);
+       u32 insn = len == 4 ? __BUG_INSN_32 : __BUG_INSN_16;
 
-       patch_text(p->addr, &insn, 1);
+       patch_text(p->addr, &insn, len);
 }
 
 /* remove breakpoint from text */
 void __kprobes arch_disarm_kprobe(struct kprobe *p)
 {
-       patch_text(p->addr, &p->opcode, 1);
+       size_t len = GET_INSN_LENGTH(p->opcode);
+
+       patch_text(p->addr, &p->opcode, len);
 }
 
 void __kprobes arch_remove_kprobe(struct kprobe *p)
index 79a001d5533ea6b728661884c3da0aecb9139eb3..a01b312913bcd40e3f01ac69630c6ab20d6a61ae 100644 (file)
@@ -16,6 +16,7 @@
 #include "bpf_jit.h"
 
 #define RV_FENTRY_NINSNS 2
+#define RV_FENTRY_NBYTES (RV_FENTRY_NINSNS * 4)
 
 #define RV_REG_TCC RV_REG_A6
 #define RV_REG_TCC_SAVED RV_REG_S6 /* Store A6 in S6 if program do calls */
@@ -672,7 +673,7 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type poke_type,
        if (ret)
                return ret;
 
-       if (memcmp(ip, old_insns, RV_FENTRY_NINSNS * 4))
+       if (memcmp(ip, old_insns, RV_FENTRY_NBYTES))
                return -EFAULT;
 
        ret = gen_jump_or_nops(new_addr, ip, new_insns, is_call);
@@ -681,8 +682,8 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type poke_type,
 
        cpus_read_lock();
        mutex_lock(&text_mutex);
-       if (memcmp(ip, new_insns, RV_FENTRY_NINSNS * 4))
-               ret = patch_text(ip, new_insns, RV_FENTRY_NINSNS);
+       if (memcmp(ip, new_insns, RV_FENTRY_NBYTES))
+               ret = patch_text(ip, new_insns, RV_FENTRY_NBYTES);
        mutex_unlock(&text_mutex);
        cpus_read_unlock();