netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] netlink: avoid extra pskb_expand_head() in netlink_trim()
@ 2025-05-28 15:36 Dmitry Antipov
  2025-05-29  0:43 ` Jakub Kicinski
  0 siblings, 1 reply; 3+ messages in thread
From: Dmitry Antipov @ 2025-05-28 15:36 UTC (permalink / raw)
  To: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman
  Cc: netdev, Dmitry Antipov

When 'netlink_trim()' processes shared skb, using 'skb_clone()' with
following 'pskb_expand_head()' looks suboptimal, and it's expected to
be a bit faster to do 'skb_copy_expand()' with desired tailroom instead.

Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
 net/netlink/af_netlink.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
index e8972a857e51..efb360433339 100644
--- a/net/netlink/af_netlink.c
+++ b/net/netlink/af_netlink.c
@@ -1285,11 +1285,15 @@ static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
 		return skb;
 
 	if (skb_shared(skb)) {
-		struct sk_buff *nskb = skb_clone(skb, allocation);
+		struct sk_buff *nskb;
+
+		nskb = skb_copy_expand(skb, skb_headroom(skb),
+				       skb_tailroom(skb) - delta,
+				       allocation);
 		if (!nskb)
 			return skb;
 		consume_skb(skb);
-		skb = nskb;
+		return nskb;
 	}
 
 	pskb_expand_head(skb, 0, -delta,
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread
* [PATCH] net: core: avoid extra pskb_expand_head() when adjusting headroom
@ 2025-06-06  7:41 Dmitry Antipov
  2025-06-06  7:41 ` [PATCH] netlink: avoid extra pskb_expand_head() in netlink_trim() Dmitry Antipov
  0 siblings, 1 reply; 3+ messages in thread
From: Dmitry Antipov @ 2025-06-06  7:41 UTC (permalink / raw)
  To: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman
  Cc: netdev, Dmitry Antipov

In 'skb_realloc_headroom()' and 'skb_expand_head()', using 'skb_clone()'
with following 'pskb_expand_head()' looks suboptimal, and it's expected to
be a bit faster to do 'skb_copy_expand()' with desired headroom instead.

Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
 net/core/skbuff.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)

diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index 85fc82f72d26..3ed540cba5a3 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -2316,14 +2316,10 @@ struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
 
 	if (delta <= 0)
 		skb2 = pskb_copy(skb, GFP_ATOMIC);
-	else {
-		skb2 = skb_clone(skb, GFP_ATOMIC);
-		if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
-					     GFP_ATOMIC)) {
-			kfree_skb(skb2);
-			skb2 = NULL;
-		}
-	}
+	else
+		skb2 = skb_copy_expand(skb, (skb_headroom(skb) +
+					     SKB_DATA_ALIGN(delta)),
+				       0, GFP_ATOMIC);
 	return skb2;
 }
 EXPORT_SYMBOL(skb_realloc_headroom);
@@ -2400,8 +2396,10 @@ struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom)
 	delta = SKB_DATA_ALIGN(delta);
 	/* pskb_expand_head() might crash, if skb is shared. */
 	if (skb_shared(skb) || !is_skb_wmem(skb)) {
-		struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
+		struct sk_buff *nskb;
 
+		nskb = skb_copy_expand(skb, skb_headroom(skb) + delta,
+				       skb_tailroom(skb), GFP_ATOMIC);
 		if (unlikely(!nskb))
 			goto fail;
 
@@ -2409,8 +2407,7 @@ struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom)
 			skb_set_owner_w(nskb, sk);
 		consume_skb(skb);
 		skb = nskb;
-	}
-	if (pskb_expand_head(skb, delta, 0, GFP_ATOMIC))
+	} else if (pskb_expand_head(skb, delta, 0, GFP_ATOMIC))
 		goto fail;
 
 	if (sk && is_skb_wmem(skb)) {
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-06-06  7:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-28 15:36 [PATCH] netlink: avoid extra pskb_expand_head() in netlink_trim() Dmitry Antipov
2025-05-29  0:43 ` Jakub Kicinski
  -- strict thread matches above, loose matches on Subject: below --
2025-06-06  7:41 [PATCH] net: core: avoid extra pskb_expand_head() when adjusting headroom Dmitry Antipov
2025-06-06  7:41 ` [PATCH] netlink: avoid extra pskb_expand_head() in netlink_trim() Dmitry Antipov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).