Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp4018286ybx; Mon, 4 Nov 2019 06:32:51 -0800 (PST) X-Google-Smtp-Source: APXvYqyOe5Xrdtrx0rn3mhXov+8yo6Ogo+hjzyHqM9SSCSqRxmf064idnr9W2NYOb24BG+yzezEd X-Received: by 2002:a17:906:a388:: with SMTP id k8mr21606998ejz.223.1572877971130; Mon, 04 Nov 2019 06:32:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1572877971; cv=none; d=google.com; s=arc-20160816; b=Z3/PvISX/MwLZYp6aPZb7N0h8BrQiQW9kHJVMeMt5gJN4noTsfgxVf4WolhY1sREzM VwGqFjWhUzku59jceIYHMhacBAmFcQ1PpJuGzQ935yBy0tSo5Eec+Up2gTW6HmGz3db1 P8N1c7aecPZyUMXOTN2jhzpbh2DVGjTthAgbuyAVGJuot945umfxdM5hE9CZ31tiJ/ug b9foVpbCL/ePt91o8q0KaL2IV6rp+OUIMz+pZLEWpPGZcbVOV1W/Tv06S+NRpCWQrDi8 ky8ofyF6lU1NqE1Ayp/1ZU6y20ptMiriltzfyWblqlmLSfFjF8Y8pheAgYHKV+lB8EiG p8nA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=sQU0jAVefneuTdQpIgbUF6GdSN4TcMOiQRxRVI16Vls=; b=vfct4qWqNi+yqwYhsDKpScKnnT7i1wUqj4uImvW8d2L97MuGSR9LfhyKrZAyU8gJCW vmpDzKlVXOGaK0FV0cRfqs2WpCcFbHyIAO70aV8d+m0BahbVeFum01OZyPRvWpcc4o4b iH3+Qfut6FsYin5wkSLIGCAAE557bexVPu5UE3uKg91KmX+K/8Qb0Rt2FLmPoEwh1kSt ZcI24duTnRCDhzm0lDIfeto4nzabfKH/994TPvG9wx8QiUp9psNFKVYkFn2iq3FsJPCF qo3oYOl6zGIJKAAX4eyXnlkGz5Hd9WvCkfr8qIjxCR7Znl3fnwjyAoyyVrBlj9D+QLiS CJ8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=FhPDq8Y8; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d6si8348463ede.119.2019.11.04.06.32.27; Mon, 04 Nov 2019 06:32:51 -0800 (PST) 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=@google.com header.s=20161025 header.b=FhPDq8Y8; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729084AbfKDO3P (ORCPT + 99 others); Mon, 4 Nov 2019 09:29:15 -0500 Received: from mail-vk1-f202.google.com ([209.85.221.202]:53243 "EHLO mail-vk1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729011AbfKDO3L (ORCPT ); Mon, 4 Nov 2019 09:29:11 -0500 Received: by mail-vk1-f202.google.com with SMTP id x1so7944504vkc.19 for ; Mon, 04 Nov 2019 06:29:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=sQU0jAVefneuTdQpIgbUF6GdSN4TcMOiQRxRVI16Vls=; b=FhPDq8Y8jgh3GaqH19Ri5l4th6SIgrfB5k6uYp6Qe1zCUALU5Q1h777JkPDXGoqtF+ rUVB4HT5dCMdMd/+svGvi1KkuFsbojwRCgfaJkdh2DPMBB53CrCa1TeF/tRilzh/U2Vv rC8CcHxh3PrEaKH9Qc4nO5dN6oULNuw2D+ebZe/2C78pc5pykjiO0Bg4leEG7yFIFNxI lT1Onq8IrfB98Hhn323WbkEF/3JHPJUZwZzMucC6nWXt4j1r7WLeMRYJuLt6zhN4vW9Q 0cZ7MGwIvE1QsZGodu2uWGo2OqoLsx5v5yIqO4x8fyNCQ7jT/uIEts0ZzNEU0DdB7OYH Ef0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=sQU0jAVefneuTdQpIgbUF6GdSN4TcMOiQRxRVI16Vls=; b=sI5ZUq5g+6e/HZTzJOv7L7FlQaqIwW6LlOkAgMvhF8myV9mVi9CjQ/yKP8W7G69nVf yfpyHhFpnBzDvzjJ2v1N914UBATCHe8u252PgLgDhI+PkQ+HWGPgxSQUnmI7MtPp88Vj BRMxHdC3QjbWSlyuW0DSUurb74k6EbsJl9oXpANYewmVLUkfTlhq/AJn4XtfV5drO7J6 r3057PKM5Ys48FafoazXITOAJRb3ygA3CYFmb1N8XM5lplh6nLkxEtcREWtzXOp4wjHq ZZgzDaBJ0FSFcyEttrLOPkDgsD78PGurXmwhQRgrKsrhw952muVSqPeiH2rS/lxefJn/ cIlA== X-Gm-Message-State: APjAAAVEX+1qwM3TavlWSiaMmjN7IIg1q4Y5D0JaOVkCjxMquTvePQuC Ck8PJT32utGQqkQpd9eIOrImxyOpNQ== X-Received: by 2002:a05:6122:2c7:: with SMTP id k7mr10982783vki.97.1572877749135; Mon, 04 Nov 2019 06:29:09 -0800 (PST) Date: Mon, 4 Nov 2019 15:27:42 +0100 In-Reply-To: <20191104142745.14722-1-elver@google.com> Message-Id: <20191104142745.14722-7-elver@google.com> Mime-Version: 1.0 References: <20191104142745.14722-1-elver@google.com> X-Mailer: git-send-email 2.24.0.rc1.363.gb1bccd3e3d-goog Subject: [PATCH v3 6/9] seqlock: Require WRITE_ONCE surrounding raw_seqcount_barrier From: Marco Elver To: elver@google.com Cc: akiyks@gmail.com, stern@rowland.harvard.edu, glider@google.com, parri.andrea@gmail.com, andreyknvl@google.com, luto@kernel.org, ard.biesheuvel@linaro.org, arnd@arndb.de, boqun.feng@gmail.com, bp@alien8.de, dja@axtens.net, dlustig@nvidia.com, dave.hansen@linux.intel.com, dhowells@redhat.com, dvyukov@google.com, hpa@zytor.com, mingo@redhat.com, j.alglave@ucl.ac.uk, joel@joelfernandes.org, corbet@lwn.net, jpoimboe@redhat.com, luc.maranget@inria.fr, mark.rutland@arm.com, npiggin@gmail.com, paulmck@kernel.org, peterz@infradead.org, tglx@linutronix.de, will@kernel.org, kasan-dev@googlegroups.com, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-efi@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, x86@kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch proposes to require marked atomic accesses surrounding raw_write_seqcount_barrier. We reason that otherwise there is no way to guarantee propagation nor atomicity of writes before/after the barrier [1]. For example, consider the compiler tears stores either before or after the barrier; in this case, readers may observe a partial value, and because readers are unaware that writes are going on (writes are not in a seq-writer critical section), will complete the seq-reader critical section while having observed some partial state. [1] https://lwn.net/Articles/793253/ This came up when designing and implementing KCSAN, because KCSAN would flag these accesses as data-races. After careful analysis, our reasoning as above led us to conclude that the best thing to do is to propose an amendment to the raw_seqcount_barrier usage. Signed-off-by: Marco Elver --- v3: * Add missing comment that was in preceding seqlock patch. --- include/linux/seqlock.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/include/linux/seqlock.h b/include/linux/seqlock.h index 61232bc223fd..f52c91be8939 100644 --- a/include/linux/seqlock.h +++ b/include/linux/seqlock.h @@ -265,6 +265,13 @@ static inline void raw_write_seqcount_end(seqcount_t *s) * usual consistency guarantee. It is one wmb cheaper, because we can * collapse the two back-to-back wmb()s. * + * Note that, writes surrounding the barrier should be declared atomic (e.g. + * via WRITE_ONCE): a) to ensure the writes become visible to other threads + * atomically, avoiding compiler optimizations; b) to document which writes are + * meant to propagate to the reader critical section. This is necessary because + * neither writes before and after the barrier are enclosed in a seq-writer + * critical section that would ensure readers are aware of ongoing writes. + * * seqcount_t seq; * bool X = true, Y = false; * @@ -284,11 +291,11 @@ static inline void raw_write_seqcount_end(seqcount_t *s) * * void write(void) * { - * Y = true; + * WRITE_ONCE(Y, true); * * raw_write_seqcount_barrier(seq); * - * X = false; + * WRITE_ONCE(X, false); * } */ static inline void raw_write_seqcount_barrier(seqcount_t *s) -- 2.24.0.rc1.363.gb1bccd3e3d-goog