Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp880909pxk; Thu, 17 Sep 2020 20:12:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxZnpoIB/rusigqDy91yeFOgutWRm3M9hRrT58ZXS9zv/dQUdg4qxSiDuvR/rnT7AnHnrnK X-Received: by 2002:aa7:ca17:: with SMTP id y23mr37621719eds.245.1600398746289; Thu, 17 Sep 2020 20:12:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600398746; cv=none; d=google.com; s=arc-20160816; b=oT80Ly9uqjNk24omMQSnS6d7N7lQx9h3rzEuDJWzlOjUiqSSr0SQ5VVw9HfunjNmRH 7z4Fj7GTp3Sg7suE/LExQLrQsJhMPymF9Kj2/V0eES/O8zgdiioOIk9/+DwHFbYzAqGs 4ShwVI526vJPRBmx+r5Iv4tZyg/COIQoJSrHZ8NceN51fqszIS6KmsNJjmqWv+qKU/W0 Tva25/X/6Cp6X4sGUiD0Tchu79mg+Dm3PInvjKaCjckgLgTocQ5QFFVP9DddO0D41SDv CnNhz65cysEg65rkcJTo7FlRERKRwADaxERNns7KAZBmwAbvpv2+xwv6nU+9bX7rINSz ESkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=H6jIgo19PkiDy3MEao/Fh7wEjA/KRxnoqDzvvMCZQP0=; b=KKObz+yBuPmMuozeD5aEm5fV36+iGaMchIW6jJ7ITrLA7rs1ueGe//2mcXepQa3KIq 7rt49WSBbMj/1/gHbcXq8q59anTOQplCGVOPijOQ0PcOOw3/C6zBkgat1cQRuLQKLgpf VzlF1kIC4vXDL6jrCmhyQhS+5vKjBM7Pw/KcFhYOxjlx9aO4syUv38t7YFq8w96IcgQu DihA6D8P5NViU/sJogKdt8CAfb3QhbVBLN/CcpS00v6NXe3feA1oKIDI6Ehxm8yRk1Lr Sb9sP4c30CkdW5KOQdAclvXwK/3HABg6GmssIgHDHSZLUq2XwvyGEoWcmbLcOYwaRTUW GBSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="gmpgl/Si"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id do18si1247406ejc.13.2020.09.17.20.12.02; Thu, 17 Sep 2020 20:12:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="gmpgl/Si"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727899AbgIRDKH (ORCPT + 99 others); Thu, 17 Sep 2020 23:10:07 -0400 Received: from mail.kernel.org ([198.145.29.99]:48512 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726819AbgIRCCx (ORCPT ); Thu, 17 Sep 2020 22:02:53 -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 C492B235FD; Fri, 18 Sep 2020 02:02:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600394572; bh=YY+ZfcENL85GrpMPOlpneEkLKoX4t+9qxP2kAEQWV0U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gmpgl/Si52GMoX9cXr4vD9cIoSrTGe0oE77euZic655y2TiJKK2PVHQiDZuOtbZS9 +Fqtc1MvpPZFIdokOiVu3JOmOc+F7xqfu/nRmib52QOXjJwYAD+ANi2bTQDtjWSVxC AO6Ohf/QW6oLoK5KuNpxG9jmcm/Dv/miqAhgvkbQ= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Marco Elver , "Paul E . McKenney" , Sasha Levin Subject: [PATCH AUTOSEL 5.4 084/330] seqlock: Require WRITE_ONCE surrounding raw_seqcount_barrier Date: Thu, 17 Sep 2020 21:57:04 -0400 Message-Id: <20200918020110.2063155-84-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200918020110.2063155-1-sashal@kernel.org> References: <20200918020110.2063155-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 bcf4cf26b8c89..a42a29952889c 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