Received: by 2002:a05:6500:1b41:b0:1fb:d597:ff75 with SMTP id cz1csp360279lqb; Tue, 4 Jun 2024 13:40:25 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWJG9A0LasQ+x2debKTfKg5t4ofCDOLpf234vFDUzFrV5R39jxc/EtK5U01/lVv50UTL97nRiojHBORy1vMde7O2ZYcRtBufPjv9LnBLA== X-Google-Smtp-Source: AGHT+IH4//ozl4sLzU7QtrXQLu/R73rlaaLNwEzPSUuWqT+TQXkjDXOFJISIWtLaOnffQHpGZeMN X-Received: by 2002:a05:6a00:2d8d:b0:702:4899:6657 with SMTP id d2e1a72fcca58-703e5a7af6dmr649153b3a.24.1717533625046; Tue, 04 Jun 2024 13:40:25 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717533625; cv=pass; d=google.com; s=arc-20160816; b=ooufzH7+MUCVCI6K3E60TrWSOYGJpE+/857aO6hHvbcHBBraKrN5/cyGOP7Cipbivq uhRoKCnVbbEdEbuQoXkpX1bp20m5kF/bQZt5RSHn1BG1VZ4SvTdMGtJCMKoSYhZT7A+2 nKXjSd89h1yuYut+fWgz+0jQPSHVbGUVXfMdK6V4IkhLRcNcs3CFUWWR0WARzQZAkiUx iaclEO/YPwTmLU/3YBhSAGhKOABI1uFEGC8IANpoHI7lCllEOxA5yFER1vYgLYF2lEnP 0ZRL+1raQ5O5gaM+Yw0zAR9YeW8J2g3YuRNQE7oPeO1zSFgYbS2TR6vJLQW8I5yBKSaD BawQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=0jT0aEf9tM+bN3Sg+Ch3w9OOLh2WMqcyNipCyZcD19Q=; fh=lZtp+aPlO3n/dA81ufB6aTjpVYBC28jYtVjXiwIxB00=; b=XY/SFcejBqCWpt89DBlGl3xsh6ApwWtfMY5fIi6GvuXGJnPxwlkAcQOLBMvZ32bUVM TAkx3q8xFSSO2zD62MOlzX1UnJqs+2A8A1uAkxBeyTFSixyIM7DoNX+DyQJCKtlaNWjV NVF+0k+28TuYKabgjc1DcJW0l6NXjg+MlxmRNhsufRpPK+vICPedpcZSSjI0tQHsV7pO cvWOwCJIr3yXInuugrCFT7lg3UE0OS1hrBuBoUaZiyOBg9FBMMuvqEwklCOqJlKgsgHS bhT8M0UuG8mFelqIMQoDSrlK/qi/hUslThMrQW6JETDW5bKheCdMU7GFvT1S1iJ3qxdt dTFg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iQuK7mO8; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-201354-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-201354-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id d2e1a72fcca58-70242d58ba1si8743532b3a.352.2024.06.04.13.40.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jun 2024 13:40:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-201354-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iQuK7mO8; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-201354-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-201354-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id AD1242817A7 for ; Tue, 4 Jun 2024 20:40:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 132BC14C585; Tue, 4 Jun 2024 20:40:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="iQuK7mO8" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 36DE13BB24 for ; Tue, 4 Jun 2024 20:40:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717533609; cv=none; b=VoRwJHtoWV+WU/6N198JBA8n/wmJUc0KIG48qg0VO7sAead0GmxtpxNR6PUqbgcPesi8EzCsKbSV5ENty3lAobMd4cUBVtimhLcVMYphdk7gmo9WgB3JQzAnI7bYeUpKFslo/57rWr/Ke5sxR7K2DPiTWnYNsyUhQMj9VnYfDCY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717533609; c=relaxed/simple; bh=OA4jXIR7HzNnpaEbG8qQ2TpYGmLYNKQZaYWLgI4fz70=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AjrEkKe+ZXWe1WNkR7GgY60cEmyZC0NrLT7uZUcxssXl1V4i5NiJr4EhVg9GRLQlyJQZiA7WcYigUh2rGxhMtYQ/LrnRabh+IG0h/OnGZp65xPCb/vr5xIFLMQe0jBx+upaY0SgdVut8Nk3RNLXo6EqrFHDVCg0dgZlx23iqzMw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iQuK7mO8; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id C5893C2BBFC; Tue, 4 Jun 2024 20:40:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717533608; bh=OA4jXIR7HzNnpaEbG8qQ2TpYGmLYNKQZaYWLgI4fz70=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iQuK7mO8wi/Niq/PkspASgP/nPjYY3fsO3u+c0iQ72Y+DdLzwdW5KhZx9S1D90eF5 H5zIDaBAQkbsSDTRfhShL+zOdJ5gUIdTgKx8xy++sd8dntdLIr8LRIgNkPiwC/qB+Y 1IO0oO9SYbRYojZHcFodL+pK2d35OT5g4lgrsSAvULWdc+O0ubFaKETiRai07Nuwoa lyUJdv49tW37eVsQAY+IKuPFongImcl5sGRj+bCLgra8GaXTFFMuZRffYCI0xfs642 gzTxpdwkarELkZS7vB4/8dEktBGZeUMyjI/xDdAtFgHqIQbAlaet5xhSW1Y0pOgMLp MCMWsJuD2dyYw== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 77B2CCE3ED6; Tue, 4 Jun 2024 13:40:08 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, kernel-team@meta.com, mingo@kernel.org Cc: elver@google.com, andreyknvl@google.com, glider@google.com, dvyukov@google.com, cai@lca.pw, boqun.feng@gmail.com, "Paul E. McKenney" , Bart Van Assche , Breno Leitao , Jens Axboe Subject: [PATCH kcsan 1/2] kcsan: Add example to data_race() kerneldoc header Date: Tue, 4 Jun 2024 13:40:05 -0700 Message-Id: <20240604204006.2367440-1-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Although the data_race() kerneldoc header accurately states what it does, some of the implications and usage patterns are non-obvious. Therefore, add a brief locking example and also state how to have KCSAN ignore accesses while also preventing the compiler from folding, spindling, or otherwise mutilating the access. [ paulmck: Apply Bart Van Assche feedback. ] [ paulmck: Apply feedback from Marco Elver. ] Reported-by: Bart Van Assche Signed-off-by: Paul E. McKenney Cc: Marco Elver Cc: Breno Leitao Cc: Jens Axboe --- include/linux/compiler.h | 10 +++++++- .../Documentation/access-marking.txt | 24 ++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/include/linux/compiler.h b/include/linux/compiler.h index 8c252e073bd81..68a24a3a69799 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -194,9 +194,17 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val, * This data_race() macro is useful for situations in which data races * should be forgiven. One example is diagnostic code that accesses * shared variables but is not a part of the core synchronization design. + * For example, if accesses to a given variable are protected by a lock, + * except for diagnostic code, then the accesses under the lock should + * be plain C-language accesses and those in the diagnostic code should + * use data_race(). This way, KCSAN will complain if buggy lockless + * accesses to that variable are introduced, even if the buggy accesses + * are protected by READ_ONCE() or WRITE_ONCE(). * * This macro *does not* affect normal code generation, but is a hint - * to tooling that data races here are to be ignored. + * to tooling that data races here are to be ignored. If the access must + * be atomic *and* KCSAN should ignore the access, use both data_race() + * and READ_ONCE(), for example, data_race(READ_ONCE(x)). */ #define data_race(expr) \ ({ \ diff --git a/tools/memory-model/Documentation/access-marking.txt b/tools/memory-model/Documentation/access-marking.txt index 65778222183e3..3377d01bb512c 100644 --- a/tools/memory-model/Documentation/access-marking.txt +++ b/tools/memory-model/Documentation/access-marking.txt @@ -24,6 +24,11 @@ The Linux kernel provides the following access-marking options: 4. WRITE_ONCE(), for example, "WRITE_ONCE(a, b);" The various forms of atomic_set() also fit in here. +5. __data_racy, for example "int __data_racy a;" + +6. KCSAN's negative-marking assertions, ASSERT_EXCLUSIVE_ACCESS() + and ASSERT_EXCLUSIVE_WRITER(), are described in the + "ACCESS-DOCUMENTATION OPTIONS" section below. These may be used in combination, as shown in this admittedly improbable example: @@ -205,6 +210,23 @@ because doing otherwise prevents KCSAN from detecting violations of your code's synchronization rules. +Use of __data_racy +------------------ + +Adding the __data_racy type qualifier to the declaration of a variable +causes KCSAN to treat all accesses to that variable as if they were +enclosed by data_race(). However, __data_racy does not affect the +compiler, though one could imagine hardened kernel builds treating the +__data_racy type qualifier as if it was the volatile keyword. + +Note well that __data_racy is subject to the same pointer-declaration +rules as are other type qualifiers such as const and volatile. +For example: + + int __data_racy *p; // Pointer to data-racy data. + int *__data_racy p; // Data-racy pointer to non-data-racy data. + + ACCESS-DOCUMENTATION OPTIONS ============================ @@ -342,7 +364,7 @@ as follows: Because foo is read locklessly, all accesses are marked. The purpose of the ASSERT_EXCLUSIVE_WRITER() is to allow KCSAN to check for a buggy -concurrent lockless write. +concurrent write, whether marked or not. Lock-Protected Writes With Heuristic Lockless Reads -- 2.40.1