Skip to contentSkip to conversation

ext4: fix incorrect tid assumptions

PATCH Applied Luis Henriques (SUSE) 3 participants 8 messages Jul 24, 2024 – Aug 27, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover ext4: fix incorrect tid assumptions

Hi

As discussed here [1], there are a few places in ext4 and jbd2 code where it is assumed that a tid of '0' is not valid. Which isn't true.

This small patchset tries to fix (hopefully!) all these places. Jan Kara had already identified the functions that needed to be fixed. I believe that the only other issue is the handling of sbi->s_fc_ineligible_tid.

Each patch in this series fixes a single function; the last one also fixes the sbi->s_fc_ineligible_tid handling.

Changes since v1:
- [PATCH 1/4] set 'has_transaction' variable on each loop iteration
- [PATCH 3/4] dropped local variable 'is_last'; renamed 'is_first' to 'first_set' (including corresponding semantic adjustment)
- [PATCH 4/4] removed extra braces in statement

[1] https://lore.kernel.org/all/20240716095201.o7kkrhfdy2bps3rw@quack3/

*** BLURB HERE ***

Luis Henriques (SUSE) (4):
ext4: fix incorrect tid assumption in ext4_wait_for_tail_page_commit()
ext4: fix incorrect tid assumption in __jbd2_log_wait_for_space()
ext4: fix incorrect tid assumption in
jbd2_journal_shrink_checkpoint_list()
ext4: fix incorrect tid assumption in ext4_fc_mark_ineligible()

 fs/ext4/fast_commit.c | 15 +++++++++++----
 fs/ext4/inode.c       | 11 +++++++----
 fs/jbd2/checkpoint.c  | 14 ++++++++++----
 3 files changed, 28 insertions(+), 12 deletions(-)
#1 lore raw

PATCH v2 1/4 ext4: fix incorrect tid assumption in ext4_wait_for_tail_page_commit()

Function ext4_wait_for_tail_page_commit() assumes that '0' is not a valid value for transaction IDs, which is incorrect. Don't assume that and invoke jbd2_log_wait_commit() if the journal had a committing transaction instead.

---
 fs/ext4/inode.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
fs/ext4/inode.c +7 -4
index 941c1c0d5c6e..a0fa5192db8e 100644
@@ -5279,8 +5279,9 @@ static void ext4_wait_for_tail_page_commit(struct inode *inode)
 {
 	unsigned offset;
 	journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
-	tid_t commit_tid = 0;
+	tid_t commit_tid;
 	int ret;
+	bool has_transaction;
 
 	offset = inode->i_size & (PAGE_SIZE - 1);
 	/*
@@ -5305,12 +5306,14 @@ static void ext4_wait_for_tail_page_commit(struct inode *inode)
 		folio_put(folio);
 		if (ret != -EBUSY)
 			return;
-		commit_tid = 0;
+		has_transaction = false;
 		read_lock(&journal->j_state_lock);
-		if (journal->j_committing_transaction)
+		if (journal->j_committing_transaction) {
 			commit_tid = journal->j_committing_transaction->t_tid;
+			has_transaction = true;
+		}
 		read_unlock(&journal->j_state_lock);
-		if (commit_tid)
+		if (has_transaction)
 			jbd2_log_wait_commit(journal, commit_tid);
 	}
 }
reviewer Reviewed-by on v2 1/4
#2 lore raw

Re: [PATCH v2 1/4] ext4: fix incorrect tid assumption in ext4_wait_for_tail_page_commit()

Quoted 5 lines · Luis Henriques (SUSE)

Function ext4_wait_for_tail_page_commit() assumes that '0' is not a valid value for transaction IDs, which is incorrect. Don't assume that and invoke jbd2_log_wait_commit() if the journal had a committing transaction instead.

Looks good. Feel free to add:

Honza

Quoted 38 lines
---
 fs/ext4/inode.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
fs/ext4/inode.c +7 -4
index 941c1c0d5c6e..a0fa5192db8e 100644
@@ -5279,8 +5279,9 @@ static void ext4_wait_for_tail_page_commit(struct inode *inode)
 {
 	unsigned offset;
 	journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
-	tid_t commit_tid = 0;
+	tid_t commit_tid;
 	int ret;
+	bool has_transaction;
 
 	offset = inode->i_size & (PAGE_SIZE - 1);
 	/*
@@ -5305,12 +5306,14 @@ static void ext4_wait_for_tail_page_commit(struct inode *inode)
 		folio_put(folio);
 		if (ret != -EBUSY)
 			return;
-		commit_tid = 0;
+		has_transaction = false;
 		read_lock(&journal->j_state_lock);
-		if (journal->j_committing_transaction)
+		if (journal->j_committing_transaction) {
 			commit_tid = journal->j_committing_transaction->t_tid;
+			has_transaction = true;
+		}
 		read_unlock(&journal->j_state_lock);
-		if (commit_tid)
+		if (has_transaction)
 			jbd2_log_wait_commit(journal, commit_tid);
 	}
 }
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#3 lore raw

PATCH v2 2/4 ext4: fix incorrect tid assumption in __jbd2_log_wait_for_space()

In reply to Luis Henriques (SUSE)

Function __jbd2_log_wait_for_space() assumes that '0' is not a valid value for transaction IDs, which is incorrect. Don't assume that and invoke jbd2_log_wait_commit() if the journal had a committing transaction instead.

---
 fs/jbd2/checkpoint.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/jbd2/checkpoint.c +5 -2
index 951f78634adf..77bc522e6821 100644
@@ -79,9 +79,12 @@ __releases(&journal->j_state_lock)
 		if (space_left < nblocks) {
 			int chkpt = journal->j_checkpoint_transactions != NULL;
 			tid_t tid = 0;
+			bool has_transaction = false;
 
-			if (journal->j_committing_transaction)
+			if (journal->j_committing_transaction) {
 				tid = journal->j_committing_transaction->t_tid;
+				has_transaction = true;
+			}
 			spin_unlock(&journal->j_list_lock);
 			write_unlock(&journal->j_state_lock);
 			if (chkpt) {
@@ -89,7 +92,7 @@ __releases(&journal->j_state_lock)
 			} else if (jbd2_cleanup_journal_tail(journal) == 0) {
 				/* We were able to recover space; yay! */
 				;
-			} else if (tid) {
+			} else if (has_transaction) {
 				/*
 				 * jbd2_journal_commit_transaction() may want
 				 * to take the checkpoint_mutex if JBD2_FLUSHED
#4 lore raw

PATCH v2 3/4 ext4: fix incorrect tid assumption in jbd2_journal_shrink_checkpoint_list()

In reply to Luis Henriques (SUSE)

Function jbd2_journal_shrink_checkpoint_list() assumes that '0' is not a valid value for transaction IDs, which is incorrect. Don't assume that and use two extra boolean variables to control the loop iterations and keep track of the first and last tid.

---
 fs/jbd2/checkpoint.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/jbd2/checkpoint.c +5 -2
index 77bc522e6821..98a0b2eb84f5 100644
@@ -410,6 +410,7 @@ unsigned long jbd2_journal_shrink_checkpoint_list(journal_t *journal,
 	tid_t tid = 0;
 	unsigned long nr_freed = 0;
 	unsigned long freed;
+	bool first_set = false;
 
 again:
 	spin_lock(&journal->j_list_lock);
@@ -429,8 +430,10 @@ unsigned long jbd2_journal_shrink_checkpoint_list(journal_t *journal,
 	else
 		transaction = journal->j_checkpoint_transactions;
 
-	if (!first_tid)
+	if (!first_set) {
 		first_tid = transaction->t_tid;
+		first_set = true;
+	}
 	last_transaction = journal->j_checkpoint_transactions->t_cpprev;
 	next_transaction = transaction;
 	last_tid = last_transaction->t_tid;
@@ -460,7 +463,7 @@ unsigned long jbd2_journal_shrink_checkpoint_list(journal_t *journal,
 	spin_unlock(&journal->j_list_lock);
 	cond_resched();
 
-	if (*nr_to_scan && next_tid)
+	if (*nr_to_scan && journal->j_shrink_transaction)
 		goto again;
 out:
 	trace_jbd2_shrink_checkpoint_list(journal, first_tid, tid, last_tid,
reviewer Reviewed-by on v2 3/4
#5 lore raw

Re: [PATCH v2 3/4] ext4: fix incorrect tid assumption in jbd2_journal_shrink_checkpoint_list()

Quoted 6 lines · Luis Henriques (SUSE)

Function jbd2_journal_shrink_checkpoint_list() assumes that '0' is not a valid value for transaction IDs, which is incorrect. Don't assume that and use two extra boolean variables to control the loop iterations and keep track of the first and last tid.

Looks good. Feel free to add:

Honza

Quoted 38 lines
---
 fs/jbd2/checkpoint.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/jbd2/checkpoint.c +5 -2
index 77bc522e6821..98a0b2eb84f5 100644
@@ -410,6 +410,7 @@ unsigned long jbd2_journal_shrink_checkpoint_list(journal_t *journal,
 	tid_t tid = 0;
 	unsigned long nr_freed = 0;
 	unsigned long freed;
+	bool first_set = false;
 
 again:
 	spin_lock(&journal->j_list_lock);
@@ -429,8 +430,10 @@ unsigned long jbd2_journal_shrink_checkpoint_list(journal_t *journal,
 	else
 		transaction = journal->j_checkpoint_transactions;
 
-	if (!first_tid)
+	if (!first_set) {
 		first_tid = transaction->t_tid;
+		first_set = true;
+	}
 	last_transaction = journal->j_checkpoint_transactions->t_cpprev;
 	next_transaction = transaction;
 	last_tid = last_transaction->t_tid;
@@ -460,7 +463,7 @@ unsigned long jbd2_journal_shrink_checkpoint_list(journal_t *journal,
 	spin_unlock(&journal->j_list_lock);
 	cond_resched();
 
-	if (*nr_to_scan && next_tid)
+	if (*nr_to_scan && journal->j_shrink_transaction)
 		goto again;
 out:
 	trace_jbd2_shrink_checkpoint_list(journal, first_tid, tid, last_tid,
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#6 lore raw

PATCH v2 4/4 ext4: fix incorrect tid assumption in ext4_fc_mark_ineligible()

In reply to Luis Henriques (SUSE)

Function jbd2_journal_shrink_checkpoint_list() assumes that '0' is not a valid value for transaction IDs, which is incorrect.

Furthermore, the sbi->s_fc_ineligible_tid handling also makes the same assumption by being initialised to '0'. Fortunately, the sb flag EXT4_MF_FC_INELIGIBLE can be used to check whether sbi->s_fc_ineligible_tid has been previously set instead of comparing it with '0'.

---
 fs/ext4/fast_commit.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
fs/ext4/fast_commit.c +11 -4
index 3926a05eceee..6f4c97bdb2d8 100644
@@ -339,22 +339,29 @@ void ext4_fc_mark_ineligible(struct super_block *sb, int reason, handle_t *handl
 {
 	struct ext4_sb_info *sbi = EXT4_SB(sb);
 	tid_t tid;
+	bool has_transaction = true;
+	bool is_ineligible;
 
 	if (ext4_fc_disabled(sb))
 		return;
 
-	ext4_set_mount_flag(sb, EXT4_MF_FC_INELIGIBLE);
 	if (handle && !IS_ERR(handle))
 		tid = handle->h_transaction->t_tid;
 	else {
 		read_lock(&sbi->s_journal->j_state_lock);
-		tid = sbi->s_journal->j_running_transaction ?
-				sbi->s_journal->j_running_transaction->t_tid : 0;
+		if (sbi->s_journal->j_running_transaction)
+			tid = sbi->s_journal->j_running_transaction->t_tid;
+		else
+			has_transaction = false;
 		read_unlock(&sbi->s_journal->j_state_lock);
 	}
 	spin_lock(&sbi->s_fc_lock);
-	if (tid_gt(tid, sbi->s_fc_ineligible_tid))
+	is_ineligible = ext4_test_mount_flag(sb, EXT4_MF_FC_INELIGIBLE);
+	if (has_transaction &&
+	    (!is_ineligible ||
+	     (is_ineligible && tid_gt(tid, sbi->s_fc_ineligible_tid))))
 		sbi->s_fc_ineligible_tid = tid;
+	ext4_set_mount_flag(sb, EXT4_MF_FC_INELIGIBLE);
 	spin_unlock(&sbi->s_fc_lock);
 	WARN_ON(reason >= EXT4_FC_REASON_MAX);
 	sbi->s_fc_stats.fc_ineligible_reason_count[reason]++;
maintainer Applied
#7 lore raw
In reply to Luis Henriques (SUSE)
Quoted 8 lines · Luis Henriques (SUSE)

As discussed here [1], there are a few places in ext4 and jbd2 code where it is assumed that a tid of '0' is not valid. Which isn't true.

This small patchset tries to fix (hopefully!) all these places. Jan Kara had already identified the functions that needed to be fixed. I believe that the only other issue is the handling of sbi->s_fc_ineligible_tid.

[...]

Applied, thanks!

[1/4] ext4: fix incorrect tid assumption in ext4_wait_for_tail_page_commit()

      commit: dd589b0f1445e1ea1085b98edca6e4d5dedb98d0
[2/4] ext4: fix incorrect tid assumption in __jbd2_log_wait_for_space()
      commit: 972090651ee15e51abfb2160e986fa050cfc7a40
[3/4] ext4: fix incorrect tid assumption in jbd2_journal_shrink_checkpoint_list()
      commit: 7a6443e1dad70281f99f0bd394d7fd342481a632
[4/4] ext4: fix incorrect tid assumption in ext4_fc_mark_ineligible()
      commit: ebc4b2c1ac92fc0f8bf3f5a9c285a871d5084a6b

Best regards,

Signature · 1 line
Theodore Ts'o <[email protected]>