Skip to contentSkip to conversation

net: skbuff: hide some bitfield members

PATCH Applied Jakub Kicinski 3 participants 9 messages Apr 17, 2023 – Apr 19, 2023

Subsystems (4)
lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

v2 cover net: skbuff: hide some bitfield members

There is a number of protocol or subsystem specific fields in struct sk_buff which are only accessed by one subsystem. We can wrap them in ifdefs with minimal code impact.

This gives us a better chance to save a 2B and a 4B holes resulting with the following savings (assuming a lucky kernel config):

- /* size: 232, cachelines: 4, members: 28 */
- /* sum members: 227, holes: 1, sum holes: 4 */
- /* sum bitfield members: 8 bits (1 bytes) */
+ /* size: 224, cachelines: 4, members: 28 */
/* forced alignments: 2 */
- /* last cacheline: 40 bytes */
+ /* last cacheline: 32 bytes */

I think that the changes shouldn't be too controversial. The only one I'm not 100% sure of is the SCTP one, 12 extra LoC for one bit.. But it did fit squarely in the "this bit has only one user" category.

v2:
- use IS_ENABLED() for nf_trace
v1: https://lore.kernel.org/all/[email protected]/

Jakub Kicinski (5):
net: skbuff: hide wifi_acked when CONFIG_WIRELESS not set
net: skbuff: hide csum_not_inet when CONFIG_IP_SCTP not set
net: skbuff: move alloc_cpu into a potential hole
net: skbuff: push nf_trace down the bitfield
net: skbuff: hide nf_trace and ipvs_property

 include/linux/skbuff.h | 38 ++++++++++++++++++++++++++++++++++----
 include/net/sock.h     |  2 +-
 net/core/dev.c         |  3 +--
 net/core/skbuff.c      |  2 ++
 net/sched/act_csum.c   |  3 +--
 net/socket.c           |  2 ++
 6 files changed, 41 insertions(+), 9 deletions(-)
Signature · 1 line
2.39.2
author maintainer
#1 lore raw

PATCH v2 1/5 net: skbuff: hide wifi_acked when CONFIG_WIRELESS not set

Datacenter kernel builds will very likely not include WIRELESS, so let them shave 2 bits off the skb by hiding the wifi fields.

---
CC: [email protected]

---
 include/linux/skbuff.h | 11 +++++++++++
 include/net/sock.h     |  2 +-
 net/core/skbuff.c      |  2 ++
 net/socket.c           |  2 ++
 4 files changed, 16 insertions(+), 1 deletion(-)
include/linux/skbuff.h +11 -0
index a823ec3aa326..513f03b23a73 100644
@@ -953,8 +953,10 @@ struct sk_buff {
 
 	__u8			l4_hash:1;
 	__u8			sw_hash:1;
+#ifdef CONFIG_WIRELESS
 	__u8			wifi_acked_valid:1;
 	__u8			wifi_acked:1;
+#endif
 	__u8			no_fcs:1;
 	/* Indicates the inner headers are valid in the skbuff. */
 	__u8			encapsulation:1;
@@ -1187,6 +1189,15 @@ static inline unsigned int skb_napi_id(const struct sk_buff *skb)
 #endif
 }
 
+static inline bool skb_wifi_acked_valid(const struct sk_buff *skb)
+{
+#ifdef CONFIG_WIRELESS
+	return skb->wifi_acked_valid;
+#else
+	return 0;
+#endif
+}
+
 /**
  * skb_unref - decrement the skb's reference count
  * @skb: buffer
include/net/sock.h +1 -1
index 5edf0038867c..8b7ed7167243 100644
@@ -2697,7 +2697,7 @@ sock_recv_timestamp(struct msghdr *msg, struct sock *sk, struct sk_buff *skb)
 	else
 		sock_write_timestamp(sk, kt);
 
-	if (sock_flag(sk, SOCK_WIFI_STATUS) && skb->wifi_acked_valid)
+	if (sock_flag(sk, SOCK_WIFI_STATUS) && skb_wifi_acked_valid(skb))
 		__sock_recv_wifi_status(msg, sk, skb);
 }
 
net/core/skbuff.c +2 -0
index ef81452759be..768f9d04911f 100644
@@ -5189,6 +5189,7 @@ void skb_tstamp_tx(struct sk_buff *orig_skb,
 }
 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
 
+#ifdef CONFIG_WIRELESS
 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
 {
 	struct sock *sk = skb->sk;
@@ -5214,6 +5215,7 @@ void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
 		kfree_skb(skb);
 }
 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
+#endif /* CONFIG_WIRELESS */
 
 /**
  * skb_partial_csum_set - set up and verify partial csum values for packet
net/socket.c +2 -0
index 73e493da4589..a7b4b37d86df 100644
@@ -957,6 +957,7 @@ void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
 }
 EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
 
+#ifdef CONFIG_WIRELESS
 void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
 	struct sk_buff *skb)
 {
@@ -972,6 +973,7 @@ void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
 	put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
 }
 EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
+#endif
 
 static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
 				   struct sk_buff *skb)
Signature · 1 line
2.39.2
author maintainer
#3 lore raw

PATCH v2 2/5 net: skbuff: hide csum_not_inet when CONFIG_IP_SCTP not set

In reply to Jakub Kicinski

SCTP is not universally deployed, allow hiding its bit from the skb.

---
 include/linux/skbuff.h | 14 ++++++++++++++
 net/core/dev.c         |  3 +--
 net/sched/act_csum.c   |  3 +--
 3 files changed, 16 insertions(+), 4 deletions(-)
include/linux/skbuff.h +14 -0
index 513f03b23a73..98d6b48f4dcf 100644
@@ -983,7 +983,9 @@ struct sk_buff {
 	__u8			decrypted:1;
 #endif
 	__u8			slow_gro:1;
+#if IS_ENABLED(CONFIG_IP_SCTP)
 	__u8			csum_not_inet:1;
+#endif
 
 #ifdef CONFIG_NET_SCHED
 	__u16			tc_index;	/* traffic control index */
@@ -5060,7 +5062,19 @@ static inline void skb_reset_redirect(struct sk_buff *skb)
 
 static inline bool skb_csum_is_sctp(struct sk_buff *skb)
 {
+#if IS_ENABLED(CONFIG_IP_SCTP)
 	return skb->csum_not_inet;
+#else
+	return 0;
+#endif
+}
+
+static inline void skb_reset_csum_not_inet(struct sk_buff *skb)
+{
+	skb->ip_summed = CHECKSUM_NONE;
+#if IS_ENABLED(CONFIG_IP_SCTP)
+	skb->csum_not_inet = 0;
+#endif
 }
 
 static inline void skb_set_kcov_handle(struct sk_buff *skb,
net/core/dev.c +1 -2
index 8aea68275172..3fc4dba71f9d 100644
@@ -3315,8 +3315,7 @@ int skb_crc32c_csum_help(struct sk_buff *skb)
 						  skb->len - start, ~(__u32)0,
 						  crc32c_csum_stub));
 	*(__le32 *)(skb->data + offset) = crc32c_csum;
-	skb->ip_summed = CHECKSUM_NONE;
-	skb->csum_not_inet = 0;
+	skb_reset_csum_not_inet(skb);
 out:
 	return ret;
 }
net/sched/act_csum.c +1 -2
index 95e9304024b7..8ed285023a40 100644
@@ -376,8 +376,7 @@ static int tcf_csum_sctp(struct sk_buff *skb, unsigned int ihl,
 
 	sctph->checksum = sctp_compute_cksum(skb,
 					     skb_network_offset(skb) + ihl);
-	skb->ip_summed = CHECKSUM_NONE;
-	skb->csum_not_inet = 0;
+	skb_reset_csum_not_inet(skb);
 
 	return 1;
 }
Signature · 1 line
2.39.2
author maintainer
#4 lore raw

PATCH v2 3/5 net: skbuff: move alloc_cpu into a potential hole

In reply to Jakub Kicinski

alloc_cpu is currently between 4 byte fields, so it's almost guaranteed to create a 2B hole. It has a knock on effect of creating a 4B hole after @end (and @end and @tail being in different cachelines).

None of this matters hugely, but for kernel configs which don't enable all the features there may well be a 2B hole after the bitfield. Move alloc_cpu there.

---
 include/linux/skbuff.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/skbuff.h +2 -1
index 98d6b48f4dcf..2595b2cfba0d 100644
@@ -991,6 +991,8 @@ struct sk_buff {
 	__u16			tc_index;	/* traffic control index */
 #endif
 
+	u16			alloc_cpu;
+
 	union {
 		__wsum		csum;
 		struct {
@@ -1014,7 +1016,6 @@ struct sk_buff {
 		unsigned int	sender_cpu;
 	};
 #endif
-	u16			alloc_cpu;
 #ifdef CONFIG_NETWORK_SECMARK
 	__u32		secmark;
 #endif
Signature · 1 line
2.39.2
author maintainer
#5 lore raw

PATCH v2 4/5 net: skbuff: push nf_trace down the bitfield

In reply to Jakub Kicinski

nf_trace is a debug feature, AFAIU, and yet it sits oddly high in the sk_buff bitfield. Move it down, pushing up dst_pending_confirm and inner_protocol_type.

Next change will make nf_trace optional (under Kconfig) and all optional fields should be placed after 2b fields to avoid 2b fields straddling bytes.

dst_pending_confirm is L3, so it makes sense next to ignore_df. inner_protocol_type goes up just to keep the balance.

---
CC: [email protected]
CC: [email protected]

---
 include/linux/skbuff.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/skbuff.h +3 -3
index 2595b2cfba0d..3ae9e8868afa 100644
@@ -934,7 +934,7 @@ struct sk_buff {
 	/* public: */
 	__u8			pkt_type:3; /* see PKT_TYPE_MAX */
 	__u8			ignore_df:1;
-	__u8			nf_trace:1;
+	__u8			dst_pending_confirm:1;
 	__u8			ip_summed:2;
 	__u8			ooo_okay:1;
 
@@ -949,7 +949,7 @@ struct sk_buff {
 	__u8			remcsum_offload:1;
 	__u8			csum_complete_sw:1;
 	__u8			csum_level:2;
-	__u8			dst_pending_confirm:1;
+	__u8			inner_protocol_type:1;
 
 	__u8			l4_hash:1;
 	__u8			sw_hash:1;
@@ -967,7 +967,7 @@ struct sk_buff {
 #endif
 
 	__u8			ipvs_property:1;
-	__u8			inner_protocol_type:1;
+	__u8			nf_trace:1;
 #ifdef CONFIG_NET_SWITCHDEV
 	__u8			offload_fwd_mark:1;
 	__u8			offload_l3_fwd_mark:1;
Signature · 1 line
2.39.2
author maintainer
#6 lore raw

PATCH v2 5/5 net: skbuff: hide nf_trace and ipvs_property

In reply to Jakub Kicinski

Accesses to nf_trace and ipvs_property are already wrapped by ifdefs where necessary. Don't allocate the bits for those fields at all if possible.

---
v2:
- use IS_ENABLED()
v1: https://lore.kernel.org/all/[email protected]/

---
 include/linux/skbuff.h | 4 ++++
 1 file changed, 4 insertions(+)
include/linux/skbuff.h +4 -0
index 3ae9e8868afa..5f120bbab9da 100644
@@ -966,8 +966,12 @@ struct sk_buff {
 	__u8			ndisc_nodetype:2;
 #endif
 
+#if IS_ENABLED(CONFIG_IP_VS)
 	__u8			ipvs_property:1;
+#endif
+#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) || IS_ENABLED(CONFIG_NF_TABLES)
 	__u8			nf_trace:1;
+#endif
 #ifdef CONFIG_NET_SWITCHDEV
 	__u8			offload_fwd_mark:1;
 	__u8			offload_l3_fwd_mark:1;
Signature · 1 line
2.39.2
author maintainer Acked-by Simon Horman on the v2 series
#7 lore raw
In reply to Jakub Kicinski
Quoted 16 lines · On Mon, 17 Apr 2023 08:53:45 -0700 Jakub Kicinski wrote:

There is a number of protocol or subsystem specific fields in struct sk_buff which are only accessed by one subsystem. We can wrap them in ifdefs with minimal code impact.

This gives us a better chance to save a 2B and a 4B holes resulting with the following savings (assuming a lucky kernel config):

(quoted diff) +2 -4
-	/* size: 232, cachelines: 4, members: 28 */
-	/* sum members: 227, holes: 1, sum holes: 4 */
-	/* sum bitfield members: 8 bits (1 bytes) */
+	/* size: 224, cachelines: 4, members: 28 */
 	/* forced alignments: 2 */
-	/* last cacheline: 40 bytes */
+	/* last cacheline: 32 bytes */
Quoted 4 lines

I think that the changes shouldn't be too controversial. The only one I'm not 100% sure of is the SCTP one, 12 extra LoC for one bit.. But it did fit squarely in the "this bit has only one user" category.

Missed Simon's tag, sorry about that:
https://lore.kernel.org/all/[email protected]/

Acked-by on v2 1/5
#2 lore raw

Re: [PATCH net-next v2 1/5] net: skbuff: hide wifi_acked when CONFIG_WIRELESS not set

In reply to Jakub Kicinski
Quoted 2 lines · Jakub Kicinski

Datacenter kernel builds will very likely not include WIRELESS, so let them shave 2 bits off the skb by hiding the wifi fields.

:-)

You could make this be CONFIG_MAC80211 if you wanted to, but I don't mind CONFIG_WIRELESS and honestly they're probably pretty much equivalent because there aren't many non-mac80211 drivers.

johannes

#8 lore raw
In reply to Jakub Kicinski
This series was applied to netdev/net-next.git (main) by David S. Miller <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by David S. Miller <[email protected]>:

Quoted 9 lines · On Mon, 17 Apr 2023 08:53:45 -0700 you wrote:

There is a number of protocol or subsystem specific fields in struct sk_buff which are only accessed by one subsystem. We can wrap them in ifdefs with minimal code impact.

This gives us a better chance to save a 2B and a 4B holes resulting with the following savings (assuming a lucky kernel config):

[...]

Here is the summary with links:
- [net-next,v2,1/5] net: skbuff: hide wifi_acked when CONFIG_WIRELESS not set

    https://git.kernel.org/netdev/net-next/c/eb6fba7555a8
  - [net-next,v2,2/5] net: skbuff: hide csum_not_inet when CONFIG_IP_SCTP not set
    https://git.kernel.org/netdev/net-next/c/c24831a13ba2
  - [net-next,v2,3/5] net: skbuff: move alloc_cpu into a potential hole
    https://git.kernel.org/netdev/net-next/c/4398f3f6d138
  - [net-next,v2,4/5] net: skbuff: push nf_trace down the bitfield
    https://git.kernel.org/netdev/net-next/c/4c60d04c2888
  - [net-next,v2,5/5] net: skbuff: hide nf_trace and ipvs_property
    https://git.kernel.org/netdev/net-next/c/48d80c394d3d

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html