[PATCH] writeback: Avoid division by 0 in wb_update_dirty_ratelimit()

From: Jan Kara
Date: Tue Aug 03 2021 - 08:24:32 EST


Fixup patch "writeback: Fix bandwidth estimate for spiky workload" which
introduced possibility of __wb_update_bandwidth() getting called at a
moment when 'elapsed' evaluates to 0.

Signed-off-by: Jan Kara <jack@xxxxxxx>
---
mm/page-writeback.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/mm/page-writeback.c b/mm/page-writeback.c
index d55a3a7411b3..b2fa9f20c764 100644
--- a/mm/page-writeback.c
+++ b/mm/page-writeback.c
@@ -1342,7 +1342,13 @@ static void __wb_update_bandwidth(struct dirty_throttle_control *gdtc,
unsigned long written;

spin_lock(&wb->list_lock);
- elapsed = now - wb->bw_time_stamp;
+ /*
+ * Lockless checks for elapsed time are racy and delayed update after
+ * IO completion doesn't do it at all (to make sure written pages are
+ * accounted reasonably quickly). Make sure elapsed >= 1 to avoid
+ * division errors.
+ */
+ elapsed = max(now - wb->bw_time_stamp, 1UL);
dirtied = percpu_counter_read(&wb->stat[WB_DIRTIED]);
written = wb_stat_sum(wb, WB_WRITTEN);

--
2.26.2


--4Ckj6UjgE2iN1+kY--