Linux EXT4 FS development
 help / color / mirror / Atom feed
* [PATCH 0/2] ext4: fix data loss when reading during inline data conversion
@ 2026-12-03 10:06 Florian Schmaus
  2026-12-03 10:06 ` [PATCH 1/2] ext4: don't zero uptodate buffers in ext4_readpage_inline() Florian Schmaus
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Florian Schmaus @ 2026-12-03 10:06 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
	Ojaswin Mujoo, Ritesh Harjani (IBM), Zhang Yi, Tao Ma
  Cc: linux-ext4, Florian Schmaus, Ulrich Müller, stable

Ulrich reported that on ext4 with 1 KiB blocks and inline_data, files
written with many small writes can end up with NUL bytes at the end [1].
Gentoo's portage ran into it once Python 3.14 started to read files with
a larger buffer.

After a delalloc conversion of inline data, ext4_readpage_inline() keeps
handling reads until writeback, and it zero-fills folios that already
hold written, uptodate buffers. Patch 1 fixes this and is tagged for
stable. Patch 2 adds a KUnit case for it. It is not tagged for stable,
as the inline data KUnit tests are new in 7.4.

Tested on 7.4-rc5 with files written as in the reproducer from patch 1
and read back through read(2), mmap, splice and sendfile. Without the
fix, all of these return NUL bytes, and the zeroes end up on disk. With
the fix, none do. All 562 ext4 KUnit tests pass, and the new case fails
without the fix.

[1] https://lore.kernel.org/ucxrjwzcv@gentoo.org

Signed-off-by: Florian Schmaus <flo@geekplace.eu>
---
Florian Schmaus (2):
      ext4: don't zero uptodate buffers in ext4_readpage_inline()
      ext4: test that ext4_readpage_inline() keeps uptodate buffers

 fs/ext4/inline-test.c | 36 ++++++++++++++++++++++++++++++++++++
 fs/ext4/inline.c      | 40 +++++++++++++++++++++++++++++++++++++---
 2 files changed, 73 insertions(+), 3 deletions(-)
---
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
change-id: 20261203-ext4-inline-readpage-400817c74e13

Best regards,
--  
Florian Schmaus <flo@geekplace.eu>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/2] ext4: don't zero uptodate buffers in ext4_readpage_inline()
  2026-12-03 10:06 [PATCH 0/2] ext4: fix data loss when reading during inline data conversion Florian Schmaus
@ 2026-12-03 10:06 ` Florian Schmaus
  2026-12-03 10:06 ` [PATCH 2/2] ext4: test that ext4_readpage_inline() keeps uptodate buffers Florian Schmaus
  2026-12-03 10:30 ` [PATCH 0/2] ext4: fix data loss when reading during inline data conversion sashiko-bot
  2 siblings, 0 replies; 5+ messages in thread
From: Florian Schmaus @ 2026-12-03 10:06 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
	Ojaswin Mujoo, Ritesh Harjani (IBM), Zhang Yi, Tao Ma
  Cc: linux-ext4, Florian Schmaus, Ulrich Müller, stable

When a file with inline data outgrows the inode, ext4 with delayed
allocation moves the data out of the inode in two steps, and the second
step only happens at writeback. If the block size is smaller than the
page size, such as 1 KiB blocks on x86, reading the file between the two
steps can replace freshly written data with zeroes, which writeback then
puts on disk.

The first step, ext4_da_convert_inline_data_to_extent(), copies the
inline data into the first folio and clears EXT4_STATE_MAY_INLINE_DATA.
The inline data itself and EXT4_INODE_INLINE_DATA stay in place until
the second step, when writeback calls ext4_destroy_inline_data(). Until
then, ext4_read_folio() hands every folio to ext4_readpage_inline(),
which assumes that only the first folio can hold data and zero-fills any
other folio that is not uptodate.

If the block size is smaller than the folio size, buffered writes that
fill a folio only partially leave the written buffers uptodate, but not
the folio. The last folio of a file written with small appending writes
is a typical example. Reading such a folio through ->read_folio() before
writeback replaces the written data with zeroes and marks the folio
uptodate. The folio is still dirty, so writeback then puts the zeroes on
disk as well.

Whether a read hits this depends on how it accesses the folio. read(2)
copies a range that lies within uptodate buffers directly, see
->is_partially_uptodate(), but calls ->read_folio() if the requested
range reaches into a buffer that is not uptodate, even one past EOF.
Page faults and splice, e.g. sendfile(2), always call ->read_folio() for
a folio that is not uptodate. With 1 KiB blocks and inline_data, a file
created with a few small writes and read back in one go loses its tail.
This prints 904 instead of 0:

  # mkfs.ext4 -b 1024 -O inline_data /dev/vdX && mount /dev/vdX /mnt
  # python3 -c '
  import os
  fd = os.open("/mnt/f", os.O_WRONLY | os.O_CREAT | os.O_TRUNC)
  for i in range(50):
      os.write(fd, b"x" * 100)
  os.close(fd)
  fd = os.open("/mnt/f", os.O_RDONLY)
  print(os.read(fd, 65536).count(0))'
  904

Gentoo's portage hit this once Python 3.14 started to read stdin with a
128 KiB buffer (Gentoo bug #987733): bash had written the ebuild
environment with many small writes, Python read it back with a tail of
NUL bytes, causing bash to refuse to source the result.

Fix this by zeroing only the buffers that are not uptodate and marking
them uptodate, leaving the others alone. As long as the inode has inline
data, no data blocks are allocated to the file, so a buffer that is not
uptodate is a hole. Folios without buffers are zeroed as before.

Fixes: 9c3569b50f12 ("ext4: add delalloc support for inline data")
Reported-by: Ulrich Müller <ulm@gentoo.org>
Closes: https://lore.kernel.org/ucxrjwzcv@gentoo.org
Closes: https://bugs.gentoo.org/987733
Cc: stable@vger.kernel.org
Signed-off-by: Florian Schmaus <flo@geekplace.eu>
---
 fs/ext4/inline.c | 40 +++++++++++++++++++++++++++++++++++++---
 1 file changed, 37 insertions(+), 3 deletions(-)

diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
index acdc65ce4796..d5d42b288c64 100644
--- a/fs/ext4/inline.c
+++ b/fs/ext4/inline.c
@@ -558,6 +558,39 @@ static int ext4_read_inline_folio(struct inode *inode, struct folio *folio)
 	return ret;
 }
 
+/*
+ * Zero the parts of a folio beyond the inline data that are not uptodate.
+ *
+ * ext4_da_convert_inline_data_to_extent() leaves the inline data in place
+ * until writeback, so buffered writes may already have put data into this
+ * folio. With a block size smaller than the folio size, a folio that was
+ * only partially written is not uptodate, but the buffers that were written
+ * are, and they hold the only copy of that data, so keep them. All other
+ * blocks of the folio are holes: as long as the inode has inline data, no
+ * blocks are allocated for it, so they read as zeroes.
+ */
+static void ext4_inline_zero_folio(struct folio *folio)
+{
+	struct buffer_head *head = folio_buffers(folio);
+	struct buffer_head *bh;
+	size_t offset = 0;
+
+	if (!head) {
+		folio_zero_segment(folio, 0, folio_size(folio));
+		return;
+	}
+
+	bh = head;
+	do {
+		if (!buffer_uptodate(bh)) {
+			folio_zero_range(folio, offset, bh->b_size);
+			set_buffer_uptodate(bh);
+		}
+		offset += bh->b_size;
+		bh = bh->b_this_page;
+	} while (bh != head);
+}
+
 int ext4_readpage_inline(struct inode *inode, struct folio *folio)
 {
 	int ret = 0;
@@ -569,13 +602,14 @@ int ext4_readpage_inline(struct inode *inode, struct folio *folio)
 	}
 
 	/*
-	 * Current inline data can only exist in the 1st page,
-	 * So for all the other pages, just set them uptodate.
+	 * Current inline data can only exist in the 1st page. The other
+	 * pages hold either nothing or data written after a delalloc
+	 * conversion that has not been written back yet.
 	 */
 	if (!folio->index)
 		ret = ext4_read_inline_folio(inode, folio);
 	else if (!folio_test_uptodate(folio)) {
-		folio_zero_segment(folio, 0, folio_size(folio));
+		ext4_inline_zero_folio(folio);
 		folio_mark_uptodate(folio);
 	}
 

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/2] ext4: test that ext4_readpage_inline() keeps uptodate buffers
  2026-12-03 10:06 [PATCH 0/2] ext4: fix data loss when reading during inline data conversion Florian Schmaus
  2026-12-03 10:06 ` [PATCH 1/2] ext4: don't zero uptodate buffers in ext4_readpage_inline() Florian Schmaus
@ 2026-12-03 10:06 ` Florian Schmaus
  2026-12-03 10:17   ` sashiko-bot
  2026-12-03 10:30 ` [PATCH 0/2] ext4: fix data loss when reading during inline data conversion sashiko-bot
  2 siblings, 1 reply; 5+ messages in thread
From: Florian Schmaus @ 2026-12-03 10:06 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
	Ojaswin Mujoo, Ritesh Harjani (IBM), Zhang Yi, Tao Ma
  Cc: linux-ext4, Florian Schmaus, Ulrich Müller

After a delalloc conversion of inline data, buffered writes can leave a
folio past the first one partially uptodate until writeback. Add a case
to the inline data KUnit tests that checks that ext4_readpage_inline()
keeps the uptodate buffer of such a folio, zeroes the other buffers and
marks all of them uptodate.

Signed-off-by: Florian Schmaus <flo@geekplace.eu>
---
 fs/ext4/inline-test.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/fs/ext4/inline-test.c b/fs/ext4/inline-test.c
index b3d7c51ad216..1558454d9d8b 100644
--- a/fs/ext4/inline-test.c
+++ b/fs/ext4/inline-test.c
@@ -895,6 +895,7 @@ static void test_readpage_inline_link_and_iomap(struct kunit *test)
 {
 	struct inline_test_ctx *ctx = test->priv;
 	struct inode *inode = &ctx->ei.vfs_inode;
+	struct buffer_head *head, *bh;
 	struct folio *folio;
 	struct iomap iomap = { 0 };
 	char content[85];
@@ -960,6 +961,41 @@ static void test_readpage_inline_link_and_iomap(struct kunit *test)
 	kunmap_local(kaddr);
 	folio_put(folio);
 
+	/*
+	 * 3b. ext4_readpage_inline on folio->index > 0 with only some buffers
+	 * uptodate: keeps their data, zeroes the other buffers and marks all
+	 * of them uptodate. Buffered writes after a delalloc conversion
+	 * leave such a folio behind until writeback destroys the inline data.
+	 */
+	folio = __filemap_get_folio(&inode->i_data, 2,
+				    FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
+				    GFP_KERNEL);
+	KUNIT_ASSERT_FALSE(test, IS_ERR(folio));
+	kaddr = kmap_local_folio(folio, 0);
+	memset(kaddr, 0xff, PAGE_SIZE);
+	memset(kaddr + TEST_BLOCK_SIZE, 0xab, TEST_BLOCK_SIZE);
+	kunmap_local(kaddr);
+	head = create_empty_buffers(folio, TEST_BLOCK_SIZE, 0);
+	set_buffer_uptodate(head->b_this_page);
+
+	ret = ext4_readpage_inline(inode, folio);
+	KUNIT_EXPECT_EQ(test, ret, 0);
+	KUNIT_EXPECT_TRUE(test, folio_test_uptodate(folio));
+	KUNIT_EXPECT_FALSE(test, folio_test_locked(folio));
+	kaddr = kmap_local_folio(folio, 0);
+	KUNIT_EXPECT_NULL(test, memchr_inv(kaddr, 0, TEST_BLOCK_SIZE));
+	KUNIT_EXPECT_NULL(test, memchr_inv(kaddr + TEST_BLOCK_SIZE, 0xab,
+					   TEST_BLOCK_SIZE));
+	KUNIT_EXPECT_NULL(test, memchr_inv(kaddr + 2 * TEST_BLOCK_SIZE, 0,
+					   PAGE_SIZE - 2 * TEST_BLOCK_SIZE));
+	kunmap_local(kaddr);
+	bh = head;
+	do {
+		KUNIT_EXPECT_TRUE(test, buffer_uptodate(bh));
+		bh = bh->b_this_page;
+	} while (bh != head);
+	folio_put(folio);
+
 	/* 4. ext4_readpage_inline when ext4_get_inode_loc fails -> propagates error */
 	truncate_inode_pages(&inode->i_data, 0);
 	ctx->get_inode_loc_err = -EIO;

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/2] ext4: test that ext4_readpage_inline() keeps uptodate buffers
  2026-12-03 10:06 ` [PATCH 2/2] ext4: test that ext4_readpage_inline() keeps uptodate buffers Florian Schmaus
@ 2026-12-03 10:17   ` sashiko-bot
  0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-03 10:17 UTC (permalink / raw)
  To: Florian Schmaus; +Cc: linux-ext4, tytso

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] ext4: buffer_head use-after-free after folio unlock in inline
  KUnit test
--

commit a38c8e059b87a8ba55ddb0f7f8b0994abdfe6412
Author: Florian Schmaus <flo@geekplace.eu>

ext4: test that ext4_readpage_inline() keeps uptodate buffers

This commit adds a KUnit test case to test_readpage_inline_link_and_iomap()
checking that ext4_readpage_inline() preserves uptodate buffers, zeroes the
remaining buffers, and marks all buffers uptodate on a partially uptodate
folio past index 0.

> diff --git a/fs/ext4/inline-test.c b/fs/ext4/inline-test.c
> index b3d7c51ad216b..1558454d9d8bf 100644
> --- a/fs/ext4/inline-test.c
> +++ b/fs/ext4/inline-test.c

[ ... ]

> @@ -960,6 +961,41 @@ static void test_readpage_inline_link_and_iomap(struct kunit *test)
>  	kunmap_local(kaddr);
>  	folio_put(folio);
>  
> +	/*
> +	 * 3b. ext4_readpage_inline on folio->index > 0 with only some buffers
> +	 * uptodate: keeps their data, zeroes the other buffers and marks all
> +	 * of them uptodate. Buffered writes after a delalloc conversion
> +	 * leave such a folio behind until writeback destroys the inline data.
> +	 */
> +	folio = __filemap_get_folio(&inode->i_data, 2,
> +				    FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
> +				    GFP_KERNEL);
> +	KUNIT_ASSERT_FALSE(test, IS_ERR(folio));
> +	kaddr = kmap_local_folio(folio, 0);
> +	memset(kaddr, 0xff, PAGE_SIZE);
> +	memset(kaddr + TEST_BLOCK_SIZE, 0xab, TEST_BLOCK_SIZE);
> +	kunmap_local(kaddr);
> +	head = create_empty_buffers(folio, TEST_BLOCK_SIZE, 0);
> +	set_buffer_uptodate(head->b_this_page);
> +
> +	ret = ext4_readpage_inline(inode, folio);
> +	KUNIT_EXPECT_EQ(test, ret, 0);
> +	KUNIT_EXPECT_TRUE(test, folio_test_uptodate(folio));
> +	KUNIT_EXPECT_FALSE(test, folio_test_locked(folio));
> +	kaddr = kmap_local_folio(folio, 0);
> +	KUNIT_EXPECT_NULL(test, memchr_inv(kaddr, 0, TEST_BLOCK_SIZE));
> +	KUNIT_EXPECT_NULL(test, memchr_inv(kaddr + TEST_BLOCK_SIZE, 0xab,
> +					   TEST_BLOCK_SIZE));
> +	KUNIT_EXPECT_NULL(test, memchr_inv(kaddr + 2 * TEST_BLOCK_SIZE, 0,
> +					   PAGE_SIZE - 2 * TEST_BLOCK_SIZE));
> +	kunmap_local(kaddr);
> +	bh = head;
> +	do {
> +		KUNIT_EXPECT_TRUE(test, buffer_uptodate(bh));
> +		bh = bh->b_this_page;
> +	} while (bh != head);

[Severity: Low]
Can walking head after ext4_readpage_inline() trigger a buffer_head
use-after-free?

ext4_readpage_inline() unlocks folio before returning.  Because folio is
clean, unmapped, and on the LRU, and none of its buffers are dirty or
pinned, memory reclaim can strip the buffer ring before the loop runs:

CPU0                                    CPU1 (reclaim)
test_readpage_inline_link_and_iomap()
  head = create_empty_buffers(folio, ...)
  ext4_readpage_inline(inode, folio)
    folio_unlock(folio)
                                        shrink_folio_list()
                                          folio_trylock(folio)
                                          filemap_release_folio(...)
                                            test_release_folio(...)
                                              try_to_free_buffers(folio)
                                                free_buffer_head(...)
  bh = head
  buffer_uptodate(bh)  <-- use-after-free

Holding a folio reference until folio_put() keeps the folio alive, but
try_to_free_buffers() requires folio_lock(folio), mapping->i_private_lock,
or a busy buffer to prevent freeing the attached buffer_heads.  Pinning
head with get_bh(head) before ext4_readpage_inline() and releasing it with
put_bh(head) after the loop would keep the buffer ring alive while the test
checks it.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203-ext4-inline-readpage-v1-0-1dd69e69f53b@geekplace.eu?part=2

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 0/2] ext4: fix data loss when reading during inline data conversion
  2026-12-03 10:06 [PATCH 0/2] ext4: fix data loss when reading during inline data conversion Florian Schmaus
  2026-12-03 10:06 ` [PATCH 1/2] ext4: don't zero uptodate buffers in ext4_readpage_inline() Florian Schmaus
  2026-12-03 10:06 ` [PATCH 2/2] ext4: test that ext4_readpage_inline() keeps uptodate buffers Florian Schmaus
@ 2026-12-03 10:30 ` sashiko-bot
  2 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-03 10:30 UTC (permalink / raw)
  To: Florian Schmaus; +Cc: linux-ext4, tytso

Sashiko has finished reviewing this 2-patch series:

- Patch 1: no issues found.
- Patch 2 ("ext4: test that ext4_readpage_inline() keeps uptodat..."): 1
  potential issue (1 Low)

A detailed review has been sent as a separate reply to patch 2.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203-ext4-inline-readpage-v1-0-1dd69e69f53b@geekplace.eu

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-03 10:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 10:06 [PATCH 0/2] ext4: fix data loss when reading during inline data conversion Florian Schmaus
2026-12-03 10:06 ` [PATCH 1/2] ext4: don't zero uptodate buffers in ext4_readpage_inline() Florian Schmaus
2026-12-03 10:06 ` [PATCH 2/2] ext4: test that ext4_readpage_inline() keeps uptodate buffers Florian Schmaus
2026-12-03 10:17   ` sashiko-bot
2026-12-03 10:30 ` [PATCH 0/2] ext4: fix data loss when reading during inline data conversion sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox