Skip to contentSkip to conversation

mm: Handle misplaced anon pages

tip:sched/numa tip-bot for Lee Schermerhorn 1 participant 1 message May 18, 2012

Subsystems (3)
lore
Share & tools
lore
#0 lore raw
Commit-ID:  65699050e8aae41078c9c73f61f6fae26e07e461
Gitweb:     http://git.kernel.org/tip/65699050e8aae41078c9c73f61f6fae26e07e461
Author:     Lee Schermerhorn <[email protected]>
AuthorDate: Thu, 12 Jan 2012 12:05:17 +0100
Committer:  Ingo Molnar <[email protected]>
CommitDate: Fri, 18 May 2012 08:16:18 +0200

mm: Handle misplaced anon pages

This patch simply hooks the anon page fault handler [do_swap_page()] to check for and migrate misplaced pages if enabled and page won't be "COWed".

This introduces can_reuse_swap_page() since reuse_swap_page() does delete_from_swap_cache() which messes our migration path (since that assumes its still a swapcache page).

[ removed the retry loops after lock_page on a swapcache which tried
to fixup the wreckage caused by ignoring the page count on migate; added can_reuse_swap_page(); moved the migrate-on-fault enabled test into check_migrate_misplaced_page() ]

Cc Suresh Siddha <[email protected]>
Cc Paul Turner <[email protected]>
Cc Dan Smith <[email protected]>
Cc Bharata B Rao <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 include/linux/swap.h |    4 +++-
 mm/memory.c          |   17 +++++++++++++++++
 mm/swapfile.c        |   13 +++++++++++++
 3 files changed, 33 insertions(+), 1 deletions(-)
include/linux/swap.h +3 -1
index b1fd5c7..0c23738 100644
@@ -352,6 +352,7 @@ extern unsigned int count_swap_pages(int, int);
 extern sector_t map_swap_page(struct page *, struct block_device **);
 extern sector_t swapdev_block(int, pgoff_t);
 extern int reuse_swap_page(struct page *);
+extern int can_reuse_swap_page(struct page *);
 extern int try_to_free_swap(struct page *);
 struct backing_dev_info;
 
@@ -462,7 +463,8 @@ static inline void delete_from_swap_cache(struct page *page)
 {
 }
 
-#define reuse_swap_page(page)	(page_mapcount(page) == 1)
+#define reuse_swap_page(page)		(page_mapcount(page) == 1)
+#define can_reuse_swap_page(page)	(page_mapcount(page) == 1)
 
 static inline int try_to_free_swap(struct page *page)
 {
mm/memory.c +17 -0
index 6105f47..08a3489 100644
@@ -57,6 +57,7 @@
 #include <linux/swapops.h>
 #include <linux/elf.h>
 #include <linux/gfp.h>
+#include <linux/mempolicy.h>	/* check_migrate_misplaced_page() */
 
 #include <asm/io.h>
 #include <asm/pgalloc.h>
@@ -2974,6 +2975,22 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
 	}
 
 	/*
+	 * No sense in migrating a page that will be "COWed" as the new
+	 * new page will be allocated according to effective mempolicy.
+	 */
+	if ((flags & FAULT_FLAG_WRITE) && can_reuse_swap_page(page)) {
+		/*
+		 * check for misplacement and migrate, if necessary/possible,
+		 * here and now.  Note that if we're racing with another thread,
+		 * we may end up discarding the migrated page after locking
+		 * the page table and checking the pte below.  However, we
+		 * don't want to hold the page table locked over migration, so
+		 * we'll live with that [unlikely, one hopes] possibility.
+		 */
+		page = check_migrate_misplaced_page(page, vma, address);
+	}
+
+	/*
 	 * Back out if somebody else already faulted in this pte.
 	 */
 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
mm/swapfile.c +13 -0
index fafc26d..c5952c0 100644
@@ -640,6 +640,19 @@ int reuse_swap_page(struct page *page)
 	return count <= 1;
 }
 
+int can_reuse_swap_page(struct page *page)
+{
+	int count;
+
+	VM_BUG_ON(!PageLocked(page));
+	if (unlikely(PageKsm(page)))
+		return 0;
+	count = page_mapcount(page);
+	if (count <= 1 && PageSwapCache(page))
+		count += page_swapcount(page);
+	return count <= 1;
+}
+
 /*
  * If swap is getting full, or if there are no more mappings of this page,
  * then try_to_free_swap is called to free its swap space.