Skip to contentSkip to conversation

sendmsg: fix pending issues.

PATCH Superseded Paolo Abeni 3 participants 11 messages Dec 2, 2019 – Dec 4, 2019

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Paolo Abeni

author
#0 lore raw

cover sendmsg: fix pending issues.

This is just a rebase of the initial RFC post.

The patch 1 should be inserted just after "tcp: clean ext on tx recycle"

The patches 2 and 3 have explicit individual squash-to tags, but will casue a lot of conflicts, the uneasy ones to solve are on patch
"mptcp: rework mptcp_sendmsg_frag to accept optional dfrag"

A tree rebased with the above patches applied is available at:

https://github.com/pabeni/mptcp/tree/sendmsg_fix_2

The original cover letter was as follow (TL;DR: patch 1 and 2 can be controversial, feedback needed!!!)
---

This series addresses the known pending issue in mptcp_sendmsg, specifically:

* the crash repored by matt (patch 3/3)
* the possible data corruption on ext allocation failure - the "TODO"" in sendmsg_frag (patches 1,2/3)

uunfortunately none of the above is actually tested, since I can't reproduce the issues - still the problems look real.

This is not squashed yet, as it will conflict with most patches, I'm just trying to collect feedback.

Open points:

The patch 2/3 introduces a per socket cache of a single skb_ext. As per recent upstream changes related to TCP rx/tx skb cache, that is debatable, could have subtle performance and memory usage implications. We could avoid such cache allways allocating the extension for each sendmsg_frag() call, and than free it if unused. Both options looks sub-optiomal to me do you have any preference? - I do have some for the selected impl ;)

The new API exposed by patch 1/3 is open to misusage. e.g. the caller could allocate a skb_ext, set arbitrary ext->offset[id] values and than call __skb_ext_add(). That will lead to memory leak or GPF. I did no try to address the issue, the '__' prefix should guide the users to extra care. Can we do any better?

(this is a pre-existing topic) the sk_stream_wait_memory() calls in sk_stream_wait_memory(), waits on the subflow. Should we wait on the mptcp socket instead? Otherwise should we release the mptcp socket lock, before such call?

Paolo Abeni (1):
skb: add helpers to allocate ext independently from sk_buff

 include/linux/skbuff.h |  3 +++
 net/core/skbuff.c      | 16 +++++++++++++++-
 2 files changed, 18 insertions(+), 1 deletion(-)
Signature · 1 line
2.21.0
author
#1 lore raw

PATCH 1/3 skb: add helpers to allocate ext independently from sk_buff

Currently we can allocate the extension only after the skb, this change allow the user to do the opposite, will simplify allocation failure handling from MPTCP.

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>
---
 include/linux/skbuff.h |  3 +++
 net/core/skbuff.c      | 16 +++++++++++++++-
 2 files changed, 18 insertions(+), 1 deletion(-)
include/linux/skbuff.h +3 -0
index f641fc76f47e..1bbb77640045 100644
@@ -4111,6 +4111,9 @@ struct skb_ext {
 	char data[0] __aligned(8);
 };
 
+struct skb_ext *skb_ext_alloc(void);
+void *__skb_ext_add(struct sk_buff *skb, enum skb_ext_id id,
+		    struct skb_ext *ext);
 void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id);
 void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id);
 void __skb_ext_put(struct skb_ext *ext);
net/core/skbuff.c +15 -1
index 0aba2cd69881..e8ed6d28fba8 100644
@@ -5981,7 +5981,7 @@ static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
 	return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
 }
 
-static struct skb_ext *skb_ext_alloc(void)
+struct skb_ext *skb_ext_alloc(void)
 {
 	struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
 
@@ -6021,6 +6021,20 @@ static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
 	return new;
 }
 
+void *__skb_ext_add(struct sk_buff *skb, enum skb_ext_id id,
+		    struct skb_ext *ext)
+{
+	unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext);
+
+	skb_ext_put(skb);
+	newlen = newoff + skb_ext_type_len[id];
+	ext->chunks = newlen;
+	ext->offset[id] = newoff;
+	skb->extensions = ext;
+	skb->active_extensions |= 1 << id;
+	return skb_ext_get_ptr(ext, id);
+}
+
 /**
  * skb_ext_add - allocate space for given extension, COW if needed
  * @skb: buffer
Signature · 1 line
2.21.0
author
#4 lore raw

PATCH 2/3 mptcp: avoid data stream corruption on allocation failure.

In reply to Paolo Abeni

Currently, when if mpext allocation fails, the data stream is corrupted, as we can add the required DSS nor roll-back the TCP status.

Allocate a skb_ext before the actuall skb_buff creation/TCP status update: in case of memory allocation failure we can bail early, avoiding the above condition.

Additionally, if the ext is unused to to skb collapsing, cache the ext in the msk for the next send.

Squash-to: "mptcp: Write MPTCP DSS headers to outgoing data packets"
Signed-off-by: Paolo Abeni <pabeni(a)redhat.com>

---
 net/mptcp/protocol.c | 39 +++++++++++++++++++++++++--------------
 net/mptcp/protocol.h |  1 +
 2 files changed, 26 insertions(+), 14 deletions(-)
net/mptcp/protocol.c +25 -14
index 3180202e56b4..ae158ab08101 100644
@@ -55,6 +55,14 @@ static struct sock *mptcp_subflow_get(const struct mptcp_sock *msk)
 	return NULL;
 }
 
+static bool mptcp_ext_cache_refill(struct mptcp_sock *msk)
+{
+	if (!msk->cached_ext)
+		msk->cached_ext = skb_ext_alloc();
+
+	return msk->cached_ext;
+}
+
 static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 			      struct msghdr *msg, long *timeo)
 {
@@ -69,7 +77,8 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 	 * from one substream to another, but do per subflow memory accounting
 	 */
 	pfrag = sk_page_frag(sk);
-	while (!sk_page_frag_refill(ssk, pfrag)) {
+	while (!sk_page_frag_refill(ssk, pfrag) ||
+	       !mptcp_ext_cache_refill(msk)) {
 		ret = sk_stream_wait_memory(ssk, timeo);
 		if (ret)
 			return ret;
@@ -103,19 +112,19 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 		iov_iter_revert(&msg->msg_iter, psize - ret);
 
 	skb = tcp_write_queue_tail(ssk);
-	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
-	if (mpext) {
-		memset(mpext, 0, sizeof(*mpext));
-		mpext->data_seq = msk->write_seq;
-		mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
-		mpext->data_len = ret;
-		mpext->use_map = 1;
-		mpext->dsn64 = 1;
-
-		pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d",
-			 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
-			 mpext->dsn64);
-	} /* TODO: else fallback */
+	mpext = __skb_ext_add(skb, SKB_EXT_MPTCP, msk->cached_ext);
+	msk->cached_ext = NULL;
+
+	memset(mpext, 0, sizeof(*mpext));
+	mpext->data_seq = msk->write_seq;
+	mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
+	mpext->data_len = ret;
+	mpext->use_map = 1;
+	mpext->dsn64 = 1;
+
+	pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d",
+		 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
+		 mpext->dsn64);
 
 	pfrag->offset += ret;
 	msk->write_seq += ret;
@@ -242,6 +251,8 @@ static void mptcp_close(struct sock *sk, long timeout)
 		sock_release(mptcp_subflow_tcp_socket(subflow));
 	}
 
+	if (msk->cached_ext)
+		__skb_ext_put(msk->cached_ext);
 	release_sock(sk);
 	sk_common_release(sk);
 }
net/mptcp/protocol.h +1 -0
index 412718c1ae5c..3b8d1548dda1 100644
@@ -58,6 +58,7 @@ struct mptcp_sock {
 	u64		write_seq;
 	u64		ack_seq;
 	u32		token;
+	struct	skb_ext *cached_ext;	/* for the next sendmsg */
 	struct list_head conn_list;
 	struct socket	*subflow; /* outgoing connect/listener/!mp_capable */
 };
Signature · 1 line
2.21.0
author
#6 lore raw

PATCH 3/3 imptcp: properly detect skb collapsing

In reply to Paolo Abeni

Currently we can misdetect skb collapsing, which will lead to a kernel warning and crash.

Changing a bit the detection logic for skb collapsing we can address the issue.

Squash-to: "mptcp: allow collapsing consecutive sendpages on the same substream"
Signed-off-by: Paolo Abeni <pabeni(a)redhat.com>

---
 net/mptcp/protocol.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
net/mptcp/protocol.c +9 -3
index 1c489eccdb58..ba918c4fdcc8 100644
@@ -107,8 +107,8 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	bool collapsed, can_collapse = false;
 	struct mptcp_ext *mpext = NULL;
+	struct sk_buff *skb, *tail;
 	struct page_frag *pfrag;
-	struct sk_buff *skb;
 	size_t psize;
 
 	/* use the mptcp page cache so that we can easily move the data
@@ -165,10 +165,16 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 	if (unlikely(ret < psize))
 		iov_iter_revert(&msg->msg_iter, psize - ret);
 
-	collapsed = skb == tcp_write_queue_tail(ssk);
+	/* if the tail skb extension is still the cached one, collapsing
+	 * really happened. Note: we can't check for 'same skb' as the sk_buff
+	 * hdr on tail can be transmitted, freed and re-allocated by the
+	 * do_tcp_sendpages() call
+	 */
+	tail = tcp_write_queue_tail(ssk);
+	collapsed = mpext &&
+		    mpext == (tail ? skb_ext_find(tail, SKB_EXT_MPTCP) : NULL);
 	if (collapsed) {
 		WARN_ON_ONCE(!can_collapse);
-		/* when collapsing mpext always exists */
 		mpext->data_len += ret;
 		goto out;
 	}
Signature · 1 line
2.21.0
#9 lore raw
In reply to Paolo Abeni

Hi Paolo,

Quoted 1 line · Paolo Abeni

This is just a rebase of the initial RFC post.

Thank you for the rebase!

Quoted 9 lines

The patch 1 should be inserted just after "tcp: clean ext on tx recycle"

The patches 2 and 3 have explicit individual squash-to tags, but will casue a lot of conflicts, the uneasy ones to solve are on patch
"mptcp: rework mptcp_sendmsg_frag to accept optional dfrag"

A tree rebased with the above patches applied is available at:

https://github.com/pabeni/mptcp/tree/sendmsg_fix_2

I hope you don't mind if I don't use your branch directly. I was going to apply Mat's data-FIN patch. And look at the code on your branch in case of conflicts.

Or I can re-create the tree now using your branch if you prefer. If some fixes are needed, they can be done in follow-up patches (if any). I only have to re-apply Davide's patch from this morning, that's easy to do.

WDYT?

Cheers,
Matt

Signature · 5 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium
author
#10 lore raw
Quoted 13 lines · Matthieu Baerts

Hi Paolo,

Quoted 1 line · Paolo Abeni

This is just a rebase of the initial RFC post.

Thank you for the rebase!

Quoted 6 lines

The patch 1 should be inserted just after "tcp: clean ext on tx recycle"

The patches 2 and 3 have explicit individual squash-to tags, but will casue a lot of conflicts, the uneasy ones to solve are on patch
"mptcp: rework mptcp_sendmsg_frag to accept optional dfrag"

Quoted 7 lines
Quoted 3 lines

A tree rebased with the above patches applied is available at:

https://github.com/pabeni/mptcp/tree/sendmsg_fix_2

I hope you don't mind if I don't use your branch directly. I was going to apply Mat's data-FIN patch. And look at the code on your branch in case of conflicts.

WFM :)

Whatever is easier for you.

It looks like we have some pending conflicting patches - I hope v1 support should land soon, too... - so really whatever works best for you will work for me, too.

Thanks,

Paolo

author
#5 lore raw

PATCH v2 2/3 mptcp: avoid data stream corruption on allocation failure.

In reply to Paolo Abeni

Currently, when if mpext allocation fails, the data stream is corrupted, as we can add the required DSS nor roll-back the TCP status.

Allocate a skb_ext before the actually skb_buff creation/TCP status update: in case of memory allocation failure we can bail early, avoiding the above condition.

Additionally, if the ext is unused to to skb collapsing, cache the ext in the msk for the next send.

Squash-to: "mptcp: Write MPTCP DSS headers to outgoing data packets"
Signed-off-by: Paolo Abeni <pabeni(a)redhat.com>
---
v1 -> v2:
- properly convert to bool mptcp_ext_cache_refill() return value

Note: a rebased tree including DATA_FIN, v1 and this patches is avaliable here:

https://github.com/pabeni/mptcp/tree/mptcp_net-next_FIN_v1_recvmsg

---
 net/mptcp/protocol.c | 39 +++++++++++++++++++++++++--------------
 net/mptcp/protocol.h |  1 +
 2 files changed, 26 insertions(+), 14 deletions(-)
net/mptcp/protocol.c +25 -14
index 3180202e56b4..baa5f84c7278 100644
@@ -55,6 +55,14 @@ static struct sock *mptcp_subflow_get(const struct mptcp_sock *msk)
 	return NULL;
 }
 
+static bool mptcp_ext_cache_refill(struct mptcp_sock *msk)
+{
+	if (!msk->cached_ext)
+		msk->cached_ext = skb_ext_alloc();
+
+	return !!msk->cached_ext;
+}
+
 static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 			      struct msghdr *msg, long *timeo)
 {
@@ -69,7 +77,8 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 	 * from one substream to another, but do per subflow memory accounting
 	 */
 	pfrag = sk_page_frag(sk);
-	while (!sk_page_frag_refill(ssk, pfrag)) {
+	while (!sk_page_frag_refill(ssk, pfrag) ||
+	       !mptcp_ext_cache_refill(msk)) {
 		ret = sk_stream_wait_memory(ssk, timeo);
 		if (ret)
 			return ret;
@@ -103,19 +112,19 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 		iov_iter_revert(&msg->msg_iter, psize - ret);
 
 	skb = tcp_write_queue_tail(ssk);
-	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
-	if (mpext) {
-		memset(mpext, 0, sizeof(*mpext));
-		mpext->data_seq = msk->write_seq;
-		mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
-		mpext->data_len = ret;
-		mpext->use_map = 1;
-		mpext->dsn64 = 1;
-
-		pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d",
-			 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
-			 mpext->dsn64);
-	} /* TODO: else fallback */
+	mpext = __skb_ext_add(skb, SKB_EXT_MPTCP, msk->cached_ext);
+	msk->cached_ext = NULL;
+
+	memset(mpext, 0, sizeof(*mpext));
+	mpext->data_seq = msk->write_seq;
+	mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
+	mpext->data_len = ret;
+	mpext->use_map = 1;
+	mpext->dsn64 = 1;
+
+	pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d",
+		 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
+		 mpext->dsn64);
 
 	pfrag->offset += ret;
 	msk->write_seq += ret;
@@ -242,6 +251,8 @@ static void mptcp_close(struct sock *sk, long timeout)
 		sock_release(mptcp_subflow_tcp_socket(subflow));
 	}
 
+	if (msk->cached_ext)
+		__skb_ext_put(msk->cached_ext);
 	release_sock(sk);
 	sk_common_release(sk);
 }
net/mptcp/protocol.h +1 -0
index 412718c1ae5c..3b8d1548dda1 100644
@@ -58,6 +58,7 @@ struct mptcp_sock {
 	u64		write_seq;
 	u64		ack_seq;
 	u32		token;
+	struct	skb_ext *cached_ext;	/* for the next sendmsg */
 	struct list_head conn_list;
 	struct socket	*subflow; /* outgoing connect/listener/!mp_capable */
 };
Signature · 1 line
2.21.0
#2 lore raw

[MPTCP] Re: [PATCH 1/3] skb: add helpers to allocate ext independently from sk_buff

In reply to Paolo Abeni
Quoted 38 lines · Paolo Abeni

Currently we can allocate the extension only after the skb, this change allow the user to do the opposite, will simplify allocation failure handling from MPTCP.

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>

---
include/linux/skbuff.h | 3 +++
net/core/skbuff.c | 16 +++++++++++++++-

2 files changed, 18 insertions(+), 1 deletion(-)
include/linux/skbuff.h +3 -0
index f641fc76f47e..1bbb77640045 100644
@@ -4111,6 +4111,9 @@ struct skb_ext {
 	char data[0] __aligned(8);
 };
 
+struct skb_ext *skb_ext_alloc(void);
+void *__skb_ext_add(struct sk_buff *skb, enum skb_ext_id id,
+		    struct skb_ext *ext);
 void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id);
 void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id);
 void __skb_ext_put(struct skb_ext *ext);
net/core/skbuff.c +1 -1
index 0aba2cd69881..e8ed6d28fba8 100644
@@ -5981,7 +5981,7 @@ static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
 	return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
 }
 
-static struct skb_ext *skb_ext_alloc(void)
+struct skb_ext *skb_ext_alloc(void)
 {
 	struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
 

net/core/skbuff.c static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old, · line 6023

Quoted 3 lines
net/core/skbuff.c +0 -0
@@ -6021,6 +6021,20 @@ static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
 	return new;
 }
 

A comment similar to the one before skb_ext_add would be helpful, especially given the extra rules for the "__" version of the function, and as you mentioned in the cover letter wanting to make sure the struct skb_ext is newly alloced.

net/core/skbuff.c static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old, · line 6034

Quoted 11 lines
net/core/skbuff.c +11 -0
+void *__skb_ext_add(struct sk_buff *skb, enum skb_ext_id id,
+		    struct skb_ext *ext)
+{
+	unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext);
+
+	skb_ext_put(skb);
+	newlen = newoff + skb_ext_type_len[id];
+	ext->chunks = newlen;
+	ext->offset[id] = newoff;
+	skb->extensions = ext;
+	skb->active_extensions |= 1 << id;

Since this isn't doing a copy-on-write, need to use "=" instead of "|=" so active_extensions doesn't have old bits set from the previous skb_ext struct (if there was one).

net/core/skbuff.c static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old, · line 6040

Quoted 6 lines
net/core/skbuff.c +3 -0
+	return skb_ext_get_ptr(ext, id);
+}
+
 /**
 * skb_ext_add - allocate space for given extension, COW if needed
 * @skb: buffer

--
Mat Martineau
Intel

#7 lore raw

[MPTCP] Re: [PATCH 3/3] imptcp: properly detect skb collapsing

In reply to Paolo Abeni
Quoted 30 lines · Paolo Abeni

Currently we can misdetect skb collapsing, which will lead to a kernel warning and crash.

Changing a bit the detection logic for skb collapsing we can address the issue.

Squash-to: "mptcp: allow collapsing consecutive sendpages on the same substream"
Signed-off-by: Paolo Abeni <pabeni(a)redhat.com>
---
net/mptcp/protocol.c | 12 +++++++++---

1 file changed, 9 insertions(+), 3 deletions(-)
net/mptcp/protocol.c +1 -1
index 1c489eccdb58..ba918c4fdcc8 100644
@@ -107,8 +107,8 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	bool collapsed, can_collapse = false;
 	struct mptcp_ext *mpext = NULL;
+	struct sk_buff *skb, *tail;
 	struct page_frag *pfrag;
-	struct sk_buff *skb;
 	size_t psize;
 
 	/* use the mptcp page cache so that we can easily move the data
@@ -165,10 +165,16 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 	if (unlikely(ret < psize))
 		iov_iter_revert(&msg->msg_iter, psize - ret);
 

net/mptcp/protocol.c static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, · line 176

Quoted 10 lines
net/mptcp/protocol.c +8 -1
-	collapsed = skb == tcp_write_queue_tail(ssk);
+	/* if the tail skb extension is still the cached one, collapsing
+	 * really happened. Note: we can't check for 'same skb' as the sk_buff
+	 * hdr on tail can be transmitted, freed and re-allocated by the
+	 * do_tcp_sendpages() call
+	 */
+	tail = tcp_write_queue_tail(ssk);
+	collapsed = mpext &&
+		    mpext == (tail ? skb_ext_find(tail, SKB_EXT_MPTCP) : NULL);
 	if (collapsed) {

I would find this a little easier to read:

if (mpext && tail && mpext == skb_ext_find(tail, SKB_EXT_MPTCP))

This also lets you remove the 'collapsed' variable.

net/mptcp/protocol.c static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, · line 180

Quoted 5 lines
net/mptcp/protocol.c +0 -1
 		WARN_ON_ONCE(!can_collapse);
-		/* when collapsing mpext always exists */
 		mpext->data_len += ret;
 		goto out;
 	}

--
Mat Martineau
Intel

author
#3 lore raw

[MPTCP] Re: [PATCH 1/3] skb: add helpers to allocate ext independently from sk_buff

In reply to Mat Martineau
Quoted 49 lines · Mat Martineau
Quoted 38 lines · Paolo Abeni

Currently we can allocate the extension only after the skb, this change allow the user to do the opposite, will simplify allocation failure handling from MPTCP.

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>

---
include/linux/skbuff.h | 3 +++
net/core/skbuff.c | 16 +++++++++++++++-

2 files changed, 18 insertions(+), 1 deletion(-)
include/linux/skbuff.h +3 -0
index f641fc76f47e..1bbb77640045 100644
@@ -4111,6 +4111,9 @@ struct skb_ext {
 	char data[0] __aligned(8);
 };
 
+struct skb_ext *skb_ext_alloc(void);
+void *__skb_ext_add(struct sk_buff *skb, enum skb_ext_id id,
+		    struct skb_ext *ext);
 void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id);
 void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id);
 void __skb_ext_put(struct skb_ext *ext);
net/core/skbuff.c +1 -1
index 0aba2cd69881..e8ed6d28fba8 100644
@@ -5981,7 +5981,7 @@ static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
 	return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
 }
 
-static struct skb_ext *skb_ext_alloc(void)
+struct skb_ext *skb_ext_alloc(void)
 {
 	struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
 

net/core/skbuff.c static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old, · line 6023

Quoted 3 lines
net/core/skbuff.c +0 -0
@@ -6021,6 +6021,20 @@ static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
 	return new;
 }
 

A comment similar to the one before skb_ext_add would be helpful, especially given the extra rules for the "__" version of the function, and as you mentioned in the cover letter wanting to make sure the struct skb_ext is newly alloced.

Quoted 15 lines

net/core/skbuff.c static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old, · line 6034

Quoted 11 lines
net/core/skbuff.c +11 -0
+void *__skb_ext_add(struct sk_buff *skb, enum skb_ext_id id,
+		    struct skb_ext *ext)
+{
+	unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext);
+
+	skb_ext_put(skb);
+	newlen = newoff + skb_ext_type_len[id];
+	ext->chunks = newlen;
+	ext->offset[id] = newoff;
+	skb->extensions = ext;
+	skb->active_extensions |= 1 << id;

Since this isn't doing a copy-on-write, need to use "=" instead of "|=" so active_extensions doesn't have old bits set from the previous skb_ext struct (if there was one).

Right. I'll also rename the helper as __skb_ext_set() to hopefully be more clear

Thanks,

Paolo

author
#8 lore raw

[MPTCP] Re: [PATCH 3/3] imptcp: properly detect skb collapsing

In reply to Mat Martineau
Quoted 45 lines · Mat Martineau
Quoted 30 lines · Paolo Abeni

Currently we can misdetect skb collapsing, which will lead to a kernel warning and crash.

Changing a bit the detection logic for skb collapsing we can address the issue.

Squash-to: "mptcp: allow collapsing consecutive sendpages on the same substream"
Signed-off-by: Paolo Abeni <pabeni(a)redhat.com>
---
net/mptcp/protocol.c | 12 +++++++++---

1 file changed, 9 insertions(+), 3 deletions(-)
net/mptcp/protocol.c +1 -1
index 1c489eccdb58..ba918c4fdcc8 100644
@@ -107,8 +107,8 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	bool collapsed, can_collapse = false;
 	struct mptcp_ext *mpext = NULL;
+	struct sk_buff *skb, *tail;
 	struct page_frag *pfrag;
-	struct sk_buff *skb;
 	size_t psize;
 
 	/* use the mptcp page cache so that we can easily move the data
@@ -165,10 +165,16 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 	if (unlikely(ret < psize))
 		iov_iter_revert(&msg->msg_iter, psize - ret);
 

net/mptcp/protocol.c static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, · line 176

Quoted 10 lines
net/mptcp/protocol.c +8 -1
-	collapsed = skb == tcp_write_queue_tail(ssk);
+	/* if the tail skb extension is still the cached one, collapsing
+	 * really happened. Note: we can't check for 'same skb' as the sk_buff
+	 * hdr on tail can be transmitted, freed and re-allocated by the
+	 * do_tcp_sendpages() call
+	 */
+	tail = tcp_write_queue_tail(ssk);
+	collapsed = mpext &&
+		    mpext == (tail ? skb_ext_find(tail, SKB_EXT_MPTCP) : NULL);
 	if (collapsed) {

I would find this a little easier to read:

Quoted 1 line

if (mpext && tail && mpext == skb_ext_find(tail, SKB_EXT_MPTCP))

Agreed, will send v2 with the above.

Quoted 1 line

This also lets you remove the 'collapsed' variable.

Yup! The flip side is an extra bunch of conflicts :( Poor Matt ;)))

Thanks,

Paolo