Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp4147279yba; Wed, 17 Apr 2019 05:42:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqw7ZItpOB2ukAoKGd33y7S9btakN12xE/7s2tGPgeQn9mgjHf9UsulECAPtBM1WMMVkDSHL X-Received: by 2002:a62:62c3:: with SMTP id w186mr56992816pfb.73.1555504962265; Wed, 17 Apr 2019 05:42:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555504962; cv=none; d=google.com; s=arc-20160816; b=yAsZOqNUcbyaofpPO+DzhR32hqZe1mFNT0/b0dJkYfv1vkXLtjaQ93bw3FUGrm8UC1 HzfVLj6042u83wFTO1FjBYfxhMq/ktJ1T5dEYoWD/LIyHGe3Z4BPZPH5b6FGdrgSAPBZ MVbiXkHhKO4jIQodVEUiVoJBxsfJQYCWuWUATxNTP1enyVsCRKHA/gEtf33PXhgs5quK LUBW2qHEUvuGnFg+SSMN64M7M212+lp3BE67ISbXIKdG90ohsoGwgqgflNrRoPT5n+JD tG91KiEPdBBiyiFvJ0k1VjMJtMT76X/kYLiXb1nRRjXZ37HSsA8mDCP78IOm9VBrbSLf hiTw== 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=Scf1hgxEroll7UaToVD0vun2XTKpeoNuRd68nAKetg0=; b=F1+MDFJfB/7TvQd+iSll+xmuB53Cn4zuSFZThoYlmvI3URiMbM/w1NvR2AOD0/d3/3 j9q0qcqzPNEGRUA5EOoGYtzT7K7M5A5iJY6Eee9Z8n8pcsEIcZlvKX4vP27yWDi//j6b fesRdLJiSOzKQzbVZE9NFZ5BvZvHvvI+/V8OmhSyAr5sbieCu5cxkwnDeDZoa92HHixz AS/1+RxDPzs+DFQqxj5W7UjP2Y3hvsIIfxUxRijI7PNABK7sXHPYmWho/p2F+Yr/cQ/Y 4KChjHkOTF+r3mPYh81jPJPpJmTqukuduplKfAvAdHh43tG0e7C5nfubDPgJe6hX/Am4 d38A== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=EbwD63oY; 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 y16si49551404plr.431.2019.04.17.05.42.26; Wed, 17 Apr 2019 05:42:42 -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=EbwD63oY; 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 S1732051AbfDQMlZ (ORCPT + 99 others); Wed, 17 Apr 2019 08:41:25 -0400 Received: from merlin.infradead.org ([205.233.59.134]:56960 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731116AbfDQMlZ (ORCPT ); Wed, 17 Apr 2019 08:41:25 -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=Scf1hgxEroll7UaToVD0vun2XTKpeoNuRd68nAKetg0=; b=EbwD63oYaJpeM/Ji2bEKEq43j jpQCqgGx4qjhgDWHwG1Msf0DyYPHLqOeuYWQjOrKd2JdzjHaan6kaqjXxW14vrMdLdXg7rPX7LwRg aTqI7w5x7kM4yFs+jc9VfqQoMJyRd99/cki85FQnZFNgej5CO0WA5xe5p29m0XTiUPc3UEuFho/Vt aEBFwZOq6ougeaAQh5NKQPCoHOprv0TDKeuM67ETY5ZYrvGErpzEmZ1Bq6XyOHt37o+79+PyKc+bD b3mT5pn/J77vI4sgt7D6p0F9BmzTEftXGj09tQ/V8T+sAYJynWeKYUw7Vc7JkPDBQL85mwXTl0+mj WgvrNjOPw==; 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 1hGjs8-0002IG-34; Wed, 17 Apr 2019 12:41:04 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 2E64029B87EBC; Wed, 17 Apr 2019 14:41:01 +0200 (CEST) Date: Wed, 17 Apr 2019 14:41:01 +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: <20190417124101.GE4038@hirez.programming.kicks-ass.net> References: <20190413172259.2740-1-longman@redhat.com> <20190413172259.2740-9-longman@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190413172259.2740-9-longman@redhat.com> 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 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; rcu_read_lock(); for (;;) { state = rwsem_owner_state((unsigned long)owner); if (!(state & OWNER_SPINNABLE)) break; 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; }