Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp6884107ybe; Wed, 18 Sep 2019 10:35:19 -0700 (PDT) X-Google-Smtp-Source: APXvYqwXNWf9xyqLkZ1TffyhO5K/ttb3xmx14tF0EfDe0ivRbxUBOU5mbIfWcR56pggAIBSo4Ioz X-Received: by 2002:a17:906:1394:: with SMTP id f20mr10987093ejc.274.1568828119563; Wed, 18 Sep 2019 10:35:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568828119; cv=none; d=google.com; s=arc-20160816; b=YLXqq3PLTBU2m1SuIBngtGND/uzOkZxpEX1vZy7aT+LCirWYtcLLqTHWLm8I+wYW4T CiBsLW36JxFAlPtHF0MxXErrg0jbtSnCVyadOWRcHmX8X7nTNSwDPFl4E2XzXoEAUzqI eQKmwQBmG/5KxalIMIkQr8Ia0aoXjYc3Ssq7ALE62ak/HkBrnWzbsGda8vL0P+6DvE05 9lplenYUEyn6QG4HjS8zWN51LQ791x62f+2XJdWaiDqQn5rjH6yIk/IsOdX7bIkrO6dJ Tl0Tp22E/vBENk/HnKnX5Ouqg0iqa7eFFMsjg1dn988XA1qG5jZN8X57i+q73/LdaING pfig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:content-transfer-encoding :content-id:mime-version:subject:cc:to:references:in-reply-to:from :organization; bh=U1iJtr7ZUYrMmK8Bb4vQyUsFV2mIYGI4D8JcBCS8zmQ=; b=uwnyYsejKXMv9CfGRTJo2tGHrJhdXw6LJ/zQQOINuVqqX7OaB0HIcCCrOP1oF5DwMV YWT2n6cn2wo7FCDXJNMaWN890AbIyvH68BDWuuv/fcLW9rdBj2Y+Y6E61oHZGn+rlIQW XUf+anvO4RSz5nKBvTmrL/eDkB3OdQq4Z+zwmUQ25rXiJr+mwZoOnuIVLMDsv1+8Z4gq C8OKvLxvBd/0sSYPwgUEB0BKr1wfGYnHSxC1yj7znJF8MYhOliKrMbf1xQukszvL8jcg nuUXIaOTg6X/5vMn/p1hrcfQ6LaLZ3dC2cPv6e80OJ6Qi+dzVwTdjqtiFdEADzIiS4od pS6g== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o25si3242291ejc.361.2019.09.18.10.34.54; Wed, 18 Sep 2019 10:35:19 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731732AbfIRPnF convert rfc822-to-8bit (ORCPT + 99 others); Wed, 18 Sep 2019 11:43:05 -0400 Received: from mx1.redhat.com ([209.132.183.28]:41462 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726558AbfIRPnE (ORCPT ); Wed, 18 Sep 2019 11:43:04 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1F509300DA78; Wed, 18 Sep 2019 15:43:04 +0000 (UTC) Received: from warthog.procyon.org.uk (ovpn-125-72.rdu2.redhat.com [10.10.125.72]) by smtp.corp.redhat.com (Postfix) with ESMTP id 579C160C5D; Wed, 18 Sep 2019 15:43:01 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 From: David Howells In-Reply-To: <20190917170716.ud457wladfhhjd6h@willie-the-truck> References: <20190917170716.ud457wladfhhjd6h@willie-the-truck> <20190915145905.hd5xkc7uzulqhtzr@willie-the-truck> <25289.1568379639@warthog.procyon.org.uk> <28447.1568728295@warthog.procyon.org.uk> To: Will Deacon Cc: dhowells@redhat.com, paulmck@linux.ibm.com, mark.rutland@arm.com, torvalds@linuxfoundation.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, peterz@infradead.org Subject: Do we need to correct barriering in circular-buffers.rst? MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <15227.1568821380.1@warthog.procyon.org.uk> Content-Transfer-Encoding: 8BIT Date: Wed, 18 Sep 2019 16:43:00 +0100 Message-ID: <15228.1568821380@warthog.procyon.org.uk> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.45]); Wed, 18 Sep 2019 15:43:04 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Will Deacon wrote: > If I'm understanding your code correctly (big 'if'), then you have things > like this in pipe_read(): > > > unsigned int head = READ_ONCE(pipe->head); > unsigned int tail = pipe->tail; > unsigned int mask = pipe->buffers - 1; > > if (tail != head) { > struct pipe_buffer *buf = &pipe->bufs[tail & mask]; > > [...] > > written = copy_page_to_iter(buf->page, buf->offset, chars, to); > > > where you want to make sure you don't read from 'buf->page' until after > you've read the updated head index. Is that right? If so, then READ_ONCE() > will not give you that guarantee on architectures such as Power and Arm, > because the 'if (tail != head)' branch can be speculated and the buffer > can be read before we've got around to looking at the head index. > > So I reckon you need smp_load_acquire() in this case. pipe_write() might be > ok with the control dependency because CPUs don't tend to make speculative > writes visible, but I didn't check it carefully and the compiler can do > crazy stuff in this area, so I'd be inclined to use smp_load_acquire() here > too unless you really need the last ounce of performance. Yeah, I probably do. Documentation/core-api/circular-buffers.rst might be wrong, then, I think. It mandates using smp_store_release() to update buffer->head in the producer and buffer->tail in the consumer - but these need pairing with memory barriers used when reading buffer->head and buffer->tail on the other side. Currently, for the producer we have: spin_lock(&producer_lock); unsigned long head = buffer->head; /* The spin_unlock() and next spin_lock() provide needed ordering. */ unsigned long tail = READ_ONCE(buffer->tail); if (CIRC_SPACE(head, tail, buffer->size) >= 1) { /* insert one item into the buffer */ struct item *item = buffer[head]; produce_item(item); smp_store_release(buffer->head, (head + 1) & (buffer->size - 1)); /* wake_up() will make sure that the head is committed before * waking anyone up */ wake_up(consumer); } spin_unlock(&producer_lock); I think the ordering comment about spin_unlock and spin_lock is wrong. There's no requirement to have a spinlock on either side - and in any case, both sides could be inside their respective locked sections when accessing the buffer. The READ_ONCE() would theoretically provide the smp_read_barrier_depends() to pair with the smp_store_release() in the consumer. Maybe I should change this to: spin_lock(&producer_lock); /* Barrier paired with consumer-side store-release on tail */ unsigned long tail = smp_load_acquire(buffer->tail); unsigned long head = buffer->head; if (CIRC_SPACE(head, tail, buffer->size) >= 1) { /* insert one item into the buffer */ struct item *item = buffer[head]; produce_item(item); smp_store_release(buffer->head, (head + 1) & (buffer->size - 1)); /* wake_up() will make sure that the head is committed before * waking anyone up */ wake_up(consumer); } spin_unlock(&producer_lock); The consumer is currently: spin_lock(&consumer_lock); /* Read index before reading contents at that index. */ unsigned long head = smp_load_acquire(buffer->head); unsigned long tail = buffer->tail; if (CIRC_CNT(head, tail, buffer->size) >= 1) { /* extract one item from the buffer */ struct item *item = buffer[tail]; consume_item(item); /* Finish reading descriptor before incrementing tail. */ smp_store_release(buffer->tail, (tail + 1) & (buffer->size - 1)); } spin_unlock(&consumer_lock); which I think is okay. And yes, I note that this does use smp_load_acquire(buffer->head) in the consumer - which I should also be doing. David