Skip to contentSkip to conversation

fs: don't allocate blocks beyond EOF from __mpage_writepage

PATCH Applied Jan Kara 5 participants 8 messages Jan 3, 2023 – Jan 26, 2023 VFS

lore
Share & tools
lore

Jan Kara

author reviewer
#0 lore raw

PATCH 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
index 0f8ae954a579..9f040c1d5912 100644
@@ -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
maintainer
#1 lore raw
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 and i_size is not extended).

---
 fs/mpage.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/mpage.c +0 -0
index 0f8ae954a579..9f040c1d5912 100644

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?

author reviewer
#2 lore raw
Quoted 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 and i_size is not extended).

---
 fs/mpage.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/mpage.c +0 -0
index 0f8ae954a579..9f040c1d5912 100644

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

Quoted 4 lines
	if (block_in_file > last_block)
		goto page_is_mapped;

after last_block has 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
Reviewed-by on v1 1/1
#3 lore raw
In reply to Jan Kara
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 and i_size is not extended).

Looks good:

author reviewer
#4 lore raw
Quoted 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 and i_size is not extended).

Looks good:

Matthew, Andrew, can one of you please pick up this fix? Thanks!

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#5 lore raw
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 and i_size is not extended).

Quoted 5 lines
Quoted 3 lines

Looks good:

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.

#6 lore raw
In reply to Jan Kara
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 and i_size is not extended).

Quoted 6 lines
Quoted 3 lines

Looks good:

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.

author reviewer
#7 lore raw
Quoted 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 and i_size is not extended).

Looks good:

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

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