Skip to content

Commit 3b8c450

Browse files
author
Kent Overstreet
committed
bcachefs: btree_trans->write_locked
As prep work for the next patch to fix a key cache reclaim issue, we need to start tracking whether we're currently holding write locks - so that we can release and retake the before calling into memory reclaim. Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
1 parent c65c13f commit 3b8c450

2 files changed

Lines changed: 50 additions & 36 deletions

File tree

fs/bcachefs/btree_trans_commit.c

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,53 @@ inline void bch2_btree_node_prep_for_write(struct btree_trans *trans,
7878
bch2_btree_init_next(trans, b);
7979
}
8080

81+
static noinline int trans_lock_write_fail(struct btree_trans *trans, struct btree_insert_entry *i)
82+
{
83+
while (--i >= trans->updates) {
84+
if (same_leaf_as_prev(trans, i))
85+
continue;
86+
87+
bch2_btree_node_unlock_write(trans, i->path, insert_l(i)->b);
88+
}
89+
90+
trace_and_count(trans->c, trans_restart_would_deadlock_write, trans);
91+
return btree_trans_restart(trans, BCH_ERR_transaction_restart_would_deadlock_write);
92+
}
93+
94+
static inline int bch2_trans_lock_write(struct btree_trans *trans)
95+
{
96+
struct btree_insert_entry *i;
97+
98+
EBUG_ON(trans->write_locked);
99+
100+
trans_for_each_update(trans, i) {
101+
if (same_leaf_as_prev(trans, i))
102+
continue;
103+
104+
if (bch2_btree_node_lock_write(trans, i->path, &insert_l(i)->b->c))
105+
return trans_lock_write_fail(trans, i);
106+
107+
if (!i->cached)
108+
bch2_btree_node_prep_for_write(trans, i->path, insert_l(i)->b);
109+
}
110+
111+
trans->write_locked = true;
112+
return 0;
113+
}
114+
115+
static inline void bch2_trans_unlock_write(struct btree_trans *trans)
116+
{
117+
if (likely(trans->write_locked)) {
118+
struct btree_insert_entry *i;
119+
120+
trans_for_each_update(trans, i)
121+
if (!same_leaf_as_prev(trans, i))
122+
bch2_btree_node_unlock_write_inlined(trans, i->path,
123+
insert_l(i)->b);
124+
trans->write_locked = false;
125+
}
126+
}
127+
81128
/* Inserting into a given leaf node (last stage of insert): */
82129

83130
/* Handle overwrites and do insert, for non extents: */
@@ -732,37 +779,6 @@ bch2_trans_commit_write_locked(struct btree_trans *trans, unsigned flags,
732779
return ret;
733780
}
734781

735-
static noinline int trans_lock_write_fail(struct btree_trans *trans, struct btree_insert_entry *i)
736-
{
737-
while (--i >= trans->updates) {
738-
if (same_leaf_as_prev(trans, i))
739-
continue;
740-
741-
bch2_btree_node_unlock_write(trans, i->path, insert_l(i)->b);
742-
}
743-
744-
trace_and_count(trans->c, trans_restart_would_deadlock_write, trans);
745-
return btree_trans_restart(trans, BCH_ERR_transaction_restart_would_deadlock_write);
746-
}
747-
748-
static inline int trans_lock_write(struct btree_trans *trans)
749-
{
750-
struct btree_insert_entry *i;
751-
752-
trans_for_each_update(trans, i) {
753-
if (same_leaf_as_prev(trans, i))
754-
continue;
755-
756-
if (bch2_btree_node_lock_write(trans, i->path, &insert_l(i)->b->c))
757-
return trans_lock_write_fail(trans, i);
758-
759-
if (!i->cached)
760-
bch2_btree_node_prep_for_write(trans, i->path, insert_l(i)->b);
761-
}
762-
763-
return 0;
764-
}
765-
766782
static noinline void bch2_drop_overwrites_from_journal(struct btree_trans *trans)
767783
{
768784
struct btree_insert_entry *i;
@@ -838,7 +854,7 @@ static inline int do_bch2_trans_commit(struct btree_trans *trans, unsigned flags
838854
if (unlikely(ret))
839855
return ret;
840856

841-
ret = trans_lock_write(trans);
857+
ret = bch2_trans_lock_write(trans);
842858
if (unlikely(ret))
843859
return ret;
844860

@@ -847,10 +863,7 @@ static inline int do_bch2_trans_commit(struct btree_trans *trans, unsigned flags
847863
if (!ret && unlikely(trans->journal_replay_not_finished))
848864
bch2_drop_overwrites_from_journal(trans);
849865

850-
trans_for_each_update(trans, i)
851-
if (!same_leaf_as_prev(trans, i))
852-
bch2_btree_node_unlock_write_inlined(trans, i->path,
853-
insert_l(i)->b);
866+
bch2_trans_unlock_write(trans);
854867

855868
if (!ret && trans->journal_pin)
856869
bch2_journal_pin_add(&c->journal, trans->journal_res.seq,

fs/bcachefs/btree_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ struct btree_trans {
409409
bool journal_transaction_names:1;
410410
bool journal_replay_not_finished:1;
411411
bool notrace_relock_fail:1;
412+
bool write_locked:1;
412413
enum bch_errcode restarted:16;
413414
u32 restart_count;
414415
unsigned long last_begin_ip;

0 commit comments

Comments
 (0)