x86: fix bitops.h warning with a moved cast
Subsystems (4)
PATCH v3 1/2 x86: fix bitops.h warning with a moved cast
Fix many sparse warnings when building with C=1.
When the kernel is compiled with C=1, there are lots of messages like:
arch/x86/include/asm/bitops.h:77:37: warning: cast truncates bits from constant value (ffffff7f becomes 7f)
CONST_MASK() is using a signed integer "1" to create the mask which is later cast to (u8) when used. Move the cast to the definition and clean up the calling sites to prevent sparse from warning.
The reason the warning was occurring is because certain bitmasks that end with a mask next to a natural boundary like 7, 15, 23, 31, end up with a mask like 0x7f, which then results in sign extension when doing an invert (but I'm not a compiler expert). It was really only "clear_bit" that was having problems, and it was only on bit checks next to a byte boundary (top bit).
Verified with a test module (see next patch) and assembly inspection that the patch doesn't introduce any change in generated code.
---
v3: Clean up
the header file changes as per peterz.
v2: use correct CC: list
--- arch/x86/include/asm/bitops.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/include/asm/bitops.h +3 -3
@@ -46,7 +46,7 @@* a mask operation on a byte. */ #define CONST_MASK_ADDR (nr, addr) WBYTE_ADDR((void *)(addr) + ((nr)>>3)) -#define CONST_MASK (nr) (1 << ((nr) & 7)) +#define CONST_MASK (nr) ((u8)1 << ((nr) & 7)) static __always_inline voidarch_set_bit (long nr,volatile unsigned long *addr)
@@ -54,7 +54,7 @@ arch_set_bit(long nr, volatile unsigned long *addr)if (__builtin_constant_p (nr)) { asm volatile(LOCK_PREFIX "orb %1,%0" : CONST_MASK_ADDR(nr, addr) - : "iq" ((u8)CONST_MASK(nr)) + : "iq" (CONST_MASK(nr)) : "memory"); }else { asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
@@ -74,7 +74,7 @@ arch_clear_bit(long nr, volatile unsigned long *addr)if (__builtin_constant_p (nr)) { asm volatile(LOCK_PREFIX "andb %1,%0" : CONST_MASK_ADDR(nr, addr) - : "iq" ((u8)~CONST_MASK(nr))); + : "iq" (0xff ^ CONST_MASK(nr))); }else { asm volatile(LOCK_PREFIX __ASM_SIZE(btr) " %1,%0" : : RLONG_ADDR(addr), "Ir" (nr) : "memory");
base-commit: ca7e1fd1026c5af6a533b4b5447e1d2f153e28f2
Signature · 1 line
2.24.1
PATCH v3 2/2 lib: make a test module with set/clear bit
Test some bit clears/sets to make sure assembly doesn't change, and that the set_bit and clear_bit functions work and don't cause sparse warnings.
Instruct Kbuild to build this file with extra warning level -Wextra, to catch new issues, and also doesn't hurt to build with C=1.
This was used to test changes to arch/x86/include/asm/bitops.h.
In particular, sparse (C=1) was very concerned when the last bit before a natural boundary, like 7, or 31, was being tested, as this causes sign extension (0xffffff7f) for instance when clearing bit 7.
Recommended usage:
make defconfigscripts/config -m CONFIG_TEST_BITOPS
make modules_prepare
make C=1 W=1 lib/test_bitops.ko
objdump -S -d lib/test_bitops.ko
---
v3: update the test to fail if bits aren't cleared, and make the test reproduce the original issue without patch 1/2, showing that the issue is fixed in patch 1/2. Thanks PeterZ!
v2: correct CC: list
--- lib/Kconfig.debug | 13 +++++++ lib/Makefile | 2 ++ lib/test_bitops.c | 55 ++++++++++++++++++++++++++++++ tools/testing/selftests/lib/config | 1 + 4 files changed, 71 insertions(+) create mode 100644 lib/test_bitops.c
lib/Kconfig.debug +13 -0
@@ -1947,6 +1947,19 @@ config TEST_LKM If unsure, say N. +config TEST_BITOPS + tristate "Test module for compilation of clear_bit/set_bit operations" + depends on m + help + This builds the "test_bitops" module that is much like the + TEST_LKM module except that it does a basic exercise of the + clear_bit and set_bit macros to make sure there are no compiler + warnings from C=1 sparse checker or -Wextra compilations. It has + no dependencies and doesn't run or load unless explicitly requested + by name. for example: modprobe test_bitops. + + If unsure, say N. + config TEST_VMALLOC tristate "Test module for stress/performance analysis of vmalloc allocator" default n
lib/Makefile +2 -0
@@ -89,6 +89,8 @@ obj-$(CONFIG_TEST_OBJAGG) += test_objagg.o obj-$(CONFIG_TEST_STACKINIT) += test_stackinit.o obj-$(CONFIG_TEST_BLACKHOLE_DEV) += test_blackhole_dev.o obj-$(CONFIG_TEST_MEMINIT) += test_meminit.o +obj-$(CONFIG_TEST_BITOPS) += test_bitops.o +CFLAGS_test_bitops.o += -Werror obj-$(CONFIG_TEST_LIVEPATCH) += livepatch/
lib/test_bitops.c new file +55 -0
55 lines not on this page · raw message
tools/testing/selftests/lib/config +1 -0
@@ -2,3 +2,4 @@ CONFIG_TEST_PRINTF=m CONFIG_TEST_BITMAP=m CONFIG_PRIME_NUMBERS=m CONFIG_TEST_STRSCPY=m +CONFIG_TEST_BITOPS=m
Signature · 1 line
2.24.1
Re: [PATCH v3 2/2] lib: make a test module with set/clear bit
Quoted 13 lines · Jesse Brandeburg
Test some bit clears/sets to make sure assembly doesn't change, and that the
set_bitandclear_bitfunctions work and don't cause sparse warnings.Instruct
Kbuildto build this file with extra warning level -Wextra, to catch new issues, and also doesn't hurt to build with C=1.This was used to test changes to
arch/x86/include/asm/bitops.h.In particular, sparse (C=1) was very concerned when the last bit before a natural boundary, like 7, or 31, was being tested, as this causes sign extension (
0xffffff7f) for instance when clearing bit 7.
Quoted 6 lines
Recommended usage:
make defconfigscripts/config-mCONFIG_TEST_BITOPS
makemodules_prepare
make C=1 W=1lib/test_bitops.ko
objdump -S -dlib/test_bitops.ko
Thanks!
One comments below, after addressing:
Quoted 65 lines
Signed-off-by Jesse Brandeburg <[email protected]>---
v3: update the test to fail if bits aren't cleared, and make the test reproduce the original issue without patch 1/2, showing that the issue is fixed in patch 1/2. Thanks PeterZ!
v2: correct CC: list--- lib/Kconfig.debug | 13 +++++++ lib/Makefile | 2 ++ lib/test_bitops.c | 55 ++++++++++++++++++++++++++++++ tools/testing/selftests/lib/config | 1 + 4 files changed, 71 insertions(+) create mode 100644 lib/test_bitops.clib/Kconfig.debug +13
-0@@ -1947,6 +1947,19 @@ config TEST_LKM If unsure, say N. +config TEST_BITOPS + tristate "Test module for compilation of clear_bit/set_bit operations" + depends on m + help + This builds the "test_bitops" module that is much like the + TEST_LKM module except that it does a basic exercise of the + clear_bit and set_bit macros to make sure there are no compiler + warnings from C=1 sparse checker or -Wextra compilations. It has + no dependencies and doesn't run or load unless explicitly requested + by name. for example: modprobe test_bitops. + + If unsure, say N. + config TEST_VMALLOC tristate "Test module for stress/performance analysis of vmalloc allocator" default nlib/Makefile +2
-0@@ -89,6 +89,8 @@ obj-$(CONFIG_TEST_OBJAGG) += test_objagg.o obj-$(CONFIG_TEST_STACKINIT) += test_stackinit.o obj-$(CONFIG_TEST_BLACKHOLE_DEV) += test_blackhole_dev.o obj-$(CONFIG_TEST_MEMINIT) += test_meminit.o +obj-$(CONFIG_TEST_BITOPS) += test_bitops.o +CFLAGS_test_bitops.o += -Werror obj-$(CONFIG_TEST_LIVEPATCH) += livepatch/lib/test_bitops.c new file +7
-0@@ -0,0 +1,55 @@ +// SPDX-License-Identifier: GPL-2.0-only +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt + +#include <linux/init.h> +#include <linux/module.h> +#include <linux/printk.h> +
lib/test_bitops.c line 17
Quoted 10 lines
lib/test_bitops.c new file +10
-0+/* a tiny module only meant to compile-test set/clear_bit */ + +/* use an enum because thats the most common BITMAP usage */ +enum bitops_fun { +BITOPS_4 =4 , +BITOPS_7 =7 , +BITOPS_11 =11 , +BITOPS_31 =31 , +BITOPS_88 =88 , +BITOPS_LAST =255
BITOPS_LAST = 255, BITOPS_LENGTH = 256
and...
lib/test_bitops.c line 20
Quoted 3 lines
lib/test_bitops.c new file +3
-0+}; + +static DECLARE_BITMAP(g_bitmap, BITOPS_LAST);
static DECLARE_BITMAP(g_bitmap, BITOPS_LENGTH);
Quoted 47 lines
lib/test_bitops.c new file +35
-0+ +static int __init test_bitops_startup (void ) +{ +pr_warn ("Loaded test module\n" ); +set_bit (BITOPS_4 , g_bitmap); +set_bit (BITOPS_7 , g_bitmap); +set_bit (BITOPS_11 , g_bitmap); +set_bit (BITOPS_31 , g_bitmap); +set_bit (BITOPS_88 , g_bitmap); +return 0 ; +} + +static void __exit test_bitops_unstartup(void) +{ +int bit_set; + +clear_bit (BITOPS_4 , g_bitmap); +clear_bit (BITOPS_7 , g_bitmap); +clear_bit (BITOPS_11 , g_bitmap); +clear_bit (BITOPS_31 , g_bitmap); +clear_bit (BITOPS_88 , g_bitmap); + + bit_set =find_first_bit (g_bitmap,BITOPS_LAST ); +if (bit_set !=BITOPS_LAST ) +pr_err ("ERROR: FOUND SET BIT %d\n" , bit_set); + +pr_warn ("Unloaded test module\n" ); +} + +module_init (test_bitops_startup); +module_exit (test_bitops_unstartup); + +MODULE_AUTHOR ("Jesse Brandeburg <[email protected]>" ); +MODULE_LICENSE ("GPL" ); +MODULE_DESCRIPTION ("Bit testing module" );tools/testing/selftests/lib/config +1
-0@@ -2,3 +2,4 @@ CONFIG_TEST_PRINTF=m CONFIG_TEST_BITMAP=m CONFIG_PRIME_NUMBERS=m CONFIG_TEST_STRSCPY=m +CONFIG_TEST_BITOPS=mSignature · 1 line
2.24.1
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v3 2/2] lib: make a test module with set/clear bit
Quoted 21 lines · On Fri, 21 Feb 2020 12:25:18 +0200 Andy wrote:
Quoted 13 lines · Jesse Brandeburg
Test some bit clears/sets to make sure assembly doesn't change, and that the
set_bitandclear_bitfunctions work and don't cause sparse warnings.Instruct
Kbuildto build this file with extra warning level -Wextra, to catch new issues, and also doesn't hurt to build with C=1.This was used to test changes to
arch/x86/include/asm/bitops.h.In particular, sparse (C=1) was very concerned when the last bit before a natural boundary, like 7, or 31, was being tested, as this causes sign extension (
0xffffff7f) for instance when clearing bit 7.Quoted 6 lines
Recommended usage:
make defconfigscripts/config-mCONFIG_TEST_BITOPS
makemodules_prepare
make C=1 W=1lib/test_bitops.ko
objdump -S -dlib/test_bitops.ko
Quoted 3 lines
Thanks!
One comments below, after addressing:Reviewed-by Andy Shevchenko <[email protected]>
Thanks!
Quoted 6 lines
BITOPS_LAST = 255, BITOPS_LENGTH = 256and...
static
DECLARE_BITMAP(g_bitmap, BITOPS_LENGTH);
Fixed in v4
Quoted 16 lines · Jesse Brandeburg
Fix many sparse warnings when building with C=1.
When the kernel is compiled with C=1, there are lots of messages like:
arch/x86/include/asm/bitops.h:77:37: warning: cast truncates bits from constant value (ffffff7fbecomes 7f)
CONST_MASK()is using a signed integer "1" to create the mask which is later cast to (u8) when used. Move the cast to the definition and clean up the calling sites to prevent sparse from warning.The reason the warning was occurring is because certain bitmasks that end with a mask next to a natural boundary like 7, 15, 23, 31, end up with a mask like
0x7f, which then results in sign extension when doing an invert (but I'm not a compiler expert). It was really only "clear_bit" that was having problems, and it was only on bit checks next to a byte boundary (top bit).
Quoted 2 lines
Verified with a test module (see next patch) and assembly inspection that the patch doesn't introduce any change in generated code.
FWIW,
Thanks for fixing this, I have experienced tons of such messages.
Quoted 46 lines
Signed-off-by Jesse Brandeburg <[email protected]>---
v3: Clean up
the header file changes as per peterz.v2: use correct CC: list
--- arch/x86/include/asm/bitops.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)arch/x86/include/asm/bitops.h +3
-3@@ -46,7 +46,7 @@* a mask operation on a byte. */ #define CONST_MASK_ADDR (nr, addr) WBYTE_ADDR((void *)(addr) + ((nr)>>3)) -#define CONST_MASK (nr) (1 << ((nr) & 7)) +#define CONST_MASK (nr) ((u8)1 << ((nr) & 7)) static __always_inline voidarch_set_bit (long nr,volatile unsigned long *addr)@@ -54,7 +54,7 @@ arch_set_bit(long nr, volatile unsigned long *addr)if (__builtin_constant_p (nr)) { asm volatile(LOCK_PREFIX "orb %1,%0" : CONST_MASK_ADDR(nr, addr) - : "iq" ((u8)CONST_MASK(nr)) + : "iq" (CONST_MASK(nr)) : "memory"); }else { asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"@@ -74,7 +74,7 @@ arch_clear_bit(long nr, volatile unsigned long *addr)if (__builtin_constant_p (nr)) { asm volatile(LOCK_PREFIX "andb %1,%0" : CONST_MASK_ADDR(nr, addr) - : "iq" ((u8)~CONST_MASK(nr))); + : "iq" (0xff ^ CONST_MASK(nr))); }else { asm volatile(LOCK_PREFIX __ASM_SIZE(btr) " %1,%0" : : RLONG_ADDR(addr), "Ir" (nr) : "memory");base-commit:
ca7e1fd1026c5af6a533b4b5447e1d2f153e28f2Signature · 1 line
2.24.1
Signature · 2 lines
With Best Regards, Andy Shevchenko
From: Jesse Brandeburg
Quoted 19 lines
Fix many sparse warnings when building with C=1.
When the kernel is compiled with C=1, there are lots of messages like:
arch/x86/include/asm/bitops.h:77:37: warning: cast truncates bits from constant value (ffffff7f
becomes 7f)
CONST_MASK()is using a signed integer "1" to create the mask which is later cast to (u8) when used. Move the cast to the definition and clean up the calling sites to prevent sparse from warning.The reason the warning was occurring is because certain bitmasks that end with a mask next to a natural boundary like 7, 15, 23, 31, end up with a mask like
0x7f, which then results in sign extension when doing an invert (but I'm not a compiler expert). It was really only "clear_bit" that was having problems, and it was only on bit checks next to a byte boundary (top bit).Verified with a test module (see next patch) and assembly inspection that the patch doesn't introduce any change in generated code.
...
Quoted 4 lines
arch/x86/include/asm/bitops.h +0
-0
...
arch/x86/include/asm/bitops.h arch_clear_bit(long nr, volatile unsigned long *addr) · line 77
Quoted 6 lines
arch/x86/include/asm/bitops.h +1
-1@@ -74,7 +74,7 @@ arch_clear_bit(long nr, volatile unsigned long *addr)if (__builtin_constant_p (nr)) { asm volatile(LOCK_PREFIX "andb %1,%0" : CONST_MASK_ADDR(nr, addr) - : "iq" ((u8)~CONST_MASK(nr))); + : "iq" (0xff ^ CONST_MASK(nr)));
IMHO (CONST_MASK(nr) ^ 0xff) is better.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)