Skip to contentSkip to conversation

bcache: fix nbuckets lower limit checking in read_super_common

PATCH In review Lin Feng 2 participants 3 messages May 17, 2023 – May 18, 2023 bcache (block layer cache)

lore
Share & tools
lore

Lin Feng

author
#0 lore raw

PATCH 1/1 bcache: fix nbuckets lower limit checking in read_super_common

In fact due to this check in cache_alloc:

    free = roundup_pow_of_two(ca->sb.nbuckets) >> 10;
    if (!free) {
        ret = -EPERM;
        err = "ca->sb.nbuckets is too small";
        goto err_free;
    }

we can only create bcache device with nbuckets greater than 512, so this patch is to make the codes logic consistent.

---
 drivers/md/bcache/super.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/bcache/super.c +1 -1
index 7e9d19fd21dd..681a7ea442b9 100644
@@ -110,7 +110,7 @@ static const char *read_super_common(struct cache_sb *sb,  struct block_device *
 		goto err;
 
 	err = "Not enough buckets";
-	if (sb->nbuckets < 1 << 7)
+	if (sb->nbuckets <= 1 << 9)
 		goto err;
 
 	err = "Bad block size (not power of 2)";
Signature · 1 line
2.40.1
#1 lore raw
Quoted 6 lines

2023年5月17日 11:51,Lin Feng <[email protected]> 写道:

In fact due to this check in cache_alloc:

   free = roundup_pow_of_two(ca->sb.nbuckets) >> 10;

This was introduced from commit 78365411b344d (“bcache: Rework allocator reserves”).

-       free = roundup_pow_of_two(ca->sb.nbuckets) >> 9;
-       free = max_t(size_t, free, (prio_buckets(ca) + 8) * 2);
+       free = roundup_pow_of_two(ca->sb.nbuckets) >> 10;
Quoted 18 lines
   if (!free) {
       ret = -EPERM;
       err = "ca->sb.nbuckets is too small";
       goto err_free;
   }

we can only create bcache device with nbuckets greater than 512, so this patch is to make the codes logic consistent.

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

1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/bcache/super.c +0 -0
index 7e9d19fd21dd..681a7ea442b9 100644

drivers/md/bcache/super.c static const char *read_super_common(struct cache_sb *sb, struct block_device * · line 113

Quoted 5 lines
drivers/md/bcache/super.c +1 -1
@@ -110,7 +110,7 @@ static const char *read_super_common(struct cache_sb *sb,  struct block_device *
 goto err;
 
 err = "Not enough buckets";
- if (sb->nbuckets < 1 << 7)
+ if (sb->nbuckets <= 1 << 9)

if (roundup_pow_of_two(ca->sb.nbuckets) < 1<< 10)

Might be more clear?

drivers/md/bcache/super.c static const char *read_super_common(struct cache_sb *sb, struct block_device * · line 116

Quoted 3 lines
drivers/md/bcache/super.c +0 -0
 goto err;
 
 err = "Bad block size (not power of 2)";

In bcache-tools the minimum nbuckets is (1<<7) too. It is fine to modify it to 1<<10, I will modify bcache-tools too.

Nice catch, thanks!

Coly

author
#2 lore raw
Quoted 32 lines · Coly Li
Quoted 6 lines

2023年5月17日 11:51,Lin Feng <[email protected]> 写道:

In fact due to this check in cache_alloc:

   free = roundup_pow_of_two(ca->sb.nbuckets) >> 10;

This was introduced from commit 78365411b344d (“bcache: Rework allocator reserves”).

(quoted diff) +1 -2
-       free = roundup_pow_of_two(ca->sb.nbuckets) >> 9;
-       free = max_t(size_t, free, (prio_buckets(ca) + 8) * 2);
+       free = roundup_pow_of_two(ca->sb.nbuckets) >> 10;
Quoted 21 lines
   if (!free) {
       ret = -EPERM;
       err = "ca->sb.nbuckets is too small";
       goto err_free;
   }

we can only create bcache device with nbuckets greater than 512, so this patch is to make the codes logic consistent.

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

1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/bcache/super.c +0 -0
index 7e9d19fd21dd..681a7ea442b9 100644
@@ -110,7 +110,7 @@ static const char *read_super_common(struct cache_sb *sb,  struct block_device *
 goto err;
 
Quoted 7 lines

drivers/md/bcache/super.c static const char *read_super_common(struct cache_sb *sb, struct block_device * · line 113

Quoted 3 lines
drivers/md/bcache/super.c +1 -1
 err = "Not enough buckets";
- if (sb->nbuckets < 1 << 7)
+ if (sb->nbuckets <= 1 << 9)

if (roundup_pow_of_two(ca->sb.nbuckets) < 1<< 10)

Might be more clear?

Yes, I also prefer your change :)

Quoted 10 lines

drivers/md/bcache/super.c static const char *read_super_common(struct cache_sb *sb, struct block_device * · line 116

Quoted 4 lines
drivers/md/bcache/super.c +0 -0
 goto err;
 
 err = "Bad block size (not power of 2)";

In bcache-tools the minimum nbuckets is (1<<7) too. It is fine to modify it to 1<<10, I will modify bcache-tools too.

bcache-tools also has this flaw, both are aligned to 1<<10 is a good choice!

Thanks,
linfeng