Commit 82241580 authored by Eric Dumazet's avatar Eric Dumazet Committed by Ben Hutchings

tcp: avoid looping in tcp_send_fin()

[ Upstream commit 845704a5 ]

Presence of an unbound loop in tcp_send_fin() had always been hard
to explain when analyzing crash dumps involving gigantic dying processes
with millions of sockets.

Lets try a different strategy :

In case of memory pressure, try to add the FIN flag to last packet
in write queue, even if packet was already sent. TCP stack will
be able to deliver this FIN after a timeout event. Note that this
FIN being delivered by a retransmit, it also carries a Push flag
given our current implementation.

By checking sk_under_memory_pressure(), we anticipate that cooking
many FIN packets might deplete tcp memory.

In the case we could not allocate a packet, even with __GFP_WAIT
allocation, then not sending a FIN seems quite reasonable if it allows
to get rid of this socket, free memory, and not block the process from
eventually doing other useful work.
Signed-off-by: default avatarEric Dumazet <edumazet@google.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
[bwh: Backported to 3.2:
 - Drop inapplicable change to sk_forced_wmem_schedule()
 - s/sk_under_memory_pressure(sk)/tcp_memory_pressure/]
Signed-off-by: default avatarBen Hutchings <ben@decadent.org.uk>
parent fccb908d
...@@ -2320,33 +2320,40 @@ void tcp_xmit_retransmit_queue(struct sock *sk) ...@@ -2320,33 +2320,40 @@ void tcp_xmit_retransmit_queue(struct sock *sk)
} }
} }
/* Send a fin. The caller locks the socket for us. This cannot be /* Send a FIN. The caller locks the socket for us.
* allowed to fail queueing a FIN frame under any circumstances. * We should try to send a FIN packet really hard, but eventually give up.
*/ */
void tcp_send_fin(struct sock *sk) void tcp_send_fin(struct sock *sk)
{ {
struct sk_buff *skb, *tskb = tcp_write_queue_tail(sk);
struct tcp_sock *tp = tcp_sk(sk); struct tcp_sock *tp = tcp_sk(sk);
struct sk_buff *skb = tcp_write_queue_tail(sk);
int mss_now;
/* Optimization, tack on the FIN if we have a queue of /* Optimization, tack on the FIN if we have one skb in write queue and
* unsent frames. But be careful about outgoing SACKS * this skb was not yet sent, or we are under memory pressure.
* and IP options. * Note: in the latter case, FIN packet will be sent after a timeout,
* as TCP stack thinks it has already been transmitted.
*/ */
mss_now = tcp_current_mss(sk); if (tskb && (tcp_send_head(sk) || tcp_memory_pressure)) {
coalesce:
if (tcp_send_head(sk) != NULL) { TCP_SKB_CB(tskb)->tcp_flags |= TCPHDR_FIN;
TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_FIN; TCP_SKB_CB(tskb)->end_seq++;
TCP_SKB_CB(skb)->end_seq++;
tp->write_seq++; tp->write_seq++;
if (!tcp_send_head(sk)) {
/* This means tskb was already sent.
* Pretend we included the FIN on previous transmit.
* We need to set tp->snd_nxt to the value it would have
* if FIN had been sent. This is because retransmit path
* does not change tp->snd_nxt.
*/
tp->snd_nxt++;
return;
}
} else { } else {
/* Socket is locked, keep trying until memory is available. */ skb = alloc_skb_fclone(MAX_TCP_HEADER, sk->sk_allocation);
for (;;) { if (unlikely(!skb)) {
skb = alloc_skb_fclone(MAX_TCP_HEADER, if (tskb)
sk->sk_allocation); goto coalesce;
if (skb) return;
break;
yield();
} }
/* Reserve space for headers and prepare control bits. */ /* Reserve space for headers and prepare control bits. */
...@@ -2356,7 +2363,7 @@ void tcp_send_fin(struct sock *sk) ...@@ -2356,7 +2363,7 @@ void tcp_send_fin(struct sock *sk)
TCPHDR_ACK | TCPHDR_FIN); TCPHDR_ACK | TCPHDR_FIN);
tcp_queue_skb(sk, skb); tcp_queue_skb(sk, skb);
} }
__tcp_push_pending_frames(sk, mss_now, TCP_NAGLE_OFF); __tcp_push_pending_frames(sk, tcp_current_mss(sk), TCP_NAGLE_OFF);
} }
/* We get here when a process closes a file descriptor (either due to /* We get here when a process closes a file descriptor (either due to
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment