From nobody Thu Apr 9 16:24:18 2026 Received: from mail-dl1-f45.google.com (mail-dl1-f45.google.com [74.125.82.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4CD73227B94 for ; Tue, 3 Mar 2026 00:55:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772499319; cv=none; b=s4phNc6W495Q7Akfc1RyLJKhcYlVqbesv2N8zg6iAIov2Kl2TdZsmUDkLJhyaopd8bja1UFB+4wzK3EsHTnUaYarAch1qlauuyf7seifFiTGAnXSVYCMwyL2XJLMWPcw0wgDpDLxM81edCzM3kWHuekE2If8cRU0v/YRCFil27Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772499319; c=relaxed/simple; bh=zbyzfNx8/Pak6zoD/choS8v5s28io8lQmomT+XUdy48=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=C2hXealDSNC/F5IvxIFkPXqWYl5AHVopBoNDksRzBBMcyM0B10v4+G2gZCWrVTGA2PK4oCsD8sN7FzPjWvLqHRqgZV5Pt0dqr5rhzBBwmdjcJEeSq6sUHigSKc1dIVOmKyJN7TVCnG9hTsazgvveuKeGkHJ1+wpUo04UfdQMth4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=U6zxz3O0; arc=none smtp.client-ip=74.125.82.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="U6zxz3O0" Received: by mail-dl1-f45.google.com with SMTP id a92af1059eb24-127380532eeso2212055c88.1 for ; Mon, 02 Mar 2026 16:55:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772499315; x=1773104115; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=cJwawZM7sG51IM4V5SWWW2Cn1eGnHGmPjyUNqQUV4dM=; b=U6zxz3O0Xoo4xgidY5krm1cW9RuWevBQaUrU8aQmDzDH9rRuiRYOdYhG82u7KVUFk/ h28Ymwtz1k3AJgpSxaEeisG9k7UeixaaXjyBUR/QnCuLCKQO6BdVV2l6d+Nh+xgaW1k1 KEg2j4aITZRq72Y33kwpgqiyc3k9Pfffy5ZrStCfbUOtiMBpM3Q+dNP3KVKJIbXO7bx3 FTn2E3GOspTAKeHqWM3+rErdh3o3+Nxl2V4kc/c3WoW99/LbCJ5CppufdBCe7XaxcWGc azLKx9KOKapMsVTopoe6YYrSEFmj1eLoNId3kRVc2Cymt+Gev07pjnkcPphfw7/jock1 0qNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772499315; x=1773104115; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=cJwawZM7sG51IM4V5SWWW2Cn1eGnHGmPjyUNqQUV4dM=; b=ay+kp6fwOeqrTrrnyBb3fzYeGe8TSkf1T0Amuqm0UEBk3p4pu58MRXYiTWDYfo+qaK pfpbmuKRHd4a8CQL1OeclyRC+sg6LTf41yA6OD+ZvrHOt+rfy8xup9n8mjD2NZv4SStg nWugJza1Ang6RVqJ4Ib0OGLqxXn1gGUpfQJXqlhYvJoy4e/oPhI2C3RIQEK2fxoWsjwU Zre5g09Hlu2CR8/w1OImIT6CAl1LuL/rTcG9neup8qWc6pGCnzhY2a299AmOSKgjx56N Z9tQDbOz0GxFYsMA966wwmwtQc/ZyWx3Bdf+KRMdCdMgxCDQdec6tH0WfViOV9IlGVYs yDRw== X-Forwarded-Encrypted: i=1; AJvYcCUFsC22Ra3sBjQ4C+MWzZk+TaXMNbuKdYlgRbPIbUvjv++hrFQgqpJE1eIAuRx8+ptlHj5XWOI6FwvjnYY=@vger.kernel.org X-Gm-Message-State: AOJu0Yypn2I6uz9EPvsb0GguUpfVIISuUgTwOWlOGClnzBoBymQfvgig 3GZRf2BLU8Sj/WDpOIyK8keAZx/rZV/R2akLf/DIEWge+70eNoPTmuPp X-Gm-Gg: ATEYQzwfletqRV4zBX5lTxaS1N7TS+23RJxOy93zaB4Okx/EBCMXxlsbPE6Izv8EtDZ sGTb+kiHsR5XzFrvr+m4eD6X1832zfoJPITFE7HvrtQvNDGuglVzNXpmZ8jlr+ekmIm+wJLixFD Sk6gbPOGKCjHQ9bJ/iHt3fEeUKezEfzpT4kbpMkc7hexYL1KecNHOA/yYi2rdxYJGneZCdqmoFQ TtbmOxHZ+yEhdJsaEGm0BeEqrwABCtJsGmsi+ynLcXks95gVcxbTHYdaOPxLmRPVxTyCWTRu74n pgCWuNr3gwp6QLF23rFpq5l04oLnBY/Bgqw22RPjECm+3JPPXol3btmqT4B+NCUnY+98Zlge9LL XnZXFkYh7GgQjYsrn4v5Bp1WbP95fj5y7BsPr15UJZ+Oqwg5qrYL7P916awkANbEWJT7uwIzFEb VUFeZtby6mbjHjnZFPHKJyHot+iYEhPMYgk9H3/AkTlBRs/Nq99MYMtARqo2HRiuIqvhqmSG8RU 0gfHac+ocn3SkMJlVXmsk7712wijEzrRBq0/6hg8MNBE9w++bGXo5Geba+QtpdlzAvfZCgWjOxM zrF7ALvH3XAbdEN5U+MT4HWEVRfy X-Received: by 2002:a05:7022:911:b0:11b:88a7:e1ac with SMTP id a92af1059eb24-1278fc6beecmr6799560c88.19.1772499315143; Mon, 02 Mar 2026 16:55:15 -0800 (PST) Received: from arch.lan (c-98-51-119-100.hsd1.ca.comcast.net. [98.51.119.100]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1279cbd1993sm6552005c88.2.2026.03.02.16.55.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Mar 2026 16:55:13 -0800 (PST) From: Milos Nikic To: jack@suse.cz Cc: tytso@mit.edu, linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org, Milos Nikic Subject: [PATCH v3 2/2] jbd2: gracefully abort on transaction state corruptions Date: Mon, 2 Mar 2026 16:55:02 -0800 Message-ID: <20260303005502.337108-3-nikic.milos@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260303005502.337108-1-nikic.milos@gmail.com> References: <20260303005502.337108-1-nikic.milos@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Auditing the jbd2 codebase reveals several legacy J_ASSERT calls that enforce internal state machine invariants (e.g., verifying jh->b_transaction or jh->b_next_transaction pointers). When these invariants are broken, the journal is in a corrupted state. However, triggering a fatal panic brings down the entire system for a localized filesystem error. This patch targets a specific class of these asserts: those residing inside functions that natively return integer error codes, booleans, or error pointers. It replaces the hard J_ASSERTs with WARN_ON_ONCE to capture the offending stack trace, safely drops any held locks, gracefully aborts the journal, and returns -EINVAL. This prevents a catastrophic kernel panic while ensuring the corrupted journal state is safely contained and upstream callers (like ext4 or ocfs2) can gracefully handle the aborted handle. Functions modified in fs/jbd2/transaction.c: - jbd2__journal_start() - do_get_write_access() - jbd2_journal_dirty_metadata() - jbd2_journal_forget() - jbd2_journal_try_to_free_buffers() - jbd2_journal_file_inode() Signed-off-by: Milos Nikic --- fs/jbd2/transaction.c | 108 +++++++++++++++++++++++++++++++++--------- 1 file changed, 85 insertions(+), 23 deletions(-) diff --git a/fs/jbd2/transaction.c b/fs/jbd2/transaction.c index 04d17a5f2a82..3cb4e524e0a6 100644 --- a/fs/jbd2/transaction.c +++ b/fs/jbd2/transaction.c @@ -474,7 +474,8 @@ handle_t *jbd2__journal_start(journal_t *journal, int n= blocks, int rsv_blocks, return ERR_PTR(-EROFS); =20 if (handle) { - J_ASSERT(handle->h_transaction->t_journal =3D=3D journal); + if (WARN_ON_ONCE(handle->h_transaction->t_journal !=3D journal)) + return ERR_PTR(-EINVAL); handle->h_ref++; return handle; } @@ -1036,7 +1037,13 @@ do_get_write_access(handle_t *handle, struct journal= _head *jh, */ if (!jh->b_transaction) { JBUFFER_TRACE(jh, "no transaction"); - J_ASSERT_JH(jh, !jh->b_next_transaction); + if (WARN_ON_ONCE(jh->b_next_transaction)) { + spin_unlock(&jh->b_state_lock); + unlock_buffer(bh); + error =3D -EINVAL; + jbd2_journal_abort(journal, error); + goto out; + } JBUFFER_TRACE(jh, "file as BJ_Reserved"); /* * Make sure all stores to jh (b_modified, b_frozen_data) are @@ -1069,13 +1076,27 @@ do_get_write_access(handle_t *handle, struct journa= l_head *jh, */ if (jh->b_frozen_data) { JBUFFER_TRACE(jh, "has frozen data"); - J_ASSERT_JH(jh, jh->b_next_transaction =3D=3D NULL); + if (WARN_ON_ONCE(jh->b_next_transaction)) { + spin_unlock(&jh->b_state_lock); + error =3D -EINVAL; + jbd2_journal_abort(journal, error); + goto out; + } goto attach_next; } =20 JBUFFER_TRACE(jh, "owned by older transaction"); - J_ASSERT_JH(jh, jh->b_next_transaction =3D=3D NULL); - J_ASSERT_JH(jh, jh->b_transaction =3D=3D journal->j_committing_transactio= n); + if (WARN_ON_ONCE(jh->b_next_transaction || + jh->b_transaction !=3D + journal->j_committing_transaction)) { + pr_err("JBD2: %s: assertion failure: b_next_transaction=3D%p b_transacti= on=3D%p j_committing_transaction=3D%p\n", + journal->j_devname, jh->b_next_transaction, + jh->b_transaction, journal->j_committing_transaction); + spin_unlock(&jh->b_state_lock); + error =3D -EINVAL; + jbd2_journal_abort(journal, error); + goto out; + } =20 /* * There is one case we have to be very careful about. If the @@ -1520,8 +1541,14 @@ int jbd2_journal_dirty_metadata(handle_t *handle, st= ruct buffer_head *bh) if (data_race(jh->b_transaction !=3D transaction && jh->b_next_transaction !=3D transaction)) { spin_lock(&jh->b_state_lock); - J_ASSERT_JH(jh, jh->b_transaction =3D=3D transaction || - jh->b_next_transaction =3D=3D transaction); + if (WARN_ON_ONCE(jh->b_transaction !=3D transaction && + jh->b_next_transaction !=3D transaction)) { + pr_err("JBD2: %s: assertion failure: b_transaction=3D%p transaction=3D%= p b_next_transaction=3D%p\n", + journal->j_devname, jh->b_transaction, + transaction, jh->b_next_transaction); + ret =3D -EINVAL; + goto out_unlock_bh; + } spin_unlock(&jh->b_state_lock); } if (data_race(jh->b_modified =3D=3D 1)) { @@ -1531,13 +1558,15 @@ int jbd2_journal_dirty_metadata(handle_t *handle, s= truct buffer_head *bh) spin_lock(&jh->b_state_lock); if (jh->b_transaction =3D=3D transaction && jh->b_jlist !=3D BJ_Metadata) - pr_err("JBD2: assertion failure: h_type=3D%u " - "h_line_no=3D%u block_no=3D%llu jlist=3D%u\n", + pr_err("JBD2: assertion failure: h_type=3D%u h_line_no=3D%u block_no= =3D%llu jlist=3D%u\n", handle->h_type, handle->h_line_no, (unsigned long long) bh->b_blocknr, jh->b_jlist); - J_ASSERT_JH(jh, jh->b_transaction !=3D transaction || - jh->b_jlist =3D=3D BJ_Metadata); + if (WARN_ON_ONCE(jh->b_transaction =3D=3D transaction && + jh->b_jlist !=3D BJ_Metadata)) { + ret =3D -EINVAL; + goto out_unlock_bh; + } spin_unlock(&jh->b_state_lock); } goto out; @@ -1636,7 +1665,10 @@ int jbd2_journal_dirty_metadata(handle_t *handle, st= ruct buffer_head *bh) } =20 /* That test should have eliminated the following case: */ - J_ASSERT_JH(jh, jh->b_frozen_data =3D=3D NULL); + if (WARN_ON_ONCE(jh->b_frozen_data)) { + ret =3D -EINVAL; + goto out_unlock_bh; + } =20 JBUFFER_TRACE(jh, "file as BJ_Metadata"); spin_lock(&journal->j_list_lock); @@ -1675,6 +1707,7 @@ int jbd2_journal_forget(handle_t *handle, struct buff= er_head *bh) int err =3D 0; int was_modified =3D 0; int wait_for_writeback =3D 0; + int abort_journal =3D 0; =20 if (is_handle_aborted(handle)) return -EROFS; @@ -1708,7 +1741,11 @@ int jbd2_journal_forget(handle_t *handle, struct buf= fer_head *bh) jh->b_modified =3D 0; =20 if (jh->b_transaction =3D=3D transaction) { - J_ASSERT_JH(jh, !jh->b_frozen_data); + if (WARN_ON_ONCE(jh->b_frozen_data)) { + err =3D -EINVAL; + abort_journal =3D 1; + goto drop; + } =20 /* If we are forgetting a buffer which is already part * of this transaction, then we can just drop it from @@ -1747,8 +1784,11 @@ int jbd2_journal_forget(handle_t *handle, struct buf= fer_head *bh) } spin_unlock(&journal->j_list_lock); } else if (jh->b_transaction) { - J_ASSERT_JH(jh, (jh->b_transaction =3D=3D - journal->j_committing_transaction)); + if (WARN_ON_ONCE(jh->b_transaction !=3D journal->j_committing_transactio= n)) { + err =3D -EINVAL; + abort_journal =3D 1; + goto drop; + } /* However, if the buffer is still owned by a prior * (committing) transaction, we can't drop it yet... */ JBUFFER_TRACE(jh, "belongs to older transaction"); @@ -1766,7 +1806,11 @@ int jbd2_journal_forget(handle_t *handle, struct buf= fer_head *bh) jh->b_next_transaction =3D transaction; spin_unlock(&journal->j_list_lock); } else { - J_ASSERT(jh->b_next_transaction =3D=3D transaction); + if (WARN_ON_ONCE(jh->b_next_transaction !=3D transaction)) { + err =3D -EINVAL; + abort_journal =3D 1; + goto drop; + } =20 /* * only drop a reference if this transaction modified @@ -1812,6 +1856,8 @@ int jbd2_journal_forget(handle_t *handle, struct buff= er_head *bh) drop: __brelse(bh); spin_unlock(&jh->b_state_lock); + if (abort_journal) + jbd2_journal_abort(journal, err); if (wait_for_writeback) wait_on_buffer(bh); jbd2_journal_put_journal_head(jh); @@ -2136,7 +2182,8 @@ bool jbd2_journal_try_to_free_buffers(journal_t *jour= nal, struct folio *folio) struct buffer_head *bh; bool ret =3D false; =20 - J_ASSERT(folio_test_locked(folio)); + if (WARN_ON_ONCE(!folio_test_locked(folio))) + return false; =20 head =3D folio_buffers(folio); bh =3D head; @@ -2651,6 +2698,8 @@ static int jbd2_journal_file_inode(handle_t *handle, = struct jbd2_inode *jinode, { transaction_t *transaction =3D handle->h_transaction; journal_t *journal; + int err =3D 0; + int abort_transaction =3D 0; =20 if (is_handle_aborted(handle)) return -EROFS; @@ -2685,20 +2734,33 @@ static int jbd2_journal_file_inode(handle_t *handle= , struct jbd2_inode *jinode, /* On some different transaction's list - should be * the committing one */ if (jinode->i_transaction) { - J_ASSERT(jinode->i_next_transaction =3D=3D NULL); - J_ASSERT(jinode->i_transaction =3D=3D - journal->j_committing_transaction); + if (WARN_ON_ONCE(jinode->i_next_transaction || + jinode->i_transaction !=3D + journal->j_committing_transaction)) { + pr_err("JBD2: %s: assertion failure: i_next_transaction=3D%p i_transact= ion=3D%p j_committing_transaction=3D%p\n", + journal->j_devname, jinode->i_next_transaction, + jinode->i_transaction, + journal->j_committing_transaction); + err =3D -EINVAL; + abort_transaction =3D 1; + goto done; + } jinode->i_next_transaction =3D transaction; goto done; } /* Not on any transaction list... */ - J_ASSERT(!jinode->i_next_transaction); + if (WARN_ON_ONCE(jinode->i_next_transaction)) { + err =3D -EINVAL; + abort_transaction =3D 1; + goto done; + } jinode->i_transaction =3D transaction; list_add(&jinode->i_list, &transaction->t_inode_list); done: spin_unlock(&journal->j_list_lock); - - return 0; + if (abort_transaction) + jbd2_journal_abort(journal, err); + return err; } =20 int jbd2_journal_inode_ranged_write(handle_t *handle, --=20 2.53.0