All of lore.kernel.org
 help / color / mirror / Atom feed
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
Subject: [PATCH v4 3/5] ext4: keep EXT4_STATE_MAY_INLINE_DATA set during inline conversion
Date: Thu, 26 Nov 2026 12:07:47 +0500	[thread overview]
Message-ID: <20261126070748.272109-5-arbabhaider649@gmail.com> (raw)
In-Reply-To: <20261126070748.272109-2-arbabhaider649@gmail.com>

In ext4_convert_inline_data_nolock(), ext4_destroy_inline_data_nolock()
cleared both EXT4_INODE_INLINE_DATA and EXT4_STATE_MAY_INLINE_DATA and
dropped i_data_sem before ext4_map_blocks() allocated and initialized
block 0.  When ext4_page_mkwrite() invokes ext4_convert_inline_data()
without holding i_rwsem, two classes of races occur during that window:

1. A concurrent ext4_da_write_begin() sees !EXT4_STATE_MAY_INLINE_DATA,
   bypasses xattr_sem, and dirties folio 0 before ext4_map_blocks() fails
   (e.g. -ENOSPC) and ext4_restore_inline_data() restores
   EXT4_INODE_INLINE_DATA and EXT4_STATE_MAY_INLINE_DATA, triggering
   BUG_ON(ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) in
   ext4_do_writepages().
2. Callers that check ext4_has_inline_data() without xattr_sem while
   holding only i_rwsem or folio_lock can observe the transiently cleared
   EXT4_INODE_INLINE_DATA flag before block 0 is initialized or rollback
   completes:
   - ext4_convert_inline_data_to_extent() and ext4_prepare_inline_data()
     can misroute writes or recreate inline data after conversion.
   - ext4_dio_read_iter() and ext4_dio_write_iter() can bypass the
     buffered I/O fallback, clear EXT4_STATE_MAY_INLINE_DATA after a
     rollback, or hit WARN_ON_ONCE(ext4_has_inline_data()) in
     ext4_iomap_begin().
   - ext4_setattr() can skip ext4_convert_inline_data() and persist
     i_size > i_inline_size after rollback.
   - ext4_block_zero_eof() can enter ext4_block_zero_range(), failing
     with -ERANGE, aliasing JBD2 buffers in data=journal mode, or
     dirtying folio 0 across rollback.
   - ext4_bmap() can hit WARN_ON_ONCE(ext4_has_inline_data()) in
     ext4_iomap_begin().
   - ext4_iomap_begin_report() (FIEMAP / SEEK_DATA / SEEK_HOLE) can read
     ei->i_data[] as indirect block pointers after rollback, triggering
     EXT4_ERROR_INODE().
   - ext4_read_folio() and ext4_readahead() can cache a zeroed folio 0 or
     read stale on-disk block contents.

Keep EXT4_STATE_MAY_INLINE_DATA set during
ext4_convert_inline_data_nolock() until ext4_map_blocks() and block
initialization succeed (clearing it on unrecoverable failure in
ext4_destroy_inline_data_nolock() / ext4_restore_inline_data(), and
clearing it on success in ext4_convert_inline_data_to_extent()).
Serialize the inline-data checks with xattr_sem in
ext4_convert_inline_data_to_extent(), ext4_prepare_inline_data(),
ext4_dio_read_iter(), ext4_dio_write_iter() (respecting IOCB_NOWAIT),
ext4_setattr(), ext4_block_zero_eof(), ext4_bmap(), and ext4_readahead()
(preserving lockless fast paths on converted inodes and preserving
readahead for sparse files extended from empty inodes), and check
EXT4_STATE_MAY_INLINE_DATA alongside ext4_has_inline_data() in
ext4_iomap_begin_report() and ext4_read_folio().

Signed-off-by: Arbab Haider <arbabhaider649@gmail.com>
---
 fs/ext4/file.c     | 73 ++++++++++++++++++++++++++++++++++++----------
 fs/ext4/inline.c   | 14 ++++++++-
 fs/ext4/inode.c    | 53 ++++++++++++++++++++++++---------
 fs/ext4/readpage.c | 15 ++++++++--
 4 files changed, 121 insertions(+), 34 deletions(-)

diff --git a/fs/ext4/file.c b/fs/ext4/file.c
index cfb22d0d5..81412b38e 100644
--- a/fs/ext4/file.c
+++ b/fs/ext4/file.c
@@ -70,6 +70,7 @@ static ssize_t ext4_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
 {
 	ssize_t ret;
 	struct inode *inode = file_inode(iocb->ki_filp);
+	bool use_dio, check_inline;
 
 	if (iocb->ki_flags & IOCB_NOWAIT) {
 		if (!inode_trylock_shared(inode))
@@ -78,7 +79,23 @@ static ssize_t ext4_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
 		inode_lock_shared(inode);
 	}
 
-	if (!ext4_should_use_dio(iocb, to)) {
+	check_inline = ext4_has_inline_data(inode) ||
+		ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
+	if (check_inline) {
+		if (iocb->ki_flags & IOCB_NOWAIT) {
+			if (!down_read_trylock(&EXT4_I(inode)->xattr_sem)) {
+				inode_unlock_shared(inode);
+				return -EAGAIN;
+			}
+		} else {
+			down_read(&EXT4_I(inode)->xattr_sem);
+		}
+	}
+	use_dio = ext4_should_use_dio(iocb, to);
+	if (check_inline)
+		up_read(&EXT4_I(inode)->xattr_sem);
+
+	if (!use_dio) {
 		inode_unlock_shared(inode);
 		/*
 		 * Fallback to buffered I/O if the operation being performed on
@@ -633,22 +650,46 @@ static ssize_t ext4_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
 	}
 
 	/* Fallback to buffered I/O if the inode does not support direct I/O. */
-	if (!ext4_should_use_dio(iocb, from)) {
-		if (ilock_shared)
-			inode_unlock_shared(inode);
-		else
-			inode_unlock(inode);
-		return ext4_buffered_write_iter(iocb, from);
-	}
+	{
+		bool check_inline = ext4_has_inline_data(inode) ||
+			ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
+
+		if (check_inline) {
+			if (iocb->ki_flags & IOCB_NOWAIT) {
+				if (!down_read_trylock(&EXT4_I(inode)->xattr_sem)) {
+					if (ilock_shared)
+						inode_unlock_shared(inode);
+					else
+						inode_unlock(inode);
+					return -EAGAIN;
+				}
+			} else {
+				down_read(&EXT4_I(inode)->xattr_sem);
+			}
+		}
+		if (!ext4_should_use_dio(iocb, from)) {
+			if (check_inline)
+				up_read(&EXT4_I(inode)->xattr_sem);
+			if (ilock_shared)
+				inode_unlock_shared(inode);
+			else
+				inode_unlock(inode);
+			return ext4_buffered_write_iter(iocb, from);
+		}
 
-	/*
-	 * Prevent inline data from being created since we are going to allocate
-	 * blocks for DIO. We know the inode does not currently have inline data
-	 * because ext4_should_use_dio() checked for it, but we have to clear
-	 * the state flag before the write checks because a lock cycle could
-	 * introduce races with other writers.
-	 */
-	ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
+		/*
+		 * Prevent inline data from being created since we are going to
+		 * allocate blocks for DIO. We know the inode does not currently
+		 * have inline data because ext4_should_use_dio() checked for it
+		 * under xattr_sem, but we have to clear the state flag before
+		 * releasing xattr_sem and running the write checks because a
+		 * lock cycle could introduce races with other writers or inline
+		 * data rollback.
+		 */
+		ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
+		if (check_inline)
+			up_read(&EXT4_I(inode)->xattr_sem);
+	}
 
 	ret = ext4_dio_write_checks(iocb, from, &ilock_shared, &extend,
 				    &dio_flags);
diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
index df50b0483..9270a2f4c 100644
--- a/fs/ext4/inline.c
+++ b/fs/ext4/inline.c
@@ -428,6 +428,10 @@ static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
 		return -ENOSPC;
 
 	ext4_write_lock_xattr(inode, &no_expand);
+	if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
+		ext4_write_unlock_xattr(inode, &no_expand);
+		return -ENOSPC;
+	}
 	/*
 	 * ei->i_inline_size may have changed since the initial check
 	 * if other xattrs were added. Recalculate to ensure
@@ -507,7 +511,8 @@ static int ext4_destroy_inline_data_nolock(handle_t *handle,
 
 	EXT4_I(inode)->i_inline_off = 0;
 	EXT4_I(inode)->i_inline_size = 0;
-	ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
+	if (error)
+		ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
 out:
 	brelse(is.iloc.bh);
 	if (error == -ENODATA)
@@ -595,14 +600,17 @@ static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
 	unsigned from, to;
 	struct ext4_iloc iloc;
 
+	down_read(&EXT4_I(inode)->xattr_sem);
 	if (!ext4_has_inline_data(inode)) {
 		/*
 		 * clear the flag so that no new write
 		 * will trap here again.
 		 */
 		ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
+		up_read(&EXT4_I(inode)->xattr_sem);
 		return 0;
 	}
+	up_read(&EXT4_I(inode)->xattr_sem);
 
 	needed_blocks = ext4_chunk_trans_extent(inode, 1);
 
@@ -647,6 +655,7 @@ static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
 	ret = ext4_destroy_inline_data_nolock(handle, inode);
 	if (ret)
 		goto out;
+	ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
 
 	if (ext4_should_dioread_nolock(inode)) {
 		ret = ext4_block_write_begin(handle, folio, from, to,
@@ -1114,6 +1123,7 @@ static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
 		ext4_msg(inode->i_sb, KERN_EMERG,
 			"error restoring inline_data for inode -- potential data loss! (inode %llu, error %d)",
 			inode->i_ino, ret);
+		ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
 		return;
 	}
 	ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
@@ -1214,6 +1224,8 @@ static int ext4_convert_inline_data_nolock(handle_t *handle,
 out_restore:
 	if (error)
 		ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
+	else
+		ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
 
 out:
 	brelse(data_bh);
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index f27aeae02..f5d0d7939 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -3613,8 +3613,16 @@ static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
 	/*
 	 * We can get here for an inline file via the FIBMAP ioctl
 	 */
-	if (ext4_has_inline_data(inode))
-		goto out;
+	if (ext4_has_inline_data(inode) ||
+	    ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
+		bool has_inline;
+
+		down_read(&EXT4_I(inode)->xattr_sem);
+		has_inline = ext4_has_inline_data(inode);
+		up_read(&EXT4_I(inode)->xattr_sem);
+		if (has_inline)
+			goto out;
+	}
 
 	if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
 	    (test_opt(inode->i_sb, DELALLOC) ||
@@ -4117,7 +4125,8 @@ static int ext4_iomap_begin_report(struct inode *inode, loff_t offset,
 	if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
 		return -EINVAL;
 
-	if (ext4_has_inline_data(inode)) {
+	if (ext4_has_inline_data(inode) ||
+	    ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
 		ret = ext4_inline_data_iomap(inode, iomap);
 		if (ret != -EAGAIN) {
 			if (ret == 0 && offset >= iomap->length)
@@ -5104,13 +5113,21 @@ int ext4_block_zero_eof(struct inode *inode, loff_t from, loff_t end)
 	if (!offset || from >= end)
 		return 0;
 	/*
-	 * Inline data has no tail block to zero out.  Note that a race with
-	 * ext4_page_mkwrite() converting inline data to an extent without
-	 * holding i_rwsem is safe, as that path zeroes the full block before
-	 * copying in the inline data.
+	 * Inline data has no tail block to zero out.  Serialize against a
+	 * concurrent ext4_page_mkwrite() inline data conversion so we do not
+	 * observe transiently cleared EXT4_INODE_INLINE_DATA before block 0 is
+	 * initialized or rolled back.
 	 */
-	if (ext4_has_inline_data(inode))
-		return 0;
+	if (ext4_has_inline_data(inode) ||
+	    ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
+		bool has_inline;
+
+		down_read(&EXT4_I(inode)->xattr_sem);
+		has_inline = ext4_has_inline_data(inode);
+		up_read(&EXT4_I(inode)->xattr_sem);
+		if (has_inline)
+			return 0;
+	}
 	/* If we are processing an encrypted inode during orphan list handling */
 	if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode))
 		return 0;
@@ -7246,11 +7263,19 @@ int ext4_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
 		 * convert to extent-based storage first to prevent inconsistent
 		 * state (inline flag set but size exceeds inline capacity).
 		 */
-		if (ext4_has_inline_data(inode) &&
-		    attr->ia_size > EXT4_I(inode)->i_inline_size) {
-			error = ext4_convert_inline_data(inode);
-			if (error)
-				return error;
+		if (ext4_has_inline_data(inode) ||
+		    ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
+			bool convert_inline;
+
+			down_read(&EXT4_I(inode)->xattr_sem);
+			convert_inline = ext4_has_inline_data(inode) &&
+				attr->ia_size > EXT4_I(inode)->i_inline_size;
+			up_read(&EXT4_I(inode)->xattr_sem);
+			if (convert_inline) {
+				error = ext4_convert_inline_data(inode);
+				if (error)
+					return error;
+			}
 		}
 
 		if (shrink) {
diff --git a/fs/ext4/readpage.c b/fs/ext4/readpage.c
index c7b6cdb2e..4ebe985c6 100644
--- a/fs/ext4/readpage.c
+++ b/fs/ext4/readpage.c
@@ -326,7 +326,8 @@ int ext4_read_folio(struct file *file, struct folio *folio)
 
 	trace_ext4_read_folio(inode, folio);
 
-	if (ext4_has_inline_data(inode)) {
+	if (ext4_has_inline_data(inode) ||
+	    ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
 		ret = ext4_readpage_inline(inode, folio);
 		if (ret != -EAGAIN)
 			return ret;
@@ -345,8 +346,16 @@ void ext4_readahead(struct readahead_control *rac)
 	struct fsverity_info *vi = NULL;
 
 	/* If the file has inline data, no need to do readahead. */
-	if (ext4_has_inline_data(inode))
-		return;
+	if (ext4_has_inline_data(inode) ||
+	    ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
+		bool has_inline;
+
+		down_read(&EXT4_I(inode)->xattr_sem);
+		has_inline = ext4_has_inline_data(inode);
+		up_read(&EXT4_I(inode)->xattr_sem);
+		if (has_inline)
+			return;
+	}
 
 	if (readahead_index(rac) < DIV_ROUND_UP(inode->i_size, PAGE_SIZE))
 		vi = fsverity_get_info(inode);
-- 
2.53.0


  parent reply	other threads:[~2026-11-26  7:08 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  7:07 [PATCH v4 0/5] ext4: fix inline data conversion races 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 ` [PATCH v4 2/5] ext4: fix delayed-allocation inline data conversion races Arbab Haider
2026-11-26  7:07 ` Arbab Haider [this message]
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
2026-11-26  7:32 ` [PATCH v4 0/5] ext4: fix inline data conversion races sashiko-bot

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-5-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=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 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.