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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E7686C433F5 for ; Tue, 14 Dec 2021 16:22:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235767AbhLNQW1 (ORCPT ); Tue, 14 Dec 2021 11:22:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235744AbhLNQWU (ORCPT ); Tue, 14 Dec 2021 11:22:20 -0500 Received: from mail-wm1-x34a.google.com (mail-wm1-x34a.google.com [IPv6:2a00:1450:4864:20::34a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B892C061574 for ; Tue, 14 Dec 2021 08:22:19 -0800 (PST) Received: by mail-wm1-x34a.google.com with SMTP id o18-20020a05600c511200b00332fa17a02eso8123458wms.5 for ; Tue, 14 Dec 2021 08:22:19 -0800 (PST) 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=Jjmj3NfcL4RIWWcjIe7Qu67hp42cRss+aplyHHmWX6A=; b=dsYBdOhqGaaa9PjsEmjVgP7jb+SydcbxMgf/q2frLLdl6Ue4dxDmWMI1uSRnt9e4tl 8EN6fuqAN1pB07ffVjLKp68NQkEugitoVyjaWEXGt5URJy4AJ0/K/EJIzia7a8ifLLHH Qdp5xARe4b4nPQ6LApIOirW8wjWL8FFt6TB/XT6z9uClOELYki2BZJTBvrbNuRnOix+n DJeTnfHEFEc8sdPd09Y1lSle0Vt/+oaJvlHX484kHFA9gE4h3Y7MyhCz4sy8ZJ1NfP5c 8FYeQNEQyK3DKidsmLUkv+Q/Uw7+4laTzO9lkD8fN+gNuOLdKyKQ+sNof7Wvdz23qc7v w0lg== 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=Jjmj3NfcL4RIWWcjIe7Qu67hp42cRss+aplyHHmWX6A=; b=VcGNQr2dHK45c5y1zyZUIW7sLDFaFwyB1vLSbPzWGJ/8wHKQDXFSugE4sK7/FFd312 iAzlQ9c4xuD9gIsarjQqoBcfmvTRsqZF1jjiJeUmtKaf5CovNLcNMf/Ltm9C/x7HryDX s77lwgrD0/Rnms4vVgA/L5yND1AxdJlG9UkHweAXkH/0O4lDYvrrW1BSbJJAtysIpzw9 2ia7ZNx4NyyCvpCib1owiBE8sJ/ZFiDFyaIm9Rh1XSeUrLoeeYlvUVXskfjnZJ5b23S9 0B3UdzIICmUYlt40a9djH88J41zgVYuBNPJljN0ACaLlT6bCRu+X0ZAEI7B+8Ts+q6rV cdmA== X-Gm-Message-State: AOAM533ItcZGSSEkU8BCIcyPcSVpnP7bBCMaa6MPPskxPYagLD/HE9MQ b2wrlJBC98GQHdZFNeCDiB+zvAonTBU= X-Google-Smtp-Source: ABdhPJx22gOJI63ONPs3T0E3/ieKvxRNELWcFZOjCmtKU2tJkN8ygCc56stf/zL88guIxkLENaHppDelx68= X-Received: from glider.muc.corp.google.com ([2a00:79e0:15:13:357e:2b9d:5b13:a652]) (user=glider job=sendgmr) by 2002:a1c:23cb:: with SMTP id j194mr47944273wmj.13.1639498938088; Tue, 14 Dec 2021 08:22:18 -0800 (PST) Date: Tue, 14 Dec 2021 17:20:16 +0100 In-Reply-To: <20211214162050.660953-1-glider@google.com> Message-Id: <20211214162050.660953-10-glider@google.com> Mime-Version: 1.0 References: <20211214162050.660953-1-glider@google.com> X-Mailer: git-send-email 2.34.1.173.g76aa8bc2d0-goog Subject: [PATCH 09/43] kmsan: introduce __no_sanitize_memory and __no_kmsan_checks From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Andrew Morton , Andrey Konovalov , Andy Lutomirski , Ard Biesheuvel , Arnd Bergmann , Borislav Petkov , Christoph Hellwig , Christoph Lameter , David Rientjes , Dmitry Vyukov , Eric Dumazet , Greg Kroah-Hartman , Herbert Xu , Ilya Leoshkevich , Ingo Molnar , Jens Axboe , Joonsoo Kim , Kees Cook , Marco Elver , Matthew Wilcox , "Michael S. Tsirkin" , Pekka Enberg , Peter Zijlstra , Petr Mladek , Steven Rostedt , Thomas Gleixner , Vasily Gorbik , Vegard Nossum , Vlastimil Babka , linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org __no_sanitize_memory is a function attribute that instructs KMSAN to skip a function during instrumentation. This is needed to e.g. implement the noinstr functions. __no_kmsan_checks is a function attribute that makes KMSAN ignore the uninitialized values coming from the function's inputs, and initialize the function's outputs. Functions marked with this attribute can't be inlined into functions not marked with it, and vice versa. __SANITIZE_MEMORY__ is a macro that's defined iff the file is instrumented with KMSAN. This is not the same as CONFIG_KMSAN, which is defined for every file. Signed-off-by: Alexander Potapenko --- Link: https://linux-review.googlesource.com/id/I004ff0360c918d3cd8b18767ddd1381c6d3281be --- include/linux/compiler-clang.h | 23 +++++++++++++++++++++++ include/linux/compiler-gcc.h | 6 ++++++ 2 files changed, 29 insertions(+) diff --git a/include/linux/compiler-clang.h b/include/linux/compiler-clang.h index 3c4de9b6c6e3e..5f11a6f269e28 100644 --- a/include/linux/compiler-clang.h +++ b/include/linux/compiler-clang.h @@ -51,6 +51,29 @@ #define __no_sanitize_undefined #endif +#if __has_feature(memory_sanitizer) +#define __SANITIZE_MEMORY__ +/* + * Unlike other sanitizers, KMSAN still inserts code into functions marked with + * no_sanitize("kernel-memory"). Using disable_sanitizer_instrumentation + * provides the behavior consistent with other __no_sanitize_ attributes, + * guaranteeing that __no_sanitize_memory functions remain uninstrumented. + */ +#define __no_sanitize_memory __disable_sanitizer_instrumentation + +/* + * The __no_kmsan_checks attribute ensures that a function does not produce + * false positive reports by: + * - initializing all local variables and memory stores in this function; + * - skipping all shadow checks; + * - passing initialized arguments to this function's callees. + */ +#define __no_kmsan_checks __attribute__((no_sanitize("kernel-memory"))) +#else +#define __no_sanitize_memory +#define __no_kmsan_checks +#endif + /* * Support for __has_feature(coverage_sanitizer) was added in Clang 13 together * with no_sanitize("coverage"). Prior versions of Clang support coverage diff --git a/include/linux/compiler-gcc.h b/include/linux/compiler-gcc.h index ccbbd31b3aae5..f6e69387aad05 100644 --- a/include/linux/compiler-gcc.h +++ b/include/linux/compiler-gcc.h @@ -129,6 +129,12 @@ #define __SANITIZE_ADDRESS__ #endif +/* + * GCC does not support KMSAN. + */ +#define __no_sanitize_memory +#define __no_kmsan_checks + /* * Turn individual warnings and errors on and off locally, depending * on version. -- 2.34.1.173.g76aa8bc2d0-goog