mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Roland McGrath <roland@redhat.com>
To: Linus Torvalds <torvalds@osdl.org>, Andrew Morton <akpm@osdl.org>
Cc: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: [PATCH] read_trylock for i386
Date: Tue, 12 Aug 2003 23:14:17 -0700	[thread overview]
Message-ID: <200308130614.h7D6EHv07972@magilla.sf.frob.com> (raw)

Linus had mentioned that he wished there were a read_trylock that could be
used when taking locks out of order to avoid the usually unnecessary full
treatment with its interrupt-blocked overhead when you unlock and relock.
This came up when adding a lock hierarchy dance to part of do_sigaction,
and the use of it is demonstrated by the patch below (forgive the fuzz).

The following header patches add the read_trylock macro, implementing it
fully for i386 and leaving other architectures with an always-fail fallback.
Other architectures include/asm-foo/spinlock.h files should define
_raw_read_trylock appropriately and define HAVE_ARCH_RAW_READ_TRYLOCK.  But
until they do, machine-independent code can use read_trylock for optimization
purposes (i.e. where it falls back to using read_lock) and architectures that
haven't implemented it will just not be getting the optimization.  

The machine-dependent function is simple enough to add to each architecture.
Even if I didn't know the x86 as well as I do, writing the new function by
looking at the existing assembly code in _raw_read_lock is easy enough
without really grokking the architecture (just tedious, so I'm not doing it
myself).


Enjoy,
Roland


Index: linux-2.5/kernel/signal.c
===================================================================
RCS file: /home/cvs/linux-2.5/kernel/signal.c,v
retrieving revision 1.98
diff -p -u -r1.98 signal.c
--- linux-2.5/kernel/signal.c	7 Aug 2003 20:06:12 -0000	1.98
+++ linux-2.5/kernel/signal.c	12 Aug 2003 21:38:15 -0000
@@ -2255,9 +2267,11 @@ do_sigaction(int sig, const struct k_sig
 			 * dance to maintain the lock hierarchy.
 			 */
 			struct task_struct *t = current;
-			spin_unlock_irq(&t->sighand->siglock);
-			read_lock(&tasklist_lock);
-			spin_lock_irq(&t->sighand->siglock);
+			if (!read_trylock(&tasklist_lock)) {
+				spin_unlock_irq(&t->sighand->siglock);
+				read_lock(&tasklist_lock);
+				spin_lock_irq(&t->sighand->siglock);
+			}
 			*k = *act;
 			sigdelsetmask(&k->sa.sa_mask,
 				      sigmask(SIGKILL) | sigmask(SIGSTOP));


Index: linux-2.5/include/linux/spinlock.h
===================================================================
RCS file: /home/cvs/linux-2.5/include/linux/spinlock.h,v
retrieving revision 1.27
diff -b -p -u -r1.27 spinlock.h
--- linux-2.5/include/linux/spinlock.h	26 Jun 2003 01:19:21 -0000	1.27
+++ linux-2.5/include/linux/spinlock.h	12 Aug 2003 21:58:24 -0000
@@ -38,6 +38,10 @@
 #ifdef CONFIG_SMP
 #include <asm/spinlock.h>
 
+# ifndef HAVE_ARCH_RAW_READ_TRYLOCK
+#  define _raw_read_trylock(lock) ({ (void)(lock); (0); })
+# endif
+
 #else
 
 #if !defined(CONFIG_PREEMPT) && !defined(CONFIG_DEBUG_SPINLOCK)
@@ -178,6 +182,7 @@ typedef struct {
 #define rwlock_init(lock)	do { (void)(lock); } while(0)
 #define _raw_read_lock(lock)	do { (void)(lock); } while(0)
 #define _raw_read_unlock(lock)	do { (void)(lock); } while(0)
+#define _raw_read_trylock(lock)	({ (void)(lock); (1); })
 #define _raw_write_lock(lock)	do { (void)(lock); } while(0)
 #define _raw_write_unlock(lock)	do { (void)(lock); } while(0)
 #define _raw_write_trylock(lock) ({ (void)(lock); (1); })
@@ -194,8 +199,8 @@ typedef struct {
 
 #define write_trylock(lock)	({preempt_disable();_raw_write_trylock(lock) ? \
 				1 : ({preempt_enable(); 0;});})
-
-/* Where's read_trylock? */
+#define read_trylock(lock)	({preempt_disable();_raw_read_trylock(lock) ? \
+				1 : ({preempt_enable(); 0;});})
 
 #if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
 void __preempt_spin_lock(spinlock_t *lock);
Index: linux-2.5/include/asm-i386/spinlock.h
===================================================================
RCS file: /home/cvs/linux-2.5/include/asm-i386/spinlock.h,v
retrieving revision 1.11
diff -b -p -u -r1.11 spinlock.h
--- linux-2.5/include/asm-i386/spinlock.h	19 May 2003 17:30:30 -0000	1.11
+++ linux-2.5/include/asm-i386/spinlock.h	12 Aug 2003 21:58:24 -0000
@@ -195,5 +195,22 @@ static inline int _raw_write_trylock(rwl
 	atomic_add(RW_LOCK_BIAS, count);
 	return 0;
 }
+
+static inline int _raw_read_trylock(rwlock_t *lock)
+{
+	unsigned char locked;
+	asm volatile(LOCK "subl %2,%0\n\t"
+		     "setns %1\n\t"
+		     "js 2f\n\t"
+		     "1:\n"
+		     LOCK_SECTION_START("")
+		     "2:\t" LOCK "incl %0\n\t"
+		     "jmp 1b\n"
+		     LOCK_SECTION_END
+		     : "=m" (lock->lock), "=qm" (locked)
+		     : "ir" (1), "0" (lock->lock) : "memory");
+	return locked;
+}
+#define HAVE_ARCH_RAW_READ_TRYLOCK 1
 
 #endif /* __ASM_SPINLOCK_H */

             reply	other threads:[~2003-08-13  6:14 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-08-13  6:14 Roland McGrath [this message]
2003-08-13  6:22 ` Christoph Hellwig
2003-08-13  9:01 ` Andrew Morton
2003-08-13  9:37   ` Roland McGrath
2003-08-13 11:27   ` Maciej W. Rozycki

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=200308130614.h7D6EHv07972@magilla.sf.frob.com \
    --to=roland@redhat.com \
    --cc=akpm@osdl.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=torvalds@osdl.org \
    /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®