From b5fd75669ab1283e7a9caf6288c425108bd382b0 Mon Sep 17 00:00:00 2001 From: Kent Overstreet Date: Sun, 28 May 2023 18:06:27 -0400 Subject: [PATCH] bcachefs: drop_locks_do() Add a new helper for the common pattern of: - trans_unlock() - do something - trans_relock() Signed-off-by: Kent Overstreet --- fs/bcachefs/btree_iter.c | 9 +++------ fs/bcachefs/btree_iter.h | 5 +++++ fs/bcachefs/btree_locking.c | 5 +---- fs/bcachefs/btree_update_interior.c | 8 ++------ fs/bcachefs/btree_update_leaf.c | 25 ++++--------------------- 5 files changed, 15 insertions(+), 37 deletions(-) diff --git a/fs/bcachefs/btree_iter.c b/fs/bcachefs/btree_iter.c index 4830d203b37b..1cc53b37f78e 100644 --- a/fs/bcachefs/btree_iter.c +++ b/fs/bcachefs/btree_iter.c @@ -41,13 +41,10 @@ static struct btree_path *btree_path_alloc(struct btree_trans *, struct btree_pa */ static inline int bch2_trans_cond_resched(struct btree_trans *trans) { - if (need_resched() || race_fault()) { - bch2_trans_unlock(trans); - schedule(); - return bch2_trans_relock(trans); - } else { + if (need_resched() || race_fault()) + return drop_locks_do(trans, (schedule(), 0)); + else return 0; - } } static inline int __btree_path_cmp(const struct btree_path *l, diff --git a/fs/bcachefs/btree_iter.h b/fs/bcachefs/btree_iter.h index 5e5e2a5c715c..9676aa335b89 100644 --- a/fs/bcachefs/btree_iter.h +++ b/fs/bcachefs/btree_iter.h @@ -854,6 +854,11 @@ __bch2_btree_iter_peek_upto_and_restart(struct btree_trans *trans, !((_ret) = bkey_err(_k)) && (_k).k; \ bch2_btree_iter_advance(&(_iter))) +#define drop_locks_do(_trans, _do) \ +({ \ + bch2_trans_unlock(_trans); \ + _do ?: bch2_trans_relock(_trans); \ +}) /* new multiple iterator interface: */ void bch2_trans_updates_to_text(struct printbuf *, struct btree_trans *); diff --git a/fs/bcachefs/btree_locking.c b/fs/bcachefs/btree_locking.c index 1f4eca898ab7..23a6d63223af 100644 --- a/fs/bcachefs/btree_locking.c +++ b/fs/bcachefs/btree_locking.c @@ -736,11 +736,8 @@ bool bch2_trans_locked(struct btree_trans *trans) int __bch2_trans_mutex_lock(struct btree_trans *trans, struct mutex *lock) { - int ret; + int ret = drop_locks_do(trans, (mutex_lock(lock), 0)); - bch2_trans_unlock(trans); - mutex_lock(lock); - ret = bch2_trans_relock(trans); if (ret) mutex_unlock(lock); return ret; diff --git a/fs/bcachefs/btree_update_interior.c b/fs/bcachefs/btree_update_interior.c index db0d09b59f2f..4d6c286edb04 100644 --- a/fs/bcachefs/btree_update_interior.c +++ b/fs/bcachefs/btree_update_interior.c @@ -1083,9 +1083,7 @@ bch2_btree_update_start(struct btree_trans *trans, struct btree_path *path, if (flags & BTREE_INSERT_GC_LOCK_HELD) lockdep_assert_held(&c->gc_lock); else if (!down_read_trylock(&c->gc_lock)) { - bch2_trans_unlock(trans); - down_read(&c->gc_lock); - ret = bch2_trans_relock(trans); + ret = drop_locks_do(trans, (down_read(&c->gc_lock), 0)); if (ret) { up_read(&c->gc_lock); return ERR_PTR(ret); @@ -2256,9 +2254,7 @@ int bch2_btree_node_update_key(struct btree_trans *trans, struct btree_iter *ite if (btree_ptr_hash_val(new_key) != b->hash_val) { ret = bch2_btree_cache_cannibalize_lock(c, &cl); if (ret) { - bch2_trans_unlock(trans); - closure_sync(&cl); - ret = bch2_trans_relock(trans); + ret = drop_locks_do(trans, (closure_sync(&cl), 0)); if (ret) return ret; } diff --git a/fs/bcachefs/btree_update_leaf.c b/fs/bcachefs/btree_update_leaf.c index 25d73db9adc6..ea7e32e7d2fd 100644 --- a/fs/bcachefs/btree_update_leaf.c +++ b/fs/bcachefs/btree_update_leaf.c @@ -316,25 +316,11 @@ static noinline int bch2_trans_journal_preres_get_cold(struct btree_trans *trans, unsigned flags, unsigned long trace_ip) { - struct bch_fs *c = trans->c; - int ret; - - bch2_trans_unlock(trans); - - ret = bch2_journal_preres_get(&c->journal, + return drop_locks_do(trans, + bch2_journal_preres_get(&trans->c->journal, &trans->journal_preres, trans->journal_preres_u64s, - (flags & JOURNAL_WATERMARK_MASK)); - if (ret) - return ret; - - ret = bch2_trans_relock(trans); - if (ret) { - trace_and_count(c, trans_restart_journal_preres_get, trans, trace_ip, 0); - return ret; - } - - return 0; + (flags & JOURNAL_WATERMARK_MASK))); } static __always_inline int bch2_trans_journal_res_get(struct btree_trans *trans, @@ -1053,10 +1039,7 @@ bch2_trans_commit_get_rw_cold(struct btree_trans *trans, unsigned flags) test_bit(BCH_FS_STARTED, &c->flags)) return -BCH_ERR_erofs_trans_commit; - bch2_trans_unlock(trans); - - ret = bch2_fs_read_write_early(c) ?: - bch2_trans_relock(trans); + ret = drop_locks_do(trans, bch2_fs_read_write_early(c)); if (ret) return ret;