# \[PATCH 0/2\] powerpc/bpf: Fix issue with atomic ops

- Subsystems: LINUX FOR POWERPC (32-BIT AND 64-BIT), BPF JIT for POWERPC (32-BIT AND 64-BIT), BPF \[GENERAL\] (Safe Dynamic Programs and Tools), THE REST
- Maintainers: Madhavan Srinivasan, Hari Bathini, Christophe Leroy, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi, Linus Torvalds
- Series: 2 patches touching 2 files, +13/-5
- Status: LANDED
- Landed in mainline.
- List: linuxppc-dev
- 10 messages, 5 authors
- Page: https://ratatoskr.run/linuxppc-dev/2021/07/7772692/t

## \[PATCH 0/2\] powerpc/bpf: Fix issue with atomic ops

- From: Naveen N. Rao \<hidden\>
- Date: 2021-07-01 15:09:46 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/7772692.md

```text
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
-- 
2.31.1

```

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

- From: Naveen N. Rao \<hidden\>
- Date: 2021-07-01 15:09:48 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/7772693.md

```text
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")
Reported-by: Jiri Olsa <redacted>
Tested-by: Jiri Olsa <redacted>
Signed-off-by: Naveen N. Rao <redacted>
---
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(-)

diff --git a/arch/powerpc/net/bpf_jit_comp64.c b/arch/powerpc/net/bpf_jit_comp64.c
index 5cad5b5a7e9774..de8595880feec6 100644
--- a/arch/powerpc/net/bpf_jit_comp64.c
+++ b/arch/powerpc/net/bpf_jit_comp64.c
@@ -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);
-- 
2.31.1

```

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

- From: Naveen N. Rao \<hidden\>
- Date: 2021-07-01 15:10:15 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/7772694.md

```text
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.

Signed-off-by: Naveen N. Rao <redacted>
---
 arch/powerpc/net/bpf_jit_comp32.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/arch/powerpc/net/bpf_jit_comp32.c b/arch/powerpc/net/bpf_jit_comp32.c
index cbe5b399ed869d..91c990335a16c9 100644
--- a/arch/powerpc/net/bpf_jit_comp32.c
+++ b/arch/powerpc/net/bpf_jit_comp32.c
@@ -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;
 
 		/*
-- 
2.31.1

```

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

- From: Alexei Starovoitov \<hidden\>
- Date: 2021-07-01 16:03:37 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/7772697.md

```text
On Thu, Jul 1, 2021 at 8:09 AM Naveen N. Rao
[ref] wrote:
>
> 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")
> Reported-by: Jiri Olsa <redacted>
> Tested-by: Jiri Olsa <redacted>
> Signed-off-by: Naveen N. Rao <redacted>
> ---
> 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?
```

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

- From: Christophe Leroy \<hidden\>
- Date: 2021-07-01 16:36:15 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/7772700.md

```text


Le 01/07/2021 à 17:08, Naveen N. Rao a écrit :
> 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.
>
> Signed-off-by: Naveen N. Rao <redacted>

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")
Cc: <redacted>

> ---
>   arch/powerpc/net/bpf_jit_comp32.c | 14 +++++++++++---
>   1 file changed, 11 insertions(+), 3 deletions(-)
>
> diff --git a/arch/powerpc/net/bpf_jit_comp32.c b/arch/powerpc/net/bpf_jit_comp32.c
> index cbe5b399ed869d..91c990335a16c9 100644
> --- a/arch/powerpc/net/bpf_jit_comp32.c
> +++ b/arch/powerpc/net/bpf_jit_comp32.c
> @@ -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;
>   
>   		/*
>
```

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

- From: Naveen N. Rao \<hidden\>
- Date: 2021-07-01 19:32:32 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/7772715.md

```text
Alexei Starovoitov wrote:
> On Thu, Jul 1, 2021 at 8:09 AM Naveen N. Rao
> [ref] wrote:
>>
>> 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")
>> Reported-by: Jiri Olsa <redacted>
>> Tested-by: Jiri Olsa <redacted>
>> Signed-off-by: Naveen N. Rao <redacted>
>> ---
>> 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.

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

> 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: <redacted> # 5.12+


- Naveen
```

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

- From: Alexei Starovoitov \<hidden\>
- Date: 2021-07-01 19:33:42 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/7772716.md

```text
On Thu, Jul 1, 2021 at 12:32 PM Naveen N. Rao
[ref] wrote:
>
> Alexei Starovoitov wrote:
> > On Thu, Jul 1, 2021 at 8:09 AM Naveen N. Rao
> > [ref] wrote:
> >>
> >> 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")
> >> Reported-by: Jiri Olsa <redacted>
> >> Tested-by: Jiri Olsa <redacted>
> >> Signed-off-by: Naveen N. Rao <redacted>
> >> ---
> >> 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.
>
> Thanks. Jiri and Brendan did the bulk of the work :)
>
> > 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!
```

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

- From: Naveen N. Rao \<hidden\>
- Date: 2021-07-01 19:36:54 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/7772717.md

```text
Christophe Leroy wrote:
>
>
> Le 01/07/2021 à 17:08, Naveen N. Rao a écrit :
>> 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.
>>
>> Signed-off-by: Naveen N. Rao <redacted>
>
> 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.

>
> Fixes: 51c66ad849a7 ("powerpc/bpf: Implement extended BPF on PPC32")
> Cc: <redacted>

Cc: <redacted> # v5.13


Thanks,
- Naveen

```

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

- From: Jiri Olsa \<hidden\>
- Date: 2021-07-02 10:26:18 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/7772733.md

```text
On Thu, Jul 01, 2021 at 08:38:58PM +0530, Naveen N. Rao wrote:
> 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")
> Reported-by: Jiri Olsa <redacted>
> Tested-by: Jiri Olsa <redacted>
> Signed-off-by: Naveen N. Rao <redacted>
> ---
> 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

> 
> - Naveen
> 
> 
>  arch/powerpc/net/bpf_jit_comp64.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/powerpc/net/bpf_jit_comp64.c b/arch/powerpc/net/bpf_jit_comp64.c
> index 5cad5b5a7e9774..de8595880feec6 100644
> --- a/arch/powerpc/net/bpf_jit_comp64.c
> +++ b/arch/powerpc/net/bpf_jit_comp64.c
> @@ -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);
> -- 
> 2.31.1
> 

```

## Re: \[PATCH 0/2\] powerpc/bpf: Fix issue with atomic ops

- From: Michael Ellerman \<hidden\>
- Date: 2021-07-06 10:52:57 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/7772791.md

```text
On Thu, 1 Jul 2021 20:38:57 +0530, Naveen N. Rao wrote:
> 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
```
