[PATCH -v6 2/2] Updating ctime and mtime for memory-mapped files

!MAILaRCHIVE_VOTE_RePLACE
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: <linux-mm@...>, <jakob@...>, <linux-kernel@...>, <valdis.kletnieks@...>, <riel@...>, <ksm@...>, <staubach@...>, <jesper.juhl@...>, <torvalds@...>, <a.p.zijlstra@...>, <akpm@...>, <protasnb@...>, <miklos@...>, <r.e.wolff@...>, <hidave.darkstar@...>, <hch@...>
Date: Thursday, January 17, 2008 - 6:31 pm

Updating file times at write references to memory-mapped files and
forcing file times update at the next write reference after
calling the msync() system call with the MS_ASYNC flag.

Signed-off-by: Anton Salikhmetov <salikhmetov@gmail.com>
---
 mm/memory.c |    6 ++++++
 mm/msync.c  |   52 +++++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 45 insertions(+), 13 deletions(-)

diff --git a/mm/memory.c b/mm/memory.c
index 4bf0b6d..13d5bbf 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -1668,6 +1668,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
@@ -2341,6 +2344,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);
 	}
diff --git a/mm/msync.c b/mm/msync.c
index a4de868..a49af28 100644
--- a/mm/msync.c
+++ b/mm/msync.c
@@ -13,11 +13,33 @@
 #include <linux/syscalls.h>
 
 /*
+ * 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) {
+		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 = pte_wrprotect(*pte);
+		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.
+ * 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 +99,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);
-			vma = find_vma(mm, start);
-			continue;
+		if (file && (vma->vm_flags & VM_SHARED)) {
+			if (flags & MS_ASYNC)
+				vma_wrprotect(vma);
+			if (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);
+				vma = find_vma(mm, start);
+				continue;
+			}
 		}
 
 		vma = vma->vm_next;
-- 
1.4.4.4

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

Messages in current thread:
[PATCH -v6 0/2] Fixing the issue with memory-mapped file times, Anton Salikhmetov, (Thu Jan 17, 6:31 pm)
Re: [PATCH -v6 0/2] Fixing the issue with memory-mapped file..., Anton Salikhmetov, (Fri Jan 18, 3:48 pm)
Re: [PATCH -v6 0/2] Fixing the issue with memory-mapped file..., Anton Salikhmetov, (Fri Jan 18, 6:31 am)
[PATCH -v6 2/2] Updating ctime and mtime for memory-mapped f..., Anton Salikhmetov, (Thu Jan 17, 6:31 pm)
Re: [PATCH -v6 2/2] Updating ctime and mtime for memory-mapp..., Anton Salikhmetov, (Mon Jan 21, 10:36 am)
Re: [PATCH -v6 2/2] Updating ctime and mtime for memory-mapp..., Anton Salikhmetov, (Fri Jan 18, 3:58 pm)
Re: [PATCH -v6 2/2] Updating ctime and mtime for memory-mapp..., Anton Salikhmetov, (Fri Jan 18, 5:03 pm)
Re: [PATCH -v6 2/2] Updating ctime and mtime for memory-mapp..., Anton Salikhmetov, (Fri Jan 18, 6:04 pm)
Re: [PATCH -v6 2/2] Updating ctime and mtime for memory-mapp..., Anton Salikhmetov, (Fri Jan 18, 6:35 pm)
Re: [PATCH -v6 2/2] Updating ctime and mtime for memory-mapp..., Anton Salikhmetov, (Fri Jan 18, 6:39 am)
[PATCH -v6 1/2] Massive code cleanup of sys_msync(), Anton Salikhmetov, (Thu Jan 17, 6:31 pm)
Re: [PATCH -v6 1/2] Massive code cleanup of sys_msync(), Miklos Szeredi, (Fri Jan 18, 5:33 am)
Re: [PATCH -v6 1/2] Massive code cleanup of sys_msync(), Anton Salikhmetov, (Fri Jan 18, 6:30 am)