net: usb: smsc95xx: stop lying about skb->truesize
authorEric Dumazet <edumazet@google.com>
Thu, 9 May 2024 08:33:13 +0000 (08:33 +0000)
committerJakub Kicinski <kuba@kernel.org>
Sat, 11 May 2024 02:22:53 +0000 (19:22 -0700)
Some usb drivers try to set small skb->truesize and break
core networking stacks.

In this patch, I removed one of the skb->truesize override.

I also replaced one skb_clone() by an allocation of a fresh
and small skb, to get minimally sized skbs, like we did
in commit 1e2c61172342 ("net: cdc_ncm: reduce skb truesize
in rx path") and 4ce62d5b2f7a ("net: usb: ax88179_178a:
stop lying about skb->truesize")

v3: also fix a sparse error ( https://lore.kernel.org/oe-kbuild-all/202405091310.KvncIecx-lkp@intel.com/ )
v2: leave the skb_trim() game because smsc95xx_rx_csum_offload()
    needs the csum part. (Jakub)
    While we are it, use get_unaligned() in smsc95xx_rx_csum_offload().

Fixes: 2f7ca802bdae ("net: Add SMSC LAN9500 USB2.0 10/100 ethernet adapter driver")
Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: Steve Glendinning <steve.glendinning@shawell.net>
Cc: UNGLinuxDriver@microchip.com
Reviewed-by: Simon Horman <horms@kernel.org>
Link: https://lore.kernel.org/r/20240509083313.2113832-1-edumazet@google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
drivers/net/usb/smsc95xx.c

index 2fa46baa589e5e87e12e145fe46268bdaf9fc219..cbea246664795f27618908838b37384e8f3b67d0 100644 (file)
@@ -1810,9 +1810,11 @@ static int smsc95xx_reset_resume(struct usb_interface *intf)
 
 static void smsc95xx_rx_csum_offload(struct sk_buff *skb)
 {
-       skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2);
+       u16 *csum_ptr = (u16 *)(skb_tail_pointer(skb) - 2);
+
+       skb->csum = (__force __wsum)get_unaligned(csum_ptr);
        skb->ip_summed = CHECKSUM_COMPLETE;
-       skb_trim(skb, skb->len - 2);
+       skb_trim(skb, skb->len - 2); /* remove csum */
 }
 
 static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
@@ -1870,25 +1872,22 @@ static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
                                if (dev->net->features & NETIF_F_RXCSUM)
                                        smsc95xx_rx_csum_offload(skb);
                                skb_trim(skb, skb->len - 4); /* remove fcs */
-                               skb->truesize = size + sizeof(struct sk_buff);
 
                                return 1;
                        }
 
-                       ax_skb = skb_clone(skb, GFP_ATOMIC);
+                       ax_skb = netdev_alloc_skb_ip_align(dev->net, size);
                        if (unlikely(!ax_skb)) {
                                netdev_warn(dev->net, "Error allocating skb\n");
                                return 0;
                        }
 
-                       ax_skb->len = size;
-                       ax_skb->data = packet;
-                       skb_set_tail_pointer(ax_skb, size);
+                       skb_put(ax_skb, size);
+                       memcpy(ax_skb->data, packet, size);
 
                        if (dev->net->features & NETIF_F_RXCSUM)
                                smsc95xx_rx_csum_offload(ax_skb);
                        skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
-                       ax_skb->truesize = size + sizeof(struct sk_buff);
 
                        usbnet_skb_return(dev, ax_skb);
                }