bpf, net: bpf_local_storage memory usage
authorYafang Shao <laoar.shao@gmail.com>
Sun, 5 Mar 2023 12:46:11 +0000 (12:46 +0000)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 7 Mar 2023 17:33:43 +0000 (09:33 -0800)
A new helper is introduced into bpf_local_storage map to calculate the
memory usage. This helper is also used by other maps like
bpf_cgrp_storage, bpf_inode_storage, bpf_task_storage and etc.

Note that currently the dynamically allocated storage elements are not
counted in the usage, since it will take extra runtime overhead in the
elements update or delete path. So let's put it aside now, and implement
it in the future when someone really need it.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
Link: https://lore.kernel.org/r/20230305124615.12358-15-laoar.shao@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
include/linux/bpf_local_storage.h
kernel/bpf/bpf_cgrp_storage.c
kernel/bpf/bpf_inode_storage.c
kernel/bpf/bpf_local_storage.c
kernel/bpf/bpf_task_storage.c
net/core/bpf_sk_storage.c

index 6d37a40cd90e8d811ef1f50a337f16c6b6a7dc87..d934248b8e81345dbf89000d891aa028a9ff73c8 100644 (file)
@@ -164,5 +164,6 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
                         void *value, u64 map_flags, gfp_t gfp_flags);
 
 void bpf_local_storage_free_rcu(struct rcu_head *rcu);
+u64 bpf_local_storage_map_mem_usage(const struct bpf_map *map);
 
 #endif /* _BPF_LOCAL_STORAGE_H */
index 6cdf6d9ed91df912aeb7036b9e577129225f471e..9ae07aedaf2332db250943ebe91665425e0cf560 100644 (file)
@@ -221,6 +221,7 @@ const struct bpf_map_ops cgrp_storage_map_ops = {
        .map_update_elem = bpf_cgrp_storage_update_elem,
        .map_delete_elem = bpf_cgrp_storage_delete_elem,
        .map_check_btf = bpf_local_storage_map_check_btf,
+       .map_mem_usage = bpf_local_storage_map_mem_usage,
        .map_btf_id = &bpf_local_storage_map_btf_id[0],
        .map_owner_storage_ptr = cgroup_storage_ptr,
 };
index 05f4c66c9089f69a40365651d5d490bcb40313bf..43e2619c8167d0d331501c0cbb1bc82d02fa4c5a 100644 (file)
@@ -223,6 +223,7 @@ const struct bpf_map_ops inode_storage_map_ops = {
        .map_update_elem = bpf_fd_inode_storage_update_elem,
        .map_delete_elem = bpf_fd_inode_storage_delete_elem,
        .map_check_btf = bpf_local_storage_map_check_btf,
+       .map_mem_usage = bpf_local_storage_map_mem_usage,
        .map_btf_id = &bpf_local_storage_map_btf_id[0],
        .map_owner_storage_ptr = inode_storage_ptr,
 };
index 3d320393a12c9bc4d6a231c9e23904a5f75b0f31..d3ba3f2db6405116f15c4dd6d387c5dc63afa4a1 100644 (file)
@@ -685,6 +685,16 @@ bool bpf_local_storage_unlink_nolock(struct bpf_local_storage *local_storage)
        return free_storage;
 }
 
+u64 bpf_local_storage_map_mem_usage(const struct bpf_map *map)
+{
+       struct bpf_local_storage_map *smap = (struct bpf_local_storage_map *)map;
+       u64 usage = sizeof(*smap);
+
+       /* The dynamically callocated selems are not counted currently. */
+       usage += sizeof(*smap->buckets) * (1ULL << smap->bucket_log);
+       return usage;
+}
+
 struct bpf_map *
 bpf_local_storage_map_alloc(union bpf_attr *attr,
                            struct bpf_local_storage_cache *cache)
index 1e486055a523dd206e306ca4775109b2d5b377d4..20f942229f3c9018cef0233cce8ce986688db003 100644 (file)
@@ -335,6 +335,7 @@ const struct bpf_map_ops task_storage_map_ops = {
        .map_update_elem = bpf_pid_task_storage_update_elem,
        .map_delete_elem = bpf_pid_task_storage_delete_elem,
        .map_check_btf = bpf_local_storage_map_check_btf,
+       .map_mem_usage = bpf_local_storage_map_mem_usage,
        .map_btf_id = &bpf_local_storage_map_btf_id[0],
        .map_owner_storage_ptr = task_storage_ptr,
 };
index bb378c33f542c219df099ce25d40209c9ce4d740..7a36353dbc22c641376b18676cf828443d520cf4 100644 (file)
@@ -324,6 +324,7 @@ const struct bpf_map_ops sk_storage_map_ops = {
        .map_local_storage_charge = bpf_sk_storage_charge,
        .map_local_storage_uncharge = bpf_sk_storage_uncharge,
        .map_owner_storage_ptr = bpf_sk_storage_ptr,
+       .map_mem_usage = bpf_local_storage_map_mem_usage,
 };
 
 const struct bpf_func_proto bpf_sk_storage_get_proto = {