Tasks which throttle in balance_dirty_pages() will loop until the amount of
dirty memory falls below the configured dirty_ratio.

This exposes the possibility that one task could be stuck in there for
arbitrary periods of time due to page dirtying activity by other tasks.

The patch changes the logic so that tasks will break out of the loop if they
have written enough pages, regardless of the current dirty memory limits.

Here "enough" pages is 1.5x the number of pages which they just dirtied.

If the amount of dirty memory in the machine happens to still exceed
dirty_ratio (say, due to MAP_SHARED activity) then the task will again
throttle after dirtying a single page.  But there is now an upper limit on
the time for which a single task will be captured in balance_dirty_pages().


 mm/page-writeback.c |   11 +++++++++--
 1 files changed, 9 insertions(+), 2 deletions(-)

diff -puN mm/page-writeback.c~limit-write-latency mm/page-writeback.c
--- 25/mm/page-writeback.c~limit-write-latency	2003-03-01 20:55:11.000000000 -0800
+++ 25-akpm/mm/page-writeback.c	2003-03-01 20:58:39.000000000 -0800
@@ -139,6 +139,9 @@ void balance_dirty_pages(struct address_
 	struct page_state ps;
 	long background_thresh;
 	long dirty_thresh;
+	unsigned long pages_written = 0;
+	unsigned long write_chunk = sync_writeback_pages();
+
 	struct backing_dev_info *bdi = mapping->backing_dev_info;
 
 	get_dirty_limits(&ps, &background_thresh, &dirty_thresh);
@@ -147,7 +150,7 @@ void balance_dirty_pages(struct address_
 			.bdi		= bdi,
 			.sync_mode	= WB_SYNC_NONE,
 			.older_than_this = NULL,
-			.nr_to_write	= sync_writeback_pages(),
+			.nr_to_write	= write_chunk,
 		};
 
 		dirty_exceeded = 1;
@@ -158,10 +161,14 @@ void balance_dirty_pages(struct address_
 		get_dirty_limits(&ps, &background_thresh, &dirty_thresh);
 		if (ps.nr_dirty + ps.nr_writeback <= dirty_thresh)
 			break;
+		pages_written += write_chunk - wbc.nr_to_write;
+		if (pages_written >= write_chunk)
+			break;		/* We've done our duty */
 		blk_congestion_wait(WRITE, HZ/10);
 	}
 
-	dirty_exceeded = 0;
+	if (ps.nr_dirty + ps.nr_writeback <= dirty_thresh)
+		dirty_exceeded = 0;
 
 	if (!writeback_in_progress(bdi) && ps.nr_dirty > background_thresh)
 		pdflush_operation(background_writeout, 0);

_