Skip to contentSkip to conversation

Libbpf API and memfd_create() fixes

PATCH Superseded Andrii Nakryiko 3 participants 18 messages Jan 30, 2024 – Jan 31, 2024

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Andrii Nakryiko

author maintainer
#0 lore raw

cover Libbpf API and memfd_create() fixes

Few small fixes identified over last few days. Main fix is for memfd_create() which causes problems on Android platforms. See individual patches for details.

Andrii Nakryiko (5):
libbpf: call memfd_create() syscall directly
libbpf: add missing LIBBPF_API annotation to libbpf_set_memlock_rlim
API
libbpf: add btf__new_split() API that was declared but not implemented
libbpf: add missed btf_ext__raw_data() API
selftests/bpf: fix bench runner SIGSEGV

 tools/lib/bpf/bpf.h                 |  2 +-
 tools/lib/bpf/btf.c                 | 11 ++++++++++-
 tools/lib/bpf/libbpf.c              | 11 ++++++++++-
 tools/lib/bpf/libbpf.map            |  5 +++--
 tools/lib/bpf/linker.c              |  2 +-
 tools/testing/selftests/bpf/bench.c | 10 +++++++++-
 6 files changed, 34 insertions(+), 7 deletions(-)
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH 1/5 libbpf: call memfd_create() syscall directly

Some versions of Android do not implement memfd_create() wrapper in their libc implementation, leading to build failures ([0]). On the other hand, memfd_create() is available as a syscall on quite old kernels (3.17+, while bpf() syscall itself is available since 3.18+), so it is ok to assume that syscall availability and call into it with syscall() helper to avoid Android-specific workarounds.

Validated in libbpf-bootstrap's CI ([1]).

[0] https://github.com/libbpf/libbpf-bootstrap/actions/runs/7701003207/job/20986080319#step:5:83
[1] https://github.com/libbpf/libbpf-bootstrap/actions/runs/7715988887/job/21031767212?pr=253

---
 tools/lib/bpf/libbpf.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +10 -1
index db65ea59a05a..b21fda6b9b85 100644
@@ -1525,11 +1525,20 @@ static Elf64_Sym *find_elf_var_sym(const struct bpf_object *obj, const char *nam
 	return ERR_PTR(-ENOENT);
 }
 
+/* Some versions of Android don't provide memfd_create() in their libc
+ * implementation, so avoid complications and just go straight to Linux
+ * syscall.
+ */
+static int sys_memfd_create(const char *name, unsigned flags)
+{
+	return syscall(__NR_memfd_create, name, flags);
+}
+
 static int create_placeholder_fd(void)
 {
 	int fd;
 
-	fd = ensure_good_fd(memfd_create("libbpf-placeholder-fd", MFD_CLOEXEC));
+	fd = ensure_good_fd(sys_memfd_create("libbpf-placeholder-fd", MFD_CLOEXEC));
 	if (fd < 0)
 		return -errno;
 	return fd;
Signature · 1 line
2.34.1
reviewer Acked-by on v1 1/5
#2 lore raw

Re: [PATCH bpf-next 1/5] libbpf: call memfd_create() syscall directly

Quoted 7 lines · Andrii Nakryiko

Some versions of Android do not implement memfd_create() wrapper in their libc implementation, leading to build failures ([0]). On the other hand, memfd_create() is available as a syscall on quite old kernels (3.17+, while bpf() syscall itself is available since 3.18+), so it is ok to assume that syscall availability and call into it with syscall() helper to avoid Android-specific workarounds.

Quoted 6 lines

Validated in libbpf-bootstrap's CI ([1]).

   [0] https://github.com/libbpf/libbpf-bootstrap/actions/runs/7701003207/job/20986080319#step:5:83
   [1] https://github.com/libbpf/libbpf-bootstrap/actions/runs/7715988887/job/21031767212?pr=253
author maintainer
#3 lore raw

PATCH 2/5 libbpf: add missing LIBBPF_API annotation to libbpf_set_memlock_rlim API

In reply to Andrii Nakryiko

LIBBPF_API annotation seems missing on libbpf_set_memlock_rlim API, so add it to make this API callable from libbpf's shared library version.

Fixes e542f2c4cd16 ("libbpf: Auto-bump RLIMIT_MEMLOCK if kernel needs it for BPF")
---
 tools/lib/bpf/bpf.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/bpf.h +1 -1
index 1441f642c563..f866e98b2436 100644
@@ -35,7 +35,7 @@
 extern "C" {
 #endif
 
-int libbpf_set_memlock_rlim(size_t memlock_bytes);
+LIBBPF_API int libbpf_set_memlock_rlim(size_t memlock_bytes);
 
 struct bpf_map_create_opts {
 	size_t sz; /* size of this struct for forward/backward compatibility */
Signature · 1 line
2.34.1
reviewer Acked-by on v1 2/5
#4 lore raw

Re: [PATCH bpf-next 2/5] libbpf: add missing LIBBPF_API annotation to libbpf_set_memlock_rlim API

Quoted 4 lines · Andrii Nakryiko

LIBBPF_API annotation seems missing on libbpf_set_memlock_rlim API, so add it to make this API callable from libbpf's shared library version.

Fixes e542f2c4cd16 ("libbpf: Auto-bump RLIMIT_MEMLOCK if kernel needs it for BPF")

Maybe we should the following commit as Fixes?

ab9a5a05dc48 libbpf: fix up few libbpf.map problems

Other than the above, LGTM.

Quoted 18 lines
---
  tools/lib/bpf/bpf.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/bpf.h +1 -1
index 1441f642c563..f866e98b2436 100644
@@ -35,7 +35,7 @@
  extern "C" {
  #endif
 
-int libbpf_set_memlock_rlim(size_t memlock_bytes);
+LIBBPF_API int libbpf_set_memlock_rlim(size_t memlock_bytes);
 
  struct bpf_map_create_opts {
  	size_t sz; /* size of this struct for forward/backward compatibility */
#5 lore raw

Re: [PATCH bpf-next 2/5] libbpf: add missing LIBBPF_API annotation to libbpf_set_memlock_rlim API

Quoted 12 lines · Yonghong Song
Quoted 4 lines · Andrii Nakryiko

LIBBPF_API annotation seems missing on libbpf_set_memlock_rlim API, so add it to make this API callable from libbpf's shared library version.

Fixes e542f2c4cd16 ("libbpf: Auto-bump RLIMIT_MEMLOCK if kernel needs it for BPF")

Maybe we should the following commit as Fixes?

ab9a5a05dc48 libbpf: fix up few libbpf.map problems

The one I referenced introduced the problem, the ab9a5a05dc48 one fixed some problems, but not all of them (for libbpf_set_memlock_rlim). So it feels like pointing to the originating commit is better?

Quoted 22 lines

Other than the above, LGTM.

Quoted 18 lines
---
  tools/lib/bpf/bpf.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/bpf.h +1 -1
index 1441f642c563..f866e98b2436 100644
@@ -35,7 +35,7 @@
  extern "C" {
  #endif
 
-int libbpf_set_memlock_rlim(size_t memlock_bytes);
+LIBBPF_API int libbpf_set_memlock_rlim(size_t memlock_bytes);
 
  struct bpf_map_create_opts {
      size_t sz; /* size of this struct for forward/backward compatibility */
reviewer
#6 lore raw

Re: [PATCH bpf-next 2/5] libbpf: add missing LIBBPF_API annotation to libbpf_set_memlock_rlim API

Quoted 7 lines · Andrii Nakryiko
Quoted 6 lines · Yonghong Song
Quoted 4 lines · Andrii Nakryiko

LIBBPF_API annotation seems missing on libbpf_set_memlock_rlim API, so add it to make this API callable from libbpf's shared library version.

Fixes e542f2c4cd16 ("libbpf: Auto-bump RLIMIT_MEMLOCK if kernel needs it for BPF")
Quoted 8 lines
Quoted 4 lines

Maybe we should the following commit as Fixes?

ab9a5a05dc48 libbpf: fix up few libbpf.map problems

The one I referenced introduced the problem, the ab9a5a05dc48 one fixed some problems, but not all of them (for libbpf_set_memlock_rlim). So it feels like pointing to the originating commit is better?

Maybe we can put two Fixes here? Just having e542f2c4cd16 is a little confusing since libbpf_set_memlock_rlim is not in libbpf.map with e542f2c4cd16.

Quoted 23 lines
Quoted 23 lines

Other than the above, LGTM.

Quoted 18 lines
---
   tools/lib/bpf/bpf.h | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/bpf.h +1 -1
index 1441f642c563..f866e98b2436 100644
@@ -35,7 +35,7 @@
   extern "C" {
   #endif
 
-int libbpf_set_memlock_rlim(size_t memlock_bytes);
+LIBBPF_API int libbpf_set_memlock_rlim(size_t memlock_bytes);
 
   struct bpf_map_create_opts {
       size_t sz; /* size of this struct for forward/backward compatibility */
#7 lore raw

Re: [PATCH bpf-next 2/5] libbpf: add missing LIBBPF_API annotation to libbpf_set_memlock_rlim API

Quoted 14 lines · Yonghong Song
Quoted 11 lines · Andrii Nakryiko
Quoted 10 lines · Yonghong Song
Quoted 4 lines · Andrii Nakryiko

LIBBPF_API annotation seems missing on libbpf_set_memlock_rlim API, so add it to make this API callable from libbpf's shared library version.

Fixes e542f2c4cd16 ("libbpf: Auto-bump RLIMIT_MEMLOCK if kernel needs it for BPF")

Maybe we should the following commit as Fixes?

ab9a5a05dc48 libbpf: fix up few libbpf.map problems

Quoted 8 lines
Quoted 4 lines

The one I referenced introduced the problem, the ab9a5a05dc48 one fixed some problems, but not all of them (for libbpf_set_memlock_rlim). So it feels like pointing to the originating commit is better?

Maybe we can put two Fixes here? Just having e542f2c4cd16 is a little confusing since libbpf_set_memlock_rlim is not in libbpf.map with e542f2c4cd16.

I don't mind, but I'll hold off on sending v2 just for this, maybe someone can add it while applying.

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines

Other than the above, LGTM.

Quoted 18 lines
---
   tools/lib/bpf/bpf.h | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/bpf.h +1 -1
index 1441f642c563..f866e98b2436 100644
@@ -35,7 +35,7 @@
   extern "C" {
   #endif
 
-int libbpf_set_memlock_rlim(size_t memlock_bytes);
+LIBBPF_API int libbpf_set_memlock_rlim(size_t memlock_bytes);
 
   struct bpf_map_create_opts {
       size_t sz; /* size of this struct for forward/backward compatibility */
author maintainer
#8 lore raw

PATCH 3/5 libbpf: add btf__new_split() API that was declared but not implemented

In reply to Andrii Nakryiko

Seems like original commit adding split BTF support intended to add btf__new_split() API, and even declared it in libbpf.map, but never added (trivial) implementation. Fix this.

Fixes ba451366bf44 ("libbpf: Implement basic split BTF support")
---
 tools/lib/bpf/btf.c      | 5 +++++
 tools/lib/bpf/libbpf.map | 3 ++-
 2 files changed, 7 insertions(+), 1 deletion(-)
tools/lib/bpf/btf.c +5 -0
index 95db88b36cf3..845034d15420 100644
@@ -1079,6 +1079,11 @@ struct btf *btf__new(const void *data, __u32 size)
 	return libbpf_ptr(btf_new(data, size, NULL));
 }
 
+struct btf *btf__new_split(const void *data, __u32 size, struct btf *base_btf)
+{
+	return libbpf_ptr(btf_new(data, size, base_btf));
+}
+
 static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
 				 struct btf_ext **btf_ext)
 {
tools/lib/bpf/libbpf.map +2 -1
index d9e1f57534fa..386964f572a8 100644
@@ -245,7 +245,6 @@ LIBBPF_0.3.0 {
 		btf__parse_raw_split;
 		btf__parse_split;
 		btf__new_empty_split;
-		btf__new_split;
 		ring_buffer__epoll_fd;
 } LIBBPF_0.2.0;
 
@@ -411,5 +410,7 @@ LIBBPF_1.3.0 {
 } LIBBPF_1.2.0;
 
 LIBBPF_1.4.0 {
+	global:
 		bpf_token_create;
+		btf__new_split;
 } LIBBPF_1.3.0;
Signature · 1 line
2.34.1
reviewer Acked-by on v1 3/5
#9 lore raw

Re: [PATCH bpf-next 3/5] libbpf: add btf__new_split() API that was declared but not implemented

Quoted 6 lines · Andrii Nakryiko

Seems like original commit adding split BTF support intended to add btf__new_split() API, and even declared it in libbpf.map, but never added (trivial) implementation. Fix this.

Fixes ba451366bf44 ("libbpf: Implement basic split BTF support")

The patch LGTM. We did some cross checking between libbpf.map and the implementation. What things are missing here to capture missed implementation or LIBBPF_API marking?

Quoted 41 lines
---
  tools/lib/bpf/btf.c      | 5 +++++
  tools/lib/bpf/libbpf.map | 3 ++-
  2 files changed, 7 insertions(+), 1 deletion(-)
tools/lib/bpf/btf.c +5 -0
index 95db88b36cf3..845034d15420 100644
@@ -1079,6 +1079,11 @@ struct btf *btf__new(const void *data, __u32 size)
  	return libbpf_ptr(btf_new(data, size, NULL));
  }
 
+struct btf *btf__new_split(const void *data, __u32 size, struct btf *base_btf)
+{
+	return libbpf_ptr(btf_new(data, size, base_btf));
+}
+
  static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
  				 struct btf_ext **btf_ext)
  {
tools/lib/bpf/libbpf.map +2 -1
index d9e1f57534fa..386964f572a8 100644
@@ -245,7 +245,6 @@ LIBBPF_0.3.0 {
  		btf__parse_raw_split;
  		btf__parse_split;
  		btf__new_empty_split;
-		btf__new_split;
  		ring_buffer__epoll_fd;
  } LIBBPF_0.2.0;
 
@@ -411,5 +410,7 @@ LIBBPF_1.3.0 {
  } LIBBPF_1.2.0;
 
  LIBBPF_1.4.0 {
+	global:
  		bpf_token_create;
+		btf__new_split;
  } LIBBPF_1.3.0;
#10 lore raw

Re: [PATCH bpf-next 3/5] libbpf: add btf__new_split() API that was declared but not implemented

Quoted 7 lines · Yonghong Song
Quoted 4 lines · Andrii Nakryiko

Seems like original commit adding split BTF support intended to add btf__new_split() API, and even declared it in libbpf.map, but never added (trivial) implementation. Fix this.

Quoted 6 lines
Quoted 2 lines
Fixes ba451366bf44 ("libbpf: Implement basic split BTF support")

The patch LGTM. We did some cross checking between libbpf.map and the implementation. What things are missing here to capture missed implementation or LIBBPF_API marking?

Yes, we still have it, and it does detect issues when API wasn't added into libbpf.map.

I haven't investigated exactly why it didn't catch the issue when API is in libbpf.map, but is not marked with LIBBPF_API, but I suspect it's because existing check doesn't take into account visibility of the symbol, so there is some room for improvement.

Similarly, not sure why it didn't detect that btf_ext__new_split() wasn't even implemented, probably because we don't distinguish UNDEF and FUNC symbols? So something to follow up on for sure.

Quoted 44 lines
Quoted 41 lines
---
  tools/lib/bpf/btf.c      | 5 +++++
  tools/lib/bpf/libbpf.map | 3 ++-
  2 files changed, 7 insertions(+), 1 deletion(-)
tools/lib/bpf/btf.c +5 -0
index 95db88b36cf3..845034d15420 100644
@@ -1079,6 +1079,11 @@ struct btf *btf__new(const void *data, __u32 size)
      return libbpf_ptr(btf_new(data, size, NULL));
  }
 
+struct btf *btf__new_split(const void *data, __u32 size, struct btf *base_btf)
+{
+     return libbpf_ptr(btf_new(data, size, base_btf));
+}
+
  static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
                               struct btf_ext **btf_ext)
  {
tools/lib/bpf/libbpf.map +2 -1
index d9e1f57534fa..386964f572a8 100644
@@ -245,7 +245,6 @@ LIBBPF_0.3.0 {
              btf__parse_raw_split;
              btf__parse_split;
              btf__new_empty_split;
-             btf__new_split;
              ring_buffer__epoll_fd;
  } LIBBPF_0.2.0;
 
@@ -411,5 +410,7 @@ LIBBPF_1.3.0 {
  } LIBBPF_1.2.0;
 
  LIBBPF_1.4.0 {
+     global:
              bpf_token_create;
+             btf__new_split;
  } LIBBPF_1.3.0;
reviewer
#11 lore raw

Re: [PATCH bpf-next 3/5] libbpf: add btf__new_split() API that was declared but not implemented

Quoted 15 lines · Andrii Nakryiko
Quoted 11 lines · Yonghong Song
Quoted 6 lines · Andrii Nakryiko

Seems like original commit adding split BTF support intended to add btf__new_split() API, and even declared it in libbpf.map, but never added (trivial) implementation. Fix this.

Fixes ba451366bf44 ("libbpf: Implement basic split BTF support")

The patch LGTM. We did some cross checking between libbpf.map and the implementation. What things are missing here to capture missed implementation or LIBBPF_API marking?

Yes, we still have it, and it does detect issues when API wasn't added into libbpf.map.

Quoted 8 lines

I haven't investigated exactly why it didn't catch the issue when API is in libbpf.map, but is not marked with LIBBPF_API, but I suspect it's because existing check doesn't take into account visibility of the symbol, so there is some room for improvement.

Similarly, not sure why it didn't detect that btf_ext__new_split() wasn't even implemented, probably because we don't distinguish UNDEF and FUNC symbols? So something to follow up on for sure.

Agreed. A followup to improve API matching between libbpf.map and code can be done later.

Quoted 44 lines
Quoted 44 lines
Quoted 41 lines
---
   tools/lib/bpf/btf.c      | 5 +++++
   tools/lib/bpf/libbpf.map | 3 ++-
   2 files changed, 7 insertions(+), 1 deletion(-)
tools/lib/bpf/btf.c +5 -0
index 95db88b36cf3..845034d15420 100644
@@ -1079,6 +1079,11 @@ struct btf *btf__new(const void *data, __u32 size)
       return libbpf_ptr(btf_new(data, size, NULL));
   }
 
+struct btf *btf__new_split(const void *data, __u32 size, struct btf *base_btf)
+{
+     return libbpf_ptr(btf_new(data, size, base_btf));
+}
+
   static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
                                struct btf_ext **btf_ext)
   {
tools/lib/bpf/libbpf.map +2 -1
index d9e1f57534fa..386964f572a8 100644
@@ -245,7 +245,6 @@ LIBBPF_0.3.0 {
               btf__parse_raw_split;
               btf__parse_split;
               btf__new_empty_split;
-             btf__new_split;
               ring_buffer__epoll_fd;
   } LIBBPF_0.2.0;
 
@@ -411,5 +410,7 @@ LIBBPF_1.3.0 {
   } LIBBPF_1.2.0;
 
   LIBBPF_1.4.0 {
+     global:
               bpf_token_create;
+             btf__new_split;
   } LIBBPF_1.3.0;
author maintainer
#12 lore raw

PATCH 4/5 libbpf: add missed btf_ext__raw_data() API

In reply to Andrii Nakryiko

Another API that was declared in libbpf.map but actual implementation was missing. btf_ext__get_raw_data() was intended as a discouraged alias to consistently-named btf_ext__raw_data(), so make this an actuality.

Fixes 20eccf29e297 ("libbpf: hide and discourage inconsistently named getters")
---
 tools/lib/bpf/btf.c      | 6 +++++-
 tools/lib/bpf/libbpf.map | 2 +-
 tools/lib/bpf/linker.c   | 2 +-
 3 files changed, 7 insertions(+), 3 deletions(-)
tools/lib/bpf/btf.c +5 -1
index 845034d15420..a17b4c9c4213 100644
@@ -3050,12 +3050,16 @@ struct btf_ext *btf_ext__new(const __u8 *data, __u32 size)
 	return btf_ext;
 }
 
-const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, __u32 *size)
+const void *btf_ext__raw_data(const struct btf_ext *btf_ext, __u32 *size)
 {
 	*size = btf_ext->data_size;
 	return btf_ext->data;
 }
 
+__attribute__((alias("btf_ext__raw_data")))
+const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, __u32 *size);
+
+
 struct btf_dedup;
 
 static struct btf_dedup *btf_dedup_new(struct btf *btf, const struct btf_dedup_opts *opts);
tools/lib/bpf/libbpf.map +1 -1
index 386964f572a8..86804fd90dd1 100644
@@ -325,7 +325,6 @@ LIBBPF_0.7.0 {
 		bpf_xdp_detach;
 		bpf_xdp_query;
 		bpf_xdp_query_id;
-		btf_ext__raw_data;
 		libbpf_probe_bpf_helper;
 		libbpf_probe_bpf_map_type;
 		libbpf_probe_bpf_prog_type;
@@ -413,4 +412,5 @@ LIBBPF_1.4.0 {
 	global:
 		bpf_token_create;
 		btf__new_split;
+		btf_ext__raw_data;
 } LIBBPF_1.3.0;
tools/lib/bpf/linker.c +1 -1
index 16bca56002ab..0d4be829551b 100644
@@ -2732,7 +2732,7 @@ static int finalize_btf(struct bpf_linker *linker)
 
 	/* Emit .BTF.ext section */
 	if (linker->btf_ext) {
-		raw_data = btf_ext__get_raw_data(linker->btf_ext, &raw_sz);
+		raw_data = btf_ext__raw_data(linker->btf_ext, &raw_sz);
 		if (!raw_data)
 			return -ENOMEM;
 
Signature · 1 line
2.34.1
reviewer Acked-by on v1 4/5
#13 lore raw

Re: [PATCH bpf-next 4/5] libbpf: add missed btf_ext__raw_data() API

Quoted 6 lines · Andrii Nakryiko

Another API that was declared in libbpf.map but actual implementation was missing. btf_ext__get_raw_data() was intended as a discouraged alias to consistently-named btf_ext__raw_data(), so make this an actuality.

Fixes 20eccf29e297 ("libbpf: hide and discourage inconsistently named getters")
author maintainer
#14 lore raw

PATCH 5/5 selftests/bpf: fix bench runner SIGSEGV

In reply to Andrii Nakryiko

Some benchmarks don't anticipate "consumer" and/or "producer" sides. Add NULL checks in corresponding places and warn about inappropriate consumer/producer count argument values.

---
 tools/testing/selftests/bpf/bench.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/bench.c +9 -1
index 73ce11b0547d..36962fc305eb 100644
@@ -330,7 +330,7 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
 		break;
 	case 'c':
 		env.consumer_cnt = strtol(arg, NULL, 10);
-		if (env.consumer_cnt <= 0) {
+		if (env.consumer_cnt < 0) {
 			fprintf(stderr, "Invalid consumer count: %s\n", arg);
 			argp_usage(state);
 		}
@@ -607,6 +607,10 @@ static void setup_benchmark(void)
 		bench->setup();
 
 	for (i = 0; i < env.consumer_cnt; i++) {
+		if (!bench->consumer_thread) {
+			fprintf(stderr, "benchmark doesn't have consumers!\n");
+			exit(1);
+		}
 		err = pthread_create(&state.consumers[i], NULL,
 				     bench->consumer_thread, (void *)(long)i);
 		if (err) {
@@ -626,6 +630,10 @@ static void setup_benchmark(void)
 		env.prod_cpus.next_cpu = env.cons_cpus.next_cpu;
 
 	for (i = 0; i < env.producer_cnt; i++) {
+		if (!bench->producer_thread) {
+			fprintf(stderr, "benchmark doesn't have producers!\n");
+			exit(1);
+		}
 		err = pthread_create(&state.producers[i], NULL,
 				     bench->producer_thread, (void *)(long)i);
 		if (err) {
Signature · 1 line
2.34.1
reviewer Acked-by on v1 5/5
#15 lore raw

Re: [PATCH bpf-next 5/5] selftests/bpf: fix bench runner SIGSEGV

Quoted 1 line · Andrii Nakryiko

Some benchmarks don't anticipate "consumer" and/or "producer" sides. Add

For this, you mean some future potential benchmarks, right?

Quoted 4 lines

NULL checks in corresponding places and warn about inappropriate consumer/producer count argument values.

Quoted 39 lines
---
  tools/testing/selftests/bpf/bench.c | 10 +++++++++-
  1 file changed, 9 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/bench.c +9 -1
index 73ce11b0547d..36962fc305eb 100644
@@ -330,7 +330,7 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
  		break;
  	case 'c':
  		env.consumer_cnt = strtol(arg, NULL, 10);
-		if (env.consumer_cnt <= 0) {
+		if (env.consumer_cnt < 0) {
  			fprintf(stderr, "Invalid consumer count: %s\n", arg);
  			argp_usage(state);
  		}
@@ -607,6 +607,10 @@ static void setup_benchmark(void)
  		bench->setup();
 
  	for (i = 0; i < env.consumer_cnt; i++) {
+		if (!bench->consumer_thread) {
+			fprintf(stderr, "benchmark doesn't have consumers!\n");
+			exit(1);
+		}
  		err = pthread_create(&state.consumers[i], NULL,
  				     bench->consumer_thread, (void *)(long)i);
  		if (err) {
@@ -626,6 +630,10 @@ static void setup_benchmark(void)
  		env.prod_cpus.next_cpu = env.cons_cpus.next_cpu;
 
  	for (i = 0; i < env.producer_cnt; i++) {
+		if (!bench->producer_thread) {
+			fprintf(stderr, "benchmark doesn't have producers!\n");
+			exit(1);
+		}
  		err = pthread_create(&state.producers[i], NULL,
  				     bench->producer_thread, (void *)(long)i);
  		if (err) {
#16 lore raw

Re: [PATCH bpf-next 5/5] selftests/bpf: fix bench runner SIGSEGV

Quoted 6 lines · Yonghong Song
Quoted 1 line · Andrii Nakryiko

Some benchmarks don't anticipate "consumer" and/or "producer" sides. Add

For this, you mean some future potential benchmarks, right?

No, existing ones as well. Like trig-tp and other "trigger" benchmarks. I ran into this when I was trying to set consumers to 0 explicitly, which wasn't allowed due to <= check. Then I fixed the check, and I ran into SIGSEGV. So I decided to fix that up.

Quoted 48 lines
Quoted 5 lines

NULL checks in corresponding places and warn about inappropriate consumer/producer count argument values.

Quoted 39 lines
---
  tools/testing/selftests/bpf/bench.c | 10 +++++++++-
  1 file changed, 9 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/bench.c +9 -1
index 73ce11b0547d..36962fc305eb 100644
@@ -330,7 +330,7 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
              break;
      case 'c':
              env.consumer_cnt = strtol(arg, NULL, 10);
-             if (env.consumer_cnt <= 0) {
+             if (env.consumer_cnt < 0) {
                      fprintf(stderr, "Invalid consumer count: %s\n", arg);
                      argp_usage(state);
              }
@@ -607,6 +607,10 @@ static void setup_benchmark(void)
              bench->setup();
 
      for (i = 0; i < env.consumer_cnt; i++) {
+             if (!bench->consumer_thread) {
+                     fprintf(stderr, "benchmark doesn't have consumers!\n");
+                     exit(1);
+             }
              err = pthread_create(&state.consumers[i], NULL,
                                   bench->consumer_thread, (void *)(long)i);
              if (err) {
@@ -626,6 +630,10 @@ static void setup_benchmark(void)
              env.prod_cpus.next_cpu = env.cons_cpus.next_cpu;
 
      for (i = 0; i < env.producer_cnt; i++) {
+             if (!bench->producer_thread) {
+                     fprintf(stderr, "benchmark doesn't have producers!\n");
+                     exit(1);
+             }
              err = pthread_create(&state.producers[i], NULL,
                                   bench->producer_thread, (void *)(long)i);
              if (err) {
reviewer
#17 lore raw

Re: [PATCH bpf-next 5/5] selftests/bpf: fix bench runner SIGSEGV

Quoted 9 lines · Andrii Nakryiko
Quoted 4 lines · Yonghong Song
Quoted 1 line · Andrii Nakryiko

Some benchmarks don't anticipate "consumer" and/or "producer" sides. Add

For this, you mean some future potential benchmarks, right?

No, existing ones as well. Like trig-tp and other "trigger" benchmarks. I ran into this when I was trying to set consumers to 0 explicitly, which wasn't allowed due to <= check. Then I fixed the check, and I ran into SIGSEGV. So I decided to fix that up.

Some description like this in the commit message will be good.

Quoted 46 lines
Quoted 46 lines
Quoted 5 lines

NULL checks in corresponding places and warn about inappropriate consumer/producer count argument values.

Quoted 39 lines
---
   tools/testing/selftests/bpf/bench.c | 10 +++++++++-
   1 file changed, 9 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/bench.c +9 -1
index 73ce11b0547d..36962fc305eb 100644
@@ -330,7 +330,7 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
               break;
       case 'c':
               env.consumer_cnt = strtol(arg, NULL, 10);
-             if (env.consumer_cnt <= 0) {
+             if (env.consumer_cnt < 0) {
                       fprintf(stderr, "Invalid consumer count: %s\n", arg);
                       argp_usage(state);
               }
@@ -607,6 +607,10 @@ static void setup_benchmark(void)
               bench->setup();
 
       for (i = 0; i < env.consumer_cnt; i++) {
+             if (!bench->consumer_thread) {
+                     fprintf(stderr, "benchmark doesn't have consumers!\n");
+                     exit(1);
+             }
               err = pthread_create(&state.consumers[i], NULL,
                                    bench->consumer_thread, (void *)(long)i);
               if (err) {
@@ -626,6 +630,10 @@ static void setup_benchmark(void)
               env.prod_cpus.next_cpu = env.cons_cpus.next_cpu;
 
       for (i = 0; i < env.producer_cnt; i++) {
+             if (!bench->producer_thread) {
+                     fprintf(stderr, "benchmark doesn't have producers!\n");
+                     exit(1);
+             }
               err = pthread_create(&state.producers[i], NULL,
                                    bench->producer_thread, (void *)(long)i);
               if (err) {