Skip to contentSkip to conversation

ionic: fix 16bit math issue when PAGE_SIZE >= 64KB

PATCH In review David Christensen 5 participants 11 messages Sep 11, 2023 – Sep 16, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ionic: fix 16bit math issue when PAGE_SIZE >= 64KB

The function ionic_rx_fill() uses 16bit math when calculating the the number of pages required for an RX descriptor given an interface MTU setting. If the system PAGE_SIZE >= 64KB, the frag_len and remain_len values will always be 0, causing unnecessary scatter- gather elements to be assigned to the RX descriptor, up to the maximum number of scatter-gather elements per descriptor.

A similar change in ionic_rx_frags() is implemented for symmetry, but has not been observed as an issue since scatter-gather elements are not necessary for such larger page sizes.

Fixes 4b0a7539a372 ("ionic: implement Rx page reuse")
---
 drivers/net/ethernet/pensando/ionic/ionic_txrx.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/net/ethernet/pensando/ionic/ionic_txrx.c +5 -5
index 26798fc635db..56502bc80e01 100644
@@ -182,8 +182,8 @@ static struct sk_buff *ionic_rx_frags(struct ionic_queue *q,
 	struct device *dev = q->dev;
 	struct sk_buff *skb;
 	unsigned int i;
-	u16 frag_len;
-	u16 len;
+	u32 frag_len;
+	u32 len;
 
 	stats = q_to_rx_stats(q);
 
@@ -207,7 +207,7 @@ static struct sk_buff *ionic_rx_frags(struct ionic_queue *q,
 			return NULL;
 		}
 
-		frag_len = min_t(u16, len, IONIC_PAGE_SIZE - buf_info->page_offset);
+		frag_len = min_t(u32, len, IONIC_PAGE_SIZE - buf_info->page_offset);
 		len -= frag_len;
 
 		dma_sync_single_for_cpu(dev,
@@ -452,7 +452,7 @@ void ionic_rx_fill(struct ionic_queue *q)
 
 		/* fill main descriptor - buf[0] */
 		desc->addr = cpu_to_le64(buf_info->dma_addr + buf_info->page_offset);
-		frag_len = min_t(u16, len, IONIC_PAGE_SIZE - buf_info->page_offset);
+		frag_len = min_t(u32, len, IONIC_PAGE_SIZE - buf_info->page_offset);
 		desc->len = cpu_to_le16(frag_len);
 		remain_len -= frag_len;
 		buf_info++;
@@ -471,7 +471,7 @@ void ionic_rx_fill(struct ionic_queue *q)
 			}
 
 			sg_elem->addr = cpu_to_le64(buf_info->dma_addr + buf_info->page_offset);
-			frag_len = min_t(u16, remain_len, IONIC_PAGE_SIZE - buf_info->page_offset);
+			frag_len = min_t(u32, remain_len, IONIC_PAGE_SIZE - buf_info->page_offset);
 			sg_elem->len = cpu_to_le16(frag_len);
 			remain_len -= frag_len;
 			buf_info++;
Signature · 1 line
2.39.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · David Christensen

The function ionic_rx_fill() uses 16bit math when calculating the the number of pages required for an RX descriptor given an interface MTU setting. If the system PAGE_SIZE >= 64KB, the frag_len and remain_len values will always be 0, causing unnecessary scatter- gather elements to be assigned to the RX descriptor, up to the maximum number of scatter-gather elements per descriptor.

Quoted 7 lines

A similar change in ionic_rx_frags() is implemented for symmetry, but has not been observed as an issue since scatter-gather elements are not necessary for such larger page sizes.

Fixes 4b0a7539a372 ("ionic: implement Rx page reuse")

---

Given this is a bug fix, it should probably have a subject of [PATCH net] or [net] to indicate its targeting the net tree.

I'm not sure I follow the logic for frag_len and remain_len always being zero, since typecasting unsigned values truncates the higher bytes (technically its guaranteed by the standard to result in the smallest value congruent modulo 2^16 for a 16bit typecast), so if page_offset was non-zero then the resulting with the typecast should be as well.. but either way its definitely not going to work as desired.

Quoted 48 lines
 drivers/net/ethernet/pensando/ionic/ionic_txrx.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/net/ethernet/pensando/ionic/ionic_txrx.c +3 -3
index 26798fc635db..56502bc80e01 100644
@@ -182,8 +182,8 @@ static struct sk_buff *ionic_rx_frags(struct ionic_queue *q,
 	struct device *dev = q->dev;
 	struct sk_buff *skb;
 	unsigned int i;
-	u16 frag_len;
-	u16 len;
+	u32 frag_len;
+	u32 len;
 
 	stats = q_to_rx_stats(q);
 
@@ -207,7 +207,7 @@ static struct sk_buff *ionic_rx_frags(struct ionic_queue *q,
 			return NULL;
 		}
 
-		frag_len = min_t(u16, len, IONIC_PAGE_SIZE - buf_info->page_offset);
+		frag_len = min_t(u32, len, IONIC_PAGE_SIZE - buf_info->page_offset);
 		len -= frag_len;
 
 
drivers/net/ethernet/pensando/ionic/ionic_txrx.c +2 -2
 		dma_sync_single_for_cpu(dev,
@@ -452,7 +452,7 @@ void ionic_rx_fill(struct ionic_queue *q)
 
 		/* fill main descriptor - buf[0] */
 		desc->addr = cpu_to_le64(buf_info->dma_addr + buf_info->page_offset);
-		frag_len = min_t(u16, len, IONIC_PAGE_SIZE - buf_info->page_offset);
+		frag_len = min_t(u32, len, IONIC_PAGE_SIZE - buf_info->page_offset);
 		desc->len = cpu_to_le16(frag_len);
 		remain_len -= frag_len;
 		buf_info++;
@@ -471,7 +471,7 @@ void ionic_rx_fill(struct ionic_queue *q)
 			}
 
 			sg_elem->addr = cpu_to_le64(buf_info->dma_addr + buf_info->page_offset);
-			frag_len = min_t(u16, remain_len, IONIC_PAGE_SIZE - buf_info->page_offset);
+			frag_len = min_t(u32, remain_len, IONIC_PAGE_SIZE - buf_info->page_offset);
 			sg_elem->len = cpu_to_le16(frag_len);
 			remain_len -= frag_len;
 			buf_info++;
#2 lore raw
Quoted 14 lines · Jacob Keller
Quoted 11 lines · David Christensen

The function ionic_rx_fill() uses 16bit math when calculating the the number of pages required for an RX descriptor given an interface MTU setting. If the system PAGE_SIZE >= 64KB, the frag_len and remain_len values will always be 0, causing unnecessary scatter- gather elements to be assigned to the RX descriptor, up to the maximum number of scatter-gather elements per descriptor.

A similar change in ionic_rx_frags() is implemented for symmetry, but has not been observed as an issue since scatter-gather elements are not necessary for such larger page sizes.

Quoted 6 lines
Quoted 3 lines
Fixes 4b0a7539a372 ("ionic: implement Rx page reuse")

---

Given this is a bug fix, it should probably have a subject of [PATCH net] or [net] to indicate its targeting the net tree.

Will resend v2 with updated Subject line.

Quoted 7 lines

I'm not sure I follow the logic for frag_len and remain_len always being zero, since typecasting unsigned values truncates the higher bytes (technically its guaranteed by the standard to result in the smallest value congruent modulo 2^16 for a 16bit typecast), so if page_offset was non-zero then the resulting with the typecast should be as well.. but either way its definitely not going to work as desired.

Sorry, tried condensing the explanation too much. I'm not sure how frequently buf_info->page_offset is non-zero, but when ionic_rx_page_alloc() allocates a new page, as happens during initial driver load, it explicitly sets buf_info->page_offset to 0. As a result, the remain_len value never decreases from the original frame size (e.g. 1522) while frag_len is always calculated as 0 ((min_t(u16, 0x5F2, (0x1_0000 - 0) -> 0).

I'll update the the description in v2.

Dave

#3 lore raw
Quoted 34 lines · David Christensen
Quoted 14 lines · Jacob Keller
Quoted 11 lines · David Christensen

The function ionic_rx_fill() uses 16bit math when calculating the the number of pages required for an RX descriptor given an interface MTU setting. If the system PAGE_SIZE >= 64KB, the frag_len and remain_len values will always be 0, causing unnecessary scatter- gather elements to be assigned to the RX descriptor, up to the maximum number of scatter-gather elements per descriptor.

A similar change in ionic_rx_frags() is implemented for symmetry, but has not been observed as an issue since scatter-gather elements are not necessary for such larger page sizes.

Quoted 6 lines
Quoted 3 lines
Fixes 4b0a7539a372 ("ionic: implement Rx page reuse")

---

Given this is a bug fix, it should probably have a subject of [PATCH net] or [net] to indicate its targeting the net tree.

Will resend v2 with updated Subject line.

Quoted 7 lines

I'm not sure I follow the logic for frag_len and remain_len always being zero, since typecasting unsigned values truncates the higher bytes (technically its guaranteed by the standard to result in the smallest value congruent modulo 2^16 for a 16bit typecast), so if page_offset was non-zero then the resulting with the typecast should be as well.. but either way its definitely not going to work as desired.

Quoted 8 lines

Sorry, tried condensing the explanation too much. I'm not sure how frequently buf_info->page_offset is non-zero, but when ionic_rx_page_alloc() allocates a new page, as happens during initial driver load, it explicitly sets buf_info->page_offset to 0. As a result, the remain_len value never decreases from the original frame size (e.g. 1522) while frag_len is always calculated as 0 ((min_t(u16, 0x5F2, (0x1_0000 - 0) -> 0).

Yea that makes sense. When the page offset is zero this definitely results in a zero value (given that PAGE_SIZE is always a power of 2, so its congruent value is 0). In either case if PAGE_SIZE > 16bits this will produce incorrect and unexpected results regardless.

Quoted 3 lines

I'll update the the description in v2.

Dave

Thanks!

-Jake

#4 lore raw
In reply to David Christensen
Quoted 8 lines · David Christensen

The function ionic_rx_fill() uses 16bit math when calculating the the number of pages required for an RX descriptor given an interface MTU setting. If the system PAGE_SIZE >= 64KB, the frag_len and remain_len values will always be 0, causing unnecessary scatter- gather elements to be assigned to the RX descriptor, up to the maximum number of scatter-gather elements per descriptor.

Quoted 6 lines

A similar change in ionic_rx_frags() is implemented for symmetry, but has not been observed as an issue since scatter-gather elements are not necessary for such larger page sizes.

Fixes 4b0a7539a372 ("ionic: implement Rx page reuse")

The subject line prefix should have "net" in it to target the bug fix at the right tree.

Quoted 29 lines
---
  drivers/net/ethernet/pensando/ionic/ionic_txrx.c | 10 +++++-----
  1 file changed, 5 insertions(+), 5 deletions(-)
drivers/net/ethernet/pensando/ionic/ionic_txrx.c +3 -3
index 26798fc635db..56502bc80e01 100644
@@ -182,8 +182,8 @@ static struct sk_buff *ionic_rx_frags(struct ionic_queue *q,
         struct device *dev = q->dev;
         struct sk_buff *skb;
         unsigned int i;
-       u16 frag_len;
-       u16 len;
+       u32 frag_len;
+       u32 len;
 
         stats = q_to_rx_stats(q);
 
@@ -207,7 +207,7 @@ static struct sk_buff *ionic_rx_frags(struct ionic_queue *q,
                         return NULL;
                 }
 
-               frag_len = min_t(u16, len, IONIC_PAGE_SIZE - buf_info->page_offset);
+               frag_len = min_t(u32, len, IONIC_PAGE_SIZE - buf_info->page_offset);
                 len -= frag_len;
 
                 dma_sync_single_for_cpu(dev,

drivers/net/ethernet/pensando/ionic/ionic_txrx.c void ionic_rx_fill(struct ionic_queue *q) · line 456

Quoted 6 lines
drivers/net/ethernet/pensando/ionic/ionic_txrx.c +1 -1
@@ -452,7 +452,7 @@ void ionic_rx_fill(struct ionic_queue *q)
 
                 /* fill main descriptor - buf[0] */
                 desc->addr = cpu_to_le64(buf_info->dma_addr + buf_info->page_offset);
-               frag_len = min_t(u16, len, IONIC_PAGE_SIZE - buf_info->page_offset);
+               frag_len = min_t(u32, len, IONIC_PAGE_SIZE - buf_info->page_offset);
                 desc->len = cpu_to_le16(frag_len);

Hmm... using cpu_to_le16() on a 32-bit value looks suspect - it might get forced to 16-bit, but looks funky, and might not be as successful in a BigEndian environment.

Since the descriptor and sg_elem length fields are limited to 16-bit, there might need to have something that assures that the resulting lengths are never bigger than 64k - 1.

drivers/net/ethernet/pensando/ionic/ionic_txrx.c void ionic_rx_fill(struct ionic_queue *q) · line 475

Quoted 9 lines
drivers/net/ethernet/pensando/ionic/ionic_txrx.c +1 -1
                 remain_len -= frag_len;
                 buf_info++;
@@ -471,7 +471,7 @@ void ionic_rx_fill(struct ionic_queue *q)
                         }
 
                         sg_elem->addr = cpu_to_le64(buf_info->dma_addr + buf_info->page_offset);
-                       frag_len = min_t(u16, remain_len, IONIC_PAGE_SIZE - buf_info->page_offset);
+                       frag_len = min_t(u32, remain_len, IONIC_PAGE_SIZE - buf_info->page_offset);
                         sg_elem->len = cpu_to_le16(frag_len);

ditto

Quoted 5 lines
drivers/net/ethernet/pensando/ionic/ionic_txrx.c +0 -1
                         remain_len -= frag_len;
                         buf_info++;
--

2.39.1

#5 lore raw
Quoted 12 lines · Shannon

drivers/net/ethernet/pensando/ionic/ionic_txrx.c void ionic_rx_fill(struct ionic_queue *q) · line 458

Quoted 9 lines
drivers/net/ethernet/pensando/ionic/ionic_txrx.c +1 -1
@@ -452,7 +452,7 @@ void ionic_rx_fill(struct ionic_queue *q)
 
                  /* fill main descriptor - buf[0] */
                  desc->addr = cpu_to_le64(buf_info->dma_addr + buf_info->page_offset);
-               frag_len = min_t(u16, len, IONIC_PAGE_SIZE - 
 buf_info->page_offset);
+               frag_len = min_t(u32, len, IONIC_PAGE_SIZE - 
 buf_info->page_offset);
                  desc->len = cpu_to_le16(frag_len);

Hmm... using cpu_to_le16() on a 32-bit value looks suspect - it might get forced to 16-bit, but looks funky, and might not be as successful in a BigEndian environment.

Quoted 2 lines

Since the descriptor and sg_elem length fields are limited to 16-bit, there might need to have something that assures that the resulting lengths are never bigger than 64k - 1.

What do you think about this:

frag_len = min_t(u16, len, min_t(u32, 0xFFFF, IONIC_PAGE_SIZE -
buf_info->page_offset));

Can you think of a test case where buf_info->page_offset will be non-zero that I can test locally?

Dave

#6 lore raw
Quoted 23 lines · David Christensen
Quoted 15 lines · Shannon
Quoted 10 lines
drivers/net/ethernet/pensando/ionic/ionic_txrx.c +0 -0
@@ -452,7 +452,7 @@ void ionic_rx_fill(struct ionic_queue *q)
 
                  /* fill main descriptor - buf[0] */

                 desc->addr = cpu_to_le64(buf_info->dma_addr + buf_info->page_offset);
-               frag_len = min_t(u16, len, IONIC_PAGE_SIZE - buf_info->page_offset);
+               frag_len = min_t(u32, len, IONIC_PAGE_SIZE - buf_info->page_offset);
                 desc->len = cpu_to_le16(frag_len);

Hmm... using cpu_to_le16() on a 32-bit value looks suspect - it might get forced to 16-bit, but looks funky, and might not be as successful in a BigEndian environment.

Quoted 4 lines

Since the descriptor and sg_elem length fields are limited to 16-bit, there might need to have something that assures that the resulting lengths are never bigger than 64k - 1.

Quoted 4 lines

What do you think about this:

 frag_len = min_t(u16, len, min_t(u32, 0xFFFF, IONIC_PAGE_SIZE -
buf_info->page_offset));

Yes, that looks a little safer.

We'll still need to do something about the 32-bit frag_len used in the cpu_to_le16() call.

Quoted 3 lines

Can you think of a test case where buf_info->page_offset will be non-zero that I can test locally?

No, I don't have one off-hand.

Thanks,
sln

Quoted 2 lines

Dave

#7 lore raw
Quoted 32 lines · Shannon' via Pensando Drivers
Quoted 23 lines · David Christensen
Quoted 15 lines · Shannon
Quoted 10 lines
drivers/net/ethernet/pensando/ionic/ionic_txrx.c +0 -0
@@ -452,7 +452,7 @@ void ionic_rx_fill(struct ionic_queue *q)
 
                  /* fill main descriptor - buf[0] */

                 desc->addr = cpu_to_le64(buf_info->dma_addr + buf_info->page_offset);
-               frag_len = min_t(u16, len, IONIC_PAGE_SIZE - buf_info->page_offset);
+               frag_len = min_t(u32, len, IONIC_PAGE_SIZE - buf_info->page_offset);
                 desc->len = cpu_to_le16(frag_len);

Hmm... using cpu_to_le16() on a 32-bit value looks suspect - it might get forced to 16-bit, but looks funky, and might not be as successful in a BigEndian environment.

Quoted 4 lines

Since the descriptor and sg_elem length fields are limited to 16-bit, there might need to have something that assures that the resulting lengths are never bigger than 64k - 1.

Quoted 4 lines

What do you think about this:

  frag_len = min_t(u16, len, min_t(u32, 0xFFFF, IONIC_PAGE_SIZE - buf_info->page_offset));

Yes, that looks a little safer.

Quoted 8 lines

We'll still need to do something about the 32-bit frag_len used in the cpu_to_le16() call.

Quoted 3 lines

Can you think of a test case where buf_info->page_offset will be non-zero that I can test locally?

No, I don't have one off-hand.

Sorry, I was thinking about the case of a large page.

In the normal 1500 MTU case with heavy rx traffic (e.g. iperf receive, pktgen, etc) we should see page split/offset use.

sln

#8 lore raw

PATCH v2 1/1 ionic: fix 16bit math issue when PAGE_SIZE >= 64KB

In reply to David Christensen

From: David Christensen <[email protected]>

The ionic device supports a maximum buffer length of 16 bits (see ionic_rxq_desc or ionic_rxq_sg_elem). When adding new buffers to the receive rings, the function ionic_rx_fill() uses 16bit math when calculating the number of pages to allocate for an RX descriptor, given the interface's MTU setting. If the system PAGE_SIZE >= 64KB, and the buf_info->page_offset is 0, the remain_len value will never decrement from the original MTU value and the frag_len value will always be 0, causing additional pages to be allocated as scatter- gather elements unnecessarily.

A similar math issue exists in ionic_rx_frags(), but no failures have been observed here since a 64KB page should not normally require any scatter-gather elements at any legal Ethernet MTU size.

Fixes 4b0a7539a372 ("ionic: implement Rx page reuse")
---
 drivers/net/ethernet/pensando/ionic/ionic_dev.h  |  1 +
 drivers/net/ethernet/pensando/ionic/ionic_txrx.c | 10 +++++++---
 2 files changed, 8 insertions(+), 3 deletions(-)
drivers/net/ethernet/pensando/ionic/ionic_dev.h +1 -0
index 6aac98bcb9f4..aae4131f146a 100644
@@ -187,6 +187,7 @@ typedef void (*ionic_desc_cb)(struct ionic_queue *q,
 			      struct ionic_desc_info *desc_info,
 			      struct ionic_cq_info *cq_info, void *cb_arg);
 
+#define IONIC_MAX_BUF_LEN			((u16)-1)
 #define IONIC_PAGE_SIZE				PAGE_SIZE
 #define IONIC_PAGE_SPLIT_SZ			(PAGE_SIZE / 2)
 #define IONIC_PAGE_GFP_MASK			(GFP_ATOMIC | __GFP_NOWARN |\
drivers/net/ethernet/pensando/ionic/ionic_txrx.c +7 -3
index 26798fc635db..44466e8c5d77 100644
@@ -207,7 +207,8 @@ static struct sk_buff *ionic_rx_frags(struct ionic_queue *q,
 			return NULL;
 		}
 
-		frag_len = min_t(u16, len, IONIC_PAGE_SIZE - buf_info->page_offset);
+		frag_len = min_t(u16, len, min_t(u32, IONIC_MAX_BUF_LEN,
+						 IONIC_PAGE_SIZE - buf_info->page_offset));
 		len -= frag_len;
 
 		dma_sync_single_for_cpu(dev,
@@ -452,7 +453,8 @@ void ionic_rx_fill(struct ionic_queue *q)
 
 		/* fill main descriptor - buf[0] */
 		desc->addr = cpu_to_le64(buf_info->dma_addr + buf_info->page_offset);
-		frag_len = min_t(u16, len, IONIC_PAGE_SIZE - buf_info->page_offset);
+		frag_len = min_t(u16, len, min_t(u32, IONIC_MAX_BUF_LEN,
+						 IONIC_PAGE_SIZE - buf_info->page_offset));
 		desc->len = cpu_to_le16(frag_len);
 		remain_len -= frag_len;
 		buf_info++;
@@ -471,7 +473,9 @@ void ionic_rx_fill(struct ionic_queue *q)
 			}
 
 			sg_elem->addr = cpu_to_le64(buf_info->dma_addr + buf_info->page_offset);
-			frag_len = min_t(u16, remain_len, IONIC_PAGE_SIZE - buf_info->page_offset);
+			frag_len = min_t(u16, remain_len, min_t(u32, IONIC_MAX_BUF_LEN,
+								IONIC_PAGE_SIZE -
+								buf_info->page_offset));
 			sg_elem->len = cpu_to_le16(frag_len);
 			remain_len -= frag_len;
 			buf_info++;
Signature · 1 line
2.39.1
Reviewed-by on v2 1/1
#9 lore raw
Quoted 13 lines · David Christensen

From: David Christensen <[email protected]>

The ionic device supports a maximum buffer length of 16 bits (see ionic_rxq_desc or ionic_rxq_sg_elem). When adding new buffers to the receive rings, the function ionic_rx_fill() uses 16bit math when calculating the number of pages to allocate for an RX descriptor, given the interface's MTU setting. If the system PAGE_SIZE >= 64KB, and the buf_info->page_offset is 0, the remain_len value will never decrement from the original MTU value and the frag_len value will always be 0, causing additional pages to be allocated as scatter- gather elements unnecessarily.

Quoted 6 lines

A similar math issue exists in ionic_rx_frags(), but no failures have been observed here since a 64KB page should not normally require any scatter-gather elements at any legal Ethernet MTU size.

Fixes 4b0a7539a372 ("ionic: implement Rx page reuse")

Thanks

Quoted 55 lines
---
  drivers/net/ethernet/pensando/ionic/ionic_dev.h  |  1 +
  drivers/net/ethernet/pensando/ionic/ionic_txrx.c | 10 +++++++---
  2 files changed, 8 insertions(+), 3 deletions(-)
drivers/net/ethernet/pensando/ionic/ionic_dev.h +1 -0
index 6aac98bcb9f4..aae4131f146a 100644
@@ -187,6 +187,7 @@ typedef void (*ionic_desc_cb)(struct ionic_queue *q,
                               struct ionic_desc_info *desc_info,
                               struct ionic_cq_info *cq_info, void *cb_arg);
 
+#define IONIC_MAX_BUF_LEN                      ((u16)-1)
  #define IONIC_PAGE_SIZE                                PAGE_SIZE
  #define IONIC_PAGE_SPLIT_SZ                    (PAGE_SIZE / 2)
  #define IONIC_PAGE_GFP_MASK                    (GFP_ATOMIC | __GFP_NOWARN |\
drivers/net/ethernet/pensando/ionic/ionic_txrx.c +7 -4
index 26798fc635db..44466e8c5d77 100644
@@ -207,7 +207,8 @@ static struct sk_buff *ionic_rx_frags(struct ionic_queue *q,
                         return NULL;
                 }
 
-               frag_len = min_t(u16, len, IONIC_PAGE_SIZE - buf_info->page_offset);
+               frag_len = min_t(u16, len, min_t(u32, IONIC_MAX_BUF_LEN,
+                                                IONIC_PAGE_SIZE - buf_info->page_offset));
                 len -= frag_len;
 
                 dma_sync_single_for_cpu(dev,
@@ -452,7 +453,8 @@ void ionic_rx_fill(struct ionic_queue *q)
 
                 /* fill main descriptor - buf[0] */
                 desc->addr = cpu_to_le64(buf_info->dma_addr + buf_info->page_offset);
-               frag_len = min_t(u16, len, IONIC_PAGE_SIZE - buf_info->page_offset);
+               frag_len = min_t(u16, len, min_t(u32, IONIC_MAX_BUF_LEN,
+                                                IONIC_PAGE_SIZE - buf_info->page_offset));
                 desc->len = cpu_to_le16(frag_len);
                 remain_len -= frag_len;
                 buf_info++;
@@ -471,7 +473,9 @@ void ionic_rx_fill(struct ionic_queue *q)
                         }
 
                         sg_elem->addr = cpu_to_le64(buf_info->dma_addr + buf_info->page_offset);
-                       frag_len = min_t(u16, remain_len, IONIC_PAGE_SIZE - buf_info->page_offset);
+                       frag_len = min_t(u16, remain_len, min_t(u32, IONIC_MAX_BUF_LEN,
+                                                               IONIC_PAGE_SIZE -
+                                                               buf_info->page_offset));
                         sg_elem->len = cpu_to_le16(frag_len);
                         remain_len -= frag_len;
                         buf_info++;
--

2.39.1

#10 lore raw
In reply to David Christensen
This patch was applied to netdev/net.git (main) by David S. Miller <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by David S. Miller <[email protected]>:

Quoted 12 lines · On Thu, 14 Sep 2023 18:02:52 -0400 you wrote:

From: David Christensen <[email protected]>

The ionic device supports a maximum buffer length of 16 bits (see ionic_rxq_desc or ionic_rxq_sg_elem). When adding new buffers to the receive rings, the function ionic_rx_fill() uses 16bit math when calculating the number of pages to allocate for an RX descriptor, given the interface's MTU setting. If the system PAGE_SIZE >= 64KB, and the buf_info->page_offset is 0, the remain_len value will never decrement from the original MTU value and the frag_len value will always be 0, causing additional pages to be allocated as scatter- gather elements unnecessarily.

Quoted 1 line

[...]

Here is the summary with links:
- [net,v2] ionic: fix 16bit math issue when PAGE_SIZE >= 64KB

    https://git.kernel.org/netdev/net/c/8f6b846b0a86

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html