Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp4477523pxb; Tue, 5 Oct 2021 04:04:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzVYwAumnjwwGHcAZSSFv8rR22T0urx83caCHTfzTFryNOMBZRaoH7zA8CYRU0IeHNzAUm1 X-Received: by 2002:a63:5c1b:: with SMTP id q27mr14811905pgb.284.1633431862672; Tue, 05 Oct 2021 04:04:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633431862; cv=none; d=google.com; s=arc-20160816; b=y8mR5MB4JpjmTatyM47x/d68TAeDuhg69W43LfwaDl1bkM+IXkF/cvEbnlcl5e4bPH CPQEm/cTjWHUfxBGI0L20LcXdj6wIgbxY2Nx1Xf+L1hbTCsrv/18FhtkLmYAeXTRO3Cm tXvnHq68SjrxgcvR9eZdMwi6LzKRNNdv1fs0xx5MYCxZ+Wh3Qdj7jWtU1mESk/Mlujfl U9wLUpTWo7YEaL4ydjJStq12BexluvdNDQQnauy/VyLyFkVpCF/3CC9aBoDddTJ6FPPu 9JKMLdEVn2i+NvjtApJx9DsBBvUYSZIsoOXhtEvwTTet3Ai+OjRsnCtGFUpRGX0LXe9m WNxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=VR1v+uOEj5JHWgZG/AjKjsmQRdRMH76n4CGK16N25tc=; b=Yh1x3XmzimG0XIBIvUi4VRXifAARGGt9gqsV+Xy2Lmy2bVVUOPr1jwGOb6cj54ci9e wVMXev51KsAfCYP2ZWJ96NkzoaPSi7AyfNOlThO6GUxQSgu4+wO3NWxSBSJZqZ2+Og3s kB2M65GMEiXlfJ93eFx0ZANoA38fg24LK+4pK4l2cjFKgoxsxD4is21tItZNiY2c3SXK 5daCFW8kcysY4NrOVQbQ40eExJVkVTj2osfaqNjmN0PVr70js+FOsh2McTkW5ftPMC5G 1Ao7Fy6D7bI7bnoK4vnd9XHbfSVckvqEQZv8Rx37A729Rrrns4mzyN5Q4UyHYeDLoqO1 hIqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=OOmVz5m9; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ob6si2044865pjb.154.2021.10.05.04.04.05; Tue, 05 Oct 2021 04:04:22 -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=@google.com header.s=20210112 header.b=OOmVz5m9; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234579AbhJELCb (ORCPT + 99 others); Tue, 5 Oct 2021 07:02:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234477AbhJELCA (ORCPT ); Tue, 5 Oct 2021 07:02:00 -0400 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 00026C06178C for ; Tue, 5 Oct 2021 04:00:04 -0700 (PDT) Received: by mail-qk1-x749.google.com with SMTP id q5-20020a05620a0d8500b0045edb4779dbso985183qkl.2 for ; Tue, 05 Oct 2021 04:00:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=VR1v+uOEj5JHWgZG/AjKjsmQRdRMH76n4CGK16N25tc=; b=OOmVz5m9sgHxZ5tpr/f91NKUEC1uc9rMyBTH60E4A2rtnobKjQUKS5BS5vP0IphX4h wZwGLnUaMH/gItLkkn3k+udKE5o/pECwpdVoaNBrXEfthT0ThUaifj4RWj+cabsTGBGf l1ZHm1JhkDyaGdhfJDbvEYYUbwneLAxtPO0KzSK63NWqe69yIu5BETJMFPUjGr/reJ5p 8C70N/BQQzpJJdfVViy3y/O1XL0IgkIsY0a+Rycv8smMQBb9va/r8WGrDJttmTTfKgVt P/LE7k6AHKligqpuKS3WqrmkDoaFPkHsV8/Gm5n/MWjw9huBNrITLIWi2bSp/lTViEvC YuYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=VR1v+uOEj5JHWgZG/AjKjsmQRdRMH76n4CGK16N25tc=; b=fXYDEj/C+o76lnl7BaKCHLj1e34XVzKabmzFlgflmtJIL60VxZawumbdTmUYETFGW2 fN4sXER6E0WSSsoZQOKlvQfvR2dCKnqpjUg264CIhhthjTtmCySeJhDLJje+9NGz9Etb lXPbspAf59ckInYz9k+fAe3AgT5jmIpFoOoZadDReV91c789DOLpiHObVWFc/H2TrA7o jWdKJHMa6aeZhZ1+xkCfDZHuSnhixp1Y7Unpl709O1US1nMV9bRSmIh6yA5A3pilP/iA plXhmicrWW+kcXsfjOzj1FVA6PWYPtTs5xK6AUeYXE26q//nSL/2DGsYlZObT2lkjYeM yPsw== X-Gm-Message-State: AOAM530kdBFwWZ9dMUUNqUq6lgJFpC97Pe8CEQhMFfdXJ/ynMhHAWuEo 3mBS3YD/VDXC/JC//Vkg016HBpmk5g== X-Received: from elver.muc.corp.google.com ([2a00:79e0:15:13:e44f:5054:55f8:fcb8]) (user=elver job=sendgmr) by 2002:a05:6214:1022:: with SMTP id k2mr27293030qvr.53.1633431604158; Tue, 05 Oct 2021 04:00:04 -0700 (PDT) Date: Tue, 5 Oct 2021 12:58:51 +0200 In-Reply-To: <20211005105905.1994700-1-elver@google.com> Message-Id: <20211005105905.1994700-10-elver@google.com> Mime-Version: 1.0 References: <20211005105905.1994700-1-elver@google.com> X-Mailer: git-send-email 2.33.0.800.g4c38ced690-goog Subject: [PATCH -rcu/kcsan 09/23] kcsan: Document modeling of weak memory From: Marco Elver To: elver@google.com, "Paul E . McKenney" Cc: Alexander Potapenko , Boqun Feng , Borislav Petkov , Dmitry Vyukov , Ingo Molnar , Josh Poimboeuf , Mark Rutland , Peter Zijlstra , Thomas Gleixner , Waiman Long , Will Deacon , kasan-dev@googlegroups.com, linux-arch@vger.kernel.org, linux-doc@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" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Document how KCSAN models a subset of weak memory and the subset of missing memory barriers it can detect as a result. Signed-off-by: Marco Elver --- Documentation/dev-tools/kcsan.rst | 72 +++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 13 deletions(-) diff --git a/Documentation/dev-tools/kcsan.rst b/Documentation/dev-tools/kcsan.rst index 7db43c7c09b8..4fc3773fead9 100644 --- a/Documentation/dev-tools/kcsan.rst +++ b/Documentation/dev-tools/kcsan.rst @@ -204,17 +204,17 @@ Ultimately this allows to determine the possible executions of concurrent code, and if that code is free from data races. KCSAN is aware of *marked atomic operations* (``READ_ONCE``, ``WRITE_ONCE``, -``atomic_*``, etc.), but is oblivious of any ordering guarantees and simply -assumes that memory barriers are placed correctly. In other words, KCSAN -assumes that as long as a plain access is not observed to race with another -conflicting access, memory operations are correctly ordered. - -This means that KCSAN will not report *potential* data races due to missing -memory ordering. Developers should therefore carefully consider the required -memory ordering requirements that remain unchecked. If, however, missing -memory ordering (that is observable with a particular compiler and -architecture) leads to an observable data race (e.g. entering a critical -section erroneously), KCSAN would report the resulting data race. +``atomic_*``, etc.), and a subset of ordering guarantees implied by memory +barriers. With ``CONFIG_KCSAN_WEAK_MEMORY=y``, KCSAN models load or store +buffering, and can detect missing ``smp_mb()``, ``smp_wmb()``, ``smp_rmb()``, +``smp_store_release()``, and all ``atomic_*`` operations with equivalent +implied barriers. + +Note, KCSAN will not report all data races due to missing memory ordering, +specifically where a memory barrier would be required to prohibit subsequent +memory operation from reordering before the barrier. Developers should +therefore carefully consider the required memory ordering requirements that +remain unchecked. Race Detection Beyond Data Races -------------------------------- @@ -268,6 +268,52 @@ marked operations, if all accesses to a variable that is accessed concurrently are properly marked, KCSAN will never trigger a watchpoint and therefore never report the accesses. +Modeling Weak Memory +~~~~~~~~~~~~~~~~~~~~ + +KCSAN's approach to detecting data races due to missing memory barriers is +based on modeling access reordering (with ``CONFIG_KCSAN_WEAK_MEMORY=y``). +Each plain memory access for which a watchpoint is set up, is also selected for +simulated reordering within the scope of its function (at most 1 in-flight +access). + +Once an access has been selected for reordering, it is checked along every +other access until the end of the function scope. If an appropriate memory +barrier is encountered, the access will no longer be considered for simulated +reordering. + +When the result of a memory operation should be ordered by a barrier, KCSAN can +then detect data races where the conflict only occurs as a result of a missing +barrier. Consider the example:: + + int x, flag; + void T1(void) + { + x = 1; // data race! + WRITE_ONCE(flag, 1); // correct: smp_store_release(&flag, 1) + } + void T2(void) + { + while (!READ_ONCE(flag)); // correct: smp_load_acquire(&flag) + ... = x; // data race! + } + +When weak memory modeling is enabled, KCSAN can consider ``x`` in ``T1`` for +simulated reordering. After the write of ``flag``, ``x`` is again checked for +concurrent accesses: because ``T2`` is able to proceed after the write of +``flag``, a data race is detected. With the correct barriers in place, ``x`` +would not be considered for reordering after the proper release of ``flag``, +and no data race would be detected. + +Deliberate trade-offs in complexity but also practical limitations mean only a +subset of data races due to missing memory barriers can be detected. Recall +that watchpoints are only set up for plain accesses, and the only access type +for which KCSAN simulates reordering. This means reordering of marked accesses +is not modeled. Furthermore, with the currently available compiler support, the +implementation is limited to modeling the effects of "buffering" (delaying +accesses), since the runtime cannot "prefetch" accesses. One implication of +this is that acquire operations do not require barrier instrumentation. + Key Properties ~~~~~~~~~~~~~~ @@ -290,8 +336,8 @@ Key Properties 4. **Detects Racy Writes from Devices:** Due to checking data values upon setting up watchpoints, racy writes from devices can also be detected. -5. **Memory Ordering:** KCSAN is *not* explicitly aware of the LKMM's ordering - rules; this may result in missed data races (false negatives). +5. **Memory Ordering:** KCSAN is aware of only a subset of LKMM ordering rules; + this may result in missed data races (false negatives). 6. **Analysis Accuracy:** For observed executions, due to using a sampling strategy, the analysis is *unsound* (false negatives possible), but aims to -- 2.33.0.800.g4c38ced690-goog