Skip to contentSkip to conversation

bcachefs: Add missing smp_rmb()

PATCH Applied Alan Huang 2 participants 4 messages Mar 18, 2025

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore

Alan Huang

author
#0 lore raw

PATCH 1/2 bcachefs: Add missing smp_rmb()

The smp_rmb() guarantees that reads from reservations.counter occur before accessing cur_entry_u64s. It's paired with the atomic64_try_cmpxchg in journal_entry_open.

---
 fs/bcachefs/journal.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/bcachefs/journal.h +3 -1
index ab68c5c4d8d8..47828771f9c2 100644
@@ -350,8 +350,10 @@ static inline int journal_res_get_fast(struct journal *j,
 
 		/*
 		 * Check if there is still room in the current journal
-		 * entry:
+		 * entry, smp_rmb() guarantees that reads from reservations.counter
+		 * occur before accessing cur_entry_u64s:
 		 */
+		smp_rmb();
 		if (new.cur_entry_offset + res->u64s > j->cur_entry_u64s)
 			return 0;
 
Signature · 1 line
2.48.1
author
#1 lore raw

PATCH 2/2 bcachefs: Remove spurious smp_mb()

The smp_mb() is paired with nothing.

---
 fs/bcachefs/journal.c | 1 -
 1 file changed, 1 deletion(-)
fs/bcachefs/journal.c +0 -1
index 8d4f3bfaa228..fc15644c9f24 100644
@@ -761,7 +761,6 @@ void bch2_journal_entry_res_resize(struct journal *j,
 		goto out;
 
 	j->cur_entry_u64s = max_t(int, 0, j->cur_entry_u64s - d);
-	smp_mb();
 	state = READ_ONCE(j->reservations);
 
 	if (state.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL &&
Signature · 1 line
2.48.1
#2 lore raw

Re: [PATCH 2/2] bcachefs: Remove spurious smp_mb()

Quoted 3 lines · Alan Huang

The smp_mb() is paired with nothing.

Applied

Quoted 19 lines
---
 fs/bcachefs/journal.c | 1 -
 1 file changed, 1 deletion(-)
fs/bcachefs/journal.c +0 -1
index 8d4f3bfaa228..fc15644c9f24 100644
@@ -761,7 +761,6 @@ void bch2_journal_entry_res_resize(struct journal *j,
 		goto out;
 
 	j->cur_entry_u64s = max_t(int, 0, j->cur_entry_u64s - d);
-	smp_mb();
 	state = READ_ONCE(j->reservations);
 
 	if (state.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL &&
Signature · 1 line
2.48.1
#3 lore raw
In reply to Alan Huang
Quoted 5 lines · Alan Huang

The smp_rmb() guarantees that reads from reservations.counter occur before accessing cur_entry_u64s. It's paired with the atomic64_try_cmpxchg in journal_entry_open.

It would also work to recheck j->cur_entry_u64s after the cmxpchg() and acquiring the journal reservation, backing out in the event of a race.

This is performance sensitive enough that we may want to test both options, as well as moving cur_entry_u64s to the same cacheline as the journal reservation state.

Quoted 23 lines
---
 fs/bcachefs/journal.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/bcachefs/journal.h +3 -1
index ab68c5c4d8d8..47828771f9c2 100644
@@ -350,8 +350,10 @@ static inline int journal_res_get_fast(struct journal *j,
 
 		/*
 		 * Check if there is still room in the current journal
-		 * entry:
+		 * entry, smp_rmb() guarantees that reads from reservations.counter
+		 * occur before accessing cur_entry_u64s:
 		 */
+		smp_rmb();
 		if (new.cur_entry_offset + res->u64s > j->cur_entry_u64s)
 			return 0;
 
Signature · 1 line
2.48.1