Skip to contentSkip to conversation

BPF: Regression test for 64bit arithmetic

PATCH Superseded Richard Palethorpe 4 participants 4 messages Sep 5, 2019 – Sep 10, 2019

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 BPF: Regression test for 64bit arithmetic

---

V2: Rebase over the top of the capabilities API patch set, Metan's rewrite
of the other BPF tests and master.

This requires the capabilities patch set and Metan's patch set to be merged first.

 include/lapi/bpf.h                         |  27 +++
 runtest/syscalls                           |   1 +
 testcases/kernel/syscalls/bpf/.gitignore   |   1 +
 testcases/kernel/syscalls/bpf/bpf_prog02.c | 182 +++++++++++++++++++++
 4 files changed, 211 insertions(+)
 create mode 100644 testcases/kernel/syscalls/bpf/bpf_prog02.c
include/lapi/bpf.h +27 -0
index 122eb5469..03073b45e 100644
@@ -18,7 +18,9 @@
 /* Start copy from linux/bpf_(common).h */
 #define BPF_CLASS(code) ((code) & 0x07)
 #define		BPF_LD		0x00
+#define		BPF_LDX		0x01
 #define		BPF_ST		0x02
+#define		BPF_STX		0x03
 #define		BPF_JMP		0x05
 
 #define BPF_SIZE(code)  ((code) & 0x18)
@@ -30,6 +32,7 @@
 
 #define BPF_OP(code)    ((code) & 0xf0)
 #define		BPF_ADD		0x00
+#define		BPF_SUB		0x10
 
 #define		BPF_JEQ		0x10
 
@@ -432,6 +435,14 @@ enum bpf_func_id {
 
 /* Start copy from tools/include/filter.h */
 
+#define BPF_ALU64_REG(OP, DST, SRC)				\
+	((struct bpf_insn) {					\
+		.code  = BPF_ALU64 | BPF_OP(OP) | BPF_X,	\
+		.dst_reg = DST,					\
+		.src_reg = SRC,					\
+		.off   = 0,					\
+		.imm   = 0 })
+
 #define BPF_ALU64_IMM(OP, DST, IMM)				\
 	((struct bpf_insn) {					\
 		.code  = BPF_ALU64 | BPF_OP(OP) | BPF_K,	\
@@ -477,6 +488,22 @@ enum bpf_func_id {
 		.off   = OFF,					\
 		.imm   = IMM })
 
+#define BPF_LDX_MEM(SIZE, DST, SRC, OFF)			\
+	((struct bpf_insn) {					\
+		.code  = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM,	\
+		.dst_reg = DST,					\
+		.src_reg = SRC,					\
+		.off   = OFF,					\
+		.imm   = 0 })
+
+#define BPF_STX_MEM(SIZE, DST, SRC, OFF)			\
+	((struct bpf_insn) {					\
+		.code  = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM,	\
+		.dst_reg = DST,					\
+		.src_reg = SRC,					\
+		.off   = OFF,					\
+		.imm   = 0 })
+
 #define BPF_JMP_IMM(OP, DST, IMM, OFF)				\
 	((struct bpf_insn) {					\
 		.code  = BPF_JMP | BPF_OP(OP) | BPF_K,		\
runtest/syscalls +1 -0
index 874ae4d4f..4e6310193 100644
@@ -34,6 +34,7 @@ bind03 bind03
 
 bpf_map01 bpf_map01
 bpf_prog01 bpf_prog01
+bpf_prog02 bpf_prog02
 
 brk01 brk01
 
testcases/kernel/syscalls/bpf/.gitignore +1 -0
index 7eb5f7c92..1704f9841 100644
@@ -1,2 +1,3 @@
 bpf_map01
 bpf_prog01
+bpf_prog02
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +182 -0

182 lines not on this page · raw message

Signature · 1 line
2.22.1
#1 lore raw

PATCH v2 1/1 BPF: Regression test for 64bit arithmetic

----- Original Message -----

testcases/kernel/syscalls/bpf/bpf_prog02.c line 47

Quoted 13 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +12 -0
+static int load_prog(int fd)
+{
+	struct bpf_insn *prog;
+	struct bpf_insn insn[] = {
+		BPF_MOV64_IMM(BPF_REG_6, 1),            /* r6 = 1 */
+
+		BPF_LD_MAP_FD(BPF_REG_1, fd),	        /* r1 = &fd */
+		BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),   /* r2 = fp */
+		BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),  /* r2 = r2 - 8 */
+		BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),    /* *r2 = 0 */
+		BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
+		BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 17), /* if(!r0) goto exit */

Patch looks good to me.

But I keep thinking if there's way to make it more obvious where offset (e.g. 17) came from.

Idea 1: use multiple lines per instruction to denote length
BPF_LD_IMM64(BPF_REG_4,

               A64INT),

Idea 2: prefix commented instructions with offset

        /* 1: r3 = r0 */
        /* 2: r4 = 2^61 */
Quoted 19 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +19 -0
+		BPF_MOV64_REG(BPF_REG_3, BPF_REG_0),    /* r3 = r0 */
+		BPF_LD_IMM64(BPF_REG_4, A64INT),        /* r4 = 2^61 */
+		BPF_ALU64_REG(BPF_ADD, BPF_REG_4, BPF_REG_6), /* r4 += r6 */
+		BPF_STX_MEM(BPF_DW, BPF_REG_3, BPF_REG_4, 0), /* *r3 = r4 */
+
+		BPF_LD_MAP_FD(BPF_REG_1, fd),	        /* r1 = &fd */
+		BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),   /* r2 = fp */
+		BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),  /* r2 = r2 - 8 */
+		BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 1),    /* *r2 = 1 */
+		BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
+		BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 5),  /* if(!r0) goto exit */
+		BPF_MOV64_REG(BPF_REG_3, BPF_REG_0),    /* r3 = r0 */
+		BPF_LD_IMM64(BPF_REG_4, A64INT),        /* r4 = 2^61 */
+		BPF_ALU64_REG(BPF_SUB, BPF_REG_4, BPF_REG_6), /* r4 -= r6 */
+		BPF_STX_MEM(BPF_DW, BPF_REG_3, BPF_REG_4, 0), /* *r3 = r4 */
+
+		BPF_MOV64_IMM(BPF_REG_0, 0),            /* r0 = 0 */
+		BPF_EXIT_INSN(),		        /* return r0 */
+	};
#2 lore raw

PATCH v2 1/1 BPF: Regression test for 64bit arithmetic

Hi!

Quoted 19 lines

testcases/kernel/syscalls/bpf/bpf_prog02.c line 47

Quoted 13 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +12 -0
+static int load_prog(int fd)
+{
+	struct bpf_insn *prog;
+	struct bpf_insn insn[] = {
+		BPF_MOV64_IMM(BPF_REG_6, 1),            /* r6 = 1 */
+
+		BPF_LD_MAP_FD(BPF_REG_1, fd),	        /* r1 = &fd */
+		BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),   /* r2 = fp */
+		BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),  /* r2 = r2 - 8 */
+		BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),    /* *r2 = 0 */
+		BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
+		BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 17), /* if(!r0) goto exit */

Patch looks good to me.

But I keep thinking if there's way to make it more obvious where offset (e.g. 17) came from.

Quoted 7 lines

Idea 1: use multiple lines per instruction to denote length
BPF_LD_IMM64(BPF_REG_4,

               A64INT),

Idea 2: prefix commented instructions with offset

        /* 1: r3 = r0 */
        /* 2: r4 = 2^61 */

I guess I like the Idea 2 better.

Another option would be having eBPF assembler included in the LTP build system. I guess that it may be useful later on and there seems to be one written in python:

https://github.com/solarflarecom/ebpf_asm

But for the short term I would go with adding the offset to the comments.

Signature · 2 lines
Cyril Hrubis
[email protected]
#3 lore raw

PATCH v2 1/1 BPF: Regression test for 64bit arithmetic

Hello,

Quoted 30 lines · Cyril Hrubis

Hi!

Quoted 19 lines

testcases/kernel/syscalls/bpf/bpf_prog02.c line 47

Quoted 13 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +12 -0
+static int load_prog(int fd)
+{
+	struct bpf_insn *prog;
+	struct bpf_insn insn[] = {
+		BPF_MOV64_IMM(BPF_REG_6, 1),            /* r6 = 1 */
+
+		BPF_LD_MAP_FD(BPF_REG_1, fd),	        /* r1 = &fd */
+		BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),   /* r2 = fp */
+		BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),  /* r2 = r2 - 8 */
+		BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),    /* *r2 = 0 */
+		BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
+		BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 17), /* if(!r0) goto exit */

Patch looks good to me.

But I keep thinking if there's way to make it more obvious where offset (e.g. 17) came from.

Quoted 7 lines

Idea 1: use multiple lines per instruction to denote length
BPF_LD_IMM64(BPF_REG_4,

               A64INT),

Idea 2: prefix commented instructions with offset

        /* 1: r3 = r0 */
        /* 2: r4 = 2^61 */

I guess I like the Idea 2 better.

Quoted 8 lines

Another option would be having eBPF assembler included in the LTP build system. I guess that it may be useful later on and there seems to be one written in python:

https://github.com/solarflarecom/ebpf_asm

But for the short term I would go with adding the offset to the comments.

Another idea I had was to use place holder values in the instruction array which can be substituted.

Infact we could use an invalid instruction code to indicate a goto instruction and another code for a tag. Then replace the tag with a NOP and replace the goto with a valid jump statement.

I decided not to try any of that because it seemed like overkill at the time. However for a more complex program I can see this getting very confusing.

I wonder if an assembler will make things better in some tests and worse in others. Sometimes the assembler has pseudo instructions where it is not clear what the resulting machine instructions will be.

--
Thank you,
Richard.