Message ID | 20211018182537.2316800-1-nathan@kernel.org (mailing list archive) |
---|---|
State | Accepted, archived |
Headers | show |
Series | platform/x86: thinkpad_acpi: Fix bitwise vs. logical warning | expand |
On Mon, Oct 18, 2021 at 11:26 AM Nathan Chancellor <nathan@kernel.org> wrote: > > A new warning in clang points out a use of bitwise OR with boolean > expressions in this driver: > > drivers/platform/x86/thinkpad_acpi.c:9061:11: error: use of bitwise '|' with boolean operands [-Werror,-Wbitwise-instead-of-logical] > else if ((strlencmp(cmd, "level disengaged") == 0) | > ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > || > drivers/platform/x86/thinkpad_acpi.c:9061:11: note: cast one or both operands to int to silence this warning > 1 error generated. > > This should clearly be a logical OR so change it to fix the warning. > > Fixes: fe98a52ce754 ("ACPI: thinkpad-acpi: add sysfs support to fan subdriver") > Link: https://github.com/ClangBuiltLinux/linux/issues/1476 > Reported-by: Tor Vic <torvic9@mailbox.org> > Signed-off-by: Nathan Chancellor <nathan@kernel.org> LGTM, thanks for the patch! I guess this would be the first "interesting" case this warning has found in kernel sources? Reviewed-by: Nick Desaulniers <ndesaulniers@google.com> > --- > drivers/platform/x86/thinkpad_acpi.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/drivers/platform/x86/thinkpad_acpi.c b/drivers/platform/x86/thinkpad_acpi.c > index 07b9710d500e..7442c3bb446a 100644 > --- a/drivers/platform/x86/thinkpad_acpi.c > +++ b/drivers/platform/x86/thinkpad_acpi.c > @@ -9058,7 +9058,7 @@ static int fan_write_cmd_level(const char *cmd, int *rc) > > if (strlencmp(cmd, "level auto") == 0) > level = TP_EC_FAN_AUTO; > - else if ((strlencmp(cmd, "level disengaged") == 0) | > + else if ((strlencmp(cmd, "level disengaged") == 0) || > (strlencmp(cmd, "level full-speed") == 0)) > level = TP_EC_FAN_FULLSPEED; > else if (sscanf(cmd, "level %d", &level) != 1) > > base-commit: 85303db36b6e170917a7bc6aae4898c31a5272a0 > -- > 2.33.1.637.gf443b226ca >
On Mon, Oct 18, 2021 at 8:34 AM Nick Desaulniers <ndesaulniers@google.com> wrote: > > LGTM, thanks for the patch! I guess this would be the first > "interesting" case this warning has found in kernel sources? The patch looks obviously correct (tm), but I'm not convinced that the warning is actually all that interesting. The thing is, using bitwise operators for booleans is _exactly_ the same as using logical ones as long as there are no side effects. In fact, any compiler worth its salt will already convert some cases between the two as an optimization just as part of code generation. Of course, that "as long as there are no side effects" is the big thing - then the short-circuiting of the actual logical operations clearly matters. But that wasn't actually the case in this situation (or in the kvm situation elsewhere). So in both of these cases, the difference between "|" and "||" ends up purely being a hint to the compiler. In this case, even if there are no side effects, it's clearly pointless to do the second strlencmp() if the first one already matched, and the "||" is unquestionably the right hint (and honestly, most compilers probably wouldn't even be able to tell "no side effects" because it's a fairly complex expression - but since it's inlined and uses compiler intrinsics, the compiler _might_ actually be able to see that the two are equivalent). But no, I don't think that warning is very interesting. In fact, the warning might be overall detrimental, in case the hints were intentional (like the kvm case - although I'm not convinced the kvm hinting was actually meaningful). Linus
On Mon, Oct 18, 2021 at 12:41 PM Linus Torvalds <torvalds@linux-foundation.org> wrote: > > On Mon, Oct 18, 2021 at 8:34 AM Nick Desaulniers > <ndesaulniers@google.com> wrote: > > > > LGTM, thanks for the patch! I guess this would be the first > > "interesting" case this warning has found in kernel sources? > > The patch looks obviously correct (tm), but I'm not convinced that the > warning is actually all that interesting. > > The thing is, using bitwise operators for booleans is _exactly_ the > same as using logical ones as long as there are no side effects. Right, the patch that added the warning explicitly checks for side effects. https://reviews.llvm.org/D108003 https://lore.kernel.org/lkml/20211018193101.2340261-1-nathan@kernel.org/ is another example that I would point to in favor of the error.
On Mon, Oct 18, 2021 at 10:14 AM Nick Desaulniers <ndesaulniers@google.com> wrote: > > Right, the patch that added the warning explicitly checks for side effects. Well, it's a bit questionable. The "side effects" are things like any pointer dereference, because it could fault, but if you know that isn't an issue, then clang basically ends up complaining about code that is perfectly fine. Maybe it was written that way on purpose, like the kvm code. Now, it's probably not worth keeping that "bitops of booleans" logic - if it is a noticeable optimization, it's generally something that the compiler should do for us, but basically clang is warning about perfectly valid code. And what I find absolutely disgusting is the suggested "fix" that clang gives you. If the warning said "maybe you meant to use a logical or (||)", then that would be one thing. But what clang suggests as the "fix" for the warning is just bad coding practice. If a warning fix involves making the code uglier, then the warning fix is wrong. This is not the first time we've had compilers suggesting garbage. Gcc used to suggest (perhaps still does) the "extra parenthesis" for "assignment used as a truth value" situation. Which is - once again - disgusting garbage. Writing code like if (a = b) .. is bad and error prone. But the suggestion to "fix" the warning with if ((a = b)) .. is just completely unacceptably stupid, and is just BAD CODE. The proper fix might be to write it like if ((a = b) != 0) ... which at least makes the truth value part explicit - in ways that a silly double parenthesis does not. Or, better yet, write it as a = b; if (a) .. instead, which is legible and fine. The clang suggestion to add a cast to 'int' to avoid the warning is the same kind of "write bad code" suggestion. Just don't do it. Linus
On Mon, Oct 18, 2021 at 05:38:09PM -1000, Linus Torvalds wrote: > On Mon, Oct 18, 2021 at 10:14 AM Nick Desaulniers > <ndesaulniers@google.com> wrote: > > > > Right, the patch that added the warning explicitly checks for side effects. > > Well, it's a bit questionable. The "side effects" are things like any > pointer dereference, because it could fault, but if you know that > isn't an issue, then clang basically ends up complaining about code > that is perfectly fine. Maybe it was written that way on purpose, like > the kvm code. > > Now, it's probably not worth keeping that "bitops of booleans" logic - > if it is a noticeable optimization, it's generally something that the > compiler should do for us, but basically clang is warning about > perfectly valid code. > > And what I find absolutely disgusting is the suggested "fix" that > clang gives you. > > If the warning said "maybe you meant to use a logical or (||)", then > that would be one thing. But what clang suggests as the "fix" for the > warning is just bad coding practice. For what it's worth, the suggested fix is the '||' underneath the warning text: In file included from arch/x86/kvm/mmu/tdp_iter.c:5: arch/x86/kvm/mmu/spte.h:318:9: error: use of bitwise '|' with boolean operands [-Werror,-Wbitwise-instead-of-logical] return __is_bad_mt_xwr(rsvd_check, spte) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ || arch/x86/kvm/mmu/spte.h:318:9: note: cast one or both operands to int to silence this warning 1 error generated. Perhaps that hint should also be added to the warning text, like: In file included from arch/x86/kvm/mmu/tdp_iter.c:5: arch/x86/kvm/mmu/spte.h:318:9: error: use of bitwise '|' with boolean operands; did you mean logical '||'? [-Werror,-Wbitwise-instead-of-logical] return __is_bad_mt_xwr(rsvd_check, spte) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ || arch/x86/kvm/mmu/spte.h:318:9: note: cast one or both operands to int to silence this warning 1 error generated. It is late for me but I can push that change to the clang developers and see what they think tomorrow if that would help? Cheers, Nathan
On Mon, Oct 18, 2021 at 7:00 PM Nathan Chancellor <nathan@kernel.org> wrote: > > For what it's worth, the suggested fix is the '||' underneath the > warning text: > > In file included from arch/x86/kvm/mmu/tdp_iter.c:5: > arch/x86/kvm/mmu/spte.h:318:9: error: use of bitwise '|' with boolean operands [-Werror,-Wbitwise-instead-of-logical] > return __is_bad_mt_xwr(rsvd_check, spte) | > ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > || > arch/x86/kvm/mmu/spte.h:318:9: note: cast one or both operands to int to silence this warning > 1 error generated. Hmm. That's not at all obvious. The *much* bigger part is that note: cast one or both operands to int to silence this warning which is what I'm complaining about. That note should die. It should say "maybe you meant to use a logical or" or something like that. > Perhaps that hint should also be added to the warning text, like: > > In file included from arch/x86/kvm/mmu/tdp_iter.c:5: > arch/x86/kvm/mmu/spte.h:318:9: error: use of bitwise '|' with boolean operands; did you mean logical '||'? [-Werror,-Wbitwise-instead-of-logical] > return __is_bad_mt_xwr(rsvd_check, spte) | > ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > || > arch/x86/kvm/mmu/spte.h:318:9: note: cast one or both operands to int to silence this warning I don't understand why you seem to continue to ignore the "note" message, which makes a completely crazy suggestion. Linus
Hi, On 10/18/21 20:25, Nathan Chancellor wrote: > A new warning in clang points out a use of bitwise OR with boolean > expressions in this driver: > > drivers/platform/x86/thinkpad_acpi.c:9061:11: error: use of bitwise '|' with boolean operands [-Werror,-Wbitwise-instead-of-logical] > else if ((strlencmp(cmd, "level disengaged") == 0) | > ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > || > drivers/platform/x86/thinkpad_acpi.c:9061:11: note: cast one or both operands to int to silence this warning > 1 error generated. > > This should clearly be a logical OR so change it to fix the warning. > > Fixes: fe98a52ce754 ("ACPI: thinkpad-acpi: add sysfs support to fan subdriver") > Link: https://github.com/ClangBuiltLinux/linux/issues/1476 > Reported-by: Tor Vic <torvic9@mailbox.org> > Signed-off-by: Nathan Chancellor <nathan@kernel.org> Thank you for your patch, I've applied this patch to my review-hans branch: https://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git/log/?h=review-hans Note it will show up in my review-hans branch once I've pushed my local branch there, which might take a while. Once I've run some tests on this branch the patches there will be added to the platform-drivers-x86/for-next branch and eventually will be included in the pdx86 pull-request to Linus for the next merge-window. Regards, Hans > --- > drivers/platform/x86/thinkpad_acpi.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/drivers/platform/x86/thinkpad_acpi.c b/drivers/platform/x86/thinkpad_acpi.c > index 07b9710d500e..7442c3bb446a 100644 > --- a/drivers/platform/x86/thinkpad_acpi.c > +++ b/drivers/platform/x86/thinkpad_acpi.c > @@ -9058,7 +9058,7 @@ static int fan_write_cmd_level(const char *cmd, int *rc) > > if (strlencmp(cmd, "level auto") == 0) > level = TP_EC_FAN_AUTO; > - else if ((strlencmp(cmd, "level disengaged") == 0) | > + else if ((strlencmp(cmd, "level disengaged") == 0) || > (strlencmp(cmd, "level full-speed") == 0)) > level = TP_EC_FAN_FULLSPEED; > else if (sscanf(cmd, "level %d", &level) != 1) > > base-commit: 85303db36b6e170917a7bc6aae4898c31a5272a0 >
diff --git a/drivers/platform/x86/thinkpad_acpi.c b/drivers/platform/x86/thinkpad_acpi.c index 07b9710d500e..7442c3bb446a 100644 --- a/drivers/platform/x86/thinkpad_acpi.c +++ b/drivers/platform/x86/thinkpad_acpi.c @@ -9058,7 +9058,7 @@ static int fan_write_cmd_level(const char *cmd, int *rc) if (strlencmp(cmd, "level auto") == 0) level = TP_EC_FAN_AUTO; - else if ((strlencmp(cmd, "level disengaged") == 0) | + else if ((strlencmp(cmd, "level disengaged") == 0) || (strlencmp(cmd, "level full-speed") == 0)) level = TP_EC_FAN_FULLSPEED; else if (sscanf(cmd, "level %d", &level) != 1)
A new warning in clang points out a use of bitwise OR with boolean expressions in this driver: drivers/platform/x86/thinkpad_acpi.c:9061:11: error: use of bitwise '|' with boolean operands [-Werror,-Wbitwise-instead-of-logical] else if ((strlencmp(cmd, "level disengaged") == 0) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ || drivers/platform/x86/thinkpad_acpi.c:9061:11: note: cast one or both operands to int to silence this warning 1 error generated. This should clearly be a logical OR so change it to fix the warning. Fixes: fe98a52ce754 ("ACPI: thinkpad-acpi: add sysfs support to fan subdriver") Link: https://github.com/ClangBuiltLinux/linux/issues/1476 Reported-by: Tor Vic <torvic9@mailbox.org> Signed-off-by: Nathan Chancellor <nathan@kernel.org> --- drivers/platform/x86/thinkpad_acpi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) base-commit: 85303db36b6e170917a7bc6aae4898c31a5272a0