@@ -721,6 +721,28 @@ static int bpf_for_each_array_elem(struct bpf_map *map, bpf_callback_t callback_
721721 return num_elems ;
722722}
723723
724+ static u64 array_map_mem_usage (const struct bpf_map * map )
725+ {
726+ struct bpf_array * array = container_of (map , struct bpf_array , map );
727+ bool percpu = map -> map_type == BPF_MAP_TYPE_PERCPU_ARRAY ;
728+ u32 elem_size = array -> elem_size ;
729+ u64 entries = map -> max_entries ;
730+ u64 usage = sizeof (* array );
731+
732+ if (percpu ) {
733+ usage += entries * sizeof (void * );
734+ usage += entries * elem_size * num_possible_cpus ();
735+ } else {
736+ if (map -> map_flags & BPF_F_MMAPABLE ) {
737+ usage = PAGE_ALIGN (usage );
738+ usage += PAGE_ALIGN (entries * elem_size );
739+ } else {
740+ usage += entries * elem_size ;
741+ }
742+ }
743+ return usage ;
744+ }
745+
724746BTF_ID_LIST_SINGLE (array_map_btf_ids , struct , bpf_array )
725747const struct bpf_map_ops array_map_ops = {
726748 .map_meta_equal = array_map_meta_equal ,
@@ -742,6 +764,7 @@ const struct bpf_map_ops array_map_ops = {
742764 .map_update_batch = generic_map_update_batch ,
743765 .map_set_for_each_callback_args = map_set_for_each_callback_args ,
744766 .map_for_each_callback = bpf_for_each_array_elem ,
767+ .map_mem_usage = array_map_mem_usage ,
745768 .map_btf_id = & array_map_btf_ids [0 ],
746769 .iter_seq_info = & iter_seq_info ,
747770};
@@ -762,6 +785,7 @@ const struct bpf_map_ops percpu_array_map_ops = {
762785 .map_update_batch = generic_map_update_batch ,
763786 .map_set_for_each_callback_args = map_set_for_each_callback_args ,
764787 .map_for_each_callback = bpf_for_each_array_elem ,
788+ .map_mem_usage = array_map_mem_usage ,
765789 .map_btf_id = & array_map_btf_ids [0 ],
766790 .iter_seq_info = & iter_seq_info ,
767791};
@@ -1156,6 +1180,7 @@ const struct bpf_map_ops prog_array_map_ops = {
11561180 .map_fd_sys_lookup_elem = prog_fd_array_sys_lookup_elem ,
11571181 .map_release_uref = prog_array_map_clear ,
11581182 .map_seq_show_elem = prog_array_map_seq_show_elem ,
1183+ .map_mem_usage = array_map_mem_usage ,
11591184 .map_btf_id = & array_map_btf_ids [0 ],
11601185};
11611186
@@ -1257,6 +1282,7 @@ const struct bpf_map_ops perf_event_array_map_ops = {
12571282 .map_fd_put_ptr = perf_event_fd_array_put_ptr ,
12581283 .map_release = perf_event_fd_array_release ,
12591284 .map_check_btf = map_check_no_btf ,
1285+ .map_mem_usage = array_map_mem_usage ,
12601286 .map_btf_id = & array_map_btf_ids [0 ],
12611287};
12621288
@@ -1291,6 +1317,7 @@ const struct bpf_map_ops cgroup_array_map_ops = {
12911317 .map_fd_get_ptr = cgroup_fd_array_get_ptr ,
12921318 .map_fd_put_ptr = cgroup_fd_array_put_ptr ,
12931319 .map_check_btf = map_check_no_btf ,
1320+ .map_mem_usage = array_map_mem_usage ,
12941321 .map_btf_id = & array_map_btf_ids [0 ],
12951322};
12961323#endif
@@ -1379,5 +1406,6 @@ const struct bpf_map_ops array_of_maps_map_ops = {
13791406 .map_lookup_batch = generic_map_lookup_batch ,
13801407 .map_update_batch = generic_map_update_batch ,
13811408 .map_check_btf = map_check_no_btf ,
1409+ .map_mem_usage = array_map_mem_usage ,
13821410 .map_btf_id = & array_map_btf_ids [0 ],
13831411};
0 commit comments