Skip to content

Commit 7381131

Browse files
author
Wu Fengguang
committed
writeback: stabilize bdi->dirty_ratelimit
There are some imperfections in balanced_dirty_ratelimit. 1) large fluctuations The dirty_rate used for computing balanced_dirty_ratelimit is merely averaged in the past 200ms (very small comparing to the 3s estimation period for write_bw), which makes rather dispersed distribution of balanced_dirty_ratelimit. It's pretty hard to average out the singular points by increasing the estimation period. Considering that the averaging technique will introduce very undesirable time lags, I give it up totally. (btw, the 3s write_bw averaging time lag is much more acceptable because its impact is one-way and therefore won't lead to oscillations.) The more practical way is filtering -- most singular balanced_dirty_ratelimit points can be filtered out by remembering some prev_balanced_rate and prev_prev_balanced_rate. However the more reliable way is to guard balanced_dirty_ratelimit with task_ratelimit. 2) due to truncates and fs redirties, the (write_bw <=> dirty_rate) match could become unbalanced, which may lead to large systematical errors in balanced_dirty_ratelimit. The truncates, due to its possibly bumpy nature, can hardly be compensated smoothly. So let's face it. When some over-estimated balanced_dirty_ratelimit brings dirty_ratelimit high, dirty pages will go higher than the setpoint. task_ratelimit will in turn become lower than dirty_ratelimit. So if we consider both balanced_dirty_ratelimit and task_ratelimit and update dirty_ratelimit only when they are on the same side of dirty_ratelimit, the systematical errors in balanced_dirty_ratelimit won't be able to bring dirty_ratelimit far away. The balanced_dirty_ratelimit estimation may also be inaccurate near @limit or @freerun, however is less an issue. 3) since we ultimately want to - keep the fluctuations of task ratelimit as small as possible - keep the dirty pages around the setpoint as long time as possible the update policy used for (2) also serves the above goals nicely: if for some reason the dirty pages are high (task_ratelimit < dirty_ratelimit), and dirty_ratelimit is low (dirty_ratelimit < balanced_dirty_ratelimit), there is no point to bring up dirty_ratelimit in a hurry only to hurt both the above two goals. So, we make use of task_ratelimit to limit the update of dirty_ratelimit in two ways: 1) avoid changing dirty rate when it's against the position control target (the adjusted rate will slow down the progress of dirty pages going back to setpoint). 2) limit the step size. task_ratelimit is changing values step by step, leaving a consistent trace comparing to the randomly jumping balanced_dirty_ratelimit. task_ratelimit also has the nice smaller errors in stable state and typically larger errors when there are big errors in rate. So it's a pretty good limiting factor for the step size of dirty_ratelimit. Note that bdi->dirty_ratelimit is always tracking balanced_dirty_ratelimit. task_ratelimit is merely used as a limiting factor. Signed-off-by: Wu Fengguang <fengguang.wu@intel.com>
1 parent be3ffa2 commit 7381131

3 files changed

Lines changed: 74 additions & 1 deletion

File tree

‎include/linux/backing-dev.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,11 @@ struct backing_dev_info {
8383
/*
8484
* The base dirty throttle rate, re-calculated on every 200ms.
8585
* All the bdi tasks' dirty rate will be curbed under it.
86+
* @dirty_ratelimit tracks the estimated @balanced_dirty_ratelimit
87+
* in small steps and is much more smooth/stable than the latter.
8688
*/
8789
unsigned long dirty_ratelimit;
90+
unsigned long balanced_dirty_ratelimit;
8891

8992
struct prop_local_percpu completions;
9093
int dirty_exceeded;

‎mm/backing-dev.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,7 @@ int bdi_init(struct backing_dev_info *bdi)
686686
bdi->bw_time_stamp = jiffies;
687687
bdi->written_stamp = 0;
688688

689+
bdi->balanced_dirty_ratelimit = INIT_BW;
689690
bdi->dirty_ratelimit = INIT_BW;
690691
bdi->write_bandwidth = INIT_BW;
691692
bdi->avg_write_bandwidth = INIT_BW;

‎mm/page-writeback.c‎

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,12 +792,17 @@ static void bdi_update_dirty_ratelimit(struct backing_dev_info *bdi,
792792
unsigned long dirtied,
793793
unsigned long elapsed)
794794
{
795+
unsigned long freerun = dirty_freerun_ceiling(thresh, bg_thresh);
796+
unsigned long limit = hard_dirty_limit(thresh);
797+
unsigned long setpoint = (freerun + limit) / 2;
795798
unsigned long write_bw = bdi->avg_write_bandwidth;
796799
unsigned long dirty_ratelimit = bdi->dirty_ratelimit;
797800
unsigned long dirty_rate;
798801
unsigned long task_ratelimit;
799802
unsigned long balanced_dirty_ratelimit;
800803
unsigned long pos_ratio;
804+
unsigned long step;
805+
unsigned long x;
801806

802807
/*
803808
* The dirty rate will match the writeout rate in long term, except
@@ -847,7 +852,71 @@ static void bdi_update_dirty_ratelimit(struct backing_dev_info *bdi,
847852
balanced_dirty_ratelimit = div_u64((u64)task_ratelimit * write_bw,
848853
dirty_rate | 1);
849854

850-
bdi->dirty_ratelimit = max(balanced_dirty_ratelimit, 1UL);
855+
/*
856+
* We could safely do this and return immediately:
857+
*
858+
* bdi->dirty_ratelimit = balanced_dirty_ratelimit;
859+
*
860+
* However to get a more stable dirty_ratelimit, the below elaborated
861+
* code makes use of task_ratelimit to filter out sigular points and
862+
* limit the step size.
863+
*
864+
* The below code essentially only uses the relative value of
865+
*
866+
* task_ratelimit - dirty_ratelimit
867+
* = (pos_ratio - 1) * dirty_ratelimit
868+
*
869+
* which reflects the direction and size of dirty position error.
870+
*/
871+
872+
/*
873+
* dirty_ratelimit will follow balanced_dirty_ratelimit iff
874+
* task_ratelimit is on the same side of dirty_ratelimit, too.
875+
* For example, when
876+
* - dirty_ratelimit > balanced_dirty_ratelimit
877+
* - dirty_ratelimit > task_ratelimit (dirty pages are above setpoint)
878+
* lowering dirty_ratelimit will help meet both the position and rate
879+
* control targets. Otherwise, don't update dirty_ratelimit if it will
880+
* only help meet the rate target. After all, what the users ultimately
881+
* feel and care are stable dirty rate and small position error.
882+
*
883+
* |task_ratelimit - dirty_ratelimit| is used to limit the step size
884+
* and filter out the sigular points of balanced_dirty_ratelimit. Which
885+
* keeps jumping around randomly and can even leap far away at times
886+
* due to the small 200ms estimation period of dirty_rate (we want to
887+
* keep that period small to reduce time lags).
888+
*/
889+
step = 0;
890+
if (dirty < setpoint) {
891+
x = min(bdi->balanced_dirty_ratelimit,
892+
min(balanced_dirty_ratelimit, task_ratelimit));
893+
if (dirty_ratelimit < x)
894+
step = x - dirty_ratelimit;
895+
} else {
896+
x = max(bdi->balanced_dirty_ratelimit,
897+
max(balanced_dirty_ratelimit, task_ratelimit));
898+
if (dirty_ratelimit > x)
899+
step = dirty_ratelimit - x;
900+
}
901+
902+
/*
903+
* Don't pursue 100% rate matching. It's impossible since the balanced
904+
* rate itself is constantly fluctuating. So decrease the track speed
905+
* when it gets close to the target. Helps eliminate pointless tremors.
906+
*/
907+
step >>= dirty_ratelimit / (2 * step + 1);
908+
/*
909+
* Limit the tracking speed to avoid overshooting.
910+
*/
911+
step = (step + 7) / 8;
912+
913+
if (dirty_ratelimit < balanced_dirty_ratelimit)
914+
dirty_ratelimit += step;
915+
else
916+
dirty_ratelimit -= step;
917+
918+
bdi->dirty_ratelimit = max(dirty_ratelimit, 1UL);
919+
bdi->balanced_dirty_ratelimit = balanced_dirty_ratelimit;
851920
}
852921

853922
void __bdi_update_bandwidth(struct backing_dev_info *bdi,

0 commit comments

Comments
 (0)