Re: [PATCH 2/2] x86: kvm: avoid constant-conversion warning

From: Sedat Dilek
Date: Fri Jul 12 2019 - 05:30:26 EST


On Fri, Jul 12, 2019 at 11:12 AM Arnd Bergmann <arnd@xxxxxxxx> wrote:
>
> clang finds a contruct suspicious that converts an unsigned
> character to a signed integer and back, causing an overflow:
>
> arch/x86/kvm/mmu.c:4605:39: error: implicit conversion from 'int' to 'u8' (aka 'unsigned char') changes value from -205 to 51 [-Werror,-Wconstant-conversion]
> u8 wf = (pfec & PFERR_WRITE_MASK) ? ~w : 0;
> ~~ ^~
> arch/x86/kvm/mmu.c:4607:38: error: implicit conversion from 'int' to 'u8' (aka 'unsigned char') changes value from -241 to 15 [-Werror,-Wconstant-conversion]
> u8 uf = (pfec & PFERR_USER_MASK) ? ~u : 0;
> ~~ ^~
> arch/x86/kvm/mmu.c:4609:39: error: implicit conversion from 'int' to 'u8' (aka 'unsigned char') changes value from -171 to 85 [-Werror,-Wconstant-conversion]
> u8 ff = (pfec & PFERR_FETCH_MASK) ? ~x : 0;
> ~~ ^~
>
> Add an explicit cast to tell clang that everything works as
> intended here.
>

Feel free to add:

Link: https://github.com/ClangBuiltLinux/linux/issues/95
( See also patch proposal of Matthias Kaehlcke )

I had a different "simpler" approach to not see this anymore :-).
( See attached 2 patches )

- Sedat -


> Signed-off-by: Arnd Bergmann <arnd@xxxxxxxx>
> ---
> arch/x86/kvm/mmu.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c
> index 17ece7b994b1..aea7f969ecb8 100644
> --- a/arch/x86/kvm/mmu.c
> +++ b/arch/x86/kvm/mmu.c
> @@ -4602,11 +4602,11 @@ static void update_permission_bitmask(struct kvm_vcpu *vcpu,
> */
>
> /* Faults from writes to non-writable pages */
> - u8 wf = (pfec & PFERR_WRITE_MASK) ? ~w : 0;
> + u8 wf = (pfec & PFERR_WRITE_MASK) ? (u8)~w : 0;
> /* Faults from user mode accesses to supervisor pages */
> - u8 uf = (pfec & PFERR_USER_MASK) ? ~u : 0;
> + u8 uf = (pfec & PFERR_USER_MASK) ? (u8)~u : 0;
> /* Faults from fetches of non-executable pages*/
> - u8 ff = (pfec & PFERR_FETCH_MASK) ? ~x : 0;
> + u8 ff = (pfec & PFERR_FETCH_MASK) ? (u8)~x : 0;
> /* Faults from kernel mode fetches of user pages */
> u8 smepf = 0;
> /* Faults from kernel mode accesses of user pages */
> --
> 2.20.0
>
> --
> You received this message because you are subscribed to the Google Groups "Clang Built Linux" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to clang-built-linux+unsubscribe@xxxxxxxxxxxxxxxxx
> To view this discussion on the web visit https://groups.google.com/d/msgid/clang-built-linux/20190712091239.716978-2-arnd%40arndb.de.
From bfc32d7f6c8d5bd7766e4292e25a184743a4b3b1 Mon Sep 17 00:00:00 2001
From: Sedat Dilek <sedat.dilek@credativ.de>
Date: Tue, 19 Mar 2019 08:56:02 +0100
Subject: [PATCH] kbuild: Enable -Wconstant-conversion warning for "make W=3"

---
scripts/Makefile.extrawarn | 1 +
1 file changed, 1 insertion(+)

diff --git a/scripts/Makefile.extrawarn b/scripts/Makefile.extrawarn
index 768306add591..eb831a8c6a20 100644
--- a/scripts/Makefile.extrawarn
+++ b/scripts/Makefile.extrawarn
@@ -47,6 +47,7 @@ warning-2 += $(call cc-option, -Wunused-macros)
warning-3 := -Wbad-function-cast
warning-3 += -Wcast-qual
warning-3 += -Wconversion
+warning-3 += $(call cc-option, -Wconstant-conversion)
warning-3 += -Wpacked
warning-3 += -Wpadded
warning-3 += -Wpointer-arith
--
2.20.1

From e958c1b7b3353372a8f6de87c55ffa95392314e6 Mon Sep 17 00:00:00 2001
From: Sedat Dilek <sedat.dilek@credativ.de>
Date: Tue, 19 Mar 2019 23:39:32 +0100
Subject: [PATCH] x86: kvm: clang: Disable -Wconstant-conversion warning

---
arch/x86/kvm/Makefile | 1 +
1 file changed, 1 insertion(+)

diff --git a/arch/x86/kvm/Makefile b/arch/x86/kvm/Makefile
index 31ecf7a76d5a..54dd52c2e927 100644
--- a/arch/x86/kvm/Makefile
+++ b/arch/x86/kvm/Makefile
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: GPL-2.0

ccflags-y += -Iarch/x86/kvm
+ccflags-y += $(call cc-disable-warning, constant-conversion)

KVM := ../../../virt/kvm

--
2.20.1