Skip to content

Commit 3584718

Browse files
edumazetkuba-moo
authored andcommitted
net: fix sk_memory_allocated_{add|sub} vs softirqs
Jonathan Heathcote reported a regression caused by blamed commit on aarch64 architecture. x86 happens to have irq-safe __this_cpu_add_return() and __this_cpu_sub(), but this is not generic. I think my confusion came from "struct sock" argument, because these helpers are called with a locked socket. But the memory accounting is per-proto (and per-cpu after the blamed commit). We might cleanup these helpers later to directly accept a "struct proto *proto" argument. Switch to this_cpu_add_return() and this_cpu_xchg() operations, and get rid of preempt_disable()/preempt_enable() pairs. Fast path becomes a bit faster as a result :) Many thanks to Jonathan Heathcote for his awesome report and investigations. Fixes: 3cd3399 ("net: implement per-cpu reserves for memory_allocated") Reported-by: Jonathan Heathcote <[email protected]> Closes: https://lore.kernel.org/netdev/VI1PR01MB42407D7947B2EA448F1E04EFD10D2@VI1PR01MB4240.eurprd01.prod.exchangelabs.com/ Signed-off-by: Eric Dumazet <[email protected]> Acked-by: Soheil Hassas Yeganeh <[email protected]> Reviewed-by: Shakeel Butt <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent a44f2eb commit 3584718

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

include/net/sock.h

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,32 +1410,34 @@ sk_memory_allocated(const struct sock *sk)
14101410
#define SK_MEMORY_PCPU_RESERVE (1 << (20 - PAGE_SHIFT))
14111411
extern int sysctl_mem_pcpu_rsv;
14121412

1413+
static inline void proto_memory_pcpu_drain(struct proto *proto)
1414+
{
1415+
int val = this_cpu_xchg(*proto->per_cpu_fw_alloc, 0);
1416+
1417+
if (val)
1418+
atomic_long_add(val, proto->memory_allocated);
1419+
}
1420+
14131421
static inline void
1414-
sk_memory_allocated_add(struct sock *sk, int amt)
1422+
sk_memory_allocated_add(const struct sock *sk, int val)
14151423
{
1416-
int local_reserve;
1424+
struct proto *proto = sk->sk_prot;
14171425

1418-
preempt_disable();
1419-
local_reserve = __this_cpu_add_return(*sk->sk_prot->per_cpu_fw_alloc, amt);
1420-
if (local_reserve >= READ_ONCE(sysctl_mem_pcpu_rsv)) {
1421-
__this_cpu_sub(*sk->sk_prot->per_cpu_fw_alloc, local_reserve);
1422-
atomic_long_add(local_reserve, sk->sk_prot->memory_allocated);
1423-
}
1424-
preempt_enable();
1426+
val = this_cpu_add_return(*proto->per_cpu_fw_alloc, val);
1427+
1428+
if (unlikely(val >= READ_ONCE(sysctl_mem_pcpu_rsv)))
1429+
proto_memory_pcpu_drain(proto);
14251430
}
14261431

14271432
static inline void
1428-
sk_memory_allocated_sub(struct sock *sk, int amt)
1433+
sk_memory_allocated_sub(const struct sock *sk, int val)
14291434
{
1430-
int local_reserve;
1435+
struct proto *proto = sk->sk_prot;
14311436

1432-
preempt_disable();
1433-
local_reserve = __this_cpu_sub_return(*sk->sk_prot->per_cpu_fw_alloc, amt);
1434-
if (local_reserve <= -READ_ONCE(sysctl_mem_pcpu_rsv)) {
1435-
__this_cpu_sub(*sk->sk_prot->per_cpu_fw_alloc, local_reserve);
1436-
atomic_long_add(local_reserve, sk->sk_prot->memory_allocated);
1437-
}
1438-
preempt_enable();
1437+
val = this_cpu_sub_return(*proto->per_cpu_fw_alloc, val);
1438+
1439+
if (unlikely(val <= -READ_ONCE(sysctl_mem_pcpu_rsv)))
1440+
proto_memory_pcpu_drain(proto);
14391441
}
14401442

14411443
#define SK_ALLOC_PERCPU_COUNTER_BATCH 16

0 commit comments

Comments
 (0)