[v2 PATCH bpf-next 2/2] bpf/selftests: Check errno when percpu map value size exceeds

Tao Chen posted 2 patches 2 months, 3 weeks ago
There is a newer version of this series
[v2 PATCH bpf-next 2/2] bpf/selftests: Check errno when percpu map value size exceeds
Posted by Tao Chen 2 months, 3 weeks ago
This test case checks the errno message when percpu map value size
exceeds PCPU_MIN_UNIT_SIZE.

root@debian:~# ./test_progs -t map_init
 #160/1   map_init/pcpu_map_init:OK
 #160/2   map_init/pcpu_lru_map_init:OK
 #160/3   map_init/pcpu map value size:OK
 #160     map_init:OK
Summary: 1/3 PASSED, 0 SKIPPED, 0 FAILED

Signed-off-by: Tao Chen <chen.dylane@gmail.com>
Signed-off-by: jinke han <jinkehan@didiglobal.com>
---
 .../selftests/bpf/prog_tests/map_init.c       | 32 +++++++++++++++++++
 .../selftests/bpf/progs/test_map_init.c       |  6 ++++
 2 files changed, 38 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/map_init.c b/tools/testing/selftests/bpf/prog_tests/map_init.c
index 14a31109dd0e..7f1a6fa3679f 100644
--- a/tools/testing/selftests/bpf/prog_tests/map_init.c
+++ b/tools/testing/selftests/bpf/prog_tests/map_init.c
@@ -6,6 +6,7 @@
 
 #define TEST_VALUE 0x1234
 #define FILL_VALUE 0xdeadbeef
+#define PCPU_MIN_UNIT_SIZE 32768
 
 static int nr_cpus;
 static int duration;
@@ -118,6 +119,35 @@ static int check_values_one_cpu(pcpu_map_value_t *value, map_value_t expected)
 
 	return 0;
 }
+/*
+ * percpu map value size is bound by PCPU_MIN_UNIT_SIZE
+ * check the errno when the value exceed PCPU_MIN_UNIT_SIZE
+ */
+static void test_pcpu_map_value_size(void)
+{
+	struct test_map_init *skel;
+	int err;
+	int value_sz = PCPU_MIN_UNIT_SIZE + 1;
+	enum bpf_map_type map_types[] = { BPF_MAP_TYPE_PERCPU_ARRAY,
+					  BPF_MAP_TYPE_PERCPU_HASH,
+					  BPF_MAP_TYPE_LRU_PERCPU_HASH };
+	for (int i = 0; i < ARRAY_SIZE(map_types); i++) {
+		skel = test_map_init__open();
+		if (!ASSERT_OK_PTR(skel, "skel_open"))
+			return;
+		err = bpf_map__set_type(skel->maps.hashmap2, map_types[i]);
+		if (!ASSERT_OK(err, "bpf_map__set_type"))
+			goto error;
+		err = bpf_map__set_value_size(skel->maps.hashmap2, value_sz);
+		if (!ASSERT_OK(err, "bpf_map__set_value_size"))
+			goto error;
+
+		err = test_map_init__load(skel);
+		ASSERT_EQ(err, -E2BIG, "skel_load");
+error:
+		test_map_init__destroy(skel);
+	}
+}
 
 /* Add key=1 elem with values set for all CPUs
  * Delete elem key=1
@@ -211,4 +241,6 @@ void test_map_init(void)
 		test_pcpu_map_init();
 	if (test__start_subtest("pcpu_lru_map_init"))
 		test_pcpu_lru_map_init();
+	if (test__start_subtest("pcpu map value size"))
+		test_pcpu_map_value_size();
 }
diff --git a/tools/testing/selftests/bpf/progs/test_map_init.c b/tools/testing/selftests/bpf/progs/test_map_init.c
index c89d28ead673..7a772cbf0570 100644
--- a/tools/testing/selftests/bpf/progs/test_map_init.c
+++ b/tools/testing/selftests/bpf/progs/test_map_init.c
@@ -15,6 +15,12 @@ struct {
 	__type(value, __u64);
 } hashmap1 SEC(".maps");
 
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, __u64);
+} hashmap2 SEC(".maps");
 
 SEC("tp/syscalls/sys_enter_getpgid")
 int sysenter_getpgid(const void *ctx)
-- 
2.25.1
Re: [v2 PATCH bpf-next 2/2] bpf/selftests: Check errno when percpu map value size exceeds
Posted by Andrii Nakryiko 2 months, 3 weeks ago
On Mon, Sep 9, 2024 at 12:14 AM Tao Chen <chen.dylane@gmail.com> wrote:
>
> This test case checks the errno message when percpu map value size
> exceeds PCPU_MIN_UNIT_SIZE.
>
> root@debian:~# ./test_progs -t map_init
>  #160/1   map_init/pcpu_map_init:OK
>  #160/2   map_init/pcpu_lru_map_init:OK
>  #160/3   map_init/pcpu map value size:OK
>  #160     map_init:OK
> Summary: 1/3 PASSED, 0 SKIPPED, 0 FAILED
>
> Signed-off-by: Tao Chen <chen.dylane@gmail.com>
> Signed-off-by: jinke han <jinkehan@didiglobal.com>
> ---
>  .../selftests/bpf/prog_tests/map_init.c       | 32 +++++++++++++++++++
>  .../selftests/bpf/progs/test_map_init.c       |  6 ++++
>  2 files changed, 38 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/map_init.c b/tools/testing/selftests/bpf/prog_tests/map_init.c
> index 14a31109dd0e..7f1a6fa3679f 100644
> --- a/tools/testing/selftests/bpf/prog_tests/map_init.c
> +++ b/tools/testing/selftests/bpf/prog_tests/map_init.c
> @@ -6,6 +6,7 @@
>
>  #define TEST_VALUE 0x1234
>  #define FILL_VALUE 0xdeadbeef
> +#define PCPU_MIN_UNIT_SIZE 32768
>
>  static int nr_cpus;
>  static int duration;
> @@ -118,6 +119,35 @@ static int check_values_one_cpu(pcpu_map_value_t *value, map_value_t expected)
>
>         return 0;
>  }
> +/*
> + * percpu map value size is bound by PCPU_MIN_UNIT_SIZE
> + * check the errno when the value exceed PCPU_MIN_UNIT_SIZE
> + */
> +static void test_pcpu_map_value_size(void)
> +{
> +       struct test_map_init *skel;
> +       int err;
> +       int value_sz = PCPU_MIN_UNIT_SIZE + 1;
> +       enum bpf_map_type map_types[] = { BPF_MAP_TYPE_PERCPU_ARRAY,
> +                                         BPF_MAP_TYPE_PERCPU_HASH,
> +                                         BPF_MAP_TYPE_LRU_PERCPU_HASH };
> +       for (int i = 0; i < ARRAY_SIZE(map_types); i++) {
> +               skel = test_map_init__open();
> +               if (!ASSERT_OK_PTR(skel, "skel_open"))
> +                       return;
> +               err = bpf_map__set_type(skel->maps.hashmap2, map_types[i]);
> +               if (!ASSERT_OK(err, "bpf_map__set_type"))
> +                       goto error;
> +               err = bpf_map__set_value_size(skel->maps.hashmap2, value_sz);
> +               if (!ASSERT_OK(err, "bpf_map__set_value_size"))
> +                       goto error;
> +
> +               err = test_map_init__load(skel);
> +               ASSERT_EQ(err, -E2BIG, "skel_load");

This is quite an overkill to test map creation. It will be much more
straightforward to just use low-level bpf_map_create() API, can you
please make use of that instead?

pw-bot: cr

> +error:
> +               test_map_init__destroy(skel);
> +       }
> +}
>
>  /* Add key=1 elem with values set for all CPUs
>   * Delete elem key=1
> @@ -211,4 +241,6 @@ void test_map_init(void)
>                 test_pcpu_map_init();
>         if (test__start_subtest("pcpu_lru_map_init"))
>                 test_pcpu_lru_map_init();
> +       if (test__start_subtest("pcpu map value size"))
> +               test_pcpu_map_value_size();
>  }
> diff --git a/tools/testing/selftests/bpf/progs/test_map_init.c b/tools/testing/selftests/bpf/progs/test_map_init.c
> index c89d28ead673..7a772cbf0570 100644
> --- a/tools/testing/selftests/bpf/progs/test_map_init.c
> +++ b/tools/testing/selftests/bpf/progs/test_map_init.c
> @@ -15,6 +15,12 @@ struct {
>         __type(value, __u64);
>  } hashmap1 SEC(".maps");
>
> +struct {
> +       __uint(type, BPF_MAP_TYPE_HASH);
> +       __uint(max_entries, 1);
> +       __type(key, __u32);
> +       __type(value, __u64);
> +} hashmap2 SEC(".maps");
>
>  SEC("tp/syscalls/sys_enter_getpgid")
>  int sysenter_getpgid(const void *ctx)
> --
> 2.25.1
>
Re: [v2 PATCH bpf-next 2/2] bpf/selftests: Check errno when percpu map value size exceeds
Posted by Tao Chen 2 months, 3 weeks ago
在 2024/9/10 04:16, Andrii Nakryiko 写道:
> On Mon, Sep 9, 2024 at 12:14 AM Tao Chen <chen.dylane@gmail.com> wrote:
>>
>> This test case checks the errno message when percpu map value size
>> exceeds PCPU_MIN_UNIT_SIZE.
>>
>> root@debian:~# ./test_progs -t map_init
>>   #160/1   map_init/pcpu_map_init:OK
>>   #160/2   map_init/pcpu_lru_map_init:OK
>>   #160/3   map_init/pcpu map value size:OK
>>   #160     map_init:OK
>> Summary: 1/3 PASSED, 0 SKIPPED, 0 FAILED
>>
>> Signed-off-by: Tao Chen <chen.dylane@gmail.com>
>> Signed-off-by: jinke han <jinkehan@didiglobal.com>
>> ---
>>   .../selftests/bpf/prog_tests/map_init.c       | 32 +++++++++++++++++++
>>   .../selftests/bpf/progs/test_map_init.c       |  6 ++++
>>   2 files changed, 38 insertions(+)
>>
>> diff --git a/tools/testing/selftests/bpf/prog_tests/map_init.c b/tools/testing/selftests/bpf/prog_tests/map_init.c
>> index 14a31109dd0e..7f1a6fa3679f 100644
>> --- a/tools/testing/selftests/bpf/prog_tests/map_init.c
>> +++ b/tools/testing/selftests/bpf/prog_tests/map_init.c
>> @@ -6,6 +6,7 @@
>>
>>   #define TEST_VALUE 0x1234
>>   #define FILL_VALUE 0xdeadbeef
>> +#define PCPU_MIN_UNIT_SIZE 32768
>>
>>   static int nr_cpus;
>>   static int duration;
>> @@ -118,6 +119,35 @@ static int check_values_one_cpu(pcpu_map_value_t *value, map_value_t expected)
>>
>>          return 0;
>>   }
>> +/*
>> + * percpu map value size is bound by PCPU_MIN_UNIT_SIZE
>> + * check the errno when the value exceed PCPU_MIN_UNIT_SIZE
>> + */
>> +static void test_pcpu_map_value_size(void)
>> +{
>> +       struct test_map_init *skel;
>> +       int err;
>> +       int value_sz = PCPU_MIN_UNIT_SIZE + 1;
>> +       enum bpf_map_type map_types[] = { BPF_MAP_TYPE_PERCPU_ARRAY,
>> +                                         BPF_MAP_TYPE_PERCPU_HASH,
>> +                                         BPF_MAP_TYPE_LRU_PERCPU_HASH };
>> +       for (int i = 0; i < ARRAY_SIZE(map_types); i++) {
>> +               skel = test_map_init__open();
>> +               if (!ASSERT_OK_PTR(skel, "skel_open"))
>> +                       return;
>> +               err = bpf_map__set_type(skel->maps.hashmap2, map_types[i]);
>> +               if (!ASSERT_OK(err, "bpf_map__set_type"))
>> +                       goto error;
>> +               err = bpf_map__set_value_size(skel->maps.hashmap2, value_sz);
>> +               if (!ASSERT_OK(err, "bpf_map__set_value_size"))
>> +                       goto error;
>> +
>> +               err = test_map_init__load(skel);
>> +               ASSERT_EQ(err, -E2BIG, "skel_load");
> 
> This is quite an overkill to test map creation. It will be much more
> straightforward to just use low-level bpf_map_create() API, can you
> please make use of that instead?
> 
> pw-bot: cr
> 

Ok, i will use the bpf_map_create API in v3.

>> +error:
>> +               test_map_init__destroy(skel);
>> +       }
>> +}
>>
>>   /* Add key=1 elem with values set for all CPUs
>>    * Delete elem key=1
>> @@ -211,4 +241,6 @@ void test_map_init(void)
>>                  test_pcpu_map_init();
>>          if (test__start_subtest("pcpu_lru_map_init"))
>>                  test_pcpu_lru_map_init();
>> +       if (test__start_subtest("pcpu map value size"))
>> +               test_pcpu_map_value_size();
>>   }
>> diff --git a/tools/testing/selftests/bpf/progs/test_map_init.c b/tools/testing/selftests/bpf/progs/test_map_init.c
>> index c89d28ead673..7a772cbf0570 100644
>> --- a/tools/testing/selftests/bpf/progs/test_map_init.c
>> +++ b/tools/testing/selftests/bpf/progs/test_map_init.c
>> @@ -15,6 +15,12 @@ struct {
>>          __type(value, __u64);
>>   } hashmap1 SEC(".maps");
>>
>> +struct {
>> +       __uint(type, BPF_MAP_TYPE_HASH);
>> +       __uint(max_entries, 1);
>> +       __type(key, __u32);
>> +       __type(value, __u64);
>> +} hashmap2 SEC(".maps");
>>
>>   SEC("tp/syscalls/sys_enter_getpgid")
>>   int sysenter_getpgid(const void *ctx)
>> --
>> 2.25.1
>>


-- 
Best Regards
Dylane Chen