2021-10-30 09:24:16

by Yang Yingliang

[permalink] [raw]
Subject: [PATCH -next] bpf/benchs: Fix return value check of bpf_program__attach()

If bpf_program__attach() fails, it never returns NULL,
we should use libbpf_get_error() to check the return value.

Reported-by: Hulk Robot <[email protected]>
Signed-off-by: Yang Yingliang <[email protected]>
---
.../bpf/benchs/bench_bloom_filter_map.c | 20 ++++++++++++++-----
1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/bpf/benchs/bench_bloom_filter_map.c b/tools/testing/selftests/bpf/benchs/bench_bloom_filter_map.c
index 6eeeed2913e6..6879340b20c4 100644
--- a/tools/testing/selftests/bpf/benchs/bench_bloom_filter_map.c
+++ b/tools/testing/selftests/bpf/benchs/bench_bloom_filter_map.c
@@ -296,6 +296,7 @@ static struct bloom_filter_bench *setup_skeleton(void)
static void bloom_lookup_setup(void)
{
struct bpf_link *link;
+ int err;

ctx.use_array_map = true;

@@ -304,7 +305,8 @@ static void bloom_lookup_setup(void)
populate_maps();

link = bpf_program__attach(ctx.skel->progs.bloom_lookup);
- if (!link) {
+ err = libbpf_get_error(link);
+ if (err) {
fprintf(stderr, "failed to attach program!\n");
exit(1);
}
@@ -313,6 +315,7 @@ static void bloom_lookup_setup(void)
static void bloom_update_setup(void)
{
struct bpf_link *link;
+ int err;

ctx.use_array_map = true;

@@ -321,7 +324,8 @@ static void bloom_update_setup(void)
populate_maps();

link = bpf_program__attach(ctx.skel->progs.bloom_update);
- if (!link) {
+ err = libbpf_get_error(link);
+ if (err) {
fprintf(stderr, "failed to attach program!\n");
exit(1);
}
@@ -330,6 +334,7 @@ static void bloom_update_setup(void)
static void false_positive_setup(void)
{
struct bpf_link *link;
+ int err;

ctx.use_hashmap = true;
ctx.hashmap_use_bloom = true;
@@ -340,7 +345,8 @@ static void false_positive_setup(void)
populate_maps();

link = bpf_program__attach(ctx.skel->progs.bloom_hashmap_lookup);
- if (!link) {
+ err = libbpf_get_error(link);
+ if (err) {
fprintf(stderr, "failed to attach program!\n");
exit(1);
}
@@ -349,6 +355,7 @@ static void false_positive_setup(void)
static void hashmap_with_bloom_setup(void)
{
struct bpf_link *link;
+ int err;

ctx.use_hashmap = true;
ctx.hashmap_use_bloom = true;
@@ -358,7 +365,8 @@ static void hashmap_with_bloom_setup(void)
populate_maps();

link = bpf_program__attach(ctx.skel->progs.bloom_hashmap_lookup);
- if (!link) {
+ err = libbpf_get_error(link);
+ if (err) {
fprintf(stderr, "failed to attach program!\n");
exit(1);
}
@@ -367,6 +375,7 @@ static void hashmap_with_bloom_setup(void)
static void hashmap_no_bloom_setup(void)
{
struct bpf_link *link;
+ int err;

ctx.use_hashmap = true;

@@ -375,7 +384,8 @@ static void hashmap_no_bloom_setup(void)
populate_maps();

link = bpf_program__attach(ctx.skel->progs.bloom_hashmap_lookup);
- if (!link) {
+ err = libbpf_get_error(link);
+ if (err) {
fprintf(stderr, "failed to attach program!\n");
exit(1);
}
--
2.25.1


2021-11-01 04:15:19

by Yonghong Song

[permalink] [raw]
Subject: Re: [PATCH -next] bpf/benchs: Fix return value check of bpf_program__attach()



On 10/30/21 2:30 AM, Yang Yingliang wrote:
> If bpf_program__attach() fails, it never returns NULL,
> we should use libbpf_get_error() to check the return value.
>
> Reported-by: Hulk Robot <[email protected]>
> Signed-off-by: Yang Yingliang <[email protected]>

Thanks for the fix. LGTM with a nit below.

Acked-by: Yonghong Song <[email protected]>

> ---
> .../bpf/benchs/bench_bloom_filter_map.c | 20 ++++++++++++++-----
> 1 file changed, 15 insertions(+), 5 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/benchs/bench_bloom_filter_map.c b/tools/testing/selftests/bpf/benchs/bench_bloom_filter_map.c
> index 6eeeed2913e6..6879340b20c4 100644
> --- a/tools/testing/selftests/bpf/benchs/bench_bloom_filter_map.c
> +++ b/tools/testing/selftests/bpf/benchs/bench_bloom_filter_map.c
> @@ -296,6 +296,7 @@ static struct bloom_filter_bench *setup_skeleton(void)
> static void bloom_lookup_setup(void)
> {
> struct bpf_link *link;
> + int err;
>
> ctx.use_array_map = true;
>
> @@ -304,7 +305,8 @@ static void bloom_lookup_setup(void)
> populate_maps();
>
> link = bpf_program__attach(ctx.skel->progs.bloom_lookup);
> - if (!link) {
> + err = libbpf_get_error(link);
> + if (err) {

There is no need to define 'int err', you can just do
if (libbpf_get_error(link))

The same for a few 'int err' below.

> fprintf(stderr, "failed to attach program!\n");
> exit(1);
> }
> @@ -313,6 +315,7 @@ static void bloom_lookup_setup(void)
> static void bloom_update_setup(void)
> {
> struct bpf_link *link;
> + int err;
>
> ctx.use_array_map = true;
>
> @@ -321,7 +324,8 @@ static void bloom_update_setup(void)
> populate_maps();
>
> link = bpf_program__attach(ctx.skel->progs.bloom_update);
> - if (!link) {
> + err = libbpf_get_error(link);
> + if (err) {
> fprintf(stderr, "failed to attach program!\n");
> exit(1);
> }
> @@ -330,6 +334,7 @@ static void bloom_update_setup(void)
> static void false_positive_setup(void)
> {
> struct bpf_link *link;
> + int err;
>
> ctx.use_hashmap = true;
> ctx.hashmap_use_bloom = true;
> @@ -340,7 +345,8 @@ static void false_positive_setup(void)
> populate_maps();
>
> link = bpf_program__attach(ctx.skel->progs.bloom_hashmap_lookup);
> - if (!link) {
> + err = libbpf_get_error(link);
> + if (err) {
> fprintf(stderr, "failed to attach program!\n");
> exit(1);
> }
> @@ -349,6 +355,7 @@ static void false_positive_setup(void)
> static void hashmap_with_bloom_setup(void)
> {
> struct bpf_link *link;
> + int err;
>
> ctx.use_hashmap = true;
> ctx.hashmap_use_bloom = true;
> @@ -358,7 +365,8 @@ static void hashmap_with_bloom_setup(void)
> populate_maps();
>
> link = bpf_program__attach(ctx.skel->progs.bloom_hashmap_lookup);
> - if (!link) {
> + err = libbpf_get_error(link);
> + if (err) {
> fprintf(stderr, "failed to attach program!\n");
> exit(1);
> }
> @@ -367,6 +375,7 @@ static void hashmap_with_bloom_setup(void)
> static void hashmap_no_bloom_setup(void)
> {
> struct bpf_link *link;
> + int err;
>
> ctx.use_hashmap = true;
>
> @@ -375,7 +384,8 @@ static void hashmap_no_bloom_setup(void)
> populate_maps();
>
> link = bpf_program__attach(ctx.skel->progs.bloom_hashmap_lookup);
> - if (!link) {
> + err = libbpf_get_error(link);
> + if (err) {
> fprintf(stderr, "failed to attach program!\n");
> exit(1);
> }
>