Skip to contentSkip to conversation

bcachefs: Add missing smp_rmb()

PATCH In review Alan Huang 2 participants 2 messages Mar 23, 2025

lore
Share & tools
lore

Alan Huang

author
#0 lore raw

PATCH v2 1/1 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.

---

Since I'm touching this part, add the READ_ONCE anyway.

 fs/bcachefs/journal.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/bcachefs/journal.h +4 -2
index ab68c5c4d8d8..fe362ddd2895 100644
@@ -350,9 +350,11 @@ 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:
 		 */
-		if (new.cur_entry_offset + res->u64s > j->cur_entry_u64s)
+		smp_rmb();
+		if (new.cur_entry_offset + res->u64s > READ_ONCE(j->cur_entry_u64s))
 			return 0;
 
 		EBUG_ON(!journal_state_count(new, new.idx));
Signature · 1 line
2.48.1
#1 lore raw
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.

We're only using this once, the barrier suffices here.

Quoted 29 lines

---

Since I'm touching this part, add the READ_ONCE anyway.

 fs/bcachefs/journal.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/bcachefs/journal.h +4 -2
index ab68c5c4d8d8..fe362ddd2895 100644
@@ -350,9 +350,11 @@ 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:
 		 */
-		if (new.cur_entry_offset + res->u64s > j->cur_entry_u64s)
+		smp_rmb();
+		if (new.cur_entry_offset + res->u64s > READ_ONCE(j->cur_entry_u64s))
 			return 0;
 
 		EBUG_ON(!journal_state_count(new, new.idx));
Signature · 1 line
2.48.1