Skip to contentSkip to conversation

net: skbuff: let struct skb_ext live inside the head

RFC In review Jakub Kicinski 4 participants 8 messages Feb 15, 2023 – Feb 16, 2023

Subsystems (2)
lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

RFC 1/1 net: skbuff: let struct skb_ext live inside the head

This is a bit more crazy than the previous patch. For drivers which already use build_skb() it's relatively easy to add more space to the shinfo. Use this approach to place skb_ext inside the head. No allocation needed.

This approach is a bit slower in trivial benchmarks than the recycling because it requires extra cache line accesses (12.1% loss, ->18.6Gbps).

In-place skb_ext may be shorter than a full skb_ext object. The driver only reserves space for exts it may use. Any later addition will reallocate the space via CoW, abandoning the in-place skb_ext and copying the data to a full slab object.

---
 include/linux/skbuff.h |  29 +++++++-
 net/core/gro.c         |   1 +
 net/core/skbuff.c      | 156 +++++++++++++++++++++++++++++++++++++++--
 3 files changed, 178 insertions(+), 8 deletions(-)
include/linux/skbuff.h +28 -1
index e68cb0a777b9..86486b667c94 100644
@@ -1257,6 +1257,8 @@ struct sk_buff *build_skb_around(struct sk_buff *skb,
 				 void *data, unsigned int frag_size);
 void skb_attempt_defer_free(struct sk_buff *skb);
 
+struct sk_buff *napi_build_skb_ext(void *data, unsigned int frag_size,
+				   unsigned int ext_size);
 struct sk_buff *napi_build_skb(void *data, unsigned int frag_size);
 struct sk_buff *slab_build_skb(void *data);
 
@@ -4601,6 +4603,16 @@ enum skb_ext_id {
 	SKB_EXT_NUM, /* must be last */
 };
 
+enum skb_ext_ref {
+	/* Normal skb ext from the kmem_cache */
+	SKB_EXT_ALLOC_SLAB,
+	/* Shard is a read-only skb ext placed after shinfo in the head,
+	 * shards may be shorter than full skb_ext length!
+	 */
+	SKB_EXT_ALLOC_SHARD_NOREF,
+	SKB_EXT_ALLOC_SHARD_REF,
+};
+
 /**
  *	struct skb_ext - sk_buff extensions
  *	@refcnt: 1 on allocation, deallocated on 0
@@ -4613,6 +4625,7 @@ enum skb_ext_id {
  */
 struct skb_ext {
 	refcount_t refcnt;
+	u8 alloc_type;
 	u8 offset[SKB_EXT_NUM]; /* in chunks of 8 bytes */
 	u8 chunks;		/* same */
 	char data[] __aligned(8);
@@ -4623,8 +4636,10 @@ void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id,
 		    struct skb_ext *ext);
 void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id);
 void *napi_skb_ext_add(struct sk_buff *skb, enum skb_ext_id id);
+void *skb_ext_add_inplace(struct sk_buff *skb, enum skb_ext_id id);
 void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id);
 void __skb_ext_put(struct skb_ext *ext);
+void __skb_ext_copy_get(struct skb_ext *ext);
 
 static inline void skb_ext_put(struct sk_buff *skb)
 {
@@ -4640,7 +4655,7 @@ static inline void __skb_ext_copy(struct sk_buff *dst,
 	if (src->active_extensions) {
 		struct skb_ext *ext = src->extensions;
 
-		refcount_inc(&ext->refcnt);
+		__skb_ext_copy_get(ext);
 		dst->extensions = ext;
 	}
 }
@@ -4699,6 +4714,18 @@ static inline void skb_ext_copy(struct sk_buff *dst, const struct sk_buff *s) {}
 static inline bool skb_has_extensions(struct sk_buff *skb) { return false; }
 #endif /* CONFIG_SKB_EXTENSIONS */
 
+/* Head got merged into another skb, skb itself will be freed later by
+ * kfree_skb_partial() or napi_skb_free_stolen_head().
+ */
+static inline void skb_head_stolen(struct sk_buff *skb)
+{
+#ifdef CONFIG_SKB_EXTENSIONS
+	if (unlikely(skb->active_extensions) &&
+	    skb->extensions->alloc_type == SKB_EXT_ALLOC_SHARD_NOREF)
+		skb->active_extensions = 0;
+#endif
+}
+
 static inline void nf_reset_ct(struct sk_buff *skb)
 {
 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
net/core/gro.c +1 -0
index a606705a0859..f1e091fa0d4c 100644
@@ -250,6 +250,7 @@ int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
 		delta_truesize = skb->truesize - new_truesize;
 		skb->truesize = new_truesize;
 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
+		skb_head_stolen(skb);
 		goto done;
 	}
 
net/core/skbuff.c +149 -7

252 lines not on this page · raw message

Signature · 1 line
2.39.1
maintainer
#1 lore raw
Quoted 5 lines · Jakub Kicinski

This is a bit more crazy than the previous patch. For drivers which already use build_skb() it's relatively easy to add more space to the shinfo. Use this approach to place skb_ext inside the head. No allocation needed.

Quoted 8 lines

This approach is a bit slower in trivial benchmarks than the recycling because it requires extra cache line accesses (12.1% loss, ->18.6Gbps).

In-place skb_ext may be shorter than a full skb_ext object. The driver only reserves space for exts it may use. Any later addition will reallocate the space via CoW, abandoning the in-place skb_ext and copying the data to a full slab object.

I personally like the other option (the non RFC series) more. It looks like this requires a quite larger code churn that could be error-prone and even non interested parties/callers/code-paths may end-up paying some extra overhead/additional cycles.

Still, if you are willing to experiment more this idea, I think you could save the extra cacheline miss encoding the 'alloc_mode' into the lower bits of skb->extensions (alike what _skb_refdst is doing with the SKB_DST_NOREF flag).

Cheers,

Paolo

author maintainer
#2 lore raw
Quoted 4 lines · On Wed, 15 Feb 2023 09:53:54 +0100 Paolo Abeni wrote:

Still, if you are willing to experiment more this idea, I think you could save the extra cacheline miss encoding the 'alloc_mode' into the lower bits of skb->extensions (alike what _skb_refdst is doing with the SKB_DST_NOREF flag).

I thought I'd start with a simpler approach where allocation type is stored in the object itself, to limit the negative reactions :P We could indeed save a cache miss (I think it'd be actually one fewer miss than the current implementation, because most cases must end up looking at the skb_ext, f.e. to read the refcount. The fact that SHARD_NOREF implies refcnt == 1 can save us reading from skb_ext).

#3 lore raw
In reply to Jakub Kicinski
Quoted 5 lines · Jakub Kicinski

This is a bit more crazy than the previous patch. For drivers which already use build_skb() it's relatively easy to add more space to the shinfo. Use this approach to place skb_ext inside the head. No allocation needed.

Quoted 8 lines

This approach is a bit slower in trivial benchmarks than the recycling because it requires extra cache line accesses (12.1% loss, ->18.6Gbps).

In-place skb_ext may be shorter than a full skb_ext object. The driver only reserves space for exts it may use. Any later addition will reallocate the space via CoW, abandoning the in-place skb_ext and copying the data to a full slab object.

I think the cleaner solution would be to move the new extension ids into sk_buff itself (at the end, uninitialized data unless used).

Those extensions would always reside there and not in the slab object.

Obviously that only makes sense for extensions where we assume that typical workload will require them, which might be a hard call to make.

I concur with Paolo that the napi-caching is nicer/less intrusive, I think we have to wait and see if it helps with psp (async crypto needed?) when it lands.

#4 lore raw
Quoted 10 lines · Florian Westphal
Quoted 8 lines · Jakub Kicinski

This is a bit more crazy than the previous patch. For drivers which already use build_skb() it's relatively easy to add more space to the shinfo. Use this approach to place skb_ext inside the head. No allocation needed.

This approach is a bit slower in trivial benchmarks than the recycling because it requires extra cache line accesses (12.1% loss, ->18.6Gbps).

Quoted 8 lines
Quoted 5 lines

In-place skb_ext may be shorter than a full skb_ext object. The driver only reserves space for exts it may use. Any later addition will reallocate the space via CoW, abandoning the in-place skb_ext and copying the data to a full slab object.

I think the cleaner solution would be to move the new extension ids into sk_buff itself (at the end, uninitialized data unless used).

Grow struct sk_buff?

Quoted 8 lines

Those extensions would always reside there and not in the slab object. Obviously that only makes sense for extensions where we assume that typical workload will require them, which might be a hard call to make.

I concur with Paolo that the napi-caching is nicer/less intrusive, I think we have to wait and see if it helps with psp (async crypto needed?) when it lands.

How much data does psp need? The google version [1] embeds structs psp_skb, which may include a 256b key. If on tx the key is looked up from skb->sk, then on rx the only truly required field is the 32-bit SPI, to match a decrypted packet's session key to the socket. With a pointer hack on the lowest bits of skb->extensions such a tiny extension could perhaps be embedded in the pointer field itself.

https://github.com/google/psp/blob/linux-v5.15-psp-v1.0/include/net/psp_defs.h

author maintainer
#5 lore raw
Quoted 8 lines · On Wed, 15 Feb 2023 09:37:09 -0500 Willem de Bruijn wrote:

How much data does psp need? The google version [1] embeds structs psp_skb, which may include a 256b key. If on tx the key is looked up from skb->sk, then on rx the only truly required field is the 32-bit SPI, to match a decrypted packet's session key to the socket. With a pointer hack on the lowest bits of skb->extensions such a tiny extension could perhaps be embedded in the pointer field itself.

https://github.com/google/psp/blob/linux-v5.15-psp-v1.0/include/net/psp_defs.h

So.. the most I could compress it to without sacrificing any security was:

struct psp_skb_ext {
	u32 spi;
	u16 generation;
	u8 version;
};

I took the liberty to cut the generation down to 16 bits. The version is not necessary if we assume all machines and flow run a single version. But then why is the auth-only version even in the spec :(

In any case, you're right that this would fit into the pointer with minor clever encoding. It felt even more hacky than extending shinfo TBH.

I'd be curious to hear other opinions!

author maintainer
#6 lore raw
In reply to Florian Westphal
Quoted 4 lines · On Wed, 15 Feb 2023 10:43:32 +0100 Florian Westphal wrote:

I think the cleaner solution would be to move the new extension ids into sk_buff itself (at the end, uninitialized data unless used).

Those extensions would always reside there and not in the slab object.

Do you mean the entire extension? 8B of metadata + (possibly) 32B of the key?

Quoted 3 lines

Obviously that only makes sense for extensions where we assume that typical workload will require them, which might be a hard call to make.

I'm guessing that's the reason why Google is okay with putting the key in the skb - they know they will use it most of the time. But an average RHEL user may appreciate the skb growth for an esoteric protocol to a much smaller extent :(

#7 lore raw
Quoted 8 lines · Jakub Kicinski
Quoted 4 lines · On Wed, 15 Feb 2023 10:43:32 +0100 Florian Westphal wrote:

I think the cleaner solution would be to move the new extension ids into sk_buff itself (at the end, uninitialized data unless used).

Those extensions would always reside there and not in the slab object.

Do you mean the entire extension? 8B of metadata + (possibly) 32B of the key?

32B is too much if its for something esoteric, but see below.

Quoted 8 lines
Quoted 3 lines

Obviously that only makes sense for extensions where we assume that typical workload will require them, which might be a hard call to make.

I'm guessing that's the reason why Google is okay with putting the key in the skb - they know they will use it most of the time. But an average RHEL user may appreciate the skb growth for an esoteric protocol to a much smaller extent :(

Absolutely, I agree that its a non-starter to place this in sk_buff itself. TX side is less of a problem here because of superpackets.

For RX I think your simpler napi-recycle patch is a good start. I feel its better to wait before doing anything further in this direction (e.g. array-of-cached extensions or whatever) until we've a better test case/more realistic workload(s).

If we need to look at further allocation avoidances one thing that could be evaluated would be placing an extension struct into sk_buff_fclones (unioned with the fclone skb).
Fclone skb is marked busy, extension release clears it again.

Just something to keep in mind for later. Only downside I see is that we can't release the extension area anymore before the skb gets queued.