Skip to contentSkip to conversation

bpf: libbpf: retry loading program on EAGAIN

PATCH In review Lorenz Bauer 2 participants 3 messages Jan 8, 2019 – Jan 15, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover bpf: libbpf: retry loading program on EAGAIN

As discussed on this mailing list, libbpf should retry loading a program if the syscall returns EAGAIN.

The patch is straightforward to write, but less straightforward to test. Please let me know if you have an idea for a reliable test.

Lorenz Bauer (1):
bpf: libbpf: retry loading program on EAGAIN

 tools/lib/bpf/bpf.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
Signature · 1 line
2.19.1
#1 lore raw

PATCH 1/1 bpf: libbpf: retry loading program on EAGAIN

Commit c3494801cd1785e2 ("bpf: check pending signals while verifying programs") makes it possible for the BPF_PROG_LOAD to fail with EAGAIN. Retry unconditionally in this case.

---
 tools/lib/bpf/bpf.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
tools/lib/bpf/bpf.c +15 -4
index 3caaa3428774..88cbd110ae58 100644
@@ -65,6 +65,17 @@ static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
 	return syscall(__NR_bpf, cmd, attr, size);
 }
 
+static inline int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size)
+{
+	int fd;
+
+	do {
+		fd = sys_bpf(BPF_PROG_LOAD, attr, size);
+	} while (fd < 0 && errno == EAGAIN);
+
+	return fd;
+}
+
 int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr)
 {
 	__u32 name_len = create_attr->name ? strlen(create_attr->name) : 0;
@@ -232,7 +243,7 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
 	memcpy(attr.prog_name, load_attr->name,
 	       min(name_len, BPF_OBJ_NAME_LEN - 1));
 
-	fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
+	fd = sys_bpf_prog_load(&attr, sizeof(attr));
 	if (fd >= 0)
 		return fd;
 
@@ -269,7 +280,7 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
 			break;
 		}
 
-		fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
+		fd = sys_bpf_prog_load(&attr, sizeof(attr));
 
 		if (fd >= 0)
 			goto done;
@@ -283,7 +294,7 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
 	attr.log_size = log_buf_sz;
 	attr.log_level = 1;
 	log_buf[0] = 0;
-	fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
+	fd = sys_bpf_prog_load(&attr, sizeof(attr));
 done:
 	free(finfo);
 	free(linfo);
@@ -328,7 +339,7 @@ int bpf_verify_program(enum bpf_prog_type type, const struct bpf_insn *insns,
 	attr.kern_version = kern_version;
 	attr.prog_flags = prog_flags;
 
-	return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
+	return sys_bpf_prog_load(&attr, sizeof(attr));
 }
 
 int bpf_map_update_elem(int fd, const void *key, const void *value,
Signature · 1 line
2.19.1
maintainer
#2 lore raw
Quoted 5 lines · Lorenz Bauer

Commit c3494801cd1785e2 ("bpf: check pending signals while verifying programs") makes it possible for the BPF_PROG_LOAD to fail with EAGAIN. Retry unconditionally in this case.

Bit back and forth whether libbpf should actually do this and not the application itself, otoh, until this point there was nothing returning EAGAIN either during load so it avoids potential breakage, anyway, applied, thanks!