soc/tegra: fuse: Fix bitwise vs. logical OR warning
PATCH 1/1 soc/tegra: fuse: Fix bitwise vs. logical OR warning
A new warning in clang points out two instances where boolean expressions are being used with a bitwise OR instead of logical OR:
drivers/soc/tegra/fuse/speedo-tegra20.c:72:9: warning: use of bitwise '|' with boolean operands [-Wbitwise-instead-of-logical]
reg = tegra_fuse_read_spare(i) |
^~~~~~~~~~~~~~~~~~~~~~~~~~
||
drivers/soc/tegra/fuse/speedo-tegra20.c:72:9: note: cast one or both operands to int to silence this warning
drivers/soc/tegra/fuse/speedo-tegra20.c:87:9: warning: use of bitwise '|' with boolean operands [-Wbitwise-instead-of-logical]
reg = tegra_fuse_read_spare(i) |
^~~~~~~~~~~~~~~~~~~~~~~~~~
||drivers/soc/tegra/fuse/speedo-tegra20.c:87:9: note: cast one or both operands to int to silence this warning 2 warnings generated.
The motivation for the warning is that logical operations short circuit while bitwise operations do not. In this case, it does not seem like short circuiting is harmful so implement the suggested fix of changing to a logical operation to fix the warning.
--- drivers/soc/tegra/fuse/speedo-tegra20.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/soc/tegra/fuse/speedo-tegra20.c +2 -2
@@ -69,7 +69,7 @@ void __init tegra20_init_speedo_data(struct tegra_sku_info *sku_info) val =0 ;for (i =CPU_SPEEDO_MSBIT ; i >=CPU_SPEEDO_LSBIT ; i--) { - reg =tegra_fuse_read_spare (i) | + reg =tegra_fuse_read_spare (i) ||tegra_fuse_read_spare (i +CPU_SPEEDO_REDUND_OFFS ); val = (val <<1 ) | (reg &0x1 ); }
@@ -84,7 +84,7 @@ void __init tegra20_init_speedo_data(struct tegra_sku_info *sku_info) val =0 ;for (i =SOC_SPEEDO_MSBIT ; i >=SOC_SPEEDO_LSBIT ; i--) { - reg =tegra_fuse_read_spare (i) | + reg =tegra_fuse_read_spare (i) ||tegra_fuse_read_spare (i +SOC_SPEEDO_REDUND_OFFS ); val = (val <<1 ) | (reg &0x1 ); }
base-commit: 519d81956ee277b4419c723adfb154603c2565ba
Signature · 1 line
2.33.1.637.gf443b226ca
On Thu, Oct 21, 2021 at 02:45:00PM -0700, Nathan Chancellor wrote:
[...]
Quoted 12 lines
drivers/soc/tegra/fuse/speedo-tegra20.c +1
-1@@ -69,7 +69,7 @@ void __init tegra20_init_speedo_data(struct tegra_sku_info *sku_info) val =0 ;for (i =CPU_SPEEDO_MSBIT ; i >=CPU_SPEEDO_LSBIT ; i--) { - reg =tegra_fuse_read_spare (i) | + reg =tegra_fuse_read_spare (i) ||tegra_fuse_read_spare (i +CPU_SPEEDO_REDUND_OFFS ); val = (val <<1 ) | (reg &0x1 ); }
drivers/soc/tegra/fuse/speedo-tegra20.c void __init tegra20_init_speedo_data(struct tegra_sku_info *sku_info) · line 90
Quoted 8 lines
drivers/soc/tegra/fuse/speedo-tegra20.c +1
-1@@ -84,7 +84,7 @@ void __init tegra20_init_speedo_data(struct tegra_sku_info *sku_info) val =0 ;for (i =SOC_SPEEDO_MSBIT ; i >=SOC_SPEEDO_LSBIT ; i--) { - reg =tegra_fuse_read_spare (i) | + reg =tegra_fuse_read_spare (i) ||tegra_fuse_read_spare (i +SOC_SPEEDO_REDUND_OFFS ); val = (val <<1 ) | (reg &0x1 ); }
It does seem correct, but nevertheless the code looks suspicious. reg is already masked with 0x1 as far as I can tell, and there are other places which depend on this (like speedo-tegra210.c). Guessing from the use of tegra_fuse_read_spare() I would recommend changing its return type as it is returing a bit value, not necessarily semantically a boolean.
Best Regards
Michał Mirosław
Hi Michał,
Quoted 15 lines · Michał Mirosław
On Thu, Oct 21, 2021 at 02:45:00PM -0700, Nathan Chancellor wrote:
[...]Quoted 13 lines
drivers/soc/tegra/fuse/speedo-tegra20.c +1
-1@@ -69,7 +69,7 @@ void __init tegra20_init_speedo_data(struct tegra_sku_info *sku_info) val =0 ;for (i =CPU_SPEEDO_MSBIT ; i >=CPU_SPEEDO_LSBIT ; i--) { - reg =tegra_fuse_read_spare (i) | + reg =tegra_fuse_read_spare (i) ||tegra_fuse_read_spare (i +CPU_SPEEDO_REDUND_OFFS ); val = (val <<1 ) | (reg &0x1 ); }@@ -84,7 +84,7 @@ void __init tegra20_init_speedo_data(struct tegra_sku_info *sku_info)
Quoted 13 lines
drivers/soc/tegra/fuse/speedo-tegra20.c void __init tegra20_init_speedo_data(struct tegra_sku_info *sku_info) · line 90
Quoted 7 lines
drivers/soc/tegra/fuse/speedo-tegra20.c +1
-1val =0 ;for (i =SOC_SPEEDO_MSBIT ; i >=SOC_SPEEDO_LSBIT ; i--) { - reg =tegra_fuse_read_spare (i) | + reg =tegra_fuse_read_spare (i) ||tegra_fuse_read_spare (i +SOC_SPEEDO_REDUND_OFFS ); val = (val <<1 ) | (reg &0x1 ); }It does seem correct, but nevertheless the code looks suspicious. reg is already masked with
0x1as far as I can tell, and there are other places which depend on this (likespeedo-tegra210.c). Guessing from the use oftegra_fuse_read_spare()I would recommend changing its return type as it is returing a bit value, not necessarily semantically a boolean.
Yes, I did notice that, as well as the use of tegra_fuse_read_spare() with boolean operators in tegra-apbmisc.c. I could change it to int if that is what the maintainers prefer, which would also solve the warning.
Cheers,
Nathan
+ Anders (combining
https://lore.kernel.org/all/[email protected]/).
Quoted 16 lines · Nathan Chancellor
A new warning in clang points out two instances where boolean expressions are being used with a bitwise OR instead of logical OR:
drivers/soc/tegra/fuse/speedo-tegra20.c:72:9: warning: use of bitwise '|' with boolean operands [-Wbitwise-instead-of-logical]reg = tegra_fuse_read_spare(i) | ^~~~~~~~~~~~~~~~~~~~~~~~~~ || drivers/soc/tegra/fuse/speedo-tegra20.c:72:9: note: cast one or both operands to int to silence this warning drivers/soc/tegra/fuse/speedo-tegra20.c:87:9: warning: use of bitwise '|' with boolean operands [-Wbitwise-instead-of-logical] reg = tegra_fuse_read_spare(i) | ^~~~~~~~~~~~~~~~~~~~~~~~~~ ||
drivers/soc/tegra/fuse/speedo-tegra20.c:87:9: note: cast one or both operands to int to silence this warning 2 warnings generated.
Quoted 7 lines
The motivation for the warning is that logical operations short circuit while bitwise operations do not. In this case, it does not seem like short circuiting is harmful so implement the suggested fix of changing to a logical operation to fix the warning.
Quoted 31 lines
--- drivers/soc/tegra/fuse/speedo-tegra20.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/soc/tegra/fuse/speedo-tegra20.c +2
-2@@ -69,7 +69,7 @@ void __init tegra20_init_speedo_data(struct tegra_sku_info *sku_info) val =0 ;for (i =CPU_SPEEDO_MSBIT ; i >=CPU_SPEEDO_LSBIT ; i--) { - reg =tegra_fuse_read_spare (i) | + reg =tegra_fuse_read_spare (i) ||tegra_fuse_read_spare (i +CPU_SPEEDO_REDUND_OFFS ); val = (val <<1 ) | (reg &0x1 ); }@@ -84,7 +84,7 @@ void __init tegra20_init_speedo_data(struct tegra_sku_info *sku_info) val =0 ;for (i =SOC_SPEEDO_MSBIT ; i >=SOC_SPEEDO_LSBIT ; i--) { - reg =tegra_fuse_read_spare (i) | + reg =tegra_fuse_read_spare (i) ||tegra_fuse_read_spare (i +SOC_SPEEDO_REDUND_OFFS ); val = (val <<1 ) | (reg &0x1 ); }base-commit:
519d81956ee277b4419c723adfb154603c2565ba--
2.33.1.637.gf443b226ca
Signature · 2 lines
Thanks, ~Nick Desaulniers