* [PATCH v2 net-next] net: use try_cmpxchg() in lock_sock_nested()
@ 2026-02-26 2:12 Eric Dumazet
2026-02-26 2:36 ` Jason Xing
2026-02-28 1:40 ` patchwork-bot+netdevbpf
0 siblings, 2 replies; 3+ messages in thread
From: Eric Dumazet @ 2026-02-26 2:12 UTC (permalink / raw)
To: David S . Miller, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Kuniyuki Iwashima, netdev, eric.dumazet,
Eric Dumazet
Add a fast path in lock_sock_nested(), to avoid acquiring
the socket spinlock only to set @owned to one:
spin_lock_bh(&sk->sk_lock.slock);
if (unlikely(sock_owned_by_user_nocheck(sk)))
__lock_sock(sk);
sk->sk_lock.owned = 1;
spin_unlock_bh(&sk->sk_lock.slock);
On x86_64 compiler generates something quite efficient:
00000000000077c0 <lock_sock_nested>:
77c0: f3 0f 1e fa endbr64
77c4: e8 00 00 00 00 call __fentry__
77c9: b9 01 00 00 00 mov $0x1,%ecx
77ce: 31 c0 xor %eax,%eax
77d0: f0 48 0f b1 8f 48 01 00 00 lock cmpxchg %rcx,0x148(%rdi)
77d9: 75 06 jne slow_path
77db: 2e e9 00 00 00 00 cs jmp __x86_return_thunk-0x4
slow_path: ...
Signed-off-by: Eric Dumazet <edumazet@google.com>
Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com>
---
include/net/sock.h | 9 +++++++--
net/core/sock.c | 13 +++++++++++++
2 files changed, 20 insertions(+), 2 deletions(-)
diff --git a/include/net/sock.h b/include/net/sock.h
index 55b61e4b0d8318887d527e919fc1103d78ac6d14..84c21fb38a28406e387dc33e0fb5decd18893950 100644
--- a/include/net/sock.h
+++ b/include/net/sock.h
@@ -81,8 +81,13 @@
* mini-semaphore synchronizes multiple users amongst themselves.
*/
typedef struct {
- spinlock_t slock;
- int owned;
+ union {
+ struct slock_owned {
+ int owned;
+ spinlock_t slock;
+ };
+ long combined;
+ };
wait_queue_head_t wq;
/*
* We express the mutex-alike socket_lock semantics
diff --git a/net/core/sock.c b/net/core/sock.c
index cfb2a6209946089669882cdbd5d1b36c53838989..86185f194d64b4eea0835ee0f9b53fda184c73cb 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -3780,6 +3780,19 @@ void noinline lock_sock_nested(struct sock *sk, int subclass)
mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_);
might_sleep();
+#ifdef CONFIG_64BIT
+ if (sizeof(struct slock_owned) == sizeof(long)) {
+ socket_lock_t tmp, old;
+
+ tmp.slock = __SPIN_LOCK_UNLOCKED(tmp.slock);
+ tmp.owned = 1;
+ old.slock = __SPIN_LOCK_UNLOCKED(old.slock);
+ old.owned = 0;
+ if (likely(try_cmpxchg(&sk->sk_lock.combined,
+ &old.combined, tmp.combined)))
+ return;
+ }
+#endif
spin_lock_bh(&sk->sk_lock.slock);
if (unlikely(sock_owned_by_user_nocheck(sk)))
__lock_sock(sk);
--
2.53.0.414.gf7e9f6c205-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v2 net-next] net: use try_cmpxchg() in lock_sock_nested()
2026-02-26 2:12 [PATCH v2 net-next] net: use try_cmpxchg() in lock_sock_nested() Eric Dumazet
@ 2026-02-26 2:36 ` Jason Xing
2026-02-28 1:40 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 3+ messages in thread
From: Jason Xing @ 2026-02-26 2:36 UTC (permalink / raw)
To: Eric Dumazet
Cc: David S . Miller, Jakub Kicinski, Paolo Abeni, Simon Horman,
Kuniyuki Iwashima, netdev, eric.dumazet
On Thu, Feb 26, 2026 at 10:12 AM Eric Dumazet <edumazet@google.com> wrote:
>
> Add a fast path in lock_sock_nested(), to avoid acquiring
> the socket spinlock only to set @owned to one:
>
> spin_lock_bh(&sk->sk_lock.slock);
> if (unlikely(sock_owned_by_user_nocheck(sk)))
> __lock_sock(sk);
> sk->sk_lock.owned = 1;
> spin_unlock_bh(&sk->sk_lock.slock);
>
> On x86_64 compiler generates something quite efficient:
>
> 00000000000077c0 <lock_sock_nested>:
> 77c0: f3 0f 1e fa endbr64
> 77c4: e8 00 00 00 00 call __fentry__
> 77c9: b9 01 00 00 00 mov $0x1,%ecx
> 77ce: 31 c0 xor %eax,%eax
> 77d0: f0 48 0f b1 8f 48 01 00 00 lock cmpxchg %rcx,0x148(%rdi)
> 77d9: 75 06 jne slow_path
> 77db: 2e e9 00 00 00 00 cs jmp __x86_return_thunk-0x4
> slow_path: ...
>
> Signed-off-by: Eric Dumazet <edumazet@google.com>
> Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com>
I like this fast path approach. Thanks, Eric!
Reviewed-by: Jason Xing <kerneljasonxing@gmail.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2 net-next] net: use try_cmpxchg() in lock_sock_nested()
2026-02-26 2:12 [PATCH v2 net-next] net: use try_cmpxchg() in lock_sock_nested() Eric Dumazet
2026-02-26 2:36 ` Jason Xing
@ 2026-02-28 1:40 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-02-28 1:40 UTC (permalink / raw)
To: Eric Dumazet; +Cc: davem, kuba, pabeni, horms, kuniyu, netdev, eric.dumazet
Hello:
This patch was applied to netdev/net-next.git (main)
by Jakub Kicinski <kuba@kernel.org>:
On Thu, 26 Feb 2026 02:12:15 +0000 you wrote:
> Add a fast path in lock_sock_nested(), to avoid acquiring
> the socket spinlock only to set @owned to one:
>
> spin_lock_bh(&sk->sk_lock.slock);
> if (unlikely(sock_owned_by_user_nocheck(sk)))
> __lock_sock(sk);
> sk->sk_lock.owned = 1;
> spin_unlock_bh(&sk->sk_lock.slock);
>
> [...]
Here is the summary with links:
- [v2,net-next] net: use try_cmpxchg() in lock_sock_nested()
https://git.kernel.org/netdev/net-next/c/5151ec54f586
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-02-28 1:40 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-26 2:12 [PATCH v2 net-next] net: use try_cmpxchg() in lock_sock_nested() Eric Dumazet
2026-02-26 2:36 ` Jason Xing
2026-02-28 1:40 ` patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox