From patchwork Thu Nov 18 08:10:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 12626189 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B83DFC433F5 for ; Thu, 18 Nov 2021 08:10:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 94CE561B44 for ; Thu, 18 Nov 2021 08:10:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244206AbhKRIN6 (ORCPT ); Thu, 18 Nov 2021 03:13:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244231AbhKRIN6 (ORCPT ); Thu, 18 Nov 2021 03:13:58 -0500 Received: from mail-wm1-x349.google.com (mail-wm1-x349.google.com [IPv6:2a00:1450:4864:20::349]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 67552C061767 for ; Thu, 18 Nov 2021 00:10:58 -0800 (PST) Received: by mail-wm1-x349.google.com with SMTP id j193-20020a1c23ca000000b003306ae8bfb7so2242086wmj.7 for ; Thu, 18 Nov 2021 00:10:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=Xzmo5mL9yKPgm1lZgxwsGIqYX+i9650OydzQnMolJjY=; b=qEJtN/DkSyckbRSxlIM9urYoLsb2hj7lFIxxPrdCEa/J11GugPm++lQKPAoPPA/En1 HAagnvpvld0c59y1nXQoSdU28kF6v7drYbyhjp6FCwRLAsX/zbPyFI5xXRjX8rc1oGu3 n5feY5KpUMjB4gzg7AMoJZWwyizpAXiugdaNx2kE2ILSDG1LgPsLdzEJCucQPCrvbhqd 8Hkype+TRhQp2L++fjjiYl8j4BG4Hz1E9HM5WKmImoj0fMbgcitWazJ9QMo6GxkbtIlw YB4XpISSa3H3WuSWwlvkFRqnezzu26Jka/PzoDc7lqgYBOcl/r+/Uc4wFXuLaCec3va2 H87g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=Xzmo5mL9yKPgm1lZgxwsGIqYX+i9650OydzQnMolJjY=; b=YBkId0oFgbBKHhAm1ZmCGOt3R5h9+mh3eWId/aE9IW3dzaJ44m1mmKDbtW5Hnx+dQq XMHJxsgPuG+81/QzSFdCkGnT49NUpH85DRsPLuh5C5FE63DkXl2DDFtmwpFyUB4lBn/7 1f+nJf6nyO4Ry/jyl9WqlQLpLkq7aDm/c0oo4jcJRK+CJWLb7N1P6QzmB5E7t86nnyKe sK4jTt9KAjiUQ1eVxpNhALz72rK1qdRYoIN66zWN3cJpJHS2IQEX8+8d/o7G/FFw4gyN ZTvve9999UwLnaHUYI3JOR3a3pDCpLA23MqREitj/AXUHgAdrRp45qb20JPu3Poh7kAH 34Gw== X-Gm-Message-State: AOAM531lzgT7Lmz9C1GmwksqLiB4/7jiJDrPto2j61jtFl8BM9oEb5fv SgCfin++KRXVsNL4r63/NHULXuYbXg== X-Google-Smtp-Source: ABdhPJzf0beCDskxs6TLI9QKNEqbgr6M0bT4DWDT314iI+YiTOIk1FtVgaYUzMPaVpD7QqDDgrzhRBUpaw== X-Received: from elver.muc.corp.google.com ([2a00:79e0:15:13:7155:1b7:fca5:3926]) (user=elver job=sendgmr) by 2002:a5d:4e52:: with SMTP id r18mr26521150wrt.224.1637223056499; Thu, 18 Nov 2021 00:10:56 -0800 (PST) Date: Thu, 18 Nov 2021 09:10:04 +0100 Message-Id: <20211118081027.3175699-1-elver@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.34.0.rc2.393.gf8c9666880-goog Subject: [PATCH v2 00/23] kcsan: Support detecting a subset of missing memory barriers 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 Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org Detection of some missing memory barriers has been on the KCSAN feature wishlist for some time: this series adds support for modeling a subset of weak memory as defined by the LKMM, which enables detection of a subset of data races due to missing memory barriers. KCSAN's approach to detecting missing memory barriers is based on modeling access reordering. Each 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). We are limited to modeling the effects of "buffering" (delaying the access), since the runtime cannot "prefetch" accesses. 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 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 due to reordering accesses. Some more details and an example are captured in the updated . Some light fuzzing with the feature also resulted in a discussion [1] around an issue which appears to be allowed, but unlikely in practice. [1] https://lkml.kernel.org/r/YRo58c+JGOvec7tc@elver.google.com The first half of the series are core KCSAN changes, documentation updates, and test changes. The second half adds instrumentation to barriers, atomics, bitops, along with enabling barrier instrumentation for some currently uninstrumented subsystems. The last two patches are objtool changes to add the usual entries to the uaccess whitelist, but also instruct objtool to remove memory barrier instrumentation from noinstr code (on x86). --- v2: * Rewrite objtool patch after rebase to v5.16-rc1. * Note the reason in documentation that address or control dependencies do not require special handling. * Rename kcsan_atomic_release() to kcsan_atomic_builtin_memorder() to avoid confusion. * Define kcsan_noinstr as noinline if we rely on objtool nop'ing out calls, to avoid things like LTO inlining it. v1: https://lore.kernel.org/all/20211005105905.1994700-1-elver@google.com/ Marco Elver (23): kcsan: Refactor reading of instrumented memory kcsan: Remove redundant zero-initialization of globals kcsan: Avoid checking scoped accesses from nested contexts kcsan: Add core support for a subset of weak memory modeling kcsan: Add core memory barrier instrumentation functions kcsan, kbuild: Add option for barrier instrumentation only kcsan: Call scoped accesses reordered in reports kcsan: Show location access was reordered to kcsan: Document modeling of weak memory kcsan: test: Match reordered or normal accesses kcsan: test: Add test cases for memory barrier instrumentation kcsan: Ignore GCC 11+ warnings about TSan runtime support kcsan: selftest: Add test case to check memory barrier instrumentation locking/barriers, kcsan: Add instrumentation for barriers locking/barriers, kcsan: Support generic instrumentation locking/atomics, kcsan: Add instrumentation for barriers asm-generic/bitops, kcsan: Add instrumentation for barriers x86/barriers, kcsan: Use generic instrumentation for non-smp barriers x86/qspinlock, kcsan: Instrument barrier of pv_queued_spin_unlock() mm, kcsan: Enable barrier instrumentation sched, kcsan: Enable memory barrier instrumentation objtool, kcsan: Add memory barrier instrumentation to whitelist objtool, kcsan: Remove memory barrier instrumentation from noinstr Documentation/dev-tools/kcsan.rst | 76 +++- arch/x86/include/asm/barrier.h | 10 +- arch/x86/include/asm/qspinlock.h | 1 + include/asm-generic/barrier.h | 54 ++- .../asm-generic/bitops/instrumented-atomic.h | 3 + .../asm-generic/bitops/instrumented-lock.h | 3 + include/linux/atomic/atomic-instrumented.h | 135 +++++- include/linux/kcsan-checks.h | 51 ++- include/linux/kcsan.h | 11 +- include/linux/sched.h | 3 + include/linux/spinlock.h | 2 +- init/init_task.c | 9 +- kernel/kcsan/Makefile | 2 + kernel/kcsan/core.c | 326 +++++++++++--- kernel/kcsan/kcsan_test.c | 416 ++++++++++++++++-- kernel/kcsan/report.c | 51 ++- kernel/kcsan/selftest.c | 141 ++++++ kernel/sched/Makefile | 7 +- lib/Kconfig.kcsan | 16 + mm/Makefile | 2 + scripts/Makefile.kcsan | 15 +- scripts/Makefile.lib | 5 + scripts/atomic/gen-atomic-instrumented.sh | 41 +- tools/objtool/check.c | 41 +- tools/objtool/include/objtool/elf.h | 2 +- 25 files changed, 1248 insertions(+), 175 deletions(-)