Skip to contentSkip to conversation

atomic_dec_and_lock() for alpha

PATCH In review Ivan Kokshaysky 2 participants 5 messages Oct 8, 2001 – Oct 12, 2001 Alpha port

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 atomic_dec_and_lock() for alpha

Similar to x86 version, but somewhat simpler due to ll/sc.

Ivan.

arch/alpha/lib/dec_and_lock.c +35 -0
@@ -0,0 +1,35 @@
+/*
+ * arch/alpha/lib/dec_and_lock.c
+ *
+ * ll/sc version of atomic_dec_and_lock()
+ */
+
+#include <linux/spinlock.h>
+#include <asm/atomic.h>
+
+int atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock)
+{
+	int cnt;
+	__asm__ __volatile__(
+	"1:	ldl_l	%0,%1\n"
+	"	subl	%0,1,%0\n"
+	"	beq	%0,2f\n"
+	"	stl_c	%0,%1\n"
+	"	beq	%0,3f\n"
+	"	mb\n"
+	"	mov	$31,$0\n"
+	"	ret\n"
+	"2:\n"
+	".subsection 2\n"
+	"3:	br	1b\n"
+	".previous"
+	:"=&r" (cnt), "=m" (atomic->counter)
+	:"m" (atomic->counter) : "$0", "memory");
+
+	/* Slow path */
+	spin_lock(lock);
+	if (atomic_dec_and_test(atomic))
+		return 1;
+	spin_unlock(lock);
+	return 0;
+}
arch/alpha/lib/Makefile +4 -0
@@ -49,6 +49,10 @@ OBJS =	__divqu.o __remqu.o __divlu.o __r
 	fpreg.o \
 	callback_srm.o srm_puts.o srm_printk.o
 
+ifeq ($(CONFIG_SMP),y)
+  OBJS += dec_and_lock.o
+endif
+
 lib.a: $(OBJS)
 	$(AR) rcs lib.a $(OBJS)
 
arch/alpha/kernel/alpha_ksyms.c +1 -0
@@ -215,6 +215,7 @@ EXPORT_SYMBOL(__global_cli);
 EXPORT_SYMBOL(__global_sti);
 EXPORT_SYMBOL(__global_save_flags);
 EXPORT_SYMBOL(__global_restore_flags);
+EXPORT_SYMBOL(atomic_dec_and_lock);
 #if DEBUG_SPINLOCK
 EXPORT_SYMBOL(spin_unlock);
 EXPORT_SYMBOL(debug_spin_lock);
arch/alpha/config.in +4 -0
@@ -217,6 +217,10 @@ then
 	bool 'Symmetric multi-processing support' CONFIG_SMP
 fi
 
+if [ "$CONFIG_SMP" = "y" ]; then
+   define_bool CONFIG_HAVE_DEC_LOCK y
+fi
+
 if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
    bool 'Discontiguous Memory Support' CONFIG_DISCONTIGMEM
    if [ "$CONFIG_DISCONTIGMEM" = "y" ]; then
#1 lore raw

arch/alpha/lib/dec_and_lock.c line 21

Quoted 1 line · Ivan Kokshaysky
arch/alpha/lib/dec_and_lock.c +1 -0
+	"	ret\n"

I am extremely uncomfortable with you returning out of the middle of an asm statement. What if the compiler decides to allocate a stack frame for some reason? You'll return without deallocating it.

Please write the whole thing in assembly or avoid the early return.

r~

#2 lore raw
In reply to Ivan Kokshaysky
Quoted 3 lines · Richard Henderson

I am extremely uncomfortable with you returning out of the middle of an asm statement. What if the compiler decides to allocate a stack frame for some reason? You'll return without deallocating it.

Indeed.

Quoted 1 line

Please write the whole thing in assembly or avoid the early return.

OK. I prefer the latter - rewriting in assembly won't allow DEBUG_SPINLOCK stuff in this function. OTOH, moving the return outside an asm statement adds only one instruction - conditional branch that falls through in the fast path.

Ivan.

arch/alpha/lib/dec_and_lock.c +37 -0
@@ -0,0 +1,37 @@
+/*
+ * arch/alpha/lib/dec_and_lock.c
+ *
+ * ll/sc version of atomic_dec_and_lock()
+ */
+
+#include <linux/compiler.h>
+#include <linux/spinlock.h>
+#include <asm/atomic.h>
+
+int atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock)
+{
+	long cnt;
+	__asm__ __volatile__(
+	"1:	ldl_l	%0,%1\n"
+	"	subl	%0,1,%0\n"
+	"	beq	%0,2f\n"
+	"	stl_c	%0,%1\n"
+	"	beq	%0,3f\n"
+	"	mb\n"
+	"2:\n"
+	".subsection 2\n"
+	"3:	br	1b\n"
+	".previous"
+	:"=&r" (cnt), "=m" (atomic->counter)
+	:"m" (atomic->counter) : "memory");
+
+	if (likely(cnt))
+		return 0;
+
+	/* Slow path */
+	spin_lock(lock);
+	if (atomic_dec_and_test(atomic))
+		return 1;
+	spin_unlock(lock);
+	return 0;
+}
arch/alpha/lib/Makefile +4 -0
@@ -49,6 +49,10 @@ OBJS =	__divqu.o __remqu.o __divlu.o __r
 	fpreg.o \
 	callback_srm.o srm_puts.o srm_printk.o
 
+ifeq ($(CONFIG_SMP),y)
+  OBJS += dec_and_lock.o
+endif
+
 lib.a: $(OBJS)
 	$(AR) rcs lib.a $(OBJS)
 
arch/alpha/kernel/alpha_ksyms.c +1 -0
@@ -215,6 +215,7 @@ EXPORT_SYMBOL(__global_cli);
 EXPORT_SYMBOL(__global_sti);
 EXPORT_SYMBOL(__global_save_flags);
 EXPORT_SYMBOL(__global_restore_flags);
+EXPORT_SYMBOL(atomic_dec_and_lock);
 #if DEBUG_SPINLOCK
 EXPORT_SYMBOL(spin_unlock);
 EXPORT_SYMBOL(debug_spin_lock);
arch/alpha/config.in +4 -0
@@ -217,6 +217,10 @@ then
 	bool 'Symmetric multi-processing support' CONFIG_SMP
 fi
 
+if [ "$CONFIG_SMP" = "y" ]; then
+   define_bool CONFIG_HAVE_DEC_LOCK y
+fi
+
 if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
    bool 'Discontiguous Memory Support' CONFIG_DISCONTIGMEM
    if [ "$CONFIG_DISCONTIGMEM" = "y" ]; then
#3 lore raw
In reply to Ivan Kokshaysky
Quoted 4 lines · Ivan Kokshaysky

OK. I prefer the latter - rewriting in assembly won't allow DEBUG_SPINLOCK stuff in this function. OTOH, moving the return outside an asm statement adds only one instruction - conditional branch that falls through in the fast path.

Hmm. What about a mixture:

  asm (".text					\n\
	.global atomic_dec_and_lock		\n\
	.ent atomic_dec_and_lock		\n\
atomic_dec_and_lock:				\n\
	.prologue 0				\n\
1:	ldl_l	$1, 0($16)			\n\
	subl	$1, 1, $1			\n\
	beq	2f				\n\
	stl_c	$1, 0($16)			\n\
	beq	$1, 3f				\n\
	mb					\n\
	ret					\n\
	.align	4				\n\

3: br 1b \n\
2: lda $27, atomic_dec_and_lock_1");

	/* FALLTHRU */

static int
atomic_dec_and_lock_1(atomic_t *atomic, spinlock_t *lock)

{
	/* Slow path */
	spin_lock(lock);
	if (atomic_dec_and_test(atomic))
		return 1;
	spin_unlock(lock);
	return 0;
}

r~

#4 lore raw
In reply to Ivan Kokshaysky
Quoted 1 line · Richard Henderson

Hmm. What about a mixture:

...

Quoted 6 lines

2: lda $27, atomic_dec_and_lock_1");

	/* FALLTHRU */

static int
atomic_dec_and_lock_1(atomic_t *atomic, spinlock_t *lock)

Oh cool.
I've made a patch of the above (with some minor changes), but are you certain that these two will be linked in proper order under any circumstances? Or "br atomic_dec_and_lock_1" is needed?

Ivan.

arch/alpha/lib/dec_and_lock.c +41 -0
@@ -0,0 +1,41 @@
+/*
+ * arch/alpha/lib/dec_and_lock.c
+ *
+ * ll/sc version of atomic_dec_and_lock() and nice example
+ * of mixing C and assembly.
+ * 
+ */
+
+#include <linux/spinlock.h>
+#include <asm/atomic.h>
+
+  asm (".text					\n\
+	.global atomic_dec_and_lock		\n\
+	.ent atomic_dec_and_lock		\n\
+	.align	4				\n\
+atomic_dec_and_lock:				\n\
+	.prologue 0				\n\
+1:	ldl_l	$1, 0($16)			\n\
+	subl	$1, 1, $1			\n\
+	beq	$1, 2f				\n\
+	stl_c	$1, 0($16)			\n\
+	beq	$1, 3f				\n\
+	mb					\n\
+	clr	$0				\n\
+	ret					\n\
+3:	br	1b				\n\
+2:	lda	$27, atomic_dec_and_lock_1	\n\
+	.end atomic_dec_and_lock");
+
+	/* FALLTHRU */
+
+static int __attribute__((unused))
+atomic_dec_and_lock_1(atomic_t *atomic, spinlock_t *lock)
+{
+	/* Slow path */
+	spin_lock(lock);
+	if (atomic_dec_and_test(atomic))
+		return 1;
+	spin_unlock(lock);
+	return 0;
+}
arch/alpha/lib/Makefile +4 -0
@@ -49,6 +49,10 @@ OBJS =	__divqu.o __remqu.o __divlu.o __r
 	fpreg.o \
 	callback_srm.o srm_puts.o srm_printk.o
 
+ifeq ($(CONFIG_SMP),y)
+  OBJS += dec_and_lock.o
+endif
+
 lib.a: $(OBJS)
 	$(AR) rcs lib.a $(OBJS)
 
arch/alpha/kernel/alpha_ksyms.c +1 -0
@@ -215,6 +215,7 @@ EXPORT_SYMBOL(__global_cli);
 EXPORT_SYMBOL(__global_sti);
 EXPORT_SYMBOL(__global_save_flags);
 EXPORT_SYMBOL(__global_restore_flags);
+EXPORT_SYMBOL(atomic_dec_and_lock);
 #if DEBUG_SPINLOCK
 EXPORT_SYMBOL(spin_unlock);
 EXPORT_SYMBOL(debug_spin_lock);
arch/alpha/config.in +4 -0
@@ -217,6 +217,10 @@ then
 	bool 'Symmetric multi-processing support' CONFIG_SMP
 fi
 
+if [ "$CONFIG_SMP" = "y" ]; then
+   define_bool CONFIG_HAVE_DEC_LOCK y
+fi
+
 if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
    bool 'Discontiguous Memory Support' CONFIG_DISCONTIGMEM
    if [ "$CONFIG_DISCONTIGMEM" = "y" ]; then