History

bpf: devmap memory usage

A new helper is introduced to calculate the memory usage of devmap and devmap_hash. The number of dynamically allocated elements are recored for devmap_hash already, but not for devmap. To track the memory size of dynamically allocated elements, this patch also count the numbers for devmap. The result as follows, - before 40: devmap name count_map flags 0x80 key 4B value 4B max_entries 65536 memlock 524288B 41: devmap_hash name count_map flags 0x80 key 4B value 4B max_entries 65536 memlock 524288B - after 40: devmap name count_map flags 0x80 <<<< no elements key 4B value 4B max_entries 65536 memlock 524608B 41: devmap_hash name count_map flags 0x80 <<<< no elements key 4B value 4B max_entries 65536 memlock 524608B Note that the number of buckets is same with max_entries for devmap_hash in this case.

Yafang Shao committed 3 years ago

Commit fa5e83d1 parent 835f1fc1 file · +24 −2Browse files

Unified · Split

kernel/bpf/devmap.c+24−2
@@ -819,8 +819,10 @@ static int dev_map_delete_elem(struct bpf_map *map, void *key) · 818 unmodified lines
819819 return -EINVAL;
820820
821821 old_dev = unrcu_pointer(xchg(&dtab->netdev_map[k], NULL));
822− if (old_dev)
822+ if (old_dev) {
823823 call_rcu(&old_dev->rcu, __dev_map_entry_free);
824+ atomic_dec((atomic_t *)&dtab->items);
825+ }
824826 return 0;
825827 }
826828
@@ -931,6 +933,8 @@ static int __dev_map_update_elem(struct net *net, struct bpf_map *map, · 104 unmodified lines
931933 old_dev = unrcu_pointer(xchg(&dtab->netdev_map[i], RCU_INITIALIZER(dev)));
932934 if (old_dev)
933935 call_rcu(&old_dev->rcu, __dev_map_entry_free);
936+ else
937+ atomic_inc((atomic_t *)&dtab->items);
934938
935939 return 0;
936940 }
@@ -1016,6 +1020,20 @@ static int dev_hash_map_redirect(struct bpf_map *map, u64 ifindex, u64 flags) · 79 unmodified lines
10161020 __dev_map_hash_lookup_elem);
10171021 }
10181022
1023+static u64 dev_map_mem_usage(const struct bpf_map *map)
1024+{
1025+ struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
1026+ u64 usage = sizeof(struct bpf_dtab);
1027+
1028+ if (map->map_type == BPF_MAP_TYPE_DEVMAP_HASH)
1029+ usage += (u64)dtab->n_buckets * sizeof(struct hlist_head);
1030+ else
1031+ usage += (u64)map->max_entries * sizeof(struct bpf_dtab_netdev *);
1032+ usage += atomic_read((atomic_t *)&dtab->items) *
1033+ (u64)sizeof(struct bpf_dtab_netdev);
1034+ return usage;
1035+}
1036+
10191037 BTF_ID_LIST_SINGLE(dev_map_btf_ids, struct, bpf_dtab)
10201038 const struct bpf_map_ops dev_map_ops = {
10211039 .map_meta_equal = bpf_map_meta_equal,
@@ -1026,6 +1044,7 @@ const struct bpf_map_ops dev_map_ops = { · 4 unmodified lines
10261044 .map_update_elem = dev_map_update_elem,
10271045 .map_delete_elem = dev_map_delete_elem,
10281046 .map_check_btf = map_check_no_btf,
1047+ .map_mem_usage = dev_map_mem_usage,
10291048 .map_btf_id = &dev_map_btf_ids[0],
10301049 .map_redirect = dev_map_redirect,
10311050 };
@@ -1039,6 +1058,7 @@ const struct bpf_map_ops dev_map_hash_ops = { · 7 unmodified lines
10391058 .map_update_elem = dev_map_hash_update_elem,
10401059 .map_delete_elem = dev_map_hash_delete_elem,
10411060 .map_check_btf = map_check_no_btf,
1061+ .map_mem_usage = dev_map_mem_usage,
10421062 .map_btf_id = &dev_map_btf_ids[0],
10431063 .map_redirect = dev_hash_map_redirect,
10441064 };
@@ -1109,9 +1129,11 @@ static int dev_map_notification(struct notifier_block *notifier, · 64 unmodified lines
11091129 if (!dev || netdev != dev->dev)
11101130 continue;
11111131 odev = unrcu_pointer(cmpxchg(&dtab->netdev_map[i], RCU_INITIALIZER(dev), NULL));
1112− if (dev == odev)
1132+ if (dev == odev) {
11131133 call_rcu(&dev->rcu,
11141134 __dev_map_entry_free);
1135+ atomic_dec((atomic_t *)&dtab->items);
1136+ }
11151137 }
11161138 }
11171139 rcu_read_unlock();