From: Arbab Haider <arbabhaider649@gmail.com>
To: tytso@mit.edu
Cc: adilger.kernel@dilger.ca, libaokun@linux.alibaba.com,
jack@suse.cz, ojaswin@linux.ibm.com, ritesh.list@gmail.com,
yi.zhang@huawei.com, yebin10@huawei.com,
linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: [PATCH v4 2/5] ext4: fix delayed-allocation inline data conversion races
Date: Thu, 26 Nov 2026 12:07:46 +0500 [thread overview]
Message-ID: <20261126070748.272109-4-arbabhaider649@gmail.com> (raw)
In-Reply-To: <20261126070748.272109-2-arbabhaider649@gmail.com>
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, several races remain in the delayed-allocation inline handoff:
1. 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), either copying stale on-disk inline data or rolling
back via ext4_restore_inline_data() and re-setting
EXT4_STATE_MAY_INLINE_DATA while writeback is pending, triggering:
kernel BUG at fs/ext4/inode.c:3028!
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
2. In ext4_da_convert_inline_data_to_extent(), folio_mark_dirty(folio)
was called before ext4_clear_inode_state(EXT4_STATE_MAY_INLINE_DATA),
and ext4_do_writepages() tested EXT4_STATE_MAY_INLINE_DATA before
acquiring xattr_sem. On concurrent or weakly ordered CPUs, writeback
can observe PAGECACHE_TAG_DIRTY with EXT4_STATE_MAY_INLINE_DATA still
set and hit BUG_ON() in ext4_do_writepages().
3. When an inode already in delayed allocation
(!EXT4_STATE_MAY_INLINE_DATA) is truncated to i_size == 0 in
ext4_inline_data_truncate() or drops a failed write at i_size == 0 in
ext4_da_truncate_failed_write(), dirty folio 0 and delayed extents are
dropped while EXT4_INODE_INLINE_DATA remains set with
!EXT4_STATE_MAY_INLINE_DATA and no dirty pages. Also, when truncated
to i_size == inline_size, ext4_inline_data_truncate() skipped removing
delayed extents beyond i_size because ext4_es_remove_extent() was
inside if (i_size < inline_size).
Fix the delayed-allocation handoff:
- In ext4_da_convert_inline_data_to_extent(), hold xattr_sem for write
and clear EXT4_STATE_MAY_INLINE_DATA before marking folio 0 dirty, and
in writeback validate the handoff and remove inline data under the
same xattr_sem write lock. Return an error if the handoff is
incomplete or removing inline data fails.
- In ext4_convert_inline_data(), serialize the initial and post-writeback
state checks under down_read(&xattr_sem) (with a lockless fast path when
neither EXT4_INODE_INLINE_DATA nor EXT4_STATE_MAY_INLINE_DATA is set),
use synchronous filemap_write_and_wait() when
!EXT4_STATE_MAY_INLINE_DATA, return ext4_emergency_state() or -EIO if
ext4_has_inline_data(inode) still holds after writeback, and re-check
EXT4_STATE_MAY_INLINE_DATA under ext4_write_lock_xattr() with a retry.
- In ext4_inline_data_truncate(), finish destroying inline data via
ext4_destroy_inline_data_nolock() when !EXT4_STATE_MAY_INLINE_DATA and
inode->i_size == 0, and remove delayed extents beyond i_size even when
i_size == inline_size. In ext4_da_truncate_failed_write(), call
ext4_truncate() when inode->i_size == 0 with EXT4_INODE_INLINE_DATA set
and !EXT4_STATE_MAY_INLINE_DATA (falling back to ext4_es_remove_extent()
if ext4_truncate() fails).
- Update test_convert_inline_data_and_rollback() in fs/ext4/inline-test.c
to match the new -EIO behavior when !EXT4_STATE_MAY_INLINE_DATA remains
inline after writeback, and also cover normal conversion with
EXT4_STATE_MAY_INLINE_DATA set.
Fixes: ef09ed5d37b8 ("ext4: fix bug_on in ext4_writepages")
Cc: stable@vger.kernel.org
Signed-off-by: Arbab Haider <arbabhaider649@gmail.com>
---
fs/ext4/inline-test.c | 17 ++++++-
fs/ext4/inline.c | 104 ++++++++++++++++++++++++++++++++----------
fs/ext4/inode.c | 18 ++++++--
3 files changed, 111 insertions(+), 28 deletions(-)
diff --git a/fs/ext4/inline-test.c b/fs/ext4/inline-test.c
index 0198ce831..006c5b3ea 100644
--- a/fs/ext4/inline-test.c
+++ b/fs/ext4/inline-test.c
@@ -717,6 +717,11 @@ static void test_write_and_destroy_inline_data(struct kunit *test)
KUNIT_ASSERT_FALSE(test, IS_ERR(handle));
ret = ext4_get_inode_loc(inode, &iloc);
KUNIT_ASSERT_EQ(test, ret, 0);
+ ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
+ ret = ext4_destroy_inline_data(handle, inode);
+ KUNIT_EXPECT_EQ(test, ret, -EIO);
+ KUNIT_EXPECT_TRUE(test, ext4_has_inline_data(inode));
+ ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
ret = ext4_destroy_inline_data(handle, inode);
KUNIT_EXPECT_EQ(test, ret, 0);
KUNIT_EXPECT_FALSE(test, ext4_has_inline_data(inode));
@@ -741,6 +746,7 @@ static void test_write_and_destroy_inline_data(struct kunit *test)
handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
KUNIT_ASSERT_FALSE(test, IS_ERR(handle));
+ ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
ret = ext4_destroy_inline_data(handle, inode);
KUNIT_EXPECT_EQ(test, ret, 0);
KUNIT_EXPECT_FALSE(test, ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS));
@@ -1139,14 +1145,23 @@ static void test_convert_inline_data_and_rollback(struct kunit *test)
/*
* 2. Inline inode without EXT4_STATE_MAY_INLINE_DATA -> calls
- * filemap_flush() and finishes converting inline data if still inline.
+ * filemap_write_and_wait() and returns -EIO if still inline instead of
+ * falling through to ext4_convert_inline_data_nolock(). With
+ * EXT4_STATE_MAY_INLINE_DATA set, converts inline data to an extent.
*/
ret = create_inline_file(inode, buf, sizeof(buf));
KUNIT_ASSERT_EQ(test, ret, 0);
ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
ret = ext4_convert_inline_data(inode);
+ KUNIT_EXPECT_EQ(test, ret, -EIO);
+ KUNIT_EXPECT_TRUE(test, ext4_has_inline_data(inode));
+
+ ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
+ ret = ext4_convert_inline_data(inode);
KUNIT_EXPECT_EQ(test, ret, 0);
KUNIT_EXPECT_FALSE(test, ext4_has_inline_data(inode));
+ KUNIT_EXPECT_FALSE(test,
+ ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA));
KUNIT_EXPECT_MEMEQ(test, ctx->data_block_buf, buf, sizeof(buf));
/*
diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
index acdc65ce4..df50b0483 100644
--- a/fs/ext4/inline.c
+++ b/fs/ext4/inline.c
@@ -921,7 +921,7 @@ static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
if (IS_ERR(folio))
return PTR_ERR(folio);
- down_read(&EXT4_I(inode)->xattr_sem);
+ down_write(&EXT4_I(inode)->xattr_sem);
if (!ext4_has_inline_data(inode)) {
ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
goto out;
@@ -938,7 +938,7 @@ static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
ret = ext4_block_write_begin(NULL, folio, 0, inline_size,
ext4_da_get_block_prep);
if (ret) {
- up_read(&EXT4_I(inode)->xattr_sem);
+ up_write(&EXT4_I(inode)->xattr_sem);
folio_unlock(folio);
folio_put(folio);
ext4_truncate_failed_write(inode);
@@ -946,12 +946,19 @@ static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
}
clear_buffer_new(folio_buffers(folio));
+ /*
+ * Clear EXT4_STATE_MAY_INLINE_DATA before marking folio 0 dirty so
+ * concurrent writeback cannot see PAGECACHE_TAG_DIRTY while
+ * EXT4_STATE_MAY_INLINE_DATA is still set. Holding xattr_sem for
+ * write prevents ext4_convert_inline_data() from observing the
+ * cleared state before folio 0 is dirtied.
+ */
+ ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
folio_mark_dirty(folio);
folio_mark_uptodate(folio);
- ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
out:
- up_read(&EXT4_I(inode)->xattr_sem);
+ up_write(&EXT4_I(inode)->xattr_sem);
if (folio) {
folio_unlock(folio);
folio_put(folio);
@@ -1842,10 +1849,18 @@ bool empty_inline_dir(struct inode *dir, int *has_inline_data)
int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
{
- int ret, no_expand;
+ int ret = 0, no_expand;
ext4_write_lock_xattr(inode, &no_expand);
+ if (!ext4_has_inline_data(inode))
+ goto out;
+ /* Only writeback of a completed delayed-allocation handoff may proceed. */
+ if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
+ ret = -EIO;
+ goto out;
+ }
ret = ext4_destroy_inline_data_nolock(handle, inode);
+out:
ext4_write_unlock_xattr(inode, &no_expand);
return ret;
@@ -1914,6 +1929,25 @@ int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
if ((err = ext4_orphan_add(handle, inode)) != 0)
goto out;
+ /*
+ * If the file was already moved to delayed allocation
+ * (!EXT4_STATE_MAY_INLINE_DATA) and is now truncated to zero,
+ * truncate_pagecache() has already dropped dirty folio 0. Finish
+ * converting the empty inode out of inline data here instead of
+ * leaving EXT4_INODE_INLINE_DATA set with !EXT4_STATE_MAY_INLINE_DATA
+ * and no dirty pages.
+ */
+ if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
+ inode->i_size == 0) {
+ down_write(&EXT4_I(inode)->i_data_sem);
+ EXT4_I(inode)->i_disksize = 0;
+ ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
+ up_write(&EXT4_I(inode)->i_data_sem);
+
+ err = ext4_destroy_inline_data_nolock(handle, inode);
+ goto out;
+ }
+
if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0)
goto out;
@@ -1922,20 +1956,19 @@ int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
inline_size = ext4_get_inline_size(inode);
EXT4_I(inode)->i_disksize = i_size;
- if (i_size < inline_size) {
- /*
- * if there's inline data to truncate and this file was
- * converted to extents after that inline data was written,
- * the extent status cache must be cleared to avoid leaving
- * behind stale delayed allocated extent entries
- */
- if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
- ext4_lblk_t lblk = EXT4_B_TO_LBLK(inode, i_size);
+ /*
+ * If this file was moved to delayed allocation
+ * (!EXT4_STATE_MAY_INLINE_DATA), remove delayed extents beyond the
+ * new i_size while keeping block 0 if i_size > 0 (folio 0 is still
+ * dirty in the page cache).
+ */
+ if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
+ ext4_lblk_t lblk = EXT4_B_TO_LBLK(inode, inode->i_size);
- ext4_es_remove_extent(inode, lblk,
- EXT_MAX_BLOCKS - lblk);
- }
+ ext4_es_remove_extent(inode, lblk, EXT_MAX_BLOCKS - lblk);
+ }
+ if (i_size < inline_size) {
/* Clear the content in the xattr space. */
if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0)
@@ -2005,22 +2038,40 @@ int ext4_convert_inline_data(struct inode *inode)
handle_t *handle;
struct ext4_iloc iloc;
+retry:
+ if (!ext4_has_inline_data(inode) &&
+ !ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
+ return 0;
+
+ down_read(&EXT4_I(inode)->xattr_sem);
if (!ext4_has_inline_data(inode)) {
ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
+ up_read(&EXT4_I(inode)->xattr_sem);
return 0;
- } else if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
+ }
+ if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
+ up_read(&EXT4_I(inode)->xattr_sem);
/*
* 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))
+ down_read(&EXT4_I(inode)->xattr_sem);
+ if (!ext4_has_inline_data(inode)) {
+ up_read(&EXT4_I(inode)->xattr_sem);
return 0;
+ }
+ up_read(&EXT4_I(inode)->xattr_sem);
+ error = ext4_emergency_state(inode->i_sb);
+ if (error)
+ return error;
+ return -EIO;
}
+ up_read(&EXT4_I(inode)->xattr_sem);
needed_blocks = ext4_chunk_trans_extent(inode, 1);
@@ -2036,8 +2087,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:
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 07a62d856..f27aeae02 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -3019,16 +3019,20 @@ static int ext4_do_writepages(struct mpage_da_data *mpd)
* we'd better clear the inline data here.
*/
if (ext4_has_inline_data(inode)) {
+ int stop_err;
+
/* Just inode will be modified... */
handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
if (IS_ERR(handle)) {
ret = PTR_ERR(handle);
goto out_writepages;
}
- BUG_ON(ext4_test_inode_state(inode,
- EXT4_STATE_MAY_INLINE_DATA));
- ext4_destroy_inline_data(handle, inode);
- ext4_journal_stop(handle);
+ ret = ext4_destroy_inline_data(handle, inode);
+ stop_err = ext4_journal_stop(handle);
+ if (!ret)
+ ret = stop_err;
+ if (ret)
+ goto out_writepages;
}
/*
@@ -3437,9 +3441,15 @@ static void ext4_da_truncate_failed_write(struct inode *inode)
filemap_invalidate_lock(mapping);
truncate_inode_pages(mapping, inode->i_size);
+ if (inode->i_size == 0 && ext4_has_inline_data(inode) &&
+ !ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
+ if (!ext4_truncate(inode))
+ goto out_unlock;
+ }
down_write(&EXT4_I(inode)->i_data_sem);
ext4_es_remove_extent(inode, lblk, EXT_MAX_BLOCKS - lblk);
up_write(&EXT4_I(inode)->i_data_sem);
+out_unlock:
filemap_invalidate_unlock(mapping);
}
--
2.53.0
next prev parent reply other threads:[~2026-11-26 7:08 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 7:07 [PATCH v4 0/5] ext4: fix " Arbab Haider
2026-11-26 7:07 ` [PATCH v4 1/5] ext4: stub the nonatomic buffer lookup in inode KUnit tests Arbab Haider
2026-11-26 7:07 ` Arbab Haider [this message]
2026-11-26 7:07 ` [PATCH v4 3/5] ext4: keep EXT4_STATE_MAY_INLINE_DATA set during inline conversion Arbab Haider
2026-11-26 7:07 ` [PATCH v4 4/5] ext4: re-check inline data under filemap_invalidate_lock in swap_inode_boot_loader Arbab Haider
2026-11-26 7:07 ` [PATCH v4 5/5] ext4: serialize inode migration against concurrent inline data conversion Arbab Haider
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261126070748.272109-4-arbabhaider649@gmail.com \
--to=arbabhaider649@gmail.com \
--cc=adilger.kernel@dilger.ca \
--cc=jack@suse.cz \
--cc=libaokun@linux.alibaba.com \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ojaswin@linux.ibm.com \
--cc=ritesh.list@gmail.com \
--cc=stable@vger.kernel.org \
--cc=tytso@mit.edu \
--cc=yebin10@huawei.com \
--cc=yi.zhang@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®