x86: kvm: avoid constant-conversion warning [Linux 4.14.137]

This Linux kernel change "x86: kvm: avoid constant-conversion warning" is included in the Linux 4.14.137 release. This change is authored by Arnd Bergmann <arnd [at] arndb.de> on Fri Jul 12 11:12:30 2019 +0200. The commit for this change in Linux stable tree is 9e5ba38 (patch) which is from upstream commit a6a6d3b. The same Linux upstream change may have been applied to various maintained Linux releases and you can find all Linux releases containing changes from upstream a6a6d3b.

x86: kvm: avoid constant-conversion warning

[ Upstream commit a6a6d3b1f867d34ba5bd61aa7bb056b48ca67cff ]

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.

Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Link: https://github.com/ClangBuiltLinux/linux/issues/95
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>

There are 6 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 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 f97b533..87a0601 100644
--- a/arch/x86/kvm/mmu.c
+++ b/arch/x86/kvm/mmu.c
@@ -4313,11 +4313,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 */

Leave a Reply

Your email address will not be published. Required fields are marked *