Skip to content

Commit 9cf6b84

Browse files
author
Kent Overstreet
committed
bcachefs: CONFIG_BCACHEFS_INJECT_TRANSACTION_RESTARTS
Incorrectly handled transaction restarts can be a source of heisenbugs; add a mode where we randomly inject them to shake them out. Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
1 parent 9f734cd commit 9cf6b84

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

fs/bcachefs/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ config BCACHEFS_DEBUG
6161
The resulting code will be significantly slower than normal; you
6262
probably shouldn't select this option unless you're a developer.
6363

64+
config BCACHEFS_INJECT_TRANSACTION_RESTARTS
65+
bool "Randomly inject transaction restarts"
66+
depends on BCACHEFS_DEBUG
67+
help
68+
Randomly inject transaction restarts in a few core paths - may have a
69+
significant performance penalty
70+
6471
config BCACHEFS_TESTS
6572
bool "bcachefs unit and performance tests"
6673
depends on BCACHEFS_FS

fs/bcachefs/btree_iter.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2357,6 +2357,12 @@ struct bkey_s_c bch2_btree_iter_peek_max(struct btree_iter *iter, struct bpos en
23572357
bch2_btree_iter_verify_entry_exit(iter);
23582358
EBUG_ON((iter->flags & BTREE_ITER_filter_snapshots) && bkey_eq(end, POS_MAX));
23592359

2360+
ret = trans_maybe_inject_restart(trans, _RET_IP_);
2361+
if (unlikely(ret)) {
2362+
k = bkey_s_c_err(ret);
2363+
goto out_no_locked;
2364+
}
2365+
23602366
if (iter->update_path) {
23612367
bch2_path_put_nokeep(trans, iter->update_path,
23622368
iter->flags & BTREE_ITER_intent);
@@ -2622,6 +2628,12 @@ struct bkey_s_c bch2_btree_iter_peek_prev_min(struct btree_iter *iter, struct bp
26222628
bch2_btree_iter_verify_entry_exit(iter);
26232629
EBUG_ON((iter->flags & BTREE_ITER_filter_snapshots) && bpos_eq(end, POS_MIN));
26242630

2631+
int ret = trans_maybe_inject_restart(trans, _RET_IP_);
2632+
if (unlikely(ret)) {
2633+
k = bkey_s_c_err(ret);
2634+
goto out_no_locked;
2635+
}
2636+
26252637
while (1) {
26262638
k = __bch2_btree_iter_peek_prev(iter, search_key);
26272639
if (unlikely(!k.k))
@@ -2749,6 +2761,12 @@ struct bkey_s_c bch2_btree_iter_peek_slot(struct btree_iter *iter)
27492761
bch2_btree_iter_verify_entry_exit(iter);
27502762
EBUG_ON(btree_iter_path(trans, iter)->level && (iter->flags & BTREE_ITER_with_key_cache));
27512763

2764+
ret = trans_maybe_inject_restart(trans, _RET_IP_);
2765+
if (unlikely(ret)) {
2766+
k = bkey_s_c_err(ret);
2767+
goto out_no_locked;
2768+
}
2769+
27522770
/* extents can't span inode numbers: */
27532771
if ((iter->flags & BTREE_ITER_is_extents) &&
27542772
unlikely(iter->pos.offset == KEY_OFFSET_MAX)) {
@@ -3106,6 +3124,10 @@ void *__bch2_trans_kmalloc(struct btree_trans *trans, size_t size)
31063124

31073125
WARN_ON_ONCE(new_bytes > BTREE_TRANS_MEM_MAX);
31083126

3127+
ret = trans_maybe_inject_restart(trans, _RET_IP_);
3128+
if (ret)
3129+
return ERR_PTR(ret);
3130+
31093131
struct btree_transaction_stats *s = btree_trans_stats(trans);
31103132
s->max_mem = max(s->max_mem, new_bytes);
31113133

@@ -3163,7 +3185,8 @@ void *__bch2_trans_kmalloc(struct btree_trans *trans, size_t size)
31633185

31643186
if (old_bytes) {
31653187
trace_and_count(c, trans_restart_mem_realloced, trans, _RET_IP_, new_bytes);
3166-
return ERR_PTR(btree_trans_restart(trans, BCH_ERR_transaction_restart_mem_realloced));
3188+
return ERR_PTR(btree_trans_restart_ip(trans,
3189+
BCH_ERR_transaction_restart_mem_realloced, _RET_IP_));
31673190
}
31683191
out_change_top:
31693192
p = trans->mem + trans->mem_top;
@@ -3271,6 +3294,14 @@ u32 bch2_trans_begin(struct btree_trans *trans)
32713294

32723295
trans->last_begin_ip = _RET_IP_;
32733296

3297+
#ifdef CONFIG_BCACHEFS_INJECT_TRANSACTION_RESTARTS
3298+
if (trans->restarted) {
3299+
trans->restart_count_this_trans++;
3300+
} else {
3301+
trans->restart_count_this_trans = 0;
3302+
}
3303+
#endif
3304+
32743305
trans_set_locked(trans, false);
32753306

32763307
if (trans->restarted) {

fs/bcachefs/btree_iter.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,18 @@ static int btree_trans_restart(struct btree_trans *trans, int err)
355355
return btree_trans_restart_ip(trans, err, _THIS_IP_);
356356
}
357357

358+
static inline int trans_maybe_inject_restart(struct btree_trans *trans, unsigned long ip)
359+
{
360+
#ifdef CONFIG_BCACHEFS_INJECT_TRANSACTION_RESTARTS
361+
if (!(ktime_get_ns() & ~(~0ULL << min(63, (10 + trans->restart_count_this_trans))))) {
362+
trace_and_count(trans->c, trans_restart_injected, trans, ip);
363+
return btree_trans_restart_ip(trans,
364+
BCH_ERR_transaction_restart_fault_inject, ip);
365+
}
366+
#endif
367+
return 0;
368+
}
369+
358370
bool bch2_btree_node_upgrade(struct btree_trans *,
359371
struct btree_path *, unsigned);
360372

fs/bcachefs/btree_trans_commit.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,10 @@ int __bch2_trans_commit(struct btree_trans *trans, unsigned flags)
999999

10001000
bch2_trans_verify_not_unlocked_or_in_restart(trans);
10011001

1002+
ret = trans_maybe_inject_restart(trans, _RET_IP_);
1003+
if (unlikely(ret))
1004+
goto out_reset;
1005+
10021006
if (!trans->nr_updates &&
10031007
!trans->journal_entries_u64s)
10041008
goto out_reset;

fs/bcachefs/btree_types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,9 @@ struct btree_trans {
509509
bool notrace_relock_fail:1;
510510
enum bch_errcode restarted:16;
511511
u32 restart_count;
512+
#ifdef CONFIG_BCACHEFS_INJECT_TRANSACTION_RESTARTS
513+
u32 restart_count_this_trans;
514+
#endif
512515

513516
u64 last_begin_time;
514517
unsigned long last_begin_ip;

0 commit comments

Comments
 (0)