[PATCH net-next v1 4/6] net: avoid stack canaries in skb_try_coalesce() callers

From
Xuanqiang Luo <hidden>
Date
2026-11-24 13:08:16
Also in
bpf, lkml, mptcp, netfilter-devel
Reply to
Xuanqiang Luo, 2026-11-24
From: Xuanqiang Luo <redacted>

skb_try_coalesce() returns the stolen head indication and the truesize
delta through pointers to locals of its callers.

With CONFIG_STACKPROTECTOR_STRONG=y, this forces a stack canary in
all of them, some are in TCP receive fast paths.

Rename it to __skb_try_coalesce(), and return its results by value in
a small structure.

skb_try_coalesce() is now an inline wrapper, callers do not change.

A successful merge never has a negative delta, so that a negative value
can be used to report a failure.

$ scripts/bloat-o-meter -t vmlinux.old vmlinux.new
add/remove: 2/2 grow/shrink: 1/8 up/down: 1303/-1677 (-374)
Function                                     old     new   delta
__skb_try_coalesce                             -    1255   +1255
tcp_data_queue                              4393    4425     +32
__pfx___skb_try_coalesce                       -      16     +16
mptcp_recvmsg                               2108    2092     -16
alloc_skb_with_frags                         534     518     -16
__pfx_skb_try_coalesce                        16       -     -16
tcp_try_coalesce                             386     359     -27
skb_copy_ubufs                              1645    1613     -32
__mptcp_move_skbs_from_subflow              1215    1167     -48
tcp_add_backlog                             1576    1527     -49
mptcp_try_coalesce                           235     168     -67
inet_frag_reasm_finish                       672     598     -74
skb_try_coalesce                            1332       -   -1332
Total: Before=24067978, After=24067604, chg -0.00%

Signed-off-by: Xuanqiang Luo <redacted>
---
 include/linux/skbuff.h | 35 +++++++++++++++++++++++++++--
 net/core/skbuff.c      | 50 +++++++++++++++++++++++-------------------
 2 files changed, 60 insertions(+), 25 deletions(-)
diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
index 55ab6392301a..bbdf64811aed 100644
--- a/include/linux/skbuff.h
+++ b/include/linux/skbuff.h
@@ -1370,8 +1370,39 @@ void __consume_stateless_skb(struct sk_buff *skb);
 void  __kfree_skb(struct sk_buff *skb);
 
 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen);
-bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
-		      bool *fragstolen, int *delta_truesize);
+
+/* Result of __skb_try_coalesce(). */
+struct skb_coalesce_res {
+	int	delta_truesize;	/* negative if not coalesced */
+	bool	fragstolen;
+};
+
+struct skb_coalesce_res __skb_try_coalesce(struct sk_buff *to,
+					   struct sk_buff *from);
+
+/**
+ * skb_try_coalesce - try to merge skb to prior one
+ * @to: prior buffer
+ * @from: buffer to add
+ * @fragstolen: pointer to boolean
+ * @delta_truesize: how much more was allocated than was requested
+ *
+ * Return: true if @from was merged into @to, false otherwise.
+ */
+static __always_inline bool skb_try_coalesce(struct sk_buff *to,
+					     struct sk_buff *from,
+					     bool *fragstolen,
+					     int *delta_truesize)
+{
+	struct skb_coalesce_res res = __skb_try_coalesce(to, from);
+
+	*fragstolen = res.fragstolen;
+	if (res.delta_truesize < 0)
+		return false;
+
+	*delta_truesize = res.delta_truesize;
+	return true;
+}
 
 struct sk_buff *__alloc_skb(unsigned int size, gfp_t priority, int flags,
 			    int node);
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index e5b0209aec1c..1da20a7a0bb4 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -6602,23 +6602,26 @@ void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
 EXPORT_SYMBOL(kfree_skb_partial);
 
 /**
- * skb_try_coalesce - try to merge skb to prior one
+ * __skb_try_coalesce - try to merge skb to prior one
  * @to: prior buffer
  * @from: buffer to add
- * @fragstolen: pointer to boolean
- * @delta_truesize: how much more was allocated than was requested
+ *
+ * Helper for skb_try_coalesce(), returning its results by value so that
+ * callers do not need a stack canary.
+ *
+ * Return: @delta_truesize is negative if @from was not merged.
  */
-bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
-		      bool *fragstolen, int *delta_truesize)
+struct skb_coalesce_res __skb_try_coalesce(struct sk_buff *to,
+					   struct sk_buff *from)
 {
+	const struct skb_coalesce_res fail = { .delta_truesize = -1 };
 	struct skb_shared_info *to_shinfo, *from_shinfo;
 	unsigned int len = from->len;
+	bool fragstolen = false;
 	int i, delta;
 
-	*fragstolen = false;
-
 	if (skb_cloned(to))
-		return false;
+		return fail;
 
 	/* In general, avoid mixing page_pool and non-page_pool allocated
 	 * pages within the same SKB. In theory we could take full
@@ -6627,14 +6630,14 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
 	 * rare, so not worth dealing with.
 	 */
 	if (to->pp_recycle != from->pp_recycle)
-		return false;
+		return fail;
 
 	/* All frags in an skb must either all be struct page-backed or all
 	 * belong to the same memory provider instance; do not coalesce skbs
 	 * that would mix them.
 	 */
 	if (skb_frags_readable(from) != skb_frags_readable(to))
-		return false;
+		return fail;
 
 	if (len <= skb_tailroom(to) && skb_frags_readable(from)) {
 		if (len) {
@@ -6642,41 +6645,40 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
 						   skb_tail_pointer(to),
 						   len))) {
 				DEBUG_NET_WARN_ON_ONCE(1);
-				return false;
+				return fail;
 			}
 			skb_put(to, len);
 		}
-		*delta_truesize = 0;
-		return true;
+		return (struct skb_coalesce_res){ .delta_truesize = 0 };
 	}
 
 	to_shinfo = skb_shinfo(to);
 	from_shinfo = skb_shinfo(from);
 	if (to_shinfo->frag_list || from_shinfo->frag_list)
-		return false;
+		return fail;
 	if (skb_zcopy(to) || skb_zcopy(from))
-		return false;
+		return fail;
 
 	if (skb_headlen(from) != 0) {
 		if (to_shinfo->nr_frags +
 		    from_shinfo->nr_frags >= MAX_SKB_FRAGS)
-			return false;
+			return fail;
 
 		if (skb_head_is_locked(from))
-			return false;
+			return fail;
 
 		delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
 	} else {
 		if (to_shinfo->nr_frags +
 		    from_shinfo->nr_frags > MAX_SKB_FRAGS)
-			return false;
+			return fail;
 
 		delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
 	}
 
 	/* @from claims more data than it has memory for.  Leave @to alone. */
 	if (WARN_ON_ONCE(delta < 0 || delta < len))
-		return false;
+		return fail;
 
 	if (skb_headlen(from) != 0) {
 		struct page *page = virt_to_head_page(from->head);
@@ -6685,7 +6687,7 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
 		offset = from->data - (unsigned char *)page_address(page);
 		skb_fill_page_desc(to, to_shinfo->nr_frags,
 				   page, offset, skb_headlen(from));
-		*fragstolen = true;
+		fragstolen = true;
 	}
 
 	memcpy(to_shinfo->frags + to_shinfo->nr_frags,
@@ -6708,10 +6710,12 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
 	to->len += len;
 	to->data_len += len;
 
-	*delta_truesize = delta;
-	return true;
+	return (struct skb_coalesce_res){
+		.delta_truesize = delta,
+		.fragstolen = fragstolen,
+	};
 }
-EXPORT_SYMBOL(skb_try_coalesce);
+EXPORT_SYMBOL(__skb_try_coalesce);
 
 static void skb_ext_scrub(struct sk_buff *skb)
 {
-- 
2.43.0
Thread (7 messages) 7 messages, 1 author, last reply 15h ago
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help