Skip to contentSkip to conversation

Fix some -Wmaybe-uninitialized errors

PATCH In review Josef Bacik 4 participants 10 messages Sep 5, 2023 – Sep 6, 2023 Btrfs file system

lore
Share & tools
lore
#0 lore raw

cover Fix some -Wmaybe-uninitialized errors

Hello,

Jens reported to me two warnings he was seeing with CONFIG_CC_OPTIMIZE_FOR_SIZE=y set in his .config. We don't see these errors without this option, and they're not correct warnings. However we've had issues where -Wmaybe-uninitialized would have caught a real bug, so this warning is generally valuable. Fix these warnings so we have a clean build. Thanks,

Josef

Josef Bacik (2):
btrfs: make sure to initialize start and len in find_free_dev_extent
btrfs: initialize start_slot in btrfs_log_prealloc_extents

 fs/btrfs/tree-log.c |  2 +-
 fs/btrfs/volumes.c  | 14 ++++++--------
 2 files changed, 7 insertions(+), 9 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/2 btrfs: make sure to initialize start and len in find_free_dev_extent

Jens reported a compiler error when using CONFIG_CC_OPTIMIZE_FOR_SIZE=y that looks like this

In function ‘gather_device_info’,
inlined from ‘btrfs_create_chunk’ at fs/btrfs/volumes.c:5507:8:
fs/btrfs/volumes.c:5245:48: warning: ‘dev_offset’ may be used uninitialized [-Wmaybe-uninitialized]

 5245 |                 devices_info[ndevs].dev_offset = dev_offset;
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~

fs/btrfs/volumes.c: In function ‘btrfs_create_chunk’:
fs/btrfs/volumes.c:5196:13: note: ‘dev_offset’ was declared here
5196 | u64 dev_offset;

This occurs because find_free_dev_extent is responsible for setting dev_offset, however if we get an -ENOMEM at the top of the function we'll return without setting the value.

This isn't actually a problem because we will see the -ENOMEM in gather_device_info() and return and not use the uninitialized value, however we also just don't want the compiler warning so rework the code slightly in find_free_dev_extent() to make sure it's always setting *start and *len to avoid the compiler error.

---
 fs/btrfs/volumes.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
fs/btrfs/volumes.c +6 -8
index 4b0e441227b2..08dba911212c 100644
@@ -1594,25 +1594,23 @@ static int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
 	u64 search_start;
 	u64 hole_size;
 	u64 max_hole_start;
-	u64 max_hole_size;
+	u64 max_hole_size = 0;
 	u64 extent_end;
 	u64 search_end = device->total_bytes;
 	int ret;
 	int slot;
 	struct extent_buffer *l;
 
-	search_start = dev_extent_search_start(device);
+	max_hole_start = search_start = dev_extent_search_start(device);
 
 	WARN_ON(device->zone_info &&
 		!IS_ALIGNED(num_bytes, device->zone_info->zone_size));
 
 	path = btrfs_alloc_path();
-	if (!path)
-		return -ENOMEM;
-
-	max_hole_start = search_start;
-	max_hole_size = 0;
-
+	if (!path) {
+		ret = -ENOMEM;
+		goto out;
+	}
 again:
 	if (search_start >= search_end ||
 		test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Signature · 1 line
2.41.0
#5 lore raw

PATCH 2/2 btrfs: initialize start_slot in btrfs_log_prealloc_extents

In reply to Josef Bacik

Jens reported a compiler warning when using CONFIG_CC_OPTIMIZE_FOR_SIZE=y that looks like this

fs/btrfs/tree-log.c: In function ‘btrfs_log_prealloc_extents’:
fs/btrfs/tree-log.c:4828:23: warning: ‘start_slot’ may be used uninitialized [-Wmaybe-uninitialized]

 4828 |                 ret = copy_items(trans, inode, dst_path, path,
      |                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 4829 |                                  start_slot, ins_nr, 1, 0);
      |                                  ~~~~~~~~~~~~~~~~~~~~~~~~~
fs/btrfs/tree-log.c:4725:13: note: ‘start_slot’ was declared here
 4725 |         int start_slot;
      |             ^~~~~~~~~~

The compiler is incorrect, as we only use this code when ins_len > 0, and when ins_len > 0 we have start_slot properly initialized. However we generally find the -Wmaybe-uninitialized warnings valuable, so initialize start_slot to get rid of the warning.

---
 fs/btrfs/tree-log.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/tree-log.c +1 -1
index d1e46b839519..cbb17b542131 100644
@@ -4722,7 +4722,7 @@ static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
 	struct extent_buffer *leaf;
 	int slot;
 	int ins_nr = 0;
-	int start_slot;
+	int start_slot = 0;
 	int ret;
 
 	if (!(inode->flags & BTRFS_INODE_PREALLOC))
Signature · 1 line
2.41.0
Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] btrfs: make sure to initialize start and len in find_free_dev_extent

In reply to Josef Bacik
Quoted 16 lines · Josef Bacik

Jens reported a compiler error when using CONFIG_CC_OPTIMIZE_FOR_SIZE=y that looks like this

In function ‘gather_device_info’,
inlined from ‘btrfs_create_chunk’ at fs/btrfs/volumes.c:5507:8:
fs/btrfs/volumes.c:5245:48: warning: ‘dev_offset’ may be used uninitialized [-Wmaybe-uninitialized]

  5245 |                 devices_info[ndevs].dev_offset = dev_offset;
       |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~

fs/btrfs/volumes.c: In function ‘btrfs_create_chunk’:
fs/btrfs/volumes.c:5196:13: note: ‘dev_offset’ was declared here
5196 | u64 dev_offset;

This occurs because find_free_dev_extent is responsible for setting dev_offset, however if we get an -ENOMEM at the top of the function we'll return without setting the value.

Quoted 8 lines

This isn't actually a problem because we will see the -ENOMEM in gather_device_info() and return and not use the uninitialized value, however we also just don't want the compiler warning so rework the code slightly in find_free_dev_extent() to make sure it's always setting *start and *len to avoid the compiler error.

Just one nitpick below.

Quoted 12 lines
---
  fs/btrfs/volumes.c | 14 ++++++--------
  1 file changed, 6 insertions(+), 8 deletions(-)
fs/btrfs/volumes.c +0 -0
index 4b0e441227b2..08dba911212c 100644
@@ -1594,25 +1594,23 @@ static int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
  	u64 search_start;
  	u64 hole_size;
  	u64 max_hole_start;

fs/btrfs/volumes.c static int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes, · line 1604

Quoted 10 lines
fs/btrfs/volumes.c +2 -2
-	u64 max_hole_size;
+	u64 max_hole_size = 0;
  	u64 extent_end;
  	u64 search_end = device->total_bytes;
  	int ret;
  	int slot;
  	struct extent_buffer *l;
 
-	search_start = dev_extent_search_start(device);
+	max_hole_start = search_start = dev_extent_search_start(device);

IIRC we don't recommend such assignment, it would be better to go two lines to do the assignment.

Thanks,
Qu

Quoted 18 lines
fs/btrfs/volumes.c +4 -6
  	WARN_ON(device->zone_info &&
  		!IS_ALIGNED(num_bytes, device->zone_info->zone_size));
 
  	path = btrfs_alloc_path();
-	if (!path)
-		return -ENOMEM;
-
-	max_hole_start = search_start;
-	max_hole_size = 0;
-
+	if (!path) {
+		ret = -ENOMEM;
+		goto out;
+	}
  again:
  	if (search_start >= search_end ||
  		test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
#7 lore raw

Re: [PATCH 2/2] btrfs: initialize start_slot in btrfs_log_prealloc_extents

In reply to Josef Bacik
Quoted 14 lines · Josef Bacik

Jens reported a compiler warning when using CONFIG_CC_OPTIMIZE_FOR_SIZE=y that looks like this

fs/btrfs/tree-log.c: In function ‘btrfs_log_prealloc_extents’:
fs/btrfs/tree-log.c:4828:23: warning: ‘start_slot’ may be used uninitialized [-Wmaybe-uninitialized]

  4828 |                 ret = copy_items(trans, inode, dst_path, path,
       |                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4829 |                                  start_slot, ins_nr, 1, 0);
       |                                  ~~~~~~~~~~~~~~~~~~~~~~~~~
fs/btrfs/tree-log.c:4725:13: note: ‘start_slot’ was declared here
  4725 |         int start_slot;
       |             ^~~~~~~~~~
Quoted 7 lines

The compiler is incorrect, as we only use this code when ins_len > 0, and when ins_len > 0 we have start_slot properly initialized. However we generally find the -Wmaybe-uninitialized warnings valuable, so initialize start_slot to get rid of the warning.

I think we're in a dilemma, if we don't do the initialization, bad compiler may warn.

But if we do the initialization, some static checker may also warn...

Thanks,
Qu

Quoted 17 lines
---
  fs/btrfs/tree-log.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/tree-log.c +1 -1
index d1e46b839519..cbb17b542131 100644
@@ -4722,7 +4722,7 @@ static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
  	struct extent_buffer *leaf;
  	int slot;
  	int ins_nr = 0;
-	int start_slot;
+	int start_slot = 0;
  	int ret;
 
  	if (!(inode->flags & BTRFS_INODE_PREALLOC))
#4 lore raw

Re: [PATCH 1/2] btrfs: make sure to initialize start and len in find_free_dev_extent

In reply to Qu Wenruo
Quoted 5 lines · Qu Wenruo

fs/btrfs/volumes.c static int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes, · line 1604

Quoted 2 lines
fs/btrfs/volumes.c +1 -1
-	search_start = dev_extent_search_start(device);
+	max_hole_start = search_start = dev_extent_search_start(device);

IIRC we don't recommend such assignment, it would be better to go two lines to do the assignment.

Right, fixed in the commit, thanks.

#8 lore raw

Re: [PATCH 2/2] btrfs: initialize start_slot in btrfs_log_prealloc_extents

In reply to Qu Wenruo
Quoted 22 lines · Qu Wenruo
Quoted 19 lines · Josef Bacik

Jens reported a compiler warning when using CONFIG_CC_OPTIMIZE_FOR_SIZE=y that looks like this

fs/btrfs/tree-log.c: In function ‘btrfs_log_prealloc_extents’:
fs/btrfs/tree-log.c:4828:23: warning: ‘start_slot’ may be used uninitialized [-Wmaybe-uninitialized]

  4828 |                 ret = copy_items(trans, inode, dst_path, path,
       |                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4829 |                                  start_slot, ins_nr, 1, 0);
       |                                  ~~~~~~~~~~~~~~~~~~~~~~~~~
fs/btrfs/tree-log.c:4725:13: note: ‘start_slot’ was declared here
  4725 |         int start_slot;
       |             ^~~~~~~~~~

The compiler is incorrect, as we only use this code when ins_len > 0, and when ins_len > 0 we have start_slot properly initialized. However we generally find the -Wmaybe-uninitialized warnings valuable, so initialize start_slot to get rid of the warning.

Quoted 7 lines
Quoted 2 lines

I think we're in a dilemma, if we don't do the initialization, bad compiler may warn.

But if we do the initialization, some static checker may also warn...

I've seen static checkers to accept variables initialized to 0 and unused, there's a lot of code that does that as a matter of coding style so there would be too many warnings for a generally good practice. If we see a warning from something else than compiler then we can reconsider the way it's fixed but I think we'd see more compiler reports than static checker reports.

#9 lore raw
In reply to Josef Bacik
Quoted 8 lines · Josef Bacik

Hello,

Jens reported to me two warnings he was seeing with CONFIG_CC_OPTIMIZE_FOR_SIZE=y set in his .config. We don't see these errors without this option, and they're not correct warnings. However we've had issues where -Wmaybe-uninitialized would have caught a real bug, so this warning is generally valuable. Fix these warnings so we have a clean build. Thanks,

Quoted 5 lines

Josef

Josef Bacik (2):
btrfs: make sure to initialize start and len in find_free_dev_extent
btrfs: initialize start_slot in btrfs_log_prealloc_extents

Added to misc-next, thanks.