Subject:  first time swap use results in heavy swapping
From: Gerald Schaefer <geraldsc@de.ibm.com>
Patch-mainline: no
References: bnc#713134

Symptom: System temporarily hangs for a period of time while a lot of
             I/O is done to the swap device
Problem: System z is different in respect to the referenced bits
             compared to other architectures. On e.g. x86 a page without
             an active mapper is not referenced by definition, on System z
             the referenced bit in the storage key can indicate that the
             page is referenced even without an active mapper. The first
             time a system with large amounts of anonymous memory runs
             into memory shortage all pages on the active / inactive
             lists will be found referenced on System z while only the
             mapped pages appear to be referenced on x86. This causes the
             memory management on System z to do a full sweep over all
             page cache pages and start writeback for all of them.
Solution: Follow the x86 model and only check the referenced bit in
             the storage key if the page has active mappers. That will
             cause some of the pages in the page cache to appear to be
             non-referenced and the page scan will stop in time.

Signed-off-by: Gerald Schaefer <geraldsc@de.ibm.com>
Acked-by: Torsten Duwe <duwe@suse.de>

---
 mm/rmap.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -870,10 +870,11 @@ int page_referenced(struct page *page,
 								vm_flags);
 		if (we_locked)
 			unlock_page(page);
+
+		if (page_test_and_clear_young(page_to_pfn(page)))
+			referenced++;
 	}
 out:
-	if (page_test_and_clear_young(page_to_pfn(page)))
-		referenced++;
 
 	return referenced;
 }
