From: Michel Lespinasse <walken@google.com>
To: David Howells <dhowells@redhat.com>, Andrew Morton <akpm@google.com>
Cc: LKML <linux-kernel@vger.kernel.org>
Subject: x86 rwsem: up_read() does not check active count in fast path
Date: Mon, 3 May 2010 15:51:05 -0700 [thread overview]
Message-ID: <20100503225105.GA18897@google.com> (raw)
Hi,
Looking at the x86 rwsem code, I have been wondering about the up_read() path.
__rwsem_do_wake() comment mentions that one should check the active count on
the way there; however I could not find that check when coming from up_read().
If there are multiple read locks active on a given semaphore, and there is
a write lock queued, each read lock will go:
__up_read
-> notice sem value is <0
-> rwsem_wake
-> take spinlock
-> add 1 in __rwsem_do_wake
-> notice there were other active lockers
-> substract 1 again under 'undo'
-> release spinlock
This could be avoided by adding a check for the active count in __up_write(),
as in the following untested patch:
diff --git a/arch/x86/include/asm/rwsem.h b/arch/x86/include/asm/rwsem.h
index 606ede1..915941f 100644
--- a/arch/x86/include/asm/rwsem.h
+++ b/arch/x86/include/asm/rwsem.h
@@ -200,11 +200,18 @@ static inline void __up_read(struct rw_semaphore *sem)
LOCK_PREFIX " xadd %1,(%2)\n\t"
/* subtracts 1, returns the old value */
" jns 1f\n\t"
+#ifdef CONFIG_X86_64
+ " dec %%edx\n\t" /* %edx = low 32 bits of %1 */
+#else
+ " dec %1\n\t"
+ " and %4,%1\n\t"
+#endif
+ " jnz 1f\n\t"
" call call_rwsem_wake\n"
"1:\n"
"# ending __up_read\n"
: "+m" (sem->count), "=d" (tmp)
- : "a" (sem), "1" (tmp)
+ : "a" (sem), "1" (tmp), "i" (RWSEM_ACTIVE_MASK)
: "memory", "cc");
}
Is there any reason not to do this ? I have to mention I don't have any
specific workload in mind that might benefit from this; I'm only sending this
because the code contradicts the __rwsem_do_wake() comment.
Thanks,
--
Michel "Walken" Lespinasse
A program is never fully debugged until the last user dies.
next reply other threads:[~2010-05-03 22:51 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-05-03 22:51 Michel Lespinasse [this message]
2010-05-04 12:41 ` x86 rwsem: up_read() does not check active count in fast path David Howells
2010-05-04 22:38 ` Michel Lespinasse
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=20100503225105.GA18897@google.com \
--to=walken@google.com \
--cc=akpm@google.com \
--cc=dhowells@redhat.com \
--cc=linux-kernel@vger.kernel.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