net: fec: fix missing kmalloc() failure check in fec_enet_alloc_buffers()
authorRussell King <rmk+kernel@arm.linux.org.uk>
Mon, 7 Jul 2014 23:23:30 +0000 (00:23 +0100)
committerDavid S. Miller <davem@davemloft.net>
Tue, 8 Jul 2014 04:21:23 +0000 (21:21 -0700)
fec_enet_alloc_buffers() assumes that kmalloc() will never fail, which
is an invalid assumption.  Fix this by implementing a common error
cleanup path, and use it to also clean up after failed bounce buffer
allocation.

Acked-by: Fugang Duan <B38611@freescale.com>
Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/ethernet/freescale/fec_main.c

index 5499bd8ad0a59f38fca4dfdb1dec33f76ba8ef4a..f43c388e2eb93414fea632944e5e5190b9549aaa 100644 (file)
@@ -2100,19 +2100,16 @@ static int fec_enet_alloc_buffers(struct net_device *ndev)
                dma_addr_t addr;
 
                skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
-               if (!skb) {
-                       fec_enet_free_buffers(ndev);
-                       return -ENOMEM;
-               }
+               if (!skb)
+                       goto err_alloc;
 
                addr = dma_map_single(&fep->pdev->dev, skb->data,
                                FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
                if (dma_mapping_error(&fep->pdev->dev, addr)) {
                        dev_kfree_skb(skb);
-                       fec_enet_free_buffers(ndev);
                        if (net_ratelimit())
                                netdev_err(ndev, "Rx DMA memory map failed\n");
-                       return -ENOMEM;
+                       goto err_alloc;
                }
 
                fep->rx_skbuff[i] = skb;
@@ -2134,6 +2131,8 @@ static int fec_enet_alloc_buffers(struct net_device *ndev)
        bdp = fep->tx_bd_base;
        for (i = 0; i < fep->tx_ring_size; i++) {
                fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
+               if (!fep->tx_bounce[i])
+                       goto err_alloc;
 
                bdp->cbd_sc = 0;
                bdp->cbd_bufaddr = 0;
@@ -2151,6 +2150,10 @@ static int fec_enet_alloc_buffers(struct net_device *ndev)
        bdp->cbd_sc |= BD_SC_WRAP;
 
        return 0;
+
+ err_alloc:
+       fec_enet_free_buffers(ndev);
+       return -ENOMEM;
 }
 
 static int