]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
bcachefs: Unlock in bch2_trans_begin() if we've held locks more than 10us
authorKent Overstreet <kent.overstreet@gmail.com>
Wed, 13 Jul 2022 10:03:21 +0000 (06:03 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:09:35 +0000 (17:09 -0400)
We try to ensure we never hold btree locks for too long - bcachefs tries
to be soft realtime. This adds a check when restarting a transaction,
where a transaction restart is cheap - if we've been holding locks for
too long, drop and retake them.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
fs/bcachefs/btree_iter.c
fs/bcachefs/btree_types.h

index b90aff2ad7755b1d364b0de57c34d512dba4d7f0..68d9d8ee6f976e484043526208104c551dd2f527 100644 (file)
@@ -3224,12 +3224,19 @@ void bch2_trans_begin(struct btree_trans *trans)
                        path->preserve = false;
        }
 
-       bch2_trans_cond_resched(trans);
+       if (!trans->restarted &&
+           (need_resched() ||
+            ktime_get_ns() - trans->last_begin_time > BTREE_TRANS_MAX_LOCK_HOLD_TIME_NS)) {
+               bch2_trans_unlock(trans);
+               cond_resched();
+               bch2_trans_relock(trans);
+       }
 
        if (trans->restarted)
                bch2_btree_path_traverse_all(trans);
 
        trans->restarted = false;
+       trans->last_begin_time = ktime_get_ns();
 }
 
 static void bch2_trans_alloc_paths(struct btree_trans *trans, struct bch_fs *c)
@@ -3259,6 +3266,7 @@ void __bch2_trans_init(struct btree_trans *trans, struct bch_fs *c,
        memset(trans, 0, sizeof(*trans));
        trans->c                = c;
        trans->fn               = fn;
+       trans->last_begin_time  = ktime_get_ns();
        trans->task             = current;
        trans->journal_replay_not_finished =
                !test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags);
index 2eb8cc11aec4057cc32efec49a8df1830f073181..b184ec512499e51ae8bc1c4c7a4fdbcab710b322 100644 (file)
@@ -382,10 +382,13 @@ struct btree_trans_commit_hook {
 
 #define BTREE_TRANS_MEM_MAX    (1U << 16)
 
+#define BTREE_TRANS_MAX_LOCK_HOLD_TIME_NS      10000
+
 struct btree_trans {
        struct bch_fs           *c;
        const char              *fn;
        struct list_head        list;
+       u64                     last_begin_time;
        struct btree            *locking;
        unsigned                locking_path_idx;
        struct bpos             locking_pos;