Commit 5ecfda041e4b4bd858d25bbf5a16c2a6c06d7272

Authored by Michel Lespinasse
Committed by Linus Torvalds
1 parent 72ddc8f722

mlock: avoid dirtying pages and triggering writeback

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>
Cc: Hugh Dickins <hughd@google.com>
Cc: Rik van Riel <riel@redhat.com>
Cc: Kosaki Motohiro <kosaki.motohiro@jp.fujitsu.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Nick Piggin <npiggin@kernel.dk>
Cc: Theodore Tso <tytso@google.com>
Cc: Michael Rubin <mrubin@google.com>
Cc: Suleiman Souhlal <suleiman@google.com>
Cc: Dave Chinner <david@fromorbit.com>
Cc: Christoph Hellwig <hch@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Showing 2 changed files with 12 additions and 2 deletions Side-by-side Diff

... ... @@ -3299,7 +3299,12 @@
3299 3299 vma = find_vma(current->mm, addr);
3300 3300 if (!vma)
3301 3301 return -ENOMEM;
3302   - write = (vma->vm_flags & VM_WRITE) != 0;
  3302 + /*
  3303 + * We want to touch writable mappings with a write fault in order
  3304 + * to break COW, except for shared mappings because these don't COW
  3305 + * and we would not want to dirty them for nothing.
  3306 + */
  3307 + write = (vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE;
3303 3308 BUG_ON(addr >= end);
3304 3309 BUG_ON(end > vma->vm_end);
3305 3310 len = DIV_ROUND_UP(end, PAGE_SIZE) - addr/PAGE_SIZE;
... ... @@ -171,7 +171,12 @@
171 171 VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
172 172  
173 173 gup_flags = FOLL_TOUCH | FOLL_GET;
174   - if (vma->vm_flags & VM_WRITE)
  174 + /*
  175 + * We want to touch writable mappings with a write fault in order
  176 + * to break COW, except for shared mappings because these don't COW
  177 + * and we would not want to dirty them for nothing.
  178 + */
  179 + if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
175 180 gup_flags |= FOLL_WRITE;
176 181  
177 182 /* We don't try to access the guard page of a stack vma */