<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">

Signed-off-by: Andrew Morton &lt;akpm@osdl.org&gt;
---

 25-akpm/include/linux/mmzone.h |    5 +++--
 25-akpm/mm/vmscan.c            |   27 ++++++++++++++++++++++-----
 2 files changed, 25 insertions(+), 7 deletions(-)

diff -puN mm/vmscan.c~no-wild-kswapd-2 mm/vmscan.c
--- 25/mm/vmscan.c~no-wild-kswapd-2	2004-10-07 23:46:22.870851856 -0700
+++ 25-akpm/mm/vmscan.c	2004-10-07 23:46:22.876850944 -0700
@@ -990,6 +990,17 @@ out:
  * of the number of free pages in the lower zones.  This interoperates with
  * the page allocator fallback scheme to ensure that aging of pages is balanced
  * across the zones.
+ *
+ * kswapd can be semi-livelocked if some other process is allocating pages
+ * while kswapd is simultaneously trying to balance the same zone.  That's OK,
+ * because we _want_ kswapd to work continuously in this situation.  But a
+ * side-effect of kswapd's ongoing work is that the pageout priority keeps on
+ * winding up so we bogusly start doing swapout.
+ *
+ * To fix this we take a snapshot of the number of pages which need to be
+ * reclaimed from each zone in zone-&gt;pages_to_reclaim and never reclaim more
+ * pages than that.  Once the required number of pages have been reclaimed from
+ * each zone, we're done.  kwsapd will go back to sleep until someone wakes it.
  */
 static int balance_pgdat(pg_data_t *pgdat, int nr_pages)
 {
@@ -1010,6 +1021,7 @@ static int balance_pgdat(pg_data_t *pgda
 		struct zone *zone = pgdat-&gt;node_zones + i;
 
 		zone-&gt;temp_priority = DEF_PRIORITY;
+		zone-&gt;pages_to_reclaim = zone-&gt;pages_high - zone-&gt;free_pages;
 	}
 
 	for (priority = DEF_PRIORITY; priority &gt;= 0; priority--) {
@@ -1032,7 +1044,7 @@ static int balance_pgdat(pg_data_t *pgda
 						priority != DEF_PRIORITY)
 					continue;
 
-				if (zone-&gt;free_pages &lt;= zone-&gt;pages_high) {
+				if (zone-&gt;pages_to_reclaim &gt; 0) {
 					end_zone = i;
 					break;
 				}
@@ -1068,10 +1080,11 @@ static int balance_pgdat(pg_data_t *pgda
 			if (zone-&gt;all_unreclaimable &amp;&amp; priority != DEF_PRIORITY)
 				continue;
 
-			if (nr_pages == 0) {	/* Not software suspend */
-				if (zone-&gt;free_pages &lt;= zone-&gt;pages_high)
-					all_zones_ok = 0;
-			}
+			if (zone-&gt;pages_to_reclaim &lt;= 0)
+				continue;
+
+			if (nr_pages == 0)	/* Not software suspend */
+				all_zones_ok = 0;
 			zone-&gt;temp_priority = priority;
 			if (zone-&gt;prev_priority &gt; priority)
 				zone-&gt;prev_priority = priority;
@@ -1081,6 +1094,10 @@ static int balance_pgdat(pg_data_t *pgda
 			shrink_zone(zone, &amp;sc);
 			reclaim_state-&gt;reclaimed_slab = 0;
 			shrink_slab(sc.nr_scanned, GFP_KERNEL, lru_pages);
+
+			/* This fails to account for slab reclaim */
+			zone-&gt;pages_to_reclaim -= sc.nr_reclaimed;
+
 			sc.nr_reclaimed += reclaim_state-&gt;reclaimed_slab;
 			total_reclaimed += sc.nr_reclaimed;
 			total_scanned += sc.nr_scanned;
diff -puN include/linux/mmzone.h~no-wild-kswapd-2 include/linux/mmzone.h
--- 25/include/linux/mmzone.h~no-wild-kswapd-2	2004-10-07 23:46:22.872851552 -0700
+++ 25-akpm/include/linux/mmzone.h	2004-10-07 23:46:22.877850792 -0700
@@ -137,8 +137,9 @@ struct zone {
 	unsigned long		nr_scan_inactive;
 	unsigned long		nr_active;
 	unsigned long		nr_inactive;
-	int			all_unreclaimable; /* All pages pinned */
-	unsigned long		pages_scanned;	   /* since last reclaim */
+	long			pages_to_reclaim;	/* kswapd usage */
+	int			all_unreclaimable;	/* All pages pinned */
+	unsigned long		pages_scanned;		/* since last reclaim */
 
 	ZONE_PADDING(_pad2_)
 
_
</pre></body></html>