Skip to content

Commit 280481d

Browse files
author
Kent Overstreet
committed
bcache: Debug code improvements
Couple changes: * Consolidate bch_check_keys() and bch_check_key_order(), and move the checks that only check_key_order() could do to bch_btree_iter_next(). * Get rid of CONFIG_BCACHE_EDEBUG - now, all that code is compiled in when CONFIG_BCACHE_DEBUG is enabled, and there's now a sysfs file to flip on the EDEBUG checks at runtime. * Dropped an old not terribly useful check in rw_unlock(), and refactored/improved a some of the other debug code. Signed-off-by: Kent Overstreet <kmo@daterainc.com>
1 parent e58ff15 commit 280481d

11 files changed

Lines changed: 162 additions & 186 deletions

File tree

‎drivers/md/bcache/Kconfig‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,8 @@ config BCACHE_DEBUG
1313
---help---
1414
Don't select this option unless you're a developer
1515

16-
Enables extra debugging tools (primarily a fuzz tester)
17-
18-
config BCACHE_EDEBUG
19-
bool "Extended runtime checks"
20-
depends on BCACHE
21-
---help---
22-
Don't select this option unless you're a developer
23-
24-
Enables extra runtime checks which significantly affect performance
16+
Enables extra debugging tools, allows expensive runtime checks to be
17+
turned on.
2518

2619
config BCACHE_CLOSURES_DEBUG
2720
bool "Debug closures"

‎drivers/md/bcache/alloc.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,7 @@ long bch_bucket_alloc(struct cache *ca, unsigned watermark, bool wait)
398398
out:
399399
wake_up_process(ca->alloc_thread);
400400

401-
#ifdef CONFIG_BCACHE_EDEBUG
402-
{
401+
if (expensive_debug_checks(ca->set)) {
403402
size_t iter;
404403
long i;
405404

@@ -413,7 +412,7 @@ long bch_bucket_alloc(struct cache *ca, unsigned watermark, bool wait)
413412
fifo_for_each(i, &ca->unused, iter)
414413
BUG_ON(i == r);
415414
}
416-
#endif
415+
417416
b = ca->buckets + r;
418417

419418
BUG_ON(atomic_read(&b->pin) != 1);

‎drivers/md/bcache/bcache.h‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,7 @@ struct cache_set {
690690
unsigned short journal_delay_ms;
691691
unsigned verify:1;
692692
unsigned key_merging_disabled:1;
693+
unsigned expensive_debug_checks:1;
693694
unsigned gc_always_rewrite:1;
694695
unsigned shrinker_disabled:1;
695696
unsigned copy_gc_enabled:1;
@@ -698,15 +699,6 @@ struct cache_set {
698699
struct hlist_head bucket_hash[1 << BUCKET_HASH_BITS];
699700
};
700701

701-
static inline bool key_merging_disabled(struct cache_set *c)
702-
{
703-
#ifdef CONFIG_BCACHE_DEBUG
704-
return c->key_merging_disabled;
705-
#else
706-
return 0;
707-
#endif
708-
}
709-
710702
struct bbio {
711703
unsigned submit_time_us;
712704
union {

‎drivers/md/bcache/bset.c‎

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,43 @@ bool __bch_ptr_invalid(struct cache_set *c, int level, const struct bkey *k)
106106
return true;
107107
}
108108

109+
static bool ptr_bad_expensive_checks(struct btree *b, const struct bkey *k,
110+
unsigned ptr)
111+
{
112+
struct bucket *g = PTR_BUCKET(b->c, k, ptr);
113+
char buf[80];
114+
115+
if (mutex_trylock(&b->c->bucket_lock)) {
116+
if (b->level) {
117+
if (KEY_DIRTY(k) ||
118+
g->prio != BTREE_PRIO ||
119+
(b->c->gc_mark_valid &&
120+
GC_MARK(g) != GC_MARK_METADATA))
121+
goto err;
122+
123+
} else {
124+
if (g->prio == BTREE_PRIO)
125+
goto err;
126+
127+
if (KEY_DIRTY(k) &&
128+
b->c->gc_mark_valid &&
129+
GC_MARK(g) != GC_MARK_DIRTY)
130+
goto err;
131+
}
132+
mutex_unlock(&b->c->bucket_lock);
133+
}
134+
135+
return false;
136+
err:
137+
mutex_unlock(&b->c->bucket_lock);
138+
bch_bkey_to_text(buf, sizeof(buf), k);
139+
btree_bug(b,
140+
"inconsistent pointer %s: bucket %zu pin %i prio %i gen %i last_gc %i mark %llu gc_gen %i",
141+
buf, PTR_BUCKET_NR(b->c, k, ptr), atomic_read(&g->pin),
142+
g->prio, g->gen, g->last_gc, GC_MARK(g), g->gc_gen);
143+
return true;
144+
}
145+
109146
bool bch_ptr_bad(struct btree *b, const struct bkey *k)
110147
{
111148
struct bucket *g;
@@ -133,46 +170,12 @@ bool bch_ptr_bad(struct btree *b, const struct bkey *k)
133170
if (stale)
134171
return true;
135172

136-
#ifdef CONFIG_BCACHE_EDEBUG
137-
if (!mutex_trylock(&b->c->bucket_lock))
138-
continue;
139-
140-
if (b->level) {
141-
if (KEY_DIRTY(k) ||
142-
g->prio != BTREE_PRIO ||
143-
(b->c->gc_mark_valid &&
144-
GC_MARK(g) != GC_MARK_METADATA))
145-
goto bug;
146-
147-
} else {
148-
if (g->prio == BTREE_PRIO)
149-
goto bug;
150-
151-
if (KEY_DIRTY(k) &&
152-
b->c->gc_mark_valid &&
153-
GC_MARK(g) != GC_MARK_DIRTY)
154-
goto bug;
155-
}
156-
mutex_unlock(&b->c->bucket_lock);
157-
#endif
173+
if (expensive_debug_checks(b->c) &&
174+
ptr_bad_expensive_checks(b, k, i))
175+
return true;
158176
}
159177

160178
return false;
161-
#ifdef CONFIG_BCACHE_EDEBUG
162-
bug:
163-
mutex_unlock(&b->c->bucket_lock);
164-
165-
{
166-
char buf[80];
167-
168-
bch_bkey_to_text(buf, sizeof(buf), k);
169-
btree_bug(b,
170-
"inconsistent pointer %s: bucket %zu pin %i prio %i gen %i last_gc %i mark %llu gc_gen %i",
171-
buf, PTR_BUCKET_NR(b->c, k, i), atomic_read(&g->pin),
172-
g->prio, g->gen, g->last_gc, GC_MARK(g), g->gc_gen);
173-
}
174-
return true;
175-
#endif
176179
}
177180

178181
/* Key/pointer manipulation */
@@ -821,16 +824,16 @@ struct bkey *__bch_bset_search(struct btree *b, struct bset_tree *t,
821824
} else
822825
i = bset_search_write_set(b, t, search);
823826

824-
#ifdef CONFIG_BCACHE_EDEBUG
825-
BUG_ON(bset_written(b, t) &&
826-
i.l != t->data->start &&
827-
bkey_cmp(tree_to_prev_bkey(t,
828-
inorder_to_tree(bkey_to_cacheline(t, i.l), t)),
829-
search) > 0);
827+
if (expensive_debug_checks(b->c)) {
828+
BUG_ON(bset_written(b, t) &&
829+
i.l != t->data->start &&
830+
bkey_cmp(tree_to_prev_bkey(t,
831+
inorder_to_tree(bkey_to_cacheline(t, i.l), t)),
832+
search) > 0);
830833

831-
BUG_ON(i.r != end(t->data) &&
832-
bkey_cmp(i.r, search) <= 0);
833-
#endif
834+
BUG_ON(i.r != end(t->data) &&
835+
bkey_cmp(i.r, search) <= 0);
836+
}
834837

835838
while (likely(i.l != i.r) &&
836839
bkey_cmp(i.l, search) <= 0)
@@ -871,12 +874,16 @@ void bch_btree_iter_push(struct btree_iter *iter, struct bkey *k,
871874
}
872875

873876
struct bkey *__bch_btree_iter_init(struct btree *b, struct btree_iter *iter,
874-
struct bkey *search, struct bset_tree *start)
877+
struct bkey *search, struct bset_tree *start)
875878
{
876879
struct bkey *ret = NULL;
877880
iter->size = ARRAY_SIZE(iter->data);
878881
iter->used = 0;
879882

883+
#ifdef CONFIG_BCACHE_DEBUG
884+
iter->b = b;
885+
#endif
886+
880887
for (; start <= &b->sets[b->nsets]; start++) {
881888
ret = bch_bset_search(b, start, search);
882889
bch_btree_iter_push(iter, ret, end(start->data));
@@ -891,6 +898,8 @@ struct bkey *bch_btree_iter_next(struct btree_iter *iter)
891898
struct bkey *ret = NULL;
892899

893900
if (!btree_iter_end(iter)) {
901+
bch_btree_iter_next_check(iter);
902+
894903
ret = iter->data->k;
895904
iter->data->k = bkey_next(iter->data->k);
896905

@@ -1002,7 +1011,6 @@ static void btree_mergesort(struct btree *b, struct bset *out,
10021011
out->keys = last ? (uint64_t *) bkey_next(last) - out->d : 0;
10031012

10041013
pr_debug("sorted %i keys", out->keys);
1005-
bch_check_key_order(b, out);
10061014
}
10071015

10081016
static void __btree_sort(struct btree *b, struct btree_iter *iter,
@@ -1063,15 +1071,15 @@ static void __btree_sort(struct btree *b, struct btree_iter *iter,
10631071

10641072
void bch_btree_sort_partial(struct btree *b, unsigned start)
10651073
{
1066-
size_t oldsize = 0, order = b->page_order, keys = 0;
1074+
size_t order = b->page_order, keys = 0;
10671075
struct btree_iter iter;
1076+
int oldsize = bch_count_data(b);
1077+
10681078
__bch_btree_iter_init(b, &iter, NULL, &b->sets[start]);
10691079

10701080
BUG_ON(b->sets[b->nsets].data == write_block(b) &&
10711081
(b->sets[b->nsets].size || b->nsets));
10721082

1073-
if (b->written)
1074-
oldsize = bch_count_data(b);
10751083

10761084
if (start) {
10771085
unsigned i;
@@ -1087,7 +1095,7 @@ void bch_btree_sort_partial(struct btree *b, unsigned start)
10871095

10881096
__btree_sort(b, &iter, start, order, false);
10891097

1090-
EBUG_ON(b->written && bch_count_data(b) != oldsize);
1098+
EBUG_ON(b->written && oldsize >= 0 && bch_count_data(b) != oldsize);
10911099
}
10921100

10931101
void bch_btree_sort_and_fix_extents(struct btree *b, struct btree_iter *iter)

‎drivers/md/bcache/bset.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@
148148

149149
struct btree_iter {
150150
size_t size, used;
151+
#ifdef CONFIG_BCACHE_DEBUG
152+
struct btree *b;
153+
#endif
151154
struct btree_iter_set {
152155
struct bkey *k, *end;
153156
} data[MAX_BSETS];

‎drivers/md/bcache/btree.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ static void bch_btree_node_read_done(struct btree *b)
216216
iter->size = b->c->sb.bucket_size / b->c->sb.block_size;
217217
iter->used = 0;
218218

219+
#ifdef CONFIG_BCACHE_DEBUG
220+
iter->b = b;
221+
#endif
222+
219223
if (!i->seq)
220224
goto err;
221225

@@ -454,7 +458,7 @@ void bch_btree_node_write(struct btree *b, struct closure *parent)
454458
BUG_ON(b->written >= btree_blocks(b));
455459
BUG_ON(b->written && !i->keys);
456460
BUG_ON(b->sets->data->seq != i->seq);
457-
bch_check_key_order(b, i);
461+
bch_check_keys(b, "writing");
458462

459463
cancel_delayed_work(&b->work);
460464

@@ -1917,7 +1921,7 @@ static bool bch_btree_insert_keys(struct btree *b, struct btree_op *op,
19171921
struct bkey *replace_key)
19181922
{
19191923
bool ret = false;
1920-
unsigned oldsize = bch_count_data(b);
1924+
int oldsize = bch_count_data(b);
19211925

19221926
while (!bch_keylist_empty(insert_keys)) {
19231927
struct bset *i = write_block(b);

‎drivers/md/bcache/btree.h‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,6 @@ static inline void rw_lock(bool w, struct btree *b, int level)
259259

260260
static inline void rw_unlock(bool w, struct btree *b)
261261
{
262-
#ifdef CONFIG_BCACHE_EDEBUG
263-
unsigned i;
264-
265-
if (w && b->key.ptr[0])
266-
for (i = 0; i <= b->nsets; i++)
267-
bch_check_key_order(b, b->sets[i].data);
268-
#endif
269-
270262
if (w)
271263
b->seq++;
272264
(w ? up_write : up_read)(&b->lock);

0 commit comments

Comments
 (0)