btrfs: zoned: fix uninitialized max_chunk_size
PATCH 1/1 btrfs: zoned: fix uninitialized max_chunk_size
From: Arnd Bergmann <[email protected]>
The ctl->max_chunk_size member might be used uninitialized when none of the three conditions for initializing it in init_alloc_chunk_ctl_policy_zoned() are true:
In function ‘init_alloc_chunk_ctl_policy_zoned’,
inlined from ‘init_alloc_chunk_ctl’ at fs/btrfs/volumes.c:5023:3,
inlined from ‘btrfs_alloc_chunk’ at fs/btrfs/volumes.c:5340:2:include/linux/compiler-gcc.h:48:45: error: ‘ctl.max_chunk_size’ may be used uninitialized [-Werror=maybe-uninitialized]
4998 | ctl->max_chunk_size = min(limit, ctl->max_chunk_size);
| ^~~fs/btrfs/volumes.c: In function ‘btrfs_alloc_chunk’:fs/btrfs/volumes.c:5316:32: note: ‘ctl’ declared here
5316 | struct alloc_chunk_ctl ctl;
| ^~~
Initialize it to UINT_MAX and rely on the min() expression to limit it.
---
Note that the -Wmaybe-unintialized warning is globally disabled by default. For some reason I got this warning anyway when building this specific file with gcc-11.
--- fs/btrfs/volumes.c | 1 + 1 file changed, 1 insertion(+)
fs/btrfs/volumes.c +1 -0
@@ -4980,6 +4980,7 @@ static void init_alloc_chunk_ctl_policy_zoned(u64 type = ctl->type; ctl->max_stripe_size = zone_size; + ctl->max_chunk_size =UINT_MAX ;if (type &BTRFS_BLOCK_GROUP_DATA ) { ctl->max_chunk_size =round_down (BTRFS_MAX_DATA_CHUNK_SIZE , zone_size);
Signature · 1 line
2.29.2
Quoted 20 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
The
ctl->max_chunk_sizemember might be used uninitialized when none of the three conditions for initializing it ininit_alloc_chunk_ctl_policy_zoned()are true:In function ‘init_alloc_chunk_ctl_policy_zoned’,
inlined from ‘init_alloc_chunk_ctl’ atfs/btrfs/volumes.c:5023:3,
inlined from ‘btrfs_alloc_chunk’ atfs/btrfs/volumes.c:5340:2:include/linux/compiler-gcc.h:48:45: error: ‘ctl.max_chunk_size’ may be used uninitialized [-Werror=maybe-uninitialized]
4998 |ctl->max_chunk_size=min(limit, ctl->max_chunk_size);
| ^~~fs/btrfs/volumes.c: In function ‘btrfs_alloc_chunk’:fs/btrfs/volumes.c:5316:32: note: ‘ctl’ declared here
5316 | structalloc_chunk_ctlctl;
| ^~~Initialize it to
UINT_MAXand rely on themin()expression to limit it.
Quoted 6 lines
Fixes 1cd6121f2a38 ("btrfs: zoned: implement zoned chunk allocator")Signed-off-by Arnd Bergmann <[email protected]>---
Note that the -Wmaybe-unintialized warning is globally disabled by default. For some reason I got this warning anyway when building this specific file with gcc-11.
The warning catches a theoretical case but this would not happen in pracitce. There are three bits to check and that covers all valid options, but there should be a final else {} like is in init_alloc_chunk_ctl_policy_regular that does not let the function continue as that would mean there are worse problems.
btrfs_alloc_chunk
init_alloc_chunk_ctl
init_alloc_chunk_ctl_policy_zoned
and btrfs_alloc_chunk validates the ctl->flags against BTRFS_BLOCK_GROUP_TYPE_MASK, which is exactly the tree branches.
fs/btrfs/volumes.c static void init_alloc_chunk_ctl_policy_zoned( · line 4983
Quoted 13 lines
--- fs/btrfs/volumes.c | 1 + 1 file changed, 1 insertion(+)fs/btrfs/volumes.c +1
-0@@ -4980,6 +4980,7 @@ static void init_alloc_chunk_ctl_policy_zoned(u64 type = ctl->type; ctl->max_stripe_size = zone_size; + ctl->max_chunk_size =UINT_MAX ;
This would allow the min() work but otherwise is not an expected to happen at all.
Quoted 5 lines
fs/btrfs/volumes.c +0
-0if (type &BTRFS_BLOCK_GROUP_DATA ) { ctl->max_chunk_size =round_down (BTRFS_MAX_DATA_CHUNK_SIZE , zone_size);Signature · 1 line
2.29.2