Skip to contentSkip to conversation

Writing out a (file) mmapped page

DISCUSSION Martin Jambor 5 participants 5 messages Jun 12, 2005 – Jun 14, 2005 VFS

lore
Share & tools
lore
#0 lore raw

Hi,

I have spent a few hours trying to find out how dirty mmapped pages are written out in filesystems using the "generic" functions but so far I have not been successful. The main thing that escapes me is the following:

block_write_full_page() writes out only buffers marked dirty or whole page when there are no buffers associated with it. Where in kernel are buffers either marked dirty or stripped off a mmaped page when the page itself becomes dirty? I would be very grateful for a pointer to the source, possibly accompanied by a brief explanation of how it gets called.

One comment in buffer.c suggests aops->prepare_write is called by a pagefault handler for mmaped pages but I found no such call (using cscope).

Thank you very much for any comment on this,

Martin Jambor

#1 lore raw
Quoted 14 lines · Martin Jambor

Hi,

I have spent a few hours trying to find out how dirty mmapped pages are written out in filesystems using the "generic" functions but so far I have not been successful. The main thing that escapes me is the following:

block_write_full_page() writes out only buffers marked dirty or whole page when there are no buffers associated with it. Where in kernel are buffers either marked dirty or stripped off a mmaped page when the page itself becomes dirty? I would be very grateful for a pointer to the source, possibly accompanied by a brief explanation of how it gets called.

Quoted 3 lines

One comment in buffer.c suggests aops->prepare_write is called by a pagefault handler for mmaped pages but I found no such call (using cscope).

generic_file_buffered_write() calls a_ops->prepare_write().

Signature · 2 lines
Coywolf Qi Hunt
http://ahbl.org/~coywolf/
#2 lore raw
Quoted 14 lines · Coywolf Qi Hunt
Quoted 13 lines · Martin Jambor

I have spent a few hours trying to find out how dirty mmapped pages are written out in filesystems using the "generic" functions but so far I have not been successful. The main thing that escapes me is the following:

block_write_full_page() writes out only buffers marked dirty or whole page when there are no buffers associated with it. Where in kernel are buffers either marked dirty or stripped off a mmaped page when the page itself becomes dirty? I would be very grateful for a pointer to the source, possibly accompanied by a brief explanation of how it gets called.

Quoted 5 lines
Quoted 3 lines

One comment in buffer.c suggests aops->prepare_write is called by a pagefault handler for mmaped pages but I found no such call (using cscope).

generic_file_buffered_write() calls a_ops->prepare_write().

That's the write path, not mmap. writepage() is your friend.

Jörn

Signature · 8 lines
Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it.
-- Brian W. Kernighan
-
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#3 lore raw
In reply to Martin Jambor

Hi,

DISCLAIMER: I am no vm expert and the below is just my take on things. And note it is i386 specific, I known next to nothing about the insides of other architectures... I am sure that if I have described anything wrong someone who knows better will correct me. (-:

Quoted 11 lines · Martin Jambor

I have spent a few hours trying to find out how dirty mmapped pages are written out in filesystems using the "generic" functions but so far I have not been successful. The main thing that escapes me is the following:

block_write_full_page() writes out only buffers marked dirty or whole page when there are no buffers associated with it. Where in kernel are buffers either marked dirty or stripped off a mmaped page when the page itself becomes dirty? I would be very grateful for a pointer to the source, possibly accompanied by a brief explanation of how it gets called.

That is a little trickier than one might expect because it is partially done in hardware (heavily arch dependent though).

When a write to a writable mmapped page happens the CPU sets the page dirty flag in hardware. So there is no code where you can see this happen. Later on, at msync() time or munmap() time, this hardware dirty bit results in a set_page_dirty() being called which for buffer based filesystems will be a __set_page_dirty_buffers() which will dirty both the buffers and the page. For example take the msync case:

mm/msync.c::sys_msync ->

	msync_interval ->
		filemap_sync->
			sync_page_range->
				sync_pud_range ->
					sync_pmd_range ->
						sync_pte_range ->
							set_page_dirty

The beauty of short inline functions. (-:

When the page is not writable, then a page fault occurs and the generic page fault handler is called:

arch/i386/kernel/entry.S defines the page_fault entry:

ENTRY(page_fault)
pushl $do_page_fault
jmp error_code

arch/i386/mm/fault.c::do_page_fault -> (We now move to non-arch specific code.)

	mm/memory.c::handle_mm_fault ->
		handle_pte_fault

Now the code paths diverge depending on what page the fault is hitting (shared or not, read-only or not, swap page or not, page not present in memory at all, etc).

In the end all cases end up generating a writable page (see pte_mkwrite), either by modifying the existing one, or by copying the existing shared page - also known as COW = Copy On Write, or by creating a new page if none was present at all, and doing a pte_mkdirty (or an equivalent thereof).

The pte_mkdirty causes the page to be dirty in the page tables and this gets caught at msync/munmap time and results in set_page_dirty being called as described above. (Note the pte_mkdirty results in exactly the same thing happening to the page as when the cpu does it in the writable-page-already-present case I described above.)

Best regards,

Anton

Signature · 4 lines
Anton Altaparmakov <aia21 at cam.ac.uk> (replace at with @)
Unix Support, Computing Service, University of Cambridge, CB2 3QH, UK
Linux NTFS maintainer / IRC: #ntfs on irc.freenode.net
WWW: http://linux-ntfs.sf.net/ & http://www-stu.christs.cam.ac.uk/~aia21/
#4 lore raw

Anton Altaparmakov writes:

[...]

Quoted 8 lines

That is a little trickier than one might expect because it is partially done in hardware (heavily arch dependent though).

When a write to a writable mmapped page happens the CPU sets the page dirty flag in hardware. So there is no code where you can see this happen. Later on, at msync() time or munmap() time, this hardware dirty bit results in a set_page_dirty() being called which for buffer based

This also happens when page migrates to the cold end of "VM LRU list" (tail of the inactive list, technically).

Nikita.