Skip to contentSkip to conversation

ocfs2: Shared writeable mmap

PATCH NAK Mark Fasheh 5 participants 9 messages Jun 19, 2006 – Jun 20, 2006

Subsystems (5)
Memory management VFS MM core OCFS2 +1 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ocfs2: Shared writeable mmap

I finally got some time to sit down and implement an OCFS2 patch to make use of the ->page_mkwrite() callback added by David Howells' patch (named 'add-page_mkwrite-vm_operations-method.patch' in -mm). The patches, and an MPI program to test this can be found at:

http://kernel.org/pub/linux/kernel/people/mfasheh/ocfs2/mmap/

There's one bug however, which will cause the test program on one of the reading nodes to see stale data if it is run several times in a row against the same file. I have verified that the same thing works fine on a local file system (ext3). I'm not sure where the issue is, but I have a feeling I'm doing something bad in ocfs2_data_convert_worker(). Another possibility is that we missed a place to put the ->page_mkwrite callback.

Unfortunately, I have to step away from this patch for a bit as I have some higher priority issues to deal with :/ Luckily, it seems to be in a state which I think warrants it being pushed out to the public for general review, testing, etc. If anyone is interested, I'd also appreciate any advice or help regarding the bug -- my VM-foo is very weak :)

	--Mark

--
Mark Fasheh
Senior Software Developer, Oracle
[email protected]

From: Mark Fasheh <[email protected]>

ocfs2: Shared writeable mmap

Implement cluster consistent shared writeable mappings using the ->page_mkwrite() callback.

---

 fs/ocfs2/dlmglue.c |   10 +++++
 fs/ocfs2/mmap.c    |  100 ++++++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 90 insertions(+), 20 deletions(-)

4c6c09a7927affae4616607c9f0da0a95b232baa

fs/ocfs2/dlmglue.c +9 -1
index 64cd528..d57860d 100644
@@ -2685,6 +2685,15 @@ static void ocfs2_data_convert_worker(st
        	inode = ocfs2_lock_res_inode(lockres);
 	mapping = inode->i_mapping;
 
+	/*
+	 * We need this before the filemap_fdatawrite() so that it can
+	 * transfer the dirty bit from the PTE to the
+	 * page. Unfortunately this means that even for EX->PR
+	 * downconverts, we'll lose our mappings and have to build
+	 * them up again.
+	 */
+	unmap_mapping_range(mapping, 0, 0, 0);
+
 	if (filemap_fdatawrite(mapping)) {
 		mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
 		     (unsigned long long)OCFS2_I(inode)->ip_blkno);
@@ -2692,7 +2701,6 @@ static void ocfs2_data_convert_worker(st
 	sync_mapping_buffers(mapping);
 	if (blocking == LKM_EXMODE) {
 		truncate_inode_pages(mapping, 0);
-		unmap_mapping_range(mapping, 0, 0, 0);
 	} else {
 		/* We only need to wait on the I/O if we're not also
 		 * truncating pages because truncate_inode_pages waits
fs/ocfs2/mmap.c +81 -19

129 lines not on this page · raw message

Signature · 1 line
1.3.3
#1 lore raw
Quoted 14 lines · Mark Fasheh

I finally got some time to sit down and implement an OCFS2 patch to make use of the ->page_mkwrite() callback added by David Howells' patch (named 'add-page_mkwrite-vm_operations-method.patch' in -mm). The patches, and an MPI program to test this can be found at:

http://kernel.org/pub/linux/kernel/people/mfasheh/ocfs2/mmap/

There's one bug however, which will cause the test program on one of the reading nodes to see stale data if it is run several times in a row against the same file. I have verified that the same thing works fine on a local file system (ext3). I'm not sure where the issue is, but I have a feeling I'm doing something bad in ocfs2_data_convert_worker(). Another possibility is that we missed a place to put the ->page_mkwrite callback.

Quoted 6 lines

Unfortunately, I have to step away from this patch for a bit as I have some higher priority issues to deal with :/ Luckily, it seems to be in a state which I think warrants it being pushed out to the public for general review, testing, etc. If anyone is interested, I'd also appreciate any advice or help regarding the bug -- my VM-foo is very weak :)

	--Mark

Hi Mark,

While this may be a great patch, you didn't actually explain what it does, how it does it or why it does it.

Regards,

Daniel

#2 lore raw
Quoted 2 lines · Daniel Phillips

While this may be a great patch, you didn't actually explain what it does, how it does it or why it does it.

Essentially data in OCFS2 is already covered by a cluster lock. We simply make use of the lock in ->page_mkwrite to ensure that data written via shared writeable mmap will be coherent with respect to other nodes.

Cluster locks can have levels of NL (no lock), PR (protected read, aka shared) or EX (exclusive). The act of taking the lock on one node triggers actions on the other nodes which have competing lock levels. In the case of locks covering inode data, this means that the other nodes will do a combination of sync and invalidate against their pages. This is what ocfs2_data_convert_worker() does.

So the patch just makes use of the callback to take a data lock on the associated inode. We also take a meta data lock to verify inode size against the page index, but I'm not completely sure that piece is absolutely necessary as we should've already done it from ->nopage.

	--Mark

--
Mark Fasheh
Senior Software Developer, Oracle
[email protected]

#3 lore raw
In reply to Mark Fasheh
Quoted 15 lines · Mark Fasheh

I finally got some time to sit down and implement an OCFS2 patch to make use of the ->page_mkwrite() callback added by David Howells' patch (named 'add-page_mkwrite-vm_operations-method.patch' in -mm). The patches, and an MPI program to test this can be found at:

http://kernel.org/pub/linux/kernel/people/mfasheh/ocfs2/mmap/

There's one bug however, which will cause the test program on one of the reading nodes to see stale data if it is run several times in a row against the same file. I have verified that the same thing works fine on a local file system (ext3). I'm not sure where the issue is, but I have a feeling I'm doing something bad in ocfs2_data_convert_worker(). Another possibility is that we missed a place to put the ->page_mkwrite callback.

Quoted 5 lines

Unfortunately, I have to step away from this patch for a bit as I have some higher priority issues to deal with :/ Luckily, it seems to be in a state which I think warrants it being pushed out to the public for general review, testing, etc. If anyone is interested, I'd also appreciate any advice or help regarding the bug -- my VM-foo is very weak :)

Peter Zijlstra told me yesterday:

There is a problem with the page-mkwrite last posted to lkml. /me checks your tree... Yeah, that version has a problem:
http://kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.17-rc6/2.6.17-rc6-mm2/broken-out/add-page_mkwrite-vm_operations-method.patch

The thing is that get_user_pages(.write=1, .force=1) can generate COW hits on read-only shared mappings, this patch traps those as mkpage_write candidates and fails to handle them the old way.

Which I was unaware of and haven't started to think about. Probably I'll drop the existing patch and pick up the one he's sending out. But it's presumably based on top of all the dirty-page-tracking patches which I also haven't thought about yet and which need _serious_ thought.

It would be better to get a fix against the existing add-page_mkwrite-vm_operations-method.patch so at least we can get that merged up. But nobody seems to be offering that.

#4 lore raw
Quoted 3 lines · Andrew Morton

The thing is that get_user_pages(.write=1, .force=1) can generate COW hits on read-only shared mappings, this patch traps those as mkpage_write candidates and fails to handle them the old way.

Ahh ok. Too bad this doesn't seem like it could be causing my bug :( Can I ask what the old way of handling this is?

Quoted 3 lines

It would be better to get a fix against the existing add-page_mkwrite-vm_operations-method.patch so at least we can get that merged up. But nobody seems to be offering that.

Were I capable, I'd certainly offer up a patch :/ Definitely though, I agree that sorting this out first seems like a reasonable way to go.

	--Mark

--
Mark Fasheh
Senior Software Developer, Oracle
[email protected]

#5 lore raw
In reply to Andrew Morton
Quoted 24 lines · Andrew Morton
Quoted 15 lines · Mark Fasheh

I finally got some time to sit down and implement an OCFS2 patch to make use of the ->page_mkwrite() callback added by David Howells' patch (named 'add-page_mkwrite-vm_operations-method.patch' in -mm). The patches, and an MPI program to test this can be found at:

http://kernel.org/pub/linux/kernel/people/mfasheh/ocfs2/mmap/

There's one bug however, which will cause the test program on one of the reading nodes to see stale data if it is run several times in a row against the same file. I have verified that the same thing works fine on a local file system (ext3). I'm not sure where the issue is, but I have a feeling I'm doing something bad in ocfs2_data_convert_worker(). Another possibility is that we missed a place to put the ->page_mkwrite callback.

Quoted 5 lines

Unfortunately, I have to step away from this patch for a bit as I have some higher priority issues to deal with :/ Luckily, it seems to be in a state which I think warrants it being pushed out to the public for general review, testing, etc. If anyone is interested, I'd also appreciate any advice or help regarding the bug -- my VM-foo is very weak :)

Peter Zijlstra told me yesterday:

Quoted 7 lines

There is a problem with the page-mkwrite last posted to lkml. /me checks your tree... Yeah, that version has a problem:
http://kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.17-rc6/2.6.17-rc6-mm2/broken-out/add-page_mkwrite-vm_operations-method.patch

The thing is that get_user_pages(.write=1, .force=1) can generate COW hits on read-only shared mappings, this patch traps those as mkpage_write candidates and fails to handle them the old way.

The -v9 version of the dirty page tracking I send out fixes this problem by affiliation; the following patch should also be enough:

---
 mm/memory.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/memory.c +2 -1
Index: 2.6-mm/mm/memory.c
===================================================================
@@ -1464,7 +1464,8 @@ static int do_wp_page(struct mm_struct *
 	if (!old_page)
 		goto gotten;
 
-	if (unlikely(vma->vm_flags & VM_SHARED)) {
+	if (unlikely(vma->vm_flags & (VM_SHARED|VM_WRITE) ==
+				VM_SHARED|VM_WRITE) {
 		if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
 			/*
 			 * Notify the address space that the page is about to
#6 lore raw

mm/memory.c static int do_wp_page(struct mm_struct * · line 1468

Quoted 3 lines · Peter Zijlstra
mm/memory.c +2 -1
-	if (unlikely(vma->vm_flags & VM_SHARED)) {
+	if (unlikely(vma->vm_flags & (VM_SHARED|VM_WRITE) ==
+				VM_SHARED|VM_WRITE) {

NAK!

"==" is higher priority than "|". What you meant was:

- if (unlikely(vma->vm_flags & VM_SHARED)) {
+ if (unlikely(vma->vm_flags & (VM_SHARED|VM_WRITE) ==
+ (VM_SHARED|VM_WRITE)) {

David

#7 lore raw

mm/memory.c static int do_wp_page(struct mm_struct *

Quoted 13 lines · David Howells

mm/memory.c static int do_wp_page(struct mm_struct * · line 1468

Quoted 3 lines · Peter Zijlstra
mm/memory.c +2 -1
-	if (unlikely(vma->vm_flags & VM_SHARED)) {
+	if (unlikely(vma->vm_flags & (VM_SHARED|VM_WRITE) ==
+				VM_SHARED|VM_WRITE) {

NAK!

"==" is higher priority than "|". What you meant was:

mm/memory.c +2 -1
-	if (unlikely(vma->vm_flags & VM_SHARED)) {
+	if (unlikely(vma->vm_flags & (VM_SHARED|VM_WRITE) ==
+				(VM_SHARED|VM_WRITE)) {

Or, rather:

- if (unlikely(vma->vm_flags & VM_SHARED)) {
+ if (unlikely(vma->vm_flags & (VM_SHARED|VM_WRITE) ==
+ (VM_SHARED|VM_WRITE))) {

It has insufficient closing brackets otherwise.

David

#8 lore raw

PATCH 1/1 Add notification of page becoming writable to VMA ops

From: David Howells <[email protected]>

The attached patch adds a new VMA operation to notify a filesystem or other driver about the MMU generating a fault because userspace attempted to write to a page mapped through a read-only PTE.

This facility permits the filesystem or driver to:

(*) Implement storage allocation/reservation on attempted write, and so to
deal with problems such as ENOSPC more gracefully (perhaps by generating SIGBUS).

(*) Delay making the page writable until the contents have been written to a
backing cache. This is useful for NFS/AFS when using FS-Cache/CacheFS. It permits the filesystem to have some guarantee about the state of the cache.

(*) Account and limit number of dirty pages. This is one piece of the puzzle
needed to make shared writable mapping work safely in FUSE.

The patch has been fixed to only check pages in shared writable VMAs in do_wp_page(), not in all shared VMAs.

---

 include/linux/mm.h |    4 ++
 mm/memory.c        |  100 ++++++++++++++++++++++++++++++++++++++++------------
 mm/mmap.c          |   12 +++++-
 mm/mprotect.c      |   11 +++++-
 4 files changed, 99 insertions(+), 28 deletions(-)
include/linux/mm.h +4 -0
index 1154684..cd3c2cf 100644
@@ -200,6 +200,10 @@ struct vm_operations_struct {
 	void (*close)(struct vm_area_struct * area);
 	struct page * (*nopage)(struct vm_area_struct * area, unsigned long address, int *type);
 	int (*populate)(struct vm_area_struct * area, unsigned long address, unsigned long len, pgprot_t prot, unsigned long pgoff, int nonblock);
+
+	/* notification that a previously read-only page is about to become
+	 * writable, if an error is returned it will cause a SIGBUS */
+	int (*page_mkwrite)(struct vm_area_struct *vma, struct page *page);
 #ifdef CONFIG_NUMA
 	int (*set_policy)(struct vm_area_struct *vma, struct mempolicy *new);
 	struct mempolicy *(*get_policy)(struct vm_area_struct *vma,
mm/memory.c +76 -24

125 lines not on this page · raw message

mm/mmap.c +10 -2
index e6ee123..6446c61 100644
@@ -1065,7 +1065,8 @@ munmap_back:
 	vma->vm_start = addr;
 	vma->vm_end = addr + len;
 	vma->vm_flags = vm_flags;
-	vma->vm_page_prot = protection_map[vm_flags & 0x0f];
+	vma->vm_page_prot = protection_map[vm_flags &
+				(VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)];
 	vma->vm_pgoff = pgoff;
 
 	if (file) {
@@ -1089,6 +1090,12 @@ munmap_back:
 			goto free_vma;
 	}
 
+	/* Don't make the VMA automatically writable if it's shared, but the
+	 * backer wishes to know when pages are first written to */
+	if (vma->vm_ops && vma->vm_ops->page_mkwrite)
+		vma->vm_page_prot =
+			protection_map[vm_flags & (VM_READ|VM_WRITE|VM_EXEC)];
+
 	/* We set VM_ACCOUNT in a shared mapping's vm_flags, to inform
 	 * shmem_zero_setup (perhaps called through /dev/zero's ->mmap)
 	 * that memory reservation must be checked; but that reservation
@@ -1921,7 +1928,8 @@ unsigned long do_brk(unsigned long addr,
 	vma->vm_end = addr + len;
 	vma->vm_pgoff = pgoff;
 	vma->vm_flags = flags;
-	vma->vm_page_prot = protection_map[flags & 0x0f];
+	vma->vm_page_prot = protection_map[flags &
+				(VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)];
 	vma_link(mm, vma, prev, rb_link, rb_parent);
 out:
 	mm->total_vm += len >> PAGE_SHIFT;
mm/mprotect.c +9 -2
index 4c14d42..2697abd 100644
@@ -106,6 +106,7 @@ mprotect_fixup(struct vm_area_struct *vm
 	unsigned long oldflags = vma->vm_flags;
 	long nrpages = (end - start) >> PAGE_SHIFT;
 	unsigned long charged = 0;
+	unsigned int mask;
 	pgprot_t newprot;
 	pgoff_t pgoff;
 	int error;
@@ -132,8 +133,6 @@ mprotect_fixup(struct vm_area_struct *vm
 		}
 	}
 
-	newprot = protection_map[newflags & 0xf];
-
 	/*
 	 * First try to merge with previous and/or next vma.
 	 */
@@ -160,6 +159,14 @@ mprotect_fixup(struct vm_area_struct *vm
 	}
 
 success:
+	/* Don't make the VMA automatically writable if it's shared, but the
+	 * backer wishes to know when pages are first written to */
+	mask = VM_READ|VM_WRITE|VM_EXEC|VM_SHARED;
+	if (vma->vm_ops && vma->vm_ops->page_mkwrite)
+		mask &= ~VM_SHARED;
+
+	newprot = protection_map[newflags & mask];
+
 	/*
 	 * vm_flags and vm_page_prot are protected by the mmap_sem
 	 * held in write mode.