From patchwork Thu Oct 5 13:56:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Zijlstra X-Patchwork-Id: 9987231 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E8B6A6029B for ; Thu, 5 Oct 2017 13:56:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D5BD928C90 for ; Thu, 5 Oct 2017 13:56:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CA48B28C92; Thu, 5 Oct 2017 13:56:29 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 5CAC428C90 for ; Thu, 5 Oct 2017 13:56:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=myxc4+On9nhTeAQnHMQmCKN718sr16+//5YRpfNig1g=; b=dNLLqNv8eohB8D oBQ1fVk1+9l+0Wd0Fmns17KDIUOzWotlGbmOabcuyCHsA5Bvh8DD92O12Pdeo6WJ2X7PjIwL7eW9e 3gUdgLQ6JaYptqSEfcHXh6/+6hpXtK6IcRH0Um3WVER3DFnOkF0J0OaKYc3jd1qcNsxLUSFT2euYH Gjtwcd0Ds1QY7MmfA1H9PhXJux4WGl0i9kBY+E4PShZPIuVJ/HIVQKYy7tbNGBy5eTCmCyKl6cEks n+RL6w1L2lk2RCfMs3A7uN0ECJMAnksGpGiO2zIy6MXiizookKzhdbzKj21ObgJCJ/5Kr8u5zuKO0 RkTlfXYP19fdD3Qot1nw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1e06dT-0001Js-Oh; Thu, 05 Oct 2017 13:56:23 +0000 Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by bombadil.infradead.org with esmtpsa (Exim 4.87 #1 (Red Hat Linux)) id 1e06dR-0001JK-LH; Thu, 05 Oct 2017 13:56:22 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id C42BF202380E1; Thu, 5 Oct 2017 15:56:18 +0200 (CEST) Date: Thu, 5 Oct 2017 15:56:18 +0200 From: Peter Zijlstra To: Will Deacon Subject: Re: [PATCH 5/6] kernel/locking: Prevent slowpath writers getting held up by fastpath Message-ID: <20171005135618.yufhaklq5cefaiyn@hirez.programming.kicks-ass.net> References: <1507208097-825-1-git-send-email-will.deacon@arm.com> <1507208097-825-6-git-send-email-will.deacon@arm.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <1507208097-825-6-git-send-email-will.deacon@arm.com> User-Agent: NeoMutt/20170609 (1.8.3) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: boqun.feng@gmail.com, linux-kernel@vger.kernel.org, Jeremy.Linton@arm.com, mingo@redhat.com, longman@redhat.com, paulmck@linux.vnet.ibm.com, linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP On Thu, Oct 05, 2017 at 01:54:56PM +0100, Will Deacon wrote: > When a prospective writer takes the qrwlock locking slowpath due to the > lock being held, it attempts to cmpxchg the wmode field from 0 to > _QW_WAITING so that concurrent lockers also take the slowpath and queue > on the spinlock accordingly, allowing the lockers to drain. > > Unfortunately, this isn't fair, because a fastpath writer that comes in > after the lock is made available but before the _QW_WAITING flag is set > can effectively jump the queue. If there is a steady stream of prospective > writers, then the waiter will be held off indefinitely. > > This patch restores fairness by separating _QW_WAITING and _QW_LOCKED > into two bits in the wmode byte and having the waiter set _QW_WAITING > unconditionally. This then forces the slow-path for concurrent lockers, > but requires that a writer unlock operation performs an > atomic_sub_release instead of a store_release so that the waiting status > is preserved. > diff --git a/include/asm-generic/qrwlock.h b/include/asm-generic/qrwlock.h > index 02c0a768e6b0..8b7edef500e5 100644 > --- a/include/asm-generic/qrwlock.h > +++ b/include/asm-generic/qrwlock.h > @@ -41,7 +41,7 @@ > * +----+----+----+----+ > */ > #define _QW_WAITING 1 /* A writer is waiting */ > -#define _QW_LOCKED 0xff /* A writer holds the lock */ > +#define _QW_LOCKED 2 /* A writer holds the lock */ > #define _QW_WMASK 0xff /* Writer mask */ > #define _QR_SHIFT 8 /* Reader count shift */ > #define _QR_BIAS (1U << _QR_SHIFT) > @@ -134,7 +134,7 @@ static inline void queued_read_unlock(struct qrwlock *lock) > */ > static inline void queued_write_unlock(struct qrwlock *lock) > { > - smp_store_release(&lock->wmode, 0); > + (void)atomic_sub_return_release(_QW_LOCKED, &lock->cnts); > } That is a fairly painful hit on x86. Changes a regular store into an "LOCK XADD" +20 cycles right there. Can't we steal one of the reader bits for waiting? Acked-by: Waiman Long diff --git a/include/asm-generic/qrwlock.h b/include/asm-generic/qrwlock.h index 7d026bf27713..5524801a02a5 100644 --- a/include/asm-generic/qrwlock.h +++ b/include/asm-generic/qrwlock.h @@ -40,10 +40,10 @@ * | rd | wr | * +----+----+----+----+ */ -#define _QW_WAITING 1 /* A writer is waiting */ -#define _QW_LOCKED 0xff /* A writer holds the lock */ -#define _QW_WMASK 0xff /* Writer mask */ -#define _QR_SHIFT 8 /* Reader count shift */ +#define _QW_WAITING 0x100 /* A writer is waiting */ +#define _QW_LOCKED 0x0ff /* A writer holds the lock */ +#define _QW_WMASK 0x1ff /* Writer mask */ +#define _QR_SHIFT 9 /* Reader count shift */ #define _QR_BIAS (1U << _QR_SHIFT) /* diff --git a/kernel/locking/qrwlock.c b/kernel/locking/qrwlock.c index 2655f26ec882..5f75caea97e0 100644 --- a/kernel/locking/qrwlock.c +++ b/kernel/locking/qrwlock.c @@ -54,7 +54,7 @@ struct __qrwlock { static __always_inline void rspin_until_writer_unlock(struct qrwlock *lock, u32 cnts) { - while ((cnts & _QW_WMASK) == _QW_LOCKED) { + while ((cnts & _QW_LOCKED)) { cpu_relax(); cnts = atomic_read_acquire(&lock->cnts); } @@ -120,21 +120,10 @@ void queued_write_lock_slowpath(struct qrwlock *lock) (atomic_cmpxchg_acquire(&lock->cnts, 0, _QW_LOCKED) == 0)) goto unlock; - /* - * Set the waiting flag to notify readers that a writer is pending, - * or wait for a previous writer to go away. - */ - for (;;) { - struct __qrwlock *l = (struct __qrwlock *)lock; - - if (!READ_ONCE(l->wmode) && - (cmpxchg_relaxed(&l->wmode, 0, _QW_WAITING) == 0)) - break; - - cpu_relax(); - } + /* Set the waiting flag to notify readers that a writer is pending */ + atomic_add(_QW_WAITING, &lock->cnts); - /* When no more readers, set the locked flag */ + /* When no more readers or writers, set the locked flag */ for (;;) { cnts = atomic_read(&lock->cnts); if ((cnts == _QW_WAITING) &&