BPF updates
Subsystems (6)
cover BPF updates
I had these cleanups still in my queue before the merge window. The set is against net-next tree, but with 83d5b7ef99 ("net: filter:
initialize A and X registers") applied on top of it, so a merge of net into net-next would be required *before* applying this set.
The main objective for these updates is that we get the code a bit more readable/comprehensible and avoid one additional instruction in the interpreter during fast-path.
Tested with Alexei's BPF test suite and seccomp test suite, no issues found.
Thanks!
Daniel Borkmann (5):
net: filter: simplify label names from jump-table
net: filter: misc/various cleanups
net: filter: get rid of sock_fprog_kern
net: filter: make register namings more comprehensible
net: filter: optimize BPF migration for ARG1/CTX handling
include/linux/filter.h | 60 ++++-- net/core/filter.c | 564 ++++++++++++++++++++++++------------------------- net/core/sock_diag.c | 4 +- 3 files changed, 325 insertions(+), 303 deletions(-)
Signature · 1 line
1.7.11.7
PATCH 1/5 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.
--- include/linux/filter.h | 3 + net/core/filter.c | 308 ++++++++++++++++++++++++------------------------- 2 files changed, 157 insertions(+), 154 deletions(-)
include/linux/filter.h +3 -0
@@ -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
PATCH 2/5 net: filter: misc/various cleanups
In reply to Daniel BorkmannThis contains only some minor misc cleanpus. We can spare us the extra variable declaration in __skb_get_pay_offset(), we can mark some unexpected conditions in the fast-path as unlikely(), 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.
--- net/core/filter.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-)
net/core/filter.c +10 -11
@@ -57,9 +57,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 ; }
@@ -68,6 +68,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); }
@@ -596,9 +597,7 @@ 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 ) ctx); }static u64 __skb_get_nlattr (u64 ctx,u64 A ,u64 X ,u64 r4,u64 r5)
@@ -609,10 +608,10 @@ static u64 __skb_get_nlattr(u64 ctx, u64 A, u64 X, u64 r4, u64 r5)if (skb_is_nonlinear (skb))return 0 ; -if (skb->len <sizeof (struct nlattr )) +if (unlikely (skb->len <sizeof (struct nlattr )))return 0 ; -if (A > skb->len -sizeof (struct nlattr )) +if (unlikely (A > skb->len -sizeof (struct nlattr )))return 0 ; nla =nla_find ((struct nlattr *) &skb->data[A ], skb->len -A ,X );
@@ -630,14 +629,14 @@ static u64 __skb_get_nlattr_nest(u64 ctx, u64 A, u64 X, u64 r4, u64 r5)if (skb_is_nonlinear (skb))return 0 ; -if (skb->len <sizeof (struct nlattr )) +if (unlikely (skb->len <sizeof (struct nlattr )))return 0 ; -if (A > skb->len -sizeof (struct nlattr )) +if (unlikely (A > skb->len -sizeof (struct nlattr )))return 0 ; nla = (struct nlattr *) &skb->data[A ]; -if (nla->nla_len > skb->len -A ) +if (unlikely (nla->nla_len > skb->len -A ))return 0 ; nla =nla_find_nested (nla,X );
@@ -655,7 +654,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,
@@ -1472,7 +1471,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
PATCH 3/5 net: filter: get rid of sock_fprog_kern
In reply to Daniel BorkmannIt is actually cleaner to just get rid of sock_fprog_kern structure. It's not really useful as we can just use sock_fprog structure as we do elsewhere in the kernel, this could throw some sparse false positives though, but getting rid of the structure duplication is probably the better way to go.
--- include/linux/filter.h | 13 +++++-------- net/core/filter.c | 24 ++++++++++++------------ net/core/sock_diag.c | 4 ++-- 3 files changed, 19 insertions(+), 22 deletions(-)
include/linux/filter.h +5 -8
@@ -67,11 +67,6 @@ struct compat_sock_fprog { };#endif -struct sock_fprog_kern { -u16 len; -struct sock_filter *filter; -}; -struct sk_buff ;struct sock ;struct seccomp_data ;
@@ -80,7 +75,7 @@ struct sk_filter {atomic_t refcnt; u32 jited:1, /* Is our filter JIT'ed? */ len:31; /* Number of filter blocks */ -struct sock_fprog_kern *orig_prog;/* Original BPF program */ +struct sock_fprog *orig_prog;/* Original BPF program */ struct rcu_head rcu;unsigned int (*bpf_func)(const struct sk_buff *skb,const struct sock_filter_int *filter);
@@ -97,8 +92,10 @@ static inline unsigned int sk_filter_size(unsigned int proglen) offsetof(struct sk_filter, insns[proglen])); } -#define sk_filter_proglen (fprog) \ - (fprog->len * sizeof(fprog->filter[0])) +static inline unsigned int sk_filter_prog_size (const struct sock_fprog *fprog) +{ +return fprog->len *sizeof (fprog->filter[0 ]); +}#define SK_RUN_FILTER (filter, ctx) \ (*filter->bpf_func)(ctx, filter->insnsi)
net/core/filter.c +12 -12
@@ -1396,17 +1396,17 @@ EXPORT_SYMBOL(sk_chk_filter);static int sk_store_orig_filter (struct sk_filter *fp,const struct sock_fprog *fprog) { -unsigned int fsize =sk_filter_proglen (fprog); -struct sock_fprog_kern *fkprog; +unsigned int fsize =sk_filter_prog_size (fprog); +struct sock_fprog *op; - fp->orig_prog =kmalloc (sizeof (*fkprog),GFP_KERNEL ); + fp->orig_prog =kmalloc (sizeof (*op),GFP_KERNEL );if (!fp->orig_prog)return -ENOMEM ; - fkprog = fp->orig_prog; - fkprog->len = fprog->len; - fkprog->filter =kmemdup (fp->insns, fsize,GFP_KERNEL ); -if (!fkprog->filter) { + op = fp->orig_prog; + op->len = fprog->len; + op->filter =kmemdup (fp->insns, fsize,GFP_KERNEL ); +if (!op->filter) {kfree (fp->orig_prog);return -ENOMEM ; }
@@ -1416,7 +1416,7 @@ static int sk_store_orig_filter(struct sk_filter *fp,static void sk_release_orig_filter (struct sk_filter *fp) { -struct sock_fprog_kern *fprog = fp->orig_prog; +struct sock_fprog *fprog = fp->orig_prog;if (fprog) {kfree (fprog->filter);
@@ -1599,7 +1599,7 @@ static struct sk_filter *__sk_prepare_filter(struct sk_filter *fp,int sk_unattached_filter_create (struct sk_filter **pfp,struct sock_fprog *fprog) { -unsigned int fsize =sk_filter_proglen (fprog); +unsigned int fsize =sk_filter_prog_size (fprog);struct sk_filter *fp;/* Make sure new filter is there and in the right amounts. */
@@ -1651,7 +1651,7 @@ EXPORT_SYMBOL_GPL(sk_unattached_filter_destroy);int sk_attach_filter (struct sock_fprog *fprog,struct sock *sk) {struct sk_filter *fp, *old_fp; -unsigned int fsize =sk_filter_proglen (fprog); +unsigned int fsize =sk_filter_prog_size (fprog);unsigned int sk_fsize =sk_filter_size (fprog->len);int err;
@@ -1799,7 +1799,7 @@ void sk_decode_filter(struct sock_filter *filt, struct sock_filter *to)int sk_get_filter (struct sock *sk,struct sock_filter __user *ubuf,unsigned int len) { -struct sock_fprog_kern *fprog; +struct sock_fprog *fprog;struct sk_filter *filter;int ret =0 ;
@@ -1824,7 +1824,7 @@ int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf, goto out; ret = -EFAULT ; -if (copy_to_user (ubuf, fprog->filter,sk_filter_proglen (fprog))) +if (copy_to_user (ubuf, fprog->filter,sk_filter_prog_size (fprog))) goto out;/* Instead of bytes, the API requests to return the number
net/core/sock_diag.c +2 -2
@@ -52,7 +52,7 @@ EXPORT_SYMBOL_GPL(sock_diag_put_meminfo);int sock_diag_put_filterinfo (struct user_namespace *user_ns,struct sock *sk,struct sk_buff *skb,int attrtype) { -struct sock_fprog_kern *fprog; +struct sock_fprog *fprog;struct sk_filter *filter;struct nlattr *attr;unsigned int flen;
@@ -69,7 +69,7 @@ int sock_diag_put_filterinfo(struct user_namespace *user_ns, struct sock *sk, goto out; fprog = filter->orig_prog; - flen =sk_filter_proglen (fprog); + flen =sk_filter_prog_size (fprog); attr =nla_reserve (skb, attrtype, flen);if (attr ==NULL ) {
Signature · 1 line
1.7.11.7
PATCH 4/5 net: filter: make register namings more comprehensible
In reply to Daniel BorkmannThe 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. Since for BPF helper functions for BPF_CALL we already use small letters, so be consistent here as well.
--- include/linux/filter.h | 44 ++++++++-- net/core/filter.c | 215 +++++++++++++++++++++++++------------------------ 2 files changed, 145 insertions(+), 114 deletions(-)
include/linux/filter.h +36 -8
@@ -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 */
@@ -97,9 +128,6 @@ static inline unsigned int sk_filter_prog_size(const struct sock_fprog *fprog)return 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
498 lines not on this page · raw message
Signature · 1 line
1.7.11.7
PATCH 5/5 net: filter: optimize BPF migration for ARG1/CTX handling
In reply to Daniel BorkmannCurrently, at initial setup in __sk_run_filter() we initialize the BPF stack's frame-pointer and CTX register. However, instead of the CTX register, we initialize context to ARG1, and during user filter migration we emit *always* an instruction that copies the content from ARG1 to CTX. ARG1 is needed in BPF_CALL instructions to setup ctx, A, X for call emission. However, we nevertheless copy CTX over to ARG1 in these cases. So all in all, we always emit one instruction at BPF program beginning that should have actually been avoided to spare this overhead.
--- net/core/filter.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-)
net/core/filter.c +1 -9
@@ -62,7 +62,6 @@#define A regs[insn->a_reg]#define X regs[insn->x_reg]#define FP regs[BPF_REG_FP] -#define ARG1 regs[BPF_REG_ARG1]#define CTX regs[BPF_REG_CTX]#define K insn->imm
@@ -257,7 +256,7 @@ unsigned int __sk_run_filter(void *ctx, const struct sock_filter_int *insn)#define CONT_JMP ({ insn++; goto select_insn; })FP = (u64 ) (unsigned long ) &stack[ARRAY_SIZE (stack)]; -ARG1 = (u64 ) (unsigned long ) ctx; +CTX = (u64 ) (unsigned long ) ctx;/* Register for user BPF programs need to be reset first. */ regs[BPF_REG_A ] =0 ;
@@ -896,13 +895,6 @@ do_pass: new_insn = new_prog; fp = prog; -if (new_insn) { - new_insn->code =BPF_ALU64 |BPF_MOV |BPF_X ; - new_insn->a_reg =BPF_REG_CTX ; - new_insn->x_reg =BPF_REG_ARG1 ; - } - new_insn++; -for (i =0 ; i < len; fp++, i++) {struct sock_filter_int tmp_insns[6 ] = { };struct sock_filter_int *insn = tmp_insns;
Signature · 1 line
1.7.11.7
Re: [PATCH net-next 5/5] net: filter: optimize BPF migration for ARG1/CTX handling
Quoted 10 lines · Daniel Borkmann
Currently, at initial setup in
__sk_run_filter()we initialize the BPF stack's frame-pointer and CTX register. However, instead of the CTX register, we initialize context to ARG1, and during user filter migration we emit *always* an instruction that copies the content from ARG1 to CTX. ARG1 is needed inBPF_CALLinstructions to setup ctx, A, X for call emission. However, we nevertheless copy CTX over to ARG1 in these cases. So all in all, we always emit one instruction at BPF program beginning that should have actually been avoided to spare this overhead.
Quoted 2 lines
Signed-off-by Daniel Borkmann <[email protected]>Cc Alexei Starovoitov <[email protected]>
First 4 patches look great, but this one I have to disagree. See below.
Quoted 17 lines
--- net/core/filter.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-)net/core/filter.c +0
-1@@ -62,7 +62,6 @@#define A regs[insn->a_reg]#define X regs[insn->x_reg]#define FP regs[BPF_REG_FP] -#define ARG1 regs[BPF_REG_ARG1]#define CTX regs[BPF_REG_CTX]#define K insn->imm
net/core/filter.c unsigned int __sk_run_filter(void *ctx, const struct sock_filter_int *insn) · line 259
Quoted 5 lines
net/core/filter.c +1
-1@@ -257,7 +256,7 @@ unsigned int __sk_run_filter(void *ctx, const struct sock_filter_int *insn)#define CONT_JMP ({ insn++; goto select_insn; })FP = (u64 ) (unsigned long ) &stack[ARRAY_SIZE (stack)]; -ARG1 = (u64 ) (unsigned long ) ctx; +CTX = (u64 ) (unsigned long ) ctx;
R1 (ARG1) is the register that used to pass first argument to the function. For seamless kernel->bpf->kernel transition we have to follow calling convention, so 'void *ctx' has to go into R1 by design.
Storing it into R6 (CTX) will only work for classic filters converted to extended.
all native ebpf filters will be broken.
In documentation we say:
* R1 - R5 - arguments from BPF program to in-kernel function
so llvm/gcc are following this ebpf ABI.
Calling convention is the same whether to call kernel function from ebpf or ebpf from kernel. So 1st argument (void *ctx) has to go into R1.
By convention ld_abs/ld_ind insns are using implicit input register 'ctx' (R6), that's why we do a copy from R1 into R6 as a first insn of the _converted_ filter.
The cost of one insn is not zero, yes, but cost of imbalanced ABI is huge. For example, I don't know how to teach llvm/gcc to have different ABIs in such cases.
This dual ABI makes JITs ugly too, since now they must remember to emit R6=R1 copy. I would like JITs to worry about instructions only and not to deal with such discrepancies.
The whole thing becomes unclean when kernel to ebpf passes 1st argument in R6, but ebpf to kernel passes 1st argument in R1.
So I would suggest to drop this one for now.
I also want to avoid extra generated R6=R1 for converted filters. Let's think it through first.
Quoted 19 lines
net/core/filter.c +0
-8/* Register for user BPF programs need to be reset first. */ regs[BPF_REG_A ] =0 ;@@ -896,13 +895,6 @@ do_pass: new_insn = new_prog; fp = prog; -if (new_insn) { - new_insn->code =BPF_ALU64 |BPF_MOV |BPF_X ; - new_insn->a_reg =BPF_REG_CTX ; - new_insn->x_reg =BPF_REG_ARG1 ; - } - new_insn++; -for (i =0 ; i < len; fp++, i++) {struct sock_filter_int tmp_insns[6 ] = { };struct sock_filter_int *insn = tmp_insns; --1.7.11.7
Re: [PATCH net-next 5/5] net: filter: optimize BPF migration for ARG1/CTX handling
Quoted 33 lines · Alexei Starovoitov
Quoted 10 lines · Daniel Borkmann
Currently, at initial setup in
__sk_run_filter()we initialize the BPF stack's frame-pointer and CTX register. However, instead of the CTX register, we initialize context to ARG1, and during user filter migration we emit *always* an instruction that copies the content from ARG1 to CTX. ARG1 is needed inBPF_CALLinstructions to setup ctx, A, X for call emission. However, we nevertheless copy CTX over to ARG1 in these cases. So all in all, we always emit one instruction at BPF program beginning that should have actually been avoided to spare this overhead.Quoted 2 lines
Signed-off-by Daniel Borkmann <[email protected]>Cc Alexei Starovoitov <[email protected]>First 4 patches look great, but this one I have to disagree. See below.
Quoted 16 lines
--- net/core/filter.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-)net/core/filter.c +0
-1@@ -62,7 +62,6 @@#define A regs[insn->a_reg]#define X regs[insn->x_reg]#define FP regs[BPF_REG_FP] -#define ARG1 regs[BPF_REG_ARG1]#define CTX regs[BPF_REG_CTX]#define K insn->imm
Quoted 8 lines
net/core/filter.c unsigned int __sk_run_filter(void *ctx, const struct sock_filter_int *insn) · line 259
Quoted 6 lines
net/core/filter.c +1
-1@@ -257,7 +256,7 @@ unsigned int __sk_run_filter(void *ctx, const struct sock_filter_int *insn)#define CONT_JMP ({ insn++; goto select_insn; })FP = (u64 ) (unsigned long ) &stack[ARRAY_SIZE (stack)]; -ARG1 = (u64 ) (unsigned long ) ctx; +CTX = (u64 ) (unsigned long ) ctx;R1 (ARG1) is the register that used to pass first argument to the function.
Yes that's clear. Which is why f.e. in convert_bpf_extensions() we currently copy ctx over to arg1 for calls, i.e.:
/* arg1 = ctx */ insn->code = BPF_ALU64 | BPF_MOV | BPF_X; insn->a_reg = ARG1_REG; insn->x_reg = CTX_REG; insn++;
Quoted 5 lines
For seamless
kernel->bpf->kerneltransition we have to follow calling convention, so 'void *ctx' has to go into R1 by design. Storing it into R6 (CTX) will only work for classic filters converted to extended.
all native ebpf filters will be broken.
My objection was that currently, we do _not_ have _any_ users or even kernel API for _native_ filters, at least not in mainline tree. The _main_ users we have are currently _all_ being converted, hence this patch. Given that these calls have likely just a minority of use cases triggered by tcpdump et al, the majority of users still need to do this overhead/additional work.
Quoted 5 lines
In documentation we say:
* R1 - R5 - arguments from BPF program to in-kernel function
so llvm/gcc are following this ebpf ABI.
Calling convention is the same whether to call kernel function from ebpf or ebpf from kernel. So 1st argument (void *ctx) has to go into R1.
Yes, that's clear and convert_bpf_extensions() is doing that. So far we're not using llvm/gcc backend here and have the internal instruction set not exposed to user space, but even there you would need to prepare R1 - R5 to hand-over arguments for the BPF_CALL insns, so why can't we load CTX into R1 at that time just as we do with convert_bpf_extensions()?
Quoted 3 lines
By convention ld_abs/ld_ind insns are using implicit input register 'ctx' (R6), that's why we do a copy from R1 into R6 as a first insn of the _converted_ filter.
Yep, and R6 stays as is here. So ld_abs/ld_ind insns are correctly using 'ctx'.
Best,
Daniel
Re: [PATCH net-next 5/5] net: filter: optimize BPF migration for ARG1/CTX handling
Quoted 83 lines · Daniel Borkmann
Quoted 41 lines · Alexei Starovoitov
Quoted 11 lines · Daniel Borkmann
Currently, at initial setup in
__sk_run_filter()we initialize the BPF stack's frame-pointer and CTX register. However, instead of the CTX register, we initialize context to ARG1, and during user filter migration we emit *always* an instruction that copies the content from ARG1 to CTX. ARG1 is needed inBPF_CALLinstructions to setup ctx, A, X for call emission. However, we nevertheless copy CTX over to ARG1 in these cases. So all in all, we always emit one instruction at BPF program beginning that should have actually been avoided to spare this overhead.Quoted 2 lines
Signed-off-by Daniel Borkmann <[email protected]>Cc Alexei Starovoitov <[email protected]>First 4 patches look great, but this one I have to disagree. See below.
Quoted 20 lines
--- net/core/filter.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-)net/core/filter.c +0
-1@@ -62,7 +62,6 @@#define A regs[insn->a_reg]#define X regs[insn->x_reg]#define FP regs[BPF_REG_FP] -#define ARG1 regs[BPF_REG_ARG1]#define CTX regs[BPF_REG_CTX]#define K insn->imm@@ -257,7 +256,7 @@ unsigned int __sk_run_filter(void *ctx, const struct sock_filter_int *insn)#define CONT_JMP ({ insn++; goto select_insn; })Quoted 7 lines
net/core/filter.c unsigned int __sk_run_filter(void *ctx, const struct sock_filter_int *insn) · line 259
Quoted 3 lines
net/core/filter.c +1
-1FP = (u64 ) (unsigned long ) &stack[ARRAY_SIZE (stack)]; -ARG1 = (u64 ) (unsigned long ) ctx; +CTX = (u64 ) (unsigned long ) ctx;R1 (ARG1) is the register that used to pass first argument to the function.
Yes that's clear. Which is why f.e. in
convert_bpf_extensions()we currently copy ctx over to arg1 for calls, i.e.:/* arg1 = ctx */
insn->code = BPF_ALU64 | BPF_MOV | BPF_X; insn->a_reg = ARG1_REG; insn->x_reg = CTX_REG; insn++;Quoted 5 lines
For seamless
kernel->bpf->kerneltransition we have to follow calling convention, so 'void *ctx' has to go into R1 by design. Storing it into R6 (CTX) will only work for classic filters converted to extended.
all native ebpf filters will be broken.My objection was that currently, we do _not_ have _any_ users or even kernel API for _native_ filters, at least not in mainline tree. The _main_ users we have are currently _all_ being converted, hence this patch. Given that these calls have likely just a minority of use cases triggered by tcpdump et al, the majority of users still need to do this overhead/additional work.
Quoted 5 lines
In documentation we say:
* R1 - R5 - arguments from BPF program to in-kernel function
so llvm/gcc are following this ebpf ABI.
Calling convention is the same whether to call kernel function from ebpf or ebpf from kernel. So 1st argument (void *ctx) has to go into R1.
Quoted 5 lines
Yes, that's clear and
convert_bpf_extensions()is doing that. So far we're not using llvm/gcc backend here and have the internal instruction set not exposed to user space, but even there you would need to prepare R1 - R5 to hand-over arguments for theBPF_CALLinsns, so why can't we load CTX into R1 at that time just as we do withconvert_bpf_extensions()?
How about then removing extra generated R6=R1 from converted and do both in __sk_run_filter() ?
regs[ARG1_REG] = (u64) (unsigned long) ctx;
regs[CTX_REG] = (u64) (unsigned long) ctx;Overhead problem will be solved and ABI is still clean.
Quoted 12 lines
Quoted 4 lines
By convention ld_abs/ld_ind insns are using implicit input register 'ctx' (R6),
that's why we do a copy from R1 into R6 as a first insn of the _converted_ filter.Yep, and R6 stays as is here. So ld_abs/ld_ind insns are correctly using 'ctx'.
Best,
Daniel
Re: [PATCH net-next 5/5] net: filter: optimize BPF migration for ARG1/CTX handling
Quoted 83 lines · Alexei Starovoitov
Quoted 76 lines · Daniel Borkmann
Quoted 39 lines · Alexei Starovoitov
Quoted 11 lines · Daniel Borkmann
Currently, at initial setup in
__sk_run_filter()we initialize the BPF stack's frame-pointer and CTX register. However, instead of the CTX register, we initialize context to ARG1, and during user filter migration we emit *always* an instruction that copies the content from ARG1 to CTX. ARG1 is needed inBPF_CALLinstructions to setup ctx, A, X for call emission. However, we nevertheless copy CTX over to ARG1 in these cases. So all in all, we always emit one instruction at BPF program beginning that should have actually been avoided to spare this overhead.Quoted 2 lines
Signed-off-by Daniel Borkmann <[email protected]>Cc Alexei Starovoitov <[email protected]>First 4 patches look great, but this one I have to disagree. See below.
Quoted 20 lines
--- net/core/filter.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-)net/core/filter.c +0
-1@@ -62,7 +62,6 @@#define A regs[insn->a_reg]#define X regs[insn->x_reg]#define FP regs[BPF_REG_FP] -#define ARG1 regs[BPF_REG_ARG1]#define CTX regs[BPF_REG_CTX]#define K insn->imm@@ -257,7 +256,7 @@ unsigned int __sk_run_filter(void *ctx, const struct sock_filter_int *insn)#define CONT_JMP ({ insn++; goto select_insn; })Quoted 7 lines
net/core/filter.c unsigned int __sk_run_filter(void *ctx, const struct sock_filter_int *insn) · line 259
Quoted 3 lines
net/core/filter.c +1
-1FP = (u64 ) (unsigned long ) &stack[ARRAY_SIZE (stack)]; -ARG1 = (u64 ) (unsigned long ) ctx; +CTX = (u64 ) (unsigned long ) ctx;R1 (ARG1) is the register that used to pass first argument to the function.
Yes that's clear. Which is why f.e. in
convert_bpf_extensions()we currently copy ctx over to arg1 for calls, i.e.:/* arg1 = ctx */ insn->code = BPF_ALU64 | BPF_MOV | BPF_X; insn->a_reg = ARG1_REG; insn->x_reg = CTX_REG; insn++;Quoted 5 lines
For seamless
kernel->bpf->kerneltransition we have to follow calling convention, so 'void *ctx' has to go into R1 by design. Storing it into R6 (CTX) will only work for classic filters converted to extended.
all native ebpf filters will be broken.My objection was that currently, we do _not_ have _any_ users or even kernel API for _native_ filters, at least not in mainline tree. The _main_ users we have are currently _all_ being converted, hence this patch. Given that these calls have likely just a minority of use cases triggered by tcpdump et al, the majority of users still need to do this overhead/additional work.
Quoted 5 lines
In documentation we say:
* R1 - R5 - arguments from BPF program to in-kernel function
so llvm/gcc are following this ebpf ABI.
Calling convention is the same whether to call kernel function from ebpf or ebpf from kernel. So 1st argument (void *ctx) has to go into R1.Quoted 5 lines
Yes, that's clear and
convert_bpf_extensions()is doing that. So far we're not using llvm/gcc backend here and have the internal instruction set not exposed to user space, but even there you would need to prepare R1 - R5 to hand-over arguments for theBPF_CALLinsns, so why can't we load CTX into R1 at that time just as we do withconvert_bpf_extensions()?
Quoted 6 lines
How about then removing extra generated R6=R1 from converted and do both in
__sk_run_filter()?regs[ARG1_REG] = (u64) (unsigned long) ctx; regs[CTX_REG] = (u64) (unsigned long) ctx;Overhead problem will be solved and ABI is still clean.
Well, I just don't understand the concerns of ABI here. Given that we do not have any native BPF code to maintain in the kernel tree and given that we currently do not expose the instruction set to user space, we're free to do what we want, no? Eventually what's in mainline kernel is that dictates an ABI, so far we have it only in kernel space and the only current user of the ABI is the conversion function from user BPF programs. Given that helper function calls may happen less often than executing instructions from the rest of the code, why can't your llvm/gcc backend emit the load of ctx into arg1? JITs don't need to treat that differently in any way, imho. Simply the one who is generating the BPF insns needs to emit ctx into arg1 just as he prepares the other argX registers. Btw, since we know a priori that __skb_get_pay_offset() and __get_raw_cpu_id() are not making use of all prepared registers, we could even go that far and avoid preparing loads for the two.
Re: [PATCH net-next 5/5] net: filter: optimize BPF migration for ARG1/CTX handling
Quoted 112 lines · Daniel Borkmann
Quoted 103 lines · Alexei Starovoitov
Quoted 90 lines · Daniel Borkmann
Quoted 43 lines · Alexei Starovoitov
Quoted 12 lines · Daniel Borkmann
Currently, at initial setup in
__sk_run_filter()we initialize the BPF stack's frame-pointer and CTX register. However, instead of the CTX register, we initialize context to ARG1, and during user filter migration we emit *always* an instruction that copies the content from ARG1 to CTX. ARG1 is needed inBPF_CALLinstructions to setup ctx, A, X for call emission. However, we nevertheless copy CTX over to ARG1 in these cases. So all in all, we always emit one instruction at BPF program beginning that should have actually been avoided to spare this overhead.Quoted 2 lines
Signed-off-by Daniel Borkmann <[email protected]>Cc Alexei Starovoitov <[email protected]>First 4 patches look great, but this one I have to disagree. See below.
Quoted 21 lines
--- net/core/filter.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-)net/core/filter.c +0
-1@@ -62,7 +62,6 @@#define A regs[insn->a_reg]#define X regs[insn->x_reg]#define FP regs[BPF_REG_FP] -#define ARG1 regs[BPF_REG_ARG1]#define CTX regs[BPF_REG_CTX]#define K insn->imm@@ -257,7 +256,7 @@ unsigned int __sk_run_filter(void *ctx, conststruct
sock_filter_int*insn)
#defineCONT_JMP({ insn++; gotoselect_insn; })Quoted 8 lines
net/core/filter.c unsigned int __sk_run_filter(void *ctx, const struct sock_filter_int *insn) · line 259
Quoted 3 lines
FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)];net/core/filter.c +1
-1-ARG1 = (u64 ) (unsigned long ) ctx; +CTX = (u64 ) (unsigned long ) ctx;R1 (ARG1) is the register that used to pass first argument to the function.
Yes that's clear. Which is why f.e. in
convert_bpf_extensions()we currently
copy ctx over to arg1 for calls, i.e.:/* arg1 = ctx */ insn->code = BPF_ALU64 | BPF_MOV | BPF_X; insn->a_reg = ARG1_REG; insn->x_reg = CTX_REG; insn++;Quoted 5 lines
For seamless
kernel->bpf->kerneltransition we have to follow calling convention, so 'void *ctx' has to go into R1 by design. Storing it into R6 (CTX) will only work for classic filters converted to extended.
all native ebpf filters will be broken.My objection was that currently, we do _not_ have _any_ users or even kernel
API for _native_ filters, at least not in mainline tree. The _main_ users we
have are currently _all_ being converted, hence this patch. Given that these
calls have likely just a minority of use cases triggered by tcpdump et al,
the majority of users still need to do this overhead/additional work.Quoted 5 lines
In documentation we say:
* R1 - R5 - arguments from BPF program to in-kernel function
so llvm/gcc are following this ebpf ABI.
Calling convention is the same whether to call kernel function from ebpf or ebpf from kernel. So 1st argument (void *ctx) has to go into R1.Quoted 8 lines
Yes, that's clear and
convert_bpf_extensions()is doing that. So far we're
not using llvm/gcc backend here and have the internal instruction set not exposed to user space, but even there you would need to prepare R1 - R5 to
hand-over arguments for theBPF_CALLinsns, so why can't we load CTX into R1
at that time just as we do withconvert_bpf_extensions()?Quoted 6 lines
How about then removing extra generated R6=R1 from converted and do both in
__sk_run_filter()?regs[ARG1_REG] = (u64) (unsigned long) ctx; regs[CTX_REG] = (u64) (unsigned long) ctx;Overhead problem will be solved and ABI is still clean.
Quoted 6 lines
Well, I just don't understand the concerns of ABI here. Given that we do not have any native BPF code to maintain in the kernel tree and given that we currently do not expose the instruction set to user space, we're free to do what we want, no? Eventually what's in mainline kernel is that dictates an ABI, so far we have it only in kernel space and the only current user of the ABI is the conversion function from user BPF programs. Given that helper
well, all the patches for llvm and the rest were posted. Obviously they cannot go in at once, but breaking this ABI right now makes it impossible to continue working on them.
I want to make ebpf engine to be useful for tracing filters and so on. Are you saying forget it, this is classic bpf engine only?
Quoted 3 lines
function calls may happen less often than executing instructions from the rest of the code, why can't your llvm/gcc backend emit the load of ctx into arg1? JITs don't need to treat that differently in any way, imho. Simply
I don't think we're on the same page here. compiler at the end is just a piece of sw and can do everything, but imbalanced ebpf ABI is really out of normal compiler work flow.
Pretty much compiler somehow need to generate one way of passing arguments into a function, but inside the function it needs to expect them in different registers?! That is practically impossible to do in normal gcc/llvm.
Even without any compiler insight.
If you insist on ctx to be in R6 only, then please kill the whole concept of ABI in the doc. It doesn't make sense to pass a value into a function in R1, but the function will see in R6?!
Quoted 5 lines
the one who is generating the BPF insns needs to emit ctx into arg1 just as he prepares the other argX registers. Btw, since we know a priori that
__skb_get_pay_offset()and__get_raw_cpu_id()are not making use of all prepared registers, we could even go that far and avoid preparing loads for the two.
Re: [PATCH net-next 5/5] net: filter: optimize BPF migration for ARG1/CTX handling
Quoted 130 lines · Alexei Starovoitov
Quoted 112 lines · Daniel Borkmann
Quoted 103 lines · Alexei Starovoitov
Quoted 90 lines · Daniel Borkmann
Quoted 43 lines · Alexei Starovoitov
Quoted 12 lines · Daniel Borkmann
Currently, at initial setup in
__sk_run_filter()we initialize the BPF stack's frame-pointer and CTX register. However, instead of the CTX register, we initialize context to ARG1, and during user filter migration we emit *always* an instruction that copies the content from ARG1 to CTX. ARG1 is needed inBPF_CALLinstructions to setup ctx, A, X for call emission. However, we nevertheless copy CTX over to ARG1 in these cases. So all in all, we always emit one instruction at BPF program beginning that should have actually been avoided to spare this overhead.Quoted 2 lines
Signed-off-by Daniel Borkmann <[email protected]>Cc Alexei Starovoitov <[email protected]>First 4 patches look great, but this one I have to disagree. See below.
Quoted 21 lines
--- net/core/filter.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-)net/core/filter.c +0
-1@@ -62,7 +62,6 @@#define A regs[insn->a_reg]#define X regs[insn->x_reg]#define FP regs[BPF_REG_FP] -#define ARG1 regs[BPF_REG_ARG1]#define CTX regs[BPF_REG_CTX]#define K insn->imm@@ -257,7 +256,7 @@ unsigned int __sk_run_filter(void *ctx, conststruct
sock_filter_int*insn)
#defineCONT_JMP({ insn++; gotoselect_insn; })Quoted 8 lines
net/core/filter.c
Quoted 3 lines
FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)];net/core/filter.c +1
-1-ARG1 = (u64 ) (unsigned long ) ctx; +CTX = (u64 ) (unsigned long ) ctx;R1 (ARG1) is the register that used to pass first argument to the function.
Yes that's clear. Which is why f.e. in
convert_bpf_extensions()we currently
copy ctx over to arg1 for calls, i.e.:/* arg1 = ctx */ insn->code = BPF_ALU64 | BPF_MOV | BPF_X; insn->a_reg = ARG1_REG; insn->x_reg = CTX_REG; insn++;Quoted 5 lines
For seamless
kernel->bpf->kerneltransition we have to follow calling convention, so 'void *ctx' has to go into R1 by design. Storing it into R6 (CTX) will only work for classic filters converted to extended.
all native ebpf filters will be broken.My objection was that currently, we do _not_ have _any_ users or even kernel
API for _native_ filters, at least not in mainline tree. The _main_ users we
have are currently _all_ being converted, hence this patch. Given that these
calls have likely just a minority of use cases triggered by tcpdump et al,
the majority of users still need to do this overhead/additional work.Quoted 5 lines
In documentation we say:
* R1 - R5 - arguments from BPF program to in-kernel function
so llvm/gcc are following this ebpf ABI.
Calling convention is the same whether to call kernel function from ebpf or ebpf from kernel. So 1st argument (void *ctx) has to go into R1.Quoted 8 lines
Yes, that's clear and
convert_bpf_extensions()is doing that. So far we're
not using llvm/gcc backend here and have the internal instruction set not exposed to user space, but even there you would need to prepare R1 - R5 to
hand-over arguments for theBPF_CALLinsns, so why can't we load CTX into R1
at that time just as we do withconvert_bpf_extensions()?Quoted 6 lines
How about then removing extra generated R6=R1 from converted and do both in
__sk_run_filter()?regs[ARG1_REG] = (u64) (unsigned long) ctx; regs[CTX_REG] = (u64) (unsigned long) ctx;Overhead problem will be solved and ABI is still clean.
Quoted 6 lines
Well, I just don't understand the concerns of ABI here. Given that we do not have any native BPF code to maintain in the kernel tree and given that we currently do not expose the instruction set to user space, we're free to do what we want, no? Eventually what's in mainline kernel is that dictates an ABI, so far we have it only in kernel space and the only current user of the ABI is the conversion function from user BPF programs. Given that helper
well, all the patches for llvm and the rest were posted. Obviously they cannot go in at once, but breaking this ABI right now makes it impossible to continue working on them.
I want to make ebpf engine to be useful for tracing filters and so on. Are you saying forget it, this is classic bpf engine only?Quoted 3 lines
function calls may happen less often than executing instructions from the rest of the code, why can't your llvm/gcc backend emit the load of ctx into arg1? JITs don't need to treat that differently in any way, imho. Simply
Quoted 7 lines
I don't think we're on the same page here. compiler at the end is just a piece of sw and can do everything, but imbalanced ebpf ABI is really out of normal compiler work flow.
Pretty much compiler somehow need to generate one way of passing arguments into a function, but inside the function it needs to expect them in different registers?! That is practically impossible to do in normal gcc/llvm.
Have to correct myself here.
sparc is an architecture where args are passed in %o regs and received by the callee in %i regs, so there is a precedent. but it's a unique sliding register window architecture. ebpf ABI is picked carefully to match x86/arm ABI one to one where in/out regs are the same.
The following patch does the same.
It saves one extra copy insn and doesn't break ABI. imo much better solution.
net/core/filter.c +0 -0
@@ -242,6 +242,7 @@ unsigned int __sk_run_filter(void *ctx, const
struct sock_filter_int *insn)
regs[FP_REG] = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)];
regs[ARG1_REG] = (u64) (unsigned long) ctx;+ regs[CTX_REG] = regs[ARG1_REG];
select_insn:
goto *jumptable[insn->code];
@@ -893,13 +894,6 @@ do_pass:
new_insn = new_prog;
fp = prog;- if (new_insn) {
- new_insn->code = BPF_ALU64 | BPF_MOV | BPF_X;
- new_insn->a_reg = CTX_REG;
- new_insn->x_reg = ARG1_REG;
- }
- new_insn++;
-
for (i = 0; i < len; fp++, i++) {
struct sock_filter_int tmp_insns[6] = { };
struct sock_filter_int *insn = tmp_insns;Quoted 10 lines
Even without any compiler insight.
If you insist on ctx to be in R6 only, then please kill the whole concept of ABI in the doc. It doesn't make sense to pass a value into a function in R1, but the function will see in R6?!Quoted 5 lines
the one who is generating the BPF insns needs to emit ctx into arg1 just as he prepares the other argX registers. Btw, since we know a priori that
__skb_get_pay_offset()and__get_raw_cpu_id()are not making use of all prepared registers, we could even go that far and avoid preparing loads for the two.
Re: [PATCH net-next 5/5] net: filter: optimize BPF migration for ARG1/CTX handling
In reply to Alexei StarovoitovOn 04/24/2014 01:14 AM, Alexei Starovoitov wrote:
...
Quoted 7 lines
Quoted 6 lines
Well, I just don't understand the concerns of ABI here. Given that we do not have any native BPF code to maintain in the kernel tree and given that we currently do not expose the instruction set to user space, we're free to do what we want, no? Eventually what's in mainline kernel is that dictates an ABI, so far we have it only in kernel space and the only current user of the ABI is the conversion function from user BPF programs. Given that helper
Quoted 5 lines
well, all the patches for llvm and the rest were posted. Obviously they cannot go in at once, but breaking this ABI right now makes it impossible to continue working on them.
I want to make ebpf engine to be useful for tracing filters and so on. Are you saying forget it, this is classic bpf engine only?
Nope, I'm not saying that at all! As tracing has something similar, yes, I already stated that it would probably be a good idea, i.e. when we could JIT compile it.
Quoted 4 lines
Quoted 3 lines
function calls may happen less often than executing instructions from the rest of the code, why can't your llvm/gcc backend emit the load of ctx into arg1? JITs don't need to treat that differently in any way, imho. Simply
Quoted 11 lines
I don't think we're on the same page here. compiler at the end is just a piece of sw and can do everything, but imbalanced ebpf ABI is really out of normal compiler work flow.
Pretty much compiler somehow need to generate one way of passing arguments into a function, but inside the function it needs to expect them in different registers?! That is practically impossible to do in normal gcc/llvm.
Even without any compiler insight.
If you insist on ctx to be in R6 only, then please kill the whole concept of ABI in the doc. It doesn't make sense to pass a value into a function in R1, but the function will see in R6?!
Sorry, I don't think that this is what I claimed. I don't have a strong opinion on this particular patch, just wanted to have clarified what we have with ABI here as there's no particular freeze from the code we have in the kernel. Anyway, I'll change it into the other variant and respin the set, as that will already spare us a bit unnecessary of work in fast-path.
Quoted 5 lines
Quoted 5 lines
the one who is generating the BPF insns needs to emit ctx into arg1 just as he prepares the other argX registers. Btw, since we know a priori that
__skb_get_pay_offset()and__get_raw_cpu_id()are not making use of all prepared registers, we could even go that far and avoid preparing loads for the two.
RE: [PATCH net-next 5/5] net: filter: optimize BPF migration for ARG1/CTX handling
In reply to Alexei StarovoitovFrom: Alexei Starovoitov
...
Quoted 3 lines
Have to correct myself here.
sparc is an architecture where args are passed in %o regs and received by the callee in %i regs, so there is a precedent.
...
Not actually true, the parameters are still in the %o regs in the called function.
It is the called function that rotates the register window, not the act of making the function call.
Small leaf functions will typically use the existing register window.
David
David Miller
maintainerRe: [PATCH net-next 5/5] net: filter: optimize BPF migration for ARG1/CTX handling
From: David Laight <[email protected]>
Date: Thu, 24 Apr 2014 08:28:56 +0000
Quoted 5 lines
From: Alexei Starovoitov
...Quoted 3 lines
Have to correct myself here.
sparc is an architecture where args are passed in %o regs and received by the callee in %i regs, so there is a precedent.
Quoted 8 lines
...
Not actually true, the parameters are still in the %o regs in the called function.
It is the called function that rotates the register window, not the act of making the function call.
Small leaf functions will typically use the existing register window.
Correct.