Skip to content

Commit cd1263b

Browse files
author
Jakub Kicinski
committed
Merge branch 'tcp-take-care-of-empty-skbs-in-write-queue'
Eric Dumazet says: ==================== tcp: take care of empty skbs in write queue We understood recently that TCP sockets could have an empty skb at the tail of the write queue, leading to various problems. This patch series : 1) Make sure we do not send an empty packet since this was unintended and causing crashes in old kernels. 2) Change tcp_write_queue_empty() to not be fooled by the presence of an empty skb. 3) Fix a bug that could trigger suboptimal epoll() application behavior under memory pressure. ==================== Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com>
2 parents 5c9934b + 216808c commit cd1263b

3 files changed

Lines changed: 23 additions & 7 deletions

File tree

‎include/net/tcp.h‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1766,9 +1766,18 @@ static inline bool tcp_skb_is_last(const struct sock *sk,
17661766
return skb_queue_is_last(&sk->sk_write_queue, skb);
17671767
}
17681768

1769+
/**
1770+
* tcp_write_queue_empty - test if any payload (or FIN) is available in write queue
1771+
* @sk: socket
1772+
*
1773+
* Since the write queue can have a temporary empty skb in it,
1774+
* we must not use "return skb_queue_empty(&sk->sk_write_queue)"
1775+
*/
17691776
static inline bool tcp_write_queue_empty(const struct sock *sk)
17701777
{
1771-
return skb_queue_empty(&sk->sk_write_queue);
1778+
const struct tcp_sock *tp = tcp_sk(sk);
1779+
1780+
return tp->write_seq == tp->snd_nxt;
17721781
}
17731782

17741783
static inline bool tcp_rtx_queue_empty(const struct sock *sk)

‎net/ipv4/tcp.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,8 +1087,7 @@ ssize_t do_tcp_sendpages(struct sock *sk, struct page *page, int offset,
10871087
goto out;
10881088
out_err:
10891089
/* make sure we wake any epoll edge trigger waiter */
1090-
if (unlikely(skb_queue_len(&sk->sk_write_queue) == 0 &&
1091-
err == -EAGAIN)) {
1090+
if (unlikely(tcp_rtx_and_write_queues_empty(sk) && err == -EAGAIN)) {
10921091
sk->sk_write_space(sk);
10931092
tcp_chrono_stop(sk, TCP_CHRONO_SNDBUF_LIMITED);
10941093
}
@@ -1419,8 +1418,7 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)
14191418
sock_zerocopy_put_abort(uarg, true);
14201419
err = sk_stream_error(sk, flags, err);
14211420
/* make sure we wake any epoll edge trigger waiter */
1422-
if (unlikely(skb_queue_len(&sk->sk_write_queue) == 0 &&
1423-
err == -EAGAIN)) {
1421+
if (unlikely(tcp_rtx_and_write_queues_empty(sk) && err == -EAGAIN)) {
14241422
sk->sk_write_space(sk);
14251423
tcp_chrono_stop(sk, TCP_CHRONO_SNDBUF_LIMITED);
14261424
}

‎net/ipv4/tcp_output.c‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,6 +2438,14 @@ static bool tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle,
24382438
if (tcp_small_queue_check(sk, skb, 0))
24392439
break;
24402440

2441+
/* Argh, we hit an empty skb(), presumably a thread
2442+
* is sleeping in sendmsg()/sk_stream_wait_memory().
2443+
* We do not want to send a pure-ack packet and have
2444+
* a strange looking rtx queue with empty packet(s).
2445+
*/
2446+
if (TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq)
2447+
break;
2448+
24412449
if (unlikely(tcp_transmit_skb(sk, skb, 1, gfp)))
24422450
break;
24432451

@@ -3121,22 +3129,23 @@ void sk_forced_mem_schedule(struct sock *sk, int size)
31213129
*/
31223130
void tcp_send_fin(struct sock *sk)
31233131
{
3124-
struct sk_buff *skb, *tskb = tcp_write_queue_tail(sk);
3132+
struct sk_buff *skb, *tskb, *tail = tcp_write_queue_tail(sk);
31253133
struct tcp_sock *tp = tcp_sk(sk);
31263134

31273135
/* Optimization, tack on the FIN if we have one skb in write queue and
31283136
* this skb was not yet sent, or we are under memory pressure.
31293137
* Note: in the latter case, FIN packet will be sent after a timeout,
31303138
* as TCP stack thinks it has already been transmitted.
31313139
*/
3140+
tskb = tail;
31323141
if (!tskb && tcp_under_memory_pressure(sk))
31333142
tskb = skb_rb_last(&sk->tcp_rtx_queue);
31343143

31353144
if (tskb) {
31363145
TCP_SKB_CB(tskb)->tcp_flags |= TCPHDR_FIN;
31373146
TCP_SKB_CB(tskb)->end_seq++;
31383147
tp->write_seq++;
3139-
if (tcp_write_queue_empty(sk)) {
3148+
if (!tail) {
31403149
/* This means tskb was already sent.
31413150
* Pretend we included the FIN on previous transmit.
31423151
* We need to set tp->snd_nxt to the value it would have

0 commit comments

Comments
 (0)