diff mbox series

[bpf-next,1/2] bpf: try to avoid kzalloc in cgroup/{s,g}etsockopt

Message ID 20201217172324.2121488-2-sdf@google.com
State New
Headers show
Series bpf: misc performance improvements for cgroup hooks | expand

Commit Message

Stanislav Fomichev Dec. 17, 2020, 5:23 p.m. UTC
When we attach a bpf program to cgroup/getsockopt any other getsockopt()
syscall starts incurring kzalloc/kfree cost. While, in general, it's
not an issue, sometimes it is, like in the case of TCP_ZEROCOPY_RECEIVE.
TCP_ZEROCOPY_RECEIVE (ab)uses getsockopt system call to implement
fastpath for incoming TCP, we don't want to have extra allocations in
there.

Let add a small buffer on the stack and use it for small (majority)
{s,g}etsockopt values. I've started with 128 bytes to cover
the options we care about (TCP_ZEROCOPY_RECEIVE which is 32 bytes
currently, with some planned extension to 64 + some headroom
for the future).

It seems natural to do the same for setsockopt, but it's a bit more
involved when the BPF program modifies the data (where we have to
kmalloc). The assumption is that for the majority of setsockopt
calls (which are doing pure BPF options or apply policy) this
will bring some benefit as well.

Signed-off-by: Stanislav Fomichev <sdf@google.com>
---
 include/linux/filter.h |  3 +++
 kernel/bpf/cgroup.c    | 41 +++++++++++++++++++++++++++++++++++++++--
 2 files changed, 42 insertions(+), 2 deletions(-)

Comments

Song Liu Dec. 21, 2020, 10:22 p.m. UTC | #1
On Thu, Dec 17, 2020 at 9:24 AM Stanislav Fomichev <sdf@google.com> wrote:
>

> When we attach a bpf program to cgroup/getsockopt any other getsockopt()

> syscall starts incurring kzalloc/kfree cost. While, in general, it's

> not an issue, sometimes it is, like in the case of TCP_ZEROCOPY_RECEIVE.

> TCP_ZEROCOPY_RECEIVE (ab)uses getsockopt system call to implement

> fastpath for incoming TCP, we don't want to have extra allocations in

> there.

>

> Let add a small buffer on the stack and use it for small (majority)

> {s,g}etsockopt values. I've started with 128 bytes to cover

> the options we care about (TCP_ZEROCOPY_RECEIVE which is 32 bytes

> currently, with some planned extension to 64 + some headroom

> for the future).


I don't really know the rule of thumb, but 128 bytes on stack feels too big to
me. I would like to hear others' opinions on this. Can we solve the problem
with some other mechanisms, e.g. a mempool?

[...]

>

> +static void *sockopt_export_buf(struct bpf_sockopt_kern *ctx)

> +{

> +       void *p;

> +

> +       if (ctx->optval != ctx->buf)

> +               return ctx->optval;

> +

> +       /* We've used bpf_sockopt_kern->buf as an intermediary storage,

> +        * but the BPF program indicates that we need to pass this

> +        * data to the kernel setsockopt handler. No way to export

> +        * on-stack buf, have to allocate a new buffer. The caller

> +        * is responsible for the kfree().

> +        */

> +       p = kzalloc(ctx->optlen, GFP_USER);

> +       if (!p)

> +               return ERR_PTR(-ENOMEM);

> +       memcpy(p, ctx->optval, ctx->optlen);

> +       return p;

> +}

> +

>  int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,

>                                        int *optname, char __user *optval,

>                                        int *optlen, char **kernel_optval)

> @@ -1389,8 +1420,14 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,

>                  * use original userspace data.

>                  */

>                 if (ctx.optlen != 0) {

> -                       *optlen = ctx.optlen;

> -                       *kernel_optval = ctx.optval;

> +                       void *buf = sockopt_export_buf(&ctx);


I found it is hard to follow the logic here (when to allocate memory, how to
fail over, etc.). Do we have plan to reuse sockopt_export_buf()? If not, it is
probably cleaner to put the logic in __cgroup_bpf_run_filter_setsockopt()?

Thanks,
Song

[...]
Song Liu Dec. 21, 2020, 10:25 p.m. UTC | #2
On Thu, Dec 17, 2020 at 9:24 AM Stanislav Fomichev <sdf@google.com> wrote:
>

> When we attach a bpf program to cgroup/getsockopt any other getsockopt()

> syscall starts incurring kzalloc/kfree cost. While, in general, it's

> not an issue, sometimes it is, like in the case of TCP_ZEROCOPY_RECEIVE.

> TCP_ZEROCOPY_RECEIVE (ab)uses getsockopt system call to implement

> fastpath for incoming TCP, we don't want to have extra allocations in

> there.

>

> Let add a small buffer on the stack and use it for small (majority)

> {s,g}etsockopt values. I've started with 128 bytes to cover

> the options we care about (TCP_ZEROCOPY_RECEIVE which is 32 bytes

> currently, with some planned extension to 64 + some headroom

> for the future).

>

> It seems natural to do the same for setsockopt, but it's a bit more

> involved when the BPF program modifies the data (where we have to

> kmalloc). The assumption is that for the majority of setsockopt

> calls (which are doing pure BPF options or apply policy) this

> will bring some benefit as well.

>

> Signed-off-by: Stanislav Fomichev <sdf@google.com>


Could you please share some performance numbers for this optimization?

Thanks,
Song

[...]
Stanislav Fomichev Dec. 22, 2020, 2:09 a.m. UTC | #3
On 12/21, Song Liu wrote:
> On Thu, Dec 17, 2020 at 9:24 AM Stanislav Fomichev <sdf@google.com> wrote:

> >

> > When we attach a bpf program to cgroup/getsockopt any other getsockopt()

> > syscall starts incurring kzalloc/kfree cost. While, in general, it's

> > not an issue, sometimes it is, like in the case of TCP_ZEROCOPY_RECEIVE.

> > TCP_ZEROCOPY_RECEIVE (ab)uses getsockopt system call to implement

> > fastpath for incoming TCP, we don't want to have extra allocations in

> > there.

> >

> > Let add a small buffer on the stack and use it for small (majority)

> > {s,g}etsockopt values. I've started with 128 bytes to cover

> > the options we care about (TCP_ZEROCOPY_RECEIVE which is 32 bytes

> > currently, with some planned extension to 64 + some headroom

> > for the future).


> I don't really know the rule of thumb, but 128 bytes on stack feels too  

> big to

> me. I would like to hear others' opinions on this. Can we solve the  

> problem

> with some other mechanisms, e.g. a mempool?

Yeah, I'm not sure as well. But given that we have at least 4k stacks,
it didn't feel like too much. And we will be paying those 128 bytes
only when bpf is attached.

Regarding mempool - I guess we can try that, depending on how the
discussion above ends up. I don't see any docs about kmalloc/mempool
overhead vs kmalloc. (and looking at mempool_alloc it seems
that it aways calls pool->alloc and mostly for guarantees, not
performance; correct me if wrong).

> > +static void *sockopt_export_buf(struct bpf_sockopt_kern *ctx)

> > +{

> > +       void *p;

> > +

> > +       if (ctx->optval != ctx->buf)

> > +               return ctx->optval;

> > +

> > +       /* We've used bpf_sockopt_kern->buf as an intermediary storage,

> > +        * but the BPF program indicates that we need to pass this

> > +        * data to the kernel setsockopt handler. No way to export

> > +        * on-stack buf, have to allocate a new buffer. The caller

> > +        * is responsible for the kfree().

> > +        */

> > +       p = kzalloc(ctx->optlen, GFP_USER);

> > +       if (!p)

> > +               return ERR_PTR(-ENOMEM);

> > +       memcpy(p, ctx->optval, ctx->optlen);

> > +       return p;

> > +}

> > +

> >  int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,

> >                                        int *optname, char __user  

> *optval,

> >                                        int *optlen, char  

> **kernel_optval)

> > @@ -1389,8 +1420,14 @@ int __cgroup_bpf_run_filter_setsockopt(struct  

> sock *sk, int *level,

> >                  * use original userspace data.

> >                  */

> >                 if (ctx.optlen != 0) {

> > -                       *optlen = ctx.optlen;

> > -                       *kernel_optval = ctx.optval;

> > +                       void *buf = sockopt_export_buf(&ctx);


> I found it is hard to follow the logic here (when to allocate memory, how  

> to

> fail over, etc.). Do we have plan to reuse sockopt_export_buf()? If not,  

> it is

> probably cleaner to put the logic in __cgroup_bpf_run_filter_setsockopt()?

Sure. I guess I can add something like 'sockopt_can_export' that
returns 'ctx->optval == ctx->buf' and depending on that do the kmalloc.
Stanislav Fomichev Dec. 22, 2020, 2:11 a.m. UTC | #4
On 12/21, Song Liu wrote:
> On Thu, Dec 17, 2020 at 9:24 AM Stanislav Fomichev <sdf@google.com> wrote:

> >

> > When we attach a bpf program to cgroup/getsockopt any other getsockopt()

> > syscall starts incurring kzalloc/kfree cost. While, in general, it's

> > not an issue, sometimes it is, like in the case of TCP_ZEROCOPY_RECEIVE.

> > TCP_ZEROCOPY_RECEIVE (ab)uses getsockopt system call to implement

> > fastpath for incoming TCP, we don't want to have extra allocations in

> > there.

> >

> > Let add a small buffer on the stack and use it for small (majority)

> > {s,g}etsockopt values. I've started with 128 bytes to cover

> > the options we care about (TCP_ZEROCOPY_RECEIVE which is 32 bytes

> > currently, with some planned extension to 64 + some headroom

> > for the future).

> >

> > It seems natural to do the same for setsockopt, but it's a bit more

> > involved when the BPF program modifies the data (where we have to

> > kmalloc). The assumption is that for the majority of setsockopt

> > calls (which are doing pure BPF options or apply policy) this

> > will bring some benefit as well.

> >

> > Signed-off-by: Stanislav Fomichev <sdf@google.com>


> Could you please share some performance numbers for this optimization?

We've found out about this problem by looking at our global google
profiler, where TCP_ZEROCOPY_RECEIVE was showing up higher than usual.

So I don't really have a nice reproducer, but I would assume I can try
to run something like tools/testing/selftests/net/tcp_mmap.c under perf
and see if there is a clear difference.
Martin KaFai Lau Dec. 22, 2020, 7:11 p.m. UTC | #5
On Thu, Dec 17, 2020 at 09:23:23AM -0800, Stanislav Fomichev wrote:
> When we attach a bpf program to cgroup/getsockopt any other getsockopt()

> syscall starts incurring kzalloc/kfree cost. While, in general, it's

> not an issue, sometimes it is, like in the case of TCP_ZEROCOPY_RECEIVE.

> TCP_ZEROCOPY_RECEIVE (ab)uses getsockopt system call to implement

> fastpath for incoming TCP, we don't want to have extra allocations in

> there.

> 

> Let add a small buffer on the stack and use it for small (majority)

> {s,g}etsockopt values. I've started with 128 bytes to cover

> the options we care about (TCP_ZEROCOPY_RECEIVE which is 32 bytes

> currently, with some planned extension to 64 + some headroom

> for the future).

> 

> It seems natural to do the same for setsockopt, but it's a bit more

> involved when the BPF program modifies the data (where we have to

> kmalloc). The assumption is that for the majority of setsockopt

> calls (which are doing pure BPF options or apply policy) this

> will bring some benefit as well.

> 

> Signed-off-by: Stanislav Fomichev <sdf@google.com>

> ---

>  include/linux/filter.h |  3 +++

>  kernel/bpf/cgroup.c    | 41 +++++++++++++++++++++++++++++++++++++++--

>  2 files changed, 42 insertions(+), 2 deletions(-)

> 

> diff --git a/include/linux/filter.h b/include/linux/filter.h

> index 29c27656165b..362eb0d7af5d 100644

> --- a/include/linux/filter.h

> +++ b/include/linux/filter.h

> @@ -1281,6 +1281,8 @@ struct bpf_sysctl_kern {

>  	u64 tmp_reg;

>  };

>  

> +#define BPF_SOCKOPT_KERN_BUF_SIZE	128

Since these 128 bytes (which then needs to be zero-ed) is modeled after
the TCP_ZEROCOPY_RECEIVE use case, it will be useful to explain
a use case on how the bpf prog will interact with
getsockopt(TCP_ZEROCOPY_RECEIVE).
Stanislav Fomichev Dec. 23, 2020, 3:09 a.m. UTC | #6
On 12/22, Martin KaFai Lau wrote:
> On Thu, Dec 17, 2020 at 09:23:23AM -0800, Stanislav Fomichev wrote:

> > When we attach a bpf program to cgroup/getsockopt any other getsockopt()

> > syscall starts incurring kzalloc/kfree cost. While, in general, it's

> > not an issue, sometimes it is, like in the case of TCP_ZEROCOPY_RECEIVE.

> > TCP_ZEROCOPY_RECEIVE (ab)uses getsockopt system call to implement

> > fastpath for incoming TCP, we don't want to have extra allocations in

> > there.

> >

> > Let add a small buffer on the stack and use it for small (majority)

> > {s,g}etsockopt values. I've started with 128 bytes to cover

> > the options we care about (TCP_ZEROCOPY_RECEIVE which is 32 bytes

> > currently, with some planned extension to 64 + some headroom

> > for the future).

> >

> > It seems natural to do the same for setsockopt, but it's a bit more

> > involved when the BPF program modifies the data (where we have to

> > kmalloc). The assumption is that for the majority of setsockopt

> > calls (which are doing pure BPF options or apply policy) this

> > will bring some benefit as well.

> >

> > Signed-off-by: Stanislav Fomichev <sdf@google.com>

> > ---

> >  include/linux/filter.h |  3 +++

> >  kernel/bpf/cgroup.c    | 41 +++++++++++++++++++++++++++++++++++++++--

> >  2 files changed, 42 insertions(+), 2 deletions(-)

> >

> > diff --git a/include/linux/filter.h b/include/linux/filter.h

> > index 29c27656165b..362eb0d7af5d 100644

> > --- a/include/linux/filter.h

> > +++ b/include/linux/filter.h

> > @@ -1281,6 +1281,8 @@ struct bpf_sysctl_kern {

> >  	u64 tmp_reg;

> >  };

> >

> > +#define BPF_SOCKOPT_KERN_BUF_SIZE	128

> Since these 128 bytes (which then needs to be zero-ed) is modeled after

> the TCP_ZEROCOPY_RECEIVE use case, it will be useful to explain

> a use case on how the bpf prog will interact with

> getsockopt(TCP_ZEROCOPY_RECEIVE).

The only thing I would expect BPF program can do is to return EPERM
to cause application to fallback to non-zerocopy path (and, mostly,
bypass). I don't think BPF can meaningfully mangle the data in struct
tcp_zerocopy_receive.

Does it address your concern? Or do you want me to add a comment or
something?
Martin KaFai Lau Dec. 31, 2020, 6:47 a.m. UTC | #7
On Mon, Dec 21, 2020 at 02:22:41PM -0800, Song Liu wrote:
> On Thu, Dec 17, 2020 at 9:24 AM Stanislav Fomichev <sdf@google.com> wrote:

> >

> > When we attach a bpf program to cgroup/getsockopt any other getsockopt()

> > syscall starts incurring kzalloc/kfree cost. While, in general, it's

> > not an issue, sometimes it is, like in the case of TCP_ZEROCOPY_RECEIVE.

> > TCP_ZEROCOPY_RECEIVE (ab)uses getsockopt system call to implement

> > fastpath for incoming TCP, we don't want to have extra allocations in

> > there.

> >

> > Let add a small buffer on the stack and use it for small (majority)

> > {s,g}etsockopt values. I've started with 128 bytes to cover

> > the options we care about (TCP_ZEROCOPY_RECEIVE which is 32 bytes

> > currently, with some planned extension to 64 + some headroom

> > for the future).

> 

> I don't really know the rule of thumb, but 128 bytes on stack feels too big to

> me. I would like to hear others' opinions on this. Can we solve the problem

> with some other mechanisms, e.g. a mempool?

It seems the do_tcp_getsockopt() is also having "struct tcp_zerocopy_receive"
in the stack.  I think the buf here is also mimicking
"struct tcp_zerocopy_receive", so should not cause any
new problem.

However, "struct tcp_zerocopy_receive" is only 40 bytes now.  I think it
is better to have a smaller buf for now and increase it later when the
the future needs in "struct tcp_zerocopy_receive" is also upstreamed.
Martin KaFai Lau Dec. 31, 2020, 6:50 a.m. UTC | #8
On Tue, Dec 22, 2020 at 07:09:33PM -0800, sdf@google.com wrote:
> On 12/22, Martin KaFai Lau wrote:

> > On Thu, Dec 17, 2020 at 09:23:23AM -0800, Stanislav Fomichev wrote:

> > > When we attach a bpf program to cgroup/getsockopt any other getsockopt()

> > > syscall starts incurring kzalloc/kfree cost. While, in general, it's

> > > not an issue, sometimes it is, like in the case of TCP_ZEROCOPY_RECEIVE.

> > > TCP_ZEROCOPY_RECEIVE (ab)uses getsockopt system call to implement

> > > fastpath for incoming TCP, we don't want to have extra allocations in

> > > there.

> > >

> > > Let add a small buffer on the stack and use it for small (majority)

> > > {s,g}etsockopt values. I've started with 128 bytes to cover

> > > the options we care about (TCP_ZEROCOPY_RECEIVE which is 32 bytes

> > > currently, with some planned extension to 64 + some headroom

> > > for the future).

> > >

> > > It seems natural to do the same for setsockopt, but it's a bit more

> > > involved when the BPF program modifies the data (where we have to

> > > kmalloc). The assumption is that for the majority of setsockopt

> > > calls (which are doing pure BPF options or apply policy) this

> > > will bring some benefit as well.

> > >

> > > Signed-off-by: Stanislav Fomichev <sdf@google.com>

> > > ---

> > >  include/linux/filter.h |  3 +++

> > >  kernel/bpf/cgroup.c    | 41 +++++++++++++++++++++++++++++++++++++++--

> > >  2 files changed, 42 insertions(+), 2 deletions(-)

> > >

> > > diff --git a/include/linux/filter.h b/include/linux/filter.h

> > > index 29c27656165b..362eb0d7af5d 100644

> > > --- a/include/linux/filter.h

> > > +++ b/include/linux/filter.h

> > > @@ -1281,6 +1281,8 @@ struct bpf_sysctl_kern {

> > >  	u64 tmp_reg;

> > >  };

> > >

> > > +#define BPF_SOCKOPT_KERN_BUF_SIZE	128

> > Since these 128 bytes (which then needs to be zero-ed) is modeled after

> > the TCP_ZEROCOPY_RECEIVE use case, it will be useful to explain

> > a use case on how the bpf prog will interact with

> > getsockopt(TCP_ZEROCOPY_RECEIVE).

> The only thing I would expect BPF program can do is to return EPERM

> to cause application to fallback to non-zerocopy path (and, mostly,

> bypass). I don't think BPF can meaningfully mangle the data in struct

> tcp_zerocopy_receive.

> 

> Does it address your concern? Or do you want me to add a comment or

> something?

I was asking because, while 128 byte may work best for TCP_ZEROCOPY_RECEIVCE,
it is many unnecessary byte-zeroings for most options though.
Hence, I am interested to see if there is a practical bpf
use case for TCP_ZEROCOPY_RECEIVE.
Stanislav Fomichev Dec. 31, 2020, 8:14 p.m. UTC | #9
On 12/30, Martin KaFai Lau wrote:
> On Mon, Dec 21, 2020 at 02:22:41PM -0800, Song Liu wrote:

> > On Thu, Dec 17, 2020 at 9:24 AM Stanislav Fomichev <sdf@google.com>  

> wrote:

> > >

> > > When we attach a bpf program to cgroup/getsockopt any other  

> getsockopt()

> > > syscall starts incurring kzalloc/kfree cost. While, in general, it's

> > > not an issue, sometimes it is, like in the case of  

> TCP_ZEROCOPY_RECEIVE.

> > > TCP_ZEROCOPY_RECEIVE (ab)uses getsockopt system call to implement

> > > fastpath for incoming TCP, we don't want to have extra allocations in

> > > there.

> > >

> > > Let add a small buffer on the stack and use it for small (majority)

> > > {s,g}etsockopt values. I've started with 128 bytes to cover

> > > the options we care about (TCP_ZEROCOPY_RECEIVE which is 32 bytes

> > > currently, with some planned extension to 64 + some headroom

> > > for the future).

> >

> > I don't really know the rule of thumb, but 128 bytes on stack feels too  

> big to

> > me. I would like to hear others' opinions on this. Can we solve the  

> problem

> > with some other mechanisms, e.g. a mempool?

> It seems the do_tcp_getsockopt() is also having "struct  

> tcp_zerocopy_receive"

> in the stack.  I think the buf here is also mimicking

> "struct tcp_zerocopy_receive", so should not cause any

> new problem.

Good point!

> However, "struct tcp_zerocopy_receive" is only 40 bytes now.  I think it

> is better to have a smaller buf for now and increase it later when the

> the future needs in "struct tcp_zerocopy_receive" is also upstreamed.

I can lower it to 64. Or even 40?

I can also try to add something like BUILD_BUG_ON(sizeof(struct
tcp_zerocopy_receive) < BPF_SOCKOPT_KERN_BUF_SIZE) to make sure this
buffer gets adjusted whenever we touch tcp_zerocopy_receive.
Stanislav Fomichev Dec. 31, 2020, 8:18 p.m. UTC | #10
On 12/30, Martin KaFai Lau wrote:
> On Tue, Dec 22, 2020 at 07:09:33PM -0800, sdf@google.com wrote:

> > On 12/22, Martin KaFai Lau wrote:

> > > On Thu, Dec 17, 2020 at 09:23:23AM -0800, Stanislav Fomichev wrote:

> > > > When we attach a bpf program to cgroup/getsockopt any other  

> getsockopt()

> > > > syscall starts incurring kzalloc/kfree cost. While, in general, it's

> > > > not an issue, sometimes it is, like in the case of  

> TCP_ZEROCOPY_RECEIVE.

> > > > TCP_ZEROCOPY_RECEIVE (ab)uses getsockopt system call to implement

> > > > fastpath for incoming TCP, we don't want to have extra allocations  

> in

> > > > there.

> > > >

> > > > Let add a small buffer on the stack and use it for small (majority)

> > > > {s,g}etsockopt values. I've started with 128 bytes to cover

> > > > the options we care about (TCP_ZEROCOPY_RECEIVE which is 32 bytes

> > > > currently, with some planned extension to 64 + some headroom

> > > > for the future).

> > > >

> > > > It seems natural to do the same for setsockopt, but it's a bit more

> > > > involved when the BPF program modifies the data (where we have to

> > > > kmalloc). The assumption is that for the majority of setsockopt

> > > > calls (which are doing pure BPF options or apply policy) this

> > > > will bring some benefit as well.

> > > >

> > > > Signed-off-by: Stanislav Fomichev <sdf@google.com>

> > > > ---

> > > >  include/linux/filter.h |  3 +++

> > > >  kernel/bpf/cgroup.c    | 41  

> +++++++++++++++++++++++++++++++++++++++--

> > > >  2 files changed, 42 insertions(+), 2 deletions(-)

> > > >

> > > > diff --git a/include/linux/filter.h b/include/linux/filter.h

> > > > index 29c27656165b..362eb0d7af5d 100644

> > > > --- a/include/linux/filter.h

> > > > +++ b/include/linux/filter.h

> > > > @@ -1281,6 +1281,8 @@ struct bpf_sysctl_kern {

> > > >  	u64 tmp_reg;

> > > >  };

> > > >

> > > > +#define BPF_SOCKOPT_KERN_BUF_SIZE	128

> > > Since these 128 bytes (which then needs to be zero-ed) is modeled  

> after

> > > the TCP_ZEROCOPY_RECEIVE use case, it will be useful to explain

> > > a use case on how the bpf prog will interact with

> > > getsockopt(TCP_ZEROCOPY_RECEIVE).

> > The only thing I would expect BPF program can do is to return EPERM

> > to cause application to fallback to non-zerocopy path (and, mostly,

> > bypass). I don't think BPF can meaningfully mangle the data in struct

> > tcp_zerocopy_receive.

> >

> > Does it address your concern? Or do you want me to add a comment or

> > something?

> I was asking because, while 128 byte may work best for  

> TCP_ZEROCOPY_RECEIVCE,

> it is many unnecessary byte-zeroings for most options though.

> Hence, I am interested to see if there is a practical bpf

> use case for TCP_ZEROCOPY_RECEIVE.

I don't see any practical use-case for TCP_ZEROCOPY_RECEIVE right now
(but you never know, maybe somebody would like to count the number
of ZQ calls? inspect the arguments? idk).

Ideally, we should bypass BPF if (optname == TCP_ZEROCOPY_RECEIVE),
but then it's not 'generic' anymore :-/
Martin KaFai Lau Jan. 4, 2021, 9:01 p.m. UTC | #11
On Thu, Dec 31, 2020 at 12:14:13PM -0800, sdf@google.com wrote:
> On 12/30, Martin KaFai Lau wrote:

> > On Mon, Dec 21, 2020 at 02:22:41PM -0800, Song Liu wrote:

> > > On Thu, Dec 17, 2020 at 9:24 AM Stanislav Fomichev <sdf@google.com>

> > wrote:

> > > >

> > > > When we attach a bpf program to cgroup/getsockopt any other

> > getsockopt()

> > > > syscall starts incurring kzalloc/kfree cost. While, in general, it's

> > > > not an issue, sometimes it is, like in the case of

> > TCP_ZEROCOPY_RECEIVE.

> > > > TCP_ZEROCOPY_RECEIVE (ab)uses getsockopt system call to implement

> > > > fastpath for incoming TCP, we don't want to have extra allocations in

> > > > there.

> > > >

> > > > Let add a small buffer on the stack and use it for small (majority)

> > > > {s,g}etsockopt values. I've started with 128 bytes to cover

> > > > the options we care about (TCP_ZEROCOPY_RECEIVE which is 32 bytes

> > > > currently, with some planned extension to 64 + some headroom

> > > > for the future).

> > >

> > > I don't really know the rule of thumb, but 128 bytes on stack feels

> > too big to

> > > me. I would like to hear others' opinions on this. Can we solve the

> > problem

> > > with some other mechanisms, e.g. a mempool?

> > It seems the do_tcp_getsockopt() is also having "struct

> > tcp_zerocopy_receive"

> > in the stack.  I think the buf here is also mimicking

> > "struct tcp_zerocopy_receive", so should not cause any

> > new problem.

> Good point!

> 

> > However, "struct tcp_zerocopy_receive" is only 40 bytes now.  I think it

> > is better to have a smaller buf for now and increase it later when the

> > the future needs in "struct tcp_zerocopy_receive" is also upstreamed.

> I can lower it to 64. Or even 40?

I think either is fine.  Both will need another cacheline on bpf_sockopt_kern.
128 is a bit too much without a clear understanding on what "some headroom
for the future" means.
diff mbox series

Patch

diff --git a/include/linux/filter.h b/include/linux/filter.h
index 29c27656165b..362eb0d7af5d 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -1281,6 +1281,8 @@  struct bpf_sysctl_kern {
 	u64 tmp_reg;
 };
 
+#define BPF_SOCKOPT_KERN_BUF_SIZE	128
+
 struct bpf_sockopt_kern {
 	struct sock	*sk;
 	u8		*optval;
@@ -1289,6 +1291,7 @@  struct bpf_sockopt_kern {
 	s32		optname;
 	s32		optlen;
 	s32		retval;
+	u8		buf[BPF_SOCKOPT_KERN_BUF_SIZE];
 };
 
 int copy_bpf_fprog_from_user(struct sock_fprog *dst, sockptr_t src, int len);
diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
index 6ec088a96302..0cb5d4376844 100644
--- a/kernel/bpf/cgroup.c
+++ b/kernel/bpf/cgroup.c
@@ -1310,6 +1310,15 @@  static int sockopt_alloc_buf(struct bpf_sockopt_kern *ctx, int max_optlen)
 		max_optlen = PAGE_SIZE;
 	}
 
+	if (max_optlen <= sizeof(ctx->buf)) {
+		/* When the optval fits into BPF_SOCKOPT_KERN_BUF_SIZE
+		 * bytes avoid the cost of kzalloc.
+		 */
+		ctx->optval = ctx->buf;
+		ctx->optval_end = ctx->optval + max_optlen;
+		return max_optlen;
+	}
+
 	ctx->optval = kzalloc(max_optlen, GFP_USER);
 	if (!ctx->optval)
 		return -ENOMEM;
@@ -1321,9 +1330,31 @@  static int sockopt_alloc_buf(struct bpf_sockopt_kern *ctx, int max_optlen)
 
 static void sockopt_free_buf(struct bpf_sockopt_kern *ctx)
 {
+	if (ctx->optval == ctx->buf)
+		return;
 	kfree(ctx->optval);
 }
 
+static void *sockopt_export_buf(struct bpf_sockopt_kern *ctx)
+{
+	void *p;
+
+	if (ctx->optval != ctx->buf)
+		return ctx->optval;
+
+	/* We've used bpf_sockopt_kern->buf as an intermediary storage,
+	 * but the BPF program indicates that we need to pass this
+	 * data to the kernel setsockopt handler. No way to export
+	 * on-stack buf, have to allocate a new buffer. The caller
+	 * is responsible for the kfree().
+	 */
+	p = kzalloc(ctx->optlen, GFP_USER);
+	if (!p)
+		return ERR_PTR(-ENOMEM);
+	memcpy(p, ctx->optval, ctx->optlen);
+	return p;
+}
+
 int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
 				       int *optname, char __user *optval,
 				       int *optlen, char **kernel_optval)
@@ -1389,8 +1420,14 @@  int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
 		 * use original userspace data.
 		 */
 		if (ctx.optlen != 0) {
-			*optlen = ctx.optlen;
-			*kernel_optval = ctx.optval;
+			void *buf = sockopt_export_buf(&ctx);
+
+			if (!IS_ERR(buf)) {
+				*optlen = ctx.optlen;
+				*kernel_optval = buf;
+			} else {
+				ret = PTR_ERR(buf);
+			}
 		}
 	}