Skip to contentSkip to conversation

bpf: increase supported cgroup storage value size

PATCH Superseded Stanislav Fomichev 3 participants 6 messages Jul 26, 2021 – Jul 27, 2021

Subsystems (5)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 bpf: increase supported cgroup storage value size

Current max cgroup storage value size is 4k (PAGE_SIZE). The other local storages accept up to 64k (BPF_LOCAL_STORAGE_MAX_VALUE_SIZE). Let's align max cgroup value size with the other storages.

For percpu, the max is 32k (PCPU_MIN_UNIT_SIZE) because percpu allocator is not happy about larger values.

netcnt test is extended to exercise those maximum values (non-percpu max size is close to, but not real max).

v3:
* refine SIZEOF_BPF_LOCAL_STORAGE_ELEM comment (Yonghong Song)
* anonymous struct in percpu_net_cnt & net_cnt (Yonghong Song)
* reorder free (Yonghong Song)

v2:
* cap max_value_size instead of BUILD_BUG_ON (Martin KaFai Lau)

Cc Martin KaFai Lau <[email protected]>
Cc Yonghong Song <[email protected]>
---
 kernel/bpf/local_storage.c                  | 11 +++++-
 tools/testing/selftests/bpf/netcnt_common.h | 38 +++++++++++++++++----
 tools/testing/selftests/bpf/test_netcnt.c   | 17 ++++++---
 3 files changed, 53 insertions(+), 13 deletions(-)
kernel/bpf/local_storage.c +10 -1
index 7ed2a14dc0de..035e9e3a7132 100644
@@ -1,6 +1,7 @@
 //SPDX-License-Identifier: GPL-2.0
 #include <linux/bpf-cgroup.h>
 #include <linux/bpf.h>
+#include <linux/bpf_local_storage.h>
 #include <linux/btf.h>
 #include <linux/bug.h>
 #include <linux/filter.h>
@@ -283,9 +284,17 @@ static int cgroup_storage_get_next_key(struct bpf_map *_map, void *key,
 
 static struct bpf_map *cgroup_storage_map_alloc(union bpf_attr *attr)
 {
+	__u32 max_value_size = BPF_LOCAL_STORAGE_MAX_VALUE_SIZE;
 	int numa_node = bpf_map_attr_numa_node(attr);
 	struct bpf_cgroup_storage_map *map;
 
+	/* percpu is bound by PCPU_MIN_UNIT_SIZE, non-percu
+	 * is the same as other local storages.
+	 */
+	if (attr->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
+		max_value_size = min_t(__u32, max_value_size,
+				       PCPU_MIN_UNIT_SIZE);
+
 	if (attr->key_size != sizeof(struct bpf_cgroup_storage_key) &&
 	    attr->key_size != sizeof(__u64))
 		return ERR_PTR(-EINVAL);
@@ -293,7 +302,7 @@ static struct bpf_map *cgroup_storage_map_alloc(union bpf_attr *attr)
 	if (attr->value_size == 0)
 		return ERR_PTR(-EINVAL);
 
-	if (attr->value_size > PAGE_SIZE)
+	if (attr->value_size > max_value_size)
 		return ERR_PTR(-E2BIG);
 
 	if (attr->map_flags & ~LOCAL_STORAGE_CREATE_FLAG_MASK ||
tools/testing/selftests/bpf/netcnt_common.h +31 -7
index 81084c1c2c23..87f5b97e1932 100644
@@ -6,19 +6,43 @@
 
 #define MAX_PERCPU_PACKETS 32
 
+/* sizeof(struct bpf_local_storage_elem):
+ *
+ * It really is about 128 bytes on x86_64, but allocate more to account for
+ * possible layout changes, different architectures, etc.
+ * The kernel will wrap up to PAGE_SIZE internally anyway.
+ */
+#define SIZEOF_BPF_LOCAL_STORAGE_ELEM		256
+
+/* Try to estimate kernel's BPF_LOCAL_STORAGE_MAX_VALUE_SIZE: */
+#define BPF_LOCAL_STORAGE_MAX_VALUE_SIZE	(0xFFFF - \
+						 SIZEOF_BPF_LOCAL_STORAGE_ELEM)
+
+#define PCPU_MIN_UNIT_SIZE			32768
+
 struct percpu_net_cnt {
-	__u64 packets;
-	__u64 bytes;
+	union {
+		struct {
+			__u64 packets;
+			__u64 bytes;
 
-	__u64 prev_ts;
+			__u64 prev_ts;
 
-	__u64 prev_packets;
-	__u64 prev_bytes;
+			__u64 prev_packets;
+			__u64 prev_bytes;
+		};
+		__u8 data[PCPU_MIN_UNIT_SIZE];
+	};
 };
 
 struct net_cnt {
-	__u64 packets;
-	__u64 bytes;
+	union {
+		struct {
+			__u64 packets;
+			__u64 bytes;
+		};
+		__u8 data[BPF_LOCAL_STORAGE_MAX_VALUE_SIZE];
+	};
 };
 
 #endif
tools/testing/selftests/bpf/test_netcnt.c +12 -5
index a7b9a69f4fd5..372afccf2d17 100644
@@ -33,11 +33,11 @@ static int bpf_find_map(const char *test, struct bpf_object *obj,
 
 int main(int argc, char **argv)
 {
-	struct percpu_net_cnt *percpu_netcnt;
+	struct percpu_net_cnt *percpu_netcnt = NULL;
 	struct bpf_cgroup_storage_key key;
+	struct net_cnt *netcnt = NULL;
 	int map_fd, percpu_map_fd;
 	int error = EXIT_FAILURE;
-	struct net_cnt netcnt;
 	struct bpf_object *obj;
 	int prog_fd, cgroup_fd;
 	unsigned long packets;
@@ -52,6 +52,12 @@ int main(int argc, char **argv)
 		goto err;
 	}
 
+	netcnt = malloc(sizeof(*netcnt));
+	if (!netcnt) {
+		printf("Not enough memory for non-per-cpu area\n");
+		goto err;
+	}
+
 	if (bpf_prog_load(BPF_PROG, BPF_PROG_TYPE_CGROUP_SKB,
 			  &obj, &prog_fd)) {
 		printf("Failed to load bpf program\n");
@@ -96,7 +102,7 @@ int main(int argc, char **argv)
 		goto err;
 	}
 
-	if (bpf_map_lookup_elem(map_fd, &key, &netcnt)) {
+	if (bpf_map_lookup_elem(map_fd, &key, netcnt)) {
 		printf("Failed to lookup cgroup storage\n");
 		goto err;
 	}
@@ -109,8 +115,8 @@ int main(int argc, char **argv)
 	/* Some packets can be still in per-cpu cache, but not more than
 	 * MAX_PERCPU_PACKETS.
 	 */
-	packets = netcnt.packets;
-	bytes = netcnt.bytes;
+	packets = netcnt->packets;
+	bytes = netcnt->bytes;
 	for (cpu = 0; cpu < nproc; cpu++) {
 		if (percpu_netcnt[cpu].packets > MAX_PERCPU_PACKETS) {
 			printf("Unexpected percpu value: %llu\n",
@@ -141,6 +147,7 @@ int main(int argc, char **argv)
 
 err:
 	cleanup_cgroup_environment();
+	free(netcnt);
 	free(percpu_netcnt);
 
 out:
Signature · 1 line
2.32.0.432.gabb21c7263-goog
Acked-by on v3 1/1
#1 lore raw
Quoted 10 lines · Stanislav Fomichev

Current max cgroup storage value size is 4k (PAGE_SIZE). The other local storages accept up to 64k (BPF_LOCAL_STORAGE_MAX_VALUE_SIZE). Let's align max cgroup value size with the other storages.

For percpu, the max is 32k (PCPU_MIN_UNIT_SIZE) because percpu allocator is not happy about larger values.

netcnt test is extended to exercise those maximum values (non-percpu max size is close to, but not real max).

Quoted 7 lines

v3:
* refine SIZEOF_BPF_LOCAL_STORAGE_ELEM comment (Yonghong Song)
* anonymous struct in percpu_net_cnt & net_cnt (Yonghong Song)
* reorder free (Yonghong Song)

v2:
* cap max_value_size instead of BUILD_BUG_ON (Martin KaFai Lau)

#2 lore raw
In reply to Stanislav Fomichev
Quoted 83 lines · Stanislav Fomichev

Current max cgroup storage value size is 4k (PAGE_SIZE). The other local storages accept up to 64k (BPF_LOCAL_STORAGE_MAX_VALUE_SIZE). Let's align max cgroup value size with the other storages.

For percpu, the max is 32k (PCPU_MIN_UNIT_SIZE) because percpu allocator is not happy about larger values.

netcnt test is extended to exercise those maximum values (non-percpu max size is close to, but not real max).

v3:
* refine SIZEOF_BPF_LOCAL_STORAGE_ELEM comment (Yonghong Song)
* anonymous struct in percpu_net_cnt & net_cnt (Yonghong Song)
* reorder free (Yonghong Song)

v2:
* cap max_value_size instead of BUILD_BUG_ON (Martin KaFai Lau)

Cc Martin KaFai Lau <[email protected]>
Cc Yonghong Song <[email protected]>
---
 kernel/bpf/local_storage.c                  | 11 +++++-
 tools/testing/selftests/bpf/netcnt_common.h | 38 +++++++++++++++++----
 tools/testing/selftests/bpf/test_netcnt.c   | 17 ++++++---
 3 files changed, 53 insertions(+), 13 deletions(-)
kernel/bpf/local_storage.c +10 -1
index 7ed2a14dc0de..035e9e3a7132 100644
@@ -1,6 +1,7 @@
 //SPDX-License-Identifier: GPL-2.0
 #include <linux/bpf-cgroup.h>
 #include <linux/bpf.h>
+#include <linux/bpf_local_storage.h>
 #include <linux/btf.h>
 #include <linux/bug.h>
 #include <linux/filter.h>
@@ -283,9 +284,17 @@ static int cgroup_storage_get_next_key(struct bpf_map *_map, void *key,
 
 static struct bpf_map *cgroup_storage_map_alloc(union bpf_attr *attr)
 {
+       __u32 max_value_size = BPF_LOCAL_STORAGE_MAX_VALUE_SIZE;
        int numa_node = bpf_map_attr_numa_node(attr);
        struct bpf_cgroup_storage_map *map;
 
+       /* percpu is bound by PCPU_MIN_UNIT_SIZE, non-percu
+        * is the same as other local storages.
+        */
+       if (attr->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
+               max_value_size = min_t(__u32, max_value_size,
+                                      PCPU_MIN_UNIT_SIZE);
+
        if (attr->key_size != sizeof(struct bpf_cgroup_storage_key) &&
            attr->key_size != sizeof(__u64))
                return ERR_PTR(-EINVAL);
@@ -293,7 +302,7 @@ static struct bpf_map *cgroup_storage_map_alloc(union bpf_attr *attr)
        if (attr->value_size == 0)
                return ERR_PTR(-EINVAL);
 
-       if (attr->value_size > PAGE_SIZE)
+       if (attr->value_size > max_value_size)
                return ERR_PTR(-E2BIG);
 
        if (attr->map_flags & ~LOCAL_STORAGE_CREATE_FLAG_MASK ||
tools/testing/selftests/bpf/netcnt_common.h +8 -0
index 81084c1c2c23..87f5b97e1932 100644
@@ -6,19 +6,43 @@
 
 #define MAX_PERCPU_PACKETS 32
 
+/* sizeof(struct bpf_local_storage_elem):
+ *
+ * It really is about 128 bytes on x86_64, but allocate more to account for
+ * possible layout changes, different architectures, etc.
+ * The kernel will wrap up to PAGE_SIZE internally anyway.
+ */
+#define SIZEOF_BPF_LOCAL_STORAGE_ELEM          256
+

tools/testing/selftests/bpf/netcnt_common.h line 24

Quoted 10 lines
tools/testing/selftests/bpf/netcnt_common.h +7 -2
+/* Try to estimate kernel's BPF_LOCAL_STORAGE_MAX_VALUE_SIZE: */
+#define BPF_LOCAL_STORAGE_MAX_VALUE_SIZE       (0xFFFF - \
+                                                SIZEOF_BPF_LOCAL_STORAGE_ELEM)
+
+#define PCPU_MIN_UNIT_SIZE                     32768
+
 struct percpu_net_cnt {
-       __u64 packets;
-       __u64 bytes;
+       union {

so you have a struct with a single anonymous union inside, isn't that right? Any problems with just making struct percpu_net_cnt into union percpu_net_cnt?

Quoted 10 lines
tools/testing/selftests/bpf/netcnt_common.h +5 -3
+               struct {
+                       __u64 packets;
+                       __u64 bytes;
 
-       __u64 prev_ts;
+                       __u64 prev_ts;
 
-       __u64 prev_packets;
-       __u64 prev_bytes;
+                       __u64 prev_packets;

tools/testing/selftests/bpf/netcnt_common.h line 39

Quoted 10 lines
tools/testing/selftests/bpf/netcnt_common.h +5 -2
+                       __u64 prev_bytes;
+               };
+               __u8 data[PCPU_MIN_UNIT_SIZE];
+       };
 };
 
 struct net_cnt {
-       __u64 packets;
-       __u64 bytes;
+       union {

similarly here

Quoted 28 lines
tools/testing/selftests/bpf/netcnt_common.h +6 -0
+               struct {
+                       __u64 packets;
+                       __u64 bytes;
+               };
+               __u8 data[BPF_LOCAL_STORAGE_MAX_VALUE_SIZE];
+       };
 };
 
 #endif
tools/testing/selftests/bpf/test_netcnt.c +2 -2
index a7b9a69f4fd5..372afccf2d17 100644
@@ -33,11 +33,11 @@ static int bpf_find_map(const char *test, struct bpf_object *obj,
 
 int main(int argc, char **argv)
 {
-       struct percpu_net_cnt *percpu_netcnt;
+       struct percpu_net_cnt *percpu_netcnt = NULL;
        struct bpf_cgroup_storage_key key;
+       struct net_cnt *netcnt = NULL;
        int map_fd, percpu_map_fd;
        int error = EXIT_FAILURE;
-       struct net_cnt netcnt;
        struct bpf_object *obj;
        int prog_fd, cgroup_fd;
        unsigned long packets;

tools/testing/selftests/bpf/test_netcnt.c int main(int argc, char **argv) · line 55

Quoted 4 lines
tools/testing/selftests/bpf/test_netcnt.c +1 -0
@@ -52,6 +52,12 @@ int main(int argc, char **argv)
                goto err;
        }
 
+       netcnt = malloc(sizeof(*netcnt));

curious, was it too big to be just allocated on the stack? Isn't the thread stack size much bigger than 64KB (at least by default)?

Quoted 39 lines
tools/testing/selftests/bpf/test_netcnt.c +9 -4
+       if (!netcnt) {
+               printf("Not enough memory for non-per-cpu area\n");
+               goto err;
+       }
+
        if (bpf_prog_load(BPF_PROG, BPF_PROG_TYPE_CGROUP_SKB,
                          &obj, &prog_fd)) {
                printf("Failed to load bpf program\n");
@@ -96,7 +102,7 @@ int main(int argc, char **argv)
                goto err;
        }
 
-       if (bpf_map_lookup_elem(map_fd, &key, &netcnt)) {
+       if (bpf_map_lookup_elem(map_fd, &key, netcnt)) {
                printf("Failed to lookup cgroup storage\n");
                goto err;
        }
@@ -109,8 +115,8 @@ int main(int argc, char **argv)
        /* Some packets can be still in per-cpu cache, but not more than
         * MAX_PERCPU_PACKETS.
         */
-       packets = netcnt.packets;
-       bytes = netcnt.bytes;
+       packets = netcnt->packets;
+       bytes = netcnt->bytes;
        for (cpu = 0; cpu < nproc; cpu++) {
                if (percpu_netcnt[cpu].packets > MAX_PERCPU_PACKETS) {
                        printf("Unexpected percpu value: %llu\n",
@@ -141,6 +147,7 @@ int main(int argc, char **argv)
 
 err:
        cleanup_cgroup_environment();
+       free(netcnt);
        free(percpu_netcnt);
 
 out:
--

2.32.0.432.gabb21c7263-goog

#3 lore raw
Quoted 93 lines · Andrii Nakryiko
Quoted 3 lines · Stanislav Fomichev

Current max cgroup storage value size is 4k (PAGE_SIZE). The other local storages accept up to 64k (BPF_LOCAL_STORAGE_MAX_VALUE_SIZE). Let's

align

kernel/bpf/local_storage.c static int cgroup_storage_get_next_key(struct

Quoted 38 lines

max cgroup value size with the other storages.

For percpu, the max is 32k (PCPU_MIN_UNIT_SIZE) because percpu allocator is not happy about larger values.

netcnt test is extended to exercise those maximum values (non-percpu max size is close to, but not real max).

v3:
* refine SIZEOF_BPF_LOCAL_STORAGE_ELEM comment (Yonghong Song)
* anonymous struct in percpu_net_cnt & net_cnt (Yonghong Song)
* reorder free (Yonghong Song)

v2:
* cap max_value_size instead of BUILD_BUG_ON (Martin KaFai Lau)

Cc Martin KaFai Lau <[email protected]>
Cc Yonghong Song <[email protected]>
---
 kernel/bpf/local_storage.c                  | 11 +++++-
 tools/testing/selftests/bpf/netcnt_common.h | 38 +++++++++++++++++----
 tools/testing/selftests/bpf/test_netcnt.c   | 17 ++++++---
 3 files changed, 53 insertions(+), 13 deletions(-)
kernel/bpf/local_storage.c +1 -0
index 7ed2a14dc0de..035e9e3a7132 100644
@@ -1,6 +1,7 @@
 //SPDX-License-Identifier: GPL-2.0
 #include <linux/bpf-cgroup.h>
 #include <linux/bpf.h>
+#include <linux/bpf_local_storage.h>
 #include <linux/btf.h>
 #include <linux/bug.h>
 #include <linux/filter.h>
@@ -283,9 +284,17 @@ static int cgroup_storage_get_next_key(struct  

bpf_map *_map, void *key,

kernel/bpf/local_storage.c static struct bpf_map

Quoted 18 lines
kernel/bpf/local_storage.c +8 -0
 static struct bpf_map *cgroup_storage_map_alloc(union bpf_attr *attr)
 {
+       __u32 max_value_size = BPF_LOCAL_STORAGE_MAX_VALUE_SIZE;
        int numa_node = bpf_map_attr_numa_node(attr);
        struct bpf_cgroup_storage_map *map;
 
+       /* percpu is bound by PCPU_MIN_UNIT_SIZE, non-percu
+        * is the same as other local storages.
+        */
+       if (attr->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
+               max_value_size = min_t(__u32, max_value_size,
+                                      PCPU_MIN_UNIT_SIZE);
+
        if (attr->key_size != sizeof(struct bpf_cgroup_storage_key) &&
            attr->key_size != sizeof(__u64))
                return ERR_PTR(-EINVAL);
@@ -293,7 +302,7 @@ static struct bpf_map  

*cgroup_storage_map_alloc(union bpf_attr *attr)

Review

Quoted 9 lines
kernel/bpf/local_storage.c +1 -1
        if (attr->value_size == 0)
                return ERR_PTR(-EINVAL);
 
-       if (attr->value_size > PAGE_SIZE)
+       if (attr->value_size > max_value_size)
                return ERR_PTR(-E2BIG);
 
        if (attr->map_flags & ~LOCAL_STORAGE_CREATE_FLAG_MASK ||
(quoted diff) +0 -0

b/tools/testing/selftests/bpf/netcnt_common.h

tools/testing/selftests/bpf/netcnt_common.h line 11

Quoted 10 lines

index 81084c1c2c23..87f5b97e1932 100644

tools/testing/selftests/bpf/netcnt_common.h +3 -0
@@ -6,19 +6,43 @@
 
 #define MAX_PERCPU_PACKETS 32
 
+/* sizeof(struct bpf_local_storage_elem):
+ *
+ * It really is about 128 bytes on x86_64, but allocate more to  

account for

tools/testing/selftests/bpf/netcnt_common.h line 20

Quoted 8 lines
tools/testing/selftests/bpf/netcnt_common.h +8 -0
+ * possible layout changes, different architectures, etc.
+ * The kernel will wrap up to PAGE_SIZE internally anyway.
+ */
+#define SIZEOF_BPF_LOCAL_STORAGE_ELEM          256
+
+/* Try to estimate kernel's BPF_LOCAL_STORAGE_MAX_VALUE_SIZE: */
+#define BPF_LOCAL_STORAGE_MAX_VALUE_SIZE       (0xFFFF - \
+                                                 

SIZEOF_BPF_LOCAL_STORAGE_ELEM)

Quoted 11 lines

tools/testing/selftests/bpf/netcnt_common.h line 24

Quoted 7 lines
tools/testing/selftests/bpf/netcnt_common.h +4 -2
+
+#define PCPU_MIN_UNIT_SIZE                     32768
+
 struct percpu_net_cnt {
-       __u64 packets;
-       __u64 bytes;
+       union {

so you have a struct with a single anonymous union inside, isn't that right? Any problems with just making struct percpu_net_cnt into union percpu_net_cnt?

We'd have to s/struct/union/ everywhere in this case, not sure we want to add more churn? Seemed easier to do anonymous union+struct.

Quoted 56 lines
Quoted 10 lines
tools/testing/selftests/bpf/netcnt_common.h +5 -3
+               struct {
+                       __u64 packets;
+                       __u64 bytes;
 
-       __u64 prev_ts;
+                       __u64 prev_ts;
 
-       __u64 prev_packets;
-       __u64 prev_bytes;
+                       __u64 prev_packets;

tools/testing/selftests/bpf/netcnt_common.h line 39

Quoted 10 lines
tools/testing/selftests/bpf/netcnt_common.h +5 -2
+                       __u64 prev_bytes;
+               };
+               __u8 data[PCPU_MIN_UNIT_SIZE];
+       };
 };
 
 struct net_cnt {
-       __u64 packets;
-       __u64 bytes;
+       union {

similarly here

Review

Quoted 10 lines
tools/testing/selftests/bpf/netcnt_common.h +6 -0
+               struct {
+                       __u64 packets;
+                       __u64 bytes;
+               };
+               __u8 data[BPF_LOCAL_STORAGE_MAX_VALUE_SIZE];
+       };
 };
 
 #endif
(quoted diff) +0 -0

b/tools/testing/selftests/bpf/test_netcnt.c

tools/testing/selftests/bpf/test_netcnt.c static int bpf_find_map(const char *test, struct

Quoted 4 lines

index a7b9a69f4fd5..372afccf2d17 100644

tools/testing/selftests/bpf/test_netcnt.c +0 -0
@@ -33,11 +33,11 @@ static int bpf_find_map(const char *test, struct  

bpf_object *obj,

Quoted 17 lines
tools/testing/selftests/bpf/test_netcnt.c +2 -2
 int main(int argc, char **argv)
 {
-       struct percpu_net_cnt *percpu_netcnt;
+       struct percpu_net_cnt *percpu_netcnt = NULL;
        struct bpf_cgroup_storage_key key;
+       struct net_cnt *netcnt = NULL;
        int map_fd, percpu_map_fd;
        int error = EXIT_FAILURE;
-       struct net_cnt netcnt;
        struct bpf_object *obj;
        int prog_fd, cgroup_fd;
        unsigned long packets;
@@ -52,6 +52,12 @@ int main(int argc, char **argv)
                goto err;
        }
 
Quoted 4 lines

tools/testing/selftests/bpf/test_netcnt.c int main(int argc, char **argv) · line 55

Quoted 1 line
tools/testing/selftests/bpf/test_netcnt.c +1 -0
+       netcnt = malloc(sizeof(*netcnt));

curious, was it too big to be just allocated on the stack? Isn't the thread stack size much bigger than 64KB (at least by default)?

I haven't tried really, I just moved it to malloc because it crossed some unconscious boundary for the 'stuff I allocate on the stack'. I can try it out if you prefer to keep it on the stack, let me know.

#4 lore raw
Quoted 106 lines · On Tue, Jul 27, 2021 at 1:47 PM <[email protected]> wrote:
Quoted 93 lines · Andrii Nakryiko
Quoted 3 lines · Stanislav Fomichev

Current max cgroup storage value size is 4k (PAGE_SIZE). The other local storages accept up to 64k (BPF_LOCAL_STORAGE_MAX_VALUE_SIZE). Let's

align

kernel/bpf/local_storage.c static int cgroup_storage_get_next_key(struct

Quoted 38 lines

max cgroup value size with the other storages.

For percpu, the max is 32k (PCPU_MIN_UNIT_SIZE) because percpu allocator is not happy about larger values.

netcnt test is extended to exercise those maximum values (non-percpu max size is close to, but not real max).

v3:
* refine SIZEOF_BPF_LOCAL_STORAGE_ELEM comment (Yonghong Song)
* anonymous struct in percpu_net_cnt & net_cnt (Yonghong Song)
* reorder free (Yonghong Song)

v2:
* cap max_value_size instead of BUILD_BUG_ON (Martin KaFai Lau)

Cc Martin KaFai Lau <[email protected]>
Cc Yonghong Song <[email protected]>
---
 kernel/bpf/local_storage.c                  | 11 +++++-
 tools/testing/selftests/bpf/netcnt_common.h | 38 +++++++++++++++++----
 tools/testing/selftests/bpf/test_netcnt.c   | 17 ++++++---
 3 files changed, 53 insertions(+), 13 deletions(-)
kernel/bpf/local_storage.c +1 -0
index 7ed2a14dc0de..035e9e3a7132 100644
@@ -1,6 +1,7 @@
 //SPDX-License-Identifier: GPL-2.0
 #include <linux/bpf-cgroup.h>
 #include <linux/bpf.h>
+#include <linux/bpf_local_storage.h>
 #include <linux/btf.h>
 #include <linux/bug.h>
 #include <linux/filter.h>
@@ -283,9 +284,17 @@ static int cgroup_storage_get_next_key(struct

bpf_map *_map, void *key,

kernel/bpf/local_storage.c static struct bpf_map

Quoted 18 lines
kernel/bpf/local_storage.c +8 -0
 static struct bpf_map *cgroup_storage_map_alloc(union bpf_attr *attr)
 {
+       __u32 max_value_size = BPF_LOCAL_STORAGE_MAX_VALUE_SIZE;
        int numa_node = bpf_map_attr_numa_node(attr);
        struct bpf_cgroup_storage_map *map;
 
+       /* percpu is bound by PCPU_MIN_UNIT_SIZE, non-percu
+        * is the same as other local storages.
+        */
+       if (attr->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
+               max_value_size = min_t(__u32, max_value_size,
+                                      PCPU_MIN_UNIT_SIZE);
+
        if (attr->key_size != sizeof(struct bpf_cgroup_storage_key) &&
            attr->key_size != sizeof(__u64))
                return ERR_PTR(-EINVAL);
@@ -293,7 +302,7 @@ static struct bpf_map

*cgroup_storage_map_alloc(union bpf_attr *attr)

Review

Quoted 9 lines
kernel/bpf/local_storage.c +1 -1
        if (attr->value_size == 0)
                return ERR_PTR(-EINVAL);
 
-       if (attr->value_size > PAGE_SIZE)
+       if (attr->value_size > max_value_size)
                return ERR_PTR(-E2BIG);
 
        if (attr->map_flags & ~LOCAL_STORAGE_CREATE_FLAG_MASK ||
(quoted diff) +0 -0

b/tools/testing/selftests/bpf/netcnt_common.h

tools/testing/selftests/bpf/netcnt_common.h line 11

Quoted 10 lines

index 81084c1c2c23..87f5b97e1932 100644

tools/testing/selftests/bpf/netcnt_common.h +3 -0
@@ -6,19 +6,43 @@
 
 #define MAX_PERCPU_PACKETS 32
 
+/* sizeof(struct bpf_local_storage_elem):
+ *
+ * It really is about 128 bytes on x86_64, but allocate more to

account for

tools/testing/selftests/bpf/netcnt_common.h line 20

Quoted 8 lines
tools/testing/selftests/bpf/netcnt_common.h +8 -0
+ * possible layout changes, different architectures, etc.
+ * The kernel will wrap up to PAGE_SIZE internally anyway.
+ */
+#define SIZEOF_BPF_LOCAL_STORAGE_ELEM          256
+
+/* Try to estimate kernel's BPF_LOCAL_STORAGE_MAX_VALUE_SIZE: */
+#define BPF_LOCAL_STORAGE_MAX_VALUE_SIZE       (0xFFFF - \
+

SIZEOF_BPF_LOCAL_STORAGE_ELEM)

Quoted 11 lines

tools/testing/selftests/bpf/netcnt_common.h line 24

Quoted 7 lines
tools/testing/selftests/bpf/netcnt_common.h +4 -2
+
+#define PCPU_MIN_UNIT_SIZE                     32768
+
 struct percpu_net_cnt {
-       __u64 packets;
-       __u64 bytes;
+       union {

so you have a struct with a single anonymous union inside, isn't that right? Any problems with just making struct percpu_net_cnt into union percpu_net_cnt?

Quoted 2 lines

We'd have to s/struct/union/ everywhere in this case, not sure we want to add more churn? Seemed easier to do anonymous union+struct.

4 occurrences for net_cnt and another 4 for percpu_net_cnt, not much churn (and all pretty localized). But I honestly don't care, just wanted to note that you don't need this extra nesting.

Quoted 57 lines
Quoted 57 lines
Quoted 11 lines
tools/testing/selftests/bpf/netcnt_common.h +5 -3
+               struct {
+                       __u64 packets;
+                       __u64 bytes;
 
-       __u64 prev_ts;
+                       __u64 prev_ts;
 
-       __u64 prev_packets;
-       __u64 prev_bytes;
+                       __u64 prev_packets;

tools/testing/selftests/bpf/netcnt_common.h line 39

Quoted 10 lines
tools/testing/selftests/bpf/netcnt_common.h +5 -2
+                       __u64 prev_bytes;
+               };
+               __u8 data[PCPU_MIN_UNIT_SIZE];
+       };
 };
 
 struct net_cnt {
-       __u64 packets;
-       __u64 bytes;
+       union {

similarly here

Review

Quoted 10 lines
tools/testing/selftests/bpf/netcnt_common.h +6 -0
+               struct {
+                       __u64 packets;
+                       __u64 bytes;
+               };
+               __u8 data[BPF_LOCAL_STORAGE_MAX_VALUE_SIZE];
+       };
 };
 
 #endif
(quoted diff) +0 -0

b/tools/testing/selftests/bpf/test_netcnt.c

tools/testing/selftests/bpf/test_netcnt.c static int bpf_find_map(const char *test, struct

Quoted 4 lines

index a7b9a69f4fd5..372afccf2d17 100644

tools/testing/selftests/bpf/test_netcnt.c +0 -0
@@ -33,11 +33,11 @@ static int bpf_find_map(const char *test, struct

bpf_object *obj,

Quoted 17 lines
tools/testing/selftests/bpf/test_netcnt.c +2 -2
 int main(int argc, char **argv)
 {
-       struct percpu_net_cnt *percpu_netcnt;
+       struct percpu_net_cnt *percpu_netcnt = NULL;
        struct bpf_cgroup_storage_key key;
+       struct net_cnt *netcnt = NULL;
        int map_fd, percpu_map_fd;
        int error = EXIT_FAILURE;
-       struct net_cnt netcnt;
        struct bpf_object *obj;
        int prog_fd, cgroup_fd;
        unsigned long packets;
@@ -52,6 +52,12 @@ int main(int argc, char **argv)
                goto err;
        }
 
Quoted 7 lines
Quoted 4 lines

tools/testing/selftests/bpf/test_netcnt.c int main(int argc, char **argv) · line 55

Quoted 1 line
tools/testing/selftests/bpf/test_netcnt.c +1 -0
+       netcnt = malloc(sizeof(*netcnt));

curious, was it too big to be just allocated on the stack? Isn't the thread stack size much bigger than 64KB (at least by default)?

I haven't tried really, I just moved it to malloc because it crossed some unconscious boundary for the 'stuff I allocate on the stack'. I can try it out if you prefer to keep it on the stack, let me know.

Yeah, if it can stay on the stack. Less thinking about freeing memory.

#5 lore raw
Quoted 112 lines · Andrii Nakryiko
Quoted 3 lines · On Tue, Jul 27, 2021 at 1:47 PM <[email protected]> wrote:
Quoted 1 line · Andrii Nakryiko

On Mon, Jul 26, 2021 at 4:00 PM Stanislav Fomichev <[email protected]>

Quoted 2 lines · wrote:
Quoted 2 lines
Quoted 2 lines

Current max cgroup storage value size is 4k (PAGE_SIZE). The other

local

Quoted 18 lines
Quoted 18 lines
Quoted 1 line

storages accept up to 64k (BPF_LOCAL_STORAGE_MAX_VALUE_SIZE). Let's

align

Quoted 16 lines

max cgroup value size with the other storages.

For percpu, the max is 32k (PCPU_MIN_UNIT_SIZE) because percpu allocator is not happy about larger values.

netcnt test is extended to exercise those maximum values (non-percpu max size is close to, but not real max).

v3:
* refine SIZEOF_BPF_LOCAL_STORAGE_ELEM comment (Yonghong Song)
* anonymous struct in percpu_net_cnt & net_cnt (Yonghong Song)
* reorder free (Yonghong Song)

v2:
* cap max_value_size instead of BUILD_BUG_ON (Martin KaFai Lau)

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Cc Martin KaFai Lau <[email protected]>
Cc Yonghong Song <[email protected]>
---
 kernel/bpf/local_storage.c                  | 11 +++++-
 tools/testing/selftests/bpf/netcnt_common.h | 38  

+++++++++++++++++----

Quoted 18 lines
Quoted 18 lines

kernel/bpf/local_storage.c static int cgroup_storage_get_next_key(struct

Quoted 16 lines
 tools/testing/selftests/bpf/test_netcnt.c   | 17 ++++++---
 3 files changed, 53 insertions(+), 13 deletions(-)
kernel/bpf/local_storage.c +1 -0
index 7ed2a14dc0de..035e9e3a7132 100644
@@ -1,6 +1,7 @@
 //SPDX-License-Identifier: GPL-2.0
 #include <linux/bpf-cgroup.h>
 #include <linux/bpf.h>
+#include <linux/bpf_local_storage.h>
 #include <linux/btf.h>
 #include <linux/bug.h>
 #include <linux/filter.h>
@@ -283,9 +284,17 @@ static int cgroup_storage_get_next_key(struct

bpf_map *_map, void *key,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

static struct bpf_map *cgroup_storage_map_alloc(union bpf_attr

*attr)

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

{

+       __u32 max_value_size = BPF_LOCAL_STORAGE_MAX_VALUE_SIZE;
        int numa_node = bpf_map_attr_numa_node(attr);
        struct bpf_cgroup_storage_map *map;
Quoted 8 lines
Quoted 8 lines

kernel/bpf/local_storage.c static int cgroup_storage_get_next_key(struct bpf_map *_map, void *key,

Quoted 8 lines
kernel/bpf/local_storage.c +7 -0
+       /* percpu is bound by PCPU_MIN_UNIT_SIZE, non-percu
+        * is the same as other local storages.
+        */
+       if (attr->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
+               max_value_size = min_t(__u32, max_value_size,
+                                      PCPU_MIN_UNIT_SIZE);
+
        if (attr->key_size != sizeof(struct bpf_cgroup_storage_key)  

&&

Quoted 45 lines
Quoted 45 lines

kernel/bpf/local_storage.c static struct bpf_map

Quoted 3 lines
            attr->key_size != sizeof(__u64))
                return ERR_PTR(-EINVAL);
kernel/bpf/local_storage.c +0 -0
@@ -293,7 +302,7 @@ static struct bpf_map

*cgroup_storage_map_alloc(union bpf_attr *attr)

Review

Quoted 9 lines
kernel/bpf/local_storage.c +1 -1
        if (attr->value_size == 0)
                return ERR_PTR(-EINVAL);
 
-       if (attr->value_size > PAGE_SIZE)
+       if (attr->value_size > max_value_size)
                return ERR_PTR(-E2BIG);
 
        if (attr->map_flags & ~LOCAL_STORAGE_CREATE_FLAG_MASK ||
(quoted diff) +0 -0

b/tools/testing/selftests/bpf/netcnt_common.h

tools/testing/selftests/bpf/netcnt_common.h line 11

Quoted 10 lines

index 81084c1c2c23..87f5b97e1932 100644

tools/testing/selftests/bpf/netcnt_common.h +3 -0
@@ -6,19 +6,43 @@
 
 #define MAX_PERCPU_PACKETS 32
 
+/* sizeof(struct bpf_local_storage_elem):
+ *
+ * It really is about 128 bytes on x86_64, but allocate more to

account for

tools/testing/selftests/bpf/netcnt_common.h line 20

Quoted 8 lines
tools/testing/selftests/bpf/netcnt_common.h +8 -0
+ * possible layout changes, different architectures, etc.
+ * The kernel will wrap up to PAGE_SIZE internally anyway.
+ */
+#define SIZEOF_BPF_LOCAL_STORAGE_ELEM          256
+
+/* Try to estimate kernel's BPF_LOCAL_STORAGE_MAX_VALUE_SIZE: */
+#define BPF_LOCAL_STORAGE_MAX_VALUE_SIZE       (0xFFFF - \
+

SIZEOF_BPF_LOCAL_STORAGE_ELEM)

tools/testing/selftests/bpf/netcnt_common.h line 24

Quoted 7 lines
tools/testing/selftests/bpf/netcnt_common.h +4 -2
+
+#define PCPU_MIN_UNIT_SIZE                     32768
+
 struct percpu_net_cnt {
-       __u64 packets;
-       __u64 bytes;
+       union {

so you have a struct with a single anonymous union inside, isn't that right? Any problems with just making struct percpu_net_cnt into union percpu_net_cnt?

Quoted 6 lines
Quoted 2 lines

We'd have to s/struct/union/ everywhere in this case, not sure we want to add more churn? Seemed easier to do anonymous union+struct.

4 occurrences for net_cnt and another 4 for percpu_net_cnt, not much churn (and all pretty localized). But I honestly don't care, just wanted to note that you don't need this extra nesting.

I might do it since I'm doing another respin anyway.

Quoted 58 lines
Quoted 58 lines
Quoted 57 lines
Quoted 10 lines
tools/testing/selftests/bpf/netcnt_common.h +5 -3
+               struct {
+                       __u64 packets;
+                       __u64 bytes;
 
-       __u64 prev_ts;
+                       __u64 prev_ts;
 
-       __u64 prev_packets;
-       __u64 prev_bytes;
+                       __u64 prev_packets;

tools/testing/selftests/bpf/netcnt_common.h line 39

Quoted 10 lines
tools/testing/selftests/bpf/netcnt_common.h +5 -2
+                       __u64 prev_bytes;
+               };
+               __u8 data[PCPU_MIN_UNIT_SIZE];
+       };
 };
 
 struct net_cnt {
-       __u64 packets;
-       __u64 bytes;
+       union {

similarly here

Review

Quoted 10 lines
tools/testing/selftests/bpf/netcnt_common.h +6 -0
+               struct {
+                       __u64 packets;
+                       __u64 bytes;
+               };
+               __u8 data[BPF_LOCAL_STORAGE_MAX_VALUE_SIZE];
+       };
 };
 
 #endif
(quoted diff) +0 -0

b/tools/testing/selftests/bpf/test_netcnt.c

tools/testing/selftests/bpf/test_netcnt.c static int bpf_find_map(const char *test, struct

Quoted 4 lines

index a7b9a69f4fd5..372afccf2d17 100644

tools/testing/selftests/bpf/test_netcnt.c +0 -0
@@ -33,11 +33,11 @@ static int bpf_find_map(const char *test, struct

bpf_object *obj,

Quoted 18 lines
tools/testing/selftests/bpf/test_netcnt.c +3 -2
 int main(int argc, char **argv)
 {
-       struct percpu_net_cnt *percpu_netcnt;
+       struct percpu_net_cnt *percpu_netcnt = NULL;
        struct bpf_cgroup_storage_key key;
+       struct net_cnt *netcnt = NULL;
        int map_fd, percpu_map_fd;
        int error = EXIT_FAILURE;
-       struct net_cnt netcnt;
        struct bpf_object *obj;
        int prog_fd, cgroup_fd;
        unsigned long packets;
@@ -52,6 +52,12 @@ int main(int argc, char **argv)
                goto err;
        }
 
+       netcnt = malloc(sizeof(*netcnt));
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

curious, was it too big to be just allocated on the stack? Isn't the thread stack size much bigger than 64KB (at least by default)?

I haven't tried really, I just moved it to malloc because it crossed some unconscious boundary for the 'stuff I allocate on the stack'. I can try it out if you prefer to keep it on the stack, let me know.

Yeah, if it can stay on the stack. Less thinking about freeing memory.

Ack, seems to be working, will resend shortly..