From: David Howells <dhowells@redhat.com>
To: Amerigo Wang <amwang@redhat.com>
Cc: dhowells@redhat.com, linux-kernel@vger.kernel.org,
Ben Woodard <bwoodard@llnl.gov>,
akpm@linux-foundation.org,
Brian Behlendorf <behlendorf1@llnl.gov>
Subject: Re: [Patch v3] rwsem: fix rwsem_is_locked() bugs
Date: Wed, 07 Oct 2009 13:19:11 +0100 [thread overview]
Message-ID: <13922.1254917951@redhat.com> (raw)
In-Reply-To: <20091006065815.3927.12069.sendpatchset@localhost.localdomain>
Amerigo Wang <amwang@redhat.com> wrote:
> static inline int rwsem_is_locked(struct rw_semaphore *sem)
> {
> - return (sem->activity != 0);
> + int ret;
> +
> + if (spin_trylock_irq(&sem->wait_lock)) {
> + ret = !(list_empty(&sem->wait_list) && sem->activity == 0);
> + spin_unlock_irq(&sem->wait_lock);
> + return ret;
> + }
> + return 1;
> }
Yep... This seems a reasonable approach, though I contend that if you're
holding the spinlock, then sem->wait_list _must_ be empty if sem->activity is
0 - so that half of the test is redundant.
sem->activity == 0 and sem->wait_list not being empty is a transitional state
that can only occur in ups and downgrades whilst they hold the spinlock.
> diff --git a/lib/rwsem-spinlock.c b/lib/rwsem-spinlock.c
> index 9df3ca5..234d83f 100644
> --- a/lib/rwsem-spinlock.c
> +++ b/lib/rwsem-spinlock.c
> @@ -78,7 +78,12 @@ __rwsem_do_wake(struct rw_semaphore *sem, int wakewrite)
>
> /* grant an infinite number of read locks to the front of the queue */
> dont_wake_writers:
> - woken = 0;
> + /*
> + * we increase ->activity just to make rwsem_is_locked() happy,
> + * to avoid potential cache line ping-pong, we don't do this
> + * within the following loop.
> + */
> + woken = sem->activity++;
> while (waiter->flags & RWSEM_WAITING_FOR_READ) {
> struct list_head *next = waiter->list.next;
>
> @@ -94,7 +99,7 @@ __rwsem_do_wake(struct rw_semaphore *sem, int wakewrite)
> waiter = list_entry(next, struct rwsem_waiter, list);
> }
>
> - sem->activity += woken;
> + sem->activity = woken;
>
> out:
> return sem;
This change to __rwsem_do_wake() is all unnecessary - you're defending against
the test of sem->activity by rwsem_is_locked() - but that now happens with the
spinlock held.
David
next prev parent reply other threads:[~2009-10-07 12:20 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-10-06 6:55 [Patch v3] rwsem: fix rwsem_is_locked() bugs Amerigo Wang
2009-10-07 9:41 ` Amerigo Wang
2009-10-07 12:19 ` David Howells [this message]
2009-10-08 9:15 ` Amerigo Wang
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=13922.1254917951@redhat.com \
--to=dhowells@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=amwang@redhat.com \
--cc=behlendorf1@llnl.gov \
--cc=bwoodard@llnl.gov \
--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