Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp4152232yba; Wed, 17 Apr 2019 05:49:21 -0700 (PDT) X-Google-Smtp-Source: APXvYqyr+592HoxTMELjE7kuqE9VnHk1DGNZsHVnlah22wtvwC1VzaZNRgcXtS0Sjf72O0KM3l25 X-Received: by 2002:a17:902:e508:: with SMTP id ck8mr86935304plb.96.1555505360845; Wed, 17 Apr 2019 05:49:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555505360; cv=none; d=google.com; s=arc-20160816; b=y+L6LTTxppogbzl8P4vs+KScxzRVYazoZRsLVOksRJX/mzOkP2PLyFhSgzNUZf+Xnj 6CclJzo4vUdwMfEgBIebSIXxHo6XrTSWEmYlq7/NE6lq2q4xX0MVJh7cbreCZRkqDVS1 Ziiuctc5/j8z/irmbg8zbByNpJFWXm8jlOJSA/vqV9A0vauNEhqqA3ucgMnjZVV6jh/U U40EmhvIoQuvRYALW+2cV/T8kpHu02pd/2tTmDQG3T7HrzjA4WK3kbn+o0vP1/r50u3n mMLwYczhf+hWnUAljKpq9RcEIrXfs6l6sJbUQ2+y0djNYXTThBWRPYZEdsGx77ge3NDx jNwQ== 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=ysoZ3sVjvRWdTwbufj+Igr5ABXbNMvaHdrYelS78dMw=; b=AKqsWTGal/DBP2r8NaIA81o06RWL2L8Dyzd0HJmMuJXEbnURCJstxnOM7s6x/kIhnV sL7Qj1rkDK8SGUpbVp8Iz1WGsMycCTZUKDVtHb9dbgeb1YLwR156paeg7B7PDwpC+4z3 i0zkVMCOaDbU3VeiMoPwUFhIbAQjEKVawblBTsxhUxnwfQHOEOcFdFfR3oJY4/GGmH4A W800eXE0KLKUgiGUsUup4gzk6v45A5uOFicJP/kUGyz+9QmT/kRswL/BYwj6hYQXRj9P 0S0mv00zTrBimxtk+LpfnO8MRgXu+92fni0Y8bxkgSh/0Vg24hjDUavvxQRiv9mQZ4Do IOMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b="GF/Y3OPY"; 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 l71si33878059pge.428.2019.04.17.05.49.05; Wed, 17 Apr 2019 05:49:20 -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="GF/Y3OPY"; 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 S1731960AbfDQMsK (ORCPT + 99 others); Wed, 17 Apr 2019 08:48:10 -0400 Received: from merlin.infradead.org ([205.233.59.134]:57002 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726800AbfDQMsJ (ORCPT ); Wed, 17 Apr 2019 08:48:09 -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=ysoZ3sVjvRWdTwbufj+Igr5ABXbNMvaHdrYelS78dMw=; b=GF/Y3OPYHRae/gJhvwUTeKDZT LCSjOBahVsNjRJA9vMJt4qZaLAgyKtUygjp5STuyxPOmWF+qhuRJHDDvRnV2M5FAaYIAvwsWcs9Ds V2HXsYkZjqVAry/wnmj2dZRO1DQI+c6KqREcuDG/5vYaFWZDomcXYsNGgzDIZ1/kJVV6uMIYDopav wMIrNjnEaS59fOQ3xKO0n/tFvNI0PTPfkA/6D57funtaVgZ4S5ihwNfkjn+7epPoW1Re/i2AsFjRf 2Uo2Zz50t5FiINOCFi029b9VMgsSwqMJMX4AcO2lI4rCW0CYeMhmLyKftbA1xgLM+hp5FNZ9dwDyM WCPoelXSw==; 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 1hGjyr-0002Mf-7P; Wed, 17 Apr 2019 12:48:01 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id C72D729B5292A; Wed, 17 Apr 2019 14:47:59 +0200 (CEST) Date: Wed, 17 Apr 2019 14:47:59 +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: <20190417124759.GT14281@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) Hmm, we should not spin on OWNER_NULL. Or at least not mixed in with the patch that changes the shape of all this. That should go in the RT thingy patch, which comes after this. > 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; > }