diff mbox series

[bpf-next] bpf: Simplify expression for identify bpf mem type

Message ID 20210317125147.2159512-1-Jianlin.Lv@arm.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series [bpf-next] bpf: Simplify expression for identify bpf mem type | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for bpf-next
netdev/subject_prefix success Link
netdev/cc_maintainers success CCed 15 of 15 maintainers
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 12261 this patch: 12261
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 34 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 12767 this patch: 12767
netdev/header_inline success Link

Commit Message

Jianlin Lv March 17, 2021, 12:51 p.m. UTC
Added BPF_SIZE_MASK macro as mask of size modifier that help to reduce
the evaluation of expressions in if statements,
and remove BPF_SIZE_MASK in netronome driver.

Signed-off-by: Jianlin Lv <Jianlin.Lv@arm.com>
---
 drivers/net/ethernet/netronome/nfp/bpf/main.h |  2 --
 include/uapi/linux/bpf.h                      |  1 +
 kernel/bpf/verifier.c                         | 12 ++++--------
 3 files changed, 5 insertions(+), 10 deletions(-)

Comments

Alexei Starovoitov March 17, 2021, 7:51 p.m. UTC | #1
On Wed, Mar 17, 2021 at 5:52 AM Jianlin Lv <Jianlin.Lv@arm.com> wrote:
>         return BPF_CLASS(meta->insn.code);
> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> index 2d3036e292a9..5d77675e7112 100644
> --- a/include/uapi/linux/bpf.h
> +++ b/include/uapi/linux/bpf.h
> @@ -21,6 +21,7 @@
>  #define BPF_DW         0x18    /* double word (64-bit) */
>  #define BPF_ATOMIC     0xc0    /* atomic memory ops - op type in immediate */
>  #define BPF_XADD       0xc0    /* exclusive add - legacy name */
> +#define BPF_SIZE_MASK  0x18    /* mask of size modifier */

Pls don't add kernel internal defines to uapi.
diff mbox series

Patch

diff --git a/drivers/net/ethernet/netronome/nfp/bpf/main.h b/drivers/net/ethernet/netronome/nfp/bpf/main.h
index d0e17eebddd9..8b1c2509ce46 100644
--- a/drivers/net/ethernet/netronome/nfp/bpf/main.h
+++ b/drivers/net/ethernet/netronome/nfp/bpf/main.h
@@ -346,8 +346,6 @@  struct nfp_insn_meta {
 	struct list_head l;
 };
 
-#define BPF_SIZE_MASK	0x18
-
 static inline u8 mbpf_class(const struct nfp_insn_meta *meta)
 {
 	return BPF_CLASS(meta->insn.code);
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 2d3036e292a9..5d77675e7112 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -21,6 +21,7 @@ 
 #define BPF_DW		0x18	/* double word (64-bit) */
 #define BPF_ATOMIC	0xc0	/* atomic memory ops - op type in immediate */
 #define BPF_XADD	0xc0	/* exclusive add - legacy name */
+#define BPF_SIZE_MASK	0x18    /* mask of size modifier */
 
 /* alu/jmp fields */
 #define BPF_MOV		0xb0	/* mov reg to reg */
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index f9096b049cd6..9755bb4d7de4 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -11384,15 +11384,11 @@  static int convert_ctx_accesses(struct bpf_verifier_env *env)
 	for (i = 0; i < insn_cnt; i++, insn++) {
 		bpf_convert_ctx_access_t convert_ctx_access;
 
-		if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) ||
-		    insn->code == (BPF_LDX | BPF_MEM | BPF_H) ||
-		    insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
-		    insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
+		/* opcode: BPF_MEM | <size> | BPF_LDX */
+		if ((insn->code & ~BPF_SIZE_MASK) == (BPF_LDX | BPF_MEM))
 			type = BPF_READ;
-		else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) ||
-			 insn->code == (BPF_STX | BPF_MEM | BPF_H) ||
-			 insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
-			 insn->code == (BPF_STX | BPF_MEM | BPF_DW))
+		/* opcode: BPF_MEM | <size> | BPF_STX */
+		else if ((insn->code & ~BPF_SIZE_MASK) == (BPF_STX | BPF_MEM))
 			type = BPF_WRITE;
 		else
 			continue;