Skip to contentSkip to conversation

Misc MIPS/BPF fixes for 3.16

PATCH In review Markos Chandras 5 participants 31 messages Jun 23, 2014 – Jun 25, 2014

Subsystems (5)
Networking Network drivers MIPS BPF +1 more subsystems
lore
Share & tools
lore
#0 lore raw

cover Misc MIPS/BPF fixes for 3.16

Hi,

Here are some fixes for MIPS/BPF for 3.16. These fixes make the bpf testsuite *almost* happy with only 2 tests (LD_IND_LL, LD_IND_NET) failing at the moment. Since fixing the remaining tests is not so trivial, it would be nice to have these fixes in 3.16 for now.

The patches are based on the upstream-sfr/mips-for-linux-next tree because they depend on https://patchwork.linux-mips.org/patch/7099/

Markos Chandras (17):
MIPS: uasm: Add s3s1s2 instruction builder
MIPS: uasm: Add slt uasm instruction
MIPS: mm: uasm: Fix lh micro-assembler instruction
MIPS: bpf: Use the LO register to get division's quotient
MIPS: bpf: Return error code if the offset is a negative number
MIPS: bpf: Use 'andi' instead of 'and' for the VLAN cases
MIPS: bpf: Add SEEN_SKB to flags when looking for the PKT_TYPE
MIPS: bpf: Fix branch conditional for BPF_J{GT/GE} cases
MIPS: bpf: Use correct mask for VLAN_TAG case
MIPS: bpf: Fix return values for VLAN_TAG_PRESENT case
MIPS: bpf: Use pr_debug instead of pr_warn for unhandled opcodes
MIPS: bpf: Fix is_range() semantics
MIPS: bpf: Drop update_on_xread and always initialize the X register
MIPS: bpf: Prevent kernel fall over for >=32bit shifts
MIPS: bpf: Fix PKT_TYPE case for big-endian cores
MIPS: bpf: Use 32 or 64-bit load instruction to load an address to
register
MIPS: bpf: Fix stack space allocation for BPF memwords on MIPS64

 arch/mips/include/asm/uasm.h      |   4 ++
 arch/mips/include/uapi/asm/inst.h |   1 +
 arch/mips/mm/uasm-micromips.c     |   1 +
 arch/mips/mm/uasm-mips.c          |   3 +-
 arch/mips/mm/uasm.c               |  10 +++-
 arch/mips/net/bpf_jit.c           | 115 ++++++++++++++++++++++++--------------
 6 files changed, 90 insertions(+), 44 deletions(-)
Signature · 1 line
2.0.0
#1 lore raw

PATCH 1/17 MIPS: uasm: Add s3s1s2 instruction builder

It will be used later on by the slt instruction.

---
 arch/mips/include/asm/uasm.h | 3 +++
 arch/mips/mm/uasm.c          | 7 +++++++
 2 files changed, 10 insertions(+)
arch/mips/include/asm/uasm.h +3 -0
index f8d63b3b40b4..43259b3fca6d 100644
@@ -67,6 +67,9 @@ void ISAOPC(op)(u32 **buf, unsigned int a, unsigned int b, signed int c)
 #define Ip_u2s3u1(op)							\
 void ISAOPC(op)(u32 **buf, unsigned int a, signed int b, unsigned int c)
 
+#define Ip_s3s1s2(op)							\
+void ISAOPC(op)(u32 **buf, int a, int b, int c)
+
 #define Ip_u2u1s3(op)							\
 void ISAOPC(op)(u32 **buf, unsigned int a, unsigned int b, signed int c)
 
arch/mips/mm/uasm.c +7 -0
index 00515805fe41..1e3e10919423 100644
@@ -139,6 +139,13 @@ Ip_u1u2u3(op)						\
 }							\
 UASM_EXPORT_SYMBOL(uasm_i##op);
 
+#define I_s3s1s2(op)					\
+Ip_s3s1s2(op)						\
+{							\
+	build_insn(buf, insn##op, b, c, a);		\
+}							\
+UASM_EXPORT_SYMBOL(uasm_i##op);
+
 #define I_u2u1u3(op)					\
 Ip_u2u1u3(op)						\
 {							\
Signature · 1 line
2.0.0
#4 lore raw

PATCH 4/17 MIPS: bpf: Use the LO register to get division's quotient

In reply to Markos Chandras

Reading from the HI register to get the division result is wrong. The quotient is placed in the LO register.

Cc "David S. Miller" <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Alexei Starovoitov <[email protected]>
---
 arch/mips/net/bpf_jit.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/mips/net/bpf_jit.c +1 -1
index f7c206404989..5cc92c4590cb 100644
@@ -408,7 +408,7 @@ static inline void emit_div(unsigned int dst, unsigned int src,
 		u32 *p = &ctx->target[ctx->idx];
 		uasm_i_divu(&p, dst, src);
 		p = &ctx->target[ctx->idx + 1];
-		uasm_i_mfhi(&p, dst);
+		uasm_i_mflo(&p, dst);
 	}
 	ctx->idx += 2; /* 2 insts */
 }
Signature · 1 line
2.0.0
#2 lore raw

PATCH 2/17 MIPS: uasm: Add slt uasm instruction

In reply to Markos Chandras

It will be used later on by bpf-jit

---
 arch/mips/include/asm/uasm.h      | 1 +
 arch/mips/include/uapi/asm/inst.h | 1 +
 arch/mips/mm/uasm-micromips.c     | 1 +
 arch/mips/mm/uasm-mips.c          | 1 +
 arch/mips/mm/uasm.c               | 3 ++-
 5 files changed, 6 insertions(+), 1 deletion(-)
arch/mips/include/asm/uasm.h +1 -0
index 43259b3fca6d..708c5d414905 100644
@@ -150,6 +150,7 @@ Ip_u2s3u1(_scd);
 Ip_u2s3u1(_sd);
 Ip_u2u1u3(_sll);
 Ip_u3u2u1(_sllv);
+Ip_s3s1s2(_slt);
 Ip_u2u1s3(_sltiu);
 Ip_u3u1u2(_sltu);
 Ip_u2u1u3(_sra);
arch/mips/include/uapi/asm/inst.h +1 -0
index 4b7160259292..4bfdb9d4c186 100644
@@ -273,6 +273,7 @@ enum mm_32a_minor_op {
 	mm_and_op = 0x250,
 	mm_or32_op = 0x290,
 	mm_xor32_op = 0x310,
+	mm_slt_op = 0x350,
 	mm_sltu_op = 0x390,
 };
 
arch/mips/mm/uasm-micromips.c +1 -0
index 775c2800cba2..8399ddf03a02 100644
@@ -102,6 +102,7 @@ static struct insn insn_table_MM[] = {
 	{ insn_sd, 0, 0 },
 	{ insn_sll, M(mm_pool32a_op, 0, 0, 0, 0, mm_sll32_op), RT | RS | RD },
 	{ insn_sllv, M(mm_pool32a_op, 0, 0, 0, 0, mm_sllv32_op), RT | RS | RD },
+	{ insn_slt, M(mm_pool32a_op, 0, 0, 0, 0, mm_slt_op), RT | RS | RD },
 	{ insn_sltiu, M(mm_sltiu32_op, 0, 0, 0, 0, 0), RT | RS | SIMM },
 	{ insn_sltu, M(mm_pool32a_op, 0, 0, 0, 0, mm_sltu_op), RT | RS | RD },
 	{ insn_sra, M(mm_pool32a_op, 0, 0, 0, 0, mm_sra_op), RT | RS | RD },
arch/mips/mm/uasm-mips.c +1 -0
index 38792c2364f5..4535a9d19ea5 100644
@@ -110,6 +110,7 @@ static struct insn insn_table[] = {
 	{ insn_sd,  M(sd_op, 0, 0, 0, 0, 0),  RS | RT | SIMM },
 	{ insn_sll,  M(spec_op, 0, 0, 0, 0, sll_op),  RT | RD | RE },
 	{ insn_sllv,  M(spec_op, 0, 0, 0, 0, sllv_op),  RS | RT | RD },
+	{ insn_slt,  M(spec_op, 0, 0, 0, 0, slt_op),  RS | RT | RD },
 	{ insn_sltiu, M(sltiu_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
 	{ insn_sltu, M(spec_op, 0, 0, 0, 0, sltu_op), RS | RT | RD },
 	{ insn_sra,  M(spec_op, 0, 0, 0, 0, sra_op),  RT | RD | RE },
arch/mips/mm/uasm.c +2 -1
index 1e3e10919423..a01b0d6cedd2 100644
@@ -53,7 +53,7 @@ enum opcode {
 	insn_ld, insn_ldx, insn_lh, insn_ll, insn_lld, insn_lui, insn_lw,
 	insn_lwx, insn_mfc0, insn_mfhi, insn_mflo, insn_mtc0, insn_mul,
 	insn_or, insn_ori, insn_pref, insn_rfe, insn_rotr, insn_sc, insn_scd,
-	insn_sd, insn_sll, insn_sllv, insn_sltiu, insn_sltu, insn_sra,
+	insn_sd, insn_sll, insn_sllv, insn_slt, insn_sltiu, insn_sltu, insn_sra,
 	insn_srl, insn_srlv, insn_subu, insn_sw, insn_sync, insn_syscall,
 	insn_tlbp, insn_tlbr, insn_tlbwi, insn_tlbwr, insn_wait, insn_wsbh,
 	insn_xor, insn_xori, insn_yield,
@@ -296,6 +296,7 @@ I_u2s3u1(_scd)
 I_u2s3u1(_sd)
 I_u2u1u3(_sll)
 I_u3u2u1(_sllv)
+I_s3s1s2(_slt)
 I_u2u1s3(_sltiu)
 I_u3u1u2(_sltu)
 I_u2u1u3(_sra)
Signature · 1 line
2.0.0
#5 lore raw

PATCH 5/17 MIPS: bpf: Return error code if the offset is a negative number

In reply to Markos Chandras

Previously, the negative offset was not checked leading to failures due to trying to load data beyond the skb struct boundaries. Until we have proper asm helpers in place, it's best if we return ENOSUPP if K is negative when trying to JIT the filter or 0 during runtime if we do an indirect load where the value of X is unknown during build time.

Cc "David S. Miller" <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Alexei Starovoitov <[email protected]>
---
 arch/mips/net/bpf_jit.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
arch/mips/net/bpf_jit.c +23 -0
index 5cc92c4590cb..95728ea6cb74 100644
@@ -331,6 +331,12 @@ static inline void emit_srl(unsigned int dst, unsigned int src,
 	emit_instr(ctx, srl, dst, src, sa);
 }
 
+static inline void emit_slt(unsigned int dst, unsigned int src1,
+			    unsigned int src2, struct jit_ctx *ctx)
+{
+	emit_instr(ctx, slt, dst, src1, src2);
+}
+
 static inline void emit_sltu(unsigned int dst, unsigned int src1,
 			     unsigned int src2, struct jit_ctx *ctx)
 {
@@ -816,8 +822,21 @@ static int build_body(struct jit_ctx *ctx)
 			/* A <- P[k:1] */
 			load_order = 0;
 load:
+			/* the interpreter will deal with the negative K */
+			if ((int)k < 0)
+				return -ENOTSUPP;
+
 			emit_load_imm(r_off, k, ctx);
 load_common:
+			/*
+			 * We may got here from the indirect loads so
+			 * return if offset is negative.
+			 */
+			emit_slt(r_s0, r_off, r_zero, ctx);
+			emit_bcond(MIPS_COND_NE, r_s0, r_zero,
+				   b_imm(prog->len, ctx), ctx);
+			emit_reg_move(r_ret, r_zero, ctx);
+
 			ctx->flags |= SEEN_CALL | SEEN_OFF | SEEN_S0 |
 				SEEN_SKB | SEEN_A;
 
@@ -880,6 +899,10 @@ load_ind:
 			emit_load(r_X, r_skb, off, ctx);
 			break;
 		case BPF_LDX | BPF_B | BPF_MSH:
+			/* the interpreter will deal with the negative K */
+			if ((int)k < 0)
+				return -ENOTSUPP;
+
 			/* X <- 4 * (P[k:1] & 0xf) */
 			ctx->flags |= SEEN_X | SEEN_CALL | SEEN_S0 | SEEN_SKB;
 			/* Load offset to a1 */
Signature · 1 line
2.0.0
#3 lore raw

PATCH 3/17 MIPS: mm: uasm: Fix lh micro-assembler instruction

In reply to Markos Chandras

Commit d6b3314b49e12e8c349deb4ca28e7028db00728f
"MIPS: uasm: Add lh uam instruction"

added the 'lh' micro-assembler instruction but it used the 'lw' opcode for it. Fix it by using the correct 'lh' opcode.

---
 arch/mips/mm/uasm-mips.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/mips/mm/uasm-mips.c +1 -1
index 4535a9d19ea5..6708a2dbf934 100644
@@ -89,7 +89,7 @@ static struct insn insn_table[] = {
 	{ insn_lb, M(lb_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
 	{ insn_ld,  M(ld_op, 0, 0, 0, 0, 0),  RS | RT | SIMM },
 	{ insn_ldx, M(spec3_op, 0, 0, 0, ldx_op, lx_op), RS | RT | RD },
-	{ insn_lh,  M(lw_op, 0, 0, 0, 0, 0),  RS | RT | SIMM },
+	{ insn_lh,  M(lh_op, 0, 0, 0, 0, 0),  RS | RT | SIMM },
 	{ insn_lld,  M(lld_op, 0, 0, 0, 0, 0),	RS | RT | SIMM },
 	{ insn_ll,  M(ll_op, 0, 0, 0, 0, 0),  RS | RT | SIMM },
 	{ insn_lui,  M(lui_op, 0, 0, 0, 0, 0),	RT | SIMM },
Signature · 1 line
2.0.0
#8 lore raw

PATCH 6/17 MIPS: bpf: Use 'andi' instead of 'and' for the VLAN cases

In reply to Markos Chandras

The VLAN_VID_MASK and VLAN_TAG_PRESENT are immediates, so using 'and' which expects 3 registers will produce wrong results. Fix this by using the 'andi' instruction.

Cc "David S. Miller" <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Alexei Starovoitov <[email protected]>
---
 arch/mips/net/bpf_jit.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/mips/net/bpf_jit.c +2 -2
index 95728ea6cb74..fe5041bdc6fb 100644
@@ -1317,9 +1317,9 @@ jmp_cmp:
 			off = offsetof(struct sk_buff, vlan_tci);
 			emit_half_load(r_s0, r_skb, off, ctx);
 			if (code == (BPF_ANC | SKF_AD_VLAN_TAG))
-				emit_and(r_A, r_s0, VLAN_VID_MASK, ctx);
+				emit_andi(r_A, r_s0, VLAN_VID_MASK, ctx);
 			else
-				emit_and(r_A, r_s0, VLAN_TAG_PRESENT, ctx);
+				emit_andi(r_A, r_s0, VLAN_TAG_PRESENT, ctx);
 			break;
 		case BPF_ANC | SKF_AD_PKTTYPE:
 			off = pkt_type_offset();
Signature · 1 line
2.0.0
#9 lore raw

PATCH 7/17 MIPS: bpf: Add SEEN_SKB to flags when looking for the PKT_TYPE

In reply to Markos Chandras

The SKF_AD_PKTTYPE uses the skb pointer so make sure it's in the flags so it will be initialized in time.

Cc "David S. Miller" <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Alexei Starovoitov <[email protected]>
---
 arch/mips/net/bpf_jit.c | 2 ++
 1 file changed, 2 insertions(+)
arch/mips/net/bpf_jit.c +2 -0
index fe5041bdc6fb..8cae27af03da 100644
@@ -1322,6 +1322,8 @@ jmp_cmp:
 				emit_andi(r_A, r_s0, VLAN_TAG_PRESENT, ctx);
 			break;
 		case BPF_ANC | SKF_AD_PKTTYPE:
+			ctx->flags |= SEEN_SKB;
+
 			off = pkt_type_offset();
 
 			if (off < 0)
Signature · 1 line
2.0.0
#10 lore raw

PATCH 8/17 MIPS: bpf: Fix branch conditional for BPF_J{GT/GE} cases

In reply to Markos Chandras

The sltiu and sltu instructions will set the scratch register to 1 if A <= X|K so fix the emitted branch conditional to check for scratch != zero rather than scratch >= zero which would complicate the resuling branch logic given that MIPS does not have a BGT or BGET instructions to compare general purpose registers directly.

Cc "David S. Miller" <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Alexei Starovoitov <[email protected]>
---
 arch/mips/net/bpf_jit.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/mips/net/bpf_jit.c +1 -1
index 8cae27af03da..500f97fdc0e1 100644
@@ -1127,7 +1127,7 @@ jmp_cmp:
 				}
 				/* A < (K|X) ? r_scrach = 1 */
 				b_off = b_imm(i + inst->jf + 1, ctx);
-				emit_bcond(MIPS_COND_GT, r_s0, r_zero, b_off,
+				emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off,
 					   ctx);
 				emit_nop(ctx);
 				/* A > (K|X) ? scratch = 0 */
Signature · 1 line
2.0.0
#11 lore raw

PATCH 9/17 MIPS: bpf: Use correct mask for VLAN_TAG case

In reply to Markos Chandras

Using VLAN_VID_MASK is not correct to get the vlan tag. Use ~VLAN_PRESENT_MASK instead and make sure it's u16 so the top 16-bits will be removed. This will ensure that the emit_andi() code will not treat this as a big 32-bit unsigned value.

Cc "David S. Miller" <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Alexei Starovoitov <[email protected]>
---
 arch/mips/net/bpf_jit.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/mips/net/bpf_jit.c +1 -1
index 500f97fdc0e1..a4d1b76e7373 100644
@@ -1317,7 +1317,7 @@ jmp_cmp:
 			off = offsetof(struct sk_buff, vlan_tci);
 			emit_half_load(r_s0, r_skb, off, ctx);
 			if (code == (BPF_ANC | SKF_AD_VLAN_TAG))
-				emit_andi(r_A, r_s0, VLAN_VID_MASK, ctx);
+				emit_andi(r_A, r_s0, (u16)~VLAN_TAG_PRESENT, ctx);
 			else
 				emit_andi(r_A, r_s0, VLAN_TAG_PRESENT, ctx);
 			break;
Signature · 1 line
2.0.0
#12 lore raw

PATCH 10/17 MIPS: bpf: Fix return values for VLAN_TAG_PRESENT case

In reply to Markos Chandras

If VLAN_TAG_PRESENT is not zero, then return 1 as expected by classic BPF. Otherwise return 0.

Cc "David S. Miller" <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Alexei Starovoitov <[email protected]>
---
 arch/mips/net/bpf_jit.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
arch/mips/net/bpf_jit.c +5 -2
index a4d1b76e7373..d852bb6d3fe3 100644
@@ -1316,10 +1316,13 @@ jmp_cmp:
 						  vlan_tci) != 2);
 			off = offsetof(struct sk_buff, vlan_tci);
 			emit_half_load(r_s0, r_skb, off, ctx);
-			if (code == (BPF_ANC | SKF_AD_VLAN_TAG))
+			if (code == (BPF_ANC | SKF_AD_VLAN_TAG)) {
 				emit_andi(r_A, r_s0, (u16)~VLAN_TAG_PRESENT, ctx);
-			else
+			} else {
 				emit_andi(r_A, r_s0, VLAN_TAG_PRESENT, ctx);
+				/* return 1 if present */
+				emit_sltu(r_A, r_zero, r_A, ctx);
+			}
 			break;
 		case BPF_ANC | SKF_AD_PKTTYPE:
 			ctx->flags |= SEEN_SKB;
Signature · 1 line
2.0.0
#13 lore raw

PATCH 11/17 MIPS: bpf: Use pr_debug instead of pr_warn for unhandled opcodes

In reply to Markos Chandras

We should prevent spamming the logs during normal execution of bpf-jit.

Cc "David S. Miller" <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Alexei Starovoitov <[email protected]>
---
 arch/mips/net/bpf_jit.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/mips/net/bpf_jit.c +2 -2
index d852bb6d3fe3..1d228d27d759 100644
@@ -1345,8 +1345,8 @@ jmp_cmp:
 			emit_half_load(r_A, r_skb, off, ctx);
 			break;
 		default:
-			pr_warn("%s: Unhandled opcode: 0x%02x\n", __FILE__,
-				inst->code);
+			pr_debug("%s: Unhandled opcode: 0x%02x\n", __FILE__,
+				 inst->code);
 			return -1;
 		}
 	}
Signature · 1 line
2.0.0
#14 lore raw

PATCH 12/17 MIPS: bpf: Fix is_range() semantics

In reply to Markos Chandras

is_range() was meant to check whether the number is within the s16 range or not. However the return values and consumers expected the exact opposite. We fix that by inverting the logic in the function to return 'true' for < s16 and 'false' for > s16.

---
 arch/mips/net/bpf_jit.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
arch/mips/net/bpf_jit.c +5 -7
index 1d228d27d759..00c4c83972bb 100644
@@ -166,9 +166,7 @@ do {							\
 /* Determine if immediate is within the 16-bit signed range */
 static inline bool is_range16(s32 imm)
 {
-	if (imm >= SBIT(15) || imm < -SBIT(15))
-		return true;
-	return false;
+	return !(imm >= SBIT(15) || imm < -SBIT(15));
 }
 
 static inline void emit_addu(unsigned int dst, unsigned int src1,
@@ -187,7 +185,7 @@ static inline void emit_load_imm(unsigned int dst, u32 imm, struct jit_ctx *ctx)
 {
 	if (ctx->target != NULL) {
 		/* addiu can only handle s16 */
-		if (is_range16(imm)) {
+		if (!is_range16(imm)) {
 			u32 *p = &ctx->target[ctx->idx];
 			uasm_i_lui(&p, r_tmp_imm, (s32)imm >> 16);
 			p = &ctx->target[ctx->idx + 1];
@@ -199,7 +197,7 @@ static inline void emit_load_imm(unsigned int dst, u32 imm, struct jit_ctx *ctx)
 	}
 	ctx->idx++;
 
-	if (is_range16(imm))
+	if (!is_range16(imm))
 		ctx->idx++;
 }
 
@@ -240,7 +238,7 @@ static inline void emit_daddiu(unsigned int dst, unsigned int src,
 static inline void emit_addiu(unsigned int dst, unsigned int src,
 			      u32 imm, struct jit_ctx *ctx)
 {
-	if (is_range16(imm)) {
+	if (!is_range16(imm)) {
 		emit_load_imm(r_tmp, imm, ctx);
 		emit_addu(dst, r_tmp, src, ctx);
 	} else {
@@ -347,7 +345,7 @@ static inline void emit_sltiu(unsigned dst, unsigned int src,
 			      unsigned int imm, struct jit_ctx *ctx)
 {
 	/* 16 bit immediate */
-	if (is_range16((s32)imm)) {
+	if (!is_range16((s32)imm)) {
 		emit_load_imm(r_tmp, imm, ctx);
 		emit_sltu(dst, src, r_tmp, ctx);
 	} else {
Signature · 1 line
2.0.0
#15 lore raw

PATCH 13/17 MIPS: bpf: Drop update_on_xread and always initialize the X register

In reply to Markos Chandras

Previously, update_on_xread() only set the reset flag if SEEN_X hasn't been set already. However, SEEN_X is used to indicate that X is used as destination or source register so there are some cases where X is only used as source register and we really need to make sure that it has been initialized in time. As a result of which, drop this function and always set X to zero if it's used in any of the opcodes.

Cc "David S. Miller" <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Alexei Starovoitov <[email protected]>
---
 arch/mips/net/bpf_jit.c | 22 +---------------------
 1 file changed, 1 insertion(+), 21 deletions(-)
arch/mips/net/bpf_jit.c +1 -21
index 00c4c83972bb..1bcd599d9971 100644
@@ -119,8 +119,6 @@
 /* Arguments used by JIT */
 #define ARGS_USED_BY_JIT	2 /* only applicable to 64-bit */
 
-#define FLAG_NEED_X_RESET	(1 << 0)
-
 #define SBIT(x)			(1 << (x)) /* Signed version of BIT() */
 
 /**
@@ -549,14 +547,6 @@ static inline u16 align_sp(unsigned int num)
 	return num;
 }
 
-static inline void update_on_xread(struct jit_ctx *ctx)
-{
-	if (!(ctx->flags & SEEN_X))
-		ctx->flags |= FLAG_NEED_X_RESET;
-
-	ctx->flags |= SEEN_X;
-}
-
 static bool is_load_to_a(u16 inst)
 {
 	switch (inst) {
@@ -701,7 +691,7 @@ static void build_prologue(struct jit_ctx *ctx)
 	if (ctx->flags & SEEN_SKB)
 		emit_reg_move(r_skb, MIPS_R_A0, ctx);
 
-	if (ctx->flags & FLAG_NEED_X_RESET)
+	if (ctx->flags & SEEN_X)
 		emit_jit_reg_move(r_X, r_zero, ctx);
 
 	/* Do not leak kernel data to userspace */
@@ -876,7 +866,6 @@ load_common:
 			/* A <- P[X + k:1] */
 			load_order = 0;
 load_ind:
-			update_on_xread(ctx);
 			ctx->flags |= SEEN_OFF | SEEN_X;
 			emit_addiu(r_off, r_X, k, ctx);
 			goto load_common;
@@ -972,7 +961,6 @@ load_ind:
 			break;
 		case BPF_ALU | BPF_MUL | BPF_X:
 			/* A *= X */
-			update_on_xread(ctx);
 			ctx->flags |= SEEN_A | SEEN_X;
 			emit_mul(r_A, r_A, r_X, ctx);
 			break;
@@ -1002,7 +990,6 @@ load_ind:
 			break;
 		case BPF_ALU | BPF_DIV | BPF_X:
 			/* A /= X */
-			update_on_xread(ctx);
 			ctx->flags |= SEEN_X | SEEN_A;
 			/* Check if r_X is zero */
 			emit_bcond(MIPS_COND_EQ, r_X, r_zero,
@@ -1012,7 +999,6 @@ load_ind:
 			break;
 		case BPF_ALU | BPF_MOD | BPF_X:
 			/* A %= X */
-			update_on_xread(ctx);
 			ctx->flags |= SEEN_X | SEEN_A;
 			/* Check if r_X is zero */
 			emit_bcond(MIPS_COND_EQ, r_X, r_zero,
@@ -1027,7 +1013,6 @@ load_ind:
 			break;
 		case BPF_ALU | BPF_OR | BPF_X:
 			/* A |= X */
-			update_on_xread(ctx);
 			ctx->flags |= SEEN_A;
 			emit_ori(r_A, r_A, r_X, ctx);
 			break;
@@ -1039,7 +1024,6 @@ load_ind:
 		case BPF_ANC | SKF_AD_ALU_XOR_X:
 		case BPF_ALU | BPF_XOR | BPF_X:
 			/* A ^= X */
-			update_on_xread(ctx);
 			ctx->flags |= SEEN_A;
 			emit_xor(r_A, r_A, r_X, ctx);
 			break;
@@ -1050,7 +1034,6 @@ load_ind:
 			break;
 		case BPF_ALU | BPF_AND | BPF_X:
 			/* A &= X */
-			update_on_xread(ctx);
 			ctx->flags |= SEEN_A | SEEN_X;
 			emit_and(r_A, r_A, r_X, ctx);
 			break;
@@ -1062,7 +1045,6 @@ load_ind:
 		case BPF_ALU | BPF_LSH | BPF_X:
 			/* A <<= X */
 			ctx->flags |= SEEN_A | SEEN_X;
-			update_on_xread(ctx);
 			emit_sllv(r_A, r_A, r_X, ctx);
 			break;
 		case BPF_ALU | BPF_RSH | BPF_K:
@@ -1072,7 +1054,6 @@ load_ind:
 			break;
 		case BPF_ALU | BPF_RSH | BPF_X:
 			ctx->flags |= SEEN_A | SEEN_X;
-			update_on_xread(ctx);
 			emit_srlv(r_A, r_A, r_X, ctx);
 			break;
 		case BPF_ALU | BPF_NEG:
@@ -1243,7 +1224,6 @@ jmp_cmp:
 		case BPF_MISC | BPF_TXA:
 			/* A = X */
 			ctx->flags |= SEEN_A | SEEN_X;
-			update_on_xread(ctx);
 			emit_jit_reg_move(r_A, r_X, ctx);
 			break;
 		/* AUX */
Signature · 1 line
2.0.0
#16 lore raw

PATCH 14/17 MIPS: bpf: Prevent kernel fall over for >=32bit shifts

In reply to Markos Chandras

Remove BUG_ON() if the shift immediate is >=32 to avoid kernel crashes due to malicious user input. Since the micro-assembler will not allow an immediate greater or equal to 32, we will use the maximum value which is 31. This will do the correct thing on either 32- or 64-bit cores since no 64-bit instructions are being used in JIT.

Cc "David S. Miller" <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Alexei Starovoitov <[email protected]>
---
 arch/mips/net/bpf_jit.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
arch/mips/net/bpf_jit.c +4 -2
index 1bcd599d9971..09ebc886c7aa 100644
@@ -309,7 +309,8 @@ static inline void emit_sll(unsigned int dst, unsigned int src,
 			    unsigned int sa, struct jit_ctx *ctx)
 {
 	/* sa is 5-bits long */
-	BUG_ON(sa >= BIT(5));
+	if (sa >= BIT(5))
+		sa = BIT(5) - 1;
 	emit_instr(ctx, sll, dst, src, sa);
 }
 
@@ -323,7 +324,8 @@ static inline void emit_srl(unsigned int dst, unsigned int src,
 			    unsigned int sa, struct jit_ctx *ctx)
 {
 	/* sa is 5-bits long */
-	BUG_ON(sa >= BIT(5));
+	if (sa >= BIT(5))
+		sa =  BIT(5) - 1;
 	emit_instr(ctx, srl, dst, src, sa);
 }
 
Signature · 1 line
2.0.0
#22 lore raw

PATCH 15/17 MIPS: bpf: Fix PKT_TYPE case for big-endian cores

In reply to Markos Chandras

The skb->pkt_type field is defined as follows:

u8 pkt_type:3,
fclone:2,
ipvs_property:1,
peeked:1,
nf_trace:1

resulting to the following layout in big-endian systems

[pkt_type][fclone][ipvs_propery][peeked][nf_trace]
^ ^

|                                                |
LSB                                             MSB

As a result, the existing code did not work because it was trying to match pkt_type == 7 whereas in reality it is 7<<5 on big-endian systems.

This has been fixed in the interpreter in 0dcceabb0c1bf2d4c12a748df9933fad303072a7
"net: filter: fix SKF_AD_PKTTYPE extension on big-endian"

The fix is to look for 7<<5 on big-endian systems for the pkt_type field, and shift by 5 so the packet type will be at the lower 3 bits of the A register.

Cc "David S. Miller" <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Alexei Starovoitov <[email protected]>
---
 arch/mips/net/bpf_jit.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
arch/mips/net/bpf_jit.c +10 -2
index 09ebc886c7aa..4920e0fd05ee 100644
@@ -745,13 +745,17 @@ static u64 jit_get_skb_w(struct sk_buff *skb, unsigned offset)
 	return (u64)err << 32 | ntohl(ret);
 }
 
-#define PKT_TYPE_MAX 7
+#ifdef __BIG_ENDIAN_BITFIELD
+#define PKT_TYPE_MAX	(7 << 5)
+#else
+#define PKT_TYPE_MAX	7
+#endif
 static int pkt_type_offset(void)
 {
 	struct sk_buff skb_probe = {
 		.pkt_type = ~0,
 	};
-	char *ct = (char *)&skb_probe;
+	u8 *ct = (u8 *)&skb_probe;
 	unsigned int off;
 
 	for (off = 0; off < sizeof(struct sk_buff); off++) {
@@ -1314,6 +1318,10 @@ jmp_cmp:
 			emit_load_byte(r_tmp, r_skb, off, ctx);
 			/* Keep only the last 3 bits */
 			emit_andi(r_A, r_tmp, PKT_TYPE_MAX, ctx);
+#ifdef __BIG_ENDIAN_BITFIELD
+			/* Get the actual packet type to the lower 3 bits */
+			emit_srl(r_A, r_A, 5, ctx);
+#endif
 			break;
 		case BPF_ANC | SKF_AD_QUEUE:
 			ctx->flags |= SEEN_SKB | SEEN_A;
Signature · 1 line
2.0.0
#23 lore raw

PATCH 16/17 MIPS: bpf: Use 32 or 64-bit load instruction to load an address to register

In reply to Markos Chandras

When loading a pointer to a register we need to use the appropriate 32 or 64bit instruction to preserve the pointer's top 32bits.

Cc "David S. Miller" <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Alexei Starovoitov <[email protected]>
---
 arch/mips/net/bpf_jit.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
arch/mips/net/bpf_jit.c +13 -1
index 4920e0fd05ee..d8dba7b523a5 100644
@@ -447,6 +447,17 @@ static inline void emit_wsbh(unsigned int dst, unsigned int src,
 	emit_instr(ctx, wsbh, dst, src);
 }
 
+/* load address to register */
+static inline void emit_load_addr(unsigned int dst, unsigned int src,
+				     int imm, struct jit_ctx *ctx)
+{
+	/* src contains the base addr of the 32/64-pointer */
+	if (config_enabled(CONFIG_64BIT))
+		emit_instr(ctx, ld, dst, imm, src);
+	else
+		emit_instr(ctx, lw, dst, imm, src);
+}
+
 /* load a function pointer to register */
 static inline void emit_load_func(unsigned int reg, ptr imm,
 				  struct jit_ctx *ctx)
@@ -1271,7 +1282,8 @@ jmp_cmp:
 			/* A = skb->dev->ifindex */
 			ctx->flags |= SEEN_SKB | SEEN_A | SEEN_S0;
 			off = offsetof(struct sk_buff, dev);
-			emit_load(r_s0, r_skb, off, ctx);
+			/* Load address of *dev member */
+			emit_load_addr(r_s0, r_skb, off, ctx);
 			/* error (0) in the delay slot */
 			emit_bcond(MIPS_COND_EQ, r_s0, r_zero,
 				   b_imm(prog->len, ctx), ctx);
Signature · 1 line
2.0.0
#28 lore raw

PATCH 17/17 MIPS: bpf: Fix stack space allocation for BPF memwords on MIPS64

In reply to Markos Chandras

When allocating stack space for BPF memwords we need to use the appropriate 32 or 64-bit instruction to avoid losing the top 32 bits of the stack pointer.

Cc "David S. Miller" <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Alexei Starovoitov <[email protected]>
---
 arch/mips/net/bpf_jit.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
arch/mips/net/bpf_jit.c +4 -1
index d8dba7b523a5..545c8487542c 100644
@@ -617,7 +617,10 @@ static void save_bpf_jit_regs(struct jit_ctx *ctx, unsigned offset)
 	if (ctx->flags & SEEN_MEM) {
 		if (real_off % (RSIZE * 2))
 			real_off += RSIZE;
-		emit_addiu(r_M, r_sp, real_off, ctx);
+		if (config_enabled(CONFIG_64BIT))
+			emit_daddiu(r_M, r_sp, real_off, ctx);
+		else
+			emit_addiu(r_M, r_sp, real_off, ctx);
 	}
 }
 
Signature · 1 line
2.0.0
#17 lore raw

RE: [PATCH 14/17] MIPS: bpf: Prevent kernel fall over for >=32bit shifts

In reply to Markos Chandras

From: Markos Chandras

Quoted 5 lines

Remove BUG_ON() if the shift immediate is >=32 to avoid kernel crashes due to malicious user input. Since the micro-assembler will not allow an immediate greater or equal to 32, we will use the maximum value which is 31. This will do the correct thing on either 32- or 64-bit cores since no 64-bit instructions are being used in JIT.

I'm not sure that bounding the shift to 31 bits 'is the correct thing'. I'd have thought that emulating the large shift or masking the shift to 5 bits are equally 'correct'.

...

arch/mips/net/bpf_jit.c static inline void emit_sll(unsigned int dst, unsigned int src, · line 314

Quoted 6 lines

{

arch/mips/net/bpf_jit.c +2 -1
 	/* sa is 5-bits long */
-	BUG_ON(sa >= BIT(5));
+	if (sa >= BIT(5))
+		sa = BIT(5) - 1;
 	emit_instr(ctx, sll, dst, src, sa);

...

David

#18 lore raw

Re: [PATCH 14/17] MIPS: bpf: Prevent kernel fall over for >=32bit shifts

Quoted 7 lines · David Laight

From: Markos Chandras

Quoted 5 lines

Remove BUG_ON() if the shift immediate is >=32 to avoid kernel crashes due to malicious user input. Since the micro-assembler will not allow an immediate greater or equal to 32, we will use the maximum value which is 31. This will do the correct thing on either 32- or 64-bit cores since no 64-bit instructions are being used in JIT.

Quoted 5 lines

I'm not sure that bounding the shift to 31 bits 'is the correct thing'. I'd have thought that emulating the large shift or masking the shift to 5 bits are equally 'correct'.

...

Hi David,

Since we use 32-bit registers (or rather, we ignore the top 32bits on MIPS64), shifting >= 32 will always result to 0.
Alexei suggested [1] to allow large shifts and emulate them, so this patch aims to do that by treating >=32 shift values as 31. Please tell me if I got this wrong.

[1] http://www.linux-mips.org/archives/linux-mips/2014-06/msg00212.html

Signature · 1 line
markos
#19 lore raw

RE: [PATCH 14/17] MIPS: bpf: Prevent kernel fall over for >=32bit shifts

From: Markos Chandras

Quoted 12 lines · David Laight
Quoted 11 lines

From: Markos Chandras

Quoted 5 lines

Remove BUG_ON() if the shift immediate is >=32 to avoid kernel crashes due to malicious user input. Since the micro-assembler will not allow an immediate greater or equal to 32, we will use the maximum value which is 31. This will do the correct thing on either 32- or 64-bit cores since no 64-bit instructions are being used in JIT.

I'm not sure that bounding the shift to 31 bits 'is the correct thing'. I'd have thought that emulating the large shift or masking the shift to 5 bits are equally 'correct'.

Quoted 8 lines
Quoted 1 line

...

Hi David,

Since we use 32-bit registers (or rather, we ignore the top 32bits on MIPS64), shifting >= 32 will always result to 0.
Alexei suggested [1] to allow large shifts and emulate them, so this patch aims to do that by treating >=32 shift values as 31. Please tell me if I got this wrong.

Shifting by 31 converts 0xffffffff to 1, not 0.

David

#20 lore raw

Re: [PATCH 14/17] MIPS: bpf: Prevent kernel fall over for >=32bit shifts

Quoted 22 lines · David Laight

From: Markos Chandras

Quoted 12 lines · David Laight
Quoted 11 lines

From: Markos Chandras

Quoted 5 lines

Remove BUG_ON() if the shift immediate is >=32 to avoid kernel crashes due to malicious user input. Since the micro-assembler will not allow an immediate greater or equal to 32, we will use the maximum value which is 31. This will do the correct thing on either 32- or 64-bit cores since no 64-bit instructions are being used in JIT.

I'm not sure that bounding the shift to 31 bits 'is the correct thing'. I'd have thought that emulating the large shift or masking the shift to 5 bits are equally 'correct'.

Quoted 8 lines
Quoted 1 line

...

Hi David,

Since we use 32-bit registers (or rather, we ignore the top 32bits on MIPS64), shifting >= 32 will always result to 0.
Alexei suggested [1] to allow large shifts and emulate them, so this patch aims to do that by treating >=32 shift values as 31. Please tell me if I got this wrong.

Quoted 6 lines

Shifting by 31 converts 0xffffffff to 1, not 0.

David

oops indeed. Maybe it can be fixed by something like this?

arch/mips/net/bpf_jit.c +1 -0
index 545c8487542c..32233ec747e0 100644
@@ -151,6 +151,8 @@ static inline int optimize_div(u32 *k)
         return 0;
 }
 
+static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx

*ctx);
+
/* Simply emit the instruction if the JIT memory space has been
allocated */
#define emit_instr(ctx, func, ...) \
do { \
@@ -310,8 +312,10 @@ static inline void emit_sll(unsigned int dst, unsigned int src,

 {
         /* sa is 5-bits long */
         if (sa >= BIT(5))

- sa = BIT(5) - 1;
- emit_instr(ctx, sll, dst, src, sa);
+ /* Shifting >= 32 results in zero */
+ emit_jit_reg_move(dst, r_zero, ctx);
+ else
+ emit_instr(ctx, sll, dst, src, sa);
}

static inline void emit_srlv(unsigned int dst, unsigned int src,
@@ -325,8 +329,10 @@ static inline void emit_srl(unsigned int dst, unsigned int src,

 {
         /* sa is 5-bits long */
         if (sa >= BIT(5))

- sa = BIT(5) - 1;
- emit_instr(ctx, srl, dst, src, sa);
+ /* Shifting >= 32 results in zero */
+ emit_jit_reg_move(dst, r_zero, ctx);
+ else
+ emit_instr(ctx, srl, dst, src, sa);
}

Signature · 1 line
markos
maintainer
#29 lore raw
In reply to Markos Chandras

From: Markos Chandras <[email protected]>
Date: Mon, 23 Jun 2014 10:38:43 +0100

Quoted 7 lines

Here are some fixes for MIPS/BPF for 3.16. These fixes make the bpf testsuite *almost* happy with only 2 tests (LD_IND_LL, LD_IND_NET) failing at the moment. Since fixing the remaining tests is not so trivial, it would be nice to have these fixes in 3.16 for now.

The patches are based on the upstream-sfr/mips-for-linux-next tree because they depend on https://patchwork.linux-mips.org/patch/7099/

You did not CC: netdev on patches 1, 2, and 3. Please do not do this, people on this list will want to review the series as a whole.

#24 lore raw

Re: [PATCH 16/17] MIPS: bpf: Use 32 or 64-bit load instruction to load an address to register

In reply to Markos Chandras
Quoted 17 lines · Markos Chandras

When loading a pointer to a register we need to use the appropriate 32 or 64bit instruction to preserve the pointer's top 32bits.

Cc "David S. Miller" <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Alexei Starovoitov <[email protected]>
---
 arch/mips/net/bpf_jit.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
arch/mips/net/bpf_jit.c +0 -0
index 4920e0fd05ee..d8dba7b523a5 100644

arch/mips/net/bpf_jit.c static inline void emit_wsbh(unsigned int dst, unsigned int src, · line 452

Quoted 6 lines
arch/mips/net/bpf_jit.c +3 -0
@@ -447,6 +447,17 @@ static inline void emit_wsbh(unsigned int dst, unsigned int src,
 	emit_instr(ctx, wsbh, dst, src);
 }
 
+/* load address to register */
+static inline void emit_load_addr(unsigned int dst, unsigned int src,
+				     int imm, struct jit_ctx *ctx)

(I originally sent this in reply to your internal posting, but assume you missed it or it got eaten somewhere along the way.)

The name emit_load_addr & comment "load address to register" makes this sound like an equivalent of the "la" pseudo instruction, but it appears to really emit a pointer sized load? How about emit_load_ptr or something instead, and similarly s/address/pointer/ in the comment?

arch/mips/net/bpf_jit.c static inline void emit_wsbh(unsigned int dst, unsigned int src, · line 458

Quoted 6 lines
arch/mips/net/bpf_jit.c +6 -0
+{
+	/* src contains the base addr of the 32/64-pointer */
+	if (config_enabled(CONFIG_64BIT))
+		emit_instr(ctx, ld, dst, imm, src);
+	else
+		emit_instr(ctx, lw, dst, imm, src);

Is there some way you could make use of the UASM_i_LW macro (note the capitalisation) instead of the if statement here?

Thanks,
Paul

Quoted 19 lines
arch/mips/net/bpf_jit.c +4 -1
+}
+
 /* load a function pointer to register */
 static inline void emit_load_func(unsigned int reg, ptr imm,
 				  struct jit_ctx *ctx)
@@ -1271,7 +1282,8 @@ jmp_cmp:
 			/* A = skb->dev->ifindex */
 			ctx->flags |= SEEN_SKB | SEEN_A | SEEN_S0;
 			off = offsetof(struct sk_buff, dev);
-			emit_load(r_s0, r_skb, off, ctx);
+			/* Load address of *dev member */
+			emit_load_addr(r_s0, r_skb, off, ctx);
 			/* error (0) in the delay slot */
 			emit_bcond(MIPS_COND_EQ, r_s0, r_zero,
 				   b_imm(prog->len, ctx), ctx);
Signature · 1 line
2.0.0
Acked-by on v1 5/17
#6 lore raw

Re: [PATCH 05/17] MIPS: bpf: Return error code if the offset is a negative number

In reply to Markos Chandras
Quoted 11 lines · Markos Chandras

Previously, the negative offset was not checked leading to failures due to trying to load data beyond the skb struct boundaries. Until we have proper asm helpers in place, it's best if we return ENOSUPP if K is negative when trying to JIT the filter or 0 during runtime if we do an indirect load where the value of X is unknown during build time.

Cc "David S. Miller" <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Alexei Starovoitov <[email protected]>

Hi Markos,

thank you for addressing all of my earlier comments. Looks like test_bpf was quite useful in finding all of these bugs :) For the patches that reached netdev:

One minor nit below:

Quoted 22 lines
---
 arch/mips/net/bpf_jit.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
arch/mips/net/bpf_jit.c +6 -0
index 5cc92c4590cb..95728ea6cb74 100644
@@ -331,6 +331,12 @@ static inline void emit_srl(unsigned int dst, unsigned int src,
        emit_instr(ctx, srl, dst, src, sa);
 }
 
+static inline void emit_slt(unsigned int dst, unsigned int src1,
+                           unsigned int src2, struct jit_ctx *ctx)
+{
+       emit_instr(ctx, slt, dst, src1, src2);
+}
+
 static inline void emit_sltu(unsigned int dst, unsigned int src1,
                             unsigned int src2, struct jit_ctx *ctx)
 {

arch/mips/net/bpf_jit.c static int build_body(struct jit_ctx *ctx) · line 826

Quoted 5 lines
arch/mips/net/bpf_jit.c +2 -0
@@ -816,8 +822,21 @@ static int build_body(struct jit_ctx *ctx)
                        /* A <- P[k:1] */
                        load_order = 0;
 load:
+                       /* the interpreter will deal with the negative K */
+                       if ((int)k < 0)

should be a space after cast.

#30 lore raw
In reply to David Miller
Quoted 8 lines · David Miller

From: Markos Chandras <[email protected]>
Date: Mon, 23 Jun 2014 10:38:43 +0100

Quoted 5 lines

Here are some fixes for MIPS/BPF for 3.16. These fixes make the bpf testsuite *almost* happy with only 2 tests (LD_IND_LL, LD_IND_NET) failing at the moment. Since fixing the remaining tests is not so trivial, it would be nice to have these fixes in 3.16 for now.

Quoted 6 lines
Quoted 2 lines

The patches are based on the upstream-sfr/mips-for-linux-next tree because they depend on https://patchwork.linux-mips.org/patch/7099/

You did not CC: netdev on patches 1, 2, and 3. Please do not do this, people on this list will want to review the series as a whole.

Apologies David. Patches 1,2,3 have nothing to do with bpf-jit and they only fix/add MIPS micro-assembler instructions so I didn't though netdev@ actually cared about that. I will be more careful in the future.

Signature · 1 line
markos
#7 lore raw

Re: [PATCH 05/17] MIPS: bpf: Return error code if the offset is a negative number

In reply to Alexei Starovoitov
Quoted 14 lines · Alexei Starovoitov
Quoted 11 lines · Markos Chandras

Previously, the negative offset was not checked leading to failures due to trying to load data beyond the skb struct boundaries. Until we have proper asm helpers in place, it's best if we return ENOSUPP if K is negative when trying to JIT the filter or 0 during runtime if we do an indirect load where the value of X is unknown during build time.

Cc "David S. Miller" <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Quoted 8 lines

Hi Markos,

thank you for addressing all of my earlier comments. Looks like test_bpf was quite useful in finding all of these bugs :) For the patches that reached netdev:

Thank you for the review and your constructive comments in your previous emails.

Signature · 1 line
markos
#25 lore raw

Re: [PATCH 16/17] MIPS: bpf: Use 32 or 64-bit load instruction to load an address to register

In reply to Paul Burton
Quoted 16 lines · Paul Burton

arch/mips/net/bpf_jit.c static inline void emit_wsbh(unsigned int dst, unsigned int src, · line 452

Quoted 11 lines · Markos Chandras
arch/mips/net/bpf_jit.c +3 -0
index 4920e0fd05ee..d8dba7b523a5 100644
@@ -447,6 +447,17 @@ static inline void emit_wsbh(unsigned int dst, unsigned int src,
 	emit_instr(ctx, wsbh, dst, src);
 }
 
+/* load address to register */
+static inline void emit_load_addr(unsigned int dst, unsigned int src,
+				     int imm, struct jit_ctx *ctx)

(I originally sent this in reply to your internal posting, but assume you missed it or it got eaten somewhere along the way.)

Quoted 5 lines

The name emit_load_addr & comment "load address to register" makes this sound like an equivalent of the "la" pseudo instruction, but it appears to really emit a pointer sized load? How about emit_load_ptr or something instead, and similarly s/address/pointer/ in the comment?

Hi Paul,

I suppose I could do that. I will send a v2

Quoted 10 lines

arch/mips/net/bpf_jit.c static inline void emit_wsbh(unsigned int dst, unsigned int src, · line 458

Quoted 6 lines
arch/mips/net/bpf_jit.c +6 -0
+{
+	/* src contains the base addr of the 32/64-pointer */
+	if (config_enabled(CONFIG_64BIT))
+		emit_instr(ctx, ld, dst, imm, src);
+	else
+		emit_instr(ctx, lw, dst, imm, src);

Is there some way you could make use of the UASM_i_LW macro (note the capitalisation) instead of the if statement here?

Not right now. I use config_enabled(CONFIG_64BIT) everywhere in that file to emit 32-bit or 64-bit instructions. So I will look into switching to the USAM_i_* macros when i submit the remaining fixes probably for 3.17.

Signature · 1 line
markos
#21 lore raw

PATCH v2 14/17 MIPS: bpf: Prevent kernel fall over for >=32bit shifts

In reply to Markos Chandras

Remove BUG_ON() if the shift immediate is >=32 to avoid kernel crashes due to malicious user input. If the shift immediate is >= 32, we simply load the destination register with 0 since only 32-bit instructions are used by JIT so this will do the correct thing even on MIPS64.

Cc "David S. Miller" <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Alexei Starovoitov <[email protected]>

---
Changes since v1:
- For sa >=32, load destination register with zero instead of treating the immediate as 31

---
 arch/mips/net/bpf_jit.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
arch/mips/net/bpf_jit.c +12 -4
index 1bcd599d9971..9476e7f061a1 100644
@@ -151,6 +151,8 @@ static inline int optimize_div(u32 *k)
 	return 0;
 }
 
+static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx);
+
 /* Simply emit the instruction if the JIT memory space has been allocated */
 #define emit_instr(ctx, func, ...)			\
 do {							\
@@ -309,8 +311,11 @@ static inline void emit_sll(unsigned int dst, unsigned int src,
 			    unsigned int sa, struct jit_ctx *ctx)
 {
 	/* sa is 5-bits long */
-	BUG_ON(sa >= BIT(5));
-	emit_instr(ctx, sll, dst, src, sa);
+	if (sa >= BIT(5))
+		/* Shifting >= 32 results in zero */
+		emit_jit_reg_move(dst, r_zero, ctx);
+	else
+		emit_instr(ctx, sll, dst, src, sa);
 }
 
 static inline void emit_srlv(unsigned int dst, unsigned int src,
@@ -323,8 +328,11 @@ static inline void emit_srl(unsigned int dst, unsigned int src,
 			    unsigned int sa, struct jit_ctx *ctx)
 {
 	/* sa is 5-bits long */
-	BUG_ON(sa >= BIT(5));
-	emit_instr(ctx, srl, dst, src, sa);
+	if (sa >= BIT(5))
+		/* Shifting >= 32 results in zero */
+		emit_jit_reg_move(dst, r_zero, ctx);
+	else
+		emit_instr(ctx, srl, dst, src, sa);
 }
 
 static inline void emit_slt(unsigned int dst, unsigned int src1,
Signature · 1 line
2.0.0
#26 lore raw

PATCH v2 16/17 MIPS: bpf: Use 32 or 64-bit load instruction to load an address to register

In reply to Markos Chandras

When loading a pointer to register we need to use the appropriate 32 or 64bit instruction to preserve the pointers' top 32bits.

Cc "David S. Miller" <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Alexei Starovoitov <[email protected]>

---
Changes since v1:
- Change function name to make it clear that we are loading a pointer to a register, not an address

---
 arch/mips/net/bpf_jit.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
arch/mips/net/bpf_jit.c +13 -1
index 4505e2e6ab53..6e3963425b64 100644
@@ -453,6 +453,17 @@ static inline void emit_wsbh(unsigned int dst, unsigned int src,
 	emit_instr(ctx, wsbh, dst, src);
 }
 
+/* load pointer to register */
+static inline void emit_load_ptr(unsigned int dst, unsigned int src,
+				     int imm, struct jit_ctx *ctx)
+{
+	/* src contains the base addr of the 32/64-pointer */
+	if (config_enabled(CONFIG_64BIT))
+		emit_instr(ctx, ld, dst, imm, src);
+	else
+		emit_instr(ctx, lw, dst, imm, src);
+}
+
 /* load a function pointer to register */
 static inline void emit_load_func(unsigned int reg, ptr imm,
 				  struct jit_ctx *ctx)
@@ -1277,7 +1288,8 @@ jmp_cmp:
 			/* A = skb->dev->ifindex */
 			ctx->flags |= SEEN_SKB | SEEN_A | SEEN_S0;
 			off = offsetof(struct sk_buff, dev);
-			emit_load(r_s0, r_skb, off, ctx);
+			/* Load *dev pointer */
+			emit_load_ptr(r_s0, r_skb, off, ctx);
 			/* error (0) in the delay slot */
 			emit_bcond(MIPS_COND_EQ, r_s0, r_zero,
 				   b_imm(prog->len, ctx), ctx);
Signature · 1 line
2.0.0
#27 lore raw

Re: [PATCH v2 16/17] MIPS: bpf: Use 32 or 64-bit load instruction to load an address to register

Quoted 8 lines · Markos Chandras

When loading a pointer to register we need to use the appropriate 32 or 64bit instruction to preserve the pointers' top 32bits.

Cc "David S. Miller" <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Quoted 4 lines

---
Changes since v1:
- Change function name to make it clear that we are loading a pointer to a register, not an address

Markos,

when you post v2, please refresh the whole series, add v2 to subject and repost all patches from scratch.

Thanks