Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp670606yba; Thu, 18 Apr 2019 07:43:48 -0700 (PDT) X-Google-Smtp-Source: APXvYqxWt2j/zcDRPqiM4evP3ylYKb2UfwV6n3MZWbVLp93d+n3M4cSwNfulq94pDoRM0XYOHNUi X-Received: by 2002:a17:902:1123:: with SMTP id d32mr75970894pla.213.1555598627939; Thu, 18 Apr 2019 07:43:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555598627; cv=none; d=google.com; s=arc-20160816; b=IP+g8fDU5LAZTKirnwCXygkpM/bA5YJySQl+C8UBItup3HpOiQj1tZh7vo8Yld1Grd 68nuLnHlYXx4bEsn1jM1YICU9aOPOf51k71xCMjxpudz9G8BYZxpq67/tnDDjehtXjdj z0xHghkCO8Gf7lDhHhbhfxqNDs9AGcZ2JpbrGWjwhHUKYsJYamzsg2C9m9wZTUt5PKcH nI+opXEnESx4tIJPeyUgVWlZIoPbPYKsSNIu1iXm+t6IL8QJTuNjfmhq025ucY/LhbVw QvNXk4Oz3R9jHAcUXM77NCfIgAQNr7y3B4b+/1WX0yyR+et67FUR1rU71UB399x4lVCY 01rA== 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=fLXzu6aQ2r9iLPnXbOeIFwwObsTLPBzstUXrcF7xVLU=; b=kx63S8pBWVI+gr719lDllaxYWdXua5C8FiozjMr+SgR7Ikqr5dOPOPykgRilB+kJ26 ai7W4WuHx3MnC5TI9AbxvI1C3I5l+rTtv9MazXRZDSyGorx0TFWJC/qRjd5bWPlCiIxK Y8Q4aW6wRYcOrz/tTmoqkGxArWep1L7/wcvXQypx1X7qKAznuVYd7yVO9G/PAIOVBwV5 npnBo5gLWsJA52+Pbe6nQcKpLZ9mJQWZfIQmLsW3SBFVimPX4Ai4dH9IrAUZM1GQy/+v rg0oeZ/kB/WSGhqgAb2pN7TBksAg6pcYbsGkGiHHrpeIG2dDCm7C74rAbDy3LnNBmJtI VjFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=DcwjK6XQ; 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 l11si2192970pgg.554.2019.04.18.07.43.30; Thu, 18 Apr 2019 07:43:47 -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=bombadil.20170209 header.b=DcwjK6XQ; 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 S2389015AbfDROkn (ORCPT + 99 others); Thu, 18 Apr 2019 10:40:43 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:57292 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731317AbfDROkn (ORCPT ); Thu, 18 Apr 2019 10:40:43 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.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=fLXzu6aQ2r9iLPnXbOeIFwwObsTLPBzstUXrcF7xVLU=; b=DcwjK6XQIr02F1B9KQ1eUFZq+ pPUB2jesFAYjJ7cCjcZLhrMfXU88hOsKQFWoRLjDN75AVg1FlYIIXQCY6xp3cDSIZ79T2nXCaM6iz CwSBcqsoa3vUgJDe1h0oKsjudJg94Hm0GIEwglpI4HhYrHTfcmrrU9DHCHTQrz9BpRKbOeeGNNIEK bMqTx01FsPtsxcIwi6KYTxkliaSdLq72hw5TaxFmNZO4GjTU/MIMZo6s14nlCzSENmMeYIo5sbfYg Z2zCqiUiPSqAvyf8BqzFUeoyTOZMDibeDT4QWBI0cY8rudalACVpULL1NfSyktAjSE4ziBjqmy67j XuydThd6g==; 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.90_1 #2 (Red Hat Linux)) id 1hH8DO-0003p8-JY; Thu, 18 Apr 2019 14:40:38 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 9A7BB29BB189E; Thu, 18 Apr 2019 16:40:36 +0200 (CEST) Date: Thu, 18 Apr 2019 16:40:36 +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 14/16] locking/rwsem: Guard against making count negative Message-ID: <20190418144036.GE12232@hirez.programming.kicks-ass.net> References: <20190413172259.2740-1-longman@redhat.com> <20190413172259.2740-15-longman@redhat.com> <20190418135151.GB12232@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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 Thu, Apr 18, 2019 at 10:08:28AM -0400, Waiman Long wrote: > On 04/18/2019 09:51 AM, Peter Zijlstra wrote: > > On Sat, Apr 13, 2019 at 01:22:57PM -0400, Waiman Long wrote: > >> inline void __down_read(struct rw_semaphore *sem) > >> { > >> + long count = atomic_long_fetch_add_acquire(RWSEM_READER_BIAS, > >> + &sem->count); > >> + > >> + if (unlikely(count & RWSEM_READ_FAILED_MASK)) { > >> + rwsem_down_read_failed(sem, count); > >> DEBUG_RWSEMS_WARN_ON(!is_rwsem_reader_owned(sem), sem); > >> } else { > >> rwsem_set_reader_owned(sem); > > *groan*, that is not provably correct. It is entirely possible to get > > enough fetch_add()s piled on top of one another to overflow regardless. > > > > Unlikely, yes, impossible, no. > > > > This makes me nervious as heck, I really don't want to ever have to > > debug something like that :-( > > The number of fetch_add() that can pile up is limited by the number of > CPUs available in the system. > Yes, if you have a 32k processor system that have all the CPUs trying > to acquire the same read-lock, we will have a problem. Having more CPUs than that is not impossible these days. > Or as Linus had said that if we could have tasks kept > preempted right after doing the fetch_add with newly scheduled tasks > doing the fetch_add at the same lock again, we could have overflow with > less CPUs. That. > How about disabling preemption before fetch_all and re-enable > it afterward to address the latter concern? Performance might be an issue, look at what preempt_disable() + preempt_enable() generate for ARM64 for example. That's not particularly pretty. > I have no solution for the first case, though. A cmpxchg() loop can fix this, but that again has performance implications like you mentioned a while back.