Skip to contentSkip to conversation

MIPS: Implement two workarounds for BPF JIT

PATCH Applied Jiaxun Yang 4 participants 6 messages Feb 28, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover MIPS: Implement two workarounds for BPF JIT

Hi all,

Just noticed eBPF JIT is not working on R4000 when messing around with QEMU.

This patchset implements two workarounds that is blocking us from enabling eBPF JIT on R4000.

Thanks.
- Jiaxun

Jiaxun Yang (2):
MIPS: ebpf jit: Implement DADDI workarounds
MIPS: ebpf jit: Implement R4000 workarounds

 arch/mips/Kconfig              | 5 +----
 arch/mips/net/bpf_jit_comp.c   | 4 ++++
 arch/mips/net/bpf_jit_comp64.c | 3 +++
 3 files changed, 8 insertions(+), 4 deletions(-)
Signature · 1 line
2.37.1 (Apple Git-137.1)
#1 lore raw

PATCH v2 1/2 MIPS: ebpf jit: Implement DADDI workarounds

For DADDI errata we just workaround by disable immediate operation for BPF_ADD / BPF_SUB to avoid generation of DADDIU.

All other use cases in JIT won't cause overflow thus they are all safe.

---
v2: Drop 64BIT ifdef

---
 arch/mips/Kconfig            | 1 -
 arch/mips/net/bpf_jit_comp.c | 4 ++++
 2 files changed, 4 insertions(+), 1 deletion(-)
arch/mips/Kconfig +0 -1
index 37072e15b263..df0910e3895c 100644
@@ -64,7 +64,6 @@ config MIPS
 	select HAVE_DMA_CONTIGUOUS
 	select HAVE_DYNAMIC_FTRACE
 	select HAVE_EBPF_JIT if !CPU_MICROMIPS && \
-				!CPU_DADDI_WORKAROUNDS && \
 				!CPU_R4000_WORKAROUNDS && \
 				!CPU_R4400_WORKAROUNDS
 	select HAVE_EXIT_THREAD
arch/mips/net/bpf_jit_comp.c +4 -0
index b17130d510d4..a40d926b6513 100644
@@ -218,9 +218,13 @@ bool valid_alu_i(u8 op, s32 imm)
 		/* All legal eBPF values are valid */
 		return true;
 	case BPF_ADD:
+		if (IS_ENABLED(CONFIG_CPU_DADDI_WORKAROUNDS))
+			return false;
 		/* imm must be 16 bits */
 		return imm >= -0x8000 && imm <= 0x7fff;
 	case BPF_SUB:
+		if (IS_ENABLED(CONFIG_CPU_DADDI_WORKAROUNDS))
+			return false;
 		/* -imm must be 16 bits */
 		return imm >= -0x7fff && imm <= 0x8000;
 	case BPF_AND:
Signature · 1 line
2.37.1 (Apple Git-137.1)
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] MIPS: ebpf jit: Implement DADDI workarounds

Quoted 5 lines · Jiaxun Yang

For DADDI errata we just workaround by disable immediate operation for BPF_ADD / BPF_SUB to avoid generation of DADDIU.

All other use cases in JIT won't cause overflow thus they are all safe.

Quoted 7 lines

---
v2: Drop 64BIT ifdef

---
  arch/mips/Kconfig            | 1 -
  arch/mips/net/bpf_jit_comp.c | 4 ++++
  2 files changed, 4 insertions(+), 1 deletion(-)
maintainer Acked-by on v2 1/2
#3 lore raw

Re: [PATCH v2 1/2] MIPS: ebpf jit: Implement DADDI workarounds

In reply to Jiaxun Yang
Quoted 47 lines · Jiaxun Yang

For DADDI errata we just workaround by disable immediate operation for BPF_ADD / BPF_SUB to avoid generation of DADDIU.

All other use cases in JIT won't cause overflow thus they are all safe.

---
v2: Drop 64BIT ifdef

---
 arch/mips/Kconfig            | 1 -
 arch/mips/net/bpf_jit_comp.c | 4 ++++
 2 files changed, 4 insertions(+), 1 deletion(-)
arch/mips/Kconfig +0 -1
index 37072e15b263..df0910e3895c 100644
@@ -64,7 +64,6 @@ config MIPS
        select HAVE_DMA_CONTIGUOUS
        select HAVE_DYNAMIC_FTRACE
        select HAVE_EBPF_JIT if !CPU_MICROMIPS && \
-                               !CPU_DADDI_WORKAROUNDS && \
                                !CPU_R4000_WORKAROUNDS && \
                                !CPU_R4400_WORKAROUNDS
        select HAVE_EXIT_THREAD
arch/mips/net/bpf_jit_comp.c +4 -1
index b17130d510d4..a40d926b6513 100644
@@ -218,9 +218,13 @@ bool valid_alu_i(u8 op, s32 imm)
                /* All legal eBPF values are valid */
                return true;
        case BPF_ADD:
+               if (IS_ENABLED(CONFIG_CPU_DADDI_WORKAROUNDS))
+                       return false;
                /* imm must be 16 bits */
                return imm >= -0x8000 && imm <= 0x7fff;
        case BPF_SUB:
+               if (IS_ENABLED(CONFIG_CPU_DADDI_WORKAROUNDS))
+                       return false;
                /* -imm must be 16 bits */
                return imm >= -0x7fff && imm <= 0x8000;
        case BPF_AND:
--

2.37.1 (Apple Git-137.1)

#4 lore raw

PATCH v2 2/2 MIPS: ebpf jit: Implement R4000 workarounds

In reply to Jiaxun Yang

For R4000 erratas around multiplication and division instructions, as our use of those instructions are always followed by mflo/mfhi instructions, the only issue we need care is

"MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0" Errata 28:
"A double-word or a variable shift may give an incorrect result if executed while an integer multiplication is in progress."

We just emit a mfhi $0 to ensure the operation is completed after every multiplication instruction according to workaround suggestion in the document.

---
v2: Drop 32bit part

---
 arch/mips/Kconfig              | 4 +---
 arch/mips/net/bpf_jit_comp64.c | 3 +++
 2 files changed, 4 insertions(+), 3 deletions(-)
arch/mips/Kconfig +1 -3
index df0910e3895c..5ea07c833c5b 100644
@@ -63,9 +63,7 @@ config MIPS
 	select HAVE_DEBUG_STACKOVERFLOW
 	select HAVE_DMA_CONTIGUOUS
 	select HAVE_DYNAMIC_FTRACE
-	select HAVE_EBPF_JIT if !CPU_MICROMIPS && \
-				!CPU_R4000_WORKAROUNDS && \
-				!CPU_R4400_WORKAROUNDS
+	select HAVE_EBPF_JIT if !CPU_MICROMIPS
 	select HAVE_EXIT_THREAD
 	select HAVE_FAST_GUP
 	select HAVE_FTRACE_MCOUNT_RECORD
arch/mips/net/bpf_jit_comp64.c +3 -0
index 0e7c1bdcf914..fa7e9aa37f49 100644
@@ -228,6 +228,9 @@ static void emit_alu_r64(struct jit_context *ctx, u8 dst, u8 src, u8 op)
 		} else {
 			emit(ctx, dmultu, dst, src);
 			emit(ctx, mflo, dst);
+			/* Ensure multiplication is completed */
+			if (IS_ENABLED(CONFIG_CPU_R4000_WORKAROUNDS))
+				emit(ctx, mfhi, MIPS_R_ZERO);
 		}
 		break;
 	/* dst = dst / src */
Signature · 1 line
2.37.1 (Apple Git-137.1)
#5 lore raw
In reply to Jiaxun Yang
This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Quoted 8 lines · On Tue, 28 Feb 2023 11:33:03 +0000 you wrote:

Hi all,

Just noticed eBPF JIT is not working on R4000 when messing around with QEMU.

This patchset implements two workarounds that is blocking us from enabling eBPF JIT on R4000.

[...]

Here is the summary with links:
- [v2,1/2] MIPS: ebpf jit: Implement DADDI workarounds

    https://git.kernel.org/bpf/bpf-next/c/bbefef2f0708
  - [v2,2/2] MIPS: ebpf jit: Implement R4000 workarounds
    https://git.kernel.org/bpf/bpf-next/c/7364d60c2661

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html