mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2025-01-06 05:06:29 +00:00
656e9007ef
Code that passes a 32-bit constant into cmpxchg() produces a harmless
sparse warning because of the truncation in the branch that is not taken:
fs/erofs/zdata.c: note: in included file (through /home/arnd/arm-soc/arch/arm/include/asm/cmpxchg.h, /home/arnd/arm-soc/arch/arm/include/asm/atomic.h, /home/arnd/arm-soc/include/linux/atomic.h, ...):
include/asm-generic/cmpxchg-local.h:29:33: warning: cast truncates bits from constant value (5f0ecafe becomes fe)
include/asm-generic/cmpxchg-local.h:33:34: warning: cast truncates bits from constant value (5f0ecafe becomes cafe)
include/asm-generic/cmpxchg-local.h:29:33: warning: cast truncates bits from constant value (5f0ecafe becomes fe)
include/asm-generic/cmpxchg-local.h:30:42: warning: cast truncates bits from constant value (5f0edead becomes ad)
include/asm-generic/cmpxchg-local.h:33:34: warning: cast truncates bits from constant value (5f0ecafe becomes cafe)
include/asm-generic/cmpxchg-local.h:34:44: warning: cast truncates bits from constant value (5f0edead becomes dead)
This was reported as a regression to Matt's recent __generic_cmpxchg_local
patch, though this patch only added more warnings on top of the ones
that were already there.
Rewording the truncation to use an explicit bitmask instead of a cast
to a smaller type avoids the warning but otherwise leaves the code
unchanged.
I had another look at why the cast is even needed for atomic_cmpxchg(),
and as Matt describes the problem here is that atomic_t contains a
signed 'int', but cmpxchg() takes an 'unsigned long' argument, and
converting between the two leads to a 64-bit sign-extension of
negative 32-bit atomics.
I checked the other implementations of arch_cmpxchg() and did not find
any others that run into the same problem as __generic_cmpxchg_local(),
but it's easy to be on the safe side here and always convert the
signed int into an unsigned int when calling arch_cmpxchg(), as this
will work even when any of the arch_cmpxchg() implementations run
into the same problem.
Fixes: 6246541522
("locking/atomic: cmpxchg: Make __generic_cmpxchg_local compare against zero-extended 'old' value")
Reviewed-by: Matt Evans <mev@rivosinc.com>
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
137 lines
3.5 KiB
C
137 lines
3.5 KiB
C
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
/*
|
|
* Generic C implementation of atomic counter operations. Do not include in
|
|
* machine independent code.
|
|
*
|
|
* Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
*/
|
|
#ifndef __ASM_GENERIC_ATOMIC_H
|
|
#define __ASM_GENERIC_ATOMIC_H
|
|
|
|
#include <asm/cmpxchg.h>
|
|
#include <asm/barrier.h>
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
/* we can build all atomic primitives from cmpxchg */
|
|
|
|
#define ATOMIC_OP(op, c_op) \
|
|
static inline void generic_atomic_##op(int i, atomic_t *v) \
|
|
{ \
|
|
int c, old; \
|
|
\
|
|
c = v->counter; \
|
|
while ((old = arch_cmpxchg(&v->counter, c, c c_op i)) != c) \
|
|
c = old; \
|
|
}
|
|
|
|
#define ATOMIC_OP_RETURN(op, c_op) \
|
|
static inline int generic_atomic_##op##_return(int i, atomic_t *v) \
|
|
{ \
|
|
int c, old; \
|
|
\
|
|
c = v->counter; \
|
|
while ((old = arch_cmpxchg(&v->counter, c, c c_op i)) != c) \
|
|
c = old; \
|
|
\
|
|
return c c_op i; \
|
|
}
|
|
|
|
#define ATOMIC_FETCH_OP(op, c_op) \
|
|
static inline int generic_atomic_fetch_##op(int i, atomic_t *v) \
|
|
{ \
|
|
int c, old; \
|
|
\
|
|
c = v->counter; \
|
|
while ((old = arch_cmpxchg(&v->counter, c, c c_op i)) != c) \
|
|
c = old; \
|
|
\
|
|
return c; \
|
|
}
|
|
|
|
#else
|
|
|
|
#include <linux/irqflags.h>
|
|
|
|
#define ATOMIC_OP(op, c_op) \
|
|
static inline void generic_atomic_##op(int i, atomic_t *v) \
|
|
{ \
|
|
unsigned long flags; \
|
|
\
|
|
raw_local_irq_save(flags); \
|
|
v->counter = v->counter c_op i; \
|
|
raw_local_irq_restore(flags); \
|
|
}
|
|
|
|
#define ATOMIC_OP_RETURN(op, c_op) \
|
|
static inline int generic_atomic_##op##_return(int i, atomic_t *v) \
|
|
{ \
|
|
unsigned long flags; \
|
|
int ret; \
|
|
\
|
|
raw_local_irq_save(flags); \
|
|
ret = (v->counter = v->counter c_op i); \
|
|
raw_local_irq_restore(flags); \
|
|
\
|
|
return ret; \
|
|
}
|
|
|
|
#define ATOMIC_FETCH_OP(op, c_op) \
|
|
static inline int generic_atomic_fetch_##op(int i, atomic_t *v) \
|
|
{ \
|
|
unsigned long flags; \
|
|
int ret; \
|
|
\
|
|
raw_local_irq_save(flags); \
|
|
ret = v->counter; \
|
|
v->counter = v->counter c_op i; \
|
|
raw_local_irq_restore(flags); \
|
|
\
|
|
return ret; \
|
|
}
|
|
|
|
#endif /* CONFIG_SMP */
|
|
|
|
ATOMIC_OP_RETURN(add, +)
|
|
ATOMIC_OP_RETURN(sub, -)
|
|
|
|
ATOMIC_FETCH_OP(add, +)
|
|
ATOMIC_FETCH_OP(sub, -)
|
|
ATOMIC_FETCH_OP(and, &)
|
|
ATOMIC_FETCH_OP(or, |)
|
|
ATOMIC_FETCH_OP(xor, ^)
|
|
|
|
ATOMIC_OP(add, +)
|
|
ATOMIC_OP(sub, -)
|
|
ATOMIC_OP(and, &)
|
|
ATOMIC_OP(or, |)
|
|
ATOMIC_OP(xor, ^)
|
|
|
|
#undef ATOMIC_FETCH_OP
|
|
#undef ATOMIC_OP_RETURN
|
|
#undef ATOMIC_OP
|
|
|
|
#define arch_atomic_add_return generic_atomic_add_return
|
|
#define arch_atomic_sub_return generic_atomic_sub_return
|
|
|
|
#define arch_atomic_fetch_add generic_atomic_fetch_add
|
|
#define arch_atomic_fetch_sub generic_atomic_fetch_sub
|
|
#define arch_atomic_fetch_and generic_atomic_fetch_and
|
|
#define arch_atomic_fetch_or generic_atomic_fetch_or
|
|
#define arch_atomic_fetch_xor generic_atomic_fetch_xor
|
|
|
|
#define arch_atomic_add generic_atomic_add
|
|
#define arch_atomic_sub generic_atomic_sub
|
|
#define arch_atomic_and generic_atomic_and
|
|
#define arch_atomic_or generic_atomic_or
|
|
#define arch_atomic_xor generic_atomic_xor
|
|
|
|
#define arch_atomic_read(v) READ_ONCE((v)->counter)
|
|
#define arch_atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
|
|
|
|
#define arch_atomic_xchg(ptr, v) (arch_xchg(&(ptr)->counter, (u32)(v)))
|
|
#define arch_atomic_cmpxchg(v, old, new) (arch_cmpxchg(&((v)->counter), (u32)(old), (u32)(new)))
|
|
|
|
#endif /* __ASM_GENERIC_ATOMIC_H */
|