commit_write less than prepared by prepare_write
Subsystems (3)
This is a request for comments and review by filesystem maintainers. Especially if you own GFS2, OCFS2, Reiserfs, JFFS! Those using fs/buffer.c routines directly should be reasonably safe.
We have several long standing deadlocks in the core MM when doing buffered writes. The full explanation is in 2.6.19-rc2-mm2 in the patch mm-fix-pagecache-write-deadlocks.patch and there are some followup fixes.
Basically: when copying the pages from user supplied address to pagecache, between prepare_write and commit_write, we can take a fault on the copy_from_user and enter the pagefault handler holding the page lock.
There is no way that this can work safely, so we must do atomic copies here, which will just return a short write in the case of a fault.
The protocol for handling short writes is as follows:
- If page uptodate, commit_write with the shorter length (potentially 0). Move the counters forward and retry.
- If the page is not uptodate, we commit a zero length commit_write, at the start offset given by the prepare_write being closed.
* Note that if we hit some other error condition, we may never run another prepare_write or commit_write, so we can't rely on that.
For the case of an uptodate page, it may be somewhat safer to commit the full length that was prepared (we can always pretend that the uncopied part got dirtied; the retry can happily overwrite it). Comments? I figure that if we have to audit everything anyway, then keeping this wart would be silly.
For the non-uptodate page:
We can't run a full length commit_write, because the data in the page is uninitialised. Can't zero out the uninitialised data, because that would lead to zeros temporarily appearing in the pagecache. Any other ways to fix it?
The problem with doing a short commit, as noted by Mark Fasheh, is that prepare_write might have allocated a disk mapping (eg. if we write over a hole), and if the commit fails and we unlock the page, then a read from the filesystem might think that is valid data.
Mark's idea is to loop through the page buffers and zero out the buffer_new ones when the caller detects a fault here. This should work because a hole is zeroes anyway, so we could mark the page uptodate and it would eventually get written back to initialise those blocks. But, is there any other filesystem specific state that would need to be fixed up? Do we need another filesystem call?
I would prefer that disk block allocation be performed at commit_write time. I realise that is probably a lot more rework, and there may be reasons why it can't be done?
Another alternative is to pre-read the page in prepare_write, which should always get us out of trouble. Could be a good option for simple filesystems. People who care about performance probably want to avoid this.
Any other ideas?
The regular buffered filesystems seem to be working OK. The bulk of the filesystems are in fs-prepare_write-fixes.patch, but it is a bit scattered at the moment. Applying the full -mm patch would be a good idea.
The conversion involves:
* ensure we don't mark the page uptodate in prepare_write if it is not (which is a bug anyway... I found a few of those).
* ensure we don't mark a non uptodate page as uptodate if the commit was short.
Filesystems that are non trivial are GFS2, OCFS2, Reiserfs, JFFS so I need maintainers to look at those.
For the "moronic filesystems that do not allow holes", the cont_ routines have already been using zero length prepare/commit for something else. OGAWA Hirofumi is thinking about this. Any comments?
Thanks,
Nick
Quoted 3 lines · Nick Piggin
Filesystems that are non trivial are GFS2, OCFS2, Reiserfs, JFFS so I need maintainers to look at those.
JFFS has been unmaintained for some years and barely evaded removal at least once. Now might be the time to pull support for good or else convert it by adding an #error to it.
Jörn
Signature · 6 lines
This above all: to thine own self be true. -- Shakespeare - 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
Hello
Quoted 24 lines · Nick Piggin
This is a request for comments and review by filesystem maintainers. Especially if you own GFS2, OCFS2, Reiserfs, JFFS! Those using
fs/buffer.croutines directly should be reasonably safe.We have several long standing deadlocks in the core MM when doing buffered writes. The full explanation is in 2.6.19-rc2-mm2 in the patch mm-fix-pagecache-write-deadlocks.patch and there are some followup fixes.
Basically: when copying the pages from user supplied address to pagecache, between
prepare_writeandcommit_write, we can take a fault on thecopy_from_userand enter the pagefault handler holding the page lock.There is no way that this can work safely, so we must do atomic copies here, which will just return a short write in the case of a fault.
The protocol for handling short writes is as follows:
- If page uptodate,
commit_writewith the shorter length (potentially 0). Move the counters forward and retry.- If the page is not uptodate, we commit a zero length
commit_write, at the start offset given by theprepare_writebeing closed.
Quoted 6 lines
* Note that if we hit some other error condition, we may never run another
prepare_writeorcommit_write, so we can't rely on that.For the case of an uptodate page, it may be somewhat safer to commit the full length that was prepared (we can always pretend that the uncopied part got dirtied; the retry can happily overwrite it). Comments?
Retry can EFAULT, I guess. For example, if file mapped to user space gets truncated.
Quoted 55 lines
I figure
that if we have to audit everything anyway, then keeping this wart would be silly.For the non-uptodate page:
We can't run a full lengthcommit_write, because the data in the page is uninitialised. Can't zero out the uninitialised data, because that would lead to zeros temporarily appearing in the pagecache. Any other ways to fix it?The problem with doing a short commit, as noted by Mark Fasheh, is that
prepare_writemight have allocated a disk mapping (eg. if we write over a hole), and if the commit fails and we unlock the page, then a read from the filesystem might think that is valid data.Mark's idea is to loop through the page buffers and zero out the
buffer_newones when the caller detects a fault here. This should work because a hole is zeroes anyway, so we could mark the page uptodate and it would eventually get written back to initialise those blocks. But, is there any other filesystem specific state that would need to be fixed up? Do we need another filesystem call?I would prefer that disk block allocation be performed at
commit_writetime. I realise that is probably a lot more rework, and there may be reasons why it can't be done?Another alternative is to pre-read the page in
prepare_write, which should always get us out of trouble. Could be a good option for simple filesystems. People who care about performance probably want to avoid this.Any other ideas?
The regular buffered filesystems seem to be working OK. The bulk of the filesystems are in fs-prepare_write-fixes.patch, but it is a bit scattered at the moment. Applying the full -mm patch would be a good idea.
The conversion involves:
* ensure we don't mark the page uptodate inprepare_writeif it is not (which is a bug anyway... I found a few of those).
* ensure we don't mark a non uptodate page as uptodate if the commit was short.Filesystems that are non trivial are GFS2, OCFS2, Reiserfs, JFFS so I need maintainers to look at those.
For the "moronic filesystems that do not allow holes", the
cont_routines have already been using zero length prepare/commit for something else. OGAWA Hirofumi is thinking about this. Any comments?Thanks,
Nick
Quoted 22 lines · Nick Piggin
For the non-uptodate page:
We can't run a full lengthcommit_write, because the data in the page is uninitialised. Can't zero out the uninitialised data, because that would lead to zeros temporarily appearing in the pagecache. Any other ways to fix it?The problem with doing a short commit, as noted by Mark Fasheh, is that
prepare_writemight have allocated a disk mapping (eg. if we write over a hole), and if the commit fails and we unlock the page, then a read from the filesystem might think that is valid data.Mark's idea is to loop through the page buffers and zero out the
buffer_newones when the caller detects a fault here. This should work because a hole is zeroes anyway, so we could mark the page uptodate and it would eventually get written back to initialise those blocks. But, is there any other filesystem specific state that would need to be fixed up? Do we need another filesystem call?I would prefer that disk block allocation be performed at
commit_writetime. I realise that is probably a lot more rework, and there may be reasons why it can't be done?
Quoted 6 lines
Another alternative is to pre-read the page in
prepare_write, which should always get us out of trouble. Could be a good option for simple filesystems. People who care about performance probably want to avoid this.Any other ideas?
OK, so I was able to pretty easily reproduce the stale data problem with CONFIG_DEBUG_VM turned on (if anyone wants a copy of the test program, let me know privately).
The following patch attempts to implement Mark's idea, and does seem to work for ext2 bh. nobh seem to be broken in a non-trivial way. Because we discard the bh, we don't know that we might have just allocated under a hole. The old "error handling" zeroed out the entire page which, AFAIKS, could destroy existing data. My patches in -mm ripped this out which introduces the same sort of stale data problem as when doing a zero length commit (arguably better? probably not: one is data corruption, the other is a data corruption + information leak).
So nobh isn't fixed, but a note is made of it. I think it was broken when I got there, though?
Are there any other filesystems that don't mark buffer_new properly, and also don't zero out allocated holes? If so, I think they are also broken before I did anything (think -EFAULT case).
Nick
--
mm/filemap.c +8 -2
@@ -1951,7 +1951,7 @@ retry_noprogress: bytes);dec_preempt_count (); -if (!PageUptodate (page)) { +if (unlikely (copied != bytes)) {/* * If the page is not uptodate, we cannot allow a * partial commit_write because when we unlock the
@@ -1967,9 +1967,15 @@ retry_noprogress:* single-segment path below, which should get the * filesystem to bring the page uputodate for us next * time. +* +* Must zero out new buffers here so that we do end +* up properly filling holes rather than leaving stale +* data in them that might be read in future. */ -if (unlikely (copied != bytes)) +if (!PageUptodate (page)) { +page_zero_new_buffers (page); copied =0 ; + } }flush_dcache_page (page);
fs/buffer.c +62 -66
169 lines not on this page · raw message
include/linux/buffer_head.h +1 -0
@@ -151,6 +151,7 @@ struct buffer_head *alloc_page_buffers(s int retry);void create_empty_buffers (struct page *,unsigned long ,unsigned long b_state); +void page_zero_new_buffers (struct page *page);void end_buffer_read_sync (struct buffer_head *bh,int uptodate);void end_buffer_write_sync (struct buffer_head *bh,int uptodate);