From patchwork Thu Sep 15 15:03: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: 12977519 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 D686BC6FA89 for ; Thu, 15 Sep 2022 15:04:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2A4528D0002; Thu, 15 Sep 2022 11:04:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 253B76B0075; Thu, 15 Sep 2022 11:04:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0F5908D0002; Thu, 15 Sep 2022 11:04:43 -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 020336B0074 for ; Thu, 15 Sep 2022 11:04:43 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id D329BC0124 for ; Thu, 15 Sep 2022 15:04:42 +0000 (UTC) X-FDA: 79914641604.19.9D8B88F Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf31.hostedemail.com (Postfix) with ESMTP id 5D285200C7 for ; Thu, 15 Sep 2022 15:04:41 +0000 (UTC) Received: by mail-ed1-f73.google.com with SMTP id dz21-20020a0564021d5500b0045217702048so7866340edb.5 for ; Thu, 15 Sep 2022 08:04:41 -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:subject:date; bh=H32LKJ4QtRE8PW83scu/rnCN+O5l16ld3Egd+3MOYzM=; b=Z7D99KP/7kWnl5znToFpIf1yzLMq5Rl3cXYi/b07RUB4TDuaBOLLDYgebb40CGKG1L 1zCtTBptLWIIE71BCILie+2r2YL30vZTv1M14oRjoZ84j37Qthbws8lENFDUTapFncHJ GMI/2NU3SzeX/kyx6KcZupV7AUQ3Ko6hrXIbFcW2S2DctuOcOUUhJU0Oim0IOCNK3Slv Et5wtGOTIMIY7gHgozwd25lD6l/zq9vpBHfdJEacezGicwlEFZgMQl9USowxIbe0UN9j WdCBEXFJTDUn1l0slw3ikke8yuPA7OPiHXDy0H4dUNtcUVd45sUoy/2pJE+/EkYb0XKl u/lw== 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:subject:date; bh=H32LKJ4QtRE8PW83scu/rnCN+O5l16ld3Egd+3MOYzM=; b=fUrpAaFsnU/GHk5/Enq3MSLwipXNAIBAB+VFyUN8eUCEATG49/ZiatPiEycFIkeNEd 85zsa0FFSULUg7UAv/F7tKx+7zEGdDWbV/Unjep2fqOf6etuXgcWduGmdKwk0GDn87TB XwHaFfRDXYVBCRR5FwFCc+LZv8i1Djm9MvtST4awR7f36rGSi0+EfAeCip9OPtjDUrNY 5vYcn3IEsctAMGO7/CZaVmiRQrzZVe3EzV1xDUh4BRXwRAqthsvwOoPjmNFJrTuk7cyK nU/hFXfdcDohtIz5KkYrzazE4qrVxnyhArPuR3mYwlWZNDr102aa6NS1mTqZp7j2rZsQ 4+uA== X-Gm-Message-State: ACrzQf1roy4k4fHmh6JIFtwy2tlDuZSDvhirmzVcrSCbcL5JGYfDGg9y Jveg/tgi386of5nXcfLgBvW0tUyr9vM= X-Google-Smtp-Source: AMsMyM5VSJ+y4oAY1cflJUKFEYKa2sNeJCpwHsdxCsIQ76K48TJwXnK+HBA3EP0jw8zgl0gp6zWhdtm0Ih0= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a05:6402:5406:b0:452:1560:f9d4 with SMTP id ev6-20020a056402540600b004521560f9d4mr246547edb.333.1663254280120; Thu, 15 Sep 2022 08:04:40 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:35 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-2-glider@google.com> Subject: [PATCH v7 01/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254281; a=rsa-sha256; cv=none; b=O+Oeb8acM/C2q83S8S/nZQf0H+sRT79IblNlg6fw+Hw9h/kAtoj+R66Bp70tEHCHuyJ350 CbaUzCVE2/Qr9V1J4Epci8vljdzllgzPN33DQQf7eoCqVN3ehVyvrKjjr6trKKqhWtFQPo sqCzXtGvV1ocvTR2RxRlfav+UcEXrKw= ARC-Authentication-Results: i=1; imf31.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="Z7D99KP/"; spf=pass (imf31.hostedemail.com: domain of 3CD8jYwYKCDMVaXSTgVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3CD8jYwYKCDMVaXSTgVddVaT.RdbaXcjm-bbZkPRZ.dgV@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=1663254281; 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=H32LKJ4QtRE8PW83scu/rnCN+O5l16ld3Egd+3MOYzM=; b=XKenXb+T+HIGI/3HGYc6voIME9blpb+eNTFApEAFI9k1/bnqv//4Nhdjfr3Q2Xmk1co5KQ bPUU8AOqmDwUbpVALrRvYQ1HphU6PNjKbObADh51GvKl+jRkBU8Y2oIMoGOCo3LQ/neRZR GlZhHy6Z83wcxgOtcJQJXm1uSMXofvg= Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="Z7D99KP/"; spf=pass (imf31.hostedemail.com: domain of 3CD8jYwYKCDMVaXSTgVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3CD8jYwYKCDMVaXSTgVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: u4mjc1t3uqtpbdj7o4yr3fp9cpj8jg47 X-Rspamd-Queue-Id: 5D285200C7 X-HE-Tag: 1663254281-248678 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 Thu Sep 15 15:03: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: 12977520 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 7E126ECAAA1 for ; Thu, 15 Sep 2022 15:04:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 081CB80007; Thu, 15 Sep 2022 11:04:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 030CA6B0075; Thu, 15 Sep 2022 11:04:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DEF1780007; Thu, 15 Sep 2022 11:04:44 -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 CDB2C6B0074 for ; Thu, 15 Sep 2022 11:04:44 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id AB37F1C60F5 for ; Thu, 15 Sep 2022 15:04:44 +0000 (UTC) X-FDA: 79914641688.01.23EF126 Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf05.hostedemail.com (Postfix) with ESMTP id 3D6301000B9 for ; Thu, 15 Sep 2022 15:04:44 +0000 (UTC) Received: by mail-ed1-f73.google.com with SMTP id r11-20020a05640251cb00b004516feb8c09so10383327edd.10 for ; Thu, 15 Sep 2022 08:04: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:subject:date; bh=myl6nRKQ/2lBkAfAPAxtf2MFDBMrGN4kiGAEDqUjhHA=; b=gS6j99y09BMp9BDjMRS1RUcbMakKgpTeqeZX6PzPrFgNC6JUyxdSvKRCjzIAswJzFY RACNurD1UwAw++C5CP/8IVwFLhwKh6jwIU1EimPsdHz7lSOgoWBUNH7rd2WrZNpDm2OE SKlmmmiGBzBNa7fDj8PHiGO2iT7RaZ12zTCZWYSTNArLj8SV2RvSCJhna5aBD06OlEsK MYVW63jPXWNru9dTAIlZZzh9S7iv3j2jcZqjzbtOPGMy+DXVJjDMb9AzkTghV1DwnKI/ qubMAz4mMwtItFPbaU7zU0DcS9Lc1NXUE92xo2vdruTx6cGeJ7VV8XEa8cy4Yoam+vkP l4cQ== 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:subject:date; bh=myl6nRKQ/2lBkAfAPAxtf2MFDBMrGN4kiGAEDqUjhHA=; b=1e8diXM1aiGeLAjBQ6iHD11ScYuWmZxQIRS3xyLi1cII3m4qCBwCen39bF/e58Q2MM FAQrRwl2lMlEe77Y9PDhHsbkEeNa8jNClIpTcZMGbLwVfaNBoO3VidBVDsE9k/jEh8Bp LMPiQ+V1jqA2c8JOnMoEkT8Z7WW687OglKF0bj3IXB5ZaOVuCgHEIJ6E6wC2s8k9iX7y pxKVnRtmibFurXhWMmsu+UYU1eqRggAyWYTOGuZzU/NO7NjdGeJ5LTnCcJsbfOG5+z4Q vpjBsDrMwQLt/io5zrW40DLY0A7S7gKDwUjR2D+I5oXhFl1uZwdtDw3dk2QjO1ojAG8z WetA== X-Gm-Message-State: ACrzQf1mX1NTgBZqQBMkRPFoHP4Dy3eGAosYAED6IugMgcBzvyItnSzs Ki+QO2qeQg5/zhGqJbXGT6W5xFizPTg= X-Google-Smtp-Source: AMsMyM5cIaAUAU3AA+LWPRVGPrLc1LHdkwZUDXgPvqFuYO3BlghU78yjZTOSc74hno8rab0+nl/oMOdeVBQ= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a05:6402:43cc:b0:451:129e:1b1e with SMTP id p12-20020a05640243cc00b00451129e1b1emr273421edc.258.1663254282917; Thu, 15 Sep 2022 08:04:42 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:36 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-3-glider@google.com> Subject: [PATCH v7 02/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254284; 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=myl6nRKQ/2lBkAfAPAxtf2MFDBMrGN4kiGAEDqUjhHA=; b=blbo5ky3ebudg+RMYwC5yF4/MNxEtsI8Uk3swcKBVT6ssxBFFOQRvZJOEFY7S4s+6OVASw U5iTdfNBWv9D1VJ5jKv/0VTT4Hq3vRKyexhc5w/I+UJgFZhzfzxpYVR+H4pJ6f94pGrsr5 4YNrnDCY9h1JHwIp7U7eQxTCzqQ0Kjk= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=gS6j99y0; spf=pass (imf05.hostedemail.com: domain of 3Cj8jYwYKCDUXcZUViXffXcV.TfdcZelo-ddbmRTb.fiX@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3Cj8jYwYKCDUXcZUViXffXcV.TfdcZelo-ddbmRTb.fiX@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254284; a=rsa-sha256; cv=none; b=wtmbbnnCD49OnY7m1+EV+pTC8txdORWhqLbDf2uY5ehJ8JGGTMyA68BHfiiZyco/5zpM4u F14hQTrlojkR4JgmsCSOoTybh9T+lDu/BnGicjN7V8zTympc8V555IPGHx2oilsQl+U72L xqbd1xnQg5AYIGvzOhM2vts/FnfrLQY= X-Stat-Signature: 19z1dewf9ppq9bwq9h37qoy1qe4g7gk6 X-Rspamd-Queue-Id: 3D6301000B9 X-Rspam-User: Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=gS6j99y0; spf=pass (imf05.hostedemail.com: domain of 3Cj8jYwYKCDUXcZUViXffXcV.TfdcZelo-ddbmRTb.fiX@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3Cj8jYwYKCDUXcZUViXffXcV.TfdcZelo-ddbmRTb.fiX@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam06 X-HE-Tag: 1663254284-160223 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 Thu Sep 15 15:03: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: 12977521 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 001A9C6FA8D for ; Thu, 15 Sep 2022 15:04:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8244480008; Thu, 15 Sep 2022 11:04:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7ACA96B0075; Thu, 15 Sep 2022 11:04:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 672CD80008; Thu, 15 Sep 2022 11:04:48 -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 59C8B6B0074 for ; Thu, 15 Sep 2022 11:04:48 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 3DAFF808CD for ; Thu, 15 Sep 2022 15:04:48 +0000 (UTC) X-FDA: 79914641856.22.045B4E7 Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) by imf14.hostedemail.com (Postfix) with ESMTP id B9D421000B6 for ; Thu, 15 Sep 2022 15:04:47 +0000 (UTC) Received: by mail-ej1-f74.google.com with SMTP id du20-20020a17090772d400b0077df99c5ab3so5451084ejc.3 for ; Thu, 15 Sep 2022 08:04:47 -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:subject:date; bh=msgzHzsxvMZcLqG5jRq1WtDuSKgOL7FjSU3mDqU1uyE=; b=OJ1zZnEDisYf20/w2ufGsvf/4BmFJL6F+ZhkDMO9RSLgOWaUeOqgIZIEGBcs1xvjyE Vhi8oq1M5fuOo1BqRow0T2kkjWn7MFIiDl4Vxh0JvAvzMUcoBFNGV5OjD/1YNXh8Cb3e yBOZYjRifZkg/DWICVkMAwQ0rJNw34aIbPzK0IaH5U++++uKm/Nb/YWU7DG9jJPbW4je c+0D9D8GEACiFbk4kbys72G+E4/BBC6t7mtRJi/G2Ikv3nyuJnt9d8HjxthckZDQoevO /FUhBUjORIPB01uQTITnhxROg/kQNGDd3OFt2E1ajNcLp0VYOYpj5PP8IggJI7JbIss8 fOrA== 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:subject:date; bh=msgzHzsxvMZcLqG5jRq1WtDuSKgOL7FjSU3mDqU1uyE=; b=6oGUZ8iQPN/Cb4l4YkgUnFmdgDMnkZ6MMXvZ92Pl8CqZeuF7WNygGMRm269QfTSkZU IKxA5vvY3pUnEeK0lAJ1TKHSNztysTn5I7PahlJtrxOP93dAn+MzvDRnZPQxkUaj70Iy x9wiF0yMAfywEcu2z0t/rqb0LwW3mT5zz9RzGg5Rfa+AxIxFsAyJEQ+7ubIDcTWLOozu bNpPwRWuwTWAU38ceC4/tl+SwGuFdz9fBDHYgZEM3FpcpUUgrxnhdZb2Nppf3rxa3gzj NX/rCCXiNgnRNOtBaPOhOljXprc2uF8XAnvJSJD7zxHQUNoaPwcIMdeuicvx7PVeD4Sn oAWA== X-Gm-Message-State: ACrzQf1/k83Huyo6NbMm1UEfZJNk1iQI7+/DL8+CPpt0azWku779SMME 40eUp63aBxMh9iyn2cI2yESCjZgvRhU= X-Google-Smtp-Source: AMsMyM5UV2lppQQoT85C2x0ZnCpLHOXrW/G23K2oT1zkSuuxsEEabT6dnZVkKB+qN2mRnFLrLNuGLySnQgE= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a17:907:7612:b0:771:db66:7b77 with SMTP id jx18-20020a170907761200b00771db667b77mr309998ejc.228.1663254286414; Thu, 15 Sep 2022 08:04:46 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:37 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-4-glider@google.com> Subject: [PATCH v7 03/43] 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 Biggers , 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 , Stephen Rothwell , 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; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=OJ1zZnED; spf=pass (imf14.hostedemail.com: domain of 3Dj8jYwYKCDkbgdYZmbjjbgZ.Xjhgdips-hhfqVXf.jmb@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3Dj8jYwYKCDkbgdYZmbjjbgZ.Xjhgdips-hhfqVXf.jmb@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254287; a=rsa-sha256; cv=none; b=iItUdq/f9jJJTh8GLtacwD91YKfvcvOeLivZcO79NTA2S9ZcQdCYv+rxWLA94rI6EkhHFk nsZ6LuELLefmgdXb+GEGUFHY2Jjstf8E6GtiiWrHueb5zLrAN/704Ym2nG3b8UmSqe5hpz zIyWJOyTHnHPz27orbt0mhWQABCbzHI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663254287; 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=msgzHzsxvMZcLqG5jRq1WtDuSKgOL7FjSU3mDqU1uyE=; b=y8rJKI7iosuCkWoYM6N9DbtA9IuuFKJgc6Q205HdstHvrsR+DqMjAzD17ssa2vgo7z5kTX 2bQ9UQ/SexXpAnEgDObIcJkc9m9ITqLROkHp7COkPgATQU3Y9SzOMdRCBxO28xCvMmTT54 VAqavhwMKzPr9u5ZwhxpThdrZ+Mn6FM= X-Rspam-User: X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: B9D421000B6 Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=OJ1zZnED; spf=pass (imf14.hostedemail.com: domain of 3Dj8jYwYKCDkbgdYZmbjjbgZ.Xjhgdips-hhfqVXf.jmb@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3Dj8jYwYKCDkbgdYZmbjjbgZ.Xjhgdips-hhfqVXf.jmb@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: pituixbaiiwtzu4ptdb7frhk4s7bdjuk X-HE-Tag: 1663254287-204521 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 Thu Sep 15 15:03: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: 12977522 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 B70BCECAAA1 for ; Thu, 15 Sep 2022 15:04:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 255FC6B0074; Thu, 15 Sep 2022 11:04:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2061C80009; Thu, 15 Sep 2022 11:04:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 07F1F6B0078; Thu, 15 Sep 2022 11:04:51 -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 EDCE36B0074 for ; Thu, 15 Sep 2022 11:04:50 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id CAE7380126 for ; Thu, 15 Sep 2022 15:04:50 +0000 (UTC) X-FDA: 79914641940.06.201A815 Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf26.hostedemail.com (Postfix) with ESMTP id 79C451400D6 for ; Thu, 15 Sep 2022 15:04:50 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id y14-20020a056402440e00b0044301c7ccd9so13177142eda.19 for ; Thu, 15 Sep 2022 08:04:50 -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:subject:date; bh=gSZzwrXvhkj2wpolUx3YVTouxdXHw9PaTEDZ4n2e3I0=; b=EznsgfSiy9WifxPdZjYT1Vwj2wYB4AW4etiNBG8scEb5xiAuby2h1T+k5Dja+N3dWf S2duxflNmAPHuHcgocbmA96vZwFvrsgfdpnNtgj4uq+eTJxlt5j2bMabt1gCsRhCJDg0 AzJpKEbhLm7ml6bETgD4W/9UBTpmd8EsT/Am6VtGzrLBUEUuaNnF2fOKbJNN/Aff33pA czLuVMpyryuAc03jszny9BsfuzYr+hnv5Q0Y0MfzpdFRm1q9kcGSv2bsQlwBLHtxb+Ub wZPRS/RuAFlH1BM4U0m04fB6G24UfKuxoKx9lbER6yfzIS/q5yq4d5MQKvhKRZhxWaXW sOjg== 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:subject:date; bh=gSZzwrXvhkj2wpolUx3YVTouxdXHw9PaTEDZ4n2e3I0=; b=tf1fYkVDSu+4jncxtahkJqUBWKTC27mT1i01TOhq+qkA7ZBkCk48cgVud4xXBnSwKQ Y1F/DYotpdhayqBHQ0EXZt4zE9UZBhQf1L6eptUdNRf7R0A6PE6dmiWFqduHJLX9fwYF dof09OKG/A/r75GbQ97x1IySR4PHWFe+sXiQHpGXsQ6EBzAY6gxXCkCxJhc8+oaIq1mN bg6AIN55WOhc/7CfQA+iVy84Xs8HAOhLFR6U+/bcfUFl+Zqb4WIy3CyC2SRwLzTF9BRb CHEcSAkXywF0GE8m0NkrF4074TjHjvqFOuX6446NT+B7NnldLk7CtcixLnsDuEySrsXu VAbg== X-Gm-Message-State: ACrzQf3GQYJtcUJDIUg8TLG8AYjIdPfouk4X8mij6p4WYlEYdMWi6c/g Hr6/bximJnrU1t5t+Y1uqUNk9i2cjvw= X-Google-Smtp-Source: AMsMyM5aonhljpQzaVwxXn/+2WXKN29fLiAxHjU40s/VJeTy2/yz2WCgfXCD6zZKzAdUnorRQiOSr1kogeM= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a05:6402:c8a:b0:44e:81b3:4b7e with SMTP id cm10-20020a0564020c8a00b0044e81b34b7emr256022edb.181.1663254289168; Thu, 15 Sep 2022 08:04:49 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:38 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-5-glider@google.com> Subject: [PATCH v7 04/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254290; a=rsa-sha256; cv=none; b=KLXlmfbVlX4hhDMliG5T+s21AN63HlHT/0hRZfPxQTc+roBbl854yfYomDm+FGPteGgeTc MaHo2h3As9C+fNYbOlydUBKwHEVnsDU5IUOCB0WZB8iq3GVI0VIxKdUyn6H13QXc1x0Pp8 nq0wWwLBgJlPTVoxVzrV6bxj468rbn8= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=EznsgfSi; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 3ET8jYwYKCDwejgbcpemmejc.amkjglsv-kkitYai.mpe@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3ET8jYwYKCDwejgbcpemmejc.amkjglsv-kkitYai.mpe@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663254290; 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=gSZzwrXvhkj2wpolUx3YVTouxdXHw9PaTEDZ4n2e3I0=; b=uM9K6QGF4veCMBt6aTY/jWGqlM+lGjLq88DZt2mZiBjXWYhzdcSWMNSguWAZsmVn5Xt+gQ JkD6G9yrG70Wofxc0lhNlQYu5DNu1DILxq8ab3nfhgo5u/mSSlzYzV+3dJtxua5n4HqT0g 4v0vrJTnmyw2zEca7cUT8kmMBY3UVUc= X-Rspam-User: X-Rspamd-Queue-Id: 79C451400D6 X-Rspamd-Server: rspam05 X-Stat-Signature: web3ahcf17xjjqitrn5bby87zenk8zen Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=EznsgfSi; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 3ET8jYwYKCDwejgbcpemmejc.amkjglsv-kkitYai.mpe@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3ET8jYwYKCDwejgbcpemmejc.amkjglsv-kkitYai.mpe@flex--glider.bounces.google.com X-HE-Tag: 1663254290-554768 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 v6: -- add missing empty definitions of instrument_get_user() and instrument_put_user() Link: https://linux-review.googlesource.com/id/Ia9f12bfe5832623250e20f1859fdf5cc485a2fce --- arch/x86/include/asm/uaccess.h | 22 +++++++++++++++------- include/linux/instrumented.h | 28 ++++++++++++++++++++++++++++ 2 files changed, 43 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) \ diff --git a/include/linux/instrumented.h b/include/linux/instrumented.h index ee8f7d17d34f5..9f1dba8f717b0 100644 --- a/include/linux/instrumented.h +++ b/include/linux/instrumented.h @@ -153,4 +153,32 @@ instrument_copy_from_user_after(const void *to, const void __user *from, { } +/** + * instrument_get_user() - add instrumentation to get_user()-like macros + * + * get_user() and friends are fragile, so it may depend on the implementation + * whether the instrumentation happens before or after the data is copied from + * the userspace. + * + * @to destination variable, may not be address-taken + */ +#define instrument_get_user(to) \ +({ \ +}) + +/** + * instrument_put_user() - add instrumentation to put_user()-like macros + * + * put_user() and friends are fragile, so it may depend on the implementation + * whether the instrumentation happens before or after the data is copied from + * the userspace. + * + * @from source address + * @ptr userspace pointer to copy to + * @size number of bytes to copy + */ +#define instrument_put_user(from, ptr, size) \ +({ \ +}) + #endif /* _LINUX_INSTRUMENTED_H */ From patchwork Thu Sep 15 15:03: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: 12977523 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 93D7EC6FA89 for ; Thu, 15 Sep 2022 15:04:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2CD8F8000A; Thu, 15 Sep 2022 11:04:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 27B8480009; Thu, 15 Sep 2022 11:04:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0F96C8000A; Thu, 15 Sep 2022 11:04: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 017D480009 for ; Thu, 15 Sep 2022 11:04:52 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id D68F2806CB for ; Thu, 15 Sep 2022 15:04:52 +0000 (UTC) X-FDA: 79914642024.16.55BF895 Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf26.hostedemail.com (Postfix) with ESMTP id 7B4BC1400D1 for ; Thu, 15 Sep 2022 15:04:52 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id y14-20020a056402440e00b0044301c7ccd9so13177248eda.19 for ; Thu, 15 Sep 2022 08:04: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:subject:date; bh=fFq4uMFAQOrpNZLU6ydmQ4L19fVmnSZ8zUbgI7i1GQ4=; b=cYFC7xgwkG3gs04oiPHichqf/b1cFGgnshWpci997Y8A5dmNKzM9GLf1pSniU9D5ku Hu0GNNgLGKOuCAjrWJaXT+n9N89m2OxLOF0aOwdMyJRfQdseBJiGghcUNTQyFs/ZUubo G0Bq2nmYZaMX470D+dLwGf+5tXv+CdH3F2TMy+uDbAiFguTk8zqG98QgRczskOtYOVDt jAjc9/Nvo2lVMh0sB6dJ8QlQ6FSwdkrIQtBbc5j54TAMFUpg1fazPK6geXTDbKZa+hK/ CtUyiXzLrd1aFajhJlu7Z3AG5P4f56KCWQ3N8ixK5cFk+NBX9dXK5inzenk1xXL3SCUs gf6g== 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:subject:date; bh=fFq4uMFAQOrpNZLU6ydmQ4L19fVmnSZ8zUbgI7i1GQ4=; b=upS4EUQFfjl2VL7j9TxXMXV6l1GLTAtoekBGNQOMbWG2cb2n+k35RMgDm7MnNvdiS8 mvR49chznY6T8xpsUCwoddby59Df24oCrcULouHuAEDTqkYW5rKduOikUUUfhsQGfHcq Ie191CxNF3TcEHnSnk4P0HFqlT8efCmxUAdnyCX7tImhaStfSIYgK2YqHQtoZcx9zAT7 RCaacSdQ0KVOqPRHKx7M43GGQcEPRj1sJaaPJ6FP9M3WucB0h+lAq8WCAEpcNXrq5UXN D5wL7Q52C5b5Gl6pet2Rlx7NtqkrryAFZh5K62+BEYGgf88ACTKlJqzni2+FX0RW6xR8 ctWw== X-Gm-Message-State: ACrzQf29Z5aHJzmyAUBp4ALYOv68j9IWql187L6xRb2RuaWd46FYtbDs JHNw4wTajuDkFwviJxuf2SfsnniufiU= X-Google-Smtp-Source: AMsMyM6nb4U8HQAI/T9hvY7h7118sHc1IkAnGq/cw4fx9f/IsFE0PzlQ/IJC4zd5NEMJksDjOy7tAJB6OCw= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a17:907:272a:b0:77c:d7df:c9da with SMTP id d10-20020a170907272a00b0077cd7dfc9damr318116ejl.332.1663254291795; Thu, 15 Sep 2022 08:04:51 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:39 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-6-glider@google.com> Subject: [PATCH v7 05/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254292; a=rsa-sha256; cv=none; b=AoHTGot1+0Sa3kkm5LV3K7RORI+bFYBBA08kDdzl5ZT2dYhkpaUZbvBgf1GlmC4lj9mdIl eZV/DnQw+CGQrQ8wCG3W2j+Q4Ujwy8HHOSsd87679VniiC8v9eRz8ynuBZQ/nKQhUWjRyE 31Kv+HsK9sfaTW+1OAvo9czDSXxvN2M= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=cYFC7xgw; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 3Ez8jYwYKCD4lqnijwlttlqj.htrqnsz2-rrp0fhp.twl@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3Ez8jYwYKCD4lqnijwlttlqj.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=1663254292; 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=fFq4uMFAQOrpNZLU6ydmQ4L19fVmnSZ8zUbgI7i1GQ4=; b=VC8q59cdLoGffa0JMVNBLsrIwKVtnog192bxa5EckHNidkfN4MG1pZG2lx+mx+qewWsJtY xWLLw2j9kTtwDmgwg9UQb0gTjScJRm4U6fMoFctISZBji+XOiXuw6mM2y4G9rXqT3V+WGn 1PEPoeSIBpLzd3C8iGSLnh4SoI1SuFM= X-Rspam-User: X-Rspamd-Queue-Id: 7B4BC1400D1 X-Rspamd-Server: rspam05 X-Stat-Signature: iuudkcrp9sfyszrzp7mkpeucnt3zu59e Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=cYFC7xgw; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 3Ez8jYwYKCD4lqnijwlttlqj.htrqnsz2-rrp0fhp.twl@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3Ez8jYwYKCD4lqnijwlttlqj.htrqnsz2-rrp0fhp.twl@flex--glider.bounces.google.com X-HE-Tag: 1663254292-788998 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 Thu Sep 15 15:03: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: 12977524 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 2A2FBC6FA8A for ; Thu, 15 Sep 2022 15:04:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B56968000B; Thu, 15 Sep 2022 11:04:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B05B280009; Thu, 15 Sep 2022 11:04:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9A7CC8000B; Thu, 15 Sep 2022 11:04:57 -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 8C54980009 for ; Thu, 15 Sep 2022 11:04:57 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 635E440A56 for ; Thu, 15 Sep 2022 15:04:57 +0000 (UTC) X-FDA: 79914642234.24.68E8FB3 Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf01.hostedemail.com (Postfix) with ESMTP id 188DF400AD for ; Thu, 15 Sep 2022 15:04:55 +0000 (UTC) Received: by mail-ed1-f73.google.com with SMTP id v11-20020a056402348b00b004516e0b7eedso10463445edc.8 for ; Thu, 15 Sep 2022 08:04: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:subject:date; bh=vQDAA7RaO97NVNbgK2nE+Ddwgte1b1V7llUj3RudfK0=; b=m+lOIzIwzLYvTh9yma+HOEX7MlepjcXGz44+pyDvaXifCcVI6kEd2N5u9fvjmhIhoP TpegfgM9mzUDZcuSV01dYrVxP9LXELqadYk95TvXfv/p11cawS0SwfCeA09gDlpLUhPr IqJ8MBhI8WH3M9uSa7z+MtWkYl7mM7K1ITTjRLkFN1AB7Ld0IiXqOQuBbs18XRNDawlk /km7YzuxlRa1to64ZYHO8rtviAr2t+manVlPO81KTS1cnrg9E9piBaj5mkGNwxdf8YqQ 3owehcinH1vwoXyiqnZ7vpaT2uD7GSjnicdQdWfDfiO1Lq3huvBoDW64toAnqEBp19+w WFJw== 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:subject:date; bh=vQDAA7RaO97NVNbgK2nE+Ddwgte1b1V7llUj3RudfK0=; b=bqMku79yVcGkF6vCu5pPobCcklC3jeJeywGYuohIe5qNOgwQv5AduGUYTVAb+ZJ0c7 oV0wG65pKXXg0K5I/Z2eKbV3aga5YJCrWSTK6ZFCjRykDUtEtAt6syBMRvVwm4ZmDcOy s+h2q3J/oejbNX12qtZD0zcUL9DGU7gm6nm8IwUg0MPz4BD4yPxdW/zjfDgLFsHm4Szj H7T22vL7kwYaUDLN7/RpBPv063ffsOT8tKFkIOywsi4GrFg331QqonJl1aRisdzLCYKq szEKa08uYSN1wBo3ViMVI4XOzfwgE90JnBpVPL2ua1q8XwPgdT2paGiC14uZTkMnhNz9 lSZQ== X-Gm-Message-State: ACrzQf21PkqgdWIPVq+Az4E6LcEOEGWv9yNMNc+jQKHCUEfwshXVVZiY Bmhus/PlRkC1EKn4SVLAuQwWj/50ewA= X-Google-Smtp-Source: AMsMyM5TjB+5jgPlQ4wYXCEXCYAWLmUXfy9ORhe5Nu7nbuV8IG5MuB3ronxM2wVuyNwdmjqAvN+zlmzaEns= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a17:907:9816:b0:741:89cc:af19 with SMTP id ji22-20020a170907981600b0074189ccaf19mr297924ejc.492.1663254294715; Thu, 15 Sep 2022 08:04:54 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:40 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-7-glider@google.com> Subject: [PATCH v7 06/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254296; 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=vQDAA7RaO97NVNbgK2nE+Ddwgte1b1V7llUj3RudfK0=; b=UWxbMWpFDnZSVSqbDhyWeR5/4qdAyRN7Et3mcI8qmSjsv1Z4HVKCwXoUzzl5t4njb/qce+ Dt38URpFSPr/+l8JOaFE2U6tYbHZfEyYKSqxZiuu7XUhclcdLSRGE72MWVGcnS5LakUnpZ B1jXw5qxo4XQyeKJVJSPV2FpDBmfNlc= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=m+lOIzIw; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf01.hostedemail.com: domain of 3Fj8jYwYKCEEjolghujrrjoh.frpolqx0-ppnydfn.ruj@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3Fj8jYwYKCEEjolghujrrjoh.frpolqx0-ppnydfn.ruj@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254296; a=rsa-sha256; cv=none; b=tSVNHXMWyDrR5wU0vssxvF/0FhWgddmEjavejJtlvlQV3B0pQ0sE0wsdW8Ajxnj9eWxChf nmiPq9I2YpTwe5qQxXM8Kcaqj/NFeCpjvAB/8HKp03suPv4TnucAv6lmZAXHg0ugPMeO/x j6tL02+Jff2sr0UMEhpJv2w9akJyPqE= Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=m+lOIzIw; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf01.hostedemail.com: domain of 3Fj8jYwYKCEEjolghujrrjoh.frpolqx0-ppnydfn.ruj@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3Fj8jYwYKCEEjolghujrrjoh.frpolqx0-ppnydfn.ruj@flex--glider.bounces.google.com X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 188DF400AD X-Stat-Signature: 9x817dh9w5dkhruwykkpyqua5nwxth9z X-HE-Tag: 1663254295-698115 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 Thu Sep 15 15:03: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: 12977525 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 687F6ECAAA1 for ; Thu, 15 Sep 2022 15:05:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F01BF8D0005; Thu, 15 Sep 2022 11:04:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E8C8B8D0001; Thu, 15 Sep 2022 11:04:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CB6058D0005; Thu, 15 Sep 2022 11:04:59 -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 BD3A78D0001 for ; Thu, 15 Sep 2022 11:04:59 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 011DA1C2642 for ; Thu, 15 Sep 2022 15:04:58 +0000 (UTC) X-FDA: 79914642276.25.E3AB8CC Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf26.hostedemail.com (Postfix) with ESMTP id 9BC021400D1 for ; Thu, 15 Sep 2022 15:04:58 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id y14-20020a056402440e00b0044301c7ccd9so13177484eda.19 for ; Thu, 15 Sep 2022 08:04: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:subject:date; bh=FNkIx/jm8VmVwIUxP0UOjGDN2D7zAcH5S0k2sC/Umx8=; b=bQZcA86h1qdzkhJGHuC1vulzCY9/vYojby8OXPpWVu/zRyh5HczDOweVFKdJ4iLXuF GyYCdbY0ycNTlctxGp2Qkk0ioxtzMFkx9QZQlbXKFcl7aeu1akUOI1KeHnOgF6CHm0Fv obXQEHRhmunHmiFIxXCbTaWBthGdhqBefEoxaeY22QEHGd8ceECbBf3z8KFzhi5TCvvT mViYy1jZUqHyxugpaM4vdP8Kdzh+QNJNBDR5XY5YwcZt4wyaxBZ9DHAOLj3aRhrrMQXE FJgQqWptGQUPr62YjXrSkGv5rqr0x+LDUJoYI+k2RwY89uUfjbxM4+mQeSfb2/XyzBo6 JcYA== 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:subject:date; bh=FNkIx/jm8VmVwIUxP0UOjGDN2D7zAcH5S0k2sC/Umx8=; b=vuTef4NuaQMZOJypFYg82sWLiW3Tt88oECevu1tjRGMfRk88x45XrKRkYPBENooCZa +ifGsmqW7rOdMoDVPpEbQN3XJZAZKJa0Qf6omfS0RX3q4h/iU1i4z68efqMfviKVFyy7 kuRFuJPIvShNOrfsadjaI+cxWLlaOdIM6TaHrbZOmL1MO4QUffNDEAnwpq9CuOxgdOMl fHnDvgk8An4JZUgLcwO9AxZ72Jr/FtXfRrGd1LPOexi6RyUgQqiXYwkCH1Kr44sZDZGm sb5QfJfk6SSSPQyDRDPSfeUmT9FHuwSDUTymNPqeLps4qHSS3pkXdHLWBn9YdV/R4PY6 xuiw== X-Gm-Message-State: ACrzQf1i9IJTozmXMZpbapMiwsfhLN7h6DH/mlZggdk1F5T/D3+Rsr8z +e8bYhv9dDOUe3SauUURod0R0X1YaZM= X-Google-Smtp-Source: AMsMyM6w2KbV7Uo7UXovOlHDL9eur1WPj/OeZn6zE9SBsrD7mTMRXaISFQ/Ug34qtuaBk27l/iQo9UqsYqI= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a17:907:94c7:b0:780:7ccd:aca7 with SMTP id dn7-20020a17090794c700b007807ccdaca7mr323937ejc.136.1663254297632; Thu, 15 Sep 2022 08:04:57 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:41 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-8-glider@google.com> Subject: [PATCH v7 07/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254298; a=rsa-sha256; cv=none; b=N6KJiW6aaIlVp+/EhZ5xqyxDT/rRaIRPSXQQq3tWSMQoYvjiG52765ury/l0ToO32Ma5p6 giDp19OgQaHjDNGLZWS+v7EDAuKNmnGbKfbCvK8Uy8yUanccogKtlNMv/TbbMHfKlk81fp yk3HgxBNYchKpSmyW6enDHboTd/7luw= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=bQZcA86h; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 3GT8jYwYKCEQmrojkxmuumrk.iusrot03-ssq1giq.uxm@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3GT8jYwYKCEQmrojkxmuumrk.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=1663254298; 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=FNkIx/jm8VmVwIUxP0UOjGDN2D7zAcH5S0k2sC/Umx8=; b=Lu4T/UJWdfhs+9i/Lk16QbCgYQ7NyxSkO2g3qowcAY0hyIdAjAUOxoy1D4ezld3C/ul3Ki H2eqK5bk9Idd3sknawbY0IURk4zzthlEBoxg81OkITC02Jmzs+BHH/NpdZZ+9l00+kHbmU uPCW5whrsqX1ARjY0TYfA0KCSFNZ724= X-Rspam-User: X-Rspamd-Queue-Id: 9BC021400D1 X-Rspamd-Server: rspam05 X-Stat-Signature: 3f6ift3oi17uhye65offc6oxfj7ge3ud Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=bQZcA86h; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 3GT8jYwYKCEQmrojkxmuumrk.iusrot03-ssq1giq.uxm@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3GT8jYwYKCEQmrojkxmuumrk.iusrot03-ssq1giq.uxm@flex--glider.bounces.google.com X-HE-Tag: 1663254298-680209 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 Thu Sep 15 15:03: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: 12977526 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 904C4C6FA8A for ; Thu, 15 Sep 2022 15:05:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 27E068D0002; Thu, 15 Sep 2022 11:05:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 22D938D0001; Thu, 15 Sep 2022 11:05:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0CE138D0002; Thu, 15 Sep 2022 11:05:02 -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 F22388D0001 for ; Thu, 15 Sep 2022 11:05:01 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id CA054A0700 for ; Thu, 15 Sep 2022 15:05:01 +0000 (UTC) X-FDA: 79914642402.07.C31AA85 Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf24.hostedemail.com (Postfix) with ESMTP id 79DE01800D7 for ; Thu, 15 Sep 2022 15:05:01 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id z9-20020a05640235c900b0044f0575e9ddso13209592edc.1 for ; Thu, 15 Sep 2022 08:05:01 -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:subject:date; bh=eIK0S734s+WuMuzHGxUrnoQabUzsOPxTYvMm/ke78tI=; b=XJ1NrdN4cE67qY8hURHcVMHDPQGupt4q/cUx1wA/HOiuLu51Fa8NOdGrL8VY2zFx6+ 8lRaRnc4ndVN0vHKZv8f+yMlsJHhkeysfQSbADbjj48OFzy6utjli2LCXqgO1W6q0KWs u8Y4BILUyHfMx3esbYPqIQ8idS1ZbqJP9fwLBBBIi6DYlYVdmCAHB4heTSfomXizMsZk GyLXvDmJRoIf7JWGAzwX+W+HzcNjI8SDif7R9oq0eb8sItwTAzK0jsJFXez5DVZ5MROh 8LodpxcePlcNUPrjrWggRF7+H/HHXuGZxlaBMNl1MIbEqokJuDf/bfbt1xR8wVM3a5yK Xh8g== 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:subject:date; bh=eIK0S734s+WuMuzHGxUrnoQabUzsOPxTYvMm/ke78tI=; b=WFz30CsnuXYhAZLmMTzmJdE3f26ewC4DmbbLnxa/sXHPMP7hNAuVLFA9G77kFDbCi/ sh4ftrUMNQ3joCFrlTu599rkkioPazgh0B7uFHf7goHVx2lFc8WTxffaVSRXiaPbz6cV Q/MpeXTb+PnQcx49YPvV9MRIQ8b9GJvs0hJvXIvtdGiEYTK4quq1wjq4dtbtYSL3vsgf ClVBkbkE/iQw6NTlPi0OSJyn/AZ7TD7EKzLuSCu80yn07193DZ+SlnEJp+809eF6TNWx O/MPND7QRp3m8nRh8LvsvmuLvysnKNzNbiF2rDrXshzz08tgbbw0M2HM9H7bOqbHLZNI 7DNg== X-Gm-Message-State: ACrzQf2mTgsVZIvACIY7XvRhNWDL7gagab/+HM2nhmzqI7wCiuT8LUrO LIg1+fAFQum/H8PV3OH3OlurilMlmdc= X-Google-Smtp-Source: AMsMyM48IxUnhOyact6I+qmIgU38+68zbAYlQB/H/hsqJ5msHgmA0uWrdQMTcWETwn+9Nr3+eHzpivVfQRk= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a17:906:9bd3:b0:76f:8cce:7a61 with SMTP id de19-20020a1709069bd300b0076f8cce7a61mr282972ejc.345.1663254300266; Thu, 15 Sep 2022 08:05:00 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:42 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-9-glider@google.com> Subject: [PATCH v7 08/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254301; 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=eIK0S734s+WuMuzHGxUrnoQabUzsOPxTYvMm/ke78tI=; b=YfC363uJf7XWNlEmLO1jdMvIBZb0nOPLuTDuG/jK+wWeIZzzG1UAeWrVyzTIdCddJjjeIK 3ismAjBmZLwHSnwBN3zZ/zFVLFRcYtV/yIoiSNmoZV+c6csN7d+xoE8o6CPqiRhmdeHyNH 47QE3Tb36BA0UxSvo3SYN+IMOFrSoO0= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=XJ1NrdN4; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf24.hostedemail.com: domain of 3HD8jYwYKCEcpurmn0pxxpun.lxvurw36-vvt4jlt.x0p@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3HD8jYwYKCEcpurmn0pxxpun.lxvurw36-vvt4jlt.x0p@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254301; a=rsa-sha256; cv=none; b=wHIDPve6qlDUJB2wTsPNKy7QfWTqydAVg+igCerYUOJJTImUzJUNGcTP3Q4KwxgtkLKSl+ 5RGfmibJDSfUdhbX3WJ8bZRUJjMi473Tzd/fpCbIonAEHHScXBp/Eh57JHHI8MNtPr36AR a5b+GLvNxKMv+Tqtv9M+1wXBmBL1htg= X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 79DE01800D7 X-Rspam-User: X-Stat-Signature: xwe5m14k1pahyhs9zrj3s1tkwyzisqt1 Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=XJ1NrdN4; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf24.hostedemail.com: domain of 3HD8jYwYKCEcpurmn0pxxpun.lxvurw36-vvt4jlt.x0p@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3HD8jYwYKCEcpurmn0pxxpun.lxvurw36-vvt4jlt.x0p@flex--glider.bounces.google.com X-HE-Tag: 1663254301-694416 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 Thu Sep 15 15:03: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: 12977527 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 F29BCC6FA89 for ; Thu, 15 Sep 2022 15:05:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 898A080009; Thu, 15 Sep 2022 11:05:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 847108D0001; Thu, 15 Sep 2022 11:05:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6E8C380009; Thu, 15 Sep 2022 11:05:05 -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 60D7C8D0001 for ; Thu, 15 Sep 2022 11:05:05 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 2DD931C6110 for ; Thu, 15 Sep 2022 15:05:05 +0000 (UTC) X-FDA: 79914642570.04.E34DB3D Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) by imf31.hostedemail.com (Postfix) with ESMTP id 61D39200CF for ; Thu, 15 Sep 2022 15:05:04 +0000 (UTC) Received: by mail-ej1-f74.google.com with SMTP id qa33-20020a17090786a100b0077a69976d24so6989985ejc.7 for ; Thu, 15 Sep 2022 08:05:04 -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:subject:date; bh=PTsTfo/gJDV7iQzWmhln0w3yo3kBxDNSc9M3IZOTBoE=; b=JPgXbB5IFWEFHgGxJgBDcBqsn7OA+OOmod3TkAJCtmEW3tmDIEAaPKco4V3rSfeVUT GdXM8f5AP9MppFWIxBTCdhy9ugH7naOnesHXUn34A53RkPodiqw3X1l45i+8mKggb98k Rfas/CBCSU/l2+KKyy5Yy+k2HFZReEtFK6RUw8luzU4yWnpDATvBrwpXvH5uKPEFz/CY fMEUsRqiEpjIjSm6svk+PcgL/IEq9k1aaJenW+l4RBh8IWYn4eFeXv42PrPIL6LXSCM+ D6Wc5KgbtUAixY99TEj/8stcJrQCa9sE6zMp90t6pz7+Avl4xRSk9+TMfjnHy6he5k4v 4d0Q== 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:subject:date; bh=PTsTfo/gJDV7iQzWmhln0w3yo3kBxDNSc9M3IZOTBoE=; b=fsld6lnjmZ4I3SEtMLmixjIvqTPcR6YMcerF1pVRIIvYaFn3FCfcaYmGWYFFZPygQq g7YZcIT2wFxrBFLYbRNxmPvroZAqj5nkqOgNJAl4jb4TG+ez1yrE4a+vbZT86vY7mi9h 8KbEHJ7oQzvDMoQ6idfExyYOZfGDeycVnIHJWnFxSEPKnUJCdq+x/xO4gqHQLNu7jcBG L5NZAiNnxoKQ60amJP5lpgw53bc8BFwetyhc7g5dbI1abyYtyF6ivy1XA+pQwUoJ6r7Z Uh+yWi19ZVPXZYGxdhc5RviQWQqP8X/PWbVJLgJlsn+EIlKJuUzbXOCd07EuducBW1lc 54+g== X-Gm-Message-State: ACrzQf3ohhLtDUcyRg3cDeoQYoWUWZTRi4+WCLbEhl27XHK4kGkomjvU ns7thobrAomUXv1CAHIx/OHxTrtlWiU= X-Google-Smtp-Source: AMsMyM4p6KqskMugQKLr3d2QoJoMFJ9ItcCVTDioHI8BQ0089p953E4XlBHrGmtCk9sUiS5VpTwgREGXwKI= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a17:907:2cce:b0:77a:6958:5aaa with SMTP id hg14-20020a1709072cce00b0077a69585aaamr280253ejc.245.1663254303074; Thu, 15 Sep 2022 08:05:03 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:43 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-10-glider@google.com> Subject: [PATCH v7 09/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254304; 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=PTsTfo/gJDV7iQzWmhln0w3yo3kBxDNSc9M3IZOTBoE=; b=1K+HDNf5Z+l5B5hGZBC/6yu7mEELD7J6hzal1esjnmwZB021tJkIlTQco/XCEEZqOhkHFa IOSiqYMCmwwlBiTbbk6NExLmkJnQIHG8yqPe05GzPNkKV2NNGaauxoVcNhumP2kPA80tZb MyeCIPp/+s0V/3QGkT0LnyDvWgF6uDY= ARC-Authentication-Results: i=1; imf31.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=JPgXbB5I; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf31.hostedemail.com: domain of 3Hz8jYwYKCEosxupq3s00sxq.o0yxuz69-yyw7mow.03s@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3Hz8jYwYKCEosxupq3s00sxq.o0yxuz69-yyw7mow.03s@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254304; a=rsa-sha256; cv=none; b=SDUllGRQP82gMQPuuUme5V/A5BmrIsVr+y5GVhlBYLRcS7gmTbFAXn/+vb0FLxdXhQQ68J vLrj+oS4f4s68e6OtLplvT/tNtofKADnQ+dBwCH8IvWqUN5591MhTgHwIq264kdXnNwJ4Z LtfBdSN3w3/f/sAwtXkgA4rsBuyK2ks= Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=JPgXbB5I; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf31.hostedemail.com: domain of 3Hz8jYwYKCEosxupq3s00sxq.o0yxuz69-yyw7mow.03s@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3Hz8jYwYKCEosxupq3s00sxq.o0yxuz69-yyw7mow.03s@flex--glider.bounces.google.com X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 61D39200CF X-Stat-Signature: iyu36is3kp4emjstfzzhtefjcquekrfk X-HE-Tag: 1663254304-268384 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 Thu Sep 15 15:03: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: 12977528 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 AB034C6FA89 for ; Thu, 15 Sep 2022 15:05:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C7838D0001; Thu, 15 Sep 2022 11:05:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 428088000C; Thu, 15 Sep 2022 11:05:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2F1DB8D0006; Thu, 15 Sep 2022 11:05:08 -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 20D6F8D0001 for ; Thu, 15 Sep 2022 11:05:08 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id DD11D16054E for ; Thu, 15 Sep 2022 15:05:07 +0000 (UTC) X-FDA: 79914642654.08.254F063 Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf18.hostedemail.com (Postfix) with ESMTP id 3509E1C00A1 for ; Thu, 15 Sep 2022 15:05:07 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id r11-20020a05640251cb00b004516feb8c09so10384199edd.10 for ; Thu, 15 Sep 2022 08:05: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:subject:date; bh=vmKBMF2McNFVJqtG6TmpI3MAJxV0RMGeKyj0ZsE0Fyg=; b=gEgjEUXtMXLGLmInSdiTVr8SJtUM9dAvRmm9gTPwlvDJlkqKGsf6gLgatpF9No6GIj f1Ed6OLsNQPbRAYwMDuikJo5an+hRugAPcYR3d2idcVQVyC6hhx41fffnwLsLvYNGn+7 fnso7t/d8ggMNfh1yjqm245oFqmKs97l5uauhCjKI2lKnuKK561skile8xyfuBwUMIR2 k4457FVVXUyo6LArzKfC16VrG33HBknRIw4PTmX8bNDYzxGBz51W2hpuA84deuKw+CD+ gndPzro+ppcGwFoGvdA4uYw55JH56lxjWCZGwiQPKeGZVtfCO924G4QvZ4U1I6ciker2 cP7g== 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:subject:date; bh=vmKBMF2McNFVJqtG6TmpI3MAJxV0RMGeKyj0ZsE0Fyg=; b=KO/S6u8UtNUA/0nkyWV0YraLc5QZLZk1OpB5NdqV5opOQW0NhkKlIMVSXkuHGz3EKN XCirgtjedz4rrIDV5X34VnuF31nRyjlSpxK+w6oiONi7BxyjdJdxBxN69wR0qPQque4X irKVfzXX5lm8AVO3N2WEnCPIUgLWjrxCathZqBRSTWT9veQZtplD/NSCQJfCSJxUoqDn Cd2UG+JSXxtgAur+oR3DfynHIxzD6L+qCKfNeCjRNV+zchPD5L8UaenZsZkNdfaFvagr V2VojwS3rGY0FgaTvvD6OIjNdSRLSzX52YHWFWNWNROqhS0UR0DoeTQ3fwsOAzivUji/ T69g== X-Gm-Message-State: ACrzQf2OPgFegNTUHl7bvA9PWAPSYgelV+7D6Hl5Vc32oZ5IMaxteJHD dn8tEdvcHu1f5RG0se24dVTEMZLtOgo= X-Google-Smtp-Source: AMsMyM4KMV/DJpiNOvzRLXTErTnexkmGTLdYCR5aeQSVv8s3wGGoLlfOMidq99otIc+vGxZhLpSlcwNoSfA= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a05:6402:5112:b0:451:cb1d:c46f with SMTP id m18-20020a056402511200b00451cb1dc46fmr254990edd.35.1663254305960; Thu, 15 Sep 2022 08:05:05 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:44 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-11-glider@google.com> Subject: [PATCH v7 10/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254307; a=rsa-sha256; cv=none; b=qhu3LiA6aGtbWV13f/M0RxkDjqUde+vLr7pOFsCLWOn2axa6syREWPGKIvi6O1ppdFx7Ps SZL202iH6nlWFQcUufTynN0NuF/dw6Z6Zxwd67SQyn5HiX/VDGzgUQFDockPQNkogeR4HS COdmZeHrmCRDAvyJQcAHDO6dJivgT1U= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=gEgjEUXt; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf18.hostedemail.com: domain of 3IT8jYwYKCEwuzwrs5u22uzs.q20zw18B-00y9oqy.25u@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3IT8jYwYKCEwuzwrs5u22uzs.q20zw18B-00y9oqy.25u@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663254307; 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=vmKBMF2McNFVJqtG6TmpI3MAJxV0RMGeKyj0ZsE0Fyg=; b=SlMJBQAz63hD+OiPe6j5XKh4jgPFFXF/ZikD/GMw+TiQvXNAZ3Ohbl0H2XOyWNga9Uxlet bir+iHOgL5lHM1cmNrT8QOXam6cXL9r4G2V4d13lYcTKsc37ZdNCUAbSM3KXlcF+Et3dkL wwwKMLiyN7eX6zhsKTnwKKmarSHWi7Y= X-Stat-Signature: ymboi1coa5w9wzricesfcuakf8cwcghw X-Rspamd-Queue-Id: 3509E1C00A1 X-Rspam-User: Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=gEgjEUXt; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf18.hostedemail.com: domain of 3IT8jYwYKCEwuzwrs5u22uzs.q20zw18B-00y9oqy.25u@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3IT8jYwYKCEwuzwrs5u22uzs.q20zw18B-00y9oqy.25u@flex--glider.bounces.google.com X-Rspamd-Server: rspam02 X-HE-Tag: 1663254307-248670 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 Thu Sep 15 15:03:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12977530 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 9F695ECAAA1 for ; Thu, 15 Sep 2022 15:05:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 42B868000D; Thu, 15 Sep 2022 11:05:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3DA328000C; Thu, 15 Sep 2022 11:05:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 206F48000D; Thu, 15 Sep 2022 11:05:11 -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 0D5EE8000C for ; Thu, 15 Sep 2022 11:05:11 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id D8FD240A0D for ; Thu, 15 Sep 2022 15:05:10 +0000 (UTC) X-FDA: 79914642780.09.DF72A08 Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf06.hostedemail.com (Postfix) with ESMTP id E6EE3180098 for ; Thu, 15 Sep 2022 15:05:09 +0000 (UTC) Received: by mail-ed1-f73.google.com with SMTP id y1-20020a056402358100b00451b144e23eso9386434edc.18 for ; Thu, 15 Sep 2022 08:05:09 -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:subject:date; bh=tRIgLlwATZdT+TTKtlpJloEvtY4wIpIACXvVBZZrTN0=; b=angyTB/bhgaJUbUW+jqctua5YqX3hJeqkuLHG1T9k42aScQlUE5rRq+tiHsk4ijTqm QVvxrbzsdXhGA14mZRrKywyNDCI+Sgj9kGWl7wAAU+tWgKa06yZehFcmTyeiHNuEDddH y8h6Bkh3fh5vMyLMG+iMVooXDRnCvA14nLhSd7yh1LuCVBMyNHhRAbonBXXkddwKIA+g LM6oaugBJms7LJOiqEZh5LqAS0ZWUw4N7gxEgGoE9WgKOAbjASlQzQCSE7EMmTQ4zrza JH/iyBpHowY7VMAn3rGggQEEyyLwGdXwPX4uQhxfP6tVb++jehh6ICqCCN4QTEOSzBKc blJg== 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:subject :date; bh=tRIgLlwATZdT+TTKtlpJloEvtY4wIpIACXvVBZZrTN0=; b=5ts4zo4C77cC8NTCM8HFt/GT+1c+oX9jFDh1vtkc5GoV1kJ9qA8sVZkEzHd+WXf0qM SBYzxCBQD6oozjh1njywPyaN72E9IiAvKO0v1XqQZpzSgm1X87ZG5oX5bdFAr56yc8tE ++sZu1RNeUJKPQNGEM6+EzQ1+N/kQtsFWz3MXTq337+PA/4+wjVgK02g5w+v5Svvxl6I /BkgNN350xFNgYHM1K0tzbrEQoClmfs9LYn6NSD/YrwML+44qvZWdgxucRdkdH3D/dnN YwnWwyAqrNl9sTw1cVu9Tym81oJuOqN5/42dPTCa8ftRaOyZAFwjvb0aP8yDjOn5+FVx TvfA== X-Gm-Message-State: ACrzQf1lQ2QIyrV/Dz9B63I8zSKqsEyM9Y8YKz9UsHoKrnpmfXdLnjzD AxJn+5dp1o6MLsH05hEMv8g2QD4JTso= X-Google-Smtp-Source: AMsMyM5EooyKj/BuigBTGWmKeu3fPitTxDZbG6XYdsijfCV3pUwEpy9ZUhvaduBQNz5C+Exk6qcQekzW3c4= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a17:907:2722:b0:77e:d84d:9e09 with SMTP id d2-20020a170907272200b0077ed84d9e09mr268366ejl.679.1663254308601; Thu, 15 Sep 2022 08:05:08 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:45 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-12-glider@google.com> Subject: [PATCH v7 11/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254309; 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=tRIgLlwATZdT+TTKtlpJloEvtY4wIpIACXvVBZZrTN0=; b=IU2OmvRbKXDyoRFB8q0juEU10UiDzzOS7qKTerVx+RcBzS3g4VDBmSuqCBWQUbbVoU4jvJ aB2JFO4mp6xfXGX3oepj/KCVhnYjDVhJVtxkaf71nz0ankTJp5VjWuEnj9LlJ12N3s+/eD OUu+bW+MYf+36VqDnn+CaB+NjCu5f1Y= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="angyTB/b"; spf=pass (imf06.hostedemail.com: domain of 3JD8jYwYKCE8x2zuv8x55x2v.t532z4BE-331Crt1.58x@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3JD8jYwYKCE8x2zuv8x55x2v.t532z4BE-331Crt1.58x@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254309; a=rsa-sha256; cv=none; b=oQjIEK6KOD1keRM/MGZ8po7LtHT1apXLiTxG218KZtccvRuLdIakBb9H3dP0YK7Z05B0vb r67ncdmF9s8a2UyAnh3WjGutR111aNJxEpEBWiiA2pHov4l9PtOVpYTG926s5n7WLxPzZR WTgWyle8k/qV/GRWsNJU5drwR3zN5u4= X-Stat-Signature: 34iw5zpi8mw6m5y33rs5ob6d3y7y4qf6 X-Rspamd-Queue-Id: E6EE3180098 X-Rspam-User: Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="angyTB/b"; spf=pass (imf06.hostedemail.com: domain of 3JD8jYwYKCE8x2zuv8x55x2v.t532z4BE-331Crt1.58x@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3JD8jYwYKCE8x2zuv8x55x2v.t532z4BE-331Crt1.58x@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam06 X-HE-Tag: 1663254309-848766 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 v6: -- move definitions of `struct kmsan_context_state` and `struct kmsan_ctx` to to avoid circular header dependencies that manifested in -mm tree. v7: -- instead of printing warnings about origin chains exceeding max depth, notify the user about truncated chains when reporting errors. Link: https://linux-review.googlesource.com/id/I9b71bfe3425466c97159f9de0062e5e8e4fec866 --- Makefile | 1 + include/linux/kmsan-checks.h | 64 +++++ include/linux/kmsan_types.h | 35 +++ 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 | 440 +++++++++++++++++++++++++++++++++++ mm/kmsan/hooks.c | 66 ++++++ mm/kmsan/instrumentation.c | 307 ++++++++++++++++++++++++ mm/kmsan/kmsan.h | 204 ++++++++++++++++ mm/kmsan/report.c | 219 +++++++++++++++++ mm/kmsan/shadow.c | 147 ++++++++++++ scripts/Makefile.kmsan | 8 + scripts/Makefile.lib | 9 + 17 files changed, 1592 insertions(+) create mode 100644 include/linux/kmsan-checks.h create mode 100644 include/linux/kmsan_types.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 a5e9d9388649a..f0f8e912287d3 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_types.h b/include/linux/kmsan_types.h new file mode 100644 index 0000000000000..8bfa6c98176d4 --- /dev/null +++ b/include/linux/kmsan_types.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * A minimal header declaring types added by KMSAN to existing kernel structs. + * + * Copyright (C) 2017-2022 Google LLC + * Author: Alexander Potapenko + * + */ +#ifndef _LINUX_KMSAN_TYPES_H +#define _LINUX_KMSAN_TYPES_H + +/* 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]; + u32 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 /* _LINUX_KMSAN_TYPES_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..b6de1045f044e 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 bcbe60d6c80c1..ff098746c16be 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..5330138fda5bc --- /dev/null +++ b/mm/kmsan/core.c @@ -0,0 +1,440 @@ +// 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" + +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) <= (KMSAN_MAX_ORIGIN_DEPTH << 1)); + + extra_bits = stack_depot_get_extra_bits(id); + depth = kmsan_depth_from_eb(extra_bits); + uaf = kmsan_uaf_from_eb(extra_bits); + + /* + * Stop chaining origins once the depth reached KMSAN_MAX_ORIGIN_DEPTH. + * This mostly happens in the case structures with uninitialized padding + * are copied around many times. Origin chains for such structures are + * usually periodic, and it does not make sense to fully store them. + */ + if (depth == KMSAN_MAX_ORIGIN_DEPTH) + 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..97d48b45dba58 --- /dev/null +++ b/mm/kmsan/kmsan.h @@ -0,0 +1,204 @@ +/* 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_MAX_ORIGIN_DEPTH 7 + +#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..02736ec757f2c --- /dev/null +++ b/mm/kmsan/report.c @@ -0,0 +1,219 @@ +// 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; + unsigned int depth; + + if (!origin) + return; + + while (true) { + nr_entries = stack_depot_fetch(origin, &entries); + depth = kmsan_depth_from_eb(stack_depot_get_extra_bits(origin)); + 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)) { + /* + * Origin chains deeper than KMSAN_MAX_ORIGIN_DEPTH are + * not stored, so the output may be incomplete. + */ + if (depth == KMSAN_MAX_ORIGIN_DEPTH) + pr_err("\n\n"); + 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 Thu Sep 15 15:03: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: 12977529 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 24DA3C6FA8A for ; Thu, 15 Sep 2022 15:05:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6EDB18000E; Thu, 15 Sep 2022 11:05:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 69B0E8000C; Thu, 15 Sep 2022 11:05:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4C6888000E; Thu, 15 Sep 2022 11:05:13 -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 3DD2C8000C for ; Thu, 15 Sep 2022 11:05:13 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 1A7221206CE for ; Thu, 15 Sep 2022 15:05:13 +0000 (UTC) X-FDA: 79914642906.28.93FCA59 Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) by imf10.hostedemail.com (Postfix) with ESMTP id 99164C0097 for ; Thu, 15 Sep 2022 15:05:12 +0000 (UTC) Received: by mail-ej1-f73.google.com with SMTP id xj11-20020a170906db0b00b0077b6ecb23fcso5863417ejb.5 for ; Thu, 15 Sep 2022 08:05: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:subject:date; bh=7DSx6RI0rEPDhrbVIpPikNsWTzcR9RC7kojIoDN82TE=; b=Ei0ZOo1tqmI3niKdg+NjyFrYcuWag/zfWQm1yP23l3BnyrmeeyX9JElhu/ainPdOo+ W/dywvadr04aecsU+ebZxNPOW9ICDqXTNUBOfu9kwz4f+GBrzmn4+l42PxeNQZxqW7qX Lc6kk/7HGejkAghJS9IEbkisG2lBRJSt45/r7bb7cF8xd11QNz4bx8M34JSwDTW4yhwt tJ78o05A6unsLVwF1wekgeIG4C2cUR1DKnuMtF3G53ElLkeVtM+TiQxtNlZWsycB4QVI EGJwbLtLFKkrnXnXXmVON0VvjQn5HAXmX0ge2JfPIbRLtNJmQD9OXFejAYW9ubOcI1mg OwYg== 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:subject:date; bh=7DSx6RI0rEPDhrbVIpPikNsWTzcR9RC7kojIoDN82TE=; b=4xvHj1AN63izsJ8DwYuzXpSByFfpHjO2lMIHeACeWgHs+MQvWu5P1YUk+7ev69ZfuH 8VQEmJuTelg7U5aGLSENyFlFfw65MJw3XIbJNAEO8vKFRzCnMxdiraUDOnc5bH20kWqP 5Fk5foK2VxBcb0irg5ZYo40F6YyA2TP1xf3R+Yq0x7yQy6lCHV+r2IgHnfIj5sePCab5 EIcRoarCY+X+fXLv3DBUYlHi0i6AGIGa9gdGNXXf4uKN+w3YAfaLePi1ZbBYVdbPJeaJ /DwqcQ2t8QiwySJF3K8P76qKBphWXt3iamTsVbw0FUj/QaSdV1UfnYeRm1f+V7dJf8iM bIyw== X-Gm-Message-State: ACrzQf1sWTbFr7HUkh9z7OFVUAr8O/wUMTjAeMWahIcf49p+YxHUT5dI azjhM85EYNIM3w8Dgt7X8Sk68D5RJGw= X-Google-Smtp-Source: AMsMyM5UGtRZWEfRuSf8jc2keSvCdwG95jpOF1wSMEeWA1a/s2P04BTVQXHj5Bx/TIBtK5vybFlxr4pFIjQ= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:aa7:c74c:0:b0:44e:a7b9:d5c9 with SMTP id c12-20020aa7c74c000000b0044ea7b9d5c9mr264063eds.19.1663254311281; Thu, 15 Sep 2022 08:05:11 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:46 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-13-glider@google.com> Subject: [PATCH v7 12/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254312; a=rsa-sha256; cv=none; b=WJMvqGPER+ujECXR+GCe3Plk8JFo1rUZKbzh5ElM8PaO19spFIzcNTcwPuiapyrujar9cd EQxjIpoNFr7Fp1gNIsoF8CXpuXQFB7w1W7Im2bRKiNDPnTLBGA1w29m/z4rtz56DaCZlpP +JpM8lC9YR3l1EWFq9+58HQkcn42IYo= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Ei0ZOo1t; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of 3Jz8jYwYKCFI052xyB08805y.w86527EH-664Fuw4.8B0@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3Jz8jYwYKCFI052xyB08805y.w86527EH-664Fuw4.8B0@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663254312; 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=7DSx6RI0rEPDhrbVIpPikNsWTzcR9RC7kojIoDN82TE=; b=VdKs6IzXeONen/RB5/zbpKhzCZXFlkTHQTuX88bPosIUKijX7oNqwE+PE+viPClUWvIGPU XtxG9RUVzjSAYtSqw6u0aMlKHEsEU91uZllpbwUAGqyAK9TWUz6tRiLPnFv2FtR8dghY+M Z5vDf60aAGUfSlgpwXbegwa6qcXTq4A= X-Rspam-User: Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Ei0ZOo1t; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of 3Jz8jYwYKCFI052xyB08805y.w86527EH-664Fuw4.8B0@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3Jz8jYwYKCFI052xyB08805y.w86527EH-664Fuw4.8B0@flex--glider.bounces.google.com X-Stat-Signature: rpooxyzip7hx1umhn31cdgh93icsyyr8 X-Rspamd-Queue-Id: 99164C0097 X-Rspamd-Server: rspam09 X-HE-Tag: 1663254312-483935 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 2c67f71f23753..2c1eb1fb0f226 100644 --- a/drivers/firmware/efi/libstub/Makefile +++ b/drivers/firmware/efi/libstub/Makefile @@ -53,6 +53,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 ffabc30a27d4e..fcebece0f5b6f 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 Thu Sep 15 15:03: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: 12977531 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 60456ECAAA1 for ; Thu, 15 Sep 2022 15:05:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EDA938000F; Thu, 15 Sep 2022 11:05:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E88FD8000C; Thu, 15 Sep 2022 11:05:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D04B18000F; Thu, 15 Sep 2022 11:05:16 -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 BAB688000C for ; Thu, 15 Sep 2022 11:05:16 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 87A6F1207FD for ; Thu, 15 Sep 2022 15:05:16 +0000 (UTC) X-FDA: 79914643032.03.6E4CB4F Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) by imf11.hostedemail.com (Postfix) with ESMTP id 9DC40400A1 for ; Thu, 15 Sep 2022 15:05:15 +0000 (UTC) Received: by mail-ej1-f74.google.com with SMTP id qk37-20020a1709077fa500b00730c2d975a0so7695361ejc.13 for ; Thu, 15 Sep 2022 08:05: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:subject:date; bh=DrXJVgAnGikXdoBfgSCi3xOinuzD72ruDjEjRoFoD7o=; b=pMDlMog1n3F76mKq0Orm26ggqAgcZbz8ZhleQGMqnTGR3RrhwhLXdNfNfQod5q7tsT FYabKAANZgyG+nKyYkX65poam5NydHvovkMw0I2y3KtNmNQdCevvAAlTdLEol2bZpy5p GwqekvuKQGjMGpceoNj67qDwmgnm6UC/gsqBZrB/xU68bigz0RWY3aVTdtDLhE6TSVY9 XrWLuEZmPoiE7vrQBFB3RlGK6SsYCNEAClW4Z+Oa3/pDIZLwYpyE8A35YjbxVNkGcUQh rQnEz8a6w1PRa2LwaF7jtwdwJsI811xbzxgcNqy5MsCW5f1GaPwCtVWuuunmQhqqg3jg lkqg== 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:subject:date; bh=DrXJVgAnGikXdoBfgSCi3xOinuzD72ruDjEjRoFoD7o=; b=nWlUuXd7nhLOzFmqhsU2tP+0nZlSUcBD4pHqGs2vckKYXeE8dCiEnXziQJ02ax4TSe 1I8iahOjOD54u2pTblFncG7lZkJwnfslOi4+SHW3osy9piB2YltjF3L5LsuhjZCeFDoR 4kdTOn/KcO3xKbzAg0J1ffWkDW9EqQXKa2tSnGfDXI38fE7JDWmQQ86Qw4UxoliCW06c YHIT1H/bu9OwZG132+cv5I8H4pDgYqw2wyujDlyvwZRUH1HX2iGdu5Y3NvSz2KttlONi EyO5prEo3cyH1DFSn3ZK7mNGzvFhDTNvXAriW3/JzlwZY+SDnm3UM8yIZpi7k1UqBvqF 6VBg== X-Gm-Message-State: ACrzQf30S1onYqYcBY08cEz11gPOYoAwgr8XfIOtmcpoLh2tHUHuzVbE OpaESpGIL31G21VWvK4i8lG+gRe4dLA= X-Google-Smtp-Source: AMsMyM723C0Ra0d6HWWOT8eZXf9pV5FKMgmRbixWPt5RD6cCVjGI7xNel4VbZv8orCGK014FpZn9G1mzW64= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a17:907:7f1c:b0:77d:248:c1c3 with SMTP id qf28-20020a1709077f1c00b0077d0248c1c3mr291412ejc.416.1663254314212; Thu, 15 Sep 2022 08:05:14 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:47 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-14-glider@google.com> Subject: [PATCH v7 13/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254315; a=rsa-sha256; cv=none; b=1pXIY4QO9P+zv1eXVJnN3Gj8eCcOsrhA3T4MiBSRK+yNmZh0f4D6/U1D56kGhC+Vw+GK5U nt+de61bOgrHOMTfHvFu9LaAnljHXx64n0vtArrgwfARtkScYZROVtEua80NdxoigXmurb 4O9FtbIORCaHcpHQbgKINy0QMHV9vpQ= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=pMDlMog1; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3Kj8jYwYKCFU38501E3BB381.zB985AHK-997Ixz7.BE3@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3Kj8jYwYKCFU38501E3BB381.zB985AHK-997Ixz7.BE3@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663254315; 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=DrXJVgAnGikXdoBfgSCi3xOinuzD72ruDjEjRoFoD7o=; b=TjkaBLHPfz0VvNRuzR+sz0vleRSKL2BMnCaGKxc5CGtXBENmgDzy/nGxoMy62aLDm+6x9w nRp2gHiTKJ0KFfcSjK90yc0zU8h0bM4eII3YGniXM2+WPb/dyZEPu50X3Z3zJCJRhI6X9i Gd28DRUsCE5kuIBszlBfInvmeJGF95k= X-Stat-Signature: sc9qxamp7so1kjrb8gz1yqxwyqejuaid X-Rspamd-Queue-Id: 9DC40400A1 X-Rspam-User: Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=pMDlMog1; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3Kj8jYwYKCFU38501E3BB381.zB985AHK-997Ixz7.BE3@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3Kj8jYwYKCFU38501E3BB381.zB985AHK-997Ixz7.BE3@flex--glider.bounces.google.com X-Rspamd-Server: rspam02 X-HE-Tag: 1663254315-118836 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 936490dcc97b6..517e71ea02156 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -11373,6 +11373,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 Thu Sep 15 15:03: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: 12977532 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 C04FDC6FA8B for ; Thu, 15 Sep 2022 15:05:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C427B80010; Thu, 15 Sep 2022 11:05:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BF2788000C; Thu, 15 Sep 2022 11:05:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A1D3180010; Thu, 15 Sep 2022 11:05:18 -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 9201B8000C for ; Thu, 15 Sep 2022 11:05:18 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 6F0AFA0382 for ; Thu, 15 Sep 2022 15:05:18 +0000 (UTC) X-FDA: 79914643116.08.41D4086 Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf08.hostedemail.com (Postfix) with ESMTP id 276EF1600B2 for ; Thu, 15 Sep 2022 15:05:17 +0000 (UTC) Received: by mail-ed1-f73.google.com with SMTP id t13-20020a056402524d00b00452c6289448so3774716edd.17 for ; Thu, 15 Sep 2022 08:05: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:subject:date; bh=8SyfjxYr3+020xbP74HATSJ8O3XHxQ0BD6WEKGl7y2Q=; b=A+NZKffX1AXqaJsmcPoA09v91IVFzeK+K6exWLrCSl7Gnd2snyDqKNuOmZBdx3lneJ UHyQIFUy6IfDyx+ENP4kMg1hRGa0KRNYgmSBEJmQitGF+zi/N1/JInf1z4vvhb1GoH3B T+UbzNCMrZV7+G1WbmZMhY/QxHUi0aQ21NsLwlH91m0wvrbOxEHwQDzy8EZdmdiVuN6c cmaHsColtiGiDt54nO1vLa5YEnIik8v4gp7kK84kDjUaNkw5NqdX1U/2eo6UlPlLVhlF FcymhUF6nu5AlJH2JGSi9pDdFAHB+MKBeI++gspi81wFRqIdpd0tLolma+RxVb7Cmhm+ h04A== 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:subject:date; bh=8SyfjxYr3+020xbP74HATSJ8O3XHxQ0BD6WEKGl7y2Q=; b=Dbmw/maSebrTVsUCr6YnfVVE+6IGFonh1NnT6xAQEvtvUvzEMy/bB7nZVraYCjvBr7 SFDlUS5UO40XvYl57/YVy4yN0sCb/t+4G7ET/9SWVkyczPl30cA5gL3DMw7sezpJxG/f sQgvnigrB/upCzXsiLEn9lc8+LwgXprqdMmTv647nAlpN0d/Ro/XOM81nIk8jfqeDhXS ahKVnLNTVZ/mEQqBhCLVvudFBD2WE18Y86TPGnO8L29eWI0h9CB8HtpKhtYUAFhCr6HH pyNXlrrrOCEmH7s8P/cF6jJl1mMySc0L+KqS2if6Mrr1VjLtDaI1NsGAyI0b0MgQjxrs ASww== X-Gm-Message-State: ACrzQf0wl4KfYF84PET5ikZ7B6/NUPRcdxFEx3yssOQDZqAzqFFlb3gD k29zj1XREqHysW44QIH+xyZeoBGTEjQ= X-Google-Smtp-Source: AMsMyM7bRRB1uoLmqUakjp/m8qDuzZF+ze3QABJLoqyQyPWhkBW3wRCJwCRSULdYlcuEGyQg0RXZ0/G0SvU= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a17:907:970a:b0:77d:a10c:e089 with SMTP id jg10-20020a170907970a00b0077da10ce089mr307572ejc.364.1663254316920; Thu, 15 Sep 2022 08:05:16 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:48 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-15-glider@google.com> Subject: [PATCH v7 14/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254318; 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=8SyfjxYr3+020xbP74HATSJ8O3XHxQ0BD6WEKGl7y2Q=; b=FO8wsdzynkoBDVIO8TEH3rTFJXkiAV7hXKIW2OeE3WDHLqzoETtHxCTp+4tW+fHFDRQQW+ K253CM8QvkCobEtJZ04T7Osay7oAKaLJQwiHhhhDJstKlAJxU4Wcdqycvl/yXScxnqj80k wiAG6So/og2FL/0Z0eScbJeL9COYLtU= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=A+NZKffX; spf=pass (imf08.hostedemail.com: domain of 3LD8jYwYKCFc5A723G5DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3LD8jYwYKCFc5A723G5DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254318; a=rsa-sha256; cv=none; b=atyHtDEew+dHhhob7oCD3wtm/laL1u0AcdNNcPAQXbFkJHscYjdYAurSnm0eBGiQjwSjRQ wt7vDtgmKezMF/qbj6vM0SSzOHo/6auorsRzRD8XcHCU+LF4RG83fMiAYG7ZjA09WCF1h1 OsabufN3OajqdKf/QkO/9Tun63B4R5o= X-Stat-Signature: 3t8u7f9o164gtdfwfbetcyad6cyym3oo X-Rspamd-Queue-Id: 276EF1600B2 X-Rspam-User: Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=A+NZKffX; spf=pass (imf08.hostedemail.com: domain of 3LD8jYwYKCFc5A723G5DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3LD8jYwYKCFc5A723G5DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam06 X-HE-Tag: 1663254317-686929 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 v6: -- doesn't exist prior 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 | 145 +++++++++++++++++++++++++++++++++ mm/internal.h | 6 ++ mm/kmsan/hooks.c | 86 +++++++++++++++++++ mm/kmsan/shadow.c | 113 +++++++++++++++++++++++++ mm/memory.c | 2 + mm/page_alloc.c | 11 +++ mm/vmalloc.c | 20 ++++- 10 files changed, 394 insertions(+), 2 deletions(-) create mode 100644 include/linux/kmsan.h 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 new file mode 100644 index 0000000000000..b36bf3db835ee --- /dev/null +++ b/include/linux/kmsan.h @@ -0,0 +1,145 @@ +/* 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 + +struct page; + +#ifdef CONFIG_KMSAN + +/** + * 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..040111bb9f6a3 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -26,6 +27,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 Thu Sep 15 15:03: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: 12977533 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 9F0F8C6FA8B for ; Thu, 15 Sep 2022 15:05:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3415E80011; Thu, 15 Sep 2022 11:05:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2F1988000C; Thu, 15 Sep 2022 11:05:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0F5A580011; Thu, 15 Sep 2022 11:05:22 -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 E9D488000C for ; Thu, 15 Sep 2022 11:05:21 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id C6B96AB34F for ; Thu, 15 Sep 2022 15:05:21 +0000 (UTC) X-FDA: 79914643242.23.40625D1 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) by imf31.hostedemail.com (Postfix) with ESMTP id 51FDC200C3 for ; Thu, 15 Sep 2022 15:05:20 +0000 (UTC) Received: by mail-wr1-f73.google.com with SMTP id o7-20020adfba07000000b00228663f217fso4177109wrg.20 for ; Thu, 15 Sep 2022 08:05: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:subject:date; bh=Ovg29tOGV0MBaJSgEZI8XqfnbTwhQ8WU6IE7DIFXMYM=; b=fhAwNeFvrzpQm5ykiqK5lKKhgyJOAsPoFb88R6hxuX8IfCO//pdhhlPDX2E+LgpnaM aOqfqaTzhpuaimXQv6BenfXef5lbtFGk2RfB9J4Qz2dCQei6mgkwg76jXQVKGSs4biIa qVwwrESeusVAOWBnrsxYAbjqORiYFVbh2HXXdBy+QEidSramJXZsGjVoJdOsj9chr1Ah NilYGaM68NdVvnoOD5kzbNfA3BNvwBDpPhojZEJNz3uEh1wZ41VvgRKrxN77Q5M2LgE+ fUy7DDiqmP3rUX8Bp+YrXCV/GVh7Zhutac4P57+Q2iO3Wyx2tDzwK5fYpKQiuz2D5gL5 //CA== 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:subject:date; bh=Ovg29tOGV0MBaJSgEZI8XqfnbTwhQ8WU6IE7DIFXMYM=; b=vyU0Wfl7YcnvcbEHAELZmrpnRZP+y+zviQwbr/sGwGd2/Wch0LV++iduRcShRlmqN+ lD0EDaWGtmg5H4co/JTE1T7uTHT3DABzyd2sFVytdu9rzFdogf1/HSOPB6NDCNOoGlXz D1KoiGRldXsksj6ihJEqxjwnFqp2en2eMe4PRv4sVZCfHUUyErhDVLWuQAxhjFdiqxkx x/sHBQwcru2V+JPgopCCNuqPVuMwlJG8bcX2UVPz+jdGq8DM9ojn8K8EFwKi6iEkVNme XjPqzMaVGH7y43qW1RDPA6eNti7gg0gKZU54BjUCeiiqaE4Sr4t5OTRh6iew4WRj5wHb x0Yw== X-Gm-Message-State: ACrzQf0m4yOyS/eji6SSHcXlG2DX8Ryw5T/YFkBY6bB+hSIOfmnIHImn mUmIYyOfB5tia6lslAK5YfEILzcW8Kg= X-Google-Smtp-Source: AMsMyM7DBQ00xLpmijt9fLMZPSUtz/n/GCaN1VvmiR00hvES4uwrkovYSQQxg3nMbW66DlfNiCZ54OkIYHM= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:adf:fac9:0:b0:228:652b:a33e with SMTP id a9-20020adffac9000000b00228652ba33emr61774wrs.61.1663254319665; Thu, 15 Sep 2022 08:05:19 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:49 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-16-glider@google.com> Subject: [PATCH v7 15/43] 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 Biggers , 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 , Stephen Rothwell , 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; imf31.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=fhAwNeFv; spf=pass (imf31.hostedemail.com: domain of 3Lz8jYwYKCFo8DA56J8GG8D6.4GEDAFMP-EECN24C.GJ8@flex--glider.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3Lz8jYwYKCFo8DA56J8GG8D6.4GEDAFMP-EECN24C.GJ8@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254321; a=rsa-sha256; cv=none; b=u2PACsGOJ1wYUHiIxmZWT948WCoQJwVbSytcc8ZVyqJ3L6yE1VK/H9gotjG3GWH8sEmlPe 3nmRX/W8spdMXrQBmKdPPy6V9iyMWYb2+tIvU8VPlX3VUeSisYmXDSSvcbFUzdCHzvfvXa gtGXDS1cov3CqY3QjGFCypSEXPGU6dU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663254321; 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=Ovg29tOGV0MBaJSgEZI8XqfnbTwhQ8WU6IE7DIFXMYM=; b=kilKRY02HyPDJIjVXGr7BCMUysJ/QMeUPBLr/Hj4eCSdI0NFIibFfeqyhthI1kCQdvMbJC RvCP31Ythp5lgmEOHMXjUOULbg8/oetlNy4Zvv1ejgsELqQ+7MlLhLn3MHkLahQFX6zgDa Qr7ZlOvuKMRgkAiuNoVn0N6n8SDAmSA= X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 51FDC200C3 X-Rspam-User: Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=fhAwNeFv; spf=pass (imf31.hostedemail.com: domain of 3Lz8jYwYKCFo8DA56J8GG8D6.4GEDAFMP-EECN24C.GJ8@flex--glider.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3Lz8jYwYKCFo8DA56J8GG8D6.4GEDAFMP-EECN24C.GJ8@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: rhmd7ogng5gmukhrsr8y51nrwaf9gf58 X-HE-Tag: 1663254320-955495 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 b36bf3db835ee..5c4e0079054e6 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -14,6 +14,7 @@ #include struct page; +struct kmem_cache; #ifdef CONFIG_KMSAN @@ -48,6 +49,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. @@ -114,6 +153,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 040111bb9f6a3..000703c563a4d 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -27,6 +27,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 Thu Sep 15 15:03: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: 12977534 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 5118BC6FA89 for ; Thu, 15 Sep 2022 15:05:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 737FF80012; Thu, 15 Sep 2022 11:05:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 712218000C; Thu, 15 Sep 2022 11:05:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5622080012; Thu, 15 Sep 2022 11:05:24 -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 481798000C for ; Thu, 15 Sep 2022 11:05:24 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 2D0421C610B for ; Thu, 15 Sep 2022 15:05:24 +0000 (UTC) X-FDA: 79914643368.18.FFD8874 Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf18.hostedemail.com (Postfix) with ESMTP id C25751C00AC for ; Thu, 15 Sep 2022 15:05:23 +0000 (UTC) Received: by mail-ed1-f73.google.com with SMTP id w20-20020a05640234d400b00450f24c8ca6so13150490edc.13 for ; Thu, 15 Sep 2022 08:05: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:subject:date; bh=uEBXmIOJ/jVJ62g3MgOZ1PWj/PPm6Zng9ZXCGF8oCGQ=; b=d96TG3Hxv6sIG0D9GCl0RiC+uFhT+JLVK6sevFTTg2cGiOmsNKa0agzQa6C6xzYpgB KrER5gjEWJxrUVaZFexycRUvOwztcaivd+5Ql+JyY3mBj0Ia9jZkUxSEK4PULX7m/EV6 XWACNEeJZDhm241z1k7FPfy66U38FvMA/gORPAoDPIW41/YFRIqK1uT63SaSHtWW0R0x q4TO7avKYTsOs8uhvI820+846g4n5hZBN5ENyr4KQ8lx22/A9EtRnuyzFUnVUEdfkMx/ sfWQuT3tK6ZVt0bBZuCbUoQf0lkYauyM3gSJyZgZsuanpkHtxDrS16OPGAfrdP3wp1IT m86g== 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:subject:date; bh=uEBXmIOJ/jVJ62g3MgOZ1PWj/PPm6Zng9ZXCGF8oCGQ=; b=Dg0MX8Sntxbd8NSCirPPKMGfbloajvAaRXsNBOd7JvClVfLzfxdxtbuh6jJqMoaBFP Aro5Zwss7US2SkX88M7oighjn4qH6RxhK9gr7J98z6Ahn+1/HjrqRnxbH9dnJMtMtcHc zlSO1cNbCTB0jMvB3/IaD6ftN6eeLQMsUVm5J+zuE8SJC8JMvdyca5gltxyAWTj73ZNo dlgoEjwY8CEI/nuOhN7+DiTMJQlahYMESMt4JC6A2lP/SsdCfKW+c6SashMEnlf0mm/l r3eMA6hvS2rsRRuI6hL1bNqqlW9yrOsXk56QrEvYFpdo/zKR0THBKXE1PF6m2EMlP0k5 lNZQ== X-Gm-Message-State: ACrzQf1Oz/YtRd9c6EhDAbn8Hu4dUrkA2TJ6SFr3gXS5lmZBl1QYzSEz RHosKt/mpn6ZMmz3mSx8si2QHGN/DuA= X-Google-Smtp-Source: AMsMyM4p9q4a8kwSPLQHupWtxmE8sSfLUWhBeeePGlfGtCaz1sm2ZmFR06/jzES7F9GL5jloyYk/aerrGvE= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a17:907:7215:b0:780:3153:cca2 with SMTP id dr21-20020a170907721500b007803153cca2mr279565ejc.427.1663254322378; Thu, 15 Sep 2022 08:05:22 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:50 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-17-glider@google.com> Subject: [PATCH v7 16/43] 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 Biggers , 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 , Stephen Rothwell , 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; imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=d96TG3Hx; spf=pass (imf18.hostedemail.com: domain of 3Mj8jYwYKCF0BGD89MBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3Mj8jYwYKCF0BGD89MBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254323; a=rsa-sha256; cv=none; b=DWNFL3TMH3/BxqZJ8RZST314PO0a9WMMuUpZhw0SBe+vWnhEzdXCubnqoj8esDL6qCrMYj wlCMtSfiOvQO1B/1KfFSXeJfzoZZ/BItCosjEfUvH+BCrM7RBqlpNBO9MNU4yHVTfz9uvJ Z5zgODeZPQ8MdMZMtQuAdYvrokauFR8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663254323; 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=uEBXmIOJ/jVJ62g3MgOZ1PWj/PPm6Zng9ZXCGF8oCGQ=; b=kOcVGwSLF84IZoexldh5eAdb9AGf7SQ6sTlJNZ6Jb3zbJK01mGSagMBiwVbt8yQBGvXpdC aNEJNedaEUWi/eX3o9UMmTn7o6sVf7FAtaOd3iDN2cVRb0cqaxZWcRD3UWz1QJb9Spx1Ha gJDfq3b6sK4OBoNlhT145Uo0BMGS4xo= X-Rspam-User: X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: C25751C00AC Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=d96TG3Hx; spf=pass (imf18.hostedemail.com: domain of 3Mj8jYwYKCF0BGD89MBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3Mj8jYwYKCF0BGD89MBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: pxmygck85efohj6cjciypr7adg1r7955 X-HE-Tag: 1663254323-318034 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 5c4e0079054e6..354aee6f7b1a2 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -15,9 +15,22 @@ struct page; struct kmem_cache; +struct task_struct; #ifdef CONFIG_KMSAN +/** + * 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(). @@ -139,6 +152,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 8a9e92068b150..a438f5ee3aed5 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 5330138fda5bc..112dce135c7f6 100644 --- a/mm/kmsan/core.c +++ b/mm/kmsan/core.c @@ -37,6 +37,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 000703c563a4d..6f3e64b0b61f8 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -27,6 +27,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 97d48b45dba58..77ee068c04ae9 100644 --- a/mm/kmsan/kmsan.h +++ b/mm/kmsan/kmsan.h @@ -180,6 +180,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 Thu Sep 15 15:03: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: 12977535 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 0C720C6FA89 for ; Thu, 15 Sep 2022 15:05:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9824280014; Thu, 15 Sep 2022 11:05:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 90B928000C; Thu, 15 Sep 2022 11:05:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 75E0A80014; Thu, 15 Sep 2022 11:05:26 -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 5E1218000C for ; Thu, 15 Sep 2022 11:05:26 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 38D7780571 for ; Thu, 15 Sep 2022 15:05:26 +0000 (UTC) X-FDA: 79914643452.09.AEFFA17 Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf18.hostedemail.com (Postfix) with ESMTP id C29DD1C00A1 for ; Thu, 15 Sep 2022 15:05:25 +0000 (UTC) Received: by mail-ed1-f73.google.com with SMTP id w20-20020a05640234d400b00450f24c8ca6so13150588edc.13 for ; Thu, 15 Sep 2022 08:05: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:subject:date; bh=GDD69Hg38hLSA3Zw/mP+awjKG9w/cRToxTsz+vFO2/s=; b=imTcK6TFY9gMCiK9OPaboq1OMLw9GJOGLbvoA4kgqnonlCN5qw/9RTdHxSkkA5oRvO aaav+bXuMC4UqW64i63bTUKyMz5+dDTwMJDpQ7HVChSUmuQOAwkzQnI7k3VhgDKibeER 0tZJGxlcmK5u79CBu0RqfKxnepNfsiN/c+45QNRFRhLFWm4yju3ZnFOV1Mg/Bj56TBoH 9dsoBBe1aGd98hXTuMHG6OjiKK7nHffoJIMt07eZCrw9cDAqi31EB8CPP6jJSgL8uzXi +TChQraiEsMjMN8Jc7llPG83ZzPNNdel1ylYYvAdm4GaqOvAJxzJoGv8K4iFYy9pJrqI YGdg== 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:subject:date; bh=GDD69Hg38hLSA3Zw/mP+awjKG9w/cRToxTsz+vFO2/s=; b=i8jCx3wVAksqH2aLXc74gPDMrAIKtJxU2e4NxoxvkILCBiPm0WqtU15Sr5q13RSlUW KUGLJwjOk4vcnD9hXgGlBemd6EoKrVC5vFiaaSpj6R3tm7akBvbffzqjvusTqm3ufOh8 TW19k5ILeu5wj2axQ2p3o0K4CVEKOaWDt3m681qiRir68yJ1eVIpkiYbvaMutivs/4pI 4oabOHHfxTs5Jkx5jbVaBURk6IFYdCVnnhMiIpQCkGnvT4Ov8puKTLcvb5nPnIu1YuEV nXY446QGjlmCouYQdIvsjpTGTCoKEkJhrnH4hYP8xycPPUJl68jaz6yAz/0M0Ic8o2WG mdHw== X-Gm-Message-State: ACrzQf3VMLJMvtdMYGwQUnzPK72ty3VCmGAg4OsDB3nrdh5j/znm+9J5 BdD8yyuyXIlIYeefPzckfhsxBWAPI9w= X-Google-Smtp-Source: AMsMyM5dMVyIHk6Tvsl9s6tYOonNcic7f1c1nyrS5gEUS5xcwWZ2kjSxaMcHwWHmQgKwsG7D/3FALYC5zrY= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a05:6402:4311:b0:451:c437:a5a9 with SMTP id m17-20020a056402431100b00451c437a5a9mr266952edc.272.1663254325073; Thu, 15 Sep 2022 08:05:25 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:51 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-18-glider@google.com> Subject: [PATCH v7 17/43] 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 Biggers , 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 , Stephen Rothwell , 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; imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=imTcK6TF; spf=pass (imf18.hostedemail.com: domain of 3NT8jYwYKCGAEJGBCPEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3NT8jYwYKCGAEJGBCPEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254325; a=rsa-sha256; cv=none; b=BkTGudRikZduNpDEocmS/hhA4NZck0HD9XwL4NuOTfJmIAAGX5JAVIKrylZuTu4cXt9el5 gdCLoQ6pbXd4nYQEIzhx8nEC4NjyCsX2+UgQKgEXFPtsEkTpcaznHt2E2vjlr83ViBW76P C2IebrNv1uESUVGLnb9ydxYAxNQ/U/c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663254325; 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=GDD69Hg38hLSA3Zw/mP+awjKG9w/cRToxTsz+vFO2/s=; b=ScnUAxKedEOSA1KsPMuWCz9lBmYxp5hXtG3qvQpj4ZJWJwNnZXAp96FgQ3CGsWkPyWJZyI Uupfh0omCIul2Tv+vrJ0y70y8LGwGEfaiqMRRHL6dB0rub2k7vs+fe39ElcrLz+OMkQfCo x6aiXuWStaDdzzBjC2w7CD1z3Q13Acg= X-Rspam-User: X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: C29DD1C00A1 Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=imTcK6TF; spf=pass (imf18.hostedemail.com: domain of 3NT8jYwYKCGAEJGBCPEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3NT8jYwYKCGAEJGBCPEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: es14wg6syceq7ced1516hc3ccjiqbhfn X-HE-Tag: 1663254325-330177 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 354aee6f7b1a2..e00de976ee438 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -31,6 +31,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(). @@ -152,6 +174,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 1fe7942f5d4a8..3afed7bf9f683 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 77ee068c04ae9..7019c46d33a74 100644 --- a/mm/kmsan/kmsan.h +++ b/mm/kmsan/kmsan.h @@ -67,6 +67,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, @@ -187,6 +188,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 Thu Sep 15 15:03: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: 12977536 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 55ECFC6FA8A for ; Thu, 15 Sep 2022 15:05:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5B2F880015; Thu, 15 Sep 2022 11:05:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 53AE58000C; Thu, 15 Sep 2022 11:05:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2C9E780015; Thu, 15 Sep 2022 11:05:29 -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 1A04D8000C for ; Thu, 15 Sep 2022 11:05:29 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id F1FA314063D for ; Thu, 15 Sep 2022 15:05:28 +0000 (UTC) X-FDA: 79914643536.23.649187B Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf18.hostedemail.com (Postfix) with ESMTP id 868261C009E for ; Thu, 15 Sep 2022 15:05:28 +0000 (UTC) Received: by mail-ed1-f73.google.com with SMTP id w20-20020a05640234d400b00450f24c8ca6so13150685edc.13 for ; Thu, 15 Sep 2022 08:05: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:subject:date; bh=78o9fcmZe1rakLyEcoaxPY9Q3UwZCgWFE2+savdbFCc=; b=rks9uNCmfYS/YqkY6U0g2qKgsPbMgCzE5to9YTQsDSGno7MLZ57OiRem/Dm1MXM0oH 61AdLXN3KgyZyk6WUXmAHXD8aHWhS7pncFYrPm6r8+mhpSEOBaFzKjhqrmkb5ObvQynG 3lyTmZNVq6qgxAzIWXhy+CSuzx0NPccWxOoEtJhpsVgel0Vw7Mfxk4UkKlJwhEMEQ0Fi 2tZcO8IrdExLJ33Ih9HNh0SgckWNA8DiGItv4ZovRLvY24k9gTbd/QACNW+CdrW7i0mE 8QR49AgHjVh9EsKUHDRNITyvpL24ACDRbhDaHoQexo2bF7giUI71j4WmqaXoSvQd5FNX krfA== 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:subject:date; bh=78o9fcmZe1rakLyEcoaxPY9Q3UwZCgWFE2+savdbFCc=; b=Iv7OOzpydxgIaw2Cv8Zj0fyToWftByH9WZ7fRa9EiGyAd0tb/GfzYVPN3tOyfd5tIa uQAI6J+WGmOT8DvQMdSVR+QpODEnr/40cWtfur3SZ5FVhY853WUcr4737LhpBHhq9sml IdWST4gJLNXBlorTQ/vBOk2XBvpIgWKETu/BmyojcLOAnU1zIz7osOOGeulD0oK5mCZu 9xK5EM9RrTwNSVTUtbT02qtZUPxoTDkeiOA7AyTDYHDCbqRp0/n68GF3whIXkgyZvzxP EtAdIuNnjptVkinRatYmMu0Tv8vKchh1TNJ9pnJ96goVcoBMN7R65jBsNzQ4Kjg9G3gW 2ImA== X-Gm-Message-State: ACrzQf0068e2jRoz7jvx5O2r+X5Cl/w/T/wya7T8P41bBozBWXh5GjM3 0zJswGsaaFgXC9v7AYavcJCjh0gzfUk= X-Google-Smtp-Source: AMsMyM7StdmW2s0JDvShx0DM9h1H+6w0xzDkuDWLDHWgfE3mb+SwKv151I2wlKdpfNQSqM2uNm3YpsTkNJU= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:aa7:d8ce:0:b0:44e:8895:89c2 with SMTP id k14-20020aa7d8ce000000b0044e889589c2mr272708eds.382.1663254327816; Thu, 15 Sep 2022 08:05:27 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:52 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-19-glider@google.com> Subject: [PATCH v7 18/43] 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 Biggers , 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 , Stephen Rothwell , 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; imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=rks9uNCm; spf=pass (imf18.hostedemail.com: domain of 3Nz8jYwYKCGIGLIDERGOOGLE.COMLINUX-MMKVACK.ORG@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3Nz8jYwYKCGIGLIDERGOOGLE.COMLINUX-MMKVACK.ORG@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254328; a=rsa-sha256; cv=none; b=sXQXxnavJF1XSPU7OwCLQaGVaBa4qfu4MCulgr8gbv3ine+z9ZnhqfqVE30d4gMBMtG6nE Wm5Z1e/kGeciwYKPXxF16SWZ8gcw4LAEEv4qew4h+zeotArd1mMgnZPIub56FAQUHH10bG wfMb2EqA+MXUP4qZ9NMeLXVuGdoQwXA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663254328; 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=78o9fcmZe1rakLyEcoaxPY9Q3UwZCgWFE2+savdbFCc=; b=reKQ+Z00LJ7lGdlsBp8E68wOtPGgjLSONAaAnyC8W6zyeY9NjZLzCcRA6FwEKVFWF/WfaE a3QgIoV4hgITmLxZpkeQHebrD+L08rxtmFwyXGrxidaEQHglf7EZ1EeAIf/Zpb8yP8oHC0 YJkYOF5PLyKBVwtpK4H9oBdFvbJjp5I= X-Rspam-User: X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 868261C009E Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=rks9uNCm; spf=pass (imf18.hostedemail.com: domain of 3Nz8jYwYKCGIGLIDERGOOGLE.COMLINUX-MMKVACK.ORG@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3Nz8jYwYKCGIGLIDERGOOGLE.COMLINUX-MMKVACK.ORG@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: 5sd8hmayfb7356y9ehijde9p9kn74bb7 X-HE-Tag: 1663254328-575683 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() v6: -- rebase after changing "x86: asm: instrument usercopy in get_user() and put_user()" Link: https://linux-review.googlesource.com/id/I43e93b9c02709e6be8d222342f1b044ac8bdbaaf --- include/linux/instrumented.h | 18 ++++++++++++----- include/linux/kmsan-checks.h | 19 ++++++++++++++++++ mm/kmsan/hooks.c | 38 ++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/include/linux/instrumented.h b/include/linux/instrumented.h index 9f1dba8f717b0..501fa84867494 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,7 @@ 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); } /** @@ -162,10 +165,14 @@ instrument_copy_from_user_after(const void *to, const void __user *from, * * @to destination variable, may not be address-taken */ -#define instrument_get_user(to) \ -({ \ +#define instrument_get_user(to) \ +({ \ + u64 __tmp = (u64)(to); \ + kmsan_unpoison_memory(&__tmp, sizeof(__tmp)); \ + to = __tmp; \ }) + /** * instrument_put_user() - add instrumentation to put_user()-like macros * @@ -177,8 +184,9 @@ instrument_copy_from_user_after(const void *to, const void __user *from, * @ptr userspace pointer to copy to * @size number of bytes to copy */ -#define instrument_put_user(from, ptr, size) \ -({ \ +#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 6f3e64b0b61f8..5c0eb25d984d7 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -205,6 +205,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 Thu Sep 15 15:03: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: 12977537 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 BAC06C6FA89 for ; Thu, 15 Sep 2022 15:05:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 761458000C; Thu, 15 Sep 2022 11:05:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 71E3B80007; Thu, 15 Sep 2022 11:05:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 589BA8000C; Thu, 15 Sep 2022 11:05:32 -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 4AC3F80007 for ; Thu, 15 Sep 2022 11:05:32 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 1934A1A0661 for ; Thu, 15 Sep 2022 15:05:32 +0000 (UTC) X-FDA: 79914643704.28.1491F34 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf18.hostedemail.com (Postfix) with ESMTP id 7CB861C00B3 for ; Thu, 15 Sep 2022 15:05:31 +0000 (UTC) Received: by mail-yb1-f202.google.com with SMTP id b18-20020a253412000000b006b0177978eeso4083040yba.21 for ; Thu, 15 Sep 2022 08:05: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:subject:date; bh=eGWHmCncvYGtki7dpdJUZ+ZMr1uc51g9yFEsa5ToQ1k=; b=NM4axnShzfWZzuI33Ijb9zibsqxdD5xtfsxcZwSTrHe96AFTu8rvShAV7WtnTK/4eC 4nzHcJP9d8bug+Uubc12GaQcx6k/3ejCXYrLT+tcZrK/pgNIR1fBp483VCnd/tU9Zh7y XfPYB6dcTcQhIpq+FbxTMWb0CeKUnruwjDaG9jw2UXaiwXMsEfOtpJLWW1VVoGfv7QQi hKhqabbw+gBnso4B7E387BQbR6384tKRT0fJSZ1oR71O7Zwq05bd4oMDZB55EIaRkXev qNZy0SQgePADDnaTGnamHWUeuhwgcEav+WKinGKfniUlTtCw049X6nA0e3P+/yrvQtHp 1mZg== 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:subject:date; bh=eGWHmCncvYGtki7dpdJUZ+ZMr1uc51g9yFEsa5ToQ1k=; b=ypczk+B1/Ege2hNSu6aoDQAqkpb24RkOGqUbh35K04kwzP6+WebrJu2K1HyicX8uTn s5s+83UpFhDkAEu43aYxOVVcXlJOCiuSBkUU2swObcH5YxkMbkgSaZSJiuKIHzv02HKD Xlyau7XRhOsw+LrNT3pVvtmeYObTztcR8onr4+hROs/NakXSezjZPr3rnMBjKyOvzVpn GjK9TJCBx45mxRcSSxKwU3NsE4+cLKSWrQhI/rILh0i2U1DXGmAI0ZZK1tz3GM8xx3Ht aWZO4tXzgY5MUEm25MYzqOw9+2iInhCfqKpm7dcC1oruRRtUQjYjJgJgtq/YTvrV4tgM 8uVw== X-Gm-Message-State: ACrzQf3KJCFoaE//311ziWf9kYdiA4h2pgK6jNzLG0AqE7IkdfNBgeES PoaSJBA7KXQ/gIjmL7WSYGb2kdRhVt8= X-Google-Smtp-Source: AMsMyM6o+wSWVtlP/k3OvN0PrRnqRlZsOKw8O80E3hQXHJtq9zcBRQiv/bB23/VneQl4/AVTxh5RkUac2iw= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a81:46c2:0:b0:341:a26e:9d9b with SMTP id t185-20020a8146c2000000b00341a26e9d9bmr209204ywa.336.1663254330722; Thu, 15 Sep 2022 08:05:30 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:53 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-20-glider@google.com> Subject: [PATCH v7 19/43] 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 Biggers , 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 , Stephen Rothwell , 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; imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=NM4axnSh; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf18.hostedemail.com: domain of 3Oj8jYwYKCGUJOLGHUJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--glider.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3Oj8jYwYKCGUJOLGHUJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254331; a=rsa-sha256; cv=none; b=hUPiapmd2ATpOC2mbDbgD3YaRZSJcsHiLfQU0b4HFhaV+52iXQgm9tSegoqcctT//ZQmtb viEcNWLhdoHQjy9ChOmLEJGqapl0JO0lmLczxmHzv00lhVCNWv841U9iOX2MTeFYNboO1Q EZ5egE5r6kHHzZT8Yzk5H8TC3z9oG7s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663254331; 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=eGWHmCncvYGtki7dpdJUZ+ZMr1uc51g9yFEsa5ToQ1k=; b=auOsdjf6ud4UIRQV1AEdyYm1JEpSEIrYhLtN433AChnWvnQ6+B60gXk4trtBCIoG0tkO2f rBAk9sMzhiaMCGGhGlaNZ7fS5VeAm4/ul+qZtf/ElzCY2owemnAKOR8ztoNKpJMASjh0Dh 3mH7ng2WLBRZinQ3BrNZSA0cXfRMNOs= Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=NM4axnSh; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf18.hostedemail.com: domain of 3Oj8jYwYKCGUJOLGHUJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--glider.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3Oj8jYwYKCGUJOLGHUJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--glider.bounces.google.com X-Rspam-User: X-Rspamd-Queue-Id: 7CB861C00B3 X-Rspamd-Server: rspam01 X-Stat-Signature: eunh37rs9qxoc5qkzbjbxfx9bm5g8zxu X-HE-Tag: 1663254331-946989 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 Thu Sep 15 15:03: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: 12977538 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 00D72C6FA8A for ; Thu, 15 Sep 2022 15:05:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6013280008; Thu, 15 Sep 2022 11:05:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 58A1380007; Thu, 15 Sep 2022 11:05:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3DC8180008; Thu, 15 Sep 2022 11:05: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 2F04E80007 for ; Thu, 15 Sep 2022 11:05:35 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id E7BB24015A for ; Thu, 15 Sep 2022 15:05:34 +0000 (UTC) X-FDA: 79914643788.04.56EF93B Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf19.hostedemail.com (Postfix) with ESMTP id 8CBDA1A0094 for ; Thu, 15 Sep 2022 15:05:34 +0000 (UTC) Received: by mail-ed1-f73.google.com with SMTP id e15-20020a056402190f00b0044f41e776a0so13139425edz.0 for ; Thu, 15 Sep 2022 08:05: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:subject:date; bh=ZKRhstpgJFHnwQb7wa4PSp1fP2zpnAEklrA/+6U+C6A=; b=ThD5yyzHqBsvDfd8Iyv19jyZ2sM12IYTnbGoqnXN5npRxs4Zmln7VoA0117GVlyvP+ L1Rc2EkzfIcjXC3nUcY1fYQ+w1h7v1L8lLr+gl/b+Stf6tF7TTC0ieLLTB5H9+8MJwTV cPTvprpV3Hin238+XDy2bE/nJu/b+H/Bo1Gl9VC7zPy+I29iyDswAthi3+gScTUunW0f D99hzKEENBt+eup4SX3Ru+YBC5gcmF6lpKUFHmW/SZGEqpj0JNmM0Xg4CfE2Gt4d5QDl B5MQFU94qthIy3W4Vl2BeQjxRVxR6rhsIvHEco9D+hMSTS0FjckD4QYWcZtWhUe10Los eI6A== 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:subject:date; bh=ZKRhstpgJFHnwQb7wa4PSp1fP2zpnAEklrA/+6U+C6A=; b=4Q6pXoNyCBjM7g1Khsbz1knlYM/qGPSPTmex3ImVAPpxqcsbkuhfjP2RW7Q464Zmtt 4ZdpraNPTTZCpxooHJoTPhQTjvOwvERUC6FwKhw8I8MQMGVewPwrigd5+7LjOKFjLVMd wUSFmhItJzHyYP4BsFdj5XKZCkUH3HmxUFuh4z6YEzXtwFzCCPdlg0W6cn7vN7xJl8yW cXfmjt6MSdgcuKaY4nHMvlVumzmNlbm5hvYlahr8lmclJaxvqGFmJzCdZ6NAdnM2BeF5 5nuc6Cp+BjDpofmR4sQ+OJQa0dhQBv3NZzqp2JtH2dgTLbT/MvpJBN7vG+4GRghizSFy i3PQ== X-Gm-Message-State: ACrzQf2c4/3bkNRe/2V5xSJwb+I+KY+A1vE74hZ3DuKXt4sikVrcw2a0 6Qp97FmVydYclyENt4Cdu5VMIG0aenI= X-Google-Smtp-Source: AMsMyM4m3KxavKH5nPSgfXzlvt1xeKFIhjuoRkROmKkrPr/lpTSHT0pnpGf9Q4HdyY1KjTTGhTSnJGDa6K8= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a05:6402:3509:b0:452:20c7:5a95 with SMTP id b9-20020a056402350900b0045220c75a95mr237534edd.427.1663254333337; Thu, 15 Sep 2022 08:05:33 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:54 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-21-glider@google.com> Subject: [PATCH v7 20/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254334; a=rsa-sha256; cv=none; b=gH3nFWZqhvV2Kl1cqKUhjmSBZUMjWVeWKKh6bo4rPQTrxk0CUuH5yS4TF7UTiuHibBXsVh oNl9H3Dn8cnfb2q+TI+MWS/OnirWIP+NnRn5BRY82NrbgLWbPXnDvokmGDmYnGyoQR6Qgt WPSTcfP//C7ahzR27YouJ/LR5qz/Poc= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=ThD5yyzH; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf19.hostedemail.com: domain of 3PT8jYwYKCGgMROJKXMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3PT8jYwYKCGgMROJKXMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663254334; 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=ZKRhstpgJFHnwQb7wa4PSp1fP2zpnAEklrA/+6U+C6A=; b=ytuFeOaYmxJqrbEaQol334ixg6K7RZ9raxx45WweFE3SfeosrKRV5cEGDgruW3wUwZcGg0 of+yFWacxSsWzYfVCJUujcuznOOyvfT1Oec7coyPCUiY2/p0vecDUfJ/I9xbIT1CQVK++K 1JgnMCQ0ZYqhQ9Y9BKA7VFDc3fjanT4= X-Stat-Signature: 4zwxe896oskq985y34hatajfxpjzfiah X-Rspamd-Queue-Id: 8CBDA1A0094 X-Rspam-User: Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=ThD5yyzH; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf19.hostedemail.com: domain of 3PT8jYwYKCGgMROJKXMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3PT8jYwYKCGgMROJKXMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--glider.bounces.google.com X-Rspamd-Server: rspam02 X-HE-Tag: 1663254334-763046 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 Thu Sep 15 15:03: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: 12977539 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 51084C6FA89 for ; Thu, 15 Sep 2022 15:05:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E166A80009; Thu, 15 Sep 2022 11:05:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DC65E80007; Thu, 15 Sep 2022 11:05:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C401380009; Thu, 15 Sep 2022 11:05:37 -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 B51AF80007 for ; Thu, 15 Sep 2022 11:05:37 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 808C58042B for ; Thu, 15 Sep 2022 15:05:37 +0000 (UTC) X-FDA: 79914643914.22.1AB976C Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf27.hostedemail.com (Postfix) with ESMTP id 42D2740113 for ; Thu, 15 Sep 2022 15:05:37 +0000 (UTC) Received: by mail-ed1-f73.google.com with SMTP id h13-20020a056402280d00b004528c8400afso5034167ede.6 for ; Thu, 15 Sep 2022 08:05: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:subject:date; bh=oN1DmmB5hIGSextjMwU3Vnw2pm00V4zpd8aSRnyHf0Q=; b=HBcCaYgx3GOtGCD6vnRBTt3kop2ZY2C8h5F9Ts8g8FkVWY7Gs7+q7OujqY0j58mgyQ PpKv7dBrhRa+ULEFZpq6YIT9w4lLPJ3o23t1xy0FoDWMsuNH+5woxQMACI2EuJ0T06Fk TQpHI9dVcGN06VXZ17nt89f3hGpMkQO1B4jODqCAJBkjoP8KIDq6UcAT4rCV9jZa2ghf 6IC/hqZCHCxTjCfjDa3/RbfIoLX91TF8zSHe2qs82MOMbXBLbenq9eBUsVb3VSvh8+SB RdQyFUC4o4BFHFnVb96baewEylVhQuM+iDRPQR9M1/qp1ZO94YJAenY82wc+l2ZQVZqV A61g== 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:subject:date; bh=oN1DmmB5hIGSextjMwU3Vnw2pm00V4zpd8aSRnyHf0Q=; b=lsDHJdm4oGZBjqzWRnQtWMUe+uLESzzM6lkhg0dMq9ffcrJqcy1uQlS6Sb3ungx6lt IiwWTswr8EbkfJpGFvmtw+G7na+A1U/5evKbPyyyO/M2lpg5eUIV1cyT8ma70nfNDPzB 3ofAVHLLCokOeE3wy22dMSMB6sZn8IFF/zeSr06Gaiv5XqYHsvRPOm5Nd35ihSspO1/Z cxo+K8U+Iz6axDQXsjAK2zZq8bOek6ahbEJ21dltrLRGUROeTX6uZ6k1qlc7rIcugZck mdIQ4rZC7ddIqZoNNHcIMqCfBAlHMhStjkVvvkvElDadhVjyPPivbRElovTFANwunQYQ Xtmw== X-Gm-Message-State: ACrzQf3CA1CeN/N+2oD6KQ7GuRi875BR6SqKeQbXooBTbh52x5VEtg2p qMgqWAm5IgXoekTFjZ9x9rSIQR0bPZg= X-Google-Smtp-Source: AMsMyM6lsL6b9WP5OjlGGCg3dKk2Bj9nZi0tLG7AoCO3goElJf/yGauNpy2SVmCPAv5cw8xVUpzz1lwDIpM= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a05:6402:2694:b0:450:d537:f6d6 with SMTP id w20-20020a056402269400b00450d537f6d6mr275499edd.344.1663254335996; Thu, 15 Sep 2022 08:05:35 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:55 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-22-glider@google.com> Subject: [PATCH v7 21/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254337; a=rsa-sha256; cv=none; b=ESPWE9ImL6ZcuUo7n2RqU32XWK+U4amhd1YlSNNvigAQZ4lJftMv17HIYSZGa9yl3hYeK5 qFQCYsF6aAhp0tmPIy9wp9q/PXS0LE8iM0kAu+MFdg6GOntbMcMyYPr/QSJqZRM6cys1nv feC3M0zhJ8k5IlJAbUs2VqDFX0Y0iX4= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=HBcCaYgx; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf27.hostedemail.com: domain of 3Pz8jYwYKCGoOTQLMZOWWOTM.KWUTQVcf-UUSdIKS.WZO@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3Pz8jYwYKCGoOTQLMZOWWOTM.KWUTQVcf-UUSdIKS.WZO@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663254337; 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=oN1DmmB5hIGSextjMwU3Vnw2pm00V4zpd8aSRnyHf0Q=; b=bECUzPmHHZa5TYhst+KALS/mAOAaGPreoih7Rrg53MBQioFBofOHXQyRexqCBytz09Wn6H 0EhuXRzjhPoL2dseg18GRuLkArXsXbDtSKBOqbMCgaY5VTk5pTv27J/DWFAvPHAq6yKHHq GFzV+B4JHl+DllBXOcH5N1PY9VOQMKo= X-Rspam-User: X-Rspamd-Queue-Id: 42D2740113 X-Rspamd-Server: rspam05 X-Stat-Signature: xs6ptoagtmx1pdu8t54jdu3wj764ueop Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=HBcCaYgx; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf27.hostedemail.com: domain of 3Pz8jYwYKCGoOTQLMZOWWOTM.KWUTQVcf-UUSdIKS.WZO@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3Pz8jYwYKCGoOTQLMZOWWOTM.KWUTQVcf-UUSdIKS.WZO@flex--glider.bounces.google.com X-HE-Tag: 1663254337-664915 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 v6: -- add a missing #include Link: https://linux-review.googlesource.com/id/Ia162dc4c5a92e74d4686c1be32a4dfeffc5c32cd --- include/linux/kmsan.h | 41 ++++++++++++++++++++++++++++++ kernel/dma/mapping.c | 10 +++++--- mm/kmsan/hooks.c | 59 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 3 deletions(-) diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h index e00de976ee438..dac296da45c55 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 @@ -16,6 +17,7 @@ struct page; struct kmem_cache; struct task_struct; +struct scatterlist; #ifdef CONFIG_KMSAN @@ -172,6 +174,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) @@ -254,6 +285,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 27f272381cf27..33437d6206445 100644 --- a/kernel/dma/mapping.c +++ b/kernel/dma/mapping.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -156,6 +157,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 +196,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 5c0eb25d984d7..563c09443a37a 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -10,10 +10,12 @@ */ #include +#include #include #include #include #include +#include #include #include @@ -243,6 +245,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 Thu Sep 15 15:03: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: 12977540 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 2CCD8ECAAA1 for ; Thu, 15 Sep 2022 15:05:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B581B80016; Thu, 15 Sep 2022 11:05:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ADF9980007; Thu, 15 Sep 2022 11:05:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9A83680016; Thu, 15 Sep 2022 11:05:40 -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 8B29480007 for ; Thu, 15 Sep 2022 11:05:40 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 54D731C506C for ; Thu, 15 Sep 2022 15:05:40 +0000 (UTC) X-FDA: 79914644040.29.4F3F4B2 Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf17.hostedemail.com (Postfix) with ESMTP id DF21F400AC for ; Thu, 15 Sep 2022 15:05:39 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id v11-20020a056402348b00b004516e0b7eedso10465267edc.8 for ; Thu, 15 Sep 2022 08:05: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:subject:date; bh=lOWe2UvDp+lzLw75WkGUXRoANIiWBb139LkOr44Ytus=; b=r8zwtK7nR6FXPBS442hkgib03/UyNe7gNUiweombLjUeUOwj8SF20sAORY+JgIxfJC xdQzkfFn7/UIZp6qs2BkVURbVTIpmVyq4CogUpIZzqnkOvB4czCVAsUxHuaXyYzg7wYj iSHZbV2gUMNZdtwTkVyP2zImSyDUIobXdqb3vW/gaStenqfHXMWXmhtPDGK7i0t2+x+V LX9ocp5gmEP0hXd6aLkG4expCb4w3nGprape1+X4fjnbTnlSElSS0OcneVvlkPCu1k1v zulBsEXPwjPW//HgEDA7XFR0M2WbDQmJA13wrulrEaaWrxCq8Z7S8FbCgch7VF6L5ax7 LfJA== 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:subject:date; bh=lOWe2UvDp+lzLw75WkGUXRoANIiWBb139LkOr44Ytus=; b=YZ4/WfBFS6iEcG6Inm7XL8dNi/h6Sc9x9iugZwkR7OH2J9AdvgfTbV9XOSEPJDcYwZ VUneVb/PZhjB4dY/NfPlbxSCFgfgGkij6PqbpHRQy5j+o8s8KXKRCom7LwRtTyphPSBI +Qlplla8AYnScgr89ZgYH+yT4OICqDU5hdo7ZX0dIHQzMTIEfCbJtNz58vYoEvHJXRqr phRe2dpiBayuV0NU5xmxx5qfPvmtk7JBtNa3A1y5PKK3VQ9gmHS5QnwF4IgpBGw6bhMv 2eKJGTpu4EVnoZEfnt5LQgNzPrnCN1hDJUZSXQcDcihabQvetnUJgymmj1X1PRfKyq3e a+sA== X-Gm-Message-State: ACrzQf37nsHOndDMX1WY2Tgy+w8SaqNvR/BB85qOrNA6YedpLschP5Oe zH8YmjfJ96636XWiA4hH0V7f6au63s4= X-Google-Smtp-Source: AMsMyM6XUfiL29ncKe/ZK/EpTsQsoLddOpxaN22/2HU0VmNdXXQuif+Od6cd/SXFxh2r6j465cljcQVOjxc= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a17:906:58c8:b0:6fe:91d5:18d2 with SMTP id e8-20020a17090658c800b006fe91d518d2mr326676ejs.190.1663254338524; Thu, 15 Sep 2022 08:05:38 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:56 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-23-glider@google.com> Subject: [PATCH v7 22/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254339; 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=lOWe2UvDp+lzLw75WkGUXRoANIiWBb139LkOr44Ytus=; b=RE+IHAiWO8kFG4C6mFZvAQQ5c2d2tHmhDD5kvC29KnLrJ/i7ItfxemOGegiECl8aAnDCaH J/tvE2Qtcr4b2IIuD8p0WEtxEDuWMJf4EVsbXjc3T/30vkJW/7pQuNhN8ffuyjv/ZTlIgr 8166ddcIr/4t6bGD0RVSFNidhxJwWnI= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=r8zwtK7n; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3Qj8jYwYKCG0RWTOPcRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3Qj8jYwYKCG0RWTOPcRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254339; a=rsa-sha256; cv=none; b=M0i5f8mP52pAkS6fzsrzkj2Z1EwEVKv7GrL5Z3vDQV0+h12SH9ado2UYrQFjJkcX0Vt+m5 tq5W2C+TGLZ5Ldomkd6ANRMYLXtFwJoVnKduleKs7mRjdEfup/GTF2gzpMUPaKcDK7A7W+ GxQSCoWQPM1tm1Q0UEPRa1v28tMuPGA= X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: DF21F400AC X-Rspam-User: X-Stat-Signature: w6gk9azqz3bwk4r97nptwpp5jaq9yiqp Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=r8zwtK7n; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3Qj8jYwYKCG0RWTOPcRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3Qj8jYwYKCG0RWTOPcRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--glider.bounces.google.com X-HE-Tag: 1663254339-854588 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 v6: -- use instead of 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..8974c34b40fda 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 Thu Sep 15 15:03: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: 12977541 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 AC1C8C6FA89 for ; Thu, 15 Sep 2022 15:05:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 16C6880017; Thu, 15 Sep 2022 11:05:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 11EF980007; Thu, 15 Sep 2022 11:05:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EFEC380017; Thu, 15 Sep 2022 11:05:42 -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 E08AC80007 for ; Thu, 15 Sep 2022 11:05:42 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id BFC64806CB for ; Thu, 15 Sep 2022 15:05:42 +0000 (UTC) X-FDA: 79914644124.15.987DEF0 Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) by imf27.hostedemail.com (Postfix) with ESMTP id 6E23C4010C for ; Thu, 15 Sep 2022 15:05:42 +0000 (UTC) Received: by mail-ej1-f73.google.com with SMTP id xj11-20020a170906db0b00b0077b6ecb23fcso5864221ejb.5 for ; Thu, 15 Sep 2022 08:05: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:subject:date; bh=2KqHkwy5ct3iw6Sq4tv6KYhLxwZCRI8E9SKEfBphS/w=; b=fPu7uIFtmE//tUnrLa1tnXaL06eOVtU9zQx1jD11vGKa9vrSb/SjRvfVtu20ImD2aq ktyAMM5zlghdVPv+ZNdaxRTBLC9V1FJxhA201qoYNf/tpvo/3r0+8ib6QSN3MOcY4115 spN1PPpnPtfSV6K/JJxozWg2zCM88cCSCywBiUfgKmXQbAosetv4LctjU4+UQP+iFrme 28d6xc5S+HnxO4hALfhDVVmueAJrgRI11Vk+WzMY8U4PpZ8rrLGIKkMWARAPUu2nHhEu vae6RzSCdFOZzWb8P6yu+5HmBos6yFQTgr9rKZ9cnfoNEOHUwMwWfwXrkFNReFByX/rE fe9A== 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:subject:date; bh=2KqHkwy5ct3iw6Sq4tv6KYhLxwZCRI8E9SKEfBphS/w=; b=pyn2IuMNqsdI1+p6td/gZ3venGmF5NiFtdEknR7GlGthHGyVOKSBk4wrE4Y6e4zsWA 15L8KHcNOqDke4vMXmqjsCpcAULRdyQexKPJPjQBavMBpp3ThW7CTxP5X5Nc4H7IF2y0 UBWTEbnWuBJQYR2cUUzkrxtFOd3vKQ1MnyAbTFgDOCfNSekqPFCHdhZe+Ot4Gb80fRn/ 9ncwjTlRr0jy3r5Srw0e2kIX+Zm+7UKG7cJDf3jLb8qpTZAEPHYjj8QQnVOIwe6B0D1S pwhG/FtB8J1NcKVgWwXpOcgSk1SkhPnSqppWc/+0pUzk72KCva+JP0hULRQlHsDfl0b0 t2Lg== X-Gm-Message-State: ACrzQf14ciGwzp7qhlgeGWHzdLd5gr7C+PSVi7/2Z32glX+VTdtzdbSB 0EPUuV0aDDidXr3zmFf3GyDO27BXo7o= X-Google-Smtp-Source: AMsMyM4HcOp+9qUt632hCRIrvaKzGPgGgVNgbAXsPiaoxZj4kxm9xBjgLt4GEuAE26rJcVwJSimZeHtdWr0= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a17:907:2e0d:b0:77e:999f:dea3 with SMTP id ig13-20020a1709072e0d00b0077e999fdea3mr271626ejc.317.1663254341181; Thu, 15 Sep 2022 08:05:41 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:57 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-24-glider@google.com> Subject: [PATCH v7 23/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254342; a=rsa-sha256; cv=none; b=eOA8QLYeKW28KrSs6O4Zb+MXBxjwM1pGbEK76MVc9kiEc1xXXNnFYimcT8jYxKAu2eyeE2 1dXRcrsW1eaaa4B4rUiupaVTv7i4m/xFvm/JT4J0dytv5gsuTriR5AsUWEGYBZr87wt6K0 K9HflAwm/Ho7AXTqt/nK35wXpJ4dZ6Y= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=fPu7uIFt; spf=pass (imf27.hostedemail.com: domain of 3RT8jYwYKCHAUZWRSfUccUZS.QcaZWbil-aaYjOQY.cfU@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3RT8jYwYKCHAUZWRSfUccUZS.QcaZWbil-aaYjOQY.cfU@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=1663254342; 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=2KqHkwy5ct3iw6Sq4tv6KYhLxwZCRI8E9SKEfBphS/w=; b=MHVEYKfFr/fZT1uRNDGZzx2iE0gUO4hCOQ2ioFujOdRZlFByAG5Mn3YD3oraYHZicSjtu0 w65JZqJnZh3TxQq+/58Yxt7qorJg8vD6FjxF8zOaPnxbAHLptcdcH+fbSsWl6zBqXSyu9a aB7/y/dSRjel/4ZZmcFf908mTdtwEWM= Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=fPu7uIFt; spf=pass (imf27.hostedemail.com: domain of 3RT8jYwYKCHAUZWRSfUccUZS.QcaZWbil-aaYjOQY.cfU@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3RT8jYwYKCHAUZWRSfUccUZS.QcaZWbil-aaYjOQY.cfU@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: r9detafeqtmjhrbm4r6ytn17cesihntg X-Rspamd-Queue-Id: 6E23C4010C X-HE-Tag: 1663254342-780969 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 v6: -- use instead of 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..9f3c54032556e 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 dac296da45c55..c473e0e21683c 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -18,6 +18,7 @@ struct page; struct kmem_cache; struct task_struct; struct scatterlist; +struct urb; #ifdef CONFIG_KMSAN @@ -203,6 +204,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) @@ -295,6 +306,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 563c09443a37a..79d7e73e2cfd8 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -18,6 +18,7 @@ #include #include #include +#include #include "../internal.h" #include "../slab.h" @@ -245,6 +246,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 Thu Sep 15 15:03: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: 12977542 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 4AF8DC6FA89 for ; Thu, 15 Sep 2022 15:05:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9FA6680018; Thu, 15 Sep 2022 11:05:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9A99980007; Thu, 15 Sep 2022 11:05:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8212580018; Thu, 15 Sep 2022 11:05:46 -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 7045480007 for ; Thu, 15 Sep 2022 11:05:46 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 3B585405B8 for ; Thu, 15 Sep 2022 15:05:46 +0000 (UTC) X-FDA: 79914644292.05.9E3074E Received: from mail-lf1-f73.google.com (mail-lf1-f73.google.com [209.85.167.73]) by imf11.hostedemail.com (Postfix) with ESMTP id C8213400A5 for ; Thu, 15 Sep 2022 15:05:45 +0000 (UTC) Received: by mail-lf1-f73.google.com with SMTP id a24-20020ac25e78000000b00497ad54b081so5542387lfr.2 for ; Thu, 15 Sep 2022 08:05:45 -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:subject:date; bh=3JQH+J/1Qh9DH/M6ilWXMVRmgzlMCJMKRJCWECEwFhM=; b=hVH6JxmqkPAVzxeh25koLhHIC+qlvQ/v36Wv3NB3JdyqAusa/ZzT6ma1Ky+EuJeUT5 DYU+gsGphMjPbBGQ/fO4+YQHA4cDEhDqfkCIE1oFQrI9RqKyCg0R5uF4g7oGIAZyQa+r plOkgO7rIgngF6jlBPQ8CRDb213UVNRZCB4zW3IpKY95uj9/tQQeLbO9KS7kWXZqzHxc XifZgy400obs25pNvlwFPEkLgryaR0prvF3+cbKzL29bVoCw5CqbYiZ0rhJMpT/U5mrr CX1+jfh7DA7iLEafB5UFAALxMwVXUT+IZqPd0wngPrItnGUDI25hkuS0Zjfh2rRRNXhC X2cA== 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:subject:date; bh=3JQH+J/1Qh9DH/M6ilWXMVRmgzlMCJMKRJCWECEwFhM=; b=jU23r/nsCY4e9j/qWAieR8DNNp5GqE2FlM1Nf0mQXtJPDYmzW2M4lVKhomEC+zofLA ct2ueURZHBMnE5G5lLRhKlzWhUoUow1WzNulRVna1r9hdDh5uRK2Z1YW3A+sOrbUacbq gZsin9oQNTapxg8w88mCExw00FNaWVhp1gZf46T+X2cR2lOLd/vCwe4UaaCfHNpwewsz ly7OuNdIu8JNBc8XSnDkp/N1buhafyQ7ddH+1TrWCmMXFmOPbIfj0mUgljO0RANJq5iH b79xHOxRbhMNKOImU0L84dPLLS2MGYM/4vAxW8P6iCGzfXSI/dKypgKZhuhPLHmY5n/i MjZA== X-Gm-Message-State: ACrzQf3F1VEAX9jUXNinxekODrhmSj+ZM1TLYJiE9Ac2tZhe10P2EwuP Wn4nxKFtsfpV5iiDaAJ9z5F5sxqnJl8= X-Google-Smtp-Source: AMsMyM5V1/lgGe3MYN2LPDbxUiTzSwP3Ml9DNSMU1gaSoc4tzm3jrt1fIH5eYBdawlwar5MFfW/8kwQd+aI= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:ac2:5f98:0:b0:49a:d9ad:f9c5 with SMTP id r24-20020ac25f98000000b0049ad9adf9c5mr124072lfe.14.1663254343865; Thu, 15 Sep 2022 08:05:43 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:58 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-25-glider@google.com> Subject: [PATCH v7 24/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254345; a=rsa-sha256; cv=none; b=D6sEn1BMlQ8twgkRWE1zB0ICCvtO+8LYNLS1H/lQ+4pVRCQ+zoi3uu7fqacj05vdHJWT/F nxvCRHsxKuR2LxwmbJ8B0oIthmKbRYTGgEiFOy5/WdN47c1JChZ4IRkev6RWQpj3YZ5YvL PM25zYYPKGx/1JNHLK0G/YFHZ1SkPZU= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=hVH6Jxmq; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3Rz8jYwYKCHIWbYTUhWeeWbU.SecbYdkn-ccalQSa.ehW@flex--glider.bounces.google.com designates 209.85.167.73 as permitted sender) smtp.mailfrom=3Rz8jYwYKCHIWbYTUhWeeWbU.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=1663254345; 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=3JQH+J/1Qh9DH/M6ilWXMVRmgzlMCJMKRJCWECEwFhM=; b=cJSO3WI9bu73M6tR8BN6PPf5HfIHIUJ5JP6pnCE3jwexmvBsVQVAbDlh4WyeEyoFbrg/Qc UOEfq8CppihrKm1wnIcxtNOhlD0we9L/4CUkFFWeAfkp9GV/YmgN445472MclNJvjmFTnH K9XLNFfHbgUTo9VXpn2bf2UcXhDlWQw= X-Rspam-User: Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=hVH6Jxmq; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3Rz8jYwYKCHIWbYTUhWeeWbU.SecbYdkn-ccalQSa.ehW@flex--glider.bounces.google.com designates 209.85.167.73 as permitted sender) smtp.mailfrom=3Rz8jYwYKCHIWbYTUhWeeWbU.SecbYdkn-ccalQSa.ehW@flex--glider.bounces.google.com X-Stat-Signature: 4459hthit9zg9odif9pcm84xmjqnhpy7 X-Rspamd-Queue-Id: C8213400A5 X-Rspamd-Server: rspam09 X-HE-Tag: 1663254345-948155 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 v7: -- add test_long_origin_chain to check for origin chains exceeding max depth Link: https://linux-review.googlesource.com/id/I49c3f59014cc37fd13541c80beb0b75a75244650 --- lib/Kconfig.kmsan | 12 + mm/kmsan/Makefile | 4 + mm/kmsan/kmsan_test.c | 581 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 597 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..9a29ea2dbfb9b --- /dev/null +++ b/mm/kmsan/kmsan_test.c @@ -0,0 +1,581 @@ +// 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 noinline void fibonacci(int *array, int size, int start) { + if (start < 2 || (start == size)) + return; + array[start] = array[start - 1] + array[start - 2]; + fibonacci(array, size, start + 1); +} + +static void test_long_origin_chain(struct kunit *test) +{ + EXPECTATION_UNINIT_VALUE_FN(expect, + "test_long_origin_chain"); + /* (KMSAN_MAX_ORIGIN_DEPTH * 2) recursive calls to fibonacci(). */ + volatile int accum[KMSAN_MAX_ORIGIN_DEPTH * 2 + 2]; + int last = ARRAY_SIZE(accum) - 1; + + kunit_info( + test, + "origin chain exceeding KMSAN_MAX_ORIGIN_DEPTH (UMR report)\n"); + /* + * We do not set accum[1] to 0, so the uninitializedness will be carried + * over to accum[2..last]. + */ + accum[0] = 1; + fibonacci((int *)accum, ARRAY_SIZE(accum), 2); + kmsan_check_memory((void *)&accum[last], sizeof(int)); + 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), + KUNIT_CASE(test_long_origin_chain), + {}, +}; + +/* ===== 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 Thu Sep 15 15:03: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: 12977543 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 573CEC6FA8A for ; Thu, 15 Sep 2022 15:05:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 84B5180019; Thu, 15 Sep 2022 11:05:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8203480007; Thu, 15 Sep 2022 11:05:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6EA9080019; Thu, 15 Sep 2022 11:05:48 -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 5A8A180007 for ; Thu, 15 Sep 2022 11:05:48 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 2A38B801F7 for ; Thu, 15 Sep 2022 15:05:48 +0000 (UTC) X-FDA: 79914644376.28.0018E1C Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf16.hostedemail.com (Postfix) with ESMTP id E218C1800A9 for ; Thu, 15 Sep 2022 15:05:47 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id w17-20020a056402269100b0045249bc17a9so6685092edd.9 for ; Thu, 15 Sep 2022 08:05:47 -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:subject:date; bh=PRETZsLeKtd8er/vGZIvwJ67UbRQnpYPS7VnNd6iitc=; b=IWkX75T/Q/431JKoP2sw1gGv+TRTk4KNLPsHPqPmF3DWGGkurKMl+8ONAEiBTDmheE /bRfKFH3UY7lflgk+tGWw2/6W93k+canHKGQEEM6HO9CEQKXMkCUdWv2f4MdMGWOzrIJ G1jDnp7yxG83XTB77HooBd8pj1DXFnHlOsQNax7eY08XATzldTmtPTs26zPGXzgiy0Mz qNMjVTPKt1EwXBmVdFMqJwU0Hi3WzmYliSNKgIFTflzD9qXlcsCQbN9DI90LX0j/d90o XtOjNQuQkTbxe0CH70hTFyO24aVCsu/CiMUIXLlZA1dstaOwJf3am2bgGgOOrjIMUJcm Q/jQ== 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:subject:date; bh=PRETZsLeKtd8er/vGZIvwJ67UbRQnpYPS7VnNd6iitc=; b=R8qfEHZ9hfzdYrbFxcGEyyjn4nyg0YlJ9SqUuU1YnCArJA/XpySwwrEWOteOOR2fqB FwmN+XJgIV0OEKytGHmkT65QKDqDrSR+ZdCuFVTK8bLijTExAWajh897A4lp4+AGlrAf 5OKwI9kfMFqn1cAYQ10hJk4bNBYZzTqYhyg/RiN8w5GCi6xtXh3NcRjproqvQQkkRNvk JyhIgBv9pIJTlYcA+v+Uufcw5E7nXAt5lS+MU1yOAIBHjzAh0jy5DlqzpA1YlnttNcid Sv7KFvYUAXW2RyrPBysE1wIE8aD2S1jRjcD1UslzkzsoUkeNSKvfEd8tzpC9dB1Z/RY+ UccQ== X-Gm-Message-State: ACrzQf2u41lsQPTqHltrSmZcBs7pWUpQupN4yAtamgCPD3slTBaR4Tgi oRwyS4vjNj4Jpqt+7Uoi+V8bqMqDTfc= X-Google-Smtp-Source: AMsMyM6VUn1IGxuterUYBQnBqfqJp+t0ecAEWvO8gXW0QL3/We8FwA+dhbmbRRZVNw5aXpi14wf65X3+jsg= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a17:906:cc59:b0:779:f094:af3d with SMTP id mm25-20020a170906cc5900b00779f094af3dmr278250ejb.239.1663254346675; Thu, 15 Sep 2022 08:05:46 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:59 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-26-glider@google.com> Subject: [PATCH v7 25/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254347; 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=PRETZsLeKtd8er/vGZIvwJ67UbRQnpYPS7VnNd6iitc=; b=aca5gCy0MPGOc0vQIW21RgVwlP64h3kvWq9eAXcGqZKZUpxvUCYqLqN/ZDfE7+UA42dNp3 nQPuREP1TjqU0wI5QnPFMaIkcjNmfglXhJMUk/LrQa9VocqvzqVtAJXfmX26EC5IoPYz7f 04kACTT6ICUQHwu++09kF2b5m+fOVpE= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="IWkX75T/"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3Sj8jYwYKCHUZebWXkZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3Sj8jYwYKCHUZebWXkZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254347; a=rsa-sha256; cv=none; b=8T/Shl8zXJLIIPqXTP3u9/znl2hMcuxTVmsDC/N0TCgHNu0fjGEcP4TmAS+Bi0nlP0Nqof r9JrqglMYilbljpmHtc2pvJiu6nsyw4jjhmwXAEndexdnZDrq3hG9yEGPz5U4B+shlnkUw IFcAsjeRiAhZW8fqzxtg2DU1Q9IMOxg= Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="IWkX75T/"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3Sj8jYwYKCHUZebWXkZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3Sj8jYwYKCHUZebWXkZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--glider.bounces.google.com X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: E218C1800A9 X-Stat-Signature: 8xgkwbd7cjmbxb9crr1mdecijctukr7m X-HE-Tag: 1663254347-263890 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 Thu Sep 15 15:04: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: 12977544 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 3954AC6FA8A for ; Thu, 15 Sep 2022 15:05:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C46E08001A; Thu, 15 Sep 2022 11:05:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BCAE280007; Thu, 15 Sep 2022 11:05:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A44778001A; Thu, 15 Sep 2022 11:05:51 -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 8AE0980007 for ; Thu, 15 Sep 2022 11:05:51 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 6EB9880718 for ; Thu, 15 Sep 2022 15:05:51 +0000 (UTC) X-FDA: 79914644502.19.12C7BBB Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf27.hostedemail.com (Postfix) with ESMTP id 8BA1A4011B for ; Thu, 15 Sep 2022 15:05:50 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id y9-20020a056402270900b00451dfbbc9b2so8664866edd.12 for ; Thu, 15 Sep 2022 08:05:50 -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:subject:date; bh=YUiFcVJy4Yf/nhkRGOhECdnAl5j2ey18UxYwbxsEwuI=; b=VliFMYVCDV7kkfINdTarJFmTqEnAbLruNe4y2nh9+Gq3VoQNDJHY/G8icVqvbom895 brVwQJJqmN+1DA0mu3l7223zCXYh3MLi6EcD3O7vSukDbUCMRBkT/JTY/9X8sj8PYdCj yUr8tmusiXJkGxomlBwBu6R6MPATnE4bP1xG3IhMXHmnAZfKYfvEfT+WT5clMVBcx9uK ksH5QVkMeQYnvZSE1GaSzTynyWZlFn+TznfEbu17bF2QQJFQrjIrtK3vdIQbBeQFK8gt iMdBUyoRRS8W32sscSSLMsDhA2N/RH0XjxNVfASBZ4SFVt3aMugme3OYe/GyigUvVeKb TRdA== 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:subject:date; bh=YUiFcVJy4Yf/nhkRGOhECdnAl5j2ey18UxYwbxsEwuI=; b=lWs/IhFbAF7B+1PtitQChxciZ7tHJw0qpaWPgQE+7+LUNo5ltfU0/bX2jVY+x7qaJN JlFa8aYpplm4nXMgSrx4U1b23rxoijEPvXQlQIpfbvXssirS1ob21yB4nW/Mbdkpw07A GmZYtOSNPPAy/ijerdnwF5tExqmL2DEpHBxeYbcMWYB/jN0oGy8+iR1cURJpHu0/a3Rp 7VIDHqafojSll1hFuPMlQ/5G29MRY8ykrPYLvngoA22wUpYUr+asP2cPC0lgVqnLvkE/ TM3pi4nDV+Ax9RwYiZizBpWK9OjwonxlMrj7rOT1/o++eS2zVDTxlWUskL7apSYlk88c ajsg== X-Gm-Message-State: ACrzQf2Uv6yKL+MYuyTgK2qwPSgR6Bn1bzIL54M/vMGDlFxkN8nqkfrj UJfW7hSLJ4u0qxqhlMwo7MbssL2Wwyo= X-Google-Smtp-Source: AMsMyM4XARs1mAQwOOJARkV/G/VxGNdo1txjOOf+nIPgNq7xRC+Bo39h3XpWkd076eK9QSmqMao1Wd/yKRY= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a17:907:2722:b0:77f:c136:62eb with SMTP id d2-20020a170907272200b0077fc13662ebmr301739ejl.522.1663254349203; Thu, 15 Sep 2022 08:05:49 -0700 (PDT) Date: Thu, 15 Sep 2022 17:04:00 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-27-glider@google.com> Subject: [PATCH v7 26/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254350; 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=YUiFcVJy4Yf/nhkRGOhECdnAl5j2ey18UxYwbxsEwuI=; b=W6nJ/aVpvKo7z6G0pJbKYVGq3uTvUda9+bgXSwyREhIT5S8eVv20eZG8cOoc0lRn5/5asJ 4fzdSD3QMHDE9jGnZ1bjxf7Vy0yZY7qETA06rCFvzrvkYcUy82ac3nM0CxcKddZ/cqxFfL 6GxVS/e67I1OMzeWhV0h3OiR17yr6QI= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=VliFMYVC; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf27.hostedemail.com: domain of 3TT8jYwYKCHgcheZanckkcha.Ykihejqt-iigrWYg.knc@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3TT8jYwYKCHgcheZanckkcha.Ykihejqt-iigrWYg.knc@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254350; a=rsa-sha256; cv=none; b=KCS+9p0o4WciLNIkgS8xWfD+Liqo9BRXmu9KJTN2lLFmScQtGByaGSlWRGJ4DzhWgLYyG4 Y/9sLj91Mlmyoj+UDTeH4v+fPlXP4tXa300No6QeVx5s8zJLL5KMnmLdnfrU+qhbBbhbX+ LFz6a3NDYJAQtj+s2mpITX1AYi2px5Q= X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 8BA1A4011B X-Rspam-User: X-Stat-Signature: ddi7fuc1yo7iae98nhj7pqyo9jgupbau Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=VliFMYVC; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf27.hostedemail.com: domain of 3TT8jYwYKCHgcheZanckkcha.Ykihejqt-iigrWYg.knc@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3TT8jYwYKCHgcheZanckkcha.Ykihejqt-iigrWYg.knc@flex--glider.bounces.google.com X-HE-Tag: 1663254350-512752 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 Thu Sep 15 15:04: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: 12977545 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 F1521ECAAA1 for ; Thu, 15 Sep 2022 15:05:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8716480011; Thu, 15 Sep 2022 11:05:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8478280007; Thu, 15 Sep 2022 11:05:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7100280011; Thu, 15 Sep 2022 11:05:54 -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 5E87B80007 for ; Thu, 15 Sep 2022 11:05:54 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 23FAE160381 for ; Thu, 15 Sep 2022 15:05:54 +0000 (UTC) X-FDA: 79914644628.18.CB56BB2 Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) by imf19.hostedemail.com (Postfix) with ESMTP id 737EB1A008F for ; Thu, 15 Sep 2022 15:05:53 +0000 (UTC) Received: by mail-ej1-f74.google.com with SMTP id gv43-20020a1709072beb00b0077c3f58a03eso5566770ejc.4 for ; Thu, 15 Sep 2022 08:05:53 -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:subject:date; bh=T4yulc1U4RDI/lAlxqlpzG4AIUYO9fQrzg+B84ydySw=; b=bjU5Y1Uj6lzmlmNdbvfJIhixfGz/+MKGRYCvur483Xf78D7CTzrNFlvd42wNziHYle Vw8zzRUPc25b8rF96my1wPCxCLp6TfupI3RftJKXAn7dq2QlPkGvBgmhor+As+69qJvo ts7cyW+lzlkiH21NDXMKOi08blf2XU9uBJs1YmQtKDbV3CYIcVN2LJH0wP7yDNAqAidw 2NVtGckV24urbHpg+UiXfOqAjGlT6CwNPdgB8y3HFou9URtWV43Pi53vkh8E0/1cN/xT J7/cYcC0yfDbDZhb+v1n2EFZPIQNAXrNcvBidelmwV20Zr+dNqjJnJC6KZ8B/YMPJfPN pmuQ== 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:subject:date; bh=T4yulc1U4RDI/lAlxqlpzG4AIUYO9fQrzg+B84ydySw=; b=RtIOl2Rl2r1a5jCDTa1une38hvYihRlVBaf/ueurBQKYHxLlAcu8t0znQJD8y+HjbZ cSuiXOUbZHIN6tsUCY9Vg4Aal4EC6BsWt2bz/WVUMbv793fKywoS+3IrgJMASNqbs7l8 DLrCwb9XiLF2qJiDLgqHc5g95DbV1Dg0o9DRUgWYYsAUjBEGL7DRLeIOc2Gj2Fu+SrLP FHA48OlXitz9q/h7CXzVhEnl6O8Q3L2vIWh0OAcaFGxolBlTWmXtZ5Mu8LKB/DElelML gO6Yo5HbVaohqMi1y7eMn/e/6qILrsw0YSttCUfi3xdyiKeZT2sXbLKbPU8oTuWOXbqq bmiA== X-Gm-Message-State: ACrzQf1O06JuHyg2AEADhZ9RcXyS5V3Y3tTTC3Ax51HCT03Qx4VRS775 xIaxGlWLOohGltHR0QU0v4M4xg1sAT8= X-Google-Smtp-Source: AMsMyM7l8lWYGJuBODDQ/HU6R5pl1eaYbgBKHbyvWwWvFjThOz32FII2oCbiswhP6heZLHjrzuBd6Y58nd4= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a05:6402:d06:b0:440:3e9d:77d with SMTP id eb6-20020a0564020d0600b004403e9d077dmr260579edb.286.1663254352056; Thu, 15 Sep 2022 08:05:52 -0700 (PDT) Date: Thu, 15 Sep 2022 17:04:01 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-28-glider@google.com> Subject: [PATCH v7 27/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254353; 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=T4yulc1U4RDI/lAlxqlpzG4AIUYO9fQrzg+B84ydySw=; b=686GdvxVe3a9AzUQyyjIx6LGQJvemXunun7mFlw+0HcPIP1pbDBAYckH5RP23cUxyaHosx VXMPjjnJ/K/e4Dep4DyWDgi0HqqPZWl07RcYJYEKkGAbHlHj4j09m71ADkHd0BsN+wb0QK LG3GM40VA2EzVjfR392CMdn/JOJxT74= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=bjU5Y1Uj; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf19.hostedemail.com: domain of 3UD8jYwYKCHsfkhcdqfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3UD8jYwYKCHsfkhcdqfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254353; a=rsa-sha256; cv=none; b=GWWmqtoirekDTaTjcG2HqDQgXdBqldO9cSTaC4frKvkifxguZbVGYy+OrjdMb+EhtHtKqB 7gLXDpYpPBHu3qRDs6TpXO3+LUIXLO3wS3RL9DL4/XoDTjlmwc7zgL8qTaH1mwY15XUoUx iIyyUYj72nJJF2fahfNt90z+qQgl6LI= X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 737EB1A008F X-Rspam-User: X-Stat-Signature: 6czqigoyc8q83cujcdzmhnj4nfzojtpd Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=bjU5Y1Uj; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf19.hostedemail.com: domain of 3UD8jYwYKCHsfkhcdqfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3UD8jYwYKCHsfkhcdqfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--glider.bounces.google.com X-HE-Tag: 1663254353-284594 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 Thu Sep 15 15:04: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: 12977546 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 0D40EECAAA1 for ; Thu, 15 Sep 2022 15:05:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 84A3E8001B; Thu, 15 Sep 2022 11:05:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7D36380007; Thu, 15 Sep 2022 11:05:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6737D8001B; Thu, 15 Sep 2022 11:05: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 5A80480007 for ; Thu, 15 Sep 2022 11:05:56 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 3E84B8041B for ; Thu, 15 Sep 2022 15:05:56 +0000 (UTC) X-FDA: 79914644712.13.CA8252B Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf19.hostedemail.com (Postfix) with ESMTP id E65C81A008F for ; Thu, 15 Sep 2022 15:05:55 +0000 (UTC) Received: by mail-ed1-f73.google.com with SMTP id b16-20020a056402279000b0044f1102e6e2so13539231ede.20 for ; Thu, 15 Sep 2022 08:05: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:subject:date; bh=r3HZXk/BEn65kcaybENH+gdA6j04yycM3f96/pfDo3Y=; b=aQq72eadbZwq/GT1Z6Tvw/xmOlMUZLlJTyI0TzrhiZrves5ntWwvh+3uFDLBYpLZrJ h1ikkP9rXSVFWTA7ciUJFF1f9AUJxqJ97pqMJydqQcXAEzGr5ROFVuNaZF31wV1P7g8M nPgn0UmEt2qu/qdoDASndOpnDkzYfrDFlTkTXR1TZFGxdGAy2NWU1EHXUoqxbAKmtWHq Drjc0QS6zlbEIhSvlyonau7djr6wX8/b9A42B31BEiQEkTzvbOq7cf836X8Y6zwp4Xt2 5z7af56B6lhbyz2HnKF7iuuVKYQhx5EEMxMssHo7LL1JYSTlA627gSdeqm8QJnNMhwiu c8cg== 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:subject:date; bh=r3HZXk/BEn65kcaybENH+gdA6j04yycM3f96/pfDo3Y=; b=NQ5ESZemlwBrsfdBgjudDHMThJiOrHRlM45Xg7hPryuhb28D9ZHR2xzS81Syg7/HJJ LLcpts7XcUTuIfKEG0/nS1yyEVe1D5ogYucvPGXZVT9VZtuPvAUQQQ7iYc7ieVv/L1Zx 3TspDZJ8S4M11wz4OGSiqyHP2bO1KaCRK1QL4ZaamNAK7yRBAoZBpYw6KNjchX0RlddJ uUtEDxBMe7qXb94GvWImtc7Tfk1SD2koAAzPNITt725241N+2bMvGRHm+jrhGwNA58qB Pv6oPn5bX6G2A6i/WsAo7ku2dTie0Ky8wGlG46e8bcW5wSyg1zVXt3NFW30pbnBnDh3v ur8g== X-Gm-Message-State: ACrzQf2uQIE2srbHABQF7lGQvLMSo6RP7x92B+kFlkMIO3BPqpX+zmM7 LjhA3x+e6KuZaenN8m6zwc0IKAj02vA= X-Google-Smtp-Source: AMsMyM6uBsA7z2khpa50VHXjQ1OsQRpz1YZswt7+7yb6nCMBvzQUnbwuUJjr2s0ZO22E81AmTxMrGHgbB8I= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a17:907:b0e:b0:77a:d97d:9afc with SMTP id h14-20020a1709070b0e00b0077ad97d9afcmr287936ejl.199.1663254354566; Thu, 15 Sep 2022 08:05:54 -0700 (PDT) Date: Thu, 15 Sep 2022 17:04:02 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-29-glider@google.com> Subject: [PATCH v7 28/43] 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 Biggers , 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 , Stephen Rothwell , 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-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=aQq72ead; spf=pass (imf19.hostedemail.com: domain of 3Uj8jYwYKCH0hmjefshpphmf.dpnmjovy-nnlwbdl.psh@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3Uj8jYwYKCH0hmjefshpphmf.dpnmjovy-nnlwbdl.psh@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254355; a=rsa-sha256; cv=none; b=ySTYUxjzlXfe1ZStnMIk0Nt7mNURIED/ph4O9NwL58lAiSWAmTpX/hvFu5sH/kn/8pa2fT 5FCxrqtrub07MS0Fg3YuKLTfAd/M25rlMyEwZjPiQdiOt22wTJlC+CW8vFYfrb6vSeeyFi hs0p5jjvqvhUIUhouBJNabpdaowKy58= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663254355; 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=r3HZXk/BEn65kcaybENH+gdA6j04yycM3f96/pfDo3Y=; b=wykkJSk/PNCiF3QW3vFe4uTXpqlMVSHTxx3hZ1otXoiiRtNAbA1s70UB4OIj8YbrHmrT6A dw8n9HgbFT5Yp1RVnrNE3pAr6t/BXpADIqhC+SvnG+wqepSoJ+YRf/1gG5X6gPO7TF0DZV 0GVii7MwfdUUPJ61AjXUb/xTtwTwGjg= X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: E65C81A008F X-Rspam-User: Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=aQq72ead; spf=pass (imf19.hostedemail.com: domain of 3Uj8jYwYKCH0hmjefshpphmf.dpnmjovy-nnlwbdl.psh@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3Uj8jYwYKCH0hmjefshpphmf.dpnmjovy-nnlwbdl.psh@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: 11o1ckrpbbjd1gosor7qq3o1o3ytwqku X-HE-Tag: 1663254355-378766 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 Thu Sep 15 15:04: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: 12977547 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 00625C6FA89 for ; Thu, 15 Sep 2022 15:05:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8BFBD8001C; Thu, 15 Sep 2022 11:05:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7F94680007; Thu, 15 Sep 2022 11:05:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 69CD68001C; Thu, 15 Sep 2022 11:05:59 -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 5593880007 for ; Thu, 15 Sep 2022 11:05:59 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 39A0E160571 for ; Thu, 15 Sep 2022 15:05:59 +0000 (UTC) X-FDA: 79914644838.20.45F3E85 Received: from mail-lf1-f73.google.com (mail-lf1-f73.google.com [209.85.167.73]) by imf13.hostedemail.com (Postfix) with ESMTP id ABF64200B4 for ; Thu, 15 Sep 2022 15:05:58 +0000 (UTC) Received: by mail-lf1-f73.google.com with SMTP id e4-20020a195004000000b004979e6a0c88so5647183lfb.22 for ; Thu, 15 Sep 2022 08:05: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:subject:date; bh=TqyxH/L5MkAtosS0jWqWllGGzUIesiLdVg3wG3p7XtY=; b=LKBPWrq4xXW7nEQB2ZmDRYo6z5ym4Le1THr8Xz3k7EBYLNOSha3Vhju0WXExFwXMZf pX6jviKwXYf/T1MVZ+5qxS4JaETzqBr+L0yjL5rYhmxXUekA0TEsSA8YRxYi5D0uDvsb NYOOkM/oK63TRvOnG9gJka71MFo0nVJFHOFee0NXtYOWDnMFjeaNsT8i5fOuOTX0XMgC 9VElsJE6GI8Iftk760oZrEHYu4XwhnR8azD6+iL8qrR3RuQo3Z8mAHYtnOhgWEQY18jM DMru+t4Du9cAUzTuKfSbPWE/QnbKa8dlrPg29e6mY17/hRx4CrdxOfA4n95mDK9E6Nlc EkVA== 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:subject:date; bh=TqyxH/L5MkAtosS0jWqWllGGzUIesiLdVg3wG3p7XtY=; b=LqZsqV5QzjKJ5oxUcph31j0yrVnPvUREZ/EsySGeQ6j2wukibX97hTknNDJxMIS+9S MJ7QP+Z+miwUU9//iVQFFShXhy7lambeEcHFWFj8JF99HfRWDRoIz+DdLoEh5XIehtXf O8FghH3TVmy2ifaOfC3YApu7zKZZy6Ye8bwwQXA1dPnpTw8+vSLpFfXr+pJRw6ZBazhM /BpFnnwcx7MDjsnTUXilrI8UfaEiUCyw/G8h4flw2dJdyGbDdVrGOjonZZYlW+fW3DK+ FuV9NKzxitZNu/3juGSp4s11Vlg8wfmFL63F0AhReAjmrEnndr6OfU9u6MROFLT3fT7L +28A== X-Gm-Message-State: ACrzQf2apvYEIjsuTzMMuDgSFsmswvV+N1/3JvITj+23HkD46WCkSsbP btjSD8hMl5iRecShqJumaCD0hcJfFp4= X-Google-Smtp-Source: AMsMyM51rNDLMIp7ozypIEsAXT0sulEDSz8gW5q4dALqMyH7lyGzgXjlA9GkTEeN37i0dxQ1XBCtNaS0ipQ= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a05:6512:3d2:b0:494:6546:dc1f with SMTP id w18-20020a05651203d200b004946546dc1fmr129949lfp.6.1663254357164; Thu, 15 Sep 2022 08:05:57 -0700 (PDT) Date: Thu, 15 Sep 2022 17:04:03 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-30-glider@google.com> Subject: [PATCH v7 29/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254358; 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=TqyxH/L5MkAtosS0jWqWllGGzUIesiLdVg3wG3p7XtY=; b=exuoC7UMww+wngKCLvcsP1ZcwScLiqCVZEKvDfGqjDlhyM664aHRp1N3g74mxv/OTdPnu+ FBaslw9OGcsS5brGMpPFSbbLEZMuiGtryBdLjv8K9zHEGgc6YqwydRw13nPRPVVVf/mvvM fLiWsRDcOxpm+95/qWCCFrkLHDEXQ2E= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=LKBPWrq4; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3VT8jYwYKCIAkpmhivksskpi.gsqpmry1-qqozego.svk@flex--glider.bounces.google.com designates 209.85.167.73 as permitted sender) smtp.mailfrom=3VT8jYwYKCIAkpmhivksskpi.gsqpmry1-qqozego.svk@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254358; a=rsa-sha256; cv=none; b=ROR+LQmkFeiyE1kw6Lk2cUWVqGUYYJwUkX0WcOU0qxQ3lvrglLus7gUgtwzb4yXGmO/rLQ ae/QeJyS6j0r1Yna1iiqUhkOicB+EmZ2w8DX5IcWxoJEf18eTGa4KCrMZs73CbZF8xHvfi MCoG0ESMt5DMM69Ims9g3OC43Enrk+I= Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=LKBPWrq4; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3VT8jYwYKCIAkpmhivksskpi.gsqpmry1-qqozego.svk@flex--glider.bounces.google.com designates 209.85.167.73 as permitted sender) smtp.mailfrom=3VT8jYwYKCIAkpmhivksskpi.gsqpmry1-qqozego.svk@flex--glider.bounces.google.com X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: ABF64200B4 X-Stat-Signature: u6f4gzff7f6gc3igq6p3s48o1urrhdr8 X-HE-Tag: 1663254358-835177 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 Thu Sep 15 15:04: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: 12977548 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 75534C6FA89 for ; Thu, 15 Sep 2022 15:06:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F01998001D; Thu, 15 Sep 2022 11:06:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EB14D80007; Thu, 15 Sep 2022 11:06:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D546B8001D; Thu, 15 Sep 2022 11:06:01 -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 C20EE80007 for ; Thu, 15 Sep 2022 11:06:01 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 8A0E51604C7 for ; Thu, 15 Sep 2022 15:06:01 +0000 (UTC) X-FDA: 79914644922.03.D1E258F Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf11.hostedemail.com (Postfix) with ESMTP id 20E1A400A0 for ; Thu, 15 Sep 2022 15:06:00 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id i17-20020a05640242d100b0044f18a5379aso13462906edc.21 for ; Thu, 15 Sep 2022 08:06: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:subject:date; bh=gorYk6QZYQqsbqX3c5FquLgSIgOkVhkBb0FBAlBlnNE=; b=kplPwV+LYMSeUsnP2/9jWw9wErn1H9lcBvU6cYlfR9a9Dsgm+kvLhjsgtxH1cQy28i BqtFI2VWtQT0leDmXz7ejpScV23UNEpG7mJUJp3p94fZxjT3BON080bQ5cWYXRzOCLrz ZUxUFyu1gz8AEgRa3BvmhVwvPqSacmkXDWPaVsqzafrIjNY67epDctUiPvU7vq5tB49w 88DUeT8ArehnGDKGPcuQSCCI6keHi8snBjlyia1r1w4gojNAEwRsQ3FJChQoxpuNNwb6 M511hTfOPRRHM1hCwd5bgSAMfa1aQqdBSl1zmfrvkVXFvA8JeL076IxXdfuOF0rLR0Cs o2vA== 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:subject:date; bh=gorYk6QZYQqsbqX3c5FquLgSIgOkVhkBb0FBAlBlnNE=; b=g+Kw7LdXGVWdIJFauFxB3UnBUi5Q5pzixaX6TeVxO/5me83Hil827sIUYQs5Mg6S1T 6DMnlPrzFqj8yWmXWRSogOsGjXxXXwYBXJnJd1f2G7WqZv9sbdTEea+PLgJZkjgXhns0 Em5sqQZwk8rhkrxYPnZU0FpRwe8hgA7PvjMQ1LEG4dpInjGaXNIduIpDD5ZFYCb3rSny ZfuxS+MK6tAztH1I1sdpzLPM+79rbT3Ynef4/GG4IkfVVNTPQWiMng1lSee8W2hBaBfu cb4d1RTSCwLxMjHZBLmhjAH/QTjPxnzT9epchbXxaO4/I6hFckDM4YHqnJ4TpPwKP3bs 9fXA== X-Gm-Message-State: ACrzQf0tNfSeRD3Ygnm0oD3y9u82glr97uLpvNGa+7wRszXW2J012GFh ExWj1kGSBQZj2C3aoei+agnG9L1rhO0= X-Google-Smtp-Source: AMsMyM7Ej6tr71uaPSqPY+N0inRBDQ4LmutJrp7lfFBWMCSzFwVfOzeQ+hQiLlB1cPu959xMS4EWDWvMK6U= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a05:6402:2201:b0:44f:443e:2a78 with SMTP id cq1-20020a056402220100b0044f443e2a78mr293434edb.76.1663254359888; Thu, 15 Sep 2022 08:05:59 -0700 (PDT) Date: Thu, 15 Sep 2022 17:04:04 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-31-glider@google.com> Subject: [PATCH v7 30/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254361; 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=gorYk6QZYQqsbqX3c5FquLgSIgOkVhkBb0FBAlBlnNE=; b=RbCm2HNnWXx0hPjOkxHnxVxwE+cS5KlLXDLUPZKWUb+CFfSP9EcKOvmETwYLV2NYwbzSwX VZ4w8wZ/q00q8llJXALzWgPW51TlcDdx7Knr3XYHHh05GqKjKzWw0JwCzk3RnOHf9jFZV2 65+9F4BBDDz0cbkAjph7FH4eCweaGlk= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=kplPwV+L; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3Vz8jYwYKCIImrojkxmuumrk.iusrot03-ssq1giq.uxm@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3Vz8jYwYKCIImrojkxmuumrk.iusrot03-ssq1giq.uxm@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254361; a=rsa-sha256; cv=none; b=MCVP00UH5gHONjBEomG5jRPvxfjG/XEliycn41TdL/cAyy/VDxbb99KeENPSDo1imhZXil YuYcng2/OdbYnUiYLLJigPSnINo3ZS0f8dwgrUUKqzX+5JmZfuOkD0fCa8X9w2blIMLuU6 GngDBkTjXK8HKnXBXMgwcA+9Lc+meWM= Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=kplPwV+L; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3Vz8jYwYKCIImrojkxmuumrk.iusrot03-ssq1giq.uxm@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3Vz8jYwYKCIImrojkxmuumrk.iusrot03-ssq1giq.uxm@flex--glider.bounces.google.com X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 20E1A400A0 X-Stat-Signature: 6euxxpo41ku959pfx311jcgpycy3eez7 X-HE-Tag: 1663254360-288376 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 Thu Sep 15 15:04: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: 12977549 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 BAE3DC6FA8B for ; Thu, 15 Sep 2022 15:06:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4A0758D0006; Thu, 15 Sep 2022 11:06:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 450948D0001; Thu, 15 Sep 2022 11:06:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2F0A68D0006; Thu, 15 Sep 2022 11:06:04 -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 22BBE8D0001 for ; Thu, 15 Sep 2022 11:06:04 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E4937C053A for ; Thu, 15 Sep 2022 15:06:03 +0000 (UTC) X-FDA: 79914645006.12.B562E47 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf25.hostedemail.com (Postfix) with ESMTP id 9F414A00D7 for ; Thu, 15 Sep 2022 15:06:03 +0000 (UTC) Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-349b78c177eso48492597b3.15 for ; Thu, 15 Sep 2022 08:06: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:subject:date; bh=mxKrKakNES2O6WNVUjUiVAG/aqg8NEyyXmfzwvDy3M8=; b=QcWSJZZkiYWs+sa+cokZ+5ih/3AigYwqha5oBzQKZX2ROZg6ZA3IUEgOs0eaSXO+su nynwTZxRJ3h5UK5bRt9ZIBKugOR9VD/wb2UQjdtResQ/B8p8IkXP6aGpAvmpsNj4uPDT g9XcO9t+eEd46cZjKpaZdtoo+KHYDdMPK4LApETzHxHqBUgFU1YKU7DIXGAmDZ5uwQk1 VZTeF44KqcNIcSibzQDkTi6BRiZq+KBK3WRtjsxy+OtraDYZ7O7tuXbkHApnF4hjaeSB xkP3yPUIvRQJsGMky0kNRvZS+Rc0renoCDGlUJjq1GKjAS9Q6aHVUEG99/PfpTuNweGG CY+w== 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:subject:date; bh=mxKrKakNES2O6WNVUjUiVAG/aqg8NEyyXmfzwvDy3M8=; b=6OPcXD84FbfafFt+TvJ5sskWJKVXR3qMfQ2CkoC+ZON2V5gfRVaGNEJzAoGNuiJOnN kmKm1bIcifspWd1oECCDU8lJMfUcUzDdj9lu2unES5g9M5s85q+wHLFvagE+WudG1nv2 jjFsJUWOiwEyDoqywA3Q1Xjq8AxM6POmB0AaPrfArvQPFK4kT0euBBwQdXdPD/dmCIBI Ow7CwbG3/1QbJ0MLkIsOmOepx1IPfRf6dSyD2irXBvnUdNP2ZaQbVZ1DzP03kSvoG55Y mPZJueCFJukKME3aawlbUnVP68ji41v3NGIXzEQo4STwAdiCmnKe/uToeqrHKuc1aPzf 5yeA== X-Gm-Message-State: ACrzQf3TU/emHsBBhTEM0rNcyTPmjXw2j3hRiia4+fWL0z8fu1n0gXXB nWaMwxAOAuMm03Jcbwh7wtPfLqmqERk= X-Google-Smtp-Source: AMsMyM5QNy6iagLQZF5XlPAvUcWfp+eaYBogp+9cAeIdO1aa1vANNHLNyhp7U4YSK9OGDcBNDqZffj+MYXA= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a81:a503:0:b0:349:f6c6:434 with SMTP id u3-20020a81a503000000b00349f6c60434mr248211ywg.70.1663254362758; Thu, 15 Sep 2022 08:06:02 -0700 (PDT) Date: Thu, 15 Sep 2022 17:04:05 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-32-glider@google.com> Subject: [PATCH v7 31/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254363; 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=mxKrKakNES2O6WNVUjUiVAG/aqg8NEyyXmfzwvDy3M8=; b=epNvJgYdZLzon8MAqXBXPftIFb3uZNW6EZ3BYKUkd0hCiD7F/zkMW17+hOPS5mUTWDQ9Sp oSN64nVhNut20IDZF4SkDgD3R7wzyiZPGkpdx1B0tIt3ETohOkdaonAcc4cxCvU4vc5nLl XK+kr+L6wg6bT7j0fO3lI//UKHF1w3I= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=QcWSJZZk; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf25.hostedemail.com: domain of 3Wj8jYwYKCIUpurmn0pxxpun.lxvurw36-vvt4jlt.x0p@flex--glider.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3Wj8jYwYKCIUpurmn0pxxpun.lxvurw36-vvt4jlt.x0p@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254363; a=rsa-sha256; cv=none; b=548fAWQUaUHBg+/uKMCz7JZ6SSbgRXdC2QFIX8i4Zbd7tu9wCJWpCKmGV1VNIiw66VMSHx KSwEaX2h3zu9GNNUIlIgmh5+FdwnGi0Lzp2Nsk9M4PWT5suDaqvdEXmKdPdTMvWZhR0Udx /opRemsHaX73P631vTWc5bEhvQGbcso= X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 9F414A00D7 X-Rspam-User: X-Stat-Signature: h8zb1tprhtfh8hce5yh9rqpkk95unsto Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=QcWSJZZk; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf25.hostedemail.com: domain of 3Wj8jYwYKCIUpurmn0pxxpun.lxvurw36-vvt4jlt.x0p@flex--glider.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3Wj8jYwYKCIUpurmn0pxxpun.lxvurw36-vvt4jlt.x0p@flex--glider.bounces.google.com X-HE-Tag: 1663254363-873467 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 e55fdf952a3a1..7c048c11ce7da 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -1062,6 +1062,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 Thu Sep 15 15:04: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: 12977550 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 90B71ECAAA1 for ; Thu, 15 Sep 2022 15:06:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2A7D78D0007; Thu, 15 Sep 2022 11:06:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 27B898D0001; Thu, 15 Sep 2022 11:06:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 143968D0007; Thu, 15 Sep 2022 11:06:08 -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 076328D0001 for ; Thu, 15 Sep 2022 11:06:08 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id DAF4C140728 for ; Thu, 15 Sep 2022 15:06:07 +0000 (UTC) X-FDA: 79914645174.21.2AAF91F Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) by imf08.hostedemail.com (Postfix) with ESMTP id 0E0961600BC for ; Thu, 15 Sep 2022 15:06:06 +0000 (UTC) Received: by mail-ej1-f74.google.com with SMTP id oz30-20020a1709077d9e00b0077239b6a915so7800530ejc.11 for ; Thu, 15 Sep 2022 08:06: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:subject:date; bh=ByRxUK8Sn+pDB+Gp2EKIbiaRYh/I0Prcbbdv1W00JlM=; b=DoOnWn76XgLh3FqF+6B7mf9zFmhgimuhE3+8V3uNL29fCgBsdcftFH3j63emu1hqBb yue1usbEsJ8tGivstviV0UDTGU37TPqiLr+h0ZVYVpBy81znrh6P3J63EnWk2dnmspuB jZiuYdHuaKBT3tl53l46EYB/n+oJtZ+X0paFYPXzKa2Gcz959+vstOCHBRj6rkF9lJ39 WVu8QAcP24QPwQp4SrjcSBBW21odhggCWPjMbNatEmym0eB7XB4lkolXQ2M5pv8V9LYN Bk2OU7qjtkBWCU7sNM4y9xnftbS8XfkQ/te4gmWOaZh49gbjQt4a+uLJe9sEzPl2IzsU U0Tg== 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:subject:date; bh=ByRxUK8Sn+pDB+Gp2EKIbiaRYh/I0Prcbbdv1W00JlM=; b=nNj0u9wqLy4WQ49Yk3Riu+BEzHz/9ctcuXIeyb3rRlpsZqoWsp5DrsaOJ1flWeXNrF bO1SiiDRPCCqME4fRxgErpSYB73GTribJ7g/9f4yzq7A7WWIRouLPfBTRmBroXb3Frij v9nn4VxDEJV6Px/WOhn85pZGoTb/m9Qg+o1RWoIFq94IH2K174AaW9HqSaFJN3CPduWb cxDK4nCiVs9RlumBUVFBd0XiyYvwROo8u/UtgYXavlhbcQOwu205eLDqR9VVGpyCU9Q5 JzFvQe9vOUsZo0HsSZ3vnhV8Uj79QuTGQ463f6mIL/IXrU+FQDq+bpBdEG1mOiMkO3FC BYMA== X-Gm-Message-State: ACrzQf1gHo7cLQO6DlTU200BZ+G4NeoVPrwkGo2zYh2B5DfxjDYQ6pvb OU6ZtYi7wxfd+R63+BhKnJHyRhmLm6Y= X-Google-Smtp-Source: AMsMyM6QJ8FdM6tWJvLpbuE+SHZYgH/q6Vk8E5GwyuArqudTGdDm2Et9aA0KgWkjKIU56rW/dUBpMAHwPK0= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a17:907:6e1c:b0:73d:7806:3c5e with SMTP id sd28-20020a1709076e1c00b0073d78063c5emr336232ejc.36.1663254365501; Thu, 15 Sep 2022 08:06:05 -0700 (PDT) Date: Thu, 15 Sep 2022 17:04:06 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-33-glider@google.com> Subject: [PATCH v7 32/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254367; 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=ByRxUK8Sn+pDB+Gp2EKIbiaRYh/I0Prcbbdv1W00JlM=; b=5CSd7tSWXEvZTOLBPTM+etEgnFjTTNHbSbJwM9yaTW1GwwMNTCQ55aWcsOoh4r73u9MzA2 ELUHLMRm5OmxVshSPFSxjCQJUsaabTXAVmLDac2WszeBfkwxyohieSP50rWRbcq632jFSU SiCIu9bYey8dgMcDnIt1J7Jq+C85lKU= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=DoOnWn76; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf08.hostedemail.com: domain of 3XT8jYwYKCIgsxupq3s00sxq.o0yxuz69-yyw7mow.03s@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3XT8jYwYKCIgsxupq3s00sxq.o0yxuz69-yyw7mow.03s@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254367; a=rsa-sha256; cv=none; b=YRFKbu0pZzPMqkALD+phRax0GBnUkASVTk3+ruiOsOM8sDxrRVptDUnHs7e+K801Iy6RH6 rvx2WCYz3tXFAPpe2pB6iyN44HB53UOzFwuxhcHIMBSzJHW5RtSp18Nn7amcQLcYvpQ0rL uwefpGPyyjo5wbDX7K/cqFp68M0Tib4= X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 0E0961600BC X-Rspam-User: X-Stat-Signature: 1rx7n4pgmstt653xz5a65q4rrb47yz9e Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=DoOnWn76; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf08.hostedemail.com: domain of 3XT8jYwYKCIgsxupq3s00sxq.o0yxuz69-yyw7mow.03s@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3XT8jYwYKCIgsxupq3s00sxq.o0yxuz69-yyw7mow.03s@flex--glider.bounces.google.com X-HE-Tag: 1663254366-636312 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 Thu Sep 15 15:04: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: 12977551 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 E5DCEC6FA89 for ; Thu, 15 Sep 2022 15:06:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 566D08D0008; Thu, 15 Sep 2022 11:06:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4EE5A8D0001; Thu, 15 Sep 2022 11:06:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 38D598D0008; Thu, 15 Sep 2022 11:06:10 -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 2BE408D0001 for ; Thu, 15 Sep 2022 11:06:10 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id D7C13409CB for ; Thu, 15 Sep 2022 15:06:09 +0000 (UTC) X-FDA: 79914645258.24.6DBA018 Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf21.hostedemail.com (Postfix) with ESMTP id 4F31E1C00BF for ; Thu, 15 Sep 2022 15:06:09 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id z9-20020a05640235c900b0044f0575e9ddso13212207edc.1 for ; Thu, 15 Sep 2022 08:06: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:subject:date; bh=VswCSWA+rV4PC5eDv8SsttMbBbTb1fmAEKvfw1RwEtM=; b=qfYRjxVXvUP5x+DcPXm3cMpN29PKGbNCTHYBFMXzeLDQrOVXXcI7lnw1mKbLyfQDyf zl3gs2m3r+JCNAr/TDs0bhFO5u7OCk1B7yPDbCbyn/nyJ8moWGvVbwEJ8b/NTADuA2hu GVJM48X6WPcJX77xHjwdMXCTk2TzlmcM2WmbJN+Tb7cJYZ8ZMxb4lsDwYDszYJI7C8rM 1v90JwvujpMn91tjws4xJKu3q+aRZi4qRprrXtU3qcvqWjX+oI7M0nFmjrmJqS8Xks0S V+NPLDQXzcvw7OagUuNKL/XuXS9vYYBVB/CqPRy/mO0mjIB75a2Iuvk5eZtREaBUnGAd /+pw== 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:subject:date; bh=VswCSWA+rV4PC5eDv8SsttMbBbTb1fmAEKvfw1RwEtM=; b=59FfpD2OrYWKua2peQjmATlxb/dmAKmINDu04XxT0sK0dexh+d0TxPCjmHb/raRXPf LkLpymF8Ndh1YkvQWh3OGGeiNSa3MKImMAyQGzRkoT/SPcyc3/kYST+LqIfCWA37lXEC Uaz2Q01xm2JyuBPBUQHepUuHTzwDDu8K4mLXowfMTN0Atxeqe2s2Zslp4tUZJoL5zZ0z U0BXsRYoU0PNT7hNpEPuuzjKKVWnF/yC5kSn/3RkKPlDu7T5tdzepVDfXrbKAw6ylGJ5 7OroFeHpS1bmb5n8Vm1xTfX0tm0Ngp7C2kDV6WEEHzTD6lwuwIzQv/bELP6OlvRrqwv1 E7hA== X-Gm-Message-State: ACrzQf1ylf38g/HTjm06vhZwF7AN1/y5wXgB0Bql9j6GbdicIlU/nmQd Cxc49OWHevoglMITF5XtAiku2oJsc/U= X-Google-Smtp-Source: AMsMyM40+Qmw8A7AGwDvjdL9jpgt8Lg9YP9jjEB7Zsvzh23DArp5ev7LtXtgv12tVm2rdXsgBXwpmJSl8vI= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a05:6402:1655:b0:44e:b208:746d with SMTP id s21-20020a056402165500b0044eb208746dmr253604edx.229.1663254368151; Thu, 15 Sep 2022 08:06:08 -0700 (PDT) Date: Thu, 15 Sep 2022 17:04:07 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-34-glider@google.com> Subject: [PATCH v7 33/43] 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 Biggers , 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 , Stephen Rothwell , 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; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=qfYRjxVX; spf=pass (imf21.hostedemail.com: domain of 3YD8jYwYKCIsv0xst6v33v0t.r310x29C-11zAprz.36v@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3YD8jYwYKCIsv0xst6v33v0t.r310x29C-11zAprz.36v@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254369; a=rsa-sha256; cv=none; b=aDL5WGieLfxDwtUCLo9hYpDj/w31Fu+j8nz2M98+34wraa6q8MZxe5iZzEfOzaPwvkvCfR ym1VBlmkHQGQIaLa5T1TjcbzYtQDRkge4qRUXKxvTTVu0Nsk3/QBNmquBnmlEg/gBEaGXh J2wBFwnJ341/MLEZdsyxzo7bMlx9JnY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663254369; 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=VswCSWA+rV4PC5eDv8SsttMbBbTb1fmAEKvfw1RwEtM=; b=Vvy0qPVlWlnDpV4m1p1GOL0SJEKwpWic9sxwQ1e4QQIby0TXX6YtKLKfXhgxkcY3jK5QD0 Eg4ZVmiXCKVhMZ03f4qpZCnv3rsNbFaqHgazj+qhRomy9JDtOYnm8gUH2arbs8rFCpFi3b 6C79adfKz1Gv8QTIl0W6B63K8nGj+8c= X-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 4F31E1C00BF Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=qfYRjxVX; spf=pass (imf21.hostedemail.com: domain of 3YD8jYwYKCIsv0xst6v33v0t.r310x29C-11zAprz.36v@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3YD8jYwYKCIsv0xst6v33v0t.r310x29C-11zAprz.36v@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: s1m8annnpaw9n8qorjimdu7h67sfsjs5 X-HE-Tag: 1663254369-338704 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 --- 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 Thu Sep 15 15:04:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12977552 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 ECF3FECAAA1 for ; Thu, 15 Sep 2022 15:06:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 84B4C8D0009; Thu, 15 Sep 2022 11:06:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 784AF8D0001; Thu, 15 Sep 2022 11:06:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 64C118D0009; Thu, 15 Sep 2022 11:06:13 -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 507118D0001 for ; Thu, 15 Sep 2022 11:06:13 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 358D11C6262 for ; Thu, 15 Sep 2022 15:06:13 +0000 (UTC) X-FDA: 79914645426.06.0F7B666 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf17.hostedemail.com (Postfix) with ESMTP id D5FE5400A7 for ; Thu, 15 Sep 2022 15:06:11 +0000 (UTC) Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-349e90aa547so13295777b3.8 for ; Thu, 15 Sep 2022 08:06: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:subject:date; bh=yflFKH40jmwCCGQluWMEQ//gU/gWIMkmRe3xdXByGVQ=; b=nHAQ6SAQ1faGRG9hCcyaUQIe9n4Ge0pIwJD55bA6+1hWT9R5/L1oJCjpqmJZktpPfg Zzf/dihTzHFGhAT9e/DSrZuoQV8vwHn9W923bX2O6zYQUYYhvbnhgiloRg7PukUjNtqq 4rFF+k8ohvV8a6XM78rLvVl2sgYj7aiX4GQ/hBOEUCoPhfBPQ/SZLt+/nkV+ankVPzU0 8z3PZ+ldmMmvDum55SGr04zcjq9E8UmqstAEXGSWTDd2uM9AOvDtxTuIIRLKoomWH6i1 KiRKZM0tStTYm/M5TBq4RUO3N3kwnZC9T4VW6TxeLO2DGHkKHIuA8r/FQKbCXKKQ/NF0 fHGA== 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:subject:date; bh=yflFKH40jmwCCGQluWMEQ//gU/gWIMkmRe3xdXByGVQ=; b=AGJjTAOS/8D3mUj5+OMiw14xdMc4tCxGgcaPj5Mr4KqLO3rwXHdzqUu05GSG3x4f3J uKrKjHDqCeFF8jMTJxaaZEJx0viPF6q4EbDOus9f/82saxkMk2noLMw66FyBgfwH2+mD 4umsQyAmGE3ISy+rgUFa/t1tqGMCi9Elp3sVxKgGy8kXysHMUsAN3Tc5qqySxfdn4Rzs 4KQsJkTjaz90Tcn3j5LhiLBdnvsplASbpooc8mxFZ1AoN+fFQIZIG3Hjf/LkFx3h4R00 8BC98SMnL29VUIARQy+38RQFncIUVWIQGRlSzlZKC7TnXVlHO5fBjlgmNMnseUi/2hJe GDbg== X-Gm-Message-State: ACrzQf22siww7cD+k61m+PzdM++n7boaZWUaWrJ5/7VjHTAHx/ACwV9z xOZwZ2zJtXWUjsHDhCtvgPniTvLDDsM= X-Google-Smtp-Source: AMsMyM6H/hSS0cPD7FS1q40ZA63aNPC7pVayxD24osgSkvd4IxyFksaB63ob13ApaIIuI+chLmajKEbvemw= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a25:9d0d:0:b0:69b:6626:6915 with SMTP id i13-20020a259d0d000000b0069b66266915mr228697ybp.294.1663254371078; Thu, 15 Sep 2022 08:06:11 -0700 (PDT) Date: Thu, 15 Sep 2022 17:04:08 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-35-glider@google.com> Subject: [PATCH v7 34/43] 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 Biggers , 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 , Stephen Rothwell , 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=nHAQ6SAQ; spf=pass (imf17.hostedemail.com: domain of 3Yz8jYwYKCI4y30vw9y66y3w.u64305CF-442Dsu2.69y@flex--glider.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3Yz8jYwYKCI4y30vw9y66y3w.u64305CF-442Dsu2.69y@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254371; a=rsa-sha256; cv=none; b=e+o9BkK/kqLHbvsKYBCSfhlFaXSJJIEiWgyd6UoqPWQQ4hyBdATPE+/2LkYBsxUTYtlniX Tq1jOI1LlUCc13FWCywZ4201YeF9C2BlZ0G/S7VfNXkM30LOitkuQplswiQJmzi/04H5nS tmY+ZCpgnSCfoWAfcW3jbugV2K+DhzQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663254371; 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=yflFKH40jmwCCGQluWMEQ//gU/gWIMkmRe3xdXByGVQ=; b=ww1LR0i52rx0NgDWLl0w54syUNxvJMvftWtvX/Dc4+kXysKXANz3XvBkJl6gakcTX//ETD 1ZODNAh620PO2b+4kXXIdqrhaxZ/SybAhLNARdOYgTEP/L2US6JH0+t/Gjfc0URzVYLMMl YSA4dUYvxwq0j2xBjwfZLz71JZ02uOY= X-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: D5FE5400A7 Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=nHAQ6SAQ; spf=pass (imf17.hostedemail.com: domain of 3Yz8jYwYKCI4y30vw9y66y3w.u64305CF-442Dsu2.69y@flex--glider.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3Yz8jYwYKCI4y30vw9y66y3w.u64305CF-442Dsu2.69y@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: 1pc19693sw9h7tx1pdjiggm3dcfhb7nz X-HE-Tag: 1663254371-465036 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 Thu Sep 15 15:04:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12977553 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 5A3FAC6FA89 for ; Thu, 15 Sep 2022 15:06:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D04F98D000A; Thu, 15 Sep 2022 11:06:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CB5A48D0001; Thu, 15 Sep 2022 11:06:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B56D48D000A; Thu, 15 Sep 2022 11:06:16 -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 A5D5D8D0001 for ; Thu, 15 Sep 2022 11:06:16 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 8A05DA03AB for ; Thu, 15 Sep 2022 15:06:16 +0000 (UTC) X-FDA: 79914645552.11.7D79C9F Received: from mail-lj1-f202.google.com (mail-lj1-f202.google.com [209.85.208.202]) by imf27.hostedemail.com (Postfix) with ESMTP id B340A4010C for ; Thu, 15 Sep 2022 15:06:15 +0000 (UTC) Received: by mail-lj1-f202.google.com with SMTP id e1-20020a2e8ec1000000b0026c27b66a2aso2094351ljl.11 for ; Thu, 15 Sep 2022 08:06: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:subject:date; bh=s0jAEbnxevXU2w5QvhShkO+su70yEEyH0HIO5IJWDY4=; b=eNKbMoJoeuDScU3OOR/LNFBkpoKPQhsynLwHhgiKEFDLcfCUexACZU4WQwQbZu34vG 7EoMGIrYWbmu1bNMt0W+B5g5HgcQObOvffkhInl291EPfXwfk6f1zh4XfEiJsxWsenlr Ed4YXhAoY4YGF7IFsdXO3XaeL5U81+P1LxeR/adMP2xTvKy7uUHwo3zzlz3YILCmQAM9 oGJR9XF/iCNgNM5pAcXM1COHTmQdzkg1GvzZO0f7nHLLKPfwPGUECgDQMa1Sx6Ary1cf j/6i5CyxJIXk+i7w4Sm1NuosHfgLH7wIyV5t0WH39UMQ7ye73Vr8soMmOIHTNweZUxjp PWwA== 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:subject:date; bh=s0jAEbnxevXU2w5QvhShkO+su70yEEyH0HIO5IJWDY4=; b=vNHlnC6+l+yT1tBn14gSB26K5jRcQenHNAmCXz2yKB2/IvX5gG64/VdunlVN9Q+kA+ EEGc1RUN0uFYrRNhWTWuJpTmhrnsa2bQfGbDDJ2OATwUEmWiHJoe5nMi/5Q1f6q7dfmF R7fTx6oo6euqELt+76zmbxWZh2WwSMUQ1gQnW96htAPm5hyuYHa23S81BSGGFqna91ZI 73FidM42pu6oZHnw8eu01vT7vt2RSobJ5QVmIBIIZTO+XI6CPLaZuSR7s6hB0w2Y6Ruz ml59ugtnrqrjLSuzjmdQH9SQbNtY5w1Tp21hUJm23q3ZuEfvusIRbTslOvm67WlqymIt 1UlQ== X-Gm-Message-State: ACrzQf3I7eVJiPF/NFQ0jopbo5NVpm7SOfTAb6wcMAoEdPpP8OuqQ1Wf mnaXmscKG48bfpKQ7s7YrV5/16CJABw= X-Google-Smtp-Source: AMsMyM5qrEYlyDR9o7INh6dG4GptGhlGiHCpycCy1PeKN2X8Ws1c8xuxIuaS5rjB9JFmB5DaKlv5ICDQnbI= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:ac2:4f03:0:b0:496:e4:4d16 with SMTP id k3-20020ac24f03000000b0049600e44d16mr117698lfr.250.1663254373987; Thu, 15 Sep 2022 08:06:13 -0700 (PDT) Date: Thu, 15 Sep 2022 17:04:09 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-36-glider@google.com> Subject: [PATCH v7 35/43] 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 Biggers , 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 , Stephen Rothwell , 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=eNKbMoJo; spf=pass (imf27.hostedemail.com: domain of 3ZT8jYwYKCJA052xyB08805y.w86527EH-664Fuw4.8B0@flex--glider.bounces.google.com designates 209.85.208.202 as permitted sender) smtp.mailfrom=3ZT8jYwYKCJA052xyB08805y.w86527EH-664Fuw4.8B0@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254375; a=rsa-sha256; cv=none; b=0RmktIzr4drqal0BDkaucVp9us8jJI7jV0ApCB0uTEeyZIIc54U5DFQAHzx4C/QjeOVXrf yC+lxWKHET0BKrWtzL4as71JbySYpDq6jgSFUw0nPzq3kszBAHUyXuI6tmnwXLu74A2Rfx aJy0qksrNa/+g6kJxw817LO/U2fM5bM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663254375; 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=s0jAEbnxevXU2w5QvhShkO+su70yEEyH0HIO5IJWDY4=; b=1NoVIpyaQpOl3JbohWgkHLEB09vpdI4d1pPh31cFUIhiQyqNOFezu/DAXmQOHKd8vfVAY4 jXVUuDmhgR4q36qv5IrD+vJwNc4py0l2Rm9bf7ClBxUL+hHCadmznN0jbXdeChf/eQUxta ca7/oT3XH9nd0TpYm9HvdAHKUY2Unlg= X-Rspam-User: X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: B340A4010C Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=eNKbMoJo; spf=pass (imf27.hostedemail.com: domain of 3ZT8jYwYKCJA052xyB08805y.w86527EH-664Fuw4.8B0@flex--glider.bounces.google.com designates 209.85.208.202 as permitted sender) smtp.mailfrom=3ZT8jYwYKCJA052xyB08805y.w86527EH-664Fuw4.8B0@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: gqyiyux4ztzenag59kahafccqx1tfznh X-HE-Tag: 1663254375-625268 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 Thu Sep 15 15:04:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12977554 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 6B404ECAAA1 for ; Thu, 15 Sep 2022 15:06:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 034848D000B; Thu, 15 Sep 2022 11:06:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F25898D0001; Thu, 15 Sep 2022 11:06:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DC6D68D000B; Thu, 15 Sep 2022 11:06: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 C33E48D0001 for ; Thu, 15 Sep 2022 11:06:18 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 93665140789 for ; Thu, 15 Sep 2022 15:06:18 +0000 (UTC) X-FDA: 79914645636.08.B17E1AA Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) by imf26.hostedemail.com (Postfix) with ESMTP id 2A3281400D0 for ; Thu, 15 Sep 2022 15:06:17 +0000 (UTC) Received: by mail-wr1-f74.google.com with SMTP id l5-20020adfa385000000b0022a482f8285so4312641wrb.5 for ; Thu, 15 Sep 2022 08:06: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:subject:date; bh=y4+04AEFcGK6lJisKBf+d7wj7/tgJIfosEPW0CJiM4s=; b=TY9zj9hsonLfxsKiJ/Nk2jseei3hK6yzpjZdnJr1tEyHclD4r1lmduXOFaXWThcHMB DJaob/V+m/PRO6csCY9Qz7+UxT/duZ0JLxrjFhCX78YuVaTTZumMHyb2TWgLeR3UbElB 7X4tMiqGIMatHsfHnsy6juWEIz1QNOu/tb14hyG2cs2B6k04dPWvwNIFfjy6W/oOeaN0 QrnIqe66ITykN//4B+h/MckE3RzTc0/Lsnyh0ahzj+igL5hjBzIYjNA8Ky1MP7CzfGn4 gexfdk9N6b5itmc9hr0TODSZeATm0DUea9IoCzI93/y47kt1AL6SpSZT+FyLO6lv3DEl 7UAg== 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:subject:date; bh=y4+04AEFcGK6lJisKBf+d7wj7/tgJIfosEPW0CJiM4s=; b=77OJIkBwsyUyokAOp/iH6rknW56e1oG/F9U/bxdesDDV+Cs1moTE9PKpS2CtjJ2292 m58JQ1lhWXFhSyg/oO7kNQiTPUyy0Nn9cDWATQEavUxOv5QJ8/WjRHJbJ2cwgst3Ldgf hBtMRMeSEpAOm5BQ1/ZuURtHdEeVutgyYZcljIN8rZwMvdoPJrv34rNxmZP6MU/1o4hA 2bIe0Y5/QqvP+snwMgRHzLvtfub289wTPl3S+fbHSiK1EexdpMXM7bRUGyPhhGVTdlN6 UD2TIwQLpidVFVrF20nviLw21/OYDJFBT2x7cJBVtDHmw4ci0y/6S85ibWKnp+hruXOL xpKQ== X-Gm-Message-State: ACrzQf2Sqr0GMPSPI2XCjY2cyXWcJ8z2hMlp//Dr5vXm8YADLSw/SBMx kzK0vvFRD8NYGJRBKiEfJlTvXvpFlKA= X-Google-Smtp-Source: AMsMyM7TO7pwHZKC5FCuc6aosYGc8fP10GsQWeB+vHWTvsz1rJA3jVBR26bmI08PTxhgRL6r8YIYohpf0Dw= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a05:6000:68c:b0:22a:bde3:f8cc with SMTP id bo12-20020a056000068c00b0022abde3f8ccmr71217wrb.556.1663254376855; Thu, 15 Sep 2022 08:06:16 -0700 (PDT) Date: Thu, 15 Sep 2022 17:04:10 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-37-glider@google.com> Subject: [PATCH v7 36/43] 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 Biggers , 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 , Stephen Rothwell , 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; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=TY9zj9hs; spf=pass (imf26.hostedemail.com: domain of 3aD8jYwYKCJM38501E3BB381.zB985AHK-997Ixz7.BE3@flex--glider.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3aD8jYwYKCJM38501E3BB381.zB985AHK-997Ixz7.BE3@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254378; a=rsa-sha256; cv=none; b=DoHdobWVI8Ucew2CJAL58rRFYhp1hiAbc/164BGW92ghT3oPrjjsLytdvxoeP0RPAVGetq 1OQWEvKKigq4N4QrVnZzHKhfXna/LX8gYPCA1rbysQE13RSZGlOKUWq7SOmN8rikEOjZVU KQ/qzgczSgzgMV0A4YdQNolb8DLqTrQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663254378; 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=y4+04AEFcGK6lJisKBf+d7wj7/tgJIfosEPW0CJiM4s=; b=xU6Fw1EsCxdPNEaKRAZV5P387pVIazfvHw7pH2nCopeVqJQqIfeZNDYmP3DuD5NuH7lK6V mST2G72B4blx+ahERkJ8G1F+7C2mItlulWhMAFGNOgBWvqwyTtB063EtJmsyrZbOBqRVaA yXOdXruB/oaQM8IHTVAAi5Hx5PSNmK0= X-Rspam-User: X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 2A3281400D0 Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=TY9zj9hs; spf=pass (imf26.hostedemail.com: domain of 3aD8jYwYKCJM38501E3BB381.zB985AHK-997Ixz7.BE3@flex--glider.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3aD8jYwYKCJM38501E3BB381.zB985AHK-997Ixz7.BE3@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: 99ssom5xkfb6zf55634fb3pogkhb4ifw X-HE-Tag: 1663254377-441601 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 Thu Sep 15 15:04:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12977555 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 B79E2ECAAA1 for ; Thu, 15 Sep 2022 15:06:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 58B728D0008; Thu, 15 Sep 2022 11:06:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4EC368D0001; Thu, 15 Sep 2022 11:06:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 38D678D0008; Thu, 15 Sep 2022 11:06:21 -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 299CF8D0001 for ; Thu, 15 Sep 2022 11:06:21 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 0E0631C317B for ; Thu, 15 Sep 2022 15:06:21 +0000 (UTC) X-FDA: 79914645762.07.F8E3AA5 Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) by imf03.hostedemail.com (Postfix) with ESMTP id B487C2009F for ; Thu, 15 Sep 2022 15:06:20 +0000 (UTC) Received: by mail-ej1-f74.google.com with SMTP id gv43-20020a1709072beb00b0077c3f58a03eso5567431ejc.4 for ; Thu, 15 Sep 2022 08:06: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:subject:date; bh=UAXLi6hWes4K0I7wTwgH8ix0vMuStYu2bu/++oTzO6o=; b=ERCi1CpV4jZif78HFY8gJQN4P8IDxcWvvUgCJpc8tDdg16sYqfI+n7+eI/UuSCUmAL SRsI5Hb91SXSeFx5sJE1WSEv/4pORbIjsWZTzgEFYcgB/oZjvlbdnJymDpzxMI9WqsSA fzkKSe+qfBYibckT/e2O3pBw/nejcxC4oW7V9C7vbDKbUxY/1oiLcrrUCzDCYPL0adsT b2RNlS/5f0ZdOsGOr5ezTbcfH4d1nDZoSyn+/iVHMh1x3nzhYUV/wDYZq+9D8wPhUAkh ifAj5YfBWK0ZWXgVZXSMG/CcCXlZscKX8+i4U7n68cPdraXGc+ejH1kH30GZoeyXCaAN BRHg== 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:subject:date; bh=UAXLi6hWes4K0I7wTwgH8ix0vMuStYu2bu/++oTzO6o=; b=TSbxmmwD+OERO4vZPHeD0uAKvd8jQoukLgzo5nC4XW6jFWbKboNNH8NJDDyfyTUmF3 gVu4h+cBQwH2SdsXU6retA5Z9K1Qqzu0HCa26Nn3hA4VsKxfTAVmaKn3GrDnI3VITKMC DRXNm5SzmsoUPv/GpL6qshEpiiKgAgbbrLLg+pXZohXAZCqJ4ZzK/Am/7LOGdE/dkAzO Pbv192rzKTk6yVTViYgMRduJAKUEArNEy4EX8KrQA4j9mkkb/tTc8kdnkxG86g2oY059 H0BdfCh1rGMzjT+JSNTA/pykLwcEYulnCY0aoHfZC/FzpPBjLY40vl0TznehB+c+6jvy FyTw== X-Gm-Message-State: ACrzQf2qEdhTg2mnznvS8rA05QQC588lUvWbUfpDVYsEp1zBo5iiR5kQ BWuSgwdVTk631uhSxw1piD8l8Eufqqk= X-Google-Smtp-Source: AMsMyM75ouYEbc3+dMPfZoPiB6oSYiohoWcuheQnJIsz7c3U6au3f9s6axieNdPO5ybuRDS+Vorz5dq1hn0= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:aa7:ca50:0:b0:44e:973b:461e with SMTP id j16-20020aa7ca50000000b0044e973b461emr269636edt.414.1663254379373; Thu, 15 Sep 2022 08:06:19 -0700 (PDT) Date: Thu, 15 Sep 2022 17:04:11 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-38-glider@google.com> Subject: [PATCH v7 37/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254380; a=rsa-sha256; cv=none; b=e/wuY617J4LIaf3val/8tRo4bgVilQscCEVdXjfk1joXBsqXSYQCGbvN48amxji5hdnIwN s0dWB5YpgJOZ7PnxsWOADBWUusGfvwFHNH7t+WdRAbBNb1dudZykNihOEeTh1q+pBG/gmv jbqYey7nprVpTVt3PTsGHYm5OpQrPEc= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=ERCi1CpV; spf=pass (imf03.hostedemail.com: domain of 3az8jYwYKCJY6B834H6EE6B4.2ECB8DKN-CCAL02A.EH6@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3az8jYwYKCJY6B834H6EE6B4.2ECB8DKN-CCAL02A.EH6@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=1663254380; 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=UAXLi6hWes4K0I7wTwgH8ix0vMuStYu2bu/++oTzO6o=; b=6C73Nm0py7Ay5pK+qQjL+VSb4Pfqd0+ONPbixRyW4vMOQFPH+kirhTt3jOimL/F0zLu55k naHCbykURCW6rWT7rZUJabHyTnqKj/zACfPnqBvrhuEUMjVeG0DlWIdIacFk9RJHt5TZaO RbzbgsVr7BoZXTANKO4mz+QVtTyofPc= Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=ERCi1CpV; spf=pass (imf03.hostedemail.com: domain of 3az8jYwYKCJY6B834H6EE6B4.2ECB8DKN-CCAL02A.EH6@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3az8jYwYKCJY6B834H6EE6B4.2ECB8DKN-CCAL02A.EH6@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: hby6so5qbtpze9fki3a6mp8xrguqkbag X-Rspamd-Queue-Id: B487C2009F X-HE-Tag: 1663254380-715617 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 Thu Sep 15 15:04:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12977556 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 860C1ECAAA1 for ; Thu, 15 Sep 2022 15:06:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 194168D000C; Thu, 15 Sep 2022 11:06:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 145778D0001; Thu, 15 Sep 2022 11:06:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F008B8D000C; Thu, 15 Sep 2022 11:06:23 -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 E25988D0001 for ; Thu, 15 Sep 2022 11:06:23 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id BD97C404DE for ; Thu, 15 Sep 2022 15:06:23 +0000 (UTC) X-FDA: 79914645846.22.BA58A62 Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf16.hostedemail.com (Postfix) with ESMTP id 6244D18009B for ; Thu, 15 Sep 2022 15:06:23 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id z2-20020a056402274200b004516734e755so10673057edd.3 for ; Thu, 15 Sep 2022 08:06: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:subject:date; bh=y7GARGHGPpfPMqaRIp9ae7D7QgvYSCiDFPeV4nzQ3Rk=; b=Ebn9L6x3k72Uf16Z2sPPbKumL6GMhSFgPt5NQNEYRWzTnLrilf1HFYWiSYnGpWzKmW FEA3UcFlSfNBEEl2K81EZ6eD7aiI4y5Lge5FU9eykMyCjFPvzA0OERI5NU1y7iIqzH4v c4bqIf2YXdWDqpJ7Z98p9HtPqrsdglQg7fUxhuQ3RQxCmBMtL3Ay3m/RSRRMUnT3krI/ r/DchyRFkiNefRiT2e63g4k5RSo2Hy1Ol6ONeQh8mlWGT0TsG9D5zangksZzjs93TeC9 m0QqPsGp0C4MAjpxPDWCraRdWM3NGQnNaRKZeuadbrgwNxMDa2D0sDAXU9zdGMOwMd5p 3N+w== 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:subject:date; bh=y7GARGHGPpfPMqaRIp9ae7D7QgvYSCiDFPeV4nzQ3Rk=; b=KUyP6C3JUB4oDjEBX9q6giyBz0Z9KBtWvPrOwAdQAXcg8FM91yLUB6zLr5OJkG50ca 5gtMs0S5fkAKLfwUv6iQ20cELQDh2v08mmYIBQoabeHk4bvm6VE6kIUio1yWmt4Yr0ih jK8QqtJV1IYEEaZFPGCb0uqgvkOf4ff/ETVINdH9AlXkPl5j7xvUQEJtWaObwy/OOdjj oZQfViMxEi4JVhz8tEfkQ80O0wExbv0Mp56OEnm11NimCE5YnIobYLehq3LLR090fIJM aDQTG/nQX8Y2MnTJXyaRLMAUAjSC0f8zCvWaNc9V3Wpg7aYYLEdBawMbwIpILMgQEF43 CAfw== X-Gm-Message-State: ACrzQf1wIdB4tBt6bvSt2TPXTDhKr6gTyYa16MQuOhg3OZoH1Wa8GClE 6YQ3C1yLC8JTy8uq89w7Lr2PEDUDRHE= X-Google-Smtp-Source: AMsMyM7TbJseKEuaq1ed2Qlb9dHi6cPdzHSmcROd4iSHRPNQzeNqnCIZX7H+dXJZfaek8LGd/2MCvKxWIXM= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a17:907:75ee:b0:77b:c559:2bcc with SMTP id jz14-20020a17090775ee00b0077bc5592bccmr266153ejc.537.1663254382083; Thu, 15 Sep 2022 08:06:22 -0700 (PDT) Date: Thu, 15 Sep 2022 17:04:12 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-39-glider@google.com> Subject: [PATCH v7 38/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254383; a=rsa-sha256; cv=none; b=n1PtCTFxHEdyf0fcnG+VHceUsfNuYd25kot/EUsygM04WpuD63b8s2TZwBEYMxUEMz9hBD 5MfwCYlaV2WFAQjp1MOhF6Lw295RSAU738AbRqAOFujxier+SUV1P7ke0KYEC6SZzTRFsf PTfQOSq9+23RMNC92WsVeiABq6oL4bM= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Ebn9L6x3; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3bj8jYwYKCJk9EB67K9HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3bj8jYwYKCJk9EB67K9HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663254383; 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=y7GARGHGPpfPMqaRIp9ae7D7QgvYSCiDFPeV4nzQ3Rk=; b=Iuf5mqdFH064mjob6zb9khl4znJd78VrsjvSRyiL8EP7voQW1t8JEvVXk0fDdx9h0IBEGf NYuD7JKFlHYQLbFC+zHtukOtl4Tt73HLV3xpwvU5gGWMjbxgeUVZpq2D3YGapp07zW7U5/ VmZX1HRUNq2MTqfsEo22mh4f5krrVLk= X-Rspam-User: Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Ebn9L6x3; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3bj8jYwYKCJk9EB67K9HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3bj8jYwYKCJk9EB67K9HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--glider.bounces.google.com X-Stat-Signature: utex86kswxoso9hk1b4utwk9sw4qz3wx X-Rspamd-Queue-Id: 6244D18009B X-Rspamd-Server: rspam09 X-HE-Tag: 1663254383-783721 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 Thu Sep 15 15:04:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12977557 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 7BD0BC6FA89 for ; Thu, 15 Sep 2022 15:06:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 16CD68D000D; Thu, 15 Sep 2022 11:06:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 120288D0001; Thu, 15 Sep 2022 11:06:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F26308D000D; Thu, 15 Sep 2022 11:06:26 -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 E04918D0001 for ; Thu, 15 Sep 2022 11:06:26 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 9D67C1A0244 for ; Thu, 15 Sep 2022 15:06:26 +0000 (UTC) X-FDA: 79914645972.22.5EFA369 Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf13.hostedemail.com (Postfix) with ESMTP id 39ADE200AC for ; Thu, 15 Sep 2022 15:06:25 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id w20-20020a05640234d400b00450f24c8ca6so13152787edc.13 for ; Thu, 15 Sep 2022 08:06: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:subject:date; bh=Rbxz0iuc7RXxWgIfjeSrHOuiWEHvXH2VAJuclXEfLdg=; b=F6VwgljKSoHlmWj1nWyCtFBHI2b5B8DMZ6H1olBVWsiyfT2kbw7W/bawpY6tA8zayB THwH3bKPewMxmxrhbJdq+mZdumidYxMIcUtzSGYEkAasnxQOXeSE7IjjawVZVIn2zcXQ r1YsO97BGqmMH/3Gm3SPgEFLbDSNRdcBrXoh3uNQ2BkTLA6uRpOGDFXhN9pCl9ZI7NBZ xhhcFg1UVJA4C2HyIq9LT8Fw+KPyPlGxH/UCui6KY5gucTlsnsRb7V38I53MLrTmhqTr tJkPsqtR6gQh2kMAprFeLbJIRRB1FXqX6TCmSGetbahnUDYy2pQaPR875kzYJHfpMY4q Y0EA== 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:subject:date; bh=Rbxz0iuc7RXxWgIfjeSrHOuiWEHvXH2VAJuclXEfLdg=; b=RgjeGALYiMk0jpbakE6cgR3K/554MClnMrAVNiEPTk18xhNjzKOVJwwuvjmXy7OnrP GJCIVneHSf6g5umPjNdRRM7u3mwxbsKDUAAdCVGYCx99FMDo6YSRY3gf++0HPDDUfZ1a x4rruhn13kImkSVY6wAGXGsaTwQbhTt0oe0bx439jTzFJmSJ8ALvLC1J47yYz2kCNERb 6oxNQlh6i19XyxzXGhLxc6zRz5EcypF/BKwdmq3yw+6zrJiw0lUOOMfLTX3OeA60jvEY npebq4UZjm5ycJGmWGWWr4DEJyQtkAayT90iJIc815OGNNmtfBIlmbGApUYGvWurwS4P mIuw== X-Gm-Message-State: ACrzQf3MVlIwvCUGiUeSbndXtz2d5CM6nr41szCxGdTWug/Nv67ShhEL +Ifsjc9dA200T7sPkWEu0xbYFFLIrT8= X-Google-Smtp-Source: AMsMyM6h5O71+6F4YlSosQQjOpItz2+q8/2EVZ42RobJy7VoqbBopgEvjyHEgtHeKPds8m8QA7fCze15fV4= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a05:6402:270f:b0:451:b5bd:95dd with SMTP id y15-20020a056402270f00b00451b5bd95ddmr251653edd.215.1663254384829; Thu, 15 Sep 2022 08:06:24 -0700 (PDT) Date: Thu, 15 Sep 2022 17:04:13 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-40-glider@google.com> Subject: [PATCH v7 39/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254386; 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=Rbxz0iuc7RXxWgIfjeSrHOuiWEHvXH2VAJuclXEfLdg=; b=f2THehcEipXr9IN/NkypjygJN/7BmvZsNk1meoG60vjhIKhQRZel81Ppc7kWnKhTNRY06g wnvtl6mXMSG1XcBV3+e8HRP2r6BN0iaWZQqo7ZpQ4oxuG5g0OYCVvXUQ3WnoEL4tbuMnrN T8NayAskcBQZPFzpDrpinbE2PeQmivc= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=F6VwgljK; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3cD8jYwYKCJsBGD89MBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3cD8jYwYKCJsBGD89MBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254386; a=rsa-sha256; cv=none; b=L/pR5tNfPVpvfH7nhM5lQWuni8hbRjRJ9Ez0yRDUcowpVOmdnIypp1EfKWO5Gpb64JL4Lb 95Llklkh9Wo+IfPLTVa9JLJchVtfdwXaUsP8QwXdelH9LyyuGjDX2JD+CrBz4lXOB7Xqsn +7akRgE31gPddLAYs6LIkBcjALDXCAM= Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=F6VwgljK; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3cD8jYwYKCJsBGD89MBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3cD8jYwYKCJsBGD89MBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--glider.bounces.google.com X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 39ADE200AC X-Stat-Signature: izpnnh91yujfwsdgsj9q98fbh6mhrz63 X-HE-Tag: 1663254385-577222 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 Thu Sep 15 15:04:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12977558 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 B0678ECAAA1 for ; Thu, 15 Sep 2022 15:06:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 516288D000E; Thu, 15 Sep 2022 11:06:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4ECDA8D0001; Thu, 15 Sep 2022 11:06:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 366A38D000E; Thu, 15 Sep 2022 11:06:30 -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 28A038D0001 for ; Thu, 15 Sep 2022 11:06:30 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 03F2A1206E3 for ; Thu, 15 Sep 2022 15:06:29 +0000 (UTC) X-FDA: 79914646140.08.3969D07 Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf15.hostedemail.com (Postfix) with ESMTP id 52FD6A00B4 for ; Thu, 15 Sep 2022 15:06:29 +0000 (UTC) Received: by mail-ed1-f73.google.com with SMTP id y1-20020a056402358100b00451b144e23eso9389505edc.18 for ; Thu, 15 Sep 2022 08:06: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:subject:date; bh=S+kwzH5LfDyg6tc+T47KWRR4zTWxpHjqyuN2TRvGEwc=; b=ikiVkwTWTLFg9q22Qo4MSxlAf8PXtNYS/YYj3pii7FEhNeb9uHoJOl8elk4rPV4ZJZ 0IGJinTNDSDUCJXFLmtqtctlrBSMtSZx+Q27jQ5t/D4KUgh+E+9+KwPOr2K16xHCw6ZT stactk3FWOzjDq3XEnZtyX+gYnhHfZkZCmjyOGFqc6N3weiBgsHifRSIeQhCUvV5vZJX CG1yQ4Azvcw+Azu27dzh37E/xbi/PjeAK04sk085Qwwj7CDPFh4bJbybx/ZYZq8xM1DR 9LGd3d+74k/4QZbbzCwxMuDFLSZDOavjAjoH2kSQnr509kCCEUIdA8blYvLSclZ5hJ2P x2mA== 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:subject:date; bh=S+kwzH5LfDyg6tc+T47KWRR4zTWxpHjqyuN2TRvGEwc=; b=nIie8zkgkl8ave/w7a2Ssb1EgrwHfge8kTE/PeJkN90NruQcynYEURI3J5TYCVNujB MOQQ5sxzGUcQ0G52QEnfsJxLFJQ2qcC0QWvbGuLx4WsCZRCBTRksAPJ0y8RKpCfBWh6s YM3KFCpz06K/Qm6s7YYa/B/B/H73Qxv3HoRkTN3MEmrb6vGaw03k1XNtWaVgac1sVlNn JIHcEvyJ0/FhtmtkkLcqFq1FyAk5GnFtprEF4mtCQaTjOUqV0uggYByO52Nbgzt8WqMh LOBH/zZOHTtV4LdGgM6nr/y0Ximk5FCuZUtr3Q5QW9PHYVOp8nSdwr78cSYFwas9LJld 7GGA== X-Gm-Message-State: ACrzQf1ds4uswTLQ0cdc6lUuviik4kOI7cyGrNkCSG8SyXkacFcUHwZt ULIFXliRCMDopNAyWN5rapVa1irqcZQ= X-Google-Smtp-Source: AMsMyM7sbunNpO+CpqIWn/hftkFrFIDp3ih3TnneGFODdNEkk+fy3fgyPU94i649AG9zEEjpdPfVfIIHuAw= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a17:906:5a5a:b0:770:86d0:fd8 with SMTP id my26-20020a1709065a5a00b0077086d00fd8mr329690ejc.164.1663254387679; Thu, 15 Sep 2022 08:06:27 -0700 (PDT) Date: Thu, 15 Sep 2022 17:04:14 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-41-glider@google.com> Subject: [PATCH v7 40/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254389; 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=S+kwzH5LfDyg6tc+T47KWRR4zTWxpHjqyuN2TRvGEwc=; b=ZzvUXnz3d5GNAVN3B03pftUuo+e5H62cCQEmYQNkKcuVSkJcD/cXFGIp/iN0MxxUoTdede uQBqs2vlcSQWLKvUfEqjXK83qHDkY+lwMiCDcdY7W42d/KhMIFMBZmKyfXkQD2128dJhTb zOboDBHsuMIKE2m5afTT+TtYR9tFsTc= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=ikiVkwTW; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf15.hostedemail.com: domain of 3cz8jYwYKCJ4EJGBCPEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3cz8jYwYKCJ4EJGBCPEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254389; a=rsa-sha256; cv=none; b=YlsCfQ/fg4uTxjd9r0kdybKRmGeXLPg7b9Zrrq/ez0fCu/IaiBpdeQbxM/ypanuR5+eWSl Af1qAwHEurSsbE0G9VkTVQHqhyOqrqSQhQQUP+2yaC987nfOhzior9GAWX0tmyzncDLBAA AnT0Hku1UOApXhA6eJsiGgumP6/RHUM= X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 52FD6A00B4 X-Rspam-User: X-Stat-Signature: onyo4nazk7ozzytm9atjd3kfd8wmiubz Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=ikiVkwTW; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf15.hostedemail.com: domain of 3cz8jYwYKCJ4EJGBCPEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3cz8jYwYKCJ4EJGBCPEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--glider.bounces.google.com X-HE-Tag: 1663254389-300683 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 c473e0e21683c..e38ae3c346184 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -214,6 +214,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) @@ -310,6 +321,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 79d7e73e2cfd8..35f6b6e6a908c 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -348,6 +348,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 Thu Sep 15 15:04:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12977559 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 01E94C6FA89 for ; Thu, 15 Sep 2022 15:06:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 90E348D000F; Thu, 15 Sep 2022 11:06:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8BD448D0001; Thu, 15 Sep 2022 11:06:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7374B8D000F; Thu, 15 Sep 2022 11:06:32 -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 6571F8D0001 for ; Thu, 15 Sep 2022 11:06:32 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 47794AAB6D for ; Thu, 15 Sep 2022 15:06:32 +0000 (UTC) X-FDA: 79914646224.23.6BFFF7E Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) by imf21.hostedemail.com (Postfix) with ESMTP id CA5E81C00B7 for ; Thu, 15 Sep 2022 15:06:31 +0000 (UTC) Received: by mail-ej1-f73.google.com with SMTP id gb9-20020a170907960900b0077d89030bb2so5492793ejc.18 for ; Thu, 15 Sep 2022 08:06: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:subject:date; bh=63Lbiss0mCSaP/JatuEwb7Qda7JVHXONR7b7gS/f0zw=; b=ldGNVPhue592Z38Ppx+dvuU/+jjZ3WM5nm+z0BSSu9h8ezP13vV/Wl1kx9FDE2kM00 8hlbbp5debq2zMwyccXnSok/EFGYS9mlFPQu03BoXGKQ1tNoqAxco9vd+gOKziowlAx1 960aMn5I15aiY7e/E9EZ/LgBtgV+tt97K0u4asdoScfhlsL/hbGWoqJyRe6AmibNWTiq J5lFB1m0MSx5XYBhZJDDB1ZWprW7OT9cPgi73vgc4CYvrUnMokBrBHdVIT4OYBHWdzX6 Wx/pz4G0JRiQM/VifFqL0HQwwn8gpGYoshZYX49Ahm0NtbyPpOr/BuoO7lm2mFArdxjX g3qg== 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:subject:date; bh=63Lbiss0mCSaP/JatuEwb7Qda7JVHXONR7b7gS/f0zw=; b=yh2/fR7wUetfMfGPbyylT66xKv6UYynsfdfZfNqhNbesEvIiLfkj7jytW990OQzdmX vsL7WBCWS9gjrq55yvStthw3eznAkMtx3YNFMX1Ef5bGXKCiB0B36MM/W1dsVxKE7ov8 vkpHIuJr/DqfSEMrizWLZHJJwhSanizLqgNfDK18zCgYvW9oVJGCU4Eosdx5SHf/ORyy 5ayYzempdbejCEkjFF6JrJPnARqcVAjgr/FvO91EZsdIcNhJdX7K/n/uzCjB7muSSFiB vGea2en7/0m8jYV6cOSIMScqp69KZk307Wjxyelk91LdWkrYPqO2c2Ol7Dy0Aa/p5XNz SiKQ== X-Gm-Message-State: ACrzQf3007YLfil9jE3nu0CXIRqAHZ8DQp0bLnehdV3VB/ynX7vzSbYW b1uu+sNuxDJq6vTH9qnymg41WqreS/8= X-Google-Smtp-Source: AMsMyM7rMyupFoo64/C2rdfMBkqr7i79dpaceIzsK7wAJRHKNpXAyoVQnsUTDRv/eo0QEd57U+v7OZRoLF0= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:aa7:c1c4:0:b0:44e:b39e:2a54 with SMTP id d4-20020aa7c1c4000000b0044eb39e2a54mr259911edp.139.1663254390559; Thu, 15 Sep 2022 08:06:30 -0700 (PDT) Date: Thu, 15 Sep 2022 17:04:15 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-42-glider@google.com> Subject: [PATCH v7 41/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254391; 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=63Lbiss0mCSaP/JatuEwb7Qda7JVHXONR7b7gS/f0zw=; b=SdtBxsIFzLDQBtj9MxNQbLox0fdwPMQvrXbd9gYRkob4Iwl0DFMDI1vyUbv34lE3mOwQc4 Zofp7iaxmqt4/StUVjgMMZ7GGSo66HleZZsYPA9hn2/IqeeTvMoD/N24DpZgzVhXN5k+JX xQGVltJPwe9ZZFJ6tBjB1mS9YqbTQJI= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=ldGNVPhu; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of 3dj8jYwYKCKEHMJEFSHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3dj8jYwYKCKEHMJEFSHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254391; a=rsa-sha256; cv=none; b=gvPT10oNIZh2FKkbCnotQn01BAGb+HPTlynj7IhtEXFB3/7Ag+iqLfShrjzNgaHOMN6DrJ S46DsxkNkMSXR1/nB+nptoH5jtZnd1htGYVtZSHpW1qPTNhH6InB4KoftioeVtslu84RQf E/LZQcYzuZtVgZxbp2ptgyMJultlgjE= Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=ldGNVPhu; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of 3dj8jYwYKCKEHMJEFSHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3dj8jYwYKCKEHMJEFSHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--glider.bounces.google.com X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: CA5E81C00B7 X-Stat-Signature: i37drq6tg9cpdgmtpr11zfcqwbb8hdp1 X-HE-Tag: 1663254391-255574 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 3d9eb3ae334ce..21c74fac5131c 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 Thu Sep 15 15:04:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12977560 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 94D44ECAAA1 for ; Thu, 15 Sep 2022 15:06:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 27EF38D0010; Thu, 15 Sep 2022 11:06:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1E0348D0001; Thu, 15 Sep 2022 11:06:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 032E98D0010; Thu, 15 Sep 2022 11:06:34 -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 E79ED8D0001 for ; Thu, 15 Sep 2022 11:06:34 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C67E0120409 for ; Thu, 15 Sep 2022 15:06:34 +0000 (UTC) X-FDA: 79914646308.28.2C2D2FB Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf20.hostedemail.com (Postfix) with ESMTP id 63A1B1C00CA for ; Thu, 15 Sep 2022 15:06:34 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id m13-20020a056402510d00b004519332f0b1so9678542edd.7 for ; Thu, 15 Sep 2022 08:06: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:subject:date; bh=RV5QTvMygwcM31S/hyrs6MnTkve28DlweGTrQchhxXo=; b=iXaNCvcxi4+6qBN7tuBlJCkmsRSEsjGSfB3SfZeymm0XXHHMDLmZS5purDypcnvs9C VSctdYvWkvSVdi7VcQVA/jXEY/asOBe0sjr7IHcePpfoj8cjbpyxzwMKoub6Mbfbj/u3 hdcX9KTPHnInN6LBhE2giGnIHgk4ArxBm8oNZQjrxTqj6l10QwAzi4OrPQG/dmLjnKeM uG5cIFQDMadCU8TrBcbzjSQl+YJOZv/uTPg3+I9KzZbdQlENHqwWW95ACZVZT9BbHfi5 bSJ9Yx8C2yAe2rvI5sDbQK6kyk//DTTYryP9Ky1nwr9gD2c+Xh/G4lERiS/g/gDCekau srrQ== 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:subject:date; bh=RV5QTvMygwcM31S/hyrs6MnTkve28DlweGTrQchhxXo=; b=Cg8ITyFdvvpvE3wJ9JSy9hJelDoCfI8JOcdtDaoqjG8UWOzzPuQqwoyyDbM1mNuUjJ g9fktOiPqAvIat28CNEg+m1AzUOcF5Jv/N7A9PFOQJ31iVlfHX+bCv79EgH8teU/Qeqv pS9FSvJ813P1ZWDCRJ7LWKECll1gMy8e5SQmIs8OFgc11uBU9CEk7ycC/jHTQHFf280D fGkHuVAz6U/UuH2FqpyKXYD7sMcWAWPmaUdjR10wyAPAlFda+mjXWkRxFM7uw13/RBx4 vesQlfhb12qiroQMFwU4bZVHL7X3Ch7lKe4AJ8kH2Wfr8uxEFDgJm7YtuydOIzYmqZ1f Calw== X-Gm-Message-State: ACrzQf1fSzCJexbGUL5qPA9xsVHBXtE8DGQV/iN5TWB1hLcB3SQfF4/B HRW/1ZCxKAe82z2G2HoLRM/UEedjD3I= X-Google-Smtp-Source: AMsMyM7bqtOZHqD5TLNiPJxrMbK30CsOC2CMkBTDAk4Y4jRe7EslTuJmgx6bYvgk5Yw3mmLO7YowFoN6c2M= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a05:6402:4306:b0:451:8034:bcb6 with SMTP id m6-20020a056402430600b004518034bcb6mr291044edc.198.1663254393224; Thu, 15 Sep 2022 08:06:33 -0700 (PDT) Date: Thu, 15 Sep 2022 17:04:16 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-43-glider@google.com> Subject: [PATCH v7 42/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254394; 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=RV5QTvMygwcM31S/hyrs6MnTkve28DlweGTrQchhxXo=; b=3ke9Kv31zWcQiUMKtfqHbKbEBMxesPUDfALNk+A7uU3RwOLhhs8W/gyacgZ8Bu4kvtv5pc 2DmJip1gAbWW4ssd44A8OMzA0oK4yKJIQjnrJPnJkLfyjza0pn4ermToV3cf9ifAqnN1vH WxQlAHE374Mnzv4NnXgZpzVUBq66exc= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=iXaNCvcx; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf20.hostedemail.com: domain of 3eT8jYwYKCKQKPMHIVKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3eT8jYwYKCKQKPMHIVKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254394; a=rsa-sha256; cv=none; b=6YyQRsxGOE6tVaNAT51kHCOwjuEXzBVYJlIg2ZnTE2dbq5TM91P+0n+KFECozv+iTkcUsG MSwtbPUofd9Q2nu95p5HdW7iNaXDurQSIPrqBY82KPI3m4Yc88mHcLPirkb5Peymm6pJOV 4+xCjb8/VIgty9owjHSco0hTT1SECwU= Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=iXaNCvcx; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf20.hostedemail.com: domain of 3eT8jYwYKCKQKPMHIVKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3eT8jYwYKCKQKPMHIVKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--glider.bounces.google.com X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 63A1B1C00CA X-Stat-Signature: 1haqdsi8cbuxfyd69rk7tino15y4aqax X-HE-Tag: 1663254394-447067 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 Thu Sep 15 15:04:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12977561 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 4F53EECAAA1 for ; Thu, 15 Sep 2022 15:06:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DC9838D0011; Thu, 15 Sep 2022 11:06:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D78108D0001; Thu, 15 Sep 2022 11:06:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C1B288D0011; Thu, 15 Sep 2022 11:06:37 -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 AF1F28D0001 for ; Thu, 15 Sep 2022 11:06:37 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 89096A0757 for ; Thu, 15 Sep 2022 15:06:37 +0000 (UTC) X-FDA: 79914646434.10.6852A98 Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) by imf07.hostedemail.com (Postfix) with ESMTP id 28A2B400A2 for ; Thu, 15 Sep 2022 15:06:36 +0000 (UTC) Received: by mail-ej1-f74.google.com with SMTP id hp18-20020a1709073e1200b0078054727517so1236355ejc.0 for ; Thu, 15 Sep 2022 08:06: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:subject:date; bh=qWMR0SPZO0jqHHxoNfMsk5w/9uuaYeqiCUk8OeqbWGY=; b=BdvNjfu5VP4f5tWEwqi8xdnrQQzMfo1UN5bQGxgPeuYIRiSF6R+M9tMaqYwiS2tecV O8LqTtykzfkn0aNHdYp3DfA09eKoKkOI/meN41FcTkWmKDI6Mlw1uBbBDJ5ItwRLsI99 fu9mFtJanTVQjiray0U8TbivD9S30FZLPiX0FzoqX/uQC4kH7HtwMZ/blaYA0jDerrjW aPeP8F3DEIDRdPbDMG4HF5n1PQvJNaW7R/M0TYz0hhV4XUWjPxlr/ASgkKiR/2NytbwG /9KCbWlhcUnlvPN/s95vfn0Q7Y+MSgUo6FfARpJ1769HCSJLQ4f+8vfKDIdzDiQ+Hfop cqfQ== 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:subject:date; bh=qWMR0SPZO0jqHHxoNfMsk5w/9uuaYeqiCUk8OeqbWGY=; b=XnH19hHVKMG3vp/SJnOBTibpAbKMvKBUkAXqlcPavvqTBk1LzRabfdWpSNluuZE0oN jh8ejbWBq8ZDshxGIuMitfVX/k/X89KwX7lpuTLoBDZx8A5WSFGl1T6CoYmCvgI6YWNX /zwQBo1w05VxfqQxX3upC3FBX17lZIhunc9eBMEBY8gFQBXKGuz5FGCs1XRQkz4boEXc b00CLDiCf1wCuG0Nq2t7p8jXbuNmGLXILATTF0EWnWV1t7aMj/nb0pHG/hIcDkeL90mQ gLSOvzGpLWvz/NTpKViEtJVLso8+LcG99so3IITqnmEiTuY+6zrePZtuqroMj6pCQsfc pplQ== X-Gm-Message-State: ACrzQf2i0KP43TI7KoBSKRX8EnUslVMYlC+YhbFPyaVt4SMK1IXFNldK qDU/zAepZ/PQAt+iU9h0YwQ0Zz/zDn4= X-Google-Smtp-Source: AMsMyM6ZSkiMDie9cIFnYKrzA4Gojr+aLMK8obg1m96Wr/dl+paR5zDF6a+Yr1FIkBtdJbuKIaikj76ZeVI= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a17:907:a0e:b0:780:72bb:5ce4 with SMTP id bb14-20020a1709070a0e00b0078072bb5ce4mr321825ejc.234.1663254395778; Thu, 15 Sep 2022 08:06:35 -0700 (PDT) Date: Thu, 15 Sep 2022 17:04:17 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-44-glider@google.com> Subject: [PATCH v7 43/43] 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 Biggers , 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 , Stephen Rothwell , 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=1663254397; a=rsa-sha256; cv=none; b=GIg40g4blV1Xbxm6mux/FXJIFlcoYRQRGdnycNdequrQHgnGSWzy0l7HXBlnMLVTESP4lc UqOP8dl71cMmdMexZnOyAFpt/YvdYkUTufJ4J+imBYHfRwFuc4MUcggdETUTIF8iQGEgxz LOoG/wOCb2ovDXr2728Y/l4wXLgiXpM= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=BdvNjfu5; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 3ez8jYwYKCKYMROJKXMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3ez8jYwYKCKYMROJKXMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663254397; 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=qWMR0SPZO0jqHHxoNfMsk5w/9uuaYeqiCUk8OeqbWGY=; b=BHm/VtG+Wm2CVPaAJKgnKO0Wh9F+iz9cFpAr6Lh58K9ovsJy+ng421YD75akVJZGq7+N7z ogOWhFxbCnpomhiV4CYXsL7TMYwnnJnHy760HP84Kq/++7kVa1QGnYyfD3sdwsIEQacLUg jAO8nrl75IgXvWDIgy0yP+KhaWXDs1g= X-Rspam-User: Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=BdvNjfu5; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 3ez8jYwYKCKYMROJKXMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3ez8jYwYKCKYMROJKXMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--glider.bounces.google.com X-Stat-Signature: nsfga49fs7ueres96wmsfh4ymt8xka8g X-Rspamd-Queue-Id: 28A2B400A2 X-Rspamd-Server: rspam09 X-HE-Tag: 1663254396-80017 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 */