Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: optimize eth_type_trans() vs CONFIG_STACKPROTECTOR_STRONG=y

Some platforms exhibit very high costs with CONFIG_STACKPROTECTOR_STRONG=y
when a function needs to pass the address of a local variable to external
functions.

eth_type_trans() (and its callers) is showing this anomaly on AMD EPYC 7B12
platforms (and maybe others).

We could :

1) inline eth_type_trans()

This would help if its callers also has the same issue, and the canary cost
would be paid by the callers already.

This is a bit cumbersome because netdev_uses_dsa() is pulling
whole <net/dsa.h> definitions.

2) Compile net/ethernet/eth.c with -fno-stack-protector

This would weaken security.

3) Hack eth_type_trans() to temporarily use skb->dev as a place holder
if skb_header_pointer() needs to pull 2 bytes not present in skb->head.

This patch implements 3), and brings a 5% improvement on TX/RX intensive
workload (tcp_rr 10,000 flows) on AMD EPYC 7B12.

Removing CONFIG_STACKPROTECTOR_STRONG on this platform can improve
performance by 25 %.
This means eth_type_trans() issue is not an isolated artifact.

Signed-off-by: Eric Dumazet <edumazet@google.com>
Reviewed-by: Willem de Bruijn <willemb@google.com>
Link: https://patch.msgid.link/20251121061725.206675-1-edumazet@google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Eric Dumazet and committed by
Jakub Kicinski
ec1e48e9 e254c212

+8 -8
+8 -8
net/ethernet/eth.c
··· 154 154 */ 155 155 __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev) 156 156 { 157 - unsigned short _service_access_point; 158 157 const unsigned short *sap; 159 158 const struct ethhdr *eth; 159 + __be16 res; 160 160 161 161 skb->dev = dev; 162 162 skb_reset_mac_header(skb); ··· 181 181 * the protocol design and runs IPX over 802.3 without an 802.2 LLC 182 182 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This 183 183 * won't work for fault tolerant netware but does for the rest. 184 + * We use skb->dev as temporary storage to not hit 185 + * CONFIG_STACKPROTECTOR_STRONG=y costs on some platforms. 184 186 */ 185 - sap = skb_header_pointer(skb, 0, sizeof(*sap), &_service_access_point); 186 - if (sap && *sap == 0xFFFF) 187 - return htons(ETH_P_802_3); 187 + sap = skb_header_pointer(skb, 0, sizeof(*sap), &skb->dev); 188 + res = (sap && *sap == 0xFFFF) ? htons(ETH_P_802_3) : htons(ETH_P_802_2); 188 189 189 - /* 190 - * Real 802.2 LLC 191 - */ 192 - return htons(ETH_P_802_2); 190 + /* restore skb->dev in case it was mangled by skb_header_pointer(). */ 191 + skb->dev = dev; 192 + return res; 193 193 } 194 194 EXPORT_SYMBOL(eth_type_trans); 195 195