Skip to contentSkip to conversation

ext4: fix concurrent fast commit durability and tracking races

PATCH In review Xing Ma 3 participants 8 messages Nov 30 – Dec 1

Subsystems (3)
lore
Share & tools
lore

Xing Ma

author
#0 lore raw

v2 cover ext4: fix concurrent fast commit durability and tracking races

Under concurrent writers and fsync() workloads (reproducible with xfstests generic/455 and MKFS_OPTIONS="-O fast_commit"), ext4 fast commit has three concurrency races that lose fsync()'d updates or leave extents inconsistent after crash recovery:

1. __jbd2_fc_end_commit() and jbd2_journal_commit_transaction() invoke ext4_fc_cleanup() before clearing JBD2_FAST_COMMIT_ONGOING / JBD2_FULL_COMMIT_ONGOING, letting concurrent updates land on FC_Q_STAGING after cleanup drains it and remain stranded during the next fast commit.

2. ext4_fc_commit() skips committing when an already-ongoing fast commit increments s_fc_subtid, even though the waiting caller's updates were queued on FC_Q_STAGING or marked EXT4_STATE_FC_REQUEUE and were not included in that commit.

3. ext4_fc_track_{inode,range,create,link,unlink}() and ext4_rename() skip tracking whenever EXT4_MF_FC_INELIGIBLE is set without comparing handle->h_transaction->t_tid against s_fc_ineligible_tid, dropping tracking for transaction T + 1 while transaction T finishes its full commit; ext4_fc_cleanup() also cleared EXT4_MF_FC_INELIGIBLE when full == 0.

Changes since v1 (addressing Sashiko AI review):
- Patch 1 (was Patch 2 in v1): Instead of splicing FC_Q_STAGING in ext4_fc_perform_commit() Step 1 (which could invert dentry order or split concurrent handles), fix the root window directly by clearing JBD2_FULL_COMMIT_ONGOING / JBD2_FAST_COMMIT_ONGOING inside ext4_fc_cleanup() while still holding s_fc_lock (and reading j_flags under j_state_lock in ext4_fc_track_template() / __track_dentry_update()). Fix Fixes: tag to bdc8a53a6f2f.
- Patch 2 (was Patch 1 in v1): Increment s_fc_subtid inside ext4_fc_cleanup() under the same write_lock(&journal->j_state_lock) that clears JBD2_FAST_COMMIT_ONGOING so ext4_fc_commit() never double-counts target_subtid during cleanup.
- Patch 3: Export ext4_fc_handle_eligible() and use it in ext4_rename() to avoid torn rename tracking; serialize the slow-path check under s_fc_lock so a cleared s_fc_ineligible_tid == 0 is never passed to tid_gt(); inline ext4_fc_eligible() into ext4_fc_eligible_test() to avoid -Wunused-function when CONFIG_EXT4_KUNIT_TESTS=n.

Testing on tytso/ext4.git dev (aceadbccd428f):
- W=1 build clean with both CONFIG_EXT4_KUNIT_TESTS=y and =n.
- KUnit ext4_fast_commit_test: 11/11 PASS, 0 lockdep warnings.
- xfstests generic/455 (-O fast_commit): 7/26 FAIL (~27%) before; PASS after (0 dmesg / lockdep / e2fsck errors).

Xing Ma (3):
jbd2, ext4: clear commit ongoing flags under s_fc_lock in

    ext4_fc_cleanup()

ext4: do not skip fast commit for updates queued during ongoing commit
ext4: keep tracking newer transactions while older tid is
fc-ineligible

 fs/ext4/ext4.h        |  2 ++
 fs/ext4/fast_commit.c | 95 +++++++++++++++++++++++++++++++++++++++----
 fs/ext4/namei.c       |  4 +-
 fs/jbd2/commit.c      | 15 ++++---
 fs/jbd2/journal.c     | 17 +++++---
 include/linux/jbd2.h  |  5 ++-
 6 files changed, 115 insertions(+), 23 deletions(-)

base-commit: aceadbccd428fb8e6593b27a3091d961f80225d1

Signature · 1 line
2.39.5
author
#1 lore raw

PATCH v2 1/3 jbd2, ext4: clear commit ongoing flags under s_fc_lock in ext4_fc_cleanup()

In both __jbd2_fc_end_commit() and jbd2_journal_commit_transaction(), j_fc_cleanup_callback (ext4_fc_cleanup()) splices FC_Q_STAGING into FC_Q_MAIN and releases s_fc_lock BEFORE JBD2_FAST_COMMIT_ONGOING / JBD2_FULL_COMMIT_ONGOING is cleared under j_state_lock.

If a concurrent writer calls ext4_fc_track_template() or __track_dentry_update() in that window, it still observes the ONGOING flag set and inserts its inode or dentry update onto FC_Q_STAGING after ext4_fc_cleanup() has already drained it. That update then stays stranded on FC_Q_STAGING across the next fast commit, silently losing fsync()'d changes after a crash.

Clear JBD2_FULL_COMMIT_ONGOING / JBD2_FAST_COMMIT_ONGOING under journal->j_state_lock inside ext4_fc_cleanup() while still holding s_fc_lock (the lock order s_fc_lock -> j_state_lock is safe because s_fc_lock is a mutex and j_state_lock is an rwlock). In __jbd2_fc_end_commit(), set JBD2_FULL_COMMIT_ONGOING before invoking j_fc_cleanup_callback() on fallback so writers keep routing to FC_Q_STAGING without a gap. In jbd2_journal_commit_transaction(), finish dropping commit_transaction before calling j_fc_cleanup_callback() and leave flag clearing to the callback when registered. Also read j_flags under j_state_lock in ext4_fc_track_template() and __track_dentry_update() so queue selection synchronizes with the flag update.

Fixes bdc8a53a6f2f ("ext4: fast commit may miss file actions")
---
 fs/ext4/fast_commit.c | 34 ++++++++++++++++++++++++++++------
 fs/jbd2/commit.c      | 15 ++++++++++-----
 fs/jbd2/journal.c     | 17 +++++++++++------
 include/linux/jbd2.h  |  5 ++++-
 4 files changed, 53 insertions(+), 18 deletions(-)
fs/ext4/fast_commit.c +28 -6
index 9beb423b208f8..ecc5fee9f6b54 100644
@@ -388,6 +388,17 @@ void ext4_fc_mark_ineligible(struct super_block *sb, int reason, handle_t *handl
  *
  * If enqueue is set, this function enqueues the inode in fast commit list.
  */
+static bool ext4_fc_commit_ongoing(journal_t *journal)
+{
+	bool ongoing;
+
+	read_lock(&journal->j_state_lock);
+	ongoing = journal->j_flags &
+		(JBD2_FULL_COMMIT_ONGOING | JBD2_FAST_COMMIT_ONGOING);
+	read_unlock(&journal->j_state_lock);
+	return ongoing;
+}
+
 static int ext4_fc_track_template(
 	handle_t *handle, struct inode *inode,
 	int (*__fc_track_fn)(handle_t *handle, struct inode *, void *, bool),
@@ -418,10 +429,9 @@ static int ext4_fc_track_template(
 	alloc_ctx = ext4_fc_lock(inode->i_sb);
 	if (list_empty(&EXT4_I(inode)->i_fc_list))
 		list_add_tail(&EXT4_I(inode)->i_fc_list,
-				(sbi->s_journal->j_flags & JBD2_FULL_COMMIT_ONGOING ||
-				 sbi->s_journal->j_flags & JBD2_FAST_COMMIT_ONGOING) ?
-				&sbi->s_fc_q[FC_Q_STAGING] :
-				&sbi->s_fc_q[FC_Q_MAIN]);
+			      ext4_fc_commit_ongoing(sbi->s_journal) ?
+			      &sbi->s_fc_q[FC_Q_STAGING] :
+			      &sbi->s_fc_q[FC_Q_MAIN]);
 	ext4_fc_unlock(inode->i_sb, alloc_ctx);
 
 	return ret;
@@ -469,8 +479,7 @@ static int __track_dentry_update(handle_t *handle, struct inode *inode,
 	INIT_LIST_HEAD(&node->fcd_dilist);
 	INIT_LIST_HEAD(&node->fcd_list);
 	alloc_ctx = ext4_fc_lock(sb);
-	if (sbi->s_journal->j_flags & JBD2_FULL_COMMIT_ONGOING ||
-		sbi->s_journal->j_flags & JBD2_FAST_COMMIT_ONGOING)
+	if (ext4_fc_commit_ongoing(sbi->s_journal))
 		list_add_tail(&node->fcd_list,
 				&sbi->s_fc_dentry_q[FC_Q_STAGING]);
 	else
@@ -1770,6 +1779,19 @@ static void ext4_fc_cleanup(journal_t *journal, int full, tid_t tid)
 
 	if (full)
 		sbi->s_fc_bytes = 0;
+
+	/*
+	 * Clear the ongoing commit flag while still holding s_fc_lock so no
+	 * concurrent tracking call can observe the flag set and land on
+	 * FC_Q_STAGING after the splice above has already drained it.
+	 */
+	write_lock(&journal->j_state_lock);
+	if (full)
+		journal->j_flags &= ~JBD2_FULL_COMMIT_ONGOING;
+	else
+		journal->j_flags &= ~JBD2_FAST_COMMIT_ONGOING;
+	write_unlock(&journal->j_state_lock);
+
 	ext4_fc_unlock(sb, alloc_ctx);
 	trace_ext4_fc_stats(sb);
 }
fs/jbd2/commit.c +10 -5
index 00a5a6b369ed5..e2e67164e52e7 100644
@@ -398,7 +398,7 @@ void jbd2_journal_commit_transaction(journal_t *journal)
 	struct blk_plug plug;
 	/* Tail of the journal */
 	unsigned long first_block;
-	tid_t first_tid;
+	tid_t first_tid, commit_tid;
 	int update_tail;
 	int csum_size = 0;
 	LIST_HEAD(io_bufs);
@@ -1152,16 +1152,17 @@ void jbd2_journal_commit_transaction(journal_t *journal)
 
 	if (journal->j_commit_callback)
 		journal->j_commit_callback(journal, commit_transaction);
-	if (journal->j_fc_cleanup_callback)
-		journal->j_fc_cleanup_callback(journal, 1, commit_transaction->t_tid);
 
 	trace_jbd2_end_commit(journal, commit_transaction);
 	jbd2_debug(1, "JBD2: commit %d complete, head %d\n",
 		  journal->j_commit_sequence, journal->j_tail_sequence);
 
+	commit_tid = commit_transaction->t_tid;
 	write_lock(&journal->j_state_lock);
-	journal->j_flags &= ~JBD2_FULL_COMMIT_ONGOING;
-	journal->j_flags &= ~JBD2_FAST_COMMIT_ONGOING;
+	if (!journal->j_fc_cleanup_callback) {
+		journal->j_flags &= ~JBD2_FULL_COMMIT_ONGOING;
+		journal->j_flags &= ~JBD2_FAST_COMMIT_ONGOING;
+	}
 	spin_lock(&journal->j_list_lock);
 	commit_transaction->t_state = T_FINISHED;
 	/* Check if the transaction can be dropped now that we are finished */
@@ -1171,6 +1172,10 @@ void jbd2_journal_commit_transaction(journal_t *journal)
 	}
 	spin_unlock(&journal->j_list_lock);
 	write_unlock(&journal->j_state_lock);
+
+	if (journal->j_fc_cleanup_callback)
+		journal->j_fc_cleanup_callback(journal, 1, commit_tid);
+
 	wake_up(&journal->j_wait_done_commit);
 	wake_up(&journal->j_fc_wait);
 
fs/jbd2/journal.c +11 -6
index eb8e944f1f9e6..c9f99a55839b7 100644
@@ -754,13 +754,18 @@ EXPORT_SYMBOL(jbd2_fc_begin_commit);
  */
 static int __jbd2_fc_end_commit(journal_t *journal, tid_t tid, bool fallback)
 {
-	if (journal->j_fc_cleanup_callback)
-		journal->j_fc_cleanup_callback(journal, 0, tid);
-	write_lock(&journal->j_state_lock);
-	journal->j_flags &= ~JBD2_FAST_COMMIT_ONGOING;
-	if (fallback)
+	if (fallback) {
+		write_lock(&journal->j_state_lock);
 		journal->j_flags |= JBD2_FULL_COMMIT_ONGOING;
-	write_unlock(&journal->j_state_lock);
+		write_unlock(&journal->j_state_lock);
+	}
+	if (journal->j_fc_cleanup_callback) {
+		journal->j_fc_cleanup_callback(journal, 0, tid);
+	} else {
+		write_lock(&journal->j_state_lock);
+		journal->j_flags &= ~JBD2_FAST_COMMIT_ONGOING;
+		write_unlock(&journal->j_state_lock);
+	}
 	wake_up(&journal->j_fc_wait);
 	if (fallback)
 		return jbd2_complete_transaction(journal, tid);
include/linux/jbd2.h +4 -1
index 69f12366a09a8..3fe1ade505dad 100644
@@ -1287,7 +1287,10 @@ struct journal_s
 	 * @j_fc_cleanup_callback:
 	 *
 	 * Clean-up after fast commit or full commit. JBD2 calls this function
-	 * after every commit operation.
+	 * after every commit operation. When set, the callback is responsible
+	 * for clearing JBD2_FULL_COMMIT_ONGOING (if @full is non-zero) or
+	 * JBD2_FAST_COMMIT_ONGOING (if @full is zero) under @j_state_lock once
+	 * queue cleanup and staging-queue splicing have completed.
 	 */
 	void (*j_fc_cleanup_callback)(struct journal_s *journal, int full, tid_t tid);
 
Signature · 1 line
2.39.5
reviewer Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] jbd2, ext4: clear commit ongoing flags under s_fc_lock in ext4_fc_cleanup()

Quoted 25 lines · Xing Ma

In both __jbd2_fc_end_commit() and jbd2_journal_commit_transaction(), j_fc_cleanup_callback (ext4_fc_cleanup()) splices FC_Q_STAGING into FC_Q_MAIN and releases s_fc_lock BEFORE JBD2_FAST_COMMIT_ONGOING / JBD2_FULL_COMMIT_ONGOING is cleared under j_state_lock.

If a concurrent writer calls ext4_fc_track_template() or __track_dentry_update() in that window, it still observes the ONGOING flag set and inserts its inode or dentry update onto FC_Q_STAGING after ext4_fc_cleanup() has already drained it. That update then stays stranded on FC_Q_STAGING across the next fast commit, silently losing fsync()'d changes after a crash.

Clear JBD2_FULL_COMMIT_ONGOING / JBD2_FAST_COMMIT_ONGOING under journal->j_state_lock inside ext4_fc_cleanup() while still holding s_fc_lock (the lock order s_fc_lock -> j_state_lock is safe because s_fc_lock is a mutex and j_state_lock is an rwlock). In __jbd2_fc_end_commit(), set JBD2_FULL_COMMIT_ONGOING before invoking j_fc_cleanup_callback() on fallback so writers keep routing to FC_Q_STAGING without a gap. In jbd2_journal_commit_transaction(), finish dropping commit_transaction before calling j_fc_cleanup_callback() and leave flag clearing to the callback when registered. Also read j_flags under j_state_lock in ext4_fc_track_template() and __track_dentry_update() so queue selection synchronizes with the flag update.

Quoted 3 lines
Fixes bdc8a53a6f2f ("ext4: fast commit may miss file actions")

The fix looks good. Feel free to add:

Just two nits below:

fs/ext4/fast_commit.c void ext4_fc_mark_ineligible(struct super_block *sb, int reason, handle_t *handl · line 400

Quoted 10 lines
fs/ext4/fast_commit.c +10 -0
+static bool ext4_fc_commit_ongoing(journal_t *journal)
+{
+	bool ongoing;
+
+	read_lock(&journal->j_state_lock);
+	ongoing = journal->j_flags &
+		(JBD2_FULL_COMMIT_ONGOING | JBD2_FAST_COMMIT_ONGOING);
+	read_unlock(&journal->j_state_lock);
+	return ongoing;
+}

This should be jbd2 helper rather than ext4 since the flags belong to the journal.

Quoted 6 lines
fs/ext4/fast_commit.c +2 -0
@@ -1770,6 +1779,19 @@ static void ext4_fc_cleanup(journal_t *journal, int full, tid_t tid)
 
 	if (full)
 		sbi->s_fc_bytes = 0;
+
+	/*

fs/ext4/fast_commit.c static void ext4_fc_cleanup(journal_t *journal, int full, tid_t tid) · line 1793

Quoted 10 lines
fs/ext4/fast_commit.c +10 -0
+	 * Clear the ongoing commit flag while still holding s_fc_lock so no
+	 * concurrent tracking call can observe the flag set and land on
+	 * FC_Q_STAGING after the splice above has already drained it.
+	 */
+	write_lock(&journal->j_state_lock);
+	if (full)
+		journal->j_flags &= ~JBD2_FULL_COMMIT_ONGOING;
+	else
+		journal->j_flags &= ~JBD2_FAST_COMMIT_ONGOING;
+	write_unlock(&journal->j_state_lock);

And this belongs into some jdb2 helper as well to maintain proper layering. Ext4 should not directly modify jbd2 flags...

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author
#3 lore raw

PATCH v2 2/3 ext4: do not skip fast commit for updates queued during ongoing commit

In reply to Xing Ma

When ext4_fc_commit() waits on an already-ongoing fast commit (T1) and jbd2_fc_begin_commit() returns -EALREADY, it currently skips restarting whenever sbi->s_fc_subtid has incremented past the value read at entry.

However, once T1 sets JBD2_FAST_COMMIT_ONGOING, any concurrent update made by another thread (T2) is placed on FC_Q_STAGING or marked EXT4_STATE_FC_REQUEUE and will NOT be written by T1. When T2 then calls fsync() while T1 is still running:

1. T2 reads s_fc_subtid == N and waits in jbd2_fc_begin_commit().
2. T1 finishes committing its own earlier snapshot, increments s_fc_subtid to N + 1, moves T2's updates from FC_Q_STAGING to FC_Q_MAIN in ext4_fc_cleanup(), and wakes T2.
3. T2 sees s_fc_subtid (N + 1) > subtid (N), skips committing (EXT4_FC_STATUS_SKIPPED), and returns 0 from fsync().

T2's fsync() thus returns success even though its updates are still uncommitted in memory, causing silent data loss or stale file size after a crash. This is readily reproduced by concurrent fsx writers in xfstests generic/455 with -O fast_commit.

Sample s_fc_subtid and JBD2_FAST_COMMIT_ONGOING under j_state_lock at entry to ext4_fc_commit(). If a fast commit is already ongoing, require target_subtid = s_fc_subtid + 2 (a subsequent fast commit started after the ongoing one finishes) or a full commit of commit_tid before skipping. Increment s_fc_subtid in ext4_fc_cleanup() under the same write_lock(&journal->j_state_lock) that clears JBD2_FAST_COMMIT_ONGOING so readers never observe the incremented subtid while the old ongoing flag is still set.

Fixes aa75f4d3daae ("ext4: main fast-commit commit path")
---
 fs/ext4/ext4.h        |  1 +
 fs/ext4/fast_commit.c | 28 +++++++++++++++++++++++-----
 2 files changed, 24 insertions(+), 5 deletions(-)
fs/ext4/ext4.h +1 -0
index dc2c181337a16..fb595277f2c1c 100644
@@ -1860,6 +1860,7 @@ struct ext4_sb_info {
 
 	/* Ext4 fast commit sub transaction ID */
 	atomic_t s_fc_subtid;
+	bool s_fc_commit_succeeded;
 
 	/*
 	 * After commit starts, the main queue gets locked, and the further
fs/ext4/fast_commit.c +23 -5
index ecc5fee9f6b54..f02dca2c9a3a1 100644
@@ -1597,7 +1597,7 @@ int ext4_fc_commit(journal_t *journal, tid_t commit_tid)
 	struct super_block *sb = journal->j_private;
 	struct ext4_sb_info *sbi = EXT4_SB(sb);
 	int nblks = 0, ret, bsize = journal->j_blocksize;
-	int subtid = atomic_read(&sbi->s_fc_subtid);
+	int target_subtid;
 	int status = EXT4_FC_STATUS_OK, fc_bufs_before = 0;
 	ktime_t start_time, commit_time;
 	int old_ioprio, journal_ioprio;
@@ -1610,11 +1610,24 @@ int ext4_fc_commit(journal_t *journal, tid_t commit_tid)
 	start_time = ktime_get();
 	old_ioprio = get_current_ioprio();
 
+	/*
+	 * If a fast commit is already ongoing when we enter ext4_fc_commit(),
+	 * updates made by this caller may have been queued on FC_Q_STAGING or
+	 * marked EXT4_STATE_FC_REQUEUE and will not be included in that
+	 * ongoing fast commit. Require a subsequent fast commit (or a full
+	 * commit of commit_tid) before skipping.
+	 */
+	read_lock(&journal->j_state_lock);
+	target_subtid = atomic_read(&sbi->s_fc_subtid) + 1;
+	if (journal->j_flags & JBD2_FAST_COMMIT_ONGOING)
+		target_subtid++;
+	read_unlock(&journal->j_state_lock);
+
 restart_fc:
 	ret = jbd2_fc_begin_commit(journal, commit_tid);
 	if (ret == -EALREADY) {
 		/* There was an ongoing commit, check if we need to restart */
-		if (atomic_read(&sbi->s_fc_subtid) <= subtid &&
+		if ((int)(atomic_read(&sbi->s_fc_subtid) - target_subtid) < 0 &&
 		    tid_gt(commit_tid, journal->j_commit_sequence))
 			goto restart_fc;
 		ext4_fc_update_stats(sb, EXT4_FC_STATUS_SKIPPED, 0, 0,
@@ -1663,7 +1676,7 @@ int ext4_fc_commit(journal_t *journal, tid_t commit_tid)
 		status = EXT4_FC_STATUS_FAILED;
 		goto fallback;
 	}
-	atomic_inc(&sbi->s_fc_subtid);
+	sbi->s_fc_commit_succeeded = true;
 	ret = jbd2_fc_end_commit(journal);
 	set_task_ioprio(current, old_ioprio);
 	/*
@@ -1786,10 +1799,15 @@ static void ext4_fc_cleanup(journal_t *journal, int full, tid_t tid)
 	 * FC_Q_STAGING after the splice above has already drained it.
 	 */
 	write_lock(&journal->j_state_lock);
-	if (full)
+	if (full) {
 		journal->j_flags &= ~JBD2_FULL_COMMIT_ONGOING;
-	else
+	} else {
+		if (sbi->s_fc_commit_succeeded) {
+			sbi->s_fc_commit_succeeded = false;
+			atomic_inc(&sbi->s_fc_subtid);
+		}
 		journal->j_flags &= ~JBD2_FAST_COMMIT_ONGOING;
+	}
 	write_unlock(&journal->j_state_lock);
 
 	ext4_fc_unlock(sb, alloc_ctx);
Signature · 1 line
2.39.5
reviewer
#4 lore raw

Re: [PATCH v2 2/3] ext4: do not skip fast commit for updates queued during ongoing commit

Quoted 30 lines · Xing Ma

When ext4_fc_commit() waits on an already-ongoing fast commit (T1) and jbd2_fc_begin_commit() returns -EALREADY, it currently skips restarting whenever sbi->s_fc_subtid has incremented past the value read at entry.

However, once T1 sets JBD2_FAST_COMMIT_ONGOING, any concurrent update made by another thread (T2) is placed on FC_Q_STAGING or marked EXT4_STATE_FC_REQUEUE and will NOT be written by T1. When T2 then calls fsync() while T1 is still running:

1. T2 reads s_fc_subtid == N and waits in jbd2_fc_begin_commit().
2. T1 finishes committing its own earlier snapshot, increments s_fc_subtid to N + 1, moves T2's updates from FC_Q_STAGING to FC_Q_MAIN in ext4_fc_cleanup(), and wakes T2.
3. T2 sees s_fc_subtid (N + 1) > subtid (N), skips committing (EXT4_FC_STATUS_SKIPPED), and returns 0 from fsync().

T2's fsync() thus returns success even though its updates are still uncommitted in memory, causing silent data loss or stale file size after a crash. This is readily reproduced by concurrent fsx writers in xfstests generic/455 with -O fast_commit.

Sample s_fc_subtid and JBD2_FAST_COMMIT_ONGOING under j_state_lock at entry to ext4_fc_commit(). If a fast commit is already ongoing, require target_subtid = s_fc_subtid + 2 (a subsequent fast commit started after the ongoing one finishes) or a full commit of commit_tid before skipping. Increment s_fc_subtid in ext4_fc_cleanup() under the same write_lock(&journal->j_state_lock) that clears JBD2_FAST_COMMIT_ONGOING so readers never observe the incremented subtid while the old ongoing flag is still set.

Quoted 3 lines
Fixes aa75f4d3daae ("ext4: main fast-commit commit path")

Thanks for the fix. It is correct but I consider it too ugly. I'd rather move the fastcommit tid tracking into jbd2 (stored in struct journal) and have there something like j_fc_running_tid and j_fc_committed_tid. j_fc_running_tid is incremented as soon as any of the JBD2_*_COMMIT_ONGOING flags are set (and so we start queueing into the staging list), j_fc_committed_tid is incremented once we are clearing these flags. And ext4_fc_commit() can just sample j_fc_running_tid on entry, pass it to jbd2_fc_begin_commit() which can now return -EALREADY only if proper fast commit transaction is committed (i.e., we get rid of the ugly looping in ext4_fc_commit() as well.

Honza

Quoted 85 lines
---
 fs/ext4/ext4.h        |  1 +
 fs/ext4/fast_commit.c | 28 +++++++++++++++++++++++-----
 2 files changed, 24 insertions(+), 5 deletions(-)
fs/ext4/ext4.h +1 -0
index dc2c181337a16..fb595277f2c1c 100644
@@ -1860,6 +1860,7 @@ struct ext4_sb_info {
 
 	/* Ext4 fast commit sub transaction ID */
 	atomic_t s_fc_subtid;
+	bool s_fc_commit_succeeded;
 
 	/*
 	 * After commit starts, the main queue gets locked, and the further
fs/ext4/fast_commit.c +23 -5
index ecc5fee9f6b54..f02dca2c9a3a1 100644
@@ -1597,7 +1597,7 @@ int ext4_fc_commit(journal_t *journal, tid_t commit_tid)
 	struct super_block *sb = journal->j_private;
 	struct ext4_sb_info *sbi = EXT4_SB(sb);
 	int nblks = 0, ret, bsize = journal->j_blocksize;
-	int subtid = atomic_read(&sbi->s_fc_subtid);
+	int target_subtid;
 	int status = EXT4_FC_STATUS_OK, fc_bufs_before = 0;
 	ktime_t start_time, commit_time;
 	int old_ioprio, journal_ioprio;
@@ -1610,11 +1610,24 @@ int ext4_fc_commit(journal_t *journal, tid_t commit_tid)
 	start_time = ktime_get();
 	old_ioprio = get_current_ioprio();
 
+	/*
+	 * If a fast commit is already ongoing when we enter ext4_fc_commit(),
+	 * updates made by this caller may have been queued on FC_Q_STAGING or
+	 * marked EXT4_STATE_FC_REQUEUE and will not be included in that
+	 * ongoing fast commit. Require a subsequent fast commit (or a full
+	 * commit of commit_tid) before skipping.
+	 */
+	read_lock(&journal->j_state_lock);
+	target_subtid = atomic_read(&sbi->s_fc_subtid) + 1;
+	if (journal->j_flags & JBD2_FAST_COMMIT_ONGOING)
+		target_subtid++;
+	read_unlock(&journal->j_state_lock);
+
 restart_fc:
 	ret = jbd2_fc_begin_commit(journal, commit_tid);
 	if (ret == -EALREADY) {
 		/* There was an ongoing commit, check if we need to restart */
-		if (atomic_read(&sbi->s_fc_subtid) <= subtid &&
+		if ((int)(atomic_read(&sbi->s_fc_subtid) - target_subtid) < 0 &&
 		    tid_gt(commit_tid, journal->j_commit_sequence))
 			goto restart_fc;
 		ext4_fc_update_stats(sb, EXT4_FC_STATUS_SKIPPED, 0, 0,
@@ -1663,7 +1676,7 @@ int ext4_fc_commit(journal_t *journal, tid_t commit_tid)
 		status = EXT4_FC_STATUS_FAILED;
 		goto fallback;
 	}
-	atomic_inc(&sbi->s_fc_subtid);
+	sbi->s_fc_commit_succeeded = true;
 	ret = jbd2_fc_end_commit(journal);
 	set_task_ioprio(current, old_ioprio);
 	/*
@@ -1786,10 +1799,15 @@ static void ext4_fc_cleanup(journal_t *journal, int full, tid_t tid)
 	 * FC_Q_STAGING after the splice above has already drained it.
 	 */
 	write_lock(&journal->j_state_lock);
-	if (full)
+	if (full) {
 		journal->j_flags &= ~JBD2_FULL_COMMIT_ONGOING;
-	else
+	} else {
+		if (sbi->s_fc_commit_succeeded) {
+			sbi->s_fc_commit_succeeded = false;
+			atomic_inc(&sbi->s_fc_subtid);
+		}
 		journal->j_flags &= ~JBD2_FAST_COMMIT_ONGOING;
+	}
 	write_unlock(&journal->j_state_lock);
 
 	ext4_fc_unlock(sb, alloc_ctx);
Signature · 1 line
2.39.5
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author
#5 lore raw

PATCH v2 3/3 ext4: keep tracking newer transactions while older tid is fc-ineligible

In reply to Xing Ma

While an ineligible transaction T (with s_fc_ineligible_tid == T) is performing a full JBD2 commit, EXT4_MF_FC_INELIGIBLE remains set until ext4_fc_cleanup(journal, full=1, T) runs at the end of that commit. Meanwhile, JBD2 already allows new handles to run in transaction T + 1.

Because ext4_fc_track_{inode,range,create,link,unlink}() and ext4_rename() check only EXT4_MF_FC_INELIGIBLE without comparing handle->h_transaction->t_tid against s_fc_ineligible_tid, updates in T + 1 silently skip fast commit tracking. Once T's full commit finishes and clears EXT4_MF_FC_INELIGIBLE, a subsequent fsync() in T + 1 proceeds with a fast commit that omits some or all of T + 1's updates:

- If both range and inode tracking were skipped, fsync() writes an empty fast commit and silently loses operations such as truncate.
- If range tracking was skipped before cleanup cleared the flag and inode tracking ran afterward, fast commit replays the shrunk inode without DEL_RANGE, leaving stale extents beyond EOF ("end of extent exceeds allowed value" in e2fsck).

In addition, ext4_fc_cleanup() cleared EXT4_MF_FC_INELIGIBLE even when full == 0, which allows jbd2_fc_end_commit_fallback() to clear the flag before the fallback full commit has even started.

Introduce ext4_fc_handle_eligible() and use it in all fast commit tracking entry points (including ext4_rename()). On the slow path when EXT4_MF_FC_INELIGIBLE is set, re-check under s_fc_lock (serializing with ext4_fc_mark_ineligible() and ext4_fc_cleanup() so a cleared s_fc_ineligible_tid == 0 is never passed to tid_gt()) and continue tracking handles whose TID is newer than s_fc_ineligible_tid. Also inline ext4_fc_eligible() into ext4_fc_eligible_test() to avoid an unused-function warning when CONFIG_EXT4_KUNIT_TESTS=n, and only clear EXT4_MF_FC_INELIGIBLE in ext4_fc_cleanup() when full is set.

Fixes e85c81ba8859 ("ext4: fast commit may not fallback for ineligible commit")
---
 fs/ext4/ext4.h        |  1 +
 fs/ext4/fast_commit.c | 47 ++++++++++++++++++++++++++++++++++---------
 fs/ext4/namei.c       |  4 +---
 3 files changed, 39 insertions(+), 13 deletions(-)
fs/ext4/ext4.h +1 -0
index fb595277f2c1c..f7ecd7925e8e3 100644
@@ -3144,6 +3144,7 @@ void ext4_fc_init(struct super_block *sb, journal_t *journal);
 void ext4_fc_init_inode(struct inode *inode);
 void ext4_fc_track_range(handle_t *handle, struct inode *inode, ext4_lblk_t start,
 			 ext4_lblk_t end);
+bool ext4_fc_handle_eligible(struct super_block *sb, handle_t *handle);
 void __ext4_fc_track_unlink(handle_t *handle, struct inode *inode,
 	struct dentry *dentry);
 void __ext4_fc_track_link(handle_t *handle, struct inode *inode,
fs/ext4/fast_commit.c +37 -10

96 lines not on this page · raw message

fs/ext4/namei.c +1 -3
index b2753daad49f4..85942dde3421a 100644
@@ -3865,9 +3865,7 @@ static int ext4_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
 
 		if (new.inode)
 			ext4_fc_track_unlink(handle, new.dentry);
-		if (test_opt2(sb, JOURNAL_FAST_COMMIT) &&
-		    !(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY) &&
-		    !(ext4_test_mount_flag(sb, EXT4_MF_FC_INELIGIBLE))) {
+		if (ext4_fc_handle_eligible(sb, handle)) {
 			__ext4_fc_track_link(handle, old.inode, new.dentry);
 			__ext4_fc_track_unlink(handle, old.inode, old.dentry);
 			if (whiteout)
Signature · 1 line
2.39.5
reviewer
#6 lore raw

Re: [PATCH v2 3/3] ext4: keep tracking newer transactions while older tid is fc-ineligible

Quoted 33 lines · Xing Ma

While an ineligible transaction T (with s_fc_ineligible_tid == T) is performing a full JBD2 commit, EXT4_MF_FC_INELIGIBLE remains set until ext4_fc_cleanup(journal, full=1, T) runs at the end of that commit. Meanwhile, JBD2 already allows new handles to run in transaction T + 1.

Because ext4_fc_track_{inode,range,create,link,unlink}() and ext4_rename() check only EXT4_MF_FC_INELIGIBLE without comparing handle->h_transaction->t_tid against s_fc_ineligible_tid, updates in T + 1 silently skip fast commit tracking. Once T's full commit finishes and clears EXT4_MF_FC_INELIGIBLE, a subsequent fsync() in T + 1 proceeds with a fast commit that omits some or all of T + 1's updates:

- If both range and inode tracking were skipped, fsync() writes an empty fast commit and silently loses operations such as truncate.
- If range tracking was skipped before cleanup cleared the flag and inode tracking ran afterward, fast commit replays the shrunk inode without DEL_RANGE, leaving stale extents beyond EOF ("end of extent exceeds allowed value" in e2fsck).

In addition, ext4_fc_cleanup() cleared EXT4_MF_FC_INELIGIBLE even when full == 0, which allows jbd2_fc_end_commit_fallback() to clear the flag before the fallback full commit has even started.

Introduce ext4_fc_handle_eligible() and use it in all fast commit tracking entry points (including ext4_rename()). On the slow path when EXT4_MF_FC_INELIGIBLE is set, re-check under s_fc_lock (serializing with ext4_fc_mark_ineligible() and ext4_fc_cleanup() so a cleared s_fc_ineligible_tid == 0 is never passed to tid_gt()) and continue tracking handles whose TID is newer than s_fc_ineligible_tid. Also inline ext4_fc_eligible() into ext4_fc_eligible_test() to avoid an unused-function warning when CONFIG_EXT4_KUNIT_TESTS=n, and only clear EXT4_MF_FC_INELIGIBLE in ext4_fc_cleanup() when full is set.

Quoted 3 lines
Fixes e85c81ba8859 ("ext4: fast commit may not fallback for ineligible commit")

I agree with the sentiment of the fix but I think it would be cleaner if the ineligibility tracking moved to jbd2. As you correctly observed, ineligibility is a property of a transaction so why not just store it there like t_fc_ineligible? jbd2_mark_fc_ineligible(journal) would set it for the running transaction, we never clear it from the transaction (which simplifies matters a lot, in particular jbd2_handle_fc_eligible() can do just a lockless check).

Also I see any case where jbd2_handle_fc_eligible() would be needed without a handle so you can drop that checking as well.

Honza

Quoted 143 lines
---
 fs/ext4/ext4.h        |  1 +
 fs/ext4/fast_commit.c | 47 ++++++++++++++++++++++++++++++++++---------
 fs/ext4/namei.c       |  4 +---
 3 files changed, 39 insertions(+), 13 deletions(-)
fs/ext4/ext4.h +1 -0
index fb595277f2c1c..f7ecd7925e8e3 100644
@@ -3144,6 +3144,7 @@ void ext4_fc_init(struct super_block *sb, journal_t *journal);
 void ext4_fc_init_inode(struct inode *inode);
 void ext4_fc_track_range(handle_t *handle, struct inode *inode, ext4_lblk_t start,
 			 ext4_lblk_t end);
+bool ext4_fc_handle_eligible(struct super_block *sb, handle_t *handle);
 void __ext4_fc_track_unlink(handle_t *handle, struct inode *inode,
 	struct dentry *dentry);
 void __ext4_fc_track_link(handle_t *handle, struct inode *inode,
fs/ext4/fast_commit.c +37 -10
index f02dca2c9a3a1..6af6c1d37e4b4 100644
@@ -229,10 +229,36 @@ static bool ext4_fc_disabled(struct super_block *sb)
 		(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY));
 }
 
-static bool ext4_fc_eligible(struct super_block *sb)
+bool ext4_fc_handle_eligible(struct super_block *sb, handle_t *handle)
 {
-	return !ext4_fc_disabled(sb) &&
-		!(ext4_test_mount_flag(sb, EXT4_MF_FC_INELIGIBLE));
+	struct ext4_sb_info *sbi = EXT4_SB(sb);
+	bool eligible;
+	int alloc_ctx;
+
+	if (ext4_fc_disabled(sb))
+		return false;
+	if (!ext4_test_mount_flag(sb, EXT4_MF_FC_INELIGIBLE))
+		return true;
+	if (!handle || !handle->h_transaction)
+		return false;
+
+	/*
+	 * EXT4_MF_FC_INELIGIBLE may still be set for an older committing
+	 * transaction while new handles are already modifying the next running
+	 * transaction. Re-check under s_fc_lock (which serializes both
+	 * ext4_fc_mark_ineligible() and ext4_fc_cleanup()) so we never compare
+	 * against a cleared s_fc_ineligible_tid == 0, and continue tracking
+	 * handles whose TID is newer than s_fc_ineligible_tid.
+	 */
+	alloc_ctx = ext4_fc_lock(sb);
+	if (!ext4_test_mount_flag(sb, EXT4_MF_FC_INELIGIBLE))
+		eligible = true;
+	else
+		eligible = tid_gt(handle->h_transaction->t_tid,
+				  sbi->s_fc_ineligible_tid);
+	ext4_fc_unlock(sb, alloc_ctx);
+
+	return eligible;
 }
 
 /*
@@ -522,7 +548,7 @@ void ext4_fc_track_unlink(handle_t *handle, struct dentry *dentry)
 {
 	struct inode *inode = d_inode(dentry);
 
-	if (ext4_fc_eligible(inode->i_sb))
+	if (ext4_fc_handle_eligible(inode->i_sb, handle))
 		__ext4_fc_track_unlink(handle, inode, dentry);
 }
 
@@ -543,7 +569,7 @@ void __ext4_fc_track_link(handle_t *handle,
 void ext4_fc_track_link(handle_t *handle, struct inode *inode,
 			struct dentry *dentry)
 {
-	if (ext4_fc_eligible(inode->i_sb))
+	if (ext4_fc_handle_eligible(inode->i_sb, handle))
 		__ext4_fc_track_link(handle, inode, dentry);
 }
 
@@ -565,7 +591,7 @@ void ext4_fc_track_create(handle_t *handle, struct dentry *dentry)
 {
 	struct inode *inode = d_inode(dentry);
 
-	if (ext4_fc_eligible(inode->i_sb))
+	if (ext4_fc_handle_eligible(inode->i_sb, handle))
 		__ext4_fc_track_create(handle, inode, dentry);
 }
 
@@ -594,7 +620,7 @@ void ext4_fc_track_inode(handle_t *handle, struct inode *inode)
 		return;
 	}
 
-	if (!ext4_fc_eligible(inode->i_sb))
+	if (!ext4_fc_handle_eligible(inode->i_sb, handle))
 		return;
 
 	/*
@@ -654,7 +680,7 @@ void ext4_fc_track_range(handle_t *handle, struct inode *inode, ext4_lblk_t star
 	if (S_ISDIR(inode->i_mode))
 		return;
 
-	if (!ext4_fc_eligible(inode->i_sb))
+	if (!ext4_fc_handle_eligible(inode->i_sb, handle))
 		return;
 
 	if (ext4_has_inline_data(inode)) {
@@ -1785,7 +1811,7 @@ static void ext4_fc_cleanup(journal_t *journal, int full, tid_t tid)
 	list_splice_init(&sbi->s_fc_q[FC_Q_STAGING],
 				&sbi->s_fc_q[FC_Q_MAIN]);
 
-	if (tid_geq(tid, sbi->s_fc_ineligible_tid)) {
+	if (full && tid_geq(tid, sbi->s_fc_ineligible_tid)) {
 		sbi->s_fc_ineligible_tid = 0;
 		ext4_clear_mount_flag(sb, EXT4_MF_FC_INELIGIBLE);
 	}
@@ -2860,7 +2886,8 @@ EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_fc_disabled_test);
 
 bool ext4_fc_eligible_test(struct super_block *sb)
 {
-	return ext4_fc_eligible(sb);
+	return !ext4_fc_disabled(sb) &&
+		!(ext4_test_mount_flag(sb, EXT4_MF_FC_INELIGIBLE));
 }
 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_fc_eligible_test);
 
fs/ext4/namei.c +1 -3
index b2753daad49f4..85942dde3421a 100644
@@ -3865,9 +3865,7 @@ static int ext4_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
 
 		if (new.inode)
 			ext4_fc_track_unlink(handle, new.dentry);
-		if (test_opt2(sb, JOURNAL_FAST_COMMIT) &&
-		    !(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY) &&
-		    !(ext4_test_mount_flag(sb, EXT4_MF_FC_INELIGIBLE))) {
+		if (ext4_fc_handle_eligible(sb, handle)) {
 			__ext4_fc_track_link(handle, old.inode, new.dentry);
 			__ext4_fc_track_unlink(handle, old.inode, old.dentry);
 			if (whiteout)
Signature · 1 line
2.39.5
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR