Skip to contentSkip to conversation

sock_map: include sk_psock memory overhead too

PATCH In review Cong Wang 3 participants 4 messages Mar 26, 2023 – Apr 2, 2023

Subsystems (5)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 sock_map: include sk_psock memory overhead too

From: Cong Wang <[email protected]>

When a socket is added to a sockmap, sk_psock is allocated too as its sk_user_data, therefore it should be consider as an overhead of sockmap memory usage.

Before this patch:

1: sockmap flags 0x0

	key 4B  value 4B  max_entries 2  memlock 656B
	pids echo-sockmap(549)

After this patch:

9: sockmap flags 0x0

	key 4B  value 4B  max_entries 2  memlock 1824B
	pids echo-sockmap(568)
Fixes 73d2c61919e9 ("bpf, net: sock_map memory usage")
Cc Yafang Shao <[email protected]>
Cc Jakub Sitnicki <[email protected]>
Cc John Fastabend <[email protected]>
---
 net/core/sock_map.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
net/core/sock_map.c +9 -1
index 7c189c2e2fbf..22197e565ece 100644
@@ -799,9 +799,17 @@ static void sock_map_fini_seq_private(void *priv_data)
 
 static u64 sock_map_mem_usage(const struct bpf_map *map)
 {
+	struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
 	u64 usage = sizeof(struct bpf_stab);
+	int i;
 
 	usage += (u64)map->max_entries * sizeof(struct sock *);
+
+	for (i = 0; i < stab->map.max_entries; i++) {
+		if (stab->sks[i])
+			usage += sizeof(struct sk_psock);
+	}
+
 	return usage;
 }
 
@@ -1412,7 +1420,7 @@ static u64 sock_hash_mem_usage(const struct bpf_map *map)
 	u64 usage = sizeof(*htab);
 
 	usage += htab->buckets_num * sizeof(struct bpf_shtab_bucket);
-	usage += atomic_read(&htab->count) * (u64)htab->elem_size;
+	usage += atomic_read(&htab->count) * ((u64)htab->elem_size + sizeof(struct sk_psock));
 	return usage;
 }
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 33 lines · Cong Wang

From: Cong Wang <[email protected]>

When a socket is added to a sockmap, sk_psock is allocated too as its sk_user_data, therefore it should be consider as an overhead of sockmap memory usage.

Before this patch:

1: sockmap flags 0x0
key 4B value 4B max_entries 2 memlock 656B pids echo-sockmap(549)

After this patch:

9: sockmap flags 0x0
key 4B value 4B max_entries 2 memlock 1824B pids echo-sockmap(568)

Fixes 73d2c61919e9 ("bpf, net: sock_map memory usage")
Cc Yafang Shao <[email protected]>
Cc Jakub Sitnicki <[email protected]>
Cc John Fastabend <[email protected]>
---
 net/core/sock_map.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
net/core/sock_map.c +0 -0
index 7c189c2e2fbf..22197e565ece 100644

net/core/sock_map.c static void sock_map_fini_seq_private(void *priv_data) · line 808

Quoted 10 lines
net/core/sock_map.c +4 -0
@@ -799,9 +799,17 @@ static void sock_map_fini_seq_private(void *priv_data)
 
 static u64 sock_map_mem_usage(const struct bpf_map *map)
 {
+       struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
        u64 usage = sizeof(struct bpf_stab);
+       int i;
 
        usage += (u64)map->max_entries * sizeof(struct sock *);
+
+       for (i = 0; i < stab->map.max_entries; i++) {

Although it adds a for-loop, the operation below is quite light. So it looks good to me.

net/core/sock_map.c static void sock_map_fini_seq_private(void *priv_data) · line 809

Quoted 1 line
net/core/sock_map.c +1 -0
+               if (stab->sks[i])

Nit, stab->sks[i] can be modified in the delete path in parallel, so there should be a READ_ONCE() here.

Quoted 18 lines
net/core/sock_map.c +4 -2
+                       usage += sizeof(struct sk_psock);
+       }
+
        return usage;
 }
 
@@ -1412,7 +1420,7 @@ static u64 sock_hash_mem_usage(const struct bpf_map *map)
        u64 usage = sizeof(*htab);
 
        usage += htab->buckets_num * sizeof(struct bpf_shtab_bucket);
-       usage += atomic_read(&htab->count) * (u64)htab->elem_size;
+       usage += atomic_read(&htab->count) * ((u64)htab->elem_size + sizeof(struct sk_psock));
        return usage;
 }
 
--

2.34.1

Signature · 2 lines
Regards
Yafang
maintainer
#2 lore raw
Quoted 41 lines · Yafang Shao
Quoted 40 lines · Cong Wang

From: Cong Wang <[email protected]>

When a socket is added to a sockmap, sk_psock is allocated too as its sk_user_data, therefore it should be consider as an overhead of sockmap memory usage.

Before this patch:

1: sockmap flags 0x0
key 4B value 4B max_entries 2 memlock 656B pids echo-sockmap(549)

After this patch:

9: sockmap flags 0x0
key 4B value 4B max_entries 2 memlock 1824B pids echo-sockmap(568)

Fixes 73d2c61919e9 ("bpf, net: sock_map memory usage")
Cc Yafang Shao <[email protected]>
Cc Jakub Sitnicki <[email protected]>
Cc John Fastabend <[email protected]>
---
 net/core/sock_map.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
net/core/sock_map.c +2 -0
index 7c189c2e2fbf..22197e565ece 100644
@@ -799,9 +799,17 @@ static void sock_map_fini_seq_private(void *priv_data)
 
 static u64 sock_map_mem_usage(const struct bpf_map *map)
 {
+       struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
        u64 usage = sizeof(struct bpf_stab);
+       int i;
 
Quoted 6 lines

net/core/sock_map.c static void sock_map_fini_seq_private(void *priv_data) · line 808

Quoted 3 lines
net/core/sock_map.c +2 -0
        usage += (u64)map->max_entries * sizeof(struct sock *);
+
+       for (i = 0; i < stab->map.max_entries; i++) {

Although it adds a for-loop, the operation below is quite light. So it looks good to me.

We could track a count from update to avoid the loop?

Quoted 29 lines

net/core/sock_map.c static void sock_map_fini_seq_private(void *priv_data) · line 809

Quoted 2 lines
net/core/sock_map.c +1 -0
+               if (stab->sks[i])

Nit, stab->sks[i] can be modified in the delete path in parallel, so there should be a READ_ONCE() here.

Quoted 18 lines
net/core/sock_map.c +4 -2
+                       usage += sizeof(struct sk_psock);
+       }
+
        return usage;
 }
 
@@ -1412,7 +1420,7 @@ static u64 sock_hash_mem_usage(const struct bpf_map *map)
        u64 usage = sizeof(*htab);
 
        usage += htab->buckets_num * sizeof(struct bpf_shtab_bucket);
-       usage += atomic_read(&htab->count) * (u64)htab->elem_size;
+       usage += atomic_read(&htab->count) * ((u64)htab->elem_size + sizeof(struct sk_psock));
        return usage;
 }
 
--

2.34.1

Signature · 2 lines
Regards
Yafang
#3 lore raw
Quoted 43 lines · John Fastabend
Quoted 41 lines · Yafang Shao
Quoted 40 lines · Cong Wang

From: Cong Wang <[email protected]>

When a socket is added to a sockmap, sk_psock is allocated too as its sk_user_data, therefore it should be consider as an overhead of sockmap memory usage.

Before this patch:

1: sockmap flags 0x0
key 4B value 4B max_entries 2 memlock 656B pids echo-sockmap(549)

After this patch:

9: sockmap flags 0x0
key 4B value 4B max_entries 2 memlock 1824B pids echo-sockmap(568)

Fixes 73d2c61919e9 ("bpf, net: sock_map memory usage")
Cc Yafang Shao <[email protected]>
Cc Jakub Sitnicki <[email protected]>
Cc John Fastabend <[email protected]>
---
 net/core/sock_map.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
net/core/sock_map.c +2 -0
index 7c189c2e2fbf..22197e565ece 100644
@@ -799,9 +799,17 @@ static void sock_map_fini_seq_private(void *priv_data)
 
 static u64 sock_map_mem_usage(const struct bpf_map *map)
 {
+       struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
        u64 usage = sizeof(struct bpf_stab);
+       int i;
 
Quoted 9 lines
Quoted 6 lines

net/core/sock_map.c static void sock_map_fini_seq_private(void *priv_data) · line 808

Quoted 3 lines
net/core/sock_map.c +2 -0
        usage += (u64)map->max_entries * sizeof(struct sock *);
+
+       for (i = 0; i < stab->map.max_entries; i++) {

Although it adds a for-loop, the operation below is quite light. So it looks good to me.

We could track a count from update to avoid the loop?

I prefer adding a count into struct bpf_stab. We can also get the number of socks easily with this new count, and it should be acceptable to modify this count in the update/delete paths.

Signature · 2 lines
Regards
Yafang