Commit 049ba123 authored by Eric Dumazet's avatar Eric Dumazet Committed by Luis Henriques

tcp: fix possible deadlock in tcp_send_fin()

commit d83769a5 upstream.

Using sk_stream_alloc_skb() in tcp_send_fin() is dangerous in
case a huge process is killed by OOM, and tcp_mem[2] is hit.

To be able to free memory we need to make progress, so this
patch allows FIN packets to not care about tcp_mem[2], if
skb allocation succeeded.

In a follow-up patch, we might abort tcp_send_fin() infinite loop
in case TIF_MEMDIE is set on this thread, as memory allocator
did its best getting extra memory already.

This patch reverts d22e1537 ("tcp: fix tcp fin memory accounting")

Fixes: d22e1537 ("tcp: fix tcp fin memory accounting")
Signed-off-by: default avatarEric Dumazet <edumazet@google.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
Signed-off-by: default avatarLuis Henriques <luis.henriques@canonical.com>
parent a283d4e4
...@@ -2675,6 +2675,21 @@ void tcp_xmit_retransmit_queue(struct sock *sk) ...@@ -2675,6 +2675,21 @@ void tcp_xmit_retransmit_queue(struct sock *sk)
} }
} }
/* We allow to exceed memory limits for FIN packets to expedite
* connection tear down and (memory) recovery.
* Otherwise tcp_send_fin() could loop forever.
*/
static void sk_forced_wmem_schedule(struct sock *sk, int size)
{
int amt, status;
if (size <= sk->sk_forward_alloc)
return;
amt = sk_mem_pages(size);
sk->sk_forward_alloc += amt * SK_MEM_QUANTUM;
sk_memory_allocated_add(sk, amt, &status);
}
/* Send a fin. The caller locks the socket for us. This cannot be /* Send a fin. The caller locks the socket for us. This cannot be
* allowed to fail queueing a FIN frame under any circumstances. * allowed to fail queueing a FIN frame under any circumstances.
*/ */
...@@ -2697,11 +2712,14 @@ void tcp_send_fin(struct sock *sk) ...@@ -2697,11 +2712,14 @@ void tcp_send_fin(struct sock *sk)
} else { } else {
/* Socket is locked, keep trying until memory is available. */ /* Socket is locked, keep trying until memory is available. */
for (;;) { for (;;) {
skb = sk_stream_alloc_skb(sk, 0, sk->sk_allocation); skb = alloc_skb_fclone(MAX_TCP_HEADER,
sk->sk_allocation);
if (skb) if (skb)
break; break;
yield(); yield();
} }
skb_reserve(skb, MAX_TCP_HEADER);
sk_forced_wmem_schedule(sk, skb->truesize);
/* FIN eats a sequence byte, write_seq advanced by tcp_queue_skb(). */ /* FIN eats a sequence byte, write_seq advanced by tcp_queue_skb(). */
tcp_init_nondata_skb(skb, tp->write_seq, tcp_init_nondata_skb(skb, tp->write_seq,
TCPHDR_ACK | TCPHDR_FIN); TCPHDR_ACK | TCPHDR_FIN);
......
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