Skip to contentSkip to conversation

inline asm helpers to access array elements

PATCH Superseded Barret Rhoden 3 participants 12 messages Jan 3, 2024 – Jan 4, 2024

Subsystems (5)
Kselftest BPF BPF selftests libbpf +1 more subsystems

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover inline asm helpers to access array elements

Sorry for the delay on this. Discussed in [1]. It's a helper for proving to the verifier that your access in the array is valid. Happy to change names or whatever. =)

Also added a libbpf helper function for mmapping an mmappable map.

We've been using both in our ghost-BPF schedulers[2].

[1] https://lore.kernel.org/bpf/[email protected]/T/
[2] https://github.com/google/ghost-userspace/blob/main/third_party/bpf/common.bpf.h#L218

Barret Rhoden (2):
libbpf: add helpers for mmapping maps
selftests/bpf: add inline assembly helpers to access array elements

 tools/bpf/bpftool/gen.c                       |  16 +-
 tools/lib/bpf/libbpf.c                        |  23 +++
 tools/lib/bpf/libbpf.h                        |   6 +
 tools/lib/bpf/libbpf.map                      |   4 +
 tools/testing/selftests/bpf/Makefile          |   2 +-
 .../bpf/prog_tests/test_array_elem.c          | 112 ++++++++++
 .../selftests/bpf/progs/array_elem_test.c     | 195 ++++++++++++++++++
 tools/testing/selftests/bpf/progs/bpf_misc.h  |  43 ++++
 8 files changed, 387 insertions(+), 14 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_array_elem.c
 create mode 100644 tools/testing/selftests/bpf/progs/array_elem_test.c
Signature · 1 line
2.43.0.472.g3155946c3a-goog
#1 lore raw

PATCH 1/2 libbpf: add helpers for mmapping maps

bpf_map__mmap_size() was internal to bpftool. Use that to make wrappers for mmap and munmap.

---
 tools/bpf/bpftool/gen.c  | 16 +++-------------
 tools/lib/bpf/libbpf.c   | 23 +++++++++++++++++++++++
 tools/lib/bpf/libbpf.h   |  6 ++++++
 tools/lib/bpf/libbpf.map |  4 ++++
 4 files changed, 36 insertions(+), 13 deletions(-)
tools/bpf/bpftool/gen.c +3 -13
index ee3ce2b8000d..a328e960c141 100644
@@ -453,16 +453,6 @@ static void print_hex(const char *data, int data_sz)
 	}
 }
 
-static size_t bpf_map_mmap_sz(const struct bpf_map *map)
-{
-	long page_sz = sysconf(_SC_PAGE_SIZE);
-	size_t map_sz;
-
-	map_sz = (size_t)roundup(bpf_map__value_size(map), 8) * bpf_map__max_entries(map);
-	map_sz = roundup(map_sz, page_sz);
-	return map_sz;
-}
-
 /* Emit type size asserts for all top-level fields in memory-mapped internal maps. */
 static void codegen_asserts(struct bpf_object *obj, const char *obj_name)
 {
@@ -641,7 +631,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
 		if (bpf_map__is_internal(map) &&
 		    (bpf_map__map_flags(map) & BPF_F_MMAPABLE))
 			printf("\tskel_free_map_data(skel->%1$s, skel->maps.%1$s.initial_value, %2$zd);\n",
-			       ident, bpf_map_mmap_sz(map));
+			       ident, bpf_map__mmap_size(map));
 		codegen("\
 			\n\
 				skel_closenz(skel->maps.%1$s.map_fd);	    \n\
@@ -723,7 +713,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
 					goto cleanup;			    \n\
 				skel->maps.%1$s.initial_value = (__u64) (long) skel->%1$s;\n\
 			}						    \n\
-			", ident, bpf_map_mmap_sz(map));
+			", ident, bpf_map__mmap_size(map));
 	}
 	codegen("\
 		\n\
@@ -780,7 +770,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
 			if (!skel->%1$s)				    \n\
 				return -ENOMEM;				    \n\
 			",
-		       ident, bpf_map_mmap_sz(map), mmap_flags);
+		       ident, bpf_map__mmap_size(map), mmap_flags);
 	}
 	codegen("\
 		\n\
tools/lib/bpf/libbpf.c +23 -0
index ebcfb2147fbd..171a977cb5fd 100644
@@ -9830,6 +9830,29 @@ void *bpf_map__initial_value(struct bpf_map *map, size_t *psize)
 	return map->mmaped;
 }
 
+size_t bpf_map__mmap_size(const struct bpf_map *map)
+{
+	long page_sz = sysconf(_SC_PAGE_SIZE);
+	size_t map_sz;
+
+	map_sz = (size_t)roundup(bpf_map__value_size(map), 8) *
+		bpf_map__max_entries(map);
+	map_sz = roundup(map_sz, page_sz);
+	return map_sz;
+}
+
+void *bpf_map__mmap(const struct bpf_map *map)
+{
+	return mmap(NULL, bpf_map__mmap_size(map),
+		    PROT_READ | PROT_WRITE, MAP_SHARED,
+		    bpf_map__fd(map), 0);
+}
+
+int bpf_map__munmap(const struct bpf_map *map, void *addr)
+{
+	return munmap(addr, bpf_map__mmap_size(map));
+}
+
 bool bpf_map__is_internal(const struct bpf_map *map)
 {
 	return map->libbpf_type != LIBBPF_MAP_UNSPEC;
tools/lib/bpf/libbpf.h +6 -0
index 6cd9c501624f..148f4c783ca7 100644
@@ -996,6 +996,12 @@ LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra);
 LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
 					  const void *data, size_t size);
 LIBBPF_API void *bpf_map__initial_value(struct bpf_map *map, size_t *psize);
+/* get the mmappable size of the map */
+LIBBPF_API size_t bpf_map__mmap_size(const struct bpf_map *map);
+/* mmap the map */
+LIBBPF_API void *bpf_map__mmap(const struct bpf_map *map);
+/* munmap the map at addr */
+LIBBPF_API int bpf_map__munmap(const struct bpf_map *map, void *addr);
 
 /**
  * @brief **bpf_map__is_internal()** tells the caller whether or not the
tools/lib/bpf/libbpf.map +4 -0
index 91c5aef7dae7..9e44de4fbf39 100644
@@ -411,4 +411,8 @@ LIBBPF_1.3.0 {
 } LIBBPF_1.2.0;
 
 LIBBPF_1.4.0 {
+	global:
+		bpf_map__mmap_size;
+		bpf_map__mmap;
+		bpf_map__munmap;
 } LIBBPF_1.3.0;
Signature · 1 line
2.43.0.472.g3155946c3a-goog
#4 lore raw

PATCH 2/2 selftests/bpf: add inline assembly helpers to access array elements

In reply to Barret Rhoden

When accessing an array, even if you insert your own bounds check, sometimes the compiler will remove the check, or modify it such that the verifier no longer knows your access is within bounds.

The compiler is even free to make a copy of a register, check the copy, and use the original to access the array. The verifier knows the *copy* is within bounds, but not the original register!

---
 tools/testing/selftests/bpf/Makefile          |   2 +-
 .../bpf/prog_tests/test_array_elem.c          | 112 ++++++++++
 .../selftests/bpf/progs/array_elem_test.c     | 195 ++++++++++++++++++
 tools/testing/selftests/bpf/progs/bpf_misc.h  |  43 ++++
 4 files changed, 351 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_array_elem.c
 create mode 100644 tools/testing/selftests/bpf/progs/array_elem_test.c
tools/testing/selftests/bpf/Makefile +1 -1
index 617ae55c3bb5..651d4663cc78 100644
@@ -34,7 +34,7 @@ LIBELF_CFLAGS	:= $(shell $(PKG_CONFIG) libelf --cflags 2>/dev/null)
 LIBELF_LIBS	:= $(shell $(PKG_CONFIG) libelf --libs 2>/dev/null || echo -lelf)
 
 CFLAGS += -g $(OPT_FLAGS) -rdynamic					\
-	  -Wall -Werror 						\
+	  -dicks -Wall -Werror 						\
 	  $(GENFLAGS) $(SAN_CFLAGS) $(LIBELF_CFLAGS)			\
 	  -I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR)		\
 	  -I$(TOOLSINCDIR) -I$(APIDIR) -I$(OUTPUT)
tools/testing/selftests/bpf/prog_tests/test_array_elem.c new file +112 -0

112 lines not on this page · raw message

tools/testing/selftests/bpf/progs/array_elem_test.c new file +195 -0

195 lines not on this page · raw message

tools/testing/selftests/bpf/progs/bpf_misc.h +43 -0

47 lines not on this page · raw message

Signature · 1 line
2.43.0.472.g3155946c3a-goog
#2 lore raw

RE: [PATCH bpf-next 1/2] libbpf: add helpers for mmapping maps

In reply to Barret Rhoden
Quoted 64 lines · Barret Rhoden

bpf_map__mmap_size() was internal to bpftool. Use that to make wrappers for mmap and munmap.

---
 tools/bpf/bpftool/gen.c  | 16 +++-------------
 tools/lib/bpf/libbpf.c   | 23 +++++++++++++++++++++++
 tools/lib/bpf/libbpf.h   |  6 ++++++
 tools/lib/bpf/libbpf.map |  4 ++++
 4 files changed, 36 insertions(+), 13 deletions(-)
tools/bpf/bpftool/gen.c +3 -13
index ee3ce2b8000d..a328e960c141 100644
@@ -453,16 +453,6 @@ static void print_hex(const char *data, int data_sz)
 	}
 }
 
-static size_t bpf_map_mmap_sz(const struct bpf_map *map)
-{
-	long page_sz = sysconf(_SC_PAGE_SIZE);
-	size_t map_sz;
-
-	map_sz = (size_t)roundup(bpf_map__value_size(map), 8) * bpf_map__max_entries(map);
-	map_sz = roundup(map_sz, page_sz);
-	return map_sz;
-}
-
 /* Emit type size asserts for all top-level fields in memory-mapped internal maps. */
 static void codegen_asserts(struct bpf_object *obj, const char *obj_name)
 {
@@ -641,7 +631,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
 		if (bpf_map__is_internal(map) &&
 		    (bpf_map__map_flags(map) & BPF_F_MMAPABLE))
 			printf("\tskel_free_map_data(skel->%1$s, skel->maps.%1$s.initial_value, %2$zd);\n",
-			       ident, bpf_map_mmap_sz(map));
+			       ident, bpf_map__mmap_size(map));
 		codegen("\
 			\n\
 				skel_closenz(skel->maps.%1$s.map_fd);	    \n\
@@ -723,7 +713,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
 					goto cleanup;			    \n\
 				skel->maps.%1$s.initial_value = (__u64) (long) skel->%1$s;\n\
 			}						    \n\
-			", ident, bpf_map_mmap_sz(map));
+			", ident, bpf_map__mmap_size(map));
 	}
 	codegen("\
 		\n\
@@ -780,7 +770,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
 			if (!skel->%1$s)				    \n\
 				return -ENOMEM;				    \n\
 			",
-		       ident, bpf_map_mmap_sz(map), mmap_flags);
+		       ident, bpf_map__mmap_size(map), mmap_flags);
 	}
 	codegen("\
 		\n\
tools/lib/bpf/libbpf.c +0 -0
index ebcfb2147fbd..171a977cb5fd 100644

tools/lib/bpf/libbpf.c void *bpf_map__initial_value(struct bpf_map *map, size_t *psize) · line 9831

Quoted 2 lines
tools/lib/bpf/libbpf.c +0 -0
@@ -9830,6 +9830,29 @@ void *bpf_map__initial_value(struct bpf_map *map, size_t *psize)
 	return map->mmaped;
 }

It seems libbpf.c has its own bpf_map_mmap_sz() as well.

static size_t bpf_map_mmap_sz(unsigned int value_sz, unsigned int max_entries)

 {                                                                                
        const long page_sz = sysconf(_SC_PAGE_SIZE);                                         
        size_t map_sz;                                                                                
                                                                                    
        map_sz = (size_t)roundup(value_sz, 8) * max_entries;
        map_sz = roundup(map_sz, page_sz);                                                  
        return map_sz;                                                              

}

Can we consolidate these a bit. Seems we don't want/need to have both bpf_map__mmap_size and bpf_map_mmap_sz() floating around.

Should bpf_map__mmap_size just calls bpf_map_mmap_sz with the correct sz and max_entries?

Quoted 61 lines
tools/lib/bpf/libbpf.c +23 -0
+size_t bpf_map__mmap_size(const struct bpf_map *map)
+{
+	long page_sz = sysconf(_SC_PAGE_SIZE);
+	size_t map_sz;
+
+	map_sz = (size_t)roundup(bpf_map__value_size(map), 8) *
+		bpf_map__max_entries(map);
+	map_sz = roundup(map_sz, page_sz);
+	return map_sz;
+}
+
+void *bpf_map__mmap(const struct bpf_map *map)
+{
+	return mmap(NULL, bpf_map__mmap_size(map),
+		    PROT_READ | PROT_WRITE, MAP_SHARED,
+		    bpf_map__fd(map), 0);
+}
+
+int bpf_map__munmap(const struct bpf_map *map, void *addr)
+{
+	return munmap(addr, bpf_map__mmap_size(map));
+}
+
 bool bpf_map__is_internal(const struct bpf_map *map)
 {
 	return map->libbpf_type != LIBBPF_MAP_UNSPEC;
tools/lib/bpf/libbpf.h +6 -0
index 6cd9c501624f..148f4c783ca7 100644
@@ -996,6 +996,12 @@ LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra);
 LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
 					  const void *data, size_t size);
 LIBBPF_API void *bpf_map__initial_value(struct bpf_map *map, size_t *psize);
+/* get the mmappable size of the map */
+LIBBPF_API size_t bpf_map__mmap_size(const struct bpf_map *map);
+/* mmap the map */
+LIBBPF_API void *bpf_map__mmap(const struct bpf_map *map);
+/* munmap the map at addr */
+LIBBPF_API int bpf_map__munmap(const struct bpf_map *map, void *addr);
 
 /**
  * @brief **bpf_map__is_internal()** tells the caller whether or not the
tools/lib/bpf/libbpf.map +4 -0
index 91c5aef7dae7..9e44de4fbf39 100644
@@ -411,4 +411,8 @@ LIBBPF_1.3.0 {
 } LIBBPF_1.2.0;
 
 LIBBPF_1.4.0 {
+	global:
+		bpf_map__mmap_size;
+		bpf_map__mmap;
+		bpf_map__munmap;
 } LIBBPF_1.3.0;
Signature · 1 line
2.43.0.472.g3155946c3a-goog
#5 lore raw

RE: [PATCH bpf-next 2/2] selftests/bpf: add inline assembly helpers to access array elements

In reply to Barret Rhoden
Quoted 43 lines · Barret Rhoden

When accessing an array, even if you insert your own bounds check, sometimes the compiler will remove the check, or modify it such that the verifier no longer knows your access is within bounds.

The compiler is even free to make a copy of a register, check the copy, and use the original to access the array. The verifier knows the *copy* is within bounds, but not the original register!

---
 tools/testing/selftests/bpf/Makefile          |   2 +-
 .../bpf/prog_tests/test_array_elem.c          | 112 ++++++++++
 .../selftests/bpf/progs/array_elem_test.c     | 195 ++++++++++++++++++
 tools/testing/selftests/bpf/progs/bpf_misc.h  |  43 ++++
 4 files changed, 351 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_array_elem.c
 create mode 100644 tools/testing/selftests/bpf/progs/array_elem_test.c
tools/testing/selftests/bpf/Makefile +1 -1
index 617ae55c3bb5..651d4663cc78 100644
@@ -34,7 +34,7 @@ LIBELF_CFLAGS	:= $(shell $(PKG_CONFIG) libelf --cflags 2>/dev/null)
 LIBELF_LIBS	:= $(shell $(PKG_CONFIG) libelf --libs 2>/dev/null || echo -lelf)
 
 CFLAGS += -g $(OPT_FLAGS) -rdynamic					\
-	  -Wall -Werror 						\
+	  -dicks -Wall -Werror 						\
 	  $(GENFLAGS) $(SAN_CFLAGS) $(LIBELF_CFLAGS)			\
 	  -I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR)		\
 	  -I$(TOOLSINCDIR) -I$(APIDIR) -I$(OUTPUT)
tools/testing/selftests/bpf/prog_tests/test_array_elem.c new file +6 -0
new file mode 100644
index 000000000000..c953636f07c9
@@ -0,0 +1,112 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2024 Google LLC. */
+#include <test_progs.h>
+#include "array_elem_test.skel.h"
+
+#define NR_MAP_ELEMS 100

[...]

Quoted 13 lines
tools/testing/selftests/bpf/progs/bpf_misc.h +5 -0
index 2fd59970c43a..002bab44cde2 100644
@@ -135,4 +135,47 @@
 /* make it look to compiler like value is read and written */
 #define __sink(expr) asm volatile("" : "+g"(expr))
 
+/*
+ * Access an array element within a bound, such that the verifier knows the
+ * access is safe.
+ *
+ * This macro asm is the equivalent of:

tools/testing/selftests/bpf/progs/bpf_misc.h line 152

Quoted 10 lines
tools/testing/selftests/bpf/progs/bpf_misc.h +10 -0
+ *
+ *	if (!arr)
+ *		return NULL;
+ *	if (idx >= arr_sz)
+ *		return NULL;
+ *	return &arr[idx];
+ *
+ * The index (___idx below) needs to be a u64, at least for certain versions of
+ * the BPF ISA, since there aren't u32 conditional jumps.
+ */

This is nice, but in practice what we've been doing is making our maps power of 2 and then just masking them as needed. I think this is more efficient if you care about performance.

FWIW I'm not opposed to having this though.

tools/testing/selftests/bpf/progs/bpf_misc.h line 160

Quoted 8 lines
tools/testing/selftests/bpf/progs/bpf_misc.h +8 -0
+#define bpf_array_elem(arr, arr_sz, idx) ({				\
+	typeof(&(arr)[0]) ___arr = arr;					\
+	__u64 ___idx = idx;						\
+	if (___arr) {							\
+		asm volatile("if %[__idx] >= %[__bound] goto 1f;	\
+			      %[__idx] *= %[__size];		\
+			      %[__arr] += %[__idx];		\
+			      goto 2f;				\

+1 for using asm goto :)

Quoted 25 lines
tools/testing/selftests/bpf/progs/bpf_misc.h +20 -0
+			      1:;				\
+			      %[__arr] = 0;			\
+			      2:				\
+			      "						\
+			     : [__arr]"+r"(___arr), [__idx]"+r"(___idx)	\
+			     : [__bound]"r"((arr_sz)),		        \
+			       [__size]"i"(sizeof(typeof((arr)[0])))	\
+			     : "cc");					\
+	}								\
+	___arr;								\
+})
+
+/*
+ * Convenience wrapper for bpf_array_elem(), where we compute the size of the
+ * array.  Be sure to use an actual array, and not a pointer, just like with the
+ * ARRAY_SIZE macro.
+ */
+#define bpf_array_sz_elem(arr, idx) \
+	bpf_array_elem(arr, sizeof(arr) / sizeof((arr)[0]), idx)
+
 #endif
Signature · 1 line
2.43.0.472.g3155946c3a-goog
#3 lore raw

Re: [PATCH bpf-next 1/2] libbpf: add helpers for mmapping maps

In reply to John Fastabend
Quoted 2 lines · John Fastabend

Should bpf_map__mmap_size just calls bpf_map_mmap_sz with the correct sz and max_entries?

will do, thanks!

#6 lore raw

Re: [PATCH bpf-next 2/2] selftests/bpf: add inline assembly helpers to access array elements

In reply to Barret Rhoden
Quoted 10 lines · Barret Rhoden

When accessing an array, even if you insert your own bounds check, sometimes the compiler will remove the check, or modify it such that the verifier no longer knows your access is within bounds.

The compiler is even free to make a copy of a register, check the copy, and use the original to access the array. The verifier knows the *copy* is within bounds, but not the original register!

Quoted 9 lines
---
 tools/testing/selftests/bpf/Makefile          |   2 +-
 .../bpf/prog_tests/test_array_elem.c          | 112 ++++++++++
 .../selftests/bpf/progs/array_elem_test.c     | 195 ++++++++++++++++++
 tools/testing/selftests/bpf/progs/bpf_misc.h  |  43 ++++
 4 files changed, 351 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_array_elem.c
 create mode 100644 tools/testing/selftests/bpf/progs/array_elem_test.c

I'm curious how bpf_cmp_likely/bpf_cmp_unlikely (just applied to bpf-next) compares to this?

tools/testing/selftests/bpf/Makefile LIBELF_CFLAGS := $(shell $(PKG_CONFIG) libelf --cflags 2>/dev/null) · line 37

Quoted 10 lines
tools/testing/selftests/bpf/Makefile +1 -1
index 617ae55c3bb5..651d4663cc78 100644
@@ -34,7 +34,7 @@ LIBELF_CFLAGS := $(shell $(PKG_CONFIG) libelf --cflags 2>/dev/null)
 LIBELF_LIBS    := $(shell $(PKG_CONFIG) libelf --libs 2>/dev/null || echo -lelf)
 
 CFLAGS += -g $(OPT_FLAGS) -rdynamic                                    \
-         -Wall -Werror                                                 \
+         -dicks -Wall -Werror                                          \

what does this magic argument do? )

Quoted 12 lines
tools/testing/selftests/bpf/Makefile +0 -0
          $(GENFLAGS) $(SAN_CFLAGS) $(LIBELF_CFLAGS)                    \
          -I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR)          \
          -I$(TOOLSINCDIR) -I$(APIDIR) -I$(OUTPUT)
tools/testing/selftests/bpf/prog_tests/test_array_elem.c new file +3 -0
new file mode 100644
index 000000000000..c953636f07c9
@@ -0,0 +1,112 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2024 Google LLC. */
+#include <test_progs.h>

tools/testing/selftests/bpf/prog_tests/test_array_elem.c line 13

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/test_array_elem.c new file +10 -0
+#include "array_elem_test.skel.h"
+
+#define NR_MAP_ELEMS 100
+
+/*
+ * Helper to load and run a program.
+ * Call must define skel, map_elems, and bss_elems.
+ * Destroy the skel when you're done.
+ */
+#define load_and_run(PROG) ({

does this have to be a macro? Can you write it as a function?

                                    \
Quoted 24 lines
tools/testing/selftests/bpf/prog_tests/test_array_elem.c new file +24 -0
+       int err;                                                        \
+       skel = array_elem_test__open();                                 \
+       if (!ASSERT_OK_PTR(skel, "array_elem_test open"))               \
+               return;                                                 \
+       bpf_program__set_autoload(skel->progs.x_ ## PROG, true);        \
+       err = array_elem_test__load(skel);                              \
+       if (!ASSERT_EQ(err, 0, "array_elem_test load")) {               \
+               array_elem_test__destroy(skel);                         \
+               return;                                                 \
+       }                                                               \
+       err = array_elem_test__attach(skel);                            \
+       if (!ASSERT_EQ(err, 0, "array_elem_test attach")) {             \
+               array_elem_test__destroy(skel);                         \
+               return;                                                 \
+       }                                                               \
+       for (int i = 0; i < NR_MAP_ELEMS; i++)                          \
+               skel->bss->lookup_indexes[i] = i;                       \
+       map_elems = bpf_map__mmap(skel->maps.arraymap);                 \
+       ASSERT_OK_PTR(map_elems, "mmap");                               \
+       bss_elems = skel->bss->bss_elems;                               \
+       skel->bss->target_pid = getpid();                               \
+       usleep(1);                                                      \
+})
+

[...]

#7 lore raw

Re: [PATCH bpf-next 2/2] selftests/bpf: add inline assembly helpers to access array elements

Quoted 2 lines · Andrii Nakryiko

I'm curious how bpf_cmp_likely/bpf_cmp_unlikely (just applied to bpf-next) compares to this?

these work great!

e.g.

        if (bpf_cmp_likely(idx, <, NR_MAP_ELEMS))
                map_elems[idx] = i;

works fine. since that's essentially the code that bpf_array_elem() was trying to replace, i'd rather just use the new bpf_cmp helpers than have the special array_elem helpers.

thanks,

barret

#8 lore raw

Re: [PATCH bpf-next 2/2] selftests/bpf: add inline assembly helpers to access array elements

Quoted 7 lines · Barret Rhoden
Quoted 2 lines · Andrii Nakryiko

I'm curious how bpf_cmp_likely/bpf_cmp_unlikely (just applied to bpf-next) compares to this?

these work great!

Quoted 8 lines

e.g.

         if (bpf_cmp_likely(idx, <, NR_MAP_ELEMS))
                 map_elems[idx] = i;

works fine. since that's essentially the code that bpf_array_elem() was trying to replace, i'd rather just use the new bpf_cmp helpers than have the special array_elem helpers.

ok, cool, thanks for checking! The less special macros, the better.

Quoted 6 lines

thanks,

barret

#9 lore raw

Re: [PATCH bpf-next 2/2] selftests/bpf: add inline assembly helpers to access array elements

Quoted 8 lines · Andrii Nakryiko
Quoted 7 lines · Barret Rhoden
Quoted 2 lines · Andrii Nakryiko

I'm curious how bpf_cmp_likely/bpf_cmp_unlikely (just applied to bpf-next) compares to this?

these work great!

e.g.

Quoted 7 lines
Quoted 6 lines
          if (bpf_cmp_likely(idx, <, NR_MAP_ELEMS))
                  map_elems[idx] = i;

works fine. since that's essentially the code that bpf_array_elem() was trying to replace, i'd rather just use the new bpf_cmp helpers than have the special array_elem helpers.

ok, cool, thanks for checking! The less special macros, the better.

sorry - turns out it only worked in testing. in my actual program, i still run into issues. the comparison is done, which is what bpf_cmp enforces. but the compiler is discarding the comparison. i have more info in the other thread, but figured i'd mention it here too. =(

#10 lore raw

Re: [PATCH bpf-next 2/2] selftests/bpf: add inline assembly helpers to access array elements

In reply to Andrii Nakryiko
Quoted 8 lines · Andrii Nakryiko

tools/testing/selftests/bpf/prog_tests/test_array_elem.c

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/test_array_elem.c new file +7 -0
+
+/*
+ * Helper to load and run a program.
+ * Call must define skel, map_elems, and bss_elems.
+ * Destroy the skel when you're done.
+ */
+#define load_and_run(PROG) ({

does this have to be a macro? Can you write it as a function?

can do. (if we keep these patches).

i used a macro for the ## PROG below, but i can do something with ints and switches to turn on the autoload for a single prog. or just copy-paste the boilerplate.

Quoted 5 lines

tools/testing/selftests/bpf/prog_tests/test_array_elem.c line 18

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/test_array_elem.c new file +5 -0
+       int err;                                                        \
+       skel = array_elem_test__open();                                 \
+       if (!ASSERT_OK_PTR(skel, "array_elem_test open"))               \
+               return;                                                 \
+       bpf_program__set_autoload(skel->progs.x_ ## PROG, true);        \

thanks,

barret

#11 lore raw

Re: [PATCH bpf-next 2/2] selftests/bpf: add inline assembly helpers to access array elements

Quoted 11 lines · Barret Rhoden
Quoted 8 lines · Andrii Nakryiko

tools/testing/selftests/bpf/prog_tests/test_array_elem.c

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/test_array_elem.c new file +7 -0
+
+/*
+ * Helper to load and run a program.
+ * Call must define skel, map_elems, and bss_elems.
+ * Destroy the skel when you're done.
+ */
+#define load_and_run(PROG) ({

does this have to be a macro? Can you write it as a function?

Quoted 5 lines

can do. (if we keep these patches).

i used a macro for the ## PROG below, but i can do something with ints and switches to turn on the autoload for a single prog. or just copy-paste the boilerplate.

why can't you pass the struct bpf_program *prog parameter?

Quoted 12 lines
Quoted 6 lines

tools/testing/selftests/bpf/prog_tests/test_array_elem.c line 18

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/test_array_elem.c new file +5 -0
+       int err;                                                        \
+       skel = array_elem_test__open();                                 \
+       if (!ASSERT_OK_PTR(skel, "array_elem_test open"))               \
+               return;                                                 \
+       bpf_program__set_autoload(skel->progs.x_ ## PROG, true);        \

thanks,

barret