From: Xuanqiang Luo <xuanqiang.luo@linux.dev>
To: netdev@vger.kernel.org
Cc: "David S . Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Simon Horman <horms@kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
John Fastabend <john.fastabend@gmail.com>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
Emil Tsalapatis <emil@etsalapatis.com>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
Matthieu Baerts <matttbe@kernel.org>,
Mat Martineau <martineau@kernel.org>,
Geliang Tang <geliang@kernel.org>,
David Ahern <dsahern@kernel.org>,
Ido Schimmel <idosch@nvidia.com>,
Stanislav Fomichev <sdf@fomichev.me>,
Neal Cardwell <ncardwell@google.com>,
Kuniyuki Iwashima <kuniyu@google.com>,
Pablo Neira Ayuso <pablo@netfilter.org>,
Florian Westphal <fw@strlen.de>, Phil Sutter <phil@nwl.cc>,
bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
mptcp@lists.linux.dev, netfilter-devel@vger.kernel.org,
coreteam@netfilter.org, Xuanqiang Luo <luoxuanqiang@kylinos.cn>
Subject: [PATCH net-next v1 4/6] net: avoid stack canaries in skb_try_coalesce() callers
Date: Tue, 24 Nov 2026 22:06:29 +0900 [thread overview]
Message-ID: <20261124130631.1886963-5-xuanqiang.luo@linux.dev> (raw)
In-Reply-To: <20261124130631.1886963-1-xuanqiang.luo@linux.dev>
From: Xuanqiang Luo <luoxuanqiang@kylinos.cn>
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 <luoxuanqiang@kylinos.cn>
---
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
next prev parent reply other threads:[~2026-11-24 13:08 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 13:06 [PATCH net-next v1 0/6] net: avoid stack canaries in TCP/IPv4 fast paths Xuanqiang Luo
2026-11-24 13:06 ` [PATCH net-next v1 1/6] tcp: avoid a stack canary in __tcp_select_window() Xuanqiang Luo
2026-11-24 13:06 ` [PATCH net-next v1 2/6] ipv4: avoid a stack canary in ip_finish_output2() Xuanqiang Luo
2026-11-25 13:09 ` sashiko-bot
2026-11-24 13:06 ` [PATCH net-next v1 3/6] ipv4: raw: do not use skb_header_pointer() in icmp_filter() Xuanqiang Luo
2026-11-24 13:06 ` Xuanqiang Luo [this message]
2026-11-24 13:06 ` [PATCH net-next v1 5/6] tcp: avoid a stack canary in tcp_rcv_established() Xuanqiang Luo
2026-11-25 13:09 ` sashiko-bot
2026-11-24 13:06 ` [PATCH net-next v1 6/6] tcp: do not call bpf_skops_write_hdr_opt() unless needed Xuanqiang Luo
2026-11-26 8:48 ` [PATCH net-next v1 0/6] net: avoid stack canaries in TCP/IPv4 fast paths gang.yan
2026-11-27 6:18 ` Xuanqiang Luo
2026-11-26 10:20 ` David Laight
2026-11-27 6:33 ` Xuanqiang Luo
2026-11-27 10:41 ` David Laight
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124130631.1886963-5-xuanqiang.luo@linux.dev \
--to=xuanqiang.luo@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=coreteam@netfilter.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=dsahern@kernel.org \
--cc=eddyz87@gmail.com \
--cc=edumazet@kernel.org \
--cc=emil@etsalapatis.com \
--cc=fw@strlen.de \
--cc=geliang@kernel.org \
--cc=horms@kernel.org \
--cc=idosch@nvidia.com \
--cc=ihor.solodrai@linux.dev \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=luoxuanqiang@kylinos.cn \
--cc=martin.lau@linux.dev \
--cc=martineau@kernel.org \
--cc=matttbe@kernel.org \
--cc=memxor@gmail.com \
--cc=mptcp@lists.linux.dev \
--cc=ncardwell@google.com \
--cc=netdev@vger.kernel.org \
--cc=netfilter-devel@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=pablo@netfilter.org \
--cc=phil@nwl.cc \
--cc=sdf@fomichev.me \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.