Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp336377yba; Thu, 18 Apr 2019 02:02:15 -0700 (PDT) X-Google-Smtp-Source: APXvYqybvIww1u/0hiB2IIldJGSFxJFiikYP+CjpsebJKt5oMD5c7R17vqNTz7kobh8QEtsxHtOk X-Received: by 2002:a17:902:e4:: with SMTP id a91mr75804757pla.2.1555578135031; Thu, 18 Apr 2019 02:02:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555578135; cv=none; d=google.com; s=arc-20160816; b=lcVpPFo4wUwavCG+IOdYWnRCnMs/dIF8A/FvuZak+5EooDzyzTtvoG1dlXDr1JzowI x5cSL3OXJdU7phqnPQXAftJb3gTvbmaIWyVY5TRSKisc2dOihYxjto2EdsmSbSQtDUSL hgaWEDq7DxmtHHb97vzbSlUtU8g6SVDJj6qgQ7kWBI6YSw5arZJbNstHS+YZ4d3aaYe5 oEmz0CKuVaO38a6578vgiJrS1Hh3KxJqeRWZ4SHUGi71WTa2HEyj6x/LKEYKlcWnf6Ox U56EI/q9aTeCVSh++fBPWZWHwUUsV5QGgvjmGM/V9OcSy1RFco8nKKzbpNF9WiBkNFU/ pD6A== 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=yvS0k5aM27hlPF/X/NetOX/zP2CDvscIx40HsGn0V+g=; b=Xzdv4JBTb4sSQHMcOvL4Gyl4oAHvSGNJ1mrOSjVKku6yAy8xxpnwBpWj7RWfbkQS/z JvvKGIL58eLie//r/GHMCsGMd0HCnsTWdgzZkPbJNGXzR0CtVzmjdTqpQHbWA2N5ijLf +utFqE0qBgJ+/iARV8j/vGhm99rgtA9+NRMrMgEK49RahGpE7uwix5xpLLhrKlsezBkf A80YMTl1ayeCZTK+W3hUx5jPsD0XRFqo8V0SkD8u8T2buut4enBKui5IUAqJFI5ZJj1u YtBr5XQ91QYm0GENhG/2dF2q4IAzCwlCEcJzR44gO5o0+zdDbHHmYOtm/Gy/JnJW4MS3 9Ujw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=TE15QQH+; 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 q81si1697478pfa.207.2019.04.18.02.01.59; Thu, 18 Apr 2019 02:02:15 -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=TE15QQH+; 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 S2388333AbfDRJA6 (ORCPT + 99 others); Thu, 18 Apr 2019 05:00:58 -0400 Received: from merlin.infradead.org ([205.233.59.134]:37518 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388195AbfDRJA6 (ORCPT ); Thu, 18 Apr 2019 05:00:58 -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=yvS0k5aM27hlPF/X/NetOX/zP2CDvscIx40HsGn0V+g=; b=TE15QQH+J0bGEX9gYX0sejNVR C0dXnrNLV38tAzN+2yfZMAGyjEJhVl7PZPp6qzSGJOq+Hi8CE7UKty8nnYx4gM2xe25WwbYmnHG6I WYYbxfEpbw76RIgE0DjTXwqA10Tf4qNM0MF7HjwXq1vTiIHatLhO/+cHoyWpa4QXQAvFXcaxuyBkW XVu+CXbaCQv4af7lx+fMBJgF9cEewa8eptdeVhy98Extp8Nuyveo/vpi0YAWlIqAzfdXu702VHW8c I7tqOBWc/hZcVpTapNvp6dEqQTnTeFLlGup5QYrB50G3IdUxJGZmpOez8/cRKoT0Y46YGkABqSFNP bNd1TJj0g==; 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 1hH2uX-0000kz-1R; Thu, 18 Apr 2019 09:00:49 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id C509029B98541; Thu, 18 Apr 2019 11:00:47 +0200 (CEST) Date: Thu, 18 Apr 2019 11:00:47 +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 11/16] locking/rwsem: Enable readers spinning on writer Message-ID: <20190418090047.GX12232@hirez.programming.kicks-ass.net> References: <20190413172259.2740-1-longman@redhat.com> <20190413172259.2740-12-longman@redhat.com> <20190417135841.GI4038@hirez.programming.kicks-ass.net> <8683197e-4eec-b89b-df44-3b61088944af@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <8683197e-4eec-b89b-df44-3b61088944af@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 Wed, Apr 17, 2019 at 01:45:10PM -0400, Waiman Long wrote: > On 04/17/2019 09:58 AM, Peter Zijlstra wrote: > > On Sat, Apr 13, 2019 at 01:22:54PM -0400, Waiman Long wrote: > >> +/* > >> + * Try to acquire read lock before the reader is put on wait queue. > >> + * Lock acquisition isn't allowed if the rwsem is locked or a writer handoff > >> + * is ongoing. > >> + */ > >> +static inline bool rwsem_try_read_lock_unqueued(struct rw_semaphore *sem) > >> +{ > >> + long count = atomic_long_read(&sem->count); > >> + > >> + if (RWSEM_COUNT_WLOCKED_OR_HANDOFF(count)) > >> + return false; > >> + > >> + count = atomic_long_fetch_add_acquire(RWSEM_READER_BIAS, &sem->count); > >> + if (!RWSEM_COUNT_WLOCKED_OR_HANDOFF(count)) { > >> + rwsem_set_reader_owned(sem); > >> + lockevent_inc(rwsem_opt_rlock); > >> + return true; > >> + } > >> + > >> + /* Back out the change */ > >> + atomic_long_add(-RWSEM_READER_BIAS, &sem->count); > >> + return false; > >> +} > > Doesn't a cmpxchg 'loop' make more sense here? > > Not really. A cmpxchg loop will have one more correctible failure mode - > a new reader acquire the lock or a reader owner does an unlock. Failures > caused by the setting of the handoff bit or writer acquiring the lock > are the same for both cases. I don't see any advantage in using cmpxchg > loop. It depends on how many failures vs successes you have. I was expecting failure to be the most common case, and then you go from 2 atomics to 1.