From nobody Mon Nov 23 20:27:01 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794870359; b=32dC8/bKKHK7iYPbJRnS7MtvgFn2ZsQwOu0NdaMx7YEjpM/tTtQe8I5ngb2tt48r3L9g T+IgffEAscbuU9cELjTEcHDO2OK5SJLI6+aTIBpIKI7g5SdZKBjRro/xpe/Ix4GIM6Oze hbUsKuTufRo1F3zM48lPJIIQWes/ihz+CAvvw6mdfEWOL1mcP+rhj49aitVgtxbbqSKNE wk99p14A40FlGenAeNosN2SXm86XvJDcFzH+x6OGomxJQj3jSp3GH46wVLvhGs5S935DC RKwU218laHkhUoATTbFY20r9ldkZF9j+Bp0Xl9pogTyGidC0qhC/a9uEoBv7UjK2J5Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794870359; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=kEaEXO/J1xQxeEQZwu8ZzIT12eLGw74qjW2MKVja9P0=; b=qe8lH16lN2pglA2l+I2n1oqUg4+WHRSLBH8poxs6Or2CTFaQJ84qQ3QKc3Wktcc3XU1G KcaF1gAWPYffVDYDdj87/9iFCbn2wUAR6awWo8gV0se63Nk1wfeAoR+H4yWAzLNaMqV45 nq+HbjbEeCZQY4cinmmWE/pr4YLoSYh8duOrKj7foQs7NVanCtvwD/pjsNmiJneoL63Dy c4g/1KDfkOb9Q5m+t5mC6tDzTCOYaGf4n9wZIoMFoLVekboOkFdAuK5DyWR1Zt5vdh94A zrc0VwEBMrWaX2ZQGyLKwk6fhO9+mB8y44Z3gxlmelzbyOoDKFJ0rL8x/6ZaFbTc1YA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--kuniyu.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=WoRpsLgp; arc=none smtp.remote-ip=209.85.214.198 Received: from mail-pl1-f198.google.com (mail-pl1-f198.google.com [209.85.214.198]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8688E606A331 for ; Mon, 16 Nov 2026 23:05:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8688E606A331 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--kuniyu.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=WoRpsLgp Received: by mail-pl1-f198.google.com with SMTP id d9443c01a7336-2e95f8543c9so56946825ad.3 for ; Mon, 16 Nov 2026 15:05:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1794870359; x=1795475159; darn=lists.linux.dev; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=kEaEXO/J1xQxeEQZwu8ZzIT12eLGw74qjW2MKVja9P0=; b=WoRpsLgpCij5w5RJXdFcPVD0HLGr+ZIv3s5GlqA26u5L0fM2EAtBTj6PgjcMiqJcg/ lojanHiyHuYXUEry0Xn+1g2ZRB1iQTWzIBERBZdCxELvThZvbOrztsrVxskA0xNSoCqy T7yHU+/PU9sWtIQa1G/+PMZcvKkjuUJjfDJGEE7XiRMT2heiG7iiHXNn3iNVPC8NmP2o X655ER2GbPfWp2GSZSGQ1st4vHE2Ld3DsKVYgK1uQGg8Nw4JDBLwC2OZlansuP8qSDuz CKJA4pyKwTs6F9T7mSLjpy2KbN6rxMXzU10tgK2sygcTsKfqfqMcZs1bg8KOHHzA04db ejpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794870359; x=1795475159; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=kEaEXO/J1xQxeEQZwu8ZzIT12eLGw74qjW2MKVja9P0=; b=thL7BrY0ZkCvBltCS7DwckUyTfF/wyLPK0NLRH1qkAbIejIjNjRMI/wtTG/1mJxlsD bv3+RMGwSTnKgtMvWgRB3xtsEcZ5Lhli/JixTu7OaqJ5bss05FqOc09t8hXXT8h6fe0i Y4+5citjqlpMhEqRS2Gql2Pk/vMQBRONEu7Ip1F6LtIxr+0IKMLKdpoa1YQBcdYdWgF8 p/34AknGqL3jEtYYCuGBRowEWUNsG5OXv+d0pYC/TCj0xeUG+NWpU7B1wGAEImDcySsS vUVjF32JAPg4wOvv4OoKm1gGA2UnQr4Ef8AzSxVvvDhwWSFLpufyZAsMnt7lR8c9PFeC xARA== X-Forwarded-Encrypted: i=1; AL+hErEkuvDp7DH54rogKWhAujMC/PbuftBoTzTFos/eDKsINSG7e78SVJndFIXVzJhLuj8DRxqhhg==@lists.linux.dev X-Gm-Message-State: APuU4Z/0il++c8reiBUNk/uSPXyN4xGVZeMg2RatvTX2XO0UT600pjt9 Ti4Hz9Ox004bsE3NINFXcp2pBbSwrP58QqhtQk7tjPj6CPYLX4+sFEIKN0Z5cy4nnwS1gTE7+q5 uOJduJQ== X-Received: from plpw11.prod.google.com ([2002:a17:902:9a8b:b0:2e8:bfd5:b889]) (user=kuniyu job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:fc47:b0:2ed:3eeb:b0db with SMTP id d9443c01a7336-2ee0de10ab1mr113092835ad.17.1794870358515; Mon, 16 Nov 2026 15:05:58 -0800 (PST) Date: Mon, 16 Nov 2026 23:05:25 +0000 In-Reply-To: <20261116230555.2305867-1-kuniyu@google.com> X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261116230555.2305867-1-kuniyu@google.com> X-Mailer: git-send-email 2.56.0.679.gc527c65306-goog Message-ID: <20261116230555.2305867-2-kuniyu@google.com> Subject: [PATCH v2 net-next 1/7] tcp: Refactor the first loop in tcp_collapse(). From: Kuniyuki Iwashima To: Eric Dumazet , Neal Cardwell , "David S . Miller" , Jakub Kicinski , Paolo Abeni , Matthieu Baerts , Mat Martineau Cc: Simon Horman , Kuniyuki Iwashima , Kuniyuki Iwashima , netdev@vger.kernel.org, mptcp@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" tcp_collapse() detects skb to start collapsing from in the first loop. If the last skb has SYN or FIN, the condition is tested again after the loop. skb_frags_readable() is placed inside and outside of the loop as well. If we goto skip_this in both cases, we can remove the second tests and simply replace end_of_skbs with the inverted loop condition. Then, we do not need a test for tcp_collapse_one() too. Also, skb_frags_readable() should be tested after the fully overlapping check. Let's refactor the loop. Note that previously, when a SYN/FIN skb overlapped with the next skb, the loop broke with end_of_skbs set to false and the check after the loop returned without collapsing anything further. Now such a skb is skipped instead, allowing the following skbs to be collapsed. Signed-off-by: Kuniyuki Iwashima Reviewed-by: Eric Dumazet --- v2: Document SYN/FIN behaviour change --- net/ipv4/tcp_input.c | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index a2074a1af422..32e63d089db8 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -5839,23 +5839,17 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *= list, struct rb_root *root, { struct sk_buff *skb =3D head, *n; struct sk_buff_head tmp; - bool end_of_skbs; =20 /* First, check that queue is collapsible and find * the point where collapsing can be useful. */ restart: - for (end_of_skbs =3D true; skb !=3D NULL && skb !=3D tail; skb =3D n) { + for (; skb && skb !=3D tail; skb =3D n) { n =3D tcp_skb_next(skb, list); =20 - if (!skb_frags_readable(skb)) - goto skip_this; - /* No new bits? It is possible on ofo queue. */ if (!before(start, TCP_SKB_CB(skb)->end_seq)) { skb =3D tcp_collapse_one(sk, skb, list, root); - if (!skb) - break; goto restart; } =20 @@ -5864,27 +5858,25 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *= list, struct rb_root *root, * - bloated or contains data before "start" or * overlaps to the next one and mptcp allow collapsing. */ - if (!(TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) && - (tcp_win_from_space(sk, skb->truesize) > skb->len || - before(TCP_SKB_CB(skb)->seq, start))) { - end_of_skbs =3D false; + if (!skb_frags_readable(skb) || + TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) + goto skip_this; + + if (tcp_win_from_space(sk, skb->truesize) > skb->len || + before(TCP_SKB_CB(skb)->seq, start)) break; - } =20 if (n && n !=3D tail && skb_frags_readable(n) && tcp_skb_can_collapse_rx(skb, n) && - TCP_SKB_CB(skb)->end_seq !=3D TCP_SKB_CB(n)->seq) { - end_of_skbs =3D false; + TCP_SKB_CB(skb)->end_seq !=3D TCP_SKB_CB(n)->seq) break; - } =20 skip_this: /* Decided to skip this, advance start seq. */ start =3D TCP_SKB_CB(skb)->end_seq; } - if (end_of_skbs || - (TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) || - !skb_frags_readable(skb)) + + if (!skb || skb =3D=3D tail) return; =20 __skb_queue_head_init(&tmp); --=20 2.56.0.679.gc527c65306-goog From nobody Mon Nov 23 20:27:01 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.71 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794870361; b=jRnU9yloKGGV7VRtcOT51pJQB4LSBm/R/HCC7kxctOgKQNIS9GiJU/x5hAXofa4NPEjg qdlb4xWcxNHEnn7EM2ztkgT8zBM8XEHJAVR9ebpRKGRJALJ2GAgqpOoOBQNNgpctFlnaa SLOxjAo1Dz5BD08g3I5OFfzFa3YnS7MVtKuVZQtu6mTA1FyrXC0U8YgosWIoiQeWRD1Zb rcOHi1nVVokqvITzYu8Q0rZ+MIScOQEFzizXBBJjTXkFIfBm7U7zmzbOa07MMvKaiIX2P UnRPI4Ru3awwx+4OPwVorO3Pr3tE7bP1JXNHVvCnzD5Cd/0CCG0y8vyUhklyG9jNkkA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794870361; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=82kaqSP7iEmpNxWJnQWMoEafhsr/yOtqX12R6w+tpRU=; b=EdXX1pkaNHdh0WbNZmuTDf4XKxg6EMqwnec5LbLLBqj5trXN6CsUeRAyyTOJmnmnFmN4 Wiivy4aunjtPPMQKMkPAx1seo1yrlhbmOpodpxBO9SHZz9fPBjzsDx9CyTNZLaeJez5Zn RjORPF0c3kDjcYfkwfVbZTxp/7M7DozunXHOY8XFVYD8u6sFryIpXjwP3RctP2F5D+FoG PX6sgRGREDdy+c9MmSU4G2dKhDJPFRvObVM+j0+WyvBMBrtXSKKHrCY8kvayPZ4u+8F+U xwKdut/4zuNPrVNjRxksw4j3rHRYJaywxbCVN6P+QnQuY3gFhYirtwqd5rnJmOPOFEA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--kuniyu.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=J36b73gi; arc=none smtp.remote-ip=209.85.216.71 Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4669D606A33A for ; Mon, 16 Nov 2026 23:06:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4669D606A33A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--kuniyu.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=J36b73gi Received: by mail-pj1-f71.google.com with SMTP id 98e67ed59e1d1-3b3a9ee822fso548091a91.0 for ; Mon, 16 Nov 2026 15:06:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1794870359; x=1795475159; darn=lists.linux.dev; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=82kaqSP7iEmpNxWJnQWMoEafhsr/yOtqX12R6w+tpRU=; b=J36b73gixq665zWKeITRVB1csegpn6C3D5aK1DeEi1E7Edd4k/TXyWX5wY5E5dQXKz Ab5Bb/UAgdUkEep01XUWHgMEOU6ouS0T+7CWQtr98v0c3P02+383nDjuNTIXewBy33+P JGaB8JNG3GQTK769gfhHNLsIjU2uy7duuX8gyeIG+WI6S4cULDSuMWmM3Gk4LsNx0o9+ sL9pjvTIeGIelqx/d+D4PG/nO8GcqneDgpX/yjV1CzZvxEOw8g/kg8fuGmgGSvvXTjdd O6km2qC2gJ0/3vcv1WP+CcGgW26s6fYR6JuRF6LDj4+BDceTTwBAZ8E3QJHCSsbknUYl SuIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794870359; x=1795475159; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=82kaqSP7iEmpNxWJnQWMoEafhsr/yOtqX12R6w+tpRU=; b=Brf0h6U6FGDPo5sztVQFXK5n3NENgDd8Urvql69UunBr9Ig97AlA1KbrIMOJOpeL6Y +rsNd3dY/Cg8s2XbgfiK/hDyF9JZbJ43dXC6jgOFiucpO+t/CQXsTkgpLNBMfkZSWaiY 0MStW+3aCimEi6P6XUEQrVajsN5FWb3lExjuj6lPbynyvtK5lOYHj7xlJxM7jGtPstNH Iupt0S9FLnHQkchbe0F3aYCf8s/+Kw42kLXD4BgdIKR7CsHF5EJ4YGPkO9ZOqIzCIRCY s6oUTQpbf1Qxv0gmJMBl12MKzRBMcB8JzZ1mNCsbqDfIV/xchQBM2QO6ANWzZ0hx0LqZ SCCA== X-Forwarded-Encrypted: i=1; AL+hErEivdb/1cEENDg2LeTdkn24gKwK0EPv/7rUeG1TECnAu0eodPXRlLKIC8WuzsIi0FlMaUMmPQ==@lists.linux.dev X-Gm-Message-State: APuU4Z+cW4yTKuZi1QALi0GWYE27D7ixo5Okyeyro/qPVnJMx6YMEEXS t410RBjLzkq+WbRhkXfIYeNalbVEPBXtLjSo8VFclSdvVx13k8/Jeq+4XBSHGFtKvtCQPZVwxsu jERjW0A== X-Received: from pjyd15.prod.google.com ([2002:a17:90a:dfcf:b0:3ad:ce09:ff3]) (user=kuniyu job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5608:b0:3ac:c024:b275 with SMTP id 98e67ed59e1d1-3b1b089e262mr12037995a91.9.1794870359342; Mon, 16 Nov 2026 15:05:59 -0800 (PST) Date: Mon, 16 Nov 2026 23:05:26 +0000 In-Reply-To: <20261116230555.2305867-1-kuniyu@google.com> X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261116230555.2305867-1-kuniyu@google.com> X-Mailer: git-send-email 2.56.0.679.gc527c65306-goog Message-ID: <20261116230555.2305867-3-kuniyu@google.com> Subject: [PATCH v2 net-next 2/7] tcp: Don't collapse high density skb. From: Kuniyuki Iwashima To: Eric Dumazet , Neal Cardwell , "David S . Miller" , Jakub Kicinski , Paolo Abeni , Matthieu Baerts , Mat Martineau Cc: Simon Horman , Kuniyuki Iwashima , Kuniyuki Iwashima , netdev@vger.kernel.org, mptcp@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" tcp_collapse() starts collapsing skb to a new skb if either of the following is true: 1. skb is less dense compared to tp->scaling_ratio 2. skb has read data before @start However, this could make situation worse. The new skb size is SKB_TRUESIZE(SKB_MAX_ORDER(0, 0)) at most, and with PAGE_SIZE being 4K, its scaling_ratio is 222 (86%). If 4K-MSS GRO skb updates tp->scaling_ratio, it can be larger than 222. Then, even the newly collapsed skb could be detected as bloated by the next tcp_collapse(). Also, even when only a few bytes are read from GRO skb, it is collapsed by the 2nd rule, making less-efficient skb. Let's cap tp->scaling_ratio with the collapsed skb's maximum scaling_ratio and use unread bytes for bloat detection. Signed-off-by: Kuniyuki Iwashima Reviewed-by: Eric Dumazet --- net/ipv4/tcp_input.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index 32e63d089db8..e07f43347409 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -5808,6 +5808,19 @@ static struct sk_buff *tcp_skb_next(struct sk_buff *= skb, struct sk_buff_head *li return skb_rb_next(skb); } =20 +#define TCP_COLLAPSE_MAX_SIZE SKB_MAX_ORDER(0, 0) +#define TCP_COLLAPSE_SCALING_RATIO \ + ((TCP_COLLAPSE_MAX_SIZE << TCP_RMEM_TO_WIN_SCALE) / \ + (SKB_TRUESIZE(TCP_COLLAPSE_MAX_SIZE))) + +static bool tcp_collapse_skb_bloated(const struct sk_buff *skb, + u32 start, u8 scaling_ratio) +{ + int unread =3D TCP_SKB_CB(skb)->end_seq - start; + + return __tcp_win_from_space(scaling_ratio, skb->truesize) > unread; +} + static struct sk_buff *tcp_collapse_one(struct sock *sk, struct sk_buff *s= kb, struct sk_buff_head *list, struct rb_root *root) @@ -5839,6 +5852,10 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *l= ist, struct rb_root *root, { struct sk_buff *skb =3D head, *n; struct sk_buff_head tmp; + u8 scaling_ratio; + + scaling_ratio =3D min(tcp_sk(sk)->scaling_ratio, + TCP_COLLAPSE_SCALING_RATIO); =20 /* First, check that queue is collapsible and find * the point where collapsing can be useful. @@ -5855,15 +5872,14 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *= list, struct rb_root *root, =20 /* The first skb to collapse is: * - not SYN/FIN and - * - bloated or contains data before "start" or + * - the unread portion starting from "start" is bloated, or * overlaps to the next one and mptcp allow collapsing. */ if (!skb_frags_readable(skb) || TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) goto skip_this; =20 - if (tcp_win_from_space(sk, skb->truesize) > skb->len || - before(TCP_SKB_CB(skb)->seq, start)) + if (tcp_collapse_skb_bloated(skb, start, scaling_ratio)) break; =20 if (n && n !=3D tail && skb_frags_readable(n) && @@ -5882,7 +5898,7 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *li= st, struct rb_root *root, __skb_queue_head_init(&tmp); =20 while (before(start, end)) { - int copy =3D min_t(int, SKB_MAX_ORDER(0, 0), end - start); + int copy =3D min_t(int, TCP_COLLAPSE_MAX_SIZE, end - start); struct sk_buff *nskb; =20 nskb =3D alloc_skb(copy, GFP_ATOMIC); --=20 2.56.0.679.gc527c65306-goog From nobody Mon Nov 23 20:27:01 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794870361; b=puIx7Bclr59UFyl0CP6w+HfuvrbGqzHBxWjDAz5XMlsPFvmMPdCaYjTlqu0heis7dYQc wixCEGWnuRcE/eEVMwWxWFDVAryrZ2vMDxAUUFEI3A4TrICoqfYozZs2WCptisDFGFkQm jzTdm8XLGo912jfxNAjzk7A+9TerM0MDUj7b3yea5M/mUxqlsXm3oSFCZ/gK3DQ2NBXFd WQ1qGA+MhOnPCqv3CqCDdz7yhBFedI9I2LS9pVDWThri5vIapz6apLxP5f0yTBeRyiCNj vJINlcEXCBNLWjqRBW+iEClPXqsIPpbOi7gaNh7AscrICClMCTurPE73uqgMXBI3S5A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794870361; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=aoUoBk1HZavue3Km3O2AQLSN8BcEHTWpRN3NYo57aBs=; b=AIKEJIbFTSiwz3vwrmVqlrN95gDn1XmrdeZcj1vflmtW1ejsPD7lBXkWYTHmbDm/9xzM sirKr2+ZSxQVmTruSJlbLVRmzpF3icOzAG2SbwI6RcqIGKZsLpW8veUJRknKOhdGm23As +rayt8PZgnfcvhpb0nZOdv20LpYXm8mP1IAAsImakgoVpTk/kpd3t2ejE5kPdtcRTbdZw dL7jJQL0UtKbihwW58xr/XEnlwxv8vafhMp9BrOSUy/7ipP41WiqvT95WA1JtvbAb1BGw nnn8l9X3mzNooXbHOwhpAXUotC1+EDsE6lOXrczKkGRVaUlVOnXxdMNiSt5NLD6ADPQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--kuniyu.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b="AxMI/Qj9"; arc=none smtp.remote-ip=209.85.215.198 Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3DFB4606A336 for ; Mon, 16 Nov 2026 23:06:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3DFB4606A336 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--kuniyu.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=AxMI/Qj9 Received: by mail-pg1-f198.google.com with SMTP id 41be03b00d2f7-cde02b50dd6so182849a12.1 for ; Mon, 16 Nov 2026 15:06:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1794870360; x=1795475160; darn=lists.linux.dev; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=aoUoBk1HZavue3Km3O2AQLSN8BcEHTWpRN3NYo57aBs=; b=AxMI/Qj9ieV175Zb27czpTU49kpqGxY0vEecZvQAFJhaalOUsyW6OI1IfWguI3BL9u xllri/K0dbykA2TnNtO4q93T7JMUEeRHuUuzch8MYVXRoVCcaNgjQjI/bRFgHiYdHtA2 0XlfVAstLJ5zgVaFfhs1WxdAQ3BoOd7IDKiX+I0xLO1v1zRLwVTRMLGaZMe/fb7ImW1X cAiU5Tea01d04GPwbtUeS2xv8MlpNf5OtlXEkG557V5kDO+2uhzeeDF/uiYfp5FOATMS SjiHXBJlQCqfX9o9UsAH8rQDBMy7esUeDg8MDqNLJzCNL29Pv+QNwQUo06MVKKffLunx nRAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794870360; x=1795475160; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=aoUoBk1HZavue3Km3O2AQLSN8BcEHTWpRN3NYo57aBs=; b=xQZsufwbTs9xFKANtQpZKdmAvi1tBwYYx5mcKmOOWMjWURBtcpexcGQyZJTIK1d1So ee5HGEzegweoFWV272u5c+hcR6G4bp5ytD0WqOWWRH10KOYUWeedqzxXjitDw6bQQI9G MrVH6fvRXmxxFHM4R+33WgzYYFSC2ZH2LnHkEAYY3j5rzHYqmexEaiegYeeletAcVPq+ 1Etq0NGK+o3gDOdCPvgNnrKlP0BbQpQN4oNnG39d+q1in5dC01rPjKasCZdz304WzIt7 0RvdrXrBg0JXP5q8fH8R03R8iyF79XvW+c4eqnW0aeJHrcg2JCfCmBEJnl4UJBaFPiG4 kKHQ== X-Forwarded-Encrypted: i=1; AL+hErGExIChT46mngqzSmF22qIMzUpGCA6OsF5l/7lMLy2Ir4E/g8kaIT/hNHZruljT2xjyIUP73Q==@lists.linux.dev X-Gm-Message-State: APuU4Z+sXq2R8AJ+eMFBn+m5wedrQfRpFw1dJOf/ylhVdzz3MBJcgk1l 2wU6HTUJF8xCkizBwfJ3Ab3TY1nBuJskxX6Gr+dGvUQTKyt9nhT9BtV6kOxCmtvJNFAIQdptSi2 /WwWFdA== X-Received: from pjwo1.prod.google.com ([2002:a17:90a:d241:b0:3b1:166b:a646]) (user=kuniyu job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4d06:b0:3b1:bd6d:fcc2 with SMTP id 98e67ed59e1d1-3b3abe7e796mr467017a91.40.1794870360098; Mon, 16 Nov 2026 15:06:00 -0800 (PST) Date: Mon, 16 Nov 2026 23:05:27 +0000 In-Reply-To: <20261116230555.2305867-1-kuniyu@google.com> X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261116230555.2305867-1-kuniyu@google.com> X-Mailer: git-send-email 2.56.0.679.gc527c65306-goog Message-ID: <20261116230555.2305867-4-kuniyu@google.com> Subject: [PATCH v2 net-next 3/7] tcp: Stop collapsing if not bloated. From: Kuniyuki Iwashima To: Eric Dumazet , Neal Cardwell , "David S . Miller" , Jakub Kicinski , Paolo Abeni , Matthieu Baerts , Mat Martineau Cc: Simon Horman , Kuniyuki Iwashima , Kuniyuki Iwashima , netdev@vger.kernel.org, mptcp@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Even when a skb is not bloated, tcp_collapse() starts collapsing if the next skb has overlapping data. Also, tcp_collapse() does not stop until it reaches the tail or unmergable skb (SYN/FIN, unreadable frags). Assume we detect a low-density skb followed by fully-packed 4K-MSS GRO skbs: skb->len truesize scaling_ratio skb #1 1448 2944 125 (49%) skb #2 65536 66432 252 (98%) skb #3 65536 66432 252 (98%) ... skb #9 65536 66432 252 (98%) -------------------------------------------- total 534400 Then, even the high-density skbs are repacked into lower-density skbs, and we end up consuming much more memory. skb->len truesize scaling_ratio nskb #1 3776 4352 222 (86%) nskb #2 3776 4352 222 (86%) ... nskb #139 3776 4352 222 (86%) nskb #140 872 2304 96 (37%) -------------------------------------------- total 607232 (+72832!) Even if we find a collapsible skb, we should not collapse subsequent skbs with scaling_ratio >=3D TCP_COLLAPSE_SCALING_RATIO. Conversely, even if tp->scaling_ratio is smaller than TCP_COLLAPSE_SCALING_RATIO, we should repack skbs once we start collapsing, which results in higher-density skb. Let's remove the next skb condition in the first loop, search for collapsible bloated skb range, and repeat collapsing for each range. Note that even when the next skb is not bloated, we must collapse it for OOO rbtree if it has overlapping data. if ((list || !before(TCP_SKB_CB(n)->seq, sub_end)) && !tcp_collapse_skb_bloated(n, sub_end, TCP_COLLAPSE_SCALING_RATIO)) For example, bloated skb_1 and non-bloated skb_2 have overlapping data and only skb_1 is collapsed to nskb_1 and nskb_2. skb_1 =3D [1000, 5000] skb_2 =3D [2000, 66000] Then, nskb_2 will be inserted after skb_2 in rbtree, wrongly setting tp->ooo_last_skb to 5000. nskb_1 =3D [1000, 4776] skb_2 =3D [2000, 66000] nskb_2 =3D [4776, 5000] This case is still inefficient, but a later patch handles this. Another notable thing is that tcp_skb_can_collapse_rx() now checks candidates against the first skb before mptcp_skb_ext_move(), not against each nskb. When a skb with an MPTCP ext spans multiple nskbs, collapsing previously stopped at the next skb with the same ext because later nskbs lacked the ext. skb1 (ext A), skb2 (ext A) nskb1 (ext A), nskb2 (no ext) ^ ^- not collapsible -' Now such skbs are also collapsed with the redundant ext dropped, while the first nskb still carries the same mapping in sequence order. skb1 (ext A), skb2 (ext A) nskb1 (ext A), nskb2 (no ext) <-' collapsed into nskb2 Signed-off-by: Kuniyuki Iwashima --- v2: Describe slight behavioural change for MPTCP --- net/ipv4/tcp_input.c | 66 +++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index e07f43347409..5bd42a74c6f5 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -5839,7 +5839,7 @@ static struct sk_buff *tcp_collapse_one(struct sock *= sk, struct sk_buff *skb, } =20 /* Collapse contiguous sequence of skbs head..tail with - * sequence numbers start..end. + * sequence numbers start..sub_end. * * If tail is NULL, this means until the end of the queue. * @@ -5848,15 +5848,18 @@ static struct sk_buff *tcp_collapse_one(struct sock= *sk, struct sk_buff *skb, */ static void tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *r= oot, - struct sk_buff *head, struct sk_buff *tail, u32 start, u32 end) + struct sk_buff *head, struct sk_buff *tail, u32 start) { struct sk_buff *skb =3D head, *n; struct sk_buff_head tmp; u8 scaling_ratio; + u32 sub_end; =20 scaling_ratio =3D min(tcp_sk(sk)->scaling_ratio, TCP_COLLAPSE_SCALING_RATIO); =20 + __skb_queue_head_init(&tmp); + /* First, check that queue is collapsible and find * the point where collapsing can be useful. */ @@ -5872,38 +5875,48 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *= list, struct rb_root *root, =20 /* The first skb to collapse is: * - not SYN/FIN and - * - the unread portion starting from "start" is bloated, or - * overlaps to the next one and mptcp allow collapsing. + * - the unread portion starting from "start" is bloated. */ - if (!skb_frags_readable(skb) || - TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) - goto skip_this; - - if (tcp_collapse_skb_bloated(skb, start, scaling_ratio)) - break; - - if (n && n !=3D tail && skb_frags_readable(n) && - tcp_skb_can_collapse_rx(skb, n) && - TCP_SKB_CB(skb)->end_seq !=3D TCP_SKB_CB(n)->seq) + if (skb_frags_readable(skb) && + !(TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) && + tcp_collapse_skb_bloated(skb, start, scaling_ratio)) break; =20 -skip_this: /* Decided to skip this, advance start seq. */ start =3D TCP_SKB_CB(skb)->end_seq; } =20 if (!skb || skb =3D=3D tail) - return; + goto end; =20 - __skb_queue_head_init(&tmp); + /* Second, find consecutive collapsible skb. */ + sub_end =3D TCP_SKB_CB(skb)->end_seq; + + for (n =3D tcp_skb_next(skb, list); n && n !=3D tail; n =3D tcp_skb_next(= n, list)) { + if (!skb_frags_readable(n) || + TCP_SKB_CB(n)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN) || + !tcp_skb_can_collapse_rx(skb, n)) + break; + + if (!after(TCP_SKB_CB(n)->end_seq, sub_end)) + continue; + + /* seq and end_seq must increase monotonically for rbtree. */ + if ((list || !before(TCP_SKB_CB(n)->seq, sub_end)) && + !tcp_collapse_skb_bloated(n, sub_end, + TCP_COLLAPSE_SCALING_RATIO)) + break; =20 - while (before(start, end)) { - int copy =3D min_t(int, TCP_COLLAPSE_MAX_SIZE, end - start); + sub_end =3D TCP_SKB_CB(n)->end_seq; + } + + while (before(start, sub_end)) { + int copy =3D min_t(int, TCP_COLLAPSE_MAX_SIZE, sub_end - start); struct sk_buff *nskb; =20 nskb =3D alloc_skb(copy, GFP_ATOMIC); if (!nskb) - break; + goto end; =20 memcpy(nskb->cb, skb->cb, sizeof(skb->cb)); TCP_SKB_CB(nskb)->has_rxtstamp =3D false; @@ -5942,15 +5955,11 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *= list, struct rb_root *root, } if (!before(start, TCP_SKB_CB(skb)->end_seq)) { skb =3D tcp_collapse_one(sk, skb, list, root); - if (!skb || - skb =3D=3D tail || - !tcp_skb_can_collapse_rx(nskb, skb) || - (TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) || - !skb_frags_readable(skb)) - goto end; } } } + + goto restart; end: skb_queue_walk_safe(&tmp, skb, n) tcp_rbtree_insert(root, skb); @@ -5989,7 +5998,7 @@ static void tcp_collapse_ofo_queue(struct sock *sk) if (range_truesize !=3D head->truesize || end - start >=3D SKB_WITH_OVERHEAD(PAGE_SIZE)) { tcp_collapse(sk, NULL, &tp->out_of_order_queue, - head, skb, start, end); + head, skb, start); } else { sum_tiny +=3D range_truesize; if (sum_tiny > sk->sk_rcvbuf >> 3) @@ -6103,8 +6112,7 @@ static int tcp_prune_queue(struct sock *sk, const str= uct sk_buff *in_skb) if (!skb_queue_empty(&sk->sk_receive_queue)) tcp_collapse(sk, &sk->sk_receive_queue, NULL, skb_peek(&sk->sk_receive_queue), - NULL, - tp->copied_seq, tp->rcv_nxt); + NULL, tp->copied_seq); =20 if (tcp_can_ingest(sk, in_skb)) return 0; --=20 2.56.0.679.gc527c65306-goog From nobody Mon Nov 23 20:27:01 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.69 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794870362; b=XnUkdVwpXmE330d/Qsr2g87LSolW1JbyHV9abtYUlagpNnnhevA0vpoz6EWvhphLhN8G Hgj3mhNxRHnpDj32i+CHsPWz6AdNNhpsa16uvtasnV/roI0n+psPbzQzaJbS+39S5KLiW 7UHGRs35z8nbccz9gGOuQ7IySRbkh5wQ3B2Zma92YRn0FFe+3AijnGjZOfcvLSjTGJ1VF U/QBOkMNM+X5hnkjHpTXzoZQdR4ERGSr9aFo8tncyi0gCZH5tZyeYApHOkcPFuH91tNMk zh6YKgvWagVkPSSb/8UiwkL684CLwqSg58U21DVUf+bEHJYHkW0+xTGkiIPKZHmfNMw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794870362; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=z4fBymZZSSUULWGJNGl+cnaSscVLy1TLb572Nad7Hrk=; b=p/XFcw84oNher2LuXEffdsTy/yJ/JeBImW2tmK1oA43kQiT6B4Q2Qwr0z/np+hr9Yls5 zDZl9+xmdTEG3DeOOWgJLX/R9oTaTxqV/+0iVsdaXaBjOAf/i8cOddFcqeC+P7+LAxJ+y PzQeU1eIC08/Sj+z6SQkppWM+47/VS88Yo0TLW0ilWU0ako17EKTrL62+8CraSwmkwiSW XAP/jzWm5G5gFTWSwx4YKa6IF82DIkce/+8okihYv1g8T5WRkoyr7s+Y6isxEcapwsfan 7qwol++TdUUZ5TnvviNmcCp3PhDzciRW+QS3lRhdTGPIC9otjf9lqbsufjzvMo9z4cQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--kuniyu.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=Vx5PzuhL; arc=none smtp.remote-ip=209.85.216.69 Received: from mail-pj1-f69.google.com (mail-pj1-f69.google.com [209.85.216.69]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E7207606A882 for ; Mon, 16 Nov 2026 23:06:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E7207606A882 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--kuniyu.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=Vx5PzuhL Received: by mail-pj1-f69.google.com with SMTP id 98e67ed59e1d1-3ada6ccd402so2874447a91.2 for ; Mon, 16 Nov 2026 15:06:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1794870361; x=1795475161; darn=lists.linux.dev; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=z4fBymZZSSUULWGJNGl+cnaSscVLy1TLb572Nad7Hrk=; b=Vx5PzuhLVqyroQvAOSmaZimf33zHM8AeZrO9OquS0mmLfqAfjlcH15FffJ5W8ibqoV RrwrubRpXEg0hwJy575LMtZTL3iMYS/hizwQScJRSB64Sy7F1lGlgeS8Ze7R7zHul/t8 U2cw63EpEpEYulygBOut41HFzB8F9qPdLwiFLG+wr5vDwKo0X07s+02mr1t21b8mwqlC KhUsYxAovPxIl24OxY6LImCJMEcjitOxnuM0w6jdr8kxxCMG5+4Nx4hnLeYMPP6OMRu1 AmOf9ZHw86uLSrYQ4GuiE7hO+OveAqn3W+cMVhyhdFmUtgdh6cMKDk7MWbe6HKuEbBoY J6Qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794870361; x=1795475161; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=z4fBymZZSSUULWGJNGl+cnaSscVLy1TLb572Nad7Hrk=; b=sYvekPXW/x8fBUWwEOBPDWJYf/KjgcMhSXrODxIEvgGF15qmyk81KtyLapgFWEllwQ ajCp2MbM89p2N7tTcPyFbohPn5ZKxAeWdZAyc64RMMxdO+YDwamr5ynIIJb4/jPxILzV UfkoUxUSKYLkR1ogq/k3IH5wsqVCoWe9bXc6RIBjkWD554SWl8XveGJfbJcTJJEPc6QZ Onk8PNwua8gbKCMeO3wJnvtn48t7Ds5RLQfUJRy0PVE5RHAvpC6ji8znqfKwoMI32T1W iizNKpDuCvgcRqAgZsdUlFa2jtwPoyVBQGPp2UEchlb7n+lfLhRlvM0nJ2NMklWdASI5 8QYQ== X-Forwarded-Encrypted: i=1; AL+hErHFIAhyRO72ORneVbVhufC/AgVLpsg3yRdMhyNtkwzwLFBjOoS2NhxtW4tgFFMKBpUdeI1rxg==@lists.linux.dev X-Gm-Message-State: APuU4Z+W0JlgiqsCkmsMWh6A5rW2+wf5vQZkKXzyJVjMGqeb111GzeHz OIdGja79MrHepkUmUGZsZyjqS+xJ6ehQqJuh5X1SGDPhv5KvXOdlNpGmPWgiXd05fuglo8AWHL1 FFCNLcg== X-Received: from pllh8.prod.google.com ([2002:a17:902:7488:b0:2ee:192c:eed2]) (user=kuniyu job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:4686:b0:2df:90d4:7758 with SMTP id d9443c01a7336-2ee0dc93c50mr104420135ad.23.1794870360916; Mon, 16 Nov 2026 15:06:00 -0800 (PST) Date: Mon, 16 Nov 2026 23:05:28 +0000 In-Reply-To: <20261116230555.2305867-1-kuniyu@google.com> X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261116230555.2305867-1-kuniyu@google.com> X-Mailer: git-send-email 2.56.0.679.gc527c65306-goog Message-ID: <20261116230555.2305867-5-kuniyu@google.com> Subject: [PATCH v2 net-next 4/7] tcp: Skip collapsing if collapsed skb is less-dense. From: Kuniyuki Iwashima To: Eric Dumazet , Neal Cardwell , "David S . Miller" , Jakub Kicinski , Paolo Abeni , Matthieu Baerts , Mat Martineau Cc: Simon Horman , Kuniyuki Iwashima , Kuniyuki Iwashima , netdev@vger.kernel.org, mptcp@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Once tcp_collapse() finds a collapsible skb, it collapses consecutive bloated skbs. However, the end result could be the same or larger than as is in some cases: 1. skb is already minimum If SKB_HEAD_ALIGN(len) <=3D SKB_SMALL_HEAD_CACHE_SIZE, skb is allocated from skbuff_small_head, and skb->truesize never changes. 2. SKB_HEAD_ALIGN() and kmalloc_size_roundup() in alloc_skb() Assume two bloated skbs with len =3D 1448 B (truesize =3D 2304 B, scaling_ratio =3D 160) and len =3D 300 B (truesize =3D 896 B, scaling_ratio =3D 85), whose total truesize is 3200 B. Collapsing them calls alloc_skb(1748), which rounds up SKB_HEAD_ALIGN(1748) =3D 2112 B to 4096 B and allocates a new skb with truesize =3D 4352 B (+1152 B!). Even worse, in case 1, such a skb is reallocated repeatedly by every tcp_collapse() in vain. Let's calculate the final collapsed truesize before collapsing skbs and skip the range if it does not shrink. We could allocate skbs first and calculate the new truesize, but given tcp_collapse() is called under memory pressure, we want to avoid unnecessary allocation. Note that kmalloc_reserve_size() uses __always_inline so that this change does not affect non-FDO builds, and returns 0 when skb_small_head_cache is used, similar to kmalloc_pfmemalloc(). Signed-off-by: Kuniyuki Iwashima --- v2: Add compute_skb_truesize() instead of macro --- include/linux/skbuff.h | 2 ++ net/core/skbuff.c | 26 +++++++++++++++++++++++--- net/ipv4/tcp_input.c | 24 +++++++++++++++++++++++- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index 1d6832878930..310e4881045b 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -274,6 +274,8 @@ SKB_DATA_ALIGN(sizeof(struct sk_buff)) + \ SKB_DATA_ALIGN(sizeof(struct skb_shared_info))) =20 +unsigned int compute_skb_truesize(unsigned int size, gfp_t flags); + struct net_device; struct scatterlist; struct pipe_inode_info; diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 2e81aacc998b..a89eaadc729f 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -589,6 +589,17 @@ struct sk_buff *napi_build_skb(void *data, unsigned in= t frag_size) } EXPORT_SYMBOL(napi_build_skb); =20 +static __always_inline size_t kmalloc_reserve_size(unsigned int size, gfp_= t flags) +{ + size_t obj_size =3D SKB_HEAD_ALIGN(size); + + if (obj_size <=3D SKB_SMALL_HEAD_CACHE_SIZE && + !(flags & KMALLOC_NOT_NORMAL_BITS)) + return 0; + + return kmalloc_size_roundup(obj_size); +} + static void *kmalloc_pfmemalloc(size_t obj_size, gfp_t flags, int node) { if (!gfp_pfmemalloc_allowed(flags)) @@ -612,9 +623,8 @@ static void *kmalloc_reserve(unsigned int *size, gfp_t = flags, int node, size_t obj_size; void *obj; =20 - obj_size =3D SKB_HEAD_ALIGN(*size); - if (obj_size <=3D SKB_SMALL_HEAD_CACHE_SIZE && - !(flags & KMALLOC_NOT_NORMAL_BITS)) { + obj_size =3D kmalloc_reserve_size(*size, flags); + if (!obj_size) { obj =3D kmem_cache_alloc_node(net_hotdata.skb_small_head_cache, flags | __GFP_NOMEMALLOC | __GFP_NOWARN, node); @@ -651,6 +661,16 @@ static void *kmalloc_reserve(unsigned int *size, gfp_t= flags, int node, return obj; } =20 +unsigned int compute_skb_truesize(unsigned int size, gfp_t flags) +{ + size_t obj_size =3D kmalloc_reserve_size(size, flags); + + if (!obj_size) + obj_size =3D SKB_SMALL_HEAD_CACHE_SIZE; + + return obj_size + SKB_DATA_ALIGN(sizeof(struct sk_buff)); +} + /* Allocate a new skbuff. We do this ourselves so we can fill in a few * 'private' fields and also do memory statistics to find all the * [BEEP] leaks. diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index 5bd42a74c6f5..0815e1891fba 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -5850,8 +5850,10 @@ static void tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *r= oot, struct sk_buff *head, struct sk_buff *tail, u32 start) { + int old_truesize, new_truesize; struct sk_buff *skb =3D head, *n; struct sk_buff_head tmp; + int remainder, nskbs; u8 scaling_ratio; u32 sub_end; =20 @@ -5891,6 +5893,7 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *li= st, struct rb_root *root, =20 /* Second, find consecutive collapsible skb. */ sub_end =3D TCP_SKB_CB(skb)->end_seq; + old_truesize =3D skb->truesize; =20 for (n =3D tcp_skb_next(skb, list); n && n !=3D tail; n =3D tcp_skb_next(= n, list)) { if (!skb_frags_readable(n) || @@ -5898,8 +5901,10 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *l= ist, struct rb_root *root, !tcp_skb_can_collapse_rx(skb, n)) break; =20 - if (!after(TCP_SKB_CB(n)->end_seq, sub_end)) + if (!after(TCP_SKB_CB(n)->end_seq, sub_end)) { + old_truesize +=3D n->truesize; continue; + } =20 /* seq and end_seq must increase monotonically for rbtree. */ if ((list || !before(TCP_SKB_CB(n)->seq, sub_end)) && @@ -5908,6 +5913,23 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *l= ist, struct rb_root *root, break; =20 sub_end =3D TCP_SKB_CB(n)->end_seq; + old_truesize +=3D n->truesize; + } + + /* Third, check if the new skbs have high density. */ + nskbs =3D (sub_end - start) / TCP_COLLAPSE_MAX_SIZE; + new_truesize =3D nskbs * SKB_TRUESIZE(TCP_COLLAPSE_MAX_SIZE); + + remainder =3D (sub_end - start) % TCP_COLLAPSE_MAX_SIZE; + if (remainder) { + nskbs++; + new_truesize +=3D compute_skb_truesize(remainder, GFP_ATOMIC); + } + + if (old_truesize <=3D new_truesize) { + start =3D sub_end; + skb =3D n; + goto restart; } =20 while (before(start, sub_end)) { --=20 2.56.0.679.gc527c65306-goog From nobody Mon Nov 23 20:27:01 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794870363; b=HHyPP8tiitt/EwBWVVVYHkwuidiDfSnMXLD39ZLja48awJFxX6Zc/9QB2g21AZeIFpIk ogmCsyCgl7aBLqbMeUUlzOtQgzfVKrUJAx3kqRINd6SZuCgP3JvRiOYVbOtMJrNOVY0dx RboG5SpMJQOA3MY6qv1TDVB1exjNbyUGI2mw7f9hKEI6tflwd7EhUfdh04XxH4BiMcsWX 6MOqtB2U3CZrVxFn2Tr/1ABdjZccPucOXloPW2iWgVK9dD3/R7z7c2+GajFw7dGCOa92d iUcqyjd2YoZSEfCNN1kSClVIv+fzEe2S0JsEGfJZl2vs85RTX8oZvmRcNnD4iExaMiA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794870363; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=Oxki1cduy7Jlef3gLyF5h+aCOZHnxGDcLpRSm3QnCQM=; b=1sfSaHuXlffCOOHc8e0QV6MpW/z48e8VkAAdKTVZkkQGFp8FFs1IJsAt239RaANQkR7V m4P00WZ3mlhrJlFu+VJa128tnfkP3oVM2bgBcCen0UnUn+vP2AIUmT+A/Mg0l9+fovoPi zh8MDtJpk9EK/HEUUuXGYkPeHX/x7TS9w/gdxMoepzvCTQ+v4jjc79xCGhoiiQ8axnbWa hq/7Chi976Yi/FYvVQJ26Du3pr2Nz2QmxXiEN/XuQ+DK65gB8t9rrGEDjE0VHpcztxMkb iIA9MZaLRq87URlEcKhPLFozVIZyN7VGpUNRp+aw40fghZp+rcqgdMk/Gm+KkWTHlnA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--kuniyu.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=j4sxo+lI; arc=none smtp.remote-ip=209.85.215.199 Received: from mail-pg1-f199.google.com (mail-pg1-f199.google.com [209.85.215.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9DD0D606A33D for ; Mon, 16 Nov 2026 23:06:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9DD0D606A33D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--kuniyu.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=j4sxo+lI Received: by mail-pg1-f199.google.com with SMTP id 41be03b00d2f7-cc4216aee8fso1920243a12.1 for ; Mon, 16 Nov 2026 15:06:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1794870362; x=1795475162; darn=lists.linux.dev; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=Oxki1cduy7Jlef3gLyF5h+aCOZHnxGDcLpRSm3QnCQM=; b=j4sxo+lIlx58kit6UudOGqXENr/uEGfowN7ZMuRNaQVBmLikcmbWQESXThjLEZ/mRW gNLsIRq8CbebTrgm/NJVDSLU2QwzXedUamYWlPR6spgTANhHUdLELkP4VnGsQA9fOkwp 7ZAzVEWNpTqDAL4JNSO4P2zYeJ3oVZC64fkjEPuwG/4eA6v+7cz/2Iz1ZiLqDYg93HAT dkGBd9YJmwWrn/ivVPN7OkRg6vs0WheA+LbUKGjfbdrkL9L2leP6BTfriNLRin53kg8U UszpG/gkw8k6qPluivni921RwI2ur1hk5NgxW8S/ZXM9trnr7SJiaTvSYP8oXv62YfBt 1lAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794870362; x=1795475162; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Oxki1cduy7Jlef3gLyF5h+aCOZHnxGDcLpRSm3QnCQM=; b=ExmjlE+t9WK/8rDpazbCw3ePJKaAF1bRuK0lo0RmYqtlNYc90UNW61CuUR/fAj9LfE 3mCv6rgzts7bsYEAg7fLl7yDwlItea2W3lfNUVQlwY4Rzpf6hcfBr4ZWyDW6suLPiGoZ Pp1UzaDBH3JUyK6jB7XDhehOi/BWdnOxaJoxV1oZHalg0hQ9YUIqRX4xT3vosJa23vds 2GPPHuFkyuu/J+TCK0WqXkF5louSvH1HJ/3nkhAaKKsP+fXjjtoDZRn1w4xxPXkKMTYy j6JO0CWpLaOoQmLmXwCVY6pdg22bAX/jyJnySEIR2F5lc9IGcOqz7aOf6Mlty8m1aWms VLew== X-Forwarded-Encrypted: i=1; AL+hErEsMd9vN8Wg5mGlO9vj34S2o7MoiF0cghJtIB0w6+YCOO2/yRzOcFc0yMF0nBYtI7foXSWcKg==@lists.linux.dev X-Gm-Message-State: APuU4Z+7UN8tJ7NbUoG+0T5iTrxJHu+z1SwkMfvIaoVINWOXY8hUXHGr EY3GaxOOgYavcWDMngVyMErkk3BmOvWa7BtRiPcJIQp0BaoN1OfMIja74OcEperpkelbtoVFa8D GpK66Og== X-Received: from pgam4.prod.google.com ([2002:a05:6a02:2b44:b0:cd8:bcc7:e588]) (user=kuniyu job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:890e:b0:3e2:9f41:9f63 with SMTP id adf61e73a8af0-3f7a49c08f7mr9647355637.1.1794870361768; Mon, 16 Nov 2026 15:06:01 -0800 (PST) Date: Mon, 16 Nov 2026 23:05:29 +0000 In-Reply-To: <20261116230555.2305867-1-kuniyu@google.com> X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261116230555.2305867-1-kuniyu@google.com> X-Mailer: git-send-email 2.56.0.679.gc527c65306-goog Message-ID: <20261116230555.2305867-6-kuniyu@google.com> Subject: [PATCH v2 net-next 5/7] tcp: Allocate all new skb before collapsing. From: Kuniyuki Iwashima To: Eric Dumazet , Neal Cardwell , "David S . Miller" , Jakub Kicinski , Paolo Abeni , Matthieu Baerts , Mat Martineau Cc: Simon Horman , Kuniyuki Iwashima , Kuniyuki Iwashima , netdev@vger.kernel.org, mptcp@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" At the final stage, tcp_collapse() iterates over collapsible candidate skbs and repacks them to new skbs. In each iteration, a new skb is allocated and inserted to the receive queue or a temporary list for RB tree. If alloc_skb() fails while splitting a large bloated skb across multiple new skbs, the partially-copied skb is not removed and new skbs remain in the queue as duplicates, consuming more memory. It is only when all candidates are properly collapsed that we can save memory. To avoid complicated rollback, let's preallocate skbs before collapsing. Now all skb in [start, sub_end) must be allocated in a batch, so the section size is normally capped at TCP_COLLAPSE_MAX_TOTAL_SIZE (16 skbs), except when a single bloated skb or overlapping OOO skbs must be collapsed together to free the original skb or keep the rbtree monotonic. Once the size exceeds the limit, the remaining skbs are re-evaluated from restart:. Note that the next skb can be skipped if its density already meets tp->scaling_ratio, and each cut adds at most one remainder skb (~3.4% worst-case truesize overhead on 4 KiB pages), which is a small tradeoff to bound the temporary allocation. Signed-off-by: Kuniyuki Iwashima Reviewed-by: Eric Dumazet --- net/ipv4/tcp_input.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index 0815e1891fba..ca4cdb93373d 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -5812,6 +5812,7 @@ static struct sk_buff *tcp_skb_next(struct sk_buff *s= kb, struct sk_buff_head *li #define TCP_COLLAPSE_SCALING_RATIO \ ((TCP_COLLAPSE_MAX_SIZE << TCP_RMEM_TO_WIN_SCALE) / \ (SKB_TRUESIZE(TCP_COLLAPSE_MAX_SIZE))) +#define TCP_COLLAPSE_MAX_TOTAL_SIZE (TCP_COLLAPSE_MAX_SIZE * 16) =20 static bool tcp_collapse_skb_bloated(const struct sk_buff *skb, u32 start, u8 scaling_ratio) @@ -5852,8 +5853,9 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *li= st, struct rb_root *root, { int old_truesize, new_truesize; struct sk_buff *skb =3D head, *n; - struct sk_buff_head tmp; + struct sk_buff_head tmp, pool; int remainder, nskbs; + struct sk_buff *nskb; u8 scaling_ratio; u32 sub_end; =20 @@ -5861,6 +5863,7 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *li= st, struct rb_root *root, TCP_COLLAPSE_SCALING_RATIO); =20 __skb_queue_head_init(&tmp); + __skb_queue_head_init(&pool); =20 /* First, check that queue is collapsible and find * the point where collapsing can be useful. @@ -5908,8 +5911,9 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *li= st, struct rb_root *root, =20 /* seq and end_seq must increase monotonically for rbtree. */ if ((list || !before(TCP_SKB_CB(n)->seq, sub_end)) && - !tcp_collapse_skb_bloated(n, sub_end, - TCP_COLLAPSE_SCALING_RATIO)) + (TCP_SKB_CB(n)->end_seq - start >=3D TCP_COLLAPSE_MAX_TOTAL_SIZE || + !tcp_collapse_skb_bloated(n, sub_end, + TCP_COLLAPSE_SCALING_RATIO))) break; =20 sub_end =3D TCP_SKB_CB(n)->end_seq; @@ -5924,6 +5928,8 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *li= st, struct rb_root *root, if (remainder) { nskbs++; new_truesize +=3D compute_skb_truesize(remainder, GFP_ATOMIC); + } else { + remainder =3D TCP_COLLAPSE_MAX_SIZE; } =20 if (old_truesize <=3D new_truesize) { @@ -5932,14 +5938,19 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *= list, struct rb_root *root, goto restart; } =20 - while (before(start, sub_end)) { - int copy =3D min_t(int, TCP_COLLAPSE_MAX_SIZE, sub_end - start); - struct sk_buff *nskb; - - nskb =3D alloc_skb(copy, GFP_ATOMIC); + while (nskbs--) { + nskb =3D alloc_skb(nskbs ? TCP_COLLAPSE_MAX_SIZE : remainder, + GFP_ATOMIC); if (!nskb) goto end; =20 + __skb_queue_tail(&pool, nskb); + } + + while (before(start, sub_end)) { + int copy =3D min_t(int, TCP_COLLAPSE_MAX_SIZE, sub_end - start); + + nskb =3D __skb_dequeue(&pool); memcpy(nskb->cb, skb->cb, sizeof(skb->cb)); TCP_SKB_CB(nskb)->has_rxtstamp =3D false; skb_copy_decrypted(nskb, skb); @@ -5985,6 +5996,8 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *li= st, struct rb_root *root, end: skb_queue_walk_safe(&tmp, skb, n) tcp_rbtree_insert(root, skb); + + __skb_queue_purge_reason(&pool, SKB_CONSUMED); } =20 /* Collapse ofo queue. Algorithm: select contiguous sequence of skbs --=20 2.56.0.679.gc527c65306-goog From nobody Mon Nov 23 20:27:01 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794870364; b=B3h8ECtAnkOb37RnO4mE5L7RFrkII1ZhoF7lW5iDKS3zG4MSF6JaTgmKiWTyNEu3EVUP GrVFtGSntSHCGs/5VrpybAnk8nPtfuK/WEGjvRmyOpx+0SqL5gRlHfb3Dl8967nkDjqW1 Nz7dfmYChwYnYXY6e8psYclou9gqw7K3yxmTIUvTX7mH+aZsHRtc1e8du+UIL0YIh4q1A lwTp7EjHyUg9tVBM3mJtcWEf4chQ+dLq4H/Izsnk0YYomnUVLFgebjlsfdNQ360jzc+iv MOyJ29+votaCw8/v3Nl0uS02F1REjQlTyKFFOd0hho1NK5tpdj4qmi/jkbMAOT0fkXA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794870364; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=+1bUuETZhuGuotXrQFC7vVEwK6wl12zrg9sjS68gw/E=; b=d2oL9trHfGuNbO45fUxCCiS82rUBoR+ymqmf41ncNphtvlkW5by7zggTHlfLP5UAxwZ+ 8IKq6VAwXqAb4y/MpuS7MD1b1wSFZQUuWvzrs62mxJ1Xc+657oaAA/hHLfBqo4BJwtQiB wKBKyOleL/xJKF1xAbIkvf4Gs0urDhf+RnYI5mOdeYk1G/5j4Bc1g6Yh36CEB8jeqeazY 8CHUidl4KVYSXxXuPqtMgl3DX5BAnMPx479X1FmfFxhCP6uPMj06tyufncSaMgpRG7zZl FFPyjcDX42L6mmJDj6MNXVWVkJnxv4jnYlIywCkJPa33jMW4MQW/9zfxszq87gTRkeg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--kuniyu.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=CJccxKiK; arc=none smtp.remote-ip=209.85.216.70 Received: from mail-pj1-f70.google.com (mail-pj1-f70.google.com [209.85.216.70]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 95CC0606A883 for ; Mon, 16 Nov 2026 23:06:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 95CC0606A883 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--kuniyu.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=CJccxKiK Received: by mail-pj1-f70.google.com with SMTP id 98e67ed59e1d1-3b3a74ee77fso377726a91.0 for ; Mon, 16 Nov 2026 15:06:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1794870363; x=1795475163; darn=lists.linux.dev; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=+1bUuETZhuGuotXrQFC7vVEwK6wl12zrg9sjS68gw/E=; b=CJccxKiKJ27WkRmFrfXmLxSC+BZCKRzHyNBrPg704V6JklkXsON9eYF58f7bGce84b 8ZTKLoa9Mxk/ObtiL9Lzl+uBbWaviOdpijK1nwEZXbeVgwZ6k8ekSycTpO17eqEcOWL7 jAdTQZ4rmn6bklMVP4JLhapvWDqnmasvTSeNvT7gEeErBzahBDOnkmpvQiWWCmb8BKQ1 dTS6umDl8myl8+PwY4eMlkVof6UGzFfDZaYQK+TtT/W3xJNw8Vl7ccZRTazO10kXsWqA NvxKTrCTPosVwKdRDT5SpqDGJH2pAVZJeHsD/UMLrdPDERFa89obBmoUzpNze24iwpfd +O/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794870363; x=1795475163; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=+1bUuETZhuGuotXrQFC7vVEwK6wl12zrg9sjS68gw/E=; b=k0QoXG5nMg46VgK3vsYgNWj5fwX4JlGjXkaaxpnzFms2hjcig4TYMuf40aF+x/57dV q6cKyxhuFRL176+983tRm4Ls/hqyeceDQGGJOwm4otmC4aXb5IkU2XCSo+fQaJim8+Hp 5yB/f+4jQP6GhIrj9QSYklv2PLptbF9jDMpNIY0nHf3wJn37+0RMp1BwsXD8uQFMXbAB Q2AqLrajnstrcRyywF6JvdLRvfckdHPAvyhV04lnneJvFlYMxi2JrPMjlqnkRZZqCoOg eywWA2AKiDVuIBNhJB770y/bXJbyVOo85iKkKWGW10phXkW26hSrV1bcuqU0CTM0/VUD 16MQ== X-Forwarded-Encrypted: i=1; AL+hErHMb3PkZmG2aaPPXkimq1Xe+ADFj78TsagbuuEBiHbDzfnwGPfWZPSWkTX2Ib9Rxub02VBEhQ==@lists.linux.dev X-Gm-Message-State: APuU4Z+uRrkuB/4+qUaGFVl/XfgoOpI6+bjl47bb9KiW12blvAX6HBf2 tHPsrtfi+iq5PB02qp7Cs5sS6DdqflgopOuJ0CF89c0q3MHtE+oZNKQEBSNvUMjbG5/FgtDWWbI 8Vl4Qug== X-Received: from pjpf15.prod.google.com ([2002:a17:90a:9b0f:b0:3ae:c243:313b]) (user=kuniyu job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:528c:b0:3ac:8897:7baa with SMTP id 98e67ed59e1d1-3b1b0a5030emr9587501a91.40.1794870362595; Mon, 16 Nov 2026 15:06:02 -0800 (PST) Date: Mon, 16 Nov 2026 23:05:30 +0000 In-Reply-To: <20261116230555.2305867-1-kuniyu@google.com> X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261116230555.2305867-1-kuniyu@google.com> X-Mailer: git-send-email 2.56.0.679.gc527c65306-goog Message-ID: <20261116230555.2305867-7-kuniyu@google.com> Subject: [PATCH v2 net-next 6/7] tcp: Batch memory accounting in tcp_collapse(). From: Kuniyuki Iwashima To: Eric Dumazet , Neal Cardwell , "David S . Miller" , Jakub Kicinski , Paolo Abeni , Matthieu Baerts , Mat Martineau Cc: Simon Horman , Kuniyuki Iwashima , Kuniyuki Iwashima , netdev@vger.kernel.org, mptcp@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Once tcp_collapse_one() calls sock_rfree(), skb->truesize is reclaimed to sk->sk_forward_alloc by sk_mem_uncharge(). However, sk_mem_uncharge() rounds down sk->sk_forward_alloc immediately once it reaches 4K since commit 4890b686f408 ("net: keep sk->sk_forward_alloc as small as possible"). This unnecessarily reclaims memory for newly allocated skbs, and the following skb_set_owner_r() for them underflows sk->sk_forward_alloc without sk_forced_mem_schedule(). Let's cache the delta during tcp_collapse() and batch accounting at the end. Then, we no longer need sk_forced_mem_schedule(), and instead DEBUG_NET_WARN_ON_ONCE() is added to catch a future issue. Signed-off-by: Kuniyuki Iwashima Reviewed-by: Eric Dumazet --- net/ipv4/tcp_input.c | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index ca4cdb93373d..713dcabd88b4 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -5822,6 +5822,24 @@ static bool tcp_collapse_skb_bloated(const struct sk= _buff *skb, return __tcp_win_from_space(scaling_ratio, skb->truesize) > unread; } =20 +static int tcp_collapse_uncharge(struct sk_buff *skb) +{ + DEBUG_NET_WARN_ON_ONCE(skb->destructor !=3D sock_rfree); + + skb->sk =3D NULL; + skb->destructor =3D NULL; + + return skb->truesize; +} + +static int tcp_collapse_charge(struct sk_buff *skb, struct sock *sk) +{ + skb->sk =3D sk; + skb->destructor =3D sock_rfree; + + return -skb->truesize; +} + static struct sk_buff *tcp_collapse_one(struct sock *sk, struct sk_buff *s= kb, struct sk_buff_head *list, struct rb_root *root) @@ -5857,6 +5875,7 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *li= st, struct rb_root *root, int remainder, nskbs; struct sk_buff *nskb; u8 scaling_ratio; + int released =3D 0; u32 sub_end; =20 scaling_ratio =3D min(tcp_sk(sk)->scaling_ratio, @@ -5874,6 +5893,7 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *li= st, struct rb_root *root, =20 /* No new bits? It is possible on ofo queue. */ if (!before(start, TCP_SKB_CB(skb)->end_seq)) { + released +=3D tcp_collapse_uncharge(skb); skb =3D tcp_collapse_one(sk, skb, list, root); goto restart; } @@ -5959,11 +5979,8 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *l= ist, struct rb_root *root, __skb_queue_before(list, skb, nskb); else __skb_queue_tail(&tmp, nskb); /* defer rbtree insertion */ - /* nskb is charged before the skbs it replaces are freed. - * Make sure sk->sk_forward_alloc can not become negative. - */ - sk_forced_mem_schedule(sk, nskb->truesize); - skb_set_owner_r(nskb, sk); + + released +=3D tcp_collapse_charge(nskb, sk); mptcp_skb_ext_move(nskb, skb); =20 /* Copy data, releasing collapsed skbs. */ @@ -5987,6 +6004,7 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *li= st, struct rb_root *root, start +=3D size; } if (!before(start, TCP_SKB_CB(skb)->end_seq)) { + released +=3D tcp_collapse_uncharge(skb); skb =3D tcp_collapse_one(sk, skb, list, root); } } @@ -5998,6 +6016,12 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *l= ist, struct rb_root *root, tcp_rbtree_insert(root, skb); =20 __skb_queue_purge_reason(&pool, SKB_CONSUMED); + + if (released) { + DEBUG_NET_WARN_ON_ONCE(released < 0); + atomic_sub(released, &sk->sk_rmem_alloc); + sk_mem_uncharge(sk, released); + } } =20 /* Collapse ofo queue. Algorithm: select contiguous sequence of skbs --=20 2.56.0.679.gc527c65306-goog From nobody Mon Nov 23 20:27:01 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794870365; b=28vbKK3TWjqpLnYnWDp+u67ymhzmODB6JS+4Fo/NqU4mC54E364ZLo0ib2Or/nm8iEyx ckSJB5JQhrsqEffeFb3gIwq0X/67VkCHOOI8lOR7v33qfE918GuKCGRNy43Xdns7wYlyJ WVwdO2YLWoaMW+0ULHEljgMYkTE5ULZEUah4iskJ0nW7VsgTKimcdKBdqYLZgNd9++XCq gMYuEzGGCnQa5WYnvgzvfQ+DrY7/N9BeO7hIxMx8aZGnNm7I2zRZALP3CRgBOS8sR6x1f Dtkm8Jn3i2zrO6tNMJJk6NPiMq2srY8sALL30klv1QBAOKc9KYe+dV3uWRivUinVeCg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794870365; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=jv4M7a0nbU2bS4cFmSjQv+EpzgY561iWKyksbxiqZK8=; b=KzaLDKabZF0LXk8GYXH/70ibeCFMv8z4OjfBxNtWBavnG0uiEW8qgLTDFkpCfW5ubaFv sQEmEuI+qIwnJ2vKMXT1wlgsV8gYCaRVIm5F1UNSs+q2ADbqbF0QiGJMaJTix1Ly9GvK6 AWcrpCaUdtw+TwoFrnB9WP3qlVBOH13fvYjmC6mNvtoPnl4J4U/NDHIGNSt1xXAm9MZSs u7EUAv8RR9AqxWjgAaNWFcAywX/+7GdmCGsmf9haP95QwNqxMPTyGlRsl9XvxSSsKMR5f J6T4J99vooV1ltCkbAMFbg12rT5iAFxCD+CSzM68++CLxZDUqE9xrAC5weZayoxx8Mg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--kuniyu.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=olnGKwNy; arc=none smtp.remote-ip=209.85.214.200 Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 950E3606A31C for ; Mon, 16 Nov 2026 23:06:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 950E3606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--kuniyu.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=olnGKwNy Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-2eaf3f829b3so33614505ad.3 for ; Mon, 16 Nov 2026 15:06:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1794870364; x=1795475164; darn=lists.linux.dev; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=jv4M7a0nbU2bS4cFmSjQv+EpzgY561iWKyksbxiqZK8=; b=olnGKwNyguynKViLDIs/8MYqVN4Q14DF+a9aJd6OGeQsggIl+agWFg+1s284ibjdxN pZ15st+2RThpVFofm+szxRCLJOHvxtYiz3ILyVNJlJf1iYblb0Pvwjv6qKlIMXU4gxId OyRG6qz+Di5z8bVNAAahrglZLR7go3rl2ubwMyhJjyyrFohgWGbXUIh83duq5Xlhrj0m cDT1tjnsY6U2LOMxq5UqA9Q0Ngf68da1scFY4oBao4yL5LIorwRYV59hb7dg7wZTGKEV 9Uwdy7A6NbwaLK50nu4GTBmsQyNK+A0C/pbyaKa9DcTlI2AFHvhDDTQueaW9A0W0ehFd amCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794870364; x=1795475164; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=jv4M7a0nbU2bS4cFmSjQv+EpzgY561iWKyksbxiqZK8=; b=h7sou0Azunr70vu38rlvQpiSrELfTmUT5r9BKGh1xHXRJt5NFgoVokjrBzBWp5g1Gy GOZQR57r/1stAO3ncBuqzbtly0fZ/m/VW6zsGNiEyKBceB9M1+s+OSkWZLnu4b+anKBG c4jonUDLeUdSLE4PE474645Z9vHuMgEbgGU85e4TwMIwKF4Exx7fVFknBh8cgPlFjs25 1nwKY5HDsv7IJd9U5wQKvClRSg5zVGA4yz9BsI3h9FITw+MmqTge5D5Kod8O3xnQEYNG tbL9BnC3If0FOO4zDpxKPKyApoehLy7GuJrri8ZziM9QksnKNj/iaFvySryrU9cqGyCf ScIQ== X-Forwarded-Encrypted: i=1; AL+hErEp9TG3a3l1noMK0zeyu8XumAllJVEaKrP3pzMWgG+3TATHNjl1NmCIu3iqSiWfb3iiku8LWQ==@lists.linux.dev X-Gm-Message-State: APuU4Z8xNZKnRLFgA6q2aVrfZeQuN9EEBFI1LClAdDeDyxFlqm6lYa7/ /f9PNZ+CtBV4zwRTW26f+9t4itBG0jpbo7fQn+nQzt6pmWnx7/7IVXnqc9b3DUfYpRNOgysFrx5 pV1JYXw== X-Received: from plbjy6.prod.google.com ([2002:a17:903:42c6:b0:2e8:7c73:6eb4]) (user=kuniyu job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:d590:b0:2dd:c053:82f6 with SMTP id d9443c01a7336-2ee0dc06de5mr102245185ad.45.1794870363434; Mon, 16 Nov 2026 15:06:03 -0800 (PST) Date: Mon, 16 Nov 2026 23:05:31 +0000 In-Reply-To: <20261116230555.2305867-1-kuniyu@google.com> X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261116230555.2305867-1-kuniyu@google.com> X-Mailer: git-send-email 2.56.0.679.gc527c65306-goog Message-ID: <20261116230555.2305867-8-kuniyu@google.com> Subject: [PATCH v2 net-next 7/7] selftest: packetdrill: Add test for tcp_collapse(). From: Kuniyuki Iwashima To: Eric Dumazet , Neal Cardwell , "David S . Miller" , Jakub Kicinski , Paolo Abeni , Matthieu Baerts , Mat Martineau Cc: Simon Horman , Kuniyuki Iwashima , Kuniyuki Iwashima , netdev@vger.kernel.org, mptcp@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" tcp_collapse() now does not collapse high-density skbs and a low-density skb sandwiched by high-density skbs. tcp_rcv_collapse_bloated.pkt verifies the behaviour by 1. set a large SO_RCVBUF 2. send one high-density skb with 100 bytes read() 3. send one high-density skb 4. send one low-density skb 5. send one high-density skb 6. send 30 low-density skbs 7. send one high-density skb 8. set a small SO_RCVBUF 9. send a small data to trigger tcp_collapse() 10. check TcpExtTCPRcvCollapsed via nstat Without the series, the first high-density skb is collapsed. In addition, the first low-density skb drags the following high-density skb into collapsing, consuming more memory. If we remove sk_forced_mem_schedule() without this series, sk->sk_forward_alloc becomes negative at the end of tcp_collapse(). Note that 30 segments are to make the memory savings large enough so that the margin can absorb minor size delta of struct sk_buff or L1_CACHE_BYTES across configs. skb truesize, TCP_COLLAPSE_MAX_SIZE, TCP_COLLAPSE_SCALING_RATIO depend on PAGE_SIZE being 4K, so the test is skipped if getconf returns non-4K PAGE_SIZE. Signed-off-by: Kuniyuki Iwashima --- .../selftests/net/packetdrill/ksft_runner.sh | 9 ++ .../packetdrill/tcp_rcv_collapse_bloated.pkt | 135 ++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 tools/testing/selftests/net/packetdrill/tcp_rcv_collaps= e_bloated.pkt diff --git a/tools/testing/selftests/net/packetdrill/ksft_runner.sh b/tools= /testing/selftests/net/packetdrill/ksft_runner.sh index 0a97d5ae3469..250d143bb9f7 100755 --- a/tools/testing/selftests/net/packetdrill/ksft_runner.sh +++ b/tools/testing/selftests/net/packetdrill/ksft_runner.sh @@ -44,6 +44,15 @@ if [ -z "$(which packetdrill)" ]; then exit "$KSFT_SKIP" fi =20 +page_size=3D$(grep -E '^--define=3D"?PAGE_SIZE=3D' $script | sed -E 's/.*P= AGE_SIZE=3D"?([0-9]+)"?.*/\1/') +if [[ -n "$page_size" ]]; then + cur_page_size=3D$(getconf PAGESIZE 2>/dev/null) + if [[ "$cur_page_size" !=3D "$page_size" ]]; then + ktap_skip_all "requires PAGE_SIZE=3D$page_size (current: ${cur_page_size= :-unknown})" + exit "$KSFT_SKIP" + fi +fi + declare -a optargs failfunc=3Dktap_test_fail =20 diff --git a/tools/testing/selftests/net/packetdrill/tcp_rcv_collapse_bloat= ed.pkt b/tools/testing/selftests/net/packetdrill/tcp_rcv_collapse_bloated.p= kt new file mode 100644 index 000000000000..90e8be86968f --- /dev/null +++ b/tools/testing/selftests/net/packetdrill/tcp_rcv_collapse_bloated.pkt @@ -0,0 +1,135 @@ +// SPDX-License-Identifier: GPL-2.0 +// +// Test that tcp_collapse() only collapses bloated skb runs, skips leading +// and trailing high-density skbs, skips isolated bloated skbs when collap= sing +// them would not shrink truesize, and properly recovers receive buffer sp= ace. + +--mss=3D1000 +--define=3D"PAGE_SIZE=3D4096" + +`./defaults.sh` + +// Establish a connection with a large receive buffer. +// __sock_set_rcvbuf() doubles, so 2000000 + +0 socket(..., SOCK_STREAM, IPPROTO_TCP) =3D 3 + +0 setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) =3D 0 + +0 setsockopt(3, SOL_SOCKET, SO_RCVBUF, [1000000], 4) =3D 0 + +0 bind(3, ..., ...) =3D 0 + +0 listen(3, 1) =3D 0 + + +0 < S 0:0(0) win 32792 + +0 > S. 0:0(0) ack 1 win 65535 + +0 < . 1:1(0) ack 1 win 65535 + + +0 accept(3, ..., ...) =3D 4 + +// 1. Queue two high-density 62,000 B segments (each adds 15 pages of frag +// truesize with enough margin across NET_SKB_PAD, L1_CACHE_BYTES, and +// MAX_SKB_FRAGS configs). +// Read 100 bytes from the first one so before(skb->seq, tp->copied_seq) i= s true. + +0 < P. 1:62001(62000) ack 1 win 65535 + * > . 1:1(0) ack 62001 + +0 read(4, ..., 100) =3D 100 + + +0 < P. 62001:124001(62000) ack 1 win 65535 + * > . 1:1(0) ack 124001 + +// 2. Queue an isolated bloated 1,000 B segment. + +0 < P. 124001:125001(1000) ack 1 win 65535 + * > . 1:1(0) ack 125001 + +// 3. Queue another high-density 62,000 B segment. + +0 < P. 125001:187001(62000) ack 1 win 65535 + * > . 1:1(0) ack 187001 + +// 4. Queue a run of 30 bloated 1,000 B segments. + +0 < P. 187001:188001(1000) ack 1 win 65535 + * > . 1:1(0) ack 188001 + +0 < P. 188001:189001(1000) ack 1 win 65535 + * > . 1:1(0) ack 189001 + +0 < P. 189001:190001(1000) ack 1 win 65535 + * > . 1:1(0) ack 190001 + +0 < P. 190001:191001(1000) ack 1 win 65535 + * > . 1:1(0) ack 191001 + +0 < P. 191001:192001(1000) ack 1 win 65535 + * > . 1:1(0) ack 192001 + +0 < P. 192001:193001(1000) ack 1 win 65535 + * > . 1:1(0) ack 193001 + +0 < P. 193001:194001(1000) ack 1 win 65535 + * > . 1:1(0) ack 194001 + +0 < P. 194001:195001(1000) ack 1 win 65535 + * > . 1:1(0) ack 195001 + +0 < P. 195001:196001(1000) ack 1 win 65535 + * > . 1:1(0) ack 196001 + +0 < P. 196001:197001(1000) ack 1 win 65535 + * > . 1:1(0) ack 197001 + +0 < P. 197001:198001(1000) ack 1 win 65535 + * > . 1:1(0) ack 198001 + +0 < P. 198001:199001(1000) ack 1 win 65535 + * > . 1:1(0) ack 199001 + +0 < P. 199001:200001(1000) ack 1 win 65535 + * > . 1:1(0) ack 200001 + +0 < P. 200001:201001(1000) ack 1 win 65535 + * > . 1:1(0) ack 201001 + +0 < P. 201001:202001(1000) ack 1 win 65535 + * > . 1:1(0) ack 202001 + +0 < P. 202001:203001(1000) ack 1 win 65535 + * > . 1:1(0) ack 203001 + +0 < P. 203001:204001(1000) ack 1 win 65535 + * > . 1:1(0) ack 204001 + +0 < P. 204001:205001(1000) ack 1 win 65535 + * > . 1:1(0) ack 205001 + +0 < P. 205001:206001(1000) ack 1 win 65535 + * > . 1:1(0) ack 206001 + +0 < P. 206001:207001(1000) ack 1 win 65535 + * > . 1:1(0) ack 207001 + +0 < P. 207001:208001(1000) ack 1 win 65535 + * > . 1:1(0) ack 208001 + +0 < P. 208001:209001(1000) ack 1 win 65535 + * > . 1:1(0) ack 209001 + +0 < P. 209001:210001(1000) ack 1 win 65535 + * > . 1:1(0) ack 210001 + +0 < P. 210001:211001(1000) ack 1 win 65535 + * > . 1:1(0) ack 211001 + +0 < P. 211001:212001(1000) ack 1 win 65535 + * > . 1:1(0) ack 212001 + +0 < P. 212001:213001(1000) ack 1 win 65535 + * > . 1:1(0) ack 213001 + +0 < P. 213001:214001(1000) ack 1 win 65535 + * > . 1:1(0) ack 214001 + +0 < P. 214001:215001(1000) ack 1 win 65535 + * > . 1:1(0) ack 215001 + +0 < P. 215001:216001(1000) ack 1 win 65535 + * > . 1:1(0) ack 216001 + +0 < P. 216001:217001(1000) ack 1 win 65535 + * > . 1:1(0) ack 217001 + +// 5. Queue a trailing high-density 62,000 B segment. + +0 < P. 217001:279001(62000) ack 1 win 65535 + * > . 1:1(0) ack 279001 + +// Reset SNMP counters. + +0 `nstat -n` + +// Shrink SO_RCVBUF so sk_rcvbuf (320,000) is smaller than current +// sk_rmem_alloc (334,592 on x86_64 default), but larger than sk_rmem_alloc +// after collapsing the 30 bloated skbs (300,288 on x86_64 default). + +0 setsockopt(4, SOL_SOCKET, SO_RCVBUF, [160000], 4) =3D 0 + +// Inject the next 1,000 B segment with a changed window to bypass +// header prediction. This triggers tcp_prune_queue() -> tcp_collapse(). +// Only the 30 bloated segments should be collapsed; the high-density +// segments and the isolated bloated segment are skipped. sk_rmem_alloc +// shrinks below sk_rcvbuf, so tcp_can_ingest() succeeds and the packet +// is accepted. + +0 < P. 279001:280001(1000) ack 1 win 65534 + * > . 1:1(0) ack 280001 + +// Verify exactly 30 skbs were collapsed. + +0 `nstat -s | grep TcpExtTCPRcvCollapsed | grep -q " 30 "` + +// Verify no packets were dropped. + +0 `test -z "$(nstat -s | grep TcpExtTCPRcvQDrop)"` + +// Read all remaining queued data to verify data integrity. + +0 read(4, ..., 300000) =3D 279900 --=20 2.56.0.679.gc527c65306-goog