From: Amerigo Wang <amwang@redhat.com>
To: David Howells <dhowells@redhat.com>
Cc: 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: Thu, 08 Oct 2009 17:15:23 +0800 [thread overview]
Message-ID: <4ACDADAB.3030403@redhat.com> (raw)
In-Reply-To: <13922.1254917951@redhat.com>
David Howells wrote:
> 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.
>
Hmm, yeah...
>> 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.
Ah, yes, I knew this, I kept this just for completeness.
I will remove this part then. :)
THanks!
prev parent reply other threads:[~2009-10-08 9:13 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
2009-10-08 9:15 ` Amerigo Wang [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=4ACDADAB.3030403@redhat.com \
--to=amwang@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=behlendorf1@llnl.gov \
--cc=bwoodard@llnl.gov \
--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