6631cb8e17c8e3d0293edd88cd390771319d7629
[openwrt/openwrt.git] / target / linux / ath79 / patches-5.4 / 200-ag71xx-Handle-allocation-errors-in-ag71xx_rings_init.patch
1 From 2cee757eaf5cc6175bc0ac7b0b808794124ec40a Mon Sep 17 00:00:00 2001
2 From: Hauke Mehrtens <hauke@hauke-m.de>
3 Date: Mon, 17 Feb 2020 23:40:14 +0100
4 Subject: [PATCH 1/3] ag71xx: Handle allocation errors in ag71xx_rings_init()
5
6 Free the allocated resources in ag71xx_rings_init() in case
7 ag71xx_ring_rx_init() returns an error.
8
9 This is only a potential problem, I did not ran into this one.
10
11 Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
12 Fixes: d51b6ce441d3 ("net: ethernet: add ag71xx driver")
13 ---
14 drivers/net/ethernet/atheros/ag71xx.c | 22 ++++++++++++++++++----
15 1 file changed, 18 insertions(+), 4 deletions(-)
16
17 --- a/drivers/net/ethernet/atheros/ag71xx.c
18 +++ b/drivers/net/ethernet/atheros/ag71xx.c
19 @@ -1133,6 +1133,7 @@ static int ag71xx_rings_init(struct ag71
20 struct ag71xx_ring *tx = &ag->tx_ring;
21 struct ag71xx_ring *rx = &ag->rx_ring;
22 int ring_size, tx_size;
23 + int ret;
24
25 ring_size = BIT(tx->order) + BIT(rx->order);
26 tx_size = BIT(tx->order);
27 @@ -1145,9 +1146,8 @@ static int ag71xx_rings_init(struct ag71
28 ring_size * AG71XX_DESC_SIZE,
29 &tx->descs_dma, GFP_KERNEL);
30 if (!tx->descs_cpu) {
31 - kfree(tx->buf);
32 - tx->buf = NULL;
33 - return -ENOMEM;
34 + ret = -ENOMEM;
35 + goto err_free_buf;
36 }
37
38 rx->buf = &tx->buf[tx_size];
39 @@ -1155,7 +1155,21 @@ static int ag71xx_rings_init(struct ag71
40 rx->descs_dma = tx->descs_dma + tx_size * AG71XX_DESC_SIZE;
41
42 ag71xx_ring_tx_init(ag);
43 - return ag71xx_ring_rx_init(ag);
44 + ret = ag71xx_ring_rx_init(ag);
45 + if (ret)
46 + goto err_free_dma;
47 +
48 + return 0;
49 +
50 +err_free_dma:
51 + dma_free_coherent(&ag->pdev->dev, ring_size * AG71XX_DESC_SIZE,
52 + tx->descs_cpu, tx->descs_dma);
53 + rx->buf = NULL;
54 +err_free_buf:
55 + kfree(tx->buf);
56 + tx->buf = NULL;
57 +
58 + return ret;
59 }
60
61 static void ag71xx_rings_free(struct ag71xx *ag)