Skip to content

Commit 6246541

Browse files
mevrvarndb
authored andcommitted
locking/atomic: cmpxchg: Make __generic_cmpxchg_local compare against zero-extended 'old' value
__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>
1 parent 88603b6 commit 6246541

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

include/asm-generic/cmpxchg-local.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ static inline unsigned long __generic_cmpxchg_local(volatile void *ptr,
2626
raw_local_irq_save(flags);
2727
switch (size) {
2828
case 1: prev = *(u8 *)ptr;
29-
if (prev == old)
29+
if (prev == (u8)old)
3030
*(u8 *)ptr = (u8)new;
3131
break;
3232
case 2: prev = *(u16 *)ptr;
33-
if (prev == old)
33+
if (prev == (u16)old)
3434
*(u16 *)ptr = (u16)new;
3535
break;
3636
case 4: prev = *(u32 *)ptr;
37-
if (prev == old)
37+
if (prev == (u32)old)
3838
*(u32 *)ptr = (u32)new;
3939
break;
4040
case 8: prev = *(u64 *)ptr;

0 commit comments

Comments
 (0)