Skip to contentSkip to conversation

Fix link error in 32bit arch because of 64bit division

PATCH Superseded Vishwanath Pai 3 participants 3 messages Sep 27, 2016

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix link error in 32bit arch because of 64bit division

Fix link error in 32bit arch because of 64bit division

Division of 64bit integers will cause linker error undefined reference to `__udivdi3'. Fix this by replacing divisions with div64_64

---
 net/netfilter/xt_hashlimit.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
net/netfilter/xt_hashlimit.c +8 -7
index 44a095e..7fc694e 100644
@@ -465,19 +465,20 @@ static u64 user2credits(u64 user, int revision)
 {
 	if (revision == 1) {
 		/* If multiplying would overflow... */
-		if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY_v1))
+		if (user > div64_u64(0xFFFFFFFF, (HZ*CREDITS_PER_JIFFY_v1)))
 			/* Divide first. */
-			return (user / XT_HASHLIMIT_SCALE) *\
+			return div64_u64(user, XT_HASHLIMIT_SCALE) *\
 						HZ * CREDITS_PER_JIFFY_v1;
 
-		return (user * HZ * CREDITS_PER_JIFFY_v1) \
-						/ XT_HASHLIMIT_SCALE;
+		return div64_u64((user * HZ * CREDITS_PER_JIFFY_v1),
+				  XT_HASHLIMIT_SCALE);
 	} else {
-		if (user > 0xFFFFFFFFFFFFFFFF / (HZ*CREDITS_PER_JIFFY))
-			return (user / XT_HASHLIMIT_SCALE_v2) *\
+		if (user > div64_u64(0xFFFFFFFFFFFFFFFF, (HZ*CREDITS_PER_JIFFY)))
+			return div64_u64(user, XT_HASHLIMIT_SCALE_v2) *\
 						HZ * CREDITS_PER_JIFFY;
 
-		return (user * HZ * CREDITS_PER_JIFFY) / XT_HASHLIMIT_SCALE_v2;
+		return div64_u64((user * HZ * CREDITS_PER_JIFFY),
+				 XT_HASHLIMIT_SCALE_v2);
 	}
 }
 
Signature · 1 line
1.9.1
#1 lore raw

Hi Vishwanath Pai,

2016-09-27 15:42 GMT+08:00 Vishwanath Pai <[email protected]>:

Quoted 1 line

Fix link error in 32bit arch because of 64bit division

This should be "netfilter: xt_hashlimit: fix ... "

net/netfilter/xt_hashlimit.c static u64 user2credits(u64 user, int revision) · line 468

Quoted 9 lines
net/netfilter/xt_hashlimit.c +1 -1
@@ -465,19 +465,20 @@ static u64 user2credits(u64 user, int revision)
 {
        if (revision == 1) {
                /* If multiplying would overflow... */
-               if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY_v1))
+               if (user > div64_u64(0xFFFFFFFF, (HZ*CREDITS_PER_JIFFY_v1)))

Here divisor and dividend are all 32-bit integer, so covert "/" to div64_u64 seems unnecessary.

net/netfilter/xt_hashlimit.c static u64 user2credits(u64 user, int revision) · line 476

Quoted 13 lines
net/netfilter/xt_hashlimit.c +4 -5
                        /* Divide first. */
-                       return (user / XT_HASHLIMIT_SCALE) *\
+                       return div64_u64(user, XT_HASHLIMIT_SCALE) *\
                                                HZ * CREDITS_PER_JIFFY_v1;
 
-               return (user * HZ * CREDITS_PER_JIFFY_v1) \
-                                               / XT_HASHLIMIT_SCALE;
+               return div64_u64((user * HZ * CREDITS_PER_JIFFY_v1),
+                                 XT_HASHLIMIT_SCALE);
        } else {
-               if (user > 0xFFFFFFFFFFFFFFFF / (HZ*CREDITS_PER_JIFFY))
-                       return (user / XT_HASHLIMIT_SCALE_v2) *\
+               if (user > div64_u64(0xFFFFFFFFFFFFFFFF, (HZ*CREDITS_PER_JIFFY)))

0xFFFFFFFFFFFFFFFF and "HZ*CREDITS_PER_JIFFY" are both constant, and GCC will do constant folding optimization, so I think convert "/" to div64_u64 here is also unnecessary.

Quoted 9 lines
net/netfilter/xt_hashlimit.c +3 -1
+                       return div64_u64(user, XT_HASHLIMIT_SCALE_v2) *\
                                                HZ * CREDITS_PER_JIFFY;
 
-               return (user * HZ * CREDITS_PER_JIFFY) / XT_HASHLIMIT_SCALE_v2;
+               return div64_u64((user * HZ * CREDITS_PER_JIFFY),
+                                XT_HASHLIMIT_SCALE_v2);
        }
 }
 
#2 lore raw
In reply to Vishwanath Pai
Quoted 16 lines · Vishwanath Pai

Fix link error in 32bit arch because of 64bit division

Division of 64bit integers will cause linker error undefined reference to `__udivdi3'. Fix this by replacing divisions with div64_64

---
 net/netfilter/xt_hashlimit.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
net/netfilter/xt_hashlimit.c +0 -0
index 44a095e..7fc694e 100644

net/netfilter/xt_hashlimit.c static u64 user2credits(u64 user, int revision) · line 468

Quoted 5 lines
net/netfilter/xt_hashlimit.c +1 -1
@@ -465,19 +465,20 @@ static u64 user2credits(u64 user, int revision)
 {
 	if (revision == 1) {
 		/* If multiplying would overflow... */
-		if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY_v1))
+		if (user > div64_u64(0xFFFFFFFF, (HZ*CREDITS_PER_JIFFY_v1)))

How can this be needed ? 0xFFFFFFFF is 32bits, compiler knows how to compute 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY_v1) itself, without using a 64 bit divide !

Please be selective.

net/netfilter/xt_hashlimit.c static u64 user2credits(u64 user, int revision) · line 477

Quoted 12 lines
net/netfilter/xt_hashlimit.c +3 -5
 			/* Divide first. */
-			return (user / XT_HASHLIMIT_SCALE) *\
+			return div64_u64(user, XT_HASHLIMIT_SCALE) *\
 						HZ * CREDITS_PER_JIFFY_v1;
 
-		return (user * HZ * CREDITS_PER_JIFFY_v1) \
-						/ XT_HASHLIMIT_SCALE;
+		return div64_u64((user * HZ * CREDITS_PER_JIFFY_v1),
+				  XT_HASHLIMIT_SCALE);
 	} else {
-		if (user > 0xFFFFFFFFFFFFFFFF / (HZ*CREDITS_PER_JIFFY))
-			return (user / XT_HASHLIMIT_SCALE_v2) *\

Probably same remark here.

Quoted 10 lines
net/netfilter/xt_hashlimit.c +4 -1
+		if (user > div64_u64(0xFFFFFFFFFFFFFFFF, (HZ*CREDITS_PER_JIFFY)))
+			return div64_u64(user, XT_HASHLIMIT_SCALE_v2) *\
 						HZ * CREDITS_PER_JIFFY;
 
-		return (user * HZ * CREDITS_PER_JIFFY) / XT_HASHLIMIT_SCALE_v2;
+		return div64_u64((user * HZ * CREDITS_PER_JIFFY),
+				 XT_HASHLIMIT_SCALE_v2);
 	}
 }