Skip to content

Commit

Permalink
bpf: Increase supported cgroup storage value size
Browse files Browse the repository at this point in the history
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).

v4:
* remove inner union (Andrii Nakryiko)
* keep net_cnt on the stack (Andrii Nakryiko)

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)

Signed-off-by: Stanislav Fomichev <[email protected]>
Signed-off-by: Andrii Nakryiko <[email protected]>
Acked-by: Martin KaFai Lau <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
  • Loading branch information
fomichev authored and anakryiko committed Jul 27, 2021
1 parent 043c5bb commit 33b57e0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
11 changes: 10 additions & 1 deletion kernel/bpf/local_storage.c
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -283,17 +284,25 @@ 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);

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 ||
Expand Down
38 changes: 29 additions & 9 deletions tools/testing/selftests/bpf/netcnt_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,39 @@

#define MAX_PERCPU_PACKETS 32

struct percpu_net_cnt {
__u64 packets;
__u64 bytes;
/* 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

__u64 prev_ts;
/* Try to estimate kernel's BPF_LOCAL_STORAGE_MAX_VALUE_SIZE: */
#define BPF_LOCAL_STORAGE_MAX_VALUE_SIZE (0xFFFF - \
SIZEOF_BPF_LOCAL_STORAGE_ELEM)

__u64 prev_packets;
__u64 prev_bytes;
#define PCPU_MIN_UNIT_SIZE 32768

union percpu_net_cnt {
struct {
__u64 packets;
__u64 bytes;

__u64 prev_ts;

__u64 prev_packets;
__u64 prev_bytes;
};
__u8 data[PCPU_MIN_UNIT_SIZE];
};

struct net_cnt {
__u64 packets;
__u64 bytes;
union net_cnt {
struct {
__u64 packets;
__u64 bytes;
};
__u8 data[BPF_LOCAL_STORAGE_MAX_VALUE_SIZE];
};

#endif
8 changes: 4 additions & 4 deletions tools/testing/selftests/bpf/progs/netcnt_prog.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE);
__type(key, struct bpf_cgroup_storage_key);
__type(value, struct percpu_net_cnt);
__type(value, union percpu_net_cnt);
} percpu_netcnt SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
__type(key, struct bpf_cgroup_storage_key);
__type(value, struct net_cnt);
__type(value, union net_cnt);
} netcnt SEC(".maps");

SEC("cgroup/skb")
int bpf_nextcnt(struct __sk_buff *skb)
{
struct percpu_net_cnt *percpu_cnt;
union percpu_net_cnt *percpu_cnt;
char fmt[] = "%d %llu %llu\n";
struct net_cnt *cnt;
union net_cnt *cnt;
__u64 ts, dt;
int ret;

Expand Down
4 changes: 2 additions & 2 deletions tools/testing/selftests/bpf/test_netcnt.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ static int bpf_find_map(const char *test, struct bpf_object *obj,

int main(int argc, char **argv)
{
struct percpu_net_cnt *percpu_netcnt;
union percpu_net_cnt *percpu_netcnt;
struct bpf_cgroup_storage_key key;
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;
union net_cnt netcnt;
unsigned long bytes;
int cpu, nproc;
__u32 prog_cnt;
Expand Down

0 comments on commit 33b57e0

Please sign in to comment.