netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH][linux-next] net: enetc: unmap DMA in enetc_send_cmd()
@ 2021-10-19 18:19 Tim Gardner
  2021-10-19 19:47 ` Claudiu Manoil
  0 siblings, 1 reply; 3+ messages in thread
From: Tim Gardner @ 2021-10-19 18:19 UTC (permalink / raw)
  To: netdev
  Cc: tim.gardner, Claudiu Manoil, David S. Miller, Jakub Kicinski,
	linux-kernel

Coverity complains of a possible dereference of a null return value.

   	5. returned_null: kzalloc returns NULL. [show details]
   	6. var_assigned: Assigning: si_data = NULL return value from kzalloc.
488        si_data = kzalloc(data_size, __GFP_DMA | GFP_KERNEL);
489        cbd.length = cpu_to_le16(data_size);
490
491        dma = dma_map_single(&priv->si->pdev->dev, si_data,
492                             data_size, DMA_FROM_DEVICE);

While this kzalloc() is unlikely to fail, I did notice that the function
returned without unmapping si_data.

Fix this by refactoring the error paths and checking for kzalloc()
failure.

Fixes: 888ae5a3952ba ("net: enetc: add tc flower psfp offload driver")
Cc: Claudiu Manoil <claudiu.manoil@nxp.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Jakub Kicinski <kuba@kernel.org>
Cc: netdev@vger.kernel.org
Cc: linux-kernel@vger.kernel.org (open list)
Signed-off-by: Tim Gardner <tim.gardner@canonical.com>
---

I am curious why you do not need to call dma_sync_single_for_device() before enetc_send_cmd()
in order to flush the contents of CPU cache to RAM. Is it because __GFP_DMA marks
that page as uncached ? Or is it because of the SOC this runs on ?

rtg
---
 .../net/ethernet/freescale/enetc/enetc_qos.c  | 22 +++++++++++--------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/drivers/net/ethernet/freescale/enetc/enetc_qos.c b/drivers/net/ethernet/freescale/enetc/enetc_qos.c
index 4577226d3c6a..a93c55b04287 100644
--- a/drivers/net/ethernet/freescale/enetc/enetc_qos.c
+++ b/drivers/net/ethernet/freescale/enetc/enetc_qos.c
@@ -486,14 +486,16 @@ static int enetc_streamid_hw_set(struct enetc_ndev_priv *priv,
 
 	data_size = sizeof(struct streamid_data);
 	si_data = kzalloc(data_size, __GFP_DMA | GFP_KERNEL);
-	cbd.length = cpu_to_le16(data_size);
+	if (!si_data)
+		return -ENOMEM;
+	cbd.length = cpu_to_le16(data_size);
 
 	dma = dma_map_single(&priv->si->pdev->dev, si_data,
 			     data_size, DMA_FROM_DEVICE);
 	if (dma_mapping_error(&priv->si->pdev->dev, dma)) {
 		netdev_err(priv->si->ndev, "DMA mapping failed!\n");
-		kfree(si_data);
-		return -ENOMEM;
+		err = -ENOMEM;
+		goto out;
 	}
 
 	cbd.addr[0] = cpu_to_le32(lower_32_bits(dma));
@@ -512,12 +514,10 @@ static int enetc_streamid_hw_set(struct enetc_ndev_priv *priv,
 
 	err = enetc_send_cmd(priv->si, &cbd);
 	if (err)
-		return -EINVAL;
+		goto out;
 
-	if (!enable) {
-		kfree(si_data);
-		return 0;
-	}
+	if (!enable)
+		goto out;
 
 	/* Enable the entry overwrite again incase space flushed by hardware */
 	memset(&cbd, 0, sizeof(cbd));
@@ -560,7 +560,11 @@ static int enetc_streamid_hw_set(struct enetc_ndev_priv *priv,
 	}
 
 	err = enetc_send_cmd(priv->si, &cbd);
-	kfree(si_data);
+out:
+	if (!dma_mapping_error(&priv->si->pdev->dev, dma))
+		dma_unmap_single(&priv->si->pdev->dev, dma, data_size, DMA_FROM_DEVICE);
+
+	kfree(si_data);
 
 	return err;
 }
-- 
2.33.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* RE: [PATCH][linux-next] net: enetc: unmap DMA in enetc_send_cmd()
  2021-10-19 18:19 [PATCH][linux-next] net: enetc: unmap DMA in enetc_send_cmd() Tim Gardner
@ 2021-10-19 19:47 ` Claudiu Manoil
  2021-10-20 22:23   ` Jakub Kicinski
  0 siblings, 1 reply; 3+ messages in thread
From: Claudiu Manoil @ 2021-10-19 19:47 UTC (permalink / raw)
  To: Tim Gardner, netdev
  Cc: David S. Miller, Jakub Kicinski, linux-kernel, Po Liu, Claudiu Manoil

> -----Original Message-----
> From: Tim Gardner <tim.gardner@canonical.com>
> Sent: Tuesday, October 19, 2021 9:20 PM
[...]
> Subject: [PATCH][linux-next] net: enetc: unmap DMA in enetc_send_cmd()
> 
> Coverity complains of a possible dereference of a null return value.
> 
>    	5. returned_null: kzalloc returns NULL. [show details]
>    	6. var_assigned: Assigning: si_data = NULL return value from kzalloc.
> 488        si_data = kzalloc(data_size, __GFP_DMA | GFP_KERNEL);
> 489        cbd.length = cpu_to_le16(data_size);
> 490
> 491        dma = dma_map_single(&priv->si->pdev->dev, si_data,
> 492                             data_size, DMA_FROM_DEVICE);
> 
> While this kzalloc() is unlikely to fail, I did notice that the function
> returned without unmapping si_data.
> 
> Fix this by refactoring the error paths and checking for kzalloc()
> failure.
> 
> Fixes: 888ae5a3952ba ("net: enetc: add tc flower psfp offload driver")
> Cc: Claudiu Manoil <claudiu.manoil@nxp.com>
> Cc: "David S. Miller" <davem@davemloft.net>
> Cc: Jakub Kicinski <kuba@kernel.org>
> Cc: netdev@vger.kernel.org
> Cc: linux-kernel@vger.kernel.org (open list)
> Signed-off-by: Tim Gardner <tim.gardner@canonical.com>
> ---
> 
> I am curious why you do not need to call dma_sync_single_for_device()
> before enetc_send_cmd()
> in order to flush the contents of CPU cache to RAM. Is it because
> __GFP_DMA marks
> that page as uncached ? Or is it because of the SOC this runs on ?
> 

Not sure how it works like this, but I think dma_alloc_coherent() should have
been used here instead of the kmalloc + dma_map scheme. I don't have the
means to test this particular code path however.

Acked-by: Claudiu Manoil <claudiu.manoil@nxp.com>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH][linux-next] net: enetc: unmap DMA in enetc_send_cmd()
  2021-10-19 19:47 ` Claudiu Manoil
@ 2021-10-20 22:23   ` Jakub Kicinski
  0 siblings, 0 replies; 3+ messages in thread
From: Jakub Kicinski @ 2021-10-20 22:23 UTC (permalink / raw)
  To: Claudiu Manoil, Tim Gardner; +Cc: netdev, David S. Miller, linux-kernel, Po Liu

On Tue, 19 Oct 2021 19:47:17 +0000 Claudiu Manoil wrote:
> > I am curious why you do not need to call dma_sync_single_for_device()
> > before enetc_send_cmd()
> > in order to flush the contents of CPU cache to RAM. Is it because
> > __GFP_DMA marks
> > that page as uncached ? Or is it because of the SOC this runs on ?
> >   
> 
> Not sure how it works like this, but I think dma_alloc_coherent() should have
> been used here instead of the kmalloc + dma_map scheme.

Using dma_alloc_coherent() seems simpler and more correct, 
let's do that instead.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-10-20 22:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-19 18:19 [PATCH][linux-next] net: enetc: unmap DMA in enetc_send_cmd() Tim Gardner
2021-10-19 19:47 ` Claudiu Manoil
2021-10-20 22:23   ` Jakub Kicinski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).