bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Stanislav Fomichev <sdf@fomichev.me>
Cc: Stanislav Fomichev <sdf@google.com>,
	Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>,
	"David S. Miller" <davem@davemloft.net>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>
Subject: Re: [PATCH bpf-next] libbpf: don't allocate 16M for log buffer by default
Date: Tue, 24 Mar 2020 17:08:13 -0700	[thread overview]
Message-ID: <CAEf4BzbF+QmKAmkhrNMn2EEo0nPMmyb0T=BwLvDm+KFE1ZrhrA@mail.gmail.com> (raw)
In-Reply-To: <20200324235938.GA2805006@mini-arch.hsd1.ca.comcast.net>

On Tue, Mar 24, 2020 at 4:59 PM Stanislav Fomichev <sdf@fomichev.me> wrote:
>
> On 03/24, Andrii Nakryiko wrote:
> > On Tue, Mar 24, 2020 at 4:31 PM Stanislav Fomichev <sdf@google.com> wrote:
> > >
> > > For each prog/btf load we allocate and free 16 megs of verifier buffer.
> > > On production systems it doesn't really make sense because the
> > > programs/btf have gone through extensive testing and (mostly) guaranteed
> > > to successfully load.
> > >
> > > Let's switch to a much smaller buffer by default (128 bytes, sys_bpf
> > > doesn't accept smaller log buffer) and resize it if the kernel returns
> > > ENOSPC. On the first ENOSPC error we resize the buffer to BPF_LOG_BUF_SIZE
> > > and then, on each subsequent ENOSPC, we keep doubling the buffer.
> > >
> > > Signed-off-by: Stanislav Fomichev <sdf@google.com>
> > > ---
> > >  tools/lib/bpf/btf.c             | 10 +++++++++-
> > >  tools/lib/bpf/libbpf.c          | 10 ++++++++--
> > >  tools/lib/bpf/libbpf_internal.h |  2 ++
> > >  3 files changed, 19 insertions(+), 3 deletions(-)
> > >
> > > diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
> > > index 3d1c25fc97ae..53c7efc3b347 100644
> > > --- a/tools/lib/bpf/btf.c
> > > +++ b/tools/lib/bpf/btf.c
> > > @@ -657,13 +657,14 @@ int btf__finalize_data(struct bpf_object *obj, struct btf *btf)
> > >
> > >  int btf__load(struct btf *btf)
> > >  {
> > > -       __u32 log_buf_size = BPF_LOG_BUF_SIZE;
> > > +       __u32 log_buf_size = BPF_MIN_LOG_BUF_SIZE;
> > >         char *log_buf = NULL;
> > >         int err = 0;
> > >
> > >         if (btf->fd >= 0)
> > >                 return -EEXIST;
> > >
> > > +retry_load:
> > >         log_buf = malloc(log_buf_size);
> > >         if (!log_buf)
> > >                 return -ENOMEM;
> >
> > I'd argue that on first try we shouldn't allocate log_buf at all, then
> > start allocating it using reasonable starting size (see below).
> Agreed, makes sense.
>
> > > @@ -673,6 +674,13 @@ int btf__load(struct btf *btf)
> > >         btf->fd = bpf_load_btf(btf->data, btf->data_size,
> > >                                log_buf, log_buf_size, false);
> > >         if (btf->fd < 0) {
> > > +               if (errno == ENOSPC) {
> > > +                       log_buf_size = max((__u32)BPF_LOG_BUF_SIZE,
> > > +                                          log_buf_size << 1);
> > > +                       free(log_buf);
> > > +                       goto retry_load;
> > > +               }
> > > +
> > >                 err = -errno;
> > >                 pr_warn("Error loading BTF: %s(%d)\n", strerror(errno), errno);
> > >                 if (*log_buf)
> > > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> > > index 085e41f9b68e..793c81b35ccc 100644
> > > --- a/tools/lib/bpf/libbpf.c
> > > +++ b/tools/lib/bpf/libbpf.c
> > > @@ -4855,7 +4855,7 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
> > >  {
> > >         struct bpf_load_program_attr load_attr;
> > >         char *cp, errmsg[STRERR_BUFSIZE];
> > > -       int log_buf_size = BPF_LOG_BUF_SIZE;
> > > +       size_t log_buf_size = BPF_MIN_LOG_BUF_SIZE;
> > >         char *log_buf;
> > >         int btf_fd, ret;
> > >
> > > @@ -4911,7 +4911,13 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
> > >         }
> > >
> > >         if (errno == ENOSPC) {
> >
> > same, doing if (!log_buf || errno == ENOSPC) should handle this
> > without any other major changes?
> Yeah, I don't see why it shouldn't. Let me try to see if I hit something
> in the selftests with that approach.
>
> > > -               log_buf_size <<= 1;
> > > +               if (errno == ENOSPC) {
> > > +                       log_buf_size = max((size_t)BPF_LOG_BUF_SIZE,
> > > +                                          log_buf_size << 1);
> > > +                       free(log_buf);
> > > +                       goto retry_load;
> > > +               }
> > > +
> > >                 free(log_buf);
> > >                 goto retry_load;
> > >         }
> > > diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h
> > > index 8c3afbd97747..2720f3366798 100644
> > > --- a/tools/lib/bpf/libbpf_internal.h
> > > +++ b/tools/lib/bpf/libbpf_internal.h
> > > @@ -23,6 +23,8 @@
> > >  #define BTF_PARAM_ENC(name, type) (name), (type)
> > >  #define BTF_VAR_SECINFO_ENC(type, offset, size) (type), (offset), (size)
> > >
> > > +#define BPF_MIN_LOG_BUF_SIZE 128
> >
> > This seems way too low, if there is some error it almost certainly
> > will be too short, probably for few iterations, just causing waste.
> > Let's make it something a bit more reasonable, like 32KB or something?
> In this case, maybe start with the existing 16M BPF_LOG_BUF_SIZE?
> My goal here is optimize for the successful case. If there is an error the
> size shouldn't matter that much.

Not feeling strongly. But we already will have a retry loop, so not
too hard to do it in steps. Then also errors do happen in production
as well, and it would be good to not eat too much memory
unnecessarily.

  reply	other threads:[~2020-03-25  0:08 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-24 23:31 [PATCH bpf-next] libbpf: don't allocate 16M for log buffer by default Stanislav Fomichev
2020-03-24 23:45 ` Andrii Nakryiko
2020-03-24 23:59   ` Stanislav Fomichev
2020-03-25  0:08     ` Andrii Nakryiko [this message]
2020-03-25  9:54       ` Daniel Borkmann

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='CAEf4BzbF+QmKAmkhrNMn2EEo0nPMmyb0T=BwLvDm+KFE1ZrhrA@mail.gmail.com' \
    --to=andrii.nakryiko@gmail.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=netdev@vger.kernel.org \
    --cc=sdf@fomichev.me \
    --cc=sdf@google.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).