Skip to contentSkip to conversation

e1000e: fix call to do_div() to use u64 arg

PATCH New Jeff Kirsher 2 participants 3 messages Apr 8, 2015 – Apr 9, 2015

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

PATCH 1/1 e1000e: fix call to do_div() to use u64 arg

We were using s64 for lat_ns (latency nano-second value) since in our calculations a negative value could be a resultant. For negative values, we then assign lat_ns to be zero, so the value passed to do_div() was never negative, but do_div() expects the argument type to be u64, so do a cast to resolve a compile warning seen on PowerPC.

---
 drivers/net/ethernet/intel/e1000e/ich8lan.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/intel/e1000e/ich8lan.c +1 -1
index 9d81c03..30d277a 100644
@@ -1045,7 +1045,7 @@ static s32 e1000_platform_pm_pch_lpt(struct e1000_hw *hw, bool link)
 		if (lat_ns < 0)
 			lat_ns = 0;
 		else
-			do_div(lat_ns, speed);
+			do_div((u64)lat_ns, speed);
 
 		value = lat_ns;
 		while (value > PCI_LTR_VALUE_MASK) {
#1 lore raw

PATCH 1/1 e1000e: fix call to do_div() to use u64 arg

From: Jeff Kirsher

Quoted 7 lines

Sent: 08 April 2015 21:59
We were using s64 for lat_ns (latency nano-second value) since in our calculations a negative value could be a resultant. For negative values, we then assign lat_ns to be zero, so the value passed to do_div() was never negative, but do_div() expects the argument type to be u64, so do a cast to resolve a compile warning seen on PowerPC.

...

drivers/net/ethernet/intel/e1000e/ich8lan.c static s32 e1000_platform_pm_pch_lpt(struct e1000_hw *hw, bool link) · line 1048

Quoted 2 lines
drivers/net/ethernet/intel/e1000e/ich8lan.c +1 -1
-			do_div(lat_ns, speed);
+			do_div((u64)lat_ns, speed);

Personally I think that adding casts to avoid warnings
from integer promotions and function arguments isn't a good idea. The problem is that C casts are too powerful and can hide other bugs. In the past I've been bitten by a cast inside a (badly written) #define function.

The above is a typical case where the compiler should really be doing value domain checks before reporting anything at all - if at all.

David

#2 lore raw

PATCH 1/1 e1000e: fix call to do_div() to use u64 arg

Quoted 20 lines · David Laight

From: Jeff Kirsher

Quoted 3 lines

Sent: 08 April 2015 21:59
We were using s64 for lat_ns (latency nano-second value) since in our calculations a negative value could be a resultant. For

negative

Quoted 4 lines

values, we then assign lat_ns to be zero, so the value passed to do_div() was never negative, but do_div() expects the argument type to be u64, so do a cast to resolve a compile warning seen on PowerPC.

...

drivers/net/ethernet/intel/e1000e/ich8lan.c static s32 e1000_platform_pm_pch_lpt(struct e1000_hw *hw, bool link) · line 1048

Quoted 2 lines
drivers/net/ethernet/intel/e1000e/ich8lan.c +1 -1
-                     do_div(lat_ns, speed);
+                     do_div((u64)lat_ns, speed);

Personally I think that adding casts to avoid warnings from integer promotions and function arguments isn't a good idea. The problem is that C casts are too powerful and can hide other bugs. In the past I've been bitten by a cast inside a (badly written) #define
function.

Quoted 2 lines

The above is a typical case where the compiler should really be doing value domain checks before reporting anything at all - if at all.

I agree with you that I do not like casts because it can potentially
hide bugs. On that note, I have a follow on patch that should resolve
the PowerPC compile warning without having to do a cast. Just re-works
the existing code, expect a v2 shortly.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part
URL: <http://lists.osuosl.org/pipermail/intel-wired-lan/attachments/20150409/d4cbbece/attachment.asc>