Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp2744359ybh; Mon, 9 Mar 2020 12:06:45 -0700 (PDT) X-Google-Smtp-Source: ADFU+vty2TEmQdz28lQGStZOtuVWJgnE4l1U6IWGu73wrG2IlrlosgimGRUnau7uHzFYG6ZC/0pz X-Received: by 2002:a9d:27c7:: with SMTP id c65mr9489042otb.318.1583780805375; Mon, 09 Mar 2020 12:06:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583780805; cv=none; d=google.com; s=arc-20160816; b=a1GXgalGEr2HHhk/Ze27q8UOi6dx4EKc7PUkkEJNT6cLZOdr8V0YVz0EfzRcJl6rYk sjzUk58rQ96w+nXhZLuJRNzeFa5s5NJEAy2vFFvysvqw9+mWS0DrRm6ZGhi9ySmw+KJQ E0K8ffQcbLYgLiWyzxdMITtJcbrYWHrBztBgqFVjpp3QVQxynuQuzikS/GDKcDHGvEfi AtphvefVV11gZoklppO3tNTkcIr4TKbBYdGxzFdeG4CyFmvNHOUyVVVD2o0N0zWNfNv9 SiFGCSeHM07nS5hHEPGVuV7t2222rRshILipGaiPah9fMQSNxIPcHrt7quLF7YQtq0WM T9aQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=aAeZE7M9XJOgfvuaaTTwmHskOOww/iLu9OR2wxfArPA=; b=z4FuNC2uoFOq3+W0TEJ6LuJBbBU2ftRPjduy7FbHtO7QPDnUPJ6P2oAofFN5eIVAgy cnLqD5KhFx4Nkf19q3J4yiv6XjHr9wZupPEpA0KsG1zyNncthYBkZP6riDy6IS3DSjEn RlgxLhu3W2ok5qUcQeEPkQvh8lrQD9sEd5xNzYPgdkT0XQRN3OinIDqJn55nE1HL+7Bl rkw0cOHos0Z+gSSxvjK6HBlo87Ioff1EiP/K37Pw23AVmLGQofeU77qBi3asdHevFas/ ObdmfC/1nlvyu+KPAg+VqGO+lbC/SLSo2ujS1vbdUplrLv65Upj421B61t9EA0soWMBQ 9/EA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=xdLsRV+A; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m11si6724983otp.174.2020.03.09.12.06.32; Mon, 09 Mar 2020 12:06:45 -0700 (PDT) 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=@kernel.org header.s=default header.b=xdLsRV+A; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727877AbgCITFf (ORCPT + 99 others); Mon, 9 Mar 2020 15:05:35 -0400 Received: from mail.kernel.org ([198.145.29.99]:47812 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727601AbgCITE0 (ORCPT ); Mon, 9 Mar 2020 15:04:26 -0400 Received: from paulmck-ThinkPad-P72.home (50-39-105-78.bvtn.or.frontiernet.net [50.39.105.78]) (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 574C424683; Mon, 9 Mar 2020 19:04:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1583780666; bh=BeRSDQfiJpgpFHo29aDwd7Eo1BJJA9FCMRIXZe86lZs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xdLsRV+AbOLO/RqlCUYHw1AYge3lmk3VWC5ZS+LbPXA1ivaPummluPrv4OrSytKHM L0O+NmMCAEqA/gWBfZyeU6z+CBPYgVHU3LmD31pLlAS4jnw970HL/mi/xP6esI5TWO 3h/kKJM8E+ye5+IPwVYLotKE6V3K9DYzHKyliOeA= From: paulmck@kernel.org To: linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, kernel-team@fb.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" Subject: [PATCH kcsan 17/32] kcsan: Introduce ASSERT_EXCLUSIVE_* macros Date: Mon, 9 Mar 2020 12:04:05 -0700 Message-Id: <20200309190420.6100-17-paulmck@kernel.org> X-Mailer: git-send-email 2.9.5 In-Reply-To: <20200309190359.GA5822@paulmck-ThinkPad-P72> References: <20200309190359.GA5822@paulmck-ThinkPad-P72> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Marco Elver Introduces ASSERT_EXCLUSIVE_WRITER and ASSERT_EXCLUSIVE_ACCESS, which may be used to assert properties of synchronization logic, where violation cannot be detected as a normal data race. Examples of the reports that may be generated: ================================================================== BUG: KCSAN: assert: race in test_thread / test_thread write to 0xffffffffab3d1540 of 8 bytes by task 466 on cpu 2: test_thread+0x8d/0x111 debugfs_write.cold+0x32/0x44 ... assert no writes to 0xffffffffab3d1540 of 8 bytes by task 464 on cpu 0: test_thread+0xa3/0x111 debugfs_write.cold+0x32/0x44 ... ================================================================== ================================================================== BUG: KCSAN: assert: race in test_thread / test_thread assert no accesses to 0xffffffffab3d1540 of 8 bytes by task 465 on cpu 1: test_thread+0xb9/0x111 debugfs_write.cold+0x32/0x44 ... read to 0xffffffffab3d1540 of 8 bytes by task 464 on cpu 0: test_thread+0x77/0x111 debugfs_write.cold+0x32/0x44 ... ================================================================== Signed-off-by: Marco Elver Suggested-by: Paul E. McKenney Signed-off-by: Paul E. McKenney --- include/linux/kcsan-checks.h | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/include/linux/kcsan-checks.h b/include/linux/kcsan-checks.h index 5dcadc2..cf69617 100644 --- a/include/linux/kcsan-checks.h +++ b/include/linux/kcsan-checks.h @@ -96,4 +96,44 @@ static inline void kcsan_check_access(const volatile void *ptr, size_t size, kcsan_check_access(ptr, size, KCSAN_ACCESS_ATOMIC | KCSAN_ACCESS_WRITE) #endif +/** + * ASSERT_EXCLUSIVE_WRITER - assert no other threads are writing @var + * + * Assert that there are no other threads writing @var; other readers are + * allowed. This assertion can be used to specify properties of concurrent code, + * where violation cannot be detected as a normal data race. + * + * For example, if a per-CPU variable is only meant to be written by a single + * CPU, but may be read from other CPUs; in this case, reads and writes must be + * marked properly, however, if an off-CPU WRITE_ONCE() races with the owning + * CPU's WRITE_ONCE(), would not constitute a data race but could be a harmful + * race condition. Using this macro allows specifying this property in the code + * and catch such bugs. + * + * @var variable to assert on + */ +#define ASSERT_EXCLUSIVE_WRITER(var) \ + __kcsan_check_access(&(var), sizeof(var), KCSAN_ACCESS_ASSERT) + +/** + * ASSERT_EXCLUSIVE_ACCESS - assert no other threads are accessing @var + * + * Assert that no other thread is accessing @var (no readers nor writers). This + * assertion can be used to specify properties of concurrent code, where + * violation cannot be detected as a normal data race. + * + * For example, in a reference-counting algorithm where exclusive access is + * expected after the refcount reaches 0. We can check that this property + * actually holds as follows: + * + * if (refcount_dec_and_test(&obj->refcnt)) { + * ASSERT_EXCLUSIVE_ACCESS(*obj); + * safely_dispose_of(obj); + * } + * + * @var variable to assert on + */ +#define ASSERT_EXCLUSIVE_ACCESS(var) \ + __kcsan_check_access(&(var), sizeof(var), KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ASSERT) + #endif /* _LINUX_KCSAN_CHECKS_H */ -- 2.9.5