btrfs: check page->mapping when loading free space cache
PATCH 1/2 btrfs: check page->mapping when loading free space cache
While testing 5.2 we ran into the following panic
[52238.017028] BUG: kernel NULL pointer dereference, address: 0000000000000001 [52238.105608] RIP: 0010:drop_buffers+0x3d/0x150 [52238.304051] Call Trace: [52238.308958] try_to_free_buffers+0x15b/0x1b0 [52238.317503] shrink_page_list+0x1164/0x1780 [52238.325877] shrink_inactive_list+0x18f/0x3b0 [52238.334596] shrink_node_memcg+0x23e/0x7d0 [52238.342790] ? do_shrink_slab+0x4f/0x290 [52238.350648] shrink_node+0xce/0x4a0 [52238.357628] balance_pgdat+0x2c7/0x510 [52238.365135] kswapd+0x216/0x3e0 [52238.371425] ? wait_woken+0x80/0x80 [52238.378412] ? balance_pgdat+0x510/0x510 [52238.386265] kthread+0x111/0x130 [52238.392727] ? kthread_create_on_node+0x60/0x60 [52238.401782] ret_from_fork+0x1f/0x30
The page we were trying to drop had a page->private, but had no page->mapping and so called drop_buffers, assuming that we had a buffer_head on the page, and then panic'ed trying to deref 1, which is our page->private for data pages.
This is happening because we're truncating the free space cache while we're trying to load the free space cache. This isn't supposed to happen, and I'll fix that in a followup patch. However we still shouldn't allow those sort of mistakes to result in messing with pages that do not belong to us. So add the page->mapping check to verify that we still own this page after dropping and re-acquiring the page lock.
--- fs/btrfs/free-space-cache.c | 6 ++++++ 1 file changed, 6 insertions(+)
fs/btrfs/free-space-cache.c +6 -0
@@ -385,6 +385,12 @@ static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, struct inode *inodeif (uptodate && !PageUptodate (page)) {btrfs_readpage (NULL , page);lock_page (page); +if (page->mapping != inode->i_mapping) { +btrfs_err (BTRFS_I (inode)->root->fs_info, +"free space cache page truncated" ); +io_ctl_drop_pages (io_ctl); +return -EIO ; + }if (!PageUptodate (page)) {btrfs_err (BTRFS_I (inode)->root->fs_info,"error reading free space cache" );
Signature · 1 line
2.21.0
PATCH 2/2 btrfs: use btrfs_block_group_cache_done in update_block_group
When free'ing extents in a block group we check to see if the block group is not cached, and then cache it if we need to. However we'll just carry on as long as we're loading the cache. This is problematic because we are dirtying the block group here. If we are fast enough we could do a transaction commit and clear the free space cache while we're still loading the space cache in another thread. This truncates the free space inode, which will keep it from loading the space cache.
Fix this by using the btrfs_block_group_cache_done helper so that we try to load the space cache unconditionally here, which will result in the caller waiting for the fast caching to complete and keep us from truncating the free space inode.
--- fs/btrfs/block-group.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/block-group.c +1 -1
@@ -2661,7 +2661,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,* is because we need the unpinning stage to actually add the * space back to the block group, otherwise we will leak space. */ -if (!alloc && cache->cached ==BTRFS_CACHE_NO ) +if (!alloc && !btrfs_block_group_cache_done (cache))btrfs_cache_block_group (cache,1 ); byte_in_group = bytenr - cache->key.objectid;
Signature · 1 line
2.21.0
Re: [PATCH 2/2] btrfs: use btrfs_block_group_cache_done in update_block_group
Quoted 5 lines · Josef Bacik
When free'ing extents in a block group we check to see if the block group is not cached, and then cache it if we need to. However we'll just carry on as long as we're loading the cache. This is problematic because we are dirtying the block group here. If we are fast enough we could do a transaction commit and clear the free space cache while we're
This would imply a race condition between loading the space cache and running delayed refs - because there where __btrfs_free_extent->btrfs_update_blockgroup(alloc=0) is called from, no ?
Quoted 7 lines
still loading the space cache in another thread. This truncates the free space inode, which will keep it from loading the space cache.
Fix this by using the
btrfs_block_group_cache_donehelper so that we try to load the space cache unconditionally here, which will result in the caller waiting for the fast caching to complete and keep us from truncating the free space inode.
So the problem was that cache->cached == BTRFS_CACHE_NO misses other interim states e.g. CACHE_STARTED/CACHE_FAST. Which leads to the aforementioned race, correct?
Quoted 2 lines
Signed-off-by Josef Bacik <[email protected]>
Based on our offlist discussion:
Quoted 18 lines
--- fs/btrfs/block-group.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/btrfs/block-group.c +1
-1@@ -2661,7 +2661,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,* is because we need the unpinning stage to actually add the * space back to the block group, otherwise we will leak space. */ -if (!alloc && cache->cached ==BTRFS_CACHE_NO ) +if (!alloc && !btrfs_block_group_cache_done (cache))btrfs_cache_block_group (cache,1 ); byte_in_group = bytenr - cache->key.objectid;
Quoted 25 lines · Josef Bacik
While testing 5.2 we ran into the following panic
[52238.017028] BUG: kernel NULL pointer dereference, address: 0000000000000001 [52238.105608] RIP: 0010:drop_buffers+0x3d/0x150 [52238.304051] Call Trace: [52238.308958] try_to_free_buffers+0x15b/0x1b0 [52238.317503] shrink_page_list+0x1164/0x1780 [52238.325877] shrink_inactive_list+0x18f/0x3b0 [52238.334596] shrink_node_memcg+0x23e/0x7d0 [52238.342790] ? do_shrink_slab+0x4f/0x290 [52238.350648] shrink_node+0xce/0x4a0 [52238.357628] balance_pgdat+0x2c7/0x510 [52238.365135] kswapd+0x216/0x3e0 [52238.371425] ? wait_woken+0x80/0x80 [52238.378412] ? balance_pgdat+0x510/0x510 [52238.386265] kthread+0x111/0x130 [52238.392727] ? kthread_create_on_node+0x60/0x60 [52238.401782] ret_from_fork+0x1f/0x30The page we were trying to drop had a
page->private, but had nopage->mappingand so calleddrop_buffers, assuming that we had abuffer_headon the page, and then panic'ed trying to deref 1, which is ourpage->privatefor data pages.
Quoted 8 lines
This is happening because we're truncating the free space cache while we're trying to load the free space cache. This isn't supposed to happen, and I'll fix that in a followup patch. However we still shouldn't allow those sort of mistakes to result in messing with pages that do not belong to us. So add the
page->mappingcheck to verify that we still own this page after dropping and re-acquiring the page lock.Signed-off-by Josef Bacik <[email protected]>
Looks good, thanks.
Quoted 24 lines
--- fs/btrfs/free-space-cache.c | 6 ++++++ 1 file changed, 6 insertions(+)fs/btrfs/free-space-cache.c +6
-1@@ -385,6 +385,12 @@ static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, struct inode *inodeif (uptodate && !PageUptodate (page)) {btrfs_readpage (NULL , page);lock_page (page); +if (page->mapping != inode->i_mapping) { +btrfs_err (BTRFS_I (inode)->root->fs_info, +"free space cache page truncated" ); +io_ctl_drop_pages (io_ctl); +return -EIO ; + }if (!PageUptodate (page)) {btrfs_err (BTRFS_I (inode)->root->fs_info,"error reading free space cache" ); --2.21.0
Signature · 3 lines
Filipe David Manana, “Whether you think you can, or you think you can't — you're right.”
Quoted 24 lines · Josef Bacik
While testing 5.2 we ran into the following panic
[52238.017028] BUG: kernel NULL pointer dereference, address: 0000000000000001 [52238.105608] RIP: 0010:drop_buffers+0x3d/0x150 [52238.304051] Call Trace: [52238.308958] try_to_free_buffers+0x15b/0x1b0 [52238.317503] shrink_page_list+0x1164/0x1780 [52238.325877] shrink_inactive_list+0x18f/0x3b0 [52238.334596] shrink_node_memcg+0x23e/0x7d0 [52238.342790] ? do_shrink_slab+0x4f/0x290 [52238.350648] shrink_node+0xce/0x4a0 [52238.357628] balance_pgdat+0x2c7/0x510 [52238.365135] kswapd+0x216/0x3e0 [52238.371425] ? wait_woken+0x80/0x80 [52238.378412] ? balance_pgdat+0x510/0x510 [52238.386265] kthread+0x111/0x130 [52238.392727] ? kthread_create_on_node+0x60/0x60 [52238.401782] ret_from_fork+0x1f/0x30The page we were trying to drop had a
page->private, but had nopage->mappingand so calleddrop_buffers, assuming that we had abuffer_headon the page, and then panic'ed trying to deref 1, which is ourpage->privatefor data pages.
Quoted 6 lines
This is happening because we're truncating the free space cache while we're trying to load the free space cache. This isn't supposed to happen, and I'll fix that in a followup patch. However we still shouldn't allow those sort of mistakes to result in messing with pages that do not belong to us. So add the
page->mappingcheck to verify that we still own this page after dropping and re-acquiring the page lock.
Where is this page being unlocked? Is it:btrfs_readpage
extent_read_full_page
__extent_read_full_page
__do_readpage
if (!nr) unlock_page <-- nr can be 0 only if submit_extent_page
returns an error, correct? That's somewhat subtle and buried inside a long call chaing and really non obvious. So please mention when is the page lock being dropped.
Code-wise LGTM:
Quoted 24 lines
Signed-off-by Josef Bacik <[email protected]>--- fs/btrfs/free-space-cache.c | 6 ++++++ 1 file changed, 6 insertions(+)fs/btrfs/free-space-cache.c +6
-0@@ -385,6 +385,12 @@ static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, struct inode *inodeif (uptodate && !PageUptodate (page)) {btrfs_readpage (NULL , page);lock_page (page); +if (page->mapping != inode->i_mapping) { +btrfs_err (BTRFS_I (inode)->root->fs_info, +"free space cache page truncated" ); +io_ctl_drop_pages (io_ctl); +return -EIO ; + }if (!PageUptodate (page)) {btrfs_err (BTRFS_I (inode)->root->fs_info,"error reading free space cache" );
Quoted 24 lines · Josef Bacik
While testing 5.2 we ran into the following panic
[52238.017028] BUG: kernel NULL pointer dereference, address: 0000000000000001 [52238.105608] RIP: 0010:drop_buffers+0x3d/0x150 [52238.304051] Call Trace: [52238.308958] try_to_free_buffers+0x15b/0x1b0 [52238.317503] shrink_page_list+0x1164/0x1780 [52238.325877] shrink_inactive_list+0x18f/0x3b0 [52238.334596] shrink_node_memcg+0x23e/0x7d0 [52238.342790] ? do_shrink_slab+0x4f/0x290 [52238.350648] shrink_node+0xce/0x4a0 [52238.357628] balance_pgdat+0x2c7/0x510 [52238.365135] kswapd+0x216/0x3e0 [52238.371425] ? wait_woken+0x80/0x80 [52238.378412] ? balance_pgdat+0x510/0x510 [52238.386265] kthread+0x111/0x130 [52238.392727] ? kthread_create_on_node+0x60/0x60 [52238.401782] ret_from_fork+0x1f/0x30The page we were trying to drop had a
page->private, but had nopage->mappingand so calleddrop_buffers, assuming that we had abuffer_headon the page, and then panic'ed trying to deref 1, which is ourpage->privatefor data pages.
Quoted 8 lines
This is happening because we're truncating the free space cache while we're trying to load the free space cache. This isn't supposed to happen, and I'll fix that in a followup patch. However we still shouldn't allow those sort of mistakes to result in messing with pages that do not belong to us. So add the
page->mappingcheck to verify that we still own this page after dropping and re-acquiring the page lock.Signed-off-by Josef Bacik <[email protected]>
Patches 1 and 2 moved to misc-next. Thanks.