Skip to contentSkip to conversation

bcachefs: WARNING in bch2_fs_journal_stop

PATCH In review Camila Alvarez 3 participants 9 messages Jul 17, 2024 – Aug 2, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bcachefs: WARNING in bch2_fs_journal_stop

last_seq and last_empty_seq suffered from an off by one error when the journal has no entries.

The indexes were fixed and an assertion is added to check that the last_empty_seq is always kept under the next valid seq number.

---
 fs/bcachefs/journal.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)
fs/bcachefs/journal.c +13 -10
index 10b19791ec98..7bbbf4b149e9 100644
@@ -1201,7 +1201,7 @@ int bch2_fs_journal_start(struct journal *j, u64 cur_seq)
 	struct journal_replay *i, **_i;
 	struct genradix_iter iter;
 	bool had_entries = false;
-	u64 last_seq = cur_seq, nr, seq;
+        u64 last_written_seq = cur_seq - 1, last_seq = cur_seq - 1, nr, seq;
 
 	genradix_for_each_reverse(&c->journal_entries, iter, _i) {
 		i = *_i;
@@ -1209,11 +1209,11 @@ int bch2_fs_journal_start(struct journal *j, u64 cur_seq)
 		if (journal_replay_ignore(i))
 			continue;
 
-		last_seq = le64_to_cpu(i->j.last_seq);
+		last_written_seq = le64_to_cpu(i->j.last_seq);
 		break;
 	}
 
-	nr = cur_seq - last_seq;
+	nr = cur_seq - last_written_seq;
 
 	if (nr + 1 > j->pin.size) {
 		free_fifo(&j->pin);
@@ -1224,14 +1224,14 @@ int bch2_fs_journal_start(struct journal *j, u64 cur_seq)
 		}
 	}
 
-	j->replay_journal_seq	= last_seq;
+	j->replay_journal_seq	= last_written_seq;
 	j->replay_journal_seq_end = cur_seq;
-	j->last_seq_ondisk	= last_seq;
-	j->flushed_seq_ondisk	= cur_seq - 1;
-	j->seq_ondisk		= cur_seq - 1;
-	j->pin.front		= last_seq;
+	j->last_seq_ondisk	= last_written_seq;
+	j->flushed_seq_ondisk	= last_seq;
+	j->seq_ondisk		= last_seq;
+	j->pin.front		= last_written_seq;
 	j->pin.back		= cur_seq;
-	atomic64_set(&j->seq, cur_seq - 1);
+	atomic64_set(&j->seq, last_seq);
 
 	fifo_for_each_entry_ptr(p, &j->pin, seq)
 		journal_pin_list_init(p, 1);
@@ -1261,7 +1261,10 @@ int bch2_fs_journal_start(struct journal *j, u64 cur_seq)
 	}
 
 	if (!had_entries)
-		j->last_empty_seq = cur_seq;
+		j->last_empty_seq = last_seq;
+
+	WARN(j->last_empty_seq >= cur_seq, "journal startup error: last empty seq %llu is higher or equal than the next seq number to be used (%llu)",
+	      j->last_empty_seq, cur_seq);
 
 	spin_lock(&j->lock);
 
Signature · 1 line
2.34.1
#1 lore raw

fs/bcachefs/journal.c int bch2_fs_journal_start(struct journal *j, u64 cur_seq) · line 1204

Quoted 10 lines · Camila Alvarez
fs/bcachefs/journal.c +1 -1
index 10b19791ec98..7bbbf4b149e9 100644
@@ -1201,7 +1201,7 @@ int bch2_fs_journal_start(struct journal *j, u64 cur_seq)
 	struct journal_replay *i, **_i;
 	struct genradix_iter iter;
 	bool had_entries = false;
-	u64 last_seq = cur_seq, nr, seq;
+        u64 last_written_seq = cur_seq - 1, last_seq = cur_seq - 1, nr, seq;

Spaces not tabs. I think consistency would be wanted so should be a tab.

AP

#2 lore raw
In reply to Camila Alvarez
Quoted 5 lines · Camila Alvarez

last_seq and last_empty_seq suffered from an off by one error when the journal has no entries.

The indexes were fixed and an assertion is added to check that the last_empty_seq is always kept under the next valid seq number.

oh nice. I'm going to need to stare at this some more, I still feel like this code needs to be clearer and less fiddly.

Quoted 70 lines
---
 fs/bcachefs/journal.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)
fs/bcachefs/journal.c +13 -10
index 10b19791ec98..7bbbf4b149e9 100644
@@ -1201,7 +1201,7 @@ int bch2_fs_journal_start(struct journal *j, u64 cur_seq)
 	struct journal_replay *i, **_i;
 	struct genradix_iter iter;
 	bool had_entries = false;
-	u64 last_seq = cur_seq, nr, seq;
+        u64 last_written_seq = cur_seq - 1, last_seq = cur_seq - 1, nr, seq;
 
 	genradix_for_each_reverse(&c->journal_entries, iter, _i) {
 		i = *_i;
@@ -1209,11 +1209,11 @@ int bch2_fs_journal_start(struct journal *j, u64 cur_seq)
 		if (journal_replay_ignore(i))
 			continue;
 
-		last_seq = le64_to_cpu(i->j.last_seq);
+		last_written_seq = le64_to_cpu(i->j.last_seq);
 		break;
 	}
 
-	nr = cur_seq - last_seq;
+	nr = cur_seq - last_written_seq;
 
 	if (nr + 1 > j->pin.size) {
 		free_fifo(&j->pin);
@@ -1224,14 +1224,14 @@ int bch2_fs_journal_start(struct journal *j, u64 cur_seq)
 		}
 	}
 
-	j->replay_journal_seq	= last_seq;
+	j->replay_journal_seq	= last_written_seq;
 	j->replay_journal_seq_end = cur_seq;
-	j->last_seq_ondisk	= last_seq;
-	j->flushed_seq_ondisk	= cur_seq - 1;
-	j->seq_ondisk		= cur_seq - 1;
-	j->pin.front		= last_seq;
+	j->last_seq_ondisk	= last_written_seq;
+	j->flushed_seq_ondisk	= last_seq;
+	j->seq_ondisk		= last_seq;
+	j->pin.front		= last_written_seq;
 	j->pin.back		= cur_seq;
-	atomic64_set(&j->seq, cur_seq - 1);
+	atomic64_set(&j->seq, last_seq);
 
 	fifo_for_each_entry_ptr(p, &j->pin, seq)
 		journal_pin_list_init(p, 1);
@@ -1261,7 +1261,10 @@ int bch2_fs_journal_start(struct journal *j, u64 cur_seq)
 	}
 
 	if (!had_entries)
-		j->last_empty_seq = cur_seq;
+		j->last_empty_seq = last_seq;
+
+	WARN(j->last_empty_seq >= cur_seq, "journal startup error: last empty seq %llu is higher or equal than the next seq number to be used (%llu)",
+	      j->last_empty_seq, cur_seq);
 
 	spin_lock(&j->lock);
 
Signature · 1 line
2.34.1
#3 lore raw
Quoted 10 lines · Kent Overstreet
Quoted 5 lines · Camila Alvarez

last_seq and last_empty_seq suffered from an off by one error when the journal has no entries.

The indexes were fixed and an assertion is added to check that the last_empty_seq is always kept under the next valid seq number.

oh nice. I'm going to need to stare at this some more, I still feel like this code needs to be clearer and less fiddly.

Sure! Please let me know if there's anything I can do to help.

Quoted 71 lines
Quoted 70 lines
---
 fs/bcachefs/journal.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)
fs/bcachefs/journal.c +13 -11
index 10b19791ec98..7bbbf4b149e9 100644
@@ -1201,7 +1201,7 @@ int bch2_fs_journal_start(struct journal *j, u64 cur_seq)
 	struct journal_replay *i, **_i;
 	struct genradix_iter iter;
 	bool had_entries = false;
-	u64 last_seq = cur_seq, nr, seq;
+        u64 last_written_seq = cur_seq - 1, last_seq = cur_seq - 1, nr, seq;
 
 	genradix_for_each_reverse(&c->journal_entries, iter, _i) {
 		i = *_i;
@@ -1209,11 +1209,11 @@ int bch2_fs_journal_start(struct journal *j, u64 cur_seq)
 		if (journal_replay_ignore(i))
 			continue;
 
-		last_seq = le64_to_cpu(i->j.last_seq);
+		last_written_seq = le64_to_cpu(i->j.last_seq);
 		break;
 	}
 
-	nr = cur_seq - last_seq;
+	nr = cur_seq - last_written_seq;
 
 	if (nr + 1 > j->pin.size) {
 		free_fifo(&j->pin);
@@ -1224,14 +1224,14 @@ int bch2_fs_journal_start(struct journal *j, u64 cur_seq)
 		}
 	}
 
-	j->replay_journal_seq	= last_seq;
+	j->replay_journal_seq	= last_written_seq;
 	j->replay_journal_seq_end = cur_seq;
-	j->last_seq_ondisk	= last_seq;
-	j->flushed_seq_ondisk	= cur_seq - 1;
-	j->seq_ondisk		= cur_seq - 1;
-	j->pin.front		= last_seq;
+	j->last_seq_ondisk	= last_written_seq;
+	j->flushed_seq_ondisk	= last_seq;
+	j->seq_ondisk		= last_seq;
+	j->pin.front		= last_written_seq;
 	j->pin.back		= cur_seq;
-	atomic64_set(&j->seq, cur_seq - 1);
+	atomic64_set(&j->seq, last_seq);
 
 	fifo_for_each_entry_ptr(p, &j->pin, seq)
 		journal_pin_list_init(p, 1);
@@ -1261,7 +1261,10 @@ int bch2_fs_journal_start(struct journal *j, u64 cur_seq)
 	}
 
 	if (!had_entries)
-		j->last_empty_seq = cur_seq;
+		j->last_empty_seq = last_seq;
+
+	WARN(j->last_empty_seq >= cur_seq, "journal startup error: last empty seq %llu is higher or equal than the next seq number to be used (%llu)",
+	      j->last_empty_seq, cur_seq);
 
 	spin_lock(&j->lock);
 
--

2.34.1

#4 lore raw
Quoted 8 lines · Camila Alvarez Inostroza
Quoted 4 lines · Kent Overstreet
Quoted 3 lines · Camila Alvarez

last_seq and last_empty_seq suffered from an off by one error when the journal has no entries.

Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

The indexes were fixed and an assertion is added to check that the last_empty_seq is always kept under the next valid seq number.

oh nice. I'm going to need to stare at this some more, I still feel like this code needs to be clearer and less fiddly.

Sure! Please let me know if there's anything I can do to help.

I'm just applying it - the warning you added gave me warm fuzzies.

I'll let you know if anything pops up in the CI (and I should give you an account so you can point it at your own branches; email me a username and ssh key)

#5 lore raw
Quoted 15 lines · Kent Overstreet
Quoted 14 lines · Camila Alvarez Inostroza
Quoted 10 lines · Kent Overstreet
Quoted 5 lines · Camila Alvarez

last_seq and last_empty_seq suffered from an off by one error when the journal has no entries.

The indexes were fixed and an assertion is added to check that the last_empty_seq is always kept under the next valid seq number.

oh nice. I'm going to need to stare at this some more, I still feel like this code needs to be clearer and less fiddly.

Quoted 8 lines
Quoted 1 line

Sure! Please let me know if there's anything I can do to help.

I'm just applying it - the warning you added gave me warm fuzzies.

I'll let you know if anything pops up in the CI (and I should give you an account so you can point it at your own branches; email me a username and ssh key)

Should I give you the info in this same thread or in a separate email?

#6 lore raw
Quoted 21 lines · Camila Alvarez Inostroza
Quoted 17 lines · Kent Overstreet
Quoted 15 lines · Camila Alvarez Inostroza
Quoted 10 lines · Kent Overstreet
Quoted 5 lines · Camila Alvarez

last_seq and last_empty_seq suffered from an off by one error when the journal has no entries.

The indexes were fixed and an assertion is added to check that the last_empty_seq is always kept under the next valid seq number.

oh nice. I'm going to need to stare at this some more, I still feel like this code needs to be clearer and less fiddly.

Sure! Please let me know if there's anything I can do to help.

Quoted 7 lines
Quoted 6 lines

I'm just applying it - the warning you added gave me warm fuzzies.

I'll let you know if anything pops up in the CI (and I should give you an account so you can point it at your own branches; email me a username and ssh key)

Should I give you the info in this same thread or in a separate email?

doesn't matter, ssh pubkeys aren't secrets

#7 lore raw
Quoted 24 lines · Kent Overstreet
Quoted 23 lines · Camila Alvarez Inostroza
Quoted 19 lines · Kent Overstreet
Quoted 8 lines · Camila Alvarez Inostroza
Quoted 4 lines · Kent Overstreet
Quoted 3 lines · Camila Alvarez

last_seq and last_empty_seq suffered from an off by one error when the journal has no entries.

Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

The indexes were fixed and an assertion is added to check that the last_empty_seq is always kept under the next valid seq number.

oh nice. I'm going to need to stare at this some more, I still feel like this code needs to be clearer and less fiddly.

Sure! Please let me know if there's anything I can do to help.

I'm just applying it - the warning you added gave me warm fuzzies.

Quoted 8 lines
Quoted 5 lines
Quoted 4 lines

I'll let you know if anything pops up in the CI (and I should give you an account so you can point it at your own branches; email me a username and ssh key)

Should I give you the info in this same thread or in a separate email?

doesn't matter, ssh pubkeys aren't secrets

I've attached my pubkey. You can use calvarez as my username. Thanks

#8 lore raw
Quoted 32 lines · Camila Alvarez Inostroza
Quoted 28 lines · Kent Overstreet
Quoted 27 lines · Camila Alvarez Inostroza
Quoted 23 lines · Kent Overstreet
Quoted 8 lines · Camila Alvarez Inostroza
Quoted 4 lines · Kent Overstreet
Quoted 3 lines · Camila Alvarez

last_seq and last_empty_seq suffered from an off by one error when the journal has no entries.

Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

The indexes were fixed and an assertion is added to check that the last_empty_seq is always kept under the next valid seq number.

oh nice. I'm going to need to stare at this some more, I still feel like this code needs to be clearer and less fiddly.

Sure! Please let me know if there's anything I can do to help.

I'm just applying it - the warning you added gave me warm fuzzies.

I'll let you know if anything pops up in the CI (and I should give you an account so you can point it at your own branches; email me a username and ssh key)

Quoted 5 lines
Quoted 4 lines
Quoted 1 line

Should I give you the info in this same thread or in a separate email?

doesn't matter, ssh pubkeys aren't secrets

I've attached my pubkey. You can use calvarez as my username. Thanks

Hi, just wondering if you had any time to take a look at this patch. Please let me know if you have any comments!