Skip to contentSkip to conversation

bpf: Add percpu map value size check

PATCH Superseded Tao Chen 3 participants 8 messages Sep 9, 2024 – Sep 10, 2024

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Tao Chen

author
#0 lore raw

v2 cover bpf: Add percpu map value size check

Check percpu map value size first and add the test case in selftest.

Change list:
- v1 -> v2:
- round up map value size with 8 bytes in patch 1
- add selftest case in patch 2

Tao Chen (2):
bpf: Check percpu map value size first bpf/selftests: Check errno when percpu map value size exceeds

 kernel/bpf/arraymap.c                         |  3 ++
 kernel/bpf/hashtab.c                          |  3 ++
 .../selftests/bpf/prog_tests/map_init.c       | 32 +++++++++++++++++++
 .../selftests/bpf/progs/test_map_init.c       |  6 ++++
 4 files changed, 44 insertions(+)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v2 1/2 bpf: Check percpu map value size first

Percpu map is often used, but the map value size limit often ignored, like issue: https://github.com/iovisor/bcc/issues/2519. Actually, percpu map value size is bound by PCPU_MIN_UNIT_SIZE, so we can check the value size whether it exceeds PCPU_MIN_UNIT_SIZE first, like percpu map of local_storage. Maybe the error message seems clearer compared with "cannot allocate memory".

---
 kernel/bpf/arraymap.c | 3 +++
 kernel/bpf/hashtab.c  | 3 +++
 2 files changed, 6 insertions(+)
kernel/bpf/arraymap.c +3 -0
index a43e62e2a8bb..79660e3fca4c 100644
@@ -73,6 +73,9 @@ int array_map_alloc_check(union bpf_attr *attr)
 	/* avoid overflow on round_up(map->value_size) */
 	if (attr->value_size > INT_MAX)
 		return -E2BIG;
+	/* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */
+	if (percpu && round_up(attr->value_size, 8) > PCPU_MIN_UNIT_SIZE)
+		return -E2BIG;
 
 	return 0;
 }
kernel/bpf/hashtab.c +3 -0
index 45c7195b65ba..b14b87463ee0 100644
@@ -462,6 +462,9 @@ static int htab_map_alloc_check(union bpf_attr *attr)
 		 * kmalloc-able later in htab_map_update_elem()
 		 */
 		return -E2BIG;
+	/* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */
+	if (percpu && round_up(attr->value_size, 8) > PCPU_MIN_UNIT_SIZE)
+		return -E2BIG;
 
 	return 0;
 }
Signature · 1 line
2.25.1
Acked-by on v2 1/2
#2 lore raw

Re: [v2 PATCH bpf-next 1/2] bpf: Check percpu map value size first

Quoted 10 lines · Tao Chen

Percpu map is often used, but the map value size limit often ignored, like issue: https://github.com/iovisor/bcc/issues/2519. Actually, percpu map value size is bound by PCPU_MIN_UNIT_SIZE, so we can check the value size whether it exceeds PCPU_MIN_UNIT_SIZE first, like percpu map of local_storage. Maybe the error message seems clearer compared with "cannot allocate memory".

names in SOB should be capitalized

the check is useful, so:

Quoted 36 lines
---
 kernel/bpf/arraymap.c | 3 +++
 kernel/bpf/hashtab.c  | 3 +++
 2 files changed, 6 insertions(+)
kernel/bpf/arraymap.c +3 -0
index a43e62e2a8bb..79660e3fca4c 100644
@@ -73,6 +73,9 @@ int array_map_alloc_check(union bpf_attr *attr)
        /* avoid overflow on round_up(map->value_size) */
        if (attr->value_size > INT_MAX)
                return -E2BIG;
+       /* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */
+       if (percpu && round_up(attr->value_size, 8) > PCPU_MIN_UNIT_SIZE)
+               return -E2BIG;
 
        return 0;
 }
kernel/bpf/hashtab.c +3 -1
index 45c7195b65ba..b14b87463ee0 100644
@@ -462,6 +462,9 @@ static int htab_map_alloc_check(union bpf_attr *attr)
                 * kmalloc-able later in htab_map_update_elem()
                 */
                return -E2BIG;
+       /* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */
+       if (percpu && round_up(attr->value_size, 8) > PCPU_MIN_UNIT_SIZE)
+               return -E2BIG;
 
        return 0;
 }
--

2.25.1

author
#3 lore raw

Re: [v2 PATCH bpf-next 1/2] bpf: Check percpu map value size first

Quoted 9 lines · Andrii Nakryiko
Quoted 8 lines · Tao Chen

Percpu map is often used, but the map value size limit often ignored, like issue: https://github.com/iovisor/bcc/issues/2519. Actually, percpu map value size is bound by PCPU_MIN_UNIT_SIZE, so we can check the value size whether it exceeds PCPU_MIN_UNIT_SIZE first, like percpu map of local_storage. Maybe the error message seems clearer compared with "cannot allocate memory".

Quoted 4 lines
Quoted 2 lines

names in SOB should be capitalized

Hi Andrii,thank you for your attention, i will change it.

Quoted 41 lines

the check is useful, so:

Quoted 36 lines
---
  kernel/bpf/arraymap.c | 3 +++
  kernel/bpf/hashtab.c  | 3 +++
  2 files changed, 6 insertions(+)
kernel/bpf/arraymap.c +3 -0
index a43e62e2a8bb..79660e3fca4c 100644
@@ -73,6 +73,9 @@ int array_map_alloc_check(union bpf_attr *attr)
         /* avoid overflow on round_up(map->value_size) */
         if (attr->value_size > INT_MAX)
                 return -E2BIG;
+       /* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */
+       if (percpu && round_up(attr->value_size, 8) > PCPU_MIN_UNIT_SIZE)
+               return -E2BIG;
 
         return 0;
  }
kernel/bpf/hashtab.c +3 -1
index 45c7195b65ba..b14b87463ee0 100644
@@ -462,6 +462,9 @@ static int htab_map_alloc_check(union bpf_attr *attr)
                  * kmalloc-able later in htab_map_update_elem()
                  */
                 return -E2BIG;
+       /* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */
+       if (percpu && round_up(attr->value_size, 8) > PCPU_MIN_UNIT_SIZE)
+               return -E2BIG;
 
         return 0;
  }
--

2.25.1

Signature · 2 lines
Best Regards
Dylane Chen
author
#4 lore raw

PATCH v2 2/2 bpf/selftests: Check errno when percpu map value size exceeds

In reply to Tao Chen

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
---
 .../selftests/bpf/prog_tests/map_init.c       | 32 +++++++++++++++++++
 .../selftests/bpf/progs/test_map_init.c       |  6 ++++
 2 files changed, 38 insertions(+)
tools/testing/selftests/bpf/prog_tests/map_init.c +32 -0
index 14a31109dd0e..7f1a6fa3679f 100644
@@ -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();
 }
tools/testing/selftests/bpf/progs/test_map_init.c +6 -0
index c89d28ead673..7a772cbf0570 100644
@@ -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)
Signature · 1 line
2.25.1
#5 lore raw

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

Quoted 49 lines · Tao Chen

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
---
 .../selftests/bpf/prog_tests/map_init.c       | 32 +++++++++++++++++++
 .../selftests/bpf/progs/test_map_init.c       |  6 ++++
 2 files changed, 38 insertions(+)
tools/testing/selftests/bpf/prog_tests/map_init.c +16 -0
index 14a31109dd0e..7f1a6fa3679f 100644
@@ -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"))

tools/testing/selftests/bpf/prog_tests/map_init.c static int check_values_one_cpu(pcpu_map_value_t *value, map_value_t expected) · line 146

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/map_init.c +10 -0
+                       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

Quoted 34 lines
tools/testing/selftests/bpf/prog_tests/map_init.c +6 -0
+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();
 }
tools/testing/selftests/bpf/progs/test_map_init.c +6 -1
index c89d28ead673..7a772cbf0570 100644
@@ -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

author
#6 lore raw

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

Quoted 65 lines · Andrii Nakryiko
Quoted 49 lines · Tao Chen

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
---
  .../selftests/bpf/prog_tests/map_init.c       | 32 +++++++++++++++++++
  .../selftests/bpf/progs/test_map_init.c       |  6 ++++
  2 files changed, 38 insertions(+)
tools/testing/selftests/bpf/prog_tests/map_init.c +16 -0
index 14a31109dd0e..7f1a6fa3679f 100644
@@ -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"))

tools/testing/selftests/bpf/prog_tests/map_init.c static int check_values_one_cpu(pcpu_map_value_t *value, map_value_t expected) · line 146

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/map_init.c +10 -0
+                       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?

Quoted 2 lines

pw-bot: cr

Ok, i will use the bpf_map_create API in v3.

Quoted 34 lines
Quoted 34 lines
tools/testing/selftests/bpf/prog_tests/map_init.c +6 -0
+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();
  }
tools/testing/selftests/bpf/progs/test_map_init.c +6 -1
index c89d28ead673..7a772cbf0570 100644
@@ -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

Signature · 2 lines
Best Regards
Dylane Chen
Acked-by on the v2 series
#7 lore raw
In reply to Tao Chen
Quoted 10 lines · Tao Chen

Check percpu map value size first and add the test case in selftest.

Change list:
- v1 -> v2:
- round up map value size with 8 bytes in patch 1
- add selftest case in patch 2

Tao Chen (2):
bpf: Check percpu map value size first bpf/selftests: Check errno when percpu map value size exceeds

lgtm

jirka

Quoted 11 lines
 kernel/bpf/arraymap.c                         |  3 ++
 kernel/bpf/hashtab.c                          |  3 ++
 .../selftests/bpf/prog_tests/map_init.c       | 32 +++++++++++++++++++
 .../selftests/bpf/progs/test_map_init.c       |  6 ++++
 4 files changed, 44 insertions(+)
Signature · 1 line
2.25.1