Skip to contentSkip to conversation

ext4: skip eof zeroing when the zero range covers the eof block

PATCH Superseded Baokun Li 4 participants 8 messages Oct 22 – Nov 19

Subsystems (2)
lore
Share & tools
lore

Baokun Li

author reviewer
#0 lore raw

PATCH 1/1 ext4: skip eof zeroing when the zero range covers the eof block

ext4_zero_range() drops the page cache of the range before allocating, to avoid exposing stale cached data. However, the dropped page can be read back by the eof zeroing:

fallocate(fd, FALLOC_FL_ZERO_RANGE, 0, 4096)

    ext4_zero_range()
      ext4_truncate_page_cache_block_range(0, 4096)
        truncate_pagecache_range(0, 4095)
          truncate_inode_pages_range()
            delete_from_page_cache_batch()  # drops the page
      ext4_alloc_file_blocks(..., 4096)
        ext4_block_zero_eof(512, LLONG_MAX) # not block aligned
          ext4_load_tail_bh()
             filemap_lock_folio(0)    # reads dropped page back
             # zeroes [512,1024), marks the buffer dirty
        ext4_map_blocks(...)          # zeroes the blocks on disk
      ext4_update_inode_size(inode, 4096)

The bytes below the old EOF keep the stale disk content. The page is left dirty, so read() returns old data while O_DIRECT reads zeroes, and a write into the same block is not converted and is lost until the page is dropped:

page [0, 4096), 1k block size, file ends at 512

   0            512          1024                      4096
   |--- data ----|-- tail ---|---- zero range ----------|
   | old data    |           |                          |

For example:

  $ xfs_io -f -c "pwrite -S 1 0 512" f
  $ sync
  $ fallocate -z -o 0 -l 4096 f
  $ xfs_io -c "pread -v 0 16" f  # old data, not zeroes

Since the whole eof block is converted to unwritten, zeroing it here is redundant. Therefore, skip the zeroing when this allocation covers the eof block entirely.

Keep the zeroing otherwise: the block is still written at that point, so touching the page cache is safe.

Fixes c7fc0366c656 ("ext4: partial zero eof block on unaligned inode size extension")
---
 fs/ext4/extents.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
fs/ext4/extents.c +9 -2
index ad0d80e33f60..2ce33fd3b912 100644
@@ -4639,8 +4639,15 @@ static int ext4_alloc_file_blocks(struct file *file, loff_t offset, loff_t len,
 	credits = ext4_chunk_trans_blocks(inode, len_lblk);
 	depth = ext_depth(inode);
 
-	/* Zero to the end of the block containing i_size */
-	if (new_size > old_size) {
+	/*
+	 * Zero to the end of the block containing i_size.
+	 * Loading the eof block back before it is converted to unwritten
+	 * may leave the page cache inconsistent with the disk, and zeroing
+	 * it is redundant, so skip it.
+	 */
+	if (new_size > old_size &&
+	    (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) ||
+	     !in_range(old_size >> blkbits, map.m_lblk, map.m_len))) {
 		ret = ext4_block_zero_eof(inode, old_size, LLONG_MAX);
 		if (ret)
 			return ret;
Signature · 1 line
2.43.7
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] ext4: stale data in ext4_alloc_file_blocks() on FALLOC_FL_WRITE_ZEROES --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] ext4: stale data in ext4_alloc_file_blocks() on FALLOC_FL_WRITE_ZEROES --

commit 5e26fed6c5f3f56bd5013064a243a3a558e3a73d
Author: Baokun Li <[email protected]>

ext4: skip eof zeroing when the zero range covers the eof block

This commit skips calling ext4_block_zero_eof() in ext4_alloc_file_blocks() when EXT4_GET_BLOCKS_CONVERT_UNWRITTEN is set and the allocation range covers the EOF block, preventing the dropped EOF page from being read back into the page cache with stale data during FALLOC_FL_ZERO_RANGE.

Quoted 12 lines
fs/ext4/extents.c +2 -2
index 7193429a05e1f..8f7ded6e08299 100644
@@ -4639,8 +4639,15 @@ static int ext4_alloc_file_blocks(struct file *file, loff_t offset, loff_t len,
 	credits = ext4_chunk_trans_blocks(inode, len_lblk);
 	depth = ext_depth(inode);
 
-	/* Zero to the end of the block containing i_size */
-	if (new_size > old_size) {
+	/*
+	 * Zero to the end of the block containing i_size.

fs/ext4/extents.c static int ext4_alloc_file_blocks(struct file *file, loff_t offset, loff_t len, · line 4653

Quoted 10 lines
fs/ext4/extents.c +7 -0
+	 * Loading the eof block back before it is converted to unwritten
+	 * may leave the page cache inconsistent with the disk, and zeroing
+	 * it is redundant, so skip it.
+	 */
+	if (new_size > old_size &&
+	    (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) ||
+	     !in_range(old_size >> blkbits, map.m_lblk, map.m_len))) {
 		ret = ext4_block_zero_eof(inode, old_size, LLONG_MAX);
 		if (ret)
 			return ret;

[Severity: Critical]
Does this condition also need to check !alloc_zero to cover the FALLOC_FL_WRITE_ZEROES path?

In ext4_zero_range(), after ext4_truncate_page_cache_block_range() drops the page cache, FALLOC_FL_WRITE_ZEROES calls ext4_alloc_file_blocks() with EXT4_GET_BLOCKS_CREATE_ZERO rather than EXT4_GET_BLOCKS_CONVERT_UNWRITTEN:

	if (align_end > align_start) {
		if (mode & FALLOC_FL_WRITE_ZEROES)
			flags = EXT4_GET_BLOCKS_CREATE_ZERO | EXT4_EX_NOCACHE;
		else
			flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN |
				  EXT4_EX_NOCACHE);
		ret = ext4_alloc_file_blocks(file, align_start,
					     align_end - align_start, new_size,
					     flags);

Earlier in ext4_alloc_file_blocks(), EXT4_GET_BLOCKS_ZERO is cleared from flags and converted into alloc_zero = true:

	if (flags & EXT4_GET_BLOCKS_ZERO) {
		flags &= ~EXT4_GET_BLOCKS_ZERO;
		flags |= EXT4_GET_BLOCKS_UNWRIT_EXT;
		alloc_zero = true;
	}

When FALLOC_FL_WRITE_ZEROES extends i_size across an unaligned written EOF block covered by [align_start, align_end),
!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) is true. As a result, ext4_block_zero_eof() is still called and reads the dropped EOF block back from disk into the page cache with stale data below old_size, marks the buffer or folio dirty, and then the loop zeroes the block on disk via ext4_issue_zeroout() and leaves the extent mapped as written.

Wouldn't this leave the dirty page cache folio with stale data that is returned on buffered reads and written back over the zeroed disk block on writeback or fsync?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author reviewer
#2 lore raw
Quoted 71 lines · On 2026/10/22 15:16, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] ext4: stale data in ext4_alloc_file_blocks() on FALLOC_FL_WRITE_ZEROES --

commit 5e26fed6c5f3f56bd5013064a243a3a558e3a73d
Author: Baokun Li <[email protected]>

ext4: skip eof zeroing when the zero range covers the eof block

This commit skips calling ext4_block_zero_eof() in ext4_alloc_file_blocks() when EXT4_GET_BLOCKS_CONVERT_UNWRITTEN is set and the allocation range covers the EOF block, preventing the dropped EOF page from being read back into the page cache with stale data during FALLOC_FL_ZERO_RANGE.

Quoted 12 lines
fs/ext4/extents.c +2 -2
index 7193429a05e1f..8f7ded6e08299 100644
@@ -4639,8 +4639,15 @@ static int ext4_alloc_file_blocks(struct file *file, loff_t offset, loff_t len,
 	credits = ext4_chunk_trans_blocks(inode, len_lblk);
 	depth = ext_depth(inode);
 
-	/* Zero to the end of the block containing i_size */
-	if (new_size > old_size) {
+	/*
+	 * Zero to the end of the block containing i_size.

fs/ext4/extents.c static int ext4_alloc_file_blocks(struct file *file, loff_t offset, loff_t len, · line 4653

Quoted 10 lines
fs/ext4/extents.c +7 -0
+	 * Loading the eof block back before it is converted to unwritten
+	 * may leave the page cache inconsistent with the disk, and zeroing
+	 * it is redundant, so skip it.
+	 */
+	if (new_size > old_size &&
+	    (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) ||
+	     !in_range(old_size >> blkbits, map.m_lblk, map.m_len))) {
 		ret = ext4_block_zero_eof(inode, old_size, LLONG_MAX);
 		if (ret)
 			return ret;

[Severity: Critical]
Does this condition also need to check !alloc_zero to cover the FALLOC_FL_WRITE_ZEROES path?

In ext4_zero_range(), after ext4_truncate_page_cache_block_range() drops the page cache, FALLOC_FL_WRITE_ZEROES calls ext4_alloc_file_blocks() with EXT4_GET_BLOCKS_CREATE_ZERO rather than EXT4_GET_BLOCKS_CONVERT_UNWRITTEN:

	if (align_end > align_start) {
		if (mode & FALLOC_FL_WRITE_ZEROES)
			flags = EXT4_GET_BLOCKS_CREATE_ZERO | EXT4_EX_NOCACHE;
		else
			flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN |
				  EXT4_EX_NOCACHE);
		ret = ext4_alloc_file_blocks(file, align_start,
					     align_end - align_start, new_size,
					     flags);

Earlier in ext4_alloc_file_blocks(), EXT4_GET_BLOCKS_ZERO is cleared from flags and converted into alloc_zero = true:

	if (flags & EXT4_GET_BLOCKS_ZERO) {
		flags &= ~EXT4_GET_BLOCKS_ZERO;
		flags |= EXT4_GET_BLOCKS_UNWRIT_EXT;
		alloc_zero = true;
	}

When FALLOC_FL_WRITE_ZEROES extends i_size across an unaligned written EOF block covered by [align_start, align_end),
!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) is true. As a result, ext4_block_zero_eof() is still called and reads the dropped EOF block back from disk into the page cache with stale data below old_size, marks the buffer or folio dirty, and then the loop zeroes the block on disk via ext4_issue_zeroout() and leaves the extent mapped as written.

Quoted 4 lines

Wouldn't this leave the dirty page cache folio with stale data that is returned on buffered reads and written back over the zeroed disk block on writeback or fsync?

This is indeed another similar issue that can be addressed in the current patch. I will send version 2 soon.

Thanks,
Baokun

author reviewer
#3 lore raw

PATCH v2 1/1 ext4: skip eof zeroing when the zero range covers the eof block

In reply to [email protected]

ext4_zero_range() drops the page cache of the range before allocating, to avoid exposing stale cached data. However, the dropped page can be read back by the eof zeroing:

fallocate(fd, FALLOC_FL_ZERO_RANGE, 0, 4096)

    ext4_zero_range()
      ext4_truncate_page_cache_block_range(0, 4096)
        truncate_pagecache_range(0, 4095)
          truncate_inode_pages_range()
            delete_from_page_cache_batch()  # drops the page
      ext4_alloc_file_blocks(..., 4096)
        ext4_block_zero_eof(512, LLONG_MAX) # not block aligned
          ext4_load_tail_bh()
             __filemap_get_folio(FGP_WRITEBEGIN | FGP_ACCESSED)
             # FGP_CREAT reads the dropped page back
             # zeroes [512,1024), marks the buffer dirty
        ext4_map_blocks(...)  # converts the extent to unwritten
        ext4_update_inode_size(4096)

The bytes below the old EOF keep the stale disk content. The page is left dirty, so read() returns old data while O_DIRECT reads zeroes, and a write into the same block is not converted and is lost until the page is dropped:

page [0, 4096), 1k block size, file ends at 512

   0            512          1024                      4096
   |--- data ----|-- tail ---|---- zero range ----------|
   | old data    |           |                          |

For example:

  $ xfs_io -f -c "pwrite -S 1 0 512" f
  $ sync
  $ fallocate -z -o 0 -l 4096 f
  $ xfs_io -c "pread -v 0 16" f  # old data, not zeroes

FALLOC_FL_WRITE_ZEROES hits the same path. ext4_issue_zeroout() zeroes the block on disk without updating the page cache, and the block stays written. Therefore, writeback of the dirty page overwrites the zeroes with old data.

Since the whole eof block is converted to unwritten or zeroed out, zeroing it here is redundant. Therefore, skip the zeroing when this allocation covers the eof block entirely.

Fixes c4602a1d09ec ("ext4: move zero partial block range functions out of active handle")
Fixes f4265b8d32c4 ("ext4: add FALLOC_FL_WRITE_ZEROES support")

---
Changes since v1:
* Also skip the eof zeroing for FALLOC_FL_WRITE_ZEROES, which reads the dropped page back in the same way (Reported by Sashiko).
* Corrected the Fixes tags: the ZERO_RANGE regression comes from c4602a1d09ec, and f4265b8d32c4 introduced the WRITE_ZEROES one.
* Fixed the call chains in the commit message.

v1: https://patch.msgid.link/[email protected]

This hunk does not apply to v6.17 - v7.0, where the eof zeroing is done inside the allocation loop; those trees need a manual backport.

 fs/ext4/extents.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
fs/ext4/extents.c +9 -2
index ad0d80e33f60..b05015b8b62c 100644
@@ -4639,8 +4639,15 @@ static int ext4_alloc_file_blocks(struct file *file, loff_t offset, loff_t len,
 	credits = ext4_chunk_trans_blocks(inode, len_lblk);
 	depth = ext_depth(inode);
 
-	/* Zero to the end of the block containing i_size */
-	if (new_size > old_size) {
+	/*
+	 * Zero to the end of the block containing i_size.
+	 * Loading the eof block back before it is converted to unwritten
+	 * or zeroed out may leave the page cache inconsistent with the
+	 * disk, and zeroing it is redundant, so skip it.
+	 */
+	if (new_size > old_size &&
+	    !(in_range(old_size >> blkbits, map.m_lblk, map.m_len) &&
+	      (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN || alloc_zero))) {
 		ret = ext4_block_zero_eof(inode, old_size, LLONG_MAX);
 		if (ret)
 			return ret;
Signature · 1 line
2.43.7
#4 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 15 lines

ext4_zero_range() drops the page cache of the range before allocating, to avoid exposing stale cached data. However, the dropped page can be read back by the eof zeroing:

fallocate(fd, FALLOC_FL_ZERO_RANGE, 0, 4096)

    ext4_zero_range()
      ext4_truncate_page_cache_block_range(0, 4096)
        truncate_pagecache_range(0, 4095)
          truncate_inode_pages_range()
            delete_from_page_cache_batch()  # drops the page
      ext4_alloc_file_blocks(..., 4096)
        ext4_block_zero_eof(512, LLONG_MAX) # not block aligned
          ext4_load_tail_bh()
             __filemap_get_folio(FGP_WRITEBEGIN | FGP_ACCESSED)
             # FGP_CREAT reads the dropped page back
Quoted 6 lines

[ ... ]

Fixes c4602a1d09ec ("ext4: move zero partial block range functions out of active handle")
Fixes f4265b8d32c4 ("ext4: add FALLOC_FL_WRITE_ZEROES support")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v2 1/1
#5 lore raw
In reply to Baokun Li
Quoted 41 lines · Baokun Li

ext4_zero_range() drops the page cache of the range before allocating, to avoid exposing stale cached data. However, the dropped page can be read back by the eof zeroing:

fallocate(fd, FALLOC_FL_ZERO_RANGE, 0, 4096)

    ext4_zero_range()
      ext4_truncate_page_cache_block_range(0, 4096)
        truncate_pagecache_range(0, 4095)
          truncate_inode_pages_range()
            delete_from_page_cache_batch()  # drops the page
      ext4_alloc_file_blocks(..., 4096)
        ext4_block_zero_eof(512, LLONG_MAX) # not block aligned
          ext4_load_tail_bh()
             __filemap_get_folio(FGP_WRITEBEGIN | FGP_ACCESSED)
             # FGP_CREAT reads the dropped page back
             # zeroes [512,1024), marks the buffer dirty
        ext4_map_blocks(...)  # converts the extent to unwritten
        ext4_update_inode_size(4096)

The bytes below the old EOF keep the stale disk content. The page is left dirty, so read() returns old data while O_DIRECT reads zeroes, and a write into the same block is not converted and is lost until the page is dropped:

page [0, 4096), 1k block size, file ends at 512

   0            512          1024                      4096
   |--- data ----|-- tail ---|---- zero range ----------|
   | old data    |           |                          |

For example:

  $ xfs_io -f -c "pwrite -S 1 0 512" f
  $ sync
  $ fallocate -z -o 0 -l 4096 f
  $ xfs_io -c "pread -v 0 16" f  # old data, not zeroes

FALLOC_FL_WRITE_ZEROES hits the same path. ext4_issue_zeroout() zeroes the block on disk without updating the page cache, and the block stays written. Therefore, writeback of the dirty page overwrites the zeroes with old data.

Quoted 8 lines

Since the whole eof block is converted to unwritten or zeroed out, zeroing it here is redundant. Therefore, skip the zeroing when this allocation covers the eof block entirely.

Fixes c4602a1d09ec ("ext4: move zero partial block range functions out of active handle")
Fixes f4265b8d32c4 ("ext4: add FALLOC_FL_WRITE_ZEROES support")

Hi Baokun,

Thank you for the fix, this is a nice catch. This makes sense to me.

One small point, the condition you added looks a bit confusing. I'm wondering whether, in the future, it would be more straightforward to extract ext4_block_zero_eof() from ext4_alloc_file_blocks() into ext4_zero_range() and ext4_do_fallocate()?

Thanks,
Yi.

Quoted 38 lines

---
Changes since v1:
* Also skip the eof zeroing for FALLOC_FL_WRITE_ZEROES, which reads the dropped page back in the same way (Reported by Sashiko).
* Corrected the Fixes tags: the ZERO_RANGE regression comes from c4602a1d09ec, and f4265b8d32c4 introduced the WRITE_ZEROES one.
* Fixed the call chains in the commit message.

v1: https://patch.msgid.link/[email protected]

This hunk does not apply to v6.17 - v7.0, where the eof zeroing is done inside the allocation loop; those trees need a manual backport.

 fs/ext4/extents.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
fs/ext4/extents.c +9 -2
index ad0d80e33f60..b05015b8b62c 100644
@@ -4639,8 +4639,15 @@ static int ext4_alloc_file_blocks(struct file *file, loff_t offset, loff_t len,
 	credits = ext4_chunk_trans_blocks(inode, len_lblk);
 	depth = ext_depth(inode);
 
-	/* Zero to the end of the block containing i_size */
-	if (new_size > old_size) {
+	/*
+	 * Zero to the end of the block containing i_size.
+	 * Loading the eof block back before it is converted to unwritten
+	 * or zeroed out may leave the page cache inconsistent with the
+	 * disk, and zeroing it is redundant, so skip it.
+	 */
+	if (new_size > old_size &&
+	    !(in_range(old_size >> blkbits, map.m_lblk, map.m_len) &&
+	      (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN || alloc_zero))) {
 		ret = ext4_block_zero_eof(inode, old_size, LLONG_MAX);
 		if (ret)
 			return ret;
author reviewer
#6 lore raw
Quoted 46 lines · Zhang Yi
Quoted 45 lines · Baokun Li

ext4_zero_range() drops the page cache of the range before allocating, to avoid exposing stale cached data. However, the dropped page can be read back by the eof zeroing:

fallocate(fd, FALLOC_FL_ZERO_RANGE, 0, 4096)

    ext4_zero_range()
      ext4_truncate_page_cache_block_range(0, 4096)
        truncate_pagecache_range(0, 4095)
          truncate_inode_pages_range()
            delete_from_page_cache_batch()  # drops the page
      ext4_alloc_file_blocks(..., 4096)
        ext4_block_zero_eof(512, LLONG_MAX) # not block aligned
          ext4_load_tail_bh()
             __filemap_get_folio(FGP_WRITEBEGIN | FGP_ACCESSED)
             # FGP_CREAT reads the dropped page back
             # zeroes [512,1024), marks the buffer dirty
        ext4_map_blocks(...)  # converts the extent to unwritten
        ext4_update_inode_size(4096)

The bytes below the old EOF keep the stale disk content. The page is left dirty, so read() returns old data while O_DIRECT reads zeroes, and a write into the same block is not converted and is lost until the page is dropped:

page [0, 4096), 1k block size, file ends at 512

   0            512          1024                      4096
   |--- data ----|-- tail ---|---- zero range ----------|
   | old data    |           |                          |

For example:

  $ xfs_io -f -c "pwrite -S 1 0 512" f
  $ sync
  $ fallocate -z -o 0 -l 4096 f
  $ xfs_io -c "pread -v 0 16" f  # old data, not zeroes

FALLOC_FL_WRITE_ZEROES hits the same path. ext4_issue_zeroout() zeroes the block on disk without updating the page cache, and the block stays written. Therefore, writeback of the dirty page overwrites the zeroes with old data.

Since the whole eof block is converted to unwritten or zeroed out, zeroing it here is redundant. Therefore, skip the zeroing when this allocation covers the eof block entirely.

Quoted 6 lines
Quoted 4 lines
Fixes c4602a1d09ec ("ext4: move zero partial block range functions out of active handle")
Fixes f4265b8d32c4 ("ext4: add FALLOC_FL_WRITE_ZEROES support")

Hi Baokun,

Hi Yi,

Quoted 4 lines

Thank you for the fix, this is a nice catch. This makes sense to me.

Thank you for your review!

Quoted 4 lines

One small point, the condition you added looks a bit confusing. I'm wondering whether, in the future, it would be more straightforward to extract ext4_block_zero_eof() from ext4_alloc_file_blocks() into ext4_zero_range() and ext4_do_fallocate()?

That sounds great. By writing zeros before truncating the page, we won’t have to worry about the page cache being read back, which means we won’t need to check the position of the EOF block anymore.

I’ll send this out as a separate refactoring patch.

Thanks for the suggestion.

Cheers,
Baokun

maintainer Applied
#7 lore raw
In reply to Baokun Li
Quoted 19 lines · Baokun Li

ext4_zero_range() drops the page cache of the range before allocating, to avoid exposing stale cached data. However, the dropped page can be read back by the eof zeroing:

fallocate(fd, FALLOC_FL_ZERO_RANGE, 0, 4096)

    ext4_zero_range()
      ext4_truncate_page_cache_block_range(0, 4096)
        truncate_pagecache_range(0, 4095)
          truncate_inode_pages_range()
            delete_from_page_cache_batch()  # drops the page
      ext4_alloc_file_blocks(..., 4096)
        ext4_block_zero_eof(512, LLONG_MAX) # not block aligned
          ext4_load_tail_bh()
             __filemap_get_folio(FGP_WRITEBEGIN | FGP_ACCESSED)
             # FGP_CREAT reads the dropped page back
             # zeroes [512,1024), marks the buffer dirty
        ext4_map_blocks(...)  # converts the extent to unwritten
        ext4_update_inode_size(4096)
Quoted 1 line

[...]

Applied, thanks!

[1/1] ext4: skip eof zeroing when the zero range covers the eof block

      commit: 3c498dda252e1729e831c48300f17d1e6a7abb71

Best regards,

Signature · 1 line
Theodore Ts'o <[email protected]>