* [PATCH v2] rtl818x_pci: Fix potential memory leaks in rtl8180_init_rx_ring()
@ 2025-11-14 9:45 Abdun Nihaal
2025-11-17 3:43 ` Ping-Ke Shih
2025-11-18 2:32 ` Ping-Ke Shih
0 siblings, 2 replies; 3+ messages in thread
From: Abdun Nihaal @ 2025-11-14 9:45 UTC (permalink / raw)
To: Ping-Ke Shih, linux-wireless; +Cc: Abdun Nihaal, linux-kernel
In rtl8180_init_rx_ring(), memory is allocated for skb packets and DMA
allocations in a loop. When an allocation fails, the previously
successful allocations are not freed on exit.
Fix that by jumping to err_free_rings label on error, which calls
rtl8180_free_rx_ring() to free the allocations. Remove the free of
rx_ring in rtl8180_init_rx_ring() error path, and set the freed
priv->rx_buf entry to null, to avoid double free.
Fixes: f653211197f3 ("Add rtl8180 wireless driver")
Signed-off-by: Abdun Nihaal <nihaal@cse.iitm.ac.in>
---
v1->v2:
- Use rtl8180_free_rx_ring() to free the buffers by jumping to
err_free_rings label, as suggested by Ping-Ke Shih.
- Remove free of rx_ring and set the freed priv->rx_buf entry to null,
in the error path to avoid it getting freed again in
rtl8180_free_rx_ring()
Calling rtl8180_free_rx_ring() directly in rtl8180_init_rx_ring()
requires moving the free function before (implicit declaration error).
So in order to minimize the code change, I used the err_free_rings label
as suggested by Ping-Ke Shih.
Link to v1:
https://lore.kernel.org/all/20251113165851.39054-1-nihaal@cse.iitm.ac.in/
drivers/net/wireless/realtek/rtl818x/rtl8180/dev.c | 9 ++-------
1 file changed, 2 insertions(+), 7 deletions(-)
diff --git a/drivers/net/wireless/realtek/rtl818x/rtl8180/dev.c b/drivers/net/wireless/realtek/rtl818x/rtl8180/dev.c
index 2905baea6239..070c0431c482 100644
--- a/drivers/net/wireless/realtek/rtl818x/rtl8180/dev.c
+++ b/drivers/net/wireless/realtek/rtl818x/rtl8180/dev.c
@@ -1023,9 +1023,6 @@ static int rtl8180_init_rx_ring(struct ieee80211_hw *dev)
dma_addr_t *mapping;
entry = priv->rx_ring + priv->rx_ring_sz*i;
if (!skb) {
- dma_free_coherent(&priv->pdev->dev,
- priv->rx_ring_sz * 32,
- priv->rx_ring, priv->rx_ring_dma);
wiphy_err(dev->wiphy, "Cannot allocate RX skb\n");
return -ENOMEM;
}
@@ -1037,9 +1034,7 @@ static int rtl8180_init_rx_ring(struct ieee80211_hw *dev)
if (dma_mapping_error(&priv->pdev->dev, *mapping)) {
kfree_skb(skb);
- dma_free_coherent(&priv->pdev->dev,
- priv->rx_ring_sz * 32,
- priv->rx_ring, priv->rx_ring_dma);
+ priv->rx_buf[i] = NULL;
wiphy_err(dev->wiphy, "Cannot map DMA for RX skb\n");
return -ENOMEM;
}
@@ -1130,7 +1125,7 @@ static int rtl8180_start(struct ieee80211_hw *dev)
ret = rtl8180_init_rx_ring(dev);
if (ret)
- return ret;
+ goto err_free_rings;
for (i = 0; i < (dev->queues + 1); i++)
if ((ret = rtl8180_init_tx_ring(dev, i, 16)))
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* RE: [PATCH v2] rtl818x_pci: Fix potential memory leaks in rtl8180_init_rx_ring()
2025-11-14 9:45 [PATCH v2] rtl818x_pci: Fix potential memory leaks in rtl8180_init_rx_ring() Abdun Nihaal
@ 2025-11-17 3:43 ` Ping-Ke Shih
2025-11-18 2:32 ` Ping-Ke Shih
1 sibling, 0 replies; 3+ messages in thread
From: Ping-Ke Shih @ 2025-11-17 3:43 UTC (permalink / raw)
To: Abdun Nihaal, linux-wireless@vger.kernel.org; +Cc: linux-kernel@vger.kernel.org
Abdun Nihaal <nihaal@cse.iitm.ac.in> wrote:
> In rtl8180_init_rx_ring(), memory is allocated for skb packets and DMA
> allocations in a loop. When an allocation fails, the previously
> successful allocations are not freed on exit.
>
> Fix that by jumping to err_free_rings label on error, which calls
> rtl8180_free_rx_ring() to free the allocations. Remove the free of
> rx_ring in rtl8180_init_rx_ring() error path, and set the freed
> priv->rx_buf entry to null, to avoid double free.
>
> Fixes: f653211197f3 ("Add rtl8180 wireless driver")
> Signed-off-by: Abdun Nihaal <nihaal@cse.iitm.ac.in>
Reviewed-by: Ping-Ke Shih <pkshih@realtek.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] rtl818x_pci: Fix potential memory leaks in rtl8180_init_rx_ring()
2025-11-14 9:45 [PATCH v2] rtl818x_pci: Fix potential memory leaks in rtl8180_init_rx_ring() Abdun Nihaal
2025-11-17 3:43 ` Ping-Ke Shih
@ 2025-11-18 2:32 ` Ping-Ke Shih
1 sibling, 0 replies; 3+ messages in thread
From: Ping-Ke Shih @ 2025-11-18 2:32 UTC (permalink / raw)
To: Abdun Nihaal, Ping-Ke Shih, linux-wireless; +Cc: Abdun Nihaal, linux-kernel
Abdun Nihaal <nihaal@cse.iitm.ac.in> wrote:
> In rtl8180_init_rx_ring(), memory is allocated for skb packets and DMA
> allocations in a loop. When an allocation fails, the previously
> successful allocations are not freed on exit.
>
> Fix that by jumping to err_free_rings label on error, which calls
> rtl8180_free_rx_ring() to free the allocations. Remove the free of
> rx_ring in rtl8180_init_rx_ring() error path, and set the freed
> priv->rx_buf entry to null, to avoid double free.
>
> Fixes: f653211197f3 ("Add rtl8180 wireless driver")
> Signed-off-by: Abdun Nihaal <nihaal@cse.iitm.ac.in>
> Reviewed-by: Ping-Ke Shih <pkshih@realtek.com>
1 patch(es) applied to rtw-next branch of rtw.git, thanks.
9b5b9c042b30 wifi: rtl818x: Fix potential memory leaks in rtl8180_init_rx_ring()
---
https://github.com/pkshih/rtw.git
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-11-18 2:32 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-14 9:45 [PATCH v2] rtl818x_pci: Fix potential memory leaks in rtl8180_init_rx_ring() Abdun Nihaal
2025-11-17 3:43 ` Ping-Ke Shih
2025-11-18 2:32 ` Ping-Ke Shih
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox