Skip to contentSkip to conversation

net: avoid possible false sharing in sk_leave_memory_pressure()

PATCH Applied Eric Dumazet 2 participants 2 messages Oct 9, 2019 – Oct 10, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: avoid possible false sharing in sk_leave_memory_pressure()

As mentioned in https://github.com/google/ktsan/wiki/READ_ONCE-and-WRITE_ONCE#it-may-improve-performance
a C compiler can legally transform :

if (memory_pressure && *memory_pressure)

        *memory_pressure = 0;

to :

if (memory_pressure)

        *memory_pressure = 0;
Fixes 0604475119de ("tcp: add TCPMemoryPressuresChrono counter")
Fixes 180d8cd942ce ("foundations of per-cgroup memory pressure controlling.")
Fixes 3ab224be6d69 ("[NET] CORE: Introducing new memory accounting interface.")
---
 net/core/sock.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/core/sock.c +2 -2
index fac2b4d80de5e54c77628073d7930bddf8a10cb3..50647a10fdb7f050e963e2734f0d3555fa4bd7aa 100644
@@ -2334,8 +2334,8 @@ static void sk_leave_memory_pressure(struct sock *sk)
 	} else {
 		unsigned long *memory_pressure = sk->sk_prot->memory_pressure;
 
-		if (memory_pressure && *memory_pressure)
-			*memory_pressure = 0;
+		if (memory_pressure && READ_ONCE(*memory_pressure))
+			WRITE_ONCE(*memory_pressure, 0);
 	}
 }
 
Signature · 1 line
2.23.0.581.g78d2f28ef7-goog
#1 lore raw
Quoted 8 lines · Eric Dumazet

As mentioned in https://github.com/google/ktsan/wiki/READ_ONCE-and-WRITE_ONCE#it-may-improve-performance
a C compiler can legally transform :

if (memory_pressure && *memory_pressure)

        *memory_pressure = 0;

to :

Quoted 7 lines

if (memory_pressure)

        *memory_pressure = 0;
Fixes 0604475119de ("tcp: add TCPMemoryPressuresChrono counter")
Fixes 180d8cd942ce ("foundations of per-cgroup memory pressure controlling.")
Fixes 3ab224be6d69 ("[NET] CORE: Introducing new memory accounting interface.")

Applied, thanks!