From patchwork Fri Aug 26 15:08:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956217 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 805EEECAAA3 for ; Fri, 26 Aug 2022 15:10:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1CDAF940018; Fri, 26 Aug 2022 11:10:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1567D940007; Fri, 26 Aug 2022 11:10:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EEB0B940018; Fri, 26 Aug 2022 11:10:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id D8ED9940007 for ; Fri, 26 Aug 2022 11:10:17 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id B67A840593 for ; Fri, 26 Aug 2022 15:10:17 +0000 (UTC) X-FDA: 79842079674.20.95F456B Received: from mail-lj1-f201.google.com (mail-lj1-f201.google.com [209.85.208.201]) by imf14.hostedemail.com (Postfix) with ESMTP id 527D6100022 for ; Fri, 26 Aug 2022 15:10:17 +0000 (UTC) Received: by mail-lj1-f201.google.com with SMTP id z11-20020a2eb52b000000b00261d940ce36so655843ljm.9 for ; Fri, 26 Aug 2022 08:10:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=fxlXGK8qL+YVfFcW6ClvjL4YS8Ri9qaHIYNR2qoUuzA=; b=Q8VLjQ5xsWoRQwcBX26q4ZfJywgvwwYMqm7WRIZT/igOO0C+hQHvWJdj6bKE/htghx DrY+lrNXNEbosReTbTCbvhlRqJK2QXAnzf9wyBVGEBrx0xxr82tqOecv4nBtt+ZxQ/yz olXwbcf4S8A0zqAksvcEzulatqIcfYttRp+CQJcmIHJmyH6BPQSBoduAFmbbmZi1yVjp Tg6Bb8K5bppJxRqKIw0j6if6T//cGZVzeA7QuH2nxxvPggC9uSXoPvQi5rO7W7h+JR7N w+bOFVhYur+hDrR+w9sC1KCjIT/m0VnGKDSplsMs4+EUNYFwUHkEBbvxDpMspo6USKZQ VmLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=fxlXGK8qL+YVfFcW6ClvjL4YS8Ri9qaHIYNR2qoUuzA=; b=Wy5QWxcR3D3hXQkTAs2C+JAXiND8s+GAY4pdJfb/WBRnBwTssLEdVKduwJWZbGcF4B HDWT1n2rsTKcpN1SmOgm0E8uPcRDFzVd6fis+JTCbevf0O7BCndYfmWW0Pyk08/n3m0y 0t+0fR5ytp/jeu9IKNfIjLamHOp1yMksSCqyFhKLliHgBTiWgIvSs0Zlzh0WuBVlC0S4 zA9+GWUbydUhJyrVwYefRQXbF20SsRbbPcSHakjCA55GMRUMKCoj5zOG+UzDbkqY96iJ bzblBD2aFws6FG38ovNVs5tNHycMMFri364JMw0hUtkKHMpq0KDQ5ewIV4WJ2d74mZtl a3XQ== X-Gm-Message-State: ACgBeo36idZt5AIxQbRNC7DC+QenoTZCyE3DmxLs8dIfkMaa1yCRmi+v RlJ/jXfzTWmos7fADpjstQCNZ2KzGYE= X-Google-Smtp-Source: AA6agR4ts2QVsl2UWCVwazOIhjWaaZlRudAXUf4/lJNteJtVBKT5mB6+2t9fiOdhKBlLEptewn0gZtbEsjs= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a19:e012:0:b0:492:bec1:7f9d with SMTP id x18-20020a19e012000000b00492bec17f9dmr2498937lfg.585.1661526615782; Fri, 26 Aug 2022 08:10:15 -0700 (PDT) Date: Fri, 26 Aug 2022 17:08:07 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-45-glider@google.com> Subject: [PATCH v5 44/44] x86: kmsan: enable KMSAN builds for x86 From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , Andrew Morton , Andrey Konovalov , Andy Lutomirski , Arnd Bergmann , Borislav Petkov , Christoph Hellwig , Christoph Lameter , David Rientjes , Dmitry Vyukov , Eric Dumazet , Greg Kroah-Hartman , Herbert Xu , Ilya Leoshkevich , Ingo Molnar , Jens Axboe , Joonsoo Kim , Kees Cook , Marco Elver , Mark Rutland , Matthew Wilcox , "Michael S. Tsirkin" , Pekka Enberg , Peter Zijlstra , Petr Mladek , Steven Rostedt , Thomas Gleixner , Vasily Gorbik , Vegard Nossum , Vlastimil Babka , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526617; a=rsa-sha256; cv=none; b=VrBi+Dod7TpRLZkk+iHlWJklvVQwJhEPLseaDxWEN77woxhxu5+ta0Xw5Ge2VmNLJakmZk V9a31uGKJsBJ87KIPrID0N5N7bvJmg6SykJGlxeeZ/CkKzvFPfzpcfLaOn+eaWV9+bcgvu UXiiFhf9W0+JuhUv9xhOUtYiDqdGhSc= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Q8VLjQ5x; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf14.hostedemail.com: domain of 3V-IIYwYKCF4CHE9ANCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--glider.bounces.google.com designates 209.85.208.201 as permitted sender) smtp.mailfrom=3V-IIYwYKCF4CHE9ANCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526617; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=fxlXGK8qL+YVfFcW6ClvjL4YS8Ri9qaHIYNR2qoUuzA=; b=oZAfPotbTdvRg9LdOOkirlMqZbOIsxZJHlaHm+1W3wNNDj2739wA9D4ng4DbUdqcfd/zlD p5u+fEJ4QMX6vND4DEA9MZXAJvZm9vuWw4tG64dv7M+AQyRmM5TO6XY+Gp7jf5s6w0lnnt 6EuS0pNw2DdxL6ER9Ff8n3MKVt+vLag= X-Rspamd-Queue-Id: 527D6100022 Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Q8VLjQ5x; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf14.hostedemail.com: domain of 3V-IIYwYKCF4CHE9ANCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--glider.bounces.google.com designates 209.85.208.201 as permitted sender) smtp.mailfrom=3V-IIYwYKCF4CHE9ANCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--glider.bounces.google.com X-Rspamd-Server: rspam12 X-Rspam-User: X-Stat-Signature: haogdyyj55ta1yxcgj5sboj4fc9fzbit X-HE-Tag: 1661526617-177795 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Make KMSAN usable by adding the necessary Kconfig bits. Also declare x86-specific functions checking address validity in arch/x86/include/asm/kmsan.h. Signed-off-by: Alexander Potapenko --- v4: -- per Marco Elver's request, create arch/x86/include/asm/kmsan.h and move arch-specific inline functions there. Link: https://linux-review.googlesource.com/id/I1d295ce8159ce15faa496d20089d953a919c125e --- arch/x86/Kconfig | 1 + arch/x86/include/asm/kmsan.h | 55 ++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 arch/x86/include/asm/kmsan.h diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 697da8dae1418..bd9436cd0f29b 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -168,6 +168,7 @@ config X86 select HAVE_ARCH_KASAN if X86_64 select HAVE_ARCH_KASAN_VMALLOC if X86_64 select HAVE_ARCH_KFENCE + select HAVE_ARCH_KMSAN if X86_64 select HAVE_ARCH_KGDB select HAVE_ARCH_MMAP_RND_BITS if MMU select HAVE_ARCH_MMAP_RND_COMPAT_BITS if MMU && COMPAT diff --git a/arch/x86/include/asm/kmsan.h b/arch/x86/include/asm/kmsan.h new file mode 100644 index 0000000000000..a790b865d0a68 --- /dev/null +++ b/arch/x86/include/asm/kmsan.h @@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * x86 KMSAN support. + * + * Copyright (C) 2022, Google LLC + * Author: Alexander Potapenko + */ + +#ifndef _ASM_X86_KMSAN_H +#define _ASM_X86_KMSAN_H + +#ifndef MODULE + +#include +#include + +/* + * Taken from arch/x86/mm/physaddr.h to avoid using an instrumented version. + */ +static inline bool kmsan_phys_addr_valid(unsigned long addr) +{ + if (IS_ENABLED(CONFIG_PHYS_ADDR_T_64BIT)) + return !(addr >> boot_cpu_data.x86_phys_bits); + else + return true; +} + +/* + * Taken from arch/x86/mm/physaddr.c to avoid using an instrumented version. + */ +static inline bool kmsan_virt_addr_valid(void *addr) +{ + unsigned long x = (unsigned long)addr; + unsigned long y = x - __START_KERNEL_map; + + /* use the carry flag to determine if x was < __START_KERNEL_map */ + if (unlikely(x > y)) { + x = y + phys_base; + + if (y >= KERNEL_IMAGE_SIZE) + return false; + } else { + x = y + (__START_KERNEL_map - PAGE_OFFSET); + + /* carry flag will be set if starting x was >= PAGE_OFFSET */ + if ((x > y) || !kmsan_phys_addr_valid(x)) + return false; + } + + return pfn_valid(x >> PAGE_SHIFT); +} + +#endif /* !MODULE */ + +#endif /* _ASM_X86_KMSAN_H */