Skip to contentSkip to conversation

bcache: call force_wake_up_gc() if necessary in check_should_bypass()

PATCH In review Coly Li 2 participants 8 messages May 27, 2024 – May 29, 2024 bcache (block layer cache)

lore
Share & tools
lore

Coly Li

author
#0 lore raw

PATCH 2/3 bcache: call force_wake_up_gc() if necessary in check_should_bypass()

If there are extreme heavy write I/O continuously hit on relative small cache device (512GB in my testing), it is possible to make counter c->gc_stats.in_use continue to increase and exceed CUTOFF_CACHE_ADD.

If 'c->gc_stats.in_use > CUTOFF_CACHE_ADD' happens, all following write requests will bypass the cache device because check_should_bypass() returns 'true'. Because all writes bypass the cache device, counter c->sectors_to_gc has no chance to be negative value, and garbage collection thread won't be waken up even the whole cache becomes clean after writeback accomplished. The aftermath is that all write I/Os go directly into backing device even the cache device is clean.

To avoid the above situation, this patch uses a quite conservative way to fix: if 'c->gc_stats.in_use > CUTOFF_CACHE_ADD' happens, only wakes up garbage collection thread when the whole cache device is clean.

Before the fix, the writes-always-bypass situation happens after 10+ hours write I/O pressure on 512GB Intel optane memory which acts as cache device. After this fix, such situation doesn't happen after 36+ hours testing.

---
 drivers/md/bcache/request.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
drivers/md/bcache/request.c +15 -1
index 83d112bd2b1c..af345dc6fde1 100644
@@ -369,10 +369,24 @@ static bool check_should_bypass(struct cached_dev *dc, struct bio *bio)
 	struct io *i;
 
 	if (test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) ||
-	    c->gc_stats.in_use > CUTOFF_CACHE_ADD ||
 	    (bio_op(bio) == REQ_OP_DISCARD))
 		goto skip;
 
+	if (c->gc_stats.in_use > CUTOFF_CACHE_ADD) {
+		/*
+		 * If cached buckets are all clean now, 'true' will be
+		 * returned and all requests will bypass the cache device.
+		 * Then c->sectors_to_gc has no chance to be negative, and
+		 * gc thread won't wake up and caching won't work forever.
+		 * Here call force_wake_up_gc() to avoid such aftermath.
+		 */
+		if (BDEV_STATE(&dc->sb) == BDEV_STATE_CLEAN &&
+		    c->gc_mark_valid)
+			force_wake_up_gc(c);
+
+		goto skip;
+	}
+
 	if (mode == CACHE_MODE_NONE ||
 	    (mode == CACHE_MODE_WRITEAROUND &&
 	     op_is_write(bio_op(bio))))
Signature · 1 line
2.35.3
author
#1 lore raw

PATCH 3/3 bcache: code cleanup in __bch_bucket_alloc_set()

In __bch_bucket_alloc_set() the lines after lable 'err:' indeed do nothing useful after multiple cache devices are removed from bcache code. This cleanup patch drops the useless code to save a bit CPU cycles.

---
 drivers/md/bcache/alloc.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/md/bcache/alloc.c +2 -6
index 32a46343097d..48ce750bf70a 100644
@@ -498,8 +498,8 @@ int __bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
 
 	ca = c->cache;
 	b = bch_bucket_alloc(ca, reserve, wait);
-	if (b == -1)
-		goto err;
+	if (b < 0)
+		return -1;
 
 	k->ptr[0] = MAKE_PTR(ca->buckets[b].gen,
 			     bucket_to_sector(c, b),
@@ -508,10 +508,6 @@ int __bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
 	SET_KEY_PTRS(k, 1);
 
 	return 0;
-err:
-	bch_bucket_free(c, k);
-	bkey_put(c, k);
-	return -1;
 }
 
 int bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
Signature · 1 line
2.35.3
#2 lore raw

Re: [PATCH 3/3] bcache: code cleanup in __bch_bucket_alloc_set()

Quoted 26 lines · Coly Li

In __bch_bucket_alloc_set() the lines after lable 'err:' indeed do nothing useful after multiple cache devices are removed from bcache code. This cleanup patch drops the useless code to save a bit CPU cycles.

---
 drivers/md/bcache/alloc.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/md/bcache/alloc.c +2 -2
index 32a46343097d..48ce750bf70a 100644
@@ -498,8 +498,8 @@ int __bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
 
 	ca = c->cache;
 	b = bch_bucket_alloc(ca, reserve, wait);
-	if (b == -1)
-		goto err;
+	if (b < 0)
+		return -1;
 
 	k->ptr[0] = MAKE_PTR(ca->buckets[b].gen,
 			     bucket_to_sector(c, b),

drivers/md/bcache/alloc.c int __bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve, · line 513

Quoted 6 lines
drivers/md/bcache/alloc.c +0 -3
@@ -508,10 +508,6 @@ int __bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
 	SET_KEY_PTRS(k, 1);
 
 	return 0;
-err:
-	bch_bucket_free(c, k);
-	bkey_put(c, k);

Is there a matching "get" somewhere that should be removed, too?

--
Eric Wheeler

Quoted 9 lines
drivers/md/bcache/alloc.c +0 -1
-	return -1;
 }
 
 int bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
Signature · 1 line
2.35.3
author
#3 lore raw

Re: [PATCH 3/3] bcache: code cleanup in __bch_bucket_alloc_set()

Quoted 33 lines

2024年5月28日 06:33,Eric Wheeler <[email protected]> 写道:

Quoted 28 lines · Coly Li

In __bch_bucket_alloc_set() the lines after lable 'err:' indeed do nothing useful after multiple cache devices are removed from bcache code. This cleanup patch drops the useless code to save a bit CPU cycles.

---
drivers/md/bcache/alloc.c | 8 ++------

1 file changed, 2 insertions(+), 6 deletions(-)
drivers/md/bcache/alloc.c +2 -2
index 32a46343097d..48ce750bf70a 100644
@@ -498,8 +498,8 @@ int __bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
 
 ca = c->cache;
 b = bch_bucket_alloc(ca, reserve, wait);
- if (b == -1)
- goto err;
+ if (b < 0)
+ return -1;
 
 k->ptr[0] = MAKE_PTR(ca->buckets[b].gen,
    bucket_to_sector(c, b),
@@ -508,10 +508,6 @@ int __bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
 SET_KEY_PTRS(k, 1);
 
Quoted 7 lines

drivers/md/bcache/alloc.c int __bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve, · line 513

Quoted 4 lines
drivers/md/bcache/alloc.c +0 -3
 return 0;
-err:
- bch_bucket_free(c, k);
- bkey_put(c, k);

Is there a matching "get" somewhere that should be removed, too?

No, it is unnecessary and should be avoided. Because k is ZERO_KEY here, calling bch_bucket_free() and bkey_put() are NOOP indeed.

Coly Li

#4 lore raw
In reply to Coly Li
Quoted 12 lines · Coly Li

If there are extreme heavy write I/O continuously hit on relative small cache device (512GB in my testing), it is possible to make counter c->gc_stats.in_use continue to increase and exceed CUTOFF_CACHE_ADD.

If 'c->gc_stats.in_use > CUTOFF_CACHE_ADD' happens, all following write requests will bypass the cache device because check_should_bypass() returns 'true'. Because all writes bypass the cache device, counter c->sectors_to_gc has no chance to be negative value, and garbage collection thread won't be waken up even the whole cache becomes clean after writeback accomplished. The aftermath is that all write I/Os go directly into backing device even the cache device is clean.

Quoted 3 lines

To avoid the above situation, this patch uses a quite conservative way to fix: if 'c->gc_stats.in_use > CUTOFF_CACHE_ADD' happens, only wakes up garbage collection thread when the whole cache device is clean.

Nice fix.

If I understand correctly, even with this fix, bcache can reach a point where it must wait until garbage collection frees a bucket (via force_wake_up_gc) before buckets can be used again. Waiting to call force_wake_up_gc until c->gc_stats.in_use exceeds CUTOFF_CACHE_ADD may not respond as fast as it could, and IO latency is important.

It may be a good idea to do c->gc_stats.in_use > CUTOFF_CACHE_ADD/2 to start garbage collection when it is half-way "full".

Reaching 50% is still quite conservative, but if you want to wait longer, then even 80% or 90% would be fine; however, I think 100% is too far. We want to avoid the case where bcache is completely "out" of buckets and we have to wait for garbage collection latency before a cache bucket can fill, since buckets should be available.

For example on our system we have 736824 buckets available:

	# cat /sys/devices/virtual/block/dm-9/bcache/nbuckets
	736824

There should be no reason to wait until all buckets are exhausted. Forcing garbage collection at 50% (368412 buckets "in use") would be good house keeping.

You know this code very well so if I have misinterpreted something here, then please fill me in on the details.

--
Eric Wheeler

Quoted 46 lines

Before the fix, the writes-always-bypass situation happens after 10+ hours write I/O pressure on 512GB Intel optane memory which acts as cache device. After this fix, such situation doesn't happen after 36+ hours testing.

---
 drivers/md/bcache/request.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
drivers/md/bcache/request.c +15 -1
index 83d112bd2b1c..af345dc6fde1 100644
@@ -369,10 +369,24 @@ static bool check_should_bypass(struct cached_dev *dc, struct bio *bio)
 	struct io *i;
 
 	if (test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) ||
-	    c->gc_stats.in_use > CUTOFF_CACHE_ADD ||
 	    (bio_op(bio) == REQ_OP_DISCARD))
 		goto skip;
 
+	if (c->gc_stats.in_use > CUTOFF_CACHE_ADD) {
+		/*
+		 * If cached buckets are all clean now, 'true' will be
+		 * returned and all requests will bypass the cache device.
+		 * Then c->sectors_to_gc has no chance to be negative, and
+		 * gc thread won't wake up and caching won't work forever.
+		 * Here call force_wake_up_gc() to avoid such aftermath.
+		 */
+		if (BDEV_STATE(&dc->sb) == BDEV_STATE_CLEAN &&
+		    c->gc_mark_valid)
+			force_wake_up_gc(c);
+
+		goto skip;
+	}
+
 	if (mode == CACHE_MODE_NONE ||
 	    (mode == CACHE_MODE_WRITEAROUND &&
 	     op_is_write(bio_op(bio))))
Signature · 1 line
2.35.3
author
#5 lore raw
Quoted 22 lines

2024年5月28日 06:31,Eric Wheeler <[email protected]> 写道:

Quoted 12 lines · Coly Li

If there are extreme heavy write I/O continuously hit on relative small cache device (512GB in my testing), it is possible to make counter c->gc_stats.in_use continue to increase and exceed CUTOFF_CACHE_ADD.

If 'c->gc_stats.in_use > CUTOFF_CACHE_ADD' happens, all following write requests will bypass the cache device because check_should_bypass() returns 'true'. Because all writes bypass the cache device, counter c->sectors_to_gc has no chance to be negative value, and garbage collection thread won't be waken up even the whole cache becomes clean after writeback accomplished. The aftermath is that all write I/Os go directly into backing device even the cache device is clean.

Quoted 3 lines

To avoid the above situation, this patch uses a quite conservative way to fix: if 'c->gc_stats.in_use > CUTOFF_CACHE_ADD' happens, only wakes up garbage collection thread when the whole cache device is clean.

Quoted 8 lines

Nice fix.

If I understand correctly, even with this fix, bcache can reach a point where it must wait until garbage collection frees a bucket (via force_wake_up_gc) before buckets can be used again. Waiting to call force_wake_up_gc until c->gc_stats.in_use exceeds CUTOFF_CACHE_ADD may not respond as fast as it could, and IO latency is important.

CUTOFF_CACHE_ADD is not for this purpose.
GC is triggered by c->sectors_to_gc, it works as
- initialized as 1/16 size of cache device.
- every allocation decreases cached size from it.
- once c->sectors_go_gc is negative value, wakeup gc thread and reset the value to 1/16 size of cache device.

CUTOFF_CACHE_ADD is to avoid something like no-space deadlock in cache space. If cache space is allocated more than CUTOFF_CACHE_ADD (95%), cache space will not be allocated out anymore and all read/write will bypass and go directly into backing device. In my testing, after 10+ hours I can see c->gc_stats.in_use is 96%. Which is a bit more than 95%, but c->sectors_go_gc is still larger than 0. This is how the forever-bypass happens. It has nothing to do with the latency of neither I/O nor gc.

Quoted 3 lines

It may be a good idea to do c->gc_stats.in_use > CUTOFF_CACHE_ADD/2 to start garbage collection when it is half-way "full".

No, it is not designed to work in this way. By the above change, all I/O will bypass the cache device and go directly into backing device when cache device is occupied only 50% space.

Quoted 10 lines

Reaching 50% is still quite conservative, but if you want to wait longer, then even 80% or 90% would be fine; however, I think 100% is too far. We want to avoid the case where bcache is completely "out" of buckets and we have to wait for garbage collection latency before a cache bucket can fill, since buckets should be available.

For example on our system we have 736824 buckets available:

# cat /sys/devices/virtual/block/dm-9/bcache/nbuckets
736824
Quoted 6 lines

There should be no reason to wait until all buckets are exhausted. Forcing garbage collection at 50% (368412 buckets "in use") would be good house keeping.

You know this code very well so if I have misinterpreted something here, then please fill me in on the details.

As I said, this patch is just to avoid a forever-bypass condition, and this is an extreme condition which is rare to happen for normal workload.

Thanks.

Coly Li

Reviewed-by
#6 lore raw
Quoted 35 lines · Coly Li
Quoted 20 lines

2024年5月28日 06:31,Eric Wheeler <[email protected]> 写道:

Quoted 12 lines · Coly Li

If there are extreme heavy write I/O continuously hit on relative small cache device (512GB in my testing), it is possible to make counter c->gc_stats.in_use continue to increase and exceed CUTOFF_CACHE_ADD.

If 'c->gc_stats.in_use > CUTOFF_CACHE_ADD' happens, all following write requests will bypass the cache device because check_should_bypass() returns 'true'. Because all writes bypass the cache device, counter c->sectors_to_gc has no chance to be negative value, and garbage collection thread won't be waken up even the whole cache becomes clean after writeback accomplished. The aftermath is that all write I/Os go directly into backing device even the cache device is clean.

Quoted 3 lines

To avoid the above situation, this patch uses a quite conservative way to fix: if 'c->gc_stats.in_use > CUTOFF_CACHE_ADD' happens, only wakes up garbage collection thread when the whole cache device is clean.

Quoted 8 lines

Nice fix.

If I understand correctly, even with this fix, bcache can reach a point where it must wait until garbage collection frees a bucket (via force_wake_up_gc) before buckets can be used again. Waiting to call force_wake_up_gc until c->gc_stats.in_use exceeds CUTOFF_CACHE_ADD may not respond as fast as it could, and IO latency is important.

CUTOFF_CACHE_ADD is not for this purpose.
GC is triggered by c->sectors_to_gc, it works as
- initialized as 1/16 size of cache device.
- every allocation decreases cached size from it.
- once c->sectors_go_gc is negative value, wakeup gc thread and reset the value to 1/16 size of cache device.

Quoted 8 lines

CUTOFF_CACHE_ADD is to avoid something like no-space deadlock in cache space. If cache space is allocated more than CUTOFF_CACHE_ADD (95%), cache space will not be allocated out anymore and all read/write will bypass and go directly into backing device. In my testing, after 10+ hours I can see c->gc_stats.in_use is 96%. Which is a bit more than 95%, but c->sectors_go_gc is still larger than 0. This is how the forever-bypass happens. It has nothing to do with the latency of neither I/O nor gc.

Understood, thank you for the explanation!

You said that this bug exists an older version even though it is difficult trigger. Perhaps it is a good idea to CC stable:

Also,

--
Eric Wheeler

Quoted 32 lines
Quoted 5 lines

It may be a good idea to do c->gc_stats.in_use > CUTOFF_CACHE_ADD/2 to start garbage collection when it is half-way "full".

No, it is not designed to work in this way. By the above change, all I/O will bypass the cache device and go directly into backing device when cache device is occupied only 50% space.

Quoted 10 lines

Reaching 50% is still quite conservative, but if you want to wait longer, then even 80% or 90% would be fine; however, I think 100% is too far. We want to avoid the case where bcache is completely "out" of buckets and we have to wait for garbage collection latency before a cache bucket can fill, since buckets should be available.

For example on our system we have 736824 buckets available:

# cat /sys/devices/virtual/block/dm-9/bcache/nbuckets
736824
Quoted 6 lines

There should be no reason to wait until all buckets are exhausted. Forcing garbage collection at 50% (368412 buckets "in use") would be good house keeping.

You know this code very well so if I have misinterpreted something here, then please fill me in on the details.

As I said, this patch is just to avoid a forever-bypass condition, and this is an extreme condition which is rare to happen for normal workload.

Thanks.

Coly Li

author
#7 lore raw
Quoted 49 lines

2024年5月29日 08:16,Eric Wheeler <[email protected]> 写道:

Quoted 35 lines · Coly Li
Quoted 20 lines

2024年5月28日 06:31,Eric Wheeler <[email protected]> 写道:

Quoted 12 lines · Coly Li

If there are extreme heavy write I/O continuously hit on relative small cache device (512GB in my testing), it is possible to make counter c->gc_stats.in_use continue to increase and exceed CUTOFF_CACHE_ADD.

If 'c->gc_stats.in_use > CUTOFF_CACHE_ADD' happens, all following write requests will bypass the cache device because check_should_bypass() returns 'true'. Because all writes bypass the cache device, counter c->sectors_to_gc has no chance to be negative value, and garbage collection thread won't be waken up even the whole cache becomes clean after writeback accomplished. The aftermath is that all write I/Os go directly into backing device even the cache device is clean.

Quoted 3 lines

To avoid the above situation, this patch uses a quite conservative way to fix: if 'c->gc_stats.in_use > CUTOFF_CACHE_ADD' happens, only wakes up garbage collection thread when the whole cache device is clean.

Quoted 8 lines

Nice fix.

If I understand correctly, even with this fix, bcache can reach a point where it must wait until garbage collection frees a bucket (via force_wake_up_gc) before buckets can be used again. Waiting to call force_wake_up_gc until c->gc_stats.in_use exceeds CUTOFF_CACHE_ADD may not respond as fast as it could, and IO latency is important.

CUTOFF_CACHE_ADD is not for this purpose.
GC is triggered by c->sectors_to_gc, it works as
- initialized as 1/16 size of cache device.
- every allocation decreases cached size from it.
- once c->sectors_go_gc is negative value, wakeup gc thread and reset the value to 1/16 size of cache device.

Quoted 8 lines

CUTOFF_CACHE_ADD is to avoid something like no-space deadlock in cache space. If cache space is allocated more than CUTOFF_CACHE_ADD (95%), cache space will not be allocated out anymore and all read/write will bypass and go directly into backing device. In my testing, after 10+ hours I can see c->gc_stats.in_use is 96%. Which is a bit more than 95%, but c->sectors_go_gc is still larger than 0. This is how the forever-bypass happens. It has nothing to do with the latency of neither I/O nor gc.

Quoted 7 lines

Understood, thank you for the explanation!

You said that this bug exists an older version even though it is difficult trigger. Perhaps it is a good idea to CC stable:

It is unnecessary to Cc stable, manually writing sysfs file trigger_gc can avoid the always-bypass situation. This patch is to make the default configuration works without extra operation.

Quoted 2 lines

Also,

Thanks for the review. Maybe it is a bit late to add the tag, I just found this series was in linux-block tree already and will show up in next -rc quite soon.

Thanks.

Coly Li

[snipped]