Re: include/asm-generic/cmpxchg.h:37:39: sparse: sparse: cast truncates bits from constant value (deadbeef becomes ef)
From: Mark Rutland
Date: Fri Nov 26 2021 - 06:05:37 EST
Hi,
On Wed, Nov 24, 2021 at 06:19:48AM +0800, kernel test robot wrote:
> tree: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
> head: 136057256686de39cc3a07c2e39ef6bc43003ff6
> commit: c7178cdecdbef8321f418fac55f3afaca3bb4c96 locking/atomic: h8300: use asm-generic exclusively
> date: 6 months ago
> config: h8300-randconfig-s031-20211117 (https://download.01.org/0day-ci/archive/20211124/202111240654.4qXNBmtC-lkp@xxxxxxxxx/config.gz)
> compiler: h8300-linux-gcc (GCC) 11.2.0
> reproduce:
> wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
> chmod +x ~/bin/make.cross
> # apt-get install sparse
> # sparse version: v0.6.4-dirty
> # https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c7178cdecdbef8321f418fac55f3afaca3bb4c96
> git remote add linus https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
> git fetch --no-tags linus master
> git checkout c7178cdecdbef8321f418fac55f3afaca3bb4c96
> # save the config file to linux build tree
> COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=h8300 SHELL=/bin/bash
>
> If you fix the issue, kindly add following tag as appropriate
> Reported-by: kernel test robot <lkp@xxxxxxxxx>
>
>
> sparse warnings: (new ones prefixed by >>)
> lib/atomic64_test.c: note: in included file (through arch/h8300/include/generated/asm/cmpxchg.h, include/asm-generic/atomic.h, arch/h8300/include/generated/asm/atomic.h, ...):
> >> include/asm-generic/cmpxchg.h:37:39: sparse: sparse: cast truncates bits from constant value (deadbeef becomes ef)
> >> include/asm-generic/cmpxchg.h:48:40: sparse: sparse: cast truncates bits from constant value (deadbeef becomes beef)
> >> include/asm-generic/cmpxchg.h:37:39: sparse: sparse: cast truncates bits from constant value (deadbeef becomes ef)
> >> include/asm-generic/cmpxchg.h:48:40: sparse: sparse: cast truncates bits from constant value (deadbeef becomes beef)
> >> include/asm-generic/cmpxchg.h:37:39: sparse: sparse: cast truncates bits from constant value (deadbeef becomes ef)
> >> include/asm-generic/cmpxchg.h:48:40: sparse: sparse: cast truncates bits from constant value (deadbeef becomes beef)
> >> include/asm-generic/cmpxchg.h:37:39: sparse: sparse: cast truncates bits from constant value (deadbeef becomes ef)
> >> include/asm-generic/cmpxchg.h:48:40: sparse: sparse: cast truncates bits from constant value (deadbeef becomes beef)
This is the expected behaviour for the code below. The truncation on line 37 is
when we assign to a u8, and the truncation on line 48 is when we assign to a
u16.
I'm a little confused by the wording of the warning, because there's no cast on
the value being assigned. I'm not sure if that's just misleading wording from
sparse. Would a cast on the right hand side of the assignment be sufficient to
suppress this, e.g.
*(volatile u8 *)pte = (u8)x;
... because if so, maybe it's worth adding, unless we think this warning would
be useful for when this situation crops up outside of test code (in which case
we should just ignore it for now).
Thanks,
Mark.
> vim +37 include/asm-generic/cmpxchg.h
>
> b4816afa398670 David Howells 2012-03-28 24
> b4816afa398670 David Howells 2012-03-28 25 static inline
> b4816afa398670 David Howells 2012-03-28 26 unsigned long __xchg(unsigned long x, volatile void *ptr, int size)
> b4816afa398670 David Howells 2012-03-28 27 {
> b4816afa398670 David Howells 2012-03-28 28 unsigned long ret, flags;
> b4816afa398670 David Howells 2012-03-28 29
> b4816afa398670 David Howells 2012-03-28 30 switch (size) {
> b4816afa398670 David Howells 2012-03-28 31 case 1:
> b4816afa398670 David Howells 2012-03-28 32 #ifdef __xchg_u8
> b4816afa398670 David Howells 2012-03-28 33 return __xchg_u8(x, ptr);
> b4816afa398670 David Howells 2012-03-28 34 #else
> b4816afa398670 David Howells 2012-03-28 35 local_irq_save(flags);
> b4816afa398670 David Howells 2012-03-28 36 ret = *(volatile u8 *)ptr;
> b4816afa398670 David Howells 2012-03-28 @37 *(volatile u8 *)ptr = x;
> b4816afa398670 David Howells 2012-03-28 38 local_irq_restore(flags);
> b4816afa398670 David Howells 2012-03-28 39 return ret;
> b4816afa398670 David Howells 2012-03-28 40 #endif /* __xchg_u8 */
> b4816afa398670 David Howells 2012-03-28 41
> b4816afa398670 David Howells 2012-03-28 42 case 2:
> b4816afa398670 David Howells 2012-03-28 43 #ifdef __xchg_u16
> b4816afa398670 David Howells 2012-03-28 44 return __xchg_u16(x, ptr);
> b4816afa398670 David Howells 2012-03-28 45 #else
> b4816afa398670 David Howells 2012-03-28 46 local_irq_save(flags);
> b4816afa398670 David Howells 2012-03-28 47 ret = *(volatile u16 *)ptr;
> b4816afa398670 David Howells 2012-03-28 @48 *(volatile u16 *)ptr = x;
> b4816afa398670 David Howells 2012-03-28 49 local_irq_restore(flags);
> b4816afa398670 David Howells 2012-03-28 50 return ret;
> b4816afa398670 David Howells 2012-03-28 51 #endif /* __xchg_u16 */
> b4816afa398670 David Howells 2012-03-28 52
> b4816afa398670 David Howells 2012-03-28 53 case 4:
> b4816afa398670 David Howells 2012-03-28 54 #ifdef __xchg_u32
> b4816afa398670 David Howells 2012-03-28 55 return __xchg_u32(x, ptr);
> b4816afa398670 David Howells 2012-03-28 56 #else
> b4816afa398670 David Howells 2012-03-28 57 local_irq_save(flags);
> b4816afa398670 David Howells 2012-03-28 58 ret = *(volatile u32 *)ptr;
> b4816afa398670 David Howells 2012-03-28 59 *(volatile u32 *)ptr = x;
> b4816afa398670 David Howells 2012-03-28 60 local_irq_restore(flags);
> b4816afa398670 David Howells 2012-03-28 61 return ret;
> b4816afa398670 David Howells 2012-03-28 62 #endif /* __xchg_u32 */
> b4816afa398670 David Howells 2012-03-28 63
>
> :::::: The code at line 37 was first introduced by commit
> :::::: b4816afa3986704d1404fc48e931da5135820472 Move the asm-generic/system.h xchg() implementation to asm-generic/cmpxchg.h
>
> :::::: TO: David Howells <dhowells@xxxxxxxxxx>
> :::::: CC: David Howells <dhowells@xxxxxxxxxx>
>
> ---
> 0-DAY CI Kernel Test Service, Intel Corporation
> https://lists.01.org/hyperkitty/list/kbuild-all@xxxxxxxxxxxx