From: Davidlohr Bueso <dave@stgolabs.net>
To: Manfred Spraul <manfred@colorfullife.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
LKML <linux-kernel@vger.kernel.org>,
Michael Kerrisk <mtk.manpages@gmail.com>,
Rafael Aquini <aquini@redhat.com>, Rik van Riel <riel@redhat.com>,
1vier1@web.de
Subject: Re: [PATCH 1/3] ipc/sem.c: Chance memory barrier in sem_lock() to smp_rmb()
Date: Thu, 09 Oct 2014 16:07:34 -0700 [thread overview]
Message-ID: <1412896054.8874.6.camel@linux-t7sj.site> (raw)
In-Reply-To: <1412620363-2759-2-git-send-email-manfred@colorfullife.com>
On Mon, 2014-10-06 at 20:32 +0200, Manfred Spraul wrote:
> When I fixed bugs in the sem_lock() logic, I was more conservative than
> necessary.
> Therefore it is safe to replace the smp_mb() with smp_rmb().
> And: With smp_rmb(), semop() syscalls are up to 10% faster.
>
> The race we must protect against is:
>
> sem->lock is free
> sma->complex_count = 0
> sma->sem_perm.lock held by thread B
>
> thread A:
>
> A: spin_lock(&sem->lock)
>
> B: sma->complex_count++; (now 1)
> B: spin_unlock(&sma->sem_perm.lock);
>
> A: spin_is_locked(&sma->sem_perm.lock);
> A: XXXXX memory barrier
> A: if (sma->complex_count == 0)
>
> Thread A must read the increased complex_count value, i.e. the read must
> not be reordered with the read of sem_perm.lock done by spin_is_locked().
>
> Since it's about ordering of reads, smp_rmb() is sufficient.
>
> Signed-off-by: Manfred Spraul <manfred@colorfullife.com>
Reviewed-by: Davidlohr Bueso <dave@stgolabs.net>
With a suggestion below.
> ---
> ipc/sem.c | 12 +++++++++---
> 1 file changed, 9 insertions(+), 3 deletions(-)
>
> diff --git a/ipc/sem.c b/ipc/sem.c
> index 454f6c6..ffc71de 100644
> --- a/ipc/sem.c
> +++ b/ipc/sem.c
> @@ -326,10 +326,16 @@ static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
>
> /* Then check that the global lock is free */
> if (!spin_is_locked(&sma->sem_perm.lock)) {
> - /* spin_is_locked() is not a memory barrier */
> - smp_mb();
> + /*
> + * The next test must happen after the test for
> + * sem_perm.lock, otherwise we can race with another
> + * thread that does
> + * complex_count++;spin_unlock(sem_perm.lock);
> + */
How about this comment instead:
/*
* The ipc object lock check must be visible on all cores before
* rechecking the complex count. Otherwise we can race with
* another thread that does:
* complex_count++++;
* spin_unlock(sem_perm.lock);
*/
prev parent reply other threads:[~2014-10-09 23:07 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-10-06 18:32 [PATCH 0/3] Misc updates to sysv ipc Manfred Spraul
2014-10-06 18:32 ` [PATCH 1/3] ipc/sem.c: Chance memory barrier in sem_lock() to smp_rmb() Manfred Spraul
2014-10-06 18:32 ` [PATCH 2/3] ipc/sem.c: increase SEMMSL, SEMMNI, SEMOPM Manfred Spraul
2014-10-06 18:32 ` [PATCH 3/3] ipc/msg: increase MSGMNI, remove scaling Manfred Spraul
2014-10-07 20:09 ` [PATCH 2/3] ipc/sem.c: increase SEMMSL, SEMMNI, SEMOPM Rafael Aquini
2014-10-07 20:08 ` [PATCH 1/3] ipc/sem.c: Chance memory barrier in sem_lock() to smp_rmb() Rafael Aquini
2014-10-09 23:07 ` Davidlohr Bueso [this message]
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=1412896054.8874.6.camel@linux-t7sj.site \
--to=dave@stgolabs.net \
--cc=1vier1@web.de \
--cc=akpm@linux-foundation.org \
--cc=aquini@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=manfred@colorfullife.com \
--cc=mtk.manpages@gmail.com \
--cc=riel@redhat.com \
/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®