Skip to contentSkip to conversation

libbpf: don't allocate 16M for log buffer by default

PATCH Superseded Stanislav Fomichev 4 participants 5 messages Mar 24, 2020 – Mar 25, 2020

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libbpf: don't allocate 16M for log buffer by default

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.

---
 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(-)
tools/lib/bpf/btf.c +9 -1
index 3d1c25fc97ae..53c7efc3b347 100644
@@ -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;
@@ -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)
tools/lib/bpf/libbpf.c +8 -2
index 085e41f9b68e..793c81b35ccc 100644
@@ -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) {
-		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;
 	}
tools/lib/bpf/libbpf_internal.h +2 -0
index 8c3afbd97747..2720f3366798 100644
@@ -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
+
 #ifndef min
 # define min(x, y) ((x) < (y) ? (x) : (y))
 #endif
Signature · 1 line
2.25.1.696.g5e7596f4ac-goog
#1 lore raw
Quoted 28 lines · Stanislav Fomichev

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.

---
 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(-)
tools/lib/bpf/btf.c +1 -1
index 3d1c25fc97ae..53c7efc3b347 100644
@@ -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;

tools/lib/bpf/btf.c int btf__finalize_data(struct bpf_object *obj, struct btf *btf) · line 670

Quoted 10 lines
tools/lib/bpf/btf.c +1 -0
        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).

Quoted 28 lines
tools/lib/bpf/btf.c +7 -0
@@ -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)
tools/lib/bpf/libbpf.c +1 -1
index 085e41f9b68e..793c81b35ccc 100644
@@ -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;
 

tools/lib/bpf/libbpf.c load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt, · line 4913

Quoted 3 lines
tools/lib/bpf/libbpf.c +0 -0
@@ -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?

Quoted 16 lines
tools/lib/bpf/libbpf.c +7 -1
-               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;
        }
tools/lib/bpf/libbpf_internal.h +0 -0
index 8c3afbd97747..2720f3366798 100644

tools/lib/bpf/libbpf_internal.h line 26

Quoted 4 lines
tools/lib/bpf/libbpf_internal.h +1 -0
@@ -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?

Quoted 7 lines
tools/lib/bpf/libbpf_internal.h +1 -1
+
 #ifndef min
 # define min(x, y) ((x) < (y) ? (x) : (y))
 #endif
--

2.25.1.696.g5e7596f4ac-goog

#2 lore raw
Quoted 35 lines · Andrii Nakryiko
Quoted 34 lines · Stanislav Fomichev

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.

---
 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(-)
tools/lib/bpf/btf.c +1 -1
index 3d1c25fc97ae..53c7efc3b347 100644
@@ -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;
 
Quoted 7 lines

tools/lib/bpf/btf.c int btf__finalize_data(struct bpf_object *obj, struct btf *btf) · line 670

Quoted 4 lines
tools/lib/bpf/btf.c +1 -0
+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.

Quoted 27 lines
Quoted 27 lines
tools/lib/bpf/btf.c +7 -0
@@ -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)
tools/lib/bpf/libbpf.c +1 -1
index 085e41f9b68e..793c81b35ccc 100644
@@ -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;
 
Quoted 7 lines

tools/lib/bpf/libbpf.c load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt, · line 4913

Quoted 4 lines
tools/lib/bpf/libbpf.c +0 -0
@@ -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.

Quoted 19 lines
Quoted 19 lines
tools/lib/bpf/libbpf.c +7 -1
-               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;
        }
tools/lib/bpf/libbpf_internal.h +0 -0
index 8c3afbd97747..2720f3366798 100644
@@ -23,6 +23,8 @@
 #define BTF_PARAM_ENC(name, type) (name), (type)
 #define BTF_VAR_SECINFO_ENC(type, offset, size) (type), (offset), (size)
 
Quoted 5 lines

tools/lib/bpf/libbpf_internal.h line 26

Quoted 1 line
tools/lib/bpf/libbpf_internal.h +1 -0
+#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.

#3 lore raw
Quoted 102 lines · Stanislav Fomichev
Quoted 35 lines · Andrii Nakryiko
Quoted 34 lines · Stanislav Fomichev

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.

---
 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(-)
tools/lib/bpf/btf.c +1 -1
index 3d1c25fc97ae..53c7efc3b347 100644
@@ -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;
 
Quoted 7 lines

tools/lib/bpf/btf.c int btf__finalize_data(struct bpf_object *obj, struct btf *btf) · line 670

Quoted 4 lines
tools/lib/bpf/btf.c +1 -0
+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.

Quoted 27 lines
Quoted 27 lines
tools/lib/bpf/btf.c +7 -0
@@ -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)
tools/lib/bpf/libbpf.c +1 -1
index 085e41f9b68e..793c81b35ccc 100644
@@ -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;
 
Quoted 7 lines

tools/lib/bpf/libbpf.c load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt, · line 4913

Quoted 4 lines
tools/lib/bpf/libbpf.c +0 -0
@@ -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.

Quoted 19 lines
Quoted 19 lines
tools/lib/bpf/libbpf.c +7 -1
-               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;
        }
tools/lib/bpf/libbpf_internal.h +0 -0
index 8c3afbd97747..2720f3366798 100644
@@ -23,6 +23,8 @@
 #define BTF_PARAM_ENC(name, type) (name), (type)
 #define BTF_VAR_SECINFO_ENC(type, offset, size) (type), (offset), (size)
 
Quoted 8 lines
Quoted 5 lines

tools/lib/bpf/libbpf_internal.h line 26

Quoted 1 line
tools/lib/bpf/libbpf_internal.h +1 -0
+#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.

maintainer
#4 lore raw
Quoted 37 lines · Andrii Nakryiko
Quoted 36 lines · Stanislav Fomichev
Quoted 35 lines · Andrii Nakryiko
Quoted 34 lines · Stanislav Fomichev

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.

---
  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(-)
tools/lib/bpf/btf.c +1 -1
index 3d1c25fc97ae..53c7efc3b347 100644
@@ -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;
 
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

tools/lib/bpf/btf.c int btf__finalize_data(struct bpf_object *obj, struct btf *btf) · line 670

Quoted 4 lines
tools/lib/bpf/btf.c +1 -0
+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.

The iproute2 BPF loader does the first try without any log buffer, and then successively increases the size on failure [0].

libbpf should also assume the success case in the very first run, and only then redo the load attempt with log buffer to avoid the overhead.

[0] https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/tree/lib/bpf.c

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
tools/lib/bpf/btf.c +7 -0
@@ -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)

[...]

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

tools/lib/bpf/libbpf_internal.h line 26

Quoted 9 lines
tools/lib/bpf/libbpf_internal.h +1 -0
index 8c3afbd97747..2720f3366798 100644
@@ -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?

Quoted 8 lines
Quoted 3 lines

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.

Yeah, 128 is way too low. Either we should just malloc the max possible size right away, or make it a two-step approach where we first try with half the size and only if that fails we retry with the max size. Given programs can be very complex this will otherwise just prolong the time unnecessarily for the failure verdict and unnecessarily puts load on the verifier.

Thanks,
Daniel