btrfs-progs: minor fixes for clang warnings
Subsystems (2)
cover btrfs-progs: minor fixes for clang warnings
Recently I'm migrating my default compiler from GCC to clang, mostly to get short comiling time (especially important on my aarch64 machines).
Thus I hit those (mostly false alerts) warnings in btrfs-progs, and come up with this patchset.
Unfortunately there is still libbtrfsutils causing warnings in setuptools, as it's still using the default flags from gcc no matter what.
Qu Wenruo (5):
btrfs-progs: remove an unnecessary branch to silent the clang warning
btrfs-progs: fix a false alert on an uninitialized variable when
BUG_ON() is involved.
btrfs-progs: fix fallthrough cases with proper attributes
btrfs-progs: move a union with variable sized type to the end
btrfs-progs: fix set but not used variables
cmds/reflink.c | 2 +- cmds/scrub.c | 12 +++--- common/format-output.c | 2 +- common/parse-utils.c | 12 +++--- common/units.c | 6 +-- crypto/xxhash.c | 73 +++++++++++++++++++------------------ image/main.c | 15 +++----- kerncompat.h | 8 ++++ kernel-shared/ctree.c | 18 +++++---- kernel-shared/extent-tree.c | 4 +- kernel-shared/print-tree.c | 2 +- kernel-shared/volumes.c | 3 +- kernel-shared/zoned.c | 6 +-- mkfs/main.c | 4 -- 14 files changed, 84 insertions(+), 83 deletions(-)
Signature · 1 line
2.39.1
PATCH 1/5 btrfs-progs: remove an unnecessary branch to silent the clang warning
[FALSE ALERT]
With clang 15.0.7, there is a false alert on uninitialized value in ctree.c:
kernel-shared/ctree.c:3418:13: warning: variable 'offset' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
} else if (ret < 0) {
^~~~~~~
kernel-shared/ctree.c:3428:41: note: uninitialized use occurs here
write_extent_buffer(eb, &subvol_id_le, offset, sizeof(subvol_id_le));
^~~~~~
kernel-shared/ctree.c:3418:9: note: remove the 'if' if its condition is always true
} else if (ret < 0) {
^~~~~~~~~~~~~
kernel-shared/ctree.c:3380:22: note: initialize the variable 'offset' to silence this warning
unsigned long offset;
^
= 0
kernel-shared/ctree.c:3418:13: warning: variable 'eb' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
} else if (ret < 0) {
^~~~~~~
kernel-shared/ctree.c:3429:26: note: uninitialized use occurs here
btrfs_mark_buffer_dirty(eb);
^~
kernel-shared/ctree.c:3418:9: note: remove the 'if' if its condition is always true
} else if (ret < 0) {
^~~~~~~~~~~~~
kernel-shared/ctree.c:3378:26: note: initialize the variable 'eb' to silence this warning
struct extent_buffer *eb;
^
= NULL[CAUSE]
The original code is handling the return value from btrfs_insert_empty_item() like this:
ret = btrfs_insert_empty_item();
if (ret >= 0) {
/* Do something for it. */
} else if (ret == -EEXIST) {
/* Do something else. */
} else if (ret < 0) {
/* Error handling. */
}But the problem is, the last one check is always true if we can reach there.
Thus clang is providing the hint to remove the if () chekc.
[FIX]
Normally we prefer to do error handling first, so move the error handling first so we don't need the if () else if () chain.
--- kernel-shared/ctree.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)
kernel-shared/ctree.c +10 -8
@@ -3400,14 +3400,22 @@ int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, u8 *uuid, u8 type, ret =btrfs_insert_empty_item (trans, uuid_root, path, &key,sizeof (subvol_id_le)); +if (ret <0 && ret != -EEXIST ) { +warning ( +"inserting uuid item failed (0x%016llx, 0x%016llx) type %u: %d" , + (unsigned long long )key.objectid, + (unsigned long long )key.offset, type, ret); + goto out; + } +if (ret >=0 ) {/* Add an item for the type for the first time */ eb = path->nodes[0 ]; slot = path->slots[0 ]; offset =btrfs_item_ptr_offset (eb, slot); - }else if (ret == -EEXIST ) { + }else {/* -* An item with that type already exists. +* ret == -EEXIST case, An item with that type already exists. * Extend the item and store the new subvol_id at the end. */ btrfs_extend_item (uuid_root, path,sizeof (subvol_id_le));
@@ -3415,12 +3423,6 @@ int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, u8 *uuid, u8 type, slot = path->slots[0 ]; offset =btrfs_item_ptr_offset (eb, slot); offset +=btrfs_item_size (eb, slot) -sizeof (subvol_id_le); - }else if (ret <0 ) { -warning ( -"inserting uuid item failed (0x%016llx, 0x%016llx) type %u: %d" , - (unsigned long long )key.objectid, - (unsigned long long )key.offset, type, ret); - goto out; } ret =0 ;
Signature · 1 line
2.39.1
PATCH 2/5 btrfs-progs: fix a false alert on an uninitialized variable when BUG_ON() is involved.
In reply to Qu Wenruo[FALSE ALERT]
Clang 15.0.7 gives the following false alert in get_dev_extent_len():
kernel-shared/extent-tree.c:3328:2: warning: variable 'div' is used uninitialized whenever switch default is taken [-Wsometimes-uninitialized]
default:
^~~~~~~
kernel-shared/extent-tree.c:3332:24: note: uninitialized use occurs here
return map->ce.size / div;
^~~
kernel-shared/extent-tree.c:3311:9: note: initialize the variable 'div' to silence this warning
int div;
^
= 0And one in btrfs_stripe_length() too:
kernel-shared/volumes.c:2781:2: warning: variable 'stripe_len' is used uninitialized whenever switch default is taken [-Wsometimes-uninitialized]
default:
^~~~~~~
kernel-shared/volumes.c:2785:9: note: uninitialized use occurs here
return stripe_len;
^~~~~~~~~~
kernel-shared/volumes.c:2754:16: note: initialize the variable 'stripe_len' to silence this warning
u64 stripe_len;
^
= 0[CAUSE]
Clang doesn't really understand what BUG_ON() means, thus in that default case, we won't get uninitialized value but crash directly.
[FIX]
Silent the errors by assigning the default value properly using the value of SINGLE profile.
--- kernel-shared/extent-tree.c | 4 ++-- kernel-shared/volumes.c | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-)
kernel-shared/extent-tree.c +2 -2
@@ -3308,7 +3308,7 @@ out:static u64 get_dev_extent_len (struct map_lookup *map) { -int div; +int div =1 ;switch (map->type &BTRFS_BLOCK_GROUP_PROFILE_MASK ) {case 0 :/* Single */
@@ -3316,7 +3316,7 @@ static u64 get_dev_extent_len(struct map_lookup *map)case BTRFS_BLOCK_GROUP_RAID1 :case BTRFS_BLOCK_GROUP_RAID1C3 :case BTRFS_BLOCK_GROUP_RAID1C4 : - div =1 ; +/* The default value can already handle it. */ break ;case BTRFS_BLOCK_GROUP_RAID5 :case BTRFS_BLOCK_GROUP_RAID6 :
kernel-shared/volumes.c +2 -1
@@ -2758,6 +2758,7 @@ u64 btrfs_stripe_length(struct btrfs_fs_info *fs_info,BTRFS_BLOCK_GROUP_PROFILE_MASK ; chunk_len =btrfs_chunk_length (leaf, chunk); + stripe_len = chunk_len;switch (profile) {case 0 :/* Single profile */
@@ -2765,7 +2766,7 @@ u64 btrfs_stripe_length(struct btrfs_fs_info *fs_info,case BTRFS_BLOCK_GROUP_RAID1C3 :case BTRFS_BLOCK_GROUP_RAID1C4 :case BTRFS_BLOCK_GROUP_DUP : - stripe_len = chunk_len; +/* The default value is already fine. */ break ;case BTRFS_BLOCK_GROUP_RAID0 : stripe_len = chunk_len / num_stripes;
Signature · 1 line
2.39.1
PATCH 3/5 btrfs-progs: fix fallthrough cases with proper attributes
In reply to Qu Wenruo[FALSE ALERT]
Unlike gcc, clang doesn't really understand the comments, thus it's reportings tons of fall through related errors:
cmds/reflink.c:124:3: warning: unannotated fall-through between switch labels [-Wimplicit-fallthrough]
case 'r':
^
cmds/reflink.c:124:3: note: insert '__attribute__((fallthrough));' to silence this warning
case 'r':
^
__attribute__((fallthrough));
cmds/reflink.c:124:3: note: insert 'break;' to avoid fall-through
case 'r':
^
break;[CAUSE]
Although gcc is fine with /* fallthrough */ comments, clang is not.
[FIX]
So just introduce a fallthrough macro to handle the situation properly, and use that macro instead.
--- cmds/reflink.c | 2 +- cmds/scrub.c | 10 +++--- common/format-output.c | 2 +- common/parse-utils.c | 12 +++---- common/units.c | 6 ++-- crypto/xxhash.c | 73 +++++++++++++++++++------------------- image/main.c | 2 +- kerncompat.h | 8 +++++ kernel-shared/print-tree.c | 2 +- 9 files changed, 63 insertions(+), 54 deletions(-)
cmds/reflink.c +1 -1
@@ -120,7 +120,7 @@ static int cmd_reflink_clone(const struct cmd_struct *cmd, int argc, char **argvswitch (c) {case 's' : same_file = true; -/* fallthrough */ + fallthrough;case 'r' : range =malloc (sizeof (struct reflink_range ));if (!range) {
cmds/scrub.c +5 -5
@@ -602,7 +602,7 @@ again:memset (p[curr],0 ,sizeof (**p)); p[curr +1 ] =NULL ; ++state; -/* fall through */ + fallthrough;case 2 :/* start of line, skip space */ while (isspace (l[i]) && i < avail) {if (l[i] =='\n' )
@@ -613,7 +613,7 @@ again: (!eof && !memchr(l + i, '\n', avail - i))) goto again; ++state; -/* fall through */ + fallthrough;case 3 :/* read fsid */ if (i == avail)continue ;
@@ -629,7 +629,7 @@ again: _SCRUB_INVALID; i += j +1 ; ++state; -/* fall through */ + fallthrough;case 4 :/* read dev id */ for (j =0 ;isdigit (l[i + j]) && i+j < avail; ++j) ;
@@ -638,7 +638,7 @@ again: p[curr]->devid =atoll (&l[i]); i += j +1 ; ++state; -/* fall through */ + fallthrough;case 5 :/* read key/value pair */ ret =0 ; _SCRUB_KVREAD(ret, &i, data_extents_scrubbed, avail, l,
@@ -682,7 +682,7 @@ again:if (ret !=1 ) _SCRUB_INVALID; ++state; -/* fall through */ + fallthrough;case 6 :/* after number */ if (l[i] =='|' ) state =5 ;
common/format-output.c +1 -1
@@ -74,7 +74,7 @@ static void print_escaped(const char *str)case '"' :case '\\' :putchar ('\\' ); -/* fallthrough */ + fallthrough;default :putchar (*str); }
common/parse-utils.c +6 -6
@@ -184,22 +184,22 @@ u64 parse_size_from_string(const char *s)switch (c) {case 'e' : mult *=1024 ; -/* fallthrough */ + fallthrough;case 'p' : mult *=1024 ; -/* fallthrough */ + fallthrough;case 't' : mult *=1024 ; -/* fallthrough */ + fallthrough;case 'g' : mult *=1024 ; -/* fallthrough */ + fallthrough;case 'm' : mult *=1024 ; -/* fallthrough */ + fallthrough;case 'k' : mult *=1024 ; -/* fallthrough */ + fallthrough;case 'b' :break ;default :
common/units.c +3 -3
@@ -89,15 +89,15 @@ int pretty_size_snprintf(u64 size, char *str, size_t str_size, unsigned unit_modcase UNITS_TBYTES : base *= mult; num_divs++; -/* fallthrough */ + fallthrough;case UNITS_GBYTES : base *= mult; num_divs++; -/* fallthrough */ + fallthrough;case UNITS_MBYTES : base *= mult; num_divs++; -/* fallthrough */ + fallthrough;case UNITS_KBYTES : num_divs++;break ;
crypto/xxhash.c +37 -36
166 lines not on this page · raw message
image/main.c +1 -1
@@ -1431,7 +1431,7 @@ static int restore_one_work(struct mdrestore_struct *mdres,switch (ret) {case Z_NEED_DICT : ret =Z_DATA_ERROR ; - __attribute__ ((fallthrough)); + fallthrough;case Z_DATA_ERROR :case Z_MEM_ERROR : goto out;
kerncompat.h +8 -0
@@ -227,6 +227,14 @@ static inline int mutex_is_locked(struct mutex *m)#define __attribute_const__ __attribute__((__const__))#endif +/* To silent compilers like clang which doesn't understand comments. */ + +#if __has_attribute (__fallthrough__) +# define fallthrough __attribute__((__fallthrough__)) +#else +# define fallthrough do {} while (0)/* fallthrough */ +#endif +/** * __set_bit - Set a bit in memory * @nr: the bit to set
kernel-shared/print-tree.c +1 -1
@@ -796,7 +796,7 @@ void print_objectid(FILE *stream, u64 objectid, u8 type)fprintf (stream,"FIRST_CHUNK_TREE" );break ; } -/* fall-thru */ + fallthrough;default :fprintf (stream,"%llu" , (unsigned long long )objectid); }
Signature · 1 line
2.39.1
Re: [PATCH 3/5] btrfs-progs: fix fallthrough cases with proper attributes
kerncompat.h static inline int mutex_is_locked(struct mutex *m) · line 232
Quoted 9 lines · Qu Wenruo
kerncompat.h +3
-0@@ -227,6 +227,14 @@ static inline int mutex_is_locked(struct mutex *m)#define __attribute_const__ __attribute__((__const__))#endif +/* To silent compilers like clang which doesn't understand comments. */ + +#if __has_attribute (__fallthrough__)
The use of __has_attribute should be beihnd and ifdef
(https://gcc.gnu.org/onlinedocs/cpp/_005f_005fhas_005fattribute.html)
otherwise it fails, eg. on the reference Centos 7 build. Fixed.
Quoted 4 lines
kerncompat.h +4
-0+# define fallthrough __attribute__((__fallthrough__)) +#else +# define fallthrough do {} while (0)/* fallthrough */ +#endif
PATCH 4/5 btrfs-progs: move a union with variable sized type to the end
In reply to Qu Wenruo[WARNING]
Clang 15.0.7 gives the following warning:
image/main.c:95:2: warning: field '' with variable sized type 'union metadump_struct::(anonymous at image/main.c:95:2)' not at the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end]
union {
^[CAUSE]
The union contains meta_cluster, which variable sized:
struct meta_cluster {
struct meta_cluster_header header;
struct meta_cluster_item items[];
} __attribute__ ((__packed__));Thus clang gives above warning since it's a GNU extension.
[FIX]
Just move the union to the end of the structure.
--- image/main.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
image/main.c +5 -5
@@ -92,11 +92,6 @@ struct metadump_struct {struct btrfs_root *root;FILE *out; -union { -struct meta_cluster cluster; -char meta_cluster_bytes[IMAGE_BLOCK_SIZE ]; - }; -pthread_t threads[MAX_WORKER_THREADS ];size_t num_threads;pthread_mutex_t mutex;
@@ -119,6 +114,11 @@ struct metadump_struct {enum sanitize_mode sanitize_names;int error; + +union { +struct meta_cluster cluster; +char meta_cluster_bytes[IMAGE_BLOCK_SIZE ]; + }; };struct mdrestore_struct {
Signature · 1 line
2.39.1
PATCH 5/5 btrfs-progs: fix set but not used variables
In reply to Qu Wenruo[WARNING]
Clang 15.0.7 warns about several unused variables:
kernel-shared/zoned.c:829:6: warning: variable 'num_sequential' set but not used [-Wunused-but-set-variable]
u32 num_sequential = 0, num_conventional = 0;
^
cmds/scrub.c:1174:6: warning: variable 'n_skip' set but not used [-Wunused-but-set-variable]
int n_skip = 0;
^
mkfs/main.c:493:6: warning: variable 'total_block_count' set but not used [-Wunused-but-set-variable]
u64 total_block_count = 0;
^
image/main.c:2246:6: warning: variable 'bytenr' set but not used [-Wunused-but-set-variable]
u64 bytenr = 0;
^[CAUSE]
Most of them just straightforward set but not used variables.
The only exception is total_block_count, which has commented out code relying on it.
[FIX]
Just remove those variables, and for @total_block_count, also remove the comments.
--- cmds/scrub.c | 2 -- image/main.c | 3 --- kernel-shared/zoned.c | 6 ++---- mkfs/main.c | 4 ---- 4 files changed, 2 insertions(+), 13 deletions(-)
cmds/scrub.c +0 -2
@@ -1171,7 +1171,6 @@ static int scrub_start(const struct cmd_struct *cmd, int argc, char **argv,int ioprio_class =IOPRIO_CLASS_IDLE ;int ioprio_classdata =0 ;int n_start =0 ; -int n_skip =0 ;int n_resume =0 ;struct btrfs_ioctl_fs_info_args fi_args;struct btrfs_ioctl_dev_info_args *di_args =NULL ;
@@ -1337,7 +1336,6 @@ static int scrub_start(const struct cmd_struct *cmd, int argc, char **argv, sp[i].scrub_args.start = last_scrub->p.last_physical; sp[i].resumed = last_scrub; }else if (resume) { - ++n_skip; sp[i].skip =1 ; sp[i].resumed = last_scrub;continue ;
image/main.c +0 -3
@@ -2243,7 +2243,6 @@ static int build_chunk_tree(struct mdrestore_struct *mdres,struct meta_cluster_header *header;struct meta_cluster_item *item =NULL ;u32 i, nritems; -u64 bytenr =0 ;u8 *buffer;int ret;
@@ -2265,7 +2264,6 @@ static int build_chunk_tree(struct mdrestore_struct *mdres,return -EIO ; } - bytenr +=IMAGE_BLOCK_SIZE ; mdres->compress_method = header->compress; nritems =le32_to_cpu (header->nritems);for (i =0 ; i < nritems; i++) {
@@ -2273,7 +2271,6 @@ static int build_chunk_tree(struct mdrestore_struct *mdres,if (le64_to_cpu (item->bytenr) ==BTRFS_SUPER_INFO_OFFSET )break ; - bytenr +=le32_to_cpu (item->size);if (fseek (mdres->in,le32_to_cpu (item->size),SEEK_CUR )) {error ("seek failed: %m" );return -EIO ;
kernel-shared/zoned.c +2 -4
@@ -826,7 +826,7 @@ int btrfs_load_block_group_zone_info(struct btrfs_fs_info *fs_info,int i;u64 *alloc_offsets =NULL ;u64 last_alloc =0 ; -u32 num_sequential =0 , num_conventional =0 ; +u32 num_conventional =0 ;if (!btrfs_is_zoned (fs_info))return 0 ;
@@ -870,9 +870,7 @@ int btrfs_load_block_group_zone_info(struct btrfs_fs_info *fs_info, } is_sequential =btrfs_dev_is_sequential (device, physical); -if (is_sequential) - num_sequential++; -else +if (!is_sequential) num_conventional++;if (!is_sequential) {
mkfs/main.c +0 -4
@@ -490,7 +490,6 @@ static void list_all_devices(struct btrfs_root *root)struct btrfs_fs_devices *fs_devices;struct btrfs_device *device;int number_of_devices =0 ; -u64 total_block_count =0 ; fs_devices = root->fs_info->fs_devices;
@@ -500,8 +499,6 @@ static void list_all_devices(struct btrfs_root *root)list_sort (NULL , &fs_devices->devices, _cmp_device_by_id);printf ("Number of devices: %d\n" , number_of_devices); -/* printf("Total devices size: %10s\n", */ -/* pretty_size(total_block_count)); */ printf ("Devices:\n" );printf (" ID SIZE PATH\n" );list_for_each_entry (device, &fs_devices->devices, dev_list) {
@@ -509,7 +506,6 @@ static void list_all_devices(struct btrfs_root *root) device->devid,pretty_size (device->total_bytes), device->name); - total_block_count += device->total_bytes; }printf ("\n" );
Signature · 1 line
2.39.1
Quoted 2 lines · Qu Wenruo
Recently I'm migrating my default compiler from GCC to clang, mostly to get short comiling time (especially important on my aarch64 machines).
Just to mention, although the cleanup itself still makes sense, it doesn't make any sense to migrate to clang, at least on my aarch64 machines.
GCC takes only 22min for my trimmed kernel config, while the same config takes clang 32min...
Thanks,
Qu
Quoted 32 lines
Thus I hit those (mostly false alerts) warnings in btrfs-progs, and come up with this patchset.
Unfortunately there is still libbtrfsutils causing warnings in setuptools, as it's still using the default flags from gcc no matter what.
Qu Wenruo (5):
btrfs-progs: remove an unnecessary branch to silent the clang warning
btrfs-progs: fix a false alert on an uninitialized variable when
BUG_ON()is involved.
btrfs-progs: fix fallthrough cases with proper attributes
btrfs-progs: move a union with variable sized type to the end
btrfs-progs: fix set but not used variablescmds/reflink.c | 2 +- cmds/scrub.c | 12 +++--- common/format-output.c | 2 +- common/parse-utils.c | 12 +++--- common/units.c | 6 +-- crypto/xxhash.c | 73 +++++++++++++++++++------------------ image/main.c | 15 +++----- kerncompat.h | 8 ++++ kernel-shared/ctree.c | 18 +++++---- kernel-shared/extent-tree.c | 4 +- kernel-shared/print-tree.c | 2 +- kernel-shared/volumes.c | 3 +- kernel-shared/zoned.c | 6 +-- mkfs/main.c | 4 -- 14 files changed, 84 insertions(+), 83 deletions(-)
Hi Qu,
I think maybe clang has some optimization which GCC doesn't.
Thanks
Quoted 45 lines · Qu Wenruo
Quoted 2 lines · Qu Wenruo
Recently I'm migrating my default compiler from GCC to clang, mostly to get short comiling time (especially important on my aarch64 machines).
Just to mention, although the cleanup itself still makes sense, it doesn't make any sense to migrate to clang, at least on my aarch64 machines.
GCC takes only 22min for my trimmed kernel config, while the same config takes clang 32min...
Thanks,
QuQuoted 32 lines
Thus I hit those (mostly false alerts) warnings in btrfs-progs, and come up with this patchset.
Unfortunately there is still libbtrfsutils causing warnings in setuptools, as it's still using the default flags from gcc no matter what.
Qu Wenruo (5):
btrfs-progs: remove an unnecessary branch to silent the clang warning
btrfs-progs: fix a false alert on an uninitialized variable whenBUG_ON()is involved.
btrfs-progs: fix fallthrough cases with proper attributes
btrfs-progs: move a union with variable sized type to the end
btrfs-progs: fix set but not used variablescmds/reflink.c | 2 +-
cmds/scrub.c | 12 +++--- common/format-output.c | 2 +-
common/parse-utils.c | 12 +++---
common/units.c | 6 +--
crypto/xxhash.c | 73 +++++++++++++++++++------------------ image/main.c | 15 +++-----
kerncompat.h | 8 ++++ kernel-shared/ctree.c | 18 +++++----kernel-shared/extent-tree.c| 4 +- kernel-shared/print-tree.c | 2 +- kernel-shared/volumes.c | 3 +-
kernel-shared/zoned.c | 6 +--
mkfs/main.c | 4 --14 files changed, 84 insertions(+), 83 deletions(-)
Quoted 10 lines · Qu Wenruo
Recently I'm migrating my default compiler from GCC to clang, mostly to get short comiling time (especially important on my aarch64 machines).
Thus I hit those (mostly false alerts) warnings in btrfs-progs, and come up with this patchset.
Unfortunately there is still libbtrfsutils causing warnings in setuptools, as it's still using the default flags from gcc no matter what.
Quoted 7 lines
Qu Wenruo (5):
btrfs-progs: remove an unnecessary branch to silent the clang warning
btrfs-progs: fix a false alert on an uninitialized variable when
BUG_ON()is involved.
btrfs-progs: fix fallthrough cases with proper attributes
btrfs-progs: move a union with variable sized type to the end
btrfs-progs: fix set but not used variables
Added to devel, thanks.