Skip to contentSkip to conversation

BPF cleanups

PATCH Superseded Daniel Borkmann 3 participants 8 messages May 1, 2014

Subsystems (5)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover BPF cleanups

v2->v3:
- Included Dave's feedback for unsigned long type in patch 3
- Patch 1 and patch 2 unchanged since v1, dropped other two for now
v1->v2:
- Only changed patch 5 as to suggestion from Alexei
- Rest is the same

Daniel Borkmann (3):
net: filter: simplify label names from jump-table
net: filter: make register namings more comprehensible
net: filter: misc/various cleanups

 include/linux/filter.h |  47 ++++-
 net/core/filter.c      | 538 +++++++++++++++++++++++++------------------------
 2 files changed, 309 insertions(+), 276 deletions(-)
Signature · 1 line
1.7.11.7
#1 lore raw

PATCH v3 1/3 net: filter: simplify label names from jump-table

This patch simplifies label naming for the BPF jump-table. When we define labels via DL(), we just concatenate/textify the combination of instruction opcode which consists of the class, subclass, word size, target register and so on. Each time we leave BPF_ prefix intact, so that e.g. the preprocessor generates a label BPF_ALU_BPF_ADD_BPF_X for DL(BPF_ALU, BPF_ADD, BPF_X) whereas a label name of ALU_ADD_X is much more easy to grasp. Pure cleanup only.

---
 include/linux/filter.h |   3 +
 net/core/filter.c      | 308 ++++++++++++++++++++++++-------------------------
 2 files changed, 157 insertions(+), 154 deletions(-)
include/linux/filter.h +3 -0
index 759abf7..b042d1d 100644
@@ -37,6 +37,9 @@
 #define BPF_CALL	0x80	/* function call */
 #define BPF_EXIT	0x90	/* function return */
 
+/* Placeholder/dummy for 0 */
+#define BPF_0		0
+
 /* BPF has 10 general purpose 64-bit registers and stack frame. */
 #define MAX_BPF_REG	11
 
net/core/filter.c +154 -154

504 lines not on this page · raw message

Signature · 1 line
1.7.11.7
#6 lore raw

PATCH v3 2/3 net: filter: make register namings more comprehensible

In reply to Daniel Borkmann

The current code is a bit hard to parse on which registers can be used, how they are mapped and all play together. It makes much more sense to define this a bit more clearly so that the code is a bit more intuitive. This patch cleans this up, and makes naming a bit more consistent among the code. This also allows for moving some of the defines into the header file. Clearing of A and X registers in __sk_run_filter() do not get a particular register name assigned as they have not an 'official' function, but rather just result from the concrete initial mapping of old BPF programs. Since for BPF helper functions for BPF_CALL we already use small letters, so be consistent here as well. No functional changes.

---
 include/linux/filter.h |  44 ++++++++--
 net/core/filter.c      | 215 +++++++++++++++++++++++++------------------------
 2 files changed, 145 insertions(+), 114 deletions(-)
include/linux/filter.h +36 -8
index b042d1d..ed1efab 100644
@@ -40,16 +40,47 @@
 /* Placeholder/dummy for 0 */
 #define BPF_0		0
 
+/* Register numbers */
+enum {
+	BPF_REG_0 = 0,
+	BPF_REG_1,
+	BPF_REG_2,
+	BPF_REG_3,
+	BPF_REG_4,
+	BPF_REG_5,
+	BPF_REG_6,
+	BPF_REG_7,
+	BPF_REG_8,
+	BPF_REG_9,
+	BPF_REG_10,
+	__MAX_BPF_REG,
+};
+
 /* BPF has 10 general purpose 64-bit registers and stack frame. */
-#define MAX_BPF_REG	11
+#define MAX_BPF_REG	__MAX_BPF_REG
+
+/* ArgX, context and stack frame pointer register positions. Note,
+ * Arg1, Arg2, Arg3, etc are used as argument mappings of function
+ * calls in BPF_CALL instruction.
+ */
+#define BPF_REG_ARG1	BPF_REG_1
+#define BPF_REG_ARG2	BPF_REG_2
+#define BPF_REG_ARG3	BPF_REG_3
+#define BPF_REG_ARG4	BPF_REG_4
+#define BPF_REG_ARG5	BPF_REG_5
+#define BPF_REG_CTX	BPF_REG_6
+#define BPF_REG_FP	BPF_REG_10
+
+/* Additional register mappings for converted user programs. */
+#define BPF_REG_A	BPF_REG_0
+#define BPF_REG_X	BPF_REG_7
+#define BPF_REG_TMP	BPF_REG_8
 
 /* BPF program can access up to 512 bytes of stack space. */
 #define MAX_BPF_STACK	512
 
-/* Arg1, context and stack frame pointer register positions. */
-#define ARG1_REG	1
-#define CTX_REG		6
-#define FP_REG		10
+/* Macro to invoke filter function. */
+#define SK_RUN_FILTER(filter, ctx)  (*filter->bpf_func)(ctx, filter->insnsi)
 
 struct sock_filter_int {
 	__u8	code;		/* opcode */
@@ -100,9 +131,6 @@ static inline unsigned int sk_filter_size(unsigned int proglen)
 #define sk_filter_proglen(fprog)			\
 		(fprog->len * sizeof(fprog->filter[0]))
 
-#define SK_RUN_FILTER(filter, ctx)			\
-		(*filter->bpf_func)(ctx, filter->insnsi)
-
 int sk_filter(struct sock *sk, struct sk_buff *skb);
 
 u32 sk_run_filter_int_seccomp(const struct seccomp_data *ctx,
net/core/filter.c +109 -106

500 lines not on this page · raw message

Signature · 1 line
1.7.11.7
#7 lore raw

PATCH v3 3/3 net: filter: misc/various cleanups

In reply to Daniel Borkmann

This contains only some minor misc cleanpus. We can spare us the extra variable declaration in __skb_get_pay_offset(), the cast in __get_random_u32() is rather unnecessary and in __sk_migrate_realloc() we can remove the memcpy() and do a direct assignment of the structs. Latter was suggested by Fengguang Wu found with coccinelle. Also, remaining pointer casts of long should be unsigned long instead.

---
 net/core/filter.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
net/core/filter.c +7 -8
index c93ca8d..36cfceb 100644
@@ -78,9 +78,9 @@ void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, uns
 		ptr = skb_network_header(skb) + k - SKF_NET_OFF;
 	else if (k >= SKF_LL_OFF)
 		ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
-
 	if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
 		return ptr;
+
 	return NULL;
 }
 
@@ -89,6 +89,7 @@ static inline void *load_pointer(const struct sk_buff *skb, int k,
 {
 	if (k >= 0)
 		return skb_header_pointer(skb, k, size, buffer);
+
 	return bpf_internal_load_pointer_neg_helper(skb, k, size);
 }
 
@@ -598,14 +599,12 @@ static unsigned int pkt_type_offset(void)
 
 static u64 __skb_get_pay_offset(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
 {
-	struct sk_buff *skb = (struct sk_buff *)(long) ctx;
-
-	return __skb_get_poff(skb);
+	return __skb_get_poff((struct sk_buff *)(long unsigned) ctx);
 }
 
 static u64 __skb_get_nlattr(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
 {
-	struct sk_buff *skb = (struct sk_buff *)(long) ctx;
+	struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx;
 	struct nlattr *nla;
 
 	if (skb_is_nonlinear(skb))
@@ -626,7 +625,7 @@ static u64 __skb_get_nlattr(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
 
 static u64 __skb_get_nlattr_nest(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
 {
-	struct sk_buff *skb = (struct sk_buff *)(long) ctx;
+	struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx;
 	struct nlattr *nla;
 
 	if (skb_is_nonlinear(skb))
@@ -657,7 +656,7 @@ static u64 __get_raw_cpu_id(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
 /* note that this only generates 32-bit random numbers */
 static u64 __get_random_u32(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
 {
-	return (u64)prandom_u32();
+	return prandom_u32();
 }
 
 static bool convert_bpf_extensions(struct sock_filter *fp,
@@ -1475,7 +1474,7 @@ static struct sk_filter *__sk_migrate_realloc(struct sk_filter *fp,
 
 	fp_new = sock_kmalloc(sk, len, GFP_KERNEL);
 	if (fp_new) {
-		memcpy(fp_new, fp, sizeof(struct sk_filter));
+		*fp_new = *fp;
 		/* As we're kepping orig_prog in fp_new along,
 		 * we need to make sure we're not evicting it
 		 * from the old fp.
Signature · 1 line
1.7.11.7
#2 lore raw

Re: [PATCH net-next v3 1/3] net: filter: simplify label names from jump-table

In reply to Daniel Borkmann
Quoted 9 lines · Daniel Borkmann

This patch simplifies label naming for the BPF jump-table. When we define labels via DL(), we just concatenate/textify the combination of instruction opcode which consists of the class, subclass, word size, target register and so on. Each time we leave BPF_ prefix intact, so that e.g. the preprocessor generates a label BPF_ALU_BPF_ADD_BPF_X for DL(BPF_ALU, BPF_ADD, BPF_X) whereas a label name of ALU_ADD_X is much more easy to grasp. Pure cleanup only.

Quoted 3 lines

---

This exactly makes harder to use grep and future code reviews.

# git grep -n BPF_XADD
include/linux/filter.h:21:#define BPF_XADD      0xc0    /* exclusive add */
net/core/filter.c:224:          DL(BPF_STX, BPF_XADD, BPF_W),
net/core/filter.c:225:          DL(BPF_STX, BPF_XADD, BPF_DW),
net/core/filter.c:481:  BPF_STX_BPF_XADD_BPF_W: /* lock xadd *(u32 *)(A + insn->off) += X */
net/core/filter.c:485:  BPF_STX_BPF_XADD_BPF_DW: /* lock xadd *(u64 *)(A + insn->off) += X */

Compare now with :

# git grep -n XADD

So I am quite against this patch.

#3 lore raw

Re: [PATCH net-next v3 1/3] net: filter: simplify label names from jump-table

Quoted 23 lines · Eric Dumazet
Quoted 9 lines · Daniel Borkmann

This patch simplifies label naming for the BPF jump-table. When we define labels via DL(), we just concatenate/textify the combination of instruction opcode which consists of the class, subclass, word size, target register and so on. Each time we leave BPF_ prefix intact, so that e.g. the preprocessor generates a label BPF_ALU_BPF_ADD_BPF_X for DL(BPF_ALU, BPF_ADD, BPF_X) whereas a label name of ALU_ADD_X is much more easy to grasp. Pure cleanup only.

Quoted 3 lines

---

This exactly makes harder to use grep and future code reviews.

# git grep -n BPF_XADD
include/linux/filter.h:21:#define BPF_XADD      0xc0    /* exclusive add */
net/core/filter.c:224:          DL(BPF_STX, BPF_XADD, BPF_W),
net/core/filter.c:225:          DL(BPF_STX, BPF_XADD, BPF_DW),
net/core/filter.c:481:  BPF_STX_BPF_XADD_BPF_W: /* lock xadd *(u32 *)(A + insn->off) += X */
net/core/filter.c:485:  BPF_STX_BPF_XADD_BPF_DW: /* lock xadd *(u64 *)(A + insn->off) += X */
Quoted 5 lines

Compare now with :

# git grep -n XADD

So I am quite against this patch.

Well, if you review the code itself in filter.c it makes it hard to read and review, with this patch, you'll immediately get the label name and what it actually does, so I think it's quite convenient and way more readable by itself. For grepping, you can always add subdirectories you're searching for, besides that I don't think that everything in the kernel needs to have unique names only so that one can grep for it among the whole tree.

#4 lore raw

Re: [PATCH net-next v3 1/3] net: filter: simplify label names from jump-table

Quoted 7 lines · Daniel Borkmann

Well, if you review the code itself in filter.c it makes it hard to read and review, with this patch, you'll immediately get the label name and what it actually does, so I think it's quite convenient and way more readable by itself. For grepping, you can always add subdirectories you're searching for, besides that I don't think that everything in the kernel needs to have unique names only so that one can grep for it among the whole tree.

Considering that Alexei claimed :

+Internal BPF is a general purpose RISC instruction set. Not every register and +every instruction are used during translation from original BPF to new format. +For example, socket filters are not using 'exclusive add' instruction, but +tracing filters may do to maintain counters of events, for example. Register R9 +is not used by socket filters either, but more complex filters may be running +out of registers and would have to resort to spill/fill to stack.

I tried to verify this claim and I could not.

After your patch, it will be really this :

"We, Daniel and Alexei, hereby certify that this code is 100% safe.

If you Eric, or anyone else, want to check, you'll have to grep in
appropriate places, and use your brain as we did."

I find very worrying all this stuff. This looks like you guys make everything possible to make it absolutely unmaintainable.

#5 lore raw

Re: [PATCH net-next v3 1/3] net: filter: simplify label names from jump-table

Quoted 21 lines · Eric Dumazet
Quoted 7 lines · Daniel Borkmann

Well, if you review the code itself in filter.c it makes it hard to read and review, with this patch, you'll immediately get the label name and what it actually does, so I think it's quite convenient and way more readable by itself. For grepping, you can always add subdirectories you're searching for, besides that I don't think that everything in the kernel needs to have unique names only so that one can grep for it among the whole tree.

Considering that Alexei claimed :

(quoted diff) +6 -0
+Internal BPF is a general purpose RISC instruction set. Not every register and
+every instruction are used during translation from original BPF to new format.
+For example, socket filters are not using 'exclusive add' instruction, but
+tracing filters may do to maintain counters of events, for example. Register R9
+is not used by socket filters either, but more complex filters may be running
+out of registers and would have to resort to spill/fill to stack.
Quoted 1 line

I tried to verify this claim and I could not.

I'm not sure what part are you referring to.
That xadd is not used right now? It's not. I think we agreed on it last week.
What else do you mean?

Quoted 3 lines

After your patch, it will be really this :

"We, Daniel and Alexei, hereby certify that this code is 100% safe.

I don't see where we say this.
Few paragraphs below the doc clearly says that it's a RISC instruction set and safety on instruction set comes from verifier which is TBD. Instruction set by itself is just an instruction set. Just like original BPF.

Quoted 5 lines

If you Eric, or anyone else, want to check, you'll have to grep in
appropriate places, and use your brain as we did."

I find very worrying all this stuff. This looks like you guys make everything possible to make it absolutely unmaintainable.

imo that's a harsh and unfair statement.
Calling it 'unmaintainable' just because you don't see how xadd can be used?
I'm working on tracing filters that will demonstrate that in a bit.

This particular Daniel's patch I think is a nice cleanup.
It indeed makes interpreter code shorter, less verbose and easier to read.

I suspect you think of this internal BPF format as it's solving only socket filters use case and all filters will be coming from user space. That's very narrow view. In case of tracing filters there is no BPF to send to kernel. User to kernel interface is a character string like "a==1 || b == 2". That is the filter. I'm parsing the string and converting it inside the kernel to internal bpf format,
so I would like to use full power of underlying cpu.
Think of internal BPF as 'uasm' that mips guys are using to construct tlb handler on the fly.
But this 'uasm' is generic for all architectures.
Eventually we may want to expose internal bpf to user space, only then verifier will be must have and we'll be discussing '100% safety'. Right now everything is internal and I'm pretty sure you understood very well how original bpf was translated to internal bpf, so I don't follow your concern about 'unmaintainable'.

At the same time it's impossible for me to follow what you guys are doing with tcp stack, but I'm not calling it unmaintainable just because I don't understand it.
If you feel more documentation is needed, sure. Please let us know what pieces are not explained well.
When I look at the doc by myself, it's hard to see what else is missing or what pieces are difficult to grasp.

Regards,
Alexei