linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Hillf Danton <hdanton@sina.com>
To: Waiman Long <longman@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>, MM <linux-mm@kvack.org>,
	David Hildenbrand <david@redhat.com>,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: [RFC] locking/rwsem: dont wake up wwaiter in case of lock holder
Date: Fri,  1 Apr 2022 20:15:33 +0800	[thread overview]
Message-ID: <20220401121533.4979-1-hdanton@sina.com> (raw)
In-Reply-To: <c4642442-aff8-c287-015f-90f41f09c323@redhat.com>

On Mon, 28 Mar 2022 11:11:31 -0400 Waiman Long wrote:
> On 3/28/22 10:18, David Hildenbrand wrote:
> > On 26.03.22 14:40, Hillf Danton wrote:
> >> In the slowpath of down for write, we bail out in case of signal received and
> >> try to wake up any pending waiter but it makes no sense to wake up a write
> >> waiter given any lock holder, either write or read.
> >
> > But is handling this better really worth additional code and runtime
> > checks? IOW, does this happen often enough that we actually care about
> > optimizing this? I have no idea :)

Thanks for taking a look, David.
> >
> >> The RFC is do nothing for wwaiter if any lock holder present - they will fill
> >> their duty at lock release time.
> >>
> >> Only for thoughts now.
> >>
> >> Hillf
> >>
> >> --- x/kernel/locking/rwsem.c
> >> +++ y/kernel/locking/rwsem.c
> >> @@ -418,6 +418,8 @@ static void rwsem_mark_wake(struct rw_se
> >>   	waiter = rwsem_first_waiter(sem);
> >>   
> >>   	if (waiter->type == RWSEM_WAITING_FOR_WRITE) {
> >> +		if (RWSEM_LOCK_MASK & atomic_long_read(&sem->count))
> >> +			return;
> >>   		if (wake_type == RWSEM_WAKE_ANY) {
> >>   			/*
> >>   			 * Mark writer at the front of the queue for wakeup.
> >> --
> 
> That check isn't good enough. First of all, any reader count in 
> sem->count can be transient due to the fact that we do an unconditional 
> atomic_long_add() on down_read(). The reader may then remove its reader 
> count in the slow path.

Correct.

> This patch may cause missed wakeup which is a 
> much bigger problem than spending a bit of cpu time to check for lock 
> availability and sleep again.

In rwsem_down_read_slowpath(), the comment prior to the RWSEM_WAKE_ANY
wakeup rules out the chance for missed wakeup, because the RFC is only
for wwaiter who is exclusive from any lock holder.

It is not unusual for me to miss anything, OTOH, particularly in cases
like this one worth twenty minutes of scratching scalp.

Hillf
	/*
	 * If there are no active locks, wake the front queued process(es).

The RFC goes in line with the top half.

	 *
	 * If there are no writers and we are first in the queue,
	 * wake our own waiter to join the existing active readers !
	 */


      reply	other threads:[~2022-04-01 12:16 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-26 13:40 Hillf Danton
2022-03-28 14:18 ` David Hildenbrand
2022-03-28 15:11   ` Waiman Long
2022-04-01 12:15     ` Hillf Danton [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=20220401121533.4979-1-hdanton@sina.com \
    --to=hdanton@sina.com \
    --cc=david@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=longman@redhat.com \
    --cc=peterz@infradead.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