inetpeer: avoid false sharing in inet_peer_xrlim_allow()
Subsystems (3)
PATCH 1/1 inetpeer: avoid false sharing in inet_peer_xrlim_allow()
Under DOS, inet_peer_xrlim_allow() might be called millions of times per second from different cpus.
Make sure to write over peer->rate_tokens and peer->rate_last only when really needed.
Note the inherent races of this function are still there, we do not care of precise ICMP rate limiting.
--- net/ipv4/inetpeer.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)
net/ipv4/inetpeer.c +11 -7
@@ -246,23 +246,27 @@ void inet_putpeer(struct inet_peer *p)#define XRLIM_BURST_FACTOR 6bool inet_peer_xrlim_allow (struct inet_peer *peer,int timeout) { -unsigned long now, token; +unsigned long now, token, otoken, delta;bool rc = false;if (!peer)return true; - token = peer->rate_tokens; + token = otoken =READ_ONCE (peer->rate_tokens); now = jiffies; - token += now - peer->rate_last; - peer->rate_last = now; -if (token >XRLIM_BURST_FACTOR * timeout) - token =XRLIM_BURST_FACTOR * timeout; + delta = now -READ_ONCE (peer->rate_last); +if (delta) { +WRITE_ONCE (peer->rate_last, now); + token += delta; +if (token >XRLIM_BURST_FACTOR * timeout) + token =XRLIM_BURST_FACTOR * timeout; + }if (token >= timeout) { token -= timeout; rc = true; } - peer->rate_tokens = token; +if (token != otoken) +WRITE_ONCE (peer->rate_tokens, token);return rc; }EXPORT_SYMBOL (inet_peer_xrlim_allow);
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Quoted 10 lines · Eric Dumazet
Under DOS,
inet_peer_xrlim_allow()might be called millions of times per second from different cpus.Make sure to write over
peer->rate_tokensandpeer->rate_lastonly when really needed.Note the inherent races of this function are still there, we do not care of precise ICMP rate limiting.
Signed-off-by Eric Dumazet <[email protected]>
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 10 lines · On Thu, 19 Dec 2024 15:03:30 +0000 you wrote:
Under DOS,
inet_peer_xrlim_allow()might be called millions of times per second from different cpus.Make sure to write over
peer->rate_tokensandpeer->rate_lastonly when really needed.Note the inherent races of this function are still there, we do not care of precise ICMP rate limiting.
[...]
Here is the summary with links:
- [net-next] inetpeer: avoid false sharing in inet_peer_xrlim_allow()
https://git.kernel.org/netdev/net-next/c/05dd04b218f4
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html