Skip to contentSkip to conversation

tcp: refactor skb_cmp_decrypted() checks

PATCH Applied Jakub Kicinski 6 participants 15 messages May 30, 2024 – Jun 4, 2024

Subsystems (5)
lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

cover tcp: refactor skb_cmp_decrypted() checks

Refactor the input patch coalescing checks and wrap "EOR forcing" logic into a helper. This will hopefully make the code easier to follow. While at it throw some DEBUG_NET checks into skb_shift().

Jakub Kicinski (3):
tcp: wrap mptcp and decrypted checks into tcp_skb_can_collapse_rx()
tcp: add a helper for setting EOR on tail skb
net: skb: add compatibility warnings to skb_shift()

 include/net/tcp.h    | 16 ++++++++++++++++
 net/core/skbuff.c    |  3 +++
 net/ipv4/tcp_input.c | 11 +++--------
 net/ipv4/tcp_ipv4.c  |  3 +--
 net/tls/tls_device.c | 11 ++---------
 5 files changed, 25 insertions(+), 19 deletions(-)
Signature · 1 line
2.45.1
author maintainer
#1 lore raw

PATCH 1/3 tcp: wrap mptcp and decrypted checks into tcp_skb_can_collapse_rx()

tcp_skb_can_collapse() checks for conditions which don't make sense on input. Because of this we ended up sprinkling a few pairs of mptcp_skb_can_collapse() and skb_cmp_decrypted() calls on the input path. Group them in a new helper. This should make it less likely that someone will check mptcp and not decrypted or vice versa when adding new code.

This implicitly adds a decrypted check early in tcp_collapse(). AFAIU this will very slightly increase our ability to collapse packets under memory pressure, not a real bug.

---
 include/net/tcp.h    |  7 +++++++
 net/ipv4/tcp_input.c | 11 +++--------
 net/ipv4/tcp_ipv4.c  |  3 +--
 3 files changed, 11 insertions(+), 10 deletions(-)
include/net/tcp.h +7 -0
index 32815a40dea1..32741856da01 100644
@@ -1071,6 +1071,13 @@ static inline bool tcp_skb_can_collapse(const struct sk_buff *to,
 		      skb_pure_zcopy_same(to, from));
 }
 
+static inline bool tcp_skb_can_collapse_rx(const struct sk_buff *to,
+					   const struct sk_buff *from)
+{
+	return likely(mptcp_skb_can_collapse(to, from) &&
+		      !skb_cmp_decrypted(to, from));
+}
+
 /* Events passed to congestion control interface */
 enum tcp_ca_event {
 	CA_EVENT_TX_START,	/* first transmit when no packets in flight */
net/ipv4/tcp_input.c +3 -8
index 5aadf64e554d..212b6fd0caf7 100644
@@ -4813,10 +4813,7 @@ static bool tcp_try_coalesce(struct sock *sk,
 	if (TCP_SKB_CB(from)->seq != TCP_SKB_CB(to)->end_seq)
 		return false;
 
-	if (!mptcp_skb_can_collapse(to, from))
-		return false;
-
-	if (skb_cmp_decrypted(from, to))
+	if (!tcp_skb_can_collapse_rx(to, from))
 		return false;
 
 	if (!skb_try_coalesce(to, from, fragstolen, &delta))
@@ -5372,7 +5369,7 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
 			break;
 		}
 
-		if (n && n != tail && mptcp_skb_can_collapse(skb, n) &&
+		if (n && n != tail && tcp_skb_can_collapse_rx(skb, n) &&
 		    TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(n)->seq) {
 			end_of_skbs = false;
 			break;
@@ -5423,11 +5420,9 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
 				skb = tcp_collapse_one(sk, skb, list, root);
 				if (!skb ||
 				    skb == tail ||
-				    !mptcp_skb_can_collapse(nskb, skb) ||
+				    !tcp_skb_can_collapse_rx(nskb, skb) ||
 				    (TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)))
 					goto end;
-				if (skb_cmp_decrypted(skb, nskb))
-					goto end;
 			}
 		}
 	}
net/ipv4/tcp_ipv4.c +1 -2
index 041c7eda9abe..228de0c95a9d 100644
@@ -2049,8 +2049,7 @@ bool tcp_add_backlog(struct sock *sk, struct sk_buff *skb,
 	      TCP_SKB_CB(skb)->tcp_flags) & TCPHDR_ACK) ||
 	    ((TCP_SKB_CB(tail)->tcp_flags ^
 	      TCP_SKB_CB(skb)->tcp_flags) & (TCPHDR_ECE | TCPHDR_CWR)) ||
-	    !mptcp_skb_can_collapse(tail, skb) ||
-	    skb_cmp_decrypted(tail, skb) ||
+	    !tcp_skb_can_collapse_rx(tail, skb) ||
 	    thtail->doff != th->doff ||
 	    memcmp(thtail + 1, th + 1, hdrlen - sizeof(*th)))
 		goto no_coalesce;
Signature · 1 line
2.45.1
author maintainer
#5 lore raw

PATCH 2/3 tcp: add a helper for setting EOR on tail skb

In reply to Jakub Kicinski

TLS (and hopefully soon PSP will) use EOR to prevent skbs with different decrypted state from getting merged, without adding new tests to the skb handling. In both cases once the connection switches to an "encrypted" state, all subsequent skbs will be encrypted, so a single "EOR fence" is sufficient to prevent mixing.

Add a helper for setting the EOR bit, to make this arrangement more explicit.

---
 include/net/tcp.h    |  9 +++++++++
 net/tls/tls_device.c | 11 ++---------
 2 files changed, 11 insertions(+), 9 deletions(-)
include/net/tcp.h +9 -0
index 32741856da01..08c3b99501cf 100644
@@ -1066,6 +1066,7 @@ static inline bool tcp_skb_can_collapse_to(const struct sk_buff *skb)
 static inline bool tcp_skb_can_collapse(const struct sk_buff *to,
 					const struct sk_buff *from)
 {
+	/* skb_cmp_decrypted() not needed, use tcp_write_collapse_fence() */
 	return likely(tcp_skb_can_collapse_to(to) &&
 		      mptcp_skb_can_collapse(to, from) &&
 		      skb_pure_zcopy_same(to, from));
@@ -2102,6 +2103,14 @@ static inline void tcp_rtx_queue_unlink_and_free(struct sk_buff *skb, struct soc
 	tcp_wmem_free_skb(sk, skb);
 }
 
+static inline void tcp_write_collapse_fence(struct sock *sk)
+{
+	struct sk_buff *skb = tcp_write_queue_tail(sk);
+
+	if (skb)
+		TCP_SKB_CB(skb)->eor = 1;
+}
+
 static inline void tcp_push_pending_frames(struct sock *sk)
 {
 	if (tcp_send_head(sk)) {
net/tls/tls_device.c +2 -9
index ab6e694f7bc2..dc063c2c7950 100644
@@ -231,14 +231,10 @@ static void tls_device_resync_tx(struct sock *sk, struct tls_context *tls_ctx,
 				 u32 seq)
 {
 	struct net_device *netdev;
-	struct sk_buff *skb;
 	int err = 0;
 	u8 *rcd_sn;
 
-	skb = tcp_write_queue_tail(sk);
-	if (skb)
-		TCP_SKB_CB(skb)->eor = 1;
-
+	tcp_write_collapse_fence(sk);
 	rcd_sn = tls_ctx->tx.rec_seq;
 
 	trace_tls_device_tx_resync_send(sk, seq, rcd_sn);
@@ -1067,7 +1063,6 @@ int tls_set_device_offload(struct sock *sk)
 	struct tls_prot_info *prot;
 	struct net_device *netdev;
 	struct tls_context *ctx;
-	struct sk_buff *skb;
 	char *iv, *rec_seq;
 	int rc;
 
@@ -1138,9 +1133,7 @@ int tls_set_device_offload(struct sock *sk)
 	 * SKBs where only part of the payload needs to be encrypted.
 	 * So mark the last skb in the write queue as end of record.
 	 */
-	skb = tcp_write_queue_tail(sk);
-	if (skb)
-		TCP_SKB_CB(skb)->eor = 1;
+	tcp_write_collapse_fence(sk);
 
 	/* Avoid offloading if the device is down
 	 * We don't want to offload new flows after
Signature · 1 line
2.45.1
author maintainer
#10 lore raw

PATCH 3/3 net: skb: add compatibility warnings to skb_shift()

In reply to Jakub Kicinski

According to current semantics we should never try to shift data between skbs which differ on decrypted or pp_recycle status.

---
 net/core/skbuff.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/skbuff.c +3 -0
index 466999a7515e..c8ac79851cd6 100644
@@ -4139,6 +4139,9 @@ int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
 	if (skb_zcopy(tgt) || skb_zcopy(skb))
 		return 0;
 
+	DEBUG_NET_WARN_ON_ONCE(tgt->pp_recycle != skb->pp_recycle);
+	DEBUG_NET_WARN_ON_ONCE(skb_cmp_decrypted(tgt, skb));
+
 	todo = shiftlen;
 	from = 0;
 	to = skb_shinfo(tgt)->nr_frags;
Signature · 1 line
2.45.1
#13 lore raw
In reply to Jakub Kicinski

Hi Jakub,

Thank you for your modifications, that's great!

Our CI did some validations and here is its report:

- KVM Validation: normal: Success! ✅
- KVM Validation: debug: Success! ✅
- KVM Validation: btf (only bpftest_all): Success! ✅
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/9310667506

Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/9ff802fa78d3
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=857501

If there are some issues, you can reproduce them using the same environment as the one used by the CI thanks to a docker image, e.g.:

    $ cd [kernel source code]
    $ docker run -v "${PWD}:${PWD}:rw" -w "${PWD}" --privileged --rm -it \
        --pull always mptcp/mptcp-upstream-virtme-docker:latest \
        auto-normal

For more details:

https://github.com/multipath-tcp/mptcp-upstream-virtme-docker

Please note that despite all the efforts that have been already done to have a stable tests suite when executed on a public CI like here, it is possible some reported issues are not due to your modifications. Still, do not hesitate to help us improve that ;-)

Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)

Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH net-next 1/3] tcp: wrap mptcp and decrypted checks into tcp_skb_can_collapse_rx()

In reply to Jakub Kicinski
Quoted 8 lines · Jakub Kicinski

tcp_skb_can_collapse() checks for conditions which don't make sense on input. Because of this we ended up sprinkling a few pairs of mptcp_skb_can_collapse() and skb_cmp_decrypted() calls on the input path. Group them in a new helper. This should make it less likely that someone will check mptcp and not decrypted or vice versa when adding new code.

Quoted 5 lines

This implicitly adds a decrypted check early in tcp_collapse(). AFAIU this will very slightly increase our ability to collapse packets under memory pressure, not a real bug.

Reviewed-by on v1 2/3
#6 lore raw

Re: [PATCH net-next 2/3] tcp: add a helper for setting EOR on tail skb

In reply to Jakub Kicinski
Quoted 8 lines · Jakub Kicinski

TLS (and hopefully soon PSP will) use EOR to prevent skbs with different decrypted state from getting merged, without adding new tests to the skb handling. In both cases once the connection switches to an "encrypted" state, all subsequent skbs will be encrypted, so a single "EOR fence" is sufficient to prevent mixing.

Quoted 4 lines

Add a helper for setting the EOR bit, to make this arrangement more explicit.

maintainer Reviewed-by on v1 1/3
#3 lore raw

Re: [PATCH net-next 1/3] tcp: wrap mptcp and decrypted checks into tcp_skb_can_collapse_rx()

In reply to Jakub Kicinski

Hi Jakub,

Quoted 10 lines · Jakub Kicinski

tcp_skb_can_collapse() checks for conditions which don't make sense on input. Because of this we ended up sprinkling a few pairs of mptcp_skb_can_collapse() and skb_cmp_decrypted() calls on the input path. Group them in a new helper. This should make it less likely that someone will check mptcp and not decrypted or vice versa when adding new code.

This implicitly adds a decrypted check early in tcp_collapse(). AFAIU this will very slightly increase our ability to collapse packets under memory pressure, not a real bug.

Good idea! Thank you for this refactoring, and for having cc'd MPTCP ML:

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.
Reviewed-by on v1 2/3
#7 lore raw

Re: [PATCH net-next 2/3] tcp: add a helper for setting EOR on tail skb

In reply to Jakub Kicinski
Quoted 11 lines · Jakub Kicinski

TLS (and hopefully soon PSP will) use EOR to prevent skbs with different decrypted state from getting merged, without adding new tests to the skb handling. In both cases once the connection switches to an "encrypted" state, all subsequent skbs will be encrypted, so a single "EOR fence" is sufficient to prevent mixing.

Add a helper for setting the EOR bit, to make this arrangement more explicit.

Quoted 18 lines
---
 include/net/tcp.h    |  9 +++++++++
 net/tls/tls_device.c | 11 ++---------
 2 files changed, 11 insertions(+), 9 deletions(-)
include/net/tcp.h +1 -0
index 32741856da01..08c3b99501cf 100644
@@ -1066,6 +1066,7 @@ static inline bool tcp_skb_can_collapse_to(const struct sk_buff *skb)
 static inline bool tcp_skb_can_collapse(const struct sk_buff *to,
 					const struct sk_buff *from)
 {
+	/* skb_cmp_decrypted() not needed, use tcp_write_collapse_fence() */
 	return likely(tcp_skb_can_collapse_to(to) &&
 		      mptcp_skb_can_collapse(to, from) &&
 		      skb_pure_zcopy_same(to, from));

include/net/tcp.h static inline void tcp_rtx_queue_unlink_and_free(struct sk_buff *skb, struct soc · line 2107

Quoted 5 lines
include/net/tcp.h +2 -0
@@ -2102,6 +2103,14 @@ static inline void tcp_rtx_queue_unlink_and_free(struct sk_buff *skb, struct soc
 	tcp_wmem_free_skb(sk, skb);
 }
 
+static inline void tcp_write_collapse_fence(struct sock *sk)
+{

const struct ptr?

Quoted 5 lines
include/net/tcp.h +5 -0
+	struct sk_buff *skb = tcp_write_queue_tail(sk);
+
+	if (skb)
+		TCP_SKB_CB(skb)->eor = 1;
+}
Reviewed-by on v1 1/3
#4 lore raw

Re: [PATCH net-next 1/3] tcp: wrap mptcp and decrypted checks into tcp_skb_can_collapse_rx()

In reply to Jakub Kicinski
Quoted 7 lines · Jakub Kicinski

tcp_skb_can_collapse() checks for conditions which don't make sense on input. Because of this we ended up sprinkling a few pairs of mptcp_skb_can_collapse() and skb_cmp_decrypted() calls on the input path. Group them in a new helper. This should make it less likely that someone will check mptcp and not decrypted or vice versa when adding new code.

Quoted 5 lines

This implicitly adds a decrypted check early in tcp_collapse(). AFAIU this will very slightly increase our ability to collapse packets under memory pressure, not a real bug.

author maintainer
#8 lore raw

Re: [PATCH net-next 2/3] tcp: add a helper for setting EOR on tail skb

In reply to Willem de Bruijn
Quoted 4 lines · On Fri, 31 May 2024 09:41:30 -0400 Willem de Bruijn wrote:

include/net/tcp.h static inline void tcp_rtx_queue_unlink_and_free(struct sk_buff *skb, struct soc · line 2107

Quoted 2 lines
include/net/tcp.h +2 -0
+static inline void tcp_write_collapse_fence(struct sock *sk)
+{  

const struct ptr?

Maybe just me, but feels kinda weird for the sole input to be const if the function does modify the object it operates on.

#9 lore raw

Re: [PATCH net-next 2/3] tcp: add a helper for setting EOR on tail skb

Quoted 8 lines · Jakub Kicinski
Quoted 4 lines · On Fri, 31 May 2024 09:41:30 -0400 Willem de Bruijn wrote:

include/net/tcp.h static inline void tcp_rtx_queue_unlink_and_free(struct sk_buff *skb, struct soc · line 2107

Quoted 2 lines
include/net/tcp.h +2 -0
+static inline void tcp_write_collapse_fence(struct sock *sk)
+{  

const struct ptr?

Maybe just me, but feels kinda weird for the sole input to be const if the function does modify the object it operates on.

Ok. I can see that.

#14 lore raw
In reply to Jakub Kicinski
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Quoted 10 lines · On Thu, 30 May 2024 16:36:13 -0700 you wrote:

Refactor the input patch coalescing checks and wrap "EOR forcing" logic into a helper. This will hopefully make the code easier to follow. While at it throw some DEBUG_NET checks into skb_shift().

Jakub Kicinski (3):
tcp: wrap mptcp and decrypted checks into tcp_skb_can_collapse_rx()
tcp: add a helper for setting EOR on tail skb
net: skb: add compatibility warnings to skb_shift()

[...]

Here is the summary with links:
- [net-next,1/3] tcp: wrap mptcp and decrypted checks into tcp_skb_can_collapse_rx()

    https://git.kernel.org/netdev/net-next/c/071115301838
  - [net-next,2/3] tcp: add a helper for setting EOR on tail skb
    https://git.kernel.org/netdev/net-next/c/1be68a87ab33
  - [net-next,3/3] net: skb: add compatibility warnings to skb_shift()
    https://git.kernel.org/netdev/net-next/c/99b8add01f98

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html