Skip to contentSkip to conversation

net/ice: fix statistics read error

PATCH Superseded Zhichao Zeng 3 participants 10 messages Nov 7, 2025 – Nov 13, 2025 Network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/ice: fix statistics read error

The statistics contain 40 bits. The lower 32 bits are read first, followed by the upper 8 bits.

In some cases, after reading the lower 32 bits, a carry occurs from the lower bits, which causes the final statistics to be incorrect.

This commit fixes this issue.

Fixes a37bde56314d ("net/ice: support statistics")
---
 drivers/net/intel/ice/ice_ethdev.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/net/intel/ice/ice_ethdev.c +12 -3
index 513777e372..be08be0826 100644
@@ -6036,10 +6036,19 @@ ice_stat_update_40(struct ice_hw *hw,
 		   uint64_t *stat)
 {
 	uint64_t new_data;
+	uint32_t lo, hi, lo2;
 
-	new_data = (uint64_t)ICE_READ_REG(hw, loreg);
-	new_data |= (uint64_t)(ICE_READ_REG(hw, hireg) & ICE_8_BIT_MASK) <<
-		    ICE_32_BIT_WIDTH;
+	lo = ICE_READ_REG(hw, loreg);
+	hi = ICE_READ_REG(hw, hireg);
+	lo2 = ICE_READ_REG(hw, loreg);
+
+	if (lo2 < lo) {
+		lo = ICE_READ_REG(hw, loreg);
+		hi = ICE_READ_REG(hw, hireg);
+	}
+
+	new_data = (uint64_t)lo;
+	new_data |= (uint64_t)(hi & ICE_8_BIT_MASK) << ICE_32_BIT_WIDTH;
 
 	if (!offset_loaded)
 		*offset = new_data;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 6 lines · Zhichao Zeng

The statistics contain 40 bits. The lower 32 bits are read first, followed by the upper 8 bits.

In some cases, after reading the lower 32 bits, a carry occurs from the lower bits, which causes the final statistics to be incorrect.

Quoted 7 lines

This commit fixes this issue.

Fixes a37bde56314d ("net/ice: support statistics")

---

See comments inline below.

/Bruce

Quoted 15 lines
 drivers/net/intel/ice/ice_ethdev.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/net/intel/ice/ice_ethdev.c +1 -2
index 513777e372..be08be0826 100644
@@ -6036,10 +6036,19 @@ ice_stat_update_40(struct ice_hw *hw,
 		   uint64_t *stat)
 {
 	uint64_t new_data;
+	uint32_t lo, hi, lo2;
 
-	new_data = (uint64_t)ICE_READ_REG(hw, loreg);
-	new_data |= (uint64_t)(ICE_READ_REG(hw, hireg) & ICE_8_BIT_MASK) <<

drivers/net/intel/ice/ice_ethdev.c ice_stat_update_40(struct ice_hw *hw, · line 6049

Quoted 10 lines
drivers/net/intel/ice/ice_ethdev.c +9 -1
-		    ICE_32_BIT_WIDTH;
+	lo = ICE_READ_REG(hw, loreg);
+	hi = ICE_READ_REG(hw, hireg);
+	lo2 = ICE_READ_REG(hw, loreg);
+
+	if (lo2 < lo) {
+		lo = ICE_READ_REG(hw, loreg);
+		hi = ICE_READ_REG(hw, hireg);
+	}
+

While I can't see multiple wrap-arounds occuring, it is theoretically possible e.g. if a process gets context switched out. Therefore, I think using a "while" construction would be better. For example:

	uint32_t lo_old, hi, lo;
	do {
		lo_old = ...
		hi = ...
		lo = ...
	} while (lo_old > lo);

drivers/net/intel/ice/ice_ethdev.c ice_stat_update_40(struct ice_hw *hw, · line 6052

Quoted 3 lines
drivers/net/intel/ice/ice_ethdev.c +2 -0
+	new_data = (uint64_t)lo;
+	new_data |= (uint64_t)(hi & ICE_8_BIT_MASK) << ICE_32_BIT_WIDTH;
 

Using "lo" rather than "lo2" here means we are returning data we know is old. We should be returning the second value read. Suggest using something like what I suggest above, where we make "lo" the second read rather than the first.

Quoted 5 lines
drivers/net/intel/ice/ice_ethdev.c +0 -0
 	if (!offset_loaded)
 		*offset = new_data;
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 1/1 net/ice: fix statistics read error

In reply to Zhichao Zeng

The statistics contain 40 bits. The lower 32 bits are read first, followed by the upper 8 bits.

In some cases, after reading the lower 32 bits, a carry occurs from the lower bits, which causes the final statistics to be incorrect.

This commit fixes this issue.

Fixes a37bde56314d ("net/ice: support statistics")

---
v2: replace single retries with loops

---
 drivers/net/intel/ice/ice_ethdev.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
drivers/net/intel/ice/ice_ethdev.c +11 -7
index 4669eba7c7..016b25c63a 100644
@@ -6351,10 +6351,16 @@ ice_stat_update_40(struct ice_hw *hw,
 		   uint64_t *stat)
 {
 	uint64_t new_data;
+	uint32_t lo_old, hi, lo;
 
-	new_data = (uint64_t)ICE_READ_REG(hw, loreg);
-	new_data |= (uint64_t)(ICE_READ_REG(hw, hireg) & ICE_8_BIT_MASK) <<
-		    ICE_32_BIT_WIDTH;
+	do {
+		lo_old = ICE_READ_REG(hw, loreg);
+		hi = ICE_READ_REG(hw, hireg);
+		lo = ICE_READ_REG(hw, loreg);
+	} while (lo_old > lo);
+
+	new_data = (uint64_t)lo;
+	new_data |= (uint64_t)(hi & ICE_8_BIT_MASK) << ICE_32_BIT_WIDTH;
 
 	if (!offset_loaded)
 		*offset = new_data;
@@ -6363,10 +6369,8 @@ ice_stat_update_40(struct ice_hw *hw,
 		*stat = new_data - *offset;
 	else
 		*stat = (uint64_t)((new_data +
-				    ((uint64_t)1 << ICE_40_BIT_WIDTH)) -
-				   *offset);
-
-	*stat &= ICE_40_BIT_MASK;
+					((uint64_t)1 << ICE_32_BIT_WIDTH))
+				   - *offset);
 }
 
 /**
Signature · 1 line
2.34.1
#3 lore raw
Quoted 44 lines · Zhichao Zeng

The statistics contain 40 bits. The lower 32 bits are read first, followed by the upper 8 bits.

In some cases, after reading the lower 32 bits, a carry occurs from the lower bits, which causes the final statistics to be incorrect.

This commit fixes this issue.

Fixes a37bde56314d ("net/ice: support statistics")

---
v2: replace single retries with loops

---
 drivers/net/intel/ice/ice_ethdev.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
drivers/net/intel/ice/ice_ethdev.c +9 -3
index 4669eba7c7..016b25c63a 100644
@@ -6351,10 +6351,16 @@ ice_stat_update_40(struct ice_hw *hw,
 		   uint64_t *stat)
 {
 	uint64_t new_data;
+	uint32_t lo_old, hi, lo;
 
-	new_data = (uint64_t)ICE_READ_REG(hw, loreg);
-	new_data |= (uint64_t)(ICE_READ_REG(hw, hireg) & ICE_8_BIT_MASK) <<
-		    ICE_32_BIT_WIDTH;
+	do {
+		lo_old = ICE_READ_REG(hw, loreg);
+		hi = ICE_READ_REG(hw, hireg);
+		lo = ICE_READ_REG(hw, loreg);
+	} while (lo_old > lo);
+
+	new_data = (uint64_t)lo;
+	new_data |= (uint64_t)(hi & ICE_8_BIT_MASK) << ICE_32_BIT_WIDTH;
 
 	if (!offset_loaded)
 		*offset = new_data;

drivers/net/intel/ice/ice_ethdev.c ice_stat_update_40(struct ice_hw *hw, · line 6373

Quoted 9 lines
drivers/net/intel/ice/ice_ethdev.c +2 -4
@@ -6363,10 +6369,8 @@ ice_stat_update_40(struct ice_hw *hw,
 		*stat = new_data - *offset;
 	else
 		*stat = (uint64_t)((new_data +
-				    ((uint64_t)1 << ICE_40_BIT_WIDTH)) -
-				   *offset);
-
-	*stat &= ICE_40_BIT_MASK;
+					((uint64_t)1 << ICE_32_BIT_WIDTH))
+				   - *offset);

This part wasn't in v1, was it? It looks wrong to me, and the original code looks correct. Given that offset and new_data should both be 40-bit quantities, any wraparound would be at 40-bits rather than 32-bits no? Can you explain why we would use a 32-bit shift here?

Quoted 6 lines
drivers/net/intel/ice/ice_ethdev.c +0 -0
 }
 
 /**
Signature · 1 line
2.34.1
#4 lore raw

Hi Bruce,

Thank you for your comments and you're right.

My apologies—since this patch was submitted some time ago, I lost track of the context when sending v2 and made an incorrect modification. I'll remove the erroneous code.

Regards

Zhichao

________________________________
发件人: Richardson, Bruce <[email protected]>
已发送: 2025 年 11 月 12 日 星期三 20:04
收件人: Zeng, ZhichaoX <[email protected]>
抄送: [email protected] <[email protected]>; [email protected] <[email protected]>; Burakov, Anatoly <[email protected]>; Jeff Guo <[email protected]>; Ferruh Yigit <[email protected]>; Wenzhuo Lu <[email protected]> 主题: Re: [PATCH v2] net/ice: fix statistics read error

Quoted 44 lines · Zhichao Zeng

The statistics contain 40 bits. The lower 32 bits are read first, followed by the upper 8 bits.

In some cases, after reading the lower 32 bits, a carry occurs from the lower bits, which causes the final statistics to be incorrect.

This commit fixes this issue.

Fixes a37bde56314d ("net/ice: support statistics")

---
v2: replace single retries with loops

---
 drivers/net/intel/ice/ice_ethdev.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
drivers/net/intel/ice/ice_ethdev.c +9 -3
index 4669eba7c7..016b25c63a 100644
@@ -6351,10 +6351,16 @@ ice_stat_update_40(struct ice_hw *hw,
                  uint64_t *stat)
 {
       uint64_t new_data;
+     uint32_t lo_old, hi, lo;
 
-     new_data = (uint64_t)ICE_READ_REG(hw, loreg);
-     new_data |= (uint64_t)(ICE_READ_REG(hw, hireg) & ICE_8_BIT_MASK) <<
-                 ICE_32_BIT_WIDTH;
+     do {
+             lo_old = ICE_READ_REG(hw, loreg);
+             hi = ICE_READ_REG(hw, hireg);
+             lo = ICE_READ_REG(hw, loreg);
+     } while (lo_old > lo);
+
+     new_data = (uint64_t)lo;
+     new_data |= (uint64_t)(hi & ICE_8_BIT_MASK) << ICE_32_BIT_WIDTH;
 
       if (!offset_loaded)
               *offset = new_data;

drivers/net/intel/ice/ice_ethdev.c ice_stat_update_40(struct ice_hw *hw, · line 6373

Quoted 9 lines
drivers/net/intel/ice/ice_ethdev.c +2 -4
@@ -6363,10 +6369,8 @@ ice_stat_update_40(struct ice_hw *hw,
               *stat = new_data - *offset;
       else
               *stat = (uint64_t)((new_data +
-                                 ((uint64_t)1 << ICE_40_BIT_WIDTH)) -
-                                *offset);
-
-     *stat &= ICE_40_BIT_MASK;
+                                     ((uint64_t)1 << ICE_32_BIT_WIDTH))
+                                - *offset);

This part wasn't in v1, was it? It looks wrong to me, and the original code looks correct. Given that offset and new_data should both be 40-bit quantities, any wraparound would be at 40-bits rather than 32-bits no? Can you explain why we would use a 32-bit shift here?

Quoted 6 lines
drivers/net/intel/ice/ice_ethdev.c +0 -1
 }
 
 /**
--

2.34.1

#5 lore raw

PATCH v3 1/1 net/ice: fix statistics read error

In reply to Zhichao Zeng

The statistics contain 40 bits. The lower 32 bits are read first, followed by the upper 8 bits.

In some cases, after reading the lower 32 bits, a carry occurs from the lower bits, which causes the final statistics to be incorrect.

This commit fixes this issue.

Fixes a37bde56314d ("net/ice: support statistics")

---
v2: replace single retries with loops
v3: fix wrong logic

---
 drivers/net/intel/ice/ice_ethdev.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/net/intel/ice/ice_ethdev.c +9 -3
index 4669eba7c7..5ec57ee2b0 100644
@@ -6351,10 +6351,16 @@ ice_stat_update_40(struct ice_hw *hw,
 		   uint64_t *stat)
 {
 	uint64_t new_data;
+	uint32_t lo_old, hi, lo;
 
-	new_data = (uint64_t)ICE_READ_REG(hw, loreg);
-	new_data |= (uint64_t)(ICE_READ_REG(hw, hireg) & ICE_8_BIT_MASK) <<
-		    ICE_32_BIT_WIDTH;
+	do {
+		lo_old = ICE_READ_REG(hw, loreg);
+		hi = ICE_READ_REG(hw, hireg);
+		lo = ICE_READ_REG(hw, loreg);
+	} while (lo_old > lo);
+
+	new_data = (uint64_t)lo;
+	new_data |= (uint64_t)(hi & ICE_8_BIT_MASK) << ICE_32_BIT_WIDTH;
 
 	if (!offset_loaded)
 		*offset = new_data;
Signature · 1 line
2.34.1
Acked-by on v3 1/1
#6 lore raw
Quoted 6 lines · Zhichao Zeng

The statistics contain 40 bits. The lower 32 bits are read first, followed by the upper 8 bits.

In some cases, after reading the lower 32 bits, a carry occurs from the lower bits, which causes the final statistics to be incorrect.

Quoted 7 lines

This commit fixes this issue.

Fixes a37bde56314d ("net/ice: support statistics")
#7 lore raw
Quoted 7 lines · Bruce Richardson
Quoted 6 lines · Zhichao Zeng

The statistics contain 40 bits. The lower 32 bits are read first, followed by the upper 8 bits.

In some cases, after reading the lower 32 bits, a carry occurs from the lower bits, which causes the final statistics to be incorrect.

Quoted 8 lines
Quoted 7 lines

This commit fixes this issue.

Fixes a37bde56314d ("net/ice: support statistics")

Applied to dpdk-next-net-intel

/Bruce

#8 lore raw
In reply to Zhichao Zeng
Quoted 6 lines · On Fri, 7 Nov 2025 15:43:38 +0800 Zhichao Zeng <[email protected]> wrote:

The statistics contain 40 bits. The lower 32 bits are read first, followed by the upper 8 bits.

In some cases, after reading the lower 32 bits, a carry occurs from the lower bits, which causes the final statistics to be incorrect.

Quoted 6 lines

This commit fixes this issue.

Fixes a37bde56314d ("net/ice: support statistics")

This works but may still have issues under really high load. I have seen code like this (from ChatGPT)

/*
* Safely read a 64-bit counter split across two 32-bit registers.
* Handles rollover and avoids inconsistency if low overflows
* between high and low reads.
*/
static inline uint64_t read_hw_counter_64(void)

{
    uint32_t hi1, lo, hi2;

    /*
     * Read high-low-high and check for wraparound:
     * if high changed, low overflowed during read — reread.
     */
    do {
        hi1 = read_reg32_high();
        lo  = read_reg32_low();
        hi2 = read_reg32_high();
    } while (hi1 != hi2);

    return ((uint64_t)hi1 << 32) | lo;
}
#9 lore raw
Quoted 11 lines · Stephen Hemminger
Quoted 8 lines · On Fri, 7 Nov 2025 15:43:38 +0800 Zhichao Zeng <[email protected]> wrote:

The statistics contain 40 bits. The lower 32 bits are read first, followed by the upper 8 bits.

In some cases, after reading the lower 32 bits, a carry occurs from the lower bits, which causes the final statistics to be incorrect.

This commit fixes this issue.

Quoted 8 lines
Quoted 4 lines
Fixes a37bde56314d ("net/ice: support statistics")

This works but may still have issues under really high load. I have seen code like this (from ChatGPT)

How is checking high for equality better? I considered when reviewing having the high be double-read, but I though we'd get better accuracy by reading the more frequency-changing counter (lo) last.

/Bruce

Quoted 22 lines

/*
* Safely read a 64-bit counter split across two 32-bit registers.
* Handles rollover and avoids inconsistency if low overflows
* between high and low reads.
*/
static inline uint64_t read_hw_counter_64(void)

{
    uint32_t hi1, lo, hi2;

    /*
     * Read high-low-high and check for wraparound:
     * if high changed, low overflowed during read — reread.
     */
    do {
        hi1 = read_reg32_high();
        lo  = read_reg32_low();
        hi2 = read_reg32_high();
    } while (hi1 != hi2);

    return ((uint64_t)hi1 << 32) | lo;
}