Skip to contentSkip to conversation

bcache: limit create flash device size

PATCH Superseded [email protected] 2 participants 3 messages Sep 7, 2022 – Sep 8, 2022 bcache (block layer cache)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bcache: limit create flash device size

From: mingzhe <[email protected]>

Currently, size is specified and not checked when creating a flash device. This will cause a problem, IO maybe hang when creating a flash device with the actual size of the device.

```
	if (attr == &sysfs_flash_vol_create) {
		int r;
		uint64_t v;

		strtoi_h_or_return(buf, v);

		r = bch_flash_dev_create(c, v);
		if (r)
			return r;
	}
```

Because the flash device needs some space for superblock, journal and btree. If the size of data reaches the available size, the new IO cannot allocate space and will hang. At this time, the gc thread will be started frequently.

Even more unreasonable, we can create flash devices larger than actual size.

```
[root@zou ~]# echo 2G > /sys/block/vdb/bcache/set/flash_vol_create
[root@zou ~]# lsblk /dev/vdb
NAME       MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vdb        252:16   0   1G  0 disk
└─bcache0  251:0    0   2G  0 disk
```

This patch will limit the size of flash device, reserving at least 5% of available size for the btree.

```
[root@zou ~]# echo 2G > /sys/block/vdb/bcache/set/flash_vol_create
[root@zou ~]# lsblk /dev/vdb
NAME       MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vdb        252:16   0    1G  0 disk
└─bcache0  251:0    0  950M  0 disk
```
---
 drivers/md/bcache/super.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/md/bcache/super.c +12 -1
index ba3909bb6bea..f41e09e0e8ee 100644
@@ -1579,6 +1579,17 @@ static int flash_devs_run(struct cache_set *c)
 	return ret;
 }
 
+static inline sector_t flash_dev_max_sectors(struct cache_set *c)
+{
+	size_t avail_nbuckets;
+	struct cache *ca = c->cache;
+	size_t first_bucket = ca->sb.first_bucket;
+	size_t njournal_buckets = ca->sb.njournal_buckets;
+
+	avail_nbuckets = c->nbuckets - first_bucket - njournal_buckets;
+	return bucket_to_sector(c, avail_nbuckets / 100 * 95);
+}
+
 int bch_flash_dev_create(struct cache_set *c, uint64_t size)
 {
 	struct uuid_entry *u;
@@ -1600,7 +1611,7 @@ int bch_flash_dev_create(struct cache_set *c, uint64_t size)
 	u->first_reg = u->last_reg = cpu_to_le32((u32)ktime_get_real_seconds());
 
 	SET_UUID_FLASH_ONLY(u, 1);
-	u->sectors = size >> 9;
+	u->sectors = min(flash_dev_max_sectors(c), size >> 9);
 
 	bch_uuid_write(c);
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 58 lines

2022年9月7日 19:29,[email protected] 写道:

From: mingzhe <[email protected]>

Currently, size is specified and not checked when creating a flash device. This will cause a problem, IO maybe hang when creating a flash device with the actual size of the device.

```
	if (attr == &sysfs_flash_vol_create) {
		int r;
		uint64_t v;

		strtoi_h_or_return(buf, v);

		r = bch_flash_dev_create(c, v);
		if (r)
			return r;
	}
```

Because the flash device needs some space for superblock, journal and btree. If the size of data reaches the available size, the new IO cannot allocate space and will hang. At this time, the gc thread will be started frequently.

Even more unreasonable, we can create flash devices larger than actual size.

```
[root@zou ~]# echo 2G > /sys/block/vdb/bcache/set/flash_vol_create
[root@zou ~]# lsblk /dev/vdb
NAME       MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vdb        252:16   0   1G  0 disk
└─bcache0  251:0    0   2G  0 disk
```

This patch will limit the size of flash device, reserving at least 5% of available size for the btree.

```
[root@zou ~]# echo 2G > /sys/block/vdb/bcache/set/flash_vol_create
[root@zou ~]# lsblk /dev/vdb
NAME       MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vdb        252:16   0    1G  0 disk
└─bcache0  251:0    0  950M  0 disk
```

---
drivers/md/bcache/super.c | 13 ++++++++++++-

1 file changed, 12 insertions(+), 1 deletion(-)
drivers/md/bcache/super.c +0 -0
index ba3909bb6bea..f41e09e0e8ee 100644

drivers/md/bcache/super.c static int flash_devs_run(struct cache_set *c) · line 1581

Quoted 3 lines
drivers/md/bcache/super.c +0 -0
@@ -1579,6 +1579,17 @@ static int flash_devs_run(struct cache_set *c)
 	return ret;
 }
 

Hi Mingzhe,

drivers/md/bcache/super.c static int flash_devs_run(struct cache_set *c) · line 1591

Quoted 10 lines
drivers/md/bcache/super.c +10 -0
+static inline sector_t flash_dev_max_sectors(struct cache_set *c)
+{
+	size_t avail_nbuckets;
+	struct cache *ca = c->cache;
+	size_t first_bucket = ca->sb.first_bucket;
+	size_t njournal_buckets = ca->sb.njournal_buckets;
+
+	avail_nbuckets = c->nbuckets - first_bucket - njournal_buckets;
+	return bucket_to_sector(c, avail_nbuckets / 100 * 95);
+}

Overall I like this idea. This is really something I didn’t realize to fix, nice catch!

BTW, I feel 95% is still quite high rate, how about using 90%? And you may define the rate as a macro in bcache.h.

Thanks for this patch.

Coly Li

Quoted 16 lines
drivers/md/bcache/super.c +2 -1
+
 int bch_flash_dev_create(struct cache_set *c, uint64_t size)
 {
 	struct uuid_entry *u;
@@ -1600,7 +1611,7 @@ int bch_flash_dev_create(struct cache_set *c, uint64_t size)
 	u->first_reg = u->last_reg = cpu_to_le32((u32)ktime_get_real_seconds());
 
 	SET_UUID_FLASH_ONLY(u, 1);
-	u->sectors = size >> 9;
+	u->sectors = min(flash_dev_max_sectors(c), size >> 9);
 
 	bch_uuid_write(c);
 
Signature · 1 line
2.17.1
#2 lore raw
Quoted 75 lines · Coly Li
Quoted 57 lines

2022年9月7日 19:29,[email protected] 写道:

From: mingzhe <[email protected]>

Currently, size is specified and not checked when creating a flash device. This will cause a problem, IO maybe hang when creating a flash device with the actual size of the device.

```
	if (attr == &sysfs_flash_vol_create) {
		int r;
		uint64_t v;

		strtoi_h_or_return(buf, v);

		r = bch_flash_dev_create(c, v);
		if (r)
			return r;
	}
```

Because the flash device needs some space for superblock, journal and btree. If the size of data reaches the available size, the new IO cannot allocate space and will hang. At this time, the gc thread will be started frequently.

Even more unreasonable, we can create flash devices larger than actual size.

```
[root@zou ~]# echo 2G > /sys/block/vdb/bcache/set/flash_vol_create
[root@zou ~]# lsblk /dev/vdb
NAME       MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vdb        252:16   0   1G  0 disk
└─bcache0  251:0    0   2G  0 disk
```

This patch will limit the size of flash device, reserving at least 5% of available size for the btree.

```
[root@zou ~]# echo 2G > /sys/block/vdb/bcache/set/flash_vol_create
[root@zou ~]# lsblk /dev/vdb
NAME       MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vdb        252:16   0    1G  0 disk
└─bcache0  251:0    0  950M  0 disk
```

---
drivers/md/bcache/super.c | 13 ++++++++++++-

1 file changed, 12 insertions(+), 1 deletion(-)
drivers/md/bcache/super.c +0 -0
index ba3909bb6bea..f41e09e0e8ee 100644

drivers/md/bcache/super.c static int flash_devs_run(struct cache_set *c) · line 1581

Quoted 3 lines
drivers/md/bcache/super.c +0 -0
@@ -1579,6 +1579,17 @@ static int flash_devs_run(struct cache_set *c)
 	return ret;
 }
 

Hi Mingzhe,

drivers/md/bcache/super.c static int flash_devs_run(struct cache_set *c) · line 1591

Quoted 10 lines
drivers/md/bcache/super.c +10 -0
+static inline sector_t flash_dev_max_sectors(struct cache_set *c)
+{
+	size_t avail_nbuckets;
+	struct cache *ca = c->cache;
+	size_t first_bucket = ca->sb.first_bucket;
+	size_t njournal_buckets = ca->sb.njournal_buckets;
+
+	avail_nbuckets = c->nbuckets - first_bucket - njournal_buckets;
+	return bucket_to_sector(c, avail_nbuckets / 100 * 95);
+}

Overall I like this idea. This is really something I didn’t realize to fix, nice catch!

Quoted 1 line

BTW, I feel 95% is still quite high rate, how about using 90%? And you may define the rate as a macro in bcache.h.

Hi Coly

Of course, it would be better to define as a macro, I will send v2.

From my testing, 95% should be safe, no IO hung.

But 90% is better than 95%, because every write 6.25% (1/16) of the total capacity will trigger a gc.

mingzhe

Quoted 22 lines

Thanks for this patch.

Coly Li

Quoted 16 lines
drivers/md/bcache/super.c +2 -1
+
 int bch_flash_dev_create(struct cache_set *c, uint64_t size)
 {
 	struct uuid_entry *u;
@@ -1600,7 +1611,7 @@ int bch_flash_dev_create(struct cache_set *c, uint64_t size)
 	u->first_reg = u->last_reg = cpu_to_le32((u32)ktime_get_real_seconds());
 
 	SET_UUID_FLASH_ONLY(u, 1);
-	u->sectors = size >> 9;
+	u->sectors = min(flash_dev_max_sectors(c), size >> 9);
 
 	bch_uuid_write(c);
 
Signature · 1 line
2.17.1