Fixing the issue with memory-mapped file times
Subsystems (3)
cover Fixing the issue with memory-mapped file times
This is the seventh version of my solution for the bug #2645:
http://bugzilla.kernel.org/show_bug.cgi?id=2645
Since the previous version, the following has changed: based on Linus' comment, SMP-safe PTE update implemented.
Discussions, which followed my past submissions, showed that it was tempting to approach this problem using very different assumptions. However, many such designs have proved to be incomplete or inefficient.
Taking into account the obvious complexity of this problem, I prepared a design document, the purpose of which is twofold. First, it summarizes previous attempts to resolve the ctime/mtime issue. Second, it attempts to prove that what the patches do is necessary and sufficient for mtime and ctime update provided that we start from a certain sane set of requirements. The design document is available via the following link:
http://bugzilla.kernel.org/show_bug.cgi?id=2645#c40
For the seventh version, comprehensive performance testing was performed. The results of performance tests, including numbers, are available here:
http://bugzilla.kernel.org/show_bug.cgi?id=2645#c43
--
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>
PATCH 1/2 Massive code cleanup of sys_msync()
Use the PAGE_ALIGN() macro instead of "manual" alignment. Improve readability of the loop, which traverses the process memory regions. Make code more symmetric and possibly boost performance on some RISC CPUs by moving variable assignments.
--- mm/msync.c | 77 ++++++++++++++++++++++++++++-------------------------------- 1 files changed, 36 insertions(+), 41 deletions(-)
mm/msync.c +36 -41
@@ -1,85 +1,83 @@/* -* linux/mm/msync.c +* The msync() system call. * -* Copyright (C) 1994-1999 Linus Torvalds +* Copyright (C) 1994-1999 Linus Torvalds +* Copyright (C) 2008 Anton Salikhmetov <[email protected]> */ -/* -* The msync() system call. -*/ +#include <linux/file.h> #include <linux/fs.h> #include <linux/mm.h> #include <linux/mman.h> -#include <linux/file.h> -#include <linux/syscalls.h> #include <linux/sched.h> +#include <linux/syscalls.h> /* * MS_SYNC syncs the entire file - including mappings. * * MS_ASYNC does not start I/O (it used to, up to 2.5.67). -* Nor does it marks the relevant pages dirty (it used to up to 2.6.17). +* Nor does it mark the relevant pages dirty (it used to up to 2.6.17). * Now it doesn't do anything, since dirty pages are properly tracked. * -* The application may now run fsync() to -* write out the dirty pages and wait on the writeout and check the result. -* Or the application may run fadvise(FADV_DONTNEED) against the fd to start -* async writeout immediately. +* The application may now run fsync() to write out the dirty pages and +* wait on the writeout and check the result. Or the application may run +* fadvise(FADV_DONTNEED) against the fd to start async writeout immediately. * So by _not_ starting I/O in MS_ASYNC we provide complete flexibility to * applications. */ asmlinkage long sys_msync (unsigned long start,size_t len,int flags) {unsigned long end; -struct mm_struct *mm = current->mm; +int error, unmapped_error;struct vm_area_struct *vma; -int unmapped_error =0 ; -int error = -EINVAL ; +struct mm_struct *mm; + error = -EINVAL ;if (flags & ~(MS_ASYNC |MS_INVALIDATE |MS_SYNC )) goto out;if (start & ~PAGE_MASK ) goto out;if ((flags &MS_ASYNC ) && (flags &MS_SYNC )) goto out; + error = -ENOMEM ; - len = (len + ~PAGE_MASK ) &PAGE_MASK ; + len =PAGE_ALIGN (len); end = start + len;if (end < start) goto out; - error =0 ; + + error = unmapped_error =0 ;if (end == start) goto out; +/* * If the interval [start,end) covers some unmapped address ranges, * just ignore them, but return -ENOMEM at the end. */ + mm = current->mm;down_read (&mm->mmap_sem); vma =find_vma (mm, start); -for (;;) { + do { struct file *file; -/* Still start < end. */ error = -ENOMEM; if (!vma) - goto out_unlock; -/* Here start < vma->vm_end. */ + break; if (start < vma->vm_start) { start = vma->vm_start; if (start >= end) - goto out_unlock; - unmapped_error = -ENOMEM ; - } -/* Here vma->vm_start <= start < vma->vm_end. */ -if ((flags &MS_INVALIDATE ) && - (vma->vm_flags &VM_LOCKED )) { - error = -EBUSY ; - goto out_unlock; + break; + unmapped_error = error; } - file = vma->vm_file; + + error = -EBUSY; + if ((flags & MS_INVALIDATE) && (vma->vm_flags & VM_LOCKED)) + break; + + error = 0; start = vma->vm_end; -if ((flags &MS_SYNC ) && file && - (vma->vm_flags &VM_SHARED )) { + file = vma->vm_file; + if (file && (vma->vm_flags & VM_SHARED) && (flags & MS_SYNC)) { get_file(file); up_read(&mm->mmap_sem); error = do_fsync(file, 0);
@@ -88,16 +86,13 @@ asmlinkage long sys_msync(unsigned long start, size_t len, int flags) goto out;down_read (&mm->mmap_sem); vma =find_vma (mm, start); - }else { -if (start >= end) { - error =0 ; - goto out_unlock; - } - vma = vma->vm_next; +continue ; } - } -out_unlock: + + vma = vma->vm_next; + }while (start < end);up_read (&mm->mmap_sem); + out: -return error ? : unmapped_error; +return error ? error : unmapped_error; }
Signature · 7 lines
1.4.4.4 -- 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>
PATCH 2/2 Update ctime and mtime for memory-mapped files
In reply to Anton SalikhmetovUpdate file times at write references to memory-mapped files. Force file times update at the next write reference after calling the msync() system call with the MS_ASYNC flag.
--- mm/memory.c | 6 ++++++ mm/msync.c | 57 ++++++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 50 insertions(+), 13 deletions(-)
mm/memory.c +6 -0
@@ -1670,6 +1670,9 @@ gotten: unlock:pte_unmap_unlock (page_table, ptl);if (dirty_page) { +if (vma->vm_file) +file_update_time (vma->vm_file); +/* * Yes, Virginia, this is actually required to prevent a race * with clear_page_dirty_for_io() from clearing the page dirty
@@ -2343,6 +2346,9 @@ out_unlocked:if (anon)page_cache_release (vmf.page);else if (dirty_page) { +if (vma->vm_file) +file_update_time (vma->vm_file); +set_page_dirty_balance (dirty_page, page_mkwrite);put_page (dirty_page); }
mm/msync.c +44 -13
77 lines not on this page · raw message
Signature · 7 lines
1.4.4.4 -- 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>
Re: [PATCH -v7 2/2] Update ctime and mtime for memory-mapped files
Some very pedantic nitpicking below;
Quoted 50 lines · Anton Salikhmetov
http://bugzilla.kernel.org/show_bug.cgi?id=2645#c40
Update file times at write references to memory-mapped files. Force file times update at the next write reference after calling the
msync()system call with theMS_ASYNCflag.Signed-off-by Anton Salikhmetov <[email protected]>--- mm/memory.c | 6 ++++++ mm/msync.c | 57 ++++++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 50 insertions(+), 13 deletions(-)mm/memory.c +6
-0@@ -1670,6 +1670,9 @@ gotten: unlock:pte_unmap_unlock (page_table, ptl);if (dirty_page) { +if (vma->vm_file) +file_update_time (vma->vm_file); +/* * Yes, Virginia, this is actually required to prevent a race * with clear_page_dirty_for_io() from clearing the page dirty @@ -2343,6 +2346,9 @@ out_unlocked:if (anon)page_cache_release (vmf.page);else if (dirty_page) { +if (vma->vm_file) +file_update_time (vma->vm_file); +set_page_dirty_balance (dirty_page, page_mkwrite);put_page (dirty_page); }mm/msync.c +1
-0@@ -5,6 +5,7 @@* Copyright (C) 2008 Anton Salikhmetov <[email protected]> */ +#include <asm/tlbflush.h> #include <linux/file.h> #include <linux/fs.h> #include <linux/mm.h> @@ -13,11 +14,37 @@#include <linux/syscalls.h>
mm/msync.c line 24
Quoted 10 lines
mm/msync.c +8
-0/* +* Scan the PTEs for pages belonging to the VMA and mark them read-only. +* It will force a pagefault on the next write access. +*/ +static void vma_wrprotect (struct vm_area_struct *vma) +{ +unsigned long addr; + +for (addr = vma->vm_start; addr < vma->vm_end; addr +=PAGE_SIZE ) {
I know it's not the common "Linux Kernel way", but 'addr' could be made to have just 'for' scope here according to C99;
for (unsigned long addr = vma->vm_start; addr < vma->vm_end;
addr += PAGE_SIZE) {
Quoted 12 lines
mm/msync.c +12
-0+spinlock_t *ptl; +pgd_t *pgd =pgd_offset (vma->vm_mm, addr); +pud_t *pud =pud_offset (pgd, addr); +pmd_t *pmd =pmd_offset (pud, addr); +pte_t *pte =pte_offset_map_lock (vma->vm_mm, pmd, addr, &ptl); + +if (pte_dirty (*pte) &&pte_write (*pte)) { +pte_t entry =ptep_clear_flush (vma, addr, pte); + + entry =pte_wrprotect (entry); +set_pte_at (vma->vm_mm, addr, pte, entry); + }
mm/msync.c line 20
Quoted 10 lines
mm/msync.c +5
-3+pte_unmap_unlock (pte, ptl); + } +} + +/* * MS_SYNC syncs the entire file - including mappings. * -* MS_ASYNC does not start I/O (it used to, up to 2.5.67). -* Nor does it mark the relevant pages dirty (it used to up to 2.6.17). -* Now it doesn't do anything, since dirty pages are properly tracked.
I think keeping some version of the "up to ..." comments makes sense. It documents that we previously had different behaviour.
Quoted 15 lines
mm/msync.c +4
-4+* MS_ASYNC does not start I/O. Instead, it marks the relevant pages +* read-only by calling vma_wrprotect(). This is needed to catch the next +* write reference to the mapped region and update the file times +* accordingly. * * The application may now run fsync() to write out the dirty pages and * wait on the writeout and check the result. Or the application may run @@ -77,16 +104,20 @@ asmlinkage long sys_msync(unsigned long start, size_t len, int flags) error =0 ; start = vma->vm_end; file = vma->vm_file; -if (file && (vma->vm_flags &VM_SHARED ) && (flags &MS_SYNC )) { -get_file (file); -up_read (&mm->mmap_sem); - error =do_fsync (file,0 );
mm/msync.c asmlinkage long sys_msync(unsigned long start, size_t len, int flags) · line 110
Quoted 10 lines
mm/msync.c +4
-6-fput (file); -if (error || start >= end) - goto out; -down_read (&mm->mmap_sem); - vma =find_vma (mm, start); -continue ; +if (file && (vma->vm_flags &VM_SHARED )) { +if (flags &MS_ASYNC ) +vma_wrprotect (vma); +if (flags &MS_SYNC ) {
"else if" ??
Quoted 13 lines
mm/msync.c +10
-0+get_file (file); +up_read (&mm->mmap_sem); + error =do_fsync (file,0 ); +fput (file); +if (error || start >= end) + goto out; +down_read (&mm->mmap_sem); + vma =find_vma (mm, start); +continue ; + } } vma = vma->vm_next;
Signature · 9 lines
Jesper Juhl <[email protected]> Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html Plain text mails only, please http://www.expita.com/nomime.html -- 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>
Re: [PATCH -v7 2/2] Update ctime and mtime for memory-mapped files
2008/1/22, Jesper Juhl <[email protected]>:
Quoted 54 lines
Some very pedantic nitpicking below;
Quoted 51 lines · Anton Salikhmetov
http://bugzilla.kernel.org/show_bug.cgi?id=2645#c40
Update file times at write references to memory-mapped files. Force file times update at the next write reference after calling the
msync()system call with theMS_ASYNCflag.Signed-off-by Anton Salikhmetov <[email protected]>--- mm/memory.c | 6 ++++++ mm/msync.c | 57 ++++++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 50 insertions(+), 13 deletions(-)mm/memory.c +6
-0@@ -1670,6 +1670,9 @@ gotten: unlock:pte_unmap_unlock (page_table, ptl);if (dirty_page) { +if (vma->vm_file) +file_update_time (vma->vm_file); +/* * Yes, Virginia, this is actually required to prevent a race * with clear_page_dirty_for_io() from clearing the page dirty @@ -2343,6 +2346,9 @@ out_unlocked:if (anon)page_cache_release (vmf.page);else if (dirty_page) { +if (vma->vm_file) +file_update_time (vma->vm_file); +set_page_dirty_balance (dirty_page, page_mkwrite);put_page (dirty_page); }mm/msync.c +1
-0@@ -5,6 +5,7 @@* Copyright (C) 2008 Anton Salikhmetov <[email protected]> */ +#include <asm/tlbflush.h> #include <linux/file.h> #include <linux/fs.h> #include <linux/mm.h> @@ -13,11 +14,37 @@#include <linux/syscalls.h>
Quoted 12 lines
mm/msync.c line 24
Quoted 9 lines
mm/msync.c +8
-0/* +* Scan the PTEs for pages belonging to the VMA and mark them read-only. +* It will force a pagefault on the next write access. +*/ +static void vma_wrprotect (struct vm_area_struct *vma) +{ +unsigned long addr; + +for (addr = vma->vm_start; addr < vma->vm_end; addr +=PAGE_SIZE ) {I know it's not the common "Linux Kernel way", but 'addr' could be made to have just 'for' scope here according to C99;
I believe that the C89 style is more common for the Linux kernel, so I've used the out-of-scope variable declaration.
Quoted 22 lines
for (unsigned long addr = vma->vm_start; addr < vma->vm_end;addr +=
PAGE_SIZE) {Quoted 17 lines
mm/msync.c +17
-0+spinlock_t *ptl; +pgd_t *pgd =pgd_offset (vma->vm_mm, addr); +pud_t *pud =pud_offset (pgd, addr); +pmd_t *pmd =pmd_offset (pud, addr); +pte_t *pte =pte_offset_map_lock (vma->vm_mm, pmd, addr, &ptl); + +if (pte_dirty (*pte) &&pte_write (*pte)) { +pte_t entry =ptep_clear_flush (vma, addr, pte); + + entry =pte_wrprotect (entry); +set_pte_at (vma->vm_mm, addr, pte, entry); + } +pte_unmap_unlock (pte, ptl); + } +} + +/*
Quoted 8 lines
mm/msync.c line 20
Quoted 5 lines
mm/msync.c +0
-3* MS_SYNC syncs the entire file - including mappings. * -* MS_ASYNC does not start I/O (it used to, up to 2.5.67). -* Nor does it mark the relevant pages dirty (it used to up to 2.6.17). -* Now it doesn't do anything, since dirty pages are properly tracked. I think keeping some version of the "up to ..." comments makes sense. It documents that we previously had different behaviour.
Earlier I had a request to remove any "changelog-style" comments from the code.
Quoted 20 lines
Quoted 20 lines
mm/msync.c +4
-8+* MS_ASYNC does not start I/O. Instead, it marks the relevant pages +* read-only by calling vma_wrprotect(). This is needed to catch the next +* write reference to the mapped region and update the file times +* accordingly. * * The application may now run fsync() to write out the dirty pages and * wait on the writeout and check the result. Or the application may run @@ -77,16 +104,20 @@ asmlinkage long sys_msync(unsigned long start, size_t len, int flags) error =0 ; start = vma->vm_end; file = vma->vm_file; -if (file && (vma->vm_flags &VM_SHARED ) && (flags &MS_SYNC )) { -get_file (file); -up_read (&mm->mmap_sem); - error =do_fsync (file,0 ); -fput (file); -if (error || start >= end) - goto out; -down_read (&mm->mmap_sem);
Quoted 8 lines
mm/msync.c asmlinkage long sys_msync(unsigned long start, size_t len, int flags) · line 110
Quoted 6 lines
mm/msync.c +4
-2- vma =find_vma (mm, start); -continue ; +if (file && (vma->vm_flags &VM_SHARED )) { +if (flags &MS_ASYNC ) +vma_wrprotect (vma); +if (flags &MS_SYNC ) {"else if" ??
The MS_ASYNC and MS_SYNC flags are mutually exclusive, that is why I did not use the "else-if" here. Moreover, this function itself checks that they never come together.
Quoted 13 lines
Quoted 13 lines
mm/msync.c +10
-0+get_file (file); +up_read (&mm->mmap_sem); + error =do_fsync (file,0 ); +fput (file); +if (error || start >= end) + goto out; +down_read (&mm->mmap_sem); + vma =find_vma (mm, start); +continue ; + } }
Quoted 7 lines
mm/msync.c asmlinkage long sys_msync(unsigned long start, size_t len, int flags) · line 123
Quoted 1 line
mm/msync.c +0
-0vma = vma->vm_next;--
Jesper Juhl <[email protected]>
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
--
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>
Re: [PATCH -v7 2/2] Update ctime and mtime for memory-mapped files
Quoted 4 lines · Anton Salikhmetov
2008/1/22, Jesper Juhl <[email protected]>:
Quoted 3 lines
Some very pedantic nitpicking below;
On 22/01/2008, Anton Salikhmetov <[email protected]> wrote:
...
Quoted 11 lines
Quoted 6 lines
mm/msync.c asmlinkage long sys_msync(unsigned long start, size_t len, int flags) · line 110
Quoted 4 lines
mm/msync.c +4
-0+if (file && (vma->vm_flags &VM_SHARED )) { +if (flags &MS_ASYNC ) +vma_wrprotect (vma); +if (flags &MS_SYNC ) {"else if" ??
The
MS_ASYNCandMS_SYNCflags are mutually exclusive, that is why I did not use the "else-if" here. Moreover, this function itself checks that they never come together.
I would say that them being mutually exclusive would be a reason *for* using "else-if" here.
Signature · 9 lines
Jesper Juhl <[email protected]> Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html Plain text mails only, please http://www.expita.com/nomime.html -- 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>
Re: [PATCH -v7 2/2] Update ctime and mtime for memory-mapped files
2008/1/22, Jesper Juhl <[email protected]>:
Quoted 13 lines · Anton Salikhmetov
Quoted 4 lines
2008/1/22, Jesper Juhl <[email protected]>:
Quoted 3 lines
Some very pedantic nitpicking below;
On 22/01/2008, Anton Salikhmetov <[email protected]> wrote:
...
Quoted 7 lines
Quoted 6 lines
mm/msync.c asmlinkage long sys_msync(unsigned long start, size_t len, int flags) · line 110
Quoted 4 lines
mm/msync.c +4
-0+if (file && (vma->vm_flags &VM_SHARED )) { +if (flags &MS_ASYNC ) +vma_wrprotect (vma); +if (flags &MS_SYNC ) {"else if" ??
Quoted 7 lines
Quoted 4 lines
The
MS_ASYNCandMS_SYNCflags are mutually exclusive, that is why I did not use the "else-if" here. Moreover, this function itself checks that they never come together.I would say that them being mutually exclusive would be a reason *for* using "else-if" here.
This check is performed by the sys_msync() function itself in its very beginning.
We don't need to check it later.
Quoted 6 lines
--
Jesper Juhl <[email protected]>
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
--
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>
Re: [PATCH -v7 2/2] Update ctime and mtime for memory-mapped files
Quoted 19 lines · Anton Salikhmetov
2008/1/22, Jesper Juhl <[email protected]>:
Quoted 18 lines · Anton Salikhmetov
Quoted 4 lines
2008/1/22, Jesper Juhl <[email protected]>:
Quoted 3 lines
Some very pedantic nitpicking below;
On 22/01/2008, Anton Salikhmetov <[email protected]> wrote:
...
Quoted 11 lines
Quoted 6 lines
mm/msync.c asmlinkage long sys_msync(unsigned long start, size_t len, int flags) · line 110
Quoted 4 lines
mm/msync.c +4
-0+if (file && (vma->vm_flags &VM_SHARED )) { +if (flags &MS_ASYNC ) +vma_wrprotect (vma); +if (flags &MS_SYNC ) {"else if" ??
The
MS_ASYNCandMS_SYNCflags are mutually exclusive, that is why I did not use the "else-if" here. Moreover, this function itself checks that they never come together.
Quoted 8 lines
Quoted 2 lines
I would say that them being mutually exclusive would be a reason *for* using "else-if" here.
This check is performed by the
sys_msync()function itself in its very beginning.We don't need to check it later.
Sure, it's just that, to me, using 'else-if' makes it explicit that the two are mutually exclusive. Using "if (...), if (...)" doesn't. Maybe it's just me, but I feel that 'else-if' here better shows the intention... No big deal.
Signature · 9 lines
Jesper Juhl <[email protected]> Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html Plain text mails only, please http://www.expita.com/nomime.html -- 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>
Re: [PATCH -v7 2/2] Update ctime and mtime for memory-mapped files
In reply to Anton Salikhmetov2008/1/22, Anton Salikhmetov <[email protected]>:
Quoted 68 lines
2008/1/22, Jesper Juhl <[email protected]>:
Quoted 54 lines
Some very pedantic nitpicking below;
Quoted 51 lines · Anton Salikhmetov
http://bugzilla.kernel.org/show_bug.cgi?id=2645#c40
Update file times at write references to memory-mapped files. Force file times update at the next write reference after calling the
msync()system call with theMS_ASYNCflag.Signed-off-by Anton Salikhmetov <[email protected]>--- mm/memory.c | 6 ++++++ mm/msync.c | 57 ++++++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 50 insertions(+), 13 deletions(-)mm/memory.c +6
-0@@ -1670,6 +1670,9 @@ gotten: unlock:pte_unmap_unlock (page_table, ptl);if (dirty_page) { +if (vma->vm_file) +file_update_time (vma->vm_file); +/* * Yes, Virginia, this is actually required to prevent a race * with clear_page_dirty_for_io() from clearing the page dirty @@ -2343,6 +2346,9 @@ out_unlocked:if (anon)page_cache_release (vmf.page);else if (dirty_page) { +if (vma->vm_file) +file_update_time (vma->vm_file); +set_page_dirty_balance (dirty_page, page_mkwrite);put_page (dirty_page); }mm/msync.c +1
-0@@ -5,6 +5,7 @@* Copyright (C) 2008 Anton Salikhmetov <[email protected]> */ +#include <asm/tlbflush.h> #include <linux/file.h> #include <linux/fs.h> #include <linux/mm.h> @@ -13,11 +14,37 @@#include <linux/syscalls.h> Quoted 12 lines
mm/msync.c line 24
Quoted 9 lines
mm/msync.c +8
-0/* +* Scan the PTEs for pages belonging to the VMA and mark them read-only. +* It will force a pagefault on the next write access. +*/ +static void vma_wrprotect (struct vm_area_struct *vma) +{ +unsigned long addr; + +for (addr = vma->vm_start; addr < vma->vm_end; addr +=PAGE_SIZE ) {I know it's not the common "Linux Kernel way", but 'addr' could be made to have just 'for' scope here according to C99;
Quoted 6 lines
I believe that the C89 style is more common for the Linux kernel, so I've used the out-of-scope variable declaration.
Quoted 3 lines
for (unsigned long addr = vma->vm_start; addr < vma->vm_end;addr +=
PAGE_SIZE) {
By the way, if we're talking "pedantic", then:
Quoted 1 line
Quoted 1 line
Quoted 1 line
debian:/tmp$ cat c.c
void f()
{
for (unsigned long i = 0; i < 10; i++)
continue;
}
debian:/tmp$ gcc -c -pedantic c.c
c.c: In function 'f':
c.c:3: error: 'for' loop initial declaration used outside C99 mode
debian:/tmp$<<<
No pun intended :)
Quoted 59 lines
Quoted 19 lines
Quoted 19 lines
mm/msync.c +17
-0+spinlock_t *ptl; +pgd_t *pgd =pgd_offset (vma->vm_mm, addr); +pud_t *pud =pud_offset (pgd, addr); +pmd_t *pmd =pmd_offset (pud, addr); +pte_t *pte =pte_offset_map_lock (vma->vm_mm, pmd, addr, &ptl); + +if (pte_dirty (*pte) &&pte_write (*pte)) { +pte_t entry =ptep_clear_flush (vma, addr, pte); + + entry =pte_wrprotect (entry); +set_pte_at (vma->vm_mm, addr, pte, entry); + } +pte_unmap_unlock (pte, ptl); + } +} + +/* Quoted 8 lines
Quoted 5 lines
*
MS_SYNCsyncs the entire file - including mappings.
*
- *MS_ASYNCdoes not start I/O (it used to, up to 2.5.67).
- * Nor does it mark the relevant pages dirty (it used to up to 2.6.17).
- * Now it doesn't do anything, since dirty pages are properly tracked.I think keeping some version of the "up to ..." comments makes sense. It documents that we previously had different behaviour.
Earlier I had a request to remove any "changelog-style" comments from the code.
Quoted 20 lines
Quoted 20 lines
+ *
MS_ASYNCdoes not start I/O. Instead, it marks the relevant pages
+ * read-only by callingvma_wrprotect(). This is needed to catch the next
+ * write reference to the mapped region and update the file times
+ * accordingly.
*
* The application may now runfsync()to write out the dirty pages and
* wait on the writeout and check the result. Or the application may runmm/msync.c +0
-8@@ -77,16 +104,20 @@ asmlinkage long sys_msync(unsigned long start, size_t len, int flags) error =0 ; start = vma->vm_end; file = vma->vm_file; -if (file && (vma->vm_flags &VM_SHARED ) && (flags &MS_SYNC )) { -get_file (file); -up_read (&mm->mmap_sem); - error =do_fsync (file,0 ); -fput (file); -if (error || start >= end) - goto out; -down_read (&mm->mmap_sem);Quoted 8 lines
mm/msync.c asmlinkage long sys_msync(unsigned long start, size_t len, int flags) · line 110
Quoted 6 lines
mm/msync.c +4
-2- vma =find_vma (mm, start); -continue ; +if (file && (vma->vm_flags &VM_SHARED )) { +if (flags &MS_ASYNC ) +vma_wrprotect (vma); +if (flags &MS_SYNC ) {"else if" ??
Quoted 25 lines
The
MS_ASYNCandMS_SYNCflags are mutually exclusive, that is why I did not use the "else-if" here. Moreover, this function itself checks that they never come together.Quoted 20 lines
Quoted 4 lines
mm/msync.c +3
-0+get_file (file); +up_read (&mm->mmap_sem); + error =do_fsync (file,0 );mm/msync.c asmlinkage long sys_msync(unsigned long start, size_t len, int flags) · line 123
Quoted 10 lines
mm/msync.c +7
-0+fput (file); +if (error || start >= end) + goto out; +down_read (&mm->mmap_sem); + vma =find_vma (mm, start); +continue ; + } } vma = vma->vm_next;--
Jesper Juhl <[email protected]>
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
--
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>
Re: [PATCH -v7 2/2] Update ctime and mtime for memory-mapped files
Quoted 4 lines · Anton Salikhmetov
2008/1/22, Anton Salikhmetov <[email protected]>:
Quoted 3 lines
2008/1/22, Jesper Juhl <[email protected]>:
Quoted 2 lines
Some very pedantic nitpicking below;
...
Quoted 5 lines
By the way, if we're talking "pedantic", then:
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 11 lines
debian:/tmp$ cat c.c
void f()
{
for (unsigned long i = 0; i < 10; i++)
continue;
}
debian:/tmp$ gcc -c -pedantic c.c
c.c: In function 'f':
c.c:3: error: 'for' loop initial declaration used outside C99 mode
debian:/tmp$Well, I just wrote the way I'd have writen the loop, I know it's not the common kernel style. Just offering my feedback/input :)
Signature · 9 lines
Jesper Juhl <[email protected]> Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html Plain text mails only, please http://www.expita.com/nomime.html -- 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>
Re: [PATCH -v7 2/2] Update ctime and mtime for memory-mapped files
In reply to Anton SalikhmetovQuoted 5 lines · Anton Salikhmetov
/*
+ * Scan the PTEs for pages belonging to the VMA and mark them read-only.
+ * It will force a pagefault on the next write access.
+ */
mm/msync.c line 29
Quoted 10 lines
mm/msync.c +10
-0+static void vma_wrprotect (struct vm_area_struct *vma) +{ +unsigned long addr; + +for (addr = vma->vm_start; addr < vma->vm_end; addr +=PAGE_SIZE ) { +spinlock_t *ptl; +pgd_t *pgd =pgd_offset (vma->vm_mm, addr); +pud_t *pud =pud_offset (pgd, addr); +pmd_t *pmd =pmd_offset (pud, addr); +pte_t *pte =pte_offset_map_lock (vma->vm_mm, pmd, addr, &ptl);
This is extremely expensive over bigger areas, especially sparsely mapped ones (it does all the lookups for all four levels over and over and over again for eachg page).
I think Peter Zijlstra posted a version that uses the regular kind of nested loop (with inline functions to keep the thing nice and clean), which gets rid of that.
[ The sad/funny part is that this is all how we *used* to do msync(), back
in the days: we're literally going back to the "pre-cleanup" logic. See commit 204ec841fbea3e5138168edbc3a76d46747cc987: "mm: msync() cleanup" for details ]
Quite frankly, I really think you might be better off just doing a
git revert 204ec841fbea3e5138168edbc3a76d46747cc987
and working from there! I just checked, and it still reverts cleanly, and you'd end up with a nice code-base that (a) has gotten years of testing and (b) already has the looping-over-the-pagetables code.
Linus
--
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>
Re: [PATCH -v7 2/2] Update ctime and mtime for memory-mapped files
2008/1/22, Linus Torvalds <[email protected]>:
Quoted 23 lines · Anton Salikhmetov
Quoted 5 lines
/*
+ * Scan the PTEs for pages belonging to the VMA and mark them read-only.
+ * It will force a pagefault on the next write access.
+ */mm/msync.c line 29
Quoted 10 lines
mm/msync.c +10
-0+static void vma_wrprotect (struct vm_area_struct *vma) +{ +unsigned long addr; + +for (addr = vma->vm_start; addr < vma->vm_end; addr +=PAGE_SIZE ) { +spinlock_t *ptl; +pgd_t *pgd =pgd_offset (vma->vm_mm, addr); +pud_t *pud =pud_offset (pgd, addr); +pmd_t *pmd =pmd_offset (pud, addr); +pte_t *pte =pte_offset_map_lock (vma->vm_mm, pmd, addr, &ptl);This is extremely expensive over bigger areas, especially sparsely mapped ones (it does all the lookups for all four levels over and over and over again for eachg page).
Quoted 3 lines
I think Peter Zijlstra posted a version that uses the regular kind of nested loop (with inline functions to keep the thing nice and clean), which gets rid of that.
Thanks for your feedback, Linus!
I will use Peter Zijlstra's version of such an operation in my next patch series.
Quoted 8 lines
[ The sad/funny part is that this is all how we *used* to do
msync(), back
in the days: we're literally going back to the "pre-cleanup" logic. See commit204ec841fbea3e5138168edbc3a76d46747cc987: "mm:msync()cleanup" for details ]Quite frankly, I really think you might be better off just doing a
Quoted 8 lines
git revert 204ec841fbea3e5138168edbc3a76d46747cc987and working from there! I just checked, and it still reverts cleanly, and you'd end up with a nice code-base that (a) has gotten years of testing and (b) already has the looping-over-the-pagetables code.
Linus
--
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>
Re: [PATCH -v7 2/2] Update ctime and mtime for memory-mapped files
Quoted 20 lines
Quoted 20 lines
Quoted 5 lines
/*
+ * Scan the PTEs for pages belonging to the VMA and mark them read-only.
+ * It will force a pagefault on the next write access.
+ */mm/msync.c line 29
Quoted 10 lines
mm/msync.c +10
-0+static void vma_wrprotect (struct vm_area_struct *vma) +{ +unsigned long addr; + +for (addr = vma->vm_start; addr < vma->vm_end; addr +=PAGE_SIZE ) { +spinlock_t *ptl; +pgd_t *pgd =pgd_offset (vma->vm_mm, addr); +pud_t *pud =pud_offset (pgd, addr); +pmd_t *pmd =pmd_offset (pud, addr); +pte_t *pte =pte_offset_map_lock (vma->vm_mm, pmd, addr, &ptl);This is extremely expensive over bigger areas, especially sparsely mapped ones (it does all the lookups for all four levels over and over and over again for eachg page).
Quoted 8 lines
Quoted 3 lines
I think Peter Zijlstra posted a version that uses the regular kind of nested loop (with inline functions to keep the thing nice and clean), which gets rid of that.
Thanks for your feedback, Linus!
I will use Peter Zijlstra's version of such an operation in my next patch series.
But note, that those functions iterate over all the vmas for the given page range, not just the one msync was performed on. This might get even more expensive, if the file is mapped lots of times.
The old version, that Linus was referring to, needs some modification as well, because it doesn't write protect the ptes, just marks them clean.
Miklos
--
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>
Re: [PATCH -v7 2/2] Update ctime and mtime for memory-mapped files
In reply to Anton SalikhmetovAnton Salikhmetov <[email protected]> writes:
You should probably put your design document somewhere in Documentation with a patch.
mm/msync.c line 29
Quoted 13 lines
+ * Scan the PTEs for pages belonging to the VMA and mark them read-only.
+ * It will force a pagefault on the next write access.
+ */mm/msync.c +10
-0+static void vma_wrprotect (struct vm_area_struct *vma) +{ +unsigned long addr; + +for (addr = vma->vm_start; addr < vma->vm_end; addr +=PAGE_SIZE ) { +spinlock_t *ptl; +pgd_t *pgd =pgd_offset (vma->vm_mm, addr); +pud_t *pud =pud_offset (pgd, addr); +pmd_t *pmd =pmd_offset (pud, addr); +pte_t *pte =pte_offset_map_lock (vma->vm_mm, pmd, addr, &ptl);
This means on i386 with highmem ptes you will map/flush tlb/unmap each PTE individually. You will do 512 times as much work as really needed per PTE leaf page.
The performance critical address space walkers use a different design pattern that avoids this.
mm/msync.c line 32
Quoted 2 lines
mm/msync.c +2
-0+if (pte_dirty (*pte) &&pte_write (*pte)) { +pte_t entry =ptep_clear_flush (vma, addr, pte);
Flushing TLBs unbatched can also be very expensive because if the MM is shared by several CPUs you'll have a inter-processor interrupt for each iteration. They are quite costly even on smaller systems.
It would be better if you did a single flush_tlb_range() at the end. This means on x86 this will currently always do a full flush, but that's still better than really slowing down in the heavily multithreaded case.
-Andi
--
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>
Quoted 18 lines · Anton Salikhmetov
This is the seventh version of my solution for the bug #2645:
http://bugzilla.kernel.org/show_bug.cgi?id=2645
Since the previous version, the following has changed: based on Linus' comment, SMP-safe PTE update implemented.
Discussions, which followed my past submissions, showed that it was tempting to approach this problem using very different assumptions. However, many such designs have proved to be incomplete or inefficient.
Taking into account the obvious complexity of this problem, I prepared a design document, the purpose of which is twofold. First, it summarizes previous attempts to resolve the ctime/mtime issue. Second, it attempts to prove that what the patches do is necessary and sufficient for mtime and ctime update provided that we start from a certain sane set of requirements. The design document is available via the following link:
Quoted 7 lines
http://bugzilla.kernel.org/show_bug.cgi?id=2645#c40
For the seventh version, comprehensive performance testing was performed. The results of performance tests, including numbers, are available here:
Hi Anton,
I applied your patches here and as far as my own test programs go, these patches solve the previously observed problems I saw with mtime not getting updated.
Thank you very much for so persistently working on these long standing issues.
Signature · 9 lines
Jesper Juhl <[email protected]> Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html Plain text mails only, please http://www.expita.com/nomime.html -- 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>
2008/1/22, Jesper Juhl <[email protected]>:
Quoted 27 lines · Anton Salikhmetov
Quoted 18 lines
This is the seventh version of my solution for the bug #2645:
http://bugzilla.kernel.org/show_bug.cgi?id=2645
Since the previous version, the following has changed: based on Linus' comment, SMP-safe PTE update implemented.
Discussions, which followed my past submissions, showed that it was tempting to approach this problem using very different assumptions. However, many such designs have proved to be incomplete or inefficient.
Taking into account the obvious complexity of this problem, I prepared a design document, the purpose of which is twofold. First, it summarizes previous attempts to resolve the ctime/mtime issue. Second, it attempts to prove that what the patches do is necessary and sufficient for mtime and ctime update provided that we start from a certain sane set of requirements. The design document is available via the following link:
Quoted 7 lines
http://bugzilla.kernel.org/show_bug.cgi?id=2645#c40
For the seventh version, comprehensive performance testing was performed. The results of performance tests, including numbers, are available here:
Quoted 7 lines
Hi Anton,
I applied your patches here and as far as my own test programs go, these patches solve the previously observed problems I saw with mtime not getting updated.
Thank you very much for so persistently working on these long standing issues.
Thank you very much for testing these patches!
Quoted 6 lines
--
Jesper Juhl <[email protected]>
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
--
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>