Skip to contentSkip to conversation

sctp: fix busy polling

PATCH Applied Eric Dumazet 3 participants 8 messages Dec 19, 2023 – Jan 4, 2024

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

PATCH 1/1 sctp: fix busy polling

Busy polling while holding the socket lock makes litle sense, because incoming packets wont reach our receive queue.

Fixes 8465a5fcd1ce ("sctp: add support for busy polling to sctp protocol")
Cc Marcelo Ricardo Leitner <[email protected]>
Cc Xin Long <[email protected]>
---
 net/sctp/socket.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
net/sctp/socket.c +4 -6
index 5fb02bbb4b349ef9ab9c2790cccb30fb4c4e897c..6b9fcdb0952a0fe599ae5d1d1cc6fa9557a3a3bc 100644
@@ -2102,6 +2102,10 @@ static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 	if (unlikely(flags & MSG_ERRQUEUE))
 		return inet_recv_error(sk, msg, len, addr_len);
 
+	if (sk_can_busy_loop(sk) &&
+	    skb_queue_empty_lockless(&sk->sk_receive_queue))
+		sk_busy_loop(sk, flags & MSG_DONTWAIT);
+
 	lock_sock(sk);
 
 	if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
@@ -9046,12 +9050,6 @@ struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags, int *err)
 		if (sk->sk_shutdown & RCV_SHUTDOWN)
 			break;
 
-		if (sk_can_busy_loop(sk)) {
-			sk_busy_loop(sk, flags & MSG_DONTWAIT);
-
-			if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
-				continue;
-		}
 
 		/* User doesn't want to wait.  */
 		error = -EAGAIN;
Signature · 1 line
2.43.0.472.g3155946c3a-goog
maintainer
#1 lore raw
Quoted 18 lines · Eric Dumazet

Busy polling while holding the socket lock makes litle sense, because incoming packets wont reach our receive queue.

Fixes 8465a5fcd1ce ("sctp: add support for busy polling to sctp protocol")
Cc Marcelo Ricardo Leitner <[email protected]>
Cc Xin Long <[email protected]>
---
 net/sctp/socket.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
net/sctp/socket.c +0 -0
index 5fb02bbb4b349ef9ab9c2790cccb30fb4c4e897c..6b9fcdb0952a0fe599ae5d1d1cc6fa9557a3a3bc 100644

net/sctp/socket.c static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, · line 2108

Quoted 7 lines
net/sctp/socket.c +4 -0
@@ -2102,6 +2102,10 @@ static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
        if (unlikely(flags & MSG_ERRQUEUE))
                return inet_recv_error(sk, msg, len, addr_len);
 
+       if (sk_can_busy_loop(sk) &&
+           skb_queue_empty_lockless(&sk->sk_receive_queue))
+               sk_busy_loop(sk, flags & MSG_DONTWAIT);
+

Here is no any sk_state check, if the SCTP socket(TCP type) has been already closed by peer, will sctp_recvmsg() block here?

Maybe here it needs a !(sk->sk_shutdown & RCV_SHUTDOWN) check, which is set when it's closed by the peer.

Thanks

Quoted 19 lines
net/sctp/socket.c +0 -7
        lock_sock(sk);
 
        if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
@@ -9046,12 +9050,6 @@ struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags, int *err)
                if (sk->sk_shutdown & RCV_SHUTDOWN)
                        break;
 
-               if (sk_can_busy_loop(sk)) {
-                       sk_busy_loop(sk, flags & MSG_DONTWAIT);
-
-                       if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
-                               continue;
-               }
 
                /* User doesn't want to wait.  */
                error = -EAGAIN;
--

2.43.0.472.g3155946c3a-goog

#2 lore raw
Quoted 23 lines · Xin Long
Quoted 21 lines · Eric Dumazet

Busy polling while holding the socket lock makes litle sense, because incoming packets wont reach our receive queue.

Fixes 8465a5fcd1ce ("sctp: add support for busy polling to sctp protocol")
Cc Marcelo Ricardo Leitner <[email protected]>
Cc Xin Long <[email protected]>
---
 net/sctp/socket.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
net/sctp/socket.c +0 -0
index 5fb02bbb4b349ef9ab9c2790cccb30fb4c4e897c..6b9fcdb0952a0fe599ae5d1d1cc6fa9557a3a3bc 100644
@@ -2102,6 +2102,10 @@ static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
        if (unlikely(flags & MSG_ERRQUEUE))
                return inet_recv_error(sk, msg, len, addr_len);
 
Quoted 6 lines

net/sctp/socket.c static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, · line 2108

Quoted 4 lines
net/sctp/socket.c +4 -0
+       if (sk_can_busy_loop(sk) &&
+           skb_queue_empty_lockless(&sk->sk_receive_queue))
+               sk_busy_loop(sk, flags & MSG_DONTWAIT);
+

Here is no any sk_state check, if the SCTP socket(TCP type) has been already closed by peer, will sctp_recvmsg() block here?

Busy polling is only polling the NIC queue, hoping to feed this socket for incoming packets.

Using more than a lockless read of sk->sk_receive_queue is not really necessary, and racy anyway.

Eliezer Tamir added a check against sk_state for no good reason in TCP, my plan is to remove it.

There are other states where it still makes sense to allow busy polling.

Quoted 3 lines

Maybe here it needs a !(sk->sk_shutdown & RCV_SHUTDOWN) check, which is set when it's closed by the peer.

See above. Keep this as simple as possible...

Quoted 22 lines

Thanks

Quoted 19 lines
net/sctp/socket.c +0 -7
        lock_sock(sk);
 
        if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
@@ -9046,12 +9050,6 @@ struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags, int *err)
                if (sk->sk_shutdown & RCV_SHUTDOWN)
                        break;
 
-               if (sk_can_busy_loop(sk)) {
-                       sk_busy_loop(sk, flags & MSG_DONTWAIT);
-
-                       if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
-                               continue;
-               }
 
                /* User doesn't want to wait.  */
                error = -EAGAIN;
--

2.43.0.472.g3155946c3a-goog

maintainer
#3 lore raw
Quoted 32 lines · Eric Dumazet
Quoted 23 lines · Xin Long
Quoted 21 lines · Eric Dumazet

Busy polling while holding the socket lock makes litle sense, because incoming packets wont reach our receive queue.

Fixes 8465a5fcd1ce ("sctp: add support for busy polling to sctp protocol")
Cc Marcelo Ricardo Leitner <[email protected]>
Cc Xin Long <[email protected]>
---
 net/sctp/socket.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
net/sctp/socket.c +0 -0
index 5fb02bbb4b349ef9ab9c2790cccb30fb4c4e897c..6b9fcdb0952a0fe599ae5d1d1cc6fa9557a3a3bc 100644
@@ -2102,6 +2102,10 @@ static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
        if (unlikely(flags & MSG_ERRQUEUE))
                return inet_recv_error(sk, msg, len, addr_len);
 
Quoted 6 lines

net/sctp/socket.c static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, · line 2108

Quoted 4 lines
net/sctp/socket.c +4 -0
+       if (sk_can_busy_loop(sk) &&
+           skb_queue_empty_lockless(&sk->sk_receive_queue))
+               sk_busy_loop(sk, flags & MSG_DONTWAIT);
+

Here is no any sk_state check, if the SCTP socket(TCP type) has been already closed by peer, will sctp_recvmsg() block here?

Quoted 2 lines

Busy polling is only polling the NIC queue, hoping to feed this socket for incoming packets.

OK, will it block if there's no incoming packets on the NIC queue?

If yes, when sysctl net.core.busy_read=1, my concern is:

     client                server
     -------------------------------
                           listen()
     connect()
                           accept()
     close()
                           recvmsg() <----

recvmsg() is supposed to return right away as the connection is already close(). With this patch, will recvmsg() be able to do that if no more incoming packets in the NIC after close()?

Thanks.

Quoted 39 lines

Using more than a lockless read of sk->sk_receive_queue is not really necessary, and racy anyway.

Eliezer Tamir added a check against sk_state for no good reason in TCP, my plan is to remove it.

There are other states where it still makes sense to allow busy polling.

Quoted 3 lines

Maybe here it needs a !(sk->sk_shutdown & RCV_SHUTDOWN) check, which is set when it's closed by the peer.

See above. Keep this as simple as possible...

Quoted 22 lines

Thanks

Quoted 19 lines
net/sctp/socket.c +0 -7
        lock_sock(sk);
 
        if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
@@ -9046,12 +9050,6 @@ struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags, int *err)
                if (sk->sk_shutdown & RCV_SHUTDOWN)
                        break;
 
-               if (sk_can_busy_loop(sk)) {
-                       sk_busy_loop(sk, flags & MSG_DONTWAIT);
-
-                       if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
-                               continue;
-               }
 
                /* User doesn't want to wait.  */
                error = -EAGAIN;
--

2.43.0.472.g3155946c3a-goog

#4 lore raw
Quoted 48 lines · Xin Long
Quoted 32 lines · Eric Dumazet
Quoted 23 lines · Xin Long
Quoted 21 lines · Eric Dumazet

Busy polling while holding the socket lock makes litle sense, because incoming packets wont reach our receive queue.

Fixes 8465a5fcd1ce ("sctp: add support for busy polling to sctp protocol")
Cc Marcelo Ricardo Leitner <[email protected]>
Cc Xin Long <[email protected]>
---
 net/sctp/socket.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
net/sctp/socket.c +0 -0
index 5fb02bbb4b349ef9ab9c2790cccb30fb4c4e897c..6b9fcdb0952a0fe599ae5d1d1cc6fa9557a3a3bc 100644
@@ -2102,6 +2102,10 @@ static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
        if (unlikely(flags & MSG_ERRQUEUE))
                return inet_recv_error(sk, msg, len, addr_len);
 
Quoted 6 lines

net/sctp/socket.c static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, · line 2108

Quoted 4 lines
net/sctp/socket.c +4 -0
+       if (sk_can_busy_loop(sk) &&
+           skb_queue_empty_lockless(&sk->sk_receive_queue))
+               sk_busy_loop(sk, flags & MSG_DONTWAIT);
+

Here is no any sk_state check, if the SCTP socket(TCP type) has been already closed by peer, will sctp_recvmsg() block here?

Quoted 2 lines

Busy polling is only polling the NIC queue, hoping to feed this socket for incoming packets.

OK, will it block if there's no incoming packets on the NIC queue?

If yes, when sysctl net.core.busy_read=1, my concern is:

     client                server
     -------------------------------
                           listen()
     connect()
                           accept()
     close()
                           recvmsg() <----
Quoted 3 lines

recvmsg() is supposed to return right away as the connection is already close(). With this patch, will recvmsg() be able to do that if no more incoming packets in the NIC after close()?

Answer is yes for a variety of reasons :

net.core.busy_read=1 means :

Busy poll will happen for
1) at most one usec, and
2) as long as there is no packet in sk->sk_receive_queue (see sk_busy_loop_end())

But busy poll is only started on sockets that had established packets.

A listener will not engage this because sk->sk_napi_id does not contain a valid NAPI ID.

Quoted 42 lines

Thanks.

Quoted 39 lines

Using more than a lockless read of sk->sk_receive_queue is not really necessary, and racy anyway.

Eliezer Tamir added a check against sk_state for no good reason in TCP, my plan is to remove it.

There are other states where it still makes sense to allow busy polling.

Quoted 3 lines

Maybe here it needs a !(sk->sk_shutdown & RCV_SHUTDOWN) check, which is set when it's closed by the peer.

See above. Keep this as simple as possible...

Quoted 22 lines

Thanks

Quoted 19 lines
net/sctp/socket.c +0 -7
        lock_sock(sk);
 
        if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
@@ -9046,12 +9050,6 @@ struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags, int *err)
                if (sk->sk_shutdown & RCV_SHUTDOWN)
                        break;
 
-               if (sk_can_busy_loop(sk)) {
-                       sk_busy_loop(sk, flags & MSG_DONTWAIT);
-
-                       if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
-                               continue;
-               }
 
                /* User doesn't want to wait.  */
                error = -EAGAIN;
--

2.43.0.472.g3155946c3a-goog

maintainer
#5 lore raw
Quoted 55 lines · Eric Dumazet
Quoted 48 lines · Xin Long
Quoted 32 lines · Eric Dumazet
Quoted 23 lines · Xin Long
Quoted 21 lines · Eric Dumazet

Busy polling while holding the socket lock makes litle sense, because incoming packets wont reach our receive queue.

Fixes 8465a5fcd1ce ("sctp: add support for busy polling to sctp protocol")
Cc Marcelo Ricardo Leitner <[email protected]>
Cc Xin Long <[email protected]>
---
 net/sctp/socket.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
net/sctp/socket.c +0 -0
index 5fb02bbb4b349ef9ab9c2790cccb30fb4c4e897c..6b9fcdb0952a0fe599ae5d1d1cc6fa9557a3a3bc 100644
@@ -2102,6 +2102,10 @@ static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
        if (unlikely(flags & MSG_ERRQUEUE))
                return inet_recv_error(sk, msg, len, addr_len);
 
Quoted 6 lines

net/sctp/socket.c static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, · line 2108

Quoted 4 lines
net/sctp/socket.c +4 -0
+       if (sk_can_busy_loop(sk) &&
+           skb_queue_empty_lockless(&sk->sk_receive_queue))
+               sk_busy_loop(sk, flags & MSG_DONTWAIT);
+

Here is no any sk_state check, if the SCTP socket(TCP type) has been already closed by peer, will sctp_recvmsg() block here?

Quoted 2 lines

Busy polling is only polling the NIC queue, hoping to feed this socket for incoming packets.

OK, will it block if there's no incoming packets on the NIC queue?

If yes, when sysctl net.core.busy_read=1, my concern is:

     client                server
     -------------------------------
                           listen()
     connect()
                           accept()
     close()
                           recvmsg() <----
Quoted 3 lines

recvmsg() is supposed to return right away as the connection is already close(). With this patch, will recvmsg() be able to do that if no more incoming packets in the NIC after close()?

Quoted 6 lines

Answer is yes for a variety of reasons :

net.core.busy_read=1 means :

Busy poll will happen for
1) at most one usec, and

I see, never used busy polling, but what if the value is set to a large value, like minutes, I might be just overthinking and no one will do this?

Quoted 2 lines

2) as long as there is no packet in sk->sk_receive_queue (see sk_busy_loop_end())

It's likely after being closed by peer, no packet at sk_receive_queue.

Quoted 2 lines

But busy poll is only started on sockets that had established packets.

I think it won't be told to break when the socket is closed by peer.

Quoted 48 lines

A listener will not engage this because sk->sk_napi_id does not contain a valid NAPI ID.

Quoted 42 lines

Thanks.

Quoted 39 lines

Using more than a lockless read of sk->sk_receive_queue is not really necessary, and racy anyway.

Eliezer Tamir added a check against sk_state for no good reason in TCP, my plan is to remove it.

There are other states where it still makes sense to allow busy polling.

Quoted 3 lines

Maybe here it needs a !(sk->sk_shutdown & RCV_SHUTDOWN) check, which is set when it's closed by the peer.

See above. Keep this as simple as possible...

Quoted 22 lines

Thanks

Quoted 19 lines
net/sctp/socket.c +0 -7
        lock_sock(sk);
 
        if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
@@ -9046,12 +9050,6 @@ struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags, int *err)
                if (sk->sk_shutdown & RCV_SHUTDOWN)
                        break;
 
-               if (sk_can_busy_loop(sk)) {
-                       sk_busy_loop(sk, flags & MSG_DONTWAIT);
-
-                       if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
-                               continue;
-               }
 
                /* User doesn't want to wait.  */
                error = -EAGAIN;
--

2.43.0.472.g3155946c3a-goog

#6 lore raw
Quoted 57 lines · Xin Long
Quoted 55 lines · Eric Dumazet
Quoted 51 lines · Xin Long
Quoted 32 lines · Eric Dumazet
Quoted 23 lines · Xin Long
Quoted 21 lines · Eric Dumazet

Busy polling while holding the socket lock makes litle sense, because incoming packets wont reach our receive queue.

Fixes 8465a5fcd1ce ("sctp: add support for busy polling to sctp protocol")
Cc Marcelo Ricardo Leitner <[email protected]>
Cc Xin Long <[email protected]>
---
 net/sctp/socket.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
net/sctp/socket.c +0 -0
index 5fb02bbb4b349ef9ab9c2790cccb30fb4c4e897c..6b9fcdb0952a0fe599ae5d1d1cc6fa9557a3a3bc 100644
@@ -2102,6 +2102,10 @@ static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
        if (unlikely(flags & MSG_ERRQUEUE))
                return inet_recv_error(sk, msg, len, addr_len);
 
Quoted 6 lines

net/sctp/socket.c static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, · line 2108

Quoted 4 lines
net/sctp/socket.c +4 -0
+       if (sk_can_busy_loop(sk) &&
+           skb_queue_empty_lockless(&sk->sk_receive_queue))
+               sk_busy_loop(sk, flags & MSG_DONTWAIT);
+

Here is no any sk_state check, if the SCTP socket(TCP type) has been already closed by peer, will sctp_recvmsg() block here?

Quoted 2 lines

Busy polling is only polling the NIC queue, hoping to feed this socket for incoming packets.

OK, will it block if there's no incoming packets on the NIC queue?

If yes, when sysctl net.core.busy_read=1, my concern is:

     client                server
     -------------------------------
                           listen()
     connect()
                           accept()
     close()
                           recvmsg() <----

recvmsg() is supposed to return right away as the connection is already close(). With this patch, will recvmsg() be able to do that if no more incoming packets in the NIC after close()?

Quoted 9 lines
Quoted 6 lines

Answer is yes for a variety of reasons :

net.core.busy_read=1 means :

Busy poll will happen for
1) at most one usec, and

I see, never used busy polling, but what if the value is set to a large value, like minutes, I might be just overthinking and no one will do this?

No problem, you can look at
https://netdevconf.info/2.1/papers/BusyPollingNextGen.pdf for a short introduction.

<quote>
Suggested settings are in the 50 to 100 us range
</quote>

Quoted 7 lines
Quoted 2 lines

2) as long as there is no packet in sk->sk_receive_queue (see sk_busy_loop_end())

It's likely after being closed by peer, no packet at sk_receive_queue.

Quoted 2 lines

But busy poll is only started on sockets that had established packets.

I think it won't be told to break when the socket is closed by peer.

This is fine really.

sk_busy_loop_end() works fine as is for UDP/TCP sockets, and it does not look at sk_state.

Keep in mind polling applications are using recvmsg() 20,000 times per second, there is no point trying to optimize the last call.

Quoted 49 lines
Quoted 49 lines

A listener will not engage this because sk->sk_napi_id does not contain a valid NAPI ID.

Quoted 42 lines

Thanks.

Quoted 39 lines

Using more than a lockless read of sk->sk_receive_queue is not really necessary, and racy anyway.

Eliezer Tamir added a check against sk_state for no good reason in TCP, my plan is to remove it.

There are other states where it still makes sense to allow busy polling.

Quoted 3 lines

Maybe here it needs a !(sk->sk_shutdown & RCV_SHUTDOWN) check, which is set when it's closed by the peer.

See above. Keep this as simple as possible...

Quoted 22 lines

Thanks

Quoted 19 lines
net/sctp/socket.c +0 -7
        lock_sock(sk);
 
        if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
@@ -9046,12 +9050,6 @@ struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags, int *err)
                if (sk->sk_shutdown & RCV_SHUTDOWN)
                        break;
 
-               if (sk_can_busy_loop(sk)) {
-                       sk_busy_loop(sk, flags & MSG_DONTWAIT);
-
-                       if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
-                               continue;
-               }
 
                /* User doesn't want to wait.  */
                error = -EAGAIN;
--

2.43.0.472.g3155946c3a-goog

#7 lore raw
In reply to Eric Dumazet
This patch was applied to netdev/net-next.git (main) by David S. Miller <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (main) by David S. Miller <[email protected]>:

Quoted 10 lines · On Tue, 19 Dec 2023 17:00:17 +0000 you wrote:

Busy polling while holding the socket lock makes litle sense, because incoming packets wont reach our receive queue.

Fixes 8465a5fcd1ce ("sctp: add support for busy polling to sctp protocol")
Cc Marcelo Ricardo Leitner <[email protected]>
Cc Xin Long <[email protected]>

[...]

Here is the summary with links:
- [net-next] sctp: fix busy polling

    https://git.kernel.org/netdev/net-next/c/a562c0a2d651

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html