Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp4161519yba; Wed, 17 Apr 2019 06:01:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqzufEt1o8HgRcebezsjRI/FKDeoKkukBt6Rq0pB4jKoDIK2g/vxwSnGuTfVrRbsowmCjGs7 X-Received: by 2002:a17:902:7883:: with SMTP id q3mr89105566pll.60.1555506095557; Wed, 17 Apr 2019 06:01:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555506095; cv=none; d=google.com; s=arc-20160816; b=ky9cgbgfLI8RZybOcFrLtZ3mBZV3oe2V7oIyl824FKN+COPZvP6WXohev6+C8qNXzl LH2AXL1Nl25BeUkw4okGogndHkI2OIMQ8u5U/OljAIxhtjC8g8bkH/kBpouvX1bJScpr 5VuFa396lOJKCi2MjuBAHU/OcI0ds06VIo7u2TaPDyD6kPuFtQ0nzl/Zc0GjuwFwAl+r juTAgAqJVd4CGwtqT3MQ6e+sKxmj+UKQM8dhV6KBWXjvIifYc7gpAV/vzDTGW2QFTW7l MCZ4S4Vcexhx+EthiF/8j4nhsTstJC00sip0c3Us3DBVzC/n74akRy0tJI2UCr5t3W3g x1AQ== 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=x3TB78eLMmND4F0NhqqOIiB/RJHNLVhUND4CEfUPCFo=; b=JQx8cgKU1VTcohO+50sucv1sHggPTAnUWpUW3L2s0FUyPua46tZ2vnzPXDE+zRMsYC u7cbJH3Qm40kbfuDpt4zn351WgS4mPGFvMIdCYS1+vzpJwZrgtZTpzL/2NRo8eudTHWT RMB13W+TYYOKSxYlpMiy+5DYJaxdB4MLzCXlrlt+qBPHQC0sNQIhPMFhKPENn26iLEOu J/o4HWCwiVhNBscGKyLpTG/m7nCFNjmrxwi5VcbF9x0tEek7CZrP0eAcW1nJ7kQ6PrbQ 3949Ymz+PViusSOJF8tXmAl1daSFTfrPfYW1KvopfcBx0BvkFfCP+XG/dvBmPsN4JLan Ag5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=KsqX64pA; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j12si50720662plk.144.2019.04.17.06.01.20; Wed, 17 Apr 2019 06:01:35 -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=fail header.i=@infradead.org header.s=merlin.20170209 header.b=KsqX64pA; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732184AbfDQNA1 (ORCPT + 99 others); Wed, 17 Apr 2019 09:00:27 -0400 Received: from merlin.infradead.org ([205.233.59.134]:57124 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729318AbfDQNA1 (ORCPT ); Wed, 17 Apr 2019 09:00:27 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=x3TB78eLMmND4F0NhqqOIiB/RJHNLVhUND4CEfUPCFo=; b=KsqX64pA42RMrZpuxVvORZeR4 n1hmBacM6fYUjLCs4X33e45ezLlY05VAcOyhWtdaxJ2quXC+JjQCzIfw8S2ynuq3gLfF2449WlAOw jt/hlDYCJUiv5JvMBVelaW4jJWy9M6fiK7Ihu7xb2FMYrn03IN9HzPrrKFvY3QJXqMbpVtXrioPA9 hEDbSHLt3/5r4xMNRlhSP2uqR5SIQJIgJ1eJ5CUTbkf3yCUPKhwVzEVZwOsoj+Uv7Qm89Ajv1vrGD nMhnOqvo2OG1MlMvEwaSuP1xQA06JjHsbWzDFEw/ZME9UNFYOcloEwx5qcL3QqjNKAxBK1/twp+IY fw+o0dMhg==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by merlin.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1hGkAj-0002VF-Qi; Wed, 17 Apr 2019 13:00:18 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 92AD429AD7C08; Wed, 17 Apr 2019 15:00:16 +0200 (CEST) Date: Wed, 17 Apr 2019 15:00:16 +0200 From: Peter Zijlstra To: Waiman Long Cc: Ingo Molnar , Will Deacon , Thomas Gleixner , linux-kernel@vger.kernel.org, x86@kernel.org, Davidlohr Bueso , Linus Torvalds , Tim Chen , huang ying Subject: Re: [PATCH v4 08/16] locking/rwsem: Make rwsem_spin_on_owner() return owner state Message-ID: <20190417130016.GU14281@hirez.programming.kicks-ass.net> References: <20190413172259.2740-1-longman@redhat.com> <20190413172259.2740-9-longman@redhat.com> <20190417124101.GE4038@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190417124101.GE4038@hirez.programming.kicks-ass.net> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 17, 2019 at 02:41:01PM +0200, Peter Zijlstra wrote: > On Sat, Apr 13, 2019 at 01:22:51PM -0400, Waiman Long wrote: > > In the special case that there is no active lock and the handoff bit > > is set, optimistic spinning has to be stopped. > > > @@ -500,9 +521,19 @@ static noinline bool rwsem_spin_on_owner(struct rw_semaphore *sem) > > > > /* > > * If there is a new owner or the owner is not set, we continue > > - * spinning. > > + * spinning except when here is no active locks and the handoff bit > > + * is set. In this case, we have to stop spinning. > > */ > > - return is_rwsem_owner_spinnable(READ_ONCE(sem->owner)); > > + owner = READ_ONCE(sem->owner); > > + if (!is_rwsem_owner_spinnable(owner)) > > + return OWNER_NONSPINNABLE; > > + if (owner && !is_rwsem_owner_reader(owner)) > > + return OWNER_WRITER; > > + > > + count = atomic_long_read(&sem->count); > > + if (RWSEM_COUNT_HANDOFF(count) && !RWSEM_COUNT_LOCKED(count)) > > + return OWNER_NONSPINNABLE; > > + return !owner ? OWNER_NULL : OWNER_READER; > > } > > So this fixes a straight up bug in the last patch (and thus should be > done before so the bug never exists), and creates unreadable code while > at it. > > Also, I think only checking HANDOFF after the loop is wrong; the moment > HANDOFF happens you have to terminate the loop, irrespective of what > @owner does. > > Does something like so work? > > --- enum owner_state { OWNER_NULL = 1 << 0, OWNER_WRITER = 1 << 1, OWNER_READER = 1 << 2, OWNER_NONSPINNABLE = 1 << 3, }; #define OWNER_SPINNABLE (OWNER_NULL | OWNER_WRITER) static inline enum owner_state rwsem_owner_state(unsigned long owner) { if (!owner) return OWNER_NULL; if (owner & RWSEM_ANONYMOUSLY_OWNED) return OWNER_NONSPINNABLE; if (owner & RWSEM_READER_OWNER) return OWNER_READER; return OWNER_WRITER; } static noinline enum owner_state rwsem_spin_on_owner(struct rw_semaphore *sem) { struct task_struct *tmp, *owner = READ_ONCE(sem->owner); enum owner_state state = rwsem_owner_state((unsigned long)owner); if (state != OWNER_WRITER) return state; rcu_read_lock(); for (;;) { if (atomic_long_read(&sem->count) & RWSEM_FLAG_HANDOFF) { state = OWNER_NONSPINNABLE; break; } tmp = READ_ONCE(sem->owner); if (tmp != owner) { state = rwsem_owner_state((unsigned long)tmp); break; } /* * Ensure we emit the owner->on_cpu, dereference _after_ * checking sem->owner still matches owner, if that fails, * owner might point to free()d memory, if it still matches, * the rcu_read_lock() ensures the memory stays valid. */ barrier(); if (need_resched() || !owner_on_cpu(owner)) { state = OWNER_NONSPINNABLE; break; } cpu_relax(); } rcu_read_unlock(); return state; }