Skip to contentSkip to conversation

skb: avoid unnecessary reallocations in __skb_cow

RFC In review Felix Fietkau 2 participants 6 messages May 27, 2012 – May 29, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC 1/1 skb: avoid unnecessary reallocations in __skb_cow

At the beginning of __skb_cow, headroom gets set to a minimum of NET_SKB_PAD. This causes unnecessary reallocations if the buffer was not cloned and the headroom is just below NET_SKB_PAD, but still more than the amount requested by the caller.
This was showing up frequently in my tests on VLAN tx, where vlan_insert_tag calls skb_cow_head(skb, VLAN_HLEN).

Fix this by only setting the headroom delta if either there is less headroom than specified by the caller, or if reallocation has to be done anyway because the skb was cloned.

---
 include/linux/skbuff.h |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)
include/linux/skbuff.h +6 -3
index 0e50171..1898471 100644
@@ -1894,12 +1894,15 @@ static inline int skb_clone_writable(const struct sk_buff *skb, unsigned int len
 static inline int __skb_cow(struct sk_buff *skb, unsigned int headroom,
 			    int cloned)
 {
+	unsigned int alloc_headroom = headroom;
 	int delta = 0;
 
 	if (headroom < NET_SKB_PAD)
-		headroom = NET_SKB_PAD;
-	if (headroom > skb_headroom(skb))
-		delta = headroom - skb_headroom(skb);
+		alloc_headroom = NET_SKB_PAD;
+	if (headroom > skb_headroom(skb) ||
+	    (cloned && alloc_headroom > skb_headroom(skb))) {
+		delta = alloc_headroom - skb_headroom(skb);
+	}
 
 	if (delta || cloned)
 		return pskb_expand_head(skb, ALIGN(delta, NET_SKB_PAD), 0,
Signature · 1 line
1.7.3.2
#1 lore raw
Quoted 29 lines · Felix Fietkau

At the beginning of __skb_cow, headroom gets set to a minimum of NET_SKB_PAD. This causes unnecessary reallocations if the buffer was not cloned and the headroom is just below NET_SKB_PAD, but still more than the amount requested by the caller.
This was showing up frequently in my tests on VLAN tx, where vlan_insert_tag calls skb_cow_head(skb, VLAN_HLEN).

Fix this by only setting the headroom delta if either there is less headroom than specified by the caller, or if reallocation has to be done anyway because the skb was cloned.

---
 include/linux/skbuff.h |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)
include/linux/skbuff.h +1 -1
index 0e50171..1898471 100644
@@ -1894,12 +1894,15 @@ static inline int skb_clone_writable(const struct sk_buff *skb, unsigned int len
 static inline int __skb_cow(struct sk_buff *skb, unsigned int headroom,
 			    int cloned)
 {
+	unsigned int alloc_headroom = headroom;
 	int delta = 0;
 
 	if (headroom < NET_SKB_PAD)
-		headroom = NET_SKB_PAD;

include/linux/skbuff.h static inline int skb_clone_writable(const struct sk_buff *skb, unsigned int len · line 1908

Quoted 10 lines
include/linux/skbuff.h +5 -2
-	if (headroom > skb_headroom(skb))
-		delta = headroom - skb_headroom(skb);
+		alloc_headroom = NET_SKB_PAD;
+	if (headroom > skb_headroom(skb) ||
+	    (cloned && alloc_headroom > skb_headroom(skb))) {
+		delta = alloc_headroom - skb_headroom(skb);
+	}
 
 	if (delta || cloned)
 		return pskb_expand_head(skb, ALIGN(delta, NET_SKB_PAD), 0,

Nice catch.

Scratching my head on this one. Why not the obvious fix ?

include/linux/skbuff.h +0 -2
index 0e50171..b534a1b 100644
@@ -1896,8 +1896,6 @@ static inline int __skb_cow(struct sk_buff *skb, unsigned int headroom,
 {
 	int delta = 0;
 
-	if (headroom < NET_SKB_PAD)
-		headroom = NET_SKB_PAD;
 	if (headroom > skb_headroom(skb))
 		delta = headroom - skb_headroom(skb);
 
#2 lore raw
Quoted 38 lines · Eric Dumazet
Quoted 37 lines · Felix Fietkau

At the beginning of __skb_cow, headroom gets set to a minimum of NET_SKB_PAD. This causes unnecessary reallocations if the buffer was not cloned and the headroom is just below NET_SKB_PAD, but still more than the amount requested by the caller.
This was showing up frequently in my tests on VLAN tx, where vlan_insert_tag calls skb_cow_head(skb, VLAN_HLEN).

Fix this by only setting the headroom delta if either there is less headroom than specified by the caller, or if reallocation has to be done anyway because the skb was cloned.

---
 include/linux/skbuff.h |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)
include/linux/skbuff.h +6 -3
index 0e50171..1898471 100644
@@ -1894,12 +1894,15 @@ static inline int skb_clone_writable(const struct sk_buff *skb, unsigned int len
 static inline int __skb_cow(struct sk_buff *skb, unsigned int headroom,
 			    int cloned)
 {
+	unsigned int alloc_headroom = headroom;
 	int delta = 0;
 
 	if (headroom < NET_SKB_PAD)
-		headroom = NET_SKB_PAD;
-	if (headroom > skb_headroom(skb))
-		delta = headroom - skb_headroom(skb);
+		alloc_headroom = NET_SKB_PAD;
+	if (headroom > skb_headroom(skb) ||
+	    (cloned && alloc_headroom > skb_headroom(skb))) {
+		delta = alloc_headroom - skb_headroom(skb);
+	}
 
Quoted 6 lines

include/linux/skbuff.h static inline int skb_clone_writable(const struct sk_buff *skb, unsigned int len · line 1908

Quoted 2 lines
include/linux/skbuff.h +0 -0
 	if (delta || cloned)
 		return pskb_expand_head(skb, ALIGN(delta, NET_SKB_PAD), 0,

Nice catch.

Scratching my head on this one. Why not the obvious fix ?

If we're reallocating anyway, we might as well put in more headroom than requested, in case something else needs even more than that.

- Felix

#3 lore raw
Quoted 39 lines · Felix Fietkau
Quoted 38 lines · Eric Dumazet
Quoted 37 lines · Felix Fietkau

At the beginning of __skb_cow, headroom gets set to a minimum of NET_SKB_PAD. This causes unnecessary reallocations if the buffer was not cloned and the headroom is just below NET_SKB_PAD, but still more than the amount requested by the caller.
This was showing up frequently in my tests on VLAN tx, where vlan_insert_tag calls skb_cow_head(skb, VLAN_HLEN).

Fix this by only setting the headroom delta if either there is less headroom than specified by the caller, or if reallocation has to be done anyway because the skb was cloned.

---
 include/linux/skbuff.h |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)
include/linux/skbuff.h +6 -3
index 0e50171..1898471 100644
@@ -1894,12 +1894,15 @@ static inline int skb_clone_writable(const struct sk_buff *skb, unsigned int len
 static inline int __skb_cow(struct sk_buff *skb, unsigned int headroom,
 			    int cloned)
 {
+	unsigned int alloc_headroom = headroom;
 	int delta = 0;
 
 	if (headroom < NET_SKB_PAD)
-		headroom = NET_SKB_PAD;
-	if (headroom > skb_headroom(skb))
-		delta = headroom - skb_headroom(skb);
+		alloc_headroom = NET_SKB_PAD;
+	if (headroom > skb_headroom(skb) ||
+	    (cloned && alloc_headroom > skb_headroom(skb))) {
+		delta = alloc_headroom - skb_headroom(skb);
+	}
 
Quoted 8 lines
Quoted 6 lines

include/linux/skbuff.h static inline int skb_clone_writable(const struct sk_buff *skb, unsigned int len · line 1908

Quoted 2 lines
include/linux/skbuff.h +0 -0
 	if (delta || cloned)
 		return pskb_expand_head(skb, ALIGN(delta, NET_SKB_PAD), 0,

Nice catch.

Scratching my head on this one. Why not the obvious fix ?

If we're reallocating anyway, we might as well put in more headroom than requested, in case something else needs even more than that.

Locally generated packets should have enough headroom, and for forward paths, we already have NET_SKB_PAD bytes of headroom.

Adding yet another NET_SKB_PAD extra space is overkill, unless you have a real use case in mind ?

#4 lore raw
Quoted 50 lines · Eric Dumazet
Quoted 39 lines · Felix Fietkau
Quoted 38 lines · Eric Dumazet
Quoted 37 lines · Felix Fietkau

At the beginning of __skb_cow, headroom gets set to a minimum of NET_SKB_PAD. This causes unnecessary reallocations if the buffer was not cloned and the headroom is just below NET_SKB_PAD, but still more than the amount requested by the caller.
This was showing up frequently in my tests on VLAN tx, where vlan_insert_tag calls skb_cow_head(skb, VLAN_HLEN).

Fix this by only setting the headroom delta if either there is less headroom than specified by the caller, or if reallocation has to be done anyway because the skb was cloned.

---
 include/linux/skbuff.h |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)
include/linux/skbuff.h +6 -3
index 0e50171..1898471 100644
@@ -1894,12 +1894,15 @@ static inline int skb_clone_writable(const struct sk_buff *skb, unsigned int len
 static inline int __skb_cow(struct sk_buff *skb, unsigned int headroom,
 			    int cloned)
 {
+	unsigned int alloc_headroom = headroom;
 	int delta = 0;
 
 	if (headroom < NET_SKB_PAD)
-		headroom = NET_SKB_PAD;
-	if (headroom > skb_headroom(skb))
-		delta = headroom - skb_headroom(skb);
+		alloc_headroom = NET_SKB_PAD;
+	if (headroom > skb_headroom(skb) ||
+	    (cloned && alloc_headroom > skb_headroom(skb))) {
+		delta = alloc_headroom - skb_headroom(skb);
+	}
 
Quoted 8 lines
Quoted 6 lines

include/linux/skbuff.h static inline int skb_clone_writable(const struct sk_buff *skb, unsigned int len · line 1908

Quoted 2 lines
include/linux/skbuff.h +0 -0
 	if (delta || cloned)
 		return pskb_expand_head(skb, ALIGN(delta, NET_SKB_PAD), 0,

Nice catch.

Scratching my head on this one. Why not the obvious fix ?

If we're reallocating anyway, we might as well put in more headroom than requested, in case something else needs even more than that.

Quoted 5 lines

Locally generated packets should have enough headroom, and for forward paths, we already have NET_SKB_PAD bytes of headroom.

Adding yet another NET_SKB_PAD extra space is overkill, unless you have a real use case in mind ?

I don't have any real use case in mind, but it's not really adding an extra NET_SKB_PAD, it simply fills up the headroom to NET_SKB_PAD, but I guess that's probably unnecessary as well.
I'll resend the patch without the extra padding later.

- Felix

#5 lore raw
Quoted 2 lines · Felix Fietkau

I don't have any real use case in mind, but it's not really adding an extra NET_SKB_PAD, it simply fills up the headroom to NET_SKB_PAD,

This is not what is doing your patch.

If cloned is true, and current skb headroom less than 64, you add an extra 64 bytes of headroom.

Just keep it simple, this is inline code and should be kept as small as possible.