Linux EXT4 FS development
 help / color / mirror / Atom feed
From: "Xing Ma" <maxing.lan@bytedance.com>
To: <linux-ext4@vger.kernel.org>
Cc: "Theodore Ts'o" <tytso@mit.edu>,
	 "Andreas Dilger" <adilger.kernel@dilger.ca>,
	"Jan Kara" <jack@suse.cz>,
	 "Baokun Li" <libaokun@linux.alibaba.com>,
	 "Ojaswin Mujoo" <ojaswin@linux.ibm.com>,
	 "Ritesh Harjani (IBM)" <ritesh.list@gmail.com>,
	 "Zhang Yi" <yi.zhang@huawei.com>,
	 "Harshad Shirwadkar" <harshadshirwadkar@gmail.com>,
	 "Xin Yin" <yinxin.x@bytedance.com>, <stable@vger.kernel.org>
Subject: [PATCH v2 1/3] jbd2, ext4: clear commit ongoing flags under s_fc_lock in ext4_fc_cleanup()
Date: Tue, 01 Dec 2026 01:37:43 +0800	[thread overview]
Message-ID: <20261201013742.1562967-2-maxing.lan@bytedance.com> (raw)
In-Reply-To: <20261201013742.1562967-1-maxing.lan@bytedance.com>

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")
Cc: stable@vger.kernel.org
Signed-off-by: Xing Ma <maxing.lan@bytedance.com>
---
 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(-)

diff --git a/fs/ext4/fast_commit.c b/fs/ext4/fast_commit.c
index 9beb423b208f8..ecc5fee9f6b54 100644
--- a/fs/ext4/fast_commit.c
+++ b/fs/ext4/fast_commit.c
@@ -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);
 }
diff --git a/fs/jbd2/commit.c b/fs/jbd2/commit.c
index 00a5a6b369ed5..e2e67164e52e7 100644
--- a/fs/jbd2/commit.c
+++ b/fs/jbd2/commit.c
@@ -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);
 
diff --git a/fs/jbd2/journal.c b/fs/jbd2/journal.c
index eb8e944f1f9e6..c9f99a55839b7 100644
--- a/fs/jbd2/journal.c
+++ b/fs/jbd2/journal.c
@@ -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);
diff --git a/include/linux/jbd2.h b/include/linux/jbd2.h
index 69f12366a09a8..3fe1ade505dad 100644
--- a/include/linux/jbd2.h
+++ b/include/linux/jbd2.h
@@ -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);
 
-- 
2.39.5

  reply	other threads:[~2026-11-30 17:38 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 17:37 [PATCH v2 0/3] ext4: fix concurrent fast commit durability and tracking races Xing Ma
2026-11-30 17:37 ` Xing Ma [this message]
2026-11-30 17:37 ` [PATCH v2 2/3] ext4: do not skip fast commit for updates queued during ongoing commit Xing Ma
2026-11-30 17:37 ` [PATCH v2 3/3] ext4: keep tracking newer transactions while older tid is fc-ineligible Xing Ma
2026-11-30 17:52 ` [PATCH v2 0/3] ext4: fix concurrent fast commit durability and tracking 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=20261201013742.1562967-2-maxing.lan@bytedance.com \
    --to=maxing.lan@bytedance.com \
    --cc=adilger.kernel@dilger.ca \
    --cc=harshadshirwadkar@gmail.com \
    --cc=jack@suse.cz \
    --cc=libaokun@linux.alibaba.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=ojaswin@linux.ibm.com \
    --cc=ritesh.list@gmail.com \
    --cc=stable@vger.kernel.org \
    --cc=tytso@mit.edu \
    --cc=yi.zhang@huawei.com \
    --cc=yinxin.x@bytedance.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