Skip to contentSkip to conversation

net/ps3_gelic_net: DMA related fixes

PATCH Superseded Geoff Levand 2 participants 9 messages Feb 5, 2023 – Feb 13, 2023

Subsystems (4)

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Geoff Levand

author maintainer
#0 lore raw

v4 cover net/ps3_gelic_net: DMA related fixes

v4: More patch cleanups.
v3: Cleaned up patches as requested.

Geoff Levand (2):
net/ps3_gelic_net: Fix RX sk_buff length
net/ps3_gelic_net: Use dma_mapping_error

 drivers/net/ethernet/toshiba/ps3_gelic_net.c | 102 +++++++++++--------
 1 file changed, 58 insertions(+), 44 deletions(-)
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v4 1/2 net/ps3_gelic_net: Fix RX sk_buff length

The Gelic Ethernet device needs to have the RX sk_buffs aligned to GELIC_NET_RXBUF_ALIGN and the length of the RX sk_buffs must be a multiple of GELIC_NET_RXBUF_ALIGN.

The current Gelic Ethernet driver was not allocating sk_buffs large enough to allow for this alignment.

Fixes various randomly occurring runtime network errors.

Fixes 02c1889166b4 (ps3: gigabit ethernet driver for PS3, take3)
---
 drivers/net/ethernet/toshiba/ps3_gelic_net.c | 56 ++++++++++++--------
 1 file changed, 34 insertions(+), 22 deletions(-)
drivers/net/ethernet/toshiba/ps3_gelic_net.c +34 -22
index cf8de8a7a8a1..7a8b5e1e77a6 100644
@@ -365,51 +365,63 @@ static int gelic_card_init_chain(struct gelic_card *card,
  *
  * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
  * Activate the descriptor state-wise
+ *
+ * Gelic RX sk_buffs must be aligned to GELIC_NET_RXBUF_ALIGN and the length
+ * must be a multiple of GELIC_NET_RXBUF_ALIGN.
  */
 static int gelic_descr_prepare_rx(struct gelic_card *card,
 				  struct gelic_descr *descr)
 {
-	int offset;
-	unsigned int bufsize;
+	struct device *dev = ctodev(card);
+	struct {
+		unsigned int total_bytes;
+		unsigned int offset;
+	} aligned_buf;
+	dma_addr_t cpu_addr;
 
 	if (gelic_descr_get_status(descr) !=  GELIC_DESCR_DMA_NOT_IN_USE)
 		dev_info(ctodev(card), "%s: ERROR status\n", __func__);
-	/* we need to round up the buffer size to a multiple of 128 */
-	bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);
 
-	/* and we need to have it 128 byte aligned, therefore we allocate a
-	 * bit more */
-	descr->skb = dev_alloc_skb(bufsize + GELIC_NET_RXBUF_ALIGN - 1);
+	aligned_buf.total_bytes = (GELIC_NET_RXBUF_ALIGN - 1) +
+		GELIC_NET_MAX_MTU + (GELIC_NET_RXBUF_ALIGN - 1);
+
+	descr->skb = dev_alloc_skb(aligned_buf.total_bytes);
+
 	if (!descr->skb) {
-		descr->buf_addr = 0; /* tell DMAC don't touch memory */
+		descr->buf_addr = 0;
 		return -ENOMEM;
 	}
-	descr->buf_size = cpu_to_be32(bufsize);
+
+	aligned_buf.offset =
+		PTR_ALIGN(descr->skb->data, GELIC_NET_RXBUF_ALIGN) -
+			descr->skb->data;
+
+	descr->buf_size = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);
 	descr->dmac_cmd_status = 0;
 	descr->result_size = 0;
 	descr->valid_size = 0;
 	descr->data_error = 0;
 
-	offset = ((unsigned long)descr->skb->data) &
-		(GELIC_NET_RXBUF_ALIGN - 1);
-	if (offset)
-		skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset);
-	/* io-mmu-map the skb */
-	descr->buf_addr = cpu_to_be32(dma_map_single(ctodev(card),
-						     descr->skb->data,
-						     GELIC_NET_MAX_MTU,
-						     DMA_FROM_DEVICE));
+	skb_reserve(descr->skb, aligned_buf.offset);
+
+	cpu_addr = dma_map_single(dev, descr->skb->data, descr->buf_size,
+		DMA_FROM_DEVICE);
+
+	descr->buf_addr = cpu_to_be32(cpu_addr);
+
 	if (!descr->buf_addr) {
 		dev_kfree_skb_any(descr->skb);
+		descr->buf_addr = 0;
+		descr->buf_size = 0;
 		descr->skb = NULL;
-		dev_info(ctodev(card),
+		dev_info(dev,
 			 "%s:Could not iommu-map rx buffer\n", __func__);
 		gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
 		return -ENOMEM;
-	} else {
-		gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
-		return 0;
 	}
+
+	gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
+	return 0;
 }
 
 /**
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH net v4 1/2] net/ps3_gelic_net: Fix RX sk_buff length

Quoted 40 lines · Geoff Levand

The Gelic Ethernet device needs to have the RX sk_buffs aligned to GELIC_NET_RXBUF_ALIGN and the length of the RX sk_buffs must be a multiple of GELIC_NET_RXBUF_ALIGN.

The current Gelic Ethernet driver was not allocating sk_buffs large enough to allow for this alignment.

Fixes various randomly occurring runtime network errors.

Fixes 02c1889166b4 (ps3: gigabit ethernet driver for PS3, take3)
---
 drivers/net/ethernet/toshiba/ps3_gelic_net.c | 56 ++++++++++++--------
 1 file changed, 34 insertions(+), 22 deletions(-)
drivers/net/ethernet/toshiba/ps3_gelic_net.c +9 -2
index cf8de8a7a8a1..7a8b5e1e77a6 100644
@@ -365,51 +365,63 @@ static int gelic_card_init_chain(struct gelic_card *card,
  *
  * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
  * Activate the descriptor state-wise
+ *
+ * Gelic RX sk_buffs must be aligned to GELIC_NET_RXBUF_ALIGN and the length
+ * must be a multiple of GELIC_NET_RXBUF_ALIGN.
  */
 static int gelic_descr_prepare_rx(struct gelic_card *card,
 				  struct gelic_descr *descr)
 {
-	int offset;
-	unsigned int bufsize;
+	struct device *dev = ctodev(card);
+	struct {
+		unsigned int total_bytes;
+		unsigned int offset;
+	} aligned_buf;
+	dma_addr_t cpu_addr;
 
 	if (gelic_descr_get_status(descr) !=  GELIC_DESCR_DMA_NOT_IN_USE)

drivers/net/ethernet/toshiba/ps3_gelic_net.c static int gelic_card_init_chain(struct gelic_card *card, · line 387

Quoted 10 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +3 -5
 		dev_info(ctodev(card), "%s: ERROR status\n", __func__);
-	/* we need to round up the buffer size to a multiple of 128 */
-	bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);
 
-	/* and we need to have it 128 byte aligned, therefore we allocate a
-	 * bit more */
-	descr->skb = dev_alloc_skb(bufsize + GELIC_NET_RXBUF_ALIGN - 1);
+	aligned_buf.total_bytes = (GELIC_NET_RXBUF_ALIGN - 1) +
+		GELIC_NET_MAX_MTU + (GELIC_NET_RXBUF_ALIGN - 1);
+

This value isn't aligned to anything as there have been no steps taken to align it. In fact it is guaranteed to be off by 2. Did you maybe mean to use an "&" somewhere?

drivers/net/ethernet/toshiba/ps3_gelic_net.c static int gelic_card_init_chain(struct gelic_card *card, · line 392

Quoted 6 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +3 -1
+	descr->skb = dev_alloc_skb(aligned_buf.total_bytes);
+
 	if (!descr->skb) {
-		descr->buf_addr = 0; /* tell DMAC don't touch memory */
+		descr->buf_addr = 0;
 		return -ENOMEM;

Why remove this comment?

drivers/net/ethernet/toshiba/ps3_gelic_net.c static int gelic_card_init_chain(struct gelic_card *card, · line 399

Quoted 8 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +6 -1
 	}
-	descr->buf_size = cpu_to_be32(bufsize);
+
+	aligned_buf.offset =
+		PTR_ALIGN(descr->skb->data, GELIC_NET_RXBUF_ALIGN) -
+			descr->skb->data;
+
+	descr->buf_size = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);

Originally this was being written using cpu_to_be32. WIth this you are writing it raw w/ the cpu endianness. Is there a byte ordering issue here?

drivers/net/ethernet/toshiba/ps3_gelic_net.c static int gelic_card_init_chain(struct gelic_card *card, · line 396

Quoted 9 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +0 -4
 	descr->dmac_cmd_status = 0;
 	descr->result_size = 0;
 	descr->valid_size = 0;
 	descr->data_error = 0;
 
-	offset = ((unsigned long)descr->skb->data) &
-		(GELIC_NET_RXBUF_ALIGN - 1);
-	if (offset)
-		skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset);

Rather than messing with all this it might be easier to just drop offset in favor of NET_SKB_PAD since that should be offset in all cases where dev_alloc_skb is being used. With that the reserve could just be a constant.

drivers/net/ethernet/toshiba/ps3_gelic_net.c static int gelic_card_init_chain(struct gelic_card *card, · line 412

Quoted 13 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +7 -5
-	/* io-mmu-map the skb */
-	descr->buf_addr = cpu_to_be32(dma_map_single(ctodev(card),
-						     descr->skb->data,
-						     GELIC_NET_MAX_MTU,
-						     DMA_FROM_DEVICE));
+	skb_reserve(descr->skb, aligned_buf.offset);
+
+	cpu_addr = dma_map_single(dev, descr->skb->data, descr->buf_size,
+		DMA_FROM_DEVICE);
+
+	descr->buf_addr = cpu_to_be32(cpu_addr);
+
 	if (!descr->buf_addr) {

This check should be for dma_mapping_error based on "cpu_addr". There are some configs that don't return NULL to indicate a mapping error.

Quoted 19 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +6 -4
 		dev_kfree_skb_any(descr->skb);
+		descr->buf_addr = 0;
+		descr->buf_size = 0;
 		descr->skb = NULL;
-		dev_info(ctodev(card),
+		dev_info(dev,
 			 "%s:Could not iommu-map rx buffer\n", __func__);
 		gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
 		return -ENOMEM;
-	} else {
-		gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
-		return 0;
 	}
+
+	gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
+	return 0;
 }
 
 /**
author maintainer
#3 lore raw

Re: [PATCH net v4 1/2] net/ps3_gelic_net: Fix RX sk_buff length

Hi Alexander,

Thanks for the review.

Quoted 23 lines · Alexander H Duyck
Quoted 22 lines · Geoff Levand

The Gelic Ethernet device needs to have the RX sk_buffs aligned to GELIC_NET_RXBUF_ALIGN and the length of the RX sk_buffs must be a multiple of GELIC_NET_RXBUF_ALIGN.

static int gelic_descr_prepare_rx(struct gelic_card *card,

drivers/net/ethernet/toshiba/ps3_gelic_net.c +6 -4
 				  struct gelic_descr *descr)
 {
-	int offset;
-	unsigned int bufsize;
+	struct device *dev = ctodev(card);
+	struct {
+		unsigned int total_bytes;
+		unsigned int offset;
+	} aligned_buf;
+	dma_addr_t cpu_addr;
 
 	if (gelic_descr_get_status(descr) !=  GELIC_DESCR_DMA_NOT_IN_USE)
 		dev_info(ctodev(card), "%s: ERROR status\n", __func__);
-	/* we need to round up the buffer size to a multiple of 128 */
-	bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);
Quoted 10 lines

drivers/net/ethernet/toshiba/ps3_gelic_net.c static int gelic_card_init_chain(struct gelic_card *card, · line 387

Quoted 6 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +3 -3
-	/* and we need to have it 128 byte aligned, therefore we allocate a
-	 * bit more */
-	descr->skb = dev_alloc_skb(bufsize + GELIC_NET_RXBUF_ALIGN - 1);
+	aligned_buf.total_bytes = (GELIC_NET_RXBUF_ALIGN - 1) +
+		GELIC_NET_MAX_MTU + (GELIC_NET_RXBUF_ALIGN - 1);
+

This value isn't aligned to anything as there have been no steps taken to align it. In fact it is guaranteed to be off by 2. Did you maybe mean to use an "&" somewhere?

total_bytes here means the total number of bytes to allocate that will allow for the desired alignment. This value a bit too much though since we really just need it to end on a GELIC_NET_RXBUF_ALIGN boundary, so adding ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN) should be enough. I'll fix that in the next patch version.

Quoted 8 lines

drivers/net/ethernet/toshiba/ps3_gelic_net.c static int gelic_card_init_chain(struct gelic_card *card, · line 392

Quoted 6 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +3 -1
+	descr->skb = dev_alloc_skb(aligned_buf.total_bytes);
+
 	if (!descr->skb) {
-		descr->buf_addr = 0; /* tell DMAC don't touch memory */
+		descr->buf_addr = 0;
 		return -ENOMEM;

Why remove this comment?

If we return -ENOMEM this descriptor shouldn't be used.

Quoted 4 lines
Quoted 4 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +2 -1
 	}
-	descr->buf_size = cpu_to_be32(bufsize);
+
+	aligned_buf.offset =
Quoted 8 lines

drivers/net/ethernet/toshiba/ps3_gelic_net.c static int gelic_card_init_chain(struct gelic_card *card, · line 399

Quoted 4 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +4 -0
+		PTR_ALIGN(descr->skb->data, GELIC_NET_RXBUF_ALIGN) -
+			descr->skb->data;
+
+	descr->buf_size = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);

Originally this was being written using cpu_to_be32. WIth this you are writing it raw w/ the cpu endianness. Is there a byte ordering issue here?

No. The PS3 has a big endian CPU, so we really don't need any of the endian conversions.

Quoted 6 lines
Quoted 6 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +0 -0
 	descr->dmac_cmd_status = 0;
 	descr->result_size = 0;
 	descr->valid_size = 0;
 	descr->data_error = 0;
Quoted 9 lines

drivers/net/ethernet/toshiba/ps3_gelic_net.c static int gelic_card_init_chain(struct gelic_card *card, · line 396

Quoted 4 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +0 -4
-	offset = ((unsigned long)descr->skb->data) &
-		(GELIC_NET_RXBUF_ALIGN - 1);
-	if (offset)
-		skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset);

Rather than messing with all this it might be easier to just drop offset in favor of NET_SKB_PAD since that should be offset in all cases where dev_alloc_skb is being used. With that the reserve could just be a constant.

GELIC_NET_RXBUF_ALIGN is a property of the gelic hardware device. I would think if NET_SKB_PAD would work it would just be by coincidence.

Quoted 8 lines
Quoted 8 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +3 -5
-	/* io-mmu-map the skb */
-	descr->buf_addr = cpu_to_be32(dma_map_single(ctodev(card),
-						     descr->skb->data,
-						     GELIC_NET_MAX_MTU,
-						     DMA_FROM_DEVICE));
+	skb_reserve(descr->skb, aligned_buf.offset);
+
+	cpu_addr = dma_map_single(dev, descr->skb->data, descr->buf_size,
Quoted 8 lines

drivers/net/ethernet/toshiba/ps3_gelic_net.c static int gelic_card_init_chain(struct gelic_card *card, · line 412

Quoted 5 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +4 -0
+		DMA_FROM_DEVICE);
+
+	descr->buf_addr = cpu_to_be32(cpu_addr);
+
 	if (!descr->buf_addr) {

This check should be for dma_mapping_error based on "cpu_addr". There are some configs that don't return NULL to indicate a mapping error.

As was requested, I have put those corrections into the second patch of this series.

-Geoff

#4 lore raw

Re: [PATCH net v4 1/2] net/ps3_gelic_net: Fix RX sk_buff length

Quoted 53 lines · Geoff Levand

Hi Alexander,

Thanks for the review.

Quoted 30 lines · Alexander H Duyck
Quoted 18 lines · Geoff Levand

The Gelic Ethernet device needs to have the RX sk_buffs aligned to GELIC_NET_RXBUF_ALIGN and the length of the RX sk_buffs must be a multiple of GELIC_NET_RXBUF_ALIGN.

static int gelic_descr_prepare_rx(struct gelic_card *card,
struct gelic_descr *descr)
{

drivers/net/ethernet/toshiba/ps3_gelic_net.c +6 -2
-    int offset;
-    unsigned int bufsize;
+    struct device *dev = ctodev(card);
+    struct {
+            unsigned int total_bytes;
+            unsigned int offset;
+    } aligned_buf;
+    dma_addr_t cpu_addr;
 
     if (gelic_descr_get_status(descr) !=  GELIC_DESCR_DMA_NOT_IN_USE)

drivers/net/ethernet/toshiba/ps3_gelic_net.c static int gelic_card_init_chain(struct gelic_card *card, · line 387

Quoted 10 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +3 -5
             dev_info(ctodev(card), "%s: ERROR status\n", __func__);
-    /* we need to round up the buffer size to a multiple of 128 */
-    bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);
 
-    /* and we need to have it 128 byte aligned, therefore we allocate a
-     * bit more */
-    descr->skb = dev_alloc_skb(bufsize + GELIC_NET_RXBUF_ALIGN - 1);
+    aligned_buf.total_bytes = (GELIC_NET_RXBUF_ALIGN - 1) +
+            GELIC_NET_MAX_MTU + (GELIC_NET_RXBUF_ALIGN - 1);
+
Quoted 3 lines

This value isn't aligned to anything as there have been no steps taken to align it. In fact it is guaranteed to be off by 2. Did you maybe mean to use an "&" somewhere?

total_bytes here means the total number of bytes to allocate that will allow for the desired alignment. This value a bit too much though since we really just need it to end on a GELIC_NET_RXBUF_ALIGN boundary, so adding ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN) should be enough. I'll fix that in the next patch version.

Quoted 7 lines
Quoted 6 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +3 -1
+    descr->skb = dev_alloc_skb(aligned_buf.total_bytes);
+
     if (!descr->skb) {
-            descr->buf_addr = 0; /* tell DMAC don't touch memory */
+            descr->buf_addr = 0;
             return -ENOMEM;
Quoted 3 lines
Quoted 1 line

Why remove this comment?

If we return -ENOMEM this descriptor shouldn't be used.

Right. But the comment is essentially saying that. The question is why remove the documentation?

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
     }
drivers/net/ethernet/toshiba/ps3_gelic_net.c +2 -1
-    descr->buf_size = cpu_to_be32(bufsize);
+
+    aligned_buf.offset =
Quoted 8 lines

drivers/net/ethernet/toshiba/ps3_gelic_net.c static int gelic_card_init_chain(struct gelic_card *card, · line 399

Quoted 4 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +4 -0
+            PTR_ALIGN(descr->skb->data, GELIC_NET_RXBUF_ALIGN) -
+                    descr->skb->data;
+
+    descr->buf_size = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);

Originally this was being written using cpu_to_be32. WIth this you are writing it raw w/ the cpu endianness. Is there a byte ordering issue here?

Quoted 2 lines

No. The PS3 has a big endian CPU, so we really don't need any of the endian conversions.

This doesn't introduce an ordering error, but it introduces a type error. The bufsize variable was defined as a CPU ordered variable whereas buf_size is a __be32. If you enable sparse checking it should return an error.

I would recommend keeping the cpu_to_be32. If your architecture is big endian it will do nothing and add no overhead. If at some point in the future someone were to plug this IP into another architecture it would take care of sorting out the byte ordering for you.

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
     descr->dmac_cmd_status = 0;
     descr->result_size = 0;
     descr->valid_size = 0;
     descr->data_error = 0;
drivers/net/ethernet/toshiba/ps3_gelic_net.c +0 -4
-    offset = ((unsigned long)descr->skb->data) &
-            (GELIC_NET_RXBUF_ALIGN - 1);
-    if (offset)
-            skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset);
Quoted 7 lines
Quoted 4 lines

Rather than messing with all this it might be easier to just drop offset in favor of NET_SKB_PAD since that should be offset in all cases where dev_alloc_skb is being used. With that the reserve could just be a constant.

GELIC_NET_RXBUF_ALIGN is a property of the gelic hardware device. I would think if NET_SKB_PAD would work it would just be by coincidence.

NET_SKB_PAD is the alignment generated when you use dev_alloc_skb. What I was getting at is that "offset" could probably be removed in favor of just using NET_SKB_PAD.

author maintainer
#5 lore raw

PATCH v4 2/2 net/ps3_gelic_net: Use dma_mapping_error

In reply to Geoff Levand

The current Gelic Etherenet driver was checking the return value of its dma_map_single call, and not using the dma_mapping_error() routine.

Fixes runtime problems like these:

DMA-API: ps3_gelic_driver sb_05: device driver failed to check map error

  WARNING: CPU: 0 PID: 0 at kernel/dma/debug.c:1027 .check_unmap+0x888/0x8dc
Fixes 02c1889166b4 (ps3: gigabit ethernet driver for PS3, take3)
---
 drivers/net/ethernet/toshiba/ps3_gelic_net.c | 52 ++++++++++----------
 1 file changed, 27 insertions(+), 25 deletions(-)
drivers/net/ethernet/toshiba/ps3_gelic_net.c +27 -25
index 7a8b5e1e77a6..5622b512e2e4 100644
@@ -309,22 +309,34 @@ static int gelic_card_init_chain(struct gelic_card *card,
 				 struct gelic_descr_chain *chain,
 				 struct gelic_descr *start_descr, int no)
 {
-	int i;
+	struct device *dev = ctodev(card);
 	struct gelic_descr *descr;
+	int i;
 
-	descr = start_descr;
-	memset(descr, 0, sizeof(*descr) * no);
+	memset(start_descr, 0, no * sizeof(*start_descr));
 
 	/* set up the hardware pointers in each descriptor */
-	for (i = 0; i < no; i++, descr++) {
+	for (i = 0, descr = start_descr; i < no; i++, descr++) {
 		gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
 		descr->bus_addr =
 			dma_map_single(ctodev(card), descr,
 				       GELIC_DESCR_SIZE,
 				       DMA_BIDIRECTIONAL);
 
-		if (!descr->bus_addr)
-			goto iommu_error;
+		if (unlikely(dma_mapping_error(dev, descr->bus_addr))) {
+			dev_err(dev, "%s:%d: dma_mapping_error\n", __func__,
+				__LINE__);
+
+			for (i--, descr--; i > 0; i--, descr--) {
+				if (descr->bus_addr) {
+					dma_unmap_single(ctodev(card),
+						descr->bus_addr,
+						GELIC_DESCR_SIZE,
+						DMA_BIDIRECTIONAL);
+				}
+			}
+			return -ENOMEM;
+		}
 
 		descr->next = descr + 1;
 		descr->prev = descr - 1;
@@ -334,8 +346,7 @@ static int gelic_card_init_chain(struct gelic_card *card,
 	start_descr->prev = (descr - 1);
 
 	/* chain bus addr of hw descriptor */
-	descr = start_descr;
-	for (i = 0; i < no; i++, descr++) {
+	for (i = 0, descr = start_descr; i < no; i++, descr++) {
 		descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
 	}
 
@@ -346,14 +357,6 @@ static int gelic_card_init_chain(struct gelic_card *card,
 	(descr - 1)->next_descr_addr = 0;
 
 	return 0;
-
-iommu_error:
-	for (i--, descr--; 0 <= i; i--, descr--)
-		if (descr->bus_addr)
-			dma_unmap_single(ctodev(card), descr->bus_addr,
-					 GELIC_DESCR_SIZE,
-					 DMA_BIDIRECTIONAL);
-	return -ENOMEM;
 }
 
 /**
@@ -407,19 +410,18 @@ static int gelic_descr_prepare_rx(struct gelic_card *card,
 	cpu_addr = dma_map_single(dev, descr->skb->data, descr->buf_size,
 		DMA_FROM_DEVICE);
 
-	descr->buf_addr = cpu_to_be32(cpu_addr);
-
-	if (!descr->buf_addr) {
+	if (unlikely(dma_mapping_error(dev, cpu_addr))) {
+		dev_err(dev, "%s:%d: dma_mapping_error\n", __func__, __LINE__);
 		dev_kfree_skb_any(descr->skb);
 		descr->buf_addr = 0;
 		descr->buf_size = 0;
 		descr->skb = NULL;
-		dev_info(dev,
-			 "%s:Could not iommu-map rx buffer\n", __func__);
 		gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
 		return -ENOMEM;
 	}
 
+	descr->buf_addr = cpu_to_be32(cpu_addr);
+
 	gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
 	return 0;
 }
@@ -775,6 +777,7 @@ static int gelic_descr_prepare_tx(struct gelic_card *card,
 				  struct gelic_descr *descr,
 				  struct sk_buff *skb)
 {
+	struct device *dev = ctodev(card);
 	dma_addr_t buf;
 
 	if (card->vlan_required) {
@@ -789,11 +792,10 @@ static int gelic_descr_prepare_tx(struct gelic_card *card,
 		skb = skb_tmp;
 	}
 
-	buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE);
+	buf = dma_map_single(dev, skb->data, skb->len, DMA_TO_DEVICE);
 
-	if (!buf) {
-		dev_err(ctodev(card),
-			"dma map 2 failed (%p, %i). Dropping packet\n",
+	if (unlikely(dma_mapping_error(dev, buf))) {
+		dev_err(dev, "dma map 2 failed (%p, %i). Dropping packet\n",
 			skb->data, skb->len);
 		return -ENOMEM;
 	}
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH net v4 2/2] net/ps3_gelic_net: Use dma_mapping_error

Quoted 29 lines · Geoff Levand

The current Gelic Etherenet driver was checking the return value of its dma_map_single call, and not using the dma_mapping_error() routine.

Fixes runtime problems like these:

DMA-API: ps3_gelic_driver sb_05: device driver failed to check map error

  WARNING: CPU: 0 PID: 0 at kernel/dma/debug.c:1027 .check_unmap+0x888/0x8dc
Fixes 02c1889166b4 (ps3: gigabit ethernet driver for PS3, take3)
---
 drivers/net/ethernet/toshiba/ps3_gelic_net.c | 52 ++++++++++----------
 1 file changed, 27 insertions(+), 25 deletions(-)
drivers/net/ethernet/toshiba/ps3_gelic_net.c +2 -3
index 7a8b5e1e77a6..5622b512e2e4 100644
@@ -309,22 +309,34 @@ static int gelic_card_init_chain(struct gelic_card *card,
 				 struct gelic_descr_chain *chain,
 				 struct gelic_descr *start_descr, int no)
 {
-	int i;
+	struct device *dev = ctodev(card);
 	struct gelic_descr *descr;
+	int i;
 
-	descr = start_descr;
-	memset(descr, 0, sizeof(*descr) * no);

drivers/net/ethernet/toshiba/ps3_gelic_net.c static int gelic_card_init_chain(struct gelic_card *card, · line 324

Quoted 10 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +2 -1
+	memset(start_descr, 0, no * sizeof(*start_descr));
 
 	/* set up the hardware pointers in each descriptor */
-	for (i = 0; i < no; i++, descr++) {
+	for (i = 0, descr = start_descr; i < no; i++, descr++) {
 		gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
 		descr->bus_addr =
 			dma_map_single(ctodev(card), descr,
 				       GELIC_DESCR_SIZE,
 				       DMA_BIDIRECTIONAL);

Are bus_addr and the CPU the same byte ordering? Just wondering since this is being passed raw. I would have expected it to go through a cpu_to_be32.

drivers/net/ethernet/toshiba/ps3_gelic_net.c static int gelic_card_init_chain(struct gelic_card *card, · line 326

Quoted 4 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +1 -2
-		if (!descr->bus_addr)
-			goto iommu_error;
+		if (unlikely(dma_mapping_error(dev, descr->bus_addr))) {

The expectation for dma_mapping_error is that the address is in cpu order. So in this case it is partially correct since bus_addr wasn't byte swapped, but generally the dma address used should be a CPU byte ordered variable.

drivers/net/ethernet/toshiba/ps3_gelic_net.c static int gelic_card_init_chain(struct gelic_card *card, · line 331

Quoted 5 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +5 -0
+			dev_err(dev, "%s:%d: dma_mapping_error\n", __func__,
+				__LINE__);
+
+			for (i--, descr--; i > 0; i--, descr--) {
+				if (descr->bus_addr) {

So I am pretty sure this is broken. Usually for something like this I will resort to just doing a while (i--) as "i == 0" should be a valid buffer to have to unmap.

Maybe something like:

			while (i--) {
				descr--;

Also I think you can get rid of the if since descr->bus_addr should be valid for all values since you populated it just a few lines above for each value of i.

Quoted 12 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +8 -0
+					dma_unmap_single(ctodev(card),
+						descr->bus_addr,
+						GELIC_DESCR_SIZE,
+						DMA_BIDIRECTIONAL);
+				}
+			}
+			return -ENOMEM;
+		}
 
 		descr->next = descr + 1;
 		descr->prev = descr - 1;

drivers/net/ethernet/toshiba/ps3_gelic_net.c static int gelic_card_init_chain(struct gelic_card *card, · line 352

Quoted 9 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +1 -2
@@ -334,8 +346,7 @@ static int gelic_card_init_chain(struct gelic_card *card,
 	start_descr->prev = (descr - 1);
 
 	/* chain bus addr of hw descriptor */
-	descr = start_descr;
-	for (i = 0; i < no; i++, descr++) {
+	for (i = 0, descr = start_descr; i < no; i++, descr++) {
 		descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
 	}
 

This seems like an unrelated change that was snuck in.

Quoted 26 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +2 -11
@@ -346,14 +357,6 @@ static int gelic_card_init_chain(struct gelic_card *card,
 	(descr - 1)->next_descr_addr = 0;
 
 	return 0;
-
-iommu_error:
-	for (i--, descr--; 0 <= i; i--, descr--)
-		if (descr->bus_addr)
-			dma_unmap_single(ctodev(card), descr->bus_addr,
-					 GELIC_DESCR_SIZE,
-					 DMA_BIDIRECTIONAL);
-	return -ENOMEM;
 }
 
 /**
@@ -407,19 +410,18 @@ static int gelic_descr_prepare_rx(struct gelic_card *card,
 	cpu_addr = dma_map_single(dev, descr->skb->data, descr->buf_size,
 		DMA_FROM_DEVICE);
 
-	descr->buf_addr = cpu_to_be32(cpu_addr);
-
-	if (!descr->buf_addr) {
+	if (unlikely(dma_mapping_error(dev, cpu_addr))) {
+		dev_err(dev, "%s:%d: dma_mapping_error\n", __func__, __LINE__);
 		dev_kfree_skb_any(descr->skb);
 		descr->buf_addr = 0;

drivers/net/ethernet/toshiba/ps3_gelic_net.c static int gelic_descr_prepare_rx(struct gelic_card *card, · line 424

Quoted 10 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +2 -2
 		descr->buf_size = 0;
 		descr->skb = NULL;
-		dev_info(dev,
-			 "%s:Could not iommu-map rx buffer\n", __func__);
 		gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
 		return -ENOMEM;
 	}
 
+	descr->buf_addr = cpu_to_be32(cpu_addr);
+

Okay, so this addresses the comment I had in the earlier patch.

Quoted 26 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +4 -4
 	gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
 	return 0;
 }
@@ -775,6 +777,7 @@ static int gelic_descr_prepare_tx(struct gelic_card *card,
 				  struct gelic_descr *descr,
 				  struct sk_buff *skb)
 {
+	struct device *dev = ctodev(card);
 	dma_addr_t buf;
 
 	if (card->vlan_required) {
@@ -789,11 +792,10 @@ static int gelic_descr_prepare_tx(struct gelic_card *card,
 		skb = skb_tmp;
 	}
 
-	buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE);
+	buf = dma_map_single(dev, skb->data, skb->len, DMA_TO_DEVICE);
 
-	if (!buf) {
-		dev_err(ctodev(card),
-			"dma map 2 failed (%p, %i). Dropping packet\n",
+	if (unlikely(dma_mapping_error(dev, buf))) {
+		dev_err(dev, "dma map 2 failed (%p, %i). Dropping packet\n",
 			skb->data, skb->len);
 		return -ENOMEM;
 	}
author maintainer
#7 lore raw

Re: [PATCH net v4 2/2] net/ps3_gelic_net: Use dma_mapping_error

Hi Alexander,

Quoted 32 lines · Alexander H Duyck
Quoted 31 lines · Geoff Levand

The current Gelic Etherenet driver was checking the return value of its dma_map_single call, and not using the dma_mapping_error() routine.

Fixes runtime problems like these:

DMA-API: ps3_gelic_driver sb_05: device driver failed to check map error

  WARNING: CPU: 0 PID: 0 at kernel/dma/debug.c:1027 .check_unmap+0x888/0x8dc
Fixes 02c1889166b4 (ps3: gigabit ethernet driver for PS3, take3)
---
 drivers/net/ethernet/toshiba/ps3_gelic_net.c | 52 ++++++++++----------
 1 file changed, 27 insertions(+), 25 deletions(-)
drivers/net/ethernet/toshiba/ps3_gelic_net.c +3 -3
index 7a8b5e1e77a6..5622b512e2e4 100644
@@ -309,22 +309,34 @@ static int gelic_card_init_chain(struct gelic_card *card,
 				 struct gelic_descr_chain *chain,
 				 struct gelic_descr *start_descr, int no)
 {
-	int i;
+	struct device *dev = ctodev(card);
 	struct gelic_descr *descr;
+	int i;
 
-	descr = start_descr;
-	memset(descr, 0, sizeof(*descr) * no);
+	memset(start_descr, 0, no * sizeof(*start_descr));
 
Quoted 12 lines

drivers/net/ethernet/toshiba/ps3_gelic_net.c static int gelic_card_init_chain(struct gelic_card *card, · line 324

Quoted 8 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +1 -1
 	/* set up the hardware pointers in each descriptor */
-	for (i = 0; i < no; i++, descr++) {
+	for (i = 0, descr = start_descr; i < no; i++, descr++) {
 		gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
 		descr->bus_addr =
 			dma_map_single(ctodev(card), descr,
 				       GELIC_DESCR_SIZE,
 				       DMA_BIDIRECTIONAL);

Are bus_addr and the CPU the same byte ordering? Just wondering since this is being passed raw. I would have expected it to go through a cpu_to_be32.

As I mentioned in my reply to the first patch, the PS3's CPU is big endian, so we really don't need any of the endian conversions.

Quoted 19 lines

drivers/net/ethernet/toshiba/ps3_gelic_net.c static int gelic_card_init_chain(struct gelic_card *card, · line 326

Quoted 3 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +1 -2
-		if (!descr->bus_addr)
-			goto iommu_error;
+		if (unlikely(dma_mapping_error(dev, descr->bus_addr))) {

The expectation for dma_mapping_error is that the address is in cpu order. So in this case it is partially correct since bus_addr wasn't byte swapped, but generally the dma address used should be a CPU byte ordered variable.

drivers/net/ethernet/toshiba/ps3_gelic_net.c static int gelic_card_init_chain(struct gelic_card *card, · line 331

Quoted 5 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +5 -0
+			dev_err(dev, "%s:%d: dma_mapping_error\n", __func__,
+				__LINE__);
+
+			for (i--, descr--; i > 0; i--, descr--) {
+				if (descr->bus_addr) {

So I am pretty sure this is broken. Usually for something like this I will resort to just doing a while (i--) as "i == 0" should be a valid buffer to have to unmap.

Quoted 7 lines

Maybe something like:

			while (i--) {
				descr--;

Also I think you can get rid of the if since descr->bus_addr should be valid for all values since you populated it just a few lines above for each value of i.

OK, I'll change that.

Quoted 14 lines
Quoted 14 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +8 -0
+					dma_unmap_single(ctodev(card),
+						descr->bus_addr,
+						GELIC_DESCR_SIZE,
+						DMA_BIDIRECTIONAL);
+				}
+			}
+			return -ENOMEM;
+		}
 
 		descr->next = descr + 1;
 		descr->prev = descr - 1;
@@ -334,8 +346,7 @@ static int gelic_card_init_chain(struct gelic_card *card,
 	start_descr->prev = (descr - 1);
 
Quoted 9 lines

drivers/net/ethernet/toshiba/ps3_gelic_net.c static int gelic_card_init_chain(struct gelic_card *card, · line 352

Quoted 7 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +1 -2
 	/* chain bus addr of hw descriptor */
-	descr = start_descr;
-	for (i = 0; i < no; i++, descr++) {
+	for (i = 0, descr = start_descr; i < no; i++, descr++) {
 		descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
 	}
 

This seems like an unrelated change that was snuck in.

I'll remove that.

Once again, thanks for the review.

-Geoff

#8 lore raw

Re: [PATCH net v4 2/2] net/ps3_gelic_net: Use dma_mapping_error

Quoted 49 lines · Geoff Levand

Hi Alexander,

Quoted 32 lines · Alexander H Duyck
Quoted 31 lines · Geoff Levand

The current Gelic Etherenet driver was checking the return value of its dma_map_single call, and not using the dma_mapping_error() routine.

Fixes runtime problems like these:

DMA-API: ps3_gelic_driver sb_05: device driver failed to check map error

  WARNING: CPU: 0 PID: 0 at kernel/dma/debug.c:1027 .check_unmap+0x888/0x8dc
Fixes 02c1889166b4 (ps3: gigabit ethernet driver for PS3, take3)
---
 drivers/net/ethernet/toshiba/ps3_gelic_net.c | 52 ++++++++++----------
 1 file changed, 27 insertions(+), 25 deletions(-)
drivers/net/ethernet/toshiba/ps3_gelic_net.c +3 -3
index 7a8b5e1e77a6..5622b512e2e4 100644
@@ -309,22 +309,34 @@ static int gelic_card_init_chain(struct gelic_card *card,
                              struct gelic_descr_chain *chain,
                              struct gelic_descr *start_descr, int no)
 {
-    int i;
+    struct device *dev = ctodev(card);
     struct gelic_descr *descr;
+    int i;
 
-    descr = start_descr;
-    memset(descr, 0, sizeof(*descr) * no);
+    memset(start_descr, 0, no * sizeof(*start_descr));
 
Quoted 12 lines

drivers/net/ethernet/toshiba/ps3_gelic_net.c static int gelic_card_init_chain(struct gelic_card *card, · line 324

Quoted 8 lines
drivers/net/ethernet/toshiba/ps3_gelic_net.c +1 -1
     /* set up the hardware pointers in each descriptor */
-    for (i = 0; i < no; i++, descr++) {
+    for (i = 0, descr = start_descr; i < no; i++, descr++) {
             gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
             descr->bus_addr =
                     dma_map_single(ctodev(card), descr,
                                    GELIC_DESCR_SIZE,
                                    DMA_BIDIRECTIONAL);

Are bus_addr and the CPU the same byte ordering? Just wondering since this is being passed raw. I would have expected it to go through a cpu_to_be32.

Quoted 2 lines

As I mentioned in my reply to the first patch, the PS3's CPU is big endian, so we really don't need any of the endian conversions.

My advice would be to still make use of the cpu_to_be32 macros. It will take care of any possible byte ordering issues should you work with a different CPU architecture in the future. Otherwise if you are certain that the values will always be CPU ordered you might try changing the type rather than using __be32 for your descriptor variable types.

For example most PCIe hardware is using a little endian architecture and on x86 we don't need to do the byte swapping since the cpu is little endian. However we still use cpu_to_le32 throughout most drivers.

You might read through the documentation for sparse at:
https://www.kernel.org/doc/html/latest/dev-tools/sparse.html