Skip to content

Commit ed69e06

Browse files
committed
Andrii Nakryiko says: ==================== pull-request: bpf-next 2023-03-08 We've added 23 non-merge commits during the last 2 day(s) which contain a total of 28 files changed, 414 insertions(+), 104 deletions(-). The main changes are: 1) Add more precise memory usage reporting for all BPF map types, from Yafang Shao. 2) Add ARM32 USDT support to libbpf, from Puranjay Mohan. 3) Fix BTF_ID_LIST size causing problems in !CONFIG_DEBUG_INFO_BTF, from Nathan Chancellor. 4) IMA selftests fix, from Roberto Sassu. 5) libbpf fix in APK support code, from Daniel Müller. * https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next: (23 commits) selftests/bpf: Fix IMA test libbpf: USDT arm arg parsing support libbpf: Refactor parse_usdt_arg() to re-use code libbpf: Fix theoretical u32 underflow in find_cd() function bpf: enforce all maps having memory usage callback bpf: offload map memory usage bpf, net: xskmap memory usage bpf, net: sock_map memory usage bpf, net: bpf_local_storage memory usage bpf: local_storage memory usage bpf: bpf_struct_ops memory usage bpf: queue_stack_maps memory usage bpf: devmap memory usage bpf: cpumap memory usage bpf: bloom_filter memory usage bpf: ringbuf memory usage bpf: reuseport_array memory usage bpf: stackmap memory usage bpf: arraymap memory usage bpf: hashtab memory usage ... ==================== Link: https://lore.kernel.org/r/20230308193533.1671597-1-andrii@kernel.org Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2 parents 1036908 + 12fabae commit ed69e06

28 files changed

+414
-104
lines changed

include/linux/bpf.h

Lines changed: 8 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

@@ -2622,6 +2624,7 @@ static inline bool bpf_map_is_offloaded(struct bpf_map *map)
26222624

26232625
struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
26242626
void bpf_map_offload_map_free(struct bpf_map *map);
2627+
u64 bpf_map_offload_map_mem_usage(const struct bpf_map *map);
26252628
int bpf_prog_test_run_syscall(struct bpf_prog *prog,
26262629
const union bpf_attr *kattr,
26272630
union bpf_attr __user *uattr);
@@ -2693,6 +2696,11 @@ static inline void bpf_map_offload_map_free(struct bpf_map *map)
26932696
{
26942697
}
26952698

2699+
static inline u64 bpf_map_offload_map_mem_usage(const struct bpf_map *map)
2700+
{
2701+
return 0;
2702+
}
2703+
26962704
static inline int bpf_prog_test_run_syscall(struct bpf_prog *prog,
26972705
const union bpf_attr *kattr,
26982706
union bpf_attr __user *uattr)

include/linux/bpf_local_storage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,6 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
164164
void *value, u64 map_flags, gfp_t gfp_flags);
165165

166166
void bpf_local_storage_free_rcu(struct rcu_head *rcu);
167+
u64 bpf_local_storage_map_mem_usage(const struct bpf_map *map);
167168

168169
#endif /* _BPF_LOCAL_STORAGE_H */

include/linux/btf_ids.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ extern struct btf_id_set8 name;
204204

205205
#else
206206

207-
#define BTF_ID_LIST(name) static u32 __maybe_unused name[16];
207+
#define BTF_ID_LIST(name) static u32 __maybe_unused name[64];
208208
#define BTF_ID(prefix, name)
209209
#define BTF_ID_FLAGS(prefix, name, ...)
210210
#define BTF_ID_UNUSED

include/net/xdp_sock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ struct xdp_umem {
3838
struct xsk_map {
3939
struct bpf_map map;
4040
spinlock_t lock; /* Synchronize map updates */
41+
atomic_t count;
4142
struct xdp_sock __rcu *xsk_map[];
4243
};
4344

kernel/bpf/arraymap.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
724746
BTF_ID_LIST_SINGLE(array_map_btf_ids, struct, bpf_array)
725747
const 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
};

kernel/bpf/bloom_filter.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,17 @@ static int bloom_map_check_btf(const struct bpf_map *map,
193193
return btf_type_is_void(key_type) ? 0 : -EINVAL;
194194
}
195195

196+
static u64 bloom_map_mem_usage(const struct bpf_map *map)
197+
{
198+
struct bpf_bloom_filter *bloom;
199+
u64 bitset_bytes;
200+
201+
bloom = container_of(map, struct bpf_bloom_filter, map);
202+
bitset_bytes = BITS_TO_BYTES((u64)bloom->bitset_mask + 1);
203+
bitset_bytes = roundup(bitset_bytes, sizeof(unsigned long));
204+
return sizeof(*bloom) + bitset_bytes;
205+
}
206+
196207
BTF_ID_LIST_SINGLE(bpf_bloom_map_btf_ids, struct, bpf_bloom_filter)
197208
const struct bpf_map_ops bloom_filter_map_ops = {
198209
.map_meta_equal = bpf_map_meta_equal,
@@ -206,5 +217,6 @@ const struct bpf_map_ops bloom_filter_map_ops = {
206217
.map_update_elem = bloom_map_update_elem,
207218
.map_delete_elem = bloom_map_delete_elem,
208219
.map_check_btf = bloom_map_check_btf,
220+
.map_mem_usage = bloom_map_mem_usage,
209221
.map_btf_id = &bpf_bloom_map_btf_ids[0],
210222
};

kernel/bpf/bpf_cgrp_storage.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ const struct bpf_map_ops cgrp_storage_map_ops = {
221221
.map_update_elem = bpf_cgrp_storage_update_elem,
222222
.map_delete_elem = bpf_cgrp_storage_delete_elem,
223223
.map_check_btf = bpf_local_storage_map_check_btf,
224+
.map_mem_usage = bpf_local_storage_map_mem_usage,
224225
.map_btf_id = &bpf_local_storage_map_btf_id[0],
225226
.map_owner_storage_ptr = cgroup_storage_ptr,
226227
};

kernel/bpf/bpf_inode_storage.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ const struct bpf_map_ops inode_storage_map_ops = {
223223
.map_update_elem = bpf_fd_inode_storage_update_elem,
224224
.map_delete_elem = bpf_fd_inode_storage_delete_elem,
225225
.map_check_btf = bpf_local_storage_map_check_btf,
226+
.map_mem_usage = bpf_local_storage_map_mem_usage,
226227
.map_btf_id = &bpf_local_storage_map_btf_id[0],
227228
.map_owner_storage_ptr = inode_storage_ptr,
228229
};

kernel/bpf/bpf_local_storage.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,16 @@ bool bpf_local_storage_unlink_nolock(struct bpf_local_storage *local_storage)
685685
return free_storage;
686686
}
687687

688+
u64 bpf_local_storage_map_mem_usage(const struct bpf_map *map)
689+
{
690+
struct bpf_local_storage_map *smap = (struct bpf_local_storage_map *)map;
691+
u64 usage = sizeof(*smap);
692+
693+
/* The dynamically callocated selems are not counted currently. */
694+
usage += sizeof(*smap->buckets) * (1ULL << smap->bucket_log);
695+
return usage;
696+
}
697+
688698
struct bpf_map *
689699
bpf_local_storage_map_alloc(union bpf_attr *attr,
690700
struct bpf_local_storage_cache *cache)

kernel/bpf/bpf_struct_ops.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,21 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
641641
return map;
642642
}
643643

644+
static u64 bpf_struct_ops_map_mem_usage(const struct bpf_map *map)
645+
{
646+
struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
647+
const struct bpf_struct_ops *st_ops = st_map->st_ops;
648+
const struct btf_type *vt = st_ops->value_type;
649+
u64 usage;
650+
651+
usage = sizeof(*st_map) +
652+
vt->size - sizeof(struct bpf_struct_ops_value);
653+
usage += vt->size;
654+
usage += btf_type_vlen(vt) * sizeof(struct bpf_links *);
655+
usage += PAGE_SIZE;
656+
return usage;
657+
}
658+
644659
BTF_ID_LIST_SINGLE(bpf_struct_ops_map_btf_ids, struct, bpf_struct_ops_map)
645660
const struct bpf_map_ops bpf_struct_ops_map_ops = {
646661
.map_alloc_check = bpf_struct_ops_map_alloc_check,
@@ -651,6 +666,7 @@ const struct bpf_map_ops bpf_struct_ops_map_ops = {
651666
.map_delete_elem = bpf_struct_ops_map_delete_elem,
652667
.map_update_elem = bpf_struct_ops_map_update_elem,
653668
.map_seq_show_elem = bpf_struct_ops_map_seq_show_elem,
669+
.map_mem_usage = bpf_struct_ops_map_mem_usage,
654670
.map_btf_id = &bpf_struct_ops_map_btf_ids[0],
655671
};
656672

0 commit comments

Comments
 (0)