Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp3208126ybn; Fri, 27 Sep 2019 02:52:00 -0700 (PDT) X-Google-Smtp-Source: APXvYqwT5CVpD+zOtSILoZ3TN0vnuK+YV3pRgtBrDjJiTDndNn8NVoFwK9R8z4SQPSygxJwqQ7tj X-Received: by 2002:a17:906:7687:: with SMTP id o7mr7025857ejm.213.1569577920758; Fri, 27 Sep 2019 02:52:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569577920; cv=none; d=google.com; s=arc-20160816; b=DkQVdS5j5W+P1ft+LljKZU1dQrXPJ8EB2u7GWiarnZXpLUzRFhHNpfP90DvJGgyNXZ 7yJOwUbbcEWILVRqLGEgc5GmRUTUKXiq3ueIZSX7/LYrQzY4xqmBFdCP/4kShuQ0z5AT hV1dm5Y3XetafAKP8qgxyjmWo3NastQRD8YaY2N1t0ouNKfXwJpv97Y5h1uWR+2lYUiC LlJ8LDVzLS6uwGXrwZV3sB84wcwH81H6rmSxPPK4thL3OYRV42PcP5uC+arJFNIJmBKS j2GRtgrsI+IEi6I4mtG5PnmqI7PewxroxGCc7SaGFJObhQsD7gzMJmXFxQANigNNdCiW loDA== 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=5UT/s5ekZUcI22BnYTDuzvND6A8rlWJDoVeIOvbjDvw=; b=PaIf+Z1+/ICCkzFgU4+m/sbJMI9rRtxzcC0o3xFBqMS0N/iFOKD6Y4KXpOD3fEzqwP ZzLCOcxQ02UvH63iFIzNkIKfdwBU5VvNteFGzGYUteiM7LCuFMOVC6yhYW885f7fB2tH bBZHtFqkLOKliLvx6lwJOgvSOGH5tfdbSWnqlKYCKvGjBqGPgOoWGJjVBcbIlQAIkQb6 3T2RNWvgDOTGeGowQuljqG8LlsmBZNmkHhQO7ToJWVaGBJ7jAC7/mlwZWitwsBlaUaXC vuyg1R9t9X4S/s4/d6BBaOWyyxjikNwiv1CFgZLvhCxgbGe51vv7QHJ4jrVXrGrYAkEw Zj0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=XPXusTm8; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x19si2422432eju.15.2019.09.27.02.51.36; Fri, 27 Sep 2019 02:52:00 -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=pass header.i=@gmail.com header.s=20161025 header.b=XPXusTm8; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726540AbfI0JvS (ORCPT + 99 others); Fri, 27 Sep 2019 05:51:18 -0400 Received: from mail-wr1-f66.google.com ([209.85.221.66]:33782 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726251AbfI0JvS (ORCPT ); Fri, 27 Sep 2019 05:51:18 -0400 Received: by mail-wr1-f66.google.com with SMTP id b9so2012133wrs.0; Fri, 27 Sep 2019 02:51:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=5UT/s5ekZUcI22BnYTDuzvND6A8rlWJDoVeIOvbjDvw=; b=XPXusTm85TcNc9mu9fufA2enGUz2JdExJ09NQmjHesOAdgAdhWFtyXegLLJMSujYCq 3fPO1n5J5F9lM1c8Pe/lgk1Wru1BpRPjpFP2OztIgoZYyKGaOS1sgyGhDtsHhJrxaSiV JbJvn2awcubv/FRjSyge5MhK0FWTseR+ibqvYfAxzRA3CogQZ9+ZYz7ScwgsQ4Dga+RP 2NzYQUo8o78zTOg7Vl9GNe1dQ2zGorPAG9wxDVRGZpXSgSWZvHwy9BxWl6ESJJlBayH9 YYW+rPvQgLiV+f2lOHV3tiN8/QcHE8jjEmsr4Y2KYBwKlSqxc0EZOzrf6tFtKA90paJE xaqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=5UT/s5ekZUcI22BnYTDuzvND6A8rlWJDoVeIOvbjDvw=; b=K74PiWfl7U1mOAjz6bt96cHD4jR8GM/vWpZwLJm9eCJAr02Jtd3LlPBgkqONK4971S 7T3ArVhCfHLOnLcE6YjbmQ9C+hIgzTBjynanFmiELk6q0ZJVRdohEitajnpvtpi9DpI2 Ai6KD316xtLFemHBfAKnjWCNkclT7uztMoWvyjDwpODfOW0aGoBlmtdUlDh5pzZq0JEK FohIuZVdhNCZv4/JMsNxzjhkVZdzSuEo+9xy/8xBPZEMvVmS4jZGw5WqrlTHdQQO9P2L 719eqdl8fHWOPNyzjpAztFFs8uJOtk4h+t9MoYUydbgU5tHHE9uur65FtHmKmdtP5ZtR jpZg== X-Gm-Message-State: APjAAAUdTQ/+6+XiACqiwrocQcUc9ZANrupmnCg6Syr7MAgKNo/KsZP7 pBkvX7lm8LIJH9kqV89/w0SRmhe2+dKi11Ia X-Received: by 2002:a5d:55d0:: with SMTP id i16mr2178637wrw.108.1569577874943; Fri, 27 Sep 2019 02:51:14 -0700 (PDT) Received: from andrea (userh626.uk.uudial.com. [194.69.102.253]) by smtp.gmail.com with ESMTPSA id c18sm2892255wrn.45.2019.09.27.02.51.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2019 02:51:14 -0700 (PDT) Date: Fri, 27 Sep 2019 11:51:07 +0200 From: Andrea Parri To: Peter Zijlstra Cc: David Howells , 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: <20190927095107.GA13098@andrea> 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> <20190923144931.GC2369@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190923144931.GC2369@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 Mon, Sep 23, 2019 at 04:49:31PM +0200, Peter Zijlstra wrote: > 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. To elaborate on this, dependencies are tricky... ;-) For the record, the LKMM doesn't currently model "order" derived from control dependencies to a _plain_ access (even if the plain access is a write): in particular, the following is racy (as far as the current LKMM is concerned): C rb { } P0(int *tail, int *data, int *head) { if (READ_ONCE(*tail)) { *data = 1; smp_wmb(); WRITE_ONCE(*head, 1); } } P1(int *tail, int *data, int *head) { int r0; int r1; r0 = READ_ONCE(*head); smp_rmb(); r1 = *data; smp_mb(); WRITE_ONCE(*tail, 1); } Replacing the plain "*data = 1" with "WRITE_ONCE(*data, 1)" (or doing s/READ_ONCE(*tail)/smp_load_acquire(tail)) suffices to avoid the race. Maybe I'm short of imagination this morning... but I can't currently see how the compiler could "break" the above scenario. I also didn't spend much time thinking about it. memory-barriers.txt has a section "CONTROL DEPENDENCIES" dedicated to "alerting developers using control dependencies for ordering". That's quite a long section (and probably still incomplete); the last paragraph summarizes: ;-) (*) Compilers do not understand control dependencies. It is therefore your job to ensure that they do not break your code. Andrea > * > * 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).