Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp1672601ybv; Thu, 6 Feb 2020 07:52:59 -0800 (PST) X-Google-Smtp-Source: APXvYqxufVUMzp/E3t1+Fz61ODEaLgc791lQJOJ9ILlvWr3DaSNmoGIz4FxuJbfMdU7VOLP7Ux6G X-Received: by 2002:a05:6808:30d:: with SMTP id i13mr7102183oie.144.1581004378936; Thu, 06 Feb 2020 07:52:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581004378; cv=none; d=google.com; s=arc-20160816; b=H4KZ0fAwJDhLgHE5/hsvpIVSzzQ1yon4wqYFsEgF1CvIntsKEmU4iOsLQYsqhIdi2a rdPIbZeUwpQ/hYmBoTDaamTGW1Q4ds4LjXvpNtI7DgxSktuPWl2O5js2nHgIg5oqB2Jx rgjcKm0Hpy6Ax1XNOK14CO+BsOg3yb6e7Iq3ufVOxYOAWZsBcEJL/zoyj6tG2+4MU8Zk mwvHt0Fp6fhb5DJCiTAMM2Q0i+mg1wn0luOU/o0QuJqc/iT1y79zEkI86Veb8cfU1iWA sv0+lptLsdJrkP2Mpx/G5Z49cAGL4WTfJoUEu29d9koBGZh9FgOnqwIEeG1/9/xuNsil 1emw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=9ivMm6BzzOGZymLSvHhw6OFUtFZ37ARIsWSjvoe/3Wc=; b=myClx85t5jDPRKLtAulDOMfWcLgC0pNEA+lrPZQB3MsuY3QN1PCHo+PHxksloDIq7O LYkXezVSRHhzK/VpElGpYkcfwoJv5vJZg57XnLyMqrhmM9tWj11Ca6Vf7GHeoJRHVszf zCKNXi1XLfCY69+Ebnars95OVoFc+H+JJbJ4W9o/k6UMOoIsBEx1GskEt3zJNgpP0Xqb /5WEe/0eZdVfwZvJZDktR4TBIx29RRfKBRRsyB+7Zy2UxRrhXAr02+Ib0KlEu3h6Sfv8 0FeVnpdbzsRF9EPkGslZ7S8HMk61VKqCfUpLrM/TgTZqHJu9XkqJAtUMTBQCvorM9NWt eACg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=KNc7TEIw; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l8si2162662oii.249.2020.02.06.07.52.46; Thu, 06 Feb 2020 07:52:58 -0800 (PST) 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=@google.com header.s=20161025 header.b=KNc7TEIw; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727530AbgBFPvx (ORCPT + 99 others); Thu, 6 Feb 2020 10:51:53 -0500 Received: from mail-wr1-f73.google.com ([209.85.221.73]:56227 "EHLO mail-wr1-f73.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727138AbgBFPvx (ORCPT ); Thu, 6 Feb 2020 10:51:53 -0500 Received: by mail-wr1-f73.google.com with SMTP id m15so3584271wrs.22 for ; Thu, 06 Feb 2020 07:51:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=9ivMm6BzzOGZymLSvHhw6OFUtFZ37ARIsWSjvoe/3Wc=; b=KNc7TEIw3xiZevfE1+iy3grNxT5hmqDXPT6BQN/a4NTgu/Lc1hvfVygu44jAxKR73F XW5oQ3UXxyyhgqmKt3NRpYCy1mMwxEVRU5zDvrWHuhCU3OWyeJgv2b8+zc1oXtfM5Nd7 5Mvc5p6vl+5L/6A8wSTrp9vGQn0ggn2VMjOjgdJsb53Ts3uMEkRbzOFcXtovHvGOZXM/ udV+++FUdci8d537TiQUlCkm6IPXj7z2y+BaPZS7GXsGSHO9/MdAtHU8KspNm/HcCQ54 +/hRQ7gcRWiysT8F5Gz/vDm8gYI0MultZnwhIJZ9D31HbQVOSHIuQGr3WINGtT4CCYL/ JD7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=9ivMm6BzzOGZymLSvHhw6OFUtFZ37ARIsWSjvoe/3Wc=; b=NBy3FK9GISwtKcMqlpGGHE12pIjI1dXzxxJErFuDKRo7n6aZ3XR+wQS32V85Yde2Cw GBeIGZuMFS6foAS7O796EgpsCgvfqq6g/MHmk2JwKhmdI+eiekhhW2m9U68yr+lhN2Cg uppuPTJjWDXnHWB6DQ1/VOK38u+vHiLGXmnqArqMAU6n3qXkFCZC5EIReYOXzVJQZwWH r5qEIgsqPKvhgi3VIu3d5xgT7G2vCf+BXTbmQp2hW74QVWhFMGC6D/ALy5NQO2Z56KIb wD0R0Zs0GChhD045Ei01Z3WF45MbIE/JMkLzPc+f9O1NzVyDczfEIk/+qhUkozTYqboa 3rWw== X-Gm-Message-State: APjAAAWaiHi46SKGy7qncMu/J4pOcpLn/A+hCYupOWN0eFQHv39/FGR4 9ztB0zHLb10TGKlRPk3WuKSUgXdzaA== X-Received: by 2002:adf:f302:: with SMTP id i2mr4427613wro.21.1581004310493; Thu, 06 Feb 2020 07:51:50 -0800 (PST) Date: Thu, 6 Feb 2020 16:46:25 +0100 In-Reply-To: <20200206154626.243230-1-elver@google.com> Message-Id: <20200206154626.243230-2-elver@google.com> Mime-Version: 1.0 References: <20200206154626.243230-1-elver@google.com> X-Mailer: git-send-email 2.25.0.341.g760bfbb309-goog Subject: [PATCH v2 2/3] kcsan: Introduce ASSERT_EXCLUSIVE_* macros From: Marco Elver To: elver@google.com Cc: paulmck@kernel.org, andreyknvl@google.com, glider@google.com, dvyukov@google.com, kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- v2: * Update ASSERT_EXCLUSIVE_ACCESS() example. --- 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 5dcadc221026e..cf6961794e9a1 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.25.0.341.g760bfbb309-goog