linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jan Stancek <jstancek@redhat.com>
To: Will Deacon <will@kernel.org>
Cc: Waiman Long <longman@redhat.com>,
	linux-kernel@vger.kernel.org, dbueso@suse.de,
	peterz@infradead.org, mingo@redhat.com,
	jade alglave <jade.alglave@arm.com>,
	paulmck@linux.vnet.ibm.com, Jan Stancek <jstancek@redhat.com>
Subject: Re: [PATCH v2] locking/rwsem: add acquire barrier to read_slowpath exit when queue is empty
Date: Thu, 18 Jul 2019 06:50:52 -0400 (EDT)	[thread overview]
Message-ID: <79224323.853324.1563447052432.JavaMail.zimbra@redhat.com> (raw)
In-Reply-To: <20190718092640.52oliw3sid7gxyh6@willie-the-truck>


----- Original Message -----
> Hi Jan, Waiman, [+Jade and Paul for the litmus test at the end]
> 
> On Wed, Jul 17, 2019 at 09:22:00PM +0200, Jan Stancek wrote:
> > On Wed, Jul 17, 2019 at 10:19:04AM -0400, Waiman Long wrote:
> > > > If you add a comment to the code outlining the issue (preferably as a
> > > > litmus
> > > > test involving sem->count and some shared data which happens to be
> > > > vmacache_seqnum in your test)), then:
> > > > 
> > > > Reviewed-by: Will Deacon <will@kernel.org>
> > > > 
> > > > Thanks,
> > > > 
> > > > Will
> > > 
> > > Agreed. A comment just above smp_acquire__after_ctrl_dep() on why this
> > > is needed will be great.
> > > 
> > > Other than that,
> > > 
> > > Acked-by: Waiman Long <longman@redhat.com>
> > > 
> > 
> > litmus test looks a bit long, would following be acceptable?
> > 
> > diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c
> > index 37524a47f002..d9c96651bfc7 100644
> > --- a/kernel/locking/rwsem.c
> > +++ b/kernel/locking/rwsem.c
> > @@ -1032,6 +1032,13 @@ static inline bool rwsem_reader_phase_trylock(struct
> > rw_semaphore *sem,
> >  		 */
> >  		if (adjustment && !(atomic_long_read(&sem->count) &
> >  		     (RWSEM_WRITER_MASK | RWSEM_FLAG_HANDOFF))) {
> > +			/*
> > +			 * down_read() issued ACQUIRE on enter, but we can race
> > +			 * with writer who did RELEASE only after us.
> > +			 * ACQUIRE here makes sure reader operations happen only
> > +			 * after all writer ones.
> > +			 */
> 
> How about an abridged form of the litmus test here, just to show the cod
> flow? e.g.:
> 
> /*
>  * We need to ensure ACQUIRE semantics when reading sem->count so that
>  * we pair with the RELEASE store performed by an unlocking/downgrading
>  * writer.
>  *
>  * P0 (writer)			P1 (reader)
>  *
>  * down_write(sem);
>  * <write shared data>
>  * downgrade_write(sem);
>  * -> fetch_add_release(&sem->count)
>  *
>  *				down_read_slowpath(sem);
>  *				-> atomic_read(&sem->count)
>  *				   <ctrl dep>
>  *				   smp_acquire__after_ctrl_dep()
>  *				<read shared data>
>  */

Works for me. The code is at 3 level of indentation, but I can try
to squeeze it in for v4.

> 
> In writing this, I also noticed that we don't have any explicit ordering
> at the end of the reader slowpath when we wait on the queue but get woken
> immediately:
> 
> 	if (!waiter.task)
> 		break;
> 
> Am I missing something?

I'm assuming this isn't problem, because set_current_state() on line above
is using smp_store_mb().

> 
> > +			smp_acquire__after_ctrl_dep();
> >  			raw_spin_unlock_irq(&sem->wait_lock);
> >  			rwsem_set_reader_owned(sem);
> >  			lockevent_inc(rwsem_rlock_fast);
> > 
> > 
> > with litmus test in commit log:
> > ----------------------------------- 8< ------------------------------------
> > C rwsem
> > 
> > {
> > 	atomic_t rwsem_count = ATOMIC_INIT(1);
> > 	int vmacache_seqnum = 10;
> > }
> > 
> > P0(int *vmacache_seqnum, atomic_t *rwsem_count)
> > {
> > 	r0 = READ_ONCE(*vmacache_seqnum);
> > 	WRITE_ONCE(*vmacache_seqnum, r0 + 1);
> > 	/* downgrade_write */
> > 	r1 = atomic_fetch_add_release(-1+256, rwsem_count);
> > }
> > 
> > P1(int *vmacache_seqnum, atomic_t *rwsem_count, spinlock_t *sem_wait_lock)
> > {
> > 	/* rwsem_read_trylock */
> > 	r0 = atomic_add_return_acquire(256, rwsem_count);
> > 	/* rwsem_down_read_slowpath */
> > 	spin_lock(sem_wait_lock);
> > 	r0 = atomic_read(rwsem_count);
> > 	if ((r0 & 1) == 0) {
> > 		// BUG: needs barrier
> > 		spin_unlock(sem_wait_lock);
> > 		r1 = READ_ONCE(*vmacache_seqnum);
> > 	}
> > }
> > exists (1:r1=10)
> > ----------------------------------- 8< ------------------------------------
> 
> Thanks for writing this! It's definitely worth sticking it in the commit
> log, but Paul and Jade might also like to include it as part of their litmus
> test repository too.
> 
> Will
> 

  reply	other threads:[~2019-07-18 10:50 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-16 16:04 [PATCH] locking/rwsem: use read_acquire in read_slowpath exit when queue is empty Jan Stancek
2019-07-16 16:53 ` Waiman Long
2019-07-16 18:34   ` Jan Stancek
2019-07-16 18:58   ` Peter Zijlstra
2019-07-16 19:09     ` Waiman Long
2019-07-17 12:02     ` [PATCH v2] locking/rwsem: add acquire barrier to " Jan Stancek
2019-07-17 13:13       ` Will Deacon
2019-07-17 14:19         ` Waiman Long
2019-07-17 19:22           ` Jan Stancek
2019-07-17 19:39             ` Waiman Long
2019-07-18  8:51               ` [PATCH v3] " Jan Stancek
2019-07-25 16:00                 ` [tip:locking/core] locking/rwsem: Add missing ACQUIRE " tip-bot for Jan Stancek
2019-07-18  9:26             ` [PATCH v2] locking/rwsem: add acquire barrier " Will Deacon
2019-07-18 10:50               ` Jan Stancek [this message]
2019-07-18 11:04                 ` Peter Zijlstra
2019-07-18 11:09                   ` Peter Zijlstra
2019-07-18 11:36                     ` Jan Stancek
2019-07-18 12:12                 ` Paul E. McKenney
2019-07-18 10:58               ` Peter Zijlstra
2019-07-18 11:45                 ` Will Deacon
2019-07-18 12:23                   ` Peter Zijlstra
2019-07-17 15:33       ` Waiman Long

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=79224323.853324.1563447052432.JavaMail.zimbra@redhat.com \
    --to=jstancek@redhat.com \
    --cc=dbueso@suse.de \
    --cc=jade.alglave@arm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mingo@redhat.com \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=peterz@infradead.org \
    --cc=will@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;
as well as URLs for NNTP newsgroup(s).