mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net-next v2 0/3]net: tcp: support probing OOM
@ 2023-08-07 13:45 menglong8.dong
  2023-08-07 13:45 ` [PATCH net-next v2 1/3] net: tcp: send zero-window ACK when no memory menglong8.dong
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: menglong8.dong @ 2023-08-07 13:45 UTC (permalink / raw)
  To: edumazet, ncardwell
  Cc: davem, kuba, pabeni, dsahern, netdev, linux-kernel, Menglong Dong

From: Menglong Dong <imagedong@tencent.com>

In this series, we make some small changes to make the tcp retransmission
become zero-window probes if the receiver drops the skb because of memory
pressure.

In the 1st patch, we reply a zero-window ACK if the skb is dropped
because out of memory, instead of dropping the skb silently.

In the 2nd patch, we allow a zero-window ACK to update the window.

In the 3rd patch, we check the timeout of a probing socket with
'(u32)icsk->icsk_timeout', instead of 'tcp_jiffies32'. This is more like
a bugfix.

After these changes, the tcp can probe the OOM of the receiver forever.

Changes since v1:
- send 0 rwin ACK for the receive queue empty case when necessary in the
  1st patch
- send the ACK immediately by using the ICSK_ACK_NOW flag in the 1st
  patch
- consider the case of the connection restart from idle, as Neal comment,
  in the 3rd patch

Menglong Dong (3):
  net: tcp: send zero-window ACK when no memory
  net: tcp: allow zero-window ACK update the window
  net: tcp: fix unexcepted socket die when snd_wnd is 0

 include/net/inet_connection_sock.h |  3 ++-
 net/ipv4/tcp_input.c               | 16 ++++++++++++----
 net/ipv4/tcp_output.c              | 14 +++++++++++---
 net/ipv4/tcp_timer.c               | 10 +++++++++-
 4 files changed, 34 insertions(+), 9 deletions(-)

-- 
2.40.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH net-next v2 1/3] net: tcp: send zero-window ACK when no memory
  2023-08-07 13:45 [PATCH net-next v2 0/3]net: tcp: support probing OOM menglong8.dong
@ 2023-08-07 13:45 ` menglong8.dong
  2023-08-07 14:05   ` Eric Dumazet
  2023-08-07 13:45 ` [PATCH net-next v2 2/3] net: tcp: allow zero-window ACK update the window menglong8.dong
  2023-08-07 13:45 ` [PATCH net-next v2 3/3] net: tcp: fix unexcepted socket die when snd_wnd is 0 menglong8.dong
  2 siblings, 1 reply; 8+ messages in thread
From: menglong8.dong @ 2023-08-07 13:45 UTC (permalink / raw)
  To: edumazet, ncardwell
  Cc: davem, kuba, pabeni, dsahern, netdev, linux-kernel, Menglong Dong

From: Menglong Dong <imagedong@tencent.com>

For now, skb will be dropped when no memory, which makes client keep
retrans util timeout and it's not friendly to the users.

In this patch, we reply an ACK with zero-window in this case to update
the snd_wnd of the sender to 0. Therefore, the sender won't timeout the
connection and will probe the zero-window with the retransmits.

Signed-off-by: Menglong Dong <imagedong@tencent.com>
---
v2:
- send 0 rwin ACK for the receive queue empty case when necessary
- send the ACK immediately by using the ICSK_ACK_NOW flag
---
 include/net/inet_connection_sock.h |  3 ++-
 net/ipv4/tcp_input.c               | 14 +++++++++++---
 net/ipv4/tcp_output.c              | 14 +++++++++++---
 3 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/include/net/inet_connection_sock.h b/include/net/inet_connection_sock.h
index c2b15f7e5516..be3c858a2ebb 100644
--- a/include/net/inet_connection_sock.h
+++ b/include/net/inet_connection_sock.h
@@ -164,7 +164,8 @@ enum inet_csk_ack_state_t {
 	ICSK_ACK_TIMER  = 2,
 	ICSK_ACK_PUSHED = 4,
 	ICSK_ACK_PUSHED2 = 8,
-	ICSK_ACK_NOW = 16	/* Send the next ACK immediately (once) */
+	ICSK_ACK_NOW = 16,	/* Send the next ACK immediately (once) */
+	ICSK_ACK_NOMEM = 32,
 };
 
 void inet_csk_init_xmit_timers(struct sock *sk,
diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index 8e96ebe373d7..aae485d0a3b6 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -5059,12 +5059,20 @@ static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)
 
 		/* Ok. In sequence. In window. */
 queue_and_out:
-		if (skb_queue_len(&sk->sk_receive_queue) == 0)
-			sk_forced_mem_schedule(sk, skb->truesize);
-		else if (tcp_try_rmem_schedule(sk, skb, skb->truesize)) {
+		if (skb_queue_len(&sk->sk_receive_queue) == 0) {
+			if (tcp_try_rmem_schedule(sk, skb, skb->truesize)) {
+				sk_forced_mem_schedule(sk, skb->truesize);
+				inet_csk(sk)->icsk_ack.pending |=
+					(ICSK_ACK_NOMEM | ICSK_ACK_NOW);
+				inet_csk_schedule_ack(sk);
+			}
+		} else if (tcp_try_rmem_schedule(sk, skb, skb->truesize)) {
 			reason = SKB_DROP_REASON_PROTO_MEM;
 			NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRCVQDROP);
 			sk->sk_data_ready(sk);
+			inet_csk(sk)->icsk_ack.pending |=
+				(ICSK_ACK_NOMEM | ICSK_ACK_NOW);
+			inet_csk_schedule_ack(sk);
 			goto drop;
 		}
 
diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c
index c5412ee77fc8..769a558159ee 100644
--- a/net/ipv4/tcp_output.c
+++ b/net/ipv4/tcp_output.c
@@ -257,11 +257,19 @@ EXPORT_SYMBOL(tcp_select_initial_window);
 static u16 tcp_select_window(struct sock *sk)
 {
 	struct tcp_sock *tp = tcp_sk(sk);
-	u32 old_win = tp->rcv_wnd;
-	u32 cur_win = tcp_receive_window(tp);
-	u32 new_win = __tcp_select_window(sk);
 	struct net *net = sock_net(sk);
+	u32 old_win = tp->rcv_wnd;
+	u32 cur_win, new_win;
+
+	/* Make the window 0 if we failed to queue the data because we
+	 * are out of memory. The window is temporary, so we don't store
+	 * it on the socket.
+	 */
+	if (unlikely(inet_csk(sk)->icsk_ack.pending & ICSK_ACK_NOMEM))
+		return 0;
 
+	cur_win = tcp_receive_window(tp);
+	new_win = __tcp_select_window(sk);
 	if (new_win < cur_win) {
 		/* Danger Will Robinson!
 		 * Don't update rcv_wup/rcv_wnd here or else
-- 
2.40.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH net-next v2 2/3] net: tcp: allow zero-window ACK update the window
  2023-08-07 13:45 [PATCH net-next v2 0/3]net: tcp: support probing OOM menglong8.dong
  2023-08-07 13:45 ` [PATCH net-next v2 1/3] net: tcp: send zero-window ACK when no memory menglong8.dong
@ 2023-08-07 13:45 ` menglong8.dong
  2023-08-07 13:45 ` [PATCH net-next v2 3/3] net: tcp: fix unexcepted socket die when snd_wnd is 0 menglong8.dong
  2 siblings, 0 replies; 8+ messages in thread
From: menglong8.dong @ 2023-08-07 13:45 UTC (permalink / raw)
  To: edumazet, ncardwell
  Cc: davem, kuba, pabeni, dsahern, netdev, linux-kernel, Menglong Dong

From: Menglong Dong <imagedong@tencent.com>

Fow now, an ACK can update the window in following case, according to
the tcp_may_update_window():

1. the ACK acknowledged new data
2. the ACK has new data
3. the ACK expand the window and the seq of it is valid

Now, we allow the ACK update the window if the window is 0, and the
seq/ack of it is valid. This is for the case that the receiver replies
an zero-window ACK when it is under memory stress and can't queue the new
data.

Signed-off-by: Menglong Dong <imagedong@tencent.com>
---
 net/ipv4/tcp_input.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index aae485d0a3b6..f17cca362086 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -3525,7 +3525,7 @@ static inline bool tcp_may_update_window(const struct tcp_sock *tp,
 {
 	return	after(ack, tp->snd_una) ||
 		after(ack_seq, tp->snd_wl1) ||
-		(ack_seq == tp->snd_wl1 && nwin > tp->snd_wnd);
+		(ack_seq == tp->snd_wl1 && (nwin > tp->snd_wnd || !nwin));
 }
 
 /* If we update tp->snd_una, also update tp->bytes_acked */
-- 
2.40.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH net-next v2 3/3] net: tcp: fix unexcepted socket die when snd_wnd is 0
  2023-08-07 13:45 [PATCH net-next v2 0/3]net: tcp: support probing OOM menglong8.dong
  2023-08-07 13:45 ` [PATCH net-next v2 1/3] net: tcp: send zero-window ACK when no memory menglong8.dong
  2023-08-07 13:45 ` [PATCH net-next v2 2/3] net: tcp: allow zero-window ACK update the window menglong8.dong
@ 2023-08-07 13:45 ` menglong8.dong
  2023-08-07 14:19   ` Eric Dumazet
  2 siblings, 1 reply; 8+ messages in thread
From: menglong8.dong @ 2023-08-07 13:45 UTC (permalink / raw)
  To: edumazet, ncardwell
  Cc: davem, kuba, pabeni, dsahern, netdev, linux-kernel, Menglong Dong

From: Menglong Dong <imagedong@tencent.com>

In tcp_retransmit_timer(), a window shrunk connection will be regarded
as timeout if 'tcp_jiffies32 - tp->rcv_tstamp > TCP_RTO_MAX'. This is not
right all the time.

The retransmits will become zero-window probes in tcp_retransmit_timer()
if the 'snd_wnd==0'. Therefore, the icsk->icsk_rto will come up to
TCP_RTO_MAX sooner or later.

However, the timer is not precise enough, as it base on timer wheel.
Sorry that I am not good at timer, but I know the concept of time-wheel.
The longer of the timer, the rougher it will be. So the timeout is not
triggered after TCP_RTO_MAX, but 122877ms as I tested.

Therefore, 'tcp_jiffies32 - tp->rcv_tstamp > TCP_RTO_MAX' is always true
once the RTO come up to TCP_RTO_MAX, and the socket will die.

Fix this by replacing the 'tcp_jiffies32' with '(u32)icsk->icsk_timeout',
which is exact the timestamp of the timeout. Meanwhile, using
"max(tp->retrans_stamp, tp->rcv_tstamp)" as the last updated timestamp in
the receiving path, as "tp->rcv_tstamp" can restart from idle, then
tp->rcv_tstamp could already be a long time (minutes or hours) in the
past even on the first RTO.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Link: https://lore.kernel.org/netdev/CADxym3YyMiO+zMD4zj03YPM3FBi-1LHi6gSD2XT8pyAMM096pg@mail.gmail.com/
Signed-off-by: Menglong Dong <imagedong@tencent.com>
---
v2:
- consider the case of the connection restart from idle, as Neal comment
---
 net/ipv4/tcp_timer.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/net/ipv4/tcp_timer.c b/net/ipv4/tcp_timer.c
index d45c96c7f5a4..e4b2d8706cae 100644
--- a/net/ipv4/tcp_timer.c
+++ b/net/ipv4/tcp_timer.c
@@ -454,6 +454,14 @@ static void tcp_fastopen_synack_timer(struct sock *sk, struct request_sock *req)
 			  req->timeout << req->num_timeout, TCP_RTO_MAX);
 }
 
+static bool tcp_rtx_probe0_timed_out(struct sock *sk)
+{
+	struct tcp_sock *tp = tcp_sk(sk);
+	u32 last_ts;
+
+	last_ts = max(tp->retrans_stamp, tp->rcv_tstamp);
+	return inet_csk(sk)->icsk_timeout - last_ts > TCP_RTO_MAX;
+}
 
 /**
  *  tcp_retransmit_timer() - The TCP retransmit timeout handler
@@ -519,7 +527,7 @@ void tcp_retransmit_timer(struct sock *sk)
 					    tp->snd_una, tp->snd_nxt);
 		}
 #endif
-		if (tcp_jiffies32 - tp->rcv_tstamp > TCP_RTO_MAX) {
+		if (tcp_rtx_probe0_timed_out(sk)) {
 			tcp_write_err(sk);
 			goto out;
 		}
-- 
2.40.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH net-next v2 1/3] net: tcp: send zero-window ACK when no memory
  2023-08-07 13:45 ` [PATCH net-next v2 1/3] net: tcp: send zero-window ACK when no memory menglong8.dong
@ 2023-08-07 14:05   ` Eric Dumazet
  2023-08-08  1:58     ` Menglong Dong
  0 siblings, 1 reply; 8+ messages in thread
From: Eric Dumazet @ 2023-08-07 14:05 UTC (permalink / raw)
  To: menglong8.dong
  Cc: ncardwell, davem, kuba, pabeni, dsahern, netdev, linux-kernel,
	Menglong Dong

On Mon, Aug 7, 2023 at 3:47 PM <menglong8.dong@gmail.com> wrote:
>
> From: Menglong Dong <imagedong@tencent.com>
>
> For now, skb will be dropped when no memory, which makes client keep
> retrans util timeout and it's not friendly to the users.
>
> In this patch, we reply an ACK with zero-window in this case to update
> the snd_wnd of the sender to 0. Therefore, the sender won't timeout the
> connection and will probe the zero-window with the retransmits.
>
> Signed-off-by: Menglong Dong <imagedong@tencent.com>
> ---
> v2:
> - send 0 rwin ACK for the receive queue empty case when necessary
> - send the ACK immediately by using the ICSK_ACK_NOW flag
> ---
>  include/net/inet_connection_sock.h |  3 ++-
>  net/ipv4/tcp_input.c               | 14 +++++++++++---
>  net/ipv4/tcp_output.c              | 14 +++++++++++---
>  3 files changed, 24 insertions(+), 7 deletions(-)
>
> diff --git a/include/net/inet_connection_sock.h b/include/net/inet_connection_sock.h
> index c2b15f7e5516..be3c858a2ebb 100644
> --- a/include/net/inet_connection_sock.h
> +++ b/include/net/inet_connection_sock.h
> @@ -164,7 +164,8 @@ enum inet_csk_ack_state_t {
>         ICSK_ACK_TIMER  = 2,
>         ICSK_ACK_PUSHED = 4,
>         ICSK_ACK_PUSHED2 = 8,
> -       ICSK_ACK_NOW = 16       /* Send the next ACK immediately (once) */
> +       ICSK_ACK_NOW = 16,      /* Send the next ACK immediately (once) */
> +       ICSK_ACK_NOMEM = 32,
>  };
>
>  void inet_csk_init_xmit_timers(struct sock *sk,
> diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
> index 8e96ebe373d7..aae485d0a3b6 100644
> --- a/net/ipv4/tcp_input.c
> +++ b/net/ipv4/tcp_input.c
> @@ -5059,12 +5059,20 @@ static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)
>
>                 /* Ok. In sequence. In window. */
>  queue_and_out:
> -               if (skb_queue_len(&sk->sk_receive_queue) == 0)
> -                       sk_forced_mem_schedule(sk, skb->truesize);
> -               else if (tcp_try_rmem_schedule(sk, skb, skb->truesize)) {
> +               if (skb_queue_len(&sk->sk_receive_queue) == 0) {
> +                       if (tcp_try_rmem_schedule(sk, skb, skb->truesize)) {
> +                               sk_forced_mem_schedule(sk, skb->truesize);

I think we want sk->sk_data_ready() here, to let applications drain the queue,
regardless of sk->sk_rcvlowat value.


> +                               inet_csk(sk)->icsk_ack.pending |=
> +                                       (ICSK_ACK_NOMEM | ICSK_ACK_NOW);
> +                               inet_csk_schedule_ack(sk);
> +                       }
> +               } else if (tcp_try_rmem_schedule(sk, skb, skb->truesize)) {
>                         reason = SKB_DROP_REASON_PROTO_MEM;
>                         NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRCVQDROP);
>                         sk->sk_data_ready(sk);

We also want to keep this sk->sk_data_ready(sk) call.

> +                       inet_csk(sk)->icsk_ack.pending |=
> +                               (ICSK_ACK_NOMEM | ICSK_ACK_NOW);
> +                       inet_csk_schedule_ack(sk);
>                         goto drop;
>                 }

This would suggest a different code refactoring, to avoid code duplication.

diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index 57c8af1859c16eba5e952a23ea959b628006f9c1..dde6c44f2c1e33dcf60c23b49cd99f270874ca96
100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -5050,13 +5050,17 @@ static void tcp_data_queue(struct sock *sk,
struct sk_buff *skb)

                /* Ok. In sequence. In window. */
 queue_and_out:
-               if (skb_queue_len(&sk->sk_receive_queue) == 0)
-                       sk_forced_mem_schedule(sk, skb->truesize);
-               else if (tcp_try_rmem_schedule(sk, skb, skb->truesize)) {
-                       reason = SKB_DROP_REASON_PROTO_MEM;
-                       NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRCVQDROP);
+               if (tcp_try_rmem_schedule(sk, skb, skb->truesize)) {
+                       /* TODO: maybe ratelimit these WIN 0 ACK ? */
+                       inet_csk(sk)->icsk_ack.pending |=
CSK_ACK_NOMEM | ICSK_ACK_NOW;
+                       inet_csk_schedule_ack(sk);
                        sk->sk_data_ready(sk);
-                       goto drop;
+                       if (skb_queue_len(&sk->sk_receive_queue)) {
+                               reason = SKB_DROP_REASON_PROTO_MEM;
+                               NET_INC_STATS(sock_net(sk),
LINUX_MIB_TCPRCVQDROP);
+                               goto drop;
+                       }
+                       sk_forced_mem_schedule(sk, skb->truesize);
                }

                eaten = tcp_queue_rcv(sk, skb, &fragstolen);

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH net-next v2 3/3] net: tcp: fix unexcepted socket die when snd_wnd is 0
  2023-08-07 13:45 ` [PATCH net-next v2 3/3] net: tcp: fix unexcepted socket die when snd_wnd is 0 menglong8.dong
@ 2023-08-07 14:19   ` Eric Dumazet
  2023-08-08  2:05     ` Menglong Dong
  0 siblings, 1 reply; 8+ messages in thread
From: Eric Dumazet @ 2023-08-07 14:19 UTC (permalink / raw)
  To: menglong8.dong
  Cc: ncardwell, davem, kuba, pabeni, dsahern, netdev, linux-kernel,
	Menglong Dong

On Mon, Aug 7, 2023 at 3:47 PM <menglong8.dong@gmail.com> wrote:
>
> From: Menglong Dong <imagedong@tencent.com>
>
> In tcp_retransmit_timer(), a window shrunk connection will be regarded
> as timeout if 'tcp_jiffies32 - tp->rcv_tstamp > TCP_RTO_MAX'. This is not
> right all the time.
>
> The retransmits will become zero-window probes in tcp_retransmit_timer()
> if the 'snd_wnd==0'. Therefore, the icsk->icsk_rto will come up to
> TCP_RTO_MAX sooner or later.
>
> However, the timer is not precise enough, as it base on timer wheel.

> Sorry that I am not good at timer, but I know the concept of time-wheel.

Can you remove this line, can we keep focused on the actual patch instead ?

Regardless of timer-wheel, a timer can be delayed under load.

> The longer of the timer, the rougher it will be. So the timeout is not
> triggered after TCP_RTO_MAX, but 122877ms as I tested.
>
> Therefore, 'tcp_jiffies32 - tp->rcv_tstamp > TCP_RTO_MAX' is always true
> once the RTO come up to TCP_RTO_MAX, and the socket will die.
>
> Fix this by replacing the 'tcp_jiffies32' with '(u32)icsk->icsk_timeout',
> which is exact the timestamp of the timeout. Meanwhile, using
> "max(tp->retrans_stamp, tp->rcv_tstamp)" as the last updated timestamp in
> the receiving path, as "tp->rcv_tstamp" can restart from idle, then
> tp->rcv_tstamp could already be a long time (minutes or hours) in the
> past even on the first RTO.
>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Link: https://lore.kernel.org/netdev/CADxym3YyMiO+zMD4zj03YPM3FBi-1LHi6gSD2XT8pyAMM096pg@mail.gmail.com/
> Signed-off-by: Menglong Dong <imagedong@tencent.com>
> ---
> v2:
> - consider the case of the connection restart from idle, as Neal comment
> ---
>  net/ipv4/tcp_timer.c | 10 +++++++++-
>  1 file changed, 9 insertions(+), 1 deletion(-)
>
> diff --git a/net/ipv4/tcp_timer.c b/net/ipv4/tcp_timer.c
> index d45c96c7f5a4..e4b2d8706cae 100644
> --- a/net/ipv4/tcp_timer.c
> +++ b/net/ipv4/tcp_timer.c
> @@ -454,6 +454,14 @@ static void tcp_fastopen_synack_timer(struct sock *sk, struct request_sock *req)
>                           req->timeout << req->num_timeout, TCP_RTO_MAX);
>  }
>
> +static bool tcp_rtx_probe0_timed_out(struct sock *sk)
> +{
> +       struct tcp_sock *tp = tcp_sk(sk);
> +       u32 last_ts;
> +
> +       last_ts = max(tp->retrans_stamp, tp->rcv_tstamp);

u32     retrans_stamp;  /* Timestamp of the last retransmit,
u32     rcv_tstamp;     /* timestamp of last received ACK (for keepalives) */

Both fields receive tcp_jiffies32 values, which wrap every 2^32 ticks.

So max(A, B) won't work as you expect...

You need to use before(), after() or something like that.

https://en.wikipedia.org/wiki/Serial_number_arithmetic#General_Solution


> +       return inet_csk(sk)->icsk_timeout - last_ts > TCP_RTO_MAX;
> +}
>
>  /**
>   *  tcp_retransmit_timer() - The TCP retransmit timeout handler
> @@ -519,7 +527,7 @@ void tcp_retransmit_timer(struct sock *sk)
>                                             tp->snd_una, tp->snd_nxt);
>                 }
>  #endif
> -               if (tcp_jiffies32 - tp->rcv_tstamp > TCP_RTO_MAX) {
> +               if (tcp_rtx_probe0_timed_out(sk)) {
>                         tcp_write_err(sk);
>                         goto out;
>                 }
> --
> 2.40.1
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH net-next v2 1/3] net: tcp: send zero-window ACK when no memory
  2023-08-07 14:05   ` Eric Dumazet
@ 2023-08-08  1:58     ` Menglong Dong
  0 siblings, 0 replies; 8+ messages in thread
From: Menglong Dong @ 2023-08-08  1:58 UTC (permalink / raw)
  To: Eric Dumazet
  Cc: ncardwell, davem, kuba, pabeni, dsahern, netdev, linux-kernel,
	Menglong Dong

On Mon, Aug 7, 2023 at 10:05 PM Eric Dumazet <edumazet@google.com> wrote:
>
> On Mon, Aug 7, 2023 at 3:47 PM <menglong8.dong@gmail.com> wrote:
> >
> > From: Menglong Dong <imagedong@tencent.com>
> >
> > For now, skb will be dropped when no memory, which makes client keep
> > retrans util timeout and it's not friendly to the users.
> >
> > In this patch, we reply an ACK with zero-window in this case to update
> > the snd_wnd of the sender to 0. Therefore, the sender won't timeout the
> > connection and will probe the zero-window with the retransmits.
> >
> > Signed-off-by: Menglong Dong <imagedong@tencent.com>
> > ---
> > v2:
> > - send 0 rwin ACK for the receive queue empty case when necessary
> > - send the ACK immediately by using the ICSK_ACK_NOW flag
> > ---
> >  include/net/inet_connection_sock.h |  3 ++-
> >  net/ipv4/tcp_input.c               | 14 +++++++++++---
> >  net/ipv4/tcp_output.c              | 14 +++++++++++---
> >  3 files changed, 24 insertions(+), 7 deletions(-)
> >
> > diff --git a/include/net/inet_connection_sock.h b/include/net/inet_connection_sock.h
> > index c2b15f7e5516..be3c858a2ebb 100644
> > --- a/include/net/inet_connection_sock.h
> > +++ b/include/net/inet_connection_sock.h
> > @@ -164,7 +164,8 @@ enum inet_csk_ack_state_t {
> >         ICSK_ACK_TIMER  = 2,
> >         ICSK_ACK_PUSHED = 4,
> >         ICSK_ACK_PUSHED2 = 8,
> > -       ICSK_ACK_NOW = 16       /* Send the next ACK immediately (once) */
> > +       ICSK_ACK_NOW = 16,      /* Send the next ACK immediately (once) */
> > +       ICSK_ACK_NOMEM = 32,
> >  };
> >
> >  void inet_csk_init_xmit_timers(struct sock *sk,
> > diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
> > index 8e96ebe373d7..aae485d0a3b6 100644
> > --- a/net/ipv4/tcp_input.c
> > +++ b/net/ipv4/tcp_input.c
> > @@ -5059,12 +5059,20 @@ static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)
> >
> >                 /* Ok. In sequence. In window. */
> >  queue_and_out:
> > -               if (skb_queue_len(&sk->sk_receive_queue) == 0)
> > -                       sk_forced_mem_schedule(sk, skb->truesize);
> > -               else if (tcp_try_rmem_schedule(sk, skb, skb->truesize)) {
> > +               if (skb_queue_len(&sk->sk_receive_queue) == 0) {
> > +                       if (tcp_try_rmem_schedule(sk, skb, skb->truesize)) {
> > +                               sk_forced_mem_schedule(sk, skb->truesize);
>
> I think we want sk->sk_data_ready() here, to let applications drain the queue,
> regardless of sk->sk_rcvlowat value.
>
>
> > +                               inet_csk(sk)->icsk_ack.pending |=
> > +                                       (ICSK_ACK_NOMEM | ICSK_ACK_NOW);
> > +                               inet_csk_schedule_ack(sk);
> > +                       }
> > +               } else if (tcp_try_rmem_schedule(sk, skb, skb->truesize)) {
> >                         reason = SKB_DROP_REASON_PROTO_MEM;
> >                         NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRCVQDROP);
> >                         sk->sk_data_ready(sk);
>
> We also want to keep this sk->sk_data_ready(sk) call.
>
> > +                       inet_csk(sk)->icsk_ack.pending |=
> > +                               (ICSK_ACK_NOMEM | ICSK_ACK_NOW);
> > +                       inet_csk_schedule_ack(sk);
> >                         goto drop;
> >                 }
>
> This would suggest a different code refactoring, to avoid code duplication.
>
> diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
> index 57c8af1859c16eba5e952a23ea959b628006f9c1..dde6c44f2c1e33dcf60c23b49cd99f270874ca96
> 100644
> --- a/net/ipv4/tcp_input.c
> +++ b/net/ipv4/tcp_input.c
> @@ -5050,13 +5050,17 @@ static void tcp_data_queue(struct sock *sk,
> struct sk_buff *skb)
>
>                 /* Ok. In sequence. In window. */
>  queue_and_out:
> -               if (skb_queue_len(&sk->sk_receive_queue) == 0)
> -                       sk_forced_mem_schedule(sk, skb->truesize);
> -               else if (tcp_try_rmem_schedule(sk, skb, skb->truesize)) {
> -                       reason = SKB_DROP_REASON_PROTO_MEM;
> -                       NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRCVQDROP);
> +               if (tcp_try_rmem_schedule(sk, skb, skb->truesize)) {
> +                       /* TODO: maybe ratelimit these WIN 0 ACK ? */
> +                       inet_csk(sk)->icsk_ack.pending |=
> CSK_ACK_NOMEM | ICSK_ACK_NOW;
> +                       inet_csk_schedule_ack(sk);
>                         sk->sk_data_ready(sk);
> -                       goto drop;
> +                       if (skb_queue_len(&sk->sk_receive_queue)) {
> +                               reason = SKB_DROP_REASON_PROTO_MEM;
> +                               NET_INC_STATS(sock_net(sk),
> LINUX_MIB_TCPRCVQDROP);
> +                               goto drop;
> +                       }
> +                       sk_forced_mem_schedule(sk, skb->truesize);
>                 }
>
>                 eaten = tcp_queue_rcv(sk, skb, &fragstolen);

Looks much better, thank you!

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH net-next v2 3/3] net: tcp: fix unexcepted socket die when snd_wnd is 0
  2023-08-07 14:19   ` Eric Dumazet
@ 2023-08-08  2:05     ` Menglong Dong
  0 siblings, 0 replies; 8+ messages in thread
From: Menglong Dong @ 2023-08-08  2:05 UTC (permalink / raw)
  To: Eric Dumazet
  Cc: ncardwell, davem, kuba, pabeni, dsahern, netdev, linux-kernel,
	Menglong Dong

On Mon, Aug 7, 2023 at 10:20 PM Eric Dumazet <edumazet@google.com> wrote:
>
> On Mon, Aug 7, 2023 at 3:47 PM <menglong8.dong@gmail.com> wrote:
> >
> > From: Menglong Dong <imagedong@tencent.com>
> >
> > In tcp_retransmit_timer(), a window shrunk connection will be regarded
> > as timeout if 'tcp_jiffies32 - tp->rcv_tstamp > TCP_RTO_MAX'. This is not
> > right all the time.
> >
> > The retransmits will become zero-window probes in tcp_retransmit_timer()
> > if the 'snd_wnd==0'. Therefore, the icsk->icsk_rto will come up to
> > TCP_RTO_MAX sooner or later.
> >
> > However, the timer is not precise enough, as it base on timer wheel.
>
> > Sorry that I am not good at timer, but I know the concept of time-wheel.
>
> Can you remove this line, can we keep focused on the actual patch instead ?
>
> Regardless of timer-wheel, a timer can be delayed under load.
>

Okay!

> > The longer of the timer, the rougher it will be. So the timeout is not
> > triggered after TCP_RTO_MAX, but 122877ms as I tested.
> >
> > Therefore, 'tcp_jiffies32 - tp->rcv_tstamp > TCP_RTO_MAX' is always true
> > once the RTO come up to TCP_RTO_MAX, and the socket will die.
> >
> > Fix this by replacing the 'tcp_jiffies32' with '(u32)icsk->icsk_timeout',
> > which is exact the timestamp of the timeout. Meanwhile, using
> > "max(tp->retrans_stamp, tp->rcv_tstamp)" as the last updated timestamp in
> > the receiving path, as "tp->rcv_tstamp" can restart from idle, then
> > tp->rcv_tstamp could already be a long time (minutes or hours) in the
> > past even on the first RTO.
> >
> > Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> > Link: https://lore.kernel.org/netdev/CADxym3YyMiO+zMD4zj03YPM3FBi-1LHi6gSD2XT8pyAMM096pg@mail.gmail.com/
> > Signed-off-by: Menglong Dong <imagedong@tencent.com>
> > ---
> > v2:
> > - consider the case of the connection restart from idle, as Neal comment
> > ---
> >  net/ipv4/tcp_timer.c | 10 +++++++++-
> >  1 file changed, 9 insertions(+), 1 deletion(-)
> >
> > diff --git a/net/ipv4/tcp_timer.c b/net/ipv4/tcp_timer.c
> > index d45c96c7f5a4..e4b2d8706cae 100644
> > --- a/net/ipv4/tcp_timer.c
> > +++ b/net/ipv4/tcp_timer.c
> > @@ -454,6 +454,14 @@ static void tcp_fastopen_synack_timer(struct sock *sk, struct request_sock *req)
> >                           req->timeout << req->num_timeout, TCP_RTO_MAX);
> >  }
> >
> > +static bool tcp_rtx_probe0_timed_out(struct sock *sk)
> > +{
> > +       struct tcp_sock *tp = tcp_sk(sk);
> > +       u32 last_ts;
> > +
> > +       last_ts = max(tp->retrans_stamp, tp->rcv_tstamp);
>
> u32     retrans_stamp;  /* Timestamp of the last retransmit,
> u32     rcv_tstamp;     /* timestamp of last received ACK (for keepalives) */
>
> Both fields receive tcp_jiffies32 values, which wrap every 2^32 ticks.
>
> So max(A, B) won't work as you expect...
>
> You need to use before(), after() or something like that.

Now I know how the before()/after() works. I thought they
simply compared the arguments.

I'll use brefore/after here instead.

Thanks!
Menglong Dong

>
> https://en.wikipedia.org/wiki/Serial_number_arithmetic#General_Solution
>
>
> > +       return inet_csk(sk)->icsk_timeout - last_ts > TCP_RTO_MAX;
> > +}
> >
> >  /**
> >   *  tcp_retransmit_timer() - The TCP retransmit timeout handler
> > @@ -519,7 +527,7 @@ void tcp_retransmit_timer(struct sock *sk)
> >                                             tp->snd_una, tp->snd_nxt);
> >                 }
> >  #endif
> > -               if (tcp_jiffies32 - tp->rcv_tstamp > TCP_RTO_MAX) {
> > +               if (tcp_rtx_probe0_timed_out(sk)) {
> >                         tcp_write_err(sk);
> >                         goto out;
> >                 }
> > --
> > 2.40.1
> >

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-08-08  2:05 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-07 13:45 [PATCH net-next v2 0/3]net: tcp: support probing OOM menglong8.dong
2023-08-07 13:45 ` [PATCH net-next v2 1/3] net: tcp: send zero-window ACK when no memory menglong8.dong
2023-08-07 14:05   ` Eric Dumazet
2023-08-08  1:58     ` Menglong Dong
2023-08-07 13:45 ` [PATCH net-next v2 2/3] net: tcp: allow zero-window ACK update the window menglong8.dong
2023-08-07 13:45 ` [PATCH net-next v2 3/3] net: tcp: fix unexcepted socket die when snd_wnd is 0 menglong8.dong
2023-08-07 14:19   ` Eric Dumazet
2023-08-08  2:05     ` Menglong Dong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®