* [PATCH 1/3] bcache: avoid invalidating buckets in use
@ 2024-11-18 9:26 mingzhe.zou
2024-11-18 9:26 ` [PATCH 2/3] bcache: fix io error during cache read race mingzhe.zou
0 siblings, 1 reply; 3+ messages in thread
From: mingzhe.zou @ 2024-11-18 9:26 UTC (permalink / raw)
To: colyli; +Cc: linux-bcache, zoumingzhe
From: Mingzhe Zou <mingzhe.zou@easystack.cn>
If the bucket was reused while our bio was in flight, we might
have read the wrong data. Currently, we will reread the data from
the backing device. This not only reduces performance, but also
makes the process more complex.
When the bucket is in use, we hope not to reclaim it.
Signed-off-by: Mingzhe Zou <mingzhe.zou@easystack.cn>
---
drivers/md/bcache/alloc.c | 30 +++++++++++++++++++++---------
drivers/md/bcache/bcache.h | 3 ++-
2 files changed, 23 insertions(+), 10 deletions(-)
diff --git a/drivers/md/bcache/alloc.c b/drivers/md/bcache/alloc.c
index da50f6661bae..32f65d6fc906 100644
--- a/drivers/md/bcache/alloc.c
+++ b/drivers/md/bcache/alloc.c
@@ -134,25 +134,39 @@ bool bch_can_invalidate_bucket(struct cache *ca, struct bucket *b)
!atomic_read(&b->pin) && can_inc_bucket_gen(b));
}
-void __bch_invalidate_one_bucket(struct cache *ca, struct bucket *b)
+bool __bch_invalidate_one_bucket(struct cache *ca, struct bucket *b)
{
lockdep_assert_held(&ca->set->bucket_lock);
BUG_ON(GC_MARK(b) && GC_MARK(b) != GC_MARK_RECLAIMABLE);
+ /*
+ * If the bucket was reused while read bio was in flight, it will
+ * reread the data from the backing device. This will increase latency
+ * and cause other errors. When b->pin is not 0, do not invalidate
+ * the bucket.
+ */
+
+ b->invalidating = 1;
+
+ if (atomic_inc_return(&b->pin) > 1) {
+ atomic_dec(&b->pin);
+ return false;
+ }
+
if (GC_SECTORS_USED(b))
trace_bcache_invalidate(ca, b - ca->buckets);
bch_inc_gen(ca, b);
b->prio = INITIAL_PRIO;
- atomic_inc(&b->pin);
b->reclaimable_in_gc = 0;
+ b->invalidating = 0;
+ return true;
}
static void bch_invalidate_one_bucket(struct cache *ca, struct bucket *b)
{
- __bch_invalidate_one_bucket(ca, b);
-
- fifo_push(&ca->free_inc, b - ca->buckets);
+ if (bch_can_invalidate_bucket(ca, b) && __bch_invalidate_one_bucket(ca, b))
+ fifo_push(&ca->free_inc, b - ca->buckets);
}
/*
@@ -253,8 +267,7 @@ static void invalidate_buckets_fifo(struct cache *ca)
b = ca->buckets + ca->fifo_last_bucket++;
- if (bch_can_invalidate_bucket(ca, b))
- bch_invalidate_one_bucket(ca, b);
+ bch_invalidate_one_bucket(ca, b);
if (++checked >= ca->sb.nbuckets) {
ca->invalidate_needs_gc = 1;
@@ -279,8 +292,7 @@ static void invalidate_buckets_random(struct cache *ca)
b = ca->buckets + n;
- if (bch_can_invalidate_bucket(ca, b))
- bch_invalidate_one_bucket(ca, b);
+ bch_invalidate_one_bucket(ca, b);
if (++checked >= ca->sb.nbuckets / 2) {
ca->invalidate_needs_gc = 1;
diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
index 785b0d9008fa..2777d72e1038 100644
--- a/drivers/md/bcache/bcache.h
+++ b/drivers/md/bcache/bcache.h
@@ -201,6 +201,7 @@ struct bucket {
uint8_t last_gc; /* Most out of date gen in the btree */
uint16_t gc_mark; /* Bitfield used by GC. See below for field */
uint16_t reclaimable_in_gc:1;
+ uint16_t invalidating:1;
};
/*
@@ -981,7 +982,7 @@ uint8_t bch_inc_gen(struct cache *ca, struct bucket *b);
void bch_rescale_priorities(struct cache_set *c, int sectors);
bool bch_can_invalidate_bucket(struct cache *ca, struct bucket *b);
-void __bch_invalidate_one_bucket(struct cache *ca, struct bucket *b);
+bool __bch_invalidate_one_bucket(struct cache *ca, struct bucket *b);
void __bch_bucket_free(struct cache *ca, struct bucket *b);
void bch_bucket_free(struct cache_set *c, struct bkey *k);
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread* [PATCH 2/3] bcache: fix io error during cache read race
2024-11-18 9:26 [PATCH 1/3] bcache: avoid invalidating buckets in use mingzhe.zou
@ 2024-11-18 9:26 ` mingzhe.zou
0 siblings, 0 replies; 3+ messages in thread
From: mingzhe.zou @ 2024-11-18 9:26 UTC (permalink / raw)
To: colyli; +Cc: linux-bcache, zoumingzhe
From: Mingzhe Zou <mingzhe.zou@easystack.cn>
In our production environment, bcache returned IO_ERROR(errno=-5).
These errors always happen during 1M read IO under high pressure
and without any message log. When the error occurred, we stopped
all reading and writing and used 1M read IO to read the entire disk
without any errors. Later we found that cache_read_races of cache_set
is non-zero.
If a large (1M) read bio is split into two or more bios, when one bio
reads dirty data, s->read_dirty_data will be set to true and remain.
If the bucket was reused while our subsequent read bio was in flight,
the read will be unrecoverable(cannot read data from backing).
This patch increases the count for bucket->pin to prevent the bucket
from being reclaimed and reused.
Signed-off-by: Mingzhe Zou <mingzhe.zou@easystack.cn>
---
drivers/md/bcache/request.c | 44 +++++++++++++++++++++++++------------
1 file changed, 30 insertions(+), 14 deletions(-)
diff --git a/drivers/md/bcache/request.c b/drivers/md/bcache/request.c
index af345dc6fde1..3e76ae687045 100644
--- a/drivers/md/bcache/request.c
+++ b/drivers/md/bcache/request.c
@@ -502,12 +502,8 @@ static void bch_cache_read_endio(struct bio *bio)
struct closure *cl = bio->bi_private;
struct search *s = container_of(cl, struct search, cl);
- /*
- * If the bucket was reused while our bio was in flight, we might have
- * read the wrong data. Set s->error but not error so it doesn't get
- * counted against the cache device, but we'll still reread the data
- * from the backing device.
- */
+ BUG_ON(ptr_stale(s->iop.c, &b->key, 0)); // bucket should not be reused
+ atomic_dec(&PTR_BUCKET(s->iop.c, &b->key, 0)->pin);
if (bio->bi_status)
s->iop.status = bio->bi_status;
@@ -520,6 +516,8 @@ static void bch_cache_read_endio(struct bio *bio)
bch_bbio_endio(s->iop.c, bio, bio->bi_status, "reading from cache");
}
+static void backing_request_endio(struct bio *bio);
+
/*
* Read from a single key, handling the initial cache miss if the key starts in
* the middle of the bio
@@ -529,7 +527,6 @@ static int cache_lookup_fn(struct btree_op *op, struct btree *b, struct bkey *k)
struct search *s = container_of(op, struct search, op);
struct bio *n, *bio = &s->bio.bio;
struct bkey *bio_key;
- unsigned int ptr;
if (bkey_cmp(k, &KEY(s->iop.inode, bio->bi_iter.bi_sector, 0)) <= 0)
return MAP_CONTINUE;
@@ -553,20 +550,39 @@ static int cache_lookup_fn(struct btree_op *op, struct btree *b, struct bkey *k)
if (!KEY_SIZE(k))
return MAP_CONTINUE;
- /* XXX: figure out best pointer - for multiple cache devices */
- ptr = 0;
+ atomic_inc(&PTR_BUCKET(s->iop.c, k, 0)->pin);
- PTR_BUCKET(b->c, k, ptr)->prio = INITIAL_PRIO;
-
- if (KEY_DIRTY(k))
- s->read_dirty_data = true;
+ PTR_BUCKET(b->c, k, 0)->prio = INITIAL_PRIO;
n = bio_next_split(bio, min_t(uint64_t, INT_MAX,
KEY_OFFSET(k) - bio->bi_iter.bi_sector),
GFP_NOIO, &s->d->bio_split);
+retry:
+ /*
+ * If the bucket was reused while our bio was in flight, we might have
+ * read the wrong data. Set s->cache_read_races and reread the data
+ * from the backing device.
+ */
+ if (ptr_stale(s->iop.c, k, 0)) {
+ if (PTR_BUCKET(b->c, k, 0)->invalidating)
+ goto retry;
+
+ atomic_dec(&PTR_BUCKET(s->iop.c, k, 0)->pin);
+ atomic_long_inc(&s->iop.c->cache_read_races);
+ pr_warn("%pU cache read race count: %lu", s->iop.c->sb.set_uuid,
+ atomic_long_read(&s->iop.c->cache_read_races));
+
+ n->bi_end_io = backing_request_endio;
+ n->bi_private = &s->cl;
+
+ /* I/O request sent to backing device */
+ closure_bio_submit(s->iop.c, n, &s->cl);
+ return n == bio ? MAP_DONE : MAP_CONTINUE;
+ }
+
bio_key = &container_of(n, struct bbio, bio)->key;
- bch_bkey_copy_single_ptr(bio_key, k, ptr);
+ bch_bkey_copy_single_ptr(bio_key, k, 0);
bch_cut_front(&KEY(s->iop.inode, n->bi_iter.bi_sector, 0), bio_key);
bch_cut_back(&KEY(s->iop.inode, bio_end_sector(n), 0), bio_key);
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH 1/3] bcache: avoid invalidating buckets in use
@ 2024-11-19 3:28 mingzhe.zou
0 siblings, 0 replies; 3+ messages in thread
From: mingzhe.zou @ 2024-11-19 3:28 UTC (permalink / raw)
To: colyli; +Cc: linux-bcache, dongsheng.yang, zoumingzhe
From: Mingzhe Zou <mingzhe.zou@easystack.cn>
If the bucket was reused while our bio was in flight, we might
have read the wrong data. Currently, we will reread the data from
the backing device. This not only reduces performance, but also
makes the process more complex.
When the bucket is in use, we hope not to reclaim it.
Signed-off-by: Mingzhe Zou <mingzhe.zou@easystack.cn>
---
drivers/md/bcache/alloc.c | 30 +++++++++++++++++++++---------
drivers/md/bcache/bcache.h | 3 ++-
2 files changed, 23 insertions(+), 10 deletions(-)
diff --git a/drivers/md/bcache/alloc.c b/drivers/md/bcache/alloc.c
index da50f6661bae..32f65d6fc906 100644
--- a/drivers/md/bcache/alloc.c
+++ b/drivers/md/bcache/alloc.c
@@ -134,25 +134,39 @@ bool bch_can_invalidate_bucket(struct cache *ca, struct bucket *b)
!atomic_read(&b->pin) && can_inc_bucket_gen(b));
}
-void __bch_invalidate_one_bucket(struct cache *ca, struct bucket *b)
+bool __bch_invalidate_one_bucket(struct cache *ca, struct bucket *b)
{
lockdep_assert_held(&ca->set->bucket_lock);
BUG_ON(GC_MARK(b) && GC_MARK(b) != GC_MARK_RECLAIMABLE);
+ /*
+ * If the bucket was reused while read bio was in flight, it will
+ * reread the data from the backing device. This will increase latency
+ * and cause other errors. When b->pin is not 0, do not invalidate
+ * the bucket.
+ */
+
+ b->invalidating = 1;
+
+ if (atomic_inc_return(&b->pin) > 1) {
+ atomic_dec(&b->pin);
+ return false;
+ }
+
if (GC_SECTORS_USED(b))
trace_bcache_invalidate(ca, b - ca->buckets);
bch_inc_gen(ca, b);
b->prio = INITIAL_PRIO;
- atomic_inc(&b->pin);
b->reclaimable_in_gc = 0;
+ b->invalidating = 0;
+ return true;
}
static void bch_invalidate_one_bucket(struct cache *ca, struct bucket *b)
{
- __bch_invalidate_one_bucket(ca, b);
-
- fifo_push(&ca->free_inc, b - ca->buckets);
+ if (bch_can_invalidate_bucket(ca, b) && __bch_invalidate_one_bucket(ca, b))
+ fifo_push(&ca->free_inc, b - ca->buckets);
}
/*
@@ -253,8 +267,7 @@ static void invalidate_buckets_fifo(struct cache *ca)
b = ca->buckets + ca->fifo_last_bucket++;
- if (bch_can_invalidate_bucket(ca, b))
- bch_invalidate_one_bucket(ca, b);
+ bch_invalidate_one_bucket(ca, b);
if (++checked >= ca->sb.nbuckets) {
ca->invalidate_needs_gc = 1;
@@ -279,8 +292,7 @@ static void invalidate_buckets_random(struct cache *ca)
b = ca->buckets + n;
- if (bch_can_invalidate_bucket(ca, b))
- bch_invalidate_one_bucket(ca, b);
+ bch_invalidate_one_bucket(ca, b);
if (++checked >= ca->sb.nbuckets / 2) {
ca->invalidate_needs_gc = 1;
diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
index 785b0d9008fa..2777d72e1038 100644
--- a/drivers/md/bcache/bcache.h
+++ b/drivers/md/bcache/bcache.h
@@ -201,6 +201,7 @@ struct bucket {
uint8_t last_gc; /* Most out of date gen in the btree */
uint16_t gc_mark; /* Bitfield used by GC. See below for field */
uint16_t reclaimable_in_gc:1;
+ uint16_t invalidating:1;
};
/*
@@ -981,7 +982,7 @@ uint8_t bch_inc_gen(struct cache *ca, struct bucket *b);
void bch_rescale_priorities(struct cache_set *c, int sectors);
bool bch_can_invalidate_bucket(struct cache *ca, struct bucket *b);
-void __bch_invalidate_one_bucket(struct cache *ca, struct bucket *b);
+bool __bch_invalidate_one_bucket(struct cache *ca, struct bucket *b);
void __bch_bucket_free(struct cache *ca, struct bucket *b);
void bch_bucket_free(struct cache_set *c, struct bkey *k);
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-11-19 9:12 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-18 9:26 [PATCH 1/3] bcache: avoid invalidating buckets in use mingzhe.zou
2024-11-18 9:26 ` [PATCH 2/3] bcache: fix io error during cache read race mingzhe.zou
-- strict thread matches above, loose matches on Subject: below --
2024-11-19 3:28 [PATCH 1/3] bcache: avoid invalidating buckets in use mingzhe.zou
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox