mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-08-26 02:39:48 +00:00
![Matt Evans](/assets/img/avatar_default.png)
__generic_cmpxchg_local takes unsigned long old/new arguments which might end up being up-cast from smaller signed types (which will sign-extend). The loaded compare value must be compared against a truncated smaller type, so down-cast appropriately for each size. The issue is apparent on 64-bit machines with code, such as atomic_dec_unless_positive(), that sign-extends from int. 64-bit machines generally don't use the generic cmpxchg but development/early ports might make use of it, so make it correct. Signed-off-by: Matt Evans <mev@rivosinc.com> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
68 lines
1.4 KiB
C
68 lines
1.4 KiB
C
/* SPDX-License-Identifier: GPL-2.0 */
|
|
#ifndef __ASM_GENERIC_CMPXCHG_LOCAL_H
|
|
#define __ASM_GENERIC_CMPXCHG_LOCAL_H
|
|
|
|
#include <linux/types.h>
|
|
#include <linux/irqflags.h>
|
|
|
|
extern unsigned long wrong_size_cmpxchg(volatile void *ptr)
|
|
__noreturn;
|
|
|
|
/*
|
|
* Generic version of __cmpxchg_local (disables interrupts). Takes an unsigned
|
|
* long parameter, supporting various types of architectures.
|
|
*/
|
|
static inline unsigned long __generic_cmpxchg_local(volatile void *ptr,
|
|
unsigned long old, unsigned long new, int size)
|
|
{
|
|
unsigned long flags, prev;
|
|
|
|
/*
|
|
* Sanity checking, compile-time.
|
|
*/
|
|
if (size == 8 && sizeof(unsigned long) != 8)
|
|
wrong_size_cmpxchg(ptr);
|
|
|
|
raw_local_irq_save(flags);
|
|
switch (size) {
|
|
case 1: prev = *(u8 *)ptr;
|
|
if (prev == (u8)old)
|
|
*(u8 *)ptr = (u8)new;
|
|
break;
|
|
case 2: prev = *(u16 *)ptr;
|
|
if (prev == (u16)old)
|
|
*(u16 *)ptr = (u16)new;
|
|
break;
|
|
case 4: prev = *(u32 *)ptr;
|
|
if (prev == (u32)old)
|
|
*(u32 *)ptr = (u32)new;
|
|
break;
|
|
case 8: prev = *(u64 *)ptr;
|
|
if (prev == old)
|
|
*(u64 *)ptr = (u64)new;
|
|
break;
|
|
default:
|
|
wrong_size_cmpxchg(ptr);
|
|
}
|
|
raw_local_irq_restore(flags);
|
|
return prev;
|
|
}
|
|
|
|
/*
|
|
* Generic version of __cmpxchg64_local. Takes an u64 parameter.
|
|
*/
|
|
static inline u64 __generic_cmpxchg64_local(volatile void *ptr,
|
|
u64 old, u64 new)
|
|
{
|
|
u64 prev;
|
|
unsigned long flags;
|
|
|
|
raw_local_irq_save(flags);
|
|
prev = *(u64 *)ptr;
|
|
if (prev == old)
|
|
*(u64 *)ptr = new;
|
|
raw_local_irq_restore(flags);
|
|
return prev;
|
|
}
|
|
|
|
#endif
|