Skip to contentSkip to conversation

kernel/power/snapshot.c: improve arithmetic divisions

PATCH Applied Wen Yang 2 participants 2 messages Jan 3, 2020 – Jan 7, 2020

Subsystems (2)
lore
Share & tools
lore

Wen Yang

author
#0 lore raw

PATCH 1/1 kernel/power/snapshot.c: improve arithmetic divisions

do_div() does a 64-by-32 division. Use div64_u64() instead of do_div() if the divisor is u64, to avoid truncation to 32-bit. This change also cleans up code a tad.

---
 kernel/power/snapshot.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
kernel/power/snapshot.c +1 -3
index 26b9168..8a6eaf7 100644
@@ -1566,9 +1566,7 @@ static unsigned long preallocate_image_highmem(unsigned long nr_pages)
  */
 static unsigned long __fraction(u64 x, u64 multiplier, u64 base)
 {
-	x *= multiplier;
-	do_div(x, base);
-	return (unsigned long)x;
+	return div64_u64(x * multiplier, base);
 }
 
 static unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
Signature · 1 line
1.8.3.1
maintainer Applied
#1 lore raw
Quoted 21 lines · Wen Yang

do_div() does a 64-by-32 division. Use div64_u64() instead of do_div() if the divisor is u64, to avoid truncation to 32-bit. This change also cleans up code a tad.

---
 kernel/power/snapshot.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
kernel/power/snapshot.c +0 -0
index 26b9168..8a6eaf7 100644
@@ -1566,9 +1566,7 @@ static unsigned long preallocate_image_highmem(unsigned long nr_pages)
  */

kernel/power/snapshot.c static unsigned long preallocate_image_highmem(unsigned long nr_pages)

Quoted 10 lines
kernel/power/snapshot.c +1 -4
 static unsigned long __fraction(u64 x, u64 multiplier, u64 base)
 {
-       x *= multiplier;
-       do_div(x, base);
-       return (unsigned long)x;
+       return div64_u64(x * multiplier, base);
 }
 
 static unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
--

Applied as 5.6 material under a new subject ("PM: hibernate: improve arithmetic division in preallocate_highmem_fraction()"), thanks!