Skip to contentSkip to conversation

samples: bpf: refactor BPF map test with libbpf

PATCH Superseded Daniel T. Lee 3 participants 19 messages Jul 2, 2020 – Jul 7, 2020

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover samples: bpf: refactor BPF map test with libbpf

There have been many changes in how the current bpf program defines map. The development of libbbpf has led to the new method called BTF-defined map, which is a new way of defining BPF maps, and thus has a lot of differences from the existing MAP definition method.

Although bpf_load was also internally using libbbpf, fragmentation in its implementation began to occur, such as using its own structure, bpf_load_map_def, to define the map.

Therefore, in this patch set, map test programs, which are closely related to changes in the definition method of BPF map, were refactored with libbbpf.

Daniel T. Lee (4):
samples: bpf: fix bpf programs with kprobe/sys_connect event
samples: bpf: refactor BPF map in map test with libbpf
samples: bpf: refactor BPF map performance test with libbpf
selftests: bpf: remove unused bpf_map_def_legacy struct

 samples/bpf/Makefile                     |   4 +-
 samples/bpf/map_perf_test_kern.c         | 182 +++++++++++------------
 samples/bpf/map_perf_test_user.c         | 130 +++++++++++-----
 samples/bpf/test_map_in_map_kern.c       |  87 ++++++-----
 samples/bpf/test_map_in_map_user.c       |  53 ++++++-
 samples/bpf/test_probe_write_user_kern.c |   2 +-
 tools/testing/selftests/bpf/bpf_legacy.h |  14 --
 7 files changed, 275 insertions(+), 197 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/4 samples: bpf: fix bpf programs with kprobe/sys_connect event

Currently, BPF programs with kprobe/sys_connect does not work properly.

Commit 34745aed515c ("samples/bpf: fix kprobe attachment issue on x64") This commit modifies the bpf_load behavior of kprobe events in the x64 architecture. If the current kprobe event target starts with "sys_*", add the prefix "__x64_" to the front of the event.

Appending "__x64_" prefix with kprobe/sys_* event was appropriate as a solution to most of the problems caused by the commit below.

commit d5a00528b58c ("syscalls/core, syscalls/x86: Rename struct pt_regs-based sys_*() to __x64_sys_*()")

However, there is a problem with the sys_connect kprobe event that does not work properly. For __sys_connect event, parameters can be fetched normally, but for __x64_sys_connect, parameters cannot be fetched.

Because of this problem, this commit fixes the sys_connect event by specifying the __sys_connect directly and this will bypass the "__x64_" appending rule of bpf_load.

Fixes 34745aed515c ("samples/bpf: fix kprobe attachment issue on x64")
---
 samples/bpf/map_perf_test_kern.c         | 2 +-
 samples/bpf/test_map_in_map_kern.c       | 2 +-
 samples/bpf/test_probe_write_user_kern.c | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
samples/bpf/map_perf_test_kern.c +1 -1
index 12e91ae64d4d..cebe2098bb24 100644
@@ -154,7 +154,7 @@ int stress_percpu_hmap_alloc(struct pt_regs *ctx)
 	return 0;
 }
 
-SEC("kprobe/sys_connect")
+SEC("kprobe/__sys_connect")
 int stress_lru_hmap_alloc(struct pt_regs *ctx)
 {
 	char fmt[] = "Failed at stress_lru_hmap_alloc. ret:%dn";
samples/bpf/test_map_in_map_kern.c +1 -1
index 6cee61e8ce9b..b1562ba2f025 100644
@@ -102,7 +102,7 @@ static __always_inline int do_inline_hash_lookup(void *inner_map, u32 port)
 	return result ? *result : -ENOENT;
 }
 
-SEC("kprobe/sys_connect")
+SEC("kprobe/__sys_connect")
 int trace_sys_connect(struct pt_regs *ctx)
 {
 	struct sockaddr_in6 *in6;
samples/bpf/test_probe_write_user_kern.c +1 -1
index 6579639a83b2..9b3c3918c37d 100644
@@ -26,7 +26,7 @@ struct {
  * This example sits on a syscall, and the syscall ABI is relatively stable
  * of course, across platforms, and over time, the ABI may change.
  */
-SEC("kprobe/sys_connect")
+SEC("kprobe/__sys_connect")
 int bpf_prog1(struct pt_regs *ctx)
 {
 	struct sockaddr_in new_addr, orig_addr = {};
Signature · 1 line
2.25.1
#10 lore raw

PATCH 2/4 samples: bpf: refactor BPF map in map test with libbpf

In reply to Daniel T. Lee

From commit 646f02ffdd49 ("libbpf: Add BTF-defined map-in-map support"), a way to define internal map in BTF-defined map has been added.

Instead of using previous 'inner_map_idx' definition, the structure to be used for the inner map can be directly defined using array directive.

__array(values, struct inner_map)

This commit refactors map in map test program with libbpf by explicitly defining inner map with BTF-defined format.

---
 samples/bpf/Makefile               |  2 +-
 samples/bpf/test_map_in_map_kern.c | 85 +++++++++++++++---------------
 samples/bpf/test_map_in_map_user.c | 53 +++++++++++++++++--
 3 files changed, 91 insertions(+), 49 deletions(-)
samples/bpf/Makefile +1 -1
index ffd0fda536da..78678d4e6842 100644
@@ -93,7 +93,7 @@ sampleip-objs := sampleip_user.o $(TRACE_HELPERS)
 tc_l2_redirect-objs := bpf_load.o tc_l2_redirect_user.o
 lwt_len_hist-objs := bpf_load.o lwt_len_hist_user.o
 xdp_tx_iptunnel-objs := xdp_tx_iptunnel_user.o
-test_map_in_map-objs := bpf_load.o test_map_in_map_user.o
+test_map_in_map-objs := test_map_in_map_user.o
 per_socket_stats_example-objs := cookie_uid_helper_example.o
 xdp_redirect-objs := xdp_redirect_user.o
 xdp_redirect_map-objs := xdp_redirect_map_user.o
samples/bpf/test_map_in_map_kern.c +42 -43

108 lines not on this page · raw message

samples/bpf/test_map_in_map_user.c +48 -5

73 lines not on this page · raw message

Signature · 1 line
2.25.1
#13 lore raw

PATCH 3/4 samples: bpf: refactor BPF map performance test with libbpf

In reply to Daniel T. Lee

Previously, in order to set the numa_node attribute at the time of map creation using "libbpf", it was necessary to call bpf_create_map_node() directly (bpf_load approach), instead of calling bpf_object_load() that handles everything on its own, including map creation. And because of this problem, this sample had problems with refactoring from bpf_load to libbbpf.

However, by commit 1bdb6c9a1c43 ("libbpf: Add a bunch of attribute getters/setters for map definitions"), a helper function which allows the numa_node attribute to be set in the map prior to calling bpf_object_load() has been added.

By using libbpf instead of bpf_load, the inner map definition has been explicitly declared with BTF-defined format. And for this reason some logic in fixup_map() was not needed and changed or removed.

---
 samples/bpf/Makefile             |   2 +-
 samples/bpf/map_perf_test_kern.c | 180 +++++++++++++++----------------
 samples/bpf/map_perf_test_user.c | 130 +++++++++++++++-------
 3 files changed, 181 insertions(+), 131 deletions(-)
samples/bpf/Makefile +1 -1
index 78678d4e6842..0cc7f18370c6 100644
@@ -76,7 +76,7 @@ trace_output-objs := trace_output_user.o $(TRACE_HELPERS)
 lathist-objs := lathist_user.o
 offwaketime-objs := offwaketime_user.o $(TRACE_HELPERS)
 spintest-objs := spintest_user.o $(TRACE_HELPERS)
-map_perf_test-objs := bpf_load.o map_perf_test_user.o
+map_perf_test-objs := map_perf_test_user.o
 test_overhead-objs := bpf_load.o test_overhead_user.o
 test_cgrp2_array_pin-objs := test_cgrp2_array_pin.o
 test_cgrp2_attach-objs := test_cgrp2_attach.o
samples/bpf/map_perf_test_kern.c +90 -90

227 lines not on this page · raw message

samples/bpf/map_perf_test_user.c +90 -40

196 lines not on this page · raw message

Signature · 1 line
2.25.1
#17 lore raw

PATCH 4/4 selftests: bpf: remove unused bpf_map_def_legacy struct

In reply to Daniel T. Lee

samples/bpf no longer use bpf_map_def_legacy and instead use the libbpf's bpf_map_def or new BTF-defined MAP format. This commit removes unused bpf_map_def_legacy struct from selftests/bpf/bpf_legacy.h.

---
 tools/testing/selftests/bpf/bpf_legacy.h | 14 --------------
 1 file changed, 14 deletions(-)
tools/testing/selftests/bpf/bpf_legacy.h +0 -14
index 6f8988738bc1..719ab56cdb5d 100644
@@ -2,20 +2,6 @@
 #ifndef __BPF_LEGACY__
 #define __BPF_LEGACY__
 
-/*
- * legacy bpf_map_def with extra fields supported only by bpf_load(), do not
- * use outside of samples/bpf
- */
-struct bpf_map_def_legacy {
-	unsigned int type;
-	unsigned int key_size;
-	unsigned int value_size;
-	unsigned int max_entries;
-	unsigned int map_flags;
-	unsigned int inner_map_idx;
-	unsigned int numa_node;
-};
-
 #define BPF_ANNOTATE_KV_PAIR(name, type_key, type_val)		\
 	struct ____btf_map_##name {				\
 		type_key key;					\
Signature · 1 line
2.25.1
#11 lore raw

Re: [PATCH bpf-next 2/4] samples: bpf: refactor BPF map in map test with libbpf

In reply to Daniel T. Lee
Quoted 20 lines · Daniel T. Lee

From commit 646f02ffdd49 ("libbpf: Add BTF-defined map-in-map support"), a way to define internal map in BTF-defined map has been added.

Instead of using previous 'inner_map_idx' definition, the structure to be used for the inner map can be directly defined using array directive.

__array(values, struct inner_map)

This commit refactors map in map test program with libbpf by explicitly defining inner map with BTF-defined format.

---
 samples/bpf/Makefile               |  2 +-
 samples/bpf/test_map_in_map_kern.c | 85 +++++++++++++++---------------
 samples/bpf/test_map_in_map_user.c | 53 +++++++++++++++++--
 3 files changed, 91 insertions(+), 49 deletions(-)

[...]

samples/bpf/test_map_in_map_user.c static void test_map_in_map(void) · line 136

Quoted 5 lines
samples/bpf/test_map_in_map_user.c +2 -3
-       if (load_bpf_file(filename)) {
-               printf("%s", bpf_log_buf);
-               return 1;
+       prog = bpf_object__find_program_by_name(obj, "trace_sys_connect");
+       if (libbpf_get_error(prog)) {

still wrong, just if (!prog)

Quoted 4 lines
samples/bpf/test_map_in_map_user.c +4 -0
+               printf("finding a prog in obj file failed\n");
+               goto cleanup;
+       }
+

[...]

#14 lore raw

Re: [PATCH bpf-next 3/4] samples: bpf: refactor BPF map performance test with libbpf

In reply to Daniel T. Lee
Quoted 24 lines · Daniel T. Lee

Previously, in order to set the numa_node attribute at the time of map creation using "libbpf", it was necessary to call bpf_create_map_node() directly (bpf_load approach), instead of calling bpf_object_load() that handles everything on its own, including map creation. And because of this problem, this sample had problems with refactoring from bpf_load to libbbpf.

However, by commit 1bdb6c9a1c43 ("libbpf: Add a bunch of attribute getters/setters for map definitions"), a helper function which allows the numa_node attribute to be set in the map prior to calling bpf_object_load() has been added.

By using libbpf instead of bpf_load, the inner map definition has been explicitly declared with BTF-defined format. And for this reason some logic in fixup_map() was not needed and changed or removed.

---
 samples/bpf/Makefile             |   2 +-
 samples/bpf/map_perf_test_kern.c | 180 +++++++++++++++----------------
 samples/bpf/map_perf_test_user.c | 130 +++++++++++++++-------
 3 files changed, 181 insertions(+), 131 deletions(-)

[...]

samples/bpf/map_perf_test_kern.c line 46

Quoted 7 lines
samples/bpf/map_perf_test_kern.c +7 -0
+struct inner_lru {
+       __uint(type, BPF_MAP_TYPE_LRU_HASH);
+       __type(key, u32);
+       __type(value, long);
+       __uint(max_entries, MAX_ENTRIES);
+       __uint(map_flags, BPF_F_NUMA_NODE); /* from _user.c, set numa_node to 0 */
+} inner_lru_hash_map SEC(".maps");

you can declaratively set numa_node here with __uint(numa_node, 0), which is actually a default, but for explicitness it's better

Quoted 8 lines
samples/bpf/map_perf_test_kern.c +8 -0
+
+struct {
+       __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
+       __uint(max_entries, MAX_NR_CPUS);
+       __uint(key_size, sizeof(u32));
+       __array(values, struct inner_lru); /* use inner_lru as inner map */
+} array_of_lru_hashs SEC(".maps");
+

[...]

Quoted 4 lines
samples/bpf/map_perf_test_user.c +2 -1
-static void fixup_map(struct bpf_map_data *map, int idx)
+static void fixup_map(struct bpf_object *obj)
 {
+       struct bpf_map *map;

samples/bpf/map_perf_test_user.c static void fill_lpm_trie(void) · line 404

Quoted 10 lines
samples/bpf/map_perf_test_user.c +2 -5
        int i;
 
-       if (!strcmp("inner_lru_hash_map", map->name)) {
-               inner_lru_hash_idx = idx;
-               inner_lru_hash_size = map->def.max_entries;
-       }
+       bpf_object__for_each_map(map, obj) {
+               const char *name = bpf_map__name(map);
 
-       if (!strcmp("array_of_lru_hashs", map->name)) {

I'm a bit too lazy right now to figure out exact logic here, but just wanted to mention that it is possible to statically set inner map elements for array_of_maps and hash_of_maps. Please check tools/testing/selftests/bpf/progs/test_btf_map_in_map.c and see if you can use this feature to simplify this logic a bit.

Quoted 15 lines
samples/bpf/map_perf_test_user.c +7 -5
-               if (inner_lru_hash_idx == -1) {
-                       printf("inner_lru_hash_map must be defined before array_of_lru_hashs\n");
-                       exit(1);
+               /* Only change the max_entries for the enabled test(s) */
+               for (i = 0; i < NR_TESTS; i++) {
+                       if (!strcmp(test_map_names[i], name) &&
+                           (check_test_flags(i))) {
+                               bpf_map__resize(map, num_map_entries);
+                               continue;
+                       }
                }
-               map->def.inner_map_idx = inner_lru_hash_idx;
-               array_of_lru_hashs_idx = idx;
        }

[...]

Acked-by on v1 4/4
#18 lore raw

Re: [PATCH bpf-next 4/4] selftests: bpf: remove unused bpf_map_def_legacy struct

In reply to Daniel T. Lee
Quoted 7 lines · Daniel T. Lee

samples/bpf no longer use bpf_map_def_legacy and instead use the libbpf's bpf_map_def or new BTF-defined MAP format. This commit removes unused bpf_map_def_legacy struct from selftests/bpf/bpf_legacy.h.

---

Great, long time coming! Thank you! Some day entire bpf_load.{c,h} will also be gone :)

Quoted 3 lines
 tools/testing/selftests/bpf/bpf_legacy.h | 14 --------------
 1 file changed, 14 deletions(-)

[...]

#2 lore raw

Re: [PATCH bpf-next 1/4] samples: bpf: fix bpf programs with kprobe/sys_connect event

In reply to Daniel T. Lee
Quoted 13 lines · Daniel T. Lee

Currently, BPF programs with kprobe/sys_connect does not work properly.

Commit 34745aed515c ("samples/bpf: fix kprobe attachment issue on x64") This commit modifies the bpf_load behavior of kprobe events in the x64 architecture. If the current kprobe event target starts with "sys_*", add the prefix "__x64_" to the front of the event.

Appending "__x64_" prefix with kprobe/sys_* event was appropriate as a solution to most of the problems caused by the commit below.

commit d5a00528b58c ("syscalls/core, syscalls/x86: Rename struct pt_regs-based sys_*() to __x64_sys_*()")

Quoted 7 lines

However, there is a problem with the sys_connect kprobe event that does not work properly. For __sys_connect event, parameters can be fetched normally, but for __x64_sys_connect, parameters cannot be fetched.

Because of this problem, this commit fixes the sys_connect event by specifying the __sys_connect directly and this will bypass the "__x64_" appending rule of bpf_load.

In the kernel code, we have

SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
int, addrlen)

{
        return __sys_connect(fd, uservaddr, addrlen);
}

Depending on compiler, there is no guarantee that __sys_connect will not be inlined. I would prefer to still use the entry point __x64_sys_* e.g.,
SEC("kprobe/" SYSCALL(sys_write))

Quoted 49 lines
Fixes 34745aed515c ("samples/bpf: fix kprobe attachment issue on x64")
---
  samples/bpf/map_perf_test_kern.c         | 2 +-
  samples/bpf/test_map_in_map_kern.c       | 2 +-
  samples/bpf/test_probe_write_user_kern.c | 2 +-
  3 files changed, 3 insertions(+), 3 deletions(-)
samples/bpf/map_perf_test_kern.c +1 -1
index 12e91ae64d4d..cebe2098bb24 100644
@@ -154,7 +154,7 @@ int stress_percpu_hmap_alloc(struct pt_regs *ctx)
  	return 0;
  }
 
-SEC("kprobe/sys_connect")
+SEC("kprobe/__sys_connect")
  int stress_lru_hmap_alloc(struct pt_regs *ctx)
  {
  	char fmt[] = "Failed at stress_lru_hmap_alloc. ret:%dn";
samples/bpf/test_map_in_map_kern.c +1 -1
index 6cee61e8ce9b..b1562ba2f025 100644
@@ -102,7 +102,7 @@ static __always_inline int do_inline_hash_lookup(void *inner_map, u32 port)
  	return result ? *result : -ENOENT;
  }
 
-SEC("kprobe/sys_connect")
+SEC("kprobe/__sys_connect")
  int trace_sys_connect(struct pt_regs *ctx)
  {
  	struct sockaddr_in6 *in6;
samples/bpf/test_probe_write_user_kern.c +1 -1
index 6579639a83b2..9b3c3918c37d 100644
@@ -26,7 +26,7 @@ struct {
   * This example sits on a syscall, and the syscall ABI is relatively stable
   * of course, across platforms, and over time, the ABI may change.
   */
-SEC("kprobe/sys_connect")
+SEC("kprobe/__sys_connect")
  int bpf_prog1(struct pt_regs *ctx)
  {
  	struct sockaddr_in new_addr, orig_addr = {};
#12 lore raw

Re: [PATCH bpf-next 2/4] samples: bpf: refactor BPF map in map test with libbpf

In reply to Andrii Nakryiko
Quoted 25 lines · Andrii Nakryiko
Quoted 20 lines · Daniel T. Lee

From commit 646f02ffdd49 ("libbpf: Add BTF-defined map-in-map support"), a way to define internal map in BTF-defined map has been added.

Instead of using previous 'inner_map_idx' definition, the structure to be used for the inner map can be directly defined using array directive.

__array(values, struct inner_map)

This commit refactors map in map test program with libbpf by explicitly defining inner map with BTF-defined format.

---
 samples/bpf/Makefile               |  2 +-
 samples/bpf/test_map_in_map_kern.c | 85 +++++++++++++++---------------
 samples/bpf/test_map_in_map_user.c | 53 +++++++++++++++++--
 3 files changed, 91 insertions(+), 49 deletions(-)

[...]

Quoted 8 lines

samples/bpf/test_map_in_map_user.c static void test_map_in_map(void) · line 136

Quoted 5 lines
samples/bpf/test_map_in_map_user.c +2 -3
-       if (load_bpf_file(filename)) {
-               printf("%s", bpf_log_buf);
-               return 1;
+       prog = bpf_object__find_program_by_name(obj, "trace_sys_connect");
+       if (libbpf_get_error(prog)) {

still wrong, just if (!prog)

Oops, my bad.
Will fix right away.

Thanks for your time and effort for the review.
Daniel.

Quoted 6 lines
Quoted 4 lines
samples/bpf/test_map_in_map_user.c +4 -0
+               printf("finding a prog in obj file failed\n");
+               goto cleanup;
+       }
+

[...]

#3 lore raw

Re: [PATCH bpf-next 1/4] samples: bpf: fix bpf programs with kprobe/sys_connect event

In reply to Yonghong Song
Quoted 29 lines · Yonghong Song
Quoted 13 lines · Daniel T. Lee

Currently, BPF programs with kprobe/sys_connect does not work properly.

Commit 34745aed515c ("samples/bpf: fix kprobe attachment issue on x64") This commit modifies the bpf_load behavior of kprobe events in the x64 architecture. If the current kprobe event target starts with "sys_*", add the prefix "__x64_" to the front of the event.

Appending "__x64_" prefix with kprobe/sys_* event was appropriate as a solution to most of the problems caused by the commit below.

commit d5a00528b58c ("syscalls/core, syscalls/x86: Rename struct pt_regs-based sys_*() to __x64_sys_*()")

Quoted 7 lines

However, there is a problem with the sys_connect kprobe event that does not work properly. For __sys_connect event, parameters can be fetched normally, but for __x64_sys_connect, parameters cannot be fetched.

Because of this problem, this commit fixes the sys_connect event by specifying the __sys_connect directly and this will bypass the "__x64_" appending rule of bpf_load.

In the kernel code, we have

SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
int, addrlen)

Quoted 9 lines
{
         return __sys_connect(fd, uservaddr, addrlen);
}

Depending on compiler, there is no guarantee that __sys_connect will not be inlined. I would prefer to still use the entry point __x64_sys_* e.g.,
SEC("kprobe/" SYSCALL(sys_write))

As you mentioned, there is clearly a possibility that problems may arise because the symbol does not exist according to the compiler.

However, in x64, when using Kprobe for __x64_sys_connect event, the tests are not working properly because the parameters cannot be fetched, and the test under selftests/bpf is using "kprobe/_sys_connect" directly.

I'm not sure how to deal with this problem. Any advice and suggestions will be greatly appreciated.

Thanks for your time and effort for the review.
Daniel

Quoted 49 lines
Quoted 49 lines
Fixes 34745aed515c ("samples/bpf: fix kprobe attachment issue on x64")
---
  samples/bpf/map_perf_test_kern.c         | 2 +-
  samples/bpf/test_map_in_map_kern.c       | 2 +-
  samples/bpf/test_probe_write_user_kern.c | 2 +-
  3 files changed, 3 insertions(+), 3 deletions(-)
samples/bpf/map_perf_test_kern.c +1 -1
index 12e91ae64d4d..cebe2098bb24 100644
@@ -154,7 +154,7 @@ int stress_percpu_hmap_alloc(struct pt_regs *ctx)
      return 0;
  }
 
-SEC("kprobe/sys_connect")
+SEC("kprobe/__sys_connect")
  int stress_lru_hmap_alloc(struct pt_regs *ctx)
  {
      char fmt[] = "Failed at stress_lru_hmap_alloc. ret:%dn";
samples/bpf/test_map_in_map_kern.c +1 -1
index 6cee61e8ce9b..b1562ba2f025 100644
@@ -102,7 +102,7 @@ static __always_inline int do_inline_hash_lookup(void *inner_map, u32 port)
      return result ? *result : -ENOENT;
  }
 
-SEC("kprobe/sys_connect")
+SEC("kprobe/__sys_connect")
  int trace_sys_connect(struct pt_regs *ctx)
  {
      struct sockaddr_in6 *in6;
samples/bpf/test_probe_write_user_kern.c +1 -1
index 6579639a83b2..9b3c3918c37d 100644
@@ -26,7 +26,7 @@ struct {
   * This example sits on a syscall, and the syscall ABI is relatively stable
   * of course, across platforms, and over time, the ABI may change.
   */
-SEC("kprobe/sys_connect")
+SEC("kprobe/__sys_connect")
  int bpf_prog1(struct pt_regs *ctx)
  {
      struct sockaddr_in new_addr, orig_addr = {};
#15 lore raw

Re: [PATCH bpf-next 3/4] samples: bpf: refactor BPF map performance test with libbpf

In reply to Andrii Nakryiko
Quoted 29 lines · Andrii Nakryiko
Quoted 24 lines · Daniel T. Lee

Previously, in order to set the numa_node attribute at the time of map creation using "libbpf", it was necessary to call bpf_create_map_node() directly (bpf_load approach), instead of calling bpf_object_load() that handles everything on its own, including map creation. And because of this problem, this sample had problems with refactoring from bpf_load to libbbpf.

However, by commit 1bdb6c9a1c43 ("libbpf: Add a bunch of attribute getters/setters for map definitions"), a helper function which allows the numa_node attribute to be set in the map prior to calling bpf_object_load() has been added.

By using libbpf instead of bpf_load, the inner map definition has been explicitly declared with BTF-defined format. And for this reason some logic in fixup_map() was not needed and changed or removed.

---
 samples/bpf/Makefile             |   2 +-
 samples/bpf/map_perf_test_kern.c | 180 +++++++++++++++----------------
 samples/bpf/map_perf_test_user.c | 130 +++++++++++++++-------
 3 files changed, 181 insertions(+), 131 deletions(-)

[...]

Quoted 11 lines

samples/bpf/map_perf_test_kern.c line 46

Quoted 7 lines
samples/bpf/map_perf_test_kern.c +7 -0
+struct inner_lru {
+       __uint(type, BPF_MAP_TYPE_LRU_HASH);
+       __type(key, u32);
+       __type(value, long);
+       __uint(max_entries, MAX_ENTRIES);
+       __uint(map_flags, BPF_F_NUMA_NODE); /* from _user.c, set numa_node to 0 */
+} inner_lru_hash_map SEC(".maps");

you can declaratively set numa_node here with __uint(numa_node, 0), which is actually a default, but for explicitness it's better

It would make _user.c code cleaner, but as you said, I'll keep with this implementation.

Quoted 24 lines
Quoted 8 lines
samples/bpf/map_perf_test_kern.c +8 -0
+
+struct {
+       __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
+       __uint(max_entries, MAX_NR_CPUS);
+       __uint(key_size, sizeof(u32));
+       __array(values, struct inner_lru); /* use inner_lru as inner map */
+} array_of_lru_hashs SEC(".maps");
+

[...]

Quoted 13 lines
samples/bpf/map_perf_test_user.c +4 -5
-static void fixup_map(struct bpf_map_data *map, int idx)
+static void fixup_map(struct bpf_object *obj)
 {
+       struct bpf_map *map;
        int i;
 
-       if (!strcmp("inner_lru_hash_map", map->name)) {
-               inner_lru_hash_idx = idx;
-               inner_lru_hash_size = map->def.max_entries;
-       }
+       bpf_object__for_each_map(map, obj) {
+               const char *name = bpf_map__name(map);
Quoted 8 lines
Quoted 1 line

- if (!strcmp("array_of_lru_hashs", map->name)) {

I'm a bit too lazy right now to figure out exact logic here, but just wanted to mention that it is possible to statically set inner map elements for array_of_maps and hash_of_maps. Please check tools/testing/selftests/bpf/progs/test_btf_map_in_map.c and see if you can use this feature to simplify this logic a bit.

Thanks for the feedback! But I'm not sure I'm following properly.

If what you are talking about is specifying the inner_map_idx of array_of_lru_hashes, I've changed it by using the __array() directives of the BTF-defined MAP.

Since inner_map_idx logic has been replaced with BTF-defined map definition, the only thing left at here fixup_map() is just resizing map size with bpf_map__resize.

Thanks for your time and effort for the review.
Daniel

Quoted 17 lines
Quoted 15 lines
samples/bpf/map_perf_test_user.c +7 -5
-               if (inner_lru_hash_idx == -1) {
-                       printf("inner_lru_hash_map must be defined before array_of_lru_hashs\n");
-                       exit(1);
+               /* Only change the max_entries for the enabled test(s) */
+               for (i = 0; i < NR_TESTS; i++) {
+                       if (!strcmp(test_map_names[i], name) &&
+                           (check_test_flags(i))) {
+                               bpf_map__resize(map, num_map_entries);
+                               continue;
+                       }
                }
-               map->def.inner_map_idx = inner_lru_hash_idx;
-               array_of_lru_hashs_idx = idx;
        }

[...]

#4 lore raw

Re: [PATCH bpf-next 1/4] samples: bpf: fix bpf programs with kprobe/sys_connect event

In reply to Daniel T. Lee
Quoted 40 lines · Daniel T. Lee
Quoted 29 lines · Yonghong Song
Quoted 13 lines · Daniel T. Lee

Currently, BPF programs with kprobe/sys_connect does not work properly.

Commit 34745aed515c ("samples/bpf: fix kprobe attachment issue on x64") This commit modifies the bpf_load behavior of kprobe events in the x64 architecture. If the current kprobe event target starts with "sys_*", add the prefix "__x64_" to the front of the event.

Appending "__x64_" prefix with kprobe/sys_* event was appropriate as a solution to most of the problems caused by the commit below.

commit d5a00528b58c ("syscalls/core, syscalls/x86: Rename struct pt_regs-based sys_*() to __x64_sys_*()")

Quoted 7 lines

However, there is a problem with the sys_connect kprobe event that does not work properly. For __sys_connect event, parameters can be fetched normally, but for __x64_sys_connect, parameters cannot be fetched.

Because of this problem, this commit fixes the sys_connect event by specifying the __sys_connect directly and this will bypass the "__x64_" appending rule of bpf_load.

In the kernel code, we have

SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
int, addrlen)

Quoted 9 lines
{
          return __sys_connect(fd, uservaddr, addrlen);
}

Depending on compiler, there is no guarantee that __sys_connect will not be inlined. I would prefer to still use the entry point __x64_sys_* e.g.,
SEC("kprobe/" SYSCALL(sys_write))

Quoted 6 lines

As you mentioned, there is clearly a possibility that problems may arise because the symbol does not exist according to the compiler.

However, in x64, when using Kprobe for __x64_sys_connect event, the tests are not working properly because the parameters cannot be fetched, and the test under selftests/bpf is using "kprobe/_sys_connect" directly.

This is the assembly code for __x64_sys_connect.

ffffffff818d3520 <__x64_sys_connect>:
ffffffff818d3520: e8 fb df 32 00 callq 0xffffffff81c01520 <__fentry__>
ffffffff818d3525: 48 8b 57 60 movq 96(%rdi), %rdx
ffffffff818d3529: 48 8b 77 68 movq 104(%rdi), %rsi
ffffffff818d352d: 48 8b 7f 70 movq 112(%rdi), %rdi
ffffffff818d3531: e8 1a ff ff ff callq 0xffffffff818d3450 <__sys_connect>
ffffffff818d3536: 48 98 cltq
ffffffff818d3538: c3 retq
ffffffff818d3539: 0f 1f 80 00 00 00 00 nopl (%rax)

In bpf program, the step is:

      struct pt_regs *real_regs = PT_REGS_PARM1(pt_regs);
      param1 = PT_REGS_PARM1(real_regs);
      param2 = PT_REGS_PARM2(real_regs);
      param3 = PT_REGS_PARM3(real_regs);

The same for s390.

For other architectures, no above indirection is needed.

I guess you can abstract the above into trace_common.h?

Quoted 56 lines

I'm not sure how to deal with this problem. Any advice and suggestions will be greatly appreciated.

Thanks for your time and effort for the review.
Daniel

Quoted 49 lines
Quoted 49 lines
Fixes 34745aed515c ("samples/bpf: fix kprobe attachment issue on x64")
---
   samples/bpf/map_perf_test_kern.c         | 2 +-
   samples/bpf/test_map_in_map_kern.c       | 2 +-
   samples/bpf/test_probe_write_user_kern.c | 2 +-
   3 files changed, 3 insertions(+), 3 deletions(-)
samples/bpf/map_perf_test_kern.c +1 -1
index 12e91ae64d4d..cebe2098bb24 100644
@@ -154,7 +154,7 @@ int stress_percpu_hmap_alloc(struct pt_regs *ctx)
       return 0;
   }
 
-SEC("kprobe/sys_connect")
+SEC("kprobe/__sys_connect")
   int stress_lru_hmap_alloc(struct pt_regs *ctx)
   {
       char fmt[] = "Failed at stress_lru_hmap_alloc. ret:%dn";
samples/bpf/test_map_in_map_kern.c +1 -1
index 6cee61e8ce9b..b1562ba2f025 100644
@@ -102,7 +102,7 @@ static __always_inline int do_inline_hash_lookup(void *inner_map, u32 port)
       return result ? *result : -ENOENT;
   }
 
-SEC("kprobe/sys_connect")
+SEC("kprobe/__sys_connect")
   int trace_sys_connect(struct pt_regs *ctx)
   {
       struct sockaddr_in6 *in6;
samples/bpf/test_probe_write_user_kern.c +1 -1
index 6579639a83b2..9b3c3918c37d 100644
@@ -26,7 +26,7 @@ struct {
    * This example sits on a syscall, and the syscall ABI is relatively stable
    * of course, across platforms, and over time, the ABI may change.
    */
-SEC("kprobe/sys_connect")
+SEC("kprobe/__sys_connect")
   int bpf_prog1(struct pt_regs *ctx)
   {
       struct sockaddr_in new_addr, orig_addr = {};
#5 lore raw

Re: [PATCH bpf-next 1/4] samples: bpf: fix bpf programs with kprobe/sys_connect event

Quoted 65 lines · Yonghong Song
Quoted 40 lines · Daniel T. Lee
Quoted 29 lines · Yonghong Song
Quoted 13 lines · Daniel T. Lee

Currently, BPF programs with kprobe/sys_connect does not work properly.

Commit 34745aed515c ("samples/bpf: fix kprobe attachment issue on x64") This commit modifies the bpf_load behavior of kprobe events in the x64 architecture. If the current kprobe event target starts with "sys_*", add the prefix "__x64_" to the front of the event.

Appending "__x64_" prefix with kprobe/sys_* event was appropriate as a solution to most of the problems caused by the commit below.

commit d5a00528b58c ("syscalls/core, syscalls/x86: Rename struct pt_regs-based sys_*() to __x64_sys_*()")

Quoted 7 lines

However, there is a problem with the sys_connect kprobe event that does not work properly. For __sys_connect event, parameters can be fetched normally, but for __x64_sys_connect, parameters cannot be fetched.

Because of this problem, this commit fixes the sys_connect event by specifying the __sys_connect directly and this will bypass the "__x64_" appending rule of bpf_load.

In the kernel code, we have

SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
int, addrlen)

Quoted 9 lines
{
          return __sys_connect(fd, uservaddr, addrlen);
}

Depending on compiler, there is no guarantee that __sys_connect will not be inlined. I would prefer to still use the entry point __x64_sys_* e.g.,
SEC("kprobe/" SYSCALL(sys_write))

Quoted 6 lines

As you mentioned, there is clearly a possibility that problems may arise because the symbol does not exist according to the compiler.

However, in x64, when using Kprobe for __x64_sys_connect event, the tests are not working properly because the parameters cannot be fetched, and the test under selftests/bpf is using "kprobe/_sys_connect" directly.

This is the assembly code for __x64_sys_connect.

ffffffff818d3520 <__x64_sys_connect>:
ffffffff818d3520: e8 fb df 32 00 callq 0xffffffff81c01520
<__fentry__>
ffffffff818d3525: 48 8b 57 60 movq 96(%rdi), %rdx
ffffffff818d3529: 48 8b 77 68 movq 104(%rdi), %rsi
ffffffff818d352d: 48 8b 7f 70 movq 112(%rdi), %rdi
ffffffff818d3531: e8 1a ff ff ff callq 0xffffffff818d3450
<__sys_connect>
ffffffff818d3536: 48 98 cltq
ffffffff818d3538: c3 retq
ffffffff818d3539: 0f 1f 80 00 00 00 00 nopl (%rax)

Quoted 7 lines

In bpf program, the step is:

       struct pt_regs *real_regs = PT_REGS_PARM1(pt_regs);
       param1 = PT_REGS_PARM1(real_regs);
       param2 = PT_REGS_PARM2(real_regs);
       param3 = PT_REGS_PARM3(real_regs);

The same for s390.

I'm sorry that I seem to get it wrong,
But is it available to access 'struct pt_regs *' recursively?

It seems nested use of PT_REGS_PARM causes invalid memory access.

    $ sudo ./test_probe_write_user
    libbpf: load bpf program failed: Permission denied
    libbpf: -- BEGIN DUMP LOG ---
    libbpf:
    Unrecognized arg#0 type PTR
    ; struct pt_regs *real_regs = PT_REGS_PARM1(ctx);
    0: (79) r1 = *(u64 *)(r1 +112)
    ; void *sockaddr_arg = (void *)PT_REGS_PARM2(real_regs);
    1: (79) r6 = *(u64 *)(r1 +104)
    R1 invalid mem access 'inv'
    processed 2 insns (limit 1000000) max_states_per_insn 0
total_states 0 peak_states 0 mark_read 0

libbpf: -- END LOG --
libbpf: failed to load program 'kprobe/__x64_sys_connect'
libbpf: failed to load object './test_probe_write_user_kern.o'
ERROR: loading BPF object file failed

I'm not fully aware of the BPF verifier's internal structure. Is there any workaround to solve this problem?

Thanks for your time and effort for the review.
Daniel.

Quoted 61 lines

For other architectures, no above indirection is needed.

I guess you can abstract the above into trace_common.h?

Quoted 56 lines

I'm not sure how to deal with this problem. Any advice and suggestions will be greatly appreciated.

Thanks for your time and effort for the review.
Daniel

Quoted 49 lines
Quoted 49 lines
Fixes 34745aed515c ("samples/bpf: fix kprobe attachment issue on x64")
---
   samples/bpf/map_perf_test_kern.c         | 2 +-
   samples/bpf/test_map_in_map_kern.c       | 2 +-
   samples/bpf/test_probe_write_user_kern.c | 2 +-
   3 files changed, 3 insertions(+), 3 deletions(-)
samples/bpf/map_perf_test_kern.c +1 -1
index 12e91ae64d4d..cebe2098bb24 100644
@@ -154,7 +154,7 @@ int stress_percpu_hmap_alloc(struct pt_regs *ctx)
       return 0;
   }
 
-SEC("kprobe/sys_connect")
+SEC("kprobe/__sys_connect")
   int stress_lru_hmap_alloc(struct pt_regs *ctx)
   {
       char fmt[] = "Failed at stress_lru_hmap_alloc. ret:%dn";
samples/bpf/test_map_in_map_kern.c +1 -1
index 6cee61e8ce9b..b1562ba2f025 100644
@@ -102,7 +102,7 @@ static __always_inline int do_inline_hash_lookup(void *inner_map, u32 port)
       return result ? *result : -ENOENT;
   }
 
-SEC("kprobe/sys_connect")
+SEC("kprobe/__sys_connect")
   int trace_sys_connect(struct pt_regs *ctx)
   {
       struct sockaddr_in6 *in6;
samples/bpf/test_probe_write_user_kern.c +1 -1
index 6579639a83b2..9b3c3918c37d 100644
@@ -26,7 +26,7 @@ struct {
    * This example sits on a syscall, and the syscall ABI is relatively stable
    * of course, across platforms, and over time, the ABI may change.
    */
-SEC("kprobe/sys_connect")
+SEC("kprobe/__sys_connect")
   int bpf_prog1(struct pt_regs *ctx)
   {
       struct sockaddr_in new_addr, orig_addr = {};
#16 lore raw

Re: [PATCH bpf-next 3/4] samples: bpf: refactor BPF map performance test with libbpf

In reply to Daniel T. Lee
Quoted 44 lines · Daniel T. Lee
Quoted 29 lines · Andrii Nakryiko
Quoted 24 lines · Daniel T. Lee

Previously, in order to set the numa_node attribute at the time of map creation using "libbpf", it was necessary to call bpf_create_map_node() directly (bpf_load approach), instead of calling bpf_object_load() that handles everything on its own, including map creation. And because of this problem, this sample had problems with refactoring from bpf_load to libbbpf.

However, by commit 1bdb6c9a1c43 ("libbpf: Add a bunch of attribute getters/setters for map definitions"), a helper function which allows the numa_node attribute to be set in the map prior to calling bpf_object_load() has been added.

By using libbpf instead of bpf_load, the inner map definition has been explicitly declared with BTF-defined format. And for this reason some logic in fixup_map() was not needed and changed or removed.

---
 samples/bpf/Makefile             |   2 +-
 samples/bpf/map_perf_test_kern.c | 180 +++++++++++++++----------------
 samples/bpf/map_perf_test_user.c | 130 +++++++++++++++-------
 3 files changed, 181 insertions(+), 131 deletions(-)

[...]

Quoted 11 lines

samples/bpf/map_perf_test_kern.c line 46

Quoted 7 lines
samples/bpf/map_perf_test_kern.c +7 -0
+struct inner_lru {
+       __uint(type, BPF_MAP_TYPE_LRU_HASH);
+       __type(key, u32);
+       __type(value, long);
+       __uint(max_entries, MAX_ENTRIES);
+       __uint(map_flags, BPF_F_NUMA_NODE); /* from _user.c, set numa_node to 0 */
+} inner_lru_hash_map SEC(".maps");

you can declaratively set numa_node here with __uint(numa_node, 0), which is actually a default, but for explicitness it's better

Quoted 2 lines

It would make _user.c code cleaner, but as you said, I'll keep with this implementation.

I meant to do __uint(numa_node, 0) declaratively, even if it's a bit redundant (because default value is already zero).

user-space bpf_program__numa_node() is inferior for such a simple static use case.

Quoted 36 lines
Quoted 25 lines
Quoted 9 lines
samples/bpf/map_perf_test_kern.c +8 -0
+
+struct {
+       __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
+       __uint(max_entries, MAX_NR_CPUS);
+       __uint(key_size, sizeof(u32));
+       __array(values, struct inner_lru); /* use inner_lru as inner map */
+} array_of_lru_hashs SEC(".maps");
+

[...]

Quoted 13 lines
samples/bpf/map_perf_test_user.c +4 -5
-static void fixup_map(struct bpf_map_data *map, int idx)
+static void fixup_map(struct bpf_object *obj)
 {
+       struct bpf_map *map;
        int i;
 
-       if (!strcmp("inner_lru_hash_map", map->name)) {
-               inner_lru_hash_idx = idx;
-               inner_lru_hash_size = map->def.max_entries;
-       }
+       bpf_object__for_each_map(map, obj) {
+               const char *name = bpf_map__name(map);
Quoted 8 lines
Quoted 1 line

- if (!strcmp("array_of_lru_hashs", map->name)) {

I'm a bit too lazy right now to figure out exact logic here, but just wanted to mention that it is possible to statically set inner map elements for array_of_maps and hash_of_maps. Please check tools/testing/selftests/bpf/progs/test_btf_map_in_map.c and see if you can use this feature to simplify this logic a bit.

Thanks for the feedback! But I'm not sure I'm following properly.

Quoted 7 lines

If what you are talking about is specifying the inner_map_idx of array_of_lru_hashes, I've changed it by using the __array() directives of the BTF-defined MAP.

Since inner_map_idx logic has been replaced with BTF-defined map definition, the only thing left at here fixup_map() is just resizing map size with bpf_map__resize.

Ok, as I said, a bit too lazy to try to figure out the entire logic of this sample. My point was to check if static initialization of ARRAY_OF_MAPS/HASH_OF_MAPS elements is doable. If not, it's fine as is as well.

Quoted 21 lines

Thanks for your time and effort for the review.
Daniel

Quoted 17 lines
Quoted 15 lines
samples/bpf/map_perf_test_user.c +7 -5
-               if (inner_lru_hash_idx == -1) {
-                       printf("inner_lru_hash_map must be defined before array_of_lru_hashs\n");
-                       exit(1);
+               /* Only change the max_entries for the enabled test(s) */
+               for (i = 0; i < NR_TESTS; i++) {
+                       if (!strcmp(test_map_names[i], name) &&
+                           (check_test_flags(i))) {
+                               bpf_map__resize(map, num_map_entries);
+                               continue;
+                       }
                }
-               map->def.inner_map_idx = inner_lru_hash_idx;
-               array_of_lru_hashs_idx = idx;
        }

[...]

#6 lore raw

Re: [PATCH bpf-next 1/4] samples: bpf: fix bpf programs with kprobe/sys_connect event

In reply to Daniel T. Lee
Quoted 93 lines · Daniel T. Lee
Quoted 65 lines · Yonghong Song
Quoted 40 lines · Daniel T. Lee
Quoted 29 lines · Yonghong Song
Quoted 13 lines · Daniel T. Lee

Currently, BPF programs with kprobe/sys_connect does not work properly.

Commit 34745aed515c ("samples/bpf: fix kprobe attachment issue on x64") This commit modifies the bpf_load behavior of kprobe events in the x64 architecture. If the current kprobe event target starts with "sys_*", add the prefix "__x64_" to the front of the event.

Appending "__x64_" prefix with kprobe/sys_* event was appropriate as a solution to most of the problems caused by the commit below.

commit d5a00528b58c ("syscalls/core, syscalls/x86: Rename struct pt_regs-based sys_*() to __x64_sys_*()")

Quoted 7 lines

However, there is a problem with the sys_connect kprobe event that does not work properly. For __sys_connect event, parameters can be fetched normally, but for __x64_sys_connect, parameters cannot be fetched.

Because of this problem, this commit fixes the sys_connect event by specifying the __sys_connect directly and this will bypass the "__x64_" appending rule of bpf_load.

In the kernel code, we have

SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
int, addrlen)

Quoted 9 lines
{
          return __sys_connect(fd, uservaddr, addrlen);
}

Depending on compiler, there is no guarantee that __sys_connect will not be inlined. I would prefer to still use the entry point __x64_sys_* e.g.,
SEC("kprobe/" SYSCALL(sys_write))

Quoted 6 lines

As you mentioned, there is clearly a possibility that problems may arise because the symbol does not exist according to the compiler.

However, in x64, when using Kprobe for __x64_sys_connect event, the tests are not working properly because the parameters cannot be fetched, and the test under selftests/bpf is using "kprobe/_sys_connect" directly.

This is the assembly code for __x64_sys_connect.

ffffffff818d3520 <__x64_sys_connect>:
ffffffff818d3520: e8 fb df 32 00 callq 0xffffffff81c01520
<__fentry__>
ffffffff818d3525: 48 8b 57 60 movq 96(%rdi), %rdx
ffffffff818d3529: 48 8b 77 68 movq 104(%rdi), %rsi
ffffffff818d352d: 48 8b 7f 70 movq 112(%rdi), %rdi
ffffffff818d3531: e8 1a ff ff ff callq 0xffffffff818d3450
<__sys_connect>
ffffffff818d3536: 48 98 cltq
ffffffff818d3538: c3 retq
ffffffff818d3539: 0f 1f 80 00 00 00 00 nopl (%rax)

Quoted 7 lines

In bpf program, the step is:

       struct pt_regs *real_regs = PT_REGS_PARM1(pt_regs);
       param1 = PT_REGS_PARM1(real_regs);
       param2 = PT_REGS_PARM2(real_regs);
       param3 = PT_REGS_PARM3(real_regs);

The same for s390.

I'm sorry that I seem to get it wrong,
But is it available to access 'struct pt_regs *' recursively?

It seems nested use of PT_REGS_PARM causes invalid memory access.

    $ sudo ./test_probe_write_user
    libbpf: load bpf program failed: Permission denied
    libbpf: -- BEGIN DUMP LOG ---
    libbpf:
    Unrecognized arg#0 type PTR
    ; struct pt_regs *real_regs = PT_REGS_PARM1(ctx);
    0: (79) r1 = *(u64 *)(r1 +112)
    ; void *sockaddr_arg = (void *)PT_REGS_PARM2(real_regs);
    1: (79) r6 = *(u64 *)(r1 +104)
    R1 invalid mem access 'inv'
    processed 2 insns (limit 1000000) max_states_per_insn 0
total_states 0 peak_states 0 mark_read 0
Quoted 7 lines

libbpf: -- END LOG --
libbpf: failed to load program 'kprobe/__x64_sys_connect'
libbpf: failed to load object './test_probe_write_user_kern.o'
ERROR: loading BPF object file failed

I'm not fully aware of the BPF verifier's internal structure. Is there any workaround to solve this problem?

You need to use bpf_probe_read_kernel() to get those arguments from real_args. Or better just use PT_REGS_PARM1_CORE(x) and others, which does that for you (+ CO-RE relocation).

Quoted 65 lines

Thanks for your time and effort for the review.
Daniel.

Quoted 61 lines

For other architectures, no above indirection is needed.

I guess you can abstract the above into trace_common.h?

Quoted 56 lines

I'm not sure how to deal with this problem. Any advice and suggestions will be greatly appreciated.

Thanks for your time and effort for the review.
Daniel

Quoted 49 lines
Quoted 49 lines
Fixes 34745aed515c ("samples/bpf: fix kprobe attachment issue on x64")
---
   samples/bpf/map_perf_test_kern.c         | 2 +-
   samples/bpf/test_map_in_map_kern.c       | 2 +-
   samples/bpf/test_probe_write_user_kern.c | 2 +-
   3 files changed, 3 insertions(+), 3 deletions(-)
samples/bpf/map_perf_test_kern.c +1 -1
index 12e91ae64d4d..cebe2098bb24 100644
@@ -154,7 +154,7 @@ int stress_percpu_hmap_alloc(struct pt_regs *ctx)
       return 0;
   }
 
-SEC("kprobe/sys_connect")
+SEC("kprobe/__sys_connect")
   int stress_lru_hmap_alloc(struct pt_regs *ctx)
   {
       char fmt[] = "Failed at stress_lru_hmap_alloc. ret:%dn";
samples/bpf/test_map_in_map_kern.c +1 -1
index 6cee61e8ce9b..b1562ba2f025 100644
@@ -102,7 +102,7 @@ static __always_inline int do_inline_hash_lookup(void *inner_map, u32 port)
       return result ? *result : -ENOENT;
   }
 
-SEC("kprobe/sys_connect")
+SEC("kprobe/__sys_connect")
   int trace_sys_connect(struct pt_regs *ctx)
   {
       struct sockaddr_in6 *in6;
samples/bpf/test_probe_write_user_kern.c +1 -1
index 6579639a83b2..9b3c3918c37d 100644
@@ -26,7 +26,7 @@ struct {
    * This example sits on a syscall, and the syscall ABI is relatively stable
    * of course, across platforms, and over time, the ABI may change.
    */
-SEC("kprobe/sys_connect")
+SEC("kprobe/__sys_connect")
   int bpf_prog1(struct pt_regs *ctx)
   {
       struct sockaddr_in new_addr, orig_addr = {};
#7 lore raw

Re: [PATCH bpf-next 1/4] samples: bpf: fix bpf programs with kprobe/sys_connect event

Quoted 100 lines · Andrii Nakryiko
Quoted 98 lines · Daniel T. Lee
Quoted 65 lines · Yonghong Song
Quoted 40 lines · Daniel T. Lee
Quoted 29 lines · Yonghong Song
Quoted 13 lines · Daniel T. Lee

Currently, BPF programs with kprobe/sys_connect does not work properly.

Commit 34745aed515c ("samples/bpf: fix kprobe attachment issue on x64") This commit modifies the bpf_load behavior of kprobe events in the x64 architecture. If the current kprobe event target starts with "sys_*", add the prefix "__x64_" to the front of the event.

Appending "__x64_" prefix with kprobe/sys_* event was appropriate as a solution to most of the problems caused by the commit below.

commit d5a00528b58c ("syscalls/core, syscalls/x86: Rename struct pt_regs-based sys_*() to __x64_sys_*()")

Quoted 7 lines

However, there is a problem with the sys_connect kprobe event that does not work properly. For __sys_connect event, parameters can be fetched normally, but for __x64_sys_connect, parameters cannot be fetched.

Because of this problem, this commit fixes the sys_connect event by specifying the __sys_connect directly and this will bypass the "__x64_" appending rule of bpf_load.

In the kernel code, we have

SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
int, addrlen)

Quoted 9 lines
{
          return __sys_connect(fd, uservaddr, addrlen);
}

Depending on compiler, there is no guarantee that __sys_connect will not be inlined. I would prefer to still use the entry point __x64_sys_* e.g.,
SEC("kprobe/" SYSCALL(sys_write))

Quoted 6 lines

As you mentioned, there is clearly a possibility that problems may arise because the symbol does not exist according to the compiler.

However, in x64, when using Kprobe for __x64_sys_connect event, the tests are not working properly because the parameters cannot be fetched, and the test under selftests/bpf is using "kprobe/_sys_connect" directly.

This is the assembly code for __x64_sys_connect.

ffffffff818d3520 <__x64_sys_connect>:
ffffffff818d3520: e8 fb df 32 00 callq 0xffffffff81c01520
<__fentry__>
ffffffff818d3525: 48 8b 57 60 movq 96(%rdi), %rdx
ffffffff818d3529: 48 8b 77 68 movq 104(%rdi), %rsi
ffffffff818d352d: 48 8b 7f 70 movq 112(%rdi), %rdi
ffffffff818d3531: e8 1a ff ff ff callq 0xffffffff818d3450
<__sys_connect>
ffffffff818d3536: 48 98 cltq
ffffffff818d3538: c3 retq
ffffffff818d3539: 0f 1f 80 00 00 00 00 nopl (%rax)

Quoted 7 lines

In bpf program, the step is:

       struct pt_regs *real_regs = PT_REGS_PARM1(pt_regs);
       param1 = PT_REGS_PARM1(real_regs);
       param2 = PT_REGS_PARM2(real_regs);
       param3 = PT_REGS_PARM3(real_regs);

The same for s390.

I'm sorry that I seem to get it wrong,
But is it available to access 'struct pt_regs *' recursively?

It seems nested use of PT_REGS_PARM causes invalid memory access.

    $ sudo ./test_probe_write_user
    libbpf: load bpf program failed: Permission denied
    libbpf: -- BEGIN DUMP LOG ---
    libbpf:
    Unrecognized arg#0 type PTR
    ; struct pt_regs *real_regs = PT_REGS_PARM1(ctx);
    0: (79) r1 = *(u64 *)(r1 +112)
    ; void *sockaddr_arg = (void *)PT_REGS_PARM2(real_regs);
    1: (79) r6 = *(u64 *)(r1 +104)
    R1 invalid mem access 'inv'
    processed 2 insns (limit 1000000) max_states_per_insn 0
total_states 0 peak_states 0 mark_read 0

libbpf: -- END LOG --
libbpf: failed to load program 'kprobe/__x64_sys_connect'
libbpf: failed to load object './test_probe_write_user_kern.o'
ERROR: loading BPF object file failed

Quoted 8 lines
Quoted 2 lines

I'm not fully aware of the BPF verifier's internal structure. Is there any workaround to solve this problem?

You need to use bpf_probe_read_kernel() to get those arguments from real_args. Or better just use PT_REGS_PARM1_CORE(x) and others, which does that for you (+ CO-RE relocation).

Thanks for the tip!

I've just tried the old hack '_(P)':
(which is similar implementation with BPF_CORE_READ())

    #define _(P) ({typeof(P) val = 0; bpf_probe_read(&val,
sizeof(val), &P); val;})
    [...]
    struct pt_regs *regs = (struct pt_regs *)PT_REGS_PARM1(ctx);
    void *sockaddr_arg = (void *)_(PT_REGS_PARM2(regs));
    int sockaddr_len = (int)_(PT_REGS_PARM3(regs));

and it works properly.

Just wondering, why is the pointer chasing of the original ctx considered as an unsafe pointer here?

    ; struct pt_regs *real_regs = (struct pt_regs *)PT_REGS_PARM1(ctx);
    0: (79) r1 = *(u64 *)(r1 +112)
    [...]
    ; void *sockaddr_arg = (void *)PT_REGS_PARM2(real_regs);
    4: (79) r6 = *(u64 *)(r1 +104)

Is it considered as an unsafe pointer since it is unknown what exists in the pointer (r1 + 104), but the instruction is trying to access it?

I am a little concerned about using PT_REGS_PARM1_CORE because it is not a CORE-related patch, but if using CORE is the direction BPF wants to take, I will use PT_REGS_PARM1_CORE() instead of _(P) hack using bpf_probe_read().

In addition, PT_REGS_PARM1_CORE() allows me to write code neatly without having to define additional macro _(P).

Thank you for your time and effort for the review. Daniel

Quoted 65 lines
Quoted 65 lines

Thanks for your time and effort for the review.
Daniel.

Quoted 61 lines

For other architectures, no above indirection is needed.

I guess you can abstract the above into trace_common.h?

Quoted 56 lines

I'm not sure how to deal with this problem. Any advice and suggestions will be greatly appreciated.

Thanks for your time and effort for the review.
Daniel

Quoted 49 lines
Quoted 49 lines
Fixes 34745aed515c ("samples/bpf: fix kprobe attachment issue on x64")
---
   samples/bpf/map_perf_test_kern.c         | 2 +-
   samples/bpf/test_map_in_map_kern.c       | 2 +-
   samples/bpf/test_probe_write_user_kern.c | 2 +-
   3 files changed, 3 insertions(+), 3 deletions(-)
samples/bpf/map_perf_test_kern.c +1 -1
index 12e91ae64d4d..cebe2098bb24 100644
@@ -154,7 +154,7 @@ int stress_percpu_hmap_alloc(struct pt_regs *ctx)
       return 0;
   }
 
-SEC("kprobe/sys_connect")
+SEC("kprobe/__sys_connect")
   int stress_lru_hmap_alloc(struct pt_regs *ctx)
   {
       char fmt[] = "Failed at stress_lru_hmap_alloc. ret:%dn";
samples/bpf/test_map_in_map_kern.c +1 -1
index 6cee61e8ce9b..b1562ba2f025 100644
@@ -102,7 +102,7 @@ static __always_inline int do_inline_hash_lookup(void *inner_map, u32 port)
       return result ? *result : -ENOENT;
   }
 
-SEC("kprobe/sys_connect")
+SEC("kprobe/__sys_connect")
   int trace_sys_connect(struct pt_regs *ctx)
   {
       struct sockaddr_in6 *in6;
samples/bpf/test_probe_write_user_kern.c +1 -1
index 6579639a83b2..9b3c3918c37d 100644
@@ -26,7 +26,7 @@ struct {
    * This example sits on a syscall, and the syscall ABI is relatively stable
    * of course, across platforms, and over time, the ABI may change.
    */
-SEC("kprobe/sys_connect")
+SEC("kprobe/__sys_connect")
   int bpf_prog1(struct pt_regs *ctx)
   {
       struct sockaddr_in new_addr, orig_addr = {};
#8 lore raw

Re: [PATCH bpf-next 1/4] samples: bpf: fix bpf programs with kprobe/sys_connect event

Quoted 129 lines · Daniel T. Lee
Quoted 100 lines · Andrii Nakryiko
Quoted 98 lines · Daniel T. Lee
Quoted 65 lines · Yonghong Song
Quoted 40 lines · Daniel T. Lee
Quoted 29 lines · Yonghong Song
Quoted 13 lines · Daniel T. Lee

Currently, BPF programs with kprobe/sys_connect does not work properly.

Commit 34745aed515c ("samples/bpf: fix kprobe attachment issue on x64") This commit modifies the bpf_load behavior of kprobe events in the x64 architecture. If the current kprobe event target starts with "sys_*", add the prefix "__x64_" to the front of the event.

Appending "__x64_" prefix with kprobe/sys_* event was appropriate as a solution to most of the problems caused by the commit below.

commit d5a00528b58c ("syscalls/core, syscalls/x86: Rename struct pt_regs-based sys_*() to __x64_sys_*()")

Quoted 7 lines

However, there is a problem with the sys_connect kprobe event that does not work properly. For __sys_connect event, parameters can be fetched normally, but for __x64_sys_connect, parameters cannot be fetched.

Because of this problem, this commit fixes the sys_connect event by specifying the __sys_connect directly and this will bypass the "__x64_" appending rule of bpf_load.

In the kernel code, we have

SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
int, addrlen)

Quoted 9 lines
{
          return __sys_connect(fd, uservaddr, addrlen);
}

Depending on compiler, there is no guarantee that __sys_connect will not be inlined. I would prefer to still use the entry point __x64_sys_* e.g.,
SEC("kprobe/" SYSCALL(sys_write))

Quoted 6 lines

As you mentioned, there is clearly a possibility that problems may arise because the symbol does not exist according to the compiler.

However, in x64, when using Kprobe for __x64_sys_connect event, the tests are not working properly because the parameters cannot be fetched, and the test under selftests/bpf is using "kprobe/_sys_connect" directly.

This is the assembly code for __x64_sys_connect.

ffffffff818d3520 <__x64_sys_connect>:
ffffffff818d3520: e8 fb df 32 00 callq 0xffffffff81c01520
<__fentry__>
ffffffff818d3525: 48 8b 57 60 movq 96(%rdi), %rdx
ffffffff818d3529: 48 8b 77 68 movq 104(%rdi), %rsi
ffffffff818d352d: 48 8b 7f 70 movq 112(%rdi), %rdi
ffffffff818d3531: e8 1a ff ff ff callq 0xffffffff818d3450
<__sys_connect>
ffffffff818d3536: 48 98 cltq
ffffffff818d3538: c3 retq
ffffffff818d3539: 0f 1f 80 00 00 00 00 nopl (%rax)

Quoted 7 lines

In bpf program, the step is:

       struct pt_regs *real_regs = PT_REGS_PARM1(pt_regs);
       param1 = PT_REGS_PARM1(real_regs);
       param2 = PT_REGS_PARM2(real_regs);
       param3 = PT_REGS_PARM3(real_regs);

The same for s390.

I'm sorry that I seem to get it wrong,
But is it available to access 'struct pt_regs *' recursively?

It seems nested use of PT_REGS_PARM causes invalid memory access.

    $ sudo ./test_probe_write_user
    libbpf: load bpf program failed: Permission denied
    libbpf: -- BEGIN DUMP LOG ---
    libbpf:
    Unrecognized arg#0 type PTR
    ; struct pt_regs *real_regs = PT_REGS_PARM1(ctx);
    0: (79) r1 = *(u64 *)(r1 +112)
    ; void *sockaddr_arg = (void *)PT_REGS_PARM2(real_regs);
    1: (79) r6 = *(u64 *)(r1 +104)
    R1 invalid mem access 'inv'
    processed 2 insns (limit 1000000) max_states_per_insn 0
total_states 0 peak_states 0 mark_read 0

libbpf: -- END LOG --
libbpf: failed to load program 'kprobe/__x64_sys_connect'
libbpf: failed to load object './test_probe_write_user_kern.o'
ERROR: loading BPF object file failed

Quoted 8 lines
Quoted 2 lines

I'm not fully aware of the BPF verifier's internal structure. Is there any workaround to solve this problem?

You need to use bpf_probe_read_kernel() to get those arguments from real_args. Or better just use PT_REGS_PARM1_CORE(x) and others, which does that for you (+ CO-RE relocation).

Thanks for the tip!

I've just tried the old hack '_(P)':
(which is similar implementation with BPF_CORE_READ())

    #define _(P) ({typeof(P) val = 0; bpf_probe_read(&val,
sizeof(val), &P); val;})
    [...]
    struct pt_regs *regs = (struct pt_regs *)PT_REGS_PARM1(ctx);
    void *sockaddr_arg = (void *)_(PT_REGS_PARM2(regs));
    int sockaddr_len = (int)_(PT_REGS_PARM3(regs));

and it works properly.

Just wondering, why is the pointer chasing of the original ctx considered as an unsafe pointer here?

Quoted 9 lines
    ; struct pt_regs *real_regs = (struct pt_regs *)PT_REGS_PARM1(ctx);
    0: (79) r1 = *(u64 *)(r1 +112)
    [...]
    ; void *sockaddr_arg = (void *)PT_REGS_PARM2(real_regs);
    4: (79) r6 = *(u64 *)(r1 +104)

Is it considered as an unsafe pointer since it is unknown what exists in the pointer (r1 + 104), but the instruction is trying to access it?

Yes.
Because after the initial pointer read, the verifier assumes that you are reading a random piece of memory.

Quoted 5 lines

I am a little concerned about using PT_REGS_PARM1_CORE because it is not a CORE-related patch, but if using CORE is the direction BPF wants to take, I will use PT_REGS_PARM1_CORE() instead of _(P) hack using bpf_probe_read().

bpf_probe_read() works as well. But yeah, BPF CO-RE is the way modern tracing applications are leaning, look at selftests and see how many are using CO-RE already. It's pretty much the only way to write portable tracing BPF applications, short of taking Clang/LLVM **runtime** dependency, the way BCC makes you do.

Quoted 27 lines

In addition, PT_REGS_PARM1_CORE() allows me to write code neatly without having to define additional macro _(P).

Thank you for your time and effort for the review. Daniel

Quoted 20 lines
Quoted 20 lines

Thanks for your time and effort for the review.
Daniel.

Quoted 16 lines

For other architectures, no above indirection is needed.

I guess you can abstract the above into trace_common.h?

Quoted 11 lines

I'm not sure how to deal with this problem. Any advice and suggestions will be greatly appreciated.

Thanks for your time and effort for the review.
Daniel

Quoted 4 lines
Quoted 4 lines
Fixes 34745aed515c ("samples/bpf: fix kprobe attachment issue on x64")

---

[...]

#9 lore raw

Re: [PATCH bpf-next 1/4] samples: bpf: fix bpf programs with kprobe/sys_connect event

Quoted 137 lines · Andrii Nakryiko
Quoted 135 lines · Daniel T. Lee
Quoted 100 lines · Andrii Nakryiko
Quoted 98 lines · Daniel T. Lee
Quoted 65 lines · Yonghong Song
Quoted 40 lines · Daniel T. Lee
Quoted 29 lines · Yonghong Song
Quoted 13 lines · Daniel T. Lee

Currently, BPF programs with kprobe/sys_connect does not work properly.

Commit 34745aed515c ("samples/bpf: fix kprobe attachment issue on x64") This commit modifies the bpf_load behavior of kprobe events in the x64 architecture. If the current kprobe event target starts with "sys_*", add the prefix "__x64_" to the front of the event.

Appending "__x64_" prefix with kprobe/sys_* event was appropriate as a solution to most of the problems caused by the commit below.

commit d5a00528b58c ("syscalls/core, syscalls/x86: Rename struct pt_regs-based sys_*() to __x64_sys_*()")

Quoted 7 lines

However, there is a problem with the sys_connect kprobe event that does not work properly. For __sys_connect event, parameters can be fetched normally, but for __x64_sys_connect, parameters cannot be fetched.

Because of this problem, this commit fixes the sys_connect event by specifying the __sys_connect directly and this will bypass the "__x64_" appending rule of bpf_load.

In the kernel code, we have

SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
int, addrlen)

Quoted 9 lines
{
          return __sys_connect(fd, uservaddr, addrlen);
}

Depending on compiler, there is no guarantee that __sys_connect will not be inlined. I would prefer to still use the entry point __x64_sys_* e.g.,
SEC("kprobe/" SYSCALL(sys_write))

Quoted 6 lines

As you mentioned, there is clearly a possibility that problems may arise because the symbol does not exist according to the compiler.

However, in x64, when using Kprobe for __x64_sys_connect event, the tests are not working properly because the parameters cannot be fetched, and the test under selftests/bpf is using "kprobe/_sys_connect" directly.

This is the assembly code for __x64_sys_connect.

ffffffff818d3520 <__x64_sys_connect>:
ffffffff818d3520: e8 fb df 32 00 callq 0xffffffff81c01520
<__fentry__>
ffffffff818d3525: 48 8b 57 60 movq 96(%rdi), %rdx
ffffffff818d3529: 48 8b 77 68 movq 104(%rdi), %rsi
ffffffff818d352d: 48 8b 7f 70 movq 112(%rdi), %rdi
ffffffff818d3531: e8 1a ff ff ff callq 0xffffffff818d3450
<__sys_connect>
ffffffff818d3536: 48 98 cltq
ffffffff818d3538: c3 retq
ffffffff818d3539: 0f 1f 80 00 00 00 00 nopl (%rax)

Quoted 7 lines

In bpf program, the step is:

       struct pt_regs *real_regs = PT_REGS_PARM1(pt_regs);
       param1 = PT_REGS_PARM1(real_regs);
       param2 = PT_REGS_PARM2(real_regs);
       param3 = PT_REGS_PARM3(real_regs);

The same for s390.

I'm sorry that I seem to get it wrong,
But is it available to access 'struct pt_regs *' recursively?

It seems nested use of PT_REGS_PARM causes invalid memory access.

    $ sudo ./test_probe_write_user
    libbpf: load bpf program failed: Permission denied
    libbpf: -- BEGIN DUMP LOG ---
    libbpf:
    Unrecognized arg#0 type PTR
    ; struct pt_regs *real_regs = PT_REGS_PARM1(ctx);
    0: (79) r1 = *(u64 *)(r1 +112)
    ; void *sockaddr_arg = (void *)PT_REGS_PARM2(real_regs);
    1: (79) r6 = *(u64 *)(r1 +104)
    R1 invalid mem access 'inv'
    processed 2 insns (limit 1000000) max_states_per_insn 0
total_states 0 peak_states 0 mark_read 0

libbpf: -- END LOG --
libbpf: failed to load program 'kprobe/__x64_sys_connect'
libbpf: failed to load object './test_probe_write_user_kern.o'
ERROR: loading BPF object file failed

Quoted 8 lines
Quoted 2 lines

I'm not fully aware of the BPF verifier's internal structure. Is there any workaround to solve this problem?

You need to use bpf_probe_read_kernel() to get those arguments from real_args. Or better just use PT_REGS_PARM1_CORE(x) and others, which does that for you (+ CO-RE relocation).

Thanks for the tip!

I've just tried the old hack '_(P)':
(which is similar implementation with BPF_CORE_READ())

    #define _(P) ({typeof(P) val = 0; bpf_probe_read(&val,
sizeof(val), &P); val;})
    [...]
    struct pt_regs *regs = (struct pt_regs *)PT_REGS_PARM1(ctx);
    void *sockaddr_arg = (void *)_(PT_REGS_PARM2(regs));
    int sockaddr_len = (int)_(PT_REGS_PARM3(regs));

and it works properly.

Just wondering, why is the pointer chasing of the original ctx considered as an unsafe pointer here?

    ; struct pt_regs *real_regs = (struct pt_regs *)PT_REGS_PARM1(ctx);
    0: (79) r1 = *(u64 *)(r1 +112)
    [...]
    ; void *sockaddr_arg = (void *)PT_REGS_PARM2(real_regs);
    4: (79) r6 = *(u64 *)(r1 +104)
Quoted 8 lines
Quoted 3 lines

Is it considered as an unsafe pointer since it is unknown what exists in the pointer (r1 + 104), but the instruction is trying to access it?

Yes.
Because after the initial pointer read, the verifier assumes that you are reading a random piece of memory.

Thanks for the confirmation. It's very helpful to me.

Quoted 6 lines
Quoted 5 lines

I am a little concerned about using PT_REGS_PARM1_CORE because it is not a CORE-related patch, but if using CORE is the direction BPF wants to take, I will use PT_REGS_PARM1_CORE() instead of _(P) hack using bpf_probe_read().

Quoted 6 lines

bpf_probe_read() works as well. But yeah, BPF CO-RE is the way modern tracing applications are leaning, look at selftests and see how many are using CO-RE already. It's pretty much the only way to write portable tracing BPF applications, short of taking Clang/LLVM **runtime** dependency, the way BCC makes you do.

I see. I'll stick with PT_REGS_PARM1_CORE approach.

I will send the next version of the patch soon.

Thank you for your time and effort for the review. Daniel

Quoted 29 lines
Quoted 27 lines

In addition, PT_REGS_PARM1_CORE() allows me to write code neatly without having to define additional macro _(P).

Thank you for your time and effort for the review. Daniel

Quoted 20 lines
Quoted 20 lines

Thanks for your time and effort for the review.
Daniel.

Quoted 16 lines

For other architectures, no above indirection is needed.

I guess you can abstract the above into trace_common.h?

Quoted 11 lines

I'm not sure how to deal with this problem. Any advice and suggestions will be greatly appreciated.

Thanks for your time and effort for the review.
Daniel

Quoted 4 lines
Quoted 4 lines
Fixes 34745aed515c ("samples/bpf: fix kprobe attachment issue on x64")

---

[...]