From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.127.231.115 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796060315; b=qdKyohVouMSidGff790K6nznrP5fFygI66dGHLI+KwNnl/52X0bkUHnjmK5zmJ5Lplln SQJ+nv17C3CCq/PWKN+0im6LDR5bcmlLdpyToAct5mfAFaiICKQUbcV3hsVl9If+cWaaW SjNhmqrFUDNQO15HFsD7FslpGIv0Nfeq6W6bHV/TrpBUtDuXiCmz6QRlS3Xh1hLfBQ/WN P6vtvc8T+J0Qg+DVrEV3doOnBciflEYSQeSyWxtl5KeTLagHS33cU2iymG8XHJ7afl137 kk0sGjqOUmAX9MB9ud21JzPJITd6Vz6aKZKdW5rV5glmKriu8iNLGTKIJHmSk58b4Sg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796060315; h=DMARC-Filter:DKIM-Signature:Mime-Version:In-Reply-To: X-Lms-Return-Path:Content-Type:X-Original-From:To:Cc:Date:From: References:Subject:Message-Id:Content-Transfer-Encoding; bh=xDwrhlxRQDI6SgBjsb1XorLVUE2M7O7QxJD/C4bNjuA=; b=ZZjesAxjv3jDfeZfEwD6Ogun/Px4LxaRHCG3y/5vSnURRZz5ftPXOHPxLZ0wbTNQDtkU GwaOIOTkd5xxtt4Eoq1Xwt15NdlmkL6dS69bH+SBxKXgyVpkWpz6A/6PAqhLliqWiODp1 q9ijti2zDNWnqplD1K/0Bf4PPM3mEumCK+YhRQMMcleVadTX3sPDgDeM4l2Mp4GXiZ1nD Cn570rjQKEr6lOypTKXvWleuJAbpQuj0Y8+8a3JBeAKwea3zC8nVvKxgw7gS161qZ3s45 dWwyNwwE7zmbwu+MojhHc5T+O2SJDjGwbNnLNH4nLCmlzxGtD24CrCpnfOno6ld17LQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=2212171451 header.b=NWgtK1yF; arc=none smtp.remote-ip=209.127.231.115 Received: from va-2-115.ptr.blmpb.com (va-2-115.ptr.blmpb.com [209.127.231.115]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 59698606A6B1 for ; Mon, 30 Nov 2026 17:38:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 59698606A6B1 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=2212171451 header.b=NWgtK1yF DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=2212171451; d=bytedance.com; t=1796060309; h=from:subject: mime-version:from:date:message-id:subject:to:cc:reply-to:content-type: mime-version:in-reply-to:message-id; bh=xDwrhlxRQDI6SgBjsb1XorLVUE2M7O7QxJD/C4bNjuA=; b=NWgtK1yFs8qCjw1SduswuO2lU+u2A80k3exve/SaI7e/t65BG47S848Vwk0u0Ac6vSAKxG G380KNF3XHs6JgoycCrjHhxBzMGcc8Zbp0K4Yr3CBzz5JnM/qYZ4Lvzg5hpcqHl+oz5CaT 72xeeoGtGiVD3hCl+jI8A+oAFhxjNQ5e930k1n8ZRFOpFFVYlzkpXFJa3GEuogVASdeWYK BQUsXI924I5k5wl+CjG4nSH/cpgETfjOJKju51NOJZhDdMblgtbFK0KgsEhPuDp3h9XPzZ jJWhnXDNMcyDkYYrpCpwiQg6tGaRm522mJMYo3Q3cdJjvuYWneaw+cnD/7JcsA== X-Mailing-List: linux-ext4@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 In-Reply-To: <20261201013742.1562967-1-maxing.lan@bytedance.com> X-Lms-Return-Path: Content-Type: text/plain; charset=UTF-8 X-Original-From: Xing Ma To: Cc: "Theodore Ts'o" , "Andreas Dilger" , "Jan Kara" , "Baokun Li" , "Ojaswin Mujoo" , "Ritesh Harjani (IBM)" , "Zhang Yi" , "Harshad Shirwadkar" , "Xin Yin" , Date: Tue, 01 Dec 2026 01:37:43 +0800 From: "Xing Ma" References: <20261201013742.1562967-1-maxing.lan@bytedance.com> Subject: [PATCH v2 1/3] jbd2, ext4: clear commit ongoing flags under s_fc_lock in ext4_fc_cleanup() Message-Id: <20261201013742.1562967-2-maxing.lan@bytedance.com> Content-Transfer-Encoding: 7bit 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 --- 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