Skip to content

Commit 3cd3399

Browse files
edumazetkuba-moo
authored andcommitted
net: implement per-cpu reserves for memory_allocated
We plan keeping sk->sk_forward_alloc as small as possible in future patches. This means we are going to call sk_memory_allocated_add() and sk_memory_allocated_sub() more often. Implement a per-cpu cache of +1/-1 MB, to reduce number of changes to sk->sk_prot->memory_allocated, which would otherwise be cause of false sharing. Signed-off-by: Eric Dumazet <[email protected]> Acked-by: Soheil Hassas Yeganeh <[email protected]> Reviewed-by: Shakeel Butt <[email protected]> Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 0defbb0 commit 3cd3399

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

include/net/sock.h

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,22 +1397,48 @@ static inline bool sk_under_memory_pressure(const struct sock *sk)
13971397
return !!*sk->sk_prot->memory_pressure;
13981398
}
13991399

1400+
static inline long
1401+
proto_memory_allocated(const struct proto *prot)
1402+
{
1403+
return max(0L, atomic_long_read(prot->memory_allocated));
1404+
}
1405+
14001406
static inline long
14011407
sk_memory_allocated(const struct sock *sk)
14021408
{
1403-
return atomic_long_read(sk->sk_prot->memory_allocated);
1409+
return proto_memory_allocated(sk->sk_prot);
14041410
}
14051411

1412+
/* 1 MB per cpu, in page units */
1413+
#define SK_MEMORY_PCPU_RESERVE (1 << (20 - PAGE_SHIFT))
1414+
14061415
static inline long
14071416
sk_memory_allocated_add(struct sock *sk, int amt)
14081417
{
1409-
return atomic_long_add_return(amt, sk->sk_prot->memory_allocated);
1418+
int local_reserve;
1419+
1420+
preempt_disable();
1421+
local_reserve = __this_cpu_add_return(*sk->sk_prot->per_cpu_fw_alloc, amt);
1422+
if (local_reserve >= SK_MEMORY_PCPU_RESERVE) {
1423+
__this_cpu_sub(*sk->sk_prot->per_cpu_fw_alloc, local_reserve);
1424+
atomic_long_add(local_reserve, sk->sk_prot->memory_allocated);
1425+
}
1426+
preempt_enable();
1427+
return sk_memory_allocated(sk);
14101428
}
14111429

14121430
static inline void
14131431
sk_memory_allocated_sub(struct sock *sk, int amt)
14141432
{
1415-
atomic_long_sub(amt, sk->sk_prot->memory_allocated);
1433+
int local_reserve;
1434+
1435+
preempt_disable();
1436+
local_reserve = __this_cpu_sub_return(*sk->sk_prot->per_cpu_fw_alloc, amt);
1437+
if (local_reserve <= -SK_MEMORY_PCPU_RESERVE) {
1438+
__this_cpu_sub(*sk->sk_prot->per_cpu_fw_alloc, local_reserve);
1439+
atomic_long_add(local_reserve, sk->sk_prot->memory_allocated);
1440+
}
1441+
preempt_enable();
14161442
}
14171443

14181444
#define SK_ALLOC_PERCPU_COUNTER_BATCH 16
@@ -1441,12 +1467,6 @@ proto_sockets_allocated_sum_positive(struct proto *prot)
14411467
return percpu_counter_sum_positive(prot->sockets_allocated);
14421468
}
14431469

1444-
static inline long
1445-
proto_memory_allocated(struct proto *prot)
1446-
{
1447-
return atomic_long_read(prot->memory_allocated);
1448-
}
1449-
14501470
static inline bool
14511471
proto_memory_pressure(struct proto *prot)
14521472
{

0 commit comments

Comments
 (0)