Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp799115ybn; Wed, 25 Sep 2019 07:54:43 -0700 (PDT) X-Google-Smtp-Source: APXvYqySTran0JahtKlD4Qusg9Xtyxfm4SXi8yLUUZkUtap4S05YHwZGFO1jxfdJpkPxkAyDqFB1 X-Received: by 2002:a05:6402:16d5:: with SMTP id r21mr3346027edx.71.1569423283264; Wed, 25 Sep 2019 07:54:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569423283; cv=none; d=google.com; s=arc-20160816; b=GWpRiwC0un2SRGkTGxZYi2NesPrTiUK0+O3K2tkyxw4wDSfxSZIdI1Nz+DRP3VYRoL 25Hejj5Efhg+osLJHLGGtmqDqOEDcTRE1pv3Arpv7unP308wUNpMSqxL+uOZGTnt5cA7 Yw4YiCZoBhqJ4ivbRp1j7x7LThK+YP1Gu1CSoBszKOi04qedQnFX5gUegQoFp5PqPHbn 4vg8cKI5/iHXvqJeZ3j7h0uuZrYYOX975GfXVELAi4AyrP/ZPzWV6vFeoBnRqjHRBN9M U9iADEYF1zQ+89KhxQyIZ04O0VtGxSAAknb4a0370tVkxnwgnnOh/KYwTOEWJrT/Ml84 iHMA== 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=V2TL1OkDh2oEnzgGPwW5rtpt6oXK+wm5wbqIRlf2l1M=; b=mTL0M3I2MqwOWkaLRVSLH7MjvKXZ0b7LmcFoIc8wdxLOYJ41Z2dTbIm0m81goHh3z2 Ia0WeIEnmzA3gvHsqX3dSMirP/LxH0ZSCoJOFE3Lzr8HFbppNAiO9jRJ7qhKQDR8iWSg CxrcVkP8bB2FGppAeL4KPZDX7To6M+f0BLZKZX6ch2FjfRuyNEXJBPmobyIYx3PGQao0 Xfvg74VJ6JQFQDkE47FpmNbex9NBo1xzR/LlyJCBeLiMaGLsIWh9/+zMg+GEKigGpKeB KniYNGUvUGMiJsbRXPdoJ1Iva5V+qroFWkIYT4tfFNDLhpZAnOC1LmApjN7AE0BRXkGZ 9SCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=fBUGYc37; 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 oz25si2690844ejb.175.2019.09.25.07.54.18; Wed, 25 Sep 2019 07:54:43 -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=fBUGYc37; 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 S2440085AbfIWOti (ORCPT + 99 others); Mon, 23 Sep 2019 10:49:38 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:42270 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2438376AbfIWOti (ORCPT ); Mon, 23 Sep 2019 10:49:38 -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=V2TL1OkDh2oEnzgGPwW5rtpt6oXK+wm5wbqIRlf2l1M=; b=fBUGYc37z1z4Dwgz8h7k1qSR0 U+bUQeuZqrp4+f7NFKghpdFDI+GyU72GyNbolbLg/ujP/Zs5Pb38MtGSQUn9FspPZdAPUKiG7HbsM /R5VkMEPorraJItOpntT35WuLltovyfHS8qPGMk4HdvsDOe9t5Ry24DMm3abqd64L/3xopQQNfAY1 +Ey9BBsSbEdCESaZZ0cAUxRg8GLitzEereuD83WHln6ObRBTesfn8VcYW39tCygANQCIHitaF0YyY G7V5DFn7EpxWIVKsSUYsa32XkQjBhj7pWmLVZ724qlmn2gi5FFqGAxkrR2s2Q7vuOls/LapBjd+Ys anbHzSNZw==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by bombadil.infradead.org with esmtpsa (Exim 4.92.2 #3 (Red Hat Linux)) id 1iCPef-0001SS-Ly; Mon, 23 Sep 2019 14:49:33 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 2DE9F303DFD; Mon, 23 Sep 2019 16:48:46 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 547962B08BBAE; Mon, 23 Sep 2019 16:49:31 +0200 (CEST) Date: Mon, 23 Sep 2019 16:49:31 +0200 From: Peter Zijlstra To: David Howells Cc: Linus Torvalds , Will Deacon , "Paul E. McKenney" , Mark Rutland , Linux List Kernel Mailing , linux-fsdevel Subject: Re: Do we need to correct barriering in circular-buffers.rst? Message-ID: <20190923144931.GC2369@hirez.programming.kicks-ass.net> References: <20190915145905.hd5xkc7uzulqhtzr@willie-the-truck> <25289.1568379639@warthog.procyon.org.uk> <28447.1568728295@warthog.procyon.org.uk> <20190917170716.ud457wladfhhjd6h@willie-the-truck> <15228.1568821380@warthog.procyon.org.uk> <5385.1568901546@warthog.procyon.org.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <5385.1568901546@warthog.procyon.org.uk> 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, Sep 19, 2019 at 02:59:06PM +0100, David Howells wrote: > But I don't agree with this. You're missing half the barriers. There should > be *four* barriers. The document mandates only 3 barriers, and uses > READ_ONCE() where the fourth should be, i.e.: > > thread #1 thread #2 > > smp_load_acquire(head) > ... read data from queue .. > smp_store_release(tail) > > READ_ONCE(tail) > ... add data to queue .. > smp_store_release(head) > Notably your READ_ONCE() pseudo code is lacking a conditional; kernel/events/ring_buffer.c writes it like so: * kernel user * * if (LOAD ->data_tail) { LOAD ->data_head * (A) smp_rmb() (C) * STORE $data LOAD $data * smp_wmb() (B) smp_mb() (D) * STORE ->data_head STORE ->data_tail * } * * Where A pairs with D, and B pairs with C. * * In our case (A) is a control dependency that separates the load of * the ->data_tail and the stores of $data. In case ->data_tail * indicates there is no room in the buffer to store $data we do not. * * D needs to be a full barrier since it separates the data READ * from the tail WRITE. * * For B a WMB is sufficient since it separates two WRITEs, and for C * an RMB is sufficient since it separates two READs. Where 'kernel' is the producer and 'user' is the consumer. This was written before load-acquire and store-release came about (I _think_), and I've so far resisted updating B to store-release because smp_wmb() is actually cheaper than store-release on a number of architectures (notably ARM). C ought to be a load-aquire, and D really should be a store-release, but I don't think the perf userspace has that (or uses C11).