Skip to contentSkip to conversation

vmscan: doing page_referenced() in batch way

RFC In review Shaohua Li 3 participants 5 messages Sep 29, 2010 – Oct 13, 2010

Subsystems (6)
lore
Share & tools
lore

Shaohua Li

author
#0 lore raw

RFC 1/1 vmscan: doing page_referenced() in batch way

when memory pressure is high, page_referenced() causes a lot of lock contention for anon_vma->lock or mapping->i_mmap_lock. Considering pages from one file usually live side by side in LRU list, we can lock several pages in shrink_page_list() and do batch page_referenced() to avoid some lock/unlock, which should reduce lock contention a lot. The locking rule documented in rmap.c is:
page_lock

	mapping->i_mmap_lock
		anon_vma->lock

For a batch of pages, we do page lock for all of them first and check their reference, and then release their i_mmap_lock or anon_vma lock. This seems not break the rule to me.
Before I further polish the patch, I'd like to know if there is anything preventing us to do such batch here.

Thanks,
Shaohua

---
 include/linux/rmap.h |    7 ++--
 mm/internal.h        |   11 ++++++
 mm/memory-failure.c  |    2 -
 mm/rmap.c            |   60 ++++++++++++++++++++++++++++------
 mm/vmscan.c          |   88 ++++++++++++++++++++++++++++++++++++++++++++++-----
 5 files changed, 147 insertions(+), 21 deletions(-)
include/linux/rmap.h +5 -2
Index: linux/include/linux/rmap.h
===================================================================
@@ -181,8 +181,10 @@ static inline void page_dup_rmap(struct 
 /*
  * Called from mm/vmscan.c to handle paging out
  */
+struct page_reference_control;
 int page_referenced(struct page *, int is_locked,
-			struct mem_cgroup *cnt, unsigned long *vm_flags);
+			struct mem_cgroup *cnt, unsigned long *vm_flags,
+			struct page_reference_control *prc);
 int page_referenced_one(struct page *, struct vm_area_struct *,
 	unsigned long address, unsigned int *mapcount, unsigned long *vm_flags);
 
@@ -230,7 +232,8 @@ int try_to_munlock(struct page *);
 /*
  * Called by memory-failure.c to kill processes.
  */
-struct anon_vma *page_lock_anon_vma(struct page *page);
+struct anon_vma *page_lock_anon_vma(struct page *page,
+	struct page_reference_control *prc);
 void page_unlock_anon_vma(struct anon_vma *anon_vma);
 int page_mapped_in_vma(struct page *page, struct vm_area_struct *vma);
 
mm/internal.h +11 -0
Index: linux/mm/internal.h
===================================================================
@@ -249,6 +249,17 @@ int __get_user_pages(struct task_struct 
 #define ZONE_RECLAIM_FULL	-1
 #define ZONE_RECLAIM_SOME	0
 #define ZONE_RECLAIM_SUCCESS	1
+
+#define PRC_PAGE_NUM 8
+struct page_reference_control {
+	int num;
+	struct page *pages[PRC_PAGE_NUM];
+	int references[PRC_PAGE_NUM];
+	struct anon_vma *anon_vma;
+	struct address_space *mapping;
+	/* no ksm */
+};
+
 #endif
 
 extern int hwpoison_filter(struct page *p);
mm/rmap.c +49 -11

129 lines not on this page · raw message

mm/vmscan.c +81 -7

143 lines not on this page · raw message

mm/memory-failure.c +1 -1
Index: linux/mm/memory-failure.c
===================================================================
@@ -382,7 +382,7 @@ static void collect_procs_anon(struct pa
 	struct anon_vma *av;
 
 	read_lock(&tasklist_lock);
-	av = page_lock_anon_vma(page);
+	av = page_lock_anon_vma(page, NULL);
 	if (av == NULL)	/* Not actually mapped anymore */
 		goto out;
 	for_each_process (tsk) {

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#1 lore raw
Quoted 7 lines · On Wed, 29 Sep 2010 10:57:33 +0800 Shaohua Li <[email protected]> wrote:

when memory pressure is high, page_referenced() causes a lot of lock contention for anon_vma->lock or mapping->i_mmap_lock. Considering pages from one file usually live side by side in LRU list, we can lock several pages in shrink_page_list() and do batch page_referenced() to avoid some lock/unlock, which should reduce lock contention a lot. The locking rule documented in rmap.c is:
page_lock

Quoted 7 lines
	mapping->i_mmap_lock
		anon_vma->lock

For a batch of pages, we do page lock for all of them first and check their reference, and then release their i_mmap_lock or anon_vma lock. This seems not break the rule to me.
Before I further polish the patch, I'd like to know if there is anything preventing us to do such batch here.

The patch adds quite a bit of complexity, so we'd need to see benchmark testing results which justify it, please.

Also, the entire patch is irrelevant for uniprocessor machines, so the runtime overhead and code-size increases for CONFIG_SMP=n builds should be as low as possible - ideally zero. Please quantify this as well within the changelog if you pursue this work.

mm/internal.h int __get_user_pages(struct task_struct · line 261

Quoted 12 lines

...

mm/internal.h +9 -0
+#define PRC_PAGE_NUM 8
+struct page_reference_control {
+	int num;
+	struct page *pages[PRC_PAGE_NUM];
+	int references[PRC_PAGE_NUM];
+	struct anon_vma *anon_vma;
+	struct address_space *mapping;
+	/* no ksm */
+};

hm, 120 bytes of stack consumed, deep in page reclaim.

Quoted 26 lines

#endif

extern int hwpoison_filter(struct page *p);

...

static int page_referenced_file(struct page *page,

mm/rmap.c +9 -2
 				struct mem_cgroup *mem_cont,
-				unsigned long *vm_flags)
+				unsigned long *vm_flags,
+				struct page_reference_control *prc)
 {
 	unsigned int mapcount;
 	struct address_space *mapping = page->mapping;
@@ -603,8 +623,25 @@ static int page_referenced_file(struct p
 	 */
 	BUG_ON(!PageLocked(page));
 
-	spin_lock(&mapping->i_mmap_lock);
+	if (prc) {
+		if (mapping == prc->mapping) {
+			goto skip_lock;
+		}
+		if (prc->anon_vma) {
+			page_unlock_anon_vma(prc->anon_vma);
+			prc->anon_vma = NULL;

mm/rmap.c static int page_referenced_file(struct p · line 642

Quoted 10 lines
mm/rmap.c +10 -0
+		}
+		if (prc->mapping) {
+			spin_unlock(&prc->mapping->i_mmap_lock);
+			prc->mapping = NULL;
+		}
+		prc->mapping = mapping;
+
+		spin_lock(&mapping->i_mmap_lock);
+	} else
+		spin_lock(&mapping->i_mmap_lock);

Move the spin_lock() outside, remove the `else' part.

Quoted 30 lines
mm/rmap.c +16 -1
+skip_lock:
 	/*
 	 * i_mmap_lock does not stabilize mapcount at all, but mapcount
 	 * is more likely to be accurate if we note it after spinning.
@@ -628,7 +665,8 @@ static int page_referenced_file(struct p
 			break;
 	}
 
-	spin_unlock(&mapping->i_mmap_lock);
+	if (!prc)
+		spin_unlock(&mapping->i_mmap_lock);
 	return referenced;
 }
 
 
...
 
+static void do_prc_batch(struct scan_control *sc,
+	struct page_reference_control *prc)
+{
+	int i;
+	for (i = 0; i < prc->num; i++)
+		prc->references[i] = page_check_references(prc->pages[i], sc,
+			prc);
+	/*
+	 * we must release all locks here, the lock ordering requries
+	 * pagelock->
+	 *   mapping->i_mmap_lock->
+	 *     anon_vma->lock
+	 * release lock guarantee we don't break the rule in next run

mm/rmap.c static int page_referenced_file(struct p

Quoted 10 lines
mm/rmap.c +10 -0
+	 */
+	if (prc->anon_vma) {
+		page_unlock_anon_vma(prc->anon_vma);
+		prc->anon_vma = NULL;
+	}
+	if (prc->mapping) {
+		spin_unlock(&prc->mapping->i_mmap_lock);
+		prc->mapping = NULL;
+	}
+}

I didn't check the locking alterations.

mm/vmscan.c static noinline_for_stack void free_page · line 673

Quoted 6 lines
mm/vmscan.c +6 -0
+static int page_check_references_batch(struct page *page, struct scan_control *sc,
+	struct page_reference_control *prc)
+{
+	/* bypass ksm pages */
+	if (PageKsm(page))
+		return 1;

A general point about code comments: the comment shouldn't describe "what" the code is doing unless that is unobvious. The comment should explain "why" the code is doing something. Because that is something which the code itself cannot explain.

mm/vmscan.c static noinline_for_stack void free_page

Quoted 10 lines
mm/vmscan.c +7 -0
+	if (prc->num < PRC_PAGE_NUM)
+		prc->pages[prc->num] = page;
+	prc->num++;
+	if (prc->num == PRC_PAGE_NUM)
+		return 1;
+	return 0;
+}
 
...

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#2 lore raw
Quoted 114 lines · Andrew Morton
Quoted 7 lines · On Wed, 29 Sep 2010 10:57:33 +0800 Shaohua Li<[email protected]> wrote:

when memory pressure is high, page_referenced() causes a lot of lock contention for anon_vma->lock or mapping->i_mmap_lock. Considering pages from one file usually live side by side in LRU list, we can lock several pages in shrink_page_list() and do batch page_referenced() to avoid some lock/unlock, which should reduce lock contention a lot. The locking rule documented in rmap.c is:
page_lock

Quoted 7 lines
	mapping->i_mmap_lock
		anon_vma->lock

For a batch of pages, we do page lock for all of them first and check their reference, and then release their i_mmap_lock or anon_vma lock. This seems not break the rule to me.
Before I further polish the patch, I'd like to know if there is anything preventing us to do such batch here.

The patch adds quite a bit of complexity, so we'd need to see benchmark testing results which justify it, please.

Also, the entire patch is irrelevant for uniprocessor machines, so the runtime overhead and code-size increases for CONFIG_SMP=n builds should be as low as possible - ideally zero. Please quantify this as well within the changelog if you pursue this work.

mm/internal.h int __get_user_pages(struct task_struct · line 261

Quoted 12 lines

...

mm/internal.h +9 -0
+#define PRC_PAGE_NUM 8
+struct page_reference_control {
+	int num;
+	struct page *pages[PRC_PAGE_NUM];
+	int references[PRC_PAGE_NUM];
+	struct anon_vma *anon_vma;
+	struct address_space *mapping;
+	/* no ksm */
+};

hm, 120 bytes of stack consumed, deep in page reclaim.

Quoted 26 lines
  #endif

  extern int hwpoison_filter(struct page *p);

...

static int page_referenced_file(struct page *page,
struct mem_cgroup *mem_cont,

mm/rmap.c +9 -2
-				unsigned long *vm_flags)
+				unsigned long *vm_flags,
+				struct page_reference_control *prc)
  {
  	unsigned int mapcount;
  	struct address_space *mapping = page->mapping;
@@ -603,8 +623,25 @@ static int page_referenced_file(struct p
  	 */
  	BUG_ON(!PageLocked(page));
 
-	spin_lock(&mapping->i_mmap_lock);
+	if (prc) {
+		if (mapping == prc->mapping) {
+			goto skip_lock;
+		}
+		if (prc->anon_vma) {
+			page_unlock_anon_vma(prc->anon_vma);
+			prc->anon_vma = NULL;

mm/rmap.c static int page_referenced_file(struct p · line 642

Quoted 10 lines
mm/rmap.c +10 -0
+		}
+		if (prc->mapping) {
+			spin_unlock(&prc->mapping->i_mmap_lock);
+			prc->mapping = NULL;
+		}
+		prc->mapping = mapping;
+
+		spin_lock(&mapping->i_mmap_lock);
+	} else
+		spin_lock(&mapping->i_mmap_lock);

Move the spin_lock() outside, remove the `else' part.

Quoted 34 lines
mm/rmap.c +20 -1
+skip_lock:
  	/*
  	 * i_mmap_lock does not stabilize mapcount at all, but mapcount
  	 * is more likely to be accurate if we note it after spinning.
@@ -628,7 +665,8 @@ static int page_referenced_file(struct p
  			break;
  	}
 
-	spin_unlock(&mapping->i_mmap_lock);
+	if (!prc)
+		spin_unlock(&mapping->i_mmap_lock);
  	return referenced;
  }
 
 
...
 
+static void do_prc_batch(struct scan_control *sc,
+	struct page_reference_control *prc)
+{
+	int i;
+	for (i = 0; i<  prc->num; i++)
+		prc->references[i] = page_check_references(prc->pages[i], sc,
+			prc);
+	/*
+	 * we must release all locks here, the lock ordering requries
+	 * pagelock->
+	 *   mapping->i_mmap_lock->
+	 *     anon_vma->lock
+	 * release lock guarantee we don't break the rule in next run
+	 */
+	if (prc->anon_vma) {
+		page_unlock_anon_vma(prc->anon_vma);
+		prc->anon_vma = NULL;
Quoted 8 lines

mm/rmap.c struct anon_vma *page_lock_anon_vma(stru · line 347

Quoted 6 lines
mm/rmap.c +6 -0
+	}
+	if (prc->mapping) {
+		spin_unlock(&prc->mapping->i_mmap_lock);
+		prc->mapping = NULL;
+	}
+}

I didn't check the locking alterations.

I've tried to wrap my head around them, but haven't yet been able to convince myself that it is safe.

What if we have multiple pages sharing the same
mapping or root anon_vma, which is not the same
as the prc->anon_vma ?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author
#3 lore raw
Quoted 124 lines · Rik van Riel
Quoted 114 lines · Andrew Morton
Quoted 7 lines · On Wed, 29 Sep 2010 10:57:33 +0800 Shaohua Li<[email protected]> wrote:

when memory pressure is high, page_referenced() causes a lot of lock contention for anon_vma->lock or mapping->i_mmap_lock. Considering pages from one file usually live side by side in LRU list, we can lock several pages in shrink_page_list() and do batch page_referenced() to avoid some lock/unlock, which should reduce lock contention a lot. The locking rule documented in rmap.c is:
page_lock

Quoted 7 lines
	mapping->i_mmap_lock
		anon_vma->lock

For a batch of pages, we do page lock for all of them first and check their reference, and then release their i_mmap_lock or anon_vma lock. This seems not break the rule to me.
Before I further polish the patch, I'd like to know if there is anything preventing us to do such batch here.

The patch adds quite a bit of complexity, so we'd need to see benchmark testing results which justify it, please.

Also, the entire patch is irrelevant for uniprocessor machines, so the runtime overhead and code-size increases for CONFIG_SMP=n builds should be as low as possible - ideally zero. Please quantify this as well within the changelog if you pursue this work.

mm/internal.h int __get_user_pages(struct task_struct · line 261

Quoted 12 lines

...

mm/internal.h +9 -0
+#define PRC_PAGE_NUM 8
+struct page_reference_control {
+	int num;
+	struct page *pages[PRC_PAGE_NUM];
+	int references[PRC_PAGE_NUM];
+	struct anon_vma *anon_vma;
+	struct address_space *mapping;
+	/* no ksm */
+};

hm, 120 bytes of stack consumed, deep in page reclaim.

Quoted 26 lines
  #endif

  extern int hwpoison_filter(struct page *p);

...

static int page_referenced_file(struct page *page,
struct mem_cgroup *mem_cont,

mm/rmap.c +9 -2
-				unsigned long *vm_flags)
+				unsigned long *vm_flags,
+				struct page_reference_control *prc)
  {
  	unsigned int mapcount;
  	struct address_space *mapping = page->mapping;
@@ -603,8 +623,25 @@ static int page_referenced_file(struct p
  	 */
  	BUG_ON(!PageLocked(page));
 
-	spin_lock(&mapping->i_mmap_lock);
+	if (prc) {
+		if (mapping == prc->mapping) {
+			goto skip_lock;
+		}
+		if (prc->anon_vma) {
+			page_unlock_anon_vma(prc->anon_vma);
+			prc->anon_vma = NULL;

mm/rmap.c static int page_referenced_file(struct p · line 642

Quoted 10 lines
mm/rmap.c +10 -0
+		}
+		if (prc->mapping) {
+			spin_unlock(&prc->mapping->i_mmap_lock);
+			prc->mapping = NULL;
+		}
+		prc->mapping = mapping;
+
+		spin_lock(&mapping->i_mmap_lock);
+	} else
+		spin_lock(&mapping->i_mmap_lock);

Move the spin_lock() outside, remove the `else' part.

Quoted 34 lines
mm/rmap.c +20 -1
+skip_lock:
  	/*
  	 * i_mmap_lock does not stabilize mapcount at all, but mapcount
  	 * is more likely to be accurate if we note it after spinning.
@@ -628,7 +665,8 @@ static int page_referenced_file(struct p
  			break;
  	}
 
-	spin_unlock(&mapping->i_mmap_lock);
+	if (!prc)
+		spin_unlock(&mapping->i_mmap_lock);
  	return referenced;
  }
 
 
...
 
+static void do_prc_batch(struct scan_control *sc,
+	struct page_reference_control *prc)
+{
+	int i;
+	for (i = 0; i<  prc->num; i++)
+		prc->references[i] = page_check_references(prc->pages[i], sc,
+			prc);
+	/*
+	 * we must release all locks here, the lock ordering requries
+	 * pagelock->
+	 *   mapping->i_mmap_lock->
+	 *     anon_vma->lock
+	 * release lock guarantee we don't break the rule in next run
+	 */
+	if (prc->anon_vma) {
+		page_unlock_anon_vma(prc->anon_vma);
+		prc->anon_vma = NULL;
Quoted 8 lines

mm/rmap.c struct anon_vma *page_lock_anon_vma(stru · line 347

Quoted 6 lines
mm/rmap.c +6 -0
+	}
+	if (prc->mapping) {
+		spin_unlock(&prc->mapping->i_mmap_lock);
+		prc->mapping = NULL;
+	}
+}

I didn't check the locking alterations.

Quoted 6 lines

I've tried to wrap my head around them, but haven't yet been able to convince myself that it is safe.

What if we have multiple pages sharing the same mapping or root anon_vma, which is not the same as the prc->anon_vma ?

if their mapping or anon_vma is different, page_lock_anon_vma() or page_referenced_file() will release the lock and retake a new lock in the patch. Since adjacent pages usually have the same mapping or anon_vma, this release/retake lock already can avoid quite a lot of lock. is this safe?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author
#4 lore raw
In reply to Andrew Morton
Quoted 17 lines · Andrew Morton
Quoted 7 lines · On Wed, 29 Sep 2010 10:57:33 +0800 Shaohua Li <[email protected]> wrote:

when memory pressure is high, page_referenced() causes a lot of lock contention for anon_vma->lock or mapping->i_mmap_lock. Considering pages from one file usually live side by side in LRU list, we can lock several pages in shrink_page_list() and do batch page_referenced() to avoid some lock/unlock, which should reduce lock contention a lot. The locking rule documented in rmap.c is:
page_lock

Quoted 7 lines
	mapping->i_mmap_lock
		anon_vma->lock

For a batch of pages, we do page lock for all of them first and check their reference, and then release their i_mmap_lock or anon_vma lock. This seems not break the rule to me.
Before I further polish the patch, I'd like to know if there is anything preventing us to do such batch here.

Thanks for your time.

Quoted 2 lines

The patch adds quite a bit of complexity, so we'd need to see benchmark testing results which justify it, please.

My test only shows around 10% improvements, which is below my expections. try_to_unmap() causes quite a lot of lock contention for such locks, and makes the page_referenced() batch not quite helpful. Looks we can do batch try_to_unmap() too. I'll report back later when I have data with try_to_unmap() batched.

Thanks,
Shaohua

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>