fs: don't allocate blocks beyond EOF from __mpage_writepage
Jan Kara
reviewerPATCH 1/1 fs: don't allocate blocks beyond EOF from __mpage_writepage
When __mpage_writepage() is called for a page beyond EOF, it will go and allocate all blocks underlying the page. This is not only unnecessary but this way blocks can get leaked (e.g. if a page beyond EOF is marked dirty but in the end write fails and i_size is not extended).
--- fs/mpage.c | 6 ++++++ 1 file changed, 6 insertions(+)
fs/mpage.c +6 -0
@@ -524,6 +524,12 @@ static int __mpage_writepage(struct page *page, struct writeback_control *wbc,*/ BUG_ON (!PageUptodate (page)); block_in_file = (sector_t )page->index << (PAGE_SHIFT - blkbits); +/* +* Whole page beyond EOF? Skip allocating blocks to avoid leaking +* space. +*/ +if (block_in_file >= (i_size + (1 << blkbits) -1 ) >> blkbits) + goto page_is_mapped; last_block = (i_size -1 ) >> blkbits; map_bh.b_page = page;for (page_block =0 ; page_block < blocks_per_page; ) {
Signature · 1 line
2.35.3
Al Viro
maintainerQuoted 15 lines · Jan Kara
When
__mpage_writepage()is called for a page beyond EOF, it will go and allocate all blocks underlying the page. This is not only unnecessary but this way blocks can get leaked (e.g. if a page beyond EOF is marked dirty but in the end write fails andi_sizeis not extended).Signed-off-by Jan Kara <[email protected]>--- fs/mpage.c | 6 ++++++ 1 file changed, 6 insertions(+)fs/mpage.c +0
-0
fs/mpage.c static int __mpage_writepage(struct page *page, struct writeback_control *wbc, · line 533
Quoted 10 lines
fs/mpage.c +6
-0@@ -524,6 +524,12 @@ static int __mpage_writepage(struct page *page, struct writeback_control *wbc,*/ BUG_ON (!PageUptodate (page)); block_in_file = (sector_t )page->index << (PAGE_SHIFT - blkbits); +/* +* Whole page beyond EOF? Skip allocating blocks to avoid leaking +* space. +*/ +if (block_in_file >= (i_size + (1 << blkbits) -1 ) >> blkbits) + goto page_is_mapped; last_block = (i_size -1 ) >> blkbits;
Why not simply
if (block_in_file > last_block) goto page_is_mapped;
after last_block has been calculated?
Jan Kara
reviewerQuoted 29 lines · Al Viro
Quoted 15 lines · Jan Kara
When
__mpage_writepage()is called for a page beyond EOF, it will go and allocate all blocks underlying the page. This is not only unnecessary but this way blocks can get leaked (e.g. if a page beyond EOF is marked dirty but in the end write fails andi_sizeis not extended).Signed-off-by Jan Kara <[email protected]>--- fs/mpage.c | 6 ++++++ 1 file changed, 6 insertions(+)fs/mpage.c +0
-0fs/mpage.c static int __mpage_writepage(struct page *page, struct writeback_control *wbc, · line 533
Quoted 10 lines
fs/mpage.c +6
-0@@ -524,6 +524,12 @@ static int __mpage_writepage(struct page *page, struct writeback_control *wbc,*/ BUG_ON (!PageUptodate (page)); block_in_file = (sector_t )page->index << (PAGE_SHIFT - blkbits); +/* +* Whole page beyond EOF? Skip allocating blocks to avoid leaking +* space. +*/ +if (block_in_file >= (i_size + (1 << blkbits) -1 ) >> blkbits) + goto page_is_mapped; last_block = (i_size -1 ) >> blkbits;Why not simply
Quoted 4 lines
if (block_in_file > last_block) goto page_is_mapped;after
last_blockhas been calculated?
Because if i_size == 0, last_block is (~0 >> blkbits) (which was actually the case the test hit).
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 4 lines · Jan Kara
When
__mpage_writepage()is called for a page beyond EOF, it will go and allocate all blocks underlying the page. This is not only unnecessary but this way blocks can get leaked (e.g. if a page beyond EOF is marked dirty but in the end write fails andi_sizeis not extended).
Looks good:
Jan Kara
reviewerQuoted 9 lines · Christoph Hellwig
Quoted 4 lines · Jan Kara
When
__mpage_writepage()is called for a page beyond EOF, it will go and allocate all blocks underlying the page. This is not only unnecessary but this way blocks can get leaked (e.g. if a page beyond EOF is marked dirty but in the end write fails andi_sizeis not extended).Looks good:
Reviewed-by Christoph Hellwig <[email protected]>
Matthew, Andrew, can one of you please pick up this fix? Thanks!
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 7 lines · Jan Kara
Quoted 6 lines · Christoph Hellwig
Quoted 4 lines · Jan Kara
When
__mpage_writepage()is called for a page beyond EOF, it will go and allocate all blocks underlying the page. This is not only unnecessary but this way blocks can get leaked (e.g. if a page beyond EOF is marked dirty but in the end write fails andi_sizeis not extended).
Quoted 5 lines
Quoted 3 lines
Looks good:
Reviewed-by Christoph Hellwig <[email protected]>Matthew, Andrew, can one of you please pick up this fix? Thanks!
I don't have a pull request pending for next merge window, so probably best if Andrew picks it up.
Quoted 7 lines · On Wed, 25 Jan 2023 15:23:51 +0100 Jan Kara <[email protected]> wrote:
Quoted 6 lines · Christoph Hellwig
Quoted 4 lines · Jan Kara
When
__mpage_writepage()is called for a page beyond EOF, it will go and allocate all blocks underlying the page. This is not only unnecessary but this way blocks can get leaked (e.g. if a page beyond EOF is marked dirty but in the end write fails andi_sizeis not extended).
Quoted 6 lines
Quoted 3 lines
Looks good:
Reviewed-by Christoph Hellwig <[email protected]>Matthew, Andrew, can one of you please pick up this fix? Thanks!
This was added to mm-stable (and hence linux-next) on Jan 18, as 4b89a37d54a0b.
Jan Kara
reviewerQuoted 11 lines · Andrew Morton
Quoted 9 lines · On Wed, 25 Jan 2023 15:23:51 +0100 Jan Kara <[email protected]> wrote:
Quoted 8 lines · Christoph Hellwig
Quoted 4 lines · Jan Kara
When
__mpage_writepage()is called for a page beyond EOF, it will go and allocate all blocks underlying the page. This is not only unnecessary but this way blocks can get leaked (e.g. if a page beyond EOF is marked dirty but in the end write fails andi_sizeis not extended).Looks good:
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Reviewed-by Christoph Hellwig <[email protected]>Matthew, Andrew, can one of you please pick up this fix? Thanks!
This was added to mm-stable (and hence linux-next) on Jan 18, as
4b89a37d54a0b.
Bah, thanks for reminder. I didn't see any reply in the thread and somehow my inbox searching from your commit-bot email failed. Sorry for the noise.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR