Skip to content

Commit f236ea4

Browse files
author
Kent Overstreet
committed
bcachefs: Set PF_MEMALLOC_NOFS when trans->locked
proper lock ordering is: fs_reclaim -> btree node locks Signed-off-by: Kent Overstreet <[email protected]>
1 parent f0f3e51 commit f236ea4

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

fs/bcachefs/btree_iter.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ static int bch2_btree_path_traverse_all(struct btree_trans *trans)
996996

997997
bch2_trans_unlock(trans);
998998
cond_resched();
999-
trans->locked = true;
999+
trans_set_locked(trans);
10001000

10011001
if (unlikely(trans->memory_allocation_failure)) {
10021002
struct closure cl;
@@ -3089,7 +3089,8 @@ u32 bch2_trans_begin(struct btree_trans *trans)
30893089
bch2_trans_srcu_unlock(trans);
30903090

30913091
trans->last_begin_ip = _RET_IP_;
3092-
trans->locked = true;
3092+
3093+
trans_set_locked(trans);
30933094

30943095
if (trans->restarted) {
30953096
bch2_btree_path_traverse_all(trans);
@@ -3159,7 +3160,6 @@ struct btree_trans *__bch2_trans_get(struct bch_fs *c, unsigned fn_idx)
31593160
trans->last_begin_time = local_clock();
31603161
trans->fn_idx = fn_idx;
31613162
trans->locking_wait.task = current;
3162-
trans->locked = true;
31633163
trans->journal_replay_not_finished =
31643164
unlikely(!test_bit(JOURNAL_replay_done, &c->journal.flags)) &&
31653165
atomic_inc_not_zero(&c->journal_keys.ref);
@@ -3193,6 +3193,7 @@ struct btree_trans *__bch2_trans_get(struct bch_fs *c, unsigned fn_idx)
31933193
trans->srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
31943194
trans->srcu_lock_time = jiffies;
31953195
trans->srcu_held = true;
3196+
trans_set_locked(trans);
31963197

31973198
closure_init_stack_release(&trans->ref);
31983199
return trans;

fs/bcachefs/btree_locking.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ static inline int __bch2_trans_relock(struct btree_trans *trans, bool trace)
792792
return bch2_trans_relock_fail(trans, path, &f, trace);
793793
}
794794

795-
trans->locked = true;
795+
trans_set_locked(trans);
796796
out:
797797
bch2_trans_verify_locks(trans);
798798
return 0;
@@ -812,16 +812,14 @@ void bch2_trans_unlock_noassert(struct btree_trans *trans)
812812
{
813813
__bch2_trans_unlock(trans);
814814

815-
trans->locked = false;
816-
trans->last_unlock_ip = _RET_IP_;
815+
trans_set_unlocked(trans);
817816
}
818817

819818
void bch2_trans_unlock(struct btree_trans *trans)
820819
{
821820
__bch2_trans_unlock(trans);
822821

823-
trans->locked = false;
824-
trans->last_unlock_ip = _RET_IP_;
822+
trans_set_unlocked(trans);
825823
}
826824

827825
void bch2_trans_unlock_long(struct btree_trans *trans)

fs/bcachefs/btree_locking.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,28 @@ int bch2_six_check_for_deadlock(struct six_lock *lock, void *p);
193193

194194
/* lock: */
195195

196+
static inline void trans_set_locked(struct btree_trans *trans)
197+
{
198+
if (!trans->locked) {
199+
trans->locked = true;
200+
trans->last_unlock_ip = 0;
201+
202+
trans->pf_memalloc_nofs = (current->flags & PF_MEMALLOC_NOFS) != 0;
203+
current->flags |= PF_MEMALLOC_NOFS;
204+
}
205+
}
206+
207+
static inline void trans_set_unlocked(struct btree_trans *trans)
208+
{
209+
if (trans->locked) {
210+
trans->locked = false;
211+
trans->last_unlock_ip = _RET_IP_;
212+
213+
if (!trans->pf_memalloc_nofs)
214+
current->flags &= ~PF_MEMALLOC_NOFS;
215+
}
216+
}
217+
196218
static inline int __btree_node_lock_nopath(struct btree_trans *trans,
197219
struct btree_bkey_cached_common *b,
198220
enum six_lock_type type,

fs/bcachefs/btree_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ struct btree_trans {
484484
bool lock_may_not_fail:1;
485485
bool srcu_held:1;
486486
bool locked:1;
487+
bool pf_memalloc_nofs:1;
487488
bool write_locked:1;
488489
bool used_mempool:1;
489490
bool in_traverse_all:1;

0 commit comments

Comments
 (0)