bpf: add new map ops ->map_mem_usage
authorYafang Shao <laoar.shao@gmail.com>
Sun, 5 Mar 2023 12:45:58 +0000 (12:45 +0000)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 7 Mar 2023 17:33:41 +0000 (09:33 -0800)
Add a new map ops ->map_mem_usage to print the memory usage of a
bpf map.

This is a preparation for the followup change.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
Link: https://lore.kernel.org/r/20230305124615.12358-2-laoar.shao@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
include/linux/bpf.h
kernel/bpf/syscall.c

index d345680..9059520 100644 (file)
@@ -161,6 +161,8 @@ struct bpf_map_ops {
                                     bpf_callback_t callback_fn,
                                     void *callback_ctx, u64 flags);
 
+       u64 (*map_mem_usage)(const struct bpf_map *map);
+
        /* BTF id of struct allocated by map_alloc */
        int *map_btf_id;
 
index ede5f98..7c96e68 100644 (file)
@@ -771,16 +771,15 @@ static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
 }
 
 #ifdef CONFIG_PROC_FS
-/* Provides an approximation of the map's memory footprint.
- * Used only to provide a backward compatibility and display
- * a reasonable "memlock" info.
- */
-static unsigned long bpf_map_memory_footprint(const struct bpf_map *map)
+/* Show the memory usage of a bpf map */
+static u64 bpf_map_memory_usage(const struct bpf_map *map)
 {
        unsigned long size;
 
-       size = round_up(map->key_size + bpf_map_value_size(map), 8);
+       if (map->ops->map_mem_usage)
+               return map->ops->map_mem_usage(map);
 
+       size = round_up(map->key_size + bpf_map_value_size(map), 8);
        return round_up(map->max_entries * size, PAGE_SIZE);
 }
 
@@ -803,7 +802,7 @@ static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
                   "max_entries:\t%u\n"
                   "map_flags:\t%#x\n"
                   "map_extra:\t%#llx\n"
-                  "memlock:\t%lu\n"
+                  "memlock:\t%llu\n"
                   "map_id:\t%u\n"
                   "frozen:\t%u\n",
                   map->map_type,
@@ -812,7 +811,7 @@ static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
                   map->max_entries,
                   map->map_flags,
                   (unsigned long long)map->map_extra,
-                  bpf_map_memory_footprint(map),
+                  bpf_map_memory_usage(map),
                   map->id,
                   READ_ONCE(map->frozen));
        if (type) {