All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/3] use div64_ul() instead of div_u64() if the divisor is unsigned long
@ 2020-01-02  8:14 Wen Yang
  2020-01-02  8:14 ` [PATCH 1/3] mm/page-writeback.c: avoid potential division by zero in wb_min_max_ratio() Wen Yang
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Wen Yang @ 2020-01-02  8:14 UTC (permalink / raw)
  To: Andrew Morton, Qian Cai
  Cc: xlpang, Wen Yang, Tejun Heo, Jens Axboe, linux-mm, linux-kernel

We were first inspired by commit b0ab99e7736a
("sched: Fix possible divide by zero in avg_atom () calculation"),
then refer to the recently analyzed mm code,
we found this suspicious place.
 201                 if (min) {
 202                         min *= this_bw;
 203                         do_div(min, tot_bw);
 204                 }

And we also disassembled and confirmed it:
/usr/src/debug/kernel-4.9.168-016.ali3000/linux-4.9.168-016.ali3000.alios7.x86_64/mm/page-writeback.c: 201
0xffffffff811c37da <__wb_calc_thresh+234>:      xor    %r10d,%r10d
0xffffffff811c37dd <__wb_calc_thresh+237>:      test   %rax,%rax
0xffffffff811c37e0 <__wb_calc_thresh+240>:      je 0xffffffff811c3800 <__wb_calc_thresh+272>
/usr/src/debug/kernel-4.9.168-016.ali3000/linux-4.9.168-016.ali3000.alios7.x86_64/mm/page-writeback.c: 202
0xffffffff811c37e2 <__wb_calc_thresh+242>:      imul   %r8,%rax
/usr/src/debug/kernel-4.9.168-016.ali3000/linux-4.9.168-016.ali3000.alios7.x86_64/mm/page-writeback.c: 203
0xffffffff811c37e6 <__wb_calc_thresh+246>:      mov    %r9d,%r10d    ---> truncates it to 32 bits here
0xffffffff811c37e9 <__wb_calc_thresh+249>:      xor    %edx,%edx
0xffffffff811c37eb <__wb_calc_thresh+251>:      div    %r10
0xffffffff811c37ee <__wb_calc_thresh+254>:      imul   %rbx,%rax
0xffffffff811c37f2 <__wb_calc_thresh+258>:      shr    $0x2,%rax
0xffffffff811c37f6 <__wb_calc_thresh+262>:      mul    %rcx
0xffffffff811c37f9 <__wb_calc_thresh+265>:      shr    $0x2,%rdx
0xffffffff811c37fd <__wb_calc_thresh+269>:      mov    %rdx,%r10

This series use div64_ul() instead of div_u64() if the divisor
is unsigned long, to avoid truncation to 32-bit on 64-bit platforms.

Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Qian Cai <cai@lca.pw>
Cc: Tejun Heo <tj@kernel.org>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org

Wen Yang (3):
  mm/page-writeback.c: avoid potential division by zero in
    wb_min_max_ratio()
  mm/page-writeback.c: use div64_ul() for u64-by-unsigned-long divide
  mm/page-writeback.c: improve arithmetic divisions

 mm/page-writeback.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

-- 
1.8.3.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] mm/page-writeback.c: avoid potential division by zero in wb_min_max_ratio()
  2020-01-02  8:14 [PATCH 0/3] use div64_ul() instead of div_u64() if the divisor is unsigned long Wen Yang
@ 2020-01-02  8:14 ` Wen Yang
  2020-01-02  8:14 ` [PATCH 2/3] mm/page-writeback.c: use div64_ul() for u64-by-unsigned-long divide Wen Yang
  2020-01-02  8:14 ` [PATCH 3/3] mm/page-writeback.c: improve arithmetic divisions Wen Yang
  2 siblings, 0 replies; 4+ messages in thread
From: Wen Yang @ 2020-01-02  8:14 UTC (permalink / raw)
  To: Andrew Morton, Qian Cai
  Cc: xlpang, Wen Yang, Tejun Heo, Jens Axboe, linux-mm, linux-kernel

The variables 'min' and 'max' are unsigned long and
do_div truncates them to 32 bits, which means it can test
non-zero and be truncated to zero for division.
Fix this issue by using div64_ul() instead.

Fixes: 693108a8a667 ("writeback: make bdi->min/max_ratio handling cgroup writeback aware")
Signed-off-by: Wen Yang <wenyang@linux.alibaba.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Qian Cai <cai@lca.pw>
Cc: Tejun Heo <tj@kernel.org>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org
---
 mm/page-writeback.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mm/page-writeback.c b/mm/page-writeback.c
index 50055d2..2d658b2 100644
--- a/mm/page-writeback.c
+++ b/mm/page-writeback.c
@@ -201,11 +201,11 @@ static void wb_min_max_ratio(struct bdi_writeback *wb,
 	if (this_bw < tot_bw) {
 		if (min) {
 			min *= this_bw;
-			do_div(min, tot_bw);
+			min = div64_ul(min, tot_bw);
 		}
 		if (max < 100) {
 			max *= this_bw;
-			do_div(max, tot_bw);
+			max = div64_ul(max, tot_bw);
 		}
 	}
 
-- 
1.8.3.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/3] mm/page-writeback.c: use div64_ul() for u64-by-unsigned-long divide
  2020-01-02  8:14 [PATCH 0/3] use div64_ul() instead of div_u64() if the divisor is unsigned long Wen Yang
  2020-01-02  8:14 ` [PATCH 1/3] mm/page-writeback.c: avoid potential division by zero in wb_min_max_ratio() Wen Yang
@ 2020-01-02  8:14 ` Wen Yang
  2020-01-02  8:14 ` [PATCH 3/3] mm/page-writeback.c: improve arithmetic divisions Wen Yang
  2 siblings, 0 replies; 4+ messages in thread
From: Wen Yang @ 2020-01-02  8:14 UTC (permalink / raw)
  To: Andrew Morton, Qian Cai
  Cc: xlpang, Wen Yang, Tejun Heo, Jens Axboe, linux-mm, linux-kernel

The two variables 'numerator' and 'denominator',
though they are declared as long, they should actually be
unsigned long (according to the implementation of the
fprop_fraction_percpu() function)

And do_div() does a 64-by-32 division, while the divisor
'denominator' is unsigned long, thus 64-bit on 64-bit platforms.
Hence the proper function to call is div64_ul().

Signed-off-by: Wen Yang <wenyang@linux.alibaba.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Qian Cai <cai@lca.pw>
Cc: Tejun Heo <tj@kernel.org>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org
---
 mm/page-writeback.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mm/page-writeback.c b/mm/page-writeback.c
index 2d658b2..c74c6bd 100644
--- a/mm/page-writeback.c
+++ b/mm/page-writeback.c
@@ -766,7 +766,7 @@ static unsigned long __wb_calc_thresh(struct dirty_throttle_control *dtc)
 	struct wb_domain *dom = dtc_dom(dtc);
 	unsigned long thresh = dtc->thresh;
 	u64 wb_thresh;
-	long numerator, denominator;
+	unsigned long numerator, denominator;
 	unsigned long wb_min_ratio, wb_max_ratio;
 
 	/*
@@ -777,7 +777,7 @@ static unsigned long __wb_calc_thresh(struct dirty_throttle_control *dtc)
 
 	wb_thresh = (thresh * (100 - bdi_min_ratio)) / 100;
 	wb_thresh *= numerator;
-	do_div(wb_thresh, denominator);
+	wb_thresh = div64_ul(wb_thresh, denominator);
 
 	wb_min_max_ratio(dtc->wb, &wb_min_ratio, &wb_max_ratio);
 
-- 
1.8.3.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 3/3] mm/page-writeback.c: improve arithmetic divisions
  2020-01-02  8:14 [PATCH 0/3] use div64_ul() instead of div_u64() if the divisor is unsigned long Wen Yang
  2020-01-02  8:14 ` [PATCH 1/3] mm/page-writeback.c: avoid potential division by zero in wb_min_max_ratio() Wen Yang
  2020-01-02  8:14 ` [PATCH 2/3] mm/page-writeback.c: use div64_ul() for u64-by-unsigned-long divide Wen Yang
@ 2020-01-02  8:14 ` Wen Yang
  2 siblings, 0 replies; 4+ messages in thread
From: Wen Yang @ 2020-01-02  8:14 UTC (permalink / raw)
  To: Andrew Morton, Qian Cai
  Cc: xlpang, Wen Yang, Tejun Heo, Jens Axboe, linux-mm, linux-kernel

Use div64_ul() instead of do_div() if the divisor is unsigned long,
to avoid truncation to 32-bit on 64-bit platforms.

Signed-off-by: Wen Yang <wenyang@linux.alibaba.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Qian Cai <cai@lca.pw>
Cc: Tejun Heo <tj@kernel.org>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org
---
 mm/page-writeback.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/page-writeback.c b/mm/page-writeback.c
index c74c6bd..2caf780 100644
--- a/mm/page-writeback.c
+++ b/mm/page-writeback.c
@@ -1102,7 +1102,7 @@ static void wb_update_write_bandwidth(struct bdi_writeback *wb,
 	bw = written - min(written, wb->written_stamp);
 	bw *= HZ;
 	if (unlikely(elapsed > period)) {
-		do_div(bw, elapsed);
+		bw = div64_ul(bw, elapsed);
 		avg = bw;
 		goto out;
 	}
-- 
1.8.3.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-01-02  8:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-02  8:14 [PATCH 0/3] use div64_ul() instead of div_u64() if the divisor is unsigned long Wen Yang
2020-01-02  8:14 ` [PATCH 1/3] mm/page-writeback.c: avoid potential division by zero in wb_min_max_ratio() Wen Yang
2020-01-02  8:14 ` [PATCH 2/3] mm/page-writeback.c: use div64_ul() for u64-by-unsigned-long divide Wen Yang
2020-01-02  8:14 ` [PATCH 3/3] mm/page-writeback.c: improve arithmetic divisions Wen Yang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.