net: do not leave an empty skb in write queue
Subsystems (3)
PATCH v2 1/1 net: do not leave an empty skb in write queue
Under memory stress conditions, tcp_sendmsg_locked() might call sk_stream_wait_memory(), thus releasing the socket lock.
If a fresh skb has been allocated prior to this, we should not leave it in the write queue otherwise tcp_write_xmit() could panic.
This apparently does not happen often, but a future change in __sk_mem_raise_allocated() that Shakeel and others are considering would increase chances of being hurt.
Under discussion is to remove this controversial part:
/* Fail only if socket is _under_ its sndbuf.
* In this case we cannot block, so that we have to fail.
*/
if (sk->sk_wmem_queued + size >= sk->sk_sndbuf) {
/* Force charge with __GFP_NOFAIL */
if (memcg_charge && !charged) {
mem_cgroup_charge_skmem(sk->sk_memcg, amt,
gfp_memcg_charge() | __GFP_NOFAIL);
}
return 1;
}---
v2: call tcp_remove_empty_skb() before tcp_push()
net/ipv4/tcp.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
net/ipv4/tcp.c +5 -3
@@ -927,10 +927,11 @@ int tcp_send_mss(struct sock *sk, int *size_goal, int flags)return mss_now; } -/* In some cases, both sendmsg() could have added an skb to the write queue, -* but failed adding payload on it. We need to remove it to consume less +/* In some cases, sendmsg() could have added an skb to the write queue, +* but failed adding payload on it. We need to remove it to consume less * memory, but more importantly be able to generate EPOLLOUT for Edge Trigger -* epoll() users. +* epoll() users. Another reason is that tcp_write_xmit() does not like +* finding an empty skb in the write queue. */ void tcp_remove_empty_skb (struct sock *sk) {
@@ -1289,6 +1290,7 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size) wait_for_space:set_bit (SOCK_NOSPACE , &sk->sk_socket->flags); +tcp_remove_empty_skb (sk);if (copied)tcp_push (sk, flags & ~MSG_MORE , mss_now,TCP_NAGLE_PUSH , size_goal);
Signature · 1 line
2.42.0.655.g421f12c284-goog
+Abel Wu
Quoted 26 lines · Eric Dumazet
Under memory stress conditions,
tcp_sendmsg_locked()might callsk_stream_wait_memory(), thus releasing the socket lock.If a fresh skb has been allocated prior to this, we should not leave it in the write queue otherwise
tcp_write_xmit()could panic.This apparently does not happen often, but a future change in
__sk_mem_raise_allocated()that Shakeel and others are considering would increase chances of being hurt.Under discussion is to remove this controversial part:
/* Fail only if socket is _under_ its sndbuf. * In this case we cannot block, so that we have to fail. */ if (sk->sk_wmem_queued + size >= sk->sk_sndbuf) { /* Force charge with __GFP_NOFAIL */ if (memcg_charge && !charged) { mem_cgroup_charge_skmem(sk->sk_memcg, amt, gfp_memcg_charge() | __GFP_NOFAIL); } return 1; }
Quoted 3 lines
Fixes fdfc5c8594c2 ("tcp: remove empty skb from write queue in error cases")Signed-off-by Eric Dumazet <[email protected]>Cc Shakeel Butt <[email protected]>
Quoted 36 lines
---
v2: calltcp_remove_empty_skb()beforetcp_push()net/ipv4/tcp.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)net/ipv4/tcp.c +5
-4@@ -927,10 +927,11 @@ int tcp_send_mss(struct sock *sk, int *size_goal, int flags)return mss_now; } -/* In some cases, both sendmsg() could have added an skb to the write queue, -* but failed adding payload on it. We need to remove it to consume less +/* In some cases, sendmsg() could have added an skb to the write queue, +* but failed adding payload on it. We need to remove it to consume less * memory, but more importantly be able to generate EPOLLOUT for Edge Trigger -* epoll() users. +* epoll() users. Another reason is that tcp_write_xmit() does not like +* finding an empty skb in the write queue. */ void tcp_remove_empty_skb (struct sock *sk) {@@ -1289,6 +1290,7 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size) wait_for_space:set_bit (SOCK_NOSPACE , &sk->sk_socket->flags); +tcp_remove_empty_skb (sk);if (copied)tcp_push (sk, flags & ~MSG_MORE , mss_now,TCP_NAGLE_PUSH , size_goal); --2.42.0.655.g421f12c284-goog
Quoted 11 lines · Shakeel Butt
+Abel Wu
Quoted 7 lines · Eric Dumazet
Under memory stress conditions,
tcp_sendmsg_locked()might callsk_stream_wait_memory(), thus releasing the socket lock.If a fresh skb has been allocated prior to this, we should not leave it in the write queue otherwise
tcp_write_xmit()could panic.
Eric, do you have a panic trace accidentally? Thanks
Quoted 62 lines
Quoted 19 lines
This apparently does not happen often, but a future change in
__sk_mem_raise_allocated()that Shakeel and others are considering would increase chances of being hurt.Under discussion is to remove this controversial part:
/* Fail only if socket is _under_ its sndbuf. * In this case we cannot block, so that we have to fail. */ if (sk->sk_wmem_queued + size >= sk->sk_sndbuf) { /* Force charge with __GFP_NOFAIL */ if (memcg_charge && !charged) { mem_cgroup_charge_skmem(sk->sk_memcg, amt, gfp_memcg_charge() | __GFP_NOFAIL); } return 1; }Quoted 3 lines
Fixes fdfc5c8594c2 ("tcp: remove empty skb from write queue in error cases")Signed-off-by Eric Dumazet <[email protected]>Cc Shakeel Butt <[email protected]>Reviewed-by Shakeel Butt <[email protected]>Quoted 36 lines
---
v2: calltcp_remove_empty_skb()beforetcp_push()net/ipv4/tcp.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)net/ipv4/tcp.c +5
-4@@ -927,10 +927,11 @@ int tcp_send_mss(struct sock *sk, int *size_goal, int flags)return mss_now; } -/* In some cases, both sendmsg() could have added an skb to the write queue, -* but failed adding payload on it. We need to remove it to consume less +/* In some cases, sendmsg() could have added an skb to the write queue, +* but failed adding payload on it. We need to remove it to consume less * memory, but more importantly be able to generate EPOLLOUT for Edge Trigger -* epoll() users. +* epoll() users. Another reason is that tcp_write_xmit() does not like +* finding an empty skb in the write queue. */ void tcp_remove_empty_skb (struct sock *sk) {@@ -1289,6 +1290,7 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size) wait_for_space:set_bit (SOCK_NOSPACE , &sk->sk_socket->flags); +tcp_remove_empty_skb (sk);if (copied)tcp_push (sk, flags & ~MSG_MORE , mss_now,TCP_NAGLE_PUSH , size_goal); --2.42.0.655.g421f12c284-goog
Quoted 7 lines · Dmitry Kravkov
Quoted 5 lines · Shakeel Butt
+Abel Wu
Quoted 1 line · Eric Dumazet
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Under memory stress conditions,
tcp_sendmsg_locked()might callsk_stream_wait_memory(), thus releasing the socket lock.If a fresh skb has been allocated prior to this, we should not leave it in the write queue otherwise
tcp_write_xmit()could panic.Eric, do you have a panic trace accidentally? Thanks
I have no panic yet. It would be a bit tricky to trigger I think, but a bit of clever fault injection could do this.
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 8 lines · On Thu, 19 Oct 2023 11:24:57 +0000 you wrote:
Under memory stress conditions,
tcp_sendmsg_locked()might callsk_stream_wait_memory(), thus releasing the socket lock.If a fresh skb has been allocated prior to this, we should not leave it in the write queue otherwise
tcp_write_xmit()could panic.[...]
Here is the summary with links:
- [v2,net] net: do not leave an empty skb in write queue
https://git.kernel.org/netdev/net/c/72bf4f1767f0
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html