All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Arbab Haider" <arbabhaider649@gmail.com>
Cc: linux-ext4@vger.kernel.org, tytso@mit.edu
Subject: Re: [PATCH] ext4: preserve EXT4_STATE_MAY_INLINE_DATA on failed inline data conversion
Date: Mon, 23 Nov 2026 07:10:10 +0000	[thread overview]
Message-ID: <sashiko-outbox-228796@kernel.org> (raw)
In-Reply-To: <20261123065448.60742-2-arbabhaider649@gmail.com>

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 <arbabhaider649@gmail.com>

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.

> diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
> index 2e14ee9b4241b..1694346f74c7c 100644
> --- a/fs/ext4/inline.c
> +++ b/fs/ext4/inline.c
> @@ -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

> @@ -1197,8 +1199,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);

[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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261123065448.60742-2-arbabhaider649@gmail.com?part=1

      reply	other threads:[~2026-11-23  7:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  6:54 [PATCH] ext4: preserve EXT4_STATE_MAY_INLINE_DATA on failed inline data conversion Arbab Haider
2026-11-23  7:10 ` sashiko-bot [this message]

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=sashiko-outbox-228796@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=arbabhaider649@gmail.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tytso@mit.edu \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.