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