net: fix __sock_gen_cookie()
Subsystems (3)
PATCH 1/1 net: fix __sock_gen_cookie()
I was mistaken how atomic64_try_cmpxchg(&sk_cookie, &res, new) is working.
I was assuming @res would contain the final sk_cookie value, regardless of the success of our cmpxchg()
We could do something like:
if (atomic64_try_cmpxchg(&sk_cookie, &res, new)
res = new;
But we can avoid a conditional and read sk_cookie again.
atomic64_cmpxchg(&sk_cookie, res, new);
res = atomic64_read(&sk_cookie);
Addresses-Coverity-ID: 1527347 ("Error handling issues")
--- net/core/sock_diag.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
net/core/sock_diag.c +4 -1
@@ -30,7 +30,10 @@ u64 __sock_gen_cookie(struct sock *sk)if (!res) {u64 new =gen_cookie_next (&sock_cookie); -atomic64_try_cmpxchg (&sk->sk_cookie, &res, new); +atomic64_cmpxchg (&sk->sk_cookie, res, new); + +/* Another thread might have changed sk_cookie before us. */ + res =atomic64_read (&sk->sk_cookie); }return res; }
Signature · 1 line
2.38.1.584.g0f3c55d4c2-goog
This patch was applied to netdev/net-next.git (master) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (master) by Jakub Kicinski <[email protected]>:
Quoted 9 lines · On Fri, 18 Nov 2022 04:38:43 +0000 you wrote:
I was mistaken how
atomic64_try_cmpxchg(&sk_cookie, &res, new)is working.I was assuming @res would contain the final
sk_cookievalue, regardless of the success of ourcmpxchg()We could do something like:
[...]
Here is the summary with links:
- [net-next] net: fix __sock_gen_cookie()
https://git.kernel.org/netdev/net-next/c/32634819ad37
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html