[PATCH 3/3] mlock: avoid dirtying pages and triggering writeback

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Michel Lespinasse
Date: Wednesday, November 17, 2010 - 5:23 am

When faulting in pages for mlock(), we want to break COW for anonymous
or file pages within VM_WRITABLE, non-VM_SHARED vmas. However, there is
no need to write-fault into VM_SHARED vmas since shared file pages can
be mlocked first and dirtied later, when/if they actually get written to.
Skipping the write fault is desirable, as we don't want to unnecessarily
cause these pages to be dirtied and queued for writeback.

Signed-off-by: Michel Lespinasse <walken@google.com>
---
 mm/memory.c |    7 ++++++-
 mm/mlock.c  |    7 ++++++-
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/mm/memory.c b/mm/memory.c
index d4c0c2e..7f45085 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -3291,7 +3291,12 @@ int make_pages_present(unsigned long addr, unsigned long end)
 	vma = find_vma(current->mm, addr);
 	if (!vma)
 		return -ENOMEM;
-	write = (vma->vm_flags & VM_WRITE) != 0;
+	/*
+	 * We want to touch writable mappings with a write fault in order
+	 * to break COW, except for shared mappings because these don't COW
+	 * and we would not want to dirty them for nothing.
+	 */
+	write = (vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE;
 	BUG_ON(addr >= end);
 	BUG_ON(end > vma->vm_end);
 	len = DIV_ROUND_UP(end, PAGE_SIZE) - addr/PAGE_SIZE;
diff --git a/mm/mlock.c b/mm/mlock.c
index b70919c..4f31864 100644
--- a/mm/mlock.c
+++ b/mm/mlock.c
@@ -171,7 +171,12 @@ static long __mlock_vma_pages_range(struct vm_area_struct *vma,
 	VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
 
 	gup_flags = FOLL_TOUCH | FOLL_GET;
-	if (vma->vm_flags & VM_WRITE)
+	/*
+	 * We want to touch writable mappings with a write fault in order
+	 * to break COW, except for shared mappings because these don't COW
+	 * and we would not want to dirty them for nothing.
+	 */
+	if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
 		gup_flags |= FOLL_WRITE;
 
 	/* We don't try to access the guard page of a stack vma */
-- 
1.7.3.1

--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH 0/3] Avoid dirtying pages during mlock, Michel Lespinasse, (Wed Nov 17, 5:23 am)
[PATCH 1/3] do_wp_page: remove the 'reuse' flag, Michel Lespinasse, (Wed Nov 17, 5:23 am)
[PATCH 2/3] do_wp_page: clarify dirty_page handling, Michel Lespinasse, (Wed Nov 17, 5:23 am)
[PATCH 3/3] mlock: avoid dirtying pages and triggering wri ..., Michel Lespinasse, (Wed Nov 17, 5:23 am)
Re: [PATCH 3/3] mlock: avoid dirtying pages and triggering ..., Michel Lespinasse, (Wed Nov 17, 3:05 pm)
Re: [PATCH 3/3] mlock: avoid dirtying pages and triggering ..., Michel Lespinasse, (Wed Nov 17, 4:31 pm)
Re: [PATCH 3/3] mlock: avoid dirtying pages and triggering ..., Michel Lespinasse, (Thu Nov 18, 4:03 am)
Re: [PATCH 3/3] mlock: avoid dirtying pages and triggering ..., Christoph Hellwig, (Thu Nov 18, 6:37 am)
Re: [PATCH 3/3] mlock: avoid dirtying pages and triggering ..., Christoph Hellwig, (Thu Nov 18, 6:39 am)
Re: [PATCH 3/3] mlock: avoid dirtying pages and triggering ..., Michel Lespinasse, (Fri Nov 19, 12:23 am)
Re: [PATCH 3/3] mlock: avoid dirtying pages and triggering ..., Christoph Hellwig, (Fri Nov 19, 8:06 am)
Re: [PATCH 3/3] mlock: avoid dirtying pages and triggering ..., Michel Lespinasse, (Fri Nov 19, 4:31 pm)