mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Nicolas Pitre <nico@cam.org>
To: Ingo Molnar <mingo@elte.hu>
Cc: lkml <linux-kernel@vger.kernel.org>,
	Arjan van de Ven <arjanv@infradead.org>,
	Russell King <rmk+lkml@arm.linux.org.uk>
Subject: Re: [patch 0/8] mutex subsystem, -V6
Date: Fri, 23 Dec 2005 00:04:49 -0500 (EST)	[thread overview]
Message-ID: <Pine.LNX.4.64.0512222359360.26663@localhost.localdomain> (raw)
In-Reply-To: <Pine.LNX.4.64.0512221846470.26663@localhost.localdomain>

On Thu, 22 Dec 2005, Nicolas Pitre wrote:

> > Nico, Christoph, does this approach work for you? Nico, you might want 
> > to try an ARM-specific mutex.h implementation.
> 
> Yes, I'm happy.  And the ARM version will be sent your way soon.

Here it is:

Index: linux-2.6/include/asm-arm/mutex.h
===================================================================
--- linux-2.6.orig/include/asm-arm/mutex.h
+++ linux-2.6/include/asm-arm/mutex.h
@@ -1,8 +1,83 @@
 /*
- * Pull in the generic wrappers for __mutex_fastpath_lock() and
- * __mutex_fastpath_unlock().
+ * include/asm-arm/mutex.h
  *
- * TODO: implement optimized primitives instead
+ * ARM optimized mutex locking primitives
+ *
+ * Please look into asm-generic/mutex-xchg.h for a formal definition.
+ */
+
+#if __LINUX_ARM_ARCH__ >= 6
+
+/*
+ * Attempting to lock a mutex on ARMv6+ can be done with a bastardized
+ * atomic decrement (it is not a reliable atomic decrement but it satisfies
+ * the defined semantics for our purpose, while being smaller and faster
+ * than a real atomic decrement or atomic swap.  The idea is to attempt
+ * decrementing the lock value only once.  If once decremented it isn't zero,
+ * or if its store-back fails due to a dispute on the exclusive store, we
+ * simply bail out immediately through the slow path where the lock will be
+ * reattempted until it succeeds.
+ */
+#define __mutex_fastpath_lock(v, fail)					\
+do {									\
+	int __ex_flag, __res;						\
+	__asm__ (							\
+	"ldrex	%0, [%2]\n\t"						\
+	"sub	%0, %0, #1\n\t"						\
+	"strex	%1, %0, [%2]"						\
+	: "=&r" (__res), "=&r" (__ex_flag)				\
+	: "r" (&(v)->counter)						\
+	: "cc","memory" );						\
+	__res |= __ex_flag;						\
+	if (unlikely(__res != 0))					\
+		fail(v);						\
+} while (0)
+
+#define __mutex_fastpath_lock_retval(v, fail)				\
+({									\
+	int __ex_flag, __res;						\
+	__asm__ (							\
+	"ldrex	%0, [%2]\n\t"						\
+	"sub	%0, %0, #1\n\t"						\
+	"strex	%1, %0, [%2]"						\
+	: "=&r" (__res), "=&r" (__ex_flag)				\
+	: "r" (&(v)->counter)						\
+	: "cc","memory" );						\
+	__res |= __ex_flag;						\
+	if (unlikely(__res != 0))					\
+		__res = fail(v);					\
+	__res;								\
+})
+
+/*
+ * Same trick is used for the unlock fast path. However the original value,
+ * rather than the result, is used to test for success in order to have
+ * better generated assembly.
  */
+#define __mutex_fastpath_unlock(v, fail)				\
+do {									\
+	int __ex_flag, __res, __orig;					\
+	__asm__ (							\
+	"ldrex	%0, [%3]\n\t"						\
+	"add	%1, %0, #1\n\t"						\
+	"strex	%2, %1, [%3]"						\
+	: "=&r" (__orig), "=&r" (__res), "=&r" (__ex_flag)		\
+	: "r" (&(v)->counter)						\
+	: "cc","memory" );						\
+	__orig |= __ex_flag;						\
+	if (unlikely(__orig != 0))					\
+		fail(v);						\
+} while (0)
 
+/*
+ * If the unlock was done on a contended lock, or if the unlock simply fails
+ * then the mutex remains locked.
+ */
+#define __mutex_slowpath_needs_to_unlock()	(1)
+
+#else
+
+/* On pre-ARMv6 hardware the swp based implementation is the most efficient. */
 #include <asm-generic/mutex-xchg.h>
+
+#endif

  reply	other threads:[~2005-12-23  5:04 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-22 23:04 Ingo Molnar
2005-12-22 23:55 ` Nicolas Pitre
2005-12-23  5:04   ` Nicolas Pitre [this message]
2005-12-23  8:09     ` Ingo Molnar
2005-12-23  4:59 ` Nicolas Pitre
2005-12-23  8:04   ` Ingo Molnar
2005-12-23  5:12 ` Nicolas Pitre

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.64.0512222359360.26663@localhost.localdomain \
    --to=nico@cam.org \
    --cc=arjanv@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=rmk+lkml@arm.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®