Skip to contentSkip to conversation

WorkStruct: Implement generic UP cmpxchg() where an arch doesn't support it [try #2]

PATCH In review David Howells 2 participants 3 messages Dec 6, 2006 Generic include/asm header files

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 WorkStruct: Implement generic UP cmpxchg() where an arch doesn't support it [try #2]

Implement generic UP cmpxchg() where an arch doesn't otherwise support it. This assuming that the arch doesn't have support SMP without providing its own cmpxchg() implementation.

This is required because cmpxchg() is used by the reduced work queue patches to adjust the management data in a work_struct.

Also provide ARMv6 with a cmpxchg() implementation using LDREX/STREXEQ. Pre-v6 ARM doesn't support SMP according to ARM's atomic.h, so the generic IRQ-disablement based cmpxchg() is entirely adequate there (if it isn't, then atomic_cmpxchg() is also broken on ARM).

---

 include/asm-arm/system.h      |   40 ++++++++++++++++++++++++++++++++++++
 include/asm-generic/cmpxchg.h |   46 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 86 insertions(+), 0 deletions(-)
include/asm-arm/system.h +40 -0
index f05fbe3..f16e42d 100644
@@ -325,6 +325,46 @@ #endif
 extern void disable_hlt(void);
 extern void enable_hlt(void);
 
+/*
+ * We only implement cmpxchg in ASM on ARMv6 where we have LDREX/STREX
+ * available, and we only implement it for word-sized exchanges
+ */
+#if __LINUX_ARM_ARCH__ >= 6
+extern void __bad_cmpxchg(volatile void *, int);
+
+#define cmpxchg(ptr, old, new)						\
+({									\
+	__typeof__ (ptr) ____p = (ptr);					\
+	__typeof__(*ptr) ____old = (old);				\
+	__typeof__(*ptr) ____new = (new);				\
+	__typeof__(*ptr) ____oldval;					\
+	__typeof__(*ptr) ____res;					\
+									\
+	switch (sizeof(____res)) {					\
+	case 4:								\
+		do {							\
+			__asm__ __volatile__("@ cmpxchg\n"		\
+			"ldrex	%1, [%2]\n"				\
+			"mov	%0, #0\n"				\
+			"teq	%1, %3\n"				\
+			"strexeq %0, %4, [%2]\n"			\
+			: "=&r" (____res), "=&r" (____oldval)		\
+			: "r" (____p), "Ir" (____old), "r" (____new)	\
+			: "cc");					\
+		} while(____res);					\
+		break;							\
+	default:							\
+		__bad_cmpxchg(____p, sizeof(____res));			\
+		____oldval = 0;						\
+		break;							\
+	}								\
+	____oldval;							\
+})
+
+#else
+#include <asm-generic/cmpxchg.h>
+#endif
+
 #endif /* __ASSEMBLY__ */
 
 #define arch_align_stack(x) (x)
include/asm-generic/cmpxchg.h new file +46 -0

46 lines not on this page · raw message

#2 lore raw

Have a look at arch/i386/kernel/cpu/intel.c. You can probably replace my code that simulates cmpxchg for 386s

arch/i386/kernel/cpu/intel.c:

#ifndef CONFIG_X86_CMPXCHG
unsigned long cmpxchg_386_u8(volatile void *ptr, u8 old, u8 new)

{
        u8 prev;
        unsigned long flags;

        /* Poor man's cmpxchg for 386. Unsuitable for SMP */
        local_irq_save(flags);
        prev = *(u8 *)ptr;
        if (prev == old)
                *(u8 *)ptr = new;
        local_irq_restore(flags);
        return prev;
}

EXPORT_SYMBOL(cmpxchg_386_u8);

unsigned long cmpxchg_386_u16(volatile void *ptr, u16 old, u16 new)

{
        u16 prev;
        unsigned long flags;

        /* Poor man's cmpxchg for 386. Unsuitable for SMP */
        local_irq_save(flags);
        prev = *(u16 *)ptr;
        if (prev == old)
                *(u16 *)ptr = new;
        local_irq_restore(flags);
        return prev;
}

EXPORT_SYMBOL(cmpxchg_386_u16);

unsigned long cmpxchg_386_u32(volatile void *ptr, u32 old, u32 new)

{
        u32 prev;
        unsigned long flags;

        /* Poor man's cmpxchg for 386. Unsuitable for SMP */
        local_irq_save(flags);
        prev = *(u32 *)ptr;
        if (prev == old)
                *(u32 *)ptr = new;
        local_irq_restore(flags);
        return prev;
}

EXPORT_SYMBOL(cmpxchg_386_u32);
#endif