From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4C489C43465 for ; Fri, 18 Sep 2020 02:36:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0F1FF23770 for ; Fri, 18 Sep 2020 02:36:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600396610; bh=Tp7YaKR66qnz472Pvf8IC6ILZsObjrxW4B9MHocF3Bg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=2VN7A6eE/QGhcSmL+siTAaAPVNORjfYsHAxqQtpvP/AVknTIgVG+S5vHcurkvkZEE ctejvIucOMF7qdRc1L7iidCtELUjAs9fSqo4fE39Lf5npIzdGY6g39n1D7eiBKiO0c M11tiT6qcA4rePppv7KnKmqHOReUXm6sLTrgXy1U= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730054AbgIRCgt (ORCPT ); Thu, 17 Sep 2020 22:36:49 -0400 Received: from mail.kernel.org ([198.145.29.99]:39552 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728835AbgIRCMu (ORCPT ); Thu, 17 Sep 2020 22:12:50 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 4F300235F8; Fri, 18 Sep 2020 02:12:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600395169; bh=Tp7YaKR66qnz472Pvf8IC6ILZsObjrxW4B9MHocF3Bg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sCy/yP0uiv0XHvTwd3mwjpZEWyCNaJOYyhhZC0xm4GEa4bWw4yDJqcCyhROCNJHPr z+cUq2WQTts1KmEnrnkQMfrY3fPPc4Yre07C7QsY/MQVoDDqDX8ZXmbZV8vXhLbeIU 63zo7XKMKaH57t5hH1tMm7N6n5KrQfzglgWabfa8= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Marco Elver , "Paul E . McKenney" , Sasha Levin Subject: [PATCH AUTOSEL 4.14 026/127] seqlock: Require WRITE_ONCE surrounding raw_seqcount_barrier Date: Thu, 17 Sep 2020 22:10:39 -0400 Message-Id: <20200918021220.2066485-26-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200918021220.2066485-1-sashal@kernel.org> References: <20200918021220.2066485-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Marco Elver [ Upstream commit bf07132f96d426bcbf2098227fb680915cf44498 ] 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 Acked-by: Paul E. McKenney Signed-off-by: Paul E. McKenney Signed-off-by: Sasha Levin --- 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 f189a8a3bbb88..7b3b5d05ab0de 100644 --- a/include/linux/seqlock.h +++ b/include/linux/seqlock.h @@ -243,6 +243,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; * @@ -262,11 +269,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.25.1