[PATCH net v2] et131x: Add missing check after DMA map

Thomas Fourier posted 1 patch 2 months, 3 weeks ago
drivers/net/ethernet/agere/et131x.c | 36 +++++++++++++++++++++++++++++
1 file changed, 36 insertions(+)
[PATCH net v2] et131x: Add missing check after DMA map
Posted by Thomas Fourier 2 months, 3 weeks ago
The DMA map functions can fail and should be tested for errors.
If the mapping fails, unmap and return an error.

Fixes: 38df6492eb51 ("et131x: Add PCIe gigabit ethernet driver et131x to drivers/net")
Signed-off-by: Thomas Fourier <fourier.thomas@gmail.com>
---
v1 -> v2:
  - Fix subject
  - Fix double decrement of frag
  - Make comment more explicit about why there are two loops

 drivers/net/ethernet/agere/et131x.c | 36 +++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/drivers/net/ethernet/agere/et131x.c b/drivers/net/ethernet/agere/et131x.c
index 678eddb36172..5c8217638dda 100644
--- a/drivers/net/ethernet/agere/et131x.c
+++ b/drivers/net/ethernet/agere/et131x.c
@@ -2459,6 +2459,10 @@ static int nic_send_packet(struct et131x_adapter *adapter, struct tcb *tcb)
 							  skb->data,
 							  skb_headlen(skb),
 							  DMA_TO_DEVICE);
+				if (dma_mapping_error(&adapter->pdev->dev,
+						      dma_addr))
+					return -ENOMEM;
+
 				desc[frag].addr_lo = lower_32_bits(dma_addr);
 				desc[frag].addr_hi = upper_32_bits(dma_addr);
 				frag++;
@@ -2468,6 +2472,10 @@ static int nic_send_packet(struct et131x_adapter *adapter, struct tcb *tcb)
 							  skb->data,
 							  skb_headlen(skb) / 2,
 							  DMA_TO_DEVICE);
+				if (dma_mapping_error(&adapter->pdev->dev,
+						      dma_addr))
+					return -ENOMEM;
+
 				desc[frag].addr_lo = lower_32_bits(dma_addr);
 				desc[frag].addr_hi = upper_32_bits(dma_addr);
 				frag++;
@@ -2478,6 +2486,10 @@ static int nic_send_packet(struct et131x_adapter *adapter, struct tcb *tcb)
 							  skb_headlen(skb) / 2,
 							  skb_headlen(skb) / 2,
 							  DMA_TO_DEVICE);
+				if (dma_mapping_error(&adapter->pdev->dev,
+						      dma_addr))
+					goto unmap_first_out;
+
 				desc[frag].addr_lo = lower_32_bits(dma_addr);
 				desc[frag].addr_hi = upper_32_bits(dma_addr);
 				frag++;
@@ -2489,6 +2501,9 @@ static int nic_send_packet(struct et131x_adapter *adapter, struct tcb *tcb)
 						    0,
 						    desc[frag].len_vlan,
 						    DMA_TO_DEVICE);
+			if (dma_mapping_error(&adapter->pdev->dev, dma_addr))
+				goto unmap_out;
+
 			desc[frag].addr_lo = lower_32_bits(dma_addr);
 			desc[frag].addr_hi = upper_32_bits(dma_addr);
 			frag++;
@@ -2578,6 +2593,27 @@ static int nic_send_packet(struct et131x_adapter *adapter, struct tcb *tcb)
 		       &adapter->regs->global.watchdog_timer);
 	}
 	return 0;
+
+unmap_out:
+	// Unmap the body of the packet with map_page
+	while (--i) {
+		frag--;
+		dma_addr = desc[frag].addr_lo;
+		dma_addr |= (u64)desc[frag].addr_hi << 32;
+		dma_unmap_page(&adapter->pdev->dev, dma_addr,
+			       desc[frag].len_vlan, DMA_TO_DEVICE);
+	}
+
+unmap_first_out:
+	// Unmap the header with map_single
+	while (frag--) {
+		dma_addr = desc[frag].addr_lo;
+		dma_addr |= (u64)desc[frag].addr_hi << 32;
+		dma_unmap_single(&adapter->pdev->dev, dma_addr,
+				 desc[frag].len_vlan, DMA_TO_DEVICE);
+	}
+
+	return -ENOMEM;
 }
 
 static int send_packet(struct sk_buff *skb, struct et131x_adapter *adapter)
-- 
2.43.0
Re: [PATCH net v2] et131x: Add missing check after DMA map
Posted by mark.einon@gmail.com 2 months, 3 weeks ago
On Wed, 2025-07-16 at 11:47 +0200, Thomas Fourier wrote:
> The DMA map functions can fail and should be tested for errors.
> If the mapping fails, unmap and return an error.
> 
> Fixes: 38df6492eb51 ("et131x: Add PCIe gigabit ethernet driver et131x
> to drivers/net")
> Signed-off-by: Thomas Fourier <fourier.thomas@gmail.com>
> ---
> v1 -> v2:
>   - Fix subject
>   - Fix double decrement of frag
>   - Make comment more explicit about why there are two loops

Thanks for the updates Thomas, LGTM (also CC'd Simon who provided the
initial comments).

Acked-by: Mark Einon <mark.einon@gmail.com>
Re: [PATCH net v2] et131x: Add missing check after DMA map
Posted by Simon Horman 2 months, 3 weeks ago
On Wed, Jul 16, 2025 at 12:19:50PM +0100, mark.einon@gmail.com wrote:
> On Wed, 2025-07-16 at 11:47 +0200, Thomas Fourier wrote:
> > The DMA map functions can fail and should be tested for errors.
> > If the mapping fails, unmap and return an error.
> > 
> > Fixes: 38df6492eb51 ("et131x: Add PCIe gigabit ethernet driver et131x
> > to drivers/net")
> > Signed-off-by: Thomas Fourier <fourier.thomas@gmail.com>
> > ---
> > v1 -> v2:
> >   - Fix subject
> >   - Fix double decrement of frag
> >   - Make comment more explicit about why there are two loops
> 
> Thanks for the updates Thomas, LGTM (also CC'd Simon who provided the
> initial comments).
> 
> Acked-by: Mark Einon <mark.einon@gmail.com>

Thanks, also LGTM.

Reviewed-by: Simon Horman <horms@kernel.org>