diff mbox series

[bpf-next,v2,4/8] arm32, bpf: add support for unconditional bswap instruction

Message ID 20230906183320.1959008-5-puranjay12@gmail.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series arm32, bpf: add support for cpuv4 insns | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 9 this patch: 9
netdev/cc_maintainers warning 1 maintainers not CCed: linux-arm-kernel@lists.infradead.org
netdev/build_clang success Errors and warnings before: 9 this patch: 9
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 9 this patch: 9
netdev/checkpatch warning WARNING: line length of 81 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-5 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-0 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-1 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 fail Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-28 success Logs for veristat
bpf/vmtest-bpf-next-VM_Test-12 fail Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 fail Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-10 fail Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-16 fail Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 fail Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 fail Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-11 fail Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for test_maps on s390x with gcc

Commit Message

Puranjay Mohan Sept. 6, 2023, 6:33 p.m. UTC
The cpuv4 added a new unconditional bswap instruction with following
behaviour:

BPF_ALU64 | BPF_TO_LE | BPF_END with imm = 16/32/64 means:
dst = bswap16(dst)
dst = bswap32(dst)
dst = bswap64(dst)

As we already support converting to big-endian from little-endian we can
use the same for unconditional bswap. just treat the unconditional scenario
the same as big-endian conversion.

Signed-off-by: Puranjay Mohan <puranjay12@gmail.com>
---
 arch/arm/net/bpf_jit_32.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Russell King (Oracle) Sept. 7, 2023, 8:48 a.m. UTC | #1
On Wed, Sep 06, 2023 at 06:33:16PM +0000, Puranjay Mohan wrote:
> @@ -1633,8 +1633,10 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
>  	/* dst = htobe(dst) */
>  	case BPF_ALU | BPF_END | BPF_FROM_LE:
>  	case BPF_ALU | BPF_END | BPF_FROM_BE:
> +	/* dst = bswap(dst) */
> +	case BPF_ALU64 | BPF_END | BPF_TO_LE:
>  		rd = arm_bpf_get_reg64(dst, tmp, ctx);
> -		if (BPF_SRC(code) == BPF_FROM_LE)
> +		if (BPF_SRC(code) == BPF_FROM_LE && BPF_CLASS(code) != BPF_ALU64)

With the addition of the BPF_ALU64 case, I'm wondering why this if() is
affected. If you were adding:

	case BPF_ALU64 | BPF_END | BPF_FROM_LE:

then maybe there would be a reason, but the BPF_ALU64 | BPF_END |
BPF_TO_LE case will never match even the original if() statement.
Puranjay Mohan Sept. 7, 2023, 9:08 a.m. UTC | #2
On Thu, Sep 07 2023, Russell King (Oracle) wrote:

> On Wed, Sep 06, 2023 at 06:33:16PM +0000, Puranjay Mohan wrote:
>> @@ -1633,8 +1633,10 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
>>  	/* dst = htobe(dst) */
>>  	case BPF_ALU | BPF_END | BPF_FROM_LE:
>>  	case BPF_ALU | BPF_END | BPF_FROM_BE:
>> +	/* dst = bswap(dst) */
>> +	case BPF_ALU64 | BPF_END | BPF_TO_LE:
>>  		rd = arm_bpf_get_reg64(dst, tmp, ctx);
>> -		if (BPF_SRC(code) == BPF_FROM_LE)
>> +		if (BPF_SRC(code) == BPF_FROM_LE && BPF_CLASS(code) != BPF_ALU64)
>
> With the addition of the BPF_ALU64 case, I'm wondering why this if() is
> affected. If you were adding:
>
> 	case BPF_ALU64 | BPF_END | BPF_FROM_LE:
>
> then maybe there would be a reason, but the BPF_ALU64 | BPF_END |
> BPF_TO_LE case will never match even the original if() statement.

The reason is that these mean the same thing.
from: include/uapi/linux/bpf.h

#define BPF_TO_LE	0x00	/* convert to little-endian */
#define BPF_TO_BE	0x08	/* convert to big-endian */
#define BPF_FROM_LE	BPF_TO_LE
#define BPF_FROM_BE	BPF_TO_BE

So, to not cause confusion and follow the earlier cases I can add:

case BPF_ALU64 | BPF_END | BPF_FROM_LE:

in the next version.


Thanks,
Puranjay
Russell King (Oracle) Sept. 7, 2023, 9:15 a.m. UTC | #3
On Thu, Sep 07, 2023 at 09:08:46AM +0000, Puranjay Mohan wrote:
> On Thu, Sep 07 2023, Russell King (Oracle) wrote:
> 
> > On Wed, Sep 06, 2023 at 06:33:16PM +0000, Puranjay Mohan wrote:
> >> @@ -1633,8 +1633,10 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
> >>  	/* dst = htobe(dst) */
> >>  	case BPF_ALU | BPF_END | BPF_FROM_LE:
> >>  	case BPF_ALU | BPF_END | BPF_FROM_BE:
> >> +	/* dst = bswap(dst) */
> >> +	case BPF_ALU64 | BPF_END | BPF_TO_LE:
> >>  		rd = arm_bpf_get_reg64(dst, tmp, ctx);
> >> -		if (BPF_SRC(code) == BPF_FROM_LE)
> >> +		if (BPF_SRC(code) == BPF_FROM_LE && BPF_CLASS(code) != BPF_ALU64)
> >
> > With the addition of the BPF_ALU64 case, I'm wondering why this if() is
> > affected. If you were adding:
> >
> > 	case BPF_ALU64 | BPF_END | BPF_FROM_LE:
> >
> > then maybe there would be a reason, but the BPF_ALU64 | BPF_END |
> > BPF_TO_LE case will never match even the original if() statement.
> 
> The reason is that these mean the same thing.
> from: include/uapi/linux/bpf.h
> 
> #define BPF_TO_LE	0x00	/* convert to little-endian */
> #define BPF_TO_BE	0x08	/* convert to big-endian */
> #define BPF_FROM_LE	BPF_TO_LE
> #define BPF_FROM_BE	BPF_TO_BE
> 
> So, to not cause confusion and follow the earlier cases I can add:
> 
> case BPF_ALU64 | BPF_END | BPF_FROM_LE:
> 
> in the next version.

It might be worth adding a comment after each stating one of:

	/* also BPF_TO_LE */
	/* also BPF_TO_BE */

as appropriate to make this more readable.

Thanks.
diff mbox series

Patch

diff --git a/arch/arm/net/bpf_jit_32.c b/arch/arm/net/bpf_jit_32.c
index 56ea8022e989..f837db5c71b1 100644
--- a/arch/arm/net/bpf_jit_32.c
+++ b/arch/arm/net/bpf_jit_32.c
@@ -1633,8 +1633,10 @@  static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
 	/* dst = htobe(dst) */
 	case BPF_ALU | BPF_END | BPF_FROM_LE:
 	case BPF_ALU | BPF_END | BPF_FROM_BE:
+	/* dst = bswap(dst) */
+	case BPF_ALU64 | BPF_END | BPF_TO_LE:
 		rd = arm_bpf_get_reg64(dst, tmp, ctx);
-		if (BPF_SRC(code) == BPF_FROM_LE)
+		if (BPF_SRC(code) == BPF_FROM_LE && BPF_CLASS(code) != BPF_ALU64)
 			goto emit_bswap_uxt;
 		switch (imm) {
 		case 16: