Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp2188547ybi; Thu, 18 Jul 2019 04:46:40 -0700 (PDT) X-Google-Smtp-Source: APXvYqyyR/lBaVbWN4E0mBYXT8zgTT8DqHUUlxs4vPZMqFu63EAYxyUJW6ph2TET0uNYb6Rsax27 X-Received: by 2002:a17:902:9307:: with SMTP id bc7mr48122062plb.183.1563450400677; Thu, 18 Jul 2019 04:46:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563450400; cv=none; d=google.com; s=arc-20160816; b=Bmm1eTSzYXpUbbQyBV5qV/ReZ0NoLdA41k0VRy+WU9gLbKmhIjBeZMjdeHpez1NjjP a3jIJUUquR59CA6Nc3M1GT3I+K/2HZ+HP4DZIi5NV2BdK1YmByD5Zuw6WrI+CxnCGI1G oK64LpjSOOBdgdx9qItbiMwQJ8j0K5OiLIONnFdOyeukxRbHQ3nJAbhNKYdr7CJ/ekvi on8UjDQY4BmD61+g3yuzMfZRqFKLtlsKKJd54phVNeUfNqb0N/McpDzTUQ8JZXHC363O VnwzcjijFjP002cwMdREoxwkKxQmTOuwRm9zEFis8xM6QXuYLqaryso6fMFGU3ku6U77 e0yw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=5nJ9Rq+e3TeayEHq6FsEuz24IJHauvAPt0C+OFuzwqc=; b=Ni7zlzLuisilMGrmph0qxTn3cTF8AR+AYWJoDASbMt7BWy5Ab6sIOXNR007z4SxL0I ycdXwD5jyjOpHxgjYrn7rakjLMynb/UuBs3mTOL8Q3ECwiIoSdL1P5gaHLExCGy6cLzY waipt9Q4F76YgaMd1FeGeurqBENyorAeHivxr9kBV1wPGc8kPEYEkKDoetKlhevncgNP IfohoUIgYrJM/wxuJnIInnv28gtEKrhNsJC0NmJd2wMBxP4VL6y1LfoxhJdeeI+84Hks ZXD5sATjRQCng5jjk28x/x+GSDfYxwsS7QsG8vAolB7AeDLrAee8B2wcCGXwXp74YuRf XR/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=bFr4hJ+a; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z5si378301pgj.213.2019.07.18.04.46.23; Thu, 18 Jul 2019 04:46:40 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=bFr4hJ+a; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390180AbfGRLpx (ORCPT + 99 others); Thu, 18 Jul 2019 07:45:53 -0400 Received: from mail.kernel.org ([198.145.29.99]:32948 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726608AbfGRLpx (ORCPT ); Thu, 18 Jul 2019 07:45:53 -0400 Received: from willie-the-truck (236.31.169.217.in-addr.arpa [217.169.31.236]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E121D2085A; Thu, 18 Jul 2019 11:45:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1563450352; bh=RPpl1KU1qr/RkmdD31fDZWCThQM0vN+6XBl50RiqJJE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=bFr4hJ+aD6ugXNf56PVi1ZCswiZWGPB+WmeEwKSIEIIn1xCZPKECRENlC5+NPXSb1 AsQsnICVRq4W/Lyojpx7N2ElsvTqLJ3FiT9Tvsnxnzq8rCoSBsY7RZ5wIb/mdZSiis UqfPno0uTpymZN4smggxej+JZavlSBuDvWDtKMqc= Date: Thu, 18 Jul 2019 12:45:47 +0100 From: Will Deacon To: Peter Zijlstra Cc: Jan Stancek , Waiman Long , linux-kernel@vger.kernel.org, dbueso@suse.de, mingo@redhat.com, jade.alglave@arm.com, paulmck@linux.vnet.ibm.com Subject: Re: [PATCH v2] locking/rwsem: add acquire barrier to read_slowpath exit when queue is empty Message-ID: <20190718114547.v4c7ucsp6k4i6o3b@willie-the-truck> References: <20190716185807.GJ3402@hirez.programming.kicks-ass.net> <20190717131335.b2ry43t2ov7ba4t4@willie-the-truck> <21ff5905-198b-6ea5-6c2a-9fb10cb48ea7@redhat.com> <20190717192200.GA17687@dustball.usersys.redhat.com> <20190718092640.52oliw3sid7gxyh6@willie-the-truck> <20190718105812.GB3419@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190718105812.GB3419@hirez.programming.kicks-ass.net> User-Agent: NeoMutt/20170113 (1.7.2) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jul 18, 2019 at 12:58:12PM +0200, Peter Zijlstra wrote: > On Thu, Jul 18, 2019 at 10:26:41AM +0100, Will Deacon wrote: > > > /* > > * 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); > > * > > * downgrade_write(sem); > > * -> fetch_add_release(&sem->count) > > * > > * down_read_slowpath(sem); > > * -> atomic_read(&sem->count) > > * > > * smp_acquire__after_ctrl_dep() > > * > > */ > > So I'm thinking all this is excessive; the simple rule is: lock acquire > should imply ACQUIRE, we all know why. Fair enough, I just thought this was worth highlighting because you can't reply on the wait_lock to give you ACQUIRE ordering. > > 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? > > Ha!, I ran into the very same one. I keep confusing myself, but I think > you're right and that needs to be smp_load_acquire() to match the > smp_store_release() in rwsem_mark_wake(). > > (the actual race there is _tiny_ due to the smp_mb() right before it, > but I cannot convince myself that is indeed sufficient) > > The signal_pending_state() case is also fun, but I think wait_lock there > is sufficient (even under RCpc). > > I've ended up with this.. > > --- > diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c > index 37524a47f002..9eb630904a17 100644 > --- a/kernel/locking/rwsem.c > +++ b/kernel/locking/rwsem.c > @@ -1000,6 +1000,7 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, int state) > atomic_long_add(-RWSEM_READER_BIAS, &sem->count); > adjustment = 0; > if (rwsem_optimistic_spin(sem, false)) { > + /* rwsem_optimistic_spin() implies ACQUIRE through rwsem_*trylock() */ I couldn't figure out if this was dependent on the return value or not, and looking at osq_lock() I also couldn't see the ACQUIRE barrier when we're spinning on node->locked. Hmm. > /* > * Wake up other readers in the wait list if the front > * waiter is a reader. > @@ -1014,6 +1015,7 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, int state) > } > return sem; > } else if (rwsem_reader_phase_trylock(sem, waiter.last_rowner)) { > + /* rwsem_reader_phase_trylock() implies ACQUIRE */ Can we add "on success" to the end of this, please? > return sem; > } > > @@ -1032,6 +1034,8 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, int state) > */ > if (adjustment && !(atomic_long_read(&sem->count) & > (RWSEM_WRITER_MASK | RWSEM_FLAG_HANDOFF))) { > + /* Provide lock ACQUIRE */ > + smp_acquire__after_ctrl_dep(); > raw_spin_unlock_irq(&sem->wait_lock); > rwsem_set_reader_owned(sem); > lockevent_inc(rwsem_rlock_fast); > @@ -1065,15 +1069,25 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, int state) > wake_up_q(&wake_q); > > /* wait to be given the lock */ > - while (true) { > + for (;;) { > set_current_state(state); > - if (!waiter.task) > + if (!smp_load_acquire(&waiter.task)) { > + /* > + * Matches rwsem_mark_wake()'s smp_store_release() and ensures > + * we're ordered against its sem->count operations. > + */ > break; > + } Ack. Also, grepping for 'waiter.task' reveals a similar usage in drivers/tty/tty_ldsem.c if you're feeling brave enough. Will