PATCH: Rewrite of truncate_inode_pages (WIP)
Subsystems (5)
Hi
the actual truncate_inode_pages can actually *write* part of
the file that has just been truncated. That makes that things doesn't work. I can trigger the BUG in fs:/inode.c:clear_inode using mmap002 and riel aging patch in 2.4.0-test1. The BUG triggered is:
if (inode->i_data.nrpages) BUG();
The problem is that inode->i_data.nrpages can become negative. I have got here -6 truncating a file of 384MB.
This patch deals with this problem, and try to do truncate_inode_pages safe.
Notice that the function only works for truncate complete files, I have left the old function for the partial case. If people think that this function is OK, I will incorporate the partial case. I have no done it because I want feedback from the MM people and the FS people, and the algorithms for complete files are cleaner.
I would like to now if it is possible to have a locked buffer without the page being locked, i.e. If I can manipulate the buffers in one page without locking them when I have the page lock. (I don't think so, and then I have to lock also buffers).
With this patch things are rock solid here.
This patch does:
- defines a new function: discard_buffer, it will lock the buffer (waiting if needed) and remove the buffer from all the queues. It is like unmap_buffer, but makes sure that we don't do any IO and that we remove the buffer from all the lists.
- defines a new function: block_destroy_buffers: it is a mix of block_flushpage and do_try_to_free_pages. It will make all the buffers in that page disappear calling discard_buffer. Notice the way that we iterate through all the buffer heads. I think that it is not racy, but I would like to hear comments from people than know more about buffer heads handling.
- It changes __remove_inode_pages to check that there are not buffers in that page. Users of the function must make sure about that. It changes all the callers of the function satisfy that requirement.
- I change invalidate_inode_pages (again). Now block_destroy_buffers can wait, then we are *civilized* citizens and drop any lock that we have before call that block_destroy_buffers, and reaquire later.
- It has my *old* rewrite of truncate_inode_pages to use two auxiliary functions and lock the same way for the partial page and for the rest. (That code is wrong anyway, pass to the next function).
- The new function truncate_inode_pages, that is a copy of invalidate_inodes, but we wait for locked pages.
Comments, please??
I am very interested in the coments from the fs-people. What they think about the locking that I am using in the buffer.c file.
Acknolegments: This patch would have not been possible without the comments or
arjan, bcrl, davej, riel and RogerL, .... in #kernelnewbies (irc.openprojects.net). They did a lot of review of previous versions of the patch. The errors are mine, but good ideas come from discussions with them :)
Later, Juan.
fs/buffer.c +86 -17
@@ -1281,6 +1281,56 @@ } } +/** +* discard_buffer - discard that buffer without doing any IO +* @bh: buffer to discard +* +* This function removes a buffer from all the queues, without doing +* any IO, we are not interested in the contents of the buffer. This +* function can block if the buffer is locked. +*/ +static struct buffer_head *discard_buffer (struct buffer_head * bh) +{ +int index =BUFSIZE_INDEX (bh->b_size); +struct buffer_head *next; + +/* grab the lru lock here to block bdflush. */ +atomic_inc (&bh->b_count); +lock_buffer (bh); + next = bh->b_this_page; +clear_bit (BH_Uptodate, &bh->b_state); +clear_bit (BH_Mapped, &bh->b_state); +clear_bit (BH_Req, &bh->b_state); +clear_bit (BH_New, &bh->b_state); + +spin_lock (&lru_list_lock); +write_lock (&hash_table_lock); +spin_lock (&free_list[index].lock); +spin_lock (&unused_list_lock); + +if (!atomic_dec_and_test (&bh->b_count)) +BUG (); + +__hash_unlink (bh); +/* The bunffer can be either on the regular +* queues or on the free list.. +*/ +if (bh->b_dev !=B_FREE ) +__remove_from_queues (bh); +else +__remove_from_free_list (bh, index); +__put_unused_buffer_head (bh); +spin_unlock (&unused_list_lock); +write_unlock (&hash_table_lock); +spin_unlock (&free_list[index].lock); +spin_unlock (&lru_list_lock); +/* We can unlock the buffer, we have just returned it. +* Ditto for the counter +*/ +return next; +} + +/* * We don't have to release all buffers here, but * we have to be sure that no dirty buffer is left
@@ -1313,24 +1363,43 @@ bh = next; }while (bh != head); -/* -* subtle. We release buffer-heads only if this is -* the 'final' flushpage. We have invalidated the get_block -* cached value unconditionally, so real IO is not -* possible anymore. -* -* If the free doesn't work out, the buffers can be -* left around - they just turn into anonymous buffers -* instead. -*/ -if (!offset) { -if (!try_to_free_buffers (page,0 )) { -atomic_inc (&buffermem_pages); -return 0 ; - } - } -return 1 ; +} + +/** +* block_destroy_buffers - Will destroy the contents of all the +* buffers in this page +* @page: page to examine the buffers +* +* This function destroy all the buffers in one page without making +* any IO. The function can block due to the fact that discad_bufferr +* can block. +*/ +void block_destroy_buffers (struct page *page) +{ +struct buffer_head *bh, *head; + +if (!PageLocked (page)) +BUG (); +if (!page->buffers) +return ; + + head = page->buffers; + bh = head; +do { +/* We need to get the next buffer from discard buffer +* because discard buffer can block and anybody else +* can change the buffer list under our feet. +*/ + bh =discard_buffer (bh); + }while (bh != head); + +/* Wake up anyone waiting for buffer heads */ +wake_up (&buffer_wait); + +/* And free the page */ + page->buffers =NULL ; +page_cache_release (page); }static void create_empty_buffers (struct page *page,struct inode *inode,unsigned long blocksize)
fs/inode.c +2 -2
@@ -322,7 +322,7 @@ inode = list_entry(inode_entry, struct inode, i_list);if (inode->i_data.nrpages) -truncate_inode_pages (&inode->i_data,0 ); +truncate_all_inode_pages (&inode->i_data);clear_inode (inode);destroy_inode (inode); }
@@ -768,7 +768,7 @@spin_unlock (&inode_lock);if (inode->i_data.nrpages) -truncate_inode_pages (&inode->i_data,0 ); +truncate_all_inode_pages (&inode->i_data); destroy =1 ;if (op && op->delete_inode) {
include/linux/fs.h +1 -0
@@ -1104,6 +1104,7 @@/* Generic buffer handling for block filesystems.. */ extern int block_flushpage (struct page *,unsigned long ); +extern void block_destroy_buffers (struct page *);extern int block_symlink (struct inode *,const char *,int );extern int block_write_full_page (struct page *,get_block_t *);extern int block_read_full_page (struct page *,get_block_t *);
include/linux/mm.h +1 -0
@@ -457,6 +457,7 @@extern unsigned long page_unuse (struct page *);extern int shrink_mmap (int ,int );extern void truncate_inode_pages (struct address_space *,loff_t ); +extern void truncate_all_inode_pages (struct address_space *);/* generic vm_area_ops exported for stackable file systems */ extern int filemap_swapout (struct page * page,struct file *file);
mm/filemap.c +103 -49
227 lines not on this page · raw message
mm/swap_state.c +3 -2
@@ -102,9 +102,10 @@if (!PageLocked (page))BUG (); -if (block_flushpage (page,0 )) -lru_cache_del (page); +if (page->buffers) +block_destroy_buffers (page); +lru_cache_del (page);__delete_from_swap_cache (page);page_cache_release (page); }
Signature · 6 lines
In theory, practice and theory are the same, but in practice they are different -- Larry McVoy -- 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.eu.org/Linux-MM/
Hi
sorry I forget one page_cache_release in invalidate_inode_pages. This patch is incremental over the previous one. Sorry for any inconveniences.
Later, Juan.
mm/filemap.c +1 -0
@@ -146,6 +146,7 @@lru_cache_del (page);page_cache_release (page);UnlockPage (page); +page_cache_release (page); goto repeat; }__remove_inode_page (page);
Signature · 6 lines
In theory, practice and theory are the same, but in practice they are different -- Larry McVoy -- 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.eu.org/Linux-MM/
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
" " == Juan J Quintela <[email protected]> writes:
> This patch does:
> - defines a new function: discard_buffer, it will lock the buffer
> (waiting if needed) and remove the buffer from all the
> queues. It is like unmap_buffer, but makes sure that we
> don't do any IO and that we remove the buffer from all the
> lists.
> - defines a new function: block_destroy_buffers: it is a mix of
> block_flushpage and do_try_to_free_pages. It will make all
> the buffers in that page disappear calling discard_buffer.
> Notice the way that we iterate through all the buffer heads.
> I think that it is not racy, but I would like to hear
> comments from people than know more about buffer heads
> handling.
> - I change invalidate_inode_pages (again). Now block_destroy_buffers
> can wait, then we are *civilized* citizens and drop any lock
> that we have before call that block_destroy_buffers, and
> reaquire later.
This is a bug! invalidate_inode_pages() is not supposed to invalidate pending writes. It is only supposed to invalidate the page cache.
Also, it is pointless to add block device-specific code to that particular function since no block devices actually use it.
Cheers,
Trond
--
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.eu.org/Linux-MM/
Quoted 19 lines
Quoted 18 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
"trond" == Trond Myklebust <[email protected]> writes:
" " == Juan J Quintela <[email protected]> writes:
This patch does:
- defines a new function:
discard_buffer, it will lock the buffer (waiting if needed) and remove the buffer from all the queues. It is likeunmap_buffer, but makes sure that we don't do any IO and that we remove the buffer from all the lists.- defines a new function:
block_destroy_buffers: it is a mix ofblock_flushpageanddo_try_to_free_pages. It will make all the buffers in that page disappear callingdiscard_buffer. Notice the way that we iterate through all the buffer heads. I think that it is not racy, but I would like to hear comments from people than know more about buffer heads handling.
Quoted 4 lines
Quoted 4 lines
- I change
invalidate_inode_pages(again). Nowblock_destroy_bufferscan wait, then we are *civilized* citizens and drop any lock that we have before call thatblock_destroy_buffers, and reaquire later.
trond> This is a bug! invalidate_inode_pages() is not supposed to invalidate trond> pending writes. It is only supposed to invalidate the page cache.
OK, the problem here is that __remove_inode_pages needs to be called with page->buffers==NULL. What do you suggest to obtain that?
We don't invalidate the pending write, we only *make sure* that the buffers of the page are freed, i.e. We wait for the writes to finish.
trond> Also, it is pointless to add block device-specific code to that trond> particular function since no block devices actually use it.
Ok, tell me *the* correct way of doing that. We need to make sere that __remove_inode_page is called with page->buffers == NULL. It is ok for you:
if(page->buffers)
BUG();
If we don't make a test like that (or the *actual* one), we left pages in the page cache which contents are invalid, and when shrink_mmap try to free that page, it would write that buffers (buffers that are in the invalidated part).
Thanks a lot for the comments.
Later,Juan.
Signature · 6 lines
In theory, practice and theory are the same, but in practice they are different -- Larry McVoy -- 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.eu.org/Linux-MM/
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
" " == Juan J Quintela <[email protected]> writes:
> OK, the problem here is that __remove_inode_pages needs to be
> called with page->buffers==NULL. What do you suggest to obtain
> that?
> Ok, tell me *the* correct way of doing that. We need to make
> sere that __remove_inode_page is called with page->buffers ==
> NULL. It is ok for you:
> if(page->buffers)
> BUG();That's good. It won't affect NFS or smbfs, and it will catch any block devices that try to use that function.
Cheers,
Trond
--
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.eu.org/Linux-MM/
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
"trond" == Trond Myklebust <[email protected]> writes:
" " == Juan J Quintela <[email protected]> writes:
OK, the problem here is that
__remove_inode_pagesneeds to be called with page->buffers==NULL. What do you suggest to obtain that?
Quoted 5 lines
Quoted 5 lines
Ok, tell me *the* correct way of doing that. We need to make sere that
__remove_inode_pageis called withpage->buffers== NULL. It is ok for you:if(page->buffers)BUG();
trond> That's good. It won't affect NFS or smbfs, and it will catch any block trond> devices that try to use that function.
Here is the patch, alan, please apply.
Later, Juan.
mm/filemap.c +7 -14
@@ -119,7 +119,9 @@* @inode: the inode which pages we want to invalidate * * This function only removes the unlocked pages, if you want to -* remove all the pages of one inode, you must call truncate_inode_pages. +* remove all the pages of one inode, you must call +* truncate_inode_pages. This function is not supposed to be called +* by block based filesystems. */ void invalidate_inode_pages (struct inode * inode) {
@@ -127,7 +129,6 @@struct page * page; head = &inode->i_mapping->pages; -repeat:spin_lock (&pagecache_lock);spin_lock (&pagemap_lru_lock); curr = head->next;
@@ -139,18 +140,10 @@/* We cannot invalidate a locked page */ if (TryLockPage (page))continue ; -if (page->buffers) { -page_cache_get (page); -spin_unlock (&pagemap_lru_lock); -spin_unlock (&pagecache_lock); -block_destroy_buffers (page); -remove_inode_page (page); -lru_cache_del (page); -page_cache_release (page); -UnlockPage (page); -page_cache_release (page); - goto repeat; - } +/* We _should not be called_ by block based filesystems */ +if (page->buffers) +BUG (); +__remove_inode_page (page);__lru_cache_del (page);UnlockPage (page);
Signature · 6 lines
In theory, practice and theory are the same, but in practice they are different -- Larry McVoy -- 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.eu.org/Linux-MM/