* [PATCH v2] ext4: avoid inline data conversion race when !EXT4_STATE_MAY_INLINE_DATA
@ 2026-11-23 7:48 Arbab Haider
2026-11-23 8:12 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Arbab Haider @ 2026-11-23 7:48 UTC (permalink / raw)
To: Theodore Ts'o
Cc: Andreas Dilger, Jan Kara, linux-ext4, linux-kernel, stable,
Arbab Haider
Commit ef09ed5d37b8 ("ext4: fix bug_on in ext4_writepages") attempted to
prevent ext4_convert_inline_data() from racing with delayed-allocation
inline data conversion by calling filemap_flush() when
EXT4_STATE_MAY_INLINE_DATA is already cleared.
However, filemap_flush() is asynchronous (WB_SYNC_NONE) and can return 0
while folio 0 is locked by a concurrent buffered write and
ext4_has_inline_data(inode) is still true. ext4_convert_inline_data()
then proceeds to ext4_convert_inline_data_nolock() even though
EXT4_STATE_MAY_INLINE_DATA is already cleared (meaning
ext4_da_convert_inline_data_to_extent() has already moved the data into
dirty folio 0). This causes multiple bugs:
1. If ext4_convert_inline_data_nolock() succeeds, it copies stale
on-disk inline data into the newly allocated block instead of the
newer dirty data in folio 0.
2. If ext4_map_blocks() fails (e.g. -ENOSPC or a corrupted block
bitmap), ext4_destroy_inline_data_nolock() has temporarily cleared
EXT4_INODE_INLINE_DATA and set EXT4_INODE_EXTENTS before
ext4_restore_inline_data() restores EXT4_INODE_INLINE_DATA and
unconditionally re-sets EXT4_STATE_MAY_INLINE_DATA. This races with
ext4_do_writepages() and triggers:
kernel BUG at fs/ext4/inode.c:2949!
RIP: 0010:ext4_do_writepages+0x25c0/0x39a0
Call Trace:
ext4_writepages+0x285/0x500
do_writepages+0x205/0x520
filemap_writeback+0x1a6/0x250
file_write_and_wait_range+0x8c/0x150
ext4_sync_file+0x2cf/0x950
ext4_buffered_write_iter+0x428/0x5d0
ext4_file_write_iter+0x7cb/0x1450
3. A concurrent ext4_da_convert_inline_data_to_extent() can also clear
EXT4_STATE_MAY_INLINE_DATA between the initial check in
ext4_convert_inline_data() and acquiring ext4_write_lock_xattr().
Fix this in ext4_convert_inline_data() by:
- Using synchronous filemap_write_and_wait() instead of filemap_flush()
when !EXT4_STATE_MAY_INLINE_DATA.
- Returning -EIO instead of proceeding to
ext4_convert_inline_data_nolock() if ext4_has_inline_data(inode) is
still true after writeback.
- Re-checking EXT4_STATE_MAY_INLINE_DATA under ext4_write_lock_xattr()
(which serializes against ext4_da_convert_inline_data_to_extent()) and
retrying via filemap_write_and_wait() if it was cleared concurrently.
Tested locally in QEMU (x86_64, 4 CPUs, KASAN+UBSAN) using a concurrent
mmap + O_SYNC write reproducer on an inline_data ext4 image: hit
kernel BUG at fs/ext4/inode.c:2949 on mount #1 in <1s before the patch,
and completed 32 fresh mount/race iterations in 10s with zero BUG,
WARNING, or KASAN reports after the patch.
Fixes: ef09ed5d37b8 ("ext4: fix bug_on in ext4_writepages")
Cc: stable@vger.kernel.org
Signed-off-by: Arbab Haider <arbabhaider649@gmail.com>
---
Changes in v2:
- Address Sashiko AI review feedback: instead of preserving
EXT4_STATE_MAY_INLINE_DATA on error in ext4_convert_inline_data_nolock(),
prevent ext4_convert_inline_data() from calling
ext4_convert_inline_data_nolock() at all when EXT4_STATE_MAY_INLINE_DATA
is already cleared (using filemap_write_and_wait(), returning -EIO if
inline data remains, and re-checking under xattr_sem).
- Fix commit description inaccuracy regarding ext4_create_inline_data().
fs/ext4/inline.c | 23 +++++++++++++++++++----
1 file changed, 19 insertions(+), 4 deletions(-)
diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
index ceee69a66..f644b229e 100644
--- a/fs/ext4/inline.c
+++ b/fs/ext4/inline.c
@@ -1977,6 +1977,7 @@ int ext4_convert_inline_data(struct inode *inode)
handle_t *handle;
struct ext4_iloc iloc;
+retry:
if (!ext4_has_inline_data(inode)) {
ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
return 0;
@@ -1984,14 +1985,21 @@ int ext4_convert_inline_data(struct inode *inode)
/*
* Inode has inline data but EXT4_STATE_MAY_INLINE_DATA is
* cleared. This means we are in the middle of moving of
- * inline data to delay allocated block. Just force writeout
- * here to finish conversion.
+ * inline data to delay allocated block. Force synchronous
+ * writeout here to finish conversion.
*/
- error = filemap_flush(inode->i_mapping);
+ error = filemap_write_and_wait(inode->i_mapping);
if (error)
return error;
if (!ext4_has_inline_data(inode))
return 0;
+ /*
+ * Writeback did not clear inline data (e.g. filesystem is in
+ * emergency state). Do not proceed to
+ * ext4_convert_inline_data_nolock(), which would read stale
+ * on-disk inline data or race with writeback.
+ */
+ return -EIO;
}
needed_blocks = ext4_chunk_trans_extent(inode, 1);
@@ -2008,8 +2016,15 @@ int ext4_convert_inline_data(struct inode *inode)
}
ext4_write_lock_xattr(inode, &no_expand);
- if (ext4_has_inline_data(inode))
+ if (ext4_has_inline_data(inode)) {
+ if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
+ ext4_write_unlock_xattr(inode, &no_expand);
+ ext4_journal_stop(handle);
+ brelse(iloc.bh);
+ goto retry;
+ }
error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
+ }
ext4_write_unlock_xattr(inode, &no_expand);
ext4_journal_stop(handle);
out_free:
--
2.53.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2] ext4: avoid inline data conversion race when !EXT4_STATE_MAY_INLINE_DATA
2026-11-23 7:48 [PATCH v2] ext4: avoid inline data conversion race when !EXT4_STATE_MAY_INLINE_DATA Arbab Haider
@ 2026-11-23 8:12 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-23 8:12 UTC (permalink / raw)
To: Arbab Haider; +Cc: linux-ext4, tytso
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] ext4: spurious -EIO in ext4_convert_inline_data() after
truncate to zero
--
commit 9ccbfe6796f4c788a0b995f876e6cdb2091e3037
Author: Arbab Haider <arbabhaider649@gmail.com>
ext4: avoid inline data conversion race when !EXT4_STATE_MAY_INLINE_DATA
This commit replaces filemap_flush() with filemap_write_and_wait() in
ext4_convert_inline_data() when EXT4_STATE_MAY_INLINE_DATA is cleared,
returns -EIO if inline data remains after writeback, and rechecks
EXT4_STATE_MAY_INLINE_DATA after acquiring xattr_sem.
> diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
> index 2e14ee9b4241b..1faf277a22908 100644
> --- a/fs/ext4/inline.c
> +++ b/fs/ext4/inline.c
> @@ -1997,6 +1997,7 @@ int ext4_convert_inline_data(struct inode *inode)
> handle_t *handle;
> struct ext4_iloc iloc;
>
> +retry:
> if (!ext4_has_inline_data(inode)) {
> ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
> return 0;
> @@ -2004,14 +2005,21 @@ int ext4_convert_inline_data(struct inode *inode)
> /*
> * Inode has inline data but EXT4_STATE_MAY_INLINE_DATA is
> * cleared. This means we are in the middle of moving of
> - * inline data to delay allocated block. Just force writeout
> - * here to finish conversion.
> + * inline data to delay allocated block. Force synchronous
> + * writeout here to finish conversion.
> */
> - error = filemap_flush(inode->i_mapping);
> + error = filemap_write_and_wait(inode->i_mapping);
> if (error)
> return error;
> if (!ext4_has_inline_data(inode))
> return 0;
> + /*
> + * Writeback did not clear inline data (e.g. filesystem is in
> + * emergency state). Do not proceed to
> + * ext4_convert_inline_data_nolock(), which would read stale
> + * on-disk inline data or race with writeback.
> + */
> + return -EIO;
> }
[Severity: High]
Can ext4_convert_inline_data() fail with a spurious -EIO after an
inline-data inode is truncated to 0 before writeback runs?
When a delayed-allocation write exceeds inline capacity,
ext4_da_convert_inline_data_to_extent() copies the data into folio 0,
marks it dirty, and clears EXT4_STATE_MAY_INLINE_DATA while leaving
EXT4_INODE_INLINE_DATA set until ext4_do_writepages() destroys the inline
data. If the file is truncated to 0 before writeback runs,
truncate_pagecache() drops dirty folio 0 and ext4_inline_data_truncate()
removes the delayed extent, but leaves EXT4_INODE_INLINE_DATA set and
EXT4_STATE_MAY_INLINE_DATA cleared.
Because inode->i_mapping no longer has dirty pages,
filemap_write_and_wait() returns 0 without calling ext4_destroy_inline_data(),
so ext4_has_inline_data(inode) remains true. Later calls to
ext4_convert_inline_data() (such as fallocate(), an extending truncate, or
ext4_page_mkwrite()) then fail with -EIO, or raise SIGBUS on shared mmap
write faults.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123074825.63294-2-arbabhaider649@gmail.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-23 8:12 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 7:48 [PATCH v2] ext4: avoid inline data conversion race when !EXT4_STATE_MAY_INLINE_DATA Arbab Haider
2026-11-23 8:12 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox