Skip to content

Commit bcff25f

Browse files
jankaraWu Fengguang
authored andcommitted
mm: properly reflect task dirty limits in dirty_exceeded logic
We set bdi->dirty_exceeded (and thus ratelimiting code starts to call balance_dirty_pages() every 8 pages) when a per-bdi limit is exceeded or global limit is exceeded. But per-bdi limit also depends on the task. Thus different tasks reach the limit on that bdi at different levels of dirty pages. The result is that with current code bdi->dirty_exceeded ping-ponged between 1 and 0 depending on which task just got into balance_dirty_pages(). We fix the issue by clearing bdi->dirty_exceeded only when per-bdi amount of dirty pages drops below the threshold (7/8 * bdi_dirty_limit) where task limits already do not have any influence. Impact: The end result is, the dirty pages are kept more tightly under control, with the average number slightly lowered than before. This reduces the risk to throttle light dirtiers and hence more responsive. However it may add overheads by enforcing balance_dirty_pages() calls on every 8 pages when there are 2+ heavy dirtiers. CC: Andrew Morton <akpm@linux-foundation.org> CC: Christoph Hellwig <hch@infradead.org> CC: Dave Chinner <david@fromorbit.com> CC: Peter Zijlstra <a.p.zijlstra@chello.nl> Signed-off-by: Jan Kara <jack@suse.cz> Signed-off-by: Wu Fengguang <fengguang.wu@intel.com>
1 parent fcc5c22 commit bcff25f

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

‎mm/page-writeback.c‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,13 @@ static inline void task_dirties_fraction(struct task_struct *tsk,
281281
* effectively curb the growth of dirty pages. Light dirtiers with high enough
282282
* dirty threshold may never get throttled.
283283
*/
284+
#define TASK_LIMIT_FRACTION 8
284285
static unsigned long task_dirty_limit(struct task_struct *tsk,
285286
unsigned long bdi_dirty)
286287
{
287288
long numerator, denominator;
288289
unsigned long dirty = bdi_dirty;
289-
u64 inv = dirty >> 3;
290+
u64 inv = dirty / TASK_LIMIT_FRACTION;
290291

291292
task_dirties_fraction(tsk, &numerator, &denominator);
292293
inv *= numerator;
@@ -297,6 +298,12 @@ static unsigned long task_dirty_limit(struct task_struct *tsk,
297298
return max(dirty, bdi_dirty/2);
298299
}
299300

301+
/* Minimum limit for any task */
302+
static unsigned long task_min_dirty_limit(unsigned long bdi_dirty)
303+
{
304+
return bdi_dirty - bdi_dirty / TASK_LIMIT_FRACTION;
305+
}
306+
300307
/*
301308
*
302309
*/
@@ -651,9 +658,12 @@ static void balance_dirty_pages(struct address_space *mapping,
651658
unsigned long background_thresh;
652659
unsigned long dirty_thresh;
653660
unsigned long bdi_thresh;
661+
unsigned long task_bdi_thresh;
662+
unsigned long min_task_bdi_thresh;
654663
unsigned long pages_written = 0;
655664
unsigned long pause = 1;
656665
bool dirty_exceeded = false;
666+
bool clear_dirty_exceeded = true;
657667
struct backing_dev_info *bdi = mapping->backing_dev_info;
658668
unsigned long start_time = jiffies;
659669

@@ -673,7 +683,8 @@ static void balance_dirty_pages(struct address_space *mapping,
673683
break;
674684

675685
bdi_thresh = bdi_dirty_limit(bdi, dirty_thresh);
676-
bdi_thresh = task_dirty_limit(current, bdi_thresh);
686+
min_task_bdi_thresh = task_min_dirty_limit(bdi_thresh);
687+
task_bdi_thresh = task_dirty_limit(current, bdi_thresh);
677688

678689
/*
679690
* In order to avoid the stacked BDI deadlock we need
@@ -685,7 +696,7 @@ static void balance_dirty_pages(struct address_space *mapping,
685696
* actually dirty; with m+n sitting in the percpu
686697
* deltas.
687698
*/
688-
if (bdi_thresh < 2*bdi_stat_error(bdi)) {
699+
if (task_bdi_thresh < 2 * bdi_stat_error(bdi)) {
689700
bdi_nr_reclaimable = bdi_stat_sum(bdi, BDI_RECLAIMABLE);
690701
bdi_dirty = bdi_nr_reclaimable +
691702
bdi_stat_sum(bdi, BDI_WRITEBACK);
@@ -701,8 +712,10 @@ static void balance_dirty_pages(struct address_space *mapping,
701712
* bdi or process from holding back light ones; The latter is
702713
* the last resort safeguard.
703714
*/
704-
dirty_exceeded = (bdi_dirty > bdi_thresh) ||
715+
dirty_exceeded = (bdi_dirty > task_bdi_thresh) ||
705716
(nr_dirty > dirty_thresh);
717+
clear_dirty_exceeded = (bdi_dirty <= min_task_bdi_thresh) &&
718+
(nr_dirty <= dirty_thresh);
706719

707720
if (!dirty_exceeded)
708721
break;
@@ -723,7 +736,7 @@ static void balance_dirty_pages(struct address_space *mapping,
723736
* up.
724737
*/
725738
trace_balance_dirty_start(bdi);
726-
if (bdi_nr_reclaimable > bdi_thresh) {
739+
if (bdi_nr_reclaimable > task_bdi_thresh) {
727740
pages_written += writeback_inodes_wb(&bdi->wb,
728741
write_chunk);
729742
trace_balance_dirty_written(bdi, pages_written);
@@ -766,7 +779,8 @@ static void balance_dirty_pages(struct address_space *mapping,
766779
pause = HZ / 10;
767780
}
768781

769-
if (!dirty_exceeded && bdi->dirty_exceeded)
782+
/* Clear dirty_exceeded flag only when no task can exceed the limit */
783+
if (clear_dirty_exceeded && bdi->dirty_exceeded)
770784
bdi->dirty_exceeded = 0;
771785

772786
if (writeback_in_progress(bdi))

0 commit comments

Comments
 (0)