Skip to contentSkip to conversation

powerpc/bpf: Fix issue with atomic ops

PATCH Applied Naveen N. Rao 5 participants 10 messages Jul 1, 2021 – Jul 6, 2021

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

cover powerpc/bpf: Fix issue with atomic ops

The first patch fixes an issue that causes a soft lockup on ppc64 with the BPF_ATOMIC bounds propagation verifier test. The second one updates ppc32 JIT to reject atomic operations properly.

- Naveen

Naveen N. Rao (2):
powerpc/bpf: Fix detecting BPF atomic instructions powerpc/bpf: Reject atomic ops in ppc32 JIT

 arch/powerpc/net/bpf_jit_comp32.c | 14 +++++++++++---
 arch/powerpc/net/bpf_jit_comp64.c |  4 ++--
 2 files changed, 13 insertions(+), 5 deletions(-)

base-commit: 086d9878e1092e7e69a69676ee9ec792690abb1d

Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/2 powerpc/bpf: Fix detecting BPF atomic instructions

Commit 91c960b0056672 ("bpf: Rename BPF_XADD and prepare to encode other atomics in .imm") converted BPF_XADD to BPF_ATOMIC and added a way to distinguish instructions based on the immediate field. Existing JIT implementations were updated to check for the immediate field and to reject programs utilizing anything more than BPF_ADD (such as BPF_FETCH) in the immediate field.

However, the check added to powerpc64 JIT did not look at the correct BPF instruction. Due to this, such programs would be accepted and incorrectly JIT'ed resulting in soft lockups, as seen with the atomic bounds test. Fix this by looking at the correct immediate value.

Fixes 91c960b0056672 ("bpf: Rename BPF_XADD and prepare to encode other atomics in .imm")

---
Hi Jiri,
FYI: I made a small change in this patch -- using 'imm' directly, rather than insn[i].imm. I've still added your Tested-by since this shouldn't impact the fix in any way.

- Naveen

 arch/powerpc/net/bpf_jit_comp64.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/powerpc/net/bpf_jit_comp64.c +2 -2
index 5cad5b5a7e9774..de8595880feec6 100644
@@ -667,7 +667,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *
 		 * BPF_STX ATOMIC (atomic ops)
 		 */
 		case BPF_STX | BPF_ATOMIC | BPF_W:
-			if (insn->imm != BPF_ADD) {
+			if (imm != BPF_ADD) {
 				pr_err_ratelimited(
 					"eBPF filter atomic op code %02x (@%d) unsupported\n",
 					code, i);
@@ -689,7 +689,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *
 			PPC_BCC_SHORT(COND_NE, tmp_idx);
 			break;
 		case BPF_STX | BPF_ATOMIC | BPF_DW:
-			if (insn->imm != BPF_ADD) {
+			if (imm != BPF_ADD) {
 				pr_err_ratelimited(
 					"eBPF filter atomic op code %02x (@%d) unsupported\n",
 					code, i);
Signature · 1 line
2.31.1
#2 lore raw

Re: [PATCH 1/2] powerpc/bpf: Fix detecting BPF atomic instructions

Quoted 17 lines · Naveen N. Rao

Commit 91c960b0056672 ("bpf: Rename BPF_XADD and prepare to encode other atomics in .imm") converted BPF_XADD to BPF_ATOMIC and added a way to distinguish instructions based on the immediate field. Existing JIT implementations were updated to check for the immediate field and to reject programs utilizing anything more than BPF_ADD (such as BPF_FETCH) in the immediate field.

However, the check added to powerpc64 JIT did not look at the correct BPF instruction. Due to this, such programs would be accepted and incorrectly JIT'ed resulting in soft lockups, as seen with the atomic bounds test. Fix this by looking at the correct immediate value.

Fixes 91c960b0056672 ("bpf: Rename BPF_XADD and prepare to encode other atomics in .imm")
Quoted 7 lines

---
Hi Jiri,
FYI: I made a small change in this patch -- using 'imm' directly, rather than insn[i].imm. I've still added your Tested-by since this shouldn't impact the fix in any way.

- Naveen

Excellent debugging! You guys are awesome.
How do you want this fix routed? via bpf tree?

#3 lore raw

Re: [PATCH 1/2] powerpc/bpf: Fix detecting BPF atomic instructions

Quoted 25 lines · Alexei Starovoitov
Quoted 23 lines · Naveen N. Rao

Commit 91c960b0056672 ("bpf: Rename BPF_XADD and prepare to encode other atomics in .imm") converted BPF_XADD to BPF_ATOMIC and added a way to distinguish instructions based on the immediate field. Existing JIT implementations were updated to check for the immediate field and to reject programs utilizing anything more than BPF_ADD (such as BPF_FETCH) in the immediate field.

However, the check added to powerpc64 JIT did not look at the correct BPF instruction. Due to this, such programs would be accepted and incorrectly JIT'ed resulting in soft lockups, as seen with the atomic bounds test. Fix this by looking at the correct immediate value.

Fixes 91c960b0056672 ("bpf: Rename BPF_XADD and prepare to encode other atomics in .imm")

---
Hi Jiri,
FYI: I made a small change in this patch -- using 'imm' directly, rather than insn[i].imm. I've still added your Tested-by since this shouldn't impact the fix in any way.

Quoted 3 lines
Quoted 1 line

- Naveen

Excellent debugging! You guys are awesome.

Thanks. Jiri and Brendan did the bulk of the work :)

Quoted 1 line

How do you want this fix routed? via bpf tree?

Michael has a few BPF patches queued up in powerpc tree for v5.14, so it might be easier to take these patches through the powerpc tree unless he feels otherwise. Michael?

This also needs to be tagged for stable:
Cc: [email protected] # 5.12+

- Naveen

#4 lore raw

Re: [PATCH 1/2] powerpc/bpf: Fix detecting BPF atomic instructions

Quoted 31 lines · Naveen N. Rao
Quoted 25 lines · Alexei Starovoitov
Quoted 23 lines · Naveen N. Rao

Commit 91c960b0056672 ("bpf: Rename BPF_XADD and prepare to encode other atomics in .imm") converted BPF_XADD to BPF_ATOMIC and added a way to distinguish instructions based on the immediate field. Existing JIT implementations were updated to check for the immediate field and to reject programs utilizing anything more than BPF_ADD (such as BPF_FETCH) in the immediate field.

However, the check added to powerpc64 JIT did not look at the correct BPF instruction. Due to this, such programs would be accepted and incorrectly JIT'ed resulting in soft lockups, as seen with the atomic bounds test. Fix this by looking at the correct immediate value.

Fixes 91c960b0056672 ("bpf: Rename BPF_XADD and prepare to encode other atomics in .imm")

---
Hi Jiri,
FYI: I made a small change in this patch -- using 'imm' directly, rather than insn[i].imm. I've still added your Tested-by since this shouldn't impact the fix in any way.

Quoted 3 lines
Quoted 1 line

- Naveen

Excellent debugging! You guys are awesome.

Quoted 7 lines

Thanks. Jiri and Brendan did the bulk of the work :)

Quoted 1 line

How do you want this fix routed? via bpf tree?

Michael has a few BPF patches queued up in powerpc tree for v5.14, so it might be easier to take these patches through the powerpc tree unless he feels otherwise. Michael?

Works for me. Thanks!

#5 lore raw

Re: [PATCH 1/2] powerpc/bpf: Fix detecting BPF atomic instructions

In reply to Naveen N. Rao
Quoted 16 lines · Naveen N. Rao

Commit 91c960b0056672 ("bpf: Rename BPF_XADD and prepare to encode other atomics in .imm") converted BPF_XADD to BPF_ATOMIC and added a way to distinguish instructions based on the immediate field. Existing JIT implementations were updated to check for the immediate field and to reject programs utilizing anything more than BPF_ADD (such as BPF_FETCH) in the immediate field.

However, the check added to powerpc64 JIT did not look at the correct BPF instruction. Due to this, such programs would be accepted and incorrectly JIT'ed resulting in soft lockups, as seen with the atomic bounds test. Fix this by looking at the correct immediate value.

Fixes 91c960b0056672 ("bpf: Rename BPF_XADD and prepare to encode other atomics in .imm")
Quoted 5 lines

---
Hi Jiri,
FYI: I made a small change in this patch -- using 'imm' directly, rather than insn[i].imm. I've still added your Tested-by since this shouldn't impact the fix in any way.

yep, it works nicely

thanks
jirka

Quoted 32 lines

- Naveen

 arch/powerpc/net/bpf_jit_comp64.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/powerpc/net/bpf_jit_comp64.c +2 -2
index 5cad5b5a7e9774..de8595880feec6 100644
@@ -667,7 +667,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *
 		 * BPF_STX ATOMIC (atomic ops)
 		 */
 		case BPF_STX | BPF_ATOMIC | BPF_W:
-			if (insn->imm != BPF_ADD) {
+			if (imm != BPF_ADD) {
 				pr_err_ratelimited(
 					"eBPF filter atomic op code %02x (@%d) unsupported\n",
 					code, i);
@@ -689,7 +689,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *
 			PPC_BCC_SHORT(COND_NE, tmp_idx);
 			break;
 		case BPF_STX | BPF_ATOMIC | BPF_DW:
-			if (insn->imm != BPF_ADD) {
+			if (imm != BPF_ADD) {
 				pr_err_ratelimited(
 					"eBPF filter atomic op code %02x (@%d) unsupported\n",
 					code, i);
Signature · 1 line
2.31.1
#6 lore raw

PATCH 2/2 powerpc/bpf: Reject atomic ops in ppc32 JIT

In reply to Naveen N. Rao

Commit 91c960b0056672 ("bpf: Rename BPF_XADD and prepare to encode other atomics in .imm") converted BPF_XADD to BPF_ATOMIC and updated all JIT implementations to reject JIT'ing instructions with an immediate value different from BPF_ADD. However, ppc32 BPF JIT was implemented around the same time and didn't include the same change. Update the ppc32 JIT accordingly.

---
 arch/powerpc/net/bpf_jit_comp32.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
arch/powerpc/net/bpf_jit_comp32.c +11 -3
index cbe5b399ed869d..91c990335a16c9 100644
@@ -773,9 +773,17 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *
 			break;
 
 		/*
-		 * BPF_STX XADD (atomic_add)
+		 * BPF_STX ATOMIC (atomic ops)
 		 */
-		case BPF_STX | BPF_XADD | BPF_W: /* *(u32 *)(dst + off) += src */
+		case BPF_STX | BPF_ATOMIC | BPF_W:
+			if (imm != BPF_ADD) {
+				pr_err_ratelimited(
+					"eBPF filter atomic op code %02x (@%d) unsupported\n", code, i);
+				return -ENOTSUPP;
+			}
+
+			/* *(u32 *)(dst + off) += src */
+
 			bpf_set_seen_register(ctx, tmp_reg);
 			/* Get offset into TMP_REG */
 			EMIT(PPC_RAW_LI(tmp_reg, off));
@@ -789,7 +797,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *
 			PPC_BCC_SHORT(COND_NE, (ctx->idx - 3) * 4);
 			break;
 
-		case BPF_STX | BPF_XADD | BPF_DW: /* *(u64 *)(dst + off) += src */
+		case BPF_STX | BPF_ATOMIC | BPF_DW: /* *(u64 *)(dst + off) += src */
 			return -EOPNOTSUPP;
 
 		/*
Signature · 1 line
2.31.1
#7 lore raw

Re: [PATCH 2/2] powerpc/bpf: Reject atomic ops in ppc32 JIT

Le 01/07/2021 à 17:08, Naveen N. Rao a écrit :

Quoted 8 lines

Commit 91c960b0056672 ("bpf: Rename BPF_XADD and prepare to encode other atomics in .imm") converted BPF_XADD to BPF_ATOMIC and updated all JIT implementations to reject JIT'ing instructions with an immediate value different from BPF_ADD. However, ppc32 BPF JIT was implemented around the same time and didn't include the same change. Update the ppc32 JIT accordingly.

Shouldn't it also include a Fixes tag and stable Cc as PPC32 eBPF was added in 5.13 ?

Fixes 51c66ad849a7 ("powerpc/bpf: Implement extended BPF on PPC32")
Quoted 38 lines
---
  arch/powerpc/net/bpf_jit_comp32.c | 14 +++++++++++---
  1 file changed, 11 insertions(+), 3 deletions(-)
arch/powerpc/net/bpf_jit_comp32.c +11 -3
index cbe5b399ed869d..91c990335a16c9 100644
@@ -773,9 +773,17 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *
  			break;
 
  		/*
-		 * BPF_STX XADD (atomic_add)
+		 * BPF_STX ATOMIC (atomic ops)
  		 */
-		case BPF_STX | BPF_XADD | BPF_W: /* *(u32 *)(dst + off) += src */
+		case BPF_STX | BPF_ATOMIC | BPF_W:
+			if (imm != BPF_ADD) {
+				pr_err_ratelimited(
+					"eBPF filter atomic op code %02x (@%d) unsupported\n", code, i);
+				return -ENOTSUPP;
+			}
+
+			/* *(u32 *)(dst + off) += src */
+
  			bpf_set_seen_register(ctx, tmp_reg);
  			/* Get offset into TMP_REG */
  			EMIT(PPC_RAW_LI(tmp_reg, off));
@@ -789,7 +797,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *
  			PPC_BCC_SHORT(COND_NE, (ctx->idx - 3) * 4);
  			break;
 
-		case BPF_STX | BPF_XADD | BPF_DW: /* *(u64 *)(dst + off) += src */
+		case BPF_STX | BPF_ATOMIC | BPF_DW: /* *(u64 *)(dst + off) += src */
  			return -EOPNOTSUPP;
 
  		/*
#8 lore raw

Re: [PATCH 2/2] powerpc/bpf: Reject atomic ops in ppc32 JIT

Quoted 10 lines · Christophe Leroy

Le 01/07/2021 à 17:08, Naveen N. Rao a écrit :

Quoted 7 lines

Commit 91c960b0056672 ("bpf: Rename BPF_XADD and prepare to encode other atomics in .imm") converted BPF_XADD to BPF_ATOMIC and updated all JIT implementations to reject JIT'ing instructions with an immediate value different from BPF_ADD. However, ppc32 BPF JIT was implemented around the same time and didn't include the same change. Update the ppc32 JIT accordingly.

Quoted 3 lines
Quoted 1 line

Shouldn't it also include a Fixes tag and stable Cc as PPC32 eBPF was added in 5.13 ?

Yes, I wasn't sure which patch to actually blame. But you're right, this should have the below fixes tag since this affects the ppc32 eBPF JIT.

Quoted 3 lines
Fixes 51c66ad849a7 ("powerpc/bpf: Implement extended BPF on PPC32")

Thanks,
- Naveen

#9 lore raw
In reply to Naveen N. Rao
Quoted 11 lines · Naveen N. Rao

The first patch fixes an issue that causes a soft lockup on ppc64 with the BPF_ATOMIC bounds propagation verifier test. The second one updates ppc32 JIT to reject atomic operations properly.

- Naveen

Naveen N. Rao (2):
powerpc/bpf: Fix detecting BPF atomic instructions powerpc/bpf: Reject atomic ops in ppc32 JIT

[...]

Applied to powerpc/fixes.

[1/2] powerpc/bpf: Fix detecting BPF atomic instructions

      https://git.kernel.org/powerpc/c/419ac821766cbdb9fd85872bb3f1a589df05c94c
[2/2] powerpc/bpf: Reject atomic ops in ppc32 JIT
      https://git.kernel.org/powerpc/c/307e5042c7bdae15308ef2e9b848833b84122eb0

cheers