From patchwork Tue Mar 29 12:39:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12794764 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7606EC433F5 for ; Tue, 29 Mar 2022 12:41:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 850D08D0017; Tue, 29 Mar 2022 08:41:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8269B8D0002; Tue, 29 Mar 2022 08:41:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6F07C8D0017; Tue, 29 Mar 2022 08:41:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.25]) by kanga.kvack.org (Postfix) with ESMTP id 5F7CF8D0002 for ; Tue, 29 Mar 2022 08:41:09 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id 3D2BA81531 for ; Tue, 29 Mar 2022 12:41:09 +0000 (UTC) X-FDA: 79297383858.06.6989EE4 Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) by imf20.hostedemail.com (Postfix) with ESMTP id C20F71C000C for ; Tue, 29 Mar 2022 12:41:08 +0000 (UTC) Received: by mail-ej1-f74.google.com with SMTP id hg13-20020a1709072ccd00b006dfa484ec23so8155750ejc.8 for ; Tue, 29 Mar 2022 05:41:08 -0700 (PDT) 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=FHzHVDPYZMTi95ptCkFXfySj60BESZp+qG5LjWpEp7c=; b=sWW5tRocZxxBnyH5c+5bB9SrN8yBbtlo2fkEMJuym0RIhB42RkiLDf+Mt3MIvJwiyJ 7j0vQ9s012H9UhxvQGmO+OVLPDT4DOl4vdbuAKldQJWeBJMxzqghX+s69XDhPtPnFmSZ ln18qWHYMmGUWOUIknOM0Bl7/4fn3Ax931TwAMeVd4qkvriskK/u0kVEIOnyYuiKjd5B kjzHcwWRbWcIxcxK6/78moEPo1Rs5jsyltN2n8NJo34SDw3L3nYlRBVlVJYFqaAjGTWX /iZyyw30rrol8h6QI3lmyhTL/EJEuNjLY2dUKHqXYZa6vP163p1YbzZ9JXRJ4sZt3h97 4xOQ== 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=FHzHVDPYZMTi95ptCkFXfySj60BESZp+qG5LjWpEp7c=; b=x/9bwEHyFMgP91qG+jLBYBpINXTJiB51a3ZN6pVqjD1DJbKnn9b8aaiIyNYrO3gZjL GZyLThc+RK+WY4f5UbSvhUkLa2S6UIhr5T2xoyqdtmqQ9ai7zuv5s37x7PFiADh6eMkN HwOQ6OEEdbRL3p5sLWXdXVla33ZeZLMD3GyicMm2n0heo/sbHE5dpfOCYNrGsdJTVvRk VdvjgW0QID8hZfwmwLdCnttXR0in7EDohEIebqIltw962Trdx/bsWlXhn8mN4ABDyX5a bFo7wEczUOyJkdjR5SmqUTV7Ei9215KJbd78usxhQbQXYbjQ/jL4aRMnUJbrHaCssDkp atjA== X-Gm-Message-State: AOAM533zpcDM5DpDickm7nhSYj78HadpBAkQ0gdtYw3ctieuaGgzum6Z KNbUYoPID9KAlDxGpJp1aqCQF9nhg3A= X-Google-Smtp-Source: ABdhPJxIjE0+jxZBSo1pLmOTWSGay+gWDn7tl9m8Nl4sQMVHZ9JOsMlLrJDRqSA80Wdi3w3BwjBTXdlkc28= X-Received: from glider.muc.corp.google.com ([2a00:79e0:15:13:36eb:759:798f:98c3]) (user=glider job=sendgmr) by 2002:a17:906:a1d3:b0:6d0:80ea:2fde with SMTP id bx19-20020a170906a1d300b006d080ea2fdemr33544446ejb.344.1648557667490; Tue, 29 Mar 2022 05:41:07 -0700 (PDT) Date: Tue, 29 Mar 2022 14:39:43 +0200 In-Reply-To: <20220329124017.737571-1-glider@google.com> Message-Id: <20220329124017.737571-15-glider@google.com> Mime-Version: 1.0 References: <20220329124017.737571-1-glider@google.com> X-Mailer: git-send-email 2.35.1.1021.g381101b075-goog Subject: [PATCH v2 14/48] kmsan: implement kmsan_init(), initialize READ_ONCE_NOCHECK() From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Andrew Morton , Andrey Konovalov , Andy Lutomirski , 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 , Mark Rutland , 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 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: C20F71C000C X-Rspam-User: Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=sWW5tRoc; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf20.hostedemail.com: domain of 3Y_5CYgYKCIQotqlmzowwotm.kwutqv25-uus3iks.wzo@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3Y_5CYgYKCIQotqlmzowwotm.kwutqv25-uus3iks.wzo@flex--glider.bounces.google.com X-Stat-Signature: atpubf4ax96mnd5zjshpgqfadxpg8iee X-HE-Tag: 1648557668-15993 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: kmsan_init() is a macro that takes a possibly uninitialized value and returns an initialized value of the same type. It can be used e.g. in cases when a value comes from non-instrumented code to avoid false positive reports. In particular, we use kmsan_init() in READ_ONCE_NOCHECK() so that it returns initialized values. This helps defeat false positives e.g. from leftover stack contents accessed by stack unwinders. Signed-off-by: Alexander Potapenko --- Link: https://linux-review.googlesource.com/id/Icd1260073666f944922f031bfb6762379ba1fa38 --- include/asm-generic/rwonce.h | 5 +++-- include/linux/kmsan-checks.h | 40 ++++++++++++++++++++++++++++++++++++ mm/kmsan/Makefile | 5 ++++- mm/kmsan/annotations.c | 28 +++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 mm/kmsan/annotations.c diff --git a/include/asm-generic/rwonce.h b/include/asm-generic/rwonce.h index 8d0a6280e9824..7cf993af8e1ea 100644 --- a/include/asm-generic/rwonce.h +++ b/include/asm-generic/rwonce.h @@ -25,6 +25,7 @@ #include #include #include +#include /* * Yes, this permits 64-bit accesses on 32-bit architectures. These will @@ -69,14 +70,14 @@ unsigned long __read_once_word_nocheck(const void *addr) /* * Use READ_ONCE_NOCHECK() instead of READ_ONCE() if you need to load a - * word from memory atomically but without telling KASAN/KCSAN. This is + * word from memory atomically but without telling KASAN/KCSAN/KMSAN. This is * usually used by unwinding code when walking the stack of a running process. */ #define READ_ONCE_NOCHECK(x) \ ({ \ compiletime_assert(sizeof(x) == sizeof(unsigned long), \ "Unsupported access size for READ_ONCE_NOCHECK()."); \ - (typeof(x))__read_once_word_nocheck(&(x)); \ + kmsan_init((typeof(x))__read_once_word_nocheck(&(x))); \ }) static __no_kasan_or_inline diff --git a/include/linux/kmsan-checks.h b/include/linux/kmsan-checks.h index a6522a0c28df9..ecd8336190fc0 100644 --- a/include/linux/kmsan-checks.h +++ b/include/linux/kmsan-checks.h @@ -14,6 +14,44 @@ #ifdef CONFIG_KMSAN +/* + * Helper functions that mark the return value initialized. + * See mm/kmsan/annotations.c. + */ +u8 kmsan_init_1(u8 value); +u16 kmsan_init_2(u16 value); +u32 kmsan_init_4(u32 value); +u64 kmsan_init_8(u64 value); + +static inline void *kmsan_init_ptr(void *ptr) +{ + return (void *)kmsan_init_8((u64)ptr); +} + +static inline char kmsan_init_char(char value) +{ + return (u8)kmsan_init_1((u8)value); +} + +#define __decl_kmsan_init_type(type, fn) unsigned type : fn, signed type : fn + +/** + * kmsan_init - Make the value initialized. + * @val: 1-, 2-, 4- or 8-byte integer that may be treated as uninitialized by + * KMSAN. + * + * Return: value of @val that KMSAN treats as initialized. + */ +#define kmsan_init(val) \ + ( \ + (typeof(val))(_Generic((val), \ + __decl_kmsan_init_type(char, kmsan_init_1), \ + __decl_kmsan_init_type(short, kmsan_init_2), \ + __decl_kmsan_init_type(int, kmsan_init_4), \ + __decl_kmsan_init_type(long, kmsan_init_8), \ + char : kmsan_init_char, \ + void * : kmsan_init_ptr)(val))) + /** * kmsan_poison_memory() - Mark the memory range as uninitialized. * @address: address to start with. @@ -48,6 +86,8 @@ void kmsan_check_memory(const void *address, size_t size); #else +#define kmsan_init(value) (value) + static inline void kmsan_poison_memory(const void *address, size_t size, gfp_t flags) { diff --git a/mm/kmsan/Makefile b/mm/kmsan/Makefile index a80dde1de7048..73b705cbf75b9 100644 --- a/mm/kmsan/Makefile +++ b/mm/kmsan/Makefile @@ -1,9 +1,11 @@ -obj-y := core.o instrumentation.o hooks.o report.o shadow.o +obj-y := core.o instrumentation.o hooks.o report.o shadow.o annotations.o KMSAN_SANITIZE := n KCOV_INSTRUMENT := n UBSAN_SANITIZE := n +KMSAN_SANITIZE_kmsan_annotations.o := y + # Disable instrumentation of KMSAN runtime with other tools. CC_FLAGS_KMSAN_RUNTIME := -fno-stack-protector CC_FLAGS_KMSAN_RUNTIME += $(call cc-option,-fno-conserve-stack) @@ -11,6 +13,7 @@ CC_FLAGS_KMSAN_RUNTIME += -DDISABLE_BRANCH_PROFILING CFLAGS_REMOVE.o = $(CC_FLAGS_FTRACE) +CFLAGS_annotations.o := $(CC_FLAGS_KMSAN_RUNTIME) CFLAGS_core.o := $(CC_FLAGS_KMSAN_RUNTIME) CFLAGS_hooks.o := $(CC_FLAGS_KMSAN_RUNTIME) CFLAGS_instrumentation.o := $(CC_FLAGS_KMSAN_RUNTIME) diff --git a/mm/kmsan/annotations.c b/mm/kmsan/annotations.c new file mode 100644 index 0000000000000..8ccde90bcd12b --- /dev/null +++ b/mm/kmsan/annotations.c @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KMSAN annotations. + * + * The kmsan_init_SIZE functions reside in a separate translation unit to + * prevent inlining them. Clang may inline functions marked with + * __no_sanitize_memory attribute into functions without it, which effectively + * results in ignoring the attribute. + * + * Copyright (C) 2017-2022 Google LLC + * Author: Alexander Potapenko + * + */ + +#include +#include + +#define DECLARE_KMSAN_INIT(size, t) \ + __no_sanitize_memory t kmsan_init_##size(t value) \ + { \ + return value; \ + } \ + EXPORT_SYMBOL(kmsan_init_##size) + +DECLARE_KMSAN_INIT(1, u8); +DECLARE_KMSAN_INIT(2, u16); +DECLARE_KMSAN_INIT(4, u32); +DECLARE_KMSAN_INIT(8, u64);