From patchwork Fri Aug 26 15:07:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956174 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 55119ECAAD5 for ; Fri, 26 Aug 2022 15:08:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E4F77940009; Fri, 26 Aug 2022 11:08:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E09B7940007; Fri, 26 Aug 2022 11:08:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C2BB2940009; Fri, 26 Aug 2022 11:08:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id B0AB0940007 for ; Fri, 26 Aug 2022 11:08:17 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 8DD61120F4B for ; Fri, 26 Aug 2022 15:08:17 +0000 (UTC) X-FDA: 79842074634.21.BE3AA26 Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) by imf22.hostedemail.com (Postfix) with ESMTP id 2F38BC002D for ; Fri, 26 Aug 2022 15:08:17 +0000 (UTC) Received: by mail-ej1-f73.google.com with SMTP id ho13-20020a1709070e8d00b00730a655e173so720491ejc.8 for ; Fri, 26 Aug 2022 08:08:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=J3VjO5owb76kN3paB0OLHywjIHrYCcgYmjupWIyAPgw=; b=aCsHGgV2sJxTYt+AJn+GIymUxqtSkf7JOSyz7vtwxv7vS0L3uEaVpG7DpI3UZFn7Rf D7zIWrIK46DZU/rRMq3m92P1QW307EPb+BIJOkzmXqQ8PCl2pATj4E2Z1WkltcOGRGH6 X4s+ctoBmXCp+cOHvURIAa7E0w3KL/dAIMuJ3L8QZdm/NfQUl1dcR/AaILFXFY/SbQCL 36XR8pcZkoIw/0JS2OLp31Xw05XYV+OvrpL2omMECtGFmMj4DIIeOo98maVmQrFF8Xkl zgX7H+3jSLRXR+rJBrs8fyG/5arariUD9UxBlfK0i9oPhMg0ntqpE7GJ0QJEkhQTjoVw jDPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=J3VjO5owb76kN3paB0OLHywjIHrYCcgYmjupWIyAPgw=; b=NTmjqlmnaH7NQVML8p2DMLTY6UUX5bGnOZ+uYZjHdBfi1qd8lOTgVi1HrWw3lbfLdE 5OtMzngwmUdvJf1Huw+gHxFmn9+1i4YrH6WbXfxdqQMjHOkEZdKaBkPXbOZjxJV6BEtC 116zpOwKy8HlG4styvNcqEBXdXkZ/iQJabRiP4zBQDZpicEmXSCCCk/P2NO4cARSeGS2 c9s8dL/iwj8tf31HBM23dnCRRsWr4kuGatf+JnxpfAJemysRRVNuQL6jtklSso+r7Lzh PVAZtcAr203GIiq7+N8Up0ezapTgXMLR6l45LT9tB/3+bZ+2LzkRYjFVWdZhXYwwITVk f+AA== X-Gm-Message-State: ACgBeo1VvB4fVAy/fvTVqUw0KeJDwCNG+S7eht7DTM36eV7NpZB+mrBz Nea+DTC2MqQi+sBo3Qo93uQ3DyRY+MA= X-Google-Smtp-Source: AA6agR6nNKRDoPy16VFGdamdWo+M+pvpnHCU2sYnAni0z8NRtgZc0jeuf/gSF+/K7FUxMeTnWWm/3XJf1sI= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:aa7:c611:0:b0:447:844d:e5a2 with SMTP id h17-20020aa7c611000000b00447844de5a2mr7350187edq.10.1661526495846; Fri, 26 Aug 2022 08:08:15 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:24 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-2-glider@google.com> Subject: [PATCH v5 01/44] x86: add missing include to sparsemem.h From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526497; a=rsa-sha256; cv=none; b=jErZaHiKezvwNX9v1ffh8V9cckFE5IdwbM1RTZEHRrGAFteH7rsA8eOwvzcLv03aTG4Wft 01aaTuDNx13rphjt0Vw3Wk4D7OctrJ5J2ow8z5+ddH2BLnwmikYBN3ghA/sKABwgetcFzi +vPwz5W20Nh4MFFI7j3tyyyzEz5R2Aw= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=aCsHGgV2; spf=pass (imf22.hostedemail.com: domain of 33-EIYwYKCOQMROJKXMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=33-EIYwYKCOQMROJKXMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526497; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=J3VjO5owb76kN3paB0OLHywjIHrYCcgYmjupWIyAPgw=; b=F6kN1dqd+IvNDy0RgmEzmo3YBabquhTkfgIhtjAOMLX6hVBKJPEH+jY95T9SX+wQs40E/3 Y1aE6Rjs4hFITQz1m0xRkExaTn4WtIHt5+W0R/TZSGaw9DZOm4kSOp7Ycl+zSIyBMOIpaP RPiBR6i0HeYF5jI/YJ46fgqXsKNkcss= X-Rspam-User: Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=aCsHGgV2; spf=pass (imf22.hostedemail.com: domain of 33-EIYwYKCOQMROJKXMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=33-EIYwYKCOQMROJKXMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam08 X-Stat-Signature: ua9qrkk5r18rszqmqbjezstey8qj9f58 X-Rspamd-Queue-Id: 2F38BC002D X-HE-Tag: 1661526497-149962 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: From: Dmitry Vyukov Including sparsemem.h from other files (e.g. transitively via asm/pgtable_64_types.h) results in compilation errors due to unknown types: sparsemem.h:34:32: error: unknown type name 'phys_addr_t' extern int phys_to_target_node(phys_addr_t start); ^ sparsemem.h:36:39: error: unknown type name 'u64' extern int memory_add_physaddr_to_nid(u64 start); ^ Fix these errors by including linux/types.h from sparsemem.h This is required for the upcoming KMSAN patches. Signed-off-by: Dmitry Vyukov Signed-off-by: Alexander Potapenko --- Link: https://linux-review.googlesource.com/id/Ifae221ce85d870d8f8d17173bd44d5cf9be2950f --- arch/x86/include/asm/sparsemem.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/x86/include/asm/sparsemem.h b/arch/x86/include/asm/sparsemem.h index 6a9ccc1b2be5d..64df897c0ee30 100644 --- a/arch/x86/include/asm/sparsemem.h +++ b/arch/x86/include/asm/sparsemem.h @@ -2,6 +2,8 @@ #ifndef _ASM_X86_SPARSEMEM_H #define _ASM_X86_SPARSEMEM_H +#include + #ifdef CONFIG_SPARSEMEM /* * generic non-linear memory support: From patchwork Fri Aug 26 15:07:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956175 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 5B9A0ECAAD4 for ; Fri, 26 Aug 2022 15:08:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EC52494000A; Fri, 26 Aug 2022 11:08:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E4ED7940007; Fri, 26 Aug 2022 11:08:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CA07B94000A; Fri, 26 Aug 2022 11:08:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id B76EC940007 for ; Fri, 26 Aug 2022 11:08:21 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 8E57C40E67 for ; Fri, 26 Aug 2022 15:08:21 +0000 (UTC) X-FDA: 79842074802.21.EB1AF56 Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf27.hostedemail.com (Postfix) with ESMTP id 46BCC40034 for ; Fri, 26 Aug 2022 15:08:20 +0000 (UTC) Received: by mail-ed1-f73.google.com with SMTP id w17-20020a056402269100b0043da2189b71so1225253edd.6 for ; Fri, 26 Aug 2022 08:08:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=omcyQ1o+RVSxzRVZJZY/qms7uBp4WMcvPZ8eVf62Bcs=; b=RA3+jZoNkZ6/iwxrpSVqaEjBMYHmdlwKi/r0P/ahW5EfxcEw95Fn/Idn7YL4qWBzll SY0uIX1NPhBlO4/v7JGD0C0GkUj5lA5653SGiEmNx4TDtgxGUvAKdSJ1fWOgyi06z/DG shPVR/vk4/TLqx/3mkmush3fs6dqtOR0yJmc+Nmy6aUkZqpT99d7lDvu0GcomBqNMqyN 19J1+veiw9TqSFJT2q2s9xkLUwnPBJrzd2UWFkMwuPWi4IOaRL2IjhrBf3Js6HDjqwHq CmvYYiW1TiqGPg7RFvpwRKn+givLPrkzFJ3DaBqEgQYzQViRUdbgNutzUbpd5CLnFdln k03w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=omcyQ1o+RVSxzRVZJZY/qms7uBp4WMcvPZ8eVf62Bcs=; b=8Q8e1mvfJnZHKncurgdiv70HDZP9ChOSrhg2jDq8rT2rxw+klfRC85HKjvKerP/6Pg 7wY0PmgOVm8e0L5QZS0DB2gPxAFfQP2CRuOnWJpTEVyy3LxBA9hqntj/Z8f+7hu8IjhO ziEV/D7jRYaAnOo7/oHJS31g1aBCBEPuF6DP45CDWEc/ZorS0RQ4SZuXyZLGjbgtTGbS mbgxRXvSLEFhhUzjV3XzPWOv30xhYvCVjKAtYExdk5WM3cUuPT8HHjEDc/7ETl+VhqY7 nQUfn+/Rd89Or7L8ZcBxa4taldy3WjG6XwqCto9ZXTJ5QLTUvxwpe96qL7HLS57DqHNi FKdA== X-Gm-Message-State: ACgBeo2exC4nySVDLTz5amI7/NR69ZxZXbjNbhV2iCpoFu0tyvcWPRA6 sZuIxVfiGwo2Zgi0TEvv69DYTEhTi1Q= X-Google-Smtp-Source: AA6agR6A/llRpE6oXEfhGAAL2j75O1MUVXyWFGmAFUP5rzL7sdADsGx3VSQrZnp9gBHmtwE7SwLSgksGbo8= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a05:6402:270a:b0:446:c9e9:6e00 with SMTP id y10-20020a056402270a00b00446c9e96e00mr6913543edd.315.1661526498732; Fri, 26 Aug 2022 08:08:18 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:25 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-3-glider@google.com> Subject: [PATCH v5 02/44] stackdepot: reserve 5 extra bits in depot_stack_handle_t From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526500; a=rsa-sha256; cv=none; b=rIjihUD02U6nL7/BSHNLrtkrsMKa7VB+x8H8BEetqtHHASufflsyTDsM6SVUpF5s3cORvA CDF322/sj3SiRwQjcmuzYyt5D902tmbdjEMdUhlZcNztAV51oVAbZKsPVsCrgDtTcSAyYD f/5kzhfs3AHMDaDRwgHYP5iRdxPBH/M= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=RA3+jZoN; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf27.hostedemail.com: domain of 34uEIYwYKCOcPURMNaPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=34uEIYwYKCOcPURMNaPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526500; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=omcyQ1o+RVSxzRVZJZY/qms7uBp4WMcvPZ8eVf62Bcs=; b=jHvtrDWSLiJX/kl0dRPfO4qiXNdg5Fp+AM9qevC7CpbYAxqx/wKlm6FhZPV0aWUBLkJGVC 7Y+wuTqHU+pDK3+5zmfMQVp/2taIdozLjDm5Toav0fUjFLJM5ynTQmN0I7M668iv126zKT 6ks/6qCOXZVrGXc7VJ7675EoHD52Wmk= Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=RA3+jZoN; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf27.hostedemail.com: domain of 34uEIYwYKCOcPURMNaPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=34uEIYwYKCOcPURMNaPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--glider.bounces.google.com X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: ei7cbg4jga8gat336jjcy54j8u9ui7ye X-Rspamd-Queue-Id: 46BCC40034 X-HE-Tag: 1661526500-344548 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: Some users (currently only KMSAN) may want to use spare bits in depot_stack_handle_t. Let them do so by adding @extra_bits to __stack_depot_save() to store arbitrary flags, and providing stack_depot_get_extra_bits() to retrieve those flags. Also adapt KASAN to the new prototype by passing extra_bits=0, as KASAN does not intend to store additional information in the stack handle. Signed-off-by: Alexander Potapenko Reviewed-by: Marco Elver --- v4: -- per Marco Elver's request, fold "kasan: common: adapt to the new prototype of __stack_depot_save()" into this patch to prevent bisection breakages. Link: https://linux-review.googlesource.com/id/I0587f6c777667864768daf07821d594bce6d8ff9 --- include/linux/stackdepot.h | 8 ++++++++ lib/stackdepot.c | 29 ++++++++++++++++++++++++----- mm/kasan/common.c | 2 +- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/include/linux/stackdepot.h b/include/linux/stackdepot.h index bc2797955de90..9ca7798d7a318 100644 --- a/include/linux/stackdepot.h +++ b/include/linux/stackdepot.h @@ -14,9 +14,15 @@ #include typedef u32 depot_stack_handle_t; +/* + * Number of bits in the handle that stack depot doesn't use. Users may store + * information in them. + */ +#define STACK_DEPOT_EXTRA_BITS 5 depot_stack_handle_t __stack_depot_save(unsigned long *entries, unsigned int nr_entries, + unsigned int extra_bits, gfp_t gfp_flags, bool can_alloc); /* @@ -59,6 +65,8 @@ depot_stack_handle_t stack_depot_save(unsigned long *entries, unsigned int stack_depot_fetch(depot_stack_handle_t handle, unsigned long **entries); +unsigned int stack_depot_get_extra_bits(depot_stack_handle_t handle); + int stack_depot_snprint(depot_stack_handle_t handle, char *buf, size_t size, int spaces); diff --git a/lib/stackdepot.c b/lib/stackdepot.c index e73fda23388d8..79e894cf84064 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -43,7 +43,8 @@ #define STACK_ALLOC_OFFSET_BITS (STACK_ALLOC_ORDER + PAGE_SHIFT - \ STACK_ALLOC_ALIGN) #define STACK_ALLOC_INDEX_BITS (DEPOT_STACK_BITS - \ - STACK_ALLOC_NULL_PROTECTION_BITS - STACK_ALLOC_OFFSET_BITS) + STACK_ALLOC_NULL_PROTECTION_BITS - \ + STACK_ALLOC_OFFSET_BITS - STACK_DEPOT_EXTRA_BITS) #define STACK_ALLOC_SLABS_CAP 8192 #define STACK_ALLOC_MAX_SLABS \ (((1LL << (STACK_ALLOC_INDEX_BITS)) < STACK_ALLOC_SLABS_CAP) ? \ @@ -56,6 +57,7 @@ union handle_parts { u32 slabindex : STACK_ALLOC_INDEX_BITS; u32 offset : STACK_ALLOC_OFFSET_BITS; u32 valid : STACK_ALLOC_NULL_PROTECTION_BITS; + u32 extra : STACK_DEPOT_EXTRA_BITS; }; }; @@ -77,6 +79,14 @@ static int next_slab_inited; static size_t depot_offset; static DEFINE_RAW_SPINLOCK(depot_lock); +unsigned int stack_depot_get_extra_bits(depot_stack_handle_t handle) +{ + union handle_parts parts = { .handle = handle }; + + return parts.extra; +} +EXPORT_SYMBOL(stack_depot_get_extra_bits); + static bool init_stack_slab(void **prealloc) { if (!*prealloc) @@ -140,6 +150,7 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) stack->handle.slabindex = depot_index; stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN; stack->handle.valid = 1; + stack->handle.extra = 0; memcpy(stack->entries, entries, flex_array_size(stack, entries, size)); depot_offset += required_size; @@ -382,6 +393,7 @@ EXPORT_SYMBOL_GPL(stack_depot_fetch); * * @entries: Pointer to storage array * @nr_entries: Size of the storage array + * @extra_bits: Flags to store in unused bits of depot_stack_handle_t * @alloc_flags: Allocation gfp flags * @can_alloc: Allocate stack slabs (increased chance of failure if false) * @@ -393,6 +405,10 @@ EXPORT_SYMBOL_GPL(stack_depot_fetch); * If the stack trace in @entries is from an interrupt, only the portion up to * interrupt entry is saved. * + * Additional opaque flags can be passed in @extra_bits, stored in the unused + * bits of the stack handle, and retrieved using stack_depot_get_extra_bits() + * without calling stack_depot_fetch(). + * * Context: Any context, but setting @can_alloc to %false is required if * alloc_pages() cannot be used from the current context. Currently * this is the case from contexts where neither %GFP_ATOMIC nor @@ -402,10 +418,11 @@ EXPORT_SYMBOL_GPL(stack_depot_fetch); */ depot_stack_handle_t __stack_depot_save(unsigned long *entries, unsigned int nr_entries, + unsigned int extra_bits, gfp_t alloc_flags, bool can_alloc) { struct stack_record *found = NULL, **bucket; - depot_stack_handle_t retval = 0; + union handle_parts retval = { .handle = 0 }; struct page *page = NULL; void *prealloc = NULL; unsigned long flags; @@ -489,9 +506,11 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, free_pages((unsigned long)prealloc, STACK_ALLOC_ORDER); } if (found) - retval = found->handle.handle; + retval.handle = found->handle.handle; fast_exit: - return retval; + retval.extra = extra_bits; + + return retval.handle; } EXPORT_SYMBOL_GPL(__stack_depot_save); @@ -511,6 +530,6 @@ depot_stack_handle_t stack_depot_save(unsigned long *entries, unsigned int nr_entries, gfp_t alloc_flags) { - return __stack_depot_save(entries, nr_entries, alloc_flags, true); + return __stack_depot_save(entries, nr_entries, 0, alloc_flags, true); } EXPORT_SYMBOL_GPL(stack_depot_save); diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 69f583855c8be..94caa2d46a327 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -36,7 +36,7 @@ depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc) unsigned int nr_entries; nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0); - return __stack_depot_save(entries, nr_entries, flags, can_alloc); + return __stack_depot_save(entries, nr_entries, 0, flags, can_alloc); } void kasan_set_track(struct kasan_track *track, gfp_t flags) From patchwork Fri Aug 26 15:07:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956176 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 33800ECAAA3 for ; Fri, 26 Aug 2022 15:08:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C06B194000B; Fri, 26 Aug 2022 11:08:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B8F86940007; Fri, 26 Aug 2022 11:08:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9BA0194000B; Fri, 26 Aug 2022 11:08:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 8A9FD940007 for ; Fri, 26 Aug 2022 11:08:23 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 63A8BA0F9A for ; Fri, 26 Aug 2022 15:08:23 +0000 (UTC) X-FDA: 79842074886.01.023E1CA Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf06.hostedemail.com (Postfix) with ESMTP id 1430C180011 for ; Fri, 26 Aug 2022 15:08:22 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id b12-20020a056402278c00b00447f2029741so1133517ede.23 for ; Fri, 26 Aug 2022 08:08:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=BPAgteciaEShNCu1utOx4VHGX1XRyq5ObvUAakfxq7k=; b=jCaizTYg1qMmJ26neRflpmyoGoKSSzwoQ3ghp5ED6sQ0QTlwBDX6eGH4Q1incOUpRf A9evOUHUN2DRNN3KIY+fweMjD/DZ4xlbZu00h6mBm12fvHomrnz+0lpoebslTIvGgTe+ gdtLap2UAeAYuweKYtWNFnZCFeo+7QmPjS4mEY0fqEBA+wTuqFT0Ebh7UvC69tA0VceO U+A1VATc2FKIdkK4YFsc/hZEovB4eynb2DvJ5fjLkaN1eoq9/dzW4ni3/SQJWfAyRBQ+ mWKfQjkLkiWrUYhGM1fBmA4YzoA+IjtGx7XKjxo7S8MYC3SRxlNpW94EGzvpucAhGMMX rdcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=BPAgteciaEShNCu1utOx4VHGX1XRyq5ObvUAakfxq7k=; b=3hO0iBaHAd7kLYlYw5Jkfkj88j+fC+l4FMPY8cZjD8g8H28vxT6TU+pwHmA+D9CwBZ jhH9VUflGvvBR2/ckZbw7czmc2BpO7PNooVE6sldcyf+Qsc6lv09RwWSKfTNTS5Po/nH WJiLE3NYMfi3Uovb7/7OUdLIfH9xqbfMF+P5D0flUT2lk3ImdR0fdjk86jbsj2EKoR7C u8uvMdXnVxd1shX+Nzy4RDlyG7tTpPT/Xv4VZpz5SVbAZlHVlofD1lvi7Rgq/wMmbDpN eFziAoChS7vTC/HoLUuME3KbMvBSvecja1iQ1L7dLex2U79BXytO+nqAgvi/H6vUfiuT JOVw== X-Gm-Message-State: ACgBeo17fiqrSROsypehFN+s5TOkyD84U3LTRRTCxRKxGMQulTxC0aeI fhTjSn089ALPZwa8DXZ2qqZiJR6p4zk= X-Google-Smtp-Source: AA6agR7KbGTD+Yt7qgz7f9gG2VmjSlBnG9STKzG8jXRNjHiFbuQJEvlbRex0Fv49kcKUtGLpmFtNC/Gl7xY= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a17:907:da0:b0:730:d0ba:7b13 with SMTP id go32-20020a1709070da000b00730d0ba7b13mr6038874ejc.332.1661526501538; Fri, 26 Aug 2022 08:08:21 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:26 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-4-glider@google.com> Subject: [PATCH v5 03/44] instrumented.h: allow instrumenting both sides of copy_from_user() From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=jCaizTYg; spf=pass (imf06.hostedemail.com: domain of 35eEIYwYKCOoSXUPQdSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=35eEIYwYKCOoSXUPQdSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526503; a=rsa-sha256; cv=none; b=ecdFJYxeuBD7fvPglsBFGNmGUKkpaSaWbMCcVsmcA8EF6Pq1dTNmhUyK6bVo0xml+wP3tB IfW0dv2xFzRhRZfTm0RYcRxeev5YukXTapyxTmx6xDn7OyCja+J614bHzn3jobwDYlAAGJ J3mESqZhJFuYQiM1q9AOj4NTbrN6P7w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526503; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=BPAgteciaEShNCu1utOx4VHGX1XRyq5ObvUAakfxq7k=; b=ouAlQlGuAE2WtqymlPWk3xvyrCgw3WT+cNEAPlk4zRhA+zLjIalhynUU/iRI12c0uwmVE+ X99ob09h9+lerK5lGPKREp8sLg06cnSISO3HsVKgT+sf/KQkliPJRnC7+vna6fUzGFYpDj 3gL3z8fdICnLQ7eFICIQnSLlIom1BfY= X-Rspam-User: Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=jCaizTYg; spf=pass (imf06.hostedemail.com: domain of 35eEIYwYKCOoSXUPQdSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=35eEIYwYKCOoSXUPQdSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam07 X-Stat-Signature: qgyd6h3mbezfuixpkz9othy11tgsc9kr X-Rspamd-Queue-Id: 1430C180011 X-HE-Tag: 1661526502-978766 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: Introduce instrument_copy_from_user_before() and instrument_copy_from_user_after() hooks to be invoked before and after the call to copy_from_user(). KASAN and KCSAN will be only using instrument_copy_from_user_before(), but for KMSAN we'll need to insert code after copy_from_user(). Signed-off-by: Alexander Potapenko Reviewed-by: Marco Elver --- v4: -- fix _copy_from_user_key() in arch/s390/lib/uaccess.c (Reported-by: kernel test robot ) Link: https://linux-review.googlesource.com/id/I855034578f0b0f126734cbd734fb4ae1d3a6af99 --- arch/s390/lib/uaccess.c | 3 ++- include/linux/instrumented.h | 21 +++++++++++++++++++-- include/linux/uaccess.h | 19 ++++++++++++++----- lib/iov_iter.c | 9 ++++++--- lib/usercopy.c | 3 ++- 5 files changed, 43 insertions(+), 12 deletions(-) diff --git a/arch/s390/lib/uaccess.c b/arch/s390/lib/uaccess.c index d7b3b193d1088..58033dfcb6d45 100644 --- a/arch/s390/lib/uaccess.c +++ b/arch/s390/lib/uaccess.c @@ -81,8 +81,9 @@ unsigned long _copy_from_user_key(void *to, const void __user *from, might_fault(); if (!should_fail_usercopy()) { - instrument_copy_from_user(to, from, n); + instrument_copy_from_user_before(to, from, n); res = raw_copy_from_user_key(to, from, n, key); + instrument_copy_from_user_after(to, from, n, res); } if (unlikely(res)) memset(to + (n - res), 0, res); diff --git a/include/linux/instrumented.h b/include/linux/instrumented.h index 42faebbaa202a..ee8f7d17d34f5 100644 --- a/include/linux/instrumented.h +++ b/include/linux/instrumented.h @@ -120,7 +120,7 @@ instrument_copy_to_user(void __user *to, const void *from, unsigned long n) } /** - * instrument_copy_from_user - instrument writes of copy_from_user + * instrument_copy_from_user_before - add instrumentation before copy_from_user * * Instrument writes to kernel memory, that are due to copy_from_user (and * variants). The instrumentation should be inserted before the accesses. @@ -130,10 +130,27 @@ instrument_copy_to_user(void __user *to, const void *from, unsigned long n) * @n number of bytes to copy */ static __always_inline void -instrument_copy_from_user(const void *to, const void __user *from, unsigned long n) +instrument_copy_from_user_before(const void *to, const void __user *from, unsigned long n) { kasan_check_write(to, n); kcsan_check_write(to, n); } +/** + * instrument_copy_from_user_after - add instrumentation after copy_from_user + * + * Instrument writes to kernel memory, that are due to copy_from_user (and + * variants). The instrumentation should be inserted after the accesses. + * + * @to destination address + * @from source address + * @n number of bytes to copy + * @left number of bytes not copied (as returned by copy_from_user) + */ +static __always_inline void +instrument_copy_from_user_after(const void *to, const void __user *from, + unsigned long n, unsigned long left) +{ +} + #endif /* _LINUX_INSTRUMENTED_H */ diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h index 47e5d374c7ebe..afb18f198843b 100644 --- a/include/linux/uaccess.h +++ b/include/linux/uaccess.h @@ -58,20 +58,28 @@ static __always_inline __must_check unsigned long __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n) { - instrument_copy_from_user(to, from, n); + unsigned long res; + + instrument_copy_from_user_before(to, from, n); check_object_size(to, n, false); - return raw_copy_from_user(to, from, n); + res = raw_copy_from_user(to, from, n); + instrument_copy_from_user_after(to, from, n, res); + return res; } static __always_inline __must_check unsigned long __copy_from_user(void *to, const void __user *from, unsigned long n) { + unsigned long res; + might_fault(); + instrument_copy_from_user_before(to, from, n); if (should_fail_usercopy()) return n; - instrument_copy_from_user(to, from, n); check_object_size(to, n, false); - return raw_copy_from_user(to, from, n); + res = raw_copy_from_user(to, from, n); + instrument_copy_from_user_after(to, from, n, res); + return res; } /** @@ -115,8 +123,9 @@ _copy_from_user(void *to, const void __user *from, unsigned long n) unsigned long res = n; might_fault(); if (!should_fail_usercopy() && likely(access_ok(from, n))) { - instrument_copy_from_user(to, from, n); + instrument_copy_from_user_before(to, from, n); res = raw_copy_from_user(to, from, n); + instrument_copy_from_user_after(to, from, n, res); } if (unlikely(res)) memset(to + (n - res), 0, res); diff --git a/lib/iov_iter.c b/lib/iov_iter.c index 4b7fce72e3e52..c3ca28ca68a65 100644 --- a/lib/iov_iter.c +++ b/lib/iov_iter.c @@ -174,13 +174,16 @@ static int copyout(void __user *to, const void *from, size_t n) static int copyin(void *to, const void __user *from, size_t n) { + size_t res = n; + if (should_fail_usercopy()) return n; if (access_ok(from, n)) { - instrument_copy_from_user(to, from, n); - n = raw_copy_from_user(to, from, n); + instrument_copy_from_user_before(to, from, n); + res = raw_copy_from_user(to, from, n); + instrument_copy_from_user_after(to, from, n, res); } - return n; + return res; } static inline struct pipe_buffer *pipe_buf(const struct pipe_inode_info *pipe, diff --git a/lib/usercopy.c b/lib/usercopy.c index 7413dd300516e..1505a52f23a01 100644 --- a/lib/usercopy.c +++ b/lib/usercopy.c @@ -12,8 +12,9 @@ unsigned long _copy_from_user(void *to, const void __user *from, unsigned long n unsigned long res = n; might_fault(); if (!should_fail_usercopy() && likely(access_ok(from, n))) { - instrument_copy_from_user(to, from, n); + instrument_copy_from_user_before(to, from, n); res = raw_copy_from_user(to, from, n); + instrument_copy_from_user_after(to, from, n, res); } if (unlikely(res)) memset(to + (n - res), 0, res); From patchwork Fri Aug 26 15:07:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956177 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 E1FC1ECAAA3 for ; Fri, 26 Aug 2022 15:08:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8362294000C; Fri, 26 Aug 2022 11:08:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E666940007; Fri, 26 Aug 2022 11:08:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6397194000C; Fri, 26 Aug 2022 11:08:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 4BC8F940007 for ; Fri, 26 Aug 2022 11:08:26 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 28A4EA0EC4 for ; Fri, 26 Aug 2022 15:08:26 +0000 (UTC) X-FDA: 79842075012.10.9C12E50 Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) by imf07.hostedemail.com (Postfix) with ESMTP id C4AB64001D for ; Fri, 26 Aug 2022 15:08:25 +0000 (UTC) Received: by mail-ej1-f73.google.com with SMTP id js11-20020a17090797cb00b00730d73eac83so716787ejc.19 for ; Fri, 26 Aug 2022 08:08:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=H9Td7/TMrTAtAVeUrHIrhZSexZsjcdbUinsVZHg+GW4=; b=lGhpRwPTwgy57+sACo10KJOabLS+OzoRYCdrY6/DZ69yKQ32Xcl4893L9mHNBXiHX+ FXTMwAj8o5xEg0UQH1YWvGS2c14JObIHuzS9Q+Cy8Ew9WrtHNB0uH/PCp25eq3/bGv9N RyTCwexA+hu/WiPHftg+7pBz23LUJRMDEYENkvxDYKktsmwZX/r78KtNbrMpmA6VSyhx v3vpLI2jMLbp9/vcMTRt+gZg2hx0sZJLdiHzG1SemMYm6jlEasuaIKQcHRhO6rJsuS08 pazvWhVpuAX2fMk3/sVIk0P6SZQRXKcSnn72mVlZhGjEov6c7m3cAMjmVdPIt4SrB+Xp EeSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=H9Td7/TMrTAtAVeUrHIrhZSexZsjcdbUinsVZHg+GW4=; b=Wk/s00OviE3oLvlxHOAudFZooAWnYOoruP8XPieEwOimPkUatqXlYtznVlKfxT5R6/ YugLOR914aowu4bT7fMqq5pow7hcco+CxnEcTVaAT9MbG9XcYGh3OBbMp1QdoyM4KWI0 eoeFg0+USYklnNbYZq/+19FzqSisNC1y3MH1Tvo65fG2dhWFNV3HX5aiL6DN4sFc0RCL 56Mk3rdLU73EyY50Q7eCpNeKa1JFL4uNkatF5RAHA2pjJsuo3/U3Gl/cbWcDNfz6ghL5 3INVd4s6P382KR/Y7JyXZfSFYV90R8gQ9b3LPX8WC5EtwHuaZdv7gI46rp+VhljVn4Qs IZlw== X-Gm-Message-State: ACgBeo1MpqsVtLNfYIR1g6CZecO7epOhZPJ/oECKyk3GWYGdOQmkr2Zv AI2kATCPuzFYATVzoQl9PKyHSjYc2pE= X-Google-Smtp-Source: AA6agR74xpTNsjJW4kV3da/I/rAj+tl6NA1SFrgmEnh0B+2JVTsbM1IMY8LB4LV38RhSRb4nx5S5NYcC2Ws= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a17:907:7fa5:b0:730:5d54:4c24 with SMTP id qk37-20020a1709077fa500b007305d544c24mr5759332ejc.641.1661526504373; Fri, 26 Aug 2022 08:08:24 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:27 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-5-glider@google.com> Subject: [PATCH v5 04/44] x86: asm: instrument usercopy in get_user() and put_user() From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526505; a=rsa-sha256; cv=none; b=4ebheZy7O+rWS3wBxMmmMO6FGs4xAM70Rh5rQeKSh/QmbICvEjmG02onMtx49SFLqve8rC poPHzJ7le422XhWaN36vE6sy3TpBL4jIL2HpyTtidxIjO/xqKQyiJT23f3LItC3Nyuzyyn JvuEZJbJexiCvMX6vfhzOW4iUmKTRLU= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=lGhpRwPT; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 36OEIYwYKCO0VaXSTgVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=36OEIYwYKCO0VaXSTgVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526505; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=H9Td7/TMrTAtAVeUrHIrhZSexZsjcdbUinsVZHg+GW4=; b=fJ5zaJbOxN3e/DN5ysCqmrgsJTzZHVeASAkhnI2MLSxKKoaLNWsaGAM9t9/SjLXpi6Zgkz IxGyQBrba1vnr4vZ0baHWd6SskNeW0ZdBwN9r4FXepvbM4PBPM9ChioaHnXNZdR8u+TK/e hzx74xseNYWHmLZvYpVbLwOZKkZ10vI= Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=lGhpRwPT; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 36OEIYwYKCO0VaXSTgVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=36OEIYwYKCO0VaXSTgVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--glider.bounces.google.com X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: d9pcgs3yn63pj8usmoxueixrwb8qf3hz X-Rspamd-Queue-Id: C4AB64001D X-HE-Tag: 1661526505-377202 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: Use hooks from instrumented.h to notify bug detection tools about usercopy events in variations of get_user() and put_user(). Signed-off-by: Alexander Potapenko --- v5: -- handle put_user(), make sure to not evaluate pointer/value twice Link: https://linux-review.googlesource.com/id/Ia9f12bfe5832623250e20f1859fdf5cc485a2fce --- arch/x86/include/asm/uaccess.h | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h index 913e593a3b45f..c1b8982899eca 100644 --- a/arch/x86/include/asm/uaccess.h +++ b/arch/x86/include/asm/uaccess.h @@ -5,6 +5,7 @@ * User space memory access functions */ #include +#include #include #include #include @@ -103,6 +104,7 @@ extern int __get_user_bad(void); : "=a" (__ret_gu), "=r" (__val_gu), \ ASM_CALL_CONSTRAINT \ : "0" (ptr), "i" (sizeof(*(ptr)))); \ + instrument_get_user(__val_gu); \ (x) = (__force __typeof__(*(ptr))) __val_gu; \ __builtin_expect(__ret_gu, 0); \ }) @@ -192,9 +194,11 @@ extern void __put_user_nocheck_8(void); int __ret_pu; \ void __user *__ptr_pu; \ register __typeof__(*(ptr)) __val_pu asm("%"_ASM_AX); \ - __chk_user_ptr(ptr); \ - __ptr_pu = (ptr); \ - __val_pu = (x); \ + __typeof__(*(ptr)) __x = (x); /* eval x once */ \ + __typeof__(ptr) __ptr = (ptr); /* eval ptr once */ \ + __chk_user_ptr(__ptr); \ + __ptr_pu = __ptr; \ + __val_pu = __x; \ asm volatile("call __" #fn "_%P[size]" \ : "=c" (__ret_pu), \ ASM_CALL_CONSTRAINT \ @@ -202,6 +206,7 @@ extern void __put_user_nocheck_8(void); "r" (__val_pu), \ [size] "i" (sizeof(*(ptr))) \ :"ebx"); \ + instrument_put_user(__x, __ptr, sizeof(*(ptr))); \ __builtin_expect(__ret_pu, 0); \ }) @@ -248,23 +253,25 @@ extern void __put_user_nocheck_8(void); #define __put_user_size(x, ptr, size, label) \ do { \ + __typeof__(*(ptr)) __x = (x); /* eval x once */ \ __chk_user_ptr(ptr); \ switch (size) { \ case 1: \ - __put_user_goto(x, ptr, "b", "iq", label); \ + __put_user_goto(__x, ptr, "b", "iq", label); \ break; \ case 2: \ - __put_user_goto(x, ptr, "w", "ir", label); \ + __put_user_goto(__x, ptr, "w", "ir", label); \ break; \ case 4: \ - __put_user_goto(x, ptr, "l", "ir", label); \ + __put_user_goto(__x, ptr, "l", "ir", label); \ break; \ case 8: \ - __put_user_goto_u64(x, ptr, label); \ + __put_user_goto_u64(__x, ptr, label); \ break; \ default: \ __put_user_bad(); \ } \ + instrument_put_user(__x, ptr, size); \ } while (0) #ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT @@ -305,6 +312,7 @@ do { \ default: \ (x) = __get_user_bad(); \ } \ + instrument_get_user(x); \ } while (0) #define __get_user_asm(x, addr, itype, ltype, label) \ From patchwork Fri Aug 26 15:07:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956178 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 D2994ECAAA3 for ; Fri, 26 Aug 2022 15:08:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6880994000D; Fri, 26 Aug 2022 11:08:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 61174940007; Fri, 26 Aug 2022 11:08:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 464E394000D; Fri, 26 Aug 2022 11:08:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 2E2DA940007 for ; Fri, 26 Aug 2022 11:08:29 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 00D5680C86 for ; Fri, 26 Aug 2022 15:08:28 +0000 (UTC) X-FDA: 79842075138.29.68B36B6 Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) by imf26.hostedemail.com (Postfix) with ESMTP id 7634A140030 for ; Fri, 26 Aug 2022 15:08:28 +0000 (UTC) Received: by mail-ej1-f74.google.com with SMTP id ne1-20020a1709077b8100b0073d957e2869so723283ejc.1 for ; Fri, 26 Aug 2022 08:08:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=P3BIEQv1T/KwZafUGZKUBc98ocOSrUKFpAGAcrUMbqQ=; b=Gfe3GoUcVz2DJqv+lUhS4wPJK1FSFGYSNmq3/sHYEp2DjOH5v5GNBK/z6hDTUoKcTj plBctrN9vrg8FpJag++EbXU4mr1O64PW/1loKp8KRZuDnOsIEMr3eAKPs++1evafDhuT A/bg0Xtm8uNzHgCE9+qQSnmN9op+um7DtTIxTFv5pIn/W8yBUx7UfecmpyVTt+dfsVnq R6+GNW8d47HlvsNhFF9IMt3QQ4Pdb3IpCNIvyzbkPzXHWf3cKsNzlrqWxpwtGBf+/qyd MYf6zzuhfYx17T33BFzvicCgkqik20kUTnJTp2XnCJSTdNO9O0hyzdD6GKvImE6ONG+Q idug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=P3BIEQv1T/KwZafUGZKUBc98ocOSrUKFpAGAcrUMbqQ=; b=xhPpfRx0iDVL7ngnODMfxV1H0LLJL3CM20l5AkLBx33AO3JlyozhRLQYPlOJesLdpC zw4in7diBQC4y/vDzeCJBHXFSEWLtZez9an+7Nu9ZyqU/Rvl4o6ogyBZdIXR8Glz4hNf 8wCNIR7Hb9+/UvSJ8wnznWeusQC2/8N6fWsbSP2UH8hNj3tFjNN48F+CDE0RFDn+JF8p dOY3AJkXp4v7wzDARSNBTc1dGqMRDX1bQr+18H8Pj05oPUBssns3xomFRpn4itoi3YFr W9jQb+Wiiles6Wfo4rPzZxGJ4Edbl41b29UlfQiu9Sbhvr3XZnGFSLdAEjPOTPuQL8vs Dlww== X-Gm-Message-State: ACgBeo3WbxPVrkEO4VCZTZDmnQxXViy/9zexplqSMBPk+R8b7n3FTBA/ Ul5FsBY8m1Bbdn7cZlMyB9G0/sUvaA0= X-Google-Smtp-Source: AA6agR7pRi8WRPCHvWZyrbJ+uMb1xTdRgH4930Ca34TprE6WyeH/LJquvC2M2sAE+eo6XOjsPj4V5JP8Hdk= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a17:907:c06:b0:701:eb60:ded with SMTP id ga6-20020a1709070c0600b00701eb600dedmr5913732ejc.178.1661526507100; Fri, 26 Aug 2022 08:08:27 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:28 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-6-glider@google.com> Subject: [PATCH v5 05/44] asm-generic: instrument usercopy in cacheflush.h From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526508; a=rsa-sha256; cv=none; b=BBD1eZr0prnZsPiZr585MxgaNIczRRPU6xuh+sdpHKg20b3zegeuFy711gwxc1F3b3Z7SV +A/aCn/CQmzNjGr9mH8BFnLzuKK4dKZYOgVfXvDH4D2RBFEWdzpXOkBqBRhqJQjIkvwPK7 B0KFdKIp13aUXN6YK5+DUaitw1NMCIE= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Gfe3GoUc; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 36-EIYwYKCPAYdaVWjYggYdW.Ugedafmp-eecnSUc.gjY@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=36-EIYwYKCPAYdaVWjYggYdW.Ugedafmp-eecnSUc.gjY@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526508; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=P3BIEQv1T/KwZafUGZKUBc98ocOSrUKFpAGAcrUMbqQ=; b=nyzXtdBBcQEKUrmcEl+Ey+2GBxQYo/oYLSrvlpkuY5SvrXTTHoOB5LkV0qkQ96oU9X4kju DQugQ8VDV64oGMm2faomxKOQHF3ZdGAw5eSQekOq/iNPsJj8PvSfz6EEGNxqXC5fcwg1n8 QAnyDWtzBFYOITBzshBsDU1BnB/sRfU= X-Rspamd-Queue-Id: 7634A140030 Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Gfe3GoUc; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 36-EIYwYKCPAYdaVWjYggYdW.Ugedafmp-eecnSUc.gjY@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=36-EIYwYKCPAYdaVWjYggYdW.Ugedafmp-eecnSUc.gjY@flex--glider.bounces.google.com X-Rspamd-Server: rspam12 X-Rspam-User: X-Stat-Signature: ppxughato4rmzx993pj6nuwq5hrxp8jj X-HE-Tag: 1661526508-500565 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: Notify memory tools about usercopy events in copy_to_user_page() and copy_from_user_page(). Signed-off-by: Alexander Potapenko Reviewed-by: Marco Elver --- v5: -- cast user pointers to `void __user *` Link: https://linux-review.googlesource.com/id/Ic1ee8da1886325f46ad67f52176f48c2c836c48f --- include/asm-generic/cacheflush.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/include/asm-generic/cacheflush.h b/include/asm-generic/cacheflush.h index 4f07afacbc239..f46258d1a080f 100644 --- a/include/asm-generic/cacheflush.h +++ b/include/asm-generic/cacheflush.h @@ -2,6 +2,8 @@ #ifndef _ASM_GENERIC_CACHEFLUSH_H #define _ASM_GENERIC_CACHEFLUSH_H +#include + struct mm_struct; struct vm_area_struct; struct page; @@ -105,14 +107,22 @@ static inline void flush_cache_vunmap(unsigned long start, unsigned long end) #ifndef copy_to_user_page #define copy_to_user_page(vma, page, vaddr, dst, src, len) \ do { \ + instrument_copy_to_user((void __user *)dst, src, len); \ memcpy(dst, src, len); \ flush_icache_user_page(vma, page, vaddr, len); \ } while (0) #endif + #ifndef copy_from_user_page -#define copy_from_user_page(vma, page, vaddr, dst, src, len) \ - memcpy(dst, src, len) +#define copy_from_user_page(vma, page, vaddr, dst, src, len) \ + do { \ + instrument_copy_from_user_before(dst, (void __user *)src, \ + len); \ + memcpy(dst, src, len); \ + instrument_copy_from_user_after(dst, (void __user *)src, len, \ + 0); \ + } while (0) #endif #endif /* _ASM_GENERIC_CACHEFLUSH_H */ From patchwork Fri Aug 26 15:07:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956179 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 8B518ECAAA3 for ; Fri, 26 Aug 2022 15:08:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2DE81940008; Fri, 26 Aug 2022 11:08:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 26757940007; Fri, 26 Aug 2022 11:08:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 09288940008; Fri, 26 Aug 2022 11:08:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id EA23A940007 for ; Fri, 26 Aug 2022 11:08:31 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id C4A71A01B4 for ; Fri, 26 Aug 2022 15:08:31 +0000 (UTC) X-FDA: 79842075222.06.4DB9DA2 Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf19.hostedemail.com (Postfix) with ESMTP id 5B95F1A0018 for ; Fri, 26 Aug 2022 15:08:31 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id z20-20020a05640235d400b0043e1e74a495so1246800edc.11 for ; Fri, 26 Aug 2022 08:08:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=hggbZ+cOdGgnZCdhDJpRznFT1vHDu/CChFRNqftxg0A=; b=EKDv/PKgOR34tp0oFw4TL9dVZHFwqGbAnQQTavn0Xb62dbzFUPj4AI2a2e29t52VSJ yaoFonp9v5PbGfPtOW6Uh8tNheGfPxvg/MU0asqfbMaiuOZNDWdFpQ94r1Zh8FOVrEnm JQ2E1I0YGWBkaD0qcUuZ5JeHn1KPGod8KQb1wyugpmuOuxG8ECd3XnLWSqUu2g0tGj2b dmEeFFRLu/AOYLusP+PU+EKyFNbiVWULVZ42tPOD6npOeAf/aXtmAiOsXIVBCl3qVl/7 zwuG92ejO3Uyng0LtzupdNhB8U3bxwl7uZHemEXKWAHihOQA6rP2bYc0y6ybBLFxrVc/ /QKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=hggbZ+cOdGgnZCdhDJpRznFT1vHDu/CChFRNqftxg0A=; b=m5rl2fz8L8ilnhahHzvGEG19dqDzAlztmbPs5URBTM3BUYOTGDtf3y4VRLRk/TBQXm suiiu6JTL7hX7u/v3RfVNT6guOn+cs9vP8XGSkwvVPMNtozWn208P+wG7hsp9F2wHsRy CyA4mIIPdtOh4Pcw9y+9DZYfMxnTd9PHPJ4Cue4RQxV743smvSgxse2t0K/hoyP8cQFw IR5ZtGp/NfInjlJzvE1LU3RzCilyhAg+/vhFKaZ1QWy4PQ3pK8KAJhIb/AE91uUj0An0 SOCbFQnik0EKdMQiPldcAnyuRECjI4x2HqhIwI74yxp+4tYswTwMXzbXyWjiISeV5EWJ A5eA== X-Gm-Message-State: ACgBeo1WI1Qp5e0a81yZvdHv8WriiwugTAlFnDS2dvd78SmaVds5m7VV oZSHNWlpc4kiqeWizHHsFN/mD6gyVQE= X-Google-Smtp-Source: AA6agR5RUyGEpeyaVgz2pyBw8iJVbCbQSsoESYlrJth4vdCU9aTTXtJ3gDZxudBR1HRU3xhSKcYSnHDQJUk= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a05:6402:360d:b0:445:bd16:803b with SMTP id el13-20020a056402360d00b00445bd16803bmr6959224edb.318.1661526509859; Fri, 26 Aug 2022 08:08:29 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:29 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-7-glider@google.com> Subject: [PATCH v5 06/44] kmsan: add ReST documentation From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526511; a=rsa-sha256; cv=none; b=FbnjZEiDOtMq7VjaAc6JrUuarUvb68+EQtCdbeJNRLr/Kq7yOXdd2gZd3sczKRVsuEwNm9 1TsBCsljNOqI29ZCe+VIIHyoE13sQ0XCW3H7sEUwQQUjTMevS7jxlkSPYBF9ALoLTNyrOp T2QMxEucgE5ByP0Osxvmxuhv+FL+pCs= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="EKDv/PKg"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf19.hostedemail.com: domain of 37eEIYwYKCPIafcXYlaiiafY.Wigfchor-ggepUWe.ila@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=37eEIYwYKCPIafcXYlaiiafY.Wigfchor-ggepUWe.ila@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526511; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hggbZ+cOdGgnZCdhDJpRznFT1vHDu/CChFRNqftxg0A=; b=c3D7XwPyuxTTVZOmXtcWCGhtQSKp+OOuWhCVRWQWt2F+uAksH/mYpeJIsMkWzPbeTvXmZw bUVYe5ztPOXm2r1GvTiTaoo/MHkqOrSN2Xluwn4EktbkZOQd1mjwLjXL2fnStCx5h+Yow9 gg00uG/OO3NmXXbpt+DMwFdqzXaKmo8= X-Rspamd-Queue-Id: 5B95F1A0018 Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="EKDv/PKg"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf19.hostedemail.com: domain of 37eEIYwYKCPIafcXYlaiiafY.Wigfchor-ggepUWe.ila@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=37eEIYwYKCPIafcXYlaiiafY.Wigfchor-ggepUWe.ila@flex--glider.bounces.google.com X-Rspam-User: X-Rspamd-Server: rspam01 X-Stat-Signature: mf5g9cjjofrzze9y4rch6cy3jtwhrm66 X-HE-Tag: 1661526511-454172 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: Add Documentation/dev-tools/kmsan.rst and reference it in the dev-tools index. Signed-off-by: Alexander Potapenko --- v2: -- added a note that KMSAN is not intended for production use v5: -- mention CONFIG_KMSAN_CHECK_PARAM_RETVAL, drop mentions of cpu_entry_area -- add SPDX license -- address Marco Elver's comments: reorganize doc structure, fix minor nits Link: https://linux-review.googlesource.com/id/I751586f79418b95550a83c6035c650b5b01567cc --- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/kmsan.rst | 427 ++++++++++++++++++++++++++++++ 2 files changed, 428 insertions(+) create mode 100644 Documentation/dev-tools/kmsan.rst diff --git a/Documentation/dev-tools/index.rst b/Documentation/dev-tools/index.rst index 4621eac290f46..6b0663075dc04 100644 --- a/Documentation/dev-tools/index.rst +++ b/Documentation/dev-tools/index.rst @@ -24,6 +24,7 @@ Documentation/dev-tools/testing-overview.rst kcov gcov kasan + kmsan ubsan kmemleak kcsan diff --git a/Documentation/dev-tools/kmsan.rst b/Documentation/dev-tools/kmsan.rst new file mode 100644 index 0000000000000..2a53a801198cb --- /dev/null +++ b/Documentation/dev-tools/kmsan.rst @@ -0,0 +1,427 @@ +.. SPDX-License-Identifier: GPL-2.0 +.. Copyright (C) 2022, Google LLC. + +=================================== +The Kernel Memory Sanitizer (KMSAN) +=================================== + +KMSAN is a dynamic error detector aimed at finding uses of uninitialized +values. It is based on compiler instrumentation, and is quite similar to the +userspace `MemorySanitizer tool`_. + +An important note is that KMSAN is not intended for production use, because it +drastically increases kernel memory footprint and slows the whole system down. + +Usage +===== + +Building the kernel +------------------- + +In order to build a kernel with KMSAN you will need a fresh Clang (14.0.6+). +Please refer to `LLVM documentation`_ for the instructions on how to build Clang. + +Now configure and build the kernel with CONFIG_KMSAN enabled. + +Example report +-------------- + +Here is an example of a KMSAN report:: + + ===================================================== + BUG: KMSAN: uninit-value in test_uninit_kmsan_check_memory+0x1be/0x380 [kmsan_test] + test_uninit_kmsan_check_memory+0x1be/0x380 mm/kmsan/kmsan_test.c:273 + kunit_run_case_internal lib/kunit/test.c:333 + kunit_try_run_case+0x206/0x420 lib/kunit/test.c:374 + kunit_generic_run_threadfn_adapter+0x6d/0xc0 lib/kunit/try-catch.c:28 + kthread+0x721/0x850 kernel/kthread.c:327 + ret_from_fork+0x1f/0x30 ??:? + + Uninit was stored to memory at: + do_uninit_local_array+0xfa/0x110 mm/kmsan/kmsan_test.c:260 + test_uninit_kmsan_check_memory+0x1a2/0x380 mm/kmsan/kmsan_test.c:271 + kunit_run_case_internal lib/kunit/test.c:333 + kunit_try_run_case+0x206/0x420 lib/kunit/test.c:374 + kunit_generic_run_threadfn_adapter+0x6d/0xc0 lib/kunit/try-catch.c:28 + kthread+0x721/0x850 kernel/kthread.c:327 + ret_from_fork+0x1f/0x30 ??:? + + Local variable uninit created at: + do_uninit_local_array+0x4a/0x110 mm/kmsan/kmsan_test.c:256 + test_uninit_kmsan_check_memory+0x1a2/0x380 mm/kmsan/kmsan_test.c:271 + + Bytes 4-7 of 8 are uninitialized + Memory access of size 8 starts at ffff888083fe3da0 + + CPU: 0 PID: 6731 Comm: kunit_try_catch Tainted: G B E 5.16.0-rc3+ #104 + Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-2 04/01/2014 + ===================================================== + +The report says that the local variable ``uninit`` was created uninitialized in +``do_uninit_local_array()``. The third stack trace corresponds to the place +where this variable was created. + +The first stack trace shows where the uninit value was used (in +``test_uninit_kmsan_check_memory()``). The tool shows the bytes which were left +uninitialized in the local variable, as well as the stack where the value was +copied to another memory location before use. + +A use of uninitialized value ``v`` is reported by KMSAN in the following cases: + - in a condition, e.g. ``if (v) { ... }``; + - in an indexing or pointer dereferencing, e.g. ``array[v]`` or ``*v``; + - when it is copied to userspace or hardware, e.g. ``copy_to_user(..., &v, ...)``; + - when it is passed as an argument to a function, and + ``CONFIG_KMSAN_CHECK_PARAM_RETVAL`` is enabled (see below). + +The mentioned cases (apart from copying data to userspace or hardware, which is +a security issue) are considered undefined behavior from the C11 Standard point +of view. + +Disabling the instrumentation +----------------------------- + +A function can be marked with ``__no_kmsan_checks``. Doing so makes KMSAN +ignore uninitialized values in that function and mark its output as initialized. +As a result, the user will not get KMSAN reports related to that function. + +Another function attribute supported by KMSAN is ``__no_sanitize_memory``. +Applying this attribute to a function will result in KMSAN not instrumenting +it, which can be helpful if we do not want the compiler to interfere with some +low-level code (e.g. that marked with ``noinstr`` which implicitly adds +``__no_sanitize_memory``). + +This however comes at a cost: stack allocations from such functions will have +incorrect shadow/origin values, likely leading to false positives. Functions +called from non-instrumented code may also receive incorrect metadata for their +parameters. + +As a rule of thumb, avoid using ``__no_sanitize_memory`` explicitly. + +It is also possible to disable KMSAN for a single file (e.g. main.o):: + + KMSAN_SANITIZE_main.o := n + +or for the whole directory:: + + KMSAN_SANITIZE := n + +in the Makefile. Think of this as applying ``__no_sanitize_memory`` to every +function in the file or directory. Most users won't need KMSAN_SANITIZE, unless +their code gets broken by KMSAN (e.g. runs at early boot time). + +Support +======= + +In order for KMSAN to work the kernel must be built with Clang, which so far is +the only compiler that has KMSAN support. The kernel instrumentation pass is +based on the userspace `MemorySanitizer tool`_. + +The runtime library only supports x86_64 at the moment. + +How KMSAN works +=============== + +KMSAN shadow memory +------------------- + +KMSAN associates a metadata byte (also called shadow byte) with every byte of +kernel memory. A bit in the shadow byte is set iff the corresponding bit of the +kernel memory byte is uninitialized. Marking the memory uninitialized (i.e. +setting its shadow bytes to ``0xff``) is called poisoning, marking it +initialized (setting the shadow bytes to ``0x00``) is called unpoisoning. + +When a new variable is allocated on the stack, it is poisoned by default by +instrumentation code inserted by the compiler (unless it is a stack variable +that is immediately initialized). Any new heap allocation done without +``__GFP_ZERO`` is also poisoned. + +Compiler instrumentation also tracks the shadow values as they are used along +the code. When needed, instrumentation code invokes the runtime library in +``mm/kmsan/`` to persist shadow values. + +The shadow value of a basic or compound type is an array of bytes of the same +length. When a constant value is written into memory, that memory is unpoisoned. +When a value is read from memory, its shadow memory is also obtained and +propagated into all the operations which use that value. For every instruction +that takes one or more values the compiler generates code that calculates the +shadow of the result depending on those values and their shadows. + +Example:: + + int a = 0xff; // i.e. 0x000000ff + int b; + int c = a | b; + +In this case the shadow of ``a`` is ``0``, shadow of ``b`` is ``0xffffffff``, +shadow of ``c`` is ``0xffffff00``. This means that the upper three bytes of +``c`` are uninitialized, while the lower byte is initialized. + +Origin tracking +--------------- + +Every four bytes of kernel memory also have a so-called origin mapped to them. +This origin describes the point in program execution at which the uninitialized +value was created. Every origin is associated with either the full allocation +stack (for heap-allocated memory), or the function containing the uninitialized +variable (for locals). + +When an uninitialized variable is allocated on stack or heap, a new origin +value is created, and that variable's origin is filled with that value. When a +value is read from memory, its origin is also read and kept together with the +shadow. For every instruction that takes one or more values, the origin of the +result is one of the origins corresponding to any of the uninitialized inputs. +If a poisoned value is written into memory, its origin is written to the +corresponding storage as well. + +Example 1:: + + int a = 42; + int b; + int c = a + b; + +In this case the origin of ``b`` is generated upon function entry, and is +stored to the origin of ``c`` right before the addition result is written into +memory. + +Several variables may share the same origin address, if they are stored in the +same four-byte chunk. In this case every write to either variable updates the +origin for all of them. We have to sacrifice precision in this case, because +storing origins for individual bits (and even bytes) would be too costly. + +Example 2:: + + int combine(short a, short b) { + union ret_t { + int i; + short s[2]; + } ret; + ret.s[0] = a; + ret.s[1] = b; + return ret.i; + } + +If ``a`` is initialized and ``b`` is not, the shadow of the result would be +0xffff0000, and the origin of the result would be the origin of ``b``. +``ret.s[0]`` would have the same origin, but it will never be used, because +that variable is initialized. + +If both function arguments are uninitialized, only the origin of the second +argument is preserved. + +Origin chaining +~~~~~~~~~~~~~~~ + +To ease debugging, KMSAN creates a new origin for every store of an +uninitialized value to memory. The new origin references both its creation stack +and the previous origin the value had. This may cause increased memory +consumption, so we limit the length of origin chains in the runtime. + +Clang instrumentation API +------------------------- + +Clang instrumentation pass inserts calls to functions defined in +``mm/kmsan/nstrumentation.c`` into the kernel code. + +Shadow manipulation +~~~~~~~~~~~~~~~~~~~ + +For every memory access the compiler emits a call to a function that returns a +pair of pointers to the shadow and origin addresses of the given memory:: + + typedef struct { + void *shadow, *origin; + } shadow_origin_ptr_t + + shadow_origin_ptr_t __msan_metadata_ptr_for_load_{1,2,4,8}(void *addr) + shadow_origin_ptr_t __msan_metadata_ptr_for_store_{1,2,4,8}(void *addr) + shadow_origin_ptr_t __msan_metadata_ptr_for_load_n(void *addr, uintptr_t size) + shadow_origin_ptr_t __msan_metadata_ptr_for_store_n(void *addr, uintptr_t size) + +The function name depends on the memory access size. + +The compiler makes sure that for every loaded value its shadow and origin +values are read from memory. When a value is stored to memory, its shadow and +origin are also stored using the metadata pointers. + +Handling locals +~~~~~~~~~~~~~~~ + +A special function is used to create a new origin value for a local variable and +set the origin of that variable to that value:: + + void __msan_poison_alloca(void *addr, uintptr_t size, char *descr) + +Access to per-task data +~~~~~~~~~~~~~~~~~~~~~~~ + +At the beginning of every instrumented function KMSAN inserts a call to +``__msan_get_context_state()``:: + + kmsan_context_state *__msan_get_context_state(void) + +``kmsan_context_state`` is declared in ``include/linux/kmsan.h``:: + + struct kmsan_context_state { + char param_tls[KMSAN_PARAM_SIZE]; + char retval_tls[KMSAN_RETVAL_SIZE]; + char va_arg_tls[KMSAN_PARAM_SIZE]; + char va_arg_origin_tls[KMSAN_PARAM_SIZE]; + u64 va_arg_overflow_size_tls; + char param_origin_tls[KMSAN_PARAM_SIZE]; + depot_stack_handle_t retval_origin_tls; + }; + +This structure is used by KMSAN to pass parameter shadows and origins between +instrumented functions (unless the parameters are checked immediately by +``CONFIG_KMSAN_CHECK_PARAM_RETVAL``). + +Passing uninitialized values to functions +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Clang's MemorySanitizer instrumentation has an option, +``-fsanitize-memory-param-retval``, which makes the compiler check function +parameters passed by value, as well as function return values. + +The option is controlled by ``CONFIG_KMSAN_CHECK_PARAM_RETVAL``, which is +enabled by default to let KMSAN report uninitialized values earlier. +Please refer to the `LKML discussion`_ for more details. + +Because of the way the checks are implemented in LLVM (they are only applied to +parameters marked as ``noundef``), not all parameters are guaranteed to be +checked, so we cannot give up the metadata storage in ``kmsan_context_state``. + +String functions +~~~~~~~~~~~~~~~~ + +The compiler replaces calls to ``memcpy()``/``memmove()``/``memset()`` with the +following functions. These functions are also called when data structures are +initialized or copied, making sure shadow and origin values are copied alongside +with the data:: + + void *__msan_memcpy(void *dst, void *src, uintptr_t n) + void *__msan_memmove(void *dst, void *src, uintptr_t n) + void *__msan_memset(void *dst, int c, uintptr_t n) + +Error reporting +~~~~~~~~~~~~~~~ + +For each use of a value the compiler emits a shadow check that calls +``__msan_warning()`` in the case that value is poisoned:: + + void __msan_warning(u32 origin) + +``__msan_warning()`` causes KMSAN runtime to print an error report. + +Inline assembly instrumentation +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +KMSAN instruments every inline assembly output with a call to:: + + void __msan_instrument_asm_store(void *addr, uintptr_t size) + +, which unpoisons the memory region. + +This approach may mask certain errors, but it also helps to avoid a lot of +false positives in bitwise operations, atomics etc. + +Sometimes the pointers passed into inline assembly do not point to valid memory. +In such cases they are ignored at runtime. + + +Runtime library +--------------- + +The code is located in ``mm/kmsan/``. + +Per-task KMSAN state +~~~~~~~~~~~~~~~~~~~~ + +Every task_struct has an associated KMSAN task state that holds the KMSAN +context (see above) and a per-task flag disallowing KMSAN reports:: + + struct kmsan_context { + ... + bool allow_reporting; + struct kmsan_context_state cstate; + ... + } + + struct task_struct { + ... + struct kmsan_context kmsan; + ... + } + +KMSAN contexts +~~~~~~~~~~~~~~ + +When running in a kernel task context, KMSAN uses ``current->kmsan.cstate`` to +hold the metadata for function parameters and return values. + +But in the case the kernel is running in the interrupt, softirq or NMI context, +where ``current`` is unavailable, KMSAN switches to per-cpu interrupt state:: + + DEFINE_PER_CPU(struct kmsan_ctx, kmsan_percpu_ctx); + +Metadata allocation +~~~~~~~~~~~~~~~~~~~ + +There are several places in the kernel for which the metadata is stored. + +1. Each ``struct page`` instance contains two pointers to its shadow and +origin pages:: + + struct page { + ... + struct page *shadow, *origin; + ... + }; + +At boot-time, the kernel allocates shadow and origin pages for every available +kernel page. This is done quite late, when the kernel address space is already +fragmented, so normal data pages may arbitrarily interleave with the metadata +pages. + +This means that in general for two contiguous memory pages their shadow/origin +pages may not be contiguous. Consequently, if a memory access crosses the +boundary of a memory block, accesses to shadow/origin memory may potentially +corrupt other pages or read incorrect values from them. + +In practice, contiguous memory pages returned by the same ``alloc_pages()`` +call will have contiguous metadata, whereas if these pages belong to two +different allocations their metadata pages can be fragmented. + +For the kernel data (``.data``, ``.bss`` etc.) and percpu memory regions +there also are no guarantees on metadata contiguity. + +In the case ``__msan_metadata_ptr_for_XXX_YYY()`` hits the border between two +pages with non-contiguous metadata, it returns pointers to fake shadow/origin regions:: + + char dummy_load_page[PAGE_SIZE] __attribute__((aligned(PAGE_SIZE))); + char dummy_store_page[PAGE_SIZE] __attribute__((aligned(PAGE_SIZE))); + +``dummy_load_page`` is zero-initialized, so reads from it always yield zeroes. +All stores to ``dummy_store_page`` are ignored. + +2. For vmalloc memory and modules, there is a direct mapping between the memory +range, its shadow and origin. KMSAN reduces the vmalloc area by 3/4, making only +the first quarter available to ``vmalloc()``. The second quarter of the vmalloc +area contains shadow memory for the first quarter, the third one holds the +origins. A small part of the fourth quarter contains shadow and origins for the +kernel modules. Please refer to ``arch/x86/include/asm/pgtable_64_types.h`` for +more details. + +When an array of pages is mapped into a contiguous virtual memory space, their +shadow and origin pages are similarly mapped into contiguous regions. + +References +========== + +E. Stepanov, K. Serebryany. `MemorySanitizer: fast detector of uninitialized +memory use in C++ +`_. +In Proceedings of CGO 2015. + +.. _MemorySanitizer tool: https://clang.llvm.org/docs/MemorySanitizer.html +.. _LLVM documentation: https://llvm.org/docs/GettingStarted.html +.. _LKML discussion: https://lore.kernel.org/all/20220614144853.3693273-1-glider@google.com/ From patchwork Fri Aug 26 15:07:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956180 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 EF0F4ECAAD4 for ; Fri, 26 Aug 2022 15:08:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7E96A94000E; Fri, 26 Aug 2022 11:08:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 776FA940007; Fri, 26 Aug 2022 11:08:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5752094000E; Fri, 26 Aug 2022 11:08:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 45C26940007 for ; Fri, 26 Aug 2022 11:08:34 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 22C74A0FAD for ; Fri, 26 Aug 2022 15:08:34 +0000 (UTC) X-FDA: 79842075348.13.C6368AF Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf24.hostedemail.com (Postfix) with ESMTP id D420D18001B for ; Fri, 26 Aug 2022 15:08:33 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id z6-20020a05640240c600b0043e1d52fd98so1240249edb.22 for ; Fri, 26 Aug 2022 08:08:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=c5NepUxkZ67sJe7jHUeDkh2lejA2rZ0n/lgzGJfAW+w=; b=c+6A8OMgE+PXXvaKjkvUnzf0Zny9FaGQw3ITZ0eOqhvk/xJ6RvxfKh+stOxXyatXl4 YGr0F0FGxuPcv8CUWTObk59qFjdxmNbRa0oHquDksvKKfT8Zu2vHF6G0vca6kOpAbhNI OVOEZTiTMx/xUIeNXpsat3eCswFWmSuMpYNt42aS00HKw95twaAXWDw2g1FqoKpczn16 IuVC8oufvBy3n96QICaiFTQbEIjqBDPQxH/4XN690eGkbiZ3y/ft7KJ0uT7Yb+5MevGJ DRahR3sL+vhkXEP7eUoI9WqyhhSukcLsMWy7BqvewYy5XGfr72OPIP7WRVR5q3FgsYZn IVMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=c5NepUxkZ67sJe7jHUeDkh2lejA2rZ0n/lgzGJfAW+w=; b=4BTywblyexXLpQv9eKNfrZV2STZ0no0ur++z3jNnhJurfLqres+0vdSwvrmk/oA5VA xxVcZ7/BBpf4ovswIUn0878ZP/PApXk0LQDPVm84YaN8T2z7GUkG/P40GB2WHU3OUTih WAhyPYho4IHM9aDHxDGL6SAQF+evCIBXJN2IVd4uU/WSpqKKsTQc2/qSd40TCOkfcyew Ig+/ev+dzgaicJToTstA05MNTF6zBdpZsB9ZX89kxoc8QN2ha8Xrwih5a5H+fBTNg54J HBWWGoToQjKCGtNkjGNrQuQ86iJeWnwPYBX/mIcSnqgxbNq1dAD0GZFl6WFuVRt8FNN4 OZvA== X-Gm-Message-State: ACgBeo0Cv1OKz7KhCXh3NjzmjCdlAsuXfIYxkCqxTddms3taJePtXSRt FX3g1nzgDhoyGaWXwvTyoGrDK42RvsQ= X-Google-Smtp-Source: AA6agR5ehbhUugsFXAPBIbmUJl2ccI1z6E6toZVNB9pK3XsABZI5EGu5QKmG8MsBqiQScrt+e+ok3n5vJiE= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a05:6402:515:b0:447:780c:39d6 with SMTP id m21-20020a056402051500b00447780c39d6mr7244136edv.265.1661526512626; Fri, 26 Aug 2022 08:08:32 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:30 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-8-glider@google.com> Subject: [PATCH v5 07/44] kmsan: introduce __no_sanitize_memory and __no_kmsan_checks From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526513; a=rsa-sha256; cv=none; b=FO2FXl/JP7je49MshLexPUE/ADN+c0+PgjflwtswQXY5J2baOUmV1AqtnO1aQHN7sQyi7M n4ojMezUb5tkCJqAu05c8N4RZXY55XT9R0/78sPZKm20UHioUnrjUPZFpyzL8L3vMVio9C 7Ge/jW4rSxM4ppNUBBvHZyG1NY/glwM= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=c+6A8OMg; spf=pass (imf24.hostedemail.com: domain of 38OEIYwYKCPUdifabodlldib.Zljifkru-jjhsXZh.lod@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=38OEIYwYKCPUdifabodlldib.Zljifkru-jjhsXZh.lod@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526513; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=c5NepUxkZ67sJe7jHUeDkh2lejA2rZ0n/lgzGJfAW+w=; b=IQxzafEdDZgMAAgn207KGSzWvUvrlq8I4pqt4Oe/RYOq78esALZY0Vttbnr0OT8bW5s1WX k7//PmKHh5Yk3Oe9o2+vSKC+t3vrJDqJAGUReb6jAeAdEUS4koQ7Nq9tQax6OAfTYDhlR8 ZN/JImum16Buo7RIi9qFBhIY1XrcUT8= X-Rspam-User: Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=c+6A8OMg; spf=pass (imf24.hostedemail.com: domain of 38OEIYwYKCPUdifabodlldib.Zljifkru-jjhsXZh.lod@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=38OEIYwYKCPUdifabodlldib.Zljifkru-jjhsXZh.lod@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam08 X-Stat-Signature: eairi6rioxn5t4tpe66mfto3m4ggik9r X-Rspamd-Queue-Id: D420D18001B X-HE-Tag: 1661526513-456240 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: __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. This behavior is overridden by __always_inline. __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 Reviewed-by: Marco Elver --- 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 c84fec767445d..4fa0cc4cbd2c8 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 9b157b71036f1..f55a37efdb974 100644 --- a/include/linux/compiler-gcc.h +++ b/include/linux/compiler-gcc.h @@ -114,6 +114,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. From patchwork Fri Aug 26 15:07:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956181 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 C2D0DECAAD4 for ; Fri, 26 Aug 2022 15:08:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 59D9894000F; Fri, 26 Aug 2022 11:08:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 525ED940007; Fri, 26 Aug 2022 11:08:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 32A6F94000F; Fri, 26 Aug 2022 11:08:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 21BD4940007 for ; Fri, 26 Aug 2022 11:08:37 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 04290AC0A0 for ; Fri, 26 Aug 2022 15:08:36 +0000 (UTC) X-FDA: 79842075474.21.A9B6208 Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf05.hostedemail.com (Postfix) with ESMTP id B962D100008 for ; Fri, 26 Aug 2022 15:08:36 +0000 (UTC) Received: by mail-ed1-f73.google.com with SMTP id b13-20020a056402350d00b0043dfc84c533so1233760edd.5 for ; Fri, 26 Aug 2022 08:08:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=TLtEZ0bDABRCjn67uEnKqh55BiGh6mAGtPfpqU3Dom0=; b=XhlU5hNd0e3S9HPBMmJkefc8yx08LwmA0SrbD/8z4iCrhfOsJcZduc1PlOL9efVBc+ RqztXd6/u4p+K40pJYJ4MKo6W7YSwm2TGNfQJRg+spj7KYsPClZuKVg0K7kt5K6whqKk q+NjffiftBk+4Wc6g4Tz0BBUvyrlit9JdAIemyH8qENuefh4JnuhzYQuq/s4MXy2lHoL NjoHCmqDqQlUi8tcUV7ZMIQKkT1OxE2DmCvSQcE4JHvpg4oQKQDno1JEzzGIKstmVxfI M0cRhGmxUmpadUGkfzrVjEWgWPnBig+aEKw2Ua3xaSSd45CO/SHC+zQxCgI2IVeAmV7R JiAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=TLtEZ0bDABRCjn67uEnKqh55BiGh6mAGtPfpqU3Dom0=; b=w3bF9InGB+dxX2xleuXPFFcS5otuWCs7eKnc4pxLWBj8QyxL/3oGkGYB/0LknUS0DI A0yuYTreHpwNiup9VRfWt3OiNGGG1MM3PKvBTKNFkux1RDxG+zWNjCRS368Eg/XO0wsb u9zHb+UhrIzGD7HPZqorppnSmBNUEZ9OUYudKo0XlMpugtkHaoCJ2VBWJTETQdeEkOFH vC82KzhjNA1KDugvd0fuE23yRw89NfB47HOqi1vgANP0kfke3VLHkvglCZqn8Z3bPOij 3lAeWSv+klyQFyFHbdO7yavqi999pGHo35jBrh2YaBv4rjC/yT1IBl8Ip0+Wx6d46W4q ARpg== X-Gm-Message-State: ACgBeo3nOQKQDnecBEmHewwWaVBNh7ASz1qFYOTHj5Th9LOwHiMFPXrI YiOCRV9HGs0OcP+86GlX0MCx14dxMco= X-Google-Smtp-Source: AA6agR6QdK8Prck1Efax3JC3LSwW7ZoPOT0nuqvyH6CShWmZZX9DGTqKWmC6+Ghh+6F66GZY1zvqUreveU8= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a17:906:9752:b0:738:364a:4ac with SMTP id o18-20020a170906975200b00738364a04acmr5783646ejy.759.1661526515449; Fri, 26 Aug 2022 08:08:35 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:31 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-9-glider@google.com> Subject: [PATCH v5 08/44] kmsan: mark noinstr as __no_sanitize_memory From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526516; a=rsa-sha256; cv=none; b=ZoVsaSsAGlgNFRk3k9ou+xbvu9qhA6C/5r4XTVs1PqhOehySVbfOnT0aqa5Y+knezsl657 02xJL6Zuvb3teycejJGrCuKvBcQAZ0FZP0DYsyBvzKiyMtlGNra+VmYfQ4kOK0cIyMoi31 zrTqAFNKiVgEXJH1YcNFXmD7IyQcVj4= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=XhlU5hNd; spf=pass (imf05.hostedemail.com: domain of 38-EIYwYKCPglqnijwlttlqj.htrqnsz2-rrp0fhp.twl@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=38-EIYwYKCPglqnijwlttlqj.htrqnsz2-rrp0fhp.twl@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526516; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=TLtEZ0bDABRCjn67uEnKqh55BiGh6mAGtPfpqU3Dom0=; b=i/06Nn3tyi64hdDrF4UG2N1vLsBeRVRL94L59atW3IODMrIG5+czfwr9Abo+DYZ+0FUzB0 JZgO3jNBPk4/4xS/H7+d2qIhKqapmyHLjVi8kSZOrqInDPDyq/PXkHOlf4JExum0Pzwx0C XY9qtV7CEoHfZN1suAxaq1P5f+3IAoM= X-Stat-Signature: fuphqth5d3uaoxw7gmwcngcn7j8bjwoq X-Rspamd-Queue-Id: B962D100008 X-Rspam-User: X-Rspamd-Server: rspam06 Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=XhlU5hNd; spf=pass (imf05.hostedemail.com: domain of 38-EIYwYKCPglqnijwlttlqj.htrqnsz2-rrp0fhp.twl@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=38-EIYwYKCPglqnijwlttlqj.htrqnsz2-rrp0fhp.twl@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-HE-Tag: 1661526516-259609 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: noinstr functions should never be instrumented, so make KMSAN skip them by applying the __no_sanitize_memory attribute. Signed-off-by: Alexander Potapenko Reviewed-by: Marco Elver --- v2: -- moved this patch earlier in the series per Mark Rutland's request Link: https://linux-review.googlesource.com/id/I3c9abe860b97b49bc0c8026918b17a50448dec0d --- include/linux/compiler_types.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h index 4f2a819fd60a3..015207a6e2bf5 100644 --- a/include/linux/compiler_types.h +++ b/include/linux/compiler_types.h @@ -229,7 +229,8 @@ struct ftrace_likely_data { /* Section for code which can't be instrumented at all */ #define noinstr \ noinline notrace __attribute((__section__(".noinstr.text"))) \ - __no_kcsan __no_sanitize_address __no_profile __no_sanitize_coverage + __no_kcsan __no_sanitize_address __no_profile __no_sanitize_coverage \ + __no_sanitize_memory #endif /* __KERNEL__ */ From patchwork Fri Aug 26 15:07:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956182 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 707E4ECAAD5 for ; Fri, 26 Aug 2022 15:08:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0957B940010; Fri, 26 Aug 2022 11:08:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 043F1940007; Fri, 26 Aug 2022 11:08:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB1CD940010; Fri, 26 Aug 2022 11:08:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C7963940007 for ; Fri, 26 Aug 2022 11:08:39 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id AA57C40EF0 for ; Fri, 26 Aug 2022 15:08:39 +0000 (UTC) X-FDA: 79842075558.27.5D4EDBC Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) by imf25.hostedemail.com (Postfix) with ESMTP id 6B664A0026 for ; Fri, 26 Aug 2022 15:08:39 +0000 (UTC) Received: by mail-ej1-f73.google.com with SMTP id sd6-20020a1709076e0600b0073315809fb5so720191ejc.10 for ; Fri, 26 Aug 2022 08:08:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=lZyR+/TbY71+6irDHrQVY8i8lV0LAE0DR0x5Sx//ziQ=; b=Ofrs8PCMHX+W0ceEYWnDzVx1uzp/bWz8GLaWKdo+wAqOx3sPZu6pI92iq0IULzGOQ8 VJ09OsHIUk+49s1paxcvdc/dQgoZURGCS4X6UxVQKGmyCn/fzomHAQt3pDibyyvopGw+ A204SQq3udS4olps/yoQNs/HdBDLMH/XYtFo5t7S3WbS0/Mi6jEQiCtdTGIheelU/NKQ ODyynPxQ+JC42TN6qhou33wAGXEzHzmJj/qEXAgKq4QXYccBrT+Ih7NpvsAuafykD+5Y +aYVMqZk7TNUIRRwqC7Wdqslm1Z8gXmVvziP4uuhA49TmwX3KTg2b8vw2WCFS2T1Wo1D S3QQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=lZyR+/TbY71+6irDHrQVY8i8lV0LAE0DR0x5Sx//ziQ=; b=DVKwQ5v9bBuWGlSjdfBuRpkD1UQvHUXteOmbL0f2hmvKdsBTqf1ggSdZIQ4ofA3w8q AfxtDy34gNcEkfi0r0YWbZfz8QePbEthnkD9h7ymDBTEKGRh5zabPJFQeiWAKxp+it88 d9TFWmCMSaPAX1oqY6R+rBmJsL10vMGyMqgGM3JilBaPw9TCN+5ttZCgVX1mW5/S8CXz lxE4UE6UqpgnJ/hFzjKkXcw0YPSJlNtoG5E95KN6Mzju1HKy/jyf2odDoJnQY87lwJcu bB6van+ZGNqfOt41V0misQv0Af3NZuyKd6W2wHbvKmwuLU3aEqpvgqJ6Sdjj+8qx8uid +VNw== X-Gm-Message-State: ACgBeo2Jvv0xELJnh9UCKqr9BUv6ryjrZx3e/TkxH7Iuzi5DcobTWtGk wEAew2G2IM+AQAHxVPkCztH+VxbrlUA= X-Google-Smtp-Source: AA6agR6DZmZvABSNd8NirMJa2iJguxZBNB8hGvX5VWCga9N1u2wq6DvLGXRRSlQxms71XTCN2etlpU9v9zU= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a17:906:f88f:b0:731:463d:4b15 with SMTP id lg15-20020a170906f88f00b00731463d4b15mr5703796ejb.299.1661526518263; Fri, 26 Aug 2022 08:08:38 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:32 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-10-glider@google.com> Subject: [PATCH v5 09/44] x86: kmsan: pgtable: reduce vmalloc space From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526519; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=lZyR+/TbY71+6irDHrQVY8i8lV0LAE0DR0x5Sx//ziQ=; b=A7FvIGxMpJ85X/LlFkz5nsCKxMyE6A7JH0RIluDlQ31AvU0oJPNl/ywfYlFRQ8Gf0D3gAo OoduAu7tlN0y8Hha7PP3o/MPBVbkG589Tmj+eaOj+z7Ar6LGKlJGkydbdMKxa/sSYpYVX2 AEroYeHeufWAWBpr9+RDFIIaS2BEMFg= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Ofrs8PCM; spf=pass (imf25.hostedemail.com: domain of 39uEIYwYKCPsjolghujrrjoh.frpolqx0-ppnydfn.ruj@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=39uEIYwYKCPsjolghujrrjoh.frpolqx0-ppnydfn.ruj@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526519; a=rsa-sha256; cv=none; b=mz2iXcZNHw4xyd6bOBero2cy/wYCsk79p6UCQJDDP0tair83eDHE6bsAHqu3eHmL34lWnd JvJin3gkpcffi4SqV+O3jh2ErwQ3oRGtmZOCgOEhpD98UnUQTTwz2W/o2vYoPe1xDP1HLX Xn1bRfqFZwNpFn9N+56yD1Zr6rtjw5o= X-Rspam-User: Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Ofrs8PCM; spf=pass (imf25.hostedemail.com: domain of 39uEIYwYKCPsjolghujrrjoh.frpolqx0-ppnydfn.ruj@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=39uEIYwYKCPsjolghujrrjoh.frpolqx0-ppnydfn.ruj@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam05 X-Stat-Signature: 4tue1w1hoomoridmj6eaziggixwpydgi X-Rspamd-Queue-Id: 6B664A0026 X-HE-Tag: 1661526519-846051 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 is going to use 3/4 of existing vmalloc space to hold the metadata, therefore we lower VMALLOC_END to make sure vmalloc() doesn't allocate past the first 1/4. Signed-off-by: Alexander Potapenko --- v2: -- added x86: to the title v5: -- add comment for VMEMORY_END Link: https://linux-review.googlesource.com/id/I9d8b7f0a88a639f1263bc693cbd5c136626f7efd --- arch/x86/include/asm/pgtable_64_types.h | 47 ++++++++++++++++++++++++- arch/x86/mm/init_64.c | 2 +- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/pgtable_64_types.h b/arch/x86/include/asm/pgtable_64_types.h index 70e360a2e5fb7..04f36063ad546 100644 --- a/arch/x86/include/asm/pgtable_64_types.h +++ b/arch/x86/include/asm/pgtable_64_types.h @@ -139,7 +139,52 @@ extern unsigned int ptrs_per_p4d; # define VMEMMAP_START __VMEMMAP_BASE_L4 #endif /* CONFIG_DYNAMIC_MEMORY_LAYOUT */ -#define VMALLOC_END (VMALLOC_START + (VMALLOC_SIZE_TB << 40) - 1) +/* + * End of the region for which vmalloc page tables are pre-allocated. + * For non-KMSAN builds, this is the same as VMALLOC_END. + * For KMSAN builds, VMALLOC_START..VMEMORY_END is 4 times bigger than + * VMALLOC_START..VMALLOC_END (see below). + */ +#define VMEMORY_END (VMALLOC_START + (VMALLOC_SIZE_TB << 40) - 1) + +#ifndef CONFIG_KMSAN +#define VMALLOC_END VMEMORY_END +#else +/* + * In KMSAN builds vmalloc area is four times smaller, and the remaining 3/4 + * are used to keep the metadata for virtual pages. The memory formerly + * belonging to vmalloc area is now laid out as follows: + * + * 1st quarter: VMALLOC_START to VMALLOC_END - new vmalloc area + * 2nd quarter: KMSAN_VMALLOC_SHADOW_START to + * VMALLOC_END+KMSAN_VMALLOC_SHADOW_OFFSET - vmalloc area shadow + * 3rd quarter: KMSAN_VMALLOC_ORIGIN_START to + * VMALLOC_END+KMSAN_VMALLOC_ORIGIN_OFFSET - vmalloc area origins + * 4th quarter: KMSAN_MODULES_SHADOW_START to KMSAN_MODULES_ORIGIN_START + * - shadow for modules, + * KMSAN_MODULES_ORIGIN_START to + * KMSAN_MODULES_ORIGIN_START + MODULES_LEN - origins for modules. + */ +#define VMALLOC_QUARTER_SIZE ((VMALLOC_SIZE_TB << 40) >> 2) +#define VMALLOC_END (VMALLOC_START + VMALLOC_QUARTER_SIZE - 1) + +/* + * vmalloc metadata addresses are calculated by adding shadow/origin offsets + * to vmalloc address. + */ +#define KMSAN_VMALLOC_SHADOW_OFFSET VMALLOC_QUARTER_SIZE +#define KMSAN_VMALLOC_ORIGIN_OFFSET (VMALLOC_QUARTER_SIZE << 1) + +#define KMSAN_VMALLOC_SHADOW_START (VMALLOC_START + KMSAN_VMALLOC_SHADOW_OFFSET) +#define KMSAN_VMALLOC_ORIGIN_START (VMALLOC_START + KMSAN_VMALLOC_ORIGIN_OFFSET) + +/* + * The shadow/origin for modules are placed one by one in the last 1/4 of + * vmalloc space. + */ +#define KMSAN_MODULES_SHADOW_START (VMALLOC_END + KMSAN_VMALLOC_ORIGIN_OFFSET + 1) +#define KMSAN_MODULES_ORIGIN_START (KMSAN_MODULES_SHADOW_START + MODULES_LEN) +#endif /* CONFIG_KMSAN */ #define MODULES_VADDR (__START_KERNEL_map + KERNEL_IMAGE_SIZE) /* The module sections ends with the start of the fixmap */ diff --git a/arch/x86/mm/init_64.c b/arch/x86/mm/init_64.c index 0fe690ebc269b..39b6bfcaa0ed4 100644 --- a/arch/x86/mm/init_64.c +++ b/arch/x86/mm/init_64.c @@ -1287,7 +1287,7 @@ static void __init preallocate_vmalloc_pages(void) unsigned long addr; const char *lvl; - for (addr = VMALLOC_START; addr <= VMALLOC_END; addr = ALIGN(addr + 1, PGDIR_SIZE)) { + for (addr = VMALLOC_START; addr <= VMEMORY_END; addr = ALIGN(addr + 1, PGDIR_SIZE)) { pgd_t *pgd = pgd_offset_k(addr); p4d_t *p4d; pud_t *pud; From patchwork Fri Aug 26 15:07:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956183 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 7F09BECAAD6 for ; Fri, 26 Aug 2022 15:08:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 15805940011; Fri, 26 Aug 2022 11:08:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0E0D7940007; Fri, 26 Aug 2022 11:08:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E9C4A940011; Fri, 26 Aug 2022 11:08:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id D7710940007 for ; Fri, 26 Aug 2022 11:08:42 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B40D3140F2D for ; Fri, 26 Aug 2022 15:08:42 +0000 (UTC) X-FDA: 79842075684.07.289AD38 Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf01.hostedemail.com (Postfix) with ESMTP id 4F47C40015 for ; Fri, 26 Aug 2022 15:08:42 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id x9-20020a05640226c900b00447e004ea4cso1241807edd.8 for ; Fri, 26 Aug 2022 08:08:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=KyITLg/74GrhViHFJeQt7QU8AnVbkb7jycJ7NufNBUo=; b=JGLI1XVMHG2i60SDAlQT0tbexjKijbMvRCyJBm7rqKpNb8F4aYTTCeDzdw3DZRTTY9 9aBJI5GzvyT5OHahe6P/FLulT47ZG48jaSm51uWVpeEM2anno2pVkr/HWnEaP4dzX2sF twstPsWzst1+5uhmUIC538U8YzMDGMv8YPX1agG8mT4BMvYcmTbtHBdrbhp45twT7nWE nz3m1f2/FPyxoUBD0WCWPwU+2VcI0cRXIUlw4UKcnV2+HEz4YBqOj5X/pB0YMkzOfjBA Gfm4kLyCfYg0BuipDD2uTTZ9O06RAPnTfMpt3Cr9PpAqZlkS4Eal6zCfjZiQC/y2Ypav PWuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=KyITLg/74GrhViHFJeQt7QU8AnVbkb7jycJ7NufNBUo=; b=ggA2f6Wa4xUvoaKiHK+NVLHMxNXlY8RktJ8tvYhEmgrzi9YXuOIOznwuEuIRmIR0Rc r07RO8/bMbebSeVpBJ5EbvhzEdY+KIjgK1bQnFxlMYYF92JJiEevlsIo4IEF34xOhCPI ze1qMKYax+5aeaGzYSlZiaxiZKUT8gooZI3MpOe8ksVvY8E/BBPXqlkpzmMunCJUj2pU qdpiEeHK1oRtTjLZw8ogWrNMfEvBW9zhVKQN1Ad0nVynvu/64Mvcyq47t0EU3/G2Z/jT 1cIpF7ofm/edHFsTgGtKtCwNtY5AOJaBl5y+wUgAWNzGEkzDuMyMc5622ODqWOMeuHlR vocg== X-Gm-Message-State: ACgBeo1PwHFihFZE+Krfoys+onA6TKZJHk9PHQhY56Ehv62NM/TE/Rwj jnF1b7Iqv+i6HoArIlefskLOxw3UUvY= X-Google-Smtp-Source: AA6agR4pAyh98UW17iINAKOiWgkXvBjhtWUMPuOGGFy1IcJGaEVoMCTj1DvlVljTnjKpWwFAIOs4GvCri44= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a17:907:a04e:b0:73d:8419:3d88 with SMTP id gz14-20020a170907a04e00b0073d84193d88mr5498703ejc.616.1661526521065; Fri, 26 Aug 2022 08:08:41 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:33 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-11-glider@google.com> Subject: [PATCH v5 10/44] libnvdimm/pfn_dev: increase MAX_STRUCT_PAGE_SIZE From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526522; a=rsa-sha256; cv=none; b=asgedusdawP2p33nUGJuzSD/3eA4BgChR5ow31+Uyg8GQKU/TLJpV94QDF1I+sim10/P78 AyoULFSvSZyNOu545rJ7N07glyKPDht5tsfY2dDBzCJiOFtYVpTYQaTpaRA4TkluuKhxVX 3rQWETXQNIBpE4BtSevA9BaRZRfbLts= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=JGLI1XVM; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf01.hostedemail.com: domain of 3-eEIYwYKCP4mrojkxmuumrk.iusrot03-ssq1giq.uxm@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3-eEIYwYKCP4mrojkxmuumrk.iusrot03-ssq1giq.uxm@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526522; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=KyITLg/74GrhViHFJeQt7QU8AnVbkb7jycJ7NufNBUo=; b=nkmSwb6lm3iby8/h0vSi0bBUpiBau4UDKrpnqYSuBbglLCntEVOVzDxXIO76RDEFRqQWDr 1nRSL25vl8+DSizc2vqEUzXMEIqEL3WO4AsmPNA+4jNBZOW5gwnpCXSpUmZXqzl00wZ7D2 +KhVz3MXGEl2FU6WjPuWZIhXR7qeCpo= X-Rspamd-Server: rspam11 X-Rspam-User: X-Rspamd-Queue-Id: 4F47C40015 Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=JGLI1XVM; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf01.hostedemail.com: domain of 3-eEIYwYKCP4mrojkxmuumrk.iusrot03-ssq1giq.uxm@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3-eEIYwYKCP4mrojkxmuumrk.iusrot03-ssq1giq.uxm@flex--glider.bounces.google.com X-Stat-Signature: hstjor4xwghiiq4zpyeqwge43ik1yjig X-HE-Tag: 1661526522-478412 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 adds extra metadata fields to struct page, so it does not fit into 64 bytes anymore. This change leads to increased memory consumption of the nvdimm driver, regardless of whether the kernel is built with KMSAN or not. Signed-off-by: Alexander Potapenko Reviewed-by: Marco Elver --- Link: https://linux-review.googlesource.com/id/I353796acc6a850bfd7bb342aa1b63e616fc614f1 --- drivers/nvdimm/nd.h | 2 +- drivers/nvdimm/pfn_devs.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/nvdimm/nd.h b/drivers/nvdimm/nd.h index ec5219680092d..85ca5b4da3cf3 100644 --- a/drivers/nvdimm/nd.h +++ b/drivers/nvdimm/nd.h @@ -652,7 +652,7 @@ void devm_namespace_disable(struct device *dev, struct nd_namespace_common *ndns); #if IS_ENABLED(CONFIG_ND_CLAIM) /* max struct page size independent of kernel config */ -#define MAX_STRUCT_PAGE_SIZE 64 +#define MAX_STRUCT_PAGE_SIZE 128 int nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap); #else static inline int nvdimm_setup_pfn(struct nd_pfn *nd_pfn, diff --git a/drivers/nvdimm/pfn_devs.c b/drivers/nvdimm/pfn_devs.c index 0e92ab4b32833..61af072ac98f9 100644 --- a/drivers/nvdimm/pfn_devs.c +++ b/drivers/nvdimm/pfn_devs.c @@ -787,7 +787,7 @@ static int nd_pfn_init(struct nd_pfn *nd_pfn) * when populating the vmemmap. This *should* be equal to * PMD_SIZE for most architectures. * - * Also make sure size of struct page is less than 64. We + * Also make sure size of struct page is less than 128. We * want to make sure we use large enough size here so that * we don't have a dynamic reserve space depending on * struct page size. But we also want to make sure we notice From patchwork Fri Aug 26 15:07:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956184 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 834D4ECAAD4 for ; Fri, 26 Aug 2022 15:08:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1F41B940012; Fri, 26 Aug 2022 11:08:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 17E16940007; Fri, 26 Aug 2022 11:08:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E4E39940012; Fri, 26 Aug 2022 11:08:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id CAD3A940007 for ; Fri, 26 Aug 2022 11:08:45 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 9EB7E1C6377 for ; Fri, 26 Aug 2022 15:08:45 +0000 (UTC) X-FDA: 79842075810.23.BBDC801 Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) by imf09.hostedemail.com (Postfix) with ESMTP id 045DB140042 for ; Fri, 26 Aug 2022 15:08:44 +0000 (UTC) Received: by mail-ej1-f73.google.com with SMTP id qf15-20020a1709077f0f00b0073d6dfb7556so717570ejc.12 for ; Fri, 26 Aug 2022 08:08:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc; bh=eB9ouCXgP1qqZdJonKnbXitFduJalY0opMmh3eEt0B8=; b=ZaEUcWpiEGxqcsbVGYdL8XcUyUT4za7rB58z2H/7YtDFty4EkDWPaPyvoKcvVACfr4 +mJm5xXZPATcxsAA6H1XsVkt/9iSXd9BzBVf5dAjgGJzAuv5mq5g++szRD9Pr3iQ/S91 1WX5KlYT4DRSsq8TKzGJGjI8Ob8gljM4z7rM3LoWynsuqTJuSFXNBdThbpEZ8fzk1Q5G KPKZQRwxi+PVHUJndiv220RCShfAsrDH1vF+wMtz/Z6J+Qkbm+3yFbbHXBoWkMZQV4Uf 8B3SgHgwl76AJKiGEdcgeX5W/bwwHbTSbduJIxHGMNsjY/l2y/kTAwAyBYyYtM64y0NY LAzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc; bh=eB9ouCXgP1qqZdJonKnbXitFduJalY0opMmh3eEt0B8=; b=p5NAyL5gmg2LpJzvaTE1waJFkQicboCdQ+HDSgqZ0hlj438iMi68NJS8G/8IN/dRop 4+yy9q63/Ec7WOiQXri5QFFVP6c9bQS1CNPJjLavlYwe/J0wrSY2NEkaaAXIRscTw+Dl fNSHv94G8vFovwC+4AlMxd/GdfaHMODNWf2oBCe3dO1lqdaMNx1qrOE+znapoA9MaTs0 FFJbpbHepiZwMwuu8Z5yYpt/Cug0MyHAFiVynzlK5sBba8ZkkseIEO3pQ/5nKpEMSS5S hnGn5j3vSuGjj/x/Vz5FeeTDm2T4+n2JIbA/FdIuEWbpjYaptEyBvYcMVmakaUWEv0uL R++A== X-Gm-Message-State: ACgBeo3SW4xkVQFJX+Bt/ELS9mnmakZhx8S/t/KrpLPUI+ERfOJ8hHLj 9l1lUklHEN7KfsMT1b1XLOEs74KwicI= X-Google-Smtp-Source: AA6agR6xcDnGo0oDwRX4VmpX9joMoikK/+wGua6z3xhzdQrYIgYVO7wwmJO/8eBWSQhdfDfgawYmdOtzFw0= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:aa7:d04a:0:b0:447:e24a:5cc9 with SMTP id n10-20020aa7d04a000000b00447e24a5cc9mr3049319edo.244.1661526523708; Fri, 26 Aug 2022 08:08:43 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:34 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-12-glider@google.com> Subject: [PATCH v5 11/44] kmsan: add KMSAN runtime core From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526525; a=rsa-sha256; cv=none; b=RDYckaeLgfBUGqKg9P8utJ4tyVLtnG/z+O7F88bCpZP0WS9mZi4TJjM//m+jyMbOogWgaJ wC43zM2MNVuZJofFDySEH3Cx5+4mXGeN6mUq8yPgiXUBvv6fohMvUMStQYGIIt/t00sQta +OohiQpun+A7YJFG+xr1L58+S9cWjBs= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=ZaEUcWpi; spf=pass (imf09.hostedemail.com: domain of 3--EIYwYKCAIinkfgtiqqing.eqonkpwz-oomxcem.qti@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3--EIYwYKCAIinkfgtiqqing.eqonkpwz-oomxcem.qti@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526525; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=eB9ouCXgP1qqZdJonKnbXitFduJalY0opMmh3eEt0B8=; b=Q/10XHk3S3/GpkQD/1ijn12CeGTr0YtAI8q7IJzYlv8mHTDRV9HcFqsyCNcxpDMJg2QsAE k1WyD2bkJU0BUo+hMNkhdhkx6L/m6XYDwYvfAjq2QK25W8EXxCef/Jf/7rF6B1ajrpGWKJ lernNFKPS59PkLoiowqfkfdVsVuy2bc= X-Stat-Signature: pjex7bwftrsyfkage8k7gxxzsn43mhun X-Rspamd-Queue-Id: 045DB140042 Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=ZaEUcWpi; spf=pass (imf09.hostedemail.com: domain of 3--EIYwYKCAIinkfgtiqqing.eqonkpwz-oomxcem.qti@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3--EIYwYKCAIinkfgtiqqing.eqonkpwz-oomxcem.qti@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspam-User: X-Rspamd-Server: rspam02 X-HE-Tag: 1661526524-350870 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: For each memory location KernelMemorySanitizer maintains two types of metadata: 1. The so-called shadow of that location - а byte:byte mapping describing whether or not individual bits of memory are initialized (shadow is 0) or not (shadow is 1). 2. The origins of that location - а 4-byte:4-byte mapping containing 4-byte IDs of the stack traces where uninitialized values were created. Each struct page now contains pointers to two struct pages holding KMSAN metadata (shadow and origins) for the original struct page. Utility routines in mm/kmsan/core.c and mm/kmsan/shadow.c handle the metadata creation, addressing, copying and checking. mm/kmsan/report.c performs error reporting in the cases an uninitialized value is used in a way that leads to undefined behavior. KMSAN compiler instrumentation is responsible for tracking the metadata along with the kernel memory. mm/kmsan/instrumentation.c provides the implementation for instrumentation hooks that are called from files compiled with -fsanitize=kernel-memory. To aid parameter passing (also done at instrumentation level), each task_struct now contains a struct kmsan_task_state used to track the metadata of function parameters and return values for that task. Finally, this patch provides CONFIG_KMSAN that enables KMSAN, and declares CFLAGS_KMSAN, which are applied to files compiled with KMSAN. The KMSAN_SANITIZE:=n Makefile directive can be used to completely disable KMSAN instrumentation for certain files. Similarly, KMSAN_ENABLE_CHECKS:=n disables KMSAN checks and makes newly created stack memory initialized. Users can also use functions from include/linux/kmsan-checks.h to mark certain memory regions as uninitialized or initialized (this is called "poisoning" and "unpoisoning") or check that a particular region is initialized. Signed-off-by: Alexander Potapenko Acked-by: Marco Elver --- v2: -- as requested by Greg K-H, moved hooks for different subsystems to respective patches, rewrote the patch description; -- addressed comments by Dmitry Vyukov; -- added a note about KMSAN being not intended for production use. -- fix case of unaligned dst in kmsan_internal_memmove_metadata() v3: -- print build IDs in reports where applicable -- drop redundant filter_irq_stacks(), unpoison the local passed to __stack_depot_save() -- remove a stray BUG() v4: (mostly fixes suggested by Marco Elver) -- add missing SPDX headers -- move CC_IS_CLANG && CLANG_VERSION under HAVE_KMSAN_COMPILER -- replace occurrences of |var| with @var -- reflow KMSAN_WARN_ON(), fix comments -- remove x86-specific code from shadow.c to improve portability -- convert kmsan_report_lock to raw spinlock -- add enter_runtime/exit_runtime around kmsan_internal_memmove_metadata() -- remove unnecessary include from kmsan.h (reported by ) -- introduce CONFIG_KMSAN_CHECK_PARAM_RETVAL (on by default), which maps to -fsanitize-memory-param-retval and makes KMSAN eagerly check values passed as function parameters and returned from functions. v5: -- do not return dummy shadow from within runtime -- preserve shadow when calling memcpy()/memmove()/memset() -- reword some code comments -- reapply clang-format, switch to modern style for-loops -- move kmsan_internal_is_vmalloc_addr() and kmsan_internal_is_module_addr() to the header -- refactor lib/Kconfig.kmsan as suggested by Marco Elver -- remove forward declaration of `struct page` from this patch Link: https://linux-review.googlesource.com/id/I9b71bfe3425466c97159f9de0062e5e8e4fec866 --- Makefile | 1 + include/linux/kmsan-checks.h | 64 +++++ include/linux/kmsan.h | 44 ++++ include/linux/mm_types.h | 12 + include/linux/sched.h | 5 + lib/Kconfig.debug | 1 + lib/Kconfig.kmsan | 50 ++++ mm/Makefile | 1 + mm/kmsan/Makefile | 23 ++ mm/kmsan/core.c | 448 +++++++++++++++++++++++++++++++++++ mm/kmsan/hooks.c | 66 ++++++ mm/kmsan/instrumentation.c | 307 ++++++++++++++++++++++++ mm/kmsan/kmsan.h | 203 ++++++++++++++++ mm/kmsan/report.c | 211 +++++++++++++++++ mm/kmsan/shadow.c | 147 ++++++++++++ scripts/Makefile.kmsan | 8 + scripts/Makefile.lib | 9 + 17 files changed, 1600 insertions(+) create mode 100644 include/linux/kmsan-checks.h create mode 100644 include/linux/kmsan.h create mode 100644 lib/Kconfig.kmsan create mode 100644 mm/kmsan/Makefile create mode 100644 mm/kmsan/core.c create mode 100644 mm/kmsan/hooks.c create mode 100644 mm/kmsan/instrumentation.c create mode 100644 mm/kmsan/kmsan.h create mode 100644 mm/kmsan/report.c create mode 100644 mm/kmsan/shadow.c create mode 100644 scripts/Makefile.kmsan diff --git a/Makefile b/Makefile index c7705f7496012..4552a15bfa4fe 100644 --- a/Makefile +++ b/Makefile @@ -1015,6 +1015,7 @@ include-y := scripts/Makefile.extrawarn include-$(CONFIG_DEBUG_INFO) += scripts/Makefile.debug include-$(CONFIG_KASAN) += scripts/Makefile.kasan include-$(CONFIG_KCSAN) += scripts/Makefile.kcsan +include-$(CONFIG_KMSAN) += scripts/Makefile.kmsan include-$(CONFIG_UBSAN) += scripts/Makefile.ubsan include-$(CONFIG_KCOV) += scripts/Makefile.kcov include-$(CONFIG_RANDSTRUCT) += scripts/Makefile.randstruct diff --git a/include/linux/kmsan-checks.h b/include/linux/kmsan-checks.h new file mode 100644 index 0000000000000..a6522a0c28df9 --- /dev/null +++ b/include/linux/kmsan-checks.h @@ -0,0 +1,64 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * KMSAN checks to be used for one-off annotations in subsystems. + * + * Copyright (C) 2017-2022 Google LLC + * Author: Alexander Potapenko + * + */ + +#ifndef _LINUX_KMSAN_CHECKS_H +#define _LINUX_KMSAN_CHECKS_H + +#include + +#ifdef CONFIG_KMSAN + +/** + * kmsan_poison_memory() - Mark the memory range as uninitialized. + * @address: address to start with. + * @size: size of buffer to poison. + * @flags: GFP flags for allocations done by this function. + * + * Until other data is written to this range, KMSAN will treat it as + * uninitialized. Error reports for this memory will reference the call site of + * kmsan_poison_memory() as origin. + */ +void kmsan_poison_memory(const void *address, size_t size, gfp_t flags); + +/** + * kmsan_unpoison_memory() - Mark the memory range as initialized. + * @address: address to start with. + * @size: size of buffer to unpoison. + * + * Until other data is written to this range, KMSAN will treat it as + * initialized. + */ +void kmsan_unpoison_memory(const void *address, size_t size); + +/** + * kmsan_check_memory() - Check the memory range for being initialized. + * @address: address to start with. + * @size: size of buffer to check. + * + * If any piece of the given range is marked as uninitialized, KMSAN will report + * an error. + */ +void kmsan_check_memory(const void *address, size_t size); + +#else + +static inline void kmsan_poison_memory(const void *address, size_t size, + gfp_t flags) +{ +} +static inline void kmsan_unpoison_memory(const void *address, size_t size) +{ +} +static inline void kmsan_check_memory(const void *address, size_t size) +{ +} + +#endif + +#endif /* _LINUX_KMSAN_CHECKS_H */ diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h new file mode 100644 index 0000000000000..e578c9627b641 --- /dev/null +++ b/include/linux/kmsan.h @@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * KMSAN API for subsystems. + * + * Copyright (C) 2017-2022 Google LLC + * Author: Alexander Potapenko + * + */ +#ifndef _LINUX_KMSAN_H +#define _LINUX_KMSAN_H + +#include +#include +#include +#include + +#ifdef CONFIG_KMSAN + +/* These constants are defined in the MSan LLVM instrumentation pass. */ +#define KMSAN_RETVAL_SIZE 800 +#define KMSAN_PARAM_SIZE 800 + +struct kmsan_context_state { + char param_tls[KMSAN_PARAM_SIZE]; + char retval_tls[KMSAN_RETVAL_SIZE]; + char va_arg_tls[KMSAN_PARAM_SIZE]; + char va_arg_origin_tls[KMSAN_PARAM_SIZE]; + u64 va_arg_overflow_size_tls; + char param_origin_tls[KMSAN_PARAM_SIZE]; + depot_stack_handle_t retval_origin_tls; +}; + +#undef KMSAN_PARAM_SIZE +#undef KMSAN_RETVAL_SIZE + +struct kmsan_ctx { + struct kmsan_context_state cstate; + int kmsan_in_runtime; + bool allow_reporting; +}; + +#endif + +#endif /* _LINUX_KMSAN_H */ diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index cf97f3884fda2..8be4f34cb8caa 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -223,6 +223,18 @@ struct page { not kmapped, ie. highmem) */ #endif /* WANT_PAGE_VIRTUAL */ +#ifdef CONFIG_KMSAN + /* + * KMSAN metadata for this page: + * - shadow page: every bit indicates whether the corresponding + * bit of the original page is initialized (0) or not (1); + * - origin page: every 4 bytes contain an id of the stack trace + * where the uninitialized value was created. + */ + struct page *kmsan_shadow; + struct page *kmsan_origin; +#endif + #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS int _last_cpupid; #endif diff --git a/include/linux/sched.h b/include/linux/sched.h index e7b2f8a5c711c..ca4b434bc778e 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -1355,6 +1356,10 @@ struct task_struct { #endif #endif +#ifdef CONFIG_KMSAN + struct kmsan_ctx kmsan_ctx; +#endif + #if IS_ENABLED(CONFIG_KUNIT) struct kunit *kunit_test; #endif diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 072e4b289c13e..1f80db29728a7 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -971,6 +971,7 @@ config DEBUG_STACKOVERFLOW source "lib/Kconfig.kasan" source "lib/Kconfig.kfence" +source "lib/Kconfig.kmsan" endmenu # "Memory Debugging" diff --git a/lib/Kconfig.kmsan b/lib/Kconfig.kmsan new file mode 100644 index 0000000000000..5b19dbd34d76e --- /dev/null +++ b/lib/Kconfig.kmsan @@ -0,0 +1,50 @@ +# SPDX-License-Identifier: GPL-2.0-only +config HAVE_ARCH_KMSAN + bool + +config HAVE_KMSAN_COMPILER + # Clang versions <14.0.0 also support -fsanitize=kernel-memory, but not + # all the features necessary to build the kernel with KMSAN. + depends on CC_IS_CLANG && CLANG_VERSION >= 140000 + def_bool $(cc-option,-fsanitize=kernel-memory -mllvm -msan-disable-checks=1) + +config KMSAN + bool "KMSAN: detector of uninitialized values use" + depends on HAVE_ARCH_KMSAN && HAVE_KMSAN_COMPILER + depends on SLUB && DEBUG_KERNEL && !KASAN && !KCSAN + select STACKDEPOT + select STACKDEPOT_ALWAYS_INIT + help + KernelMemorySanitizer (KMSAN) is a dynamic detector of uses of + uninitialized values in the kernel. It is based on compiler + instrumentation provided by Clang and thus requires Clang to build. + + An important note is that KMSAN is not intended for production use, + because it drastically increases kernel memory footprint and slows + the whole system down. + + See for more details. + +if KMSAN + +config HAVE_KMSAN_PARAM_RETVAL + # -fsanitize-memory-param-retval is supported only by Clang >= 14. + depends on HAVE_KMSAN_COMPILER + def_bool $(cc-option,-fsanitize=kernel-memory -fsanitize-memory-param-retval) + +config KMSAN_CHECK_PARAM_RETVAL + bool "Check for uninitialized values passed to and returned from functions" + default y + depends on HAVE_KMSAN_PARAM_RETVAL + help + If the compiler supports -fsanitize-memory-param-retval, KMSAN will + eagerly check every function parameter passed by value and every + function return value. + + Disabling KMSAN_CHECK_PARAM_RETVAL will result in tracking shadow for + function parameters and return values across function borders. This + is a more relaxed mode, but it generates more instrumentation code and + may potentially report errors in corner cases when non-instrumented + functions call instrumented ones. + +endif diff --git a/mm/Makefile b/mm/Makefile index 9a564f8364035..cce88e5b6d76f 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -89,6 +89,7 @@ obj-$(CONFIG_SLAB) += slab.o obj-$(CONFIG_SLUB) += slub.o obj-$(CONFIG_KASAN) += kasan/ obj-$(CONFIG_KFENCE) += kfence/ +obj-$(CONFIG_KMSAN) += kmsan/ obj-$(CONFIG_FAILSLAB) += failslab.o obj-$(CONFIG_MEMTEST) += memtest.o obj-$(CONFIG_MIGRATION) += migrate.o diff --git a/mm/kmsan/Makefile b/mm/kmsan/Makefile new file mode 100644 index 0000000000000..550ad8625e4f9 --- /dev/null +++ b/mm/kmsan/Makefile @@ -0,0 +1,23 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Makefile for KernelMemorySanitizer (KMSAN). +# +# +obj-y := core.o instrumentation.o hooks.o report.o shadow.o + +KMSAN_SANITIZE := n +KCOV_INSTRUMENT := n +UBSAN_SANITIZE := n + +# 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) +CC_FLAGS_KMSAN_RUNTIME += -DDISABLE_BRANCH_PROFILING + +CFLAGS_REMOVE.o = $(CC_FLAGS_FTRACE) + +CFLAGS_core.o := $(CC_FLAGS_KMSAN_RUNTIME) +CFLAGS_hooks.o := $(CC_FLAGS_KMSAN_RUNTIME) +CFLAGS_instrumentation.o := $(CC_FLAGS_KMSAN_RUNTIME) +CFLAGS_report.o := $(CC_FLAGS_KMSAN_RUNTIME) +CFLAGS_shadow.o := $(CC_FLAGS_KMSAN_RUNTIME) diff --git a/mm/kmsan/core.c b/mm/kmsan/core.c new file mode 100644 index 0000000000000..a5dec6e62b4ef --- /dev/null +++ b/mm/kmsan/core.c @@ -0,0 +1,448 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KMSAN runtime library. + * + * Copyright (C) 2017-2022 Google LLC + * Author: Alexander Potapenko + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../slab.h" +#include "kmsan.h" + +/* + * Avoid creating too long origin chains, these are unlikely to participate in + * real reports. + */ +#define MAX_CHAIN_DEPTH 7 +#define NUM_SKIPPED_TO_WARN 10000 + +bool kmsan_enabled __read_mostly; + +/* + * Per-CPU KMSAN context to be used in interrupts, where current->kmsan is + * unavaliable. + */ +DEFINE_PER_CPU(struct kmsan_ctx, kmsan_percpu_ctx); + +void kmsan_internal_poison_memory(void *address, size_t size, gfp_t flags, + unsigned int poison_flags) +{ + u32 extra_bits = + kmsan_extra_bits(/*depth*/ 0, poison_flags & KMSAN_POISON_FREE); + bool checked = poison_flags & KMSAN_POISON_CHECK; + depot_stack_handle_t handle; + + handle = kmsan_save_stack_with_flags(flags, extra_bits); + kmsan_internal_set_shadow_origin(address, size, -1, handle, checked); +} + +void kmsan_internal_unpoison_memory(void *address, size_t size, bool checked) +{ + kmsan_internal_set_shadow_origin(address, size, 0, 0, checked); +} + +depot_stack_handle_t kmsan_save_stack_with_flags(gfp_t flags, + unsigned int extra) +{ + unsigned long entries[KMSAN_STACK_DEPTH]; + unsigned int nr_entries; + + nr_entries = stack_trace_save(entries, KMSAN_STACK_DEPTH, 0); + + /* Don't sleep (see might_sleep_if() in __alloc_pages_nodemask()). */ + flags &= ~__GFP_DIRECT_RECLAIM; + + return __stack_depot_save(entries, nr_entries, extra, flags, true); +} + +/* Copy the metadata following the memmove() behavior. */ +void kmsan_internal_memmove_metadata(void *dst, void *src, size_t n) +{ + depot_stack_handle_t old_origin = 0, new_origin = 0; + int src_slots, dst_slots, i, iter, step, skip_bits; + depot_stack_handle_t *origin_src, *origin_dst; + void *shadow_src, *shadow_dst; + u32 *align_shadow_src, shadow; + bool backwards; + + shadow_dst = kmsan_get_metadata(dst, KMSAN_META_SHADOW); + if (!shadow_dst) + return; + KMSAN_WARN_ON(!kmsan_metadata_is_contiguous(dst, n)); + + shadow_src = kmsan_get_metadata(src, KMSAN_META_SHADOW); + if (!shadow_src) { + /* + * @src is untracked: zero out destination shadow, ignore the + * origins, we're done. + */ + __memset(shadow_dst, 0, n); + return; + } + KMSAN_WARN_ON(!kmsan_metadata_is_contiguous(src, n)); + + __memmove(shadow_dst, shadow_src, n); + + origin_dst = kmsan_get_metadata(dst, KMSAN_META_ORIGIN); + origin_src = kmsan_get_metadata(src, KMSAN_META_ORIGIN); + KMSAN_WARN_ON(!origin_dst || !origin_src); + src_slots = (ALIGN((u64)src + n, KMSAN_ORIGIN_SIZE) - + ALIGN_DOWN((u64)src, KMSAN_ORIGIN_SIZE)) / + KMSAN_ORIGIN_SIZE; + dst_slots = (ALIGN((u64)dst + n, KMSAN_ORIGIN_SIZE) - + ALIGN_DOWN((u64)dst, KMSAN_ORIGIN_SIZE)) / + KMSAN_ORIGIN_SIZE; + KMSAN_WARN_ON((src_slots < 1) || (dst_slots < 1)); + KMSAN_WARN_ON((src_slots - dst_slots > 1) || + (dst_slots - src_slots < -1)); + + backwards = dst > src; + i = backwards ? min(src_slots, dst_slots) - 1 : 0; + iter = backwards ? -1 : 1; + + align_shadow_src = + (u32 *)ALIGN_DOWN((u64)shadow_src, KMSAN_ORIGIN_SIZE); + for (step = 0; step < min(src_slots, dst_slots); step++, i += iter) { + KMSAN_WARN_ON(i < 0); + shadow = align_shadow_src[i]; + if (i == 0) { + /* + * If @src isn't aligned on KMSAN_ORIGIN_SIZE, don't + * look at the first @src % KMSAN_ORIGIN_SIZE bytes + * of the first shadow slot. + */ + skip_bits = ((u64)src % KMSAN_ORIGIN_SIZE) * 8; + shadow = (shadow >> skip_bits) << skip_bits; + } + if (i == src_slots - 1) { + /* + * If @src + n isn't aligned on + * KMSAN_ORIGIN_SIZE, don't look at the last + * (@src + n) % KMSAN_ORIGIN_SIZE bytes of the + * last shadow slot. + */ + skip_bits = (((u64)src + n) % KMSAN_ORIGIN_SIZE) * 8; + shadow = (shadow << skip_bits) >> skip_bits; + } + /* + * Overwrite the origin only if the corresponding + * shadow is nonempty. + */ + if (origin_src[i] && (origin_src[i] != old_origin) && shadow) { + old_origin = origin_src[i]; + new_origin = kmsan_internal_chain_origin(old_origin); + /* + * kmsan_internal_chain_origin() may return + * NULL, but we don't want to lose the previous + * origin value. + */ + if (!new_origin) + new_origin = old_origin; + } + if (shadow) + origin_dst[i] = new_origin; + else + origin_dst[i] = 0; + } + /* + * If dst_slots is greater than src_slots (i.e. + * dst_slots == src_slots + 1), there is an extra origin slot at the + * beginning or end of the destination buffer, for which we take the + * origin from the previous slot. + * This is only done if the part of the source shadow corresponding to + * slot is non-zero. + * + * E.g. if we copy 8 aligned bytes that are marked as uninitialized + * and have origins o111 and o222, to an unaligned buffer with offset 1, + * these two origins are copied to three origin slots, so one of then + * needs to be duplicated, depending on the copy direction (@backwards) + * + * src shadow: |uuuu|uuuu|....| + * src origin: |o111|o222|....| + * + * backwards = 0: + * dst shadow: |.uuu|uuuu|u...| + * dst origin: |....|o111|o222| - fill the empty slot with o111 + * backwards = 1: + * dst shadow: |.uuu|uuuu|u...| + * dst origin: |o111|o222|....| - fill the empty slot with o222 + */ + if (src_slots < dst_slots) { + if (backwards) { + shadow = align_shadow_src[src_slots - 1]; + skip_bits = (((u64)dst + n) % KMSAN_ORIGIN_SIZE) * 8; + shadow = (shadow << skip_bits) >> skip_bits; + if (shadow) + /* src_slots > 0, therefore dst_slots is at least 2 */ + origin_dst[dst_slots - 1] = + origin_dst[dst_slots - 2]; + } else { + shadow = align_shadow_src[0]; + skip_bits = ((u64)dst % KMSAN_ORIGIN_SIZE) * 8; + shadow = (shadow >> skip_bits) << skip_bits; + if (shadow) + origin_dst[0] = origin_dst[1]; + } + } +} + +depot_stack_handle_t kmsan_internal_chain_origin(depot_stack_handle_t id) +{ + unsigned long entries[3]; + u32 extra_bits; + int depth; + bool uaf; + + if (!id) + return id; + /* + * Make sure we have enough spare bits in @id to hold the UAF bit and + * the chain depth. + */ + BUILD_BUG_ON((1 << STACK_DEPOT_EXTRA_BITS) <= (MAX_CHAIN_DEPTH << 1)); + + extra_bits = stack_depot_get_extra_bits(id); + depth = kmsan_depth_from_eb(extra_bits); + uaf = kmsan_uaf_from_eb(extra_bits); + + if (depth >= MAX_CHAIN_DEPTH) { + static atomic_long_t kmsan_skipped_origins; + long skipped = atomic_long_inc_return(&kmsan_skipped_origins); + + if (skipped % NUM_SKIPPED_TO_WARN == 0) { + pr_warn("not chained %ld origins\n", skipped); + dump_stack(); + kmsan_print_origin(id); + } + return id; + } + depth++; + extra_bits = kmsan_extra_bits(depth, uaf); + + entries[0] = KMSAN_CHAIN_MAGIC_ORIGIN; + entries[1] = kmsan_save_stack_with_flags(GFP_ATOMIC, 0); + entries[2] = id; + /* + * @entries is a local var in non-instrumented code, so KMSAN does not + * know it is initialized. Explicitly unpoison it to avoid false + * positives when __stack_depot_save() passes it to instrumented code. + */ + kmsan_internal_unpoison_memory(entries, sizeof(entries), false); + return __stack_depot_save(entries, ARRAY_SIZE(entries), extra_bits, + GFP_ATOMIC, true); +} + +void kmsan_internal_set_shadow_origin(void *addr, size_t size, int b, + u32 origin, bool checked) +{ + u64 address = (u64)addr; + void *shadow_start; + u32 *origin_start; + size_t pad = 0; + + KMSAN_WARN_ON(!kmsan_metadata_is_contiguous(addr, size)); + shadow_start = kmsan_get_metadata(addr, KMSAN_META_SHADOW); + if (!shadow_start) { + /* + * kmsan_metadata_is_contiguous() is true, so either all shadow + * and origin pages are NULL, or all are non-NULL. + */ + if (checked) { + pr_err("%s: not memsetting %ld bytes starting at %px, because the shadow is NULL\n", + __func__, size, addr); + KMSAN_WARN_ON(true); + } + return; + } + __memset(shadow_start, b, size); + + if (!IS_ALIGNED(address, KMSAN_ORIGIN_SIZE)) { + pad = address % KMSAN_ORIGIN_SIZE; + address -= pad; + size += pad; + } + size = ALIGN(size, KMSAN_ORIGIN_SIZE); + origin_start = + (u32 *)kmsan_get_metadata((void *)address, KMSAN_META_ORIGIN); + + for (int i = 0; i < size / KMSAN_ORIGIN_SIZE; i++) + origin_start[i] = origin; +} + +struct page *kmsan_vmalloc_to_page_or_null(void *vaddr) +{ + struct page *page; + + if (!kmsan_internal_is_vmalloc_addr(vaddr) && + !kmsan_internal_is_module_addr(vaddr)) + return NULL; + page = vmalloc_to_page(vaddr); + if (pfn_valid(page_to_pfn(page))) + return page; + else + return NULL; +} + +void kmsan_internal_check_memory(void *addr, size_t size, const void *user_addr, + int reason) +{ + depot_stack_handle_t cur_origin = 0, new_origin = 0; + unsigned long addr64 = (unsigned long)addr; + depot_stack_handle_t *origin = NULL; + unsigned char *shadow = NULL; + int cur_off_start = -1; + int chunk_size; + size_t pos = 0; + + if (!size) + return; + KMSAN_WARN_ON(!kmsan_metadata_is_contiguous(addr, size)); + while (pos < size) { + chunk_size = min(size - pos, + PAGE_SIZE - ((addr64 + pos) % PAGE_SIZE)); + shadow = kmsan_get_metadata((void *)(addr64 + pos), + KMSAN_META_SHADOW); + if (!shadow) { + /* + * This page is untracked. If there were uninitialized + * bytes before, report them. + */ + if (cur_origin) { + kmsan_enter_runtime(); + kmsan_report(cur_origin, addr, size, + cur_off_start, pos - 1, user_addr, + reason); + kmsan_leave_runtime(); + } + cur_origin = 0; + cur_off_start = -1; + pos += chunk_size; + continue; + } + for (int i = 0; i < chunk_size; i++) { + if (!shadow[i]) { + /* + * This byte is unpoisoned. If there were + * poisoned bytes before, report them. + */ + if (cur_origin) { + kmsan_enter_runtime(); + kmsan_report(cur_origin, addr, size, + cur_off_start, pos + i - 1, + user_addr, reason); + kmsan_leave_runtime(); + } + cur_origin = 0; + cur_off_start = -1; + continue; + } + origin = kmsan_get_metadata((void *)(addr64 + pos + i), + KMSAN_META_ORIGIN); + KMSAN_WARN_ON(!origin); + new_origin = *origin; + /* + * Encountered new origin - report the previous + * uninitialized range. + */ + if (cur_origin != new_origin) { + if (cur_origin) { + kmsan_enter_runtime(); + kmsan_report(cur_origin, addr, size, + cur_off_start, pos + i - 1, + user_addr, reason); + kmsan_leave_runtime(); + } + cur_origin = new_origin; + cur_off_start = pos + i; + } + } + pos += chunk_size; + } + KMSAN_WARN_ON(pos != size); + if (cur_origin) { + kmsan_enter_runtime(); + kmsan_report(cur_origin, addr, size, cur_off_start, pos - 1, + user_addr, reason); + kmsan_leave_runtime(); + } +} + +bool kmsan_metadata_is_contiguous(void *addr, size_t size) +{ + char *cur_shadow = NULL, *next_shadow = NULL, *cur_origin = NULL, + *next_origin = NULL; + u64 cur_addr = (u64)addr, next_addr = cur_addr + PAGE_SIZE; + depot_stack_handle_t *origin_p; + bool all_untracked = false; + + if (!size) + return true; + + /* The whole range belongs to the same page. */ + if (ALIGN_DOWN(cur_addr + size - 1, PAGE_SIZE) == + ALIGN_DOWN(cur_addr, PAGE_SIZE)) + return true; + + cur_shadow = kmsan_get_metadata((void *)cur_addr, /*is_origin*/ false); + if (!cur_shadow) + all_untracked = true; + cur_origin = kmsan_get_metadata((void *)cur_addr, /*is_origin*/ true); + if (all_untracked && cur_origin) + goto report; + + for (; next_addr < (u64)addr + size; + cur_addr = next_addr, cur_shadow = next_shadow, + cur_origin = next_origin, next_addr += PAGE_SIZE) { + next_shadow = kmsan_get_metadata((void *)next_addr, false); + next_origin = kmsan_get_metadata((void *)next_addr, true); + if (all_untracked) { + if (next_shadow || next_origin) + goto report; + if (!next_shadow && !next_origin) + continue; + } + if (((u64)cur_shadow == ((u64)next_shadow - PAGE_SIZE)) && + ((u64)cur_origin == ((u64)next_origin - PAGE_SIZE))) + continue; + goto report; + } + return true; + +report: + pr_err("%s: attempting to access two shadow page ranges.\n", __func__); + pr_err("Access of size %ld at %px.\n", size, addr); + pr_err("Addresses belonging to different ranges: %px and %px\n", + (void *)cur_addr, (void *)next_addr); + pr_err("page[0].shadow: %px, page[1].shadow: %px\n", cur_shadow, + next_shadow); + pr_err("page[0].origin: %px, page[1].origin: %px\n", cur_origin, + next_origin); + origin_p = kmsan_get_metadata(addr, KMSAN_META_ORIGIN); + if (origin_p) { + pr_err("Origin: %08x\n", *origin_p); + kmsan_print_origin(*origin_p); + } else { + pr_err("Origin: unavailable\n"); + } + return false; +} diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c new file mode 100644 index 0000000000000..4ac62fa67a02a --- /dev/null +++ b/mm/kmsan/hooks.c @@ -0,0 +1,66 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KMSAN hooks for kernel subsystems. + * + * These functions handle creation of KMSAN metadata for memory allocations. + * + * Copyright (C) 2018-2022 Google LLC + * Author: Alexander Potapenko + * + */ + +#include +#include +#include +#include +#include +#include + +#include "../internal.h" +#include "../slab.h" +#include "kmsan.h" + +/* + * Instrumented functions shouldn't be called under + * kmsan_enter_runtime()/kmsan_leave_runtime(), because this will lead to + * skipping effects of functions like memset() inside instrumented code. + */ + +/* Functions from kmsan-checks.h follow. */ +void kmsan_poison_memory(const void *address, size_t size, gfp_t flags) +{ + if (!kmsan_enabled || kmsan_in_runtime()) + return; + kmsan_enter_runtime(); + /* The users may want to poison/unpoison random memory. */ + kmsan_internal_poison_memory((void *)address, size, flags, + KMSAN_POISON_NOCHECK); + kmsan_leave_runtime(); +} +EXPORT_SYMBOL(kmsan_poison_memory); + +void kmsan_unpoison_memory(const void *address, size_t size) +{ + unsigned long ua_flags; + + if (!kmsan_enabled || kmsan_in_runtime()) + return; + + ua_flags = user_access_save(); + kmsan_enter_runtime(); + /* The users may want to poison/unpoison random memory. */ + kmsan_internal_unpoison_memory((void *)address, size, + KMSAN_POISON_NOCHECK); + kmsan_leave_runtime(); + user_access_restore(ua_flags); +} +EXPORT_SYMBOL(kmsan_unpoison_memory); + +void kmsan_check_memory(const void *addr, size_t size) +{ + if (!kmsan_enabled) + return; + return kmsan_internal_check_memory((void *)addr, size, /*user_addr*/ 0, + REASON_ANY); +} +EXPORT_SYMBOL(kmsan_check_memory); diff --git a/mm/kmsan/instrumentation.c b/mm/kmsan/instrumentation.c new file mode 100644 index 0000000000000..280d154132684 --- /dev/null +++ b/mm/kmsan/instrumentation.c @@ -0,0 +1,307 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KMSAN compiler API. + * + * This file implements __msan_XXX hooks that Clang inserts into the code + * compiled with -fsanitize=kernel-memory. + * See Documentation/dev-tools/kmsan.rst for more information on how KMSAN + * instrumentation works. + * + * Copyright (C) 2017-2022 Google LLC + * Author: Alexander Potapenko + * + */ + +#include "kmsan.h" +#include +#include +#include + +static inline bool is_bad_asm_addr(void *addr, uintptr_t size, bool is_store) +{ + if ((u64)addr < TASK_SIZE) + return true; + if (!kmsan_get_metadata(addr, KMSAN_META_SHADOW)) + return true; + return false; +} + +static inline struct shadow_origin_ptr +get_shadow_origin_ptr(void *addr, u64 size, bool store) +{ + unsigned long ua_flags = user_access_save(); + struct shadow_origin_ptr ret; + + ret = kmsan_get_shadow_origin_ptr(addr, size, store); + user_access_restore(ua_flags); + return ret; +} + +/* Get shadow and origin pointers for a memory load with non-standard size. */ +struct shadow_origin_ptr __msan_metadata_ptr_for_load_n(void *addr, + uintptr_t size) +{ + return get_shadow_origin_ptr(addr, size, /*store*/ false); +} +EXPORT_SYMBOL(__msan_metadata_ptr_for_load_n); + +/* Get shadow and origin pointers for a memory store with non-standard size. */ +struct shadow_origin_ptr __msan_metadata_ptr_for_store_n(void *addr, + uintptr_t size) +{ + return get_shadow_origin_ptr(addr, size, /*store*/ true); +} +EXPORT_SYMBOL(__msan_metadata_ptr_for_store_n); + +/* + * Declare functions that obtain shadow/origin pointers for loads and stores + * with fixed size. + */ +#define DECLARE_METADATA_PTR_GETTER(size) \ + struct shadow_origin_ptr __msan_metadata_ptr_for_load_##size( \ + void *addr) \ + { \ + return get_shadow_origin_ptr(addr, size, /*store*/ false); \ + } \ + EXPORT_SYMBOL(__msan_metadata_ptr_for_load_##size); \ + struct shadow_origin_ptr __msan_metadata_ptr_for_store_##size( \ + void *addr) \ + { \ + return get_shadow_origin_ptr(addr, size, /*store*/ true); \ + } \ + EXPORT_SYMBOL(__msan_metadata_ptr_for_store_##size) + +DECLARE_METADATA_PTR_GETTER(1); +DECLARE_METADATA_PTR_GETTER(2); +DECLARE_METADATA_PTR_GETTER(4); +DECLARE_METADATA_PTR_GETTER(8); + +/* + * Handle a memory store performed by inline assembly. KMSAN conservatively + * attempts to unpoison the outputs of asm() directives to prevent false + * positives caused by missed stores. + */ +void __msan_instrument_asm_store(void *addr, uintptr_t size) +{ + unsigned long ua_flags; + + if (!kmsan_enabled || kmsan_in_runtime()) + return; + + ua_flags = user_access_save(); + /* + * Most of the accesses are below 32 bytes. The two exceptions so far + * are clwb() (64 bytes) and FPU state (512 bytes). + * It's unlikely that the assembly will touch more than 512 bytes. + */ + if (size > 512) { + WARN_ONCE(1, "assembly store size too big: %ld\n", size); + size = 8; + } + if (is_bad_asm_addr(addr, size, /*is_store*/ true)) { + user_access_restore(ua_flags); + return; + } + kmsan_enter_runtime(); + /* Unpoisoning the memory on best effort. */ + kmsan_internal_unpoison_memory(addr, size, /*checked*/ false); + kmsan_leave_runtime(); + user_access_restore(ua_flags); +} +EXPORT_SYMBOL(__msan_instrument_asm_store); + +/* + * KMSAN instrumentation pass replaces LLVM memcpy, memmove and memset + * intrinsics with calls to respective __msan_ functions. We use + * get_param0_metadata() and set_retval_metadata() to store the shadow/origin + * values for the destination argument of these functions and use them for the + * functions' return values. + */ +static inline void get_param0_metadata(u64 *shadow, + depot_stack_handle_t *origin) +{ + struct kmsan_ctx *ctx = kmsan_get_context(); + + *shadow = *(u64 *)(ctx->cstate.param_tls); + *origin = ctx->cstate.param_origin_tls[0]; +} + +static inline void set_retval_metadata(u64 shadow, depot_stack_handle_t origin) +{ + struct kmsan_ctx *ctx = kmsan_get_context(); + + *(u64 *)(ctx->cstate.retval_tls) = shadow; + ctx->cstate.retval_origin_tls = origin; +} + +/* Handle llvm.memmove intrinsic. */ +void *__msan_memmove(void *dst, const void *src, uintptr_t n) +{ + depot_stack_handle_t origin; + void *result; + u64 shadow; + + get_param0_metadata(&shadow, &origin); + result = __memmove(dst, src, n); + if (!n) + /* Some people call memmove() with zero length. */ + return result; + if (!kmsan_enabled || kmsan_in_runtime()) + return result; + + kmsan_enter_runtime(); + kmsan_internal_memmove_metadata(dst, (void *)src, n); + kmsan_leave_runtime(); + + set_retval_metadata(shadow, origin); + return result; +} +EXPORT_SYMBOL(__msan_memmove); + +/* Handle llvm.memcpy intrinsic. */ +void *__msan_memcpy(void *dst, const void *src, uintptr_t n) +{ + depot_stack_handle_t origin; + void *result; + u64 shadow; + + get_param0_metadata(&shadow, &origin); + result = __memcpy(dst, src, n); + if (!n) + /* Some people call memcpy() with zero length. */ + return result; + + if (!kmsan_enabled || kmsan_in_runtime()) + return result; + + kmsan_enter_runtime(); + /* Using memmove instead of memcpy doesn't affect correctness. */ + kmsan_internal_memmove_metadata(dst, (void *)src, n); + kmsan_leave_runtime(); + + set_retval_metadata(shadow, origin); + return result; +} +EXPORT_SYMBOL(__msan_memcpy); + +/* Handle llvm.memset intrinsic. */ +void *__msan_memset(void *dst, int c, uintptr_t n) +{ + depot_stack_handle_t origin; + void *result; + u64 shadow; + + get_param0_metadata(&shadow, &origin); + result = __memset(dst, c, n); + if (!kmsan_enabled || kmsan_in_runtime()) + return result; + + kmsan_enter_runtime(); + /* + * Clang doesn't pass parameter metadata here, so it is impossible to + * use shadow of @c to set up the shadow for @dst. + */ + kmsan_internal_unpoison_memory(dst, n, /*checked*/ false); + kmsan_leave_runtime(); + + set_retval_metadata(shadow, origin); + return result; +} +EXPORT_SYMBOL(__msan_memset); + +/* + * Create a new origin from an old one. This is done when storing an + * uninitialized value to memory. When reporting an error, KMSAN unrolls and + * prints the whole chain of stores that preceded the use of this value. + */ +depot_stack_handle_t __msan_chain_origin(depot_stack_handle_t origin) +{ + depot_stack_handle_t ret = 0; + unsigned long ua_flags; + + if (!kmsan_enabled || kmsan_in_runtime()) + return ret; + + ua_flags = user_access_save(); + + /* Creating new origins may allocate memory. */ + kmsan_enter_runtime(); + ret = kmsan_internal_chain_origin(origin); + kmsan_leave_runtime(); + user_access_restore(ua_flags); + return ret; +} +EXPORT_SYMBOL(__msan_chain_origin); + +/* Poison a local variable when entering a function. */ +void __msan_poison_alloca(void *address, uintptr_t size, char *descr) +{ + depot_stack_handle_t handle; + unsigned long entries[4]; + unsigned long ua_flags; + + if (!kmsan_enabled || kmsan_in_runtime()) + return; + + ua_flags = user_access_save(); + entries[0] = KMSAN_ALLOCA_MAGIC_ORIGIN; + entries[1] = (u64)descr; + entries[2] = (u64)__builtin_return_address(0); + /* + * With frame pointers enabled, it is possible to quickly fetch the + * second frame of the caller stack without calling the unwinder. + * Without them, simply do not bother. + */ + if (IS_ENABLED(CONFIG_UNWINDER_FRAME_POINTER)) + entries[3] = (u64)__builtin_return_address(1); + else + entries[3] = 0; + + /* stack_depot_save() may allocate memory. */ + kmsan_enter_runtime(); + handle = stack_depot_save(entries, ARRAY_SIZE(entries), GFP_ATOMIC); + kmsan_leave_runtime(); + + kmsan_internal_set_shadow_origin(address, size, -1, handle, + /*checked*/ true); + user_access_restore(ua_flags); +} +EXPORT_SYMBOL(__msan_poison_alloca); + +/* Unpoison a local variable. */ +void __msan_unpoison_alloca(void *address, uintptr_t size) +{ + if (!kmsan_enabled || kmsan_in_runtime()) + return; + + kmsan_enter_runtime(); + kmsan_internal_unpoison_memory(address, size, /*checked*/ true); + kmsan_leave_runtime(); +} +EXPORT_SYMBOL(__msan_unpoison_alloca); + +/* + * Report that an uninitialized value with the given origin was used in a way + * that constituted undefined behavior. + */ +void __msan_warning(u32 origin) +{ + if (!kmsan_enabled || kmsan_in_runtime()) + return; + kmsan_enter_runtime(); + kmsan_report(origin, /*address*/ 0, /*size*/ 0, + /*off_first*/ 0, /*off_last*/ 0, /*user_addr*/ 0, + REASON_ANY); + kmsan_leave_runtime(); +} +EXPORT_SYMBOL(__msan_warning); + +/* + * At the beginning of an instrumented function, obtain the pointer to + * `struct kmsan_context_state` holding the metadata for function parameters. + */ +struct kmsan_context_state *__msan_get_context_state(void) +{ + return &kmsan_get_context()->cstate; +} +EXPORT_SYMBOL(__msan_get_context_state); diff --git a/mm/kmsan/kmsan.h b/mm/kmsan/kmsan.h new file mode 100644 index 0000000000000..6b9deee3b7f32 --- /dev/null +++ b/mm/kmsan/kmsan.h @@ -0,0 +1,203 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Functions used by the KMSAN runtime. + * + * Copyright (C) 2017-2022 Google LLC + * Author: Alexander Potapenko + * + */ + +#ifndef __MM_KMSAN_KMSAN_H +#define __MM_KMSAN_KMSAN_H + +#include +#include +#include +#include +#include +#include +#include +#include + +#define KMSAN_ALLOCA_MAGIC_ORIGIN 0xabcd0100 +#define KMSAN_CHAIN_MAGIC_ORIGIN 0xabcd0200 + +#define KMSAN_POISON_NOCHECK 0x0 +#define KMSAN_POISON_CHECK 0x1 +#define KMSAN_POISON_FREE 0x2 + +#define KMSAN_ORIGIN_SIZE 4 + +#define KMSAN_STACK_DEPTH 64 + +#define KMSAN_META_SHADOW (false) +#define KMSAN_META_ORIGIN (true) + +extern bool kmsan_enabled; +extern int panic_on_kmsan; + +/* + * KMSAN performs a lot of consistency checks that are currently enabled by + * default. BUG_ON is normally discouraged in the kernel, unless used for + * debugging, but KMSAN itself is a debugging tool, so it makes little sense to + * recover if something goes wrong. + */ +#define KMSAN_WARN_ON(cond) \ + ({ \ + const bool __cond = WARN_ON(cond); \ + if (unlikely(__cond)) { \ + WRITE_ONCE(kmsan_enabled, false); \ + if (panic_on_kmsan) { \ + /* Can't call panic() here because */ \ + /* of uaccess checks. */ \ + BUG(); \ + } \ + } \ + __cond; \ + }) + +/* + * A pair of metadata pointers to be returned by the instrumentation functions. + */ +struct shadow_origin_ptr { + void *shadow, *origin; +}; + +struct shadow_origin_ptr kmsan_get_shadow_origin_ptr(void *addr, u64 size, + bool store); +void *kmsan_get_metadata(void *addr, bool is_origin); + +enum kmsan_bug_reason { + REASON_ANY, + REASON_COPY_TO_USER, + REASON_SUBMIT_URB, +}; + +void kmsan_print_origin(depot_stack_handle_t origin); + +/** + * kmsan_report() - Report a use of uninitialized value. + * @origin: Stack ID of the uninitialized value. + * @address: Address at which the memory access happens. + * @size: Memory access size. + * @off_first: Offset (from @address) of the first byte to be reported. + * @off_last: Offset (from @address) of the last byte to be reported. + * @user_addr: When non-NULL, denotes the userspace address to which the kernel + * is leaking data. + * @reason: Error type from enum kmsan_bug_reason. + * + * kmsan_report() prints an error message for a consequent group of bytes + * sharing the same origin. If an uninitialized value is used in a comparison, + * this function is called once without specifying the addresses. When checking + * a memory range, KMSAN may call kmsan_report() multiple times with the same + * @address, @size, @user_addr and @reason, but different @off_first and + * @off_last corresponding to different @origin values. + */ +void kmsan_report(depot_stack_handle_t origin, void *address, int size, + int off_first, int off_last, const void *user_addr, + enum kmsan_bug_reason reason); + +DECLARE_PER_CPU(struct kmsan_ctx, kmsan_percpu_ctx); + +static __always_inline struct kmsan_ctx *kmsan_get_context(void) +{ + return in_task() ? ¤t->kmsan_ctx : raw_cpu_ptr(&kmsan_percpu_ctx); +} + +/* + * When a compiler hook or KMSAN runtime function is invoked, it may make a + * call to instrumented code and eventually call itself recursively. To avoid + * that, we guard the runtime entry regions with + * kmsan_enter_runtime()/kmsan_leave_runtime() and exit the hook if + * kmsan_in_runtime() is true. + * + * Non-runtime code may occasionally get executed in nested IRQs from the + * runtime code (e.g. when called via smp_call_function_single()). Because some + * KMSAN routines may take locks (e.g. for memory allocation), we conservatively + * bail out instead of calling them. To minimize the effect of this (potentially + * missing initialization events) kmsan_in_runtime() is not checked in + * non-blocking runtime functions. + */ +static __always_inline bool kmsan_in_runtime(void) +{ + if ((hardirq_count() >> HARDIRQ_SHIFT) > 1) + return true; + return kmsan_get_context()->kmsan_in_runtime; +} + +static __always_inline void kmsan_enter_runtime(void) +{ + struct kmsan_ctx *ctx; + + ctx = kmsan_get_context(); + KMSAN_WARN_ON(ctx->kmsan_in_runtime++); +} + +static __always_inline void kmsan_leave_runtime(void) +{ + struct kmsan_ctx *ctx = kmsan_get_context(); + + KMSAN_WARN_ON(--ctx->kmsan_in_runtime); +} + +depot_stack_handle_t kmsan_save_stack(void); +depot_stack_handle_t kmsan_save_stack_with_flags(gfp_t flags, + unsigned int extra_bits); + +/* + * Pack and unpack the origin chain depth and UAF flag to/from the extra bits + * provided by the stack depot. + * The UAF flag is stored in the lowest bit, followed by the depth in the upper + * bits. + * set_dsh_extra_bits() is responsible for clamping the value. + */ +static __always_inline unsigned int kmsan_extra_bits(unsigned int depth, + bool uaf) +{ + return (depth << 1) | uaf; +} + +static __always_inline bool kmsan_uaf_from_eb(unsigned int extra_bits) +{ + return extra_bits & 1; +} + +static __always_inline unsigned int kmsan_depth_from_eb(unsigned int extra_bits) +{ + return extra_bits >> 1; +} + +/* + * kmsan_internal_ functions are supposed to be very simple and not require the + * kmsan_in_runtime() checks. + */ +void kmsan_internal_memmove_metadata(void *dst, void *src, size_t n); +void kmsan_internal_poison_memory(void *address, size_t size, gfp_t flags, + unsigned int poison_flags); +void kmsan_internal_unpoison_memory(void *address, size_t size, bool checked); +void kmsan_internal_set_shadow_origin(void *address, size_t size, int b, + u32 origin, bool checked); +depot_stack_handle_t kmsan_internal_chain_origin(depot_stack_handle_t id); + +bool kmsan_metadata_is_contiguous(void *addr, size_t size); +void kmsan_internal_check_memory(void *addr, size_t size, const void *user_addr, + int reason); + +struct page *kmsan_vmalloc_to_page_or_null(void *vaddr); + +/* + * kmsan_internal_is_module_addr() and kmsan_internal_is_vmalloc_addr() are + * non-instrumented versions of is_module_address() and is_vmalloc_addr() that + * are safe to call from KMSAN runtime without recursion. + */ +static inline bool kmsan_internal_is_module_addr(void *vaddr) +{ + return ((u64)vaddr >= MODULES_VADDR) && ((u64)vaddr < MODULES_END); +} + +static inline bool kmsan_internal_is_vmalloc_addr(void *addr) +{ + return ((u64)addr >= VMALLOC_START) && ((u64)addr < VMALLOC_END); +} + +#endif /* __MM_KMSAN_KMSAN_H */ diff --git a/mm/kmsan/report.c b/mm/kmsan/report.c new file mode 100644 index 0000000000000..64e061f7da496 --- /dev/null +++ b/mm/kmsan/report.c @@ -0,0 +1,211 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KMSAN error reporting routines. + * + * Copyright (C) 2019-2022 Google LLC + * Author: Alexander Potapenko + * + */ + +#include +#include +#include +#include +#include + +#include "kmsan.h" + +static DEFINE_RAW_SPINLOCK(kmsan_report_lock); +#define DESCR_SIZE 128 +/* Protected by kmsan_report_lock */ +static char report_local_descr[DESCR_SIZE]; +int panic_on_kmsan __read_mostly; + +#ifdef MODULE_PARAM_PREFIX +#undef MODULE_PARAM_PREFIX +#endif +#define MODULE_PARAM_PREFIX "kmsan." +module_param_named(panic, panic_on_kmsan, int, 0); + +/* + * Skip internal KMSAN frames. + */ +static int get_stack_skipnr(const unsigned long stack_entries[], + int num_entries) +{ + int len, skip; + char buf[64]; + + for (skip = 0; skip < num_entries; ++skip) { + len = scnprintf(buf, sizeof(buf), "%ps", + (void *)stack_entries[skip]); + + /* Never show __msan_* or kmsan_* functions. */ + if ((strnstr(buf, "__msan_", len) == buf) || + (strnstr(buf, "kmsan_", len) == buf)) + continue; + + /* + * No match for runtime functions -- @skip entries to skip to + * get to first frame of interest. + */ + break; + } + + return skip; +} + +/* + * Currently the descriptions of locals generated by Clang look as follows: + * ----local_name@function_name + * We want to print only the name of the local, as other information in that + * description can be confusing. + * The meaningful part of the description is copied to a global buffer to avoid + * allocating memory. + */ +static char *pretty_descr(char *descr) +{ + int pos = 0, len = strlen(descr); + + for (int i = 0; i < len; i++) { + if (descr[i] == '@') + break; + if (descr[i] == '-') + continue; + report_local_descr[pos] = descr[i]; + if (pos + 1 == DESCR_SIZE) + break; + pos++; + } + report_local_descr[pos] = 0; + return report_local_descr; +} + +void kmsan_print_origin(depot_stack_handle_t origin) +{ + unsigned long *entries = NULL, *chained_entries = NULL; + unsigned int nr_entries, chained_nr_entries, skipnr; + void *pc1 = NULL, *pc2 = NULL; + depot_stack_handle_t head; + unsigned long magic; + char *descr = NULL; + + if (!origin) + return; + + while (true) { + nr_entries = stack_depot_fetch(origin, &entries); + magic = nr_entries ? entries[0] : 0; + if ((nr_entries == 4) && (magic == KMSAN_ALLOCA_MAGIC_ORIGIN)) { + descr = (char *)entries[1]; + pc1 = (void *)entries[2]; + pc2 = (void *)entries[3]; + pr_err("Local variable %s created at:\n", + pretty_descr(descr)); + if (pc1) + pr_err(" %pSb\n", pc1); + if (pc2) + pr_err(" %pSb\n", pc2); + break; + } + if ((nr_entries == 3) && (magic == KMSAN_CHAIN_MAGIC_ORIGIN)) { + head = entries[1]; + origin = entries[2]; + pr_err("Uninit was stored to memory at:\n"); + chained_nr_entries = + stack_depot_fetch(head, &chained_entries); + kmsan_internal_unpoison_memory( + chained_entries, + chained_nr_entries * sizeof(*chained_entries), + /*checked*/ false); + skipnr = get_stack_skipnr(chained_entries, + chained_nr_entries); + stack_trace_print(chained_entries + skipnr, + chained_nr_entries - skipnr, 0); + pr_err("\n"); + continue; + } + pr_err("Uninit was created at:\n"); + if (nr_entries) { + skipnr = get_stack_skipnr(entries, nr_entries); + stack_trace_print(entries + skipnr, nr_entries - skipnr, + 0); + } else { + pr_err("(stack is not available)\n"); + } + break; + } +} + +void kmsan_report(depot_stack_handle_t origin, void *address, int size, + int off_first, int off_last, const void *user_addr, + enum kmsan_bug_reason reason) +{ + unsigned long stack_entries[KMSAN_STACK_DEPTH]; + int num_stack_entries, skipnr; + char *bug_type = NULL; + unsigned long ua_flags; + bool is_uaf; + + if (!kmsan_enabled) + return; + if (!current->kmsan_ctx.allow_reporting) + return; + if (!origin) + return; + + current->kmsan_ctx.allow_reporting = false; + ua_flags = user_access_save(); + raw_spin_lock(&kmsan_report_lock); + pr_err("=====================================================\n"); + is_uaf = kmsan_uaf_from_eb(stack_depot_get_extra_bits(origin)); + switch (reason) { + case REASON_ANY: + bug_type = is_uaf ? "use-after-free" : "uninit-value"; + break; + case REASON_COPY_TO_USER: + bug_type = is_uaf ? "kernel-infoleak-after-free" : + "kernel-infoleak"; + break; + case REASON_SUBMIT_URB: + bug_type = is_uaf ? "kernel-usb-infoleak-after-free" : + "kernel-usb-infoleak"; + break; + } + + num_stack_entries = + stack_trace_save(stack_entries, KMSAN_STACK_DEPTH, 1); + skipnr = get_stack_skipnr(stack_entries, num_stack_entries); + + pr_err("BUG: KMSAN: %s in %pSb\n", bug_type, + (void *)stack_entries[skipnr]); + stack_trace_print(stack_entries + skipnr, num_stack_entries - skipnr, + 0); + pr_err("\n"); + + kmsan_print_origin(origin); + + if (size) { + pr_err("\n"); + if (off_first == off_last) + pr_err("Byte %d of %d is uninitialized\n", off_first, + size); + else + pr_err("Bytes %d-%d of %d are uninitialized\n", + off_first, off_last, size); + } + if (address) + pr_err("Memory access of size %d starts at %px\n", size, + address); + if (user_addr && reason == REASON_COPY_TO_USER) + pr_err("Data copied to user address %px\n", user_addr); + pr_err("\n"); + dump_stack_print_info(KERN_ERR); + pr_err("=====================================================\n"); + add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); + raw_spin_unlock(&kmsan_report_lock); + if (panic_on_kmsan) + panic("kmsan.panic set ...\n"); + user_access_restore(ua_flags); + current->kmsan_ctx.allow_reporting = true; +} diff --git a/mm/kmsan/shadow.c b/mm/kmsan/shadow.c new file mode 100644 index 0000000000000..acc5279acc3be --- /dev/null +++ b/mm/kmsan/shadow.c @@ -0,0 +1,147 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KMSAN shadow implementation. + * + * Copyright (C) 2017-2022 Google LLC + * Author: Alexander Potapenko + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../internal.h" +#include "kmsan.h" + +#define shadow_page_for(page) ((page)->kmsan_shadow) + +#define origin_page_for(page) ((page)->kmsan_origin) + +static void *shadow_ptr_for(struct page *page) +{ + return page_address(shadow_page_for(page)); +} + +static void *origin_ptr_for(struct page *page) +{ + return page_address(origin_page_for(page)); +} + +static bool page_has_metadata(struct page *page) +{ + return shadow_page_for(page) && origin_page_for(page); +} + +static void set_no_shadow_origin_page(struct page *page) +{ + shadow_page_for(page) = NULL; + origin_page_for(page) = NULL; +} + +/* + * Dummy load and store pages to be used when the real metadata is unavailable. + * There are separate pages for loads and stores, so that every load returns a + * zero, and every store doesn't affect other loads. + */ +static char dummy_load_page[PAGE_SIZE] __aligned(PAGE_SIZE); +static char dummy_store_page[PAGE_SIZE] __aligned(PAGE_SIZE); + +static unsigned long vmalloc_meta(void *addr, bool is_origin) +{ + unsigned long addr64 = (unsigned long)addr, off; + + KMSAN_WARN_ON(is_origin && !IS_ALIGNED(addr64, KMSAN_ORIGIN_SIZE)); + if (kmsan_internal_is_vmalloc_addr(addr)) { + off = addr64 - VMALLOC_START; + return off + (is_origin ? KMSAN_VMALLOC_ORIGIN_START : + KMSAN_VMALLOC_SHADOW_START); + } + if (kmsan_internal_is_module_addr(addr)) { + off = addr64 - MODULES_VADDR; + return off + (is_origin ? KMSAN_MODULES_ORIGIN_START : + KMSAN_MODULES_SHADOW_START); + } + return 0; +} + +static struct page *virt_to_page_or_null(void *vaddr) +{ + if (kmsan_virt_addr_valid(vaddr)) + return virt_to_page(vaddr); + else + return NULL; +} + +struct shadow_origin_ptr kmsan_get_shadow_origin_ptr(void *address, u64 size, + bool store) +{ + struct shadow_origin_ptr ret; + void *shadow; + + /* + * Even if we redirect this memory access to the dummy page, it will + * go out of bounds. + */ + KMSAN_WARN_ON(size > PAGE_SIZE); + + if (!kmsan_enabled) + goto return_dummy; + + KMSAN_WARN_ON(!kmsan_metadata_is_contiguous(address, size)); + shadow = kmsan_get_metadata(address, KMSAN_META_SHADOW); + if (!shadow) + goto return_dummy; + + ret.shadow = shadow; + ret.origin = kmsan_get_metadata(address, KMSAN_META_ORIGIN); + return ret; + +return_dummy: + if (store) { + /* Ignore this store. */ + ret.shadow = dummy_store_page; + ret.origin = dummy_store_page; + } else { + /* This load will return zero. */ + ret.shadow = dummy_load_page; + ret.origin = dummy_load_page; + } + return ret; +} + +/* + * Obtain the shadow or origin pointer for the given address, or NULL if there's + * none. The caller must check the return value for being non-NULL if needed. + * The return value of this function should not depend on whether we're in the + * runtime or not. + */ +void *kmsan_get_metadata(void *address, bool is_origin) +{ + u64 addr = (u64)address, pad, off; + struct page *page; + + if (is_origin && !IS_ALIGNED(addr, KMSAN_ORIGIN_SIZE)) { + pad = addr % KMSAN_ORIGIN_SIZE; + addr -= pad; + } + address = (void *)addr; + if (kmsan_internal_is_vmalloc_addr(address) || + kmsan_internal_is_module_addr(address)) + return (void *)vmalloc_meta(address, is_origin); + + page = virt_to_page_or_null(address); + if (!page) + return NULL; + if (!page_has_metadata(page)) + return NULL; + off = addr % PAGE_SIZE; + + return (is_origin ? origin_ptr_for(page) : shadow_ptr_for(page)) + off; +} diff --git a/scripts/Makefile.kmsan b/scripts/Makefile.kmsan new file mode 100644 index 0000000000000..b5b0aa61322ec --- /dev/null +++ b/scripts/Makefile.kmsan @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0 +kmsan-cflags := -fsanitize=kernel-memory + +ifdef CONFIG_KMSAN_CHECK_PARAM_RETVAL +kmsan-cflags += -fsanitize-memory-param-retval +endif + +export CFLAGS_KMSAN := $(kmsan-cflags) diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib index 3fb6a99e78c47..ac32429e93b73 100644 --- a/scripts/Makefile.lib +++ b/scripts/Makefile.lib @@ -157,6 +157,15 @@ _c_flags += $(if $(patsubst n%,, \ endif endif +ifeq ($(CONFIG_KMSAN),y) +_c_flags += $(if $(patsubst n%,, \ + $(KMSAN_SANITIZE_$(basetarget).o)$(KMSAN_SANITIZE)y), \ + $(CFLAGS_KMSAN)) +_c_flags += $(if $(patsubst n%,, \ + $(KMSAN_ENABLE_CHECKS_$(basetarget).o)$(KMSAN_ENABLE_CHECKS)y), \ + , -mllvm -msan-disable-checks=1) +endif + ifeq ($(CONFIG_UBSAN),y) _c_flags += $(if $(patsubst n%,, \ $(UBSAN_SANITIZE_$(basetarget).o)$(UBSAN_SANITIZE)$(CONFIG_UBSAN_SANITIZE_ALL)), \ From patchwork Fri Aug 26 15:07:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956185 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 32AACECAAD5 for ; Fri, 26 Aug 2022 15:08:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C5790940013; Fri, 26 Aug 2022 11:08:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B19A1940007; Fri, 26 Aug 2022 11:08:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8F658940013; Fri, 26 Aug 2022 11:08:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 7CC3B940007 for ; Fri, 26 Aug 2022 11:08:47 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 5709C120F4C for ; Fri, 26 Aug 2022 15:08:47 +0000 (UTC) X-FDA: 79842075894.19.2201319 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf12.hostedemail.com (Postfix) with ESMTP id EBCE640026 for ; Fri, 26 Aug 2022 15:08:46 +0000 (UTC) Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-33da75a471cso29359517b3.20 for ; Fri, 26 Aug 2022 08:08:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=mLlFYFB/rgc9qqJnOoWYqSqEWYlWfzhxjk78c+cLuAA=; b=XiFqi+HvFYWVKfCkxZSEvaLp4dj6PJCrtRX+y89NfMTPZ6+goUHOrJlaygaqkuIGBr cdc5TBzN75bo5l86pnTnW79iKtt96Ou0SDdJM2YIL9g3uoi3Job0DZK7q/nIm6l/tWpV UNDqvTKx+/6Oadm/ZjmMPEuU3raZ5GAff/rF0sMmJtdeCbxKEsKayU6Bd3s50LCPKUcZ oMZ+RIGrr0zlDQke70b2ZaiBxuU5QGWCP+5v5NfPFB1kpt3JEHHNKAiGV5LkFVztHN6G +Uh7pGeyKXy0VtS5AtzRj9P6HtYc37b8quRD9x2XVb1xZapp20z/LcMX0wNNYJelokgN IPMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=mLlFYFB/rgc9qqJnOoWYqSqEWYlWfzhxjk78c+cLuAA=; b=O56yYwd9nlK398AtVUJtdx0FY4ypb9JvAk2NXk60rvq/p9OTd3/BAL1H+7EX6+B778 S8oQMQLuL7jP8ose7mhZWUi2Ynm+3XO1FY1giRbhntWtLa/PEmaz3APNl62LRmY50TO5 BauTId0o8JRxtg1Rcr/Te9vCHFc/Ngm12cMMv1Mrw3CFBWdMpLz3XHhMJi3vw/AQz7P9 vU00NoGnJgZAJlJo59kygU39oCmsvVfkcFWOYI+H079MYyA8vGimRXwqtfd8jfuZUbQM ZjVL40v1Le/bodlOiUxtmRlVocD52tpv9dUnRYPeVYD8QwfpUTK4vlyCb/b1h178BtUc w+xg== X-Gm-Message-State: ACgBeo0wS//Obs9z5lMt0e6bIpHw8tdU+DgVGHVN1cjoEZVi94x3636x Stf1dTwlTCIKg+fHSIY//Bd+nMLsqbs= X-Google-Smtp-Source: AA6agR57RONmOxoyPLmSyjKKyC9XY3IjUGI1gX8npn/cM8EWP8iDs6QDrjkGDieykAPqCJaWURQiX5dgMQY= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a0d:f144:0:b0:33d:a554:b9b6 with SMTP id a65-20020a0df144000000b0033da554b9b6mr135036ywf.172.1661526526267; Fri, 26 Aug 2022 08:08:46 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:35 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-13-glider@google.com> Subject: [PATCH v5 12/44] kmsan: disable instrumentation of unsupported common kernel code From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=XiFqi+Hv; spf=pass (imf12.hostedemail.com: domain of 3_uEIYwYKCAUlqnijwlttlqj.htrqnsz2-rrp0fhp.twl@flex--glider.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3_uEIYwYKCAUlqnijwlttlqj.htrqnsz2-rrp0fhp.twl@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526527; a=rsa-sha256; cv=none; b=P66/48AWYq2psk1oR1tzmdtjEg96t2BxgqPxUoosljXFEzdxdWaQrou+vLoi0jq4DoZRDs q2jD9J4XBTsS8k4J9pJM/tqAbJjCGik9odARgAUBiDcXfK42iVt+OxzQ+haGW6OynBVQP9 YKJNFtEr/jJaFduDobyutlVeOvfU5S4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526527; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=mLlFYFB/rgc9qqJnOoWYqSqEWYlWfzhxjk78c+cLuAA=; b=z74aKcS72RweFJPmnYl0Y3e+LHArywynujL80Y2iKl3GgEi4X6Q078sl0mBKkua5BXhDtl UGvd+k5PO11HWHIDdiZe8Pqk+1+IqsYNFERjVGwRH7DMis57JH1lWjrr10utmVOO628ANJ 5iFDZLK7+lcMHrNIfxtxZeEUFgaPtKY= X-Rspam-User: X-Stat-Signature: qcmeftdz1iybughpo9cdbjj8qmso1isp X-Rspamd-Queue-Id: EBCE640026 Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=XiFqi+Hv; spf=pass (imf12.hostedemail.com: domain of 3_uEIYwYKCAUlqnijwlttlqj.htrqnsz2-rrp0fhp.twl@flex--glider.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3_uEIYwYKCAUlqnijwlttlqj.htrqnsz2-rrp0fhp.twl@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam09 X-HE-Tag: 1661526526-878528 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: EFI stub cannot be linked with KMSAN runtime, so we disable instrumentation for it. Instrumenting kcov, stackdepot or lockdep leads to infinite recursion caused by instrumentation hooks calling instrumented code again. Signed-off-by: Alexander Potapenko Reviewed-by: Marco Elver --- v4: -- This patch was previously part of "kmsan: disable KMSAN instrumentation for certain kernel parts", but was split away per Mark Rutland's request. v5: -- remove unnecessary comment belonging to another patch Link: https://linux-review.googlesource.com/id/I41ae706bd3474f074f6a870bfc3f0f90e9c720f7 --- drivers/firmware/efi/libstub/Makefile | 1 + kernel/Makefile | 1 + kernel/locking/Makefile | 3 ++- lib/Makefile | 3 +++ 4 files changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/libstub/Makefile index d0537573501e9..81432d0c904b1 100644 --- a/drivers/firmware/efi/libstub/Makefile +++ b/drivers/firmware/efi/libstub/Makefile @@ -46,6 +46,7 @@ GCOV_PROFILE := n # Sanitizer runtimes are unavailable and cannot be linked here. KASAN_SANITIZE := n KCSAN_SANITIZE := n +KMSAN_SANITIZE := n UBSAN_SANITIZE := n OBJECT_FILES_NON_STANDARD := y diff --git a/kernel/Makefile b/kernel/Makefile index 318789c728d32..d754e0be1176d 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -38,6 +38,7 @@ KCOV_INSTRUMENT_kcov.o := n KASAN_SANITIZE_kcov.o := n KCSAN_SANITIZE_kcov.o := n UBSAN_SANITIZE_kcov.o := n +KMSAN_SANITIZE_kcov.o := n CFLAGS_kcov.o := $(call cc-option, -fno-conserve-stack) -fno-stack-protector # Don't instrument error handlers diff --git a/kernel/locking/Makefile b/kernel/locking/Makefile index d51cabf28f382..ea925731fa40f 100644 --- a/kernel/locking/Makefile +++ b/kernel/locking/Makefile @@ -5,8 +5,9 @@ KCOV_INSTRUMENT := n obj-y += mutex.o semaphore.o rwsem.o percpu-rwsem.o -# Avoid recursion lockdep -> KCSAN -> ... -> lockdep. +# Avoid recursion lockdep -> sanitizer -> ... -> lockdep. KCSAN_SANITIZE_lockdep.o := n +KMSAN_SANITIZE_lockdep.o := n ifdef CONFIG_FUNCTION_TRACER CFLAGS_REMOVE_lockdep.o = $(CC_FLAGS_FTRACE) diff --git a/lib/Makefile b/lib/Makefile index 5927d7fa08063..22c064b61b3be 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -275,6 +275,9 @@ obj-$(CONFIG_POLYNOMIAL) += polynomial.o CFLAGS_stackdepot.o += -fno-builtin obj-$(CONFIG_STACKDEPOT) += stackdepot.o KASAN_SANITIZE_stackdepot.o := n +# In particular, instrumenting stackdepot.c with KMSAN will result in infinite +# recursion. +KMSAN_SANITIZE_stackdepot.o := n KCOV_INSTRUMENT_stackdepot.o := n obj-$(CONFIG_REF_TRACKER) += ref_tracker.o From patchwork Fri Aug 26 15:07:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956186 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 63D0EECAAA3 for ; Fri, 26 Aug 2022 15:08:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F3BC6940014; Fri, 26 Aug 2022 11:08:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EEBE5940007; Fri, 26 Aug 2022 11:08:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D3CC4940014; Fri, 26 Aug 2022 11:08:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id C2C0F940007 for ; Fri, 26 Aug 2022 11:08:50 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 963061A07BA for ; Fri, 26 Aug 2022 15:08:50 +0000 (UTC) X-FDA: 79842076020.11.0D06503 Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf16.hostedemail.com (Postfix) with ESMTP id 4C4E518001C for ; Fri, 26 Aug 2022 15:08:50 +0000 (UTC) Received: by mail-ed1-f73.google.com with SMTP id i6-20020a05640242c600b00447c00a776aso1248647edc.20 for ; Fri, 26 Aug 2022 08:08:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=PmohPhKomZXd1zOAWK/EQjp0UhZ3tSbnWnyq8ud/fAU=; b=YJO4Xg0+O4fDaxcbYp1umz/9KOZq6MT64iPBuvb1Eyk0CRlXy5BoZxooWMgHq81Mww aAkaFVvfJmx8okxcpaJTFDvGl68dPKJ+48I8mfAqwXsEanHocOAaCh/MGEgmAYwBPoyU B98RoAIMKx99hewKWZNnFsrHAzTgDEPPakAw+ENqyBC9iSPWmrVjYUMFS5UztA5Jrts/ NAmFaPfKOWhmTiRrueAff3+7UOYpVyTReHij2nZk2PTf9O6wfSScGw/khRnRflK4HTor aK26uUCVllQrwo/IEachNt1mk39lrVOLqFljJSHkCaPXcdzGpzQO93EM3hejm5qh5wEj xnmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=PmohPhKomZXd1zOAWK/EQjp0UhZ3tSbnWnyq8ud/fAU=; b=khrm2geX8ZYHOcc3Md9YGXwEb3j18KXQda8JurNptl2nJULQh1zWF4Oy6PFg6HGc51 jLV2Uh2hbA1kNN0XD+Xwtrwh51wUAlGEl53Wc+jmFM1HNT19xrmIQ/R8egE0OUu5ms9v 85wj01VCnyloNSpneWH0JsWqKes5SiB8xNjDFsgsj4CRbisMjpFremx1NCEp0o0ovTe5 AmyCd3WNCVSQcEIniTfiwmXFBQ5kRwkzwwrcY5nNQciQatzikLdFpfUDmWsmJ80eaEw4 C6qiE1Xeis/oi05TwDRoBKWSbTGn6EW6gLgLO/bkwrsA6oLI2SQcHLD0acImmaqieFYv xGVw== X-Gm-Message-State: ACgBeo20qlg72gOT7jDkZdqlUhjE/0GQHBLUucbHj6WfwETTpK0UnAtb 69/TYZtNJxiIbfCr/Zd4ZW1v38h6Oh0= X-Google-Smtp-Source: AA6agR7e2paC/ZuzvRhAHjB4767/oTAx7KXCr2XuebS6jhDF8RYl+Uz9hljfZfbpg8cNZSTTP2UQ/IVJ12o= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a17:907:2cd0:b0:73d:d80c:b51f with SMTP id hg16-20020a1709072cd000b0073dd80cb51fmr4282876ejc.619.1661526528970; Fri, 26 Aug 2022 08:08:48 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:36 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-14-glider@google.com> Subject: [PATCH v5 13/44] MAINTAINERS: add entry for KMSAN From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526530; a=rsa-sha256; cv=none; b=rEd0DRCZXb9NULOhJ0pJ4kn3orqlyLmy1zwWC+qdL4pV3soskHxTE6wHfwItR+/vSay8u9 6/WQevGAkTd+G9zCCdwc8epdZV7vB2t45Vfj2KHl/H10IYeSVfr8eDC8sJm4aIcdQJnNJA jiJDG29GHjTtvQNG5J3zAafEr77tiyk= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=YJO4Xg0+; spf=pass (imf16.hostedemail.com: domain of 3AOIIYwYKCAcnspklynvvnsl.jvtspu14-ttr2hjr.vyn@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3AOIIYwYKCAcnspklynvvnsl.jvtspu14-ttr2hjr.vyn@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526530; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=PmohPhKomZXd1zOAWK/EQjp0UhZ3tSbnWnyq8ud/fAU=; b=kARobxlPOUI/DEB/UegUYBiaiIxd1JyZWCx5M+rHxeEApvq22WMjO8vqbEndeHVbjotwDT WCGvJsY2jBMCJPMxsiPvclXlyjBrzqg2/Z85zBpze9ixenwTomC54CjKnoKOzJLo0cG88i tbd9yFIGzLiZc0Kolh7e8oKg7fNtabE= X-Rspam-User: Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=YJO4Xg0+; spf=pass (imf16.hostedemail.com: domain of 3AOIIYwYKCAcnspklynvvnsl.jvtspu14-ttr2hjr.vyn@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3AOIIYwYKCAcnspklynvvnsl.jvtspu14-ttr2hjr.vyn@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam08 X-Stat-Signature: rcdngp5m5womgq3fi6exxc4578gp7i3p X-Rspamd-Queue-Id: 4C4E518001C X-HE-Tag: 1661526530-361159 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: Add entry for KMSAN maintainers/reviewers. Signed-off-by: Alexander Potapenko --- v5: -- add arch/*/include/asm/kmsan.h Link: https://linux-review.googlesource.com/id/Ic5836c2bceb6b63f71a60d3327d18af3aa3dab77 --- MAINTAINERS | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 9d7f64dc0efe8..3bae9c4c2b73d 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -11369,6 +11369,19 @@ F: kernel/kmod.c F: lib/test_kmod.c F: tools/testing/selftests/kmod/ +KMSAN +M: Alexander Potapenko +R: Marco Elver +R: Dmitry Vyukov +L: kasan-dev@googlegroups.com +S: Maintained +F: Documentation/dev-tools/kmsan.rst +F: arch/*/include/asm/kmsan.h +F: include/linux/kmsan*.h +F: lib/Kconfig.kmsan +F: mm/kmsan/ +F: scripts/Makefile.kmsan + KPROBES M: Naveen N. Rao M: Anil S Keshavamurthy From patchwork Fri Aug 26 15:07:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956187 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 79317ECAAD5 for ; Fri, 26 Aug 2022 15:08:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1574E940015; Fri, 26 Aug 2022 11:08:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0E0BA940007; Fri, 26 Aug 2022 11:08:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E26AD940015; Fri, 26 Aug 2022 11:08:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id CCAF9940007 for ; Fri, 26 Aug 2022 11:08:53 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 9D0F91A0F67 for ; Fri, 26 Aug 2022 15:08:53 +0000 (UTC) X-FDA: 79842076146.05.B4151C7 Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) by imf04.hostedemail.com (Postfix) with ESMTP id 4C9814001C for ; Fri, 26 Aug 2022 15:08:53 +0000 (UTC) Received: by mail-ej1-f74.google.com with SMTP id qb39-20020a1709077ea700b0073ddc845586so718988ejc.2 for ; Fri, 26 Aug 2022 08:08:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=RCEJUrdGaZnDzkCVeq8BsOQ2VOxKVJNx7oe/faTwpqA=; b=DoBVgTeGHV+u4ohZMjhohR9m7JZpg5bj+EOWAguhfJxw2njpO0bZUyxV8bU0tZn0Ra g4GMhUWzdfTwnZoVAfqv/IdaiB7JqDdJRTOE0WCN13NA9+T3eSW1XFXvQP71IHqd2jau fDN/vaIVtdpziHVBYi42qzwTu4xZLCKsOWysr/uioRDhab3l/hrj2S/vWl5g1PHoNlq/ x4jy0FkjbzwiKmzmpAvufx4skS2+F9vqUJjiXlYirlGW6/nMQAxovyBg53kdNZamtimt YzDX3SBfPazo7/1hJANLwD98EfVSr0TuJWea7uopoguN5rNEeopAcAF0FN5+ztQ7PxGg 1KDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=RCEJUrdGaZnDzkCVeq8BsOQ2VOxKVJNx7oe/faTwpqA=; b=Bx7KDeeW9DObBVjgoLyo2UmkdUyeKnDn90QmgnGoFHcAJMpoBFRXfhshO75hXq9RBd jMInAWWYtmLYlsC1fjm9VVSQtCh1AhIm4lXVxpZrQ/SOT53Jip5w6LB6KYLbtj08VsxF qJ4ZBYW/dxMl8yGF/YE3+PYY8Bsw7xJCrr+sopV+7aO93maYeDAEbTywrbeeMgWdpA+N maufR2rFmkEpS793oikoau1xiQ2l+xGIPIATma00q1ZEApsIIMpns3EDIir0oOLvvg2K QoBCLT9meD7XuqCoJbSfpjvBEafmr3MQvlv8P/sc8eTmJvBzi+9RFcqrRb1R4R+oz1eA ChkA== X-Gm-Message-State: ACgBeo1NGaAunFEjx7el76De9NSTm/aQ10BD47xaFfZCOow7gvLNBCex KiBpWC3rXAB2uP0c50p+q62TwsimZTg= X-Google-Smtp-Source: AA6agR5TJ2215a/FX4xfXZffmhokPd+WXLuXNU73CeC2DKzV0N0DB4LEJ7M+hq0lpB1gpBiSGrqZAF8M6ss= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a17:906:cc15:b0:73d:d87f:f039 with SMTP id ml21-20020a170906cc1500b0073dd87ff039mr4286773ejb.253.1661526532079; Fri, 26 Aug 2022 08:08:52 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:37 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-15-glider@google.com> Subject: [PATCH v5 14/44] mm: kmsan: maintain KMSAN metadata for page operations From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526533; a=rsa-sha256; cv=none; b=Rz1fDY+nc9pRkK7hTo31iXjjnmpvK4oyckSxPra5X6wux6yG+1pwx/VFaGlZ3iZAFqyO9n sK7JdfXROumpSsb9giXMYrmLj72N9e4SoD1DonddYM/3MKwx0+dX1oHn1OCtSmAIfC5tzb /P0MvnJPsrmNCUfty3Tw8X6Jp7Tl75A= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=DoBVgTeG; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf04.hostedemail.com: domain of 3BOIIYwYKCAsrwtop2rzzrwp.nzxwty58-xxv6lnv.z2r@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3BOIIYwYKCAsrwtop2rzzrwp.nzxwty58-xxv6lnv.z2r@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526533; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=RCEJUrdGaZnDzkCVeq8BsOQ2VOxKVJNx7oe/faTwpqA=; b=uc7MjMFjTUozK7EsZ4yC4DEP2UP7VyUMfbVlVW7qBTf4nnq2uP6N175TCG0WO6aGLF56wW 9MtcY0VdeqBv/H84KtdyuKkQ0HGImh79e8YVxqByrLcqHqCgn9Vy34WtM20T0cVTPJvJ4b 9GuoU2yQcrwe8uecwVwsqAfWSKUDOMc= X-Rspamd-Queue-Id: 4C9814001C Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=DoBVgTeG; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf04.hostedemail.com: domain of 3BOIIYwYKCAsrwtop2rzzrwp.nzxwty58-xxv6lnv.z2r@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3BOIIYwYKCAsrwtop2rzzrwp.nzxwty58-xxv6lnv.z2r@flex--glider.bounces.google.com X-Rspamd-Server: rspam12 X-Rspam-User: X-Stat-Signature: ty5wtn6huuxc45qt3k9ci1ccaddrcbod X-HE-Tag: 1661526533-199119 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: Insert KMSAN hooks that make the necessary bookkeeping changes: - poison page shadow and origins in alloc_pages()/free_page(); - clear page shadow and origins in clear_page(), copy_user_highpage(); - copy page metadata in copy_highpage(), wp_page_copy(); - handle vmap()/vunmap()/iounmap(); Signed-off-by: Alexander Potapenko --- v2: -- move page metadata hooks implementation here -- remove call to kmsan_memblock_free_pages() v3: -- use PAGE_SHIFT in kmsan_ioremap_page_range() v4: -- change sizeof(type) to sizeof(*ptr) -- replace occurrences of |var| with @var -- swap mm: and kmsan: in the subject -- drop __no_sanitize_memory from clear_page() v5: -- do not export KMSAN hooks that are not called from modules -- use modern style for-loops -- simplify clear_page() instrumentation as suggested by Marco Elver -- move forward declaration of `struct page` in kmsan.h to this patch Link: https://linux-review.googlesource.com/id/I6d4f53a0e7eab46fa29f0348f3095d9f2e326850 --- arch/x86/include/asm/page_64.h | 7 ++ arch/x86/mm/ioremap.c | 3 + include/linux/highmem.h | 3 + include/linux/kmsan.h | 125 +++++++++++++++++++++++++++++++++ mm/internal.h | 6 ++ mm/kmsan/hooks.c | 85 ++++++++++++++++++++++ mm/kmsan/shadow.c | 113 +++++++++++++++++++++++++++++ mm/memory.c | 2 + mm/page_alloc.c | 11 +++ mm/vmalloc.c | 20 +++++- 10 files changed, 373 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/page_64.h b/arch/x86/include/asm/page_64.h index baa70451b8df5..198e03e59ca19 100644 --- a/arch/x86/include/asm/page_64.h +++ b/arch/x86/include/asm/page_64.h @@ -8,6 +8,8 @@ #include #include +#include + /* duplicated to the one in bootmem.h */ extern unsigned long max_pfn; extern unsigned long phys_base; @@ -47,6 +49,11 @@ void clear_page_erms(void *page); static inline void clear_page(void *page) { + /* + * Clean up KMSAN metadata for the page being cleared. The assembly call + * below clobbers @page, so we perform unpoisoning before it. + */ + kmsan_unpoison_memory(page, PAGE_SIZE); alternative_call_2(clear_page_orig, clear_page_rep, X86_FEATURE_REP_GOOD, clear_page_erms, X86_FEATURE_ERMS, diff --git a/arch/x86/mm/ioremap.c b/arch/x86/mm/ioremap.c index 1ad0228f8ceb9..78c5bc654cff5 100644 --- a/arch/x86/mm/ioremap.c +++ b/arch/x86/mm/ioremap.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -479,6 +480,8 @@ void iounmap(volatile void __iomem *addr) return; } + kmsan_iounmap_page_range((unsigned long)addr, + (unsigned long)addr + get_vm_area_size(p)); memtype_free(p->phys_addr, p->phys_addr + get_vm_area_size(p)); /* Finally remove it */ diff --git a/include/linux/highmem.h b/include/linux/highmem.h index 25679035ca283..e9912da5441b4 100644 --- a/include/linux/highmem.h +++ b/include/linux/highmem.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -311,6 +312,7 @@ static inline void copy_user_highpage(struct page *to, struct page *from, vfrom = kmap_local_page(from); vto = kmap_local_page(to); copy_user_page(vto, vfrom, vaddr, to); + kmsan_unpoison_memory(page_address(to), PAGE_SIZE); kunmap_local(vto); kunmap_local(vfrom); } @@ -326,6 +328,7 @@ static inline void copy_highpage(struct page *to, struct page *from) vfrom = kmap_local_page(from); vto = kmap_local_page(to); copy_page(vto, vfrom); + kmsan_copy_page_meta(to, from); kunmap_local(vto); kunmap_local(vfrom); } diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h index e578c9627b641..699fe4f5b3bee 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -14,6 +14,8 @@ #include #include +struct page; + #ifdef CONFIG_KMSAN /* These constants are defined in the MSan LLVM instrumentation pass. */ @@ -39,6 +41,129 @@ struct kmsan_ctx { bool allow_reporting; }; +/** + * kmsan_alloc_page() - Notify KMSAN about an alloc_pages() call. + * @page: struct page pointer returned by alloc_pages(). + * @order: order of allocated struct page. + * @flags: GFP flags used by alloc_pages() + * + * KMSAN marks 1<<@order pages starting at @page as uninitialized, unless + * @flags contain __GFP_ZERO. + */ +void kmsan_alloc_page(struct page *page, unsigned int order, gfp_t flags); + +/** + * kmsan_free_page() - Notify KMSAN about a free_pages() call. + * @page: struct page pointer passed to free_pages(). + * @order: order of deallocated struct page. + * + * KMSAN marks freed memory as uninitialized. + */ +void kmsan_free_page(struct page *page, unsigned int order); + +/** + * kmsan_copy_page_meta() - Copy KMSAN metadata between two pages. + * @dst: destination page. + * @src: source page. + * + * KMSAN copies the contents of metadata pages for @src into the metadata pages + * for @dst. If @dst has no associated metadata pages, nothing happens. + * If @src has no associated metadata pages, @dst metadata pages are unpoisoned. + */ +void kmsan_copy_page_meta(struct page *dst, struct page *src); + +/** + * kmsan_map_kernel_range_noflush() - Notify KMSAN about a vmap. + * @start: start of vmapped range. + * @end: end of vmapped range. + * @prot: page protection flags used for vmap. + * @pages: array of pages. + * @page_shift: page_shift passed to vmap_range_noflush(). + * + * KMSAN maps shadow and origin pages of @pages into contiguous ranges in + * vmalloc metadata address range. + */ +void kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end, + pgprot_t prot, struct page **pages, + unsigned int page_shift); + +/** + * kmsan_vunmap_kernel_range_noflush() - Notify KMSAN about a vunmap. + * @start: start of vunmapped range. + * @end: end of vunmapped range. + * + * KMSAN unmaps the contiguous metadata ranges created by + * kmsan_map_kernel_range_noflush(). + */ +void kmsan_vunmap_range_noflush(unsigned long start, unsigned long end); + +/** + * kmsan_ioremap_page_range() - Notify KMSAN about a ioremap_page_range() call. + * @addr: range start. + * @end: range end. + * @phys_addr: physical range start. + * @prot: page protection flags used for ioremap_page_range(). + * @page_shift: page_shift argument passed to vmap_range_noflush(). + * + * KMSAN creates new metadata pages for the physical pages mapped into the + * virtual memory. + */ +void kmsan_ioremap_page_range(unsigned long addr, unsigned long end, + phys_addr_t phys_addr, pgprot_t prot, + unsigned int page_shift); + +/** + * kmsan_iounmap_page_range() - Notify KMSAN about a iounmap_page_range() call. + * @start: range start. + * @end: range end. + * + * KMSAN unmaps the metadata pages for the given range and, unlike for + * vunmap_page_range(), also deallocates them. + */ +void kmsan_iounmap_page_range(unsigned long start, unsigned long end); + +#else + +static inline int kmsan_alloc_page(struct page *page, unsigned int order, + gfp_t flags) +{ + return 0; +} + +static inline void kmsan_free_page(struct page *page, unsigned int order) +{ +} + +static inline void kmsan_copy_page_meta(struct page *dst, struct page *src) +{ +} + +static inline void kmsan_vmap_pages_range_noflush(unsigned long start, + unsigned long end, + pgprot_t prot, + struct page **pages, + unsigned int page_shift) +{ +} + +static inline void kmsan_vunmap_range_noflush(unsigned long start, + unsigned long end) +{ +} + +static inline void kmsan_ioremap_page_range(unsigned long start, + unsigned long end, + phys_addr_t phys_addr, + pgprot_t prot, + unsigned int page_shift) +{ +} + +static inline void kmsan_iounmap_page_range(unsigned long start, + unsigned long end) +{ +} + #endif #endif /* _LINUX_KMSAN_H */ diff --git a/mm/internal.h b/mm/internal.h index 785409805ed79..fd7247a2367ed 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -847,8 +847,14 @@ int vmap_pages_range_noflush(unsigned long addr, unsigned long end, } #endif +int __vmap_pages_range_noflush(unsigned long addr, unsigned long end, + pgprot_t prot, struct page **pages, + unsigned int page_shift); + void vunmap_range_noflush(unsigned long start, unsigned long end); +void __vunmap_range_noflush(unsigned long start, unsigned long end); + int numa_migrate_prep(struct page *page, struct vm_area_struct *vma, unsigned long addr, int page_nid, int *flags); diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c index 4ac62fa67a02a..2624b4ea3d8ef 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -26,6 +26,91 @@ * skipping effects of functions like memset() inside instrumented code. */ +static unsigned long vmalloc_shadow(unsigned long addr) +{ + return (unsigned long)kmsan_get_metadata((void *)addr, + KMSAN_META_SHADOW); +} + +static unsigned long vmalloc_origin(unsigned long addr) +{ + return (unsigned long)kmsan_get_metadata((void *)addr, + KMSAN_META_ORIGIN); +} + +void kmsan_vunmap_range_noflush(unsigned long start, unsigned long end) +{ + __vunmap_range_noflush(vmalloc_shadow(start), vmalloc_shadow(end)); + __vunmap_range_noflush(vmalloc_origin(start), vmalloc_origin(end)); + flush_cache_vmap(vmalloc_shadow(start), vmalloc_shadow(end)); + flush_cache_vmap(vmalloc_origin(start), vmalloc_origin(end)); +} + +/* + * This function creates new shadow/origin pages for the physical pages mapped + * into the virtual memory. If those physical pages already had shadow/origin, + * those are ignored. + */ +void kmsan_ioremap_page_range(unsigned long start, unsigned long end, + phys_addr_t phys_addr, pgprot_t prot, + unsigned int page_shift) +{ + gfp_t gfp_mask = GFP_KERNEL | __GFP_ZERO; + struct page *shadow, *origin; + unsigned long off = 0; + int nr; + + if (!kmsan_enabled || kmsan_in_runtime()) + return; + + nr = (end - start) / PAGE_SIZE; + kmsan_enter_runtime(); + for (int i = 0; i < nr; i++, off += PAGE_SIZE) { + shadow = alloc_pages(gfp_mask, 1); + origin = alloc_pages(gfp_mask, 1); + __vmap_pages_range_noflush( + vmalloc_shadow(start + off), + vmalloc_shadow(start + off + PAGE_SIZE), prot, &shadow, + PAGE_SHIFT); + __vmap_pages_range_noflush( + vmalloc_origin(start + off), + vmalloc_origin(start + off + PAGE_SIZE), prot, &origin, + PAGE_SHIFT); + } + flush_cache_vmap(vmalloc_shadow(start), vmalloc_shadow(end)); + flush_cache_vmap(vmalloc_origin(start), vmalloc_origin(end)); + kmsan_leave_runtime(); +} + +void kmsan_iounmap_page_range(unsigned long start, unsigned long end) +{ + unsigned long v_shadow, v_origin; + struct page *shadow, *origin; + int nr; + + if (!kmsan_enabled || kmsan_in_runtime()) + return; + + nr = (end - start) / PAGE_SIZE; + kmsan_enter_runtime(); + v_shadow = (unsigned long)vmalloc_shadow(start); + v_origin = (unsigned long)vmalloc_origin(start); + for (int i = 0; i < nr; + i++, v_shadow += PAGE_SIZE, v_origin += PAGE_SIZE) { + shadow = kmsan_vmalloc_to_page_or_null((void *)v_shadow); + origin = kmsan_vmalloc_to_page_or_null((void *)v_origin); + __vunmap_range_noflush(v_shadow, vmalloc_shadow(end)); + __vunmap_range_noflush(v_origin, vmalloc_origin(end)); + if (shadow) + __free_pages(shadow, 1); + if (origin) + __free_pages(origin, 1); + } + flush_cache_vmap(vmalloc_shadow(start), vmalloc_shadow(end)); + flush_cache_vmap(vmalloc_origin(start), vmalloc_origin(end)); + kmsan_leave_runtime(); +} + /* Functions from kmsan-checks.h follow. */ void kmsan_poison_memory(const void *address, size_t size, gfp_t flags) { diff --git a/mm/kmsan/shadow.c b/mm/kmsan/shadow.c index acc5279acc3be..8c81a059beea6 100644 --- a/mm/kmsan/shadow.c +++ b/mm/kmsan/shadow.c @@ -145,3 +145,116 @@ void *kmsan_get_metadata(void *address, bool is_origin) return (is_origin ? origin_ptr_for(page) : shadow_ptr_for(page)) + off; } + +void kmsan_copy_page_meta(struct page *dst, struct page *src) +{ + if (!kmsan_enabled || kmsan_in_runtime()) + return; + if (!dst || !page_has_metadata(dst)) + return; + if (!src || !page_has_metadata(src)) { + kmsan_internal_unpoison_memory(page_address(dst), PAGE_SIZE, + /*checked*/ false); + return; + } + + kmsan_enter_runtime(); + __memcpy(shadow_ptr_for(dst), shadow_ptr_for(src), PAGE_SIZE); + __memcpy(origin_ptr_for(dst), origin_ptr_for(src), PAGE_SIZE); + kmsan_leave_runtime(); +} + +void kmsan_alloc_page(struct page *page, unsigned int order, gfp_t flags) +{ + bool initialized = (flags & __GFP_ZERO) || !kmsan_enabled; + struct page *shadow, *origin; + depot_stack_handle_t handle; + int pages = 1 << order; + + if (!page) + return; + + shadow = shadow_page_for(page); + origin = origin_page_for(page); + + if (initialized) { + __memset(page_address(shadow), 0, PAGE_SIZE * pages); + __memset(page_address(origin), 0, PAGE_SIZE * pages); + return; + } + + /* Zero pages allocated by the runtime should also be initialized. */ + if (kmsan_in_runtime()) + return; + + __memset(page_address(shadow), -1, PAGE_SIZE * pages); + kmsan_enter_runtime(); + handle = kmsan_save_stack_with_flags(flags, /*extra_bits*/ 0); + kmsan_leave_runtime(); + /* + * Addresses are page-aligned, pages are contiguous, so it's ok + * to just fill the origin pages with @handle. + */ + for (int i = 0; i < PAGE_SIZE * pages / sizeof(handle); i++) + ((depot_stack_handle_t *)page_address(origin))[i] = handle; +} + +void kmsan_free_page(struct page *page, unsigned int order) +{ + if (!kmsan_enabled || kmsan_in_runtime()) + return; + kmsan_enter_runtime(); + kmsan_internal_poison_memory(page_address(page), + PAGE_SIZE << compound_order(page), + GFP_KERNEL, + KMSAN_POISON_CHECK | KMSAN_POISON_FREE); + kmsan_leave_runtime(); +} + +void kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end, + pgprot_t prot, struct page **pages, + unsigned int page_shift) +{ + unsigned long shadow_start, origin_start, shadow_end, origin_end; + struct page **s_pages, **o_pages; + int nr, mapped; + + if (!kmsan_enabled) + return; + + shadow_start = vmalloc_meta((void *)start, KMSAN_META_SHADOW); + shadow_end = vmalloc_meta((void *)end, KMSAN_META_SHADOW); + if (!shadow_start) + return; + + nr = (end - start) / PAGE_SIZE; + s_pages = kcalloc(nr, sizeof(*s_pages), GFP_KERNEL); + o_pages = kcalloc(nr, sizeof(*o_pages), GFP_KERNEL); + if (!s_pages || !o_pages) + goto ret; + for (int i = 0; i < nr; i++) { + s_pages[i] = shadow_page_for(pages[i]); + o_pages[i] = origin_page_for(pages[i]); + } + prot = __pgprot(pgprot_val(prot) | _PAGE_NX); + prot = PAGE_KERNEL; + + origin_start = vmalloc_meta((void *)start, KMSAN_META_ORIGIN); + origin_end = vmalloc_meta((void *)end, KMSAN_META_ORIGIN); + kmsan_enter_runtime(); + mapped = __vmap_pages_range_noflush(shadow_start, shadow_end, prot, + s_pages, page_shift); + KMSAN_WARN_ON(mapped); + mapped = __vmap_pages_range_noflush(origin_start, origin_end, prot, + o_pages, page_shift); + KMSAN_WARN_ON(mapped); + kmsan_leave_runtime(); + flush_tlb_kernel_range(shadow_start, shadow_end); + flush_tlb_kernel_range(origin_start, origin_end); + flush_cache_vmap(shadow_start, shadow_end); + flush_cache_vmap(origin_start, origin_end); + +ret: + kfree(s_pages); + kfree(o_pages); +} diff --git a/mm/memory.c b/mm/memory.c index 4ba73f5aa8bb7..6cc35d2cae8fd 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -52,6 +52,7 @@ #include #include #include +#include #include #include #include @@ -3128,6 +3129,7 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf) delayacct_wpcopy_end(); return 0; } + kmsan_copy_page_meta(new_page, old_page); } if (mem_cgroup_charge(page_folio(new_page), mm, GFP_KERNEL)) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index e5486d47406e8..d488dab76a6e8 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -1398,6 +1399,7 @@ static __always_inline bool free_pages_prepare(struct page *page, VM_BUG_ON_PAGE(PageTail(page), page); trace_mm_page_free(page, order); + kmsan_free_page(page, order); if (unlikely(PageHWPoison(page)) && !order) { /* @@ -3817,6 +3819,14 @@ static struct page *rmqueue_pcplist(struct zone *preferred_zone, /* * Allocate a page from the given zone. Use pcplists for order-0 allocations. */ + +/* + * Do not instrument rmqueue() with KMSAN. This function may call + * __msan_poison_alloca() through a call to set_pfnblock_flags_mask(). + * If __msan_poison_alloca() attempts to allocate pages for the stack depot, it + * may call rmqueue() again, which will result in a deadlock. + */ +__no_sanitize_memory static inline struct page *rmqueue(struct zone *preferred_zone, struct zone *zone, unsigned int order, @@ -5535,6 +5545,7 @@ struct page *__alloc_pages(gfp_t gfp, unsigned int order, int preferred_nid, } trace_mm_page_alloc(page, order, alloc_gfp, ac.migratetype); + kmsan_alloc_page(page, order, alloc_gfp); return page; } diff --git a/mm/vmalloc.c b/mm/vmalloc.c index dd6cdb2011953..68b656e0125c9 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -320,6 +320,9 @@ int ioremap_page_range(unsigned long addr, unsigned long end, err = vmap_range_noflush(addr, end, phys_addr, pgprot_nx(prot), ioremap_max_page_shift); flush_cache_vmap(addr, end); + if (!err) + kmsan_ioremap_page_range(addr, end, phys_addr, prot, + ioremap_max_page_shift); return err; } @@ -416,7 +419,7 @@ static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end, * * This is an internal function only. Do not use outside mm/. */ -void vunmap_range_noflush(unsigned long start, unsigned long end) +void __vunmap_range_noflush(unsigned long start, unsigned long end) { unsigned long next; pgd_t *pgd; @@ -438,6 +441,12 @@ void vunmap_range_noflush(unsigned long start, unsigned long end) arch_sync_kernel_mappings(start, end); } +void vunmap_range_noflush(unsigned long start, unsigned long end) +{ + kmsan_vunmap_range_noflush(start, end); + __vunmap_range_noflush(start, end); +} + /** * vunmap_range - unmap kernel virtual addresses * @addr: start of the VM area to unmap @@ -575,7 +584,7 @@ static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end, * * This is an internal function only. Do not use outside mm/. */ -int vmap_pages_range_noflush(unsigned long addr, unsigned long end, +int __vmap_pages_range_noflush(unsigned long addr, unsigned long end, pgprot_t prot, struct page **pages, unsigned int page_shift) { unsigned int i, nr = (end - addr) >> PAGE_SHIFT; @@ -601,6 +610,13 @@ int vmap_pages_range_noflush(unsigned long addr, unsigned long end, return 0; } +int vmap_pages_range_noflush(unsigned long addr, unsigned long end, + pgprot_t prot, struct page **pages, unsigned int page_shift) +{ + kmsan_vmap_pages_range_noflush(addr, end, prot, pages, page_shift); + return __vmap_pages_range_noflush(addr, end, prot, pages, page_shift); +} + /** * vmap_pages_range - map pages to a kernel virtual address * @addr: start of the VM area to map From patchwork Fri Aug 26 15:07:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956188 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 C1D38ECAAD4 for ; Fri, 26 Aug 2022 15:08:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 59EA6940008; Fri, 26 Aug 2022 11:08:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 526AB940007; Fri, 26 Aug 2022 11:08:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 351AC940008; Fri, 26 Aug 2022 11:08:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 22C48940007 for ; Fri, 26 Aug 2022 11:08:56 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id ED097120E89 for ; Fri, 26 Aug 2022 15:08:55 +0000 (UTC) X-FDA: 79842076230.18.9245379 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf05.hostedemail.com (Postfix) with ESMTP id 9BC97100016 for ; Fri, 26 Aug 2022 15:08:55 +0000 (UTC) Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-33dbfb6d2a3so29612587b3.11 for ; Fri, 26 Aug 2022 08:08:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=jx2yh37l5SDq2Q8QSIum21pACdE/sm5g4yjUaLUM63Q=; b=YwocNvFxNg+yRyYunyC8K2i0xvtr8YUlMRSXD4KB07qL6OVAVJoi69FVpTqeb6vo98 HNk/rkY+3aMZsuK1uxh+Akq9TcZkV7+UoG9UsOjRo11a/ebkyHu3Zk6ZybTqbnEICNl6 fVKoim3Gbw01E9ari0gpTSeEb1JS1RrQj9AECprq43H/JBol2bhhdu4xkoVyjXPE9+Ze 8ecJmXHBW0h1jwcuB7WCNu3ewzYMljdqqcbJK9oNsyWlgppHytBzzquDRDlGLBrz1LbR 1srEjiZAt2vaWQ/YMD9BNRcRUdlTr4PzP3ErRactmJa3ktJHlpH5z9Y33Q29N2lW/xGz lxzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=jx2yh37l5SDq2Q8QSIum21pACdE/sm5g4yjUaLUM63Q=; b=WM9Bx25ZyEep2rki8/qC2S0u8oa9XXyUhJiexFGZGJ8MbWaRKdpmp0KiiDcTwzkhPb j+wsQARWz7ZRWL7GBXsFVXYcCFxVlAwtPwr7bvHxeck7laOUMHYTzZpJ3urfD0QjnN27 KLsOhkMHRoW57MkCjS/y1iZ0Lp+Z9jgnU7UPlWKMHx9wqQkujado27ZhiQXa2HpA8pxo xzADEbHu+wJ/9q/GjYAlK863CQnIMBCXRAnH14FQ0XauQVvT0o//gtnd3TswJv1i11U9 E83XO7XVsw+9UmOeeQSMSHa4/y+rXJIBQ2fYP2FUdIiUCeeTEi6GiznyiSGZEs3K9Gs5 ezjw== X-Gm-Message-State: ACgBeo3OjL2KKJK7HbgcenZ3lARXH14OksR5QQCrNFdxxjgA6Yl6CA79 Sfupep6ANzWYFzNSvPZLsXAs0/sdCsw= X-Google-Smtp-Source: AA6agR603nVbq19tqyU4+Jy5vddr/H1MZ8Z1v7IqPxKbCCNuQvmAeaYyVDn51ytxXs9PxCixOvP5yDsDceQ= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a81:710a:0:b0:33d:c742:4a20 with SMTP id m10-20020a81710a000000b0033dc7424a20mr69884ywc.343.1661526534913; Fri, 26 Aug 2022 08:08:54 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:38 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-16-glider@google.com> Subject: [PATCH v5 15/44] mm: kmsan: call KMSAN hooks from SLUB code From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526535; a=rsa-sha256; cv=none; b=G1u/yhXMy1YwSNRMXQIK65HvEUB4DAazysCwOpLj+tdvIc1bbIyoL1qXU1KJMznXqQIv5m aKDaocGBoU6RUmBABxvxL9crue+pC+Q5+zqXdWkfed4Xd1MZK3/Mzna79AvD2DYwPDqV+s RnVtwT38xTFAd+5Vs9tBr84Mk0ekmFo= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=YwocNvFx; spf=pass (imf05.hostedemail.com: domain of 3BuIIYwYKCA0tyvqr4t11tyr.p1zyv07A-zzx8npx.14t@flex--glider.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3BuIIYwYKCA0tyvqr4t11tyr.p1zyv07A-zzx8npx.14t@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526535; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=jx2yh37l5SDq2Q8QSIum21pACdE/sm5g4yjUaLUM63Q=; b=d6zG/ag/AyduECf5sQgeilshRcllwYP756Fsywj5+DkL4ULvaO6rCNx0q2wclsDOksmJrh +Gp5Syi+/2md8zo4FUe6T2gQ08hyHBj4yEyNrDtawgMEZ0kYU8lw3LJe/4myrysqd3aMEt b7OXX7R6LbPr3nZQu2rrVDmKIHcC8/o= X-Rspam-User: Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=YwocNvFx; spf=pass (imf05.hostedemail.com: domain of 3BuIIYwYKCA0tyvqr4t11tyr.p1zyv07A-zzx8npx.14t@flex--glider.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3BuIIYwYKCA0tyvqr4t11tyr.p1zyv07A-zzx8npx.14t@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam08 X-Stat-Signature: pigonk4kdieszpqbg8ixqwbopgasdgm8 X-Rspamd-Queue-Id: 9BC97100016 X-HE-Tag: 1661526535-160997 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: In order to report uninitialized memory coming from heap allocations KMSAN has to poison them unless they're created with __GFP_ZERO. It's handy that we need KMSAN hooks in the places where init_on_alloc/init_on_free initialization is performed. In addition, we apply __no_kmsan_checks to get_freepointer_safe() to suppress reports when accessing freelist pointers that reside in freed objects. Signed-off-by: Alexander Potapenko Reviewed-by: Marco Elver --- v2: -- move the implementation of SLUB hooks here v4: -- change sizeof(type) to sizeof(*ptr) -- swap mm: and kmsan: in the subject -- get rid of kmsan_init(), replace it with __no_kmsan_checks v5: -- do not export KMSAN hooks that are not called from modules -- drop an unnecessary whitespace change Link: https://linux-review.googlesource.com/id/I6954b386c5c5d7f99f48bb6cbcc74b75136ce86e --- include/linux/kmsan.h | 57 ++++++++++++++++++++++++++++++++ mm/kmsan/hooks.c | 76 +++++++++++++++++++++++++++++++++++++++++++ mm/slab.h | 1 + mm/slub.c | 17 ++++++++++ 4 files changed, 151 insertions(+) diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h index 699fe4f5b3bee..fd76cea338878 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -15,6 +15,7 @@ #include struct page; +struct kmem_cache; #ifdef CONFIG_KMSAN @@ -72,6 +73,44 @@ void kmsan_free_page(struct page *page, unsigned int order); */ void kmsan_copy_page_meta(struct page *dst, struct page *src); +/** + * kmsan_slab_alloc() - Notify KMSAN about a slab allocation. + * @s: slab cache the object belongs to. + * @object: object pointer. + * @flags: GFP flags passed to the allocator. + * + * Depending on cache flags and GFP flags, KMSAN sets up the metadata of the + * newly created object, marking it as initialized or uninitialized. + */ +void kmsan_slab_alloc(struct kmem_cache *s, void *object, gfp_t flags); + +/** + * kmsan_slab_free() - Notify KMSAN about a slab deallocation. + * @s: slab cache the object belongs to. + * @object: object pointer. + * + * KMSAN marks the freed object as uninitialized. + */ +void kmsan_slab_free(struct kmem_cache *s, void *object); + +/** + * kmsan_kmalloc_large() - Notify KMSAN about a large slab allocation. + * @ptr: object pointer. + * @size: object size. + * @flags: GFP flags passed to the allocator. + * + * Similar to kmsan_slab_alloc(), but for large allocations. + */ +void kmsan_kmalloc_large(const void *ptr, size_t size, gfp_t flags); + +/** + * kmsan_kfree_large() - Notify KMSAN about a large slab deallocation. + * @ptr: object pointer. + * + * Similar to kmsan_slab_free(), but for large allocations. + */ +void kmsan_kfree_large(const void *ptr); + /** * kmsan_map_kernel_range_noflush() - Notify KMSAN about a vmap. * @start: start of vmapped range. @@ -138,6 +177,24 @@ static inline void kmsan_copy_page_meta(struct page *dst, struct page *src) { } +static inline void kmsan_slab_alloc(struct kmem_cache *s, void *object, + gfp_t flags) +{ +} + +static inline void kmsan_slab_free(struct kmem_cache *s, void *object) +{ +} + +static inline void kmsan_kmalloc_large(const void *ptr, size_t size, + gfp_t flags) +{ +} + +static inline void kmsan_kfree_large(const void *ptr) +{ +} + static inline void kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end, pgprot_t prot, diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c index 2624b4ea3d8ef..519a7a1dcb4aa 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -26,6 +26,82 @@ * skipping effects of functions like memset() inside instrumented code. */ +void kmsan_slab_alloc(struct kmem_cache *s, void *object, gfp_t flags) +{ + if (unlikely(object == NULL)) + return; + if (!kmsan_enabled || kmsan_in_runtime()) + return; + /* + * There's a ctor or this is an RCU cache - do nothing. The memory + * status hasn't changed since last use. + */ + if (s->ctor || (s->flags & SLAB_TYPESAFE_BY_RCU)) + return; + + kmsan_enter_runtime(); + if (flags & __GFP_ZERO) + kmsan_internal_unpoison_memory(object, s->object_size, + KMSAN_POISON_CHECK); + else + kmsan_internal_poison_memory(object, s->object_size, flags, + KMSAN_POISON_CHECK); + kmsan_leave_runtime(); +} + +void kmsan_slab_free(struct kmem_cache *s, void *object) +{ + if (!kmsan_enabled || kmsan_in_runtime()) + return; + + /* RCU slabs could be legally used after free within the RCU period */ + if (unlikely(s->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON))) + return; + /* + * If there's a constructor, freed memory must remain in the same state + * until the next allocation. We cannot save its state to detect + * use-after-free bugs, instead we just keep it unpoisoned. + */ + if (s->ctor) + return; + kmsan_enter_runtime(); + kmsan_internal_poison_memory(object, s->object_size, GFP_KERNEL, + KMSAN_POISON_CHECK | KMSAN_POISON_FREE); + kmsan_leave_runtime(); +} + +void kmsan_kmalloc_large(const void *ptr, size_t size, gfp_t flags) +{ + if (unlikely(ptr == NULL)) + return; + if (!kmsan_enabled || kmsan_in_runtime()) + return; + kmsan_enter_runtime(); + if (flags & __GFP_ZERO) + kmsan_internal_unpoison_memory((void *)ptr, size, + /*checked*/ true); + else + kmsan_internal_poison_memory((void *)ptr, size, flags, + KMSAN_POISON_CHECK); + kmsan_leave_runtime(); +} + +void kmsan_kfree_large(const void *ptr) +{ + struct page *page; + + if (!kmsan_enabled || kmsan_in_runtime()) + return; + kmsan_enter_runtime(); + page = virt_to_head_page((void *)ptr); + KMSAN_WARN_ON(ptr != page_address(page)); + kmsan_internal_poison_memory((void *)ptr, + PAGE_SIZE << compound_order(page), + GFP_KERNEL, + KMSAN_POISON_CHECK | KMSAN_POISON_FREE); + kmsan_leave_runtime(); +} + static unsigned long vmalloc_shadow(unsigned long addr) { return (unsigned long)kmsan_get_metadata((void *)addr, diff --git a/mm/slab.h b/mm/slab.h index 4ec82bec15ecd..9d0afd2985df7 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -729,6 +729,7 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s, memset(p[i], 0, s->object_size); kmemleak_alloc_recursive(p[i], s->object_size, 1, s->flags, flags); + kmsan_slab_alloc(s, p[i], flags); } memcg_slab_post_alloc_hook(s, objcg, flags, size, p); diff --git a/mm/slub.c b/mm/slub.c index 862dbd9af4f52..2c323d83d0526 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -359,6 +360,17 @@ static void prefetch_freepointer(const struct kmem_cache *s, void *object) prefetchw(object + s->offset); } +/* + * When running under KMSAN, get_freepointer_safe() may return an uninitialized + * pointer value in the case the current thread loses the race for the next + * memory chunk in the freelist. In that case this_cpu_cmpxchg_double() in + * slab_alloc_node() will fail, so the uninitialized value won't be used, but + * KMSAN will still check all arguments of cmpxchg because of imperfect + * handling of inline assembly. + * To work around this problem, we apply __no_kmsan_checks to ensure that + * get_freepointer_safe() returns initialized memory. + */ +__no_kmsan_checks static inline void *get_freepointer_safe(struct kmem_cache *s, void *object) { unsigned long freepointer_addr; @@ -1709,6 +1721,7 @@ static inline void *kmalloc_large_node_hook(void *ptr, size_t size, gfp_t flags) ptr = kasan_kmalloc_large(ptr, size, flags); /* As ptr might get tagged, call kmemleak hook after KASAN. */ kmemleak_alloc(ptr, size, 1, flags); + kmsan_kmalloc_large(ptr, size, flags); return ptr; } @@ -1716,12 +1729,14 @@ static __always_inline void kfree_hook(void *x) { kmemleak_free(x); kasan_kfree_large(x); + kmsan_kfree_large(x); } static __always_inline bool slab_free_hook(struct kmem_cache *s, void *x, bool init) { kmemleak_free_recursive(x, s->flags); + kmsan_slab_free(s, x); debug_check_no_locks_freed(x, s->object_size); @@ -5915,6 +5930,7 @@ static char *create_unique_id(struct kmem_cache *s) p += sprintf(p, "%07u", s->size); BUG_ON(p > name + ID_STR_LENGTH - 1); + kmsan_unpoison_memory(name, p - name); return name; } @@ -6016,6 +6032,7 @@ static int sysfs_slab_alias(struct kmem_cache *s, const char *name) al->name = name; al->next = alias_list; alias_list = al; + kmsan_unpoison_memory(al, sizeof(*al)); return 0; } From patchwork Fri Aug 26 15:07:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956189 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 0B856ECAAD5 for ; Fri, 26 Aug 2022 15:09:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 970F5940009; Fri, 26 Aug 2022 11:08:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8AA25940007; Fri, 26 Aug 2022 11:08:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6FC64940009; Fri, 26 Aug 2022 11:08:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 5DE3F940007 for ; Fri, 26 Aug 2022 11:08:59 -0400 (EDT) Received: from smtpin31.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 3B7021C183E for ; Fri, 26 Aug 2022 15:08:59 +0000 (UTC) X-FDA: 79842076398.31.3A7A567 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf06.hostedemail.com (Postfix) with ESMTP id DA916180013 for ; Fri, 26 Aug 2022 15:08:58 +0000 (UTC) Received: by mail-wm1-f74.google.com with SMTP id r131-20020a1c4489000000b003a7b679981cso803120wma.6 for ; Fri, 26 Aug 2022 08:08:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=Oj/n9T6llj5wDAAKfJeFunm60o607TLggj1MRB6CXqU=; b=PxLdCUrn0I9r2KhoyjcHAgRjabnegFod8IN6xz54blzpZh/1HZMNwSAgdShD0YvvTh Zxa5m/TO4KXMsG3OReoI+v+75UBjGjQZGLkWHPGtaoCFJKvIQldeDs2TPHJBt92k+HBR GqPmBiuONAM/IYWYOjJo+NjVJdkfYt9fsorrmjsf8jhWXVxxLXlhRRcVPT7xJPiq3MGR sOERai0/gwO4NtaVb2+CTxwQIjtjIGirbBBqxUONp9htYRv0TtzY3VfMqWDgDmyiC71n pI0ppgM4t9DRD3K6ZENiNPm8eVLb/eKuoAsYeEkEZa3aYmpCTXb/EK1nCWbE6o9dPH2L xa+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=Oj/n9T6llj5wDAAKfJeFunm60o607TLggj1MRB6CXqU=; b=JxPGhjXjQbwwRq/qMlzDzOFA6imgvf3/hGpfAnMU/CBdTwdw1ehCj1kK0xFONtlGGf XrFpP366owc0HpgWvjotMz94F4ifIDtiZOZnJqUQU2EkNeqZychVvJGRUPeXWrK8FXBz O7fmeyC5e3mEJK2gJraYAEQ8mOfMuTyWvOS42ck/Ae7CQQxwdMJ7TnQkVKTicjAgMok3 mnM+frEWhax0sqaemKUnG2SyHSlSn2j9G2CFboGlz9iadHB/XYwE0YE/SZuXVPk8X9H0 T+TqcQQBdHXEUNOXxjGb6qvbfsH1ehbnK+QZkWgGDzvXryTHEmuFvIkDL8HxlI+PpG/s k0gg== X-Gm-Message-State: ACgBeo2tzJUFTWAElt1/qkayPcxGNm0JQlpyHIDNYvw740KiBe2hDJij YrCCWAbFt+U8Xnv2LSWMWMbQlmKtI7U= X-Google-Smtp-Source: AA6agR5rvjq/H6vV+DBU+cdF375w+nJz6Qf5RMmSw4joFRAU0pGVVVqBSQBGZeHf42N72d45iOUQjmMAIWY= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a5d:6483:0:b0:225:7fb7:f163 with SMTP id o3-20020a5d6483000000b002257fb7f163mr49905wri.391.1661526537522; Fri, 26 Aug 2022 08:08:57 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:39 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-17-glider@google.com> Subject: [PATCH v5 16/44] kmsan: handle task creation and exiting From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526538; a=rsa-sha256; cv=none; b=6D2IgE78b1M68G3juioFf6E3fK6RHPiaxFqV+aODysNkZpPeUPqYChAJYuenQZ90J4u6jJ 3fLABwtuL3nHHmmrTZTvg6ybe+av+5bNfwlmENhbyP0RhTqKVXRa0BYJ0Gq0pkPnWlvmYG NutaqAbLdyHUE/Dxq9D03HFzLARiBS0= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=PxLdCUrn; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf06.hostedemail.com: domain of 3CeIIYwYKCBAw1ytu7w44w1u.s421y3AD-220Bqs0.47w@flex--glider.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3CeIIYwYKCBAw1ytu7w44w1u.s421y3AD-220Bqs0.47w@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526538; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Oj/n9T6llj5wDAAKfJeFunm60o607TLggj1MRB6CXqU=; b=o39INJy2NLR/8dmcDHe6kdzb/koNbiYzGOfxddIhdKnbXxq2+y3KT025if4/pQYfLiqOwV UyxVfHbU7ZGwXk2pk9bQZBXvY5FvtFw/rTCoDDTUqJAYIX0UUiHMoMjkNkUWLL9TQgASNy Er53YHy2e6dZqoNWcEP3YDpV/nu+KqI= Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=PxLdCUrn; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf06.hostedemail.com: domain of 3CeIIYwYKCBAw1ytu7w44w1u.s421y3AD-220Bqs0.47w@flex--glider.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3CeIIYwYKCBAw1ytu7w44w1u.s421y3AD-220Bqs0.47w@flex--glider.bounces.google.com X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: wr99bjdarjq1gz7iy8dhid416z4b9w3f X-Rspamd-Queue-Id: DA916180013 X-HE-Tag: 1661526538-596626 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: Tell KMSAN that a new task is created, so the tool creates a backing metadata structure for that task. Signed-off-by: Alexander Potapenko --- v2: -- move implementation of kmsan_task_create() and kmsan_task_exit() here v4: -- change sizeof(type) to sizeof(*ptr) v5: -- do not export KMSAN hooks that are not called from modules -- minor comment fix Link: https://linux-review.googlesource.com/id/I0f41c3a1c7d66f7e14aabcfdfc7c69addb945805 --- include/linux/kmsan.h | 21 +++++++++++++++++++++ kernel/exit.c | 2 ++ kernel/fork.c | 2 ++ mm/kmsan/core.c | 10 ++++++++++ mm/kmsan/hooks.c | 17 +++++++++++++++++ mm/kmsan/kmsan.h | 2 ++ 6 files changed, 54 insertions(+) diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h index fd76cea338878..5ec056380a43b 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -16,6 +16,7 @@ struct page; struct kmem_cache; +struct task_struct; #ifdef CONFIG_KMSAN @@ -42,6 +43,18 @@ struct kmsan_ctx { bool allow_reporting; }; +/** + * kmsan_task_create() - Initialize KMSAN state for the task. + * @task: task to initialize. + */ +void kmsan_task_create(struct task_struct *task); + +/** + * kmsan_task_exit() - Notify KMSAN that a task has exited. + * @task: task about to finish. + */ +void kmsan_task_exit(struct task_struct *task); + /** * kmsan_alloc_page() - Notify KMSAN about an alloc_pages() call. * @page: struct page pointer returned by alloc_pages(). @@ -163,6 +176,14 @@ void kmsan_iounmap_page_range(unsigned long start, unsigned long end); #else +static inline void kmsan_task_create(struct task_struct *task) +{ +} + +static inline void kmsan_task_exit(struct task_struct *task) +{ +} + static inline int kmsan_alloc_page(struct page *page, unsigned int order, gfp_t flags) { diff --git a/kernel/exit.c b/kernel/exit.c index 84021b24f79e3..f5d620c315662 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -60,6 +60,7 @@ #include #include #include +#include #include #include #include @@ -741,6 +742,7 @@ void __noreturn do_exit(long code) WARN_ON(tsk->plug); kcov_task_exit(tsk); + kmsan_task_exit(tsk); coredump_task_exit(tsk); ptrace_event(PTRACE_EVENT_EXIT, code); diff --git a/kernel/fork.c b/kernel/fork.c index 90c85b17bf698..7cf3eea01ceef 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -1026,6 +1027,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node) tsk->worker_private = NULL; kcov_task_init(tsk); + kmsan_task_create(tsk); kmap_local_fork(tsk); #ifdef CONFIG_FAULT_INJECTION diff --git a/mm/kmsan/core.c b/mm/kmsan/core.c index a5dec6e62b4ef..a640e0cccbb64 100644 --- a/mm/kmsan/core.c +++ b/mm/kmsan/core.c @@ -44,6 +44,16 @@ bool kmsan_enabled __read_mostly; */ DEFINE_PER_CPU(struct kmsan_ctx, kmsan_percpu_ctx); +void kmsan_internal_task_create(struct task_struct *task) +{ + struct kmsan_ctx *ctx = &task->kmsan_ctx; + struct thread_info *info = current_thread_info(); + + __memset(ctx, 0, sizeof(*ctx)); + ctx->allow_reporting = true; + kmsan_internal_unpoison_memory(info, sizeof(*info), false); +} + void kmsan_internal_poison_memory(void *address, size_t size, gfp_t flags, unsigned int poison_flags) { diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c index 519a7a1dcb4aa..4ab8c629acd0c 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -26,6 +26,23 @@ * skipping effects of functions like memset() inside instrumented code. */ +void kmsan_task_create(struct task_struct *task) +{ + kmsan_enter_runtime(); + kmsan_internal_task_create(task); + kmsan_leave_runtime(); +} + +void kmsan_task_exit(struct task_struct *task) +{ + struct kmsan_ctx *ctx = &task->kmsan_ctx; + + if (!kmsan_enabled || kmsan_in_runtime()) + return; + + ctx->allow_reporting = false; +} + void kmsan_slab_alloc(struct kmem_cache *s, void *object, gfp_t flags) { if (unlikely(object == NULL)) diff --git a/mm/kmsan/kmsan.h b/mm/kmsan/kmsan.h index 6b9deee3b7f32..04954b83c5d65 100644 --- a/mm/kmsan/kmsan.h +++ b/mm/kmsan/kmsan.h @@ -179,6 +179,8 @@ void kmsan_internal_set_shadow_origin(void *address, size_t size, int b, u32 origin, bool checked); depot_stack_handle_t kmsan_internal_chain_origin(depot_stack_handle_t id); +void kmsan_internal_task_create(struct task_struct *task); + bool kmsan_metadata_is_contiguous(void *addr, size_t size); void kmsan_internal_check_memory(void *addr, size_t size, const void *user_addr, int reason); From patchwork Fri Aug 26 15:07:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956190 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 7815CECAAD4 for ; Fri, 26 Aug 2022 15:09:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1A5E194000A; Fri, 26 Aug 2022 11:09:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 12F66940007; Fri, 26 Aug 2022 11:09:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC33B94000A; Fri, 26 Aug 2022 11:09:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id D718A940007 for ; Fri, 26 Aug 2022 11:09:02 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id AE4ED1A0F5C for ; Fri, 26 Aug 2022 15:09:02 +0000 (UTC) X-FDA: 79842076524.24.7886FC2 Received: from mail-lj1-f201.google.com (mail-lj1-f201.google.com [209.85.208.201]) by imf23.hostedemail.com (Postfix) with ESMTP id 667F514002B for ; Fri, 26 Aug 2022 15:09:02 +0000 (UTC) Received: by mail-lj1-f201.google.com with SMTP id m1-20020a2eb6c1000000b00261e5aa37feso664113ljo.6 for ; Fri, 26 Aug 2022 08:09:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=peKPwrvfH0Rhral1Ak/YbLPiQRpKcgzst7DNXReDuC8=; b=JVje2ZYrNSyUOT6EgsrAThh/ZSHL42NNHSzlW0T/DzIwcN7lzoCRgvgEnLZLhi/9jf KuRmxjlhPjFXkvU5CU+Kl763kcDY/6yl2mP1K+7T0VTAT2SJhxeY0SvLz/qi4ofIOgOA a5IryecLZM05NFvj8DWVat33QHqz0A9Cnzpy1JtGMxJOJLzdGcY8pR/rHltG9MDtljee IES9DBwjkVTXEKAD5OwUBrTXsV8dB6EN8Rl5ckNsV5h+8qWdAWciDM9Ep0xXhjexN5Ms uPXAhAPV+KmSCBrpOeXGsqJ+c7IcS1n4l6qoHs0SN0Uy9KeC5/kddkft3Mb4UT4O/p5J Cxww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=peKPwrvfH0Rhral1Ak/YbLPiQRpKcgzst7DNXReDuC8=; b=gutVQIjLC7tNliZ1ZvQQOtBnJ1UWS2O7EwAEnzXItOjn9/g2vC/9Ct4p/qWSWHVp66 miknTccCnMcuHTxoxQ+YDjiuiRxk0/5733avW5xILHUr+I9S2EU5DgKLs4q8PTuHF7e+ 1GC4FEbOWaNt0J0nHLmS1YEwgfROBQHvjc5NKk0klctDvNKx8AM5XNJ3tWr9iRz317Lj 2hoQfm7rfc4j3hUY3n9GIQ+IkvMDhqZroZ8pqShIu7Xy6iaXQpCS6SD7XKfSzoM+Fhpc jd5dsMWMx6lQHTg811HEwnJf914G36oXeBbLTv8Hi7crAW/M+JBQUoQc3U6CUQqEOsq9 vDVw== X-Gm-Message-State: ACgBeo2JjE9CMQr0UZ7x7cdhM05xmfTzBGexh5DrKGVr2QVHZ8SH09yZ zDM72Xb/EAGE1jfoQb/6LRShPKbVw2A= X-Google-Smtp-Source: AA6agR41Puui4Cpl2H4PtFlER9econMJbdI5v//Xr2UOlz9OxsMRMsoZnbZjh7y9JvU2HGHE61lkWYqLyPQ= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a05:6512:39c6:b0:48b:9d1d:fd9c with SMTP id k6-20020a05651239c600b0048b9d1dfd9cmr2923286lfu.633.1661526540422; Fri, 26 Aug 2022 08:09:00 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:40 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-18-glider@google.com> Subject: [PATCH v5 17/44] init: kmsan: call KMSAN initialization routines From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526542; a=rsa-sha256; cv=none; b=FiUr/VxvyHw96E06LoVA0HxDnJ8gb9WHEGmczVcSlI56yWTCGvbWXv2pZlVpWOpIzUb5gp wRoRvxazDBr4TpERk+LjRbQYT6X6hUns1Vdf6jRUJH/fjW3BtwbvwL5Qu8YbMzE1z5JlNI 6nJuHEV3NhvmQHFoo167tYHS/MeBOhA= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=JVje2ZYr; spf=pass (imf23.hostedemail.com: domain of 3DOIIYwYKCBMz41wxAz77z4x.v75416DG-553Etv3.7Az@flex--glider.bounces.google.com designates 209.85.208.201 as permitted sender) smtp.mailfrom=3DOIIYwYKCBMz41wxAz77z4x.v75416DG-553Etv3.7Az@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526542; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=peKPwrvfH0Rhral1Ak/YbLPiQRpKcgzst7DNXReDuC8=; b=OKbsU7q0W5ygprZK9Di/YSm3WGm5yFHE+HBZiRqf7Yo/IPmWnECtcr4e0hrdB2FipVFRxj i2yOVLTLPAXT4rOntsnig0mV63CUfyhOmywutv0foheMd3H3uUkOBbQsYmTil8g64K4+sj D4QotuLSFpiCbh7E9FM4QqGjUVQmW3Q= X-Rspam-User: Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=JVje2ZYr; spf=pass (imf23.hostedemail.com: domain of 3DOIIYwYKCBMz41wxAz77z4x.v75416DG-553Etv3.7Az@flex--glider.bounces.google.com designates 209.85.208.201 as permitted sender) smtp.mailfrom=3DOIIYwYKCBMz41wxAz77z4x.v75416DG-553Etv3.7Az@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam10 X-Stat-Signature: 9y4eug4z91oxtf1ihdrpwmadadza1nxe X-Rspamd-Queue-Id: 667F514002B X-HE-Tag: 1661526542-766730 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_shadow() scans the mappings created at boot time and creates metadata pages for those mappings. When the memblock allocator returns pages to pagealloc, we reserve 2/3 of those pages and use them as metadata for the remaining 1/3. Once KMSAN starts, every page allocated by pagealloc has its associated shadow and origin pages. kmsan_initialize() initializes the bookkeeping for init_task and enables KMSAN. Signed-off-by: Alexander Potapenko --- v2: -- move mm/kmsan/init.c and kmsan_memblock_free_pages() to this patch -- print a warning that KMSAN is a debugging tool (per Greg K-H's request) v4: -- change sizeof(type) to sizeof(*ptr) -- replace occurrences of |var| with @var -- swap init: and kmsan: in the subject v5: -- address Marco Elver's comments -- don't export initialization routines -- use modern style for-loops -- better name for struct page_pair -- delete duplicate function prototypes Link: https://linux-review.googlesource.com/id/I7bc53706141275914326df2345881ffe0cdd16bd --- include/linux/kmsan.h | 36 +++++++ init/main.c | 3 + mm/kmsan/Makefile | 3 +- mm/kmsan/init.c | 235 ++++++++++++++++++++++++++++++++++++++++++ mm/kmsan/kmsan.h | 3 + mm/kmsan/shadow.c | 34 ++++++ mm/page_alloc.c | 4 + 7 files changed, 317 insertions(+), 1 deletion(-) create mode 100644 mm/kmsan/init.c diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h index 5ec056380a43b..f056ba8a7a551 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -55,6 +55,28 @@ void kmsan_task_create(struct task_struct *task); */ void kmsan_task_exit(struct task_struct *task); +/** + * kmsan_init_shadow() - Initialize KMSAN shadow at boot time. + * + * Allocate and initialize KMSAN metadata for early allocations. + */ +void __init kmsan_init_shadow(void); + +/** + * kmsan_init_runtime() - Initialize KMSAN state and enable KMSAN. + */ +void __init kmsan_init_runtime(void); + +/** + * kmsan_memblock_free_pages() - handle freeing of memblock pages. + * @page: struct page to free. + * @order: order of @page. + * + * Freed pages are either returned to buddy allocator or held back to be used + * as metadata pages. + */ +bool __init kmsan_memblock_free_pages(struct page *page, unsigned int order); + /** * kmsan_alloc_page() - Notify KMSAN about an alloc_pages() call. * @page: struct page pointer returned by alloc_pages(). @@ -176,6 +198,20 @@ void kmsan_iounmap_page_range(unsigned long start, unsigned long end); #else +static inline void kmsan_init_shadow(void) +{ +} + +static inline void kmsan_init_runtime(void) +{ +} + +static inline bool kmsan_memblock_free_pages(struct page *page, + unsigned int order) +{ + return true; +} + static inline void kmsan_task_create(struct task_struct *task) { } diff --git a/init/main.c b/init/main.c index 91642a4e69be6..f8ca3ad78fbcd 100644 --- a/init/main.c +++ b/init/main.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -836,6 +837,7 @@ static void __init mm_init(void) init_mem_debugging_and_hardening(); kfence_alloc_pool(); report_meminit(); + kmsan_init_shadow(); stack_depot_early_init(); mem_init(); mem_init_print_info(); @@ -853,6 +855,7 @@ static void __init mm_init(void) init_espfix_bsp(); /* Should be run after espfix64 is set up. */ pti_init(); + kmsan_init_runtime(); } #ifdef CONFIG_RANDOMIZE_KSTACK_OFFSET diff --git a/mm/kmsan/Makefile b/mm/kmsan/Makefile index 550ad8625e4f9..401acb1a491ce 100644 --- a/mm/kmsan/Makefile +++ b/mm/kmsan/Makefile @@ -3,7 +3,7 @@ # Makefile for KernelMemorySanitizer (KMSAN). # # -obj-y := core.o instrumentation.o hooks.o report.o shadow.o +obj-y := core.o instrumentation.o init.o hooks.o report.o shadow.o KMSAN_SANITIZE := n KCOV_INSTRUMENT := n @@ -18,6 +18,7 @@ CFLAGS_REMOVE.o = $(CC_FLAGS_FTRACE) CFLAGS_core.o := $(CC_FLAGS_KMSAN_RUNTIME) CFLAGS_hooks.o := $(CC_FLAGS_KMSAN_RUNTIME) +CFLAGS_init.o := $(CC_FLAGS_KMSAN_RUNTIME) CFLAGS_instrumentation.o := $(CC_FLAGS_KMSAN_RUNTIME) CFLAGS_report.o := $(CC_FLAGS_KMSAN_RUNTIME) CFLAGS_shadow.o := $(CC_FLAGS_KMSAN_RUNTIME) diff --git a/mm/kmsan/init.c b/mm/kmsan/init.c new file mode 100644 index 0000000000000..7fb794242fad0 --- /dev/null +++ b/mm/kmsan/init.c @@ -0,0 +1,235 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KMSAN initialization routines. + * + * Copyright (C) 2017-2021 Google LLC + * Author: Alexander Potapenko + * + */ + +#include "kmsan.h" + +#include +#include +#include + +#include "../internal.h" + +#define NUM_FUTURE_RANGES 128 +struct start_end_pair { + u64 start, end; +}; + +static struct start_end_pair start_end_pairs[NUM_FUTURE_RANGES] __initdata; +static int future_index __initdata; + +/* + * Record a range of memory for which the metadata pages will be created once + * the page allocator becomes available. + */ +static void __init kmsan_record_future_shadow_range(void *start, void *end) +{ + u64 nstart = (u64)start, nend = (u64)end, cstart, cend; + bool merged = false; + + KMSAN_WARN_ON(future_index == NUM_FUTURE_RANGES); + KMSAN_WARN_ON((nstart >= nend) || !nstart || !nend); + nstart = ALIGN_DOWN(nstart, PAGE_SIZE); + nend = ALIGN(nend, PAGE_SIZE); + + /* + * Scan the existing ranges to see if any of them overlaps with + * [start, end). In that case, merge the two ranges instead of + * creating a new one. + * The number of ranges is less than 20, so there is no need to organize + * them into a more intelligent data structure. + */ + for (int i = 0; i < future_index; i++) { + cstart = start_end_pairs[i].start; + cend = start_end_pairs[i].end; + if ((cstart < nstart && cend < nstart) || + (cstart > nend && cend > nend)) + /* ranges are disjoint - do not merge */ + continue; + start_end_pairs[i].start = min(nstart, cstart); + start_end_pairs[i].end = max(nend, cend); + merged = true; + break; + } + if (merged) + return; + start_end_pairs[future_index].start = nstart; + start_end_pairs[future_index].end = nend; + future_index++; +} + +/* + * Initialize the shadow for existing mappings during kernel initialization. + * These include kernel text/data sections, NODE_DATA and future ranges + * registered while creating other data (e.g. percpu). + * + * Allocations via memblock can be only done before slab is initialized. + */ +void __init kmsan_init_shadow(void) +{ + const size_t nd_size = roundup(sizeof(pg_data_t), PAGE_SIZE); + phys_addr_t p_start, p_end; + u64 loop; + int nid; + + for_each_reserved_mem_range(loop, &p_start, &p_end) + kmsan_record_future_shadow_range(phys_to_virt(p_start), + phys_to_virt(p_end)); + /* Allocate shadow for .data */ + kmsan_record_future_shadow_range(_sdata, _edata); + + for_each_online_node(nid) + kmsan_record_future_shadow_range( + NODE_DATA(nid), (char *)NODE_DATA(nid) + nd_size); + + for (int i = 0; i < future_index; i++) + kmsan_init_alloc_meta_for_range( + (void *)start_end_pairs[i].start, + (void *)start_end_pairs[i].end); +} + +struct metadata_page_pair { + struct page *shadow, *origin; +}; +static struct metadata_page_pair held_back[MAX_ORDER] __initdata; + +/* + * Eager metadata allocation. When the memblock allocator is freeing pages to + * pagealloc, we use 2/3 of them as metadata for the remaining 1/3. + * We store the pointers to the returned blocks of pages in held_back[] grouped + * by their order: when kmsan_memblock_free_pages() is called for the first + * time with a certain order, it is reserved as a shadow block, for the second + * time - as an origin block. On the third time the incoming block receives its + * shadow and origin ranges from the previously saved shadow and origin blocks, + * after which held_back[order] can be used again. + * + * At the very end there may be leftover blocks in held_back[]. They are + * collected later by kmsan_memblock_discard(). + */ +bool kmsan_memblock_free_pages(struct page *page, unsigned int order) +{ + struct page *shadow, *origin; + + if (!held_back[order].shadow) { + held_back[order].shadow = page; + return false; + } + if (!held_back[order].origin) { + held_back[order].origin = page; + return false; + } + shadow = held_back[order].shadow; + origin = held_back[order].origin; + kmsan_setup_meta(page, shadow, origin, order); + + held_back[order].shadow = NULL; + held_back[order].origin = NULL; + return true; +} + +#define MAX_BLOCKS 8 +struct smallstack { + struct page *items[MAX_BLOCKS]; + int index; + int order; +}; + +static struct smallstack collect = { + .index = 0, + .order = MAX_ORDER, +}; + +static void smallstack_push(struct smallstack *stack, struct page *pages) +{ + KMSAN_WARN_ON(stack->index == MAX_BLOCKS); + stack->items[stack->index] = pages; + stack->index++; +} +#undef MAX_BLOCKS + +static struct page *smallstack_pop(struct smallstack *stack) +{ + struct page *ret; + + KMSAN_WARN_ON(stack->index == 0); + stack->index--; + ret = stack->items[stack->index]; + stack->items[stack->index] = NULL; + return ret; +} + +static void do_collection(void) +{ + struct page *page, *shadow, *origin; + + while (collect.index >= 3) { + page = smallstack_pop(&collect); + shadow = smallstack_pop(&collect); + origin = smallstack_pop(&collect); + kmsan_setup_meta(page, shadow, origin, collect.order); + __free_pages_core(page, collect.order); + } +} + +static void collect_split(void) +{ + struct smallstack tmp = { + .order = collect.order - 1, + .index = 0, + }; + struct page *page; + + if (!collect.order) + return; + while (collect.index) { + page = smallstack_pop(&collect); + smallstack_push(&tmp, &page[0]); + smallstack_push(&tmp, &page[1 << tmp.order]); + } + __memcpy(&collect, &tmp, sizeof(tmp)); +} + +/* + * Memblock is about to go away. Split the page blocks left over in held_back[] + * and return 1/3 of that memory to the system. + */ +static void kmsan_memblock_discard(void) +{ + /* + * For each order=N: + * - push held_back[N].shadow and .origin to @collect; + * - while there are >= 3 elements in @collect, do garbage collection: + * - pop 3 ranges from @collect; + * - use two of them as shadow and origin for the third one; + * - repeat; + * - split each remaining element from @collect into 2 ranges of + * order=N-1, + * - repeat. + */ + collect.order = MAX_ORDER - 1; + for (int i = MAX_ORDER - 1; i >= 0; i--) { + if (held_back[i].shadow) + smallstack_push(&collect, held_back[i].shadow); + if (held_back[i].origin) + smallstack_push(&collect, held_back[i].origin); + held_back[i].shadow = NULL; + held_back[i].origin = NULL; + do_collection(); + collect_split(); + } +} + +void __init kmsan_init_runtime(void) +{ + /* Assuming current is init_task */ + kmsan_internal_task_create(current); + kmsan_memblock_discard(); + pr_info("Starting KernelMemorySanitizer\n"); + pr_info("ATTENTION: KMSAN is a debugging tool! Do not use it on production machines!\n"); + kmsan_enabled = true; +} diff --git a/mm/kmsan/kmsan.h b/mm/kmsan/kmsan.h index 04954b83c5d65..e064b4601af9d 100644 --- a/mm/kmsan/kmsan.h +++ b/mm/kmsan/kmsan.h @@ -66,6 +66,7 @@ struct shadow_origin_ptr { struct shadow_origin_ptr kmsan_get_shadow_origin_ptr(void *addr, u64 size, bool store); void *kmsan_get_metadata(void *addr, bool is_origin); +void __init kmsan_init_alloc_meta_for_range(void *start, void *end); enum kmsan_bug_reason { REASON_ANY, @@ -186,6 +187,8 @@ void kmsan_internal_check_memory(void *addr, size_t size, const void *user_addr, int reason); struct page *kmsan_vmalloc_to_page_or_null(void *vaddr); +void kmsan_setup_meta(struct page *page, struct page *shadow, + struct page *origin, int order); /* * kmsan_internal_is_module_addr() and kmsan_internal_is_vmalloc_addr() are diff --git a/mm/kmsan/shadow.c b/mm/kmsan/shadow.c index 8c81a059beea6..6e90a806a7045 100644 --- a/mm/kmsan/shadow.c +++ b/mm/kmsan/shadow.c @@ -258,3 +258,37 @@ void kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end, kfree(s_pages); kfree(o_pages); } + +/* Allocate metadata for pages allocated at boot time. */ +void __init kmsan_init_alloc_meta_for_range(void *start, void *end) +{ + struct page *shadow_p, *origin_p; + void *shadow, *origin; + struct page *page; + u64 size; + + start = (void *)ALIGN_DOWN((u64)start, PAGE_SIZE); + size = ALIGN((u64)end - (u64)start, PAGE_SIZE); + shadow = memblock_alloc(size, PAGE_SIZE); + origin = memblock_alloc(size, PAGE_SIZE); + for (u64 addr = 0; addr < size; addr += PAGE_SIZE) { + page = virt_to_page_or_null((char *)start + addr); + shadow_p = virt_to_page_or_null((char *)shadow + addr); + set_no_shadow_origin_page(shadow_p); + shadow_page_for(page) = shadow_p; + origin_p = virt_to_page_or_null((char *)origin + addr); + set_no_shadow_origin_page(origin_p); + origin_page_for(page) = origin_p; + } +} + +void kmsan_setup_meta(struct page *page, struct page *shadow, + struct page *origin, int order) +{ + for (int i = 0; i < (1 << order); i++) { + set_no_shadow_origin_page(&shadow[i]); + set_no_shadow_origin_page(&origin[i]); + shadow_page_for(&page[i]) = &shadow[i]; + origin_page_for(&page[i]) = &origin[i]; + } +} diff --git a/mm/page_alloc.c b/mm/page_alloc.c index d488dab76a6e8..b28093e3bb42a 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1806,6 +1806,10 @@ void __init memblock_free_pages(struct page *page, unsigned long pfn, { if (early_page_uninitialised(pfn)) return; + if (!kmsan_memblock_free_pages(page, order)) { + /* KMSAN will take care of these pages. */ + return; + } __free_pages_core(page, order); } From patchwork Fri Aug 26 15:07:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956191 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 443DAECAAA3 for ; Fri, 26 Aug 2022 15:09:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D4064940010; Fri, 26 Aug 2022 11:09:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CC7BE940007; Fri, 26 Aug 2022 11:09:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A577E940010; Fri, 26 Aug 2022 11:09:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 90E31940007 for ; Fri, 26 Aug 2022 11:09:04 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 604E180E24 for ; Fri, 26 Aug 2022 15:09:04 +0000 (UTC) X-FDA: 79842076608.28.D040E26 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf27.hostedemail.com (Postfix) with ESMTP id 0D25740034 for ; Fri, 26 Aug 2022 15:09:03 +0000 (UTC) Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-33dce8cae71so29654717b3.8 for ; Fri, 26 Aug 2022 08:09:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=pkHi/aIOhGHio3xjvQCqySH1KrxER9e0jmCiCnEZ6lc=; b=JXeupyYJFRZ1PBcUpgnvFCPsxW7O3TyLbJjLRr3bEw8bazd8AjxTgtEgaL4RFoFAcL axwLX7U/Nn4sG6TkxjstDO07G6IATCfIb/BK3SRzs+9AI2e4/1+GdLwmIEJAttOe66rh Ws/2NAnzJijP6+LerNKmbWUU6tsOs6rlS8LAx5U8e8udODCsNmtDKvJ0M8kzi7mHIsnC 1uQR2PNT1cLqbsuMDArU6TuVj+uqAyRd+Csr8b6p3yrQ9uuoqqQUbbD56/JURxVHp/cJ GEPe8uFr3m7dK02AbiE1YpxJbBPoF8luaPkbHEb4VALaEurXZ1rnMfxSeN0afBuyjAX3 L8uQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=pkHi/aIOhGHio3xjvQCqySH1KrxER9e0jmCiCnEZ6lc=; b=nyeTxBpn73q864IQKkLwlD2geZThUgEHrMMf9bvANFhPkFdNcFalQgrilRfzBmREHk /Tkj7VG2eBW7YoxnAeDazzMd+D65Sk+dw++vjTHNlgfD40p9kRTQRIgemW3Pl73nIShB ovwoSAZ4JowZWe0dZ0O6LW45CnOy6a38fpPOtqFCY4ZC/N6xc0Y2c4X8TWc5an/3SpFc fLytoSVAF+J9TcgE1Wyv8aQWbZOztKndeZcX+AYzv8X8YXWMcXosTH1n9DTn/8svl5th 6OB1Cp65QsNtwtAS4UbjsQpIMywvbqaXTO1nb+sk0GpKiV2uFErEqJ6ZWzsp8c/xo2UX ZsFg== X-Gm-Message-State: ACgBeo1RqSYN0lFnFGvScnmERoZ7BsfLeDQAStEzcYsR59hagkzSEwre 38+RPDBL6/noz8gJvUDxekFaOVI7gYo= X-Google-Smtp-Source: AA6agR7L66nO8W0zasRYtvPf81KdTk/iVgrMnKBKmf6fg3J3ezLsPgPrAE30oNB5jA6Nys8ONQqvG/KyGJI= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a81:d0d:0:b0:333:99b1:44f1 with SMTP id 13-20020a810d0d000000b0033399b144f1mr119968ywn.288.1661526543285; Fri, 26 Aug 2022 08:09:03 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:41 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-19-glider@google.com> Subject: [PATCH v5 18/44] instrumented.h: add KMSAN support From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=JXeupyYJ; spf=pass (imf27.hostedemail.com: domain of 3D-IIYwYKCBY274z0D2AA270.yA8749GJ-886Hwy6.AD2@flex--glider.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3D-IIYwYKCBY274z0D2AA270.yA8749GJ-886Hwy6.AD2@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526544; a=rsa-sha256; cv=none; b=eqLXh+Rku7b9kEtO9g6/onyutMasPxPL4q7zZkAzKJWZcT4CB+yEmf8rclseC1iGXsSBKb 64Fj84dAjyQYWm//VJIDTy9ACr69A+kGhRExfR4+kPQ1OBeGy5jhyHeq+NcJQj5TfOU87u xuaFKPVvrjIcvsSTVHrxCsnL62DD0Nk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526544; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=pkHi/aIOhGHio3xjvQCqySH1KrxER9e0jmCiCnEZ6lc=; b=zQOOkZSCS5FTKK4xtXymDU2PZCoWfmHFjYQBtXURsaRUc92QGRjN5IXbpCN/OB/uG8A4wG /AQGnUWz51dGGuMviZ70fPA0fCUjpL30B3OoTwGNuib+SGvK1qv1Jl/QqEmThNizTPAivQ 1bgjpaQG8V61xF7AiauMIZGDBbLumR4= X-Rspam-User: X-Stat-Signature: xgf3tbfzrw4d8aebixeisxcbn8q3ghwd X-Rspamd-Queue-Id: 0D25740034 Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=JXeupyYJ; spf=pass (imf27.hostedemail.com: domain of 3D-IIYwYKCBY274z0D2AA270.yA8749GJ-886Hwy6.AD2@flex--glider.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3D-IIYwYKCBY274z0D2AA270.yA8749GJ-886Hwy6.AD2@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam09 X-HE-Tag: 1661526543-377580 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: To avoid false positives, KMSAN needs to unpoison the data copied from the userspace. To detect infoleaks - check the memory buffer passed to copy_to_user(). Signed-off-by: Alexander Potapenko Reviewed-by: Marco Elver --- v2: -- move implementation of kmsan_copy_to_user() here v5: -- simplify kmsan_copy_to_user() -- provide instrument_get_user() and instrument_put_user() Link: https://linux-review.googlesource.com/id/I43e93b9c02709e6be8d222342f1b044ac8bdbaaf --- include/linux/instrumented.h | 17 +++++++++++++++- include/linux/kmsan-checks.h | 19 ++++++++++++++++++ mm/kmsan/hooks.c | 38 ++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/include/linux/instrumented.h b/include/linux/instrumented.h index ee8f7d17d34f5..5f0525d95026a 100644 --- a/include/linux/instrumented.h +++ b/include/linux/instrumented.h @@ -2,7 +2,7 @@ /* * This header provides generic wrappers for memory access instrumentation that - * the compiler cannot emit for: KASAN, KCSAN. + * the compiler cannot emit for: KASAN, KCSAN, KMSAN. */ #ifndef _LINUX_INSTRUMENTED_H #define _LINUX_INSTRUMENTED_H @@ -10,6 +10,7 @@ #include #include #include +#include #include /** @@ -117,6 +118,7 @@ instrument_copy_to_user(void __user *to, const void *from, unsigned long n) { kasan_check_read(from, n); kcsan_check_read(from, n); + kmsan_copy_to_user(to, from, n, 0); } /** @@ -151,6 +153,19 @@ static __always_inline void instrument_copy_from_user_after(const void *to, const void __user *from, unsigned long n, unsigned long left) { + kmsan_unpoison_memory(to, n - left); } +#define instrument_get_user(to) \ +({ \ + u64 __tmp = (u64)(to); \ + kmsan_unpoison_memory(&__tmp, sizeof(__tmp)); \ + to = __tmp; \ +}) + +#define instrument_put_user(from, ptr, size) \ +({ \ + kmsan_copy_to_user(ptr, &from, sizeof(from), 0); \ +}) + #endif /* _LINUX_INSTRUMENTED_H */ diff --git a/include/linux/kmsan-checks.h b/include/linux/kmsan-checks.h index a6522a0c28df9..c4cae333deec5 100644 --- a/include/linux/kmsan-checks.h +++ b/include/linux/kmsan-checks.h @@ -46,6 +46,21 @@ void kmsan_unpoison_memory(const void *address, size_t size); */ void kmsan_check_memory(const void *address, size_t size); +/** + * kmsan_copy_to_user() - Notify KMSAN about a data transfer to userspace. + * @to: destination address in the userspace. + * @from: source address in the kernel. + * @to_copy: number of bytes to copy. + * @left: number of bytes not copied. + * + * If this is a real userspace data transfer, KMSAN checks the bytes that were + * actually copied to ensure there was no information leak. If @to belongs to + * the kernel space (which is possible for compat syscalls), KMSAN just copies + * the metadata. + */ +void kmsan_copy_to_user(void __user *to, const void *from, size_t to_copy, + size_t left); + #else static inline void kmsan_poison_memory(const void *address, size_t size, @@ -58,6 +73,10 @@ static inline void kmsan_unpoison_memory(const void *address, size_t size) static inline void kmsan_check_memory(const void *address, size_t size) { } +static inline void kmsan_copy_to_user(void __user *to, const void *from, + size_t to_copy, size_t left) +{ +} #endif diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c index 4ab8c629acd0c..a8a03f079a8a5 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -204,6 +204,44 @@ void kmsan_iounmap_page_range(unsigned long start, unsigned long end) kmsan_leave_runtime(); } +void kmsan_copy_to_user(void __user *to, const void *from, size_t to_copy, + size_t left) +{ + unsigned long ua_flags; + + if (!kmsan_enabled || kmsan_in_runtime()) + return; + /* + * At this point we've copied the memory already. It's hard to check it + * before copying, as the size of actually copied buffer is unknown. + */ + + /* copy_to_user() may copy zero bytes. No need to check. */ + if (!to_copy) + return; + /* Or maybe copy_to_user() failed to copy anything. */ + if (to_copy <= left) + return; + + ua_flags = user_access_save(); + if ((u64)to < TASK_SIZE) { + /* This is a user memory access, check it. */ + kmsan_internal_check_memory((void *)from, to_copy - left, to, + REASON_COPY_TO_USER); + } else { + /* Otherwise this is a kernel memory access. This happens when a + * compat syscall passes an argument allocated on the kernel + * stack to a real syscall. + * Don't check anything, just copy the shadow of the copied + * bytes. + */ + kmsan_internal_memmove_metadata((void *)to, (void *)from, + to_copy - left); + } + user_access_restore(ua_flags); +} +EXPORT_SYMBOL(kmsan_copy_to_user); + /* Functions from kmsan-checks.h follow. */ void kmsan_poison_memory(const void *address, size_t size, gfp_t flags) { From patchwork Fri Aug 26 15:07:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956192 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 1DFEFECAAD4 for ; Fri, 26 Aug 2022 15:09:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B1C0D94000B; Fri, 26 Aug 2022 11:09:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AA4BC940007; Fri, 26 Aug 2022 11:09:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9211794000B; Fri, 26 Aug 2022 11:09:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 7C85F940007 for ; Fri, 26 Aug 2022 11:09:07 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 5D16EC0E54 for ; Fri, 26 Aug 2022 15:09:07 +0000 (UTC) X-FDA: 79842076734.19.249C5BC Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) by imf17.hostedemail.com (Postfix) with ESMTP id 15F004000C for ; Fri, 26 Aug 2022 15:09:06 +0000 (UTC) Received: by mail-ej1-f73.google.com with SMTP id sd6-20020a1709076e0600b0073315809fb5so720610ejc.10 for ; Fri, 26 Aug 2022 08:09:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=9AAV0khTKYQgqo5/UGS2nmhcIjrHMBeghL7l+ejLApU=; b=sDB5PJpJBZJ8zJUYHqSaroGLwunSs5Rwaz9QmpK5RO8+mlf1nYQAGNhjMXl1m0oFZH vzMQVlJHXNGU9aieZuBV0ihX2Xs7GeZCanFYLMBLdccTjtufIvPj0/C/g25QTIwI6/Qp sReL0ZmVrFu7c6GRkrze5jnIbC0eHcDyF/YHGMdyIlE3tPtruebYgAGRN7/qwGwqWIG0 d7O6U7dzRkmvp5N9Pl4k3vT+ZH5Z9JXhcKEWunCg7MMWVXl8aGl5D3s28uVHexHgHJDm JLeAgRz2XBzUtSevuOYUNl/dsbWhlT4VB5dIjKptqOcveXztv31OO3ARcYOVZyimZv3m XuFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=9AAV0khTKYQgqo5/UGS2nmhcIjrHMBeghL7l+ejLApU=; b=mUktXoTORUVvE55cMCtm2O2EPwl18E9wf19+dLA1F/Kq0eJvTbulKN4AeYgdNgJC8b S01l+9bIOU+byo32saNNANz14dKGueO2XzypzdPJY/tCBsikmE90HrvUzQW7DxTSCs4O h4EvQcGswRtA8goIvbLK9sxVHXhfPTh1CxNz3tl4zkNiFv1lPBliDipAsVTHKEq1GGl6 tjCLn9xqgtaB2XI+q2IwVI9fTK4beI7ns+JlqcHBaUyfY2JMjdlG/FfSPzXmwijPsPTT 8ZGnZutRb8gCtbadr8g7KE3jcuy7+rLi8gHUWkO+qvZyrowzbIXE5noMJjrTq1p5QmNO 99RQ== X-Gm-Message-State: ACgBeo1R0JBeEusw9QxDYYros6g/tQjVpI3pNPjmuDGDwU2dJdk6KCgt RFYvwSCWEf+Sgx4aS5vM2RpwjOLzflE= X-Google-Smtp-Source: AA6agR7OB7JfBU0FR/Gx7VVFS0BYXkDkX/D+5e1f7tZDKGDtu48HBg9ANVBecrhroNJG7bsthdjAB8Rc34M= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a17:906:8a77:b0:73d:deef:8f76 with SMTP id hy23-20020a1709068a7700b0073ddeef8f76mr3332086ejc.765.1661526545890; Fri, 26 Aug 2022 08:09:05 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:42 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-20-glider@google.com> Subject: [PATCH v5 19/44] kmsan: unpoison @tlb in arch_tlb_gather_mmu() From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=sDB5PJpJ; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3EeIIYwYKCBg49612F4CC492.0CA96BIL-AA8Jy08.CF4@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3EeIIYwYKCBg49612F4CC492.0CA96BIL-AA8Jy08.CF4@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526547; a=rsa-sha256; cv=none; b=rIm/XvxFe9OoZlGQtD7FZEb86rpa9G1PLwC4Pjl//tclWBCDKf7HM1UOJPY4kDpA3WRuKp 1hQzYU29sQr2vdgfussjKZtZBq+iCcpuvKkyK+TtUqE9FJhYoypXG4J48q2TGZgALYgmeX ASYyOVamutqb96IJwn87mFWINBDOuFY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526547; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=9AAV0khTKYQgqo5/UGS2nmhcIjrHMBeghL7l+ejLApU=; b=2D12cosG6wmlApv/erAm9xGpXEffLMMzCmb0ZLAN70FKi765cnAFk4eu6PN8rb+2wy8AW+ PpK0ljhp9flsGGAczMLheNVsGTtjDhyEpTcV9GeJD2VN55NDso3drDj+PgK3qn+RcEWLBr /cBL6+6wmRG6kw7aZ08lneGPRz+wYYg= X-Rspam-User: X-Rspamd-Queue-Id: 15F004000C Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=sDB5PJpJ; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3EeIIYwYKCBg49612F4CC492.0CA96BIL-AA8Jy08.CF4@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3EeIIYwYKCBg49612F4CC492.0CA96BIL-AA8Jy08.CF4@flex--glider.bounces.google.com X-Stat-Signature: wkn6xunu5hchgox9y4gcue4dfkjd35hj X-Rspamd-Server: rspam03 X-HE-Tag: 1661526546-28125 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: This is an optimization to reduce stackdepot pressure. struct mmu_gather contains 7 1-bit fields packed into a 32-bit unsigned int value. The remaining 25 bits remain uninitialized and are never used, but KMSAN updates the origin for them in zap_pXX_range() in mm/memory.c, thus creating very long origin chains. This is technically correct, but consumes too much memory. Unpoisoning the whole structure will prevent creating such chains. Signed-off-by: Alexander Potapenko Acked-by: Marco Elver --- v5: -- updated description as suggested by Marco Elver Link: https://linux-review.googlesource.com/id/I76abee411b8323acfdbc29bc3a60dca8cff2de77 --- mm/mmu_gather.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/mm/mmu_gather.c b/mm/mmu_gather.c index a71924bd38c0d..add4244e5790d 100644 --- a/mm/mmu_gather.c +++ b/mm/mmu_gather.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -265,6 +266,15 @@ void tlb_flush_mmu(struct mmu_gather *tlb) static void __tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, bool fullmm) { + /* + * struct mmu_gather contains 7 1-bit fields packed into a 32-bit + * unsigned int value. The remaining 25 bits remain uninitialized + * and are never used, but KMSAN updates the origin for them in + * zap_pXX_range() in mm/memory.c, thus creating very long origin + * chains. This is technically correct, but consumes too much memory. + * Unpoisoning the whole structure will prevent creating such chains. + */ + kmsan_unpoison_memory(tlb, sizeof(*tlb)); tlb->mm = mm; tlb->fullmm = fullmm; From patchwork Fri Aug 26 15:07: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: 12956193 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 3099DECAAD4 for ; Fri, 26 Aug 2022 15:09:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C076E940008; Fri, 26 Aug 2022 11:09:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B8E65940007; Fri, 26 Aug 2022 11:09:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9BAF7940008; Fri, 26 Aug 2022 11:09:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 89B75940007 for ; Fri, 26 Aug 2022 11:09:10 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 576E41A0EBA for ; Fri, 26 Aug 2022 15:09:10 +0000 (UTC) X-FDA: 79842076860.21.8B6E3A2 Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf14.hostedemail.com (Postfix) with ESMTP id 009D210001A for ; Fri, 26 Aug 2022 15:09:09 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id q32-20020a05640224a000b004462f105fa9so1239079eda.4 for ; Fri, 26 Aug 2022 08:09:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=sAAv4rjcKBcVdRkEmlFE/2pa6fVQ+FymuVP5r3ZkSlI=; b=JZiG5Re4MF+VCa2xcEOtoGIN0T2/OlhDseNXFK0iQ1PnqQdTbI1JZrkp7lVyg8+XXy 3FX58aOp3yEhQ69BL9KLP1xsx5YJIe9+zppmM6QMfAhSuxAKThSHgjQM2yycPHihDuKz i2djpfTU43h2g5u3Lr7aPVAqaJIecBrr7/zGlgzSAGfdlt6KxN6oVSMfJ2omoi6N/bK4 GRhnLyqkHBfxBRxMrlZ83fqspSPBqI2I6ldHAHjwHJMOXx3wWPO3DfQ80yGF5CLrbpfN 7wncFTD4b9cCQe70WIerWDhBUOJbuexSLBDBhL5Axyzz/siEoiRyAYSkCi88l1QWaf/L BH0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=sAAv4rjcKBcVdRkEmlFE/2pa6fVQ+FymuVP5r3ZkSlI=; b=zeCqmAGKYwkd1TuvdtmkS/DMG0Ya31PgVOPEVJoMZWABJYa1uzSep5o++7n4qWqNv0 e029ELie9Ex8qA54wB5hHIul6m5zKG0imeUUCq+cHlGjKL3udOrvmDU3+fK6+0ykTkoy EEKZHZRJ9wEsxlFmXbSNHOlblSHvHYsGjNQwCM94juhMD2l350MKykyAv6m89uZxFvP8 fx5NOy1CrHbvyRGT9UqA/e/ILeyGBo/cRWNsTMBSVARXg/OeXBaCKmlLpPW/z3LJ7zlY yGt+5ptTTXq4d6/wyY4iJ7uG71t00uTIzDVVCMnCiu1Rbu7BVvk0pO0NLwTFAU0oVlhG sSEw== X-Gm-Message-State: ACgBeo0h0n44b2qtg4Now/2f7u7tZ4/g8nH/yiR2BMhzue76j3H8fqEN Msku1ooz+lqg+kzV1WfUvcfzrQRtWaQ= X-Google-Smtp-Source: AA6agR7I8UjZBgTpe1NFCsD09x2YoQcBDBFrSyqklOpQRZZDZsEGkSDDS6+A5L7hb/nYEuWrc359HGbAT9Q= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a05:6402:894:b0:447:fe25:15cf with SMTP id e20-20020a056402089400b00447fe2515cfmr1613274edy.404.1661526548777; Fri, 26 Aug 2022 08:09:08 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:43 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-21-glider@google.com> Subject: [PATCH v5 20/44] kmsan: add iomap support From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526550; a=rsa-sha256; cv=none; b=Mnde9U7Y8CrPSLr7Tf7FYyt8B0bl4+vr2kV3NBCYlOMsXscpRXcouCg2L1omoeL66PPjHX Sl2qPbjeYP7JYKR4e+KLqfYxvD/zc/nLQJABsfcJC6x7u7k6OpfMfhAEaX1XX0B/NJD/Js +h9LiogxcYNFeE8y6NHtGCXONRYpDD8= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=JZiG5Re4; spf=pass (imf14.hostedemail.com: domain of 3FOIIYwYKCBs7C945I7FF7C5.3FDC9ELO-DDBM13B.FI7@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3FOIIYwYKCBs7C945I7FF7C5.3FDC9ELO-DDBM13B.FI7@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526550; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=sAAv4rjcKBcVdRkEmlFE/2pa6fVQ+FymuVP5r3ZkSlI=; b=XvnY/0ZVv7ptIqo08ymXZM9/yjMqu3egE+fnvP89aufIW8u2Nf5a3dwQJ/WWTJcB8aYMWN xvNwM47DoTvRyQtMlaiJw5w8GRQtZtYZ1YZqf+UnT2Rrg9BV+JYHOhof40BahwVJJ7f54Q HAegC38bOMQVJWkpy4fnjifmRZ2SxOc= X-Rspam-User: Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=JZiG5Re4; spf=pass (imf14.hostedemail.com: domain of 3FOIIYwYKCBs7C945I7FF7C5.3FDC9ELO-DDBM13B.FI7@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3FOIIYwYKCBs7C945I7FF7C5.3FDC9ELO-DDBM13B.FI7@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam08 X-Stat-Signature: zouxjy9atrzc88dmbkmphdmjz6qec5yt X-Rspamd-Queue-Id: 009D210001A X-HE-Tag: 1661526549-2636 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: Functions from lib/iomap.c interact with hardware, so KMSAN must ensure that: - every read function returns an initialized value - every write function checks values before sending them to hardware. Signed-off-by: Alexander Potapenko --- v4: -- switch from __no_sanitize_memory (which now means "no KMSAN instrumentation") to __no_kmsan_checks (i.e. "unpoison everything") Link: https://linux-review.googlesource.com/id/I45527599f09090aca046dfe1a26df453adab100d --- lib/iomap.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/lib/iomap.c b/lib/iomap.c index fbaa3e8f19d6c..4f8b31baa5752 100644 --- a/lib/iomap.c +++ b/lib/iomap.c @@ -6,6 +6,7 @@ */ #include #include +#include #include @@ -70,26 +71,35 @@ static void bad_io_access(unsigned long port, const char *access) #define mmio_read64be(addr) swab64(readq(addr)) #endif +/* + * Here and below, we apply __no_kmsan_checks to functions reading data from + * hardware, to ensure that KMSAN marks their return values as initialized. + */ +__no_kmsan_checks unsigned int ioread8(const void __iomem *addr) { IO_COND(addr, return inb(port), return readb(addr)); return 0xff; } +__no_kmsan_checks unsigned int ioread16(const void __iomem *addr) { IO_COND(addr, return inw(port), return readw(addr)); return 0xffff; } +__no_kmsan_checks unsigned int ioread16be(const void __iomem *addr) { IO_COND(addr, return pio_read16be(port), return mmio_read16be(addr)); return 0xffff; } +__no_kmsan_checks unsigned int ioread32(const void __iomem *addr) { IO_COND(addr, return inl(port), return readl(addr)); return 0xffffffff; } +__no_kmsan_checks unsigned int ioread32be(const void __iomem *addr) { IO_COND(addr, return pio_read32be(port), return mmio_read32be(addr)); @@ -142,18 +152,21 @@ static u64 pio_read64be_hi_lo(unsigned long port) return lo | (hi << 32); } +__no_kmsan_checks u64 ioread64_lo_hi(const void __iomem *addr) { IO_COND(addr, return pio_read64_lo_hi(port), return readq(addr)); return 0xffffffffffffffffULL; } +__no_kmsan_checks u64 ioread64_hi_lo(const void __iomem *addr) { IO_COND(addr, return pio_read64_hi_lo(port), return readq(addr)); return 0xffffffffffffffffULL; } +__no_kmsan_checks u64 ioread64be_lo_hi(const void __iomem *addr) { IO_COND(addr, return pio_read64be_lo_hi(port), @@ -161,6 +174,7 @@ u64 ioread64be_lo_hi(const void __iomem *addr) return 0xffffffffffffffffULL; } +__no_kmsan_checks u64 ioread64be_hi_lo(const void __iomem *addr) { IO_COND(addr, return pio_read64be_hi_lo(port), @@ -188,22 +202,32 @@ EXPORT_SYMBOL(ioread64be_hi_lo); void iowrite8(u8 val, void __iomem *addr) { + /* Make sure uninitialized memory isn't copied to devices. */ + kmsan_check_memory(&val, sizeof(val)); IO_COND(addr, outb(val,port), writeb(val, addr)); } void iowrite16(u16 val, void __iomem *addr) { + /* Make sure uninitialized memory isn't copied to devices. */ + kmsan_check_memory(&val, sizeof(val)); IO_COND(addr, outw(val,port), writew(val, addr)); } void iowrite16be(u16 val, void __iomem *addr) { + /* Make sure uninitialized memory isn't copied to devices. */ + kmsan_check_memory(&val, sizeof(val)); IO_COND(addr, pio_write16be(val,port), mmio_write16be(val, addr)); } void iowrite32(u32 val, void __iomem *addr) { + /* Make sure uninitialized memory isn't copied to devices. */ + kmsan_check_memory(&val, sizeof(val)); IO_COND(addr, outl(val,port), writel(val, addr)); } void iowrite32be(u32 val, void __iomem *addr) { + /* Make sure uninitialized memory isn't copied to devices. */ + kmsan_check_memory(&val, sizeof(val)); IO_COND(addr, pio_write32be(val,port), mmio_write32be(val, addr)); } EXPORT_SYMBOL(iowrite8); @@ -239,24 +263,32 @@ static void pio_write64be_hi_lo(u64 val, unsigned long port) void iowrite64_lo_hi(u64 val, void __iomem *addr) { + /* Make sure uninitialized memory isn't copied to devices. */ + kmsan_check_memory(&val, sizeof(val)); IO_COND(addr, pio_write64_lo_hi(val, port), writeq(val, addr)); } void iowrite64_hi_lo(u64 val, void __iomem *addr) { + /* Make sure uninitialized memory isn't copied to devices. */ + kmsan_check_memory(&val, sizeof(val)); IO_COND(addr, pio_write64_hi_lo(val, port), writeq(val, addr)); } void iowrite64be_lo_hi(u64 val, void __iomem *addr) { + /* Make sure uninitialized memory isn't copied to devices. */ + kmsan_check_memory(&val, sizeof(val)); IO_COND(addr, pio_write64be_lo_hi(val, port), mmio_write64be(val, addr)); } void iowrite64be_hi_lo(u64 val, void __iomem *addr) { + /* Make sure uninitialized memory isn't copied to devices. */ + kmsan_check_memory(&val, sizeof(val)); IO_COND(addr, pio_write64be_hi_lo(val, port), mmio_write64be(val, addr)); } @@ -328,14 +360,20 @@ static inline void mmio_outsl(void __iomem *addr, const u32 *src, int count) void ioread8_rep(const void __iomem *addr, void *dst, unsigned long count) { IO_COND(addr, insb(port,dst,count), mmio_insb(addr, dst, count)); + /* KMSAN must treat values read from devices as initialized. */ + kmsan_unpoison_memory(dst, count); } void ioread16_rep(const void __iomem *addr, void *dst, unsigned long count) { IO_COND(addr, insw(port,dst,count), mmio_insw(addr, dst, count)); + /* KMSAN must treat values read from devices as initialized. */ + kmsan_unpoison_memory(dst, count * 2); } void ioread32_rep(const void __iomem *addr, void *dst, unsigned long count) { IO_COND(addr, insl(port,dst,count), mmio_insl(addr, dst, count)); + /* KMSAN must treat values read from devices as initialized. */ + kmsan_unpoison_memory(dst, count * 4); } EXPORT_SYMBOL(ioread8_rep); EXPORT_SYMBOL(ioread16_rep); @@ -343,14 +381,20 @@ EXPORT_SYMBOL(ioread32_rep); void iowrite8_rep(void __iomem *addr, const void *src, unsigned long count) { + /* Make sure uninitialized memory isn't copied to devices. */ + kmsan_check_memory(src, count); IO_COND(addr, outsb(port, src, count), mmio_outsb(addr, src, count)); } void iowrite16_rep(void __iomem *addr, const void *src, unsigned long count) { + /* Make sure uninitialized memory isn't copied to devices. */ + kmsan_check_memory(src, count * 2); IO_COND(addr, outsw(port, src, count), mmio_outsw(addr, src, count)); } void iowrite32_rep(void __iomem *addr, const void *src, unsigned long count) { + /* Make sure uninitialized memory isn't copied to devices. */ + kmsan_check_memory(src, count * 4); IO_COND(addr, outsl(port, src,count), mmio_outsl(addr, src, count)); } EXPORT_SYMBOL(iowrite8_rep); From patchwork Fri Aug 26 15:07:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956194 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 0DF85ECAAD4 for ; Fri, 26 Aug 2022 15:09:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9957A94000C; Fri, 26 Aug 2022 11:09:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 92233940007; Fri, 26 Aug 2022 11:09:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7705494000C; Fri, 26 Aug 2022 11:09:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 66341940007 for ; Fri, 26 Aug 2022 11:09:13 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 4AF77AC227 for ; Fri, 26 Aug 2022 15:09:13 +0000 (UTC) X-FDA: 79842076986.30.7FD7ED8 Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) by imf22.hostedemail.com (Postfix) with ESMTP id EE5A3C0015 for ; Fri, 26 Aug 2022 15:09:12 +0000 (UTC) Received: by mail-ej1-f74.google.com with SMTP id qw34-20020a1709066a2200b00730ca5a94bfso721685ejc.3 for ; Fri, 26 Aug 2022 08:09:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=7FFI4T9ldWmH+mtJkkuNX0Iy/L7lZFULsA12RNz3xXM=; b=QeYGUAhX5R1EbSsJKxcsBcPRZBVW1PHkAvLdrlFwdXozptYGSnAUgNLSENVZNbs/pm K7DsUzuQ3O+CgsIf/IsEjZ9LO6DDEbJ/9UkwQ3NFfO5E9GyCNzZ0bQPXg5zln9YU9onl g042XVM8aCr5IqAR958NCkCsQWdTGI4OQtHiGm/rgicIY9qzg4V73Kb90uYceut/3UVw nFA6k+3xgurgpKXQPV00WyHGYNj3CUHxYWE3tNF2YhTlAM4LRfiVradwPFKvsYBl8Bzt CIjpkvmBr/QmH0E2CwFFacPDaEu6Y7HjWsctPdxYCvSgU8zQkP/JHmVv1mOQDs6l/MdM WRRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=7FFI4T9ldWmH+mtJkkuNX0Iy/L7lZFULsA12RNz3xXM=; b=pbrLGnLf6FbRie67sNAMuOFP7liSP13863VteKfq2WMFrLl3wqZa1XmgtCfnCyNR0R NfiYpHSwXHLxWNMM3u0fTQEVswp5goSQYS+YCULW7NaFqi8nBr7zcRZdFosXN1psMj/9 mxkjgDsgmNupME5oeClKc4ZaepqqF+5JzRofpBQNLb9oEtKDksqf3qE0bJClGrPrKVKQ /zLizF52rhGiL+giHAfmrU8rB0+w6cD9KPwiIo7BAMxF6ZVtOy3+Ra/rnsEYoyHceyBO 6I3+hdxPq+FTtsd7F9f2sj0uqtRoqDK1b9z+89cihmHWNvaThN+udHI+Dzi0OkDqZ6Xb MYxQ== X-Gm-Message-State: ACgBeo1ZRiVr2JaSuj3xqnxLTiLvf6vN4qYdZiB97C/u3iVN3e1dYnBj HBcy3VBc3gOR787BKf2ggDAXY9ZkBTo= X-Google-Smtp-Source: AA6agR4fgqA+dojvdGvqhLNd5rEasz16MWjupX7avc3NFMUfZTqvlMHxZx6remvRJmIFi8mix2S1kZP+8vA= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a17:907:6d9b:b0:731:1135:dc2d with SMTP id sb27-20020a1709076d9b00b007311135dc2dmr5939518ejc.76.1661526551609; Fri, 26 Aug 2022 08:09:11 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:44 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-22-glider@google.com> Subject: [PATCH v5 21/44] Input: libps2: mark data received in __ps2_command() as initialized From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526552; a=rsa-sha256; cv=none; b=tGUqqCL6lfUxupebff4GwZSG2ltZuLJvSzjm97Sb1d58msRDevowZOjxYKhDwpfl3u9cc6 OO9p5fSwXWuk3/1uXPhhHUpYnNL+JWba86jnstVQjby4RzirS6e/bn5aYGXWx/oZl+Mrmf E2ftjCudbwyX3QKdFEIwBIxnpQhBbVk= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=QeYGUAhX; spf=pass (imf22.hostedemail.com: domain of 3F-IIYwYKCB4AFC78LAIIAF8.6IGFCHOR-GGEP46E.ILA@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3F-IIYwYKCB4AFC78LAIIAF8.6IGFCHOR-GGEP46E.ILA@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526552; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=7FFI4T9ldWmH+mtJkkuNX0Iy/L7lZFULsA12RNz3xXM=; b=Mg3YuBl56fl36ACywTH+dpDpMvghlKMbEztUTjSP68xC5wAj0uTPbSrQNt/otiZZUFXbE4 aEdAY3fZqh/+BFEESx5A77XIXYH2RDKmO/hgrFT6Gl4AozUbCBtzmWgIARVZ7cBK6MOS47 yjB1xeufuaCqzew5QJlXnXljEp31+Gk= X-Rspam-User: Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=QeYGUAhX; spf=pass (imf22.hostedemail.com: domain of 3F-IIYwYKCB4AFC78LAIIAF8.6IGFCHOR-GGEP46E.ILA@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3F-IIYwYKCB4AFC78LAIIAF8.6IGFCHOR-GGEP46E.ILA@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam10 X-Stat-Signature: x7kf8nph6469rn58uoahwnd4uaihr9kr X-Rspamd-Queue-Id: EE5A3C0015 X-HE-Tag: 1661526552-705588 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 does not know that the device initializes certain bytes in ps2dev->cmdbuf. Call kmsan_unpoison_memory() to explicitly mark them as initialized. Signed-off-by: Alexander Potapenko --- Link: https://linux-review.googlesource.com/id/I2d26f6baa45271d37320d3f4a528c39cb7e545f0 --- drivers/input/serio/libps2.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/input/serio/libps2.c b/drivers/input/serio/libps2.c index 250e213cc80c6..3e19344eda93c 100644 --- a/drivers/input/serio/libps2.c +++ b/drivers/input/serio/libps2.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -294,9 +295,11 @@ int __ps2_command(struct ps2dev *ps2dev, u8 *param, unsigned int command) serio_pause_rx(ps2dev->serio); - if (param) + if (param) { for (i = 0; i < receive; i++) param[i] = ps2dev->cmdbuf[(receive - 1) - i]; + kmsan_unpoison_memory(param, receive); + } if (ps2dev->cmdcnt && (command != PS2_CMD_RESET_BAT || ps2dev->cmdcnt != 1)) { From patchwork Fri Aug 26 15:07:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956195 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 172EFECAAD4 for ; Fri, 26 Aug 2022 15:09:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A804494000D; Fri, 26 Aug 2022 11:09:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A0782940007; Fri, 26 Aug 2022 11:09:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8826394000D; Fri, 26 Aug 2022 11:09:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 763B8940007 for ; Fri, 26 Aug 2022 11:09:17 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 46545C0E50 for ; Fri, 26 Aug 2022 15:09:17 +0000 (UTC) X-FDA: 79842077154.16.91EAE7F Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf01.hostedemail.com (Postfix) with ESMTP id 711EF4001D for ; Fri, 26 Aug 2022 15:09:15 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id c14-20020a05640227ce00b0043e5df12e2cso1236774ede.15 for ; Fri, 26 Aug 2022 08:09:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=76jHGgwozZvM48bVgyafH0ePqqgPygQeeBv9QE8zFSs=; b=Ez/lNwvAaDDeJuJMLhCTbPEpOPlHhfNIq7VyjE0qAVyZCRontZv0oB0LtGJ+nv4bwC gZw/mTwPTBWyA0dCxVGdhKH7XEOHdENq7Cv0XhQdf86eksUUxOa2+VWsVcz6rEA4dWpu Zvs62QVT+xB2cN1avkjLXHEoEa+e1ekxcYkU9ofidPy0VrkaWYbMd4GmDWtb/KlvsLO2 yXZ6UmXxmwAic31Rzt9UwrAuioHtXeVlWsWj3qGlJA79GCkFWZNr2tEMTQpyVXsI1d7b 0KcOCYL7oQy7CjEb6pflswFigH5TXa8Rfml+4QXtapWs+MN75E6ySnIaQBQTkmU2kvfY TSxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=76jHGgwozZvM48bVgyafH0ePqqgPygQeeBv9QE8zFSs=; b=ePSeg4n8MmkI+KoSH6FMLLak0ULm8WMzP7GLDrYaScs6KUzv+YPnGxGLng6d6dVEbG WDKKiPESzsMiXz8Jn1GfRF4P4mNY3ZR12t2DZFF1VDqtqrspvGYsvzPcpL69OnOe6ocO 01LIKhp2X2ktKSjsKemxaKDe98/GzEP+WfTjgiHS1RrMXcbqSQ2tILPFYShPlBYw/Y/n ez/vwvZqK6F5CtI4trkQuLIEHD8u+2737LRoTbP8rRUxP/60LUDJuXK5o4AkFRbl9RtK lK5BM6vxexzDi6TWnMVO5ZdSzNzJEzJkQfVoNH6WMIF1MlxQ7DoebLt2jg8OHGrz6Cij UADA== X-Gm-Message-State: ACgBeo3VtCMVzioDOQ3OUlt7AknwXPFV4tp639OcIeSKu+IpeT5+Uqh4 qElFZe+f10TNhQIFBZ7DdvCUdf9Byyo= X-Google-Smtp-Source: AA6agR6bf5SCba3ePQHoJhycDuv14GdKQb0ZRH0Sro37EO5apbOGfCrAwn2LLTqoAO1tZW/O5CJdylcsL78= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a17:906:8458:b0:73d:d0e9:4b27 with SMTP id e24-20020a170906845800b0073dd0e94b27mr5257444ejy.766.1661526554281; Fri, 26 Aug 2022 08:09:14 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:45 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-23-glider@google.com> Subject: [PATCH v5 22/44] dma: kmsan: unpoison DMA mappings From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526555; a=rsa-sha256; cv=none; b=pNCYRH7dxy2BDKkCLdf85Hq4zYK2w81RooSsHObLIu+gBXgkZCiQBrssGLEqjm5+60Wvou ErdAVGc07sr+UGVwb98tbeN6h/snIy4kbE3XITZeI88CsdpdW2l6ixloujannhzmMotC/N ZhigzoV1+Yuz/oJeI7ua9sckC6KJrFY= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="Ez/lNwvA"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf01.hostedemail.com: domain of 3GuIIYwYKCCEDIFABODLLDIB.9LJIFKRU-JJHS79H.LOD@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3GuIIYwYKCCEDIFABODLLDIB.9LJIFKRU-JJHS79H.LOD@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526555; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=76jHGgwozZvM48bVgyafH0ePqqgPygQeeBv9QE8zFSs=; b=EJ4GFxtGQ7+shJpsJNUYxLQyLXt1+oixIJRTZA9fO8RWVx6+VnyA1VlJDkRtuNA5c8d7Vw EHc5hveLjpl+MCqieQzOssB/FGX05V0xeZYqgVsyCLDWgfwwMZMCyEeAJOEdWXc0OrxDCA SAzT5nSNJCf2seom4kYgw0jIY2rVvN8= X-Rspamd-Queue-Id: 711EF4001D Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="Ez/lNwvA"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf01.hostedemail.com: domain of 3GuIIYwYKCCEDIFABODLLDIB.9LJIFKRU-JJHS79H.LOD@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3GuIIYwYKCCEDIFABODLLDIB.9LJIFKRU-JJHS79H.LOD@flex--glider.bounces.google.com X-Rspamd-Server: rspam12 X-Rspam-User: X-Stat-Signature: 5jj4s8ys3dhroufoiitd316uiymuz3cb X-HE-Tag: 1661526555-581171 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 doesn't know about DMA memory writes performed by devices. We unpoison such memory when it's mapped to avoid false positive reports. Signed-off-by: Alexander Potapenko --- v2: -- move implementation of kmsan_handle_dma() and kmsan_handle_dma_sg() here v4: -- swap dma: and kmsan: int the subject v5: -- do not export KMSAN hooks that are not called from modules Link: https://linux-review.googlesource.com/id/Ia162dc4c5a92e74d4686c1be32a4dfeffc5c32cd --- include/linux/kmsan.h | 41 ++++++++++++++++++++++++++++++ kernel/dma/mapping.c | 9 ++++--- mm/kmsan/hooks.c | 59 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 3 deletions(-) diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h index f056ba8a7a551..c6ae00e327e5e 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -9,6 +9,7 @@ #ifndef _LINUX_KMSAN_H #define _LINUX_KMSAN_H +#include #include #include #include @@ -17,6 +18,7 @@ struct page; struct kmem_cache; struct task_struct; +struct scatterlist; #ifdef CONFIG_KMSAN @@ -196,6 +198,35 @@ void kmsan_ioremap_page_range(unsigned long addr, unsigned long end, */ void kmsan_iounmap_page_range(unsigned long start, unsigned long end); +/** + * kmsan_handle_dma() - Handle a DMA data transfer. + * @page: first page of the buffer. + * @offset: offset of the buffer within the first page. + * @size: buffer size. + * @dir: one of possible dma_data_direction values. + * + * Depending on @direction, KMSAN: + * * checks the buffer, if it is copied to device; + * * initializes the buffer, if it is copied from device; + * * does both, if this is a DMA_BIDIRECTIONAL transfer. + */ +void kmsan_handle_dma(struct page *page, size_t offset, size_t size, + enum dma_data_direction dir); + +/** + * kmsan_handle_dma_sg() - Handle a DMA transfer using scatterlist. + * @sg: scatterlist holding DMA buffers. + * @nents: number of scatterlist entries. + * @dir: one of possible dma_data_direction values. + * + * Depending on @direction, KMSAN: + * * checks the buffers in the scatterlist, if they are copied to device; + * * initializes the buffers, if they are copied from device; + * * does both, if this is a DMA_BIDIRECTIONAL transfer. + */ +void kmsan_handle_dma_sg(struct scatterlist *sg, int nents, + enum dma_data_direction dir); + #else static inline void kmsan_init_shadow(void) @@ -278,6 +309,16 @@ static inline void kmsan_iounmap_page_range(unsigned long start, { } +static inline void kmsan_handle_dma(struct page *page, size_t offset, + size_t size, enum dma_data_direction dir) +{ +} + +static inline void kmsan_handle_dma_sg(struct scatterlist *sg, int nents, + enum dma_data_direction dir) +{ +} + #endif #endif /* _LINUX_KMSAN_H */ diff --git a/kernel/dma/mapping.c b/kernel/dma/mapping.c index 49cbf3e33de71..48dfd11807be2 100644 --- a/kernel/dma/mapping.c +++ b/kernel/dma/mapping.c @@ -156,6 +156,7 @@ dma_addr_t dma_map_page_attrs(struct device *dev, struct page *page, addr = dma_direct_map_page(dev, page, offset, size, dir, attrs); else addr = ops->map_page(dev, page, offset, size, dir, attrs); + kmsan_handle_dma(page, offset, size, dir); debug_dma_map_page(dev, page, offset, size, dir, addr, attrs); return addr; @@ -194,11 +195,13 @@ static int __dma_map_sg_attrs(struct device *dev, struct scatterlist *sg, else ents = ops->map_sg(dev, sg, nents, dir, attrs); - if (ents > 0) + if (ents > 0) { + kmsan_handle_dma_sg(sg, nents, dir); debug_dma_map_sg(dev, sg, nents, ents, dir, attrs); - else if (WARN_ON_ONCE(ents != -EINVAL && ents != -ENOMEM && - ents != -EIO && ents != -EREMOTEIO)) + } else if (WARN_ON_ONCE(ents != -EINVAL && ents != -ENOMEM && + ents != -EIO && ents != -EREMOTEIO)) { return -EIO; + } return ents; } diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c index a8a03f079a8a5..41b6b41e6183a 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -10,9 +10,11 @@ */ #include +#include #include #include #include +#include #include #include @@ -242,6 +244,63 @@ void kmsan_copy_to_user(void __user *to, const void *from, size_t to_copy, } EXPORT_SYMBOL(kmsan_copy_to_user); +static void kmsan_handle_dma_page(const void *addr, size_t size, + enum dma_data_direction dir) +{ + switch (dir) { + case DMA_BIDIRECTIONAL: + kmsan_internal_check_memory((void *)addr, size, /*user_addr*/ 0, + REASON_ANY); + kmsan_internal_unpoison_memory((void *)addr, size, + /*checked*/ false); + break; + case DMA_TO_DEVICE: + kmsan_internal_check_memory((void *)addr, size, /*user_addr*/ 0, + REASON_ANY); + break; + case DMA_FROM_DEVICE: + kmsan_internal_unpoison_memory((void *)addr, size, + /*checked*/ false); + break; + case DMA_NONE: + break; + } +} + +/* Helper function to handle DMA data transfers. */ +void kmsan_handle_dma(struct page *page, size_t offset, size_t size, + enum dma_data_direction dir) +{ + u64 page_offset, to_go, addr; + + if (PageHighMem(page)) + return; + addr = (u64)page_address(page) + offset; + /* + * The kernel may occasionally give us adjacent DMA pages not belonging + * to the same allocation. Process them separately to avoid triggering + * internal KMSAN checks. + */ + while (size > 0) { + page_offset = addr % PAGE_SIZE; + to_go = min(PAGE_SIZE - page_offset, (u64)size); + kmsan_handle_dma_page((void *)addr, to_go, dir); + addr += to_go; + size -= to_go; + } +} + +void kmsan_handle_dma_sg(struct scatterlist *sg, int nents, + enum dma_data_direction dir) +{ + struct scatterlist *item; + int i; + + for_each_sg(sg, item, nents, i) + kmsan_handle_dma(sg_page(item), item->offset, item->length, + dir); +} + /* Functions from kmsan-checks.h follow. */ void kmsan_poison_memory(const void *address, size_t size, gfp_t flags) { From patchwork Fri Aug 26 15:07:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956196 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 171C0ECAAA3 for ; Fri, 26 Aug 2022 15:09:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4DB8494000E; Fri, 26 Aug 2022 11:09:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 464DC940007; Fri, 26 Aug 2022 11:09:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 240E794000E; Fri, 26 Aug 2022 11:09:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 11ED3940007 for ; Fri, 26 Aug 2022 11:09:18 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E731DC0E40 for ; Fri, 26 Aug 2022 15:09:17 +0000 (UTC) X-FDA: 79842077154.13.1319C54 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf10.hostedemail.com (Postfix) with ESMTP id A9B82C0015 for ; Fri, 26 Aug 2022 15:09:17 +0000 (UTC) Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-33931a5c133so29305947b3.17 for ; Fri, 26 Aug 2022 08:09:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=XOIO6yMrJJaBLCVdiKJxecGrRstBRdkXKAU8JD1YObM=; b=RBRSbeDrL8qGQexUijKhcV3H7NuqN9uz8ZM5rQWAltRN3zhZJP9TKmmIf+p+yKdl6T 3Am09npoAEtHEWxF/Bl4GBV+lvaWA+zyaViXoAoS6FK+zkjAU0V2E1ILS8x3lB5fXtKj 9IHWzkzR0lKD9H/hxXtLbHPszHgU+gNR/ouI3pNZH7yhF0/W+C1cpcyiViY1J1iEBFY8 /rQ0ukMFzwE2rHptS3PuB+v+/gBN3s2bGa1zvZC9PEceq+oso9Jptib23PUbkHkaJtUF ybmLJcK3rBbne7ulW3JpsBkspXiEuaq15wUwrHaqYN3CJnLRo71eoIyBsvku5gZs6p/s mp3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=XOIO6yMrJJaBLCVdiKJxecGrRstBRdkXKAU8JD1YObM=; b=q5MpQSHg+8yd0ZdXirIRjJrH8/2gY51VVgKwsHuIqa2rogM0bOY97Pbw9WSvmiMGAr C1cGlNGPLSuPM1ADintcqr4bELKmXdw1g7MQJLiL9AMhSWVHdzipTQntWcaX3xUimXaz geFqiGtOYNK1BhBhlHUiaX06Z7VRaRdJaD4rsc28m/PRbEC3aa1KaCmCOtV/d54lCAer 0P+LMUZBlAYkyE9xVw92HYnqlwW7Bp55C+Erq5fkOMhVQWzQwd5/MrvtEebvXP3IJV/g 8uLxhxxzDx12qaeZ9syZuwep+rg9Dp1bN6m72aJQfi+5mJx+wwnfWqJFlEeVisudgfXJ jG7g== X-Gm-Message-State: ACgBeo0P+A5S1xHX0o/S3HV1bvCURhHFHqHM9XhB45MQOT+5Pwa8l3lm MxUvmncLHFO4riiWtpp5J3uJM0QVecA= X-Google-Smtp-Source: AA6agR70pOgUYoVn/e77Ld0R/nqA9LDlWtFjFwsnYYnj15N1nfRnKaSK4PvjO0uXZsTDiRTt9eMo2zal+mA= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a0d:e6cc:0:b0:338:c82b:9520 with SMTP id p195-20020a0de6cc000000b00338c82b9520mr151356ywe.66.1661526556996; Fri, 26 Aug 2022 08:09:16 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:46 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-24-glider@google.com> Subject: [PATCH v5 23/44] virtio: kmsan: check/unpoison scatterlist in vring_map_one_sg() From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526557; a=rsa-sha256; cv=none; b=zTMJitVZi/yGDpjBii6LSnq+90tRWAtmXsomoYjX9jaf3d/AkgbMrSOXiq/0LXsy/7p2zI /tHPXFSY6jQkBqFb2r0HS0FVQEKTDc609pQQJizp4EK5rwWZMSMjBBO/7oXvI4hS7AXskV 1Xop2lFxWgS/j2D9f+5Vp+K4LXFlZE0= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=RBRSbeDr; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of 3HOIIYwYKCCMFKHCDQFNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--glider.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3HOIIYwYKCCMFKHCDQFNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526557; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=XOIO6yMrJJaBLCVdiKJxecGrRstBRdkXKAU8JD1YObM=; b=X+fiXzF2r+Bs0hexJcojzof5SKgN6y3Wase3Ql8PjS8Zx4Hv92ISaUFwT8EwaXjvnjyKN1 l/sBpcZEjB+XUrjJmrFaQcfzT1I+6OKSgknn2R/qxDhX+xhjFiqVcieGqcfFSk+JZoFs41 Xy+HdgO960NUWu8m9bZV6B8XgHfS0CU= Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=RBRSbeDr; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of 3HOIIYwYKCCMFKHCDQFNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--glider.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3HOIIYwYKCCMFKHCDQFNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--glider.bounces.google.com X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: hnxb6ycgpbq1pgietkea19a6fu66wibh X-Rspamd-Queue-Id: A9B82C0015 X-HE-Tag: 1661526557-799243 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: If vring doesn't use the DMA API, KMSAN is unable to tell whether the memory is initialized by hardware. Explicitly call kmsan_handle_dma() from vring_map_one_sg() in this case to prevent false positives. Signed-off-by: Alexander Potapenko Acked-by: Michael S. Tsirkin --- v4: -- swap virtio: and kmsan: in the subject Link: https://linux-review.googlesource.com/id/I211533ecb86a66624e151551f83ddd749536b3af --- drivers/virtio/virtio_ring.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 4620e9d79dde8..a9f06ec5b3c27 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -352,8 +353,15 @@ static dma_addr_t vring_map_one_sg(const struct vring_virtqueue *vq, struct scatterlist *sg, enum dma_data_direction direction) { - if (!vq->use_dma_api) + if (!vq->use_dma_api) { + /* + * If DMA is not used, KMSAN doesn't know that the scatterlist + * is initialized by the hardware. Explicitly check/unpoison it + * depending on the direction. + */ + kmsan_handle_dma(sg_page(sg), sg->offset, sg->length, direction); return (dma_addr_t)sg_phys(sg); + } /* * We can't use dma_map_sg, because we don't use scatterlists in From patchwork Fri Aug 26 15:07:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956197 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 39595ECAAA3 for ; Fri, 26 Aug 2022 15:09:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C147D94000A; Fri, 26 Aug 2022 11:09:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B8EA2940007; Fri, 26 Aug 2022 11:09:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9E1D794000A; Fri, 26 Aug 2022 11:09:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 8C565940007 for ; Fri, 26 Aug 2022 11:09:21 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 61CAC160E48 for ; Fri, 26 Aug 2022 15:09:21 +0000 (UTC) X-FDA: 79842077322.05.3001C4E Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf27.hostedemail.com (Postfix) with ESMTP id E6F804002E for ; Fri, 26 Aug 2022 15:09:20 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id y14-20020a056402440e00b0044301c7ccd9so1220638eda.19 for ; Fri, 26 Aug 2022 08:09:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=PLcPoxyp1Xs3M8CttNEhz4gH6ZMFq0yiGHMdwreSHbw=; b=eh86TxxX8CwLNIBltPXsm5933LECqY2eAlL/5pBl4nWbjLT9wwFJMOBUyie0dUx/gK N82SKr30PljWUvEhhV3/fUE7T4qgCesvwEM4KVHBlRwHJfYrj27gozIMUwRuSF5xcEdQ H894CHa15jZo0p4ubi/COw4OT1+/6PqIggnVwzZ+BFxk13rWrsC3JvKzP9SXV8kNUkWw FrowsNGwL8zWTGrIAcdj6cj5ZG+bP7h4Z/52tgm2GPymsQ498t6GXSFQ1gYV+/OZFD23 v1gAArrhOq4so6cVSxA30W0GuwEXdIdz3gBKSYzqaYbpXdCyauICiavckkCPIg9Kvzky 3pOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=PLcPoxyp1Xs3M8CttNEhz4gH6ZMFq0yiGHMdwreSHbw=; b=pnTitmIuhBK0wYLEarmzmD1kFQvLBXYQ+i1YlAoSLM3cKVQR8r2pVsZD1JUIogSZHO YeQ/+9pudWzHqjtdOimFcZ17oK5V1U1rrQsquU4oWnqy9TN7gN7xnpO6zhtRerVbnU1L /umFIi2XhhP3Y92zKcLDCIlRNVh28tiMek4U8jB+b2fv4M6hoSPoVKvwzna3ujm5rosr 3+fA0cFq/xrb3vu0jz/ABbiGSe82IhCia/s5aIlkqgLNatQV5mLkT3Il1DgHOKftlbc3 2rk2Ojxwgra72a9Gprnl0qYl6vHox9ZGjMubNU/G581jSjTBxsfChpnc9/0hZdUAIfC5 HJXw== X-Gm-Message-State: ACgBeo2W8MOLtsWLORxXqIQ0N79YjFcdL0/bO06zOPd6R00iwVJx8+Bn NszRxhjfAAA2pWvpFHoKEvFY6kAQOso= X-Google-Smtp-Source: AA6agR5Si5/g8lHkQ9CsnDZQfnsfIgUA6fce6EFE/ZPTPJJ8orAZDc8GQqAPENSTfPcrOej8JbL/c8L8O08= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a05:6402:2804:b0:439:83c2:8be2 with SMTP id h4-20020a056402280400b0043983c28be2mr7114308ede.292.1661526559748; Fri, 26 Aug 2022 08:09:19 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:47 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-25-glider@google.com> Subject: [PATCH v5 24/44] kmsan: handle memory sent to/from USB From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526560; a=rsa-sha256; cv=none; b=phoyt6xIX3BrV0/P4x0/vvrnOCo1Wk+Tcie2bd8lc7QnjnnkiTW45mw9fGHD62wPZy2LoU /MlmKScW/A7oaLQs3cHxcQLyv3lzK23tecOk/OiJK0QeNcYpffRZuiQp2jFfH+UMRnKWyz P1+ozkb4N/WK5PRV02uOuH7wf7XpFok= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=eh86TxxX; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf27.hostedemail.com: domain of 3H-IIYwYKCCYINKFGTIQQING.EQONKPWZ-OOMXCEM.QTI@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3H-IIYwYKCCYINKFGTIQQING.EQONKPWZ-OOMXCEM.QTI@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526560; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=PLcPoxyp1Xs3M8CttNEhz4gH6ZMFq0yiGHMdwreSHbw=; b=mc/cPC5+ecHuA7TV/y7+6+SGl/+AjNXfpZuESyDypfqfuFgh3a6mPuFEe+quVslNbtRmlI XXDTJoTmeoZ9PP6Jm+YxdfrnxPNSZQ+dMVJVa7huE13/KHcouod1d58g8dBpz8BQWHYc7F LGWa56h/k4sPXIr33kM06Fd3qRCYTWk= X-Rspamd-Queue-Id: E6F804002E Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=eh86TxxX; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf27.hostedemail.com: domain of 3H-IIYwYKCCYINKFGTIQQING.EQONKPWZ-OOMXCEM.QTI@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3H-IIYwYKCCYINKFGTIQQING.EQONKPWZ-OOMXCEM.QTI@flex--glider.bounces.google.com X-Rspamd-Server: rspam12 X-Rspam-User: X-Stat-Signature: hy1wgdw1pipux3qouwnt9x4adxaug85k X-HE-Tag: 1661526560-447497 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: Depending on the value of is_out kmsan_handle_urb() KMSAN either marks the data copied to the kernel from a USB device as initialized, or checks the data sent to the device for being initialized. Signed-off-by: Alexander Potapenko --- v2: -- move kmsan_handle_urb() implementation to this patch v5: -- do not export KMSAN hooks that are not called from modules Link: https://linux-review.googlesource.com/id/Ifa67fb72015d4de14c30e971556f99fc8b2ee506 --- drivers/usb/core/urb.c | 2 ++ include/linux/kmsan.h | 15 +++++++++++++++ mm/kmsan/hooks.c | 16 ++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/drivers/usb/core/urb.c b/drivers/usb/core/urb.c index 33d62d7e3929f..1fe3f23205624 100644 --- a/drivers/usb/core/urb.c +++ b/drivers/usb/core/urb.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -426,6 +427,7 @@ int usb_submit_urb(struct urb *urb, gfp_t mem_flags) URB_SETUP_MAP_SINGLE | URB_SETUP_MAP_LOCAL | URB_DMA_SG_COMBINED); urb->transfer_flags |= (is_out ? URB_DIR_OUT : URB_DIR_IN); + kmsan_handle_urb(urb, is_out); if (xfertype != USB_ENDPOINT_XFER_CONTROL && dev->state < USB_STATE_CONFIGURED) diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h index c6ae00e327e5e..84dddf3aa5f8b 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -19,6 +19,7 @@ struct page; struct kmem_cache; struct task_struct; struct scatterlist; +struct urb; #ifdef CONFIG_KMSAN @@ -227,6 +228,16 @@ void kmsan_handle_dma(struct page *page, size_t offset, size_t size, void kmsan_handle_dma_sg(struct scatterlist *sg, int nents, enum dma_data_direction dir); +/** + * kmsan_handle_urb() - Handle a USB data transfer. + * @urb: struct urb pointer. + * @is_out: data transfer direction (true means output to hardware). + * + * If @is_out is true, KMSAN checks the transfer buffer of @urb. Otherwise, + * KMSAN initializes the transfer buffer. + */ +void kmsan_handle_urb(const struct urb *urb, bool is_out); + #else static inline void kmsan_init_shadow(void) @@ -319,6 +330,10 @@ static inline void kmsan_handle_dma_sg(struct scatterlist *sg, int nents, { } +static inline void kmsan_handle_urb(const struct urb *urb, bool is_out) +{ +} + #endif #endif /* _LINUX_KMSAN_H */ diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c index 41b6b41e6183a..58334fa32ff86 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "../internal.h" #include "../slab.h" @@ -244,6 +245,21 @@ void kmsan_copy_to_user(void __user *to, const void *from, size_t to_copy, } EXPORT_SYMBOL(kmsan_copy_to_user); +/* Helper function to check an URB. */ +void kmsan_handle_urb(const struct urb *urb, bool is_out) +{ + if (!urb) + return; + if (is_out) + kmsan_internal_check_memory(urb->transfer_buffer, + urb->transfer_buffer_length, + /*user_addr*/ 0, REASON_SUBMIT_URB); + else + kmsan_internal_unpoison_memory(urb->transfer_buffer, + urb->transfer_buffer_length, + /*checked*/ false); +} + static void kmsan_handle_dma_page(const void *addr, size_t size, enum dma_data_direction dir) { From patchwork Fri Aug 26 15:07:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956198 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 260D5ECAAA3 for ; Fri, 26 Aug 2022 15:09:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B6A29940008; Fri, 26 Aug 2022 11:09:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B19EA940007; Fri, 26 Aug 2022 11:09:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8F71D940008; Fri, 26 Aug 2022 11:09:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 79511940007 for ; Fri, 26 Aug 2022 11:09:24 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 558F8405A0 for ; Fri, 26 Aug 2022 15:09:24 +0000 (UTC) X-FDA: 79842077448.25.6D72EF7 Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) by imf23.hostedemail.com (Postfix) with ESMTP id 8C02F14002B for ; Fri, 26 Aug 2022 15:09:23 +0000 (UTC) Received: by mail-ej1-f74.google.com with SMTP id sa33-20020a1709076d2100b0073d83e062c8so721185ejc.0 for ; Fri, 26 Aug 2022 08:09:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=BnKMvyT06ziUGsHEJ+YEVg1tx1KzLMz0gVcHVszo8K8=; b=EiVhLZEafaAyrZD6tszEO+TFY0OwioS1rJEC090Ib8NqUc5RrN05qy/vhK9oORIELc q/QX5phZQlclN0vAwmre9pavOoMPSMR6b7/nDdQZXXzMUIiI3CVSylEHitI3THbl54p2 4qw7aI/LSIwMFTpLkQNpGoFPsRI8VBvLcPDhmP9K+AZRW4ddxRhKn6L+7er0aEKIUpsq bMu4phiNih75hkLHoSGS+NneFlparqIZFfB/r7Zkjuxpl6dn28Io0lrtCQeI4wsT5Qmi 1J6Z/uAiwcf/d7OWXeqDUXR7Tj6ogWTRPjMPRiCmUlSHq/K3bRhSzPnBf90OnZ0vFN0L 8vMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=BnKMvyT06ziUGsHEJ+YEVg1tx1KzLMz0gVcHVszo8K8=; b=fNNtBGbPjX3y9UXJUwEglj01pIvwB1cTOvyUc7RngsPgfeAeOragtnjs33lcP7U7JQ O7u4WJf3YXsPmmrf18ODuaqX3RgSJP4RlcM5qkIbVTIP/joFFEer0O6fu71kl+JXxPB/ Y1rZcF+2ddRDssMTkJpmRmZ9lg31IwoGbyIE8stTCn/Ok2hT5KC4gmoVW9NOtdxbYANn nc0FrPH07Wrp8M3s2YWLwaNna+JyuydMbE8d/ztj3JjLaJXUXSgWpjMLk44kxGbG6ZQI o4ICPNPKU6I9vk/+hHugk2thrueUeDh1zBcsstlxF1Njw35r9I/rxvmDod8aoTSx9k8q b1Qg== X-Gm-Message-State: ACgBeo2dxJSKbDUOYbTx1L70gyRgOYnSKlJySwNjqzzxHgE//m7RSXfG Sh26EputgVnojJsHPslU614EBkMqzGY= X-Google-Smtp-Source: AA6agR530TBlVn6mJbY7hmqOW/mIwoixWfGev+K32pLS7qNxq2UBJFhzrDfOIH4huqWlAfhVzxFwU8akYLI= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a17:907:7388:b0:73d:c311:2f3e with SMTP id er8-20020a170907738800b0073dc3112f3emr5671141ejc.158.1661526562331; Fri, 26 Aug 2022 08:09:22 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:48 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-26-glider@google.com> Subject: [PATCH v5 25/44] kmsan: add tests for KMSAN From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526563; a=rsa-sha256; cv=none; b=wEHGRv2W+I+a/FEF5A57tsnmCZ003dq0pOngCyU/kThTdYbANx/qDGEhXj1OH7IP8YZ51U 2uMzBvo80DkvxBvUTAzbMyFVKeqH/3WxaYpixK9CEegojUoFCfRlNz7pnBQlws5kk2z53o 8oi2rwkbwrX+kC+ByzNP17VUNuwUiwY= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=EiVhLZEa; spf=pass (imf23.hostedemail.com: domain of 3IuIIYwYKCCkLQNIJWLTTLQJ.HTRQNSZc-RRPaFHP.TWL@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3IuIIYwYKCCkLQNIJWLTTLQJ.HTRQNSZc-RRPaFHP.TWL@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526563; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=BnKMvyT06ziUGsHEJ+YEVg1tx1KzLMz0gVcHVszo8K8=; b=6v6F5AsK+DJQadmaaEI7MJnNygcqtJ8ohgcdoFktlBI/AAkDfytHLuArQr6FN/c9oyZJau f5mSazhJI9cDDEJuaJ97KJ7Him2glfrgaV3OkZtNDnsDG5Ft2n6U4hMIZGyhB4ysDXXAUm 3/AmXwv/8mms2kfCY+Y22P9Vzs8duyk= X-Stat-Signature: mjm44bgtdenzrgyjjxx3u4p4nspzropj X-Rspamd-Queue-Id: 8C02F14002B X-Rspam-User: X-Rspamd-Server: rspam06 Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=EiVhLZEa; spf=pass (imf23.hostedemail.com: domain of 3IuIIYwYKCCkLQNIJWLTTLQJ.HTRQNSZc-RRPaFHP.TWL@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3IuIIYwYKCCkLQNIJWLTTLQJ.HTRQNSZc-RRPaFHP.TWL@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-HE-Tag: 1661526563-193856 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: The testing module triggers KMSAN warnings in different cases and checks that the errors are properly reported, using console probes to capture the tool's output. Signed-off-by: Alexander Potapenko --- v2: -- add memcpy tests v4: -- change sizeof(type) to sizeof(*ptr) -- add test expectations for CONFIG_KMSAN_CHECK_PARAM_RETVAL v5: -- reapply clang-format -- use modern style for-loops -- address Marco Elver's comments Link: https://linux-review.googlesource.com/id/I49c3f59014cc37fd13541c80beb0b75a75244650 --- lib/Kconfig.kmsan | 12 + mm/kmsan/Makefile | 4 + mm/kmsan/kmsan_test.c | 552 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 568 insertions(+) create mode 100644 mm/kmsan/kmsan_test.c diff --git a/lib/Kconfig.kmsan b/lib/Kconfig.kmsan index 5b19dbd34d76e..b2489dd6503fa 100644 --- a/lib/Kconfig.kmsan +++ b/lib/Kconfig.kmsan @@ -47,4 +47,16 @@ config KMSAN_CHECK_PARAM_RETVAL may potentially report errors in corner cases when non-instrumented functions call instrumented ones. +config KMSAN_KUNIT_TEST + tristate "KMSAN integration test suite" if !KUNIT_ALL_TESTS + default KUNIT_ALL_TESTS + depends on TRACEPOINTS && KUNIT + help + Test suite for KMSAN, testing various error detection scenarios, + and checking that reports are correctly output to console. + + Say Y here if you want the test to be built into the kernel and run + during boot; say M if you want the test to build as a module; say N + if you are unsure. + endif diff --git a/mm/kmsan/Makefile b/mm/kmsan/Makefile index 401acb1a491ce..98eab2856626f 100644 --- a/mm/kmsan/Makefile +++ b/mm/kmsan/Makefile @@ -22,3 +22,7 @@ CFLAGS_init.o := $(CC_FLAGS_KMSAN_RUNTIME) CFLAGS_instrumentation.o := $(CC_FLAGS_KMSAN_RUNTIME) CFLAGS_report.o := $(CC_FLAGS_KMSAN_RUNTIME) CFLAGS_shadow.o := $(CC_FLAGS_KMSAN_RUNTIME) + +obj-$(CONFIG_KMSAN_KUNIT_TEST) += kmsan_test.o +KMSAN_SANITIZE_kmsan_test.o := y +CFLAGS_kmsan_test.o += $(call cc-disable-warning, uninitialized) diff --git a/mm/kmsan/kmsan_test.c b/mm/kmsan/kmsan_test.c new file mode 100644 index 0000000000000..b68f4334cf184 --- /dev/null +++ b/mm/kmsan/kmsan_test.c @@ -0,0 +1,552 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Test cases for KMSAN. + * For each test case checks the presence (or absence) of generated reports. + * Relies on 'console' tracepoint to capture reports as they appear in the + * kernel log. + * + * Copyright (C) 2021-2022, Google LLC. + * Author: Alexander Potapenko + * + */ + +#include +#include "kmsan.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static DEFINE_PER_CPU(int, per_cpu_var); + +/* Report as observed from console. */ +static struct { + spinlock_t lock; + bool available; + bool ignore; /* Stop console output collection. */ + char header[256]; +} observed = { + .lock = __SPIN_LOCK_UNLOCKED(observed.lock), +}; + +/* Probe for console output: obtains observed lines of interest. */ +static void probe_console(void *ignore, const char *buf, size_t len) +{ + unsigned long flags; + + if (observed.ignore) + return; + spin_lock_irqsave(&observed.lock, flags); + + if (strnstr(buf, "BUG: KMSAN: ", len)) { + /* + * KMSAN report and related to the test. + * + * The provided @buf is not NUL-terminated; copy no more than + * @len bytes and let strscpy() add the missing NUL-terminator. + */ + strscpy(observed.header, buf, + min(len + 1, sizeof(observed.header))); + WRITE_ONCE(observed.available, true); + observed.ignore = true; + } + spin_unlock_irqrestore(&observed.lock, flags); +} + +/* Check if a report related to the test exists. */ +static bool report_available(void) +{ + return READ_ONCE(observed.available); +} + +/* Information we expect in a report. */ +struct expect_report { + const char *error_type; /* Error type. */ + /* + * Kernel symbol from the error header, or NULL if no report is + * expected. + */ + const char *symbol; +}; + +/* Check observed report matches information in @r. */ +static bool report_matches(const struct expect_report *r) +{ + typeof(observed.header) expected_header; + unsigned long flags; + bool ret = false; + const char *end; + char *cur; + + /* Doubled-checked locking. */ + if (!report_available() || !r->symbol) + return (!report_available() && !r->symbol); + + /* Generate expected report contents. */ + + /* Title */ + cur = expected_header; + end = &expected_header[sizeof(expected_header) - 1]; + + cur += scnprintf(cur, end - cur, "BUG: KMSAN: %s", r->error_type); + + scnprintf(cur, end - cur, " in %s", r->symbol); + /* The exact offset won't match, remove it; also strip module name. */ + cur = strchr(expected_header, '+'); + if (cur) + *cur = '\0'; + + spin_lock_irqsave(&observed.lock, flags); + if (!report_available()) + goto out; /* A new report is being captured. */ + + /* Finally match expected output to what we actually observed. */ + ret = strstr(observed.header, expected_header); +out: + spin_unlock_irqrestore(&observed.lock, flags); + + return ret; +} + +/* ===== Test cases ===== */ + +/* Prevent replacing branch with select in LLVM. */ +static noinline void check_true(char *arg) +{ + pr_info("%s is true\n", arg); +} + +static noinline void check_false(char *arg) +{ + pr_info("%s is false\n", arg); +} + +#define USE(x) \ + do { \ + if (x) \ + check_true(#x); \ + else \ + check_false(#x); \ + } while (0) + +#define EXPECTATION_ETYPE_FN(e, reason, fn) \ + struct expect_report e = { \ + .error_type = reason, \ + .symbol = fn, \ + } + +#define EXPECTATION_NO_REPORT(e) EXPECTATION_ETYPE_FN(e, NULL, NULL) +#define EXPECTATION_UNINIT_VALUE_FN(e, fn) \ + EXPECTATION_ETYPE_FN(e, "uninit-value", fn) +#define EXPECTATION_UNINIT_VALUE(e) EXPECTATION_UNINIT_VALUE_FN(e, __func__) +#define EXPECTATION_USE_AFTER_FREE(e) \ + EXPECTATION_ETYPE_FN(e, "use-after-free", __func__) + +/* Test case: ensure that kmalloc() returns uninitialized memory. */ +static void test_uninit_kmalloc(struct kunit *test) +{ + EXPECTATION_UNINIT_VALUE(expect); + int *ptr; + + kunit_info(test, "uninitialized kmalloc test (UMR report)\n"); + ptr = kmalloc(sizeof(*ptr), GFP_KERNEL); + USE(*ptr); + KUNIT_EXPECT_TRUE(test, report_matches(&expect)); +} + +/* + * Test case: ensure that kmalloc'ed memory becomes initialized after memset(). + */ +static void test_init_kmalloc(struct kunit *test) +{ + EXPECTATION_NO_REPORT(expect); + int *ptr; + + kunit_info(test, "initialized kmalloc test (no reports)\n"); + ptr = kmalloc(sizeof(*ptr), GFP_KERNEL); + memset(ptr, 0, sizeof(*ptr)); + USE(*ptr); + KUNIT_EXPECT_TRUE(test, report_matches(&expect)); +} + +/* Test case: ensure that kzalloc() returns initialized memory. */ +static void test_init_kzalloc(struct kunit *test) +{ + EXPECTATION_NO_REPORT(expect); + int *ptr; + + kunit_info(test, "initialized kzalloc test (no reports)\n"); + ptr = kzalloc(sizeof(*ptr), GFP_KERNEL); + USE(*ptr); + KUNIT_EXPECT_TRUE(test, report_matches(&expect)); +} + +/* Test case: ensure that local variables are uninitialized by default. */ +static void test_uninit_stack_var(struct kunit *test) +{ + EXPECTATION_UNINIT_VALUE(expect); + volatile int cond; + + kunit_info(test, "uninitialized stack variable (UMR report)\n"); + USE(cond); + KUNIT_EXPECT_TRUE(test, report_matches(&expect)); +} + +/* Test case: ensure that local variables with initializers are initialized. */ +static void test_init_stack_var(struct kunit *test) +{ + EXPECTATION_NO_REPORT(expect); + volatile int cond = 1; + + kunit_info(test, "initialized stack variable (no reports)\n"); + USE(cond); + KUNIT_EXPECT_TRUE(test, report_matches(&expect)); +} + +static noinline void two_param_fn_2(int arg1, int arg2) +{ + USE(arg1); + USE(arg2); +} + +static noinline void one_param_fn(int arg) +{ + two_param_fn_2(arg, arg); + USE(arg); +} + +static noinline void two_param_fn(int arg1, int arg2) +{ + int init = 0; + + one_param_fn(init); + USE(arg1); + USE(arg2); +} + +static void test_params(struct kunit *test) +{ +#ifdef CONFIG_KMSAN_CHECK_PARAM_RETVAL + /* + * With eager param/retval checking enabled, KMSAN will report an error + * before the call to two_param_fn(). + */ + EXPECTATION_UNINIT_VALUE_FN(expect, "test_params"); +#else + EXPECTATION_UNINIT_VALUE_FN(expect, "two_param_fn"); +#endif + volatile int uninit, init = 1; + + kunit_info(test, + "uninit passed through a function parameter (UMR report)\n"); + two_param_fn(uninit, init); + KUNIT_EXPECT_TRUE(test, report_matches(&expect)); +} + +static int signed_sum3(int a, int b, int c) +{ + return a + b + c; +} + +/* + * Test case: ensure that uninitialized values are tracked through function + * arguments. + */ +static void test_uninit_multiple_params(struct kunit *test) +{ + EXPECTATION_UNINIT_VALUE(expect); + volatile char b = 3, c; + volatile int a; + + kunit_info(test, "uninitialized local passed to fn (UMR report)\n"); + USE(signed_sum3(a, b, c)); + KUNIT_EXPECT_TRUE(test, report_matches(&expect)); +} + +/* Helper function to make an array uninitialized. */ +static noinline void do_uninit_local_array(char *array, int start, int stop) +{ + volatile char uninit; + + for (int i = start; i < stop; i++) + array[i] = uninit; +} + +/* + * Test case: ensure kmsan_check_memory() reports an error when checking + * uninitialized memory. + */ +static void test_uninit_kmsan_check_memory(struct kunit *test) +{ + EXPECTATION_UNINIT_VALUE_FN(expect, "test_uninit_kmsan_check_memory"); + volatile char local_array[8]; + + kunit_info( + test, + "kmsan_check_memory() called on uninit local (UMR report)\n"); + do_uninit_local_array((char *)local_array, 5, 7); + + kmsan_check_memory((char *)local_array, 8); + KUNIT_EXPECT_TRUE(test, report_matches(&expect)); +} + +/* + * Test case: check that a virtual memory range created with vmap() from + * initialized pages is still considered as initialized. + */ +static void test_init_kmsan_vmap_vunmap(struct kunit *test) +{ + EXPECTATION_NO_REPORT(expect); + const int npages = 2; + struct page **pages; + void *vbuf; + + kunit_info(test, "pages initialized via vmap (no reports)\n"); + + pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL); + for (int i = 0; i < npages; i++) + pages[i] = alloc_page(GFP_KERNEL); + vbuf = vmap(pages, npages, VM_MAP, PAGE_KERNEL); + memset(vbuf, 0xfe, npages * PAGE_SIZE); + for (int i = 0; i < npages; i++) + kmsan_check_memory(page_address(pages[i]), PAGE_SIZE); + + if (vbuf) + vunmap(vbuf); + for (int i = 0; i < npages; i++) { + if (pages[i]) + __free_page(pages[i]); + } + kfree(pages); + KUNIT_EXPECT_TRUE(test, report_matches(&expect)); +} + +/* + * Test case: ensure that memset() can initialize a buffer allocated via + * vmalloc(). + */ +static void test_init_vmalloc(struct kunit *test) +{ + EXPECTATION_NO_REPORT(expect); + int npages = 8; + char *buf; + + kunit_info(test, "vmalloc buffer can be initialized (no reports)\n"); + buf = vmalloc(PAGE_SIZE * npages); + buf[0] = 1; + memset(buf, 0xfe, PAGE_SIZE * npages); + USE(buf[0]); + for (int i = 0; i < npages; i++) + kmsan_check_memory(&buf[PAGE_SIZE * i], PAGE_SIZE); + vfree(buf); + KUNIT_EXPECT_TRUE(test, report_matches(&expect)); +} + +/* Test case: ensure that use-after-free reporting works. */ +static void test_uaf(struct kunit *test) +{ + EXPECTATION_USE_AFTER_FREE(expect); + volatile int value; + volatile int *var; + + kunit_info(test, "use-after-free in kmalloc-ed buffer (UMR report)\n"); + var = kmalloc(80, GFP_KERNEL); + var[3] = 0xfeedface; + kfree((int *)var); + /* Copy the invalid value before checking it. */ + value = var[3]; + USE(value); + KUNIT_EXPECT_TRUE(test, report_matches(&expect)); +} + +/* + * Test case: ensure that uninitialized values are propagated through per-CPU + * memory. + */ +static void test_percpu_propagate(struct kunit *test) +{ + EXPECTATION_UNINIT_VALUE(expect); + volatile int uninit, check; + + kunit_info(test, + "uninit local stored to per_cpu memory (UMR report)\n"); + + this_cpu_write(per_cpu_var, uninit); + check = this_cpu_read(per_cpu_var); + USE(check); + KUNIT_EXPECT_TRUE(test, report_matches(&expect)); +} + +/* + * Test case: ensure that passing uninitialized values to printk() leads to an + * error report. + */ +static void test_printk(struct kunit *test) +{ +#ifdef CONFIG_KMSAN_CHECK_PARAM_RETVAL + /* + * With eager param/retval checking enabled, KMSAN will report an error + * before the call to pr_info(). + */ + EXPECTATION_UNINIT_VALUE_FN(expect, "test_printk"); +#else + EXPECTATION_UNINIT_VALUE_FN(expect, "number"); +#endif + volatile int uninit; + + kunit_info(test, "uninit local passed to pr_info() (UMR report)\n"); + pr_info("%px contains %d\n", &uninit, uninit); + KUNIT_EXPECT_TRUE(test, report_matches(&expect)); +} + +/* + * Test case: ensure that memcpy() correctly copies uninitialized values between + * aligned `src` and `dst`. + */ +static void test_memcpy_aligned_to_aligned(struct kunit *test) +{ + EXPECTATION_UNINIT_VALUE_FN(expect, "test_memcpy_aligned_to_aligned"); + volatile int uninit_src; + volatile int dst = 0; + + kunit_info( + test, + "memcpy()ing aligned uninit src to aligned dst (UMR report)\n"); + memcpy((void *)&dst, (void *)&uninit_src, sizeof(uninit_src)); + kmsan_check_memory((void *)&dst, sizeof(dst)); + KUNIT_EXPECT_TRUE(test, report_matches(&expect)); +} + +/* + * Test case: ensure that memcpy() correctly copies uninitialized values between + * aligned `src` and unaligned `dst`. + * + * Copying aligned 4-byte value to an unaligned one leads to touching two + * aligned 4-byte values. This test case checks that KMSAN correctly reports an + * error on the first of the two values. + */ +static void test_memcpy_aligned_to_unaligned(struct kunit *test) +{ + EXPECTATION_UNINIT_VALUE_FN(expect, "test_memcpy_aligned_to_unaligned"); + volatile int uninit_src; + volatile char dst[8] = { 0 }; + + kunit_info( + test, + "memcpy()ing aligned uninit src to unaligned dst (UMR report)\n"); + memcpy((void *)&dst[1], (void *)&uninit_src, sizeof(uninit_src)); + kmsan_check_memory((void *)dst, 4); + KUNIT_EXPECT_TRUE(test, report_matches(&expect)); +} + +/* + * Test case: ensure that memcpy() correctly copies uninitialized values between + * aligned `src` and unaligned `dst`. + * + * Copying aligned 4-byte value to an unaligned one leads to touching two + * aligned 4-byte values. This test case checks that KMSAN correctly reports an + * error on the second of the two values. + */ +static void test_memcpy_aligned_to_unaligned2(struct kunit *test) +{ + EXPECTATION_UNINIT_VALUE_FN(expect, + "test_memcpy_aligned_to_unaligned2"); + volatile int uninit_src; + volatile char dst[8] = { 0 }; + + kunit_info( + test, + "memcpy()ing aligned uninit src to unaligned dst - part 2 (UMR report)\n"); + memcpy((void *)&dst[1], (void *)&uninit_src, sizeof(uninit_src)); + kmsan_check_memory((void *)&dst[4], sizeof(uninit_src)); + KUNIT_EXPECT_TRUE(test, report_matches(&expect)); +} + +static struct kunit_case kmsan_test_cases[] = { + KUNIT_CASE(test_uninit_kmalloc), + KUNIT_CASE(test_init_kmalloc), + KUNIT_CASE(test_init_kzalloc), + KUNIT_CASE(test_uninit_stack_var), + KUNIT_CASE(test_init_stack_var), + KUNIT_CASE(test_params), + KUNIT_CASE(test_uninit_multiple_params), + KUNIT_CASE(test_uninit_kmsan_check_memory), + KUNIT_CASE(test_init_kmsan_vmap_vunmap), + KUNIT_CASE(test_init_vmalloc), + KUNIT_CASE(test_uaf), + KUNIT_CASE(test_percpu_propagate), + KUNIT_CASE(test_printk), + KUNIT_CASE(test_memcpy_aligned_to_aligned), + KUNIT_CASE(test_memcpy_aligned_to_unaligned), + KUNIT_CASE(test_memcpy_aligned_to_unaligned2), + {}, +}; + +/* ===== End test cases ===== */ + +static int test_init(struct kunit *test) +{ + unsigned long flags; + + spin_lock_irqsave(&observed.lock, flags); + observed.header[0] = '\0'; + observed.ignore = false; + observed.available = false; + spin_unlock_irqrestore(&observed.lock, flags); + + return 0; +} + +static void test_exit(struct kunit *test) +{ +} + +static void register_tracepoints(struct tracepoint *tp, void *ignore) +{ + check_trace_callback_type_console(probe_console); + if (!strcmp(tp->name, "console")) + WARN_ON(tracepoint_probe_register(tp, probe_console, NULL)); +} + +static void unregister_tracepoints(struct tracepoint *tp, void *ignore) +{ + if (!strcmp(tp->name, "console")) + tracepoint_probe_unregister(tp, probe_console, NULL); +} + +static int kmsan_suite_init(struct kunit_suite *suite) +{ + /* + * Because we want to be able to build the test as a module, we need to + * iterate through all known tracepoints, since the static registration + * won't work here. + */ + for_each_kernel_tracepoint(register_tracepoints, NULL); + return 0; +} + +static void kmsan_suite_exit(struct kunit_suite *suite) +{ + for_each_kernel_tracepoint(unregister_tracepoints, NULL); + tracepoint_synchronize_unregister(); +} + +static struct kunit_suite kmsan_test_suite = { + .name = "kmsan", + .test_cases = kmsan_test_cases, + .init = test_init, + .exit = test_exit, + .suite_init = kmsan_suite_init, + .suite_exit = kmsan_suite_exit, +}; +kunit_test_suites(&kmsan_test_suite); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Alexander Potapenko "); From patchwork Fri Aug 26 15:07:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956199 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 8345CECAAA3 for ; Fri, 26 Aug 2022 15:09:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 130F694000B; Fri, 26 Aug 2022 11:09:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0BAB5940007; Fri, 26 Aug 2022 11:09:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E776894000B; Fri, 26 Aug 2022 11:09:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id D34EB940007 for ; Fri, 26 Aug 2022 11:09:26 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id B4A76AC311 for ; Fri, 26 Aug 2022 15:09:26 +0000 (UTC) X-FDA: 79842077532.03.1FCB6B4 Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) by imf02.hostedemail.com (Postfix) with ESMTP id 5EB9680021 for ; Fri, 26 Aug 2022 15:09:26 +0000 (UTC) Received: by mail-ej1-f73.google.com with SMTP id hr32-20020a1709073fa000b00730a39f36ddso715324ejc.5 for ; Fri, 26 Aug 2022 08:09:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=78OHj/gzoCKqAY5c+j3k9RRp1IwnWy2nlunSdcq054Y=; b=infDBXfaloZqVmGL5DNRFAfhMOqS8NqxfxjZ9dKspnlgueeDAHoYYH+4Gi8exWGIuo Nw/AkcesiN61mybTdSO3jhwM5TKCtIZhjXZo1HG7sK+cjrWBUsaf4R/cHM2iBvYj2bOA WkeD3rhneP4hNHRSfpfEujFgSGje7KupjxgjkfUMA9MS0vN6o7veSyC7WryDqeiljwvb FmhY2QfU0D3vj7dQTHo2CxstSRJlqjkORV6jtBYWg/wZqUdM5bvjWUyfkBV2EDhFtv/1 Hn+WikD7nGPTrxjpCCiMPssZSlPqnH7OceYjz25N+b1EK3r2roffSRjICRe9X9QMXNjz qm8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=78OHj/gzoCKqAY5c+j3k9RRp1IwnWy2nlunSdcq054Y=; b=kj1iiPOtz+lt3Xh0a09RhviHFm6rEZiUMkqV1QTeVlA5/rm2FS4kXrZRvcSRv7AT33 xm8LaSHd3JYLnFAi5IDyM5ynC9rrLWJmgflsySfoAhx8tWW6AAVluqOukHsUQHxMv3gK claBsg7pamGKeVxO6TJFDXrt8GJWZmI6wYqgwe9XZzXyRudRNcO7IH8B+5d92h9VPdrH /ONjNGT4GGtvE1nB41WYvpirylHTOSsp02c4uvC7VEm4k4dehtFmcpZv/oooAaYb+VUL eXoOzZvozWLcQDezP1z1zPBloBKLcAGDxJa7kjgAouD4kvgT7/X3Y++nxDzkYfVKyfIn B9nQ== X-Gm-Message-State: ACgBeo2crxUZuesyY1J8Jvw/Gwp5ktm9RKFGYC/zsLkuPd/RlEW/ExUN r9v/+djCeJeWYFGCNv8SS+Angk/eP9w= X-Google-Smtp-Source: AA6agR6OeypAkAbsTcwRMz6skRqVX67u6RKiOQC813CDO+HQWRif6q4KXUdIv6HG7IvLOSgOm7Y2kqCqBbc= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a50:fc17:0:b0:446:861b:ee10 with SMTP id i23-20020a50fc17000000b00446861bee10mr7463262edr.251.1661526565221; Fri, 26 Aug 2022 08:09:25 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:49 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-27-glider@google.com> Subject: [PATCH v5 26/44] kmsan: disable strscpy() optimization under KMSAN From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526566; a=rsa-sha256; cv=none; b=A/zR86JrGg5/Xl1i0nXJJpsmrgviEYpLignx4JiKNC59r019DHIFbDXJ+8/kZXX8UM66PJ ca0cpzGnWNQ+ybvAVd01CVN52ms+Z79MbUBz+yFkffYx9bDfQogRaXh5/7MM1/0/dVkygW tkcEqUxK6At42G1kMkF3MBNCl5Gz/ss= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=infDBXfa; spf=pass (imf02.hostedemail.com: domain of 3JeIIYwYKCCwOTQLMZOWWOTM.KWUTQVcf-UUSdIKS.WZO@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3JeIIYwYKCCwOTQLMZOWWOTM.KWUTQVcf-UUSdIKS.WZO@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526566; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=78OHj/gzoCKqAY5c+j3k9RRp1IwnWy2nlunSdcq054Y=; b=AC2S6reVq1kwUTWSV8kcLkSmz41Pp2XkyUnJ68qvWC3ntnF+QHqeG2Zuc2fYQ17f9tcDRq o6Juj8xfo/coJoEJ1yiE9vdZL2Sx8ls4IDTibCjrnmJMDNV7uomjX0IdP0Ps70jti7RUvK gPhGKHUNLpjbZOXCSVReJmMYl2j/T8k= X-Rspam-User: Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=infDBXfa; spf=pass (imf02.hostedemail.com: domain of 3JeIIYwYKCCwOTQLMZOWWOTM.KWUTQVcf-UUSdIKS.WZO@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3JeIIYwYKCCwOTQLMZOWWOTM.KWUTQVcf-UUSdIKS.WZO@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam10 X-Stat-Signature: jgqjxeuczgjdtipcs6tfud5errsjkp74 X-Rspamd-Queue-Id: 5EB9680021 X-HE-Tag: 1661526566-577955 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: Disable the efficient 8-byte reading under KMSAN to avoid false positives. Signed-off-by: Alexander Potapenko --- Link: https://linux-review.googlesource.com/id/Iffd8336965e88fce915db2e6a9d6524422975f69 --- lib/string.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/string.c b/lib/string.c index 6f334420f6871..3371d26a0e390 100644 --- a/lib/string.c +++ b/lib/string.c @@ -197,6 +197,14 @@ ssize_t strscpy(char *dest, const char *src, size_t count) max = 0; #endif + /* + * read_word_at_a_time() below may read uninitialized bytes after the + * trailing zero and use them in comparisons. Disable this optimization + * under KMSAN to prevent false positive reports. + */ + if (IS_ENABLED(CONFIG_KMSAN)) + max = 0; + while (max >= sizeof(unsigned long)) { unsigned long c, data; From patchwork Fri Aug 26 15:07:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956200 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 5ACD8ECAAD5 for ; Fri, 26 Aug 2022 15:09:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E775B94000C; Fri, 26 Aug 2022 11:09:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DFF88940007; Fri, 26 Aug 2022 11:09:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C2BC094000C; Fri, 26 Aug 2022 11:09:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id AFDF9940007 for ; Fri, 26 Aug 2022 11:09:29 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 8148B1607BB for ; Fri, 26 Aug 2022 15:09:29 +0000 (UTC) X-FDA: 79842077658.22.3E3610F Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf21.hostedemail.com (Postfix) with ESMTP id 281B61C0011 for ; Fri, 26 Aug 2022 15:09:29 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id z6-20020a05640240c600b0043e1d52fd98so1241505edb.22 for ; Fri, 26 Aug 2022 08:09:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=1gFHCXdD1YZhZE1n87Od5kpjEzNiLhH4oD7CKEg8NyA=; b=C36uYdPsFbGg613/pEp7hUDG4/V4Hby9x4YJDjLdSHXRVADGe3qYmz+FhENc9qWMpn 1fHH+6dULxF6nztHlikbu+gl/MQWdeDCP9VB2zmHX7SxVaqPprtH8w7ACXfxBbSDIKlS kWtqNRu27MLLf6vQ4GWLQZltqrIfq9HjjMB2CrQ8XK2g1T2veDcTsdCIa2olLf0K3eB0 rg+ABbqOAL/7dhxwjAOJ4Hp69x9veOEyPpurQPf3/PZhpf9MC+hMgs6YPAy2sAPS4ix/ hldXHsSDQFNMlA9A7JgVHVRI19radrkKrNpF4RzeUo6MiKv+aKuCDRBFbaAp0IFkmn8h cu8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=1gFHCXdD1YZhZE1n87Od5kpjEzNiLhH4oD7CKEg8NyA=; b=MHNtQ7tkL9CugdtlkwJT7Y7sERN+fvMKicKgpCt6rbEakJggCUWQA5DsGZc/g3ecPS P5SbOR/q8Fr6rg8p6OlMGQCp+MAAzINnLh/9BnWXFRU9sDf6X03jmX8kNZb6bP1kvF73 RZG2aQdeVq62ndRwneIUtl/Wr1Vo8kPPZ9U/M3zc3ZKiAt/6cKIeCdfqhyTxQW3npx/U K/6WsQNBzlIdNzXUp/FQ55u0Bcg+p7UwZSmF9/5SGIvlCI/hi0lHT5P8K4WzNyq5F/ft CGy3/ChzIUWOB4AWbWNlvW48mHhUwWxgQ+DZFK96aW23O7sC6LK163POkZK6eZClSrG8 i2+Q== X-Gm-Message-State: ACgBeo0FovJTHtp8hWHiGvwdyNw5GQ4vAmoHXp7GsLUT/VZ7JBlGO51b +dBZ6MXuBp0cjQJmkau4CZsoPcZbATg= X-Google-Smtp-Source: AA6agR4dU1oVd473jtavrnrWb2PmEIjOlLC7x44i18xmbb0FTAubsZR2JzCTs0yhSsAGbXPQw2wAkr5SI/I= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a05:6402:882:b0:447:e6b9:89db with SMTP id e2-20020a056402088200b00447e6b989dbmr2901305edy.346.1661526567955; Fri, 26 Aug 2022 08:09:27 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:50 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-28-glider@google.com> Subject: [PATCH v5 27/44] crypto: kmsan: disable accelerated configs under KMSAN From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526569; a=rsa-sha256; cv=none; b=tCchzP6GGXd5xEykYvAdPjJKnj/wkxm48LUZCrotVDl8Dz4zOrXSiRDl75PIFe7tfCQVju ut3TRoV80qsCgkFEzfQ0vRYnp4JxdOMWSKx5E2oa5R65qZ493NX25q3kyllqCP1V8RhmnR SF6KcfkW9W4T3eqHRuC77OVb/frkTxA= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=C36uYdPs; spf=pass (imf21.hostedemail.com: domain of 3J-IIYwYKCC4QVSNObQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3J-IIYwYKCC4QVSNObQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526569; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=1gFHCXdD1YZhZE1n87Od5kpjEzNiLhH4oD7CKEg8NyA=; b=c3ikCrwFAGGyM3HLslyhMWYC9DkdiCt9L7cSeYHPrfKbDOd4MRRgnmm7fX216EgIrHi8l/ i4sWofzOtjggW2u74pCo9TOxCgeSCxMEmUMkoPvaJqHxDBPCcaQNwuJJWBLx18nEfzCE+Z gboZoqZGR9K6eyv6X6EFfdeZDr/Xb34= X-Rspam-User: Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=C36uYdPs; spf=pass (imf21.hostedemail.com: domain of 3J-IIYwYKCC4QVSNObQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3J-IIYwYKCC4QVSNObQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam08 X-Stat-Signature: fspjht1berm17o9qomy6gnpihxncondf X-Rspamd-Queue-Id: 281B61C0011 X-HE-Tag: 1661526568-572368 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 is unable to understand when initialized values come from assembly. Disable accelerated configs in KMSAN builds to prevent false positive reports. Signed-off-by: Alexander Potapenko --- Link: https://linux-review.googlesource.com/id/Idb2334bf3a1b68b31b399709baefaa763038cc50 --- crypto/Kconfig | 30 ++++++++++++++++++++++++++++++ drivers/net/Kconfig | 1 + 2 files changed, 31 insertions(+) diff --git a/crypto/Kconfig b/crypto/Kconfig index bb427a835e44a..182fb817ebb52 100644 --- a/crypto/Kconfig +++ b/crypto/Kconfig @@ -319,6 +319,7 @@ config CRYPTO_CURVE25519 config CRYPTO_CURVE25519_X86 tristate "x86_64 accelerated Curve25519 scalar multiplication library" depends on X86 && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_LIB_CURVE25519_GENERIC select CRYPTO_ARCH_HAVE_LIB_CURVE25519 @@ -367,11 +368,13 @@ config CRYPTO_AEGIS128 config CRYPTO_AEGIS128_SIMD bool "Support SIMD acceleration for AEGIS-128" depends on CRYPTO_AEGIS128 && ((ARM || ARM64) && KERNEL_MODE_NEON) + depends on !KMSAN # avoid false positives from assembly default y config CRYPTO_AEGIS128_AESNI_SSE2 tristate "AEGIS-128 AEAD algorithm (x86_64 AESNI+SSE2 implementation)" depends on X86 && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_AEAD select CRYPTO_SIMD help @@ -517,6 +520,7 @@ config CRYPTO_NHPOLY1305 config CRYPTO_NHPOLY1305_SSE2 tristate "NHPoly1305 hash function (x86_64 SSE2 implementation)" depends on X86 && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_NHPOLY1305 help SSE2 optimized implementation of the hash function used by the @@ -525,6 +529,7 @@ config CRYPTO_NHPOLY1305_SSE2 config CRYPTO_NHPOLY1305_AVX2 tristate "NHPoly1305 hash function (x86_64 AVX2 implementation)" depends on X86 && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_NHPOLY1305 help AVX2 optimized implementation of the hash function used by the @@ -649,6 +654,7 @@ config CRYPTO_CRC32C config CRYPTO_CRC32C_INTEL tristate "CRC32c INTEL hardware acceleration" depends on X86 + depends on !KMSAN # avoid false positives from assembly select CRYPTO_HASH help In Intel processor with SSE4.2 supported, the processor will @@ -689,6 +695,7 @@ config CRYPTO_CRC32 config CRYPTO_CRC32_PCLMUL tristate "CRC32 PCLMULQDQ hardware acceleration" depends on X86 + depends on !KMSAN # avoid false positives from assembly select CRYPTO_HASH select CRC32 help @@ -748,6 +755,7 @@ config CRYPTO_BLAKE2B config CRYPTO_BLAKE2S_X86 bool "BLAKE2s digest algorithm (x86 accelerated version)" depends on X86 && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_LIB_BLAKE2S_GENERIC select CRYPTO_ARCH_HAVE_LIB_BLAKE2S @@ -762,6 +770,7 @@ config CRYPTO_CRCT10DIF config CRYPTO_CRCT10DIF_PCLMUL tristate "CRCT10DIF PCLMULQDQ hardware acceleration" depends on X86 && 64BIT && CRC_T10DIF + depends on !KMSAN # avoid false positives from assembly select CRYPTO_HASH help For x86_64 processors with SSE4.2 and PCLMULQDQ supported, @@ -831,6 +840,7 @@ config CRYPTO_POLY1305 config CRYPTO_POLY1305_X86_64 tristate "Poly1305 authenticator algorithm (x86_64/SSE2/AVX2)" depends on X86 && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_LIB_POLY1305_GENERIC select CRYPTO_ARCH_HAVE_LIB_POLY1305 help @@ -920,6 +930,7 @@ config CRYPTO_SHA1 config CRYPTO_SHA1_SSSE3 tristate "SHA1 digest algorithm (SSSE3/AVX/AVX2/SHA-NI)" depends on X86 && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_SHA1 select CRYPTO_HASH help @@ -931,6 +942,7 @@ config CRYPTO_SHA1_SSSE3 config CRYPTO_SHA256_SSSE3 tristate "SHA256 digest algorithm (SSSE3/AVX/AVX2/SHA-NI)" depends on X86 && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_SHA256 select CRYPTO_HASH help @@ -943,6 +955,7 @@ config CRYPTO_SHA256_SSSE3 config CRYPTO_SHA512_SSSE3 tristate "SHA512 digest algorithm (SSSE3/AVX/AVX2)" depends on X86 && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_SHA512 select CRYPTO_HASH help @@ -1168,6 +1181,7 @@ config CRYPTO_WP512 config CRYPTO_GHASH_CLMUL_NI_INTEL tristate "GHASH hash function (CLMUL-NI accelerated)" depends on X86 && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_CRYPTD help This is the x86_64 CLMUL-NI accelerated implementation of @@ -1228,6 +1242,7 @@ config CRYPTO_AES_TI config CRYPTO_AES_NI_INTEL tristate "AES cipher algorithms (AES-NI)" depends on X86 + depends on !KMSAN # avoid false positives from assembly select CRYPTO_AEAD select CRYPTO_LIB_AES select CRYPTO_ALGAPI @@ -1369,6 +1384,7 @@ config CRYPTO_BLOWFISH_COMMON config CRYPTO_BLOWFISH_X86_64 tristate "Blowfish cipher algorithm (x86_64)" depends on X86 && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_SKCIPHER select CRYPTO_BLOWFISH_COMMON imply CRYPTO_CTR @@ -1399,6 +1415,7 @@ config CRYPTO_CAMELLIA config CRYPTO_CAMELLIA_X86_64 tristate "Camellia cipher algorithm (x86_64)" depends on X86 && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_SKCIPHER imply CRYPTO_CTR help @@ -1415,6 +1432,7 @@ config CRYPTO_CAMELLIA_X86_64 config CRYPTO_CAMELLIA_AESNI_AVX_X86_64 tristate "Camellia cipher algorithm (x86_64/AES-NI/AVX)" depends on X86 && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_SKCIPHER select CRYPTO_CAMELLIA_X86_64 select CRYPTO_SIMD @@ -1433,6 +1451,7 @@ config CRYPTO_CAMELLIA_AESNI_AVX_X86_64 config CRYPTO_CAMELLIA_AESNI_AVX2_X86_64 tristate "Camellia cipher algorithm (x86_64/AES-NI/AVX2)" depends on X86 && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_CAMELLIA_AESNI_AVX_X86_64 help Camellia cipher algorithm module (x86_64/AES-NI/AVX2). @@ -1478,6 +1497,7 @@ config CRYPTO_CAST5 config CRYPTO_CAST5_AVX_X86_64 tristate "CAST5 (CAST-128) cipher algorithm (x86_64/AVX)" depends on X86 && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_SKCIPHER select CRYPTO_CAST5 select CRYPTO_CAST_COMMON @@ -1501,6 +1521,7 @@ config CRYPTO_CAST6 config CRYPTO_CAST6_AVX_X86_64 tristate "CAST6 (CAST-256) cipher algorithm (x86_64/AVX)" depends on X86 && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_SKCIPHER select CRYPTO_CAST6 select CRYPTO_CAST_COMMON @@ -1534,6 +1555,7 @@ config CRYPTO_DES_SPARC64 config CRYPTO_DES3_EDE_X86_64 tristate "Triple DES EDE cipher algorithm (x86-64)" depends on X86 && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_SKCIPHER select CRYPTO_LIB_DES imply CRYPTO_CTR @@ -1604,6 +1626,7 @@ config CRYPTO_CHACHA20 config CRYPTO_CHACHA20_X86_64 tristate "ChaCha stream cipher algorithms (x86_64/SSSE3/AVX2/AVX-512VL)" depends on X86 && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_SKCIPHER select CRYPTO_LIB_CHACHA_GENERIC select CRYPTO_ARCH_HAVE_LIB_CHACHA @@ -1674,6 +1697,7 @@ config CRYPTO_SERPENT config CRYPTO_SERPENT_SSE2_X86_64 tristate "Serpent cipher algorithm (x86_64/SSE2)" depends on X86 && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_SKCIPHER select CRYPTO_SERPENT select CRYPTO_SIMD @@ -1693,6 +1717,7 @@ config CRYPTO_SERPENT_SSE2_X86_64 config CRYPTO_SERPENT_SSE2_586 tristate "Serpent cipher algorithm (i586/SSE2)" depends on X86 && !64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_SKCIPHER select CRYPTO_SERPENT select CRYPTO_SIMD @@ -1712,6 +1737,7 @@ config CRYPTO_SERPENT_SSE2_586 config CRYPTO_SERPENT_AVX_X86_64 tristate "Serpent cipher algorithm (x86_64/AVX)" depends on X86 && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_SKCIPHER select CRYPTO_SERPENT select CRYPTO_SIMD @@ -1732,6 +1758,7 @@ config CRYPTO_SERPENT_AVX_X86_64 config CRYPTO_SERPENT_AVX2_X86_64 tristate "Serpent cipher algorithm (x86_64/AVX2)" depends on X86 && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_SERPENT_AVX_X86_64 help Serpent cipher algorithm, by Anderson, Biham & Knudsen. @@ -1876,6 +1903,7 @@ config CRYPTO_TWOFISH_586 config CRYPTO_TWOFISH_X86_64 tristate "Twofish cipher algorithm (x86_64)" depends on (X86 || UML_X86) && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_ALGAPI select CRYPTO_TWOFISH_COMMON imply CRYPTO_CTR @@ -1893,6 +1921,7 @@ config CRYPTO_TWOFISH_X86_64 config CRYPTO_TWOFISH_X86_64_3WAY tristate "Twofish cipher algorithm (x86_64, 3-way parallel)" depends on X86 && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_SKCIPHER select CRYPTO_TWOFISH_COMMON select CRYPTO_TWOFISH_X86_64 @@ -1913,6 +1942,7 @@ config CRYPTO_TWOFISH_X86_64_3WAY config CRYPTO_TWOFISH_AVX_X86_64 tristate "Twofish cipher algorithm (x86_64/AVX)" depends on X86 && 64BIT + depends on !KMSAN # avoid false positives from assembly select CRYPTO_SKCIPHER select CRYPTO_SIMD select CRYPTO_TWOFISH_COMMON diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig index 94c889802566a..2aaf02bfe6f7e 100644 --- a/drivers/net/Kconfig +++ b/drivers/net/Kconfig @@ -76,6 +76,7 @@ config WIREGUARD tristate "WireGuard secure network tunnel" depends on NET && INET depends on IPV6 || !IPV6 + depends on !KMSAN # KMSAN doesn't support the crypto configs below select NET_UDP_TUNNEL select DST_CACHE select CRYPTO From patchwork Fri Aug 26 15:07:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956201 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 E9809ECAAA3 for ; Fri, 26 Aug 2022 15:09:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 80EEF940009; Fri, 26 Aug 2022 11:09:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7976D940007; Fri, 26 Aug 2022 11:09:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5EC36940009; Fri, 26 Aug 2022 11:09:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 4D01A940007 for ; Fri, 26 Aug 2022 11:09:32 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 340F740E3E for ; Fri, 26 Aug 2022 15:09:32 +0000 (UTC) X-FDA: 79842077784.04.DF5C1D8 Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) by imf15.hostedemail.com (Postfix) with ESMTP id D7778A0027 for ; Fri, 26 Aug 2022 15:09:31 +0000 (UTC) Received: by mail-ej1-f74.google.com with SMTP id mp41-20020a1709071b2900b0073d84b11981so731143ejc.23 for ; Fri, 26 Aug 2022 08:09:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=+5QrT5xfdlDeiLaqkJXiB84dUzsqj5PxvneJHk/QPSg=; b=psU325BcC9yCrTgirTaG4c6+Xtt5yEuOoqBDgzPyoh8Hu/P8wcF78qEe5Fv8Uq6mxM 5keARp+YcPeQ8kw2cba6I1H8TcR6qm0KRbaLOkU+IfLk8PtK83FSP/gowyt+6cMWfvxZ fE9laQ1Snqp2aoMqiOY1YPig7tDAMkED/KaWIJ682ZVuc6+uGvL4n3H/HrRhkL0dFAfP qPPg29aXbvpDPvKUJJFkWAZ+F418Tz4/xxZqjZPJP6QnWJRDQgXx2bOBAWB+HjiGP74T HU0Ev+YyOY48tZD9hgwrs4RkUWaxRCmaLSAihTVflh5YMg0/YJxLiIdMMS9B8R/uWWUE aznw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=+5QrT5xfdlDeiLaqkJXiB84dUzsqj5PxvneJHk/QPSg=; b=YjORjUZC70Om+LbAzwSMBAo6fM/HqV8RcIsEoac3ghw0RyBoKAb0+4mLrnXZRUokyU UnDXsSgU2WGej23MEgtxSwbvhSSjC0Tf4gqulvIezq5M6HZQyF530DODmCaLRg6R2KTN LGWhI2ldDVi+VlgCIAzCy+VJMNgnsAf5WXZcEDqe/u0gS4wqaXIl/akBQom5kIXMCuRp /EWJMnIzMtf4nt0YMoDWW4dmBTL3tB7sJ5ou8I+JSCOgimoxolW0TCzB6PQaAIOr8h26 m7mtD+VdzS8QxTNvJnPNN3/+HkMW3+TVtVMnE/7/bE1BzJeQS/KCnYOjbDBzc/o3QmxM LqoQ== X-Gm-Message-State: ACgBeo1AS83zG35GuRmsXxEapeJIKE3soTYv/EWwAycoXofvXUFQwBN5 yNR/7/Bd80yrdVw287BB9tgDu/FQSbM= X-Google-Smtp-Source: AA6agR64mNuFawO1j56e/34eHqe4IGdV2CngnQTQuYSPV0pxJ0eEBVtcq2c/F1Fd/CUKRPUFcQzSoaSzSas= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a05:6402:496:b0:443:a5f5:d3b with SMTP id k22-20020a056402049600b00443a5f50d3bmr7388597edv.331.1661526570674; Fri, 26 Aug 2022 08:09:30 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:51 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-29-glider@google.com> Subject: [PATCH v5 28/44] kmsan: disable physical page merging in biovec From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526571; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=+5QrT5xfdlDeiLaqkJXiB84dUzsqj5PxvneJHk/QPSg=; b=N3pPIpEudr1cLN7klLnozmAsqXfN6wenIQQRUcfsgQHsN+q3tuXpZEVpBjNORfuvQGNg9K fgw135MS9HwQnwCipTF2TAVNfvcuaor58K6jTx/f6PGBSx6HXHn3WKkO6P7YBTXU2EfJ0x qC2Y1t++GkTo78Ugo571+B12Ow7NUTI= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=psU325Bc; spf=pass (imf15.hostedemail.com: domain of 3KuIIYwYKCDETYVQReTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3KuIIYwYKCDETYVQReTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526571; a=rsa-sha256; cv=none; b=iedK29VMn7NHAv4RhxN5TycvZVTsDz4HrI3vY8WmbYw2H5pVCSesEEhO1DAUEJ/dLe3vO/ NSPdLYOrx8afOTXtKan9Ww5VjqaCQxKTGMdWJWL3yhMVohKEtmknMJhot9Ece7Dn5GFJ67 AjDJ5uCPDiz9kO9eburX646aDPtrlmw= X-Rspam-User: Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=psU325Bc; spf=pass (imf15.hostedemail.com: domain of 3KuIIYwYKCDETYVQReTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3KuIIYwYKCDETYVQReTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam05 X-Stat-Signature: 1mebhaex7hqymbbbaxgyiauwkjpy7cmd X-Rspamd-Queue-Id: D7778A0027 X-HE-Tag: 1661526571-644535 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 metadata for adjacent physical pages may not be adjacent, therefore accessing such pages together may lead to metadata corruption. We disable merging pages in biovec to prevent such corruptions. Signed-off-by: Alexander Potapenko --- Link: https://linux-review.googlesource.com/id/Iece16041be5ee47904fbc98121b105e5be5fea5c --- block/blk.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/block/blk.h b/block/blk.h index d7142c4d2fefb..af02b93c1dba5 100644 --- a/block/blk.h +++ b/block/blk.h @@ -88,6 +88,13 @@ static inline bool biovec_phys_mergeable(struct request_queue *q, phys_addr_t addr1 = page_to_phys(vec1->bv_page) + vec1->bv_offset; phys_addr_t addr2 = page_to_phys(vec2->bv_page) + vec2->bv_offset; + /* + * Merging adjacent physical pages may not work correctly under KMSAN + * if their metadata pages aren't adjacent. Just disable merging. + */ + if (IS_ENABLED(CONFIG_KMSAN)) + return false; + if (addr1 + vec1->bv_len != addr2) return false; if (xen_domain() && !xen_biovec_phys_mergeable(vec1, vec2->bv_page)) From patchwork Fri Aug 26 15:07:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956202 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 C8F7DECAAA3 for ; Fri, 26 Aug 2022 15:09:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6AE9394000D; Fri, 26 Aug 2022 11:09:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6378A940007; Fri, 26 Aug 2022 11:09:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B17394000D; Fri, 26 Aug 2022 11:09:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 34CFB940007 for ; Fri, 26 Aug 2022 11:09:35 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 19A0740ECF for ; Fri, 26 Aug 2022 15:09:35 +0000 (UTC) X-FDA: 79842077910.26.5C46DA6 Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf11.hostedemail.com (Postfix) with ESMTP id B49494001A for ; Fri, 26 Aug 2022 15:09:34 +0000 (UTC) Received: by mail-ed1-f73.google.com with SMTP id w17-20020a056402269100b0043da2189b71so1226897edd.6 for ; Fri, 26 Aug 2022 08:09:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=ThlNJAw9Kex8KZrskRhHj0vt2sXcunlblCUmHxCmdH0=; b=qQ2U4pH+aEHV3VhL/BG61D7fD+fTTCboEipAha6RdIlQZwkpHT+D6S+zluEN3z1B+q p6KSrVqARRa5621nYeg6EZduZRldgx+r7wiEO2xx4Q1xdyk3NHKRP+n4BtOU+EkTRiBv n9pYohc41L9MNpZ4FFw1iKeBekfif+vSNYJKYp9IPK+WdwoL5Xq+pRz96l2kWYetAuz9 ItAJhGyVh4AlEUo/NPut7UEgT36U97b+onPRJ1+xwb33JHHm9d1XBCaLKaZdeHB78F1o GDrzwrmR2heq2RM/Ijy6NFuNEoHEGLQn9rAcq11k6RQRmGERKya5Rzxx6la0Wnscwu1/ yKKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=ThlNJAw9Kex8KZrskRhHj0vt2sXcunlblCUmHxCmdH0=; b=toAo0WFukpe3h8Frvir0lf3AyCrXntuIteLFG4DQ1lpbPXJS0vVIpKPjh1UA398IEV zQmG4rQaIaOfznR3AOva5O+Kkal0j9e6xXHrKxd35RCLFPH6WlfdZc2Lp1wn1aRhgPyo FTpQLHD+yXVZEXD2OcliKtI681D2i/roJIjXZj/ESSgvPXOOtASdOTQ6YYlgw5d0AGZ5 zVJEQ3S0rX3qeqpHNE4chFxWxjYChFnCdNtkiLuT874otST7BRghH+oqiHw2wWfp8wy4 w1eamV+6CEdw/gi6x21X4rF0B5exp//QRTbbru2UhmS1VCG9WpZJavRm+3TChxS+Gy4q bT3w== X-Gm-Message-State: ACgBeo1MdVgz4/YHA8f0ZpsKyhZ6RbzYddFamF8P47E8+7GWoNm7yXYD yo9x9av2KeDxp9hyjHFazUDgnilBMSg= X-Google-Smtp-Source: AA6agR7P1+orY1RNadpoV0ZNi4BGK9MvXVRSI3uWf43JN88nANc9J/bmowMSnW9dE0hDywQIYKYvs/S52Ck= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a17:906:9b16:b0:73d:af6f:746e with SMTP id eo22-20020a1709069b1600b0073daf6f746emr5932849ejc.32.1661526573448; Fri, 26 Aug 2022 08:09:33 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:52 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-30-glider@google.com> Subject: [PATCH v5 29/44] block: kmsan: skip bio block merging logic for KMSAN From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Biggers ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526574; a=rsa-sha256; cv=none; b=DFt+DJZn5FSMrrvt5VCknUtmHnPSOMf4Zd1zirAzbcAqPQyc4ACXLeXeee6jHeCEKBSRve PqmIsL4jSZfqjhiYKHpumMfLn05soYtGlXXOJatntYzpjd/QjhQj6t74CK4znexgkjKxsT wS4r2trOLTEpHpjlG7r9PeXDC9bNmnE= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=qQ2U4pH+; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3LeIIYwYKCDQWbYTUhWeeWbU.SecbYdkn-ccalQSa.ehW@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3LeIIYwYKCDQWbYTUhWeeWbU.SecbYdkn-ccalQSa.ehW@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526574; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ThlNJAw9Kex8KZrskRhHj0vt2sXcunlblCUmHxCmdH0=; b=g5uGvQYtYGV7k4pXqBX+kiTMSGxDQmwxiiOsOeK/RArmHz7RP0ZzzQ8C6Apx/5ZzXVZ+PM Jy1e3BKsZG+Aym/II4mtsYI8IzTtSRtdjZAp7KAg3d7a7QE9hbZPG23ytH568berjQZA2v KSkIhVo9iwhz1jJd0yMWzWOb25jG3o4= Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=qQ2U4pH+; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3LeIIYwYKCDQWbYTUhWeeWbU.SecbYdkn-ccalQSa.ehW@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3LeIIYwYKCDQWbYTUhWeeWbU.SecbYdkn-ccalQSa.ehW@flex--glider.bounces.google.com X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 8119rm4a9d8hnp4s6p4rwgjxkjt99jha X-Rspamd-Queue-Id: B49494001A X-HE-Tag: 1661526574-601820 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 doesn't allow treating adjacent memory pages as such, if they were allocated by different alloc_pages() calls. The block layer however does so: adjacent pages end up being used together. To prevent this, make page_is_mergeable() return false under KMSAN. Suggested-by: Eric Biggers Signed-off-by: Alexander Potapenko --- v4: -- swap block: and kmsan: in the subject v5: -- address Marco Elver's comments Link: https://linux-review.googlesource.com/id/Ie29cc2464c70032347c32ab2a22e1e7a0b37b905 --- block/bio.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/block/bio.c b/block/bio.c index 3d3a2678fea25..106ef14f28c2a 100644 --- a/block/bio.c +++ b/block/bio.c @@ -869,6 +869,8 @@ static inline bool page_is_mergeable(const struct bio_vec *bv, *same_page = ((vec_end_addr & PAGE_MASK) == page_addr); if (*same_page) return true; + else if (IS_ENABLED(CONFIG_KMSAN)) + return false; return (bv->bv_page + bv_end / PAGE_SIZE) == (page + off / PAGE_SIZE); } From patchwork Fri Aug 26 15:07:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956203 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 B9533ECAAD4 for ; Fri, 26 Aug 2022 15:09:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 54FD894000A; Fri, 26 Aug 2022 11:09:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4B12B940007; Fri, 26 Aug 2022 11:09:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3052994000E; Fri, 26 Aug 2022 11:09:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 1D575940007 for ; Fri, 26 Aug 2022 11:09:38 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id D6F8E80E37 for ; Fri, 26 Aug 2022 15:09:37 +0000 (UTC) X-FDA: 79842077994.01.079D194 Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf21.hostedemail.com (Postfix) with ESMTP id 47C9F1C001B for ; Fri, 26 Aug 2022 15:09:37 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id z6-20020a05640240c600b0043e1d52fd98so1241700edb.22 for ; Fri, 26 Aug 2022 08:09:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=RpdJ5KT5GAuxqWidUZuS2M2CiPNp+gZwFhAUBa4xHko=; b=TJz1UTJBESEdKuAr0m8ClDSUAjamVWg2DEv+2WQqCh6Zcxv2oRD55g+O0oye9l40zE VbOvZ3R60SbIN++3IKS2yh90SOwgtqzaCgsQtPATI2d4WJvG5qImDFrX2yJUjrWx8T39 MQQKFjvZ3fA1TYUVsHaA5FR0hdMwNCkK7TJVCIsBqtjkpd/DAEBvNeqI5bfg6Gepswe+ l1jCxIh1lrZEBivplg5yDUBpa6io4UGCJ9ztmW+CUFCm1dTP8QhVkTov1blbHQ6U4xC3 IWO5hCXy6iolpqSQIc2bH9AGoDcxJQSD0LkhoSBkctagTpdhEEFLo7Y5mpIRWVWOlKNE COTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=RpdJ5KT5GAuxqWidUZuS2M2CiPNp+gZwFhAUBa4xHko=; b=0l/nwL+deL6D0RPFyVtsIVb3sti+1mYrGGr6m28UoUECOT1H2l+YRQOGIHUOTXyP6E ZxfgczQFaqTd+7i/5VF+nCe9Fqpr1bMsvuf3kL4u9YZFT/X3cWqnDgZOo1x3JUGwFR1v 0mGJgwcAla6DeTWJev8qh075AZ3Af5dmgxHxNkEbMIsx5WO0zpgUCPSBNpyb/zVM7Lu6 pJUx+Xg1gIjOZsaY0exp4gJXfRbIA5/7rmh0hkfGfN51//M8LZeO8Aj5miNjNPUSkuK3 8XC/D20Bb8Pq2wv3fTib2lcaKTGHMTn5M9hZdmsEty9+azj4/xb2E6miZwNK1AIA2ReZ YuFg== X-Gm-Message-State: ACgBeo25X+HBiD5WIsE8ybwatiZXnXRl5m4sQuXTkjLd0k1N66+d8Zlz buL1XsNsOTCpDgCpnlmnVLnOGDrFeeo= X-Google-Smtp-Source: AA6agR5j/r012ARsOS/cLTFyia8pItWWXJH9zXKKE4ubXclVuI6mHNquDQgDo/C5PUvhnsbEwfNt5cuHLt4= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a17:907:2173:b0:73d:c95d:1179 with SMTP id rl19-20020a170907217300b0073dc95d1179mr5669815ejb.89.1661526576513; Fri, 26 Aug 2022 08:09:36 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:53 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-31-glider@google.com> Subject: [PATCH v5 30/44] kcov: kmsan: unpoison area->list in kcov_remote_area_put() From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526577; a=rsa-sha256; cv=none; b=mOW4Y/kvS8D04z8RAqH6VS3smfYaTnchBD/DgcXZEWmkJ7ZllP1N6lwx2kIm/g+ZlB8Q8N VP3AjZ0s29YUhXfGR8hU3c5VBRwVk/SlNorWJJ9ucpF0KrmC8+ZgHAwp0rc2Az4bWJRent 7r++6biJ+SiKaxtTE23btm7bv+JvxtM= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=TJz1UTJB; spf=pass (imf21.hostedemail.com: domain of 3MOIIYwYKCDcZebWXkZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3MOIIYwYKCDcZebWXkZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526577; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=RpdJ5KT5GAuxqWidUZuS2M2CiPNp+gZwFhAUBa4xHko=; b=NwFrZKS6cjGO48xQ72RgUu/wxYhWvCyjW0QtFT2j7JgJdSKMjbNQXnDj3nA6vaNedm4UFt PAUImdeWBtTQLkj7hjl4nnOAZxTGahhXkAKwJHSL3pnTglpRzpizCjrt5xUtbwCTNfj4nj C1NSYhjD1OebdkqAYD75YVD827vJChQ= X-Rspam-User: Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=TJz1UTJB; spf=pass (imf21.hostedemail.com: domain of 3MOIIYwYKCDcZebWXkZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3MOIIYwYKCDcZebWXkZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam08 X-Stat-Signature: kxtee9oayrjf4hj3rddy7cx4ad7q463o X-Rspamd-Queue-Id: 47C9F1C001B X-HE-Tag: 1661526577-975384 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 does not instrument kernel/kcov.c for performance reasons (with CONFIG_KCOV=y virtually every place in the kernel invokes kcov instrumentation). Therefore the tool may miss writes from kcov.c that initialize memory. When CONFIG_DEBUG_LIST is enabled, list pointers from kernel/kcov.c are passed to instrumented helpers in lib/list_debug.c, resulting in false positives. To work around these reports, we unpoison the contents of area->list after initializing it. Signed-off-by: Alexander Potapenko --- v4: -- change sizeof(type) to sizeof(*ptr) -- swap kcov: and kmsan: in the subject Link: https://linux-review.googlesource.com/id/Ie17f2ee47a7af58f5cdf716d585ebf0769348a5a --- kernel/kcov.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/kernel/kcov.c b/kernel/kcov.c index e19c84b02452e..e5cd09fd8a050 100644 --- a/kernel/kcov.c +++ b/kernel/kcov.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -152,6 +153,12 @@ static void kcov_remote_area_put(struct kcov_remote_area *area, INIT_LIST_HEAD(&area->list); area->size = size; list_add(&area->list, &kcov_remote_areas); + /* + * KMSAN doesn't instrument this file, so it may not know area->list + * is initialized. Unpoison it explicitly to avoid reports in + * kcov_remote_area_get(). + */ + kmsan_unpoison_memory(&area->list, sizeof(area->list)); } static notrace bool check_kcov_mode(enum kcov_mode needed_mode, struct task_struct *t) From patchwork Fri Aug 26 15:07:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956204 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 46637ECAAD4 for ; Fri, 26 Aug 2022 15:09:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DB82194000C; Fri, 26 Aug 2022 11:09:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D3C72940007; Fri, 26 Aug 2022 11:09:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B68D494000C; Fri, 26 Aug 2022 11:09:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id A4BDC940007 for ; Fri, 26 Aug 2022 11:09:40 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 83138405A0 for ; Fri, 26 Aug 2022 15:09:40 +0000 (UTC) X-FDA: 79842078120.09.A2FE24F Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf30.hostedemail.com (Postfix) with ESMTP id 4523980024 for ; Fri, 26 Aug 2022 15:09:40 +0000 (UTC) Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-340a4dcb403so8026067b3.22 for ; Fri, 26 Aug 2022 08:09:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=l4zm1sbHMytNqYdogbjGF6C06dm3yrxkJ0CqfKBvE4Y=; b=kOld3GJAUrMocd7PaWFDM8/h7CQ5GhgeZ5vgr609uraCY0vVS54oj+Y6R1RPWTH0by Bq5BNkaD35EK0na6N/L6VeFnDv7KaDTKmgH3r12GWsfNfB14gNvqr5BIJupwb+18QZwN EpJnwctEvW1mQUg89fj3mc+MvJbBuJ+7CVC7Ke4U8VHWQC9tI9ma3K6ekZgZjWQ54T49 jfxIHJNRWKeLLjMm2ThoMvBFZ4QwkjUTc4z2kPoHnmbFzmrfDXm4QKlvn1DyC5ty9ZjW KFjhK6XLeZbpXZp2sTAzuIjtMg/FCCX5xS76W6rdKcowYCqow2Y6Z86qCxoGhCl8H6pW cgBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=l4zm1sbHMytNqYdogbjGF6C06dm3yrxkJ0CqfKBvE4Y=; b=D0E2LCnpGszcgENNycSG2jxVxgt7Sy3JKhBwO2rULB3iIiR+vS62Zh52NwC9NljKuB +eYGUEB2GV9RYL2K8ysZ8BNnK4KFxBL5gWJ64baazBDegW+oqBiQ3SVzDus8YOd4aNaJ A6uZMgADmBmxLrKvuwox6027yIiZHP1CfS4nNMJtprhKkqwasgt0vhRvQ7G6Q691yHg0 r3LAR/VJtkIBIxo7RCwrCvjMDpGIX0NYPHnWNsqsA5ZfinshVu/tx3hf+zoJ38XMJoCB YG6oWY93NKGPAcdn3IIP4rAXr84gjvZGxMmFES2rXeawWZbKG22YBjpFo5BeX9v4V9O/ ybDw== X-Gm-Message-State: ACgBeo3wGH2yaOsN/5Xb62FtgklD7D3usqu9rjctNhiURvCV1v1N3Su0 Nqt7Yt/uPPpZQ6qYsesBenqtBKRieAQ= X-Google-Smtp-Source: AA6agR5iGmN+D99SNwa8hGxZHvyED7rP3I7yucWmOXY/88v8RIwEBSFuEsCfj86kvBE/XBr/3trnlWmswJk= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a81:6e06:0:b0:33b:4e1:dd6e with SMTP id j6-20020a816e06000000b0033b04e1dd6emr129882ywc.212.1661526579691; Fri, 26 Aug 2022 08:09:39 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:54 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-32-glider@google.com> Subject: [PATCH v5 31/44] security: kmsan: fix interoperability with auto-initialization From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526580; a=rsa-sha256; cv=none; b=r9l3Xcd+Mkr/XX1tJE88loj2sDd2uj/EbV7eqCXDMZtN8j63+KXVm86rXEn6jblgbquMTI ec4jsROPmF0YoPyW52xv/M9FBstWCJWrnAz4XFKBh3lbWwlJ952BofhQWmtZ6qne8lj4qW yeEYTu21+rVN4JXOAikrrsx33pZaU04= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=kOld3GJA; spf=pass (imf30.hostedemail.com: domain of 3M-IIYwYKCDocheZanckkcha.Ykihejqt-iigrWYg.knc@flex--glider.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3M-IIYwYKCDocheZanckkcha.Ykihejqt-iigrWYg.knc@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526580; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=l4zm1sbHMytNqYdogbjGF6C06dm3yrxkJ0CqfKBvE4Y=; b=uhPIGzMg6iF1e28frZZY2P1CYRxbNIyFevwmKZf/MMUOz2RMd7p2jvdrr2Mbk1oqBNxz1V YqNOBwn1L0sI0/Uiya4pGAU7fT7Z58oB9G+Hsmd4xPuAKpV8M4IcEHPTL3CDgPNq34mebU NzIZCkF60YkylynOvlHxXLBwYNEUrlo= X-Stat-Signature: yw41kbqwnq4hrqzfx4jzbaq4itm98drk X-Rspamd-Queue-Id: 4523980024 Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=kOld3GJA; spf=pass (imf30.hostedemail.com: domain of 3M-IIYwYKCDocheZanckkcha.Ykihejqt-iigrWYg.knc@flex--glider.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3M-IIYwYKCDocheZanckkcha.Ykihejqt-iigrWYg.knc@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspam-User: X-Rspamd-Server: rspam02 X-HE-Tag: 1661526580-783466 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: Heap and stack initialization is great, but not when we are trying uses of uninitialized memory. When the kernel is built with KMSAN, having kernel memory initialization enabled may introduce false negatives. We disable CONFIG_INIT_STACK_ALL_PATTERN and CONFIG_INIT_STACK_ALL_ZERO under CONFIG_KMSAN, making it impossible to auto-initialize stack variables in KMSAN builds. We also disable CONFIG_INIT_ON_ALLOC_DEFAULT_ON and CONFIG_INIT_ON_FREE_DEFAULT_ON to prevent accidental use of heap auto-initialization. We however still let the users enable heap auto-initialization at boot-time (by setting init_on_alloc=1 or init_on_free=1), in which case a warning is printed. Signed-off-by: Alexander Potapenko --- Link: https://linux-review.googlesource.com/id/I86608dd867018683a14ae1870f1928ad925f42e9 --- mm/page_alloc.c | 4 ++++ security/Kconfig.hardening | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index b28093e3bb42a..e5eed276ee41d 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -936,6 +936,10 @@ void init_mem_debugging_and_hardening(void) else static_branch_disable(&init_on_free); + if (IS_ENABLED(CONFIG_KMSAN) && + (_init_on_alloc_enabled_early || _init_on_free_enabled_early)) + pr_info("mem auto-init: please make sure init_on_alloc and init_on_free are disabled when running KMSAN\n"); + #ifdef CONFIG_DEBUG_PAGEALLOC if (!debug_pagealloc_enabled()) return; diff --git a/security/Kconfig.hardening b/security/Kconfig.hardening index bd2aabb2c60f9..2739a6776454e 100644 --- a/security/Kconfig.hardening +++ b/security/Kconfig.hardening @@ -106,6 +106,7 @@ choice config INIT_STACK_ALL_PATTERN bool "pattern-init everything (strongest)" depends on CC_HAS_AUTO_VAR_INIT_PATTERN + depends on !KMSAN help Initializes everything on the stack (including padding) with a specific debug value. This is intended to eliminate @@ -124,6 +125,7 @@ choice config INIT_STACK_ALL_ZERO bool "zero-init everything (strongest and safest)" depends on CC_HAS_AUTO_VAR_INIT_ZERO + depends on !KMSAN help Initializes everything on the stack (including padding) with a zero value. This is intended to eliminate all @@ -218,6 +220,7 @@ config STACKLEAK_RUNTIME_DISABLE config INIT_ON_ALLOC_DEFAULT_ON bool "Enable heap memory zeroing on allocation by default" + depends on !KMSAN help This has the effect of setting "init_on_alloc=1" on the kernel command line. This can be disabled with "init_on_alloc=0". @@ -230,6 +233,7 @@ config INIT_ON_ALLOC_DEFAULT_ON config INIT_ON_FREE_DEFAULT_ON bool "Enable heap memory zeroing on free by default" + depends on !KMSAN help This has the effect of setting "init_on_free=1" on the kernel command line. This can be disabled with "init_on_free=0". From patchwork Fri Aug 26 15:07:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956205 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 F31FCECAAA3 for ; Fri, 26 Aug 2022 15:09:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9477394000B; Fri, 26 Aug 2022 11:09:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8CFC3940007; Fri, 26 Aug 2022 11:09:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7223594000B; Fri, 26 Aug 2022 11:09:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 615F2940007 for ; Fri, 26 Aug 2022 11:09:44 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 3E41140E67 for ; Fri, 26 Aug 2022 15:09:44 +0000 (UTC) X-FDA: 79842078288.30.8C8814A Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf28.hostedemail.com (Postfix) with ESMTP id DE5F3C0023 for ; Fri, 26 Aug 2022 15:09:43 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id i6-20020a05640242c600b00447c00a776aso1249872edc.20 for ; Fri, 26 Aug 2022 08:09:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=wnBRr6RijpcrZwDY4IvPeedZIJ4FbGOivCI4jGGTq68=; b=Q2dLT3h7TWqrdhr9cKJaZHS7iaLiqYTtydJ8WFBafxLMcKO+tWNvB/c1oHlNA44pJ7 /OXz8Sr59WCHZVNiYq5ZO1SudMDbRYsAoPTTGJZwqRehknAE2rlYQmFGjPCW+F59/jA8 4CGBdHbeqGVMdW2YUIUBHbdr0K3ckRBLlyr7LkOy1/yf2HWblMEVwWDcc4r9h7xID/vX XMqQsXajhwumZ46fKGFKaccQ9fQsrPuYa0ruT4rNz0CxhrMmw4c71iIpLPuYx4UZ7eQv mqdpWuXSHPVCrwsBJhHLWeKUt/81aDaMfsn199V9hC6WQQ+JcGqn5dar3vI5lIw7PM1Z VyHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=wnBRr6RijpcrZwDY4IvPeedZIJ4FbGOivCI4jGGTq68=; b=gyIAzMoIqqAzT0mmJyLhhZq+yfdCK91OxSGZYACIx0AmiPn4xRpFF3LY01kNIiwujY rzSdAmVQmcEL3xxg2K5RpnpqqFR7TMn6t8qMmWrYggtdWed5g5soDMUMDkmSSWR9sh62 9/OSnHt9xJAEnrVTDZmRIGcczXZyhc7gam/O7+cP9jwM6p4EPbeyKJdPDR/xwP6+ab8e aSxCmB5UmdgQYNGX2BPJ8JCPezAW/PVKq0SCaT4zBghDrdJZzWB7yMJWv4ZeLjBLHx90 Ya0QUxMQEeKntRMOl7zSLkBA5oFlyYqwxPQ8Ei3pIqdCxO3oIsPpnT2SuCn2Zgx0rM18 zfvw== X-Gm-Message-State: ACgBeo3gFTKOMnWZEr8Tv7CywplE4ZTTdG8IsVwHqb0PSGp1uk4e76fp sgJaSih2HSs/syQWUPn/iBKyRPKW8pk= X-Google-Smtp-Source: AA6agR5Cviceae2TWrL6o3zdALqTpxVHsH33a+v7kdsRPAsBV/Ys+W35Cv65k+Ev6zMJg2mViJephdzVroE= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a05:6402:268d:b0:43d:b9d0:9efc with SMTP id w13-20020a056402268d00b0043db9d09efcmr7340129edd.92.1661526582428; Fri, 26 Aug 2022 08:09:42 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:55 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-33-glider@google.com> Subject: [PATCH v5 32/44] objtool: kmsan: list KMSAN API functions as uaccess-safe From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526583; a=rsa-sha256; cv=none; b=a0MzxTd/Kt+hzaKFOps7RP9QFoeM1AydkITFWDLmggAgCuxj6W11gewiB9rfF+wJPcBzHu /1SLMrQfWYvX9hlWk7DEgGJ55wFwQgR6lnNXsB3z2CAMki6NRMvhEkDWK9c7+l9S5Uq3hz 1grA13x/3NlQSyCngUQwpMRkLgOWqCU= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Q2dLT3h7; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf28.hostedemail.com: domain of 3NuIIYwYKCD0fkhcdqfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3NuIIYwYKCD0fkhcdqfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526583; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=wnBRr6RijpcrZwDY4IvPeedZIJ4FbGOivCI4jGGTq68=; b=aGu/VxZJcn3zIZW5VnrDmvoFofcHhioT/HQRsFvH84kooJ90k9Wg7MKrNha1F5oh/z94MO u3hJvXqnXM7whLpiEcXyIjMS9xC11todVgSlFGj8C1w3lPANRBJZzdG598v2Zzx0Q/E7co ie4Wo8RS2X5xolU7KJbEA2SbfeVTHvk= X-Rspamd-Queue-Id: DE5F3C0023 Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Q2dLT3h7; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf28.hostedemail.com: domain of 3NuIIYwYKCD0fkhcdqfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3NuIIYwYKCD0fkhcdqfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--glider.bounces.google.com X-Rspamd-Server: rspam12 X-Rspam-User: X-Stat-Signature: 5coznnd1rnc356kdh7zhx6i9dcmg3sb9 X-HE-Tag: 1661526583-579562 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 inserts API function calls in a lot of places (function entries and exits, local variables, memory accesses), so they may get called from the uaccess regions as well. KMSAN API functions are used to update the metadata (shadow/origin pages) for kernel memory accesses. The metadata pages for kernel pointers are also located in the kernel memory, so touching them is not a problem. For userspace pointers, no metadata is allocated. If an API function is supposed to read or modify the metadata, it does so for kernel pointers and ignores userspace pointers. If an API function is supposed to return a pair of metadata pointers for the instrumentation to use (like all __msan_metadata_ptr_for_TYPE_SIZE() functions do), it returns the allocated metadata for kernel pointers and special dummy buffers residing in the kernel memory for userspace pointers. As a result, none of KMSAN API functions perform userspace accesses, but since they might be called from UACCESS regions they use user_access_save/restore(). Signed-off-by: Alexander Potapenko --- v3: -- updated the patch description v4: -- add kmsan_unpoison_entry_regs() Link: https://linux-review.googlesource.com/id/I242bc9816273fecad4ea3d977393784396bb3c35 --- tools/objtool/check.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 91678252a9b67..577dfdca635e8 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -1060,6 +1060,26 @@ static const char *uaccess_safe_builtin[] = { "__sanitizer_cov_trace_cmp4", "__sanitizer_cov_trace_cmp8", "__sanitizer_cov_trace_switch", + /* KMSAN */ + "kmsan_copy_to_user", + "kmsan_report", + "kmsan_unpoison_entry_regs", + "kmsan_unpoison_memory", + "__msan_chain_origin", + "__msan_get_context_state", + "__msan_instrument_asm_store", + "__msan_metadata_ptr_for_load_1", + "__msan_metadata_ptr_for_load_2", + "__msan_metadata_ptr_for_load_4", + "__msan_metadata_ptr_for_load_8", + "__msan_metadata_ptr_for_load_n", + "__msan_metadata_ptr_for_store_1", + "__msan_metadata_ptr_for_store_2", + "__msan_metadata_ptr_for_store_4", + "__msan_metadata_ptr_for_store_8", + "__msan_metadata_ptr_for_store_n", + "__msan_poison_alloca", + "__msan_warning", /* UBSAN */ "ubsan_type_mismatch_common", "__ubsan_handle_type_mismatch", From patchwork Fri Aug 26 15:07:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956206 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 9E5A0ECAAA3 for ; Fri, 26 Aug 2022 15:09:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3A22794000E; Fri, 26 Aug 2022 11:09:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 32B8F940007; Fri, 26 Aug 2022 11:09:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 17E3A94000E; Fri, 26 Aug 2022 11:09:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 07A45940007 for ; Fri, 26 Aug 2022 11:09:47 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id DDE011A0EBA for ; Fri, 26 Aug 2022 15:09:46 +0000 (UTC) X-FDA: 79842078372.29.CAC5E0F Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) by imf26.hostedemail.com (Postfix) with ESMTP id 90E4E140038 for ; Fri, 26 Aug 2022 15:09:46 +0000 (UTC) Received: by mail-ej1-f74.google.com with SMTP id sb14-20020a1709076d8e00b0073d48a10e10so726503ejc.16 for ; Fri, 26 Aug 2022 08:09:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=TL1IO7xzXsHa87B/8VTzh6zaFcPyOOUQBNZupLxKBzY=; b=HmM0VO6gyAfILflJdnMgQCrJGnTi5XVkH3YSfQa6Ep7rg4UjuRksDwsSKfdFWywrKQ X82KrgKGqPOH7W1Y085jaYB8hH48o1UO09SOdX25urL9IS+XrJIM0Ssl/7EKgVgUGuxb suAFkuoPLhsCY3yiCTAbiGImLbwkQ5FdkgDFrYP2s7UE16Wpzs+b87M7ywZwNSRcm9YT cGnqAcNCOOLNxnkJmTHzwJPrKl7dQaDEoIofp21fLLf8UCuU8TSnY933jziAvQ7zwjo3 /ax/hZi6c/lKBcQc7+qabk0byKdenq8IrwbzMmcRBwdyqFxWLtVcbbN3QqtnjPKNbk+n t0fA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=TL1IO7xzXsHa87B/8VTzh6zaFcPyOOUQBNZupLxKBzY=; b=mFnHjkHYlN060+5Ct+pdftRP21pdooLVqEuQZjXLJBJdKhg2/ySzjTwMVwFFSKoZ4p UYTAqYX1hrXzO4fctCKG3OJKf/67Wv054HPk5I466sCzsCC6HwJVKqVR/OfxqSm48OeH 2EZiGa3LNi+23ctwq+fN5ypfXB7Bsk0XyRxtJYyAvUt/qrMLwvM4NiFUebmcfxvBfAVW wc0IzFNo6LpPaNJlvx6S1UHQzet7Yk2riJy/IRbKT5Q2SKBjDJYBQhIn1rdWKINS40wH 0nE1arCEexD042UR5vsaxfomYMqdXdbLUlOSKqPm7jYN6fvaStx3FOV1HL7/T5XCf1j+ dMhA== X-Gm-Message-State: ACgBeo1OpkkjmNv86BBFlJfBnJYoE0EAWlEkQgEMqMHGgoG49qSRu9el jb08EJgjrcNdkeYCXof+h0rKgslJMj8= X-Google-Smtp-Source: AA6agR76x5AzTzEwn7EdrRuhdUjJW6rIUhqvD9uYbbBW+SigP/bQyvCEvWgW5kLb9e6YBzYDFPgy1GTjMDc= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a50:fe91:0:b0:43d:c97d:1b93 with SMTP id d17-20020a50fe91000000b0043dc97d1b93mr7390732edt.67.1661526585272; Fri, 26 Aug 2022 08:09:45 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:56 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-34-glider@google.com> Subject: [PATCH v5 33/44] x86: kmsan: disable instrumentation of unsupported code From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526586; a=rsa-sha256; cv=none; b=pDpYOEKPfrgFTW0jLrG3ReNbmgFfRgXlq/3DQPh9NpRxbZLT6srFzPF5p8yMJAAEBF3jtn xcHTifMAYKHTvgp5sxfEkjYeK6EanVz+ObRpDyWZZgM3Tgv/zBqiI8TNm8g6uHD5mZLFE8 Oa1fH/M6OZJd2HdUq2X2zHFdmDO/mk0= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=HmM0VO6g; spf=pass (imf26.hostedemail.com: domain of 3OeIIYwYKCEAinkfgtiqqing.eqonkpwz-oomxcem.qti@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3OeIIYwYKCEAinkfgtiqqing.eqonkpwz-oomxcem.qti@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526586; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=TL1IO7xzXsHa87B/8VTzh6zaFcPyOOUQBNZupLxKBzY=; b=SHbf2Cgo75KRA+keYRd3LnKRyNqujN5zZSSGNJzVrAfzvgjSTN9wiYnu1/LFbrYtEmMjD8 MTPbEFzl0L4eYLmeY68xnAtdXce/mbobblMJCCZfu4Ci58qES1Ymc7s4mk26z6y32HHVma 3ro317aV22R/D3sxTS4gqymExJasmk0= X-Stat-Signature: 7n36tria1ke9hn69shop7zfq7nprh88q X-Rspamd-Queue-Id: 90E4E140038 X-Rspam-User: X-Rspamd-Server: rspam06 Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=HmM0VO6g; spf=pass (imf26.hostedemail.com: domain of 3OeIIYwYKCEAinkfgtiqqing.eqonkpwz-oomxcem.qti@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3OeIIYwYKCEAinkfgtiqqing.eqonkpwz-oomxcem.qti@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-HE-Tag: 1661526586-868748 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: Instrumenting some files with KMSAN will result in kernel being unable to link, boot or crashing at runtime for various reasons (e.g. infinite recursion caused by instrumentation hooks calling instrumented code again). Completely omit KMSAN instrumentation in the following places: - arch/x86/boot and arch/x86/realmode/rm, as KMSAN doesn't work for i386; - arch/x86/entry/vdso, which isn't linked with KMSAN runtime; - three files in arch/x86/kernel - boot problems; - arch/x86/mm/cpu_entry_area.c - recursion. Signed-off-by: Alexander Potapenko --- v2: -- moved the patch earlier in the series so that KMSAN can compile -- split off the non-x86 part into a separate patch v3: -- added a comment to lib/Makefile v5: -- removed a comment belonging to another patch Link: https://linux-review.googlesource.com/id/Id5e5c4a9f9d53c24a35ebb633b814c414628d81b --- arch/x86/boot/Makefile | 1 + arch/x86/boot/compressed/Makefile | 1 + arch/x86/entry/vdso/Makefile | 3 +++ arch/x86/kernel/Makefile | 2 ++ arch/x86/kernel/cpu/Makefile | 1 + arch/x86/mm/Makefile | 2 ++ arch/x86/realmode/rm/Makefile | 1 + 7 files changed, 11 insertions(+) diff --git a/arch/x86/boot/Makefile b/arch/x86/boot/Makefile index ffec8bb01ba8c..9860ca5979f8a 100644 --- a/arch/x86/boot/Makefile +++ b/arch/x86/boot/Makefile @@ -12,6 +12,7 @@ # Sanitizer runtimes are unavailable and cannot be linked for early boot code. KASAN_SANITIZE := n KCSAN_SANITIZE := n +KMSAN_SANITIZE := n OBJECT_FILES_NON_STANDARD := y # Kernel does not boot with kcov instrumentation here. diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile index 35ce1a64068b7..3a261abb6d158 100644 --- a/arch/x86/boot/compressed/Makefile +++ b/arch/x86/boot/compressed/Makefile @@ -20,6 +20,7 @@ # Sanitizer runtimes are unavailable and cannot be linked for early boot code. KASAN_SANITIZE := n KCSAN_SANITIZE := n +KMSAN_SANITIZE := n OBJECT_FILES_NON_STANDARD := y # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in. diff --git a/arch/x86/entry/vdso/Makefile b/arch/x86/entry/vdso/Makefile index 12f6c4d714cd6..ce4eb7e44e5b8 100644 --- a/arch/x86/entry/vdso/Makefile +++ b/arch/x86/entry/vdso/Makefile @@ -11,6 +11,9 @@ include $(srctree)/lib/vdso/Makefile # Sanitizer runtimes are unavailable and cannot be linked here. KASAN_SANITIZE := n +KMSAN_SANITIZE_vclock_gettime.o := n +KMSAN_SANITIZE_vgetcpu.o := n + UBSAN_SANITIZE := n KCSAN_SANITIZE := n OBJECT_FILES_NON_STANDARD := y diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile index a20a5ebfacd73..ac564c5d7b1f0 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -33,6 +33,8 @@ KASAN_SANITIZE_sev.o := n # With some compiler versions the generated code results in boot hangs, caused # by several compilation units. To be safe, disable all instrumentation. KCSAN_SANITIZE := n +KMSAN_SANITIZE_head$(BITS).o := n +KMSAN_SANITIZE_nmi.o := n # If instrumentation of this dir is enabled, boot hangs during first second. # Probably could be more selective here, but note that files related to irqs, diff --git a/arch/x86/kernel/cpu/Makefile b/arch/x86/kernel/cpu/Makefile index 9661e3e802be5..f10a921ee7565 100644 --- a/arch/x86/kernel/cpu/Makefile +++ b/arch/x86/kernel/cpu/Makefile @@ -12,6 +12,7 @@ endif # If these files are instrumented, boot hangs during the first second. KCOV_INSTRUMENT_common.o := n KCOV_INSTRUMENT_perf_event.o := n +KMSAN_SANITIZE_common.o := n # As above, instrumenting secondary CPU boot code causes boot hangs. KCSAN_SANITIZE_common.o := n diff --git a/arch/x86/mm/Makefile b/arch/x86/mm/Makefile index f8220fd2c169a..39c0700c9955c 100644 --- a/arch/x86/mm/Makefile +++ b/arch/x86/mm/Makefile @@ -12,6 +12,8 @@ KASAN_SANITIZE_mem_encrypt_identity.o := n # Disable KCSAN entirely, because otherwise we get warnings that some functions # reference __initdata sections. KCSAN_SANITIZE := n +# Avoid recursion by not calling KMSAN hooks for CEA code. +KMSAN_SANITIZE_cpu_entry_area.o := n ifdef CONFIG_FUNCTION_TRACER CFLAGS_REMOVE_mem_encrypt.o = -pg diff --git a/arch/x86/realmode/rm/Makefile b/arch/x86/realmode/rm/Makefile index 83f1b6a56449f..f614009d3e4e2 100644 --- a/arch/x86/realmode/rm/Makefile +++ b/arch/x86/realmode/rm/Makefile @@ -10,6 +10,7 @@ # Sanitizer runtimes are unavailable and cannot be linked here. KASAN_SANITIZE := n KCSAN_SANITIZE := n +KMSAN_SANITIZE := n OBJECT_FILES_NON_STANDARD := y # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in. From patchwork Fri Aug 26 15:07:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956207 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 792B7ECAAD4 for ; Fri, 26 Aug 2022 15:09:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1CDEB94000F; Fri, 26 Aug 2022 11:09:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 12F06940007; Fri, 26 Aug 2022 11:09:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC35894000F; Fri, 26 Aug 2022 11:09:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id D6169940007 for ; Fri, 26 Aug 2022 11:09:49 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B2113140F28 for ; Fri, 26 Aug 2022 15:09:49 +0000 (UTC) X-FDA: 79842078498.01.04EE7FB Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) by imf02.hostedemail.com (Postfix) with ESMTP id 6ACC580028 for ; Fri, 26 Aug 2022 15:09:49 +0000 (UTC) Received: by mail-ej1-f73.google.com with SMTP id hb37-20020a170907162500b0073d7f7fbbbfso725620ejc.17 for ; Fri, 26 Aug 2022 08:09:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=3xJV8lOEjvc/s3Zvuma5zSeTndu+UAUJOaQAKluuQYo=; b=iowtS53RHlpCL6qU/VWV5GQVG1CF6AM2cb2K8UF+xf9Eri/m9bP/5b0fP/Yv5g71u0 G4xosRyjwnbYFvqXAKPuwh/r0w9aoa69XWRuAOHrIhWmWSx0iV2XmDVoCP0JlE47FuAB nmm0HUugDUAGgo/u5XwfJyioj1O9wMPKtRlUGvvX8gKw+EEmNXhvxQbPL/ZKDRcZQQPf MtPg7V/4IJP2t5P4sgZyKRHmwuOCzbI6xunPoBepArW292PT19a5ne9C4DB1Q+Nr1eTB S6mxBrnLUZM9ZZ8fq3aL2JmDi/WAivgNEzeAkYRHzPgQwniErjEHU3w34yBn1hByADXw pmoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=3xJV8lOEjvc/s3Zvuma5zSeTndu+UAUJOaQAKluuQYo=; b=HwO0v7UkPCxf3yFHaosFRaPOKCbT46HTRZLQAIyI6lP14PItDnQ7eLQAEAxFPt/Gxw iF+Ilgd9/ZQ9cJmj54eEGY+sTwFOuHsWAa2F20kJzsJ9yWDf/8FInbRtry0ymuwBi5r9 eHdHxsWAWiwaJqslOd4iEPMVY3q76B7tvQ9Dc0xAO+mD+CLapuMi2oqhYg7lKUemcDF5 UozFgol1XshcZ1ca/ULenC91vviRcQrzrggcvaEsGi6Y7JcdzWPdSevf20id20b2JCZF 8/16TBH7Cm60ndHvr2eD91sXzgKxyjHFB9Szip47BhfVMEO0Ht6XmSVRPhAMdwZ6xrdx 6juw== X-Gm-Message-State: ACgBeo1niTM8xu4YWwwBCxIBTnpJ+eT3SQ3M92gZV9bBNEbR7ogt14Xx 7c12wpIWOZYLtp8fwcvTUuMkxgEU3l8= X-Google-Smtp-Source: AA6agR7ZtlC3yezkLYO1v2tXY0fliINor4H6Q3eNUkRHGD2H51HnwKZSVDlZC6/hqKT8jj4k53bs2dn79I8= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a05:6402:3491:b0:446:ea7d:8d9c with SMTP id v17-20020a056402349100b00446ea7d8d9cmr7130674edc.184.1661526588030; Fri, 26 Aug 2022 08:09:48 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:57 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-35-glider@google.com> Subject: [PATCH v5 34/44] x86: kmsan: skip shadow checks in __switch_to() From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526589; a=rsa-sha256; cv=none; b=pNOJwBHMG5nG3XkUiQZpRCTK9bRnFKeDTZK4Ed3Tc3ARUNBRsqlWL3jiqvPXcdBZGuiumI CFqYwol3T8coz/Ecgs0irLDBoXI5qnmlXuFoFk1XgjzlGYskLPMaW4VxPnh2aLZuncGTIA PpQq6UtAoPkQsSh/Np6leUHc2jRdrKE= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=iowtS53R; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of 3POIIYwYKCEMlqnijwlttlqj.htrqnsz2-rrp0fhp.twl@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3POIIYwYKCEMlqnijwlttlqj.htrqnsz2-rrp0fhp.twl@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526589; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3xJV8lOEjvc/s3Zvuma5zSeTndu+UAUJOaQAKluuQYo=; b=z57ySbWAdz/Olh8nRVFxT3JJq/E7wyIORpOIpvB5pAeU8gWIbXUu+HdcHufbH/I3Qv2EmB 329cSf+0qHEAMvhUq6hsxeiW1L4Mz6NQdP5DYDWHH2KltZiRGlyYee67TiRTVlU4fzT1pM T7PVj2Ju1CEKctBJHt2OPcAvB1AISDM= X-Rspamd-Server: rspam11 X-Rspam-User: X-Rspamd-Queue-Id: 6ACC580028 Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=iowtS53R; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of 3POIIYwYKCEMlqnijwlttlqj.htrqnsz2-rrp0fhp.twl@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3POIIYwYKCEMlqnijwlttlqj.htrqnsz2-rrp0fhp.twl@flex--glider.bounces.google.com X-Stat-Signature: ruw6xhpmqzznikcs48j9aqnmxwqd7deh X-HE-Tag: 1661526589-966026 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: When instrumenting functions, KMSAN obtains the per-task state (mostly pointers to metadata for function arguments and return values) once per function at its beginning, using the `current` pointer. Every time the instrumented function calls another function, this state (`struct kmsan_context_state`) is updated with shadow/origin data of the passed and returned values. When `current` changes in the low-level arch code, instrumented code can not notice that, and will still refer to the old state, possibly corrupting it or using stale data. This may result in false positive reports. To deal with that, we need to apply __no_kmsan_checks to the functions performing context switching - this will result in skipping all KMSAN shadow checks and marking newly created values as initialized, preventing all false positive reports in those functions. False negatives are still possible, but we expect them to be rare and impersistent. Suggested-by: Marco Elver Signed-off-by: Alexander Potapenko Link: https://linux-review.googlesource.com/id/I520c414f52c19f3ea22377a9c570fff0d5943a95 --- v2: -- This patch was previously called "kmsan: skip shadow checks in files doing context switches". Per Mark Rutland's suggestion, we now only skip checks in low-level arch-specific code, as context switches in common code should be invisible to KMSAN. We also apply the checks to precisely the functions performing the context switch instead of the whole file. v5: -- Replace KMSAN_ENABLE_CHECKS_process_64.o with __no_kmsan_checks Link: https://linux-review.googlesource.com/id/I45e3ed9c5f66ee79b0409d1673d66ae419029bcb --- arch/x86/kernel/process_64.c | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index 1962008fe7437..6b3418bff3261 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -553,6 +553,7 @@ void compat_start_thread(struct pt_regs *regs, u32 new_ip, u32 new_sp, bool x32) * Kprobes not supported here. Set the probe on schedule instead. * Function graph tracer not supported too. */ +__no_kmsan_checks __visible __notrace_funcgraph struct task_struct * __switch_to(struct task_struct *prev_p, struct task_struct *next_p) { From patchwork Fri Aug 26 15:07:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956208 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 E44A5ECAAD4 for ; Fri, 26 Aug 2022 15:09:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7E788940010; Fri, 26 Aug 2022 11:09:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 77028940007; Fri, 26 Aug 2022 11:09:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5EA0F940010; Fri, 26 Aug 2022 11:09:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 4CE33940007 for ; Fri, 26 Aug 2022 11:09:52 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 2E6FB80E5A for ; Fri, 26 Aug 2022 15:09:52 +0000 (UTC) X-FDA: 79842078624.09.1A7DB53 Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf28.hostedemail.com (Postfix) with ESMTP id E0892C0031 for ; Fri, 26 Aug 2022 15:09:51 +0000 (UTC) Received: by mail-ed1-f73.google.com with SMTP id b12-20020a056402278c00b00447f2029741so1135568ede.23 for ; Fri, 26 Aug 2022 08:09:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=+OT6SjVSfWjyqzxbCZyABchDUcmsVpvJACt5vGUex24=; b=Z6njr41sW+TrPWrMaWFwC05L0RlOcYNPG3bg02BhPFfdHpKhjWZb3JQSd3EE7bQR4t BLm0gQ80M2u3Pdlj1pDIh1/qXUSrg7ZtmpWtyd9GiSzXUcv17N5l1NutHYYducPw8Nm1 53fip5LebyiAQRD1QGNbSqOqemN8I8dcjPld+gmWIt0k8q85y5Jr57b99LSEVn0lBt+H zdJQxrYdMQVOvNNpr1+RziHtXPGmKyGhGOD17LJ7bJ7AAVD99iIBY1+G52ypCLREDV+F yPwT4hR+BLBGY/xYmzyBjGu/5to6IUIp+jowlZqMWLLM1SmRm2HCrOTVeGnkhu8gk10k xggQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=+OT6SjVSfWjyqzxbCZyABchDUcmsVpvJACt5vGUex24=; b=DsJ8/2f7Hn17IjcRwW/xRqsC5qw6kGIiD1ZcmLQJOW7EPAm7T1o2kgc2rdiiik5+cq zuGTc4DZher/Gd33hyP86F/uSm/7hS2yS+cYHF6O2rwXnW5cq0BOr7UDxxCfroJthZHG 7EnEvHX7e88jixkSjU6C1xAwCWaFmIVDXFHFgaLN/cCwNqzoz2eijkqapfet8BJLT1s3 UHCsNF8y8dcQ3Zxem/UXch+CEDiNcZAV6fj+VlMb4zf3Ix1dAAo/Pd9jod4Nasg2/qoZ SD9CAIJGhJ7pFK7NpJMMuQp9E8+EoMuTmMY1HphRm2PQU8NrErT68IwZiUVGiCbmp8by vcEA== X-Gm-Message-State: ACgBeo0NxeJTV+L1m/uyh+lSxwWuHJfo19GrPpf6sQvT15uWnQwEp9PA 5Hyb+HBYaUyT1ext91rLyd3ZyxMGQIc= X-Google-Smtp-Source: AA6agR4t0eef92ReeAZR+Mrv7r8ag8b4BdV1NIADifSFVxHgYfyFuws8EoWxRalcgwj85FhAMYNvlh2MVzI= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a05:6402:43c6:b0:43d:79a6:4e32 with SMTP id p6-20020a05640243c600b0043d79a64e32mr6871771edc.281.1661526590665; Fri, 26 Aug 2022 08:09:50 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:58 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-36-glider@google.com> Subject: [PATCH v5 35/44] x86: kmsan: handle open-coded assembly in lib/iomem.c From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Z6njr41s; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf28.hostedemail.com: domain of 3PuIIYwYKCEUnspklynvvnsl.jvtspu14-ttr2hjr.vyn@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3PuIIYwYKCEUnspklynvvnsl.jvtspu14-ttr2hjr.vyn@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526591; a=rsa-sha256; cv=none; b=ky3W86vD7ZmzAUSK8+zP8olCaAuO+Zw1nx2PGm62qxho/wS3rW8860DvqKUm2DiO8VAJdW w7GZ9SN5LgbNGrgS+ZBxN5j6AnzxdxxI6iaXwsTpIw5vhBAv2N0ra+H8K0fFBGX1/Mu0i1 RKvgy01jAZJZ+paEkwCYnwK4073PfEA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526591; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=+OT6SjVSfWjyqzxbCZyABchDUcmsVpvJACt5vGUex24=; b=jVMOsMS+0lSIpC6V8aVJ+jJCTUISbNYqryO9BHC+tHibBt0bqv4xbhTWfAK+Atv49A08YA t3kcfhgzdKYawmcjr+4fCVlkQKgjUEN+c/kJEmuID0RZM4dq7E39rk4pNMR9L6y2jdwjpp Nyr4TZm6eAtpZbJAJzipnDAVIpBdN4A= X-Rspam-User: X-Rspamd-Queue-Id: E0892C0031 Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Z6njr41s; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf28.hostedemail.com: domain of 3PuIIYwYKCEUnspklynvvnsl.jvtspu14-ttr2hjr.vyn@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3PuIIYwYKCEUnspklynvvnsl.jvtspu14-ttr2hjr.vyn@flex--glider.bounces.google.com X-Stat-Signature: yy3jmpjhuaz8r7cpoytsh8wdft18yofh X-Rspamd-Server: rspam03 X-HE-Tag: 1661526591-654583 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 cannot intercept memory accesses within asm() statements. That's why we add kmsan_unpoison_memory() and kmsan_check_memory() to hint it how to handle memory copied from/to I/O memory. Signed-off-by: Alexander Potapenko --- Link: https://linux-review.googlesource.com/id/Icb16bf17269087e475debf07a7fe7d4bebc3df23 --- arch/x86/lib/iomem.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/arch/x86/lib/iomem.c b/arch/x86/lib/iomem.c index 3e2f33fc33de2..e0411a3774d49 100644 --- a/arch/x86/lib/iomem.c +++ b/arch/x86/lib/iomem.c @@ -1,6 +1,7 @@ #include #include #include +#include #define movs(type,to,from) \ asm volatile("movs" type:"=&D" (to), "=&S" (from):"0" (to), "1" (from):"memory") @@ -37,6 +38,8 @@ static void string_memcpy_fromio(void *to, const volatile void __iomem *from, si n-=2; } rep_movs(to, (const void *)from, n); + /* KMSAN must treat values read from devices as initialized. */ + kmsan_unpoison_memory(to, n); } static void string_memcpy_toio(volatile void __iomem *to, const void *from, size_t n) @@ -44,6 +47,8 @@ static void string_memcpy_toio(volatile void __iomem *to, const void *from, size if (unlikely(!n)) return; + /* Make sure uninitialized memory isn't copied to devices. */ + kmsan_check_memory(from, n); /* Align any unaligned destination IO */ if (unlikely(1 & (unsigned long)to)) { movs("b", to, from); From patchwork Fri Aug 26 15:07:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956209 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 A828AECAAD4 for ; Fri, 26 Aug 2022 15:09:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4B439940011; Fri, 26 Aug 2022 11:09:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 41581940007; Fri, 26 Aug 2022 11:09:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 24064940011; Fri, 26 Aug 2022 11:09:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 09170940007 for ; Fri, 26 Aug 2022 11:09:55 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E6590A0FA8 for ; Fri, 26 Aug 2022 15:09:54 +0000 (UTC) X-FDA: 79842078708.16.F672681 Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) by imf04.hostedemail.com (Postfix) with ESMTP id 9D0DD4001C for ; Fri, 26 Aug 2022 15:09:54 +0000 (UTC) Received: by mail-ej1-f73.google.com with SMTP id sc3-20020a1709078a0300b0073d77f805b3so732535ejc.22 for ; Fri, 26 Aug 2022 08:09:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=uVzdHqFGYZKd4Qu5GJB2AnYrpVyvjfHfk7LqoEQIkSc=; b=BC1C5JPZyis+Kes9Uu/ankA8YmaxeSDQUFcfBHXb3GZVLgly9+DxFLSdN6o6y7p7/P zVWe/gK4/+LWKxQGuvyhxewoxvI19U5hTqi2IpBvOJg3uq8Ik8xr0FYeUi72cpJlmij9 nf0JKqqIO86qyQ9jue4hqNcyWyObBn/CgX3JM/gkgO+Veqds0ttkLZ3Zg/KDXgiSSHRr oeNOglxG71/shVVy6M1hhBJW9QDDOpd1SAOcY4gcwkPldDcHeI0LxKdaN/3bAC+XERwl jFbWJk+m8LHqzyufpaIWhW/s3AL9MyZZGADD18fKDUyYSRK0x9/0JYGypV1K9dG68puO R0ZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=uVzdHqFGYZKd4Qu5GJB2AnYrpVyvjfHfk7LqoEQIkSc=; b=wfkCkHtVLYGGWyWAyR5m2OijVMMKonQ8FzMX4O88t9CMv8s2RaJX9QS3kia+oeFn4s EIkVA6hHoRuaO70mWNG3ey/oNZgwd7yThWe4MErE6EeCmuenLVi+6omNaW6BQUJrIxsC yLeUaprn0PAf1ShUXNI1yJfE9rrutNW2daNEf5yGt6lvLDH+XqYF7Uy8miRvAbCaQTLm 4VgmWr68aHH9Qt0FrEj1D9VqpmSEOq57b02wpt7odKhf361MSDNlORGrWS00smzrhTcW 6t/w/Ouh69wAFrIfoS9J885N/Q69a1aCg7p+6vDSUAgpBD7qXatLe86IZwPx0R7aO6Wh oFmw== X-Gm-Message-State: ACgBeo0cHztUX7wyKNmcBsNTuW92jV2CGagesmVRCSwp7wZEdVHR1qEV 6NjVw/VzKCMA1zHtUKNbVOF1uVn4qC4= X-Google-Smtp-Source: AA6agR46uXnxQGKu9ULnyAc1BLhctUrVNAcVdLs11pJsS5/NKYX8LczIxzp8tXMl7eS110R8HE5vOzH3J7w= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a05:6402:368c:b0:446:48d9:2be with SMTP id ej12-20020a056402368c00b0044648d902bemr6858842edb.167.1661526593376; Fri, 26 Aug 2022 08:09:53 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:59 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-37-glider@google.com> Subject: [PATCH v5 36/44] x86: kmsan: use __msan_ string functions where possible. From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526594; a=rsa-sha256; cv=none; b=jAxVlFyV3UikljdfKMmOysRInnT0fYs1plzP1BlgubRg/mvl86EG5ZRt9MPBlsnCpkNbbL jJOWKzPHqjuloaPg2VNHNZrTbPAVNZGo9hLN8gAlu622pLPgSFoTbHQH5PkhELdcPB4NJd qDRr6Ilrl1fbZFBfml/ke/De6qdd80s= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=BC1C5JPZ; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf04.hostedemail.com: domain of 3QeIIYwYKCEgqvsno1qyyqvo.mywvsx47-wwu5kmu.y1q@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3QeIIYwYKCEgqvsno1qyyqvo.mywvsx47-wwu5kmu.y1q@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526594; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=uVzdHqFGYZKd4Qu5GJB2AnYrpVyvjfHfk7LqoEQIkSc=; b=g0GgI2TOzj240Wa29u3VV9tIPXB9z3Jf+O5eqbzFq6wahDTAZu7EpLTAY5VpVtAMEsP0TB S9u5KVoKtSp5yZnvUX/z8Tir24qxa6XAlo10hXrpHbxytrijyxIxaItO/OwwI0emqMbZN+ lcscrgg3R5bR/miKW6Ife3gRkQms6Ec= X-Rspamd-Queue-Id: 9D0DD4001C Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=BC1C5JPZ; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf04.hostedemail.com: domain of 3QeIIYwYKCEgqvsno1qyyqvo.mywvsx47-wwu5kmu.y1q@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3QeIIYwYKCEgqvsno1qyyqvo.mywvsx47-wwu5kmu.y1q@flex--glider.bounces.google.com X-Rspam-User: X-Rspamd-Server: rspam01 X-Stat-Signature: gw65bcoykq6wkxz9js97s53ar4eh8hin X-HE-Tag: 1661526594-85523 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: Unless stated otherwise (by explicitly calling __memcpy(), __memset() or __memmove()) we want all string functions to call their __msan_ versions (e.g. __msan_memcpy() instead of memcpy()), so that shadow and origin values are updated accordingly. Bootloader must still use the default string functions to avoid crashes. Signed-off-by: Alexander Potapenko --- Link: https://linux-review.googlesource.com/id/I7ca9bd6b4f5c9b9816404862ae87ca7984395f33 --- arch/x86/include/asm/string_64.h | 23 +++++++++++++++++++++-- include/linux/fortify-string.h | 2 ++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/string_64.h b/arch/x86/include/asm/string_64.h index 6e450827f677a..3b87d889b6e16 100644 --- a/arch/x86/include/asm/string_64.h +++ b/arch/x86/include/asm/string_64.h @@ -11,11 +11,23 @@ function. */ #define __HAVE_ARCH_MEMCPY 1 +#if defined(__SANITIZE_MEMORY__) +#undef memcpy +void *__msan_memcpy(void *dst, const void *src, size_t size); +#define memcpy __msan_memcpy +#else extern void *memcpy(void *to, const void *from, size_t len); +#endif extern void *__memcpy(void *to, const void *from, size_t len); #define __HAVE_ARCH_MEMSET +#if defined(__SANITIZE_MEMORY__) +extern void *__msan_memset(void *s, int c, size_t n); +#undef memset +#define memset __msan_memset +#else void *memset(void *s, int c, size_t n); +#endif void *__memset(void *s, int c, size_t n); #define __HAVE_ARCH_MEMSET16 @@ -55,7 +67,13 @@ static inline void *memset64(uint64_t *s, uint64_t v, size_t n) } #define __HAVE_ARCH_MEMMOVE +#if defined(__SANITIZE_MEMORY__) +#undef memmove +void *__msan_memmove(void *dest, const void *src, size_t len); +#define memmove __msan_memmove +#else void *memmove(void *dest, const void *src, size_t count); +#endif void *__memmove(void *dest, const void *src, size_t count); int memcmp(const void *cs, const void *ct, size_t count); @@ -64,8 +82,7 @@ char *strcpy(char *dest, const char *src); char *strcat(char *dest, const char *src); int strcmp(const char *cs, const char *ct); -#if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__) - +#if (defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__)) /* * For files that not instrumented (e.g. mm/slub.c) we * should use not instrumented version of mem* functions. @@ -73,7 +90,9 @@ int strcmp(const char *cs, const char *ct); #undef memcpy #define memcpy(dst, src, len) __memcpy(dst, src, len) +#undef memmove #define memmove(dst, src, len) __memmove(dst, src, len) +#undef memset #define memset(s, c, n) __memset(s, c, n) #ifndef __NO_FORTIFY diff --git a/include/linux/fortify-string.h b/include/linux/fortify-string.h index 3b401fa0f3746..6c8a1a29d0b63 100644 --- a/include/linux/fortify-string.h +++ b/include/linux/fortify-string.h @@ -285,8 +285,10 @@ __FORTIFY_INLINE void fortify_memset_chk(__kernel_size_t size, * __builtin_object_size() must be captured here to avoid evaluating argument * side-effects further into the macro layers. */ +#ifndef CONFIG_KMSAN #define memset(p, c, s) __fortify_memset_chk(p, c, s, \ __builtin_object_size(p, 0), __builtin_object_size(p, 1)) +#endif /* * To make sure the compiler can enforce protection against buffer overflows, From patchwork Fri Aug 26 15:08:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956210 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 772D3ECAAA3 for ; Fri, 26 Aug 2022 15:09:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 130EF940012; Fri, 26 Aug 2022 11:09:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0BA23940007; Fri, 26 Aug 2022 11:09:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E7634940012; Fri, 26 Aug 2022 11:09:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id D5384940007 for ; Fri, 26 Aug 2022 11:09:58 -0400 (EDT) Received: from smtpin31.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id B2DD3160E62 for ; Fri, 26 Aug 2022 15:09:58 +0000 (UTC) X-FDA: 79842078876.31.D9E065F Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf18.hostedemail.com (Postfix) with ESMTP id 57B261C0024 for ; Fri, 26 Aug 2022 15:09:58 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id z6-20020a05640240c600b0043e1d52fd98so1242184edb.22 for ; Fri, 26 Aug 2022 08:09:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=03rMp3Lo5JUXz6YDRuTXWFQl7G4ZPrO7G1beymmSRqE=; b=sAVtSR5T+WmzYcO0y/rL5vqCNNkvHaCr2ijspx7pp0tTQssYQZLgmJ9706bnECI/Pi YvE0hXaOB/bpoPUuj28jQP33PpftgYXVTG+qpwIi3L5mxwe+6kaftp5iFooMAa5EApv+ 4Xho0ql82BCrzE6U0OTZGJ/taBfHGO1y8osby63H8xs97a7h4A461xjeIy2Zy7keH0xZ +t+I84JRMBYSgKA0pc92tEXu95OISOZBUXZ056nBZFYe+Q+0daqg0RU0hYyWf00i7zRH 6jRE7es/OUAGkP/ew2ox8ehfjiFp5faSOGaeaVZskTV8TnVAMbg5VUMNyZ3ZbwBFqN1c qKMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=03rMp3Lo5JUXz6YDRuTXWFQl7G4ZPrO7G1beymmSRqE=; b=mp6bNcEUj42Gky8uGMX4mxHh5RMUWLWDnG785uUzsXUqIhx2d3WYQ1QY7AFmITzYTf ZS6uKCPS/xLY/GpPVXmpaKh3KJ14ktrfky0jppQcH/rJ2wu2ug+M7YI1XKisRnVTktyM gmBk7uzuCYc+RE1E19EvXbVnX8pZh9+6IOImMV2zUs5tB1EM68fhj+zaMdWhtlkgfTxr 943KxS9fbVT1bIpyKpJKajEQIJ6+4ErEC46AansoAo1l4c1TSQo7zuceoeP4lW1b9Uk8 WVrJsJtyoxdIXp7em6DcU3g7AyaptyYA7DI3kg9sPshXSraj1oscwe5ouA7oNFNj7V1D QN4Q== X-Gm-Message-State: ACgBeo3FAT5/+8nrJO+s6XusG2wF1poiVDtlJMngUDu9FdiTzgW73rg6 FudboCi3m0hZ/29iaLURidP5kkY+lk8= X-Google-Smtp-Source: AA6agR5sfXXazfCHPhI3z6MZ77T83FgZy9TTJ1m+QzHsaIqLLsWY/SYhrcqQDvh5VnxcE117PZ//mWwCuBo= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a05:6402:26c6:b0:446:b4aa:5d00 with SMTP id x6-20020a05640226c600b00446b4aa5d00mr7346330edd.63.1661526597110; Fri, 26 Aug 2022 08:09:57 -0700 (PDT) Date: Fri, 26 Aug 2022 17:08:00 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-38-glider@google.com> Subject: [PATCH v5 37/44] x86: kmsan: sync metadata pages on page fault From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526598; a=rsa-sha256; cv=none; b=bLhvUtpPYhvqog8DL8mlJnaLVrMmEC7nZkwehzOvT5dZkmyfRuOVeEzg+qf1EgQgT4206M gudF9UyTL1RXzNGhyS9Izu9EjHqYCOlSGiKAk4p9MFpcEe8e5DhntYGNnm55UbVRhX3QOk TrObMQbeXq56JCOI6TWP/Yb+Va0/7jQ= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=sAVtSR5T; spf=pass (imf18.hostedemail.com: domain of 3ReIIYwYKCEwuzwrs5u22uzs.q20zw18B-00y9oqy.25u@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3ReIIYwYKCEwuzwrs5u22uzs.q20zw18B-00y9oqy.25u@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526598; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=03rMp3Lo5JUXz6YDRuTXWFQl7G4ZPrO7G1beymmSRqE=; b=O1R9ASKZizvSrksMMe+pdKo5hfNAxON8OTvkaKstYOQsOYoLVSzOdE1w7NDvAQ8T9IHA6K xGYFa0cvICb6h9ZOMG2kIFZyI7BkNyXy6iLs4aaAVbr1CLlEhUS69P+3FXm11x6iEhCBBu EelWohnS9dKdreGDRtQjliqds5Yatdg= X-Stat-Signature: b6pnyfqj4m6rk1qhxr8a5q8fjyfytndu X-Rspamd-Queue-Id: 57B261C0024 Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=sAVtSR5T; spf=pass (imf18.hostedemail.com: domain of 3ReIIYwYKCEwuzwrs5u22uzs.q20zw18B-00y9oqy.25u@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3ReIIYwYKCEwuzwrs5u22uzs.q20zw18B-00y9oqy.25u@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspam-User: X-Rspamd-Server: rspam02 X-HE-Tag: 1661526598-804393 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 assumes shadow and origin pages for every allocated page are accessible. For pages between [VMALLOC_START, VMALLOC_END] those metadata pages start at KMSAN_VMALLOC_SHADOW_START and KMSAN_VMALLOC_ORIGIN_START, therefore we must sync a bigger memory region. Signed-off-by: Alexander Potapenko --- v2: -- addressed reports from kernel test robot Link: https://linux-review.googlesource.com/id/Ia5bd541e54f1ecc11b86666c3ec87c62ac0bdfb8 --- arch/x86/mm/fault.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index fa71a5d12e872..d728791be8ace 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -260,7 +260,7 @@ static noinline int vmalloc_fault(unsigned long address) } NOKPROBE_SYMBOL(vmalloc_fault); -void arch_sync_kernel_mappings(unsigned long start, unsigned long end) +static void __arch_sync_kernel_mappings(unsigned long start, unsigned long end) { unsigned long addr; @@ -284,6 +284,27 @@ void arch_sync_kernel_mappings(unsigned long start, unsigned long end) } } +void arch_sync_kernel_mappings(unsigned long start, unsigned long end) +{ + __arch_sync_kernel_mappings(start, end); +#ifdef CONFIG_KMSAN + /* + * KMSAN maintains two additional metadata page mappings for the + * [VMALLOC_START, VMALLOC_END) range. These mappings start at + * KMSAN_VMALLOC_SHADOW_START and KMSAN_VMALLOC_ORIGIN_START and + * have to be synced together with the vmalloc memory mapping. + */ + if (start >= VMALLOC_START && end < VMALLOC_END) { + __arch_sync_kernel_mappings( + start - VMALLOC_START + KMSAN_VMALLOC_SHADOW_START, + end - VMALLOC_START + KMSAN_VMALLOC_SHADOW_START); + __arch_sync_kernel_mappings( + start - VMALLOC_START + KMSAN_VMALLOC_ORIGIN_START, + end - VMALLOC_START + KMSAN_VMALLOC_ORIGIN_START); + } +#endif +} + static bool low_pfn(unsigned long pfn) { return pfn < max_low_pfn; From patchwork Fri Aug 26 15:08:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956211 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 528DDECAAA3 for ; Fri, 26 Aug 2022 15:10:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E0315940008; Fri, 26 Aug 2022 11:10:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D8AA8940007; Fri, 26 Aug 2022 11:10:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BDD8B940008; Fri, 26 Aug 2022 11:10:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id AAD78940007 for ; Fri, 26 Aug 2022 11:10:01 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 8EFD740CE7 for ; Fri, 26 Aug 2022 15:10:01 +0000 (UTC) X-FDA: 79842079002.07.B1D34B6 Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) by imf21.hostedemail.com (Postfix) with ESMTP id 168FA1C001B for ; Fri, 26 Aug 2022 15:10:00 +0000 (UTC) Received: by mail-ej1-f74.google.com with SMTP id hb37-20020a170907162500b0073d7f7fbbbfso725843ejc.17 for ; Fri, 26 Aug 2022 08:10:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=G7zTeV7HivGGK7/AmFGwNvvgE+xYoTaOW1pCL0nph+s=; b=rllUU8td54J92uwD4gqMdf7quRrn9vs+RF/VGfmhTNifoonDDOGDzA+ehk2F0Nktis bzsb1L86DwkC+0Lwn7biXr7xSRRAyztcTTabYoOev0SalCjTFvsRPApNrtWrG92arrNL vYy30mjcDtboOMLaYF/EGB9Bb67OhSnqrd9929GMzB46msZdCnUGmupRwBiLOAMpOTu/ xgXxZRY8JK8LqcDPH4Z1qbgGAF+6QrIMppgLaa1fjdmq/jeISdUH5rHhnHGN0itBSODe GmUzrI95aXvhJ1Ss7JdEnAwqbHxBSkafgMVWr/h9flFgsjbzEK99uA8DU4lsh1XM5zFP k9rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=G7zTeV7HivGGK7/AmFGwNvvgE+xYoTaOW1pCL0nph+s=; b=qyxgby37daU0GmLoRVg2a2n9NqcUtQDX/3OsJJOfjF42OSjX2odb7rvdyPCqolPEzA 2oCoVBQeLqGONg4Xr8vOMuFN3lNDaQJ65oZprWNfQjP0hf6bjmR1e9BNOyFf0cyQa+Yo buv8Gic6YwA7psdWkSvZ93yNGxn7ETuqvy95E4e1eIJsAgDR3Wux3uSCoMVIS+Jpimrp HW8YCFIKuiwK/+RtlrVkcxRhsn1GrvzpxwfTeyAKdo2Ph4RR6NETHNkFge7NnSUvifSt P1zxR9E4vMmzjgPv/VPnIhNV2iIruRQdLljLZUyiTTb/NbLLLqTVftRY0kcWmqAAFB/E vpoA== X-Gm-Message-State: ACgBeo37QuaU3JZakJSMXUUOch/+i3Bp3Qlw5yH+IFeeGXnKDTAUMjAG 81Uxpk9i4j51EiYLPTVLM8YV1yf8FuM= X-Google-Smtp-Source: AA6agR6mygGnpoLM0Fz1GI/HNspwZBf2HxeWieGPgMrRMoJDYKMR11CPv0kXU0SX1XneClLqmTxEKKFSAKQ= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a17:907:b17:b0:72a:edb8:7529 with SMTP id h23-20020a1709070b1700b0072aedb87529mr5717591ejl.749.1661526599889; Fri, 26 Aug 2022 08:09:59 -0700 (PDT) Date: Fri, 26 Aug 2022 17:08:01 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-39-glider@google.com> Subject: [PATCH v5 38/44] x86: kasan: kmsan: support CONFIG_GENERIC_CSUM on x86, enable it for KASAN/KMSAN From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526601; a=rsa-sha256; cv=none; b=P40Bc3mepvTXbjyswHQBhexC2iNKYWIpLG4kIoknA3ARQy4LGsJqpmjmYG2EQOM+fnzuBb pjI74Bu8TbwF/eNxwgaMjaqBn0a716aRu+6p/poJMG96E947iSl7jwK7OH9pLKrzkECvjQ 9nJRDhEBZ2ijQcENCYvDK3gww2fnwbU= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=rllUU8td; spf=pass (imf21.hostedemail.com: domain of 3R-IIYwYKCE4w1ytu7w44w1u.s421y3AD-220Bqs0.47w@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3R-IIYwYKCE4w1ytu7w44w1u.s421y3AD-220Bqs0.47w@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526601; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=G7zTeV7HivGGK7/AmFGwNvvgE+xYoTaOW1pCL0nph+s=; b=aT9v7ille6HGf1p52lzK9PZao4srLNObHNdntVgN/rKH9ljfOGE0CJp8rJLwo+jUV7J0bB uSY0KKxjgzUWAlVlYqVyrkzlGhv2hICci1XsMfWT1KrmlwQWoTth7S3cIkm+oyxe6oO8ZE hk9BajjWZAwt2T32n5Fw9xih4RX2AiQ= X-Stat-Signature: gx55s37zzchgsmcx1p43egx3xtjj6xs6 X-Rspamd-Queue-Id: 168FA1C001B Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=rllUU8td; spf=pass (imf21.hostedemail.com: domain of 3R-IIYwYKCE4w1ytu7w44w1u.s421y3AD-220Bqs0.47w@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3R-IIYwYKCE4w1ytu7w44w1u.s421y3AD-220Bqs0.47w@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspam-User: X-Rspamd-Server: rspam02 X-HE-Tag: 1661526600-191770 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: This is needed to allow memory tools like KASAN and KMSAN see the memory accesses from the checksum code. Without CONFIG_GENERIC_CSUM the tools can't see memory accesses originating from handwritten assembly code. For KASAN it's a question of detecting more bugs, for KMSAN using the C implementation also helps avoid false positives originating from seemingly uninitialized checksum values. Signed-off-by: Alexander Potapenko --- Link: https://linux-review.googlesource.com/id/I3e95247be55b1112af59dbba07e8cbf34e50a581 --- arch/x86/Kconfig | 4 ++++ arch/x86/include/asm/checksum.h | 16 ++++++++++------ arch/x86/lib/Makefile | 2 ++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index f9920f1341c8d..33f4d4baba079 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -324,6 +324,10 @@ config GENERIC_ISA_DMA def_bool y depends on ISA_DMA_API +config GENERIC_CSUM + bool + default y if KMSAN || KASAN + config GENERIC_BUG def_bool y depends on BUG diff --git a/arch/x86/include/asm/checksum.h b/arch/x86/include/asm/checksum.h index bca625a60186c..6df6ece8a28ec 100644 --- a/arch/x86/include/asm/checksum.h +++ b/arch/x86/include/asm/checksum.h @@ -1,9 +1,13 @@ /* SPDX-License-Identifier: GPL-2.0 */ -#define _HAVE_ARCH_COPY_AND_CSUM_FROM_USER 1 -#define HAVE_CSUM_COPY_USER -#define _HAVE_ARCH_CSUM_AND_COPY -#ifdef CONFIG_X86_32 -# include +#ifdef CONFIG_GENERIC_CSUM +# include #else -# include +# define _HAVE_ARCH_COPY_AND_CSUM_FROM_USER 1 +# define HAVE_CSUM_COPY_USER +# define _HAVE_ARCH_CSUM_AND_COPY +# ifdef CONFIG_X86_32 +# include +# else +# include +# endif #endif diff --git a/arch/x86/lib/Makefile b/arch/x86/lib/Makefile index f76747862bd2e..7ba5f61d72735 100644 --- a/arch/x86/lib/Makefile +++ b/arch/x86/lib/Makefile @@ -65,7 +65,9 @@ ifneq ($(CONFIG_X86_CMPXCHG64),y) endif else obj-y += iomap_copy_64.o +ifneq ($(CONFIG_GENERIC_CSUM),y) lib-y += csum-partial_64.o csum-copy_64.o csum-wrappers_64.o +endif lib-y += clear_page_64.o copy_page_64.o lib-y += memmove_64.o memset_64.o lib-y += copy_user_64.o From patchwork Fri Aug 26 15:08:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956212 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 791C5ECAAD6 for ; Fri, 26 Aug 2022 15:10:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 157D3940013; Fri, 26 Aug 2022 11:10:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0E0F3940007; Fri, 26 Aug 2022 11:10:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DD930940013; Fri, 26 Aug 2022 11:10:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id CF351940007 for ; Fri, 26 Aug 2022 11:10:03 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id AEC47160E99 for ; Fri, 26 Aug 2022 15:10:03 +0000 (UTC) X-FDA: 79842079086.30.CF76409 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf11.hostedemail.com (Postfix) with ESMTP id 507FE40014 for ; Fri, 26 Aug 2022 15:10:03 +0000 (UTC) Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-33d9f6f4656so29463707b3.21 for ; Fri, 26 Aug 2022 08:10:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=4pDNTP8Rcz1c/6m0qOjb0X1O42ovrN3YaDrJNkQcLng=; b=D8g9K+F2VtTbVGCiyA3gHOZ8vWyfLv3kiBVO8NzVmbbuaiAQz5lidowjJcTUnqN9NV 0M45L50FmJ4PxOKToUEzR27CpoNoU/oyH1Qxm5OUEXk0XU/8/XXprRa1vk5Bi3TeJfkw q081N/bHUsn7mQv/yoQ1Ey2nzz9m+S9Fo7PM6zBWmqx38XFTQk025tvKxOEGgVZjqGty /9F1JErkTDPulPdl0zxIDtlkVyQUOPBuGPdH9gBMfPQwL19d8No0nlzWwOQohCYKDZm6 MPulEwsqymX7KjroU/sIuuObTC/BUFlxcZTS2qy9QDHhU8TveQCUg6HprGxaMBW2ev2v GJow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=4pDNTP8Rcz1c/6m0qOjb0X1O42ovrN3YaDrJNkQcLng=; b=Kc4vpwLLZ8ulos9kOiqSPFWTJbjsP0YM6xNzLBou9Et8ueOB4Rio/xlFMKvfbVYqGz V5q7AuEmxX792YICrEj9BoFDPrmyBXZaWuWuRJRKMAHcae+wteVGqUr75NOjTNUIw4b9 qhpuVt1LAaGzQTmKb0GntbwXU6Ne6+JqTcWxjVjas5A3mixLnpphbsBWqBo2KlHvxvrz c4wv+A1MlBA+REzgJb+288bDd/n2iSOwqlF81LXEfowMFj3T6T6LRJDyiZMhben8oEIR rPOq6RW5PNlelh+1Gf1QxSmclUMN+V1TW8/b+U9MxZM0Q4H/q/dfkGXL0hBQUXqRguDQ vG8g== X-Gm-Message-State: ACgBeo3m4sTQrdbS2/yCfrrK/GG9rTplW7w6+B+Gs6+SxXTRg0UeMuRo aEtDoxZ/H3+c7UV8RCaoLcfSQ51CmvA= X-Google-Smtp-Source: AA6agR5vVQmtlhIgTKQ7dnFjHT2Qul1eY6YRiLp6VijdR+cm0ab1oQKWZ6dDBOhtA58d0ZDce6nTKPF8G7g= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a05:6902:10ca:b0:671:3616:9147 with SMTP id w10-20020a05690210ca00b0067136169147mr92484ybu.105.1661526602616; Fri, 26 Aug 2022 08:10:02 -0700 (PDT) Date: Fri, 26 Aug 2022 17:08:02 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-40-glider@google.com> Subject: [PATCH v5 39/44] x86: fs: kmsan: disable CONFIG_DCACHE_WORD_ACCESS From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, Andrey Konovalov ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526603; a=rsa-sha256; cv=none; b=qimphwSapwpv+6t0pstxhAB423Q+NVGAaFjbqngjba53oOgIqSLHbD2uP3fy1rw2/zkDHy G+m9RrKvBQlziYrmNR3OcgAHBM1qAwt3uUBvcQ9BiFuvqT1naePnksI7giiSCXi5N3vOVY XKOaqwO5T/StH23M5+hvhmhNHSluqFg= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=D8g9K+F2; spf=pass (imf11.hostedemail.com: domain of 3SuIIYwYKCFEz41wxAz77z4x.v75416DG-553Etv3.7Az@flex--glider.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3SuIIYwYKCFEz41wxAz77z4x.v75416DG-553Etv3.7Az@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526603; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4pDNTP8Rcz1c/6m0qOjb0X1O42ovrN3YaDrJNkQcLng=; b=seeKYHVaHa91/3cjC2344Y9XOLX1U9pyuMA4EUhaESEBHpMxR20Ik1oO4dchb6L9RPdkMM hhJudyJ6+ddb8ufiMNxfqvdtExXFFwwz5YtJEZCotlzUVGO28c/8G0BLOMTXBHhFUcVFbG hWxam7RfpaAlCEB7wEcdaInDZpFvBAE= X-Stat-Signature: gc7x8r6z864qqqwqei6u59g1hue164po X-Rspamd-Queue-Id: 507FE40014 X-Rspam-User: X-Rspamd-Server: rspam06 Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=D8g9K+F2; spf=pass (imf11.hostedemail.com: domain of 3SuIIYwYKCFEz41wxAz77z4x.v75416DG-553Etv3.7Az@flex--glider.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3SuIIYwYKCFEz41wxAz77z4x.v75416DG-553Etv3.7Az@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-HE-Tag: 1661526603-527102 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: dentry_string_cmp() calls read_word_at_a_time(), which might read uninitialized bytes to optimize string comparisons. Disabling CONFIG_DCACHE_WORD_ACCESS should prohibit this optimization, as well as (probably) similar ones. Suggested-by: Andrey Konovalov Signed-off-by: Alexander Potapenko --- Link: https://linux-review.googlesource.com/id/I4c0073224ac2897cafb8c037362c49dda9cfa133 --- arch/x86/Kconfig | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 33f4d4baba079..697da8dae1418 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -128,7 +128,9 @@ config X86 select CLKEVT_I8253 select CLOCKSOURCE_VALIDATE_LAST_CYCLE select CLOCKSOURCE_WATCHDOG - select DCACHE_WORD_ACCESS + # Word-size accesses may read uninitialized data past the trailing \0 + # in strings and cause false KMSAN reports. + select DCACHE_WORD_ACCESS if !KMSAN select DYNAMIC_SIGFRAME select EDAC_ATOMIC_SCRUB select EDAC_SUPPORT From patchwork Fri Aug 26 15:08:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956213 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 8EF2DECAAA3 for ; Fri, 26 Aug 2022 15:10:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 32D91940014; Fri, 26 Aug 2022 11:10:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2DCE6940007; Fri, 26 Aug 2022 11:10:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 12F72940014; Fri, 26 Aug 2022 11:10:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 009C1940007 for ; Fri, 26 Aug 2022 11:10:06 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id CE4B2140F3F for ; Fri, 26 Aug 2022 15:10:06 +0000 (UTC) X-FDA: 79842079212.07.7DA8217 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf06.hostedemail.com (Postfix) with ESMTP id 7B858180011 for ; Fri, 26 Aug 2022 15:10:06 +0000 (UTC) Received: by mail-wm1-f73.google.com with SMTP id i7-20020a1c3b07000000b003a534ec2570so4189113wma.7 for ; Fri, 26 Aug 2022 08:10:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=BglHskuQEcyUyCGOJUH508pmeGJAtuUxUacPJvhZbyk=; b=dLOEePxQJ3Q8NTUKaCKscxiPUndoB4UtDuE3c4k6e51Ijeopi1Q2jwY0fQKx91/gRV tjQCOKAvn8SFyX8Z/lnl+CXNB72GSSg2BB8Hq6Vt7SqvWnhLYEqRJylixp3qoaDTG2zm raV0B1FrZYeNKlh2L47/qDrSM5egy+WYAo9m1hFPEaFpdxuy6sh5qTqGIMfCAvjZyI9j 3naaSJ5illmzgZ7ldkbeAiiQNTjVVsYNVEYdyNBLpQSwjzyRINT8fbPdzQGGY0zreFu8 UWmcW7wtBNkjvMa2czQus+EuHAF96CwCURikCoYjx9Tb33q6CXTdGxIjYlAAIrQDAeMj IDGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=BglHskuQEcyUyCGOJUH508pmeGJAtuUxUacPJvhZbyk=; b=sYuKmpnlZgbmJS1WIYTTmKJjVXxfNxezu52cpVumk745tHhK13I46y21jlNdTa4PgH W+F4BMf+OaqPB7Eohe8bJxJZlQI81G4VjLLzqzZ4ctcqqXL0y7XTYopXewsWaroYLE5S BJCg9YkVrohofcboseXd9HBLFTdmVGysTetgVHWYb8rhRv43kXAX2+SvVgbjUBVYrWhS /qYwkXPADJHlfKhQ64kM6+HQOntMJ/TMCT4Kxpiol8MS7NZnOzESP9kWndex6n9hPpAp 9xdpBnb2AIwiPAKg7nnQCDxESH+U/gHIen2tD8LVrfq5+CzgJAwIxVCrK3ULoswNRZxA UPqQ== X-Gm-Message-State: ACgBeo2LVrYzaExGx54Wd2wisOiyZfs/8LphLi07fes37Key0mnJ1cWn czorL/9cCKgT+LF4j7hmK9LMTDsZpgQ= X-Google-Smtp-Source: AA6agR6NknND7Ys7dLoaR0YpbVpHtssuPzgQY9WE+k93NXy2tjYdfG5ALV06XNy3sKThCmImd1fIcvZnQyg= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a5d:58d6:0:b0:226:cf81:f68d with SMTP id o22-20020a5d58d6000000b00226cf81f68dmr60412wrf.131.1661526605261; Fri, 26 Aug 2022 08:10:05 -0700 (PDT) Date: Fri, 26 Aug 2022 17:08:03 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-41-glider@google.com> Subject: [PATCH v5 40/44] x86: kmsan: don't instrument stack walking functions From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=dLOEePxQ; spf=pass (imf06.hostedemail.com: domain of 3TeIIYwYKCFQ274z0D2AA270.yA8749GJ-886Hwy6.AD2@flex--glider.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3TeIIYwYKCFQ274z0D2AA270.yA8749GJ-886Hwy6.AD2@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526606; a=rsa-sha256; cv=none; b=Sh9tiw56GqWaAKjiJl3Kz85W594tMr2/Os3xESw432zLce4wrrrh1LU9Wz+wxtgiJWQVz/ blp/oYPkiNvCwLxL7ttHn5FQv6K0Wr0UmVAJ8ZPwBFxk0Emy4P30E9n8pvPNhTgH9KqMWD 5lqM7ToIeqbU8CsFLj1Q7fohwG7a+/k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526606; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=BglHskuQEcyUyCGOJUH508pmeGJAtuUxUacPJvhZbyk=; b=i953kYvS7jkbvJpwfl5onj8fg7S5AGV2RG08Xx0PmY2i7xhXJGKRDHc/z3xbOAK4WexhwJ DET8rZRH4pYrjgMXqC6QeJGo0EzWcWhZ6WJYCkiSTsIvvPgjXDL6K3aVzRxb3HVT+9LN2J guV6XDVu+8dsbMeX8uDE8LCIsBMWe0w= X-Rspam-User: Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=dLOEePxQ; spf=pass (imf06.hostedemail.com: domain of 3TeIIYwYKCFQ274z0D2AA270.yA8749GJ-886Hwy6.AD2@flex--glider.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3TeIIYwYKCFQ274z0D2AA270.yA8749GJ-886Hwy6.AD2@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam07 X-Stat-Signature: ff9fhfxpsiapgcrwfhcd8y67jo1bw4ey X-Rspamd-Queue-Id: 7B858180011 X-HE-Tag: 1661526606-728533 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: Upon function exit, KMSAN marks local variables as uninitialized. Further function calls may result in the compiler creating the stack frame where these local variables resided. This results in frame pointers being marked as uninitialized data, which is normally correct, because they are not stack-allocated. However stack unwinding functions are supposed to read and dereference the frame pointers, in which case KMSAN might be reporting uses of uninitialized values. To work around that, we mark update_stack_state(), unwind_next_frame() and show_trace_log_lvl() with __no_kmsan_checks, preventing all KMSAN reports inside those functions and making them return initialized values. Signed-off-by: Alexander Potapenko --- Link: https://linux-review.googlesource.com/id/I6550563768fbb08aa60b2a96803675dcba93d802 --- arch/x86/kernel/dumpstack.c | 6 ++++++ arch/x86/kernel/unwind_frame.c | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/arch/x86/kernel/dumpstack.c b/arch/x86/kernel/dumpstack.c index afae4dd774951..476eb504084e4 100644 --- a/arch/x86/kernel/dumpstack.c +++ b/arch/x86/kernel/dumpstack.c @@ -177,6 +177,12 @@ static void show_regs_if_on_stack(struct stack_info *info, struct pt_regs *regs, } } +/* + * This function reads pointers from the stack and dereferences them. The + * pointers may not have their KMSAN shadow set up properly, which may result + * in false positive reports. Disable instrumentation to avoid those. + */ +__no_kmsan_checks static void show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs, unsigned long *stack, const char *log_lvl) { diff --git a/arch/x86/kernel/unwind_frame.c b/arch/x86/kernel/unwind_frame.c index 8e1c50c86e5db..d8ba93778ae32 100644 --- a/arch/x86/kernel/unwind_frame.c +++ b/arch/x86/kernel/unwind_frame.c @@ -183,6 +183,16 @@ static struct pt_regs *decode_frame_pointer(unsigned long *bp) } #endif +/* + * While walking the stack, KMSAN may stomp on stale locals from other + * functions that were marked as uninitialized upon function exit, and + * now hold the call frame information for the current function (e.g. the frame + * pointer). Because KMSAN does not specifically mark call frames as + * initialized, false positive reports are possible. To prevent such reports, + * we mark the functions scanning the stack (here and below) with + * __no_kmsan_checks. + */ +__no_kmsan_checks static bool update_stack_state(struct unwind_state *state, unsigned long *next_bp) { @@ -250,6 +260,7 @@ static bool update_stack_state(struct unwind_state *state, return true; } +__no_kmsan_checks bool unwind_next_frame(struct unwind_state *state) { struct pt_regs *regs; From patchwork Fri Aug 26 15:08:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956214 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 34293ECAAD4 for ; Fri, 26 Aug 2022 15:10:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C0789940015; Fri, 26 Aug 2022 11:10:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B8EC7940007; Fri, 26 Aug 2022 11:10:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9E1E5940015; Fri, 26 Aug 2022 11:10:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 8C666940007 for ; Fri, 26 Aug 2022 11:10:09 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 641D980E3C for ; Fri, 26 Aug 2022 15:10:09 +0000 (UTC) X-FDA: 79842079338.23.6CD63DA Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf24.hostedemail.com (Postfix) with ESMTP id 2609F18001F for ; Fri, 26 Aug 2022 15:10:08 +0000 (UTC) Received: by mail-ed1-f73.google.com with SMTP id p4-20020a056402500400b00447e8b6f62bso1237627eda.17 for ; Fri, 26 Aug 2022 08:10:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=VDn7tLtoTqUri5wCNae2jgpDMB9XRf/m0RZ2a2IuTLU=; b=XuSgIJWo5MH1NG3rWGo/F6MDltFcf2qPl6MCg/lTmi6AVs/W3AFN0L3RCnMqTlX9lr dKd/GckQv5Z6rSA+ayYP2KyxzcAyFxigMT3ptZSUl+gwPWVJSY9aubJhhTBy8zE+VVZx CGTMVKUA67P93/ZY15+zBfj4PaUpI6K4dk8spYaiA6AzXLDnbdicLnYpwutgnjjkOzyS hnFo1lTrpffm0Zz8iscFAwJfrYozG7CPI8rmN9qs3VLS/3azTHfOe6oXTOh5+hkHW5Bu RpNeHbpB3NC1YbvSkhEm3j/KxYQQRtFq+lVJn9jc6zvqweY5eCiiYGU2uk30ec6mXLQI dUWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=VDn7tLtoTqUri5wCNae2jgpDMB9XRf/m0RZ2a2IuTLU=; b=72fMcX1pxMik1t4ZJNqkb8aoIMa6Rlt6HY2bGlacGWSYH68fHZLrj+tuNoHjBzaUKs VPTa6jitfrJ0fzMs72fJ4TyDnOMDdY2ht/2U/LojFwL/G26VGIX6HzIMhj5lS3ON8VgL LodPLxu9WQ8nI7nSkOcqGG7vhRInvIxtaJDGCoofkcWJ8JBO+lghh0TgBFVXGkNbXriP 1kj77xH9hJXe/pcOalMfasDLjFUWnZe5Sb6Sl374GgJUsr8/f+743zmnwwNmeiIPUG46 8XcZlRLSGHdvaC3wFAufdQ4FVpZ+PIJxS8F6cm5/510WQvGWtd7t3QmT2g918sgSQ4kz dRtQ== X-Gm-Message-State: ACgBeo0RZ2Y+06kDBb9Fe0oyZ+pwCBoNTtNcRlnm9PmjT2863HtjyblE gdNng6SxHKjXNDJoDQqbUhu6mUVfL2I= X-Google-Smtp-Source: AA6agR7Aswd52JiieVR1uDzHxTIsNC8FPiPrY6f0RXtM2U6YqiEtATQBhicPX6UGtVM3aylaikgOQRe7gwE= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a05:6402:1f87:b0:43b:b88d:1d93 with SMTP id c7-20020a0564021f8700b0043bb88d1d93mr7088766edc.314.1661526607921; Fri, 26 Aug 2022 08:10:07 -0700 (PDT) Date: Fri, 26 Aug 2022 17:08:04 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-42-glider@google.com> Subject: [PATCH v5 41/44] entry: kmsan: introduce kmsan_unpoison_entry_regs() From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=XuSgIJWo; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf24.hostedemail.com: domain of 3T-IIYwYKCFY49612F4CC492.0CA96BIL-AA8Jy08.CF4@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3T-IIYwYKCFY49612F4CC492.0CA96BIL-AA8Jy08.CF4@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526609; a=rsa-sha256; cv=none; b=ESEFopQFg+vSO1yOOy8NkXi8JUYaO9L1X2ZG+93YolrFIYYXUOKZSE3vJgwSrS/Xni+s7B MgOfiJur3sQKPbCJAkkmPhq0S6Ji95ibbmyqnqYU/74aLdjDUPkBvbzwIZ2AVDtBYLDz/y Dks2BtSZ0n7MuPaYByp2ApIcTuRnoD4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526609; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=VDn7tLtoTqUri5wCNae2jgpDMB9XRf/m0RZ2a2IuTLU=; b=hQN7RCbzTU7nL6YsY9vCpTeOHGTheiDrMYN+DzllDLR1WTKUelQwAf9439FI0dnJdi/CNx oFj1yZbbHpCMeu1Hv3HIMfRccYn+yyspiL4lv99oAWh7iBGD1JYi2AnN1xsStpr8PObOWC Qx1AQMNc0NnHRTVoUKKyjqH500pLipQ= X-Rspam-User: X-Rspamd-Queue-Id: 2609F18001F Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=XuSgIJWo; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf24.hostedemail.com: domain of 3T-IIYwYKCFY49612F4CC492.0CA96BIL-AA8Jy08.CF4@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3T-IIYwYKCFY49612F4CC492.0CA96BIL-AA8Jy08.CF4@flex--glider.bounces.google.com X-Stat-Signature: tjkqt4w53cwgj6zd65tpghf5uyz7t49o X-Rspamd-Server: rspam03 X-HE-Tag: 1661526608-780238 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: struct pt_regs passed into IRQ entry code is set up by uninstrumented asm functions, therefore KMSAN may not notice the registers are initialized. kmsan_unpoison_entry_regs() unpoisons the contents of struct pt_regs, preventing potential false positives. Unlike kmsan_unpoison_memory(), it can be called under kmsan_in_runtime(), which is often the case in IRQ entry code. Signed-off-by: Alexander Potapenko --- Link: https://linux-review.googlesource.com/id/Ibfd7018ac847fd8e5491681f508ba5d14e4669cf --- include/linux/kmsan.h | 15 +++++++++++++++ kernel/entry/common.c | 5 +++++ mm/kmsan/hooks.c | 26 ++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h index 84dddf3aa5f8b..f4015a7546e39 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -238,6 +238,17 @@ void kmsan_handle_dma_sg(struct scatterlist *sg, int nents, */ void kmsan_handle_urb(const struct urb *urb, bool is_out); +/** + * kmsan_unpoison_entry_regs() - Handle pt_regs in low-level entry code. + * @regs: struct pt_regs pointer received from assembly code. + * + * KMSAN unpoisons the contents of the passed pt_regs, preventing potential + * false positive reports. Unlike kmsan_unpoison_memory(), + * kmsan_unpoison_entry_regs() can be called from the regions where + * kmsan_in_runtime() returns true, which is the case in early entry code. + */ +void kmsan_unpoison_entry_regs(const struct pt_regs *regs); + #else static inline void kmsan_init_shadow(void) @@ -334,6 +345,10 @@ static inline void kmsan_handle_urb(const struct urb *urb, bool is_out) { } +static inline void kmsan_unpoison_entry_regs(const struct pt_regs *regs) +{ +} + #endif #endif /* _LINUX_KMSAN_H */ diff --git a/kernel/entry/common.c b/kernel/entry/common.c index 063068a9ea9b3..846add8394c41 100644 --- a/kernel/entry/common.c +++ b/kernel/entry/common.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -24,6 +25,7 @@ static __always_inline void __enter_from_user_mode(struct pt_regs *regs) user_exit_irqoff(); instrumentation_begin(); + kmsan_unpoison_entry_regs(regs); trace_hardirqs_off_finish(); instrumentation_end(); } @@ -352,6 +354,7 @@ noinstr irqentry_state_t irqentry_enter(struct pt_regs *regs) lockdep_hardirqs_off(CALLER_ADDR0); ct_irq_enter(); instrumentation_begin(); + kmsan_unpoison_entry_regs(regs); trace_hardirqs_off_finish(); instrumentation_end(); @@ -367,6 +370,7 @@ noinstr irqentry_state_t irqentry_enter(struct pt_regs *regs) */ lockdep_hardirqs_off(CALLER_ADDR0); instrumentation_begin(); + kmsan_unpoison_entry_regs(regs); rcu_irq_enter_check_tick(); trace_hardirqs_off_finish(); instrumentation_end(); @@ -452,6 +456,7 @@ irqentry_state_t noinstr irqentry_nmi_enter(struct pt_regs *regs) ct_nmi_enter(); instrumentation_begin(); + kmsan_unpoison_entry_regs(regs); trace_hardirqs_off_finish(); ftrace_nmi_enter(); instrumentation_end(); diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c index 58334fa32ff86..14d6c78a793b8 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -347,6 +347,32 @@ void kmsan_unpoison_memory(const void *address, size_t size) } EXPORT_SYMBOL(kmsan_unpoison_memory); +/* + * Version of kmsan_unpoison_memory() that can be called from within the KMSAN + * runtime. + * + * Non-instrumented IRQ entry functions receive struct pt_regs from assembly + * code. Those regs need to be unpoisoned, otherwise using them will result in + * false positives. + * Using kmsan_unpoison_memory() is not an option in entry code, because the + * return value of in_task() is inconsistent - as a result, certain calls to + * kmsan_unpoison_memory() are ignored. kmsan_unpoison_entry_regs() ensures that + * the registers are unpoisoned even if kmsan_in_runtime() is true in the early + * entry code. + */ +void kmsan_unpoison_entry_regs(const struct pt_regs *regs) +{ + unsigned long ua_flags; + + if (!kmsan_enabled) + return; + + ua_flags = user_access_save(); + kmsan_internal_unpoison_memory((void *)regs, sizeof(*regs), + KMSAN_POISON_NOCHECK); + user_access_restore(ua_flags); +} + void kmsan_check_memory(const void *addr, size_t size) { if (!kmsan_enabled) From patchwork Fri Aug 26 15:08:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956215 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 CE4B0ECAAA3 for ; Fri, 26 Aug 2022 15:10:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 74BE2940016; Fri, 26 Aug 2022 11:10:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6D3D1940007; Fri, 26 Aug 2022 11:10:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 528F9940016; Fri, 26 Aug 2022 11:10:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 3AD27940007 for ; Fri, 26 Aug 2022 11:10:12 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 1E942AC311 for ; Fri, 26 Aug 2022 15:10:12 +0000 (UTC) X-FDA: 79842079464.24.E3FC162 Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) by imf10.hostedemail.com (Postfix) with ESMTP id CDB67C001E for ; Fri, 26 Aug 2022 15:10:11 +0000 (UTC) Received: by mail-ej1-f73.google.com with SMTP id mp41-20020a1709071b2900b0073d84b11981so731777ejc.23 for ; Fri, 26 Aug 2022 08:10:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=rCTBedHFoD5mjPc5axfz3IhJFNkE8btw4kiDfQuZoWM=; b=fg/g7W5SFXhbc3u2Ka10S6sfG+hleQgHtIujYd0emCXLuaRjW/Lp6/k4/2cWo7l/yp VDQQl06jPUO4V5hkPUjOfNuax5shhJaKnbpXyd21jWZdCRxSmNTw/c5xxG3zrihbz+OE Ljd50zcy2oKUZWQBLmYWsMOuW6qxf6tCX2W3+KQJUFv3PXm77b5Ak7Io2QiMeQ/oK9vK ZD8Tqs0aoGX8nDwtNuhyBXUzYeCZIaWWl/08zjdMFlG1dJKaN0WeEcKRi8ejkQvW4T1P wvM92OSrEzTbGCfmQLEw8aa8qLoI04nx9tn9Pj1TgR/4CFpWvS9JnaF3HfLp1oJuw/pj zqCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=rCTBedHFoD5mjPc5axfz3IhJFNkE8btw4kiDfQuZoWM=; b=DELRQH87VK4CQAWresBkbbyJWl1SV8Z6Bp/SiGpGhtY/jPtWTvd1qIdgR9DS/uh7ws TiS9/agP8P8WjFjkJFa6+VQdYsxgs66MOi4kJei47GbRbEVys6X3Pk3gPNgQ+XXKQNep 6kV5+zaEZIaAiVhDo59+tFwWuChQ2XfXLvTIrgMIxZS7nlC+4GwG7M7qGt0EGC+hhols D+dkmeWMrL5Qw5TZt5uO4VgaYsuQz4GeUlbLsK045u89cZCSMwE1KvVkMxQoKd15urR6 /PEwo5XwKTdxqxdWxyKnERU/3iF1jN5WQ9tVHGx8PiQK6ZIpaV77C4PRxq67CMLWeCmS BvaA== X-Gm-Message-State: ACgBeo0qMqQvsGAaDcSPV9uW9FMposZe+yCmFfBEwBMBuwjT/19fXeAX XN6rmSn4SnmV62iaVau+eAZ0e/vtLHQ= X-Google-Smtp-Source: AA6agR7tXjpYxk8MsRplqzj3thTzocuzhsrofVe2ZMXjeqhyHHZW7zWMaS3lAs0uR1Cb77CsZjqkIFO/xsw= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a05:6402:2816:b0:434:ed38:16f3 with SMTP id h22-20020a056402281600b00434ed3816f3mr7084895ede.116.1661526610607; Fri, 26 Aug 2022 08:10:10 -0700 (PDT) Date: Fri, 26 Aug 2022 17:08:05 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-43-glider@google.com> Subject: [PATCH v5 42/44] bpf: kmsan: initialize BPF registers with zeroes From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526611; a=rsa-sha256; cv=none; b=ApIyhbOh6geHwy1QP4cMG7J73LqtRwB5LrnKQEb2mbbgZtvpqFKejo5/rCGR+rB+9/0LcZ 9lgvttF3qLwrgB/pdBJHr468x3lG5VlIjEuHq32OMMR2kJqj2+WTzmfSQ0eF3uAoGsNw0l wJDzybJHeoY0WAoq/ogMF+lBDaOEJQc= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="fg/g7W5S"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of 3UuIIYwYKCFk7C945I7FF7C5.3FDC9ELO-DDBM13B.FI7@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3UuIIYwYKCFk7C945I7FF7C5.3FDC9ELO-DDBM13B.FI7@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526611; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=rCTBedHFoD5mjPc5axfz3IhJFNkE8btw4kiDfQuZoWM=; b=2EjEi3f/Gn5c1C2oyOFEbRGRjgZrrxbCmkYDpcLR6hZIBcKeA/Y6mu3xANBTFuuIHU/85K MdPtQDpkpEK4D1jQ1ETbi4wcbcwbXy45oHUY7HC3BVP80MvDhe/agedI1CUfwR7uY261dz Xux+80anCx7d51RuiMTfeJvaGl307Jk= X-Rspamd-Queue-Id: CDB67C001E Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="fg/g7W5S"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of 3UuIIYwYKCFk7C945I7FF7C5.3FDC9ELO-DDBM13B.FI7@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3UuIIYwYKCFk7C945I7FF7C5.3FDC9ELO-DDBM13B.FI7@flex--glider.bounces.google.com X-Rspamd-Server: rspam12 X-Rspam-User: X-Stat-Signature: o6kjsgywcidu3pumsyk8wfbzs6k7316i X-HE-Tag: 1661526611-192659 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: When executing BPF programs, certain registers may get passed uninitialized to helper functions. E.g. when performing a JMP_CALL, registers BPF_R1-BPF_R5 are always passed to the helper, no matter how many of them are actually used. Passing uninitialized values as function parameters is technically undefined behavior, so we work around it by always initializing the registers. Signed-off-by: Alexander Potapenko --- Link: https://linux-review.googlesource.com/id/I8ef9dbe94724cee5ad1e3a162f2b805345bc0586 --- kernel/bpf/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index c1e10d088dbb7..547d139ab98af 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -2002,7 +2002,7 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn) static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \ { \ u64 stack[stack_size / sizeof(u64)]; \ - u64 regs[MAX_BPF_EXT_REG]; \ + u64 regs[MAX_BPF_EXT_REG] = {}; \ \ FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \ ARG1 = (u64) (unsigned long) ctx; \ From patchwork Fri Aug 26 15:08:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956216 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 DBD31ECAAA3 for ; Fri, 26 Aug 2022 15:10:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7C17F940017; Fri, 26 Aug 2022 11:10:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 74954940007; Fri, 26 Aug 2022 11:10:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5758F940017; Fri, 26 Aug 2022 11:10:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 41454940007 for ; Fri, 26 Aug 2022 11:10:15 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 13B46C06B3 for ; Fri, 26 Aug 2022 15:10:15 +0000 (UTC) X-FDA: 79842079590.30.AB66D36 Received: from mail-lj1-f201.google.com (mail-lj1-f201.google.com [209.85.208.201]) by imf23.hostedemail.com (Postfix) with ESMTP id B5BB514000E for ; Fri, 26 Aug 2022 15:10:14 +0000 (UTC) Received: by mail-lj1-f201.google.com with SMTP id e1-20020a2e9841000000b002602ebb584fso662014ljj.14 for ; Fri, 26 Aug 2022 08:10:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=dGjdyFKcPZ8AZlzw9ep1EXgYBHGgW0m7GguSz7kBI6Q=; b=DZGsI9l3W9MQLA+ou9XdSB4IZE46ComuEd6rS14iWZ0xSDfAJ+BWwB8A7M05fO5zjT Wh7mYAH6WAAzh+FI/uVm5Ljcf9NEeu2Z9UZM+4qfZzFoj+6Fh/tecr4Yol7TBl6MiU/L tCTqBg9m2iJte3DzNqf8V7ws94Br5Wtp68vVWk7plrk6xmxEjPcPZVSvIrk/LnFF/INL 93xvlWF5Ha1xHXpbxLntxctwzalog3vxzBxrVMuGDkqbsYK5RzyESy+/nuRwFZ41sSYu t2owLKhR+yzd+pCanHFbrVCWUmN1zsn8mhRcH/6ROVVHuRMuA9Jhn32qCWbGf06hi10p XV8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=dGjdyFKcPZ8AZlzw9ep1EXgYBHGgW0m7GguSz7kBI6Q=; b=HhXRga5lz3AepNN9jGlYlEE9huIVzfdVlHlpghfTrkhTPmMnxog8FHBrm60AsIQ0nV r/Sk483wsQYSX9bhjcUUMx5+JW3ILUMlZfNyjohDr3sYvmSVYlzAWY6FIwbwJxo4GB7V 6ReNOrERYKTSlOnw1QAzgmHN1KShdjOdOAkFkKCY187qfI1T8vTzx27nHMEEqfmmvc8I khYCzGx/gc+6vReD2opoAKpZmLatWr3E9HKhggqZVAt2LIKXAzBcUc2v4DgmahYej5P5 U1EJxJWshdPvCnahH+nAPI1uLcfOIoGoznDfFp0wXR1Z6tGffEUq9FQa9oM0ISjwMIoN wyKg== X-Gm-Message-State: ACgBeo1WNWizVLZhkwh6XDIxK7Q9IC2t20GvUtKoWoPj2uVCaoW554FY RWkNvPcX0Oz6aigqJ2MuOxW9JvdOanE= X-Google-Smtp-Source: AA6agR5+Ww/s7u4XD1azUk5I5ZYmSipZxiExxoopJysRcyPZ8zjk++GJnOf429p9ygUYKujEBktJ1Rf+Pzg= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a05:6512:3054:b0:48a:f489:1d68 with SMTP id b20-20020a056512305400b0048af4891d68mr2401202lfb.260.1661526613226; Fri, 26 Aug 2022 08:10:13 -0700 (PDT) Date: Fri, 26 Aug 2022 17:08:06 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-44-glider@google.com> Subject: [PATCH v5 43/44] mm: fs: initialize fsdata passed to write_begin/write_end interface From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=DZGsI9l3; spf=pass (imf23.hostedemail.com: domain of 3VeIIYwYKCFwAFC78LAIIAF8.6IGFCHOR-GGEP46E.ILA@flex--glider.bounces.google.com designates 209.85.208.201 as permitted sender) smtp.mailfrom=3VeIIYwYKCFwAFC78LAIIAF8.6IGFCHOR-GGEP46E.ILA@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526614; a=rsa-sha256; cv=none; b=W5EtS/L8oJNIYAb++rsiJGmMG9F4nH4Tcc5iciU2uGe/uM1CYL1497olrt51RmEy02/e4z /0A4pKaAywu9Mg/BGCnUvWIVsfC3AoVdCnJBrtpJBNe9GgMGfIDf7V3wc0vnj7WPf3jYQx CPCMye7GH/zIaSlzELIGBJiTnUu0mdA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526614; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=dGjdyFKcPZ8AZlzw9ep1EXgYBHGgW0m7GguSz7kBI6Q=; b=jChbddR25b8aQ4ntHnGU2mKROQ9i5Vmgyy7qeG+WI0zSYqGITHzfZu6Pz1o5UsaUw4htet DQC7aF476M3tkoLYzweZI5XpakH2x4flbPPQasmoa7a/5X37T5cA7W+f0kcg4JaBXNZtp1 cwLhIO4CW/zS4Y2VrZtuBqKJkWZ+qoE= X-Rspam-User: X-Stat-Signature: ufpne9s9fzfnz9zwahwewdawz9944grc X-Rspamd-Queue-Id: B5BB514000E Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=DZGsI9l3; spf=pass (imf23.hostedemail.com: domain of 3VeIIYwYKCFwAFC78LAIIAF8.6IGFCHOR-GGEP46E.ILA@flex--glider.bounces.google.com designates 209.85.208.201 as permitted sender) smtp.mailfrom=3VeIIYwYKCFwAFC78LAIIAF8.6IGFCHOR-GGEP46E.ILA@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam09 X-HE-Tag: 1661526614-438443 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: Functions implementing the a_ops->write_end() interface accept the `void *fsdata` parameter that is supposed to be initialized by the corresponding a_ops->write_begin() (which accepts `void **fsdata`). However not all a_ops->write_begin() implementations initialize `fsdata` unconditionally, so it may get passed uninitialized to a_ops->write_end(), resulting in undefined behavior. Fix this by initializing fsdata with NULL before the call to write_begin(), rather than doing so in all possible a_ops implementations. This patch covers only the following cases found by running x86 KMSAN under syzkaller: - generic_perform_write() - cont_expand_zero() and generic_cont_expand_simple() - page_symlink() Other cases of passing uninitialized fsdata may persist in the codebase. Signed-off-by: Alexander Potapenko --- Link: https://linux-review.googlesource.com/id/Ie300c21bbe9dea69a730745bd3c6d2720953bf41 --- fs/buffer.c | 4 ++-- fs/namei.c | 2 +- mm/filemap.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/fs/buffer.c b/fs/buffer.c index 55e762a58eb65..e1198f4b28c8f 100644 --- a/fs/buffer.c +++ b/fs/buffer.c @@ -2352,7 +2352,7 @@ int generic_cont_expand_simple(struct inode *inode, loff_t size) struct address_space *mapping = inode->i_mapping; const struct address_space_operations *aops = mapping->a_ops; struct page *page; - void *fsdata; + void *fsdata = NULL; int err; err = inode_newsize_ok(inode, size); @@ -2378,7 +2378,7 @@ static int cont_expand_zero(struct file *file, struct address_space *mapping, const struct address_space_operations *aops = mapping->a_ops; unsigned int blocksize = i_blocksize(inode); struct page *page; - void *fsdata; + void *fsdata = NULL; pgoff_t index, curidx; loff_t curpos; unsigned zerofrom, offset, len; diff --git a/fs/namei.c b/fs/namei.c index 53b4bc094db23..076ae96ca0b14 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -5088,7 +5088,7 @@ int page_symlink(struct inode *inode, const char *symname, int len) const struct address_space_operations *aops = mapping->a_ops; bool nofs = !mapping_gfp_constraint(mapping, __GFP_FS); struct page *page; - void *fsdata; + void *fsdata = NULL; int err; unsigned int flags; diff --git a/mm/filemap.c b/mm/filemap.c index 15800334147b3..ada25b9f45ad1 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3712,7 +3712,7 @@ ssize_t generic_perform_write(struct kiocb *iocb, struct iov_iter *i) unsigned long offset; /* Offset into pagecache page */ unsigned long bytes; /* Bytes to write to page */ size_t copied; /* Bytes copied from user */ - void *fsdata; + void *fsdata = NULL; offset = (pos & (PAGE_SIZE - 1)); bytes = min_t(unsigned long, PAGE_SIZE - offset, From patchwork Fri Aug 26 15:08:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956217 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 805EEECAAA3 for ; Fri, 26 Aug 2022 15:10:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1CDAF940018; Fri, 26 Aug 2022 11:10:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1567D940007; Fri, 26 Aug 2022 11:10:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EEB0B940018; Fri, 26 Aug 2022 11:10:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id D8ED9940007 for ; Fri, 26 Aug 2022 11:10:17 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id B67A840593 for ; Fri, 26 Aug 2022 15:10:17 +0000 (UTC) X-FDA: 79842079674.20.95F456B Received: from mail-lj1-f201.google.com (mail-lj1-f201.google.com [209.85.208.201]) by imf14.hostedemail.com (Postfix) with ESMTP id 527D6100022 for ; Fri, 26 Aug 2022 15:10:17 +0000 (UTC) Received: by mail-lj1-f201.google.com with SMTP id z11-20020a2eb52b000000b00261d940ce36so655843ljm.9 for ; Fri, 26 Aug 2022 08:10:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=fxlXGK8qL+YVfFcW6ClvjL4YS8Ri9qaHIYNR2qoUuzA=; b=Q8VLjQ5xsWoRQwcBX26q4ZfJywgvwwYMqm7WRIZT/igOO0C+hQHvWJdj6bKE/htghx DrY+lrNXNEbosReTbTCbvhlRqJK2QXAnzf9wyBVGEBrx0xxr82tqOecv4nBtt+ZxQ/yz olXwbcf4S8A0zqAksvcEzulatqIcfYttRp+CQJcmIHJmyH6BPQSBoduAFmbbmZi1yVjp Tg6Bb8K5bppJxRqKIw0j6if6T//cGZVzeA7QuH2nxxvPggC9uSXoPvQi5rO7W7h+JR7N w+bOFVhYur+hDrR+w9sC1KCjIT/m0VnGKDSplsMs4+EUNYFwUHkEBbvxDpMspo6USKZQ VmLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=fxlXGK8qL+YVfFcW6ClvjL4YS8Ri9qaHIYNR2qoUuzA=; b=Wy5QWxcR3D3hXQkTAs2C+JAXiND8s+GAY4pdJfb/WBRnBwTssLEdVKduwJWZbGcF4B HDWT1n2rsTKcpN1SmOgm0E8uPcRDFzVd6fis+JTCbevf0O7BCndYfmWW0Pyk08/n3m0y 0t+0fR5ytp/jeu9IKNfIjLamHOp1yMksSCqyFhKLliHgBTiWgIvSs0Zlzh0WuBVlC0S4 zA9+GWUbydUhJyrVwYefRQXbF20SsRbbPcSHakjCA55GMRUMKCoj5zOG+UzDbkqY96iJ bzblBD2aFws6FG38ovNVs5tNHycMMFri364JMw0hUtkKHMpq0KDQ5ewIV4WJ2d74mZtl a3XQ== X-Gm-Message-State: ACgBeo36idZt5AIxQbRNC7DC+QenoTZCyE3DmxLs8dIfkMaa1yCRmi+v RlJ/jXfzTWmos7fADpjstQCNZ2KzGYE= X-Google-Smtp-Source: AA6agR4ts2QVsl2UWCVwazOIhjWaaZlRudAXUf4/lJNteJtVBKT5mB6+2t9fiOdhKBlLEptewn0gZtbEsjs= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a19:e012:0:b0:492:bec1:7f9d with SMTP id x18-20020a19e012000000b00492bec17f9dmr2498937lfg.585.1661526615782; Fri, 26 Aug 2022 08:10:15 -0700 (PDT) Date: Fri, 26 Aug 2022 17:08:07 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-45-glider@google.com> Subject: [PATCH v5 44/44] x86: kmsan: enable KMSAN builds for x86 From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526617; a=rsa-sha256; cv=none; b=VrBi+Dod7TpRLZkk+iHlWJklvVQwJhEPLseaDxWEN77woxhxu5+ta0Xw5Ge2VmNLJakmZk V9a31uGKJsBJ87KIPrID0N5N7bvJmg6SykJGlxeeZ/CkKzvFPfzpcfLaOn+eaWV9+bcgvu UXiiFhf9W0+JuhUv9xhOUtYiDqdGhSc= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Q8VLjQ5x; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf14.hostedemail.com: domain of 3V-IIYwYKCF4CHE9ANCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--glider.bounces.google.com designates 209.85.208.201 as permitted sender) smtp.mailfrom=3V-IIYwYKCF4CHE9ANCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526617; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=fxlXGK8qL+YVfFcW6ClvjL4YS8Ri9qaHIYNR2qoUuzA=; b=oZAfPotbTdvRg9LdOOkirlMqZbOIsxZJHlaHm+1W3wNNDj2739wA9D4ng4DbUdqcfd/zlD p5u+fEJ4QMX6vND4DEA9MZXAJvZm9vuWw4tG64dv7M+AQyRmM5TO6XY+Gp7jf5s6w0lnnt 6EuS0pNw2DdxL6ER9Ff8n3MKVt+vLag= X-Rspamd-Queue-Id: 527D6100022 Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Q8VLjQ5x; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf14.hostedemail.com: domain of 3V-IIYwYKCF4CHE9ANCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--glider.bounces.google.com designates 209.85.208.201 as permitted sender) smtp.mailfrom=3V-IIYwYKCF4CHE9ANCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--glider.bounces.google.com X-Rspamd-Server: rspam12 X-Rspam-User: X-Stat-Signature: haogdyyj55ta1yxcgj5sboj4fc9fzbit X-HE-Tag: 1661526617-177795 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: Make KMSAN usable by adding the necessary Kconfig bits. Also declare x86-specific functions checking address validity in arch/x86/include/asm/kmsan.h. Signed-off-by: Alexander Potapenko --- v4: -- per Marco Elver's request, create arch/x86/include/asm/kmsan.h and move arch-specific inline functions there. Link: https://linux-review.googlesource.com/id/I1d295ce8159ce15faa496d20089d953a919c125e --- arch/x86/Kconfig | 1 + arch/x86/include/asm/kmsan.h | 55 ++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 arch/x86/include/asm/kmsan.h diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 697da8dae1418..bd9436cd0f29b 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -168,6 +168,7 @@ config X86 select HAVE_ARCH_KASAN if X86_64 select HAVE_ARCH_KASAN_VMALLOC if X86_64 select HAVE_ARCH_KFENCE + select HAVE_ARCH_KMSAN if X86_64 select HAVE_ARCH_KGDB select HAVE_ARCH_MMAP_RND_BITS if MMU select HAVE_ARCH_MMAP_RND_COMPAT_BITS if MMU && COMPAT diff --git a/arch/x86/include/asm/kmsan.h b/arch/x86/include/asm/kmsan.h new file mode 100644 index 0000000000000..a790b865d0a68 --- /dev/null +++ b/arch/x86/include/asm/kmsan.h @@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * x86 KMSAN support. + * + * Copyright (C) 2022, Google LLC + * Author: Alexander Potapenko + */ + +#ifndef _ASM_X86_KMSAN_H +#define _ASM_X86_KMSAN_H + +#ifndef MODULE + +#include +#include + +/* + * Taken from arch/x86/mm/physaddr.h to avoid using an instrumented version. + */ +static inline bool kmsan_phys_addr_valid(unsigned long addr) +{ + if (IS_ENABLED(CONFIG_PHYS_ADDR_T_64BIT)) + return !(addr >> boot_cpu_data.x86_phys_bits); + else + return true; +} + +/* + * Taken from arch/x86/mm/physaddr.c to avoid using an instrumented version. + */ +static inline bool kmsan_virt_addr_valid(void *addr) +{ + unsigned long x = (unsigned long)addr; + unsigned long y = x - __START_KERNEL_map; + + /* use the carry flag to determine if x was < __START_KERNEL_map */ + if (unlikely(x > y)) { + x = y + phys_base; + + if (y >= KERNEL_IMAGE_SIZE) + return false; + } else { + x = y + (__START_KERNEL_map - PAGE_OFFSET); + + /* carry flag will be set if starting x was >= PAGE_OFFSET */ + if ((x > y) || !kmsan_phys_addr_valid(x)) + return false; + } + + return pfn_valid(x >> PAGE_SHIFT); +} + +#endif /* !MODULE */ + +#endif /* _ASM_X86_KMSAN_H */