Skip to content

Commit f5a5589

Browse files
tracywwnjkuba-moo
authored andcommitted
tcp: use a smaller percpu_counter batch size for sk_alloc
Currently, a percpu_counter with the default batch size (2*nr_cpus) is used to record the total # of active sockets per protocol. This means sk_sockets_allocated_read_positive() could be off by +/-2*(nr_cpus^2). This under/over-estimation could lead to wrong memory suppression conditions in __sk_raise_mem_allocated(). Fix this by using a more reasonable fixed batch size of 16. See related commit cf86a08 ("net/dst: use a smaller percpu_counter batch for dst entries accounting") that addresses a similar issue. Signed-off-by: Wei Wang <[email protected]> Signed-off-by: Eric Dumazet <[email protected]> Reviewed-by: Soheil Hassas Yeganeh <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 6fd5eee commit f5a5589

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

include/net/sock.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,14 +1350,18 @@ sk_memory_allocated_sub(struct sock *sk, int amt)
13501350
atomic_long_sub(amt, sk->sk_prot->memory_allocated);
13511351
}
13521352

1353+
#define SK_ALLOC_PERCPU_COUNTER_BATCH 16
1354+
13531355
static inline void sk_sockets_allocated_dec(struct sock *sk)
13541356
{
1355-
percpu_counter_dec(sk->sk_prot->sockets_allocated);
1357+
percpu_counter_add_batch(sk->sk_prot->sockets_allocated, -1,
1358+
SK_ALLOC_PERCPU_COUNTER_BATCH);
13561359
}
13571360

13581361
static inline void sk_sockets_allocated_inc(struct sock *sk)
13591362
{
1360-
percpu_counter_inc(sk->sk_prot->sockets_allocated);
1363+
percpu_counter_add_batch(sk->sk_prot->sockets_allocated, 1,
1364+
SK_ALLOC_PERCPU_COUNTER_BATCH);
13611365
}
13621366

13631367
static inline u64

0 commit comments

Comments
 (0)