* [PATCH bpf-next v3 0/2] selftests/bpf: Replaces CHECK macros and adds malloc failure checks to bpf_iter [not found] <cover.1698461732.git.yuran.pereira@hotmail.com> @ 2023-10-28 5:24 ` Yuran Pereira 2023-10-28 5:24 ` [PATCH bpf-next v3 1/2] selftests/bpf: Convert CHECK macros to ASSERT_* macros in bpf_iter Yuran Pereira 2023-10-28 5:24 ` [PATCH bpf-next v3 2/2] selftests/bpf: Add malloc failure checks " Yuran Pereira 2 siblings, 0 replies; 8+ messages in thread From: Yuran Pereira @ 2023-10-28 5:24 UTC (permalink / raw) To: bpf, yonghong.song Cc: sinquersw, ast, brauner, daniel, haoluo, iii, john.fastabend, jolsa, kpsingh, kuifeng, linux-kernel, linux-kselftest, mykolal, sdf, shuah, song, linux-kernel-mentees, Yuran Pereira This patch series contains the following updates: - Replaces the usage of all the CHECK macros in bpf_iter to the appropriate ASSERT_* macro calls - Adds appropriate malloc failure checks to bpf_iter Changes in v3: - Modified and removed unnecessary `if` statements as suggested by Kui-Feng [1] Changes in v2: - Removed unused "duration" variable which caused compilation error as reported by Yonghong Song [2] - Improved malloc failure handling as suggested by Kui-Feng Lee [3] - Ensured propper patch formatting (i.e. sending it as a set instead of two distinct patches) [1] https://lore.kernel.org/lkml/3739e65c-b86d-4c11-9cf5-7b76080400c2@gmail.com/ [2] https://lore.kernel.org/lkml/3531360b-c933-4c5f-a84c-17edf0592519@linux.dev/ [3] https://lore.kernel.org/lkml/7d703c4c-1a24-4806-a483-c02efb666059@gmail.com Previous versions: v2 - https://lore.kernel.org/lkml/DB3PR10MB6835598B901975BEAEBA8601E8DDA@DB3PR10MB6835.EURPRD10.PROD.OUTLOOK.COM - https://lore.kernel.org/lkml/DB3PR10MB68352B683F26EE9C342B71A9E8DDA@DB3PR10MB6835.EURPRD10.PROD.OUTLOOK.COM v1 - https://lore.kernel.org/lkml/DB3PR10MB683589A5F705C6CA5BE0D325E8DFA@DB3PR10MB6835.EURPRD10.PROD.OUTLOOK.COM - https://lore.kernel.org/lkml/DB3PR10MB68356D7CDF6005480BE5876CE8DEA@DB3PR10MB6835.EURPRD10.PROD.OUTLOOK.COM Yuran Pereira (2): selftests/bpf: Convert CHECK macros to ASSERT_* macros in bpf_iter selftests/bpf: Add malloc failure checks in bpf_iter .../selftests/bpf/prog_tests/bpf_iter.c | 85 +++++++++---------- 1 file changed, 40 insertions(+), 45 deletions(-) -- 2.25.1 ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH bpf-next v3 1/2] selftests/bpf: Convert CHECK macros to ASSERT_* macros in bpf_iter [not found] <cover.1698461732.git.yuran.pereira@hotmail.com> 2023-10-28 5:24 ` [PATCH bpf-next v3 0/2] selftests/bpf: Replaces CHECK macros and adds malloc failure checks to bpf_iter Yuran Pereira @ 2023-10-28 5:24 ` Yuran Pereira 2023-10-29 17:06 ` Yonghong Song ` (2 more replies) 2023-10-28 5:24 ` [PATCH bpf-next v3 2/2] selftests/bpf: Add malloc failure checks " Yuran Pereira 2 siblings, 3 replies; 8+ messages in thread From: Yuran Pereira @ 2023-10-28 5:24 UTC (permalink / raw) To: bpf, yonghong.song Cc: sinquersw, ast, brauner, daniel, haoluo, iii, john.fastabend, jolsa, kpsingh, kuifeng, linux-kernel, linux-kselftest, mykolal, sdf, shuah, song, linux-kernel-mentees, Yuran Pereira As it was pointed out by Yonghong Song [1], in the bpf selftests the use of the ASSERT_* series of macros is preferred over the CHECK macro. This patch replaces all CHECK calls in bpf_iter with the appropriate ASSERT_* macros. [1] https://lore.kernel.org/lkml/0a142924-633c-44e6-9a92-2dc019656bf2@linux.dev Suggested-by: Yonghong Song <yonghong.song@linux.dev> Signed-off-by: Yuran Pereira <yuran.pereira@hotmail.com> --- .../selftests/bpf/prog_tests/bpf_iter.c | 79 ++++++++----------- 1 file changed, 35 insertions(+), 44 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c index 1f02168103dd..123a3502b8f0 100644 --- a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c +++ b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c @@ -34,8 +34,6 @@ #include "bpf_iter_ksym.skel.h" #include "bpf_iter_sockmap.skel.h" -static int duration; - static void test_btf_id_or_null(void) { struct bpf_iter_test_kern3 *skel; @@ -64,7 +62,7 @@ static void do_dummy_read_opts(struct bpf_program *prog, struct bpf_iter_attach_ /* not check contents, but ensure read() ends without error */ while ((len = read(iter_fd, buf, sizeof(buf))) > 0) ; - CHECK(len < 0, "read", "read failed: %s\n", strerror(errno)); + ASSERT_GE(len, 0, "read"); close(iter_fd); @@ -413,7 +411,7 @@ static int do_btf_read(struct bpf_iter_task_btf *skel) goto free_link; } - if (CHECK(err < 0, "read", "read failed: %s\n", strerror(errno))) + if (!ASSERT_GE(err, 0, "read")) goto free_link; ASSERT_HAS_SUBSTR(taskbuf, "(struct task_struct)", @@ -526,11 +524,11 @@ static int do_read_with_fd(int iter_fd, const char *expected, start = 0; while ((len = read(iter_fd, buf + start, read_buf_len)) > 0) { start += len; - if (CHECK(start >= 16, "read", "read len %d\n", len)) + if (!ASSERT_LT(start, 16, "read")) return -1; read_buf_len = read_one_char ? 1 : 16 - start; } - if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno))) + if (!ASSERT_GE(len, 0, "read")) return -1; if (!ASSERT_STREQ(buf, expected, "read")) @@ -571,8 +569,7 @@ static int do_read(const char *path, const char *expected) int err, iter_fd; iter_fd = open(path, O_RDONLY); - if (CHECK(iter_fd < 0, "open", "open %s failed: %s\n", - path, strerror(errno))) + if (!ASSERT_GE(iter_fd, 0, "open")) return -1; err = do_read_with_fd(iter_fd, expected, false); @@ -600,7 +597,7 @@ static void test_file_iter(void) unlink(path); err = bpf_link__pin(link, path); - if (CHECK(err, "pin_iter", "pin_iter to %s failed: %d\n", path, err)) + if (!ASSERT_OK(err, "pin_iter")) goto free_link; err = do_read(path, "abcd"); @@ -651,12 +648,10 @@ static void test_overflow(bool test_e2big_overflow, bool ret1) * overflow and needs restart. */ map1_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, 4, 8, 1, NULL); - if (CHECK(map1_fd < 0, "bpf_map_create", - "map_creation failed: %s\n", strerror(errno))) + if (!ASSERT_GE(map1_fd, 0, "bpf_map_create")) goto out; map2_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, 4, 8, 1, NULL); - if (CHECK(map2_fd < 0, "bpf_map_create", - "map_creation failed: %s\n", strerror(errno))) + if (!ASSERT_GE(map2_fd, 0, "bpf_map_create")) goto free_map1; /* bpf_seq_printf kernel buffer is 8 pages, so one map @@ -685,14 +680,12 @@ static void test_overflow(bool test_e2big_overflow, bool ret1) /* setup filtering map_id in bpf program */ map_info_len = sizeof(map_info); err = bpf_map_get_info_by_fd(map1_fd, &map_info, &map_info_len); - if (CHECK(err, "get_map_info", "get map info failed: %s\n", - strerror(errno))) + if (!ASSERT_OK(err, "get_map_info")) goto free_map2; skel->bss->map1_id = map_info.id; err = bpf_map_get_info_by_fd(map2_fd, &map_info, &map_info_len); - if (CHECK(err, "get_map_info", "get map info failed: %s\n", - strerror(errno))) + if (!ASSERT_OK(err, "get_map_info")) goto free_map2; skel->bss->map2_id = map_info.id; @@ -714,16 +707,14 @@ static void test_overflow(bool test_e2big_overflow, bool ret1) while ((len = read(iter_fd, buf, expected_read_len)) > 0) total_read_len += len; - CHECK(len != -1 || errno != E2BIG, "read", - "expected ret -1, errno E2BIG, but get ret %d, error %s\n", - len, strerror(errno)); + ASSERT_EQ(len, -1, "read"); + ASSERT_EQ(errno, E2BIG, "read"); goto free_buf; } else if (!ret1) { while ((len = read(iter_fd, buf, expected_read_len)) > 0) total_read_len += len; - if (CHECK(len < 0, "read", "read failed: %s\n", - strerror(errno))) + if (!ASSERT_GE(len, 0, "read")) goto free_buf; } else { do { @@ -732,8 +723,7 @@ static void test_overflow(bool test_e2big_overflow, bool ret1) total_read_len += len; } while (len > 0 || len == -EAGAIN); - if (CHECK(len < 0, "read", "read failed: %s\n", - strerror(errno))) + if (!ASSERT_GE(len, 0, "read")) goto free_buf; } @@ -836,7 +826,7 @@ static void test_bpf_hash_map(void) /* do some tests */ while ((len = read(iter_fd, buf, sizeof(buf))) > 0) ; - if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno))) + if (!ASSERT_GE(len, 0, "read")) goto close_iter; /* test results */ @@ -917,7 +907,7 @@ static void test_bpf_percpu_hash_map(void) /* do some tests */ while ((len = read(iter_fd, buf, sizeof(buf))) > 0) ; - if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno))) + if (!ASSERT_GE(len, 0, "read")) goto close_iter; /* test results */ @@ -983,17 +973,14 @@ static void test_bpf_array_map(void) start = 0; while ((len = read(iter_fd, buf + start, sizeof(buf) - start)) > 0) start += len; - if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno))) + if (!ASSERT_GE(len, 0, "read")) goto close_iter; /* test results */ res_first_key = *(__u32 *)buf; res_first_val = *(__u64 *)(buf + sizeof(__u32)); - if (CHECK(res_first_key != 0 || res_first_val != first_val, - "bpf_seq_write", - "seq_write failure: first key %u vs expected 0, " - " first value %llu vs expected %llu\n", - res_first_key, res_first_val, first_val)) + if (!ASSERT_EQ(res_first_key, 0, "bpf_seq_write") || + !ASSERT_EQ(res_first_val, first_val, "bpf_seq_write")) goto close_iter; if (!ASSERT_EQ(skel->bss->key_sum, expected_key, "key_sum")) @@ -1092,7 +1079,7 @@ static void test_bpf_percpu_array_map(void) /* do some tests */ while ((len = read(iter_fd, buf, sizeof(buf))) > 0) ; - if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno))) + if (!ASSERT_GE(len, 0, "read")) goto close_iter; /* test results */ @@ -1131,6 +1118,7 @@ static void test_bpf_sk_storage_delete(void) sock_fd = socket(AF_INET6, SOCK_STREAM, 0); if (!ASSERT_GE(sock_fd, 0, "socket")) goto out; + err = bpf_map_update_elem(map_fd, &sock_fd, &val, BPF_NOEXIST); if (!ASSERT_OK(err, "map_update")) goto out; @@ -1151,14 +1139,19 @@ static void test_bpf_sk_storage_delete(void) /* do some tests */ while ((len = read(iter_fd, buf, sizeof(buf))) > 0) ; - if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno))) + if (!ASSERT_GE(len, 0, "read")) goto close_iter; /* test results */ err = bpf_map_lookup_elem(map_fd, &sock_fd, &val); - if (CHECK(!err || errno != ENOENT, "bpf_map_lookup_elem", - "map value wasn't deleted (err=%d, errno=%d)\n", err, errno)) - goto close_iter; + + /* Note: The following assertions serve to ensure + * the value was deleted. It does so by asserting + * that bpf_map_lookup_elem has failed. This might + * seem counterintuitive at first. + */ + ASSERT_ERR(err, "bpf_map_lookup_elem"); + ASSERT_EQ(errno, ENOENT, "bpf_map_lookup_elem"); close_iter: close(iter_fd); @@ -1203,17 +1196,15 @@ static void test_bpf_sk_storage_get(void) do_dummy_read(skel->progs.fill_socket_owner); err = bpf_map_lookup_elem(map_fd, &sock_fd, &val); - if (CHECK(err || val != getpid(), "bpf_map_lookup_elem", - "map value wasn't set correctly (expected %d, got %d, err=%d)\n", - getpid(), val, err)) + if (!ASSERT_OK(err, "bpf_map_lookup_elem") || + !ASSERT_EQ(val, getpid(), "bpf_map_lookup_elem")) goto close_socket; do_dummy_read(skel->progs.negate_socket_local_storage); err = bpf_map_lookup_elem(map_fd, &sock_fd, &val); - CHECK(err || val != -getpid(), "bpf_map_lookup_elem", - "map value wasn't set correctly (expected %d, got %d, err=%d)\n", - -getpid(), val, err); + ASSERT_OK(err, "bpf_map_lookup_elem"); + ASSERT_EQ(val, -getpid(), "bpf_map_lookup_elem"); close_socket: close(sock_fd); @@ -1290,7 +1281,7 @@ static void test_bpf_sk_storage_map(void) /* do some tests */ while ((len = read(iter_fd, buf, sizeof(buf))) > 0) ; - if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno))) + if (!ASSERT_GE(len, 0, "read")) goto close_iter; /* test results */ -- 2.25.1 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH bpf-next v3 1/2] selftests/bpf: Convert CHECK macros to ASSERT_* macros in bpf_iter 2023-10-28 5:24 ` [PATCH bpf-next v3 1/2] selftests/bpf: Convert CHECK macros to ASSERT_* macros in bpf_iter Yuran Pereira @ 2023-10-29 17:06 ` Yonghong Song 2023-10-30 19:39 ` Kui-Feng Lee 2023-11-02 6:00 ` patchwork-bot+netdevbpf 2 siblings, 0 replies; 8+ messages in thread From: Yonghong Song @ 2023-10-29 17:06 UTC (permalink / raw) To: Yuran Pereira, bpf Cc: sinquersw, ast, brauner, daniel, haoluo, iii, john.fastabend, jolsa, kpsingh, kuifeng, linux-kernel, linux-kselftest, mykolal, sdf, shuah, song, linux-kernel-mentees On 10/27/23 10:24 PM, Yuran Pereira wrote: > As it was pointed out by Yonghong Song [1], in the bpf selftests the use > of the ASSERT_* series of macros is preferred over the CHECK macro. > This patch replaces all CHECK calls in bpf_iter with the appropriate > ASSERT_* macros. > > [1] https://lore.kernel.org/lkml/0a142924-633c-44e6-9a92-2dc019656bf2@linux.dev > > Suggested-by: Yonghong Song <yonghong.song@linux.dev> > Signed-off-by: Yuran Pereira <yuran.pereira@hotmail.com> Acked-by: Yonghong Song <yonghong.song@linux.dev> ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH bpf-next v3 1/2] selftests/bpf: Convert CHECK macros to ASSERT_* macros in bpf_iter 2023-10-28 5:24 ` [PATCH bpf-next v3 1/2] selftests/bpf: Convert CHECK macros to ASSERT_* macros in bpf_iter Yuran Pereira 2023-10-29 17:06 ` Yonghong Song @ 2023-10-30 19:39 ` Kui-Feng Lee 2023-11-02 6:00 ` patchwork-bot+netdevbpf 2 siblings, 0 replies; 8+ messages in thread From: Kui-Feng Lee @ 2023-10-30 19:39 UTC (permalink / raw) To: Yuran Pereira, bpf, yonghong.song Cc: ast, brauner, daniel, haoluo, iii, john.fastabend, jolsa, kpsingh, kuifeng, linux-kernel, linux-kselftest, mykolal, sdf, shuah, song, linux-kernel-mentees Acked-by: Kui-Feng Lee <thinker.li@gmail.com> On 10/27/23 22:24, Yuran Pereira wrote: > As it was pointed out by Yonghong Song [1], in the bpf selftests the use > of the ASSERT_* series of macros is preferred over the CHECK macro. > This patch replaces all CHECK calls in bpf_iter with the appropriate > ASSERT_* macros. > > [1] https://lore.kernel.org/lkml/0a142924-633c-44e6-9a92-2dc019656bf2@linux.dev > > Suggested-by: Yonghong Song <yonghong.song@linux.dev> > Signed-off-by: Yuran Pereira <yuran.pereira@hotmail.com> > --- > .../selftests/bpf/prog_tests/bpf_iter.c | 79 ++++++++----------- > 1 file changed, 35 insertions(+), 44 deletions(-) > > diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c > index 1f02168103dd..123a3502b8f0 100644 > --- a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c > +++ b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c > @@ -34,8 +34,6 @@ > #include "bpf_iter_ksym.skel.h" > #include "bpf_iter_sockmap.skel.h" > > -static int duration; > - > static void test_btf_id_or_null(void) > { > struct bpf_iter_test_kern3 *skel; > @@ -64,7 +62,7 @@ static void do_dummy_read_opts(struct bpf_program *prog, struct bpf_iter_attach_ > /* not check contents, but ensure read() ends without error */ > while ((len = read(iter_fd, buf, sizeof(buf))) > 0) > ; > - CHECK(len < 0, "read", "read failed: %s\n", strerror(errno)); > + ASSERT_GE(len, 0, "read"); > > close(iter_fd); > > @@ -413,7 +411,7 @@ static int do_btf_read(struct bpf_iter_task_btf *skel) > goto free_link; > } > > - if (CHECK(err < 0, "read", "read failed: %s\n", strerror(errno))) > + if (!ASSERT_GE(err, 0, "read")) > goto free_link; > > ASSERT_HAS_SUBSTR(taskbuf, "(struct task_struct)", > @@ -526,11 +524,11 @@ static int do_read_with_fd(int iter_fd, const char *expected, > start = 0; > while ((len = read(iter_fd, buf + start, read_buf_len)) > 0) { > start += len; > - if (CHECK(start >= 16, "read", "read len %d\n", len)) > + if (!ASSERT_LT(start, 16, "read")) > return -1; > read_buf_len = read_one_char ? 1 : 16 - start; > } > - if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno))) > + if (!ASSERT_GE(len, 0, "read")) > return -1; > > if (!ASSERT_STREQ(buf, expected, "read")) > @@ -571,8 +569,7 @@ static int do_read(const char *path, const char *expected) > int err, iter_fd; > > iter_fd = open(path, O_RDONLY); > - if (CHECK(iter_fd < 0, "open", "open %s failed: %s\n", > - path, strerror(errno))) > + if (!ASSERT_GE(iter_fd, 0, "open")) > return -1; > > err = do_read_with_fd(iter_fd, expected, false); > @@ -600,7 +597,7 @@ static void test_file_iter(void) > unlink(path); > > err = bpf_link__pin(link, path); > - if (CHECK(err, "pin_iter", "pin_iter to %s failed: %d\n", path, err)) > + if (!ASSERT_OK(err, "pin_iter")) > goto free_link; > > err = do_read(path, "abcd"); > @@ -651,12 +648,10 @@ static void test_overflow(bool test_e2big_overflow, bool ret1) > * overflow and needs restart. > */ > map1_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, 4, 8, 1, NULL); > - if (CHECK(map1_fd < 0, "bpf_map_create", > - "map_creation failed: %s\n", strerror(errno))) > + if (!ASSERT_GE(map1_fd, 0, "bpf_map_create")) > goto out; > map2_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, 4, 8, 1, NULL); > - if (CHECK(map2_fd < 0, "bpf_map_create", > - "map_creation failed: %s\n", strerror(errno))) > + if (!ASSERT_GE(map2_fd, 0, "bpf_map_create")) > goto free_map1; > > /* bpf_seq_printf kernel buffer is 8 pages, so one map > @@ -685,14 +680,12 @@ static void test_overflow(bool test_e2big_overflow, bool ret1) > /* setup filtering map_id in bpf program */ > map_info_len = sizeof(map_info); > err = bpf_map_get_info_by_fd(map1_fd, &map_info, &map_info_len); > - if (CHECK(err, "get_map_info", "get map info failed: %s\n", > - strerror(errno))) > + if (!ASSERT_OK(err, "get_map_info")) > goto free_map2; > skel->bss->map1_id = map_info.id; > > err = bpf_map_get_info_by_fd(map2_fd, &map_info, &map_info_len); > - if (CHECK(err, "get_map_info", "get map info failed: %s\n", > - strerror(errno))) > + if (!ASSERT_OK(err, "get_map_info")) > goto free_map2; > skel->bss->map2_id = map_info.id; > > @@ -714,16 +707,14 @@ static void test_overflow(bool test_e2big_overflow, bool ret1) > while ((len = read(iter_fd, buf, expected_read_len)) > 0) > total_read_len += len; > > - CHECK(len != -1 || errno != E2BIG, "read", > - "expected ret -1, errno E2BIG, but get ret %d, error %s\n", > - len, strerror(errno)); > + ASSERT_EQ(len, -1, "read"); > + ASSERT_EQ(errno, E2BIG, "read"); > goto free_buf; > } else if (!ret1) { > while ((len = read(iter_fd, buf, expected_read_len)) > 0) > total_read_len += len; > > - if (CHECK(len < 0, "read", "read failed: %s\n", > - strerror(errno))) > + if (!ASSERT_GE(len, 0, "read")) > goto free_buf; > } else { > do { > @@ -732,8 +723,7 @@ static void test_overflow(bool test_e2big_overflow, bool ret1) > total_read_len += len; > } while (len > 0 || len == -EAGAIN); > > - if (CHECK(len < 0, "read", "read failed: %s\n", > - strerror(errno))) > + if (!ASSERT_GE(len, 0, "read")) > goto free_buf; > } > > @@ -836,7 +826,7 @@ static void test_bpf_hash_map(void) > /* do some tests */ > while ((len = read(iter_fd, buf, sizeof(buf))) > 0) > ; > - if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno))) > + if (!ASSERT_GE(len, 0, "read")) > goto close_iter; > > /* test results */ > @@ -917,7 +907,7 @@ static void test_bpf_percpu_hash_map(void) > /* do some tests */ > while ((len = read(iter_fd, buf, sizeof(buf))) > 0) > ; > - if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno))) > + if (!ASSERT_GE(len, 0, "read")) > goto close_iter; > > /* test results */ > @@ -983,17 +973,14 @@ static void test_bpf_array_map(void) > start = 0; > while ((len = read(iter_fd, buf + start, sizeof(buf) - start)) > 0) > start += len; > - if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno))) > + if (!ASSERT_GE(len, 0, "read")) > goto close_iter; > > /* test results */ > res_first_key = *(__u32 *)buf; > res_first_val = *(__u64 *)(buf + sizeof(__u32)); > - if (CHECK(res_first_key != 0 || res_first_val != first_val, > - "bpf_seq_write", > - "seq_write failure: first key %u vs expected 0, " > - " first value %llu vs expected %llu\n", > - res_first_key, res_first_val, first_val)) > + if (!ASSERT_EQ(res_first_key, 0, "bpf_seq_write") || > + !ASSERT_EQ(res_first_val, first_val, "bpf_seq_write")) > goto close_iter; > > if (!ASSERT_EQ(skel->bss->key_sum, expected_key, "key_sum")) > @@ -1092,7 +1079,7 @@ static void test_bpf_percpu_array_map(void) > /* do some tests */ > while ((len = read(iter_fd, buf, sizeof(buf))) > 0) > ; > - if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno))) > + if (!ASSERT_GE(len, 0, "read")) > goto close_iter; > > /* test results */ > @@ -1131,6 +1118,7 @@ static void test_bpf_sk_storage_delete(void) > sock_fd = socket(AF_INET6, SOCK_STREAM, 0); > if (!ASSERT_GE(sock_fd, 0, "socket")) > goto out; > + > err = bpf_map_update_elem(map_fd, &sock_fd, &val, BPF_NOEXIST); > if (!ASSERT_OK(err, "map_update")) > goto out; > @@ -1151,14 +1139,19 @@ static void test_bpf_sk_storage_delete(void) > /* do some tests */ > while ((len = read(iter_fd, buf, sizeof(buf))) > 0) > ; > - if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno))) > + if (!ASSERT_GE(len, 0, "read")) > goto close_iter; > > /* test results */ > err = bpf_map_lookup_elem(map_fd, &sock_fd, &val); > - if (CHECK(!err || errno != ENOENT, "bpf_map_lookup_elem", > - "map value wasn't deleted (err=%d, errno=%d)\n", err, errno)) > - goto close_iter; > + > + /* Note: The following assertions serve to ensure > + * the value was deleted. It does so by asserting > + * that bpf_map_lookup_elem has failed. This might > + * seem counterintuitive at first. > + */ > + ASSERT_ERR(err, "bpf_map_lookup_elem"); > + ASSERT_EQ(errno, ENOENT, "bpf_map_lookup_elem"); > > close_iter: > close(iter_fd); > @@ -1203,17 +1196,15 @@ static void test_bpf_sk_storage_get(void) > do_dummy_read(skel->progs.fill_socket_owner); > > err = bpf_map_lookup_elem(map_fd, &sock_fd, &val); > - if (CHECK(err || val != getpid(), "bpf_map_lookup_elem", > - "map value wasn't set correctly (expected %d, got %d, err=%d)\n", > - getpid(), val, err)) > + if (!ASSERT_OK(err, "bpf_map_lookup_elem") || > + !ASSERT_EQ(val, getpid(), "bpf_map_lookup_elem")) > goto close_socket; > > do_dummy_read(skel->progs.negate_socket_local_storage); > > err = bpf_map_lookup_elem(map_fd, &sock_fd, &val); > - CHECK(err || val != -getpid(), "bpf_map_lookup_elem", > - "map value wasn't set correctly (expected %d, got %d, err=%d)\n", > - -getpid(), val, err); > + ASSERT_OK(err, "bpf_map_lookup_elem"); > + ASSERT_EQ(val, -getpid(), "bpf_map_lookup_elem"); > > close_socket: > close(sock_fd); > @@ -1290,7 +1281,7 @@ static void test_bpf_sk_storage_map(void) > /* do some tests */ > while ((len = read(iter_fd, buf, sizeof(buf))) > 0) > ; > - if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno))) > + if (!ASSERT_GE(len, 0, "read")) > goto close_iter; > > /* test results */ ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH bpf-next v3 1/2] selftests/bpf: Convert CHECK macros to ASSERT_* macros in bpf_iter 2023-10-28 5:24 ` [PATCH bpf-next v3 1/2] selftests/bpf: Convert CHECK macros to ASSERT_* macros in bpf_iter Yuran Pereira 2023-10-29 17:06 ` Yonghong Song 2023-10-30 19:39 ` Kui-Feng Lee @ 2023-11-02 6:00 ` patchwork-bot+netdevbpf 2 siblings, 0 replies; 8+ messages in thread From: patchwork-bot+netdevbpf @ 2023-11-02 6:00 UTC (permalink / raw) To: Yuran Pereira Cc: bpf, yonghong.song, sinquersw, ast, brauner, daniel, haoluo, iii, john.fastabend, jolsa, kpsingh, kuifeng, linux-kernel, linux-kselftest, mykolal, sdf, shuah, song, linux-kernel-mentees Hello: This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <ast@kernel.org>: On Sat, 28 Oct 2023 10:54:13 +0530 you wrote: > As it was pointed out by Yonghong Song [1], in the bpf selftests the use > of the ASSERT_* series of macros is preferred over the CHECK macro. > This patch replaces all CHECK calls in bpf_iter with the appropriate > ASSERT_* macros. > > [1] https://lore.kernel.org/lkml/0a142924-633c-44e6-9a92-2dc019656bf2@linux.dev > > [...] Here is the summary with links: - [bpf-next,v3,1/2] selftests/bpf: Convert CHECK macros to ASSERT_* macros in bpf_iter https://git.kernel.org/bpf/bpf-next/c/ed47cb27586d - [bpf-next,v3,2/2] selftests/bpf: Add malloc failure checks in bpf_iter https://git.kernel.org/bpf/bpf-next/c/cb3c6a58be50 You are awesome, thank you! -- Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH bpf-next v3 2/2] selftests/bpf: Add malloc failure checks in bpf_iter [not found] <cover.1698461732.git.yuran.pereira@hotmail.com> 2023-10-28 5:24 ` [PATCH bpf-next v3 0/2] selftests/bpf: Replaces CHECK macros and adds malloc failure checks to bpf_iter Yuran Pereira 2023-10-28 5:24 ` [PATCH bpf-next v3 1/2] selftests/bpf: Convert CHECK macros to ASSERT_* macros in bpf_iter Yuran Pereira @ 2023-10-28 5:24 ` Yuran Pereira 2023-10-29 17:07 ` Yonghong Song 2023-10-30 19:39 ` Kui-Feng Lee 2 siblings, 2 replies; 8+ messages in thread From: Yuran Pereira @ 2023-10-28 5:24 UTC (permalink / raw) To: bpf, yonghong.song Cc: sinquersw, ast, brauner, daniel, haoluo, iii, john.fastabend, jolsa, kpsingh, kuifeng, linux-kernel, linux-kselftest, mykolal, sdf, shuah, song, linux-kernel-mentees, Yuran Pereira Since some malloc calls in bpf_iter may at times fail, this patch adds the appropriate fail checks, and ensures that any previously allocated resource is appropriately destroyed before returning the function. Signed-off-by: Yuran Pereira <yuran.pereira@hotmail.com> --- tools/testing/selftests/bpf/prog_tests/bpf_iter.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c index 123a3502b8f0..1e02d1ba1c18 100644 --- a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c +++ b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c @@ -698,7 +698,7 @@ static void test_overflow(bool test_e2big_overflow, bool ret1) goto free_link; buf = malloc(expected_read_len); - if (!buf) + if (!ASSERT_OK_PTR(buf, "malloc")) goto close_iter; /* do read */ @@ -868,6 +868,8 @@ static void test_bpf_percpu_hash_map(void) skel->rodata->num_cpus = bpf_num_possible_cpus(); val = malloc(8 * bpf_num_possible_cpus()); + if (!ASSERT_OK_PTR(val, "malloc")) + goto out; err = bpf_iter_bpf_percpu_hash_map__load(skel); if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_percpu_hash_map__load")) @@ -1044,6 +1046,8 @@ static void test_bpf_percpu_array_map(void) skel->rodata->num_cpus = bpf_num_possible_cpus(); val = malloc(8 * bpf_num_possible_cpus()); + if (!ASSERT_OK_PTR(val, "malloc")) + goto out; err = bpf_iter_bpf_percpu_array_map__load(skel); if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_percpu_array_map__load")) -- 2.25.1 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH bpf-next v3 2/2] selftests/bpf: Add malloc failure checks in bpf_iter 2023-10-28 5:24 ` [PATCH bpf-next v3 2/2] selftests/bpf: Add malloc failure checks " Yuran Pereira @ 2023-10-29 17:07 ` Yonghong Song 2023-10-30 19:39 ` Kui-Feng Lee 1 sibling, 0 replies; 8+ messages in thread From: Yonghong Song @ 2023-10-29 17:07 UTC (permalink / raw) To: Yuran Pereira, bpf Cc: sinquersw, ast, brauner, daniel, haoluo, iii, john.fastabend, jolsa, kpsingh, kuifeng, linux-kernel, linux-kselftest, mykolal, sdf, shuah, song, linux-kernel-mentees On 10/27/23 10:24 PM, Yuran Pereira wrote: > Since some malloc calls in bpf_iter may at times fail, > this patch adds the appropriate fail checks, and ensures that > any previously allocated resource is appropriately destroyed > before returning the function. > > Signed-off-by: Yuran Pereira <yuran.pereira@hotmail.com> Acked-by: Yonghong Song <yonghong.song@linux.dev> ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH bpf-next v3 2/2] selftests/bpf: Add malloc failure checks in bpf_iter 2023-10-28 5:24 ` [PATCH bpf-next v3 2/2] selftests/bpf: Add malloc failure checks " Yuran Pereira 2023-10-29 17:07 ` Yonghong Song @ 2023-10-30 19:39 ` Kui-Feng Lee 1 sibling, 0 replies; 8+ messages in thread From: Kui-Feng Lee @ 2023-10-30 19:39 UTC (permalink / raw) To: Yuran Pereira, bpf, yonghong.song Cc: ast, brauner, daniel, haoluo, iii, john.fastabend, jolsa, kpsingh, kuifeng, linux-kernel, linux-kselftest, mykolal, sdf, shuah, song, linux-kernel-mentees Acked-by: Kui-Feng Lee <thinker.li@gmail.com> On 10/27/23 22:24, Yuran Pereira wrote: > Since some malloc calls in bpf_iter may at times fail, > this patch adds the appropriate fail checks, and ensures that > any previously allocated resource is appropriately destroyed > before returning the function. > > Signed-off-by: Yuran Pereira <yuran.pereira@hotmail.com> > --- > tools/testing/selftests/bpf/prog_tests/bpf_iter.c | 6 +++++- > 1 file changed, 5 insertions(+), 1 deletion(-) > > diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c > index 123a3502b8f0..1e02d1ba1c18 100644 > --- a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c > +++ b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c > @@ -698,7 +698,7 @@ static void test_overflow(bool test_e2big_overflow, bool ret1) > goto free_link; > > buf = malloc(expected_read_len); > - if (!buf) > + if (!ASSERT_OK_PTR(buf, "malloc")) > goto close_iter; > > /* do read */ > @@ -868,6 +868,8 @@ static void test_bpf_percpu_hash_map(void) > > skel->rodata->num_cpus = bpf_num_possible_cpus(); > val = malloc(8 * bpf_num_possible_cpus()); > + if (!ASSERT_OK_PTR(val, "malloc")) > + goto out; > > err = bpf_iter_bpf_percpu_hash_map__load(skel); > if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_percpu_hash_map__load")) > @@ -1044,6 +1046,8 @@ static void test_bpf_percpu_array_map(void) > > skel->rodata->num_cpus = bpf_num_possible_cpus(); > val = malloc(8 * bpf_num_possible_cpus()); > + if (!ASSERT_OK_PTR(val, "malloc")) > + goto out; > > err = bpf_iter_bpf_percpu_array_map__load(skel); > if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_percpu_array_map__load")) ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2023-11-02 6:00 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <cover.1698461732.git.yuran.pereira@hotmail.com>
2023-10-28 5:24 ` [PATCH bpf-next v3 0/2] selftests/bpf: Replaces CHECK macros and adds malloc failure checks to bpf_iter Yuran Pereira
2023-10-28 5:24 ` [PATCH bpf-next v3 1/2] selftests/bpf: Convert CHECK macros to ASSERT_* macros in bpf_iter Yuran Pereira
2023-10-29 17:06 ` Yonghong Song
2023-10-30 19:39 ` Kui-Feng Lee
2023-11-02 6:00 ` patchwork-bot+netdevbpf
2023-10-28 5:24 ` [PATCH bpf-next v3 2/2] selftests/bpf: Add malloc failure checks " Yuran Pereira
2023-10-29 17:07 ` Yonghong Song
2023-10-30 19:39 ` Kui-Feng Lee
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox