Skip to contentSkip to conversation

ext4: preserve EXT4_STATE_MAY_INLINE_DATA on failed inline data conversion

PATCH New Arbab Haider 2 participants 2 messages Nov 23

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ext4: preserve EXT4_STATE_MAY_INLINE_DATA on failed inline data conversion

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 falls through to ext4_convert_inline_data_nolock() with EXT4_STATE_MAY_INLINE_DATA cleared. If ext4_map_blocks() fails (for example due to -ENOSPC or a corrupted block bitmap), the out_restore path calls ext4_restore_inline_data(), which unconditionally re-sets EXT4_STATE_MAY_INLINE_DATA (both inside ext4_create_inline_data() and directly).

When ext4_do_writepages() subsequently runs (e.g. via O_SYNC ext4_sync_file()), it finds both ext4_has_inline_data(inode) and EXT4_STATE_MAY_INLINE_DATA set 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

Fix this in ext4_convert_inline_data_nolock() (called under xattr_sem, which serializes against ext4_da_convert_inline_data_to_extent()) by saving EXT4_STATE_MAY_INLINE_DATA before destroying inline data and clearing it again after ext4_restore_inline_data() if it was not set originally.

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")
---
 fs/ext4/inline.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/ext4/inline.c +7 -1
index ceee69a66..c4ef91cd3 100644
@@ -1105,6 +1105,8 @@ static int ext4_convert_inline_data_nolock(handle_t *handle,
 	struct buffer_head *data_bh = NULL;
 	struct ext4_map_blocks map;
 	int inline_size;
+	bool may_inline = ext4_test_inode_state(inode,
+						EXT4_STATE_MAY_INLINE_DATA);
 
 	inline_size = ext4_get_inline_size(inode);
 	buf = kmalloc(inline_size, GFP_NOFS);
@@ -1181,8 +1183,12 @@ static int ext4_convert_inline_data_nolock(handle_t *handle,
 	}
 
 out_restore:
-	if (error)
+	if (error) {
 		ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
+		if (!may_inline)
+			ext4_clear_inode_state(inode,
+					       EXT4_STATE_MAY_INLINE_DATA);
+	}
 
 out:
 	brelse(data_bh);
Signature · 1 line
2.53.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] ext4: race and stale data in ext4_convert_inline_data_nolock() when !may_inline - [Medium] ext4…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] ext4: race and stale data in ext4_convert_inline_data_nolock() when !may_inline
- [Medium] ext4: transient EXT4_STATE_MAY_INLINE_DATA in ext4_restore_inline_data()
--

commit 8c43e34008561b523f81b41ad4705762c74a86fc
Author: Arbab Haider <[email protected]>

ext4: preserve EXT4_STATE_MAY_INLINE_DATA on failed inline data conversion

This commit saves the initial EXT4_STATE_MAY_INLINE_DATA state in ext4_convert_inline_data_nolock() and clears it again after ext4_restore_inline_data() if the flag was not set on entry.

fs/ext4/inline.c static int ext4_convert_inline_data_nolock(handle_t *handle, · line 1128

Quoted 13 lines
fs/ext4/inline.c +2 -0
index 2e14ee9b4241b..1694346f74c7c 100644
@@ -1121,6 +1121,8 @@ static int ext4_convert_inline_data_nolock(handle_t *handle,
 	struct buffer_head *data_bh = NULL;
 	struct ext4_map_blocks map;
 	int inline_size;
+	bool may_inline = ext4_test_inode_state(inode,
+						EXT4_STATE_MAY_INLINE_DATA);
 
 	inline_size = ext4_get_inline_size(inode);
 	buf = kmalloc(inline_size, GFP_NOFS);

[Severity: High]
Should ext4_convert_inline_data_nolock() proceed at all when may_inline is false?

When EXT4_STATE_MAY_INLINE_DATA is cleared, ext4_da_convert_inline_data_to_extent() has already moved the inline data into dirty folio 0 with a delayed-allocation buffer head. If ext4_convert_inline_data_nolock() runs while that writeout is still pending, two problems occur:

1. If ext4_convert_inline_data_nolock() succeeds, it reads stale on-disk inline data via ext4_read_inline_data() and copies it into the newly allocated block buffer head, ignoring the newer dirty data in folio 0.

2. If ext4_map_blocks() fails, ext4_destroy_inline_data_nolock() has temporarily cleared EXT4_INODE_INLINE_DATA and set EXT4_INODE_EXTENTS before out_restore restores EXT4_INODE_INLINE_DATA. Because ext4_do_writepages() checks ext4_has_inline_data(inode) without holding xattr_sem, it can miss ext4_destroy_inline_data() during that window and proceed to map blocks after EXT4_INODE_EXTENTS is cleared again:

CPU0 CPU1
ext4_convert_inline_data()
filemap_flush()
ext4_write_lock_xattr()
ext4_convert_inline_data_nolock()

    ext4_destroy_inline_data_nolock()
      /* clears INLINE_DATA,
         sets EXTENTS */
                                      ext4_do_writepages()
                                        if (ext4_has_inline_data(inode))
                                          /* false, skipped */
    ext4_map_blocks() -> fails
    ext4_restore_inline_data()
      /* clears EXTENTS,
         restores INLINE_DATA */
  ext4_write_unlock_xattr()
                                        mpage_prepare_extent_to_map()
                                          ...
                                          ext4_map_blocks()
                                            ext4_ind_map_blocks()
                                              <-- maps inline data as
                                                  indirect blocks
Quoted 4 lines
fs/ext4/inline.c +0 -0
@@ -1197,8 +1199,12 @@ static int ext4_convert_inline_data_nolock(handle_t *handle,
 	}
 
 out_restore:

fs/ext4/inline.c static int ext4_convert_inline_data_nolock(handle_t *handle, · line 1210

Quoted 10 lines
fs/ext4/inline.c +5 -1
-	if (error)
+	if (error) {
 		ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
+		if (!may_inline)
+			ext4_clear_inode_state(inode,
+					       EXT4_STATE_MAY_INLINE_DATA);
+	}
 
 out:
 	brelse(data_bh);

[Severity: Medium]
Does clearing EXT4_STATE_MAY_INLINE_DATA after ext4_restore_inline_data() still leave a window where both EXT4_INODE_INLINE_DATA and EXT4_STATE_MAY_INLINE_DATA are set?

ext4_restore_inline_data() re-creates the inline data (setting EXT4_INODE_INLINE_DATA) and unconditionally sets EXT4_STATE_MAY_INLINE_DATA before returning to ext4_convert_inline_data_nolock():

fs/ext4/inline.c:ext4_restore_inline_data() {
	ret = ext4_create_inline_data(handle, inode, inline_size);
	if (ret) {
    ...
	}
	ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
	ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
}

Because ext4_do_writepages() checks ext4_has_inline_data(inode) and BUG_ON(ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) before ext4_destroy_inline_data() acquires xattr_sem, a concurrent writeback can still hit that BUG_ON() in the window before ext4_clear_inode_state() runs:

CPU0 CPU1
ext4_convert_inline_data_nolock()
ext4_restore_inline_data()

    ext4_create_inline_data()
      /* sets EXT4_INODE_INLINE_DATA */
    ext4_set_inode_state(...,
      EXT4_STATE_MAY_INLINE_DATA)
                                      ext4_do_writepages()
                                        if (ext4_has_inline_data(inode))
                                          ...
                                          BUG_ON(ext4_test_inode_state(
                                            inode,
                                            EXT4_STATE_MAY_INLINE_DATA))
                                            <-- kernel BUG
  if (!may_inline)
    ext4_clear_inode_state(...,
      EXT4_STATE_MAY_INLINE_DATA)

Passing may_inline into ext4_restore_inline_data() (which has no other callers) and only setting EXT4_STATE_MAY_INLINE_DATA when may_inline is true would avoid publishing the flag when it was already cleared. Also, note that the commit message says ext4_create_inline_data() sets EXT4_STATE_MAY_INLINE_DATA, whereas ext4_create_inline_data() only sets EXT4_INODE_INLINE_DATA.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1