From: sdf@google.com
To: Song Liu <song@kernel.org>
Cc: Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>
Subject: Re: [PATCH bpf-next 1/2] bpf: try to avoid kzalloc in cgroup/{s,g}etsockopt
Date: Mon, 21 Dec 2020 18:09:39 -0800 [thread overview]
Message-ID: <X+FVY0sWeVoC9wY1@google.com> (raw)
In-Reply-To: <CAPhsuW52eTurJ4pPAgZtv0giw2C+7r6aMacZXx8XkwUxBGARAQ@mail.gmail.com>
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.
next prev parent reply other threads:[~2020-12-22 2:10 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-17 17:23 [PATCH bpf-next 0/2] bpf: misc performance improvements for cgroup hooks Stanislav Fomichev
2020-12-17 17:23 ` [PATCH bpf-next 1/2] bpf: try to avoid kzalloc in cgroup/{s,g}etsockopt Stanislav Fomichev
2020-12-21 22:22 ` Song Liu
2020-12-22 2:09 ` sdf [this message]
2020-12-31 6:47 ` Martin KaFai Lau
2020-12-31 20:14 ` sdf
2021-01-04 21:01 ` Martin KaFai Lau
2020-12-21 22:25 ` Song Liu
2020-12-22 2:11 ` sdf
2020-12-22 19:11 ` Martin KaFai Lau
2020-12-23 3:09 ` sdf
2020-12-31 6:50 ` Martin KaFai Lau
2020-12-31 20:18 ` sdf
2020-12-17 17:23 ` [PATCH bpf-next 2/2] bpf: split cgroup_bpf_enabled per attach type Stanislav Fomichev
2020-12-21 22:40 ` Song Liu
2020-12-22 1:57 ` sdf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=X+FVY0sWeVoC9wY1@google.com \
--to=sdf@google.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=netdev@vger.kernel.org \
--cc=song@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.