Skip to content

Commit b97af72

Browse files
committed
Merge branch 'UDP-sock_wfree-opts'
Pavel Begunkov says: ==================== UDP sock_wfree optimisations The series is not UDP specific but that the main beneficiary. 2/3 saves one atomic in sock_wfree() and on top 3/3 removes an extra barrier. Tested with UDP over dummy netdev, 2038491 -> 2099071 req/s (or around +3%). note: in regards to 1/3, there is a "Should agree with poll..." comment that I don't completely get, and there is no git history to explain it. Though I can't see how it could rely on having the second check without racing with tasks woken by wake_up*(). The series was split from a larger patchset, see https://lore.kernel.org/netdev/[email protected]/ ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents 0ed99ec + 0a8afd9 commit b97af72

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

net/core/sock.c

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@
146146
static DEFINE_MUTEX(proto_list_mutex);
147147
static LIST_HEAD(proto_list);
148148

149+
static void sock_def_write_space_wfree(struct sock *sk);
150+
static void sock_def_write_space(struct sock *sk);
151+
149152
/**
150153
* sk_ns_capable - General socket capability test
151154
* @sk: Socket to use a capability on or through
@@ -2324,8 +2327,20 @@ void sock_wfree(struct sk_buff *skb)
23242327
{
23252328
struct sock *sk = skb->sk;
23262329
unsigned int len = skb->truesize;
2330+
bool free;
23272331

23282332
if (!sock_flag(sk, SOCK_USE_WRITE_QUEUE)) {
2333+
if (sock_flag(sk, SOCK_RCU_FREE) &&
2334+
sk->sk_write_space == sock_def_write_space) {
2335+
rcu_read_lock();
2336+
free = refcount_sub_and_test(len, &sk->sk_wmem_alloc);
2337+
sock_def_write_space_wfree(sk);
2338+
rcu_read_unlock();
2339+
if (unlikely(free))
2340+
__sk_free(sk);
2341+
return;
2342+
}
2343+
23292344
/*
23302345
* Keep a reference on sk_wmem_alloc, this will be released
23312346
* after sk_write_space() call
@@ -3191,20 +3206,42 @@ static void sock_def_write_space(struct sock *sk)
31913206
/* Do not wake up a writer until he can make "significant"
31923207
* progress. --DaveM
31933208
*/
3194-
if ((refcount_read(&sk->sk_wmem_alloc) << 1) <= READ_ONCE(sk->sk_sndbuf)) {
3209+
if (sock_writeable(sk)) {
31953210
wq = rcu_dereference(sk->sk_wq);
31963211
if (skwq_has_sleeper(wq))
31973212
wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
31983213
EPOLLWRNORM | EPOLLWRBAND);
31993214

32003215
/* Should agree with poll, otherwise some programs break */
3201-
if (sock_writeable(sk))
3202-
sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
3216+
sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
32033217
}
32043218

32053219
rcu_read_unlock();
32063220
}
32073221

3222+
/* An optimised version of sock_def_write_space(), should only be called
3223+
* for SOCK_RCU_FREE sockets under RCU read section and after putting
3224+
* ->sk_wmem_alloc.
3225+
*/
3226+
static void sock_def_write_space_wfree(struct sock *sk)
3227+
{
3228+
/* Do not wake up a writer until he can make "significant"
3229+
* progress. --DaveM
3230+
*/
3231+
if (sock_writeable(sk)) {
3232+
struct socket_wq *wq = rcu_dereference(sk->sk_wq);
3233+
3234+
/* rely on refcount_sub from sock_wfree() */
3235+
smp_mb__after_atomic();
3236+
if (wq && waitqueue_active(&wq->wait))
3237+
wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
3238+
EPOLLWRNORM | EPOLLWRBAND);
3239+
3240+
/* Should agree with poll, otherwise some programs break */
3241+
sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
3242+
}
3243+
}
3244+
32083245
static void sock_def_destruct(struct sock *sk)
32093246
{
32103247
}

0 commit comments

Comments
 (0)