Skip to content

Commit 90a5527

Browse files
laoarAlexei Starovoitov
authored andcommitted
bpf: add new map ops ->map_mem_usage
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>
1 parent 2d5bcdc commit 90a5527

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

include/linux/bpf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ struct bpf_map_ops {
161161
bpf_callback_t callback_fn,
162162
void *callback_ctx, u64 flags);
163163

164+
u64 (*map_mem_usage)(const struct bpf_map *map);
165+
164166
/* BTF id of struct allocated by map_alloc */
165167
int *map_btf_id;
166168

kernel/bpf/syscall.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -771,16 +771,15 @@ static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
771771
}
772772

773773
#ifdef CONFIG_PROC_FS
774-
/* Provides an approximation of the map's memory footprint.
775-
* Used only to provide a backward compatibility and display
776-
* a reasonable "memlock" info.
777-
*/
778-
static unsigned long bpf_map_memory_footprint(const struct bpf_map *map)
774+
/* Show the memory usage of a bpf map */
775+
static u64 bpf_map_memory_usage(const struct bpf_map *map)
779776
{
780777
unsigned long size;
781778

782-
size = round_up(map->key_size + bpf_map_value_size(map), 8);
779+
if (map->ops->map_mem_usage)
780+
return map->ops->map_mem_usage(map);
783781

782+
size = round_up(map->key_size + bpf_map_value_size(map), 8);
784783
return round_up(map->max_entries * size, PAGE_SIZE);
785784
}
786785

@@ -803,7 +802,7 @@ static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
803802
"max_entries:\t%u\n"
804803
"map_flags:\t%#x\n"
805804
"map_extra:\t%#llx\n"
806-
"memlock:\t%lu\n"
805+
"memlock:\t%llu\n"
807806
"map_id:\t%u\n"
808807
"frozen:\t%u\n",
809808
map->map_type,
@@ -812,7 +811,7 @@ static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
812811
map->max_entries,
813812
map->map_flags,
814813
(unsigned long long)map->map_extra,
815-
bpf_map_memory_footprint(map),
814+
bpf_map_memory_usage(map),
816815
map->id,
817816
READ_ONCE(map->frozen));
818817
if (type) {

0 commit comments

Comments
 (0)